xref: /onnv-gate/usr/src/cmd/zoneadm/zoneadm.c (revision 8301:f9ac5184ec27)
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 /*
235829Sgjelinek  * Copyright 2008 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;
1052712Snn35248 static boolean_t is_native_zone = B_TRUE;
1060Sstevel@tonic-gate 
1071915Sgjelinek #define	LOOPBACK_IF	"lo0"
1081915Sgjelinek #define	SOCKET_AF(af)	(((af) == AF_UNSPEC) ? AF_INET : (af))
1091915Sgjelinek 
1101915Sgjelinek struct net_if {
1111915Sgjelinek 	char	*name;
1121915Sgjelinek 	int	af;
1131915Sgjelinek };
1141915Sgjelinek 
1150Sstevel@tonic-gate /* 0755 is the default directory mode. */
1160Sstevel@tonic-gate #define	DEFAULT_DIR_MODE \
1170Sstevel@tonic-gate 	(S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH)
1180Sstevel@tonic-gate 
1190Sstevel@tonic-gate struct cmd {
1200Sstevel@tonic-gate 	uint_t	cmd_num;				/* command number */
1210Sstevel@tonic-gate 	char	*cmd_name;				/* command name */
1220Sstevel@tonic-gate 	char	*short_usage;				/* short form help */
1230Sstevel@tonic-gate 	int	(*handler)(int argc, char *argv[]);	/* function to call */
1240Sstevel@tonic-gate 
1250Sstevel@tonic-gate };
1260Sstevel@tonic-gate 
1270Sstevel@tonic-gate #define	SHELP_HELP	"help"
1282267Sdp #define	SHELP_BOOT	"boot [-- boot_arguments]"
1290Sstevel@tonic-gate #define	SHELP_HALT	"halt"
1300Sstevel@tonic-gate #define	SHELP_READY	"ready"
1312267Sdp #define	SHELP_REBOOT	"reboot [-- boot_arguments]"
1320Sstevel@tonic-gate #define	SHELP_LIST	"list [-cipv]"
1330Sstevel@tonic-gate #define	SHELP_VERIFY	"verify"
1342712Snn35248 #define	SHELP_INSTALL	"install [-x nodataset] [brand-specific args]"
1357089Sgjelinek #define	SHELP_UNINSTALL	"uninstall [-F] [brand-specific args]"
1367089Sgjelinek #define	SHELP_CLONE	"clone [-m method] [-s <ZFS snapshot>] "\
1377089Sgjelinek 	"[brand-specific args] zonename"
1381300Sgjelinek #define	SHELP_MOVE	"move zonepath"
1397089Sgjelinek #define	SHELP_DETACH	"detach [-n] [brand-specific args]"
1407089Sgjelinek #define	SHELP_ATTACH	"attach [-F] [-n <path>] [brand-specific args]"
1412303Scarlsonj #define	SHELP_MARK	"mark incomplete"
1420Sstevel@tonic-gate 
1432712Snn35248 #define	EXEC_PREFIX	"exec "
1442712Snn35248 #define	EXEC_LEN	(strlen(EXEC_PREFIX))
1452712Snn35248 #define	RMCOMMAND	"/usr/bin/rm -rf"
1462712Snn35248 
1472712Snn35248 static int cleanup_zonepath(char *, boolean_t);
1482712Snn35248 
1493448Sdh155122 
1500Sstevel@tonic-gate static int help_func(int argc, char *argv[]);
1510Sstevel@tonic-gate static int ready_func(int argc, char *argv[]);
1520Sstevel@tonic-gate static int boot_func(int argc, char *argv[]);
1530Sstevel@tonic-gate static int halt_func(int argc, char *argv[]);
1540Sstevel@tonic-gate static int reboot_func(int argc, char *argv[]);
1550Sstevel@tonic-gate static int list_func(int argc, char *argv[]);
1560Sstevel@tonic-gate static int verify_func(int argc, char *argv[]);
1570Sstevel@tonic-gate static int install_func(int argc, char *argv[]);
1580Sstevel@tonic-gate static int uninstall_func(int argc, char *argv[]);
159766Scarlsonj static int mount_func(int argc, char *argv[]);
160766Scarlsonj static int unmount_func(int argc, char *argv[]);
1611300Sgjelinek static int clone_func(int argc, char *argv[]);
1621300Sgjelinek static int move_func(int argc, char *argv[]);
1631507Sgjelinek static int detach_func(int argc, char *argv[]);
1641507Sgjelinek static int attach_func(int argc, char *argv[]);
1652303Scarlsonj static int mark_func(int argc, char *argv[]);
1663247Sgjelinek static int apply_func(int argc, char *argv[]);
1670Sstevel@tonic-gate static int sanity_check(char *zone, int cmd_num, boolean_t running,
1682712Snn35248     boolean_t unsafe_when_running, boolean_t force);
1690Sstevel@tonic-gate static int cmd_match(char *cmd);
1703339Szt129084 static int verify_details(int, char *argv[]);
1713339Szt129084 static int verify_brand(zone_dochandle_t, int, char *argv[]);
1723339Szt129084 static int invoke_brand_handler(int, char *argv[]);
1730Sstevel@tonic-gate 
1740Sstevel@tonic-gate static struct cmd cmdtab[] = {
1750Sstevel@tonic-gate 	{ CMD_HELP,		"help",		SHELP_HELP,	help_func },
1760Sstevel@tonic-gate 	{ CMD_BOOT,		"boot",		SHELP_BOOT,	boot_func },
1770Sstevel@tonic-gate 	{ CMD_HALT,		"halt",		SHELP_HALT,	halt_func },
1780Sstevel@tonic-gate 	{ CMD_READY,		"ready",	SHELP_READY,	ready_func },
1790Sstevel@tonic-gate 	{ CMD_REBOOT,		"reboot",	SHELP_REBOOT,	reboot_func },
1800Sstevel@tonic-gate 	{ CMD_LIST,		"list",		SHELP_LIST,	list_func },
1810Sstevel@tonic-gate 	{ CMD_VERIFY,		"verify",	SHELP_VERIFY,	verify_func },
1820Sstevel@tonic-gate 	{ CMD_INSTALL,		"install",	SHELP_INSTALL,	install_func },
1830Sstevel@tonic-gate 	{ CMD_UNINSTALL,	"uninstall",	SHELP_UNINSTALL,
184766Scarlsonj 	    uninstall_func },
1851300Sgjelinek 	/* mount and unmount are private commands for admin/install */
186766Scarlsonj 	{ CMD_MOUNT,		"mount",	NULL,		mount_func },
1871300Sgjelinek 	{ CMD_UNMOUNT,		"unmount",	NULL,		unmount_func },
1881300Sgjelinek 	{ CMD_CLONE,		"clone",	SHELP_CLONE,	clone_func },
1891507Sgjelinek 	{ CMD_MOVE,		"move",		SHELP_MOVE,	move_func },
1901507Sgjelinek 	{ CMD_DETACH,		"detach",	SHELP_DETACH,	detach_func },
1912303Scarlsonj 	{ CMD_ATTACH,		"attach",	SHELP_ATTACH,	attach_func },
1923247Sgjelinek 	{ CMD_MARK,		"mark",		SHELP_MARK,	mark_func },
1933247Sgjelinek 	{ CMD_APPLY,		"apply",	NULL,		apply_func }
1940Sstevel@tonic-gate };
1950Sstevel@tonic-gate 
1960Sstevel@tonic-gate /* global variables */
1970Sstevel@tonic-gate 
1980Sstevel@tonic-gate /* set early in main(), never modified thereafter, used all over the place */
1990Sstevel@tonic-gate static char *execname;
2002712Snn35248 static char target_brand[MAXNAMELEN];
2010Sstevel@tonic-gate static char *locale;
2021867Sgjelinek char *target_zone;
2032303Scarlsonj static char *target_uuid;
2040Sstevel@tonic-gate 
2050Sstevel@tonic-gate /* used in do_subproc() and signal handler */
2060Sstevel@tonic-gate static volatile boolean_t child_killed;
2072712Snn35248 static int do_subproc_cnt = 0;
2082712Snn35248 
2092712Snn35248 /*
2102712Snn35248  * Used to indicate whether this zoneadm instance has another zoneadm
2112712Snn35248  * instance in its ancestry.
2122712Snn35248  */
2132712Snn35248 static boolean_t zoneadm_is_nested = B_FALSE;
2142712Snn35248 
2151867Sgjelinek char *
2160Sstevel@tonic-gate cmd_to_str(int cmd_num)
2170Sstevel@tonic-gate {
2180Sstevel@tonic-gate 	assert(cmd_num >= CMD_MIN && cmd_num <= CMD_MAX);
2190Sstevel@tonic-gate 	return (cmdtab[cmd_num].cmd_name);
2200Sstevel@tonic-gate }
2210Sstevel@tonic-gate 
2220Sstevel@tonic-gate /* This is a separate function because of gettext() wrapping. */
2230Sstevel@tonic-gate static char *
2240Sstevel@tonic-gate long_help(int cmd_num)
2250Sstevel@tonic-gate {
226222Scomay 	assert(cmd_num >= CMD_MIN && cmd_num <= CMD_MAX);
2270Sstevel@tonic-gate 	switch (cmd_num) {
2281634Sgjelinek 	case CMD_HELP:
2291634Sgjelinek 		return (gettext("Print usage message."));
2301634Sgjelinek 	case CMD_BOOT:
2312267Sdp 		return (gettext("Activates (boots) specified zone.  See "
2322267Sdp 		    "zoneadm(1m) for valid boot\n\targuments."));
2331634Sgjelinek 	case CMD_HALT:
2341634Sgjelinek 		return (gettext("Halts specified zone, bypassing shutdown "
2351634Sgjelinek 		    "scripts and removing runtime\n\tresources of the zone."));
2361634Sgjelinek 	case CMD_READY:
2371634Sgjelinek 		return (gettext("Prepares a zone for running applications but "
2381634Sgjelinek 		    "does not start any user\n\tprocesses in the zone."));
2391634Sgjelinek 	case CMD_REBOOT:
2401634Sgjelinek 		return (gettext("Restarts the zone (equivalent to a halt / "
2412267Sdp 		    "boot sequence).\n\tFails if the zone is not active.  "
2422267Sdp 		    "See zoneadm(1m) for valid boot\n\targuments."));
2431634Sgjelinek 	case CMD_LIST:
2441634Sgjelinek 		return (gettext("Lists the current zones, or a "
2451634Sgjelinek 		    "specific zone if indicated.  By default,\n\tall "
2461634Sgjelinek 		    "running zones are listed, though this can be "
2471634Sgjelinek 		    "expanded to all\n\tinstalled zones with the -i "
2481634Sgjelinek 		    "option or all configured zones with the\n\t-c "
2492303Scarlsonj 		    "option.  When used with the general -z <zone> and/or -u "
2502303Scarlsonj 		    "<uuid-match>\n\toptions, lists only the specified "
2512303Scarlsonj 		    "matching zone, but lists it\n\tregardless of its state, "
2522303Scarlsonj 		    "and the -i and -c options are disallowed.  The\n\t-v "
2532303Scarlsonj 		    "option can be used to display verbose information: zone "
2542303Scarlsonj 		    "name, id,\n\tcurrent state, root directory and options.  "
2552303Scarlsonj 		    "The -p option can be used\n\tto request machine-parsable "
2562303Scarlsonj 		    "output.  The -v and -p options are mutually\n\texclusive."
2572303Scarlsonj 		    "  If neither -v nor -p is used, just the zone name is "
2582303Scarlsonj 		    "listed."));
2591634Sgjelinek 	case CMD_VERIFY:
2601634Sgjelinek 		return (gettext("Check to make sure the configuration "
2611634Sgjelinek 		    "can safely be instantiated\n\ton the machine: "
2621634Sgjelinek 		    "physical network interfaces exist, etc."));
2631634Sgjelinek 	case CMD_INSTALL:
2641867Sgjelinek 		return (gettext("Install the configuration on to the system.  "
2651867Sgjelinek 		    "The -x nodataset option\n\tcan be used to prevent the "
2661867Sgjelinek 		    "creation of a new ZFS file system for the\n\tzone "
2672712Snn35248 		    "(assuming the zonepath is within a ZFS file system).\n\t"
2682712Snn35248 		    "All other arguments are passed to the brand installation "
2697089Sgjelinek 		    "function;\n\tsee brands(5) for more information."));
2701634Sgjelinek 	case CMD_UNINSTALL:
2711634Sgjelinek 		return (gettext("Uninstall the configuration from the system.  "
2727089Sgjelinek 		    "The -F flag can be used\n\tto force the action.  All "
2737089Sgjelinek 		    "other arguments are passed to the brand\n\tuninstall "
2747089Sgjelinek 		    "function; see brands(5) for more information."));
2751634Sgjelinek 	case CMD_CLONE:
2761867Sgjelinek 		return (gettext("Clone the installation of another zone.  "
2771867Sgjelinek 		    "The -m option can be used to\n\tspecify 'copy' which "
2781867Sgjelinek 		    "forces a copy of the source zone.  The -s option\n\t"
2791867Sgjelinek 		    "can be used to specify the name of a ZFS snapshot "
2801867Sgjelinek 		    "that was taken from\n\ta previous clone command.  The "
2811867Sgjelinek 		    "snapshot will be used as the source\n\tinstead of "
2827089Sgjelinek 		    "creating a new ZFS snapshot.  All other arguments are "
2837089Sgjelinek 		    "passed\n\tto the brand clone function; see "
2847089Sgjelinek 		    "brands(5) for more information."));
2851634Sgjelinek 	case CMD_MOVE:
2861634Sgjelinek 		return (gettext("Move the zone to a new zonepath."));
2871634Sgjelinek 	case CMD_DETACH:
2881634Sgjelinek 		return (gettext("Detach the zone from the system. The zone "
2891634Sgjelinek 		    "state is changed to\n\t'configured' (but the files under "
2901634Sgjelinek 		    "the zonepath are untouched).\n\tThe zone can subsequently "
2911634Sgjelinek 		    "be attached, or can be moved to another\n\tsystem and "
2922078Sgjelinek 		    "attached there.  The -n option can be used to specify\n\t"
2932078Sgjelinek 		    "'no-execute' mode.  When -n is used, the information "
2942078Sgjelinek 		    "needed to attach\n\tthe zone is sent to standard output "
2957089Sgjelinek 		    "but the zone is not actually\n\tdetached.  All other "
2967089Sgjelinek 		    "arguments are passed to the brand detach function;\n\tsee "
2977089Sgjelinek 		    "brands(5) for more information."));
2981634Sgjelinek 	case CMD_ATTACH:
2991634Sgjelinek 		return (gettext("Attach the zone to the system.  The zone "
3001634Sgjelinek 		    "state must be 'configured'\n\tprior to attach; upon "
3011634Sgjelinek 		    "successful completion, the zone state will be\n\t"
3021634Sgjelinek 		    "'installed'.  The system software on the current "
3031634Sgjelinek 		    "system must be\n\tcompatible with the software on the "
3047089Sgjelinek 		    "zone's original system.\n\tSpecify -F "
3055829Sgjelinek 		    "to force the attach and skip software compatibility "
3065829Sgjelinek 		    "tests.\n\tThe -n option can be used to specify "
3075829Sgjelinek 		    "'no-execute' mode.  When -n is\n\tused, the information "
3085829Sgjelinek 		    "needed to attach the zone is read from the\n\tspecified "
3095829Sgjelinek 		    "path and the configuration is only validated.  The path "
3107089Sgjelinek 		    "can\n\tbe '-' to specify standard input.  The -F and -n "
3117089Sgjelinek 		    "options are mutually\n\texclusive.  All other arguments "
3127089Sgjelinek 		    "are passed to the brand attach\n\tfunction; see "
3137089Sgjelinek 		    "brands(5) for more information."));
3142303Scarlsonj 	case CMD_MARK:
3152303Scarlsonj 		return (gettext("Set the state of the zone.  This can be used "
3162303Scarlsonj 		    "to force the zone\n\tstate to 'incomplete' "
3172303Scarlsonj 		    "administratively if some activity has rendered\n\tthe "
3182303Scarlsonj 		    "zone permanently unusable.  The only valid state that "
3192303Scarlsonj 		    "may be\n\tspecified is 'incomplete'."));
3201634Sgjelinek 	default:
3211634Sgjelinek 		return ("");
3220Sstevel@tonic-gate 	}
3230Sstevel@tonic-gate 	/* NOTREACHED */
324222Scomay 	return (NULL);
3250Sstevel@tonic-gate }
3260Sstevel@tonic-gate 
3270Sstevel@tonic-gate /*
3280Sstevel@tonic-gate  * Called with explicit B_TRUE when help is explicitly requested, B_FALSE for
3290Sstevel@tonic-gate  * unexpected errors.
3300Sstevel@tonic-gate  */
3310Sstevel@tonic-gate 
3320Sstevel@tonic-gate static int
3330Sstevel@tonic-gate usage(boolean_t explicit)
3340Sstevel@tonic-gate {
3350Sstevel@tonic-gate 	int i;
3360Sstevel@tonic-gate 	FILE *fd = explicit ? stdout : stderr;
3370Sstevel@tonic-gate 
3380Sstevel@tonic-gate 	(void) fprintf(fd, "%s:\t%s help\n", gettext("usage"), execname);
3392303Scarlsonj 	(void) fprintf(fd, "\t%s [-z <zone>] [-u <uuid-match>] list\n",
3402303Scarlsonj 	    execname);
3412303Scarlsonj 	(void) fprintf(fd, "\t%s {-z <zone>|-u <uuid-match>} <%s>\n", execname,
3420Sstevel@tonic-gate 	    gettext("subcommand"));
3430Sstevel@tonic-gate 	(void) fprintf(fd, "\n%s:\n\n", gettext("Subcommands"));
3440Sstevel@tonic-gate 	for (i = CMD_MIN; i <= CMD_MAX; i++) {
345766Scarlsonj 		if (cmdtab[i].short_usage == NULL)
346766Scarlsonj 			continue;
3470Sstevel@tonic-gate 		(void) fprintf(fd, "%s\n", cmdtab[i].short_usage);
3480Sstevel@tonic-gate 		if (explicit)
3490Sstevel@tonic-gate 			(void) fprintf(fd, "\t%s\n\n", long_help(i));
3500Sstevel@tonic-gate 	}
3510Sstevel@tonic-gate 	if (!explicit)
3520Sstevel@tonic-gate 		(void) fputs("\n", fd);
3530Sstevel@tonic-gate 	return (Z_USAGE);
3540Sstevel@tonic-gate }
3550Sstevel@tonic-gate 
3560Sstevel@tonic-gate static void
3570Sstevel@tonic-gate sub_usage(char *short_usage, int cmd_num)
3580Sstevel@tonic-gate {
3590Sstevel@tonic-gate 	(void) fprintf(stderr, "%s:\t%s\n", gettext("usage"), short_usage);
3600Sstevel@tonic-gate 	(void) fprintf(stderr, "\t%s\n", long_help(cmd_num));
3610Sstevel@tonic-gate }
3620Sstevel@tonic-gate 
3630Sstevel@tonic-gate /*
3640Sstevel@tonic-gate  * zperror() is like perror(3c) except that this also prints the executable
3650Sstevel@tonic-gate  * name at the start of the message, and takes a boolean indicating whether
3660Sstevel@tonic-gate  * to call libc'c strerror() or that from libzonecfg.
3670Sstevel@tonic-gate  */
3680Sstevel@tonic-gate 
3691867Sgjelinek void
3700Sstevel@tonic-gate zperror(const char *str, boolean_t zonecfg_error)
3710Sstevel@tonic-gate {
3720Sstevel@tonic-gate 	(void) fprintf(stderr, "%s: %s: %s\n", execname, str,
3730Sstevel@tonic-gate 	    zonecfg_error ? zonecfg_strerror(errno) : strerror(errno));
3740Sstevel@tonic-gate }
3750Sstevel@tonic-gate 
3760Sstevel@tonic-gate /*
3770Sstevel@tonic-gate  * zperror2() is very similar to zperror() above, except it also prints a
3780Sstevel@tonic-gate  * supplied zone name after the executable.
3790Sstevel@tonic-gate  *
3800Sstevel@tonic-gate  * All current consumers of this function want libzonecfg's strerror() rather
3810Sstevel@tonic-gate  * than libc's; if this ever changes, this function can be made more generic
3820Sstevel@tonic-gate  * like zperror() above.
3830Sstevel@tonic-gate  */
3840Sstevel@tonic-gate 
3851867Sgjelinek void
3860Sstevel@tonic-gate zperror2(const char *zone, const char *str)
3870Sstevel@tonic-gate {
3880Sstevel@tonic-gate 	(void) fprintf(stderr, "%s: %s: %s: %s\n", execname, zone, str,
3890Sstevel@tonic-gate 	    zonecfg_strerror(errno));
3900Sstevel@tonic-gate }
3910Sstevel@tonic-gate 
3920Sstevel@tonic-gate /* PRINTFLIKE1 */
3931867Sgjelinek void
3940Sstevel@tonic-gate zerror(const char *fmt, ...)
3950Sstevel@tonic-gate {
3960Sstevel@tonic-gate 	va_list alist;
3970Sstevel@tonic-gate 
3980Sstevel@tonic-gate 	va_start(alist, fmt);
3990Sstevel@tonic-gate 	(void) fprintf(stderr, "%s: ", execname);
4000Sstevel@tonic-gate 	if (target_zone != NULL)
4010Sstevel@tonic-gate 		(void) fprintf(stderr, "zone '%s': ", target_zone);
4020Sstevel@tonic-gate 	(void) vfprintf(stderr, fmt, alist);
4030Sstevel@tonic-gate 	(void) fprintf(stderr, "\n");
4040Sstevel@tonic-gate 	va_end(alist);
4050Sstevel@tonic-gate }
4060Sstevel@tonic-gate 
4070Sstevel@tonic-gate static void *
4080Sstevel@tonic-gate safe_calloc(size_t nelem, size_t elsize)
4090Sstevel@tonic-gate {
4100Sstevel@tonic-gate 	void *r = calloc(nelem, elsize);
4110Sstevel@tonic-gate 
4120Sstevel@tonic-gate 	if (r == NULL) {
4130Sstevel@tonic-gate 		zerror(gettext("failed to allocate %lu bytes: %s"),
4140Sstevel@tonic-gate 		    (ulong_t)nelem * elsize, strerror(errno));
4150Sstevel@tonic-gate 		exit(Z_ERR);
4160Sstevel@tonic-gate 	}
4170Sstevel@tonic-gate 	return (r);
4180Sstevel@tonic-gate }
4190Sstevel@tonic-gate 
4200Sstevel@tonic-gate static void
4210Sstevel@tonic-gate zone_print(zone_entry_t *zent, boolean_t verbose, boolean_t parsable)
4220Sstevel@tonic-gate {
4230Sstevel@tonic-gate 	static boolean_t firsttime = B_TRUE;
4243448Sdh155122 	char *ip_type_str;
4253448Sdh155122 
4263448Sdh155122 	if (zent->ziptype == ZS_EXCLUSIVE)
4273448Sdh155122 		ip_type_str = "excl";
4283448Sdh155122 	else
4293448Sdh155122 		ip_type_str = "shared";
4300Sstevel@tonic-gate 
4310Sstevel@tonic-gate 	assert(!(verbose && parsable));
4320Sstevel@tonic-gate 	if (firsttime && verbose) {
4330Sstevel@tonic-gate 		firsttime = B_FALSE;
4343448Sdh155122 		(void) printf("%*s %-16s %-10s %-30s %-8s %-6s\n",
4353448Sdh155122 		    ZONEID_WIDTH, "ID", "NAME", "STATUS", "PATH", "BRAND",
4363448Sdh155122 		    "IP");
4370Sstevel@tonic-gate 	}
4380Sstevel@tonic-gate 	if (!verbose) {
4392303Scarlsonj 		char *cp, *clim;
4402303Scarlsonj 
4410Sstevel@tonic-gate 		if (!parsable) {
4420Sstevel@tonic-gate 			(void) printf("%s\n", zent->zname);
4430Sstevel@tonic-gate 			return;
4440Sstevel@tonic-gate 		}
4450Sstevel@tonic-gate 		if (zent->zid == ZONE_ID_UNDEFINED)
4460Sstevel@tonic-gate 			(void) printf("-");
4470Sstevel@tonic-gate 		else
4480Sstevel@tonic-gate 			(void) printf("%lu", zent->zid);
4492303Scarlsonj 		(void) printf(":%s:%s:", zent->zname, zent->zstate_str);
4502303Scarlsonj 		cp = zent->zroot;
4512303Scarlsonj 		while ((clim = strchr(cp, ':')) != NULL) {
4522303Scarlsonj 			(void) printf("%.*s\\:", clim - cp, cp);
4532303Scarlsonj 			cp = clim + 1;
4542303Scarlsonj 		}
4553448Sdh155122 		(void) printf("%s:%s:%s:%s\n", cp, zent->zuuid, zent->zbrand,
4563448Sdh155122 		    ip_type_str);
4570Sstevel@tonic-gate 		return;
4580Sstevel@tonic-gate 	}
4590Sstevel@tonic-gate 	if (zent->zstate_str != NULL) {
4600Sstevel@tonic-gate 		if (zent->zid == ZONE_ID_UNDEFINED)
4610Sstevel@tonic-gate 			(void) printf("%*s", ZONEID_WIDTH, "-");
4620Sstevel@tonic-gate 		else
4630Sstevel@tonic-gate 			(void) printf("%*lu", ZONEID_WIDTH, zent->zid);
4643448Sdh155122 		(void) printf(" %-16s %-10s %-30s %-8s %-6s\n", zent->zname,
4653448Sdh155122 		    zent->zstate_str, zent->zroot, zent->zbrand, ip_type_str);
4660Sstevel@tonic-gate 	}
4670Sstevel@tonic-gate }
4680Sstevel@tonic-gate 
4690Sstevel@tonic-gate static int
470766Scarlsonj lookup_zone_info(const char *zone_name, zoneid_t zid, zone_entry_t *zent)
4710Sstevel@tonic-gate {
4721676Sjpk 	char root[MAXPATHLEN], *cp;
4730Sstevel@tonic-gate 	int err;
4742303Scarlsonj 	uuid_t uuid;
4750Sstevel@tonic-gate 
4760Sstevel@tonic-gate 	(void) strlcpy(zent->zname, zone_name, sizeof (zent->zname));
4770Sstevel@tonic-gate 	(void) strlcpy(zent->zroot, "???", sizeof (zent->zroot));
4782712Snn35248 	(void) strlcpy(zent->zbrand, "???", sizeof (zent->zbrand));
4790Sstevel@tonic-gate 	zent->zstate_str = "???";
4800Sstevel@tonic-gate 
481766Scarlsonj 	zent->zid = zid;
4820Sstevel@tonic-gate 
4832303Scarlsonj 	if (zonecfg_get_uuid(zone_name, uuid) == Z_OK &&
4842303Scarlsonj 	    !uuid_is_null(uuid))
4852303Scarlsonj 		uuid_unparse(uuid, zent->zuuid);
4862303Scarlsonj 	else
4872303Scarlsonj 		zent->zuuid[0] = '\0';
4882303Scarlsonj 
4891676Sjpk 	/*
4901676Sjpk 	 * For labeled zones which query the zone path of lower-level
4911676Sjpk 	 * zones, the path needs to be adjusted to drop the final
4921676Sjpk 	 * "/root" component. This adjusted path is then useful
4931676Sjpk 	 * for reading down any exported directories from the
4941676Sjpk 	 * lower-level zone.
4951676Sjpk 	 */
4961676Sjpk 	if (is_system_labeled() && zent->zid != ZONE_ID_UNDEFINED) {
4971676Sjpk 		if (zone_getattr(zent->zid, ZONE_ATTR_ROOT, zent->zroot,
4981676Sjpk 		    sizeof (zent->zroot)) == -1) {
4991676Sjpk 			zperror2(zent->zname,
5001676Sjpk 			    gettext("could not get zone path."));
5011676Sjpk 			return (Z_ERR);
5021676Sjpk 		}
5031676Sjpk 		cp = zent->zroot + strlen(zent->zroot) - 5;
5041676Sjpk 		if (cp > zent->zroot && strcmp(cp, "/root") == 0)
5051676Sjpk 			*cp = 0;
5061676Sjpk 	} else {
5071676Sjpk 		if ((err = zone_get_zonepath(zent->zname, root,
5081676Sjpk 		    sizeof (root))) != Z_OK) {
5091676Sjpk 			errno = err;
5101676Sjpk 			zperror2(zent->zname,
5111676Sjpk 			    gettext("could not get zone path."));
5121676Sjpk 			return (Z_ERR);
5131676Sjpk 		}
5141676Sjpk 		(void) strlcpy(zent->zroot, root, sizeof (zent->zroot));
5151676Sjpk 	}
5160Sstevel@tonic-gate 
5170Sstevel@tonic-gate 	if ((err = zone_get_state(zent->zname, &zent->zstate_num)) != Z_OK) {
5180Sstevel@tonic-gate 		errno = err;
5190Sstevel@tonic-gate 		zperror2(zent->zname, gettext("could not get state"));
5200Sstevel@tonic-gate 		return (Z_ERR);
5210Sstevel@tonic-gate 	}
5220Sstevel@tonic-gate 	zent->zstate_str = zone_state_str(zent->zstate_num);
5233009Snn35248 
5243009Snn35248 	/*
5253009Snn35248 	 * A zone's brand is only available in the .xml file describing it,
5263009Snn35248 	 * which is only visible to the global zone.  This causes
5273009Snn35248 	 * zone_get_brand() to fail when called from within a non-global
5283009Snn35248 	 * zone.  Fortunately we only do this on labeled systems, where we
5293009Snn35248 	 * know all zones are native.
5303009Snn35248 	 */
5313009Snn35248 	if (getzoneid() != GLOBAL_ZONEID) {
5323009Snn35248 		assert(is_system_labeled() != 0);
5333009Snn35248 		(void) strlcpy(zent->zbrand, NATIVE_BRAND_NAME,
5343009Snn35248 		    sizeof (zent->zbrand));
5353009Snn35248 	} else if (zone_get_brand(zent->zname, zent->zbrand,
5362712Snn35248 	    sizeof (zent->zbrand)) != Z_OK) {
5372712Snn35248 		zperror2(zent->zname, gettext("could not get brand name"));
5382712Snn35248 		return (Z_ERR);
5392712Snn35248 	}
5400Sstevel@tonic-gate 
5413448Sdh155122 	/*
5423448Sdh155122 	 * Get ip type of the zone.
5433448Sdh155122 	 * Note for global zone, ZS_SHARED is set always.
5443448Sdh155122 	 */
5453448Sdh155122 	if (zid == GLOBAL_ZONEID) {
5463448Sdh155122 		zent->ziptype = ZS_SHARED;
5473448Sdh155122 	} else {
5483448Sdh155122 
5493448Sdh155122 		if (zent->zstate_num == ZONE_STATE_RUNNING) {
5503448Sdh155122 			ushort_t flags;
5513448Sdh155122 
5523448Sdh155122 			if (zone_getattr(zid, ZONE_ATTR_FLAGS, &flags,
5533448Sdh155122 			    sizeof (flags)) < 0) {
5543448Sdh155122 				zperror2(zent->zname,
5553448Sdh155122 				    gettext("could not get zone flags"));
5563448Sdh155122 				return (Z_ERR);
5573448Sdh155122 			}
5583448Sdh155122 			if (flags & ZF_NET_EXCL)
5593448Sdh155122 				zent->ziptype = ZS_EXCLUSIVE;
5603448Sdh155122 			else
5613448Sdh155122 				zent->ziptype = ZS_SHARED;
5623448Sdh155122 		} else {
5633448Sdh155122 			zone_dochandle_t handle;
5643448Sdh155122 
5653448Sdh155122 			if ((handle = zonecfg_init_handle()) == NULL) {
5663448Sdh155122 				zperror2(zent->zname,
5673448Sdh155122 				    gettext("could not init handle"));
5683448Sdh155122 				return (Z_ERR);
5693448Sdh155122 			}
5703448Sdh155122 			if ((err = zonecfg_get_handle(zent->zname, handle))
5713448Sdh155122 			    != Z_OK) {
5723448Sdh155122 				zperror2(zent->zname,
5733448Sdh155122 				    gettext("could not get handle"));
5743448Sdh155122 				zonecfg_fini_handle(handle);
5753448Sdh155122 				return (Z_ERR);
5763448Sdh155122 			}
5773448Sdh155122 
5783448Sdh155122 			if ((err = zonecfg_get_iptype(handle, &zent->ziptype))
5793448Sdh155122 			    != Z_OK) {
5803448Sdh155122 				zperror2(zent->zname,
5813448Sdh155122 				    gettext("could not get ip-type"));
5823448Sdh155122 				zonecfg_fini_handle(handle);
5833448Sdh155122 				return (Z_ERR);
5843448Sdh155122 			}
5853448Sdh155122 			zonecfg_fini_handle(handle);
5863448Sdh155122 		}
5873448Sdh155122 	}
5883448Sdh155122 
5890Sstevel@tonic-gate 	return (Z_OK);
5900Sstevel@tonic-gate }
5910Sstevel@tonic-gate 
5920Sstevel@tonic-gate /*
5930Sstevel@tonic-gate  * fetch_zents() calls zone_list(2) to find out how many zones are running
5940Sstevel@tonic-gate  * (which is stored in the global nzents), then calls zone_list(2) again
5950Sstevel@tonic-gate  * to fetch the list of running zones (stored in the global zents).  This
5960Sstevel@tonic-gate  * function may be called multiple times, so if zents is already set, we
5970Sstevel@tonic-gate  * return immediately to save work.
5980Sstevel@tonic-gate  */
5990Sstevel@tonic-gate 
6000Sstevel@tonic-gate static int
601766Scarlsonj fetch_zents(void)
6020Sstevel@tonic-gate {
6030Sstevel@tonic-gate 	zoneid_t *zids = NULL;
6040Sstevel@tonic-gate 	uint_t nzents_saved;
605766Scarlsonj 	int i, retv;
606766Scarlsonj 	FILE *fp;
607766Scarlsonj 	boolean_t inaltroot;
608766Scarlsonj 	zone_entry_t *zentp;
6090Sstevel@tonic-gate 
6100Sstevel@tonic-gate 	if (nzents > 0)
6110Sstevel@tonic-gate 		return (Z_OK);
6120Sstevel@tonic-gate 
6130Sstevel@tonic-gate 	if (zone_list(NULL, &nzents) != 0) {
6140Sstevel@tonic-gate 		zperror(gettext("failed to get zoneid list"), B_FALSE);
6150Sstevel@tonic-gate 		return (Z_ERR);
6160Sstevel@tonic-gate 	}
6170Sstevel@tonic-gate 
6180Sstevel@tonic-gate again:
6190Sstevel@tonic-gate 	if (nzents == 0)
6200Sstevel@tonic-gate 		return (Z_OK);
6210Sstevel@tonic-gate 
6220Sstevel@tonic-gate 	zids = safe_calloc(nzents, sizeof (zoneid_t));
6230Sstevel@tonic-gate 	nzents_saved = nzents;
6240Sstevel@tonic-gate 
6250Sstevel@tonic-gate 	if (zone_list(zids, &nzents) != 0) {
6260Sstevel@tonic-gate 		zperror(gettext("failed to get zone list"), B_FALSE);
6270Sstevel@tonic-gate 		free(zids);
6280Sstevel@tonic-gate 		return (Z_ERR);
6290Sstevel@tonic-gate 	}
6300Sstevel@tonic-gate 	if (nzents != nzents_saved) {
6310Sstevel@tonic-gate 		/* list changed, try again */
6320Sstevel@tonic-gate 		free(zids);
6330Sstevel@tonic-gate 		goto again;
6340Sstevel@tonic-gate 	}
6350Sstevel@tonic-gate 
6360Sstevel@tonic-gate 	zents = safe_calloc(nzents, sizeof (zone_entry_t));
6370Sstevel@tonic-gate 
638766Scarlsonj 	inaltroot = zonecfg_in_alt_root();
639766Scarlsonj 	if (inaltroot)
640766Scarlsonj 		fp = zonecfg_open_scratch("", B_FALSE);
641766Scarlsonj 	else
642766Scarlsonj 		fp = NULL;
643766Scarlsonj 	zentp = zents;
644766Scarlsonj 	retv = Z_OK;
6450Sstevel@tonic-gate 	for (i = 0; i < nzents; i++) {
6460Sstevel@tonic-gate 		char name[ZONENAME_MAX];
647766Scarlsonj 		char altname[ZONENAME_MAX];
6480Sstevel@tonic-gate 
649766Scarlsonj 		if (getzonenamebyid(zids[i], name, sizeof (name)) < 0) {
6500Sstevel@tonic-gate 			zperror(gettext("failed to get zone name"), B_FALSE);
651766Scarlsonj 			retv = Z_ERR;
652766Scarlsonj 			continue;
653766Scarlsonj 		}
654766Scarlsonj 		if (zonecfg_is_scratch(name)) {
655766Scarlsonj 			/* Ignore scratch zones by default */
656766Scarlsonj 			if (!inaltroot)
657766Scarlsonj 				continue;
658766Scarlsonj 			if (fp == NULL ||
659766Scarlsonj 			    zonecfg_reverse_scratch(fp, name, altname,
660766Scarlsonj 			    sizeof (altname), NULL, 0) == -1) {
661924Sgjelinek 				zerror(gettext("could not resolve scratch "
662766Scarlsonj 				    "zone %s"), name);
663766Scarlsonj 				retv = Z_ERR;
664766Scarlsonj 				continue;
665766Scarlsonj 			}
666766Scarlsonj 			(void) strcpy(name, altname);
667766Scarlsonj 		} else {
668766Scarlsonj 			/* Ignore non-scratch when in an alternate root */
669766Scarlsonj 			if (inaltroot && strcmp(name, GLOBAL_ZONENAME) != 0)
670766Scarlsonj 				continue;
671766Scarlsonj 		}
672766Scarlsonj 		if (lookup_zone_info(name, zids[i], zentp) != Z_OK) {
673766Scarlsonj 			zerror(gettext("failed to get zone data"));
674766Scarlsonj 			retv = Z_ERR;
675766Scarlsonj 			continue;
676766Scarlsonj 		}
677766Scarlsonj 		zentp++;
6780Sstevel@tonic-gate 	}
679766Scarlsonj 	nzents = zentp - zents;
680766Scarlsonj 	if (fp != NULL)
681766Scarlsonj 		zonecfg_close_scratch(fp);
6820Sstevel@tonic-gate 
6830Sstevel@tonic-gate 	free(zids);
684766Scarlsonj 	return (retv);
6850Sstevel@tonic-gate }
6860Sstevel@tonic-gate 
687766Scarlsonj static int
6880Sstevel@tonic-gate zone_print_list(zone_state_t min_state, boolean_t verbose, boolean_t parsable)
6890Sstevel@tonic-gate {
6900Sstevel@tonic-gate 	int i;
6910Sstevel@tonic-gate 	zone_entry_t zent;
6920Sstevel@tonic-gate 	FILE *cookie;
6930Sstevel@tonic-gate 	char *name;
6940Sstevel@tonic-gate 
6950Sstevel@tonic-gate 	/*
6960Sstevel@tonic-gate 	 * First get the list of running zones from the kernel and print them.
6970Sstevel@tonic-gate 	 * If that is all we need, then return.
6980Sstevel@tonic-gate 	 */
699766Scarlsonj 	if ((i = fetch_zents()) != Z_OK) {
7000Sstevel@tonic-gate 		/*
7010Sstevel@tonic-gate 		 * No need for error messages; fetch_zents() has already taken
7020Sstevel@tonic-gate 		 * care of this.
7030Sstevel@tonic-gate 		 */
704766Scarlsonj 		return (i);
7050Sstevel@tonic-gate 	}
706766Scarlsonj 	for (i = 0; i < nzents; i++)
7070Sstevel@tonic-gate 		zone_print(&zents[i], verbose, parsable);
7080Sstevel@tonic-gate 	if (min_state >= ZONE_STATE_RUNNING)
709766Scarlsonj 		return (Z_OK);
7100Sstevel@tonic-gate 	/*
7110Sstevel@tonic-gate 	 * Next, get the full list of zones from the configuration, skipping
7120Sstevel@tonic-gate 	 * any we have already printed.
7130Sstevel@tonic-gate 	 */
7140Sstevel@tonic-gate 	cookie = setzoneent();
7150Sstevel@tonic-gate 	while ((name = getzoneent(cookie)) != NULL) {
7160Sstevel@tonic-gate 		for (i = 0; i < nzents; i++) {
7170Sstevel@tonic-gate 			if (strcmp(zents[i].zname, name) == 0)
7180Sstevel@tonic-gate 				break;
7190Sstevel@tonic-gate 		}
7200Sstevel@tonic-gate 		if (i < nzents) {
7210Sstevel@tonic-gate 			free(name);
7220Sstevel@tonic-gate 			continue;
7230Sstevel@tonic-gate 		}
724766Scarlsonj 		if (lookup_zone_info(name, ZONE_ID_UNDEFINED, &zent) != Z_OK) {
7250Sstevel@tonic-gate 			free(name);
7260Sstevel@tonic-gate 			continue;
7270Sstevel@tonic-gate 		}
7280Sstevel@tonic-gate 		free(name);
7290Sstevel@tonic-gate 		if (zent.zstate_num >= min_state)
7300Sstevel@tonic-gate 			zone_print(&zent, verbose, parsable);
7310Sstevel@tonic-gate 	}
7320Sstevel@tonic-gate 	endzoneent(cookie);
733766Scarlsonj 	return (Z_OK);
7340Sstevel@tonic-gate }
7350Sstevel@tonic-gate 
7368083SJordan.Vaughan@Sun.com /*
7378083SJordan.Vaughan@Sun.com  * Retrieve a zone entry by name.  Returns NULL if no such zone exists.
7388083SJordan.Vaughan@Sun.com  */
7390Sstevel@tonic-gate static zone_entry_t *
7408083SJordan.Vaughan@Sun.com lookup_running_zone(const char *str)
7410Sstevel@tonic-gate {
7420Sstevel@tonic-gate 	int i;
7430Sstevel@tonic-gate 
7440Sstevel@tonic-gate 	if (fetch_zents() != Z_OK)
7450Sstevel@tonic-gate 		return (NULL);
7460Sstevel@tonic-gate 
7470Sstevel@tonic-gate 	for (i = 0; i < nzents; i++) {
7480Sstevel@tonic-gate 		if (strcmp(str, zents[i].zname) == 0)
7490Sstevel@tonic-gate 			return (&zents[i]);
7500Sstevel@tonic-gate 	}
7510Sstevel@tonic-gate 	return (NULL);
7520Sstevel@tonic-gate }
7530Sstevel@tonic-gate 
7540Sstevel@tonic-gate /*
7550Sstevel@tonic-gate  * Check a bit in a mode_t: if on is B_TRUE, that bit should be on; if
7560Sstevel@tonic-gate  * B_FALSE, it should be off.  Return B_TRUE if the mode is bad (incorrect).
7570Sstevel@tonic-gate  */
7580Sstevel@tonic-gate static boolean_t
7590Sstevel@tonic-gate bad_mode_bit(mode_t mode, mode_t bit, boolean_t on, char *file)
7600Sstevel@tonic-gate {
7610Sstevel@tonic-gate 	char *str;
7620Sstevel@tonic-gate 
7630Sstevel@tonic-gate 	assert(bit == S_IRUSR || bit == S_IWUSR || bit == S_IXUSR ||
7640Sstevel@tonic-gate 	    bit == S_IRGRP || bit == S_IWGRP || bit == S_IXGRP ||
7650Sstevel@tonic-gate 	    bit == S_IROTH || bit == S_IWOTH || bit == S_IXOTH);
7660Sstevel@tonic-gate 	/*
7670Sstevel@tonic-gate 	 * TRANSLATION_NOTE
7680Sstevel@tonic-gate 	 * The strings below will be used as part of a larger message,
7690Sstevel@tonic-gate 	 * either:
7700Sstevel@tonic-gate 	 * (file name) must be (owner|group|world) (read|writ|execut)able
7710Sstevel@tonic-gate 	 * or
7720Sstevel@tonic-gate 	 * (file name) must not be (owner|group|world) (read|writ|execut)able
7730Sstevel@tonic-gate 	 */
7740Sstevel@tonic-gate 	switch (bit) {
7750Sstevel@tonic-gate 	case S_IRUSR:
7760Sstevel@tonic-gate 		str = gettext("owner readable");
7770Sstevel@tonic-gate 		break;
7780Sstevel@tonic-gate 	case S_IWUSR:
7790Sstevel@tonic-gate 		str = gettext("owner writable");
7800Sstevel@tonic-gate 		break;
7810Sstevel@tonic-gate 	case S_IXUSR:
7820Sstevel@tonic-gate 		str = gettext("owner executable");
7830Sstevel@tonic-gate 		break;
7840Sstevel@tonic-gate 	case S_IRGRP:
7850Sstevel@tonic-gate 		str = gettext("group readable");
7860Sstevel@tonic-gate 		break;
7870Sstevel@tonic-gate 	case S_IWGRP:
7880Sstevel@tonic-gate 		str = gettext("group writable");
7890Sstevel@tonic-gate 		break;
7900Sstevel@tonic-gate 	case S_IXGRP:
7910Sstevel@tonic-gate 		str = gettext("group executable");
7920Sstevel@tonic-gate 		break;
7930Sstevel@tonic-gate 	case S_IROTH:
7940Sstevel@tonic-gate 		str = gettext("world readable");
7950Sstevel@tonic-gate 		break;
7960Sstevel@tonic-gate 	case S_IWOTH:
7970Sstevel@tonic-gate 		str = gettext("world writable");
7980Sstevel@tonic-gate 		break;
7990Sstevel@tonic-gate 	case S_IXOTH:
8000Sstevel@tonic-gate 		str = gettext("world executable");
8010Sstevel@tonic-gate 		break;
8020Sstevel@tonic-gate 	}
8030Sstevel@tonic-gate 	if ((mode & bit) == (on ? 0 : bit)) {
8040Sstevel@tonic-gate 		/*
8050Sstevel@tonic-gate 		 * TRANSLATION_NOTE
8060Sstevel@tonic-gate 		 * The first parameter below is a file name; the second
8070Sstevel@tonic-gate 		 * is one of the "(owner|group|world) (read|writ|execut)able"
8080Sstevel@tonic-gate 		 * strings from above.
8090Sstevel@tonic-gate 		 */
8100Sstevel@tonic-gate 		/*
8110Sstevel@tonic-gate 		 * The code below could be simplified but not in a way
8120Sstevel@tonic-gate 		 * that would easily translate to non-English locales.
8130Sstevel@tonic-gate 		 */
8140Sstevel@tonic-gate 		if (on) {
8150Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("%s must be %s.\n"),
8160Sstevel@tonic-gate 			    file, str);
8170Sstevel@tonic-gate 		} else {
8180Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("%s must not be %s.\n"),
8190Sstevel@tonic-gate 			    file, str);
8200Sstevel@tonic-gate 		}
8210Sstevel@tonic-gate 		return (B_TRUE);
8220Sstevel@tonic-gate 	}
8230Sstevel@tonic-gate 	return (B_FALSE);
8240Sstevel@tonic-gate }
8250Sstevel@tonic-gate 
8260Sstevel@tonic-gate /*
8270Sstevel@tonic-gate  * We want to make sure that no zone has its zone path as a child node
8280Sstevel@tonic-gate  * (in the directory sense) of any other.  We do that by comparing this
8290Sstevel@tonic-gate  * zone's path to the path of all other (non-global) zones.  The comparison
8300Sstevel@tonic-gate  * in each case is simple: add '/' to the end of the path, then do a
8310Sstevel@tonic-gate  * strncmp() of the two paths, using the length of the shorter one.
8320Sstevel@tonic-gate  */
8330Sstevel@tonic-gate 
8340Sstevel@tonic-gate static int
8350Sstevel@tonic-gate crosscheck_zonepaths(char *path)
8360Sstevel@tonic-gate {
8370Sstevel@tonic-gate 	char rpath[MAXPATHLEN];		/* resolved path */
8380Sstevel@tonic-gate 	char path_copy[MAXPATHLEN];	/* copy of original path */
8390Sstevel@tonic-gate 	char rpath_copy[MAXPATHLEN];	/* copy of original rpath */
8400Sstevel@tonic-gate 	struct zoneent *ze;
8410Sstevel@tonic-gate 	int res, err;
8420Sstevel@tonic-gate 	FILE *cookie;
8430Sstevel@tonic-gate 
8440Sstevel@tonic-gate 	cookie = setzoneent();
8450Sstevel@tonic-gate 	while ((ze = getzoneent_private(cookie)) != NULL) {
8460Sstevel@tonic-gate 		/* Skip zones which are not installed. */
8470Sstevel@tonic-gate 		if (ze->zone_state < ZONE_STATE_INSTALLED) {
8480Sstevel@tonic-gate 			free(ze);
8490Sstevel@tonic-gate 			continue;
8500Sstevel@tonic-gate 		}
8510Sstevel@tonic-gate 		/* Skip the global zone and the current target zone. */
8520Sstevel@tonic-gate 		if (strcmp(ze->zone_name, GLOBAL_ZONENAME) == 0 ||
8530Sstevel@tonic-gate 		    strcmp(ze->zone_name, target_zone) == 0) {
8540Sstevel@tonic-gate 			free(ze);
8550Sstevel@tonic-gate 			continue;
8560Sstevel@tonic-gate 		}
8570Sstevel@tonic-gate 		if (strlen(ze->zone_path) == 0) {
8580Sstevel@tonic-gate 			/* old index file without path, fall back */
8590Sstevel@tonic-gate 			if ((err = zone_get_zonepath(ze->zone_name,
8600Sstevel@tonic-gate 			    ze->zone_path, sizeof (ze->zone_path))) != Z_OK) {
8610Sstevel@tonic-gate 				errno = err;
8620Sstevel@tonic-gate 				zperror2(ze->zone_name,
8630Sstevel@tonic-gate 				    gettext("could not get zone path"));
8640Sstevel@tonic-gate 				free(ze);
8650Sstevel@tonic-gate 				continue;
8660Sstevel@tonic-gate 			}
8670Sstevel@tonic-gate 		}
868766Scarlsonj 		(void) snprintf(path_copy, sizeof (path_copy), "%s%s",
869766Scarlsonj 		    zonecfg_get_root(), ze->zone_path);
870766Scarlsonj 		res = resolvepath(path_copy, rpath, sizeof (rpath));
8710Sstevel@tonic-gate 		if (res == -1) {
8720Sstevel@tonic-gate 			if (errno != ENOENT) {
873766Scarlsonj 				zperror(path_copy, B_FALSE);
8740Sstevel@tonic-gate 				free(ze);
8750Sstevel@tonic-gate 				return (Z_ERR);
8760Sstevel@tonic-gate 			}
8770Sstevel@tonic-gate 			(void) printf(gettext("WARNING: zone %s is installed, "
8780Sstevel@tonic-gate 			    "but its %s %s does not exist.\n"), ze->zone_name,
879766Scarlsonj 			    "zonepath", path_copy);
8800Sstevel@tonic-gate 			free(ze);
8810Sstevel@tonic-gate 			continue;
8820Sstevel@tonic-gate 		}
8830Sstevel@tonic-gate 		rpath[res] = '\0';
8840Sstevel@tonic-gate 		(void) snprintf(path_copy, sizeof (path_copy), "%s/", path);
8850Sstevel@tonic-gate 		(void) snprintf(rpath_copy, sizeof (rpath_copy), "%s/", rpath);
8860Sstevel@tonic-gate 		if (strncmp(path_copy, rpath_copy,
8870Sstevel@tonic-gate 		    min(strlen(path_copy), strlen(rpath_copy))) == 0) {
888924Sgjelinek 			/*
889924Sgjelinek 			 * TRANSLATION_NOTE
890924Sgjelinek 			 * zonepath is a literal that should not be translated.
891924Sgjelinek 			 */
8920Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("%s zonepath (%s) and "
8930Sstevel@tonic-gate 			    "%s zonepath (%s) overlap.\n"),
8940Sstevel@tonic-gate 			    target_zone, path, ze->zone_name, rpath);
8950Sstevel@tonic-gate 			free(ze);
8960Sstevel@tonic-gate 			return (Z_ERR);
8970Sstevel@tonic-gate 		}
8980Sstevel@tonic-gate 		free(ze);
8990Sstevel@tonic-gate 	}
9000Sstevel@tonic-gate 	endzoneent(cookie);
9010Sstevel@tonic-gate 	return (Z_OK);
9020Sstevel@tonic-gate }
9030Sstevel@tonic-gate 
9040Sstevel@tonic-gate static int
9050Sstevel@tonic-gate validate_zonepath(char *path, int cmd_num)
9060Sstevel@tonic-gate {
9070Sstevel@tonic-gate 	int res;			/* result of last library/system call */
9080Sstevel@tonic-gate 	boolean_t err = B_FALSE;	/* have we run into an error? */
9090Sstevel@tonic-gate 	struct stat stbuf;
9102267Sdp 	struct statvfs64 vfsbuf;
9110Sstevel@tonic-gate 	char rpath[MAXPATHLEN];		/* resolved path */
9120Sstevel@tonic-gate 	char ppath[MAXPATHLEN];		/* parent path */
9130Sstevel@tonic-gate 	char rppath[MAXPATHLEN];	/* resolved parent path */
9140Sstevel@tonic-gate 	char rootpath[MAXPATHLEN];	/* root path */
9150Sstevel@tonic-gate 	zone_state_t state;
9160Sstevel@tonic-gate 
9170Sstevel@tonic-gate 	if (path[0] != '/') {
9180Sstevel@tonic-gate 		(void) fprintf(stderr,
9190Sstevel@tonic-gate 		    gettext("%s is not an absolute path.\n"), path);
9200Sstevel@tonic-gate 		return (Z_ERR);
9210Sstevel@tonic-gate 	}
9220Sstevel@tonic-gate 	if ((res = resolvepath(path, rpath, sizeof (rpath))) == -1) {
9230Sstevel@tonic-gate 		if ((errno != ENOENT) ||
9241300Sgjelinek 		    (cmd_num != CMD_VERIFY && cmd_num != CMD_INSTALL &&
9251300Sgjelinek 		    cmd_num != CMD_CLONE && cmd_num != CMD_MOVE)) {
9260Sstevel@tonic-gate 			zperror(path, B_FALSE);
9270Sstevel@tonic-gate 			return (Z_ERR);
9280Sstevel@tonic-gate 		}
9290Sstevel@tonic-gate 		if (cmd_num == CMD_VERIFY) {
930924Sgjelinek 			/*
931924Sgjelinek 			 * TRANSLATION_NOTE
932924Sgjelinek 			 * zoneadm is a literal that should not be translated.
933924Sgjelinek 			 */
9340Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("WARNING: %s does not "
935924Sgjelinek 			    "exist, so it could not be verified.\nWhen "
936924Sgjelinek 			    "'zoneadm %s' is run, '%s' will try to create\n%s, "
937924Sgjelinek 			    "and '%s' will be tried again,\nbut the '%s' may "
938924Sgjelinek 			    "fail if:\nthe parent directory of %s is group- or "
939924Sgjelinek 			    "other-writable\nor\n%s overlaps with any other "
9400Sstevel@tonic-gate 			    "installed zones.\n"), path,
9410Sstevel@tonic-gate 			    cmd_to_str(CMD_INSTALL), cmd_to_str(CMD_INSTALL),
9420Sstevel@tonic-gate 			    path, cmd_to_str(CMD_VERIFY),
9430Sstevel@tonic-gate 			    cmd_to_str(CMD_VERIFY), path, path);
9440Sstevel@tonic-gate 			return (Z_OK);
9450Sstevel@tonic-gate 		}
9460Sstevel@tonic-gate 		/*
9470Sstevel@tonic-gate 		 * The zonepath is supposed to be mode 700 but its
9480Sstevel@tonic-gate 		 * parent(s) 755.  So use 755 on the mkdirp() then
9490Sstevel@tonic-gate 		 * chmod() the zonepath itself to 700.
9500Sstevel@tonic-gate 		 */
9510Sstevel@tonic-gate 		if (mkdirp(path, DEFAULT_DIR_MODE) < 0) {
9520Sstevel@tonic-gate 			zperror(path, B_FALSE);
9530Sstevel@tonic-gate 			return (Z_ERR);
9540Sstevel@tonic-gate 		}
9550Sstevel@tonic-gate 		/*
9560Sstevel@tonic-gate 		 * If the chmod() fails, report the error, but might
9570Sstevel@tonic-gate 		 * as well continue the verify procedure.
9580Sstevel@tonic-gate 		 */
9590Sstevel@tonic-gate 		if (chmod(path, S_IRWXU) != 0)
9600Sstevel@tonic-gate 			zperror(path, B_FALSE);
9610Sstevel@tonic-gate 		/*
9620Sstevel@tonic-gate 		 * Since the mkdir() succeeded, we should not have to
9630Sstevel@tonic-gate 		 * worry about a subsequent ENOENT, thus this should
9640Sstevel@tonic-gate 		 * only recurse once.
9650Sstevel@tonic-gate 		 */
9661300Sgjelinek 		return (validate_zonepath(path, cmd_num));
9670Sstevel@tonic-gate 	}
9680Sstevel@tonic-gate 	rpath[res] = '\0';
9690Sstevel@tonic-gate 	if (strcmp(path, rpath) != 0) {
9700Sstevel@tonic-gate 		errno = Z_RESOLVED_PATH;
9710Sstevel@tonic-gate 		zperror(path, B_TRUE);
9720Sstevel@tonic-gate 		return (Z_ERR);
9730Sstevel@tonic-gate 	}
9740Sstevel@tonic-gate 	if ((res = stat(rpath, &stbuf)) != 0) {
9750Sstevel@tonic-gate 		zperror(rpath, B_FALSE);
9760Sstevel@tonic-gate 		return (Z_ERR);
9770Sstevel@tonic-gate 	}
9780Sstevel@tonic-gate 	if (!S_ISDIR(stbuf.st_mode)) {
9790Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("%s is not a directory.\n"),
9800Sstevel@tonic-gate 		    rpath);
9810Sstevel@tonic-gate 		return (Z_ERR);
9820Sstevel@tonic-gate 	}
9833445Sblakej 	if (strcmp(stbuf.st_fstype, MNTTYPE_TMPFS) == 0) {
9840Sstevel@tonic-gate 		(void) printf(gettext("WARNING: %s is on a temporary "
9851867Sgjelinek 		    "file system.\n"), rpath);
9860Sstevel@tonic-gate 	}
9870Sstevel@tonic-gate 	if (crosscheck_zonepaths(rpath) != Z_OK)
9880Sstevel@tonic-gate 		return (Z_ERR);
9890Sstevel@tonic-gate 	/*
9900Sstevel@tonic-gate 	 * Try to collect and report as many minor errors as possible
9910Sstevel@tonic-gate 	 * before returning, so the user can learn everything that needs
9920Sstevel@tonic-gate 	 * to be fixed up front.
9930Sstevel@tonic-gate 	 */
9940Sstevel@tonic-gate 	if (stbuf.st_uid != 0) {
9950Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("%s is not owned by root.\n"),
9960Sstevel@tonic-gate 		    rpath);
9970Sstevel@tonic-gate 		err = B_TRUE;
9980Sstevel@tonic-gate 	}
9990Sstevel@tonic-gate 	err |= bad_mode_bit(stbuf.st_mode, S_IRUSR, B_TRUE, rpath);
10000Sstevel@tonic-gate 	err |= bad_mode_bit(stbuf.st_mode, S_IWUSR, B_TRUE, rpath);
10010Sstevel@tonic-gate 	err |= bad_mode_bit(stbuf.st_mode, S_IXUSR, B_TRUE, rpath);
10020Sstevel@tonic-gate 	err |= bad_mode_bit(stbuf.st_mode, S_IRGRP, B_FALSE, rpath);
10030Sstevel@tonic-gate 	err |= bad_mode_bit(stbuf.st_mode, S_IWGRP, B_FALSE, rpath);
10040Sstevel@tonic-gate 	err |= bad_mode_bit(stbuf.st_mode, S_IXGRP, B_FALSE, rpath);
10050Sstevel@tonic-gate 	err |= bad_mode_bit(stbuf.st_mode, S_IROTH, B_FALSE, rpath);
10060Sstevel@tonic-gate 	err |= bad_mode_bit(stbuf.st_mode, S_IWOTH, B_FALSE, rpath);
10070Sstevel@tonic-gate 	err |= bad_mode_bit(stbuf.st_mode, S_IXOTH, B_FALSE, rpath);
10080Sstevel@tonic-gate 
10090Sstevel@tonic-gate 	(void) snprintf(ppath, sizeof (ppath), "%s/..", path);
10100Sstevel@tonic-gate 	if ((res = resolvepath(ppath, rppath, sizeof (rppath))) == -1) {
10110Sstevel@tonic-gate 		zperror(ppath, B_FALSE);
10120Sstevel@tonic-gate 		return (Z_ERR);
10130Sstevel@tonic-gate 	}
10140Sstevel@tonic-gate 	rppath[res] = '\0';
10150Sstevel@tonic-gate 	if ((res = stat(rppath, &stbuf)) != 0) {
10160Sstevel@tonic-gate 		zperror(rppath, B_FALSE);
10170Sstevel@tonic-gate 		return (Z_ERR);
10180Sstevel@tonic-gate 	}
10190Sstevel@tonic-gate 	/* theoretically impossible */
10200Sstevel@tonic-gate 	if (!S_ISDIR(stbuf.st_mode)) {
10210Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("%s is not a directory.\n"),
10220Sstevel@tonic-gate 		    rppath);
10230Sstevel@tonic-gate 		return (Z_ERR);
10240Sstevel@tonic-gate 	}
10250Sstevel@tonic-gate 	if (stbuf.st_uid != 0) {
10260Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("%s is not owned by root.\n"),
10270Sstevel@tonic-gate 		    rppath);
10280Sstevel@tonic-gate 		err = B_TRUE;
10290Sstevel@tonic-gate 	}
10300Sstevel@tonic-gate 	err |= bad_mode_bit(stbuf.st_mode, S_IRUSR, B_TRUE, rppath);
10310Sstevel@tonic-gate 	err |= bad_mode_bit(stbuf.st_mode, S_IWUSR, B_TRUE, rppath);
10320Sstevel@tonic-gate 	err |= bad_mode_bit(stbuf.st_mode, S_IXUSR, B_TRUE, rppath);
10330Sstevel@tonic-gate 	err |= bad_mode_bit(stbuf.st_mode, S_IWGRP, B_FALSE, rppath);
10340Sstevel@tonic-gate 	err |= bad_mode_bit(stbuf.st_mode, S_IWOTH, B_FALSE, rppath);
10350Sstevel@tonic-gate 	if (strcmp(rpath, rppath) == 0) {
10360Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("%s is its own parent.\n"),
10370Sstevel@tonic-gate 		    rppath);
10380Sstevel@tonic-gate 		err = B_TRUE;
10390Sstevel@tonic-gate 	}
10400Sstevel@tonic-gate 
10412267Sdp 	if (statvfs64(rpath, &vfsbuf) != 0) {
10420Sstevel@tonic-gate 		zperror(rpath, B_FALSE);
10430Sstevel@tonic-gate 		return (Z_ERR);
10440Sstevel@tonic-gate 	}
1045823Sgjelinek 	if (strcmp(vfsbuf.f_basetype, MNTTYPE_NFS) == 0) {
1046924Sgjelinek 		/*
1047924Sgjelinek 		 * TRANSLATION_NOTE
1048924Sgjelinek 		 * Zonepath and NFS are literals that should not be translated.
1049924Sgjelinek 		 */
1050924Sgjelinek 		(void) fprintf(stderr, gettext("Zonepath %s is on an NFS "
10511867Sgjelinek 		    "mounted file system.\n"
10521867Sgjelinek 		    "\tA local file system must be used.\n"), rpath);
1053823Sgjelinek 		return (Z_ERR);
1054823Sgjelinek 	}
1055823Sgjelinek 	if (vfsbuf.f_flag & ST_NOSUID) {
1056924Sgjelinek 		/*
1057924Sgjelinek 		 * TRANSLATION_NOTE
1058924Sgjelinek 		 * Zonepath and nosuid are literals that should not be
1059924Sgjelinek 		 * translated.
1060924Sgjelinek 		 */
1061823Sgjelinek 		(void) fprintf(stderr, gettext("Zonepath %s is on a nosuid "
10621867Sgjelinek 		    "file system.\n"), rpath);
10630Sstevel@tonic-gate 		return (Z_ERR);
10640Sstevel@tonic-gate 	}
10650Sstevel@tonic-gate 
10660Sstevel@tonic-gate 	if ((res = zone_get_state(target_zone, &state)) != Z_OK) {
10670Sstevel@tonic-gate 		errno = res;
10680Sstevel@tonic-gate 		zperror2(target_zone, gettext("could not get state"));
10690Sstevel@tonic-gate 		return (Z_ERR);
10700Sstevel@tonic-gate 	}
10710Sstevel@tonic-gate 	/*
10720Sstevel@tonic-gate 	 * The existence of the root path is only bad in the configured state,
10730Sstevel@tonic-gate 	 * as it is *supposed* to be there at the installed and later states.
10741507Sgjelinek 	 * However, the root path is expected to be there if the zone is
10751507Sgjelinek 	 * detached.
10760Sstevel@tonic-gate 	 * State/command mismatches are caught earlier in verify_details().
10770Sstevel@tonic-gate 	 */
10781507Sgjelinek 	if (state == ZONE_STATE_CONFIGURED && cmd_num != CMD_ATTACH) {
10790Sstevel@tonic-gate 		if (snprintf(rootpath, sizeof (rootpath), "%s/root", rpath) >=
10800Sstevel@tonic-gate 		    sizeof (rootpath)) {
1081924Sgjelinek 			/*
1082924Sgjelinek 			 * TRANSLATION_NOTE
1083924Sgjelinek 			 * Zonepath is a literal that should not be translated.
1084924Sgjelinek 			 */
10850Sstevel@tonic-gate 			(void) fprintf(stderr,
10860Sstevel@tonic-gate 			    gettext("Zonepath %s is too long.\n"), rpath);
10870Sstevel@tonic-gate 			return (Z_ERR);
10880Sstevel@tonic-gate 		}
10890Sstevel@tonic-gate 		if ((res = stat(rootpath, &stbuf)) == 0) {
10907782Sgerald.jelinek@sun.com 			struct dirent	*dp;
10917782Sgerald.jelinek@sun.com 			DIR		*dirp;
10927782Sgerald.jelinek@sun.com 			boolean_t	empty = B_TRUE;
10937782Sgerald.jelinek@sun.com 
10947782Sgerald.jelinek@sun.com 			if (zonecfg_detached(rpath)) {
10951507Sgjelinek 				(void) fprintf(stderr,
10961507Sgjelinek 				    gettext("Cannot %s detached "
10971507Sgjelinek 				    "zone.\nUse attach or remove %s "
10981507Sgjelinek 				    "directory.\n"), cmd_to_str(cmd_num),
10991507Sgjelinek 				    rpath);
11007782Sgerald.jelinek@sun.com 				return (Z_ERR);
11017782Sgerald.jelinek@sun.com 			}
11027782Sgerald.jelinek@sun.com 
11037782Sgerald.jelinek@sun.com 			/* Not detached, check if it really looks ok. */
11047782Sgerald.jelinek@sun.com 
11057782Sgerald.jelinek@sun.com 			if (!S_ISDIR(stbuf.st_mode)) {
11067782Sgerald.jelinek@sun.com 				(void) fprintf(stderr, gettext("%s is not a "
11077782Sgerald.jelinek@sun.com 				    "directory.\n"), rootpath);
11087782Sgerald.jelinek@sun.com 				return (Z_ERR);
11097782Sgerald.jelinek@sun.com 			}
11107782Sgerald.jelinek@sun.com 
11117782Sgerald.jelinek@sun.com 			if (stbuf.st_uid != 0) {
11127782Sgerald.jelinek@sun.com 				(void) fprintf(stderr, gettext("%s is not "
11137782Sgerald.jelinek@sun.com 				    "owned by root.\n"), rootpath);
11147782Sgerald.jelinek@sun.com 				return (Z_ERR);
11157782Sgerald.jelinek@sun.com 			}
11167782Sgerald.jelinek@sun.com 
11177782Sgerald.jelinek@sun.com 			if ((stbuf.st_mode & 0777) != 0755) {
11187782Sgerald.jelinek@sun.com 				(void) fprintf(stderr, gettext("%s mode is not "
11197782Sgerald.jelinek@sun.com 				    "0755.\n"), rootpath);
11207782Sgerald.jelinek@sun.com 				return (Z_ERR);
11217782Sgerald.jelinek@sun.com 			}
11227782Sgerald.jelinek@sun.com 
11237782Sgerald.jelinek@sun.com 			if ((dirp = opendir(rootpath)) == NULL) {
11247782Sgerald.jelinek@sun.com 				(void) fprintf(stderr, gettext("Could not "
11257782Sgerald.jelinek@sun.com 				    "open rootpath %s\n"), rootpath);
11267782Sgerald.jelinek@sun.com 				return (Z_ERR);
11277782Sgerald.jelinek@sun.com 			}
11287782Sgerald.jelinek@sun.com 
11297782Sgerald.jelinek@sun.com 			/* Verify that the dir is empty. */
11307782Sgerald.jelinek@sun.com 			while ((dp = readdir(dirp)) != NULL) {
11317782Sgerald.jelinek@sun.com 				if (strcmp(dp->d_name, ".") == 0 ||
11327782Sgerald.jelinek@sun.com 				    strcmp(dp->d_name, "..") == 0)
11337782Sgerald.jelinek@sun.com 					continue;
11347782Sgerald.jelinek@sun.com 
11357782Sgerald.jelinek@sun.com 				empty = B_FALSE;
11367782Sgerald.jelinek@sun.com 				break;
11377782Sgerald.jelinek@sun.com 			}
11387782Sgerald.jelinek@sun.com 			(void) closedir(dirp);
11397782Sgerald.jelinek@sun.com 
11407782Sgerald.jelinek@sun.com 			if (!empty) {
11417782Sgerald.jelinek@sun.com 				(void) fprintf(stderr, gettext("Rootpath %s "
11427782Sgerald.jelinek@sun.com 				    "exists and contains data; remove or move "
11437782Sgerald.jelinek@sun.com 				    "aside prior to %s.\n"), rootpath,
11447782Sgerald.jelinek@sun.com 				    cmd_to_str(cmd_num));
11457782Sgerald.jelinek@sun.com 				return (Z_ERR);
11467782Sgerald.jelinek@sun.com 			}
11477782Sgerald.jelinek@sun.com 
11480Sstevel@tonic-gate 		}
11490Sstevel@tonic-gate 	}
11500Sstevel@tonic-gate 
11510Sstevel@tonic-gate 	return (err ? Z_ERR : Z_OK);
11520Sstevel@tonic-gate }
11530Sstevel@tonic-gate 
11540Sstevel@tonic-gate static int
11553339Szt129084 invoke_brand_handler(int cmd_num, char *argv[])
11563339Szt129084 {
11573339Szt129084 	zone_dochandle_t handle;
11583339Szt129084 	int err;
11593339Szt129084 
11603339Szt129084 	if ((handle = zonecfg_init_handle()) == NULL) {
11613339Szt129084 		zperror(cmd_to_str(cmd_num), B_TRUE);
11623339Szt129084 		return (Z_ERR);
11633339Szt129084 	}
11643339Szt129084 	if ((err = zonecfg_get_handle(target_zone, handle)) != Z_OK) {
11653339Szt129084 		errno = err;
11663339Szt129084 		zperror(cmd_to_str(cmd_num), B_TRUE);
11673339Szt129084 		zonecfg_fini_handle(handle);
11683339Szt129084 		return (Z_ERR);
11693339Szt129084 	}
11703339Szt129084 	if (verify_brand(handle, cmd_num, argv) != Z_OK) {
11713339Szt129084 		zonecfg_fini_handle(handle);
11723339Szt129084 		return (Z_ERR);
11733339Szt129084 	}
11743339Szt129084 	zonecfg_fini_handle(handle);
11753339Szt129084 	return (Z_OK);
11763339Szt129084 }
11773339Szt129084 
11783339Szt129084 static int
11790Sstevel@tonic-gate ready_func(int argc, char *argv[])
11800Sstevel@tonic-gate {
11810Sstevel@tonic-gate 	zone_cmd_arg_t zarg;
11820Sstevel@tonic-gate 	int arg;
11830Sstevel@tonic-gate 
1184766Scarlsonj 	if (zonecfg_in_alt_root()) {
1185766Scarlsonj 		zerror(gettext("cannot ready zone in alternate root"));
1186766Scarlsonj 		return (Z_ERR);
1187766Scarlsonj 	}
1188766Scarlsonj 
11890Sstevel@tonic-gate 	optind = 0;
11900Sstevel@tonic-gate 	if ((arg = getopt(argc, argv, "?")) != EOF) {
11910Sstevel@tonic-gate 		switch (arg) {
11920Sstevel@tonic-gate 		case '?':
11930Sstevel@tonic-gate 			sub_usage(SHELP_READY, CMD_READY);
11940Sstevel@tonic-gate 			return (optopt == '?' ? Z_OK : Z_USAGE);
11950Sstevel@tonic-gate 		default:
11960Sstevel@tonic-gate 			sub_usage(SHELP_READY, CMD_READY);
11970Sstevel@tonic-gate 			return (Z_USAGE);
11980Sstevel@tonic-gate 		}
11990Sstevel@tonic-gate 	}
12000Sstevel@tonic-gate 	if (argc > optind) {
12010Sstevel@tonic-gate 		sub_usage(SHELP_READY, CMD_READY);
12020Sstevel@tonic-gate 		return (Z_USAGE);
12030Sstevel@tonic-gate 	}
12042712Snn35248 	if (sanity_check(target_zone, CMD_READY, B_FALSE, B_FALSE, B_FALSE)
12052712Snn35248 	    != Z_OK)
12060Sstevel@tonic-gate 		return (Z_ERR);
12073339Szt129084 	if (verify_details(CMD_READY, argv) != Z_OK)
12080Sstevel@tonic-gate 		return (Z_ERR);
12090Sstevel@tonic-gate 
12100Sstevel@tonic-gate 	zarg.cmd = Z_READY;
12117089Sgjelinek 	if (zonecfg_call_zoneadmd(target_zone, &zarg, locale, B_TRUE) != 0) {
12120Sstevel@tonic-gate 		zerror(gettext("call to %s failed"), "zoneadmd");
12130Sstevel@tonic-gate 		return (Z_ERR);
12140Sstevel@tonic-gate 	}
12150Sstevel@tonic-gate 	return (Z_OK);
12160Sstevel@tonic-gate }
12170Sstevel@tonic-gate 
12180Sstevel@tonic-gate static int
12190Sstevel@tonic-gate boot_func(int argc, char *argv[])
12200Sstevel@tonic-gate {
12210Sstevel@tonic-gate 	zone_cmd_arg_t zarg;
12222712Snn35248 	boolean_t force = B_FALSE;
12230Sstevel@tonic-gate 	int arg;
12240Sstevel@tonic-gate 
1225766Scarlsonj 	if (zonecfg_in_alt_root()) {
1226766Scarlsonj 		zerror(gettext("cannot boot zone in alternate root"));
1227766Scarlsonj 		return (Z_ERR);
1228766Scarlsonj 	}
1229766Scarlsonj 
12300Sstevel@tonic-gate 	zarg.bootbuf[0] = '\0';
12310Sstevel@tonic-gate 
12320Sstevel@tonic-gate 	/*
12332267Sdp 	 * The following getopt processes arguments to zone boot; that
12342267Sdp 	 * is to say, the [here] portion of the argument string:
12352267Sdp 	 *
12362267Sdp 	 *	zoneadm -z myzone boot [here] -- -v -m verbose
12372267Sdp 	 *
12382267Sdp 	 * Where [here] can either be nothing, -? (in which case we bail
12392712Snn35248 	 * and print usage), -f (a private option to indicate that the
12402712Snn35248 	 * boot operation should be 'forced'), or -s.  Support for -s is
12412712Snn35248 	 * vestigal and obsolete, but is retained because it was a
12422712Snn35248 	 * documented interface and there are known consumers including
12432712Snn35248 	 * admin/install; the proper way to specify boot arguments like -s
12442712Snn35248 	 * is:
12452267Sdp 	 *
12462267Sdp 	 *	zoneadm -z myzone boot -- -s -v -m verbose.
12470Sstevel@tonic-gate 	 */
12480Sstevel@tonic-gate 	optind = 0;
12492712Snn35248 	while ((arg = getopt(argc, argv, "?fs")) != EOF) {
12500Sstevel@tonic-gate 		switch (arg) {
12510Sstevel@tonic-gate 		case '?':
12520Sstevel@tonic-gate 			sub_usage(SHELP_BOOT, CMD_BOOT);
12530Sstevel@tonic-gate 			return (optopt == '?' ? Z_OK : Z_USAGE);
12540Sstevel@tonic-gate 		case 's':
12550Sstevel@tonic-gate 			(void) strlcpy(zarg.bootbuf, "-s",
12560Sstevel@tonic-gate 			    sizeof (zarg.bootbuf));
12570Sstevel@tonic-gate 			break;
12582712Snn35248 		case 'f':
12592712Snn35248 			force = B_TRUE;
12602712Snn35248 			break;
12610Sstevel@tonic-gate 		default:
12620Sstevel@tonic-gate 			sub_usage(SHELP_BOOT, CMD_BOOT);
12630Sstevel@tonic-gate 			return (Z_USAGE);
12640Sstevel@tonic-gate 		}
12650Sstevel@tonic-gate 	}
12662267Sdp 
12672267Sdp 	for (; optind < argc; optind++) {
12682267Sdp 		if (strlcat(zarg.bootbuf, argv[optind],
12692267Sdp 		    sizeof (zarg.bootbuf)) >= sizeof (zarg.bootbuf)) {
12702267Sdp 			zerror(gettext("Boot argument list too long"));
12712267Sdp 			return (Z_ERR);
12722267Sdp 		}
12732267Sdp 		if (optind < argc - 1)
12742267Sdp 			if (strlcat(zarg.bootbuf, " ", sizeof (zarg.bootbuf)) >=
12752267Sdp 			    sizeof (zarg.bootbuf)) {
12762267Sdp 				zerror(gettext("Boot argument list too long"));
12772267Sdp 				return (Z_ERR);
12782267Sdp 			}
12792267Sdp 	}
12802712Snn35248 	if (sanity_check(target_zone, CMD_BOOT, B_FALSE, B_FALSE, force)
12812712Snn35248 	    != Z_OK)
12820Sstevel@tonic-gate 		return (Z_ERR);
12833339Szt129084 	if (verify_details(CMD_BOOT, argv) != Z_OK)
12840Sstevel@tonic-gate 		return (Z_ERR);
12852712Snn35248 	zarg.cmd = force ? Z_FORCEBOOT : Z_BOOT;
12867089Sgjelinek 	if (zonecfg_call_zoneadmd(target_zone, &zarg, locale, B_TRUE) != 0) {
12870Sstevel@tonic-gate 		zerror(gettext("call to %s failed"), "zoneadmd");
12880Sstevel@tonic-gate 		return (Z_ERR);
12890Sstevel@tonic-gate 	}
12903247Sgjelinek 
12910Sstevel@tonic-gate 	return (Z_OK);
12920Sstevel@tonic-gate }
12930Sstevel@tonic-gate 
12940Sstevel@tonic-gate static void
12950Sstevel@tonic-gate fake_up_local_zone(zoneid_t zid, zone_entry_t *zeptr)
12960Sstevel@tonic-gate {
12970Sstevel@tonic-gate 	ssize_t result;
12982303Scarlsonj 	uuid_t uuid;
12992303Scarlsonj 	FILE *fp;
13003448Sdh155122 	ushort_t flags;
13012303Scarlsonj 
13022303Scarlsonj 	(void) memset(zeptr, 0, sizeof (*zeptr));
13030Sstevel@tonic-gate 
13040Sstevel@tonic-gate 	zeptr->zid = zid;
13052303Scarlsonj 
13060Sstevel@tonic-gate 	/*
13070Sstevel@tonic-gate 	 * Since we're looking up our own (non-global) zone name,
13080Sstevel@tonic-gate 	 * we can be assured that it will succeed.
13090Sstevel@tonic-gate 	 */
13100Sstevel@tonic-gate 	result = getzonenamebyid(zid, zeptr->zname, sizeof (zeptr->zname));
13110Sstevel@tonic-gate 	assert(result >= 0);
13122303Scarlsonj 	if (zonecfg_is_scratch(zeptr->zname) &&
13132303Scarlsonj 	    (fp = zonecfg_open_scratch("", B_FALSE)) != NULL) {
13142303Scarlsonj 		(void) zonecfg_reverse_scratch(fp, zeptr->zname, zeptr->zname,
13152303Scarlsonj 		    sizeof (zeptr->zname), NULL, 0);
13162303Scarlsonj 		zonecfg_close_scratch(fp);
13172303Scarlsonj 	}
13182303Scarlsonj 
13192303Scarlsonj 	if (is_system_labeled()) {
13201676Sjpk 		(void) zone_getattr(zid, ZONE_ATTR_ROOT, zeptr->zroot,
13211676Sjpk 		    sizeof (zeptr->zroot));
13222712Snn35248 		(void) strlcpy(zeptr->zbrand, NATIVE_BRAND_NAME,
13234350Std153743 		    sizeof (zeptr->zbrand));
13242303Scarlsonj 	} else {
13252303Scarlsonj 		(void) strlcpy(zeptr->zroot, "/", sizeof (zeptr->zroot));
13262712Snn35248 		(void) zone_getattr(zid, ZONE_ATTR_BRAND, zeptr->zbrand,
13272712Snn35248 		    sizeof (zeptr->zbrand));
13282303Scarlsonj 	}
13292303Scarlsonj 
13300Sstevel@tonic-gate 	zeptr->zstate_str = "running";
13312303Scarlsonj 	if (zonecfg_get_uuid(zeptr->zname, uuid) == Z_OK &&
13322303Scarlsonj 	    !uuid_is_null(uuid))
13332303Scarlsonj 		uuid_unparse(uuid, zeptr->zuuid);
13343448Sdh155122 
13353448Sdh155122 	if (zone_getattr(zid, ZONE_ATTR_FLAGS, &flags, sizeof (flags)) < 0) {
13363448Sdh155122 		zperror2(zeptr->zname, gettext("could not get zone flags"));
13373448Sdh155122 		exit(Z_ERR);
13383448Sdh155122 	}
13393448Sdh155122 	if (flags & ZF_NET_EXCL)
13403448Sdh155122 		zeptr->ziptype = ZS_EXCLUSIVE;
13413448Sdh155122 	else
13423448Sdh155122 		zeptr->ziptype = ZS_SHARED;
13430Sstevel@tonic-gate }
13440Sstevel@tonic-gate 
13450Sstevel@tonic-gate static int
13460Sstevel@tonic-gate list_func(int argc, char *argv[])
13470Sstevel@tonic-gate {
13480Sstevel@tonic-gate 	zone_entry_t *zentp, zent;
1349766Scarlsonj 	int arg, retv;
13500Sstevel@tonic-gate 	boolean_t output = B_FALSE, verbose = B_FALSE, parsable = B_FALSE;
13510Sstevel@tonic-gate 	zone_state_t min_state = ZONE_STATE_RUNNING;
13520Sstevel@tonic-gate 	zoneid_t zone_id = getzoneid();
13530Sstevel@tonic-gate 
13540Sstevel@tonic-gate 	if (target_zone == NULL) {
13550Sstevel@tonic-gate 		/* all zones: default view to running but allow override */
13560Sstevel@tonic-gate 		optind = 0;
13570Sstevel@tonic-gate 		while ((arg = getopt(argc, argv, "?cipv")) != EOF) {
13580Sstevel@tonic-gate 			switch (arg) {
13590Sstevel@tonic-gate 			case '?':
13600Sstevel@tonic-gate 				sub_usage(SHELP_LIST, CMD_LIST);
13610Sstevel@tonic-gate 				return (optopt == '?' ? Z_OK : Z_USAGE);
13621339Sjonb 				/*
13631339Sjonb 				 * The 'i' and 'c' options are not mutually
13641339Sjonb 				 * exclusive so if 'c' is given, then min_state
13651339Sjonb 				 * is set to 0 (ZONE_STATE_CONFIGURED) which is
13661339Sjonb 				 * the lowest possible state.  If 'i' is given,
13671339Sjonb 				 * then min_state is set to be the lowest state
13681339Sjonb 				 * so far.
13691339Sjonb 				 */
13700Sstevel@tonic-gate 			case 'c':
13710Sstevel@tonic-gate 				min_state = ZONE_STATE_CONFIGURED;
13720Sstevel@tonic-gate 				break;
13730Sstevel@tonic-gate 			case 'i':
13741339Sjonb 				min_state = min(ZONE_STATE_INSTALLED,
13751339Sjonb 				    min_state);
13761339Sjonb 
13770Sstevel@tonic-gate 				break;
13780Sstevel@tonic-gate 			case 'p':
13790Sstevel@tonic-gate 				parsable = B_TRUE;
13800Sstevel@tonic-gate 				break;
13810Sstevel@tonic-gate 			case 'v':
13820Sstevel@tonic-gate 				verbose = B_TRUE;
13830Sstevel@tonic-gate 				break;
13840Sstevel@tonic-gate 			default:
13850Sstevel@tonic-gate 				sub_usage(SHELP_LIST, CMD_LIST);
13860Sstevel@tonic-gate 				return (Z_USAGE);
13870Sstevel@tonic-gate 			}
13880Sstevel@tonic-gate 		}
13890Sstevel@tonic-gate 		if (parsable && verbose) {
13900Sstevel@tonic-gate 			zerror(gettext("%s -p and -v are mutually exclusive."),
13910Sstevel@tonic-gate 			    cmd_to_str(CMD_LIST));
13920Sstevel@tonic-gate 			return (Z_ERR);
13930Sstevel@tonic-gate 		}
13941676Sjpk 		if (zone_id == GLOBAL_ZONEID || is_system_labeled()) {
1395766Scarlsonj 			retv = zone_print_list(min_state, verbose, parsable);
13960Sstevel@tonic-gate 		} else {
13972712Snn35248 			fake_up_local_zone(zone_id, &zent);
1398766Scarlsonj 			retv = Z_OK;
13990Sstevel@tonic-gate 			zone_print(&zent, verbose, parsable);
14000Sstevel@tonic-gate 		}
1401766Scarlsonj 		return (retv);
14020Sstevel@tonic-gate 	}
14030Sstevel@tonic-gate 
14040Sstevel@tonic-gate 	/*
14050Sstevel@tonic-gate 	 * Specific target zone: disallow -i/-c suboptions.
14060Sstevel@tonic-gate 	 */
14070Sstevel@tonic-gate 	optind = 0;
14080Sstevel@tonic-gate 	while ((arg = getopt(argc, argv, "?pv")) != EOF) {
14090Sstevel@tonic-gate 		switch (arg) {
14100Sstevel@tonic-gate 		case '?':
14110Sstevel@tonic-gate 			sub_usage(SHELP_LIST, CMD_LIST);
14120Sstevel@tonic-gate 			return (optopt == '?' ? Z_OK : Z_USAGE);
14130Sstevel@tonic-gate 		case 'p':
14140Sstevel@tonic-gate 			parsable = B_TRUE;
14150Sstevel@tonic-gate 			break;
14160Sstevel@tonic-gate 		case 'v':
14170Sstevel@tonic-gate 			verbose = B_TRUE;
14180Sstevel@tonic-gate 			break;
14190Sstevel@tonic-gate 		default:
14200Sstevel@tonic-gate 			sub_usage(SHELP_LIST, CMD_LIST);
14210Sstevel@tonic-gate 			return (Z_USAGE);
14220Sstevel@tonic-gate 		}
14230Sstevel@tonic-gate 	}
14240Sstevel@tonic-gate 	if (parsable && verbose) {
14250Sstevel@tonic-gate 		zerror(gettext("%s -p and -v are mutually exclusive."),
14260Sstevel@tonic-gate 		    cmd_to_str(CMD_LIST));
14270Sstevel@tonic-gate 		return (Z_ERR);
14280Sstevel@tonic-gate 	}
14290Sstevel@tonic-gate 	if (argc > optind) {
14300Sstevel@tonic-gate 		sub_usage(SHELP_LIST, CMD_LIST);
14310Sstevel@tonic-gate 		return (Z_USAGE);
14320Sstevel@tonic-gate 	}
14333979Sgjelinek 	if (zone_id != GLOBAL_ZONEID && !is_system_labeled()) {
14340Sstevel@tonic-gate 		fake_up_local_zone(zone_id, &zent);
14350Sstevel@tonic-gate 		/*
14360Sstevel@tonic-gate 		 * main() will issue a Z_NO_ZONE error if it cannot get an
14370Sstevel@tonic-gate 		 * id for target_zone, which in a non-global zone should
14380Sstevel@tonic-gate 		 * happen for any zone name except `zonename`.  Thus we
14390Sstevel@tonic-gate 		 * assert() that here but don't otherwise check.
14400Sstevel@tonic-gate 		 */
14410Sstevel@tonic-gate 		assert(strcmp(zent.zname, target_zone) == 0);
14420Sstevel@tonic-gate 		zone_print(&zent, verbose, parsable);
14430Sstevel@tonic-gate 		output = B_TRUE;
14440Sstevel@tonic-gate 	} else if ((zentp = lookup_running_zone(target_zone)) != NULL) {
14450Sstevel@tonic-gate 		zone_print(zentp, verbose, parsable);
14460Sstevel@tonic-gate 		output = B_TRUE;
1447766Scarlsonj 	} else if (lookup_zone_info(target_zone, ZONE_ID_UNDEFINED,
1448766Scarlsonj 	    &zent) == Z_OK) {
14490Sstevel@tonic-gate 		zone_print(&zent, verbose, parsable);
14500Sstevel@tonic-gate 		output = B_TRUE;
14510Sstevel@tonic-gate 	}
14523339Szt129084 
14533339Szt129084 	/*
14543339Szt129084 	 * Invoke brand-specific handler. Note that we do this
14553542Sgjelinek 	 * only if we're in the global zone, and target_zone is specified
14563542Sgjelinek 	 * and it is not the global zone.
14573339Szt129084 	 */
14583542Sgjelinek 	if (zone_id == GLOBAL_ZONEID && target_zone != NULL &&
14593542Sgjelinek 	    strcmp(target_zone, GLOBAL_ZONENAME) != 0)
14603339Szt129084 		if (invoke_brand_handler(CMD_LIST, argv) != Z_OK)
14613339Szt129084 			return (Z_ERR);
14623339Szt129084 
14630Sstevel@tonic-gate 	return (output ? Z_OK : Z_ERR);
14640Sstevel@tonic-gate }
14650Sstevel@tonic-gate 
14660Sstevel@tonic-gate static void
14670Sstevel@tonic-gate sigterm(int sig)
14680Sstevel@tonic-gate {
14690Sstevel@tonic-gate 	/*
14700Sstevel@tonic-gate 	 * Ignore SIG{INT,TERM}, so we don't end up in an infinite loop,
14710Sstevel@tonic-gate 	 * then propagate the signal to our process group.
14720Sstevel@tonic-gate 	 */
14732712Snn35248 	assert(sig == SIGINT || sig == SIGTERM);
14740Sstevel@tonic-gate 	(void) sigset(SIGINT, SIG_IGN);
14750Sstevel@tonic-gate 	(void) sigset(SIGTERM, SIG_IGN);
14760Sstevel@tonic-gate 	(void) kill(0, sig);
14770Sstevel@tonic-gate 	child_killed = B_TRUE;
14780Sstevel@tonic-gate }
14790Sstevel@tonic-gate 
14800Sstevel@tonic-gate static int
14810Sstevel@tonic-gate do_subproc(char *cmdbuf)
14820Sstevel@tonic-gate {
14830Sstevel@tonic-gate 	char inbuf[1024];	/* arbitrary large amount */
14840Sstevel@tonic-gate 	FILE *file;
14850Sstevel@tonic-gate 
14862712Snn35248 	do_subproc_cnt++;
14870Sstevel@tonic-gate 	child_killed = B_FALSE;
14880Sstevel@tonic-gate 	/*
14890Sstevel@tonic-gate 	 * We use popen(3c) to launch child processes for [un]install;
14900Sstevel@tonic-gate 	 * this library call does not return a PID, so we have to kill
14910Sstevel@tonic-gate 	 * the whole process group.  To avoid killing our parent, we
14920Sstevel@tonic-gate 	 * become a process group leader here.  But doing so can wreak
14930Sstevel@tonic-gate 	 * havoc with reading from stdin when launched by a non-job-control
14940Sstevel@tonic-gate 	 * shell, so we close stdin and reopen it as /dev/null first.
14950Sstevel@tonic-gate 	 */
14960Sstevel@tonic-gate 	(void) close(STDIN_FILENO);
14972712Snn35248 	(void) openat(STDIN_FILENO, "/dev/null", O_RDONLY);
14982712Snn35248 	if (!zoneadm_is_nested)
14992712Snn35248 		(void) setpgid(0, 0);
15000Sstevel@tonic-gate 	(void) sigset(SIGINT, sigterm);
15010Sstevel@tonic-gate 	(void) sigset(SIGTERM, sigterm);
15020Sstevel@tonic-gate 	file = popen(cmdbuf, "r");
15030Sstevel@tonic-gate 	for (;;) {
15040Sstevel@tonic-gate 		if (child_killed || fgets(inbuf, sizeof (inbuf), file) == NULL)
15050Sstevel@tonic-gate 			break;
15060Sstevel@tonic-gate 		(void) fputs(inbuf, stdout);
15070Sstevel@tonic-gate 	}
15080Sstevel@tonic-gate 	(void) sigset(SIGINT, SIG_DFL);
15090Sstevel@tonic-gate 	(void) sigset(SIGTERM, SIG_DFL);
15100Sstevel@tonic-gate 	return (pclose(file));
15110Sstevel@tonic-gate }
15120Sstevel@tonic-gate 
15137089Sgjelinek int
15142712Snn35248 do_subproc_interactive(char *cmdbuf)
15152712Snn35248 {
15162712Snn35248 	void (*saveint)(int);
15172712Snn35248 	void (*saveterm)(int);
15182712Snn35248 	void (*savequit)(int);
15192712Snn35248 	void (*savehup)(int);
15202712Snn35248 	int pid, child, status;
15212712Snn35248 
15222712Snn35248 	/*
15232712Snn35248 	 * do_subproc() links stdin to /dev/null, which would break any
15242712Snn35248 	 * interactive subprocess we try to launch here.  Similarly, we
15252712Snn35248 	 * can't have been launched as a subprocess ourselves.
15262712Snn35248 	 */
15272712Snn35248 	assert(do_subproc_cnt == 0 && !zoneadm_is_nested);
15282712Snn35248 
15292712Snn35248 	if ((child = vfork()) == 0) {
15302712Snn35248 		(void) execl("/bin/sh", "sh", "-c", cmdbuf, (char *)NULL);
15312712Snn35248 	}
15322712Snn35248 
15332712Snn35248 	if (child == -1)
15342712Snn35248 		return (-1);
15352712Snn35248 
15362712Snn35248 	saveint = sigset(SIGINT, SIG_IGN);
15372712Snn35248 	saveterm = sigset(SIGTERM, SIG_IGN);
15382712Snn35248 	savequit = sigset(SIGQUIT, SIG_IGN);
15392712Snn35248 	savehup = sigset(SIGHUP, SIG_IGN);
15402712Snn35248 
15412712Snn35248 	while ((pid = waitpid(child, &status, 0)) != child && pid != -1)
15422712Snn35248 		;
15432712Snn35248 
15442712Snn35248 	(void) sigset(SIGINT, saveint);
15452712Snn35248 	(void) sigset(SIGTERM, saveterm);
15462712Snn35248 	(void) sigset(SIGQUIT, savequit);
15472712Snn35248 	(void) sigset(SIGHUP, savehup);
15482712Snn35248 
15492712Snn35248 	return (pid == -1 ? -1 : status);
15502712Snn35248 }
15512712Snn35248 
15527089Sgjelinek int
15532712Snn35248 subproc_status(const char *cmd, int status, boolean_t verbose_failure)
15540Sstevel@tonic-gate {
15550Sstevel@tonic-gate 	if (WIFEXITED(status)) {
15560Sstevel@tonic-gate 		int exit_code = WEXITSTATUS(status);
15570Sstevel@tonic-gate 
15582712Snn35248 		if ((verbose_failure) && (exit_code != ZONE_SUBPROC_OK))
15592712Snn35248 			zerror(gettext("'%s' failed with exit code %d."), cmd,
15602712Snn35248 			    exit_code);
15612712Snn35248 
15622712Snn35248 		return (exit_code);
15630Sstevel@tonic-gate 	} else if (WIFSIGNALED(status)) {
15640Sstevel@tonic-gate 		int signal = WTERMSIG(status);
15650Sstevel@tonic-gate 		char sigstr[SIG2STR_MAX];
15660Sstevel@tonic-gate 
15670Sstevel@tonic-gate 		if (sig2str(signal, sigstr) == 0) {
15680Sstevel@tonic-gate 			zerror(gettext("'%s' terminated by signal SIG%s."), cmd,
15690Sstevel@tonic-gate 			    sigstr);
15700Sstevel@tonic-gate 		} else {
15710Sstevel@tonic-gate 			zerror(gettext("'%s' terminated by an unknown signal."),
15720Sstevel@tonic-gate 			    cmd);
15730Sstevel@tonic-gate 		}
15740Sstevel@tonic-gate 	} else {
15750Sstevel@tonic-gate 		zerror(gettext("'%s' failed for unknown reasons."), cmd);
15760Sstevel@tonic-gate 	}
15772712Snn35248 
15782712Snn35248 	/*
15792712Snn35248 	 * Assume a subprocess that died due to a signal or an unknown error
15802712Snn35248 	 * should be considered an exit code of ZONE_SUBPROC_FATAL, as the
15812712Snn35248 	 * user will likely need to do some manual cleanup.
15822712Snn35248 	 */
15832712Snn35248 	return (ZONE_SUBPROC_FATAL);
15840Sstevel@tonic-gate }
15850Sstevel@tonic-gate 
15860Sstevel@tonic-gate /*
15870Sstevel@tonic-gate  * Various sanity checks; make sure:
15880Sstevel@tonic-gate  * 1. We're in the global zone.
15890Sstevel@tonic-gate  * 2. The calling user has sufficient privilege.
15900Sstevel@tonic-gate  * 3. The target zone is neither the global zone nor anything starting with
15910Sstevel@tonic-gate  *    "SUNW".
15920Sstevel@tonic-gate  * 4a. If we're looking for a 'not running' (i.e., configured or installed)
15930Sstevel@tonic-gate  *     zone, the name service knows about it.
15940Sstevel@tonic-gate  * 4b. For some operations which expect a zone not to be running, that it is
15950Sstevel@tonic-gate  *     not already running (or ready).
15960Sstevel@tonic-gate  */
15970Sstevel@tonic-gate static int
15980Sstevel@tonic-gate sanity_check(char *zone, int cmd_num, boolean_t running,
15992712Snn35248     boolean_t unsafe_when_running, boolean_t force)
16000Sstevel@tonic-gate {
16010Sstevel@tonic-gate 	zone_entry_t *zent;
16020Sstevel@tonic-gate 	priv_set_t *privset;
16032712Snn35248 	zone_state_t state, min_state;
1604766Scarlsonj 	char kernzone[ZONENAME_MAX];
1605766Scarlsonj 	FILE *fp;
16060Sstevel@tonic-gate 
16070Sstevel@tonic-gate 	if (getzoneid() != GLOBAL_ZONEID) {
16081645Scomay 		switch (cmd_num) {
16091645Scomay 		case CMD_HALT:
16101645Scomay 			zerror(gettext("use %s to %s this zone."), "halt(1M)",
16111645Scomay 			    cmd_to_str(cmd_num));
16121645Scomay 			break;
16131645Scomay 		case CMD_REBOOT:
16141645Scomay 			zerror(gettext("use %s to %s this zone."),
16151645Scomay 			    "reboot(1M)", cmd_to_str(cmd_num));
16161645Scomay 			break;
16171645Scomay 		default:
16181645Scomay 			zerror(gettext("must be in the global zone to %s a "
16191645Scomay 			    "zone."), cmd_to_str(cmd_num));
16201645Scomay 			break;
16211645Scomay 		}
16220Sstevel@tonic-gate 		return (Z_ERR);
16230Sstevel@tonic-gate 	}
16240Sstevel@tonic-gate 
16250Sstevel@tonic-gate 	if ((privset = priv_allocset()) == NULL) {
16260Sstevel@tonic-gate 		zerror(gettext("%s failed"), "priv_allocset");
16270Sstevel@tonic-gate 		return (Z_ERR);
16280Sstevel@tonic-gate 	}
16290Sstevel@tonic-gate 
16300Sstevel@tonic-gate 	if (getppriv(PRIV_EFFECTIVE, privset) != 0) {
16310Sstevel@tonic-gate 		zerror(gettext("%s failed"), "getppriv");
16320Sstevel@tonic-gate 		priv_freeset(privset);
16330Sstevel@tonic-gate 		return (Z_ERR);
16340Sstevel@tonic-gate 	}
16350Sstevel@tonic-gate 
16360Sstevel@tonic-gate 	if (priv_isfullset(privset) == B_FALSE) {
16370Sstevel@tonic-gate 		zerror(gettext("only a privileged user may %s a zone."),
16380Sstevel@tonic-gate 		    cmd_to_str(cmd_num));
16390Sstevel@tonic-gate 		priv_freeset(privset);
16400Sstevel@tonic-gate 		return (Z_ERR);
16410Sstevel@tonic-gate 	}
16420Sstevel@tonic-gate 	priv_freeset(privset);
16430Sstevel@tonic-gate 
16440Sstevel@tonic-gate 	if (zone == NULL) {
16450Sstevel@tonic-gate 		zerror(gettext("no zone specified"));
16460Sstevel@tonic-gate 		return (Z_ERR);
16470Sstevel@tonic-gate 	}
16480Sstevel@tonic-gate 
16490Sstevel@tonic-gate 	if (strcmp(zone, GLOBAL_ZONENAME) == 0) {
16500Sstevel@tonic-gate 		zerror(gettext("%s operation is invalid for the global zone."),
16510Sstevel@tonic-gate 		    cmd_to_str(cmd_num));
16520Sstevel@tonic-gate 		return (Z_ERR);
16530Sstevel@tonic-gate 	}
16540Sstevel@tonic-gate 
16550Sstevel@tonic-gate 	if (strncmp(zone, "SUNW", 4) == 0) {
16560Sstevel@tonic-gate 		zerror(gettext("%s operation is invalid for zones starting "
16570Sstevel@tonic-gate 		    "with SUNW."), cmd_to_str(cmd_num));
16580Sstevel@tonic-gate 		return (Z_ERR);
16590Sstevel@tonic-gate 	}
16600Sstevel@tonic-gate 
1661766Scarlsonj 	if (!zonecfg_in_alt_root()) {
1662766Scarlsonj 		zent = lookup_running_zone(zone);
1663766Scarlsonj 	} else if ((fp = zonecfg_open_scratch("", B_FALSE)) == NULL) {
1664766Scarlsonj 		zent = NULL;
1665766Scarlsonj 	} else {
1666766Scarlsonj 		if (zonecfg_find_scratch(fp, zone, zonecfg_get_root(),
1667766Scarlsonj 		    kernzone, sizeof (kernzone)) == 0)
1668766Scarlsonj 			zent = lookup_running_zone(kernzone);
1669766Scarlsonj 		else
1670766Scarlsonj 			zent = NULL;
1671766Scarlsonj 		zonecfg_close_scratch(fp);
1672766Scarlsonj 	}
1673766Scarlsonj 
16740Sstevel@tonic-gate 	/*
16750Sstevel@tonic-gate 	 * Look up from the kernel for 'running' zones.
16760Sstevel@tonic-gate 	 */
16772712Snn35248 	if (running && !force) {
16780Sstevel@tonic-gate 		if (zent == NULL) {
16790Sstevel@tonic-gate 			zerror(gettext("not running"));
16800Sstevel@tonic-gate 			return (Z_ERR);
16810Sstevel@tonic-gate 		}
16820Sstevel@tonic-gate 	} else {
16830Sstevel@tonic-gate 		int err;
16840Sstevel@tonic-gate 
16850Sstevel@tonic-gate 		if (unsafe_when_running && zent != NULL) {
16860Sstevel@tonic-gate 			/* check whether the zone is ready or running */
16870Sstevel@tonic-gate 			if ((err = zone_get_state(zent->zname,
16880Sstevel@tonic-gate 			    &zent->zstate_num)) != Z_OK) {
16890Sstevel@tonic-gate 				errno = err;
16900Sstevel@tonic-gate 				zperror2(zent->zname,
16910Sstevel@tonic-gate 				    gettext("could not get state"));
16920Sstevel@tonic-gate 				/* can't tell, so hedge */
16930Sstevel@tonic-gate 				zent->zstate_str = "ready/running";
16940Sstevel@tonic-gate 			} else {
16950Sstevel@tonic-gate 				zent->zstate_str =
16960Sstevel@tonic-gate 				    zone_state_str(zent->zstate_num);
16970Sstevel@tonic-gate 			}
16980Sstevel@tonic-gate 			zerror(gettext("%s operation is invalid for %s zones."),
16990Sstevel@tonic-gate 			    cmd_to_str(cmd_num), zent->zstate_str);
17000Sstevel@tonic-gate 			return (Z_ERR);
17010Sstevel@tonic-gate 		}
17020Sstevel@tonic-gate 		if ((err = zone_get_state(zone, &state)) != Z_OK) {
17030Sstevel@tonic-gate 			errno = err;
17040Sstevel@tonic-gate 			zperror2(zone, gettext("could not get state"));
17050Sstevel@tonic-gate 			return (Z_ERR);
17060Sstevel@tonic-gate 		}
17070Sstevel@tonic-gate 		switch (cmd_num) {
17080Sstevel@tonic-gate 		case CMD_UNINSTALL:
17090Sstevel@tonic-gate 			if (state == ZONE_STATE_CONFIGURED) {
17100Sstevel@tonic-gate 				zerror(gettext("is already in state '%s'."),
17110Sstevel@tonic-gate 				    zone_state_str(ZONE_STATE_CONFIGURED));
17120Sstevel@tonic-gate 				return (Z_ERR);
17130Sstevel@tonic-gate 			}
17140Sstevel@tonic-gate 			break;
17151507Sgjelinek 		case CMD_ATTACH:
17161300Sgjelinek 		case CMD_CLONE:
17170Sstevel@tonic-gate 		case CMD_INSTALL:
17180Sstevel@tonic-gate 			if (state == ZONE_STATE_INSTALLED) {
17190Sstevel@tonic-gate 				zerror(gettext("is already %s."),
17200Sstevel@tonic-gate 				    zone_state_str(ZONE_STATE_INSTALLED));
17210Sstevel@tonic-gate 				return (Z_ERR);
17220Sstevel@tonic-gate 			} else if (state == ZONE_STATE_INCOMPLETE) {
17230Sstevel@tonic-gate 				zerror(gettext("zone is %s; %s required."),
17240Sstevel@tonic-gate 				    zone_state_str(ZONE_STATE_INCOMPLETE),
17250Sstevel@tonic-gate 				    cmd_to_str(CMD_UNINSTALL));
17260Sstevel@tonic-gate 				return (Z_ERR);
17270Sstevel@tonic-gate 			}
17280Sstevel@tonic-gate 			break;
17291507Sgjelinek 		case CMD_DETACH:
17301300Sgjelinek 		case CMD_MOVE:
17310Sstevel@tonic-gate 		case CMD_READY:
17320Sstevel@tonic-gate 		case CMD_BOOT:
1733766Scarlsonj 		case CMD_MOUNT:
17342303Scarlsonj 		case CMD_MARK:
17352712Snn35248 			if ((cmd_num == CMD_BOOT || cmd_num == CMD_MOUNT) &&
17362712Snn35248 			    force)
17372712Snn35248 				min_state = ZONE_STATE_INCOMPLETE;
17382712Snn35248 			else
17392712Snn35248 				min_state = ZONE_STATE_INSTALLED;
17402712Snn35248 
17412712Snn35248 			if (force && cmd_num == CMD_BOOT && is_native_zone) {
17422712Snn35248 				zerror(gettext("Only branded zones may be "
17432712Snn35248 				    "force-booted."));
17442712Snn35248 				return (Z_ERR);
17452712Snn35248 			}
17462712Snn35248 
17472712Snn35248 			if (state < min_state) {
17480Sstevel@tonic-gate 				zerror(gettext("must be %s before %s."),
17492712Snn35248 				    zone_state_str(min_state),
17500Sstevel@tonic-gate 				    cmd_to_str(cmd_num));
17510Sstevel@tonic-gate 				return (Z_ERR);
17520Sstevel@tonic-gate 			}
17530Sstevel@tonic-gate 			break;
17540Sstevel@tonic-gate 		case CMD_VERIFY:
17550Sstevel@tonic-gate 			if (state == ZONE_STATE_INCOMPLETE) {
17560Sstevel@tonic-gate 				zerror(gettext("zone is %s; %s required."),
17570Sstevel@tonic-gate 				    zone_state_str(ZONE_STATE_INCOMPLETE),
17580Sstevel@tonic-gate 				    cmd_to_str(CMD_UNINSTALL));
17590Sstevel@tonic-gate 				return (Z_ERR);
17600Sstevel@tonic-gate 			}
17610Sstevel@tonic-gate 			break;
1762766Scarlsonj 		case CMD_UNMOUNT:
1763766Scarlsonj 			if (state != ZONE_STATE_MOUNTED) {
1764766Scarlsonj 				zerror(gettext("must be %s before %s."),
1765766Scarlsonj 				    zone_state_str(ZONE_STATE_MOUNTED),
1766766Scarlsonj 				    cmd_to_str(cmd_num));
1767766Scarlsonj 				return (Z_ERR);
1768766Scarlsonj 			}
1769766Scarlsonj 			break;
17700Sstevel@tonic-gate 		}
17710Sstevel@tonic-gate 	}
17720Sstevel@tonic-gate 	return (Z_OK);
17730Sstevel@tonic-gate }
17740Sstevel@tonic-gate 
17750Sstevel@tonic-gate static int
17760Sstevel@tonic-gate halt_func(int argc, char *argv[])
17770Sstevel@tonic-gate {
17780Sstevel@tonic-gate 	zone_cmd_arg_t zarg;
17790Sstevel@tonic-gate 	int arg;
17800Sstevel@tonic-gate 
1781766Scarlsonj 	if (zonecfg_in_alt_root()) {
1782766Scarlsonj 		zerror(gettext("cannot halt zone in alternate root"));
1783766Scarlsonj 		return (Z_ERR);
1784766Scarlsonj 	}
1785766Scarlsonj 
17860Sstevel@tonic-gate 	optind = 0;
17870Sstevel@tonic-gate 	if ((arg = getopt(argc, argv, "?")) != EOF) {
17880Sstevel@tonic-gate 		switch (arg) {
17890Sstevel@tonic-gate 		case '?':
17900Sstevel@tonic-gate 			sub_usage(SHELP_HALT, CMD_HALT);
17910Sstevel@tonic-gate 			return (optopt == '?' ? Z_OK : Z_USAGE);
17920Sstevel@tonic-gate 		default:
17930Sstevel@tonic-gate 			sub_usage(SHELP_HALT, CMD_HALT);
17940Sstevel@tonic-gate 			return (Z_USAGE);
17950Sstevel@tonic-gate 		}
17960Sstevel@tonic-gate 	}
17970Sstevel@tonic-gate 	if (argc > optind) {
17980Sstevel@tonic-gate 		sub_usage(SHELP_HALT, CMD_HALT);
17990Sstevel@tonic-gate 		return (Z_USAGE);
18000Sstevel@tonic-gate 	}
18010Sstevel@tonic-gate 	/*
18020Sstevel@tonic-gate 	 * zoneadmd should be the one to decide whether or not to proceed,
18030Sstevel@tonic-gate 	 * so even though it seems that the fourth parameter below should
18040Sstevel@tonic-gate 	 * perhaps be B_TRUE, it really shouldn't be.
18050Sstevel@tonic-gate 	 */
18062712Snn35248 	if (sanity_check(target_zone, CMD_HALT, B_FALSE, B_FALSE, B_FALSE)
18072712Snn35248 	    != Z_OK)
18080Sstevel@tonic-gate 		return (Z_ERR);
18090Sstevel@tonic-gate 
18103339Szt129084 	/*
18113339Szt129084 	 * Invoke brand-specific handler.
18123339Szt129084 	 */
18133339Szt129084 	if (invoke_brand_handler(CMD_HALT, argv) != Z_OK)
18143339Szt129084 		return (Z_ERR);
18153339Szt129084 
18160Sstevel@tonic-gate 	zarg.cmd = Z_HALT;
18177089Sgjelinek 	return ((zonecfg_call_zoneadmd(target_zone, &zarg, locale,
18187089Sgjelinek 	    B_TRUE) == 0) ?  Z_OK : Z_ERR);
18190Sstevel@tonic-gate }
18200Sstevel@tonic-gate 
18210Sstevel@tonic-gate static int
18220Sstevel@tonic-gate reboot_func(int argc, char *argv[])
18230Sstevel@tonic-gate {
18240Sstevel@tonic-gate 	zone_cmd_arg_t zarg;
18250Sstevel@tonic-gate 	int arg;
18260Sstevel@tonic-gate 
1827766Scarlsonj 	if (zonecfg_in_alt_root()) {
1828766Scarlsonj 		zerror(gettext("cannot reboot zone in alternate root"));
1829766Scarlsonj 		return (Z_ERR);
1830766Scarlsonj 	}
1831766Scarlsonj 
18320Sstevel@tonic-gate 	optind = 0;
18330Sstevel@tonic-gate 	if ((arg = getopt(argc, argv, "?")) != EOF) {
18340Sstevel@tonic-gate 		switch (arg) {
18350Sstevel@tonic-gate 		case '?':
18360Sstevel@tonic-gate 			sub_usage(SHELP_REBOOT, CMD_REBOOT);
18370Sstevel@tonic-gate 			return (optopt == '?' ? Z_OK : Z_USAGE);
18380Sstevel@tonic-gate 		default:
18390Sstevel@tonic-gate 			sub_usage(SHELP_REBOOT, CMD_REBOOT);
18400Sstevel@tonic-gate 			return (Z_USAGE);
18410Sstevel@tonic-gate 		}
18420Sstevel@tonic-gate 	}
18432267Sdp 
18442267Sdp 	zarg.bootbuf[0] = '\0';
18452267Sdp 	for (; optind < argc; optind++) {
18462267Sdp 		if (strlcat(zarg.bootbuf, argv[optind],
18472267Sdp 		    sizeof (zarg.bootbuf)) >= sizeof (zarg.bootbuf)) {
18482267Sdp 			zerror(gettext("Boot argument list too long"));
18492267Sdp 			return (Z_ERR);
18502267Sdp 		}
18512267Sdp 		if (optind < argc - 1)
18522267Sdp 			if (strlcat(zarg.bootbuf, " ", sizeof (zarg.bootbuf)) >=
18532267Sdp 			    sizeof (zarg.bootbuf)) {
18542267Sdp 				zerror(gettext("Boot argument list too long"));
18552267Sdp 				return (Z_ERR);
18562267Sdp 			}
18572267Sdp 	}
18582267Sdp 
18592267Sdp 
18600Sstevel@tonic-gate 	/*
18610Sstevel@tonic-gate 	 * zoneadmd should be the one to decide whether or not to proceed,
18620Sstevel@tonic-gate 	 * so even though it seems that the fourth parameter below should
18630Sstevel@tonic-gate 	 * perhaps be B_TRUE, it really shouldn't be.
18640Sstevel@tonic-gate 	 */
18652712Snn35248 	if (sanity_check(target_zone, CMD_REBOOT, B_TRUE, B_FALSE, B_FALSE)
18662712Snn35248 	    != Z_OK)
18670Sstevel@tonic-gate 		return (Z_ERR);
18683339Szt129084 	if (verify_details(CMD_REBOOT, argv) != Z_OK)
1869823Sgjelinek 		return (Z_ERR);
18700Sstevel@tonic-gate 
18710Sstevel@tonic-gate 	zarg.cmd = Z_REBOOT;
18727089Sgjelinek 	return ((zonecfg_call_zoneadmd(target_zone, &zarg, locale, B_TRUE) == 0)
18737089Sgjelinek 	    ? Z_OK : Z_ERR);
18747089Sgjelinek }
18757089Sgjelinek 
18767089Sgjelinek static int
18777089Sgjelinek get_hook(brand_handle_t bh, char *cmd, size_t len, int (*bp)(brand_handle_t,
18787089Sgjelinek     const char *, const char *, char *, size_t), char *zonename, char *zonepath)
18797089Sgjelinek {
18807089Sgjelinek 	if (strlcpy(cmd, EXEC_PREFIX, len) >= len)
18817089Sgjelinek 		return (Z_ERR);
18827089Sgjelinek 
18837089Sgjelinek 	if (bp(bh, zonename, zonepath, cmd + EXEC_LEN, len - EXEC_LEN) != 0)
18847089Sgjelinek 		return (Z_ERR);
18857089Sgjelinek 
18867089Sgjelinek 	if (strlen(cmd) <= EXEC_LEN)
18877089Sgjelinek 		cmd[0] = '\0';
18887089Sgjelinek 
18897089Sgjelinek 	return (Z_OK);
18900Sstevel@tonic-gate }
18910Sstevel@tonic-gate 
18920Sstevel@tonic-gate static int
18933339Szt129084 verify_brand(zone_dochandle_t handle, int cmd_num, char *argv[])
18942712Snn35248 {
18952712Snn35248 	char cmdbuf[MAXPATHLEN];
18962712Snn35248 	int err;
18972712Snn35248 	char zonepath[MAXPATHLEN];
18982727Sedp 	brand_handle_t bh = NULL;
18993339Szt129084 	int status, i;
19002712Snn35248 
19012712Snn35248 	/*
19022712Snn35248 	 * Fetch the verify command from the brand configuration.
19032712Snn35248 	 * "exec" the command so that the returned status is that of
19042712Snn35248 	 * the command and not the shell.
19052712Snn35248 	 */
19067089Sgjelinek 	if (handle == NULL) {
19077089Sgjelinek 		(void) strlcpy(zonepath, "-", sizeof (zonepath));
19087089Sgjelinek 	} else if ((err = zonecfg_get_zonepath(handle, zonepath,
19097089Sgjelinek 	    sizeof (zonepath))) != Z_OK) {
19102712Snn35248 		errno = err;
19113339Szt129084 		zperror(cmd_to_str(cmd_num), B_TRUE);
19122712Snn35248 		return (Z_ERR);
19132712Snn35248 	}
19142727Sedp 	if ((bh = brand_open(target_brand)) == NULL) {
19152712Snn35248 		zerror(gettext("missing or invalid brand"));
19162712Snn35248 		return (Z_ERR);
19172712Snn35248 	}
19182712Snn35248 
19192712Snn35248 	/*
19202712Snn35248 	 * If the brand has its own verification routine, execute it now.
19213339Szt129084 	 * The verification routine validates the intended zoneadm
19223339Szt129084 	 * operation for the specific brand. The zoneadm subcommand and
19233339Szt129084 	 * all its arguments are passed to the routine.
19242712Snn35248 	 */
19257089Sgjelinek 	err = get_hook(bh, cmdbuf, sizeof (cmdbuf), brand_get_verify_adm,
19267089Sgjelinek 	    target_zone, zonepath);
19272727Sedp 	brand_close(bh);
19287089Sgjelinek 	if (err != Z_OK)
19293339Szt129084 		return (Z_BRAND_ERROR);
19307089Sgjelinek 	if (cmdbuf[0] == '\0')
19313339Szt129084 		return (Z_OK);
19323339Szt129084 
19333339Szt129084 	if (strlcat(cmdbuf, cmd_to_str(cmd_num),
19343339Szt129084 	    sizeof (cmdbuf)) >= sizeof (cmdbuf))
19353339Szt129084 		return (Z_ERR);
19363339Szt129084 
19373339Szt129084 	/* Build the argv string */
19383339Szt129084 	i = 0;
19393339Szt129084 	while (argv[i] != NULL) {
19403339Szt129084 		if ((strlcat(cmdbuf, " ",
19413339Szt129084 		    sizeof (cmdbuf)) >= sizeof (cmdbuf)) ||
19423339Szt129084 		    (strlcat(cmdbuf, argv[i++],
19433339Szt129084 		    sizeof (cmdbuf)) >= sizeof (cmdbuf)))
19443339Szt129084 			return (Z_ERR);
19453339Szt129084 	}
19463339Szt129084 
19473686Sgjelinek 	if (zoneadm_is_nested)
19483686Sgjelinek 		status = do_subproc(cmdbuf);
19493686Sgjelinek 	else
19503686Sgjelinek 		status = do_subproc_interactive(cmdbuf);
19513339Szt129084 	err = subproc_status(gettext("brand-specific verification"),
19523339Szt129084 	    status, B_FALSE);
19533339Szt129084 
19543339Szt129084 	return ((err == ZONE_SUBPROC_OK) ? Z_OK : Z_BRAND_ERROR);
19552712Snn35248 }
19562712Snn35248 
19572712Snn35248 static int
19580Sstevel@tonic-gate verify_rctls(zone_dochandle_t handle)
19590Sstevel@tonic-gate {
19600Sstevel@tonic-gate 	struct zone_rctltab rctltab;
19610Sstevel@tonic-gate 	size_t rbs = rctlblk_size();
19620Sstevel@tonic-gate 	rctlblk_t *rctlblk;
19630Sstevel@tonic-gate 	int error = Z_INVAL;
19640Sstevel@tonic-gate 
19650Sstevel@tonic-gate 	if ((rctlblk = malloc(rbs)) == NULL) {
19660Sstevel@tonic-gate 		zerror(gettext("failed to allocate %lu bytes: %s"), rbs,
19670Sstevel@tonic-gate 		    strerror(errno));
19680Sstevel@tonic-gate 		return (Z_NOMEM);
19690Sstevel@tonic-gate 	}
19700Sstevel@tonic-gate 
19710Sstevel@tonic-gate 	if (zonecfg_setrctlent(handle) != Z_OK) {
19720Sstevel@tonic-gate 		zerror(gettext("zonecfg_setrctlent failed"));
19730Sstevel@tonic-gate 		free(rctlblk);
19740Sstevel@tonic-gate 		return (error);
19750Sstevel@tonic-gate 	}
19760Sstevel@tonic-gate 
19770Sstevel@tonic-gate 	rctltab.zone_rctl_valptr = NULL;
19780Sstevel@tonic-gate 	while (zonecfg_getrctlent(handle, &rctltab) == Z_OK) {
19790Sstevel@tonic-gate 		struct zone_rctlvaltab *rctlval;
19800Sstevel@tonic-gate 		const char *name = rctltab.zone_rctl_name;
19810Sstevel@tonic-gate 
19820Sstevel@tonic-gate 		if (!zonecfg_is_rctl(name)) {
19830Sstevel@tonic-gate 			zerror(gettext("WARNING: Ignoring unrecognized rctl "
19840Sstevel@tonic-gate 			    "'%s'."),  name);
19850Sstevel@tonic-gate 			zonecfg_free_rctl_value_list(rctltab.zone_rctl_valptr);
19860Sstevel@tonic-gate 			rctltab.zone_rctl_valptr = NULL;
19870Sstevel@tonic-gate 			continue;
19880Sstevel@tonic-gate 		}
19890Sstevel@tonic-gate 
19900Sstevel@tonic-gate 		for (rctlval = rctltab.zone_rctl_valptr; rctlval != NULL;
19910Sstevel@tonic-gate 		    rctlval = rctlval->zone_rctlval_next) {
19920Sstevel@tonic-gate 			if (zonecfg_construct_rctlblk(rctlval, rctlblk)
19930Sstevel@tonic-gate 			    != Z_OK) {
19940Sstevel@tonic-gate 				zerror(gettext("invalid rctl value: "
19950Sstevel@tonic-gate 				    "(priv=%s,limit=%s,action%s)"),
19960Sstevel@tonic-gate 				    rctlval->zone_rctlval_priv,
19970Sstevel@tonic-gate 				    rctlval->zone_rctlval_limit,
19980Sstevel@tonic-gate 				    rctlval->zone_rctlval_action);
19990Sstevel@tonic-gate 				goto out;
20000Sstevel@tonic-gate 			}
20010Sstevel@tonic-gate 			if (!zonecfg_valid_rctl(name, rctlblk)) {
20020Sstevel@tonic-gate 				zerror(gettext("(priv=%s,limit=%s,action=%s) "
20030Sstevel@tonic-gate 				    "is not a valid value for rctl '%s'"),
20040Sstevel@tonic-gate 				    rctlval->zone_rctlval_priv,
20050Sstevel@tonic-gate 				    rctlval->zone_rctlval_limit,
20060Sstevel@tonic-gate 				    rctlval->zone_rctlval_action,
20070Sstevel@tonic-gate 				    name);
20080Sstevel@tonic-gate 				goto out;
20090Sstevel@tonic-gate 			}
20100Sstevel@tonic-gate 		}
20110Sstevel@tonic-gate 		zonecfg_free_rctl_value_list(rctltab.zone_rctl_valptr);
20120Sstevel@tonic-gate 	}
20130Sstevel@tonic-gate 	rctltab.zone_rctl_valptr = NULL;
20140Sstevel@tonic-gate 	error = Z_OK;
20150Sstevel@tonic-gate out:
20160Sstevel@tonic-gate 	zonecfg_free_rctl_value_list(rctltab.zone_rctl_valptr);
20170Sstevel@tonic-gate 	(void) zonecfg_endrctlent(handle);
20180Sstevel@tonic-gate 	free(rctlblk);
20190Sstevel@tonic-gate 	return (error);
20200Sstevel@tonic-gate }
20210Sstevel@tonic-gate 
20220Sstevel@tonic-gate static int
20230Sstevel@tonic-gate verify_pool(zone_dochandle_t handle)
20240Sstevel@tonic-gate {
20250Sstevel@tonic-gate 	char poolname[MAXPATHLEN];
20260Sstevel@tonic-gate 	pool_conf_t *poolconf;
20270Sstevel@tonic-gate 	pool_t *pool;
20280Sstevel@tonic-gate 	int status;
20290Sstevel@tonic-gate 	int error;
20300Sstevel@tonic-gate 
20310Sstevel@tonic-gate 	/*
20320Sstevel@tonic-gate 	 * This ends up being very similar to the check done in zoneadmd.
20330Sstevel@tonic-gate 	 */
20340Sstevel@tonic-gate 	error = zonecfg_get_pool(handle, poolname, sizeof (poolname));
20350Sstevel@tonic-gate 	if (error == Z_NO_ENTRY || (error == Z_OK && strlen(poolname) == 0)) {
20360Sstevel@tonic-gate 		/*
20370Sstevel@tonic-gate 		 * No pool specified.
20380Sstevel@tonic-gate 		 */
20390Sstevel@tonic-gate 		return (0);
20400Sstevel@tonic-gate 	}
20410Sstevel@tonic-gate 	if (error != Z_OK) {
20420Sstevel@tonic-gate 		zperror(gettext("Unable to retrieve pool name from "
20430Sstevel@tonic-gate 		    "configuration"), B_TRUE);
20440Sstevel@tonic-gate 		return (error);
20450Sstevel@tonic-gate 	}
20460Sstevel@tonic-gate 	/*
20470Sstevel@tonic-gate 	 * Don't do anything if pools aren't enabled.
20480Sstevel@tonic-gate 	 */
20490Sstevel@tonic-gate 	if (pool_get_status(&status) != PO_SUCCESS || status != POOL_ENABLED) {
20500Sstevel@tonic-gate 		zerror(gettext("WARNING: pools facility not active; "
20510Sstevel@tonic-gate 		    "zone will not be bound to pool '%s'."), poolname);
20520Sstevel@tonic-gate 		return (Z_OK);
20530Sstevel@tonic-gate 	}
20540Sstevel@tonic-gate 	/*
20550Sstevel@tonic-gate 	 * Try to provide a sane error message if the requested pool doesn't
20560Sstevel@tonic-gate 	 * exist.  It isn't clear that pools-related failures should
20570Sstevel@tonic-gate 	 * necessarily translate to a failure to verify the zone configuration,
20580Sstevel@tonic-gate 	 * hence they are not considered errors.
20590Sstevel@tonic-gate 	 */
20600Sstevel@tonic-gate 	if ((poolconf = pool_conf_alloc()) == NULL) {
20610Sstevel@tonic-gate 		zerror(gettext("WARNING: pool_conf_alloc failed; "
20620Sstevel@tonic-gate 		    "using default pool"));
20630Sstevel@tonic-gate 		return (Z_OK);
20640Sstevel@tonic-gate 	}
20650Sstevel@tonic-gate 	if (pool_conf_open(poolconf, pool_dynamic_location(), PO_RDONLY) !=
20660Sstevel@tonic-gate 	    PO_SUCCESS) {
20670Sstevel@tonic-gate 		zerror(gettext("WARNING: pool_conf_open failed; "
20680Sstevel@tonic-gate 		    "using default pool"));
20690Sstevel@tonic-gate 		pool_conf_free(poolconf);
20700Sstevel@tonic-gate 		return (Z_OK);
20710Sstevel@tonic-gate 	}
20720Sstevel@tonic-gate 	pool = pool_get_pool(poolconf, poolname);
20730Sstevel@tonic-gate 	(void) pool_conf_close(poolconf);
20740Sstevel@tonic-gate 	pool_conf_free(poolconf);
20750Sstevel@tonic-gate 	if (pool == NULL) {
20760Sstevel@tonic-gate 		zerror(gettext("WARNING: pool '%s' not found. "
20770Sstevel@tonic-gate 		    "using default pool"), poolname);
20780Sstevel@tonic-gate 	}
20790Sstevel@tonic-gate 
20800Sstevel@tonic-gate 	return (Z_OK);
20810Sstevel@tonic-gate }
20820Sstevel@tonic-gate 
20830Sstevel@tonic-gate static int
2084823Sgjelinek verify_ipd(zone_dochandle_t handle)
2085823Sgjelinek {
2086823Sgjelinek 	int return_code = Z_OK;
2087823Sgjelinek 	struct zone_fstab fstab;
2088823Sgjelinek 	struct stat st;
2089823Sgjelinek 	char specdir[MAXPATHLEN];
2090823Sgjelinek 
2091823Sgjelinek 	if (zonecfg_setipdent(handle) != Z_OK) {
2092924Sgjelinek 		/*
2093924Sgjelinek 		 * TRANSLATION_NOTE
2094924Sgjelinek 		 * inherit-pkg-dirs is a literal that should not be translated.
2095924Sgjelinek 		 */
2096924Sgjelinek 		(void) fprintf(stderr, gettext("could not verify "
2097823Sgjelinek 		    "inherit-pkg-dirs: unable to enumerate mounts\n"));
2098823Sgjelinek 		return (Z_ERR);
2099823Sgjelinek 	}
2100823Sgjelinek 	while (zonecfg_getipdent(handle, &fstab) == Z_OK) {
2101823Sgjelinek 		/*
2102823Sgjelinek 		 * Verify fs_dir exists.
2103823Sgjelinek 		 */
2104823Sgjelinek 		(void) snprintf(specdir, sizeof (specdir), "%s%s",
2105823Sgjelinek 		    zonecfg_get_root(), fstab.zone_fs_dir);
2106823Sgjelinek 		if (stat(specdir, &st) != 0) {
2107924Sgjelinek 			/*
2108924Sgjelinek 			 * TRANSLATION_NOTE
2109924Sgjelinek 			 * inherit-pkg-dir is a literal that should not be
2110924Sgjelinek 			 * translated.
2111924Sgjelinek 			 */
2112924Sgjelinek 			(void) fprintf(stderr, gettext("could not verify "
2113823Sgjelinek 			    "inherit-pkg-dir %s: %s\n"),
2114823Sgjelinek 			    fstab.zone_fs_dir, strerror(errno));
2115823Sgjelinek 			return_code = Z_ERR;
2116823Sgjelinek 		}
2117823Sgjelinek 		if (strcmp(st.st_fstype, MNTTYPE_NFS) == 0) {
2118924Sgjelinek 			/*
2119924Sgjelinek 			 * TRANSLATION_NOTE
2120924Sgjelinek 			 * inherit-pkg-dir and NFS are literals that should
2121924Sgjelinek 			 * not be translated.
2122924Sgjelinek 			 */
2123823Sgjelinek 			(void) fprintf(stderr, gettext("cannot verify "
21241867Sgjelinek 			    "inherit-pkg-dir %s: NFS mounted file system.\n"
21251867Sgjelinek 			    "\tA local file system must be used.\n"),
2126823Sgjelinek 			    fstab.zone_fs_dir);
2127823Sgjelinek 			return_code = Z_ERR;
2128823Sgjelinek 		}
2129823Sgjelinek 	}
2130823Sgjelinek 	(void) zonecfg_endipdent(handle);
2131823Sgjelinek 
2132823Sgjelinek 	return (return_code);
2133823Sgjelinek }
2134823Sgjelinek 
21351393Slling /*
21361867Sgjelinek  * Verify that the special device/file system exists and is valid.
21371393Slling  */
21381393Slling static int
21391393Slling verify_fs_special(struct zone_fstab *fstab)
21401393Slling {
21416734Sjohnlev 	struct stat64 st;
21421393Slling 
21432971Sgjelinek 	/*
21442971Sgjelinek 	 * This validation is really intended for standard zone administration.
21452971Sgjelinek 	 * If we are in a mini-root or some other upgrade situation where
21462971Sgjelinek 	 * we are using the scratch zone, just by-pass this.
21472971Sgjelinek 	 */
21482971Sgjelinek 	if (zonecfg_in_alt_root())
21492971Sgjelinek 		return (Z_OK);
21502971Sgjelinek 
21511393Slling 	if (strcmp(fstab->zone_fs_type, MNTTYPE_ZFS) == 0)
21521393Slling 		return (verify_fs_zfs(fstab));
21531393Slling 
21546734Sjohnlev 	if (stat64(fstab->zone_fs_special, &st) != 0) {
21551397Slling 		(void) fprintf(stderr, gettext("could not verify fs "
21561393Slling 		    "%s: could not access %s: %s\n"), fstab->zone_fs_dir,
21571393Slling 		    fstab->zone_fs_special, strerror(errno));
21581393Slling 		return (Z_ERR);
21591393Slling 	}
21601393Slling 
21611393Slling 	if (strcmp(st.st_fstype, MNTTYPE_NFS) == 0) {
21621393Slling 		/*
21631393Slling 		 * TRANSLATION_NOTE
21641393Slling 		 * fs and NFS are literals that should
21651393Slling 		 * not be translated.
21661393Slling 		 */
21671393Slling 		(void) fprintf(stderr, gettext("cannot verify "
21681867Sgjelinek 		    "fs %s: NFS mounted file system.\n"
21691867Sgjelinek 		    "\tA local file system must be used.\n"),
21701393Slling 		    fstab->zone_fs_special);
21711393Slling 		return (Z_ERR);
21721393Slling 	}
21731393Slling 
21741393Slling 	return (Z_OK);
21751393Slling }
21761393Slling 
2177823Sgjelinek static int
21786734Sjohnlev isregfile(const char *path)
21796734Sjohnlev {
21806734Sjohnlev 	struct stat64 st;
21816734Sjohnlev 
21826734Sjohnlev 	if (stat64(path, &st) == -1)
21836734Sjohnlev 		return (-1);
21846734Sjohnlev 
21856734Sjohnlev 	return (S_ISREG(st.st_mode));
21866734Sjohnlev }
21876734Sjohnlev 
21886734Sjohnlev static int
21890Sstevel@tonic-gate verify_filesystems(zone_dochandle_t handle)
21900Sstevel@tonic-gate {
21910Sstevel@tonic-gate 	int return_code = Z_OK;
21920Sstevel@tonic-gate 	struct zone_fstab fstab;
21930Sstevel@tonic-gate 	char cmdbuf[MAXPATHLEN];
21940Sstevel@tonic-gate 	struct stat st;
21950Sstevel@tonic-gate 
21960Sstevel@tonic-gate 	/*
21970Sstevel@tonic-gate 	 * No need to verify inherit-pkg-dir fs types, as their type is
21980Sstevel@tonic-gate 	 * implicitly lofs, which is known.  Therefore, the types are only
21991867Sgjelinek 	 * verified for regular file systems below.
22000Sstevel@tonic-gate 	 *
22010Sstevel@tonic-gate 	 * Since the actual mount point is not known until the dependent mounts
22020Sstevel@tonic-gate 	 * are performed, we don't attempt any path validation here: that will
22030Sstevel@tonic-gate 	 * happen later when zoneadmd actually does the mounts.
22040Sstevel@tonic-gate 	 */
22050Sstevel@tonic-gate 	if (zonecfg_setfsent(handle) != Z_OK) {
22061867Sgjelinek 		(void) fprintf(stderr, gettext("could not verify file systems: "
22070Sstevel@tonic-gate 		    "unable to enumerate mounts\n"));
22080Sstevel@tonic-gate 		return (Z_ERR);
22090Sstevel@tonic-gate 	}
22100Sstevel@tonic-gate 	while (zonecfg_getfsent(handle, &fstab) == Z_OK) {
22110Sstevel@tonic-gate 		if (!zonecfg_valid_fs_type(fstab.zone_fs_type)) {
22120Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("cannot verify fs %s: "
22130Sstevel@tonic-gate 			    "type %s is not allowed.\n"), fstab.zone_fs_dir,
22140Sstevel@tonic-gate 			    fstab.zone_fs_type);
22150Sstevel@tonic-gate 			return_code = Z_ERR;
22160Sstevel@tonic-gate 			goto next_fs;
22170Sstevel@tonic-gate 		}
22180Sstevel@tonic-gate 		/*
22190Sstevel@tonic-gate 		 * Verify /usr/lib/fs/<fstype>/mount exists.
22200Sstevel@tonic-gate 		 */
22210Sstevel@tonic-gate 		if (snprintf(cmdbuf, sizeof (cmdbuf), "/usr/lib/fs/%s/mount",
22220Sstevel@tonic-gate 		    fstab.zone_fs_type) > sizeof (cmdbuf)) {
22230Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("cannot verify fs %s: "
22240Sstevel@tonic-gate 			    "type %s is too long.\n"), fstab.zone_fs_dir,
22250Sstevel@tonic-gate 			    fstab.zone_fs_type);
22260Sstevel@tonic-gate 			return_code = Z_ERR;
22270Sstevel@tonic-gate 			goto next_fs;
22280Sstevel@tonic-gate 		}
22290Sstevel@tonic-gate 		if (stat(cmdbuf, &st) != 0) {
2230924Sgjelinek 			(void) fprintf(stderr, gettext("could not verify fs "
2231924Sgjelinek 			    "%s: could not access %s: %s\n"), fstab.zone_fs_dir,
22320Sstevel@tonic-gate 			    cmdbuf, strerror(errno));
22330Sstevel@tonic-gate 			return_code = Z_ERR;
22340Sstevel@tonic-gate 			goto next_fs;
22350Sstevel@tonic-gate 		}
22360Sstevel@tonic-gate 		if (!S_ISREG(st.st_mode)) {
2237924Sgjelinek 			(void) fprintf(stderr, gettext("could not verify fs "
2238924Sgjelinek 			    "%s: %s is not a regular file\n"),
2239924Sgjelinek 			    fstab.zone_fs_dir, cmdbuf);
22400Sstevel@tonic-gate 			return_code = Z_ERR;
22410Sstevel@tonic-gate 			goto next_fs;
22420Sstevel@tonic-gate 		}
22430Sstevel@tonic-gate 		/*
22446734Sjohnlev 		 * If zone_fs_raw is set, verify that there's an fsck
22456734Sjohnlev 		 * binary for it.  If zone_fs_raw is not set, and it's
22466734Sjohnlev 		 * not a regular file (lofi mount), and there's an fsck
22476734Sjohnlev 		 * binary for it, complain.
22480Sstevel@tonic-gate 		 */
22490Sstevel@tonic-gate 		if (snprintf(cmdbuf, sizeof (cmdbuf), "/usr/lib/fs/%s/fsck",
22500Sstevel@tonic-gate 		    fstab.zone_fs_type) > sizeof (cmdbuf)) {
22510Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("cannot verify fs %s: "
22520Sstevel@tonic-gate 			    "type %s is too long.\n"), fstab.zone_fs_dir,
22530Sstevel@tonic-gate 			    fstab.zone_fs_type);
22540Sstevel@tonic-gate 			return_code = Z_ERR;
22550Sstevel@tonic-gate 			goto next_fs;
22560Sstevel@tonic-gate 		}
22570Sstevel@tonic-gate 		if (fstab.zone_fs_raw[0] != '\0' &&
22580Sstevel@tonic-gate 		    (stat(cmdbuf, &st) != 0 || !S_ISREG(st.st_mode))) {
22590Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("cannot verify fs %s: "
22600Sstevel@tonic-gate 			    "'raw' device specified but "
22610Sstevel@tonic-gate 			    "no fsck executable exists for %s\n"),
22620Sstevel@tonic-gate 			    fstab.zone_fs_dir, fstab.zone_fs_type);
22630Sstevel@tonic-gate 			return_code = Z_ERR;
22640Sstevel@tonic-gate 			goto next_fs;
22656734Sjohnlev 		} else if (fstab.zone_fs_raw[0] == '\0' &&
22666734Sjohnlev 		    stat(cmdbuf, &st) == 0 &&
22676734Sjohnlev 		    isregfile(fstab.zone_fs_special) != 1) {
22686734Sjohnlev 			(void) fprintf(stderr, gettext("could not verify fs "
22696734Sjohnlev 			    "%s: must specify 'raw' device for %s "
22706734Sjohnlev 			    "file systems\n"),
22716734Sjohnlev 			    fstab.zone_fs_dir, fstab.zone_fs_type);
22726734Sjohnlev 			return_code = Z_ERR;
22736734Sjohnlev 			goto next_fs;
22740Sstevel@tonic-gate 		}
22751393Slling 
22761393Slling 		/* Verify fs_special. */
22771393Slling 		if ((return_code = verify_fs_special(&fstab)) != Z_OK)
2278823Sgjelinek 			goto next_fs;
22791393Slling 
22801393Slling 		/* Verify fs_raw. */
2281823Sgjelinek 		if (fstab.zone_fs_raw[0] != '\0' &&
2282823Sgjelinek 		    stat(fstab.zone_fs_raw, &st) != 0) {
2283924Sgjelinek 			/*
2284924Sgjelinek 			 * TRANSLATION_NOTE
2285924Sgjelinek 			 * fs is a literal that should not be translated.
2286924Sgjelinek 			 */
2287924Sgjelinek 			(void) fprintf(stderr, gettext("could not verify fs "
2288924Sgjelinek 			    "%s: could not access %s: %s\n"), fstab.zone_fs_dir,
2289823Sgjelinek 			    fstab.zone_fs_raw, strerror(errno));
2290823Sgjelinek 			return_code = Z_ERR;
2291823Sgjelinek 			goto next_fs;
2292823Sgjelinek 		}
22930Sstevel@tonic-gate next_fs:
22940Sstevel@tonic-gate 		zonecfg_free_fs_option_list(fstab.zone_fs_options);
22950Sstevel@tonic-gate 	}
22960Sstevel@tonic-gate 	(void) zonecfg_endfsent(handle);
22970Sstevel@tonic-gate 
22980Sstevel@tonic-gate 	return (return_code);
22990Sstevel@tonic-gate }
23000Sstevel@tonic-gate 
23010Sstevel@tonic-gate static int
23021645Scomay verify_limitpriv(zone_dochandle_t handle)
23031645Scomay {
23041645Scomay 	char *privname = NULL;
23051645Scomay 	int err;
23061645Scomay 	priv_set_t *privs;
23071645Scomay 
23081645Scomay 	if ((privs = priv_allocset()) == NULL) {
23091645Scomay 		zperror(gettext("failed to allocate privilege set"), B_FALSE);
23101645Scomay 		return (Z_NOMEM);
23111645Scomay 	}
23121645Scomay 	err = zonecfg_get_privset(handle, privs, &privname);
23131645Scomay 	switch (err) {
23141645Scomay 	case Z_OK:
23151645Scomay 		break;
23161645Scomay 	case Z_PRIV_PROHIBITED:
23171645Scomay 		(void) fprintf(stderr, gettext("privilege \"%s\" is not "
23181645Scomay 		    "permitted within the zone's privilege set\n"), privname);
23191645Scomay 		break;
23201645Scomay 	case Z_PRIV_REQUIRED:
23211645Scomay 		(void) fprintf(stderr, gettext("required privilege \"%s\" is "
23221645Scomay 		    "missing from the zone's privilege set\n"), privname);
23231645Scomay 		break;
23241645Scomay 	case Z_PRIV_UNKNOWN:
23251645Scomay 		(void) fprintf(stderr, gettext("unknown privilege \"%s\" "
23261645Scomay 		    "specified in the zone's privilege set\n"), privname);
23271645Scomay 		break;
23281645Scomay 	default:
23291645Scomay 		zperror(
23301645Scomay 		    gettext("failed to determine the zone's privilege set"),
23311645Scomay 		    B_TRUE);
23321645Scomay 		break;
23331645Scomay 	}
23341645Scomay 	free(privname);
23351645Scomay 	priv_freeset(privs);
23361645Scomay 	return (err);
23371645Scomay }
23381645Scomay 
23391915Sgjelinek static void
23401915Sgjelinek free_local_netifs(int if_cnt, struct net_if **if_list)
23411915Sgjelinek {
23421915Sgjelinek 	int		i;
23431915Sgjelinek 
23441915Sgjelinek 	for (i = 0; i < if_cnt; i++) {
23451915Sgjelinek 		free(if_list[i]->name);
23461915Sgjelinek 		free(if_list[i]);
23471915Sgjelinek 	}
23481915Sgjelinek 	free(if_list);
23491915Sgjelinek }
23501915Sgjelinek 
23511915Sgjelinek /*
23521915Sgjelinek  * Get a list of the network interfaces, along with their address families,
23531915Sgjelinek  * that are plumbed in the global zone.  See if_tcp(7p) for a description
23541915Sgjelinek  * of the ioctls used here.
23551915Sgjelinek  */
23561915Sgjelinek static int
23571915Sgjelinek get_local_netifs(int *if_cnt, struct net_if ***if_list)
23581915Sgjelinek {
23591915Sgjelinek 	int		s;
23601915Sgjelinek 	int		i;
23611915Sgjelinek 	int		res = Z_OK;
23621915Sgjelinek 	int		space_needed;
23631915Sgjelinek 	int		cnt = 0;
23641915Sgjelinek 	struct		lifnum if_num;
23651915Sgjelinek 	struct		lifconf if_conf;
23661915Sgjelinek 	struct		lifreq *if_reqp;
23671915Sgjelinek 	char		*if_buf;
23681915Sgjelinek 	struct net_if	**local_ifs = NULL;
23691915Sgjelinek 
23701915Sgjelinek 	*if_cnt = 0;
23711915Sgjelinek 	*if_list = NULL;
23721915Sgjelinek 
23731915Sgjelinek 	if ((s = socket(SOCKET_AF(AF_INET), SOCK_DGRAM, 0)) < 0)
23741915Sgjelinek 		return (Z_ERR);
23751915Sgjelinek 
23761915Sgjelinek 	/*
23771915Sgjelinek 	 * Come back here in the unlikely event that the number of interfaces
23781915Sgjelinek 	 * increases between the time we get the count and the time we do the
23791915Sgjelinek 	 * SIOCGLIFCONF ioctl.
23801915Sgjelinek 	 */
23811915Sgjelinek retry:
23821915Sgjelinek 	/* Get the number of interfaces. */
23831915Sgjelinek 	if_num.lifn_family = AF_UNSPEC;
23841915Sgjelinek 	if_num.lifn_flags = LIFC_NOXMIT;
23851915Sgjelinek 	if (ioctl(s, SIOCGLIFNUM, &if_num) < 0) {
23861915Sgjelinek 		(void) close(s);
23871915Sgjelinek 		return (Z_ERR);
23881915Sgjelinek 	}
23891915Sgjelinek 
23901915Sgjelinek 	/* Get the interface configuration list. */
23911915Sgjelinek 	space_needed = if_num.lifn_count * sizeof (struct lifreq);
23921915Sgjelinek 	if ((if_buf = malloc(space_needed)) == NULL) {
23931915Sgjelinek 		(void) close(s);
23941915Sgjelinek 		return (Z_ERR);
23951915Sgjelinek 	}
23961915Sgjelinek 	if_conf.lifc_family = AF_UNSPEC;
23971915Sgjelinek 	if_conf.lifc_flags = LIFC_NOXMIT;
23981915Sgjelinek 	if_conf.lifc_len = space_needed;
23991915Sgjelinek 	if_conf.lifc_buf = if_buf;
24001915Sgjelinek 	if (ioctl(s, SIOCGLIFCONF, &if_conf) < 0) {
24011915Sgjelinek 		free(if_buf);
24021915Sgjelinek 		/*
24031915Sgjelinek 		 * SIOCGLIFCONF returns EINVAL if the buffer we passed in is
24041915Sgjelinek 		 * too small.  In this case go back and get the new if cnt.
24051915Sgjelinek 		 */
24061915Sgjelinek 		if (errno == EINVAL)
24071915Sgjelinek 			goto retry;
24081915Sgjelinek 
24091915Sgjelinek 		(void) close(s);
24101915Sgjelinek 		return (Z_ERR);
24111915Sgjelinek 	}
24121915Sgjelinek 	(void) close(s);
24131915Sgjelinek 
24141915Sgjelinek 	/* Get the name and address family for each interface. */
24151915Sgjelinek 	if_reqp = if_conf.lifc_req;
24161915Sgjelinek 	for (i = 0; i < (if_conf.lifc_len / sizeof (struct lifreq)); i++) {
24171915Sgjelinek 		struct net_if	**p;
24181915Sgjelinek 		struct lifreq	req;
24191915Sgjelinek 
24201915Sgjelinek 		if (strcmp(LOOPBACK_IF, if_reqp->lifr_name) == 0) {
24211915Sgjelinek 			if_reqp++;
24221915Sgjelinek 			continue;
24231915Sgjelinek 		}
24241915Sgjelinek 
24251915Sgjelinek 		if ((s = socket(SOCKET_AF(if_reqp->lifr_addr.ss_family),
24261915Sgjelinek 		    SOCK_DGRAM, 0)) == -1) {
24271915Sgjelinek 			res = Z_ERR;
24281915Sgjelinek 			break;
24291915Sgjelinek 		}
24301915Sgjelinek 
24311915Sgjelinek 		(void) strncpy(req.lifr_name, if_reqp->lifr_name,
24321915Sgjelinek 		    sizeof (req.lifr_name));
24331915Sgjelinek 		if (ioctl(s, SIOCGLIFADDR, &req) < 0) {
24341915Sgjelinek 			(void) close(s);
24351915Sgjelinek 			if_reqp++;
24361915Sgjelinek 			continue;
24371915Sgjelinek 		}
24381915Sgjelinek 
24391915Sgjelinek 		if ((p = (struct net_if **)realloc(local_ifs,
24401915Sgjelinek 		    sizeof (struct net_if *) * (cnt + 1))) == NULL) {
24411915Sgjelinek 			res = Z_ERR;
24421915Sgjelinek 			break;
24431915Sgjelinek 		}
24441915Sgjelinek 		local_ifs = p;
24451915Sgjelinek 
24461915Sgjelinek 		if ((local_ifs[cnt] = malloc(sizeof (struct net_if))) == NULL) {
24471915Sgjelinek 			res = Z_ERR;
24481915Sgjelinek 			break;
24491915Sgjelinek 		}
24501915Sgjelinek 
24511915Sgjelinek 		if ((local_ifs[cnt]->name = strdup(if_reqp->lifr_name))
24521915Sgjelinek 		    == NULL) {
24531915Sgjelinek 			free(local_ifs[cnt]);
24541915Sgjelinek 			res = Z_ERR;
24551915Sgjelinek 			break;
24561915Sgjelinek 		}
24571915Sgjelinek 		local_ifs[cnt]->af = req.lifr_addr.ss_family;
24581915Sgjelinek 		cnt++;
24591915Sgjelinek 
24601915Sgjelinek 		(void) close(s);
24611915Sgjelinek 		if_reqp++;
24621915Sgjelinek 	}
24631915Sgjelinek 
24641915Sgjelinek 	free(if_buf);
24651915Sgjelinek 
24661915Sgjelinek 	if (res != Z_OK) {
24671915Sgjelinek 		free_local_netifs(cnt, local_ifs);
24681915Sgjelinek 	} else {
24691915Sgjelinek 		*if_cnt = cnt;
24701915Sgjelinek 		*if_list = local_ifs;
24711915Sgjelinek 	}
24721915Sgjelinek 
24731915Sgjelinek 	return (res);
24741915Sgjelinek }
24751915Sgjelinek 
24761915Sgjelinek static char *
24771915Sgjelinek af2str(int af)
24781915Sgjelinek {
24791915Sgjelinek 	switch (af) {
24801915Sgjelinek 	case AF_INET:
24811915Sgjelinek 		return ("IPv4");
24821915Sgjelinek 	case AF_INET6:
24831915Sgjelinek 		return ("IPv6");
24841915Sgjelinek 	default:
24851915Sgjelinek 		return ("Unknown");
24861915Sgjelinek 	}
24871915Sgjelinek }
24881915Sgjelinek 
24891915Sgjelinek /*
24901915Sgjelinek  * Cross check the network interface name and address family with the
24911915Sgjelinek  * interfaces that are set up in the global zone so that we can print the
24921915Sgjelinek  * appropriate error message.
24931915Sgjelinek  */
24941915Sgjelinek static void
24951915Sgjelinek print_net_err(char *phys, char *addr, int af, char *msg)
24961915Sgjelinek {
24971915Sgjelinek 	int		i;
24981915Sgjelinek 	int		local_if_cnt = 0;
24991915Sgjelinek 	struct net_if	**local_ifs = NULL;
25001915Sgjelinek 	boolean_t	found_if = B_FALSE;
25011915Sgjelinek 	boolean_t	found_af = B_FALSE;
25021915Sgjelinek 
25031915Sgjelinek 	if (get_local_netifs(&local_if_cnt, &local_ifs) != Z_OK) {
25041915Sgjelinek 		(void) fprintf(stderr,
25051915Sgjelinek 		    gettext("could not verify %s %s=%s %s=%s\n\t%s\n"),
25061915Sgjelinek 		    "net", "address", addr, "physical", phys, msg);
25071915Sgjelinek 		return;
25081915Sgjelinek 	}
25091915Sgjelinek 
25101915Sgjelinek 	for (i = 0; i < local_if_cnt; i++) {
25111915Sgjelinek 		if (strcmp(phys, local_ifs[i]->name) == 0) {
25121915Sgjelinek 			found_if = B_TRUE;
25131915Sgjelinek 			if (af == local_ifs[i]->af) {
25141915Sgjelinek 				found_af = B_TRUE;
25151915Sgjelinek 				break;
25161915Sgjelinek 			}
25171915Sgjelinek 		}
25181915Sgjelinek 	}
25191915Sgjelinek 
25201915Sgjelinek 	free_local_netifs(local_if_cnt, local_ifs);
25211915Sgjelinek 
25221915Sgjelinek 	if (!found_if) {
25231915Sgjelinek 		(void) fprintf(stderr,
25241915Sgjelinek 		    gettext("could not verify %s %s=%s\n\t"
25251915Sgjelinek 		    "network interface %s is not plumbed in the global zone\n"),
25261915Sgjelinek 		    "net", "physical", phys, phys);
25271915Sgjelinek 		return;
25281915Sgjelinek 	}
25291915Sgjelinek 
25301915Sgjelinek 	/*
25311915Sgjelinek 	 * Print this error if we were unable to find the address family
25321915Sgjelinek 	 * for this interface.  If the af variable is not initialized to
25331915Sgjelinek 	 * to something meaningful by the caller (not AF_UNSPEC) then we
25341915Sgjelinek 	 * also skip this message since it wouldn't be informative.
25351915Sgjelinek 	 */
25361915Sgjelinek 	if (!found_af && af != AF_UNSPEC) {
25371915Sgjelinek 		(void) fprintf(stderr,
25381915Sgjelinek 		    gettext("could not verify %s %s=%s %s=%s\n\tthe %s address "
25393448Sdh155122 		    "family is not configured on this network interface in "
25403448Sdh155122 		    "the\n\tglobal zone\n"),
25411915Sgjelinek 		    "net", "address", addr, "physical", phys, af2str(af));
25421915Sgjelinek 		return;
25431915Sgjelinek 	}
25441915Sgjelinek 
25451915Sgjelinek 	(void) fprintf(stderr,
25461915Sgjelinek 	    gettext("could not verify %s %s=%s %s=%s\n\t%s\n"),
25471915Sgjelinek 	    "net", "address", addr, "physical", phys, msg);
25481915Sgjelinek }
25491915Sgjelinek 
25501645Scomay static int
25513339Szt129084 verify_handle(int cmd_num, zone_dochandle_t handle, char *argv[])
25520Sstevel@tonic-gate {
25530Sstevel@tonic-gate 	struct zone_nwiftab nwiftab;
25540Sstevel@tonic-gate 	int return_code = Z_OK;
25550Sstevel@tonic-gate 	int err;
2556766Scarlsonj 	boolean_t in_alt_root;
25573448Sdh155122 	zone_iptype_t iptype;
25584456Sss150715 	dlpi_handle_t dh;
25590Sstevel@tonic-gate 
2560766Scarlsonj 	in_alt_root = zonecfg_in_alt_root();
2561766Scarlsonj 	if (in_alt_root)
2562766Scarlsonj 		goto no_net;
2563766Scarlsonj 
25643448Sdh155122 	if ((err = zonecfg_get_iptype(handle, &iptype)) != Z_OK) {
25653448Sdh155122 		errno = err;
25663448Sdh155122 		zperror(cmd_to_str(cmd_num), B_TRUE);
25673448Sdh155122 		zonecfg_fini_handle(handle);
25683448Sdh155122 		return (Z_ERR);
25693448Sdh155122 	}
25700Sstevel@tonic-gate 	if ((err = zonecfg_setnwifent(handle)) != Z_OK) {
25710Sstevel@tonic-gate 		errno = err;
25720Sstevel@tonic-gate 		zperror(cmd_to_str(cmd_num), B_TRUE);
25730Sstevel@tonic-gate 		zonecfg_fini_handle(handle);
25740Sstevel@tonic-gate 		return (Z_ERR);
25750Sstevel@tonic-gate 	}
25760Sstevel@tonic-gate 	while (zonecfg_getnwifent(handle, &nwiftab) == Z_OK) {
25770Sstevel@tonic-gate 		struct lifreq lifr;
25781915Sgjelinek 		sa_family_t af = AF_UNSPEC;
25793448Sdh155122 		char dl_owner_zname[ZONENAME_MAX];
25803448Sdh155122 		zoneid_t dl_owner_zid;
25813448Sdh155122 		zoneid_t target_zid;
25823448Sdh155122 		int res;
25830Sstevel@tonic-gate 
25840Sstevel@tonic-gate 		/* skip any loopback interfaces */
25850Sstevel@tonic-gate 		if (strcmp(nwiftab.zone_nwif_physical, "lo0") == 0)
25860Sstevel@tonic-gate 			continue;
25873448Sdh155122 		switch (iptype) {
25883448Sdh155122 		case ZS_SHARED:
25893448Sdh155122 			if ((res = zonecfg_valid_net_address(
25903448Sdh155122 			    nwiftab.zone_nwif_address, &lifr)) != Z_OK) {
25913448Sdh155122 				print_net_err(nwiftab.zone_nwif_physical,
25923448Sdh155122 				    nwiftab.zone_nwif_address, af,
25933448Sdh155122 				    zonecfg_strerror(res));
25944350Std153743 				return_code = Z_ERR;
25954350Std153743 				continue;
25963448Sdh155122 			}
25973448Sdh155122 			af = lifr.lifr_addr.ss_family;
25983448Sdh155122 			if (!zonecfg_ifname_exists(af,
25993448Sdh155122 			    nwiftab.zone_nwif_physical)) {
26003448Sdh155122 				/*
26013448Sdh155122 				 * The interface failed to come up. We continue
26023448Sdh155122 				 * on anyway for the sake of consistency: a
26033448Sdh155122 				 * zone is not shut down if the interface fails
26043448Sdh155122 				 * any time after boot, nor does the global zone
26053448Sdh155122 				 * fail to boot if an interface fails.
26063448Sdh155122 				 */
26073448Sdh155122 				(void) fprintf(stderr,
26083448Sdh155122 				    gettext("WARNING: skipping network "
26094350Std153743 				    "interface '%s' which may not be "
26104350Std153743 				    "present/plumbed in the global "
26114350Std153743 				    "zone.\n"),
26123448Sdh155122 				    nwiftab.zone_nwif_physical);
26133448Sdh155122 			}
26143448Sdh155122 			break;
26153448Sdh155122 		case ZS_EXCLUSIVE:
26163448Sdh155122 			/* Warning if it exists for either IPv4 or IPv6 */
26173448Sdh155122 
26183448Sdh155122 			if (zonecfg_ifname_exists(AF_INET,
26193448Sdh155122 			    nwiftab.zone_nwif_physical) ||
26203448Sdh155122 			    zonecfg_ifname_exists(AF_INET6,
26213448Sdh155122 			    nwiftab.zone_nwif_physical)) {
26223448Sdh155122 				(void) fprintf(stderr,
26233448Sdh155122 				    gettext("WARNING: skipping network "
26243448Sdh155122 				    "interface '%s' which is used in the "
26253448Sdh155122 				    "global zone.\n"),
26263448Sdh155122 				    nwiftab.zone_nwif_physical);
26273448Sdh155122 				break;
26283448Sdh155122 			}
26294456Sss150715 
26302611Svp157776 			/*
26314456Sss150715 			 * Verify that the physical interface can be opened.
26323448Sdh155122 			 */
26334456Sss150715 			err = dlpi_open(nwiftab.zone_nwif_physical, &dh, 0);
26344456Sss150715 			if (err != DLPI_SUCCESS) {
26353448Sdh155122 				(void) fprintf(stderr,
26363448Sdh155122 				    gettext("WARNING: skipping network "
26374456Sss150715 				    "interface '%s' which cannot be opened: "
26384456Sss150715 				    "dlpi error (%s).\n"),
26394456Sss150715 				    nwiftab.zone_nwif_physical,
26404456Sss150715 				    dlpi_strerror(err));
26413448Sdh155122 				break;
26423448Sdh155122 			} else {
26434456Sss150715 				dlpi_close(dh);
26443448Sdh155122 			}
26453448Sdh155122 			/*
26463448Sdh155122 			 * Verify whether the physical interface is already
26473448Sdh155122 			 * used by a zone.
26483448Sdh155122 			 */
26493448Sdh155122 			dl_owner_zid = ALL_ZONES;
26503448Sdh155122 			if (zone_check_datalink(&dl_owner_zid,
26513448Sdh155122 			    nwiftab.zone_nwif_physical) != 0)
26523448Sdh155122 				break;
26533448Sdh155122 
26543448Sdh155122 			/*
26553448Sdh155122 			 * If the zone being verified is
26563448Sdh155122 			 * running and owns the interface
26573448Sdh155122 			 */
26583448Sdh155122 			target_zid = getzoneidbyname(target_zone);
26593448Sdh155122 			if (target_zid == dl_owner_zid)
26603448Sdh155122 				break;
26613448Sdh155122 
26623448Sdh155122 			/* Zone id match failed, use name to check */
26633448Sdh155122 			if (getzonenamebyid(dl_owner_zid, dl_owner_zname,
26643448Sdh155122 			    ZONENAME_MAX) < 0) {
26653448Sdh155122 				/* No name, show ID instead */
26663448Sdh155122 				(void) snprintf(dl_owner_zname, ZONENAME_MAX,
26673448Sdh155122 				    "<%d>", dl_owner_zid);
26683448Sdh155122 			} else if (strcmp(dl_owner_zname, target_zone) == 0)
26693448Sdh155122 				break;
26703448Sdh155122 
26713448Sdh155122 			/*
26723448Sdh155122 			 * Note here we only report a warning that
26733448Sdh155122 			 * the interface is already in use by another
26743448Sdh155122 			 * running zone, and the verify process just
26753448Sdh155122 			 * goes on, if the interface is still in use
26763448Sdh155122 			 * when this zone really boots up, zoneadmd
26773448Sdh155122 			 * will find it. If the name of the zone which
26783448Sdh155122 			 * owns this interface cannot be determined,
26793448Sdh155122 			 * then it is not possible to determine if there
26803448Sdh155122 			 * is a conflict so just report it as a warning.
26812611Svp157776 			 */
26822611Svp157776 			(void) fprintf(stderr,
26833448Sdh155122 			    gettext("WARNING: skipping network interface "
26843448Sdh155122 			    "'%s' which is used by the non-global zone "
26853448Sdh155122 			    "'%s'.\n"), nwiftab.zone_nwif_physical,
26863448Sdh155122 			    dl_owner_zname);
26873448Sdh155122 			break;
26880Sstevel@tonic-gate 		}
26890Sstevel@tonic-gate 	}
26900Sstevel@tonic-gate 	(void) zonecfg_endnwifent(handle);
2691766Scarlsonj no_net:
26920Sstevel@tonic-gate 
26931931Sgjelinek 	/* verify that lofs has not been excluded from the kernel */
26942078Sgjelinek 	if (!(cmd_num == CMD_DETACH || cmd_num == CMD_ATTACH ||
26952078Sgjelinek 	    cmd_num == CMD_MOVE || cmd_num == CMD_CLONE) &&
26962078Sgjelinek 	    modctl(MODLOAD, 1, "fs/lofs", NULL) != 0) {
26971931Sgjelinek 		if (errno == ENXIO)
26981931Sgjelinek 			(void) fprintf(stderr, gettext("could not verify "
26991931Sgjelinek 			    "lofs(7FS): possibly excluded in /etc/system\n"));
27001931Sgjelinek 		else
27011931Sgjelinek 			(void) fprintf(stderr, gettext("could not verify "
27021931Sgjelinek 			    "lofs(7FS): %s\n"), strerror(errno));
27031931Sgjelinek 		return_code = Z_ERR;
27041931Sgjelinek 	}
27051931Sgjelinek 
27060Sstevel@tonic-gate 	if (verify_filesystems(handle) != Z_OK)
27070Sstevel@tonic-gate 		return_code = Z_ERR;
2708823Sgjelinek 	if (verify_ipd(handle) != Z_OK)
2709823Sgjelinek 		return_code = Z_ERR;
2710766Scarlsonj 	if (!in_alt_root && verify_rctls(handle) != Z_OK)
27110Sstevel@tonic-gate 		return_code = Z_ERR;
2712766Scarlsonj 	if (!in_alt_root && verify_pool(handle) != Z_OK)
27130Sstevel@tonic-gate 		return_code = Z_ERR;
27143339Szt129084 	if (!in_alt_root && verify_brand(handle, cmd_num, argv) != Z_OK)
27152712Snn35248 		return_code = Z_ERR;
2716789Sahrens 	if (!in_alt_root && verify_datasets(handle) != Z_OK)
2717789Sahrens 		return_code = Z_ERR;
27181645Scomay 
27191645Scomay 	/*
27201645Scomay 	 * As the "mount" command is used for patching/upgrading of zones
27211645Scomay 	 * or other maintenance processes, the zone's privilege set is not
27221645Scomay 	 * checked in this case.  Instead, the default, safe set of
27231645Scomay 	 * privileges will be used when this zone is created in the
27241645Scomay 	 * kernel.
27251645Scomay 	 */
27261645Scomay 	if (!in_alt_root && cmd_num != CMD_MOUNT &&
27271645Scomay 	    verify_limitpriv(handle) != Z_OK)
27281645Scomay 		return_code = Z_ERR;
27292078Sgjelinek 
27302078Sgjelinek 	return (return_code);
27312078Sgjelinek }
27322078Sgjelinek 
27332078Sgjelinek static int
27343339Szt129084 verify_details(int cmd_num, char *argv[])
27352078Sgjelinek {
27362078Sgjelinek 	zone_dochandle_t handle;
27372078Sgjelinek 	char zonepath[MAXPATHLEN], checkpath[MAXPATHLEN];
27382078Sgjelinek 	int return_code = Z_OK;
27392078Sgjelinek 	int err;
27402078Sgjelinek 
27412078Sgjelinek 	if ((handle = zonecfg_init_handle()) == NULL) {
27422078Sgjelinek 		zperror(cmd_to_str(cmd_num), B_TRUE);
27432078Sgjelinek 		return (Z_ERR);
27442078Sgjelinek 	}
27452078Sgjelinek 	if ((err = zonecfg_get_handle(target_zone, handle)) != Z_OK) {
27462078Sgjelinek 		errno = err;
27472078Sgjelinek 		zperror(cmd_to_str(cmd_num), B_TRUE);
27482078Sgjelinek 		zonecfg_fini_handle(handle);
27492078Sgjelinek 		return (Z_ERR);
27502078Sgjelinek 	}
27512078Sgjelinek 	if ((err = zonecfg_get_zonepath(handle, zonepath, sizeof (zonepath))) !=
27522078Sgjelinek 	    Z_OK) {
27532078Sgjelinek 		errno = err;
27542078Sgjelinek 		zperror(cmd_to_str(cmd_num), B_TRUE);
27552078Sgjelinek 		zonecfg_fini_handle(handle);
27562078Sgjelinek 		return (Z_ERR);
27572078Sgjelinek 	}
27582078Sgjelinek 	/*
27592078Sgjelinek 	 * zonecfg_get_zonepath() gets its data from the XML repository.
27602078Sgjelinek 	 * Verify this against the index file, which is checked first by
27612078Sgjelinek 	 * zone_get_zonepath().  If they don't match, bail out.
27622078Sgjelinek 	 */
27632078Sgjelinek 	if ((err = zone_get_zonepath(target_zone, checkpath,
27642078Sgjelinek 	    sizeof (checkpath))) != Z_OK) {
27652078Sgjelinek 		errno = err;
27662078Sgjelinek 		zperror2(target_zone, gettext("could not get zone path"));
27673716Sgjelinek 		zonecfg_fini_handle(handle);
27682078Sgjelinek 		return (Z_ERR);
27692078Sgjelinek 	}
27702078Sgjelinek 	if (strcmp(zonepath, checkpath) != 0) {
27712078Sgjelinek 		/*
27722078Sgjelinek 		 * TRANSLATION_NOTE
27732078Sgjelinek 		 * XML and zonepath are literals that should not be translated.
27742078Sgjelinek 		 */
27752078Sgjelinek 		(void) fprintf(stderr, gettext("The XML repository has "
27762078Sgjelinek 		    "zonepath '%s',\nbut the index file has zonepath '%s'.\n"
27772078Sgjelinek 		    "These must match, so fix the incorrect entry.\n"),
27782078Sgjelinek 		    zonepath, checkpath);
27793716Sgjelinek 		zonecfg_fini_handle(handle);
27802078Sgjelinek 		return (Z_ERR);
27812078Sgjelinek 	}
27822078Sgjelinek 	if (validate_zonepath(zonepath, cmd_num) != Z_OK) {
27832078Sgjelinek 		(void) fprintf(stderr, gettext("could not verify zonepath %s "
27842078Sgjelinek 		    "because of the above errors.\n"), zonepath);
27852078Sgjelinek 		return_code = Z_ERR;
27862078Sgjelinek 	}
27872078Sgjelinek 
27883339Szt129084 	if (verify_handle(cmd_num, handle, argv) != Z_OK)
27892078Sgjelinek 		return_code = Z_ERR;
27902078Sgjelinek 
27910Sstevel@tonic-gate 	zonecfg_fini_handle(handle);
27920Sstevel@tonic-gate 	if (return_code == Z_ERR)
27930Sstevel@tonic-gate 		(void) fprintf(stderr,
27940Sstevel@tonic-gate 		    gettext("%s: zone %s failed to verify\n"),
27950Sstevel@tonic-gate 		    execname, target_zone);
27960Sstevel@tonic-gate 	return (return_code);
27970Sstevel@tonic-gate }
27980Sstevel@tonic-gate 
27990Sstevel@tonic-gate static int
28000Sstevel@tonic-gate verify_func(int argc, char *argv[])
28010Sstevel@tonic-gate {
28020Sstevel@tonic-gate 	int arg;
28030Sstevel@tonic-gate 
28040Sstevel@tonic-gate 	optind = 0;
28050Sstevel@tonic-gate 	if ((arg = getopt(argc, argv, "?")) != EOF) {
28060Sstevel@tonic-gate 		switch (arg) {
28070Sstevel@tonic-gate 		case '?':
28080Sstevel@tonic-gate 			sub_usage(SHELP_VERIFY, CMD_VERIFY);
28090Sstevel@tonic-gate 			return (optopt == '?' ? Z_OK : Z_USAGE);
28100Sstevel@tonic-gate 		default:
28110Sstevel@tonic-gate 			sub_usage(SHELP_VERIFY, CMD_VERIFY);
28120Sstevel@tonic-gate 			return (Z_USAGE);
28130Sstevel@tonic-gate 		}
28140Sstevel@tonic-gate 	}
28150Sstevel@tonic-gate 	if (argc > optind) {
28160Sstevel@tonic-gate 		sub_usage(SHELP_VERIFY, CMD_VERIFY);
28170Sstevel@tonic-gate 		return (Z_USAGE);
28180Sstevel@tonic-gate 	}
28192712Snn35248 	if (sanity_check(target_zone, CMD_VERIFY, B_FALSE, B_FALSE, B_FALSE)
28202712Snn35248 	    != Z_OK)
28210Sstevel@tonic-gate 		return (Z_ERR);
28223339Szt129084 	return (verify_details(CMD_VERIFY, argv));
28230Sstevel@tonic-gate }
28240Sstevel@tonic-gate 
28252712Snn35248 static int
28267089Sgjelinek addoptions(char *buf, char *argv[], size_t len)
28277089Sgjelinek {
28287089Sgjelinek 	int i = 0;
28297089Sgjelinek 
28307089Sgjelinek 	if (buf[0] == '\0')
28317089Sgjelinek 		return (Z_OK);
28327089Sgjelinek 
28337089Sgjelinek 	while (argv[i] != NULL) {
28347089Sgjelinek 		if (strlcat(buf, " ", len) >= len ||
28357089Sgjelinek 		    strlcat(buf, argv[i++], len) >= len) {
28367089Sgjelinek 			zerror("Command line too long");
28377089Sgjelinek 			return (Z_ERR);
28387089Sgjelinek 		}
28397089Sgjelinek 	}
28407089Sgjelinek 
28417089Sgjelinek 	return (Z_OK);
28427089Sgjelinek }
28437089Sgjelinek 
28447089Sgjelinek static int
28452712Snn35248 addopt(char *buf, int opt, char *optarg, size_t bufsize)
28462712Snn35248 {
28472712Snn35248 	char optstring[4];
28482712Snn35248 
28492712Snn35248 	if (opt > 0)
28502712Snn35248 		(void) sprintf(optstring, " -%c", opt);
28512712Snn35248 	else
28522712Snn35248 		(void) strcpy(optstring, " ");
28532712Snn35248 
28542712Snn35248 	if ((strlcat(buf, optstring, bufsize) > bufsize))
28552712Snn35248 		return (Z_ERR);
28567089Sgjelinek 
28572712Snn35248 	if ((optarg != NULL) && (strlcat(buf, optarg, bufsize) > bufsize))
28582712Snn35248 		return (Z_ERR);
28597089Sgjelinek 
28602712Snn35248 	return (Z_OK);
28612712Snn35248 }
28620Sstevel@tonic-gate 
28637089Sgjelinek /* ARGSUSED */
28640Sstevel@tonic-gate static int
28650Sstevel@tonic-gate install_func(int argc, char *argv[])
28660Sstevel@tonic-gate {
28672712Snn35248 	char cmdbuf[MAXPATHLEN];
28684586Sgjelinek 	char postcmdbuf[MAXPATHLEN];
28690Sstevel@tonic-gate 	int lockfd;
28702712Snn35248 	int arg, err, subproc_err;
28710Sstevel@tonic-gate 	char zonepath[MAXPATHLEN];
28722727Sedp 	brand_handle_t bh = NULL;
28730Sstevel@tonic-gate 	int status;
28741867Sgjelinek 	boolean_t nodataset = B_FALSE;
28754586Sgjelinek 	boolean_t do_postinstall = B_FALSE;
28767089Sgjelinek 	boolean_t brand_help = B_FALSE;
28772712Snn35248 	char opts[128];
28782712Snn35248 
28792712Snn35248 	if (target_zone == NULL) {
28802712Snn35248 		sub_usage(SHELP_INSTALL, CMD_INSTALL);
28812712Snn35248 		return (Z_USAGE);
28822712Snn35248 	}
28830Sstevel@tonic-gate 
2884766Scarlsonj 	if (zonecfg_in_alt_root()) {
2885766Scarlsonj 		zerror(gettext("cannot install zone in alternate root"));
2886766Scarlsonj 		return (Z_ERR);
2887766Scarlsonj 	}
2888766Scarlsonj 
28892712Snn35248 	if ((err = zone_get_zonepath(target_zone, zonepath,
28902712Snn35248 	    sizeof (zonepath))) != Z_OK) {
28912712Snn35248 		errno = err;
28922712Snn35248 		zperror2(target_zone, gettext("could not get zone path"));
28932712Snn35248 		return (Z_ERR);
28942712Snn35248 	}
28952712Snn35248 
28962712Snn35248 	/* Fetch the install command from the brand configuration.  */
28972727Sedp 	if ((bh = brand_open(target_brand)) == NULL) {
28982712Snn35248 		zerror(gettext("missing or invalid brand"));
28992712Snn35248 		return (Z_ERR);
29002712Snn35248 	}
29012712Snn35248 
29027089Sgjelinek 	if (get_hook(bh, cmdbuf, sizeof (cmdbuf), brand_get_install,
29037089Sgjelinek 	    target_zone, zonepath) != Z_OK) {
29042712Snn35248 		zerror("invalid brand configuration: missing install resource");
29052727Sedp 		brand_close(bh);
29062712Snn35248 		return (Z_ERR);
29072712Snn35248 	}
29082712Snn35248 
29097089Sgjelinek 	if (get_hook(bh, postcmdbuf, sizeof (postcmdbuf), brand_get_postinstall,
29107089Sgjelinek 	    target_zone, zonepath) != Z_OK) {
29114586Sgjelinek 		zerror("invalid brand configuration: missing postinstall "
29124586Sgjelinek 		    "resource");
29134586Sgjelinek 		brand_close(bh);
29144586Sgjelinek 		return (Z_ERR);
29157089Sgjelinek 	}
29167089Sgjelinek 
29177089Sgjelinek 	if (postcmdbuf[0] != '\0')
29184586Sgjelinek 		do_postinstall = B_TRUE;
29194586Sgjelinek 
29202712Snn35248 	(void) strcpy(opts, "?x:");
29217089Sgjelinek 	/*
29227089Sgjelinek 	 * Fetch the list of recognized command-line options from
29237089Sgjelinek 	 * the brand configuration file.
29247089Sgjelinek 	 */
29257089Sgjelinek 	if (brand_get_installopts(bh, opts + strlen(opts),
29267089Sgjelinek 	    sizeof (opts) - strlen(opts)) != 0) {
29277089Sgjelinek 		zerror("invalid brand configuration: missing "
29287089Sgjelinek 		    "install options resource");
29297089Sgjelinek 		brand_close(bh);
29307089Sgjelinek 		return (Z_ERR);
29317089Sgjelinek 	}
29327089Sgjelinek 
29332727Sedp 	brand_close(bh);
29342712Snn35248 
29357089Sgjelinek 	if (cmdbuf[0] == '\0') {
29367089Sgjelinek 		zerror("Missing brand install command");
29377089Sgjelinek 		return (Z_ERR);
29387089Sgjelinek 	}
29397089Sgjelinek 
29407089Sgjelinek 	/* Check the argv string for args we handle internally */
29410Sstevel@tonic-gate 	optind = 0;
29427089Sgjelinek 	opterr = 0;
29432712Snn35248 	while ((arg = getopt(argc, argv, opts)) != EOF) {
29440Sstevel@tonic-gate 		switch (arg) {
29450Sstevel@tonic-gate 		case '?':
29467089Sgjelinek 			if (optopt == '?') {
29471867Sgjelinek 				sub_usage(SHELP_INSTALL, CMD_INSTALL);
29487089Sgjelinek 				brand_help = B_TRUE;
29492712Snn35248 			}
29507089Sgjelinek 			/* Ignore unknown options - may be brand specific. */
29517089Sgjelinek 			break;
29527089Sgjelinek 		case 'x':
29537089Sgjelinek 			/* Handle this option internally, don't pass to brand */
29547089Sgjelinek 			if (strcmp(optarg, "nodataset") == 0) {
29557089Sgjelinek 				/* Handle this option internally */
29567089Sgjelinek 				nodataset = B_TRUE;
29572712Snn35248 			}
29587089Sgjelinek 			continue;
29597089Sgjelinek 		default:
29607089Sgjelinek 			/* Ignore unknown options - may be brand specific. */
29612712Snn35248 			break;
29620Sstevel@tonic-gate 		}
29637089Sgjelinek 
29647089Sgjelinek 		/*
29657089Sgjelinek 		 * Append the option to the command line passed to the
29667089Sgjelinek 		 * brand-specific install and postinstall routines.
29677089Sgjelinek 		 */
29687089Sgjelinek 		if (addopt(cmdbuf, optopt, optarg, sizeof (cmdbuf)) != Z_OK) {
29697089Sgjelinek 			zerror("Install command line too long");
29707089Sgjelinek 			return (Z_ERR);
29717089Sgjelinek 		}
29727089Sgjelinek 		if (addopt(postcmdbuf, optopt, optarg, sizeof (postcmdbuf))
29737089Sgjelinek 		    != Z_OK) {
29747089Sgjelinek 			zerror("Post-Install command line too long");
29757089Sgjelinek 			return (Z_ERR);
29767089Sgjelinek 		}
29777089Sgjelinek 	}
29787089Sgjelinek 
29797089Sgjelinek 	for (; optind < argc; optind++) {
29807089Sgjelinek 		if (addopt(cmdbuf, 0, argv[optind], sizeof (cmdbuf)) != Z_OK) {
29817089Sgjelinek 			zerror("Install command line too long");
29827089Sgjelinek 			return (Z_ERR);
29837089Sgjelinek 		}
29847089Sgjelinek 
29857089Sgjelinek 		if (addopt(postcmdbuf, 0, argv[optind], sizeof (postcmdbuf))
29867089Sgjelinek 		    != Z_OK) {
29877089Sgjelinek 			zerror("Post-Install command line too long");
29887089Sgjelinek 			return (Z_ERR);
29892712Snn35248 		}
29902712Snn35248 	}
29912712Snn35248 
29927089Sgjelinek 	if (!brand_help) {
29937089Sgjelinek 		if (sanity_check(target_zone, CMD_INSTALL, B_FALSE, B_TRUE,
29947089Sgjelinek 		    B_FALSE) != Z_OK)
29957089Sgjelinek 			return (Z_ERR);
29967089Sgjelinek 		if (verify_details(CMD_INSTALL, argv) != Z_OK)
29977089Sgjelinek 			return (Z_ERR);
29987089Sgjelinek 
29997089Sgjelinek 		if (zonecfg_grab_lock_file(target_zone, &lockfd) != Z_OK) {
30007089Sgjelinek 			zerror(gettext("another %s may have an operation in "
30017089Sgjelinek 			    "progress."), "zoneadm");
30027089Sgjelinek 			return (Z_ERR);
30037089Sgjelinek 		}
30047089Sgjelinek 		err = zone_set_state(target_zone, ZONE_STATE_INCOMPLETE);
30057089Sgjelinek 		if (err != Z_OK) {
30067089Sgjelinek 			errno = err;
30077089Sgjelinek 			zperror2(target_zone, gettext("could not set state"));
30087089Sgjelinek 			goto done;
30097089Sgjelinek 		}
30107089Sgjelinek 
30117089Sgjelinek 		if (!nodataset)
30127089Sgjelinek 			create_zfs_zonepath(zonepath);
30137089Sgjelinek 	}
30147089Sgjelinek 
30157089Sgjelinek 	status = do_subproc_interactive(cmdbuf);
30162712Snn35248 	if ((subproc_err =
30172712Snn35248 	    subproc_status(gettext("brand-specific installation"), status,
30182712Snn35248 	    B_FALSE)) != ZONE_SUBPROC_OK) {
30197089Sgjelinek 		if (subproc_err == ZONE_SUBPROC_USAGE && !brand_help) {
30207089Sgjelinek 			sub_usage(SHELP_INSTALL, CMD_INSTALL);
30217089Sgjelinek 			zonecfg_release_lock_file(target_zone, lockfd);
30227089Sgjelinek 			return (Z_ERR);
30237089Sgjelinek 		}
30242712Snn35248 		err = Z_ERR;
30250Sstevel@tonic-gate 		goto done;
30262712Snn35248 	}
30270Sstevel@tonic-gate 
30287089Sgjelinek 	if (brand_help)
30297089Sgjelinek 		return (Z_OK);
30307089Sgjelinek 
30310Sstevel@tonic-gate 	if ((err = zone_set_state(target_zone, ZONE_STATE_INSTALLED)) != Z_OK) {
30320Sstevel@tonic-gate 		errno = err;
30330Sstevel@tonic-gate 		zperror2(target_zone, gettext("could not set state"));
30340Sstevel@tonic-gate 		goto done;
30350Sstevel@tonic-gate 	}
30360Sstevel@tonic-gate 
30374586Sgjelinek 	if (do_postinstall) {
30384586Sgjelinek 		status = do_subproc(postcmdbuf);
30394586Sgjelinek 
30404586Sgjelinek 		if ((subproc_err =
30414586Sgjelinek 		    subproc_status(gettext("brand-specific post-install"),
30424586Sgjelinek 		    status, B_FALSE)) != ZONE_SUBPROC_OK) {
30434586Sgjelinek 			err = Z_ERR;
30444586Sgjelinek 			(void) zone_set_state(target_zone,
30454586Sgjelinek 			    ZONE_STATE_INCOMPLETE);
30464586Sgjelinek 		}
30474586Sgjelinek 	}
30484586Sgjelinek 
30490Sstevel@tonic-gate done:
30502712Snn35248 	/*
30517089Sgjelinek 	 * If the install script exited with ZONE_SUBPROC_NOTCOMPLETE, try to
30527089Sgjelinek 	 * clean up the zone and leave the zone in the CONFIGURED state so that
30537089Sgjelinek 	 * another install can be attempted without requiring an uninstall
30547089Sgjelinek 	 * first.
30552712Snn35248 	 */
30567089Sgjelinek 	if (subproc_err == ZONE_SUBPROC_NOTCOMPLETE) {
30572712Snn35248 		if ((err = cleanup_zonepath(zonepath, B_FALSE)) != Z_OK) {
30582712Snn35248 			errno = err;
30592712Snn35248 			zperror2(target_zone,
30602712Snn35248 			    gettext("cleaning up zonepath failed"));
30612712Snn35248 		} else if ((err = zone_set_state(target_zone,
30622712Snn35248 		    ZONE_STATE_CONFIGURED)) != Z_OK) {
30632712Snn35248 			errno = err;
30642712Snn35248 			zperror2(target_zone, gettext("could not set state"));
30652712Snn35248 		}
30662712Snn35248 	}
30672712Snn35248 
30687089Sgjelinek 	if (!brand_help)
30697089Sgjelinek 		zonecfg_release_lock_file(target_zone, lockfd);
30700Sstevel@tonic-gate 	return ((err == Z_OK) ? Z_OK : Z_ERR);
30710Sstevel@tonic-gate }
30720Sstevel@tonic-gate 
30730Sstevel@tonic-gate /*
30741300Sgjelinek  * Check that the inherited pkg dirs are the same for the clone and its source.
30751300Sgjelinek  * The easiest way to do that is check that the list of ipds is the same
30761300Sgjelinek  * by matching each one against the other.  This algorithm should be fine since
30771300Sgjelinek  * the list of ipds should not be that long.
30781300Sgjelinek  */
30791300Sgjelinek static int
30801300Sgjelinek valid_ipd_clone(zone_dochandle_t s_handle, char *source_zone,
30811300Sgjelinek 	zone_dochandle_t t_handle, char *target_zone)
30821300Sgjelinek {
30831300Sgjelinek 	int err;
30841300Sgjelinek 	int res = Z_OK;
30851300Sgjelinek 	int s_cnt = 0;
30861300Sgjelinek 	int t_cnt = 0;
30871300Sgjelinek 	struct zone_fstab s_fstab;
30881300Sgjelinek 	struct zone_fstab t_fstab;
30891300Sgjelinek 
30901300Sgjelinek 	/*
30911300Sgjelinek 	 * First check the source of the clone against the target.
30921300Sgjelinek 	 */
30931300Sgjelinek 	if ((err = zonecfg_setipdent(s_handle)) != Z_OK) {
30941300Sgjelinek 		errno = err;
30951300Sgjelinek 		zperror2(source_zone, gettext("could not enumerate "
30961300Sgjelinek 		    "inherit-pkg-dirs"));
30971300Sgjelinek 		return (Z_ERR);
30981300Sgjelinek 	}
30991300Sgjelinek 
31001300Sgjelinek 	while (zonecfg_getipdent(s_handle, &s_fstab) == Z_OK) {
31011300Sgjelinek 		boolean_t match = B_FALSE;
31021300Sgjelinek 
31031300Sgjelinek 		s_cnt++;
31041300Sgjelinek 
31051300Sgjelinek 		if ((err = zonecfg_setipdent(t_handle)) != Z_OK) {
31061300Sgjelinek 			errno = err;
31071300Sgjelinek 			zperror2(target_zone, gettext("could not enumerate "
31081300Sgjelinek 			    "inherit-pkg-dirs"));
31091300Sgjelinek 			(void) zonecfg_endipdent(s_handle);
31101300Sgjelinek 			return (Z_ERR);
31111300Sgjelinek 		}
31121300Sgjelinek 
31131300Sgjelinek 		while (zonecfg_getipdent(t_handle, &t_fstab) == Z_OK) {
31141300Sgjelinek 			if (strcmp(s_fstab.zone_fs_dir, t_fstab.zone_fs_dir)
31151300Sgjelinek 			    == 0) {
31161300Sgjelinek 				match = B_TRUE;
31171300Sgjelinek 				break;
31181300Sgjelinek 			}
31191300Sgjelinek 		}
31201300Sgjelinek 		(void) zonecfg_endipdent(t_handle);
31211300Sgjelinek 
31221300Sgjelinek 		if (!match) {
31231300Sgjelinek 			(void) fprintf(stderr, gettext("inherit-pkg-dir "
31241300Sgjelinek 			    "'%s' is not configured in zone %s.\n"),
31251300Sgjelinek 			    s_fstab.zone_fs_dir, target_zone);
31261300Sgjelinek 			res = Z_ERR;
31271300Sgjelinek 		}
31281300Sgjelinek 	}
31291300Sgjelinek 
31301300Sgjelinek 	(void) zonecfg_endipdent(s_handle);
31311300Sgjelinek 
31321300Sgjelinek 	/* skip the next check if we already have errors */
31331300Sgjelinek 	if (res == Z_ERR)
31341300Sgjelinek 		return (res);
31351300Sgjelinek 
31361300Sgjelinek 	/*
31371300Sgjelinek 	 * Now check the number of ipds in the target so we can verify
31381300Sgjelinek 	 * that the source is not a subset of the target.
31391300Sgjelinek 	 */
31401300Sgjelinek 	if ((err = zonecfg_setipdent(t_handle)) != Z_OK) {
31411300Sgjelinek 		errno = err;
31421300Sgjelinek 		zperror2(target_zone, gettext("could not enumerate "
31431300Sgjelinek 		    "inherit-pkg-dirs"));
31441300Sgjelinek 		return (Z_ERR);
31451300Sgjelinek 	}
31461300Sgjelinek 
31471300Sgjelinek 	while (zonecfg_getipdent(t_handle, &t_fstab) == Z_OK)
31481300Sgjelinek 		t_cnt++;
31491300Sgjelinek 
31501300Sgjelinek 	(void) zonecfg_endipdent(t_handle);
31511300Sgjelinek 
31521300Sgjelinek 	if (t_cnt != s_cnt) {
31531300Sgjelinek 		(void) fprintf(stderr, gettext("Zone %s is configured "
31541300Sgjelinek 		    "with inherit-pkg-dirs that are not configured in zone "
31551300Sgjelinek 		    "%s.\n"), target_zone, source_zone);
31561300Sgjelinek 		res = Z_ERR;
31571300Sgjelinek 	}
31581300Sgjelinek 
31591300Sgjelinek 	return (res);
31601300Sgjelinek }
31611300Sgjelinek 
31621300Sgjelinek static void
31631300Sgjelinek warn_dev_match(zone_dochandle_t s_handle, char *source_zone,
31641300Sgjelinek 	zone_dochandle_t t_handle, char *target_zone)
31651300Sgjelinek {
31661300Sgjelinek 	int err;
31671300Sgjelinek 	struct zone_devtab s_devtab;
31681300Sgjelinek 	struct zone_devtab t_devtab;
31691300Sgjelinek 
31701300Sgjelinek 	if ((err = zonecfg_setdevent(t_handle)) != Z_OK) {
31711300Sgjelinek 		errno = err;
31721300Sgjelinek 		zperror2(target_zone, gettext("could not enumerate devices"));
31731300Sgjelinek 		return;
31741300Sgjelinek 	}
31751300Sgjelinek 
31761300Sgjelinek 	while (zonecfg_getdevent(t_handle, &t_devtab) == Z_OK) {
31771300Sgjelinek 		if ((err = zonecfg_setdevent(s_handle)) != Z_OK) {
31781300Sgjelinek 			errno = err;
31791300Sgjelinek 			zperror2(source_zone,
31801300Sgjelinek 			    gettext("could not enumerate devices"));
31811300Sgjelinek 			(void) zonecfg_enddevent(t_handle);
31821300Sgjelinek 			return;
31831300Sgjelinek 		}
31841300Sgjelinek 
31851300Sgjelinek 		while (zonecfg_getdevent(s_handle, &s_devtab) == Z_OK) {
31861300Sgjelinek 			/*
31871300Sgjelinek 			 * Use fnmatch to catch the case where wildcards
31881300Sgjelinek 			 * were used in one zone and the other has an
31891300Sgjelinek 			 * explicit entry (e.g. /dev/dsk/c0t0d0s6 vs.
31901300Sgjelinek 			 * /dev/\*dsk/c0t0d0s6).
31911300Sgjelinek 			 */
31921300Sgjelinek 			if (fnmatch(t_devtab.zone_dev_match,
31931300Sgjelinek 			    s_devtab.zone_dev_match, FNM_PATHNAME) == 0 ||
31941300Sgjelinek 			    fnmatch(s_devtab.zone_dev_match,
31951300Sgjelinek 			    t_devtab.zone_dev_match, FNM_PATHNAME) == 0) {
31961300Sgjelinek 				(void) fprintf(stderr,
31971300Sgjelinek 				    gettext("WARNING: device '%s' "
31981300Sgjelinek 				    "is configured in both zones.\n"),
31991300Sgjelinek 				    t_devtab.zone_dev_match);
32001300Sgjelinek 				break;
32011300Sgjelinek 			}
32021300Sgjelinek 		}
32031300Sgjelinek 		(void) zonecfg_enddevent(s_handle);
32041300Sgjelinek 	}
32051300Sgjelinek 
32061300Sgjelinek 	(void) zonecfg_enddevent(t_handle);
32071300Sgjelinek }
32081300Sgjelinek 
32091300Sgjelinek /*
32101300Sgjelinek  * Check if the specified mount option (opt) is contained within the
32111300Sgjelinek  * options string.
32121300Sgjelinek  */
32131300Sgjelinek static boolean_t
32141300Sgjelinek opt_match(char *opt, char *options)
32151300Sgjelinek {
32161300Sgjelinek 	char *p;
32171300Sgjelinek 	char *lastp;
32181300Sgjelinek 
32191300Sgjelinek 	if ((p = strtok_r(options, ",", &lastp)) != NULL) {
32201300Sgjelinek 		if (strcmp(p, opt) == 0)
32211300Sgjelinek 			return (B_TRUE);
32221300Sgjelinek 		while ((p = strtok_r(NULL, ",", &lastp)) != NULL) {
32231300Sgjelinek 			if (strcmp(p, opt) == 0)
32241300Sgjelinek 				return (B_TRUE);
32251300Sgjelinek 		}
32261300Sgjelinek 	}
32271300Sgjelinek 
32281300Sgjelinek 	return (B_FALSE);
32291300Sgjelinek }
32301300Sgjelinek 
32311867Sgjelinek #define	RW_LOFS	"WARNING: read-write lofs file system on '%s' is configured " \
32321300Sgjelinek 	"in both zones.\n"
32331300Sgjelinek 
32341300Sgjelinek static void
32351300Sgjelinek print_fs_warnings(struct zone_fstab *s_fstab, struct zone_fstab *t_fstab)
32361300Sgjelinek {
32371300Sgjelinek 	/*
32381300Sgjelinek 	 * It is ok to have shared lofs mounted fs but we want to warn if
32391300Sgjelinek 	 * either is rw since this will effect the other zone.
32401300Sgjelinek 	 */
32411300Sgjelinek 	if (strcmp(t_fstab->zone_fs_type, "lofs") == 0) {
32421300Sgjelinek 		zone_fsopt_t *optp;
32431300Sgjelinek 
32441300Sgjelinek 		/* The default is rw so no options means rw */
32451300Sgjelinek 		if (t_fstab->zone_fs_options == NULL ||
32461300Sgjelinek 		    s_fstab->zone_fs_options == NULL) {
32471300Sgjelinek 			(void) fprintf(stderr, gettext(RW_LOFS),
32481300Sgjelinek 			    t_fstab->zone_fs_special);
32491300Sgjelinek 			return;
32501300Sgjelinek 		}
32511300Sgjelinek 
32521300Sgjelinek 		for (optp = s_fstab->zone_fs_options; optp != NULL;
32531300Sgjelinek 		    optp = optp->zone_fsopt_next) {
32541300Sgjelinek 			if (opt_match("rw", optp->zone_fsopt_opt)) {
32551300Sgjelinek 				(void) fprintf(stderr, gettext(RW_LOFS),
32561300Sgjelinek 				    s_fstab->zone_fs_special);
32571300Sgjelinek 				return;
32581300Sgjelinek 			}
32591300Sgjelinek 		}
32601300Sgjelinek 
32611300Sgjelinek 		for (optp = t_fstab->zone_fs_options; optp != NULL;
32621300Sgjelinek 		    optp = optp->zone_fsopt_next) {
32631300Sgjelinek 			if (opt_match("rw", optp->zone_fsopt_opt)) {
32641300Sgjelinek 				(void) fprintf(stderr, gettext(RW_LOFS),
32651300Sgjelinek 				    t_fstab->zone_fs_special);
32661300Sgjelinek 				return;
32671300Sgjelinek 			}
32681300Sgjelinek 		}
32691300Sgjelinek 
32701300Sgjelinek 		return;
32711300Sgjelinek 	}
32721300Sgjelinek 
32731300Sgjelinek 	/*
32741300Sgjelinek 	 * TRANSLATION_NOTE
32751867Sgjelinek 	 * The first variable is the file system type and the second is
32761867Sgjelinek 	 * the file system special device.  For example,
32771867Sgjelinek 	 * WARNING: ufs file system on '/dev/dsk/c0t0d0s0' ...
32781300Sgjelinek 	 */
32791867Sgjelinek 	(void) fprintf(stderr, gettext("WARNING: %s file system on '%s' "
32801300Sgjelinek 	    "is configured in both zones.\n"), t_fstab->zone_fs_type,
32811300Sgjelinek 	    t_fstab->zone_fs_special);
32821300Sgjelinek }
32831300Sgjelinek 
32841300Sgjelinek static void
32851300Sgjelinek warn_fs_match(zone_dochandle_t s_handle, char *source_zone,
32861300Sgjelinek 	zone_dochandle_t t_handle, char *target_zone)
32871300Sgjelinek {
32881300Sgjelinek 	int err;
32891300Sgjelinek 	struct zone_fstab s_fstab;
32901300Sgjelinek 	struct zone_fstab t_fstab;
32911300Sgjelinek 
32921300Sgjelinek 	if ((err = zonecfg_setfsent(t_handle)) != Z_OK) {
32931300Sgjelinek 		errno = err;
32941300Sgjelinek 		zperror2(target_zone,
32951867Sgjelinek 		    gettext("could not enumerate file systems"));
32961300Sgjelinek 		return;
32971300Sgjelinek 	}
32981300Sgjelinek 
32991300Sgjelinek 	while (zonecfg_getfsent(t_handle, &t_fstab) == Z_OK) {
33001300Sgjelinek 		if ((err = zonecfg_setfsent(s_handle)) != Z_OK) {
33011300Sgjelinek 			errno = err;
33021300Sgjelinek 			zperror2(source_zone,
33031867Sgjelinek 			    gettext("could not enumerate file systems"));
33041300Sgjelinek 			(void) zonecfg_endfsent(t_handle);
33051300Sgjelinek 			return;
33061300Sgjelinek 		}
33071300Sgjelinek 
33081300Sgjelinek 		while (zonecfg_getfsent(s_handle, &s_fstab) == Z_OK) {
33091300Sgjelinek 			if (strcmp(t_fstab.zone_fs_special,
33101300Sgjelinek 			    s_fstab.zone_fs_special) == 0) {
33111300Sgjelinek 				print_fs_warnings(&s_fstab, &t_fstab);
33121300Sgjelinek 				break;
33131300Sgjelinek 			}
33141300Sgjelinek 		}
33151300Sgjelinek 		(void) zonecfg_endfsent(s_handle);
33161300Sgjelinek 	}
33171300Sgjelinek 
33181300Sgjelinek 	(void) zonecfg_endfsent(t_handle);
33191300Sgjelinek }
33201300Sgjelinek 
33211300Sgjelinek /*
33221300Sgjelinek  * We don't catch the case where you used the same IP address but
33231300Sgjelinek  * it is not an exact string match.  For example, 192.9.0.128 vs. 192.09.0.128.
33241300Sgjelinek  * However, we're not going to worry about that but we will check for
33251300Sgjelinek  * a possible netmask on one of the addresses (e.g. 10.0.0.1 and 10.0.0.1/24)
33261300Sgjelinek  * and handle that case as a match.
33271300Sgjelinek  */
33281300Sgjelinek static void
33291300Sgjelinek warn_ip_match(zone_dochandle_t s_handle, char *source_zone,
33301300Sgjelinek 	zone_dochandle_t t_handle, char *target_zone)
33311300Sgjelinek {
33321300Sgjelinek 	int err;
33331300Sgjelinek 	struct zone_nwiftab s_nwiftab;
33341300Sgjelinek 	struct zone_nwiftab t_nwiftab;
33351300Sgjelinek 
33361300Sgjelinek 	if ((err = zonecfg_setnwifent(t_handle)) != Z_OK) {
33371300Sgjelinek 		errno = err;
33381300Sgjelinek 		zperror2(target_zone,
33391300Sgjelinek 		    gettext("could not enumerate network interfaces"));
33401300Sgjelinek 		return;
33411300Sgjelinek 	}
33421300Sgjelinek 
33431300Sgjelinek 	while (zonecfg_getnwifent(t_handle, &t_nwiftab) == Z_OK) {
33441300Sgjelinek 		char *p;
33451300Sgjelinek 
33461300Sgjelinek 		/* remove an (optional) netmask from the address */
33471300Sgjelinek 		if ((p = strchr(t_nwiftab.zone_nwif_address, '/')) != NULL)
33481300Sgjelinek 			*p = '\0';
33491300Sgjelinek 
33501300Sgjelinek 		if ((err = zonecfg_setnwifent(s_handle)) != Z_OK) {
33511300Sgjelinek 			errno = err;
33521300Sgjelinek 			zperror2(source_zone,
33531300Sgjelinek 			    gettext("could not enumerate network interfaces"));
33541300Sgjelinek 			(void) zonecfg_endnwifent(t_handle);
33551300Sgjelinek 			return;
33561300Sgjelinek 		}
33571300Sgjelinek 
33581300Sgjelinek 		while (zonecfg_getnwifent(s_handle, &s_nwiftab) == Z_OK) {
33591300Sgjelinek 			/* remove an (optional) netmask from the address */
33601300Sgjelinek 			if ((p = strchr(s_nwiftab.zone_nwif_address, '/'))
33611300Sgjelinek 			    != NULL)
33621300Sgjelinek 				*p = '\0';
33631300Sgjelinek 
33643448Sdh155122 			/* For exclusive-IP zones, address is not specified. */
33653448Sdh155122 			if (strlen(s_nwiftab.zone_nwif_address) == 0)
33663448Sdh155122 				continue;
33673448Sdh155122 
33681300Sgjelinek 			if (strcmp(t_nwiftab.zone_nwif_address,
33691300Sgjelinek 			    s_nwiftab.zone_nwif_address) == 0) {
33701300Sgjelinek 				(void) fprintf(stderr,
33711300Sgjelinek 				    gettext("WARNING: network address '%s' "
33721300Sgjelinek 				    "is configured in both zones.\n"),
33731300Sgjelinek 				    t_nwiftab.zone_nwif_address);
33741300Sgjelinek 				break;
33751300Sgjelinek 			}
33761300Sgjelinek 		}
33771300Sgjelinek 		(void) zonecfg_endnwifent(s_handle);
33781300Sgjelinek 	}
33791300Sgjelinek 
33801300Sgjelinek 	(void) zonecfg_endnwifent(t_handle);
33811300Sgjelinek }
33821300Sgjelinek 
33831300Sgjelinek static void
33842712Snn35248 warn_dataset_match(zone_dochandle_t s_handle, char *source,
33852712Snn35248 	zone_dochandle_t t_handle, char *target)
33861300Sgjelinek {
33871300Sgjelinek 	int err;
33881300Sgjelinek 	struct zone_dstab s_dstab;
33891300Sgjelinek 	struct zone_dstab t_dstab;
33901300Sgjelinek 
33911300Sgjelinek 	if ((err = zonecfg_setdsent(t_handle)) != Z_OK) {
33921300Sgjelinek 		errno = err;
33932712Snn35248 		zperror2(target, gettext("could not enumerate datasets"));
33941300Sgjelinek 		return;
33951300Sgjelinek 	}
33961300Sgjelinek 
33971300Sgjelinek 	while (zonecfg_getdsent(t_handle, &t_dstab) == Z_OK) {
33981300Sgjelinek 		if ((err = zonecfg_setdsent(s_handle)) != Z_OK) {
33991300Sgjelinek 			errno = err;
34002712Snn35248 			zperror2(source,
34011300Sgjelinek 			    gettext("could not enumerate datasets"));
34021300Sgjelinek 			(void) zonecfg_enddsent(t_handle);
34031300Sgjelinek 			return;
34041300Sgjelinek 		}
34051300Sgjelinek 
34061300Sgjelinek 		while (zonecfg_getdsent(s_handle, &s_dstab) == Z_OK) {
34071300Sgjelinek 			if (strcmp(t_dstab.zone_dataset_name,
34081300Sgjelinek 			    s_dstab.zone_dataset_name) == 0) {
34092712Snn35248 				target_zone = source;
34102712Snn35248 				zerror(gettext("WARNING: dataset '%s' "
34111300Sgjelinek 				    "is configured in both zones.\n"),
34121300Sgjelinek 				    t_dstab.zone_dataset_name);
34131300Sgjelinek 				break;
34141300Sgjelinek 			}
34151300Sgjelinek 		}
34161300Sgjelinek 		(void) zonecfg_enddsent(s_handle);
34171300Sgjelinek 	}
34181300Sgjelinek 
34191300Sgjelinek 	(void) zonecfg_enddsent(t_handle);
34201300Sgjelinek }
34211300Sgjelinek 
34222712Snn35248 /*
34232712Snn35248  * Check that the clone and its source have the same brand type.
34242712Snn35248  */
34252712Snn35248 static int
34262712Snn35248 valid_brand_clone(char *source_zone, char *target_zone)
34272712Snn35248 {
34282727Sedp 	brand_handle_t bh;
34292712Snn35248 	char source_brand[MAXNAMELEN];
34302712Snn35248 
34312712Snn35248 	if ((zone_get_brand(source_zone, source_brand,
34322712Snn35248 	    sizeof (source_brand))) != Z_OK) {
34332712Snn35248 		(void) fprintf(stderr, "%s: zone '%s': %s\n",
34342712Snn35248 		    execname, source_zone, gettext("missing or invalid brand"));
34352712Snn35248 		return (Z_ERR);
34362712Snn35248 	}
34372712Snn35248 
34382712Snn35248 	if (strcmp(source_brand, target_brand) != NULL) {
34392712Snn35248 		(void) fprintf(stderr,
34402712Snn35248 		    gettext("%s: Zones '%s' and '%s' have different brand "
34412712Snn35248 		    "types.\n"), execname, source_zone, target_zone);
34422712Snn35248 		return (Z_ERR);
34432712Snn35248 	}
34442712Snn35248 
34452727Sedp 	if ((bh = brand_open(target_brand)) == NULL) {
34462712Snn35248 		zerror(gettext("missing or invalid brand"));
34472712Snn35248 		return (Z_ERR);
34482712Snn35248 	}
34492727Sedp 	brand_close(bh);
34502712Snn35248 	return (Z_OK);
34512712Snn35248 }
34522712Snn35248 
34531300Sgjelinek static int
34541300Sgjelinek validate_clone(char *source_zone, char *target_zone)
34551300Sgjelinek {
34561300Sgjelinek 	int err = Z_OK;
34571300Sgjelinek 	zone_dochandle_t s_handle;
34581300Sgjelinek 	zone_dochandle_t t_handle;
34591300Sgjelinek 
34601300Sgjelinek 	if ((t_handle = zonecfg_init_handle()) == NULL) {
34611300Sgjelinek 		zperror(cmd_to_str(CMD_CLONE), B_TRUE);
34621300Sgjelinek 		return (Z_ERR);
34631300Sgjelinek 	}
34641300Sgjelinek 	if ((err = zonecfg_get_handle(target_zone, t_handle)) != Z_OK) {
34651300Sgjelinek 		errno = err;
34661300Sgjelinek 		zperror(cmd_to_str(CMD_CLONE), B_TRUE);
34671300Sgjelinek 		zonecfg_fini_handle(t_handle);
34681300Sgjelinek 		return (Z_ERR);
34691300Sgjelinek 	}
34701300Sgjelinek 
34711300Sgjelinek 	if ((s_handle = zonecfg_init_handle()) == NULL) {
34721300Sgjelinek 		zperror(cmd_to_str(CMD_CLONE), B_TRUE);
34731300Sgjelinek 		zonecfg_fini_handle(t_handle);
34741300Sgjelinek 		return (Z_ERR);
34751300Sgjelinek 	}
34761300Sgjelinek 	if ((err = zonecfg_get_handle(source_zone, s_handle)) != Z_OK) {
34771300Sgjelinek 		errno = err;
34781300Sgjelinek 		zperror(cmd_to_str(CMD_CLONE), B_TRUE);
34791300Sgjelinek 		goto done;
34801300Sgjelinek 	}
34811300Sgjelinek 
34822712Snn35248 	/* verify new zone has same brand type */
34832712Snn35248 	err = valid_brand_clone(source_zone, target_zone);
34842712Snn35248 	if (err != Z_OK)
34852712Snn35248 		goto done;
34862712Snn35248 
34871300Sgjelinek 	/* verify new zone has same inherit-pkg-dirs */
34881300Sgjelinek 	err = valid_ipd_clone(s_handle, source_zone, t_handle, target_zone);
34891300Sgjelinek 
34901300Sgjelinek 	/* warn about imported fs's which are the same */
34911300Sgjelinek 	warn_fs_match(s_handle, source_zone, t_handle, target_zone);
34921300Sgjelinek 
34931300Sgjelinek 	/* warn about imported IP addresses which are the same */
34941300Sgjelinek 	warn_ip_match(s_handle, source_zone, t_handle, target_zone);
34951300Sgjelinek 
34961300Sgjelinek 	/* warn about imported devices which are the same */
34971300Sgjelinek 	warn_dev_match(s_handle, source_zone, t_handle, target_zone);
34981300Sgjelinek 
34991300Sgjelinek 	/* warn about imported datasets which are the same */
35001300Sgjelinek 	warn_dataset_match(s_handle, source_zone, t_handle, target_zone);
35011300Sgjelinek 
35021300Sgjelinek done:
35031300Sgjelinek 	zonecfg_fini_handle(t_handle);
35041300Sgjelinek 	zonecfg_fini_handle(s_handle);
35051300Sgjelinek 
35061300Sgjelinek 	return ((err == Z_OK) ? Z_OK : Z_ERR);
35071300Sgjelinek }
35081300Sgjelinek 
35091300Sgjelinek static int
35101300Sgjelinek copy_zone(char *src, char *dst)
35111300Sgjelinek {
35121300Sgjelinek 	boolean_t out_null = B_FALSE;
35131300Sgjelinek 	int status;
35141300Sgjelinek 	char *outfile;
35151300Sgjelinek 	char cmdbuf[MAXPATHLEN * 2 + 128];
35161300Sgjelinek 
35171300Sgjelinek 	if ((outfile = tempnam("/var/log", "zone")) == NULL) {
35181300Sgjelinek 		outfile = "/dev/null";
35191300Sgjelinek 		out_null = B_TRUE;
35201300Sgjelinek 	}
35211300Sgjelinek 
35221867Sgjelinek 	/*
35231867Sgjelinek 	 * Use find to get the list of files to copy.  We need to skip
35241867Sgjelinek 	 * files of type "socket" since cpio can't handle those but that
35251867Sgjelinek 	 * should be ok since the app will recreate the socket when it runs.
35261867Sgjelinek 	 * We also need to filter out anything under the .zfs subdir.  Since
35271867Sgjelinek 	 * find is running depth-first, we need the extra egrep to filter .zfs.
35281867Sgjelinek 	 */
35291300Sgjelinek 	(void) snprintf(cmdbuf, sizeof (cmdbuf),
35301867Sgjelinek 	    "cd %s && /usr/bin/find . -type s -prune -o -depth -print | "
35311607Sgjelinek 	    "/usr/bin/egrep -v '^\\./\\.zfs$|^\\./\\.zfs/' | "
35321300Sgjelinek 	    "/usr/bin/cpio -pdmuP@ %s > %s 2>&1",
35331300Sgjelinek 	    src, dst, outfile);
35341300Sgjelinek 
35351300Sgjelinek 	status = do_subproc(cmdbuf);
35361300Sgjelinek 
35372712Snn35248 	if (subproc_status("copy", status, B_TRUE) != ZONE_SUBPROC_OK) {
35381300Sgjelinek 		if (!out_null)
35391300Sgjelinek 			(void) fprintf(stderr, gettext("\nThe copy failed.\n"
35401300Sgjelinek 			    "More information can be found in %s\n"), outfile);
35412712Snn35248 		return (Z_ERR);
35421300Sgjelinek 	}
35431300Sgjelinek 
35441300Sgjelinek 	if (!out_null)
35451300Sgjelinek 		(void) unlink(outfile);
35461300Sgjelinek 
35471300Sgjelinek 	return (Z_OK);
35481300Sgjelinek }
35491300Sgjelinek 
35501300Sgjelinek /* ARGSUSED */
35511867Sgjelinek static int
35521300Sgjelinek zfm_print(const char *p, void *r) {
35531300Sgjelinek 	zerror("  %s\n", p);
35541300Sgjelinek 	return (0);
35551300Sgjelinek }
35561300Sgjelinek 
35571867Sgjelinek int
35581867Sgjelinek clone_copy(char *source_zonepath, char *zonepath)
35591867Sgjelinek {
35601867Sgjelinek 	int err;
35611867Sgjelinek 
35621867Sgjelinek 	/* Don't clone the zone if anything is still mounted there */
35631867Sgjelinek 	if (zonecfg_find_mounts(source_zonepath, NULL, NULL)) {
35641867Sgjelinek 		zerror(gettext("These file systems are mounted on "
35651867Sgjelinek 		    "subdirectories of %s.\n"), source_zonepath);
35661867Sgjelinek 		(void) zonecfg_find_mounts(source_zonepath, zfm_print, NULL);
35671867Sgjelinek 		return (Z_ERR);
35681867Sgjelinek 	}
35691867Sgjelinek 
35701867Sgjelinek 	/*
35711867Sgjelinek 	 * Attempt to create a ZFS fs for the zonepath.  As usual, we don't
35721867Sgjelinek 	 * care if this works or not since we always have the default behavior
35731867Sgjelinek 	 * of a simple directory for the zonepath.
35741867Sgjelinek 	 */
35751867Sgjelinek 	create_zfs_zonepath(zonepath);
35761867Sgjelinek 
35771867Sgjelinek 	(void) printf(gettext("Copying %s..."), source_zonepath);
35781867Sgjelinek 	(void) fflush(stdout);
35791867Sgjelinek 
35801867Sgjelinek 	err = copy_zone(source_zonepath, zonepath);
35811867Sgjelinek 
35821867Sgjelinek 	(void) printf("\n");
35831867Sgjelinek 
35841867Sgjelinek 	return (err);
35851867Sgjelinek }
35861867Sgjelinek 
35871300Sgjelinek static int
35881300Sgjelinek clone_func(int argc, char *argv[])
35891300Sgjelinek {
35901300Sgjelinek 	char *source_zone = NULL;
35911300Sgjelinek 	int lockfd;
35921300Sgjelinek 	int err, arg;
35931300Sgjelinek 	char zonepath[MAXPATHLEN];
35941300Sgjelinek 	char source_zonepath[MAXPATHLEN];
35951300Sgjelinek 	zone_state_t state;
35961300Sgjelinek 	zone_entry_t *zent;
35971867Sgjelinek 	char *method = NULL;
35981867Sgjelinek 	char *snapshot = NULL;
35997089Sgjelinek 	char cmdbuf[MAXPATHLEN];
36007089Sgjelinek 	char postcmdbuf[MAXPATHLEN];
36017089Sgjelinek 	char presnapbuf[MAXPATHLEN];
36027089Sgjelinek 	char postsnapbuf[MAXPATHLEN];
36037089Sgjelinek 	char validsnapbuf[MAXPATHLEN];
36047089Sgjelinek 	brand_handle_t bh = NULL;
36057089Sgjelinek 	int status;
36067089Sgjelinek 	boolean_t brand_help = B_FALSE;
36071300Sgjelinek 
36081300Sgjelinek 	if (zonecfg_in_alt_root()) {
36091300Sgjelinek 		zerror(gettext("cannot clone zone in alternate root"));
36101300Sgjelinek 		return (Z_ERR);
36111300Sgjelinek 	}
36121300Sgjelinek 
36137089Sgjelinek 	/* Check the argv string for args we handle internally */
36141300Sgjelinek 	optind = 0;
36157089Sgjelinek 	opterr = 0;
36167089Sgjelinek 	while ((arg = getopt(argc, argv, "?m:s:")) != EOF) {
36171300Sgjelinek 		switch (arg) {
36181300Sgjelinek 		case '?':
36197089Sgjelinek 			if (optopt == '?') {
36207089Sgjelinek 				sub_usage(SHELP_CLONE, CMD_CLONE);
36217089Sgjelinek 				brand_help = B_TRUE;
36227089Sgjelinek 			}
36237089Sgjelinek 			/* Ignore unknown options - may be brand specific. */
36247089Sgjelinek 			break;
36251300Sgjelinek 		case 'm':
36261300Sgjelinek 			method = optarg;
36271300Sgjelinek 			break;
36281867Sgjelinek 		case 's':
36291867Sgjelinek 			snapshot = optarg;
36301867Sgjelinek 			break;
36311300Sgjelinek 		default:
36327089Sgjelinek 			/* Ignore unknown options - may be brand specific. */
36337089Sgjelinek 			break;
36341300Sgjelinek 		}
36351300Sgjelinek 	}
36367089Sgjelinek 
36377089Sgjelinek 	if (argc != (optind + 1)) {
36381300Sgjelinek 		sub_usage(SHELP_CLONE, CMD_CLONE);
36391300Sgjelinek 		return (Z_USAGE);
36401300Sgjelinek 	}
36417089Sgjelinek 
36421300Sgjelinek 	source_zone = argv[optind];
36437089Sgjelinek 
36447089Sgjelinek 	if (!brand_help) {
36457089Sgjelinek 		if (sanity_check(target_zone, CMD_CLONE, B_FALSE, B_TRUE,
36467089Sgjelinek 		    B_FALSE) != Z_OK)
36477089Sgjelinek 			return (Z_ERR);
36487089Sgjelinek 		if (verify_details(CMD_CLONE, argv) != Z_OK)
36497089Sgjelinek 			return (Z_ERR);
36507089Sgjelinek 
36517089Sgjelinek 		/*
36527089Sgjelinek 		 * We also need to do some extra validation on the source zone.
36537089Sgjelinek 		 */
36547089Sgjelinek 		if (strcmp(source_zone, GLOBAL_ZONENAME) == 0) {
36557089Sgjelinek 			zerror(gettext("%s operation is invalid for the "
36567089Sgjelinek 			    "global zone."), cmd_to_str(CMD_CLONE));
36577089Sgjelinek 			return (Z_ERR);
36587089Sgjelinek 		}
36597089Sgjelinek 
36607089Sgjelinek 		if (strncmp(source_zone, "SUNW", 4) == 0) {
36617089Sgjelinek 			zerror(gettext("%s operation is invalid for zones "
36627089Sgjelinek 			    "starting with SUNW."), cmd_to_str(CMD_CLONE));
36637089Sgjelinek 			return (Z_ERR);
36647089Sgjelinek 		}
36657089Sgjelinek 
36667089Sgjelinek 		zent = lookup_running_zone(source_zone);
36677089Sgjelinek 		if (zent != NULL) {
36687089Sgjelinek 			/* check whether the zone is ready or running */
36697089Sgjelinek 			if ((err = zone_get_state(zent->zname,
36707089Sgjelinek 			    &zent->zstate_num)) != Z_OK) {
36717089Sgjelinek 				errno = err;
36727089Sgjelinek 				zperror2(zent->zname, gettext("could not get "
36737089Sgjelinek 				    "state"));
36747089Sgjelinek 				/* can't tell, so hedge */
36757089Sgjelinek 				zent->zstate_str = "ready/running";
36767089Sgjelinek 			} else {
36777089Sgjelinek 				zent->zstate_str =
36787089Sgjelinek 				    zone_state_str(zent->zstate_num);
36797089Sgjelinek 			}
36807089Sgjelinek 			zerror(gettext("%s operation is invalid for %s zones."),
36817089Sgjelinek 			    cmd_to_str(CMD_CLONE), zent->zstate_str);
36827089Sgjelinek 			return (Z_ERR);
36837089Sgjelinek 		}
36847089Sgjelinek 
36857089Sgjelinek 		if ((err = zone_get_state(source_zone, &state)) != Z_OK) {
36861300Sgjelinek 			errno = err;
36877089Sgjelinek 			zperror2(source_zone, gettext("could not get state"));
36887089Sgjelinek 			return (Z_ERR);
36897089Sgjelinek 		}
36907089Sgjelinek 		if (state != ZONE_STATE_INSTALLED) {
36917089Sgjelinek 			(void) fprintf(stderr,
36927089Sgjelinek 			    gettext("%s: zone %s is %s; %s is required.\n"),
36937089Sgjelinek 			    execname, source_zone, zone_state_str(state),
36947089Sgjelinek 			    zone_state_str(ZONE_STATE_INSTALLED));
36957089Sgjelinek 			return (Z_ERR);
36961300Sgjelinek 		}
36977089Sgjelinek 
36987089Sgjelinek 		/*
36997089Sgjelinek 		 * The source zone checks out ok, continue with the clone.
37007089Sgjelinek 		 */
37017089Sgjelinek 
37027089Sgjelinek 		if (validate_clone(source_zone, target_zone) != Z_OK)
37037089Sgjelinek 			return (Z_ERR);
37047089Sgjelinek 
37057089Sgjelinek 		if (zonecfg_grab_lock_file(target_zone, &lockfd) != Z_OK) {
37067089Sgjelinek 			zerror(gettext("another %s may have an operation in "
37077089Sgjelinek 			    "progress."), "zoneadm");
37087089Sgjelinek 			return (Z_ERR);
37097089Sgjelinek 		}
37101300Sgjelinek 	}
37111300Sgjelinek 
37121300Sgjelinek 	if ((err = zone_get_zonepath(source_zone, source_zonepath,
37131300Sgjelinek 	    sizeof (source_zonepath))) != Z_OK) {
37141300Sgjelinek 		errno = err;
37151300Sgjelinek 		zperror2(source_zone, gettext("could not get zone path"));
37161300Sgjelinek 		goto done;
37171300Sgjelinek 	}
37181300Sgjelinek 
37191300Sgjelinek 	if ((err = zone_get_zonepath(target_zone, zonepath, sizeof (zonepath)))
37201300Sgjelinek 	    != Z_OK) {
37211300Sgjelinek 		errno = err;
37221300Sgjelinek 		zperror2(target_zone, gettext("could not get zone path"));
37231300Sgjelinek 		goto done;
37241300Sgjelinek 	}
37251300Sgjelinek 
37267089Sgjelinek 	/*
37277089Sgjelinek 	 * Fetch the clone and postclone hooks from the brand configuration.
37287089Sgjelinek 	 */
37297089Sgjelinek 	if ((bh = brand_open(target_brand)) == NULL) {
37307089Sgjelinek 		zerror(gettext("missing or invalid brand"));
37317089Sgjelinek 		err = Z_ERR;
37327089Sgjelinek 		goto done;
37337089Sgjelinek 	}
37347089Sgjelinek 
37357089Sgjelinek 	if (get_hook(bh, cmdbuf, sizeof (cmdbuf), brand_get_clone, target_zone,
37367089Sgjelinek 	    zonepath) != Z_OK) {
37377089Sgjelinek 		zerror("invalid brand configuration: missing clone resource");
37387089Sgjelinek 		brand_close(bh);
37397089Sgjelinek 		err = Z_ERR;
37407089Sgjelinek 		goto done;
37417089Sgjelinek 	}
37427089Sgjelinek 
37437089Sgjelinek 	if (get_hook(bh, postcmdbuf, sizeof (postcmdbuf), brand_get_postclone,
37447089Sgjelinek 	    target_zone, zonepath) != Z_OK) {
37457089Sgjelinek 		zerror("invalid brand configuration: missing postclone "
37467089Sgjelinek 		    "resource");
37477089Sgjelinek 		brand_close(bh);
37487089Sgjelinek 		err = Z_ERR;
37497089Sgjelinek 		goto done;
37507089Sgjelinek 	}
37517089Sgjelinek 
37527089Sgjelinek 	if (get_hook(bh, presnapbuf, sizeof (presnapbuf), brand_get_presnap,
37537089Sgjelinek 	    source_zone, source_zonepath) != Z_OK) {
37547089Sgjelinek 		zerror("invalid brand configuration: missing presnap "
37557089Sgjelinek 		    "resource");
37567089Sgjelinek 		brand_close(bh);
37577089Sgjelinek 		err = Z_ERR;
37581300Sgjelinek 		goto done;
37591300Sgjelinek 	}
37601300Sgjelinek 
37617089Sgjelinek 	if (get_hook(bh, postsnapbuf, sizeof (postsnapbuf), brand_get_postsnap,
37627089Sgjelinek 	    source_zone, source_zonepath) != Z_OK) {
37637089Sgjelinek 		zerror("invalid brand configuration: missing postsnap "
37647089Sgjelinek 		    "resource");
37657089Sgjelinek 		brand_close(bh);
37667089Sgjelinek 		err = Z_ERR;
37677089Sgjelinek 		goto done;
37687089Sgjelinek 	}
37697089Sgjelinek 
37707089Sgjelinek 	if (get_hook(bh, validsnapbuf, sizeof (validsnapbuf),
37717089Sgjelinek 	    brand_get_validatesnap, target_zone, zonepath) != Z_OK) {
37727089Sgjelinek 		zerror("invalid brand configuration: missing validatesnap "
37737089Sgjelinek 		    "resource");
37747089Sgjelinek 		brand_close(bh);
37751867Sgjelinek 		err = Z_ERR;
37767089Sgjelinek 		goto done;
37777089Sgjelinek 	}
37787089Sgjelinek 	brand_close(bh);
37797089Sgjelinek 
37807089Sgjelinek 	/* Append all options to clone hook. */
37817089Sgjelinek 	if (addoptions(cmdbuf, argv, sizeof (cmdbuf)) != Z_OK) {
37827089Sgjelinek 		err = Z_ERR;
37837089Sgjelinek 		goto done;
37847089Sgjelinek 	}
37857089Sgjelinek 
37867089Sgjelinek 	/* Append all options to postclone hook. */
37877089Sgjelinek 	if (addoptions(postcmdbuf, argv, sizeof (postcmdbuf)) != Z_OK) {
37887089Sgjelinek 		err = Z_ERR;
37897089Sgjelinek 		goto done;
37907089Sgjelinek 	}
37917089Sgjelinek 
37927089Sgjelinek 	if (!brand_help) {
37937089Sgjelinek 		if ((err = zone_set_state(target_zone, ZONE_STATE_INCOMPLETE))
37947089Sgjelinek 		    != Z_OK) {
37957089Sgjelinek 			errno = err;
37967089Sgjelinek 			zperror2(target_zone, gettext("could not set state"));
37977089Sgjelinek 			goto done;
37987089Sgjelinek 		}
37991867Sgjelinek 	}
38001867Sgjelinek 
38012712Snn35248 	/*
38027089Sgjelinek 	 * The clone hook is optional.  If it exists, use the hook for
38037089Sgjelinek 	 * cloning, otherwise use the built-in clone support
38042712Snn35248 	 */
38057089Sgjelinek 	if (cmdbuf[0] != '\0') {
38067089Sgjelinek 		/* Run the clone hook */
38077089Sgjelinek 		status = do_subproc_interactive(cmdbuf);
38087089Sgjelinek 		if ((status = subproc_status(gettext("brand-specific clone"),
38097089Sgjelinek 		    status, B_FALSE)) != ZONE_SUBPROC_OK) {
38107089Sgjelinek 			if (status == ZONE_SUBPROC_USAGE && !brand_help)
38117089Sgjelinek 				sub_usage(SHELP_CLONE, CMD_CLONE);
38127089Sgjelinek 			err = Z_ERR;
38137089Sgjelinek 			goto done;
38147089Sgjelinek 		}
38157089Sgjelinek 
38167089Sgjelinek 		if (brand_help)
38177089Sgjelinek 			return (Z_OK);
38187089Sgjelinek 
38197089Sgjelinek 	} else {
38207089Sgjelinek 		/* If just help, we're done since there is no brand help. */
38217089Sgjelinek 		if (brand_help)
38227089Sgjelinek 			return (Z_OK);
38237089Sgjelinek 
38247089Sgjelinek 		/* Run the built-in clone support. */
38257089Sgjelinek 
38267089Sgjelinek 		/* The only explicit built-in method is "copy". */
38277089Sgjelinek 		if (method != NULL && strcmp(method, "copy") != 0) {
38287089Sgjelinek 			sub_usage(SHELP_CLONE, CMD_CLONE);
38297089Sgjelinek 			err = Z_USAGE;
38307089Sgjelinek 			goto done;
38317089Sgjelinek 		}
38327089Sgjelinek 
38337089Sgjelinek 		if (snapshot != NULL) {
38347089Sgjelinek 			err = clone_snapshot_zfs(snapshot, zonepath,
38357089Sgjelinek 			    validsnapbuf);
38367089Sgjelinek 		} else {
38377089Sgjelinek 			/*
38387089Sgjelinek 			 * We always copy the clone unless the source is ZFS
38397089Sgjelinek 			 * and a ZFS clone worked.  We fallback to copying if
38407089Sgjelinek 			 * the ZFS clone fails for some reason.
38417089Sgjelinek 			 */
38427089Sgjelinek 			err = Z_ERR;
38437089Sgjelinek 			if (method == NULL && is_zonepath_zfs(source_zonepath))
38447089Sgjelinek 				err = clone_zfs(source_zonepath, zonepath,
38457089Sgjelinek 				    presnapbuf, postsnapbuf);
38467089Sgjelinek 
38477089Sgjelinek 			if (err != Z_OK)
38487089Sgjelinek 				err = clone_copy(source_zonepath, zonepath);
38497089Sgjelinek 		}
38507089Sgjelinek 	}
38517089Sgjelinek 
38527089Sgjelinek 	if (err == Z_OK && postcmdbuf[0] != '\0') {
38537089Sgjelinek 		status = do_subproc(postcmdbuf);
38547089Sgjelinek 		if ((err = subproc_status("postclone", status, B_FALSE))
38557089Sgjelinek 		    != ZONE_SUBPROC_OK) {
38567089Sgjelinek 			zerror(gettext("post-clone configuration failed."));
38577089Sgjelinek 			err = Z_ERR;
38587089Sgjelinek 		}
38597089Sgjelinek 	}
38601300Sgjelinek 
38611300Sgjelinek done:
38622712Snn35248 	/*
38632712Snn35248 	 * If everything went well, we mark the zone as installed.
38642712Snn35248 	 */
38652712Snn35248 	if (err == Z_OK) {
38662712Snn35248 		err = zone_set_state(target_zone, ZONE_STATE_INSTALLED);
38672712Snn35248 		if (err != Z_OK) {
38682712Snn35248 			errno = err;
38692712Snn35248 			zperror2(target_zone, gettext("could not set state"));
38702712Snn35248 		}
38712712Snn35248 	}
38727089Sgjelinek 	if (!brand_help)
38737089Sgjelinek 		zonecfg_release_lock_file(target_zone, lockfd);
38741300Sgjelinek 	return ((err == Z_OK) ? Z_OK : Z_ERR);
38751300Sgjelinek }
38761300Sgjelinek 
38771607Sgjelinek /*
38781867Sgjelinek  * Used when removing a zonepath after uninstalling or cleaning up after
38791867Sgjelinek  * the move subcommand.  This handles a zonepath that has non-standard
38801867Sgjelinek  * contents so that we will only cleanup the stuff we know about and leave
38811867Sgjelinek  * any user data alone.
38821607Sgjelinek  *
38831867Sgjelinek  * If the "all" parameter is true then we should remove the whole zonepath
38841867Sgjelinek  * even if it has non-standard files/directories in it.  This can be used when
38851867Sgjelinek  * we need to cleanup after moving the zonepath across file systems.
38861867Sgjelinek  *
38871867Sgjelinek  * We "exec" the RMCOMMAND so that the returned status is that of RMCOMMAND
38881867Sgjelinek  * and not the shell.
38891607Sgjelinek  */
38901607Sgjelinek static int
38911867Sgjelinek cleanup_zonepath(char *zonepath, boolean_t all)
38921607Sgjelinek {
38931867Sgjelinek 	int		status;
38941867Sgjelinek 	int		i;
38951867Sgjelinek 	boolean_t	non_std = B_FALSE;
38961867Sgjelinek 	struct dirent	*dp;
38971867Sgjelinek 	DIR		*dirp;
38983686Sgjelinek 			/*
38993686Sgjelinek 			 * The SUNWattached.xml file is expected since it might
39003686Sgjelinek 			 * exist if the zone was force-attached after a
39013686Sgjelinek 			 * migration.
39023686Sgjelinek 			 */
39033686Sgjelinek 	char		*std_entries[] = {"dev", "lu", "root",
39043686Sgjelinek 			    "SUNWattached.xml", NULL};
39051867Sgjelinek 			/* (MAXPATHLEN * 3) is for the 3 std_entries dirs */
39061867Sgjelinek 	char		cmdbuf[sizeof (RMCOMMAND) + (MAXPATHLEN * 3) + 64];
39071867Sgjelinek 
39081867Sgjelinek 	/*
39091867Sgjelinek 	 * We shouldn't need these checks but lets be paranoid since we
39101867Sgjelinek 	 * could blow away the whole system here if we got the wrong zonepath.
39111867Sgjelinek 	 */
39121867Sgjelinek 	if (*zonepath == NULL || strcmp(zonepath, "/") == 0) {
39131867Sgjelinek 		(void) fprintf(stderr, "invalid zonepath '%s'\n", zonepath);
39141867Sgjelinek 		return (Z_INVAL);
39151867Sgjelinek 	}
39161867Sgjelinek 
39171867Sgjelinek 	/*
39181867Sgjelinek 	 * If the dirpath is already gone (maybe it was manually removed) then
39191867Sgjelinek 	 * we just return Z_OK so that the cleanup is successful.
39201867Sgjelinek 	 */
39211867Sgjelinek 	if ((dirp = opendir(zonepath)) == NULL)
39221867Sgjelinek 		return (Z_OK);
39231867Sgjelinek 
39241867Sgjelinek 	/*
39251867Sgjelinek 	 * Look through the zonepath directory to see if there are any
39261867Sgjelinek 	 * non-standard files/dirs.  Also skip .zfs since that might be
39271867Sgjelinek 	 * there but we'll handle ZFS file systems as a special case.
39281867Sgjelinek 	 */
39291867Sgjelinek 	while ((dp = readdir(dirp)) != NULL) {
39301867Sgjelinek 		if (strcmp(dp->d_name, ".") == 0 ||
39311867Sgjelinek 		    strcmp(dp->d_name, "..") == 0 ||
39321867Sgjelinek 		    strcmp(dp->d_name, ".zfs") == 0)
39331867Sgjelinek 			continue;
39341867Sgjelinek 
39351867Sgjelinek 		for (i = 0; std_entries[i] != NULL; i++)
39361867Sgjelinek 			if (strcmp(dp->d_name, std_entries[i]) == 0)
39371867Sgjelinek 				break;
39381867Sgjelinek 
39391867Sgjelinek 		if (std_entries[i] == NULL)
39401867Sgjelinek 			non_std = B_TRUE;
39411867Sgjelinek 	}
39421867Sgjelinek 	(void) closedir(dirp);
39431867Sgjelinek 
39441867Sgjelinek 	if (!all && non_std) {
39451607Sgjelinek 		/*
39461867Sgjelinek 		 * There are extra, non-standard directories/files in the
39471867Sgjelinek 		 * zonepath so we don't want to remove the zonepath.  We
39481867Sgjelinek 		 * just want to remove the standard directories and leave
39491867Sgjelinek 		 * the user data alone.
39501607Sgjelinek 		 */
39511867Sgjelinek 		(void) snprintf(cmdbuf, sizeof (cmdbuf), "exec " RMCOMMAND);
39521867Sgjelinek 
39531867Sgjelinek 		for (i = 0; std_entries[i] != NULL; i++) {
39541867Sgjelinek 			char tmpbuf[MAXPATHLEN];
39551867Sgjelinek 
39561867Sgjelinek 			if (snprintf(tmpbuf, sizeof (tmpbuf), " %s/%s",
39571867Sgjelinek 			    zonepath, std_entries[i]) >= sizeof (tmpbuf) ||
39581867Sgjelinek 			    strlcat(cmdbuf, tmpbuf, sizeof (cmdbuf)) >=
39591867Sgjelinek 			    sizeof (cmdbuf)) {
39601867Sgjelinek 				(void) fprintf(stderr,
39611867Sgjelinek 				    gettext("path is too long\n"));
39621867Sgjelinek 				return (Z_INVAL);
39631867Sgjelinek 			}
39641867Sgjelinek 		}
39651867Sgjelinek 
39661867Sgjelinek 		status = do_subproc(cmdbuf);
39671867Sgjelinek 
39681867Sgjelinek 		(void) fprintf(stderr, gettext("WARNING: Unable to completely "
39691867Sgjelinek 		    "remove %s\nbecause it contains additional user data.  "
39701867Sgjelinek 		    "Only the standard directory\nentries have been "
39711867Sgjelinek 		    "removed.\n"),
39721867Sgjelinek 		    zonepath);
39731867Sgjelinek 
39742712Snn35248 		return ((subproc_status(RMCOMMAND, status, B_TRUE) ==
39752712Snn35248 		    ZONE_SUBPROC_OK) ? Z_OK : Z_ERR);
39761607Sgjelinek 	}
39771607Sgjelinek 
39781867Sgjelinek 	/*
39791867Sgjelinek 	 * There is nothing unexpected in the zonepath, try to get rid of the
39801867Sgjelinek 	 * whole zonepath directory.
39811867Sgjelinek 	 *
39821867Sgjelinek 	 * If the zonepath is its own zfs file system, try to destroy the
39831867Sgjelinek 	 * file system.  If that fails for some reason (e.g. it has clones)
39841867Sgjelinek 	 * then we'll just remove the contents of the zonepath.
39851867Sgjelinek 	 */
39861867Sgjelinek 	if (is_zonepath_zfs(zonepath)) {
39871867Sgjelinek 		if (destroy_zfs(zonepath) == Z_OK)
39881867Sgjelinek 			return (Z_OK);
39891867Sgjelinek 		(void) snprintf(cmdbuf, sizeof (cmdbuf), "exec " RMCOMMAND
39901867Sgjelinek 		    " %s/*", zonepath);
39911867Sgjelinek 		status = do_subproc(cmdbuf);
39922712Snn35248 		return ((subproc_status(RMCOMMAND, status, B_TRUE) ==
39932712Snn35248 		    ZONE_SUBPROC_OK) ? Z_OK : Z_ERR);
39941867Sgjelinek 	}
39951867Sgjelinek 
39961867Sgjelinek 	(void) snprintf(cmdbuf, sizeof (cmdbuf), "exec " RMCOMMAND " %s",
39971867Sgjelinek 	    zonepath);
39981607Sgjelinek 	status = do_subproc(cmdbuf);
39992712Snn35248 
40002712Snn35248 	return ((subproc_status(RMCOMMAND, status, B_TRUE) == ZONE_SUBPROC_OK)
40012712Snn35248 	    ? Z_OK : Z_ERR);
40021607Sgjelinek }
40031607Sgjelinek 
40041300Sgjelinek static int
40051300Sgjelinek move_func(int argc, char *argv[])
40061300Sgjelinek {
40071300Sgjelinek 	char *new_zonepath = NULL;
40081300Sgjelinek 	int lockfd;
40091300Sgjelinek 	int err, arg;
40101300Sgjelinek 	char zonepath[MAXPATHLEN];
40111300Sgjelinek 	zone_dochandle_t handle;
40121300Sgjelinek 	boolean_t fast;
40131867Sgjelinek 	boolean_t is_zfs = B_FALSE;
40141867Sgjelinek 	struct dirent *dp;
40151867Sgjelinek 	DIR *dirp;
40161867Sgjelinek 	boolean_t empty = B_TRUE;
40171300Sgjelinek 	boolean_t revert;
40181300Sgjelinek 	struct stat zonepath_buf;
40191300Sgjelinek 	struct stat new_zonepath_buf;
40201300Sgjelinek 
40211300Sgjelinek 	if (zonecfg_in_alt_root()) {
40221300Sgjelinek 		zerror(gettext("cannot move zone in alternate root"));
40231300Sgjelinek 		return (Z_ERR);
40241300Sgjelinek 	}
40251300Sgjelinek 
40261300Sgjelinek 	optind = 0;
40271300Sgjelinek 	if ((arg = getopt(argc, argv, "?")) != EOF) {
40281300Sgjelinek 		switch (arg) {
40291300Sgjelinek 		case '?':
40301300Sgjelinek 			sub_usage(SHELP_MOVE, CMD_MOVE);
40311300Sgjelinek 			return (optopt == '?' ? Z_OK : Z_USAGE);
40321300Sgjelinek 		default:
40331300Sgjelinek 			sub_usage(SHELP_MOVE, CMD_MOVE);
40341300Sgjelinek 			return (Z_USAGE);
40351300Sgjelinek 		}
40361300Sgjelinek 	}
40371300Sgjelinek 	if (argc != (optind + 1)) {
40381300Sgjelinek 		sub_usage(SHELP_MOVE, CMD_MOVE);
40391300Sgjelinek 		return (Z_USAGE);
40401300Sgjelinek 	}
40411300Sgjelinek 	new_zonepath = argv[optind];
40422712Snn35248 	if (sanity_check(target_zone, CMD_MOVE, B_FALSE, B_TRUE, B_FALSE)
40432712Snn35248 	    != Z_OK)
40441300Sgjelinek 		return (Z_ERR);
40453339Szt129084 	if (verify_details(CMD_MOVE, argv) != Z_OK)
40461300Sgjelinek 		return (Z_ERR);
40471300Sgjelinek 
40481300Sgjelinek 	/*
40491300Sgjelinek 	 * Check out the new zonepath.  This has the side effect of creating
40501300Sgjelinek 	 * a directory for the new zonepath.  We depend on this later when we
40511867Sgjelinek 	 * stat to see if we are doing a cross file system move or not.
40521300Sgjelinek 	 */
40531300Sgjelinek 	if (validate_zonepath(new_zonepath, CMD_MOVE) != Z_OK)
40541300Sgjelinek 		return (Z_ERR);
40551300Sgjelinek 
40561300Sgjelinek 	if ((err = zone_get_zonepath(target_zone, zonepath, sizeof (zonepath)))
40571300Sgjelinek 	    != Z_OK) {
40581300Sgjelinek 		errno = err;
40591300Sgjelinek 		zperror2(target_zone, gettext("could not get zone path"));
40601300Sgjelinek 		return (Z_ERR);
40611300Sgjelinek 	}
40621300Sgjelinek 
40631300Sgjelinek 	if (stat(zonepath, &zonepath_buf) == -1) {
40641300Sgjelinek 		zperror(gettext("could not stat zone path"), B_FALSE);
40651300Sgjelinek 		return (Z_ERR);
40661300Sgjelinek 	}
40671300Sgjelinek 
40681300Sgjelinek 	if (stat(new_zonepath, &new_zonepath_buf) == -1) {
40691300Sgjelinek 		zperror(gettext("could not stat new zone path"), B_FALSE);
40701300Sgjelinek 		return (Z_ERR);
40711300Sgjelinek 	}
40721300Sgjelinek 
40731867Sgjelinek 	/*
40741867Sgjelinek 	 * Check if the destination directory is empty.
40751867Sgjelinek 	 */
40761867Sgjelinek 	if ((dirp = opendir(new_zonepath)) == NULL) {
40771867Sgjelinek 		zperror(gettext("could not open new zone path"), B_FALSE);
40781867Sgjelinek 		return (Z_ERR);
40791867Sgjelinek 	}
40801867Sgjelinek 	while ((dp = readdir(dirp)) != (struct dirent *)0) {
40811867Sgjelinek 		if (strcmp(dp->d_name, ".") == 0 ||
40821867Sgjelinek 		    strcmp(dp->d_name, "..") == 0)
40831867Sgjelinek 			continue;
40841867Sgjelinek 		empty = B_FALSE;
40851867Sgjelinek 		break;
40861867Sgjelinek 	}
40871867Sgjelinek 	(void) closedir(dirp);
40881867Sgjelinek 
40891867Sgjelinek 	/* Error if there is anything in the destination directory. */
40901867Sgjelinek 	if (!empty) {
40911867Sgjelinek 		(void) fprintf(stderr, gettext("could not move zone to %s: "
40921867Sgjelinek 		    "directory not empty\n"), new_zonepath);
40931867Sgjelinek 		return (Z_ERR);
40941867Sgjelinek 	}
40951867Sgjelinek 
40961300Sgjelinek 	/* Don't move the zone if anything is still mounted there */
40971300Sgjelinek 	if (zonecfg_find_mounts(zonepath, NULL, NULL)) {
40981867Sgjelinek 		zerror(gettext("These file systems are mounted on "
40991300Sgjelinek 		    "subdirectories of %s.\n"), zonepath);
41001300Sgjelinek 		(void) zonecfg_find_mounts(zonepath, zfm_print, NULL);
41011300Sgjelinek 		return (Z_ERR);
41021300Sgjelinek 	}
41031300Sgjelinek 
41041300Sgjelinek 	/*
41051867Sgjelinek 	 * Check if we are moving in the same file system and can do a fast
41061867Sgjelinek 	 * move or if we are crossing file systems and have to copy the data.
41071300Sgjelinek 	 */
41081300Sgjelinek 	fast = (zonepath_buf.st_dev == new_zonepath_buf.st_dev);
41091300Sgjelinek 
41101300Sgjelinek 	if ((handle = zonecfg_init_handle()) == NULL) {
41111300Sgjelinek 		zperror(cmd_to_str(CMD_MOVE), B_TRUE);
41121300Sgjelinek 		return (Z_ERR);
41131300Sgjelinek 	}
41141300Sgjelinek 
41151300Sgjelinek 	if ((err = zonecfg_get_handle(target_zone, handle)) != Z_OK) {
41161300Sgjelinek 		errno = err;
41171300Sgjelinek 		zperror(cmd_to_str(CMD_MOVE), B_TRUE);
41181300Sgjelinek 		zonecfg_fini_handle(handle);
41191300Sgjelinek 		return (Z_ERR);
41201300Sgjelinek 	}
41211300Sgjelinek 
41227089Sgjelinek 	if (zonecfg_grab_lock_file(target_zone, &lockfd) != Z_OK) {
41231300Sgjelinek 		zerror(gettext("another %s may have an operation in progress."),
41241300Sgjelinek 		    "zoneadm");
41251300Sgjelinek 		zonecfg_fini_handle(handle);
41261300Sgjelinek 		return (Z_ERR);
41271300Sgjelinek 	}
41281300Sgjelinek 
41291300Sgjelinek 	/*
41301867Sgjelinek 	 * We're making some file system changes now so we have to clean up
41311867Sgjelinek 	 * the file system before we are done.  This will either clean up the
41321300Sgjelinek 	 * new zonepath if the zonecfg update failed or it will clean up the
41331300Sgjelinek 	 * old zonepath if everything is ok.
41341300Sgjelinek 	 */
41351300Sgjelinek 	revert = B_TRUE;
41361300Sgjelinek 
41371867Sgjelinek 	if (is_zonepath_zfs(zonepath) &&
41381867Sgjelinek 	    move_zfs(zonepath, new_zonepath) != Z_ERR) {
41391867Sgjelinek 		is_zfs = B_TRUE;
41401867Sgjelinek 
41411867Sgjelinek 	} else if (fast) {
41421867Sgjelinek 		/* same file system, use rename for a quick move */
41431300Sgjelinek 
41441300Sgjelinek 		/*
41451300Sgjelinek 		 * Remove the new_zonepath directory that got created above
41461300Sgjelinek 		 * during the validation.  It gets in the way of the rename.
41471300Sgjelinek 		 */
41481300Sgjelinek 		if (rmdir(new_zonepath) != 0) {
41491300Sgjelinek 			zperror(gettext("could not rmdir new zone path"),
41501300Sgjelinek 			    B_FALSE);
41511300Sgjelinek 			zonecfg_fini_handle(handle);
41527089Sgjelinek 			zonecfg_release_lock_file(target_zone, lockfd);
41531300Sgjelinek 			return (Z_ERR);
41541300Sgjelinek 		}
41551300Sgjelinek 
41561300Sgjelinek 		if (rename(zonepath, new_zonepath) != 0) {
41571300Sgjelinek 			/*
41581300Sgjelinek 			 * If this fails we don't need to do all of the
41591300Sgjelinek 			 * cleanup that happens for the rest of the code
41601300Sgjelinek 			 * so just return from this error.
41611300Sgjelinek 			 */
41621300Sgjelinek 			zperror(gettext("could not move zone"), B_FALSE);
41631300Sgjelinek 			zonecfg_fini_handle(handle);
41647089Sgjelinek 			zonecfg_release_lock_file(target_zone, lockfd);
41651300Sgjelinek 			return (Z_ERR);
41661300Sgjelinek 		}
41671300Sgjelinek 
41681300Sgjelinek 	} else {
41691867Sgjelinek 		/*
41701867Sgjelinek 		 * Attempt to create a ZFS fs for the new zonepath.  As usual,
41711867Sgjelinek 		 * we don't care if this works or not since we always have the
41721867Sgjelinek 		 * default behavior of a simple directory for the zonepath.
41731867Sgjelinek 		 */
41741867Sgjelinek 		create_zfs_zonepath(new_zonepath);
41751867Sgjelinek 
41761300Sgjelinek 		(void) printf(gettext(
41771867Sgjelinek 		    "Moving across file systems; copying zonepath %s..."),
41781300Sgjelinek 		    zonepath);
41791300Sgjelinek 		(void) fflush(stdout);
41801300Sgjelinek 
41811300Sgjelinek 		err = copy_zone(zonepath, new_zonepath);
41821300Sgjelinek 
41831300Sgjelinek 		(void) printf("\n");
41841300Sgjelinek 		if (err != Z_OK)
41851300Sgjelinek 			goto done;
41861300Sgjelinek 	}
41871300Sgjelinek 
41881300Sgjelinek 	if ((err = zonecfg_set_zonepath(handle, new_zonepath)) != Z_OK) {
41891300Sgjelinek 		errno = err;
41901300Sgjelinek 		zperror(gettext("could not set new zonepath"), B_TRUE);
41911300Sgjelinek 		goto done;
41921300Sgjelinek 	}
41931300Sgjelinek 
41941300Sgjelinek 	if ((err = zonecfg_save(handle)) != Z_OK) {
41951300Sgjelinek 		errno = err;
41961300Sgjelinek 		zperror(gettext("zonecfg save failed"), B_TRUE);
41971300Sgjelinek 		goto done;
41981300Sgjelinek 	}
41991300Sgjelinek 
42001300Sgjelinek 	revert = B_FALSE;
42011300Sgjelinek 
42021300Sgjelinek done:
42031300Sgjelinek 	zonecfg_fini_handle(handle);
42047089Sgjelinek 	zonecfg_release_lock_file(target_zone, lockfd);
42051300Sgjelinek 
42061300Sgjelinek 	/*
42071867Sgjelinek 	 * Clean up the file system based on how things went.  We either
42081300Sgjelinek 	 * clean up the new zonepath if the operation failed for some reason
42091300Sgjelinek 	 * or we clean up the old zonepath if everything is ok.
42101300Sgjelinek 	 */
42111300Sgjelinek 	if (revert) {
42121300Sgjelinek 		/* The zonecfg update failed, cleanup the new zonepath. */
42131867Sgjelinek 		if (is_zfs) {
42141867Sgjelinek 			if (move_zfs(new_zonepath, zonepath) == Z_ERR) {
42151867Sgjelinek 				(void) fprintf(stderr, gettext("could not "
42161867Sgjelinek 				    "restore zonepath, the zfs mountpoint is "
42171867Sgjelinek 				    "set as:\n%s\n"), new_zonepath);
42181867Sgjelinek 				/*
42191867Sgjelinek 				 * err is already != Z_OK since we're reverting
42201867Sgjelinek 				 */
42211867Sgjelinek 			}
42221867Sgjelinek 
42231867Sgjelinek 		} else if (fast) {
42241300Sgjelinek 			if (rename(new_zonepath, zonepath) != 0) {
42251300Sgjelinek 				zperror(gettext("could not restore zonepath"),
42261300Sgjelinek 				    B_FALSE);
42271300Sgjelinek 				/*
42281300Sgjelinek 				 * err is already != Z_OK since we're reverting
42291300Sgjelinek 				 */
42301300Sgjelinek 			}
42311300Sgjelinek 		} else {
42321300Sgjelinek 			(void) printf(gettext("Cleaning up zonepath %s..."),
42331300Sgjelinek 			    new_zonepath);
42341300Sgjelinek 			(void) fflush(stdout);
42351867Sgjelinek 			err = cleanup_zonepath(new_zonepath, B_TRUE);
42361300Sgjelinek 			(void) printf("\n");
42371300Sgjelinek 
42381607Sgjelinek 			if (err != Z_OK) {
42391300Sgjelinek 				errno = err;
42401300Sgjelinek 				zperror(gettext("could not remove new "
42411300Sgjelinek 				    "zonepath"), B_TRUE);
42421300Sgjelinek 			} else {
42431300Sgjelinek 				/*
42441300Sgjelinek 				 * Because we're reverting we know the mainline
42451300Sgjelinek 				 * code failed but we just reused the err
42461300Sgjelinek 				 * variable so we reset it back to Z_ERR.
42471300Sgjelinek 				 */
42481300Sgjelinek 				err = Z_ERR;
42491300Sgjelinek 			}
42501300Sgjelinek 		}
42511300Sgjelinek 
42521300Sgjelinek 	} else {
42531300Sgjelinek 		/* The move was successful, cleanup the old zonepath. */
42541867Sgjelinek 		if (!is_zfs && !fast) {
42551300Sgjelinek 			(void) printf(
42561300Sgjelinek 			    gettext("Cleaning up zonepath %s..."), zonepath);
42571300Sgjelinek 			(void) fflush(stdout);
42581867Sgjelinek 			err = cleanup_zonepath(zonepath, B_TRUE);
42591300Sgjelinek 			(void) printf("\n");
42601300Sgjelinek 
42611607Sgjelinek 			if (err != Z_OK) {
42621300Sgjelinek 				errno = err;
42631300Sgjelinek 				zperror(gettext("could not remove zonepath"),
42641300Sgjelinek 				    B_TRUE);
42651300Sgjelinek 			}
42661300Sgjelinek 		}
42671300Sgjelinek 	}
42681300Sgjelinek 
42691300Sgjelinek 	return ((err == Z_OK) ? Z_OK : Z_ERR);
42701300Sgjelinek }
42711300Sgjelinek 
42727089Sgjelinek /* ARGSUSED */
42731507Sgjelinek static int
42741507Sgjelinek detach_func(int argc, char *argv[])
42751507Sgjelinek {
4276*8301Sgerald.jelinek@sun.com 	int lockfd = -1;
42771507Sgjelinek 	int err, arg;
42781507Sgjelinek 	char zonepath[MAXPATHLEN];
42794785Sgjelinek 	char cmdbuf[MAXPATHLEN];
42807089Sgjelinek 	char precmdbuf[MAXPATHLEN];
42812078Sgjelinek 	boolean_t execute = B_TRUE;
42827089Sgjelinek 	boolean_t brand_help = B_FALSE;
42834785Sgjelinek 	brand_handle_t bh = NULL;
42847089Sgjelinek 	int status;
42851507Sgjelinek 
42861507Sgjelinek 	if (zonecfg_in_alt_root()) {
42871507Sgjelinek 		zerror(gettext("cannot detach zone in alternate root"));
42881507Sgjelinek 		return (Z_ERR);
42891507Sgjelinek 	}
42901507Sgjelinek 
42917089Sgjelinek 	/* Check the argv string for args we handle internally */
42921507Sgjelinek 	optind = 0;
42937089Sgjelinek 	opterr = 0;
42947089Sgjelinek 	while ((arg = getopt(argc, argv, "?n")) != EOF) {
42951507Sgjelinek 		switch (arg) {
42961507Sgjelinek 		case '?':
42977089Sgjelinek 			if (optopt == '?') {
42987089Sgjelinek 				sub_usage(SHELP_DETACH, CMD_DETACH);
42997089Sgjelinek 				brand_help = B_TRUE;
43007089Sgjelinek 			}
43017089Sgjelinek 			/* Ignore unknown options - may be brand specific. */
43027089Sgjelinek 			break;
43032078Sgjelinek 		case 'n':
43042078Sgjelinek 			execute = B_FALSE;
43052078Sgjelinek 			break;
43061507Sgjelinek 		default:
43077089Sgjelinek 			/* Ignore unknown options - may be brand specific. */
43087089Sgjelinek 			break;
43091507Sgjelinek 		}
43101507Sgjelinek 	}
43112712Snn35248 
43127089Sgjelinek 	if (brand_help)
43137089Sgjelinek 		execute = B_FALSE;
43147089Sgjelinek 
43152078Sgjelinek 	if (execute) {
43162712Snn35248 		if (sanity_check(target_zone, CMD_DETACH, B_FALSE, B_TRUE,
43172712Snn35248 		    B_FALSE) != Z_OK)
43182078Sgjelinek 			return (Z_ERR);
43193339Szt129084 		if (verify_details(CMD_DETACH, argv) != Z_OK)
43202078Sgjelinek 			return (Z_ERR);
43212078Sgjelinek 	} else {
43222078Sgjelinek 		/*
43232078Sgjelinek 		 * We want a dry-run to work for a non-privileged user so we
43242078Sgjelinek 		 * only do minimal validation.
43252078Sgjelinek 		 */
43262078Sgjelinek 		if (target_zone == NULL) {
43272078Sgjelinek 			zerror(gettext("no zone specified"));
43282078Sgjelinek 			return (Z_ERR);
43292078Sgjelinek 		}
43302078Sgjelinek 
43312078Sgjelinek 		if (strcmp(target_zone, GLOBAL_ZONENAME) == 0) {
43322078Sgjelinek 			zerror(gettext("%s operation is invalid for the "
43332078Sgjelinek 			    "global zone."), cmd_to_str(CMD_DETACH));
43342078Sgjelinek 			return (Z_ERR);
43352078Sgjelinek 		}
43362078Sgjelinek 	}
43371507Sgjelinek 
43381507Sgjelinek 	if ((err = zone_get_zonepath(target_zone, zonepath, sizeof (zonepath)))
43391507Sgjelinek 	    != Z_OK) {
43401507Sgjelinek 		errno = err;
43411507Sgjelinek 		zperror2(target_zone, gettext("could not get zone path"));
43421507Sgjelinek 		return (Z_ERR);
43431507Sgjelinek 	}
43441507Sgjelinek 
43457089Sgjelinek 	/* Fetch the detach and predetach hooks from the brand configuration. */
43464785Sgjelinek 	if ((bh = brand_open(target_brand)) == NULL) {
43474785Sgjelinek 		zerror(gettext("missing or invalid brand"));
43484785Sgjelinek 		return (Z_ERR);
43494785Sgjelinek 	}
43504785Sgjelinek 
43517089Sgjelinek 	if (get_hook(bh, cmdbuf, sizeof (cmdbuf), brand_get_detach, target_zone,
43527089Sgjelinek 	    zonepath) != Z_OK) {
43537089Sgjelinek 		zerror("invalid brand configuration: missing detach resource");
43547089Sgjelinek 		brand_close(bh);
43557089Sgjelinek 		return (Z_ERR);
43567089Sgjelinek 	}
43577089Sgjelinek 
43587089Sgjelinek 	if (get_hook(bh, precmdbuf, sizeof (precmdbuf), brand_get_predetach,
43597089Sgjelinek 	    target_zone, zonepath) != Z_OK) {
43604785Sgjelinek 		zerror("invalid brand configuration: missing predetach "
43614785Sgjelinek 		    "resource");
43624785Sgjelinek 		brand_close(bh);
43634785Sgjelinek 		return (Z_ERR);
43644785Sgjelinek 	}
43654785Sgjelinek 	brand_close(bh);
43664785Sgjelinek 
43677089Sgjelinek 	/* Append all options to predetach hook. */
43687089Sgjelinek 	if (addoptions(precmdbuf, argv, sizeof (precmdbuf)) != Z_OK)
43697089Sgjelinek 		return (Z_ERR);
43707089Sgjelinek 
43717089Sgjelinek 	/* Append all options to detach hook. */
43727089Sgjelinek 	if (addoptions(cmdbuf, argv, sizeof (cmdbuf)) != Z_OK)
43737089Sgjelinek 		return (Z_ERR);
43747089Sgjelinek 
43757089Sgjelinek 	if (execute && zonecfg_grab_lock_file(target_zone, &lockfd) != Z_OK) {
43761507Sgjelinek 		zerror(gettext("another %s may have an operation in progress."),
43771507Sgjelinek 		    "zoneadm");
43781507Sgjelinek 		return (Z_ERR);
43791507Sgjelinek 	}
43801507Sgjelinek 
43817089Sgjelinek 	/* If we have a brand predetach hook, run it. */
43827089Sgjelinek 	if (!brand_help && precmdbuf[0] != '\0') {
43837089Sgjelinek 		status = do_subproc(precmdbuf);
43847089Sgjelinek 		if (subproc_status(gettext("brand-specific predetach"),
43857089Sgjelinek 		    status, B_FALSE) != ZONE_SUBPROC_OK) {
43867089Sgjelinek 
4387*8301Sgerald.jelinek@sun.com 			if (execute) {
4388*8301Sgerald.jelinek@sun.com 				assert(lockfd >= 0);
43897089Sgjelinek 				zonecfg_release_lock_file(target_zone, lockfd);
4390*8301Sgerald.jelinek@sun.com 				lockfd = -1;
4391*8301Sgerald.jelinek@sun.com 			}
4392*8301Sgerald.jelinek@sun.com 
4393*8301Sgerald.jelinek@sun.com 			assert(lockfd == -1);
43947089Sgjelinek 			return (Z_ERR);
43957089Sgjelinek 		}
43967089Sgjelinek 	}
43977089Sgjelinek 
43987089Sgjelinek 	if (cmdbuf[0] != '\0') {
43997089Sgjelinek 		/* Run the detach hook */
44007089Sgjelinek 		status = do_subproc_interactive(cmdbuf);
44017089Sgjelinek 		if ((status = subproc_status(gettext("brand-specific detach"),
44027089Sgjelinek 		    status, B_FALSE)) != ZONE_SUBPROC_OK) {
44037089Sgjelinek 			if (status == ZONE_SUBPROC_USAGE && !brand_help)
44047089Sgjelinek 				sub_usage(SHELP_DETACH, CMD_DETACH);
44057089Sgjelinek 
4406*8301Sgerald.jelinek@sun.com 			if (execute) {
4407*8301Sgerald.jelinek@sun.com 				assert(lockfd >= 0);
44087089Sgjelinek 				zonecfg_release_lock_file(target_zone, lockfd);
4409*8301Sgerald.jelinek@sun.com 				lockfd = -1;
4410*8301Sgerald.jelinek@sun.com 			}
4411*8301Sgerald.jelinek@sun.com 
4412*8301Sgerald.jelinek@sun.com 			assert(lockfd == -1);
44137089Sgjelinek 			return (Z_ERR);
44147089Sgjelinek 		}
44157089Sgjelinek 
44167089Sgjelinek 	} else {
4417*8301Sgerald.jelinek@sun.com 		zone_dochandle_t handle;
4418*8301Sgerald.jelinek@sun.com 
44197089Sgjelinek 		/* If just help, we're done since there is no brand help. */
4420*8301Sgerald.jelinek@sun.com 		if (brand_help) {
4421*8301Sgerald.jelinek@sun.com 			assert(lockfd == -1);
44227089Sgjelinek 			return (Z_OK);
4423*8301Sgerald.jelinek@sun.com 		}
44247089Sgjelinek 
44257089Sgjelinek 		/*
44267089Sgjelinek 		 * Run the built-in detach support.  Just generate a simple
44277089Sgjelinek 		 * zone definition XML file and detach.
44287089Sgjelinek 		 */
44297089Sgjelinek 
44307089Sgjelinek 		/* Don't detach the zone if anything is still mounted there */
44317089Sgjelinek 		if (execute && zonecfg_find_mounts(zonepath, NULL, NULL)) {
44327089Sgjelinek 			(void) fprintf(stderr, gettext("These file systems are "
44337089Sgjelinek 			    "mounted on subdirectories of %s.\n"), zonepath);
44347089Sgjelinek 			(void) zonecfg_find_mounts(zonepath, zfm_print, NULL);
44357089Sgjelinek 			err = ZONE_SUBPROC_NOTCOMPLETE;
44367089Sgjelinek 			goto done;
44377089Sgjelinek 		}
44387089Sgjelinek 
44397089Sgjelinek 		if ((handle = zonecfg_init_handle()) == NULL) {
44407089Sgjelinek 			zperror(cmd_to_str(CMD_DETACH), B_TRUE);
44417089Sgjelinek 			err = ZONE_SUBPROC_NOTCOMPLETE;
44427089Sgjelinek 			goto done;
44437089Sgjelinek 		}
44447089Sgjelinek 
44457089Sgjelinek 		if ((err = zonecfg_get_handle(target_zone, handle)) != Z_OK) {
44467089Sgjelinek 			errno = err;
44477089Sgjelinek 			zperror(cmd_to_str(CMD_DETACH), B_TRUE);
4448*8301Sgerald.jelinek@sun.com 
4449*8301Sgerald.jelinek@sun.com 		} else if ((err = zonecfg_detach_save(handle,
44507089Sgjelinek 		    (execute ? 0 : ZONE_DRY_RUN))) != Z_OK) {
44517089Sgjelinek 			errno = err;
44527089Sgjelinek 			zperror(gettext("saving the detach manifest failed"),
44537089Sgjelinek 			    B_TRUE);
44547089Sgjelinek 		}
44557089Sgjelinek 
44567089Sgjelinek 		zonecfg_fini_handle(handle);
4457*8301Sgerald.jelinek@sun.com 		if (err != Z_OK)
4458*8301Sgerald.jelinek@sun.com 			goto done;
44591507Sgjelinek 	}
44601507Sgjelinek 
44612078Sgjelinek 	/*
44622078Sgjelinek 	 * Set the zone state back to configured unless we are running with the
44632078Sgjelinek 	 * no-execute option.
44642078Sgjelinek 	 */
44652078Sgjelinek 	if (execute && (err = zone_set_state(target_zone,
44662078Sgjelinek 	    ZONE_STATE_CONFIGURED)) != Z_OK) {
44671507Sgjelinek 		errno = err;
44681507Sgjelinek 		zperror(gettext("could not reset state"), B_TRUE);
44691507Sgjelinek 	}
44701507Sgjelinek 
44711507Sgjelinek done:
4472*8301Sgerald.jelinek@sun.com 	if (execute) {
4473*8301Sgerald.jelinek@sun.com 		assert(lockfd >= 0);
44747089Sgjelinek 		zonecfg_release_lock_file(target_zone, lockfd);
4475*8301Sgerald.jelinek@sun.com 		lockfd = -1;
4476*8301Sgerald.jelinek@sun.com 	}
4477*8301Sgerald.jelinek@sun.com 
4478*8301Sgerald.jelinek@sun.com 	assert(lockfd == -1);
44791507Sgjelinek 	return ((err == Z_OK) ? Z_OK : Z_ERR);
44801507Sgjelinek }
44811507Sgjelinek 
44821507Sgjelinek /*
44837089Sgjelinek  * Determine the brand when doing a dry-run attach.  The zone does not have to
44847089Sgjelinek  * exist, so we have to read the incoming manifest to determine the zone's
44857089Sgjelinek  * brand.
44867089Sgjelinek  *
44877089Sgjelinek  * Because the manifest has to be processed twice; once to determine the brand
44887089Sgjelinek  * and once to do the brand-specific attach logic, we always read it into a tmp
44897089Sgjelinek  * file.  This handles the manifest coming from stdin or a regular file.  The
44907089Sgjelinek  * tmpname parameter returns the name of the temporary file that the manifest
44917089Sgjelinek  * was read into.
44921507Sgjelinek  */
44931507Sgjelinek static int
44947089Sgjelinek dryrun_get_brand(char *manifest_path, char *tmpname, int size)
44952078Sgjelinek {
44962078Sgjelinek 	int fd;
44972078Sgjelinek 	int err;
44987089Sgjelinek 	int res = Z_OK;
44992078Sgjelinek 	zone_dochandle_t local_handle;
45002078Sgjelinek 	zone_dochandle_t rem_handle = NULL;
45017089Sgjelinek 	int len;
45027089Sgjelinek 	int ofd;
45037089Sgjelinek 	char buf[512];
45042078Sgjelinek 
45052078Sgjelinek 	if (strcmp(manifest_path, "-") == 0) {
45067089Sgjelinek 		fd = STDIN_FILENO;
45077089Sgjelinek 	} else {
45087089Sgjelinek 		if ((fd = open(manifest_path, O_RDONLY)) < 0) {
45097089Sgjelinek 			if (getcwd(buf, sizeof (buf)) == NULL)
45107089Sgjelinek 				(void) strlcpy(buf, "/", sizeof (buf));
45117089Sgjelinek 			zerror(gettext("could not open manifest path %s%s: %s"),
45127089Sgjelinek 			    (*manifest_path == '/' ? "" : buf), manifest_path,
45137089Sgjelinek 			    strerror(errno));
45147089Sgjelinek 			return (Z_ERR);
45157089Sgjelinek 		}
45167089Sgjelinek 	}
45177089Sgjelinek 
45187089Sgjelinek 	(void) snprintf(tmpname, size, "/var/run/zone.%d", getpid());
45197089Sgjelinek 
45207089Sgjelinek 	if ((ofd = open(tmpname, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR)) < 0) {
45217089Sgjelinek 		zperror(gettext("could not save manifest"), B_FALSE);
45227089Sgjelinek 		(void) close(fd);
45237089Sgjelinek 		return (Z_ERR);
45247089Sgjelinek 	}
45257089Sgjelinek 
45267089Sgjelinek 	while ((len = read(fd, buf, sizeof (buf))) > 0) {
45277089Sgjelinek 		if (write(ofd, buf, len) == -1) {
45287089Sgjelinek 			zperror(gettext("could not save manifest"), B_FALSE);
45297089Sgjelinek 			(void) close(ofd);
45307089Sgjelinek 			(void) close(fd);
45317089Sgjelinek 			return (Z_ERR);
45327089Sgjelinek 		}
45337089Sgjelinek 	}
45347089Sgjelinek 
45357089Sgjelinek 	if (close(ofd) != 0) {
45367089Sgjelinek 		zperror(gettext("could not save manifest"), B_FALSE);
45377089Sgjelinek 		(void) close(fd);
45387089Sgjelinek 		return (Z_ERR);
45397089Sgjelinek 	}
45407089Sgjelinek 
45417089Sgjelinek 	(void) close(fd);
45427089Sgjelinek 
45437089Sgjelinek 	if ((fd = open(tmpname, O_RDONLY)) < 0) {
45442078Sgjelinek 		zperror(gettext("could not open manifest path"), B_FALSE);
45452078Sgjelinek 		return (Z_ERR);
45462078Sgjelinek 	}
45472078Sgjelinek 
45482078Sgjelinek 	if ((local_handle = zonecfg_init_handle()) == NULL) {
45492078Sgjelinek 		zperror(cmd_to_str(CMD_ATTACH), B_TRUE);
45502078Sgjelinek 		res = Z_ERR;
45512078Sgjelinek 		goto done;
45522078Sgjelinek 	}
45532078Sgjelinek 
45542078Sgjelinek 	if ((rem_handle = zonecfg_init_handle()) == NULL) {
45552078Sgjelinek 		zperror(cmd_to_str(CMD_ATTACH), B_TRUE);
45562078Sgjelinek 		res = Z_ERR;
45572078Sgjelinek 		goto done;
45582078Sgjelinek 	}
45592078Sgjelinek 
45602078Sgjelinek 	if ((err = zonecfg_attach_manifest(fd, local_handle, rem_handle))
45612078Sgjelinek 	    != Z_OK) {
45623686Sgjelinek 		res = Z_ERR;
45633686Sgjelinek 
45643686Sgjelinek 		if (err == Z_INVALID_DOCUMENT) {
45653686Sgjelinek 			struct stat st;
45663686Sgjelinek 			char buf[6];
45673686Sgjelinek 
45683686Sgjelinek 			if (strcmp(manifest_path, "-") == 0) {
45693686Sgjelinek 				zerror(gettext("Input is not a valid XML "
45703686Sgjelinek 				    "file"));
45713686Sgjelinek 				goto done;
45723686Sgjelinek 			}
45733686Sgjelinek 
45743686Sgjelinek 			if (fstat(fd, &st) == -1 || !S_ISREG(st.st_mode)) {
45753686Sgjelinek 				zerror(gettext("%s is not an XML file"),
45763686Sgjelinek 				    manifest_path);
45773686Sgjelinek 				goto done;
45783686Sgjelinek 			}
45793686Sgjelinek 
45803686Sgjelinek 			bzero(buf, sizeof (buf));
45813686Sgjelinek 			(void) lseek(fd, 0L, SEEK_SET);
45823686Sgjelinek 			if (read(fd, buf, sizeof (buf) - 1) < 0 ||
45833686Sgjelinek 			    strncmp(buf, "<?xml", 5) != 0)
45843686Sgjelinek 				zerror(gettext("%s is not an XML file"),
45853686Sgjelinek 				    manifest_path);
45863686Sgjelinek 			else
45873686Sgjelinek 				zerror(gettext("Cannot attach to an earlier "
45883686Sgjelinek 				    "release of the operating system"));
45893686Sgjelinek 		} else {
45902078Sgjelinek 			zperror(cmd_to_str(CMD_ATTACH), B_TRUE);
45913686Sgjelinek 		}
45922078Sgjelinek 		goto done;
45932078Sgjelinek 	}
45942078Sgjelinek 
45957089Sgjelinek 	/* Retrieve remote handle brand type. */
45963172Sgjelinek 	if (zonecfg_get_brand(rem_handle, target_brand, sizeof (target_brand))
45973172Sgjelinek 	    != Z_OK) {
45983172Sgjelinek 		zerror(gettext("missing or invalid brand"));
45993172Sgjelinek 		exit(Z_ERR);
46003172Sgjelinek 	}
46012078Sgjelinek 
46022078Sgjelinek done:
46032078Sgjelinek 	zonecfg_fini_handle(local_handle);
46042078Sgjelinek 	zonecfg_fini_handle(rem_handle);
46057089Sgjelinek 	(void) close(fd);
46062078Sgjelinek 
46072078Sgjelinek 	return ((res == Z_OK) ? Z_OK : Z_ERR);
46082078Sgjelinek }
46092078Sgjelinek 
46105829Sgjelinek /* ARGSUSED */
46111507Sgjelinek static int
46121507Sgjelinek attach_func(int argc, char *argv[])
46131507Sgjelinek {
4614*8301Sgerald.jelinek@sun.com 	int lockfd = -1;
46151507Sgjelinek 	int err, arg;
46161507Sgjelinek 	boolean_t force = B_FALSE;
46171507Sgjelinek 	zone_dochandle_t handle;
46181507Sgjelinek 	char zonepath[MAXPATHLEN];
46194785Sgjelinek 	char cmdbuf[MAXPATHLEN];
46207089Sgjelinek 	char postcmdbuf[MAXPATHLEN];
46212078Sgjelinek 	boolean_t execute = B_TRUE;
46227089Sgjelinek 	boolean_t brand_help = B_FALSE;
46232078Sgjelinek 	char *manifest_path;
46247089Sgjelinek 	char tmpmanifest[80];
46257089Sgjelinek 	int manifest_pos;
46264785Sgjelinek 	brand_handle_t bh = NULL;
46277089Sgjelinek 	int status;
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;
46377089Sgjelinek 	while ((arg = getopt(argc, argv, "?Fn:")) != 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;
46541507Sgjelinek 		default:
46557089Sgjelinek 			/* Ignore unknown options - may be brand specific. */
46567089Sgjelinek 			break;
46571507Sgjelinek 		}
46581507Sgjelinek 	}
46592078Sgjelinek 
46607089Sgjelinek 	if (brand_help) {
46617089Sgjelinek 		force = B_FALSE;
46627089Sgjelinek 		execute = B_TRUE;
46637089Sgjelinek 	}
46647089Sgjelinek 
46657089Sgjelinek 	/* dry-run and force flags are mutually exclusive */
46667089Sgjelinek 	if (!execute && force) {
46677089Sgjelinek 		zerror(gettext("-F and -n flags are mutually exclusive"));
46685829Sgjelinek 		return (Z_ERR);
46695829Sgjelinek 	}
46705829Sgjelinek 
46712078Sgjelinek 	/*
46727089Sgjelinek 	 * If the no-execute option was specified, we don't do validation and
46737089Sgjelinek 	 * need to figure out the brand, since there is no zone required to be
46742078Sgjelinek 	 * configured for this option.
46752078Sgjelinek 	 */
46767089Sgjelinek 	if (execute) {
46777089Sgjelinek 		if (!brand_help) {
46787089Sgjelinek 			if (sanity_check(target_zone, CMD_ATTACH, B_FALSE,
46797089Sgjelinek 			    B_TRUE, B_FALSE) != Z_OK)
46807089Sgjelinek 				return (Z_ERR);
46817089Sgjelinek 			if (verify_details(CMD_ATTACH, argv) != Z_OK)
46827089Sgjelinek 				return (Z_ERR);
46837089Sgjelinek 		}
46847089Sgjelinek 
46857089Sgjelinek 		if ((err = zone_get_zonepath(target_zone, zonepath,
46867089Sgjelinek 		    sizeof (zonepath))) != Z_OK) {
46877089Sgjelinek 			errno = err;
46887089Sgjelinek 			zperror2(target_zone,
46897089Sgjelinek 			    gettext("could not get zone path"));
46907089Sgjelinek 			return (Z_ERR);
46917089Sgjelinek 		}
46927089Sgjelinek 	} else {
46937089Sgjelinek 		if (dryrun_get_brand(manifest_path, tmpmanifest,
46947089Sgjelinek 		    sizeof (tmpmanifest)) != Z_OK)
46957089Sgjelinek 			return (Z_ERR);
46967089Sgjelinek 
46977089Sgjelinek 		argv[manifest_pos] = tmpmanifest;
46987089Sgjelinek 		target_zone = "-";
46997089Sgjelinek 		(void) strlcpy(zonepath, "-", sizeof (zonepath));
47007089Sgjelinek 
47017089Sgjelinek 		/* Run the brand's verify_adm hook. */
47027089Sgjelinek 		if (verify_brand(NULL, CMD_ATTACH, argv) != Z_OK)
47037089Sgjelinek 			return (Z_ERR);
47047089Sgjelinek 	}
47057089Sgjelinek 
47067089Sgjelinek 	/*
47077089Sgjelinek 	 * Fetch the attach and postattach hooks from the brand configuration.
47087089Sgjelinek 	 */
47094785Sgjelinek 	if ((bh = brand_open(target_brand)) == NULL) {
47104785Sgjelinek 		zerror(gettext("missing or invalid brand"));
47114785Sgjelinek 		return (Z_ERR);
47124785Sgjelinek 	}
47134785Sgjelinek 
47147089Sgjelinek 	if (get_hook(bh, cmdbuf, sizeof (cmdbuf), brand_get_attach, target_zone,
47157089Sgjelinek 	    zonepath) != Z_OK) {
47167089Sgjelinek 		zerror("invalid brand configuration: missing attach resource");
47177089Sgjelinek 		brand_close(bh);
47187089Sgjelinek 		return (Z_ERR);
47197089Sgjelinek 	}
47207089Sgjelinek 
47217089Sgjelinek 	if (get_hook(bh, postcmdbuf, sizeof (postcmdbuf), brand_get_postattach,
47227089Sgjelinek 	    target_zone, zonepath) != Z_OK) {
47234785Sgjelinek 		zerror("invalid brand configuration: missing postattach "
47244785Sgjelinek 		    "resource");
47254785Sgjelinek 		brand_close(bh);
47264785Sgjelinek 		return (Z_ERR);
47274785Sgjelinek 	}
47284785Sgjelinek 	brand_close(bh);
47294785Sgjelinek 
47307089Sgjelinek 	/* Append all options to attach hook. */
47317089Sgjelinek 	if (addoptions(cmdbuf, argv, sizeof (cmdbuf)) != Z_OK)
47327089Sgjelinek 		return (Z_ERR);
47337089Sgjelinek 
47347089Sgjelinek 	/* Append all options to postattach hook. */
47357089Sgjelinek 	if (addoptions(postcmdbuf, argv, sizeof (postcmdbuf)) != Z_OK)
47367089Sgjelinek 		return (Z_ERR);
47377089Sgjelinek 
47387089Sgjelinek 	if (execute && !brand_help) {
47397089Sgjelinek 		if (zonecfg_grab_lock_file(target_zone, &lockfd) != Z_OK) {
47407089Sgjelinek 			zerror(gettext("another %s may have an operation in "
47417089Sgjelinek 			    "progress."), "zoneadm");
47427089Sgjelinek 			return (Z_ERR);
47433777Sgjelinek 		}
4744*8301Sgerald.jelinek@sun.com 	}
4745*8301Sgerald.jelinek@sun.com 
4746*8301Sgerald.jelinek@sun.com 	if (!force) {
4747*8301Sgerald.jelinek@sun.com 		/*
4748*8301Sgerald.jelinek@sun.com 		 * Not a force-attach, so we need to actually do the work.
4749*8301Sgerald.jelinek@sun.com 		 */
4750*8301Sgerald.jelinek@sun.com 		if (cmdbuf[0] != '\0') {
4751*8301Sgerald.jelinek@sun.com 			/* Run the attach hook */
4752*8301Sgerald.jelinek@sun.com 			status = do_subproc_interactive(cmdbuf);
4753*8301Sgerald.jelinek@sun.com 			if ((status = subproc_status(gettext("brand-specific "
4754*8301Sgerald.jelinek@sun.com 			    "attach"), status, B_FALSE)) != ZONE_SUBPROC_OK) {
4755*8301Sgerald.jelinek@sun.com 				if (status == ZONE_SUBPROC_USAGE && !brand_help)
4756*8301Sgerald.jelinek@sun.com 					sub_usage(SHELP_ATTACH, CMD_ATTACH);
4757*8301Sgerald.jelinek@sun.com 
4758*8301Sgerald.jelinek@sun.com 				if (execute && !brand_help) {
4759*8301Sgerald.jelinek@sun.com 					assert(lockfd >= 0);
4760*8301Sgerald.jelinek@sun.com 					zonecfg_release_lock_file(target_zone,
4761*8301Sgerald.jelinek@sun.com 					    lockfd);
4762*8301Sgerald.jelinek@sun.com 					lockfd = -1;
4763*8301Sgerald.jelinek@sun.com 				}
4764*8301Sgerald.jelinek@sun.com 
4765*8301Sgerald.jelinek@sun.com 				assert(lockfd == -1);
4766*8301Sgerald.jelinek@sun.com 				return (Z_ERR);
4767*8301Sgerald.jelinek@sun.com 			}
4768*8301Sgerald.jelinek@sun.com 		}
4769*8301Sgerald.jelinek@sun.com 
4770*8301Sgerald.jelinek@sun.com 		/*
4771*8301Sgerald.jelinek@sun.com 		 * Else run the built-in attach support.
4772*8301Sgerald.jelinek@sun.com 		 * This is a no-op since there is nothing to validate.
4773*8301Sgerald.jelinek@sun.com 		 */
4774*8301Sgerald.jelinek@sun.com 
4775*8301Sgerald.jelinek@sun.com 		/* If dry-run or help, then we're done. */
4776*8301Sgerald.jelinek@sun.com 		if (!execute || brand_help) {
4777*8301Sgerald.jelinek@sun.com 			if (!execute)
4778*8301Sgerald.jelinek@sun.com 				(void) unlink(tmpmanifest);
4779*8301Sgerald.jelinek@sun.com 			assert(lockfd == -1);
4780*8301Sgerald.jelinek@sun.com 			return (Z_OK);
4781*8301Sgerald.jelinek@sun.com 		}
4782*8301Sgerald.jelinek@sun.com 	}
4783*8301Sgerald.jelinek@sun.com 
4784*8301Sgerald.jelinek@sun.com 	if ((handle = zonecfg_init_handle()) == NULL) {
4785*8301Sgerald.jelinek@sun.com 		zperror(cmd_to_str(CMD_ATTACH), B_TRUE);
4786*8301Sgerald.jelinek@sun.com 		err = Z_ERR;
4787*8301Sgerald.jelinek@sun.com 	} else if ((err = zonecfg_get_handle(target_zone, handle)) != Z_OK) {
4788*8301Sgerald.jelinek@sun.com 		errno = err;
4789*8301Sgerald.jelinek@sun.com 		zperror(cmd_to_str(CMD_ATTACH), B_TRUE);
4790*8301Sgerald.jelinek@sun.com 		zonecfg_fini_handle(handle);
4791*8301Sgerald.jelinek@sun.com 	} else {
4792*8301Sgerald.jelinek@sun.com 		zonecfg_rm_detached(handle, force);
4793*8301Sgerald.jelinek@sun.com 		zonecfg_fini_handle(handle);
4794*8301Sgerald.jelinek@sun.com 	}
4795*8301Sgerald.jelinek@sun.com 
4796*8301Sgerald.jelinek@sun.com 	if (err == Z_OK &&
4797*8301Sgerald.jelinek@sun.com 	    (err = zone_set_state(target_zone, ZONE_STATE_INSTALLED)) != Z_OK) {
47981507Sgjelinek 		errno = err;
47991507Sgjelinek 		zperror(gettext("could not reset state"), B_TRUE);
48001507Sgjelinek 	}
48011507Sgjelinek 
4802*8301Sgerald.jelinek@sun.com 	assert(lockfd >= 0);
48037089Sgjelinek 	zonecfg_release_lock_file(target_zone, lockfd);
4804*8301Sgerald.jelinek@sun.com 	lockfd = -1;
48051507Sgjelinek 
48064785Sgjelinek 	/* If we have a brand postattach hook, run it. */
48077089Sgjelinek 	if (err == Z_OK && !force && postcmdbuf[0] != '\0') {
48087089Sgjelinek 		status = do_subproc(postcmdbuf);
48094785Sgjelinek 		if (subproc_status(gettext("brand-specific postattach"),
48104785Sgjelinek 		    status, B_FALSE) != ZONE_SUBPROC_OK) {
48114785Sgjelinek 			if ((err = zone_set_state(target_zone,
48124785Sgjelinek 			    ZONE_STATE_CONFIGURED)) != Z_OK) {
48134785Sgjelinek 				errno = err;
48144785Sgjelinek 				zperror(gettext("could not reset state"),
48154785Sgjelinek 				    B_TRUE);
48164785Sgjelinek 			}
48174785Sgjelinek 		}
48184785Sgjelinek 	}
48194785Sgjelinek 
4820*8301Sgerald.jelinek@sun.com 	assert(lockfd == -1);
48211507Sgjelinek 	return ((err == Z_OK) ? Z_OK : Z_ERR);
48221507Sgjelinek }
48231507Sgjelinek 
48241300Sgjelinek /*
48250Sstevel@tonic-gate  * On input, TRUE => yes, FALSE => no.
48260Sstevel@tonic-gate  * On return, TRUE => 1, FALSE => 0, could not ask => -1.
48270Sstevel@tonic-gate  */
48280Sstevel@tonic-gate 
48290Sstevel@tonic-gate static int
48300Sstevel@tonic-gate ask_yesno(boolean_t default_answer, const char *question)
48310Sstevel@tonic-gate {
48320Sstevel@tonic-gate 	char line[64];	/* should be large enough to answer yes or no */
48330Sstevel@tonic-gate 
48340Sstevel@tonic-gate 	if (!isatty(STDIN_FILENO))
48350Sstevel@tonic-gate 		return (-1);
48360Sstevel@tonic-gate 	for (;;) {
48370Sstevel@tonic-gate 		(void) printf("%s (%s)? ", question,
48380Sstevel@tonic-gate 		    default_answer ? "[y]/n" : "y/[n]");
48390Sstevel@tonic-gate 		if (fgets(line, sizeof (line), stdin) == NULL ||
48400Sstevel@tonic-gate 		    line[0] == '\n')
48410Sstevel@tonic-gate 			return (default_answer ? 1 : 0);
48420Sstevel@tonic-gate 		if (tolower(line[0]) == 'y')
48430Sstevel@tonic-gate 			return (1);
48440Sstevel@tonic-gate 		if (tolower(line[0]) == 'n')
48450Sstevel@tonic-gate 			return (0);
48460Sstevel@tonic-gate 	}
48470Sstevel@tonic-gate }
48480Sstevel@tonic-gate 
48497089Sgjelinek /* ARGSUSED */
48500Sstevel@tonic-gate static int
48510Sstevel@tonic-gate uninstall_func(int argc, char *argv[])
48520Sstevel@tonic-gate {
48530Sstevel@tonic-gate 	char line[ZONENAME_MAX + 128];	/* Enough for "Are you sure ..." */
48541867Sgjelinek 	char rootpath[MAXPATHLEN], zonepath[MAXPATHLEN];
48554785Sgjelinek 	char cmdbuf[MAXPATHLEN];
48567089Sgjelinek 	char precmdbuf[MAXPATHLEN];
48570Sstevel@tonic-gate 	boolean_t force = B_FALSE;
48580Sstevel@tonic-gate 	int lockfd, answer;
48590Sstevel@tonic-gate 	int err, arg;
48607089Sgjelinek 	boolean_t brand_help = B_FALSE;
48614785Sgjelinek 	brand_handle_t bh = NULL;
48627089Sgjelinek 	int status;
48630Sstevel@tonic-gate 
4864766Scarlsonj 	if (zonecfg_in_alt_root()) {
4865766Scarlsonj 		zerror(gettext("cannot uninstall zone in alternate root"));
4866766Scarlsonj 		return (Z_ERR);
4867766Scarlsonj 	}
4868766Scarlsonj 
48697089Sgjelinek 	/* Check the argv string for args we handle internally */
48700Sstevel@tonic-gate 	optind = 0;
48717089Sgjelinek 	opterr = 0;
48720Sstevel@tonic-gate 	while ((arg = getopt(argc, argv, "?F")) != EOF) {
48730Sstevel@tonic-gate 		switch (arg) {
48740Sstevel@tonic-gate 		case '?':
48757089Sgjelinek 			if (optopt == '?') {
48767089Sgjelinek 				sub_usage(SHELP_UNINSTALL, CMD_UNINSTALL);
48777089Sgjelinek 				brand_help = B_TRUE;
48787089Sgjelinek 			}
48797089Sgjelinek 			/* Ignore unknown options - may be brand specific. */
48807089Sgjelinek 			break;
48810Sstevel@tonic-gate 		case 'F':
48820Sstevel@tonic-gate 			force = B_TRUE;
48830Sstevel@tonic-gate 			break;
48840Sstevel@tonic-gate 		default:
48857089Sgjelinek 			/* Ignore unknown options - may be brand specific. */
48867089Sgjelinek 			break;
48870Sstevel@tonic-gate 		}
48880Sstevel@tonic-gate 	}
48897089Sgjelinek 
48907089Sgjelinek 	if (!brand_help) {
48917089Sgjelinek 		if (sanity_check(target_zone, CMD_UNINSTALL, B_FALSE, B_TRUE,
48927089Sgjelinek 		    B_FALSE) != Z_OK)
48937089Sgjelinek 			return (Z_ERR);
48947089Sgjelinek 
48957089Sgjelinek 		/*
48967089Sgjelinek 		 * Invoke brand-specific handler.
48977089Sgjelinek 		 */
48987089Sgjelinek 		if (invoke_brand_handler(CMD_UNINSTALL, argv) != Z_OK)
48990Sstevel@tonic-gate 			return (Z_ERR);
49007089Sgjelinek 
49017089Sgjelinek 		if (!force) {
49027089Sgjelinek 			(void) snprintf(line, sizeof (line),
49037089Sgjelinek 			    gettext("Are you sure you want to %s zone %s"),
49047089Sgjelinek 			    cmd_to_str(CMD_UNINSTALL), target_zone);
49057089Sgjelinek 			if ((answer = ask_yesno(B_FALSE, line)) == 0) {
49067089Sgjelinek 				return (Z_OK);
49077089Sgjelinek 			} else if (answer == -1) {
49087089Sgjelinek 				zerror(gettext("Input not from terminal and -F "
49097089Sgjelinek 				    "not specified: %s not done."),
49107089Sgjelinek 				    cmd_to_str(CMD_UNINSTALL));
49117089Sgjelinek 				return (Z_ERR);
49127089Sgjelinek 			}
49130Sstevel@tonic-gate 		}
49140Sstevel@tonic-gate 	}
49150Sstevel@tonic-gate 
49161867Sgjelinek 	if ((err = zone_get_zonepath(target_zone, zonepath,
49171867Sgjelinek 	    sizeof (zonepath))) != Z_OK) {
49180Sstevel@tonic-gate 		errno = err;
49190Sstevel@tonic-gate 		zperror2(target_zone, gettext("could not get zone path"));
49200Sstevel@tonic-gate 		return (Z_ERR);
49210Sstevel@tonic-gate 	}
49220Sstevel@tonic-gate 
49230Sstevel@tonic-gate 	/*
49247089Sgjelinek 	 * Fetch the uninstall and preuninstall hooks from the brand
49257089Sgjelinek 	 * configuration.
49260Sstevel@tonic-gate 	 */
49274785Sgjelinek 	if ((bh = brand_open(target_brand)) == NULL) {
49284785Sgjelinek 		zerror(gettext("missing or invalid brand"));
49294785Sgjelinek 		return (Z_ERR);
49304785Sgjelinek 	}
49314785Sgjelinek 
49327089Sgjelinek 	if (get_hook(bh, cmdbuf, sizeof (cmdbuf), brand_get_uninstall,
49337089Sgjelinek 	    target_zone, zonepath) != Z_OK) {
49347089Sgjelinek 		zerror("invalid brand configuration: missing uninstall "
49357089Sgjelinek 		    "resource");
49367089Sgjelinek 		brand_close(bh);
49377089Sgjelinek 		return (Z_ERR);
49387089Sgjelinek 	}
49397089Sgjelinek 
49407089Sgjelinek 	if (get_hook(bh, precmdbuf, sizeof (precmdbuf), brand_get_preuninstall,
49417089Sgjelinek 	    target_zone, zonepath) != Z_OK) {
49424785Sgjelinek 		zerror("invalid brand configuration: missing preuninstall "
49434785Sgjelinek 		    "resource");
49444785Sgjelinek 		brand_close(bh);
49454785Sgjelinek 		return (Z_ERR);
49464785Sgjelinek 	}
49474785Sgjelinek 	brand_close(bh);
49484785Sgjelinek 
49497089Sgjelinek 	/* Append all options to preuninstall hook. */
49507089Sgjelinek 	if (addoptions(precmdbuf, argv, sizeof (precmdbuf)) != Z_OK)
49517089Sgjelinek 		return (Z_ERR);
49527089Sgjelinek 
49537089Sgjelinek 	/* Append all options to uninstall hook. */
49547089Sgjelinek 	if (addoptions(cmdbuf, argv, sizeof (cmdbuf)) != Z_OK)
49557089Sgjelinek 		return (Z_ERR);
49567089Sgjelinek 
49577089Sgjelinek 	if (!brand_help) {
49587089Sgjelinek 		if ((err = zone_get_rootpath(target_zone, rootpath,
49597089Sgjelinek 		    sizeof (rootpath))) != Z_OK) {
49607089Sgjelinek 			errno = err;
49617089Sgjelinek 			zperror2(target_zone, gettext("could not get root "
49627089Sgjelinek 			    "path"));
49634785Sgjelinek 			return (Z_ERR);
49644785Sgjelinek 		}
49654785Sgjelinek 
49667089Sgjelinek 		/*
49677089Sgjelinek 		 * If there seems to be a zoneadmd running for this zone, call
49687089Sgjelinek 		 * it to tell it that an uninstall is happening; if all goes
49697089Sgjelinek 		 * well it will then shut itself down.
49707089Sgjelinek 		 */
49717089Sgjelinek 		if (zonecfg_ping_zoneadmd(target_zone) == Z_OK) {
49727089Sgjelinek 			zone_cmd_arg_t zarg;
49737089Sgjelinek 			zarg.cmd = Z_NOTE_UNINSTALLING;
49747089Sgjelinek 			/* we don't care too much if this fails, just plow on */
49757089Sgjelinek 			(void) zonecfg_call_zoneadmd(target_zone, &zarg, locale,
49767089Sgjelinek 			    B_TRUE);
49777089Sgjelinek 		}
49787089Sgjelinek 
49797089Sgjelinek 		if (zonecfg_grab_lock_file(target_zone, &lockfd) != Z_OK) {
49807089Sgjelinek 			zerror(gettext("another %s may have an operation in "
49817089Sgjelinek 			    "progress."), "zoneadm");
49827089Sgjelinek 			return (Z_ERR);
49837089Sgjelinek 		}
49847089Sgjelinek 
49857089Sgjelinek 		/* Don't uninstall the zone if anything is mounted there */
49867089Sgjelinek 		err = zonecfg_find_mounts(rootpath, NULL, NULL);
49877089Sgjelinek 		if (err) {
49887089Sgjelinek 			zerror(gettext("These file systems are mounted on "
49897089Sgjelinek 			    "subdirectories of %s.\n"), rootpath);
49907089Sgjelinek 			(void) zonecfg_find_mounts(rootpath, zfm_print, NULL);
49917089Sgjelinek 			zonecfg_release_lock_file(target_zone, lockfd);
49927089Sgjelinek 			return (Z_ERR);
49937089Sgjelinek 		}
49947089Sgjelinek 	}
49957089Sgjelinek 
49967089Sgjelinek 	/* If we have a brand preuninstall hook, run it. */
49977089Sgjelinek 	if (!brand_help && precmdbuf[0] != '\0') {
49984785Sgjelinek 		status = do_subproc(cmdbuf);
49994785Sgjelinek 		if (subproc_status(gettext("brand-specific preuninstall"),
50004785Sgjelinek 		    status, B_FALSE) != ZONE_SUBPROC_OK) {
50017089Sgjelinek 			zonecfg_release_lock_file(target_zone, lockfd);
50024785Sgjelinek 			return (Z_ERR);
50034785Sgjelinek 		}
50044785Sgjelinek 	}
50054785Sgjelinek 
50067089Sgjelinek 	if (!brand_help) {
50077089Sgjelinek 		err = zone_set_state(target_zone, ZONE_STATE_INCOMPLETE);
50087089Sgjelinek 		if (err != Z_OK) {
50097089Sgjelinek 			errno = err;
50107089Sgjelinek 			zperror2(target_zone, gettext("could not set state"));
50117089Sgjelinek 			goto bad;
50127089Sgjelinek 		}
50137089Sgjelinek 	}
50147089Sgjelinek 
50157089Sgjelinek 	/*
50167089Sgjelinek 	 * If there is a brand uninstall hook, use it, otherwise use the
50177089Sgjelinek 	 * built-in uninstall code.
50187089Sgjelinek 	 */
50197089Sgjelinek 	if (cmdbuf[0] != '\0') {
50207089Sgjelinek 		/* Run the uninstall hook */
50217089Sgjelinek 		status = do_subproc_interactive(cmdbuf);
50227089Sgjelinek 		if ((status = subproc_status(gettext("brand-specific "
50237089Sgjelinek 		    "uninstall"), status, B_FALSE)) != ZONE_SUBPROC_OK) {
50247089Sgjelinek 			if (status == ZONE_SUBPROC_USAGE && !brand_help)
50257089Sgjelinek 				sub_usage(SHELP_UNINSTALL, CMD_UNINSTALL);
50267089Sgjelinek 			if (!brand_help)
50277089Sgjelinek 				zonecfg_release_lock_file(target_zone, lockfd);
50287089Sgjelinek 			return (Z_ERR);
50297089Sgjelinek 		}
50307089Sgjelinek 
50317089Sgjelinek 		if (brand_help)
50327089Sgjelinek 			return (Z_OK);
50337089Sgjelinek 	} else {
50347089Sgjelinek 		/* If just help, we're done since there is no brand help. */
50357089Sgjelinek 		if (brand_help)
50367089Sgjelinek 			return (Z_OK);
50377089Sgjelinek 
50387089Sgjelinek 		/* Run the built-in uninstall support. */
50397089Sgjelinek 		if ((err = cleanup_zonepath(zonepath, B_FALSE)) != Z_OK) {
50407089Sgjelinek 			errno = err;
50417089Sgjelinek 			zperror2(target_zone, gettext("cleaning up zonepath "
50427089Sgjelinek 			    "failed"));
50437089Sgjelinek 			goto bad;
50447089Sgjelinek 		}
50451867Sgjelinek 	}
50461867Sgjelinek 
50470Sstevel@tonic-gate 	err = zone_set_state(target_zone, ZONE_STATE_CONFIGURED);
50480Sstevel@tonic-gate 	if (err != Z_OK) {
50490Sstevel@tonic-gate 		errno = err;
50500Sstevel@tonic-gate 		zperror2(target_zone, gettext("could not reset state"));
50510Sstevel@tonic-gate 	}
50520Sstevel@tonic-gate bad:
50537089Sgjelinek 	zonecfg_release_lock_file(target_zone, lockfd);
50540Sstevel@tonic-gate 	return (err);
50550Sstevel@tonic-gate }
50560Sstevel@tonic-gate 
5057766Scarlsonj /* ARGSUSED */
5058766Scarlsonj static int
5059766Scarlsonj mount_func(int argc, char *argv[])
5060766Scarlsonj {
5061766Scarlsonj 	zone_cmd_arg_t zarg;
50622712Snn35248 	boolean_t force = B_FALSE;
50632712Snn35248 	int arg;
50642712Snn35248 
50652712Snn35248 	/*
50662712Snn35248 	 * The only supported subargument to the "mount" subcommand is
50672712Snn35248 	 * "-f", which forces us to mount a zone in the INCOMPLETE state.
50682712Snn35248 	 */
50692712Snn35248 	optind = 0;
50702712Snn35248 	if ((arg = getopt(argc, argv, "f")) != EOF) {
50712712Snn35248 		switch (arg) {
50722712Snn35248 		case 'f':
50732712Snn35248 			force = B_TRUE;
50742712Snn35248 			break;
50752712Snn35248 		default:
50762712Snn35248 			return (Z_USAGE);
50772712Snn35248 		}
50782712Snn35248 	}
50792712Snn35248 	if (argc > optind)
5080766Scarlsonj 		return (Z_USAGE);
50812712Snn35248 
50822712Snn35248 	if (sanity_check(target_zone, CMD_MOUNT, B_FALSE, B_FALSE, force)
50832712Snn35248 	    != Z_OK)
5084766Scarlsonj 		return (Z_ERR);
50853339Szt129084 	if (verify_details(CMD_MOUNT, argv) != Z_OK)
5086766Scarlsonj 		return (Z_ERR);
5087766Scarlsonj 
50882712Snn35248 	zarg.cmd = force ? Z_FORCEMOUNT : Z_MOUNT;
50895829Sgjelinek 	zarg.bootbuf[0] = '\0';
50907089Sgjelinek 	if (zonecfg_call_zoneadmd(target_zone, &zarg, locale, B_TRUE) != 0) {
5091766Scarlsonj 		zerror(gettext("call to %s failed"), "zoneadmd");
5092766Scarlsonj 		return (Z_ERR);
5093766Scarlsonj 	}
5094766Scarlsonj 	return (Z_OK);
5095766Scarlsonj }
5096766Scarlsonj 
5097766Scarlsonj /* ARGSUSED */
5098766Scarlsonj static int
5099766Scarlsonj unmount_func(int argc, char *argv[])
5100766Scarlsonj {
5101766Scarlsonj 	zone_cmd_arg_t zarg;
5102766Scarlsonj 
5103766Scarlsonj 	if (argc > 0)
5104766Scarlsonj 		return (Z_USAGE);
51052712Snn35248 	if (sanity_check(target_zone, CMD_UNMOUNT, B_FALSE, B_FALSE, B_FALSE)
51062712Snn35248 	    != Z_OK)
5107766Scarlsonj 		return (Z_ERR);
5108766Scarlsonj 
5109766Scarlsonj 	zarg.cmd = Z_UNMOUNT;
51107089Sgjelinek 	if (zonecfg_call_zoneadmd(target_zone, &zarg, locale, B_TRUE) != 0) {
5111766Scarlsonj 		zerror(gettext("call to %s failed"), "zoneadmd");
5112766Scarlsonj 		return (Z_ERR);
5113766Scarlsonj 	}
5114766Scarlsonj 	return (Z_OK);
5115766Scarlsonj }
5116766Scarlsonj 
51170Sstevel@tonic-gate static int
51182303Scarlsonj mark_func(int argc, char *argv[])
51192303Scarlsonj {
51202303Scarlsonj 	int err, lockfd;
51212303Scarlsonj 
51222303Scarlsonj 	if (argc != 1 || strcmp(argv[0], "incomplete") != 0)
51232303Scarlsonj 		return (Z_USAGE);
51242712Snn35248 	if (sanity_check(target_zone, CMD_MARK, B_FALSE, B_FALSE, B_FALSE)
51252712Snn35248 	    != Z_OK)
51262303Scarlsonj 		return (Z_ERR);
51272303Scarlsonj 
51283339Szt129084 	/*
51293339Szt129084 	 * Invoke brand-specific handler.
51303339Szt129084 	 */
51313339Szt129084 	if (invoke_brand_handler(CMD_MARK, argv) != Z_OK)
51323339Szt129084 		return (Z_ERR);
51333339Szt129084 
51347089Sgjelinek 	if (zonecfg_grab_lock_file(target_zone, &lockfd) != Z_OK) {
51352303Scarlsonj 		zerror(gettext("another %s may have an operation in progress."),
51362303Scarlsonj 		    "zoneadm");
51372303Scarlsonj 		return (Z_ERR);
51382303Scarlsonj 	}
51392303Scarlsonj 
51402303Scarlsonj 	err = zone_set_state(target_zone, ZONE_STATE_INCOMPLETE);
51412303Scarlsonj 	if (err != Z_OK) {
51422303Scarlsonj 		errno = err;
51432303Scarlsonj 		zperror2(target_zone, gettext("could not set state"));
51442303Scarlsonj 	}
51457089Sgjelinek 	zonecfg_release_lock_file(target_zone, lockfd);
51462303Scarlsonj 
51472303Scarlsonj 	return (err);
51482303Scarlsonj }
51492303Scarlsonj 
51503247Sgjelinek /*
51513247Sgjelinek  * Check what scheduling class we're running under and print a warning if
51523247Sgjelinek  * we're not using FSS.
51533247Sgjelinek  */
51543247Sgjelinek static int
51553247Sgjelinek check_sched_fss(zone_dochandle_t handle)
51563247Sgjelinek {
51573247Sgjelinek 	char class_name[PC_CLNMSZ];
51583247Sgjelinek 
51593247Sgjelinek 	if (zonecfg_get_dflt_sched_class(handle, class_name,
51603247Sgjelinek 	    sizeof (class_name)) != Z_OK) {
51613247Sgjelinek 		zerror(gettext("WARNING: unable to determine the zone's "
51623247Sgjelinek 		    "scheduling class"));
51633247Sgjelinek 	} else if (strcmp("FSS", class_name) != 0) {
51643247Sgjelinek 		zerror(gettext("WARNING: The zone.cpu-shares rctl is set but\n"
51653247Sgjelinek 		    "FSS is not the default scheduling class for this zone.  "
51663247Sgjelinek 		    "FSS will be\nused for processes in the zone but to get "
51673247Sgjelinek 		    "the full benefit of FSS,\nit should be the default "
51683247Sgjelinek 		    "scheduling class.  See dispadmin(1M) for\nmore details."));
51693247Sgjelinek 		return (Z_SYSTEM);
51703247Sgjelinek 	}
51713247Sgjelinek 
51723247Sgjelinek 	return (Z_OK);
51733247Sgjelinek }
51743247Sgjelinek 
51753247Sgjelinek static int
51763247Sgjelinek check_cpu_shares_sched(zone_dochandle_t handle)
51773247Sgjelinek {
51783247Sgjelinek 	int err;
51793247Sgjelinek 	int res = Z_OK;
51803247Sgjelinek 	struct zone_rctltab rctl;
51813247Sgjelinek 
51823247Sgjelinek 	if ((err = zonecfg_setrctlent(handle)) != Z_OK) {
51833247Sgjelinek 		errno = err;
51843247Sgjelinek 		zperror(cmd_to_str(CMD_APPLY), B_TRUE);
51853247Sgjelinek 		return (err);
51863247Sgjelinek 	}
51873247Sgjelinek 
51883247Sgjelinek 	while (zonecfg_getrctlent(handle, &rctl) == Z_OK) {
51893247Sgjelinek 		if (strcmp(rctl.zone_rctl_name, "zone.cpu-shares") == 0) {
51903247Sgjelinek 			if (check_sched_fss(handle) != Z_OK)
51913247Sgjelinek 				res = Z_SYSTEM;
51923247Sgjelinek 			break;
51933247Sgjelinek 		}
51943247Sgjelinek 	}
51953247Sgjelinek 
51963247Sgjelinek 	(void) zonecfg_endrctlent(handle);
51973247Sgjelinek 
51983247Sgjelinek 	return (res);
51993247Sgjelinek }
52003247Sgjelinek 
52013247Sgjelinek /*
52023352Sgjelinek  * Check if there is a mix of processes running in different pools within the
52033352Sgjelinek  * zone.  This is currently only going to be called for the global zone from
52043352Sgjelinek  * apply_func but that could be generalized in the future.
52053352Sgjelinek  */
52063352Sgjelinek static boolean_t
52073352Sgjelinek mixed_pools(zoneid_t zoneid)
52083352Sgjelinek {
52093352Sgjelinek 	DIR *dirp;
52103352Sgjelinek 	dirent_t *dent;
52113352Sgjelinek 	boolean_t mixed = B_FALSE;
52123352Sgjelinek 	boolean_t poolid_set = B_FALSE;
52133352Sgjelinek 	poolid_t last_poolid = 0;
52143352Sgjelinek 
52153352Sgjelinek 	if ((dirp = opendir("/proc")) == NULL) {
52163352Sgjelinek 		zerror(gettext("could not open /proc"));
52173352Sgjelinek 		return (B_FALSE);
52183352Sgjelinek 	}
52193352Sgjelinek 
52203352Sgjelinek 	while ((dent = readdir(dirp)) != NULL) {
52213352Sgjelinek 		int procfd;
52223352Sgjelinek 		psinfo_t ps;
52233352Sgjelinek 		char procpath[MAXPATHLEN];
52243352Sgjelinek 
52253352Sgjelinek 		if (dent->d_name[0] == '.')
52263352Sgjelinek 			continue;
52273352Sgjelinek 
52283352Sgjelinek 		(void) snprintf(procpath, sizeof (procpath), "/proc/%s/psinfo",
52293352Sgjelinek 		    dent->d_name);
52303352Sgjelinek 
52313352Sgjelinek 		if ((procfd = open(procpath, O_RDONLY)) == -1)
52323352Sgjelinek 			continue;
52333352Sgjelinek 
52343352Sgjelinek 		if (read(procfd, &ps, sizeof (ps)) == sizeof (psinfo_t)) {
52353352Sgjelinek 			/* skip processes in other zones and system processes */
52363352Sgjelinek 			if (zoneid != ps.pr_zoneid || ps.pr_flag & SSYS) {
52373352Sgjelinek 				(void) close(procfd);
52383352Sgjelinek 				continue;
52393352Sgjelinek 			}
52403352Sgjelinek 
52413352Sgjelinek 			if (poolid_set) {
52423352Sgjelinek 				if (ps.pr_poolid != last_poolid)
52433352Sgjelinek 					mixed = B_TRUE;
52443352Sgjelinek 			} else {
52453352Sgjelinek 				last_poolid = ps.pr_poolid;
52463352Sgjelinek 				poolid_set = B_TRUE;
52473352Sgjelinek 			}
52483352Sgjelinek 		}
52493352Sgjelinek 
52503352Sgjelinek 		(void) close(procfd);
52513352Sgjelinek 
52523352Sgjelinek 		if (mixed)
52533352Sgjelinek 			break;
52543352Sgjelinek 	}
52553352Sgjelinek 
52563352Sgjelinek 	(void) closedir(dirp);
52573352Sgjelinek 
52583352Sgjelinek 	return (mixed);
52593352Sgjelinek }
52603352Sgjelinek 
52613352Sgjelinek /*
52623352Sgjelinek  * Check if a persistent or temporary pool is configured for the zone.
52633352Sgjelinek  * This is currently only going to be called for the global zone from
52643352Sgjelinek  * apply_func but that could be generalized in the future.
52653352Sgjelinek  */
52663352Sgjelinek static boolean_t
52673352Sgjelinek pool_configured(zone_dochandle_t handle)
52683352Sgjelinek {
52693352Sgjelinek 	int err1, err2;
52703352Sgjelinek 	struct zone_psettab pset_tab;
52713352Sgjelinek 	char poolname[MAXPATHLEN];
52723352Sgjelinek 
52733352Sgjelinek 	err1 = zonecfg_lookup_pset(handle, &pset_tab);
52743352Sgjelinek 	err2 = zonecfg_get_pool(handle, poolname, sizeof (poolname));
52753352Sgjelinek 
52763352Sgjelinek 	if (err1 == Z_NO_ENTRY &&
52773352Sgjelinek 	    (err2 == Z_NO_ENTRY || (err2 == Z_OK && strlen(poolname) == 0)))
52783352Sgjelinek 		return (B_FALSE);
52793352Sgjelinek 
52803352Sgjelinek 	return (B_TRUE);
52813352Sgjelinek }
52823352Sgjelinek 
52833352Sgjelinek /*
52843247Sgjelinek  * This is an undocumented interface which is currently only used to apply
52853247Sgjelinek  * the global zone resource management settings when the system boots.
52863247Sgjelinek  * This function does not yet properly handle updating a running system so
52873247Sgjelinek  * any projects running in the zone would be trashed if this function
52883247Sgjelinek  * were to run after the zone had booted.  It also does not reset any
52893247Sgjelinek  * rctl settings that were removed from zonecfg.  There is still work to be
52903247Sgjelinek  * done before we can properly support dynamically updating the resource
52913247Sgjelinek  * management settings for a running zone (global or non-global).  Thus, this
52923247Sgjelinek  * functionality is undocumented for now.
52933247Sgjelinek  */
52943247Sgjelinek /* ARGSUSED */
52953247Sgjelinek static int
52963247Sgjelinek apply_func(int argc, char *argv[])
52973247Sgjelinek {
52983247Sgjelinek 	int err;
52993247Sgjelinek 	int res = Z_OK;
53003247Sgjelinek 	priv_set_t *privset;
53013247Sgjelinek 	zoneid_t zoneid;
53023247Sgjelinek 	zone_dochandle_t handle;
53033247Sgjelinek 	struct zone_mcaptab mcap;
53043247Sgjelinek 	char pool_err[128];
53053247Sgjelinek 
53063247Sgjelinek 	zoneid = getzoneid();
53073247Sgjelinek 
53083247Sgjelinek 	if (zonecfg_in_alt_root() || zoneid != GLOBAL_ZONEID ||
53093247Sgjelinek 	    target_zone == NULL || strcmp(target_zone, GLOBAL_ZONENAME) != 0)
53103247Sgjelinek 		return (usage(B_FALSE));
53113247Sgjelinek 
53123247Sgjelinek 	if ((privset = priv_allocset()) == NULL) {
53133247Sgjelinek 		zerror(gettext("%s failed"), "priv_allocset");
53143247Sgjelinek 		return (Z_ERR);
53153247Sgjelinek 	}
53163247Sgjelinek 
53173247Sgjelinek 	if (getppriv(PRIV_EFFECTIVE, privset) != 0) {
53183247Sgjelinek 		zerror(gettext("%s failed"), "getppriv");
53193247Sgjelinek 		priv_freeset(privset);
53203247Sgjelinek 		return (Z_ERR);
53213247Sgjelinek 	}
53223247Sgjelinek 
53233247Sgjelinek 	if (priv_isfullset(privset) == B_FALSE) {
53243247Sgjelinek 		(void) usage(B_FALSE);
53253247Sgjelinek 		priv_freeset(privset);
53263247Sgjelinek 		return (Z_ERR);
53273247Sgjelinek 	}
53283247Sgjelinek 	priv_freeset(privset);
53293247Sgjelinek 
53303247Sgjelinek 	if ((handle = zonecfg_init_handle()) == NULL) {
53313247Sgjelinek 		zperror(cmd_to_str(CMD_APPLY), B_TRUE);
53323247Sgjelinek 		return (Z_ERR);
53333247Sgjelinek 	}
53343247Sgjelinek 
53353247Sgjelinek 	if ((err = zonecfg_get_handle(target_zone, handle)) != Z_OK) {
53363247Sgjelinek 		errno = err;
53373247Sgjelinek 		zperror(cmd_to_str(CMD_APPLY), B_TRUE);
53383247Sgjelinek 		zonecfg_fini_handle(handle);
53393247Sgjelinek 		return (Z_ERR);
53403247Sgjelinek 	}
53413247Sgjelinek 
53423247Sgjelinek 	/* specific error msgs are printed within apply_rctls */
53433247Sgjelinek 	if ((err = zonecfg_apply_rctls(target_zone, handle)) != Z_OK) {
53443247Sgjelinek 		errno = err;
53453247Sgjelinek 		zperror(cmd_to_str(CMD_APPLY), B_TRUE);
53463247Sgjelinek 		res = Z_ERR;
53473247Sgjelinek 	}
53483247Sgjelinek 
53493247Sgjelinek 	if ((err = check_cpu_shares_sched(handle)) != Z_OK)
53503247Sgjelinek 		res = Z_ERR;
53513247Sgjelinek 
53523352Sgjelinek 	if (pool_configured(handle)) {
53533352Sgjelinek 		if (mixed_pools(zoneid)) {
53543352Sgjelinek 			zerror(gettext("Zone is using multiple resource "
53553352Sgjelinek 			    "pools.  The pool\nconfiguration cannot be "
53563352Sgjelinek 			    "applied without rebooting."));
53573352Sgjelinek 			res = Z_ERR;
53583352Sgjelinek 		} else {
53593352Sgjelinek 
53603352Sgjelinek 			/*
53613352Sgjelinek 			 * The next two blocks of code attempt to set up
53623352Sgjelinek 			 * temporary pools as well as persistent pools.  In
53633352Sgjelinek 			 * both cases we call the functions unconditionally.
53643352Sgjelinek 			 * Within each funtion the code will check if the zone
53653352Sgjelinek 			 * is actually configured for a temporary pool or
53663352Sgjelinek 			 * persistent pool and just return if there is nothing
53673352Sgjelinek 			 * to do.
53683352Sgjelinek 			 */
53693352Sgjelinek 			if ((err = zonecfg_bind_tmp_pool(handle, zoneid,
53703352Sgjelinek 			    pool_err, sizeof (pool_err))) != Z_OK) {
53713352Sgjelinek 				if (err == Z_POOL || err == Z_POOL_CREATE ||
53723352Sgjelinek 				    err == Z_POOL_BIND)
53733352Sgjelinek 					zerror("%s: %s", zonecfg_strerror(err),
53743352Sgjelinek 					    pool_err);
53753352Sgjelinek 				else
53763352Sgjelinek 					zerror(gettext("could not bind zone to "
53773352Sgjelinek 					    "temporary pool: %s"),
53783352Sgjelinek 					    zonecfg_strerror(err));
53793352Sgjelinek 				res = Z_ERR;
53803352Sgjelinek 			}
53813352Sgjelinek 
53823352Sgjelinek 			if ((err = zonecfg_bind_pool(handle, zoneid, pool_err,
53833352Sgjelinek 			    sizeof (pool_err))) != Z_OK) {
53843352Sgjelinek 				if (err == Z_POOL || err == Z_POOL_BIND)
53853352Sgjelinek 					zerror("%s: %s", zonecfg_strerror(err),
53863352Sgjelinek 					    pool_err);
53873352Sgjelinek 				else
53883352Sgjelinek 					zerror("%s", zonecfg_strerror(err));
53893352Sgjelinek 			}
53903352Sgjelinek 		}
53913247Sgjelinek 	}
53923247Sgjelinek 
53933247Sgjelinek 	/*
53943247Sgjelinek 	 * If a memory cap is configured, set the cap in the kernel using
53953247Sgjelinek 	 * zone_setattr() and make sure the rcapd SMF service is enabled.
53963247Sgjelinek 	 */
53973247Sgjelinek 	if (zonecfg_getmcapent(handle, &mcap) == Z_OK) {
53983247Sgjelinek 		uint64_t num;
53993247Sgjelinek 		char smf_err[128];
54003247Sgjelinek 
54013247Sgjelinek 		num = (uint64_t)strtoll(mcap.zone_physmem_cap, NULL, 10);
54023247Sgjelinek 		if (zone_setattr(zoneid, ZONE_ATTR_PHYS_MCAP, &num, 0) == -1) {
54033247Sgjelinek 			zerror(gettext("could not set zone memory cap"));
54043247Sgjelinek 			res = Z_ERR;
54053247Sgjelinek 		}
54063247Sgjelinek 
54073247Sgjelinek 		if (zonecfg_enable_rcapd(smf_err, sizeof (smf_err)) != Z_OK) {
54083247Sgjelinek 			zerror(gettext("enabling system/rcap service failed: "
54093247Sgjelinek 			    "%s"), smf_err);
54103247Sgjelinek 			res = Z_ERR;
54113247Sgjelinek 		}
54123247Sgjelinek 	}
54133247Sgjelinek 
54143247Sgjelinek 	zonecfg_fini_handle(handle);
54153247Sgjelinek 
54163247Sgjelinek 	return (res);
54173247Sgjelinek }
54183247Sgjelinek 
54192303Scarlsonj static int
54200Sstevel@tonic-gate help_func(int argc, char *argv[])
54210Sstevel@tonic-gate {
54220Sstevel@tonic-gate 	int arg, cmd_num;
54230Sstevel@tonic-gate 
54240Sstevel@tonic-gate 	if (argc == 0) {
54250Sstevel@tonic-gate 		(void) usage(B_TRUE);
54260Sstevel@tonic-gate 		return (Z_OK);
54270Sstevel@tonic-gate 	}
54280Sstevel@tonic-gate 	optind = 0;
54290Sstevel@tonic-gate 	if ((arg = getopt(argc, argv, "?")) != EOF) {
54300Sstevel@tonic-gate 		switch (arg) {
54310Sstevel@tonic-gate 		case '?':
54320Sstevel@tonic-gate 			sub_usage(SHELP_HELP, CMD_HELP);
54330Sstevel@tonic-gate 			return (optopt == '?' ? Z_OK : Z_USAGE);
54340Sstevel@tonic-gate 		default:
54350Sstevel@tonic-gate 			sub_usage(SHELP_HELP, CMD_HELP);
54360Sstevel@tonic-gate 			return (Z_USAGE);
54370Sstevel@tonic-gate 		}
54380Sstevel@tonic-gate 	}
54390Sstevel@tonic-gate 	while (optind < argc) {
5440988Scarlsonj 		/* Private commands have NULL short_usage; omit them */
5441988Scarlsonj 		if ((cmd_num = cmd_match(argv[optind])) < 0 ||
5442988Scarlsonj 		    cmdtab[cmd_num].short_usage == NULL) {
54430Sstevel@tonic-gate 			sub_usage(SHELP_HELP, CMD_HELP);
54440Sstevel@tonic-gate 			return (Z_USAGE);
54450Sstevel@tonic-gate 		}
54460Sstevel@tonic-gate 		sub_usage(cmdtab[cmd_num].short_usage, cmd_num);
54470Sstevel@tonic-gate 		optind++;
54480Sstevel@tonic-gate 	}
54490Sstevel@tonic-gate 	return (Z_OK);
54500Sstevel@tonic-gate }
54510Sstevel@tonic-gate 
54520Sstevel@tonic-gate /*
54530Sstevel@tonic-gate  * Returns: CMD_MIN thru CMD_MAX on success, -1 on error
54540Sstevel@tonic-gate  */
54550Sstevel@tonic-gate 
54560Sstevel@tonic-gate static int
54570Sstevel@tonic-gate cmd_match(char *cmd)
54580Sstevel@tonic-gate {
54590Sstevel@tonic-gate 	int i;
54600Sstevel@tonic-gate 
54610Sstevel@tonic-gate 	for (i = CMD_MIN; i <= CMD_MAX; i++) {
54620Sstevel@tonic-gate 		/* return only if there is an exact match */
54630Sstevel@tonic-gate 		if (strcmp(cmd, cmdtab[i].cmd_name) == 0)
54640Sstevel@tonic-gate 			return (cmdtab[i].cmd_num);
54650Sstevel@tonic-gate 	}
54660Sstevel@tonic-gate 	return (-1);
54670Sstevel@tonic-gate }
54680Sstevel@tonic-gate 
54690Sstevel@tonic-gate static int
54700Sstevel@tonic-gate parse_and_run(int argc, char *argv[])
54710Sstevel@tonic-gate {
54720Sstevel@tonic-gate 	int i = cmd_match(argv[0]);
54730Sstevel@tonic-gate 
54740Sstevel@tonic-gate 	if (i < 0)
54750Sstevel@tonic-gate 		return (usage(B_FALSE));
54760Sstevel@tonic-gate 	return (cmdtab[i].handler(argc - 1, &(argv[1])));
54770Sstevel@tonic-gate }
54780Sstevel@tonic-gate 
54790Sstevel@tonic-gate static char *
54800Sstevel@tonic-gate get_execbasename(char *execfullname)
54810Sstevel@tonic-gate {
54820Sstevel@tonic-gate 	char *last_slash, *execbasename;
54830Sstevel@tonic-gate 
54840Sstevel@tonic-gate 	/* guard against '/' at end of command invocation */
54850Sstevel@tonic-gate 	for (;;) {
54860Sstevel@tonic-gate 		last_slash = strrchr(execfullname, '/');
54870Sstevel@tonic-gate 		if (last_slash == NULL) {
54880Sstevel@tonic-gate 			execbasename = execfullname;
54890Sstevel@tonic-gate 			break;
54900Sstevel@tonic-gate 		} else {
54910Sstevel@tonic-gate 			execbasename = last_slash + 1;
54920Sstevel@tonic-gate 			if (*execbasename == '\0') {
54930Sstevel@tonic-gate 				*last_slash = '\0';
54940Sstevel@tonic-gate 				continue;
54950Sstevel@tonic-gate 			}
54960Sstevel@tonic-gate 			break;
54970Sstevel@tonic-gate 		}
54980Sstevel@tonic-gate 	}
54990Sstevel@tonic-gate 	return (execbasename);
55000Sstevel@tonic-gate }
55010Sstevel@tonic-gate 
55020Sstevel@tonic-gate int
55030Sstevel@tonic-gate main(int argc, char **argv)
55040Sstevel@tonic-gate {
55050Sstevel@tonic-gate 	int arg;
55060Sstevel@tonic-gate 	zoneid_t zid;
5507766Scarlsonj 	struct stat st;
55082712Snn35248 	char *zone_lock_env;
55092712Snn35248 	int err;
55100Sstevel@tonic-gate 
55110Sstevel@tonic-gate 	if ((locale = setlocale(LC_ALL, "")) == NULL)
55120Sstevel@tonic-gate 		locale = "C";
55130Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
55140Sstevel@tonic-gate 	setbuf(stdout, NULL);
55150Sstevel@tonic-gate 	(void) sigset(SIGHUP, SIG_IGN);
55160Sstevel@tonic-gate 	execname = get_execbasename(argv[0]);
55170Sstevel@tonic-gate 	target_zone = NULL;
55180Sstevel@tonic-gate 	if (chdir("/") != 0) {
55190Sstevel@tonic-gate 		zerror(gettext("could not change directory to /."));
55200Sstevel@tonic-gate 		exit(Z_ERR);
55210Sstevel@tonic-gate 	}
55220Sstevel@tonic-gate 
55232082Seschrock 	if (init_zfs() != Z_OK)
55242082Seschrock 		exit(Z_ERR);
55252082Seschrock 
55262303Scarlsonj 	while ((arg = getopt(argc, argv, "?u:z:R:")) != EOF) {
55270Sstevel@tonic-gate 		switch (arg) {
55280Sstevel@tonic-gate 		case '?':
55290Sstevel@tonic-gate 			return (usage(B_TRUE));
55302303Scarlsonj 		case 'u':
55312303Scarlsonj 			target_uuid = optarg;
55322303Scarlsonj 			break;
55330Sstevel@tonic-gate 		case 'z':
55340Sstevel@tonic-gate 			target_zone = optarg;
55350Sstevel@tonic-gate 			break;
5536766Scarlsonj 		case 'R':	/* private option for admin/install use */
5537766Scarlsonj 			if (*optarg != '/') {
5538766Scarlsonj 				zerror(gettext("root path must be absolute."));
5539766Scarlsonj 				exit(Z_ERR);
5540766Scarlsonj 			}
5541766Scarlsonj 			if (stat(optarg, &st) == -1 || !S_ISDIR(st.st_mode)) {
5542766Scarlsonj 				zerror(
5543766Scarlsonj 				    gettext("root path must be a directory."));
5544766Scarlsonj 				exit(Z_ERR);
5545766Scarlsonj 			}
5546766Scarlsonj 			zonecfg_set_root(optarg);
5547766Scarlsonj 			break;
55480Sstevel@tonic-gate 		default:
55490Sstevel@tonic-gate 			return (usage(B_FALSE));
55500Sstevel@tonic-gate 		}
55510Sstevel@tonic-gate 	}
55520Sstevel@tonic-gate 
55530Sstevel@tonic-gate 	if (optind >= argc)
55540Sstevel@tonic-gate 		return (usage(B_FALSE));
55552303Scarlsonj 
55562303Scarlsonj 	if (target_uuid != NULL && *target_uuid != '\0') {
55572303Scarlsonj 		uuid_t uuid;
55582303Scarlsonj 		static char newtarget[ZONENAME_MAX];
55592303Scarlsonj 
55602303Scarlsonj 		if (uuid_parse(target_uuid, uuid) == -1) {
55612303Scarlsonj 			zerror(gettext("illegal UUID value specified"));
55622303Scarlsonj 			exit(Z_ERR);
55632303Scarlsonj 		}
55642303Scarlsonj 		if (zonecfg_get_name_by_uuid(uuid, newtarget,
55652303Scarlsonj 		    sizeof (newtarget)) == Z_OK)
55662303Scarlsonj 			target_zone = newtarget;
55672303Scarlsonj 	}
55682303Scarlsonj 
55690Sstevel@tonic-gate 	if (target_zone != NULL && zone_get_id(target_zone, &zid) != 0) {
55700Sstevel@tonic-gate 		errno = Z_NO_ZONE;
55710Sstevel@tonic-gate 		zperror(target_zone, B_TRUE);
55720Sstevel@tonic-gate 		exit(Z_ERR);
55730Sstevel@tonic-gate 	}
55742712Snn35248 
55752712Snn35248 	/*
55762712Snn35248 	 * See if we have inherited the right to manipulate this zone from
55772712Snn35248 	 * a zoneadm instance in our ancestry.  If so, set zone_lock_cnt to
55782712Snn35248 	 * indicate it.  If not, make that explicit in our environment.
55792712Snn35248 	 */
55807089Sgjelinek 	zonecfg_init_lock_file(target_zone, &zone_lock_env);
55817089Sgjelinek 	if (zone_lock_env != NULL)
55822712Snn35248 		zoneadm_is_nested = B_TRUE;
55832712Snn35248 
55842712Snn35248 	/*
55852712Snn35248 	 * If we are going to be operating on a single zone, retrieve its
55862712Snn35248 	 * brand type and determine whether it is native or not.
55872712Snn35248 	 */
55882712Snn35248 	if ((target_zone != NULL) &&
55898057SJordan.Vaughan@Sun.com 	    (strcmp(target_zone, GLOBAL_ZONENAME) != 0)) {
55902712Snn35248 		if (zone_get_brand(target_zone, target_brand,
55912712Snn35248 		    sizeof (target_brand)) != Z_OK) {
55922712Snn35248 			zerror(gettext("missing or invalid brand"));
55932712Snn35248 			exit(Z_ERR);
55942712Snn35248 		}
55952712Snn35248 		is_native_zone = (strcmp(target_brand, NATIVE_BRAND_NAME) == 0);
55962712Snn35248 	}
55972712Snn35248 
55982712Snn35248 	err = parse_and_run(argc - optind, &argv[optind]);
55992712Snn35248 
56002712Snn35248 	return (err);
56010Sstevel@tonic-gate }
5602