xref: /titanic_44/usr/src/cmd/zoneadm/zoneadm.c (revision b6831eafa2ec2cb283f783b06ee86bf79d5a17c3)
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.
243c7284bdSAlexander Eremin  * Copyright 2014 Nexenta Systems, Inc. All rights reserved.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate /*
287c478bd9Sstevel@tonic-gate  * zoneadm is a command interpreter for zone administration.  It is all in
297c478bd9Sstevel@tonic-gate  * C (i.e., no lex/yacc), and all the argument passing is argc/argv based.
307c478bd9Sstevel@tonic-gate  * main() calls parse_and_run() which calls cmd_match(), then invokes the
317c478bd9Sstevel@tonic-gate  * appropriate command's handler function.  The rest of the program is the
327c478bd9Sstevel@tonic-gate  * handler functions and their helper functions.
337c478bd9Sstevel@tonic-gate  *
347c478bd9Sstevel@tonic-gate  * Some of the helper functions are used largely to simplify I18N: reducing
357c478bd9Sstevel@tonic-gate  * the need for translation notes.  This is particularly true of many of
367c478bd9Sstevel@tonic-gate  * the zerror() calls: doing e.g. zerror(gettext("%s failed"), "foo") rather
377c478bd9Sstevel@tonic-gate  * than zerror(gettext("foo failed")) with a translation note indicating
387c478bd9Sstevel@tonic-gate  * that "foo" need not be translated.
397c478bd9Sstevel@tonic-gate  */
407c478bd9Sstevel@tonic-gate 
417c478bd9Sstevel@tonic-gate #include <stdio.h>
427c478bd9Sstevel@tonic-gate #include <errno.h>
437c478bd9Sstevel@tonic-gate #include <unistd.h>
447c478bd9Sstevel@tonic-gate #include <signal.h>
457c478bd9Sstevel@tonic-gate #include <stdarg.h>
467c478bd9Sstevel@tonic-gate #include <ctype.h>
477c478bd9Sstevel@tonic-gate #include <stdlib.h>
487c478bd9Sstevel@tonic-gate #include <string.h>
497c478bd9Sstevel@tonic-gate #include <wait.h>
507c478bd9Sstevel@tonic-gate #include <zone.h>
517c478bd9Sstevel@tonic-gate #include <priv.h>
527c478bd9Sstevel@tonic-gate #include <locale.h>
537c478bd9Sstevel@tonic-gate #include <libintl.h>
547c478bd9Sstevel@tonic-gate #include <libzonecfg.h>
557c478bd9Sstevel@tonic-gate #include <bsm/adt.h>
569acbbeafSnn35248 #include <sys/brand.h>
577c478bd9Sstevel@tonic-gate #include <sys/param.h>
587c478bd9Sstevel@tonic-gate #include <sys/types.h>
597c478bd9Sstevel@tonic-gate #include <sys/stat.h>
607c478bd9Sstevel@tonic-gate #include <sys/statvfs.h>
617c478bd9Sstevel@tonic-gate #include <assert.h>
627c478bd9Sstevel@tonic-gate #include <sys/sockio.h>
637c478bd9Sstevel@tonic-gate #include <sys/mntent.h>
647c478bd9Sstevel@tonic-gate #include <limits.h>
650b5de56dSgjelinek #include <dirent.h>
66555afedfScarlsonj #include <uuid/uuid.h>
677c478bd9Sstevel@tonic-gate #include <fcntl.h>
687c478bd9Sstevel@tonic-gate #include <door.h>
697c478bd9Sstevel@tonic-gate #include <macros.h>
707c478bd9Sstevel@tonic-gate #include <libgen.h>
71865e09a4Sgjelinek #include <fnmatch.h>
72e7f3c547Sgjelinek #include <sys/modctl.h>
739acbbeafSnn35248 #include <libbrand.h>
740209230bSgjelinek #include <libscf.h>
757ef01d19Sgjelinek #include <procfs.h>
76d9e728a2Sgjelinek #include <strings.h>
777c478bd9Sstevel@tonic-gate #include <pool.h>
787c478bd9Sstevel@tonic-gate #include <sys/pool.h>
790209230bSgjelinek #include <sys/priocntl.h>
800209230bSgjelinek #include <sys/fsspriocntl.h>
812b24ab6bSSebastien Roy #include <libdladm.h>
822b24ab6bSSebastien Roy #include <libdllink.h>
83cb8a054bSGlenn Faden #include <pwd.h>
84cb8a054bSGlenn Faden #include <auth_list.h>
85cb8a054bSGlenn Faden #include <auth_attr.h>
86cb8a054bSGlenn Faden #include <secdb.h>
877c478bd9Sstevel@tonic-gate 
880b5de56dSgjelinek #include "zoneadm.h"
890b5de56dSgjelinek 
907c478bd9Sstevel@tonic-gate #define	MAXARGS	8
91cb8a054bSGlenn Faden #define	SOURCE_ZONE (CMD_MAX + 1)
927c478bd9Sstevel@tonic-gate 
937c478bd9Sstevel@tonic-gate /* Reflects kernel zone entries */
947c478bd9Sstevel@tonic-gate typedef struct zone_entry {
957c478bd9Sstevel@tonic-gate 	zoneid_t	zid;
967c478bd9Sstevel@tonic-gate 	char		zname[ZONENAME_MAX];
977c478bd9Sstevel@tonic-gate 	char		*zstate_str;
987c478bd9Sstevel@tonic-gate 	zone_state_t	zstate_num;
999acbbeafSnn35248 	char		zbrand[MAXNAMELEN];
1007c478bd9Sstevel@tonic-gate 	char		zroot[MAXPATHLEN];
101555afedfScarlsonj 	char		zuuid[UUID_PRINTABLE_STRING_LENGTH];
102f4b3ec61Sdh155122 	zone_iptype_t	ziptype;
1037c478bd9Sstevel@tonic-gate } zone_entry_t;
1047c478bd9Sstevel@tonic-gate 
10584561e8cStd153743 #define	CLUSTER_BRAND_NAME	"cluster"
10684561e8cStd153743 
1077c478bd9Sstevel@tonic-gate static zone_entry_t *zents;
1087c478bd9Sstevel@tonic-gate static size_t nzents;
1097c478bd9Sstevel@tonic-gate 
1101390a385Sgjelinek #define	LOOPBACK_IF	"lo0"
1111390a385Sgjelinek #define	SOCKET_AF(af)	(((af) == AF_UNSPEC) ? AF_INET : (af))
1121390a385Sgjelinek 
1131390a385Sgjelinek struct net_if {
1141390a385Sgjelinek 	char	*name;
1151390a385Sgjelinek 	int	af;
1161390a385Sgjelinek };
1171390a385Sgjelinek 
1187c478bd9Sstevel@tonic-gate /* 0755 is the default directory mode. */
1197c478bd9Sstevel@tonic-gate #define	DEFAULT_DIR_MODE \
1207c478bd9Sstevel@tonic-gate 	(S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH)
1217c478bd9Sstevel@tonic-gate 
1227c478bd9Sstevel@tonic-gate struct cmd {
1237c478bd9Sstevel@tonic-gate 	uint_t	cmd_num;				/* command number */
1247c478bd9Sstevel@tonic-gate 	char	*cmd_name;				/* command name */
1257c478bd9Sstevel@tonic-gate 	char	*short_usage;				/* short form help */
1267c478bd9Sstevel@tonic-gate 	int	(*handler)(int argc, char *argv[]);	/* function to call */
1277c478bd9Sstevel@tonic-gate 
1287c478bd9Sstevel@tonic-gate };
1297c478bd9Sstevel@tonic-gate 
1307c478bd9Sstevel@tonic-gate #define	SHELP_HELP	"help"
1313f2f09c1Sdp #define	SHELP_BOOT	"boot [-- boot_arguments]"
1327c478bd9Sstevel@tonic-gate #define	SHELP_HALT	"halt"
1337c478bd9Sstevel@tonic-gate #define	SHELP_READY	"ready"
1343c7284bdSAlexander Eremin #define	SHELP_SHUTDOWN	"shutdown [-r [-- boot_arguments]]"
1353f2f09c1Sdp #define	SHELP_REBOOT	"reboot [-- boot_arguments]"
1367c478bd9Sstevel@tonic-gate #define	SHELP_LIST	"list [-cipv]"
1377c478bd9Sstevel@tonic-gate #define	SHELP_VERIFY	"verify"
138ad02e316Sbatschul #define	SHELP_INSTALL	"install [brand-specific args]"
139ff17c8bfSgjelinek #define	SHELP_UNINSTALL	"uninstall [-F] [brand-specific args]"
140ff17c8bfSgjelinek #define	SHELP_CLONE	"clone [-m method] [-s <ZFS snapshot>] "\
141ff17c8bfSgjelinek 	"[brand-specific args] zonename"
142865e09a4Sgjelinek #define	SHELP_MOVE	"move zonepath"
143ff17c8bfSgjelinek #define	SHELP_DETACH	"detach [-n] [brand-specific args]"
144ff17c8bfSgjelinek #define	SHELP_ATTACH	"attach [-F] [-n <path>] [brand-specific args]"
145555afedfScarlsonj #define	SHELP_MARK	"mark incomplete"
1467c478bd9Sstevel@tonic-gate 
1479acbbeafSnn35248 #define	EXEC_PREFIX	"exec "
1489acbbeafSnn35248 #define	EXEC_LEN	(strlen(EXEC_PREFIX))
1499acbbeafSnn35248 #define	RMCOMMAND	"/usr/bin/rm -rf"
1509acbbeafSnn35248 
1519acbbeafSnn35248 static int cleanup_zonepath(char *, boolean_t);
1529acbbeafSnn35248 
153f4b3ec61Sdh155122 
1547c478bd9Sstevel@tonic-gate static int help_func(int argc, char *argv[]);
1557c478bd9Sstevel@tonic-gate static int ready_func(int argc, char *argv[]);
1567c478bd9Sstevel@tonic-gate static int boot_func(int argc, char *argv[]);
1573c7284bdSAlexander Eremin static int shutdown_func(int argc, char *argv[]);
1587c478bd9Sstevel@tonic-gate static int halt_func(int argc, char *argv[]);
1597c478bd9Sstevel@tonic-gate static int reboot_func(int argc, char *argv[]);
1607c478bd9Sstevel@tonic-gate static int list_func(int argc, char *argv[]);
1617c478bd9Sstevel@tonic-gate static int verify_func(int argc, char *argv[]);
1627c478bd9Sstevel@tonic-gate static int install_func(int argc, char *argv[]);
1637c478bd9Sstevel@tonic-gate static int uninstall_func(int argc, char *argv[]);
164108322fbScarlsonj static int mount_func(int argc, char *argv[]);
165108322fbScarlsonj static int unmount_func(int argc, char *argv[]);
166865e09a4Sgjelinek static int clone_func(int argc, char *argv[]);
167865e09a4Sgjelinek static int move_func(int argc, char *argv[]);
168ee519a1fSgjelinek static int detach_func(int argc, char *argv[]);
169ee519a1fSgjelinek static int attach_func(int argc, char *argv[]);
170555afedfScarlsonj static int mark_func(int argc, char *argv[]);
1710209230bSgjelinek static int apply_func(int argc, char *argv[]);
172fbbfbc6eSjv227347 static int sysboot_func(int argc, char *argv[]);
1737c478bd9Sstevel@tonic-gate static int sanity_check(char *zone, int cmd_num, boolean_t running,
1749acbbeafSnn35248     boolean_t unsafe_when_running, boolean_t force);
1757c478bd9Sstevel@tonic-gate static int cmd_match(char *cmd);
176ce28b40eSzt129084 static int verify_details(int, char *argv[]);
177ce28b40eSzt129084 static int verify_brand(zone_dochandle_t, int, char *argv[]);
178ce28b40eSzt129084 static int invoke_brand_handler(int, char *argv[]);
1797c478bd9Sstevel@tonic-gate 
1807c478bd9Sstevel@tonic-gate static struct cmd cmdtab[] = {
1817c478bd9Sstevel@tonic-gate 	{ CMD_HELP,		"help",		SHELP_HELP,	help_func },
1827c478bd9Sstevel@tonic-gate 	{ CMD_BOOT,		"boot",		SHELP_BOOT,	boot_func },
1837c478bd9Sstevel@tonic-gate 	{ CMD_HALT,		"halt",		SHELP_HALT,	halt_func },
1847c478bd9Sstevel@tonic-gate 	{ CMD_READY,		"ready",	SHELP_READY,	ready_func },
1853c7284bdSAlexander Eremin 	{ CMD_SHUTDOWN,		"shutdown",	SHELP_SHUTDOWN,	shutdown_func },
1867c478bd9Sstevel@tonic-gate 	{ CMD_REBOOT,		"reboot",	SHELP_REBOOT,	reboot_func },
1877c478bd9Sstevel@tonic-gate 	{ CMD_LIST,		"list",		SHELP_LIST,	list_func },
1887c478bd9Sstevel@tonic-gate 	{ CMD_VERIFY,		"verify",	SHELP_VERIFY,	verify_func },
1897c478bd9Sstevel@tonic-gate 	{ CMD_INSTALL,		"install",	SHELP_INSTALL,	install_func },
1907c478bd9Sstevel@tonic-gate 	{ CMD_UNINSTALL,	"uninstall",	SHELP_UNINSTALL,
191108322fbScarlsonj 	    uninstall_func },
192865e09a4Sgjelinek 	/* mount and unmount are private commands for admin/install */
193108322fbScarlsonj 	{ CMD_MOUNT,		"mount",	NULL,		mount_func },
194865e09a4Sgjelinek 	{ CMD_UNMOUNT,		"unmount",	NULL,		unmount_func },
195865e09a4Sgjelinek 	{ CMD_CLONE,		"clone",	SHELP_CLONE,	clone_func },
196ee519a1fSgjelinek 	{ CMD_MOVE,		"move",		SHELP_MOVE,	move_func },
197ee519a1fSgjelinek 	{ CMD_DETACH,		"detach",	SHELP_DETACH,	detach_func },
198555afedfScarlsonj 	{ CMD_ATTACH,		"attach",	SHELP_ATTACH,	attach_func },
1990209230bSgjelinek 	{ CMD_MARK,		"mark",		SHELP_MARK,	mark_func },
200fbbfbc6eSjv227347 	{ CMD_APPLY,		"apply",	NULL,		apply_func },
201fbbfbc6eSjv227347 	{ CMD_SYSBOOT,		"sysboot",	NULL,		sysboot_func }
2027c478bd9Sstevel@tonic-gate };
2037c478bd9Sstevel@tonic-gate 
2047c478bd9Sstevel@tonic-gate /* global variables */
2057c478bd9Sstevel@tonic-gate 
2067c478bd9Sstevel@tonic-gate /* set early in main(), never modified thereafter, used all over the place */
2077c478bd9Sstevel@tonic-gate static char *execname;
2089acbbeafSnn35248 static char target_brand[MAXNAMELEN];
209e5816e35SEdward Pilatowicz static char default_brand[MAXPATHLEN];
2107c478bd9Sstevel@tonic-gate static char *locale;
2110b5de56dSgjelinek char *target_zone;
212555afedfScarlsonj static char *target_uuid;
213cb8a054bSGlenn Faden char *username;
2147c478bd9Sstevel@tonic-gate 
2150b5de56dSgjelinek char *
cmd_to_str(int cmd_num)2167c478bd9Sstevel@tonic-gate cmd_to_str(int cmd_num)
2177c478bd9Sstevel@tonic-gate {
2187c478bd9Sstevel@tonic-gate 	assert(cmd_num >= CMD_MIN && cmd_num <= CMD_MAX);
2197c478bd9Sstevel@tonic-gate 	return (cmdtab[cmd_num].cmd_name);
2207c478bd9Sstevel@tonic-gate }
2217c478bd9Sstevel@tonic-gate 
2227c478bd9Sstevel@tonic-gate /* This is a separate function because of gettext() wrapping. */
2237c478bd9Sstevel@tonic-gate static char *
long_help(int cmd_num)2247c478bd9Sstevel@tonic-gate long_help(int cmd_num)
2257c478bd9Sstevel@tonic-gate {
2267e362f58Scomay 	assert(cmd_num >= CMD_MIN && cmd_num <= CMD_MAX);
2277c478bd9Sstevel@tonic-gate 	switch (cmd_num) {
2287c478bd9Sstevel@tonic-gate 	case CMD_HELP:
2297c478bd9Sstevel@tonic-gate 		return (gettext("Print usage message."));
2307c478bd9Sstevel@tonic-gate 	case CMD_BOOT:
2313f2f09c1Sdp 		return (gettext("Activates (boots) specified zone.  See "
2323f2f09c1Sdp 		    "zoneadm(1m) for valid boot\n\targuments."));
2337c478bd9Sstevel@tonic-gate 	case CMD_HALT:
2349e518655Sgjelinek 		return (gettext("Halts specified zone, bypassing shutdown "
2359e518655Sgjelinek 		    "scripts and removing runtime\n\tresources of the zone."));
2367c478bd9Sstevel@tonic-gate 	case CMD_READY:
2379e518655Sgjelinek 		return (gettext("Prepares a zone for running applications but "
2389e518655Sgjelinek 		    "does not start any user\n\tprocesses in the zone."));
2393c7284bdSAlexander Eremin 	case CMD_SHUTDOWN:
2403c7284bdSAlexander Eremin 		return (gettext("Gracefully shutdown the zone or reboot if "
2413c7284bdSAlexander Eremin 		    "the '-r' option is specified.\n\t"
2423c7284bdSAlexander Eremin 		    "See zoneadm(1m) for valid boot arguments."));
2437c478bd9Sstevel@tonic-gate 	case CMD_REBOOT:
2449e518655Sgjelinek 		return (gettext("Restarts the zone (equivalent to a halt / "
2453f2f09c1Sdp 		    "boot sequence).\n\tFails if the zone is not active.  "
2463f2f09c1Sdp 		    "See zoneadm(1m) for valid boot\n\targuments."));
2477c478bd9Sstevel@tonic-gate 	case CMD_LIST:
2487c478bd9Sstevel@tonic-gate 		return (gettext("Lists the current zones, or a "
2497c478bd9Sstevel@tonic-gate 		    "specific zone if indicated.  By default,\n\tall "
2507c478bd9Sstevel@tonic-gate 		    "running zones are listed, though this can be "
2517c478bd9Sstevel@tonic-gate 		    "expanded to all\n\tinstalled zones with the -i "
2527c478bd9Sstevel@tonic-gate 		    "option or all configured zones with the\n\t-c "
253555afedfScarlsonj 		    "option.  When used with the general -z <zone> and/or -u "
254555afedfScarlsonj 		    "<uuid-match>\n\toptions, lists only the specified "
255555afedfScarlsonj 		    "matching zone, but lists it\n\tregardless of its state, "
256555afedfScarlsonj 		    "and the -i and -c options are disallowed.  The\n\t-v "
257555afedfScarlsonj 		    "option can be used to display verbose information: zone "
258555afedfScarlsonj 		    "name, id,\n\tcurrent state, root directory and options.  "
259555afedfScarlsonj 		    "The -p option can be used\n\tto request machine-parsable "
260555afedfScarlsonj 		    "output.  The -v and -p options are mutually\n\texclusive."
261555afedfScarlsonj 		    "  If neither -v nor -p is used, just the zone name is "
262555afedfScarlsonj 		    "listed."));
2637c478bd9Sstevel@tonic-gate 	case CMD_VERIFY:
2647c478bd9Sstevel@tonic-gate 		return (gettext("Check to make sure the configuration "
2657c478bd9Sstevel@tonic-gate 		    "can safely be instantiated\n\ton the machine: "
2667c478bd9Sstevel@tonic-gate 		    "physical network interfaces exist, etc."));
2677c478bd9Sstevel@tonic-gate 	case CMD_INSTALL:
2680b5de56dSgjelinek 		return (gettext("Install the configuration on to the system.  "
269ad02e316Sbatschul 		    "All arguments are passed to the brand installation "
270ff17c8bfSgjelinek 		    "function;\n\tsee brands(5) for more information."));
2717c478bd9Sstevel@tonic-gate 	case CMD_UNINSTALL:
2729e518655Sgjelinek 		return (gettext("Uninstall the configuration from the system.  "
273ff17c8bfSgjelinek 		    "The -F flag can be used\n\tto force the action.  All "
274ff17c8bfSgjelinek 		    "other arguments are passed to the brand\n\tuninstall "
275ff17c8bfSgjelinek 		    "function; see brands(5) for more information."));
276865e09a4Sgjelinek 	case CMD_CLONE:
2770b5de56dSgjelinek 		return (gettext("Clone the installation of another zone.  "
2780b5de56dSgjelinek 		    "The -m option can be used to\n\tspecify 'copy' which "
2790b5de56dSgjelinek 		    "forces a copy of the source zone.  The -s option\n\t"
2800b5de56dSgjelinek 		    "can be used to specify the name of a ZFS snapshot "
2810b5de56dSgjelinek 		    "that was taken from\n\ta previous clone command.  The "
2820b5de56dSgjelinek 		    "snapshot will be used as the source\n\tinstead of "
283ff17c8bfSgjelinek 		    "creating a new ZFS snapshot.  All other arguments are "
284ff17c8bfSgjelinek 		    "passed\n\tto the brand clone function; see "
285ff17c8bfSgjelinek 		    "brands(5) for more information."));
286865e09a4Sgjelinek 	case CMD_MOVE:
287865e09a4Sgjelinek 		return (gettext("Move the zone to a new zonepath."));
2889e518655Sgjelinek 	case CMD_DETACH:
2899e518655Sgjelinek 		return (gettext("Detach the zone from the system. The zone "
2909e518655Sgjelinek 		    "state is changed to\n\t'configured' (but the files under "
2919e518655Sgjelinek 		    "the zonepath are untouched).\n\tThe zone can subsequently "
2929e518655Sgjelinek 		    "be attached, or can be moved to another\n\tsystem and "
2938cd327d5Sgjelinek 		    "attached there.  The -n option can be used to specify\n\t"
2948cd327d5Sgjelinek 		    "'no-execute' mode.  When -n is used, the information "
2958cd327d5Sgjelinek 		    "needed to attach\n\tthe zone is sent to standard output "
296ff17c8bfSgjelinek 		    "but the zone is not actually\n\tdetached.  All other "
297ff17c8bfSgjelinek 		    "arguments are passed to the brand detach function;\n\tsee "
298ff17c8bfSgjelinek 		    "brands(5) for more information."));
2999e518655Sgjelinek 	case CMD_ATTACH:
3009e518655Sgjelinek 		return (gettext("Attach the zone to the system.  The zone "
3019e518655Sgjelinek 		    "state must be 'configured'\n\tprior to attach; upon "
3029e518655Sgjelinek 		    "successful completion, the zone state will be\n\t"
3039e518655Sgjelinek 		    "'installed'.  The system software on the current "
3049e518655Sgjelinek 		    "system must be\n\tcompatible with the software on the "
305ff17c8bfSgjelinek 		    "zone's original system.\n\tSpecify -F "
3066cfd72c6Sgjelinek 		    "to force the attach and skip software compatibility "
3076cfd72c6Sgjelinek 		    "tests.\n\tThe -n option can be used to specify "
3086cfd72c6Sgjelinek 		    "'no-execute' mode.  When -n is\n\tused, the information "
3096cfd72c6Sgjelinek 		    "needed to attach the zone is read from the\n\tspecified "
3106cfd72c6Sgjelinek 		    "path and the configuration is only validated.  The path "
311ff17c8bfSgjelinek 		    "can\n\tbe '-' to specify standard input.  The -F and -n "
312ff17c8bfSgjelinek 		    "options are mutually\n\texclusive.  All other arguments "
313ff17c8bfSgjelinek 		    "are passed to the brand attach\n\tfunction; see "
314ff17c8bfSgjelinek 		    "brands(5) for more information."));
315555afedfScarlsonj 	case CMD_MARK:
316555afedfScarlsonj 		return (gettext("Set the state of the zone.  This can be used "
317555afedfScarlsonj 		    "to force the zone\n\tstate to 'incomplete' "
318555afedfScarlsonj 		    "administratively if some activity has rendered\n\tthe "
319555afedfScarlsonj 		    "zone permanently unusable.  The only valid state that "
320555afedfScarlsonj 		    "may be\n\tspecified is 'incomplete'."));
321108322fbScarlsonj 	default:
322108322fbScarlsonj 		return ("");
3237c478bd9Sstevel@tonic-gate 	}
3247c478bd9Sstevel@tonic-gate 	/* NOTREACHED */
3257e362f58Scomay 	return (NULL);
3267c478bd9Sstevel@tonic-gate }
3277c478bd9Sstevel@tonic-gate 
3287c478bd9Sstevel@tonic-gate /*
3297c478bd9Sstevel@tonic-gate  * Called with explicit B_TRUE when help is explicitly requested, B_FALSE for
3307c478bd9Sstevel@tonic-gate  * unexpected errors.
3317c478bd9Sstevel@tonic-gate  */
3327c478bd9Sstevel@tonic-gate 
3337c478bd9Sstevel@tonic-gate static int
usage(boolean_t explicit)3347c478bd9Sstevel@tonic-gate usage(boolean_t explicit)
3357c478bd9Sstevel@tonic-gate {
3367c478bd9Sstevel@tonic-gate 	int i;
3377c478bd9Sstevel@tonic-gate 	FILE *fd = explicit ? stdout : stderr;
3387c478bd9Sstevel@tonic-gate 
3397c478bd9Sstevel@tonic-gate 	(void) fprintf(fd, "%s:\t%s help\n", gettext("usage"), execname);
340555afedfScarlsonj 	(void) fprintf(fd, "\t%s [-z <zone>] [-u <uuid-match>] list\n",
341555afedfScarlsonj 	    execname);
342555afedfScarlsonj 	(void) fprintf(fd, "\t%s {-z <zone>|-u <uuid-match>} <%s>\n", execname,
3437c478bd9Sstevel@tonic-gate 	    gettext("subcommand"));
3447c478bd9Sstevel@tonic-gate 	(void) fprintf(fd, "\n%s:\n\n", gettext("Subcommands"));
3457c478bd9Sstevel@tonic-gate 	for (i = CMD_MIN; i <= CMD_MAX; i++) {
346108322fbScarlsonj 		if (cmdtab[i].short_usage == NULL)
347108322fbScarlsonj 			continue;
3487c478bd9Sstevel@tonic-gate 		(void) fprintf(fd, "%s\n", cmdtab[i].short_usage);
3497c478bd9Sstevel@tonic-gate 		if (explicit)
3507c478bd9Sstevel@tonic-gate 			(void) fprintf(fd, "\t%s\n\n", long_help(i));
3517c478bd9Sstevel@tonic-gate 	}
3527c478bd9Sstevel@tonic-gate 	if (!explicit)
3537c478bd9Sstevel@tonic-gate 		(void) fputs("\n", fd);
3547c478bd9Sstevel@tonic-gate 	return (Z_USAGE);
3557c478bd9Sstevel@tonic-gate }
3567c478bd9Sstevel@tonic-gate 
3577c478bd9Sstevel@tonic-gate static void
sub_usage(char * short_usage,int cmd_num)3587c478bd9Sstevel@tonic-gate sub_usage(char *short_usage, int cmd_num)
3597c478bd9Sstevel@tonic-gate {
3607c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "%s:\t%s\n", gettext("usage"), short_usage);
3617c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "\t%s\n", long_help(cmd_num));
3627c478bd9Sstevel@tonic-gate }
3637c478bd9Sstevel@tonic-gate 
3647c478bd9Sstevel@tonic-gate /*
3657c478bd9Sstevel@tonic-gate  * zperror() is like perror(3c) except that this also prints the executable
3667c478bd9Sstevel@tonic-gate  * name at the start of the message, and takes a boolean indicating whether
3677c478bd9Sstevel@tonic-gate  * to call libc'c strerror() or that from libzonecfg.
3687c478bd9Sstevel@tonic-gate  */
3697c478bd9Sstevel@tonic-gate 
3700b5de56dSgjelinek void
zperror(const char * str,boolean_t zonecfg_error)3717c478bd9Sstevel@tonic-gate zperror(const char *str, boolean_t zonecfg_error)
3727c478bd9Sstevel@tonic-gate {
3737c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "%s: %s: %s\n", execname, str,
3747c478bd9Sstevel@tonic-gate 	    zonecfg_error ? zonecfg_strerror(errno) : strerror(errno));
3757c478bd9Sstevel@tonic-gate }
3767c478bd9Sstevel@tonic-gate 
3777c478bd9Sstevel@tonic-gate /*
3787c478bd9Sstevel@tonic-gate  * zperror2() is very similar to zperror() above, except it also prints a
3797c478bd9Sstevel@tonic-gate  * supplied zone name after the executable.
3807c478bd9Sstevel@tonic-gate  *
3817c478bd9Sstevel@tonic-gate  * All current consumers of this function want libzonecfg's strerror() rather
3827c478bd9Sstevel@tonic-gate  * than libc's; if this ever changes, this function can be made more generic
3837c478bd9Sstevel@tonic-gate  * like zperror() above.
3847c478bd9Sstevel@tonic-gate  */
3857c478bd9Sstevel@tonic-gate 
3860b5de56dSgjelinek void
zperror2(const char * zone,const char * str)3877c478bd9Sstevel@tonic-gate zperror2(const char *zone, const char *str)
3887c478bd9Sstevel@tonic-gate {
3897c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "%s: %s: %s: %s\n", execname, zone, str,
3907c478bd9Sstevel@tonic-gate 	    zonecfg_strerror(errno));
3917c478bd9Sstevel@tonic-gate }
3927c478bd9Sstevel@tonic-gate 
3937c478bd9Sstevel@tonic-gate /* PRINTFLIKE1 */
3940b5de56dSgjelinek void
zerror(const char * fmt,...)3957c478bd9Sstevel@tonic-gate zerror(const char *fmt, ...)
3967c478bd9Sstevel@tonic-gate {
3977c478bd9Sstevel@tonic-gate 	va_list alist;
3987c478bd9Sstevel@tonic-gate 
3997c478bd9Sstevel@tonic-gate 	va_start(alist, fmt);
4007c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "%s: ", execname);
4017c478bd9Sstevel@tonic-gate 	if (target_zone != NULL)
4027c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "zone '%s': ", target_zone);
4037c478bd9Sstevel@tonic-gate 	(void) vfprintf(stderr, fmt, alist);
4047c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "\n");
4057c478bd9Sstevel@tonic-gate 	va_end(alist);
4067c478bd9Sstevel@tonic-gate }
4077c478bd9Sstevel@tonic-gate 
4087c478bd9Sstevel@tonic-gate static void *
safe_calloc(size_t nelem,size_t elsize)4097c478bd9Sstevel@tonic-gate safe_calloc(size_t nelem, size_t elsize)
4107c478bd9Sstevel@tonic-gate {
4117c478bd9Sstevel@tonic-gate 	void *r = calloc(nelem, elsize);
4127c478bd9Sstevel@tonic-gate 
4137c478bd9Sstevel@tonic-gate 	if (r == NULL) {
4147c478bd9Sstevel@tonic-gate 		zerror(gettext("failed to allocate %lu bytes: %s"),
4157c478bd9Sstevel@tonic-gate 		    (ulong_t)nelem * elsize, strerror(errno));
4167c478bd9Sstevel@tonic-gate 		exit(Z_ERR);
4177c478bd9Sstevel@tonic-gate 	}
4187c478bd9Sstevel@tonic-gate 	return (r);
4197c478bd9Sstevel@tonic-gate }
4207c478bd9Sstevel@tonic-gate 
4217c478bd9Sstevel@tonic-gate static void
zone_print(zone_entry_t * zent,boolean_t verbose,boolean_t parsable)4227c478bd9Sstevel@tonic-gate zone_print(zone_entry_t *zent, boolean_t verbose, boolean_t parsable)
4237c478bd9Sstevel@tonic-gate {
4247c478bd9Sstevel@tonic-gate 	static boolean_t firsttime = B_TRUE;
425f4b3ec61Sdh155122 	char *ip_type_str;
426f4b3ec61Sdh155122 
4272766118bS 	/* Skip a zone that shutdown while we were collecting data. */
4282766118bS 	if (zent->zname[0] == '\0')
4292766118bS 		return;
4302766118bS 
431f4b3ec61Sdh155122 	if (zent->ziptype == ZS_EXCLUSIVE)
432f4b3ec61Sdh155122 		ip_type_str = "excl";
433f4b3ec61Sdh155122 	else
434f4b3ec61Sdh155122 		ip_type_str = "shared";
4357c478bd9Sstevel@tonic-gate 
4367c478bd9Sstevel@tonic-gate 	assert(!(verbose && parsable));
4377c478bd9Sstevel@tonic-gate 	if (firsttime && verbose) {
4387c478bd9Sstevel@tonic-gate 		firsttime = B_FALSE;
439f4b3ec61Sdh155122 		(void) printf("%*s %-16s %-10s %-30s %-8s %-6s\n",
440f4b3ec61Sdh155122 		    ZONEID_WIDTH, "ID", "NAME", "STATUS", "PATH", "BRAND",
441f4b3ec61Sdh155122 		    "IP");
4427c478bd9Sstevel@tonic-gate 	}
4437c478bd9Sstevel@tonic-gate 	if (!verbose) {
444555afedfScarlsonj 		char *cp, *clim;
445555afedfScarlsonj 
4467c478bd9Sstevel@tonic-gate 		if (!parsable) {
4477c478bd9Sstevel@tonic-gate 			(void) printf("%s\n", zent->zname);
4487c478bd9Sstevel@tonic-gate 			return;
4497c478bd9Sstevel@tonic-gate 		}
4507c478bd9Sstevel@tonic-gate 		if (zent->zid == ZONE_ID_UNDEFINED)
4517c478bd9Sstevel@tonic-gate 			(void) printf("-");
4527c478bd9Sstevel@tonic-gate 		else
4537c478bd9Sstevel@tonic-gate 			(void) printf("%lu", zent->zid);
454555afedfScarlsonj 		(void) printf(":%s:%s:", zent->zname, zent->zstate_str);
455555afedfScarlsonj 		cp = zent->zroot;
456555afedfScarlsonj 		while ((clim = strchr(cp, ':')) != NULL) {
457555afedfScarlsonj 			(void) printf("%.*s\\:", clim - cp, cp);
458555afedfScarlsonj 			cp = clim + 1;
459555afedfScarlsonj 		}
460f4b3ec61Sdh155122 		(void) printf("%s:%s:%s:%s\n", cp, zent->zuuid, zent->zbrand,
461f4b3ec61Sdh155122 		    ip_type_str);
4627c478bd9Sstevel@tonic-gate 		return;
4637c478bd9Sstevel@tonic-gate 	}
4647c478bd9Sstevel@tonic-gate 	if (zent->zstate_str != NULL) {
4657c478bd9Sstevel@tonic-gate 		if (zent->zid == ZONE_ID_UNDEFINED)
4667c478bd9Sstevel@tonic-gate 			(void) printf("%*s", ZONEID_WIDTH, "-");
4677c478bd9Sstevel@tonic-gate 		else
4687c478bd9Sstevel@tonic-gate 			(void) printf("%*lu", ZONEID_WIDTH, zent->zid);
469f4b3ec61Sdh155122 		(void) printf(" %-16s %-10s %-30s %-8s %-6s\n", zent->zname,
470f4b3ec61Sdh155122 		    zent->zstate_str, zent->zroot, zent->zbrand, ip_type_str);
4717c478bd9Sstevel@tonic-gate 	}
4727c478bd9Sstevel@tonic-gate }
4737c478bd9Sstevel@tonic-gate 
4747c478bd9Sstevel@tonic-gate static int
lookup_zone_info(const char * zone_name,zoneid_t zid,zone_entry_t * zent)475108322fbScarlsonj lookup_zone_info(const char *zone_name, zoneid_t zid, zone_entry_t *zent)
4767c478bd9Sstevel@tonic-gate {
47745916cd2Sjpk 	char root[MAXPATHLEN], *cp;
4787c478bd9Sstevel@tonic-gate 	int err;
479555afedfScarlsonj 	uuid_t uuid;
4802766118bS 	zone_dochandle_t handle;
4817c478bd9Sstevel@tonic-gate 
4827c478bd9Sstevel@tonic-gate 	(void) strlcpy(zent->zname, zone_name, sizeof (zent->zname));
4837c478bd9Sstevel@tonic-gate 	(void) strlcpy(zent->zroot, "???", sizeof (zent->zroot));
4849acbbeafSnn35248 	(void) strlcpy(zent->zbrand, "???", sizeof (zent->zbrand));
4857c478bd9Sstevel@tonic-gate 	zent->zstate_str = "???";
4867c478bd9Sstevel@tonic-gate 
487108322fbScarlsonj 	zent->zid = zid;
4887c478bd9Sstevel@tonic-gate 
489555afedfScarlsonj 	if (zonecfg_get_uuid(zone_name, uuid) == Z_OK &&
490555afedfScarlsonj 	    !uuid_is_null(uuid))
491555afedfScarlsonj 		uuid_unparse(uuid, zent->zuuid);
492555afedfScarlsonj 	else
493555afedfScarlsonj 		zent->zuuid[0] = '\0';
494555afedfScarlsonj 
49545916cd2Sjpk 	/*
49645916cd2Sjpk 	 * For labeled zones which query the zone path of lower-level
49745916cd2Sjpk 	 * zones, the path needs to be adjusted to drop the final
49845916cd2Sjpk 	 * "/root" component. This adjusted path is then useful
49945916cd2Sjpk 	 * for reading down any exported directories from the
50045916cd2Sjpk 	 * lower-level zone.
50145916cd2Sjpk 	 */
50245916cd2Sjpk 	if (is_system_labeled() && zent->zid != ZONE_ID_UNDEFINED) {
50345916cd2Sjpk 		if (zone_getattr(zent->zid, ZONE_ATTR_ROOT, zent->zroot,
50445916cd2Sjpk 		    sizeof (zent->zroot)) == -1) {
50545916cd2Sjpk 			zperror2(zent->zname,
50645916cd2Sjpk 			    gettext("could not get zone path."));
50745916cd2Sjpk 			return (Z_ERR);
50845916cd2Sjpk 		}
50945916cd2Sjpk 		cp = zent->zroot + strlen(zent->zroot) - 5;
51045916cd2Sjpk 		if (cp > zent->zroot && strcmp(cp, "/root") == 0)
51145916cd2Sjpk 			*cp = 0;
51245916cd2Sjpk 	} else {
51345916cd2Sjpk 		if ((err = zone_get_zonepath(zent->zname, root,
51445916cd2Sjpk 		    sizeof (root))) != Z_OK) {
5157c478bd9Sstevel@tonic-gate 			errno = err;
51645916cd2Sjpk 			zperror2(zent->zname,
51745916cd2Sjpk 			    gettext("could not get zone path."));
5187c478bd9Sstevel@tonic-gate 			return (Z_ERR);
5197c478bd9Sstevel@tonic-gate 		}
5207c478bd9Sstevel@tonic-gate 		(void) strlcpy(zent->zroot, root, sizeof (zent->zroot));
52145916cd2Sjpk 	}
5227c478bd9Sstevel@tonic-gate 
5237c478bd9Sstevel@tonic-gate 	if ((err = zone_get_state(zent->zname, &zent->zstate_num)) != Z_OK) {
5247c478bd9Sstevel@tonic-gate 		errno = err;
5257c478bd9Sstevel@tonic-gate 		zperror2(zent->zname, gettext("could not get state"));
5267c478bd9Sstevel@tonic-gate 		return (Z_ERR);
5277c478bd9Sstevel@tonic-gate 	}
5287c478bd9Sstevel@tonic-gate 	zent->zstate_str = zone_state_str(zent->zstate_num);
529bafa7067Snn35248 
530bafa7067Snn35248 	/*
531bafa7067Snn35248 	 * A zone's brand is only available in the .xml file describing it,
532bafa7067Snn35248 	 * which is only visible to the global zone.  This causes
533bafa7067Snn35248 	 * zone_get_brand() to fail when called from within a non-global
534bafa7067Snn35248 	 * zone.  Fortunately we only do this on labeled systems, where we
535bafa7067Snn35248 	 * know all zones are native.
536bafa7067Snn35248 	 */
537bafa7067Snn35248 	if (getzoneid() != GLOBAL_ZONEID) {
538bafa7067Snn35248 		assert(is_system_labeled() != 0);
539e5816e35SEdward Pilatowicz 		(void) strlcpy(zent->zbrand, default_brand,
540bafa7067Snn35248 		    sizeof (zent->zbrand));
541bafa7067Snn35248 	} else if (zone_get_brand(zent->zname, zent->zbrand,
5429acbbeafSnn35248 	    sizeof (zent->zbrand)) != Z_OK) {
5439acbbeafSnn35248 		zperror2(zent->zname, gettext("could not get brand name"));
5449acbbeafSnn35248 		return (Z_ERR);
5459acbbeafSnn35248 	}
5467c478bd9Sstevel@tonic-gate 
547f4b3ec61Sdh155122 	/*
548f4b3ec61Sdh155122 	 * Get ip type of the zone.
549f4b3ec61Sdh155122 	 * Note for global zone, ZS_SHARED is set always.
550f4b3ec61Sdh155122 	 */
551f4b3ec61Sdh155122 	if (zid == GLOBAL_ZONEID) {
552f4b3ec61Sdh155122 		zent->ziptype = ZS_SHARED;
5532766118bS 		return (Z_OK);
5542766118bS 	}
555f4b3ec61Sdh155122 
5562766118bS 	/*
5572766118bS 	 * There is a race condition where the zone could boot while
5582766118bS 	 * we're walking the index file.  In this case the zone state
5592766118bS 	 * could be seen as running from the call above, but the zoneid
5602766118bS 	 * would be undefined.
5612766118bS 	 *
5622766118bS 	 * There is also a race condition where the zone could shutdown after
5632766118bS 	 * we got its running state above.  This is also not an error and
5642766118bS 	 * we fall back to getting the ziptype from the zone configuration.
5652766118bS 	 */
5662766118bS 	if (zent->zstate_num == ZONE_STATE_RUNNING &&
5672766118bS 	    zid != ZONE_ID_UNDEFINED) {
568f4b3ec61Sdh155122 		ushort_t flags;
569f4b3ec61Sdh155122 
570f4b3ec61Sdh155122 		if (zone_getattr(zid, ZONE_ATTR_FLAGS, &flags,
5712766118bS 		    sizeof (flags)) >= 0) {
572f4b3ec61Sdh155122 			if (flags & ZF_NET_EXCL)
573f4b3ec61Sdh155122 				zent->ziptype = ZS_EXCLUSIVE;
574f4b3ec61Sdh155122 			else
575f4b3ec61Sdh155122 				zent->ziptype = ZS_SHARED;
5762766118bS 			return (Z_OK);
5772766118bS 		}
5782766118bS 	}
579f4b3ec61Sdh155122 
580f4b3ec61Sdh155122 	if ((handle = zonecfg_init_handle()) == NULL) {
5812766118bS 		zperror2(zent->zname, gettext("could not init handle"));
582f4b3ec61Sdh155122 		return (Z_ERR);
583f4b3ec61Sdh155122 	}
5842766118bS 	if ((err = zonecfg_get_handle(zent->zname, handle)) != Z_OK) {
5852766118bS 		zperror2(zent->zname, gettext("could not get handle"));
586f4b3ec61Sdh155122 		zonecfg_fini_handle(handle);
587f4b3ec61Sdh155122 		return (Z_ERR);
588f4b3ec61Sdh155122 	}
589f4b3ec61Sdh155122 
5902766118bS 	if ((err = zonecfg_get_iptype(handle, &zent->ziptype)) != Z_OK) {
5912766118bS 		zperror2(zent->zname, gettext("could not get ip-type"));
592f4b3ec61Sdh155122 		zonecfg_fini_handle(handle);
593f4b3ec61Sdh155122 		return (Z_ERR);
594f4b3ec61Sdh155122 	}
595f4b3ec61Sdh155122 	zonecfg_fini_handle(handle);
596f4b3ec61Sdh155122 
5977c478bd9Sstevel@tonic-gate 	return (Z_OK);
5987c478bd9Sstevel@tonic-gate }
5997c478bd9Sstevel@tonic-gate 
6007c478bd9Sstevel@tonic-gate /*
6017c478bd9Sstevel@tonic-gate  * fetch_zents() calls zone_list(2) to find out how many zones are running
6027c478bd9Sstevel@tonic-gate  * (which is stored in the global nzents), then calls zone_list(2) again
6037c478bd9Sstevel@tonic-gate  * to fetch the list of running zones (stored in the global zents).  This
6047c478bd9Sstevel@tonic-gate  * function may be called multiple times, so if zents is already set, we
6057c478bd9Sstevel@tonic-gate  * return immediately to save work.
6062766118bS  *
6072766118bS  * Note that the data about running zones can change while this function
6082766118bS  * is running, so its possible that the list of zones will have empty slots
6092766118bS  * at the end.
6107c478bd9Sstevel@tonic-gate  */
6117c478bd9Sstevel@tonic-gate 
6127c478bd9Sstevel@tonic-gate static int
fetch_zents(void)613108322fbScarlsonj fetch_zents(void)
6147c478bd9Sstevel@tonic-gate {
6157c478bd9Sstevel@tonic-gate 	zoneid_t *zids = NULL;
6167c478bd9Sstevel@tonic-gate 	uint_t nzents_saved;
617108322fbScarlsonj 	int i, retv;
618108322fbScarlsonj 	FILE *fp;
619108322fbScarlsonj 	boolean_t inaltroot;
620108322fbScarlsonj 	zone_entry_t *zentp;
621c8845d44SSusan Kamm-Worrell 	const char *altroot;
6227c478bd9Sstevel@tonic-gate 
6237c478bd9Sstevel@tonic-gate 	if (nzents > 0)
6247c478bd9Sstevel@tonic-gate 		return (Z_OK);
6257c478bd9Sstevel@tonic-gate 
6267c478bd9Sstevel@tonic-gate 	if (zone_list(NULL, &nzents) != 0) {
6277c478bd9Sstevel@tonic-gate 		zperror(gettext("failed to get zoneid list"), B_FALSE);
6287c478bd9Sstevel@tonic-gate 		return (Z_ERR);
6297c478bd9Sstevel@tonic-gate 	}
6307c478bd9Sstevel@tonic-gate 
6317c478bd9Sstevel@tonic-gate again:
6327c478bd9Sstevel@tonic-gate 	if (nzents == 0)
6337c478bd9Sstevel@tonic-gate 		return (Z_OK);
6347c478bd9Sstevel@tonic-gate 
6357c478bd9Sstevel@tonic-gate 	zids = safe_calloc(nzents, sizeof (zoneid_t));
6367c478bd9Sstevel@tonic-gate 	nzents_saved = nzents;
6377c478bd9Sstevel@tonic-gate 
6387c478bd9Sstevel@tonic-gate 	if (zone_list(zids, &nzents) != 0) {
6397c478bd9Sstevel@tonic-gate 		zperror(gettext("failed to get zone list"), B_FALSE);
6407c478bd9Sstevel@tonic-gate 		free(zids);
6417c478bd9Sstevel@tonic-gate 		return (Z_ERR);
6427c478bd9Sstevel@tonic-gate 	}
6437c478bd9Sstevel@tonic-gate 	if (nzents != nzents_saved) {
6447c478bd9Sstevel@tonic-gate 		/* list changed, try again */
6457c478bd9Sstevel@tonic-gate 		free(zids);
6467c478bd9Sstevel@tonic-gate 		goto again;
6477c478bd9Sstevel@tonic-gate 	}
6487c478bd9Sstevel@tonic-gate 
6497c478bd9Sstevel@tonic-gate 	zents = safe_calloc(nzents, sizeof (zone_entry_t));
6507c478bd9Sstevel@tonic-gate 
651108322fbScarlsonj 	inaltroot = zonecfg_in_alt_root();
652c8845d44SSusan Kamm-Worrell 	if (inaltroot) {
653108322fbScarlsonj 		fp = zonecfg_open_scratch("", B_FALSE);
654c8845d44SSusan Kamm-Worrell 		altroot = zonecfg_get_root();
655c8845d44SSusan Kamm-Worrell 	} else {
656108322fbScarlsonj 		fp = NULL;
657c8845d44SSusan Kamm-Worrell 	}
658108322fbScarlsonj 	zentp = zents;
659108322fbScarlsonj 	retv = Z_OK;
6607c478bd9Sstevel@tonic-gate 	for (i = 0; i < nzents; i++) {
6617c478bd9Sstevel@tonic-gate 		char name[ZONENAME_MAX];
662108322fbScarlsonj 		char altname[ZONENAME_MAX];
663c8845d44SSusan Kamm-Worrell 		char rev_altroot[MAXPATHLEN];
6647c478bd9Sstevel@tonic-gate 
665108322fbScarlsonj 		if (getzonenamebyid(zids[i], name, sizeof (name)) < 0) {
6662766118bS 			/*
6672766118bS 			 * There is a race condition where the zone may have
6682766118bS 			 * shutdown since we retrieved the number of running
6692766118bS 			 * zones above.  This is not an error, there will be
6702766118bS 			 * an empty slot at the end of the list.
6712766118bS 			 */
672108322fbScarlsonj 			continue;
6737c478bd9Sstevel@tonic-gate 		}
674108322fbScarlsonj 		if (zonecfg_is_scratch(name)) {
675108322fbScarlsonj 			/* Ignore scratch zones by default */
676108322fbScarlsonj 			if (!inaltroot)
677108322fbScarlsonj 				continue;
678108322fbScarlsonj 			if (fp == NULL ||
679108322fbScarlsonj 			    zonecfg_reverse_scratch(fp, name, altname,
680c8845d44SSusan Kamm-Worrell 			    sizeof (altname), rev_altroot,
681c8845d44SSusan Kamm-Worrell 			    sizeof (rev_altroot)) == -1) {
6825ee84fbdSgjelinek 				zerror(gettext("could not resolve scratch "
683108322fbScarlsonj 				    "zone %s"), name);
684108322fbScarlsonj 				retv = Z_ERR;
685108322fbScarlsonj 				continue;
686108322fbScarlsonj 			}
687c8845d44SSusan Kamm-Worrell 			/* Ignore zones in other alternate roots */
688c8845d44SSusan Kamm-Worrell 			if (strcmp(rev_altroot, altroot) != 0)
689c8845d44SSusan Kamm-Worrell 				continue;
690108322fbScarlsonj 			(void) strcpy(name, altname);
691108322fbScarlsonj 		} else {
692108322fbScarlsonj 			/* Ignore non-scratch when in an alternate root */
693108322fbScarlsonj 			if (inaltroot && strcmp(name, GLOBAL_ZONENAME) != 0)
694108322fbScarlsonj 				continue;
695108322fbScarlsonj 		}
696108322fbScarlsonj 		if (lookup_zone_info(name, zids[i], zentp) != Z_OK) {
6972766118bS 			/*
6982766118bS 			 * There is a race condition where the zone may have
6992766118bS 			 * shutdown since we retrieved the number of running
7002766118bS 			 * zones above.  This is not an error, there will be
7012766118bS 			 * an empty slot at the end of the list.
7022766118bS 			 */
703108322fbScarlsonj 			continue;
704108322fbScarlsonj 		}
705108322fbScarlsonj 		zentp++;
706108322fbScarlsonj 	}
707108322fbScarlsonj 	nzents = zentp - zents;
708108322fbScarlsonj 	if (fp != NULL)
709108322fbScarlsonj 		zonecfg_close_scratch(fp);
7107c478bd9Sstevel@tonic-gate 
7117c478bd9Sstevel@tonic-gate 	free(zids);
712108322fbScarlsonj 	return (retv);
7137c478bd9Sstevel@tonic-gate }
7147c478bd9Sstevel@tonic-gate 
715108322fbScarlsonj static int
zone_print_list(zone_state_t min_state,boolean_t verbose,boolean_t parsable)7167c478bd9Sstevel@tonic-gate zone_print_list(zone_state_t min_state, boolean_t verbose, boolean_t parsable)
7177c478bd9Sstevel@tonic-gate {
7187c478bd9Sstevel@tonic-gate 	int i;
7197c478bd9Sstevel@tonic-gate 	zone_entry_t zent;
7207c478bd9Sstevel@tonic-gate 	FILE *cookie;
7217c478bd9Sstevel@tonic-gate 	char *name;
7227c478bd9Sstevel@tonic-gate 
7237c478bd9Sstevel@tonic-gate 	/*
7247c478bd9Sstevel@tonic-gate 	 * First get the list of running zones from the kernel and print them.
7257c478bd9Sstevel@tonic-gate 	 * If that is all we need, then return.
7267c478bd9Sstevel@tonic-gate 	 */
727108322fbScarlsonj 	if ((i = fetch_zents()) != Z_OK) {
7287c478bd9Sstevel@tonic-gate 		/*
7297c478bd9Sstevel@tonic-gate 		 * No need for error messages; fetch_zents() has already taken
7307c478bd9Sstevel@tonic-gate 		 * care of this.
7317c478bd9Sstevel@tonic-gate 		 */
732108322fbScarlsonj 		return (i);
7337c478bd9Sstevel@tonic-gate 	}
734108322fbScarlsonj 	for (i = 0; i < nzents; i++)
7357c478bd9Sstevel@tonic-gate 		zone_print(&zents[i], verbose, parsable);
7367c478bd9Sstevel@tonic-gate 	if (min_state >= ZONE_STATE_RUNNING)
737108322fbScarlsonj 		return (Z_OK);
7387c478bd9Sstevel@tonic-gate 	/*
7397c478bd9Sstevel@tonic-gate 	 * Next, get the full list of zones from the configuration, skipping
7407c478bd9Sstevel@tonic-gate 	 * any we have already printed.
7417c478bd9Sstevel@tonic-gate 	 */
7427c478bd9Sstevel@tonic-gate 	cookie = setzoneent();
7437c478bd9Sstevel@tonic-gate 	while ((name = getzoneent(cookie)) != NULL) {
7447c478bd9Sstevel@tonic-gate 		for (i = 0; i < nzents; i++) {
7457c478bd9Sstevel@tonic-gate 			if (strcmp(zents[i].zname, name) == 0)
7467c478bd9Sstevel@tonic-gate 				break;
7477c478bd9Sstevel@tonic-gate 		}
7487c478bd9Sstevel@tonic-gate 		if (i < nzents) {
7497c478bd9Sstevel@tonic-gate 			free(name);
7507c478bd9Sstevel@tonic-gate 			continue;
7517c478bd9Sstevel@tonic-gate 		}
752108322fbScarlsonj 		if (lookup_zone_info(name, ZONE_ID_UNDEFINED, &zent) != Z_OK) {
7537c478bd9Sstevel@tonic-gate 			free(name);
7547c478bd9Sstevel@tonic-gate 			continue;
7557c478bd9Sstevel@tonic-gate 		}
7567c478bd9Sstevel@tonic-gate 		free(name);
7577c478bd9Sstevel@tonic-gate 		if (zent.zstate_num >= min_state)
7587c478bd9Sstevel@tonic-gate 			zone_print(&zent, verbose, parsable);
7597c478bd9Sstevel@tonic-gate 	}
7607c478bd9Sstevel@tonic-gate 	endzoneent(cookie);
761108322fbScarlsonj 	return (Z_OK);
7627c478bd9Sstevel@tonic-gate }
7637c478bd9Sstevel@tonic-gate 
764c9f134eaSjv227347 /*
765c9f134eaSjv227347  * Retrieve a zone entry by name.  Returns NULL if no such zone exists.
766c9f134eaSjv227347  */
7677c478bd9Sstevel@tonic-gate static zone_entry_t *
lookup_running_zone(const char * str)768c9f134eaSjv227347 lookup_running_zone(const char *str)
7697c478bd9Sstevel@tonic-gate {
7707c478bd9Sstevel@tonic-gate 	int i;
7717c478bd9Sstevel@tonic-gate 
7727c478bd9Sstevel@tonic-gate 	if (fetch_zents() != Z_OK)
7737c478bd9Sstevel@tonic-gate 		return (NULL);
7747c478bd9Sstevel@tonic-gate 
7757c478bd9Sstevel@tonic-gate 	for (i = 0; i < nzents; i++) {
7767c478bd9Sstevel@tonic-gate 		if (strcmp(str, zents[i].zname) == 0)
7777c478bd9Sstevel@tonic-gate 			return (&zents[i]);
7787c478bd9Sstevel@tonic-gate 	}
7797c478bd9Sstevel@tonic-gate 	return (NULL);
7807c478bd9Sstevel@tonic-gate }
7817c478bd9Sstevel@tonic-gate 
7827c478bd9Sstevel@tonic-gate /*
7837c478bd9Sstevel@tonic-gate  * Check a bit in a mode_t: if on is B_TRUE, that bit should be on; if
7847c478bd9Sstevel@tonic-gate  * B_FALSE, it should be off.  Return B_TRUE if the mode is bad (incorrect).
7857c478bd9Sstevel@tonic-gate  */
7867c478bd9Sstevel@tonic-gate static boolean_t
bad_mode_bit(mode_t mode,mode_t bit,boolean_t on,char * file)7877c478bd9Sstevel@tonic-gate bad_mode_bit(mode_t mode, mode_t bit, boolean_t on, char *file)
7887c478bd9Sstevel@tonic-gate {
7897c478bd9Sstevel@tonic-gate 	char *str;
7907c478bd9Sstevel@tonic-gate 
7917c478bd9Sstevel@tonic-gate 	assert(bit == S_IRUSR || bit == S_IWUSR || bit == S_IXUSR ||
7927c478bd9Sstevel@tonic-gate 	    bit == S_IRGRP || bit == S_IWGRP || bit == S_IXGRP ||
7937c478bd9Sstevel@tonic-gate 	    bit == S_IROTH || bit == S_IWOTH || bit == S_IXOTH);
7947c478bd9Sstevel@tonic-gate 	/*
7957c478bd9Sstevel@tonic-gate 	 * TRANSLATION_NOTE
7967c478bd9Sstevel@tonic-gate 	 * The strings below will be used as part of a larger message,
7977c478bd9Sstevel@tonic-gate 	 * either:
7987c478bd9Sstevel@tonic-gate 	 * (file name) must be (owner|group|world) (read|writ|execut)able
7997c478bd9Sstevel@tonic-gate 	 * or
8007c478bd9Sstevel@tonic-gate 	 * (file name) must not be (owner|group|world) (read|writ|execut)able
8017c478bd9Sstevel@tonic-gate 	 */
8027c478bd9Sstevel@tonic-gate 	switch (bit) {
8037c478bd9Sstevel@tonic-gate 	case S_IRUSR:
8047c478bd9Sstevel@tonic-gate 		str = gettext("owner readable");
8057c478bd9Sstevel@tonic-gate 		break;
8067c478bd9Sstevel@tonic-gate 	case S_IWUSR:
8077c478bd9Sstevel@tonic-gate 		str = gettext("owner writable");
8087c478bd9Sstevel@tonic-gate 		break;
8097c478bd9Sstevel@tonic-gate 	case S_IXUSR:
8107c478bd9Sstevel@tonic-gate 		str = gettext("owner executable");
8117c478bd9Sstevel@tonic-gate 		break;
8127c478bd9Sstevel@tonic-gate 	case S_IRGRP:
8137c478bd9Sstevel@tonic-gate 		str = gettext("group readable");
8147c478bd9Sstevel@tonic-gate 		break;
8157c478bd9Sstevel@tonic-gate 	case S_IWGRP:
8167c478bd9Sstevel@tonic-gate 		str = gettext("group writable");
8177c478bd9Sstevel@tonic-gate 		break;
8187c478bd9Sstevel@tonic-gate 	case S_IXGRP:
8197c478bd9Sstevel@tonic-gate 		str = gettext("group executable");
8207c478bd9Sstevel@tonic-gate 		break;
8217c478bd9Sstevel@tonic-gate 	case S_IROTH:
8227c478bd9Sstevel@tonic-gate 		str = gettext("world readable");
8237c478bd9Sstevel@tonic-gate 		break;
8247c478bd9Sstevel@tonic-gate 	case S_IWOTH:
8257c478bd9Sstevel@tonic-gate 		str = gettext("world writable");
8267c478bd9Sstevel@tonic-gate 		break;
8277c478bd9Sstevel@tonic-gate 	case S_IXOTH:
8287c478bd9Sstevel@tonic-gate 		str = gettext("world executable");
8297c478bd9Sstevel@tonic-gate 		break;
8307c478bd9Sstevel@tonic-gate 	}
8317c478bd9Sstevel@tonic-gate 	if ((mode & bit) == (on ? 0 : bit)) {
8327c478bd9Sstevel@tonic-gate 		/*
8337c478bd9Sstevel@tonic-gate 		 * TRANSLATION_NOTE
8347c478bd9Sstevel@tonic-gate 		 * The first parameter below is a file name; the second
8357c478bd9Sstevel@tonic-gate 		 * is one of the "(owner|group|world) (read|writ|execut)able"
8367c478bd9Sstevel@tonic-gate 		 * strings from above.
8377c478bd9Sstevel@tonic-gate 		 */
8387c478bd9Sstevel@tonic-gate 		/*
8397c478bd9Sstevel@tonic-gate 		 * The code below could be simplified but not in a way
8407c478bd9Sstevel@tonic-gate 		 * that would easily translate to non-English locales.
8417c478bd9Sstevel@tonic-gate 		 */
8427c478bd9Sstevel@tonic-gate 		if (on) {
8437c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("%s must be %s.\n"),
8447c478bd9Sstevel@tonic-gate 			    file, str);
8457c478bd9Sstevel@tonic-gate 		} else {
8467c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("%s must not be %s.\n"),
8477c478bd9Sstevel@tonic-gate 			    file, str);
8487c478bd9Sstevel@tonic-gate 		}
8497c478bd9Sstevel@tonic-gate 		return (B_TRUE);
8507c478bd9Sstevel@tonic-gate 	}
8517c478bd9Sstevel@tonic-gate 	return (B_FALSE);
8527c478bd9Sstevel@tonic-gate }
8537c478bd9Sstevel@tonic-gate 
8547c478bd9Sstevel@tonic-gate /*
8557c478bd9Sstevel@tonic-gate  * We want to make sure that no zone has its zone path as a child node
8567c478bd9Sstevel@tonic-gate  * (in the directory sense) of any other.  We do that by comparing this
8577c478bd9Sstevel@tonic-gate  * zone's path to the path of all other (non-global) zones.  The comparison
8587c478bd9Sstevel@tonic-gate  * in each case is simple: add '/' to the end of the path, then do a
8597c478bd9Sstevel@tonic-gate  * strncmp() of the two paths, using the length of the shorter one.
8607c478bd9Sstevel@tonic-gate  */
8617c478bd9Sstevel@tonic-gate 
8627c478bd9Sstevel@tonic-gate static int
crosscheck_zonepaths(char * path)8637c478bd9Sstevel@tonic-gate crosscheck_zonepaths(char *path)
8647c478bd9Sstevel@tonic-gate {
8657c478bd9Sstevel@tonic-gate 	char rpath[MAXPATHLEN];		/* resolved path */
8667c478bd9Sstevel@tonic-gate 	char path_copy[MAXPATHLEN];	/* copy of original path */
8677c478bd9Sstevel@tonic-gate 	char rpath_copy[MAXPATHLEN];	/* copy of original rpath */
8687c478bd9Sstevel@tonic-gate 	struct zoneent *ze;
8697c478bd9Sstevel@tonic-gate 	int res, err;
8707c478bd9Sstevel@tonic-gate 	FILE *cookie;
8717c478bd9Sstevel@tonic-gate 
8727c478bd9Sstevel@tonic-gate 	cookie = setzoneent();
8737c478bd9Sstevel@tonic-gate 	while ((ze = getzoneent_private(cookie)) != NULL) {
8747c478bd9Sstevel@tonic-gate 		/* Skip zones which are not installed. */
8757c478bd9Sstevel@tonic-gate 		if (ze->zone_state < ZONE_STATE_INSTALLED) {
8767c478bd9Sstevel@tonic-gate 			free(ze);
8777c478bd9Sstevel@tonic-gate 			continue;
8787c478bd9Sstevel@tonic-gate 		}
8797c478bd9Sstevel@tonic-gate 		/* Skip the global zone and the current target zone. */
8807c478bd9Sstevel@tonic-gate 		if (strcmp(ze->zone_name, GLOBAL_ZONENAME) == 0 ||
8817c478bd9Sstevel@tonic-gate 		    strcmp(ze->zone_name, target_zone) == 0) {
8827c478bd9Sstevel@tonic-gate 			free(ze);
8837c478bd9Sstevel@tonic-gate 			continue;
8847c478bd9Sstevel@tonic-gate 		}
8857c478bd9Sstevel@tonic-gate 		if (strlen(ze->zone_path) == 0) {
8867c478bd9Sstevel@tonic-gate 			/* old index file without path, fall back */
8877c478bd9Sstevel@tonic-gate 			if ((err = zone_get_zonepath(ze->zone_name,
8887c478bd9Sstevel@tonic-gate 			    ze->zone_path, sizeof (ze->zone_path))) != Z_OK) {
8897c478bd9Sstevel@tonic-gate 				errno = err;
8907c478bd9Sstevel@tonic-gate 				zperror2(ze->zone_name,
8917c478bd9Sstevel@tonic-gate 				    gettext("could not get zone path"));
8927c478bd9Sstevel@tonic-gate 				free(ze);
8937c478bd9Sstevel@tonic-gate 				continue;
8947c478bd9Sstevel@tonic-gate 			}
8957c478bd9Sstevel@tonic-gate 		}
896108322fbScarlsonj 		(void) snprintf(path_copy, sizeof (path_copy), "%s%s",
897108322fbScarlsonj 		    zonecfg_get_root(), ze->zone_path);
898108322fbScarlsonj 		res = resolvepath(path_copy, rpath, sizeof (rpath));
8997c478bd9Sstevel@tonic-gate 		if (res == -1) {
9007c478bd9Sstevel@tonic-gate 			if (errno != ENOENT) {
901108322fbScarlsonj 				zperror(path_copy, B_FALSE);
9027c478bd9Sstevel@tonic-gate 				free(ze);
9037c478bd9Sstevel@tonic-gate 				return (Z_ERR);
9047c478bd9Sstevel@tonic-gate 			}
9057c478bd9Sstevel@tonic-gate 			(void) printf(gettext("WARNING: zone %s is installed, "
9067c478bd9Sstevel@tonic-gate 			    "but its %s %s does not exist.\n"), ze->zone_name,
907108322fbScarlsonj 			    "zonepath", path_copy);
9087c478bd9Sstevel@tonic-gate 			free(ze);
9097c478bd9Sstevel@tonic-gate 			continue;
9107c478bd9Sstevel@tonic-gate 		}
9117c478bd9Sstevel@tonic-gate 		rpath[res] = '\0';
9127c478bd9Sstevel@tonic-gate 		(void) snprintf(path_copy, sizeof (path_copy), "%s/", path);
9137c478bd9Sstevel@tonic-gate 		(void) snprintf(rpath_copy, sizeof (rpath_copy), "%s/", rpath);
9147c478bd9Sstevel@tonic-gate 		if (strncmp(path_copy, rpath_copy,
9157c478bd9Sstevel@tonic-gate 		    min(strlen(path_copy), strlen(rpath_copy))) == 0) {
9165ee84fbdSgjelinek 			/*
9175ee84fbdSgjelinek 			 * TRANSLATION_NOTE
9185ee84fbdSgjelinek 			 * zonepath is a literal that should not be translated.
9195ee84fbdSgjelinek 			 */
9207c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("%s zonepath (%s) and "
9217c478bd9Sstevel@tonic-gate 			    "%s zonepath (%s) overlap.\n"),
9227c478bd9Sstevel@tonic-gate 			    target_zone, path, ze->zone_name, rpath);
9237c478bd9Sstevel@tonic-gate 			free(ze);
9247c478bd9Sstevel@tonic-gate 			return (Z_ERR);
9257c478bd9Sstevel@tonic-gate 		}
9267c478bd9Sstevel@tonic-gate 		free(ze);
9277c478bd9Sstevel@tonic-gate 	}
9287c478bd9Sstevel@tonic-gate 	endzoneent(cookie);
9297c478bd9Sstevel@tonic-gate 	return (Z_OK);
9307c478bd9Sstevel@tonic-gate }
9317c478bd9Sstevel@tonic-gate 
9327c478bd9Sstevel@tonic-gate static int
validate_zonepath(char * path,int cmd_num)9337c478bd9Sstevel@tonic-gate validate_zonepath(char *path, int cmd_num)
9347c478bd9Sstevel@tonic-gate {
9357c478bd9Sstevel@tonic-gate 	int res;			/* result of last library/system call */
9367c478bd9Sstevel@tonic-gate 	boolean_t err = B_FALSE;	/* have we run into an error? */
9377c478bd9Sstevel@tonic-gate 	struct stat stbuf;
9383f2f09c1Sdp 	struct statvfs64 vfsbuf;
9397c478bd9Sstevel@tonic-gate 	char rpath[MAXPATHLEN];		/* resolved path */
9407c478bd9Sstevel@tonic-gate 	char ppath[MAXPATHLEN];		/* parent path */
9417c478bd9Sstevel@tonic-gate 	char rppath[MAXPATHLEN];	/* resolved parent path */
9427c478bd9Sstevel@tonic-gate 	char rootpath[MAXPATHLEN];	/* root path */
9437c478bd9Sstevel@tonic-gate 	zone_state_t state;
9447c478bd9Sstevel@tonic-gate 
9457c478bd9Sstevel@tonic-gate 	if (path[0] != '/') {
9467c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
9477c478bd9Sstevel@tonic-gate 		    gettext("%s is not an absolute path.\n"), path);
9487c478bd9Sstevel@tonic-gate 		return (Z_ERR);
9497c478bd9Sstevel@tonic-gate 	}
9507c478bd9Sstevel@tonic-gate 	if ((res = resolvepath(path, rpath, sizeof (rpath))) == -1) {
9517c478bd9Sstevel@tonic-gate 		if ((errno != ENOENT) ||
952865e09a4Sgjelinek 		    (cmd_num != CMD_VERIFY && cmd_num != CMD_INSTALL &&
953865e09a4Sgjelinek 		    cmd_num != CMD_CLONE && cmd_num != CMD_MOVE)) {
9547c478bd9Sstevel@tonic-gate 			zperror(path, B_FALSE);
9557c478bd9Sstevel@tonic-gate 			return (Z_ERR);
9567c478bd9Sstevel@tonic-gate 		}
9577c478bd9Sstevel@tonic-gate 		if (cmd_num == CMD_VERIFY) {
9585ee84fbdSgjelinek 			/*
9595ee84fbdSgjelinek 			 * TRANSLATION_NOTE
9605ee84fbdSgjelinek 			 * zoneadm is a literal that should not be translated.
9615ee84fbdSgjelinek 			 */
9627c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("WARNING: %s does not "
9635ee84fbdSgjelinek 			    "exist, so it could not be verified.\nWhen "
9645ee84fbdSgjelinek 			    "'zoneadm %s' is run, '%s' will try to create\n%s, "
9655ee84fbdSgjelinek 			    "and '%s' will be tried again,\nbut the '%s' may "
9665ee84fbdSgjelinek 			    "fail if:\nthe parent directory of %s is group- or "
9675ee84fbdSgjelinek 			    "other-writable\nor\n%s overlaps with any other "
9687c478bd9Sstevel@tonic-gate 			    "installed zones.\n"), path,
9697c478bd9Sstevel@tonic-gate 			    cmd_to_str(CMD_INSTALL), cmd_to_str(CMD_INSTALL),
9707c478bd9Sstevel@tonic-gate 			    path, cmd_to_str(CMD_VERIFY),
9717c478bd9Sstevel@tonic-gate 			    cmd_to_str(CMD_VERIFY), path, path);
9727c478bd9Sstevel@tonic-gate 			return (Z_OK);
9737c478bd9Sstevel@tonic-gate 		}
9747c478bd9Sstevel@tonic-gate 		/*
9757c478bd9Sstevel@tonic-gate 		 * The zonepath is supposed to be mode 700 but its
9767c478bd9Sstevel@tonic-gate 		 * parent(s) 755.  So use 755 on the mkdirp() then
9777c478bd9Sstevel@tonic-gate 		 * chmod() the zonepath itself to 700.
9787c478bd9Sstevel@tonic-gate 		 */
9797c478bd9Sstevel@tonic-gate 		if (mkdirp(path, DEFAULT_DIR_MODE) < 0) {
9807c478bd9Sstevel@tonic-gate 			zperror(path, B_FALSE);
9817c478bd9Sstevel@tonic-gate 			return (Z_ERR);
9827c478bd9Sstevel@tonic-gate 		}
9837c478bd9Sstevel@tonic-gate 		/*
9847c478bd9Sstevel@tonic-gate 		 * If the chmod() fails, report the error, but might
9857c478bd9Sstevel@tonic-gate 		 * as well continue the verify procedure.
9867c478bd9Sstevel@tonic-gate 		 */
9877c478bd9Sstevel@tonic-gate 		if (chmod(path, S_IRWXU) != 0)
9887c478bd9Sstevel@tonic-gate 			zperror(path, B_FALSE);
9897c478bd9Sstevel@tonic-gate 		/*
9907c478bd9Sstevel@tonic-gate 		 * Since the mkdir() succeeded, we should not have to
9917c478bd9Sstevel@tonic-gate 		 * worry about a subsequent ENOENT, thus this should
9927c478bd9Sstevel@tonic-gate 		 * only recurse once.
9937c478bd9Sstevel@tonic-gate 		 */
994865e09a4Sgjelinek 		return (validate_zonepath(path, cmd_num));
9957c478bd9Sstevel@tonic-gate 	}
9967c478bd9Sstevel@tonic-gate 	rpath[res] = '\0';
9977c478bd9Sstevel@tonic-gate 	if (strcmp(path, rpath) != 0) {
9987c478bd9Sstevel@tonic-gate 		errno = Z_RESOLVED_PATH;
9997c478bd9Sstevel@tonic-gate 		zperror(path, B_TRUE);
10007c478bd9Sstevel@tonic-gate 		return (Z_ERR);
10017c478bd9Sstevel@tonic-gate 	}
10027c478bd9Sstevel@tonic-gate 	if ((res = stat(rpath, &stbuf)) != 0) {
10037c478bd9Sstevel@tonic-gate 		zperror(rpath, B_FALSE);
10047c478bd9Sstevel@tonic-gate 		return (Z_ERR);
10057c478bd9Sstevel@tonic-gate 	}
10067c478bd9Sstevel@tonic-gate 	if (!S_ISDIR(stbuf.st_mode)) {
10077c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("%s is not a directory.\n"),
10087c478bd9Sstevel@tonic-gate 		    rpath);
10097c478bd9Sstevel@tonic-gate 		return (Z_ERR);
10107c478bd9Sstevel@tonic-gate 	}
10114fceebdfSblakej 	if (strcmp(stbuf.st_fstype, MNTTYPE_TMPFS) == 0) {
10127c478bd9Sstevel@tonic-gate 		(void) printf(gettext("WARNING: %s is on a temporary "
10130b5de56dSgjelinek 		    "file system.\n"), rpath);
10147c478bd9Sstevel@tonic-gate 	}
10157c478bd9Sstevel@tonic-gate 	if (crosscheck_zonepaths(rpath) != Z_OK)
10167c478bd9Sstevel@tonic-gate 		return (Z_ERR);
10177c478bd9Sstevel@tonic-gate 	/*
10187c478bd9Sstevel@tonic-gate 	 * Try to collect and report as many minor errors as possible
10197c478bd9Sstevel@tonic-gate 	 * before returning, so the user can learn everything that needs
10207c478bd9Sstevel@tonic-gate 	 * to be fixed up front.
10217c478bd9Sstevel@tonic-gate 	 */
10227c478bd9Sstevel@tonic-gate 	if (stbuf.st_uid != 0) {
10237c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("%s is not owned by root.\n"),
10247c478bd9Sstevel@tonic-gate 		    rpath);
10257c478bd9Sstevel@tonic-gate 		err = B_TRUE;
1026*b6831eafSAlexander Eremin 
1027*b6831eafSAlexander Eremin 		/* Try to change owner */
1028*b6831eafSAlexander Eremin 		if (cmd_num != CMD_VERIFY) {
1029*b6831eafSAlexander Eremin 			(void) fprintf(stderr, gettext("%s: changing owner "
1030*b6831eafSAlexander Eremin 			    "to root.\n"), rpath);
1031*b6831eafSAlexander Eremin 			if (chown(rpath, 0, -1) != 0) {
1032*b6831eafSAlexander Eremin 				zperror(rpath, B_FALSE);
1033*b6831eafSAlexander Eremin 				return (Z_ERR);
1034*b6831eafSAlexander Eremin 			} else {
1035*b6831eafSAlexander Eremin 				err = B_FALSE;
1036*b6831eafSAlexander Eremin 			}
1037*b6831eafSAlexander Eremin 		}
10387c478bd9Sstevel@tonic-gate 	}
10397c478bd9Sstevel@tonic-gate 	err |= bad_mode_bit(stbuf.st_mode, S_IRUSR, B_TRUE, rpath);
10407c478bd9Sstevel@tonic-gate 	err |= bad_mode_bit(stbuf.st_mode, S_IWUSR, B_TRUE, rpath);
10417c478bd9Sstevel@tonic-gate 	err |= bad_mode_bit(stbuf.st_mode, S_IXUSR, B_TRUE, rpath);
10427c478bd9Sstevel@tonic-gate 	err |= bad_mode_bit(stbuf.st_mode, S_IRGRP, B_FALSE, rpath);
10437c478bd9Sstevel@tonic-gate 	err |= bad_mode_bit(stbuf.st_mode, S_IWGRP, B_FALSE, rpath);
10447c478bd9Sstevel@tonic-gate 	err |= bad_mode_bit(stbuf.st_mode, S_IXGRP, B_FALSE, rpath);
10457c478bd9Sstevel@tonic-gate 	err |= bad_mode_bit(stbuf.st_mode, S_IROTH, B_FALSE, rpath);
10467c478bd9Sstevel@tonic-gate 	err |= bad_mode_bit(stbuf.st_mode, S_IWOTH, B_FALSE, rpath);
10477c478bd9Sstevel@tonic-gate 	err |= bad_mode_bit(stbuf.st_mode, S_IXOTH, B_FALSE, rpath);
10487c478bd9Sstevel@tonic-gate 
1049*b6831eafSAlexander Eremin 	/* If the group perms are wrong, fix them */
1050*b6831eafSAlexander Eremin 	if (err && (cmd_num != CMD_VERIFY)) {
1051*b6831eafSAlexander Eremin 		(void) fprintf(stderr, gettext("%s: changing permissions "
1052*b6831eafSAlexander Eremin 		    "to 0700.\n"), rpath);
1053*b6831eafSAlexander Eremin 		if (chmod(rpath, S_IRWXU) != 0) {
1054*b6831eafSAlexander Eremin 			zperror(path, B_FALSE);
1055*b6831eafSAlexander Eremin 		} else {
1056*b6831eafSAlexander Eremin 			err = B_FALSE;
1057*b6831eafSAlexander Eremin 		}
1058*b6831eafSAlexander Eremin 	}
1059*b6831eafSAlexander Eremin 
10607c478bd9Sstevel@tonic-gate 	(void) snprintf(ppath, sizeof (ppath), "%s/..", path);
10617c478bd9Sstevel@tonic-gate 	if ((res = resolvepath(ppath, rppath, sizeof (rppath))) == -1) {
10627c478bd9Sstevel@tonic-gate 		zperror(ppath, B_FALSE);
10637c478bd9Sstevel@tonic-gate 		return (Z_ERR);
10647c478bd9Sstevel@tonic-gate 	}
10657c478bd9Sstevel@tonic-gate 	rppath[res] = '\0';
10667c478bd9Sstevel@tonic-gate 	if ((res = stat(rppath, &stbuf)) != 0) {
10677c478bd9Sstevel@tonic-gate 		zperror(rppath, B_FALSE);
10687c478bd9Sstevel@tonic-gate 		return (Z_ERR);
10697c478bd9Sstevel@tonic-gate 	}
10707c478bd9Sstevel@tonic-gate 	/* theoretically impossible */
10717c478bd9Sstevel@tonic-gate 	if (!S_ISDIR(stbuf.st_mode)) {
10727c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("%s is not a directory.\n"),
10737c478bd9Sstevel@tonic-gate 		    rppath);
10747c478bd9Sstevel@tonic-gate 		return (Z_ERR);
10757c478bd9Sstevel@tonic-gate 	}
10767c478bd9Sstevel@tonic-gate 	if (stbuf.st_uid != 0) {
10777c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("%s is not owned by root.\n"),
10787c478bd9Sstevel@tonic-gate 		    rppath);
10797c478bd9Sstevel@tonic-gate 		err = B_TRUE;
10807c478bd9Sstevel@tonic-gate 	}
10817c478bd9Sstevel@tonic-gate 	err |= bad_mode_bit(stbuf.st_mode, S_IRUSR, B_TRUE, rppath);
10827c478bd9Sstevel@tonic-gate 	err |= bad_mode_bit(stbuf.st_mode, S_IWUSR, B_TRUE, rppath);
10837c478bd9Sstevel@tonic-gate 	err |= bad_mode_bit(stbuf.st_mode, S_IXUSR, B_TRUE, rppath);
10847c478bd9Sstevel@tonic-gate 	err |= bad_mode_bit(stbuf.st_mode, S_IWGRP, B_FALSE, rppath);
10857c478bd9Sstevel@tonic-gate 	err |= bad_mode_bit(stbuf.st_mode, S_IWOTH, B_FALSE, rppath);
10867c478bd9Sstevel@tonic-gate 	if (strcmp(rpath, rppath) == 0) {
10877c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("%s is its own parent.\n"),
10887c478bd9Sstevel@tonic-gate 		    rppath);
10897c478bd9Sstevel@tonic-gate 		err = B_TRUE;
10907c478bd9Sstevel@tonic-gate 	}
10917c478bd9Sstevel@tonic-gate 
10923f2f09c1Sdp 	if (statvfs64(rpath, &vfsbuf) != 0) {
10937c478bd9Sstevel@tonic-gate 		zperror(rpath, B_FALSE);
10947c478bd9Sstevel@tonic-gate 		return (Z_ERR);
10957c478bd9Sstevel@tonic-gate 	}
1096b5abaf04Sgjelinek 	if (strcmp(vfsbuf.f_basetype, MNTTYPE_NFS) == 0) {
10975ee84fbdSgjelinek 		/*
10985ee84fbdSgjelinek 		 * TRANSLATION_NOTE
10995ee84fbdSgjelinek 		 * Zonepath and NFS are literals that should not be translated.
11005ee84fbdSgjelinek 		 */
11015ee84fbdSgjelinek 		(void) fprintf(stderr, gettext("Zonepath %s is on an NFS "
11020b5de56dSgjelinek 		    "mounted file system.\n"
11030b5de56dSgjelinek 		    "\tA local file system must be used.\n"), rpath);
1104b5abaf04Sgjelinek 		return (Z_ERR);
1105b5abaf04Sgjelinek 	}
1106b5abaf04Sgjelinek 	if (vfsbuf.f_flag & ST_NOSUID) {
11075ee84fbdSgjelinek 		/*
11085ee84fbdSgjelinek 		 * TRANSLATION_NOTE
11095ee84fbdSgjelinek 		 * Zonepath and nosuid are literals that should not be
11105ee84fbdSgjelinek 		 * translated.
11115ee84fbdSgjelinek 		 */
1112b5abaf04Sgjelinek 		(void) fprintf(stderr, gettext("Zonepath %s is on a nosuid "
11130b5de56dSgjelinek 		    "file system.\n"), rpath);
11147c478bd9Sstevel@tonic-gate 		return (Z_ERR);
11157c478bd9Sstevel@tonic-gate 	}
11167c478bd9Sstevel@tonic-gate 
11177c478bd9Sstevel@tonic-gate 	if ((res = zone_get_state(target_zone, &state)) != Z_OK) {
11187c478bd9Sstevel@tonic-gate 		errno = res;
11197c478bd9Sstevel@tonic-gate 		zperror2(target_zone, gettext("could not get state"));
11207c478bd9Sstevel@tonic-gate 		return (Z_ERR);
11217c478bd9Sstevel@tonic-gate 	}
11227c478bd9Sstevel@tonic-gate 	/*
11237c478bd9Sstevel@tonic-gate 	 * The existence of the root path is only bad in the configured state,
11247c478bd9Sstevel@tonic-gate 	 * as it is *supposed* to be there at the installed and later states.
1125ee519a1fSgjelinek 	 * However, the root path is expected to be there if the zone is
1126ee519a1fSgjelinek 	 * detached.
11277c478bd9Sstevel@tonic-gate 	 * State/command mismatches are caught earlier in verify_details().
11287c478bd9Sstevel@tonic-gate 	 */
1129ee519a1fSgjelinek 	if (state == ZONE_STATE_CONFIGURED && cmd_num != CMD_ATTACH) {
11307c478bd9Sstevel@tonic-gate 		if (snprintf(rootpath, sizeof (rootpath), "%s/root", rpath) >=
11317c478bd9Sstevel@tonic-gate 		    sizeof (rootpath)) {
11325ee84fbdSgjelinek 			/*
11335ee84fbdSgjelinek 			 * TRANSLATION_NOTE
11345ee84fbdSgjelinek 			 * Zonepath is a literal that should not be translated.
11355ee84fbdSgjelinek 			 */
11367c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
11377c478bd9Sstevel@tonic-gate 			    gettext("Zonepath %s is too long.\n"), rpath);
11387c478bd9Sstevel@tonic-gate 			return (Z_ERR);
11397c478bd9Sstevel@tonic-gate 		}
11407c478bd9Sstevel@tonic-gate 		if ((res = stat(rootpath, &stbuf)) == 0) {
1141c4a45622S 			if (zonecfg_detached(rpath)) {
1142ee519a1fSgjelinek 				(void) fprintf(stderr,
1143ee519a1fSgjelinek 				    gettext("Cannot %s detached "
1144ee519a1fSgjelinek 				    "zone.\nUse attach or remove %s "
1145ee519a1fSgjelinek 				    "directory.\n"), cmd_to_str(cmd_num),
1146ee519a1fSgjelinek 				    rpath);
11477c478bd9Sstevel@tonic-gate 				return (Z_ERR);
11487c478bd9Sstevel@tonic-gate 			}
1149c4a45622S 
1150c4a45622S 			/* Not detached, check if it really looks ok. */
1151c4a45622S 
1152c4a45622S 			if (!S_ISDIR(stbuf.st_mode)) {
1153c4a45622S 				(void) fprintf(stderr, gettext("%s is not a "
1154c4a45622S 				    "directory.\n"), rootpath);
1155c4a45622S 				return (Z_ERR);
1156c4a45622S 			}
1157c4a45622S 
1158c4a45622S 			if (stbuf.st_uid != 0) {
1159c4a45622S 				(void) fprintf(stderr, gettext("%s is not "
1160c4a45622S 				    "owned by root.\n"), rootpath);
1161c4a45622S 				return (Z_ERR);
1162c4a45622S 			}
1163c4a45622S 
1164c4a45622S 			if ((stbuf.st_mode & 0777) != 0755) {
1165c4a45622S 				(void) fprintf(stderr, gettext("%s mode is not "
1166c4a45622S 				    "0755.\n"), rootpath);
1167c4a45622S 				return (Z_ERR);
1168c4a45622S 			}
1169c4a45622S 		}
11707c478bd9Sstevel@tonic-gate 	}
11717c478bd9Sstevel@tonic-gate 
11727c478bd9Sstevel@tonic-gate 	return (err ? Z_ERR : Z_OK);
11737c478bd9Sstevel@tonic-gate }
11747c478bd9Sstevel@tonic-gate 
11757c478bd9Sstevel@tonic-gate static int
invoke_brand_handler(int cmd_num,char * argv[])1176ce28b40eSzt129084 invoke_brand_handler(int cmd_num, char *argv[])
1177ce28b40eSzt129084 {
1178ce28b40eSzt129084 	zone_dochandle_t handle;
1179ce28b40eSzt129084 	int err;
1180ce28b40eSzt129084 
1181ce28b40eSzt129084 	if ((handle = zonecfg_init_handle()) == NULL) {
1182ce28b40eSzt129084 		zperror(cmd_to_str(cmd_num), B_TRUE);
1183ce28b40eSzt129084 		return (Z_ERR);
1184ce28b40eSzt129084 	}
1185ce28b40eSzt129084 	if ((err = zonecfg_get_handle(target_zone, handle)) != Z_OK) {
1186ce28b40eSzt129084 		errno = err;
1187ce28b40eSzt129084 		zperror(cmd_to_str(cmd_num), B_TRUE);
1188ce28b40eSzt129084 		zonecfg_fini_handle(handle);
1189ce28b40eSzt129084 		return (Z_ERR);
1190ce28b40eSzt129084 	}
1191ce28b40eSzt129084 	if (verify_brand(handle, cmd_num, argv) != Z_OK) {
1192ce28b40eSzt129084 		zonecfg_fini_handle(handle);
1193ce28b40eSzt129084 		return (Z_ERR);
1194ce28b40eSzt129084 	}
1195ce28b40eSzt129084 	zonecfg_fini_handle(handle);
1196ce28b40eSzt129084 	return (Z_OK);
1197ce28b40eSzt129084 }
1198ce28b40eSzt129084 
1199ce28b40eSzt129084 static int
ready_func(int argc,char * argv[])12007c478bd9Sstevel@tonic-gate ready_func(int argc, char *argv[])
12017c478bd9Sstevel@tonic-gate {
12027c478bd9Sstevel@tonic-gate 	zone_cmd_arg_t zarg;
12037c478bd9Sstevel@tonic-gate 	int arg;
12047c478bd9Sstevel@tonic-gate 
1205108322fbScarlsonj 	if (zonecfg_in_alt_root()) {
1206108322fbScarlsonj 		zerror(gettext("cannot ready zone in alternate root"));
1207108322fbScarlsonj 		return (Z_ERR);
1208108322fbScarlsonj 	}
1209108322fbScarlsonj 
12107c478bd9Sstevel@tonic-gate 	optind = 0;
12117c478bd9Sstevel@tonic-gate 	if ((arg = getopt(argc, argv, "?")) != EOF) {
12127c478bd9Sstevel@tonic-gate 		switch (arg) {
12137c478bd9Sstevel@tonic-gate 		case '?':
12147c478bd9Sstevel@tonic-gate 			sub_usage(SHELP_READY, CMD_READY);
12157c478bd9Sstevel@tonic-gate 			return (optopt == '?' ? Z_OK : Z_USAGE);
12167c478bd9Sstevel@tonic-gate 		default:
12177c478bd9Sstevel@tonic-gate 			sub_usage(SHELP_READY, CMD_READY);
12187c478bd9Sstevel@tonic-gate 			return (Z_USAGE);
12197c478bd9Sstevel@tonic-gate 		}
12207c478bd9Sstevel@tonic-gate 	}
12217c478bd9Sstevel@tonic-gate 	if (argc > optind) {
12227c478bd9Sstevel@tonic-gate 		sub_usage(SHELP_READY, CMD_READY);
12237c478bd9Sstevel@tonic-gate 		return (Z_USAGE);
12247c478bd9Sstevel@tonic-gate 	}
12259acbbeafSnn35248 	if (sanity_check(target_zone, CMD_READY, B_FALSE, B_FALSE, B_FALSE)
12269acbbeafSnn35248 	    != Z_OK)
12277c478bd9Sstevel@tonic-gate 		return (Z_ERR);
1228ce28b40eSzt129084 	if (verify_details(CMD_READY, argv) != Z_OK)
12297c478bd9Sstevel@tonic-gate 		return (Z_ERR);
12307c478bd9Sstevel@tonic-gate 
12317c478bd9Sstevel@tonic-gate 	zarg.cmd = Z_READY;
1232ff17c8bfSgjelinek 	if (zonecfg_call_zoneadmd(target_zone, &zarg, locale, B_TRUE) != 0) {
12337c478bd9Sstevel@tonic-gate 		zerror(gettext("call to %s failed"), "zoneadmd");
12347c478bd9Sstevel@tonic-gate 		return (Z_ERR);
12357c478bd9Sstevel@tonic-gate 	}
12367c478bd9Sstevel@tonic-gate 	return (Z_OK);
12377c478bd9Sstevel@tonic-gate }
12387c478bd9Sstevel@tonic-gate 
12397c478bd9Sstevel@tonic-gate static int
boot_func(int argc,char * argv[])12407c478bd9Sstevel@tonic-gate boot_func(int argc, char *argv[])
12417c478bd9Sstevel@tonic-gate {
12427c478bd9Sstevel@tonic-gate 	zone_cmd_arg_t zarg;
12439acbbeafSnn35248 	boolean_t force = B_FALSE;
12447c478bd9Sstevel@tonic-gate 	int arg;
12457c478bd9Sstevel@tonic-gate 
1246108322fbScarlsonj 	if (zonecfg_in_alt_root()) {
1247108322fbScarlsonj 		zerror(gettext("cannot boot zone in alternate root"));
1248108322fbScarlsonj 		return (Z_ERR);
1249108322fbScarlsonj 	}
1250108322fbScarlsonj 
12517c478bd9Sstevel@tonic-gate 	zarg.bootbuf[0] = '\0';
12527c478bd9Sstevel@tonic-gate 
12537c478bd9Sstevel@tonic-gate 	/*
12543f2f09c1Sdp 	 * The following getopt processes arguments to zone boot; that
12553f2f09c1Sdp 	 * is to say, the [here] portion of the argument string:
12563f2f09c1Sdp 	 *
12573f2f09c1Sdp 	 *	zoneadm -z myzone boot [here] -- -v -m verbose
12583f2f09c1Sdp 	 *
12593f2f09c1Sdp 	 * Where [here] can either be nothing, -? (in which case we bail
12609acbbeafSnn35248 	 * and print usage), -f (a private option to indicate that the
12619acbbeafSnn35248 	 * boot operation should be 'forced'), or -s.  Support for -s is
12629acbbeafSnn35248 	 * vestigal and obsolete, but is retained because it was a
12639acbbeafSnn35248 	 * documented interface and there are known consumers including
12649acbbeafSnn35248 	 * admin/install; the proper way to specify boot arguments like -s
12659acbbeafSnn35248 	 * is:
12663f2f09c1Sdp 	 *
12673f2f09c1Sdp 	 *	zoneadm -z myzone boot -- -s -v -m verbose.
12687c478bd9Sstevel@tonic-gate 	 */
12697c478bd9Sstevel@tonic-gate 	optind = 0;
12709acbbeafSnn35248 	while ((arg = getopt(argc, argv, "?fs")) != EOF) {
12717c478bd9Sstevel@tonic-gate 		switch (arg) {
12727c478bd9Sstevel@tonic-gate 		case '?':
12737c478bd9Sstevel@tonic-gate 			sub_usage(SHELP_BOOT, CMD_BOOT);
12747c478bd9Sstevel@tonic-gate 			return (optopt == '?' ? Z_OK : Z_USAGE);
12757c478bd9Sstevel@tonic-gate 		case 's':
12767c478bd9Sstevel@tonic-gate 			(void) strlcpy(zarg.bootbuf, "-s",
12777c478bd9Sstevel@tonic-gate 			    sizeof (zarg.bootbuf));
12787c478bd9Sstevel@tonic-gate 			break;
12799acbbeafSnn35248 		case 'f':
12809acbbeafSnn35248 			force = B_TRUE;
12819acbbeafSnn35248 			break;
12827c478bd9Sstevel@tonic-gate 		default:
12837c478bd9Sstevel@tonic-gate 			sub_usage(SHELP_BOOT, CMD_BOOT);
12847c478bd9Sstevel@tonic-gate 			return (Z_USAGE);
12857c478bd9Sstevel@tonic-gate 		}
12867c478bd9Sstevel@tonic-gate 	}
12873f2f09c1Sdp 
12883f2f09c1Sdp 	for (; optind < argc; optind++) {
12893f2f09c1Sdp 		if (strlcat(zarg.bootbuf, argv[optind],
12903f2f09c1Sdp 		    sizeof (zarg.bootbuf)) >= sizeof (zarg.bootbuf)) {
12913f2f09c1Sdp 			zerror(gettext("Boot argument list too long"));
12923f2f09c1Sdp 			return (Z_ERR);
12937c478bd9Sstevel@tonic-gate 		}
12943f2f09c1Sdp 		if (optind < argc - 1)
12953f2f09c1Sdp 			if (strlcat(zarg.bootbuf, " ", sizeof (zarg.bootbuf)) >=
12963f2f09c1Sdp 			    sizeof (zarg.bootbuf)) {
12973f2f09c1Sdp 				zerror(gettext("Boot argument list too long"));
12983f2f09c1Sdp 				return (Z_ERR);
12993f2f09c1Sdp 			}
13003f2f09c1Sdp 	}
13019acbbeafSnn35248 	if (sanity_check(target_zone, CMD_BOOT, B_FALSE, B_FALSE, force)
13029acbbeafSnn35248 	    != Z_OK)
13037c478bd9Sstevel@tonic-gate 		return (Z_ERR);
1304ce28b40eSzt129084 	if (verify_details(CMD_BOOT, argv) != Z_OK)
13057c478bd9Sstevel@tonic-gate 		return (Z_ERR);
13069acbbeafSnn35248 	zarg.cmd = force ? Z_FORCEBOOT : Z_BOOT;
1307ff17c8bfSgjelinek 	if (zonecfg_call_zoneadmd(target_zone, &zarg, locale, B_TRUE) != 0) {
13087c478bd9Sstevel@tonic-gate 		zerror(gettext("call to %s failed"), "zoneadmd");
13097c478bd9Sstevel@tonic-gate 		return (Z_ERR);
13107c478bd9Sstevel@tonic-gate 	}
13110209230bSgjelinek 
13127c478bd9Sstevel@tonic-gate 	return (Z_OK);
13137c478bd9Sstevel@tonic-gate }
13147c478bd9Sstevel@tonic-gate 
13157c478bd9Sstevel@tonic-gate static void
fake_up_local_zone(zoneid_t zid,zone_entry_t * zeptr)13167c478bd9Sstevel@tonic-gate fake_up_local_zone(zoneid_t zid, zone_entry_t *zeptr)
13177c478bd9Sstevel@tonic-gate {
13187c478bd9Sstevel@tonic-gate 	ssize_t result;
1319555afedfScarlsonj 	uuid_t uuid;
1320555afedfScarlsonj 	FILE *fp;
1321f4b3ec61Sdh155122 	ushort_t flags;
1322555afedfScarlsonj 
1323555afedfScarlsonj 	(void) memset(zeptr, 0, sizeof (*zeptr));
13247c478bd9Sstevel@tonic-gate 
13257c478bd9Sstevel@tonic-gate 	zeptr->zid = zid;
1326555afedfScarlsonj 
13277c478bd9Sstevel@tonic-gate 	/*
13287c478bd9Sstevel@tonic-gate 	 * Since we're looking up our own (non-global) zone name,
13297c478bd9Sstevel@tonic-gate 	 * we can be assured that it will succeed.
13307c478bd9Sstevel@tonic-gate 	 */
13317c478bd9Sstevel@tonic-gate 	result = getzonenamebyid(zid, zeptr->zname, sizeof (zeptr->zname));
13327c478bd9Sstevel@tonic-gate 	assert(result >= 0);
1333555afedfScarlsonj 	if (zonecfg_is_scratch(zeptr->zname) &&
1334555afedfScarlsonj 	    (fp = zonecfg_open_scratch("", B_FALSE)) != NULL) {
1335555afedfScarlsonj 		(void) zonecfg_reverse_scratch(fp, zeptr->zname, zeptr->zname,
1336555afedfScarlsonj 		    sizeof (zeptr->zname), NULL, 0);
1337555afedfScarlsonj 		zonecfg_close_scratch(fp);
1338555afedfScarlsonj 	}
1339555afedfScarlsonj 
1340555afedfScarlsonj 	if (is_system_labeled()) {
134145916cd2Sjpk 		(void) zone_getattr(zid, ZONE_ATTR_ROOT, zeptr->zroot,
134245916cd2Sjpk 		    sizeof (zeptr->zroot));
13439acbbeafSnn35248 		(void) strlcpy(zeptr->zbrand, NATIVE_BRAND_NAME,
13449acbbeafSnn35248 		    sizeof (zeptr->zbrand));
1345555afedfScarlsonj 	} else {
1346555afedfScarlsonj 		(void) strlcpy(zeptr->zroot, "/", sizeof (zeptr->zroot));
13479acbbeafSnn35248 		(void) zone_getattr(zid, ZONE_ATTR_BRAND, zeptr->zbrand,
13489acbbeafSnn35248 		    sizeof (zeptr->zbrand));
134945916cd2Sjpk 	}
1350555afedfScarlsonj 
13517c478bd9Sstevel@tonic-gate 	zeptr->zstate_str = "running";
1352555afedfScarlsonj 	if (zonecfg_get_uuid(zeptr->zname, uuid) == Z_OK &&
1353555afedfScarlsonj 	    !uuid_is_null(uuid))
1354555afedfScarlsonj 		uuid_unparse(uuid, zeptr->zuuid);
1355f4b3ec61Sdh155122 
1356f4b3ec61Sdh155122 	if (zone_getattr(zid, ZONE_ATTR_FLAGS, &flags, sizeof (flags)) < 0) {
1357f4b3ec61Sdh155122 		zperror2(zeptr->zname, gettext("could not get zone flags"));
1358f4b3ec61Sdh155122 		exit(Z_ERR);
1359f4b3ec61Sdh155122 	}
1360f4b3ec61Sdh155122 	if (flags & ZF_NET_EXCL)
1361f4b3ec61Sdh155122 		zeptr->ziptype = ZS_EXCLUSIVE;
1362f4b3ec61Sdh155122 	else
1363f4b3ec61Sdh155122 		zeptr->ziptype = ZS_SHARED;
13647c478bd9Sstevel@tonic-gate }
13657c478bd9Sstevel@tonic-gate 
13667c478bd9Sstevel@tonic-gate static int
list_func(int argc,char * argv[])13677c478bd9Sstevel@tonic-gate list_func(int argc, char *argv[])
13687c478bd9Sstevel@tonic-gate {
13697c478bd9Sstevel@tonic-gate 	zone_entry_t *zentp, zent;
1370108322fbScarlsonj 	int arg, retv;
13717c478bd9Sstevel@tonic-gate 	boolean_t output = B_FALSE, verbose = B_FALSE, parsable = B_FALSE;
13727c478bd9Sstevel@tonic-gate 	zone_state_t min_state = ZONE_STATE_RUNNING;
13737c478bd9Sstevel@tonic-gate 	zoneid_t zone_id = getzoneid();
13747c478bd9Sstevel@tonic-gate 
13757c478bd9Sstevel@tonic-gate 	if (target_zone == NULL) {
13767c478bd9Sstevel@tonic-gate 		/* all zones: default view to running but allow override */
13777c478bd9Sstevel@tonic-gate 		optind = 0;
13787c478bd9Sstevel@tonic-gate 		while ((arg = getopt(argc, argv, "?cipv")) != EOF) {
13797c478bd9Sstevel@tonic-gate 			switch (arg) {
13807c478bd9Sstevel@tonic-gate 			case '?':
13817c478bd9Sstevel@tonic-gate 				sub_usage(SHELP_LIST, CMD_LIST);
13827c478bd9Sstevel@tonic-gate 				return (optopt == '?' ? Z_OK : Z_USAGE);
1383475d78a4Sjonb 				/*
1384475d78a4Sjonb 				 * The 'i' and 'c' options are not mutually
1385475d78a4Sjonb 				 * exclusive so if 'c' is given, then min_state
1386475d78a4Sjonb 				 * is set to 0 (ZONE_STATE_CONFIGURED) which is
1387475d78a4Sjonb 				 * the lowest possible state.  If 'i' is given,
1388475d78a4Sjonb 				 * then min_state is set to be the lowest state
1389475d78a4Sjonb 				 * so far.
1390475d78a4Sjonb 				 */
13917c478bd9Sstevel@tonic-gate 			case 'c':
13927c478bd9Sstevel@tonic-gate 				min_state = ZONE_STATE_CONFIGURED;
13937c478bd9Sstevel@tonic-gate 				break;
13947c478bd9Sstevel@tonic-gate 			case 'i':
1395475d78a4Sjonb 				min_state = min(ZONE_STATE_INSTALLED,
1396475d78a4Sjonb 				    min_state);
1397475d78a4Sjonb 
13987c478bd9Sstevel@tonic-gate 				break;
13997c478bd9Sstevel@tonic-gate 			case 'p':
14007c478bd9Sstevel@tonic-gate 				parsable = B_TRUE;
14017c478bd9Sstevel@tonic-gate 				break;
14027c478bd9Sstevel@tonic-gate 			case 'v':
14037c478bd9Sstevel@tonic-gate 				verbose = B_TRUE;
14047c478bd9Sstevel@tonic-gate 				break;
14057c478bd9Sstevel@tonic-gate 			default:
14067c478bd9Sstevel@tonic-gate 				sub_usage(SHELP_LIST, CMD_LIST);
14077c478bd9Sstevel@tonic-gate 				return (Z_USAGE);
14087c478bd9Sstevel@tonic-gate 			}
14097c478bd9Sstevel@tonic-gate 		}
14107c478bd9Sstevel@tonic-gate 		if (parsable && verbose) {
14117c478bd9Sstevel@tonic-gate 			zerror(gettext("%s -p and -v are mutually exclusive."),
14127c478bd9Sstevel@tonic-gate 			    cmd_to_str(CMD_LIST));
14137c478bd9Sstevel@tonic-gate 			return (Z_ERR);
14147c478bd9Sstevel@tonic-gate 		}
141545916cd2Sjpk 		if (zone_id == GLOBAL_ZONEID || is_system_labeled()) {
1416108322fbScarlsonj 			retv = zone_print_list(min_state, verbose, parsable);
14177c478bd9Sstevel@tonic-gate 		} else {
14187c478bd9Sstevel@tonic-gate 			fake_up_local_zone(zone_id, &zent);
14199acbbeafSnn35248 			retv = Z_OK;
14207c478bd9Sstevel@tonic-gate 			zone_print(&zent, verbose, parsable);
14217c478bd9Sstevel@tonic-gate 		}
1422108322fbScarlsonj 		return (retv);
14237c478bd9Sstevel@tonic-gate 	}
14247c478bd9Sstevel@tonic-gate 
14257c478bd9Sstevel@tonic-gate 	/*
14267c478bd9Sstevel@tonic-gate 	 * Specific target zone: disallow -i/-c suboptions.
14277c478bd9Sstevel@tonic-gate 	 */
14287c478bd9Sstevel@tonic-gate 	optind = 0;
14297c478bd9Sstevel@tonic-gate 	while ((arg = getopt(argc, argv, "?pv")) != EOF) {
14307c478bd9Sstevel@tonic-gate 		switch (arg) {
14317c478bd9Sstevel@tonic-gate 		case '?':
14327c478bd9Sstevel@tonic-gate 			sub_usage(SHELP_LIST, CMD_LIST);
14337c478bd9Sstevel@tonic-gate 			return (optopt == '?' ? Z_OK : Z_USAGE);
14347c478bd9Sstevel@tonic-gate 		case 'p':
14357c478bd9Sstevel@tonic-gate 			parsable = B_TRUE;
14367c478bd9Sstevel@tonic-gate 			break;
14377c478bd9Sstevel@tonic-gate 		case 'v':
14387c478bd9Sstevel@tonic-gate 			verbose = B_TRUE;
14397c478bd9Sstevel@tonic-gate 			break;
14407c478bd9Sstevel@tonic-gate 		default:
14417c478bd9Sstevel@tonic-gate 			sub_usage(SHELP_LIST, CMD_LIST);
14427c478bd9Sstevel@tonic-gate 			return (Z_USAGE);
14437c478bd9Sstevel@tonic-gate 		}
14447c478bd9Sstevel@tonic-gate 	}
14457c478bd9Sstevel@tonic-gate 	if (parsable && verbose) {
14467c478bd9Sstevel@tonic-gate 		zerror(gettext("%s -p and -v are mutually exclusive."),
14477c478bd9Sstevel@tonic-gate 		    cmd_to_str(CMD_LIST));
14487c478bd9Sstevel@tonic-gate 		return (Z_ERR);
14497c478bd9Sstevel@tonic-gate 	}
14507c478bd9Sstevel@tonic-gate 	if (argc > optind) {
14517c478bd9Sstevel@tonic-gate 		sub_usage(SHELP_LIST, CMD_LIST);
14527c478bd9Sstevel@tonic-gate 		return (Z_USAGE);
14537c478bd9Sstevel@tonic-gate 	}
14542ec67e04Sgjelinek 	if (zone_id != GLOBAL_ZONEID && !is_system_labeled()) {
14557c478bd9Sstevel@tonic-gate 		fake_up_local_zone(zone_id, &zent);
14567c478bd9Sstevel@tonic-gate 		/*
14577c478bd9Sstevel@tonic-gate 		 * main() will issue a Z_NO_ZONE error if it cannot get an
14587c478bd9Sstevel@tonic-gate 		 * id for target_zone, which in a non-global zone should
14597c478bd9Sstevel@tonic-gate 		 * happen for any zone name except `zonename`.  Thus we
14607c478bd9Sstevel@tonic-gate 		 * assert() that here but don't otherwise check.
14617c478bd9Sstevel@tonic-gate 		 */
14627c478bd9Sstevel@tonic-gate 		assert(strcmp(zent.zname, target_zone) == 0);
14637c478bd9Sstevel@tonic-gate 		zone_print(&zent, verbose, parsable);
14647c478bd9Sstevel@tonic-gate 		output = B_TRUE;
14657c478bd9Sstevel@tonic-gate 	} else if ((zentp = lookup_running_zone(target_zone)) != NULL) {
14667c478bd9Sstevel@tonic-gate 		zone_print(zentp, verbose, parsable);
14677c478bd9Sstevel@tonic-gate 		output = B_TRUE;
1468108322fbScarlsonj 	} else if (lookup_zone_info(target_zone, ZONE_ID_UNDEFINED,
1469108322fbScarlsonj 	    &zent) == Z_OK) {
14707c478bd9Sstevel@tonic-gate 		zone_print(&zent, verbose, parsable);
14717c478bd9Sstevel@tonic-gate 		output = B_TRUE;
14727c478bd9Sstevel@tonic-gate 	}
1473ce28b40eSzt129084 
1474ce28b40eSzt129084 	/*
1475ce28b40eSzt129084 	 * Invoke brand-specific handler. Note that we do this
1476db628066Sgjelinek 	 * only if we're in the global zone, and target_zone is specified
1477db628066Sgjelinek 	 * and it is not the global zone.
1478ce28b40eSzt129084 	 */
1479db628066Sgjelinek 	if (zone_id == GLOBAL_ZONEID && target_zone != NULL &&
1480db628066Sgjelinek 	    strcmp(target_zone, GLOBAL_ZONENAME) != 0)
1481ce28b40eSzt129084 		if (invoke_brand_handler(CMD_LIST, argv) != Z_OK)
1482ce28b40eSzt129084 			return (Z_ERR);
1483ce28b40eSzt129084 
14847c478bd9Sstevel@tonic-gate 	return (output ? Z_OK : Z_ERR);
14857c478bd9Sstevel@tonic-gate }
14867c478bd9Sstevel@tonic-gate 
1487ff17c8bfSgjelinek int
do_subproc(char * cmdbuf)1488c75cc341S do_subproc(char *cmdbuf)
14899acbbeafSnn35248 {
14909acbbeafSnn35248 	void (*saveint)(int);
14919acbbeafSnn35248 	void (*saveterm)(int);
14929acbbeafSnn35248 	void (*savequit)(int);
14939acbbeafSnn35248 	void (*savehup)(int);
14949acbbeafSnn35248 	int pid, child, status;
14959acbbeafSnn35248 
14969acbbeafSnn35248 	if ((child = vfork()) == 0) {
14979acbbeafSnn35248 		(void) execl("/bin/sh", "sh", "-c", cmdbuf, (char *)NULL);
14989acbbeafSnn35248 	}
14999acbbeafSnn35248 
15009acbbeafSnn35248 	if (child == -1)
15019acbbeafSnn35248 		return (-1);
15029acbbeafSnn35248 
15039acbbeafSnn35248 	saveint = sigset(SIGINT, SIG_IGN);
15049acbbeafSnn35248 	saveterm = sigset(SIGTERM, SIG_IGN);
15059acbbeafSnn35248 	savequit = sigset(SIGQUIT, SIG_IGN);
15069acbbeafSnn35248 	savehup = sigset(SIGHUP, SIG_IGN);
15079acbbeafSnn35248 
15089acbbeafSnn35248 	while ((pid = waitpid(child, &status, 0)) != child && pid != -1)
15099acbbeafSnn35248 		;
15109acbbeafSnn35248 
15119acbbeafSnn35248 	(void) sigset(SIGINT, saveint);
15129acbbeafSnn35248 	(void) sigset(SIGTERM, saveterm);
15139acbbeafSnn35248 	(void) sigset(SIGQUIT, savequit);
15149acbbeafSnn35248 	(void) sigset(SIGHUP, savehup);
15159acbbeafSnn35248 
15169acbbeafSnn35248 	return (pid == -1 ? -1 : status);
15179acbbeafSnn35248 }
15189acbbeafSnn35248 
1519ff17c8bfSgjelinek int
subproc_status(const char * cmd,int status,boolean_t verbose_failure)15209acbbeafSnn35248 subproc_status(const char *cmd, int status, boolean_t verbose_failure)
15217c478bd9Sstevel@tonic-gate {
15227c478bd9Sstevel@tonic-gate 	if (WIFEXITED(status)) {
15237c478bd9Sstevel@tonic-gate 		int exit_code = WEXITSTATUS(status);
15247c478bd9Sstevel@tonic-gate 
15259acbbeafSnn35248 		if ((verbose_failure) && (exit_code != ZONE_SUBPROC_OK))
15267c478bd9Sstevel@tonic-gate 			zerror(gettext("'%s' failed with exit code %d."), cmd,
15277c478bd9Sstevel@tonic-gate 			    exit_code);
15289acbbeafSnn35248 
15299acbbeafSnn35248 		return (exit_code);
15307c478bd9Sstevel@tonic-gate 	} else if (WIFSIGNALED(status)) {
15317c478bd9Sstevel@tonic-gate 		int signal = WTERMSIG(status);
15327c478bd9Sstevel@tonic-gate 		char sigstr[SIG2STR_MAX];
15337c478bd9Sstevel@tonic-gate 
15347c478bd9Sstevel@tonic-gate 		if (sig2str(signal, sigstr) == 0) {
15357c478bd9Sstevel@tonic-gate 			zerror(gettext("'%s' terminated by signal SIG%s."), cmd,
15367c478bd9Sstevel@tonic-gate 			    sigstr);
15377c478bd9Sstevel@tonic-gate 		} else {
15387c478bd9Sstevel@tonic-gate 			zerror(gettext("'%s' terminated by an unknown signal."),
15397c478bd9Sstevel@tonic-gate 			    cmd);
15407c478bd9Sstevel@tonic-gate 		}
15417c478bd9Sstevel@tonic-gate 	} else {
15427c478bd9Sstevel@tonic-gate 		zerror(gettext("'%s' failed for unknown reasons."), cmd);
15437c478bd9Sstevel@tonic-gate 	}
15449acbbeafSnn35248 
15459acbbeafSnn35248 	/*
15469acbbeafSnn35248 	 * Assume a subprocess that died due to a signal or an unknown error
15479acbbeafSnn35248 	 * should be considered an exit code of ZONE_SUBPROC_FATAL, as the
15489acbbeafSnn35248 	 * user will likely need to do some manual cleanup.
15499acbbeafSnn35248 	 */
15509acbbeafSnn35248 	return (ZONE_SUBPROC_FATAL);
15517c478bd9Sstevel@tonic-gate }
15527c478bd9Sstevel@tonic-gate 
1553cb8a054bSGlenn Faden static int
auth_check(char * user,char * zone,int cmd_num)1554cb8a054bSGlenn Faden auth_check(char *user, char *zone, int cmd_num)
1555cb8a054bSGlenn Faden {
1556cb8a054bSGlenn Faden 	char authname[MAXAUTHS];
1557cb8a054bSGlenn Faden 
1558cb8a054bSGlenn Faden 	switch (cmd_num) {
1559cb8a054bSGlenn Faden 	case CMD_LIST:
1560cb8a054bSGlenn Faden 	case CMD_HELP:
1561cb8a054bSGlenn Faden 		return (Z_OK);
1562cb8a054bSGlenn Faden 	case SOURCE_ZONE:
1563cb8a054bSGlenn Faden 		(void) strlcpy(authname, ZONE_CLONEFROM_AUTH, MAXAUTHS);
1564cb8a054bSGlenn Faden 		break;
1565cb8a054bSGlenn Faden 	case CMD_BOOT:
1566cb8a054bSGlenn Faden 	case CMD_HALT:
1567cb8a054bSGlenn Faden 	case CMD_READY:
15683c7284bdSAlexander Eremin 	case CMD_SHUTDOWN:
1569cb8a054bSGlenn Faden 	case CMD_REBOOT:
1570cb8a054bSGlenn Faden 	case CMD_SYSBOOT:
1571cb8a054bSGlenn Faden 	case CMD_VERIFY:
1572cb8a054bSGlenn Faden 	case CMD_INSTALL:
1573cb8a054bSGlenn Faden 	case CMD_UNINSTALL:
1574cb8a054bSGlenn Faden 	case CMD_MOUNT:
1575cb8a054bSGlenn Faden 	case CMD_UNMOUNT:
1576cb8a054bSGlenn Faden 	case CMD_CLONE:
1577cb8a054bSGlenn Faden 	case CMD_MOVE:
1578cb8a054bSGlenn Faden 	case CMD_DETACH:
1579cb8a054bSGlenn Faden 	case CMD_ATTACH:
1580cb8a054bSGlenn Faden 	case CMD_MARK:
1581cb8a054bSGlenn Faden 	case CMD_APPLY:
1582cb8a054bSGlenn Faden 	default:
1583cb8a054bSGlenn Faden 		(void) strlcpy(authname, ZONE_MANAGE_AUTH, MAXAUTHS);
1584cb8a054bSGlenn Faden 		break;
1585cb8a054bSGlenn Faden 	}
1586cb8a054bSGlenn Faden 	(void) strlcat(authname, KV_OBJECT, MAXAUTHS);
1587cb8a054bSGlenn Faden 	(void) strlcat(authname, zone, MAXAUTHS);
1588cb8a054bSGlenn Faden 	if (chkauthattr(authname, user) == 0) {
1589cb8a054bSGlenn Faden 		return (Z_ERR);
1590cb8a054bSGlenn Faden 	} else {
1591cb8a054bSGlenn Faden 		/*
1592cb8a054bSGlenn Faden 		 * Some subcommands, e.g. install, run subcommands,
1593cb8a054bSGlenn Faden 		 * e.g. sysidcfg, that require a real uid of root,
1594cb8a054bSGlenn Faden 		 *  so switch to root, here.
1595cb8a054bSGlenn Faden 		 */
1596cb8a054bSGlenn Faden 		if (setuid(0) == -1) {
1597cb8a054bSGlenn Faden 			zperror(gettext("insufficient privilege"), B_TRUE);
1598cb8a054bSGlenn Faden 			return (Z_ERR);
1599cb8a054bSGlenn Faden 		}
1600cb8a054bSGlenn Faden 		return (Z_OK);
1601cb8a054bSGlenn Faden 	}
1602cb8a054bSGlenn Faden }
1603cb8a054bSGlenn Faden 
16047c478bd9Sstevel@tonic-gate /*
16057c478bd9Sstevel@tonic-gate  * Various sanity checks; make sure:
16067c478bd9Sstevel@tonic-gate  * 1. We're in the global zone.
16077c478bd9Sstevel@tonic-gate  * 2. The calling user has sufficient privilege.
16087c478bd9Sstevel@tonic-gate  * 3. The target zone is neither the global zone nor anything starting with
16097c478bd9Sstevel@tonic-gate  *    "SUNW".
16107c478bd9Sstevel@tonic-gate  * 4a. If we're looking for a 'not running' (i.e., configured or installed)
16117c478bd9Sstevel@tonic-gate  *     zone, the name service knows about it.
16127c478bd9Sstevel@tonic-gate  * 4b. For some operations which expect a zone not to be running, that it is
16137c478bd9Sstevel@tonic-gate  *     not already running (or ready).
16147c478bd9Sstevel@tonic-gate  */
16157c478bd9Sstevel@tonic-gate static int
sanity_check(char * zone,int cmd_num,boolean_t running,boolean_t unsafe_when_running,boolean_t force)16167c478bd9Sstevel@tonic-gate sanity_check(char *zone, int cmd_num, boolean_t running,
16179acbbeafSnn35248     boolean_t unsafe_when_running, boolean_t force)
16187c478bd9Sstevel@tonic-gate {
16197c478bd9Sstevel@tonic-gate 	zone_entry_t *zent;
16207c478bd9Sstevel@tonic-gate 	priv_set_t *privset;
16219acbbeafSnn35248 	zone_state_t state, min_state;
1622108322fbScarlsonj 	char kernzone[ZONENAME_MAX];
1623108322fbScarlsonj 	FILE *fp;
16247c478bd9Sstevel@tonic-gate 
16257c478bd9Sstevel@tonic-gate 	if (getzoneid() != GLOBAL_ZONEID) {
1626ffbafc53Scomay 		switch (cmd_num) {
1627ffbafc53Scomay 		case CMD_HALT:
1628ffbafc53Scomay 			zerror(gettext("use %s to %s this zone."), "halt(1M)",
16297c478bd9Sstevel@tonic-gate 			    cmd_to_str(cmd_num));
1630ffbafc53Scomay 			break;
16313c7284bdSAlexander Eremin 		case CMD_SHUTDOWN:
16323c7284bdSAlexander Eremin 			zerror(gettext("use %s to %s this zone."),
16333c7284bdSAlexander Eremin 			    "shutdown(1M)", cmd_to_str(cmd_num));
16343c7284bdSAlexander Eremin 			break;
1635ffbafc53Scomay 		case CMD_REBOOT:
1636ffbafc53Scomay 			zerror(gettext("use %s to %s this zone."),
1637ffbafc53Scomay 			    "reboot(1M)", cmd_to_str(cmd_num));
1638ffbafc53Scomay 			break;
1639ffbafc53Scomay 		default:
1640ffbafc53Scomay 			zerror(gettext("must be in the global zone to %s a "
1641ffbafc53Scomay 			    "zone."), cmd_to_str(cmd_num));
1642ffbafc53Scomay 			break;
1643ffbafc53Scomay 		}
16447c478bd9Sstevel@tonic-gate 		return (Z_ERR);
16457c478bd9Sstevel@tonic-gate 	}
16467c478bd9Sstevel@tonic-gate 
16477c478bd9Sstevel@tonic-gate 	if ((privset = priv_allocset()) == NULL) {
16487c478bd9Sstevel@tonic-gate 		zerror(gettext("%s failed"), "priv_allocset");
16497c478bd9Sstevel@tonic-gate 		return (Z_ERR);
16507c478bd9Sstevel@tonic-gate 	}
16517c478bd9Sstevel@tonic-gate 
16527c478bd9Sstevel@tonic-gate 	if (getppriv(PRIV_EFFECTIVE, privset) != 0) {
16537c478bd9Sstevel@tonic-gate 		zerror(gettext("%s failed"), "getppriv");
16547c478bd9Sstevel@tonic-gate 		priv_freeset(privset);
16557c478bd9Sstevel@tonic-gate 		return (Z_ERR);
16567c478bd9Sstevel@tonic-gate 	}
16577c478bd9Sstevel@tonic-gate 
16587c478bd9Sstevel@tonic-gate 	if (priv_isfullset(privset) == B_FALSE) {
16597c478bd9Sstevel@tonic-gate 		zerror(gettext("only a privileged user may %s a zone."),
16607c478bd9Sstevel@tonic-gate 		    cmd_to_str(cmd_num));
16617c478bd9Sstevel@tonic-gate 		priv_freeset(privset);
16627c478bd9Sstevel@tonic-gate 		return (Z_ERR);
16637c478bd9Sstevel@tonic-gate 	}
16647c478bd9Sstevel@tonic-gate 	priv_freeset(privset);
16657c478bd9Sstevel@tonic-gate 
16667c478bd9Sstevel@tonic-gate 	if (zone == NULL) {
16677c478bd9Sstevel@tonic-gate 		zerror(gettext("no zone specified"));
16687c478bd9Sstevel@tonic-gate 		return (Z_ERR);
16697c478bd9Sstevel@tonic-gate 	}
16707c478bd9Sstevel@tonic-gate 
1671cb8a054bSGlenn Faden 	if (auth_check(username, zone, cmd_num) == Z_ERR) {
1672cb8a054bSGlenn Faden 		zerror(gettext("User %s is not authorized to %s this zone."),
1673cb8a054bSGlenn Faden 		    username, cmd_to_str(cmd_num));
1674cb8a054bSGlenn Faden 		return (Z_ERR);
1675cb8a054bSGlenn Faden 	}
1676cb8a054bSGlenn Faden 
16777c478bd9Sstevel@tonic-gate 	if (strcmp(zone, GLOBAL_ZONENAME) == 0) {
16787c478bd9Sstevel@tonic-gate 		zerror(gettext("%s operation is invalid for the global zone."),
16797c478bd9Sstevel@tonic-gate 		    cmd_to_str(cmd_num));
16807c478bd9Sstevel@tonic-gate 		return (Z_ERR);
16817c478bd9Sstevel@tonic-gate 	}
16827c478bd9Sstevel@tonic-gate 
16837c478bd9Sstevel@tonic-gate 	if (strncmp(zone, "SUNW", 4) == 0) {
16847c478bd9Sstevel@tonic-gate 		zerror(gettext("%s operation is invalid for zones starting "
16857c478bd9Sstevel@tonic-gate 		    "with SUNW."), cmd_to_str(cmd_num));
16867c478bd9Sstevel@tonic-gate 		return (Z_ERR);
16877c478bd9Sstevel@tonic-gate 	}
16887c478bd9Sstevel@tonic-gate 
1689108322fbScarlsonj 	if (!zonecfg_in_alt_root()) {
1690108322fbScarlsonj 		zent = lookup_running_zone(zone);
1691108322fbScarlsonj 	} else if ((fp = zonecfg_open_scratch("", B_FALSE)) == NULL) {
1692108322fbScarlsonj 		zent = NULL;
1693108322fbScarlsonj 	} else {
1694108322fbScarlsonj 		if (zonecfg_find_scratch(fp, zone, zonecfg_get_root(),
1695108322fbScarlsonj 		    kernzone, sizeof (kernzone)) == 0)
1696108322fbScarlsonj 			zent = lookup_running_zone(kernzone);
1697108322fbScarlsonj 		else
1698108322fbScarlsonj 			zent = NULL;
1699108322fbScarlsonj 		zonecfg_close_scratch(fp);
1700108322fbScarlsonj 	}
1701108322fbScarlsonj 
17027c478bd9Sstevel@tonic-gate 	/*
17037c478bd9Sstevel@tonic-gate 	 * Look up from the kernel for 'running' zones.
17047c478bd9Sstevel@tonic-gate 	 */
17059acbbeafSnn35248 	if (running && !force) {
17067c478bd9Sstevel@tonic-gate 		if (zent == NULL) {
17077c478bd9Sstevel@tonic-gate 			zerror(gettext("not running"));
17087c478bd9Sstevel@tonic-gate 			return (Z_ERR);
17097c478bd9Sstevel@tonic-gate 		}
17107c478bd9Sstevel@tonic-gate 	} else {
17117c478bd9Sstevel@tonic-gate 		int err;
17127c478bd9Sstevel@tonic-gate 
17137c478bd9Sstevel@tonic-gate 		if (unsafe_when_running && zent != NULL) {
17147c478bd9Sstevel@tonic-gate 			/* check whether the zone is ready or running */
17157c478bd9Sstevel@tonic-gate 			if ((err = zone_get_state(zent->zname,
17167c478bd9Sstevel@tonic-gate 			    &zent->zstate_num)) != Z_OK) {
17177c478bd9Sstevel@tonic-gate 				errno = err;
17187c478bd9Sstevel@tonic-gate 				zperror2(zent->zname,
17197c478bd9Sstevel@tonic-gate 				    gettext("could not get state"));
17207c478bd9Sstevel@tonic-gate 				/* can't tell, so hedge */
17217c478bd9Sstevel@tonic-gate 				zent->zstate_str = "ready/running";
17227c478bd9Sstevel@tonic-gate 			} else {
17237c478bd9Sstevel@tonic-gate 				zent->zstate_str =
17247c478bd9Sstevel@tonic-gate 				    zone_state_str(zent->zstate_num);
17257c478bd9Sstevel@tonic-gate 			}
17267c478bd9Sstevel@tonic-gate 			zerror(gettext("%s operation is invalid for %s zones."),
17277c478bd9Sstevel@tonic-gate 			    cmd_to_str(cmd_num), zent->zstate_str);
17287c478bd9Sstevel@tonic-gate 			return (Z_ERR);
17297c478bd9Sstevel@tonic-gate 		}
17307c478bd9Sstevel@tonic-gate 		if ((err = zone_get_state(zone, &state)) != Z_OK) {
17317c478bd9Sstevel@tonic-gate 			errno = err;
17327c478bd9Sstevel@tonic-gate 			zperror2(zone, gettext("could not get state"));
17337c478bd9Sstevel@tonic-gate 			return (Z_ERR);
17347c478bd9Sstevel@tonic-gate 		}
17357c478bd9Sstevel@tonic-gate 		switch (cmd_num) {
17367c478bd9Sstevel@tonic-gate 		case CMD_UNINSTALL:
17377c478bd9Sstevel@tonic-gate 			if (state == ZONE_STATE_CONFIGURED) {
17387c478bd9Sstevel@tonic-gate 				zerror(gettext("is already in state '%s'."),
17397c478bd9Sstevel@tonic-gate 				    zone_state_str(ZONE_STATE_CONFIGURED));
17407c478bd9Sstevel@tonic-gate 				return (Z_ERR);
17417c478bd9Sstevel@tonic-gate 			}
17427c478bd9Sstevel@tonic-gate 			break;
1743ee519a1fSgjelinek 		case CMD_ATTACH:
1744edfa49ffS 			if (state == ZONE_STATE_INSTALLED) {
1745edfa49ffS 				zerror(gettext("is already %s."),
1746edfa49ffS 				    zone_state_str(ZONE_STATE_INSTALLED));
1747edfa49ffS 				return (Z_ERR);
1748edfa49ffS 			} else if (state == ZONE_STATE_INCOMPLETE && !force) {
1749edfa49ffS 				zerror(gettext("zone is %s; %s required."),
1750edfa49ffS 				    zone_state_str(ZONE_STATE_INCOMPLETE),
1751edfa49ffS 				    cmd_to_str(CMD_UNINSTALL));
1752edfa49ffS 				return (Z_ERR);
1753edfa49ffS 			}
1754edfa49ffS 			break;
1755865e09a4Sgjelinek 		case CMD_CLONE:
17567c478bd9Sstevel@tonic-gate 		case CMD_INSTALL:
17577c478bd9Sstevel@tonic-gate 			if (state == ZONE_STATE_INSTALLED) {
17587c478bd9Sstevel@tonic-gate 				zerror(gettext("is already %s."),
17597c478bd9Sstevel@tonic-gate 				    zone_state_str(ZONE_STATE_INSTALLED));
17607c478bd9Sstevel@tonic-gate 				return (Z_ERR);
17617c478bd9Sstevel@tonic-gate 			} else if (state == ZONE_STATE_INCOMPLETE) {
17627c478bd9Sstevel@tonic-gate 				zerror(gettext("zone is %s; %s required."),
17637c478bd9Sstevel@tonic-gate 				    zone_state_str(ZONE_STATE_INCOMPLETE),
17647c478bd9Sstevel@tonic-gate 				    cmd_to_str(CMD_UNINSTALL));
17657c478bd9Sstevel@tonic-gate 				return (Z_ERR);
17667c478bd9Sstevel@tonic-gate 			}
17677c478bd9Sstevel@tonic-gate 			break;
1768ee519a1fSgjelinek 		case CMD_DETACH:
1769865e09a4Sgjelinek 		case CMD_MOVE:
17707c478bd9Sstevel@tonic-gate 		case CMD_READY:
17717c478bd9Sstevel@tonic-gate 		case CMD_BOOT:
1772108322fbScarlsonj 		case CMD_MOUNT:
1773555afedfScarlsonj 		case CMD_MARK:
17749acbbeafSnn35248 			if ((cmd_num == CMD_BOOT || cmd_num == CMD_MOUNT) &&
17759acbbeafSnn35248 			    force)
17769acbbeafSnn35248 				min_state = ZONE_STATE_INCOMPLETE;
1777edfa49ffS 			else if (cmd_num == CMD_MARK)
1778edfa49ffS 				min_state = ZONE_STATE_CONFIGURED;
17799acbbeafSnn35248 			else
17809acbbeafSnn35248 				min_state = ZONE_STATE_INSTALLED;
17819acbbeafSnn35248 
17829acbbeafSnn35248 			if (state < min_state) {
17837c478bd9Sstevel@tonic-gate 				zerror(gettext("must be %s before %s."),
17849acbbeafSnn35248 				    zone_state_str(min_state),
17857c478bd9Sstevel@tonic-gate 				    cmd_to_str(cmd_num));
17867c478bd9Sstevel@tonic-gate 				return (Z_ERR);
17877c478bd9Sstevel@tonic-gate 			}
17887c478bd9Sstevel@tonic-gate 			break;
17897c478bd9Sstevel@tonic-gate 		case CMD_VERIFY:
17907c478bd9Sstevel@tonic-gate 			if (state == ZONE_STATE_INCOMPLETE) {
17917c478bd9Sstevel@tonic-gate 				zerror(gettext("zone is %s; %s required."),
17927c478bd9Sstevel@tonic-gate 				    zone_state_str(ZONE_STATE_INCOMPLETE),
17937c478bd9Sstevel@tonic-gate 				    cmd_to_str(CMD_UNINSTALL));
17947c478bd9Sstevel@tonic-gate 				return (Z_ERR);
17957c478bd9Sstevel@tonic-gate 			}
17967c478bd9Sstevel@tonic-gate 			break;
1797108322fbScarlsonj 		case CMD_UNMOUNT:
1798108322fbScarlsonj 			if (state != ZONE_STATE_MOUNTED) {
1799108322fbScarlsonj 				zerror(gettext("must be %s before %s."),
1800108322fbScarlsonj 				    zone_state_str(ZONE_STATE_MOUNTED),
1801108322fbScarlsonj 				    cmd_to_str(cmd_num));
1802108322fbScarlsonj 				return (Z_ERR);
1803108322fbScarlsonj 			}
1804108322fbScarlsonj 			break;
1805fbbfbc6eSjv227347 		case CMD_SYSBOOT:
1806fbbfbc6eSjv227347 			if (state != ZONE_STATE_INSTALLED) {
1807fbbfbc6eSjv227347 				zerror(gettext("%s operation is invalid for %s "
1808fbbfbc6eSjv227347 				    "zones."), cmd_to_str(cmd_num),
1809fbbfbc6eSjv227347 				    zone_state_str(state));
1810fbbfbc6eSjv227347 				return (Z_ERR);
1811fbbfbc6eSjv227347 			}
1812fbbfbc6eSjv227347 			break;
18137c478bd9Sstevel@tonic-gate 		}
18147c478bd9Sstevel@tonic-gate 	}
18157c478bd9Sstevel@tonic-gate 	return (Z_OK);
18167c478bd9Sstevel@tonic-gate }
18177c478bd9Sstevel@tonic-gate 
18187c478bd9Sstevel@tonic-gate static int
halt_func(int argc,char * argv[])18197c478bd9Sstevel@tonic-gate halt_func(int argc, char *argv[])
18207c478bd9Sstevel@tonic-gate {
18217c478bd9Sstevel@tonic-gate 	zone_cmd_arg_t zarg;
18227c478bd9Sstevel@tonic-gate 	int arg;
18237c478bd9Sstevel@tonic-gate 
1824108322fbScarlsonj 	if (zonecfg_in_alt_root()) {
1825108322fbScarlsonj 		zerror(gettext("cannot halt zone in alternate root"));
1826108322fbScarlsonj 		return (Z_ERR);
1827108322fbScarlsonj 	}
1828108322fbScarlsonj 
18297c478bd9Sstevel@tonic-gate 	optind = 0;
18307c478bd9Sstevel@tonic-gate 	if ((arg = getopt(argc, argv, "?")) != EOF) {
18317c478bd9Sstevel@tonic-gate 		switch (arg) {
18327c478bd9Sstevel@tonic-gate 		case '?':
18337c478bd9Sstevel@tonic-gate 			sub_usage(SHELP_HALT, CMD_HALT);
18347c478bd9Sstevel@tonic-gate 			return (optopt == '?' ? Z_OK : Z_USAGE);
18357c478bd9Sstevel@tonic-gate 		default:
18367c478bd9Sstevel@tonic-gate 			sub_usage(SHELP_HALT, CMD_HALT);
18377c478bd9Sstevel@tonic-gate 			return (Z_USAGE);
18387c478bd9Sstevel@tonic-gate 		}
18397c478bd9Sstevel@tonic-gate 	}
18407c478bd9Sstevel@tonic-gate 	if (argc > optind) {
18417c478bd9Sstevel@tonic-gate 		sub_usage(SHELP_HALT, CMD_HALT);
18427c478bd9Sstevel@tonic-gate 		return (Z_USAGE);
18437c478bd9Sstevel@tonic-gate 	}
18447c478bd9Sstevel@tonic-gate 	/*
18457c478bd9Sstevel@tonic-gate 	 * zoneadmd should be the one to decide whether or not to proceed,
18467c478bd9Sstevel@tonic-gate 	 * so even though it seems that the fourth parameter below should
18477c478bd9Sstevel@tonic-gate 	 * perhaps be B_TRUE, it really shouldn't be.
18487c478bd9Sstevel@tonic-gate 	 */
18499acbbeafSnn35248 	if (sanity_check(target_zone, CMD_HALT, B_FALSE, B_FALSE, B_FALSE)
18509acbbeafSnn35248 	    != Z_OK)
18517c478bd9Sstevel@tonic-gate 		return (Z_ERR);
18527c478bd9Sstevel@tonic-gate 
1853ce28b40eSzt129084 	/*
1854ce28b40eSzt129084 	 * Invoke brand-specific handler.
1855ce28b40eSzt129084 	 */
1856ce28b40eSzt129084 	if (invoke_brand_handler(CMD_HALT, argv) != Z_OK)
1857ce28b40eSzt129084 		return (Z_ERR);
1858ce28b40eSzt129084 
18597c478bd9Sstevel@tonic-gate 	zarg.cmd = Z_HALT;
1860ff17c8bfSgjelinek 	return ((zonecfg_call_zoneadmd(target_zone, &zarg, locale,
1861ff17c8bfSgjelinek 	    B_TRUE) == 0) ?  Z_OK : Z_ERR);
18627c478bd9Sstevel@tonic-gate }
18637c478bd9Sstevel@tonic-gate 
18647c478bd9Sstevel@tonic-gate static int
shutdown_func(int argc,char * argv[])18653c7284bdSAlexander Eremin shutdown_func(int argc, char *argv[])
18663c7284bdSAlexander Eremin {
18673c7284bdSAlexander Eremin 	zone_cmd_arg_t zarg;
18683c7284bdSAlexander Eremin 	int arg;
18693c7284bdSAlexander Eremin 	boolean_t reboot = B_FALSE;
18703c7284bdSAlexander Eremin 
18713c7284bdSAlexander Eremin 	zarg.cmd = Z_SHUTDOWN;
18723c7284bdSAlexander Eremin 
18733c7284bdSAlexander Eremin 	if (zonecfg_in_alt_root()) {
18743c7284bdSAlexander Eremin 		zerror(gettext("cannot shut down zone in alternate root"));
18753c7284bdSAlexander Eremin 		return (Z_ERR);
18763c7284bdSAlexander Eremin 	}
18773c7284bdSAlexander Eremin 
18783c7284bdSAlexander Eremin 	optind = 0;
18793c7284bdSAlexander Eremin 	while ((arg = getopt(argc, argv, "?r")) != EOF) {
18803c7284bdSAlexander Eremin 		switch (arg) {
18813c7284bdSAlexander Eremin 		case '?':
18823c7284bdSAlexander Eremin 			sub_usage(SHELP_SHUTDOWN, CMD_SHUTDOWN);
18833c7284bdSAlexander Eremin 			return (optopt == '?' ? Z_OK : Z_USAGE);
18843c7284bdSAlexander Eremin 		case 'r':
18853c7284bdSAlexander Eremin 			reboot = B_TRUE;
18863c7284bdSAlexander Eremin 			break;
18873c7284bdSAlexander Eremin 		default:
18883c7284bdSAlexander Eremin 			sub_usage(SHELP_SHUTDOWN, CMD_SHUTDOWN);
18893c7284bdSAlexander Eremin 			return (Z_USAGE);
18903c7284bdSAlexander Eremin 		}
18913c7284bdSAlexander Eremin 	}
18923c7284bdSAlexander Eremin 
18933c7284bdSAlexander Eremin 	zarg.bootbuf[0] = '\0';
18943c7284bdSAlexander Eremin 	for (; optind < argc; optind++) {
18953c7284bdSAlexander Eremin 		if (strlcat(zarg.bootbuf, argv[optind],
18963c7284bdSAlexander Eremin 		    sizeof (zarg.bootbuf)) >= sizeof (zarg.bootbuf)) {
18973c7284bdSAlexander Eremin 			zerror(gettext("Boot argument list too long"));
18983c7284bdSAlexander Eremin 			return (Z_ERR);
18993c7284bdSAlexander Eremin 		}
19003c7284bdSAlexander Eremin 		if (optind < argc - 1)
19013c7284bdSAlexander Eremin 			if (strlcat(zarg.bootbuf, " ", sizeof (zarg.bootbuf)) >=
19023c7284bdSAlexander Eremin 			    sizeof (zarg.bootbuf)) {
19033c7284bdSAlexander Eremin 				zerror(gettext("Boot argument list too long"));
19043c7284bdSAlexander Eremin 				return (Z_ERR);
19053c7284bdSAlexander Eremin 			}
19063c7284bdSAlexander Eremin 	}
19073c7284bdSAlexander Eremin 
19083c7284bdSAlexander Eremin 	/*
19093c7284bdSAlexander Eremin 	 * zoneadmd should be the one to decide whether or not to proceed,
19103c7284bdSAlexander Eremin 	 * so even though it seems that the third parameter below should
19113c7284bdSAlexander Eremin 	 * perhaps be B_TRUE, it really shouldn't be.
19123c7284bdSAlexander Eremin 	 */
19133c7284bdSAlexander Eremin 	if (sanity_check(target_zone, CMD_SHUTDOWN, B_TRUE, B_FALSE, B_FALSE)
19143c7284bdSAlexander Eremin 	    != Z_OK)
19153c7284bdSAlexander Eremin 		return (Z_ERR);
19163c7284bdSAlexander Eremin 
19173c7284bdSAlexander Eremin 	if (zonecfg_call_zoneadmd(target_zone, &zarg, locale, B_TRUE) != Z_OK)
19183c7284bdSAlexander Eremin 		return (Z_ERR);
19193c7284bdSAlexander Eremin 
19203c7284bdSAlexander Eremin 	if (reboot) {
19213c7284bdSAlexander Eremin 		if (sanity_check(target_zone, CMD_BOOT, B_FALSE, B_FALSE,
19223c7284bdSAlexander Eremin 		    B_FALSE) != Z_OK)
19233c7284bdSAlexander Eremin 			return (Z_ERR);
19243c7284bdSAlexander Eremin 
19253c7284bdSAlexander Eremin 		zarg.cmd = Z_BOOT;
19263c7284bdSAlexander Eremin 		if (zonecfg_call_zoneadmd(target_zone, &zarg, locale,
19273c7284bdSAlexander Eremin 		    B_TRUE) != Z_OK)
19283c7284bdSAlexander Eremin 			return (Z_ERR);
19293c7284bdSAlexander Eremin 	}
19303c7284bdSAlexander Eremin 	return (Z_OK);
19313c7284bdSAlexander Eremin }
19323c7284bdSAlexander Eremin 
19333c7284bdSAlexander Eremin static int
reboot_func(int argc,char * argv[])19347c478bd9Sstevel@tonic-gate reboot_func(int argc, char *argv[])
19357c478bd9Sstevel@tonic-gate {
19367c478bd9Sstevel@tonic-gate 	zone_cmd_arg_t zarg;
19377c478bd9Sstevel@tonic-gate 	int arg;
19387c478bd9Sstevel@tonic-gate 
1939108322fbScarlsonj 	if (zonecfg_in_alt_root()) {
1940108322fbScarlsonj 		zerror(gettext("cannot reboot zone in alternate root"));
1941108322fbScarlsonj 		return (Z_ERR);
1942108322fbScarlsonj 	}
1943108322fbScarlsonj 
19447c478bd9Sstevel@tonic-gate 	optind = 0;
19457c478bd9Sstevel@tonic-gate 	if ((arg = getopt(argc, argv, "?")) != EOF) {
19467c478bd9Sstevel@tonic-gate 		switch (arg) {
19477c478bd9Sstevel@tonic-gate 		case '?':
19487c478bd9Sstevel@tonic-gate 			sub_usage(SHELP_REBOOT, CMD_REBOOT);
19497c478bd9Sstevel@tonic-gate 			return (optopt == '?' ? Z_OK : Z_USAGE);
19507c478bd9Sstevel@tonic-gate 		default:
19517c478bd9Sstevel@tonic-gate 			sub_usage(SHELP_REBOOT, CMD_REBOOT);
19527c478bd9Sstevel@tonic-gate 			return (Z_USAGE);
19537c478bd9Sstevel@tonic-gate 		}
19547c478bd9Sstevel@tonic-gate 	}
19553f2f09c1Sdp 
19563f2f09c1Sdp 	zarg.bootbuf[0] = '\0';
19573f2f09c1Sdp 	for (; optind < argc; optind++) {
19583f2f09c1Sdp 		if (strlcat(zarg.bootbuf, argv[optind],
19593f2f09c1Sdp 		    sizeof (zarg.bootbuf)) >= sizeof (zarg.bootbuf)) {
19603f2f09c1Sdp 			zerror(gettext("Boot argument list too long"));
19613f2f09c1Sdp 			return (Z_ERR);
19627c478bd9Sstevel@tonic-gate 		}
19633f2f09c1Sdp 		if (optind < argc - 1)
19643f2f09c1Sdp 			if (strlcat(zarg.bootbuf, " ", sizeof (zarg.bootbuf)) >=
19653f2f09c1Sdp 			    sizeof (zarg.bootbuf)) {
19663f2f09c1Sdp 				zerror(gettext("Boot argument list too long"));
19673f2f09c1Sdp 				return (Z_ERR);
19683f2f09c1Sdp 			}
19693f2f09c1Sdp 	}
19703f2f09c1Sdp 
19713f2f09c1Sdp 
19727c478bd9Sstevel@tonic-gate 	/*
19737c478bd9Sstevel@tonic-gate 	 * zoneadmd should be the one to decide whether or not to proceed,
19747c478bd9Sstevel@tonic-gate 	 * so even though it seems that the fourth parameter below should
19757c478bd9Sstevel@tonic-gate 	 * perhaps be B_TRUE, it really shouldn't be.
19767c478bd9Sstevel@tonic-gate 	 */
19779acbbeafSnn35248 	if (sanity_check(target_zone, CMD_REBOOT, B_TRUE, B_FALSE, B_FALSE)
19789acbbeafSnn35248 	    != Z_OK)
19797c478bd9Sstevel@tonic-gate 		return (Z_ERR);
1980ce28b40eSzt129084 	if (verify_details(CMD_REBOOT, argv) != Z_OK)
1981b5abaf04Sgjelinek 		return (Z_ERR);
19827c478bd9Sstevel@tonic-gate 
19837c478bd9Sstevel@tonic-gate 	zarg.cmd = Z_REBOOT;
1984ff17c8bfSgjelinek 	return ((zonecfg_call_zoneadmd(target_zone, &zarg, locale, B_TRUE) == 0)
1985ff17c8bfSgjelinek 	    ? Z_OK : Z_ERR);
1986ff17c8bfSgjelinek }
1987ff17c8bfSgjelinek 
1988ff17c8bfSgjelinek static int
get_hook(brand_handle_t bh,char * cmd,size_t len,int (* bp)(brand_handle_t,const char *,const char *,char *,size_t),char * zonename,char * zonepath)1989ff17c8bfSgjelinek get_hook(brand_handle_t bh, char *cmd, size_t len, int (*bp)(brand_handle_t,
1990ff17c8bfSgjelinek     const char *, const char *, char *, size_t), char *zonename, char *zonepath)
1991ff17c8bfSgjelinek {
1992ff17c8bfSgjelinek 	if (strlcpy(cmd, EXEC_PREFIX, len) >= len)
1993ff17c8bfSgjelinek 		return (Z_ERR);
1994ff17c8bfSgjelinek 
1995ff17c8bfSgjelinek 	if (bp(bh, zonename, zonepath, cmd + EXEC_LEN, len - EXEC_LEN) != 0)
1996ff17c8bfSgjelinek 		return (Z_ERR);
1997ff17c8bfSgjelinek 
1998ff17c8bfSgjelinek 	if (strlen(cmd) <= EXEC_LEN)
1999ff17c8bfSgjelinek 		cmd[0] = '\0';
2000ff17c8bfSgjelinek 
2001ff17c8bfSgjelinek 	return (Z_OK);
20027c478bd9Sstevel@tonic-gate }
20037c478bd9Sstevel@tonic-gate 
20047c478bd9Sstevel@tonic-gate static int
verify_brand(zone_dochandle_t handle,int cmd_num,char * argv[])2005ce28b40eSzt129084 verify_brand(zone_dochandle_t handle, int cmd_num, char *argv[])
20069acbbeafSnn35248 {
20079acbbeafSnn35248 	char cmdbuf[MAXPATHLEN];
20089acbbeafSnn35248 	int err;
20099acbbeafSnn35248 	char zonepath[MAXPATHLEN];
2010123807fbSedp 	brand_handle_t bh = NULL;
2011ce28b40eSzt129084 	int status, i;
20129acbbeafSnn35248 
20139acbbeafSnn35248 	/*
20149acbbeafSnn35248 	 * Fetch the verify command from the brand configuration.
20159acbbeafSnn35248 	 * "exec" the command so that the returned status is that of
20169acbbeafSnn35248 	 * the command and not the shell.
20179acbbeafSnn35248 	 */
2018ff17c8bfSgjelinek 	if (handle == NULL) {
2019ff17c8bfSgjelinek 		(void) strlcpy(zonepath, "-", sizeof (zonepath));
2020ff17c8bfSgjelinek 	} else if ((err = zonecfg_get_zonepath(handle, zonepath,
2021ff17c8bfSgjelinek 	    sizeof (zonepath))) != Z_OK) {
20229acbbeafSnn35248 		errno = err;
2023ce28b40eSzt129084 		zperror(cmd_to_str(cmd_num), B_TRUE);
20249acbbeafSnn35248 		return (Z_ERR);
20259acbbeafSnn35248 	}
2026123807fbSedp 	if ((bh = brand_open(target_brand)) == NULL) {
20279acbbeafSnn35248 		zerror(gettext("missing or invalid brand"));
20289acbbeafSnn35248 		return (Z_ERR);
20299acbbeafSnn35248 	}
20309acbbeafSnn35248 
20319acbbeafSnn35248 	/*
20329acbbeafSnn35248 	 * If the brand has its own verification routine, execute it now.
2033ce28b40eSzt129084 	 * The verification routine validates the intended zoneadm
2034ce28b40eSzt129084 	 * operation for the specific brand. The zoneadm subcommand and
2035ce28b40eSzt129084 	 * all its arguments are passed to the routine.
20369acbbeafSnn35248 	 */
2037ff17c8bfSgjelinek 	err = get_hook(bh, cmdbuf, sizeof (cmdbuf), brand_get_verify_adm,
2038ff17c8bfSgjelinek 	    target_zone, zonepath);
2039123807fbSedp 	brand_close(bh);
2040ff17c8bfSgjelinek 	if (err != Z_OK)
2041ce28b40eSzt129084 		return (Z_BRAND_ERROR);
2042ff17c8bfSgjelinek 	if (cmdbuf[0] == '\0')
2043ce28b40eSzt129084 		return (Z_OK);
2044ce28b40eSzt129084 
2045ce28b40eSzt129084 	if (strlcat(cmdbuf, cmd_to_str(cmd_num),
2046ce28b40eSzt129084 	    sizeof (cmdbuf)) >= sizeof (cmdbuf))
2047ce28b40eSzt129084 		return (Z_ERR);
2048ce28b40eSzt129084 
2049ce28b40eSzt129084 	/* Build the argv string */
2050ce28b40eSzt129084 	i = 0;
2051ce28b40eSzt129084 	while (argv[i] != NULL) {
2052ce28b40eSzt129084 		if ((strlcat(cmdbuf, " ",
2053ce28b40eSzt129084 		    sizeof (cmdbuf)) >= sizeof (cmdbuf)) ||
2054ce28b40eSzt129084 		    (strlcat(cmdbuf, argv[i++],
2055ce28b40eSzt129084 		    sizeof (cmdbuf)) >= sizeof (cmdbuf)))
2056ce28b40eSzt129084 			return (Z_ERR);
2057ce28b40eSzt129084 	}
2058ce28b40eSzt129084 
2059d9e728a2Sgjelinek 	status = do_subproc(cmdbuf);
20609acbbeafSnn35248 	err = subproc_status(gettext("brand-specific verification"),
20619acbbeafSnn35248 	    status, B_FALSE);
20629acbbeafSnn35248 
2063ce28b40eSzt129084 	return ((err == ZONE_SUBPROC_OK) ? Z_OK : Z_BRAND_ERROR);
20649acbbeafSnn35248 }
20659acbbeafSnn35248 
20669acbbeafSnn35248 static int
verify_rctls(zone_dochandle_t handle)20677c478bd9Sstevel@tonic-gate verify_rctls(zone_dochandle_t handle)
20687c478bd9Sstevel@tonic-gate {
20697c478bd9Sstevel@tonic-gate 	struct zone_rctltab rctltab;
20707c478bd9Sstevel@tonic-gate 	size_t rbs = rctlblk_size();
20717c478bd9Sstevel@tonic-gate 	rctlblk_t *rctlblk;
20727c478bd9Sstevel@tonic-gate 	int error = Z_INVAL;
20737c478bd9Sstevel@tonic-gate 
20747c478bd9Sstevel@tonic-gate 	if ((rctlblk = malloc(rbs)) == NULL) {
20757c478bd9Sstevel@tonic-gate 		zerror(gettext("failed to allocate %lu bytes: %s"), rbs,
20767c478bd9Sstevel@tonic-gate 		    strerror(errno));
20777c478bd9Sstevel@tonic-gate 		return (Z_NOMEM);
20787c478bd9Sstevel@tonic-gate 	}
20797c478bd9Sstevel@tonic-gate 
20807c478bd9Sstevel@tonic-gate 	if (zonecfg_setrctlent(handle) != Z_OK) {
20817c478bd9Sstevel@tonic-gate 		zerror(gettext("zonecfg_setrctlent failed"));
20827c478bd9Sstevel@tonic-gate 		free(rctlblk);
20837c478bd9Sstevel@tonic-gate 		return (error);
20847c478bd9Sstevel@tonic-gate 	}
20857c478bd9Sstevel@tonic-gate 
20867c478bd9Sstevel@tonic-gate 	rctltab.zone_rctl_valptr = NULL;
20877c478bd9Sstevel@tonic-gate 	while (zonecfg_getrctlent(handle, &rctltab) == Z_OK) {
20887c478bd9Sstevel@tonic-gate 		struct zone_rctlvaltab *rctlval;
20897c478bd9Sstevel@tonic-gate 		const char *name = rctltab.zone_rctl_name;
20907c478bd9Sstevel@tonic-gate 
20917c478bd9Sstevel@tonic-gate 		if (!zonecfg_is_rctl(name)) {
20927c478bd9Sstevel@tonic-gate 			zerror(gettext("WARNING: Ignoring unrecognized rctl "
20937c478bd9Sstevel@tonic-gate 			    "'%s'."),  name);
20947c478bd9Sstevel@tonic-gate 			zonecfg_free_rctl_value_list(rctltab.zone_rctl_valptr);
20957c478bd9Sstevel@tonic-gate 			rctltab.zone_rctl_valptr = NULL;
20967c478bd9Sstevel@tonic-gate 			continue;
20977c478bd9Sstevel@tonic-gate 		}
20987c478bd9Sstevel@tonic-gate 
20997c478bd9Sstevel@tonic-gate 		for (rctlval = rctltab.zone_rctl_valptr; rctlval != NULL;
21007c478bd9Sstevel@tonic-gate 		    rctlval = rctlval->zone_rctlval_next) {
21017c478bd9Sstevel@tonic-gate 			if (zonecfg_construct_rctlblk(rctlval, rctlblk)
21027c478bd9Sstevel@tonic-gate 			    != Z_OK) {
21037c478bd9Sstevel@tonic-gate 				zerror(gettext("invalid rctl value: "
21047c478bd9Sstevel@tonic-gate 				    "(priv=%s,limit=%s,action%s)"),
21057c478bd9Sstevel@tonic-gate 				    rctlval->zone_rctlval_priv,
21067c478bd9Sstevel@tonic-gate 				    rctlval->zone_rctlval_limit,
21077c478bd9Sstevel@tonic-gate 				    rctlval->zone_rctlval_action);
21087c478bd9Sstevel@tonic-gate 				goto out;
21097c478bd9Sstevel@tonic-gate 			}
21107c478bd9Sstevel@tonic-gate 			if (!zonecfg_valid_rctl(name, rctlblk)) {
21117c478bd9Sstevel@tonic-gate 				zerror(gettext("(priv=%s,limit=%s,action=%s) "
21127c478bd9Sstevel@tonic-gate 				    "is not a valid value for rctl '%s'"),
21137c478bd9Sstevel@tonic-gate 				    rctlval->zone_rctlval_priv,
21147c478bd9Sstevel@tonic-gate 				    rctlval->zone_rctlval_limit,
21157c478bd9Sstevel@tonic-gate 				    rctlval->zone_rctlval_action,
21167c478bd9Sstevel@tonic-gate 				    name);
21177c478bd9Sstevel@tonic-gate 				goto out;
21187c478bd9Sstevel@tonic-gate 			}
21197c478bd9Sstevel@tonic-gate 		}
21207c478bd9Sstevel@tonic-gate 		zonecfg_free_rctl_value_list(rctltab.zone_rctl_valptr);
21217c478bd9Sstevel@tonic-gate 	}
21227c478bd9Sstevel@tonic-gate 	rctltab.zone_rctl_valptr = NULL;
21237c478bd9Sstevel@tonic-gate 	error = Z_OK;
21247c478bd9Sstevel@tonic-gate out:
21257c478bd9Sstevel@tonic-gate 	zonecfg_free_rctl_value_list(rctltab.zone_rctl_valptr);
21267c478bd9Sstevel@tonic-gate 	(void) zonecfg_endrctlent(handle);
21277c478bd9Sstevel@tonic-gate 	free(rctlblk);
21287c478bd9Sstevel@tonic-gate 	return (error);
21297c478bd9Sstevel@tonic-gate }
21307c478bd9Sstevel@tonic-gate 
21317c478bd9Sstevel@tonic-gate static int
verify_pool(zone_dochandle_t handle)21327c478bd9Sstevel@tonic-gate verify_pool(zone_dochandle_t handle)
21337c478bd9Sstevel@tonic-gate {
21347c478bd9Sstevel@tonic-gate 	char poolname[MAXPATHLEN];
21357c478bd9Sstevel@tonic-gate 	pool_conf_t *poolconf;
21367c478bd9Sstevel@tonic-gate 	pool_t *pool;
21377c478bd9Sstevel@tonic-gate 	int status;
21387c478bd9Sstevel@tonic-gate 	int error;
21397c478bd9Sstevel@tonic-gate 
21407c478bd9Sstevel@tonic-gate 	/*
21417c478bd9Sstevel@tonic-gate 	 * This ends up being very similar to the check done in zoneadmd.
21427c478bd9Sstevel@tonic-gate 	 */
21437c478bd9Sstevel@tonic-gate 	error = zonecfg_get_pool(handle, poolname, sizeof (poolname));
21447c478bd9Sstevel@tonic-gate 	if (error == Z_NO_ENTRY || (error == Z_OK && strlen(poolname) == 0)) {
21457c478bd9Sstevel@tonic-gate 		/*
21467c478bd9Sstevel@tonic-gate 		 * No pool specified.
21477c478bd9Sstevel@tonic-gate 		 */
21487c478bd9Sstevel@tonic-gate 		return (0);
21497c478bd9Sstevel@tonic-gate 	}
21507c478bd9Sstevel@tonic-gate 	if (error != Z_OK) {
21517c478bd9Sstevel@tonic-gate 		zperror(gettext("Unable to retrieve pool name from "
21527c478bd9Sstevel@tonic-gate 		    "configuration"), B_TRUE);
21537c478bd9Sstevel@tonic-gate 		return (error);
21547c478bd9Sstevel@tonic-gate 	}
21557c478bd9Sstevel@tonic-gate 	/*
21567c478bd9Sstevel@tonic-gate 	 * Don't do anything if pools aren't enabled.
21577c478bd9Sstevel@tonic-gate 	 */
21587c478bd9Sstevel@tonic-gate 	if (pool_get_status(&status) != PO_SUCCESS || status != POOL_ENABLED) {
21597c478bd9Sstevel@tonic-gate 		zerror(gettext("WARNING: pools facility not active; "
21607c478bd9Sstevel@tonic-gate 		    "zone will not be bound to pool '%s'."), poolname);
21617c478bd9Sstevel@tonic-gate 		return (Z_OK);
21627c478bd9Sstevel@tonic-gate 	}
21637c478bd9Sstevel@tonic-gate 	/*
21647c478bd9Sstevel@tonic-gate 	 * Try to provide a sane error message if the requested pool doesn't
21657c478bd9Sstevel@tonic-gate 	 * exist.  It isn't clear that pools-related failures should
21667c478bd9Sstevel@tonic-gate 	 * necessarily translate to a failure to verify the zone configuration,
21677c478bd9Sstevel@tonic-gate 	 * hence they are not considered errors.
21687c478bd9Sstevel@tonic-gate 	 */
21697c478bd9Sstevel@tonic-gate 	if ((poolconf = pool_conf_alloc()) == NULL) {
21707c478bd9Sstevel@tonic-gate 		zerror(gettext("WARNING: pool_conf_alloc failed; "
21717c478bd9Sstevel@tonic-gate 		    "using default pool"));
21727c478bd9Sstevel@tonic-gate 		return (Z_OK);
21737c478bd9Sstevel@tonic-gate 	}
21747c478bd9Sstevel@tonic-gate 	if (pool_conf_open(poolconf, pool_dynamic_location(), PO_RDONLY) !=
21757c478bd9Sstevel@tonic-gate 	    PO_SUCCESS) {
21767c478bd9Sstevel@tonic-gate 		zerror(gettext("WARNING: pool_conf_open failed; "
21777c478bd9Sstevel@tonic-gate 		    "using default pool"));
21787c478bd9Sstevel@tonic-gate 		pool_conf_free(poolconf);
21797c478bd9Sstevel@tonic-gate 		return (Z_OK);
21807c478bd9Sstevel@tonic-gate 	}
21817c478bd9Sstevel@tonic-gate 	pool = pool_get_pool(poolconf, poolname);
21827c478bd9Sstevel@tonic-gate 	(void) pool_conf_close(poolconf);
21837c478bd9Sstevel@tonic-gate 	pool_conf_free(poolconf);
21847c478bd9Sstevel@tonic-gate 	if (pool == NULL) {
21857c478bd9Sstevel@tonic-gate 		zerror(gettext("WARNING: pool '%s' not found. "
21867c478bd9Sstevel@tonic-gate 		    "using default pool"), poolname);
21877c478bd9Sstevel@tonic-gate 	}
21887c478bd9Sstevel@tonic-gate 
21897c478bd9Sstevel@tonic-gate 	return (Z_OK);
21907c478bd9Sstevel@tonic-gate }
21917c478bd9Sstevel@tonic-gate 
219220c8013fSlling /*
219320c8013fSlling  * Verify that the special device/file system exists and is valid.
219420c8013fSlling  */
219520c8013fSlling static int
verify_fs_special(struct zone_fstab * fstab)219620c8013fSlling verify_fs_special(struct zone_fstab *fstab)
219720c8013fSlling {
219893239addSjohnlev 	struct stat64 st;
219920c8013fSlling 
22006cc813faSgjelinek 	/*
22016cc813faSgjelinek 	 * This validation is really intended for standard zone administration.
22026cc813faSgjelinek 	 * If we are in a mini-root or some other upgrade situation where
22036cc813faSgjelinek 	 * we are using the scratch zone, just by-pass this.
22046cc813faSgjelinek 	 */
22056cc813faSgjelinek 	if (zonecfg_in_alt_root())
22066cc813faSgjelinek 		return (Z_OK);
22076cc813faSgjelinek 
220820c8013fSlling 	if (strcmp(fstab->zone_fs_type, MNTTYPE_ZFS) == 0)
220920c8013fSlling 		return (verify_fs_zfs(fstab));
221020c8013fSlling 
221193239addSjohnlev 	if (stat64(fstab->zone_fs_special, &st) != 0) {
22125c358068Slling 		(void) fprintf(stderr, gettext("could not verify fs "
221320c8013fSlling 		    "%s: could not access %s: %s\n"), fstab->zone_fs_dir,
221420c8013fSlling 		    fstab->zone_fs_special, strerror(errno));
221520c8013fSlling 		return (Z_ERR);
221620c8013fSlling 	}
221720c8013fSlling 
221820c8013fSlling 	if (strcmp(st.st_fstype, MNTTYPE_NFS) == 0) {
221920c8013fSlling 		/*
222020c8013fSlling 		 * TRANSLATION_NOTE
222120c8013fSlling 		 * fs and NFS are literals that should
222220c8013fSlling 		 * not be translated.
222320c8013fSlling 		 */
222420c8013fSlling 		(void) fprintf(stderr, gettext("cannot verify "
22250b5de56dSgjelinek 		    "fs %s: NFS mounted file system.\n"
22260b5de56dSgjelinek 		    "\tA local file system must be used.\n"),
222720c8013fSlling 		    fstab->zone_fs_special);
222820c8013fSlling 		return (Z_ERR);
222920c8013fSlling 	}
223020c8013fSlling 
223120c8013fSlling 	return (Z_OK);
223220c8013fSlling }
223320c8013fSlling 
2234b5abaf04Sgjelinek static int
isregfile(const char * path)223593239addSjohnlev isregfile(const char *path)
223693239addSjohnlev {
223793239addSjohnlev 	struct stat64 st;
223893239addSjohnlev 
223993239addSjohnlev 	if (stat64(path, &st) == -1)
224093239addSjohnlev 		return (-1);
224193239addSjohnlev 
224293239addSjohnlev 	return (S_ISREG(st.st_mode));
224393239addSjohnlev }
224493239addSjohnlev 
224593239addSjohnlev static int
verify_filesystems(zone_dochandle_t handle)22467c478bd9Sstevel@tonic-gate verify_filesystems(zone_dochandle_t handle)
22477c478bd9Sstevel@tonic-gate {
22487c478bd9Sstevel@tonic-gate 	int return_code = Z_OK;
22497c478bd9Sstevel@tonic-gate 	struct zone_fstab fstab;
22507c478bd9Sstevel@tonic-gate 	char cmdbuf[MAXPATHLEN];
22517c478bd9Sstevel@tonic-gate 	struct stat st;
22527c478bd9Sstevel@tonic-gate 
22537c478bd9Sstevel@tonic-gate 	/*
22547c478bd9Sstevel@tonic-gate 	 * Since the actual mount point is not known until the dependent mounts
22557c478bd9Sstevel@tonic-gate 	 * are performed, we don't attempt any path validation here: that will
22567c478bd9Sstevel@tonic-gate 	 * happen later when zoneadmd actually does the mounts.
22577c478bd9Sstevel@tonic-gate 	 */
22587c478bd9Sstevel@tonic-gate 	if (zonecfg_setfsent(handle) != Z_OK) {
22590b5de56dSgjelinek 		(void) fprintf(stderr, gettext("could not verify file systems: "
22607c478bd9Sstevel@tonic-gate 		    "unable to enumerate mounts\n"));
22617c478bd9Sstevel@tonic-gate 		return (Z_ERR);
22627c478bd9Sstevel@tonic-gate 	}
22637c478bd9Sstevel@tonic-gate 	while (zonecfg_getfsent(handle, &fstab) == Z_OK) {
22647c478bd9Sstevel@tonic-gate 		if (!zonecfg_valid_fs_type(fstab.zone_fs_type)) {
22657c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("cannot verify fs %s: "
22667c478bd9Sstevel@tonic-gate 			    "type %s is not allowed.\n"), fstab.zone_fs_dir,
22677c478bd9Sstevel@tonic-gate 			    fstab.zone_fs_type);
22687c478bd9Sstevel@tonic-gate 			return_code = Z_ERR;
22697c478bd9Sstevel@tonic-gate 			goto next_fs;
22707c478bd9Sstevel@tonic-gate 		}
22717c478bd9Sstevel@tonic-gate 		/*
22727c478bd9Sstevel@tonic-gate 		 * Verify /usr/lib/fs/<fstype>/mount exists.
22737c478bd9Sstevel@tonic-gate 		 */
22747c478bd9Sstevel@tonic-gate 		if (snprintf(cmdbuf, sizeof (cmdbuf), "/usr/lib/fs/%s/mount",
22757c478bd9Sstevel@tonic-gate 		    fstab.zone_fs_type) > sizeof (cmdbuf)) {
22767c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("cannot verify fs %s: "
22777c478bd9Sstevel@tonic-gate 			    "type %s is too long.\n"), fstab.zone_fs_dir,
22787c478bd9Sstevel@tonic-gate 			    fstab.zone_fs_type);
22797c478bd9Sstevel@tonic-gate 			return_code = Z_ERR;
22807c478bd9Sstevel@tonic-gate 			goto next_fs;
22817c478bd9Sstevel@tonic-gate 		}
22827c478bd9Sstevel@tonic-gate 		if (stat(cmdbuf, &st) != 0) {
22835ee84fbdSgjelinek 			(void) fprintf(stderr, gettext("could not verify fs "
22845ee84fbdSgjelinek 			    "%s: could not access %s: %s\n"), fstab.zone_fs_dir,
22857c478bd9Sstevel@tonic-gate 			    cmdbuf, strerror(errno));
22867c478bd9Sstevel@tonic-gate 			return_code = Z_ERR;
22877c478bd9Sstevel@tonic-gate 			goto next_fs;
22887c478bd9Sstevel@tonic-gate 		}
22897c478bd9Sstevel@tonic-gate 		if (!S_ISREG(st.st_mode)) {
22905ee84fbdSgjelinek 			(void) fprintf(stderr, gettext("could not verify fs "
22915ee84fbdSgjelinek 			    "%s: %s is not a regular file\n"),
22925ee84fbdSgjelinek 			    fstab.zone_fs_dir, cmdbuf);
22937c478bd9Sstevel@tonic-gate 			return_code = Z_ERR;
22947c478bd9Sstevel@tonic-gate 			goto next_fs;
22957c478bd9Sstevel@tonic-gate 		}
22967c478bd9Sstevel@tonic-gate 		/*
229793239addSjohnlev 		 * If zone_fs_raw is set, verify that there's an fsck
229893239addSjohnlev 		 * binary for it.  If zone_fs_raw is not set, and it's
229993239addSjohnlev 		 * not a regular file (lofi mount), and there's an fsck
230093239addSjohnlev 		 * binary for it, complain.
23017c478bd9Sstevel@tonic-gate 		 */
23027c478bd9Sstevel@tonic-gate 		if (snprintf(cmdbuf, sizeof (cmdbuf), "/usr/lib/fs/%s/fsck",
23037c478bd9Sstevel@tonic-gate 		    fstab.zone_fs_type) > sizeof (cmdbuf)) {
23047c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("cannot verify fs %s: "
23057c478bd9Sstevel@tonic-gate 			    "type %s is too long.\n"), fstab.zone_fs_dir,
23067c478bd9Sstevel@tonic-gate 			    fstab.zone_fs_type);
23077c478bd9Sstevel@tonic-gate 			return_code = Z_ERR;
23087c478bd9Sstevel@tonic-gate 			goto next_fs;
23097c478bd9Sstevel@tonic-gate 		}
23107c478bd9Sstevel@tonic-gate 		if (fstab.zone_fs_raw[0] != '\0' &&
23117c478bd9Sstevel@tonic-gate 		    (stat(cmdbuf, &st) != 0 || !S_ISREG(st.st_mode))) {
23127c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("cannot verify fs %s: "
23137c478bd9Sstevel@tonic-gate 			    "'raw' device specified but "
23147c478bd9Sstevel@tonic-gate 			    "no fsck executable exists for %s\n"),
23157c478bd9Sstevel@tonic-gate 			    fstab.zone_fs_dir, fstab.zone_fs_type);
23167c478bd9Sstevel@tonic-gate 			return_code = Z_ERR;
23177c478bd9Sstevel@tonic-gate 			goto next_fs;
231893239addSjohnlev 		} else if (fstab.zone_fs_raw[0] == '\0' &&
231993239addSjohnlev 		    stat(cmdbuf, &st) == 0 &&
232093239addSjohnlev 		    isregfile(fstab.zone_fs_special) != 1) {
232193239addSjohnlev 			(void) fprintf(stderr, gettext("could not verify fs "
232293239addSjohnlev 			    "%s: must specify 'raw' device for %s "
232393239addSjohnlev 			    "file systems\n"),
232493239addSjohnlev 			    fstab.zone_fs_dir, fstab.zone_fs_type);
232593239addSjohnlev 			return_code = Z_ERR;
232693239addSjohnlev 			goto next_fs;
23277c478bd9Sstevel@tonic-gate 		}
232820c8013fSlling 
232920c8013fSlling 		/* Verify fs_special. */
233020c8013fSlling 		if ((return_code = verify_fs_special(&fstab)) != Z_OK)
2331b5abaf04Sgjelinek 			goto next_fs;
233220c8013fSlling 
233320c8013fSlling 		/* Verify fs_raw. */
2334b5abaf04Sgjelinek 		if (fstab.zone_fs_raw[0] != '\0' &&
2335b5abaf04Sgjelinek 		    stat(fstab.zone_fs_raw, &st) != 0) {
23365ee84fbdSgjelinek 			/*
23375ee84fbdSgjelinek 			 * TRANSLATION_NOTE
23385ee84fbdSgjelinek 			 * fs is a literal that should not be translated.
23395ee84fbdSgjelinek 			 */
23405ee84fbdSgjelinek 			(void) fprintf(stderr, gettext("could not verify fs "
23415ee84fbdSgjelinek 			    "%s: could not access %s: %s\n"), fstab.zone_fs_dir,
2342b5abaf04Sgjelinek 			    fstab.zone_fs_raw, strerror(errno));
2343b5abaf04Sgjelinek 			return_code = Z_ERR;
2344b5abaf04Sgjelinek 			goto next_fs;
2345b5abaf04Sgjelinek 		}
23467c478bd9Sstevel@tonic-gate next_fs:
23477c478bd9Sstevel@tonic-gate 		zonecfg_free_fs_option_list(fstab.zone_fs_options);
23487c478bd9Sstevel@tonic-gate 	}
23497c478bd9Sstevel@tonic-gate 	(void) zonecfg_endfsent(handle);
23507c478bd9Sstevel@tonic-gate 
23517c478bd9Sstevel@tonic-gate 	return (return_code);
23527c478bd9Sstevel@tonic-gate }
23537c478bd9Sstevel@tonic-gate 
23547c478bd9Sstevel@tonic-gate static int
verify_limitpriv(zone_dochandle_t handle)2355ffbafc53Scomay verify_limitpriv(zone_dochandle_t handle)
2356ffbafc53Scomay {
2357ffbafc53Scomay 	char *privname = NULL;
2358ffbafc53Scomay 	int err;
2359ffbafc53Scomay 	priv_set_t *privs;
2360ffbafc53Scomay 
2361ffbafc53Scomay 	if ((privs = priv_allocset()) == NULL) {
2362ffbafc53Scomay 		zperror(gettext("failed to allocate privilege set"), B_FALSE);
2363ffbafc53Scomay 		return (Z_NOMEM);
2364ffbafc53Scomay 	}
2365ffbafc53Scomay 	err = zonecfg_get_privset(handle, privs, &privname);
2366ffbafc53Scomay 	switch (err) {
2367ffbafc53Scomay 	case Z_OK:
2368ffbafc53Scomay 		break;
2369ffbafc53Scomay 	case Z_PRIV_PROHIBITED:
2370ffbafc53Scomay 		(void) fprintf(stderr, gettext("privilege \"%s\" is not "
2371ffbafc53Scomay 		    "permitted within the zone's privilege set\n"), privname);
2372ffbafc53Scomay 		break;
2373ffbafc53Scomay 	case Z_PRIV_REQUIRED:
2374ffbafc53Scomay 		(void) fprintf(stderr, gettext("required privilege \"%s\" is "
2375ffbafc53Scomay 		    "missing from the zone's privilege set\n"), privname);
2376ffbafc53Scomay 		break;
2377ffbafc53Scomay 	case Z_PRIV_UNKNOWN:
2378ffbafc53Scomay 		(void) fprintf(stderr, gettext("unknown privilege \"%s\" "
2379ffbafc53Scomay 		    "specified in the zone's privilege set\n"), privname);
2380ffbafc53Scomay 		break;
2381ffbafc53Scomay 	default:
2382ffbafc53Scomay 		zperror(
2383ffbafc53Scomay 		    gettext("failed to determine the zone's privilege set"),
2384ffbafc53Scomay 		    B_TRUE);
2385ffbafc53Scomay 		break;
2386ffbafc53Scomay 	}
2387ffbafc53Scomay 	free(privname);
2388ffbafc53Scomay 	priv_freeset(privs);
2389ffbafc53Scomay 	return (err);
2390ffbafc53Scomay }
2391ffbafc53Scomay 
23921390a385Sgjelinek static void
free_local_netifs(int if_cnt,struct net_if ** if_list)23931390a385Sgjelinek free_local_netifs(int if_cnt, struct net_if **if_list)
23941390a385Sgjelinek {
23951390a385Sgjelinek 	int		i;
23961390a385Sgjelinek 
23971390a385Sgjelinek 	for (i = 0; i < if_cnt; i++) {
23981390a385Sgjelinek 		free(if_list[i]->name);
23991390a385Sgjelinek 		free(if_list[i]);
24001390a385Sgjelinek 	}
24011390a385Sgjelinek 	free(if_list);
24021390a385Sgjelinek }
24031390a385Sgjelinek 
24041390a385Sgjelinek /*
24051390a385Sgjelinek  * Get a list of the network interfaces, along with their address families,
24061390a385Sgjelinek  * that are plumbed in the global zone.  See if_tcp(7p) for a description
24071390a385Sgjelinek  * of the ioctls used here.
24081390a385Sgjelinek  */
24091390a385Sgjelinek static int
get_local_netifs(int * if_cnt,struct net_if *** if_list)24101390a385Sgjelinek get_local_netifs(int *if_cnt, struct net_if ***if_list)
24111390a385Sgjelinek {
24121390a385Sgjelinek 	int		s;
24131390a385Sgjelinek 	int		i;
24141390a385Sgjelinek 	int		res = Z_OK;
24151390a385Sgjelinek 	int		space_needed;
24161390a385Sgjelinek 	int		cnt = 0;
24171390a385Sgjelinek 	struct		lifnum if_num;
24181390a385Sgjelinek 	struct		lifconf if_conf;
24191390a385Sgjelinek 	struct		lifreq *if_reqp;
24201390a385Sgjelinek 	char		*if_buf;
24211390a385Sgjelinek 	struct net_if	**local_ifs = NULL;
24221390a385Sgjelinek 
24231390a385Sgjelinek 	*if_cnt = 0;
24241390a385Sgjelinek 	*if_list = NULL;
24251390a385Sgjelinek 
24261390a385Sgjelinek 	if ((s = socket(SOCKET_AF(AF_INET), SOCK_DGRAM, 0)) < 0)
24271390a385Sgjelinek 		return (Z_ERR);
24281390a385Sgjelinek 
24291390a385Sgjelinek 	/*
24301390a385Sgjelinek 	 * Come back here in the unlikely event that the number of interfaces
24311390a385Sgjelinek 	 * increases between the time we get the count and the time we do the
24321390a385Sgjelinek 	 * SIOCGLIFCONF ioctl.
24331390a385Sgjelinek 	 */
24341390a385Sgjelinek retry:
24351390a385Sgjelinek 	/* Get the number of interfaces. */
24361390a385Sgjelinek 	if_num.lifn_family = AF_UNSPEC;
24371390a385Sgjelinek 	if_num.lifn_flags = LIFC_NOXMIT;
24381390a385Sgjelinek 	if (ioctl(s, SIOCGLIFNUM, &if_num) < 0) {
24391390a385Sgjelinek 		(void) close(s);
24401390a385Sgjelinek 		return (Z_ERR);
24411390a385Sgjelinek 	}
24421390a385Sgjelinek 
24431390a385Sgjelinek 	/* Get the interface configuration list. */
24441390a385Sgjelinek 	space_needed = if_num.lifn_count * sizeof (struct lifreq);
24451390a385Sgjelinek 	if ((if_buf = malloc(space_needed)) == NULL) {
24461390a385Sgjelinek 		(void) close(s);
24471390a385Sgjelinek 		return (Z_ERR);
24481390a385Sgjelinek 	}
24491390a385Sgjelinek 	if_conf.lifc_family = AF_UNSPEC;
24501390a385Sgjelinek 	if_conf.lifc_flags = LIFC_NOXMIT;
24511390a385Sgjelinek 	if_conf.lifc_len = space_needed;
24521390a385Sgjelinek 	if_conf.lifc_buf = if_buf;
24531390a385Sgjelinek 	if (ioctl(s, SIOCGLIFCONF, &if_conf) < 0) {
24541390a385Sgjelinek 		free(if_buf);
24551390a385Sgjelinek 		/*
24561390a385Sgjelinek 		 * SIOCGLIFCONF returns EINVAL if the buffer we passed in is
24571390a385Sgjelinek 		 * too small.  In this case go back and get the new if cnt.
24581390a385Sgjelinek 		 */
24591390a385Sgjelinek 		if (errno == EINVAL)
24601390a385Sgjelinek 			goto retry;
24611390a385Sgjelinek 
24621390a385Sgjelinek 		(void) close(s);
24631390a385Sgjelinek 		return (Z_ERR);
24641390a385Sgjelinek 	}
24651390a385Sgjelinek 	(void) close(s);
24661390a385Sgjelinek 
24671390a385Sgjelinek 	/* Get the name and address family for each interface. */
24681390a385Sgjelinek 	if_reqp = if_conf.lifc_req;
24691390a385Sgjelinek 	for (i = 0; i < (if_conf.lifc_len / sizeof (struct lifreq)); i++) {
24701390a385Sgjelinek 		struct net_if	**p;
24711390a385Sgjelinek 		struct lifreq	req;
24721390a385Sgjelinek 
24731390a385Sgjelinek 		if (strcmp(LOOPBACK_IF, if_reqp->lifr_name) == 0) {
24741390a385Sgjelinek 			if_reqp++;
24751390a385Sgjelinek 			continue;
24761390a385Sgjelinek 		}
24771390a385Sgjelinek 
24781390a385Sgjelinek 		if ((s = socket(SOCKET_AF(if_reqp->lifr_addr.ss_family),
24791390a385Sgjelinek 		    SOCK_DGRAM, 0)) == -1) {
24801390a385Sgjelinek 			res = Z_ERR;
24811390a385Sgjelinek 			break;
24821390a385Sgjelinek 		}
24831390a385Sgjelinek 
24841390a385Sgjelinek 		(void) strncpy(req.lifr_name, if_reqp->lifr_name,
24851390a385Sgjelinek 		    sizeof (req.lifr_name));
24861390a385Sgjelinek 		if (ioctl(s, SIOCGLIFADDR, &req) < 0) {
24871390a385Sgjelinek 			(void) close(s);
24881390a385Sgjelinek 			if_reqp++;
24891390a385Sgjelinek 			continue;
24901390a385Sgjelinek 		}
24911390a385Sgjelinek 
24921390a385Sgjelinek 		if ((p = (struct net_if **)realloc(local_ifs,
24931390a385Sgjelinek 		    sizeof (struct net_if *) * (cnt + 1))) == NULL) {
24941390a385Sgjelinek 			res = Z_ERR;
24951390a385Sgjelinek 			break;
24961390a385Sgjelinek 		}
24971390a385Sgjelinek 		local_ifs = p;
24981390a385Sgjelinek 
24991390a385Sgjelinek 		if ((local_ifs[cnt] = malloc(sizeof (struct net_if))) == NULL) {
25001390a385Sgjelinek 			res = Z_ERR;
25011390a385Sgjelinek 			break;
25021390a385Sgjelinek 		}
25031390a385Sgjelinek 
25041390a385Sgjelinek 		if ((local_ifs[cnt]->name = strdup(if_reqp->lifr_name))
25051390a385Sgjelinek 		    == NULL) {
25061390a385Sgjelinek 			free(local_ifs[cnt]);
25071390a385Sgjelinek 			res = Z_ERR;
25081390a385Sgjelinek 			break;
25091390a385Sgjelinek 		}
25101390a385Sgjelinek 		local_ifs[cnt]->af = req.lifr_addr.ss_family;
25111390a385Sgjelinek 		cnt++;
25121390a385Sgjelinek 
25131390a385Sgjelinek 		(void) close(s);
25141390a385Sgjelinek 		if_reqp++;
25151390a385Sgjelinek 	}
25161390a385Sgjelinek 
25171390a385Sgjelinek 	free(if_buf);
25181390a385Sgjelinek 
25191390a385Sgjelinek 	if (res != Z_OK) {
25201390a385Sgjelinek 		free_local_netifs(cnt, local_ifs);
25211390a385Sgjelinek 	} else {
25221390a385Sgjelinek 		*if_cnt = cnt;
25231390a385Sgjelinek 		*if_list = local_ifs;
25241390a385Sgjelinek 	}
25251390a385Sgjelinek 
25261390a385Sgjelinek 	return (res);
25271390a385Sgjelinek }
25281390a385Sgjelinek 
25291390a385Sgjelinek static char *
af2str(int af)25301390a385Sgjelinek af2str(int af)
25311390a385Sgjelinek {
25321390a385Sgjelinek 	switch (af) {
25331390a385Sgjelinek 	case AF_INET:
25341390a385Sgjelinek 		return ("IPv4");
25351390a385Sgjelinek 	case AF_INET6:
25361390a385Sgjelinek 		return ("IPv6");
25371390a385Sgjelinek 	default:
25381390a385Sgjelinek 		return ("Unknown");
25391390a385Sgjelinek 	}
25401390a385Sgjelinek }
25411390a385Sgjelinek 
25421390a385Sgjelinek /*
25431390a385Sgjelinek  * Cross check the network interface name and address family with the
25441390a385Sgjelinek  * interfaces that are set up in the global zone so that we can print the
25451390a385Sgjelinek  * appropriate error message.
25461390a385Sgjelinek  */
25471390a385Sgjelinek static void
print_net_err(char * phys,char * addr,int af,char * msg)25481390a385Sgjelinek print_net_err(char *phys, char *addr, int af, char *msg)
25491390a385Sgjelinek {
25501390a385Sgjelinek 	int		i;
25511390a385Sgjelinek 	int		local_if_cnt = 0;
25521390a385Sgjelinek 	struct net_if	**local_ifs = NULL;
25531390a385Sgjelinek 	boolean_t	found_if = B_FALSE;
25541390a385Sgjelinek 	boolean_t	found_af = B_FALSE;
25551390a385Sgjelinek 
25561390a385Sgjelinek 	if (get_local_netifs(&local_if_cnt, &local_ifs) != Z_OK) {
25571390a385Sgjelinek 		(void) fprintf(stderr,
25581390a385Sgjelinek 		    gettext("could not verify %s %s=%s %s=%s\n\t%s\n"),
25591390a385Sgjelinek 		    "net", "address", addr, "physical", phys, msg);
25601390a385Sgjelinek 		return;
25611390a385Sgjelinek 	}
25621390a385Sgjelinek 
25631390a385Sgjelinek 	for (i = 0; i < local_if_cnt; i++) {
25641390a385Sgjelinek 		if (strcmp(phys, local_ifs[i]->name) == 0) {
25651390a385Sgjelinek 			found_if = B_TRUE;
25661390a385Sgjelinek 			if (af == local_ifs[i]->af) {
25671390a385Sgjelinek 				found_af = B_TRUE;
25681390a385Sgjelinek 				break;
25691390a385Sgjelinek 			}
25701390a385Sgjelinek 		}
25711390a385Sgjelinek 	}
25721390a385Sgjelinek 
25731390a385Sgjelinek 	free_local_netifs(local_if_cnt, local_ifs);
25741390a385Sgjelinek 
25751390a385Sgjelinek 	if (!found_if) {
25761390a385Sgjelinek 		(void) fprintf(stderr,
25771390a385Sgjelinek 		    gettext("could not verify %s %s=%s\n\t"
25781390a385Sgjelinek 		    "network interface %s is not plumbed in the global zone\n"),
25791390a385Sgjelinek 		    "net", "physical", phys, phys);
25801390a385Sgjelinek 		return;
25811390a385Sgjelinek 	}
25821390a385Sgjelinek 
25831390a385Sgjelinek 	/*
25841390a385Sgjelinek 	 * Print this error if we were unable to find the address family
25851390a385Sgjelinek 	 * for this interface.  If the af variable is not initialized to
25861390a385Sgjelinek 	 * to something meaningful by the caller (not AF_UNSPEC) then we
25871390a385Sgjelinek 	 * also skip this message since it wouldn't be informative.
25881390a385Sgjelinek 	 */
25891390a385Sgjelinek 	if (!found_af && af != AF_UNSPEC) {
25901390a385Sgjelinek 		(void) fprintf(stderr,
25911390a385Sgjelinek 		    gettext("could not verify %s %s=%s %s=%s\n\tthe %s address "
2592f4b3ec61Sdh155122 		    "family is not configured on this network interface in "
2593f4b3ec61Sdh155122 		    "the\n\tglobal zone\n"),
25941390a385Sgjelinek 		    "net", "address", addr, "physical", phys, af2str(af));
25951390a385Sgjelinek 		return;
25961390a385Sgjelinek 	}
25971390a385Sgjelinek 
25981390a385Sgjelinek 	(void) fprintf(stderr,
25991390a385Sgjelinek 	    gettext("could not verify %s %s=%s %s=%s\n\t%s\n"),
26001390a385Sgjelinek 	    "net", "address", addr, "physical", phys, msg);
26011390a385Sgjelinek }
26021390a385Sgjelinek 
2603ffbafc53Scomay static int
verify_handle(int cmd_num,zone_dochandle_t handle,char * argv[])2604ce28b40eSzt129084 verify_handle(int cmd_num, zone_dochandle_t handle, char *argv[])
26057c478bd9Sstevel@tonic-gate {
26067c478bd9Sstevel@tonic-gate 	struct zone_nwiftab nwiftab;
26077c478bd9Sstevel@tonic-gate 	int return_code = Z_OK;
26087c478bd9Sstevel@tonic-gate 	int err;
2609108322fbScarlsonj 	boolean_t in_alt_root;
2610f4b3ec61Sdh155122 	zone_iptype_t iptype;
26112b24ab6bSSebastien Roy 	dladm_handle_t dh;
26122b24ab6bSSebastien Roy 	dladm_status_t status;
26132b24ab6bSSebastien Roy 	datalink_id_t linkid;
26142b24ab6bSSebastien Roy 	char errmsg[DLADM_STRSIZE];
26157c478bd9Sstevel@tonic-gate 
2616108322fbScarlsonj 	in_alt_root = zonecfg_in_alt_root();
2617108322fbScarlsonj 	if (in_alt_root)
2618108322fbScarlsonj 		goto no_net;
2619108322fbScarlsonj 
2620f4b3ec61Sdh155122 	if ((err = zonecfg_get_iptype(handle, &iptype)) != Z_OK) {
2621f4b3ec61Sdh155122 		errno = err;
2622f4b3ec61Sdh155122 		zperror(cmd_to_str(cmd_num), B_TRUE);
2623f4b3ec61Sdh155122 		zonecfg_fini_handle(handle);
2624f4b3ec61Sdh155122 		return (Z_ERR);
2625f4b3ec61Sdh155122 	}
26267c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_setnwifent(handle)) != Z_OK) {
26277c478bd9Sstevel@tonic-gate 		errno = err;
26287c478bd9Sstevel@tonic-gate 		zperror(cmd_to_str(cmd_num), B_TRUE);
26297c478bd9Sstevel@tonic-gate 		zonecfg_fini_handle(handle);
26307c478bd9Sstevel@tonic-gate 		return (Z_ERR);
26317c478bd9Sstevel@tonic-gate 	}
26327c478bd9Sstevel@tonic-gate 	while (zonecfg_getnwifent(handle, &nwiftab) == Z_OK) {
26337c478bd9Sstevel@tonic-gate 		struct lifreq lifr;
26341390a385Sgjelinek 		sa_family_t af = AF_UNSPEC;
2635f4b3ec61Sdh155122 		char dl_owner_zname[ZONENAME_MAX];
2636f4b3ec61Sdh155122 		zoneid_t dl_owner_zid;
2637f4b3ec61Sdh155122 		zoneid_t target_zid;
2638f4b3ec61Sdh155122 		int res;
26397c478bd9Sstevel@tonic-gate 
26407c478bd9Sstevel@tonic-gate 		/* skip any loopback interfaces */
26417c478bd9Sstevel@tonic-gate 		if (strcmp(nwiftab.zone_nwif_physical, "lo0") == 0)
26427c478bd9Sstevel@tonic-gate 			continue;
2643f4b3ec61Sdh155122 		switch (iptype) {
2644f4b3ec61Sdh155122 		case ZS_SHARED:
2645f4b3ec61Sdh155122 			if ((res = zonecfg_valid_net_address(
2646f4b3ec61Sdh155122 			    nwiftab.zone_nwif_address, &lifr)) != Z_OK) {
26471390a385Sgjelinek 				print_net_err(nwiftab.zone_nwif_physical,
26481390a385Sgjelinek 				    nwiftab.zone_nwif_address, af,
26491390a385Sgjelinek 				    zonecfg_strerror(res));
26507c478bd9Sstevel@tonic-gate 				return_code = Z_ERR;
26517c478bd9Sstevel@tonic-gate 				continue;
26527c478bd9Sstevel@tonic-gate 			}
26537c478bd9Sstevel@tonic-gate 			af = lifr.lifr_addr.ss_family;
2654f4b3ec61Sdh155122 			if (!zonecfg_ifname_exists(af,
2655f4b3ec61Sdh155122 			    nwiftab.zone_nwif_physical)) {
265622321485Svp157776 				/*
2657f4b3ec61Sdh155122 				 * The interface failed to come up. We continue
2658f4b3ec61Sdh155122 				 * on anyway for the sake of consistency: a
2659f4b3ec61Sdh155122 				 * zone is not shut down if the interface fails
2660f4b3ec61Sdh155122 				 * any time after boot, nor does the global zone
2661f4b3ec61Sdh155122 				 * fail to boot if an interface fails.
266222321485Svp157776 				 */
266322321485Svp157776 				(void) fprintf(stderr,
2664f4b3ec61Sdh155122 				    gettext("WARNING: skipping network "
2665f4b3ec61Sdh155122 				    "interface '%s' which may not be "
2666f4b3ec61Sdh155122 				    "present/plumbed in the global "
2667f4b3ec61Sdh155122 				    "zone.\n"),
266822321485Svp157776 				    nwiftab.zone_nwif_physical);
26697c478bd9Sstevel@tonic-gate 			}
2670f4b3ec61Sdh155122 			break;
2671f4b3ec61Sdh155122 		case ZS_EXCLUSIVE:
2672f4b3ec61Sdh155122 			/* Warning if it exists for either IPv4 or IPv6 */
2673f4b3ec61Sdh155122 
2674f4b3ec61Sdh155122 			if (zonecfg_ifname_exists(AF_INET,
2675f4b3ec61Sdh155122 			    nwiftab.zone_nwif_physical) ||
2676f4b3ec61Sdh155122 			    zonecfg_ifname_exists(AF_INET6,
2677f4b3ec61Sdh155122 			    nwiftab.zone_nwif_physical)) {
2678f4b3ec61Sdh155122 				(void) fprintf(stderr,
2679f4b3ec61Sdh155122 				    gettext("WARNING: skipping network "
2680f4b3ec61Sdh155122 				    "interface '%s' which is used in the "
2681f4b3ec61Sdh155122 				    "global zone.\n"),
2682f4b3ec61Sdh155122 				    nwiftab.zone_nwif_physical);
2683f4b3ec61Sdh155122 				break;
2684f4b3ec61Sdh155122 			}
2685948f2876Sss150715 
2686f4b3ec61Sdh155122 			/*
26872b24ab6bSSebastien Roy 			 * Verify that the datalink exists and that it isn't
26882b24ab6bSSebastien Roy 			 * already assigned to a zone.
2689f4b3ec61Sdh155122 			 */
26902b24ab6bSSebastien Roy 			if ((status = dladm_open(&dh)) == DLADM_STATUS_OK) {
26912b24ab6bSSebastien Roy 				status = dladm_name2info(dh,
26922b24ab6bSSebastien Roy 				    nwiftab.zone_nwif_physical, &linkid, NULL,
26932b24ab6bSSebastien Roy 				    NULL, NULL);
26942b24ab6bSSebastien Roy 				dladm_close(dh);
26952b24ab6bSSebastien Roy 			}
26962b24ab6bSSebastien Roy 			if (status != DLADM_STATUS_OK) {
2697f4b3ec61Sdh155122 				(void) fprintf(stderr,
2698f4b3ec61Sdh155122 				    gettext("WARNING: skipping network "
26992b24ab6bSSebastien Roy 				    "interface '%s': %s\n"),
2700948f2876Sss150715 				    nwiftab.zone_nwif_physical,
27012b24ab6bSSebastien Roy 				    dladm_status2str(status, errmsg));
2702f4b3ec61Sdh155122 				break;
2703f4b3ec61Sdh155122 			}
2704f4b3ec61Sdh155122 			dl_owner_zid = ALL_ZONES;
27052b24ab6bSSebastien Roy 			if (zone_check_datalink(&dl_owner_zid, linkid) != 0)
2706f4b3ec61Sdh155122 				break;
2707f4b3ec61Sdh155122 
2708f4b3ec61Sdh155122 			/*
2709f4b3ec61Sdh155122 			 * If the zone being verified is
2710f4b3ec61Sdh155122 			 * running and owns the interface
2711f4b3ec61Sdh155122 			 */
2712f4b3ec61Sdh155122 			target_zid = getzoneidbyname(target_zone);
2713f4b3ec61Sdh155122 			if (target_zid == dl_owner_zid)
2714f4b3ec61Sdh155122 				break;
2715f4b3ec61Sdh155122 
2716f4b3ec61Sdh155122 			/* Zone id match failed, use name to check */
2717f4b3ec61Sdh155122 			if (getzonenamebyid(dl_owner_zid, dl_owner_zname,
2718f4b3ec61Sdh155122 			    ZONENAME_MAX) < 0) {
2719f4b3ec61Sdh155122 				/* No name, show ID instead */
2720f4b3ec61Sdh155122 				(void) snprintf(dl_owner_zname, ZONENAME_MAX,
2721f4b3ec61Sdh155122 				    "<%d>", dl_owner_zid);
2722f4b3ec61Sdh155122 			} else if (strcmp(dl_owner_zname, target_zone) == 0)
2723f4b3ec61Sdh155122 				break;
2724f4b3ec61Sdh155122 
2725f4b3ec61Sdh155122 			/*
2726f4b3ec61Sdh155122 			 * Note here we only report a warning that
2727f4b3ec61Sdh155122 			 * the interface is already in use by another
2728f4b3ec61Sdh155122 			 * running zone, and the verify process just
2729f4b3ec61Sdh155122 			 * goes on, if the interface is still in use
2730f4b3ec61Sdh155122 			 * when this zone really boots up, zoneadmd
2731f4b3ec61Sdh155122 			 * will find it. If the name of the zone which
2732f4b3ec61Sdh155122 			 * owns this interface cannot be determined,
2733f4b3ec61Sdh155122 			 * then it is not possible to determine if there
2734f4b3ec61Sdh155122 			 * is a conflict so just report it as a warning.
2735f4b3ec61Sdh155122 			 */
2736f4b3ec61Sdh155122 			(void) fprintf(stderr,
2737f4b3ec61Sdh155122 			    gettext("WARNING: skipping network interface "
2738f4b3ec61Sdh155122 			    "'%s' which is used by the non-global zone "
2739f4b3ec61Sdh155122 			    "'%s'.\n"), nwiftab.zone_nwif_physical,
2740f4b3ec61Sdh155122 			    dl_owner_zname);
2741f4b3ec61Sdh155122 			break;
2742f4b3ec61Sdh155122 		}
27437c478bd9Sstevel@tonic-gate 	}
27447c478bd9Sstevel@tonic-gate 	(void) zonecfg_endnwifent(handle);
2745108322fbScarlsonj no_net:
27467c478bd9Sstevel@tonic-gate 
2747e7f3c547Sgjelinek 	/* verify that lofs has not been excluded from the kernel */
27488cd327d5Sgjelinek 	if (!(cmd_num == CMD_DETACH || cmd_num == CMD_ATTACH ||
27498cd327d5Sgjelinek 	    cmd_num == CMD_MOVE || cmd_num == CMD_CLONE) &&
27508cd327d5Sgjelinek 	    modctl(MODLOAD, 1, "fs/lofs", NULL) != 0) {
2751e7f3c547Sgjelinek 		if (errno == ENXIO)
2752e7f3c547Sgjelinek 			(void) fprintf(stderr, gettext("could not verify "
2753e7f3c547Sgjelinek 			    "lofs(7FS): possibly excluded in /etc/system\n"));
2754e7f3c547Sgjelinek 		else
2755e7f3c547Sgjelinek 			(void) fprintf(stderr, gettext("could not verify "
2756e7f3c547Sgjelinek 			    "lofs(7FS): %s\n"), strerror(errno));
2757e7f3c547Sgjelinek 		return_code = Z_ERR;
2758e7f3c547Sgjelinek 	}
2759e7f3c547Sgjelinek 
27607c478bd9Sstevel@tonic-gate 	if (verify_filesystems(handle) != Z_OK)
27617c478bd9Sstevel@tonic-gate 		return_code = Z_ERR;
2762108322fbScarlsonj 	if (!in_alt_root && verify_rctls(handle) != Z_OK)
27637c478bd9Sstevel@tonic-gate 		return_code = Z_ERR;
2764108322fbScarlsonj 	if (!in_alt_root && verify_pool(handle) != Z_OK)
27657c478bd9Sstevel@tonic-gate 		return_code = Z_ERR;
2766ce28b40eSzt129084 	if (!in_alt_root && verify_brand(handle, cmd_num, argv) != Z_OK)
27679acbbeafSnn35248 		return_code = Z_ERR;
2768fa9e4066Sahrens 	if (!in_alt_root && verify_datasets(handle) != Z_OK)
2769fa9e4066Sahrens 		return_code = Z_ERR;
2770ffbafc53Scomay 
2771ffbafc53Scomay 	/*
2772ffbafc53Scomay 	 * As the "mount" command is used for patching/upgrading of zones
2773ffbafc53Scomay 	 * or other maintenance processes, the zone's privilege set is not
2774ffbafc53Scomay 	 * checked in this case.  Instead, the default, safe set of
2775ffbafc53Scomay 	 * privileges will be used when this zone is created in the
2776ffbafc53Scomay 	 * kernel.
2777ffbafc53Scomay 	 */
2778ffbafc53Scomay 	if (!in_alt_root && cmd_num != CMD_MOUNT &&
2779ffbafc53Scomay 	    verify_limitpriv(handle) != Z_OK)
2780ffbafc53Scomay 		return_code = Z_ERR;
27818cd327d5Sgjelinek 
27828cd327d5Sgjelinek 	return (return_code);
27838cd327d5Sgjelinek }
27848cd327d5Sgjelinek 
27858cd327d5Sgjelinek static int
verify_details(int cmd_num,char * argv[])2786ce28b40eSzt129084 verify_details(int cmd_num, char *argv[])
27878cd327d5Sgjelinek {
27888cd327d5Sgjelinek 	zone_dochandle_t handle;
27898cd327d5Sgjelinek 	char zonepath[MAXPATHLEN], checkpath[MAXPATHLEN];
27908cd327d5Sgjelinek 	int return_code = Z_OK;
27918cd327d5Sgjelinek 	int err;
27928cd327d5Sgjelinek 
27938cd327d5Sgjelinek 	if ((handle = zonecfg_init_handle()) == NULL) {
27948cd327d5Sgjelinek 		zperror(cmd_to_str(cmd_num), B_TRUE);
27958cd327d5Sgjelinek 		return (Z_ERR);
27968cd327d5Sgjelinek 	}
27978cd327d5Sgjelinek 	if ((err = zonecfg_get_handle(target_zone, handle)) != Z_OK) {
27988cd327d5Sgjelinek 		errno = err;
27998cd327d5Sgjelinek 		zperror(cmd_to_str(cmd_num), B_TRUE);
28008cd327d5Sgjelinek 		zonecfg_fini_handle(handle);
28018cd327d5Sgjelinek 		return (Z_ERR);
28028cd327d5Sgjelinek 	}
28038cd327d5Sgjelinek 	if ((err = zonecfg_get_zonepath(handle, zonepath, sizeof (zonepath))) !=
28048cd327d5Sgjelinek 	    Z_OK) {
28058cd327d5Sgjelinek 		errno = err;
28068cd327d5Sgjelinek 		zperror(cmd_to_str(cmd_num), B_TRUE);
28078cd327d5Sgjelinek 		zonecfg_fini_handle(handle);
28088cd327d5Sgjelinek 		return (Z_ERR);
28098cd327d5Sgjelinek 	}
28108cd327d5Sgjelinek 	/*
28118cd327d5Sgjelinek 	 * zonecfg_get_zonepath() gets its data from the XML repository.
28128cd327d5Sgjelinek 	 * Verify this against the index file, which is checked first by
28138cd327d5Sgjelinek 	 * zone_get_zonepath().  If they don't match, bail out.
28148cd327d5Sgjelinek 	 */
28158cd327d5Sgjelinek 	if ((err = zone_get_zonepath(target_zone, checkpath,
28168cd327d5Sgjelinek 	    sizeof (checkpath))) != Z_OK) {
28178cd327d5Sgjelinek 		errno = err;
28188cd327d5Sgjelinek 		zperror2(target_zone, gettext("could not get zone path"));
2819e767a340Sgjelinek 		zonecfg_fini_handle(handle);
28208cd327d5Sgjelinek 		return (Z_ERR);
28218cd327d5Sgjelinek 	}
28228cd327d5Sgjelinek 	if (strcmp(zonepath, checkpath) != 0) {
28238cd327d5Sgjelinek 		/*
28248cd327d5Sgjelinek 		 * TRANSLATION_NOTE
28258cd327d5Sgjelinek 		 * XML and zonepath are literals that should not be translated.
28268cd327d5Sgjelinek 		 */
28278cd327d5Sgjelinek 		(void) fprintf(stderr, gettext("The XML repository has "
28288cd327d5Sgjelinek 		    "zonepath '%s',\nbut the index file has zonepath '%s'.\n"
28298cd327d5Sgjelinek 		    "These must match, so fix the incorrect entry.\n"),
28308cd327d5Sgjelinek 		    zonepath, checkpath);
2831e767a340Sgjelinek 		zonecfg_fini_handle(handle);
28328cd327d5Sgjelinek 		return (Z_ERR);
28338cd327d5Sgjelinek 	}
2834ca733e25S 	if (cmd_num != CMD_ATTACH &&
2835ca733e25S 	    validate_zonepath(zonepath, cmd_num) != Z_OK) {
28368cd327d5Sgjelinek 		(void) fprintf(stderr, gettext("could not verify zonepath %s "
28378cd327d5Sgjelinek 		    "because of the above errors.\n"), zonepath);
28388cd327d5Sgjelinek 		return_code = Z_ERR;
28398cd327d5Sgjelinek 	}
28408cd327d5Sgjelinek 
2841ce28b40eSzt129084 	if (verify_handle(cmd_num, handle, argv) != Z_OK)
28428cd327d5Sgjelinek 		return_code = Z_ERR;
28438cd327d5Sgjelinek 
28447c478bd9Sstevel@tonic-gate 	zonecfg_fini_handle(handle);
28457c478bd9Sstevel@tonic-gate 	if (return_code == Z_ERR)
28467c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
28477c478bd9Sstevel@tonic-gate 		    gettext("%s: zone %s failed to verify\n"),
28487c478bd9Sstevel@tonic-gate 		    execname, target_zone);
28497c478bd9Sstevel@tonic-gate 	return (return_code);
28507c478bd9Sstevel@tonic-gate }
28517c478bd9Sstevel@tonic-gate 
28527c478bd9Sstevel@tonic-gate static int
verify_func(int argc,char * argv[])28537c478bd9Sstevel@tonic-gate verify_func(int argc, char *argv[])
28547c478bd9Sstevel@tonic-gate {
28557c478bd9Sstevel@tonic-gate 	int arg;
28567c478bd9Sstevel@tonic-gate 
28577c478bd9Sstevel@tonic-gate 	optind = 0;
28587c478bd9Sstevel@tonic-gate 	if ((arg = getopt(argc, argv, "?")) != EOF) {
28597c478bd9Sstevel@tonic-gate 		switch (arg) {
28607c478bd9Sstevel@tonic-gate 		case '?':
28617c478bd9Sstevel@tonic-gate 			sub_usage(SHELP_VERIFY, CMD_VERIFY);
28627c478bd9Sstevel@tonic-gate 			return (optopt == '?' ? Z_OK : Z_USAGE);
28637c478bd9Sstevel@tonic-gate 		default:
28647c478bd9Sstevel@tonic-gate 			sub_usage(SHELP_VERIFY, CMD_VERIFY);
28657c478bd9Sstevel@tonic-gate 			return (Z_USAGE);
28667c478bd9Sstevel@tonic-gate 		}
28677c478bd9Sstevel@tonic-gate 	}
28687c478bd9Sstevel@tonic-gate 	if (argc > optind) {
28697c478bd9Sstevel@tonic-gate 		sub_usage(SHELP_VERIFY, CMD_VERIFY);
28707c478bd9Sstevel@tonic-gate 		return (Z_USAGE);
28717c478bd9Sstevel@tonic-gate 	}
28729acbbeafSnn35248 	if (sanity_check(target_zone, CMD_VERIFY, B_FALSE, B_FALSE, B_FALSE)
28739acbbeafSnn35248 	    != Z_OK)
28747c478bd9Sstevel@tonic-gate 		return (Z_ERR);
2875ce28b40eSzt129084 	return (verify_details(CMD_VERIFY, argv));
28767c478bd9Sstevel@tonic-gate }
28777c478bd9Sstevel@tonic-gate 
28789acbbeafSnn35248 static int
addoptions(char * buf,char * argv[],size_t len)2879ff17c8bfSgjelinek addoptions(char *buf, char *argv[], size_t len)
2880ff17c8bfSgjelinek {
2881ff17c8bfSgjelinek 	int i = 0;
2882ff17c8bfSgjelinek 
2883ff17c8bfSgjelinek 	if (buf[0] == '\0')
2884ff17c8bfSgjelinek 		return (Z_OK);
2885ff17c8bfSgjelinek 
2886ff17c8bfSgjelinek 	while (argv[i] != NULL) {
2887ff17c8bfSgjelinek 		if (strlcat(buf, " ", len) >= len ||
2888ff17c8bfSgjelinek 		    strlcat(buf, argv[i++], len) >= len) {
2889ff17c8bfSgjelinek 			zerror("Command line too long");
2890ff17c8bfSgjelinek 			return (Z_ERR);
2891ff17c8bfSgjelinek 		}
2892ff17c8bfSgjelinek 	}
2893ff17c8bfSgjelinek 
2894ff17c8bfSgjelinek 	return (Z_OK);
2895ff17c8bfSgjelinek }
2896ff17c8bfSgjelinek 
2897ff17c8bfSgjelinek static int
addopt(char * buf,int opt,char * optarg,size_t bufsize)28989acbbeafSnn35248 addopt(char *buf, int opt, char *optarg, size_t bufsize)
28999acbbeafSnn35248 {
29009acbbeafSnn35248 	char optstring[4];
29019acbbeafSnn35248 
29029acbbeafSnn35248 	if (opt > 0)
29039acbbeafSnn35248 		(void) sprintf(optstring, " -%c", opt);
29049acbbeafSnn35248 	else
29059acbbeafSnn35248 		(void) strcpy(optstring, " ");
29069acbbeafSnn35248 
29079acbbeafSnn35248 	if ((strlcat(buf, optstring, bufsize) > bufsize))
29089acbbeafSnn35248 		return (Z_ERR);
2909ff17c8bfSgjelinek 
29109acbbeafSnn35248 	if ((optarg != NULL) && (strlcat(buf, optarg, bufsize) > bufsize))
29119acbbeafSnn35248 		return (Z_ERR);
2912ff17c8bfSgjelinek 
29139acbbeafSnn35248 	return (Z_OK);
29149acbbeafSnn35248 }
29157c478bd9Sstevel@tonic-gate 
2916ff17c8bfSgjelinek /* ARGSUSED */
29177c478bd9Sstevel@tonic-gate static int
install_func(int argc,char * argv[])29187c478bd9Sstevel@tonic-gate install_func(int argc, char *argv[])
29197c478bd9Sstevel@tonic-gate {
29209acbbeafSnn35248 	char cmdbuf[MAXPATHLEN];
29211100f00dSgjelinek 	char postcmdbuf[MAXPATHLEN];
29227c478bd9Sstevel@tonic-gate 	int lockfd;
29239acbbeafSnn35248 	int arg, err, subproc_err;
29247c478bd9Sstevel@tonic-gate 	char zonepath[MAXPATHLEN];
2925123807fbSedp 	brand_handle_t bh = NULL;
29267c478bd9Sstevel@tonic-gate 	int status;
29271100f00dSgjelinek 	boolean_t do_postinstall = B_FALSE;
2928ff17c8bfSgjelinek 	boolean_t brand_help = B_FALSE;
29299acbbeafSnn35248 	char opts[128];
29309acbbeafSnn35248 
29319acbbeafSnn35248 	if (target_zone == NULL) {
29329acbbeafSnn35248 		sub_usage(SHELP_INSTALL, CMD_INSTALL);
29339acbbeafSnn35248 		return (Z_USAGE);
29349acbbeafSnn35248 	}
29357c478bd9Sstevel@tonic-gate 
2936108322fbScarlsonj 	if (zonecfg_in_alt_root()) {
2937108322fbScarlsonj 		zerror(gettext("cannot install zone in alternate root"));
2938108322fbScarlsonj 		return (Z_ERR);
2939108322fbScarlsonj 	}
2940108322fbScarlsonj 
29419acbbeafSnn35248 	if ((err = zone_get_zonepath(target_zone, zonepath,
29429acbbeafSnn35248 	    sizeof (zonepath))) != Z_OK) {
29439acbbeafSnn35248 		errno = err;
29449acbbeafSnn35248 		zperror2(target_zone, gettext("could not get zone path"));
29459acbbeafSnn35248 		return (Z_ERR);
29469acbbeafSnn35248 	}
29479acbbeafSnn35248 
29489acbbeafSnn35248 	/* Fetch the install command from the brand configuration.  */
2949123807fbSedp 	if ((bh = brand_open(target_brand)) == NULL) {
29509acbbeafSnn35248 		zerror(gettext("missing or invalid brand"));
29519acbbeafSnn35248 		return (Z_ERR);
29529acbbeafSnn35248 	}
29539acbbeafSnn35248 
2954ff17c8bfSgjelinek 	if (get_hook(bh, cmdbuf, sizeof (cmdbuf), brand_get_install,
2955ff17c8bfSgjelinek 	    target_zone, zonepath) != Z_OK) {
29569acbbeafSnn35248 		zerror("invalid brand configuration: missing install resource");
2957123807fbSedp 		brand_close(bh);
29589acbbeafSnn35248 		return (Z_ERR);
29599acbbeafSnn35248 	}
29609acbbeafSnn35248 
2961ff17c8bfSgjelinek 	if (get_hook(bh, postcmdbuf, sizeof (postcmdbuf), brand_get_postinstall,
2962ff17c8bfSgjelinek 	    target_zone, zonepath) != Z_OK) {
29631100f00dSgjelinek 		zerror("invalid brand configuration: missing postinstall "
29641100f00dSgjelinek 		    "resource");
29651100f00dSgjelinek 		brand_close(bh);
29661100f00dSgjelinek 		return (Z_ERR);
29671100f00dSgjelinek 	}
29681100f00dSgjelinek 
2969ff17c8bfSgjelinek 	if (postcmdbuf[0] != '\0')
2970ff17c8bfSgjelinek 		do_postinstall = B_TRUE;
2971ff17c8bfSgjelinek 
29729acbbeafSnn35248 	(void) strcpy(opts, "?x:");
29739acbbeafSnn35248 	/*
29749acbbeafSnn35248 	 * Fetch the list of recognized command-line options from
29759acbbeafSnn35248 	 * the brand configuration file.
29769acbbeafSnn35248 	 */
2977123807fbSedp 	if (brand_get_installopts(bh, opts + strlen(opts),
29789acbbeafSnn35248 	    sizeof (opts) - strlen(opts)) != 0) {
29799acbbeafSnn35248 		zerror("invalid brand configuration: missing "
29809acbbeafSnn35248 		    "install options resource");
2981123807fbSedp 		brand_close(bh);
29829acbbeafSnn35248 		return (Z_ERR);
29839acbbeafSnn35248 	}
2984ff17c8bfSgjelinek 
2985123807fbSedp 	brand_close(bh);
29869acbbeafSnn35248 
2987ff17c8bfSgjelinek 	if (cmdbuf[0] == '\0') {
2988ff17c8bfSgjelinek 		zerror("Missing brand install command");
2989ff17c8bfSgjelinek 		return (Z_ERR);
2990ff17c8bfSgjelinek 	}
2991ff17c8bfSgjelinek 
2992ff17c8bfSgjelinek 	/* Check the argv string for args we handle internally */
29937c478bd9Sstevel@tonic-gate 	optind = 0;
2994ff17c8bfSgjelinek 	opterr = 0;
29959acbbeafSnn35248 	while ((arg = getopt(argc, argv, opts)) != EOF) {
29967c478bd9Sstevel@tonic-gate 		switch (arg) {
29977c478bd9Sstevel@tonic-gate 		case '?':
2998ff17c8bfSgjelinek 			if (optopt == '?') {
29997c478bd9Sstevel@tonic-gate 				sub_usage(SHELP_INSTALL, CMD_INSTALL);
3000ff17c8bfSgjelinek 				brand_help = B_TRUE;
30010b5de56dSgjelinek 			}
3002ff17c8bfSgjelinek 			/* Ignore unknown options - may be brand specific. */
30030b5de56dSgjelinek 			break;
30047c478bd9Sstevel@tonic-gate 		default:
3005ff17c8bfSgjelinek 			/* Ignore unknown options - may be brand specific. */
3006ff17c8bfSgjelinek 			break;
30077c478bd9Sstevel@tonic-gate 		}
30089acbbeafSnn35248 
30099acbbeafSnn35248 		/*
3010ff17c8bfSgjelinek 		 * Append the option to the command line passed to the
3011ff17c8bfSgjelinek 		 * brand-specific install and postinstall routines.
30129acbbeafSnn35248 		 */
3013ff17c8bfSgjelinek 		if (addopt(cmdbuf, optopt, optarg, sizeof (cmdbuf)) != Z_OK) {
30149acbbeafSnn35248 			zerror("Install command line too long");
30159acbbeafSnn35248 			return (Z_ERR);
30167c478bd9Sstevel@tonic-gate 		}
3017ff17c8bfSgjelinek 		if (addopt(postcmdbuf, optopt, optarg, sizeof (postcmdbuf))
3018ff17c8bfSgjelinek 		    != Z_OK) {
30191100f00dSgjelinek 			zerror("Post-Install command line too long");
30201100f00dSgjelinek 			return (Z_ERR);
30211100f00dSgjelinek 		}
30229acbbeafSnn35248 	}
30239acbbeafSnn35248 
30249acbbeafSnn35248 	for (; optind < argc; optind++) {
3025ff17c8bfSgjelinek 		if (addopt(cmdbuf, 0, argv[optind], sizeof (cmdbuf)) != Z_OK) {
30269acbbeafSnn35248 			zerror("Install command line too long");
30279acbbeafSnn35248 			return (Z_ERR);
30289acbbeafSnn35248 		}
3029ff17c8bfSgjelinek 
3030ff17c8bfSgjelinek 		if (addopt(postcmdbuf, 0, argv[optind], sizeof (postcmdbuf))
3031ff17c8bfSgjelinek 		    != Z_OK) {
30321100f00dSgjelinek 			zerror("Post-Install command line too long");
30331100f00dSgjelinek 			return (Z_ERR);
30341100f00dSgjelinek 		}
30359acbbeafSnn35248 	}
30369acbbeafSnn35248 
3037ff17c8bfSgjelinek 	if (!brand_help) {
3038ff17c8bfSgjelinek 		if (sanity_check(target_zone, CMD_INSTALL, B_FALSE, B_TRUE,
3039ff17c8bfSgjelinek 		    B_FALSE) != Z_OK)
30407c478bd9Sstevel@tonic-gate 			return (Z_ERR);
3041ce28b40eSzt129084 		if (verify_details(CMD_INSTALL, argv) != Z_OK)
30427c478bd9Sstevel@tonic-gate 			return (Z_ERR);
30437c478bd9Sstevel@tonic-gate 
3044ff17c8bfSgjelinek 		if (zonecfg_grab_lock_file(target_zone, &lockfd) != Z_OK) {
3045ff17c8bfSgjelinek 			zerror(gettext("another %s may have an operation in "
3046ff17c8bfSgjelinek 			    "progress."), "zoneadm");
30477c478bd9Sstevel@tonic-gate 			return (Z_ERR);
30487c478bd9Sstevel@tonic-gate 		}
30497c478bd9Sstevel@tonic-gate 		err = zone_set_state(target_zone, ZONE_STATE_INCOMPLETE);
30507c478bd9Sstevel@tonic-gate 		if (err != Z_OK) {
30517c478bd9Sstevel@tonic-gate 			errno = err;
30527c478bd9Sstevel@tonic-gate 			zperror2(target_zone, gettext("could not set state"));
30537c478bd9Sstevel@tonic-gate 			goto done;
30547c478bd9Sstevel@tonic-gate 		}
30557c478bd9Sstevel@tonic-gate 
30569acbbeafSnn35248 		create_zfs_zonepath(zonepath);
30577c478bd9Sstevel@tonic-gate 	}
30587c478bd9Sstevel@tonic-gate 
3059c75cc341S 	status = do_subproc(cmdbuf);
30609acbbeafSnn35248 	if ((subproc_err =
30619acbbeafSnn35248 	    subproc_status(gettext("brand-specific installation"), status,
30629acbbeafSnn35248 	    B_FALSE)) != ZONE_SUBPROC_OK) {
3063ff17c8bfSgjelinek 		if (subproc_err == ZONE_SUBPROC_USAGE && !brand_help) {
3064ff17c8bfSgjelinek 			sub_usage(SHELP_INSTALL, CMD_INSTALL);
3065ff17c8bfSgjelinek 			zonecfg_release_lock_file(target_zone, lockfd);
3066ff17c8bfSgjelinek 			return (Z_ERR);
3067ff17c8bfSgjelinek 		}
3068ad02e316Sbatschul 		errno = subproc_err;
30699acbbeafSnn35248 		err = Z_ERR;
30707c478bd9Sstevel@tonic-gate 		goto done;
30719acbbeafSnn35248 	}
30727c478bd9Sstevel@tonic-gate 
3073ff17c8bfSgjelinek 	if (brand_help)
3074ff17c8bfSgjelinek 		return (Z_OK);
3075ff17c8bfSgjelinek 
30767c478bd9Sstevel@tonic-gate 	if ((err = zone_set_state(target_zone, ZONE_STATE_INSTALLED)) != Z_OK) {
30777c478bd9Sstevel@tonic-gate 		errno = err;
30787c478bd9Sstevel@tonic-gate 		zperror2(target_zone, gettext("could not set state"));
30797c478bd9Sstevel@tonic-gate 		goto done;
30807c478bd9Sstevel@tonic-gate 	}
30817c478bd9Sstevel@tonic-gate 
30821100f00dSgjelinek 	if (do_postinstall) {
30831100f00dSgjelinek 		status = do_subproc(postcmdbuf);
30841100f00dSgjelinek 
30851100f00dSgjelinek 		if ((subproc_err =
30861100f00dSgjelinek 		    subproc_status(gettext("brand-specific post-install"),
30871100f00dSgjelinek 		    status, B_FALSE)) != ZONE_SUBPROC_OK) {
3088ad02e316Sbatschul 			errno = subproc_err;
30891100f00dSgjelinek 			err = Z_ERR;
30901100f00dSgjelinek 			(void) zone_set_state(target_zone,
30911100f00dSgjelinek 			    ZONE_STATE_INCOMPLETE);
30921100f00dSgjelinek 		}
30931100f00dSgjelinek 	}
30941100f00dSgjelinek 
30957c478bd9Sstevel@tonic-gate done:
30969acbbeafSnn35248 	/*
3097ff17c8bfSgjelinek 	 * If the install script exited with ZONE_SUBPROC_NOTCOMPLETE, try to
3098ff17c8bfSgjelinek 	 * clean up the zone and leave the zone in the CONFIGURED state so that
3099ff17c8bfSgjelinek 	 * another install can be attempted without requiring an uninstall
3100ff17c8bfSgjelinek 	 * first.
31019acbbeafSnn35248 	 */
3102ff17c8bfSgjelinek 	if (subproc_err == ZONE_SUBPROC_NOTCOMPLETE) {
3103ad02e316Sbatschul 		int temp_err;
3104ad02e316Sbatschul 
3105ad02e316Sbatschul 		if ((temp_err = cleanup_zonepath(zonepath, B_FALSE)) != Z_OK) {
3106ad02e316Sbatschul 			errno = err = temp_err;
31079acbbeafSnn35248 			zperror2(target_zone,
31089acbbeafSnn35248 			    gettext("cleaning up zonepath failed"));
3109ad02e316Sbatschul 		} else if ((temp_err = zone_set_state(target_zone,
31109acbbeafSnn35248 		    ZONE_STATE_CONFIGURED)) != Z_OK) {
3111ad02e316Sbatschul 			errno = err = temp_err;
31129acbbeafSnn35248 			zperror2(target_zone, gettext("could not set state"));
31139acbbeafSnn35248 		}
31149acbbeafSnn35248 	}
31159acbbeafSnn35248 
3116ff17c8bfSgjelinek 	if (!brand_help)
3117ff17c8bfSgjelinek 		zonecfg_release_lock_file(target_zone, lockfd);
31187c478bd9Sstevel@tonic-gate 	return ((err == Z_OK) ? Z_OK : Z_ERR);
31197c478bd9Sstevel@tonic-gate }
31207c478bd9Sstevel@tonic-gate 
3121865e09a4Sgjelinek static void
warn_dev_match(zone_dochandle_t s_handle,char * source_zone,zone_dochandle_t t_handle,char * target_zone)3122865e09a4Sgjelinek warn_dev_match(zone_dochandle_t s_handle, char *source_zone,
3123865e09a4Sgjelinek 	zone_dochandle_t t_handle, char *target_zone)
3124865e09a4Sgjelinek {
3125865e09a4Sgjelinek 	int err;
3126865e09a4Sgjelinek 	struct zone_devtab s_devtab;
3127865e09a4Sgjelinek 	struct zone_devtab t_devtab;
3128865e09a4Sgjelinek 
3129865e09a4Sgjelinek 	if ((err = zonecfg_setdevent(t_handle)) != Z_OK) {
3130865e09a4Sgjelinek 		errno = err;
3131865e09a4Sgjelinek 		zperror2(target_zone, gettext("could not enumerate devices"));
3132865e09a4Sgjelinek 		return;
3133865e09a4Sgjelinek 	}
3134865e09a4Sgjelinek 
3135865e09a4Sgjelinek 	while (zonecfg_getdevent(t_handle, &t_devtab) == Z_OK) {
3136865e09a4Sgjelinek 		if ((err = zonecfg_setdevent(s_handle)) != Z_OK) {
3137865e09a4Sgjelinek 			errno = err;
3138865e09a4Sgjelinek 			zperror2(source_zone,
3139865e09a4Sgjelinek 			    gettext("could not enumerate devices"));
3140865e09a4Sgjelinek 			(void) zonecfg_enddevent(t_handle);
3141865e09a4Sgjelinek 			return;
3142865e09a4Sgjelinek 		}
3143865e09a4Sgjelinek 
3144865e09a4Sgjelinek 		while (zonecfg_getdevent(s_handle, &s_devtab) == Z_OK) {
3145865e09a4Sgjelinek 			/*
3146865e09a4Sgjelinek 			 * Use fnmatch to catch the case where wildcards
3147865e09a4Sgjelinek 			 * were used in one zone and the other has an
3148865e09a4Sgjelinek 			 * explicit entry (e.g. /dev/dsk/c0t0d0s6 vs.
3149865e09a4Sgjelinek 			 * /dev/\*dsk/c0t0d0s6).
3150865e09a4Sgjelinek 			 */
3151865e09a4Sgjelinek 			if (fnmatch(t_devtab.zone_dev_match,
3152865e09a4Sgjelinek 			    s_devtab.zone_dev_match, FNM_PATHNAME) == 0 ||
3153865e09a4Sgjelinek 			    fnmatch(s_devtab.zone_dev_match,
3154865e09a4Sgjelinek 			    t_devtab.zone_dev_match, FNM_PATHNAME) == 0) {
3155865e09a4Sgjelinek 				(void) fprintf(stderr,
3156865e09a4Sgjelinek 				    gettext("WARNING: device '%s' "
3157865e09a4Sgjelinek 				    "is configured in both zones.\n"),
3158865e09a4Sgjelinek 				    t_devtab.zone_dev_match);
3159865e09a4Sgjelinek 				break;
3160865e09a4Sgjelinek 			}
3161865e09a4Sgjelinek 		}
3162865e09a4Sgjelinek 		(void) zonecfg_enddevent(s_handle);
3163865e09a4Sgjelinek 	}
3164865e09a4Sgjelinek 
3165865e09a4Sgjelinek 	(void) zonecfg_enddevent(t_handle);
3166865e09a4Sgjelinek }
3167865e09a4Sgjelinek 
3168865e09a4Sgjelinek /*
3169865e09a4Sgjelinek  * Check if the specified mount option (opt) is contained within the
3170865e09a4Sgjelinek  * options string.
3171865e09a4Sgjelinek  */
3172865e09a4Sgjelinek static boolean_t
opt_match(char * opt,char * options)3173865e09a4Sgjelinek opt_match(char *opt, char *options)
3174865e09a4Sgjelinek {
3175865e09a4Sgjelinek 	char *p;
3176865e09a4Sgjelinek 	char *lastp;
3177865e09a4Sgjelinek 
3178865e09a4Sgjelinek 	if ((p = strtok_r(options, ",", &lastp)) != NULL) {
3179865e09a4Sgjelinek 		if (strcmp(p, opt) == 0)
3180865e09a4Sgjelinek 			return (B_TRUE);
3181865e09a4Sgjelinek 		while ((p = strtok_r(NULL, ",", &lastp)) != NULL) {
3182865e09a4Sgjelinek 			if (strcmp(p, opt) == 0)
3183865e09a4Sgjelinek 				return (B_TRUE);
3184865e09a4Sgjelinek 		}
3185865e09a4Sgjelinek 	}
3186865e09a4Sgjelinek 
3187865e09a4Sgjelinek 	return (B_FALSE);
3188865e09a4Sgjelinek }
3189865e09a4Sgjelinek 
31900b5de56dSgjelinek #define	RW_LOFS	"WARNING: read-write lofs file system on '%s' is configured " \
3191865e09a4Sgjelinek 	"in both zones.\n"
3192865e09a4Sgjelinek 
3193865e09a4Sgjelinek static void
print_fs_warnings(struct zone_fstab * s_fstab,struct zone_fstab * t_fstab)3194865e09a4Sgjelinek print_fs_warnings(struct zone_fstab *s_fstab, struct zone_fstab *t_fstab)
3195865e09a4Sgjelinek {
3196865e09a4Sgjelinek 	/*
3197865e09a4Sgjelinek 	 * It is ok to have shared lofs mounted fs but we want to warn if
3198865e09a4Sgjelinek 	 * either is rw since this will effect the other zone.
3199865e09a4Sgjelinek 	 */
3200865e09a4Sgjelinek 	if (strcmp(t_fstab->zone_fs_type, "lofs") == 0) {
3201865e09a4Sgjelinek 		zone_fsopt_t *optp;
3202865e09a4Sgjelinek 
3203865e09a4Sgjelinek 		/* The default is rw so no options means rw */
3204865e09a4Sgjelinek 		if (t_fstab->zone_fs_options == NULL ||
3205865e09a4Sgjelinek 		    s_fstab->zone_fs_options == NULL) {
3206865e09a4Sgjelinek 			(void) fprintf(stderr, gettext(RW_LOFS),
3207865e09a4Sgjelinek 			    t_fstab->zone_fs_special);
3208865e09a4Sgjelinek 			return;
3209865e09a4Sgjelinek 		}
3210865e09a4Sgjelinek 
3211865e09a4Sgjelinek 		for (optp = s_fstab->zone_fs_options; optp != NULL;
3212865e09a4Sgjelinek 		    optp = optp->zone_fsopt_next) {
3213865e09a4Sgjelinek 			if (opt_match("rw", optp->zone_fsopt_opt)) {
3214865e09a4Sgjelinek 				(void) fprintf(stderr, gettext(RW_LOFS),
3215865e09a4Sgjelinek 				    s_fstab->zone_fs_special);
3216865e09a4Sgjelinek 				return;
3217865e09a4Sgjelinek 			}
3218865e09a4Sgjelinek 		}
3219865e09a4Sgjelinek 
3220865e09a4Sgjelinek 		for (optp = t_fstab->zone_fs_options; optp != NULL;
3221865e09a4Sgjelinek 		    optp = optp->zone_fsopt_next) {
3222865e09a4Sgjelinek 			if (opt_match("rw", optp->zone_fsopt_opt)) {
3223865e09a4Sgjelinek 				(void) fprintf(stderr, gettext(RW_LOFS),
3224865e09a4Sgjelinek 				    t_fstab->zone_fs_special);
3225865e09a4Sgjelinek 				return;
3226865e09a4Sgjelinek 			}
3227865e09a4Sgjelinek 		}
3228865e09a4Sgjelinek 
3229865e09a4Sgjelinek 		return;
3230865e09a4Sgjelinek 	}
3231865e09a4Sgjelinek 
3232865e09a4Sgjelinek 	/*
3233865e09a4Sgjelinek 	 * TRANSLATION_NOTE
32340b5de56dSgjelinek 	 * The first variable is the file system type and the second is
32350b5de56dSgjelinek 	 * the file system special device.  For example,
32360b5de56dSgjelinek 	 * WARNING: ufs file system on '/dev/dsk/c0t0d0s0' ...
3237865e09a4Sgjelinek 	 */
32380b5de56dSgjelinek 	(void) fprintf(stderr, gettext("WARNING: %s file system on '%s' "
3239865e09a4Sgjelinek 	    "is configured in both zones.\n"), t_fstab->zone_fs_type,
3240865e09a4Sgjelinek 	    t_fstab->zone_fs_special);
3241865e09a4Sgjelinek }
3242865e09a4Sgjelinek 
3243865e09a4Sgjelinek static void
warn_fs_match(zone_dochandle_t s_handle,char * source_zone,zone_dochandle_t t_handle,char * target_zone)3244865e09a4Sgjelinek warn_fs_match(zone_dochandle_t s_handle, char *source_zone,
3245865e09a4Sgjelinek 	zone_dochandle_t t_handle, char *target_zone)
3246865e09a4Sgjelinek {
3247865e09a4Sgjelinek 	int err;
3248865e09a4Sgjelinek 	struct zone_fstab s_fstab;
3249865e09a4Sgjelinek 	struct zone_fstab t_fstab;
3250865e09a4Sgjelinek 
3251865e09a4Sgjelinek 	if ((err = zonecfg_setfsent(t_handle)) != Z_OK) {
3252865e09a4Sgjelinek 		errno = err;
3253865e09a4Sgjelinek 		zperror2(target_zone,
32540b5de56dSgjelinek 		    gettext("could not enumerate file systems"));
3255865e09a4Sgjelinek 		return;
3256865e09a4Sgjelinek 	}
3257865e09a4Sgjelinek 
3258865e09a4Sgjelinek 	while (zonecfg_getfsent(t_handle, &t_fstab) == Z_OK) {
3259865e09a4Sgjelinek 		if ((err = zonecfg_setfsent(s_handle)) != Z_OK) {
3260865e09a4Sgjelinek 			errno = err;
3261865e09a4Sgjelinek 			zperror2(source_zone,
32620b5de56dSgjelinek 			    gettext("could not enumerate file systems"));
3263865e09a4Sgjelinek 			(void) zonecfg_endfsent(t_handle);
3264865e09a4Sgjelinek 			return;
3265865e09a4Sgjelinek 		}
3266865e09a4Sgjelinek 
3267865e09a4Sgjelinek 		while (zonecfg_getfsent(s_handle, &s_fstab) == Z_OK) {
3268865e09a4Sgjelinek 			if (strcmp(t_fstab.zone_fs_special,
3269865e09a4Sgjelinek 			    s_fstab.zone_fs_special) == 0) {
3270865e09a4Sgjelinek 				print_fs_warnings(&s_fstab, &t_fstab);
3271865e09a4Sgjelinek 				break;
3272865e09a4Sgjelinek 			}
3273865e09a4Sgjelinek 		}
3274865e09a4Sgjelinek 		(void) zonecfg_endfsent(s_handle);
3275865e09a4Sgjelinek 	}
3276865e09a4Sgjelinek 
3277865e09a4Sgjelinek 	(void) zonecfg_endfsent(t_handle);
3278865e09a4Sgjelinek }
3279865e09a4Sgjelinek 
3280865e09a4Sgjelinek /*
3281865e09a4Sgjelinek  * We don't catch the case where you used the same IP address but
3282865e09a4Sgjelinek  * it is not an exact string match.  For example, 192.9.0.128 vs. 192.09.0.128.
3283865e09a4Sgjelinek  * However, we're not going to worry about that but we will check for
3284865e09a4Sgjelinek  * a possible netmask on one of the addresses (e.g. 10.0.0.1 and 10.0.0.1/24)
3285865e09a4Sgjelinek  * and handle that case as a match.
3286865e09a4Sgjelinek  */
3287865e09a4Sgjelinek static void
warn_ip_match(zone_dochandle_t s_handle,char * source_zone,zone_dochandle_t t_handle,char * target_zone)3288865e09a4Sgjelinek warn_ip_match(zone_dochandle_t s_handle, char *source_zone,
3289865e09a4Sgjelinek 	zone_dochandle_t t_handle, char *target_zone)
3290865e09a4Sgjelinek {
3291865e09a4Sgjelinek 	int err;
3292865e09a4Sgjelinek 	struct zone_nwiftab s_nwiftab;
3293865e09a4Sgjelinek 	struct zone_nwiftab t_nwiftab;
3294865e09a4Sgjelinek 
3295865e09a4Sgjelinek 	if ((err = zonecfg_setnwifent(t_handle)) != Z_OK) {
3296865e09a4Sgjelinek 		errno = err;
3297865e09a4Sgjelinek 		zperror2(target_zone,
3298865e09a4Sgjelinek 		    gettext("could not enumerate network interfaces"));
3299865e09a4Sgjelinek 		return;
3300865e09a4Sgjelinek 	}
3301865e09a4Sgjelinek 
3302865e09a4Sgjelinek 	while (zonecfg_getnwifent(t_handle, &t_nwiftab) == Z_OK) {
3303865e09a4Sgjelinek 		char *p;
3304865e09a4Sgjelinek 
3305865e09a4Sgjelinek 		/* remove an (optional) netmask from the address */
3306865e09a4Sgjelinek 		if ((p = strchr(t_nwiftab.zone_nwif_address, '/')) != NULL)
3307865e09a4Sgjelinek 			*p = '\0';
3308865e09a4Sgjelinek 
3309865e09a4Sgjelinek 		if ((err = zonecfg_setnwifent(s_handle)) != Z_OK) {
3310865e09a4Sgjelinek 			errno = err;
3311865e09a4Sgjelinek 			zperror2(source_zone,
3312865e09a4Sgjelinek 			    gettext("could not enumerate network interfaces"));
3313865e09a4Sgjelinek 			(void) zonecfg_endnwifent(t_handle);
3314865e09a4Sgjelinek 			return;
3315865e09a4Sgjelinek 		}
3316865e09a4Sgjelinek 
3317865e09a4Sgjelinek 		while (zonecfg_getnwifent(s_handle, &s_nwiftab) == Z_OK) {
3318865e09a4Sgjelinek 			/* remove an (optional) netmask from the address */
3319865e09a4Sgjelinek 			if ((p = strchr(s_nwiftab.zone_nwif_address, '/'))
3320865e09a4Sgjelinek 			    != NULL)
3321865e09a4Sgjelinek 				*p = '\0';
3322865e09a4Sgjelinek 
3323f4b3ec61Sdh155122 			/* For exclusive-IP zones, address is not specified. */
3324f4b3ec61Sdh155122 			if (strlen(s_nwiftab.zone_nwif_address) == 0)
3325f4b3ec61Sdh155122 				continue;
3326f4b3ec61Sdh155122 
3327865e09a4Sgjelinek 			if (strcmp(t_nwiftab.zone_nwif_address,
3328865e09a4Sgjelinek 			    s_nwiftab.zone_nwif_address) == 0) {
3329865e09a4Sgjelinek 				(void) fprintf(stderr,
3330865e09a4Sgjelinek 				    gettext("WARNING: network address '%s' "
3331865e09a4Sgjelinek 				    "is configured in both zones.\n"),
3332865e09a4Sgjelinek 				    t_nwiftab.zone_nwif_address);
3333865e09a4Sgjelinek 				break;
3334865e09a4Sgjelinek 			}
3335865e09a4Sgjelinek 		}
3336865e09a4Sgjelinek 		(void) zonecfg_endnwifent(s_handle);
3337865e09a4Sgjelinek 	}
3338865e09a4Sgjelinek 
3339865e09a4Sgjelinek 	(void) zonecfg_endnwifent(t_handle);
3340865e09a4Sgjelinek }
3341865e09a4Sgjelinek 
3342865e09a4Sgjelinek static void
warn_dataset_match(zone_dochandle_t s_handle,char * source,zone_dochandle_t t_handle,char * target)33439acbbeafSnn35248 warn_dataset_match(zone_dochandle_t s_handle, char *source,
33449acbbeafSnn35248 	zone_dochandle_t t_handle, char *target)
3345865e09a4Sgjelinek {
3346865e09a4Sgjelinek 	int err;
3347865e09a4Sgjelinek 	struct zone_dstab s_dstab;
3348865e09a4Sgjelinek 	struct zone_dstab t_dstab;
3349865e09a4Sgjelinek 
3350865e09a4Sgjelinek 	if ((err = zonecfg_setdsent(t_handle)) != Z_OK) {
3351865e09a4Sgjelinek 		errno = err;
33529acbbeafSnn35248 		zperror2(target, gettext("could not enumerate datasets"));
3353865e09a4Sgjelinek 		return;
3354865e09a4Sgjelinek 	}
3355865e09a4Sgjelinek 
3356865e09a4Sgjelinek 	while (zonecfg_getdsent(t_handle, &t_dstab) == Z_OK) {
3357865e09a4Sgjelinek 		if ((err = zonecfg_setdsent(s_handle)) != Z_OK) {
3358865e09a4Sgjelinek 			errno = err;
33599acbbeafSnn35248 			zperror2(source,
3360865e09a4Sgjelinek 			    gettext("could not enumerate datasets"));
3361865e09a4Sgjelinek 			(void) zonecfg_enddsent(t_handle);
3362865e09a4Sgjelinek 			return;
3363865e09a4Sgjelinek 		}
3364865e09a4Sgjelinek 
3365865e09a4Sgjelinek 		while (zonecfg_getdsent(s_handle, &s_dstab) == Z_OK) {
3366865e09a4Sgjelinek 			if (strcmp(t_dstab.zone_dataset_name,
3367865e09a4Sgjelinek 			    s_dstab.zone_dataset_name) == 0) {
33689acbbeafSnn35248 				target_zone = source;
33699acbbeafSnn35248 				zerror(gettext("WARNING: dataset '%s' "
3370865e09a4Sgjelinek 				    "is configured in both zones.\n"),
3371865e09a4Sgjelinek 				    t_dstab.zone_dataset_name);
3372865e09a4Sgjelinek 				break;
3373865e09a4Sgjelinek 			}
3374865e09a4Sgjelinek 		}
3375865e09a4Sgjelinek 		(void) zonecfg_enddsent(s_handle);
3376865e09a4Sgjelinek 	}
3377865e09a4Sgjelinek 
3378865e09a4Sgjelinek 	(void) zonecfg_enddsent(t_handle);
3379865e09a4Sgjelinek }
3380865e09a4Sgjelinek 
33819acbbeafSnn35248 /*
33829acbbeafSnn35248  * Check that the clone and its source have the same brand type.
33839acbbeafSnn35248  */
33849acbbeafSnn35248 static int
valid_brand_clone(char * source_zone,char * target_zone)33859acbbeafSnn35248 valid_brand_clone(char *source_zone, char *target_zone)
33869acbbeafSnn35248 {
3387123807fbSedp 	brand_handle_t bh;
33889acbbeafSnn35248 	char source_brand[MAXNAMELEN];
33899acbbeafSnn35248 
33909acbbeafSnn35248 	if ((zone_get_brand(source_zone, source_brand,
33919acbbeafSnn35248 	    sizeof (source_brand))) != Z_OK) {
33929acbbeafSnn35248 		(void) fprintf(stderr, "%s: zone '%s': %s\n",
33939acbbeafSnn35248 		    execname, source_zone, gettext("missing or invalid brand"));
33949acbbeafSnn35248 		return (Z_ERR);
33959acbbeafSnn35248 	}
33969acbbeafSnn35248 
3397ad02e316Sbatschul 	if (strcmp(source_brand, target_brand) != 0) {
33989acbbeafSnn35248 		(void) fprintf(stderr,
33999acbbeafSnn35248 		    gettext("%s: Zones '%s' and '%s' have different brand "
34009acbbeafSnn35248 		    "types.\n"), execname, source_zone, target_zone);
34019acbbeafSnn35248 		return (Z_ERR);
34029acbbeafSnn35248 	}
34039acbbeafSnn35248 
3404123807fbSedp 	if ((bh = brand_open(target_brand)) == NULL) {
34059acbbeafSnn35248 		zerror(gettext("missing or invalid brand"));
34069acbbeafSnn35248 		return (Z_ERR);
34079acbbeafSnn35248 	}
3408123807fbSedp 	brand_close(bh);
34099acbbeafSnn35248 	return (Z_OK);
34109acbbeafSnn35248 }
34119acbbeafSnn35248 
3412865e09a4Sgjelinek static int
validate_clone(char * source_zone,char * target_zone)3413865e09a4Sgjelinek validate_clone(char *source_zone, char *target_zone)
3414865e09a4Sgjelinek {
3415865e09a4Sgjelinek 	int err = Z_OK;
3416865e09a4Sgjelinek 	zone_dochandle_t s_handle;
3417865e09a4Sgjelinek 	zone_dochandle_t t_handle;
3418865e09a4Sgjelinek 
3419865e09a4Sgjelinek 	if ((t_handle = zonecfg_init_handle()) == NULL) {
3420865e09a4Sgjelinek 		zperror(cmd_to_str(CMD_CLONE), B_TRUE);
3421865e09a4Sgjelinek 		return (Z_ERR);
3422865e09a4Sgjelinek 	}
3423865e09a4Sgjelinek 	if ((err = zonecfg_get_handle(target_zone, t_handle)) != Z_OK) {
3424865e09a4Sgjelinek 		errno = err;
3425865e09a4Sgjelinek 		zperror(cmd_to_str(CMD_CLONE), B_TRUE);
3426865e09a4Sgjelinek 		zonecfg_fini_handle(t_handle);
3427865e09a4Sgjelinek 		return (Z_ERR);
3428865e09a4Sgjelinek 	}
3429865e09a4Sgjelinek 
3430865e09a4Sgjelinek 	if ((s_handle = zonecfg_init_handle()) == NULL) {
3431865e09a4Sgjelinek 		zperror(cmd_to_str(CMD_CLONE), B_TRUE);
3432865e09a4Sgjelinek 		zonecfg_fini_handle(t_handle);
3433865e09a4Sgjelinek 		return (Z_ERR);
3434865e09a4Sgjelinek 	}
3435865e09a4Sgjelinek 	if ((err = zonecfg_get_handle(source_zone, s_handle)) != Z_OK) {
3436865e09a4Sgjelinek 		errno = err;
3437865e09a4Sgjelinek 		zperror(cmd_to_str(CMD_CLONE), B_TRUE);
3438865e09a4Sgjelinek 		goto done;
3439865e09a4Sgjelinek 	}
3440865e09a4Sgjelinek 
34419acbbeafSnn35248 	/* verify new zone has same brand type */
34429acbbeafSnn35248 	err = valid_brand_clone(source_zone, target_zone);
34439acbbeafSnn35248 	if (err != Z_OK)
34449acbbeafSnn35248 		goto done;
34459acbbeafSnn35248 
3446865e09a4Sgjelinek 	/* warn about imported fs's which are the same */
3447865e09a4Sgjelinek 	warn_fs_match(s_handle, source_zone, t_handle, target_zone);
3448865e09a4Sgjelinek 
3449865e09a4Sgjelinek 	/* warn about imported IP addresses which are the same */
3450865e09a4Sgjelinek 	warn_ip_match(s_handle, source_zone, t_handle, target_zone);
3451865e09a4Sgjelinek 
3452865e09a4Sgjelinek 	/* warn about imported devices which are the same */
3453865e09a4Sgjelinek 	warn_dev_match(s_handle, source_zone, t_handle, target_zone);
3454865e09a4Sgjelinek 
3455865e09a4Sgjelinek 	/* warn about imported datasets which are the same */
3456865e09a4Sgjelinek 	warn_dataset_match(s_handle, source_zone, t_handle, target_zone);
3457865e09a4Sgjelinek 
3458865e09a4Sgjelinek done:
3459865e09a4Sgjelinek 	zonecfg_fini_handle(t_handle);
3460865e09a4Sgjelinek 	zonecfg_fini_handle(s_handle);
3461865e09a4Sgjelinek 
3462865e09a4Sgjelinek 	return ((err == Z_OK) ? Z_OK : Z_ERR);
3463865e09a4Sgjelinek }
3464865e09a4Sgjelinek 
3465865e09a4Sgjelinek static int
copy_zone(char * src,char * dst)3466865e09a4Sgjelinek copy_zone(char *src, char *dst)
3467865e09a4Sgjelinek {
3468865e09a4Sgjelinek 	boolean_t out_null = B_FALSE;
3469865e09a4Sgjelinek 	int status;
3470865e09a4Sgjelinek 	char *outfile;
3471865e09a4Sgjelinek 	char cmdbuf[MAXPATHLEN * 2 + 128];
3472865e09a4Sgjelinek 
3473865e09a4Sgjelinek 	if ((outfile = tempnam("/var/log", "zone")) == NULL) {
3474865e09a4Sgjelinek 		outfile = "/dev/null";
3475865e09a4Sgjelinek 		out_null = B_TRUE;
3476865e09a4Sgjelinek 	}
3477865e09a4Sgjelinek 
34780b5de56dSgjelinek 	/*
34790b5de56dSgjelinek 	 * Use find to get the list of files to copy.  We need to skip
34800b5de56dSgjelinek 	 * files of type "socket" since cpio can't handle those but that
34810b5de56dSgjelinek 	 * should be ok since the app will recreate the socket when it runs.
34820b5de56dSgjelinek 	 * We also need to filter out anything under the .zfs subdir.  Since
34830b5de56dSgjelinek 	 * find is running depth-first, we need the extra egrep to filter .zfs.
34840b5de56dSgjelinek 	 */
3485865e09a4Sgjelinek 	(void) snprintf(cmdbuf, sizeof (cmdbuf),
34860b5de56dSgjelinek 	    "cd %s && /usr/bin/find . -type s -prune -o -depth -print | "
348707b574eeSgjelinek 	    "/usr/bin/egrep -v '^\\./\\.zfs$|^\\./\\.zfs/' | "
3488865e09a4Sgjelinek 	    "/usr/bin/cpio -pdmuP@ %s > %s 2>&1",
3489865e09a4Sgjelinek 	    src, dst, outfile);
3490865e09a4Sgjelinek 
3491865e09a4Sgjelinek 	status = do_subproc(cmdbuf);
3492865e09a4Sgjelinek 
34939acbbeafSnn35248 	if (subproc_status("copy", status, B_TRUE) != ZONE_SUBPROC_OK) {
3494865e09a4Sgjelinek 		if (!out_null)
3495865e09a4Sgjelinek 			(void) fprintf(stderr, gettext("\nThe copy failed.\n"
3496865e09a4Sgjelinek 			    "More information can be found in %s\n"), outfile);
34979acbbeafSnn35248 		return (Z_ERR);
3498865e09a4Sgjelinek 	}
3499865e09a4Sgjelinek 
3500865e09a4Sgjelinek 	if (!out_null)
3501865e09a4Sgjelinek 		(void) unlink(outfile);
3502865e09a4Sgjelinek 
3503865e09a4Sgjelinek 	return (Z_OK);
3504865e09a4Sgjelinek }
3505865e09a4Sgjelinek 
3506865e09a4Sgjelinek /* ARGSUSED */
35070094b373Sjv227347 int
zfm_print(const struct mnttab * p,void * r)35080094b373Sjv227347 zfm_print(const struct mnttab *p, void *r) {
35090094b373Sjv227347 	zerror("  %s\n", p->mnt_mountp);
3510865e09a4Sgjelinek 	return (0);
3511865e09a4Sgjelinek }
3512865e09a4Sgjelinek 
35130b5de56dSgjelinek int
clone_copy(char * source_zonepath,char * zonepath)35140b5de56dSgjelinek clone_copy(char *source_zonepath, char *zonepath)
35150b5de56dSgjelinek {
35160b5de56dSgjelinek 	int err;
35170b5de56dSgjelinek 
35180b5de56dSgjelinek 	/* Don't clone the zone if anything is still mounted there */
35190b5de56dSgjelinek 	if (zonecfg_find_mounts(source_zonepath, NULL, NULL)) {
35200b5de56dSgjelinek 		zerror(gettext("These file systems are mounted on "
35210b5de56dSgjelinek 		    "subdirectories of %s.\n"), source_zonepath);
35220b5de56dSgjelinek 		(void) zonecfg_find_mounts(source_zonepath, zfm_print, NULL);
35230b5de56dSgjelinek 		return (Z_ERR);
35240b5de56dSgjelinek 	}
35250b5de56dSgjelinek 
35260b5de56dSgjelinek 	/*
35270b5de56dSgjelinek 	 * Attempt to create a ZFS fs for the zonepath.  As usual, we don't
35280b5de56dSgjelinek 	 * care if this works or not since we always have the default behavior
35290b5de56dSgjelinek 	 * of a simple directory for the zonepath.
35300b5de56dSgjelinek 	 */
35310b5de56dSgjelinek 	create_zfs_zonepath(zonepath);
35320b5de56dSgjelinek 
35330b5de56dSgjelinek 	(void) printf(gettext("Copying %s..."), source_zonepath);
35340b5de56dSgjelinek 	(void) fflush(stdout);
35350b5de56dSgjelinek 
35360b5de56dSgjelinek 	err = copy_zone(source_zonepath, zonepath);
35370b5de56dSgjelinek 
35380b5de56dSgjelinek 	(void) printf("\n");
35390b5de56dSgjelinek 
35400b5de56dSgjelinek 	return (err);
35410b5de56dSgjelinek }
35420b5de56dSgjelinek 
3543865e09a4Sgjelinek static int
clone_func(int argc,char * argv[])3544865e09a4Sgjelinek clone_func(int argc, char *argv[])
3545865e09a4Sgjelinek {
3546865e09a4Sgjelinek 	char *source_zone = NULL;
3547865e09a4Sgjelinek 	int lockfd;
3548865e09a4Sgjelinek 	int err, arg;
3549865e09a4Sgjelinek 	char zonepath[MAXPATHLEN];
3550865e09a4Sgjelinek 	char source_zonepath[MAXPATHLEN];
3551865e09a4Sgjelinek 	zone_state_t state;
3552865e09a4Sgjelinek 	zone_entry_t *zent;
35530b5de56dSgjelinek 	char *method = NULL;
35540b5de56dSgjelinek 	char *snapshot = NULL;
3555ff17c8bfSgjelinek 	char cmdbuf[MAXPATHLEN];
3556ff17c8bfSgjelinek 	char postcmdbuf[MAXPATHLEN];
3557ff17c8bfSgjelinek 	char presnapbuf[MAXPATHLEN];
3558ff17c8bfSgjelinek 	char postsnapbuf[MAXPATHLEN];
3559ff17c8bfSgjelinek 	char validsnapbuf[MAXPATHLEN];
3560ff17c8bfSgjelinek 	brand_handle_t bh = NULL;
3561ff17c8bfSgjelinek 	int status;
3562ff17c8bfSgjelinek 	boolean_t brand_help = B_FALSE;
3563865e09a4Sgjelinek 
3564865e09a4Sgjelinek 	if (zonecfg_in_alt_root()) {
3565865e09a4Sgjelinek 		zerror(gettext("cannot clone zone in alternate root"));
3566865e09a4Sgjelinek 		return (Z_ERR);
3567865e09a4Sgjelinek 	}
3568865e09a4Sgjelinek 
3569ff17c8bfSgjelinek 	/* Check the argv string for args we handle internally */
3570865e09a4Sgjelinek 	optind = 0;
3571ff17c8bfSgjelinek 	opterr = 0;
3572ff17c8bfSgjelinek 	while ((arg = getopt(argc, argv, "?m:s:")) != EOF) {
3573865e09a4Sgjelinek 		switch (arg) {
3574865e09a4Sgjelinek 		case '?':
3575ff17c8bfSgjelinek 			if (optopt == '?') {
3576865e09a4Sgjelinek 				sub_usage(SHELP_CLONE, CMD_CLONE);
3577ff17c8bfSgjelinek 				brand_help = B_TRUE;
3578ff17c8bfSgjelinek 			}
3579ff17c8bfSgjelinek 			/* Ignore unknown options - may be brand specific. */
3580ff17c8bfSgjelinek 			break;
3581865e09a4Sgjelinek 		case 'm':
3582865e09a4Sgjelinek 			method = optarg;
3583865e09a4Sgjelinek 			break;
35840b5de56dSgjelinek 		case 's':
35850b5de56dSgjelinek 			snapshot = optarg;
35860b5de56dSgjelinek 			break;
3587865e09a4Sgjelinek 		default:
3588ff17c8bfSgjelinek 			/* Ignore unknown options - may be brand specific. */
3589ff17c8bfSgjelinek 			break;
3590ff17c8bfSgjelinek 		}
3591ff17c8bfSgjelinek 	}
3592ff17c8bfSgjelinek 
3593ff17c8bfSgjelinek 	if (argc != (optind + 1)) {
3594865e09a4Sgjelinek 		sub_usage(SHELP_CLONE, CMD_CLONE);
3595865e09a4Sgjelinek 		return (Z_USAGE);
3596865e09a4Sgjelinek 	}
3597ff17c8bfSgjelinek 
3598865e09a4Sgjelinek 	source_zone = argv[optind];
3599ff17c8bfSgjelinek 
3600ff17c8bfSgjelinek 	if (!brand_help) {
3601ff17c8bfSgjelinek 		if (sanity_check(target_zone, CMD_CLONE, B_FALSE, B_TRUE,
3602ff17c8bfSgjelinek 		    B_FALSE) != Z_OK)
3603865e09a4Sgjelinek 			return (Z_ERR);
3604ce28b40eSzt129084 		if (verify_details(CMD_CLONE, argv) != Z_OK)
3605865e09a4Sgjelinek 			return (Z_ERR);
3606865e09a4Sgjelinek 
3607865e09a4Sgjelinek 		/*
3608865e09a4Sgjelinek 		 * We also need to do some extra validation on the source zone.
3609865e09a4Sgjelinek 		 */
3610865e09a4Sgjelinek 		if (strcmp(source_zone, GLOBAL_ZONENAME) == 0) {
3611ff17c8bfSgjelinek 			zerror(gettext("%s operation is invalid for the "
3612ff17c8bfSgjelinek 			    "global zone."), cmd_to_str(CMD_CLONE));
3613865e09a4Sgjelinek 			return (Z_ERR);
3614865e09a4Sgjelinek 		}
3615865e09a4Sgjelinek 
3616865e09a4Sgjelinek 		if (strncmp(source_zone, "SUNW", 4) == 0) {
3617ff17c8bfSgjelinek 			zerror(gettext("%s operation is invalid for zones "
3618ff17c8bfSgjelinek 			    "starting with SUNW."), cmd_to_str(CMD_CLONE));
3619865e09a4Sgjelinek 			return (Z_ERR);
3620865e09a4Sgjelinek 		}
3621865e09a4Sgjelinek 
3622cb8a054bSGlenn Faden 		if (auth_check(username, source_zone, SOURCE_ZONE) == Z_ERR) {
3623cb8a054bSGlenn Faden 			zerror(gettext("%s operation is invalid because "
3624cb8a054bSGlenn Faden 			    "user is not authorized to read source zone."),
3625cb8a054bSGlenn Faden 			    cmd_to_str(CMD_CLONE));
3626cb8a054bSGlenn Faden 			return (Z_ERR);
3627cb8a054bSGlenn Faden 		}
3628cb8a054bSGlenn Faden 
3629865e09a4Sgjelinek 		zent = lookup_running_zone(source_zone);
3630865e09a4Sgjelinek 		if (zent != NULL) {
3631865e09a4Sgjelinek 			/* check whether the zone is ready or running */
3632ff17c8bfSgjelinek 			if ((err = zone_get_state(zent->zname,
3633ff17c8bfSgjelinek 			    &zent->zstate_num)) != Z_OK) {
3634865e09a4Sgjelinek 				errno = err;
3635ff17c8bfSgjelinek 				zperror2(zent->zname, gettext("could not get "
3636ff17c8bfSgjelinek 				    "state"));
3637865e09a4Sgjelinek 				/* can't tell, so hedge */
3638865e09a4Sgjelinek 				zent->zstate_str = "ready/running";
3639865e09a4Sgjelinek 			} else {
3640ff17c8bfSgjelinek 				zent->zstate_str =
3641ff17c8bfSgjelinek 				    zone_state_str(zent->zstate_num);
3642865e09a4Sgjelinek 			}
3643865e09a4Sgjelinek 			zerror(gettext("%s operation is invalid for %s zones."),
3644865e09a4Sgjelinek 			    cmd_to_str(CMD_CLONE), zent->zstate_str);
3645865e09a4Sgjelinek 			return (Z_ERR);
3646865e09a4Sgjelinek 		}
3647865e09a4Sgjelinek 
3648865e09a4Sgjelinek 		if ((err = zone_get_state(source_zone, &state)) != Z_OK) {
3649865e09a4Sgjelinek 			errno = err;
3650865e09a4Sgjelinek 			zperror2(source_zone, gettext("could not get state"));
3651865e09a4Sgjelinek 			return (Z_ERR);
3652865e09a4Sgjelinek 		}
3653865e09a4Sgjelinek 		if (state != ZONE_STATE_INSTALLED) {
3654865e09a4Sgjelinek 			(void) fprintf(stderr,
3655865e09a4Sgjelinek 			    gettext("%s: zone %s is %s; %s is required.\n"),
3656865e09a4Sgjelinek 			    execname, source_zone, zone_state_str(state),
3657865e09a4Sgjelinek 			    zone_state_str(ZONE_STATE_INSTALLED));
3658865e09a4Sgjelinek 			return (Z_ERR);
3659865e09a4Sgjelinek 		}
3660865e09a4Sgjelinek 
3661865e09a4Sgjelinek 		/*
3662865e09a4Sgjelinek 		 * The source zone checks out ok, continue with the clone.
3663865e09a4Sgjelinek 		 */
3664865e09a4Sgjelinek 
3665865e09a4Sgjelinek 		if (validate_clone(source_zone, target_zone) != Z_OK)
3666865e09a4Sgjelinek 			return (Z_ERR);
3667865e09a4Sgjelinek 
3668ff17c8bfSgjelinek 		if (zonecfg_grab_lock_file(target_zone, &lockfd) != Z_OK) {
3669ff17c8bfSgjelinek 			zerror(gettext("another %s may have an operation in "
3670ff17c8bfSgjelinek 			    "progress."), "zoneadm");
3671865e09a4Sgjelinek 			return (Z_ERR);
3672865e09a4Sgjelinek 		}
3673ff17c8bfSgjelinek 	}
3674865e09a4Sgjelinek 
3675865e09a4Sgjelinek 	if ((err = zone_get_zonepath(source_zone, source_zonepath,
3676865e09a4Sgjelinek 	    sizeof (source_zonepath))) != Z_OK) {
3677865e09a4Sgjelinek 		errno = err;
3678865e09a4Sgjelinek 		zperror2(source_zone, gettext("could not get zone path"));
3679865e09a4Sgjelinek 		goto done;
3680865e09a4Sgjelinek 	}
3681865e09a4Sgjelinek 
3682865e09a4Sgjelinek 	if ((err = zone_get_zonepath(target_zone, zonepath, sizeof (zonepath)))
3683865e09a4Sgjelinek 	    != Z_OK) {
3684865e09a4Sgjelinek 		errno = err;
3685865e09a4Sgjelinek 		zperror2(target_zone, gettext("could not get zone path"));
3686865e09a4Sgjelinek 		goto done;
3687865e09a4Sgjelinek 	}
3688865e09a4Sgjelinek 
3689ff17c8bfSgjelinek 	/*
3690ff17c8bfSgjelinek 	 * Fetch the clone and postclone hooks from the brand configuration.
3691ff17c8bfSgjelinek 	 */
3692ff17c8bfSgjelinek 	if ((bh = brand_open(target_brand)) == NULL) {
3693ff17c8bfSgjelinek 		zerror(gettext("missing or invalid brand"));
3694ff17c8bfSgjelinek 		err = Z_ERR;
3695ff17c8bfSgjelinek 		goto done;
3696ff17c8bfSgjelinek 	}
3697ff17c8bfSgjelinek 
3698ff17c8bfSgjelinek 	if (get_hook(bh, cmdbuf, sizeof (cmdbuf), brand_get_clone, target_zone,
3699ff17c8bfSgjelinek 	    zonepath) != Z_OK) {
3700ff17c8bfSgjelinek 		zerror("invalid brand configuration: missing clone resource");
3701ff17c8bfSgjelinek 		brand_close(bh);
3702ff17c8bfSgjelinek 		err = Z_ERR;
3703ff17c8bfSgjelinek 		goto done;
3704ff17c8bfSgjelinek 	}
3705ff17c8bfSgjelinek 
3706ff17c8bfSgjelinek 	if (get_hook(bh, postcmdbuf, sizeof (postcmdbuf), brand_get_postclone,
3707ff17c8bfSgjelinek 	    target_zone, zonepath) != Z_OK) {
3708ff17c8bfSgjelinek 		zerror("invalid brand configuration: missing postclone "
3709ff17c8bfSgjelinek 		    "resource");
3710ff17c8bfSgjelinek 		brand_close(bh);
3711ff17c8bfSgjelinek 		err = Z_ERR;
3712ff17c8bfSgjelinek 		goto done;
3713ff17c8bfSgjelinek 	}
3714ff17c8bfSgjelinek 
3715ff17c8bfSgjelinek 	if (get_hook(bh, presnapbuf, sizeof (presnapbuf), brand_get_presnap,
3716ff17c8bfSgjelinek 	    source_zone, source_zonepath) != Z_OK) {
3717ff17c8bfSgjelinek 		zerror("invalid brand configuration: missing presnap "
3718ff17c8bfSgjelinek 		    "resource");
3719ff17c8bfSgjelinek 		brand_close(bh);
3720ff17c8bfSgjelinek 		err = Z_ERR;
3721ff17c8bfSgjelinek 		goto done;
3722ff17c8bfSgjelinek 	}
3723ff17c8bfSgjelinek 
3724ff17c8bfSgjelinek 	if (get_hook(bh, postsnapbuf, sizeof (postsnapbuf), brand_get_postsnap,
3725ff17c8bfSgjelinek 	    source_zone, source_zonepath) != Z_OK) {
3726ff17c8bfSgjelinek 		zerror("invalid brand configuration: missing postsnap "
3727ff17c8bfSgjelinek 		    "resource");
3728ff17c8bfSgjelinek 		brand_close(bh);
3729ff17c8bfSgjelinek 		err = Z_ERR;
3730ff17c8bfSgjelinek 		goto done;
3731ff17c8bfSgjelinek 	}
3732ff17c8bfSgjelinek 
3733ff17c8bfSgjelinek 	if (get_hook(bh, validsnapbuf, sizeof (validsnapbuf),
3734ff17c8bfSgjelinek 	    brand_get_validatesnap, target_zone, zonepath) != Z_OK) {
3735ff17c8bfSgjelinek 		zerror("invalid brand configuration: missing validatesnap "
3736ff17c8bfSgjelinek 		    "resource");
3737ff17c8bfSgjelinek 		brand_close(bh);
3738ff17c8bfSgjelinek 		err = Z_ERR;
3739ff17c8bfSgjelinek 		goto done;
3740ff17c8bfSgjelinek 	}
3741ff17c8bfSgjelinek 	brand_close(bh);
3742ff17c8bfSgjelinek 
3743ff17c8bfSgjelinek 	/* Append all options to clone hook. */
3744ff17c8bfSgjelinek 	if (addoptions(cmdbuf, argv, sizeof (cmdbuf)) != Z_OK) {
3745ff17c8bfSgjelinek 		err = Z_ERR;
3746ff17c8bfSgjelinek 		goto done;
3747ff17c8bfSgjelinek 	}
3748ff17c8bfSgjelinek 
3749ff17c8bfSgjelinek 	/* Append all options to postclone hook. */
3750ff17c8bfSgjelinek 	if (addoptions(postcmdbuf, argv, sizeof (postcmdbuf)) != Z_OK) {
3751ff17c8bfSgjelinek 		err = Z_ERR;
3752ff17c8bfSgjelinek 		goto done;
3753ff17c8bfSgjelinek 	}
3754ff17c8bfSgjelinek 
3755ff17c8bfSgjelinek 	if (!brand_help) {
3756865e09a4Sgjelinek 		if ((err = zone_set_state(target_zone, ZONE_STATE_INCOMPLETE))
3757865e09a4Sgjelinek 		    != Z_OK) {
3758865e09a4Sgjelinek 			errno = err;
3759865e09a4Sgjelinek 			zperror2(target_zone, gettext("could not set state"));
3760865e09a4Sgjelinek 			goto done;
3761865e09a4Sgjelinek 		}
3762ff17c8bfSgjelinek 	}
3763ff17c8bfSgjelinek 
3764ff17c8bfSgjelinek 	/*
3765ff17c8bfSgjelinek 	 * The clone hook is optional.  If it exists, use the hook for
3766ff17c8bfSgjelinek 	 * cloning, otherwise use the built-in clone support
3767ff17c8bfSgjelinek 	 */
3768ff17c8bfSgjelinek 	if (cmdbuf[0] != '\0') {
3769ff17c8bfSgjelinek 		/* Run the clone hook */
3770c75cc341S 		status = do_subproc(cmdbuf);
3771ff17c8bfSgjelinek 		if ((status = subproc_status(gettext("brand-specific clone"),
3772ff17c8bfSgjelinek 		    status, B_FALSE)) != ZONE_SUBPROC_OK) {
3773ff17c8bfSgjelinek 			if (status == ZONE_SUBPROC_USAGE && !brand_help)
3774ff17c8bfSgjelinek 				sub_usage(SHELP_CLONE, CMD_CLONE);
3775ff17c8bfSgjelinek 			err = Z_ERR;
3776ff17c8bfSgjelinek 			goto done;
3777ff17c8bfSgjelinek 		}
3778ff17c8bfSgjelinek 
3779ff17c8bfSgjelinek 		if (brand_help)
3780ff17c8bfSgjelinek 			return (Z_OK);
3781ff17c8bfSgjelinek 
3782ff17c8bfSgjelinek 	} else {
3783ff17c8bfSgjelinek 		/* If just help, we're done since there is no brand help. */
3784ff17c8bfSgjelinek 		if (brand_help)
3785ff17c8bfSgjelinek 			return (Z_OK);
3786ff17c8bfSgjelinek 
3787ff17c8bfSgjelinek 		/* Run the built-in clone support. */
3788ff17c8bfSgjelinek 
3789ff17c8bfSgjelinek 		/* The only explicit built-in method is "copy". */
3790ff17c8bfSgjelinek 		if (method != NULL && strcmp(method, "copy") != 0) {
3791ff17c8bfSgjelinek 			sub_usage(SHELP_CLONE, CMD_CLONE);
3792ff17c8bfSgjelinek 			err = Z_USAGE;
3793ff17c8bfSgjelinek 			goto done;
3794ff17c8bfSgjelinek 		}
3795865e09a4Sgjelinek 
37960b5de56dSgjelinek 		if (snapshot != NULL) {
3797ff17c8bfSgjelinek 			err = clone_snapshot_zfs(snapshot, zonepath,
3798ff17c8bfSgjelinek 			    validsnapbuf);
37990b5de56dSgjelinek 		} else {
38000b5de56dSgjelinek 			/*
3801ff17c8bfSgjelinek 			 * We always copy the clone unless the source is ZFS
3802ff17c8bfSgjelinek 			 * and a ZFS clone worked.  We fallback to copying if
3803ff17c8bfSgjelinek 			 * the ZFS clone fails for some reason.
38040b5de56dSgjelinek 			 */
38050b5de56dSgjelinek 			err = Z_ERR;
38060b5de56dSgjelinek 			if (method == NULL && is_zonepath_zfs(source_zonepath))
3807ff17c8bfSgjelinek 				err = clone_zfs(source_zonepath, zonepath,
3808ff17c8bfSgjelinek 				    presnapbuf, postsnapbuf);
3809865e09a4Sgjelinek 
38105cd08338Sgjelinek 			if (err != Z_OK)
38110b5de56dSgjelinek 				err = clone_copy(source_zonepath, zonepath);
38120b5de56dSgjelinek 		}
3813ff17c8bfSgjelinek 	}
3814865e09a4Sgjelinek 
3815ff17c8bfSgjelinek 	if (err == Z_OK && postcmdbuf[0] != '\0') {
3816ff17c8bfSgjelinek 		status = do_subproc(postcmdbuf);
3817ff17c8bfSgjelinek 		if ((err = subproc_status("postclone", status, B_FALSE))
3818ff17c8bfSgjelinek 		    != ZONE_SUBPROC_OK) {
3819ff17c8bfSgjelinek 			zerror(gettext("post-clone configuration failed."));
3820ff17c8bfSgjelinek 			err = Z_ERR;
3821ff17c8bfSgjelinek 		}
3822ff17c8bfSgjelinek 	}
3823865e09a4Sgjelinek 
3824865e09a4Sgjelinek done:
38259acbbeafSnn35248 	/*
38269acbbeafSnn35248 	 * If everything went well, we mark the zone as installed.
38279acbbeafSnn35248 	 */
38289acbbeafSnn35248 	if (err == Z_OK) {
38299acbbeafSnn35248 		err = zone_set_state(target_zone, ZONE_STATE_INSTALLED);
38309acbbeafSnn35248 		if (err != Z_OK) {
38319acbbeafSnn35248 			errno = err;
38329acbbeafSnn35248 			zperror2(target_zone, gettext("could not set state"));
38339acbbeafSnn35248 		}
38349acbbeafSnn35248 	}
3835ff17c8bfSgjelinek 	if (!brand_help)
3836ff17c8bfSgjelinek 		zonecfg_release_lock_file(target_zone, lockfd);
3837865e09a4Sgjelinek 	return ((err == Z_OK) ? Z_OK : Z_ERR);
3838865e09a4Sgjelinek }
3839865e09a4Sgjelinek 
384007b574eeSgjelinek /*
38410b5de56dSgjelinek  * Used when removing a zonepath after uninstalling or cleaning up after
38420b5de56dSgjelinek  * the move subcommand.  This handles a zonepath that has non-standard
38430b5de56dSgjelinek  * contents so that we will only cleanup the stuff we know about and leave
38440b5de56dSgjelinek  * any user data alone.
384507b574eeSgjelinek  *
38460b5de56dSgjelinek  * If the "all" parameter is true then we should remove the whole zonepath
38470b5de56dSgjelinek  * even if it has non-standard files/directories in it.  This can be used when
38480b5de56dSgjelinek  * we need to cleanup after moving the zonepath across file systems.
38490b5de56dSgjelinek  *
38500b5de56dSgjelinek  * We "exec" the RMCOMMAND so that the returned status is that of RMCOMMAND
38510b5de56dSgjelinek  * and not the shell.
385207b574eeSgjelinek  */
385307b574eeSgjelinek static int
cleanup_zonepath(char * zonepath,boolean_t all)38540b5de56dSgjelinek cleanup_zonepath(char *zonepath, boolean_t all)
385507b574eeSgjelinek {
385607b574eeSgjelinek 	int		status;
38570b5de56dSgjelinek 	int		i;
38580b5de56dSgjelinek 	boolean_t	non_std = B_FALSE;
38590b5de56dSgjelinek 	struct dirent	*dp;
38600b5de56dSgjelinek 	DIR		*dirp;
3861d9e728a2Sgjelinek 			/*
3862d9e728a2Sgjelinek 			 * The SUNWattached.xml file is expected since it might
3863d9e728a2Sgjelinek 			 * exist if the zone was force-attached after a
3864d9e728a2Sgjelinek 			 * migration.
3865d9e728a2Sgjelinek 			 */
3866d9e728a2Sgjelinek 	char		*std_entries[] = {"dev", "lu", "root",
3867d9e728a2Sgjelinek 			    "SUNWattached.xml", NULL};
38680b5de56dSgjelinek 			/* (MAXPATHLEN * 3) is for the 3 std_entries dirs */
38690b5de56dSgjelinek 	char		cmdbuf[sizeof (RMCOMMAND) + (MAXPATHLEN * 3) + 64];
387007b574eeSgjelinek 
387107b574eeSgjelinek 	/*
38720b5de56dSgjelinek 	 * We shouldn't need these checks but lets be paranoid since we
38730b5de56dSgjelinek 	 * could blow away the whole system here if we got the wrong zonepath.
387407b574eeSgjelinek 	 */
38750b5de56dSgjelinek 	if (*zonepath == NULL || strcmp(zonepath, "/") == 0) {
38760b5de56dSgjelinek 		(void) fprintf(stderr, "invalid zonepath '%s'\n", zonepath);
38770b5de56dSgjelinek 		return (Z_INVAL);
38780b5de56dSgjelinek 	}
38790b5de56dSgjelinek 
388007b574eeSgjelinek 	/*
38810b5de56dSgjelinek 	 * If the dirpath is already gone (maybe it was manually removed) then
38820b5de56dSgjelinek 	 * we just return Z_OK so that the cleanup is successful.
388307b574eeSgjelinek 	 */
38840b5de56dSgjelinek 	if ((dirp = opendir(zonepath)) == NULL)
38850b5de56dSgjelinek 		return (Z_OK);
38860b5de56dSgjelinek 
38870b5de56dSgjelinek 	/*
38880b5de56dSgjelinek 	 * Look through the zonepath directory to see if there are any
38890b5de56dSgjelinek 	 * non-standard files/dirs.  Also skip .zfs since that might be
38900b5de56dSgjelinek 	 * there but we'll handle ZFS file systems as a special case.
38910b5de56dSgjelinek 	 */
38920b5de56dSgjelinek 	while ((dp = readdir(dirp)) != NULL) {
38930b5de56dSgjelinek 		if (strcmp(dp->d_name, ".") == 0 ||
38940b5de56dSgjelinek 		    strcmp(dp->d_name, "..") == 0 ||
38950b5de56dSgjelinek 		    strcmp(dp->d_name, ".zfs") == 0)
38960b5de56dSgjelinek 			continue;
38970b5de56dSgjelinek 
38980b5de56dSgjelinek 		for (i = 0; std_entries[i] != NULL; i++)
38990b5de56dSgjelinek 			if (strcmp(dp->d_name, std_entries[i]) == 0)
39000b5de56dSgjelinek 				break;
39010b5de56dSgjelinek 
39020b5de56dSgjelinek 		if (std_entries[i] == NULL)
39030b5de56dSgjelinek 			non_std = B_TRUE;
39040b5de56dSgjelinek 	}
39050b5de56dSgjelinek 	(void) closedir(dirp);
39060b5de56dSgjelinek 
39070b5de56dSgjelinek 	if (!all && non_std) {
39080b5de56dSgjelinek 		/*
39090b5de56dSgjelinek 		 * There are extra, non-standard directories/files in the
39100b5de56dSgjelinek 		 * zonepath so we don't want to remove the zonepath.  We
39110b5de56dSgjelinek 		 * just want to remove the standard directories and leave
39120b5de56dSgjelinek 		 * the user data alone.
39130b5de56dSgjelinek 		 */
39140b5de56dSgjelinek 		(void) snprintf(cmdbuf, sizeof (cmdbuf), "exec " RMCOMMAND);
39150b5de56dSgjelinek 
39160b5de56dSgjelinek 		for (i = 0; std_entries[i] != NULL; i++) {
39170b5de56dSgjelinek 			char tmpbuf[MAXPATHLEN];
39180b5de56dSgjelinek 
39190b5de56dSgjelinek 			if (snprintf(tmpbuf, sizeof (tmpbuf), " %s/%s",
39200b5de56dSgjelinek 			    zonepath, std_entries[i]) >= sizeof (tmpbuf) ||
39210b5de56dSgjelinek 			    strlcat(cmdbuf, tmpbuf, sizeof (cmdbuf)) >=
39220b5de56dSgjelinek 			    sizeof (cmdbuf)) {
39230b5de56dSgjelinek 				(void) fprintf(stderr,
39240b5de56dSgjelinek 				    gettext("path is too long\n"));
39250b5de56dSgjelinek 				return (Z_INVAL);
39260b5de56dSgjelinek 			}
392707b574eeSgjelinek 		}
392807b574eeSgjelinek 
392907b574eeSgjelinek 		status = do_subproc(cmdbuf);
393007b574eeSgjelinek 
39310b5de56dSgjelinek 		(void) fprintf(stderr, gettext("WARNING: Unable to completely "
39320b5de56dSgjelinek 		    "remove %s\nbecause it contains additional user data.  "
39330b5de56dSgjelinek 		    "Only the standard directory\nentries have been "
39340b5de56dSgjelinek 		    "removed.\n"),
39350b5de56dSgjelinek 		    zonepath);
393607b574eeSgjelinek 
39379acbbeafSnn35248 		return ((subproc_status(RMCOMMAND, status, B_TRUE) ==
39389acbbeafSnn35248 		    ZONE_SUBPROC_OK) ? Z_OK : Z_ERR);
39390b5de56dSgjelinek 	}
39400b5de56dSgjelinek 
39410b5de56dSgjelinek 	/*
39420b5de56dSgjelinek 	 * There is nothing unexpected in the zonepath, try to get rid of the
39430b5de56dSgjelinek 	 * whole zonepath directory.
39440b5de56dSgjelinek 	 *
39450b5de56dSgjelinek 	 * If the zonepath is its own zfs file system, try to destroy the
39460b5de56dSgjelinek 	 * file system.  If that fails for some reason (e.g. it has clones)
39470b5de56dSgjelinek 	 * then we'll just remove the contents of the zonepath.
39480b5de56dSgjelinek 	 */
39490b5de56dSgjelinek 	if (is_zonepath_zfs(zonepath)) {
39500b5de56dSgjelinek 		if (destroy_zfs(zonepath) == Z_OK)
39510b5de56dSgjelinek 			return (Z_OK);
39520b5de56dSgjelinek 		(void) snprintf(cmdbuf, sizeof (cmdbuf), "exec " RMCOMMAND
39530b5de56dSgjelinek 		    " %s/*", zonepath);
39540b5de56dSgjelinek 		status = do_subproc(cmdbuf);
39559acbbeafSnn35248 		return ((subproc_status(RMCOMMAND, status, B_TRUE) ==
39569acbbeafSnn35248 		    ZONE_SUBPROC_OK) ? Z_OK : Z_ERR);
39570b5de56dSgjelinek 	}
39580b5de56dSgjelinek 
39590b5de56dSgjelinek 	(void) snprintf(cmdbuf, sizeof (cmdbuf), "exec " RMCOMMAND " %s",
39600b5de56dSgjelinek 	    zonepath);
39610b5de56dSgjelinek 	status = do_subproc(cmdbuf);
39629acbbeafSnn35248 
39639acbbeafSnn35248 	return ((subproc_status(RMCOMMAND, status, B_TRUE) == ZONE_SUBPROC_OK)
39649acbbeafSnn35248 	    ? Z_OK : Z_ERR);
396507b574eeSgjelinek }
396607b574eeSgjelinek 
3967865e09a4Sgjelinek static int
move_func(int argc,char * argv[])3968865e09a4Sgjelinek move_func(int argc, char *argv[])
3969865e09a4Sgjelinek {
3970865e09a4Sgjelinek 	char *new_zonepath = NULL;
3971865e09a4Sgjelinek 	int lockfd;
3972865e09a4Sgjelinek 	int err, arg;
3973865e09a4Sgjelinek 	char zonepath[MAXPATHLEN];
3974865e09a4Sgjelinek 	zone_dochandle_t handle;
3975865e09a4Sgjelinek 	boolean_t fast;
39760b5de56dSgjelinek 	boolean_t is_zfs = B_FALSE;
39770094b373Sjv227347 	boolean_t root_fs_mounted = B_FALSE;
39780b5de56dSgjelinek 	struct dirent *dp;
39790b5de56dSgjelinek 	DIR *dirp;
39800b5de56dSgjelinek 	boolean_t empty = B_TRUE;
3981865e09a4Sgjelinek 	boolean_t revert;
3982865e09a4Sgjelinek 	struct stat zonepath_buf;
3983865e09a4Sgjelinek 	struct stat new_zonepath_buf;
39840094b373Sjv227347 	zone_mounts_t mounts;
3985865e09a4Sgjelinek 
3986865e09a4Sgjelinek 	if (zonecfg_in_alt_root()) {
3987865e09a4Sgjelinek 		zerror(gettext("cannot move zone in alternate root"));
3988865e09a4Sgjelinek 		return (Z_ERR);
3989865e09a4Sgjelinek 	}
3990865e09a4Sgjelinek 
3991865e09a4Sgjelinek 	optind = 0;
3992865e09a4Sgjelinek 	if ((arg = getopt(argc, argv, "?")) != EOF) {
3993865e09a4Sgjelinek 		switch (arg) {
3994865e09a4Sgjelinek 		case '?':
3995865e09a4Sgjelinek 			sub_usage(SHELP_MOVE, CMD_MOVE);
3996865e09a4Sgjelinek 			return (optopt == '?' ? Z_OK : Z_USAGE);
3997865e09a4Sgjelinek 		default:
3998865e09a4Sgjelinek 			sub_usage(SHELP_MOVE, CMD_MOVE);
3999865e09a4Sgjelinek 			return (Z_USAGE);
4000865e09a4Sgjelinek 		}
4001865e09a4Sgjelinek 	}
4002865e09a4Sgjelinek 	if (argc != (optind + 1)) {
4003865e09a4Sgjelinek 		sub_usage(SHELP_MOVE, CMD_MOVE);
4004865e09a4Sgjelinek 		return (Z_USAGE);
4005865e09a4Sgjelinek 	}
4006865e09a4Sgjelinek 	new_zonepath = argv[optind];
40079acbbeafSnn35248 	if (sanity_check(target_zone, CMD_MOVE, B_FALSE, B_TRUE, B_FALSE)
40089acbbeafSnn35248 	    != Z_OK)
4009865e09a4Sgjelinek 		return (Z_ERR);
4010ce28b40eSzt129084 	if (verify_details(CMD_MOVE, argv) != Z_OK)
4011865e09a4Sgjelinek 		return (Z_ERR);
4012865e09a4Sgjelinek 
4013865e09a4Sgjelinek 	/*
4014865e09a4Sgjelinek 	 * Check out the new zonepath.  This has the side effect of creating
4015865e09a4Sgjelinek 	 * a directory for the new zonepath.  We depend on this later when we
40160b5de56dSgjelinek 	 * stat to see if we are doing a cross file system move or not.
4017865e09a4Sgjelinek 	 */
4018865e09a4Sgjelinek 	if (validate_zonepath(new_zonepath, CMD_MOVE) != Z_OK)
4019865e09a4Sgjelinek 		return (Z_ERR);
4020865e09a4Sgjelinek 
4021865e09a4Sgjelinek 	if ((err = zone_get_zonepath(target_zone, zonepath, sizeof (zonepath)))
4022865e09a4Sgjelinek 	    != Z_OK) {
4023865e09a4Sgjelinek 		errno = err;
4024865e09a4Sgjelinek 		zperror2(target_zone, gettext("could not get zone path"));
4025865e09a4Sgjelinek 		return (Z_ERR);
4026865e09a4Sgjelinek 	}
4027865e09a4Sgjelinek 
4028865e09a4Sgjelinek 	if (stat(zonepath, &zonepath_buf) == -1) {
4029865e09a4Sgjelinek 		zperror(gettext("could not stat zone path"), B_FALSE);
4030865e09a4Sgjelinek 		return (Z_ERR);
4031865e09a4Sgjelinek 	}
4032865e09a4Sgjelinek 
4033865e09a4Sgjelinek 	if (stat(new_zonepath, &new_zonepath_buf) == -1) {
4034865e09a4Sgjelinek 		zperror(gettext("could not stat new zone path"), B_FALSE);
4035865e09a4Sgjelinek 		return (Z_ERR);
4036865e09a4Sgjelinek 	}
4037865e09a4Sgjelinek 
40380b5de56dSgjelinek 	/*
40390b5de56dSgjelinek 	 * Check if the destination directory is empty.
40400b5de56dSgjelinek 	 */
40410b5de56dSgjelinek 	if ((dirp = opendir(new_zonepath)) == NULL) {
40420b5de56dSgjelinek 		zperror(gettext("could not open new zone path"), B_FALSE);
40430b5de56dSgjelinek 		return (Z_ERR);
40440b5de56dSgjelinek 	}
40450b5de56dSgjelinek 	while ((dp = readdir(dirp)) != (struct dirent *)0) {
40460b5de56dSgjelinek 		if (strcmp(dp->d_name, ".") == 0 ||
40470b5de56dSgjelinek 		    strcmp(dp->d_name, "..") == 0)
40480b5de56dSgjelinek 			continue;
40490b5de56dSgjelinek 		empty = B_FALSE;
40500b5de56dSgjelinek 		break;
40510b5de56dSgjelinek 	}
40520b5de56dSgjelinek 	(void) closedir(dirp);
40530b5de56dSgjelinek 
40540b5de56dSgjelinek 	/* Error if there is anything in the destination directory. */
40550b5de56dSgjelinek 	if (!empty) {
40560b5de56dSgjelinek 		(void) fprintf(stderr, gettext("could not move zone to %s: "
40570b5de56dSgjelinek 		    "directory not empty\n"), new_zonepath);
40580b5de56dSgjelinek 		return (Z_ERR);
40590b5de56dSgjelinek 	}
40600b5de56dSgjelinek 
40610094b373Sjv227347 	/*
40620094b373Sjv227347 	 * Collect information about mounts within the zone's zonepath.
40630094b373Sjv227347 	 * Overlay mounts on the zone's root directory are erroneous.
40640094b373Sjv227347 	 * Bail if we encounter any unexpected mounts.
40650094b373Sjv227347 	 */
40660094b373Sjv227347 	if (zone_mounts_init(&mounts, zonepath) != 0)
4067865e09a4Sgjelinek 		return (Z_ERR);
40680094b373Sjv227347 	if (mounts.num_root_overlay_mounts != 0) {
40690094b373Sjv227347 		zerror(gettext("%d overlay mount(s) detected on %s/root."),
40700094b373Sjv227347 		    mounts.num_root_overlay_mounts, zonepath);
40710094b373Sjv227347 		goto err_and_mounts_destroy;
4072865e09a4Sgjelinek 	}
40730094b373Sjv227347 	if (mounts.num_unexpected_mounts != 0)
40740094b373Sjv227347 		goto err_and_mounts_destroy;
4075865e09a4Sgjelinek 
4076865e09a4Sgjelinek 	/*
4077865e09a4Sgjelinek 	 * Check if we are moving in the same file system and can do a fast
4078865e09a4Sgjelinek 	 * move or if we are crossing file systems and have to copy the data.
4079865e09a4Sgjelinek 	 */
4080865e09a4Sgjelinek 	fast = (zonepath_buf.st_dev == new_zonepath_buf.st_dev);
4081865e09a4Sgjelinek 
4082865e09a4Sgjelinek 	if ((handle = zonecfg_init_handle()) == NULL) {
4083865e09a4Sgjelinek 		zperror(cmd_to_str(CMD_MOVE), B_TRUE);
40840094b373Sjv227347 		goto err_and_mounts_destroy;
4085865e09a4Sgjelinek 	}
4086865e09a4Sgjelinek 
4087865e09a4Sgjelinek 	if ((err = zonecfg_get_handle(target_zone, handle)) != Z_OK) {
4088865e09a4Sgjelinek 		errno = err;
4089865e09a4Sgjelinek 		zperror(cmd_to_str(CMD_MOVE), B_TRUE);
40900094b373Sjv227347 		goto err_and_fini_handle;
4091865e09a4Sgjelinek 	}
4092865e09a4Sgjelinek 
4093ff17c8bfSgjelinek 	if (zonecfg_grab_lock_file(target_zone, &lockfd) != Z_OK) {
4094865e09a4Sgjelinek 		zerror(gettext("another %s may have an operation in progress."),
4095865e09a4Sgjelinek 		    "zoneadm");
40960094b373Sjv227347 		goto err_and_fini_handle;
4097865e09a4Sgjelinek 	}
4098865e09a4Sgjelinek 
4099865e09a4Sgjelinek 	/*
41000094b373Sjv227347 	 * Unmount the zone's root filesystem before we move the zone's
41010094b373Sjv227347 	 * zonepath.
41020094b373Sjv227347 	 */
41030094b373Sjv227347 	if (zone_unmount_rootfs(&mounts, zonepath, B_FALSE) != 0)
41040094b373Sjv227347 		goto err_and_rele_lockfile;
41050094b373Sjv227347 
41060094b373Sjv227347 	/*
41070b5de56dSgjelinek 	 * We're making some file system changes now so we have to clean up
41080b5de56dSgjelinek 	 * the file system before we are done.  This will either clean up the
4109865e09a4Sgjelinek 	 * new zonepath if the zonecfg update failed or it will clean up the
4110865e09a4Sgjelinek 	 * old zonepath if everything is ok.
4111865e09a4Sgjelinek 	 */
4112865e09a4Sgjelinek 	revert = B_TRUE;
4113865e09a4Sgjelinek 
41140b5de56dSgjelinek 	if (is_zonepath_zfs(zonepath) &&
41150b5de56dSgjelinek 	    move_zfs(zonepath, new_zonepath) != Z_ERR) {
41160b5de56dSgjelinek 		is_zfs = B_TRUE;
41170b5de56dSgjelinek 
41180b5de56dSgjelinek 	} else if (fast) {
4119865e09a4Sgjelinek 		/* same file system, use rename for a quick move */
4120865e09a4Sgjelinek 
4121865e09a4Sgjelinek 		/*
4122865e09a4Sgjelinek 		 * Remove the new_zonepath directory that got created above
4123865e09a4Sgjelinek 		 * during the validation.  It gets in the way of the rename.
4124865e09a4Sgjelinek 		 */
4125865e09a4Sgjelinek 		if (rmdir(new_zonepath) != 0) {
4126865e09a4Sgjelinek 			zperror(gettext("could not rmdir new zone path"),
4127865e09a4Sgjelinek 			    B_FALSE);
41280094b373Sjv227347 			(void) zone_mount_rootfs(&mounts, zonepath);
41290094b373Sjv227347 			goto err_and_rele_lockfile;
4130865e09a4Sgjelinek 		}
4131865e09a4Sgjelinek 
4132865e09a4Sgjelinek 		if (rename(zonepath, new_zonepath) != 0) {
4133865e09a4Sgjelinek 			/*
4134865e09a4Sgjelinek 			 * If this fails we don't need to do all of the
4135865e09a4Sgjelinek 			 * cleanup that happens for the rest of the code
4136865e09a4Sgjelinek 			 * so just return from this error.
4137865e09a4Sgjelinek 			 */
4138865e09a4Sgjelinek 			zperror(gettext("could not move zone"), B_FALSE);
41390094b373Sjv227347 			(void) zone_mount_rootfs(&mounts, zonepath);
41400094b373Sjv227347 			goto err_and_rele_lockfile;
4141865e09a4Sgjelinek 		}
4142865e09a4Sgjelinek 
4143865e09a4Sgjelinek 	} else {
41440b5de56dSgjelinek 		/*
41450b5de56dSgjelinek 		 * Attempt to create a ZFS fs for the new zonepath.  As usual,
41460b5de56dSgjelinek 		 * we don't care if this works or not since we always have the
41470b5de56dSgjelinek 		 * default behavior of a simple directory for the zonepath.
41480b5de56dSgjelinek 		 */
41490b5de56dSgjelinek 		create_zfs_zonepath(new_zonepath);
41500b5de56dSgjelinek 
4151865e09a4Sgjelinek 		(void) printf(gettext(
41520b5de56dSgjelinek 		    "Moving across file systems; copying zonepath %s..."),
4153865e09a4Sgjelinek 		    zonepath);
4154865e09a4Sgjelinek 		(void) fflush(stdout);
4155865e09a4Sgjelinek 
4156865e09a4Sgjelinek 		err = copy_zone(zonepath, new_zonepath);
4157865e09a4Sgjelinek 
4158865e09a4Sgjelinek 		(void) printf("\n");
4159865e09a4Sgjelinek 		if (err != Z_OK)
4160865e09a4Sgjelinek 			goto done;
4161865e09a4Sgjelinek 	}
4162865e09a4Sgjelinek 
41630094b373Sjv227347 	/*
41640094b373Sjv227347 	 * Mount the zone's root filesystem in the new zonepath if there was
41650094b373Sjv227347 	 * a root mount prior to the move.
41660094b373Sjv227347 	 */
41670094b373Sjv227347 	if (zone_mount_rootfs(&mounts, new_zonepath) != 0) {
41680094b373Sjv227347 		err = Z_ERR;
41690094b373Sjv227347 		goto done;
41700094b373Sjv227347 	}
41710094b373Sjv227347 	root_fs_mounted = B_TRUE;
41720094b373Sjv227347 
4173865e09a4Sgjelinek 	if ((err = zonecfg_set_zonepath(handle, new_zonepath)) != Z_OK) {
4174865e09a4Sgjelinek 		errno = err;
4175865e09a4Sgjelinek 		zperror(gettext("could not set new zonepath"), B_TRUE);
4176865e09a4Sgjelinek 		goto done;
4177865e09a4Sgjelinek 	}
4178865e09a4Sgjelinek 
4179865e09a4Sgjelinek 	if ((err = zonecfg_save(handle)) != Z_OK) {
4180865e09a4Sgjelinek 		errno = err;
4181865e09a4Sgjelinek 		zperror(gettext("zonecfg save failed"), B_TRUE);
4182865e09a4Sgjelinek 		goto done;
4183865e09a4Sgjelinek 	}
4184865e09a4Sgjelinek 
4185865e09a4Sgjelinek 	revert = B_FALSE;
4186865e09a4Sgjelinek 
4187865e09a4Sgjelinek done:
4188865e09a4Sgjelinek 	zonecfg_fini_handle(handle);
4189ff17c8bfSgjelinek 	zonecfg_release_lock_file(target_zone, lockfd);
4190865e09a4Sgjelinek 
4191865e09a4Sgjelinek 	/*
41920b5de56dSgjelinek 	 * Clean up the file system based on how things went.  We either
4193865e09a4Sgjelinek 	 * clean up the new zonepath if the operation failed for some reason
4194865e09a4Sgjelinek 	 * or we clean up the old zonepath if everything is ok.
4195865e09a4Sgjelinek 	 */
4196865e09a4Sgjelinek 	if (revert) {
41970094b373Sjv227347 		/*
41980094b373Sjv227347 		 * Check for the unlikely scenario in which the zone's
41990094b373Sjv227347 		 * zonepath and its root file system moved but libzonecfg
42000094b373Sjv227347 		 * couldn't save the new zonepath to the zone's configuration
42010094b373Sjv227347 		 * file.  The mounted root filesystem must be unmounted before
42020094b373Sjv227347 		 * zoneadm restores the zone's zonepath.
42030094b373Sjv227347 		 */
42040094b373Sjv227347 		if (root_fs_mounted && zone_unmount_rootfs(&mounts,
42050094b373Sjv227347 		    new_zonepath, B_TRUE) != 0) {
42060094b373Sjv227347 			/*
42070094b373Sjv227347 			 * We can't forcibly unmount the zone's root file system
42080094b373Sjv227347 			 * from the new zonepath.  Bail!
42090094b373Sjv227347 			 */
42100094b373Sjv227347 			zerror(gettext("fatal error: cannot unmount %s/root\n"),
42110094b373Sjv227347 			    new_zonepath);
42120094b373Sjv227347 			goto err_and_mounts_destroy;
42130094b373Sjv227347 		}
42140094b373Sjv227347 
4215865e09a4Sgjelinek 		/* The zonecfg update failed, cleanup the new zonepath. */
42160b5de56dSgjelinek 		if (is_zfs) {
42170b5de56dSgjelinek 			if (move_zfs(new_zonepath, zonepath) == Z_ERR) {
42180b5de56dSgjelinek 				(void) fprintf(stderr, gettext("could not "
42190b5de56dSgjelinek 				    "restore zonepath, the zfs mountpoint is "
42200b5de56dSgjelinek 				    "set as:\n%s\n"), new_zonepath);
42210b5de56dSgjelinek 				/*
42220b5de56dSgjelinek 				 * err is already != Z_OK since we're reverting
42230b5de56dSgjelinek 				 */
42240094b373Sjv227347 			} else {
42250094b373Sjv227347 				(void) zone_mount_rootfs(&mounts, zonepath);
42260b5de56dSgjelinek 			}
42270b5de56dSgjelinek 		} else if (fast) {
4228865e09a4Sgjelinek 			if (rename(new_zonepath, zonepath) != 0) {
4229865e09a4Sgjelinek 				zperror(gettext("could not restore zonepath"),
4230865e09a4Sgjelinek 				    B_FALSE);
4231865e09a4Sgjelinek 				/*
4232865e09a4Sgjelinek 				 * err is already != Z_OK since we're reverting
4233865e09a4Sgjelinek 				 */
42340094b373Sjv227347 			} else {
42350094b373Sjv227347 				(void) zone_mount_rootfs(&mounts, zonepath);
4236865e09a4Sgjelinek 			}
4237865e09a4Sgjelinek 		} else {
4238865e09a4Sgjelinek 			(void) printf(gettext("Cleaning up zonepath %s..."),
4239865e09a4Sgjelinek 			    new_zonepath);
4240865e09a4Sgjelinek 			(void) fflush(stdout);
42410b5de56dSgjelinek 			err = cleanup_zonepath(new_zonepath, B_TRUE);
4242865e09a4Sgjelinek 			(void) printf("\n");
4243865e09a4Sgjelinek 
424407b574eeSgjelinek 			if (err != Z_OK) {
4245865e09a4Sgjelinek 				errno = err;
4246865e09a4Sgjelinek 				zperror(gettext("could not remove new "
4247865e09a4Sgjelinek 				    "zonepath"), B_TRUE);
4248865e09a4Sgjelinek 			} else {
4249865e09a4Sgjelinek 				/*
4250865e09a4Sgjelinek 				 * Because we're reverting we know the mainline
4251865e09a4Sgjelinek 				 * code failed but we just reused the err
4252865e09a4Sgjelinek 				 * variable so we reset it back to Z_ERR.
4253865e09a4Sgjelinek 				 */
4254865e09a4Sgjelinek 				err = Z_ERR;
4255865e09a4Sgjelinek 			}
4256865e09a4Sgjelinek 
42570094b373Sjv227347 			(void) zone_mount_rootfs(&mounts, zonepath);
42580094b373Sjv227347 		}
4259865e09a4Sgjelinek 	} else {
4260865e09a4Sgjelinek 		/* The move was successful, cleanup the old zonepath. */
42610b5de56dSgjelinek 		if (!is_zfs && !fast) {
4262865e09a4Sgjelinek 			(void) printf(
4263865e09a4Sgjelinek 			    gettext("Cleaning up zonepath %s..."), zonepath);
4264865e09a4Sgjelinek 			(void) fflush(stdout);
42650b5de56dSgjelinek 			err = cleanup_zonepath(zonepath, B_TRUE);
4266865e09a4Sgjelinek 			(void) printf("\n");
4267865e09a4Sgjelinek 
426807b574eeSgjelinek 			if (err != Z_OK) {
4269865e09a4Sgjelinek 				errno = err;
4270865e09a4Sgjelinek 				zperror(gettext("could not remove zonepath"),
4271865e09a4Sgjelinek 				    B_TRUE);
4272865e09a4Sgjelinek 			}
4273865e09a4Sgjelinek 		}
4274865e09a4Sgjelinek 	}
4275865e09a4Sgjelinek 
42760094b373Sjv227347 	zone_mounts_destroy(&mounts);
4277865e09a4Sgjelinek 	return ((err == Z_OK) ? Z_OK : Z_ERR);
42780094b373Sjv227347 
42790094b373Sjv227347 err_and_rele_lockfile:
42800094b373Sjv227347 	zonecfg_release_lock_file(target_zone, lockfd);
42810094b373Sjv227347 err_and_fini_handle:
42820094b373Sjv227347 	zonecfg_fini_handle(handle);
42830094b373Sjv227347 err_and_mounts_destroy:
42840094b373Sjv227347 	zone_mounts_destroy(&mounts);
42850094b373Sjv227347 	return (Z_ERR);
4286865e09a4Sgjelinek }
4287865e09a4Sgjelinek 
4288ff17c8bfSgjelinek /* ARGSUSED */
4289ee519a1fSgjelinek static int
detach_func(int argc,char * argv[])4290ee519a1fSgjelinek detach_func(int argc, char *argv[])
4291ee519a1fSgjelinek {
429271443f5aS 	int lockfd = -1;
4293ee519a1fSgjelinek 	int err, arg;
4294ee519a1fSgjelinek 	char zonepath[MAXPATHLEN];
429537774979Sgjelinek 	char cmdbuf[MAXPATHLEN];
4296ff17c8bfSgjelinek 	char precmdbuf[MAXPATHLEN];
42978cd327d5Sgjelinek 	boolean_t execute = B_TRUE;
4298ff17c8bfSgjelinek 	boolean_t brand_help = B_FALSE;
429937774979Sgjelinek 	brand_handle_t bh = NULL;
4300ff17c8bfSgjelinek 	int status;
4301ee519a1fSgjelinek 
4302ee519a1fSgjelinek 	if (zonecfg_in_alt_root()) {
4303ee519a1fSgjelinek 		zerror(gettext("cannot detach zone in alternate root"));
4304ee519a1fSgjelinek 		return (Z_ERR);
4305ee519a1fSgjelinek 	}
4306ee519a1fSgjelinek 
4307ff17c8bfSgjelinek 	/* Check the argv string for args we handle internally */
4308ee519a1fSgjelinek 	optind = 0;
4309ff17c8bfSgjelinek 	opterr = 0;
4310ff17c8bfSgjelinek 	while ((arg = getopt(argc, argv, "?n")) != EOF) {
4311ee519a1fSgjelinek 		switch (arg) {
4312ee519a1fSgjelinek 		case '?':
4313ff17c8bfSgjelinek 			if (optopt == '?') {
4314ee519a1fSgjelinek 				sub_usage(SHELP_DETACH, CMD_DETACH);
4315ff17c8bfSgjelinek 				brand_help = B_TRUE;
4316ff17c8bfSgjelinek 			}
4317ff17c8bfSgjelinek 			/* Ignore unknown options - may be brand specific. */
4318ff17c8bfSgjelinek 			break;
43198cd327d5Sgjelinek 		case 'n':
43208cd327d5Sgjelinek 			execute = B_FALSE;
43218cd327d5Sgjelinek 			break;
4322ee519a1fSgjelinek 		default:
4323ff17c8bfSgjelinek 			/* Ignore unknown options - may be brand specific. */
4324ff17c8bfSgjelinek 			break;
4325ee519a1fSgjelinek 		}
4326ee519a1fSgjelinek 	}
43279acbbeafSnn35248 
4328ff17c8bfSgjelinek 	if (brand_help)
4329ff17c8bfSgjelinek 		execute = B_FALSE;
4330ff17c8bfSgjelinek 
43318cd327d5Sgjelinek 	if (execute) {
43329acbbeafSnn35248 		if (sanity_check(target_zone, CMD_DETACH, B_FALSE, B_TRUE,
43339acbbeafSnn35248 		    B_FALSE) != Z_OK)
4334ee519a1fSgjelinek 			return (Z_ERR);
4335ce28b40eSzt129084 		if (verify_details(CMD_DETACH, argv) != Z_OK)
4336ee519a1fSgjelinek 			return (Z_ERR);
43378cd327d5Sgjelinek 	} else {
43388cd327d5Sgjelinek 		/*
43398cd327d5Sgjelinek 		 * We want a dry-run to work for a non-privileged user so we
43408cd327d5Sgjelinek 		 * only do minimal validation.
43418cd327d5Sgjelinek 		 */
43428cd327d5Sgjelinek 		if (target_zone == NULL) {
43438cd327d5Sgjelinek 			zerror(gettext("no zone specified"));
43448cd327d5Sgjelinek 			return (Z_ERR);
43458cd327d5Sgjelinek 		}
43468cd327d5Sgjelinek 
43478cd327d5Sgjelinek 		if (strcmp(target_zone, GLOBAL_ZONENAME) == 0) {
43488cd327d5Sgjelinek 			zerror(gettext("%s operation is invalid for the "
43498cd327d5Sgjelinek 			    "global zone."), cmd_to_str(CMD_DETACH));
43508cd327d5Sgjelinek 			return (Z_ERR);
43518cd327d5Sgjelinek 		}
43528cd327d5Sgjelinek 	}
4353ee519a1fSgjelinek 
4354ee519a1fSgjelinek 	if ((err = zone_get_zonepath(target_zone, zonepath, sizeof (zonepath)))
4355ee519a1fSgjelinek 	    != Z_OK) {
4356ee519a1fSgjelinek 		errno = err;
4357ee519a1fSgjelinek 		zperror2(target_zone, gettext("could not get zone path"));
4358ee519a1fSgjelinek 		return (Z_ERR);
4359ee519a1fSgjelinek 	}
4360ee519a1fSgjelinek 
4361ff17c8bfSgjelinek 	/* Fetch the detach and predetach hooks from the brand configuration. */
436237774979Sgjelinek 	if ((bh = brand_open(target_brand)) == NULL) {
436337774979Sgjelinek 		zerror(gettext("missing or invalid brand"));
436437774979Sgjelinek 		return (Z_ERR);
436537774979Sgjelinek 	}
436637774979Sgjelinek 
4367ff17c8bfSgjelinek 	if (get_hook(bh, cmdbuf, sizeof (cmdbuf), brand_get_detach, target_zone,
4368ff17c8bfSgjelinek 	    zonepath) != Z_OK) {
4369ff17c8bfSgjelinek 		zerror("invalid brand configuration: missing detach resource");
4370ff17c8bfSgjelinek 		brand_close(bh);
4371ff17c8bfSgjelinek 		return (Z_ERR);
4372ff17c8bfSgjelinek 	}
4373ff17c8bfSgjelinek 
4374ff17c8bfSgjelinek 	if (get_hook(bh, precmdbuf, sizeof (precmdbuf), brand_get_predetach,
4375ff17c8bfSgjelinek 	    target_zone, zonepath) != Z_OK) {
437637774979Sgjelinek 		zerror("invalid brand configuration: missing predetach "
437737774979Sgjelinek 		    "resource");
437837774979Sgjelinek 		brand_close(bh);
437937774979Sgjelinek 		return (Z_ERR);
438037774979Sgjelinek 	}
438137774979Sgjelinek 	brand_close(bh);
438237774979Sgjelinek 
4383ff17c8bfSgjelinek 	/* Append all options to predetach hook. */
4384ff17c8bfSgjelinek 	if (addoptions(precmdbuf, argv, sizeof (precmdbuf)) != Z_OK)
438537774979Sgjelinek 		return (Z_ERR);
438637774979Sgjelinek 
4387ff17c8bfSgjelinek 	/* Append all options to detach hook. */
4388ff17c8bfSgjelinek 	if (addoptions(cmdbuf, argv, sizeof (cmdbuf)) != Z_OK)
438937774979Sgjelinek 		return (Z_ERR);
439037774979Sgjelinek 
4391ff17c8bfSgjelinek 	if (execute && zonecfg_grab_lock_file(target_zone, &lockfd) != Z_OK) {
4392ee519a1fSgjelinek 		zerror(gettext("another %s may have an operation in progress."),
4393ee519a1fSgjelinek 		    "zoneadm");
4394ee519a1fSgjelinek 		return (Z_ERR);
4395ee519a1fSgjelinek 	}
4396ee519a1fSgjelinek 
4397ff17c8bfSgjelinek 	/* If we have a brand predetach hook, run it. */
4398ff17c8bfSgjelinek 	if (!brand_help && precmdbuf[0] != '\0') {
4399ff17c8bfSgjelinek 		status = do_subproc(precmdbuf);
4400ff17c8bfSgjelinek 		if (subproc_status(gettext("brand-specific predetach"),
4401ff17c8bfSgjelinek 		    status, B_FALSE) != ZONE_SUBPROC_OK) {
4402ff17c8bfSgjelinek 
440371443f5aS 			if (execute) {
440471443f5aS 				assert(lockfd >= 0);
4405ff17c8bfSgjelinek 				zonecfg_release_lock_file(target_zone, lockfd);
440671443f5aS 				lockfd = -1;
440771443f5aS 			}
440871443f5aS 
440971443f5aS 			assert(lockfd == -1);
4410ff17c8bfSgjelinek 			return (Z_ERR);
4411ff17c8bfSgjelinek 		}
4412ff17c8bfSgjelinek 	}
4413ff17c8bfSgjelinek 
4414ff17c8bfSgjelinek 	if (cmdbuf[0] != '\0') {
4415ff17c8bfSgjelinek 		/* Run the detach hook */
4416c75cc341S 		status = do_subproc(cmdbuf);
4417ff17c8bfSgjelinek 		if ((status = subproc_status(gettext("brand-specific detach"),
4418ff17c8bfSgjelinek 		    status, B_FALSE)) != ZONE_SUBPROC_OK) {
4419ff17c8bfSgjelinek 			if (status == ZONE_SUBPROC_USAGE && !brand_help)
4420ff17c8bfSgjelinek 				sub_usage(SHELP_DETACH, CMD_DETACH);
4421ff17c8bfSgjelinek 
442271443f5aS 			if (execute) {
442371443f5aS 				assert(lockfd >= 0);
4424ff17c8bfSgjelinek 				zonecfg_release_lock_file(target_zone, lockfd);
442571443f5aS 				lockfd = -1;
442671443f5aS 			}
4427ff17c8bfSgjelinek 
442871443f5aS 			assert(lockfd == -1);
4429ff17c8bfSgjelinek 			return (Z_ERR);
4430ff17c8bfSgjelinek 		}
4431ff17c8bfSgjelinek 
4432ff17c8bfSgjelinek 	} else {
443371443f5aS 		zone_dochandle_t handle;
443471443f5aS 
4435ff17c8bfSgjelinek 		/* If just help, we're done since there is no brand help. */
443671443f5aS 		if (brand_help) {
443771443f5aS 			assert(lockfd == -1);
4438ff17c8bfSgjelinek 			return (Z_OK);
443971443f5aS 		}
4440ff17c8bfSgjelinek 
4441ff17c8bfSgjelinek 		/*
4442ff17c8bfSgjelinek 		 * Run the built-in detach support.  Just generate a simple
4443ff17c8bfSgjelinek 		 * zone definition XML file and detach.
4444ff17c8bfSgjelinek 		 */
4445ff17c8bfSgjelinek 
4446ff17c8bfSgjelinek 		/* Don't detach the zone if anything is still mounted there */
4447ff17c8bfSgjelinek 		if (execute && zonecfg_find_mounts(zonepath, NULL, NULL)) {
4448ff17c8bfSgjelinek 			(void) fprintf(stderr, gettext("These file systems are "
4449ff17c8bfSgjelinek 			    "mounted on subdirectories of %s.\n"), zonepath);
4450ff17c8bfSgjelinek 			(void) zonecfg_find_mounts(zonepath, zfm_print, NULL);
4451ff17c8bfSgjelinek 			err = ZONE_SUBPROC_NOTCOMPLETE;
4452ff17c8bfSgjelinek 			goto done;
4453ff17c8bfSgjelinek 		}
4454ff17c8bfSgjelinek 
4455ff17c8bfSgjelinek 		if ((handle = zonecfg_init_handle()) == NULL) {
4456ff17c8bfSgjelinek 			zperror(cmd_to_str(CMD_DETACH), B_TRUE);
4457ff17c8bfSgjelinek 			err = ZONE_SUBPROC_NOTCOMPLETE;
4458ff17c8bfSgjelinek 			goto done;
4459ff17c8bfSgjelinek 		}
4460ff17c8bfSgjelinek 
4461ff17c8bfSgjelinek 		if ((err = zonecfg_get_handle(target_zone, handle)) != Z_OK) {
4462ee519a1fSgjelinek 			errno = err;
4463ff17c8bfSgjelinek 			zperror(cmd_to_str(CMD_DETACH), B_TRUE);
4464ff17c8bfSgjelinek 
446571443f5aS 		} else if ((err = zonecfg_detach_save(handle,
4466ff17c8bfSgjelinek 		    (execute ? 0 : ZONE_DRY_RUN))) != Z_OK) {
4467ff17c8bfSgjelinek 			errno = err;
4468ff17c8bfSgjelinek 			zperror(gettext("saving the detach manifest failed"),
4469ee519a1fSgjelinek 			    B_TRUE);
4470ee519a1fSgjelinek 		}
4471ee519a1fSgjelinek 
4472ff17c8bfSgjelinek 		zonecfg_fini_handle(handle);
447371443f5aS 		if (err != Z_OK)
447471443f5aS 			goto done;
4475ee519a1fSgjelinek 	}
4476ee519a1fSgjelinek 
44778cd327d5Sgjelinek 	/*
44788cd327d5Sgjelinek 	 * Set the zone state back to configured unless we are running with the
44798cd327d5Sgjelinek 	 * no-execute option.
44808cd327d5Sgjelinek 	 */
44818cd327d5Sgjelinek 	if (execute && (err = zone_set_state(target_zone,
44828cd327d5Sgjelinek 	    ZONE_STATE_CONFIGURED)) != Z_OK) {
4483ee519a1fSgjelinek 		errno = err;
4484ee519a1fSgjelinek 		zperror(gettext("could not reset state"), B_TRUE);
4485ee519a1fSgjelinek 	}
4486ee519a1fSgjelinek 
4487ee519a1fSgjelinek done:
448871443f5aS 	if (execute) {
448971443f5aS 		assert(lockfd >= 0);
4490ff17c8bfSgjelinek 		zonecfg_release_lock_file(target_zone, lockfd);
449171443f5aS 		lockfd = -1;
449271443f5aS 	}
4493ee519a1fSgjelinek 
449471443f5aS 	assert(lockfd == -1);
4495ee519a1fSgjelinek 	return ((err == Z_OK) ? Z_OK : Z_ERR);
4496ee519a1fSgjelinek }
4497ee519a1fSgjelinek 
4498ee519a1fSgjelinek /*
4499ff17c8bfSgjelinek  * Determine the brand when doing a dry-run attach.  The zone does not have to
4500ff17c8bfSgjelinek  * exist, so we have to read the incoming manifest to determine the zone's
4501ff17c8bfSgjelinek  * brand.
4502ff17c8bfSgjelinek  *
4503ff17c8bfSgjelinek  * Because the manifest has to be processed twice; once to determine the brand
4504ff17c8bfSgjelinek  * and once to do the brand-specific attach logic, we always read it into a tmp
4505ff17c8bfSgjelinek  * file.  This handles the manifest coming from stdin or a regular file.  The
4506ff17c8bfSgjelinek  * tmpname parameter returns the name of the temporary file that the manifest
4507ff17c8bfSgjelinek  * was read into.
4508ee519a1fSgjelinek  */
4509ee519a1fSgjelinek static int
dryrun_get_brand(char * manifest_path,char * tmpname,int size)4510ff17c8bfSgjelinek dryrun_get_brand(char *manifest_path, char *tmpname, int size)
45118cd327d5Sgjelinek {
45128cd327d5Sgjelinek 	int fd;
45138cd327d5Sgjelinek 	int err;
4514ff17c8bfSgjelinek 	int res = Z_OK;
45158cd327d5Sgjelinek 	zone_dochandle_t local_handle;
45168cd327d5Sgjelinek 	zone_dochandle_t rem_handle = NULL;
4517ff17c8bfSgjelinek 	int len;
4518ff17c8bfSgjelinek 	int ofd;
4519ff17c8bfSgjelinek 	char buf[512];
45208cd327d5Sgjelinek 
45218cd327d5Sgjelinek 	if (strcmp(manifest_path, "-") == 0) {
4522ff17c8bfSgjelinek 		fd = STDIN_FILENO;
4523ff17c8bfSgjelinek 	} else {
4524ff17c8bfSgjelinek 		if ((fd = open(manifest_path, O_RDONLY)) < 0) {
4525ff17c8bfSgjelinek 			if (getcwd(buf, sizeof (buf)) == NULL)
4526ff17c8bfSgjelinek 				(void) strlcpy(buf, "/", sizeof (buf));
4527ff17c8bfSgjelinek 			zerror(gettext("could not open manifest path %s%s: %s"),
4528ff17c8bfSgjelinek 			    (*manifest_path == '/' ? "" : buf), manifest_path,
4529ff17c8bfSgjelinek 			    strerror(errno));
4530ff17c8bfSgjelinek 			return (Z_ERR);
4531ff17c8bfSgjelinek 		}
4532ff17c8bfSgjelinek 	}
4533ff17c8bfSgjelinek 
4534ff17c8bfSgjelinek 	(void) snprintf(tmpname, size, "/var/run/zone.%d", getpid());
4535ff17c8bfSgjelinek 
4536ff17c8bfSgjelinek 	if ((ofd = open(tmpname, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR)) < 0) {
4537ff17c8bfSgjelinek 		zperror(gettext("could not save manifest"), B_FALSE);
4538ff17c8bfSgjelinek 		(void) close(fd);
4539ff17c8bfSgjelinek 		return (Z_ERR);
4540ff17c8bfSgjelinek 	}
4541ff17c8bfSgjelinek 
4542ff17c8bfSgjelinek 	while ((len = read(fd, buf, sizeof (buf))) > 0) {
4543ff17c8bfSgjelinek 		if (write(ofd, buf, len) == -1) {
4544ff17c8bfSgjelinek 			zperror(gettext("could not save manifest"), B_FALSE);
4545ff17c8bfSgjelinek 			(void) close(ofd);
4546ff17c8bfSgjelinek 			(void) close(fd);
4547ff17c8bfSgjelinek 			return (Z_ERR);
4548ff17c8bfSgjelinek 		}
4549ff17c8bfSgjelinek 	}
4550ff17c8bfSgjelinek 
4551ff17c8bfSgjelinek 	if (close(ofd) != 0) {
4552ff17c8bfSgjelinek 		zperror(gettext("could not save manifest"), B_FALSE);
4553ff17c8bfSgjelinek 		(void) close(fd);
4554ff17c8bfSgjelinek 		return (Z_ERR);
4555ff17c8bfSgjelinek 	}
4556ff17c8bfSgjelinek 
4557ff17c8bfSgjelinek 	(void) close(fd);
4558ff17c8bfSgjelinek 
4559ff17c8bfSgjelinek 	if ((fd = open(tmpname, O_RDONLY)) < 0) {
45608cd327d5Sgjelinek 		zperror(gettext("could not open manifest path"), B_FALSE);
45618cd327d5Sgjelinek 		return (Z_ERR);
45628cd327d5Sgjelinek 	}
45638cd327d5Sgjelinek 
45648cd327d5Sgjelinek 	if ((local_handle = zonecfg_init_handle()) == NULL) {
45658cd327d5Sgjelinek 		zperror(cmd_to_str(CMD_ATTACH), B_TRUE);
45668cd327d5Sgjelinek 		res = Z_ERR;
45678cd327d5Sgjelinek 		goto done;
45688cd327d5Sgjelinek 	}
45698cd327d5Sgjelinek 
45708cd327d5Sgjelinek 	if ((rem_handle = zonecfg_init_handle()) == NULL) {
45718cd327d5Sgjelinek 		zperror(cmd_to_str(CMD_ATTACH), B_TRUE);
45728cd327d5Sgjelinek 		res = Z_ERR;
45738cd327d5Sgjelinek 		goto done;
45748cd327d5Sgjelinek 	}
45758cd327d5Sgjelinek 
45768cd327d5Sgjelinek 	if ((err = zonecfg_attach_manifest(fd, local_handle, rem_handle))
45778cd327d5Sgjelinek 	    != Z_OK) {
45788cd327d5Sgjelinek 		res = Z_ERR;
4579d9e728a2Sgjelinek 
4580d9e728a2Sgjelinek 		if (err == Z_INVALID_DOCUMENT) {
4581d9e728a2Sgjelinek 			struct stat st;
4582d9e728a2Sgjelinek 			char buf[6];
4583d9e728a2Sgjelinek 
4584d9e728a2Sgjelinek 			if (strcmp(manifest_path, "-") == 0) {
4585d9e728a2Sgjelinek 				zerror(gettext("Input is not a valid XML "
4586d9e728a2Sgjelinek 				    "file"));
4587d9e728a2Sgjelinek 				goto done;
4588d9e728a2Sgjelinek 			}
4589d9e728a2Sgjelinek 
4590d9e728a2Sgjelinek 			if (fstat(fd, &st) == -1 || !S_ISREG(st.st_mode)) {
4591d9e728a2Sgjelinek 				zerror(gettext("%s is not an XML file"),
4592d9e728a2Sgjelinek 				    manifest_path);
4593d9e728a2Sgjelinek 				goto done;
4594d9e728a2Sgjelinek 			}
4595d9e728a2Sgjelinek 
4596d9e728a2Sgjelinek 			bzero(buf, sizeof (buf));
4597d9e728a2Sgjelinek 			(void) lseek(fd, 0L, SEEK_SET);
4598d9e728a2Sgjelinek 			if (read(fd, buf, sizeof (buf) - 1) < 0 ||
4599d9e728a2Sgjelinek 			    strncmp(buf, "<?xml", 5) != 0)
4600d9e728a2Sgjelinek 				zerror(gettext("%s is not an XML file"),
4601d9e728a2Sgjelinek 				    manifest_path);
4602d9e728a2Sgjelinek 			else
4603d9e728a2Sgjelinek 				zerror(gettext("Cannot attach to an earlier "
4604d9e728a2Sgjelinek 				    "release of the operating system"));
4605d9e728a2Sgjelinek 		} else {
4606d9e728a2Sgjelinek 			zperror(cmd_to_str(CMD_ATTACH), B_TRUE);
4607d9e728a2Sgjelinek 		}
46088cd327d5Sgjelinek 		goto done;
46098cd327d5Sgjelinek 	}
46108cd327d5Sgjelinek 
4611ff17c8bfSgjelinek 	/* Retrieve remote handle brand type. */
4612e2482d1aSgjelinek 	if (zonecfg_get_brand(rem_handle, target_brand, sizeof (target_brand))
4613e2482d1aSgjelinek 	    != Z_OK) {
4614e2482d1aSgjelinek 		zerror(gettext("missing or invalid brand"));
4615e2482d1aSgjelinek 		exit(Z_ERR);
4616e2482d1aSgjelinek 	}
46178cd327d5Sgjelinek 
46188cd327d5Sgjelinek done:
46198cd327d5Sgjelinek 	zonecfg_fini_handle(local_handle);
46208cd327d5Sgjelinek 	zonecfg_fini_handle(rem_handle);
4621ff17c8bfSgjelinek 	(void) close(fd);
46228cd327d5Sgjelinek 
46238cd327d5Sgjelinek 	return ((res == Z_OK) ? Z_OK : Z_ERR);
46248cd327d5Sgjelinek }
46258cd327d5Sgjelinek 
46266cfd72c6Sgjelinek /* ARGSUSED */
4627ee519a1fSgjelinek static int
attach_func(int argc,char * argv[])4628ee519a1fSgjelinek attach_func(int argc, char *argv[])
4629ee519a1fSgjelinek {
463071443f5aS 	int lockfd = -1;
4631ee519a1fSgjelinek 	int err, arg;
4632ee519a1fSgjelinek 	boolean_t force = B_FALSE;
4633ee519a1fSgjelinek 	zone_dochandle_t handle;
4634ee519a1fSgjelinek 	char zonepath[MAXPATHLEN];
463537774979Sgjelinek 	char cmdbuf[MAXPATHLEN];
4636ff17c8bfSgjelinek 	char postcmdbuf[MAXPATHLEN];
46378cd327d5Sgjelinek 	boolean_t execute = B_TRUE;
4638ff17c8bfSgjelinek 	boolean_t brand_help = B_FALSE;
46398cd327d5Sgjelinek 	char *manifest_path;
4640ff17c8bfSgjelinek 	char tmpmanifest[80];
4641ff17c8bfSgjelinek 	int manifest_pos;
464237774979Sgjelinek 	brand_handle_t bh = NULL;
4643ff17c8bfSgjelinek 	int status;
4644edfa49ffS 	int last_index = 0;
4645edfa49ffS 	int offset;
4646edfa49ffS 	char *up;
4647edfa49ffS 	boolean_t forced_update = B_FALSE;
4648ee519a1fSgjelinek 
4649ee519a1fSgjelinek 	if (zonecfg_in_alt_root()) {
4650ee519a1fSgjelinek 		zerror(gettext("cannot attach zone in alternate root"));
4651ee519a1fSgjelinek 		return (Z_ERR);
4652ee519a1fSgjelinek 	}
4653ee519a1fSgjelinek 
4654ff17c8bfSgjelinek 	/* Check the argv string for args we handle internally */
4655ee519a1fSgjelinek 	optind = 0;
4656ff17c8bfSgjelinek 	opterr = 0;
4657edfa49ffS 	while ((arg = getopt(argc, argv, "?Fn:U")) != EOF) {
4658ee519a1fSgjelinek 		switch (arg) {
4659ee519a1fSgjelinek 		case '?':
4660ff17c8bfSgjelinek 			if (optopt == '?') {
4661ee519a1fSgjelinek 				sub_usage(SHELP_ATTACH, CMD_ATTACH);
4662ff17c8bfSgjelinek 				brand_help = B_TRUE;
4663ff17c8bfSgjelinek 			}
4664ff17c8bfSgjelinek 			/* Ignore unknown options - may be brand specific. */
4665ff17c8bfSgjelinek 			break;
4666ee519a1fSgjelinek 		case 'F':
4667ee519a1fSgjelinek 			force = B_TRUE;
4668ee519a1fSgjelinek 			break;
46698cd327d5Sgjelinek 		case 'n':
46708cd327d5Sgjelinek 			execute = B_FALSE;
46718cd327d5Sgjelinek 			manifest_path = optarg;
4672ff17c8bfSgjelinek 			manifest_pos = optind - 1;
46736cfd72c6Sgjelinek 			break;
4674edfa49ffS 		case 'U':
4675edfa49ffS 			/*
4676edfa49ffS 			 * Undocumented 'force update' option for p2v update on
4677edfa49ffS 			 * attach when zone is in the incomplete state.  Change
4678edfa49ffS 			 * the option back to 'u' and set forced_update flag.
4679edfa49ffS 			 */
4680edfa49ffS 			if (optind == last_index)
4681edfa49ffS 				offset = optind;
4682edfa49ffS 			else
4683edfa49ffS 				offset = optind - 1;
4684edfa49ffS 			if ((up = index(argv[offset], 'U')) != NULL)
4685edfa49ffS 				*up = 'u';
4686edfa49ffS 			forced_update = B_TRUE;
4687edfa49ffS 			break;
4688ee519a1fSgjelinek 		default:
4689ff17c8bfSgjelinek 			/* Ignore unknown options - may be brand specific. */
4690ff17c8bfSgjelinek 			break;
4691ee519a1fSgjelinek 		}
4692edfa49ffS 		last_index = optind;
4693ee519a1fSgjelinek 	}
46948cd327d5Sgjelinek 
4695ff17c8bfSgjelinek 	if (brand_help) {
4696ff17c8bfSgjelinek 		force = B_FALSE;
4697ff17c8bfSgjelinek 		execute = B_TRUE;
4698ff17c8bfSgjelinek 	}
4699ff17c8bfSgjelinek 
4700ff17c8bfSgjelinek 	/* dry-run and force flags are mutually exclusive */
4701ff17c8bfSgjelinek 	if (!execute && force) {
4702ff17c8bfSgjelinek 		zerror(gettext("-F and -n flags are mutually exclusive"));
47036cfd72c6Sgjelinek 		return (Z_ERR);
47046cfd72c6Sgjelinek 	}
47056cfd72c6Sgjelinek 
47068cd327d5Sgjelinek 	/*
4707ff17c8bfSgjelinek 	 * If the no-execute option was specified, we don't do validation and
4708ff17c8bfSgjelinek 	 * need to figure out the brand, since there is no zone required to be
47098cd327d5Sgjelinek 	 * configured for this option.
47108cd327d5Sgjelinek 	 */
4711ff17c8bfSgjelinek 	if (execute) {
4712ff17c8bfSgjelinek 		if (!brand_help) {
4713ff17c8bfSgjelinek 			if (sanity_check(target_zone, CMD_ATTACH, B_FALSE,
4714edfa49ffS 			    B_TRUE, forced_update) != Z_OK)
4715ee519a1fSgjelinek 				return (Z_ERR);
4716ce28b40eSzt129084 			if (verify_details(CMD_ATTACH, argv) != Z_OK)
4717ee519a1fSgjelinek 				return (Z_ERR);
4718ff17c8bfSgjelinek 		}
4719ee519a1fSgjelinek 
4720ff17c8bfSgjelinek 		if ((err = zone_get_zonepath(target_zone, zonepath,
4721ff17c8bfSgjelinek 		    sizeof (zonepath))) != Z_OK) {
4722ee519a1fSgjelinek 			errno = err;
4723ff17c8bfSgjelinek 			zperror2(target_zone,
4724ff17c8bfSgjelinek 			    gettext("could not get zone path"));
4725ee519a1fSgjelinek 			return (Z_ERR);
4726ee519a1fSgjelinek 		}
4727ff17c8bfSgjelinek 	} else {
4728ff17c8bfSgjelinek 		if (dryrun_get_brand(manifest_path, tmpmanifest,
4729ff17c8bfSgjelinek 		    sizeof (tmpmanifest)) != Z_OK)
4730ff17c8bfSgjelinek 			return (Z_ERR);
4731ff17c8bfSgjelinek 
4732ff17c8bfSgjelinek 		argv[manifest_pos] = tmpmanifest;
4733ff17c8bfSgjelinek 		target_zone = "-";
4734ff17c8bfSgjelinek 		(void) strlcpy(zonepath, "-", sizeof (zonepath));
4735ff17c8bfSgjelinek 
4736ff17c8bfSgjelinek 		/* Run the brand's verify_adm hook. */
4737ff17c8bfSgjelinek 		if (verify_brand(NULL, CMD_ATTACH, argv) != Z_OK)
4738ff17c8bfSgjelinek 			return (Z_ERR);
4739ff17c8bfSgjelinek 	}
4740ff17c8bfSgjelinek 
4741ff17c8bfSgjelinek 	/*
4742ff17c8bfSgjelinek 	 * Fetch the attach and postattach hooks from the brand configuration.
4743ff17c8bfSgjelinek 	 */
474437774979Sgjelinek 	if ((bh = brand_open(target_brand)) == NULL) {
474537774979Sgjelinek 		zerror(gettext("missing or invalid brand"));
474637774979Sgjelinek 		return (Z_ERR);
474737774979Sgjelinek 	}
474837774979Sgjelinek 
4749ff17c8bfSgjelinek 	if (get_hook(bh, cmdbuf, sizeof (cmdbuf), brand_get_attach, target_zone,
4750ff17c8bfSgjelinek 	    zonepath) != Z_OK) {
4751ff17c8bfSgjelinek 		zerror("invalid brand configuration: missing attach resource");
4752ff17c8bfSgjelinek 		brand_close(bh);
4753ff17c8bfSgjelinek 		return (Z_ERR);
4754ff17c8bfSgjelinek 	}
4755ff17c8bfSgjelinek 
4756ff17c8bfSgjelinek 	if (get_hook(bh, postcmdbuf, sizeof (postcmdbuf), brand_get_postattach,
4757ff17c8bfSgjelinek 	    target_zone, zonepath) != Z_OK) {
475837774979Sgjelinek 		zerror("invalid brand configuration: missing postattach "
475937774979Sgjelinek 		    "resource");
476037774979Sgjelinek 		brand_close(bh);
476137774979Sgjelinek 		return (Z_ERR);
476237774979Sgjelinek 	}
476337774979Sgjelinek 	brand_close(bh);
476437774979Sgjelinek 
4765ff17c8bfSgjelinek 	/* Append all options to attach hook. */
4766ff17c8bfSgjelinek 	if (addoptions(cmdbuf, argv, sizeof (cmdbuf)) != Z_OK)
476737774979Sgjelinek 		return (Z_ERR);
476837774979Sgjelinek 
4769ff17c8bfSgjelinek 	/* Append all options to postattach hook. */
4770ff17c8bfSgjelinek 	if (addoptions(postcmdbuf, argv, sizeof (postcmdbuf)) != Z_OK)
4771ff17c8bfSgjelinek 		return (Z_ERR);
4772ff17c8bfSgjelinek 
4773ff17c8bfSgjelinek 	if (execute && !brand_help) {
4774ff17c8bfSgjelinek 		if (zonecfg_grab_lock_file(target_zone, &lockfd) != Z_OK) {
4775ff17c8bfSgjelinek 			zerror(gettext("another %s may have an operation in "
4776ff17c8bfSgjelinek 			    "progress."), "zoneadm");
4777ee519a1fSgjelinek 			return (Z_ERR);
4778ee519a1fSgjelinek 		}
4779ff17c8bfSgjelinek 	}
4780ee519a1fSgjelinek 
478171443f5aS 	if (!force) {
478271443f5aS 		/*
478371443f5aS 		 * Not a force-attach, so we need to actually do the work.
478471443f5aS 		 */
4785ff17c8bfSgjelinek 		if (cmdbuf[0] != '\0') {
4786ff17c8bfSgjelinek 			/* Run the attach hook */
4787edfa49ffS 			status = do_subproc(cmdbuf);
478871443f5aS 			if ((status = subproc_status(gettext("brand-specific "
478971443f5aS 			    "attach"), status, B_FALSE)) != ZONE_SUBPROC_OK) {
4790ff17c8bfSgjelinek 				if (status == ZONE_SUBPROC_USAGE && !brand_help)
4791ff17c8bfSgjelinek 					sub_usage(SHELP_ATTACH, CMD_ATTACH);
4792ff17c8bfSgjelinek 
479371443f5aS 				if (execute && !brand_help) {
4794edfa49ffS 					assert(zonecfg_lock_file_held(&lockfd));
479571443f5aS 					zonecfg_release_lock_file(target_zone,
479671443f5aS 					    lockfd);
479771443f5aS 					lockfd = -1;
4798ee519a1fSgjelinek 				}
4799ee519a1fSgjelinek 
480071443f5aS 				assert(lockfd == -1);
480171443f5aS 				return (Z_ERR);
480271443f5aS 			}
4803ee519a1fSgjelinek 		}
4804ee519a1fSgjelinek 
48059acbbeafSnn35248 		/*
4806ff17c8bfSgjelinek 		 * Else run the built-in attach support.
4807ff17c8bfSgjelinek 		 * This is a no-op since there is nothing to validate.
48089acbbeafSnn35248 		 */
4809ff17c8bfSgjelinek 
4810ff17c8bfSgjelinek 		/* If dry-run or help, then we're done. */
4811ff17c8bfSgjelinek 		if (!execute || brand_help) {
4812ff17c8bfSgjelinek 			if (!execute)
4813ff17c8bfSgjelinek 				(void) unlink(tmpmanifest);
481471443f5aS 			assert(lockfd == -1);
4815ff17c8bfSgjelinek 			return (Z_OK);
48169acbbeafSnn35248 		}
481771443f5aS 	}
48189acbbeafSnn35248 
4819ca733e25S 	/* Now we can validate that the zonepath exists. */
4820ca733e25S 	if (validate_zonepath(zonepath, CMD_ATTACH) != Z_OK) {
4821ca733e25S 		(void) fprintf(stderr, gettext("could not verify zonepath %s "
4822ca733e25S 		    "because of the above errors.\n"), zonepath);
4823ca733e25S 
4824ca733e25S 		assert(zonecfg_lock_file_held(&lockfd));
4825ca733e25S 		zonecfg_release_lock_file(target_zone, lockfd);
4826ca733e25S 		return (Z_ERR);
4827ca733e25S 	}
4828ca733e25S 
482971443f5aS 	if ((handle = zonecfg_init_handle()) == NULL) {
483071443f5aS 		zperror(cmd_to_str(CMD_ATTACH), B_TRUE);
483171443f5aS 		err = Z_ERR;
483271443f5aS 	} else if ((err = zonecfg_get_handle(target_zone, handle)) != Z_OK) {
483371443f5aS 		errno = err;
483471443f5aS 		zperror(cmd_to_str(CMD_ATTACH), B_TRUE);
483571443f5aS 		zonecfg_fini_handle(handle);
483671443f5aS 	} else {
4837ee519a1fSgjelinek 		zonecfg_rm_detached(handle, force);
483871443f5aS 		zonecfg_fini_handle(handle);
483971443f5aS 	}
4840ee519a1fSgjelinek 
484171443f5aS 	if (err == Z_OK &&
484271443f5aS 	    (err = zone_set_state(target_zone, ZONE_STATE_INSTALLED)) != Z_OK) {
4843ee519a1fSgjelinek 		errno = err;
4844ee519a1fSgjelinek 		zperror(gettext("could not reset state"), B_TRUE);
4845ee519a1fSgjelinek 	}
4846ee519a1fSgjelinek 
4847edfa49ffS 	assert(zonecfg_lock_file_held(&lockfd));
4848ff17c8bfSgjelinek 	zonecfg_release_lock_file(target_zone, lockfd);
484971443f5aS 	lockfd = -1;
4850ee519a1fSgjelinek 
485137774979Sgjelinek 	/* If we have a brand postattach hook, run it. */
4852ff17c8bfSgjelinek 	if (err == Z_OK && !force && postcmdbuf[0] != '\0') {
4853ff17c8bfSgjelinek 		status = do_subproc(postcmdbuf);
485437774979Sgjelinek 		if (subproc_status(gettext("brand-specific postattach"),
485537774979Sgjelinek 		    status, B_FALSE) != ZONE_SUBPROC_OK) {
485637774979Sgjelinek 			if ((err = zone_set_state(target_zone,
485737774979Sgjelinek 			    ZONE_STATE_CONFIGURED)) != Z_OK) {
485837774979Sgjelinek 				errno = err;
485937774979Sgjelinek 				zperror(gettext("could not reset state"),
486037774979Sgjelinek 				    B_TRUE);
486137774979Sgjelinek 			}
486237774979Sgjelinek 		}
486337774979Sgjelinek 	}
486437774979Sgjelinek 
486571443f5aS 	assert(lockfd == -1);
4866ee519a1fSgjelinek 	return ((err == Z_OK) ? Z_OK : Z_ERR);
4867ee519a1fSgjelinek }
4868ee519a1fSgjelinek 
4869865e09a4Sgjelinek /*
48707c478bd9Sstevel@tonic-gate  * On input, TRUE => yes, FALSE => no.
48717c478bd9Sstevel@tonic-gate  * On return, TRUE => 1, FALSE => 0, could not ask => -1.
48727c478bd9Sstevel@tonic-gate  */
48737c478bd9Sstevel@tonic-gate 
48747c478bd9Sstevel@tonic-gate static int
ask_yesno(boolean_t default_answer,const char * question)48757c478bd9Sstevel@tonic-gate ask_yesno(boolean_t default_answer, const char *question)
48767c478bd9Sstevel@tonic-gate {
48777c478bd9Sstevel@tonic-gate 	char line[64];	/* should be large enough to answer yes or no */
48787c478bd9Sstevel@tonic-gate 
48797c478bd9Sstevel@tonic-gate 	if (!isatty(STDIN_FILENO))
48807c478bd9Sstevel@tonic-gate 		return (-1);
48817c478bd9Sstevel@tonic-gate 	for (;;) {
48827c478bd9Sstevel@tonic-gate 		(void) printf("%s (%s)? ", question,
48837c478bd9Sstevel@tonic-gate 		    default_answer ? "[y]/n" : "y/[n]");
48847c478bd9Sstevel@tonic-gate 		if (fgets(line, sizeof (line), stdin) == NULL ||
48857c478bd9Sstevel@tonic-gate 		    line[0] == '\n')
48867c478bd9Sstevel@tonic-gate 			return (default_answer ? 1 : 0);
48877c478bd9Sstevel@tonic-gate 		if (tolower(line[0]) == 'y')
48887c478bd9Sstevel@tonic-gate 			return (1);
48897c478bd9Sstevel@tonic-gate 		if (tolower(line[0]) == 'n')
48907c478bd9Sstevel@tonic-gate 			return (0);
48917c478bd9Sstevel@tonic-gate 	}
48927c478bd9Sstevel@tonic-gate }
48937c478bd9Sstevel@tonic-gate 
4894ff17c8bfSgjelinek /* ARGSUSED */
48957c478bd9Sstevel@tonic-gate static int
uninstall_func(int argc,char * argv[])48967c478bd9Sstevel@tonic-gate uninstall_func(int argc, char *argv[])
48977c478bd9Sstevel@tonic-gate {
48987c478bd9Sstevel@tonic-gate 	char line[ZONENAME_MAX + 128];	/* Enough for "Are you sure ..." */
48990b5de56dSgjelinek 	char rootpath[MAXPATHLEN], zonepath[MAXPATHLEN];
490037774979Sgjelinek 	char cmdbuf[MAXPATHLEN];
4901ff17c8bfSgjelinek 	char precmdbuf[MAXPATHLEN];
49027c478bd9Sstevel@tonic-gate 	boolean_t force = B_FALSE;
49037c478bd9Sstevel@tonic-gate 	int lockfd, answer;
49047c478bd9Sstevel@tonic-gate 	int err, arg;
4905ff17c8bfSgjelinek 	boolean_t brand_help = B_FALSE;
490637774979Sgjelinek 	brand_handle_t bh = NULL;
4907ff17c8bfSgjelinek 	int status;
49087c478bd9Sstevel@tonic-gate 
4909108322fbScarlsonj 	if (zonecfg_in_alt_root()) {
4910108322fbScarlsonj 		zerror(gettext("cannot uninstall zone in alternate root"));
4911108322fbScarlsonj 		return (Z_ERR);
4912108322fbScarlsonj 	}
4913108322fbScarlsonj 
4914ff17c8bfSgjelinek 	/* Check the argv string for args we handle internally */
49157c478bd9Sstevel@tonic-gate 	optind = 0;
4916ff17c8bfSgjelinek 	opterr = 0;
49177c478bd9Sstevel@tonic-gate 	while ((arg = getopt(argc, argv, "?F")) != EOF) {
49187c478bd9Sstevel@tonic-gate 		switch (arg) {
49197c478bd9Sstevel@tonic-gate 		case '?':
4920ff17c8bfSgjelinek 			if (optopt == '?') {
49217c478bd9Sstevel@tonic-gate 				sub_usage(SHELP_UNINSTALL, CMD_UNINSTALL);
4922ff17c8bfSgjelinek 				brand_help = B_TRUE;
4923ff17c8bfSgjelinek 			}
4924ff17c8bfSgjelinek 			/* Ignore unknown options - may be brand specific. */
4925ff17c8bfSgjelinek 			break;
49267c478bd9Sstevel@tonic-gate 		case 'F':
49277c478bd9Sstevel@tonic-gate 			force = B_TRUE;
49287c478bd9Sstevel@tonic-gate 			break;
49297c478bd9Sstevel@tonic-gate 		default:
4930ff17c8bfSgjelinek 			/* Ignore unknown options - may be brand specific. */
4931ff17c8bfSgjelinek 			break;
49327c478bd9Sstevel@tonic-gate 		}
49337c478bd9Sstevel@tonic-gate 	}
49347c478bd9Sstevel@tonic-gate 
4935ff17c8bfSgjelinek 	if (!brand_help) {
4936ff17c8bfSgjelinek 		if (sanity_check(target_zone, CMD_UNINSTALL, B_FALSE, B_TRUE,
4937ff17c8bfSgjelinek 		    B_FALSE) != Z_OK)
49387c478bd9Sstevel@tonic-gate 			return (Z_ERR);
49397c478bd9Sstevel@tonic-gate 
4940ce28b40eSzt129084 		/*
4941ce28b40eSzt129084 		 * Invoke brand-specific handler.
4942ce28b40eSzt129084 		 */
4943ce28b40eSzt129084 		if (invoke_brand_handler(CMD_UNINSTALL, argv) != Z_OK)
4944ce28b40eSzt129084 			return (Z_ERR);
4945ce28b40eSzt129084 
49467c478bd9Sstevel@tonic-gate 		if (!force) {
49477c478bd9Sstevel@tonic-gate 			(void) snprintf(line, sizeof (line),
49487c478bd9Sstevel@tonic-gate 			    gettext("Are you sure you want to %s zone %s"),
49497c478bd9Sstevel@tonic-gate 			    cmd_to_str(CMD_UNINSTALL), target_zone);
49507c478bd9Sstevel@tonic-gate 			if ((answer = ask_yesno(B_FALSE, line)) == 0) {
49517c478bd9Sstevel@tonic-gate 				return (Z_OK);
49527c478bd9Sstevel@tonic-gate 			} else if (answer == -1) {
49537c478bd9Sstevel@tonic-gate 				zerror(gettext("Input not from terminal and -F "
49547c478bd9Sstevel@tonic-gate 				    "not specified: %s not done."),
49557c478bd9Sstevel@tonic-gate 				    cmd_to_str(CMD_UNINSTALL));
49567c478bd9Sstevel@tonic-gate 				return (Z_ERR);
49577c478bd9Sstevel@tonic-gate 			}
49587c478bd9Sstevel@tonic-gate 		}
4959ff17c8bfSgjelinek 	}
49607c478bd9Sstevel@tonic-gate 
49610b5de56dSgjelinek 	if ((err = zone_get_zonepath(target_zone, zonepath,
49620b5de56dSgjelinek 	    sizeof (zonepath))) != Z_OK) {
49637c478bd9Sstevel@tonic-gate 		errno = err;
49647c478bd9Sstevel@tonic-gate 		zperror2(target_zone, gettext("could not get zone path"));
49657c478bd9Sstevel@tonic-gate 		return (Z_ERR);
49667c478bd9Sstevel@tonic-gate 	}
4967ff17c8bfSgjelinek 
4968ff17c8bfSgjelinek 	/*
4969ff17c8bfSgjelinek 	 * Fetch the uninstall and preuninstall hooks from the brand
4970ff17c8bfSgjelinek 	 * configuration.
4971ff17c8bfSgjelinek 	 */
4972ff17c8bfSgjelinek 	if ((bh = brand_open(target_brand)) == NULL) {
4973ff17c8bfSgjelinek 		zerror(gettext("missing or invalid brand"));
4974ff17c8bfSgjelinek 		return (Z_ERR);
4975ff17c8bfSgjelinek 	}
4976ff17c8bfSgjelinek 
4977ff17c8bfSgjelinek 	if (get_hook(bh, cmdbuf, sizeof (cmdbuf), brand_get_uninstall,
4978ff17c8bfSgjelinek 	    target_zone, zonepath) != Z_OK) {
4979ff17c8bfSgjelinek 		zerror("invalid brand configuration: missing uninstall "
4980ff17c8bfSgjelinek 		    "resource");
4981ff17c8bfSgjelinek 		brand_close(bh);
4982ff17c8bfSgjelinek 		return (Z_ERR);
4983ff17c8bfSgjelinek 	}
4984ff17c8bfSgjelinek 
4985ff17c8bfSgjelinek 	if (get_hook(bh, precmdbuf, sizeof (precmdbuf), brand_get_preuninstall,
4986ff17c8bfSgjelinek 	    target_zone, zonepath) != Z_OK) {
4987ff17c8bfSgjelinek 		zerror("invalid brand configuration: missing preuninstall "
4988ff17c8bfSgjelinek 		    "resource");
4989ff17c8bfSgjelinek 		brand_close(bh);
4990ff17c8bfSgjelinek 		return (Z_ERR);
4991ff17c8bfSgjelinek 	}
4992ff17c8bfSgjelinek 	brand_close(bh);
4993ff17c8bfSgjelinek 
4994ff17c8bfSgjelinek 	/* Append all options to preuninstall hook. */
4995ff17c8bfSgjelinek 	if (addoptions(precmdbuf, argv, sizeof (precmdbuf)) != Z_OK)
4996ff17c8bfSgjelinek 		return (Z_ERR);
4997ff17c8bfSgjelinek 
4998ff17c8bfSgjelinek 	/* Append all options to uninstall hook. */
4999ff17c8bfSgjelinek 	if (addoptions(cmdbuf, argv, sizeof (cmdbuf)) != Z_OK)
5000ff17c8bfSgjelinek 		return (Z_ERR);
5001ff17c8bfSgjelinek 
5002ff17c8bfSgjelinek 	if (!brand_help) {
50037c478bd9Sstevel@tonic-gate 		if ((err = zone_get_rootpath(target_zone, rootpath,
50047c478bd9Sstevel@tonic-gate 		    sizeof (rootpath))) != Z_OK) {
50057c478bd9Sstevel@tonic-gate 			errno = err;
5006ff17c8bfSgjelinek 			zperror2(target_zone, gettext("could not get root "
5007ff17c8bfSgjelinek 			    "path"));
50087c478bd9Sstevel@tonic-gate 			return (Z_ERR);
50097c478bd9Sstevel@tonic-gate 		}
50107c478bd9Sstevel@tonic-gate 
50117c478bd9Sstevel@tonic-gate 		/*
5012ff17c8bfSgjelinek 		 * If there seems to be a zoneadmd running for this zone, call
5013ff17c8bfSgjelinek 		 * it to tell it that an uninstall is happening; if all goes
5014ff17c8bfSgjelinek 		 * well it will then shut itself down.
50157c478bd9Sstevel@tonic-gate 		 */
5016ff17c8bfSgjelinek 		if (zonecfg_ping_zoneadmd(target_zone) == Z_OK) {
50177c478bd9Sstevel@tonic-gate 			zone_cmd_arg_t zarg;
50187c478bd9Sstevel@tonic-gate 			zarg.cmd = Z_NOTE_UNINSTALLING;
5019ff17c8bfSgjelinek 			/* we don't care too much if this fails, just plow on */
5020ff17c8bfSgjelinek 			(void) zonecfg_call_zoneadmd(target_zone, &zarg, locale,
5021ff17c8bfSgjelinek 			    B_TRUE);
50227c478bd9Sstevel@tonic-gate 		}
50237c478bd9Sstevel@tonic-gate 
5024ff17c8bfSgjelinek 		if (zonecfg_grab_lock_file(target_zone, &lockfd) != Z_OK) {
5025ff17c8bfSgjelinek 			zerror(gettext("another %s may have an operation in "
5026ff17c8bfSgjelinek 			    "progress."), "zoneadm");
50277c478bd9Sstevel@tonic-gate 			return (Z_ERR);
50287c478bd9Sstevel@tonic-gate 		}
50297c478bd9Sstevel@tonic-gate 
50307c478bd9Sstevel@tonic-gate 		/* Don't uninstall the zone if anything is mounted there */
50317c478bd9Sstevel@tonic-gate 		err = zonecfg_find_mounts(rootpath, NULL, NULL);
50327c478bd9Sstevel@tonic-gate 		if (err) {
50330b5de56dSgjelinek 			zerror(gettext("These file systems are mounted on "
50347c478bd9Sstevel@tonic-gate 			    "subdirectories of %s.\n"), rootpath);
50357c478bd9Sstevel@tonic-gate 			(void) zonecfg_find_mounts(rootpath, zfm_print, NULL);
5036ff17c8bfSgjelinek 			zonecfg_release_lock_file(target_zone, lockfd);
50377c478bd9Sstevel@tonic-gate 			return (Z_ERR);
50387c478bd9Sstevel@tonic-gate 		}
503937774979Sgjelinek 	}
504037774979Sgjelinek 
5041ff17c8bfSgjelinek 	/* If we have a brand preuninstall hook, run it. */
5042ff17c8bfSgjelinek 	if (!brand_help && precmdbuf[0] != '\0') {
5043ab5dfd5eS 		status = do_subproc(precmdbuf);
504437774979Sgjelinek 		if (subproc_status(gettext("brand-specific preuninstall"),
504537774979Sgjelinek 		    status, B_FALSE) != ZONE_SUBPROC_OK) {
5046ff17c8bfSgjelinek 			zonecfg_release_lock_file(target_zone, lockfd);
504737774979Sgjelinek 			return (Z_ERR);
504837774979Sgjelinek 		}
504937774979Sgjelinek 	}
505037774979Sgjelinek 
5051ff17c8bfSgjelinek 	if (!brand_help) {
50527c478bd9Sstevel@tonic-gate 		err = zone_set_state(target_zone, ZONE_STATE_INCOMPLETE);
50537c478bd9Sstevel@tonic-gate 		if (err != Z_OK) {
50547c478bd9Sstevel@tonic-gate 			errno = err;
50557c478bd9Sstevel@tonic-gate 			zperror2(target_zone, gettext("could not set state"));
50567c478bd9Sstevel@tonic-gate 			goto bad;
50577c478bd9Sstevel@tonic-gate 		}
5058ff17c8bfSgjelinek 	}
50597c478bd9Sstevel@tonic-gate 
5060ff17c8bfSgjelinek 	/*
5061ff17c8bfSgjelinek 	 * If there is a brand uninstall hook, use it, otherwise use the
5062ff17c8bfSgjelinek 	 * built-in uninstall code.
5063ff17c8bfSgjelinek 	 */
5064ff17c8bfSgjelinek 	if (cmdbuf[0] != '\0') {
5065ff17c8bfSgjelinek 		/* Run the uninstall hook */
5066c75cc341S 		status = do_subproc(cmdbuf);
5067ff17c8bfSgjelinek 		if ((status = subproc_status(gettext("brand-specific "
5068ff17c8bfSgjelinek 		    "uninstall"), status, B_FALSE)) != ZONE_SUBPROC_OK) {
5069ff17c8bfSgjelinek 			if (status == ZONE_SUBPROC_USAGE && !brand_help)
5070ff17c8bfSgjelinek 				sub_usage(SHELP_UNINSTALL, CMD_UNINSTALL);
5071ff17c8bfSgjelinek 			if (!brand_help)
5072ff17c8bfSgjelinek 				zonecfg_release_lock_file(target_zone, lockfd);
5073ff17c8bfSgjelinek 			return (Z_ERR);
5074ff17c8bfSgjelinek 		}
5075ff17c8bfSgjelinek 
5076ff17c8bfSgjelinek 		if (brand_help)
5077ff17c8bfSgjelinek 			return (Z_OK);
5078ff17c8bfSgjelinek 	} else {
5079ff17c8bfSgjelinek 		/* If just help, we're done since there is no brand help. */
5080ff17c8bfSgjelinek 		if (brand_help)
5081ff17c8bfSgjelinek 			return (Z_OK);
5082ff17c8bfSgjelinek 
5083ff17c8bfSgjelinek 		/* Run the built-in uninstall support. */
50840b5de56dSgjelinek 		if ((err = cleanup_zonepath(zonepath, B_FALSE)) != Z_OK) {
50850b5de56dSgjelinek 			errno = err;
5086ff17c8bfSgjelinek 			zperror2(target_zone, gettext("cleaning up zonepath "
5087ff17c8bfSgjelinek 			    "failed"));
50887c478bd9Sstevel@tonic-gate 			goto bad;
50890b5de56dSgjelinek 		}
5090ff17c8bfSgjelinek 	}
50910b5de56dSgjelinek 
50927c478bd9Sstevel@tonic-gate 	err = zone_set_state(target_zone, ZONE_STATE_CONFIGURED);
50937c478bd9Sstevel@tonic-gate 	if (err != Z_OK) {
50947c478bd9Sstevel@tonic-gate 		errno = err;
50957c478bd9Sstevel@tonic-gate 		zperror2(target_zone, gettext("could not reset state"));
50967c478bd9Sstevel@tonic-gate 	}
50977c478bd9Sstevel@tonic-gate bad:
5098ff17c8bfSgjelinek 	zonecfg_release_lock_file(target_zone, lockfd);
50997c478bd9Sstevel@tonic-gate 	return (err);
51007c478bd9Sstevel@tonic-gate }
51017c478bd9Sstevel@tonic-gate 
5102108322fbScarlsonj /* ARGSUSED */
5103108322fbScarlsonj static int
mount_func(int argc,char * argv[])5104108322fbScarlsonj mount_func(int argc, char *argv[])
5105108322fbScarlsonj {
5106108322fbScarlsonj 	zone_cmd_arg_t zarg;
51079acbbeafSnn35248 	boolean_t force = B_FALSE;
51089acbbeafSnn35248 	int arg;
5109108322fbScarlsonj 
51109acbbeafSnn35248 	/*
51119acbbeafSnn35248 	 * The only supported subargument to the "mount" subcommand is
51129acbbeafSnn35248 	 * "-f", which forces us to mount a zone in the INCOMPLETE state.
51139acbbeafSnn35248 	 */
51149acbbeafSnn35248 	optind = 0;
51159acbbeafSnn35248 	if ((arg = getopt(argc, argv, "f")) != EOF) {
51169acbbeafSnn35248 		switch (arg) {
51179acbbeafSnn35248 		case 'f':
51189acbbeafSnn35248 			force = B_TRUE;
51199acbbeafSnn35248 			break;
51209acbbeafSnn35248 		default:
5121108322fbScarlsonj 			return (Z_USAGE);
51229acbbeafSnn35248 		}
51239acbbeafSnn35248 	}
51249acbbeafSnn35248 	if (argc > optind)
51259acbbeafSnn35248 		return (Z_USAGE);
51269acbbeafSnn35248 
51279acbbeafSnn35248 	if (sanity_check(target_zone, CMD_MOUNT, B_FALSE, B_FALSE, force)
51289acbbeafSnn35248 	    != Z_OK)
5129108322fbScarlsonj 		return (Z_ERR);
5130ce28b40eSzt129084 	if (verify_details(CMD_MOUNT, argv) != Z_OK)
5131108322fbScarlsonj 		return (Z_ERR);
5132108322fbScarlsonj 
51339acbbeafSnn35248 	zarg.cmd = force ? Z_FORCEMOUNT : Z_MOUNT;
51346cfd72c6Sgjelinek 	zarg.bootbuf[0] = '\0';
5135ff17c8bfSgjelinek 	if (zonecfg_call_zoneadmd(target_zone, &zarg, locale, B_TRUE) != 0) {
5136108322fbScarlsonj 		zerror(gettext("call to %s failed"), "zoneadmd");
5137108322fbScarlsonj 		return (Z_ERR);
5138108322fbScarlsonj 	}
5139108322fbScarlsonj 	return (Z_OK);
5140108322fbScarlsonj }
5141108322fbScarlsonj 
5142108322fbScarlsonj /* ARGSUSED */
5143108322fbScarlsonj static int
unmount_func(int argc,char * argv[])5144108322fbScarlsonj unmount_func(int argc, char *argv[])
5145108322fbScarlsonj {
5146108322fbScarlsonj 	zone_cmd_arg_t zarg;
5147108322fbScarlsonj 
5148108322fbScarlsonj 	if (argc > 0)
5149108322fbScarlsonj 		return (Z_USAGE);
51509acbbeafSnn35248 	if (sanity_check(target_zone, CMD_UNMOUNT, B_FALSE, B_FALSE, B_FALSE)
51519acbbeafSnn35248 	    != Z_OK)
5152108322fbScarlsonj 		return (Z_ERR);
5153108322fbScarlsonj 
5154108322fbScarlsonj 	zarg.cmd = Z_UNMOUNT;
5155ff17c8bfSgjelinek 	if (zonecfg_call_zoneadmd(target_zone, &zarg, locale, B_TRUE) != 0) {
5156108322fbScarlsonj 		zerror(gettext("call to %s failed"), "zoneadmd");
5157108322fbScarlsonj 		return (Z_ERR);
5158108322fbScarlsonj 	}
5159108322fbScarlsonj 	return (Z_OK);
5160108322fbScarlsonj }
5161108322fbScarlsonj 
51627c478bd9Sstevel@tonic-gate static int
mark_func(int argc,char * argv[])5163555afedfScarlsonj mark_func(int argc, char *argv[])
5164555afedfScarlsonj {
5165555afedfScarlsonj 	int err, lockfd;
5166edfa49ffS 	int arg;
5167edfa49ffS 	boolean_t force = B_FALSE;
5168edfa49ffS 	int state;
5169555afedfScarlsonj 
5170edfa49ffS 	optind = 0;
5171edfa49ffS 	opterr = 0;
5172edfa49ffS 	while ((arg = getopt(argc, argv, "F")) != EOF) {
5173edfa49ffS 		switch (arg) {
5174edfa49ffS 		case 'F':
5175edfa49ffS 			force = B_TRUE;
5176edfa49ffS 			break;
5177edfa49ffS 		default:
5178555afedfScarlsonj 			return (Z_USAGE);
5179edfa49ffS 		}
5180edfa49ffS 	}
5181edfa49ffS 
5182edfa49ffS 	if (argc != (optind + 1))
5183edfa49ffS 		return (Z_USAGE);
5184edfa49ffS 
5185edfa49ffS 	if (strcmp(argv[optind], "configured") == 0)
5186edfa49ffS 		state = ZONE_STATE_CONFIGURED;
5187edfa49ffS 	else if (strcmp(argv[optind], "incomplete") == 0)
5188edfa49ffS 		state = ZONE_STATE_INCOMPLETE;
5189edfa49ffS 	else if (strcmp(argv[optind], "installed") == 0)
5190edfa49ffS 		state = ZONE_STATE_INSTALLED;
5191edfa49ffS 	else
5192edfa49ffS 		return (Z_USAGE);
5193edfa49ffS 
5194edfa49ffS 	if (state != ZONE_STATE_INCOMPLETE && !force)
5195edfa49ffS 		return (Z_USAGE);
5196edfa49ffS 
5197edfa49ffS 	if (sanity_check(target_zone, CMD_MARK, B_FALSE, B_TRUE, B_FALSE)
51989acbbeafSnn35248 	    != Z_OK)
5199555afedfScarlsonj 		return (Z_ERR);
5200555afedfScarlsonj 
5201ce28b40eSzt129084 	/*
5202ce28b40eSzt129084 	 * Invoke brand-specific handler.
5203ce28b40eSzt129084 	 */
5204ce28b40eSzt129084 	if (invoke_brand_handler(CMD_MARK, argv) != Z_OK)
5205ce28b40eSzt129084 		return (Z_ERR);
5206ce28b40eSzt129084 
5207ff17c8bfSgjelinek 	if (zonecfg_grab_lock_file(target_zone, &lockfd) != Z_OK) {
5208555afedfScarlsonj 		zerror(gettext("another %s may have an operation in progress."),
5209555afedfScarlsonj 		    "zoneadm");
5210555afedfScarlsonj 		return (Z_ERR);
5211555afedfScarlsonj 	}
5212555afedfScarlsonj 
5213edfa49ffS 	err = zone_set_state(target_zone, state);
5214555afedfScarlsonj 	if (err != Z_OK) {
5215555afedfScarlsonj 		errno = err;
5216555afedfScarlsonj 		zperror2(target_zone, gettext("could not set state"));
5217555afedfScarlsonj 	}
5218ff17c8bfSgjelinek 	zonecfg_release_lock_file(target_zone, lockfd);
5219555afedfScarlsonj 
5220555afedfScarlsonj 	return (err);
5221555afedfScarlsonj }
5222555afedfScarlsonj 
52230209230bSgjelinek /*
52240209230bSgjelinek  * Check what scheduling class we're running under and print a warning if
52250209230bSgjelinek  * we're not using FSS.
52260209230bSgjelinek  */
52270209230bSgjelinek static int
check_sched_fss(zone_dochandle_t handle)52280209230bSgjelinek check_sched_fss(zone_dochandle_t handle)
52290209230bSgjelinek {
52300209230bSgjelinek 	char class_name[PC_CLNMSZ];
52310209230bSgjelinek 
52320209230bSgjelinek 	if (zonecfg_get_dflt_sched_class(handle, class_name,
52330209230bSgjelinek 	    sizeof (class_name)) != Z_OK) {
52340209230bSgjelinek 		zerror(gettext("WARNING: unable to determine the zone's "
52350209230bSgjelinek 		    "scheduling class"));
52360209230bSgjelinek 	} else if (strcmp("FSS", class_name) != 0) {
52370209230bSgjelinek 		zerror(gettext("WARNING: The zone.cpu-shares rctl is set but\n"
52380209230bSgjelinek 		    "FSS is not the default scheduling class for this zone.  "
52390209230bSgjelinek 		    "FSS will be\nused for processes in the zone but to get "
52400209230bSgjelinek 		    "the full benefit of FSS,\nit should be the default "
52410209230bSgjelinek 		    "scheduling class.  See dispadmin(1M) for\nmore details."));
52420209230bSgjelinek 		return (Z_SYSTEM);
52430209230bSgjelinek 	}
52440209230bSgjelinek 
52450209230bSgjelinek 	return (Z_OK);
52460209230bSgjelinek }
52470209230bSgjelinek 
52480209230bSgjelinek static int
check_cpu_shares_sched(zone_dochandle_t handle)52490209230bSgjelinek check_cpu_shares_sched(zone_dochandle_t handle)
52500209230bSgjelinek {
52510209230bSgjelinek 	int err;
52520209230bSgjelinek 	int res = Z_OK;
52530209230bSgjelinek 	struct zone_rctltab rctl;
52540209230bSgjelinek 
52550209230bSgjelinek 	if ((err = zonecfg_setrctlent(handle)) != Z_OK) {
52560209230bSgjelinek 		errno = err;
52570209230bSgjelinek 		zperror(cmd_to_str(CMD_APPLY), B_TRUE);
52580209230bSgjelinek 		return (err);
52590209230bSgjelinek 	}
52600209230bSgjelinek 
52610209230bSgjelinek 	while (zonecfg_getrctlent(handle, &rctl) == Z_OK) {
52620209230bSgjelinek 		if (strcmp(rctl.zone_rctl_name, "zone.cpu-shares") == 0) {
52630209230bSgjelinek 			if (check_sched_fss(handle) != Z_OK)
52640209230bSgjelinek 				res = Z_SYSTEM;
52650209230bSgjelinek 			break;
52660209230bSgjelinek 		}
52670209230bSgjelinek 	}
52680209230bSgjelinek 
52690209230bSgjelinek 	(void) zonecfg_endrctlent(handle);
52700209230bSgjelinek 
52710209230bSgjelinek 	return (res);
52720209230bSgjelinek }
52730209230bSgjelinek 
52740209230bSgjelinek /*
52757ef01d19Sgjelinek  * Check if there is a mix of processes running in different pools within the
52767ef01d19Sgjelinek  * zone.  This is currently only going to be called for the global zone from
52777ef01d19Sgjelinek  * apply_func but that could be generalized in the future.
52787ef01d19Sgjelinek  */
52797ef01d19Sgjelinek static boolean_t
mixed_pools(zoneid_t zoneid)52807ef01d19Sgjelinek mixed_pools(zoneid_t zoneid)
52817ef01d19Sgjelinek {
52827ef01d19Sgjelinek 	DIR *dirp;
52837ef01d19Sgjelinek 	dirent_t *dent;
52847ef01d19Sgjelinek 	boolean_t mixed = B_FALSE;
52857ef01d19Sgjelinek 	boolean_t poolid_set = B_FALSE;
52867ef01d19Sgjelinek 	poolid_t last_poolid = 0;
52877ef01d19Sgjelinek 
52887ef01d19Sgjelinek 	if ((dirp = opendir("/proc")) == NULL) {
52897ef01d19Sgjelinek 		zerror(gettext("could not open /proc"));
52907ef01d19Sgjelinek 		return (B_FALSE);
52917ef01d19Sgjelinek 	}
52927ef01d19Sgjelinek 
52937ef01d19Sgjelinek 	while ((dent = readdir(dirp)) != NULL) {
52947ef01d19Sgjelinek 		int procfd;
52957ef01d19Sgjelinek 		psinfo_t ps;
52967ef01d19Sgjelinek 		char procpath[MAXPATHLEN];
52977ef01d19Sgjelinek 
52987ef01d19Sgjelinek 		if (dent->d_name[0] == '.')
52997ef01d19Sgjelinek 			continue;
53007ef01d19Sgjelinek 
53017ef01d19Sgjelinek 		(void) snprintf(procpath, sizeof (procpath), "/proc/%s/psinfo",
53027ef01d19Sgjelinek 		    dent->d_name);
53037ef01d19Sgjelinek 
53047ef01d19Sgjelinek 		if ((procfd = open(procpath, O_RDONLY)) == -1)
53057ef01d19Sgjelinek 			continue;
53067ef01d19Sgjelinek 
53077ef01d19Sgjelinek 		if (read(procfd, &ps, sizeof (ps)) == sizeof (psinfo_t)) {
53087ef01d19Sgjelinek 			/* skip processes in other zones and system processes */
53097ef01d19Sgjelinek 			if (zoneid != ps.pr_zoneid || ps.pr_flag & SSYS) {
53107ef01d19Sgjelinek 				(void) close(procfd);
53117ef01d19Sgjelinek 				continue;
53127ef01d19Sgjelinek 			}
53137ef01d19Sgjelinek 
53147ef01d19Sgjelinek 			if (poolid_set) {
53157ef01d19Sgjelinek 				if (ps.pr_poolid != last_poolid)
53167ef01d19Sgjelinek 					mixed = B_TRUE;
53177ef01d19Sgjelinek 			} else {
53187ef01d19Sgjelinek 				last_poolid = ps.pr_poolid;
53197ef01d19Sgjelinek 				poolid_set = B_TRUE;
53207ef01d19Sgjelinek 			}
53217ef01d19Sgjelinek 		}
53227ef01d19Sgjelinek 
53237ef01d19Sgjelinek 		(void) close(procfd);
53247ef01d19Sgjelinek 
53257ef01d19Sgjelinek 		if (mixed)
53267ef01d19Sgjelinek 			break;
53277ef01d19Sgjelinek 	}
53287ef01d19Sgjelinek 
53297ef01d19Sgjelinek 	(void) closedir(dirp);
53307ef01d19Sgjelinek 
53317ef01d19Sgjelinek 	return (mixed);
53327ef01d19Sgjelinek }
53337ef01d19Sgjelinek 
53347ef01d19Sgjelinek /*
53357ef01d19Sgjelinek  * Check if a persistent or temporary pool is configured for the zone.
53367ef01d19Sgjelinek  * This is currently only going to be called for the global zone from
53377ef01d19Sgjelinek  * apply_func but that could be generalized in the future.
53387ef01d19Sgjelinek  */
53397ef01d19Sgjelinek static boolean_t
pool_configured(zone_dochandle_t handle)53407ef01d19Sgjelinek pool_configured(zone_dochandle_t handle)
53417ef01d19Sgjelinek {
53427ef01d19Sgjelinek 	int err1, err2;
53437ef01d19Sgjelinek 	struct zone_psettab pset_tab;
53447ef01d19Sgjelinek 	char poolname[MAXPATHLEN];
53457ef01d19Sgjelinek 
53467ef01d19Sgjelinek 	err1 = zonecfg_lookup_pset(handle, &pset_tab);
53477ef01d19Sgjelinek 	err2 = zonecfg_get_pool(handle, poolname, sizeof (poolname));
53487ef01d19Sgjelinek 
53497ef01d19Sgjelinek 	if (err1 == Z_NO_ENTRY &&
53507ef01d19Sgjelinek 	    (err2 == Z_NO_ENTRY || (err2 == Z_OK && strlen(poolname) == 0)))
53517ef01d19Sgjelinek 		return (B_FALSE);
53527ef01d19Sgjelinek 
53537ef01d19Sgjelinek 	return (B_TRUE);
53547ef01d19Sgjelinek }
53557ef01d19Sgjelinek 
53567ef01d19Sgjelinek /*
53570209230bSgjelinek  * This is an undocumented interface which is currently only used to apply
53580209230bSgjelinek  * the global zone resource management settings when the system boots.
53590209230bSgjelinek  * This function does not yet properly handle updating a running system so
53600209230bSgjelinek  * any projects running in the zone would be trashed if this function
53610209230bSgjelinek  * were to run after the zone had booted.  It also does not reset any
53620209230bSgjelinek  * rctl settings that were removed from zonecfg.  There is still work to be
53630209230bSgjelinek  * done before we can properly support dynamically updating the resource
53640209230bSgjelinek  * management settings for a running zone (global or non-global).  Thus, this
53650209230bSgjelinek  * functionality is undocumented for now.
53660209230bSgjelinek  */
53670209230bSgjelinek /* ARGSUSED */
53680209230bSgjelinek static int
apply_func(int argc,char * argv[])53690209230bSgjelinek apply_func(int argc, char *argv[])
53700209230bSgjelinek {
53710209230bSgjelinek 	int err;
53720209230bSgjelinek 	int res = Z_OK;
53730209230bSgjelinek 	priv_set_t *privset;
53740209230bSgjelinek 	zoneid_t zoneid;
53750209230bSgjelinek 	zone_dochandle_t handle;
53760209230bSgjelinek 	struct zone_mcaptab mcap;
53770209230bSgjelinek 	char pool_err[128];
53780209230bSgjelinek 
53790209230bSgjelinek 	zoneid = getzoneid();
53800209230bSgjelinek 
53810209230bSgjelinek 	if (zonecfg_in_alt_root() || zoneid != GLOBAL_ZONEID ||
53820209230bSgjelinek 	    target_zone == NULL || strcmp(target_zone, GLOBAL_ZONENAME) != 0)
53830209230bSgjelinek 		return (usage(B_FALSE));
53840209230bSgjelinek 
53850209230bSgjelinek 	if ((privset = priv_allocset()) == NULL) {
53860209230bSgjelinek 		zerror(gettext("%s failed"), "priv_allocset");
53870209230bSgjelinek 		return (Z_ERR);
53880209230bSgjelinek 	}
53890209230bSgjelinek 
53900209230bSgjelinek 	if (getppriv(PRIV_EFFECTIVE, privset) != 0) {
53910209230bSgjelinek 		zerror(gettext("%s failed"), "getppriv");
53920209230bSgjelinek 		priv_freeset(privset);
53930209230bSgjelinek 		return (Z_ERR);
53940209230bSgjelinek 	}
53950209230bSgjelinek 
53960209230bSgjelinek 	if (priv_isfullset(privset) == B_FALSE) {
53970209230bSgjelinek 		(void) usage(B_FALSE);
53980209230bSgjelinek 		priv_freeset(privset);
53990209230bSgjelinek 		return (Z_ERR);
54000209230bSgjelinek 	}
54010209230bSgjelinek 	priv_freeset(privset);
54020209230bSgjelinek 
54030209230bSgjelinek 	if ((handle = zonecfg_init_handle()) == NULL) {
54040209230bSgjelinek 		zperror(cmd_to_str(CMD_APPLY), B_TRUE);
54050209230bSgjelinek 		return (Z_ERR);
54060209230bSgjelinek 	}
54070209230bSgjelinek 
54080209230bSgjelinek 	if ((err = zonecfg_get_handle(target_zone, handle)) != Z_OK) {
54090209230bSgjelinek 		errno = err;
54100209230bSgjelinek 		zperror(cmd_to_str(CMD_APPLY), B_TRUE);
54110209230bSgjelinek 		zonecfg_fini_handle(handle);
54120209230bSgjelinek 		return (Z_ERR);
54130209230bSgjelinek 	}
54140209230bSgjelinek 
54150209230bSgjelinek 	/* specific error msgs are printed within apply_rctls */
54160209230bSgjelinek 	if ((err = zonecfg_apply_rctls(target_zone, handle)) != Z_OK) {
54170209230bSgjelinek 		errno = err;
54180209230bSgjelinek 		zperror(cmd_to_str(CMD_APPLY), B_TRUE);
54190209230bSgjelinek 		res = Z_ERR;
54200209230bSgjelinek 	}
54210209230bSgjelinek 
54220209230bSgjelinek 	if ((err = check_cpu_shares_sched(handle)) != Z_OK)
54230209230bSgjelinek 		res = Z_ERR;
54240209230bSgjelinek 
54257ef01d19Sgjelinek 	if (pool_configured(handle)) {
54267ef01d19Sgjelinek 		if (mixed_pools(zoneid)) {
54277ef01d19Sgjelinek 			zerror(gettext("Zone is using multiple resource "
54287ef01d19Sgjelinek 			    "pools.  The pool\nconfiguration cannot be "
54297ef01d19Sgjelinek 			    "applied without rebooting."));
54307ef01d19Sgjelinek 			res = Z_ERR;
54317ef01d19Sgjelinek 		} else {
54327ef01d19Sgjelinek 
54330209230bSgjelinek 			/*
54347ef01d19Sgjelinek 			 * The next two blocks of code attempt to set up
54357ef01d19Sgjelinek 			 * temporary pools as well as persistent pools.  In
54367ef01d19Sgjelinek 			 * both cases we call the functions unconditionally.
54377ef01d19Sgjelinek 			 * Within each funtion the code will check if the zone
54387ef01d19Sgjelinek 			 * is actually configured for a temporary pool or
54397ef01d19Sgjelinek 			 * persistent pool and just return if there is nothing
54407ef01d19Sgjelinek 			 * to do.
54410209230bSgjelinek 			 */
54427ef01d19Sgjelinek 			if ((err = zonecfg_bind_tmp_pool(handle, zoneid,
54437ef01d19Sgjelinek 			    pool_err, sizeof (pool_err))) != Z_OK) {
54447ef01d19Sgjelinek 				if (err == Z_POOL || err == Z_POOL_CREATE ||
54457ef01d19Sgjelinek 				    err == Z_POOL_BIND)
54467ef01d19Sgjelinek 					zerror("%s: %s", zonecfg_strerror(err),
54477ef01d19Sgjelinek 					    pool_err);
54480209230bSgjelinek 				else
54497ef01d19Sgjelinek 					zerror(gettext("could not bind zone to "
54507ef01d19Sgjelinek 					    "temporary pool: %s"),
54517ef01d19Sgjelinek 					    zonecfg_strerror(err));
54520209230bSgjelinek 				res = Z_ERR;
54530209230bSgjelinek 			}
54540209230bSgjelinek 
54550209230bSgjelinek 			if ((err = zonecfg_bind_pool(handle, zoneid, pool_err,
54560209230bSgjelinek 			    sizeof (pool_err))) != Z_OK) {
54570209230bSgjelinek 				if (err == Z_POOL || err == Z_POOL_BIND)
54587ef01d19Sgjelinek 					zerror("%s: %s", zonecfg_strerror(err),
54597ef01d19Sgjelinek 					    pool_err);
54600209230bSgjelinek 				else
54610209230bSgjelinek 					zerror("%s", zonecfg_strerror(err));
54620209230bSgjelinek 			}
54637ef01d19Sgjelinek 		}
54647ef01d19Sgjelinek 	}
54650209230bSgjelinek 
54660209230bSgjelinek 	/*
54670209230bSgjelinek 	 * If a memory cap is configured, set the cap in the kernel using
54680209230bSgjelinek 	 * zone_setattr() and make sure the rcapd SMF service is enabled.
54690209230bSgjelinek 	 */
54700209230bSgjelinek 	if (zonecfg_getmcapent(handle, &mcap) == Z_OK) {
54710209230bSgjelinek 		uint64_t num;
54720209230bSgjelinek 		char smf_err[128];
54730209230bSgjelinek 
54740209230bSgjelinek 		num = (uint64_t)strtoll(mcap.zone_physmem_cap, NULL, 10);
54750209230bSgjelinek 		if (zone_setattr(zoneid, ZONE_ATTR_PHYS_MCAP, &num, 0) == -1) {
54760209230bSgjelinek 			zerror(gettext("could not set zone memory cap"));
54770209230bSgjelinek 			res = Z_ERR;
54780209230bSgjelinek 		}
54790209230bSgjelinek 
54800209230bSgjelinek 		if (zonecfg_enable_rcapd(smf_err, sizeof (smf_err)) != Z_OK) {
54810209230bSgjelinek 			zerror(gettext("enabling system/rcap service failed: "
54820209230bSgjelinek 			    "%s"), smf_err);
54830209230bSgjelinek 			res = Z_ERR;
54840209230bSgjelinek 		}
54850209230bSgjelinek 	}
54860209230bSgjelinek 
54870209230bSgjelinek 	zonecfg_fini_handle(handle);
54880209230bSgjelinek 
54890209230bSgjelinek 	return (res);
54900209230bSgjelinek }
54910209230bSgjelinek 
5492fbbfbc6eSjv227347 /*
5493fbbfbc6eSjv227347  * This is an undocumented interface that is invoked by the zones SMF service
5494fbbfbc6eSjv227347  * for installed zones that won't automatically boot.
5495fbbfbc6eSjv227347  */
5496fbbfbc6eSjv227347 /* ARGSUSED */
5497fbbfbc6eSjv227347 static int
sysboot_func(int argc,char * argv[])5498fbbfbc6eSjv227347 sysboot_func(int argc, char *argv[])
5499fbbfbc6eSjv227347 {
5500fbbfbc6eSjv227347 	int err;
5501fbbfbc6eSjv227347 	zone_dochandle_t zone_handle;
5502fbbfbc6eSjv227347 	brand_handle_t brand_handle;
5503fbbfbc6eSjv227347 	char cmdbuf[MAXPATHLEN];
5504fbbfbc6eSjv227347 	char zonepath[MAXPATHLEN];
5505fbbfbc6eSjv227347 
5506fbbfbc6eSjv227347 	/*
5507fbbfbc6eSjv227347 	 * This subcommand can only be executed in the global zone on non-global
5508fbbfbc6eSjv227347 	 * zones.
5509fbbfbc6eSjv227347 	 */
5510fbbfbc6eSjv227347 	if (zonecfg_in_alt_root())
5511fbbfbc6eSjv227347 		return (usage(B_FALSE));
5512fbbfbc6eSjv227347 	if (sanity_check(target_zone, CMD_SYSBOOT, B_FALSE, B_TRUE, B_FALSE) !=
5513fbbfbc6eSjv227347 	    Z_OK)
5514fbbfbc6eSjv227347 		return (Z_ERR);
5515fbbfbc6eSjv227347 
5516fbbfbc6eSjv227347 	/*
5517fbbfbc6eSjv227347 	 * Fetch the sysboot hook from the target zone's brand.
5518fbbfbc6eSjv227347 	 */
5519fbbfbc6eSjv227347 	if ((zone_handle = zonecfg_init_handle()) == NULL) {
5520fbbfbc6eSjv227347 		zperror(cmd_to_str(CMD_SYSBOOT), B_TRUE);
5521fbbfbc6eSjv227347 		return (Z_ERR);
5522fbbfbc6eSjv227347 	}
5523fbbfbc6eSjv227347 	if ((err = zonecfg_get_handle(target_zone, zone_handle)) != Z_OK) {
5524fbbfbc6eSjv227347 		errno = err;
5525fbbfbc6eSjv227347 		zperror(cmd_to_str(CMD_SYSBOOT), B_TRUE);
5526fbbfbc6eSjv227347 		zonecfg_fini_handle(zone_handle);
5527fbbfbc6eSjv227347 		return (Z_ERR);
5528fbbfbc6eSjv227347 	}
5529fbbfbc6eSjv227347 	if ((err = zonecfg_get_zonepath(zone_handle, zonepath,
5530fbbfbc6eSjv227347 	    sizeof (zonepath))) != Z_OK) {
5531fbbfbc6eSjv227347 		errno = err;
5532fbbfbc6eSjv227347 		zperror(cmd_to_str(CMD_SYSBOOT), B_TRUE);
5533fbbfbc6eSjv227347 		zonecfg_fini_handle(zone_handle);
5534fbbfbc6eSjv227347 		return (Z_ERR);
5535fbbfbc6eSjv227347 	}
5536fbbfbc6eSjv227347 	if ((brand_handle = brand_open(target_brand)) == NULL) {
5537fbbfbc6eSjv227347 		zerror(gettext("missing or invalid brand during %s operation: "
5538fbbfbc6eSjv227347 		    "%s"), cmd_to_str(CMD_SYSBOOT), target_brand);
5539fbbfbc6eSjv227347 		zonecfg_fini_handle(zone_handle);
5540fbbfbc6eSjv227347 		return (Z_ERR);
5541fbbfbc6eSjv227347 	}
5542fbbfbc6eSjv227347 	err = get_hook(brand_handle, cmdbuf, sizeof (cmdbuf), brand_get_sysboot,
5543fbbfbc6eSjv227347 	    target_zone, zonepath);
5544fbbfbc6eSjv227347 	brand_close(brand_handle);
5545fbbfbc6eSjv227347 	zonecfg_fini_handle(zone_handle);
5546fbbfbc6eSjv227347 	if (err != Z_OK) {
5547fbbfbc6eSjv227347 		zerror(gettext("unable to get brand hook from brand %s for %s "
5548fbbfbc6eSjv227347 		    "operation"), target_brand, cmd_to_str(CMD_SYSBOOT));
5549fbbfbc6eSjv227347 		return (Z_ERR);
5550fbbfbc6eSjv227347 	}
5551fbbfbc6eSjv227347 
5552fbbfbc6eSjv227347 	/*
5553fbbfbc6eSjv227347 	 * If the hook wasn't defined (which is OK), then indicate success and
5554fbbfbc6eSjv227347 	 * return.  Otherwise, execute the hook.
5555fbbfbc6eSjv227347 	 */
5556fbbfbc6eSjv227347 	if (cmdbuf[0] != '\0')
5557fbbfbc6eSjv227347 		return ((subproc_status(gettext("brand sysboot operation"),
5558fbbfbc6eSjv227347 		    do_subproc(cmdbuf), B_FALSE) == ZONE_SUBPROC_OK) ? Z_OK :
5559fbbfbc6eSjv227347 		    Z_BRAND_ERROR);
5560fbbfbc6eSjv227347 	return (Z_OK);
5561fbbfbc6eSjv227347 }
5562fbbfbc6eSjv227347 
5563555afedfScarlsonj static int
help_func(int argc,char * argv[])55647c478bd9Sstevel@tonic-gate help_func(int argc, char *argv[])
55657c478bd9Sstevel@tonic-gate {
55667c478bd9Sstevel@tonic-gate 	int arg, cmd_num;
55677c478bd9Sstevel@tonic-gate 
55687c478bd9Sstevel@tonic-gate 	if (argc == 0) {
55697c478bd9Sstevel@tonic-gate 		(void) usage(B_TRUE);
55707c478bd9Sstevel@tonic-gate 		return (Z_OK);
55717c478bd9Sstevel@tonic-gate 	}
55727c478bd9Sstevel@tonic-gate 	optind = 0;
55737c478bd9Sstevel@tonic-gate 	if ((arg = getopt(argc, argv, "?")) != EOF) {
55747c478bd9Sstevel@tonic-gate 		switch (arg) {
55757c478bd9Sstevel@tonic-gate 		case '?':
55767c478bd9Sstevel@tonic-gate 			sub_usage(SHELP_HELP, CMD_HELP);
55777c478bd9Sstevel@tonic-gate 			return (optopt == '?' ? Z_OK : Z_USAGE);
55787c478bd9Sstevel@tonic-gate 		default:
55797c478bd9Sstevel@tonic-gate 			sub_usage(SHELP_HELP, CMD_HELP);
55807c478bd9Sstevel@tonic-gate 			return (Z_USAGE);
55817c478bd9Sstevel@tonic-gate 		}
55827c478bd9Sstevel@tonic-gate 	}
55837c478bd9Sstevel@tonic-gate 	while (optind < argc) {
5584394a25e2Scarlsonj 		/* Private commands have NULL short_usage; omit them */
5585394a25e2Scarlsonj 		if ((cmd_num = cmd_match(argv[optind])) < 0 ||
5586394a25e2Scarlsonj 		    cmdtab[cmd_num].short_usage == NULL) {
55877c478bd9Sstevel@tonic-gate 			sub_usage(SHELP_HELP, CMD_HELP);
55887c478bd9Sstevel@tonic-gate 			return (Z_USAGE);
55897c478bd9Sstevel@tonic-gate 		}
55907c478bd9Sstevel@tonic-gate 		sub_usage(cmdtab[cmd_num].short_usage, cmd_num);
55917c478bd9Sstevel@tonic-gate 		optind++;
55927c478bd9Sstevel@tonic-gate 	}
55937c478bd9Sstevel@tonic-gate 	return (Z_OK);
55947c478bd9Sstevel@tonic-gate }
55957c478bd9Sstevel@tonic-gate 
55967c478bd9Sstevel@tonic-gate /*
55977c478bd9Sstevel@tonic-gate  * Returns: CMD_MIN thru CMD_MAX on success, -1 on error
55987c478bd9Sstevel@tonic-gate  */
55997c478bd9Sstevel@tonic-gate 
56007c478bd9Sstevel@tonic-gate static int
cmd_match(char * cmd)56017c478bd9Sstevel@tonic-gate cmd_match(char *cmd)
56027c478bd9Sstevel@tonic-gate {
56037c478bd9Sstevel@tonic-gate 	int i;
56047c478bd9Sstevel@tonic-gate 
56057c478bd9Sstevel@tonic-gate 	for (i = CMD_MIN; i <= CMD_MAX; i++) {
56067c478bd9Sstevel@tonic-gate 		/* return only if there is an exact match */
56077c478bd9Sstevel@tonic-gate 		if (strcmp(cmd, cmdtab[i].cmd_name) == 0)
56087c478bd9Sstevel@tonic-gate 			return (cmdtab[i].cmd_num);
56097c478bd9Sstevel@tonic-gate 	}
56107c478bd9Sstevel@tonic-gate 	return (-1);
56117c478bd9Sstevel@tonic-gate }
56127c478bd9Sstevel@tonic-gate 
56137c478bd9Sstevel@tonic-gate static int
parse_and_run(int argc,char * argv[])56147c478bd9Sstevel@tonic-gate parse_and_run(int argc, char *argv[])
56157c478bd9Sstevel@tonic-gate {
56167c478bd9Sstevel@tonic-gate 	int i = cmd_match(argv[0]);
56177c478bd9Sstevel@tonic-gate 
56187c478bd9Sstevel@tonic-gate 	if (i < 0)
56197c478bd9Sstevel@tonic-gate 		return (usage(B_FALSE));
56207c478bd9Sstevel@tonic-gate 	return (cmdtab[i].handler(argc - 1, &(argv[1])));
56217c478bd9Sstevel@tonic-gate }
56227c478bd9Sstevel@tonic-gate 
56237c478bd9Sstevel@tonic-gate static char *
get_execbasename(char * execfullname)56247c478bd9Sstevel@tonic-gate get_execbasename(char *execfullname)
56257c478bd9Sstevel@tonic-gate {
56267c478bd9Sstevel@tonic-gate 	char *last_slash, *execbasename;
56277c478bd9Sstevel@tonic-gate 
56287c478bd9Sstevel@tonic-gate 	/* guard against '/' at end of command invocation */
56297c478bd9Sstevel@tonic-gate 	for (;;) {
56307c478bd9Sstevel@tonic-gate 		last_slash = strrchr(execfullname, '/');
56317c478bd9Sstevel@tonic-gate 		if (last_slash == NULL) {
56327c478bd9Sstevel@tonic-gate 			execbasename = execfullname;
56337c478bd9Sstevel@tonic-gate 			break;
56347c478bd9Sstevel@tonic-gate 		} else {
56357c478bd9Sstevel@tonic-gate 			execbasename = last_slash + 1;
56367c478bd9Sstevel@tonic-gate 			if (*execbasename == '\0') {
56377c478bd9Sstevel@tonic-gate 				*last_slash = '\0';
56387c478bd9Sstevel@tonic-gate 				continue;
56397c478bd9Sstevel@tonic-gate 			}
56407c478bd9Sstevel@tonic-gate 			break;
56417c478bd9Sstevel@tonic-gate 		}
56427c478bd9Sstevel@tonic-gate 	}
56437c478bd9Sstevel@tonic-gate 	return (execbasename);
56447c478bd9Sstevel@tonic-gate }
56457c478bd9Sstevel@tonic-gate 
5646cb8a054bSGlenn Faden static char *
get_username()5647cb8a054bSGlenn Faden get_username()
5648cb8a054bSGlenn Faden {
5649cb8a054bSGlenn Faden 	uid_t uid;
5650cb8a054bSGlenn Faden 	struct passwd *nptr;
5651cb8a054bSGlenn Faden 
5652cb8a054bSGlenn Faden 
5653cb8a054bSGlenn Faden 	/*
5654cb8a054bSGlenn Faden 	 * Authorizations are checked to restrict access based on the
5655cb8a054bSGlenn Faden 	 * requested operation and zone name, It is assumed that the
5656cb8a054bSGlenn Faden 	 * program is running with all privileges, but that the real
5657cb8a054bSGlenn Faden 	 * user ID is that of the user or role on whose behalf we are
5658cb8a054bSGlenn Faden 	 * operating. So we start by getting the username that will be
5659cb8a054bSGlenn Faden 	 * used for subsequent authorization checks.
5660cb8a054bSGlenn Faden 	 */
5661cb8a054bSGlenn Faden 
5662cb8a054bSGlenn Faden 	uid = getuid();
5663cb8a054bSGlenn Faden 	if ((nptr = getpwuid(uid)) == NULL) {
5664cb8a054bSGlenn Faden 		zerror(gettext("could not get user name."));
5665cb8a054bSGlenn Faden 		exit(Z_ERR);
5666cb8a054bSGlenn Faden 	}
5667cb8a054bSGlenn Faden 	return (nptr->pw_name);
5668cb8a054bSGlenn Faden }
5669cb8a054bSGlenn Faden 
56707c478bd9Sstevel@tonic-gate int
main(int argc,char ** argv)56717c478bd9Sstevel@tonic-gate main(int argc, char **argv)
56727c478bd9Sstevel@tonic-gate {
56737c478bd9Sstevel@tonic-gate 	int arg;
56747c478bd9Sstevel@tonic-gate 	zoneid_t zid;
5675108322fbScarlsonj 	struct stat st;
56769acbbeafSnn35248 	char *zone_lock_env;
56779acbbeafSnn35248 	int err;
56787c478bd9Sstevel@tonic-gate 
56797c478bd9Sstevel@tonic-gate 	if ((locale = setlocale(LC_ALL, "")) == NULL)
56807c478bd9Sstevel@tonic-gate 		locale = "C";
56817c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
56827c478bd9Sstevel@tonic-gate 	setbuf(stdout, NULL);
56837c478bd9Sstevel@tonic-gate 	(void) sigset(SIGHUP, SIG_IGN);
56847c478bd9Sstevel@tonic-gate 	execname = get_execbasename(argv[0]);
5685cb8a054bSGlenn Faden 	username = get_username();
56867c478bd9Sstevel@tonic-gate 	target_zone = NULL;
56877c478bd9Sstevel@tonic-gate 	if (chdir("/") != 0) {
56887c478bd9Sstevel@tonic-gate 		zerror(gettext("could not change directory to /."));
56897c478bd9Sstevel@tonic-gate 		exit(Z_ERR);
56907c478bd9Sstevel@tonic-gate 	}
5691cb8a054bSGlenn Faden 
5692d0e4f536SSudheer A 	/*
5693d0e4f536SSudheer A 	 * Use the default system mask rather than anything that may have been
5694d0e4f536SSudheer A 	 * set by the caller.
5695d0e4f536SSudheer A 	 */
5696d0e4f536SSudheer A 	(void) umask(CMASK);
56977c478bd9Sstevel@tonic-gate 
569899653d4eSeschrock 	if (init_zfs() != Z_OK)
569999653d4eSeschrock 		exit(Z_ERR);
570099653d4eSeschrock 
5701555afedfScarlsonj 	while ((arg = getopt(argc, argv, "?u:z:R:")) != EOF) {
57027c478bd9Sstevel@tonic-gate 		switch (arg) {
57037c478bd9Sstevel@tonic-gate 		case '?':
57047c478bd9Sstevel@tonic-gate 			return (usage(B_TRUE));
5705555afedfScarlsonj 		case 'u':
5706555afedfScarlsonj 			target_uuid = optarg;
5707555afedfScarlsonj 			break;
57087c478bd9Sstevel@tonic-gate 		case 'z':
57097c478bd9Sstevel@tonic-gate 			target_zone = optarg;
57107c478bd9Sstevel@tonic-gate 			break;
5711108322fbScarlsonj 		case 'R':	/* private option for admin/install use */
5712108322fbScarlsonj 			if (*optarg != '/') {
5713108322fbScarlsonj 				zerror(gettext("root path must be absolute."));
5714108322fbScarlsonj 				exit(Z_ERR);
5715108322fbScarlsonj 			}
5716108322fbScarlsonj 			if (stat(optarg, &st) == -1 || !S_ISDIR(st.st_mode)) {
5717108322fbScarlsonj 				zerror(
5718108322fbScarlsonj 				    gettext("root path must be a directory."));
5719108322fbScarlsonj 				exit(Z_ERR);
5720108322fbScarlsonj 			}
5721108322fbScarlsonj 			zonecfg_set_root(optarg);
5722108322fbScarlsonj 			break;
57237c478bd9Sstevel@tonic-gate 		default:
57247c478bd9Sstevel@tonic-gate 			return (usage(B_FALSE));
57257c478bd9Sstevel@tonic-gate 		}
57267c478bd9Sstevel@tonic-gate 	}
57277c478bd9Sstevel@tonic-gate 
57287c478bd9Sstevel@tonic-gate 	if (optind >= argc)
57297c478bd9Sstevel@tonic-gate 		return (usage(B_FALSE));
5730555afedfScarlsonj 
5731555afedfScarlsonj 	if (target_uuid != NULL && *target_uuid != '\0') {
5732555afedfScarlsonj 		uuid_t uuid;
5733555afedfScarlsonj 		static char newtarget[ZONENAME_MAX];
5734555afedfScarlsonj 
5735555afedfScarlsonj 		if (uuid_parse(target_uuid, uuid) == -1) {
5736555afedfScarlsonj 			zerror(gettext("illegal UUID value specified"));
5737555afedfScarlsonj 			exit(Z_ERR);
5738555afedfScarlsonj 		}
5739555afedfScarlsonj 		if (zonecfg_get_name_by_uuid(uuid, newtarget,
5740555afedfScarlsonj 		    sizeof (newtarget)) == Z_OK)
5741555afedfScarlsonj 			target_zone = newtarget;
5742555afedfScarlsonj 	}
5743555afedfScarlsonj 
57447c478bd9Sstevel@tonic-gate 	if (target_zone != NULL && zone_get_id(target_zone, &zid) != 0) {
57457c478bd9Sstevel@tonic-gate 		errno = Z_NO_ZONE;
57467c478bd9Sstevel@tonic-gate 		zperror(target_zone, B_TRUE);
57477c478bd9Sstevel@tonic-gate 		exit(Z_ERR);
57487c478bd9Sstevel@tonic-gate 	}
57499acbbeafSnn35248 
57509acbbeafSnn35248 	/*
57519acbbeafSnn35248 	 * See if we have inherited the right to manipulate this zone from
57529acbbeafSnn35248 	 * a zoneadm instance in our ancestry.  If so, set zone_lock_cnt to
57539acbbeafSnn35248 	 * indicate it.  If not, make that explicit in our environment.
57549acbbeafSnn35248 	 */
5755ff17c8bfSgjelinek 	zonecfg_init_lock_file(target_zone, &zone_lock_env);
57569acbbeafSnn35248 
5757e5816e35SEdward Pilatowicz 	/* Figure out what the system's default brand is */
5758e5816e35SEdward Pilatowicz 	if (zonecfg_default_brand(default_brand,
5759e5816e35SEdward Pilatowicz 	    sizeof (default_brand)) != Z_OK) {
5760e5816e35SEdward Pilatowicz 		zerror(gettext("unable to determine default brand"));
5761e5816e35SEdward Pilatowicz 		return (Z_ERR);
5762e5816e35SEdward Pilatowicz 	}
5763e5816e35SEdward Pilatowicz 
57649acbbeafSnn35248 	/*
57659acbbeafSnn35248 	 * If we are going to be operating on a single zone, retrieve its
57669acbbeafSnn35248 	 * brand type and determine whether it is native or not.
57679acbbeafSnn35248 	 */
57689acbbeafSnn35248 	if ((target_zone != NULL) &&
57692ad45a84Sjv227347 	    (strcmp(target_zone, GLOBAL_ZONENAME) != 0)) {
57709acbbeafSnn35248 		if (zone_get_brand(target_zone, target_brand,
57719acbbeafSnn35248 		    sizeof (target_brand)) != Z_OK) {
57729acbbeafSnn35248 			zerror(gettext("missing or invalid brand"));
57739acbbeafSnn35248 			exit(Z_ERR);
57749acbbeafSnn35248 		}
577562868012SSteve Lawrence 		/*
577662868012SSteve Lawrence 		 * In the alternate root environment, the only supported
577762868012SSteve Lawrence 		 * operations are mount and unmount.  In this case, just treat
577862868012SSteve Lawrence 		 * the zone as native if it is cluster.  Cluster zones can be
577962868012SSteve Lawrence 		 * native for the purpose of LU or upgrade, and the cluster
578062868012SSteve Lawrence 		 * brand may not exist in the miniroot (such as in net install
578162868012SSteve Lawrence 		 * upgrade).
578262868012SSteve Lawrence 		 */
578362868012SSteve Lawrence 		if (strcmp(target_brand, CLUSTER_BRAND_NAME) == 0) {
578462868012SSteve Lawrence 			if (zonecfg_in_alt_root()) {
5785e5816e35SEdward Pilatowicz 				(void) strlcpy(target_brand, default_brand,
578662868012SSteve Lawrence 				    sizeof (target_brand));
578762868012SSteve Lawrence 			}
578862868012SSteve Lawrence 		}
57899acbbeafSnn35248 	}
57909acbbeafSnn35248 
57919acbbeafSnn35248 	err = parse_and_run(argc - optind, &argv[optind]);
57929acbbeafSnn35248 
57939acbbeafSnn35248 	return (err);
57947c478bd9Sstevel@tonic-gate }
5795