xref: /onnv-gate/usr/src/cmd/zoneadm/zoneadm.c (revision 9128:ac1f68909e54)
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 
4258942Sgerald.jelinek@sun.com 	/* Skip a zone that shutdown while we were collecting data. */
4268942Sgerald.jelinek@sun.com 	if (zent->zname[0] == '\0')
4278942Sgerald.jelinek@sun.com 		return;
4288942Sgerald.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;
4788942Sgerald.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;
5518942Sgerald.jelinek@sun.com 		return (Z_OK);
5528942Sgerald.jelinek@sun.com 	}
5538942Sgerald.jelinek@sun.com 
5548942Sgerald.jelinek@sun.com 	/*
5558942Sgerald.jelinek@sun.com 	 * There is a race condition where the zone could boot while
5568942Sgerald.jelinek@sun.com 	 * we're walking the index file.  In this case the zone state
5578942Sgerald.jelinek@sun.com 	 * could be seen as running from the call above, but the zoneid
5588942Sgerald.jelinek@sun.com 	 * would be undefined.
5598942Sgerald.jelinek@sun.com 	 *
5608942Sgerald.jelinek@sun.com 	 * There is also a race condition where the zone could shutdown after
5618942Sgerald.jelinek@sun.com 	 * we got its running state above.  This is also not an error and
5628942Sgerald.jelinek@sun.com 	 * we fall back to getting the ziptype from the zone configuration.
5638942Sgerald.jelinek@sun.com 	 */
5648942Sgerald.jelinek@sun.com 	if (zent->zstate_num == ZONE_STATE_RUNNING &&
5658942Sgerald.jelinek@sun.com 	    zid != ZONE_ID_UNDEFINED) {
5668942Sgerald.jelinek@sun.com 		ushort_t flags;
5678942Sgerald.jelinek@sun.com 
5688942Sgerald.jelinek@sun.com 		if (zone_getattr(zid, ZONE_ATTR_FLAGS, &flags,
5698942Sgerald.jelinek@sun.com 		    sizeof (flags)) >= 0) {
5703448Sdh155122 			if (flags & ZF_NET_EXCL)
5713448Sdh155122 				zent->ziptype = ZS_EXCLUSIVE;
5723448Sdh155122 			else
5733448Sdh155122 				zent->ziptype = ZS_SHARED;
5748942Sgerald.jelinek@sun.com 			return (Z_OK);
5753448Sdh155122 		}
5763448Sdh155122 	}
5773448Sdh155122 
5788942Sgerald.jelinek@sun.com 	if ((handle = zonecfg_init_handle()) == NULL) {
5798942Sgerald.jelinek@sun.com 		zperror2(zent->zname, gettext("could not init handle"));
5808942Sgerald.jelinek@sun.com 		return (Z_ERR);
5818942Sgerald.jelinek@sun.com 	}
5828942Sgerald.jelinek@sun.com 	if ((err = zonecfg_get_handle(zent->zname, handle)) != Z_OK) {
5838942Sgerald.jelinek@sun.com 		zperror2(zent->zname, gettext("could not get handle"));
5848942Sgerald.jelinek@sun.com 		zonecfg_fini_handle(handle);
5858942Sgerald.jelinek@sun.com 		return (Z_ERR);
5868942Sgerald.jelinek@sun.com 	}
5878942Sgerald.jelinek@sun.com 
5888942Sgerald.jelinek@sun.com 	if ((err = zonecfg_get_iptype(handle, &zent->ziptype)) != Z_OK) {
5898942Sgerald.jelinek@sun.com 		zperror2(zent->zname, gettext("could not get ip-type"));
5908942Sgerald.jelinek@sun.com 		zonecfg_fini_handle(handle);
5918942Sgerald.jelinek@sun.com 		return (Z_ERR);
5928942Sgerald.jelinek@sun.com 	}
5938942Sgerald.jelinek@sun.com 	zonecfg_fini_handle(handle);
5948942Sgerald.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.
6048942Sgerald.jelinek@sun.com  *
6058942Sgerald.jelinek@sun.com  * Note that the data about running zones can change while this function
6068942Sgerald.jelinek@sun.com  * is running, so its possible that the list of zones will have empty slots
6078942Sgerald.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) {
6608942Sgerald.jelinek@sun.com 			/*
6618942Sgerald.jelinek@sun.com 			 * There is a race condition where the zone may have
6628942Sgerald.jelinek@sun.com 			 * shutdown since we retrieved the number of running
6638942Sgerald.jelinek@sun.com 			 * zones above.  This is not an error, there will be
6648942Sgerald.jelinek@sun.com 			 * an empty slot at the end of the list.
6658942Sgerald.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) {
6878942Sgerald.jelinek@sun.com 			/*
6888942Sgerald.jelinek@sun.com 			 * There is a race condition where the zone may have
6898942Sgerald.jelinek@sun.com 			 * shutdown since we retrieved the number of running
6908942Sgerald.jelinek@sun.com 			 * zones above.  This is not an error, there will be
6918942Sgerald.jelinek@sun.com 			 * an empty slot at the end of the list.
6928942Sgerald.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 	}
2777*9128Sgerald.jelinek@sun.com 	if (cmd_num != CMD_ATTACH &&
2778*9128Sgerald.jelinek@sun.com 	    validate_zonepath(zonepath, cmd_num) != Z_OK) {
27792078Sgjelinek 		(void) fprintf(stderr, gettext("could not verify zonepath %s "
27802078Sgjelinek 		    "because of the above errors.\n"), zonepath);
27812078Sgjelinek 		return_code = Z_ERR;
27822078Sgjelinek 	}
27832078Sgjelinek 
27843339Szt129084 	if (verify_handle(cmd_num, handle, argv) != Z_OK)
27852078Sgjelinek 		return_code = Z_ERR;
27862078Sgjelinek 
27870Sstevel@tonic-gate 	zonecfg_fini_handle(handle);
27880Sstevel@tonic-gate 	if (return_code == Z_ERR)
27890Sstevel@tonic-gate 		(void) fprintf(stderr,
27900Sstevel@tonic-gate 		    gettext("%s: zone %s failed to verify\n"),
27910Sstevel@tonic-gate 		    execname, target_zone);
27920Sstevel@tonic-gate 	return (return_code);
27930Sstevel@tonic-gate }
27940Sstevel@tonic-gate 
27950Sstevel@tonic-gate static int
27960Sstevel@tonic-gate verify_func(int argc, char *argv[])
27970Sstevel@tonic-gate {
27980Sstevel@tonic-gate 	int arg;
27990Sstevel@tonic-gate 
28000Sstevel@tonic-gate 	optind = 0;
28010Sstevel@tonic-gate 	if ((arg = getopt(argc, argv, "?")) != EOF) {
28020Sstevel@tonic-gate 		switch (arg) {
28030Sstevel@tonic-gate 		case '?':
28040Sstevel@tonic-gate 			sub_usage(SHELP_VERIFY, CMD_VERIFY);
28050Sstevel@tonic-gate 			return (optopt == '?' ? Z_OK : Z_USAGE);
28060Sstevel@tonic-gate 		default:
28070Sstevel@tonic-gate 			sub_usage(SHELP_VERIFY, CMD_VERIFY);
28080Sstevel@tonic-gate 			return (Z_USAGE);
28090Sstevel@tonic-gate 		}
28100Sstevel@tonic-gate 	}
28110Sstevel@tonic-gate 	if (argc > optind) {
28120Sstevel@tonic-gate 		sub_usage(SHELP_VERIFY, CMD_VERIFY);
28130Sstevel@tonic-gate 		return (Z_USAGE);
28140Sstevel@tonic-gate 	}
28152712Snn35248 	if (sanity_check(target_zone, CMD_VERIFY, B_FALSE, B_FALSE, B_FALSE)
28162712Snn35248 	    != Z_OK)
28170Sstevel@tonic-gate 		return (Z_ERR);
28183339Szt129084 	return (verify_details(CMD_VERIFY, argv));
28190Sstevel@tonic-gate }
28200Sstevel@tonic-gate 
28212712Snn35248 static int
28227089Sgjelinek addoptions(char *buf, char *argv[], size_t len)
28237089Sgjelinek {
28247089Sgjelinek 	int i = 0;
28257089Sgjelinek 
28267089Sgjelinek 	if (buf[0] == '\0')
28277089Sgjelinek 		return (Z_OK);
28287089Sgjelinek 
28297089Sgjelinek 	while (argv[i] != NULL) {
28307089Sgjelinek 		if (strlcat(buf, " ", len) >= len ||
28317089Sgjelinek 		    strlcat(buf, argv[i++], len) >= len) {
28327089Sgjelinek 			zerror("Command line too long");
28337089Sgjelinek 			return (Z_ERR);
28347089Sgjelinek 		}
28357089Sgjelinek 	}
28367089Sgjelinek 
28377089Sgjelinek 	return (Z_OK);
28387089Sgjelinek }
28397089Sgjelinek 
28407089Sgjelinek static int
28412712Snn35248 addopt(char *buf, int opt, char *optarg, size_t bufsize)
28422712Snn35248 {
28432712Snn35248 	char optstring[4];
28442712Snn35248 
28452712Snn35248 	if (opt > 0)
28462712Snn35248 		(void) sprintf(optstring, " -%c", opt);
28472712Snn35248 	else
28482712Snn35248 		(void) strcpy(optstring, " ");
28492712Snn35248 
28502712Snn35248 	if ((strlcat(buf, optstring, bufsize) > bufsize))
28512712Snn35248 		return (Z_ERR);
28527089Sgjelinek 
28532712Snn35248 	if ((optarg != NULL) && (strlcat(buf, optarg, bufsize) > bufsize))
28542712Snn35248 		return (Z_ERR);
28557089Sgjelinek 
28562712Snn35248 	return (Z_OK);
28572712Snn35248 }
28580Sstevel@tonic-gate 
28597089Sgjelinek /* ARGSUSED */
28600Sstevel@tonic-gate static int
28610Sstevel@tonic-gate install_func(int argc, char *argv[])
28620Sstevel@tonic-gate {
28632712Snn35248 	char cmdbuf[MAXPATHLEN];
28644586Sgjelinek 	char postcmdbuf[MAXPATHLEN];
28650Sstevel@tonic-gate 	int lockfd;
28662712Snn35248 	int arg, err, subproc_err;
28670Sstevel@tonic-gate 	char zonepath[MAXPATHLEN];
28682727Sedp 	brand_handle_t bh = NULL;
28690Sstevel@tonic-gate 	int status;
28701867Sgjelinek 	boolean_t nodataset = B_FALSE;
28714586Sgjelinek 	boolean_t do_postinstall = B_FALSE;
28727089Sgjelinek 	boolean_t brand_help = B_FALSE;
28732712Snn35248 	char opts[128];
28742712Snn35248 
28752712Snn35248 	if (target_zone == NULL) {
28762712Snn35248 		sub_usage(SHELP_INSTALL, CMD_INSTALL);
28772712Snn35248 		return (Z_USAGE);
28782712Snn35248 	}
28790Sstevel@tonic-gate 
2880766Scarlsonj 	if (zonecfg_in_alt_root()) {
2881766Scarlsonj 		zerror(gettext("cannot install zone in alternate root"));
2882766Scarlsonj 		return (Z_ERR);
2883766Scarlsonj 	}
2884766Scarlsonj 
28852712Snn35248 	if ((err = zone_get_zonepath(target_zone, zonepath,
28862712Snn35248 	    sizeof (zonepath))) != Z_OK) {
28872712Snn35248 		errno = err;
28882712Snn35248 		zperror2(target_zone, gettext("could not get zone path"));
28892712Snn35248 		return (Z_ERR);
28902712Snn35248 	}
28912712Snn35248 
28922712Snn35248 	/* Fetch the install command from the brand configuration.  */
28932727Sedp 	if ((bh = brand_open(target_brand)) == NULL) {
28942712Snn35248 		zerror(gettext("missing or invalid brand"));
28952712Snn35248 		return (Z_ERR);
28962712Snn35248 	}
28972712Snn35248 
28987089Sgjelinek 	if (get_hook(bh, cmdbuf, sizeof (cmdbuf), brand_get_install,
28997089Sgjelinek 	    target_zone, zonepath) != Z_OK) {
29002712Snn35248 		zerror("invalid brand configuration: missing install resource");
29012727Sedp 		brand_close(bh);
29022712Snn35248 		return (Z_ERR);
29032712Snn35248 	}
29042712Snn35248 
29057089Sgjelinek 	if (get_hook(bh, postcmdbuf, sizeof (postcmdbuf), brand_get_postinstall,
29067089Sgjelinek 	    target_zone, zonepath) != Z_OK) {
29074586Sgjelinek 		zerror("invalid brand configuration: missing postinstall "
29084586Sgjelinek 		    "resource");
29094586Sgjelinek 		brand_close(bh);
29104586Sgjelinek 		return (Z_ERR);
29117089Sgjelinek 	}
29127089Sgjelinek 
29137089Sgjelinek 	if (postcmdbuf[0] != '\0')
29144586Sgjelinek 		do_postinstall = B_TRUE;
29154586Sgjelinek 
29162712Snn35248 	(void) strcpy(opts, "?x:");
29177089Sgjelinek 	/*
29187089Sgjelinek 	 * Fetch the list of recognized command-line options from
29197089Sgjelinek 	 * the brand configuration file.
29207089Sgjelinek 	 */
29217089Sgjelinek 	if (brand_get_installopts(bh, opts + strlen(opts),
29227089Sgjelinek 	    sizeof (opts) - strlen(opts)) != 0) {
29237089Sgjelinek 		zerror("invalid brand configuration: missing "
29247089Sgjelinek 		    "install options resource");
29257089Sgjelinek 		brand_close(bh);
29267089Sgjelinek 		return (Z_ERR);
29277089Sgjelinek 	}
29287089Sgjelinek 
29292727Sedp 	brand_close(bh);
29302712Snn35248 
29317089Sgjelinek 	if (cmdbuf[0] == '\0') {
29327089Sgjelinek 		zerror("Missing brand install command");
29337089Sgjelinek 		return (Z_ERR);
29347089Sgjelinek 	}
29357089Sgjelinek 
29367089Sgjelinek 	/* Check the argv string for args we handle internally */
29370Sstevel@tonic-gate 	optind = 0;
29387089Sgjelinek 	opterr = 0;
29392712Snn35248 	while ((arg = getopt(argc, argv, opts)) != EOF) {
29400Sstevel@tonic-gate 		switch (arg) {
29410Sstevel@tonic-gate 		case '?':
29427089Sgjelinek 			if (optopt == '?') {
29431867Sgjelinek 				sub_usage(SHELP_INSTALL, CMD_INSTALL);
29447089Sgjelinek 				brand_help = B_TRUE;
29452712Snn35248 			}
29467089Sgjelinek 			/* Ignore unknown options - may be brand specific. */
29477089Sgjelinek 			break;
29487089Sgjelinek 		case 'x':
29497089Sgjelinek 			/* Handle this option internally, don't pass to brand */
29507089Sgjelinek 			if (strcmp(optarg, "nodataset") == 0) {
29517089Sgjelinek 				/* Handle this option internally */
29527089Sgjelinek 				nodataset = B_TRUE;
29532712Snn35248 			}
29547089Sgjelinek 			continue;
29557089Sgjelinek 		default:
29567089Sgjelinek 			/* Ignore unknown options - may be brand specific. */
29572712Snn35248 			break;
29580Sstevel@tonic-gate 		}
29597089Sgjelinek 
29607089Sgjelinek 		/*
29617089Sgjelinek 		 * Append the option to the command line passed to the
29627089Sgjelinek 		 * brand-specific install and postinstall routines.
29637089Sgjelinek 		 */
29647089Sgjelinek 		if (addopt(cmdbuf, optopt, optarg, sizeof (cmdbuf)) != Z_OK) {
29657089Sgjelinek 			zerror("Install command line too long");
29667089Sgjelinek 			return (Z_ERR);
29677089Sgjelinek 		}
29687089Sgjelinek 		if (addopt(postcmdbuf, optopt, optarg, sizeof (postcmdbuf))
29697089Sgjelinek 		    != Z_OK) {
29707089Sgjelinek 			zerror("Post-Install command line too long");
29717089Sgjelinek 			return (Z_ERR);
29727089Sgjelinek 		}
29737089Sgjelinek 	}
29747089Sgjelinek 
29757089Sgjelinek 	for (; optind < argc; optind++) {
29767089Sgjelinek 		if (addopt(cmdbuf, 0, argv[optind], sizeof (cmdbuf)) != Z_OK) {
29777089Sgjelinek 			zerror("Install command line too long");
29787089Sgjelinek 			return (Z_ERR);
29797089Sgjelinek 		}
29807089Sgjelinek 
29817089Sgjelinek 		if (addopt(postcmdbuf, 0, argv[optind], sizeof (postcmdbuf))
29827089Sgjelinek 		    != Z_OK) {
29837089Sgjelinek 			zerror("Post-Install command line too long");
29847089Sgjelinek 			return (Z_ERR);
29852712Snn35248 		}
29862712Snn35248 	}
29872712Snn35248 
29887089Sgjelinek 	if (!brand_help) {
29897089Sgjelinek 		if (sanity_check(target_zone, CMD_INSTALL, B_FALSE, B_TRUE,
29907089Sgjelinek 		    B_FALSE) != Z_OK)
29917089Sgjelinek 			return (Z_ERR);
29927089Sgjelinek 		if (verify_details(CMD_INSTALL, argv) != Z_OK)
29937089Sgjelinek 			return (Z_ERR);
29947089Sgjelinek 
29957089Sgjelinek 		if (zonecfg_grab_lock_file(target_zone, &lockfd) != Z_OK) {
29967089Sgjelinek 			zerror(gettext("another %s may have an operation in "
29977089Sgjelinek 			    "progress."), "zoneadm");
29987089Sgjelinek 			return (Z_ERR);
29997089Sgjelinek 		}
30007089Sgjelinek 		err = zone_set_state(target_zone, ZONE_STATE_INCOMPLETE);
30017089Sgjelinek 		if (err != Z_OK) {
30027089Sgjelinek 			errno = err;
30037089Sgjelinek 			zperror2(target_zone, gettext("could not set state"));
30047089Sgjelinek 			goto done;
30057089Sgjelinek 		}
30067089Sgjelinek 
30077089Sgjelinek 		if (!nodataset)
30087089Sgjelinek 			create_zfs_zonepath(zonepath);
30097089Sgjelinek 	}
30107089Sgjelinek 
30117089Sgjelinek 	status = do_subproc_interactive(cmdbuf);
30122712Snn35248 	if ((subproc_err =
30132712Snn35248 	    subproc_status(gettext("brand-specific installation"), status,
30142712Snn35248 	    B_FALSE)) != ZONE_SUBPROC_OK) {
30157089Sgjelinek 		if (subproc_err == ZONE_SUBPROC_USAGE && !brand_help) {
30167089Sgjelinek 			sub_usage(SHELP_INSTALL, CMD_INSTALL);
30177089Sgjelinek 			zonecfg_release_lock_file(target_zone, lockfd);
30187089Sgjelinek 			return (Z_ERR);
30197089Sgjelinek 		}
30202712Snn35248 		err = Z_ERR;
30210Sstevel@tonic-gate 		goto done;
30222712Snn35248 	}
30230Sstevel@tonic-gate 
30247089Sgjelinek 	if (brand_help)
30257089Sgjelinek 		return (Z_OK);
30267089Sgjelinek 
30270Sstevel@tonic-gate 	if ((err = zone_set_state(target_zone, ZONE_STATE_INSTALLED)) != Z_OK) {
30280Sstevel@tonic-gate 		errno = err;
30290Sstevel@tonic-gate 		zperror2(target_zone, gettext("could not set state"));
30300Sstevel@tonic-gate 		goto done;
30310Sstevel@tonic-gate 	}
30320Sstevel@tonic-gate 
30334586Sgjelinek 	if (do_postinstall) {
30344586Sgjelinek 		status = do_subproc(postcmdbuf);
30354586Sgjelinek 
30364586Sgjelinek 		if ((subproc_err =
30374586Sgjelinek 		    subproc_status(gettext("brand-specific post-install"),
30384586Sgjelinek 		    status, B_FALSE)) != ZONE_SUBPROC_OK) {
30394586Sgjelinek 			err = Z_ERR;
30404586Sgjelinek 			(void) zone_set_state(target_zone,
30414586Sgjelinek 			    ZONE_STATE_INCOMPLETE);
30424586Sgjelinek 		}
30434586Sgjelinek 	}
30444586Sgjelinek 
30450Sstevel@tonic-gate done:
30462712Snn35248 	/*
30477089Sgjelinek 	 * If the install script exited with ZONE_SUBPROC_NOTCOMPLETE, try to
30487089Sgjelinek 	 * clean up the zone and leave the zone in the CONFIGURED state so that
30497089Sgjelinek 	 * another install can be attempted without requiring an uninstall
30507089Sgjelinek 	 * first.
30512712Snn35248 	 */
30527089Sgjelinek 	if (subproc_err == ZONE_SUBPROC_NOTCOMPLETE) {
30532712Snn35248 		if ((err = cleanup_zonepath(zonepath, B_FALSE)) != Z_OK) {
30542712Snn35248 			errno = err;
30552712Snn35248 			zperror2(target_zone,
30562712Snn35248 			    gettext("cleaning up zonepath failed"));
30572712Snn35248 		} else if ((err = zone_set_state(target_zone,
30582712Snn35248 		    ZONE_STATE_CONFIGURED)) != Z_OK) {
30592712Snn35248 			errno = err;
30602712Snn35248 			zperror2(target_zone, gettext("could not set state"));
30612712Snn35248 		}
30622712Snn35248 	}
30632712Snn35248 
30647089Sgjelinek 	if (!brand_help)
30657089Sgjelinek 		zonecfg_release_lock_file(target_zone, lockfd);
30660Sstevel@tonic-gate 	return ((err == Z_OK) ? Z_OK : Z_ERR);
30670Sstevel@tonic-gate }
30680Sstevel@tonic-gate 
30690Sstevel@tonic-gate /*
30701300Sgjelinek  * Check that the inherited pkg dirs are the same for the clone and its source.
30711300Sgjelinek  * The easiest way to do that is check that the list of ipds is the same
30721300Sgjelinek  * by matching each one against the other.  This algorithm should be fine since
30731300Sgjelinek  * the list of ipds should not be that long.
30741300Sgjelinek  */
30751300Sgjelinek static int
30761300Sgjelinek valid_ipd_clone(zone_dochandle_t s_handle, char *source_zone,
30771300Sgjelinek 	zone_dochandle_t t_handle, char *target_zone)
30781300Sgjelinek {
30791300Sgjelinek 	int err;
30801300Sgjelinek 	int res = Z_OK;
30811300Sgjelinek 	int s_cnt = 0;
30821300Sgjelinek 	int t_cnt = 0;
30831300Sgjelinek 	struct zone_fstab s_fstab;
30841300Sgjelinek 	struct zone_fstab t_fstab;
30851300Sgjelinek 
30861300Sgjelinek 	/*
30871300Sgjelinek 	 * First check the source of the clone against the target.
30881300Sgjelinek 	 */
30891300Sgjelinek 	if ((err = zonecfg_setipdent(s_handle)) != Z_OK) {
30901300Sgjelinek 		errno = err;
30911300Sgjelinek 		zperror2(source_zone, gettext("could not enumerate "
30921300Sgjelinek 		    "inherit-pkg-dirs"));
30931300Sgjelinek 		return (Z_ERR);
30941300Sgjelinek 	}
30951300Sgjelinek 
30961300Sgjelinek 	while (zonecfg_getipdent(s_handle, &s_fstab) == Z_OK) {
30971300Sgjelinek 		boolean_t match = B_FALSE;
30981300Sgjelinek 
30991300Sgjelinek 		s_cnt++;
31001300Sgjelinek 
31011300Sgjelinek 		if ((err = zonecfg_setipdent(t_handle)) != Z_OK) {
31021300Sgjelinek 			errno = err;
31031300Sgjelinek 			zperror2(target_zone, gettext("could not enumerate "
31041300Sgjelinek 			    "inherit-pkg-dirs"));
31051300Sgjelinek 			(void) zonecfg_endipdent(s_handle);
31061300Sgjelinek 			return (Z_ERR);
31071300Sgjelinek 		}
31081300Sgjelinek 
31091300Sgjelinek 		while (zonecfg_getipdent(t_handle, &t_fstab) == Z_OK) {
31101300Sgjelinek 			if (strcmp(s_fstab.zone_fs_dir, t_fstab.zone_fs_dir)
31111300Sgjelinek 			    == 0) {
31121300Sgjelinek 				match = B_TRUE;
31131300Sgjelinek 				break;
31141300Sgjelinek 			}
31151300Sgjelinek 		}
31161300Sgjelinek 		(void) zonecfg_endipdent(t_handle);
31171300Sgjelinek 
31181300Sgjelinek 		if (!match) {
31191300Sgjelinek 			(void) fprintf(stderr, gettext("inherit-pkg-dir "
31201300Sgjelinek 			    "'%s' is not configured in zone %s.\n"),
31211300Sgjelinek 			    s_fstab.zone_fs_dir, target_zone);
31221300Sgjelinek 			res = Z_ERR;
31231300Sgjelinek 		}
31241300Sgjelinek 	}
31251300Sgjelinek 
31261300Sgjelinek 	(void) zonecfg_endipdent(s_handle);
31271300Sgjelinek 
31281300Sgjelinek 	/* skip the next check if we already have errors */
31291300Sgjelinek 	if (res == Z_ERR)
31301300Sgjelinek 		return (res);
31311300Sgjelinek 
31321300Sgjelinek 	/*
31331300Sgjelinek 	 * Now check the number of ipds in the target so we can verify
31341300Sgjelinek 	 * that the source is not a subset of the target.
31351300Sgjelinek 	 */
31361300Sgjelinek 	if ((err = zonecfg_setipdent(t_handle)) != Z_OK) {
31371300Sgjelinek 		errno = err;
31381300Sgjelinek 		zperror2(target_zone, gettext("could not enumerate "
31391300Sgjelinek 		    "inherit-pkg-dirs"));
31401300Sgjelinek 		return (Z_ERR);
31411300Sgjelinek 	}
31421300Sgjelinek 
31431300Sgjelinek 	while (zonecfg_getipdent(t_handle, &t_fstab) == Z_OK)
31441300Sgjelinek 		t_cnt++;
31451300Sgjelinek 
31461300Sgjelinek 	(void) zonecfg_endipdent(t_handle);
31471300Sgjelinek 
31481300Sgjelinek 	if (t_cnt != s_cnt) {
31491300Sgjelinek 		(void) fprintf(stderr, gettext("Zone %s is configured "
31501300Sgjelinek 		    "with inherit-pkg-dirs that are not configured in zone "
31511300Sgjelinek 		    "%s.\n"), target_zone, source_zone);
31521300Sgjelinek 		res = Z_ERR;
31531300Sgjelinek 	}
31541300Sgjelinek 
31551300Sgjelinek 	return (res);
31561300Sgjelinek }
31571300Sgjelinek 
31581300Sgjelinek static void
31591300Sgjelinek warn_dev_match(zone_dochandle_t s_handle, char *source_zone,
31601300Sgjelinek 	zone_dochandle_t t_handle, char *target_zone)
31611300Sgjelinek {
31621300Sgjelinek 	int err;
31631300Sgjelinek 	struct zone_devtab s_devtab;
31641300Sgjelinek 	struct zone_devtab t_devtab;
31651300Sgjelinek 
31661300Sgjelinek 	if ((err = zonecfg_setdevent(t_handle)) != Z_OK) {
31671300Sgjelinek 		errno = err;
31681300Sgjelinek 		zperror2(target_zone, gettext("could not enumerate devices"));
31691300Sgjelinek 		return;
31701300Sgjelinek 	}
31711300Sgjelinek 
31721300Sgjelinek 	while (zonecfg_getdevent(t_handle, &t_devtab) == Z_OK) {
31731300Sgjelinek 		if ((err = zonecfg_setdevent(s_handle)) != Z_OK) {
31741300Sgjelinek 			errno = err;
31751300Sgjelinek 			zperror2(source_zone,
31761300Sgjelinek 			    gettext("could not enumerate devices"));
31771300Sgjelinek 			(void) zonecfg_enddevent(t_handle);
31781300Sgjelinek 			return;
31791300Sgjelinek 		}
31801300Sgjelinek 
31811300Sgjelinek 		while (zonecfg_getdevent(s_handle, &s_devtab) == Z_OK) {
31821300Sgjelinek 			/*
31831300Sgjelinek 			 * Use fnmatch to catch the case where wildcards
31841300Sgjelinek 			 * were used in one zone and the other has an
31851300Sgjelinek 			 * explicit entry (e.g. /dev/dsk/c0t0d0s6 vs.
31861300Sgjelinek 			 * /dev/\*dsk/c0t0d0s6).
31871300Sgjelinek 			 */
31881300Sgjelinek 			if (fnmatch(t_devtab.zone_dev_match,
31891300Sgjelinek 			    s_devtab.zone_dev_match, FNM_PATHNAME) == 0 ||
31901300Sgjelinek 			    fnmatch(s_devtab.zone_dev_match,
31911300Sgjelinek 			    t_devtab.zone_dev_match, FNM_PATHNAME) == 0) {
31921300Sgjelinek 				(void) fprintf(stderr,
31931300Sgjelinek 				    gettext("WARNING: device '%s' "
31941300Sgjelinek 				    "is configured in both zones.\n"),
31951300Sgjelinek 				    t_devtab.zone_dev_match);
31961300Sgjelinek 				break;
31971300Sgjelinek 			}
31981300Sgjelinek 		}
31991300Sgjelinek 		(void) zonecfg_enddevent(s_handle);
32001300Sgjelinek 	}
32011300Sgjelinek 
32021300Sgjelinek 	(void) zonecfg_enddevent(t_handle);
32031300Sgjelinek }
32041300Sgjelinek 
32051300Sgjelinek /*
32061300Sgjelinek  * Check if the specified mount option (opt) is contained within the
32071300Sgjelinek  * options string.
32081300Sgjelinek  */
32091300Sgjelinek static boolean_t
32101300Sgjelinek opt_match(char *opt, char *options)
32111300Sgjelinek {
32121300Sgjelinek 	char *p;
32131300Sgjelinek 	char *lastp;
32141300Sgjelinek 
32151300Sgjelinek 	if ((p = strtok_r(options, ",", &lastp)) != NULL) {
32161300Sgjelinek 		if (strcmp(p, opt) == 0)
32171300Sgjelinek 			return (B_TRUE);
32181300Sgjelinek 		while ((p = strtok_r(NULL, ",", &lastp)) != NULL) {
32191300Sgjelinek 			if (strcmp(p, opt) == 0)
32201300Sgjelinek 				return (B_TRUE);
32211300Sgjelinek 		}
32221300Sgjelinek 	}
32231300Sgjelinek 
32241300Sgjelinek 	return (B_FALSE);
32251300Sgjelinek }
32261300Sgjelinek 
32271867Sgjelinek #define	RW_LOFS	"WARNING: read-write lofs file system on '%s' is configured " \
32281300Sgjelinek 	"in both zones.\n"
32291300Sgjelinek 
32301300Sgjelinek static void
32311300Sgjelinek print_fs_warnings(struct zone_fstab *s_fstab, struct zone_fstab *t_fstab)
32321300Sgjelinek {
32331300Sgjelinek 	/*
32341300Sgjelinek 	 * It is ok to have shared lofs mounted fs but we want to warn if
32351300Sgjelinek 	 * either is rw since this will effect the other zone.
32361300Sgjelinek 	 */
32371300Sgjelinek 	if (strcmp(t_fstab->zone_fs_type, "lofs") == 0) {
32381300Sgjelinek 		zone_fsopt_t *optp;
32391300Sgjelinek 
32401300Sgjelinek 		/* The default is rw so no options means rw */
32411300Sgjelinek 		if (t_fstab->zone_fs_options == NULL ||
32421300Sgjelinek 		    s_fstab->zone_fs_options == NULL) {
32431300Sgjelinek 			(void) fprintf(stderr, gettext(RW_LOFS),
32441300Sgjelinek 			    t_fstab->zone_fs_special);
32451300Sgjelinek 			return;
32461300Sgjelinek 		}
32471300Sgjelinek 
32481300Sgjelinek 		for (optp = s_fstab->zone_fs_options; optp != NULL;
32491300Sgjelinek 		    optp = optp->zone_fsopt_next) {
32501300Sgjelinek 			if (opt_match("rw", optp->zone_fsopt_opt)) {
32511300Sgjelinek 				(void) fprintf(stderr, gettext(RW_LOFS),
32521300Sgjelinek 				    s_fstab->zone_fs_special);
32531300Sgjelinek 				return;
32541300Sgjelinek 			}
32551300Sgjelinek 		}
32561300Sgjelinek 
32571300Sgjelinek 		for (optp = t_fstab->zone_fs_options; optp != NULL;
32581300Sgjelinek 		    optp = optp->zone_fsopt_next) {
32591300Sgjelinek 			if (opt_match("rw", optp->zone_fsopt_opt)) {
32601300Sgjelinek 				(void) fprintf(stderr, gettext(RW_LOFS),
32611300Sgjelinek 				    t_fstab->zone_fs_special);
32621300Sgjelinek 				return;
32631300Sgjelinek 			}
32641300Sgjelinek 		}
32651300Sgjelinek 
32661300Sgjelinek 		return;
32671300Sgjelinek 	}
32681300Sgjelinek 
32691300Sgjelinek 	/*
32701300Sgjelinek 	 * TRANSLATION_NOTE
32711867Sgjelinek 	 * The first variable is the file system type and the second is
32721867Sgjelinek 	 * the file system special device.  For example,
32731867Sgjelinek 	 * WARNING: ufs file system on '/dev/dsk/c0t0d0s0' ...
32741300Sgjelinek 	 */
32751867Sgjelinek 	(void) fprintf(stderr, gettext("WARNING: %s file system on '%s' "
32761300Sgjelinek 	    "is configured in both zones.\n"), t_fstab->zone_fs_type,
32771300Sgjelinek 	    t_fstab->zone_fs_special);
32781300Sgjelinek }
32791300Sgjelinek 
32801300Sgjelinek static void
32811300Sgjelinek warn_fs_match(zone_dochandle_t s_handle, char *source_zone,
32821300Sgjelinek 	zone_dochandle_t t_handle, char *target_zone)
32831300Sgjelinek {
32841300Sgjelinek 	int err;
32851300Sgjelinek 	struct zone_fstab s_fstab;
32861300Sgjelinek 	struct zone_fstab t_fstab;
32871300Sgjelinek 
32881300Sgjelinek 	if ((err = zonecfg_setfsent(t_handle)) != Z_OK) {
32891300Sgjelinek 		errno = err;
32901300Sgjelinek 		zperror2(target_zone,
32911867Sgjelinek 		    gettext("could not enumerate file systems"));
32921300Sgjelinek 		return;
32931300Sgjelinek 	}
32941300Sgjelinek 
32951300Sgjelinek 	while (zonecfg_getfsent(t_handle, &t_fstab) == Z_OK) {
32961300Sgjelinek 		if ((err = zonecfg_setfsent(s_handle)) != Z_OK) {
32971300Sgjelinek 			errno = err;
32981300Sgjelinek 			zperror2(source_zone,
32991867Sgjelinek 			    gettext("could not enumerate file systems"));
33001300Sgjelinek 			(void) zonecfg_endfsent(t_handle);
33011300Sgjelinek 			return;
33021300Sgjelinek 		}
33031300Sgjelinek 
33041300Sgjelinek 		while (zonecfg_getfsent(s_handle, &s_fstab) == Z_OK) {
33051300Sgjelinek 			if (strcmp(t_fstab.zone_fs_special,
33061300Sgjelinek 			    s_fstab.zone_fs_special) == 0) {
33071300Sgjelinek 				print_fs_warnings(&s_fstab, &t_fstab);
33081300Sgjelinek 				break;
33091300Sgjelinek 			}
33101300Sgjelinek 		}
33111300Sgjelinek 		(void) zonecfg_endfsent(s_handle);
33121300Sgjelinek 	}
33131300Sgjelinek 
33141300Sgjelinek 	(void) zonecfg_endfsent(t_handle);
33151300Sgjelinek }
33161300Sgjelinek 
33171300Sgjelinek /*
33181300Sgjelinek  * We don't catch the case where you used the same IP address but
33191300Sgjelinek  * it is not an exact string match.  For example, 192.9.0.128 vs. 192.09.0.128.
33201300Sgjelinek  * However, we're not going to worry about that but we will check for
33211300Sgjelinek  * a possible netmask on one of the addresses (e.g. 10.0.0.1 and 10.0.0.1/24)
33221300Sgjelinek  * and handle that case as a match.
33231300Sgjelinek  */
33241300Sgjelinek static void
33251300Sgjelinek warn_ip_match(zone_dochandle_t s_handle, char *source_zone,
33261300Sgjelinek 	zone_dochandle_t t_handle, char *target_zone)
33271300Sgjelinek {
33281300Sgjelinek 	int err;
33291300Sgjelinek 	struct zone_nwiftab s_nwiftab;
33301300Sgjelinek 	struct zone_nwiftab t_nwiftab;
33311300Sgjelinek 
33321300Sgjelinek 	if ((err = zonecfg_setnwifent(t_handle)) != Z_OK) {
33331300Sgjelinek 		errno = err;
33341300Sgjelinek 		zperror2(target_zone,
33351300Sgjelinek 		    gettext("could not enumerate network interfaces"));
33361300Sgjelinek 		return;
33371300Sgjelinek 	}
33381300Sgjelinek 
33391300Sgjelinek 	while (zonecfg_getnwifent(t_handle, &t_nwiftab) == Z_OK) {
33401300Sgjelinek 		char *p;
33411300Sgjelinek 
33421300Sgjelinek 		/* remove an (optional) netmask from the address */
33431300Sgjelinek 		if ((p = strchr(t_nwiftab.zone_nwif_address, '/')) != NULL)
33441300Sgjelinek 			*p = '\0';
33451300Sgjelinek 
33461300Sgjelinek 		if ((err = zonecfg_setnwifent(s_handle)) != Z_OK) {
33471300Sgjelinek 			errno = err;
33481300Sgjelinek 			zperror2(source_zone,
33491300Sgjelinek 			    gettext("could not enumerate network interfaces"));
33501300Sgjelinek 			(void) zonecfg_endnwifent(t_handle);
33511300Sgjelinek 			return;
33521300Sgjelinek 		}
33531300Sgjelinek 
33541300Sgjelinek 		while (zonecfg_getnwifent(s_handle, &s_nwiftab) == Z_OK) {
33551300Sgjelinek 			/* remove an (optional) netmask from the address */
33561300Sgjelinek 			if ((p = strchr(s_nwiftab.zone_nwif_address, '/'))
33571300Sgjelinek 			    != NULL)
33581300Sgjelinek 				*p = '\0';
33591300Sgjelinek 
33603448Sdh155122 			/* For exclusive-IP zones, address is not specified. */
33613448Sdh155122 			if (strlen(s_nwiftab.zone_nwif_address) == 0)
33623448Sdh155122 				continue;
33633448Sdh155122 
33641300Sgjelinek 			if (strcmp(t_nwiftab.zone_nwif_address,
33651300Sgjelinek 			    s_nwiftab.zone_nwif_address) == 0) {
33661300Sgjelinek 				(void) fprintf(stderr,
33671300Sgjelinek 				    gettext("WARNING: network address '%s' "
33681300Sgjelinek 				    "is configured in both zones.\n"),
33691300Sgjelinek 				    t_nwiftab.zone_nwif_address);
33701300Sgjelinek 				break;
33711300Sgjelinek 			}
33721300Sgjelinek 		}
33731300Sgjelinek 		(void) zonecfg_endnwifent(s_handle);
33741300Sgjelinek 	}
33751300Sgjelinek 
33761300Sgjelinek 	(void) zonecfg_endnwifent(t_handle);
33771300Sgjelinek }
33781300Sgjelinek 
33791300Sgjelinek static void
33802712Snn35248 warn_dataset_match(zone_dochandle_t s_handle, char *source,
33812712Snn35248 	zone_dochandle_t t_handle, char *target)
33821300Sgjelinek {
33831300Sgjelinek 	int err;
33841300Sgjelinek 	struct zone_dstab s_dstab;
33851300Sgjelinek 	struct zone_dstab t_dstab;
33861300Sgjelinek 
33871300Sgjelinek 	if ((err = zonecfg_setdsent(t_handle)) != Z_OK) {
33881300Sgjelinek 		errno = err;
33892712Snn35248 		zperror2(target, gettext("could not enumerate datasets"));
33901300Sgjelinek 		return;
33911300Sgjelinek 	}
33921300Sgjelinek 
33931300Sgjelinek 	while (zonecfg_getdsent(t_handle, &t_dstab) == Z_OK) {
33941300Sgjelinek 		if ((err = zonecfg_setdsent(s_handle)) != Z_OK) {
33951300Sgjelinek 			errno = err;
33962712Snn35248 			zperror2(source,
33971300Sgjelinek 			    gettext("could not enumerate datasets"));
33981300Sgjelinek 			(void) zonecfg_enddsent(t_handle);
33991300Sgjelinek 			return;
34001300Sgjelinek 		}
34011300Sgjelinek 
34021300Sgjelinek 		while (zonecfg_getdsent(s_handle, &s_dstab) == Z_OK) {
34031300Sgjelinek 			if (strcmp(t_dstab.zone_dataset_name,
34041300Sgjelinek 			    s_dstab.zone_dataset_name) == 0) {
34052712Snn35248 				target_zone = source;
34062712Snn35248 				zerror(gettext("WARNING: dataset '%s' "
34071300Sgjelinek 				    "is configured in both zones.\n"),
34081300Sgjelinek 				    t_dstab.zone_dataset_name);
34091300Sgjelinek 				break;
34101300Sgjelinek 			}
34111300Sgjelinek 		}
34121300Sgjelinek 		(void) zonecfg_enddsent(s_handle);
34131300Sgjelinek 	}
34141300Sgjelinek 
34151300Sgjelinek 	(void) zonecfg_enddsent(t_handle);
34161300Sgjelinek }
34171300Sgjelinek 
34182712Snn35248 /*
34192712Snn35248  * Check that the clone and its source have the same brand type.
34202712Snn35248  */
34212712Snn35248 static int
34222712Snn35248 valid_brand_clone(char *source_zone, char *target_zone)
34232712Snn35248 {
34242727Sedp 	brand_handle_t bh;
34252712Snn35248 	char source_brand[MAXNAMELEN];
34262712Snn35248 
34272712Snn35248 	if ((zone_get_brand(source_zone, source_brand,
34282712Snn35248 	    sizeof (source_brand))) != Z_OK) {
34292712Snn35248 		(void) fprintf(stderr, "%s: zone '%s': %s\n",
34302712Snn35248 		    execname, source_zone, gettext("missing or invalid brand"));
34312712Snn35248 		return (Z_ERR);
34322712Snn35248 	}
34332712Snn35248 
34342712Snn35248 	if (strcmp(source_brand, target_brand) != NULL) {
34352712Snn35248 		(void) fprintf(stderr,
34362712Snn35248 		    gettext("%s: Zones '%s' and '%s' have different brand "
34372712Snn35248 		    "types.\n"), execname, source_zone, target_zone);
34382712Snn35248 		return (Z_ERR);
34392712Snn35248 	}
34402712Snn35248 
34412727Sedp 	if ((bh = brand_open(target_brand)) == NULL) {
34422712Snn35248 		zerror(gettext("missing or invalid brand"));
34432712Snn35248 		return (Z_ERR);
34442712Snn35248 	}
34452727Sedp 	brand_close(bh);
34462712Snn35248 	return (Z_OK);
34472712Snn35248 }
34482712Snn35248 
34491300Sgjelinek static int
34501300Sgjelinek validate_clone(char *source_zone, char *target_zone)
34511300Sgjelinek {
34521300Sgjelinek 	int err = Z_OK;
34531300Sgjelinek 	zone_dochandle_t s_handle;
34541300Sgjelinek 	zone_dochandle_t t_handle;
34551300Sgjelinek 
34561300Sgjelinek 	if ((t_handle = zonecfg_init_handle()) == NULL) {
34571300Sgjelinek 		zperror(cmd_to_str(CMD_CLONE), B_TRUE);
34581300Sgjelinek 		return (Z_ERR);
34591300Sgjelinek 	}
34601300Sgjelinek 	if ((err = zonecfg_get_handle(target_zone, t_handle)) != Z_OK) {
34611300Sgjelinek 		errno = err;
34621300Sgjelinek 		zperror(cmd_to_str(CMD_CLONE), B_TRUE);
34631300Sgjelinek 		zonecfg_fini_handle(t_handle);
34641300Sgjelinek 		return (Z_ERR);
34651300Sgjelinek 	}
34661300Sgjelinek 
34671300Sgjelinek 	if ((s_handle = zonecfg_init_handle()) == NULL) {
34681300Sgjelinek 		zperror(cmd_to_str(CMD_CLONE), B_TRUE);
34691300Sgjelinek 		zonecfg_fini_handle(t_handle);
34701300Sgjelinek 		return (Z_ERR);
34711300Sgjelinek 	}
34721300Sgjelinek 	if ((err = zonecfg_get_handle(source_zone, s_handle)) != Z_OK) {
34731300Sgjelinek 		errno = err;
34741300Sgjelinek 		zperror(cmd_to_str(CMD_CLONE), B_TRUE);
34751300Sgjelinek 		goto done;
34761300Sgjelinek 	}
34771300Sgjelinek 
34782712Snn35248 	/* verify new zone has same brand type */
34792712Snn35248 	err = valid_brand_clone(source_zone, target_zone);
34802712Snn35248 	if (err != Z_OK)
34812712Snn35248 		goto done;
34822712Snn35248 
34831300Sgjelinek 	/* verify new zone has same inherit-pkg-dirs */
34841300Sgjelinek 	err = valid_ipd_clone(s_handle, source_zone, t_handle, target_zone);
34851300Sgjelinek 
34861300Sgjelinek 	/* warn about imported fs's which are the same */
34871300Sgjelinek 	warn_fs_match(s_handle, source_zone, t_handle, target_zone);
34881300Sgjelinek 
34891300Sgjelinek 	/* warn about imported IP addresses which are the same */
34901300Sgjelinek 	warn_ip_match(s_handle, source_zone, t_handle, target_zone);
34911300Sgjelinek 
34921300Sgjelinek 	/* warn about imported devices which are the same */
34931300Sgjelinek 	warn_dev_match(s_handle, source_zone, t_handle, target_zone);
34941300Sgjelinek 
34951300Sgjelinek 	/* warn about imported datasets which are the same */
34961300Sgjelinek 	warn_dataset_match(s_handle, source_zone, t_handle, target_zone);
34971300Sgjelinek 
34981300Sgjelinek done:
34991300Sgjelinek 	zonecfg_fini_handle(t_handle);
35001300Sgjelinek 	zonecfg_fini_handle(s_handle);
35011300Sgjelinek 
35021300Sgjelinek 	return ((err == Z_OK) ? Z_OK : Z_ERR);
35031300Sgjelinek }
35041300Sgjelinek 
35051300Sgjelinek static int
35061300Sgjelinek copy_zone(char *src, char *dst)
35071300Sgjelinek {
35081300Sgjelinek 	boolean_t out_null = B_FALSE;
35091300Sgjelinek 	int status;
35101300Sgjelinek 	char *outfile;
35111300Sgjelinek 	char cmdbuf[MAXPATHLEN * 2 + 128];
35121300Sgjelinek 
35131300Sgjelinek 	if ((outfile = tempnam("/var/log", "zone")) == NULL) {
35141300Sgjelinek 		outfile = "/dev/null";
35151300Sgjelinek 		out_null = B_TRUE;
35161300Sgjelinek 	}
35171300Sgjelinek 
35181867Sgjelinek 	/*
35191867Sgjelinek 	 * Use find to get the list of files to copy.  We need to skip
35201867Sgjelinek 	 * files of type "socket" since cpio can't handle those but that
35211867Sgjelinek 	 * should be ok since the app will recreate the socket when it runs.
35221867Sgjelinek 	 * We also need to filter out anything under the .zfs subdir.  Since
35231867Sgjelinek 	 * find is running depth-first, we need the extra egrep to filter .zfs.
35241867Sgjelinek 	 */
35251300Sgjelinek 	(void) snprintf(cmdbuf, sizeof (cmdbuf),
35261867Sgjelinek 	    "cd %s && /usr/bin/find . -type s -prune -o -depth -print | "
35271607Sgjelinek 	    "/usr/bin/egrep -v '^\\./\\.zfs$|^\\./\\.zfs/' | "
35281300Sgjelinek 	    "/usr/bin/cpio -pdmuP@ %s > %s 2>&1",
35291300Sgjelinek 	    src, dst, outfile);
35301300Sgjelinek 
35311300Sgjelinek 	status = do_subproc(cmdbuf);
35321300Sgjelinek 
35332712Snn35248 	if (subproc_status("copy", status, B_TRUE) != ZONE_SUBPROC_OK) {
35341300Sgjelinek 		if (!out_null)
35351300Sgjelinek 			(void) fprintf(stderr, gettext("\nThe copy failed.\n"
35361300Sgjelinek 			    "More information can be found in %s\n"), outfile);
35372712Snn35248 		return (Z_ERR);
35381300Sgjelinek 	}
35391300Sgjelinek 
35401300Sgjelinek 	if (!out_null)
35411300Sgjelinek 		(void) unlink(outfile);
35421300Sgjelinek 
35431300Sgjelinek 	return (Z_OK);
35441300Sgjelinek }
35451300Sgjelinek 
35461300Sgjelinek /* ARGSUSED */
35471867Sgjelinek static int
35481300Sgjelinek zfm_print(const char *p, void *r) {
35491300Sgjelinek 	zerror("  %s\n", p);
35501300Sgjelinek 	return (0);
35511300Sgjelinek }
35521300Sgjelinek 
35531867Sgjelinek int
35541867Sgjelinek clone_copy(char *source_zonepath, char *zonepath)
35551867Sgjelinek {
35561867Sgjelinek 	int err;
35571867Sgjelinek 
35581867Sgjelinek 	/* Don't clone the zone if anything is still mounted there */
35591867Sgjelinek 	if (zonecfg_find_mounts(source_zonepath, NULL, NULL)) {
35601867Sgjelinek 		zerror(gettext("These file systems are mounted on "
35611867Sgjelinek 		    "subdirectories of %s.\n"), source_zonepath);
35621867Sgjelinek 		(void) zonecfg_find_mounts(source_zonepath, zfm_print, NULL);
35631867Sgjelinek 		return (Z_ERR);
35641867Sgjelinek 	}
35651867Sgjelinek 
35661867Sgjelinek 	/*
35671867Sgjelinek 	 * Attempt to create a ZFS fs for the zonepath.  As usual, we don't
35681867Sgjelinek 	 * care if this works or not since we always have the default behavior
35691867Sgjelinek 	 * of a simple directory for the zonepath.
35701867Sgjelinek 	 */
35711867Sgjelinek 	create_zfs_zonepath(zonepath);
35721867Sgjelinek 
35731867Sgjelinek 	(void) printf(gettext("Copying %s..."), source_zonepath);
35741867Sgjelinek 	(void) fflush(stdout);
35751867Sgjelinek 
35761867Sgjelinek 	err = copy_zone(source_zonepath, zonepath);
35771867Sgjelinek 
35781867Sgjelinek 	(void) printf("\n");
35791867Sgjelinek 
35801867Sgjelinek 	return (err);
35811867Sgjelinek }
35821867Sgjelinek 
35831300Sgjelinek static int
35841300Sgjelinek clone_func(int argc, char *argv[])
35851300Sgjelinek {
35861300Sgjelinek 	char *source_zone = NULL;
35871300Sgjelinek 	int lockfd;
35881300Sgjelinek 	int err, arg;
35891300Sgjelinek 	char zonepath[MAXPATHLEN];
35901300Sgjelinek 	char source_zonepath[MAXPATHLEN];
35911300Sgjelinek 	zone_state_t state;
35921300Sgjelinek 	zone_entry_t *zent;
35931867Sgjelinek 	char *method = NULL;
35941867Sgjelinek 	char *snapshot = NULL;
35957089Sgjelinek 	char cmdbuf[MAXPATHLEN];
35967089Sgjelinek 	char postcmdbuf[MAXPATHLEN];
35977089Sgjelinek 	char presnapbuf[MAXPATHLEN];
35987089Sgjelinek 	char postsnapbuf[MAXPATHLEN];
35997089Sgjelinek 	char validsnapbuf[MAXPATHLEN];
36007089Sgjelinek 	brand_handle_t bh = NULL;
36017089Sgjelinek 	int status;
36027089Sgjelinek 	boolean_t brand_help = B_FALSE;
36031300Sgjelinek 
36041300Sgjelinek 	if (zonecfg_in_alt_root()) {
36051300Sgjelinek 		zerror(gettext("cannot clone zone in alternate root"));
36061300Sgjelinek 		return (Z_ERR);
36071300Sgjelinek 	}
36081300Sgjelinek 
36097089Sgjelinek 	/* Check the argv string for args we handle internally */
36101300Sgjelinek 	optind = 0;
36117089Sgjelinek 	opterr = 0;
36127089Sgjelinek 	while ((arg = getopt(argc, argv, "?m:s:")) != EOF) {
36131300Sgjelinek 		switch (arg) {
36141300Sgjelinek 		case '?':
36157089Sgjelinek 			if (optopt == '?') {
36167089Sgjelinek 				sub_usage(SHELP_CLONE, CMD_CLONE);
36177089Sgjelinek 				brand_help = B_TRUE;
36187089Sgjelinek 			}
36197089Sgjelinek 			/* Ignore unknown options - may be brand specific. */
36207089Sgjelinek 			break;
36211300Sgjelinek 		case 'm':
36221300Sgjelinek 			method = optarg;
36231300Sgjelinek 			break;
36241867Sgjelinek 		case 's':
36251867Sgjelinek 			snapshot = optarg;
36261867Sgjelinek 			break;
36271300Sgjelinek 		default:
36287089Sgjelinek 			/* Ignore unknown options - may be brand specific. */
36297089Sgjelinek 			break;
36301300Sgjelinek 		}
36311300Sgjelinek 	}
36327089Sgjelinek 
36337089Sgjelinek 	if (argc != (optind + 1)) {
36341300Sgjelinek 		sub_usage(SHELP_CLONE, CMD_CLONE);
36351300Sgjelinek 		return (Z_USAGE);
36361300Sgjelinek 	}
36377089Sgjelinek 
36381300Sgjelinek 	source_zone = argv[optind];
36397089Sgjelinek 
36407089Sgjelinek 	if (!brand_help) {
36417089Sgjelinek 		if (sanity_check(target_zone, CMD_CLONE, B_FALSE, B_TRUE,
36427089Sgjelinek 		    B_FALSE) != Z_OK)
36437089Sgjelinek 			return (Z_ERR);
36447089Sgjelinek 		if (verify_details(CMD_CLONE, argv) != Z_OK)
36457089Sgjelinek 			return (Z_ERR);
36467089Sgjelinek 
36477089Sgjelinek 		/*
36487089Sgjelinek 		 * We also need to do some extra validation on the source zone.
36497089Sgjelinek 		 */
36507089Sgjelinek 		if (strcmp(source_zone, GLOBAL_ZONENAME) == 0) {
36517089Sgjelinek 			zerror(gettext("%s operation is invalid for the "
36527089Sgjelinek 			    "global zone."), cmd_to_str(CMD_CLONE));
36537089Sgjelinek 			return (Z_ERR);
36547089Sgjelinek 		}
36557089Sgjelinek 
36567089Sgjelinek 		if (strncmp(source_zone, "SUNW", 4) == 0) {
36577089Sgjelinek 			zerror(gettext("%s operation is invalid for zones "
36587089Sgjelinek 			    "starting with SUNW."), cmd_to_str(CMD_CLONE));
36597089Sgjelinek 			return (Z_ERR);
36607089Sgjelinek 		}
36617089Sgjelinek 
36627089Sgjelinek 		zent = lookup_running_zone(source_zone);
36637089Sgjelinek 		if (zent != NULL) {
36647089Sgjelinek 			/* check whether the zone is ready or running */
36657089Sgjelinek 			if ((err = zone_get_state(zent->zname,
36667089Sgjelinek 			    &zent->zstate_num)) != Z_OK) {
36677089Sgjelinek 				errno = err;
36687089Sgjelinek 				zperror2(zent->zname, gettext("could not get "
36697089Sgjelinek 				    "state"));
36707089Sgjelinek 				/* can't tell, so hedge */
36717089Sgjelinek 				zent->zstate_str = "ready/running";
36727089Sgjelinek 			} else {
36737089Sgjelinek 				zent->zstate_str =
36747089Sgjelinek 				    zone_state_str(zent->zstate_num);
36757089Sgjelinek 			}
36767089Sgjelinek 			zerror(gettext("%s operation is invalid for %s zones."),
36777089Sgjelinek 			    cmd_to_str(CMD_CLONE), zent->zstate_str);
36787089Sgjelinek 			return (Z_ERR);
36797089Sgjelinek 		}
36807089Sgjelinek 
36817089Sgjelinek 		if ((err = zone_get_state(source_zone, &state)) != Z_OK) {
36821300Sgjelinek 			errno = err;
36837089Sgjelinek 			zperror2(source_zone, gettext("could not get state"));
36847089Sgjelinek 			return (Z_ERR);
36857089Sgjelinek 		}
36867089Sgjelinek 		if (state != ZONE_STATE_INSTALLED) {
36877089Sgjelinek 			(void) fprintf(stderr,
36887089Sgjelinek 			    gettext("%s: zone %s is %s; %s is required.\n"),
36897089Sgjelinek 			    execname, source_zone, zone_state_str(state),
36907089Sgjelinek 			    zone_state_str(ZONE_STATE_INSTALLED));
36917089Sgjelinek 			return (Z_ERR);
36921300Sgjelinek 		}
36937089Sgjelinek 
36947089Sgjelinek 		/*
36957089Sgjelinek 		 * The source zone checks out ok, continue with the clone.
36967089Sgjelinek 		 */
36977089Sgjelinek 
36987089Sgjelinek 		if (validate_clone(source_zone, target_zone) != Z_OK)
36997089Sgjelinek 			return (Z_ERR);
37007089Sgjelinek 
37017089Sgjelinek 		if (zonecfg_grab_lock_file(target_zone, &lockfd) != Z_OK) {
37027089Sgjelinek 			zerror(gettext("another %s may have an operation in "
37037089Sgjelinek 			    "progress."), "zoneadm");
37047089Sgjelinek 			return (Z_ERR);
37057089Sgjelinek 		}
37061300Sgjelinek 	}
37071300Sgjelinek 
37081300Sgjelinek 	if ((err = zone_get_zonepath(source_zone, source_zonepath,
37091300Sgjelinek 	    sizeof (source_zonepath))) != Z_OK) {
37101300Sgjelinek 		errno = err;
37111300Sgjelinek 		zperror2(source_zone, gettext("could not get zone path"));
37121300Sgjelinek 		goto done;
37131300Sgjelinek 	}
37141300Sgjelinek 
37151300Sgjelinek 	if ((err = zone_get_zonepath(target_zone, zonepath, sizeof (zonepath)))
37161300Sgjelinek 	    != Z_OK) {
37171300Sgjelinek 		errno = err;
37181300Sgjelinek 		zperror2(target_zone, gettext("could not get zone path"));
37191300Sgjelinek 		goto done;
37201300Sgjelinek 	}
37211300Sgjelinek 
37227089Sgjelinek 	/*
37237089Sgjelinek 	 * Fetch the clone and postclone hooks from the brand configuration.
37247089Sgjelinek 	 */
37257089Sgjelinek 	if ((bh = brand_open(target_brand)) == NULL) {
37267089Sgjelinek 		zerror(gettext("missing or invalid brand"));
37277089Sgjelinek 		err = Z_ERR;
37287089Sgjelinek 		goto done;
37297089Sgjelinek 	}
37307089Sgjelinek 
37317089Sgjelinek 	if (get_hook(bh, cmdbuf, sizeof (cmdbuf), brand_get_clone, target_zone,
37327089Sgjelinek 	    zonepath) != Z_OK) {
37337089Sgjelinek 		zerror("invalid brand configuration: missing clone resource");
37347089Sgjelinek 		brand_close(bh);
37357089Sgjelinek 		err = Z_ERR;
37367089Sgjelinek 		goto done;
37377089Sgjelinek 	}
37387089Sgjelinek 
37397089Sgjelinek 	if (get_hook(bh, postcmdbuf, sizeof (postcmdbuf), brand_get_postclone,
37407089Sgjelinek 	    target_zone, zonepath) != Z_OK) {
37417089Sgjelinek 		zerror("invalid brand configuration: missing postclone "
37427089Sgjelinek 		    "resource");
37437089Sgjelinek 		brand_close(bh);
37447089Sgjelinek 		err = Z_ERR;
37457089Sgjelinek 		goto done;
37467089Sgjelinek 	}
37477089Sgjelinek 
37487089Sgjelinek 	if (get_hook(bh, presnapbuf, sizeof (presnapbuf), brand_get_presnap,
37497089Sgjelinek 	    source_zone, source_zonepath) != Z_OK) {
37507089Sgjelinek 		zerror("invalid brand configuration: missing presnap "
37517089Sgjelinek 		    "resource");
37527089Sgjelinek 		brand_close(bh);
37537089Sgjelinek 		err = Z_ERR;
37541300Sgjelinek 		goto done;
37551300Sgjelinek 	}
37561300Sgjelinek 
37577089Sgjelinek 	if (get_hook(bh, postsnapbuf, sizeof (postsnapbuf), brand_get_postsnap,
37587089Sgjelinek 	    source_zone, source_zonepath) != Z_OK) {
37597089Sgjelinek 		zerror("invalid brand configuration: missing postsnap "
37607089Sgjelinek 		    "resource");
37617089Sgjelinek 		brand_close(bh);
37627089Sgjelinek 		err = Z_ERR;
37637089Sgjelinek 		goto done;
37647089Sgjelinek 	}
37657089Sgjelinek 
37667089Sgjelinek 	if (get_hook(bh, validsnapbuf, sizeof (validsnapbuf),
37677089Sgjelinek 	    brand_get_validatesnap, target_zone, zonepath) != Z_OK) {
37687089Sgjelinek 		zerror("invalid brand configuration: missing validatesnap "
37697089Sgjelinek 		    "resource");
37707089Sgjelinek 		brand_close(bh);
37711867Sgjelinek 		err = Z_ERR;
37727089Sgjelinek 		goto done;
37737089Sgjelinek 	}
37747089Sgjelinek 	brand_close(bh);
37757089Sgjelinek 
37767089Sgjelinek 	/* Append all options to clone hook. */
37777089Sgjelinek 	if (addoptions(cmdbuf, argv, sizeof (cmdbuf)) != Z_OK) {
37787089Sgjelinek 		err = Z_ERR;
37797089Sgjelinek 		goto done;
37807089Sgjelinek 	}
37817089Sgjelinek 
37827089Sgjelinek 	/* Append all options to postclone hook. */
37837089Sgjelinek 	if (addoptions(postcmdbuf, argv, sizeof (postcmdbuf)) != Z_OK) {
37847089Sgjelinek 		err = Z_ERR;
37857089Sgjelinek 		goto done;
37867089Sgjelinek 	}
37877089Sgjelinek 
37887089Sgjelinek 	if (!brand_help) {
37897089Sgjelinek 		if ((err = zone_set_state(target_zone, ZONE_STATE_INCOMPLETE))
37907089Sgjelinek 		    != Z_OK) {
37917089Sgjelinek 			errno = err;
37927089Sgjelinek 			zperror2(target_zone, gettext("could not set state"));
37937089Sgjelinek 			goto done;
37947089Sgjelinek 		}
37951867Sgjelinek 	}
37961867Sgjelinek 
37972712Snn35248 	/*
37987089Sgjelinek 	 * The clone hook is optional.  If it exists, use the hook for
37997089Sgjelinek 	 * cloning, otherwise use the built-in clone support
38002712Snn35248 	 */
38017089Sgjelinek 	if (cmdbuf[0] != '\0') {
38027089Sgjelinek 		/* Run the clone hook */
38037089Sgjelinek 		status = do_subproc_interactive(cmdbuf);
38047089Sgjelinek 		if ((status = subproc_status(gettext("brand-specific clone"),
38057089Sgjelinek 		    status, B_FALSE)) != ZONE_SUBPROC_OK) {
38067089Sgjelinek 			if (status == ZONE_SUBPROC_USAGE && !brand_help)
38077089Sgjelinek 				sub_usage(SHELP_CLONE, CMD_CLONE);
38087089Sgjelinek 			err = Z_ERR;
38097089Sgjelinek 			goto done;
38107089Sgjelinek 		}
38117089Sgjelinek 
38127089Sgjelinek 		if (brand_help)
38137089Sgjelinek 			return (Z_OK);
38147089Sgjelinek 
38157089Sgjelinek 	} else {
38167089Sgjelinek 		/* If just help, we're done since there is no brand help. */
38177089Sgjelinek 		if (brand_help)
38187089Sgjelinek 			return (Z_OK);
38197089Sgjelinek 
38207089Sgjelinek 		/* Run the built-in clone support. */
38217089Sgjelinek 
38227089Sgjelinek 		/* The only explicit built-in method is "copy". */
38237089Sgjelinek 		if (method != NULL && strcmp(method, "copy") != 0) {
38247089Sgjelinek 			sub_usage(SHELP_CLONE, CMD_CLONE);
38257089Sgjelinek 			err = Z_USAGE;
38267089Sgjelinek 			goto done;
38277089Sgjelinek 		}
38287089Sgjelinek 
38297089Sgjelinek 		if (snapshot != NULL) {
38307089Sgjelinek 			err = clone_snapshot_zfs(snapshot, zonepath,
38317089Sgjelinek 			    validsnapbuf);
38327089Sgjelinek 		} else {
38337089Sgjelinek 			/*
38347089Sgjelinek 			 * We always copy the clone unless the source is ZFS
38357089Sgjelinek 			 * and a ZFS clone worked.  We fallback to copying if
38367089Sgjelinek 			 * the ZFS clone fails for some reason.
38377089Sgjelinek 			 */
38387089Sgjelinek 			err = Z_ERR;
38397089Sgjelinek 			if (method == NULL && is_zonepath_zfs(source_zonepath))
38407089Sgjelinek 				err = clone_zfs(source_zonepath, zonepath,
38417089Sgjelinek 				    presnapbuf, postsnapbuf);
38427089Sgjelinek 
38437089Sgjelinek 			if (err != Z_OK)
38447089Sgjelinek 				err = clone_copy(source_zonepath, zonepath);
38457089Sgjelinek 		}
38467089Sgjelinek 	}
38477089Sgjelinek 
38487089Sgjelinek 	if (err == Z_OK && postcmdbuf[0] != '\0') {
38497089Sgjelinek 		status = do_subproc(postcmdbuf);
38507089Sgjelinek 		if ((err = subproc_status("postclone", status, B_FALSE))
38517089Sgjelinek 		    != ZONE_SUBPROC_OK) {
38527089Sgjelinek 			zerror(gettext("post-clone configuration failed."));
38537089Sgjelinek 			err = Z_ERR;
38547089Sgjelinek 		}
38557089Sgjelinek 	}
38561300Sgjelinek 
38571300Sgjelinek done:
38582712Snn35248 	/*
38592712Snn35248 	 * If everything went well, we mark the zone as installed.
38602712Snn35248 	 */
38612712Snn35248 	if (err == Z_OK) {
38622712Snn35248 		err = zone_set_state(target_zone, ZONE_STATE_INSTALLED);
38632712Snn35248 		if (err != Z_OK) {
38642712Snn35248 			errno = err;
38652712Snn35248 			zperror2(target_zone, gettext("could not set state"));
38662712Snn35248 		}
38672712Snn35248 	}
38687089Sgjelinek 	if (!brand_help)
38697089Sgjelinek 		zonecfg_release_lock_file(target_zone, lockfd);
38701300Sgjelinek 	return ((err == Z_OK) ? Z_OK : Z_ERR);
38711300Sgjelinek }
38721300Sgjelinek 
38731607Sgjelinek /*
38741867Sgjelinek  * Used when removing a zonepath after uninstalling or cleaning up after
38751867Sgjelinek  * the move subcommand.  This handles a zonepath that has non-standard
38761867Sgjelinek  * contents so that we will only cleanup the stuff we know about and leave
38771867Sgjelinek  * any user data alone.
38781607Sgjelinek  *
38791867Sgjelinek  * If the "all" parameter is true then we should remove the whole zonepath
38801867Sgjelinek  * even if it has non-standard files/directories in it.  This can be used when
38811867Sgjelinek  * we need to cleanup after moving the zonepath across file systems.
38821867Sgjelinek  *
38831867Sgjelinek  * We "exec" the RMCOMMAND so that the returned status is that of RMCOMMAND
38841867Sgjelinek  * and not the shell.
38851607Sgjelinek  */
38861607Sgjelinek static int
38871867Sgjelinek cleanup_zonepath(char *zonepath, boolean_t all)
38881607Sgjelinek {
38891867Sgjelinek 	int		status;
38901867Sgjelinek 	int		i;
38911867Sgjelinek 	boolean_t	non_std = B_FALSE;
38921867Sgjelinek 	struct dirent	*dp;
38931867Sgjelinek 	DIR		*dirp;
38943686Sgjelinek 			/*
38953686Sgjelinek 			 * The SUNWattached.xml file is expected since it might
38963686Sgjelinek 			 * exist if the zone was force-attached after a
38973686Sgjelinek 			 * migration.
38983686Sgjelinek 			 */
38993686Sgjelinek 	char		*std_entries[] = {"dev", "lu", "root",
39003686Sgjelinek 			    "SUNWattached.xml", NULL};
39011867Sgjelinek 			/* (MAXPATHLEN * 3) is for the 3 std_entries dirs */
39021867Sgjelinek 	char		cmdbuf[sizeof (RMCOMMAND) + (MAXPATHLEN * 3) + 64];
39031867Sgjelinek 
39041867Sgjelinek 	/*
39051867Sgjelinek 	 * We shouldn't need these checks but lets be paranoid since we
39061867Sgjelinek 	 * could blow away the whole system here if we got the wrong zonepath.
39071867Sgjelinek 	 */
39081867Sgjelinek 	if (*zonepath == NULL || strcmp(zonepath, "/") == 0) {
39091867Sgjelinek 		(void) fprintf(stderr, "invalid zonepath '%s'\n", zonepath);
39101867Sgjelinek 		return (Z_INVAL);
39111867Sgjelinek 	}
39121867Sgjelinek 
39131867Sgjelinek 	/*
39141867Sgjelinek 	 * If the dirpath is already gone (maybe it was manually removed) then
39151867Sgjelinek 	 * we just return Z_OK so that the cleanup is successful.
39161867Sgjelinek 	 */
39171867Sgjelinek 	if ((dirp = opendir(zonepath)) == NULL)
39181867Sgjelinek 		return (Z_OK);
39191867Sgjelinek 
39201867Sgjelinek 	/*
39211867Sgjelinek 	 * Look through the zonepath directory to see if there are any
39221867Sgjelinek 	 * non-standard files/dirs.  Also skip .zfs since that might be
39231867Sgjelinek 	 * there but we'll handle ZFS file systems as a special case.
39241867Sgjelinek 	 */
39251867Sgjelinek 	while ((dp = readdir(dirp)) != NULL) {
39261867Sgjelinek 		if (strcmp(dp->d_name, ".") == 0 ||
39271867Sgjelinek 		    strcmp(dp->d_name, "..") == 0 ||
39281867Sgjelinek 		    strcmp(dp->d_name, ".zfs") == 0)
39291867Sgjelinek 			continue;
39301867Sgjelinek 
39311867Sgjelinek 		for (i = 0; std_entries[i] != NULL; i++)
39321867Sgjelinek 			if (strcmp(dp->d_name, std_entries[i]) == 0)
39331867Sgjelinek 				break;
39341867Sgjelinek 
39351867Sgjelinek 		if (std_entries[i] == NULL)
39361867Sgjelinek 			non_std = B_TRUE;
39371867Sgjelinek 	}
39381867Sgjelinek 	(void) closedir(dirp);
39391867Sgjelinek 
39401867Sgjelinek 	if (!all && non_std) {
39411607Sgjelinek 		/*
39421867Sgjelinek 		 * There are extra, non-standard directories/files in the
39431867Sgjelinek 		 * zonepath so we don't want to remove the zonepath.  We
39441867Sgjelinek 		 * just want to remove the standard directories and leave
39451867Sgjelinek 		 * the user data alone.
39461607Sgjelinek 		 */
39471867Sgjelinek 		(void) snprintf(cmdbuf, sizeof (cmdbuf), "exec " RMCOMMAND);
39481867Sgjelinek 
39491867Sgjelinek 		for (i = 0; std_entries[i] != NULL; i++) {
39501867Sgjelinek 			char tmpbuf[MAXPATHLEN];
39511867Sgjelinek 
39521867Sgjelinek 			if (snprintf(tmpbuf, sizeof (tmpbuf), " %s/%s",
39531867Sgjelinek 			    zonepath, std_entries[i]) >= sizeof (tmpbuf) ||
39541867Sgjelinek 			    strlcat(cmdbuf, tmpbuf, sizeof (cmdbuf)) >=
39551867Sgjelinek 			    sizeof (cmdbuf)) {
39561867Sgjelinek 				(void) fprintf(stderr,
39571867Sgjelinek 				    gettext("path is too long\n"));
39581867Sgjelinek 				return (Z_INVAL);
39591867Sgjelinek 			}
39601867Sgjelinek 		}
39611867Sgjelinek 
39621867Sgjelinek 		status = do_subproc(cmdbuf);
39631867Sgjelinek 
39641867Sgjelinek 		(void) fprintf(stderr, gettext("WARNING: Unable to completely "
39651867Sgjelinek 		    "remove %s\nbecause it contains additional user data.  "
39661867Sgjelinek 		    "Only the standard directory\nentries have been "
39671867Sgjelinek 		    "removed.\n"),
39681867Sgjelinek 		    zonepath);
39691867Sgjelinek 
39702712Snn35248 		return ((subproc_status(RMCOMMAND, status, B_TRUE) ==
39712712Snn35248 		    ZONE_SUBPROC_OK) ? Z_OK : Z_ERR);
39721607Sgjelinek 	}
39731607Sgjelinek 
39741867Sgjelinek 	/*
39751867Sgjelinek 	 * There is nothing unexpected in the zonepath, try to get rid of the
39761867Sgjelinek 	 * whole zonepath directory.
39771867Sgjelinek 	 *
39781867Sgjelinek 	 * If the zonepath is its own zfs file system, try to destroy the
39791867Sgjelinek 	 * file system.  If that fails for some reason (e.g. it has clones)
39801867Sgjelinek 	 * then we'll just remove the contents of the zonepath.
39811867Sgjelinek 	 */
39821867Sgjelinek 	if (is_zonepath_zfs(zonepath)) {
39831867Sgjelinek 		if (destroy_zfs(zonepath) == Z_OK)
39841867Sgjelinek 			return (Z_OK);
39851867Sgjelinek 		(void) snprintf(cmdbuf, sizeof (cmdbuf), "exec " RMCOMMAND
39861867Sgjelinek 		    " %s/*", zonepath);
39871867Sgjelinek 		status = do_subproc(cmdbuf);
39882712Snn35248 		return ((subproc_status(RMCOMMAND, status, B_TRUE) ==
39892712Snn35248 		    ZONE_SUBPROC_OK) ? Z_OK : Z_ERR);
39901867Sgjelinek 	}
39911867Sgjelinek 
39921867Sgjelinek 	(void) snprintf(cmdbuf, sizeof (cmdbuf), "exec " RMCOMMAND " %s",
39931867Sgjelinek 	    zonepath);
39941607Sgjelinek 	status = do_subproc(cmdbuf);
39952712Snn35248 
39962712Snn35248 	return ((subproc_status(RMCOMMAND, status, B_TRUE) == ZONE_SUBPROC_OK)
39972712Snn35248 	    ? Z_OK : Z_ERR);
39981607Sgjelinek }
39991607Sgjelinek 
40001300Sgjelinek static int
40011300Sgjelinek move_func(int argc, char *argv[])
40021300Sgjelinek {
40031300Sgjelinek 	char *new_zonepath = NULL;
40041300Sgjelinek 	int lockfd;
40051300Sgjelinek 	int err, arg;
40061300Sgjelinek 	char zonepath[MAXPATHLEN];
40071300Sgjelinek 	zone_dochandle_t handle;
40081300Sgjelinek 	boolean_t fast;
40091867Sgjelinek 	boolean_t is_zfs = B_FALSE;
40101867Sgjelinek 	struct dirent *dp;
40111867Sgjelinek 	DIR *dirp;
40121867Sgjelinek 	boolean_t empty = B_TRUE;
40131300Sgjelinek 	boolean_t revert;
40141300Sgjelinek 	struct stat zonepath_buf;
40151300Sgjelinek 	struct stat new_zonepath_buf;
40161300Sgjelinek 
40171300Sgjelinek 	if (zonecfg_in_alt_root()) {
40181300Sgjelinek 		zerror(gettext("cannot move zone in alternate root"));
40191300Sgjelinek 		return (Z_ERR);
40201300Sgjelinek 	}
40211300Sgjelinek 
40221300Sgjelinek 	optind = 0;
40231300Sgjelinek 	if ((arg = getopt(argc, argv, "?")) != EOF) {
40241300Sgjelinek 		switch (arg) {
40251300Sgjelinek 		case '?':
40261300Sgjelinek 			sub_usage(SHELP_MOVE, CMD_MOVE);
40271300Sgjelinek 			return (optopt == '?' ? Z_OK : Z_USAGE);
40281300Sgjelinek 		default:
40291300Sgjelinek 			sub_usage(SHELP_MOVE, CMD_MOVE);
40301300Sgjelinek 			return (Z_USAGE);
40311300Sgjelinek 		}
40321300Sgjelinek 	}
40331300Sgjelinek 	if (argc != (optind + 1)) {
40341300Sgjelinek 		sub_usage(SHELP_MOVE, CMD_MOVE);
40351300Sgjelinek 		return (Z_USAGE);
40361300Sgjelinek 	}
40371300Sgjelinek 	new_zonepath = argv[optind];
40382712Snn35248 	if (sanity_check(target_zone, CMD_MOVE, B_FALSE, B_TRUE, B_FALSE)
40392712Snn35248 	    != Z_OK)
40401300Sgjelinek 		return (Z_ERR);
40413339Szt129084 	if (verify_details(CMD_MOVE, argv) != Z_OK)
40421300Sgjelinek 		return (Z_ERR);
40431300Sgjelinek 
40441300Sgjelinek 	/*
40451300Sgjelinek 	 * Check out the new zonepath.  This has the side effect of creating
40461300Sgjelinek 	 * a directory for the new zonepath.  We depend on this later when we
40471867Sgjelinek 	 * stat to see if we are doing a cross file system move or not.
40481300Sgjelinek 	 */
40491300Sgjelinek 	if (validate_zonepath(new_zonepath, CMD_MOVE) != Z_OK)
40501300Sgjelinek 		return (Z_ERR);
40511300Sgjelinek 
40521300Sgjelinek 	if ((err = zone_get_zonepath(target_zone, zonepath, sizeof (zonepath)))
40531300Sgjelinek 	    != Z_OK) {
40541300Sgjelinek 		errno = err;
40551300Sgjelinek 		zperror2(target_zone, gettext("could not get zone path"));
40561300Sgjelinek 		return (Z_ERR);
40571300Sgjelinek 	}
40581300Sgjelinek 
40591300Sgjelinek 	if (stat(zonepath, &zonepath_buf) == -1) {
40601300Sgjelinek 		zperror(gettext("could not stat zone path"), B_FALSE);
40611300Sgjelinek 		return (Z_ERR);
40621300Sgjelinek 	}
40631300Sgjelinek 
40641300Sgjelinek 	if (stat(new_zonepath, &new_zonepath_buf) == -1) {
40651300Sgjelinek 		zperror(gettext("could not stat new zone path"), B_FALSE);
40661300Sgjelinek 		return (Z_ERR);
40671300Sgjelinek 	}
40681300Sgjelinek 
40691867Sgjelinek 	/*
40701867Sgjelinek 	 * Check if the destination directory is empty.
40711867Sgjelinek 	 */
40721867Sgjelinek 	if ((dirp = opendir(new_zonepath)) == NULL) {
40731867Sgjelinek 		zperror(gettext("could not open new zone path"), B_FALSE);
40741867Sgjelinek 		return (Z_ERR);
40751867Sgjelinek 	}
40761867Sgjelinek 	while ((dp = readdir(dirp)) != (struct dirent *)0) {
40771867Sgjelinek 		if (strcmp(dp->d_name, ".") == 0 ||
40781867Sgjelinek 		    strcmp(dp->d_name, "..") == 0)
40791867Sgjelinek 			continue;
40801867Sgjelinek 		empty = B_FALSE;
40811867Sgjelinek 		break;
40821867Sgjelinek 	}
40831867Sgjelinek 	(void) closedir(dirp);
40841867Sgjelinek 
40851867Sgjelinek 	/* Error if there is anything in the destination directory. */
40861867Sgjelinek 	if (!empty) {
40871867Sgjelinek 		(void) fprintf(stderr, gettext("could not move zone to %s: "
40881867Sgjelinek 		    "directory not empty\n"), new_zonepath);
40891867Sgjelinek 		return (Z_ERR);
40901867Sgjelinek 	}
40911867Sgjelinek 
40921300Sgjelinek 	/* Don't move the zone if anything is still mounted there */
40931300Sgjelinek 	if (zonecfg_find_mounts(zonepath, NULL, NULL)) {
40941867Sgjelinek 		zerror(gettext("These file systems are mounted on "
40951300Sgjelinek 		    "subdirectories of %s.\n"), zonepath);
40961300Sgjelinek 		(void) zonecfg_find_mounts(zonepath, zfm_print, NULL);
40971300Sgjelinek 		return (Z_ERR);
40981300Sgjelinek 	}
40991300Sgjelinek 
41001300Sgjelinek 	/*
41011867Sgjelinek 	 * Check if we are moving in the same file system and can do a fast
41021867Sgjelinek 	 * move or if we are crossing file systems and have to copy the data.
41031300Sgjelinek 	 */
41041300Sgjelinek 	fast = (zonepath_buf.st_dev == new_zonepath_buf.st_dev);
41051300Sgjelinek 
41061300Sgjelinek 	if ((handle = zonecfg_init_handle()) == NULL) {
41071300Sgjelinek 		zperror(cmd_to_str(CMD_MOVE), B_TRUE);
41081300Sgjelinek 		return (Z_ERR);
41091300Sgjelinek 	}
41101300Sgjelinek 
41111300Sgjelinek 	if ((err = zonecfg_get_handle(target_zone, handle)) != Z_OK) {
41121300Sgjelinek 		errno = err;
41131300Sgjelinek 		zperror(cmd_to_str(CMD_MOVE), B_TRUE);
41141300Sgjelinek 		zonecfg_fini_handle(handle);
41151300Sgjelinek 		return (Z_ERR);
41161300Sgjelinek 	}
41171300Sgjelinek 
41187089Sgjelinek 	if (zonecfg_grab_lock_file(target_zone, &lockfd) != Z_OK) {
41191300Sgjelinek 		zerror(gettext("another %s may have an operation in progress."),
41201300Sgjelinek 		    "zoneadm");
41211300Sgjelinek 		zonecfg_fini_handle(handle);
41221300Sgjelinek 		return (Z_ERR);
41231300Sgjelinek 	}
41241300Sgjelinek 
41251300Sgjelinek 	/*
41261867Sgjelinek 	 * We're making some file system changes now so we have to clean up
41271867Sgjelinek 	 * the file system before we are done.  This will either clean up the
41281300Sgjelinek 	 * new zonepath if the zonecfg update failed or it will clean up the
41291300Sgjelinek 	 * old zonepath if everything is ok.
41301300Sgjelinek 	 */
41311300Sgjelinek 	revert = B_TRUE;
41321300Sgjelinek 
41331867Sgjelinek 	if (is_zonepath_zfs(zonepath) &&
41341867Sgjelinek 	    move_zfs(zonepath, new_zonepath) != Z_ERR) {
41351867Sgjelinek 		is_zfs = B_TRUE;
41361867Sgjelinek 
41371867Sgjelinek 	} else if (fast) {
41381867Sgjelinek 		/* same file system, use rename for a quick move */
41391300Sgjelinek 
41401300Sgjelinek 		/*
41411300Sgjelinek 		 * Remove the new_zonepath directory that got created above
41421300Sgjelinek 		 * during the validation.  It gets in the way of the rename.
41431300Sgjelinek 		 */
41441300Sgjelinek 		if (rmdir(new_zonepath) != 0) {
41451300Sgjelinek 			zperror(gettext("could not rmdir new zone path"),
41461300Sgjelinek 			    B_FALSE);
41471300Sgjelinek 			zonecfg_fini_handle(handle);
41487089Sgjelinek 			zonecfg_release_lock_file(target_zone, lockfd);
41491300Sgjelinek 			return (Z_ERR);
41501300Sgjelinek 		}
41511300Sgjelinek 
41521300Sgjelinek 		if (rename(zonepath, new_zonepath) != 0) {
41531300Sgjelinek 			/*
41541300Sgjelinek 			 * If this fails we don't need to do all of the
41551300Sgjelinek 			 * cleanup that happens for the rest of the code
41561300Sgjelinek 			 * so just return from this error.
41571300Sgjelinek 			 */
41581300Sgjelinek 			zperror(gettext("could not move zone"), B_FALSE);
41591300Sgjelinek 			zonecfg_fini_handle(handle);
41607089Sgjelinek 			zonecfg_release_lock_file(target_zone, lockfd);
41611300Sgjelinek 			return (Z_ERR);
41621300Sgjelinek 		}
41631300Sgjelinek 
41641300Sgjelinek 	} else {
41651867Sgjelinek 		/*
41661867Sgjelinek 		 * Attempt to create a ZFS fs for the new zonepath.  As usual,
41671867Sgjelinek 		 * we don't care if this works or not since we always have the
41681867Sgjelinek 		 * default behavior of a simple directory for the zonepath.
41691867Sgjelinek 		 */
41701867Sgjelinek 		create_zfs_zonepath(new_zonepath);
41711867Sgjelinek 
41721300Sgjelinek 		(void) printf(gettext(
41731867Sgjelinek 		    "Moving across file systems; copying zonepath %s..."),
41741300Sgjelinek 		    zonepath);
41751300Sgjelinek 		(void) fflush(stdout);
41761300Sgjelinek 
41771300Sgjelinek 		err = copy_zone(zonepath, new_zonepath);
41781300Sgjelinek 
41791300Sgjelinek 		(void) printf("\n");
41801300Sgjelinek 		if (err != Z_OK)
41811300Sgjelinek 			goto done;
41821300Sgjelinek 	}
41831300Sgjelinek 
41841300Sgjelinek 	if ((err = zonecfg_set_zonepath(handle, new_zonepath)) != Z_OK) {
41851300Sgjelinek 		errno = err;
41861300Sgjelinek 		zperror(gettext("could not set new zonepath"), B_TRUE);
41871300Sgjelinek 		goto done;
41881300Sgjelinek 	}
41891300Sgjelinek 
41901300Sgjelinek 	if ((err = zonecfg_save(handle)) != Z_OK) {
41911300Sgjelinek 		errno = err;
41921300Sgjelinek 		zperror(gettext("zonecfg save failed"), B_TRUE);
41931300Sgjelinek 		goto done;
41941300Sgjelinek 	}
41951300Sgjelinek 
41961300Sgjelinek 	revert = B_FALSE;
41971300Sgjelinek 
41981300Sgjelinek done:
41991300Sgjelinek 	zonecfg_fini_handle(handle);
42007089Sgjelinek 	zonecfg_release_lock_file(target_zone, lockfd);
42011300Sgjelinek 
42021300Sgjelinek 	/*
42031867Sgjelinek 	 * Clean up the file system based on how things went.  We either
42041300Sgjelinek 	 * clean up the new zonepath if the operation failed for some reason
42051300Sgjelinek 	 * or we clean up the old zonepath if everything is ok.
42061300Sgjelinek 	 */
42071300Sgjelinek 	if (revert) {
42081300Sgjelinek 		/* The zonecfg update failed, cleanup the new zonepath. */
42091867Sgjelinek 		if (is_zfs) {
42101867Sgjelinek 			if (move_zfs(new_zonepath, zonepath) == Z_ERR) {
42111867Sgjelinek 				(void) fprintf(stderr, gettext("could not "
42121867Sgjelinek 				    "restore zonepath, the zfs mountpoint is "
42131867Sgjelinek 				    "set as:\n%s\n"), new_zonepath);
42141867Sgjelinek 				/*
42151867Sgjelinek 				 * err is already != Z_OK since we're reverting
42161867Sgjelinek 				 */
42171867Sgjelinek 			}
42181867Sgjelinek 
42191867Sgjelinek 		} else if (fast) {
42201300Sgjelinek 			if (rename(new_zonepath, zonepath) != 0) {
42211300Sgjelinek 				zperror(gettext("could not restore zonepath"),
42221300Sgjelinek 				    B_FALSE);
42231300Sgjelinek 				/*
42241300Sgjelinek 				 * err is already != Z_OK since we're reverting
42251300Sgjelinek 				 */
42261300Sgjelinek 			}
42271300Sgjelinek 		} else {
42281300Sgjelinek 			(void) printf(gettext("Cleaning up zonepath %s..."),
42291300Sgjelinek 			    new_zonepath);
42301300Sgjelinek 			(void) fflush(stdout);
42311867Sgjelinek 			err = cleanup_zonepath(new_zonepath, B_TRUE);
42321300Sgjelinek 			(void) printf("\n");
42331300Sgjelinek 
42341607Sgjelinek 			if (err != Z_OK) {
42351300Sgjelinek 				errno = err;
42361300Sgjelinek 				zperror(gettext("could not remove new "
42371300Sgjelinek 				    "zonepath"), B_TRUE);
42381300Sgjelinek 			} else {
42391300Sgjelinek 				/*
42401300Sgjelinek 				 * Because we're reverting we know the mainline
42411300Sgjelinek 				 * code failed but we just reused the err
42421300Sgjelinek 				 * variable so we reset it back to Z_ERR.
42431300Sgjelinek 				 */
42441300Sgjelinek 				err = Z_ERR;
42451300Sgjelinek 			}
42461300Sgjelinek 		}
42471300Sgjelinek 
42481300Sgjelinek 	} else {
42491300Sgjelinek 		/* The move was successful, cleanup the old zonepath. */
42501867Sgjelinek 		if (!is_zfs && !fast) {
42511300Sgjelinek 			(void) printf(
42521300Sgjelinek 			    gettext("Cleaning up zonepath %s..."), zonepath);
42531300Sgjelinek 			(void) fflush(stdout);
42541867Sgjelinek 			err = cleanup_zonepath(zonepath, B_TRUE);
42551300Sgjelinek 			(void) printf("\n");
42561300Sgjelinek 
42571607Sgjelinek 			if (err != Z_OK) {
42581300Sgjelinek 				errno = err;
42591300Sgjelinek 				zperror(gettext("could not remove zonepath"),
42601300Sgjelinek 				    B_TRUE);
42611300Sgjelinek 			}
42621300Sgjelinek 		}
42631300Sgjelinek 	}
42641300Sgjelinek 
42651300Sgjelinek 	return ((err == Z_OK) ? Z_OK : Z_ERR);
42661300Sgjelinek }
42671300Sgjelinek 
42687089Sgjelinek /* ARGSUSED */
42691507Sgjelinek static int
42701507Sgjelinek detach_func(int argc, char *argv[])
42711507Sgjelinek {
42728301Sgerald.jelinek@sun.com 	int lockfd = -1;
42731507Sgjelinek 	int err, arg;
42741507Sgjelinek 	char zonepath[MAXPATHLEN];
42754785Sgjelinek 	char cmdbuf[MAXPATHLEN];
42767089Sgjelinek 	char precmdbuf[MAXPATHLEN];
42772078Sgjelinek 	boolean_t execute = B_TRUE;
42787089Sgjelinek 	boolean_t brand_help = B_FALSE;
42794785Sgjelinek 	brand_handle_t bh = NULL;
42807089Sgjelinek 	int status;
42811507Sgjelinek 
42821507Sgjelinek 	if (zonecfg_in_alt_root()) {
42831507Sgjelinek 		zerror(gettext("cannot detach zone in alternate root"));
42841507Sgjelinek 		return (Z_ERR);
42851507Sgjelinek 	}
42861507Sgjelinek 
42877089Sgjelinek 	/* Check the argv string for args we handle internally */
42881507Sgjelinek 	optind = 0;
42897089Sgjelinek 	opterr = 0;
42907089Sgjelinek 	while ((arg = getopt(argc, argv, "?n")) != EOF) {
42911507Sgjelinek 		switch (arg) {
42921507Sgjelinek 		case '?':
42937089Sgjelinek 			if (optopt == '?') {
42947089Sgjelinek 				sub_usage(SHELP_DETACH, CMD_DETACH);
42957089Sgjelinek 				brand_help = B_TRUE;
42967089Sgjelinek 			}
42977089Sgjelinek 			/* Ignore unknown options - may be brand specific. */
42987089Sgjelinek 			break;
42992078Sgjelinek 		case 'n':
43002078Sgjelinek 			execute = B_FALSE;
43012078Sgjelinek 			break;
43021507Sgjelinek 		default:
43037089Sgjelinek 			/* Ignore unknown options - may be brand specific. */
43047089Sgjelinek 			break;
43051507Sgjelinek 		}
43061507Sgjelinek 	}
43072712Snn35248 
43087089Sgjelinek 	if (brand_help)
43097089Sgjelinek 		execute = B_FALSE;
43107089Sgjelinek 
43112078Sgjelinek 	if (execute) {
43122712Snn35248 		if (sanity_check(target_zone, CMD_DETACH, B_FALSE, B_TRUE,
43132712Snn35248 		    B_FALSE) != Z_OK)
43142078Sgjelinek 			return (Z_ERR);
43153339Szt129084 		if (verify_details(CMD_DETACH, argv) != Z_OK)
43162078Sgjelinek 			return (Z_ERR);
43172078Sgjelinek 	} else {
43182078Sgjelinek 		/*
43192078Sgjelinek 		 * We want a dry-run to work for a non-privileged user so we
43202078Sgjelinek 		 * only do minimal validation.
43212078Sgjelinek 		 */
43222078Sgjelinek 		if (target_zone == NULL) {
43232078Sgjelinek 			zerror(gettext("no zone specified"));
43242078Sgjelinek 			return (Z_ERR);
43252078Sgjelinek 		}
43262078Sgjelinek 
43272078Sgjelinek 		if (strcmp(target_zone, GLOBAL_ZONENAME) == 0) {
43282078Sgjelinek 			zerror(gettext("%s operation is invalid for the "
43292078Sgjelinek 			    "global zone."), cmd_to_str(CMD_DETACH));
43302078Sgjelinek 			return (Z_ERR);
43312078Sgjelinek 		}
43322078Sgjelinek 	}
43331507Sgjelinek 
43341507Sgjelinek 	if ((err = zone_get_zonepath(target_zone, zonepath, sizeof (zonepath)))
43351507Sgjelinek 	    != Z_OK) {
43361507Sgjelinek 		errno = err;
43371507Sgjelinek 		zperror2(target_zone, gettext("could not get zone path"));
43381507Sgjelinek 		return (Z_ERR);
43391507Sgjelinek 	}
43401507Sgjelinek 
43417089Sgjelinek 	/* Fetch the detach and predetach hooks from the brand configuration. */
43424785Sgjelinek 	if ((bh = brand_open(target_brand)) == NULL) {
43434785Sgjelinek 		zerror(gettext("missing or invalid brand"));
43444785Sgjelinek 		return (Z_ERR);
43454785Sgjelinek 	}
43464785Sgjelinek 
43477089Sgjelinek 	if (get_hook(bh, cmdbuf, sizeof (cmdbuf), brand_get_detach, target_zone,
43487089Sgjelinek 	    zonepath) != Z_OK) {
43497089Sgjelinek 		zerror("invalid brand configuration: missing detach resource");
43507089Sgjelinek 		brand_close(bh);
43517089Sgjelinek 		return (Z_ERR);
43527089Sgjelinek 	}
43537089Sgjelinek 
43547089Sgjelinek 	if (get_hook(bh, precmdbuf, sizeof (precmdbuf), brand_get_predetach,
43557089Sgjelinek 	    target_zone, zonepath) != Z_OK) {
43564785Sgjelinek 		zerror("invalid brand configuration: missing predetach "
43574785Sgjelinek 		    "resource");
43584785Sgjelinek 		brand_close(bh);
43594785Sgjelinek 		return (Z_ERR);
43604785Sgjelinek 	}
43614785Sgjelinek 	brand_close(bh);
43624785Sgjelinek 
43637089Sgjelinek 	/* Append all options to predetach hook. */
43647089Sgjelinek 	if (addoptions(precmdbuf, argv, sizeof (precmdbuf)) != Z_OK)
43657089Sgjelinek 		return (Z_ERR);
43667089Sgjelinek 
43677089Sgjelinek 	/* Append all options to detach hook. */
43687089Sgjelinek 	if (addoptions(cmdbuf, argv, sizeof (cmdbuf)) != Z_OK)
43697089Sgjelinek 		return (Z_ERR);
43707089Sgjelinek 
43717089Sgjelinek 	if (execute && zonecfg_grab_lock_file(target_zone, &lockfd) != Z_OK) {
43721507Sgjelinek 		zerror(gettext("another %s may have an operation in progress."),
43731507Sgjelinek 		    "zoneadm");
43741507Sgjelinek 		return (Z_ERR);
43751507Sgjelinek 	}
43761507Sgjelinek 
43777089Sgjelinek 	/* If we have a brand predetach hook, run it. */
43787089Sgjelinek 	if (!brand_help && precmdbuf[0] != '\0') {
43797089Sgjelinek 		status = do_subproc(precmdbuf);
43807089Sgjelinek 		if (subproc_status(gettext("brand-specific predetach"),
43817089Sgjelinek 		    status, B_FALSE) != ZONE_SUBPROC_OK) {
43827089Sgjelinek 
43838301Sgerald.jelinek@sun.com 			if (execute) {
43848301Sgerald.jelinek@sun.com 				assert(lockfd >= 0);
43857089Sgjelinek 				zonecfg_release_lock_file(target_zone, lockfd);
43868301Sgerald.jelinek@sun.com 				lockfd = -1;
43878301Sgerald.jelinek@sun.com 			}
43888301Sgerald.jelinek@sun.com 
43898301Sgerald.jelinek@sun.com 			assert(lockfd == -1);
43907089Sgjelinek 			return (Z_ERR);
43917089Sgjelinek 		}
43927089Sgjelinek 	}
43937089Sgjelinek 
43947089Sgjelinek 	if (cmdbuf[0] != '\0') {
43957089Sgjelinek 		/* Run the detach hook */
43967089Sgjelinek 		status = do_subproc_interactive(cmdbuf);
43977089Sgjelinek 		if ((status = subproc_status(gettext("brand-specific detach"),
43987089Sgjelinek 		    status, B_FALSE)) != ZONE_SUBPROC_OK) {
43997089Sgjelinek 			if (status == ZONE_SUBPROC_USAGE && !brand_help)
44007089Sgjelinek 				sub_usage(SHELP_DETACH, CMD_DETACH);
44017089Sgjelinek 
44028301Sgerald.jelinek@sun.com 			if (execute) {
44038301Sgerald.jelinek@sun.com 				assert(lockfd >= 0);
44047089Sgjelinek 				zonecfg_release_lock_file(target_zone, lockfd);
44058301Sgerald.jelinek@sun.com 				lockfd = -1;
44068301Sgerald.jelinek@sun.com 			}
44078301Sgerald.jelinek@sun.com 
44088301Sgerald.jelinek@sun.com 			assert(lockfd == -1);
44097089Sgjelinek 			return (Z_ERR);
44107089Sgjelinek 		}
44117089Sgjelinek 
44127089Sgjelinek 	} else {
44138301Sgerald.jelinek@sun.com 		zone_dochandle_t handle;
44148301Sgerald.jelinek@sun.com 
44157089Sgjelinek 		/* If just help, we're done since there is no brand help. */
44168301Sgerald.jelinek@sun.com 		if (brand_help) {
44178301Sgerald.jelinek@sun.com 			assert(lockfd == -1);
44187089Sgjelinek 			return (Z_OK);
44198301Sgerald.jelinek@sun.com 		}
44207089Sgjelinek 
44217089Sgjelinek 		/*
44227089Sgjelinek 		 * Run the built-in detach support.  Just generate a simple
44237089Sgjelinek 		 * zone definition XML file and detach.
44247089Sgjelinek 		 */
44257089Sgjelinek 
44267089Sgjelinek 		/* Don't detach the zone if anything is still mounted there */
44277089Sgjelinek 		if (execute && zonecfg_find_mounts(zonepath, NULL, NULL)) {
44287089Sgjelinek 			(void) fprintf(stderr, gettext("These file systems are "
44297089Sgjelinek 			    "mounted on subdirectories of %s.\n"), zonepath);
44307089Sgjelinek 			(void) zonecfg_find_mounts(zonepath, zfm_print, NULL);
44317089Sgjelinek 			err = ZONE_SUBPROC_NOTCOMPLETE;
44327089Sgjelinek 			goto done;
44337089Sgjelinek 		}
44347089Sgjelinek 
44357089Sgjelinek 		if ((handle = zonecfg_init_handle()) == NULL) {
44367089Sgjelinek 			zperror(cmd_to_str(CMD_DETACH), B_TRUE);
44377089Sgjelinek 			err = ZONE_SUBPROC_NOTCOMPLETE;
44387089Sgjelinek 			goto done;
44397089Sgjelinek 		}
44407089Sgjelinek 
44417089Sgjelinek 		if ((err = zonecfg_get_handle(target_zone, handle)) != Z_OK) {
44427089Sgjelinek 			errno = err;
44437089Sgjelinek 			zperror(cmd_to_str(CMD_DETACH), B_TRUE);
44448301Sgerald.jelinek@sun.com 
44458301Sgerald.jelinek@sun.com 		} else if ((err = zonecfg_detach_save(handle,
44467089Sgjelinek 		    (execute ? 0 : ZONE_DRY_RUN))) != Z_OK) {
44477089Sgjelinek 			errno = err;
44487089Sgjelinek 			zperror(gettext("saving the detach manifest failed"),
44497089Sgjelinek 			    B_TRUE);
44507089Sgjelinek 		}
44517089Sgjelinek 
44527089Sgjelinek 		zonecfg_fini_handle(handle);
44538301Sgerald.jelinek@sun.com 		if (err != Z_OK)
44548301Sgerald.jelinek@sun.com 			goto done;
44551507Sgjelinek 	}
44561507Sgjelinek 
44572078Sgjelinek 	/*
44582078Sgjelinek 	 * Set the zone state back to configured unless we are running with the
44592078Sgjelinek 	 * no-execute option.
44602078Sgjelinek 	 */
44612078Sgjelinek 	if (execute && (err = zone_set_state(target_zone,
44622078Sgjelinek 	    ZONE_STATE_CONFIGURED)) != Z_OK) {
44631507Sgjelinek 		errno = err;
44641507Sgjelinek 		zperror(gettext("could not reset state"), B_TRUE);
44651507Sgjelinek 	}
44661507Sgjelinek 
44671507Sgjelinek done:
44688301Sgerald.jelinek@sun.com 	if (execute) {
44698301Sgerald.jelinek@sun.com 		assert(lockfd >= 0);
44707089Sgjelinek 		zonecfg_release_lock_file(target_zone, lockfd);
44718301Sgerald.jelinek@sun.com 		lockfd = -1;
44728301Sgerald.jelinek@sun.com 	}
44738301Sgerald.jelinek@sun.com 
44748301Sgerald.jelinek@sun.com 	assert(lockfd == -1);
44751507Sgjelinek 	return ((err == Z_OK) ? Z_OK : Z_ERR);
44761507Sgjelinek }
44771507Sgjelinek 
44781507Sgjelinek /*
44797089Sgjelinek  * Determine the brand when doing a dry-run attach.  The zone does not have to
44807089Sgjelinek  * exist, so we have to read the incoming manifest to determine the zone's
44817089Sgjelinek  * brand.
44827089Sgjelinek  *
44837089Sgjelinek  * Because the manifest has to be processed twice; once to determine the brand
44847089Sgjelinek  * and once to do the brand-specific attach logic, we always read it into a tmp
44857089Sgjelinek  * file.  This handles the manifest coming from stdin or a regular file.  The
44867089Sgjelinek  * tmpname parameter returns the name of the temporary file that the manifest
44877089Sgjelinek  * was read into.
44881507Sgjelinek  */
44891507Sgjelinek static int
44907089Sgjelinek dryrun_get_brand(char *manifest_path, char *tmpname, int size)
44912078Sgjelinek {
44922078Sgjelinek 	int fd;
44932078Sgjelinek 	int err;
44947089Sgjelinek 	int res = Z_OK;
44952078Sgjelinek 	zone_dochandle_t local_handle;
44962078Sgjelinek 	zone_dochandle_t rem_handle = NULL;
44977089Sgjelinek 	int len;
44987089Sgjelinek 	int ofd;
44997089Sgjelinek 	char buf[512];
45002078Sgjelinek 
45012078Sgjelinek 	if (strcmp(manifest_path, "-") == 0) {
45027089Sgjelinek 		fd = STDIN_FILENO;
45037089Sgjelinek 	} else {
45047089Sgjelinek 		if ((fd = open(manifest_path, O_RDONLY)) < 0) {
45057089Sgjelinek 			if (getcwd(buf, sizeof (buf)) == NULL)
45067089Sgjelinek 				(void) strlcpy(buf, "/", sizeof (buf));
45077089Sgjelinek 			zerror(gettext("could not open manifest path %s%s: %s"),
45087089Sgjelinek 			    (*manifest_path == '/' ? "" : buf), manifest_path,
45097089Sgjelinek 			    strerror(errno));
45107089Sgjelinek 			return (Z_ERR);
45117089Sgjelinek 		}
45127089Sgjelinek 	}
45137089Sgjelinek 
45147089Sgjelinek 	(void) snprintf(tmpname, size, "/var/run/zone.%d", getpid());
45157089Sgjelinek 
45167089Sgjelinek 	if ((ofd = open(tmpname, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR)) < 0) {
45177089Sgjelinek 		zperror(gettext("could not save manifest"), B_FALSE);
45187089Sgjelinek 		(void) close(fd);
45197089Sgjelinek 		return (Z_ERR);
45207089Sgjelinek 	}
45217089Sgjelinek 
45227089Sgjelinek 	while ((len = read(fd, buf, sizeof (buf))) > 0) {
45237089Sgjelinek 		if (write(ofd, buf, len) == -1) {
45247089Sgjelinek 			zperror(gettext("could not save manifest"), B_FALSE);
45257089Sgjelinek 			(void) close(ofd);
45267089Sgjelinek 			(void) close(fd);
45277089Sgjelinek 			return (Z_ERR);
45287089Sgjelinek 		}
45297089Sgjelinek 	}
45307089Sgjelinek 
45317089Sgjelinek 	if (close(ofd) != 0) {
45327089Sgjelinek 		zperror(gettext("could not save manifest"), B_FALSE);
45337089Sgjelinek 		(void) close(fd);
45347089Sgjelinek 		return (Z_ERR);
45357089Sgjelinek 	}
45367089Sgjelinek 
45377089Sgjelinek 	(void) close(fd);
45387089Sgjelinek 
45397089Sgjelinek 	if ((fd = open(tmpname, O_RDONLY)) < 0) {
45402078Sgjelinek 		zperror(gettext("could not open manifest path"), B_FALSE);
45412078Sgjelinek 		return (Z_ERR);
45422078Sgjelinek 	}
45432078Sgjelinek 
45442078Sgjelinek 	if ((local_handle = zonecfg_init_handle()) == NULL) {
45452078Sgjelinek 		zperror(cmd_to_str(CMD_ATTACH), B_TRUE);
45462078Sgjelinek 		res = Z_ERR;
45472078Sgjelinek 		goto done;
45482078Sgjelinek 	}
45492078Sgjelinek 
45502078Sgjelinek 	if ((rem_handle = zonecfg_init_handle()) == NULL) {
45512078Sgjelinek 		zperror(cmd_to_str(CMD_ATTACH), B_TRUE);
45522078Sgjelinek 		res = Z_ERR;
45532078Sgjelinek 		goto done;
45542078Sgjelinek 	}
45552078Sgjelinek 
45562078Sgjelinek 	if ((err = zonecfg_attach_manifest(fd, local_handle, rem_handle))
45572078Sgjelinek 	    != Z_OK) {
45583686Sgjelinek 		res = Z_ERR;
45593686Sgjelinek 
45603686Sgjelinek 		if (err == Z_INVALID_DOCUMENT) {
45613686Sgjelinek 			struct stat st;
45623686Sgjelinek 			char buf[6];
45633686Sgjelinek 
45643686Sgjelinek 			if (strcmp(manifest_path, "-") == 0) {
45653686Sgjelinek 				zerror(gettext("Input is not a valid XML "
45663686Sgjelinek 				    "file"));
45673686Sgjelinek 				goto done;
45683686Sgjelinek 			}
45693686Sgjelinek 
45703686Sgjelinek 			if (fstat(fd, &st) == -1 || !S_ISREG(st.st_mode)) {
45713686Sgjelinek 				zerror(gettext("%s is not an XML file"),
45723686Sgjelinek 				    manifest_path);
45733686Sgjelinek 				goto done;
45743686Sgjelinek 			}
45753686Sgjelinek 
45763686Sgjelinek 			bzero(buf, sizeof (buf));
45773686Sgjelinek 			(void) lseek(fd, 0L, SEEK_SET);
45783686Sgjelinek 			if (read(fd, buf, sizeof (buf) - 1) < 0 ||
45793686Sgjelinek 			    strncmp(buf, "<?xml", 5) != 0)
45803686Sgjelinek 				zerror(gettext("%s is not an XML file"),
45813686Sgjelinek 				    manifest_path);
45823686Sgjelinek 			else
45833686Sgjelinek 				zerror(gettext("Cannot attach to an earlier "
45843686Sgjelinek 				    "release of the operating system"));
45853686Sgjelinek 		} else {
45862078Sgjelinek 			zperror(cmd_to_str(CMD_ATTACH), B_TRUE);
45873686Sgjelinek 		}
45882078Sgjelinek 		goto done;
45892078Sgjelinek 	}
45902078Sgjelinek 
45917089Sgjelinek 	/* Retrieve remote handle brand type. */
45923172Sgjelinek 	if (zonecfg_get_brand(rem_handle, target_brand, sizeof (target_brand))
45933172Sgjelinek 	    != Z_OK) {
45943172Sgjelinek 		zerror(gettext("missing or invalid brand"));
45953172Sgjelinek 		exit(Z_ERR);
45963172Sgjelinek 	}
45972078Sgjelinek 
45982078Sgjelinek done:
45992078Sgjelinek 	zonecfg_fini_handle(local_handle);
46002078Sgjelinek 	zonecfg_fini_handle(rem_handle);
46017089Sgjelinek 	(void) close(fd);
46022078Sgjelinek 
46032078Sgjelinek 	return ((res == Z_OK) ? Z_OK : Z_ERR);
46042078Sgjelinek }
46052078Sgjelinek 
46065829Sgjelinek /* ARGSUSED */
46071507Sgjelinek static int
46081507Sgjelinek attach_func(int argc, char *argv[])
46091507Sgjelinek {
46108301Sgerald.jelinek@sun.com 	int lockfd = -1;
46111507Sgjelinek 	int err, arg;
46121507Sgjelinek 	boolean_t force = B_FALSE;
46131507Sgjelinek 	zone_dochandle_t handle;
46141507Sgjelinek 	char zonepath[MAXPATHLEN];
46154785Sgjelinek 	char cmdbuf[MAXPATHLEN];
46167089Sgjelinek 	char postcmdbuf[MAXPATHLEN];
46172078Sgjelinek 	boolean_t execute = B_TRUE;
46187089Sgjelinek 	boolean_t brand_help = B_FALSE;
46192078Sgjelinek 	char *manifest_path;
46207089Sgjelinek 	char tmpmanifest[80];
46217089Sgjelinek 	int manifest_pos;
46224785Sgjelinek 	brand_handle_t bh = NULL;
46237089Sgjelinek 	int status;
46248759Sgerald.jelinek@sun.com 	int last_index = 0;
46258759Sgerald.jelinek@sun.com 	int offset;
46268759Sgerald.jelinek@sun.com 	char *up;
46278759Sgerald.jelinek@sun.com 	boolean_t forced_update = B_FALSE;
46281507Sgjelinek 
46291507Sgjelinek 	if (zonecfg_in_alt_root()) {
46301507Sgjelinek 		zerror(gettext("cannot attach zone in alternate root"));
46311507Sgjelinek 		return (Z_ERR);
46321507Sgjelinek 	}
46331507Sgjelinek 
46347089Sgjelinek 	/* Check the argv string for args we handle internally */
46351507Sgjelinek 	optind = 0;
46367089Sgjelinek 	opterr = 0;
46378759Sgerald.jelinek@sun.com 	while ((arg = getopt(argc, argv, "?Fn:U")) != EOF) {
46381507Sgjelinek 		switch (arg) {
46391507Sgjelinek 		case '?':
46407089Sgjelinek 			if (optopt == '?') {
46417089Sgjelinek 				sub_usage(SHELP_ATTACH, CMD_ATTACH);
46427089Sgjelinek 				brand_help = B_TRUE;
46437089Sgjelinek 			}
46447089Sgjelinek 			/* Ignore unknown options - may be brand specific. */
46457089Sgjelinek 			break;
46461507Sgjelinek 		case 'F':
46471507Sgjelinek 			force = B_TRUE;
46481507Sgjelinek 			break;
46492078Sgjelinek 		case 'n':
46502078Sgjelinek 			execute = B_FALSE;
46512078Sgjelinek 			manifest_path = optarg;
46527089Sgjelinek 			manifest_pos = optind - 1;
46535829Sgjelinek 			break;
46548759Sgerald.jelinek@sun.com 		case 'U':
46558759Sgerald.jelinek@sun.com 			/*
46568759Sgerald.jelinek@sun.com 			 * Undocumented 'force update' option for p2v update on
46578759Sgerald.jelinek@sun.com 			 * attach when zone is in the incomplete state.  Change
46588759Sgerald.jelinek@sun.com 			 * the option back to 'u' and set forced_update flag.
46598759Sgerald.jelinek@sun.com 			 */
46608759Sgerald.jelinek@sun.com 			if (optind == last_index)
46618759Sgerald.jelinek@sun.com 				offset = optind;
46628759Sgerald.jelinek@sun.com 			else
46638759Sgerald.jelinek@sun.com 				offset = optind - 1;
46648759Sgerald.jelinek@sun.com 			if ((up = index(argv[offset], 'U')) != NULL)
46658759Sgerald.jelinek@sun.com 				*up = 'u';
46668759Sgerald.jelinek@sun.com 			forced_update = B_TRUE;
46678759Sgerald.jelinek@sun.com 			break;
46681507Sgjelinek 		default:
46697089Sgjelinek 			/* Ignore unknown options - may be brand specific. */
46707089Sgjelinek 			break;
46711507Sgjelinek 		}
46728759Sgerald.jelinek@sun.com 		last_index = optind;
46731507Sgjelinek 	}
46742078Sgjelinek 
46757089Sgjelinek 	if (brand_help) {
46767089Sgjelinek 		force = B_FALSE;
46777089Sgjelinek 		execute = B_TRUE;
46787089Sgjelinek 	}
46797089Sgjelinek 
46807089Sgjelinek 	/* dry-run and force flags are mutually exclusive */
46817089Sgjelinek 	if (!execute && force) {
46827089Sgjelinek 		zerror(gettext("-F and -n flags are mutually exclusive"));
46835829Sgjelinek 		return (Z_ERR);
46845829Sgjelinek 	}
46855829Sgjelinek 
46862078Sgjelinek 	/*
46877089Sgjelinek 	 * If the no-execute option was specified, we don't do validation and
46887089Sgjelinek 	 * need to figure out the brand, since there is no zone required to be
46892078Sgjelinek 	 * configured for this option.
46902078Sgjelinek 	 */
46917089Sgjelinek 	if (execute) {
46927089Sgjelinek 		if (!brand_help) {
46937089Sgjelinek 			if (sanity_check(target_zone, CMD_ATTACH, B_FALSE,
46948759Sgerald.jelinek@sun.com 			    B_TRUE, forced_update) != Z_OK)
46957089Sgjelinek 				return (Z_ERR);
46967089Sgjelinek 			if (verify_details(CMD_ATTACH, argv) != Z_OK)
46977089Sgjelinek 				return (Z_ERR);
46987089Sgjelinek 		}
46997089Sgjelinek 
47007089Sgjelinek 		if ((err = zone_get_zonepath(target_zone, zonepath,
47017089Sgjelinek 		    sizeof (zonepath))) != Z_OK) {
47027089Sgjelinek 			errno = err;
47037089Sgjelinek 			zperror2(target_zone,
47047089Sgjelinek 			    gettext("could not get zone path"));
47057089Sgjelinek 			return (Z_ERR);
47067089Sgjelinek 		}
47077089Sgjelinek 	} else {
47087089Sgjelinek 		if (dryrun_get_brand(manifest_path, tmpmanifest,
47097089Sgjelinek 		    sizeof (tmpmanifest)) != Z_OK)
47107089Sgjelinek 			return (Z_ERR);
47117089Sgjelinek 
47127089Sgjelinek 		argv[manifest_pos] = tmpmanifest;
47137089Sgjelinek 		target_zone = "-";
47147089Sgjelinek 		(void) strlcpy(zonepath, "-", sizeof (zonepath));
47157089Sgjelinek 
47167089Sgjelinek 		/* Run the brand's verify_adm hook. */
47177089Sgjelinek 		if (verify_brand(NULL, CMD_ATTACH, argv) != Z_OK)
47187089Sgjelinek 			return (Z_ERR);
47197089Sgjelinek 	}
47207089Sgjelinek 
47217089Sgjelinek 	/*
47227089Sgjelinek 	 * Fetch the attach and postattach hooks from the brand configuration.
47237089Sgjelinek 	 */
47244785Sgjelinek 	if ((bh = brand_open(target_brand)) == NULL) {
47254785Sgjelinek 		zerror(gettext("missing or invalid brand"));
47264785Sgjelinek 		return (Z_ERR);
47274785Sgjelinek 	}
47284785Sgjelinek 
47297089Sgjelinek 	if (get_hook(bh, cmdbuf, sizeof (cmdbuf), brand_get_attach, target_zone,
47307089Sgjelinek 	    zonepath) != Z_OK) {
47317089Sgjelinek 		zerror("invalid brand configuration: missing attach resource");
47327089Sgjelinek 		brand_close(bh);
47337089Sgjelinek 		return (Z_ERR);
47347089Sgjelinek 	}
47357089Sgjelinek 
47367089Sgjelinek 	if (get_hook(bh, postcmdbuf, sizeof (postcmdbuf), brand_get_postattach,
47377089Sgjelinek 	    target_zone, zonepath) != Z_OK) {
47384785Sgjelinek 		zerror("invalid brand configuration: missing postattach "
47394785Sgjelinek 		    "resource");
47404785Sgjelinek 		brand_close(bh);
47414785Sgjelinek 		return (Z_ERR);
47424785Sgjelinek 	}
47434785Sgjelinek 	brand_close(bh);
47444785Sgjelinek 
47457089Sgjelinek 	/* Append all options to attach hook. */
47467089Sgjelinek 	if (addoptions(cmdbuf, argv, sizeof (cmdbuf)) != Z_OK)
47477089Sgjelinek 		return (Z_ERR);
47487089Sgjelinek 
47497089Sgjelinek 	/* Append all options to postattach hook. */
47507089Sgjelinek 	if (addoptions(postcmdbuf, argv, sizeof (postcmdbuf)) != Z_OK)
47517089Sgjelinek 		return (Z_ERR);
47527089Sgjelinek 
47537089Sgjelinek 	if (execute && !brand_help) {
47547089Sgjelinek 		if (zonecfg_grab_lock_file(target_zone, &lockfd) != Z_OK) {
47557089Sgjelinek 			zerror(gettext("another %s may have an operation in "
47567089Sgjelinek 			    "progress."), "zoneadm");
47577089Sgjelinek 			return (Z_ERR);
47583777Sgjelinek 		}
47598301Sgerald.jelinek@sun.com 	}
47608301Sgerald.jelinek@sun.com 
47618301Sgerald.jelinek@sun.com 	if (!force) {
47628301Sgerald.jelinek@sun.com 		/*
47638301Sgerald.jelinek@sun.com 		 * Not a force-attach, so we need to actually do the work.
47648301Sgerald.jelinek@sun.com 		 */
47658301Sgerald.jelinek@sun.com 		if (cmdbuf[0] != '\0') {
47668301Sgerald.jelinek@sun.com 			/* Run the attach hook */
47678759Sgerald.jelinek@sun.com 			status = do_subproc(cmdbuf);
47688301Sgerald.jelinek@sun.com 			if ((status = subproc_status(gettext("brand-specific "
47698301Sgerald.jelinek@sun.com 			    "attach"), status, B_FALSE)) != ZONE_SUBPROC_OK) {
47708301Sgerald.jelinek@sun.com 				if (status == ZONE_SUBPROC_USAGE && !brand_help)
47718301Sgerald.jelinek@sun.com 					sub_usage(SHELP_ATTACH, CMD_ATTACH);
47728301Sgerald.jelinek@sun.com 
47738301Sgerald.jelinek@sun.com 				if (execute && !brand_help) {
47748759Sgerald.jelinek@sun.com 					assert(zonecfg_lock_file_held(&lockfd));
47758301Sgerald.jelinek@sun.com 					zonecfg_release_lock_file(target_zone,
47768301Sgerald.jelinek@sun.com 					    lockfd);
47778301Sgerald.jelinek@sun.com 					lockfd = -1;
47788301Sgerald.jelinek@sun.com 				}
47798301Sgerald.jelinek@sun.com 
47808301Sgerald.jelinek@sun.com 				assert(lockfd == -1);
47818301Sgerald.jelinek@sun.com 				return (Z_ERR);
47828301Sgerald.jelinek@sun.com 			}
47838301Sgerald.jelinek@sun.com 		}
47848301Sgerald.jelinek@sun.com 
47858301Sgerald.jelinek@sun.com 		/*
47868301Sgerald.jelinek@sun.com 		 * Else run the built-in attach support.
47878301Sgerald.jelinek@sun.com 		 * This is a no-op since there is nothing to validate.
47888301Sgerald.jelinek@sun.com 		 */
47898301Sgerald.jelinek@sun.com 
47908301Sgerald.jelinek@sun.com 		/* If dry-run or help, then we're done. */
47918301Sgerald.jelinek@sun.com 		if (!execute || brand_help) {
47928301Sgerald.jelinek@sun.com 			if (!execute)
47938301Sgerald.jelinek@sun.com 				(void) unlink(tmpmanifest);
47948301Sgerald.jelinek@sun.com 			assert(lockfd == -1);
47958301Sgerald.jelinek@sun.com 			return (Z_OK);
47968301Sgerald.jelinek@sun.com 		}
47978301Sgerald.jelinek@sun.com 	}
47988301Sgerald.jelinek@sun.com 
4799*9128Sgerald.jelinek@sun.com 	/* Now we can validate that the zonepath exists. */
4800*9128Sgerald.jelinek@sun.com 	if (validate_zonepath(zonepath, CMD_ATTACH) != Z_OK) {
4801*9128Sgerald.jelinek@sun.com 		(void) fprintf(stderr, gettext("could not verify zonepath %s "
4802*9128Sgerald.jelinek@sun.com 		    "because of the above errors.\n"), zonepath);
4803*9128Sgerald.jelinek@sun.com 
4804*9128Sgerald.jelinek@sun.com 		assert(zonecfg_lock_file_held(&lockfd));
4805*9128Sgerald.jelinek@sun.com 		zonecfg_release_lock_file(target_zone, lockfd);
4806*9128Sgerald.jelinek@sun.com 		return (Z_ERR);
4807*9128Sgerald.jelinek@sun.com 	}
4808*9128Sgerald.jelinek@sun.com 
48098301Sgerald.jelinek@sun.com 	if ((handle = zonecfg_init_handle()) == NULL) {
48108301Sgerald.jelinek@sun.com 		zperror(cmd_to_str(CMD_ATTACH), B_TRUE);
48118301Sgerald.jelinek@sun.com 		err = Z_ERR;
48128301Sgerald.jelinek@sun.com 	} else if ((err = zonecfg_get_handle(target_zone, handle)) != Z_OK) {
48138301Sgerald.jelinek@sun.com 		errno = err;
48148301Sgerald.jelinek@sun.com 		zperror(cmd_to_str(CMD_ATTACH), B_TRUE);
48158301Sgerald.jelinek@sun.com 		zonecfg_fini_handle(handle);
48168301Sgerald.jelinek@sun.com 	} else {
48178301Sgerald.jelinek@sun.com 		zonecfg_rm_detached(handle, force);
48188301Sgerald.jelinek@sun.com 		zonecfg_fini_handle(handle);
48198301Sgerald.jelinek@sun.com 	}
48208301Sgerald.jelinek@sun.com 
48218301Sgerald.jelinek@sun.com 	if (err == Z_OK &&
48228301Sgerald.jelinek@sun.com 	    (err = zone_set_state(target_zone, ZONE_STATE_INSTALLED)) != Z_OK) {
48231507Sgjelinek 		errno = err;
48241507Sgjelinek 		zperror(gettext("could not reset state"), B_TRUE);
48251507Sgjelinek 	}
48261507Sgjelinek 
48278759Sgerald.jelinek@sun.com 	assert(zonecfg_lock_file_held(&lockfd));
48287089Sgjelinek 	zonecfg_release_lock_file(target_zone, lockfd);
48298301Sgerald.jelinek@sun.com 	lockfd = -1;
48301507Sgjelinek 
48314785Sgjelinek 	/* If we have a brand postattach hook, run it. */
48327089Sgjelinek 	if (err == Z_OK && !force && postcmdbuf[0] != '\0') {
48337089Sgjelinek 		status = do_subproc(postcmdbuf);
48344785Sgjelinek 		if (subproc_status(gettext("brand-specific postattach"),
48354785Sgjelinek 		    status, B_FALSE) != ZONE_SUBPROC_OK) {
48364785Sgjelinek 			if ((err = zone_set_state(target_zone,
48374785Sgjelinek 			    ZONE_STATE_CONFIGURED)) != Z_OK) {
48384785Sgjelinek 				errno = err;
48394785Sgjelinek 				zperror(gettext("could not reset state"),
48404785Sgjelinek 				    B_TRUE);
48414785Sgjelinek 			}
48424785Sgjelinek 		}
48434785Sgjelinek 	}
48444785Sgjelinek 
48458301Sgerald.jelinek@sun.com 	assert(lockfd == -1);
48461507Sgjelinek 	return ((err == Z_OK) ? Z_OK : Z_ERR);
48471507Sgjelinek }
48481507Sgjelinek 
48491300Sgjelinek /*
48500Sstevel@tonic-gate  * On input, TRUE => yes, FALSE => no.
48510Sstevel@tonic-gate  * On return, TRUE => 1, FALSE => 0, could not ask => -1.
48520Sstevel@tonic-gate  */
48530Sstevel@tonic-gate 
48540Sstevel@tonic-gate static int
48550Sstevel@tonic-gate ask_yesno(boolean_t default_answer, const char *question)
48560Sstevel@tonic-gate {
48570Sstevel@tonic-gate 	char line[64];	/* should be large enough to answer yes or no */
48580Sstevel@tonic-gate 
48590Sstevel@tonic-gate 	if (!isatty(STDIN_FILENO))
48600Sstevel@tonic-gate 		return (-1);
48610Sstevel@tonic-gate 	for (;;) {
48620Sstevel@tonic-gate 		(void) printf("%s (%s)? ", question,
48630Sstevel@tonic-gate 		    default_answer ? "[y]/n" : "y/[n]");
48640Sstevel@tonic-gate 		if (fgets(line, sizeof (line), stdin) == NULL ||
48650Sstevel@tonic-gate 		    line[0] == '\n')
48660Sstevel@tonic-gate 			return (default_answer ? 1 : 0);
48670Sstevel@tonic-gate 		if (tolower(line[0]) == 'y')
48680Sstevel@tonic-gate 			return (1);
48690Sstevel@tonic-gate 		if (tolower(line[0]) == 'n')
48700Sstevel@tonic-gate 			return (0);
48710Sstevel@tonic-gate 	}
48720Sstevel@tonic-gate }
48730Sstevel@tonic-gate 
48747089Sgjelinek /* ARGSUSED */
48750Sstevel@tonic-gate static int
48760Sstevel@tonic-gate uninstall_func(int argc, char *argv[])
48770Sstevel@tonic-gate {
48780Sstevel@tonic-gate 	char line[ZONENAME_MAX + 128];	/* Enough for "Are you sure ..." */
48791867Sgjelinek 	char rootpath[MAXPATHLEN], zonepath[MAXPATHLEN];
48804785Sgjelinek 	char cmdbuf[MAXPATHLEN];
48817089Sgjelinek 	char precmdbuf[MAXPATHLEN];
48820Sstevel@tonic-gate 	boolean_t force = B_FALSE;
48830Sstevel@tonic-gate 	int lockfd, answer;
48840Sstevel@tonic-gate 	int err, arg;
48857089Sgjelinek 	boolean_t brand_help = B_FALSE;
48864785Sgjelinek 	brand_handle_t bh = NULL;
48877089Sgjelinek 	int status;
48880Sstevel@tonic-gate 
4889766Scarlsonj 	if (zonecfg_in_alt_root()) {
4890766Scarlsonj 		zerror(gettext("cannot uninstall zone in alternate root"));
4891766Scarlsonj 		return (Z_ERR);
4892766Scarlsonj 	}
4893766Scarlsonj 
48947089Sgjelinek 	/* Check the argv string for args we handle internally */
48950Sstevel@tonic-gate 	optind = 0;
48967089Sgjelinek 	opterr = 0;
48970Sstevel@tonic-gate 	while ((arg = getopt(argc, argv, "?F")) != EOF) {
48980Sstevel@tonic-gate 		switch (arg) {
48990Sstevel@tonic-gate 		case '?':
49007089Sgjelinek 			if (optopt == '?') {
49017089Sgjelinek 				sub_usage(SHELP_UNINSTALL, CMD_UNINSTALL);
49027089Sgjelinek 				brand_help = B_TRUE;
49037089Sgjelinek 			}
49047089Sgjelinek 			/* Ignore unknown options - may be brand specific. */
49057089Sgjelinek 			break;
49060Sstevel@tonic-gate 		case 'F':
49070Sstevel@tonic-gate 			force = B_TRUE;
49080Sstevel@tonic-gate 			break;
49090Sstevel@tonic-gate 		default:
49107089Sgjelinek 			/* Ignore unknown options - may be brand specific. */
49117089Sgjelinek 			break;
49120Sstevel@tonic-gate 		}
49130Sstevel@tonic-gate 	}
49147089Sgjelinek 
49157089Sgjelinek 	if (!brand_help) {
49167089Sgjelinek 		if (sanity_check(target_zone, CMD_UNINSTALL, B_FALSE, B_TRUE,
49177089Sgjelinek 		    B_FALSE) != Z_OK)
49187089Sgjelinek 			return (Z_ERR);
49197089Sgjelinek 
49207089Sgjelinek 		/*
49217089Sgjelinek 		 * Invoke brand-specific handler.
49227089Sgjelinek 		 */
49237089Sgjelinek 		if (invoke_brand_handler(CMD_UNINSTALL, argv) != Z_OK)
49240Sstevel@tonic-gate 			return (Z_ERR);
49257089Sgjelinek 
49267089Sgjelinek 		if (!force) {
49277089Sgjelinek 			(void) snprintf(line, sizeof (line),
49287089Sgjelinek 			    gettext("Are you sure you want to %s zone %s"),
49297089Sgjelinek 			    cmd_to_str(CMD_UNINSTALL), target_zone);
49307089Sgjelinek 			if ((answer = ask_yesno(B_FALSE, line)) == 0) {
49317089Sgjelinek 				return (Z_OK);
49327089Sgjelinek 			} else if (answer == -1) {
49337089Sgjelinek 				zerror(gettext("Input not from terminal and -F "
49347089Sgjelinek 				    "not specified: %s not done."),
49357089Sgjelinek 				    cmd_to_str(CMD_UNINSTALL));
49367089Sgjelinek 				return (Z_ERR);
49377089Sgjelinek 			}
49380Sstevel@tonic-gate 		}
49390Sstevel@tonic-gate 	}
49400Sstevel@tonic-gate 
49411867Sgjelinek 	if ((err = zone_get_zonepath(target_zone, zonepath,
49421867Sgjelinek 	    sizeof (zonepath))) != Z_OK) {
49430Sstevel@tonic-gate 		errno = err;
49440Sstevel@tonic-gate 		zperror2(target_zone, gettext("could not get zone path"));
49450Sstevel@tonic-gate 		return (Z_ERR);
49460Sstevel@tonic-gate 	}
49470Sstevel@tonic-gate 
49480Sstevel@tonic-gate 	/*
49497089Sgjelinek 	 * Fetch the uninstall and preuninstall hooks from the brand
49507089Sgjelinek 	 * configuration.
49510Sstevel@tonic-gate 	 */
49524785Sgjelinek 	if ((bh = brand_open(target_brand)) == NULL) {
49534785Sgjelinek 		zerror(gettext("missing or invalid brand"));
49544785Sgjelinek 		return (Z_ERR);
49554785Sgjelinek 	}
49564785Sgjelinek 
49577089Sgjelinek 	if (get_hook(bh, cmdbuf, sizeof (cmdbuf), brand_get_uninstall,
49587089Sgjelinek 	    target_zone, zonepath) != Z_OK) {
49597089Sgjelinek 		zerror("invalid brand configuration: missing uninstall "
49607089Sgjelinek 		    "resource");
49617089Sgjelinek 		brand_close(bh);
49627089Sgjelinek 		return (Z_ERR);
49637089Sgjelinek 	}
49647089Sgjelinek 
49657089Sgjelinek 	if (get_hook(bh, precmdbuf, sizeof (precmdbuf), brand_get_preuninstall,
49667089Sgjelinek 	    target_zone, zonepath) != Z_OK) {
49674785Sgjelinek 		zerror("invalid brand configuration: missing preuninstall "
49684785Sgjelinek 		    "resource");
49694785Sgjelinek 		brand_close(bh);
49704785Sgjelinek 		return (Z_ERR);
49714785Sgjelinek 	}
49724785Sgjelinek 	brand_close(bh);
49734785Sgjelinek 
49747089Sgjelinek 	/* Append all options to preuninstall hook. */
49757089Sgjelinek 	if (addoptions(precmdbuf, argv, sizeof (precmdbuf)) != Z_OK)
49767089Sgjelinek 		return (Z_ERR);
49777089Sgjelinek 
49787089Sgjelinek 	/* Append all options to uninstall hook. */
49797089Sgjelinek 	if (addoptions(cmdbuf, argv, sizeof (cmdbuf)) != Z_OK)
49807089Sgjelinek 		return (Z_ERR);
49817089Sgjelinek 
49827089Sgjelinek 	if (!brand_help) {
49837089Sgjelinek 		if ((err = zone_get_rootpath(target_zone, rootpath,
49847089Sgjelinek 		    sizeof (rootpath))) != Z_OK) {
49857089Sgjelinek 			errno = err;
49867089Sgjelinek 			zperror2(target_zone, gettext("could not get root "
49877089Sgjelinek 			    "path"));
49884785Sgjelinek 			return (Z_ERR);
49894785Sgjelinek 		}
49904785Sgjelinek 
49917089Sgjelinek 		/*
49927089Sgjelinek 		 * If there seems to be a zoneadmd running for this zone, call
49937089Sgjelinek 		 * it to tell it that an uninstall is happening; if all goes
49947089Sgjelinek 		 * well it will then shut itself down.
49957089Sgjelinek 		 */
49967089Sgjelinek 		if (zonecfg_ping_zoneadmd(target_zone) == Z_OK) {
49977089Sgjelinek 			zone_cmd_arg_t zarg;
49987089Sgjelinek 			zarg.cmd = Z_NOTE_UNINSTALLING;
49997089Sgjelinek 			/* we don't care too much if this fails, just plow on */
50007089Sgjelinek 			(void) zonecfg_call_zoneadmd(target_zone, &zarg, locale,
50017089Sgjelinek 			    B_TRUE);
50027089Sgjelinek 		}
50037089Sgjelinek 
50047089Sgjelinek 		if (zonecfg_grab_lock_file(target_zone, &lockfd) != Z_OK) {
50057089Sgjelinek 			zerror(gettext("another %s may have an operation in "
50067089Sgjelinek 			    "progress."), "zoneadm");
50077089Sgjelinek 			return (Z_ERR);
50087089Sgjelinek 		}
50097089Sgjelinek 
50107089Sgjelinek 		/* Don't uninstall the zone if anything is mounted there */
50117089Sgjelinek 		err = zonecfg_find_mounts(rootpath, NULL, NULL);
50127089Sgjelinek 		if (err) {
50137089Sgjelinek 			zerror(gettext("These file systems are mounted on "
50147089Sgjelinek 			    "subdirectories of %s.\n"), rootpath);
50157089Sgjelinek 			(void) zonecfg_find_mounts(rootpath, zfm_print, NULL);
50167089Sgjelinek 			zonecfg_release_lock_file(target_zone, lockfd);
50177089Sgjelinek 			return (Z_ERR);
50187089Sgjelinek 		}
50197089Sgjelinek 	}
50207089Sgjelinek 
50217089Sgjelinek 	/* If we have a brand preuninstall hook, run it. */
50227089Sgjelinek 	if (!brand_help && precmdbuf[0] != '\0') {
50234785Sgjelinek 		status = do_subproc(cmdbuf);
50244785Sgjelinek 		if (subproc_status(gettext("brand-specific preuninstall"),
50254785Sgjelinek 		    status, B_FALSE) != ZONE_SUBPROC_OK) {
50267089Sgjelinek 			zonecfg_release_lock_file(target_zone, lockfd);
50274785Sgjelinek 			return (Z_ERR);
50284785Sgjelinek 		}
50294785Sgjelinek 	}
50304785Sgjelinek 
50317089Sgjelinek 	if (!brand_help) {
50327089Sgjelinek 		err = zone_set_state(target_zone, ZONE_STATE_INCOMPLETE);
50337089Sgjelinek 		if (err != Z_OK) {
50347089Sgjelinek 			errno = err;
50357089Sgjelinek 			zperror2(target_zone, gettext("could not set state"));
50367089Sgjelinek 			goto bad;
50377089Sgjelinek 		}
50387089Sgjelinek 	}
50397089Sgjelinek 
50407089Sgjelinek 	/*
50417089Sgjelinek 	 * If there is a brand uninstall hook, use it, otherwise use the
50427089Sgjelinek 	 * built-in uninstall code.
50437089Sgjelinek 	 */
50447089Sgjelinek 	if (cmdbuf[0] != '\0') {
50457089Sgjelinek 		/* Run the uninstall hook */
50467089Sgjelinek 		status = do_subproc_interactive(cmdbuf);
50477089Sgjelinek 		if ((status = subproc_status(gettext("brand-specific "
50487089Sgjelinek 		    "uninstall"), status, B_FALSE)) != ZONE_SUBPROC_OK) {
50497089Sgjelinek 			if (status == ZONE_SUBPROC_USAGE && !brand_help)
50507089Sgjelinek 				sub_usage(SHELP_UNINSTALL, CMD_UNINSTALL);
50517089Sgjelinek 			if (!brand_help)
50527089Sgjelinek 				zonecfg_release_lock_file(target_zone, lockfd);
50537089Sgjelinek 			return (Z_ERR);
50547089Sgjelinek 		}
50557089Sgjelinek 
50567089Sgjelinek 		if (brand_help)
50577089Sgjelinek 			return (Z_OK);
50587089Sgjelinek 	} else {
50597089Sgjelinek 		/* If just help, we're done since there is no brand help. */
50607089Sgjelinek 		if (brand_help)
50617089Sgjelinek 			return (Z_OK);
50627089Sgjelinek 
50637089Sgjelinek 		/* Run the built-in uninstall support. */
50647089Sgjelinek 		if ((err = cleanup_zonepath(zonepath, B_FALSE)) != Z_OK) {
50657089Sgjelinek 			errno = err;
50667089Sgjelinek 			zperror2(target_zone, gettext("cleaning up zonepath "
50677089Sgjelinek 			    "failed"));
50687089Sgjelinek 			goto bad;
50697089Sgjelinek 		}
50701867Sgjelinek 	}
50711867Sgjelinek 
50720Sstevel@tonic-gate 	err = zone_set_state(target_zone, ZONE_STATE_CONFIGURED);
50730Sstevel@tonic-gate 	if (err != Z_OK) {
50740Sstevel@tonic-gate 		errno = err;
50750Sstevel@tonic-gate 		zperror2(target_zone, gettext("could not reset state"));
50760Sstevel@tonic-gate 	}
50770Sstevel@tonic-gate bad:
50787089Sgjelinek 	zonecfg_release_lock_file(target_zone, lockfd);
50790Sstevel@tonic-gate 	return (err);
50800Sstevel@tonic-gate }
50810Sstevel@tonic-gate 
5082766Scarlsonj /* ARGSUSED */
5083766Scarlsonj static int
5084766Scarlsonj mount_func(int argc, char *argv[])
5085766Scarlsonj {
5086766Scarlsonj 	zone_cmd_arg_t zarg;
50872712Snn35248 	boolean_t force = B_FALSE;
50882712Snn35248 	int arg;
50892712Snn35248 
50902712Snn35248 	/*
50912712Snn35248 	 * The only supported subargument to the "mount" subcommand is
50922712Snn35248 	 * "-f", which forces us to mount a zone in the INCOMPLETE state.
50932712Snn35248 	 */
50942712Snn35248 	optind = 0;
50952712Snn35248 	if ((arg = getopt(argc, argv, "f")) != EOF) {
50962712Snn35248 		switch (arg) {
50972712Snn35248 		case 'f':
50982712Snn35248 			force = B_TRUE;
50992712Snn35248 			break;
51002712Snn35248 		default:
51012712Snn35248 			return (Z_USAGE);
51022712Snn35248 		}
51032712Snn35248 	}
51042712Snn35248 	if (argc > optind)
5105766Scarlsonj 		return (Z_USAGE);
51062712Snn35248 
51072712Snn35248 	if (sanity_check(target_zone, CMD_MOUNT, B_FALSE, B_FALSE, force)
51082712Snn35248 	    != Z_OK)
5109766Scarlsonj 		return (Z_ERR);
51103339Szt129084 	if (verify_details(CMD_MOUNT, argv) != Z_OK)
5111766Scarlsonj 		return (Z_ERR);
5112766Scarlsonj 
51132712Snn35248 	zarg.cmd = force ? Z_FORCEMOUNT : Z_MOUNT;
51145829Sgjelinek 	zarg.bootbuf[0] = '\0';
51157089Sgjelinek 	if (zonecfg_call_zoneadmd(target_zone, &zarg, locale, B_TRUE) != 0) {
5116766Scarlsonj 		zerror(gettext("call to %s failed"), "zoneadmd");
5117766Scarlsonj 		return (Z_ERR);
5118766Scarlsonj 	}
5119766Scarlsonj 	return (Z_OK);
5120766Scarlsonj }
5121766Scarlsonj 
5122766Scarlsonj /* ARGSUSED */
5123766Scarlsonj static int
5124766Scarlsonj unmount_func(int argc, char *argv[])
5125766Scarlsonj {
5126766Scarlsonj 	zone_cmd_arg_t zarg;
5127766Scarlsonj 
5128766Scarlsonj 	if (argc > 0)
5129766Scarlsonj 		return (Z_USAGE);
51302712Snn35248 	if (sanity_check(target_zone, CMD_UNMOUNT, B_FALSE, B_FALSE, B_FALSE)
51312712Snn35248 	    != Z_OK)
5132766Scarlsonj 		return (Z_ERR);
5133766Scarlsonj 
5134766Scarlsonj 	zarg.cmd = Z_UNMOUNT;
51357089Sgjelinek 	if (zonecfg_call_zoneadmd(target_zone, &zarg, locale, B_TRUE) != 0) {
5136766Scarlsonj 		zerror(gettext("call to %s failed"), "zoneadmd");
5137766Scarlsonj 		return (Z_ERR);
5138766Scarlsonj 	}
5139766Scarlsonj 	return (Z_OK);
5140766Scarlsonj }
5141766Scarlsonj 
51420Sstevel@tonic-gate static int
51432303Scarlsonj mark_func(int argc, char *argv[])
51442303Scarlsonj {
51452303Scarlsonj 	int err, lockfd;
51468759Sgerald.jelinek@sun.com 	int arg;
51478759Sgerald.jelinek@sun.com 	boolean_t force = B_FALSE;
51488759Sgerald.jelinek@sun.com 	int state;
51498759Sgerald.jelinek@sun.com 
51508759Sgerald.jelinek@sun.com 	optind = 0;
51518759Sgerald.jelinek@sun.com 	opterr = 0;
51528759Sgerald.jelinek@sun.com 	while ((arg = getopt(argc, argv, "F")) != EOF) {
51538759Sgerald.jelinek@sun.com 		switch (arg) {
51548759Sgerald.jelinek@sun.com 		case 'F':
51558759Sgerald.jelinek@sun.com 			force = B_TRUE;
51568759Sgerald.jelinek@sun.com 			break;
51578759Sgerald.jelinek@sun.com 		default:
51588759Sgerald.jelinek@sun.com 			return (Z_USAGE);
51598759Sgerald.jelinek@sun.com 		}
51608759Sgerald.jelinek@sun.com 	}
51618759Sgerald.jelinek@sun.com 
51628759Sgerald.jelinek@sun.com 	if (argc != (optind + 1))
51632303Scarlsonj 		return (Z_USAGE);
51648759Sgerald.jelinek@sun.com 
51658759Sgerald.jelinek@sun.com 	if (strcmp(argv[optind], "configured") == 0)
51668759Sgerald.jelinek@sun.com 		state = ZONE_STATE_CONFIGURED;
51678759Sgerald.jelinek@sun.com 	else if (strcmp(argv[optind], "incomplete") == 0)
51688759Sgerald.jelinek@sun.com 		state = ZONE_STATE_INCOMPLETE;
51698759Sgerald.jelinek@sun.com 	else if (strcmp(argv[optind], "installed") == 0)
51708759Sgerald.jelinek@sun.com 		state = ZONE_STATE_INSTALLED;
51718759Sgerald.jelinek@sun.com 	else
51728759Sgerald.jelinek@sun.com 		return (Z_USAGE);
51738759Sgerald.jelinek@sun.com 
51748759Sgerald.jelinek@sun.com 	if (state != ZONE_STATE_INCOMPLETE && !force)
51758759Sgerald.jelinek@sun.com 		return (Z_USAGE);
51768759Sgerald.jelinek@sun.com 
51778759Sgerald.jelinek@sun.com 	if (sanity_check(target_zone, CMD_MARK, B_FALSE, B_TRUE, B_FALSE)
51782712Snn35248 	    != Z_OK)
51792303Scarlsonj 		return (Z_ERR);
51802303Scarlsonj 
51813339Szt129084 	/*
51823339Szt129084 	 * Invoke brand-specific handler.
51833339Szt129084 	 */
51843339Szt129084 	if (invoke_brand_handler(CMD_MARK, argv) != Z_OK)
51853339Szt129084 		return (Z_ERR);
51863339Szt129084 
51877089Sgjelinek 	if (zonecfg_grab_lock_file(target_zone, &lockfd) != Z_OK) {
51882303Scarlsonj 		zerror(gettext("another %s may have an operation in progress."),
51892303Scarlsonj 		    "zoneadm");
51902303Scarlsonj 		return (Z_ERR);
51912303Scarlsonj 	}
51922303Scarlsonj 
51938759Sgerald.jelinek@sun.com 	err = zone_set_state(target_zone, state);
51942303Scarlsonj 	if (err != Z_OK) {
51952303Scarlsonj 		errno = err;
51962303Scarlsonj 		zperror2(target_zone, gettext("could not set state"));
51972303Scarlsonj 	}
51987089Sgjelinek 	zonecfg_release_lock_file(target_zone, lockfd);
51992303Scarlsonj 
52002303Scarlsonj 	return (err);
52012303Scarlsonj }
52022303Scarlsonj 
52033247Sgjelinek /*
52043247Sgjelinek  * Check what scheduling class we're running under and print a warning if
52053247Sgjelinek  * we're not using FSS.
52063247Sgjelinek  */
52073247Sgjelinek static int
52083247Sgjelinek check_sched_fss(zone_dochandle_t handle)
52093247Sgjelinek {
52103247Sgjelinek 	char class_name[PC_CLNMSZ];
52113247Sgjelinek 
52123247Sgjelinek 	if (zonecfg_get_dflt_sched_class(handle, class_name,
52133247Sgjelinek 	    sizeof (class_name)) != Z_OK) {
52143247Sgjelinek 		zerror(gettext("WARNING: unable to determine the zone's "
52153247Sgjelinek 		    "scheduling class"));
52163247Sgjelinek 	} else if (strcmp("FSS", class_name) != 0) {
52173247Sgjelinek 		zerror(gettext("WARNING: The zone.cpu-shares rctl is set but\n"
52183247Sgjelinek 		    "FSS is not the default scheduling class for this zone.  "
52193247Sgjelinek 		    "FSS will be\nused for processes in the zone but to get "
52203247Sgjelinek 		    "the full benefit of FSS,\nit should be the default "
52213247Sgjelinek 		    "scheduling class.  See dispadmin(1M) for\nmore details."));
52223247Sgjelinek 		return (Z_SYSTEM);
52233247Sgjelinek 	}
52243247Sgjelinek 
52253247Sgjelinek 	return (Z_OK);
52263247Sgjelinek }
52273247Sgjelinek 
52283247Sgjelinek static int
52293247Sgjelinek check_cpu_shares_sched(zone_dochandle_t handle)
52303247Sgjelinek {
52313247Sgjelinek 	int err;
52323247Sgjelinek 	int res = Z_OK;
52333247Sgjelinek 	struct zone_rctltab rctl;
52343247Sgjelinek 
52353247Sgjelinek 	if ((err = zonecfg_setrctlent(handle)) != Z_OK) {
52363247Sgjelinek 		errno = err;
52373247Sgjelinek 		zperror(cmd_to_str(CMD_APPLY), B_TRUE);
52383247Sgjelinek 		return (err);
52393247Sgjelinek 	}
52403247Sgjelinek 
52413247Sgjelinek 	while (zonecfg_getrctlent(handle, &rctl) == Z_OK) {
52423247Sgjelinek 		if (strcmp(rctl.zone_rctl_name, "zone.cpu-shares") == 0) {
52433247Sgjelinek 			if (check_sched_fss(handle) != Z_OK)
52443247Sgjelinek 				res = Z_SYSTEM;
52453247Sgjelinek 			break;
52463247Sgjelinek 		}
52473247Sgjelinek 	}
52483247Sgjelinek 
52493247Sgjelinek 	(void) zonecfg_endrctlent(handle);
52503247Sgjelinek 
52513247Sgjelinek 	return (res);
52523247Sgjelinek }
52533247Sgjelinek 
52543247Sgjelinek /*
52553352Sgjelinek  * Check if there is a mix of processes running in different pools within the
52563352Sgjelinek  * zone.  This is currently only going to be called for the global zone from
52573352Sgjelinek  * apply_func but that could be generalized in the future.
52583352Sgjelinek  */
52593352Sgjelinek static boolean_t
52603352Sgjelinek mixed_pools(zoneid_t zoneid)
52613352Sgjelinek {
52623352Sgjelinek 	DIR *dirp;
52633352Sgjelinek 	dirent_t *dent;
52643352Sgjelinek 	boolean_t mixed = B_FALSE;
52653352Sgjelinek 	boolean_t poolid_set = B_FALSE;
52663352Sgjelinek 	poolid_t last_poolid = 0;
52673352Sgjelinek 
52683352Sgjelinek 	if ((dirp = opendir("/proc")) == NULL) {
52693352Sgjelinek 		zerror(gettext("could not open /proc"));
52703352Sgjelinek 		return (B_FALSE);
52713352Sgjelinek 	}
52723352Sgjelinek 
52733352Sgjelinek 	while ((dent = readdir(dirp)) != NULL) {
52743352Sgjelinek 		int procfd;
52753352Sgjelinek 		psinfo_t ps;
52763352Sgjelinek 		char procpath[MAXPATHLEN];
52773352Sgjelinek 
52783352Sgjelinek 		if (dent->d_name[0] == '.')
52793352Sgjelinek 			continue;
52803352Sgjelinek 
52813352Sgjelinek 		(void) snprintf(procpath, sizeof (procpath), "/proc/%s/psinfo",
52823352Sgjelinek 		    dent->d_name);
52833352Sgjelinek 
52843352Sgjelinek 		if ((procfd = open(procpath, O_RDONLY)) == -1)
52853352Sgjelinek 			continue;
52863352Sgjelinek 
52873352Sgjelinek 		if (read(procfd, &ps, sizeof (ps)) == sizeof (psinfo_t)) {
52883352Sgjelinek 			/* skip processes in other zones and system processes */
52893352Sgjelinek 			if (zoneid != ps.pr_zoneid || ps.pr_flag & SSYS) {
52903352Sgjelinek 				(void) close(procfd);
52913352Sgjelinek 				continue;
52923352Sgjelinek 			}
52933352Sgjelinek 
52943352Sgjelinek 			if (poolid_set) {
52953352Sgjelinek 				if (ps.pr_poolid != last_poolid)
52963352Sgjelinek 					mixed = B_TRUE;
52973352Sgjelinek 			} else {
52983352Sgjelinek 				last_poolid = ps.pr_poolid;
52993352Sgjelinek 				poolid_set = B_TRUE;
53003352Sgjelinek 			}
53013352Sgjelinek 		}
53023352Sgjelinek 
53033352Sgjelinek 		(void) close(procfd);
53043352Sgjelinek 
53053352Sgjelinek 		if (mixed)
53063352Sgjelinek 			break;
53073352Sgjelinek 	}
53083352Sgjelinek 
53093352Sgjelinek 	(void) closedir(dirp);
53103352Sgjelinek 
53113352Sgjelinek 	return (mixed);
53123352Sgjelinek }
53133352Sgjelinek 
53143352Sgjelinek /*
53153352Sgjelinek  * Check if a persistent or temporary pool is configured for the zone.
53163352Sgjelinek  * This is currently only going to be called for the global zone from
53173352Sgjelinek  * apply_func but that could be generalized in the future.
53183352Sgjelinek  */
53193352Sgjelinek static boolean_t
53203352Sgjelinek pool_configured(zone_dochandle_t handle)
53213352Sgjelinek {
53223352Sgjelinek 	int err1, err2;
53233352Sgjelinek 	struct zone_psettab pset_tab;
53243352Sgjelinek 	char poolname[MAXPATHLEN];
53253352Sgjelinek 
53263352Sgjelinek 	err1 = zonecfg_lookup_pset(handle, &pset_tab);
53273352Sgjelinek 	err2 = zonecfg_get_pool(handle, poolname, sizeof (poolname));
53283352Sgjelinek 
53293352Sgjelinek 	if (err1 == Z_NO_ENTRY &&
53303352Sgjelinek 	    (err2 == Z_NO_ENTRY || (err2 == Z_OK && strlen(poolname) == 0)))
53313352Sgjelinek 		return (B_FALSE);
53323352Sgjelinek 
53333352Sgjelinek 	return (B_TRUE);
53343352Sgjelinek }
53353352Sgjelinek 
53363352Sgjelinek /*
53373247Sgjelinek  * This is an undocumented interface which is currently only used to apply
53383247Sgjelinek  * the global zone resource management settings when the system boots.
53393247Sgjelinek  * This function does not yet properly handle updating a running system so
53403247Sgjelinek  * any projects running in the zone would be trashed if this function
53413247Sgjelinek  * were to run after the zone had booted.  It also does not reset any
53423247Sgjelinek  * rctl settings that were removed from zonecfg.  There is still work to be
53433247Sgjelinek  * done before we can properly support dynamically updating the resource
53443247Sgjelinek  * management settings for a running zone (global or non-global).  Thus, this
53453247Sgjelinek  * functionality is undocumented for now.
53463247Sgjelinek  */
53473247Sgjelinek /* ARGSUSED */
53483247Sgjelinek static int
53493247Sgjelinek apply_func(int argc, char *argv[])
53503247Sgjelinek {
53513247Sgjelinek 	int err;
53523247Sgjelinek 	int res = Z_OK;
53533247Sgjelinek 	priv_set_t *privset;
53543247Sgjelinek 	zoneid_t zoneid;
53553247Sgjelinek 	zone_dochandle_t handle;
53563247Sgjelinek 	struct zone_mcaptab mcap;
53573247Sgjelinek 	char pool_err[128];
53583247Sgjelinek 
53593247Sgjelinek 	zoneid = getzoneid();
53603247Sgjelinek 
53613247Sgjelinek 	if (zonecfg_in_alt_root() || zoneid != GLOBAL_ZONEID ||
53623247Sgjelinek 	    target_zone == NULL || strcmp(target_zone, GLOBAL_ZONENAME) != 0)
53633247Sgjelinek 		return (usage(B_FALSE));
53643247Sgjelinek 
53653247Sgjelinek 	if ((privset = priv_allocset()) == NULL) {
53663247Sgjelinek 		zerror(gettext("%s failed"), "priv_allocset");
53673247Sgjelinek 		return (Z_ERR);
53683247Sgjelinek 	}
53693247Sgjelinek 
53703247Sgjelinek 	if (getppriv(PRIV_EFFECTIVE, privset) != 0) {
53713247Sgjelinek 		zerror(gettext("%s failed"), "getppriv");
53723247Sgjelinek 		priv_freeset(privset);
53733247Sgjelinek 		return (Z_ERR);
53743247Sgjelinek 	}
53753247Sgjelinek 
53763247Sgjelinek 	if (priv_isfullset(privset) == B_FALSE) {
53773247Sgjelinek 		(void) usage(B_FALSE);
53783247Sgjelinek 		priv_freeset(privset);
53793247Sgjelinek 		return (Z_ERR);
53803247Sgjelinek 	}
53813247Sgjelinek 	priv_freeset(privset);
53823247Sgjelinek 
53833247Sgjelinek 	if ((handle = zonecfg_init_handle()) == NULL) {
53843247Sgjelinek 		zperror(cmd_to_str(CMD_APPLY), B_TRUE);
53853247Sgjelinek 		return (Z_ERR);
53863247Sgjelinek 	}
53873247Sgjelinek 
53883247Sgjelinek 	if ((err = zonecfg_get_handle(target_zone, handle)) != Z_OK) {
53893247Sgjelinek 		errno = err;
53903247Sgjelinek 		zperror(cmd_to_str(CMD_APPLY), B_TRUE);
53913247Sgjelinek 		zonecfg_fini_handle(handle);
53923247Sgjelinek 		return (Z_ERR);
53933247Sgjelinek 	}
53943247Sgjelinek 
53953247Sgjelinek 	/* specific error msgs are printed within apply_rctls */
53963247Sgjelinek 	if ((err = zonecfg_apply_rctls(target_zone, handle)) != Z_OK) {
53973247Sgjelinek 		errno = err;
53983247Sgjelinek 		zperror(cmd_to_str(CMD_APPLY), B_TRUE);
53993247Sgjelinek 		res = Z_ERR;
54003247Sgjelinek 	}
54013247Sgjelinek 
54023247Sgjelinek 	if ((err = check_cpu_shares_sched(handle)) != Z_OK)
54033247Sgjelinek 		res = Z_ERR;
54043247Sgjelinek 
54053352Sgjelinek 	if (pool_configured(handle)) {
54063352Sgjelinek 		if (mixed_pools(zoneid)) {
54073352Sgjelinek 			zerror(gettext("Zone is using multiple resource "
54083352Sgjelinek 			    "pools.  The pool\nconfiguration cannot be "
54093352Sgjelinek 			    "applied without rebooting."));
54103352Sgjelinek 			res = Z_ERR;
54113352Sgjelinek 		} else {
54123352Sgjelinek 
54133352Sgjelinek 			/*
54143352Sgjelinek 			 * The next two blocks of code attempt to set up
54153352Sgjelinek 			 * temporary pools as well as persistent pools.  In
54163352Sgjelinek 			 * both cases we call the functions unconditionally.
54173352Sgjelinek 			 * Within each funtion the code will check if the zone
54183352Sgjelinek 			 * is actually configured for a temporary pool or
54193352Sgjelinek 			 * persistent pool and just return if there is nothing
54203352Sgjelinek 			 * to do.
54213352Sgjelinek 			 */
54223352Sgjelinek 			if ((err = zonecfg_bind_tmp_pool(handle, zoneid,
54233352Sgjelinek 			    pool_err, sizeof (pool_err))) != Z_OK) {
54243352Sgjelinek 				if (err == Z_POOL || err == Z_POOL_CREATE ||
54253352Sgjelinek 				    err == Z_POOL_BIND)
54263352Sgjelinek 					zerror("%s: %s", zonecfg_strerror(err),
54273352Sgjelinek 					    pool_err);
54283352Sgjelinek 				else
54293352Sgjelinek 					zerror(gettext("could not bind zone to "
54303352Sgjelinek 					    "temporary pool: %s"),
54313352Sgjelinek 					    zonecfg_strerror(err));
54323352Sgjelinek 				res = Z_ERR;
54333352Sgjelinek 			}
54343352Sgjelinek 
54353352Sgjelinek 			if ((err = zonecfg_bind_pool(handle, zoneid, pool_err,
54363352Sgjelinek 			    sizeof (pool_err))) != Z_OK) {
54373352Sgjelinek 				if (err == Z_POOL || err == Z_POOL_BIND)
54383352Sgjelinek 					zerror("%s: %s", zonecfg_strerror(err),
54393352Sgjelinek 					    pool_err);
54403352Sgjelinek 				else
54413352Sgjelinek 					zerror("%s", zonecfg_strerror(err));
54423352Sgjelinek 			}
54433352Sgjelinek 		}
54443247Sgjelinek 	}
54453247Sgjelinek 
54463247Sgjelinek 	/*
54473247Sgjelinek 	 * If a memory cap is configured, set the cap in the kernel using
54483247Sgjelinek 	 * zone_setattr() and make sure the rcapd SMF service is enabled.
54493247Sgjelinek 	 */
54503247Sgjelinek 	if (zonecfg_getmcapent(handle, &mcap) == Z_OK) {
54513247Sgjelinek 		uint64_t num;
54523247Sgjelinek 		char smf_err[128];
54533247Sgjelinek 
54543247Sgjelinek 		num = (uint64_t)strtoll(mcap.zone_physmem_cap, NULL, 10);
54553247Sgjelinek 		if (zone_setattr(zoneid, ZONE_ATTR_PHYS_MCAP, &num, 0) == -1) {
54563247Sgjelinek 			zerror(gettext("could not set zone memory cap"));
54573247Sgjelinek 			res = Z_ERR;
54583247Sgjelinek 		}
54593247Sgjelinek 
54603247Sgjelinek 		if (zonecfg_enable_rcapd(smf_err, sizeof (smf_err)) != Z_OK) {
54613247Sgjelinek 			zerror(gettext("enabling system/rcap service failed: "
54623247Sgjelinek 			    "%s"), smf_err);
54633247Sgjelinek 			res = Z_ERR;
54643247Sgjelinek 		}
54653247Sgjelinek 	}
54663247Sgjelinek 
54673247Sgjelinek 	zonecfg_fini_handle(handle);
54683247Sgjelinek 
54693247Sgjelinek 	return (res);
54703247Sgjelinek }
54713247Sgjelinek 
54722303Scarlsonj static int
54730Sstevel@tonic-gate help_func(int argc, char *argv[])
54740Sstevel@tonic-gate {
54750Sstevel@tonic-gate 	int arg, cmd_num;
54760Sstevel@tonic-gate 
54770Sstevel@tonic-gate 	if (argc == 0) {
54780Sstevel@tonic-gate 		(void) usage(B_TRUE);
54790Sstevel@tonic-gate 		return (Z_OK);
54800Sstevel@tonic-gate 	}
54810Sstevel@tonic-gate 	optind = 0;
54820Sstevel@tonic-gate 	if ((arg = getopt(argc, argv, "?")) != EOF) {
54830Sstevel@tonic-gate 		switch (arg) {
54840Sstevel@tonic-gate 		case '?':
54850Sstevel@tonic-gate 			sub_usage(SHELP_HELP, CMD_HELP);
54860Sstevel@tonic-gate 			return (optopt == '?' ? Z_OK : Z_USAGE);
54870Sstevel@tonic-gate 		default:
54880Sstevel@tonic-gate 			sub_usage(SHELP_HELP, CMD_HELP);
54890Sstevel@tonic-gate 			return (Z_USAGE);
54900Sstevel@tonic-gate 		}
54910Sstevel@tonic-gate 	}
54920Sstevel@tonic-gate 	while (optind < argc) {
5493988Scarlsonj 		/* Private commands have NULL short_usage; omit them */
5494988Scarlsonj 		if ((cmd_num = cmd_match(argv[optind])) < 0 ||
5495988Scarlsonj 		    cmdtab[cmd_num].short_usage == NULL) {
54960Sstevel@tonic-gate 			sub_usage(SHELP_HELP, CMD_HELP);
54970Sstevel@tonic-gate 			return (Z_USAGE);
54980Sstevel@tonic-gate 		}
54990Sstevel@tonic-gate 		sub_usage(cmdtab[cmd_num].short_usage, cmd_num);
55000Sstevel@tonic-gate 		optind++;
55010Sstevel@tonic-gate 	}
55020Sstevel@tonic-gate 	return (Z_OK);
55030Sstevel@tonic-gate }
55040Sstevel@tonic-gate 
55050Sstevel@tonic-gate /*
55060Sstevel@tonic-gate  * Returns: CMD_MIN thru CMD_MAX on success, -1 on error
55070Sstevel@tonic-gate  */
55080Sstevel@tonic-gate 
55090Sstevel@tonic-gate static int
55100Sstevel@tonic-gate cmd_match(char *cmd)
55110Sstevel@tonic-gate {
55120Sstevel@tonic-gate 	int i;
55130Sstevel@tonic-gate 
55140Sstevel@tonic-gate 	for (i = CMD_MIN; i <= CMD_MAX; i++) {
55150Sstevel@tonic-gate 		/* return only if there is an exact match */
55160Sstevel@tonic-gate 		if (strcmp(cmd, cmdtab[i].cmd_name) == 0)
55170Sstevel@tonic-gate 			return (cmdtab[i].cmd_num);
55180Sstevel@tonic-gate 	}
55190Sstevel@tonic-gate 	return (-1);
55200Sstevel@tonic-gate }
55210Sstevel@tonic-gate 
55220Sstevel@tonic-gate static int
55230Sstevel@tonic-gate parse_and_run(int argc, char *argv[])
55240Sstevel@tonic-gate {
55250Sstevel@tonic-gate 	int i = cmd_match(argv[0]);
55260Sstevel@tonic-gate 
55270Sstevel@tonic-gate 	if (i < 0)
55280Sstevel@tonic-gate 		return (usage(B_FALSE));
55290Sstevel@tonic-gate 	return (cmdtab[i].handler(argc - 1, &(argv[1])));
55300Sstevel@tonic-gate }
55310Sstevel@tonic-gate 
55320Sstevel@tonic-gate static char *
55330Sstevel@tonic-gate get_execbasename(char *execfullname)
55340Sstevel@tonic-gate {
55350Sstevel@tonic-gate 	char *last_slash, *execbasename;
55360Sstevel@tonic-gate 
55370Sstevel@tonic-gate 	/* guard against '/' at end of command invocation */
55380Sstevel@tonic-gate 	for (;;) {
55390Sstevel@tonic-gate 		last_slash = strrchr(execfullname, '/');
55400Sstevel@tonic-gate 		if (last_slash == NULL) {
55410Sstevel@tonic-gate 			execbasename = execfullname;
55420Sstevel@tonic-gate 			break;
55430Sstevel@tonic-gate 		} else {
55440Sstevel@tonic-gate 			execbasename = last_slash + 1;
55450Sstevel@tonic-gate 			if (*execbasename == '\0') {
55460Sstevel@tonic-gate 				*last_slash = '\0';
55470Sstevel@tonic-gate 				continue;
55480Sstevel@tonic-gate 			}
55490Sstevel@tonic-gate 			break;
55500Sstevel@tonic-gate 		}
55510Sstevel@tonic-gate 	}
55520Sstevel@tonic-gate 	return (execbasename);
55530Sstevel@tonic-gate }
55540Sstevel@tonic-gate 
55550Sstevel@tonic-gate int
55560Sstevel@tonic-gate main(int argc, char **argv)
55570Sstevel@tonic-gate {
55580Sstevel@tonic-gate 	int arg;
55590Sstevel@tonic-gate 	zoneid_t zid;
5560766Scarlsonj 	struct stat st;
55612712Snn35248 	char *zone_lock_env;
55622712Snn35248 	int err;
55630Sstevel@tonic-gate 
55640Sstevel@tonic-gate 	if ((locale = setlocale(LC_ALL, "")) == NULL)
55650Sstevel@tonic-gate 		locale = "C";
55660Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
55670Sstevel@tonic-gate 	setbuf(stdout, NULL);
55680Sstevel@tonic-gate 	(void) sigset(SIGHUP, SIG_IGN);
55690Sstevel@tonic-gate 	execname = get_execbasename(argv[0]);
55700Sstevel@tonic-gate 	target_zone = NULL;
55710Sstevel@tonic-gate 	if (chdir("/") != 0) {
55720Sstevel@tonic-gate 		zerror(gettext("could not change directory to /."));
55730Sstevel@tonic-gate 		exit(Z_ERR);
55740Sstevel@tonic-gate 	}
55759049SSudheer.Abdul-Salam@Sun.COM 	/*
55769049SSudheer.Abdul-Salam@Sun.COM 	 * Use the default system mask rather than anything that may have been
55779049SSudheer.Abdul-Salam@Sun.COM 	 * set by the caller.
55789049SSudheer.Abdul-Salam@Sun.COM 	 */
55799049SSudheer.Abdul-Salam@Sun.COM 	(void) umask(CMASK);
55800Sstevel@tonic-gate 
55812082Seschrock 	if (init_zfs() != Z_OK)
55822082Seschrock 		exit(Z_ERR);
55832082Seschrock 
55842303Scarlsonj 	while ((arg = getopt(argc, argv, "?u:z:R:")) != EOF) {
55850Sstevel@tonic-gate 		switch (arg) {
55860Sstevel@tonic-gate 		case '?':
55870Sstevel@tonic-gate 			return (usage(B_TRUE));
55882303Scarlsonj 		case 'u':
55892303Scarlsonj 			target_uuid = optarg;
55902303Scarlsonj 			break;
55910Sstevel@tonic-gate 		case 'z':
55920Sstevel@tonic-gate 			target_zone = optarg;
55930Sstevel@tonic-gate 			break;
5594766Scarlsonj 		case 'R':	/* private option for admin/install use */
5595766Scarlsonj 			if (*optarg != '/') {
5596766Scarlsonj 				zerror(gettext("root path must be absolute."));
5597766Scarlsonj 				exit(Z_ERR);
5598766Scarlsonj 			}
5599766Scarlsonj 			if (stat(optarg, &st) == -1 || !S_ISDIR(st.st_mode)) {
5600766Scarlsonj 				zerror(
5601766Scarlsonj 				    gettext("root path must be a directory."));
5602766Scarlsonj 				exit(Z_ERR);
5603766Scarlsonj 			}
5604766Scarlsonj 			zonecfg_set_root(optarg);
5605766Scarlsonj 			break;
56060Sstevel@tonic-gate 		default:
56070Sstevel@tonic-gate 			return (usage(B_FALSE));
56080Sstevel@tonic-gate 		}
56090Sstevel@tonic-gate 	}
56100Sstevel@tonic-gate 
56110Sstevel@tonic-gate 	if (optind >= argc)
56120Sstevel@tonic-gate 		return (usage(B_FALSE));
56132303Scarlsonj 
56142303Scarlsonj 	if (target_uuid != NULL && *target_uuid != '\0') {
56152303Scarlsonj 		uuid_t uuid;
56162303Scarlsonj 		static char newtarget[ZONENAME_MAX];
56172303Scarlsonj 
56182303Scarlsonj 		if (uuid_parse(target_uuid, uuid) == -1) {
56192303Scarlsonj 			zerror(gettext("illegal UUID value specified"));
56202303Scarlsonj 			exit(Z_ERR);
56212303Scarlsonj 		}
56222303Scarlsonj 		if (zonecfg_get_name_by_uuid(uuid, newtarget,
56232303Scarlsonj 		    sizeof (newtarget)) == Z_OK)
56242303Scarlsonj 			target_zone = newtarget;
56252303Scarlsonj 	}
56262303Scarlsonj 
56270Sstevel@tonic-gate 	if (target_zone != NULL && zone_get_id(target_zone, &zid) != 0) {
56280Sstevel@tonic-gate 		errno = Z_NO_ZONE;
56290Sstevel@tonic-gate 		zperror(target_zone, B_TRUE);
56300Sstevel@tonic-gate 		exit(Z_ERR);
56310Sstevel@tonic-gate 	}
56322712Snn35248 
56332712Snn35248 	/*
56342712Snn35248 	 * See if we have inherited the right to manipulate this zone from
56352712Snn35248 	 * a zoneadm instance in our ancestry.  If so, set zone_lock_cnt to
56362712Snn35248 	 * indicate it.  If not, make that explicit in our environment.
56372712Snn35248 	 */
56387089Sgjelinek 	zonecfg_init_lock_file(target_zone, &zone_lock_env);
56397089Sgjelinek 	if (zone_lock_env != NULL)
56402712Snn35248 		zoneadm_is_nested = B_TRUE;
56412712Snn35248 
56422712Snn35248 	/*
56432712Snn35248 	 * If we are going to be operating on a single zone, retrieve its
56442712Snn35248 	 * brand type and determine whether it is native or not.
56452712Snn35248 	 */
56462712Snn35248 	if ((target_zone != NULL) &&
56478057SJordan.Vaughan@Sun.com 	    (strcmp(target_zone, GLOBAL_ZONENAME) != 0)) {
56482712Snn35248 		if (zone_get_brand(target_zone, target_brand,
56492712Snn35248 		    sizeof (target_brand)) != Z_OK) {
56502712Snn35248 			zerror(gettext("missing or invalid brand"));
56512712Snn35248 			exit(Z_ERR);
56522712Snn35248 		}
56532712Snn35248 	}
56542712Snn35248 
56552712Snn35248 	err = parse_and_run(argc - optind, &argv[optind]);
56562712Snn35248 
56572712Snn35248 	return (err);
56580Sstevel@tonic-gate }
5659