xref: /onnv-gate/usr/src/cmd/zoneadm/zoneadm.c (revision 8942:d9afb306bc8c)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
51507Sgjelinek  * Common Development and Distribution License (the "License").
61507Sgjelinek  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
21222Scomay 
220Sstevel@tonic-gate /*
238759Sgerald.jelinek@sun.com  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate /*
280Sstevel@tonic-gate  * zoneadm is a command interpreter for zone administration.  It is all in
290Sstevel@tonic-gate  * C (i.e., no lex/yacc), and all the argument passing is argc/argv based.
300Sstevel@tonic-gate  * main() calls parse_and_run() which calls cmd_match(), then invokes the
310Sstevel@tonic-gate  * appropriate command's handler function.  The rest of the program is the
320Sstevel@tonic-gate  * handler functions and their helper functions.
330Sstevel@tonic-gate  *
340Sstevel@tonic-gate  * Some of the helper functions are used largely to simplify I18N: reducing
350Sstevel@tonic-gate  * the need for translation notes.  This is particularly true of many of
360Sstevel@tonic-gate  * the zerror() calls: doing e.g. zerror(gettext("%s failed"), "foo") rather
370Sstevel@tonic-gate  * than zerror(gettext("foo failed")) with a translation note indicating
380Sstevel@tonic-gate  * that "foo" need not be translated.
390Sstevel@tonic-gate  */
400Sstevel@tonic-gate 
410Sstevel@tonic-gate #include <stdio.h>
420Sstevel@tonic-gate #include <errno.h>
430Sstevel@tonic-gate #include <unistd.h>
440Sstevel@tonic-gate #include <signal.h>
450Sstevel@tonic-gate #include <stdarg.h>
460Sstevel@tonic-gate #include <ctype.h>
470Sstevel@tonic-gate #include <stdlib.h>
480Sstevel@tonic-gate #include <string.h>
490Sstevel@tonic-gate #include <wait.h>
500Sstevel@tonic-gate #include <zone.h>
510Sstevel@tonic-gate #include <priv.h>
520Sstevel@tonic-gate #include <locale.h>
530Sstevel@tonic-gate #include <libintl.h>
540Sstevel@tonic-gate #include <libzonecfg.h>
550Sstevel@tonic-gate #include <bsm/adt.h>
562712Snn35248 #include <sys/brand.h>
570Sstevel@tonic-gate #include <sys/param.h>
580Sstevel@tonic-gate #include <sys/types.h>
590Sstevel@tonic-gate #include <sys/stat.h>
600Sstevel@tonic-gate #include <sys/statvfs.h>
610Sstevel@tonic-gate #include <assert.h>
620Sstevel@tonic-gate #include <sys/sockio.h>
630Sstevel@tonic-gate #include <sys/mntent.h>
640Sstevel@tonic-gate #include <limits.h>
651867Sgjelinek #include <dirent.h>
662303Scarlsonj #include <uuid/uuid.h>
674456Sss150715 #include <libdlpi.h>
680Sstevel@tonic-gate 
690Sstevel@tonic-gate #include <fcntl.h>
700Sstevel@tonic-gate #include <door.h>
710Sstevel@tonic-gate #include <macros.h>
720Sstevel@tonic-gate #include <libgen.h>
731300Sgjelinek #include <fnmatch.h>
741931Sgjelinek #include <sys/modctl.h>
752712Snn35248 #include <libbrand.h>
763247Sgjelinek #include <libscf.h>
773352Sgjelinek #include <procfs.h>
783686Sgjelinek #include <strings.h>
790Sstevel@tonic-gate 
800Sstevel@tonic-gate #include <pool.h>
810Sstevel@tonic-gate #include <sys/pool.h>
823247Sgjelinek #include <sys/priocntl.h>
833247Sgjelinek #include <sys/fsspriocntl.h>
840Sstevel@tonic-gate 
851867Sgjelinek #include "zoneadm.h"
861867Sgjelinek 
870Sstevel@tonic-gate #define	MAXARGS	8
880Sstevel@tonic-gate 
890Sstevel@tonic-gate /* Reflects kernel zone entries */
900Sstevel@tonic-gate typedef struct zone_entry {
910Sstevel@tonic-gate 	zoneid_t	zid;
920Sstevel@tonic-gate 	char		zname[ZONENAME_MAX];
930Sstevel@tonic-gate 	char		*zstate_str;
940Sstevel@tonic-gate 	zone_state_t	zstate_num;
952712Snn35248 	char		zbrand[MAXNAMELEN];
960Sstevel@tonic-gate 	char		zroot[MAXPATHLEN];
972303Scarlsonj 	char		zuuid[UUID_PRINTABLE_STRING_LENGTH];
983448Sdh155122 	zone_iptype_t	ziptype;
990Sstevel@tonic-gate } zone_entry_t;
1000Sstevel@tonic-gate 
1014350Std153743 #define	CLUSTER_BRAND_NAME	"cluster"
1024350Std153743 
1030Sstevel@tonic-gate static zone_entry_t *zents;
1040Sstevel@tonic-gate static size_t nzents;
1050Sstevel@tonic-gate 
1061915Sgjelinek #define	LOOPBACK_IF	"lo0"
1071915Sgjelinek #define	SOCKET_AF(af)	(((af) == AF_UNSPEC) ? AF_INET : (af))
1081915Sgjelinek 
1091915Sgjelinek struct net_if {
1101915Sgjelinek 	char	*name;
1111915Sgjelinek 	int	af;
1121915Sgjelinek };
1131915Sgjelinek 
1140Sstevel@tonic-gate /* 0755 is the default directory mode. */
1150Sstevel@tonic-gate #define	DEFAULT_DIR_MODE \
1160Sstevel@tonic-gate 	(S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH)
1170Sstevel@tonic-gate 
1180Sstevel@tonic-gate struct cmd {
1190Sstevel@tonic-gate 	uint_t	cmd_num;				/* command number */
1200Sstevel@tonic-gate 	char	*cmd_name;				/* command name */
1210Sstevel@tonic-gate 	char	*short_usage;				/* short form help */
1220Sstevel@tonic-gate 	int	(*handler)(int argc, char *argv[]);	/* function to call */
1230Sstevel@tonic-gate 
1240Sstevel@tonic-gate };
1250Sstevel@tonic-gate 
1260Sstevel@tonic-gate #define	SHELP_HELP	"help"
1272267Sdp #define	SHELP_BOOT	"boot [-- boot_arguments]"
1280Sstevel@tonic-gate #define	SHELP_HALT	"halt"
1290Sstevel@tonic-gate #define	SHELP_READY	"ready"
1302267Sdp #define	SHELP_REBOOT	"reboot [-- boot_arguments]"
1310Sstevel@tonic-gate #define	SHELP_LIST	"list [-cipv]"
1320Sstevel@tonic-gate #define	SHELP_VERIFY	"verify"
1332712Snn35248 #define	SHELP_INSTALL	"install [-x nodataset] [brand-specific args]"
1347089Sgjelinek #define	SHELP_UNINSTALL	"uninstall [-F] [brand-specific args]"
1357089Sgjelinek #define	SHELP_CLONE	"clone [-m method] [-s <ZFS snapshot>] "\
1367089Sgjelinek 	"[brand-specific args] zonename"
1371300Sgjelinek #define	SHELP_MOVE	"move zonepath"
1387089Sgjelinek #define	SHELP_DETACH	"detach [-n] [brand-specific args]"
1397089Sgjelinek #define	SHELP_ATTACH	"attach [-F] [-n <path>] [brand-specific args]"
1402303Scarlsonj #define	SHELP_MARK	"mark incomplete"
1410Sstevel@tonic-gate 
1422712Snn35248 #define	EXEC_PREFIX	"exec "
1432712Snn35248 #define	EXEC_LEN	(strlen(EXEC_PREFIX))
1442712Snn35248 #define	RMCOMMAND	"/usr/bin/rm -rf"
1452712Snn35248 
1462712Snn35248 static int cleanup_zonepath(char *, boolean_t);
1472712Snn35248 
1483448Sdh155122 
1490Sstevel@tonic-gate static int help_func(int argc, char *argv[]);
1500Sstevel@tonic-gate static int ready_func(int argc, char *argv[]);
1510Sstevel@tonic-gate static int boot_func(int argc, char *argv[]);
1520Sstevel@tonic-gate static int halt_func(int argc, char *argv[]);
1530Sstevel@tonic-gate static int reboot_func(int argc, char *argv[]);
1540Sstevel@tonic-gate static int list_func(int argc, char *argv[]);
1550Sstevel@tonic-gate static int verify_func(int argc, char *argv[]);
1560Sstevel@tonic-gate static int install_func(int argc, char *argv[]);
1570Sstevel@tonic-gate static int uninstall_func(int argc, char *argv[]);
158766Scarlsonj static int mount_func(int argc, char *argv[]);
159766Scarlsonj static int unmount_func(int argc, char *argv[]);
1601300Sgjelinek static int clone_func(int argc, char *argv[]);
1611300Sgjelinek static int move_func(int argc, char *argv[]);
1621507Sgjelinek static int detach_func(int argc, char *argv[]);
1631507Sgjelinek static int attach_func(int argc, char *argv[]);
1642303Scarlsonj static int mark_func(int argc, char *argv[]);
1653247Sgjelinek static int apply_func(int argc, char *argv[]);
1660Sstevel@tonic-gate static int sanity_check(char *zone, int cmd_num, boolean_t running,
1672712Snn35248     boolean_t unsafe_when_running, boolean_t force);
1680Sstevel@tonic-gate static int cmd_match(char *cmd);
1693339Szt129084 static int verify_details(int, char *argv[]);
1703339Szt129084 static int verify_brand(zone_dochandle_t, int, char *argv[]);
1713339Szt129084 static int invoke_brand_handler(int, char *argv[]);
1720Sstevel@tonic-gate 
1730Sstevel@tonic-gate static struct cmd cmdtab[] = {
1740Sstevel@tonic-gate 	{ CMD_HELP,		"help",		SHELP_HELP,	help_func },
1750Sstevel@tonic-gate 	{ CMD_BOOT,		"boot",		SHELP_BOOT,	boot_func },
1760Sstevel@tonic-gate 	{ CMD_HALT,		"halt",		SHELP_HALT,	halt_func },
1770Sstevel@tonic-gate 	{ CMD_READY,		"ready",	SHELP_READY,	ready_func },
1780Sstevel@tonic-gate 	{ CMD_REBOOT,		"reboot",	SHELP_REBOOT,	reboot_func },
1790Sstevel@tonic-gate 	{ CMD_LIST,		"list",		SHELP_LIST,	list_func },
1800Sstevel@tonic-gate 	{ CMD_VERIFY,		"verify",	SHELP_VERIFY,	verify_func },
1810Sstevel@tonic-gate 	{ CMD_INSTALL,		"install",	SHELP_INSTALL,	install_func },
1820Sstevel@tonic-gate 	{ CMD_UNINSTALL,	"uninstall",	SHELP_UNINSTALL,
183766Scarlsonj 	    uninstall_func },
1841300Sgjelinek 	/* mount and unmount are private commands for admin/install */
185766Scarlsonj 	{ CMD_MOUNT,		"mount",	NULL,		mount_func },
1861300Sgjelinek 	{ CMD_UNMOUNT,		"unmount",	NULL,		unmount_func },
1871300Sgjelinek 	{ CMD_CLONE,		"clone",	SHELP_CLONE,	clone_func },
1881507Sgjelinek 	{ CMD_MOVE,		"move",		SHELP_MOVE,	move_func },
1891507Sgjelinek 	{ CMD_DETACH,		"detach",	SHELP_DETACH,	detach_func },
1902303Scarlsonj 	{ CMD_ATTACH,		"attach",	SHELP_ATTACH,	attach_func },
1913247Sgjelinek 	{ CMD_MARK,		"mark",		SHELP_MARK,	mark_func },
1923247Sgjelinek 	{ CMD_APPLY,		"apply",	NULL,		apply_func }
1930Sstevel@tonic-gate };
1940Sstevel@tonic-gate 
1950Sstevel@tonic-gate /* global variables */
1960Sstevel@tonic-gate 
1970Sstevel@tonic-gate /* set early in main(), never modified thereafter, used all over the place */
1980Sstevel@tonic-gate static char *execname;
1992712Snn35248 static char target_brand[MAXNAMELEN];
2000Sstevel@tonic-gate static char *locale;
2011867Sgjelinek char *target_zone;
2022303Scarlsonj static char *target_uuid;
2030Sstevel@tonic-gate 
2040Sstevel@tonic-gate /* used in do_subproc() and signal handler */
2050Sstevel@tonic-gate static volatile boolean_t child_killed;
2062712Snn35248 static int do_subproc_cnt = 0;
2072712Snn35248 
2082712Snn35248 /*
2092712Snn35248  * Used to indicate whether this zoneadm instance has another zoneadm
2102712Snn35248  * instance in its ancestry.
2112712Snn35248  */
2122712Snn35248 static boolean_t zoneadm_is_nested = B_FALSE;
2132712Snn35248 
2141867Sgjelinek char *
2150Sstevel@tonic-gate cmd_to_str(int cmd_num)
2160Sstevel@tonic-gate {
2170Sstevel@tonic-gate 	assert(cmd_num >= CMD_MIN && cmd_num <= CMD_MAX);
2180Sstevel@tonic-gate 	return (cmdtab[cmd_num].cmd_name);
2190Sstevel@tonic-gate }
2200Sstevel@tonic-gate 
2210Sstevel@tonic-gate /* This is a separate function because of gettext() wrapping. */
2220Sstevel@tonic-gate static char *
2230Sstevel@tonic-gate long_help(int cmd_num)
2240Sstevel@tonic-gate {
225222Scomay 	assert(cmd_num >= CMD_MIN && cmd_num <= CMD_MAX);
2260Sstevel@tonic-gate 	switch (cmd_num) {
2271634Sgjelinek 	case CMD_HELP:
2281634Sgjelinek 		return (gettext("Print usage message."));
2291634Sgjelinek 	case CMD_BOOT:
2302267Sdp 		return (gettext("Activates (boots) specified zone.  See "
2312267Sdp 		    "zoneadm(1m) for valid boot\n\targuments."));
2321634Sgjelinek 	case CMD_HALT:
2331634Sgjelinek 		return (gettext("Halts specified zone, bypassing shutdown "
2341634Sgjelinek 		    "scripts and removing runtime\n\tresources of the zone."));
2351634Sgjelinek 	case CMD_READY:
2361634Sgjelinek 		return (gettext("Prepares a zone for running applications but "
2371634Sgjelinek 		    "does not start any user\n\tprocesses in the zone."));
2381634Sgjelinek 	case CMD_REBOOT:
2391634Sgjelinek 		return (gettext("Restarts the zone (equivalent to a halt / "
2402267Sdp 		    "boot sequence).\n\tFails if the zone is not active.  "
2412267Sdp 		    "See zoneadm(1m) for valid boot\n\targuments."));
2421634Sgjelinek 	case CMD_LIST:
2431634Sgjelinek 		return (gettext("Lists the current zones, or a "
2441634Sgjelinek 		    "specific zone if indicated.  By default,\n\tall "
2451634Sgjelinek 		    "running zones are listed, though this can be "
2461634Sgjelinek 		    "expanded to all\n\tinstalled zones with the -i "
2471634Sgjelinek 		    "option or all configured zones with the\n\t-c "
2482303Scarlsonj 		    "option.  When used with the general -z <zone> and/or -u "
2492303Scarlsonj 		    "<uuid-match>\n\toptions, lists only the specified "
2502303Scarlsonj 		    "matching zone, but lists it\n\tregardless of its state, "
2512303Scarlsonj 		    "and the -i and -c options are disallowed.  The\n\t-v "
2522303Scarlsonj 		    "option can be used to display verbose information: zone "
2532303Scarlsonj 		    "name, id,\n\tcurrent state, root directory and options.  "
2542303Scarlsonj 		    "The -p option can be used\n\tto request machine-parsable "
2552303Scarlsonj 		    "output.  The -v and -p options are mutually\n\texclusive."
2562303Scarlsonj 		    "  If neither -v nor -p is used, just the zone name is "
2572303Scarlsonj 		    "listed."));
2581634Sgjelinek 	case CMD_VERIFY:
2591634Sgjelinek 		return (gettext("Check to make sure the configuration "
2601634Sgjelinek 		    "can safely be instantiated\n\ton the machine: "
2611634Sgjelinek 		    "physical network interfaces exist, etc."));
2621634Sgjelinek 	case CMD_INSTALL:
2631867Sgjelinek 		return (gettext("Install the configuration on to the system.  "
2641867Sgjelinek 		    "The -x nodataset option\n\tcan be used to prevent the "
2651867Sgjelinek 		    "creation of a new ZFS file system for the\n\tzone "
2662712Snn35248 		    "(assuming the zonepath is within a ZFS file system).\n\t"
2672712Snn35248 		    "All other arguments are passed to the brand installation "
2687089Sgjelinek 		    "function;\n\tsee brands(5) for more information."));
2691634Sgjelinek 	case CMD_UNINSTALL:
2701634Sgjelinek 		return (gettext("Uninstall the configuration from the system.  "
2717089Sgjelinek 		    "The -F flag can be used\n\tto force the action.  All "
2727089Sgjelinek 		    "other arguments are passed to the brand\n\tuninstall "
2737089Sgjelinek 		    "function; see brands(5) for more information."));
2741634Sgjelinek 	case CMD_CLONE:
2751867Sgjelinek 		return (gettext("Clone the installation of another zone.  "
2761867Sgjelinek 		    "The -m option can be used to\n\tspecify 'copy' which "
2771867Sgjelinek 		    "forces a copy of the source zone.  The -s option\n\t"
2781867Sgjelinek 		    "can be used to specify the name of a ZFS snapshot "
2791867Sgjelinek 		    "that was taken from\n\ta previous clone command.  The "
2801867Sgjelinek 		    "snapshot will be used as the source\n\tinstead of "
2817089Sgjelinek 		    "creating a new ZFS snapshot.  All other arguments are "
2827089Sgjelinek 		    "passed\n\tto the brand clone function; see "
2837089Sgjelinek 		    "brands(5) for more information."));
2841634Sgjelinek 	case CMD_MOVE:
2851634Sgjelinek 		return (gettext("Move the zone to a new zonepath."));
2861634Sgjelinek 	case CMD_DETACH:
2871634Sgjelinek 		return (gettext("Detach the zone from the system. The zone "
2881634Sgjelinek 		    "state is changed to\n\t'configured' (but the files under "
2891634Sgjelinek 		    "the zonepath are untouched).\n\tThe zone can subsequently "
2901634Sgjelinek 		    "be attached, or can be moved to another\n\tsystem and "
2912078Sgjelinek 		    "attached there.  The -n option can be used to specify\n\t"
2922078Sgjelinek 		    "'no-execute' mode.  When -n is used, the information "
2932078Sgjelinek 		    "needed to attach\n\tthe zone is sent to standard output "
2947089Sgjelinek 		    "but the zone is not actually\n\tdetached.  All other "
2957089Sgjelinek 		    "arguments are passed to the brand detach function;\n\tsee "
2967089Sgjelinek 		    "brands(5) for more information."));
2971634Sgjelinek 	case CMD_ATTACH:
2981634Sgjelinek 		return (gettext("Attach the zone to the system.  The zone "
2991634Sgjelinek 		    "state must be 'configured'\n\tprior to attach; upon "
3001634Sgjelinek 		    "successful completion, the zone state will be\n\t"
3011634Sgjelinek 		    "'installed'.  The system software on the current "
3021634Sgjelinek 		    "system must be\n\tcompatible with the software on the "
3037089Sgjelinek 		    "zone's original system.\n\tSpecify -F "
3045829Sgjelinek 		    "to force the attach and skip software compatibility "
3055829Sgjelinek 		    "tests.\n\tThe -n option can be used to specify "
3065829Sgjelinek 		    "'no-execute' mode.  When -n is\n\tused, the information "
3075829Sgjelinek 		    "needed to attach the zone is read from the\n\tspecified "
3085829Sgjelinek 		    "path and the configuration is only validated.  The path "
3097089Sgjelinek 		    "can\n\tbe '-' to specify standard input.  The -F and -n "
3107089Sgjelinek 		    "options are mutually\n\texclusive.  All other arguments "
3117089Sgjelinek 		    "are passed to the brand attach\n\tfunction; see "
3127089Sgjelinek 		    "brands(5) for more information."));
3132303Scarlsonj 	case CMD_MARK:
3142303Scarlsonj 		return (gettext("Set the state of the zone.  This can be used "
3152303Scarlsonj 		    "to force the zone\n\tstate to 'incomplete' "
3162303Scarlsonj 		    "administratively if some activity has rendered\n\tthe "
3172303Scarlsonj 		    "zone permanently unusable.  The only valid state that "
3182303Scarlsonj 		    "may be\n\tspecified is 'incomplete'."));
3191634Sgjelinek 	default:
3201634Sgjelinek 		return ("");
3210Sstevel@tonic-gate 	}
3220Sstevel@tonic-gate 	/* NOTREACHED */
323222Scomay 	return (NULL);
3240Sstevel@tonic-gate }
3250Sstevel@tonic-gate 
3260Sstevel@tonic-gate /*
3270Sstevel@tonic-gate  * Called with explicit B_TRUE when help is explicitly requested, B_FALSE for
3280Sstevel@tonic-gate  * unexpected errors.
3290Sstevel@tonic-gate  */
3300Sstevel@tonic-gate 
3310Sstevel@tonic-gate static int
3320Sstevel@tonic-gate usage(boolean_t explicit)
3330Sstevel@tonic-gate {
3340Sstevel@tonic-gate 	int i;
3350Sstevel@tonic-gate 	FILE *fd = explicit ? stdout : stderr;
3360Sstevel@tonic-gate 
3370Sstevel@tonic-gate 	(void) fprintf(fd, "%s:\t%s help\n", gettext("usage"), execname);
3382303Scarlsonj 	(void) fprintf(fd, "\t%s [-z <zone>] [-u <uuid-match>] list\n",
3392303Scarlsonj 	    execname);
3402303Scarlsonj 	(void) fprintf(fd, "\t%s {-z <zone>|-u <uuid-match>} <%s>\n", execname,
3410Sstevel@tonic-gate 	    gettext("subcommand"));
3420Sstevel@tonic-gate 	(void) fprintf(fd, "\n%s:\n\n", gettext("Subcommands"));
3430Sstevel@tonic-gate 	for (i = CMD_MIN; i <= CMD_MAX; i++) {
344766Scarlsonj 		if (cmdtab[i].short_usage == NULL)
345766Scarlsonj 			continue;
3460Sstevel@tonic-gate 		(void) fprintf(fd, "%s\n", cmdtab[i].short_usage);
3470Sstevel@tonic-gate 		if (explicit)
3480Sstevel@tonic-gate 			(void) fprintf(fd, "\t%s\n\n", long_help(i));
3490Sstevel@tonic-gate 	}
3500Sstevel@tonic-gate 	if (!explicit)
3510Sstevel@tonic-gate 		(void) fputs("\n", fd);
3520Sstevel@tonic-gate 	return (Z_USAGE);
3530Sstevel@tonic-gate }
3540Sstevel@tonic-gate 
3550Sstevel@tonic-gate static void
3560Sstevel@tonic-gate sub_usage(char *short_usage, int cmd_num)
3570Sstevel@tonic-gate {
3580Sstevel@tonic-gate 	(void) fprintf(stderr, "%s:\t%s\n", gettext("usage"), short_usage);
3590Sstevel@tonic-gate 	(void) fprintf(stderr, "\t%s\n", long_help(cmd_num));
3600Sstevel@tonic-gate }
3610Sstevel@tonic-gate 
3620Sstevel@tonic-gate /*
3630Sstevel@tonic-gate  * zperror() is like perror(3c) except that this also prints the executable
3640Sstevel@tonic-gate  * name at the start of the message, and takes a boolean indicating whether
3650Sstevel@tonic-gate  * to call libc'c strerror() or that from libzonecfg.
3660Sstevel@tonic-gate  */
3670Sstevel@tonic-gate 
3681867Sgjelinek void
3690Sstevel@tonic-gate zperror(const char *str, boolean_t zonecfg_error)
3700Sstevel@tonic-gate {
3710Sstevel@tonic-gate 	(void) fprintf(stderr, "%s: %s: %s\n", execname, str,
3720Sstevel@tonic-gate 	    zonecfg_error ? zonecfg_strerror(errno) : strerror(errno));
3730Sstevel@tonic-gate }
3740Sstevel@tonic-gate 
3750Sstevel@tonic-gate /*
3760Sstevel@tonic-gate  * zperror2() is very similar to zperror() above, except it also prints a
3770Sstevel@tonic-gate  * supplied zone name after the executable.
3780Sstevel@tonic-gate  *
3790Sstevel@tonic-gate  * All current consumers of this function want libzonecfg's strerror() rather
3800Sstevel@tonic-gate  * than libc's; if this ever changes, this function can be made more generic
3810Sstevel@tonic-gate  * like zperror() above.
3820Sstevel@tonic-gate  */
3830Sstevel@tonic-gate 
3841867Sgjelinek void
3850Sstevel@tonic-gate zperror2(const char *zone, const char *str)
3860Sstevel@tonic-gate {
3870Sstevel@tonic-gate 	(void) fprintf(stderr, "%s: %s: %s: %s\n", execname, zone, str,
3880Sstevel@tonic-gate 	    zonecfg_strerror(errno));
3890Sstevel@tonic-gate }
3900Sstevel@tonic-gate 
3910Sstevel@tonic-gate /* PRINTFLIKE1 */
3921867Sgjelinek void
3930Sstevel@tonic-gate zerror(const char *fmt, ...)
3940Sstevel@tonic-gate {
3950Sstevel@tonic-gate 	va_list alist;
3960Sstevel@tonic-gate 
3970Sstevel@tonic-gate 	va_start(alist, fmt);
3980Sstevel@tonic-gate 	(void) fprintf(stderr, "%s: ", execname);
3990Sstevel@tonic-gate 	if (target_zone != NULL)
4000Sstevel@tonic-gate 		(void) fprintf(stderr, "zone '%s': ", target_zone);
4010Sstevel@tonic-gate 	(void) vfprintf(stderr, fmt, alist);
4020Sstevel@tonic-gate 	(void) fprintf(stderr, "\n");
4030Sstevel@tonic-gate 	va_end(alist);
4040Sstevel@tonic-gate }
4050Sstevel@tonic-gate 
4060Sstevel@tonic-gate static void *
4070Sstevel@tonic-gate safe_calloc(size_t nelem, size_t elsize)
4080Sstevel@tonic-gate {
4090Sstevel@tonic-gate 	void *r = calloc(nelem, elsize);
4100Sstevel@tonic-gate 
4110Sstevel@tonic-gate 	if (r == NULL) {
4120Sstevel@tonic-gate 		zerror(gettext("failed to allocate %lu bytes: %s"),
4130Sstevel@tonic-gate 		    (ulong_t)nelem * elsize, strerror(errno));
4140Sstevel@tonic-gate 		exit(Z_ERR);
4150Sstevel@tonic-gate 	}
4160Sstevel@tonic-gate 	return (r);
4170Sstevel@tonic-gate }
4180Sstevel@tonic-gate 
4190Sstevel@tonic-gate static void
4200Sstevel@tonic-gate zone_print(zone_entry_t *zent, boolean_t verbose, boolean_t parsable)
4210Sstevel@tonic-gate {
4220Sstevel@tonic-gate 	static boolean_t firsttime = B_TRUE;
4233448Sdh155122 	char *ip_type_str;
4243448Sdh155122 
425*8942Sgerald.jelinek@sun.com 	/* Skip a zone that shutdown while we were collecting data. */
426*8942Sgerald.jelinek@sun.com 	if (zent->zname[0] == '\0')
427*8942Sgerald.jelinek@sun.com 		return;
428*8942Sgerald.jelinek@sun.com 
4293448Sdh155122 	if (zent->ziptype == ZS_EXCLUSIVE)
4303448Sdh155122 		ip_type_str = "excl";
4313448Sdh155122 	else
4323448Sdh155122 		ip_type_str = "shared";
4330Sstevel@tonic-gate 
4340Sstevel@tonic-gate 	assert(!(verbose && parsable));
4350Sstevel@tonic-gate 	if (firsttime && verbose) {
4360Sstevel@tonic-gate 		firsttime = B_FALSE;
4373448Sdh155122 		(void) printf("%*s %-16s %-10s %-30s %-8s %-6s\n",
4383448Sdh155122 		    ZONEID_WIDTH, "ID", "NAME", "STATUS", "PATH", "BRAND",
4393448Sdh155122 		    "IP");
4400Sstevel@tonic-gate 	}
4410Sstevel@tonic-gate 	if (!verbose) {
4422303Scarlsonj 		char *cp, *clim;
4432303Scarlsonj 
4440Sstevel@tonic-gate 		if (!parsable) {
4450Sstevel@tonic-gate 			(void) printf("%s\n", zent->zname);
4460Sstevel@tonic-gate 			return;
4470Sstevel@tonic-gate 		}
4480Sstevel@tonic-gate 		if (zent->zid == ZONE_ID_UNDEFINED)
4490Sstevel@tonic-gate 			(void) printf("-");
4500Sstevel@tonic-gate 		else
4510Sstevel@tonic-gate 			(void) printf("%lu", zent->zid);
4522303Scarlsonj 		(void) printf(":%s:%s:", zent->zname, zent->zstate_str);
4532303Scarlsonj 		cp = zent->zroot;
4542303Scarlsonj 		while ((clim = strchr(cp, ':')) != NULL) {
4552303Scarlsonj 			(void) printf("%.*s\\:", clim - cp, cp);
4562303Scarlsonj 			cp = clim + 1;
4572303Scarlsonj 		}
4583448Sdh155122 		(void) printf("%s:%s:%s:%s\n", cp, zent->zuuid, zent->zbrand,
4593448Sdh155122 		    ip_type_str);
4600Sstevel@tonic-gate 		return;
4610Sstevel@tonic-gate 	}
4620Sstevel@tonic-gate 	if (zent->zstate_str != NULL) {
4630Sstevel@tonic-gate 		if (zent->zid == ZONE_ID_UNDEFINED)
4640Sstevel@tonic-gate 			(void) printf("%*s", ZONEID_WIDTH, "-");
4650Sstevel@tonic-gate 		else
4660Sstevel@tonic-gate 			(void) printf("%*lu", ZONEID_WIDTH, zent->zid);
4673448Sdh155122 		(void) printf(" %-16s %-10s %-30s %-8s %-6s\n", zent->zname,
4683448Sdh155122 		    zent->zstate_str, zent->zroot, zent->zbrand, ip_type_str);
4690Sstevel@tonic-gate 	}
4700Sstevel@tonic-gate }
4710Sstevel@tonic-gate 
4720Sstevel@tonic-gate static int
473766Scarlsonj lookup_zone_info(const char *zone_name, zoneid_t zid, zone_entry_t *zent)
4740Sstevel@tonic-gate {
4751676Sjpk 	char root[MAXPATHLEN], *cp;
4760Sstevel@tonic-gate 	int err;
4772303Scarlsonj 	uuid_t uuid;
478*8942Sgerald.jelinek@sun.com 	zone_dochandle_t handle;
4790Sstevel@tonic-gate 
4800Sstevel@tonic-gate 	(void) strlcpy(zent->zname, zone_name, sizeof (zent->zname));
4810Sstevel@tonic-gate 	(void) strlcpy(zent->zroot, "???", sizeof (zent->zroot));
4822712Snn35248 	(void) strlcpy(zent->zbrand, "???", sizeof (zent->zbrand));
4830Sstevel@tonic-gate 	zent->zstate_str = "???";
4840Sstevel@tonic-gate 
485766Scarlsonj 	zent->zid = zid;
4860Sstevel@tonic-gate 
4872303Scarlsonj 	if (zonecfg_get_uuid(zone_name, uuid) == Z_OK &&
4882303Scarlsonj 	    !uuid_is_null(uuid))
4892303Scarlsonj 		uuid_unparse(uuid, zent->zuuid);
4902303Scarlsonj 	else
4912303Scarlsonj 		zent->zuuid[0] = '\0';
4922303Scarlsonj 
4931676Sjpk 	/*
4941676Sjpk 	 * For labeled zones which query the zone path of lower-level
4951676Sjpk 	 * zones, the path needs to be adjusted to drop the final
4961676Sjpk 	 * "/root" component. This adjusted path is then useful
4971676Sjpk 	 * for reading down any exported directories from the
4981676Sjpk 	 * lower-level zone.
4991676Sjpk 	 */
5001676Sjpk 	if (is_system_labeled() && zent->zid != ZONE_ID_UNDEFINED) {
5011676Sjpk 		if (zone_getattr(zent->zid, ZONE_ATTR_ROOT, zent->zroot,
5021676Sjpk 		    sizeof (zent->zroot)) == -1) {
5031676Sjpk 			zperror2(zent->zname,
5041676Sjpk 			    gettext("could not get zone path."));
5051676Sjpk 			return (Z_ERR);
5061676Sjpk 		}
5071676Sjpk 		cp = zent->zroot + strlen(zent->zroot) - 5;
5081676Sjpk 		if (cp > zent->zroot && strcmp(cp, "/root") == 0)
5091676Sjpk 			*cp = 0;
5101676Sjpk 	} else {
5111676Sjpk 		if ((err = zone_get_zonepath(zent->zname, root,
5121676Sjpk 		    sizeof (root))) != Z_OK) {
5131676Sjpk 			errno = err;
5141676Sjpk 			zperror2(zent->zname,
5151676Sjpk 			    gettext("could not get zone path."));
5161676Sjpk 			return (Z_ERR);
5171676Sjpk 		}
5181676Sjpk 		(void) strlcpy(zent->zroot, root, sizeof (zent->zroot));
5191676Sjpk 	}
5200Sstevel@tonic-gate 
5210Sstevel@tonic-gate 	if ((err = zone_get_state(zent->zname, &zent->zstate_num)) != Z_OK) {
5220Sstevel@tonic-gate 		errno = err;
5230Sstevel@tonic-gate 		zperror2(zent->zname, gettext("could not get state"));
5240Sstevel@tonic-gate 		return (Z_ERR);
5250Sstevel@tonic-gate 	}
5260Sstevel@tonic-gate 	zent->zstate_str = zone_state_str(zent->zstate_num);
5273009Snn35248 
5283009Snn35248 	/*
5293009Snn35248 	 * A zone's brand is only available in the .xml file describing it,
5303009Snn35248 	 * which is only visible to the global zone.  This causes
5313009Snn35248 	 * zone_get_brand() to fail when called from within a non-global
5323009Snn35248 	 * zone.  Fortunately we only do this on labeled systems, where we
5333009Snn35248 	 * know all zones are native.
5343009Snn35248 	 */
5353009Snn35248 	if (getzoneid() != GLOBAL_ZONEID) {
5363009Snn35248 		assert(is_system_labeled() != 0);
5373009Snn35248 		(void) strlcpy(zent->zbrand, NATIVE_BRAND_NAME,
5383009Snn35248 		    sizeof (zent->zbrand));
5393009Snn35248 	} else if (zone_get_brand(zent->zname, zent->zbrand,
5402712Snn35248 	    sizeof (zent->zbrand)) != Z_OK) {
5412712Snn35248 		zperror2(zent->zname, gettext("could not get brand name"));
5422712Snn35248 		return (Z_ERR);
5432712Snn35248 	}
5440Sstevel@tonic-gate 
5453448Sdh155122 	/*
5463448Sdh155122 	 * Get ip type of the zone.
5473448Sdh155122 	 * Note for global zone, ZS_SHARED is set always.
5483448Sdh155122 	 */
5493448Sdh155122 	if (zid == GLOBAL_ZONEID) {
5503448Sdh155122 		zent->ziptype = ZS_SHARED;
551*8942Sgerald.jelinek@sun.com 		return (Z_OK);
552*8942Sgerald.jelinek@sun.com 	}
553*8942Sgerald.jelinek@sun.com 
554*8942Sgerald.jelinek@sun.com 	/*
555*8942Sgerald.jelinek@sun.com 	 * There is a race condition where the zone could boot while
556*8942Sgerald.jelinek@sun.com 	 * we're walking the index file.  In this case the zone state
557*8942Sgerald.jelinek@sun.com 	 * could be seen as running from the call above, but the zoneid
558*8942Sgerald.jelinek@sun.com 	 * would be undefined.
559*8942Sgerald.jelinek@sun.com 	 *
560*8942Sgerald.jelinek@sun.com 	 * There is also a race condition where the zone could shutdown after
561*8942Sgerald.jelinek@sun.com 	 * we got its running state above.  This is also not an error and
562*8942Sgerald.jelinek@sun.com 	 * we fall back to getting the ziptype from the zone configuration.
563*8942Sgerald.jelinek@sun.com 	 */
564*8942Sgerald.jelinek@sun.com 	if (zent->zstate_num == ZONE_STATE_RUNNING &&
565*8942Sgerald.jelinek@sun.com 	    zid != ZONE_ID_UNDEFINED) {
566*8942Sgerald.jelinek@sun.com 		ushort_t flags;
567*8942Sgerald.jelinek@sun.com 
568*8942Sgerald.jelinek@sun.com 		if (zone_getattr(zid, ZONE_ATTR_FLAGS, &flags,
569*8942Sgerald.jelinek@sun.com 		    sizeof (flags)) >= 0) {
5703448Sdh155122 			if (flags & ZF_NET_EXCL)
5713448Sdh155122 				zent->ziptype = ZS_EXCLUSIVE;
5723448Sdh155122 			else
5733448Sdh155122 				zent->ziptype = ZS_SHARED;
574*8942Sgerald.jelinek@sun.com 			return (Z_OK);
5753448Sdh155122 		}
5763448Sdh155122 	}
5773448Sdh155122 
578*8942Sgerald.jelinek@sun.com 	if ((handle = zonecfg_init_handle()) == NULL) {
579*8942Sgerald.jelinek@sun.com 		zperror2(zent->zname, gettext("could not init handle"));
580*8942Sgerald.jelinek@sun.com 		return (Z_ERR);
581*8942Sgerald.jelinek@sun.com 	}
582*8942Sgerald.jelinek@sun.com 	if ((err = zonecfg_get_handle(zent->zname, handle)) != Z_OK) {
583*8942Sgerald.jelinek@sun.com 		zperror2(zent->zname, gettext("could not get handle"));
584*8942Sgerald.jelinek@sun.com 		zonecfg_fini_handle(handle);
585*8942Sgerald.jelinek@sun.com 		return (Z_ERR);
586*8942Sgerald.jelinek@sun.com 	}
587*8942Sgerald.jelinek@sun.com 
588*8942Sgerald.jelinek@sun.com 	if ((err = zonecfg_get_iptype(handle, &zent->ziptype)) != Z_OK) {
589*8942Sgerald.jelinek@sun.com 		zperror2(zent->zname, gettext("could not get ip-type"));
590*8942Sgerald.jelinek@sun.com 		zonecfg_fini_handle(handle);
591*8942Sgerald.jelinek@sun.com 		return (Z_ERR);
592*8942Sgerald.jelinek@sun.com 	}
593*8942Sgerald.jelinek@sun.com 	zonecfg_fini_handle(handle);
594*8942Sgerald.jelinek@sun.com 
5950Sstevel@tonic-gate 	return (Z_OK);
5960Sstevel@tonic-gate }
5970Sstevel@tonic-gate 
5980Sstevel@tonic-gate /*
5990Sstevel@tonic-gate  * fetch_zents() calls zone_list(2) to find out how many zones are running
6000Sstevel@tonic-gate  * (which is stored in the global nzents), then calls zone_list(2) again
6010Sstevel@tonic-gate  * to fetch the list of running zones (stored in the global zents).  This
6020Sstevel@tonic-gate  * function may be called multiple times, so if zents is already set, we
6030Sstevel@tonic-gate  * return immediately to save work.
604*8942Sgerald.jelinek@sun.com  *
605*8942Sgerald.jelinek@sun.com  * Note that the data about running zones can change while this function
606*8942Sgerald.jelinek@sun.com  * is running, so its possible that the list of zones will have empty slots
607*8942Sgerald.jelinek@sun.com  * at the end.
6080Sstevel@tonic-gate  */
6090Sstevel@tonic-gate 
6100Sstevel@tonic-gate static int
611766Scarlsonj fetch_zents(void)
6120Sstevel@tonic-gate {
6130Sstevel@tonic-gate 	zoneid_t *zids = NULL;
6140Sstevel@tonic-gate 	uint_t nzents_saved;
615766Scarlsonj 	int i, retv;
616766Scarlsonj 	FILE *fp;
617766Scarlsonj 	boolean_t inaltroot;
618766Scarlsonj 	zone_entry_t *zentp;
6190Sstevel@tonic-gate 
6200Sstevel@tonic-gate 	if (nzents > 0)
6210Sstevel@tonic-gate 		return (Z_OK);
6220Sstevel@tonic-gate 
6230Sstevel@tonic-gate 	if (zone_list(NULL, &nzents) != 0) {
6240Sstevel@tonic-gate 		zperror(gettext("failed to get zoneid list"), B_FALSE);
6250Sstevel@tonic-gate 		return (Z_ERR);
6260Sstevel@tonic-gate 	}
6270Sstevel@tonic-gate 
6280Sstevel@tonic-gate again:
6290Sstevel@tonic-gate 	if (nzents == 0)
6300Sstevel@tonic-gate 		return (Z_OK);
6310Sstevel@tonic-gate 
6320Sstevel@tonic-gate 	zids = safe_calloc(nzents, sizeof (zoneid_t));
6330Sstevel@tonic-gate 	nzents_saved = nzents;
6340Sstevel@tonic-gate 
6350Sstevel@tonic-gate 	if (zone_list(zids, &nzents) != 0) {
6360Sstevel@tonic-gate 		zperror(gettext("failed to get zone list"), B_FALSE);
6370Sstevel@tonic-gate 		free(zids);
6380Sstevel@tonic-gate 		return (Z_ERR);
6390Sstevel@tonic-gate 	}
6400Sstevel@tonic-gate 	if (nzents != nzents_saved) {
6410Sstevel@tonic-gate 		/* list changed, try again */
6420Sstevel@tonic-gate 		free(zids);
6430Sstevel@tonic-gate 		goto again;
6440Sstevel@tonic-gate 	}
6450Sstevel@tonic-gate 
6460Sstevel@tonic-gate 	zents = safe_calloc(nzents, sizeof (zone_entry_t));
6470Sstevel@tonic-gate 
648766Scarlsonj 	inaltroot = zonecfg_in_alt_root();
649766Scarlsonj 	if (inaltroot)
650766Scarlsonj 		fp = zonecfg_open_scratch("", B_FALSE);
651766Scarlsonj 	else
652766Scarlsonj 		fp = NULL;
653766Scarlsonj 	zentp = zents;
654766Scarlsonj 	retv = Z_OK;
6550Sstevel@tonic-gate 	for (i = 0; i < nzents; i++) {
6560Sstevel@tonic-gate 		char name[ZONENAME_MAX];
657766Scarlsonj 		char altname[ZONENAME_MAX];
6580Sstevel@tonic-gate 
659766Scarlsonj 		if (getzonenamebyid(zids[i], name, sizeof (name)) < 0) {
660*8942Sgerald.jelinek@sun.com 			/*
661*8942Sgerald.jelinek@sun.com 			 * There is a race condition where the zone may have
662*8942Sgerald.jelinek@sun.com 			 * shutdown since we retrieved the number of running
663*8942Sgerald.jelinek@sun.com 			 * zones above.  This is not an error, there will be
664*8942Sgerald.jelinek@sun.com 			 * an empty slot at the end of the list.
665*8942Sgerald.jelinek@sun.com 			 */
666766Scarlsonj 			continue;
667766Scarlsonj 		}
668766Scarlsonj 		if (zonecfg_is_scratch(name)) {
669766Scarlsonj 			/* Ignore scratch zones by default */
670766Scarlsonj 			if (!inaltroot)
671766Scarlsonj 				continue;
672766Scarlsonj 			if (fp == NULL ||
673766Scarlsonj 			    zonecfg_reverse_scratch(fp, name, altname,
674766Scarlsonj 			    sizeof (altname), NULL, 0) == -1) {
675924Sgjelinek 				zerror(gettext("could not resolve scratch "
676766Scarlsonj 				    "zone %s"), name);
677766Scarlsonj 				retv = Z_ERR;
678766Scarlsonj 				continue;
679766Scarlsonj 			}
680766Scarlsonj 			(void) strcpy(name, altname);
681766Scarlsonj 		} else {
682766Scarlsonj 			/* Ignore non-scratch when in an alternate root */
683766Scarlsonj 			if (inaltroot && strcmp(name, GLOBAL_ZONENAME) != 0)
684766Scarlsonj 				continue;
685766Scarlsonj 		}
686766Scarlsonj 		if (lookup_zone_info(name, zids[i], zentp) != Z_OK) {
687*8942Sgerald.jelinek@sun.com 			/*
688*8942Sgerald.jelinek@sun.com 			 * There is a race condition where the zone may have
689*8942Sgerald.jelinek@sun.com 			 * shutdown since we retrieved the number of running
690*8942Sgerald.jelinek@sun.com 			 * zones above.  This is not an error, there will be
691*8942Sgerald.jelinek@sun.com 			 * an empty slot at the end of the list.
692*8942Sgerald.jelinek@sun.com 			 */
693766Scarlsonj 			continue;
694766Scarlsonj 		}
695766Scarlsonj 		zentp++;
6960Sstevel@tonic-gate 	}
697766Scarlsonj 	nzents = zentp - zents;
698766Scarlsonj 	if (fp != NULL)
699766Scarlsonj 		zonecfg_close_scratch(fp);
7000Sstevel@tonic-gate 
7010Sstevel@tonic-gate 	free(zids);
702766Scarlsonj 	return (retv);
7030Sstevel@tonic-gate }
7040Sstevel@tonic-gate 
705766Scarlsonj static int
7060Sstevel@tonic-gate zone_print_list(zone_state_t min_state, boolean_t verbose, boolean_t parsable)
7070Sstevel@tonic-gate {
7080Sstevel@tonic-gate 	int i;
7090Sstevel@tonic-gate 	zone_entry_t zent;
7100Sstevel@tonic-gate 	FILE *cookie;
7110Sstevel@tonic-gate 	char *name;
7120Sstevel@tonic-gate 
7130Sstevel@tonic-gate 	/*
7140Sstevel@tonic-gate 	 * First get the list of running zones from the kernel and print them.
7150Sstevel@tonic-gate 	 * If that is all we need, then return.
7160Sstevel@tonic-gate 	 */
717766Scarlsonj 	if ((i = fetch_zents()) != Z_OK) {
7180Sstevel@tonic-gate 		/*
7190Sstevel@tonic-gate 		 * No need for error messages; fetch_zents() has already taken
7200Sstevel@tonic-gate 		 * care of this.
7210Sstevel@tonic-gate 		 */
722766Scarlsonj 		return (i);
7230Sstevel@tonic-gate 	}
724766Scarlsonj 	for (i = 0; i < nzents; i++)
7250Sstevel@tonic-gate 		zone_print(&zents[i], verbose, parsable);
7260Sstevel@tonic-gate 	if (min_state >= ZONE_STATE_RUNNING)
727766Scarlsonj 		return (Z_OK);
7280Sstevel@tonic-gate 	/*
7290Sstevel@tonic-gate 	 * Next, get the full list of zones from the configuration, skipping
7300Sstevel@tonic-gate 	 * any we have already printed.
7310Sstevel@tonic-gate 	 */
7320Sstevel@tonic-gate 	cookie = setzoneent();
7330Sstevel@tonic-gate 	while ((name = getzoneent(cookie)) != NULL) {
7340Sstevel@tonic-gate 		for (i = 0; i < nzents; i++) {
7350Sstevel@tonic-gate 			if (strcmp(zents[i].zname, name) == 0)
7360Sstevel@tonic-gate 				break;
7370Sstevel@tonic-gate 		}
7380Sstevel@tonic-gate 		if (i < nzents) {
7390Sstevel@tonic-gate 			free(name);
7400Sstevel@tonic-gate 			continue;
7410Sstevel@tonic-gate 		}
742766Scarlsonj 		if (lookup_zone_info(name, ZONE_ID_UNDEFINED, &zent) != Z_OK) {
7430Sstevel@tonic-gate 			free(name);
7440Sstevel@tonic-gate 			continue;
7450Sstevel@tonic-gate 		}
7460Sstevel@tonic-gate 		free(name);
7470Sstevel@tonic-gate 		if (zent.zstate_num >= min_state)
7480Sstevel@tonic-gate 			zone_print(&zent, verbose, parsable);
7490Sstevel@tonic-gate 	}
7500Sstevel@tonic-gate 	endzoneent(cookie);
751766Scarlsonj 	return (Z_OK);
7520Sstevel@tonic-gate }
7530Sstevel@tonic-gate 
7548083SJordan.Vaughan@Sun.com /*
7558083SJordan.Vaughan@Sun.com  * Retrieve a zone entry by name.  Returns NULL if no such zone exists.
7568083SJordan.Vaughan@Sun.com  */
7570Sstevel@tonic-gate static zone_entry_t *
7588083SJordan.Vaughan@Sun.com lookup_running_zone(const char *str)
7590Sstevel@tonic-gate {
7600Sstevel@tonic-gate 	int i;
7610Sstevel@tonic-gate 
7620Sstevel@tonic-gate 	if (fetch_zents() != Z_OK)
7630Sstevel@tonic-gate 		return (NULL);
7640Sstevel@tonic-gate 
7650Sstevel@tonic-gate 	for (i = 0; i < nzents; i++) {
7660Sstevel@tonic-gate 		if (strcmp(str, zents[i].zname) == 0)
7670Sstevel@tonic-gate 			return (&zents[i]);
7680Sstevel@tonic-gate 	}
7690Sstevel@tonic-gate 	return (NULL);
7700Sstevel@tonic-gate }
7710Sstevel@tonic-gate 
7720Sstevel@tonic-gate /*
7730Sstevel@tonic-gate  * Check a bit in a mode_t: if on is B_TRUE, that bit should be on; if
7740Sstevel@tonic-gate  * B_FALSE, it should be off.  Return B_TRUE if the mode is bad (incorrect).
7750Sstevel@tonic-gate  */
7760Sstevel@tonic-gate static boolean_t
7770Sstevel@tonic-gate bad_mode_bit(mode_t mode, mode_t bit, boolean_t on, char *file)
7780Sstevel@tonic-gate {
7790Sstevel@tonic-gate 	char *str;
7800Sstevel@tonic-gate 
7810Sstevel@tonic-gate 	assert(bit == S_IRUSR || bit == S_IWUSR || bit == S_IXUSR ||
7820Sstevel@tonic-gate 	    bit == S_IRGRP || bit == S_IWGRP || bit == S_IXGRP ||
7830Sstevel@tonic-gate 	    bit == S_IROTH || bit == S_IWOTH || bit == S_IXOTH);
7840Sstevel@tonic-gate 	/*
7850Sstevel@tonic-gate 	 * TRANSLATION_NOTE
7860Sstevel@tonic-gate 	 * The strings below will be used as part of a larger message,
7870Sstevel@tonic-gate 	 * either:
7880Sstevel@tonic-gate 	 * (file name) must be (owner|group|world) (read|writ|execut)able
7890Sstevel@tonic-gate 	 * or
7900Sstevel@tonic-gate 	 * (file name) must not be (owner|group|world) (read|writ|execut)able
7910Sstevel@tonic-gate 	 */
7920Sstevel@tonic-gate 	switch (bit) {
7930Sstevel@tonic-gate 	case S_IRUSR:
7940Sstevel@tonic-gate 		str = gettext("owner readable");
7950Sstevel@tonic-gate 		break;
7960Sstevel@tonic-gate 	case S_IWUSR:
7970Sstevel@tonic-gate 		str = gettext("owner writable");
7980Sstevel@tonic-gate 		break;
7990Sstevel@tonic-gate 	case S_IXUSR:
8000Sstevel@tonic-gate 		str = gettext("owner executable");
8010Sstevel@tonic-gate 		break;
8020Sstevel@tonic-gate 	case S_IRGRP:
8030Sstevel@tonic-gate 		str = gettext("group readable");
8040Sstevel@tonic-gate 		break;
8050Sstevel@tonic-gate 	case S_IWGRP:
8060Sstevel@tonic-gate 		str = gettext("group writable");
8070Sstevel@tonic-gate 		break;
8080Sstevel@tonic-gate 	case S_IXGRP:
8090Sstevel@tonic-gate 		str = gettext("group executable");
8100Sstevel@tonic-gate 		break;
8110Sstevel@tonic-gate 	case S_IROTH:
8120Sstevel@tonic-gate 		str = gettext("world readable");
8130Sstevel@tonic-gate 		break;
8140Sstevel@tonic-gate 	case S_IWOTH:
8150Sstevel@tonic-gate 		str = gettext("world writable");
8160Sstevel@tonic-gate 		break;
8170Sstevel@tonic-gate 	case S_IXOTH:
8180Sstevel@tonic-gate 		str = gettext("world executable");
8190Sstevel@tonic-gate 		break;
8200Sstevel@tonic-gate 	}
8210Sstevel@tonic-gate 	if ((mode & bit) == (on ? 0 : bit)) {
8220Sstevel@tonic-gate 		/*
8230Sstevel@tonic-gate 		 * TRANSLATION_NOTE
8240Sstevel@tonic-gate 		 * The first parameter below is a file name; the second
8250Sstevel@tonic-gate 		 * is one of the "(owner|group|world) (read|writ|execut)able"
8260Sstevel@tonic-gate 		 * strings from above.
8270Sstevel@tonic-gate 		 */
8280Sstevel@tonic-gate 		/*
8290Sstevel@tonic-gate 		 * The code below could be simplified but not in a way
8300Sstevel@tonic-gate 		 * that would easily translate to non-English locales.
8310Sstevel@tonic-gate 		 */
8320Sstevel@tonic-gate 		if (on) {
8330Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("%s must be %s.\n"),
8340Sstevel@tonic-gate 			    file, str);
8350Sstevel@tonic-gate 		} else {
8360Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("%s must not be %s.\n"),
8370Sstevel@tonic-gate 			    file, str);
8380Sstevel@tonic-gate 		}
8390Sstevel@tonic-gate 		return (B_TRUE);
8400Sstevel@tonic-gate 	}
8410Sstevel@tonic-gate 	return (B_FALSE);
8420Sstevel@tonic-gate }
8430Sstevel@tonic-gate 
8440Sstevel@tonic-gate /*
8450Sstevel@tonic-gate  * We want to make sure that no zone has its zone path as a child node
8460Sstevel@tonic-gate  * (in the directory sense) of any other.  We do that by comparing this
8470Sstevel@tonic-gate  * zone's path to the path of all other (non-global) zones.  The comparison
8480Sstevel@tonic-gate  * in each case is simple: add '/' to the end of the path, then do a
8490Sstevel@tonic-gate  * strncmp() of the two paths, using the length of the shorter one.
8500Sstevel@tonic-gate  */
8510Sstevel@tonic-gate 
8520Sstevel@tonic-gate static int
8530Sstevel@tonic-gate crosscheck_zonepaths(char *path)
8540Sstevel@tonic-gate {
8550Sstevel@tonic-gate 	char rpath[MAXPATHLEN];		/* resolved path */
8560Sstevel@tonic-gate 	char path_copy[MAXPATHLEN];	/* copy of original path */
8570Sstevel@tonic-gate 	char rpath_copy[MAXPATHLEN];	/* copy of original rpath */
8580Sstevel@tonic-gate 	struct zoneent *ze;
8590Sstevel@tonic-gate 	int res, err;
8600Sstevel@tonic-gate 	FILE *cookie;
8610Sstevel@tonic-gate 
8620Sstevel@tonic-gate 	cookie = setzoneent();
8630Sstevel@tonic-gate 	while ((ze = getzoneent_private(cookie)) != NULL) {
8640Sstevel@tonic-gate 		/* Skip zones which are not installed. */
8650Sstevel@tonic-gate 		if (ze->zone_state < ZONE_STATE_INSTALLED) {
8660Sstevel@tonic-gate 			free(ze);
8670Sstevel@tonic-gate 			continue;
8680Sstevel@tonic-gate 		}
8690Sstevel@tonic-gate 		/* Skip the global zone and the current target zone. */
8700Sstevel@tonic-gate 		if (strcmp(ze->zone_name, GLOBAL_ZONENAME) == 0 ||
8710Sstevel@tonic-gate 		    strcmp(ze->zone_name, target_zone) == 0) {
8720Sstevel@tonic-gate 			free(ze);
8730Sstevel@tonic-gate 			continue;
8740Sstevel@tonic-gate 		}
8750Sstevel@tonic-gate 		if (strlen(ze->zone_path) == 0) {
8760Sstevel@tonic-gate 			/* old index file without path, fall back */
8770Sstevel@tonic-gate 			if ((err = zone_get_zonepath(ze->zone_name,
8780Sstevel@tonic-gate 			    ze->zone_path, sizeof (ze->zone_path))) != Z_OK) {
8790Sstevel@tonic-gate 				errno = err;
8800Sstevel@tonic-gate 				zperror2(ze->zone_name,
8810Sstevel@tonic-gate 				    gettext("could not get zone path"));
8820Sstevel@tonic-gate 				free(ze);
8830Sstevel@tonic-gate 				continue;
8840Sstevel@tonic-gate 			}
8850Sstevel@tonic-gate 		}
886766Scarlsonj 		(void) snprintf(path_copy, sizeof (path_copy), "%s%s",
887766Scarlsonj 		    zonecfg_get_root(), ze->zone_path);
888766Scarlsonj 		res = resolvepath(path_copy, rpath, sizeof (rpath));
8890Sstevel@tonic-gate 		if (res == -1) {
8900Sstevel@tonic-gate 			if (errno != ENOENT) {
891766Scarlsonj 				zperror(path_copy, B_FALSE);
8920Sstevel@tonic-gate 				free(ze);
8930Sstevel@tonic-gate 				return (Z_ERR);
8940Sstevel@tonic-gate 			}
8950Sstevel@tonic-gate 			(void) printf(gettext("WARNING: zone %s is installed, "
8960Sstevel@tonic-gate 			    "but its %s %s does not exist.\n"), ze->zone_name,
897766Scarlsonj 			    "zonepath", path_copy);
8980Sstevel@tonic-gate 			free(ze);
8990Sstevel@tonic-gate 			continue;
9000Sstevel@tonic-gate 		}
9010Sstevel@tonic-gate 		rpath[res] = '\0';
9020Sstevel@tonic-gate 		(void) snprintf(path_copy, sizeof (path_copy), "%s/", path);
9030Sstevel@tonic-gate 		(void) snprintf(rpath_copy, sizeof (rpath_copy), "%s/", rpath);
9040Sstevel@tonic-gate 		if (strncmp(path_copy, rpath_copy,
9050Sstevel@tonic-gate 		    min(strlen(path_copy), strlen(rpath_copy))) == 0) {
906924Sgjelinek 			/*
907924Sgjelinek 			 * TRANSLATION_NOTE
908924Sgjelinek 			 * zonepath is a literal that should not be translated.
909924Sgjelinek 			 */
9100Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("%s zonepath (%s) and "
9110Sstevel@tonic-gate 			    "%s zonepath (%s) overlap.\n"),
9120Sstevel@tonic-gate 			    target_zone, path, ze->zone_name, rpath);
9130Sstevel@tonic-gate 			free(ze);
9140Sstevel@tonic-gate 			return (Z_ERR);
9150Sstevel@tonic-gate 		}
9160Sstevel@tonic-gate 		free(ze);
9170Sstevel@tonic-gate 	}
9180Sstevel@tonic-gate 	endzoneent(cookie);
9190Sstevel@tonic-gate 	return (Z_OK);
9200Sstevel@tonic-gate }
9210Sstevel@tonic-gate 
9220Sstevel@tonic-gate static int
9230Sstevel@tonic-gate validate_zonepath(char *path, int cmd_num)
9240Sstevel@tonic-gate {
9250Sstevel@tonic-gate 	int res;			/* result of last library/system call */
9260Sstevel@tonic-gate 	boolean_t err = B_FALSE;	/* have we run into an error? */
9270Sstevel@tonic-gate 	struct stat stbuf;
9282267Sdp 	struct statvfs64 vfsbuf;
9290Sstevel@tonic-gate 	char rpath[MAXPATHLEN];		/* resolved path */
9300Sstevel@tonic-gate 	char ppath[MAXPATHLEN];		/* parent path */
9310Sstevel@tonic-gate 	char rppath[MAXPATHLEN];	/* resolved parent path */
9320Sstevel@tonic-gate 	char rootpath[MAXPATHLEN];	/* root path */
9330Sstevel@tonic-gate 	zone_state_t state;
9340Sstevel@tonic-gate 
9350Sstevel@tonic-gate 	if (path[0] != '/') {
9360Sstevel@tonic-gate 		(void) fprintf(stderr,
9370Sstevel@tonic-gate 		    gettext("%s is not an absolute path.\n"), path);
9380Sstevel@tonic-gate 		return (Z_ERR);
9390Sstevel@tonic-gate 	}
9400Sstevel@tonic-gate 	if ((res = resolvepath(path, rpath, sizeof (rpath))) == -1) {
9410Sstevel@tonic-gate 		if ((errno != ENOENT) ||
9421300Sgjelinek 		    (cmd_num != CMD_VERIFY && cmd_num != CMD_INSTALL &&
9431300Sgjelinek 		    cmd_num != CMD_CLONE && cmd_num != CMD_MOVE)) {
9440Sstevel@tonic-gate 			zperror(path, B_FALSE);
9450Sstevel@tonic-gate 			return (Z_ERR);
9460Sstevel@tonic-gate 		}
9470Sstevel@tonic-gate 		if (cmd_num == CMD_VERIFY) {
948924Sgjelinek 			/*
949924Sgjelinek 			 * TRANSLATION_NOTE
950924Sgjelinek 			 * zoneadm is a literal that should not be translated.
951924Sgjelinek 			 */
9520Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("WARNING: %s does not "
953924Sgjelinek 			    "exist, so it could not be verified.\nWhen "
954924Sgjelinek 			    "'zoneadm %s' is run, '%s' will try to create\n%s, "
955924Sgjelinek 			    "and '%s' will be tried again,\nbut the '%s' may "
956924Sgjelinek 			    "fail if:\nthe parent directory of %s is group- or "
957924Sgjelinek 			    "other-writable\nor\n%s overlaps with any other "
9580Sstevel@tonic-gate 			    "installed zones.\n"), path,
9590Sstevel@tonic-gate 			    cmd_to_str(CMD_INSTALL), cmd_to_str(CMD_INSTALL),
9600Sstevel@tonic-gate 			    path, cmd_to_str(CMD_VERIFY),
9610Sstevel@tonic-gate 			    cmd_to_str(CMD_VERIFY), path, path);
9620Sstevel@tonic-gate 			return (Z_OK);
9630Sstevel@tonic-gate 		}
9640Sstevel@tonic-gate 		/*
9650Sstevel@tonic-gate 		 * The zonepath is supposed to be mode 700 but its
9660Sstevel@tonic-gate 		 * parent(s) 755.  So use 755 on the mkdirp() then
9670Sstevel@tonic-gate 		 * chmod() the zonepath itself to 700.
9680Sstevel@tonic-gate 		 */
9690Sstevel@tonic-gate 		if (mkdirp(path, DEFAULT_DIR_MODE) < 0) {
9700Sstevel@tonic-gate 			zperror(path, B_FALSE);
9710Sstevel@tonic-gate 			return (Z_ERR);
9720Sstevel@tonic-gate 		}
9730Sstevel@tonic-gate 		/*
9740Sstevel@tonic-gate 		 * If the chmod() fails, report the error, but might
9750Sstevel@tonic-gate 		 * as well continue the verify procedure.
9760Sstevel@tonic-gate 		 */
9770Sstevel@tonic-gate 		if (chmod(path, S_IRWXU) != 0)
9780Sstevel@tonic-gate 			zperror(path, B_FALSE);
9790Sstevel@tonic-gate 		/*
9800Sstevel@tonic-gate 		 * Since the mkdir() succeeded, we should not have to
9810Sstevel@tonic-gate 		 * worry about a subsequent ENOENT, thus this should
9820Sstevel@tonic-gate 		 * only recurse once.
9830Sstevel@tonic-gate 		 */
9841300Sgjelinek 		return (validate_zonepath(path, cmd_num));
9850Sstevel@tonic-gate 	}
9860Sstevel@tonic-gate 	rpath[res] = '\0';
9870Sstevel@tonic-gate 	if (strcmp(path, rpath) != 0) {
9880Sstevel@tonic-gate 		errno = Z_RESOLVED_PATH;
9890Sstevel@tonic-gate 		zperror(path, B_TRUE);
9900Sstevel@tonic-gate 		return (Z_ERR);
9910Sstevel@tonic-gate 	}
9920Sstevel@tonic-gate 	if ((res = stat(rpath, &stbuf)) != 0) {
9930Sstevel@tonic-gate 		zperror(rpath, B_FALSE);
9940Sstevel@tonic-gate 		return (Z_ERR);
9950Sstevel@tonic-gate 	}
9960Sstevel@tonic-gate 	if (!S_ISDIR(stbuf.st_mode)) {
9970Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("%s is not a directory.\n"),
9980Sstevel@tonic-gate 		    rpath);
9990Sstevel@tonic-gate 		return (Z_ERR);
10000Sstevel@tonic-gate 	}
10013445Sblakej 	if (strcmp(stbuf.st_fstype, MNTTYPE_TMPFS) == 0) {
10020Sstevel@tonic-gate 		(void) printf(gettext("WARNING: %s is on a temporary "
10031867Sgjelinek 		    "file system.\n"), rpath);
10040Sstevel@tonic-gate 	}
10050Sstevel@tonic-gate 	if (crosscheck_zonepaths(rpath) != Z_OK)
10060Sstevel@tonic-gate 		return (Z_ERR);
10070Sstevel@tonic-gate 	/*
10080Sstevel@tonic-gate 	 * Try to collect and report as many minor errors as possible
10090Sstevel@tonic-gate 	 * before returning, so the user can learn everything that needs
10100Sstevel@tonic-gate 	 * to be fixed up front.
10110Sstevel@tonic-gate 	 */
10120Sstevel@tonic-gate 	if (stbuf.st_uid != 0) {
10130Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("%s is not owned by root.\n"),
10140Sstevel@tonic-gate 		    rpath);
10150Sstevel@tonic-gate 		err = B_TRUE;
10160Sstevel@tonic-gate 	}
10170Sstevel@tonic-gate 	err |= bad_mode_bit(stbuf.st_mode, S_IRUSR, B_TRUE, rpath);
10180Sstevel@tonic-gate 	err |= bad_mode_bit(stbuf.st_mode, S_IWUSR, B_TRUE, rpath);
10190Sstevel@tonic-gate 	err |= bad_mode_bit(stbuf.st_mode, S_IXUSR, B_TRUE, rpath);
10200Sstevel@tonic-gate 	err |= bad_mode_bit(stbuf.st_mode, S_IRGRP, B_FALSE, rpath);
10210Sstevel@tonic-gate 	err |= bad_mode_bit(stbuf.st_mode, S_IWGRP, B_FALSE, rpath);
10220Sstevel@tonic-gate 	err |= bad_mode_bit(stbuf.st_mode, S_IXGRP, B_FALSE, rpath);
10230Sstevel@tonic-gate 	err |= bad_mode_bit(stbuf.st_mode, S_IROTH, B_FALSE, rpath);
10240Sstevel@tonic-gate 	err |= bad_mode_bit(stbuf.st_mode, S_IWOTH, B_FALSE, rpath);
10250Sstevel@tonic-gate 	err |= bad_mode_bit(stbuf.st_mode, S_IXOTH, B_FALSE, rpath);
10260Sstevel@tonic-gate 
10270Sstevel@tonic-gate 	(void) snprintf(ppath, sizeof (ppath), "%s/..", path);
10280Sstevel@tonic-gate 	if ((res = resolvepath(ppath, rppath, sizeof (rppath))) == -1) {
10290Sstevel@tonic-gate 		zperror(ppath, B_FALSE);
10300Sstevel@tonic-gate 		return (Z_ERR);
10310Sstevel@tonic-gate 	}
10320Sstevel@tonic-gate 	rppath[res] = '\0';
10330Sstevel@tonic-gate 	if ((res = stat(rppath, &stbuf)) != 0) {
10340Sstevel@tonic-gate 		zperror(rppath, B_FALSE);
10350Sstevel@tonic-gate 		return (Z_ERR);
10360Sstevel@tonic-gate 	}
10370Sstevel@tonic-gate 	/* theoretically impossible */
10380Sstevel@tonic-gate 	if (!S_ISDIR(stbuf.st_mode)) {
10390Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("%s is not a directory.\n"),
10400Sstevel@tonic-gate 		    rppath);
10410Sstevel@tonic-gate 		return (Z_ERR);
10420Sstevel@tonic-gate 	}
10430Sstevel@tonic-gate 	if (stbuf.st_uid != 0) {
10440Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("%s is not owned by root.\n"),
10450Sstevel@tonic-gate 		    rppath);
10460Sstevel@tonic-gate 		err = B_TRUE;
10470Sstevel@tonic-gate 	}
10480Sstevel@tonic-gate 	err |= bad_mode_bit(stbuf.st_mode, S_IRUSR, B_TRUE, rppath);
10490Sstevel@tonic-gate 	err |= bad_mode_bit(stbuf.st_mode, S_IWUSR, B_TRUE, rppath);
10500Sstevel@tonic-gate 	err |= bad_mode_bit(stbuf.st_mode, S_IXUSR, B_TRUE, rppath);
10510Sstevel@tonic-gate 	err |= bad_mode_bit(stbuf.st_mode, S_IWGRP, B_FALSE, rppath);
10520Sstevel@tonic-gate 	err |= bad_mode_bit(stbuf.st_mode, S_IWOTH, B_FALSE, rppath);
10530Sstevel@tonic-gate 	if (strcmp(rpath, rppath) == 0) {
10540Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("%s is its own parent.\n"),
10550Sstevel@tonic-gate 		    rppath);
10560Sstevel@tonic-gate 		err = B_TRUE;
10570Sstevel@tonic-gate 	}
10580Sstevel@tonic-gate 
10592267Sdp 	if (statvfs64(rpath, &vfsbuf) != 0) {
10600Sstevel@tonic-gate 		zperror(rpath, B_FALSE);
10610Sstevel@tonic-gate 		return (Z_ERR);
10620Sstevel@tonic-gate 	}
1063823Sgjelinek 	if (strcmp(vfsbuf.f_basetype, MNTTYPE_NFS) == 0) {
1064924Sgjelinek 		/*
1065924Sgjelinek 		 * TRANSLATION_NOTE
1066924Sgjelinek 		 * Zonepath and NFS are literals that should not be translated.
1067924Sgjelinek 		 */
1068924Sgjelinek 		(void) fprintf(stderr, gettext("Zonepath %s is on an NFS "
10691867Sgjelinek 		    "mounted file system.\n"
10701867Sgjelinek 		    "\tA local file system must be used.\n"), rpath);
1071823Sgjelinek 		return (Z_ERR);
1072823Sgjelinek 	}
1073823Sgjelinek 	if (vfsbuf.f_flag & ST_NOSUID) {
1074924Sgjelinek 		/*
1075924Sgjelinek 		 * TRANSLATION_NOTE
1076924Sgjelinek 		 * Zonepath and nosuid are literals that should not be
1077924Sgjelinek 		 * translated.
1078924Sgjelinek 		 */
1079823Sgjelinek 		(void) fprintf(stderr, gettext("Zonepath %s is on a nosuid "
10801867Sgjelinek 		    "file system.\n"), rpath);
10810Sstevel@tonic-gate 		return (Z_ERR);
10820Sstevel@tonic-gate 	}
10830Sstevel@tonic-gate 
10840Sstevel@tonic-gate 	if ((res = zone_get_state(target_zone, &state)) != Z_OK) {
10850Sstevel@tonic-gate 		errno = res;
10860Sstevel@tonic-gate 		zperror2(target_zone, gettext("could not get state"));
10870Sstevel@tonic-gate 		return (Z_ERR);
10880Sstevel@tonic-gate 	}
10890Sstevel@tonic-gate 	/*
10900Sstevel@tonic-gate 	 * The existence of the root path is only bad in the configured state,
10910Sstevel@tonic-gate 	 * as it is *supposed* to be there at the installed and later states.
10921507Sgjelinek 	 * However, the root path is expected to be there if the zone is
10931507Sgjelinek 	 * detached.
10940Sstevel@tonic-gate 	 * State/command mismatches are caught earlier in verify_details().
10950Sstevel@tonic-gate 	 */
10961507Sgjelinek 	if (state == ZONE_STATE_CONFIGURED && cmd_num != CMD_ATTACH) {
10970Sstevel@tonic-gate 		if (snprintf(rootpath, sizeof (rootpath), "%s/root", rpath) >=
10980Sstevel@tonic-gate 		    sizeof (rootpath)) {
1099924Sgjelinek 			/*
1100924Sgjelinek 			 * TRANSLATION_NOTE
1101924Sgjelinek 			 * Zonepath is a literal that should not be translated.
1102924Sgjelinek 			 */
11030Sstevel@tonic-gate 			(void) fprintf(stderr,
11040Sstevel@tonic-gate 			    gettext("Zonepath %s is too long.\n"), rpath);
11050Sstevel@tonic-gate 			return (Z_ERR);
11060Sstevel@tonic-gate 		}
11070Sstevel@tonic-gate 		if ((res = stat(rootpath, &stbuf)) == 0) {
11087782Sgerald.jelinek@sun.com 			if (zonecfg_detached(rpath)) {
11091507Sgjelinek 				(void) fprintf(stderr,
11101507Sgjelinek 				    gettext("Cannot %s detached "
11111507Sgjelinek 				    "zone.\nUse attach or remove %s "
11121507Sgjelinek 				    "directory.\n"), cmd_to_str(cmd_num),
11131507Sgjelinek 				    rpath);
11147782Sgerald.jelinek@sun.com 				return (Z_ERR);
11157782Sgerald.jelinek@sun.com 			}
11167782Sgerald.jelinek@sun.com 
11177782Sgerald.jelinek@sun.com 			/* Not detached, check if it really looks ok. */
11187782Sgerald.jelinek@sun.com 
11197782Sgerald.jelinek@sun.com 			if (!S_ISDIR(stbuf.st_mode)) {
11207782Sgerald.jelinek@sun.com 				(void) fprintf(stderr, gettext("%s is not a "
11217782Sgerald.jelinek@sun.com 				    "directory.\n"), rootpath);
11227782Sgerald.jelinek@sun.com 				return (Z_ERR);
11237782Sgerald.jelinek@sun.com 			}
11247782Sgerald.jelinek@sun.com 
11257782Sgerald.jelinek@sun.com 			if (stbuf.st_uid != 0) {
11267782Sgerald.jelinek@sun.com 				(void) fprintf(stderr, gettext("%s is not "
11277782Sgerald.jelinek@sun.com 				    "owned by root.\n"), rootpath);
11287782Sgerald.jelinek@sun.com 				return (Z_ERR);
11297782Sgerald.jelinek@sun.com 			}
11307782Sgerald.jelinek@sun.com 
11317782Sgerald.jelinek@sun.com 			if ((stbuf.st_mode & 0777) != 0755) {
11327782Sgerald.jelinek@sun.com 				(void) fprintf(stderr, gettext("%s mode is not "
11337782Sgerald.jelinek@sun.com 				    "0755.\n"), rootpath);
11347782Sgerald.jelinek@sun.com 				return (Z_ERR);
11357782Sgerald.jelinek@sun.com 			}
11360Sstevel@tonic-gate 		}
11370Sstevel@tonic-gate 	}
11380Sstevel@tonic-gate 
11390Sstevel@tonic-gate 	return (err ? Z_ERR : Z_OK);
11400Sstevel@tonic-gate }
11410Sstevel@tonic-gate 
11420Sstevel@tonic-gate static int
11433339Szt129084 invoke_brand_handler(int cmd_num, char *argv[])
11443339Szt129084 {
11453339Szt129084 	zone_dochandle_t handle;
11463339Szt129084 	int err;
11473339Szt129084 
11483339Szt129084 	if ((handle = zonecfg_init_handle()) == NULL) {
11493339Szt129084 		zperror(cmd_to_str(cmd_num), B_TRUE);
11503339Szt129084 		return (Z_ERR);
11513339Szt129084 	}
11523339Szt129084 	if ((err = zonecfg_get_handle(target_zone, handle)) != Z_OK) {
11533339Szt129084 		errno = err;
11543339Szt129084 		zperror(cmd_to_str(cmd_num), B_TRUE);
11553339Szt129084 		zonecfg_fini_handle(handle);
11563339Szt129084 		return (Z_ERR);
11573339Szt129084 	}
11583339Szt129084 	if (verify_brand(handle, cmd_num, argv) != Z_OK) {
11593339Szt129084 		zonecfg_fini_handle(handle);
11603339Szt129084 		return (Z_ERR);
11613339Szt129084 	}
11623339Szt129084 	zonecfg_fini_handle(handle);
11633339Szt129084 	return (Z_OK);
11643339Szt129084 }
11653339Szt129084 
11663339Szt129084 static int
11670Sstevel@tonic-gate ready_func(int argc, char *argv[])
11680Sstevel@tonic-gate {
11690Sstevel@tonic-gate 	zone_cmd_arg_t zarg;
11700Sstevel@tonic-gate 	int arg;
11710Sstevel@tonic-gate 
1172766Scarlsonj 	if (zonecfg_in_alt_root()) {
1173766Scarlsonj 		zerror(gettext("cannot ready zone in alternate root"));
1174766Scarlsonj 		return (Z_ERR);
1175766Scarlsonj 	}
1176766Scarlsonj 
11770Sstevel@tonic-gate 	optind = 0;
11780Sstevel@tonic-gate 	if ((arg = getopt(argc, argv, "?")) != EOF) {
11790Sstevel@tonic-gate 		switch (arg) {
11800Sstevel@tonic-gate 		case '?':
11810Sstevel@tonic-gate 			sub_usage(SHELP_READY, CMD_READY);
11820Sstevel@tonic-gate 			return (optopt == '?' ? Z_OK : Z_USAGE);
11830Sstevel@tonic-gate 		default:
11840Sstevel@tonic-gate 			sub_usage(SHELP_READY, CMD_READY);
11850Sstevel@tonic-gate 			return (Z_USAGE);
11860Sstevel@tonic-gate 		}
11870Sstevel@tonic-gate 	}
11880Sstevel@tonic-gate 	if (argc > optind) {
11890Sstevel@tonic-gate 		sub_usage(SHELP_READY, CMD_READY);
11900Sstevel@tonic-gate 		return (Z_USAGE);
11910Sstevel@tonic-gate 	}
11922712Snn35248 	if (sanity_check(target_zone, CMD_READY, B_FALSE, B_FALSE, B_FALSE)
11932712Snn35248 	    != Z_OK)
11940Sstevel@tonic-gate 		return (Z_ERR);
11953339Szt129084 	if (verify_details(CMD_READY, argv) != Z_OK)
11960Sstevel@tonic-gate 		return (Z_ERR);
11970Sstevel@tonic-gate 
11980Sstevel@tonic-gate 	zarg.cmd = Z_READY;
11997089Sgjelinek 	if (zonecfg_call_zoneadmd(target_zone, &zarg, locale, B_TRUE) != 0) {
12000Sstevel@tonic-gate 		zerror(gettext("call to %s failed"), "zoneadmd");
12010Sstevel@tonic-gate 		return (Z_ERR);
12020Sstevel@tonic-gate 	}
12030Sstevel@tonic-gate 	return (Z_OK);
12040Sstevel@tonic-gate }
12050Sstevel@tonic-gate 
12060Sstevel@tonic-gate static int
12070Sstevel@tonic-gate boot_func(int argc, char *argv[])
12080Sstevel@tonic-gate {
12090Sstevel@tonic-gate 	zone_cmd_arg_t zarg;
12102712Snn35248 	boolean_t force = B_FALSE;
12110Sstevel@tonic-gate 	int arg;
12120Sstevel@tonic-gate 
1213766Scarlsonj 	if (zonecfg_in_alt_root()) {
1214766Scarlsonj 		zerror(gettext("cannot boot zone in alternate root"));
1215766Scarlsonj 		return (Z_ERR);
1216766Scarlsonj 	}
1217766Scarlsonj 
12180Sstevel@tonic-gate 	zarg.bootbuf[0] = '\0';
12190Sstevel@tonic-gate 
12200Sstevel@tonic-gate 	/*
12212267Sdp 	 * The following getopt processes arguments to zone boot; that
12222267Sdp 	 * is to say, the [here] portion of the argument string:
12232267Sdp 	 *
12242267Sdp 	 *	zoneadm -z myzone boot [here] -- -v -m verbose
12252267Sdp 	 *
12262267Sdp 	 * Where [here] can either be nothing, -? (in which case we bail
12272712Snn35248 	 * and print usage), -f (a private option to indicate that the
12282712Snn35248 	 * boot operation should be 'forced'), or -s.  Support for -s is
12292712Snn35248 	 * vestigal and obsolete, but is retained because it was a
12302712Snn35248 	 * documented interface and there are known consumers including
12312712Snn35248 	 * admin/install; the proper way to specify boot arguments like -s
12322712Snn35248 	 * is:
12332267Sdp 	 *
12342267Sdp 	 *	zoneadm -z myzone boot -- -s -v -m verbose.
12350Sstevel@tonic-gate 	 */
12360Sstevel@tonic-gate 	optind = 0;
12372712Snn35248 	while ((arg = getopt(argc, argv, "?fs")) != EOF) {
12380Sstevel@tonic-gate 		switch (arg) {
12390Sstevel@tonic-gate 		case '?':
12400Sstevel@tonic-gate 			sub_usage(SHELP_BOOT, CMD_BOOT);
12410Sstevel@tonic-gate 			return (optopt == '?' ? Z_OK : Z_USAGE);
12420Sstevel@tonic-gate 		case 's':
12430Sstevel@tonic-gate 			(void) strlcpy(zarg.bootbuf, "-s",
12440Sstevel@tonic-gate 			    sizeof (zarg.bootbuf));
12450Sstevel@tonic-gate 			break;
12462712Snn35248 		case 'f':
12472712Snn35248 			force = B_TRUE;
12482712Snn35248 			break;
12490Sstevel@tonic-gate 		default:
12500Sstevel@tonic-gate 			sub_usage(SHELP_BOOT, CMD_BOOT);
12510Sstevel@tonic-gate 			return (Z_USAGE);
12520Sstevel@tonic-gate 		}
12530Sstevel@tonic-gate 	}
12542267Sdp 
12552267Sdp 	for (; optind < argc; optind++) {
12562267Sdp 		if (strlcat(zarg.bootbuf, argv[optind],
12572267Sdp 		    sizeof (zarg.bootbuf)) >= sizeof (zarg.bootbuf)) {
12582267Sdp 			zerror(gettext("Boot argument list too long"));
12592267Sdp 			return (Z_ERR);
12602267Sdp 		}
12612267Sdp 		if (optind < argc - 1)
12622267Sdp 			if (strlcat(zarg.bootbuf, " ", sizeof (zarg.bootbuf)) >=
12632267Sdp 			    sizeof (zarg.bootbuf)) {
12642267Sdp 				zerror(gettext("Boot argument list too long"));
12652267Sdp 				return (Z_ERR);
12662267Sdp 			}
12672267Sdp 	}
12682712Snn35248 	if (sanity_check(target_zone, CMD_BOOT, B_FALSE, B_FALSE, force)
12692712Snn35248 	    != Z_OK)
12700Sstevel@tonic-gate 		return (Z_ERR);
12713339Szt129084 	if (verify_details(CMD_BOOT, argv) != Z_OK)
12720Sstevel@tonic-gate 		return (Z_ERR);
12732712Snn35248 	zarg.cmd = force ? Z_FORCEBOOT : Z_BOOT;
12747089Sgjelinek 	if (zonecfg_call_zoneadmd(target_zone, &zarg, locale, B_TRUE) != 0) {
12750Sstevel@tonic-gate 		zerror(gettext("call to %s failed"), "zoneadmd");
12760Sstevel@tonic-gate 		return (Z_ERR);
12770Sstevel@tonic-gate 	}
12783247Sgjelinek 
12790Sstevel@tonic-gate 	return (Z_OK);
12800Sstevel@tonic-gate }
12810Sstevel@tonic-gate 
12820Sstevel@tonic-gate static void
12830Sstevel@tonic-gate fake_up_local_zone(zoneid_t zid, zone_entry_t *zeptr)
12840Sstevel@tonic-gate {
12850Sstevel@tonic-gate 	ssize_t result;
12862303Scarlsonj 	uuid_t uuid;
12872303Scarlsonj 	FILE *fp;
12883448Sdh155122 	ushort_t flags;
12892303Scarlsonj 
12902303Scarlsonj 	(void) memset(zeptr, 0, sizeof (*zeptr));
12910Sstevel@tonic-gate 
12920Sstevel@tonic-gate 	zeptr->zid = zid;
12932303Scarlsonj 
12940Sstevel@tonic-gate 	/*
12950Sstevel@tonic-gate 	 * Since we're looking up our own (non-global) zone name,
12960Sstevel@tonic-gate 	 * we can be assured that it will succeed.
12970Sstevel@tonic-gate 	 */
12980Sstevel@tonic-gate 	result = getzonenamebyid(zid, zeptr->zname, sizeof (zeptr->zname));
12990Sstevel@tonic-gate 	assert(result >= 0);
13002303Scarlsonj 	if (zonecfg_is_scratch(zeptr->zname) &&
13012303Scarlsonj 	    (fp = zonecfg_open_scratch("", B_FALSE)) != NULL) {
13022303Scarlsonj 		(void) zonecfg_reverse_scratch(fp, zeptr->zname, zeptr->zname,
13032303Scarlsonj 		    sizeof (zeptr->zname), NULL, 0);
13042303Scarlsonj 		zonecfg_close_scratch(fp);
13052303Scarlsonj 	}
13062303Scarlsonj 
13072303Scarlsonj 	if (is_system_labeled()) {
13081676Sjpk 		(void) zone_getattr(zid, ZONE_ATTR_ROOT, zeptr->zroot,
13091676Sjpk 		    sizeof (zeptr->zroot));
13102712Snn35248 		(void) strlcpy(zeptr->zbrand, NATIVE_BRAND_NAME,
13114350Std153743 		    sizeof (zeptr->zbrand));
13122303Scarlsonj 	} else {
13132303Scarlsonj 		(void) strlcpy(zeptr->zroot, "/", sizeof (zeptr->zroot));
13142712Snn35248 		(void) zone_getattr(zid, ZONE_ATTR_BRAND, zeptr->zbrand,
13152712Snn35248 		    sizeof (zeptr->zbrand));
13162303Scarlsonj 	}
13172303Scarlsonj 
13180Sstevel@tonic-gate 	zeptr->zstate_str = "running";
13192303Scarlsonj 	if (zonecfg_get_uuid(zeptr->zname, uuid) == Z_OK &&
13202303Scarlsonj 	    !uuid_is_null(uuid))
13212303Scarlsonj 		uuid_unparse(uuid, zeptr->zuuid);
13223448Sdh155122 
13233448Sdh155122 	if (zone_getattr(zid, ZONE_ATTR_FLAGS, &flags, sizeof (flags)) < 0) {
13243448Sdh155122 		zperror2(zeptr->zname, gettext("could not get zone flags"));
13253448Sdh155122 		exit(Z_ERR);
13263448Sdh155122 	}
13273448Sdh155122 	if (flags & ZF_NET_EXCL)
13283448Sdh155122 		zeptr->ziptype = ZS_EXCLUSIVE;
13293448Sdh155122 	else
13303448Sdh155122 		zeptr->ziptype = ZS_SHARED;
13310Sstevel@tonic-gate }
13320Sstevel@tonic-gate 
13330Sstevel@tonic-gate static int
13340Sstevel@tonic-gate list_func(int argc, char *argv[])
13350Sstevel@tonic-gate {
13360Sstevel@tonic-gate 	zone_entry_t *zentp, zent;
1337766Scarlsonj 	int arg, retv;
13380Sstevel@tonic-gate 	boolean_t output = B_FALSE, verbose = B_FALSE, parsable = B_FALSE;
13390Sstevel@tonic-gate 	zone_state_t min_state = ZONE_STATE_RUNNING;
13400Sstevel@tonic-gate 	zoneid_t zone_id = getzoneid();
13410Sstevel@tonic-gate 
13420Sstevel@tonic-gate 	if (target_zone == NULL) {
13430Sstevel@tonic-gate 		/* all zones: default view to running but allow override */
13440Sstevel@tonic-gate 		optind = 0;
13450Sstevel@tonic-gate 		while ((arg = getopt(argc, argv, "?cipv")) != EOF) {
13460Sstevel@tonic-gate 			switch (arg) {
13470Sstevel@tonic-gate 			case '?':
13480Sstevel@tonic-gate 				sub_usage(SHELP_LIST, CMD_LIST);
13490Sstevel@tonic-gate 				return (optopt == '?' ? Z_OK : Z_USAGE);
13501339Sjonb 				/*
13511339Sjonb 				 * The 'i' and 'c' options are not mutually
13521339Sjonb 				 * exclusive so if 'c' is given, then min_state
13531339Sjonb 				 * is set to 0 (ZONE_STATE_CONFIGURED) which is
13541339Sjonb 				 * the lowest possible state.  If 'i' is given,
13551339Sjonb 				 * then min_state is set to be the lowest state
13561339Sjonb 				 * so far.
13571339Sjonb 				 */
13580Sstevel@tonic-gate 			case 'c':
13590Sstevel@tonic-gate 				min_state = ZONE_STATE_CONFIGURED;
13600Sstevel@tonic-gate 				break;
13610Sstevel@tonic-gate 			case 'i':
13621339Sjonb 				min_state = min(ZONE_STATE_INSTALLED,
13631339Sjonb 				    min_state);
13641339Sjonb 
13650Sstevel@tonic-gate 				break;
13660Sstevel@tonic-gate 			case 'p':
13670Sstevel@tonic-gate 				parsable = B_TRUE;
13680Sstevel@tonic-gate 				break;
13690Sstevel@tonic-gate 			case 'v':
13700Sstevel@tonic-gate 				verbose = B_TRUE;
13710Sstevel@tonic-gate 				break;
13720Sstevel@tonic-gate 			default:
13730Sstevel@tonic-gate 				sub_usage(SHELP_LIST, CMD_LIST);
13740Sstevel@tonic-gate 				return (Z_USAGE);
13750Sstevel@tonic-gate 			}
13760Sstevel@tonic-gate 		}
13770Sstevel@tonic-gate 		if (parsable && verbose) {
13780Sstevel@tonic-gate 			zerror(gettext("%s -p and -v are mutually exclusive."),
13790Sstevel@tonic-gate 			    cmd_to_str(CMD_LIST));
13800Sstevel@tonic-gate 			return (Z_ERR);
13810Sstevel@tonic-gate 		}
13821676Sjpk 		if (zone_id == GLOBAL_ZONEID || is_system_labeled()) {
1383766Scarlsonj 			retv = zone_print_list(min_state, verbose, parsable);
13840Sstevel@tonic-gate 		} else {
13852712Snn35248 			fake_up_local_zone(zone_id, &zent);
1386766Scarlsonj 			retv = Z_OK;
13870Sstevel@tonic-gate 			zone_print(&zent, verbose, parsable);
13880Sstevel@tonic-gate 		}
1389766Scarlsonj 		return (retv);
13900Sstevel@tonic-gate 	}
13910Sstevel@tonic-gate 
13920Sstevel@tonic-gate 	/*
13930Sstevel@tonic-gate 	 * Specific target zone: disallow -i/-c suboptions.
13940Sstevel@tonic-gate 	 */
13950Sstevel@tonic-gate 	optind = 0;
13960Sstevel@tonic-gate 	while ((arg = getopt(argc, argv, "?pv")) != EOF) {
13970Sstevel@tonic-gate 		switch (arg) {
13980Sstevel@tonic-gate 		case '?':
13990Sstevel@tonic-gate 			sub_usage(SHELP_LIST, CMD_LIST);
14000Sstevel@tonic-gate 			return (optopt == '?' ? Z_OK : Z_USAGE);
14010Sstevel@tonic-gate 		case 'p':
14020Sstevel@tonic-gate 			parsable = B_TRUE;
14030Sstevel@tonic-gate 			break;
14040Sstevel@tonic-gate 		case 'v':
14050Sstevel@tonic-gate 			verbose = B_TRUE;
14060Sstevel@tonic-gate 			break;
14070Sstevel@tonic-gate 		default:
14080Sstevel@tonic-gate 			sub_usage(SHELP_LIST, CMD_LIST);
14090Sstevel@tonic-gate 			return (Z_USAGE);
14100Sstevel@tonic-gate 		}
14110Sstevel@tonic-gate 	}
14120Sstevel@tonic-gate 	if (parsable && verbose) {
14130Sstevel@tonic-gate 		zerror(gettext("%s -p and -v are mutually exclusive."),
14140Sstevel@tonic-gate 		    cmd_to_str(CMD_LIST));
14150Sstevel@tonic-gate 		return (Z_ERR);
14160Sstevel@tonic-gate 	}
14170Sstevel@tonic-gate 	if (argc > optind) {
14180Sstevel@tonic-gate 		sub_usage(SHELP_LIST, CMD_LIST);
14190Sstevel@tonic-gate 		return (Z_USAGE);
14200Sstevel@tonic-gate 	}
14213979Sgjelinek 	if (zone_id != GLOBAL_ZONEID && !is_system_labeled()) {
14220Sstevel@tonic-gate 		fake_up_local_zone(zone_id, &zent);
14230Sstevel@tonic-gate 		/*
14240Sstevel@tonic-gate 		 * main() will issue a Z_NO_ZONE error if it cannot get an
14250Sstevel@tonic-gate 		 * id for target_zone, which in a non-global zone should
14260Sstevel@tonic-gate 		 * happen for any zone name except `zonename`.  Thus we
14270Sstevel@tonic-gate 		 * assert() that here but don't otherwise check.
14280Sstevel@tonic-gate 		 */
14290Sstevel@tonic-gate 		assert(strcmp(zent.zname, target_zone) == 0);
14300Sstevel@tonic-gate 		zone_print(&zent, verbose, parsable);
14310Sstevel@tonic-gate 		output = B_TRUE;
14320Sstevel@tonic-gate 	} else if ((zentp = lookup_running_zone(target_zone)) != NULL) {
14330Sstevel@tonic-gate 		zone_print(zentp, verbose, parsable);
14340Sstevel@tonic-gate 		output = B_TRUE;
1435766Scarlsonj 	} else if (lookup_zone_info(target_zone, ZONE_ID_UNDEFINED,
1436766Scarlsonj 	    &zent) == Z_OK) {
14370Sstevel@tonic-gate 		zone_print(&zent, verbose, parsable);
14380Sstevel@tonic-gate 		output = B_TRUE;
14390Sstevel@tonic-gate 	}
14403339Szt129084 
14413339Szt129084 	/*
14423339Szt129084 	 * Invoke brand-specific handler. Note that we do this
14433542Sgjelinek 	 * only if we're in the global zone, and target_zone is specified
14443542Sgjelinek 	 * and it is not the global zone.
14453339Szt129084 	 */
14463542Sgjelinek 	if (zone_id == GLOBAL_ZONEID && target_zone != NULL &&
14473542Sgjelinek 	    strcmp(target_zone, GLOBAL_ZONENAME) != 0)
14483339Szt129084 		if (invoke_brand_handler(CMD_LIST, argv) != Z_OK)
14493339Szt129084 			return (Z_ERR);
14503339Szt129084 
14510Sstevel@tonic-gate 	return (output ? Z_OK : Z_ERR);
14520Sstevel@tonic-gate }
14530Sstevel@tonic-gate 
14540Sstevel@tonic-gate static void
14550Sstevel@tonic-gate sigterm(int sig)
14560Sstevel@tonic-gate {
14570Sstevel@tonic-gate 	/*
14580Sstevel@tonic-gate 	 * Ignore SIG{INT,TERM}, so we don't end up in an infinite loop,
14590Sstevel@tonic-gate 	 * then propagate the signal to our process group.
14600Sstevel@tonic-gate 	 */
14612712Snn35248 	assert(sig == SIGINT || sig == SIGTERM);
14620Sstevel@tonic-gate 	(void) sigset(SIGINT, SIG_IGN);
14630Sstevel@tonic-gate 	(void) sigset(SIGTERM, SIG_IGN);
14640Sstevel@tonic-gate 	(void) kill(0, sig);
14650Sstevel@tonic-gate 	child_killed = B_TRUE;
14660Sstevel@tonic-gate }
14670Sstevel@tonic-gate 
14680Sstevel@tonic-gate static int
14690Sstevel@tonic-gate do_subproc(char *cmdbuf)
14700Sstevel@tonic-gate {
14710Sstevel@tonic-gate 	char inbuf[1024];	/* arbitrary large amount */
14720Sstevel@tonic-gate 	FILE *file;
14730Sstevel@tonic-gate 
14742712Snn35248 	do_subproc_cnt++;
14750Sstevel@tonic-gate 	child_killed = B_FALSE;
14760Sstevel@tonic-gate 	/*
14770Sstevel@tonic-gate 	 * We use popen(3c) to launch child processes for [un]install;
14780Sstevel@tonic-gate 	 * this library call does not return a PID, so we have to kill
14790Sstevel@tonic-gate 	 * the whole process group.  To avoid killing our parent, we
14800Sstevel@tonic-gate 	 * become a process group leader here.  But doing so can wreak
14810Sstevel@tonic-gate 	 * havoc with reading from stdin when launched by a non-job-control
14820Sstevel@tonic-gate 	 * shell, so we close stdin and reopen it as /dev/null first.
14830Sstevel@tonic-gate 	 */
14840Sstevel@tonic-gate 	(void) close(STDIN_FILENO);
14852712Snn35248 	(void) openat(STDIN_FILENO, "/dev/null", O_RDONLY);
14862712Snn35248 	if (!zoneadm_is_nested)
14872712Snn35248 		(void) setpgid(0, 0);
14880Sstevel@tonic-gate 	(void) sigset(SIGINT, sigterm);
14890Sstevel@tonic-gate 	(void) sigset(SIGTERM, sigterm);
14900Sstevel@tonic-gate 	file = popen(cmdbuf, "r");
14910Sstevel@tonic-gate 	for (;;) {
14920Sstevel@tonic-gate 		if (child_killed || fgets(inbuf, sizeof (inbuf), file) == NULL)
14930Sstevel@tonic-gate 			break;
14940Sstevel@tonic-gate 		(void) fputs(inbuf, stdout);
14950Sstevel@tonic-gate 	}
14960Sstevel@tonic-gate 	(void) sigset(SIGINT, SIG_DFL);
14970Sstevel@tonic-gate 	(void) sigset(SIGTERM, SIG_DFL);
14980Sstevel@tonic-gate 	return (pclose(file));
14990Sstevel@tonic-gate }
15000Sstevel@tonic-gate 
15017089Sgjelinek int
15022712Snn35248 do_subproc_interactive(char *cmdbuf)
15032712Snn35248 {
15042712Snn35248 	void (*saveint)(int);
15052712Snn35248 	void (*saveterm)(int);
15062712Snn35248 	void (*savequit)(int);
15072712Snn35248 	void (*savehup)(int);
15082712Snn35248 	int pid, child, status;
15092712Snn35248 
15102712Snn35248 	/*
15112712Snn35248 	 * do_subproc() links stdin to /dev/null, which would break any
15122712Snn35248 	 * interactive subprocess we try to launch here.  Similarly, we
15132712Snn35248 	 * can't have been launched as a subprocess ourselves.
15142712Snn35248 	 */
15152712Snn35248 	assert(do_subproc_cnt == 0 && !zoneadm_is_nested);
15162712Snn35248 
15172712Snn35248 	if ((child = vfork()) == 0) {
15182712Snn35248 		(void) execl("/bin/sh", "sh", "-c", cmdbuf, (char *)NULL);
15192712Snn35248 	}
15202712Snn35248 
15212712Snn35248 	if (child == -1)
15222712Snn35248 		return (-1);
15232712Snn35248 
15242712Snn35248 	saveint = sigset(SIGINT, SIG_IGN);
15252712Snn35248 	saveterm = sigset(SIGTERM, SIG_IGN);
15262712Snn35248 	savequit = sigset(SIGQUIT, SIG_IGN);
15272712Snn35248 	savehup = sigset(SIGHUP, SIG_IGN);
15282712Snn35248 
15292712Snn35248 	while ((pid = waitpid(child, &status, 0)) != child && pid != -1)
15302712Snn35248 		;
15312712Snn35248 
15322712Snn35248 	(void) sigset(SIGINT, saveint);
15332712Snn35248 	(void) sigset(SIGTERM, saveterm);
15342712Snn35248 	(void) sigset(SIGQUIT, savequit);
15352712Snn35248 	(void) sigset(SIGHUP, savehup);
15362712Snn35248 
15372712Snn35248 	return (pid == -1 ? -1 : status);
15382712Snn35248 }
15392712Snn35248 
15407089Sgjelinek int
15412712Snn35248 subproc_status(const char *cmd, int status, boolean_t verbose_failure)
15420Sstevel@tonic-gate {
15430Sstevel@tonic-gate 	if (WIFEXITED(status)) {
15440Sstevel@tonic-gate 		int exit_code = WEXITSTATUS(status);
15450Sstevel@tonic-gate 
15462712Snn35248 		if ((verbose_failure) && (exit_code != ZONE_SUBPROC_OK))
15472712Snn35248 			zerror(gettext("'%s' failed with exit code %d."), cmd,
15482712Snn35248 			    exit_code);
15492712Snn35248 
15502712Snn35248 		return (exit_code);
15510Sstevel@tonic-gate 	} else if (WIFSIGNALED(status)) {
15520Sstevel@tonic-gate 		int signal = WTERMSIG(status);
15530Sstevel@tonic-gate 		char sigstr[SIG2STR_MAX];
15540Sstevel@tonic-gate 
15550Sstevel@tonic-gate 		if (sig2str(signal, sigstr) == 0) {
15560Sstevel@tonic-gate 			zerror(gettext("'%s' terminated by signal SIG%s."), cmd,
15570Sstevel@tonic-gate 			    sigstr);
15580Sstevel@tonic-gate 		} else {
15590Sstevel@tonic-gate 			zerror(gettext("'%s' terminated by an unknown signal."),
15600Sstevel@tonic-gate 			    cmd);
15610Sstevel@tonic-gate 		}
15620Sstevel@tonic-gate 	} else {
15630Sstevel@tonic-gate 		zerror(gettext("'%s' failed for unknown reasons."), cmd);
15640Sstevel@tonic-gate 	}
15652712Snn35248 
15662712Snn35248 	/*
15672712Snn35248 	 * Assume a subprocess that died due to a signal or an unknown error
15682712Snn35248 	 * should be considered an exit code of ZONE_SUBPROC_FATAL, as the
15692712Snn35248 	 * user will likely need to do some manual cleanup.
15702712Snn35248 	 */
15712712Snn35248 	return (ZONE_SUBPROC_FATAL);
15720Sstevel@tonic-gate }
15730Sstevel@tonic-gate 
15740Sstevel@tonic-gate /*
15750Sstevel@tonic-gate  * Various sanity checks; make sure:
15760Sstevel@tonic-gate  * 1. We're in the global zone.
15770Sstevel@tonic-gate  * 2. The calling user has sufficient privilege.
15780Sstevel@tonic-gate  * 3. The target zone is neither the global zone nor anything starting with
15790Sstevel@tonic-gate  *    "SUNW".
15800Sstevel@tonic-gate  * 4a. If we're looking for a 'not running' (i.e., configured or installed)
15810Sstevel@tonic-gate  *     zone, the name service knows about it.
15820Sstevel@tonic-gate  * 4b. For some operations which expect a zone not to be running, that it is
15830Sstevel@tonic-gate  *     not already running (or ready).
15840Sstevel@tonic-gate  */
15850Sstevel@tonic-gate static int
15860Sstevel@tonic-gate sanity_check(char *zone, int cmd_num, boolean_t running,
15872712Snn35248     boolean_t unsafe_when_running, boolean_t force)
15880Sstevel@tonic-gate {
15890Sstevel@tonic-gate 	zone_entry_t *zent;
15900Sstevel@tonic-gate 	priv_set_t *privset;
15912712Snn35248 	zone_state_t state, min_state;
1592766Scarlsonj 	char kernzone[ZONENAME_MAX];
1593766Scarlsonj 	FILE *fp;
15940Sstevel@tonic-gate 
15950Sstevel@tonic-gate 	if (getzoneid() != GLOBAL_ZONEID) {
15961645Scomay 		switch (cmd_num) {
15971645Scomay 		case CMD_HALT:
15981645Scomay 			zerror(gettext("use %s to %s this zone."), "halt(1M)",
15991645Scomay 			    cmd_to_str(cmd_num));
16001645Scomay 			break;
16011645Scomay 		case CMD_REBOOT:
16021645Scomay 			zerror(gettext("use %s to %s this zone."),
16031645Scomay 			    "reboot(1M)", cmd_to_str(cmd_num));
16041645Scomay 			break;
16051645Scomay 		default:
16061645Scomay 			zerror(gettext("must be in the global zone to %s a "
16071645Scomay 			    "zone."), cmd_to_str(cmd_num));
16081645Scomay 			break;
16091645Scomay 		}
16100Sstevel@tonic-gate 		return (Z_ERR);
16110Sstevel@tonic-gate 	}
16120Sstevel@tonic-gate 
16130Sstevel@tonic-gate 	if ((privset = priv_allocset()) == NULL) {
16140Sstevel@tonic-gate 		zerror(gettext("%s failed"), "priv_allocset");
16150Sstevel@tonic-gate 		return (Z_ERR);
16160Sstevel@tonic-gate 	}
16170Sstevel@tonic-gate 
16180Sstevel@tonic-gate 	if (getppriv(PRIV_EFFECTIVE, privset) != 0) {
16190Sstevel@tonic-gate 		zerror(gettext("%s failed"), "getppriv");
16200Sstevel@tonic-gate 		priv_freeset(privset);
16210Sstevel@tonic-gate 		return (Z_ERR);
16220Sstevel@tonic-gate 	}
16230Sstevel@tonic-gate 
16240Sstevel@tonic-gate 	if (priv_isfullset(privset) == B_FALSE) {
16250Sstevel@tonic-gate 		zerror(gettext("only a privileged user may %s a zone."),
16260Sstevel@tonic-gate 		    cmd_to_str(cmd_num));
16270Sstevel@tonic-gate 		priv_freeset(privset);
16280Sstevel@tonic-gate 		return (Z_ERR);
16290Sstevel@tonic-gate 	}
16300Sstevel@tonic-gate 	priv_freeset(privset);
16310Sstevel@tonic-gate 
16320Sstevel@tonic-gate 	if (zone == NULL) {
16330Sstevel@tonic-gate 		zerror(gettext("no zone specified"));
16340Sstevel@tonic-gate 		return (Z_ERR);
16350Sstevel@tonic-gate 	}
16360Sstevel@tonic-gate 
16370Sstevel@tonic-gate 	if (strcmp(zone, GLOBAL_ZONENAME) == 0) {
16380Sstevel@tonic-gate 		zerror(gettext("%s operation is invalid for the global zone."),
16390Sstevel@tonic-gate 		    cmd_to_str(cmd_num));
16400Sstevel@tonic-gate 		return (Z_ERR);
16410Sstevel@tonic-gate 	}
16420Sstevel@tonic-gate 
16430Sstevel@tonic-gate 	if (strncmp(zone, "SUNW", 4) == 0) {
16440Sstevel@tonic-gate 		zerror(gettext("%s operation is invalid for zones starting "
16450Sstevel@tonic-gate 		    "with SUNW."), cmd_to_str(cmd_num));
16460Sstevel@tonic-gate 		return (Z_ERR);
16470Sstevel@tonic-gate 	}
16480Sstevel@tonic-gate 
1649766Scarlsonj 	if (!zonecfg_in_alt_root()) {
1650766Scarlsonj 		zent = lookup_running_zone(zone);
1651766Scarlsonj 	} else if ((fp = zonecfg_open_scratch("", B_FALSE)) == NULL) {
1652766Scarlsonj 		zent = NULL;
1653766Scarlsonj 	} else {
1654766Scarlsonj 		if (zonecfg_find_scratch(fp, zone, zonecfg_get_root(),
1655766Scarlsonj 		    kernzone, sizeof (kernzone)) == 0)
1656766Scarlsonj 			zent = lookup_running_zone(kernzone);
1657766Scarlsonj 		else
1658766Scarlsonj 			zent = NULL;
1659766Scarlsonj 		zonecfg_close_scratch(fp);
1660766Scarlsonj 	}
1661766Scarlsonj 
16620Sstevel@tonic-gate 	/*
16630Sstevel@tonic-gate 	 * Look up from the kernel for 'running' zones.
16640Sstevel@tonic-gate 	 */
16652712Snn35248 	if (running && !force) {
16660Sstevel@tonic-gate 		if (zent == NULL) {
16670Sstevel@tonic-gate 			zerror(gettext("not running"));
16680Sstevel@tonic-gate 			return (Z_ERR);
16690Sstevel@tonic-gate 		}
16700Sstevel@tonic-gate 	} else {
16710Sstevel@tonic-gate 		int err;
16720Sstevel@tonic-gate 
16730Sstevel@tonic-gate 		if (unsafe_when_running && zent != NULL) {
16740Sstevel@tonic-gate 			/* check whether the zone is ready or running */
16750Sstevel@tonic-gate 			if ((err = zone_get_state(zent->zname,
16760Sstevel@tonic-gate 			    &zent->zstate_num)) != Z_OK) {
16770Sstevel@tonic-gate 				errno = err;
16780Sstevel@tonic-gate 				zperror2(zent->zname,
16790Sstevel@tonic-gate 				    gettext("could not get state"));
16800Sstevel@tonic-gate 				/* can't tell, so hedge */
16810Sstevel@tonic-gate 				zent->zstate_str = "ready/running";
16820Sstevel@tonic-gate 			} else {
16830Sstevel@tonic-gate 				zent->zstate_str =
16840Sstevel@tonic-gate 				    zone_state_str(zent->zstate_num);
16850Sstevel@tonic-gate 			}
16860Sstevel@tonic-gate 			zerror(gettext("%s operation is invalid for %s zones."),
16870Sstevel@tonic-gate 			    cmd_to_str(cmd_num), zent->zstate_str);
16880Sstevel@tonic-gate 			return (Z_ERR);
16890Sstevel@tonic-gate 		}
16900Sstevel@tonic-gate 		if ((err = zone_get_state(zone, &state)) != Z_OK) {
16910Sstevel@tonic-gate 			errno = err;
16920Sstevel@tonic-gate 			zperror2(zone, gettext("could not get state"));
16930Sstevel@tonic-gate 			return (Z_ERR);
16940Sstevel@tonic-gate 		}
16950Sstevel@tonic-gate 		switch (cmd_num) {
16960Sstevel@tonic-gate 		case CMD_UNINSTALL:
16970Sstevel@tonic-gate 			if (state == ZONE_STATE_CONFIGURED) {
16980Sstevel@tonic-gate 				zerror(gettext("is already in state '%s'."),
16990Sstevel@tonic-gate 				    zone_state_str(ZONE_STATE_CONFIGURED));
17000Sstevel@tonic-gate 				return (Z_ERR);
17010Sstevel@tonic-gate 			}
17020Sstevel@tonic-gate 			break;
17031507Sgjelinek 		case CMD_ATTACH:
17048759Sgerald.jelinek@sun.com 			if (state == ZONE_STATE_INSTALLED) {
17058759Sgerald.jelinek@sun.com 				zerror(gettext("is already %s."),
17068759Sgerald.jelinek@sun.com 				    zone_state_str(ZONE_STATE_INSTALLED));
17078759Sgerald.jelinek@sun.com 				return (Z_ERR);
17088759Sgerald.jelinek@sun.com 			} else if (state == ZONE_STATE_INCOMPLETE && !force) {
17098759Sgerald.jelinek@sun.com 				zerror(gettext("zone is %s; %s required."),
17108759Sgerald.jelinek@sun.com 				    zone_state_str(ZONE_STATE_INCOMPLETE),
17118759Sgerald.jelinek@sun.com 				    cmd_to_str(CMD_UNINSTALL));
17128759Sgerald.jelinek@sun.com 				return (Z_ERR);
17138759Sgerald.jelinek@sun.com 			}
17148759Sgerald.jelinek@sun.com 			break;
17151300Sgjelinek 		case CMD_CLONE:
17160Sstevel@tonic-gate 		case CMD_INSTALL:
17170Sstevel@tonic-gate 			if (state == ZONE_STATE_INSTALLED) {
17180Sstevel@tonic-gate 				zerror(gettext("is already %s."),
17190Sstevel@tonic-gate 				    zone_state_str(ZONE_STATE_INSTALLED));
17200Sstevel@tonic-gate 				return (Z_ERR);
17210Sstevel@tonic-gate 			} else if (state == ZONE_STATE_INCOMPLETE) {
17220Sstevel@tonic-gate 				zerror(gettext("zone is %s; %s required."),
17230Sstevel@tonic-gate 				    zone_state_str(ZONE_STATE_INCOMPLETE),
17240Sstevel@tonic-gate 				    cmd_to_str(CMD_UNINSTALL));
17250Sstevel@tonic-gate 				return (Z_ERR);
17260Sstevel@tonic-gate 			}
17270Sstevel@tonic-gate 			break;
17281507Sgjelinek 		case CMD_DETACH:
17291300Sgjelinek 		case CMD_MOVE:
17300Sstevel@tonic-gate 		case CMD_READY:
17310Sstevel@tonic-gate 		case CMD_BOOT:
1732766Scarlsonj 		case CMD_MOUNT:
17332303Scarlsonj 		case CMD_MARK:
17342712Snn35248 			if ((cmd_num == CMD_BOOT || cmd_num == CMD_MOUNT) &&
17352712Snn35248 			    force)
17362712Snn35248 				min_state = ZONE_STATE_INCOMPLETE;
17378759Sgerald.jelinek@sun.com 			else if (cmd_num == CMD_MARK)
17388759Sgerald.jelinek@sun.com 				min_state = ZONE_STATE_CONFIGURED;
17392712Snn35248 			else
17402712Snn35248 				min_state = ZONE_STATE_INSTALLED;
17412712Snn35248 
17422712Snn35248 			if (state < min_state) {
17430Sstevel@tonic-gate 				zerror(gettext("must be %s before %s."),
17442712Snn35248 				    zone_state_str(min_state),
17450Sstevel@tonic-gate 				    cmd_to_str(cmd_num));
17460Sstevel@tonic-gate 				return (Z_ERR);
17470Sstevel@tonic-gate 			}
17480Sstevel@tonic-gate 			break;
17490Sstevel@tonic-gate 		case CMD_VERIFY:
17500Sstevel@tonic-gate 			if (state == ZONE_STATE_INCOMPLETE) {
17510Sstevel@tonic-gate 				zerror(gettext("zone is %s; %s required."),
17520Sstevel@tonic-gate 				    zone_state_str(ZONE_STATE_INCOMPLETE),
17530Sstevel@tonic-gate 				    cmd_to_str(CMD_UNINSTALL));
17540Sstevel@tonic-gate 				return (Z_ERR);
17550Sstevel@tonic-gate 			}
17560Sstevel@tonic-gate 			break;
1757766Scarlsonj 		case CMD_UNMOUNT:
1758766Scarlsonj 			if (state != ZONE_STATE_MOUNTED) {
1759766Scarlsonj 				zerror(gettext("must be %s before %s."),
1760766Scarlsonj 				    zone_state_str(ZONE_STATE_MOUNTED),
1761766Scarlsonj 				    cmd_to_str(cmd_num));
1762766Scarlsonj 				return (Z_ERR);
1763766Scarlsonj 			}
1764766Scarlsonj 			break;
17650Sstevel@tonic-gate 		}
17660Sstevel@tonic-gate 	}
17670Sstevel@tonic-gate 	return (Z_OK);
17680Sstevel@tonic-gate }
17690Sstevel@tonic-gate 
17700Sstevel@tonic-gate static int
17710Sstevel@tonic-gate halt_func(int argc, char *argv[])
17720Sstevel@tonic-gate {
17730Sstevel@tonic-gate 	zone_cmd_arg_t zarg;
17740Sstevel@tonic-gate 	int arg;
17750Sstevel@tonic-gate 
1776766Scarlsonj 	if (zonecfg_in_alt_root()) {
1777766Scarlsonj 		zerror(gettext("cannot halt zone in alternate root"));
1778766Scarlsonj 		return (Z_ERR);
1779766Scarlsonj 	}
1780766Scarlsonj 
17810Sstevel@tonic-gate 	optind = 0;
17820Sstevel@tonic-gate 	if ((arg = getopt(argc, argv, "?")) != EOF) {
17830Sstevel@tonic-gate 		switch (arg) {
17840Sstevel@tonic-gate 		case '?':
17850Sstevel@tonic-gate 			sub_usage(SHELP_HALT, CMD_HALT);
17860Sstevel@tonic-gate 			return (optopt == '?' ? Z_OK : Z_USAGE);
17870Sstevel@tonic-gate 		default:
17880Sstevel@tonic-gate 			sub_usage(SHELP_HALT, CMD_HALT);
17890Sstevel@tonic-gate 			return (Z_USAGE);
17900Sstevel@tonic-gate 		}
17910Sstevel@tonic-gate 	}
17920Sstevel@tonic-gate 	if (argc > optind) {
17930Sstevel@tonic-gate 		sub_usage(SHELP_HALT, CMD_HALT);
17940Sstevel@tonic-gate 		return (Z_USAGE);
17950Sstevel@tonic-gate 	}
17960Sstevel@tonic-gate 	/*
17970Sstevel@tonic-gate 	 * zoneadmd should be the one to decide whether or not to proceed,
17980Sstevel@tonic-gate 	 * so even though it seems that the fourth parameter below should
17990Sstevel@tonic-gate 	 * perhaps be B_TRUE, it really shouldn't be.
18000Sstevel@tonic-gate 	 */
18012712Snn35248 	if (sanity_check(target_zone, CMD_HALT, B_FALSE, B_FALSE, B_FALSE)
18022712Snn35248 	    != Z_OK)
18030Sstevel@tonic-gate 		return (Z_ERR);
18040Sstevel@tonic-gate 
18053339Szt129084 	/*
18063339Szt129084 	 * Invoke brand-specific handler.
18073339Szt129084 	 */
18083339Szt129084 	if (invoke_brand_handler(CMD_HALT, argv) != Z_OK)
18093339Szt129084 		return (Z_ERR);
18103339Szt129084 
18110Sstevel@tonic-gate 	zarg.cmd = Z_HALT;
18127089Sgjelinek 	return ((zonecfg_call_zoneadmd(target_zone, &zarg, locale,
18137089Sgjelinek 	    B_TRUE) == 0) ?  Z_OK : Z_ERR);
18140Sstevel@tonic-gate }
18150Sstevel@tonic-gate 
18160Sstevel@tonic-gate static int
18170Sstevel@tonic-gate reboot_func(int argc, char *argv[])
18180Sstevel@tonic-gate {
18190Sstevel@tonic-gate 	zone_cmd_arg_t zarg;
18200Sstevel@tonic-gate 	int arg;
18210Sstevel@tonic-gate 
1822766Scarlsonj 	if (zonecfg_in_alt_root()) {
1823766Scarlsonj 		zerror(gettext("cannot reboot zone in alternate root"));
1824766Scarlsonj 		return (Z_ERR);
1825766Scarlsonj 	}
1826766Scarlsonj 
18270Sstevel@tonic-gate 	optind = 0;
18280Sstevel@tonic-gate 	if ((arg = getopt(argc, argv, "?")) != EOF) {
18290Sstevel@tonic-gate 		switch (arg) {
18300Sstevel@tonic-gate 		case '?':
18310Sstevel@tonic-gate 			sub_usage(SHELP_REBOOT, CMD_REBOOT);
18320Sstevel@tonic-gate 			return (optopt == '?' ? Z_OK : Z_USAGE);
18330Sstevel@tonic-gate 		default:
18340Sstevel@tonic-gate 			sub_usage(SHELP_REBOOT, CMD_REBOOT);
18350Sstevel@tonic-gate 			return (Z_USAGE);
18360Sstevel@tonic-gate 		}
18370Sstevel@tonic-gate 	}
18382267Sdp 
18392267Sdp 	zarg.bootbuf[0] = '\0';
18402267Sdp 	for (; optind < argc; optind++) {
18412267Sdp 		if (strlcat(zarg.bootbuf, argv[optind],
18422267Sdp 		    sizeof (zarg.bootbuf)) >= sizeof (zarg.bootbuf)) {
18432267Sdp 			zerror(gettext("Boot argument list too long"));
18442267Sdp 			return (Z_ERR);
18452267Sdp 		}
18462267Sdp 		if (optind < argc - 1)
18472267Sdp 			if (strlcat(zarg.bootbuf, " ", sizeof (zarg.bootbuf)) >=
18482267Sdp 			    sizeof (zarg.bootbuf)) {
18492267Sdp 				zerror(gettext("Boot argument list too long"));
18502267Sdp 				return (Z_ERR);
18512267Sdp 			}
18522267Sdp 	}
18532267Sdp 
18542267Sdp 
18550Sstevel@tonic-gate 	/*
18560Sstevel@tonic-gate 	 * zoneadmd should be the one to decide whether or not to proceed,
18570Sstevel@tonic-gate 	 * so even though it seems that the fourth parameter below should
18580Sstevel@tonic-gate 	 * perhaps be B_TRUE, it really shouldn't be.
18590Sstevel@tonic-gate 	 */
18602712Snn35248 	if (sanity_check(target_zone, CMD_REBOOT, B_TRUE, B_FALSE, B_FALSE)
18612712Snn35248 	    != Z_OK)
18620Sstevel@tonic-gate 		return (Z_ERR);
18633339Szt129084 	if (verify_details(CMD_REBOOT, argv) != Z_OK)
1864823Sgjelinek 		return (Z_ERR);
18650Sstevel@tonic-gate 
18660Sstevel@tonic-gate 	zarg.cmd = Z_REBOOT;
18677089Sgjelinek 	return ((zonecfg_call_zoneadmd(target_zone, &zarg, locale, B_TRUE) == 0)
18687089Sgjelinek 	    ? Z_OK : Z_ERR);
18697089Sgjelinek }
18707089Sgjelinek 
18717089Sgjelinek static int
18727089Sgjelinek get_hook(brand_handle_t bh, char *cmd, size_t len, int (*bp)(brand_handle_t,
18737089Sgjelinek     const char *, const char *, char *, size_t), char *zonename, char *zonepath)
18747089Sgjelinek {
18757089Sgjelinek 	if (strlcpy(cmd, EXEC_PREFIX, len) >= len)
18767089Sgjelinek 		return (Z_ERR);
18777089Sgjelinek 
18787089Sgjelinek 	if (bp(bh, zonename, zonepath, cmd + EXEC_LEN, len - EXEC_LEN) != 0)
18797089Sgjelinek 		return (Z_ERR);
18807089Sgjelinek 
18817089Sgjelinek 	if (strlen(cmd) <= EXEC_LEN)
18827089Sgjelinek 		cmd[0] = '\0';
18837089Sgjelinek 
18847089Sgjelinek 	return (Z_OK);
18850Sstevel@tonic-gate }
18860Sstevel@tonic-gate 
18870Sstevel@tonic-gate static int
18883339Szt129084 verify_brand(zone_dochandle_t handle, int cmd_num, char *argv[])
18892712Snn35248 {
18902712Snn35248 	char cmdbuf[MAXPATHLEN];
18912712Snn35248 	int err;
18922712Snn35248 	char zonepath[MAXPATHLEN];
18932727Sedp 	brand_handle_t bh = NULL;
18943339Szt129084 	int status, i;
18952712Snn35248 
18962712Snn35248 	/*
18972712Snn35248 	 * Fetch the verify command from the brand configuration.
18982712Snn35248 	 * "exec" the command so that the returned status is that of
18992712Snn35248 	 * the command and not the shell.
19002712Snn35248 	 */
19017089Sgjelinek 	if (handle == NULL) {
19027089Sgjelinek 		(void) strlcpy(zonepath, "-", sizeof (zonepath));
19037089Sgjelinek 	} else if ((err = zonecfg_get_zonepath(handle, zonepath,
19047089Sgjelinek 	    sizeof (zonepath))) != Z_OK) {
19052712Snn35248 		errno = err;
19063339Szt129084 		zperror(cmd_to_str(cmd_num), B_TRUE);
19072712Snn35248 		return (Z_ERR);
19082712Snn35248 	}
19092727Sedp 	if ((bh = brand_open(target_brand)) == NULL) {
19102712Snn35248 		zerror(gettext("missing or invalid brand"));
19112712Snn35248 		return (Z_ERR);
19122712Snn35248 	}
19132712Snn35248 
19142712Snn35248 	/*
19152712Snn35248 	 * If the brand has its own verification routine, execute it now.
19163339Szt129084 	 * The verification routine validates the intended zoneadm
19173339Szt129084 	 * operation for the specific brand. The zoneadm subcommand and
19183339Szt129084 	 * all its arguments are passed to the routine.
19192712Snn35248 	 */
19207089Sgjelinek 	err = get_hook(bh, cmdbuf, sizeof (cmdbuf), brand_get_verify_adm,
19217089Sgjelinek 	    target_zone, zonepath);
19222727Sedp 	brand_close(bh);
19237089Sgjelinek 	if (err != Z_OK)
19243339Szt129084 		return (Z_BRAND_ERROR);
19257089Sgjelinek 	if (cmdbuf[0] == '\0')
19263339Szt129084 		return (Z_OK);
19273339Szt129084 
19283339Szt129084 	if (strlcat(cmdbuf, cmd_to_str(cmd_num),
19293339Szt129084 	    sizeof (cmdbuf)) >= sizeof (cmdbuf))
19303339Szt129084 		return (Z_ERR);
19313339Szt129084 
19323339Szt129084 	/* Build the argv string */
19333339Szt129084 	i = 0;
19343339Szt129084 	while (argv[i] != NULL) {
19353339Szt129084 		if ((strlcat(cmdbuf, " ",
19363339Szt129084 		    sizeof (cmdbuf)) >= sizeof (cmdbuf)) ||
19373339Szt129084 		    (strlcat(cmdbuf, argv[i++],
19383339Szt129084 		    sizeof (cmdbuf)) >= sizeof (cmdbuf)))
19393339Szt129084 			return (Z_ERR);
19403339Szt129084 	}
19413339Szt129084 
19423686Sgjelinek 	if (zoneadm_is_nested)
19433686Sgjelinek 		status = do_subproc(cmdbuf);
19443686Sgjelinek 	else
19453686Sgjelinek 		status = do_subproc_interactive(cmdbuf);
19463339Szt129084 	err = subproc_status(gettext("brand-specific verification"),
19473339Szt129084 	    status, B_FALSE);
19483339Szt129084 
19493339Szt129084 	return ((err == ZONE_SUBPROC_OK) ? Z_OK : Z_BRAND_ERROR);
19502712Snn35248 }
19512712Snn35248 
19522712Snn35248 static int
19530Sstevel@tonic-gate verify_rctls(zone_dochandle_t handle)
19540Sstevel@tonic-gate {
19550Sstevel@tonic-gate 	struct zone_rctltab rctltab;
19560Sstevel@tonic-gate 	size_t rbs = rctlblk_size();
19570Sstevel@tonic-gate 	rctlblk_t *rctlblk;
19580Sstevel@tonic-gate 	int error = Z_INVAL;
19590Sstevel@tonic-gate 
19600Sstevel@tonic-gate 	if ((rctlblk = malloc(rbs)) == NULL) {
19610Sstevel@tonic-gate 		zerror(gettext("failed to allocate %lu bytes: %s"), rbs,
19620Sstevel@tonic-gate 		    strerror(errno));
19630Sstevel@tonic-gate 		return (Z_NOMEM);
19640Sstevel@tonic-gate 	}
19650Sstevel@tonic-gate 
19660Sstevel@tonic-gate 	if (zonecfg_setrctlent(handle) != Z_OK) {
19670Sstevel@tonic-gate 		zerror(gettext("zonecfg_setrctlent failed"));
19680Sstevel@tonic-gate 		free(rctlblk);
19690Sstevel@tonic-gate 		return (error);
19700Sstevel@tonic-gate 	}
19710Sstevel@tonic-gate 
19720Sstevel@tonic-gate 	rctltab.zone_rctl_valptr = NULL;
19730Sstevel@tonic-gate 	while (zonecfg_getrctlent(handle, &rctltab) == Z_OK) {
19740Sstevel@tonic-gate 		struct zone_rctlvaltab *rctlval;
19750Sstevel@tonic-gate 		const char *name = rctltab.zone_rctl_name;
19760Sstevel@tonic-gate 
19770Sstevel@tonic-gate 		if (!zonecfg_is_rctl(name)) {
19780Sstevel@tonic-gate 			zerror(gettext("WARNING: Ignoring unrecognized rctl "
19790Sstevel@tonic-gate 			    "'%s'."),  name);
19800Sstevel@tonic-gate 			zonecfg_free_rctl_value_list(rctltab.zone_rctl_valptr);
19810Sstevel@tonic-gate 			rctltab.zone_rctl_valptr = NULL;
19820Sstevel@tonic-gate 			continue;
19830Sstevel@tonic-gate 		}
19840Sstevel@tonic-gate 
19850Sstevel@tonic-gate 		for (rctlval = rctltab.zone_rctl_valptr; rctlval != NULL;
19860Sstevel@tonic-gate 		    rctlval = rctlval->zone_rctlval_next) {
19870Sstevel@tonic-gate 			if (zonecfg_construct_rctlblk(rctlval, rctlblk)
19880Sstevel@tonic-gate 			    != Z_OK) {
19890Sstevel@tonic-gate 				zerror(gettext("invalid rctl value: "
19900Sstevel@tonic-gate 				    "(priv=%s,limit=%s,action%s)"),
19910Sstevel@tonic-gate 				    rctlval->zone_rctlval_priv,
19920Sstevel@tonic-gate 				    rctlval->zone_rctlval_limit,
19930Sstevel@tonic-gate 				    rctlval->zone_rctlval_action);
19940Sstevel@tonic-gate 				goto out;
19950Sstevel@tonic-gate 			}
19960Sstevel@tonic-gate 			if (!zonecfg_valid_rctl(name, rctlblk)) {
19970Sstevel@tonic-gate 				zerror(gettext("(priv=%s,limit=%s,action=%s) "
19980Sstevel@tonic-gate 				    "is not a valid value for rctl '%s'"),
19990Sstevel@tonic-gate 				    rctlval->zone_rctlval_priv,
20000Sstevel@tonic-gate 				    rctlval->zone_rctlval_limit,
20010Sstevel@tonic-gate 				    rctlval->zone_rctlval_action,
20020Sstevel@tonic-gate 				    name);
20030Sstevel@tonic-gate 				goto out;
20040Sstevel@tonic-gate 			}
20050Sstevel@tonic-gate 		}
20060Sstevel@tonic-gate 		zonecfg_free_rctl_value_list(rctltab.zone_rctl_valptr);
20070Sstevel@tonic-gate 	}
20080Sstevel@tonic-gate 	rctltab.zone_rctl_valptr = NULL;
20090Sstevel@tonic-gate 	error = Z_OK;
20100Sstevel@tonic-gate out:
20110Sstevel@tonic-gate 	zonecfg_free_rctl_value_list(rctltab.zone_rctl_valptr);
20120Sstevel@tonic-gate 	(void) zonecfg_endrctlent(handle);
20130Sstevel@tonic-gate 	free(rctlblk);
20140Sstevel@tonic-gate 	return (error);
20150Sstevel@tonic-gate }
20160Sstevel@tonic-gate 
20170Sstevel@tonic-gate static int
20180Sstevel@tonic-gate verify_pool(zone_dochandle_t handle)
20190Sstevel@tonic-gate {
20200Sstevel@tonic-gate 	char poolname[MAXPATHLEN];
20210Sstevel@tonic-gate 	pool_conf_t *poolconf;
20220Sstevel@tonic-gate 	pool_t *pool;
20230Sstevel@tonic-gate 	int status;
20240Sstevel@tonic-gate 	int error;
20250Sstevel@tonic-gate 
20260Sstevel@tonic-gate 	/*
20270Sstevel@tonic-gate 	 * This ends up being very similar to the check done in zoneadmd.
20280Sstevel@tonic-gate 	 */
20290Sstevel@tonic-gate 	error = zonecfg_get_pool(handle, poolname, sizeof (poolname));
20300Sstevel@tonic-gate 	if (error == Z_NO_ENTRY || (error == Z_OK && strlen(poolname) == 0)) {
20310Sstevel@tonic-gate 		/*
20320Sstevel@tonic-gate 		 * No pool specified.
20330Sstevel@tonic-gate 		 */
20340Sstevel@tonic-gate 		return (0);
20350Sstevel@tonic-gate 	}
20360Sstevel@tonic-gate 	if (error != Z_OK) {
20370Sstevel@tonic-gate 		zperror(gettext("Unable to retrieve pool name from "
20380Sstevel@tonic-gate 		    "configuration"), B_TRUE);
20390Sstevel@tonic-gate 		return (error);
20400Sstevel@tonic-gate 	}
20410Sstevel@tonic-gate 	/*
20420Sstevel@tonic-gate 	 * Don't do anything if pools aren't enabled.
20430Sstevel@tonic-gate 	 */
20440Sstevel@tonic-gate 	if (pool_get_status(&status) != PO_SUCCESS || status != POOL_ENABLED) {
20450Sstevel@tonic-gate 		zerror(gettext("WARNING: pools facility not active; "
20460Sstevel@tonic-gate 		    "zone will not be bound to pool '%s'."), poolname);
20470Sstevel@tonic-gate 		return (Z_OK);
20480Sstevel@tonic-gate 	}
20490Sstevel@tonic-gate 	/*
20500Sstevel@tonic-gate 	 * Try to provide a sane error message if the requested pool doesn't
20510Sstevel@tonic-gate 	 * exist.  It isn't clear that pools-related failures should
20520Sstevel@tonic-gate 	 * necessarily translate to a failure to verify the zone configuration,
20530Sstevel@tonic-gate 	 * hence they are not considered errors.
20540Sstevel@tonic-gate 	 */
20550Sstevel@tonic-gate 	if ((poolconf = pool_conf_alloc()) == NULL) {
20560Sstevel@tonic-gate 		zerror(gettext("WARNING: pool_conf_alloc failed; "
20570Sstevel@tonic-gate 		    "using default pool"));
20580Sstevel@tonic-gate 		return (Z_OK);
20590Sstevel@tonic-gate 	}
20600Sstevel@tonic-gate 	if (pool_conf_open(poolconf, pool_dynamic_location(), PO_RDONLY) !=
20610Sstevel@tonic-gate 	    PO_SUCCESS) {
20620Sstevel@tonic-gate 		zerror(gettext("WARNING: pool_conf_open failed; "
20630Sstevel@tonic-gate 		    "using default pool"));
20640Sstevel@tonic-gate 		pool_conf_free(poolconf);
20650Sstevel@tonic-gate 		return (Z_OK);
20660Sstevel@tonic-gate 	}
20670Sstevel@tonic-gate 	pool = pool_get_pool(poolconf, poolname);
20680Sstevel@tonic-gate 	(void) pool_conf_close(poolconf);
20690Sstevel@tonic-gate 	pool_conf_free(poolconf);
20700Sstevel@tonic-gate 	if (pool == NULL) {
20710Sstevel@tonic-gate 		zerror(gettext("WARNING: pool '%s' not found. "
20720Sstevel@tonic-gate 		    "using default pool"), poolname);
20730Sstevel@tonic-gate 	}
20740Sstevel@tonic-gate 
20750Sstevel@tonic-gate 	return (Z_OK);
20760Sstevel@tonic-gate }
20770Sstevel@tonic-gate 
20780Sstevel@tonic-gate static int
2079823Sgjelinek verify_ipd(zone_dochandle_t handle)
2080823Sgjelinek {
2081823Sgjelinek 	int return_code = Z_OK;
2082823Sgjelinek 	struct zone_fstab fstab;
2083823Sgjelinek 	struct stat st;
2084823Sgjelinek 	char specdir[MAXPATHLEN];
2085823Sgjelinek 
2086823Sgjelinek 	if (zonecfg_setipdent(handle) != Z_OK) {
2087924Sgjelinek 		/*
2088924Sgjelinek 		 * TRANSLATION_NOTE
2089924Sgjelinek 		 * inherit-pkg-dirs is a literal that should not be translated.
2090924Sgjelinek 		 */
2091924Sgjelinek 		(void) fprintf(stderr, gettext("could not verify "
2092823Sgjelinek 		    "inherit-pkg-dirs: unable to enumerate mounts\n"));
2093823Sgjelinek 		return (Z_ERR);
2094823Sgjelinek 	}
2095823Sgjelinek 	while (zonecfg_getipdent(handle, &fstab) == Z_OK) {
2096823Sgjelinek 		/*
2097823Sgjelinek 		 * Verify fs_dir exists.
2098823Sgjelinek 		 */
2099823Sgjelinek 		(void) snprintf(specdir, sizeof (specdir), "%s%s",
2100823Sgjelinek 		    zonecfg_get_root(), fstab.zone_fs_dir);
2101823Sgjelinek 		if (stat(specdir, &st) != 0) {
2102924Sgjelinek 			/*
2103924Sgjelinek 			 * TRANSLATION_NOTE
2104924Sgjelinek 			 * inherit-pkg-dir is a literal that should not be
2105924Sgjelinek 			 * translated.
2106924Sgjelinek 			 */
2107924Sgjelinek 			(void) fprintf(stderr, gettext("could not verify "
2108823Sgjelinek 			    "inherit-pkg-dir %s: %s\n"),
2109823Sgjelinek 			    fstab.zone_fs_dir, strerror(errno));
2110823Sgjelinek 			return_code = Z_ERR;
2111823Sgjelinek 		}
2112823Sgjelinek 		if (strcmp(st.st_fstype, MNTTYPE_NFS) == 0) {
2113924Sgjelinek 			/*
2114924Sgjelinek 			 * TRANSLATION_NOTE
2115924Sgjelinek 			 * inherit-pkg-dir and NFS are literals that should
2116924Sgjelinek 			 * not be translated.
2117924Sgjelinek 			 */
2118823Sgjelinek 			(void) fprintf(stderr, gettext("cannot verify "
21191867Sgjelinek 			    "inherit-pkg-dir %s: NFS mounted file system.\n"
21201867Sgjelinek 			    "\tA local file system must be used.\n"),
2121823Sgjelinek 			    fstab.zone_fs_dir);
2122823Sgjelinek 			return_code = Z_ERR;
2123823Sgjelinek 		}
2124823Sgjelinek 	}
2125823Sgjelinek 	(void) zonecfg_endipdent(handle);
2126823Sgjelinek 
2127823Sgjelinek 	return (return_code);
2128823Sgjelinek }
2129823Sgjelinek 
21301393Slling /*
21311867Sgjelinek  * Verify that the special device/file system exists and is valid.
21321393Slling  */
21331393Slling static int
21341393Slling verify_fs_special(struct zone_fstab *fstab)
21351393Slling {
21366734Sjohnlev 	struct stat64 st;
21371393Slling 
21382971Sgjelinek 	/*
21392971Sgjelinek 	 * This validation is really intended for standard zone administration.
21402971Sgjelinek 	 * If we are in a mini-root or some other upgrade situation where
21412971Sgjelinek 	 * we are using the scratch zone, just by-pass this.
21422971Sgjelinek 	 */
21432971Sgjelinek 	if (zonecfg_in_alt_root())
21442971Sgjelinek 		return (Z_OK);
21452971Sgjelinek 
21461393Slling 	if (strcmp(fstab->zone_fs_type, MNTTYPE_ZFS) == 0)
21471393Slling 		return (verify_fs_zfs(fstab));
21481393Slling 
21496734Sjohnlev 	if (stat64(fstab->zone_fs_special, &st) != 0) {
21501397Slling 		(void) fprintf(stderr, gettext("could not verify fs "
21511393Slling 		    "%s: could not access %s: %s\n"), fstab->zone_fs_dir,
21521393Slling 		    fstab->zone_fs_special, strerror(errno));
21531393Slling 		return (Z_ERR);
21541393Slling 	}
21551393Slling 
21561393Slling 	if (strcmp(st.st_fstype, MNTTYPE_NFS) == 0) {
21571393Slling 		/*
21581393Slling 		 * TRANSLATION_NOTE
21591393Slling 		 * fs and NFS are literals that should
21601393Slling 		 * not be translated.
21611393Slling 		 */
21621393Slling 		(void) fprintf(stderr, gettext("cannot verify "
21631867Sgjelinek 		    "fs %s: NFS mounted file system.\n"
21641867Sgjelinek 		    "\tA local file system must be used.\n"),
21651393Slling 		    fstab->zone_fs_special);
21661393Slling 		return (Z_ERR);
21671393Slling 	}
21681393Slling 
21691393Slling 	return (Z_OK);
21701393Slling }
21711393Slling 
2172823Sgjelinek static int
21736734Sjohnlev isregfile(const char *path)
21746734Sjohnlev {
21756734Sjohnlev 	struct stat64 st;
21766734Sjohnlev 
21776734Sjohnlev 	if (stat64(path, &st) == -1)
21786734Sjohnlev 		return (-1);
21796734Sjohnlev 
21806734Sjohnlev 	return (S_ISREG(st.st_mode));
21816734Sjohnlev }
21826734Sjohnlev 
21836734Sjohnlev static int
21840Sstevel@tonic-gate verify_filesystems(zone_dochandle_t handle)
21850Sstevel@tonic-gate {
21860Sstevel@tonic-gate 	int return_code = Z_OK;
21870Sstevel@tonic-gate 	struct zone_fstab fstab;
21880Sstevel@tonic-gate 	char cmdbuf[MAXPATHLEN];
21890Sstevel@tonic-gate 	struct stat st;
21900Sstevel@tonic-gate 
21910Sstevel@tonic-gate 	/*
21920Sstevel@tonic-gate 	 * No need to verify inherit-pkg-dir fs types, as their type is
21930Sstevel@tonic-gate 	 * implicitly lofs, which is known.  Therefore, the types are only
21941867Sgjelinek 	 * verified for regular file systems below.
21950Sstevel@tonic-gate 	 *
21960Sstevel@tonic-gate 	 * Since the actual mount point is not known until the dependent mounts
21970Sstevel@tonic-gate 	 * are performed, we don't attempt any path validation here: that will
21980Sstevel@tonic-gate 	 * happen later when zoneadmd actually does the mounts.
21990Sstevel@tonic-gate 	 */
22000Sstevel@tonic-gate 	if (zonecfg_setfsent(handle) != Z_OK) {
22011867Sgjelinek 		(void) fprintf(stderr, gettext("could not verify file systems: "
22020Sstevel@tonic-gate 		    "unable to enumerate mounts\n"));
22030Sstevel@tonic-gate 		return (Z_ERR);
22040Sstevel@tonic-gate 	}
22050Sstevel@tonic-gate 	while (zonecfg_getfsent(handle, &fstab) == Z_OK) {
22060Sstevel@tonic-gate 		if (!zonecfg_valid_fs_type(fstab.zone_fs_type)) {
22070Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("cannot verify fs %s: "
22080Sstevel@tonic-gate 			    "type %s is not allowed.\n"), fstab.zone_fs_dir,
22090Sstevel@tonic-gate 			    fstab.zone_fs_type);
22100Sstevel@tonic-gate 			return_code = Z_ERR;
22110Sstevel@tonic-gate 			goto next_fs;
22120Sstevel@tonic-gate 		}
22130Sstevel@tonic-gate 		/*
22140Sstevel@tonic-gate 		 * Verify /usr/lib/fs/<fstype>/mount exists.
22150Sstevel@tonic-gate 		 */
22160Sstevel@tonic-gate 		if (snprintf(cmdbuf, sizeof (cmdbuf), "/usr/lib/fs/%s/mount",
22170Sstevel@tonic-gate 		    fstab.zone_fs_type) > sizeof (cmdbuf)) {
22180Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("cannot verify fs %s: "
22190Sstevel@tonic-gate 			    "type %s is too long.\n"), fstab.zone_fs_dir,
22200Sstevel@tonic-gate 			    fstab.zone_fs_type);
22210Sstevel@tonic-gate 			return_code = Z_ERR;
22220Sstevel@tonic-gate 			goto next_fs;
22230Sstevel@tonic-gate 		}
22240Sstevel@tonic-gate 		if (stat(cmdbuf, &st) != 0) {
2225924Sgjelinek 			(void) fprintf(stderr, gettext("could not verify fs "
2226924Sgjelinek 			    "%s: could not access %s: %s\n"), fstab.zone_fs_dir,
22270Sstevel@tonic-gate 			    cmdbuf, strerror(errno));
22280Sstevel@tonic-gate 			return_code = Z_ERR;
22290Sstevel@tonic-gate 			goto next_fs;
22300Sstevel@tonic-gate 		}
22310Sstevel@tonic-gate 		if (!S_ISREG(st.st_mode)) {
2232924Sgjelinek 			(void) fprintf(stderr, gettext("could not verify fs "
2233924Sgjelinek 			    "%s: %s is not a regular file\n"),
2234924Sgjelinek 			    fstab.zone_fs_dir, cmdbuf);
22350Sstevel@tonic-gate 			return_code = Z_ERR;
22360Sstevel@tonic-gate 			goto next_fs;
22370Sstevel@tonic-gate 		}
22380Sstevel@tonic-gate 		/*
22396734Sjohnlev 		 * If zone_fs_raw is set, verify that there's an fsck
22406734Sjohnlev 		 * binary for it.  If zone_fs_raw is not set, and it's
22416734Sjohnlev 		 * not a regular file (lofi mount), and there's an fsck
22426734Sjohnlev 		 * binary for it, complain.
22430Sstevel@tonic-gate 		 */
22440Sstevel@tonic-gate 		if (snprintf(cmdbuf, sizeof (cmdbuf), "/usr/lib/fs/%s/fsck",
22450Sstevel@tonic-gate 		    fstab.zone_fs_type) > sizeof (cmdbuf)) {
22460Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("cannot verify fs %s: "
22470Sstevel@tonic-gate 			    "type %s is too long.\n"), fstab.zone_fs_dir,
22480Sstevel@tonic-gate 			    fstab.zone_fs_type);
22490Sstevel@tonic-gate 			return_code = Z_ERR;
22500Sstevel@tonic-gate 			goto next_fs;
22510Sstevel@tonic-gate 		}
22520Sstevel@tonic-gate 		if (fstab.zone_fs_raw[0] != '\0' &&
22530Sstevel@tonic-gate 		    (stat(cmdbuf, &st) != 0 || !S_ISREG(st.st_mode))) {
22540Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("cannot verify fs %s: "
22550Sstevel@tonic-gate 			    "'raw' device specified but "
22560Sstevel@tonic-gate 			    "no fsck executable exists for %s\n"),
22570Sstevel@tonic-gate 			    fstab.zone_fs_dir, fstab.zone_fs_type);
22580Sstevel@tonic-gate 			return_code = Z_ERR;
22590Sstevel@tonic-gate 			goto next_fs;
22606734Sjohnlev 		} else if (fstab.zone_fs_raw[0] == '\0' &&
22616734Sjohnlev 		    stat(cmdbuf, &st) == 0 &&
22626734Sjohnlev 		    isregfile(fstab.zone_fs_special) != 1) {
22636734Sjohnlev 			(void) fprintf(stderr, gettext("could not verify fs "
22646734Sjohnlev 			    "%s: must specify 'raw' device for %s "
22656734Sjohnlev 			    "file systems\n"),
22666734Sjohnlev 			    fstab.zone_fs_dir, fstab.zone_fs_type);
22676734Sjohnlev 			return_code = Z_ERR;
22686734Sjohnlev 			goto next_fs;
22690Sstevel@tonic-gate 		}
22701393Slling 
22711393Slling 		/* Verify fs_special. */
22721393Slling 		if ((return_code = verify_fs_special(&fstab)) != Z_OK)
2273823Sgjelinek 			goto next_fs;
22741393Slling 
22751393Slling 		/* Verify fs_raw. */
2276823Sgjelinek 		if (fstab.zone_fs_raw[0] != '\0' &&
2277823Sgjelinek 		    stat(fstab.zone_fs_raw, &st) != 0) {
2278924Sgjelinek 			/*
2279924Sgjelinek 			 * TRANSLATION_NOTE
2280924Sgjelinek 			 * fs is a literal that should not be translated.
2281924Sgjelinek 			 */
2282924Sgjelinek 			(void) fprintf(stderr, gettext("could not verify fs "
2283924Sgjelinek 			    "%s: could not access %s: %s\n"), fstab.zone_fs_dir,
2284823Sgjelinek 			    fstab.zone_fs_raw, strerror(errno));
2285823Sgjelinek 			return_code = Z_ERR;
2286823Sgjelinek 			goto next_fs;
2287823Sgjelinek 		}
22880Sstevel@tonic-gate next_fs:
22890Sstevel@tonic-gate 		zonecfg_free_fs_option_list(fstab.zone_fs_options);
22900Sstevel@tonic-gate 	}
22910Sstevel@tonic-gate 	(void) zonecfg_endfsent(handle);
22920Sstevel@tonic-gate 
22930Sstevel@tonic-gate 	return (return_code);
22940Sstevel@tonic-gate }
22950Sstevel@tonic-gate 
22960Sstevel@tonic-gate static int
22971645Scomay verify_limitpriv(zone_dochandle_t handle)
22981645Scomay {
22991645Scomay 	char *privname = NULL;
23001645Scomay 	int err;
23011645Scomay 	priv_set_t *privs;
23021645Scomay 
23031645Scomay 	if ((privs = priv_allocset()) == NULL) {
23041645Scomay 		zperror(gettext("failed to allocate privilege set"), B_FALSE);
23051645Scomay 		return (Z_NOMEM);
23061645Scomay 	}
23071645Scomay 	err = zonecfg_get_privset(handle, privs, &privname);
23081645Scomay 	switch (err) {
23091645Scomay 	case Z_OK:
23101645Scomay 		break;
23111645Scomay 	case Z_PRIV_PROHIBITED:
23121645Scomay 		(void) fprintf(stderr, gettext("privilege \"%s\" is not "
23131645Scomay 		    "permitted within the zone's privilege set\n"), privname);
23141645Scomay 		break;
23151645Scomay 	case Z_PRIV_REQUIRED:
23161645Scomay 		(void) fprintf(stderr, gettext("required privilege \"%s\" is "
23171645Scomay 		    "missing from the zone's privilege set\n"), privname);
23181645Scomay 		break;
23191645Scomay 	case Z_PRIV_UNKNOWN:
23201645Scomay 		(void) fprintf(stderr, gettext("unknown privilege \"%s\" "
23211645Scomay 		    "specified in the zone's privilege set\n"), privname);
23221645Scomay 		break;
23231645Scomay 	default:
23241645Scomay 		zperror(
23251645Scomay 		    gettext("failed to determine the zone's privilege set"),
23261645Scomay 		    B_TRUE);
23271645Scomay 		break;
23281645Scomay 	}
23291645Scomay 	free(privname);
23301645Scomay 	priv_freeset(privs);
23311645Scomay 	return (err);
23321645Scomay }
23331645Scomay 
23341915Sgjelinek static void
23351915Sgjelinek free_local_netifs(int if_cnt, struct net_if **if_list)
23361915Sgjelinek {
23371915Sgjelinek 	int		i;
23381915Sgjelinek 
23391915Sgjelinek 	for (i = 0; i < if_cnt; i++) {
23401915Sgjelinek 		free(if_list[i]->name);
23411915Sgjelinek 		free(if_list[i]);
23421915Sgjelinek 	}
23431915Sgjelinek 	free(if_list);
23441915Sgjelinek }
23451915Sgjelinek 
23461915Sgjelinek /*
23471915Sgjelinek  * Get a list of the network interfaces, along with their address families,
23481915Sgjelinek  * that are plumbed in the global zone.  See if_tcp(7p) for a description
23491915Sgjelinek  * of the ioctls used here.
23501915Sgjelinek  */
23511915Sgjelinek static int
23521915Sgjelinek get_local_netifs(int *if_cnt, struct net_if ***if_list)
23531915Sgjelinek {
23541915Sgjelinek 	int		s;
23551915Sgjelinek 	int		i;
23561915Sgjelinek 	int		res = Z_OK;
23571915Sgjelinek 	int		space_needed;
23581915Sgjelinek 	int		cnt = 0;
23591915Sgjelinek 	struct		lifnum if_num;
23601915Sgjelinek 	struct		lifconf if_conf;
23611915Sgjelinek 	struct		lifreq *if_reqp;
23621915Sgjelinek 	char		*if_buf;
23631915Sgjelinek 	struct net_if	**local_ifs = NULL;
23641915Sgjelinek 
23651915Sgjelinek 	*if_cnt = 0;
23661915Sgjelinek 	*if_list = NULL;
23671915Sgjelinek 
23681915Sgjelinek 	if ((s = socket(SOCKET_AF(AF_INET), SOCK_DGRAM, 0)) < 0)
23691915Sgjelinek 		return (Z_ERR);
23701915Sgjelinek 
23711915Sgjelinek 	/*
23721915Sgjelinek 	 * Come back here in the unlikely event that the number of interfaces
23731915Sgjelinek 	 * increases between the time we get the count and the time we do the
23741915Sgjelinek 	 * SIOCGLIFCONF ioctl.
23751915Sgjelinek 	 */
23761915Sgjelinek retry:
23771915Sgjelinek 	/* Get the number of interfaces. */
23781915Sgjelinek 	if_num.lifn_family = AF_UNSPEC;
23791915Sgjelinek 	if_num.lifn_flags = LIFC_NOXMIT;
23801915Sgjelinek 	if (ioctl(s, SIOCGLIFNUM, &if_num) < 0) {
23811915Sgjelinek 		(void) close(s);
23821915Sgjelinek 		return (Z_ERR);
23831915Sgjelinek 	}
23841915Sgjelinek 
23851915Sgjelinek 	/* Get the interface configuration list. */
23861915Sgjelinek 	space_needed = if_num.lifn_count * sizeof (struct lifreq);
23871915Sgjelinek 	if ((if_buf = malloc(space_needed)) == NULL) {
23881915Sgjelinek 		(void) close(s);
23891915Sgjelinek 		return (Z_ERR);
23901915Sgjelinek 	}
23911915Sgjelinek 	if_conf.lifc_family = AF_UNSPEC;
23921915Sgjelinek 	if_conf.lifc_flags = LIFC_NOXMIT;
23931915Sgjelinek 	if_conf.lifc_len = space_needed;
23941915Sgjelinek 	if_conf.lifc_buf = if_buf;
23951915Sgjelinek 	if (ioctl(s, SIOCGLIFCONF, &if_conf) < 0) {
23961915Sgjelinek 		free(if_buf);
23971915Sgjelinek 		/*
23981915Sgjelinek 		 * SIOCGLIFCONF returns EINVAL if the buffer we passed in is
23991915Sgjelinek 		 * too small.  In this case go back and get the new if cnt.
24001915Sgjelinek 		 */
24011915Sgjelinek 		if (errno == EINVAL)
24021915Sgjelinek 			goto retry;
24031915Sgjelinek 
24041915Sgjelinek 		(void) close(s);
24051915Sgjelinek 		return (Z_ERR);
24061915Sgjelinek 	}
24071915Sgjelinek 	(void) close(s);
24081915Sgjelinek 
24091915Sgjelinek 	/* Get the name and address family for each interface. */
24101915Sgjelinek 	if_reqp = if_conf.lifc_req;
24111915Sgjelinek 	for (i = 0; i < (if_conf.lifc_len / sizeof (struct lifreq)); i++) {
24121915Sgjelinek 		struct net_if	**p;
24131915Sgjelinek 		struct lifreq	req;
24141915Sgjelinek 
24151915Sgjelinek 		if (strcmp(LOOPBACK_IF, if_reqp->lifr_name) == 0) {
24161915Sgjelinek 			if_reqp++;
24171915Sgjelinek 			continue;
24181915Sgjelinek 		}
24191915Sgjelinek 
24201915Sgjelinek 		if ((s = socket(SOCKET_AF(if_reqp->lifr_addr.ss_family),
24211915Sgjelinek 		    SOCK_DGRAM, 0)) == -1) {
24221915Sgjelinek 			res = Z_ERR;
24231915Sgjelinek 			break;
24241915Sgjelinek 		}
24251915Sgjelinek 
24261915Sgjelinek 		(void) strncpy(req.lifr_name, if_reqp->lifr_name,
24271915Sgjelinek 		    sizeof (req.lifr_name));
24281915Sgjelinek 		if (ioctl(s, SIOCGLIFADDR, &req) < 0) {
24291915Sgjelinek 			(void) close(s);
24301915Sgjelinek 			if_reqp++;
24311915Sgjelinek 			continue;
24321915Sgjelinek 		}
24331915Sgjelinek 
24341915Sgjelinek 		if ((p = (struct net_if **)realloc(local_ifs,
24351915Sgjelinek 		    sizeof (struct net_if *) * (cnt + 1))) == NULL) {
24361915Sgjelinek 			res = Z_ERR;
24371915Sgjelinek 			break;
24381915Sgjelinek 		}
24391915Sgjelinek 		local_ifs = p;
24401915Sgjelinek 
24411915Sgjelinek 		if ((local_ifs[cnt] = malloc(sizeof (struct net_if))) == NULL) {
24421915Sgjelinek 			res = Z_ERR;
24431915Sgjelinek 			break;
24441915Sgjelinek 		}
24451915Sgjelinek 
24461915Sgjelinek 		if ((local_ifs[cnt]->name = strdup(if_reqp->lifr_name))
24471915Sgjelinek 		    == NULL) {
24481915Sgjelinek 			free(local_ifs[cnt]);
24491915Sgjelinek 			res = Z_ERR;
24501915Sgjelinek 			break;
24511915Sgjelinek 		}
24521915Sgjelinek 		local_ifs[cnt]->af = req.lifr_addr.ss_family;
24531915Sgjelinek 		cnt++;
24541915Sgjelinek 
24551915Sgjelinek 		(void) close(s);
24561915Sgjelinek 		if_reqp++;
24571915Sgjelinek 	}
24581915Sgjelinek 
24591915Sgjelinek 	free(if_buf);
24601915Sgjelinek 
24611915Sgjelinek 	if (res != Z_OK) {
24621915Sgjelinek 		free_local_netifs(cnt, local_ifs);
24631915Sgjelinek 	} else {
24641915Sgjelinek 		*if_cnt = cnt;
24651915Sgjelinek 		*if_list = local_ifs;
24661915Sgjelinek 	}
24671915Sgjelinek 
24681915Sgjelinek 	return (res);
24691915Sgjelinek }
24701915Sgjelinek 
24711915Sgjelinek static char *
24721915Sgjelinek af2str(int af)
24731915Sgjelinek {
24741915Sgjelinek 	switch (af) {
24751915Sgjelinek 	case AF_INET:
24761915Sgjelinek 		return ("IPv4");
24771915Sgjelinek 	case AF_INET6:
24781915Sgjelinek 		return ("IPv6");
24791915Sgjelinek 	default:
24801915Sgjelinek 		return ("Unknown");
24811915Sgjelinek 	}
24821915Sgjelinek }
24831915Sgjelinek 
24841915Sgjelinek /*
24851915Sgjelinek  * Cross check the network interface name and address family with the
24861915Sgjelinek  * interfaces that are set up in the global zone so that we can print the
24871915Sgjelinek  * appropriate error message.
24881915Sgjelinek  */
24891915Sgjelinek static void
24901915Sgjelinek print_net_err(char *phys, char *addr, int af, char *msg)
24911915Sgjelinek {
24921915Sgjelinek 	int		i;
24931915Sgjelinek 	int		local_if_cnt = 0;
24941915Sgjelinek 	struct net_if	**local_ifs = NULL;
24951915Sgjelinek 	boolean_t	found_if = B_FALSE;
24961915Sgjelinek 	boolean_t	found_af = B_FALSE;
24971915Sgjelinek 
24981915Sgjelinek 	if (get_local_netifs(&local_if_cnt, &local_ifs) != Z_OK) {
24991915Sgjelinek 		(void) fprintf(stderr,
25001915Sgjelinek 		    gettext("could not verify %s %s=%s %s=%s\n\t%s\n"),
25011915Sgjelinek 		    "net", "address", addr, "physical", phys, msg);
25021915Sgjelinek 		return;
25031915Sgjelinek 	}
25041915Sgjelinek 
25051915Sgjelinek 	for (i = 0; i < local_if_cnt; i++) {
25061915Sgjelinek 		if (strcmp(phys, local_ifs[i]->name) == 0) {
25071915Sgjelinek 			found_if = B_TRUE;
25081915Sgjelinek 			if (af == local_ifs[i]->af) {
25091915Sgjelinek 				found_af = B_TRUE;
25101915Sgjelinek 				break;
25111915Sgjelinek 			}
25121915Sgjelinek 		}
25131915Sgjelinek 	}
25141915Sgjelinek 
25151915Sgjelinek 	free_local_netifs(local_if_cnt, local_ifs);
25161915Sgjelinek 
25171915Sgjelinek 	if (!found_if) {
25181915Sgjelinek 		(void) fprintf(stderr,
25191915Sgjelinek 		    gettext("could not verify %s %s=%s\n\t"
25201915Sgjelinek 		    "network interface %s is not plumbed in the global zone\n"),
25211915Sgjelinek 		    "net", "physical", phys, phys);
25221915Sgjelinek 		return;
25231915Sgjelinek 	}
25241915Sgjelinek 
25251915Sgjelinek 	/*
25261915Sgjelinek 	 * Print this error if we were unable to find the address family
25271915Sgjelinek 	 * for this interface.  If the af variable is not initialized to
25281915Sgjelinek 	 * to something meaningful by the caller (not AF_UNSPEC) then we
25291915Sgjelinek 	 * also skip this message since it wouldn't be informative.
25301915Sgjelinek 	 */
25311915Sgjelinek 	if (!found_af && af != AF_UNSPEC) {
25321915Sgjelinek 		(void) fprintf(stderr,
25331915Sgjelinek 		    gettext("could not verify %s %s=%s %s=%s\n\tthe %s address "
25343448Sdh155122 		    "family is not configured on this network interface in "
25353448Sdh155122 		    "the\n\tglobal zone\n"),
25361915Sgjelinek 		    "net", "address", addr, "physical", phys, af2str(af));
25371915Sgjelinek 		return;
25381915Sgjelinek 	}
25391915Sgjelinek 
25401915Sgjelinek 	(void) fprintf(stderr,
25411915Sgjelinek 	    gettext("could not verify %s %s=%s %s=%s\n\t%s\n"),
25421915Sgjelinek 	    "net", "address", addr, "physical", phys, msg);
25431915Sgjelinek }
25441915Sgjelinek 
25451645Scomay static int
25463339Szt129084 verify_handle(int cmd_num, zone_dochandle_t handle, char *argv[])
25470Sstevel@tonic-gate {
25480Sstevel@tonic-gate 	struct zone_nwiftab nwiftab;
25490Sstevel@tonic-gate 	int return_code = Z_OK;
25500Sstevel@tonic-gate 	int err;
2551766Scarlsonj 	boolean_t in_alt_root;
25523448Sdh155122 	zone_iptype_t iptype;
25534456Sss150715 	dlpi_handle_t dh;
25540Sstevel@tonic-gate 
2555766Scarlsonj 	in_alt_root = zonecfg_in_alt_root();
2556766Scarlsonj 	if (in_alt_root)
2557766Scarlsonj 		goto no_net;
2558766Scarlsonj 
25593448Sdh155122 	if ((err = zonecfg_get_iptype(handle, &iptype)) != Z_OK) {
25603448Sdh155122 		errno = err;
25613448Sdh155122 		zperror(cmd_to_str(cmd_num), B_TRUE);
25623448Sdh155122 		zonecfg_fini_handle(handle);
25633448Sdh155122 		return (Z_ERR);
25643448Sdh155122 	}
25650Sstevel@tonic-gate 	if ((err = zonecfg_setnwifent(handle)) != Z_OK) {
25660Sstevel@tonic-gate 		errno = err;
25670Sstevel@tonic-gate 		zperror(cmd_to_str(cmd_num), B_TRUE);
25680Sstevel@tonic-gate 		zonecfg_fini_handle(handle);
25690Sstevel@tonic-gate 		return (Z_ERR);
25700Sstevel@tonic-gate 	}
25710Sstevel@tonic-gate 	while (zonecfg_getnwifent(handle, &nwiftab) == Z_OK) {
25720Sstevel@tonic-gate 		struct lifreq lifr;
25731915Sgjelinek 		sa_family_t af = AF_UNSPEC;
25743448Sdh155122 		char dl_owner_zname[ZONENAME_MAX];
25753448Sdh155122 		zoneid_t dl_owner_zid;
25763448Sdh155122 		zoneid_t target_zid;
25773448Sdh155122 		int res;
25780Sstevel@tonic-gate 
25790Sstevel@tonic-gate 		/* skip any loopback interfaces */
25800Sstevel@tonic-gate 		if (strcmp(nwiftab.zone_nwif_physical, "lo0") == 0)
25810Sstevel@tonic-gate 			continue;
25823448Sdh155122 		switch (iptype) {
25833448Sdh155122 		case ZS_SHARED:
25843448Sdh155122 			if ((res = zonecfg_valid_net_address(
25853448Sdh155122 			    nwiftab.zone_nwif_address, &lifr)) != Z_OK) {
25863448Sdh155122 				print_net_err(nwiftab.zone_nwif_physical,
25873448Sdh155122 				    nwiftab.zone_nwif_address, af,
25883448Sdh155122 				    zonecfg_strerror(res));
25894350Std153743 				return_code = Z_ERR;
25904350Std153743 				continue;
25913448Sdh155122 			}
25923448Sdh155122 			af = lifr.lifr_addr.ss_family;
25933448Sdh155122 			if (!zonecfg_ifname_exists(af,
25943448Sdh155122 			    nwiftab.zone_nwif_physical)) {
25953448Sdh155122 				/*
25963448Sdh155122 				 * The interface failed to come up. We continue
25973448Sdh155122 				 * on anyway for the sake of consistency: a
25983448Sdh155122 				 * zone is not shut down if the interface fails
25993448Sdh155122 				 * any time after boot, nor does the global zone
26003448Sdh155122 				 * fail to boot if an interface fails.
26013448Sdh155122 				 */
26023448Sdh155122 				(void) fprintf(stderr,
26033448Sdh155122 				    gettext("WARNING: skipping network "
26044350Std153743 				    "interface '%s' which may not be "
26054350Std153743 				    "present/plumbed in the global "
26064350Std153743 				    "zone.\n"),
26073448Sdh155122 				    nwiftab.zone_nwif_physical);
26083448Sdh155122 			}
26093448Sdh155122 			break;
26103448Sdh155122 		case ZS_EXCLUSIVE:
26113448Sdh155122 			/* Warning if it exists for either IPv4 or IPv6 */
26123448Sdh155122 
26133448Sdh155122 			if (zonecfg_ifname_exists(AF_INET,
26143448Sdh155122 			    nwiftab.zone_nwif_physical) ||
26153448Sdh155122 			    zonecfg_ifname_exists(AF_INET6,
26163448Sdh155122 			    nwiftab.zone_nwif_physical)) {
26173448Sdh155122 				(void) fprintf(stderr,
26183448Sdh155122 				    gettext("WARNING: skipping network "
26193448Sdh155122 				    "interface '%s' which is used in the "
26203448Sdh155122 				    "global zone.\n"),
26213448Sdh155122 				    nwiftab.zone_nwif_physical);
26223448Sdh155122 				break;
26233448Sdh155122 			}
26244456Sss150715 
26252611Svp157776 			/*
26264456Sss150715 			 * Verify that the physical interface can be opened.
26273448Sdh155122 			 */
26284456Sss150715 			err = dlpi_open(nwiftab.zone_nwif_physical, &dh, 0);
26294456Sss150715 			if (err != DLPI_SUCCESS) {
26303448Sdh155122 				(void) fprintf(stderr,
26313448Sdh155122 				    gettext("WARNING: skipping network "
26324456Sss150715 				    "interface '%s' which cannot be opened: "
26334456Sss150715 				    "dlpi error (%s).\n"),
26344456Sss150715 				    nwiftab.zone_nwif_physical,
26354456Sss150715 				    dlpi_strerror(err));
26363448Sdh155122 				break;
26373448Sdh155122 			} else {
26384456Sss150715 				dlpi_close(dh);
26393448Sdh155122 			}
26403448Sdh155122 			/*
26413448Sdh155122 			 * Verify whether the physical interface is already
26423448Sdh155122 			 * used by a zone.
26433448Sdh155122 			 */
26443448Sdh155122 			dl_owner_zid = ALL_ZONES;
26453448Sdh155122 			if (zone_check_datalink(&dl_owner_zid,
26463448Sdh155122 			    nwiftab.zone_nwif_physical) != 0)
26473448Sdh155122 				break;
26483448Sdh155122 
26493448Sdh155122 			/*
26503448Sdh155122 			 * If the zone being verified is
26513448Sdh155122 			 * running and owns the interface
26523448Sdh155122 			 */
26533448Sdh155122 			target_zid = getzoneidbyname(target_zone);
26543448Sdh155122 			if (target_zid == dl_owner_zid)
26553448Sdh155122 				break;
26563448Sdh155122 
26573448Sdh155122 			/* Zone id match failed, use name to check */
26583448Sdh155122 			if (getzonenamebyid(dl_owner_zid, dl_owner_zname,
26593448Sdh155122 			    ZONENAME_MAX) < 0) {
26603448Sdh155122 				/* No name, show ID instead */
26613448Sdh155122 				(void) snprintf(dl_owner_zname, ZONENAME_MAX,
26623448Sdh155122 				    "<%d>", dl_owner_zid);
26633448Sdh155122 			} else if (strcmp(dl_owner_zname, target_zone) == 0)
26643448Sdh155122 				break;
26653448Sdh155122 
26663448Sdh155122 			/*
26673448Sdh155122 			 * Note here we only report a warning that
26683448Sdh155122 			 * the interface is already in use by another
26693448Sdh155122 			 * running zone, and the verify process just
26703448Sdh155122 			 * goes on, if the interface is still in use
26713448Sdh155122 			 * when this zone really boots up, zoneadmd
26723448Sdh155122 			 * will find it. If the name of the zone which
26733448Sdh155122 			 * owns this interface cannot be determined,
26743448Sdh155122 			 * then it is not possible to determine if there
26753448Sdh155122 			 * is a conflict so just report it as a warning.
26762611Svp157776 			 */
26772611Svp157776 			(void) fprintf(stderr,
26783448Sdh155122 			    gettext("WARNING: skipping network interface "
26793448Sdh155122 			    "'%s' which is used by the non-global zone "
26803448Sdh155122 			    "'%s'.\n"), nwiftab.zone_nwif_physical,
26813448Sdh155122 			    dl_owner_zname);
26823448Sdh155122 			break;
26830Sstevel@tonic-gate 		}
26840Sstevel@tonic-gate 	}
26850Sstevel@tonic-gate 	(void) zonecfg_endnwifent(handle);
2686766Scarlsonj no_net:
26870Sstevel@tonic-gate 
26881931Sgjelinek 	/* verify that lofs has not been excluded from the kernel */
26892078Sgjelinek 	if (!(cmd_num == CMD_DETACH || cmd_num == CMD_ATTACH ||
26902078Sgjelinek 	    cmd_num == CMD_MOVE || cmd_num == CMD_CLONE) &&
26912078Sgjelinek 	    modctl(MODLOAD, 1, "fs/lofs", NULL) != 0) {
26921931Sgjelinek 		if (errno == ENXIO)
26931931Sgjelinek 			(void) fprintf(stderr, gettext("could not verify "
26941931Sgjelinek 			    "lofs(7FS): possibly excluded in /etc/system\n"));
26951931Sgjelinek 		else
26961931Sgjelinek 			(void) fprintf(stderr, gettext("could not verify "
26971931Sgjelinek 			    "lofs(7FS): %s\n"), strerror(errno));
26981931Sgjelinek 		return_code = Z_ERR;
26991931Sgjelinek 	}
27001931Sgjelinek 
27010Sstevel@tonic-gate 	if (verify_filesystems(handle) != Z_OK)
27020Sstevel@tonic-gate 		return_code = Z_ERR;
2703823Sgjelinek 	if (verify_ipd(handle) != Z_OK)
2704823Sgjelinek 		return_code = Z_ERR;
2705766Scarlsonj 	if (!in_alt_root && verify_rctls(handle) != Z_OK)
27060Sstevel@tonic-gate 		return_code = Z_ERR;
2707766Scarlsonj 	if (!in_alt_root && verify_pool(handle) != Z_OK)
27080Sstevel@tonic-gate 		return_code = Z_ERR;
27093339Szt129084 	if (!in_alt_root && verify_brand(handle, cmd_num, argv) != Z_OK)
27102712Snn35248 		return_code = Z_ERR;
2711789Sahrens 	if (!in_alt_root && verify_datasets(handle) != Z_OK)
2712789Sahrens 		return_code = Z_ERR;
27131645Scomay 
27141645Scomay 	/*
27151645Scomay 	 * As the "mount" command is used for patching/upgrading of zones
27161645Scomay 	 * or other maintenance processes, the zone's privilege set is not
27171645Scomay 	 * checked in this case.  Instead, the default, safe set of
27181645Scomay 	 * privileges will be used when this zone is created in the
27191645Scomay 	 * kernel.
27201645Scomay 	 */
27211645Scomay 	if (!in_alt_root && cmd_num != CMD_MOUNT &&
27221645Scomay 	    verify_limitpriv(handle) != Z_OK)
27231645Scomay 		return_code = Z_ERR;
27242078Sgjelinek 
27252078Sgjelinek 	return (return_code);
27262078Sgjelinek }
27272078Sgjelinek 
27282078Sgjelinek static int
27293339Szt129084 verify_details(int cmd_num, char *argv[])
27302078Sgjelinek {
27312078Sgjelinek 	zone_dochandle_t handle;
27322078Sgjelinek 	char zonepath[MAXPATHLEN], checkpath[MAXPATHLEN];
27332078Sgjelinek 	int return_code = Z_OK;
27342078Sgjelinek 	int err;
27352078Sgjelinek 
27362078Sgjelinek 	if ((handle = zonecfg_init_handle()) == NULL) {
27372078Sgjelinek 		zperror(cmd_to_str(cmd_num), B_TRUE);
27382078Sgjelinek 		return (Z_ERR);
27392078Sgjelinek 	}
27402078Sgjelinek 	if ((err = zonecfg_get_handle(target_zone, handle)) != Z_OK) {
27412078Sgjelinek 		errno = err;
27422078Sgjelinek 		zperror(cmd_to_str(cmd_num), B_TRUE);
27432078Sgjelinek 		zonecfg_fini_handle(handle);
27442078Sgjelinek 		return (Z_ERR);
27452078Sgjelinek 	}
27462078Sgjelinek 	if ((err = zonecfg_get_zonepath(handle, zonepath, sizeof (zonepath))) !=
27472078Sgjelinek 	    Z_OK) {
27482078Sgjelinek 		errno = err;
27492078Sgjelinek 		zperror(cmd_to_str(cmd_num), B_TRUE);
27502078Sgjelinek 		zonecfg_fini_handle(handle);
27512078Sgjelinek 		return (Z_ERR);
27522078Sgjelinek 	}
27532078Sgjelinek 	/*
27542078Sgjelinek 	 * zonecfg_get_zonepath() gets its data from the XML repository.
27552078Sgjelinek 	 * Verify this against the index file, which is checked first by
27562078Sgjelinek 	 * zone_get_zonepath().  If they don't match, bail out.
27572078Sgjelinek 	 */
27582078Sgjelinek 	if ((err = zone_get_zonepath(target_zone, checkpath,
27592078Sgjelinek 	    sizeof (checkpath))) != Z_OK) {
27602078Sgjelinek 		errno = err;
27612078Sgjelinek 		zperror2(target_zone, gettext("could not get zone path"));
27623716Sgjelinek 		zonecfg_fini_handle(handle);
27632078Sgjelinek 		return (Z_ERR);
27642078Sgjelinek 	}
27652078Sgjelinek 	if (strcmp(zonepath, checkpath) != 0) {
27662078Sgjelinek 		/*
27672078Sgjelinek 		 * TRANSLATION_NOTE
27682078Sgjelinek 		 * XML and zonepath are literals that should not be translated.
27692078Sgjelinek 		 */
27702078Sgjelinek 		(void) fprintf(stderr, gettext("The XML repository has "
27712078Sgjelinek 		    "zonepath '%s',\nbut the index file has zonepath '%s'.\n"
27722078Sgjelinek 		    "These must match, so fix the incorrect entry.\n"),
27732078Sgjelinek 		    zonepath, checkpath);
27743716Sgjelinek 		zonecfg_fini_handle(handle);
27752078Sgjelinek 		return (Z_ERR);
27762078Sgjelinek 	}
27772078Sgjelinek 	if (validate_zonepath(zonepath, cmd_num) != Z_OK) {
27782078Sgjelinek 		(void) fprintf(stderr, gettext("could not verify zonepath %s "
27792078Sgjelinek 		    "because of the above errors.\n"), zonepath);
27802078Sgjelinek 		return_code = Z_ERR;
27812078Sgjelinek 	}
27822078Sgjelinek 
27833339Szt129084 	if (verify_handle(cmd_num, handle, argv) != Z_OK)
27842078Sgjelinek 		return_code = Z_ERR;
27852078Sgjelinek 
27860Sstevel@tonic-gate 	zonecfg_fini_handle(handle);
27870Sstevel@tonic-gate 	if (return_code == Z_ERR)
27880Sstevel@tonic-gate 		(void) fprintf(stderr,
27890Sstevel@tonic-gate 		    gettext("%s: zone %s failed to verify\n"),
27900Sstevel@tonic-gate 		    execname, target_zone);
27910Sstevel@tonic-gate 	return (return_code);
27920Sstevel@tonic-gate }
27930Sstevel@tonic-gate 
27940Sstevel@tonic-gate static int
27950Sstevel@tonic-gate verify_func(int argc, char *argv[])
27960Sstevel@tonic-gate {
27970Sstevel@tonic-gate 	int arg;
27980Sstevel@tonic-gate 
27990Sstevel@tonic-gate 	optind = 0;
28000Sstevel@tonic-gate 	if ((arg = getopt(argc, argv, "?")) != EOF) {
28010Sstevel@tonic-gate 		switch (arg) {
28020Sstevel@tonic-gate 		case '?':
28030Sstevel@tonic-gate 			sub_usage(SHELP_VERIFY, CMD_VERIFY);
28040Sstevel@tonic-gate 			return (optopt == '?' ? Z_OK : Z_USAGE);
28050Sstevel@tonic-gate 		default:
28060Sstevel@tonic-gate 			sub_usage(SHELP_VERIFY, CMD_VERIFY);
28070Sstevel@tonic-gate 			return (Z_USAGE);
28080Sstevel@tonic-gate 		}
28090Sstevel@tonic-gate 	}
28100Sstevel@tonic-gate 	if (argc > optind) {
28110Sstevel@tonic-gate 		sub_usage(SHELP_VERIFY, CMD_VERIFY);
28120Sstevel@tonic-gate 		return (Z_USAGE);
28130Sstevel@tonic-gate 	}
28142712Snn35248 	if (sanity_check(target_zone, CMD_VERIFY, B_FALSE, B_FALSE, B_FALSE)
28152712Snn35248 	    != Z_OK)
28160Sstevel@tonic-gate 		return (Z_ERR);
28173339Szt129084 	return (verify_details(CMD_VERIFY, argv));
28180Sstevel@tonic-gate }
28190Sstevel@tonic-gate 
28202712Snn35248 static int
28217089Sgjelinek addoptions(char *buf, char *argv[], size_t len)
28227089Sgjelinek {
28237089Sgjelinek 	int i = 0;
28247089Sgjelinek 
28257089Sgjelinek 	if (buf[0] == '\0')
28267089Sgjelinek 		return (Z_OK);
28277089Sgjelinek 
28287089Sgjelinek 	while (argv[i] != NULL) {
28297089Sgjelinek 		if (strlcat(buf, " ", len) >= len ||
28307089Sgjelinek 		    strlcat(buf, argv[i++], len) >= len) {
28317089Sgjelinek 			zerror("Command line too long");
28327089Sgjelinek 			return (Z_ERR);
28337089Sgjelinek 		}
28347089Sgjelinek 	}
28357089Sgjelinek 
28367089Sgjelinek 	return (Z_OK);
28377089Sgjelinek }
28387089Sgjelinek 
28397089Sgjelinek static int
28402712Snn35248 addopt(char *buf, int opt, char *optarg, size_t bufsize)
28412712Snn35248 {
28422712Snn35248 	char optstring[4];
28432712Snn35248 
28442712Snn35248 	if (opt > 0)
28452712Snn35248 		(void) sprintf(optstring, " -%c", opt);
28462712Snn35248 	else
28472712Snn35248 		(void) strcpy(optstring, " ");
28482712Snn35248 
28492712Snn35248 	if ((strlcat(buf, optstring, bufsize) > bufsize))
28502712Snn35248 		return (Z_ERR);
28517089Sgjelinek 
28522712Snn35248 	if ((optarg != NULL) && (strlcat(buf, optarg, bufsize) > bufsize))
28532712Snn35248 		return (Z_ERR);
28547089Sgjelinek 
28552712Snn35248 	return (Z_OK);
28562712Snn35248 }
28570Sstevel@tonic-gate 
28587089Sgjelinek /* ARGSUSED */
28590Sstevel@tonic-gate static int
28600Sstevel@tonic-gate install_func(int argc, char *argv[])
28610Sstevel@tonic-gate {
28622712Snn35248 	char cmdbuf[MAXPATHLEN];
28634586Sgjelinek 	char postcmdbuf[MAXPATHLEN];
28640Sstevel@tonic-gate 	int lockfd;
28652712Snn35248 	int arg, err, subproc_err;
28660Sstevel@tonic-gate 	char zonepath[MAXPATHLEN];
28672727Sedp 	brand_handle_t bh = NULL;
28680Sstevel@tonic-gate 	int status;
28691867Sgjelinek 	boolean_t nodataset = B_FALSE;
28704586Sgjelinek 	boolean_t do_postinstall = B_FALSE;
28717089Sgjelinek 	boolean_t brand_help = B_FALSE;
28722712Snn35248 	char opts[128];
28732712Snn35248 
28742712Snn35248 	if (target_zone == NULL) {
28752712Snn35248 		sub_usage(SHELP_INSTALL, CMD_INSTALL);
28762712Snn35248 		return (Z_USAGE);
28772712Snn35248 	}
28780Sstevel@tonic-gate 
2879766Scarlsonj 	if (zonecfg_in_alt_root()) {
2880766Scarlsonj 		zerror(gettext("cannot install zone in alternate root"));
2881766Scarlsonj 		return (Z_ERR);
2882766Scarlsonj 	}
2883766Scarlsonj 
28842712Snn35248 	if ((err = zone_get_zonepath(target_zone, zonepath,
28852712Snn35248 	    sizeof (zonepath))) != Z_OK) {
28862712Snn35248 		errno = err;
28872712Snn35248 		zperror2(target_zone, gettext("could not get zone path"));
28882712Snn35248 		return (Z_ERR);
28892712Snn35248 	}
28902712Snn35248 
28912712Snn35248 	/* Fetch the install command from the brand configuration.  */
28922727Sedp 	if ((bh = brand_open(target_brand)) == NULL) {
28932712Snn35248 		zerror(gettext("missing or invalid brand"));
28942712Snn35248 		return (Z_ERR);
28952712Snn35248 	}
28962712Snn35248 
28977089Sgjelinek 	if (get_hook(bh, cmdbuf, sizeof (cmdbuf), brand_get_install,
28987089Sgjelinek 	    target_zone, zonepath) != Z_OK) {
28992712Snn35248 		zerror("invalid brand configuration: missing install resource");
29002727Sedp 		brand_close(bh);
29012712Snn35248 		return (Z_ERR);
29022712Snn35248 	}
29032712Snn35248 
29047089Sgjelinek 	if (get_hook(bh, postcmdbuf, sizeof (postcmdbuf), brand_get_postinstall,
29057089Sgjelinek 	    target_zone, zonepath) != Z_OK) {
29064586Sgjelinek 		zerror("invalid brand configuration: missing postinstall "
29074586Sgjelinek 		    "resource");
29084586Sgjelinek 		brand_close(bh);
29094586Sgjelinek 		return (Z_ERR);
29107089Sgjelinek 	}
29117089Sgjelinek 
29127089Sgjelinek 	if (postcmdbuf[0] != '\0')
29134586Sgjelinek 		do_postinstall = B_TRUE;
29144586Sgjelinek 
29152712Snn35248 	(void) strcpy(opts, "?x:");
29167089Sgjelinek 	/*
29177089Sgjelinek 	 * Fetch the list of recognized command-line options from
29187089Sgjelinek 	 * the brand configuration file.
29197089Sgjelinek 	 */
29207089Sgjelinek 	if (brand_get_installopts(bh, opts + strlen(opts),
29217089Sgjelinek 	    sizeof (opts) - strlen(opts)) != 0) {
29227089Sgjelinek 		zerror("invalid brand configuration: missing "
29237089Sgjelinek 		    "install options resource");
29247089Sgjelinek 		brand_close(bh);
29257089Sgjelinek 		return (Z_ERR);
29267089Sgjelinek 	}
29277089Sgjelinek 
29282727Sedp 	brand_close(bh);
29292712Snn35248 
29307089Sgjelinek 	if (cmdbuf[0] == '\0') {
29317089Sgjelinek 		zerror("Missing brand install command");
29327089Sgjelinek 		return (Z_ERR);
29337089Sgjelinek 	}
29347089Sgjelinek 
29357089Sgjelinek 	/* Check the argv string for args we handle internally */
29360Sstevel@tonic-gate 	optind = 0;
29377089Sgjelinek 	opterr = 0;
29382712Snn35248 	while ((arg = getopt(argc, argv, opts)) != EOF) {
29390Sstevel@tonic-gate 		switch (arg) {
29400Sstevel@tonic-gate 		case '?':
29417089Sgjelinek 			if (optopt == '?') {
29421867Sgjelinek 				sub_usage(SHELP_INSTALL, CMD_INSTALL);
29437089Sgjelinek 				brand_help = B_TRUE;
29442712Snn35248 			}
29457089Sgjelinek 			/* Ignore unknown options - may be brand specific. */
29467089Sgjelinek 			break;
29477089Sgjelinek 		case 'x':
29487089Sgjelinek 			/* Handle this option internally, don't pass to brand */
29497089Sgjelinek 			if (strcmp(optarg, "nodataset") == 0) {
29507089Sgjelinek 				/* Handle this option internally */
29517089Sgjelinek 				nodataset = B_TRUE;
29522712Snn35248 			}
29537089Sgjelinek 			continue;
29547089Sgjelinek 		default:
29557089Sgjelinek 			/* Ignore unknown options - may be brand specific. */
29562712Snn35248 			break;
29570Sstevel@tonic-gate 		}
29587089Sgjelinek 
29597089Sgjelinek 		/*
29607089Sgjelinek 		 * Append the option to the command line passed to the
29617089Sgjelinek 		 * brand-specific install and postinstall routines.
29627089Sgjelinek 		 */
29637089Sgjelinek 		if (addopt(cmdbuf, optopt, optarg, sizeof (cmdbuf)) != Z_OK) {
29647089Sgjelinek 			zerror("Install command line too long");
29657089Sgjelinek 			return (Z_ERR);
29667089Sgjelinek 		}
29677089Sgjelinek 		if (addopt(postcmdbuf, optopt, optarg, sizeof (postcmdbuf))
29687089Sgjelinek 		    != Z_OK) {
29697089Sgjelinek 			zerror("Post-Install command line too long");
29707089Sgjelinek 			return (Z_ERR);
29717089Sgjelinek 		}
29727089Sgjelinek 	}
29737089Sgjelinek 
29747089Sgjelinek 	for (; optind < argc; optind++) {
29757089Sgjelinek 		if (addopt(cmdbuf, 0, argv[optind], sizeof (cmdbuf)) != Z_OK) {
29767089Sgjelinek 			zerror("Install command line too long");
29777089Sgjelinek 			return (Z_ERR);
29787089Sgjelinek 		}
29797089Sgjelinek 
29807089Sgjelinek 		if (addopt(postcmdbuf, 0, argv[optind], sizeof (postcmdbuf))
29817089Sgjelinek 		    != Z_OK) {
29827089Sgjelinek 			zerror("Post-Install command line too long");
29837089Sgjelinek 			return (Z_ERR);
29842712Snn35248 		}
29852712Snn35248 	}
29862712Snn35248 
29877089Sgjelinek 	if (!brand_help) {
29887089Sgjelinek 		if (sanity_check(target_zone, CMD_INSTALL, B_FALSE, B_TRUE,
29897089Sgjelinek 		    B_FALSE) != Z_OK)
29907089Sgjelinek 			return (Z_ERR);
29917089Sgjelinek 		if (verify_details(CMD_INSTALL, argv) != Z_OK)
29927089Sgjelinek 			return (Z_ERR);
29937089Sgjelinek 
29947089Sgjelinek 		if (zonecfg_grab_lock_file(target_zone, &lockfd) != Z_OK) {
29957089Sgjelinek 			zerror(gettext("another %s may have an operation in "
29967089Sgjelinek 			    "progress."), "zoneadm");
29977089Sgjelinek 			return (Z_ERR);
29987089Sgjelinek 		}
29997089Sgjelinek 		err = zone_set_state(target_zone, ZONE_STATE_INCOMPLETE);
30007089Sgjelinek 		if (err != Z_OK) {
30017089Sgjelinek 			errno = err;
30027089Sgjelinek 			zperror2(target_zone, gettext("could not set state"));
30037089Sgjelinek 			goto done;
30047089Sgjelinek 		}
30057089Sgjelinek 
30067089Sgjelinek 		if (!nodataset)
30077089Sgjelinek 			create_zfs_zonepath(zonepath);
30087089Sgjelinek 	}
30097089Sgjelinek 
30107089Sgjelinek 	status = do_subproc_interactive(cmdbuf);
30112712Snn35248 	if ((subproc_err =
30122712Snn35248 	    subproc_status(gettext("brand-specific installation"), status,
30132712Snn35248 	    B_FALSE)) != ZONE_SUBPROC_OK) {
30147089Sgjelinek 		if (subproc_err == ZONE_SUBPROC_USAGE && !brand_help) {
30157089Sgjelinek 			sub_usage(SHELP_INSTALL, CMD_INSTALL);
30167089Sgjelinek 			zonecfg_release_lock_file(target_zone, lockfd);
30177089Sgjelinek 			return (Z_ERR);
30187089Sgjelinek 		}
30192712Snn35248 		err = Z_ERR;
30200Sstevel@tonic-gate 		goto done;
30212712Snn35248 	}
30220Sstevel@tonic-gate 
30237089Sgjelinek 	if (brand_help)
30247089Sgjelinek 		return (Z_OK);
30257089Sgjelinek 
30260Sstevel@tonic-gate 	if ((err = zone_set_state(target_zone, ZONE_STATE_INSTALLED)) != Z_OK) {
30270Sstevel@tonic-gate 		errno = err;
30280Sstevel@tonic-gate 		zperror2(target_zone, gettext("could not set state"));
30290Sstevel@tonic-gate 		goto done;
30300Sstevel@tonic-gate 	}
30310Sstevel@tonic-gate 
30324586Sgjelinek 	if (do_postinstall) {
30334586Sgjelinek 		status = do_subproc(postcmdbuf);
30344586Sgjelinek 
30354586Sgjelinek 		if ((subproc_err =
30364586Sgjelinek 		    subproc_status(gettext("brand-specific post-install"),
30374586Sgjelinek 		    status, B_FALSE)) != ZONE_SUBPROC_OK) {
30384586Sgjelinek 			err = Z_ERR;
30394586Sgjelinek 			(void) zone_set_state(target_zone,
30404586Sgjelinek 			    ZONE_STATE_INCOMPLETE);
30414586Sgjelinek 		}
30424586Sgjelinek 	}
30434586Sgjelinek 
30440Sstevel@tonic-gate done:
30452712Snn35248 	/*
30467089Sgjelinek 	 * If the install script exited with ZONE_SUBPROC_NOTCOMPLETE, try to
30477089Sgjelinek 	 * clean up the zone and leave the zone in the CONFIGURED state so that
30487089Sgjelinek 	 * another install can be attempted without requiring an uninstall
30497089Sgjelinek 	 * first.
30502712Snn35248 	 */
30517089Sgjelinek 	if (subproc_err == ZONE_SUBPROC_NOTCOMPLETE) {
30522712Snn35248 		if ((err = cleanup_zonepath(zonepath, B_FALSE)) != Z_OK) {
30532712Snn35248 			errno = err;
30542712Snn35248 			zperror2(target_zone,
30552712Snn35248 			    gettext("cleaning up zonepath failed"));
30562712Snn35248 		} else if ((err = zone_set_state(target_zone,
30572712Snn35248 		    ZONE_STATE_CONFIGURED)) != Z_OK) {
30582712Snn35248 			errno = err;
30592712Snn35248 			zperror2(target_zone, gettext("could not set state"));
30602712Snn35248 		}
30612712Snn35248 	}
30622712Snn35248 
30637089Sgjelinek 	if (!brand_help)
30647089Sgjelinek 		zonecfg_release_lock_file(target_zone, lockfd);
30650Sstevel@tonic-gate 	return ((err == Z_OK) ? Z_OK : Z_ERR);
30660Sstevel@tonic-gate }
30670Sstevel@tonic-gate 
30680Sstevel@tonic-gate /*
30691300Sgjelinek  * Check that the inherited pkg dirs are the same for the clone and its source.
30701300Sgjelinek  * The easiest way to do that is check that the list of ipds is the same
30711300Sgjelinek  * by matching each one against the other.  This algorithm should be fine since
30721300Sgjelinek  * the list of ipds should not be that long.
30731300Sgjelinek  */
30741300Sgjelinek static int
30751300Sgjelinek valid_ipd_clone(zone_dochandle_t s_handle, char *source_zone,
30761300Sgjelinek 	zone_dochandle_t t_handle, char *target_zone)
30771300Sgjelinek {
30781300Sgjelinek 	int err;
30791300Sgjelinek 	int res = Z_OK;
30801300Sgjelinek 	int s_cnt = 0;
30811300Sgjelinek 	int t_cnt = 0;
30821300Sgjelinek 	struct zone_fstab s_fstab;
30831300Sgjelinek 	struct zone_fstab t_fstab;
30841300Sgjelinek 
30851300Sgjelinek 	/*
30861300Sgjelinek 	 * First check the source of the clone against the target.
30871300Sgjelinek 	 */
30881300Sgjelinek 	if ((err = zonecfg_setipdent(s_handle)) != Z_OK) {
30891300Sgjelinek 		errno = err;
30901300Sgjelinek 		zperror2(source_zone, gettext("could not enumerate "
30911300Sgjelinek 		    "inherit-pkg-dirs"));
30921300Sgjelinek 		return (Z_ERR);
30931300Sgjelinek 	}
30941300Sgjelinek 
30951300Sgjelinek 	while (zonecfg_getipdent(s_handle, &s_fstab) == Z_OK) {
30961300Sgjelinek 		boolean_t match = B_FALSE;
30971300Sgjelinek 
30981300Sgjelinek 		s_cnt++;
30991300Sgjelinek 
31001300Sgjelinek 		if ((err = zonecfg_setipdent(t_handle)) != Z_OK) {
31011300Sgjelinek 			errno = err;
31021300Sgjelinek 			zperror2(target_zone, gettext("could not enumerate "
31031300Sgjelinek 			    "inherit-pkg-dirs"));
31041300Sgjelinek 			(void) zonecfg_endipdent(s_handle);
31051300Sgjelinek 			return (Z_ERR);
31061300Sgjelinek 		}
31071300Sgjelinek 
31081300Sgjelinek 		while (zonecfg_getipdent(t_handle, &t_fstab) == Z_OK) {
31091300Sgjelinek 			if (strcmp(s_fstab.zone_fs_dir, t_fstab.zone_fs_dir)
31101300Sgjelinek 			    == 0) {
31111300Sgjelinek 				match = B_TRUE;
31121300Sgjelinek 				break;
31131300Sgjelinek 			}
31141300Sgjelinek 		}
31151300Sgjelinek 		(void) zonecfg_endipdent(t_handle);
31161300Sgjelinek 
31171300Sgjelinek 		if (!match) {
31181300Sgjelinek 			(void) fprintf(stderr, gettext("inherit-pkg-dir "
31191300Sgjelinek 			    "'%s' is not configured in zone %s.\n"),
31201300Sgjelinek 			    s_fstab.zone_fs_dir, target_zone);
31211300Sgjelinek 			res = Z_ERR;
31221300Sgjelinek 		}
31231300Sgjelinek 	}
31241300Sgjelinek 
31251300Sgjelinek 	(void) zonecfg_endipdent(s_handle);
31261300Sgjelinek 
31271300Sgjelinek 	/* skip the next check if we already have errors */
31281300Sgjelinek 	if (res == Z_ERR)
31291300Sgjelinek 		return (res);
31301300Sgjelinek 
31311300Sgjelinek 	/*
31321300Sgjelinek 	 * Now check the number of ipds in the target so we can verify
31331300Sgjelinek 	 * that the source is not a subset of the target.
31341300Sgjelinek 	 */
31351300Sgjelinek 	if ((err = zonecfg_setipdent(t_handle)) != Z_OK) {
31361300Sgjelinek 		errno = err;
31371300Sgjelinek 		zperror2(target_zone, gettext("could not enumerate "
31381300Sgjelinek 		    "inherit-pkg-dirs"));
31391300Sgjelinek 		return (Z_ERR);
31401300Sgjelinek 	}
31411300Sgjelinek 
31421300Sgjelinek 	while (zonecfg_getipdent(t_handle, &t_fstab) == Z_OK)
31431300Sgjelinek 		t_cnt++;
31441300Sgjelinek 
31451300Sgjelinek 	(void) zonecfg_endipdent(t_handle);
31461300Sgjelinek 
31471300Sgjelinek 	if (t_cnt != s_cnt) {
31481300Sgjelinek 		(void) fprintf(stderr, gettext("Zone %s is configured "
31491300Sgjelinek 		    "with inherit-pkg-dirs that are not configured in zone "
31501300Sgjelinek 		    "%s.\n"), target_zone, source_zone);
31511300Sgjelinek 		res = Z_ERR;
31521300Sgjelinek 	}
31531300Sgjelinek 
31541300Sgjelinek 	return (res);
31551300Sgjelinek }
31561300Sgjelinek 
31571300Sgjelinek static void
31581300Sgjelinek warn_dev_match(zone_dochandle_t s_handle, char *source_zone,
31591300Sgjelinek 	zone_dochandle_t t_handle, char *target_zone)
31601300Sgjelinek {
31611300Sgjelinek 	int err;
31621300Sgjelinek 	struct zone_devtab s_devtab;
31631300Sgjelinek 	struct zone_devtab t_devtab;
31641300Sgjelinek 
31651300Sgjelinek 	if ((err = zonecfg_setdevent(t_handle)) != Z_OK) {
31661300Sgjelinek 		errno = err;
31671300Sgjelinek 		zperror2(target_zone, gettext("could not enumerate devices"));
31681300Sgjelinek 		return;
31691300Sgjelinek 	}
31701300Sgjelinek 
31711300Sgjelinek 	while (zonecfg_getdevent(t_handle, &t_devtab) == Z_OK) {
31721300Sgjelinek 		if ((err = zonecfg_setdevent(s_handle)) != Z_OK) {
31731300Sgjelinek 			errno = err;
31741300Sgjelinek 			zperror2(source_zone,
31751300Sgjelinek 			    gettext("could not enumerate devices"));
31761300Sgjelinek 			(void) zonecfg_enddevent(t_handle);
31771300Sgjelinek 			return;
31781300Sgjelinek 		}
31791300Sgjelinek 
31801300Sgjelinek 		while (zonecfg_getdevent(s_handle, &s_devtab) == Z_OK) {
31811300Sgjelinek 			/*
31821300Sgjelinek 			 * Use fnmatch to catch the case where wildcards
31831300Sgjelinek 			 * were used in one zone and the other has an
31841300Sgjelinek 			 * explicit entry (e.g. /dev/dsk/c0t0d0s6 vs.
31851300Sgjelinek 			 * /dev/\*dsk/c0t0d0s6).
31861300Sgjelinek 			 */
31871300Sgjelinek 			if (fnmatch(t_devtab.zone_dev_match,
31881300Sgjelinek 			    s_devtab.zone_dev_match, FNM_PATHNAME) == 0 ||
31891300Sgjelinek 			    fnmatch(s_devtab.zone_dev_match,
31901300Sgjelinek 			    t_devtab.zone_dev_match, FNM_PATHNAME) == 0) {
31911300Sgjelinek 				(void) fprintf(stderr,
31921300Sgjelinek 				    gettext("WARNING: device '%s' "
31931300Sgjelinek 				    "is configured in both zones.\n"),
31941300Sgjelinek 				    t_devtab.zone_dev_match);
31951300Sgjelinek 				break;
31961300Sgjelinek 			}
31971300Sgjelinek 		}
31981300Sgjelinek 		(void) zonecfg_enddevent(s_handle);
31991300Sgjelinek 	}
32001300Sgjelinek 
32011300Sgjelinek 	(void) zonecfg_enddevent(t_handle);
32021300Sgjelinek }
32031300Sgjelinek 
32041300Sgjelinek /*
32051300Sgjelinek  * Check if the specified mount option (opt) is contained within the
32061300Sgjelinek  * options string.
32071300Sgjelinek  */
32081300Sgjelinek static boolean_t
32091300Sgjelinek opt_match(char *opt, char *options)
32101300Sgjelinek {
32111300Sgjelinek 	char *p;
32121300Sgjelinek 	char *lastp;
32131300Sgjelinek 
32141300Sgjelinek 	if ((p = strtok_r(options, ",", &lastp)) != NULL) {
32151300Sgjelinek 		if (strcmp(p, opt) == 0)
32161300Sgjelinek 			return (B_TRUE);
32171300Sgjelinek 		while ((p = strtok_r(NULL, ",", &lastp)) != NULL) {
32181300Sgjelinek 			if (strcmp(p, opt) == 0)
32191300Sgjelinek 				return (B_TRUE);
32201300Sgjelinek 		}
32211300Sgjelinek 	}
32221300Sgjelinek 
32231300Sgjelinek 	return (B_FALSE);
32241300Sgjelinek }
32251300Sgjelinek 
32261867Sgjelinek #define	RW_LOFS	"WARNING: read-write lofs file system on '%s' is configured " \
32271300Sgjelinek 	"in both zones.\n"
32281300Sgjelinek 
32291300Sgjelinek static void
32301300Sgjelinek print_fs_warnings(struct zone_fstab *s_fstab, struct zone_fstab *t_fstab)
32311300Sgjelinek {
32321300Sgjelinek 	/*
32331300Sgjelinek 	 * It is ok to have shared lofs mounted fs but we want to warn if
32341300Sgjelinek 	 * either is rw since this will effect the other zone.
32351300Sgjelinek 	 */
32361300Sgjelinek 	if (strcmp(t_fstab->zone_fs_type, "lofs") == 0) {
32371300Sgjelinek 		zone_fsopt_t *optp;
32381300Sgjelinek 
32391300Sgjelinek 		/* The default is rw so no options means rw */
32401300Sgjelinek 		if (t_fstab->zone_fs_options == NULL ||
32411300Sgjelinek 		    s_fstab->zone_fs_options == NULL) {
32421300Sgjelinek 			(void) fprintf(stderr, gettext(RW_LOFS),
32431300Sgjelinek 			    t_fstab->zone_fs_special);
32441300Sgjelinek 			return;
32451300Sgjelinek 		}
32461300Sgjelinek 
32471300Sgjelinek 		for (optp = s_fstab->zone_fs_options; optp != NULL;
32481300Sgjelinek 		    optp = optp->zone_fsopt_next) {
32491300Sgjelinek 			if (opt_match("rw", optp->zone_fsopt_opt)) {
32501300Sgjelinek 				(void) fprintf(stderr, gettext(RW_LOFS),
32511300Sgjelinek 				    s_fstab->zone_fs_special);
32521300Sgjelinek 				return;
32531300Sgjelinek 			}
32541300Sgjelinek 		}
32551300Sgjelinek 
32561300Sgjelinek 		for (optp = t_fstab->zone_fs_options; optp != NULL;
32571300Sgjelinek 		    optp = optp->zone_fsopt_next) {
32581300Sgjelinek 			if (opt_match("rw", optp->zone_fsopt_opt)) {
32591300Sgjelinek 				(void) fprintf(stderr, gettext(RW_LOFS),
32601300Sgjelinek 				    t_fstab->zone_fs_special);
32611300Sgjelinek 				return;
32621300Sgjelinek 			}
32631300Sgjelinek 		}
32641300Sgjelinek 
32651300Sgjelinek 		return;
32661300Sgjelinek 	}
32671300Sgjelinek 
32681300Sgjelinek 	/*
32691300Sgjelinek 	 * TRANSLATION_NOTE
32701867Sgjelinek 	 * The first variable is the file system type and the second is
32711867Sgjelinek 	 * the file system special device.  For example,
32721867Sgjelinek 	 * WARNING: ufs file system on '/dev/dsk/c0t0d0s0' ...
32731300Sgjelinek 	 */
32741867Sgjelinek 	(void) fprintf(stderr, gettext("WARNING: %s file system on '%s' "
32751300Sgjelinek 	    "is configured in both zones.\n"), t_fstab->zone_fs_type,
32761300Sgjelinek 	    t_fstab->zone_fs_special);
32771300Sgjelinek }
32781300Sgjelinek 
32791300Sgjelinek static void
32801300Sgjelinek warn_fs_match(zone_dochandle_t s_handle, char *source_zone,
32811300Sgjelinek 	zone_dochandle_t t_handle, char *target_zone)
32821300Sgjelinek {
32831300Sgjelinek 	int err;
32841300Sgjelinek 	struct zone_fstab s_fstab;
32851300Sgjelinek 	struct zone_fstab t_fstab;
32861300Sgjelinek 
32871300Sgjelinek 	if ((err = zonecfg_setfsent(t_handle)) != Z_OK) {
32881300Sgjelinek 		errno = err;
32891300Sgjelinek 		zperror2(target_zone,
32901867Sgjelinek 		    gettext("could not enumerate file systems"));
32911300Sgjelinek 		return;
32921300Sgjelinek 	}
32931300Sgjelinek 
32941300Sgjelinek 	while (zonecfg_getfsent(t_handle, &t_fstab) == Z_OK) {
32951300Sgjelinek 		if ((err = zonecfg_setfsent(s_handle)) != Z_OK) {
32961300Sgjelinek 			errno = err;
32971300Sgjelinek 			zperror2(source_zone,
32981867Sgjelinek 			    gettext("could not enumerate file systems"));
32991300Sgjelinek 			(void) zonecfg_endfsent(t_handle);
33001300Sgjelinek 			return;
33011300Sgjelinek 		}
33021300Sgjelinek 
33031300Sgjelinek 		while (zonecfg_getfsent(s_handle, &s_fstab) == Z_OK) {
33041300Sgjelinek 			if (strcmp(t_fstab.zone_fs_special,
33051300Sgjelinek 			    s_fstab.zone_fs_special) == 0) {
33061300Sgjelinek 				print_fs_warnings(&s_fstab, &t_fstab);
33071300Sgjelinek 				break;
33081300Sgjelinek 			}
33091300Sgjelinek 		}
33101300Sgjelinek 		(void) zonecfg_endfsent(s_handle);
33111300Sgjelinek 	}
33121300Sgjelinek 
33131300Sgjelinek 	(void) zonecfg_endfsent(t_handle);
33141300Sgjelinek }
33151300Sgjelinek 
33161300Sgjelinek /*
33171300Sgjelinek  * We don't catch the case where you used the same IP address but
33181300Sgjelinek  * it is not an exact string match.  For example, 192.9.0.128 vs. 192.09.0.128.
33191300Sgjelinek  * However, we're not going to worry about that but we will check for
33201300Sgjelinek  * a possible netmask on one of the addresses (e.g. 10.0.0.1 and 10.0.0.1/24)
33211300Sgjelinek  * and handle that case as a match.
33221300Sgjelinek  */
33231300Sgjelinek static void
33241300Sgjelinek warn_ip_match(zone_dochandle_t s_handle, char *source_zone,
33251300Sgjelinek 	zone_dochandle_t t_handle, char *target_zone)
33261300Sgjelinek {
33271300Sgjelinek 	int err;
33281300Sgjelinek 	struct zone_nwiftab s_nwiftab;
33291300Sgjelinek 	struct zone_nwiftab t_nwiftab;
33301300Sgjelinek 
33311300Sgjelinek 	if ((err = zonecfg_setnwifent(t_handle)) != Z_OK) {
33321300Sgjelinek 		errno = err;
33331300Sgjelinek 		zperror2(target_zone,
33341300Sgjelinek 		    gettext("could not enumerate network interfaces"));
33351300Sgjelinek 		return;
33361300Sgjelinek 	}
33371300Sgjelinek 
33381300Sgjelinek 	while (zonecfg_getnwifent(t_handle, &t_nwiftab) == Z_OK) {
33391300Sgjelinek 		char *p;
33401300Sgjelinek 
33411300Sgjelinek 		/* remove an (optional) netmask from the address */
33421300Sgjelinek 		if ((p = strchr(t_nwiftab.zone_nwif_address, '/')) != NULL)
33431300Sgjelinek 			*p = '\0';
33441300Sgjelinek 
33451300Sgjelinek 		if ((err = zonecfg_setnwifent(s_handle)) != Z_OK) {
33461300Sgjelinek 			errno = err;
33471300Sgjelinek 			zperror2(source_zone,
33481300Sgjelinek 			    gettext("could not enumerate network interfaces"));
33491300Sgjelinek 			(void) zonecfg_endnwifent(t_handle);
33501300Sgjelinek 			return;
33511300Sgjelinek 		}
33521300Sgjelinek 
33531300Sgjelinek 		while (zonecfg_getnwifent(s_handle, &s_nwiftab) == Z_OK) {
33541300Sgjelinek 			/* remove an (optional) netmask from the address */
33551300Sgjelinek 			if ((p = strchr(s_nwiftab.zone_nwif_address, '/'))
33561300Sgjelinek 			    != NULL)
33571300Sgjelinek 				*p = '\0';
33581300Sgjelinek 
33593448Sdh155122 			/* For exclusive-IP zones, address is not specified. */
33603448Sdh155122 			if (strlen(s_nwiftab.zone_nwif_address) == 0)
33613448Sdh155122 				continue;
33623448Sdh155122 
33631300Sgjelinek 			if (strcmp(t_nwiftab.zone_nwif_address,
33641300Sgjelinek 			    s_nwiftab.zone_nwif_address) == 0) {
33651300Sgjelinek 				(void) fprintf(stderr,
33661300Sgjelinek 				    gettext("WARNING: network address '%s' "
33671300Sgjelinek 				    "is configured in both zones.\n"),
33681300Sgjelinek 				    t_nwiftab.zone_nwif_address);
33691300Sgjelinek 				break;
33701300Sgjelinek 			}
33711300Sgjelinek 		}
33721300Sgjelinek 		(void) zonecfg_endnwifent(s_handle);
33731300Sgjelinek 	}
33741300Sgjelinek 
33751300Sgjelinek 	(void) zonecfg_endnwifent(t_handle);
33761300Sgjelinek }
33771300Sgjelinek 
33781300Sgjelinek static void
33792712Snn35248 warn_dataset_match(zone_dochandle_t s_handle, char *source,
33802712Snn35248 	zone_dochandle_t t_handle, char *target)
33811300Sgjelinek {
33821300Sgjelinek 	int err;
33831300Sgjelinek 	struct zone_dstab s_dstab;
33841300Sgjelinek 	struct zone_dstab t_dstab;
33851300Sgjelinek 
33861300Sgjelinek 	if ((err = zonecfg_setdsent(t_handle)) != Z_OK) {
33871300Sgjelinek 		errno = err;
33882712Snn35248 		zperror2(target, gettext("could not enumerate datasets"));
33891300Sgjelinek 		return;
33901300Sgjelinek 	}
33911300Sgjelinek 
33921300Sgjelinek 	while (zonecfg_getdsent(t_handle, &t_dstab) == Z_OK) {
33931300Sgjelinek 		if ((err = zonecfg_setdsent(s_handle)) != Z_OK) {
33941300Sgjelinek 			errno = err;
33952712Snn35248 			zperror2(source,
33961300Sgjelinek 			    gettext("could not enumerate datasets"));
33971300Sgjelinek 			(void) zonecfg_enddsent(t_handle);
33981300Sgjelinek 			return;
33991300Sgjelinek 		}
34001300Sgjelinek 
34011300Sgjelinek 		while (zonecfg_getdsent(s_handle, &s_dstab) == Z_OK) {
34021300Sgjelinek 			if (strcmp(t_dstab.zone_dataset_name,
34031300Sgjelinek 			    s_dstab.zone_dataset_name) == 0) {
34042712Snn35248 				target_zone = source;
34052712Snn35248 				zerror(gettext("WARNING: dataset '%s' "
34061300Sgjelinek 				    "is configured in both zones.\n"),
34071300Sgjelinek 				    t_dstab.zone_dataset_name);
34081300Sgjelinek 				break;
34091300Sgjelinek 			}
34101300Sgjelinek 		}
34111300Sgjelinek 		(void) zonecfg_enddsent(s_handle);
34121300Sgjelinek 	}
34131300Sgjelinek 
34141300Sgjelinek 	(void) zonecfg_enddsent(t_handle);
34151300Sgjelinek }
34161300Sgjelinek 
34172712Snn35248 /*
34182712Snn35248  * Check that the clone and its source have the same brand type.
34192712Snn35248  */
34202712Snn35248 static int
34212712Snn35248 valid_brand_clone(char *source_zone, char *target_zone)
34222712Snn35248 {
34232727Sedp 	brand_handle_t bh;
34242712Snn35248 	char source_brand[MAXNAMELEN];
34252712Snn35248 
34262712Snn35248 	if ((zone_get_brand(source_zone, source_brand,
34272712Snn35248 	    sizeof (source_brand))) != Z_OK) {
34282712Snn35248 		(void) fprintf(stderr, "%s: zone '%s': %s\n",
34292712Snn35248 		    execname, source_zone, gettext("missing or invalid brand"));
34302712Snn35248 		return (Z_ERR);
34312712Snn35248 	}
34322712Snn35248 
34332712Snn35248 	if (strcmp(source_brand, target_brand) != NULL) {
34342712Snn35248 		(void) fprintf(stderr,
34352712Snn35248 		    gettext("%s: Zones '%s' and '%s' have different brand "
34362712Snn35248 		    "types.\n"), execname, source_zone, target_zone);
34372712Snn35248 		return (Z_ERR);
34382712Snn35248 	}
34392712Snn35248 
34402727Sedp 	if ((bh = brand_open(target_brand)) == NULL) {
34412712Snn35248 		zerror(gettext("missing or invalid brand"));
34422712Snn35248 		return (Z_ERR);
34432712Snn35248 	}
34442727Sedp 	brand_close(bh);
34452712Snn35248 	return (Z_OK);
34462712Snn35248 }
34472712Snn35248 
34481300Sgjelinek static int
34491300Sgjelinek validate_clone(char *source_zone, char *target_zone)
34501300Sgjelinek {
34511300Sgjelinek 	int err = Z_OK;
34521300Sgjelinek 	zone_dochandle_t s_handle;
34531300Sgjelinek 	zone_dochandle_t t_handle;
34541300Sgjelinek 
34551300Sgjelinek 	if ((t_handle = zonecfg_init_handle()) == NULL) {
34561300Sgjelinek 		zperror(cmd_to_str(CMD_CLONE), B_TRUE);
34571300Sgjelinek 		return (Z_ERR);
34581300Sgjelinek 	}
34591300Sgjelinek 	if ((err = zonecfg_get_handle(target_zone, t_handle)) != Z_OK) {
34601300Sgjelinek 		errno = err;
34611300Sgjelinek 		zperror(cmd_to_str(CMD_CLONE), B_TRUE);
34621300Sgjelinek 		zonecfg_fini_handle(t_handle);
34631300Sgjelinek 		return (Z_ERR);
34641300Sgjelinek 	}
34651300Sgjelinek 
34661300Sgjelinek 	if ((s_handle = zonecfg_init_handle()) == NULL) {
34671300Sgjelinek 		zperror(cmd_to_str(CMD_CLONE), B_TRUE);
34681300Sgjelinek 		zonecfg_fini_handle(t_handle);
34691300Sgjelinek 		return (Z_ERR);
34701300Sgjelinek 	}
34711300Sgjelinek 	if ((err = zonecfg_get_handle(source_zone, s_handle)) != Z_OK) {
34721300Sgjelinek 		errno = err;
34731300Sgjelinek 		zperror(cmd_to_str(CMD_CLONE), B_TRUE);
34741300Sgjelinek 		goto done;
34751300Sgjelinek 	}
34761300Sgjelinek 
34772712Snn35248 	/* verify new zone has same brand type */
34782712Snn35248 	err = valid_brand_clone(source_zone, target_zone);
34792712Snn35248 	if (err != Z_OK)
34802712Snn35248 		goto done;
34812712Snn35248 
34821300Sgjelinek 	/* verify new zone has same inherit-pkg-dirs */
34831300Sgjelinek 	err = valid_ipd_clone(s_handle, source_zone, t_handle, target_zone);
34841300Sgjelinek 
34851300Sgjelinek 	/* warn about imported fs's which are the same */
34861300Sgjelinek 	warn_fs_match(s_handle, source_zone, t_handle, target_zone);
34871300Sgjelinek 
34881300Sgjelinek 	/* warn about imported IP addresses which are the same */
34891300Sgjelinek 	warn_ip_match(s_handle, source_zone, t_handle, target_zone);
34901300Sgjelinek 
34911300Sgjelinek 	/* warn about imported devices which are the same */
34921300Sgjelinek 	warn_dev_match(s_handle, source_zone, t_handle, target_zone);
34931300Sgjelinek 
34941300Sgjelinek 	/* warn about imported datasets which are the same */
34951300Sgjelinek 	warn_dataset_match(s_handle, source_zone, t_handle, target_zone);
34961300Sgjelinek 
34971300Sgjelinek done:
34981300Sgjelinek 	zonecfg_fini_handle(t_handle);
34991300Sgjelinek 	zonecfg_fini_handle(s_handle);
35001300Sgjelinek 
35011300Sgjelinek 	return ((err == Z_OK) ? Z_OK : Z_ERR);
35021300Sgjelinek }
35031300Sgjelinek 
35041300Sgjelinek static int
35051300Sgjelinek copy_zone(char *src, char *dst)
35061300Sgjelinek {
35071300Sgjelinek 	boolean_t out_null = B_FALSE;
35081300Sgjelinek 	int status;
35091300Sgjelinek 	char *outfile;
35101300Sgjelinek 	char cmdbuf[MAXPATHLEN * 2 + 128];
35111300Sgjelinek 
35121300Sgjelinek 	if ((outfile = tempnam("/var/log", "zone")) == NULL) {
35131300Sgjelinek 		outfile = "/dev/null";
35141300Sgjelinek 		out_null = B_TRUE;
35151300Sgjelinek 	}
35161300Sgjelinek 
35171867Sgjelinek 	/*
35181867Sgjelinek 	 * Use find to get the list of files to copy.  We need to skip
35191867Sgjelinek 	 * files of type "socket" since cpio can't handle those but that
35201867Sgjelinek 	 * should be ok since the app will recreate the socket when it runs.
35211867Sgjelinek 	 * We also need to filter out anything under the .zfs subdir.  Since
35221867Sgjelinek 	 * find is running depth-first, we need the extra egrep to filter .zfs.
35231867Sgjelinek 	 */
35241300Sgjelinek 	(void) snprintf(cmdbuf, sizeof (cmdbuf),
35251867Sgjelinek 	    "cd %s && /usr/bin/find . -type s -prune -o -depth -print | "
35261607Sgjelinek 	    "/usr/bin/egrep -v '^\\./\\.zfs$|^\\./\\.zfs/' | "
35271300Sgjelinek 	    "/usr/bin/cpio -pdmuP@ %s > %s 2>&1",
35281300Sgjelinek 	    src, dst, outfile);
35291300Sgjelinek 
35301300Sgjelinek 	status = do_subproc(cmdbuf);
35311300Sgjelinek 
35322712Snn35248 	if (subproc_status("copy", status, B_TRUE) != ZONE_SUBPROC_OK) {
35331300Sgjelinek 		if (!out_null)
35341300Sgjelinek 			(void) fprintf(stderr, gettext("\nThe copy failed.\n"
35351300Sgjelinek 			    "More information can be found in %s\n"), outfile);
35362712Snn35248 		return (Z_ERR);
35371300Sgjelinek 	}
35381300Sgjelinek 
35391300Sgjelinek 	if (!out_null)
35401300Sgjelinek 		(void) unlink(outfile);
35411300Sgjelinek 
35421300Sgjelinek 	return (Z_OK);
35431300Sgjelinek }
35441300Sgjelinek 
35451300Sgjelinek /* ARGSUSED */
35461867Sgjelinek static int
35471300Sgjelinek zfm_print(const char *p, void *r) {
35481300Sgjelinek 	zerror("  %s\n", p);
35491300Sgjelinek 	return (0);
35501300Sgjelinek }
35511300Sgjelinek 
35521867Sgjelinek int
35531867Sgjelinek clone_copy(char *source_zonepath, char *zonepath)
35541867Sgjelinek {
35551867Sgjelinek 	int err;
35561867Sgjelinek 
35571867Sgjelinek 	/* Don't clone the zone if anything is still mounted there */
35581867Sgjelinek 	if (zonecfg_find_mounts(source_zonepath, NULL, NULL)) {
35591867Sgjelinek 		zerror(gettext("These file systems are mounted on "
35601867Sgjelinek 		    "subdirectories of %s.\n"), source_zonepath);
35611867Sgjelinek 		(void) zonecfg_find_mounts(source_zonepath, zfm_print, NULL);
35621867Sgjelinek 		return (Z_ERR);
35631867Sgjelinek 	}
35641867Sgjelinek 
35651867Sgjelinek 	/*
35661867Sgjelinek 	 * Attempt to create a ZFS fs for the zonepath.  As usual, we don't
35671867Sgjelinek 	 * care if this works or not since we always have the default behavior
35681867Sgjelinek 	 * of a simple directory for the zonepath.
35691867Sgjelinek 	 */
35701867Sgjelinek 	create_zfs_zonepath(zonepath);
35711867Sgjelinek 
35721867Sgjelinek 	(void) printf(gettext("Copying %s..."), source_zonepath);
35731867Sgjelinek 	(void) fflush(stdout);
35741867Sgjelinek 
35751867Sgjelinek 	err = copy_zone(source_zonepath, zonepath);
35761867Sgjelinek 
35771867Sgjelinek 	(void) printf("\n");
35781867Sgjelinek 
35791867Sgjelinek 	return (err);
35801867Sgjelinek }
35811867Sgjelinek 
35821300Sgjelinek static int
35831300Sgjelinek clone_func(int argc, char *argv[])
35841300Sgjelinek {
35851300Sgjelinek 	char *source_zone = NULL;
35861300Sgjelinek 	int lockfd;
35871300Sgjelinek 	int err, arg;
35881300Sgjelinek 	char zonepath[MAXPATHLEN];
35891300Sgjelinek 	char source_zonepath[MAXPATHLEN];
35901300Sgjelinek 	zone_state_t state;
35911300Sgjelinek 	zone_entry_t *zent;
35921867Sgjelinek 	char *method = NULL;
35931867Sgjelinek 	char *snapshot = NULL;
35947089Sgjelinek 	char cmdbuf[MAXPATHLEN];
35957089Sgjelinek 	char postcmdbuf[MAXPATHLEN];
35967089Sgjelinek 	char presnapbuf[MAXPATHLEN];
35977089Sgjelinek 	char postsnapbuf[MAXPATHLEN];
35987089Sgjelinek 	char validsnapbuf[MAXPATHLEN];
35997089Sgjelinek 	brand_handle_t bh = NULL;
36007089Sgjelinek 	int status;
36017089Sgjelinek 	boolean_t brand_help = B_FALSE;
36021300Sgjelinek 
36031300Sgjelinek 	if (zonecfg_in_alt_root()) {
36041300Sgjelinek 		zerror(gettext("cannot clone zone in alternate root"));
36051300Sgjelinek 		return (Z_ERR);
36061300Sgjelinek 	}
36071300Sgjelinek 
36087089Sgjelinek 	/* Check the argv string for args we handle internally */
36091300Sgjelinek 	optind = 0;
36107089Sgjelinek 	opterr = 0;
36117089Sgjelinek 	while ((arg = getopt(argc, argv, "?m:s:")) != EOF) {
36121300Sgjelinek 		switch (arg) {
36131300Sgjelinek 		case '?':
36147089Sgjelinek 			if (optopt == '?') {
36157089Sgjelinek 				sub_usage(SHELP_CLONE, CMD_CLONE);
36167089Sgjelinek 				brand_help = B_TRUE;
36177089Sgjelinek 			}
36187089Sgjelinek 			/* Ignore unknown options - may be brand specific. */
36197089Sgjelinek 			break;
36201300Sgjelinek 		case 'm':
36211300Sgjelinek 			method = optarg;
36221300Sgjelinek 			break;
36231867Sgjelinek 		case 's':
36241867Sgjelinek 			snapshot = optarg;
36251867Sgjelinek 			break;
36261300Sgjelinek 		default:
36277089Sgjelinek 			/* Ignore unknown options - may be brand specific. */
36287089Sgjelinek 			break;
36291300Sgjelinek 		}
36301300Sgjelinek 	}
36317089Sgjelinek 
36327089Sgjelinek 	if (argc != (optind + 1)) {
36331300Sgjelinek 		sub_usage(SHELP_CLONE, CMD_CLONE);
36341300Sgjelinek 		return (Z_USAGE);
36351300Sgjelinek 	}
36367089Sgjelinek 
36371300Sgjelinek 	source_zone = argv[optind];
36387089Sgjelinek 
36397089Sgjelinek 	if (!brand_help) {
36407089Sgjelinek 		if (sanity_check(target_zone, CMD_CLONE, B_FALSE, B_TRUE,
36417089Sgjelinek 		    B_FALSE) != Z_OK)
36427089Sgjelinek 			return (Z_ERR);
36437089Sgjelinek 		if (verify_details(CMD_CLONE, argv) != Z_OK)
36447089Sgjelinek 			return (Z_ERR);
36457089Sgjelinek 
36467089Sgjelinek 		/*
36477089Sgjelinek 		 * We also need to do some extra validation on the source zone.
36487089Sgjelinek 		 */
36497089Sgjelinek 		if (strcmp(source_zone, GLOBAL_ZONENAME) == 0) {
36507089Sgjelinek 			zerror(gettext("%s operation is invalid for the "
36517089Sgjelinek 			    "global zone."), cmd_to_str(CMD_CLONE));
36527089Sgjelinek 			return (Z_ERR);
36537089Sgjelinek 		}
36547089Sgjelinek 
36557089Sgjelinek 		if (strncmp(source_zone, "SUNW", 4) == 0) {
36567089Sgjelinek 			zerror(gettext("%s operation is invalid for zones "
36577089Sgjelinek 			    "starting with SUNW."), cmd_to_str(CMD_CLONE));
36587089Sgjelinek 			return (Z_ERR);
36597089Sgjelinek 		}
36607089Sgjelinek 
36617089Sgjelinek 		zent = lookup_running_zone(source_zone);
36627089Sgjelinek 		if (zent != NULL) {
36637089Sgjelinek 			/* check whether the zone is ready or running */
36647089Sgjelinek 			if ((err = zone_get_state(zent->zname,
36657089Sgjelinek 			    &zent->zstate_num)) != Z_OK) {
36667089Sgjelinek 				errno = err;
36677089Sgjelinek 				zperror2(zent->zname, gettext("could not get "
36687089Sgjelinek 				    "state"));
36697089Sgjelinek 				/* can't tell, so hedge */
36707089Sgjelinek 				zent->zstate_str = "ready/running";
36717089Sgjelinek 			} else {
36727089Sgjelinek 				zent->zstate_str =
36737089Sgjelinek 				    zone_state_str(zent->zstate_num);
36747089Sgjelinek 			}
36757089Sgjelinek 			zerror(gettext("%s operation is invalid for %s zones."),
36767089Sgjelinek 			    cmd_to_str(CMD_CLONE), zent->zstate_str);
36777089Sgjelinek 			return (Z_ERR);
36787089Sgjelinek 		}
36797089Sgjelinek 
36807089Sgjelinek 		if ((err = zone_get_state(source_zone, &state)) != Z_OK) {
36811300Sgjelinek 			errno = err;
36827089Sgjelinek 			zperror2(source_zone, gettext("could not get state"));
36837089Sgjelinek 			return (Z_ERR);
36847089Sgjelinek 		}
36857089Sgjelinek 		if (state != ZONE_STATE_INSTALLED) {
36867089Sgjelinek 			(void) fprintf(stderr,
36877089Sgjelinek 			    gettext("%s: zone %s is %s; %s is required.\n"),
36887089Sgjelinek 			    execname, source_zone, zone_state_str(state),
36897089Sgjelinek 			    zone_state_str(ZONE_STATE_INSTALLED));
36907089Sgjelinek 			return (Z_ERR);
36911300Sgjelinek 		}
36927089Sgjelinek 
36937089Sgjelinek 		/*
36947089Sgjelinek 		 * The source zone checks out ok, continue with the clone.
36957089Sgjelinek 		 */
36967089Sgjelinek 
36977089Sgjelinek 		if (validate_clone(source_zone, target_zone) != Z_OK)
36987089Sgjelinek 			return (Z_ERR);
36997089Sgjelinek 
37007089Sgjelinek 		if (zonecfg_grab_lock_file(target_zone, &lockfd) != Z_OK) {
37017089Sgjelinek 			zerror(gettext("another %s may have an operation in "
37027089Sgjelinek 			    "progress."), "zoneadm");
37037089Sgjelinek 			return (Z_ERR);
37047089Sgjelinek 		}
37051300Sgjelinek 	}
37061300Sgjelinek 
37071300Sgjelinek 	if ((err = zone_get_zonepath(source_zone, source_zonepath,
37081300Sgjelinek 	    sizeof (source_zonepath))) != Z_OK) {
37091300Sgjelinek 		errno = err;
37101300Sgjelinek 		zperror2(source_zone, gettext("could not get zone path"));
37111300Sgjelinek 		goto done;
37121300Sgjelinek 	}
37131300Sgjelinek 
37141300Sgjelinek 	if ((err = zone_get_zonepath(target_zone, zonepath, sizeof (zonepath)))
37151300Sgjelinek 	    != Z_OK) {
37161300Sgjelinek 		errno = err;
37171300Sgjelinek 		zperror2(target_zone, gettext("could not get zone path"));
37181300Sgjelinek 		goto done;
37191300Sgjelinek 	}
37201300Sgjelinek 
37217089Sgjelinek 	/*
37227089Sgjelinek 	 * Fetch the clone and postclone hooks from the brand configuration.
37237089Sgjelinek 	 */
37247089Sgjelinek 	if ((bh = brand_open(target_brand)) == NULL) {
37257089Sgjelinek 		zerror(gettext("missing or invalid brand"));
37267089Sgjelinek 		err = Z_ERR;
37277089Sgjelinek 		goto done;
37287089Sgjelinek 	}
37297089Sgjelinek 
37307089Sgjelinek 	if (get_hook(bh, cmdbuf, sizeof (cmdbuf), brand_get_clone, target_zone,
37317089Sgjelinek 	    zonepath) != Z_OK) {
37327089Sgjelinek 		zerror("invalid brand configuration: missing clone resource");
37337089Sgjelinek 		brand_close(bh);
37347089Sgjelinek 		err = Z_ERR;
37357089Sgjelinek 		goto done;
37367089Sgjelinek 	}
37377089Sgjelinek 
37387089Sgjelinek 	if (get_hook(bh, postcmdbuf, sizeof (postcmdbuf), brand_get_postclone,
37397089Sgjelinek 	    target_zone, zonepath) != Z_OK) {
37407089Sgjelinek 		zerror("invalid brand configuration: missing postclone "
37417089Sgjelinek 		    "resource");
37427089Sgjelinek 		brand_close(bh);
37437089Sgjelinek 		err = Z_ERR;
37447089Sgjelinek 		goto done;
37457089Sgjelinek 	}
37467089Sgjelinek 
37477089Sgjelinek 	if (get_hook(bh, presnapbuf, sizeof (presnapbuf), brand_get_presnap,
37487089Sgjelinek 	    source_zone, source_zonepath) != Z_OK) {
37497089Sgjelinek 		zerror("invalid brand configuration: missing presnap "
37507089Sgjelinek 		    "resource");
37517089Sgjelinek 		brand_close(bh);
37527089Sgjelinek 		err = Z_ERR;
37531300Sgjelinek 		goto done;
37541300Sgjelinek 	}
37551300Sgjelinek 
37567089Sgjelinek 	if (get_hook(bh, postsnapbuf, sizeof (postsnapbuf), brand_get_postsnap,
37577089Sgjelinek 	    source_zone, source_zonepath) != Z_OK) {
37587089Sgjelinek 		zerror("invalid brand configuration: missing postsnap "
37597089Sgjelinek 		    "resource");
37607089Sgjelinek 		brand_close(bh);
37617089Sgjelinek 		err = Z_ERR;
37627089Sgjelinek 		goto done;
37637089Sgjelinek 	}
37647089Sgjelinek 
37657089Sgjelinek 	if (get_hook(bh, validsnapbuf, sizeof (validsnapbuf),
37667089Sgjelinek 	    brand_get_validatesnap, target_zone, zonepath) != Z_OK) {
37677089Sgjelinek 		zerror("invalid brand configuration: missing validatesnap "
37687089Sgjelinek 		    "resource");
37697089Sgjelinek 		brand_close(bh);
37701867Sgjelinek 		err = Z_ERR;
37717089Sgjelinek 		goto done;
37727089Sgjelinek 	}
37737089Sgjelinek 	brand_close(bh);
37747089Sgjelinek 
37757089Sgjelinek 	/* Append all options to clone hook. */
37767089Sgjelinek 	if (addoptions(cmdbuf, argv, sizeof (cmdbuf)) != Z_OK) {
37777089Sgjelinek 		err = Z_ERR;
37787089Sgjelinek 		goto done;
37797089Sgjelinek 	}
37807089Sgjelinek 
37817089Sgjelinek 	/* Append all options to postclone hook. */
37827089Sgjelinek 	if (addoptions(postcmdbuf, argv, sizeof (postcmdbuf)) != Z_OK) {
37837089Sgjelinek 		err = Z_ERR;
37847089Sgjelinek 		goto done;
37857089Sgjelinek 	}
37867089Sgjelinek 
37877089Sgjelinek 	if (!brand_help) {
37887089Sgjelinek 		if ((err = zone_set_state(target_zone, ZONE_STATE_INCOMPLETE))
37897089Sgjelinek 		    != Z_OK) {
37907089Sgjelinek 			errno = err;
37917089Sgjelinek 			zperror2(target_zone, gettext("could not set state"));
37927089Sgjelinek 			goto done;
37937089Sgjelinek 		}
37941867Sgjelinek 	}
37951867Sgjelinek 
37962712Snn35248 	/*
37977089Sgjelinek 	 * The clone hook is optional.  If it exists, use the hook for
37987089Sgjelinek 	 * cloning, otherwise use the built-in clone support
37992712Snn35248 	 */
38007089Sgjelinek 	if (cmdbuf[0] != '\0') {
38017089Sgjelinek 		/* Run the clone hook */
38027089Sgjelinek 		status = do_subproc_interactive(cmdbuf);
38037089Sgjelinek 		if ((status = subproc_status(gettext("brand-specific clone"),
38047089Sgjelinek 		    status, B_FALSE)) != ZONE_SUBPROC_OK) {
38057089Sgjelinek 			if (status == ZONE_SUBPROC_USAGE && !brand_help)
38067089Sgjelinek 				sub_usage(SHELP_CLONE, CMD_CLONE);
38077089Sgjelinek 			err = Z_ERR;
38087089Sgjelinek 			goto done;
38097089Sgjelinek 		}
38107089Sgjelinek 
38117089Sgjelinek 		if (brand_help)
38127089Sgjelinek 			return (Z_OK);
38137089Sgjelinek 
38147089Sgjelinek 	} else {
38157089Sgjelinek 		/* If just help, we're done since there is no brand help. */
38167089Sgjelinek 		if (brand_help)
38177089Sgjelinek 			return (Z_OK);
38187089Sgjelinek 
38197089Sgjelinek 		/* Run the built-in clone support. */
38207089Sgjelinek 
38217089Sgjelinek 		/* The only explicit built-in method is "copy". */
38227089Sgjelinek 		if (method != NULL && strcmp(method, "copy") != 0) {
38237089Sgjelinek 			sub_usage(SHELP_CLONE, CMD_CLONE);
38247089Sgjelinek 			err = Z_USAGE;
38257089Sgjelinek 			goto done;
38267089Sgjelinek 		}
38277089Sgjelinek 
38287089Sgjelinek 		if (snapshot != NULL) {
38297089Sgjelinek 			err = clone_snapshot_zfs(snapshot, zonepath,
38307089Sgjelinek 			    validsnapbuf);
38317089Sgjelinek 		} else {
38327089Sgjelinek 			/*
38337089Sgjelinek 			 * We always copy the clone unless the source is ZFS
38347089Sgjelinek 			 * and a ZFS clone worked.  We fallback to copying if
38357089Sgjelinek 			 * the ZFS clone fails for some reason.
38367089Sgjelinek 			 */
38377089Sgjelinek 			err = Z_ERR;
38387089Sgjelinek 			if (method == NULL && is_zonepath_zfs(source_zonepath))
38397089Sgjelinek 				err = clone_zfs(source_zonepath, zonepath,
38407089Sgjelinek 				    presnapbuf, postsnapbuf);
38417089Sgjelinek 
38427089Sgjelinek 			if (err != Z_OK)
38437089Sgjelinek 				err = clone_copy(source_zonepath, zonepath);
38447089Sgjelinek 		}
38457089Sgjelinek 	}
38467089Sgjelinek 
38477089Sgjelinek 	if (err == Z_OK && postcmdbuf[0] != '\0') {
38487089Sgjelinek 		status = do_subproc(postcmdbuf);
38497089Sgjelinek 		if ((err = subproc_status("postclone", status, B_FALSE))
38507089Sgjelinek 		    != ZONE_SUBPROC_OK) {
38517089Sgjelinek 			zerror(gettext("post-clone configuration failed."));
38527089Sgjelinek 			err = Z_ERR;
38537089Sgjelinek 		}
38547089Sgjelinek 	}
38551300Sgjelinek 
38561300Sgjelinek done:
38572712Snn35248 	/*
38582712Snn35248 	 * If everything went well, we mark the zone as installed.
38592712Snn35248 	 */
38602712Snn35248 	if (err == Z_OK) {
38612712Snn35248 		err = zone_set_state(target_zone, ZONE_STATE_INSTALLED);
38622712Snn35248 		if (err != Z_OK) {
38632712Snn35248 			errno = err;
38642712Snn35248 			zperror2(target_zone, gettext("could not set state"));
38652712Snn35248 		}
38662712Snn35248 	}
38677089Sgjelinek 	if (!brand_help)
38687089Sgjelinek 		zonecfg_release_lock_file(target_zone, lockfd);
38691300Sgjelinek 	return ((err == Z_OK) ? Z_OK : Z_ERR);
38701300Sgjelinek }
38711300Sgjelinek 
38721607Sgjelinek /*
38731867Sgjelinek  * Used when removing a zonepath after uninstalling or cleaning up after
38741867Sgjelinek  * the move subcommand.  This handles a zonepath that has non-standard
38751867Sgjelinek  * contents so that we will only cleanup the stuff we know about and leave
38761867Sgjelinek  * any user data alone.
38771607Sgjelinek  *
38781867Sgjelinek  * If the "all" parameter is true then we should remove the whole zonepath
38791867Sgjelinek  * even if it has non-standard files/directories in it.  This can be used when
38801867Sgjelinek  * we need to cleanup after moving the zonepath across file systems.
38811867Sgjelinek  *
38821867Sgjelinek  * We "exec" the RMCOMMAND so that the returned status is that of RMCOMMAND
38831867Sgjelinek  * and not the shell.
38841607Sgjelinek  */
38851607Sgjelinek static int
38861867Sgjelinek cleanup_zonepath(char *zonepath, boolean_t all)
38871607Sgjelinek {
38881867Sgjelinek 	int		status;
38891867Sgjelinek 	int		i;
38901867Sgjelinek 	boolean_t	non_std = B_FALSE;
38911867Sgjelinek 	struct dirent	*dp;
38921867Sgjelinek 	DIR		*dirp;
38933686Sgjelinek 			/*
38943686Sgjelinek 			 * The SUNWattached.xml file is expected since it might
38953686Sgjelinek 			 * exist if the zone was force-attached after a
38963686Sgjelinek 			 * migration.
38973686Sgjelinek 			 */
38983686Sgjelinek 	char		*std_entries[] = {"dev", "lu", "root",
38993686Sgjelinek 			    "SUNWattached.xml", NULL};
39001867Sgjelinek 			/* (MAXPATHLEN * 3) is for the 3 std_entries dirs */
39011867Sgjelinek 	char		cmdbuf[sizeof (RMCOMMAND) + (MAXPATHLEN * 3) + 64];
39021867Sgjelinek 
39031867Sgjelinek 	/*
39041867Sgjelinek 	 * We shouldn't need these checks but lets be paranoid since we
39051867Sgjelinek 	 * could blow away the whole system here if we got the wrong zonepath.
39061867Sgjelinek 	 */
39071867Sgjelinek 	if (*zonepath == NULL || strcmp(zonepath, "/") == 0) {
39081867Sgjelinek 		(void) fprintf(stderr, "invalid zonepath '%s'\n", zonepath);
39091867Sgjelinek 		return (Z_INVAL);
39101867Sgjelinek 	}
39111867Sgjelinek 
39121867Sgjelinek 	/*
39131867Sgjelinek 	 * If the dirpath is already gone (maybe it was manually removed) then
39141867Sgjelinek 	 * we just return Z_OK so that the cleanup is successful.
39151867Sgjelinek 	 */
39161867Sgjelinek 	if ((dirp = opendir(zonepath)) == NULL)
39171867Sgjelinek 		return (Z_OK);
39181867Sgjelinek 
39191867Sgjelinek 	/*
39201867Sgjelinek 	 * Look through the zonepath directory to see if there are any
39211867Sgjelinek 	 * non-standard files/dirs.  Also skip .zfs since that might be
39221867Sgjelinek 	 * there but we'll handle ZFS file systems as a special case.
39231867Sgjelinek 	 */
39241867Sgjelinek 	while ((dp = readdir(dirp)) != NULL) {
39251867Sgjelinek 		if (strcmp(dp->d_name, ".") == 0 ||
39261867Sgjelinek 		    strcmp(dp->d_name, "..") == 0 ||
39271867Sgjelinek 		    strcmp(dp->d_name, ".zfs") == 0)
39281867Sgjelinek 			continue;
39291867Sgjelinek 
39301867Sgjelinek 		for (i = 0; std_entries[i] != NULL; i++)
39311867Sgjelinek 			if (strcmp(dp->d_name, std_entries[i]) == 0)
39321867Sgjelinek 				break;
39331867Sgjelinek 
39341867Sgjelinek 		if (std_entries[i] == NULL)
39351867Sgjelinek 			non_std = B_TRUE;
39361867Sgjelinek 	}
39371867Sgjelinek 	(void) closedir(dirp);
39381867Sgjelinek 
39391867Sgjelinek 	if (!all && non_std) {
39401607Sgjelinek 		/*
39411867Sgjelinek 		 * There are extra, non-standard directories/files in the
39421867Sgjelinek 		 * zonepath so we don't want to remove the zonepath.  We
39431867Sgjelinek 		 * just want to remove the standard directories and leave
39441867Sgjelinek 		 * the user data alone.
39451607Sgjelinek 		 */
39461867Sgjelinek 		(void) snprintf(cmdbuf, sizeof (cmdbuf), "exec " RMCOMMAND);
39471867Sgjelinek 
39481867Sgjelinek 		for (i = 0; std_entries[i] != NULL; i++) {
39491867Sgjelinek 			char tmpbuf[MAXPATHLEN];
39501867Sgjelinek 
39511867Sgjelinek 			if (snprintf(tmpbuf, sizeof (tmpbuf), " %s/%s",
39521867Sgjelinek 			    zonepath, std_entries[i]) >= sizeof (tmpbuf) ||
39531867Sgjelinek 			    strlcat(cmdbuf, tmpbuf, sizeof (cmdbuf)) >=
39541867Sgjelinek 			    sizeof (cmdbuf)) {
39551867Sgjelinek 				(void) fprintf(stderr,
39561867Sgjelinek 				    gettext("path is too long\n"));
39571867Sgjelinek 				return (Z_INVAL);
39581867Sgjelinek 			}
39591867Sgjelinek 		}
39601867Sgjelinek 
39611867Sgjelinek 		status = do_subproc(cmdbuf);
39621867Sgjelinek 
39631867Sgjelinek 		(void) fprintf(stderr, gettext("WARNING: Unable to completely "
39641867Sgjelinek 		    "remove %s\nbecause it contains additional user data.  "
39651867Sgjelinek 		    "Only the standard directory\nentries have been "
39661867Sgjelinek 		    "removed.\n"),
39671867Sgjelinek 		    zonepath);
39681867Sgjelinek 
39692712Snn35248 		return ((subproc_status(RMCOMMAND, status, B_TRUE) ==
39702712Snn35248 		    ZONE_SUBPROC_OK) ? Z_OK : Z_ERR);
39711607Sgjelinek 	}
39721607Sgjelinek 
39731867Sgjelinek 	/*
39741867Sgjelinek 	 * There is nothing unexpected in the zonepath, try to get rid of the
39751867Sgjelinek 	 * whole zonepath directory.
39761867Sgjelinek 	 *
39771867Sgjelinek 	 * If the zonepath is its own zfs file system, try to destroy the
39781867Sgjelinek 	 * file system.  If that fails for some reason (e.g. it has clones)
39791867Sgjelinek 	 * then we'll just remove the contents of the zonepath.
39801867Sgjelinek 	 */
39811867Sgjelinek 	if (is_zonepath_zfs(zonepath)) {
39821867Sgjelinek 		if (destroy_zfs(zonepath) == Z_OK)
39831867Sgjelinek 			return (Z_OK);
39841867Sgjelinek 		(void) snprintf(cmdbuf, sizeof (cmdbuf), "exec " RMCOMMAND
39851867Sgjelinek 		    " %s/*", zonepath);
39861867Sgjelinek 		status = do_subproc(cmdbuf);
39872712Snn35248 		return ((subproc_status(RMCOMMAND, status, B_TRUE) ==
39882712Snn35248 		    ZONE_SUBPROC_OK) ? Z_OK : Z_ERR);
39891867Sgjelinek 	}
39901867Sgjelinek 
39911867Sgjelinek 	(void) snprintf(cmdbuf, sizeof (cmdbuf), "exec " RMCOMMAND " %s",
39921867Sgjelinek 	    zonepath);
39931607Sgjelinek 	status = do_subproc(cmdbuf);
39942712Snn35248 
39952712Snn35248 	return ((subproc_status(RMCOMMAND, status, B_TRUE) == ZONE_SUBPROC_OK)
39962712Snn35248 	    ? Z_OK : Z_ERR);
39971607Sgjelinek }
39981607Sgjelinek 
39991300Sgjelinek static int
40001300Sgjelinek move_func(int argc, char *argv[])
40011300Sgjelinek {
40021300Sgjelinek 	char *new_zonepath = NULL;
40031300Sgjelinek 	int lockfd;
40041300Sgjelinek 	int err, arg;
40051300Sgjelinek 	char zonepath[MAXPATHLEN];
40061300Sgjelinek 	zone_dochandle_t handle;
40071300Sgjelinek 	boolean_t fast;
40081867Sgjelinek 	boolean_t is_zfs = B_FALSE;
40091867Sgjelinek 	struct dirent *dp;
40101867Sgjelinek 	DIR *dirp;
40111867Sgjelinek 	boolean_t empty = B_TRUE;
40121300Sgjelinek 	boolean_t revert;
40131300Sgjelinek 	struct stat zonepath_buf;
40141300Sgjelinek 	struct stat new_zonepath_buf;
40151300Sgjelinek 
40161300Sgjelinek 	if (zonecfg_in_alt_root()) {
40171300Sgjelinek 		zerror(gettext("cannot move zone in alternate root"));
40181300Sgjelinek 		return (Z_ERR);
40191300Sgjelinek 	}
40201300Sgjelinek 
40211300Sgjelinek 	optind = 0;
40221300Sgjelinek 	if ((arg = getopt(argc, argv, "?")) != EOF) {
40231300Sgjelinek 		switch (arg) {
40241300Sgjelinek 		case '?':
40251300Sgjelinek 			sub_usage(SHELP_MOVE, CMD_MOVE);
40261300Sgjelinek 			return (optopt == '?' ? Z_OK : Z_USAGE);
40271300Sgjelinek 		default:
40281300Sgjelinek 			sub_usage(SHELP_MOVE, CMD_MOVE);
40291300Sgjelinek 			return (Z_USAGE);
40301300Sgjelinek 		}
40311300Sgjelinek 	}
40321300Sgjelinek 	if (argc != (optind + 1)) {
40331300Sgjelinek 		sub_usage(SHELP_MOVE, CMD_MOVE);
40341300Sgjelinek 		return (Z_USAGE);
40351300Sgjelinek 	}
40361300Sgjelinek 	new_zonepath = argv[optind];
40372712Snn35248 	if (sanity_check(target_zone, CMD_MOVE, B_FALSE, B_TRUE, B_FALSE)
40382712Snn35248 	    != Z_OK)
40391300Sgjelinek 		return (Z_ERR);
40403339Szt129084 	if (verify_details(CMD_MOVE, argv) != Z_OK)
40411300Sgjelinek 		return (Z_ERR);
40421300Sgjelinek 
40431300Sgjelinek 	/*
40441300Sgjelinek 	 * Check out the new zonepath.  This has the side effect of creating
40451300Sgjelinek 	 * a directory for the new zonepath.  We depend on this later when we
40461867Sgjelinek 	 * stat to see if we are doing a cross file system move or not.
40471300Sgjelinek 	 */
40481300Sgjelinek 	if (validate_zonepath(new_zonepath, CMD_MOVE) != Z_OK)
40491300Sgjelinek 		return (Z_ERR);
40501300Sgjelinek 
40511300Sgjelinek 	if ((err = zone_get_zonepath(target_zone, zonepath, sizeof (zonepath)))
40521300Sgjelinek 	    != Z_OK) {
40531300Sgjelinek 		errno = err;
40541300Sgjelinek 		zperror2(target_zone, gettext("could not get zone path"));
40551300Sgjelinek 		return (Z_ERR);
40561300Sgjelinek 	}
40571300Sgjelinek 
40581300Sgjelinek 	if (stat(zonepath, &zonepath_buf) == -1) {
40591300Sgjelinek 		zperror(gettext("could not stat zone path"), B_FALSE);
40601300Sgjelinek 		return (Z_ERR);
40611300Sgjelinek 	}
40621300Sgjelinek 
40631300Sgjelinek 	if (stat(new_zonepath, &new_zonepath_buf) == -1) {
40641300Sgjelinek 		zperror(gettext("could not stat new zone path"), B_FALSE);
40651300Sgjelinek 		return (Z_ERR);
40661300Sgjelinek 	}
40671300Sgjelinek 
40681867Sgjelinek 	/*
40691867Sgjelinek 	 * Check if the destination directory is empty.
40701867Sgjelinek 	 */
40711867Sgjelinek 	if ((dirp = opendir(new_zonepath)) == NULL) {
40721867Sgjelinek 		zperror(gettext("could not open new zone path"), B_FALSE);
40731867Sgjelinek 		return (Z_ERR);
40741867Sgjelinek 	}
40751867Sgjelinek 	while ((dp = readdir(dirp)) != (struct dirent *)0) {
40761867Sgjelinek 		if (strcmp(dp->d_name, ".") == 0 ||
40771867Sgjelinek 		    strcmp(dp->d_name, "..") == 0)
40781867Sgjelinek 			continue;
40791867Sgjelinek 		empty = B_FALSE;
40801867Sgjelinek 		break;
40811867Sgjelinek 	}
40821867Sgjelinek 	(void) closedir(dirp);
40831867Sgjelinek 
40841867Sgjelinek 	/* Error if there is anything in the destination directory. */
40851867Sgjelinek 	if (!empty) {
40861867Sgjelinek 		(void) fprintf(stderr, gettext("could not move zone to %s: "
40871867Sgjelinek 		    "directory not empty\n"), new_zonepath);
40881867Sgjelinek 		return (Z_ERR);
40891867Sgjelinek 	}
40901867Sgjelinek 
40911300Sgjelinek 	/* Don't move the zone if anything is still mounted there */
40921300Sgjelinek 	if (zonecfg_find_mounts(zonepath, NULL, NULL)) {
40931867Sgjelinek 		zerror(gettext("These file systems are mounted on "
40941300Sgjelinek 		    "subdirectories of %s.\n"), zonepath);
40951300Sgjelinek 		(void) zonecfg_find_mounts(zonepath, zfm_print, NULL);
40961300Sgjelinek 		return (Z_ERR);
40971300Sgjelinek 	}
40981300Sgjelinek 
40991300Sgjelinek 	/*
41001867Sgjelinek 	 * Check if we are moving in the same file system and can do a fast
41011867Sgjelinek 	 * move or if we are crossing file systems and have to copy the data.
41021300Sgjelinek 	 */
41031300Sgjelinek 	fast = (zonepath_buf.st_dev == new_zonepath_buf.st_dev);
41041300Sgjelinek 
41051300Sgjelinek 	if ((handle = zonecfg_init_handle()) == NULL) {
41061300Sgjelinek 		zperror(cmd_to_str(CMD_MOVE), B_TRUE);
41071300Sgjelinek 		return (Z_ERR);
41081300Sgjelinek 	}
41091300Sgjelinek 
41101300Sgjelinek 	if ((err = zonecfg_get_handle(target_zone, handle)) != Z_OK) {
41111300Sgjelinek 		errno = err;
41121300Sgjelinek 		zperror(cmd_to_str(CMD_MOVE), B_TRUE);
41131300Sgjelinek 		zonecfg_fini_handle(handle);
41141300Sgjelinek 		return (Z_ERR);
41151300Sgjelinek 	}
41161300Sgjelinek 
41177089Sgjelinek 	if (zonecfg_grab_lock_file(target_zone, &lockfd) != Z_OK) {
41181300Sgjelinek 		zerror(gettext("another %s may have an operation in progress."),
41191300Sgjelinek 		    "zoneadm");
41201300Sgjelinek 		zonecfg_fini_handle(handle);
41211300Sgjelinek 		return (Z_ERR);
41221300Sgjelinek 	}
41231300Sgjelinek 
41241300Sgjelinek 	/*
41251867Sgjelinek 	 * We're making some file system changes now so we have to clean up
41261867Sgjelinek 	 * the file system before we are done.  This will either clean up the
41271300Sgjelinek 	 * new zonepath if the zonecfg update failed or it will clean up the
41281300Sgjelinek 	 * old zonepath if everything is ok.
41291300Sgjelinek 	 */
41301300Sgjelinek 	revert = B_TRUE;
41311300Sgjelinek 
41321867Sgjelinek 	if (is_zonepath_zfs(zonepath) &&
41331867Sgjelinek 	    move_zfs(zonepath, new_zonepath) != Z_ERR) {
41341867Sgjelinek 		is_zfs = B_TRUE;
41351867Sgjelinek 
41361867Sgjelinek 	} else if (fast) {
41371867Sgjelinek 		/* same file system, use rename for a quick move */
41381300Sgjelinek 
41391300Sgjelinek 		/*
41401300Sgjelinek 		 * Remove the new_zonepath directory that got created above
41411300Sgjelinek 		 * during the validation.  It gets in the way of the rename.
41421300Sgjelinek 		 */
41431300Sgjelinek 		if (rmdir(new_zonepath) != 0) {
41441300Sgjelinek 			zperror(gettext("could not rmdir new zone path"),
41451300Sgjelinek 			    B_FALSE);
41461300Sgjelinek 			zonecfg_fini_handle(handle);
41477089Sgjelinek 			zonecfg_release_lock_file(target_zone, lockfd);
41481300Sgjelinek 			return (Z_ERR);
41491300Sgjelinek 		}
41501300Sgjelinek 
41511300Sgjelinek 		if (rename(zonepath, new_zonepath) != 0) {
41521300Sgjelinek 			/*
41531300Sgjelinek 			 * If this fails we don't need to do all of the
41541300Sgjelinek 			 * cleanup that happens for the rest of the code
41551300Sgjelinek 			 * so just return from this error.
41561300Sgjelinek 			 */
41571300Sgjelinek 			zperror(gettext("could not move zone"), B_FALSE);
41581300Sgjelinek 			zonecfg_fini_handle(handle);
41597089Sgjelinek 			zonecfg_release_lock_file(target_zone, lockfd);
41601300Sgjelinek 			return (Z_ERR);
41611300Sgjelinek 		}
41621300Sgjelinek 
41631300Sgjelinek 	} else {
41641867Sgjelinek 		/*
41651867Sgjelinek 		 * Attempt to create a ZFS fs for the new zonepath.  As usual,
41661867Sgjelinek 		 * we don't care if this works or not since we always have the
41671867Sgjelinek 		 * default behavior of a simple directory for the zonepath.
41681867Sgjelinek 		 */
41691867Sgjelinek 		create_zfs_zonepath(new_zonepath);
41701867Sgjelinek 
41711300Sgjelinek 		(void) printf(gettext(
41721867Sgjelinek 		    "Moving across file systems; copying zonepath %s..."),
41731300Sgjelinek 		    zonepath);
41741300Sgjelinek 		(void) fflush(stdout);
41751300Sgjelinek 
41761300Sgjelinek 		err = copy_zone(zonepath, new_zonepath);
41771300Sgjelinek 
41781300Sgjelinek 		(void) printf("\n");
41791300Sgjelinek 		if (err != Z_OK)
41801300Sgjelinek 			goto done;
41811300Sgjelinek 	}
41821300Sgjelinek 
41831300Sgjelinek 	if ((err = zonecfg_set_zonepath(handle, new_zonepath)) != Z_OK) {
41841300Sgjelinek 		errno = err;
41851300Sgjelinek 		zperror(gettext("could not set new zonepath"), B_TRUE);
41861300Sgjelinek 		goto done;
41871300Sgjelinek 	}
41881300Sgjelinek 
41891300Sgjelinek 	if ((err = zonecfg_save(handle)) != Z_OK) {
41901300Sgjelinek 		errno = err;
41911300Sgjelinek 		zperror(gettext("zonecfg save failed"), B_TRUE);
41921300Sgjelinek 		goto done;
41931300Sgjelinek 	}
41941300Sgjelinek 
41951300Sgjelinek 	revert = B_FALSE;
41961300Sgjelinek 
41971300Sgjelinek done:
41981300Sgjelinek 	zonecfg_fini_handle(handle);
41997089Sgjelinek 	zonecfg_release_lock_file(target_zone, lockfd);
42001300Sgjelinek 
42011300Sgjelinek 	/*
42021867Sgjelinek 	 * Clean up the file system based on how things went.  We either
42031300Sgjelinek 	 * clean up the new zonepath if the operation failed for some reason
42041300Sgjelinek 	 * or we clean up the old zonepath if everything is ok.
42051300Sgjelinek 	 */
42061300Sgjelinek 	if (revert) {
42071300Sgjelinek 		/* The zonecfg update failed, cleanup the new zonepath. */
42081867Sgjelinek 		if (is_zfs) {
42091867Sgjelinek 			if (move_zfs(new_zonepath, zonepath) == Z_ERR) {
42101867Sgjelinek 				(void) fprintf(stderr, gettext("could not "
42111867Sgjelinek 				    "restore zonepath, the zfs mountpoint is "
42121867Sgjelinek 				    "set as:\n%s\n"), new_zonepath);
42131867Sgjelinek 				/*
42141867Sgjelinek 				 * err is already != Z_OK since we're reverting
42151867Sgjelinek 				 */
42161867Sgjelinek 			}
42171867Sgjelinek 
42181867Sgjelinek 		} else if (fast) {
42191300Sgjelinek 			if (rename(new_zonepath, zonepath) != 0) {
42201300Sgjelinek 				zperror(gettext("could not restore zonepath"),
42211300Sgjelinek 				    B_FALSE);
42221300Sgjelinek 				/*
42231300Sgjelinek 				 * err is already != Z_OK since we're reverting
42241300Sgjelinek 				 */
42251300Sgjelinek 			}
42261300Sgjelinek 		} else {
42271300Sgjelinek 			(void) printf(gettext("Cleaning up zonepath %s..."),
42281300Sgjelinek 			    new_zonepath);
42291300Sgjelinek 			(void) fflush(stdout);
42301867Sgjelinek 			err = cleanup_zonepath(new_zonepath, B_TRUE);
42311300Sgjelinek 			(void) printf("\n");
42321300Sgjelinek 
42331607Sgjelinek 			if (err != Z_OK) {
42341300Sgjelinek 				errno = err;
42351300Sgjelinek 				zperror(gettext("could not remove new "
42361300Sgjelinek 				    "zonepath"), B_TRUE);
42371300Sgjelinek 			} else {
42381300Sgjelinek 				/*
42391300Sgjelinek 				 * Because we're reverting we know the mainline
42401300Sgjelinek 				 * code failed but we just reused the err
42411300Sgjelinek 				 * variable so we reset it back to Z_ERR.
42421300Sgjelinek 				 */
42431300Sgjelinek 				err = Z_ERR;
42441300Sgjelinek 			}
42451300Sgjelinek 		}
42461300Sgjelinek 
42471300Sgjelinek 	} else {
42481300Sgjelinek 		/* The move was successful, cleanup the old zonepath. */
42491867Sgjelinek 		if (!is_zfs && !fast) {
42501300Sgjelinek 			(void) printf(
42511300Sgjelinek 			    gettext("Cleaning up zonepath %s..."), zonepath);
42521300Sgjelinek 			(void) fflush(stdout);
42531867Sgjelinek 			err = cleanup_zonepath(zonepath, B_TRUE);
42541300Sgjelinek 			(void) printf("\n");
42551300Sgjelinek 
42561607Sgjelinek 			if (err != Z_OK) {
42571300Sgjelinek 				errno = err;
42581300Sgjelinek 				zperror(gettext("could not remove zonepath"),
42591300Sgjelinek 				    B_TRUE);
42601300Sgjelinek 			}
42611300Sgjelinek 		}
42621300Sgjelinek 	}
42631300Sgjelinek 
42641300Sgjelinek 	return ((err == Z_OK) ? Z_OK : Z_ERR);
42651300Sgjelinek }
42661300Sgjelinek 
42677089Sgjelinek /* ARGSUSED */
42681507Sgjelinek static int
42691507Sgjelinek detach_func(int argc, char *argv[])
42701507Sgjelinek {
42718301Sgerald.jelinek@sun.com 	int lockfd = -1;
42721507Sgjelinek 	int err, arg;
42731507Sgjelinek 	char zonepath[MAXPATHLEN];
42744785Sgjelinek 	char cmdbuf[MAXPATHLEN];
42757089Sgjelinek 	char precmdbuf[MAXPATHLEN];
42762078Sgjelinek 	boolean_t execute = B_TRUE;
42777089Sgjelinek 	boolean_t brand_help = B_FALSE;
42784785Sgjelinek 	brand_handle_t bh = NULL;
42797089Sgjelinek 	int status;
42801507Sgjelinek 
42811507Sgjelinek 	if (zonecfg_in_alt_root()) {
42821507Sgjelinek 		zerror(gettext("cannot detach zone in alternate root"));
42831507Sgjelinek 		return (Z_ERR);
42841507Sgjelinek 	}
42851507Sgjelinek 
42867089Sgjelinek 	/* Check the argv string for args we handle internally */
42871507Sgjelinek 	optind = 0;
42887089Sgjelinek 	opterr = 0;
42897089Sgjelinek 	while ((arg = getopt(argc, argv, "?n")) != EOF) {
42901507Sgjelinek 		switch (arg) {
42911507Sgjelinek 		case '?':
42927089Sgjelinek 			if (optopt == '?') {
42937089Sgjelinek 				sub_usage(SHELP_DETACH, CMD_DETACH);
42947089Sgjelinek 				brand_help = B_TRUE;
42957089Sgjelinek 			}
42967089Sgjelinek 			/* Ignore unknown options - may be brand specific. */
42977089Sgjelinek 			break;
42982078Sgjelinek 		case 'n':
42992078Sgjelinek 			execute = B_FALSE;
43002078Sgjelinek 			break;
43011507Sgjelinek 		default:
43027089Sgjelinek 			/* Ignore unknown options - may be brand specific. */
43037089Sgjelinek 			break;
43041507Sgjelinek 		}
43051507Sgjelinek 	}
43062712Snn35248 
43077089Sgjelinek 	if (brand_help)
43087089Sgjelinek 		execute = B_FALSE;
43097089Sgjelinek 
43102078Sgjelinek 	if (execute) {
43112712Snn35248 		if (sanity_check(target_zone, CMD_DETACH, B_FALSE, B_TRUE,
43122712Snn35248 		    B_FALSE) != Z_OK)
43132078Sgjelinek 			return (Z_ERR);
43143339Szt129084 		if (verify_details(CMD_DETACH, argv) != Z_OK)
43152078Sgjelinek 			return (Z_ERR);
43162078Sgjelinek 	} else {
43172078Sgjelinek 		/*
43182078Sgjelinek 		 * We want a dry-run to work for a non-privileged user so we
43192078Sgjelinek 		 * only do minimal validation.
43202078Sgjelinek 		 */
43212078Sgjelinek 		if (target_zone == NULL) {
43222078Sgjelinek 			zerror(gettext("no zone specified"));
43232078Sgjelinek 			return (Z_ERR);
43242078Sgjelinek 		}
43252078Sgjelinek 
43262078Sgjelinek 		if (strcmp(target_zone, GLOBAL_ZONENAME) == 0) {
43272078Sgjelinek 			zerror(gettext("%s operation is invalid for the "
43282078Sgjelinek 			    "global zone."), cmd_to_str(CMD_DETACH));
43292078Sgjelinek 			return (Z_ERR);
43302078Sgjelinek 		}
43312078Sgjelinek 	}
43321507Sgjelinek 
43331507Sgjelinek 	if ((err = zone_get_zonepath(target_zone, zonepath, sizeof (zonepath)))
43341507Sgjelinek 	    != Z_OK) {
43351507Sgjelinek 		errno = err;
43361507Sgjelinek 		zperror2(target_zone, gettext("could not get zone path"));
43371507Sgjelinek 		return (Z_ERR);
43381507Sgjelinek 	}
43391507Sgjelinek 
43407089Sgjelinek 	/* Fetch the detach and predetach hooks from the brand configuration. */
43414785Sgjelinek 	if ((bh = brand_open(target_brand)) == NULL) {
43424785Sgjelinek 		zerror(gettext("missing or invalid brand"));
43434785Sgjelinek 		return (Z_ERR);
43444785Sgjelinek 	}
43454785Sgjelinek 
43467089Sgjelinek 	if (get_hook(bh, cmdbuf, sizeof (cmdbuf), brand_get_detach, target_zone,
43477089Sgjelinek 	    zonepath) != Z_OK) {
43487089Sgjelinek 		zerror("invalid brand configuration: missing detach resource");
43497089Sgjelinek 		brand_close(bh);
43507089Sgjelinek 		return (Z_ERR);
43517089Sgjelinek 	}
43527089Sgjelinek 
43537089Sgjelinek 	if (get_hook(bh, precmdbuf, sizeof (precmdbuf), brand_get_predetach,
43547089Sgjelinek 	    target_zone, zonepath) != Z_OK) {
43554785Sgjelinek 		zerror("invalid brand configuration: missing predetach "
43564785Sgjelinek 		    "resource");
43574785Sgjelinek 		brand_close(bh);
43584785Sgjelinek 		return (Z_ERR);
43594785Sgjelinek 	}
43604785Sgjelinek 	brand_close(bh);
43614785Sgjelinek 
43627089Sgjelinek 	/* Append all options to predetach hook. */
43637089Sgjelinek 	if (addoptions(precmdbuf, argv, sizeof (precmdbuf)) != Z_OK)
43647089Sgjelinek 		return (Z_ERR);
43657089Sgjelinek 
43667089Sgjelinek 	/* Append all options to detach hook. */
43677089Sgjelinek 	if (addoptions(cmdbuf, argv, sizeof (cmdbuf)) != Z_OK)
43687089Sgjelinek 		return (Z_ERR);
43697089Sgjelinek 
43707089Sgjelinek 	if (execute && zonecfg_grab_lock_file(target_zone, &lockfd) != Z_OK) {
43711507Sgjelinek 		zerror(gettext("another %s may have an operation in progress."),
43721507Sgjelinek 		    "zoneadm");
43731507Sgjelinek 		return (Z_ERR);
43741507Sgjelinek 	}
43751507Sgjelinek 
43767089Sgjelinek 	/* If we have a brand predetach hook, run it. */
43777089Sgjelinek 	if (!brand_help && precmdbuf[0] != '\0') {
43787089Sgjelinek 		status = do_subproc(precmdbuf);
43797089Sgjelinek 		if (subproc_status(gettext("brand-specific predetach"),
43807089Sgjelinek 		    status, B_FALSE) != ZONE_SUBPROC_OK) {
43817089Sgjelinek 
43828301Sgerald.jelinek@sun.com 			if (execute) {
43838301Sgerald.jelinek@sun.com 				assert(lockfd >= 0);
43847089Sgjelinek 				zonecfg_release_lock_file(target_zone, lockfd);
43858301Sgerald.jelinek@sun.com 				lockfd = -1;
43868301Sgerald.jelinek@sun.com 			}
43878301Sgerald.jelinek@sun.com 
43888301Sgerald.jelinek@sun.com 			assert(lockfd == -1);
43897089Sgjelinek 			return (Z_ERR);
43907089Sgjelinek 		}
43917089Sgjelinek 	}
43927089Sgjelinek 
43937089Sgjelinek 	if (cmdbuf[0] != '\0') {
43947089Sgjelinek 		/* Run the detach hook */
43957089Sgjelinek 		status = do_subproc_interactive(cmdbuf);
43967089Sgjelinek 		if ((status = subproc_status(gettext("brand-specific detach"),
43977089Sgjelinek 		    status, B_FALSE)) != ZONE_SUBPROC_OK) {
43987089Sgjelinek 			if (status == ZONE_SUBPROC_USAGE && !brand_help)
43997089Sgjelinek 				sub_usage(SHELP_DETACH, CMD_DETACH);
44007089Sgjelinek 
44018301Sgerald.jelinek@sun.com 			if (execute) {
44028301Sgerald.jelinek@sun.com 				assert(lockfd >= 0);
44037089Sgjelinek 				zonecfg_release_lock_file(target_zone, lockfd);
44048301Sgerald.jelinek@sun.com 				lockfd = -1;
44058301Sgerald.jelinek@sun.com 			}
44068301Sgerald.jelinek@sun.com 
44078301Sgerald.jelinek@sun.com 			assert(lockfd == -1);
44087089Sgjelinek 			return (Z_ERR);
44097089Sgjelinek 		}
44107089Sgjelinek 
44117089Sgjelinek 	} else {
44128301Sgerald.jelinek@sun.com 		zone_dochandle_t handle;
44138301Sgerald.jelinek@sun.com 
44147089Sgjelinek 		/* If just help, we're done since there is no brand help. */
44158301Sgerald.jelinek@sun.com 		if (brand_help) {
44168301Sgerald.jelinek@sun.com 			assert(lockfd == -1);
44177089Sgjelinek 			return (Z_OK);
44188301Sgerald.jelinek@sun.com 		}
44197089Sgjelinek 
44207089Sgjelinek 		/*
44217089Sgjelinek 		 * Run the built-in detach support.  Just generate a simple
44227089Sgjelinek 		 * zone definition XML file and detach.
44237089Sgjelinek 		 */
44247089Sgjelinek 
44257089Sgjelinek 		/* Don't detach the zone if anything is still mounted there */
44267089Sgjelinek 		if (execute && zonecfg_find_mounts(zonepath, NULL, NULL)) {
44277089Sgjelinek 			(void) fprintf(stderr, gettext("These file systems are "
44287089Sgjelinek 			    "mounted on subdirectories of %s.\n"), zonepath);
44297089Sgjelinek 			(void) zonecfg_find_mounts(zonepath, zfm_print, NULL);
44307089Sgjelinek 			err = ZONE_SUBPROC_NOTCOMPLETE;
44317089Sgjelinek 			goto done;
44327089Sgjelinek 		}
44337089Sgjelinek 
44347089Sgjelinek 		if ((handle = zonecfg_init_handle()) == NULL) {
44357089Sgjelinek 			zperror(cmd_to_str(CMD_DETACH), B_TRUE);
44367089Sgjelinek 			err = ZONE_SUBPROC_NOTCOMPLETE;
44377089Sgjelinek 			goto done;
44387089Sgjelinek 		}
44397089Sgjelinek 
44407089Sgjelinek 		if ((err = zonecfg_get_handle(target_zone, handle)) != Z_OK) {
44417089Sgjelinek 			errno = err;
44427089Sgjelinek 			zperror(cmd_to_str(CMD_DETACH), B_TRUE);
44438301Sgerald.jelinek@sun.com 
44448301Sgerald.jelinek@sun.com 		} else if ((err = zonecfg_detach_save(handle,
44457089Sgjelinek 		    (execute ? 0 : ZONE_DRY_RUN))) != Z_OK) {
44467089Sgjelinek 			errno = err;
44477089Sgjelinek 			zperror(gettext("saving the detach manifest failed"),
44487089Sgjelinek 			    B_TRUE);
44497089Sgjelinek 		}
44507089Sgjelinek 
44517089Sgjelinek 		zonecfg_fini_handle(handle);
44528301Sgerald.jelinek@sun.com 		if (err != Z_OK)
44538301Sgerald.jelinek@sun.com 			goto done;
44541507Sgjelinek 	}
44551507Sgjelinek 
44562078Sgjelinek 	/*
44572078Sgjelinek 	 * Set the zone state back to configured unless we are running with the
44582078Sgjelinek 	 * no-execute option.
44592078Sgjelinek 	 */
44602078Sgjelinek 	if (execute && (err = zone_set_state(target_zone,
44612078Sgjelinek 	    ZONE_STATE_CONFIGURED)) != Z_OK) {
44621507Sgjelinek 		errno = err;
44631507Sgjelinek 		zperror(gettext("could not reset state"), B_TRUE);
44641507Sgjelinek 	}
44651507Sgjelinek 
44661507Sgjelinek done:
44678301Sgerald.jelinek@sun.com 	if (execute) {
44688301Sgerald.jelinek@sun.com 		assert(lockfd >= 0);
44697089Sgjelinek 		zonecfg_release_lock_file(target_zone, lockfd);
44708301Sgerald.jelinek@sun.com 		lockfd = -1;
44718301Sgerald.jelinek@sun.com 	}
44728301Sgerald.jelinek@sun.com 
44738301Sgerald.jelinek@sun.com 	assert(lockfd == -1);
44741507Sgjelinek 	return ((err == Z_OK) ? Z_OK : Z_ERR);
44751507Sgjelinek }
44761507Sgjelinek 
44771507Sgjelinek /*
44787089Sgjelinek  * Determine the brand when doing a dry-run attach.  The zone does not have to
44797089Sgjelinek  * exist, so we have to read the incoming manifest to determine the zone's
44807089Sgjelinek  * brand.
44817089Sgjelinek  *
44827089Sgjelinek  * Because the manifest has to be processed twice; once to determine the brand
44837089Sgjelinek  * and once to do the brand-specific attach logic, we always read it into a tmp
44847089Sgjelinek  * file.  This handles the manifest coming from stdin or a regular file.  The
44857089Sgjelinek  * tmpname parameter returns the name of the temporary file that the manifest
44867089Sgjelinek  * was read into.
44871507Sgjelinek  */
44881507Sgjelinek static int
44897089Sgjelinek dryrun_get_brand(char *manifest_path, char *tmpname, int size)
44902078Sgjelinek {
44912078Sgjelinek 	int fd;
44922078Sgjelinek 	int err;
44937089Sgjelinek 	int res = Z_OK;
44942078Sgjelinek 	zone_dochandle_t local_handle;
44952078Sgjelinek 	zone_dochandle_t rem_handle = NULL;
44967089Sgjelinek 	int len;
44977089Sgjelinek 	int ofd;
44987089Sgjelinek 	char buf[512];
44992078Sgjelinek 
45002078Sgjelinek 	if (strcmp(manifest_path, "-") == 0) {
45017089Sgjelinek 		fd = STDIN_FILENO;
45027089Sgjelinek 	} else {
45037089Sgjelinek 		if ((fd = open(manifest_path, O_RDONLY)) < 0) {
45047089Sgjelinek 			if (getcwd(buf, sizeof (buf)) == NULL)
45057089Sgjelinek 				(void) strlcpy(buf, "/", sizeof (buf));
45067089Sgjelinek 			zerror(gettext("could not open manifest path %s%s: %s"),
45077089Sgjelinek 			    (*manifest_path == '/' ? "" : buf), manifest_path,
45087089Sgjelinek 			    strerror(errno));
45097089Sgjelinek 			return (Z_ERR);
45107089Sgjelinek 		}
45117089Sgjelinek 	}
45127089Sgjelinek 
45137089Sgjelinek 	(void) snprintf(tmpname, size, "/var/run/zone.%d", getpid());
45147089Sgjelinek 
45157089Sgjelinek 	if ((ofd = open(tmpname, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR)) < 0) {
45167089Sgjelinek 		zperror(gettext("could not save manifest"), B_FALSE);
45177089Sgjelinek 		(void) close(fd);
45187089Sgjelinek 		return (Z_ERR);
45197089Sgjelinek 	}
45207089Sgjelinek 
45217089Sgjelinek 	while ((len = read(fd, buf, sizeof (buf))) > 0) {
45227089Sgjelinek 		if (write(ofd, buf, len) == -1) {
45237089Sgjelinek 			zperror(gettext("could not save manifest"), B_FALSE);
45247089Sgjelinek 			(void) close(ofd);
45257089Sgjelinek 			(void) close(fd);
45267089Sgjelinek 			return (Z_ERR);
45277089Sgjelinek 		}
45287089Sgjelinek 	}
45297089Sgjelinek 
45307089Sgjelinek 	if (close(ofd) != 0) {
45317089Sgjelinek 		zperror(gettext("could not save manifest"), B_FALSE);
45327089Sgjelinek 		(void) close(fd);
45337089Sgjelinek 		return (Z_ERR);
45347089Sgjelinek 	}
45357089Sgjelinek 
45367089Sgjelinek 	(void) close(fd);
45377089Sgjelinek 
45387089Sgjelinek 	if ((fd = open(tmpname, O_RDONLY)) < 0) {
45392078Sgjelinek 		zperror(gettext("could not open manifest path"), B_FALSE);
45402078Sgjelinek 		return (Z_ERR);
45412078Sgjelinek 	}
45422078Sgjelinek 
45432078Sgjelinek 	if ((local_handle = zonecfg_init_handle()) == NULL) {
45442078Sgjelinek 		zperror(cmd_to_str(CMD_ATTACH), B_TRUE);
45452078Sgjelinek 		res = Z_ERR;
45462078Sgjelinek 		goto done;
45472078Sgjelinek 	}
45482078Sgjelinek 
45492078Sgjelinek 	if ((rem_handle = zonecfg_init_handle()) == NULL) {
45502078Sgjelinek 		zperror(cmd_to_str(CMD_ATTACH), B_TRUE);
45512078Sgjelinek 		res = Z_ERR;
45522078Sgjelinek 		goto done;
45532078Sgjelinek 	}
45542078Sgjelinek 
45552078Sgjelinek 	if ((err = zonecfg_attach_manifest(fd, local_handle, rem_handle))
45562078Sgjelinek 	    != Z_OK) {
45573686Sgjelinek 		res = Z_ERR;
45583686Sgjelinek 
45593686Sgjelinek 		if (err == Z_INVALID_DOCUMENT) {
45603686Sgjelinek 			struct stat st;
45613686Sgjelinek 			char buf[6];
45623686Sgjelinek 
45633686Sgjelinek 			if (strcmp(manifest_path, "-") == 0) {
45643686Sgjelinek 				zerror(gettext("Input is not a valid XML "
45653686Sgjelinek 				    "file"));
45663686Sgjelinek 				goto done;
45673686Sgjelinek 			}
45683686Sgjelinek 
45693686Sgjelinek 			if (fstat(fd, &st) == -1 || !S_ISREG(st.st_mode)) {
45703686Sgjelinek 				zerror(gettext("%s is not an XML file"),
45713686Sgjelinek 				    manifest_path);
45723686Sgjelinek 				goto done;
45733686Sgjelinek 			}
45743686Sgjelinek 
45753686Sgjelinek 			bzero(buf, sizeof (buf));
45763686Sgjelinek 			(void) lseek(fd, 0L, SEEK_SET);
45773686Sgjelinek 			if (read(fd, buf, sizeof (buf) - 1) < 0 ||
45783686Sgjelinek 			    strncmp(buf, "<?xml", 5) != 0)
45793686Sgjelinek 				zerror(gettext("%s is not an XML file"),
45803686Sgjelinek 				    manifest_path);
45813686Sgjelinek 			else
45823686Sgjelinek 				zerror(gettext("Cannot attach to an earlier "
45833686Sgjelinek 				    "release of the operating system"));
45843686Sgjelinek 		} else {
45852078Sgjelinek 			zperror(cmd_to_str(CMD_ATTACH), B_TRUE);
45863686Sgjelinek 		}
45872078Sgjelinek 		goto done;
45882078Sgjelinek 	}
45892078Sgjelinek 
45907089Sgjelinek 	/* Retrieve remote handle brand type. */
45913172Sgjelinek 	if (zonecfg_get_brand(rem_handle, target_brand, sizeof (target_brand))
45923172Sgjelinek 	    != Z_OK) {
45933172Sgjelinek 		zerror(gettext("missing or invalid brand"));
45943172Sgjelinek 		exit(Z_ERR);
45953172Sgjelinek 	}
45962078Sgjelinek 
45972078Sgjelinek done:
45982078Sgjelinek 	zonecfg_fini_handle(local_handle);
45992078Sgjelinek 	zonecfg_fini_handle(rem_handle);
46007089Sgjelinek 	(void) close(fd);
46012078Sgjelinek 
46022078Sgjelinek 	return ((res == Z_OK) ? Z_OK : Z_ERR);
46032078Sgjelinek }
46042078Sgjelinek 
46055829Sgjelinek /* ARGSUSED */
46061507Sgjelinek static int
46071507Sgjelinek attach_func(int argc, char *argv[])
46081507Sgjelinek {
46098301Sgerald.jelinek@sun.com 	int lockfd = -1;
46101507Sgjelinek 	int err, arg;
46111507Sgjelinek 	boolean_t force = B_FALSE;
46121507Sgjelinek 	zone_dochandle_t handle;
46131507Sgjelinek 	char zonepath[MAXPATHLEN];
46144785Sgjelinek 	char cmdbuf[MAXPATHLEN];
46157089Sgjelinek 	char postcmdbuf[MAXPATHLEN];
46162078Sgjelinek 	boolean_t execute = B_TRUE;
46177089Sgjelinek 	boolean_t brand_help = B_FALSE;
46182078Sgjelinek 	char *manifest_path;
46197089Sgjelinek 	char tmpmanifest[80];
46207089Sgjelinek 	int manifest_pos;
46214785Sgjelinek 	brand_handle_t bh = NULL;
46227089Sgjelinek 	int status;
46238759Sgerald.jelinek@sun.com 	int last_index = 0;
46248759Sgerald.jelinek@sun.com 	int offset;
46258759Sgerald.jelinek@sun.com 	char *up;
46268759Sgerald.jelinek@sun.com 	boolean_t forced_update = B_FALSE;
46271507Sgjelinek 
46281507Sgjelinek 	if (zonecfg_in_alt_root()) {
46291507Sgjelinek 		zerror(gettext("cannot attach zone in alternate root"));
46301507Sgjelinek 		return (Z_ERR);
46311507Sgjelinek 	}
46321507Sgjelinek 
46337089Sgjelinek 	/* Check the argv string for args we handle internally */
46341507Sgjelinek 	optind = 0;
46357089Sgjelinek 	opterr = 0;
46368759Sgerald.jelinek@sun.com 	while ((arg = getopt(argc, argv, "?Fn:U")) != EOF) {
46371507Sgjelinek 		switch (arg) {
46381507Sgjelinek 		case '?':
46397089Sgjelinek 			if (optopt == '?') {
46407089Sgjelinek 				sub_usage(SHELP_ATTACH, CMD_ATTACH);
46417089Sgjelinek 				brand_help = B_TRUE;
46427089Sgjelinek 			}
46437089Sgjelinek 			/* Ignore unknown options - may be brand specific. */
46447089Sgjelinek 			break;
46451507Sgjelinek 		case 'F':
46461507Sgjelinek 			force = B_TRUE;
46471507Sgjelinek 			break;
46482078Sgjelinek 		case 'n':
46492078Sgjelinek 			execute = B_FALSE;
46502078Sgjelinek 			manifest_path = optarg;
46517089Sgjelinek 			manifest_pos = optind - 1;
46525829Sgjelinek 			break;
46538759Sgerald.jelinek@sun.com 		case 'U':
46548759Sgerald.jelinek@sun.com 			/*
46558759Sgerald.jelinek@sun.com 			 * Undocumented 'force update' option for p2v update on
46568759Sgerald.jelinek@sun.com 			 * attach when zone is in the incomplete state.  Change
46578759Sgerald.jelinek@sun.com 			 * the option back to 'u' and set forced_update flag.
46588759Sgerald.jelinek@sun.com 			 */
46598759Sgerald.jelinek@sun.com 			if (optind == last_index)
46608759Sgerald.jelinek@sun.com 				offset = optind;
46618759Sgerald.jelinek@sun.com 			else
46628759Sgerald.jelinek@sun.com 				offset = optind - 1;
46638759Sgerald.jelinek@sun.com 			if ((up = index(argv[offset], 'U')) != NULL)
46648759Sgerald.jelinek@sun.com 				*up = 'u';
46658759Sgerald.jelinek@sun.com 			forced_update = B_TRUE;
46668759Sgerald.jelinek@sun.com 			break;
46671507Sgjelinek 		default:
46687089Sgjelinek 			/* Ignore unknown options - may be brand specific. */
46697089Sgjelinek 			break;
46701507Sgjelinek 		}
46718759Sgerald.jelinek@sun.com 		last_index = optind;
46721507Sgjelinek 	}
46732078Sgjelinek 
46747089Sgjelinek 	if (brand_help) {
46757089Sgjelinek 		force = B_FALSE;
46767089Sgjelinek 		execute = B_TRUE;
46777089Sgjelinek 	}
46787089Sgjelinek 
46797089Sgjelinek 	/* dry-run and force flags are mutually exclusive */
46807089Sgjelinek 	if (!execute && force) {
46817089Sgjelinek 		zerror(gettext("-F and -n flags are mutually exclusive"));
46825829Sgjelinek 		return (Z_ERR);
46835829Sgjelinek 	}
46845829Sgjelinek 
46852078Sgjelinek 	/*
46867089Sgjelinek 	 * If the no-execute option was specified, we don't do validation and
46877089Sgjelinek 	 * need to figure out the brand, since there is no zone required to be
46882078Sgjelinek 	 * configured for this option.
46892078Sgjelinek 	 */
46907089Sgjelinek 	if (execute) {
46917089Sgjelinek 		if (!brand_help) {
46927089Sgjelinek 			if (sanity_check(target_zone, CMD_ATTACH, B_FALSE,
46938759Sgerald.jelinek@sun.com 			    B_TRUE, forced_update) != Z_OK)
46947089Sgjelinek 				return (Z_ERR);
46957089Sgjelinek 			if (verify_details(CMD_ATTACH, argv) != Z_OK)
46967089Sgjelinek 				return (Z_ERR);
46977089Sgjelinek 		}
46987089Sgjelinek 
46997089Sgjelinek 		if ((err = zone_get_zonepath(target_zone, zonepath,
47007089Sgjelinek 		    sizeof (zonepath))) != Z_OK) {
47017089Sgjelinek 			errno = err;
47027089Sgjelinek 			zperror2(target_zone,
47037089Sgjelinek 			    gettext("could not get zone path"));
47047089Sgjelinek 			return (Z_ERR);
47057089Sgjelinek 		}
47067089Sgjelinek 	} else {
47077089Sgjelinek 		if (dryrun_get_brand(manifest_path, tmpmanifest,
47087089Sgjelinek 		    sizeof (tmpmanifest)) != Z_OK)
47097089Sgjelinek 			return (Z_ERR);
47107089Sgjelinek 
47117089Sgjelinek 		argv[manifest_pos] = tmpmanifest;
47127089Sgjelinek 		target_zone = "-";
47137089Sgjelinek 		(void) strlcpy(zonepath, "-", sizeof (zonepath));
47147089Sgjelinek 
47157089Sgjelinek 		/* Run the brand's verify_adm hook. */
47167089Sgjelinek 		if (verify_brand(NULL, CMD_ATTACH, argv) != Z_OK)
47177089Sgjelinek 			return (Z_ERR);
47187089Sgjelinek 	}
47197089Sgjelinek 
47207089Sgjelinek 	/*
47217089Sgjelinek 	 * Fetch the attach and postattach hooks from the brand configuration.
47227089Sgjelinek 	 */
47234785Sgjelinek 	if ((bh = brand_open(target_brand)) == NULL) {
47244785Sgjelinek 		zerror(gettext("missing or invalid brand"));
47254785Sgjelinek 		return (Z_ERR);
47264785Sgjelinek 	}
47274785Sgjelinek 
47287089Sgjelinek 	if (get_hook(bh, cmdbuf, sizeof (cmdbuf), brand_get_attach, target_zone,
47297089Sgjelinek 	    zonepath) != Z_OK) {
47307089Sgjelinek 		zerror("invalid brand configuration: missing attach resource");
47317089Sgjelinek 		brand_close(bh);
47327089Sgjelinek 		return (Z_ERR);
47337089Sgjelinek 	}
47347089Sgjelinek 
47357089Sgjelinek 	if (get_hook(bh, postcmdbuf, sizeof (postcmdbuf), brand_get_postattach,
47367089Sgjelinek 	    target_zone, zonepath) != Z_OK) {
47374785Sgjelinek 		zerror("invalid brand configuration: missing postattach "
47384785Sgjelinek 		    "resource");
47394785Sgjelinek 		brand_close(bh);
47404785Sgjelinek 		return (Z_ERR);
47414785Sgjelinek 	}
47424785Sgjelinek 	brand_close(bh);
47434785Sgjelinek 
47447089Sgjelinek 	/* Append all options to attach hook. */
47457089Sgjelinek 	if (addoptions(cmdbuf, argv, sizeof (cmdbuf)) != Z_OK)
47467089Sgjelinek 		return (Z_ERR);
47477089Sgjelinek 
47487089Sgjelinek 	/* Append all options to postattach hook. */
47497089Sgjelinek 	if (addoptions(postcmdbuf, argv, sizeof (postcmdbuf)) != Z_OK)
47507089Sgjelinek 		return (Z_ERR);
47517089Sgjelinek 
47527089Sgjelinek 	if (execute && !brand_help) {
47537089Sgjelinek 		if (zonecfg_grab_lock_file(target_zone, &lockfd) != Z_OK) {
47547089Sgjelinek 			zerror(gettext("another %s may have an operation in "
47557089Sgjelinek 			    "progress."), "zoneadm");
47567089Sgjelinek 			return (Z_ERR);
47573777Sgjelinek 		}
47588301Sgerald.jelinek@sun.com 	}
47598301Sgerald.jelinek@sun.com 
47608301Sgerald.jelinek@sun.com 	if (!force) {
47618301Sgerald.jelinek@sun.com 		/*
47628301Sgerald.jelinek@sun.com 		 * Not a force-attach, so we need to actually do the work.
47638301Sgerald.jelinek@sun.com 		 */
47648301Sgerald.jelinek@sun.com 		if (cmdbuf[0] != '\0') {
47658301Sgerald.jelinek@sun.com 			/* Run the attach hook */
47668759Sgerald.jelinek@sun.com 			status = do_subproc(cmdbuf);
47678301Sgerald.jelinek@sun.com 			if ((status = subproc_status(gettext("brand-specific "
47688301Sgerald.jelinek@sun.com 			    "attach"), status, B_FALSE)) != ZONE_SUBPROC_OK) {
47698301Sgerald.jelinek@sun.com 				if (status == ZONE_SUBPROC_USAGE && !brand_help)
47708301Sgerald.jelinek@sun.com 					sub_usage(SHELP_ATTACH, CMD_ATTACH);
47718301Sgerald.jelinek@sun.com 
47728301Sgerald.jelinek@sun.com 				if (execute && !brand_help) {
47738759Sgerald.jelinek@sun.com 					assert(zonecfg_lock_file_held(&lockfd));
47748301Sgerald.jelinek@sun.com 					zonecfg_release_lock_file(target_zone,
47758301Sgerald.jelinek@sun.com 					    lockfd);
47768301Sgerald.jelinek@sun.com 					lockfd = -1;
47778301Sgerald.jelinek@sun.com 				}
47788301Sgerald.jelinek@sun.com 
47798301Sgerald.jelinek@sun.com 				assert(lockfd == -1);
47808301Sgerald.jelinek@sun.com 				return (Z_ERR);
47818301Sgerald.jelinek@sun.com 			}
47828301Sgerald.jelinek@sun.com 		}
47838301Sgerald.jelinek@sun.com 
47848301Sgerald.jelinek@sun.com 		/*
47858301Sgerald.jelinek@sun.com 		 * Else run the built-in attach support.
47868301Sgerald.jelinek@sun.com 		 * This is a no-op since there is nothing to validate.
47878301Sgerald.jelinek@sun.com 		 */
47888301Sgerald.jelinek@sun.com 
47898301Sgerald.jelinek@sun.com 		/* If dry-run or help, then we're done. */
47908301Sgerald.jelinek@sun.com 		if (!execute || brand_help) {
47918301Sgerald.jelinek@sun.com 			if (!execute)
47928301Sgerald.jelinek@sun.com 				(void) unlink(tmpmanifest);
47938301Sgerald.jelinek@sun.com 			assert(lockfd == -1);
47948301Sgerald.jelinek@sun.com 			return (Z_OK);
47958301Sgerald.jelinek@sun.com 		}
47968301Sgerald.jelinek@sun.com 	}
47978301Sgerald.jelinek@sun.com 
47988301Sgerald.jelinek@sun.com 	if ((handle = zonecfg_init_handle()) == NULL) {
47998301Sgerald.jelinek@sun.com 		zperror(cmd_to_str(CMD_ATTACH), B_TRUE);
48008301Sgerald.jelinek@sun.com 		err = Z_ERR;
48018301Sgerald.jelinek@sun.com 	} else if ((err = zonecfg_get_handle(target_zone, handle)) != Z_OK) {
48028301Sgerald.jelinek@sun.com 		errno = err;
48038301Sgerald.jelinek@sun.com 		zperror(cmd_to_str(CMD_ATTACH), B_TRUE);
48048301Sgerald.jelinek@sun.com 		zonecfg_fini_handle(handle);
48058301Sgerald.jelinek@sun.com 	} else {
48068301Sgerald.jelinek@sun.com 		zonecfg_rm_detached(handle, force);
48078301Sgerald.jelinek@sun.com 		zonecfg_fini_handle(handle);
48088301Sgerald.jelinek@sun.com 	}
48098301Sgerald.jelinek@sun.com 
48108301Sgerald.jelinek@sun.com 	if (err == Z_OK &&
48118301Sgerald.jelinek@sun.com 	    (err = zone_set_state(target_zone, ZONE_STATE_INSTALLED)) != Z_OK) {
48121507Sgjelinek 		errno = err;
48131507Sgjelinek 		zperror(gettext("could not reset state"), B_TRUE);
48141507Sgjelinek 	}
48151507Sgjelinek 
48168759Sgerald.jelinek@sun.com 	assert(zonecfg_lock_file_held(&lockfd));
48177089Sgjelinek 	zonecfg_release_lock_file(target_zone, lockfd);
48188301Sgerald.jelinek@sun.com 	lockfd = -1;
48191507Sgjelinek 
48204785Sgjelinek 	/* If we have a brand postattach hook, run it. */
48217089Sgjelinek 	if (err == Z_OK && !force && postcmdbuf[0] != '\0') {
48227089Sgjelinek 		status = do_subproc(postcmdbuf);
48234785Sgjelinek 		if (subproc_status(gettext("brand-specific postattach"),
48244785Sgjelinek 		    status, B_FALSE) != ZONE_SUBPROC_OK) {
48254785Sgjelinek 			if ((err = zone_set_state(target_zone,
48264785Sgjelinek 			    ZONE_STATE_CONFIGURED)) != Z_OK) {
48274785Sgjelinek 				errno = err;
48284785Sgjelinek 				zperror(gettext("could not reset state"),
48294785Sgjelinek 				    B_TRUE);
48304785Sgjelinek 			}
48314785Sgjelinek 		}
48324785Sgjelinek 	}
48334785Sgjelinek 
48348301Sgerald.jelinek@sun.com 	assert(lockfd == -1);
48351507Sgjelinek 	return ((err == Z_OK) ? Z_OK : Z_ERR);
48361507Sgjelinek }
48371507Sgjelinek 
48381300Sgjelinek /*
48390Sstevel@tonic-gate  * On input, TRUE => yes, FALSE => no.
48400Sstevel@tonic-gate  * On return, TRUE => 1, FALSE => 0, could not ask => -1.
48410Sstevel@tonic-gate  */
48420Sstevel@tonic-gate 
48430Sstevel@tonic-gate static int
48440Sstevel@tonic-gate ask_yesno(boolean_t default_answer, const char *question)
48450Sstevel@tonic-gate {
48460Sstevel@tonic-gate 	char line[64];	/* should be large enough to answer yes or no */
48470Sstevel@tonic-gate 
48480Sstevel@tonic-gate 	if (!isatty(STDIN_FILENO))
48490Sstevel@tonic-gate 		return (-1);
48500Sstevel@tonic-gate 	for (;;) {
48510Sstevel@tonic-gate 		(void) printf("%s (%s)? ", question,
48520Sstevel@tonic-gate 		    default_answer ? "[y]/n" : "y/[n]");
48530Sstevel@tonic-gate 		if (fgets(line, sizeof (line), stdin) == NULL ||
48540Sstevel@tonic-gate 		    line[0] == '\n')
48550Sstevel@tonic-gate 			return (default_answer ? 1 : 0);
48560Sstevel@tonic-gate 		if (tolower(line[0]) == 'y')
48570Sstevel@tonic-gate 			return (1);
48580Sstevel@tonic-gate 		if (tolower(line[0]) == 'n')
48590Sstevel@tonic-gate 			return (0);
48600Sstevel@tonic-gate 	}
48610Sstevel@tonic-gate }
48620Sstevel@tonic-gate 
48637089Sgjelinek /* ARGSUSED */
48640Sstevel@tonic-gate static int
48650Sstevel@tonic-gate uninstall_func(int argc, char *argv[])
48660Sstevel@tonic-gate {
48670Sstevel@tonic-gate 	char line[ZONENAME_MAX + 128];	/* Enough for "Are you sure ..." */
48681867Sgjelinek 	char rootpath[MAXPATHLEN], zonepath[MAXPATHLEN];
48694785Sgjelinek 	char cmdbuf[MAXPATHLEN];
48707089Sgjelinek 	char precmdbuf[MAXPATHLEN];
48710Sstevel@tonic-gate 	boolean_t force = B_FALSE;
48720Sstevel@tonic-gate 	int lockfd, answer;
48730Sstevel@tonic-gate 	int err, arg;
48747089Sgjelinek 	boolean_t brand_help = B_FALSE;
48754785Sgjelinek 	brand_handle_t bh = NULL;
48767089Sgjelinek 	int status;
48770Sstevel@tonic-gate 
4878766Scarlsonj 	if (zonecfg_in_alt_root()) {
4879766Scarlsonj 		zerror(gettext("cannot uninstall zone in alternate root"));
4880766Scarlsonj 		return (Z_ERR);
4881766Scarlsonj 	}
4882766Scarlsonj 
48837089Sgjelinek 	/* Check the argv string for args we handle internally */
48840Sstevel@tonic-gate 	optind = 0;
48857089Sgjelinek 	opterr = 0;
48860Sstevel@tonic-gate 	while ((arg = getopt(argc, argv, "?F")) != EOF) {
48870Sstevel@tonic-gate 		switch (arg) {
48880Sstevel@tonic-gate 		case '?':
48897089Sgjelinek 			if (optopt == '?') {
48907089Sgjelinek 				sub_usage(SHELP_UNINSTALL, CMD_UNINSTALL);
48917089Sgjelinek 				brand_help = B_TRUE;
48927089Sgjelinek 			}
48937089Sgjelinek 			/* Ignore unknown options - may be brand specific. */
48947089Sgjelinek 			break;
48950Sstevel@tonic-gate 		case 'F':
48960Sstevel@tonic-gate 			force = B_TRUE;
48970Sstevel@tonic-gate 			break;
48980Sstevel@tonic-gate 		default:
48997089Sgjelinek 			/* Ignore unknown options - may be brand specific. */
49007089Sgjelinek 			break;
49010Sstevel@tonic-gate 		}
49020Sstevel@tonic-gate 	}
49037089Sgjelinek 
49047089Sgjelinek 	if (!brand_help) {
49057089Sgjelinek 		if (sanity_check(target_zone, CMD_UNINSTALL, B_FALSE, B_TRUE,
49067089Sgjelinek 		    B_FALSE) != Z_OK)
49077089Sgjelinek 			return (Z_ERR);
49087089Sgjelinek 
49097089Sgjelinek 		/*
49107089Sgjelinek 		 * Invoke brand-specific handler.
49117089Sgjelinek 		 */
49127089Sgjelinek 		if (invoke_brand_handler(CMD_UNINSTALL, argv) != Z_OK)
49130Sstevel@tonic-gate 			return (Z_ERR);
49147089Sgjelinek 
49157089Sgjelinek 		if (!force) {
49167089Sgjelinek 			(void) snprintf(line, sizeof (line),
49177089Sgjelinek 			    gettext("Are you sure you want to %s zone %s"),
49187089Sgjelinek 			    cmd_to_str(CMD_UNINSTALL), target_zone);
49197089Sgjelinek 			if ((answer = ask_yesno(B_FALSE, line)) == 0) {
49207089Sgjelinek 				return (Z_OK);
49217089Sgjelinek 			} else if (answer == -1) {
49227089Sgjelinek 				zerror(gettext("Input not from terminal and -F "
49237089Sgjelinek 				    "not specified: %s not done."),
49247089Sgjelinek 				    cmd_to_str(CMD_UNINSTALL));
49257089Sgjelinek 				return (Z_ERR);
49267089Sgjelinek 			}
49270Sstevel@tonic-gate 		}
49280Sstevel@tonic-gate 	}
49290Sstevel@tonic-gate 
49301867Sgjelinek 	if ((err = zone_get_zonepath(target_zone, zonepath,
49311867Sgjelinek 	    sizeof (zonepath))) != Z_OK) {
49320Sstevel@tonic-gate 		errno = err;
49330Sstevel@tonic-gate 		zperror2(target_zone, gettext("could not get zone path"));
49340Sstevel@tonic-gate 		return (Z_ERR);
49350Sstevel@tonic-gate 	}
49360Sstevel@tonic-gate 
49370Sstevel@tonic-gate 	/*
49387089Sgjelinek 	 * Fetch the uninstall and preuninstall hooks from the brand
49397089Sgjelinek 	 * configuration.
49400Sstevel@tonic-gate 	 */
49414785Sgjelinek 	if ((bh = brand_open(target_brand)) == NULL) {
49424785Sgjelinek 		zerror(gettext("missing or invalid brand"));
49434785Sgjelinek 		return (Z_ERR);
49444785Sgjelinek 	}
49454785Sgjelinek 
49467089Sgjelinek 	if (get_hook(bh, cmdbuf, sizeof (cmdbuf), brand_get_uninstall,
49477089Sgjelinek 	    target_zone, zonepath) != Z_OK) {
49487089Sgjelinek 		zerror("invalid brand configuration: missing uninstall "
49497089Sgjelinek 		    "resource");
49507089Sgjelinek 		brand_close(bh);
49517089Sgjelinek 		return (Z_ERR);
49527089Sgjelinek 	}
49537089Sgjelinek 
49547089Sgjelinek 	if (get_hook(bh, precmdbuf, sizeof (precmdbuf), brand_get_preuninstall,
49557089Sgjelinek 	    target_zone, zonepath) != Z_OK) {
49564785Sgjelinek 		zerror("invalid brand configuration: missing preuninstall "
49574785Sgjelinek 		    "resource");
49584785Sgjelinek 		brand_close(bh);
49594785Sgjelinek 		return (Z_ERR);
49604785Sgjelinek 	}
49614785Sgjelinek 	brand_close(bh);
49624785Sgjelinek 
49637089Sgjelinek 	/* Append all options to preuninstall hook. */
49647089Sgjelinek 	if (addoptions(precmdbuf, argv, sizeof (precmdbuf)) != Z_OK)
49657089Sgjelinek 		return (Z_ERR);
49667089Sgjelinek 
49677089Sgjelinek 	/* Append all options to uninstall hook. */
49687089Sgjelinek 	if (addoptions(cmdbuf, argv, sizeof (cmdbuf)) != Z_OK)
49697089Sgjelinek 		return (Z_ERR);
49707089Sgjelinek 
49717089Sgjelinek 	if (!brand_help) {
49727089Sgjelinek 		if ((err = zone_get_rootpath(target_zone, rootpath,
49737089Sgjelinek 		    sizeof (rootpath))) != Z_OK) {
49747089Sgjelinek 			errno = err;
49757089Sgjelinek 			zperror2(target_zone, gettext("could not get root "
49767089Sgjelinek 			    "path"));
49774785Sgjelinek 			return (Z_ERR);
49784785Sgjelinek 		}
49794785Sgjelinek 
49807089Sgjelinek 		/*
49817089Sgjelinek 		 * If there seems to be a zoneadmd running for this zone, call
49827089Sgjelinek 		 * it to tell it that an uninstall is happening; if all goes
49837089Sgjelinek 		 * well it will then shut itself down.
49847089Sgjelinek 		 */
49857089Sgjelinek 		if (zonecfg_ping_zoneadmd(target_zone) == Z_OK) {
49867089Sgjelinek 			zone_cmd_arg_t zarg;
49877089Sgjelinek 			zarg.cmd = Z_NOTE_UNINSTALLING;
49887089Sgjelinek 			/* we don't care too much if this fails, just plow on */
49897089Sgjelinek 			(void) zonecfg_call_zoneadmd(target_zone, &zarg, locale,
49907089Sgjelinek 			    B_TRUE);
49917089Sgjelinek 		}
49927089Sgjelinek 
49937089Sgjelinek 		if (zonecfg_grab_lock_file(target_zone, &lockfd) != Z_OK) {
49947089Sgjelinek 			zerror(gettext("another %s may have an operation in "
49957089Sgjelinek 			    "progress."), "zoneadm");
49967089Sgjelinek 			return (Z_ERR);
49977089Sgjelinek 		}
49987089Sgjelinek 
49997089Sgjelinek 		/* Don't uninstall the zone if anything is mounted there */
50007089Sgjelinek 		err = zonecfg_find_mounts(rootpath, NULL, NULL);
50017089Sgjelinek 		if (err) {
50027089Sgjelinek 			zerror(gettext("These file systems are mounted on "
50037089Sgjelinek 			    "subdirectories of %s.\n"), rootpath);
50047089Sgjelinek 			(void) zonecfg_find_mounts(rootpath, zfm_print, NULL);
50057089Sgjelinek 			zonecfg_release_lock_file(target_zone, lockfd);
50067089Sgjelinek 			return (Z_ERR);
50077089Sgjelinek 		}
50087089Sgjelinek 	}
50097089Sgjelinek 
50107089Sgjelinek 	/* If we have a brand preuninstall hook, run it. */
50117089Sgjelinek 	if (!brand_help && precmdbuf[0] != '\0') {
50124785Sgjelinek 		status = do_subproc(cmdbuf);
50134785Sgjelinek 		if (subproc_status(gettext("brand-specific preuninstall"),
50144785Sgjelinek 		    status, B_FALSE) != ZONE_SUBPROC_OK) {
50157089Sgjelinek 			zonecfg_release_lock_file(target_zone, lockfd);
50164785Sgjelinek 			return (Z_ERR);
50174785Sgjelinek 		}
50184785Sgjelinek 	}
50194785Sgjelinek 
50207089Sgjelinek 	if (!brand_help) {
50217089Sgjelinek 		err = zone_set_state(target_zone, ZONE_STATE_INCOMPLETE);
50227089Sgjelinek 		if (err != Z_OK) {
50237089Sgjelinek 			errno = err;
50247089Sgjelinek 			zperror2(target_zone, gettext("could not set state"));
50257089Sgjelinek 			goto bad;
50267089Sgjelinek 		}
50277089Sgjelinek 	}
50287089Sgjelinek 
50297089Sgjelinek 	/*
50307089Sgjelinek 	 * If there is a brand uninstall hook, use it, otherwise use the
50317089Sgjelinek 	 * built-in uninstall code.
50327089Sgjelinek 	 */
50337089Sgjelinek 	if (cmdbuf[0] != '\0') {
50347089Sgjelinek 		/* Run the uninstall hook */
50357089Sgjelinek 		status = do_subproc_interactive(cmdbuf);
50367089Sgjelinek 		if ((status = subproc_status(gettext("brand-specific "
50377089Sgjelinek 		    "uninstall"), status, B_FALSE)) != ZONE_SUBPROC_OK) {
50387089Sgjelinek 			if (status == ZONE_SUBPROC_USAGE && !brand_help)
50397089Sgjelinek 				sub_usage(SHELP_UNINSTALL, CMD_UNINSTALL);
50407089Sgjelinek 			if (!brand_help)
50417089Sgjelinek 				zonecfg_release_lock_file(target_zone, lockfd);
50427089Sgjelinek 			return (Z_ERR);
50437089Sgjelinek 		}
50447089Sgjelinek 
50457089Sgjelinek 		if (brand_help)
50467089Sgjelinek 			return (Z_OK);
50477089Sgjelinek 	} else {
50487089Sgjelinek 		/* If just help, we're done since there is no brand help. */
50497089Sgjelinek 		if (brand_help)
50507089Sgjelinek 			return (Z_OK);
50517089Sgjelinek 
50527089Sgjelinek 		/* Run the built-in uninstall support. */
50537089Sgjelinek 		if ((err = cleanup_zonepath(zonepath, B_FALSE)) != Z_OK) {
50547089Sgjelinek 			errno = err;
50557089Sgjelinek 			zperror2(target_zone, gettext("cleaning up zonepath "
50567089Sgjelinek 			    "failed"));
50577089Sgjelinek 			goto bad;
50587089Sgjelinek 		}
50591867Sgjelinek 	}
50601867Sgjelinek 
50610Sstevel@tonic-gate 	err = zone_set_state(target_zone, ZONE_STATE_CONFIGURED);
50620Sstevel@tonic-gate 	if (err != Z_OK) {
50630Sstevel@tonic-gate 		errno = err;
50640Sstevel@tonic-gate 		zperror2(target_zone, gettext("could not reset state"));
50650Sstevel@tonic-gate 	}
50660Sstevel@tonic-gate bad:
50677089Sgjelinek 	zonecfg_release_lock_file(target_zone, lockfd);
50680Sstevel@tonic-gate 	return (err);
50690Sstevel@tonic-gate }
50700Sstevel@tonic-gate 
5071766Scarlsonj /* ARGSUSED */
5072766Scarlsonj static int
5073766Scarlsonj mount_func(int argc, char *argv[])
5074766Scarlsonj {
5075766Scarlsonj 	zone_cmd_arg_t zarg;
50762712Snn35248 	boolean_t force = B_FALSE;
50772712Snn35248 	int arg;
50782712Snn35248 
50792712Snn35248 	/*
50802712Snn35248 	 * The only supported subargument to the "mount" subcommand is
50812712Snn35248 	 * "-f", which forces us to mount a zone in the INCOMPLETE state.
50822712Snn35248 	 */
50832712Snn35248 	optind = 0;
50842712Snn35248 	if ((arg = getopt(argc, argv, "f")) != EOF) {
50852712Snn35248 		switch (arg) {
50862712Snn35248 		case 'f':
50872712Snn35248 			force = B_TRUE;
50882712Snn35248 			break;
50892712Snn35248 		default:
50902712Snn35248 			return (Z_USAGE);
50912712Snn35248 		}
50922712Snn35248 	}
50932712Snn35248 	if (argc > optind)
5094766Scarlsonj 		return (Z_USAGE);
50952712Snn35248 
50962712Snn35248 	if (sanity_check(target_zone, CMD_MOUNT, B_FALSE, B_FALSE, force)
50972712Snn35248 	    != Z_OK)
5098766Scarlsonj 		return (Z_ERR);
50993339Szt129084 	if (verify_details(CMD_MOUNT, argv) != Z_OK)
5100766Scarlsonj 		return (Z_ERR);
5101766Scarlsonj 
51022712Snn35248 	zarg.cmd = force ? Z_FORCEMOUNT : Z_MOUNT;
51035829Sgjelinek 	zarg.bootbuf[0] = '\0';
51047089Sgjelinek 	if (zonecfg_call_zoneadmd(target_zone, &zarg, locale, B_TRUE) != 0) {
5105766Scarlsonj 		zerror(gettext("call to %s failed"), "zoneadmd");
5106766Scarlsonj 		return (Z_ERR);
5107766Scarlsonj 	}
5108766Scarlsonj 	return (Z_OK);
5109766Scarlsonj }
5110766Scarlsonj 
5111766Scarlsonj /* ARGSUSED */
5112766Scarlsonj static int
5113766Scarlsonj unmount_func(int argc, char *argv[])
5114766Scarlsonj {
5115766Scarlsonj 	zone_cmd_arg_t zarg;
5116766Scarlsonj 
5117766Scarlsonj 	if (argc > 0)
5118766Scarlsonj 		return (Z_USAGE);
51192712Snn35248 	if (sanity_check(target_zone, CMD_UNMOUNT, B_FALSE, B_FALSE, B_FALSE)
51202712Snn35248 	    != Z_OK)
5121766Scarlsonj 		return (Z_ERR);
5122766Scarlsonj 
5123766Scarlsonj 	zarg.cmd = Z_UNMOUNT;
51247089Sgjelinek 	if (zonecfg_call_zoneadmd(target_zone, &zarg, locale, B_TRUE) != 0) {
5125766Scarlsonj 		zerror(gettext("call to %s failed"), "zoneadmd");
5126766Scarlsonj 		return (Z_ERR);
5127766Scarlsonj 	}
5128766Scarlsonj 	return (Z_OK);
5129766Scarlsonj }
5130766Scarlsonj 
51310Sstevel@tonic-gate static int
51322303Scarlsonj mark_func(int argc, char *argv[])
51332303Scarlsonj {
51342303Scarlsonj 	int err, lockfd;
51358759Sgerald.jelinek@sun.com 	int arg;
51368759Sgerald.jelinek@sun.com 	boolean_t force = B_FALSE;
51378759Sgerald.jelinek@sun.com 	int state;
51388759Sgerald.jelinek@sun.com 
51398759Sgerald.jelinek@sun.com 	optind = 0;
51408759Sgerald.jelinek@sun.com 	opterr = 0;
51418759Sgerald.jelinek@sun.com 	while ((arg = getopt(argc, argv, "F")) != EOF) {
51428759Sgerald.jelinek@sun.com 		switch (arg) {
51438759Sgerald.jelinek@sun.com 		case 'F':
51448759Sgerald.jelinek@sun.com 			force = B_TRUE;
51458759Sgerald.jelinek@sun.com 			break;
51468759Sgerald.jelinek@sun.com 		default:
51478759Sgerald.jelinek@sun.com 			return (Z_USAGE);
51488759Sgerald.jelinek@sun.com 		}
51498759Sgerald.jelinek@sun.com 	}
51508759Sgerald.jelinek@sun.com 
51518759Sgerald.jelinek@sun.com 	if (argc != (optind + 1))
51522303Scarlsonj 		return (Z_USAGE);
51538759Sgerald.jelinek@sun.com 
51548759Sgerald.jelinek@sun.com 	if (strcmp(argv[optind], "configured") == 0)
51558759Sgerald.jelinek@sun.com 		state = ZONE_STATE_CONFIGURED;
51568759Sgerald.jelinek@sun.com 	else if (strcmp(argv[optind], "incomplete") == 0)
51578759Sgerald.jelinek@sun.com 		state = ZONE_STATE_INCOMPLETE;
51588759Sgerald.jelinek@sun.com 	else if (strcmp(argv[optind], "installed") == 0)
51598759Sgerald.jelinek@sun.com 		state = ZONE_STATE_INSTALLED;
51608759Sgerald.jelinek@sun.com 	else
51618759Sgerald.jelinek@sun.com 		return (Z_USAGE);
51628759Sgerald.jelinek@sun.com 
51638759Sgerald.jelinek@sun.com 	if (state != ZONE_STATE_INCOMPLETE && !force)
51648759Sgerald.jelinek@sun.com 		return (Z_USAGE);
51658759Sgerald.jelinek@sun.com 
51668759Sgerald.jelinek@sun.com 	if (sanity_check(target_zone, CMD_MARK, B_FALSE, B_TRUE, B_FALSE)
51672712Snn35248 	    != Z_OK)
51682303Scarlsonj 		return (Z_ERR);
51692303Scarlsonj 
51703339Szt129084 	/*
51713339Szt129084 	 * Invoke brand-specific handler.
51723339Szt129084 	 */
51733339Szt129084 	if (invoke_brand_handler(CMD_MARK, argv) != Z_OK)
51743339Szt129084 		return (Z_ERR);
51753339Szt129084 
51767089Sgjelinek 	if (zonecfg_grab_lock_file(target_zone, &lockfd) != Z_OK) {
51772303Scarlsonj 		zerror(gettext("another %s may have an operation in progress."),
51782303Scarlsonj 		    "zoneadm");
51792303Scarlsonj 		return (Z_ERR);
51802303Scarlsonj 	}
51812303Scarlsonj 
51828759Sgerald.jelinek@sun.com 	err = zone_set_state(target_zone, state);
51832303Scarlsonj 	if (err != Z_OK) {
51842303Scarlsonj 		errno = err;
51852303Scarlsonj 		zperror2(target_zone, gettext("could not set state"));
51862303Scarlsonj 	}
51877089Sgjelinek 	zonecfg_release_lock_file(target_zone, lockfd);
51882303Scarlsonj 
51892303Scarlsonj 	return (err);
51902303Scarlsonj }
51912303Scarlsonj 
51923247Sgjelinek /*
51933247Sgjelinek  * Check what scheduling class we're running under and print a warning if
51943247Sgjelinek  * we're not using FSS.
51953247Sgjelinek  */
51963247Sgjelinek static int
51973247Sgjelinek check_sched_fss(zone_dochandle_t handle)
51983247Sgjelinek {
51993247Sgjelinek 	char class_name[PC_CLNMSZ];
52003247Sgjelinek 
52013247Sgjelinek 	if (zonecfg_get_dflt_sched_class(handle, class_name,
52023247Sgjelinek 	    sizeof (class_name)) != Z_OK) {
52033247Sgjelinek 		zerror(gettext("WARNING: unable to determine the zone's "
52043247Sgjelinek 		    "scheduling class"));
52053247Sgjelinek 	} else if (strcmp("FSS", class_name) != 0) {
52063247Sgjelinek 		zerror(gettext("WARNING: The zone.cpu-shares rctl is set but\n"
52073247Sgjelinek 		    "FSS is not the default scheduling class for this zone.  "
52083247Sgjelinek 		    "FSS will be\nused for processes in the zone but to get "
52093247Sgjelinek 		    "the full benefit of FSS,\nit should be the default "
52103247Sgjelinek 		    "scheduling class.  See dispadmin(1M) for\nmore details."));
52113247Sgjelinek 		return (Z_SYSTEM);
52123247Sgjelinek 	}
52133247Sgjelinek 
52143247Sgjelinek 	return (Z_OK);
52153247Sgjelinek }
52163247Sgjelinek 
52173247Sgjelinek static int
52183247Sgjelinek check_cpu_shares_sched(zone_dochandle_t handle)
52193247Sgjelinek {
52203247Sgjelinek 	int err;
52213247Sgjelinek 	int res = Z_OK;
52223247Sgjelinek 	struct zone_rctltab rctl;
52233247Sgjelinek 
52243247Sgjelinek 	if ((err = zonecfg_setrctlent(handle)) != Z_OK) {
52253247Sgjelinek 		errno = err;
52263247Sgjelinek 		zperror(cmd_to_str(CMD_APPLY), B_TRUE);
52273247Sgjelinek 		return (err);
52283247Sgjelinek 	}
52293247Sgjelinek 
52303247Sgjelinek 	while (zonecfg_getrctlent(handle, &rctl) == Z_OK) {
52313247Sgjelinek 		if (strcmp(rctl.zone_rctl_name, "zone.cpu-shares") == 0) {
52323247Sgjelinek 			if (check_sched_fss(handle) != Z_OK)
52333247Sgjelinek 				res = Z_SYSTEM;
52343247Sgjelinek 			break;
52353247Sgjelinek 		}
52363247Sgjelinek 	}
52373247Sgjelinek 
52383247Sgjelinek 	(void) zonecfg_endrctlent(handle);
52393247Sgjelinek 
52403247Sgjelinek 	return (res);
52413247Sgjelinek }
52423247Sgjelinek 
52433247Sgjelinek /*
52443352Sgjelinek  * Check if there is a mix of processes running in different pools within the
52453352Sgjelinek  * zone.  This is currently only going to be called for the global zone from
52463352Sgjelinek  * apply_func but that could be generalized in the future.
52473352Sgjelinek  */
52483352Sgjelinek static boolean_t
52493352Sgjelinek mixed_pools(zoneid_t zoneid)
52503352Sgjelinek {
52513352Sgjelinek 	DIR *dirp;
52523352Sgjelinek 	dirent_t *dent;
52533352Sgjelinek 	boolean_t mixed = B_FALSE;
52543352Sgjelinek 	boolean_t poolid_set = B_FALSE;
52553352Sgjelinek 	poolid_t last_poolid = 0;
52563352Sgjelinek 
52573352Sgjelinek 	if ((dirp = opendir("/proc")) == NULL) {
52583352Sgjelinek 		zerror(gettext("could not open /proc"));
52593352Sgjelinek 		return (B_FALSE);
52603352Sgjelinek 	}
52613352Sgjelinek 
52623352Sgjelinek 	while ((dent = readdir(dirp)) != NULL) {
52633352Sgjelinek 		int procfd;
52643352Sgjelinek 		psinfo_t ps;
52653352Sgjelinek 		char procpath[MAXPATHLEN];
52663352Sgjelinek 
52673352Sgjelinek 		if (dent->d_name[0] == '.')
52683352Sgjelinek 			continue;
52693352Sgjelinek 
52703352Sgjelinek 		(void) snprintf(procpath, sizeof (procpath), "/proc/%s/psinfo",
52713352Sgjelinek 		    dent->d_name);
52723352Sgjelinek 
52733352Sgjelinek 		if ((procfd = open(procpath, O_RDONLY)) == -1)
52743352Sgjelinek 			continue;
52753352Sgjelinek 
52763352Sgjelinek 		if (read(procfd, &ps, sizeof (ps)) == sizeof (psinfo_t)) {
52773352Sgjelinek 			/* skip processes in other zones and system processes */
52783352Sgjelinek 			if (zoneid != ps.pr_zoneid || ps.pr_flag & SSYS) {
52793352Sgjelinek 				(void) close(procfd);
52803352Sgjelinek 				continue;
52813352Sgjelinek 			}
52823352Sgjelinek 
52833352Sgjelinek 			if (poolid_set) {
52843352Sgjelinek 				if (ps.pr_poolid != last_poolid)
52853352Sgjelinek 					mixed = B_TRUE;
52863352Sgjelinek 			} else {
52873352Sgjelinek 				last_poolid = ps.pr_poolid;
52883352Sgjelinek 				poolid_set = B_TRUE;
52893352Sgjelinek 			}
52903352Sgjelinek 		}
52913352Sgjelinek 
52923352Sgjelinek 		(void) close(procfd);
52933352Sgjelinek 
52943352Sgjelinek 		if (mixed)
52953352Sgjelinek 			break;
52963352Sgjelinek 	}
52973352Sgjelinek 
52983352Sgjelinek 	(void) closedir(dirp);
52993352Sgjelinek 
53003352Sgjelinek 	return (mixed);
53013352Sgjelinek }
53023352Sgjelinek 
53033352Sgjelinek /*
53043352Sgjelinek  * Check if a persistent or temporary pool is configured for the zone.
53053352Sgjelinek  * This is currently only going to be called for the global zone from
53063352Sgjelinek  * apply_func but that could be generalized in the future.
53073352Sgjelinek  */
53083352Sgjelinek static boolean_t
53093352Sgjelinek pool_configured(zone_dochandle_t handle)
53103352Sgjelinek {
53113352Sgjelinek 	int err1, err2;
53123352Sgjelinek 	struct zone_psettab pset_tab;
53133352Sgjelinek 	char poolname[MAXPATHLEN];
53143352Sgjelinek 
53153352Sgjelinek 	err1 = zonecfg_lookup_pset(handle, &pset_tab);
53163352Sgjelinek 	err2 = zonecfg_get_pool(handle, poolname, sizeof (poolname));
53173352Sgjelinek 
53183352Sgjelinek 	if (err1 == Z_NO_ENTRY &&
53193352Sgjelinek 	    (err2 == Z_NO_ENTRY || (err2 == Z_OK && strlen(poolname) == 0)))
53203352Sgjelinek 		return (B_FALSE);
53213352Sgjelinek 
53223352Sgjelinek 	return (B_TRUE);
53233352Sgjelinek }
53243352Sgjelinek 
53253352Sgjelinek /*
53263247Sgjelinek  * This is an undocumented interface which is currently only used to apply
53273247Sgjelinek  * the global zone resource management settings when the system boots.
53283247Sgjelinek  * This function does not yet properly handle updating a running system so
53293247Sgjelinek  * any projects running in the zone would be trashed if this function
53303247Sgjelinek  * were to run after the zone had booted.  It also does not reset any
53313247Sgjelinek  * rctl settings that were removed from zonecfg.  There is still work to be
53323247Sgjelinek  * done before we can properly support dynamically updating the resource
53333247Sgjelinek  * management settings for a running zone (global or non-global).  Thus, this
53343247Sgjelinek  * functionality is undocumented for now.
53353247Sgjelinek  */
53363247Sgjelinek /* ARGSUSED */
53373247Sgjelinek static int
53383247Sgjelinek apply_func(int argc, char *argv[])
53393247Sgjelinek {
53403247Sgjelinek 	int err;
53413247Sgjelinek 	int res = Z_OK;
53423247Sgjelinek 	priv_set_t *privset;
53433247Sgjelinek 	zoneid_t zoneid;
53443247Sgjelinek 	zone_dochandle_t handle;
53453247Sgjelinek 	struct zone_mcaptab mcap;
53463247Sgjelinek 	char pool_err[128];
53473247Sgjelinek 
53483247Sgjelinek 	zoneid = getzoneid();
53493247Sgjelinek 
53503247Sgjelinek 	if (zonecfg_in_alt_root() || zoneid != GLOBAL_ZONEID ||
53513247Sgjelinek 	    target_zone == NULL || strcmp(target_zone, GLOBAL_ZONENAME) != 0)
53523247Sgjelinek 		return (usage(B_FALSE));
53533247Sgjelinek 
53543247Sgjelinek 	if ((privset = priv_allocset()) == NULL) {
53553247Sgjelinek 		zerror(gettext("%s failed"), "priv_allocset");
53563247Sgjelinek 		return (Z_ERR);
53573247Sgjelinek 	}
53583247Sgjelinek 
53593247Sgjelinek 	if (getppriv(PRIV_EFFECTIVE, privset) != 0) {
53603247Sgjelinek 		zerror(gettext("%s failed"), "getppriv");
53613247Sgjelinek 		priv_freeset(privset);
53623247Sgjelinek 		return (Z_ERR);
53633247Sgjelinek 	}
53643247Sgjelinek 
53653247Sgjelinek 	if (priv_isfullset(privset) == B_FALSE) {
53663247Sgjelinek 		(void) usage(B_FALSE);
53673247Sgjelinek 		priv_freeset(privset);
53683247Sgjelinek 		return (Z_ERR);
53693247Sgjelinek 	}
53703247Sgjelinek 	priv_freeset(privset);
53713247Sgjelinek 
53723247Sgjelinek 	if ((handle = zonecfg_init_handle()) == NULL) {
53733247Sgjelinek 		zperror(cmd_to_str(CMD_APPLY), B_TRUE);
53743247Sgjelinek 		return (Z_ERR);
53753247Sgjelinek 	}
53763247Sgjelinek 
53773247Sgjelinek 	if ((err = zonecfg_get_handle(target_zone, handle)) != Z_OK) {
53783247Sgjelinek 		errno = err;
53793247Sgjelinek 		zperror(cmd_to_str(CMD_APPLY), B_TRUE);
53803247Sgjelinek 		zonecfg_fini_handle(handle);
53813247Sgjelinek 		return (Z_ERR);
53823247Sgjelinek 	}
53833247Sgjelinek 
53843247Sgjelinek 	/* specific error msgs are printed within apply_rctls */
53853247Sgjelinek 	if ((err = zonecfg_apply_rctls(target_zone, handle)) != Z_OK) {
53863247Sgjelinek 		errno = err;
53873247Sgjelinek 		zperror(cmd_to_str(CMD_APPLY), B_TRUE);
53883247Sgjelinek 		res = Z_ERR;
53893247Sgjelinek 	}
53903247Sgjelinek 
53913247Sgjelinek 	if ((err = check_cpu_shares_sched(handle)) != Z_OK)
53923247Sgjelinek 		res = Z_ERR;
53933247Sgjelinek 
53943352Sgjelinek 	if (pool_configured(handle)) {
53953352Sgjelinek 		if (mixed_pools(zoneid)) {
53963352Sgjelinek 			zerror(gettext("Zone is using multiple resource "
53973352Sgjelinek 			    "pools.  The pool\nconfiguration cannot be "
53983352Sgjelinek 			    "applied without rebooting."));
53993352Sgjelinek 			res = Z_ERR;
54003352Sgjelinek 		} else {
54013352Sgjelinek 
54023352Sgjelinek 			/*
54033352Sgjelinek 			 * The next two blocks of code attempt to set up
54043352Sgjelinek 			 * temporary pools as well as persistent pools.  In
54053352Sgjelinek 			 * both cases we call the functions unconditionally.
54063352Sgjelinek 			 * Within each funtion the code will check if the zone
54073352Sgjelinek 			 * is actually configured for a temporary pool or
54083352Sgjelinek 			 * persistent pool and just return if there is nothing
54093352Sgjelinek 			 * to do.
54103352Sgjelinek 			 */
54113352Sgjelinek 			if ((err = zonecfg_bind_tmp_pool(handle, zoneid,
54123352Sgjelinek 			    pool_err, sizeof (pool_err))) != Z_OK) {
54133352Sgjelinek 				if (err == Z_POOL || err == Z_POOL_CREATE ||
54143352Sgjelinek 				    err == Z_POOL_BIND)
54153352Sgjelinek 					zerror("%s: %s", zonecfg_strerror(err),
54163352Sgjelinek 					    pool_err);
54173352Sgjelinek 				else
54183352Sgjelinek 					zerror(gettext("could not bind zone to "
54193352Sgjelinek 					    "temporary pool: %s"),
54203352Sgjelinek 					    zonecfg_strerror(err));
54213352Sgjelinek 				res = Z_ERR;
54223352Sgjelinek 			}
54233352Sgjelinek 
54243352Sgjelinek 			if ((err = zonecfg_bind_pool(handle, zoneid, pool_err,
54253352Sgjelinek 			    sizeof (pool_err))) != Z_OK) {
54263352Sgjelinek 				if (err == Z_POOL || err == Z_POOL_BIND)
54273352Sgjelinek 					zerror("%s: %s", zonecfg_strerror(err),
54283352Sgjelinek 					    pool_err);
54293352Sgjelinek 				else
54303352Sgjelinek 					zerror("%s", zonecfg_strerror(err));
54313352Sgjelinek 			}
54323352Sgjelinek 		}
54333247Sgjelinek 	}
54343247Sgjelinek 
54353247Sgjelinek 	/*
54363247Sgjelinek 	 * If a memory cap is configured, set the cap in the kernel using
54373247Sgjelinek 	 * zone_setattr() and make sure the rcapd SMF service is enabled.
54383247Sgjelinek 	 */
54393247Sgjelinek 	if (zonecfg_getmcapent(handle, &mcap) == Z_OK) {
54403247Sgjelinek 		uint64_t num;
54413247Sgjelinek 		char smf_err[128];
54423247Sgjelinek 
54433247Sgjelinek 		num = (uint64_t)strtoll(mcap.zone_physmem_cap, NULL, 10);
54443247Sgjelinek 		if (zone_setattr(zoneid, ZONE_ATTR_PHYS_MCAP, &num, 0) == -1) {
54453247Sgjelinek 			zerror(gettext("could not set zone memory cap"));
54463247Sgjelinek 			res = Z_ERR;
54473247Sgjelinek 		}
54483247Sgjelinek 
54493247Sgjelinek 		if (zonecfg_enable_rcapd(smf_err, sizeof (smf_err)) != Z_OK) {
54503247Sgjelinek 			zerror(gettext("enabling system/rcap service failed: "
54513247Sgjelinek 			    "%s"), smf_err);
54523247Sgjelinek 			res = Z_ERR;
54533247Sgjelinek 		}
54543247Sgjelinek 	}
54553247Sgjelinek 
54563247Sgjelinek 	zonecfg_fini_handle(handle);
54573247Sgjelinek 
54583247Sgjelinek 	return (res);
54593247Sgjelinek }
54603247Sgjelinek 
54612303Scarlsonj static int
54620Sstevel@tonic-gate help_func(int argc, char *argv[])
54630Sstevel@tonic-gate {
54640Sstevel@tonic-gate 	int arg, cmd_num;
54650Sstevel@tonic-gate 
54660Sstevel@tonic-gate 	if (argc == 0) {
54670Sstevel@tonic-gate 		(void) usage(B_TRUE);
54680Sstevel@tonic-gate 		return (Z_OK);
54690Sstevel@tonic-gate 	}
54700Sstevel@tonic-gate 	optind = 0;
54710Sstevel@tonic-gate 	if ((arg = getopt(argc, argv, "?")) != EOF) {
54720Sstevel@tonic-gate 		switch (arg) {
54730Sstevel@tonic-gate 		case '?':
54740Sstevel@tonic-gate 			sub_usage(SHELP_HELP, CMD_HELP);
54750Sstevel@tonic-gate 			return (optopt == '?' ? Z_OK : Z_USAGE);
54760Sstevel@tonic-gate 		default:
54770Sstevel@tonic-gate 			sub_usage(SHELP_HELP, CMD_HELP);
54780Sstevel@tonic-gate 			return (Z_USAGE);
54790Sstevel@tonic-gate 		}
54800Sstevel@tonic-gate 	}
54810Sstevel@tonic-gate 	while (optind < argc) {
5482988Scarlsonj 		/* Private commands have NULL short_usage; omit them */
5483988Scarlsonj 		if ((cmd_num = cmd_match(argv[optind])) < 0 ||
5484988Scarlsonj 		    cmdtab[cmd_num].short_usage == NULL) {
54850Sstevel@tonic-gate 			sub_usage(SHELP_HELP, CMD_HELP);
54860Sstevel@tonic-gate 			return (Z_USAGE);
54870Sstevel@tonic-gate 		}
54880Sstevel@tonic-gate 		sub_usage(cmdtab[cmd_num].short_usage, cmd_num);
54890Sstevel@tonic-gate 		optind++;
54900Sstevel@tonic-gate 	}
54910Sstevel@tonic-gate 	return (Z_OK);
54920Sstevel@tonic-gate }
54930Sstevel@tonic-gate 
54940Sstevel@tonic-gate /*
54950Sstevel@tonic-gate  * Returns: CMD_MIN thru CMD_MAX on success, -1 on error
54960Sstevel@tonic-gate  */
54970Sstevel@tonic-gate 
54980Sstevel@tonic-gate static int
54990Sstevel@tonic-gate cmd_match(char *cmd)
55000Sstevel@tonic-gate {
55010Sstevel@tonic-gate 	int i;
55020Sstevel@tonic-gate 
55030Sstevel@tonic-gate 	for (i = CMD_MIN; i <= CMD_MAX; i++) {
55040Sstevel@tonic-gate 		/* return only if there is an exact match */
55050Sstevel@tonic-gate 		if (strcmp(cmd, cmdtab[i].cmd_name) == 0)
55060Sstevel@tonic-gate 			return (cmdtab[i].cmd_num);
55070Sstevel@tonic-gate 	}
55080Sstevel@tonic-gate 	return (-1);
55090Sstevel@tonic-gate }
55100Sstevel@tonic-gate 
55110Sstevel@tonic-gate static int
55120Sstevel@tonic-gate parse_and_run(int argc, char *argv[])
55130Sstevel@tonic-gate {
55140Sstevel@tonic-gate 	int i = cmd_match(argv[0]);
55150Sstevel@tonic-gate 
55160Sstevel@tonic-gate 	if (i < 0)
55170Sstevel@tonic-gate 		return (usage(B_FALSE));
55180Sstevel@tonic-gate 	return (cmdtab[i].handler(argc - 1, &(argv[1])));
55190Sstevel@tonic-gate }
55200Sstevel@tonic-gate 
55210Sstevel@tonic-gate static char *
55220Sstevel@tonic-gate get_execbasename(char *execfullname)
55230Sstevel@tonic-gate {
55240Sstevel@tonic-gate 	char *last_slash, *execbasename;
55250Sstevel@tonic-gate 
55260Sstevel@tonic-gate 	/* guard against '/' at end of command invocation */
55270Sstevel@tonic-gate 	for (;;) {
55280Sstevel@tonic-gate 		last_slash = strrchr(execfullname, '/');
55290Sstevel@tonic-gate 		if (last_slash == NULL) {
55300Sstevel@tonic-gate 			execbasename = execfullname;
55310Sstevel@tonic-gate 			break;
55320Sstevel@tonic-gate 		} else {
55330Sstevel@tonic-gate 			execbasename = last_slash + 1;
55340Sstevel@tonic-gate 			if (*execbasename == '\0') {
55350Sstevel@tonic-gate 				*last_slash = '\0';
55360Sstevel@tonic-gate 				continue;
55370Sstevel@tonic-gate 			}
55380Sstevel@tonic-gate 			break;
55390Sstevel@tonic-gate 		}
55400Sstevel@tonic-gate 	}
55410Sstevel@tonic-gate 	return (execbasename);
55420Sstevel@tonic-gate }
55430Sstevel@tonic-gate 
55440Sstevel@tonic-gate int
55450Sstevel@tonic-gate main(int argc, char **argv)
55460Sstevel@tonic-gate {
55470Sstevel@tonic-gate 	int arg;
55480Sstevel@tonic-gate 	zoneid_t zid;
5549766Scarlsonj 	struct stat st;
55502712Snn35248 	char *zone_lock_env;
55512712Snn35248 	int err;
55520Sstevel@tonic-gate 
55530Sstevel@tonic-gate 	if ((locale = setlocale(LC_ALL, "")) == NULL)
55540Sstevel@tonic-gate 		locale = "C";
55550Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
55560Sstevel@tonic-gate 	setbuf(stdout, NULL);
55570Sstevel@tonic-gate 	(void) sigset(SIGHUP, SIG_IGN);
55580Sstevel@tonic-gate 	execname = get_execbasename(argv[0]);
55590Sstevel@tonic-gate 	target_zone = NULL;
55600Sstevel@tonic-gate 	if (chdir("/") != 0) {
55610Sstevel@tonic-gate 		zerror(gettext("could not change directory to /."));
55620Sstevel@tonic-gate 		exit(Z_ERR);
55630Sstevel@tonic-gate 	}
55640Sstevel@tonic-gate 
55652082Seschrock 	if (init_zfs() != Z_OK)
55662082Seschrock 		exit(Z_ERR);
55672082Seschrock 
55682303Scarlsonj 	while ((arg = getopt(argc, argv, "?u:z:R:")) != EOF) {
55690Sstevel@tonic-gate 		switch (arg) {
55700Sstevel@tonic-gate 		case '?':
55710Sstevel@tonic-gate 			return (usage(B_TRUE));
55722303Scarlsonj 		case 'u':
55732303Scarlsonj 			target_uuid = optarg;
55742303Scarlsonj 			break;
55750Sstevel@tonic-gate 		case 'z':
55760Sstevel@tonic-gate 			target_zone = optarg;
55770Sstevel@tonic-gate 			break;
5578766Scarlsonj 		case 'R':	/* private option for admin/install use */
5579766Scarlsonj 			if (*optarg != '/') {
5580766Scarlsonj 				zerror(gettext("root path must be absolute."));
5581766Scarlsonj 				exit(Z_ERR);
5582766Scarlsonj 			}
5583766Scarlsonj 			if (stat(optarg, &st) == -1 || !S_ISDIR(st.st_mode)) {
5584766Scarlsonj 				zerror(
5585766Scarlsonj 				    gettext("root path must be a directory."));
5586766Scarlsonj 				exit(Z_ERR);
5587766Scarlsonj 			}
5588766Scarlsonj 			zonecfg_set_root(optarg);
5589766Scarlsonj 			break;
55900Sstevel@tonic-gate 		default:
55910Sstevel@tonic-gate 			return (usage(B_FALSE));
55920Sstevel@tonic-gate 		}
55930Sstevel@tonic-gate 	}
55940Sstevel@tonic-gate 
55950Sstevel@tonic-gate 	if (optind >= argc)
55960Sstevel@tonic-gate 		return (usage(B_FALSE));
55972303Scarlsonj 
55982303Scarlsonj 	if (target_uuid != NULL && *target_uuid != '\0') {
55992303Scarlsonj 		uuid_t uuid;
56002303Scarlsonj 		static char newtarget[ZONENAME_MAX];
56012303Scarlsonj 
56022303Scarlsonj 		if (uuid_parse(target_uuid, uuid) == -1) {
56032303Scarlsonj 			zerror(gettext("illegal UUID value specified"));
56042303Scarlsonj 			exit(Z_ERR);
56052303Scarlsonj 		}
56062303Scarlsonj 		if (zonecfg_get_name_by_uuid(uuid, newtarget,
56072303Scarlsonj 		    sizeof (newtarget)) == Z_OK)
56082303Scarlsonj 			target_zone = newtarget;
56092303Scarlsonj 	}
56102303Scarlsonj 
56110Sstevel@tonic-gate 	if (target_zone != NULL && zone_get_id(target_zone, &zid) != 0) {
56120Sstevel@tonic-gate 		errno = Z_NO_ZONE;
56130Sstevel@tonic-gate 		zperror(target_zone, B_TRUE);
56140Sstevel@tonic-gate 		exit(Z_ERR);
56150Sstevel@tonic-gate 	}
56162712Snn35248 
56172712Snn35248 	/*
56182712Snn35248 	 * See if we have inherited the right to manipulate this zone from
56192712Snn35248 	 * a zoneadm instance in our ancestry.  If so, set zone_lock_cnt to
56202712Snn35248 	 * indicate it.  If not, make that explicit in our environment.
56212712Snn35248 	 */
56227089Sgjelinek 	zonecfg_init_lock_file(target_zone, &zone_lock_env);
56237089Sgjelinek 	if (zone_lock_env != NULL)
56242712Snn35248 		zoneadm_is_nested = B_TRUE;
56252712Snn35248 
56262712Snn35248 	/*
56272712Snn35248 	 * If we are going to be operating on a single zone, retrieve its
56282712Snn35248 	 * brand type and determine whether it is native or not.
56292712Snn35248 	 */
56302712Snn35248 	if ((target_zone != NULL) &&
56318057SJordan.Vaughan@Sun.com 	    (strcmp(target_zone, GLOBAL_ZONENAME) != 0)) {
56322712Snn35248 		if (zone_get_brand(target_zone, target_brand,
56332712Snn35248 		    sizeof (target_brand)) != Z_OK) {
56342712Snn35248 			zerror(gettext("missing or invalid brand"));
56352712Snn35248 			exit(Z_ERR);
56362712Snn35248 		}
56372712Snn35248 	}
56382712Snn35248 
56392712Snn35248 	err = parse_and_run(argc - optind, &argv[optind]);
56402712Snn35248 
56412712Snn35248 	return (err);
56420Sstevel@tonic-gate }
5643