xref: /onnv-gate/usr/src/cmd/zoneadm/zoneadm.c (revision 12841:b4f105cf454f)
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 /*
2312110SFrank.Batschulat@Sun.COM  * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate /*
270Sstevel@tonic-gate  * zoneadm is a command interpreter for zone administration.  It is all in
280Sstevel@tonic-gate  * C (i.e., no lex/yacc), and all the argument passing is argc/argv based.
290Sstevel@tonic-gate  * main() calls parse_and_run() which calls cmd_match(), then invokes the
300Sstevel@tonic-gate  * appropriate command's handler function.  The rest of the program is the
310Sstevel@tonic-gate  * handler functions and their helper functions.
320Sstevel@tonic-gate  *
330Sstevel@tonic-gate  * Some of the helper functions are used largely to simplify I18N: reducing
340Sstevel@tonic-gate  * the need for translation notes.  This is particularly true of many of
350Sstevel@tonic-gate  * the zerror() calls: doing e.g. zerror(gettext("%s failed"), "foo") rather
360Sstevel@tonic-gate  * than zerror(gettext("foo failed")) with a translation note indicating
370Sstevel@tonic-gate  * that "foo" need not be translated.
380Sstevel@tonic-gate  */
390Sstevel@tonic-gate 
400Sstevel@tonic-gate #include <stdio.h>
410Sstevel@tonic-gate #include <errno.h>
420Sstevel@tonic-gate #include <unistd.h>
430Sstevel@tonic-gate #include <signal.h>
440Sstevel@tonic-gate #include <stdarg.h>
450Sstevel@tonic-gate #include <ctype.h>
460Sstevel@tonic-gate #include <stdlib.h>
470Sstevel@tonic-gate #include <string.h>
480Sstevel@tonic-gate #include <wait.h>
490Sstevel@tonic-gate #include <zone.h>
500Sstevel@tonic-gate #include <priv.h>
510Sstevel@tonic-gate #include <locale.h>
520Sstevel@tonic-gate #include <libintl.h>
530Sstevel@tonic-gate #include <libzonecfg.h>
540Sstevel@tonic-gate #include <bsm/adt.h>
552712Snn35248 #include <sys/brand.h>
560Sstevel@tonic-gate #include <sys/param.h>
570Sstevel@tonic-gate #include <sys/types.h>
580Sstevel@tonic-gate #include <sys/stat.h>
590Sstevel@tonic-gate #include <sys/statvfs.h>
600Sstevel@tonic-gate #include <assert.h>
610Sstevel@tonic-gate #include <sys/sockio.h>
620Sstevel@tonic-gate #include <sys/mntent.h>
630Sstevel@tonic-gate #include <limits.h>
641867Sgjelinek #include <dirent.h>
652303Scarlsonj #include <uuid/uuid.h>
660Sstevel@tonic-gate #include <fcntl.h>
670Sstevel@tonic-gate #include <door.h>
680Sstevel@tonic-gate #include <macros.h>
690Sstevel@tonic-gate #include <libgen.h>
701300Sgjelinek #include <fnmatch.h>
711931Sgjelinek #include <sys/modctl.h>
722712Snn35248 #include <libbrand.h>
733247Sgjelinek #include <libscf.h>
743352Sgjelinek #include <procfs.h>
753686Sgjelinek #include <strings.h>
760Sstevel@tonic-gate #include <pool.h>
770Sstevel@tonic-gate #include <sys/pool.h>
783247Sgjelinek #include <sys/priocntl.h>
793247Sgjelinek #include <sys/fsspriocntl.h>
8010616SSebastien.Roy@Sun.COM #include <libdladm.h>
8110616SSebastien.Roy@Sun.COM #include <libdllink.h>
8212578SGlenn.Faden@Sun.COM #include <pwd.h>
8312578SGlenn.Faden@Sun.COM #include <auth_list.h>
8412578SGlenn.Faden@Sun.COM #include <auth_attr.h>
8512578SGlenn.Faden@Sun.COM #include <secdb.h>
860Sstevel@tonic-gate 
871867Sgjelinek #include "zoneadm.h"
881867Sgjelinek 
890Sstevel@tonic-gate #define	MAXARGS	8
9012578SGlenn.Faden@Sun.COM #define	SOURCE_ZONE (CMD_MAX + 1)
910Sstevel@tonic-gate 
920Sstevel@tonic-gate /* Reflects kernel zone entries */
930Sstevel@tonic-gate typedef struct zone_entry {
940Sstevel@tonic-gate 	zoneid_t	zid;
950Sstevel@tonic-gate 	char		zname[ZONENAME_MAX];
960Sstevel@tonic-gate 	char		*zstate_str;
970Sstevel@tonic-gate 	zone_state_t	zstate_num;
982712Snn35248 	char		zbrand[MAXNAMELEN];
990Sstevel@tonic-gate 	char		zroot[MAXPATHLEN];
1002303Scarlsonj 	char		zuuid[UUID_PRINTABLE_STRING_LENGTH];
1013448Sdh155122 	zone_iptype_t	ziptype;
1020Sstevel@tonic-gate } zone_entry_t;
1030Sstevel@tonic-gate 
1044350Std153743 #define	CLUSTER_BRAND_NAME	"cluster"
1054350Std153743 
1060Sstevel@tonic-gate static zone_entry_t *zents;
1070Sstevel@tonic-gate static size_t nzents;
1080Sstevel@tonic-gate 
1091915Sgjelinek #define	LOOPBACK_IF	"lo0"
1101915Sgjelinek #define	SOCKET_AF(af)	(((af) == AF_UNSPEC) ? AF_INET : (af))
1111915Sgjelinek 
1121915Sgjelinek struct net_if {
1131915Sgjelinek 	char	*name;
1141915Sgjelinek 	int	af;
1151915Sgjelinek };
1161915Sgjelinek 
1170Sstevel@tonic-gate /* 0755 is the default directory mode. */
1180Sstevel@tonic-gate #define	DEFAULT_DIR_MODE \
1190Sstevel@tonic-gate 	(S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH)
1200Sstevel@tonic-gate 
1210Sstevel@tonic-gate struct cmd {
1220Sstevel@tonic-gate 	uint_t	cmd_num;				/* command number */
1230Sstevel@tonic-gate 	char	*cmd_name;				/* command name */
1240Sstevel@tonic-gate 	char	*short_usage;				/* short form help */
1250Sstevel@tonic-gate 	int	(*handler)(int argc, char *argv[]);	/* function to call */
1260Sstevel@tonic-gate 
1270Sstevel@tonic-gate };
1280Sstevel@tonic-gate 
1290Sstevel@tonic-gate #define	SHELP_HELP	"help"
1302267Sdp #define	SHELP_BOOT	"boot [-- boot_arguments]"
1310Sstevel@tonic-gate #define	SHELP_HALT	"halt"
1320Sstevel@tonic-gate #define	SHELP_READY	"ready"
1332267Sdp #define	SHELP_REBOOT	"reboot [-- boot_arguments]"
1340Sstevel@tonic-gate #define	SHELP_LIST	"list [-cipv]"
1350Sstevel@tonic-gate #define	SHELP_VERIFY	"verify"
13612110SFrank.Batschulat@Sun.COM #define	SHELP_INSTALL	"install [brand-specific args]"
1377089Sgjelinek #define	SHELP_UNINSTALL	"uninstall [-F] [brand-specific args]"
1387089Sgjelinek #define	SHELP_CLONE	"clone [-m method] [-s <ZFS snapshot>] "\
1397089Sgjelinek 	"[brand-specific args] zonename"
1401300Sgjelinek #define	SHELP_MOVE	"move zonepath"
1417089Sgjelinek #define	SHELP_DETACH	"detach [-n] [brand-specific args]"
1427089Sgjelinek #define	SHELP_ATTACH	"attach [-F] [-n <path>] [brand-specific args]"
1432303Scarlsonj #define	SHELP_MARK	"mark incomplete"
1440Sstevel@tonic-gate 
1452712Snn35248 #define	EXEC_PREFIX	"exec "
1462712Snn35248 #define	EXEC_LEN	(strlen(EXEC_PREFIX))
1472712Snn35248 #define	RMCOMMAND	"/usr/bin/rm -rf"
1482712Snn35248 
1492712Snn35248 static int cleanup_zonepath(char *, boolean_t);
1502712Snn35248 
1513448Sdh155122 
1520Sstevel@tonic-gate static int help_func(int argc, char *argv[]);
1530Sstevel@tonic-gate static int ready_func(int argc, char *argv[]);
1540Sstevel@tonic-gate static int boot_func(int argc, char *argv[]);
1550Sstevel@tonic-gate static int halt_func(int argc, char *argv[]);
1560Sstevel@tonic-gate static int reboot_func(int argc, char *argv[]);
1570Sstevel@tonic-gate static int list_func(int argc, char *argv[]);
1580Sstevel@tonic-gate static int verify_func(int argc, char *argv[]);
1590Sstevel@tonic-gate static int install_func(int argc, char *argv[]);
1600Sstevel@tonic-gate static int uninstall_func(int argc, char *argv[]);
161766Scarlsonj static int mount_func(int argc, char *argv[]);
162766Scarlsonj static int unmount_func(int argc, char *argv[]);
1631300Sgjelinek static int clone_func(int argc, char *argv[]);
1641300Sgjelinek static int move_func(int argc, char *argv[]);
1651507Sgjelinek static int detach_func(int argc, char *argv[]);
1661507Sgjelinek static int attach_func(int argc, char *argv[]);
1672303Scarlsonj static int mark_func(int argc, char *argv[]);
1683247Sgjelinek static int apply_func(int argc, char *argv[]);
16910718SJordan.Vaughan@Sun.com static int sysboot_func(int argc, char *argv[]);
1700Sstevel@tonic-gate static int sanity_check(char *zone, int cmd_num, boolean_t running,
1712712Snn35248     boolean_t unsafe_when_running, boolean_t force);
1720Sstevel@tonic-gate static int cmd_match(char *cmd);
1733339Szt129084 static int verify_details(int, char *argv[]);
1743339Szt129084 static int verify_brand(zone_dochandle_t, int, char *argv[]);
1753339Szt129084 static int invoke_brand_handler(int, char *argv[]);
1760Sstevel@tonic-gate 
1770Sstevel@tonic-gate static struct cmd cmdtab[] = {
1780Sstevel@tonic-gate 	{ CMD_HELP,		"help",		SHELP_HELP,	help_func },
1790Sstevel@tonic-gate 	{ CMD_BOOT,		"boot",		SHELP_BOOT,	boot_func },
1800Sstevel@tonic-gate 	{ CMD_HALT,		"halt",		SHELP_HALT,	halt_func },
1810Sstevel@tonic-gate 	{ CMD_READY,		"ready",	SHELP_READY,	ready_func },
1820Sstevel@tonic-gate 	{ CMD_REBOOT,		"reboot",	SHELP_REBOOT,	reboot_func },
1830Sstevel@tonic-gate 	{ CMD_LIST,		"list",		SHELP_LIST,	list_func },
1840Sstevel@tonic-gate 	{ CMD_VERIFY,		"verify",	SHELP_VERIFY,	verify_func },
1850Sstevel@tonic-gate 	{ CMD_INSTALL,		"install",	SHELP_INSTALL,	install_func },
1860Sstevel@tonic-gate 	{ CMD_UNINSTALL,	"uninstall",	SHELP_UNINSTALL,
187766Scarlsonj 	    uninstall_func },
1881300Sgjelinek 	/* mount and unmount are private commands for admin/install */
189766Scarlsonj 	{ CMD_MOUNT,		"mount",	NULL,		mount_func },
1901300Sgjelinek 	{ CMD_UNMOUNT,		"unmount",	NULL,		unmount_func },
1911300Sgjelinek 	{ CMD_CLONE,		"clone",	SHELP_CLONE,	clone_func },
1921507Sgjelinek 	{ CMD_MOVE,		"move",		SHELP_MOVE,	move_func },
1931507Sgjelinek 	{ CMD_DETACH,		"detach",	SHELP_DETACH,	detach_func },
1942303Scarlsonj 	{ CMD_ATTACH,		"attach",	SHELP_ATTACH,	attach_func },
1953247Sgjelinek 	{ CMD_MARK,		"mark",		SHELP_MARK,	mark_func },
19610718SJordan.Vaughan@Sun.com 	{ CMD_APPLY,		"apply",	NULL,		apply_func },
19710718SJordan.Vaughan@Sun.com 	{ CMD_SYSBOOT,		"sysboot",	NULL,		sysboot_func }
1980Sstevel@tonic-gate };
1990Sstevel@tonic-gate 
2000Sstevel@tonic-gate /* global variables */
2010Sstevel@tonic-gate 
2020Sstevel@tonic-gate /* set early in main(), never modified thereafter, used all over the place */
2030Sstevel@tonic-gate static char *execname;
2042712Snn35248 static char target_brand[MAXNAMELEN];
20510943SEdward.Pilatowicz@Sun.COM static char default_brand[MAXPATHLEN];
2060Sstevel@tonic-gate static char *locale;
2071867Sgjelinek char *target_zone;
2082303Scarlsonj static char *target_uuid;
20912578SGlenn.Faden@Sun.COM char *username;
2100Sstevel@tonic-gate 
2111867Sgjelinek char *
cmd_to_str(int cmd_num)2120Sstevel@tonic-gate cmd_to_str(int cmd_num)
2130Sstevel@tonic-gate {
2140Sstevel@tonic-gate 	assert(cmd_num >= CMD_MIN && cmd_num <= CMD_MAX);
2150Sstevel@tonic-gate 	return (cmdtab[cmd_num].cmd_name);
2160Sstevel@tonic-gate }
2170Sstevel@tonic-gate 
2180Sstevel@tonic-gate /* This is a separate function because of gettext() wrapping. */
2190Sstevel@tonic-gate static char *
long_help(int cmd_num)2200Sstevel@tonic-gate long_help(int cmd_num)
2210Sstevel@tonic-gate {
222222Scomay 	assert(cmd_num >= CMD_MIN && cmd_num <= CMD_MAX);
2230Sstevel@tonic-gate 	switch (cmd_num) {
2241634Sgjelinek 	case CMD_HELP:
2251634Sgjelinek 		return (gettext("Print usage message."));
2261634Sgjelinek 	case CMD_BOOT:
2272267Sdp 		return (gettext("Activates (boots) specified zone.  See "
2282267Sdp 		    "zoneadm(1m) for valid boot\n\targuments."));
2291634Sgjelinek 	case CMD_HALT:
2301634Sgjelinek 		return (gettext("Halts specified zone, bypassing shutdown "
2311634Sgjelinek 		    "scripts and removing runtime\n\tresources of the zone."));
2321634Sgjelinek 	case CMD_READY:
2331634Sgjelinek 		return (gettext("Prepares a zone for running applications but "
2341634Sgjelinek 		    "does not start any user\n\tprocesses in the zone."));
2351634Sgjelinek 	case CMD_REBOOT:
2361634Sgjelinek 		return (gettext("Restarts the zone (equivalent to a halt / "
2372267Sdp 		    "boot sequence).\n\tFails if the zone is not active.  "
2382267Sdp 		    "See zoneadm(1m) for valid boot\n\targuments."));
2391634Sgjelinek 	case CMD_LIST:
2401634Sgjelinek 		return (gettext("Lists the current zones, or a "
2411634Sgjelinek 		    "specific zone if indicated.  By default,\n\tall "
2421634Sgjelinek 		    "running zones are listed, though this can be "
2431634Sgjelinek 		    "expanded to all\n\tinstalled zones with the -i "
2441634Sgjelinek 		    "option or all configured zones with the\n\t-c "
2452303Scarlsonj 		    "option.  When used with the general -z <zone> and/or -u "
2462303Scarlsonj 		    "<uuid-match>\n\toptions, lists only the specified "
2472303Scarlsonj 		    "matching zone, but lists it\n\tregardless of its state, "
2482303Scarlsonj 		    "and the -i and -c options are disallowed.  The\n\t-v "
2492303Scarlsonj 		    "option can be used to display verbose information: zone "
2502303Scarlsonj 		    "name, id,\n\tcurrent state, root directory and options.  "
2512303Scarlsonj 		    "The -p option can be used\n\tto request machine-parsable "
2522303Scarlsonj 		    "output.  The -v and -p options are mutually\n\texclusive."
2532303Scarlsonj 		    "  If neither -v nor -p is used, just the zone name is "
2542303Scarlsonj 		    "listed."));
2551634Sgjelinek 	case CMD_VERIFY:
2561634Sgjelinek 		return (gettext("Check to make sure the configuration "
2571634Sgjelinek 		    "can safely be instantiated\n\ton the machine: "
2581634Sgjelinek 		    "physical network interfaces exist, etc."));
2591634Sgjelinek 	case CMD_INSTALL:
2601867Sgjelinek 		return (gettext("Install the configuration on to the system.  "
26112110SFrank.Batschulat@Sun.COM 		    "All arguments are passed to the brand installation "
2627089Sgjelinek 		    "function;\n\tsee brands(5) for more information."));
2631634Sgjelinek 	case CMD_UNINSTALL:
2641634Sgjelinek 		return (gettext("Uninstall the configuration from the system.  "
2657089Sgjelinek 		    "The -F flag can be used\n\tto force the action.  All "
2667089Sgjelinek 		    "other arguments are passed to the brand\n\tuninstall "
2677089Sgjelinek 		    "function; see brands(5) for more information."));
2681634Sgjelinek 	case CMD_CLONE:
2691867Sgjelinek 		return (gettext("Clone the installation of another zone.  "
2701867Sgjelinek 		    "The -m option can be used to\n\tspecify 'copy' which "
2711867Sgjelinek 		    "forces a copy of the source zone.  The -s option\n\t"
2721867Sgjelinek 		    "can be used to specify the name of a ZFS snapshot "
2731867Sgjelinek 		    "that was taken from\n\ta previous clone command.  The "
2741867Sgjelinek 		    "snapshot will be used as the source\n\tinstead of "
2757089Sgjelinek 		    "creating a new ZFS snapshot.  All other arguments are "
2767089Sgjelinek 		    "passed\n\tto the brand clone function; see "
2777089Sgjelinek 		    "brands(5) for more information."));
2781634Sgjelinek 	case CMD_MOVE:
2791634Sgjelinek 		return (gettext("Move the zone to a new zonepath."));
2801634Sgjelinek 	case CMD_DETACH:
2811634Sgjelinek 		return (gettext("Detach the zone from the system. The zone "
2821634Sgjelinek 		    "state is changed to\n\t'configured' (but the files under "
2831634Sgjelinek 		    "the zonepath are untouched).\n\tThe zone can subsequently "
2841634Sgjelinek 		    "be attached, or can be moved to another\n\tsystem and "
2852078Sgjelinek 		    "attached there.  The -n option can be used to specify\n\t"
2862078Sgjelinek 		    "'no-execute' mode.  When -n is used, the information "
2872078Sgjelinek 		    "needed to attach\n\tthe zone is sent to standard output "
2887089Sgjelinek 		    "but the zone is not actually\n\tdetached.  All other "
2897089Sgjelinek 		    "arguments are passed to the brand detach function;\n\tsee "
2907089Sgjelinek 		    "brands(5) for more information."));
2911634Sgjelinek 	case CMD_ATTACH:
2921634Sgjelinek 		return (gettext("Attach the zone to the system.  The zone "
2931634Sgjelinek 		    "state must be 'configured'\n\tprior to attach; upon "
2941634Sgjelinek 		    "successful completion, the zone state will be\n\t"
2951634Sgjelinek 		    "'installed'.  The system software on the current "
2961634Sgjelinek 		    "system must be\n\tcompatible with the software on the "
2977089Sgjelinek 		    "zone's original system.\n\tSpecify -F "
2985829Sgjelinek 		    "to force the attach and skip software compatibility "
2995829Sgjelinek 		    "tests.\n\tThe -n option can be used to specify "
3005829Sgjelinek 		    "'no-execute' mode.  When -n is\n\tused, the information "
3015829Sgjelinek 		    "needed to attach the zone is read from the\n\tspecified "
3025829Sgjelinek 		    "path and the configuration is only validated.  The path "
3037089Sgjelinek 		    "can\n\tbe '-' to specify standard input.  The -F and -n "
3047089Sgjelinek 		    "options are mutually\n\texclusive.  All other arguments "
3057089Sgjelinek 		    "are passed to the brand attach\n\tfunction; see "
3067089Sgjelinek 		    "brands(5) for more information."));
3072303Scarlsonj 	case CMD_MARK:
3082303Scarlsonj 		return (gettext("Set the state of the zone.  This can be used "
3092303Scarlsonj 		    "to force the zone\n\tstate to 'incomplete' "
3102303Scarlsonj 		    "administratively if some activity has rendered\n\tthe "
3112303Scarlsonj 		    "zone permanently unusable.  The only valid state that "
3122303Scarlsonj 		    "may be\n\tspecified is 'incomplete'."));
3131634Sgjelinek 	default:
3141634Sgjelinek 		return ("");
3150Sstevel@tonic-gate 	}
3160Sstevel@tonic-gate 	/* NOTREACHED */
317222Scomay 	return (NULL);
3180Sstevel@tonic-gate }
3190Sstevel@tonic-gate 
3200Sstevel@tonic-gate /*
3210Sstevel@tonic-gate  * Called with explicit B_TRUE when help is explicitly requested, B_FALSE for
3220Sstevel@tonic-gate  * unexpected errors.
3230Sstevel@tonic-gate  */
3240Sstevel@tonic-gate 
3250Sstevel@tonic-gate static int
usage(boolean_t explicit)3260Sstevel@tonic-gate usage(boolean_t explicit)
3270Sstevel@tonic-gate {
3280Sstevel@tonic-gate 	int i;
3290Sstevel@tonic-gate 	FILE *fd = explicit ? stdout : stderr;
3300Sstevel@tonic-gate 
3310Sstevel@tonic-gate 	(void) fprintf(fd, "%s:\t%s help\n", gettext("usage"), execname);
3322303Scarlsonj 	(void) fprintf(fd, "\t%s [-z <zone>] [-u <uuid-match>] list\n",
3332303Scarlsonj 	    execname);
3342303Scarlsonj 	(void) fprintf(fd, "\t%s {-z <zone>|-u <uuid-match>} <%s>\n", execname,
3350Sstevel@tonic-gate 	    gettext("subcommand"));
3360Sstevel@tonic-gate 	(void) fprintf(fd, "\n%s:\n\n", gettext("Subcommands"));
3370Sstevel@tonic-gate 	for (i = CMD_MIN; i <= CMD_MAX; i++) {
338766Scarlsonj 		if (cmdtab[i].short_usage == NULL)
339766Scarlsonj 			continue;
3400Sstevel@tonic-gate 		(void) fprintf(fd, "%s\n", cmdtab[i].short_usage);
3410Sstevel@tonic-gate 		if (explicit)
3420Sstevel@tonic-gate 			(void) fprintf(fd, "\t%s\n\n", long_help(i));
3430Sstevel@tonic-gate 	}
3440Sstevel@tonic-gate 	if (!explicit)
3450Sstevel@tonic-gate 		(void) fputs("\n", fd);
3460Sstevel@tonic-gate 	return (Z_USAGE);
3470Sstevel@tonic-gate }
3480Sstevel@tonic-gate 
3490Sstevel@tonic-gate static void
sub_usage(char * short_usage,int cmd_num)3500Sstevel@tonic-gate sub_usage(char *short_usage, int cmd_num)
3510Sstevel@tonic-gate {
3520Sstevel@tonic-gate 	(void) fprintf(stderr, "%s:\t%s\n", gettext("usage"), short_usage);
3530Sstevel@tonic-gate 	(void) fprintf(stderr, "\t%s\n", long_help(cmd_num));
3540Sstevel@tonic-gate }
3550Sstevel@tonic-gate 
3560Sstevel@tonic-gate /*
3570Sstevel@tonic-gate  * zperror() is like perror(3c) except that this also prints the executable
3580Sstevel@tonic-gate  * name at the start of the message, and takes a boolean indicating whether
3590Sstevel@tonic-gate  * to call libc'c strerror() or that from libzonecfg.
3600Sstevel@tonic-gate  */
3610Sstevel@tonic-gate 
3621867Sgjelinek void
zperror(const char * str,boolean_t zonecfg_error)3630Sstevel@tonic-gate zperror(const char *str, boolean_t zonecfg_error)
3640Sstevel@tonic-gate {
3650Sstevel@tonic-gate 	(void) fprintf(stderr, "%s: %s: %s\n", execname, str,
3660Sstevel@tonic-gate 	    zonecfg_error ? zonecfg_strerror(errno) : strerror(errno));
3670Sstevel@tonic-gate }
3680Sstevel@tonic-gate 
3690Sstevel@tonic-gate /*
3700Sstevel@tonic-gate  * zperror2() is very similar to zperror() above, except it also prints a
3710Sstevel@tonic-gate  * supplied zone name after the executable.
3720Sstevel@tonic-gate  *
3730Sstevel@tonic-gate  * All current consumers of this function want libzonecfg's strerror() rather
3740Sstevel@tonic-gate  * than libc's; if this ever changes, this function can be made more generic
3750Sstevel@tonic-gate  * like zperror() above.
3760Sstevel@tonic-gate  */
3770Sstevel@tonic-gate 
3781867Sgjelinek void
zperror2(const char * zone,const char * str)3790Sstevel@tonic-gate zperror2(const char *zone, const char *str)
3800Sstevel@tonic-gate {
3810Sstevel@tonic-gate 	(void) fprintf(stderr, "%s: %s: %s: %s\n", execname, zone, str,
3820Sstevel@tonic-gate 	    zonecfg_strerror(errno));
3830Sstevel@tonic-gate }
3840Sstevel@tonic-gate 
3850Sstevel@tonic-gate /* PRINTFLIKE1 */
3861867Sgjelinek void
zerror(const char * fmt,...)3870Sstevel@tonic-gate zerror(const char *fmt, ...)
3880Sstevel@tonic-gate {
3890Sstevel@tonic-gate 	va_list alist;
3900Sstevel@tonic-gate 
3910Sstevel@tonic-gate 	va_start(alist, fmt);
3920Sstevel@tonic-gate 	(void) fprintf(stderr, "%s: ", execname);
3930Sstevel@tonic-gate 	if (target_zone != NULL)
3940Sstevel@tonic-gate 		(void) fprintf(stderr, "zone '%s': ", target_zone);
3950Sstevel@tonic-gate 	(void) vfprintf(stderr, fmt, alist);
3960Sstevel@tonic-gate 	(void) fprintf(stderr, "\n");
3970Sstevel@tonic-gate 	va_end(alist);
3980Sstevel@tonic-gate }
3990Sstevel@tonic-gate 
4000Sstevel@tonic-gate static void *
safe_calloc(size_t nelem,size_t elsize)4010Sstevel@tonic-gate safe_calloc(size_t nelem, size_t elsize)
4020Sstevel@tonic-gate {
4030Sstevel@tonic-gate 	void *r = calloc(nelem, elsize);
4040Sstevel@tonic-gate 
4050Sstevel@tonic-gate 	if (r == NULL) {
4060Sstevel@tonic-gate 		zerror(gettext("failed to allocate %lu bytes: %s"),
4070Sstevel@tonic-gate 		    (ulong_t)nelem * elsize, strerror(errno));
4080Sstevel@tonic-gate 		exit(Z_ERR);
4090Sstevel@tonic-gate 	}
4100Sstevel@tonic-gate 	return (r);
4110Sstevel@tonic-gate }
4120Sstevel@tonic-gate 
4130Sstevel@tonic-gate static void
zone_print(zone_entry_t * zent,boolean_t verbose,boolean_t parsable)4140Sstevel@tonic-gate zone_print(zone_entry_t *zent, boolean_t verbose, boolean_t parsable)
4150Sstevel@tonic-gate {
4160Sstevel@tonic-gate 	static boolean_t firsttime = B_TRUE;
4173448Sdh155122 	char *ip_type_str;
4183448Sdh155122 
4198942Sgerald.jelinek@sun.com 	/* Skip a zone that shutdown while we were collecting data. */
4208942Sgerald.jelinek@sun.com 	if (zent->zname[0] == '\0')
4218942Sgerald.jelinek@sun.com 		return;
4228942Sgerald.jelinek@sun.com 
4233448Sdh155122 	if (zent->ziptype == ZS_EXCLUSIVE)
4243448Sdh155122 		ip_type_str = "excl";
4253448Sdh155122 	else
4263448Sdh155122 		ip_type_str = "shared";
4270Sstevel@tonic-gate 
4280Sstevel@tonic-gate 	assert(!(verbose && parsable));
4290Sstevel@tonic-gate 	if (firsttime && verbose) {
4300Sstevel@tonic-gate 		firsttime = B_FALSE;
4313448Sdh155122 		(void) printf("%*s %-16s %-10s %-30s %-8s %-6s\n",
4323448Sdh155122 		    ZONEID_WIDTH, "ID", "NAME", "STATUS", "PATH", "BRAND",
4333448Sdh155122 		    "IP");
4340Sstevel@tonic-gate 	}
4350Sstevel@tonic-gate 	if (!verbose) {
4362303Scarlsonj 		char *cp, *clim;
4372303Scarlsonj 
4380Sstevel@tonic-gate 		if (!parsable) {
4390Sstevel@tonic-gate 			(void) printf("%s\n", zent->zname);
4400Sstevel@tonic-gate 			return;
4410Sstevel@tonic-gate 		}
4420Sstevel@tonic-gate 		if (zent->zid == ZONE_ID_UNDEFINED)
4430Sstevel@tonic-gate 			(void) printf("-");
4440Sstevel@tonic-gate 		else
4450Sstevel@tonic-gate 			(void) printf("%lu", zent->zid);
4462303Scarlsonj 		(void) printf(":%s:%s:", zent->zname, zent->zstate_str);
4472303Scarlsonj 		cp = zent->zroot;
4482303Scarlsonj 		while ((clim = strchr(cp, ':')) != NULL) {
4492303Scarlsonj 			(void) printf("%.*s\\:", clim - cp, cp);
4502303Scarlsonj 			cp = clim + 1;
4512303Scarlsonj 		}
4523448Sdh155122 		(void) printf("%s:%s:%s:%s\n", cp, zent->zuuid, zent->zbrand,
4533448Sdh155122 		    ip_type_str);
4540Sstevel@tonic-gate 		return;
4550Sstevel@tonic-gate 	}
4560Sstevel@tonic-gate 	if (zent->zstate_str != NULL) {
4570Sstevel@tonic-gate 		if (zent->zid == ZONE_ID_UNDEFINED)
4580Sstevel@tonic-gate 			(void) printf("%*s", ZONEID_WIDTH, "-");
4590Sstevel@tonic-gate 		else
4600Sstevel@tonic-gate 			(void) printf("%*lu", ZONEID_WIDTH, zent->zid);
4613448Sdh155122 		(void) printf(" %-16s %-10s %-30s %-8s %-6s\n", zent->zname,
4623448Sdh155122 		    zent->zstate_str, zent->zroot, zent->zbrand, ip_type_str);
4630Sstevel@tonic-gate 	}
4640Sstevel@tonic-gate }
4650Sstevel@tonic-gate 
4660Sstevel@tonic-gate static int
lookup_zone_info(const char * zone_name,zoneid_t zid,zone_entry_t * zent)467766Scarlsonj lookup_zone_info(const char *zone_name, zoneid_t zid, zone_entry_t *zent)
4680Sstevel@tonic-gate {
4691676Sjpk 	char root[MAXPATHLEN], *cp;
4700Sstevel@tonic-gate 	int err;
4712303Scarlsonj 	uuid_t uuid;
4728942Sgerald.jelinek@sun.com 	zone_dochandle_t handle;
4730Sstevel@tonic-gate 
4740Sstevel@tonic-gate 	(void) strlcpy(zent->zname, zone_name, sizeof (zent->zname));
4750Sstevel@tonic-gate 	(void) strlcpy(zent->zroot, "???", sizeof (zent->zroot));
4762712Snn35248 	(void) strlcpy(zent->zbrand, "???", sizeof (zent->zbrand));
4770Sstevel@tonic-gate 	zent->zstate_str = "???";
4780Sstevel@tonic-gate 
479766Scarlsonj 	zent->zid = zid;
4800Sstevel@tonic-gate 
4812303Scarlsonj 	if (zonecfg_get_uuid(zone_name, uuid) == Z_OK &&
4822303Scarlsonj 	    !uuid_is_null(uuid))
4832303Scarlsonj 		uuid_unparse(uuid, zent->zuuid);
4842303Scarlsonj 	else
4852303Scarlsonj 		zent->zuuid[0] = '\0';
4862303Scarlsonj 
4871676Sjpk 	/*
4881676Sjpk 	 * For labeled zones which query the zone path of lower-level
4891676Sjpk 	 * zones, the path needs to be adjusted to drop the final
4901676Sjpk 	 * "/root" component. This adjusted path is then useful
4911676Sjpk 	 * for reading down any exported directories from the
4921676Sjpk 	 * lower-level zone.
4931676Sjpk 	 */
4941676Sjpk 	if (is_system_labeled() && zent->zid != ZONE_ID_UNDEFINED) {
4951676Sjpk 		if (zone_getattr(zent->zid, ZONE_ATTR_ROOT, zent->zroot,
4961676Sjpk 		    sizeof (zent->zroot)) == -1) {
4971676Sjpk 			zperror2(zent->zname,
4981676Sjpk 			    gettext("could not get zone path."));
4991676Sjpk 			return (Z_ERR);
5001676Sjpk 		}
5011676Sjpk 		cp = zent->zroot + strlen(zent->zroot) - 5;
5021676Sjpk 		if (cp > zent->zroot && strcmp(cp, "/root") == 0)
5031676Sjpk 			*cp = 0;
5041676Sjpk 	} else {
5051676Sjpk 		if ((err = zone_get_zonepath(zent->zname, root,
5061676Sjpk 		    sizeof (root))) != Z_OK) {
5071676Sjpk 			errno = err;
5081676Sjpk 			zperror2(zent->zname,
5091676Sjpk 			    gettext("could not get zone path."));
5101676Sjpk 			return (Z_ERR);
5111676Sjpk 		}
5121676Sjpk 		(void) strlcpy(zent->zroot, root, sizeof (zent->zroot));
5131676Sjpk 	}
5140Sstevel@tonic-gate 
5150Sstevel@tonic-gate 	if ((err = zone_get_state(zent->zname, &zent->zstate_num)) != Z_OK) {
5160Sstevel@tonic-gate 		errno = err;
5170Sstevel@tonic-gate 		zperror2(zent->zname, gettext("could not get state"));
5180Sstevel@tonic-gate 		return (Z_ERR);
5190Sstevel@tonic-gate 	}
5200Sstevel@tonic-gate 	zent->zstate_str = zone_state_str(zent->zstate_num);
5213009Snn35248 
5223009Snn35248 	/*
5233009Snn35248 	 * A zone's brand is only available in the .xml file describing it,
5243009Snn35248 	 * which is only visible to the global zone.  This causes
5253009Snn35248 	 * zone_get_brand() to fail when called from within a non-global
5263009Snn35248 	 * zone.  Fortunately we only do this on labeled systems, where we
5273009Snn35248 	 * know all zones are native.
5283009Snn35248 	 */
5293009Snn35248 	if (getzoneid() != GLOBAL_ZONEID) {
5303009Snn35248 		assert(is_system_labeled() != 0);
53110943SEdward.Pilatowicz@Sun.COM 		(void) strlcpy(zent->zbrand, default_brand,
5323009Snn35248 		    sizeof (zent->zbrand));
5333009Snn35248 	} else if (zone_get_brand(zent->zname, zent->zbrand,
5342712Snn35248 	    sizeof (zent->zbrand)) != Z_OK) {
5352712Snn35248 		zperror2(zent->zname, gettext("could not get brand name"));
5362712Snn35248 		return (Z_ERR);
5372712Snn35248 	}
5380Sstevel@tonic-gate 
5393448Sdh155122 	/*
5403448Sdh155122 	 * Get ip type of the zone.
5413448Sdh155122 	 * Note for global zone, ZS_SHARED is set always.
5423448Sdh155122 	 */
5433448Sdh155122 	if (zid == GLOBAL_ZONEID) {
5443448Sdh155122 		zent->ziptype = ZS_SHARED;
5458942Sgerald.jelinek@sun.com 		return (Z_OK);
5468942Sgerald.jelinek@sun.com 	}
5478942Sgerald.jelinek@sun.com 
5488942Sgerald.jelinek@sun.com 	/*
5498942Sgerald.jelinek@sun.com 	 * There is a race condition where the zone could boot while
5508942Sgerald.jelinek@sun.com 	 * we're walking the index file.  In this case the zone state
5518942Sgerald.jelinek@sun.com 	 * could be seen as running from the call above, but the zoneid
5528942Sgerald.jelinek@sun.com 	 * would be undefined.
5538942Sgerald.jelinek@sun.com 	 *
5548942Sgerald.jelinek@sun.com 	 * There is also a race condition where the zone could shutdown after
5558942Sgerald.jelinek@sun.com 	 * we got its running state above.  This is also not an error and
5568942Sgerald.jelinek@sun.com 	 * we fall back to getting the ziptype from the zone configuration.
5578942Sgerald.jelinek@sun.com 	 */
5588942Sgerald.jelinek@sun.com 	if (zent->zstate_num == ZONE_STATE_RUNNING &&
5598942Sgerald.jelinek@sun.com 	    zid != ZONE_ID_UNDEFINED) {
5608942Sgerald.jelinek@sun.com 		ushort_t flags;
5618942Sgerald.jelinek@sun.com 
5628942Sgerald.jelinek@sun.com 		if (zone_getattr(zid, ZONE_ATTR_FLAGS, &flags,
5638942Sgerald.jelinek@sun.com 		    sizeof (flags)) >= 0) {
5643448Sdh155122 			if (flags & ZF_NET_EXCL)
5653448Sdh155122 				zent->ziptype = ZS_EXCLUSIVE;
5663448Sdh155122 			else
5673448Sdh155122 				zent->ziptype = ZS_SHARED;
5688942Sgerald.jelinek@sun.com 			return (Z_OK);
5693448Sdh155122 		}
5703448Sdh155122 	}
5713448Sdh155122 
5728942Sgerald.jelinek@sun.com 	if ((handle = zonecfg_init_handle()) == NULL) {
5738942Sgerald.jelinek@sun.com 		zperror2(zent->zname, gettext("could not init handle"));
5748942Sgerald.jelinek@sun.com 		return (Z_ERR);
5758942Sgerald.jelinek@sun.com 	}
5768942Sgerald.jelinek@sun.com 	if ((err = zonecfg_get_handle(zent->zname, handle)) != Z_OK) {
5778942Sgerald.jelinek@sun.com 		zperror2(zent->zname, gettext("could not get handle"));
5788942Sgerald.jelinek@sun.com 		zonecfg_fini_handle(handle);
5798942Sgerald.jelinek@sun.com 		return (Z_ERR);
5808942Sgerald.jelinek@sun.com 	}
5818942Sgerald.jelinek@sun.com 
5828942Sgerald.jelinek@sun.com 	if ((err = zonecfg_get_iptype(handle, &zent->ziptype)) != Z_OK) {
5838942Sgerald.jelinek@sun.com 		zperror2(zent->zname, gettext("could not get ip-type"));
5848942Sgerald.jelinek@sun.com 		zonecfg_fini_handle(handle);
5858942Sgerald.jelinek@sun.com 		return (Z_ERR);
5868942Sgerald.jelinek@sun.com 	}
5878942Sgerald.jelinek@sun.com 	zonecfg_fini_handle(handle);
5888942Sgerald.jelinek@sun.com 
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.
5988942Sgerald.jelinek@sun.com  *
5998942Sgerald.jelinek@sun.com  * Note that the data about running zones can change while this function
6008942Sgerald.jelinek@sun.com  * is running, so its possible that the list of zones will have empty slots
6018942Sgerald.jelinek@sun.com  * at the end.
6020Sstevel@tonic-gate  */
6030Sstevel@tonic-gate 
6040Sstevel@tonic-gate static int
fetch_zents(void)605766Scarlsonj fetch_zents(void)
6060Sstevel@tonic-gate {
6070Sstevel@tonic-gate 	zoneid_t *zids = NULL;
6080Sstevel@tonic-gate 	uint_t nzents_saved;
609766Scarlsonj 	int i, retv;
610766Scarlsonj 	FILE *fp;
611766Scarlsonj 	boolean_t inaltroot;
612766Scarlsonj 	zone_entry_t *zentp;
613*12841SSusan.Kamm-Worrell@Sun.COM 	const char *altroot;
6140Sstevel@tonic-gate 
6150Sstevel@tonic-gate 	if (nzents > 0)
6160Sstevel@tonic-gate 		return (Z_OK);
6170Sstevel@tonic-gate 
6180Sstevel@tonic-gate 	if (zone_list(NULL, &nzents) != 0) {
6190Sstevel@tonic-gate 		zperror(gettext("failed to get zoneid list"), B_FALSE);
6200Sstevel@tonic-gate 		return (Z_ERR);
6210Sstevel@tonic-gate 	}
6220Sstevel@tonic-gate 
6230Sstevel@tonic-gate again:
6240Sstevel@tonic-gate 	if (nzents == 0)
6250Sstevel@tonic-gate 		return (Z_OK);
6260Sstevel@tonic-gate 
6270Sstevel@tonic-gate 	zids = safe_calloc(nzents, sizeof (zoneid_t));
6280Sstevel@tonic-gate 	nzents_saved = nzents;
6290Sstevel@tonic-gate 
6300Sstevel@tonic-gate 	if (zone_list(zids, &nzents) != 0) {
6310Sstevel@tonic-gate 		zperror(gettext("failed to get zone list"), B_FALSE);
6320Sstevel@tonic-gate 		free(zids);
6330Sstevel@tonic-gate 		return (Z_ERR);
6340Sstevel@tonic-gate 	}
6350Sstevel@tonic-gate 	if (nzents != nzents_saved) {
6360Sstevel@tonic-gate 		/* list changed, try again */
6370Sstevel@tonic-gate 		free(zids);
6380Sstevel@tonic-gate 		goto again;
6390Sstevel@tonic-gate 	}
6400Sstevel@tonic-gate 
6410Sstevel@tonic-gate 	zents = safe_calloc(nzents, sizeof (zone_entry_t));
6420Sstevel@tonic-gate 
643766Scarlsonj 	inaltroot = zonecfg_in_alt_root();
644*12841SSusan.Kamm-Worrell@Sun.COM 	if (inaltroot) {
645766Scarlsonj 		fp = zonecfg_open_scratch("", B_FALSE);
646*12841SSusan.Kamm-Worrell@Sun.COM 		altroot = zonecfg_get_root();
647*12841SSusan.Kamm-Worrell@Sun.COM 	} else {
648766Scarlsonj 		fp = NULL;
649*12841SSusan.Kamm-Worrell@Sun.COM 	}
650766Scarlsonj 	zentp = zents;
651766Scarlsonj 	retv = Z_OK;
6520Sstevel@tonic-gate 	for (i = 0; i < nzents; i++) {
6530Sstevel@tonic-gate 		char name[ZONENAME_MAX];
654766Scarlsonj 		char altname[ZONENAME_MAX];
655*12841SSusan.Kamm-Worrell@Sun.COM 		char rev_altroot[MAXPATHLEN];
6560Sstevel@tonic-gate 
657766Scarlsonj 		if (getzonenamebyid(zids[i], name, sizeof (name)) < 0) {
6588942Sgerald.jelinek@sun.com 			/*
6598942Sgerald.jelinek@sun.com 			 * There is a race condition where the zone may have
6608942Sgerald.jelinek@sun.com 			 * shutdown since we retrieved the number of running
6618942Sgerald.jelinek@sun.com 			 * zones above.  This is not an error, there will be
6628942Sgerald.jelinek@sun.com 			 * an empty slot at the end of the list.
6638942Sgerald.jelinek@sun.com 			 */
664766Scarlsonj 			continue;
665766Scarlsonj 		}
666766Scarlsonj 		if (zonecfg_is_scratch(name)) {
667766Scarlsonj 			/* Ignore scratch zones by default */
668766Scarlsonj 			if (!inaltroot)
669766Scarlsonj 				continue;
670766Scarlsonj 			if (fp == NULL ||
671766Scarlsonj 			    zonecfg_reverse_scratch(fp, name, altname,
672*12841SSusan.Kamm-Worrell@Sun.COM 			    sizeof (altname), rev_altroot,
673*12841SSusan.Kamm-Worrell@Sun.COM 			    sizeof (rev_altroot)) == -1) {
674924Sgjelinek 				zerror(gettext("could not resolve scratch "
675766Scarlsonj 				    "zone %s"), name);
676766Scarlsonj 				retv = Z_ERR;
677766Scarlsonj 				continue;
678766Scarlsonj 			}
679*12841SSusan.Kamm-Worrell@Sun.COM 			/* Ignore zones in other alternate roots */
680*12841SSusan.Kamm-Worrell@Sun.COM 			if (strcmp(rev_altroot, altroot) != 0)
681*12841SSusan.Kamm-Worrell@Sun.COM 				continue;
682766Scarlsonj 			(void) strcpy(name, altname);
683766Scarlsonj 		} else {
684766Scarlsonj 			/* Ignore non-scratch when in an alternate root */
685766Scarlsonj 			if (inaltroot && strcmp(name, GLOBAL_ZONENAME) != 0)
686766Scarlsonj 				continue;
687766Scarlsonj 		}
688766Scarlsonj 		if (lookup_zone_info(name, zids[i], zentp) != Z_OK) {
6898942Sgerald.jelinek@sun.com 			/*
6908942Sgerald.jelinek@sun.com 			 * There is a race condition where the zone may have
6918942Sgerald.jelinek@sun.com 			 * shutdown since we retrieved the number of running
6928942Sgerald.jelinek@sun.com 			 * zones above.  This is not an error, there will be
6938942Sgerald.jelinek@sun.com 			 * an empty slot at the end of the list.
6948942Sgerald.jelinek@sun.com 			 */
695766Scarlsonj 			continue;
696766Scarlsonj 		}
697766Scarlsonj 		zentp++;
6980Sstevel@tonic-gate 	}
699766Scarlsonj 	nzents = zentp - zents;
700766Scarlsonj 	if (fp != NULL)
701766Scarlsonj 		zonecfg_close_scratch(fp);
7020Sstevel@tonic-gate 
7030Sstevel@tonic-gate 	free(zids);
704766Scarlsonj 	return (retv);
7050Sstevel@tonic-gate }
7060Sstevel@tonic-gate 
707766Scarlsonj static int
zone_print_list(zone_state_t min_state,boolean_t verbose,boolean_t parsable)7080Sstevel@tonic-gate zone_print_list(zone_state_t min_state, boolean_t verbose, boolean_t parsable)
7090Sstevel@tonic-gate {
7100Sstevel@tonic-gate 	int i;
7110Sstevel@tonic-gate 	zone_entry_t zent;
7120Sstevel@tonic-gate 	FILE *cookie;
7130Sstevel@tonic-gate 	char *name;
7140Sstevel@tonic-gate 
7150Sstevel@tonic-gate 	/*
7160Sstevel@tonic-gate 	 * First get the list of running zones from the kernel and print them.
7170Sstevel@tonic-gate 	 * If that is all we need, then return.
7180Sstevel@tonic-gate 	 */
719766Scarlsonj 	if ((i = fetch_zents()) != Z_OK) {
7200Sstevel@tonic-gate 		/*
7210Sstevel@tonic-gate 		 * No need for error messages; fetch_zents() has already taken
7220Sstevel@tonic-gate 		 * care of this.
7230Sstevel@tonic-gate 		 */
724766Scarlsonj 		return (i);
7250Sstevel@tonic-gate 	}
726766Scarlsonj 	for (i = 0; i < nzents; i++)
7270Sstevel@tonic-gate 		zone_print(&zents[i], verbose, parsable);
7280Sstevel@tonic-gate 	if (min_state >= ZONE_STATE_RUNNING)
729766Scarlsonj 		return (Z_OK);
7300Sstevel@tonic-gate 	/*
7310Sstevel@tonic-gate 	 * Next, get the full list of zones from the configuration, skipping
7320Sstevel@tonic-gate 	 * any we have already printed.
7330Sstevel@tonic-gate 	 */
7340Sstevel@tonic-gate 	cookie = setzoneent();
7350Sstevel@tonic-gate 	while ((name = getzoneent(cookie)) != NULL) {
7360Sstevel@tonic-gate 		for (i = 0; i < nzents; i++) {
7370Sstevel@tonic-gate 			if (strcmp(zents[i].zname, name) == 0)
7380Sstevel@tonic-gate 				break;
7390Sstevel@tonic-gate 		}
7400Sstevel@tonic-gate 		if (i < nzents) {
7410Sstevel@tonic-gate 			free(name);
7420Sstevel@tonic-gate 			continue;
7430Sstevel@tonic-gate 		}
744766Scarlsonj 		if (lookup_zone_info(name, ZONE_ID_UNDEFINED, &zent) != Z_OK) {
7450Sstevel@tonic-gate 			free(name);
7460Sstevel@tonic-gate 			continue;
7470Sstevel@tonic-gate 		}
7480Sstevel@tonic-gate 		free(name);
7490Sstevel@tonic-gate 		if (zent.zstate_num >= min_state)
7500Sstevel@tonic-gate 			zone_print(&zent, verbose, parsable);
7510Sstevel@tonic-gate 	}
7520Sstevel@tonic-gate 	endzoneent(cookie);
753766Scarlsonj 	return (Z_OK);
7540Sstevel@tonic-gate }
7550Sstevel@tonic-gate 
7568083SJordan.Vaughan@Sun.com /*
7578083SJordan.Vaughan@Sun.com  * Retrieve a zone entry by name.  Returns NULL if no such zone exists.
7588083SJordan.Vaughan@Sun.com  */
7590Sstevel@tonic-gate static zone_entry_t *
lookup_running_zone(const char * str)7608083SJordan.Vaughan@Sun.com lookup_running_zone(const char *str)
7610Sstevel@tonic-gate {
7620Sstevel@tonic-gate 	int i;
7630Sstevel@tonic-gate 
7640Sstevel@tonic-gate 	if (fetch_zents() != Z_OK)
7650Sstevel@tonic-gate 		return (NULL);
7660Sstevel@tonic-gate 
7670Sstevel@tonic-gate 	for (i = 0; i < nzents; i++) {
7680Sstevel@tonic-gate 		if (strcmp(str, zents[i].zname) == 0)
7690Sstevel@tonic-gate 			return (&zents[i]);
7700Sstevel@tonic-gate 	}
7710Sstevel@tonic-gate 	return (NULL);
7720Sstevel@tonic-gate }
7730Sstevel@tonic-gate 
7740Sstevel@tonic-gate /*
7750Sstevel@tonic-gate  * Check a bit in a mode_t: if on is B_TRUE, that bit should be on; if
7760Sstevel@tonic-gate  * B_FALSE, it should be off.  Return B_TRUE if the mode is bad (incorrect).
7770Sstevel@tonic-gate  */
7780Sstevel@tonic-gate static boolean_t
bad_mode_bit(mode_t mode,mode_t bit,boolean_t on,char * file)7790Sstevel@tonic-gate bad_mode_bit(mode_t mode, mode_t bit, boolean_t on, char *file)
7800Sstevel@tonic-gate {
7810Sstevel@tonic-gate 	char *str;
7820Sstevel@tonic-gate 
7830Sstevel@tonic-gate 	assert(bit == S_IRUSR || bit == S_IWUSR || bit == S_IXUSR ||
7840Sstevel@tonic-gate 	    bit == S_IRGRP || bit == S_IWGRP || bit == S_IXGRP ||
7850Sstevel@tonic-gate 	    bit == S_IROTH || bit == S_IWOTH || bit == S_IXOTH);
7860Sstevel@tonic-gate 	/*
7870Sstevel@tonic-gate 	 * TRANSLATION_NOTE
7880Sstevel@tonic-gate 	 * The strings below will be used as part of a larger message,
7890Sstevel@tonic-gate 	 * either:
7900Sstevel@tonic-gate 	 * (file name) must be (owner|group|world) (read|writ|execut)able
7910Sstevel@tonic-gate 	 * or
7920Sstevel@tonic-gate 	 * (file name) must not be (owner|group|world) (read|writ|execut)able
7930Sstevel@tonic-gate 	 */
7940Sstevel@tonic-gate 	switch (bit) {
7950Sstevel@tonic-gate 	case S_IRUSR:
7960Sstevel@tonic-gate 		str = gettext("owner readable");
7970Sstevel@tonic-gate 		break;
7980Sstevel@tonic-gate 	case S_IWUSR:
7990Sstevel@tonic-gate 		str = gettext("owner writable");
8000Sstevel@tonic-gate 		break;
8010Sstevel@tonic-gate 	case S_IXUSR:
8020Sstevel@tonic-gate 		str = gettext("owner executable");
8030Sstevel@tonic-gate 		break;
8040Sstevel@tonic-gate 	case S_IRGRP:
8050Sstevel@tonic-gate 		str = gettext("group readable");
8060Sstevel@tonic-gate 		break;
8070Sstevel@tonic-gate 	case S_IWGRP:
8080Sstevel@tonic-gate 		str = gettext("group writable");
8090Sstevel@tonic-gate 		break;
8100Sstevel@tonic-gate 	case S_IXGRP:
8110Sstevel@tonic-gate 		str = gettext("group executable");
8120Sstevel@tonic-gate 		break;
8130Sstevel@tonic-gate 	case S_IROTH:
8140Sstevel@tonic-gate 		str = gettext("world readable");
8150Sstevel@tonic-gate 		break;
8160Sstevel@tonic-gate 	case S_IWOTH:
8170Sstevel@tonic-gate 		str = gettext("world writable");
8180Sstevel@tonic-gate 		break;
8190Sstevel@tonic-gate 	case S_IXOTH:
8200Sstevel@tonic-gate 		str = gettext("world executable");
8210Sstevel@tonic-gate 		break;
8220Sstevel@tonic-gate 	}
8230Sstevel@tonic-gate 	if ((mode & bit) == (on ? 0 : bit)) {
8240Sstevel@tonic-gate 		/*
8250Sstevel@tonic-gate 		 * TRANSLATION_NOTE
8260Sstevel@tonic-gate 		 * The first parameter below is a file name; the second
8270Sstevel@tonic-gate 		 * is one of the "(owner|group|world) (read|writ|execut)able"
8280Sstevel@tonic-gate 		 * strings from above.
8290Sstevel@tonic-gate 		 */
8300Sstevel@tonic-gate 		/*
8310Sstevel@tonic-gate 		 * The code below could be simplified but not in a way
8320Sstevel@tonic-gate 		 * that would easily translate to non-English locales.
8330Sstevel@tonic-gate 		 */
8340Sstevel@tonic-gate 		if (on) {
8350Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("%s must be %s.\n"),
8360Sstevel@tonic-gate 			    file, str);
8370Sstevel@tonic-gate 		} else {
8380Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("%s must not be %s.\n"),
8390Sstevel@tonic-gate 			    file, str);
8400Sstevel@tonic-gate 		}
8410Sstevel@tonic-gate 		return (B_TRUE);
8420Sstevel@tonic-gate 	}
8430Sstevel@tonic-gate 	return (B_FALSE);
8440Sstevel@tonic-gate }
8450Sstevel@tonic-gate 
8460Sstevel@tonic-gate /*
8470Sstevel@tonic-gate  * We want to make sure that no zone has its zone path as a child node
8480Sstevel@tonic-gate  * (in the directory sense) of any other.  We do that by comparing this
8490Sstevel@tonic-gate  * zone's path to the path of all other (non-global) zones.  The comparison
8500Sstevel@tonic-gate  * in each case is simple: add '/' to the end of the path, then do a
8510Sstevel@tonic-gate  * strncmp() of the two paths, using the length of the shorter one.
8520Sstevel@tonic-gate  */
8530Sstevel@tonic-gate 
8540Sstevel@tonic-gate static int
crosscheck_zonepaths(char * path)8550Sstevel@tonic-gate crosscheck_zonepaths(char *path)
8560Sstevel@tonic-gate {
8570Sstevel@tonic-gate 	char rpath[MAXPATHLEN];		/* resolved path */
8580Sstevel@tonic-gate 	char path_copy[MAXPATHLEN];	/* copy of original path */
8590Sstevel@tonic-gate 	char rpath_copy[MAXPATHLEN];	/* copy of original rpath */
8600Sstevel@tonic-gate 	struct zoneent *ze;
8610Sstevel@tonic-gate 	int res, err;
8620Sstevel@tonic-gate 	FILE *cookie;
8630Sstevel@tonic-gate 
8640Sstevel@tonic-gate 	cookie = setzoneent();
8650Sstevel@tonic-gate 	while ((ze = getzoneent_private(cookie)) != NULL) {
8660Sstevel@tonic-gate 		/* Skip zones which are not installed. */
8670Sstevel@tonic-gate 		if (ze->zone_state < ZONE_STATE_INSTALLED) {
8680Sstevel@tonic-gate 			free(ze);
8690Sstevel@tonic-gate 			continue;
8700Sstevel@tonic-gate 		}
8710Sstevel@tonic-gate 		/* Skip the global zone and the current target zone. */
8720Sstevel@tonic-gate 		if (strcmp(ze->zone_name, GLOBAL_ZONENAME) == 0 ||
8730Sstevel@tonic-gate 		    strcmp(ze->zone_name, target_zone) == 0) {
8740Sstevel@tonic-gate 			free(ze);
8750Sstevel@tonic-gate 			continue;
8760Sstevel@tonic-gate 		}
8770Sstevel@tonic-gate 		if (strlen(ze->zone_path) == 0) {
8780Sstevel@tonic-gate 			/* old index file without path, fall back */
8790Sstevel@tonic-gate 			if ((err = zone_get_zonepath(ze->zone_name,
8800Sstevel@tonic-gate 			    ze->zone_path, sizeof (ze->zone_path))) != Z_OK) {
8810Sstevel@tonic-gate 				errno = err;
8820Sstevel@tonic-gate 				zperror2(ze->zone_name,
8830Sstevel@tonic-gate 				    gettext("could not get zone path"));
8840Sstevel@tonic-gate 				free(ze);
8850Sstevel@tonic-gate 				continue;
8860Sstevel@tonic-gate 			}
8870Sstevel@tonic-gate 		}
888766Scarlsonj 		(void) snprintf(path_copy, sizeof (path_copy), "%s%s",
889766Scarlsonj 		    zonecfg_get_root(), ze->zone_path);
890766Scarlsonj 		res = resolvepath(path_copy, rpath, sizeof (rpath));
8910Sstevel@tonic-gate 		if (res == -1) {
8920Sstevel@tonic-gate 			if (errno != ENOENT) {
893766Scarlsonj 				zperror(path_copy, B_FALSE);
8940Sstevel@tonic-gate 				free(ze);
8950Sstevel@tonic-gate 				return (Z_ERR);
8960Sstevel@tonic-gate 			}
8970Sstevel@tonic-gate 			(void) printf(gettext("WARNING: zone %s is installed, "
8980Sstevel@tonic-gate 			    "but its %s %s does not exist.\n"), ze->zone_name,
899766Scarlsonj 			    "zonepath", path_copy);
9000Sstevel@tonic-gate 			free(ze);
9010Sstevel@tonic-gate 			continue;
9020Sstevel@tonic-gate 		}
9030Sstevel@tonic-gate 		rpath[res] = '\0';
9040Sstevel@tonic-gate 		(void) snprintf(path_copy, sizeof (path_copy), "%s/", path);
9050Sstevel@tonic-gate 		(void) snprintf(rpath_copy, sizeof (rpath_copy), "%s/", rpath);
9060Sstevel@tonic-gate 		if (strncmp(path_copy, rpath_copy,
9070Sstevel@tonic-gate 		    min(strlen(path_copy), strlen(rpath_copy))) == 0) {
908924Sgjelinek 			/*
909924Sgjelinek 			 * TRANSLATION_NOTE
910924Sgjelinek 			 * zonepath is a literal that should not be translated.
911924Sgjelinek 			 */
9120Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("%s zonepath (%s) and "
9130Sstevel@tonic-gate 			    "%s zonepath (%s) overlap.\n"),
9140Sstevel@tonic-gate 			    target_zone, path, ze->zone_name, rpath);
9150Sstevel@tonic-gate 			free(ze);
9160Sstevel@tonic-gate 			return (Z_ERR);
9170Sstevel@tonic-gate 		}
9180Sstevel@tonic-gate 		free(ze);
9190Sstevel@tonic-gate 	}
9200Sstevel@tonic-gate 	endzoneent(cookie);
9210Sstevel@tonic-gate 	return (Z_OK);
9220Sstevel@tonic-gate }
9230Sstevel@tonic-gate 
9240Sstevel@tonic-gate static int
validate_zonepath(char * path,int cmd_num)9250Sstevel@tonic-gate validate_zonepath(char *path, int cmd_num)
9260Sstevel@tonic-gate {
9270Sstevel@tonic-gate 	int res;			/* result of last library/system call */
9280Sstevel@tonic-gate 	boolean_t err = B_FALSE;	/* have we run into an error? */
9290Sstevel@tonic-gate 	struct stat stbuf;
9302267Sdp 	struct statvfs64 vfsbuf;
9310Sstevel@tonic-gate 	char rpath[MAXPATHLEN];		/* resolved path */
9320Sstevel@tonic-gate 	char ppath[MAXPATHLEN];		/* parent path */
9330Sstevel@tonic-gate 	char rppath[MAXPATHLEN];	/* resolved parent path */
9340Sstevel@tonic-gate 	char rootpath[MAXPATHLEN];	/* root path */
9350Sstevel@tonic-gate 	zone_state_t state;
9360Sstevel@tonic-gate 
9370Sstevel@tonic-gate 	if (path[0] != '/') {
9380Sstevel@tonic-gate 		(void) fprintf(stderr,
9390Sstevel@tonic-gate 		    gettext("%s is not an absolute path.\n"), path);
9400Sstevel@tonic-gate 		return (Z_ERR);
9410Sstevel@tonic-gate 	}
9420Sstevel@tonic-gate 	if ((res = resolvepath(path, rpath, sizeof (rpath))) == -1) {
9430Sstevel@tonic-gate 		if ((errno != ENOENT) ||
9441300Sgjelinek 		    (cmd_num != CMD_VERIFY && cmd_num != CMD_INSTALL &&
9451300Sgjelinek 		    cmd_num != CMD_CLONE && cmd_num != CMD_MOVE)) {
9460Sstevel@tonic-gate 			zperror(path, B_FALSE);
9470Sstevel@tonic-gate 			return (Z_ERR);
9480Sstevel@tonic-gate 		}
9490Sstevel@tonic-gate 		if (cmd_num == CMD_VERIFY) {
950924Sgjelinek 			/*
951924Sgjelinek 			 * TRANSLATION_NOTE
952924Sgjelinek 			 * zoneadm is a literal that should not be translated.
953924Sgjelinek 			 */
9540Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("WARNING: %s does not "
955924Sgjelinek 			    "exist, so it could not be verified.\nWhen "
956924Sgjelinek 			    "'zoneadm %s' is run, '%s' will try to create\n%s, "
957924Sgjelinek 			    "and '%s' will be tried again,\nbut the '%s' may "
958924Sgjelinek 			    "fail if:\nthe parent directory of %s is group- or "
959924Sgjelinek 			    "other-writable\nor\n%s overlaps with any other "
9600Sstevel@tonic-gate 			    "installed zones.\n"), path,
9610Sstevel@tonic-gate 			    cmd_to_str(CMD_INSTALL), cmd_to_str(CMD_INSTALL),
9620Sstevel@tonic-gate 			    path, cmd_to_str(CMD_VERIFY),
9630Sstevel@tonic-gate 			    cmd_to_str(CMD_VERIFY), path, path);
9640Sstevel@tonic-gate 			return (Z_OK);
9650Sstevel@tonic-gate 		}
9660Sstevel@tonic-gate 		/*
9670Sstevel@tonic-gate 		 * The zonepath is supposed to be mode 700 but its
9680Sstevel@tonic-gate 		 * parent(s) 755.  So use 755 on the mkdirp() then
9690Sstevel@tonic-gate 		 * chmod() the zonepath itself to 700.
9700Sstevel@tonic-gate 		 */
9710Sstevel@tonic-gate 		if (mkdirp(path, DEFAULT_DIR_MODE) < 0) {
9720Sstevel@tonic-gate 			zperror(path, B_FALSE);
9730Sstevel@tonic-gate 			return (Z_ERR);
9740Sstevel@tonic-gate 		}
9750Sstevel@tonic-gate 		/*
9760Sstevel@tonic-gate 		 * If the chmod() fails, report the error, but might
9770Sstevel@tonic-gate 		 * as well continue the verify procedure.
9780Sstevel@tonic-gate 		 */
9790Sstevel@tonic-gate 		if (chmod(path, S_IRWXU) != 0)
9800Sstevel@tonic-gate 			zperror(path, B_FALSE);
9810Sstevel@tonic-gate 		/*
9820Sstevel@tonic-gate 		 * Since the mkdir() succeeded, we should not have to
9830Sstevel@tonic-gate 		 * worry about a subsequent ENOENT, thus this should
9840Sstevel@tonic-gate 		 * only recurse once.
9850Sstevel@tonic-gate 		 */
9861300Sgjelinek 		return (validate_zonepath(path, cmd_num));
9870Sstevel@tonic-gate 	}
9880Sstevel@tonic-gate 	rpath[res] = '\0';
9890Sstevel@tonic-gate 	if (strcmp(path, rpath) != 0) {
9900Sstevel@tonic-gate 		errno = Z_RESOLVED_PATH;
9910Sstevel@tonic-gate 		zperror(path, B_TRUE);
9920Sstevel@tonic-gate 		return (Z_ERR);
9930Sstevel@tonic-gate 	}
9940Sstevel@tonic-gate 	if ((res = stat(rpath, &stbuf)) != 0) {
9950Sstevel@tonic-gate 		zperror(rpath, B_FALSE);
9960Sstevel@tonic-gate 		return (Z_ERR);
9970Sstevel@tonic-gate 	}
9980Sstevel@tonic-gate 	if (!S_ISDIR(stbuf.st_mode)) {
9990Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("%s is not a directory.\n"),
10000Sstevel@tonic-gate 		    rpath);
10010Sstevel@tonic-gate 		return (Z_ERR);
10020Sstevel@tonic-gate 	}
10033445Sblakej 	if (strcmp(stbuf.st_fstype, MNTTYPE_TMPFS) == 0) {
10040Sstevel@tonic-gate 		(void) printf(gettext("WARNING: %s is on a temporary "
10051867Sgjelinek 		    "file system.\n"), rpath);
10060Sstevel@tonic-gate 	}
10070Sstevel@tonic-gate 	if (crosscheck_zonepaths(rpath) != Z_OK)
10080Sstevel@tonic-gate 		return (Z_ERR);
10090Sstevel@tonic-gate 	/*
10100Sstevel@tonic-gate 	 * Try to collect and report as many minor errors as possible
10110Sstevel@tonic-gate 	 * before returning, so the user can learn everything that needs
10120Sstevel@tonic-gate 	 * to be fixed up front.
10130Sstevel@tonic-gate 	 */
10140Sstevel@tonic-gate 	if (stbuf.st_uid != 0) {
10150Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("%s is not owned by root.\n"),
10160Sstevel@tonic-gate 		    rpath);
10170Sstevel@tonic-gate 		err = B_TRUE;
10180Sstevel@tonic-gate 	}
10190Sstevel@tonic-gate 	err |= bad_mode_bit(stbuf.st_mode, S_IRUSR, B_TRUE, rpath);
10200Sstevel@tonic-gate 	err |= bad_mode_bit(stbuf.st_mode, S_IWUSR, B_TRUE, rpath);
10210Sstevel@tonic-gate 	err |= bad_mode_bit(stbuf.st_mode, S_IXUSR, B_TRUE, rpath);
10220Sstevel@tonic-gate 	err |= bad_mode_bit(stbuf.st_mode, S_IRGRP, B_FALSE, rpath);
10230Sstevel@tonic-gate 	err |= bad_mode_bit(stbuf.st_mode, S_IWGRP, B_FALSE, rpath);
10240Sstevel@tonic-gate 	err |= bad_mode_bit(stbuf.st_mode, S_IXGRP, B_FALSE, rpath);
10250Sstevel@tonic-gate 	err |= bad_mode_bit(stbuf.st_mode, S_IROTH, B_FALSE, rpath);
10260Sstevel@tonic-gate 	err |= bad_mode_bit(stbuf.st_mode, S_IWOTH, B_FALSE, rpath);
10270Sstevel@tonic-gate 	err |= bad_mode_bit(stbuf.st_mode, S_IXOTH, B_FALSE, rpath);
10280Sstevel@tonic-gate 
10290Sstevel@tonic-gate 	(void) snprintf(ppath, sizeof (ppath), "%s/..", path);
10300Sstevel@tonic-gate 	if ((res = resolvepath(ppath, rppath, sizeof (rppath))) == -1) {
10310Sstevel@tonic-gate 		zperror(ppath, B_FALSE);
10320Sstevel@tonic-gate 		return (Z_ERR);
10330Sstevel@tonic-gate 	}
10340Sstevel@tonic-gate 	rppath[res] = '\0';
10350Sstevel@tonic-gate 	if ((res = stat(rppath, &stbuf)) != 0) {
10360Sstevel@tonic-gate 		zperror(rppath, B_FALSE);
10370Sstevel@tonic-gate 		return (Z_ERR);
10380Sstevel@tonic-gate 	}
10390Sstevel@tonic-gate 	/* theoretically impossible */
10400Sstevel@tonic-gate 	if (!S_ISDIR(stbuf.st_mode)) {
10410Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("%s is not a directory.\n"),
10420Sstevel@tonic-gate 		    rppath);
10430Sstevel@tonic-gate 		return (Z_ERR);
10440Sstevel@tonic-gate 	}
10450Sstevel@tonic-gate 	if (stbuf.st_uid != 0) {
10460Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("%s is not owned by root.\n"),
10470Sstevel@tonic-gate 		    rppath);
10480Sstevel@tonic-gate 		err = B_TRUE;
10490Sstevel@tonic-gate 	}
10500Sstevel@tonic-gate 	err |= bad_mode_bit(stbuf.st_mode, S_IRUSR, B_TRUE, rppath);
10510Sstevel@tonic-gate 	err |= bad_mode_bit(stbuf.st_mode, S_IWUSR, B_TRUE, rppath);
10520Sstevel@tonic-gate 	err |= bad_mode_bit(stbuf.st_mode, S_IXUSR, B_TRUE, rppath);
10530Sstevel@tonic-gate 	err |= bad_mode_bit(stbuf.st_mode, S_IWGRP, B_FALSE, rppath);
10540Sstevel@tonic-gate 	err |= bad_mode_bit(stbuf.st_mode, S_IWOTH, B_FALSE, rppath);
10550Sstevel@tonic-gate 	if (strcmp(rpath, rppath) == 0) {
10560Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("%s is its own parent.\n"),
10570Sstevel@tonic-gate 		    rppath);
10580Sstevel@tonic-gate 		err = B_TRUE;
10590Sstevel@tonic-gate 	}
10600Sstevel@tonic-gate 
10612267Sdp 	if (statvfs64(rpath, &vfsbuf) != 0) {
10620Sstevel@tonic-gate 		zperror(rpath, B_FALSE);
10630Sstevel@tonic-gate 		return (Z_ERR);
10640Sstevel@tonic-gate 	}
1065823Sgjelinek 	if (strcmp(vfsbuf.f_basetype, MNTTYPE_NFS) == 0) {
1066924Sgjelinek 		/*
1067924Sgjelinek 		 * TRANSLATION_NOTE
1068924Sgjelinek 		 * Zonepath and NFS are literals that should not be translated.
1069924Sgjelinek 		 */
1070924Sgjelinek 		(void) fprintf(stderr, gettext("Zonepath %s is on an NFS "
10711867Sgjelinek 		    "mounted file system.\n"
10721867Sgjelinek 		    "\tA local file system must be used.\n"), rpath);
1073823Sgjelinek 		return (Z_ERR);
1074823Sgjelinek 	}
1075823Sgjelinek 	if (vfsbuf.f_flag & ST_NOSUID) {
1076924Sgjelinek 		/*
1077924Sgjelinek 		 * TRANSLATION_NOTE
1078924Sgjelinek 		 * Zonepath and nosuid are literals that should not be
1079924Sgjelinek 		 * translated.
1080924Sgjelinek 		 */
1081823Sgjelinek 		(void) fprintf(stderr, gettext("Zonepath %s is on a nosuid "
10821867Sgjelinek 		    "file system.\n"), rpath);
10830Sstevel@tonic-gate 		return (Z_ERR);
10840Sstevel@tonic-gate 	}
10850Sstevel@tonic-gate 
10860Sstevel@tonic-gate 	if ((res = zone_get_state(target_zone, &state)) != Z_OK) {
10870Sstevel@tonic-gate 		errno = res;
10880Sstevel@tonic-gate 		zperror2(target_zone, gettext("could not get state"));
10890Sstevel@tonic-gate 		return (Z_ERR);
10900Sstevel@tonic-gate 	}
10910Sstevel@tonic-gate 	/*
10920Sstevel@tonic-gate 	 * The existence of the root path is only bad in the configured state,
10930Sstevel@tonic-gate 	 * as it is *supposed* to be there at the installed and later states.
10941507Sgjelinek 	 * However, the root path is expected to be there if the zone is
10951507Sgjelinek 	 * detached.
10960Sstevel@tonic-gate 	 * State/command mismatches are caught earlier in verify_details().
10970Sstevel@tonic-gate 	 */
10981507Sgjelinek 	if (state == ZONE_STATE_CONFIGURED && cmd_num != CMD_ATTACH) {
10990Sstevel@tonic-gate 		if (snprintf(rootpath, sizeof (rootpath), "%s/root", rpath) >=
11000Sstevel@tonic-gate 		    sizeof (rootpath)) {
1101924Sgjelinek 			/*
1102924Sgjelinek 			 * TRANSLATION_NOTE
1103924Sgjelinek 			 * Zonepath is a literal that should not be translated.
1104924Sgjelinek 			 */
11050Sstevel@tonic-gate 			(void) fprintf(stderr,
11060Sstevel@tonic-gate 			    gettext("Zonepath %s is too long.\n"), rpath);
11070Sstevel@tonic-gate 			return (Z_ERR);
11080Sstevel@tonic-gate 		}
11090Sstevel@tonic-gate 		if ((res = stat(rootpath, &stbuf)) == 0) {
11107782Sgerald.jelinek@sun.com 			if (zonecfg_detached(rpath)) {
11111507Sgjelinek 				(void) fprintf(stderr,
11121507Sgjelinek 				    gettext("Cannot %s detached "
11131507Sgjelinek 				    "zone.\nUse attach or remove %s "
11141507Sgjelinek 				    "directory.\n"), cmd_to_str(cmd_num),
11151507Sgjelinek 				    rpath);
11167782Sgerald.jelinek@sun.com 				return (Z_ERR);
11177782Sgerald.jelinek@sun.com 			}
11187782Sgerald.jelinek@sun.com 
11197782Sgerald.jelinek@sun.com 			/* Not detached, check if it really looks ok. */
11207782Sgerald.jelinek@sun.com 
11217782Sgerald.jelinek@sun.com 			if (!S_ISDIR(stbuf.st_mode)) {
11227782Sgerald.jelinek@sun.com 				(void) fprintf(stderr, gettext("%s is not a "
11237782Sgerald.jelinek@sun.com 				    "directory.\n"), rootpath);
11247782Sgerald.jelinek@sun.com 				return (Z_ERR);
11257782Sgerald.jelinek@sun.com 			}
11267782Sgerald.jelinek@sun.com 
11277782Sgerald.jelinek@sun.com 			if (stbuf.st_uid != 0) {
11287782Sgerald.jelinek@sun.com 				(void) fprintf(stderr, gettext("%s is not "
11297782Sgerald.jelinek@sun.com 				    "owned by root.\n"), rootpath);
11307782Sgerald.jelinek@sun.com 				return (Z_ERR);
11317782Sgerald.jelinek@sun.com 			}
11327782Sgerald.jelinek@sun.com 
11337782Sgerald.jelinek@sun.com 			if ((stbuf.st_mode & 0777) != 0755) {
11347782Sgerald.jelinek@sun.com 				(void) fprintf(stderr, gettext("%s mode is not "
11357782Sgerald.jelinek@sun.com 				    "0755.\n"), rootpath);
11367782Sgerald.jelinek@sun.com 				return (Z_ERR);
11377782Sgerald.jelinek@sun.com 			}
11380Sstevel@tonic-gate 		}
11390Sstevel@tonic-gate 	}
11400Sstevel@tonic-gate 
11410Sstevel@tonic-gate 	return (err ? Z_ERR : Z_OK);
11420Sstevel@tonic-gate }
11430Sstevel@tonic-gate 
11440Sstevel@tonic-gate static int
invoke_brand_handler(int cmd_num,char * argv[])11453339Szt129084 invoke_brand_handler(int cmd_num, char *argv[])
11463339Szt129084 {
11473339Szt129084 	zone_dochandle_t handle;
11483339Szt129084 	int err;
11493339Szt129084 
11503339Szt129084 	if ((handle = zonecfg_init_handle()) == NULL) {
11513339Szt129084 		zperror(cmd_to_str(cmd_num), B_TRUE);
11523339Szt129084 		return (Z_ERR);
11533339Szt129084 	}
11543339Szt129084 	if ((err = zonecfg_get_handle(target_zone, handle)) != Z_OK) {
11553339Szt129084 		errno = err;
11563339Szt129084 		zperror(cmd_to_str(cmd_num), B_TRUE);
11573339Szt129084 		zonecfg_fini_handle(handle);
11583339Szt129084 		return (Z_ERR);
11593339Szt129084 	}
11603339Szt129084 	if (verify_brand(handle, cmd_num, argv) != Z_OK) {
11613339Szt129084 		zonecfg_fini_handle(handle);
11623339Szt129084 		return (Z_ERR);
11633339Szt129084 	}
11643339Szt129084 	zonecfg_fini_handle(handle);
11653339Szt129084 	return (Z_OK);
11663339Szt129084 }
11673339Szt129084 
11683339Szt129084 static int
ready_func(int argc,char * argv[])11690Sstevel@tonic-gate ready_func(int argc, char *argv[])
11700Sstevel@tonic-gate {
11710Sstevel@tonic-gate 	zone_cmd_arg_t zarg;
11720Sstevel@tonic-gate 	int arg;
11730Sstevel@tonic-gate 
1174766Scarlsonj 	if (zonecfg_in_alt_root()) {
1175766Scarlsonj 		zerror(gettext("cannot ready zone in alternate root"));
1176766Scarlsonj 		return (Z_ERR);
1177766Scarlsonj 	}
1178766Scarlsonj 
11790Sstevel@tonic-gate 	optind = 0;
11800Sstevel@tonic-gate 	if ((arg = getopt(argc, argv, "?")) != EOF) {
11810Sstevel@tonic-gate 		switch (arg) {
11820Sstevel@tonic-gate 		case '?':
11830Sstevel@tonic-gate 			sub_usage(SHELP_READY, CMD_READY);
11840Sstevel@tonic-gate 			return (optopt == '?' ? Z_OK : Z_USAGE);
11850Sstevel@tonic-gate 		default:
11860Sstevel@tonic-gate 			sub_usage(SHELP_READY, CMD_READY);
11870Sstevel@tonic-gate 			return (Z_USAGE);
11880Sstevel@tonic-gate 		}
11890Sstevel@tonic-gate 	}
11900Sstevel@tonic-gate 	if (argc > optind) {
11910Sstevel@tonic-gate 		sub_usage(SHELP_READY, CMD_READY);
11920Sstevel@tonic-gate 		return (Z_USAGE);
11930Sstevel@tonic-gate 	}
11942712Snn35248 	if (sanity_check(target_zone, CMD_READY, B_FALSE, B_FALSE, B_FALSE)
11952712Snn35248 	    != Z_OK)
11960Sstevel@tonic-gate 		return (Z_ERR);
11973339Szt129084 	if (verify_details(CMD_READY, argv) != Z_OK)
11980Sstevel@tonic-gate 		return (Z_ERR);
11990Sstevel@tonic-gate 
12000Sstevel@tonic-gate 	zarg.cmd = Z_READY;
12017089Sgjelinek 	if (zonecfg_call_zoneadmd(target_zone, &zarg, locale, B_TRUE) != 0) {
12020Sstevel@tonic-gate 		zerror(gettext("call to %s failed"), "zoneadmd");
12030Sstevel@tonic-gate 		return (Z_ERR);
12040Sstevel@tonic-gate 	}
12050Sstevel@tonic-gate 	return (Z_OK);
12060Sstevel@tonic-gate }
12070Sstevel@tonic-gate 
12080Sstevel@tonic-gate static int
boot_func(int argc,char * argv[])12090Sstevel@tonic-gate boot_func(int argc, char *argv[])
12100Sstevel@tonic-gate {
12110Sstevel@tonic-gate 	zone_cmd_arg_t zarg;
12122712Snn35248 	boolean_t force = B_FALSE;
12130Sstevel@tonic-gate 	int arg;
12140Sstevel@tonic-gate 
1215766Scarlsonj 	if (zonecfg_in_alt_root()) {
1216766Scarlsonj 		zerror(gettext("cannot boot zone in alternate root"));
1217766Scarlsonj 		return (Z_ERR);
1218766Scarlsonj 	}
1219766Scarlsonj 
12200Sstevel@tonic-gate 	zarg.bootbuf[0] = '\0';
12210Sstevel@tonic-gate 
12220Sstevel@tonic-gate 	/*
12232267Sdp 	 * The following getopt processes arguments to zone boot; that
12242267Sdp 	 * is to say, the [here] portion of the argument string:
12252267Sdp 	 *
12262267Sdp 	 *	zoneadm -z myzone boot [here] -- -v -m verbose
12272267Sdp 	 *
12282267Sdp 	 * Where [here] can either be nothing, -? (in which case we bail
12292712Snn35248 	 * and print usage), -f (a private option to indicate that the
12302712Snn35248 	 * boot operation should be 'forced'), or -s.  Support for -s is
12312712Snn35248 	 * vestigal and obsolete, but is retained because it was a
12322712Snn35248 	 * documented interface and there are known consumers including
12332712Snn35248 	 * admin/install; the proper way to specify boot arguments like -s
12342712Snn35248 	 * is:
12352267Sdp 	 *
12362267Sdp 	 *	zoneadm -z myzone boot -- -s -v -m verbose.
12370Sstevel@tonic-gate 	 */
12380Sstevel@tonic-gate 	optind = 0;
12392712Snn35248 	while ((arg = getopt(argc, argv, "?fs")) != EOF) {
12400Sstevel@tonic-gate 		switch (arg) {
12410Sstevel@tonic-gate 		case '?':
12420Sstevel@tonic-gate 			sub_usage(SHELP_BOOT, CMD_BOOT);
12430Sstevel@tonic-gate 			return (optopt == '?' ? Z_OK : Z_USAGE);
12440Sstevel@tonic-gate 		case 's':
12450Sstevel@tonic-gate 			(void) strlcpy(zarg.bootbuf, "-s",
12460Sstevel@tonic-gate 			    sizeof (zarg.bootbuf));
12470Sstevel@tonic-gate 			break;
12482712Snn35248 		case 'f':
12492712Snn35248 			force = B_TRUE;
12502712Snn35248 			break;
12510Sstevel@tonic-gate 		default:
12520Sstevel@tonic-gate 			sub_usage(SHELP_BOOT, CMD_BOOT);
12530Sstevel@tonic-gate 			return (Z_USAGE);
12540Sstevel@tonic-gate 		}
12550Sstevel@tonic-gate 	}
12562267Sdp 
12572267Sdp 	for (; optind < argc; optind++) {
12582267Sdp 		if (strlcat(zarg.bootbuf, argv[optind],
12592267Sdp 		    sizeof (zarg.bootbuf)) >= sizeof (zarg.bootbuf)) {
12602267Sdp 			zerror(gettext("Boot argument list too long"));
12612267Sdp 			return (Z_ERR);
12622267Sdp 		}
12632267Sdp 		if (optind < argc - 1)
12642267Sdp 			if (strlcat(zarg.bootbuf, " ", sizeof (zarg.bootbuf)) >=
12652267Sdp 			    sizeof (zarg.bootbuf)) {
12662267Sdp 				zerror(gettext("Boot argument list too long"));
12672267Sdp 				return (Z_ERR);
12682267Sdp 			}
12692267Sdp 	}
12702712Snn35248 	if (sanity_check(target_zone, CMD_BOOT, B_FALSE, B_FALSE, force)
12712712Snn35248 	    != Z_OK)
12720Sstevel@tonic-gate 		return (Z_ERR);
12733339Szt129084 	if (verify_details(CMD_BOOT, argv) != Z_OK)
12740Sstevel@tonic-gate 		return (Z_ERR);
12752712Snn35248 	zarg.cmd = force ? Z_FORCEBOOT : Z_BOOT;
12767089Sgjelinek 	if (zonecfg_call_zoneadmd(target_zone, &zarg, locale, B_TRUE) != 0) {
12770Sstevel@tonic-gate 		zerror(gettext("call to %s failed"), "zoneadmd");
12780Sstevel@tonic-gate 		return (Z_ERR);
12790Sstevel@tonic-gate 	}
12803247Sgjelinek 
12810Sstevel@tonic-gate 	return (Z_OK);
12820Sstevel@tonic-gate }
12830Sstevel@tonic-gate 
12840Sstevel@tonic-gate static void
fake_up_local_zone(zoneid_t zid,zone_entry_t * zeptr)12850Sstevel@tonic-gate fake_up_local_zone(zoneid_t zid, zone_entry_t *zeptr)
12860Sstevel@tonic-gate {
12870Sstevel@tonic-gate 	ssize_t result;
12882303Scarlsonj 	uuid_t uuid;
12892303Scarlsonj 	FILE *fp;
12903448Sdh155122 	ushort_t flags;
12912303Scarlsonj 
12922303Scarlsonj 	(void) memset(zeptr, 0, sizeof (*zeptr));
12930Sstevel@tonic-gate 
12940Sstevel@tonic-gate 	zeptr->zid = zid;
12952303Scarlsonj 
12960Sstevel@tonic-gate 	/*
12970Sstevel@tonic-gate 	 * Since we're looking up our own (non-global) zone name,
12980Sstevel@tonic-gate 	 * we can be assured that it will succeed.
12990Sstevel@tonic-gate 	 */
13000Sstevel@tonic-gate 	result = getzonenamebyid(zid, zeptr->zname, sizeof (zeptr->zname));
13010Sstevel@tonic-gate 	assert(result >= 0);
13022303Scarlsonj 	if (zonecfg_is_scratch(zeptr->zname) &&
13032303Scarlsonj 	    (fp = zonecfg_open_scratch("", B_FALSE)) != NULL) {
13042303Scarlsonj 		(void) zonecfg_reverse_scratch(fp, zeptr->zname, zeptr->zname,
13052303Scarlsonj 		    sizeof (zeptr->zname), NULL, 0);
13062303Scarlsonj 		zonecfg_close_scratch(fp);
13072303Scarlsonj 	}
13082303Scarlsonj 
13092303Scarlsonj 	if (is_system_labeled()) {
13101676Sjpk 		(void) zone_getattr(zid, ZONE_ATTR_ROOT, zeptr->zroot,
13111676Sjpk 		    sizeof (zeptr->zroot));
13122712Snn35248 		(void) strlcpy(zeptr->zbrand, NATIVE_BRAND_NAME,
13134350Std153743 		    sizeof (zeptr->zbrand));
13142303Scarlsonj 	} else {
13152303Scarlsonj 		(void) strlcpy(zeptr->zroot, "/", sizeof (zeptr->zroot));
13162712Snn35248 		(void) zone_getattr(zid, ZONE_ATTR_BRAND, zeptr->zbrand,
13172712Snn35248 		    sizeof (zeptr->zbrand));
13182303Scarlsonj 	}
13192303Scarlsonj 
13200Sstevel@tonic-gate 	zeptr->zstate_str = "running";
13212303Scarlsonj 	if (zonecfg_get_uuid(zeptr->zname, uuid) == Z_OK &&
13222303Scarlsonj 	    !uuid_is_null(uuid))
13232303Scarlsonj 		uuid_unparse(uuid, zeptr->zuuid);
13243448Sdh155122 
13253448Sdh155122 	if (zone_getattr(zid, ZONE_ATTR_FLAGS, &flags, sizeof (flags)) < 0) {
13263448Sdh155122 		zperror2(zeptr->zname, gettext("could not get zone flags"));
13273448Sdh155122 		exit(Z_ERR);
13283448Sdh155122 	}
13293448Sdh155122 	if (flags & ZF_NET_EXCL)
13303448Sdh155122 		zeptr->ziptype = ZS_EXCLUSIVE;
13313448Sdh155122 	else
13323448Sdh155122 		zeptr->ziptype = ZS_SHARED;
13330Sstevel@tonic-gate }
13340Sstevel@tonic-gate 
13350Sstevel@tonic-gate static int
list_func(int argc,char * argv[])13360Sstevel@tonic-gate list_func(int argc, char *argv[])
13370Sstevel@tonic-gate {
13380Sstevel@tonic-gate 	zone_entry_t *zentp, zent;
1339766Scarlsonj 	int arg, retv;
13400Sstevel@tonic-gate 	boolean_t output = B_FALSE, verbose = B_FALSE, parsable = B_FALSE;
13410Sstevel@tonic-gate 	zone_state_t min_state = ZONE_STATE_RUNNING;
13420Sstevel@tonic-gate 	zoneid_t zone_id = getzoneid();
13430Sstevel@tonic-gate 
13440Sstevel@tonic-gate 	if (target_zone == NULL) {
13450Sstevel@tonic-gate 		/* all zones: default view to running but allow override */
13460Sstevel@tonic-gate 		optind = 0;
13470Sstevel@tonic-gate 		while ((arg = getopt(argc, argv, "?cipv")) != EOF) {
13480Sstevel@tonic-gate 			switch (arg) {
13490Sstevel@tonic-gate 			case '?':
13500Sstevel@tonic-gate 				sub_usage(SHELP_LIST, CMD_LIST);
13510Sstevel@tonic-gate 				return (optopt == '?' ? Z_OK : Z_USAGE);
13521339Sjonb 				/*
13531339Sjonb 				 * The 'i' and 'c' options are not mutually
13541339Sjonb 				 * exclusive so if 'c' is given, then min_state
13551339Sjonb 				 * is set to 0 (ZONE_STATE_CONFIGURED) which is
13561339Sjonb 				 * the lowest possible state.  If 'i' is given,
13571339Sjonb 				 * then min_state is set to be the lowest state
13581339Sjonb 				 * so far.
13591339Sjonb 				 */
13600Sstevel@tonic-gate 			case 'c':
13610Sstevel@tonic-gate 				min_state = ZONE_STATE_CONFIGURED;
13620Sstevel@tonic-gate 				break;
13630Sstevel@tonic-gate 			case 'i':
13641339Sjonb 				min_state = min(ZONE_STATE_INSTALLED,
13651339Sjonb 				    min_state);
13661339Sjonb 
13670Sstevel@tonic-gate 				break;
13680Sstevel@tonic-gate 			case 'p':
13690Sstevel@tonic-gate 				parsable = B_TRUE;
13700Sstevel@tonic-gate 				break;
13710Sstevel@tonic-gate 			case 'v':
13720Sstevel@tonic-gate 				verbose = B_TRUE;
13730Sstevel@tonic-gate 				break;
13740Sstevel@tonic-gate 			default:
13750Sstevel@tonic-gate 				sub_usage(SHELP_LIST, CMD_LIST);
13760Sstevel@tonic-gate 				return (Z_USAGE);
13770Sstevel@tonic-gate 			}
13780Sstevel@tonic-gate 		}
13790Sstevel@tonic-gate 		if (parsable && verbose) {
13800Sstevel@tonic-gate 			zerror(gettext("%s -p and -v are mutually exclusive."),
13810Sstevel@tonic-gate 			    cmd_to_str(CMD_LIST));
13820Sstevel@tonic-gate 			return (Z_ERR);
13830Sstevel@tonic-gate 		}
13841676Sjpk 		if (zone_id == GLOBAL_ZONEID || is_system_labeled()) {
1385766Scarlsonj 			retv = zone_print_list(min_state, verbose, parsable);
13860Sstevel@tonic-gate 		} else {
13872712Snn35248 			fake_up_local_zone(zone_id, &zent);
1388766Scarlsonj 			retv = Z_OK;
13890Sstevel@tonic-gate 			zone_print(&zent, verbose, parsable);
13900Sstevel@tonic-gate 		}
1391766Scarlsonj 		return (retv);
13920Sstevel@tonic-gate 	}
13930Sstevel@tonic-gate 
13940Sstevel@tonic-gate 	/*
13950Sstevel@tonic-gate 	 * Specific target zone: disallow -i/-c suboptions.
13960Sstevel@tonic-gate 	 */
13970Sstevel@tonic-gate 	optind = 0;
13980Sstevel@tonic-gate 	while ((arg = getopt(argc, argv, "?pv")) != EOF) {
13990Sstevel@tonic-gate 		switch (arg) {
14000Sstevel@tonic-gate 		case '?':
14010Sstevel@tonic-gate 			sub_usage(SHELP_LIST, CMD_LIST);
14020Sstevel@tonic-gate 			return (optopt == '?' ? Z_OK : Z_USAGE);
14030Sstevel@tonic-gate 		case 'p':
14040Sstevel@tonic-gate 			parsable = B_TRUE;
14050Sstevel@tonic-gate 			break;
14060Sstevel@tonic-gate 		case 'v':
14070Sstevel@tonic-gate 			verbose = B_TRUE;
14080Sstevel@tonic-gate 			break;
14090Sstevel@tonic-gate 		default:
14100Sstevel@tonic-gate 			sub_usage(SHELP_LIST, CMD_LIST);
14110Sstevel@tonic-gate 			return (Z_USAGE);
14120Sstevel@tonic-gate 		}
14130Sstevel@tonic-gate 	}
14140Sstevel@tonic-gate 	if (parsable && verbose) {
14150Sstevel@tonic-gate 		zerror(gettext("%s -p and -v are mutually exclusive."),
14160Sstevel@tonic-gate 		    cmd_to_str(CMD_LIST));
14170Sstevel@tonic-gate 		return (Z_ERR);
14180Sstevel@tonic-gate 	}
14190Sstevel@tonic-gate 	if (argc > optind) {
14200Sstevel@tonic-gate 		sub_usage(SHELP_LIST, CMD_LIST);
14210Sstevel@tonic-gate 		return (Z_USAGE);
14220Sstevel@tonic-gate 	}
14233979Sgjelinek 	if (zone_id != GLOBAL_ZONEID && !is_system_labeled()) {
14240Sstevel@tonic-gate 		fake_up_local_zone(zone_id, &zent);
14250Sstevel@tonic-gate 		/*
14260Sstevel@tonic-gate 		 * main() will issue a Z_NO_ZONE error if it cannot get an
14270Sstevel@tonic-gate 		 * id for target_zone, which in a non-global zone should
14280Sstevel@tonic-gate 		 * happen for any zone name except `zonename`.  Thus we
14290Sstevel@tonic-gate 		 * assert() that here but don't otherwise check.
14300Sstevel@tonic-gate 		 */
14310Sstevel@tonic-gate 		assert(strcmp(zent.zname, target_zone) == 0);
14320Sstevel@tonic-gate 		zone_print(&zent, verbose, parsable);
14330Sstevel@tonic-gate 		output = B_TRUE;
14340Sstevel@tonic-gate 	} else if ((zentp = lookup_running_zone(target_zone)) != NULL) {
14350Sstevel@tonic-gate 		zone_print(zentp, verbose, parsable);
14360Sstevel@tonic-gate 		output = B_TRUE;
1437766Scarlsonj 	} else if (lookup_zone_info(target_zone, ZONE_ID_UNDEFINED,
1438766Scarlsonj 	    &zent) == Z_OK) {
14390Sstevel@tonic-gate 		zone_print(&zent, verbose, parsable);
14400Sstevel@tonic-gate 		output = B_TRUE;
14410Sstevel@tonic-gate 	}
14423339Szt129084 
14433339Szt129084 	/*
14443339Szt129084 	 * Invoke brand-specific handler. Note that we do this
14453542Sgjelinek 	 * only if we're in the global zone, and target_zone is specified
14463542Sgjelinek 	 * and it is not the global zone.
14473339Szt129084 	 */
14483542Sgjelinek 	if (zone_id == GLOBAL_ZONEID && target_zone != NULL &&
14493542Sgjelinek 	    strcmp(target_zone, GLOBAL_ZONENAME) != 0)
14503339Szt129084 		if (invoke_brand_handler(CMD_LIST, argv) != Z_OK)
14513339Szt129084 			return (Z_ERR);
14523339Szt129084 
14530Sstevel@tonic-gate 	return (output ? Z_OK : Z_ERR);
14540Sstevel@tonic-gate }
14550Sstevel@tonic-gate 
14569310Sgerald.jelinek@sun.com int
do_subproc(char * cmdbuf)14570Sstevel@tonic-gate do_subproc(char *cmdbuf)
14580Sstevel@tonic-gate {
14592712Snn35248 	void (*saveint)(int);
14602712Snn35248 	void (*saveterm)(int);
14612712Snn35248 	void (*savequit)(int);
14622712Snn35248 	void (*savehup)(int);
14632712Snn35248 	int pid, child, status;
14642712Snn35248 
14652712Snn35248 	if ((child = vfork()) == 0) {
14662712Snn35248 		(void) execl("/bin/sh", "sh", "-c", cmdbuf, (char *)NULL);
14672712Snn35248 	}
14682712Snn35248 
14692712Snn35248 	if (child == -1)
14702712Snn35248 		return (-1);
14712712Snn35248 
14722712Snn35248 	saveint = sigset(SIGINT, SIG_IGN);
14732712Snn35248 	saveterm = sigset(SIGTERM, SIG_IGN);
14742712Snn35248 	savequit = sigset(SIGQUIT, SIG_IGN);
14752712Snn35248 	savehup = sigset(SIGHUP, SIG_IGN);
14762712Snn35248 
14772712Snn35248 	while ((pid = waitpid(child, &status, 0)) != child && pid != -1)
14782712Snn35248 		;
14792712Snn35248 
14802712Snn35248 	(void) sigset(SIGINT, saveint);
14812712Snn35248 	(void) sigset(SIGTERM, saveterm);
14822712Snn35248 	(void) sigset(SIGQUIT, savequit);
14832712Snn35248 	(void) sigset(SIGHUP, savehup);
14842712Snn35248 
14852712Snn35248 	return (pid == -1 ? -1 : status);
14862712Snn35248 }
14872712Snn35248 
14887089Sgjelinek int
subproc_status(const char * cmd,int status,boolean_t verbose_failure)14892712Snn35248 subproc_status(const char *cmd, int status, boolean_t verbose_failure)
14900Sstevel@tonic-gate {
14910Sstevel@tonic-gate 	if (WIFEXITED(status)) {
14920Sstevel@tonic-gate 		int exit_code = WEXITSTATUS(status);
14930Sstevel@tonic-gate 
14942712Snn35248 		if ((verbose_failure) && (exit_code != ZONE_SUBPROC_OK))
14952712Snn35248 			zerror(gettext("'%s' failed with exit code %d."), cmd,
14962712Snn35248 			    exit_code);
14972712Snn35248 
14982712Snn35248 		return (exit_code);
14990Sstevel@tonic-gate 	} else if (WIFSIGNALED(status)) {
15000Sstevel@tonic-gate 		int signal = WTERMSIG(status);
15010Sstevel@tonic-gate 		char sigstr[SIG2STR_MAX];
15020Sstevel@tonic-gate 
15030Sstevel@tonic-gate 		if (sig2str(signal, sigstr) == 0) {
15040Sstevel@tonic-gate 			zerror(gettext("'%s' terminated by signal SIG%s."), cmd,
15050Sstevel@tonic-gate 			    sigstr);
15060Sstevel@tonic-gate 		} else {
15070Sstevel@tonic-gate 			zerror(gettext("'%s' terminated by an unknown signal."),
15080Sstevel@tonic-gate 			    cmd);
15090Sstevel@tonic-gate 		}
15100Sstevel@tonic-gate 	} else {
15110Sstevel@tonic-gate 		zerror(gettext("'%s' failed for unknown reasons."), cmd);
15120Sstevel@tonic-gate 	}
15132712Snn35248 
15142712Snn35248 	/*
15152712Snn35248 	 * Assume a subprocess that died due to a signal or an unknown error
15162712Snn35248 	 * should be considered an exit code of ZONE_SUBPROC_FATAL, as the
15172712Snn35248 	 * user will likely need to do some manual cleanup.
15182712Snn35248 	 */
15192712Snn35248 	return (ZONE_SUBPROC_FATAL);
15200Sstevel@tonic-gate }
15210Sstevel@tonic-gate 
152212578SGlenn.Faden@Sun.COM static int
auth_check(char * user,char * zone,int cmd_num)152312578SGlenn.Faden@Sun.COM auth_check(char *user, char *zone, int cmd_num)
152412578SGlenn.Faden@Sun.COM {
152512578SGlenn.Faden@Sun.COM 	char authname[MAXAUTHS];
152612578SGlenn.Faden@Sun.COM 
152712578SGlenn.Faden@Sun.COM 	switch (cmd_num) {
152812578SGlenn.Faden@Sun.COM 	case CMD_LIST:
152912578SGlenn.Faden@Sun.COM 	case CMD_HELP:
153012578SGlenn.Faden@Sun.COM 		return (Z_OK);
153112578SGlenn.Faden@Sun.COM 	case SOURCE_ZONE:
153212578SGlenn.Faden@Sun.COM 		(void) strlcpy(authname, ZONE_CLONEFROM_AUTH, MAXAUTHS);
153312578SGlenn.Faden@Sun.COM 		break;
153412578SGlenn.Faden@Sun.COM 	case CMD_BOOT:
153512578SGlenn.Faden@Sun.COM 	case CMD_HALT:
153612578SGlenn.Faden@Sun.COM 	case CMD_READY:
153712578SGlenn.Faden@Sun.COM 	case CMD_REBOOT:
153812578SGlenn.Faden@Sun.COM 	case CMD_SYSBOOT:
153912578SGlenn.Faden@Sun.COM 	case CMD_VERIFY:
154012578SGlenn.Faden@Sun.COM 	case CMD_INSTALL:
154112578SGlenn.Faden@Sun.COM 	case CMD_UNINSTALL:
154212578SGlenn.Faden@Sun.COM 	case CMD_MOUNT:
154312578SGlenn.Faden@Sun.COM 	case CMD_UNMOUNT:
154412578SGlenn.Faden@Sun.COM 	case CMD_CLONE:
154512578SGlenn.Faden@Sun.COM 	case CMD_MOVE:
154612578SGlenn.Faden@Sun.COM 	case CMD_DETACH:
154712578SGlenn.Faden@Sun.COM 	case CMD_ATTACH:
154812578SGlenn.Faden@Sun.COM 	case CMD_MARK:
154912578SGlenn.Faden@Sun.COM 	case CMD_APPLY:
155012578SGlenn.Faden@Sun.COM 	default:
155112578SGlenn.Faden@Sun.COM 		(void) strlcpy(authname, ZONE_MANAGE_AUTH, MAXAUTHS);
155212578SGlenn.Faden@Sun.COM 		break;
155312578SGlenn.Faden@Sun.COM 	}
155412578SGlenn.Faden@Sun.COM 	(void) strlcat(authname, KV_OBJECT, MAXAUTHS);
155512578SGlenn.Faden@Sun.COM 	(void) strlcat(authname, zone, MAXAUTHS);
155612578SGlenn.Faden@Sun.COM 	if (chkauthattr(authname, user) == 0) {
155712578SGlenn.Faden@Sun.COM 		return (Z_ERR);
155812578SGlenn.Faden@Sun.COM 	} else {
155912578SGlenn.Faden@Sun.COM 		/*
156012578SGlenn.Faden@Sun.COM 		 * Some subcommands, e.g. install, run subcommands,
156112578SGlenn.Faden@Sun.COM 		 * e.g. sysidcfg, that require a real uid of root,
156212578SGlenn.Faden@Sun.COM 		 *  so switch to root, here.
156312578SGlenn.Faden@Sun.COM 		 */
156412578SGlenn.Faden@Sun.COM 		if (setuid(0) == -1) {
156512578SGlenn.Faden@Sun.COM 			zperror(gettext("insufficient privilege"), B_TRUE);
156612578SGlenn.Faden@Sun.COM 			return (Z_ERR);
156712578SGlenn.Faden@Sun.COM 		}
156812578SGlenn.Faden@Sun.COM 		return (Z_OK);
156912578SGlenn.Faden@Sun.COM 	}
157012578SGlenn.Faden@Sun.COM }
157112578SGlenn.Faden@Sun.COM 
15720Sstevel@tonic-gate /*
15730Sstevel@tonic-gate  * Various sanity checks; make sure:
15740Sstevel@tonic-gate  * 1. We're in the global zone.
15750Sstevel@tonic-gate  * 2. The calling user has sufficient privilege.
15760Sstevel@tonic-gate  * 3. The target zone is neither the global zone nor anything starting with
15770Sstevel@tonic-gate  *    "SUNW".
15780Sstevel@tonic-gate  * 4a. If we're looking for a 'not running' (i.e., configured or installed)
15790Sstevel@tonic-gate  *     zone, the name service knows about it.
15800Sstevel@tonic-gate  * 4b. For some operations which expect a zone not to be running, that it is
15810Sstevel@tonic-gate  *     not already running (or ready).
15820Sstevel@tonic-gate  */
15830Sstevel@tonic-gate static int
sanity_check(char * zone,int cmd_num,boolean_t running,boolean_t unsafe_when_running,boolean_t force)15840Sstevel@tonic-gate sanity_check(char *zone, int cmd_num, boolean_t running,
15852712Snn35248     boolean_t unsafe_when_running, boolean_t force)
15860Sstevel@tonic-gate {
15870Sstevel@tonic-gate 	zone_entry_t *zent;
15880Sstevel@tonic-gate 	priv_set_t *privset;
15892712Snn35248 	zone_state_t state, min_state;
1590766Scarlsonj 	char kernzone[ZONENAME_MAX];
1591766Scarlsonj 	FILE *fp;
15920Sstevel@tonic-gate 
15930Sstevel@tonic-gate 	if (getzoneid() != GLOBAL_ZONEID) {
15941645Scomay 		switch (cmd_num) {
15951645Scomay 		case CMD_HALT:
15961645Scomay 			zerror(gettext("use %s to %s this zone."), "halt(1M)",
15971645Scomay 			    cmd_to_str(cmd_num));
15981645Scomay 			break;
15991645Scomay 		case CMD_REBOOT:
16001645Scomay 			zerror(gettext("use %s to %s this zone."),
16011645Scomay 			    "reboot(1M)", cmd_to_str(cmd_num));
16021645Scomay 			break;
16031645Scomay 		default:
16041645Scomay 			zerror(gettext("must be in the global zone to %s a "
16051645Scomay 			    "zone."), cmd_to_str(cmd_num));
16061645Scomay 			break;
16071645Scomay 		}
16080Sstevel@tonic-gate 		return (Z_ERR);
16090Sstevel@tonic-gate 	}
16100Sstevel@tonic-gate 
16110Sstevel@tonic-gate 	if ((privset = priv_allocset()) == NULL) {
16120Sstevel@tonic-gate 		zerror(gettext("%s failed"), "priv_allocset");
16130Sstevel@tonic-gate 		return (Z_ERR);
16140Sstevel@tonic-gate 	}
16150Sstevel@tonic-gate 
16160Sstevel@tonic-gate 	if (getppriv(PRIV_EFFECTIVE, privset) != 0) {
16170Sstevel@tonic-gate 		zerror(gettext("%s failed"), "getppriv");
16180Sstevel@tonic-gate 		priv_freeset(privset);
16190Sstevel@tonic-gate 		return (Z_ERR);
16200Sstevel@tonic-gate 	}
16210Sstevel@tonic-gate 
16220Sstevel@tonic-gate 	if (priv_isfullset(privset) == B_FALSE) {
16230Sstevel@tonic-gate 		zerror(gettext("only a privileged user may %s a zone."),
16240Sstevel@tonic-gate 		    cmd_to_str(cmd_num));
16250Sstevel@tonic-gate 		priv_freeset(privset);
16260Sstevel@tonic-gate 		return (Z_ERR);
16270Sstevel@tonic-gate 	}
16280Sstevel@tonic-gate 	priv_freeset(privset);
16290Sstevel@tonic-gate 
16300Sstevel@tonic-gate 	if (zone == NULL) {
16310Sstevel@tonic-gate 		zerror(gettext("no zone specified"));
16320Sstevel@tonic-gate 		return (Z_ERR);
16330Sstevel@tonic-gate 	}
16340Sstevel@tonic-gate 
163512578SGlenn.Faden@Sun.COM 	if (auth_check(username, zone, cmd_num) == Z_ERR) {
163612578SGlenn.Faden@Sun.COM 		zerror(gettext("User %s is not authorized to %s this zone."),
163712578SGlenn.Faden@Sun.COM 		    username, cmd_to_str(cmd_num));
163812578SGlenn.Faden@Sun.COM 		return (Z_ERR);
163912578SGlenn.Faden@Sun.COM 	}
164012578SGlenn.Faden@Sun.COM 
16410Sstevel@tonic-gate 	if (strcmp(zone, GLOBAL_ZONENAME) == 0) {
16420Sstevel@tonic-gate 		zerror(gettext("%s operation is invalid for the global zone."),
16430Sstevel@tonic-gate 		    cmd_to_str(cmd_num));
16440Sstevel@tonic-gate 		return (Z_ERR);
16450Sstevel@tonic-gate 	}
16460Sstevel@tonic-gate 
16470Sstevel@tonic-gate 	if (strncmp(zone, "SUNW", 4) == 0) {
16480Sstevel@tonic-gate 		zerror(gettext("%s operation is invalid for zones starting "
16490Sstevel@tonic-gate 		    "with SUNW."), cmd_to_str(cmd_num));
16500Sstevel@tonic-gate 		return (Z_ERR);
16510Sstevel@tonic-gate 	}
16520Sstevel@tonic-gate 
1653766Scarlsonj 	if (!zonecfg_in_alt_root()) {
1654766Scarlsonj 		zent = lookup_running_zone(zone);
1655766Scarlsonj 	} else if ((fp = zonecfg_open_scratch("", B_FALSE)) == NULL) {
1656766Scarlsonj 		zent = NULL;
1657766Scarlsonj 	} else {
1658766Scarlsonj 		if (zonecfg_find_scratch(fp, zone, zonecfg_get_root(),
1659766Scarlsonj 		    kernzone, sizeof (kernzone)) == 0)
1660766Scarlsonj 			zent = lookup_running_zone(kernzone);
1661766Scarlsonj 		else
1662766Scarlsonj 			zent = NULL;
1663766Scarlsonj 		zonecfg_close_scratch(fp);
1664766Scarlsonj 	}
1665766Scarlsonj 
16660Sstevel@tonic-gate 	/*
16670Sstevel@tonic-gate 	 * Look up from the kernel for 'running' zones.
16680Sstevel@tonic-gate 	 */
16692712Snn35248 	if (running && !force) {
16700Sstevel@tonic-gate 		if (zent == NULL) {
16710Sstevel@tonic-gate 			zerror(gettext("not running"));
16720Sstevel@tonic-gate 			return (Z_ERR);
16730Sstevel@tonic-gate 		}
16740Sstevel@tonic-gate 	} else {
16750Sstevel@tonic-gate 		int err;
16760Sstevel@tonic-gate 
16770Sstevel@tonic-gate 		if (unsafe_when_running && zent != NULL) {
16780Sstevel@tonic-gate 			/* check whether the zone is ready or running */
16790Sstevel@tonic-gate 			if ((err = zone_get_state(zent->zname,
16800Sstevel@tonic-gate 			    &zent->zstate_num)) != Z_OK) {
16810Sstevel@tonic-gate 				errno = err;
16820Sstevel@tonic-gate 				zperror2(zent->zname,
16830Sstevel@tonic-gate 				    gettext("could not get state"));
16840Sstevel@tonic-gate 				/* can't tell, so hedge */
16850Sstevel@tonic-gate 				zent->zstate_str = "ready/running";
16860Sstevel@tonic-gate 			} else {
16870Sstevel@tonic-gate 				zent->zstate_str =
16880Sstevel@tonic-gate 				    zone_state_str(zent->zstate_num);
16890Sstevel@tonic-gate 			}
16900Sstevel@tonic-gate 			zerror(gettext("%s operation is invalid for %s zones."),
16910Sstevel@tonic-gate 			    cmd_to_str(cmd_num), zent->zstate_str);
16920Sstevel@tonic-gate 			return (Z_ERR);
16930Sstevel@tonic-gate 		}
16940Sstevel@tonic-gate 		if ((err = zone_get_state(zone, &state)) != Z_OK) {
16950Sstevel@tonic-gate 			errno = err;
16960Sstevel@tonic-gate 			zperror2(zone, gettext("could not get state"));
16970Sstevel@tonic-gate 			return (Z_ERR);
16980Sstevel@tonic-gate 		}
16990Sstevel@tonic-gate 		switch (cmd_num) {
17000Sstevel@tonic-gate 		case CMD_UNINSTALL:
17010Sstevel@tonic-gate 			if (state == ZONE_STATE_CONFIGURED) {
17020Sstevel@tonic-gate 				zerror(gettext("is already in state '%s'."),
17030Sstevel@tonic-gate 				    zone_state_str(ZONE_STATE_CONFIGURED));
17040Sstevel@tonic-gate 				return (Z_ERR);
17050Sstevel@tonic-gate 			}
17060Sstevel@tonic-gate 			break;
17071507Sgjelinek 		case CMD_ATTACH:
17088759Sgerald.jelinek@sun.com 			if (state == ZONE_STATE_INSTALLED) {
17098759Sgerald.jelinek@sun.com 				zerror(gettext("is already %s."),
17108759Sgerald.jelinek@sun.com 				    zone_state_str(ZONE_STATE_INSTALLED));
17118759Sgerald.jelinek@sun.com 				return (Z_ERR);
17128759Sgerald.jelinek@sun.com 			} else if (state == ZONE_STATE_INCOMPLETE && !force) {
17138759Sgerald.jelinek@sun.com 				zerror(gettext("zone is %s; %s required."),
17148759Sgerald.jelinek@sun.com 				    zone_state_str(ZONE_STATE_INCOMPLETE),
17158759Sgerald.jelinek@sun.com 				    cmd_to_str(CMD_UNINSTALL));
17168759Sgerald.jelinek@sun.com 				return (Z_ERR);
17178759Sgerald.jelinek@sun.com 			}
17188759Sgerald.jelinek@sun.com 			break;
17191300Sgjelinek 		case CMD_CLONE:
17200Sstevel@tonic-gate 		case CMD_INSTALL:
17210Sstevel@tonic-gate 			if (state == ZONE_STATE_INSTALLED) {
17220Sstevel@tonic-gate 				zerror(gettext("is already %s."),
17230Sstevel@tonic-gate 				    zone_state_str(ZONE_STATE_INSTALLED));
17240Sstevel@tonic-gate 				return (Z_ERR);
17250Sstevel@tonic-gate 			} else if (state == ZONE_STATE_INCOMPLETE) {
17260Sstevel@tonic-gate 				zerror(gettext("zone is %s; %s required."),
17270Sstevel@tonic-gate 				    zone_state_str(ZONE_STATE_INCOMPLETE),
17280Sstevel@tonic-gate 				    cmd_to_str(CMD_UNINSTALL));
17290Sstevel@tonic-gate 				return (Z_ERR);
17300Sstevel@tonic-gate 			}
17310Sstevel@tonic-gate 			break;
17321507Sgjelinek 		case CMD_DETACH:
17331300Sgjelinek 		case CMD_MOVE:
17340Sstevel@tonic-gate 		case CMD_READY:
17350Sstevel@tonic-gate 		case CMD_BOOT:
1736766Scarlsonj 		case CMD_MOUNT:
17372303Scarlsonj 		case CMD_MARK:
17382712Snn35248 			if ((cmd_num == CMD_BOOT || cmd_num == CMD_MOUNT) &&
17392712Snn35248 			    force)
17402712Snn35248 				min_state = ZONE_STATE_INCOMPLETE;
17418759Sgerald.jelinek@sun.com 			else if (cmd_num == CMD_MARK)
17428759Sgerald.jelinek@sun.com 				min_state = ZONE_STATE_CONFIGURED;
17432712Snn35248 			else
17442712Snn35248 				min_state = ZONE_STATE_INSTALLED;
17452712Snn35248 
17462712Snn35248 			if (state < min_state) {
17470Sstevel@tonic-gate 				zerror(gettext("must be %s before %s."),
17482712Snn35248 				    zone_state_str(min_state),
17490Sstevel@tonic-gate 				    cmd_to_str(cmd_num));
17500Sstevel@tonic-gate 				return (Z_ERR);
17510Sstevel@tonic-gate 			}
17520Sstevel@tonic-gate 			break;
17530Sstevel@tonic-gate 		case CMD_VERIFY:
17540Sstevel@tonic-gate 			if (state == ZONE_STATE_INCOMPLETE) {
17550Sstevel@tonic-gate 				zerror(gettext("zone is %s; %s required."),
17560Sstevel@tonic-gate 				    zone_state_str(ZONE_STATE_INCOMPLETE),
17570Sstevel@tonic-gate 				    cmd_to_str(CMD_UNINSTALL));
17580Sstevel@tonic-gate 				return (Z_ERR);
17590Sstevel@tonic-gate 			}
17600Sstevel@tonic-gate 			break;
1761766Scarlsonj 		case CMD_UNMOUNT:
1762766Scarlsonj 			if (state != ZONE_STATE_MOUNTED) {
1763766Scarlsonj 				zerror(gettext("must be %s before %s."),
1764766Scarlsonj 				    zone_state_str(ZONE_STATE_MOUNTED),
1765766Scarlsonj 				    cmd_to_str(cmd_num));
1766766Scarlsonj 				return (Z_ERR);
1767766Scarlsonj 			}
1768766Scarlsonj 			break;
176910718SJordan.Vaughan@Sun.com 		case CMD_SYSBOOT:
177010718SJordan.Vaughan@Sun.com 			if (state != ZONE_STATE_INSTALLED) {
177110718SJordan.Vaughan@Sun.com 				zerror(gettext("%s operation is invalid for %s "
177210718SJordan.Vaughan@Sun.com 				    "zones."), cmd_to_str(cmd_num),
177310718SJordan.Vaughan@Sun.com 				    zone_state_str(state));
177410718SJordan.Vaughan@Sun.com 				return (Z_ERR);
177510718SJordan.Vaughan@Sun.com 			}
177610718SJordan.Vaughan@Sun.com 			break;
17770Sstevel@tonic-gate 		}
17780Sstevel@tonic-gate 	}
17790Sstevel@tonic-gate 	return (Z_OK);
17800Sstevel@tonic-gate }
17810Sstevel@tonic-gate 
17820Sstevel@tonic-gate static int
halt_func(int argc,char * argv[])17830Sstevel@tonic-gate halt_func(int argc, char *argv[])
17840Sstevel@tonic-gate {
17850Sstevel@tonic-gate 	zone_cmd_arg_t zarg;
17860Sstevel@tonic-gate 	int arg;
17870Sstevel@tonic-gate 
1788766Scarlsonj 	if (zonecfg_in_alt_root()) {
1789766Scarlsonj 		zerror(gettext("cannot halt zone in alternate root"));
1790766Scarlsonj 		return (Z_ERR);
1791766Scarlsonj 	}
1792766Scarlsonj 
17930Sstevel@tonic-gate 	optind = 0;
17940Sstevel@tonic-gate 	if ((arg = getopt(argc, argv, "?")) != EOF) {
17950Sstevel@tonic-gate 		switch (arg) {
17960Sstevel@tonic-gate 		case '?':
17970Sstevel@tonic-gate 			sub_usage(SHELP_HALT, CMD_HALT);
17980Sstevel@tonic-gate 			return (optopt == '?' ? Z_OK : Z_USAGE);
17990Sstevel@tonic-gate 		default:
18000Sstevel@tonic-gate 			sub_usage(SHELP_HALT, CMD_HALT);
18010Sstevel@tonic-gate 			return (Z_USAGE);
18020Sstevel@tonic-gate 		}
18030Sstevel@tonic-gate 	}
18040Sstevel@tonic-gate 	if (argc > optind) {
18050Sstevel@tonic-gate 		sub_usage(SHELP_HALT, CMD_HALT);
18060Sstevel@tonic-gate 		return (Z_USAGE);
18070Sstevel@tonic-gate 	}
18080Sstevel@tonic-gate 	/*
18090Sstevel@tonic-gate 	 * zoneadmd should be the one to decide whether or not to proceed,
18100Sstevel@tonic-gate 	 * so even though it seems that the fourth parameter below should
18110Sstevel@tonic-gate 	 * perhaps be B_TRUE, it really shouldn't be.
18120Sstevel@tonic-gate 	 */
18132712Snn35248 	if (sanity_check(target_zone, CMD_HALT, B_FALSE, B_FALSE, B_FALSE)
18142712Snn35248 	    != Z_OK)
18150Sstevel@tonic-gate 		return (Z_ERR);
18160Sstevel@tonic-gate 
18173339Szt129084 	/*
18183339Szt129084 	 * Invoke brand-specific handler.
18193339Szt129084 	 */
18203339Szt129084 	if (invoke_brand_handler(CMD_HALT, argv) != Z_OK)
18213339Szt129084 		return (Z_ERR);
18223339Szt129084 
18230Sstevel@tonic-gate 	zarg.cmd = Z_HALT;
18247089Sgjelinek 	return ((zonecfg_call_zoneadmd(target_zone, &zarg, locale,
18257089Sgjelinek 	    B_TRUE) == 0) ?  Z_OK : Z_ERR);
18260Sstevel@tonic-gate }
18270Sstevel@tonic-gate 
18280Sstevel@tonic-gate static int
reboot_func(int argc,char * argv[])18290Sstevel@tonic-gate reboot_func(int argc, char *argv[])
18300Sstevel@tonic-gate {
18310Sstevel@tonic-gate 	zone_cmd_arg_t zarg;
18320Sstevel@tonic-gate 	int arg;
18330Sstevel@tonic-gate 
1834766Scarlsonj 	if (zonecfg_in_alt_root()) {
1835766Scarlsonj 		zerror(gettext("cannot reboot zone in alternate root"));
1836766Scarlsonj 		return (Z_ERR);
1837766Scarlsonj 	}
1838766Scarlsonj 
18390Sstevel@tonic-gate 	optind = 0;
18400Sstevel@tonic-gate 	if ((arg = getopt(argc, argv, "?")) != EOF) {
18410Sstevel@tonic-gate 		switch (arg) {
18420Sstevel@tonic-gate 		case '?':
18430Sstevel@tonic-gate 			sub_usage(SHELP_REBOOT, CMD_REBOOT);
18440Sstevel@tonic-gate 			return (optopt == '?' ? Z_OK : Z_USAGE);
18450Sstevel@tonic-gate 		default:
18460Sstevel@tonic-gate 			sub_usage(SHELP_REBOOT, CMD_REBOOT);
18470Sstevel@tonic-gate 			return (Z_USAGE);
18480Sstevel@tonic-gate 		}
18490Sstevel@tonic-gate 	}
18502267Sdp 
18512267Sdp 	zarg.bootbuf[0] = '\0';
18522267Sdp 	for (; optind < argc; optind++) {
18532267Sdp 		if (strlcat(zarg.bootbuf, argv[optind],
18542267Sdp 		    sizeof (zarg.bootbuf)) >= sizeof (zarg.bootbuf)) {
18552267Sdp 			zerror(gettext("Boot argument list too long"));
18562267Sdp 			return (Z_ERR);
18572267Sdp 		}
18582267Sdp 		if (optind < argc - 1)
18592267Sdp 			if (strlcat(zarg.bootbuf, " ", sizeof (zarg.bootbuf)) >=
18602267Sdp 			    sizeof (zarg.bootbuf)) {
18612267Sdp 				zerror(gettext("Boot argument list too long"));
18622267Sdp 				return (Z_ERR);
18632267Sdp 			}
18642267Sdp 	}
18652267Sdp 
18662267Sdp 
18670Sstevel@tonic-gate 	/*
18680Sstevel@tonic-gate 	 * zoneadmd should be the one to decide whether or not to proceed,
18690Sstevel@tonic-gate 	 * so even though it seems that the fourth parameter below should
18700Sstevel@tonic-gate 	 * perhaps be B_TRUE, it really shouldn't be.
18710Sstevel@tonic-gate 	 */
18722712Snn35248 	if (sanity_check(target_zone, CMD_REBOOT, B_TRUE, B_FALSE, B_FALSE)
18732712Snn35248 	    != Z_OK)
18740Sstevel@tonic-gate 		return (Z_ERR);
18753339Szt129084 	if (verify_details(CMD_REBOOT, argv) != Z_OK)
1876823Sgjelinek 		return (Z_ERR);
18770Sstevel@tonic-gate 
18780Sstevel@tonic-gate 	zarg.cmd = Z_REBOOT;
18797089Sgjelinek 	return ((zonecfg_call_zoneadmd(target_zone, &zarg, locale, B_TRUE) == 0)
18807089Sgjelinek 	    ? Z_OK : Z_ERR);
18817089Sgjelinek }
18827089Sgjelinek 
18837089Sgjelinek static int
get_hook(brand_handle_t bh,char * cmd,size_t len,int (* bp)(brand_handle_t,const char *,const char *,char *,size_t),char * zonename,char * zonepath)18847089Sgjelinek get_hook(brand_handle_t bh, char *cmd, size_t len, int (*bp)(brand_handle_t,
18857089Sgjelinek     const char *, const char *, char *, size_t), char *zonename, char *zonepath)
18867089Sgjelinek {
18877089Sgjelinek 	if (strlcpy(cmd, EXEC_PREFIX, len) >= len)
18887089Sgjelinek 		return (Z_ERR);
18897089Sgjelinek 
18907089Sgjelinek 	if (bp(bh, zonename, zonepath, cmd + EXEC_LEN, len - EXEC_LEN) != 0)
18917089Sgjelinek 		return (Z_ERR);
18927089Sgjelinek 
18937089Sgjelinek 	if (strlen(cmd) <= EXEC_LEN)
18947089Sgjelinek 		cmd[0] = '\0';
18957089Sgjelinek 
18967089Sgjelinek 	return (Z_OK);
18970Sstevel@tonic-gate }
18980Sstevel@tonic-gate 
18990Sstevel@tonic-gate static int
verify_brand(zone_dochandle_t handle,int cmd_num,char * argv[])19003339Szt129084 verify_brand(zone_dochandle_t handle, int cmd_num, char *argv[])
19012712Snn35248 {
19022712Snn35248 	char cmdbuf[MAXPATHLEN];
19032712Snn35248 	int err;
19042712Snn35248 	char zonepath[MAXPATHLEN];
19052727Sedp 	brand_handle_t bh = NULL;
19063339Szt129084 	int status, i;
19072712Snn35248 
19082712Snn35248 	/*
19092712Snn35248 	 * Fetch the verify command from the brand configuration.
19102712Snn35248 	 * "exec" the command so that the returned status is that of
19112712Snn35248 	 * the command and not the shell.
19122712Snn35248 	 */
19137089Sgjelinek 	if (handle == NULL) {
19147089Sgjelinek 		(void) strlcpy(zonepath, "-", sizeof (zonepath));
19157089Sgjelinek 	} else if ((err = zonecfg_get_zonepath(handle, zonepath,
19167089Sgjelinek 	    sizeof (zonepath))) != Z_OK) {
19172712Snn35248 		errno = err;
19183339Szt129084 		zperror(cmd_to_str(cmd_num), B_TRUE);
19192712Snn35248 		return (Z_ERR);
19202712Snn35248 	}
19212727Sedp 	if ((bh = brand_open(target_brand)) == NULL) {
19222712Snn35248 		zerror(gettext("missing or invalid brand"));
19232712Snn35248 		return (Z_ERR);
19242712Snn35248 	}
19252712Snn35248 
19262712Snn35248 	/*
19272712Snn35248 	 * If the brand has its own verification routine, execute it now.
19283339Szt129084 	 * The verification routine validates the intended zoneadm
19293339Szt129084 	 * operation for the specific brand. The zoneadm subcommand and
19303339Szt129084 	 * all its arguments are passed to the routine.
19312712Snn35248 	 */
19327089Sgjelinek 	err = get_hook(bh, cmdbuf, sizeof (cmdbuf), brand_get_verify_adm,
19337089Sgjelinek 	    target_zone, zonepath);
19342727Sedp 	brand_close(bh);
19357089Sgjelinek 	if (err != Z_OK)
19363339Szt129084 		return (Z_BRAND_ERROR);
19377089Sgjelinek 	if (cmdbuf[0] == '\0')
19383339Szt129084 		return (Z_OK);
19393339Szt129084 
19403339Szt129084 	if (strlcat(cmdbuf, cmd_to_str(cmd_num),
19413339Szt129084 	    sizeof (cmdbuf)) >= sizeof (cmdbuf))
19423339Szt129084 		return (Z_ERR);
19433339Szt129084 
19443339Szt129084 	/* Build the argv string */
19453339Szt129084 	i = 0;
19463339Szt129084 	while (argv[i] != NULL) {
19473339Szt129084 		if ((strlcat(cmdbuf, " ",
19483339Szt129084 		    sizeof (cmdbuf)) >= sizeof (cmdbuf)) ||
19493339Szt129084 		    (strlcat(cmdbuf, argv[i++],
19503339Szt129084 		    sizeof (cmdbuf)) >= sizeof (cmdbuf)))
19513339Szt129084 			return (Z_ERR);
19523339Szt129084 	}
19533339Szt129084 
19549310Sgerald.jelinek@sun.com 	status = do_subproc(cmdbuf);
19553339Szt129084 	err = subproc_status(gettext("brand-specific verification"),
19563339Szt129084 	    status, B_FALSE);
19573339Szt129084 
19583339Szt129084 	return ((err == ZONE_SUBPROC_OK) ? Z_OK : Z_BRAND_ERROR);
19592712Snn35248 }
19602712Snn35248 
19612712Snn35248 static int
verify_rctls(zone_dochandle_t handle)19620Sstevel@tonic-gate verify_rctls(zone_dochandle_t handle)
19630Sstevel@tonic-gate {
19640Sstevel@tonic-gate 	struct zone_rctltab rctltab;
19650Sstevel@tonic-gate 	size_t rbs = rctlblk_size();
19660Sstevel@tonic-gate 	rctlblk_t *rctlblk;
19670Sstevel@tonic-gate 	int error = Z_INVAL;
19680Sstevel@tonic-gate 
19690Sstevel@tonic-gate 	if ((rctlblk = malloc(rbs)) == NULL) {
19700Sstevel@tonic-gate 		zerror(gettext("failed to allocate %lu bytes: %s"), rbs,
19710Sstevel@tonic-gate 		    strerror(errno));
19720Sstevel@tonic-gate 		return (Z_NOMEM);
19730Sstevel@tonic-gate 	}
19740Sstevel@tonic-gate 
19750Sstevel@tonic-gate 	if (zonecfg_setrctlent(handle) != Z_OK) {
19760Sstevel@tonic-gate 		zerror(gettext("zonecfg_setrctlent failed"));
19770Sstevel@tonic-gate 		free(rctlblk);
19780Sstevel@tonic-gate 		return (error);
19790Sstevel@tonic-gate 	}
19800Sstevel@tonic-gate 
19810Sstevel@tonic-gate 	rctltab.zone_rctl_valptr = NULL;
19820Sstevel@tonic-gate 	while (zonecfg_getrctlent(handle, &rctltab) == Z_OK) {
19830Sstevel@tonic-gate 		struct zone_rctlvaltab *rctlval;
19840Sstevel@tonic-gate 		const char *name = rctltab.zone_rctl_name;
19850Sstevel@tonic-gate 
19860Sstevel@tonic-gate 		if (!zonecfg_is_rctl(name)) {
19870Sstevel@tonic-gate 			zerror(gettext("WARNING: Ignoring unrecognized rctl "
19880Sstevel@tonic-gate 			    "'%s'."),  name);
19890Sstevel@tonic-gate 			zonecfg_free_rctl_value_list(rctltab.zone_rctl_valptr);
19900Sstevel@tonic-gate 			rctltab.zone_rctl_valptr = NULL;
19910Sstevel@tonic-gate 			continue;
19920Sstevel@tonic-gate 		}
19930Sstevel@tonic-gate 
19940Sstevel@tonic-gate 		for (rctlval = rctltab.zone_rctl_valptr; rctlval != NULL;
19950Sstevel@tonic-gate 		    rctlval = rctlval->zone_rctlval_next) {
19960Sstevel@tonic-gate 			if (zonecfg_construct_rctlblk(rctlval, rctlblk)
19970Sstevel@tonic-gate 			    != Z_OK) {
19980Sstevel@tonic-gate 				zerror(gettext("invalid rctl value: "
19990Sstevel@tonic-gate 				    "(priv=%s,limit=%s,action%s)"),
20000Sstevel@tonic-gate 				    rctlval->zone_rctlval_priv,
20010Sstevel@tonic-gate 				    rctlval->zone_rctlval_limit,
20020Sstevel@tonic-gate 				    rctlval->zone_rctlval_action);
20030Sstevel@tonic-gate 				goto out;
20040Sstevel@tonic-gate 			}
20050Sstevel@tonic-gate 			if (!zonecfg_valid_rctl(name, rctlblk)) {
20060Sstevel@tonic-gate 				zerror(gettext("(priv=%s,limit=%s,action=%s) "
20070Sstevel@tonic-gate 				    "is not a valid value for rctl '%s'"),
20080Sstevel@tonic-gate 				    rctlval->zone_rctlval_priv,
20090Sstevel@tonic-gate 				    rctlval->zone_rctlval_limit,
20100Sstevel@tonic-gate 				    rctlval->zone_rctlval_action,
20110Sstevel@tonic-gate 				    name);
20120Sstevel@tonic-gate 				goto out;
20130Sstevel@tonic-gate 			}
20140Sstevel@tonic-gate 		}
20150Sstevel@tonic-gate 		zonecfg_free_rctl_value_list(rctltab.zone_rctl_valptr);
20160Sstevel@tonic-gate 	}
20170Sstevel@tonic-gate 	rctltab.zone_rctl_valptr = NULL;
20180Sstevel@tonic-gate 	error = Z_OK;
20190Sstevel@tonic-gate out:
20200Sstevel@tonic-gate 	zonecfg_free_rctl_value_list(rctltab.zone_rctl_valptr);
20210Sstevel@tonic-gate 	(void) zonecfg_endrctlent(handle);
20220Sstevel@tonic-gate 	free(rctlblk);
20230Sstevel@tonic-gate 	return (error);
20240Sstevel@tonic-gate }
20250Sstevel@tonic-gate 
20260Sstevel@tonic-gate static int
verify_pool(zone_dochandle_t handle)20270Sstevel@tonic-gate verify_pool(zone_dochandle_t handle)
20280Sstevel@tonic-gate {
20290Sstevel@tonic-gate 	char poolname[MAXPATHLEN];
20300Sstevel@tonic-gate 	pool_conf_t *poolconf;
20310Sstevel@tonic-gate 	pool_t *pool;
20320Sstevel@tonic-gate 	int status;
20330Sstevel@tonic-gate 	int error;
20340Sstevel@tonic-gate 
20350Sstevel@tonic-gate 	/*
20360Sstevel@tonic-gate 	 * This ends up being very similar to the check done in zoneadmd.
20370Sstevel@tonic-gate 	 */
20380Sstevel@tonic-gate 	error = zonecfg_get_pool(handle, poolname, sizeof (poolname));
20390Sstevel@tonic-gate 	if (error == Z_NO_ENTRY || (error == Z_OK && strlen(poolname) == 0)) {
20400Sstevel@tonic-gate 		/*
20410Sstevel@tonic-gate 		 * No pool specified.
20420Sstevel@tonic-gate 		 */
20430Sstevel@tonic-gate 		return (0);
20440Sstevel@tonic-gate 	}
20450Sstevel@tonic-gate 	if (error != Z_OK) {
20460Sstevel@tonic-gate 		zperror(gettext("Unable to retrieve pool name from "
20470Sstevel@tonic-gate 		    "configuration"), B_TRUE);
20480Sstevel@tonic-gate 		return (error);
20490Sstevel@tonic-gate 	}
20500Sstevel@tonic-gate 	/*
20510Sstevel@tonic-gate 	 * Don't do anything if pools aren't enabled.
20520Sstevel@tonic-gate 	 */
20530Sstevel@tonic-gate 	if (pool_get_status(&status) != PO_SUCCESS || status != POOL_ENABLED) {
20540Sstevel@tonic-gate 		zerror(gettext("WARNING: pools facility not active; "
20550Sstevel@tonic-gate 		    "zone will not be bound to pool '%s'."), poolname);
20560Sstevel@tonic-gate 		return (Z_OK);
20570Sstevel@tonic-gate 	}
20580Sstevel@tonic-gate 	/*
20590Sstevel@tonic-gate 	 * Try to provide a sane error message if the requested pool doesn't
20600Sstevel@tonic-gate 	 * exist.  It isn't clear that pools-related failures should
20610Sstevel@tonic-gate 	 * necessarily translate to a failure to verify the zone configuration,
20620Sstevel@tonic-gate 	 * hence they are not considered errors.
20630Sstevel@tonic-gate 	 */
20640Sstevel@tonic-gate 	if ((poolconf = pool_conf_alloc()) == NULL) {
20650Sstevel@tonic-gate 		zerror(gettext("WARNING: pool_conf_alloc failed; "
20660Sstevel@tonic-gate 		    "using default pool"));
20670Sstevel@tonic-gate 		return (Z_OK);
20680Sstevel@tonic-gate 	}
20690Sstevel@tonic-gate 	if (pool_conf_open(poolconf, pool_dynamic_location(), PO_RDONLY) !=
20700Sstevel@tonic-gate 	    PO_SUCCESS) {
20710Sstevel@tonic-gate 		zerror(gettext("WARNING: pool_conf_open failed; "
20720Sstevel@tonic-gate 		    "using default pool"));
20730Sstevel@tonic-gate 		pool_conf_free(poolconf);
20740Sstevel@tonic-gate 		return (Z_OK);
20750Sstevel@tonic-gate 	}
20760Sstevel@tonic-gate 	pool = pool_get_pool(poolconf, poolname);
20770Sstevel@tonic-gate 	(void) pool_conf_close(poolconf);
20780Sstevel@tonic-gate 	pool_conf_free(poolconf);
20790Sstevel@tonic-gate 	if (pool == NULL) {
20800Sstevel@tonic-gate 		zerror(gettext("WARNING: pool '%s' not found. "
20810Sstevel@tonic-gate 		    "using default pool"), poolname);
20820Sstevel@tonic-gate 	}
20830Sstevel@tonic-gate 
20840Sstevel@tonic-gate 	return (Z_OK);
20850Sstevel@tonic-gate }
20860Sstevel@tonic-gate 
20871393Slling /*
20881867Sgjelinek  * Verify that the special device/file system exists and is valid.
20891393Slling  */
20901393Slling static int
verify_fs_special(struct zone_fstab * fstab)20911393Slling verify_fs_special(struct zone_fstab *fstab)
20921393Slling {
20936734Sjohnlev 	struct stat64 st;
20941393Slling 
20952971Sgjelinek 	/*
20962971Sgjelinek 	 * This validation is really intended for standard zone administration.
20972971Sgjelinek 	 * If we are in a mini-root or some other upgrade situation where
20982971Sgjelinek 	 * we are using the scratch zone, just by-pass this.
20992971Sgjelinek 	 */
21002971Sgjelinek 	if (zonecfg_in_alt_root())
21012971Sgjelinek 		return (Z_OK);
21022971Sgjelinek 
21031393Slling 	if (strcmp(fstab->zone_fs_type, MNTTYPE_ZFS) == 0)
21041393Slling 		return (verify_fs_zfs(fstab));
21051393Slling 
21066734Sjohnlev 	if (stat64(fstab->zone_fs_special, &st) != 0) {
21071397Slling 		(void) fprintf(stderr, gettext("could not verify fs "
21081393Slling 		    "%s: could not access %s: %s\n"), fstab->zone_fs_dir,
21091393Slling 		    fstab->zone_fs_special, strerror(errno));
21101393Slling 		return (Z_ERR);
21111393Slling 	}
21121393Slling 
21131393Slling 	if (strcmp(st.st_fstype, MNTTYPE_NFS) == 0) {
21141393Slling 		/*
21151393Slling 		 * TRANSLATION_NOTE
21161393Slling 		 * fs and NFS are literals that should
21171393Slling 		 * not be translated.
21181393Slling 		 */
21191393Slling 		(void) fprintf(stderr, gettext("cannot verify "
21201867Sgjelinek 		    "fs %s: NFS mounted file system.\n"
21211867Sgjelinek 		    "\tA local file system must be used.\n"),
21221393Slling 		    fstab->zone_fs_special);
21231393Slling 		return (Z_ERR);
21241393Slling 	}
21251393Slling 
21261393Slling 	return (Z_OK);
21271393Slling }
21281393Slling 
2129823Sgjelinek static int
isregfile(const char * path)21306734Sjohnlev isregfile(const char *path)
21316734Sjohnlev {
21326734Sjohnlev 	struct stat64 st;
21336734Sjohnlev 
21346734Sjohnlev 	if (stat64(path, &st) == -1)
21356734Sjohnlev 		return (-1);
21366734Sjohnlev 
21376734Sjohnlev 	return (S_ISREG(st.st_mode));
21386734Sjohnlev }
21396734Sjohnlev 
21406734Sjohnlev static int
verify_filesystems(zone_dochandle_t handle)21410Sstevel@tonic-gate verify_filesystems(zone_dochandle_t handle)
21420Sstevel@tonic-gate {
21430Sstevel@tonic-gate 	int return_code = Z_OK;
21440Sstevel@tonic-gate 	struct zone_fstab fstab;
21450Sstevel@tonic-gate 	char cmdbuf[MAXPATHLEN];
21460Sstevel@tonic-gate 	struct stat st;
21470Sstevel@tonic-gate 
21480Sstevel@tonic-gate 	/*
21490Sstevel@tonic-gate 	 * Since the actual mount point is not known until the dependent mounts
21500Sstevel@tonic-gate 	 * are performed, we don't attempt any path validation here: that will
21510Sstevel@tonic-gate 	 * happen later when zoneadmd actually does the mounts.
21520Sstevel@tonic-gate 	 */
21530Sstevel@tonic-gate 	if (zonecfg_setfsent(handle) != Z_OK) {
21541867Sgjelinek 		(void) fprintf(stderr, gettext("could not verify file systems: "
21550Sstevel@tonic-gate 		    "unable to enumerate mounts\n"));
21560Sstevel@tonic-gate 		return (Z_ERR);
21570Sstevel@tonic-gate 	}
21580Sstevel@tonic-gate 	while (zonecfg_getfsent(handle, &fstab) == Z_OK) {
21590Sstevel@tonic-gate 		if (!zonecfg_valid_fs_type(fstab.zone_fs_type)) {
21600Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("cannot verify fs %s: "
21610Sstevel@tonic-gate 			    "type %s is not allowed.\n"), fstab.zone_fs_dir,
21620Sstevel@tonic-gate 			    fstab.zone_fs_type);
21630Sstevel@tonic-gate 			return_code = Z_ERR;
21640Sstevel@tonic-gate 			goto next_fs;
21650Sstevel@tonic-gate 		}
21660Sstevel@tonic-gate 		/*
21670Sstevel@tonic-gate 		 * Verify /usr/lib/fs/<fstype>/mount exists.
21680Sstevel@tonic-gate 		 */
21690Sstevel@tonic-gate 		if (snprintf(cmdbuf, sizeof (cmdbuf), "/usr/lib/fs/%s/mount",
21700Sstevel@tonic-gate 		    fstab.zone_fs_type) > sizeof (cmdbuf)) {
21710Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("cannot verify fs %s: "
21720Sstevel@tonic-gate 			    "type %s is too long.\n"), fstab.zone_fs_dir,
21730Sstevel@tonic-gate 			    fstab.zone_fs_type);
21740Sstevel@tonic-gate 			return_code = Z_ERR;
21750Sstevel@tonic-gate 			goto next_fs;
21760Sstevel@tonic-gate 		}
21770Sstevel@tonic-gate 		if (stat(cmdbuf, &st) != 0) {
2178924Sgjelinek 			(void) fprintf(stderr, gettext("could not verify fs "
2179924Sgjelinek 			    "%s: could not access %s: %s\n"), fstab.zone_fs_dir,
21800Sstevel@tonic-gate 			    cmdbuf, strerror(errno));
21810Sstevel@tonic-gate 			return_code = Z_ERR;
21820Sstevel@tonic-gate 			goto next_fs;
21830Sstevel@tonic-gate 		}
21840Sstevel@tonic-gate 		if (!S_ISREG(st.st_mode)) {
2185924Sgjelinek 			(void) fprintf(stderr, gettext("could not verify fs "
2186924Sgjelinek 			    "%s: %s is not a regular file\n"),
2187924Sgjelinek 			    fstab.zone_fs_dir, cmdbuf);
21880Sstevel@tonic-gate 			return_code = Z_ERR;
21890Sstevel@tonic-gate 			goto next_fs;
21900Sstevel@tonic-gate 		}
21910Sstevel@tonic-gate 		/*
21926734Sjohnlev 		 * If zone_fs_raw is set, verify that there's an fsck
21936734Sjohnlev 		 * binary for it.  If zone_fs_raw is not set, and it's
21946734Sjohnlev 		 * not a regular file (lofi mount), and there's an fsck
21956734Sjohnlev 		 * binary for it, complain.
21960Sstevel@tonic-gate 		 */
21970Sstevel@tonic-gate 		if (snprintf(cmdbuf, sizeof (cmdbuf), "/usr/lib/fs/%s/fsck",
21980Sstevel@tonic-gate 		    fstab.zone_fs_type) > sizeof (cmdbuf)) {
21990Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("cannot verify fs %s: "
22000Sstevel@tonic-gate 			    "type %s is too long.\n"), fstab.zone_fs_dir,
22010Sstevel@tonic-gate 			    fstab.zone_fs_type);
22020Sstevel@tonic-gate 			return_code = Z_ERR;
22030Sstevel@tonic-gate 			goto next_fs;
22040Sstevel@tonic-gate 		}
22050Sstevel@tonic-gate 		if (fstab.zone_fs_raw[0] != '\0' &&
22060Sstevel@tonic-gate 		    (stat(cmdbuf, &st) != 0 || !S_ISREG(st.st_mode))) {
22070Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("cannot verify fs %s: "
22080Sstevel@tonic-gate 			    "'raw' device specified but "
22090Sstevel@tonic-gate 			    "no fsck executable exists for %s\n"),
22100Sstevel@tonic-gate 			    fstab.zone_fs_dir, fstab.zone_fs_type);
22110Sstevel@tonic-gate 			return_code = Z_ERR;
22120Sstevel@tonic-gate 			goto next_fs;
22136734Sjohnlev 		} else if (fstab.zone_fs_raw[0] == '\0' &&
22146734Sjohnlev 		    stat(cmdbuf, &st) == 0 &&
22156734Sjohnlev 		    isregfile(fstab.zone_fs_special) != 1) {
22166734Sjohnlev 			(void) fprintf(stderr, gettext("could not verify fs "
22176734Sjohnlev 			    "%s: must specify 'raw' device for %s "
22186734Sjohnlev 			    "file systems\n"),
22196734Sjohnlev 			    fstab.zone_fs_dir, fstab.zone_fs_type);
22206734Sjohnlev 			return_code = Z_ERR;
22216734Sjohnlev 			goto next_fs;
22220Sstevel@tonic-gate 		}
22231393Slling 
22241393Slling 		/* Verify fs_special. */
22251393Slling 		if ((return_code = verify_fs_special(&fstab)) != Z_OK)
2226823Sgjelinek 			goto next_fs;
22271393Slling 
22281393Slling 		/* Verify fs_raw. */
2229823Sgjelinek 		if (fstab.zone_fs_raw[0] != '\0' &&
2230823Sgjelinek 		    stat(fstab.zone_fs_raw, &st) != 0) {
2231924Sgjelinek 			/*
2232924Sgjelinek 			 * TRANSLATION_NOTE
2233924Sgjelinek 			 * fs is a literal that should not be translated.
2234924Sgjelinek 			 */
2235924Sgjelinek 			(void) fprintf(stderr, gettext("could not verify fs "
2236924Sgjelinek 			    "%s: could not access %s: %s\n"), fstab.zone_fs_dir,
2237823Sgjelinek 			    fstab.zone_fs_raw, strerror(errno));
2238823Sgjelinek 			return_code = Z_ERR;
2239823Sgjelinek 			goto next_fs;
2240823Sgjelinek 		}
22410Sstevel@tonic-gate next_fs:
22420Sstevel@tonic-gate 		zonecfg_free_fs_option_list(fstab.zone_fs_options);
22430Sstevel@tonic-gate 	}
22440Sstevel@tonic-gate 	(void) zonecfg_endfsent(handle);
22450Sstevel@tonic-gate 
22460Sstevel@tonic-gate 	return (return_code);
22470Sstevel@tonic-gate }
22480Sstevel@tonic-gate 
22490Sstevel@tonic-gate static int
verify_limitpriv(zone_dochandle_t handle)22501645Scomay verify_limitpriv(zone_dochandle_t handle)
22511645Scomay {
22521645Scomay 	char *privname = NULL;
22531645Scomay 	int err;
22541645Scomay 	priv_set_t *privs;
22551645Scomay 
22561645Scomay 	if ((privs = priv_allocset()) == NULL) {
22571645Scomay 		zperror(gettext("failed to allocate privilege set"), B_FALSE);
22581645Scomay 		return (Z_NOMEM);
22591645Scomay 	}
22601645Scomay 	err = zonecfg_get_privset(handle, privs, &privname);
22611645Scomay 	switch (err) {
22621645Scomay 	case Z_OK:
22631645Scomay 		break;
22641645Scomay 	case Z_PRIV_PROHIBITED:
22651645Scomay 		(void) fprintf(stderr, gettext("privilege \"%s\" is not "
22661645Scomay 		    "permitted within the zone's privilege set\n"), privname);
22671645Scomay 		break;
22681645Scomay 	case Z_PRIV_REQUIRED:
22691645Scomay 		(void) fprintf(stderr, gettext("required privilege \"%s\" is "
22701645Scomay 		    "missing from the zone's privilege set\n"), privname);
22711645Scomay 		break;
22721645Scomay 	case Z_PRIV_UNKNOWN:
22731645Scomay 		(void) fprintf(stderr, gettext("unknown privilege \"%s\" "
22741645Scomay 		    "specified in the zone's privilege set\n"), privname);
22751645Scomay 		break;
22761645Scomay 	default:
22771645Scomay 		zperror(
22781645Scomay 		    gettext("failed to determine the zone's privilege set"),
22791645Scomay 		    B_TRUE);
22801645Scomay 		break;
22811645Scomay 	}
22821645Scomay 	free(privname);
22831645Scomay 	priv_freeset(privs);
22841645Scomay 	return (err);
22851645Scomay }
22861645Scomay 
22871915Sgjelinek static void
free_local_netifs(int if_cnt,struct net_if ** if_list)22881915Sgjelinek free_local_netifs(int if_cnt, struct net_if **if_list)
22891915Sgjelinek {
22901915Sgjelinek 	int		i;
22911915Sgjelinek 
22921915Sgjelinek 	for (i = 0; i < if_cnt; i++) {
22931915Sgjelinek 		free(if_list[i]->name);
22941915Sgjelinek 		free(if_list[i]);
22951915Sgjelinek 	}
22961915Sgjelinek 	free(if_list);
22971915Sgjelinek }
22981915Sgjelinek 
22991915Sgjelinek /*
23001915Sgjelinek  * Get a list of the network interfaces, along with their address families,
23011915Sgjelinek  * that are plumbed in the global zone.  See if_tcp(7p) for a description
23021915Sgjelinek  * of the ioctls used here.
23031915Sgjelinek  */
23041915Sgjelinek static int
get_local_netifs(int * if_cnt,struct net_if *** if_list)23051915Sgjelinek get_local_netifs(int *if_cnt, struct net_if ***if_list)
23061915Sgjelinek {
23071915Sgjelinek 	int		s;
23081915Sgjelinek 	int		i;
23091915Sgjelinek 	int		res = Z_OK;
23101915Sgjelinek 	int		space_needed;
23111915Sgjelinek 	int		cnt = 0;
23121915Sgjelinek 	struct		lifnum if_num;
23131915Sgjelinek 	struct		lifconf if_conf;
23141915Sgjelinek 	struct		lifreq *if_reqp;
23151915Sgjelinek 	char		*if_buf;
23161915Sgjelinek 	struct net_if	**local_ifs = NULL;
23171915Sgjelinek 
23181915Sgjelinek 	*if_cnt = 0;
23191915Sgjelinek 	*if_list = NULL;
23201915Sgjelinek 
23211915Sgjelinek 	if ((s = socket(SOCKET_AF(AF_INET), SOCK_DGRAM, 0)) < 0)
23221915Sgjelinek 		return (Z_ERR);
23231915Sgjelinek 
23241915Sgjelinek 	/*
23251915Sgjelinek 	 * Come back here in the unlikely event that the number of interfaces
23261915Sgjelinek 	 * increases between the time we get the count and the time we do the
23271915Sgjelinek 	 * SIOCGLIFCONF ioctl.
23281915Sgjelinek 	 */
23291915Sgjelinek retry:
23301915Sgjelinek 	/* Get the number of interfaces. */
23311915Sgjelinek 	if_num.lifn_family = AF_UNSPEC;
23321915Sgjelinek 	if_num.lifn_flags = LIFC_NOXMIT;
23331915Sgjelinek 	if (ioctl(s, SIOCGLIFNUM, &if_num) < 0) {
23341915Sgjelinek 		(void) close(s);
23351915Sgjelinek 		return (Z_ERR);
23361915Sgjelinek 	}
23371915Sgjelinek 
23381915Sgjelinek 	/* Get the interface configuration list. */
23391915Sgjelinek 	space_needed = if_num.lifn_count * sizeof (struct lifreq);
23401915Sgjelinek 	if ((if_buf = malloc(space_needed)) == NULL) {
23411915Sgjelinek 		(void) close(s);
23421915Sgjelinek 		return (Z_ERR);
23431915Sgjelinek 	}
23441915Sgjelinek 	if_conf.lifc_family = AF_UNSPEC;
23451915Sgjelinek 	if_conf.lifc_flags = LIFC_NOXMIT;
23461915Sgjelinek 	if_conf.lifc_len = space_needed;
23471915Sgjelinek 	if_conf.lifc_buf = if_buf;
23481915Sgjelinek 	if (ioctl(s, SIOCGLIFCONF, &if_conf) < 0) {
23491915Sgjelinek 		free(if_buf);
23501915Sgjelinek 		/*
23511915Sgjelinek 		 * SIOCGLIFCONF returns EINVAL if the buffer we passed in is
23521915Sgjelinek 		 * too small.  In this case go back and get the new if cnt.
23531915Sgjelinek 		 */
23541915Sgjelinek 		if (errno == EINVAL)
23551915Sgjelinek 			goto retry;
23561915Sgjelinek 
23571915Sgjelinek 		(void) close(s);
23581915Sgjelinek 		return (Z_ERR);
23591915Sgjelinek 	}
23601915Sgjelinek 	(void) close(s);
23611915Sgjelinek 
23621915Sgjelinek 	/* Get the name and address family for each interface. */
23631915Sgjelinek 	if_reqp = if_conf.lifc_req;
23641915Sgjelinek 	for (i = 0; i < (if_conf.lifc_len / sizeof (struct lifreq)); i++) {
23651915Sgjelinek 		struct net_if	**p;
23661915Sgjelinek 		struct lifreq	req;
23671915Sgjelinek 
23681915Sgjelinek 		if (strcmp(LOOPBACK_IF, if_reqp->lifr_name) == 0) {
23691915Sgjelinek 			if_reqp++;
23701915Sgjelinek 			continue;
23711915Sgjelinek 		}
23721915Sgjelinek 
23731915Sgjelinek 		if ((s = socket(SOCKET_AF(if_reqp->lifr_addr.ss_family),
23741915Sgjelinek 		    SOCK_DGRAM, 0)) == -1) {
23751915Sgjelinek 			res = Z_ERR;
23761915Sgjelinek 			break;
23771915Sgjelinek 		}
23781915Sgjelinek 
23791915Sgjelinek 		(void) strncpy(req.lifr_name, if_reqp->lifr_name,
23801915Sgjelinek 		    sizeof (req.lifr_name));
23811915Sgjelinek 		if (ioctl(s, SIOCGLIFADDR, &req) < 0) {
23821915Sgjelinek 			(void) close(s);
23831915Sgjelinek 			if_reqp++;
23841915Sgjelinek 			continue;
23851915Sgjelinek 		}
23861915Sgjelinek 
23871915Sgjelinek 		if ((p = (struct net_if **)realloc(local_ifs,
23881915Sgjelinek 		    sizeof (struct net_if *) * (cnt + 1))) == NULL) {
23891915Sgjelinek 			res = Z_ERR;
23901915Sgjelinek 			break;
23911915Sgjelinek 		}
23921915Sgjelinek 		local_ifs = p;
23931915Sgjelinek 
23941915Sgjelinek 		if ((local_ifs[cnt] = malloc(sizeof (struct net_if))) == NULL) {
23951915Sgjelinek 			res = Z_ERR;
23961915Sgjelinek 			break;
23971915Sgjelinek 		}
23981915Sgjelinek 
23991915Sgjelinek 		if ((local_ifs[cnt]->name = strdup(if_reqp->lifr_name))
24001915Sgjelinek 		    == NULL) {
24011915Sgjelinek 			free(local_ifs[cnt]);
24021915Sgjelinek 			res = Z_ERR;
24031915Sgjelinek 			break;
24041915Sgjelinek 		}
24051915Sgjelinek 		local_ifs[cnt]->af = req.lifr_addr.ss_family;
24061915Sgjelinek 		cnt++;
24071915Sgjelinek 
24081915Sgjelinek 		(void) close(s);
24091915Sgjelinek 		if_reqp++;
24101915Sgjelinek 	}
24111915Sgjelinek 
24121915Sgjelinek 	free(if_buf);
24131915Sgjelinek 
24141915Sgjelinek 	if (res != Z_OK) {
24151915Sgjelinek 		free_local_netifs(cnt, local_ifs);
24161915Sgjelinek 	} else {
24171915Sgjelinek 		*if_cnt = cnt;
24181915Sgjelinek 		*if_list = local_ifs;
24191915Sgjelinek 	}
24201915Sgjelinek 
24211915Sgjelinek 	return (res);
24221915Sgjelinek }
24231915Sgjelinek 
24241915Sgjelinek static char *
af2str(int af)24251915Sgjelinek af2str(int af)
24261915Sgjelinek {
24271915Sgjelinek 	switch (af) {
24281915Sgjelinek 	case AF_INET:
24291915Sgjelinek 		return ("IPv4");
24301915Sgjelinek 	case AF_INET6:
24311915Sgjelinek 		return ("IPv6");
24321915Sgjelinek 	default:
24331915Sgjelinek 		return ("Unknown");
24341915Sgjelinek 	}
24351915Sgjelinek }
24361915Sgjelinek 
24371915Sgjelinek /*
24381915Sgjelinek  * Cross check the network interface name and address family with the
24391915Sgjelinek  * interfaces that are set up in the global zone so that we can print the
24401915Sgjelinek  * appropriate error message.
24411915Sgjelinek  */
24421915Sgjelinek static void
print_net_err(char * phys,char * addr,int af,char * msg)24431915Sgjelinek print_net_err(char *phys, char *addr, int af, char *msg)
24441915Sgjelinek {
24451915Sgjelinek 	int		i;
24461915Sgjelinek 	int		local_if_cnt = 0;
24471915Sgjelinek 	struct net_if	**local_ifs = NULL;
24481915Sgjelinek 	boolean_t	found_if = B_FALSE;
24491915Sgjelinek 	boolean_t	found_af = B_FALSE;
24501915Sgjelinek 
24511915Sgjelinek 	if (get_local_netifs(&local_if_cnt, &local_ifs) != Z_OK) {
24521915Sgjelinek 		(void) fprintf(stderr,
24531915Sgjelinek 		    gettext("could not verify %s %s=%s %s=%s\n\t%s\n"),
24541915Sgjelinek 		    "net", "address", addr, "physical", phys, msg);
24551915Sgjelinek 		return;
24561915Sgjelinek 	}
24571915Sgjelinek 
24581915Sgjelinek 	for (i = 0; i < local_if_cnt; i++) {
24591915Sgjelinek 		if (strcmp(phys, local_ifs[i]->name) == 0) {
24601915Sgjelinek 			found_if = B_TRUE;
24611915Sgjelinek 			if (af == local_ifs[i]->af) {
24621915Sgjelinek 				found_af = B_TRUE;
24631915Sgjelinek 				break;
24641915Sgjelinek 			}
24651915Sgjelinek 		}
24661915Sgjelinek 	}
24671915Sgjelinek 
24681915Sgjelinek 	free_local_netifs(local_if_cnt, local_ifs);
24691915Sgjelinek 
24701915Sgjelinek 	if (!found_if) {
24711915Sgjelinek 		(void) fprintf(stderr,
24721915Sgjelinek 		    gettext("could not verify %s %s=%s\n\t"
24731915Sgjelinek 		    "network interface %s is not plumbed in the global zone\n"),
24741915Sgjelinek 		    "net", "physical", phys, phys);
24751915Sgjelinek 		return;
24761915Sgjelinek 	}
24771915Sgjelinek 
24781915Sgjelinek 	/*
24791915Sgjelinek 	 * Print this error if we were unable to find the address family
24801915Sgjelinek 	 * for this interface.  If the af variable is not initialized to
24811915Sgjelinek 	 * to something meaningful by the caller (not AF_UNSPEC) then we
24821915Sgjelinek 	 * also skip this message since it wouldn't be informative.
24831915Sgjelinek 	 */
24841915Sgjelinek 	if (!found_af && af != AF_UNSPEC) {
24851915Sgjelinek 		(void) fprintf(stderr,
24861915Sgjelinek 		    gettext("could not verify %s %s=%s %s=%s\n\tthe %s address "
24873448Sdh155122 		    "family is not configured on this network interface in "
24883448Sdh155122 		    "the\n\tglobal zone\n"),
24891915Sgjelinek 		    "net", "address", addr, "physical", phys, af2str(af));
24901915Sgjelinek 		return;
24911915Sgjelinek 	}
24921915Sgjelinek 
24931915Sgjelinek 	(void) fprintf(stderr,
24941915Sgjelinek 	    gettext("could not verify %s %s=%s %s=%s\n\t%s\n"),
24951915Sgjelinek 	    "net", "address", addr, "physical", phys, msg);
24961915Sgjelinek }
24971915Sgjelinek 
24981645Scomay static int
verify_handle(int cmd_num,zone_dochandle_t handle,char * argv[])24993339Szt129084 verify_handle(int cmd_num, zone_dochandle_t handle, char *argv[])
25000Sstevel@tonic-gate {
25010Sstevel@tonic-gate 	struct zone_nwiftab nwiftab;
25020Sstevel@tonic-gate 	int return_code = Z_OK;
25030Sstevel@tonic-gate 	int err;
2504766Scarlsonj 	boolean_t in_alt_root;
25053448Sdh155122 	zone_iptype_t iptype;
250610616SSebastien.Roy@Sun.COM 	dladm_handle_t dh;
250710616SSebastien.Roy@Sun.COM 	dladm_status_t status;
250810616SSebastien.Roy@Sun.COM 	datalink_id_t linkid;
250910616SSebastien.Roy@Sun.COM 	char errmsg[DLADM_STRSIZE];
25100Sstevel@tonic-gate 
2511766Scarlsonj 	in_alt_root = zonecfg_in_alt_root();
2512766Scarlsonj 	if (in_alt_root)
2513766Scarlsonj 		goto no_net;
2514766Scarlsonj 
25153448Sdh155122 	if ((err = zonecfg_get_iptype(handle, &iptype)) != Z_OK) {
25163448Sdh155122 		errno = err;
25173448Sdh155122 		zperror(cmd_to_str(cmd_num), B_TRUE);
25183448Sdh155122 		zonecfg_fini_handle(handle);
25193448Sdh155122 		return (Z_ERR);
25203448Sdh155122 	}
25210Sstevel@tonic-gate 	if ((err = zonecfg_setnwifent(handle)) != Z_OK) {
25220Sstevel@tonic-gate 		errno = err;
25230Sstevel@tonic-gate 		zperror(cmd_to_str(cmd_num), B_TRUE);
25240Sstevel@tonic-gate 		zonecfg_fini_handle(handle);
25250Sstevel@tonic-gate 		return (Z_ERR);
25260Sstevel@tonic-gate 	}
25270Sstevel@tonic-gate 	while (zonecfg_getnwifent(handle, &nwiftab) == Z_OK) {
25280Sstevel@tonic-gate 		struct lifreq lifr;
25291915Sgjelinek 		sa_family_t af = AF_UNSPEC;
25303448Sdh155122 		char dl_owner_zname[ZONENAME_MAX];
25313448Sdh155122 		zoneid_t dl_owner_zid;
25323448Sdh155122 		zoneid_t target_zid;
25333448Sdh155122 		int res;
25340Sstevel@tonic-gate 
25350Sstevel@tonic-gate 		/* skip any loopback interfaces */
25360Sstevel@tonic-gate 		if (strcmp(nwiftab.zone_nwif_physical, "lo0") == 0)
25370Sstevel@tonic-gate 			continue;
25383448Sdh155122 		switch (iptype) {
25393448Sdh155122 		case ZS_SHARED:
25403448Sdh155122 			if ((res = zonecfg_valid_net_address(
25413448Sdh155122 			    nwiftab.zone_nwif_address, &lifr)) != Z_OK) {
25423448Sdh155122 				print_net_err(nwiftab.zone_nwif_physical,
25433448Sdh155122 				    nwiftab.zone_nwif_address, af,
25443448Sdh155122 				    zonecfg_strerror(res));
25454350Std153743 				return_code = Z_ERR;
25464350Std153743 				continue;
25473448Sdh155122 			}
25483448Sdh155122 			af = lifr.lifr_addr.ss_family;
25493448Sdh155122 			if (!zonecfg_ifname_exists(af,
25503448Sdh155122 			    nwiftab.zone_nwif_physical)) {
25513448Sdh155122 				/*
25523448Sdh155122 				 * The interface failed to come up. We continue
25533448Sdh155122 				 * on anyway for the sake of consistency: a
25543448Sdh155122 				 * zone is not shut down if the interface fails
25553448Sdh155122 				 * any time after boot, nor does the global zone
25563448Sdh155122 				 * fail to boot if an interface fails.
25573448Sdh155122 				 */
25583448Sdh155122 				(void) fprintf(stderr,
25593448Sdh155122 				    gettext("WARNING: skipping network "
25604350Std153743 				    "interface '%s' which may not be "
25614350Std153743 				    "present/plumbed in the global "
25624350Std153743 				    "zone.\n"),
25633448Sdh155122 				    nwiftab.zone_nwif_physical);
25643448Sdh155122 			}
25653448Sdh155122 			break;
25663448Sdh155122 		case ZS_EXCLUSIVE:
25673448Sdh155122 			/* Warning if it exists for either IPv4 or IPv6 */
25683448Sdh155122 
25693448Sdh155122 			if (zonecfg_ifname_exists(AF_INET,
25703448Sdh155122 			    nwiftab.zone_nwif_physical) ||
25713448Sdh155122 			    zonecfg_ifname_exists(AF_INET6,
25723448Sdh155122 			    nwiftab.zone_nwif_physical)) {
25733448Sdh155122 				(void) fprintf(stderr,
25743448Sdh155122 				    gettext("WARNING: skipping network "
25753448Sdh155122 				    "interface '%s' which is used in the "
25763448Sdh155122 				    "global zone.\n"),
25773448Sdh155122 				    nwiftab.zone_nwif_physical);
25783448Sdh155122 				break;
25793448Sdh155122 			}
25804456Sss150715 
25812611Svp157776 			/*
258210616SSebastien.Roy@Sun.COM 			 * Verify that the datalink exists and that it isn't
258310616SSebastien.Roy@Sun.COM 			 * already assigned to a zone.
25843448Sdh155122 			 */
258510616SSebastien.Roy@Sun.COM 			if ((status = dladm_open(&dh)) == DLADM_STATUS_OK) {
258610616SSebastien.Roy@Sun.COM 				status = dladm_name2info(dh,
258710616SSebastien.Roy@Sun.COM 				    nwiftab.zone_nwif_physical, &linkid, NULL,
258810616SSebastien.Roy@Sun.COM 				    NULL, NULL);
258910616SSebastien.Roy@Sun.COM 				dladm_close(dh);
259010616SSebastien.Roy@Sun.COM 			}
259110616SSebastien.Roy@Sun.COM 			if (status != DLADM_STATUS_OK) {
25923448Sdh155122 				(void) fprintf(stderr,
25933448Sdh155122 				    gettext("WARNING: skipping network "
259410616SSebastien.Roy@Sun.COM 				    "interface '%s': %s\n"),
25954456Sss150715 				    nwiftab.zone_nwif_physical,
259610616SSebastien.Roy@Sun.COM 				    dladm_status2str(status, errmsg));
25973448Sdh155122 				break;
25983448Sdh155122 			}
25993448Sdh155122 			dl_owner_zid = ALL_ZONES;
260010616SSebastien.Roy@Sun.COM 			if (zone_check_datalink(&dl_owner_zid, linkid) != 0)
26013448Sdh155122 				break;
26023448Sdh155122 
26033448Sdh155122 			/*
26043448Sdh155122 			 * If the zone being verified is
26053448Sdh155122 			 * running and owns the interface
26063448Sdh155122 			 */
26073448Sdh155122 			target_zid = getzoneidbyname(target_zone);
26083448Sdh155122 			if (target_zid == dl_owner_zid)
26093448Sdh155122 				break;
26103448Sdh155122 
26113448Sdh155122 			/* Zone id match failed, use name to check */
26123448Sdh155122 			if (getzonenamebyid(dl_owner_zid, dl_owner_zname,
26133448Sdh155122 			    ZONENAME_MAX) < 0) {
26143448Sdh155122 				/* No name, show ID instead */
26153448Sdh155122 				(void) snprintf(dl_owner_zname, ZONENAME_MAX,
26163448Sdh155122 				    "<%d>", dl_owner_zid);
26173448Sdh155122 			} else if (strcmp(dl_owner_zname, target_zone) == 0)
26183448Sdh155122 				break;
26193448Sdh155122 
26203448Sdh155122 			/*
26213448Sdh155122 			 * Note here we only report a warning that
26223448Sdh155122 			 * the interface is already in use by another
26233448Sdh155122 			 * running zone, and the verify process just
26243448Sdh155122 			 * goes on, if the interface is still in use
26253448Sdh155122 			 * when this zone really boots up, zoneadmd
26263448Sdh155122 			 * will find it. If the name of the zone which
26273448Sdh155122 			 * owns this interface cannot be determined,
26283448Sdh155122 			 * then it is not possible to determine if there
26293448Sdh155122 			 * is a conflict so just report it as a warning.
26302611Svp157776 			 */
26312611Svp157776 			(void) fprintf(stderr,
26323448Sdh155122 			    gettext("WARNING: skipping network interface "
26333448Sdh155122 			    "'%s' which is used by the non-global zone "
26343448Sdh155122 			    "'%s'.\n"), nwiftab.zone_nwif_physical,
26353448Sdh155122 			    dl_owner_zname);
26363448Sdh155122 			break;
26370Sstevel@tonic-gate 		}
26380Sstevel@tonic-gate 	}
26390Sstevel@tonic-gate 	(void) zonecfg_endnwifent(handle);
2640766Scarlsonj no_net:
26410Sstevel@tonic-gate 
26421931Sgjelinek 	/* verify that lofs has not been excluded from the kernel */
26432078Sgjelinek 	if (!(cmd_num == CMD_DETACH || cmd_num == CMD_ATTACH ||
26442078Sgjelinek 	    cmd_num == CMD_MOVE || cmd_num == CMD_CLONE) &&
26452078Sgjelinek 	    modctl(MODLOAD, 1, "fs/lofs", NULL) != 0) {
26461931Sgjelinek 		if (errno == ENXIO)
26471931Sgjelinek 			(void) fprintf(stderr, gettext("could not verify "
26481931Sgjelinek 			    "lofs(7FS): possibly excluded in /etc/system\n"));
26491931Sgjelinek 		else
26501931Sgjelinek 			(void) fprintf(stderr, gettext("could not verify "
26511931Sgjelinek 			    "lofs(7FS): %s\n"), strerror(errno));
26521931Sgjelinek 		return_code = Z_ERR;
26531931Sgjelinek 	}
26541931Sgjelinek 
26550Sstevel@tonic-gate 	if (verify_filesystems(handle) != Z_OK)
26560Sstevel@tonic-gate 		return_code = Z_ERR;
2657766Scarlsonj 	if (!in_alt_root && verify_rctls(handle) != Z_OK)
26580Sstevel@tonic-gate 		return_code = Z_ERR;
2659766Scarlsonj 	if (!in_alt_root && verify_pool(handle) != Z_OK)
26600Sstevel@tonic-gate 		return_code = Z_ERR;
26613339Szt129084 	if (!in_alt_root && verify_brand(handle, cmd_num, argv) != Z_OK)
26622712Snn35248 		return_code = Z_ERR;
2663789Sahrens 	if (!in_alt_root && verify_datasets(handle) != Z_OK)
2664789Sahrens 		return_code = Z_ERR;
26651645Scomay 
26661645Scomay 	/*
26671645Scomay 	 * As the "mount" command is used for patching/upgrading of zones
26681645Scomay 	 * or other maintenance processes, the zone's privilege set is not
26691645Scomay 	 * checked in this case.  Instead, the default, safe set of
26701645Scomay 	 * privileges will be used when this zone is created in the
26711645Scomay 	 * kernel.
26721645Scomay 	 */
26731645Scomay 	if (!in_alt_root && cmd_num != CMD_MOUNT &&
26741645Scomay 	    verify_limitpriv(handle) != Z_OK)
26751645Scomay 		return_code = Z_ERR;
26762078Sgjelinek 
26772078Sgjelinek 	return (return_code);
26782078Sgjelinek }
26792078Sgjelinek 
26802078Sgjelinek static int
verify_details(int cmd_num,char * argv[])26813339Szt129084 verify_details(int cmd_num, char *argv[])
26822078Sgjelinek {
26832078Sgjelinek 	zone_dochandle_t handle;
26842078Sgjelinek 	char zonepath[MAXPATHLEN], checkpath[MAXPATHLEN];
26852078Sgjelinek 	int return_code = Z_OK;
26862078Sgjelinek 	int err;
26872078Sgjelinek 
26882078Sgjelinek 	if ((handle = zonecfg_init_handle()) == NULL) {
26892078Sgjelinek 		zperror(cmd_to_str(cmd_num), B_TRUE);
26902078Sgjelinek 		return (Z_ERR);
26912078Sgjelinek 	}
26922078Sgjelinek 	if ((err = zonecfg_get_handle(target_zone, handle)) != Z_OK) {
26932078Sgjelinek 		errno = err;
26942078Sgjelinek 		zperror(cmd_to_str(cmd_num), B_TRUE);
26952078Sgjelinek 		zonecfg_fini_handle(handle);
26962078Sgjelinek 		return (Z_ERR);
26972078Sgjelinek 	}
26982078Sgjelinek 	if ((err = zonecfg_get_zonepath(handle, zonepath, sizeof (zonepath))) !=
26992078Sgjelinek 	    Z_OK) {
27002078Sgjelinek 		errno = err;
27012078Sgjelinek 		zperror(cmd_to_str(cmd_num), B_TRUE);
27022078Sgjelinek 		zonecfg_fini_handle(handle);
27032078Sgjelinek 		return (Z_ERR);
27042078Sgjelinek 	}
27052078Sgjelinek 	/*
27062078Sgjelinek 	 * zonecfg_get_zonepath() gets its data from the XML repository.
27072078Sgjelinek 	 * Verify this against the index file, which is checked first by
27082078Sgjelinek 	 * zone_get_zonepath().  If they don't match, bail out.
27092078Sgjelinek 	 */
27102078Sgjelinek 	if ((err = zone_get_zonepath(target_zone, checkpath,
27112078Sgjelinek 	    sizeof (checkpath))) != Z_OK) {
27122078Sgjelinek 		errno = err;
27132078Sgjelinek 		zperror2(target_zone, gettext("could not get zone path"));
27143716Sgjelinek 		zonecfg_fini_handle(handle);
27152078Sgjelinek 		return (Z_ERR);
27162078Sgjelinek 	}
27172078Sgjelinek 	if (strcmp(zonepath, checkpath) != 0) {
27182078Sgjelinek 		/*
27192078Sgjelinek 		 * TRANSLATION_NOTE
27202078Sgjelinek 		 * XML and zonepath are literals that should not be translated.
27212078Sgjelinek 		 */
27222078Sgjelinek 		(void) fprintf(stderr, gettext("The XML repository has "
27232078Sgjelinek 		    "zonepath '%s',\nbut the index file has zonepath '%s'.\n"
27242078Sgjelinek 		    "These must match, so fix the incorrect entry.\n"),
27252078Sgjelinek 		    zonepath, checkpath);
27263716Sgjelinek 		zonecfg_fini_handle(handle);
27272078Sgjelinek 		return (Z_ERR);
27282078Sgjelinek 	}
27299128Sgerald.jelinek@sun.com 	if (cmd_num != CMD_ATTACH &&
27309128Sgerald.jelinek@sun.com 	    validate_zonepath(zonepath, cmd_num) != Z_OK) {
27312078Sgjelinek 		(void) fprintf(stderr, gettext("could not verify zonepath %s "
27322078Sgjelinek 		    "because of the above errors.\n"), zonepath);
27332078Sgjelinek 		return_code = Z_ERR;
27342078Sgjelinek 	}
27352078Sgjelinek 
27363339Szt129084 	if (verify_handle(cmd_num, handle, argv) != Z_OK)
27372078Sgjelinek 		return_code = Z_ERR;
27382078Sgjelinek 
27390Sstevel@tonic-gate 	zonecfg_fini_handle(handle);
27400Sstevel@tonic-gate 	if (return_code == Z_ERR)
27410Sstevel@tonic-gate 		(void) fprintf(stderr,
27420Sstevel@tonic-gate 		    gettext("%s: zone %s failed to verify\n"),
27430Sstevel@tonic-gate 		    execname, target_zone);
27440Sstevel@tonic-gate 	return (return_code);
27450Sstevel@tonic-gate }
27460Sstevel@tonic-gate 
27470Sstevel@tonic-gate static int
verify_func(int argc,char * argv[])27480Sstevel@tonic-gate verify_func(int argc, char *argv[])
27490Sstevel@tonic-gate {
27500Sstevel@tonic-gate 	int arg;
27510Sstevel@tonic-gate 
27520Sstevel@tonic-gate 	optind = 0;
27530Sstevel@tonic-gate 	if ((arg = getopt(argc, argv, "?")) != EOF) {
27540Sstevel@tonic-gate 		switch (arg) {
27550Sstevel@tonic-gate 		case '?':
27560Sstevel@tonic-gate 			sub_usage(SHELP_VERIFY, CMD_VERIFY);
27570Sstevel@tonic-gate 			return (optopt == '?' ? Z_OK : Z_USAGE);
27580Sstevel@tonic-gate 		default:
27590Sstevel@tonic-gate 			sub_usage(SHELP_VERIFY, CMD_VERIFY);
27600Sstevel@tonic-gate 			return (Z_USAGE);
27610Sstevel@tonic-gate 		}
27620Sstevel@tonic-gate 	}
27630Sstevel@tonic-gate 	if (argc > optind) {
27640Sstevel@tonic-gate 		sub_usage(SHELP_VERIFY, CMD_VERIFY);
27650Sstevel@tonic-gate 		return (Z_USAGE);
27660Sstevel@tonic-gate 	}
27672712Snn35248 	if (sanity_check(target_zone, CMD_VERIFY, B_FALSE, B_FALSE, B_FALSE)
27682712Snn35248 	    != Z_OK)
27690Sstevel@tonic-gate 		return (Z_ERR);
27703339Szt129084 	return (verify_details(CMD_VERIFY, argv));
27710Sstevel@tonic-gate }
27720Sstevel@tonic-gate 
27732712Snn35248 static int
addoptions(char * buf,char * argv[],size_t len)27747089Sgjelinek addoptions(char *buf, char *argv[], size_t len)
27757089Sgjelinek {
27767089Sgjelinek 	int i = 0;
27777089Sgjelinek 
27787089Sgjelinek 	if (buf[0] == '\0')
27797089Sgjelinek 		return (Z_OK);
27807089Sgjelinek 
27817089Sgjelinek 	while (argv[i] != NULL) {
27827089Sgjelinek 		if (strlcat(buf, " ", len) >= len ||
27837089Sgjelinek 		    strlcat(buf, argv[i++], len) >= len) {
27847089Sgjelinek 			zerror("Command line too long");
27857089Sgjelinek 			return (Z_ERR);
27867089Sgjelinek 		}
27877089Sgjelinek 	}
27887089Sgjelinek 
27897089Sgjelinek 	return (Z_OK);
27907089Sgjelinek }
27917089Sgjelinek 
27927089Sgjelinek static int
addopt(char * buf,int opt,char * optarg,size_t bufsize)27932712Snn35248 addopt(char *buf, int opt, char *optarg, size_t bufsize)
27942712Snn35248 {
27952712Snn35248 	char optstring[4];
27962712Snn35248 
27972712Snn35248 	if (opt > 0)
27982712Snn35248 		(void) sprintf(optstring, " -%c", opt);
27992712Snn35248 	else
28002712Snn35248 		(void) strcpy(optstring, " ");
28012712Snn35248 
28022712Snn35248 	if ((strlcat(buf, optstring, bufsize) > bufsize))
28032712Snn35248 		return (Z_ERR);
28047089Sgjelinek 
28052712Snn35248 	if ((optarg != NULL) && (strlcat(buf, optarg, bufsize) > bufsize))
28062712Snn35248 		return (Z_ERR);
28077089Sgjelinek 
28082712Snn35248 	return (Z_OK);
28092712Snn35248 }
28100Sstevel@tonic-gate 
28117089Sgjelinek /* ARGSUSED */
28120Sstevel@tonic-gate static int
install_func(int argc,char * argv[])28130Sstevel@tonic-gate install_func(int argc, char *argv[])
28140Sstevel@tonic-gate {
28152712Snn35248 	char cmdbuf[MAXPATHLEN];
28164586Sgjelinek 	char postcmdbuf[MAXPATHLEN];
28170Sstevel@tonic-gate 	int lockfd;
28182712Snn35248 	int arg, err, subproc_err;
28190Sstevel@tonic-gate 	char zonepath[MAXPATHLEN];
28202727Sedp 	brand_handle_t bh = NULL;
28210Sstevel@tonic-gate 	int status;
28224586Sgjelinek 	boolean_t do_postinstall = B_FALSE;
28237089Sgjelinek 	boolean_t brand_help = B_FALSE;
28242712Snn35248 	char opts[128];
28252712Snn35248 
28262712Snn35248 	if (target_zone == NULL) {
28272712Snn35248 		sub_usage(SHELP_INSTALL, CMD_INSTALL);
28282712Snn35248 		return (Z_USAGE);
28292712Snn35248 	}
28300Sstevel@tonic-gate 
2831766Scarlsonj 	if (zonecfg_in_alt_root()) {
2832766Scarlsonj 		zerror(gettext("cannot install zone in alternate root"));
2833766Scarlsonj 		return (Z_ERR);
2834766Scarlsonj 	}
2835766Scarlsonj 
28362712Snn35248 	if ((err = zone_get_zonepath(target_zone, zonepath,
28372712Snn35248 	    sizeof (zonepath))) != Z_OK) {
28382712Snn35248 		errno = err;
28392712Snn35248 		zperror2(target_zone, gettext("could not get zone path"));
28402712Snn35248 		return (Z_ERR);
28412712Snn35248 	}
28422712Snn35248 
28432712Snn35248 	/* Fetch the install command from the brand configuration.  */
28442727Sedp 	if ((bh = brand_open(target_brand)) == NULL) {
28452712Snn35248 		zerror(gettext("missing or invalid brand"));
28462712Snn35248 		return (Z_ERR);
28472712Snn35248 	}
28482712Snn35248 
28497089Sgjelinek 	if (get_hook(bh, cmdbuf, sizeof (cmdbuf), brand_get_install,
28507089Sgjelinek 	    target_zone, zonepath) != Z_OK) {
28512712Snn35248 		zerror("invalid brand configuration: missing install resource");
28522727Sedp 		brand_close(bh);
28532712Snn35248 		return (Z_ERR);
28542712Snn35248 	}
28552712Snn35248 
28567089Sgjelinek 	if (get_hook(bh, postcmdbuf, sizeof (postcmdbuf), brand_get_postinstall,
28577089Sgjelinek 	    target_zone, zonepath) != Z_OK) {
28584586Sgjelinek 		zerror("invalid brand configuration: missing postinstall "
28594586Sgjelinek 		    "resource");
28604586Sgjelinek 		brand_close(bh);
28614586Sgjelinek 		return (Z_ERR);
28627089Sgjelinek 	}
28637089Sgjelinek 
28647089Sgjelinek 	if (postcmdbuf[0] != '\0')
28654586Sgjelinek 		do_postinstall = B_TRUE;
28664586Sgjelinek 
28672712Snn35248 	(void) strcpy(opts, "?x:");
28687089Sgjelinek 	/*
28697089Sgjelinek 	 * Fetch the list of recognized command-line options from
28707089Sgjelinek 	 * the brand configuration file.
28717089Sgjelinek 	 */
28727089Sgjelinek 	if (brand_get_installopts(bh, opts + strlen(opts),
28737089Sgjelinek 	    sizeof (opts) - strlen(opts)) != 0) {
28747089Sgjelinek 		zerror("invalid brand configuration: missing "
28757089Sgjelinek 		    "install options resource");
28767089Sgjelinek 		brand_close(bh);
28777089Sgjelinek 		return (Z_ERR);
28787089Sgjelinek 	}
28797089Sgjelinek 
28802727Sedp 	brand_close(bh);
28812712Snn35248 
28827089Sgjelinek 	if (cmdbuf[0] == '\0') {
28837089Sgjelinek 		zerror("Missing brand install command");
28847089Sgjelinek 		return (Z_ERR);
28857089Sgjelinek 	}
28867089Sgjelinek 
28877089Sgjelinek 	/* Check the argv string for args we handle internally */
28880Sstevel@tonic-gate 	optind = 0;
28897089Sgjelinek 	opterr = 0;
28902712Snn35248 	while ((arg = getopt(argc, argv, opts)) != EOF) {
28910Sstevel@tonic-gate 		switch (arg) {
28920Sstevel@tonic-gate 		case '?':
28937089Sgjelinek 			if (optopt == '?') {
28941867Sgjelinek 				sub_usage(SHELP_INSTALL, CMD_INSTALL);
28957089Sgjelinek 				brand_help = B_TRUE;
28962712Snn35248 			}
28977089Sgjelinek 			/* Ignore unknown options - may be brand specific. */
28987089Sgjelinek 			break;
28997089Sgjelinek 		default:
29007089Sgjelinek 			/* Ignore unknown options - may be brand specific. */
29012712Snn35248 			break;
29020Sstevel@tonic-gate 		}
29037089Sgjelinek 
29047089Sgjelinek 		/*
29057089Sgjelinek 		 * Append the option to the command line passed to the
29067089Sgjelinek 		 * brand-specific install and postinstall routines.
29077089Sgjelinek 		 */
29087089Sgjelinek 		if (addopt(cmdbuf, optopt, optarg, sizeof (cmdbuf)) != Z_OK) {
29097089Sgjelinek 			zerror("Install command line too long");
29107089Sgjelinek 			return (Z_ERR);
29117089Sgjelinek 		}
29127089Sgjelinek 		if (addopt(postcmdbuf, optopt, optarg, sizeof (postcmdbuf))
29137089Sgjelinek 		    != Z_OK) {
29147089Sgjelinek 			zerror("Post-Install command line too long");
29157089Sgjelinek 			return (Z_ERR);
29167089Sgjelinek 		}
29177089Sgjelinek 	}
29187089Sgjelinek 
29197089Sgjelinek 	for (; optind < argc; optind++) {
29207089Sgjelinek 		if (addopt(cmdbuf, 0, argv[optind], sizeof (cmdbuf)) != Z_OK) {
29217089Sgjelinek 			zerror("Install command line too long");
29227089Sgjelinek 			return (Z_ERR);
29237089Sgjelinek 		}
29247089Sgjelinek 
29257089Sgjelinek 		if (addopt(postcmdbuf, 0, argv[optind], sizeof (postcmdbuf))
29267089Sgjelinek 		    != Z_OK) {
29277089Sgjelinek 			zerror("Post-Install command line too long");
29287089Sgjelinek 			return (Z_ERR);
29292712Snn35248 		}
29302712Snn35248 	}
29312712Snn35248 
29327089Sgjelinek 	if (!brand_help) {
29337089Sgjelinek 		if (sanity_check(target_zone, CMD_INSTALL, B_FALSE, B_TRUE,
29347089Sgjelinek 		    B_FALSE) != Z_OK)
29357089Sgjelinek 			return (Z_ERR);
29367089Sgjelinek 		if (verify_details(CMD_INSTALL, argv) != Z_OK)
29377089Sgjelinek 			return (Z_ERR);
29387089Sgjelinek 
29397089Sgjelinek 		if (zonecfg_grab_lock_file(target_zone, &lockfd) != Z_OK) {
29407089Sgjelinek 			zerror(gettext("another %s may have an operation in "
29417089Sgjelinek 			    "progress."), "zoneadm");
29427089Sgjelinek 			return (Z_ERR);
29437089Sgjelinek 		}
29447089Sgjelinek 		err = zone_set_state(target_zone, ZONE_STATE_INCOMPLETE);
29457089Sgjelinek 		if (err != Z_OK) {
29467089Sgjelinek 			errno = err;
29477089Sgjelinek 			zperror2(target_zone, gettext("could not set state"));
29487089Sgjelinek 			goto done;
29497089Sgjelinek 		}
29507089Sgjelinek 
295112110SFrank.Batschulat@Sun.COM 		create_zfs_zonepath(zonepath);
29527089Sgjelinek 	}
29537089Sgjelinek 
29549310Sgerald.jelinek@sun.com 	status = do_subproc(cmdbuf);
29552712Snn35248 	if ((subproc_err =
29562712Snn35248 	    subproc_status(gettext("brand-specific installation"), status,
29572712Snn35248 	    B_FALSE)) != ZONE_SUBPROC_OK) {
29587089Sgjelinek 		if (subproc_err == ZONE_SUBPROC_USAGE && !brand_help) {
29597089Sgjelinek 			sub_usage(SHELP_INSTALL, CMD_INSTALL);
29607089Sgjelinek 			zonecfg_release_lock_file(target_zone, lockfd);
29617089Sgjelinek 			return (Z_ERR);
29627089Sgjelinek 		}
296312110SFrank.Batschulat@Sun.COM 		errno = subproc_err;
29642712Snn35248 		err = Z_ERR;
29650Sstevel@tonic-gate 		goto done;
29662712Snn35248 	}
29670Sstevel@tonic-gate 
29687089Sgjelinek 	if (brand_help)
29697089Sgjelinek 		return (Z_OK);
29707089Sgjelinek 
29710Sstevel@tonic-gate 	if ((err = zone_set_state(target_zone, ZONE_STATE_INSTALLED)) != Z_OK) {
29720Sstevel@tonic-gate 		errno = err;
29730Sstevel@tonic-gate 		zperror2(target_zone, gettext("could not set state"));
29740Sstevel@tonic-gate 		goto done;
29750Sstevel@tonic-gate 	}
29760Sstevel@tonic-gate 
29774586Sgjelinek 	if (do_postinstall) {
29784586Sgjelinek 		status = do_subproc(postcmdbuf);
29794586Sgjelinek 
29804586Sgjelinek 		if ((subproc_err =
29814586Sgjelinek 		    subproc_status(gettext("brand-specific post-install"),
29824586Sgjelinek 		    status, B_FALSE)) != ZONE_SUBPROC_OK) {
298312110SFrank.Batschulat@Sun.COM 			errno = subproc_err;
29844586Sgjelinek 			err = Z_ERR;
29854586Sgjelinek 			(void) zone_set_state(target_zone,
29864586Sgjelinek 			    ZONE_STATE_INCOMPLETE);
29874586Sgjelinek 		}
29884586Sgjelinek 	}
29894586Sgjelinek 
29900Sstevel@tonic-gate done:
29912712Snn35248 	/*
29927089Sgjelinek 	 * If the install script exited with ZONE_SUBPROC_NOTCOMPLETE, try to
29937089Sgjelinek 	 * clean up the zone and leave the zone in the CONFIGURED state so that
29947089Sgjelinek 	 * another install can be attempted without requiring an uninstall
29957089Sgjelinek 	 * first.
29962712Snn35248 	 */
29977089Sgjelinek 	if (subproc_err == ZONE_SUBPROC_NOTCOMPLETE) {
299812110SFrank.Batschulat@Sun.COM 		int temp_err;
299912110SFrank.Batschulat@Sun.COM 
300012110SFrank.Batschulat@Sun.COM 		if ((temp_err = cleanup_zonepath(zonepath, B_FALSE)) != Z_OK) {
300112110SFrank.Batschulat@Sun.COM 			errno = err = temp_err;
30022712Snn35248 			zperror2(target_zone,
30032712Snn35248 			    gettext("cleaning up zonepath failed"));
300412110SFrank.Batschulat@Sun.COM 		} else if ((temp_err = zone_set_state(target_zone,
30052712Snn35248 		    ZONE_STATE_CONFIGURED)) != Z_OK) {
300612110SFrank.Batschulat@Sun.COM 			errno = err = temp_err;
30072712Snn35248 			zperror2(target_zone, gettext("could not set state"));
30082712Snn35248 		}
30092712Snn35248 	}
30102712Snn35248 
30117089Sgjelinek 	if (!brand_help)
30127089Sgjelinek 		zonecfg_release_lock_file(target_zone, lockfd);
30130Sstevel@tonic-gate 	return ((err == Z_OK) ? Z_OK : Z_ERR);
30140Sstevel@tonic-gate }
30150Sstevel@tonic-gate 
30161300Sgjelinek static void
warn_dev_match(zone_dochandle_t s_handle,char * source_zone,zone_dochandle_t t_handle,char * target_zone)30171300Sgjelinek warn_dev_match(zone_dochandle_t s_handle, char *source_zone,
30181300Sgjelinek 	zone_dochandle_t t_handle, char *target_zone)
30191300Sgjelinek {
30201300Sgjelinek 	int err;
30211300Sgjelinek 	struct zone_devtab s_devtab;
30221300Sgjelinek 	struct zone_devtab t_devtab;
30231300Sgjelinek 
30241300Sgjelinek 	if ((err = zonecfg_setdevent(t_handle)) != Z_OK) {
30251300Sgjelinek 		errno = err;
30261300Sgjelinek 		zperror2(target_zone, gettext("could not enumerate devices"));
30271300Sgjelinek 		return;
30281300Sgjelinek 	}
30291300Sgjelinek 
30301300Sgjelinek 	while (zonecfg_getdevent(t_handle, &t_devtab) == Z_OK) {
30311300Sgjelinek 		if ((err = zonecfg_setdevent(s_handle)) != Z_OK) {
30321300Sgjelinek 			errno = err;
30331300Sgjelinek 			zperror2(source_zone,
30341300Sgjelinek 			    gettext("could not enumerate devices"));
30351300Sgjelinek 			(void) zonecfg_enddevent(t_handle);
30361300Sgjelinek 			return;
30371300Sgjelinek 		}
30381300Sgjelinek 
30391300Sgjelinek 		while (zonecfg_getdevent(s_handle, &s_devtab) == Z_OK) {
30401300Sgjelinek 			/*
30411300Sgjelinek 			 * Use fnmatch to catch the case where wildcards
30421300Sgjelinek 			 * were used in one zone and the other has an
30431300Sgjelinek 			 * explicit entry (e.g. /dev/dsk/c0t0d0s6 vs.
30441300Sgjelinek 			 * /dev/\*dsk/c0t0d0s6).
30451300Sgjelinek 			 */
30461300Sgjelinek 			if (fnmatch(t_devtab.zone_dev_match,
30471300Sgjelinek 			    s_devtab.zone_dev_match, FNM_PATHNAME) == 0 ||
30481300Sgjelinek 			    fnmatch(s_devtab.zone_dev_match,
30491300Sgjelinek 			    t_devtab.zone_dev_match, FNM_PATHNAME) == 0) {
30501300Sgjelinek 				(void) fprintf(stderr,
30511300Sgjelinek 				    gettext("WARNING: device '%s' "
30521300Sgjelinek 				    "is configured in both zones.\n"),
30531300Sgjelinek 				    t_devtab.zone_dev_match);
30541300Sgjelinek 				break;
30551300Sgjelinek 			}
30561300Sgjelinek 		}
30571300Sgjelinek 		(void) zonecfg_enddevent(s_handle);
30581300Sgjelinek 	}
30591300Sgjelinek 
30601300Sgjelinek 	(void) zonecfg_enddevent(t_handle);
30611300Sgjelinek }
30621300Sgjelinek 
30631300Sgjelinek /*
30641300Sgjelinek  * Check if the specified mount option (opt) is contained within the
30651300Sgjelinek  * options string.
30661300Sgjelinek  */
30671300Sgjelinek static boolean_t
opt_match(char * opt,char * options)30681300Sgjelinek opt_match(char *opt, char *options)
30691300Sgjelinek {
30701300Sgjelinek 	char *p;
30711300Sgjelinek 	char *lastp;
30721300Sgjelinek 
30731300Sgjelinek 	if ((p = strtok_r(options, ",", &lastp)) != NULL) {
30741300Sgjelinek 		if (strcmp(p, opt) == 0)
30751300Sgjelinek 			return (B_TRUE);
30761300Sgjelinek 		while ((p = strtok_r(NULL, ",", &lastp)) != NULL) {
30771300Sgjelinek 			if (strcmp(p, opt) == 0)
30781300Sgjelinek 				return (B_TRUE);
30791300Sgjelinek 		}
30801300Sgjelinek 	}
30811300Sgjelinek 
30821300Sgjelinek 	return (B_FALSE);
30831300Sgjelinek }
30841300Sgjelinek 
30851867Sgjelinek #define	RW_LOFS	"WARNING: read-write lofs file system on '%s' is configured " \
30861300Sgjelinek 	"in both zones.\n"
30871300Sgjelinek 
30881300Sgjelinek static void
print_fs_warnings(struct zone_fstab * s_fstab,struct zone_fstab * t_fstab)30891300Sgjelinek print_fs_warnings(struct zone_fstab *s_fstab, struct zone_fstab *t_fstab)
30901300Sgjelinek {
30911300Sgjelinek 	/*
30921300Sgjelinek 	 * It is ok to have shared lofs mounted fs but we want to warn if
30931300Sgjelinek 	 * either is rw since this will effect the other zone.
30941300Sgjelinek 	 */
30951300Sgjelinek 	if (strcmp(t_fstab->zone_fs_type, "lofs") == 0) {
30961300Sgjelinek 		zone_fsopt_t *optp;
30971300Sgjelinek 
30981300Sgjelinek 		/* The default is rw so no options means rw */
30991300Sgjelinek 		if (t_fstab->zone_fs_options == NULL ||
31001300Sgjelinek 		    s_fstab->zone_fs_options == NULL) {
31011300Sgjelinek 			(void) fprintf(stderr, gettext(RW_LOFS),
31021300Sgjelinek 			    t_fstab->zone_fs_special);
31031300Sgjelinek 			return;
31041300Sgjelinek 		}
31051300Sgjelinek 
31061300Sgjelinek 		for (optp = s_fstab->zone_fs_options; optp != NULL;
31071300Sgjelinek 		    optp = optp->zone_fsopt_next) {
31081300Sgjelinek 			if (opt_match("rw", optp->zone_fsopt_opt)) {
31091300Sgjelinek 				(void) fprintf(stderr, gettext(RW_LOFS),
31101300Sgjelinek 				    s_fstab->zone_fs_special);
31111300Sgjelinek 				return;
31121300Sgjelinek 			}
31131300Sgjelinek 		}
31141300Sgjelinek 
31151300Sgjelinek 		for (optp = t_fstab->zone_fs_options; optp != NULL;
31161300Sgjelinek 		    optp = optp->zone_fsopt_next) {
31171300Sgjelinek 			if (opt_match("rw", optp->zone_fsopt_opt)) {
31181300Sgjelinek 				(void) fprintf(stderr, gettext(RW_LOFS),
31191300Sgjelinek 				    t_fstab->zone_fs_special);
31201300Sgjelinek 				return;
31211300Sgjelinek 			}
31221300Sgjelinek 		}
31231300Sgjelinek 
31241300Sgjelinek 		return;
31251300Sgjelinek 	}
31261300Sgjelinek 
31271300Sgjelinek 	/*
31281300Sgjelinek 	 * TRANSLATION_NOTE
31291867Sgjelinek 	 * The first variable is the file system type and the second is
31301867Sgjelinek 	 * the file system special device.  For example,
31311867Sgjelinek 	 * WARNING: ufs file system on '/dev/dsk/c0t0d0s0' ...
31321300Sgjelinek 	 */
31331867Sgjelinek 	(void) fprintf(stderr, gettext("WARNING: %s file system on '%s' "
31341300Sgjelinek 	    "is configured in both zones.\n"), t_fstab->zone_fs_type,
31351300Sgjelinek 	    t_fstab->zone_fs_special);
31361300Sgjelinek }
31371300Sgjelinek 
31381300Sgjelinek static void
warn_fs_match(zone_dochandle_t s_handle,char * source_zone,zone_dochandle_t t_handle,char * target_zone)31391300Sgjelinek warn_fs_match(zone_dochandle_t s_handle, char *source_zone,
31401300Sgjelinek 	zone_dochandle_t t_handle, char *target_zone)
31411300Sgjelinek {
31421300Sgjelinek 	int err;
31431300Sgjelinek 	struct zone_fstab s_fstab;
31441300Sgjelinek 	struct zone_fstab t_fstab;
31451300Sgjelinek 
31461300Sgjelinek 	if ((err = zonecfg_setfsent(t_handle)) != Z_OK) {
31471300Sgjelinek 		errno = err;
31481300Sgjelinek 		zperror2(target_zone,
31491867Sgjelinek 		    gettext("could not enumerate file systems"));
31501300Sgjelinek 		return;
31511300Sgjelinek 	}
31521300Sgjelinek 
31531300Sgjelinek 	while (zonecfg_getfsent(t_handle, &t_fstab) == Z_OK) {
31541300Sgjelinek 		if ((err = zonecfg_setfsent(s_handle)) != Z_OK) {
31551300Sgjelinek 			errno = err;
31561300Sgjelinek 			zperror2(source_zone,
31571867Sgjelinek 			    gettext("could not enumerate file systems"));
31581300Sgjelinek 			(void) zonecfg_endfsent(t_handle);
31591300Sgjelinek 			return;
31601300Sgjelinek 		}
31611300Sgjelinek 
31621300Sgjelinek 		while (zonecfg_getfsent(s_handle, &s_fstab) == Z_OK) {
31631300Sgjelinek 			if (strcmp(t_fstab.zone_fs_special,
31641300Sgjelinek 			    s_fstab.zone_fs_special) == 0) {
31651300Sgjelinek 				print_fs_warnings(&s_fstab, &t_fstab);
31661300Sgjelinek 				break;
31671300Sgjelinek 			}
31681300Sgjelinek 		}
31691300Sgjelinek 		(void) zonecfg_endfsent(s_handle);
31701300Sgjelinek 	}
31711300Sgjelinek 
31721300Sgjelinek 	(void) zonecfg_endfsent(t_handle);
31731300Sgjelinek }
31741300Sgjelinek 
31751300Sgjelinek /*
31761300Sgjelinek  * We don't catch the case where you used the same IP address but
31771300Sgjelinek  * it is not an exact string match.  For example, 192.9.0.128 vs. 192.09.0.128.
31781300Sgjelinek  * However, we're not going to worry about that but we will check for
31791300Sgjelinek  * a possible netmask on one of the addresses (e.g. 10.0.0.1 and 10.0.0.1/24)
31801300Sgjelinek  * and handle that case as a match.
31811300Sgjelinek  */
31821300Sgjelinek static void
warn_ip_match(zone_dochandle_t s_handle,char * source_zone,zone_dochandle_t t_handle,char * target_zone)31831300Sgjelinek warn_ip_match(zone_dochandle_t s_handle, char *source_zone,
31841300Sgjelinek 	zone_dochandle_t t_handle, char *target_zone)
31851300Sgjelinek {
31861300Sgjelinek 	int err;
31871300Sgjelinek 	struct zone_nwiftab s_nwiftab;
31881300Sgjelinek 	struct zone_nwiftab t_nwiftab;
31891300Sgjelinek 
31901300Sgjelinek 	if ((err = zonecfg_setnwifent(t_handle)) != Z_OK) {
31911300Sgjelinek 		errno = err;
31921300Sgjelinek 		zperror2(target_zone,
31931300Sgjelinek 		    gettext("could not enumerate network interfaces"));
31941300Sgjelinek 		return;
31951300Sgjelinek 	}
31961300Sgjelinek 
31971300Sgjelinek 	while (zonecfg_getnwifent(t_handle, &t_nwiftab) == Z_OK) {
31981300Sgjelinek 		char *p;
31991300Sgjelinek 
32001300Sgjelinek 		/* remove an (optional) netmask from the address */
32011300Sgjelinek 		if ((p = strchr(t_nwiftab.zone_nwif_address, '/')) != NULL)
32021300Sgjelinek 			*p = '\0';
32031300Sgjelinek 
32041300Sgjelinek 		if ((err = zonecfg_setnwifent(s_handle)) != Z_OK) {
32051300Sgjelinek 			errno = err;
32061300Sgjelinek 			zperror2(source_zone,
32071300Sgjelinek 			    gettext("could not enumerate network interfaces"));
32081300Sgjelinek 			(void) zonecfg_endnwifent(t_handle);
32091300Sgjelinek 			return;
32101300Sgjelinek 		}
32111300Sgjelinek 
32121300Sgjelinek 		while (zonecfg_getnwifent(s_handle, &s_nwiftab) == Z_OK) {
32131300Sgjelinek 			/* remove an (optional) netmask from the address */
32141300Sgjelinek 			if ((p = strchr(s_nwiftab.zone_nwif_address, '/'))
32151300Sgjelinek 			    != NULL)
32161300Sgjelinek 				*p = '\0';
32171300Sgjelinek 
32183448Sdh155122 			/* For exclusive-IP zones, address is not specified. */
32193448Sdh155122 			if (strlen(s_nwiftab.zone_nwif_address) == 0)
32203448Sdh155122 				continue;
32213448Sdh155122 
32221300Sgjelinek 			if (strcmp(t_nwiftab.zone_nwif_address,
32231300Sgjelinek 			    s_nwiftab.zone_nwif_address) == 0) {
32241300Sgjelinek 				(void) fprintf(stderr,
32251300Sgjelinek 				    gettext("WARNING: network address '%s' "
32261300Sgjelinek 				    "is configured in both zones.\n"),
32271300Sgjelinek 				    t_nwiftab.zone_nwif_address);
32281300Sgjelinek 				break;
32291300Sgjelinek 			}
32301300Sgjelinek 		}
32311300Sgjelinek 		(void) zonecfg_endnwifent(s_handle);
32321300Sgjelinek 	}
32331300Sgjelinek 
32341300Sgjelinek 	(void) zonecfg_endnwifent(t_handle);
32351300Sgjelinek }
32361300Sgjelinek 
32371300Sgjelinek static void
warn_dataset_match(zone_dochandle_t s_handle,char * source,zone_dochandle_t t_handle,char * target)32382712Snn35248 warn_dataset_match(zone_dochandle_t s_handle, char *source,
32392712Snn35248 	zone_dochandle_t t_handle, char *target)
32401300Sgjelinek {
32411300Sgjelinek 	int err;
32421300Sgjelinek 	struct zone_dstab s_dstab;
32431300Sgjelinek 	struct zone_dstab t_dstab;
32441300Sgjelinek 
32451300Sgjelinek 	if ((err = zonecfg_setdsent(t_handle)) != Z_OK) {
32461300Sgjelinek 		errno = err;
32472712Snn35248 		zperror2(target, gettext("could not enumerate datasets"));
32481300Sgjelinek 		return;
32491300Sgjelinek 	}
32501300Sgjelinek 
32511300Sgjelinek 	while (zonecfg_getdsent(t_handle, &t_dstab) == Z_OK) {
32521300Sgjelinek 		if ((err = zonecfg_setdsent(s_handle)) != Z_OK) {
32531300Sgjelinek 			errno = err;
32542712Snn35248 			zperror2(source,
32551300Sgjelinek 			    gettext("could not enumerate datasets"));
32561300Sgjelinek 			(void) zonecfg_enddsent(t_handle);
32571300Sgjelinek 			return;
32581300Sgjelinek 		}
32591300Sgjelinek 
32601300Sgjelinek 		while (zonecfg_getdsent(s_handle, &s_dstab) == Z_OK) {
32611300Sgjelinek 			if (strcmp(t_dstab.zone_dataset_name,
32621300Sgjelinek 			    s_dstab.zone_dataset_name) == 0) {
32632712Snn35248 				target_zone = source;
32642712Snn35248 				zerror(gettext("WARNING: dataset '%s' "
32651300Sgjelinek 				    "is configured in both zones.\n"),
32661300Sgjelinek 				    t_dstab.zone_dataset_name);
32671300Sgjelinek 				break;
32681300Sgjelinek 			}
32691300Sgjelinek 		}
32701300Sgjelinek 		(void) zonecfg_enddsent(s_handle);
32711300Sgjelinek 	}
32721300Sgjelinek 
32731300Sgjelinek 	(void) zonecfg_enddsent(t_handle);
32741300Sgjelinek }
32751300Sgjelinek 
32762712Snn35248 /*
32772712Snn35248  * Check that the clone and its source have the same brand type.
32782712Snn35248  */
32792712Snn35248 static int
valid_brand_clone(char * source_zone,char * target_zone)32802712Snn35248 valid_brand_clone(char *source_zone, char *target_zone)
32812712Snn35248 {
32822727Sedp 	brand_handle_t bh;
32832712Snn35248 	char source_brand[MAXNAMELEN];
32842712Snn35248 
32852712Snn35248 	if ((zone_get_brand(source_zone, source_brand,
32862712Snn35248 	    sizeof (source_brand))) != Z_OK) {
32872712Snn35248 		(void) fprintf(stderr, "%s: zone '%s': %s\n",
32882712Snn35248 		    execname, source_zone, gettext("missing or invalid brand"));
32892712Snn35248 		return (Z_ERR);
32902712Snn35248 	}
32912712Snn35248 
329212110SFrank.Batschulat@Sun.COM 	if (strcmp(source_brand, target_brand) != 0) {
32932712Snn35248 		(void) fprintf(stderr,
32942712Snn35248 		    gettext("%s: Zones '%s' and '%s' have different brand "
32952712Snn35248 		    "types.\n"), execname, source_zone, target_zone);
32962712Snn35248 		return (Z_ERR);
32972712Snn35248 	}
32982712Snn35248 
32992727Sedp 	if ((bh = brand_open(target_brand)) == NULL) {
33002712Snn35248 		zerror(gettext("missing or invalid brand"));
33012712Snn35248 		return (Z_ERR);
33022712Snn35248 	}
33032727Sedp 	brand_close(bh);
33042712Snn35248 	return (Z_OK);
33052712Snn35248 }
33062712Snn35248 
33071300Sgjelinek static int
validate_clone(char * source_zone,char * target_zone)33081300Sgjelinek validate_clone(char *source_zone, char *target_zone)
33091300Sgjelinek {
33101300Sgjelinek 	int err = Z_OK;
33111300Sgjelinek 	zone_dochandle_t s_handle;
33121300Sgjelinek 	zone_dochandle_t t_handle;
33131300Sgjelinek 
33141300Sgjelinek 	if ((t_handle = zonecfg_init_handle()) == NULL) {
33151300Sgjelinek 		zperror(cmd_to_str(CMD_CLONE), B_TRUE);
33161300Sgjelinek 		return (Z_ERR);
33171300Sgjelinek 	}
33181300Sgjelinek 	if ((err = zonecfg_get_handle(target_zone, t_handle)) != Z_OK) {
33191300Sgjelinek 		errno = err;
33201300Sgjelinek 		zperror(cmd_to_str(CMD_CLONE), B_TRUE);
33211300Sgjelinek 		zonecfg_fini_handle(t_handle);
33221300Sgjelinek 		return (Z_ERR);
33231300Sgjelinek 	}
33241300Sgjelinek 
33251300Sgjelinek 	if ((s_handle = zonecfg_init_handle()) == NULL) {
33261300Sgjelinek 		zperror(cmd_to_str(CMD_CLONE), B_TRUE);
33271300Sgjelinek 		zonecfg_fini_handle(t_handle);
33281300Sgjelinek 		return (Z_ERR);
33291300Sgjelinek 	}
33301300Sgjelinek 	if ((err = zonecfg_get_handle(source_zone, s_handle)) != Z_OK) {
33311300Sgjelinek 		errno = err;
33321300Sgjelinek 		zperror(cmd_to_str(CMD_CLONE), B_TRUE);
33331300Sgjelinek 		goto done;
33341300Sgjelinek 	}
33351300Sgjelinek 
33362712Snn35248 	/* verify new zone has same brand type */
33372712Snn35248 	err = valid_brand_clone(source_zone, target_zone);
33382712Snn35248 	if (err != Z_OK)
33392712Snn35248 		goto done;
33402712Snn35248 
33411300Sgjelinek 	/* warn about imported fs's which are the same */
33421300Sgjelinek 	warn_fs_match(s_handle, source_zone, t_handle, target_zone);
33431300Sgjelinek 
33441300Sgjelinek 	/* warn about imported IP addresses which are the same */
33451300Sgjelinek 	warn_ip_match(s_handle, source_zone, t_handle, target_zone);
33461300Sgjelinek 
33471300Sgjelinek 	/* warn about imported devices which are the same */
33481300Sgjelinek 	warn_dev_match(s_handle, source_zone, t_handle, target_zone);
33491300Sgjelinek 
33501300Sgjelinek 	/* warn about imported datasets which are the same */
33511300Sgjelinek 	warn_dataset_match(s_handle, source_zone, t_handle, target_zone);
33521300Sgjelinek 
33531300Sgjelinek done:
33541300Sgjelinek 	zonecfg_fini_handle(t_handle);
33551300Sgjelinek 	zonecfg_fini_handle(s_handle);
33561300Sgjelinek 
33571300Sgjelinek 	return ((err == Z_OK) ? Z_OK : Z_ERR);
33581300Sgjelinek }
33591300Sgjelinek 
33601300Sgjelinek static int
copy_zone(char * src,char * dst)33611300Sgjelinek copy_zone(char *src, char *dst)
33621300Sgjelinek {
33631300Sgjelinek 	boolean_t out_null = B_FALSE;
33641300Sgjelinek 	int status;
33651300Sgjelinek 	char *outfile;
33661300Sgjelinek 	char cmdbuf[MAXPATHLEN * 2 + 128];
33671300Sgjelinek 
33681300Sgjelinek 	if ((outfile = tempnam("/var/log", "zone")) == NULL) {
33691300Sgjelinek 		outfile = "/dev/null";
33701300Sgjelinek 		out_null = B_TRUE;
33711300Sgjelinek 	}
33721300Sgjelinek 
33731867Sgjelinek 	/*
33741867Sgjelinek 	 * Use find to get the list of files to copy.  We need to skip
33751867Sgjelinek 	 * files of type "socket" since cpio can't handle those but that
33761867Sgjelinek 	 * should be ok since the app will recreate the socket when it runs.
33771867Sgjelinek 	 * We also need to filter out anything under the .zfs subdir.  Since
33781867Sgjelinek 	 * find is running depth-first, we need the extra egrep to filter .zfs.
33791867Sgjelinek 	 */
33801300Sgjelinek 	(void) snprintf(cmdbuf, sizeof (cmdbuf),
33811867Sgjelinek 	    "cd %s && /usr/bin/find . -type s -prune -o -depth -print | "
33821607Sgjelinek 	    "/usr/bin/egrep -v '^\\./\\.zfs$|^\\./\\.zfs/' | "
33831300Sgjelinek 	    "/usr/bin/cpio -pdmuP@ %s > %s 2>&1",
33841300Sgjelinek 	    src, dst, outfile);
33851300Sgjelinek 
33861300Sgjelinek 	status = do_subproc(cmdbuf);
33871300Sgjelinek 
33882712Snn35248 	if (subproc_status("copy", status, B_TRUE) != ZONE_SUBPROC_OK) {
33891300Sgjelinek 		if (!out_null)
33901300Sgjelinek 			(void) fprintf(stderr, gettext("\nThe copy failed.\n"
33911300Sgjelinek 			    "More information can be found in %s\n"), outfile);
33922712Snn35248 		return (Z_ERR);
33931300Sgjelinek 	}
33941300Sgjelinek 
33951300Sgjelinek 	if (!out_null)
33961300Sgjelinek 		(void) unlink(outfile);
33971300Sgjelinek 
33981300Sgjelinek 	return (Z_OK);
33991300Sgjelinek }
34001300Sgjelinek 
34011300Sgjelinek /* ARGSUSED */
340211276SJordan.Vaughan@Sun.com int
zfm_print(const struct mnttab * p,void * r)340311276SJordan.Vaughan@Sun.com zfm_print(const struct mnttab *p, void *r) {
340411276SJordan.Vaughan@Sun.com 	zerror("  %s\n", p->mnt_mountp);
34051300Sgjelinek 	return (0);
34061300Sgjelinek }
34071300Sgjelinek 
34081867Sgjelinek int
clone_copy(char * source_zonepath,char * zonepath)34091867Sgjelinek clone_copy(char *source_zonepath, char *zonepath)
34101867Sgjelinek {
34111867Sgjelinek 	int err;
34121867Sgjelinek 
34131867Sgjelinek 	/* Don't clone the zone if anything is still mounted there */
34141867Sgjelinek 	if (zonecfg_find_mounts(source_zonepath, NULL, NULL)) {
34151867Sgjelinek 		zerror(gettext("These file systems are mounted on "
34161867Sgjelinek 		    "subdirectories of %s.\n"), source_zonepath);
34171867Sgjelinek 		(void) zonecfg_find_mounts(source_zonepath, zfm_print, NULL);
34181867Sgjelinek 		return (Z_ERR);
34191867Sgjelinek 	}
34201867Sgjelinek 
34211867Sgjelinek 	/*
34221867Sgjelinek 	 * Attempt to create a ZFS fs for the zonepath.  As usual, we don't
34231867Sgjelinek 	 * care if this works or not since we always have the default behavior
34241867Sgjelinek 	 * of a simple directory for the zonepath.
34251867Sgjelinek 	 */
34261867Sgjelinek 	create_zfs_zonepath(zonepath);
34271867Sgjelinek 
34281867Sgjelinek 	(void) printf(gettext("Copying %s..."), source_zonepath);
34291867Sgjelinek 	(void) fflush(stdout);
34301867Sgjelinek 
34311867Sgjelinek 	err = copy_zone(source_zonepath, zonepath);
34321867Sgjelinek 
34331867Sgjelinek 	(void) printf("\n");
34341867Sgjelinek 
34351867Sgjelinek 	return (err);
34361867Sgjelinek }
34371867Sgjelinek 
34381300Sgjelinek static int
clone_func(int argc,char * argv[])34391300Sgjelinek clone_func(int argc, char *argv[])
34401300Sgjelinek {
34411300Sgjelinek 	char *source_zone = NULL;
34421300Sgjelinek 	int lockfd;
34431300Sgjelinek 	int err, arg;
34441300Sgjelinek 	char zonepath[MAXPATHLEN];
34451300Sgjelinek 	char source_zonepath[MAXPATHLEN];
34461300Sgjelinek 	zone_state_t state;
34471300Sgjelinek 	zone_entry_t *zent;
34481867Sgjelinek 	char *method = NULL;
34491867Sgjelinek 	char *snapshot = NULL;
34507089Sgjelinek 	char cmdbuf[MAXPATHLEN];
34517089Sgjelinek 	char postcmdbuf[MAXPATHLEN];
34527089Sgjelinek 	char presnapbuf[MAXPATHLEN];
34537089Sgjelinek 	char postsnapbuf[MAXPATHLEN];
34547089Sgjelinek 	char validsnapbuf[MAXPATHLEN];
34557089Sgjelinek 	brand_handle_t bh = NULL;
34567089Sgjelinek 	int status;
34577089Sgjelinek 	boolean_t brand_help = B_FALSE;
34581300Sgjelinek 
34591300Sgjelinek 	if (zonecfg_in_alt_root()) {
34601300Sgjelinek 		zerror(gettext("cannot clone zone in alternate root"));
34611300Sgjelinek 		return (Z_ERR);
34621300Sgjelinek 	}
34631300Sgjelinek 
34647089Sgjelinek 	/* Check the argv string for args we handle internally */
34651300Sgjelinek 	optind = 0;
34667089Sgjelinek 	opterr = 0;
34677089Sgjelinek 	while ((arg = getopt(argc, argv, "?m:s:")) != EOF) {
34681300Sgjelinek 		switch (arg) {
34691300Sgjelinek 		case '?':
34707089Sgjelinek 			if (optopt == '?') {
34717089Sgjelinek 				sub_usage(SHELP_CLONE, CMD_CLONE);
34727089Sgjelinek 				brand_help = B_TRUE;
34737089Sgjelinek 			}
34747089Sgjelinek 			/* Ignore unknown options - may be brand specific. */
34757089Sgjelinek 			break;
34761300Sgjelinek 		case 'm':
34771300Sgjelinek 			method = optarg;
34781300Sgjelinek 			break;
34791867Sgjelinek 		case 's':
34801867Sgjelinek 			snapshot = optarg;
34811867Sgjelinek 			break;
34821300Sgjelinek 		default:
34837089Sgjelinek 			/* Ignore unknown options - may be brand specific. */
34847089Sgjelinek 			break;
34851300Sgjelinek 		}
34861300Sgjelinek 	}
34877089Sgjelinek 
34887089Sgjelinek 	if (argc != (optind + 1)) {
34891300Sgjelinek 		sub_usage(SHELP_CLONE, CMD_CLONE);
34901300Sgjelinek 		return (Z_USAGE);
34911300Sgjelinek 	}
34927089Sgjelinek 
34931300Sgjelinek 	source_zone = argv[optind];
34947089Sgjelinek 
34957089Sgjelinek 	if (!brand_help) {
34967089Sgjelinek 		if (sanity_check(target_zone, CMD_CLONE, B_FALSE, B_TRUE,
34977089Sgjelinek 		    B_FALSE) != Z_OK)
34987089Sgjelinek 			return (Z_ERR);
34997089Sgjelinek 		if (verify_details(CMD_CLONE, argv) != Z_OK)
35007089Sgjelinek 			return (Z_ERR);
35017089Sgjelinek 
35027089Sgjelinek 		/*
35037089Sgjelinek 		 * We also need to do some extra validation on the source zone.
35047089Sgjelinek 		 */
35057089Sgjelinek 		if (strcmp(source_zone, GLOBAL_ZONENAME) == 0) {
35067089Sgjelinek 			zerror(gettext("%s operation is invalid for the "
35077089Sgjelinek 			    "global zone."), cmd_to_str(CMD_CLONE));
35087089Sgjelinek 			return (Z_ERR);
35097089Sgjelinek 		}
35107089Sgjelinek 
35117089Sgjelinek 		if (strncmp(source_zone, "SUNW", 4) == 0) {
35127089Sgjelinek 			zerror(gettext("%s operation is invalid for zones "
35137089Sgjelinek 			    "starting with SUNW."), cmd_to_str(CMD_CLONE));
35147089Sgjelinek 			return (Z_ERR);
35157089Sgjelinek 		}
35167089Sgjelinek 
351712578SGlenn.Faden@Sun.COM 		if (auth_check(username, source_zone, SOURCE_ZONE) == Z_ERR) {
351812578SGlenn.Faden@Sun.COM 			zerror(gettext("%s operation is invalid because "
351912578SGlenn.Faden@Sun.COM 			    "user is not authorized to read source zone."),
352012578SGlenn.Faden@Sun.COM 			    cmd_to_str(CMD_CLONE));
352112578SGlenn.Faden@Sun.COM 			return (Z_ERR);
352212578SGlenn.Faden@Sun.COM 		}
352312578SGlenn.Faden@Sun.COM 
35247089Sgjelinek 		zent = lookup_running_zone(source_zone);
35257089Sgjelinek 		if (zent != NULL) {
35267089Sgjelinek 			/* check whether the zone is ready or running */
35277089Sgjelinek 			if ((err = zone_get_state(zent->zname,
35287089Sgjelinek 			    &zent->zstate_num)) != Z_OK) {
35297089Sgjelinek 				errno = err;
35307089Sgjelinek 				zperror2(zent->zname, gettext("could not get "
35317089Sgjelinek 				    "state"));
35327089Sgjelinek 				/* can't tell, so hedge */
35337089Sgjelinek 				zent->zstate_str = "ready/running";
35347089Sgjelinek 			} else {
35357089Sgjelinek 				zent->zstate_str =
35367089Sgjelinek 				    zone_state_str(zent->zstate_num);
35377089Sgjelinek 			}
35387089Sgjelinek 			zerror(gettext("%s operation is invalid for %s zones."),
35397089Sgjelinek 			    cmd_to_str(CMD_CLONE), zent->zstate_str);
35407089Sgjelinek 			return (Z_ERR);
35417089Sgjelinek 		}
35427089Sgjelinek 
35437089Sgjelinek 		if ((err = zone_get_state(source_zone, &state)) != Z_OK) {
35441300Sgjelinek 			errno = err;
35457089Sgjelinek 			zperror2(source_zone, gettext("could not get state"));
35467089Sgjelinek 			return (Z_ERR);
35477089Sgjelinek 		}
35487089Sgjelinek 		if (state != ZONE_STATE_INSTALLED) {
35497089Sgjelinek 			(void) fprintf(stderr,
35507089Sgjelinek 			    gettext("%s: zone %s is %s; %s is required.\n"),
35517089Sgjelinek 			    execname, source_zone, zone_state_str(state),
35527089Sgjelinek 			    zone_state_str(ZONE_STATE_INSTALLED));
35537089Sgjelinek 			return (Z_ERR);
35541300Sgjelinek 		}
35557089Sgjelinek 
35567089Sgjelinek 		/*
35577089Sgjelinek 		 * The source zone checks out ok, continue with the clone.
35587089Sgjelinek 		 */
35597089Sgjelinek 
35607089Sgjelinek 		if (validate_clone(source_zone, target_zone) != Z_OK)
35617089Sgjelinek 			return (Z_ERR);
35627089Sgjelinek 
35637089Sgjelinek 		if (zonecfg_grab_lock_file(target_zone, &lockfd) != Z_OK) {
35647089Sgjelinek 			zerror(gettext("another %s may have an operation in "
35657089Sgjelinek 			    "progress."), "zoneadm");
35667089Sgjelinek 			return (Z_ERR);
35677089Sgjelinek 		}
35681300Sgjelinek 	}
35691300Sgjelinek 
35701300Sgjelinek 	if ((err = zone_get_zonepath(source_zone, source_zonepath,
35711300Sgjelinek 	    sizeof (source_zonepath))) != Z_OK) {
35721300Sgjelinek 		errno = err;
35731300Sgjelinek 		zperror2(source_zone, gettext("could not get zone path"));
35741300Sgjelinek 		goto done;
35751300Sgjelinek 	}
35761300Sgjelinek 
35771300Sgjelinek 	if ((err = zone_get_zonepath(target_zone, zonepath, sizeof (zonepath)))
35781300Sgjelinek 	    != Z_OK) {
35791300Sgjelinek 		errno = err;
35801300Sgjelinek 		zperror2(target_zone, gettext("could not get zone path"));
35811300Sgjelinek 		goto done;
35821300Sgjelinek 	}
35831300Sgjelinek 
35847089Sgjelinek 	/*
35857089Sgjelinek 	 * Fetch the clone and postclone hooks from the brand configuration.
35867089Sgjelinek 	 */
35877089Sgjelinek 	if ((bh = brand_open(target_brand)) == NULL) {
35887089Sgjelinek 		zerror(gettext("missing or invalid brand"));
35897089Sgjelinek 		err = Z_ERR;
35907089Sgjelinek 		goto done;
35917089Sgjelinek 	}
35927089Sgjelinek 
35937089Sgjelinek 	if (get_hook(bh, cmdbuf, sizeof (cmdbuf), brand_get_clone, target_zone,
35947089Sgjelinek 	    zonepath) != Z_OK) {
35957089Sgjelinek 		zerror("invalid brand configuration: missing clone resource");
35967089Sgjelinek 		brand_close(bh);
35977089Sgjelinek 		err = Z_ERR;
35987089Sgjelinek 		goto done;
35997089Sgjelinek 	}
36007089Sgjelinek 
36017089Sgjelinek 	if (get_hook(bh, postcmdbuf, sizeof (postcmdbuf), brand_get_postclone,
36027089Sgjelinek 	    target_zone, zonepath) != Z_OK) {
36037089Sgjelinek 		zerror("invalid brand configuration: missing postclone "
36047089Sgjelinek 		    "resource");
36057089Sgjelinek 		brand_close(bh);
36067089Sgjelinek 		err = Z_ERR;
36077089Sgjelinek 		goto done;
36087089Sgjelinek 	}
36097089Sgjelinek 
36107089Sgjelinek 	if (get_hook(bh, presnapbuf, sizeof (presnapbuf), brand_get_presnap,
36117089Sgjelinek 	    source_zone, source_zonepath) != Z_OK) {
36127089Sgjelinek 		zerror("invalid brand configuration: missing presnap "
36137089Sgjelinek 		    "resource");
36147089Sgjelinek 		brand_close(bh);
36157089Sgjelinek 		err = Z_ERR;
36161300Sgjelinek 		goto done;
36171300Sgjelinek 	}
36181300Sgjelinek 
36197089Sgjelinek 	if (get_hook(bh, postsnapbuf, sizeof (postsnapbuf), brand_get_postsnap,
36207089Sgjelinek 	    source_zone, source_zonepath) != Z_OK) {
36217089Sgjelinek 		zerror("invalid brand configuration: missing postsnap "
36227089Sgjelinek 		    "resource");
36237089Sgjelinek 		brand_close(bh);
36247089Sgjelinek 		err = Z_ERR;
36257089Sgjelinek 		goto done;
36267089Sgjelinek 	}
36277089Sgjelinek 
36287089Sgjelinek 	if (get_hook(bh, validsnapbuf, sizeof (validsnapbuf),
36297089Sgjelinek 	    brand_get_validatesnap, target_zone, zonepath) != Z_OK) {
36307089Sgjelinek 		zerror("invalid brand configuration: missing validatesnap "
36317089Sgjelinek 		    "resource");
36327089Sgjelinek 		brand_close(bh);
36331867Sgjelinek 		err = Z_ERR;
36347089Sgjelinek 		goto done;
36357089Sgjelinek 	}
36367089Sgjelinek 	brand_close(bh);
36377089Sgjelinek 
36387089Sgjelinek 	/* Append all options to clone hook. */
36397089Sgjelinek 	if (addoptions(cmdbuf, argv, sizeof (cmdbuf)) != Z_OK) {
36407089Sgjelinek 		err = Z_ERR;
36417089Sgjelinek 		goto done;
36427089Sgjelinek 	}
36437089Sgjelinek 
36447089Sgjelinek 	/* Append all options to postclone hook. */
36457089Sgjelinek 	if (addoptions(postcmdbuf, argv, sizeof (postcmdbuf)) != Z_OK) {
36467089Sgjelinek 		err = Z_ERR;
36477089Sgjelinek 		goto done;
36487089Sgjelinek 	}
36497089Sgjelinek 
36507089Sgjelinek 	if (!brand_help) {
36517089Sgjelinek 		if ((err = zone_set_state(target_zone, ZONE_STATE_INCOMPLETE))
36527089Sgjelinek 		    != Z_OK) {
36537089Sgjelinek 			errno = err;
36547089Sgjelinek 			zperror2(target_zone, gettext("could not set state"));
36557089Sgjelinek 			goto done;
36567089Sgjelinek 		}
36571867Sgjelinek 	}
36581867Sgjelinek 
36592712Snn35248 	/*
36607089Sgjelinek 	 * The clone hook is optional.  If it exists, use the hook for
36617089Sgjelinek 	 * cloning, otherwise use the built-in clone support
36622712Snn35248 	 */
36637089Sgjelinek 	if (cmdbuf[0] != '\0') {
36647089Sgjelinek 		/* Run the clone hook */
36659310Sgerald.jelinek@sun.com 		status = do_subproc(cmdbuf);
36667089Sgjelinek 		if ((status = subproc_status(gettext("brand-specific clone"),
36677089Sgjelinek 		    status, B_FALSE)) != ZONE_SUBPROC_OK) {
36687089Sgjelinek 			if (status == ZONE_SUBPROC_USAGE && !brand_help)
36697089Sgjelinek 				sub_usage(SHELP_CLONE, CMD_CLONE);
36707089Sgjelinek 			err = Z_ERR;
36717089Sgjelinek 			goto done;
36727089Sgjelinek 		}
36737089Sgjelinek 
36747089Sgjelinek 		if (brand_help)
36757089Sgjelinek 			return (Z_OK);
36767089Sgjelinek 
36777089Sgjelinek 	} else {
36787089Sgjelinek 		/* If just help, we're done since there is no brand help. */
36797089Sgjelinek 		if (brand_help)
36807089Sgjelinek 			return (Z_OK);
36817089Sgjelinek 
36827089Sgjelinek 		/* Run the built-in clone support. */
36837089Sgjelinek 
36847089Sgjelinek 		/* The only explicit built-in method is "copy". */
36857089Sgjelinek 		if (method != NULL && strcmp(method, "copy") != 0) {
36867089Sgjelinek 			sub_usage(SHELP_CLONE, CMD_CLONE);
36877089Sgjelinek 			err = Z_USAGE;
36887089Sgjelinek 			goto done;
36897089Sgjelinek 		}
36907089Sgjelinek 
36917089Sgjelinek 		if (snapshot != NULL) {
36927089Sgjelinek 			err = clone_snapshot_zfs(snapshot, zonepath,
36937089Sgjelinek 			    validsnapbuf);
36947089Sgjelinek 		} else {
36957089Sgjelinek 			/*
36967089Sgjelinek 			 * We always copy the clone unless the source is ZFS
36977089Sgjelinek 			 * and a ZFS clone worked.  We fallback to copying if
36987089Sgjelinek 			 * the ZFS clone fails for some reason.
36997089Sgjelinek 			 */
37007089Sgjelinek 			err = Z_ERR;
37017089Sgjelinek 			if (method == NULL && is_zonepath_zfs(source_zonepath))
37027089Sgjelinek 				err = clone_zfs(source_zonepath, zonepath,
37037089Sgjelinek 				    presnapbuf, postsnapbuf);
37047089Sgjelinek 
37057089Sgjelinek 			if (err != Z_OK)
37067089Sgjelinek 				err = clone_copy(source_zonepath, zonepath);
37077089Sgjelinek 		}
37087089Sgjelinek 	}
37097089Sgjelinek 
37107089Sgjelinek 	if (err == Z_OK && postcmdbuf[0] != '\0') {
37117089Sgjelinek 		status = do_subproc(postcmdbuf);
37127089Sgjelinek 		if ((err = subproc_status("postclone", status, B_FALSE))
37137089Sgjelinek 		    != ZONE_SUBPROC_OK) {
37147089Sgjelinek 			zerror(gettext("post-clone configuration failed."));
37157089Sgjelinek 			err = Z_ERR;
37167089Sgjelinek 		}
37177089Sgjelinek 	}
37181300Sgjelinek 
37191300Sgjelinek done:
37202712Snn35248 	/*
37212712Snn35248 	 * If everything went well, we mark the zone as installed.
37222712Snn35248 	 */
37232712Snn35248 	if (err == Z_OK) {
37242712Snn35248 		err = zone_set_state(target_zone, ZONE_STATE_INSTALLED);
37252712Snn35248 		if (err != Z_OK) {
37262712Snn35248 			errno = err;
37272712Snn35248 			zperror2(target_zone, gettext("could not set state"));
37282712Snn35248 		}
37292712Snn35248 	}
37307089Sgjelinek 	if (!brand_help)
37317089Sgjelinek 		zonecfg_release_lock_file(target_zone, lockfd);
37321300Sgjelinek 	return ((err == Z_OK) ? Z_OK : Z_ERR);
37331300Sgjelinek }
37341300Sgjelinek 
37351607Sgjelinek /*
37361867Sgjelinek  * Used when removing a zonepath after uninstalling or cleaning up after
37371867Sgjelinek  * the move subcommand.  This handles a zonepath that has non-standard
37381867Sgjelinek  * contents so that we will only cleanup the stuff we know about and leave
37391867Sgjelinek  * any user data alone.
37401607Sgjelinek  *
37411867Sgjelinek  * If the "all" parameter is true then we should remove the whole zonepath
37421867Sgjelinek  * even if it has non-standard files/directories in it.  This can be used when
37431867Sgjelinek  * we need to cleanup after moving the zonepath across file systems.
37441867Sgjelinek  *
37451867Sgjelinek  * We "exec" the RMCOMMAND so that the returned status is that of RMCOMMAND
37461867Sgjelinek  * and not the shell.
37471607Sgjelinek  */
37481607Sgjelinek static int
cleanup_zonepath(char * zonepath,boolean_t all)37491867Sgjelinek cleanup_zonepath(char *zonepath, boolean_t all)
37501607Sgjelinek {
37511867Sgjelinek 	int		status;
37521867Sgjelinek 	int		i;
37531867Sgjelinek 	boolean_t	non_std = B_FALSE;
37541867Sgjelinek 	struct dirent	*dp;
37551867Sgjelinek 	DIR		*dirp;
37563686Sgjelinek 			/*
37573686Sgjelinek 			 * The SUNWattached.xml file is expected since it might
37583686Sgjelinek 			 * exist if the zone was force-attached after a
37593686Sgjelinek 			 * migration.
37603686Sgjelinek 			 */
37613686Sgjelinek 	char		*std_entries[] = {"dev", "lu", "root",
37623686Sgjelinek 			    "SUNWattached.xml", NULL};
37631867Sgjelinek 			/* (MAXPATHLEN * 3) is for the 3 std_entries dirs */
37641867Sgjelinek 	char		cmdbuf[sizeof (RMCOMMAND) + (MAXPATHLEN * 3) + 64];
37651867Sgjelinek 
37661867Sgjelinek 	/*
37671867Sgjelinek 	 * We shouldn't need these checks but lets be paranoid since we
37681867Sgjelinek 	 * could blow away the whole system here if we got the wrong zonepath.
37691867Sgjelinek 	 */
37701867Sgjelinek 	if (*zonepath == NULL || strcmp(zonepath, "/") == 0) {
37711867Sgjelinek 		(void) fprintf(stderr, "invalid zonepath '%s'\n", zonepath);
37721867Sgjelinek 		return (Z_INVAL);
37731867Sgjelinek 	}
37741867Sgjelinek 
37751867Sgjelinek 	/*
37761867Sgjelinek 	 * If the dirpath is already gone (maybe it was manually removed) then
37771867Sgjelinek 	 * we just return Z_OK so that the cleanup is successful.
37781867Sgjelinek 	 */
37791867Sgjelinek 	if ((dirp = opendir(zonepath)) == NULL)
37801867Sgjelinek 		return (Z_OK);
37811867Sgjelinek 
37821867Sgjelinek 	/*
37831867Sgjelinek 	 * Look through the zonepath directory to see if there are any
37841867Sgjelinek 	 * non-standard files/dirs.  Also skip .zfs since that might be
37851867Sgjelinek 	 * there but we'll handle ZFS file systems as a special case.
37861867Sgjelinek 	 */
37871867Sgjelinek 	while ((dp = readdir(dirp)) != NULL) {
37881867Sgjelinek 		if (strcmp(dp->d_name, ".") == 0 ||
37891867Sgjelinek 		    strcmp(dp->d_name, "..") == 0 ||
37901867Sgjelinek 		    strcmp(dp->d_name, ".zfs") == 0)
37911867Sgjelinek 			continue;
37921867Sgjelinek 
37931867Sgjelinek 		for (i = 0; std_entries[i] != NULL; i++)
37941867Sgjelinek 			if (strcmp(dp->d_name, std_entries[i]) == 0)
37951867Sgjelinek 				break;
37961867Sgjelinek 
37971867Sgjelinek 		if (std_entries[i] == NULL)
37981867Sgjelinek 			non_std = B_TRUE;
37991867Sgjelinek 	}
38001867Sgjelinek 	(void) closedir(dirp);
38011867Sgjelinek 
38021867Sgjelinek 	if (!all && non_std) {
38031607Sgjelinek 		/*
38041867Sgjelinek 		 * There are extra, non-standard directories/files in the
38051867Sgjelinek 		 * zonepath so we don't want to remove the zonepath.  We
38061867Sgjelinek 		 * just want to remove the standard directories and leave
38071867Sgjelinek 		 * the user data alone.
38081607Sgjelinek 		 */
38091867Sgjelinek 		(void) snprintf(cmdbuf, sizeof (cmdbuf), "exec " RMCOMMAND);
38101867Sgjelinek 
38111867Sgjelinek 		for (i = 0; std_entries[i] != NULL; i++) {
38121867Sgjelinek 			char tmpbuf[MAXPATHLEN];
38131867Sgjelinek 
38141867Sgjelinek 			if (snprintf(tmpbuf, sizeof (tmpbuf), " %s/%s",
38151867Sgjelinek 			    zonepath, std_entries[i]) >= sizeof (tmpbuf) ||
38161867Sgjelinek 			    strlcat(cmdbuf, tmpbuf, sizeof (cmdbuf)) >=
38171867Sgjelinek 			    sizeof (cmdbuf)) {
38181867Sgjelinek 				(void) fprintf(stderr,
38191867Sgjelinek 				    gettext("path is too long\n"));
38201867Sgjelinek 				return (Z_INVAL);
38211867Sgjelinek 			}
38221867Sgjelinek 		}
38231867Sgjelinek 
38241867Sgjelinek 		status = do_subproc(cmdbuf);
38251867Sgjelinek 
38261867Sgjelinek 		(void) fprintf(stderr, gettext("WARNING: Unable to completely "
38271867Sgjelinek 		    "remove %s\nbecause it contains additional user data.  "
38281867Sgjelinek 		    "Only the standard directory\nentries have been "
38291867Sgjelinek 		    "removed.\n"),
38301867Sgjelinek 		    zonepath);
38311867Sgjelinek 
38322712Snn35248 		return ((subproc_status(RMCOMMAND, status, B_TRUE) ==
38332712Snn35248 		    ZONE_SUBPROC_OK) ? Z_OK : Z_ERR);
38341607Sgjelinek 	}
38351607Sgjelinek 
38361867Sgjelinek 	/*
38371867Sgjelinek 	 * There is nothing unexpected in the zonepath, try to get rid of the
38381867Sgjelinek 	 * whole zonepath directory.
38391867Sgjelinek 	 *
38401867Sgjelinek 	 * If the zonepath is its own zfs file system, try to destroy the
38411867Sgjelinek 	 * file system.  If that fails for some reason (e.g. it has clones)
38421867Sgjelinek 	 * then we'll just remove the contents of the zonepath.
38431867Sgjelinek 	 */
38441867Sgjelinek 	if (is_zonepath_zfs(zonepath)) {
38451867Sgjelinek 		if (destroy_zfs(zonepath) == Z_OK)
38461867Sgjelinek 			return (Z_OK);
38471867Sgjelinek 		(void) snprintf(cmdbuf, sizeof (cmdbuf), "exec " RMCOMMAND
38481867Sgjelinek 		    " %s/*", zonepath);
38491867Sgjelinek 		status = do_subproc(cmdbuf);
38502712Snn35248 		return ((subproc_status(RMCOMMAND, status, B_TRUE) ==
38512712Snn35248 		    ZONE_SUBPROC_OK) ? Z_OK : Z_ERR);
38521867Sgjelinek 	}
38531867Sgjelinek 
38541867Sgjelinek 	(void) snprintf(cmdbuf, sizeof (cmdbuf), "exec " RMCOMMAND " %s",
38551867Sgjelinek 	    zonepath);
38561607Sgjelinek 	status = do_subproc(cmdbuf);
38572712Snn35248 
38582712Snn35248 	return ((subproc_status(RMCOMMAND, status, B_TRUE) == ZONE_SUBPROC_OK)
38592712Snn35248 	    ? Z_OK : Z_ERR);
38601607Sgjelinek }
38611607Sgjelinek 
38621300Sgjelinek static int
move_func(int argc,char * argv[])38631300Sgjelinek move_func(int argc, char *argv[])
38641300Sgjelinek {
38651300Sgjelinek 	char *new_zonepath = NULL;
38661300Sgjelinek 	int lockfd;
38671300Sgjelinek 	int err, arg;
38681300Sgjelinek 	char zonepath[MAXPATHLEN];
38691300Sgjelinek 	zone_dochandle_t handle;
38701300Sgjelinek 	boolean_t fast;
38711867Sgjelinek 	boolean_t is_zfs = B_FALSE;
387211276SJordan.Vaughan@Sun.com 	boolean_t root_fs_mounted = B_FALSE;
38731867Sgjelinek 	struct dirent *dp;
38741867Sgjelinek 	DIR *dirp;
38751867Sgjelinek 	boolean_t empty = B_TRUE;
38761300Sgjelinek 	boolean_t revert;
38771300Sgjelinek 	struct stat zonepath_buf;
38781300Sgjelinek 	struct stat new_zonepath_buf;
387911276SJordan.Vaughan@Sun.com 	zone_mounts_t mounts;
38801300Sgjelinek 
38811300Sgjelinek 	if (zonecfg_in_alt_root()) {
38821300Sgjelinek 		zerror(gettext("cannot move zone in alternate root"));
38831300Sgjelinek 		return (Z_ERR);
38841300Sgjelinek 	}
38851300Sgjelinek 
38861300Sgjelinek 	optind = 0;
38871300Sgjelinek 	if ((arg = getopt(argc, argv, "?")) != EOF) {
38881300Sgjelinek 		switch (arg) {
38891300Sgjelinek 		case '?':
38901300Sgjelinek 			sub_usage(SHELP_MOVE, CMD_MOVE);
38911300Sgjelinek 			return (optopt == '?' ? Z_OK : Z_USAGE);
38921300Sgjelinek 		default:
38931300Sgjelinek 			sub_usage(SHELP_MOVE, CMD_MOVE);
38941300Sgjelinek 			return (Z_USAGE);
38951300Sgjelinek 		}
38961300Sgjelinek 	}
38971300Sgjelinek 	if (argc != (optind + 1)) {
38981300Sgjelinek 		sub_usage(SHELP_MOVE, CMD_MOVE);
38991300Sgjelinek 		return (Z_USAGE);
39001300Sgjelinek 	}
39011300Sgjelinek 	new_zonepath = argv[optind];
39022712Snn35248 	if (sanity_check(target_zone, CMD_MOVE, B_FALSE, B_TRUE, B_FALSE)
39032712Snn35248 	    != Z_OK)
39041300Sgjelinek 		return (Z_ERR);
39053339Szt129084 	if (verify_details(CMD_MOVE, argv) != Z_OK)
39061300Sgjelinek 		return (Z_ERR);
39071300Sgjelinek 
39081300Sgjelinek 	/*
39091300Sgjelinek 	 * Check out the new zonepath.  This has the side effect of creating
39101300Sgjelinek 	 * a directory for the new zonepath.  We depend on this later when we
39111867Sgjelinek 	 * stat to see if we are doing a cross file system move or not.
39121300Sgjelinek 	 */
39131300Sgjelinek 	if (validate_zonepath(new_zonepath, CMD_MOVE) != Z_OK)
39141300Sgjelinek 		return (Z_ERR);
39151300Sgjelinek 
39161300Sgjelinek 	if ((err = zone_get_zonepath(target_zone, zonepath, sizeof (zonepath)))
39171300Sgjelinek 	    != Z_OK) {
39181300Sgjelinek 		errno = err;
39191300Sgjelinek 		zperror2(target_zone, gettext("could not get zone path"));
39201300Sgjelinek 		return (Z_ERR);
39211300Sgjelinek 	}
39221300Sgjelinek 
39231300Sgjelinek 	if (stat(zonepath, &zonepath_buf) == -1) {
39241300Sgjelinek 		zperror(gettext("could not stat zone path"), B_FALSE);
39251300Sgjelinek 		return (Z_ERR);
39261300Sgjelinek 	}
39271300Sgjelinek 
39281300Sgjelinek 	if (stat(new_zonepath, &new_zonepath_buf) == -1) {
39291300Sgjelinek 		zperror(gettext("could not stat new zone path"), B_FALSE);
39301300Sgjelinek 		return (Z_ERR);
39311300Sgjelinek 	}
39321300Sgjelinek 
39331867Sgjelinek 	/*
39341867Sgjelinek 	 * Check if the destination directory is empty.
39351867Sgjelinek 	 */
39361867Sgjelinek 	if ((dirp = opendir(new_zonepath)) == NULL) {
39371867Sgjelinek 		zperror(gettext("could not open new zone path"), B_FALSE);
39381867Sgjelinek 		return (Z_ERR);
39391867Sgjelinek 	}
39401867Sgjelinek 	while ((dp = readdir(dirp)) != (struct dirent *)0) {
39411867Sgjelinek 		if (strcmp(dp->d_name, ".") == 0 ||
39421867Sgjelinek 		    strcmp(dp->d_name, "..") == 0)
39431867Sgjelinek 			continue;
39441867Sgjelinek 		empty = B_FALSE;
39451867Sgjelinek 		break;
39461867Sgjelinek 	}
39471867Sgjelinek 	(void) closedir(dirp);
39481867Sgjelinek 
39491867Sgjelinek 	/* Error if there is anything in the destination directory. */
39501867Sgjelinek 	if (!empty) {
39511867Sgjelinek 		(void) fprintf(stderr, gettext("could not move zone to %s: "
39521867Sgjelinek 		    "directory not empty\n"), new_zonepath);
39531867Sgjelinek 		return (Z_ERR);
39541867Sgjelinek 	}
39551867Sgjelinek 
395611276SJordan.Vaughan@Sun.com 	/*
395711276SJordan.Vaughan@Sun.com 	 * Collect information about mounts within the zone's zonepath.
395811276SJordan.Vaughan@Sun.com 	 * Overlay mounts on the zone's root directory are erroneous.
395911276SJordan.Vaughan@Sun.com 	 * Bail if we encounter any unexpected mounts.
396011276SJordan.Vaughan@Sun.com 	 */
396111276SJordan.Vaughan@Sun.com 	if (zone_mounts_init(&mounts, zonepath) != 0)
39621300Sgjelinek 		return (Z_ERR);
396311276SJordan.Vaughan@Sun.com 	if (mounts.num_root_overlay_mounts != 0) {
396411276SJordan.Vaughan@Sun.com 		zerror(gettext("%d overlay mount(s) detected on %s/root."),
396511276SJordan.Vaughan@Sun.com 		    mounts.num_root_overlay_mounts, zonepath);
396611276SJordan.Vaughan@Sun.com 		goto err_and_mounts_destroy;
396711276SJordan.Vaughan@Sun.com 	}
396811276SJordan.Vaughan@Sun.com 	if (mounts.num_unexpected_mounts != 0)
396911276SJordan.Vaughan@Sun.com 		goto err_and_mounts_destroy;
39701300Sgjelinek 
39711300Sgjelinek 	/*
39721867Sgjelinek 	 * Check if we are moving in the same file system and can do a fast
39731867Sgjelinek 	 * move or if we are crossing file systems and have to copy the data.
39741300Sgjelinek 	 */
39751300Sgjelinek 	fast = (zonepath_buf.st_dev == new_zonepath_buf.st_dev);
39761300Sgjelinek 
39771300Sgjelinek 	if ((handle = zonecfg_init_handle()) == NULL) {
39781300Sgjelinek 		zperror(cmd_to_str(CMD_MOVE), B_TRUE);
397911276SJordan.Vaughan@Sun.com 		goto err_and_mounts_destroy;
39801300Sgjelinek 	}
39811300Sgjelinek 
39821300Sgjelinek 	if ((err = zonecfg_get_handle(target_zone, handle)) != Z_OK) {
39831300Sgjelinek 		errno = err;
39841300Sgjelinek 		zperror(cmd_to_str(CMD_MOVE), B_TRUE);
398511276SJordan.Vaughan@Sun.com 		goto err_and_fini_handle;
39861300Sgjelinek 	}
39871300Sgjelinek 
39887089Sgjelinek 	if (zonecfg_grab_lock_file(target_zone, &lockfd) != Z_OK) {
39891300Sgjelinek 		zerror(gettext("another %s may have an operation in progress."),
39901300Sgjelinek 		    "zoneadm");
399111276SJordan.Vaughan@Sun.com 		goto err_and_fini_handle;
399211276SJordan.Vaughan@Sun.com 	}
399311276SJordan.Vaughan@Sun.com 
399411276SJordan.Vaughan@Sun.com 	/*
399511276SJordan.Vaughan@Sun.com 	 * Unmount the zone's root filesystem before we move the zone's
399611276SJordan.Vaughan@Sun.com 	 * zonepath.
399711276SJordan.Vaughan@Sun.com 	 */
399811276SJordan.Vaughan@Sun.com 	if (zone_unmount_rootfs(&mounts, zonepath, B_FALSE) != 0)
399911276SJordan.Vaughan@Sun.com 		goto err_and_rele_lockfile;
40001300Sgjelinek 
40011300Sgjelinek 	/*
40021867Sgjelinek 	 * We're making some file system changes now so we have to clean up
40031867Sgjelinek 	 * the file system before we are done.  This will either clean up the
40041300Sgjelinek 	 * new zonepath if the zonecfg update failed or it will clean up the
40051300Sgjelinek 	 * old zonepath if everything is ok.
40061300Sgjelinek 	 */
40071300Sgjelinek 	revert = B_TRUE;
40081300Sgjelinek 
40091867Sgjelinek 	if (is_zonepath_zfs(zonepath) &&
40101867Sgjelinek 	    move_zfs(zonepath, new_zonepath) != Z_ERR) {
40111867Sgjelinek 		is_zfs = B_TRUE;
40121867Sgjelinek 
40131867Sgjelinek 	} else if (fast) {
40141867Sgjelinek 		/* same file system, use rename for a quick move */
40151300Sgjelinek 
40161300Sgjelinek 		/*
40171300Sgjelinek 		 * Remove the new_zonepath directory that got created above
40181300Sgjelinek 		 * during the validation.  It gets in the way of the rename.
40191300Sgjelinek 		 */
40201300Sgjelinek 		if (rmdir(new_zonepath) != 0) {
40211300Sgjelinek 			zperror(gettext("could not rmdir new zone path"),
40221300Sgjelinek 			    B_FALSE);
402311276SJordan.Vaughan@Sun.com 			(void) zone_mount_rootfs(&mounts, zonepath);
402411276SJordan.Vaughan@Sun.com 			goto err_and_rele_lockfile;
40251300Sgjelinek 		}
40261300Sgjelinek 
40271300Sgjelinek 		if (rename(zonepath, new_zonepath) != 0) {
40281300Sgjelinek 			/*
40291300Sgjelinek 			 * If this fails we don't need to do all of the
40301300Sgjelinek 			 * cleanup that happens for the rest of the code
40311300Sgjelinek 			 * so just return from this error.
40321300Sgjelinek 			 */
40331300Sgjelinek 			zperror(gettext("could not move zone"), B_FALSE);
403411276SJordan.Vaughan@Sun.com 			(void) zone_mount_rootfs(&mounts, zonepath);
403511276SJordan.Vaughan@Sun.com 			goto err_and_rele_lockfile;
40361300Sgjelinek 		}
40371300Sgjelinek 
40381300Sgjelinek 	} else {
40391867Sgjelinek 		/*
40401867Sgjelinek 		 * Attempt to create a ZFS fs for the new zonepath.  As usual,
40411867Sgjelinek 		 * we don't care if this works or not since we always have the
40421867Sgjelinek 		 * default behavior of a simple directory for the zonepath.
40431867Sgjelinek 		 */
40441867Sgjelinek 		create_zfs_zonepath(new_zonepath);
40451867Sgjelinek 
40461300Sgjelinek 		(void) printf(gettext(
40471867Sgjelinek 		    "Moving across file systems; copying zonepath %s..."),
40481300Sgjelinek 		    zonepath);
40491300Sgjelinek 		(void) fflush(stdout);
40501300Sgjelinek 
40511300Sgjelinek 		err = copy_zone(zonepath, new_zonepath);
40521300Sgjelinek 
40531300Sgjelinek 		(void) printf("\n");
40541300Sgjelinek 		if (err != Z_OK)
40551300Sgjelinek 			goto done;
40561300Sgjelinek 	}
40571300Sgjelinek 
405811276SJordan.Vaughan@Sun.com 	/*
405911276SJordan.Vaughan@Sun.com 	 * Mount the zone's root filesystem in the new zonepath if there was
406011276SJordan.Vaughan@Sun.com 	 * a root mount prior to the move.
406111276SJordan.Vaughan@Sun.com 	 */
406211276SJordan.Vaughan@Sun.com 	if (zone_mount_rootfs(&mounts, new_zonepath) != 0) {
406311276SJordan.Vaughan@Sun.com 		err = Z_ERR;
406411276SJordan.Vaughan@Sun.com 		goto done;
406511276SJordan.Vaughan@Sun.com 	}
406611276SJordan.Vaughan@Sun.com 	root_fs_mounted = B_TRUE;
406711276SJordan.Vaughan@Sun.com 
40681300Sgjelinek 	if ((err = zonecfg_set_zonepath(handle, new_zonepath)) != Z_OK) {
40691300Sgjelinek 		errno = err;
40701300Sgjelinek 		zperror(gettext("could not set new zonepath"), B_TRUE);
40711300Sgjelinek 		goto done;
40721300Sgjelinek 	}
40731300Sgjelinek 
40741300Sgjelinek 	if ((err = zonecfg_save(handle)) != Z_OK) {
40751300Sgjelinek 		errno = err;
40761300Sgjelinek 		zperror(gettext("zonecfg save failed"), B_TRUE);
40771300Sgjelinek 		goto done;
40781300Sgjelinek 	}
40791300Sgjelinek 
40801300Sgjelinek 	revert = B_FALSE;
40811300Sgjelinek 
40821300Sgjelinek done:
40831300Sgjelinek 	zonecfg_fini_handle(handle);
40847089Sgjelinek 	zonecfg_release_lock_file(target_zone, lockfd);
40851300Sgjelinek 
40861300Sgjelinek 	/*
40871867Sgjelinek 	 * Clean up the file system based on how things went.  We either
40881300Sgjelinek 	 * clean up the new zonepath if the operation failed for some reason
40891300Sgjelinek 	 * or we clean up the old zonepath if everything is ok.
40901300Sgjelinek 	 */
40911300Sgjelinek 	if (revert) {
409211276SJordan.Vaughan@Sun.com 		/*
409311276SJordan.Vaughan@Sun.com 		 * Check for the unlikely scenario in which the zone's
409411276SJordan.Vaughan@Sun.com 		 * zonepath and its root file system moved but libzonecfg
409511276SJordan.Vaughan@Sun.com 		 * couldn't save the new zonepath to the zone's configuration
409611276SJordan.Vaughan@Sun.com 		 * file.  The mounted root filesystem must be unmounted before
409711276SJordan.Vaughan@Sun.com 		 * zoneadm restores the zone's zonepath.
409811276SJordan.Vaughan@Sun.com 		 */
409911276SJordan.Vaughan@Sun.com 		if (root_fs_mounted && zone_unmount_rootfs(&mounts,
410011276SJordan.Vaughan@Sun.com 		    new_zonepath, B_TRUE) != 0) {
410111276SJordan.Vaughan@Sun.com 			/*
410211276SJordan.Vaughan@Sun.com 			 * We can't forcibly unmount the zone's root file system
410311276SJordan.Vaughan@Sun.com 			 * from the new zonepath.  Bail!
410411276SJordan.Vaughan@Sun.com 			 */
410511276SJordan.Vaughan@Sun.com 			zerror(gettext("fatal error: cannot unmount %s/root\n"),
410611276SJordan.Vaughan@Sun.com 			    new_zonepath);
410711276SJordan.Vaughan@Sun.com 			goto err_and_mounts_destroy;
410811276SJordan.Vaughan@Sun.com 		}
410911276SJordan.Vaughan@Sun.com 
41101300Sgjelinek 		/* The zonecfg update failed, cleanup the new zonepath. */
41111867Sgjelinek 		if (is_zfs) {
41121867Sgjelinek 			if (move_zfs(new_zonepath, zonepath) == Z_ERR) {
41131867Sgjelinek 				(void) fprintf(stderr, gettext("could not "
41141867Sgjelinek 				    "restore zonepath, the zfs mountpoint is "
41151867Sgjelinek 				    "set as:\n%s\n"), new_zonepath);
41161867Sgjelinek 				/*
41171867Sgjelinek 				 * err is already != Z_OK since we're reverting
41181867Sgjelinek 				 */
411911276SJordan.Vaughan@Sun.com 			} else {
412011276SJordan.Vaughan@Sun.com 				(void) zone_mount_rootfs(&mounts, zonepath);
41211867Sgjelinek 			}
41221867Sgjelinek 		} else if (fast) {
41231300Sgjelinek 			if (rename(new_zonepath, zonepath) != 0) {
41241300Sgjelinek 				zperror(gettext("could not restore zonepath"),
41251300Sgjelinek 				    B_FALSE);
41261300Sgjelinek 				/*
41271300Sgjelinek 				 * err is already != Z_OK since we're reverting
41281300Sgjelinek 				 */
412911276SJordan.Vaughan@Sun.com 			} else {
413011276SJordan.Vaughan@Sun.com 				(void) zone_mount_rootfs(&mounts, zonepath);
41311300Sgjelinek 			}
41321300Sgjelinek 		} else {
41331300Sgjelinek 			(void) printf(gettext("Cleaning up zonepath %s..."),
41341300Sgjelinek 			    new_zonepath);
41351300Sgjelinek 			(void) fflush(stdout);
41361867Sgjelinek 			err = cleanup_zonepath(new_zonepath, B_TRUE);
41371300Sgjelinek 			(void) printf("\n");
41381300Sgjelinek 
41391607Sgjelinek 			if (err != Z_OK) {
41401300Sgjelinek 				errno = err;
41411300Sgjelinek 				zperror(gettext("could not remove new "
41421300Sgjelinek 				    "zonepath"), B_TRUE);
41431300Sgjelinek 			} else {
41441300Sgjelinek 				/*
41451300Sgjelinek 				 * Because we're reverting we know the mainline
41461300Sgjelinek 				 * code failed but we just reused the err
41471300Sgjelinek 				 * variable so we reset it back to Z_ERR.
41481300Sgjelinek 				 */
41491300Sgjelinek 				err = Z_ERR;
41501300Sgjelinek 			}
415111276SJordan.Vaughan@Sun.com 
415211276SJordan.Vaughan@Sun.com 			(void) zone_mount_rootfs(&mounts, zonepath);
41531300Sgjelinek 		}
41541300Sgjelinek 	} else {
41551300Sgjelinek 		/* The move was successful, cleanup the old zonepath. */
41561867Sgjelinek 		if (!is_zfs && !fast) {
41571300Sgjelinek 			(void) printf(
41581300Sgjelinek 			    gettext("Cleaning up zonepath %s..."), zonepath);
41591300Sgjelinek 			(void) fflush(stdout);
41601867Sgjelinek 			err = cleanup_zonepath(zonepath, B_TRUE);
41611300Sgjelinek 			(void) printf("\n");
41621300Sgjelinek 
41631607Sgjelinek 			if (err != Z_OK) {
41641300Sgjelinek 				errno = err;
41651300Sgjelinek 				zperror(gettext("could not remove zonepath"),
41661300Sgjelinek 				    B_TRUE);
41671300Sgjelinek 			}
41681300Sgjelinek 		}
41691300Sgjelinek 	}
41701300Sgjelinek 
417111276SJordan.Vaughan@Sun.com 	zone_mounts_destroy(&mounts);
41721300Sgjelinek 	return ((err == Z_OK) ? Z_OK : Z_ERR);
417311276SJordan.Vaughan@Sun.com 
417411276SJordan.Vaughan@Sun.com err_and_rele_lockfile:
417511276SJordan.Vaughan@Sun.com 	zonecfg_release_lock_file(target_zone, lockfd);
417611276SJordan.Vaughan@Sun.com err_and_fini_handle:
417711276SJordan.Vaughan@Sun.com 	zonecfg_fini_handle(handle);
417811276SJordan.Vaughan@Sun.com err_and_mounts_destroy:
417911276SJordan.Vaughan@Sun.com 	zone_mounts_destroy(&mounts);
418011276SJordan.Vaughan@Sun.com 	return (Z_ERR);
41811300Sgjelinek }
41821300Sgjelinek 
41837089Sgjelinek /* ARGSUSED */
41841507Sgjelinek static int
detach_func(int argc,char * argv[])41851507Sgjelinek detach_func(int argc, char *argv[])
41861507Sgjelinek {
41878301Sgerald.jelinek@sun.com 	int lockfd = -1;
41881507Sgjelinek 	int err, arg;
41891507Sgjelinek 	char zonepath[MAXPATHLEN];
41904785Sgjelinek 	char cmdbuf[MAXPATHLEN];
41917089Sgjelinek 	char precmdbuf[MAXPATHLEN];
41922078Sgjelinek 	boolean_t execute = B_TRUE;
41937089Sgjelinek 	boolean_t brand_help = B_FALSE;
41944785Sgjelinek 	brand_handle_t bh = NULL;
41957089Sgjelinek 	int status;
41961507Sgjelinek 
41971507Sgjelinek 	if (zonecfg_in_alt_root()) {
41981507Sgjelinek 		zerror(gettext("cannot detach zone in alternate root"));
41991507Sgjelinek 		return (Z_ERR);
42001507Sgjelinek 	}
42011507Sgjelinek 
42027089Sgjelinek 	/* Check the argv string for args we handle internally */
42031507Sgjelinek 	optind = 0;
42047089Sgjelinek 	opterr = 0;
42057089Sgjelinek 	while ((arg = getopt(argc, argv, "?n")) != EOF) {
42061507Sgjelinek 		switch (arg) {
42071507Sgjelinek 		case '?':
42087089Sgjelinek 			if (optopt == '?') {
42097089Sgjelinek 				sub_usage(SHELP_DETACH, CMD_DETACH);
42107089Sgjelinek 				brand_help = B_TRUE;
42117089Sgjelinek 			}
42127089Sgjelinek 			/* Ignore unknown options - may be brand specific. */
42137089Sgjelinek 			break;
42142078Sgjelinek 		case 'n':
42152078Sgjelinek 			execute = B_FALSE;
42162078Sgjelinek 			break;
42171507Sgjelinek 		default:
42187089Sgjelinek 			/* Ignore unknown options - may be brand specific. */
42197089Sgjelinek 			break;
42201507Sgjelinek 		}
42211507Sgjelinek 	}
42222712Snn35248 
42237089Sgjelinek 	if (brand_help)
42247089Sgjelinek 		execute = B_FALSE;
42257089Sgjelinek 
42262078Sgjelinek 	if (execute) {
42272712Snn35248 		if (sanity_check(target_zone, CMD_DETACH, B_FALSE, B_TRUE,
42282712Snn35248 		    B_FALSE) != Z_OK)
42292078Sgjelinek 			return (Z_ERR);
42303339Szt129084 		if (verify_details(CMD_DETACH, argv) != Z_OK)
42312078Sgjelinek 			return (Z_ERR);
42322078Sgjelinek 	} else {
42332078Sgjelinek 		/*
42342078Sgjelinek 		 * We want a dry-run to work for a non-privileged user so we
42352078Sgjelinek 		 * only do minimal validation.
42362078Sgjelinek 		 */
42372078Sgjelinek 		if (target_zone == NULL) {
42382078Sgjelinek 			zerror(gettext("no zone specified"));
42392078Sgjelinek 			return (Z_ERR);
42402078Sgjelinek 		}
42412078Sgjelinek 
42422078Sgjelinek 		if (strcmp(target_zone, GLOBAL_ZONENAME) == 0) {
42432078Sgjelinek 			zerror(gettext("%s operation is invalid for the "
42442078Sgjelinek 			    "global zone."), cmd_to_str(CMD_DETACH));
42452078Sgjelinek 			return (Z_ERR);
42462078Sgjelinek 		}
42472078Sgjelinek 	}
42481507Sgjelinek 
42491507Sgjelinek 	if ((err = zone_get_zonepath(target_zone, zonepath, sizeof (zonepath)))
42501507Sgjelinek 	    != Z_OK) {
42511507Sgjelinek 		errno = err;
42521507Sgjelinek 		zperror2(target_zone, gettext("could not get zone path"));
42531507Sgjelinek 		return (Z_ERR);
42541507Sgjelinek 	}
42551507Sgjelinek 
42567089Sgjelinek 	/* Fetch the detach and predetach hooks from the brand configuration. */
42574785Sgjelinek 	if ((bh = brand_open(target_brand)) == NULL) {
42584785Sgjelinek 		zerror(gettext("missing or invalid brand"));
42594785Sgjelinek 		return (Z_ERR);
42604785Sgjelinek 	}
42614785Sgjelinek 
42627089Sgjelinek 	if (get_hook(bh, cmdbuf, sizeof (cmdbuf), brand_get_detach, target_zone,
42637089Sgjelinek 	    zonepath) != Z_OK) {
42647089Sgjelinek 		zerror("invalid brand configuration: missing detach resource");
42657089Sgjelinek 		brand_close(bh);
42667089Sgjelinek 		return (Z_ERR);
42677089Sgjelinek 	}
42687089Sgjelinek 
42697089Sgjelinek 	if (get_hook(bh, precmdbuf, sizeof (precmdbuf), brand_get_predetach,
42707089Sgjelinek 	    target_zone, zonepath) != Z_OK) {
42714785Sgjelinek 		zerror("invalid brand configuration: missing predetach "
42724785Sgjelinek 		    "resource");
42734785Sgjelinek 		brand_close(bh);
42744785Sgjelinek 		return (Z_ERR);
42754785Sgjelinek 	}
42764785Sgjelinek 	brand_close(bh);
42774785Sgjelinek 
42787089Sgjelinek 	/* Append all options to predetach hook. */
42797089Sgjelinek 	if (addoptions(precmdbuf, argv, sizeof (precmdbuf)) != Z_OK)
42807089Sgjelinek 		return (Z_ERR);
42817089Sgjelinek 
42827089Sgjelinek 	/* Append all options to detach hook. */
42837089Sgjelinek 	if (addoptions(cmdbuf, argv, sizeof (cmdbuf)) != Z_OK)
42847089Sgjelinek 		return (Z_ERR);
42857089Sgjelinek 
42867089Sgjelinek 	if (execute && zonecfg_grab_lock_file(target_zone, &lockfd) != Z_OK) {
42871507Sgjelinek 		zerror(gettext("another %s may have an operation in progress."),
42881507Sgjelinek 		    "zoneadm");
42891507Sgjelinek 		return (Z_ERR);
42901507Sgjelinek 	}
42911507Sgjelinek 
42927089Sgjelinek 	/* If we have a brand predetach hook, run it. */
42937089Sgjelinek 	if (!brand_help && precmdbuf[0] != '\0') {
42947089Sgjelinek 		status = do_subproc(precmdbuf);
42957089Sgjelinek 		if (subproc_status(gettext("brand-specific predetach"),
42967089Sgjelinek 		    status, B_FALSE) != ZONE_SUBPROC_OK) {
42977089Sgjelinek 
42988301Sgerald.jelinek@sun.com 			if (execute) {
42998301Sgerald.jelinek@sun.com 				assert(lockfd >= 0);
43007089Sgjelinek 				zonecfg_release_lock_file(target_zone, lockfd);
43018301Sgerald.jelinek@sun.com 				lockfd = -1;
43028301Sgerald.jelinek@sun.com 			}
43038301Sgerald.jelinek@sun.com 
43048301Sgerald.jelinek@sun.com 			assert(lockfd == -1);
43057089Sgjelinek 			return (Z_ERR);
43067089Sgjelinek 		}
43077089Sgjelinek 	}
43087089Sgjelinek 
43097089Sgjelinek 	if (cmdbuf[0] != '\0') {
43107089Sgjelinek 		/* Run the detach hook */
43119310Sgerald.jelinek@sun.com 		status = do_subproc(cmdbuf);
43127089Sgjelinek 		if ((status = subproc_status(gettext("brand-specific detach"),
43137089Sgjelinek 		    status, B_FALSE)) != ZONE_SUBPROC_OK) {
43147089Sgjelinek 			if (status == ZONE_SUBPROC_USAGE && !brand_help)
43157089Sgjelinek 				sub_usage(SHELP_DETACH, CMD_DETACH);
43167089Sgjelinek 
43178301Sgerald.jelinek@sun.com 			if (execute) {
43188301Sgerald.jelinek@sun.com 				assert(lockfd >= 0);
43197089Sgjelinek 				zonecfg_release_lock_file(target_zone, lockfd);
43208301Sgerald.jelinek@sun.com 				lockfd = -1;
43218301Sgerald.jelinek@sun.com 			}
43228301Sgerald.jelinek@sun.com 
43238301Sgerald.jelinek@sun.com 			assert(lockfd == -1);
43247089Sgjelinek 			return (Z_ERR);
43257089Sgjelinek 		}
43267089Sgjelinek 
43277089Sgjelinek 	} else {
43288301Sgerald.jelinek@sun.com 		zone_dochandle_t handle;
43298301Sgerald.jelinek@sun.com 
43307089Sgjelinek 		/* If just help, we're done since there is no brand help. */
43318301Sgerald.jelinek@sun.com 		if (brand_help) {
43328301Sgerald.jelinek@sun.com 			assert(lockfd == -1);
43337089Sgjelinek 			return (Z_OK);
43348301Sgerald.jelinek@sun.com 		}
43357089Sgjelinek 
43367089Sgjelinek 		/*
43377089Sgjelinek 		 * Run the built-in detach support.  Just generate a simple
43387089Sgjelinek 		 * zone definition XML file and detach.
43397089Sgjelinek 		 */
43407089Sgjelinek 
43417089Sgjelinek 		/* Don't detach the zone if anything is still mounted there */
43427089Sgjelinek 		if (execute && zonecfg_find_mounts(zonepath, NULL, NULL)) {
43437089Sgjelinek 			(void) fprintf(stderr, gettext("These file systems are "
43447089Sgjelinek 			    "mounted on subdirectories of %s.\n"), zonepath);
43457089Sgjelinek 			(void) zonecfg_find_mounts(zonepath, zfm_print, NULL);
43467089Sgjelinek 			err = ZONE_SUBPROC_NOTCOMPLETE;
43477089Sgjelinek 			goto done;
43487089Sgjelinek 		}
43497089Sgjelinek 
43507089Sgjelinek 		if ((handle = zonecfg_init_handle()) == NULL) {
43517089Sgjelinek 			zperror(cmd_to_str(CMD_DETACH), B_TRUE);
43527089Sgjelinek 			err = ZONE_SUBPROC_NOTCOMPLETE;
43537089Sgjelinek 			goto done;
43547089Sgjelinek 		}
43557089Sgjelinek 
43567089Sgjelinek 		if ((err = zonecfg_get_handle(target_zone, handle)) != Z_OK) {
43577089Sgjelinek 			errno = err;
43587089Sgjelinek 			zperror(cmd_to_str(CMD_DETACH), B_TRUE);
43598301Sgerald.jelinek@sun.com 
43608301Sgerald.jelinek@sun.com 		} else if ((err = zonecfg_detach_save(handle,
43617089Sgjelinek 		    (execute ? 0 : ZONE_DRY_RUN))) != Z_OK) {
43627089Sgjelinek 			errno = err;
43637089Sgjelinek 			zperror(gettext("saving the detach manifest failed"),
43647089Sgjelinek 			    B_TRUE);
43657089Sgjelinek 		}
43667089Sgjelinek 
43677089Sgjelinek 		zonecfg_fini_handle(handle);
43688301Sgerald.jelinek@sun.com 		if (err != Z_OK)
43698301Sgerald.jelinek@sun.com 			goto done;
43701507Sgjelinek 	}
43711507Sgjelinek 
43722078Sgjelinek 	/*
43732078Sgjelinek 	 * Set the zone state back to configured unless we are running with the
43742078Sgjelinek 	 * no-execute option.
43752078Sgjelinek 	 */
43762078Sgjelinek 	if (execute && (err = zone_set_state(target_zone,
43772078Sgjelinek 	    ZONE_STATE_CONFIGURED)) != Z_OK) {
43781507Sgjelinek 		errno = err;
43791507Sgjelinek 		zperror(gettext("could not reset state"), B_TRUE);
43801507Sgjelinek 	}
43811507Sgjelinek 
43821507Sgjelinek done:
43838301Sgerald.jelinek@sun.com 	if (execute) {
43848301Sgerald.jelinek@sun.com 		assert(lockfd >= 0);
43857089Sgjelinek 		zonecfg_release_lock_file(target_zone, lockfd);
43868301Sgerald.jelinek@sun.com 		lockfd = -1;
43878301Sgerald.jelinek@sun.com 	}
43888301Sgerald.jelinek@sun.com 
43898301Sgerald.jelinek@sun.com 	assert(lockfd == -1);
43901507Sgjelinek 	return ((err == Z_OK) ? Z_OK : Z_ERR);
43911507Sgjelinek }
43921507Sgjelinek 
43931507Sgjelinek /*
43947089Sgjelinek  * Determine the brand when doing a dry-run attach.  The zone does not have to
43957089Sgjelinek  * exist, so we have to read the incoming manifest to determine the zone's
43967089Sgjelinek  * brand.
43977089Sgjelinek  *
43987089Sgjelinek  * Because the manifest has to be processed twice; once to determine the brand
43997089Sgjelinek  * and once to do the brand-specific attach logic, we always read it into a tmp
44007089Sgjelinek  * file.  This handles the manifest coming from stdin or a regular file.  The
44017089Sgjelinek  * tmpname parameter returns the name of the temporary file that the manifest
44027089Sgjelinek  * was read into.
44031507Sgjelinek  */
44041507Sgjelinek static int
dryrun_get_brand(char * manifest_path,char * tmpname,int size)44057089Sgjelinek dryrun_get_brand(char *manifest_path, char *tmpname, int size)
44062078Sgjelinek {
44072078Sgjelinek 	int fd;
44082078Sgjelinek 	int err;
44097089Sgjelinek 	int res = Z_OK;
44102078Sgjelinek 	zone_dochandle_t local_handle;
44112078Sgjelinek 	zone_dochandle_t rem_handle = NULL;
44127089Sgjelinek 	int len;
44137089Sgjelinek 	int ofd;
44147089Sgjelinek 	char buf[512];
44152078Sgjelinek 
44162078Sgjelinek 	if (strcmp(manifest_path, "-") == 0) {
44177089Sgjelinek 		fd = STDIN_FILENO;
44187089Sgjelinek 	} else {
44197089Sgjelinek 		if ((fd = open(manifest_path, O_RDONLY)) < 0) {
44207089Sgjelinek 			if (getcwd(buf, sizeof (buf)) == NULL)
44217089Sgjelinek 				(void) strlcpy(buf, "/", sizeof (buf));
44227089Sgjelinek 			zerror(gettext("could not open manifest path %s%s: %s"),
44237089Sgjelinek 			    (*manifest_path == '/' ? "" : buf), manifest_path,
44247089Sgjelinek 			    strerror(errno));
44257089Sgjelinek 			return (Z_ERR);
44267089Sgjelinek 		}
44277089Sgjelinek 	}
44287089Sgjelinek 
44297089Sgjelinek 	(void) snprintf(tmpname, size, "/var/run/zone.%d", getpid());
44307089Sgjelinek 
44317089Sgjelinek 	if ((ofd = open(tmpname, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR)) < 0) {
44327089Sgjelinek 		zperror(gettext("could not save manifest"), B_FALSE);
44337089Sgjelinek 		(void) close(fd);
44347089Sgjelinek 		return (Z_ERR);
44357089Sgjelinek 	}
44367089Sgjelinek 
44377089Sgjelinek 	while ((len = read(fd, buf, sizeof (buf))) > 0) {
44387089Sgjelinek 		if (write(ofd, buf, len) == -1) {
44397089Sgjelinek 			zperror(gettext("could not save manifest"), B_FALSE);
44407089Sgjelinek 			(void) close(ofd);
44417089Sgjelinek 			(void) close(fd);
44427089Sgjelinek 			return (Z_ERR);
44437089Sgjelinek 		}
44447089Sgjelinek 	}
44457089Sgjelinek 
44467089Sgjelinek 	if (close(ofd) != 0) {
44477089Sgjelinek 		zperror(gettext("could not save manifest"), B_FALSE);
44487089Sgjelinek 		(void) close(fd);
44497089Sgjelinek 		return (Z_ERR);
44507089Sgjelinek 	}
44517089Sgjelinek 
44527089Sgjelinek 	(void) close(fd);
44537089Sgjelinek 
44547089Sgjelinek 	if ((fd = open(tmpname, O_RDONLY)) < 0) {
44552078Sgjelinek 		zperror(gettext("could not open manifest path"), B_FALSE);
44562078Sgjelinek 		return (Z_ERR);
44572078Sgjelinek 	}
44582078Sgjelinek 
44592078Sgjelinek 	if ((local_handle = zonecfg_init_handle()) == NULL) {
44602078Sgjelinek 		zperror(cmd_to_str(CMD_ATTACH), B_TRUE);
44612078Sgjelinek 		res = Z_ERR;
44622078Sgjelinek 		goto done;
44632078Sgjelinek 	}
44642078Sgjelinek 
44652078Sgjelinek 	if ((rem_handle = zonecfg_init_handle()) == NULL) {
44662078Sgjelinek 		zperror(cmd_to_str(CMD_ATTACH), B_TRUE);
44672078Sgjelinek 		res = Z_ERR;
44682078Sgjelinek 		goto done;
44692078Sgjelinek 	}
44702078Sgjelinek 
44712078Sgjelinek 	if ((err = zonecfg_attach_manifest(fd, local_handle, rem_handle))
44722078Sgjelinek 	    != Z_OK) {
44733686Sgjelinek 		res = Z_ERR;
44743686Sgjelinek 
44753686Sgjelinek 		if (err == Z_INVALID_DOCUMENT) {
44763686Sgjelinek 			struct stat st;
44773686Sgjelinek 			char buf[6];
44783686Sgjelinek 
44793686Sgjelinek 			if (strcmp(manifest_path, "-") == 0) {
44803686Sgjelinek 				zerror(gettext("Input is not a valid XML "
44813686Sgjelinek 				    "file"));
44823686Sgjelinek 				goto done;
44833686Sgjelinek 			}
44843686Sgjelinek 
44853686Sgjelinek 			if (fstat(fd, &st) == -1 || !S_ISREG(st.st_mode)) {
44863686Sgjelinek 				zerror(gettext("%s is not an XML file"),
44873686Sgjelinek 				    manifest_path);
44883686Sgjelinek 				goto done;
44893686Sgjelinek 			}
44903686Sgjelinek 
44913686Sgjelinek 			bzero(buf, sizeof (buf));
44923686Sgjelinek 			(void) lseek(fd, 0L, SEEK_SET);
44933686Sgjelinek 			if (read(fd, buf, sizeof (buf) - 1) < 0 ||
44943686Sgjelinek 			    strncmp(buf, "<?xml", 5) != 0)
44953686Sgjelinek 				zerror(gettext("%s is not an XML file"),
44963686Sgjelinek 				    manifest_path);
44973686Sgjelinek 			else
44983686Sgjelinek 				zerror(gettext("Cannot attach to an earlier "
44993686Sgjelinek 				    "release of the operating system"));
45003686Sgjelinek 		} else {
45012078Sgjelinek 			zperror(cmd_to_str(CMD_ATTACH), B_TRUE);
45023686Sgjelinek 		}
45032078Sgjelinek 		goto done;
45042078Sgjelinek 	}
45052078Sgjelinek 
45067089Sgjelinek 	/* Retrieve remote handle brand type. */
45073172Sgjelinek 	if (zonecfg_get_brand(rem_handle, target_brand, sizeof (target_brand))
45083172Sgjelinek 	    != Z_OK) {
45093172Sgjelinek 		zerror(gettext("missing or invalid brand"));
45103172Sgjelinek 		exit(Z_ERR);
45113172Sgjelinek 	}
45122078Sgjelinek 
45132078Sgjelinek done:
45142078Sgjelinek 	zonecfg_fini_handle(local_handle);
45152078Sgjelinek 	zonecfg_fini_handle(rem_handle);
45167089Sgjelinek 	(void) close(fd);
45172078Sgjelinek 
45182078Sgjelinek 	return ((res == Z_OK) ? Z_OK : Z_ERR);
45192078Sgjelinek }
45202078Sgjelinek 
45215829Sgjelinek /* ARGSUSED */
45221507Sgjelinek static int
attach_func(int argc,char * argv[])45231507Sgjelinek attach_func(int argc, char *argv[])
45241507Sgjelinek {
45258301Sgerald.jelinek@sun.com 	int lockfd = -1;
45261507Sgjelinek 	int err, arg;
45271507Sgjelinek 	boolean_t force = B_FALSE;
45281507Sgjelinek 	zone_dochandle_t handle;
45291507Sgjelinek 	char zonepath[MAXPATHLEN];
45304785Sgjelinek 	char cmdbuf[MAXPATHLEN];
45317089Sgjelinek 	char postcmdbuf[MAXPATHLEN];
45322078Sgjelinek 	boolean_t execute = B_TRUE;
45337089Sgjelinek 	boolean_t brand_help = B_FALSE;
45342078Sgjelinek 	char *manifest_path;
45357089Sgjelinek 	char tmpmanifest[80];
45367089Sgjelinek 	int manifest_pos;
45374785Sgjelinek 	brand_handle_t bh = NULL;
45387089Sgjelinek 	int status;
45398759Sgerald.jelinek@sun.com 	int last_index = 0;
45408759Sgerald.jelinek@sun.com 	int offset;
45418759Sgerald.jelinek@sun.com 	char *up;
45428759Sgerald.jelinek@sun.com 	boolean_t forced_update = B_FALSE;
45431507Sgjelinek 
45441507Sgjelinek 	if (zonecfg_in_alt_root()) {
45451507Sgjelinek 		zerror(gettext("cannot attach zone in alternate root"));
45461507Sgjelinek 		return (Z_ERR);
45471507Sgjelinek 	}
45481507Sgjelinek 
45497089Sgjelinek 	/* Check the argv string for args we handle internally */
45501507Sgjelinek 	optind = 0;
45517089Sgjelinek 	opterr = 0;
45528759Sgerald.jelinek@sun.com 	while ((arg = getopt(argc, argv, "?Fn:U")) != EOF) {
45531507Sgjelinek 		switch (arg) {
45541507Sgjelinek 		case '?':
45557089Sgjelinek 			if (optopt == '?') {
45567089Sgjelinek 				sub_usage(SHELP_ATTACH, CMD_ATTACH);
45577089Sgjelinek 				brand_help = B_TRUE;
45587089Sgjelinek 			}
45597089Sgjelinek 			/* Ignore unknown options - may be brand specific. */
45607089Sgjelinek 			break;
45611507Sgjelinek 		case 'F':
45621507Sgjelinek 			force = B_TRUE;
45631507Sgjelinek 			break;
45642078Sgjelinek 		case 'n':
45652078Sgjelinek 			execute = B_FALSE;
45662078Sgjelinek 			manifest_path = optarg;
45677089Sgjelinek 			manifest_pos = optind - 1;
45685829Sgjelinek 			break;
45698759Sgerald.jelinek@sun.com 		case 'U':
45708759Sgerald.jelinek@sun.com 			/*
45718759Sgerald.jelinek@sun.com 			 * Undocumented 'force update' option for p2v update on
45728759Sgerald.jelinek@sun.com 			 * attach when zone is in the incomplete state.  Change
45738759Sgerald.jelinek@sun.com 			 * the option back to 'u' and set forced_update flag.
45748759Sgerald.jelinek@sun.com 			 */
45758759Sgerald.jelinek@sun.com 			if (optind == last_index)
45768759Sgerald.jelinek@sun.com 				offset = optind;
45778759Sgerald.jelinek@sun.com 			else
45788759Sgerald.jelinek@sun.com 				offset = optind - 1;
45798759Sgerald.jelinek@sun.com 			if ((up = index(argv[offset], 'U')) != NULL)
45808759Sgerald.jelinek@sun.com 				*up = 'u';
45818759Sgerald.jelinek@sun.com 			forced_update = B_TRUE;
45828759Sgerald.jelinek@sun.com 			break;
45831507Sgjelinek 		default:
45847089Sgjelinek 			/* Ignore unknown options - may be brand specific. */
45857089Sgjelinek 			break;
45861507Sgjelinek 		}
45878759Sgerald.jelinek@sun.com 		last_index = optind;
45881507Sgjelinek 	}
45892078Sgjelinek 
45907089Sgjelinek 	if (brand_help) {
45917089Sgjelinek 		force = B_FALSE;
45927089Sgjelinek 		execute = B_TRUE;
45937089Sgjelinek 	}
45947089Sgjelinek 
45957089Sgjelinek 	/* dry-run and force flags are mutually exclusive */
45967089Sgjelinek 	if (!execute && force) {
45977089Sgjelinek 		zerror(gettext("-F and -n flags are mutually exclusive"));
45985829Sgjelinek 		return (Z_ERR);
45995829Sgjelinek 	}
46005829Sgjelinek 
46012078Sgjelinek 	/*
46027089Sgjelinek 	 * If the no-execute option was specified, we don't do validation and
46037089Sgjelinek 	 * need to figure out the brand, since there is no zone required to be
46042078Sgjelinek 	 * configured for this option.
46052078Sgjelinek 	 */
46067089Sgjelinek 	if (execute) {
46077089Sgjelinek 		if (!brand_help) {
46087089Sgjelinek 			if (sanity_check(target_zone, CMD_ATTACH, B_FALSE,
46098759Sgerald.jelinek@sun.com 			    B_TRUE, forced_update) != Z_OK)
46107089Sgjelinek 				return (Z_ERR);
46117089Sgjelinek 			if (verify_details(CMD_ATTACH, argv) != Z_OK)
46127089Sgjelinek 				return (Z_ERR);
46137089Sgjelinek 		}
46147089Sgjelinek 
46157089Sgjelinek 		if ((err = zone_get_zonepath(target_zone, zonepath,
46167089Sgjelinek 		    sizeof (zonepath))) != Z_OK) {
46177089Sgjelinek 			errno = err;
46187089Sgjelinek 			zperror2(target_zone,
46197089Sgjelinek 			    gettext("could not get zone path"));
46207089Sgjelinek 			return (Z_ERR);
46217089Sgjelinek 		}
46227089Sgjelinek 	} else {
46237089Sgjelinek 		if (dryrun_get_brand(manifest_path, tmpmanifest,
46247089Sgjelinek 		    sizeof (tmpmanifest)) != Z_OK)
46257089Sgjelinek 			return (Z_ERR);
46267089Sgjelinek 
46277089Sgjelinek 		argv[manifest_pos] = tmpmanifest;
46287089Sgjelinek 		target_zone = "-";
46297089Sgjelinek 		(void) strlcpy(zonepath, "-", sizeof (zonepath));
46307089Sgjelinek 
46317089Sgjelinek 		/* Run the brand's verify_adm hook. */
46327089Sgjelinek 		if (verify_brand(NULL, CMD_ATTACH, argv) != Z_OK)
46337089Sgjelinek 			return (Z_ERR);
46347089Sgjelinek 	}
46357089Sgjelinek 
46367089Sgjelinek 	/*
46377089Sgjelinek 	 * Fetch the attach and postattach hooks from the brand configuration.
46387089Sgjelinek 	 */
46394785Sgjelinek 	if ((bh = brand_open(target_brand)) == NULL) {
46404785Sgjelinek 		zerror(gettext("missing or invalid brand"));
46414785Sgjelinek 		return (Z_ERR);
46424785Sgjelinek 	}
46434785Sgjelinek 
46447089Sgjelinek 	if (get_hook(bh, cmdbuf, sizeof (cmdbuf), brand_get_attach, target_zone,
46457089Sgjelinek 	    zonepath) != Z_OK) {
46467089Sgjelinek 		zerror("invalid brand configuration: missing attach resource");
46477089Sgjelinek 		brand_close(bh);
46487089Sgjelinek 		return (Z_ERR);
46497089Sgjelinek 	}
46507089Sgjelinek 
46517089Sgjelinek 	if (get_hook(bh, postcmdbuf, sizeof (postcmdbuf), brand_get_postattach,
46527089Sgjelinek 	    target_zone, zonepath) != Z_OK) {
46534785Sgjelinek 		zerror("invalid brand configuration: missing postattach "
46544785Sgjelinek 		    "resource");
46554785Sgjelinek 		brand_close(bh);
46564785Sgjelinek 		return (Z_ERR);
46574785Sgjelinek 	}
46584785Sgjelinek 	brand_close(bh);
46594785Sgjelinek 
46607089Sgjelinek 	/* Append all options to attach hook. */
46617089Sgjelinek 	if (addoptions(cmdbuf, argv, sizeof (cmdbuf)) != Z_OK)
46627089Sgjelinek 		return (Z_ERR);
46637089Sgjelinek 
46647089Sgjelinek 	/* Append all options to postattach hook. */
46657089Sgjelinek 	if (addoptions(postcmdbuf, argv, sizeof (postcmdbuf)) != Z_OK)
46667089Sgjelinek 		return (Z_ERR);
46677089Sgjelinek 
46687089Sgjelinek 	if (execute && !brand_help) {
46697089Sgjelinek 		if (zonecfg_grab_lock_file(target_zone, &lockfd) != Z_OK) {
46707089Sgjelinek 			zerror(gettext("another %s may have an operation in "
46717089Sgjelinek 			    "progress."), "zoneadm");
46727089Sgjelinek 			return (Z_ERR);
46733777Sgjelinek 		}
46748301Sgerald.jelinek@sun.com 	}
46758301Sgerald.jelinek@sun.com 
46768301Sgerald.jelinek@sun.com 	if (!force) {
46778301Sgerald.jelinek@sun.com 		/*
46788301Sgerald.jelinek@sun.com 		 * Not a force-attach, so we need to actually do the work.
46798301Sgerald.jelinek@sun.com 		 */
46808301Sgerald.jelinek@sun.com 		if (cmdbuf[0] != '\0') {
46818301Sgerald.jelinek@sun.com 			/* Run the attach hook */
46828759Sgerald.jelinek@sun.com 			status = do_subproc(cmdbuf);
46838301Sgerald.jelinek@sun.com 			if ((status = subproc_status(gettext("brand-specific "
46848301Sgerald.jelinek@sun.com 			    "attach"), status, B_FALSE)) != ZONE_SUBPROC_OK) {
46858301Sgerald.jelinek@sun.com 				if (status == ZONE_SUBPROC_USAGE && !brand_help)
46868301Sgerald.jelinek@sun.com 					sub_usage(SHELP_ATTACH, CMD_ATTACH);
46878301Sgerald.jelinek@sun.com 
46888301Sgerald.jelinek@sun.com 				if (execute && !brand_help) {
46898759Sgerald.jelinek@sun.com 					assert(zonecfg_lock_file_held(&lockfd));
46908301Sgerald.jelinek@sun.com 					zonecfg_release_lock_file(target_zone,
46918301Sgerald.jelinek@sun.com 					    lockfd);
46928301Sgerald.jelinek@sun.com 					lockfd = -1;
46938301Sgerald.jelinek@sun.com 				}
46948301Sgerald.jelinek@sun.com 
46958301Sgerald.jelinek@sun.com 				assert(lockfd == -1);
46968301Sgerald.jelinek@sun.com 				return (Z_ERR);
46978301Sgerald.jelinek@sun.com 			}
46988301Sgerald.jelinek@sun.com 		}
46998301Sgerald.jelinek@sun.com 
47008301Sgerald.jelinek@sun.com 		/*
47018301Sgerald.jelinek@sun.com 		 * Else run the built-in attach support.
47028301Sgerald.jelinek@sun.com 		 * This is a no-op since there is nothing to validate.
47038301Sgerald.jelinek@sun.com 		 */
47048301Sgerald.jelinek@sun.com 
47058301Sgerald.jelinek@sun.com 		/* If dry-run or help, then we're done. */
47068301Sgerald.jelinek@sun.com 		if (!execute || brand_help) {
47078301Sgerald.jelinek@sun.com 			if (!execute)
47088301Sgerald.jelinek@sun.com 				(void) unlink(tmpmanifest);
47098301Sgerald.jelinek@sun.com 			assert(lockfd == -1);
47108301Sgerald.jelinek@sun.com 			return (Z_OK);
47118301Sgerald.jelinek@sun.com 		}
47128301Sgerald.jelinek@sun.com 	}
47138301Sgerald.jelinek@sun.com 
47149128Sgerald.jelinek@sun.com 	/* Now we can validate that the zonepath exists. */
47159128Sgerald.jelinek@sun.com 	if (validate_zonepath(zonepath, CMD_ATTACH) != Z_OK) {
47169128Sgerald.jelinek@sun.com 		(void) fprintf(stderr, gettext("could not verify zonepath %s "
47179128Sgerald.jelinek@sun.com 		    "because of the above errors.\n"), zonepath);
47189128Sgerald.jelinek@sun.com 
47199128Sgerald.jelinek@sun.com 		assert(zonecfg_lock_file_held(&lockfd));
47209128Sgerald.jelinek@sun.com 		zonecfg_release_lock_file(target_zone, lockfd);
47219128Sgerald.jelinek@sun.com 		return (Z_ERR);
47229128Sgerald.jelinek@sun.com 	}
47239128Sgerald.jelinek@sun.com 
47248301Sgerald.jelinek@sun.com 	if ((handle = zonecfg_init_handle()) == NULL) {
47258301Sgerald.jelinek@sun.com 		zperror(cmd_to_str(CMD_ATTACH), B_TRUE);
47268301Sgerald.jelinek@sun.com 		err = Z_ERR;
47278301Sgerald.jelinek@sun.com 	} else if ((err = zonecfg_get_handle(target_zone, handle)) != Z_OK) {
47288301Sgerald.jelinek@sun.com 		errno = err;
47298301Sgerald.jelinek@sun.com 		zperror(cmd_to_str(CMD_ATTACH), B_TRUE);
47308301Sgerald.jelinek@sun.com 		zonecfg_fini_handle(handle);
47318301Sgerald.jelinek@sun.com 	} else {
47328301Sgerald.jelinek@sun.com 		zonecfg_rm_detached(handle, force);
47338301Sgerald.jelinek@sun.com 		zonecfg_fini_handle(handle);
47348301Sgerald.jelinek@sun.com 	}
47358301Sgerald.jelinek@sun.com 
47368301Sgerald.jelinek@sun.com 	if (err == Z_OK &&
47378301Sgerald.jelinek@sun.com 	    (err = zone_set_state(target_zone, ZONE_STATE_INSTALLED)) != Z_OK) {
47381507Sgjelinek 		errno = err;
47391507Sgjelinek 		zperror(gettext("could not reset state"), B_TRUE);
47401507Sgjelinek 	}
47411507Sgjelinek 
47428759Sgerald.jelinek@sun.com 	assert(zonecfg_lock_file_held(&lockfd));
47437089Sgjelinek 	zonecfg_release_lock_file(target_zone, lockfd);
47448301Sgerald.jelinek@sun.com 	lockfd = -1;
47451507Sgjelinek 
47464785Sgjelinek 	/* If we have a brand postattach hook, run it. */
47477089Sgjelinek 	if (err == Z_OK && !force && postcmdbuf[0] != '\0') {
47487089Sgjelinek 		status = do_subproc(postcmdbuf);
47494785Sgjelinek 		if (subproc_status(gettext("brand-specific postattach"),
47504785Sgjelinek 		    status, B_FALSE) != ZONE_SUBPROC_OK) {
47514785Sgjelinek 			if ((err = zone_set_state(target_zone,
47524785Sgjelinek 			    ZONE_STATE_CONFIGURED)) != Z_OK) {
47534785Sgjelinek 				errno = err;
47544785Sgjelinek 				zperror(gettext("could not reset state"),
47554785Sgjelinek 				    B_TRUE);
47564785Sgjelinek 			}
47574785Sgjelinek 		}
47584785Sgjelinek 	}
47594785Sgjelinek 
47608301Sgerald.jelinek@sun.com 	assert(lockfd == -1);
47611507Sgjelinek 	return ((err == Z_OK) ? Z_OK : Z_ERR);
47621507Sgjelinek }
47631507Sgjelinek 
47641300Sgjelinek /*
47650Sstevel@tonic-gate  * On input, TRUE => yes, FALSE => no.
47660Sstevel@tonic-gate  * On return, TRUE => 1, FALSE => 0, could not ask => -1.
47670Sstevel@tonic-gate  */
47680Sstevel@tonic-gate 
47690Sstevel@tonic-gate static int
ask_yesno(boolean_t default_answer,const char * question)47700Sstevel@tonic-gate ask_yesno(boolean_t default_answer, const char *question)
47710Sstevel@tonic-gate {
47720Sstevel@tonic-gate 	char line[64];	/* should be large enough to answer yes or no */
47730Sstevel@tonic-gate 
47740Sstevel@tonic-gate 	if (!isatty(STDIN_FILENO))
47750Sstevel@tonic-gate 		return (-1);
47760Sstevel@tonic-gate 	for (;;) {
47770Sstevel@tonic-gate 		(void) printf("%s (%s)? ", question,
47780Sstevel@tonic-gate 		    default_answer ? "[y]/n" : "y/[n]");
47790Sstevel@tonic-gate 		if (fgets(line, sizeof (line), stdin) == NULL ||
47800Sstevel@tonic-gate 		    line[0] == '\n')
47810Sstevel@tonic-gate 			return (default_answer ? 1 : 0);
47820Sstevel@tonic-gate 		if (tolower(line[0]) == 'y')
47830Sstevel@tonic-gate 			return (1);
47840Sstevel@tonic-gate 		if (tolower(line[0]) == 'n')
47850Sstevel@tonic-gate 			return (0);
47860Sstevel@tonic-gate 	}
47870Sstevel@tonic-gate }
47880Sstevel@tonic-gate 
47897089Sgjelinek /* ARGSUSED */
47900Sstevel@tonic-gate static int
uninstall_func(int argc,char * argv[])47910Sstevel@tonic-gate uninstall_func(int argc, char *argv[])
47920Sstevel@tonic-gate {
47930Sstevel@tonic-gate 	char line[ZONENAME_MAX + 128];	/* Enough for "Are you sure ..." */
47941867Sgjelinek 	char rootpath[MAXPATHLEN], zonepath[MAXPATHLEN];
47954785Sgjelinek 	char cmdbuf[MAXPATHLEN];
47967089Sgjelinek 	char precmdbuf[MAXPATHLEN];
47970Sstevel@tonic-gate 	boolean_t force = B_FALSE;
47980Sstevel@tonic-gate 	int lockfd, answer;
47990Sstevel@tonic-gate 	int err, arg;
48007089Sgjelinek 	boolean_t brand_help = B_FALSE;
48014785Sgjelinek 	brand_handle_t bh = NULL;
48027089Sgjelinek 	int status;
48030Sstevel@tonic-gate 
4804766Scarlsonj 	if (zonecfg_in_alt_root()) {
4805766Scarlsonj 		zerror(gettext("cannot uninstall zone in alternate root"));
4806766Scarlsonj 		return (Z_ERR);
4807766Scarlsonj 	}
4808766Scarlsonj 
48097089Sgjelinek 	/* Check the argv string for args we handle internally */
48100Sstevel@tonic-gate 	optind = 0;
48117089Sgjelinek 	opterr = 0;
48120Sstevel@tonic-gate 	while ((arg = getopt(argc, argv, "?F")) != EOF) {
48130Sstevel@tonic-gate 		switch (arg) {
48140Sstevel@tonic-gate 		case '?':
48157089Sgjelinek 			if (optopt == '?') {
48167089Sgjelinek 				sub_usage(SHELP_UNINSTALL, CMD_UNINSTALL);
48177089Sgjelinek 				brand_help = B_TRUE;
48187089Sgjelinek 			}
48197089Sgjelinek 			/* Ignore unknown options - may be brand specific. */
48207089Sgjelinek 			break;
48210Sstevel@tonic-gate 		case 'F':
48220Sstevel@tonic-gate 			force = B_TRUE;
48230Sstevel@tonic-gate 			break;
48240Sstevel@tonic-gate 		default:
48257089Sgjelinek 			/* Ignore unknown options - may be brand specific. */
48267089Sgjelinek 			break;
48270Sstevel@tonic-gate 		}
48280Sstevel@tonic-gate 	}
48297089Sgjelinek 
48307089Sgjelinek 	if (!brand_help) {
48317089Sgjelinek 		if (sanity_check(target_zone, CMD_UNINSTALL, B_FALSE, B_TRUE,
48327089Sgjelinek 		    B_FALSE) != Z_OK)
48337089Sgjelinek 			return (Z_ERR);
48347089Sgjelinek 
48357089Sgjelinek 		/*
48367089Sgjelinek 		 * Invoke brand-specific handler.
48377089Sgjelinek 		 */
48387089Sgjelinek 		if (invoke_brand_handler(CMD_UNINSTALL, argv) != Z_OK)
48390Sstevel@tonic-gate 			return (Z_ERR);
48407089Sgjelinek 
48417089Sgjelinek 		if (!force) {
48427089Sgjelinek 			(void) snprintf(line, sizeof (line),
48437089Sgjelinek 			    gettext("Are you sure you want to %s zone %s"),
48447089Sgjelinek 			    cmd_to_str(CMD_UNINSTALL), target_zone);
48457089Sgjelinek 			if ((answer = ask_yesno(B_FALSE, line)) == 0) {
48467089Sgjelinek 				return (Z_OK);
48477089Sgjelinek 			} else if (answer == -1) {
48487089Sgjelinek 				zerror(gettext("Input not from terminal and -F "
48497089Sgjelinek 				    "not specified: %s not done."),
48507089Sgjelinek 				    cmd_to_str(CMD_UNINSTALL));
48517089Sgjelinek 				return (Z_ERR);
48527089Sgjelinek 			}
48530Sstevel@tonic-gate 		}
48540Sstevel@tonic-gate 	}
48550Sstevel@tonic-gate 
48561867Sgjelinek 	if ((err = zone_get_zonepath(target_zone, zonepath,
48571867Sgjelinek 	    sizeof (zonepath))) != Z_OK) {
48580Sstevel@tonic-gate 		errno = err;
48590Sstevel@tonic-gate 		zperror2(target_zone, gettext("could not get zone path"));
48600Sstevel@tonic-gate 		return (Z_ERR);
48610Sstevel@tonic-gate 	}
48620Sstevel@tonic-gate 
48630Sstevel@tonic-gate 	/*
48647089Sgjelinek 	 * Fetch the uninstall and preuninstall hooks from the brand
48657089Sgjelinek 	 * configuration.
48660Sstevel@tonic-gate 	 */
48674785Sgjelinek 	if ((bh = brand_open(target_brand)) == NULL) {
48684785Sgjelinek 		zerror(gettext("missing or invalid brand"));
48694785Sgjelinek 		return (Z_ERR);
48704785Sgjelinek 	}
48714785Sgjelinek 
48727089Sgjelinek 	if (get_hook(bh, cmdbuf, sizeof (cmdbuf), brand_get_uninstall,
48737089Sgjelinek 	    target_zone, zonepath) != Z_OK) {
48747089Sgjelinek 		zerror("invalid brand configuration: missing uninstall "
48757089Sgjelinek 		    "resource");
48767089Sgjelinek 		brand_close(bh);
48777089Sgjelinek 		return (Z_ERR);
48787089Sgjelinek 	}
48797089Sgjelinek 
48807089Sgjelinek 	if (get_hook(bh, precmdbuf, sizeof (precmdbuf), brand_get_preuninstall,
48817089Sgjelinek 	    target_zone, zonepath) != Z_OK) {
48824785Sgjelinek 		zerror("invalid brand configuration: missing preuninstall "
48834785Sgjelinek 		    "resource");
48844785Sgjelinek 		brand_close(bh);
48854785Sgjelinek 		return (Z_ERR);
48864785Sgjelinek 	}
48874785Sgjelinek 	brand_close(bh);
48884785Sgjelinek 
48897089Sgjelinek 	/* Append all options to preuninstall hook. */
48907089Sgjelinek 	if (addoptions(precmdbuf, argv, sizeof (precmdbuf)) != Z_OK)
48917089Sgjelinek 		return (Z_ERR);
48927089Sgjelinek 
48937089Sgjelinek 	/* Append all options to uninstall hook. */
48947089Sgjelinek 	if (addoptions(cmdbuf, argv, sizeof (cmdbuf)) != Z_OK)
48957089Sgjelinek 		return (Z_ERR);
48967089Sgjelinek 
48977089Sgjelinek 	if (!brand_help) {
48987089Sgjelinek 		if ((err = zone_get_rootpath(target_zone, rootpath,
48997089Sgjelinek 		    sizeof (rootpath))) != Z_OK) {
49007089Sgjelinek 			errno = err;
49017089Sgjelinek 			zperror2(target_zone, gettext("could not get root "
49027089Sgjelinek 			    "path"));
49034785Sgjelinek 			return (Z_ERR);
49044785Sgjelinek 		}
49054785Sgjelinek 
49067089Sgjelinek 		/*
49077089Sgjelinek 		 * If there seems to be a zoneadmd running for this zone, call
49087089Sgjelinek 		 * it to tell it that an uninstall is happening; if all goes
49097089Sgjelinek 		 * well it will then shut itself down.
49107089Sgjelinek 		 */
49117089Sgjelinek 		if (zonecfg_ping_zoneadmd(target_zone) == Z_OK) {
49127089Sgjelinek 			zone_cmd_arg_t zarg;
49137089Sgjelinek 			zarg.cmd = Z_NOTE_UNINSTALLING;
49147089Sgjelinek 			/* we don't care too much if this fails, just plow on */
49157089Sgjelinek 			(void) zonecfg_call_zoneadmd(target_zone, &zarg, locale,
49167089Sgjelinek 			    B_TRUE);
49177089Sgjelinek 		}
49187089Sgjelinek 
49197089Sgjelinek 		if (zonecfg_grab_lock_file(target_zone, &lockfd) != Z_OK) {
49207089Sgjelinek 			zerror(gettext("another %s may have an operation in "
49217089Sgjelinek 			    "progress."), "zoneadm");
49227089Sgjelinek 			return (Z_ERR);
49237089Sgjelinek 		}
49247089Sgjelinek 
49257089Sgjelinek 		/* Don't uninstall the zone if anything is mounted there */
49267089Sgjelinek 		err = zonecfg_find_mounts(rootpath, NULL, NULL);
49277089Sgjelinek 		if (err) {
49287089Sgjelinek 			zerror(gettext("These file systems are mounted on "
49297089Sgjelinek 			    "subdirectories of %s.\n"), rootpath);
49307089Sgjelinek 			(void) zonecfg_find_mounts(rootpath, zfm_print, NULL);
49317089Sgjelinek 			zonecfg_release_lock_file(target_zone, lockfd);
49327089Sgjelinek 			return (Z_ERR);
49337089Sgjelinek 		}
49347089Sgjelinek 	}
49357089Sgjelinek 
49367089Sgjelinek 	/* If we have a brand preuninstall hook, run it. */
49377089Sgjelinek 	if (!brand_help && precmdbuf[0] != '\0') {
493811771Sgerald.jelinek@sun.com 		status = do_subproc(precmdbuf);
49394785Sgjelinek 		if (subproc_status(gettext("brand-specific preuninstall"),
49404785Sgjelinek 		    status, B_FALSE) != ZONE_SUBPROC_OK) {
49417089Sgjelinek 			zonecfg_release_lock_file(target_zone, lockfd);
49424785Sgjelinek 			return (Z_ERR);
49434785Sgjelinek 		}
49444785Sgjelinek 	}
49454785Sgjelinek 
49467089Sgjelinek 	if (!brand_help) {
49477089Sgjelinek 		err = zone_set_state(target_zone, ZONE_STATE_INCOMPLETE);
49487089Sgjelinek 		if (err != Z_OK) {
49497089Sgjelinek 			errno = err;
49507089Sgjelinek 			zperror2(target_zone, gettext("could not set state"));
49517089Sgjelinek 			goto bad;
49527089Sgjelinek 		}
49537089Sgjelinek 	}
49547089Sgjelinek 
49557089Sgjelinek 	/*
49567089Sgjelinek 	 * If there is a brand uninstall hook, use it, otherwise use the
49577089Sgjelinek 	 * built-in uninstall code.
49587089Sgjelinek 	 */
49597089Sgjelinek 	if (cmdbuf[0] != '\0') {
49607089Sgjelinek 		/* Run the uninstall hook */
49619310Sgerald.jelinek@sun.com 		status = do_subproc(cmdbuf);
49627089Sgjelinek 		if ((status = subproc_status(gettext("brand-specific "
49637089Sgjelinek 		    "uninstall"), status, B_FALSE)) != ZONE_SUBPROC_OK) {
49647089Sgjelinek 			if (status == ZONE_SUBPROC_USAGE && !brand_help)
49657089Sgjelinek 				sub_usage(SHELP_UNINSTALL, CMD_UNINSTALL);
49667089Sgjelinek 			if (!brand_help)
49677089Sgjelinek 				zonecfg_release_lock_file(target_zone, lockfd);
49687089Sgjelinek 			return (Z_ERR);
49697089Sgjelinek 		}
49707089Sgjelinek 
49717089Sgjelinek 		if (brand_help)
49727089Sgjelinek 			return (Z_OK);
49737089Sgjelinek 	} else {
49747089Sgjelinek 		/* If just help, we're done since there is no brand help. */
49757089Sgjelinek 		if (brand_help)
49767089Sgjelinek 			return (Z_OK);
49777089Sgjelinek 
49787089Sgjelinek 		/* Run the built-in uninstall support. */
49797089Sgjelinek 		if ((err = cleanup_zonepath(zonepath, B_FALSE)) != Z_OK) {
49807089Sgjelinek 			errno = err;
49817089Sgjelinek 			zperror2(target_zone, gettext("cleaning up zonepath "
49827089Sgjelinek 			    "failed"));
49837089Sgjelinek 			goto bad;
49847089Sgjelinek 		}
49851867Sgjelinek 	}
49861867Sgjelinek 
49870Sstevel@tonic-gate 	err = zone_set_state(target_zone, ZONE_STATE_CONFIGURED);
49880Sstevel@tonic-gate 	if (err != Z_OK) {
49890Sstevel@tonic-gate 		errno = err;
49900Sstevel@tonic-gate 		zperror2(target_zone, gettext("could not reset state"));
49910Sstevel@tonic-gate 	}
49920Sstevel@tonic-gate bad:
49937089Sgjelinek 	zonecfg_release_lock_file(target_zone, lockfd);
49940Sstevel@tonic-gate 	return (err);
49950Sstevel@tonic-gate }
49960Sstevel@tonic-gate 
4997766Scarlsonj /* ARGSUSED */
4998766Scarlsonj static int
mount_func(int argc,char * argv[])4999766Scarlsonj mount_func(int argc, char *argv[])
5000766Scarlsonj {
5001766Scarlsonj 	zone_cmd_arg_t zarg;
50022712Snn35248 	boolean_t force = B_FALSE;
50032712Snn35248 	int arg;
50042712Snn35248 
50052712Snn35248 	/*
50062712Snn35248 	 * The only supported subargument to the "mount" subcommand is
50072712Snn35248 	 * "-f", which forces us to mount a zone in the INCOMPLETE state.
50082712Snn35248 	 */
50092712Snn35248 	optind = 0;
50102712Snn35248 	if ((arg = getopt(argc, argv, "f")) != EOF) {
50112712Snn35248 		switch (arg) {
50122712Snn35248 		case 'f':
50132712Snn35248 			force = B_TRUE;
50142712Snn35248 			break;
50152712Snn35248 		default:
50162712Snn35248 			return (Z_USAGE);
50172712Snn35248 		}
50182712Snn35248 	}
50192712Snn35248 	if (argc > optind)
5020766Scarlsonj 		return (Z_USAGE);
50212712Snn35248 
50222712Snn35248 	if (sanity_check(target_zone, CMD_MOUNT, B_FALSE, B_FALSE, force)
50232712Snn35248 	    != Z_OK)
5024766Scarlsonj 		return (Z_ERR);
50253339Szt129084 	if (verify_details(CMD_MOUNT, argv) != Z_OK)
5026766Scarlsonj 		return (Z_ERR);
5027766Scarlsonj 
50282712Snn35248 	zarg.cmd = force ? Z_FORCEMOUNT : Z_MOUNT;
50295829Sgjelinek 	zarg.bootbuf[0] = '\0';
50307089Sgjelinek 	if (zonecfg_call_zoneadmd(target_zone, &zarg, locale, B_TRUE) != 0) {
5031766Scarlsonj 		zerror(gettext("call to %s failed"), "zoneadmd");
5032766Scarlsonj 		return (Z_ERR);
5033766Scarlsonj 	}
5034766Scarlsonj 	return (Z_OK);
5035766Scarlsonj }
5036766Scarlsonj 
5037766Scarlsonj /* ARGSUSED */
5038766Scarlsonj static int
unmount_func(int argc,char * argv[])5039766Scarlsonj unmount_func(int argc, char *argv[])
5040766Scarlsonj {
5041766Scarlsonj 	zone_cmd_arg_t zarg;
5042766Scarlsonj 
5043766Scarlsonj 	if (argc > 0)
5044766Scarlsonj 		return (Z_USAGE);
50452712Snn35248 	if (sanity_check(target_zone, CMD_UNMOUNT, B_FALSE, B_FALSE, B_FALSE)
50462712Snn35248 	    != Z_OK)
5047766Scarlsonj 		return (Z_ERR);
5048766Scarlsonj 
5049766Scarlsonj 	zarg.cmd = Z_UNMOUNT;
50507089Sgjelinek 	if (zonecfg_call_zoneadmd(target_zone, &zarg, locale, B_TRUE) != 0) {
5051766Scarlsonj 		zerror(gettext("call to %s failed"), "zoneadmd");
5052766Scarlsonj 		return (Z_ERR);
5053766Scarlsonj 	}
5054766Scarlsonj 	return (Z_OK);
5055766Scarlsonj }
5056766Scarlsonj 
50570Sstevel@tonic-gate static int
mark_func(int argc,char * argv[])50582303Scarlsonj mark_func(int argc, char *argv[])
50592303Scarlsonj {
50602303Scarlsonj 	int err, lockfd;
50618759Sgerald.jelinek@sun.com 	int arg;
50628759Sgerald.jelinek@sun.com 	boolean_t force = B_FALSE;
50638759Sgerald.jelinek@sun.com 	int state;
50648759Sgerald.jelinek@sun.com 
50658759Sgerald.jelinek@sun.com 	optind = 0;
50668759Sgerald.jelinek@sun.com 	opterr = 0;
50678759Sgerald.jelinek@sun.com 	while ((arg = getopt(argc, argv, "F")) != EOF) {
50688759Sgerald.jelinek@sun.com 		switch (arg) {
50698759Sgerald.jelinek@sun.com 		case 'F':
50708759Sgerald.jelinek@sun.com 			force = B_TRUE;
50718759Sgerald.jelinek@sun.com 			break;
50728759Sgerald.jelinek@sun.com 		default:
50738759Sgerald.jelinek@sun.com 			return (Z_USAGE);
50748759Sgerald.jelinek@sun.com 		}
50758759Sgerald.jelinek@sun.com 	}
50768759Sgerald.jelinek@sun.com 
50778759Sgerald.jelinek@sun.com 	if (argc != (optind + 1))
50782303Scarlsonj 		return (Z_USAGE);
50798759Sgerald.jelinek@sun.com 
50808759Sgerald.jelinek@sun.com 	if (strcmp(argv[optind], "configured") == 0)
50818759Sgerald.jelinek@sun.com 		state = ZONE_STATE_CONFIGURED;
50828759Sgerald.jelinek@sun.com 	else if (strcmp(argv[optind], "incomplete") == 0)
50838759Sgerald.jelinek@sun.com 		state = ZONE_STATE_INCOMPLETE;
50848759Sgerald.jelinek@sun.com 	else if (strcmp(argv[optind], "installed") == 0)
50858759Sgerald.jelinek@sun.com 		state = ZONE_STATE_INSTALLED;
50868759Sgerald.jelinek@sun.com 	else
50878759Sgerald.jelinek@sun.com 		return (Z_USAGE);
50888759Sgerald.jelinek@sun.com 
50898759Sgerald.jelinek@sun.com 	if (state != ZONE_STATE_INCOMPLETE && !force)
50908759Sgerald.jelinek@sun.com 		return (Z_USAGE);
50918759Sgerald.jelinek@sun.com 
50928759Sgerald.jelinek@sun.com 	if (sanity_check(target_zone, CMD_MARK, B_FALSE, B_TRUE, B_FALSE)
50932712Snn35248 	    != Z_OK)
50942303Scarlsonj 		return (Z_ERR);
50952303Scarlsonj 
50963339Szt129084 	/*
50973339Szt129084 	 * Invoke brand-specific handler.
50983339Szt129084 	 */
50993339Szt129084 	if (invoke_brand_handler(CMD_MARK, argv) != Z_OK)
51003339Szt129084 		return (Z_ERR);
51013339Szt129084 
51027089Sgjelinek 	if (zonecfg_grab_lock_file(target_zone, &lockfd) != Z_OK) {
51032303Scarlsonj 		zerror(gettext("another %s may have an operation in progress."),
51042303Scarlsonj 		    "zoneadm");
51052303Scarlsonj 		return (Z_ERR);
51062303Scarlsonj 	}
51072303Scarlsonj 
51088759Sgerald.jelinek@sun.com 	err = zone_set_state(target_zone, state);
51092303Scarlsonj 	if (err != Z_OK) {
51102303Scarlsonj 		errno = err;
51112303Scarlsonj 		zperror2(target_zone, gettext("could not set state"));
51122303Scarlsonj 	}
51137089Sgjelinek 	zonecfg_release_lock_file(target_zone, lockfd);
51142303Scarlsonj 
51152303Scarlsonj 	return (err);
51162303Scarlsonj }
51172303Scarlsonj 
51183247Sgjelinek /*
51193247Sgjelinek  * Check what scheduling class we're running under and print a warning if
51203247Sgjelinek  * we're not using FSS.
51213247Sgjelinek  */
51223247Sgjelinek static int
check_sched_fss(zone_dochandle_t handle)51233247Sgjelinek check_sched_fss(zone_dochandle_t handle)
51243247Sgjelinek {
51253247Sgjelinek 	char class_name[PC_CLNMSZ];
51263247Sgjelinek 
51273247Sgjelinek 	if (zonecfg_get_dflt_sched_class(handle, class_name,
51283247Sgjelinek 	    sizeof (class_name)) != Z_OK) {
51293247Sgjelinek 		zerror(gettext("WARNING: unable to determine the zone's "
51303247Sgjelinek 		    "scheduling class"));
51313247Sgjelinek 	} else if (strcmp("FSS", class_name) != 0) {
51323247Sgjelinek 		zerror(gettext("WARNING: The zone.cpu-shares rctl is set but\n"
51333247Sgjelinek 		    "FSS is not the default scheduling class for this zone.  "
51343247Sgjelinek 		    "FSS will be\nused for processes in the zone but to get "
51353247Sgjelinek 		    "the full benefit of FSS,\nit should be the default "
51363247Sgjelinek 		    "scheduling class.  See dispadmin(1M) for\nmore details."));
51373247Sgjelinek 		return (Z_SYSTEM);
51383247Sgjelinek 	}
51393247Sgjelinek 
51403247Sgjelinek 	return (Z_OK);
51413247Sgjelinek }
51423247Sgjelinek 
51433247Sgjelinek static int
check_cpu_shares_sched(zone_dochandle_t handle)51443247Sgjelinek check_cpu_shares_sched(zone_dochandle_t handle)
51453247Sgjelinek {
51463247Sgjelinek 	int err;
51473247Sgjelinek 	int res = Z_OK;
51483247Sgjelinek 	struct zone_rctltab rctl;
51493247Sgjelinek 
51503247Sgjelinek 	if ((err = zonecfg_setrctlent(handle)) != Z_OK) {
51513247Sgjelinek 		errno = err;
51523247Sgjelinek 		zperror(cmd_to_str(CMD_APPLY), B_TRUE);
51533247Sgjelinek 		return (err);
51543247Sgjelinek 	}
51553247Sgjelinek 
51563247Sgjelinek 	while (zonecfg_getrctlent(handle, &rctl) == Z_OK) {
51573247Sgjelinek 		if (strcmp(rctl.zone_rctl_name, "zone.cpu-shares") == 0) {
51583247Sgjelinek 			if (check_sched_fss(handle) != Z_OK)
51593247Sgjelinek 				res = Z_SYSTEM;
51603247Sgjelinek 			break;
51613247Sgjelinek 		}
51623247Sgjelinek 	}
51633247Sgjelinek 
51643247Sgjelinek 	(void) zonecfg_endrctlent(handle);
51653247Sgjelinek 
51663247Sgjelinek 	return (res);
51673247Sgjelinek }
51683247Sgjelinek 
51693247Sgjelinek /*
51703352Sgjelinek  * Check if there is a mix of processes running in different pools within the
51713352Sgjelinek  * zone.  This is currently only going to be called for the global zone from
51723352Sgjelinek  * apply_func but that could be generalized in the future.
51733352Sgjelinek  */
51743352Sgjelinek static boolean_t
mixed_pools(zoneid_t zoneid)51753352Sgjelinek mixed_pools(zoneid_t zoneid)
51763352Sgjelinek {
51773352Sgjelinek 	DIR *dirp;
51783352Sgjelinek 	dirent_t *dent;
51793352Sgjelinek 	boolean_t mixed = B_FALSE;
51803352Sgjelinek 	boolean_t poolid_set = B_FALSE;
51813352Sgjelinek 	poolid_t last_poolid = 0;
51823352Sgjelinek 
51833352Sgjelinek 	if ((dirp = opendir("/proc")) == NULL) {
51843352Sgjelinek 		zerror(gettext("could not open /proc"));
51853352Sgjelinek 		return (B_FALSE);
51863352Sgjelinek 	}
51873352Sgjelinek 
51883352Sgjelinek 	while ((dent = readdir(dirp)) != NULL) {
51893352Sgjelinek 		int procfd;
51903352Sgjelinek 		psinfo_t ps;
51913352Sgjelinek 		char procpath[MAXPATHLEN];
51923352Sgjelinek 
51933352Sgjelinek 		if (dent->d_name[0] == '.')
51943352Sgjelinek 			continue;
51953352Sgjelinek 
51963352Sgjelinek 		(void) snprintf(procpath, sizeof (procpath), "/proc/%s/psinfo",
51973352Sgjelinek 		    dent->d_name);
51983352Sgjelinek 
51993352Sgjelinek 		if ((procfd = open(procpath, O_RDONLY)) == -1)
52003352Sgjelinek 			continue;
52013352Sgjelinek 
52023352Sgjelinek 		if (read(procfd, &ps, sizeof (ps)) == sizeof (psinfo_t)) {
52033352Sgjelinek 			/* skip processes in other zones and system processes */
52043352Sgjelinek 			if (zoneid != ps.pr_zoneid || ps.pr_flag & SSYS) {
52053352Sgjelinek 				(void) close(procfd);
52063352Sgjelinek 				continue;
52073352Sgjelinek 			}
52083352Sgjelinek 
52093352Sgjelinek 			if (poolid_set) {
52103352Sgjelinek 				if (ps.pr_poolid != last_poolid)
52113352Sgjelinek 					mixed = B_TRUE;
52123352Sgjelinek 			} else {
52133352Sgjelinek 				last_poolid = ps.pr_poolid;
52143352Sgjelinek 				poolid_set = B_TRUE;
52153352Sgjelinek 			}
52163352Sgjelinek 		}
52173352Sgjelinek 
52183352Sgjelinek 		(void) close(procfd);
52193352Sgjelinek 
52203352Sgjelinek 		if (mixed)
52213352Sgjelinek 			break;
52223352Sgjelinek 	}
52233352Sgjelinek 
52243352Sgjelinek 	(void) closedir(dirp);
52253352Sgjelinek 
52263352Sgjelinek 	return (mixed);
52273352Sgjelinek }
52283352Sgjelinek 
52293352Sgjelinek /*
52303352Sgjelinek  * Check if a persistent or temporary pool is configured for the zone.
52313352Sgjelinek  * This is currently only going to be called for the global zone from
52323352Sgjelinek  * apply_func but that could be generalized in the future.
52333352Sgjelinek  */
52343352Sgjelinek static boolean_t
pool_configured(zone_dochandle_t handle)52353352Sgjelinek pool_configured(zone_dochandle_t handle)
52363352Sgjelinek {
52373352Sgjelinek 	int err1, err2;
52383352Sgjelinek 	struct zone_psettab pset_tab;
52393352Sgjelinek 	char poolname[MAXPATHLEN];
52403352Sgjelinek 
52413352Sgjelinek 	err1 = zonecfg_lookup_pset(handle, &pset_tab);
52423352Sgjelinek 	err2 = zonecfg_get_pool(handle, poolname, sizeof (poolname));
52433352Sgjelinek 
52443352Sgjelinek 	if (err1 == Z_NO_ENTRY &&
52453352Sgjelinek 	    (err2 == Z_NO_ENTRY || (err2 == Z_OK && strlen(poolname) == 0)))
52463352Sgjelinek 		return (B_FALSE);
52473352Sgjelinek 
52483352Sgjelinek 	return (B_TRUE);
52493352Sgjelinek }
52503352Sgjelinek 
52513352Sgjelinek /*
52523247Sgjelinek  * This is an undocumented interface which is currently only used to apply
52533247Sgjelinek  * the global zone resource management settings when the system boots.
52543247Sgjelinek  * This function does not yet properly handle updating a running system so
52553247Sgjelinek  * any projects running in the zone would be trashed if this function
52563247Sgjelinek  * were to run after the zone had booted.  It also does not reset any
52573247Sgjelinek  * rctl settings that were removed from zonecfg.  There is still work to be
52583247Sgjelinek  * done before we can properly support dynamically updating the resource
52593247Sgjelinek  * management settings for a running zone (global or non-global).  Thus, this
52603247Sgjelinek  * functionality is undocumented for now.
52613247Sgjelinek  */
52623247Sgjelinek /* ARGSUSED */
52633247Sgjelinek static int
apply_func(int argc,char * argv[])52643247Sgjelinek apply_func(int argc, char *argv[])
52653247Sgjelinek {
52663247Sgjelinek 	int err;
52673247Sgjelinek 	int res = Z_OK;
52683247Sgjelinek 	priv_set_t *privset;
52693247Sgjelinek 	zoneid_t zoneid;
52703247Sgjelinek 	zone_dochandle_t handle;
52713247Sgjelinek 	struct zone_mcaptab mcap;
52723247Sgjelinek 	char pool_err[128];
52733247Sgjelinek 
52743247Sgjelinek 	zoneid = getzoneid();
52753247Sgjelinek 
52763247Sgjelinek 	if (zonecfg_in_alt_root() || zoneid != GLOBAL_ZONEID ||
52773247Sgjelinek 	    target_zone == NULL || strcmp(target_zone, GLOBAL_ZONENAME) != 0)
52783247Sgjelinek 		return (usage(B_FALSE));
52793247Sgjelinek 
52803247Sgjelinek 	if ((privset = priv_allocset()) == NULL) {
52813247Sgjelinek 		zerror(gettext("%s failed"), "priv_allocset");
52823247Sgjelinek 		return (Z_ERR);
52833247Sgjelinek 	}
52843247Sgjelinek 
52853247Sgjelinek 	if (getppriv(PRIV_EFFECTIVE, privset) != 0) {
52863247Sgjelinek 		zerror(gettext("%s failed"), "getppriv");
52873247Sgjelinek 		priv_freeset(privset);
52883247Sgjelinek 		return (Z_ERR);
52893247Sgjelinek 	}
52903247Sgjelinek 
52913247Sgjelinek 	if (priv_isfullset(privset) == B_FALSE) {
52923247Sgjelinek 		(void) usage(B_FALSE);
52933247Sgjelinek 		priv_freeset(privset);
52943247Sgjelinek 		return (Z_ERR);
52953247Sgjelinek 	}
52963247Sgjelinek 	priv_freeset(privset);
52973247Sgjelinek 
52983247Sgjelinek 	if ((handle = zonecfg_init_handle()) == NULL) {
52993247Sgjelinek 		zperror(cmd_to_str(CMD_APPLY), B_TRUE);
53003247Sgjelinek 		return (Z_ERR);
53013247Sgjelinek 	}
53023247Sgjelinek 
53033247Sgjelinek 	if ((err = zonecfg_get_handle(target_zone, handle)) != Z_OK) {
53043247Sgjelinek 		errno = err;
53053247Sgjelinek 		zperror(cmd_to_str(CMD_APPLY), B_TRUE);
53063247Sgjelinek 		zonecfg_fini_handle(handle);
53073247Sgjelinek 		return (Z_ERR);
53083247Sgjelinek 	}
53093247Sgjelinek 
53103247Sgjelinek 	/* specific error msgs are printed within apply_rctls */
53113247Sgjelinek 	if ((err = zonecfg_apply_rctls(target_zone, handle)) != Z_OK) {
53123247Sgjelinek 		errno = err;
53133247Sgjelinek 		zperror(cmd_to_str(CMD_APPLY), B_TRUE);
53143247Sgjelinek 		res = Z_ERR;
53153247Sgjelinek 	}
53163247Sgjelinek 
53173247Sgjelinek 	if ((err = check_cpu_shares_sched(handle)) != Z_OK)
53183247Sgjelinek 		res = Z_ERR;
53193247Sgjelinek 
53203352Sgjelinek 	if (pool_configured(handle)) {
53213352Sgjelinek 		if (mixed_pools(zoneid)) {
53223352Sgjelinek 			zerror(gettext("Zone is using multiple resource "
53233352Sgjelinek 			    "pools.  The pool\nconfiguration cannot be "
53243352Sgjelinek 			    "applied without rebooting."));
53253352Sgjelinek 			res = Z_ERR;
53263352Sgjelinek 		} else {
53273352Sgjelinek 
53283352Sgjelinek 			/*
53293352Sgjelinek 			 * The next two blocks of code attempt to set up
53303352Sgjelinek 			 * temporary pools as well as persistent pools.  In
53313352Sgjelinek 			 * both cases we call the functions unconditionally.
53323352Sgjelinek 			 * Within each funtion the code will check if the zone
53333352Sgjelinek 			 * is actually configured for a temporary pool or
53343352Sgjelinek 			 * persistent pool and just return if there is nothing
53353352Sgjelinek 			 * to do.
53363352Sgjelinek 			 */
53373352Sgjelinek 			if ((err = zonecfg_bind_tmp_pool(handle, zoneid,
53383352Sgjelinek 			    pool_err, sizeof (pool_err))) != Z_OK) {
53393352Sgjelinek 				if (err == Z_POOL || err == Z_POOL_CREATE ||
53403352Sgjelinek 				    err == Z_POOL_BIND)
53413352Sgjelinek 					zerror("%s: %s", zonecfg_strerror(err),
53423352Sgjelinek 					    pool_err);
53433352Sgjelinek 				else
53443352Sgjelinek 					zerror(gettext("could not bind zone to "
53453352Sgjelinek 					    "temporary pool: %s"),
53463352Sgjelinek 					    zonecfg_strerror(err));
53473352Sgjelinek 				res = Z_ERR;
53483352Sgjelinek 			}
53493352Sgjelinek 
53503352Sgjelinek 			if ((err = zonecfg_bind_pool(handle, zoneid, pool_err,
53513352Sgjelinek 			    sizeof (pool_err))) != Z_OK) {
53523352Sgjelinek 				if (err == Z_POOL || err == Z_POOL_BIND)
53533352Sgjelinek 					zerror("%s: %s", zonecfg_strerror(err),
53543352Sgjelinek 					    pool_err);
53553352Sgjelinek 				else
53563352Sgjelinek 					zerror("%s", zonecfg_strerror(err));
53573352Sgjelinek 			}
53583352Sgjelinek 		}
53593247Sgjelinek 	}
53603247Sgjelinek 
53613247Sgjelinek 	/*
53623247Sgjelinek 	 * If a memory cap is configured, set the cap in the kernel using
53633247Sgjelinek 	 * zone_setattr() and make sure the rcapd SMF service is enabled.
53643247Sgjelinek 	 */
53653247Sgjelinek 	if (zonecfg_getmcapent(handle, &mcap) == Z_OK) {
53663247Sgjelinek 		uint64_t num;
53673247Sgjelinek 		char smf_err[128];
53683247Sgjelinek 
53693247Sgjelinek 		num = (uint64_t)strtoll(mcap.zone_physmem_cap, NULL, 10);
53703247Sgjelinek 		if (zone_setattr(zoneid, ZONE_ATTR_PHYS_MCAP, &num, 0) == -1) {
53713247Sgjelinek 			zerror(gettext("could not set zone memory cap"));
53723247Sgjelinek 			res = Z_ERR;
53733247Sgjelinek 		}
53743247Sgjelinek 
53753247Sgjelinek 		if (zonecfg_enable_rcapd(smf_err, sizeof (smf_err)) != Z_OK) {
53763247Sgjelinek 			zerror(gettext("enabling system/rcap service failed: "
53773247Sgjelinek 			    "%s"), smf_err);
53783247Sgjelinek 			res = Z_ERR;
53793247Sgjelinek 		}
53803247Sgjelinek 	}
53813247Sgjelinek 
53823247Sgjelinek 	zonecfg_fini_handle(handle);
53833247Sgjelinek 
53843247Sgjelinek 	return (res);
53853247Sgjelinek }
53863247Sgjelinek 
538710718SJordan.Vaughan@Sun.com /*
538810718SJordan.Vaughan@Sun.com  * This is an undocumented interface that is invoked by the zones SMF service
538910718SJordan.Vaughan@Sun.com  * for installed zones that won't automatically boot.
539010718SJordan.Vaughan@Sun.com  */
539110718SJordan.Vaughan@Sun.com /* ARGSUSED */
539210718SJordan.Vaughan@Sun.com static int
sysboot_func(int argc,char * argv[])539310718SJordan.Vaughan@Sun.com sysboot_func(int argc, char *argv[])
539410718SJordan.Vaughan@Sun.com {
539510718SJordan.Vaughan@Sun.com 	int err;
539610718SJordan.Vaughan@Sun.com 	zone_dochandle_t zone_handle;
539710718SJordan.Vaughan@Sun.com 	brand_handle_t brand_handle;
539810718SJordan.Vaughan@Sun.com 	char cmdbuf[MAXPATHLEN];
539910718SJordan.Vaughan@Sun.com 	char zonepath[MAXPATHLEN];
540010718SJordan.Vaughan@Sun.com 
540110718SJordan.Vaughan@Sun.com 	/*
540210718SJordan.Vaughan@Sun.com 	 * This subcommand can only be executed in the global zone on non-global
540310718SJordan.Vaughan@Sun.com 	 * zones.
540410718SJordan.Vaughan@Sun.com 	 */
540510718SJordan.Vaughan@Sun.com 	if (zonecfg_in_alt_root())
540610718SJordan.Vaughan@Sun.com 		return (usage(B_FALSE));
540710718SJordan.Vaughan@Sun.com 	if (sanity_check(target_zone, CMD_SYSBOOT, B_FALSE, B_TRUE, B_FALSE) !=
540810718SJordan.Vaughan@Sun.com 	    Z_OK)
540910718SJordan.Vaughan@Sun.com 		return (Z_ERR);
541010718SJordan.Vaughan@Sun.com 
541110718SJordan.Vaughan@Sun.com 	/*
541210718SJordan.Vaughan@Sun.com 	 * Fetch the sysboot hook from the target zone's brand.
541310718SJordan.Vaughan@Sun.com 	 */
541410718SJordan.Vaughan@Sun.com 	if ((zone_handle = zonecfg_init_handle()) == NULL) {
541510718SJordan.Vaughan@Sun.com 		zperror(cmd_to_str(CMD_SYSBOOT), B_TRUE);
541610718SJordan.Vaughan@Sun.com 		return (Z_ERR);
541710718SJordan.Vaughan@Sun.com 	}
541810718SJordan.Vaughan@Sun.com 	if ((err = zonecfg_get_handle(target_zone, zone_handle)) != Z_OK) {
541910718SJordan.Vaughan@Sun.com 		errno = err;
542010718SJordan.Vaughan@Sun.com 		zperror(cmd_to_str(CMD_SYSBOOT), B_TRUE);
542110718SJordan.Vaughan@Sun.com 		zonecfg_fini_handle(zone_handle);
542210718SJordan.Vaughan@Sun.com 		return (Z_ERR);
542310718SJordan.Vaughan@Sun.com 	}
542410718SJordan.Vaughan@Sun.com 	if ((err = zonecfg_get_zonepath(zone_handle, zonepath,
542510718SJordan.Vaughan@Sun.com 	    sizeof (zonepath))) != Z_OK) {
542610718SJordan.Vaughan@Sun.com 		errno = err;
542710718SJordan.Vaughan@Sun.com 		zperror(cmd_to_str(CMD_SYSBOOT), B_TRUE);
542810718SJordan.Vaughan@Sun.com 		zonecfg_fini_handle(zone_handle);
542910718SJordan.Vaughan@Sun.com 		return (Z_ERR);
543010718SJordan.Vaughan@Sun.com 	}
543110718SJordan.Vaughan@Sun.com 	if ((brand_handle = brand_open(target_brand)) == NULL) {
543210718SJordan.Vaughan@Sun.com 		zerror(gettext("missing or invalid brand during %s operation: "
543310718SJordan.Vaughan@Sun.com 		    "%s"), cmd_to_str(CMD_SYSBOOT), target_brand);
543410718SJordan.Vaughan@Sun.com 		zonecfg_fini_handle(zone_handle);
543510718SJordan.Vaughan@Sun.com 		return (Z_ERR);
543610718SJordan.Vaughan@Sun.com 	}
543710718SJordan.Vaughan@Sun.com 	err = get_hook(brand_handle, cmdbuf, sizeof (cmdbuf), brand_get_sysboot,
543810718SJordan.Vaughan@Sun.com 	    target_zone, zonepath);
543910718SJordan.Vaughan@Sun.com 	brand_close(brand_handle);
544010718SJordan.Vaughan@Sun.com 	zonecfg_fini_handle(zone_handle);
544110718SJordan.Vaughan@Sun.com 	if (err != Z_OK) {
544210718SJordan.Vaughan@Sun.com 		zerror(gettext("unable to get brand hook from brand %s for %s "
544310718SJordan.Vaughan@Sun.com 		    "operation"), target_brand, cmd_to_str(CMD_SYSBOOT));
544410718SJordan.Vaughan@Sun.com 		return (Z_ERR);
544510718SJordan.Vaughan@Sun.com 	}
544610718SJordan.Vaughan@Sun.com 
544710718SJordan.Vaughan@Sun.com 	/*
544810718SJordan.Vaughan@Sun.com 	 * If the hook wasn't defined (which is OK), then indicate success and
544910718SJordan.Vaughan@Sun.com 	 * return.  Otherwise, execute the hook.
545010718SJordan.Vaughan@Sun.com 	 */
545110718SJordan.Vaughan@Sun.com 	if (cmdbuf[0] != '\0')
545210718SJordan.Vaughan@Sun.com 		return ((subproc_status(gettext("brand sysboot operation"),
545310718SJordan.Vaughan@Sun.com 		    do_subproc(cmdbuf), B_FALSE) == ZONE_SUBPROC_OK) ? Z_OK :
545410718SJordan.Vaughan@Sun.com 		    Z_BRAND_ERROR);
545510718SJordan.Vaughan@Sun.com 	return (Z_OK);
545610718SJordan.Vaughan@Sun.com }
545710718SJordan.Vaughan@Sun.com 
54582303Scarlsonj static int
help_func(int argc,char * argv[])54590Sstevel@tonic-gate help_func(int argc, char *argv[])
54600Sstevel@tonic-gate {
54610Sstevel@tonic-gate 	int arg, cmd_num;
54620Sstevel@tonic-gate 
54630Sstevel@tonic-gate 	if (argc == 0) {
54640Sstevel@tonic-gate 		(void) usage(B_TRUE);
54650Sstevel@tonic-gate 		return (Z_OK);
54660Sstevel@tonic-gate 	}
54670Sstevel@tonic-gate 	optind = 0;
54680Sstevel@tonic-gate 	if ((arg = getopt(argc, argv, "?")) != EOF) {
54690Sstevel@tonic-gate 		switch (arg) {
54700Sstevel@tonic-gate 		case '?':
54710Sstevel@tonic-gate 			sub_usage(SHELP_HELP, CMD_HELP);
54720Sstevel@tonic-gate 			return (optopt == '?' ? Z_OK : Z_USAGE);
54730Sstevel@tonic-gate 		default:
54740Sstevel@tonic-gate 			sub_usage(SHELP_HELP, CMD_HELP);
54750Sstevel@tonic-gate 			return (Z_USAGE);
54760Sstevel@tonic-gate 		}
54770Sstevel@tonic-gate 	}
54780Sstevel@tonic-gate 	while (optind < argc) {
5479988Scarlsonj 		/* Private commands have NULL short_usage; omit them */
5480988Scarlsonj 		if ((cmd_num = cmd_match(argv[optind])) < 0 ||
5481988Scarlsonj 		    cmdtab[cmd_num].short_usage == NULL) {
54820Sstevel@tonic-gate 			sub_usage(SHELP_HELP, CMD_HELP);
54830Sstevel@tonic-gate 			return (Z_USAGE);
54840Sstevel@tonic-gate 		}
54850Sstevel@tonic-gate 		sub_usage(cmdtab[cmd_num].short_usage, cmd_num);
54860Sstevel@tonic-gate 		optind++;
54870Sstevel@tonic-gate 	}
54880Sstevel@tonic-gate 	return (Z_OK);
54890Sstevel@tonic-gate }
54900Sstevel@tonic-gate 
54910Sstevel@tonic-gate /*
54920Sstevel@tonic-gate  * Returns: CMD_MIN thru CMD_MAX on success, -1 on error
54930Sstevel@tonic-gate  */
54940Sstevel@tonic-gate 
54950Sstevel@tonic-gate static int
cmd_match(char * cmd)54960Sstevel@tonic-gate cmd_match(char *cmd)
54970Sstevel@tonic-gate {
54980Sstevel@tonic-gate 	int i;
54990Sstevel@tonic-gate 
55000Sstevel@tonic-gate 	for (i = CMD_MIN; i <= CMD_MAX; i++) {
55010Sstevel@tonic-gate 		/* return only if there is an exact match */
55020Sstevel@tonic-gate 		if (strcmp(cmd, cmdtab[i].cmd_name) == 0)
55030Sstevel@tonic-gate 			return (cmdtab[i].cmd_num);
55040Sstevel@tonic-gate 	}
55050Sstevel@tonic-gate 	return (-1);
55060Sstevel@tonic-gate }
55070Sstevel@tonic-gate 
55080Sstevel@tonic-gate static int
parse_and_run(int argc,char * argv[])55090Sstevel@tonic-gate parse_and_run(int argc, char *argv[])
55100Sstevel@tonic-gate {
55110Sstevel@tonic-gate 	int i = cmd_match(argv[0]);
55120Sstevel@tonic-gate 
55130Sstevel@tonic-gate 	if (i < 0)
55140Sstevel@tonic-gate 		return (usage(B_FALSE));
55150Sstevel@tonic-gate 	return (cmdtab[i].handler(argc - 1, &(argv[1])));
55160Sstevel@tonic-gate }
55170Sstevel@tonic-gate 
55180Sstevel@tonic-gate static char *
get_execbasename(char * execfullname)55190Sstevel@tonic-gate get_execbasename(char *execfullname)
55200Sstevel@tonic-gate {
55210Sstevel@tonic-gate 	char *last_slash, *execbasename;
55220Sstevel@tonic-gate 
55230Sstevel@tonic-gate 	/* guard against '/' at end of command invocation */
55240Sstevel@tonic-gate 	for (;;) {
55250Sstevel@tonic-gate 		last_slash = strrchr(execfullname, '/');
55260Sstevel@tonic-gate 		if (last_slash == NULL) {
55270Sstevel@tonic-gate 			execbasename = execfullname;
55280Sstevel@tonic-gate 			break;
55290Sstevel@tonic-gate 		} else {
55300Sstevel@tonic-gate 			execbasename = last_slash + 1;
55310Sstevel@tonic-gate 			if (*execbasename == '\0') {
55320Sstevel@tonic-gate 				*last_slash = '\0';
55330Sstevel@tonic-gate 				continue;
55340Sstevel@tonic-gate 			}
55350Sstevel@tonic-gate 			break;
55360Sstevel@tonic-gate 		}
55370Sstevel@tonic-gate 	}
55380Sstevel@tonic-gate 	return (execbasename);
55390Sstevel@tonic-gate }
55400Sstevel@tonic-gate 
554112578SGlenn.Faden@Sun.COM static char *
get_username()554212578SGlenn.Faden@Sun.COM get_username()
554312578SGlenn.Faden@Sun.COM {
554412578SGlenn.Faden@Sun.COM 	uid_t uid;
554512578SGlenn.Faden@Sun.COM 	struct passwd *nptr;
554612578SGlenn.Faden@Sun.COM 
554712578SGlenn.Faden@Sun.COM 
554812578SGlenn.Faden@Sun.COM 	/*
554912578SGlenn.Faden@Sun.COM 	 * Authorizations are checked to restrict access based on the
555012578SGlenn.Faden@Sun.COM 	 * requested operation and zone name, It is assumed that the
555112578SGlenn.Faden@Sun.COM 	 * program is running with all privileges, but that the real
555212578SGlenn.Faden@Sun.COM 	 * user ID is that of the user or role on whose behalf we are
555312578SGlenn.Faden@Sun.COM 	 * operating. So we start by getting the username that will be
555412578SGlenn.Faden@Sun.COM 	 * used for subsequent authorization checks.
555512578SGlenn.Faden@Sun.COM 	 */
555612578SGlenn.Faden@Sun.COM 
555712578SGlenn.Faden@Sun.COM 	uid = getuid();
555812578SGlenn.Faden@Sun.COM 	if ((nptr = getpwuid(uid)) == NULL) {
555912578SGlenn.Faden@Sun.COM 		zerror(gettext("could not get user name."));
556012578SGlenn.Faden@Sun.COM 		exit(Z_ERR);
556112578SGlenn.Faden@Sun.COM 	}
556212578SGlenn.Faden@Sun.COM 	return (nptr->pw_name);
556312578SGlenn.Faden@Sun.COM }
556412578SGlenn.Faden@Sun.COM 
55650Sstevel@tonic-gate int
main(int argc,char ** argv)55660Sstevel@tonic-gate main(int argc, char **argv)
55670Sstevel@tonic-gate {
55680Sstevel@tonic-gate 	int arg;
55690Sstevel@tonic-gate 	zoneid_t zid;
5570766Scarlsonj 	struct stat st;
55712712Snn35248 	char *zone_lock_env;
55722712Snn35248 	int err;
55730Sstevel@tonic-gate 
55740Sstevel@tonic-gate 	if ((locale = setlocale(LC_ALL, "")) == NULL)
55750Sstevel@tonic-gate 		locale = "C";
55760Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
55770Sstevel@tonic-gate 	setbuf(stdout, NULL);
55780Sstevel@tonic-gate 	(void) sigset(SIGHUP, SIG_IGN);
55790Sstevel@tonic-gate 	execname = get_execbasename(argv[0]);
558012578SGlenn.Faden@Sun.COM 	username = get_username();
55810Sstevel@tonic-gate 	target_zone = NULL;
55820Sstevel@tonic-gate 	if (chdir("/") != 0) {
55830Sstevel@tonic-gate 		zerror(gettext("could not change directory to /."));
55840Sstevel@tonic-gate 		exit(Z_ERR);
55850Sstevel@tonic-gate 	}
558612578SGlenn.Faden@Sun.COM 
55879049SSudheer.Abdul-Salam@Sun.COM 	/*
55889049SSudheer.Abdul-Salam@Sun.COM 	 * Use the default system mask rather than anything that may have been
55899049SSudheer.Abdul-Salam@Sun.COM 	 * set by the caller.
55909049SSudheer.Abdul-Salam@Sun.COM 	 */
55919049SSudheer.Abdul-Salam@Sun.COM 	(void) umask(CMASK);
55920Sstevel@tonic-gate 
55932082Seschrock 	if (init_zfs() != Z_OK)
55942082Seschrock 		exit(Z_ERR);
55952082Seschrock 
55962303Scarlsonj 	while ((arg = getopt(argc, argv, "?u:z:R:")) != EOF) {
55970Sstevel@tonic-gate 		switch (arg) {
55980Sstevel@tonic-gate 		case '?':
55990Sstevel@tonic-gate 			return (usage(B_TRUE));
56002303Scarlsonj 		case 'u':
56012303Scarlsonj 			target_uuid = optarg;
56022303Scarlsonj 			break;
56030Sstevel@tonic-gate 		case 'z':
56040Sstevel@tonic-gate 			target_zone = optarg;
56050Sstevel@tonic-gate 			break;
5606766Scarlsonj 		case 'R':	/* private option for admin/install use */
5607766Scarlsonj 			if (*optarg != '/') {
5608766Scarlsonj 				zerror(gettext("root path must be absolute."));
5609766Scarlsonj 				exit(Z_ERR);
5610766Scarlsonj 			}
5611766Scarlsonj 			if (stat(optarg, &st) == -1 || !S_ISDIR(st.st_mode)) {
5612766Scarlsonj 				zerror(
5613766Scarlsonj 				    gettext("root path must be a directory."));
5614766Scarlsonj 				exit(Z_ERR);
5615766Scarlsonj 			}
5616766Scarlsonj 			zonecfg_set_root(optarg);
5617766Scarlsonj 			break;
56180Sstevel@tonic-gate 		default:
56190Sstevel@tonic-gate 			return (usage(B_FALSE));
56200Sstevel@tonic-gate 		}
56210Sstevel@tonic-gate 	}
56220Sstevel@tonic-gate 
56230Sstevel@tonic-gate 	if (optind >= argc)
56240Sstevel@tonic-gate 		return (usage(B_FALSE));
56252303Scarlsonj 
56262303Scarlsonj 	if (target_uuid != NULL && *target_uuid != '\0') {
56272303Scarlsonj 		uuid_t uuid;
56282303Scarlsonj 		static char newtarget[ZONENAME_MAX];
56292303Scarlsonj 
56302303Scarlsonj 		if (uuid_parse(target_uuid, uuid) == -1) {
56312303Scarlsonj 			zerror(gettext("illegal UUID value specified"));
56322303Scarlsonj 			exit(Z_ERR);
56332303Scarlsonj 		}
56342303Scarlsonj 		if (zonecfg_get_name_by_uuid(uuid, newtarget,
56352303Scarlsonj 		    sizeof (newtarget)) == Z_OK)
56362303Scarlsonj 			target_zone = newtarget;
56372303Scarlsonj 	}
56382303Scarlsonj 
56390Sstevel@tonic-gate 	if (target_zone != NULL && zone_get_id(target_zone, &zid) != 0) {
56400Sstevel@tonic-gate 		errno = Z_NO_ZONE;
56410Sstevel@tonic-gate 		zperror(target_zone, B_TRUE);
56420Sstevel@tonic-gate 		exit(Z_ERR);
56430Sstevel@tonic-gate 	}
56442712Snn35248 
56452712Snn35248 	/*
56462712Snn35248 	 * See if we have inherited the right to manipulate this zone from
56472712Snn35248 	 * a zoneadm instance in our ancestry.  If so, set zone_lock_cnt to
56482712Snn35248 	 * indicate it.  If not, make that explicit in our environment.
56492712Snn35248 	 */
56507089Sgjelinek 	zonecfg_init_lock_file(target_zone, &zone_lock_env);
56512712Snn35248 
565210943SEdward.Pilatowicz@Sun.COM 	/* Figure out what the system's default brand is */
565310943SEdward.Pilatowicz@Sun.COM 	if (zonecfg_default_brand(default_brand,
565410943SEdward.Pilatowicz@Sun.COM 	    sizeof (default_brand)) != Z_OK) {
565510943SEdward.Pilatowicz@Sun.COM 		zerror(gettext("unable to determine default brand"));
565610943SEdward.Pilatowicz@Sun.COM 		return (Z_ERR);
565710943SEdward.Pilatowicz@Sun.COM 	}
565810943SEdward.Pilatowicz@Sun.COM 
56592712Snn35248 	/*
56602712Snn35248 	 * If we are going to be operating on a single zone, retrieve its
56612712Snn35248 	 * brand type and determine whether it is native or not.
56622712Snn35248 	 */
56632712Snn35248 	if ((target_zone != NULL) &&
56648057SJordan.Vaughan@Sun.com 	    (strcmp(target_zone, GLOBAL_ZONENAME) != 0)) {
56652712Snn35248 		if (zone_get_brand(target_zone, target_brand,
56662712Snn35248 		    sizeof (target_brand)) != Z_OK) {
56672712Snn35248 			zerror(gettext("missing or invalid brand"));
56682712Snn35248 			exit(Z_ERR);
56692712Snn35248 		}
567010796SStephen.Lawrence@Sun.COM 		/*
567110796SStephen.Lawrence@Sun.COM 		 * In the alternate root environment, the only supported
567210796SStephen.Lawrence@Sun.COM 		 * operations are mount and unmount.  In this case, just treat
567310796SStephen.Lawrence@Sun.COM 		 * the zone as native if it is cluster.  Cluster zones can be
567410796SStephen.Lawrence@Sun.COM 		 * native for the purpose of LU or upgrade, and the cluster
567510796SStephen.Lawrence@Sun.COM 		 * brand may not exist in the miniroot (such as in net install
567610796SStephen.Lawrence@Sun.COM 		 * upgrade).
567710796SStephen.Lawrence@Sun.COM 		 */
567810796SStephen.Lawrence@Sun.COM 		if (strcmp(target_brand, CLUSTER_BRAND_NAME) == 0) {
567910796SStephen.Lawrence@Sun.COM 			if (zonecfg_in_alt_root()) {
568010943SEdward.Pilatowicz@Sun.COM 				(void) strlcpy(target_brand, default_brand,
568110796SStephen.Lawrence@Sun.COM 				    sizeof (target_brand));
568210796SStephen.Lawrence@Sun.COM 			}
568310796SStephen.Lawrence@Sun.COM 		}
56842712Snn35248 	}
56852712Snn35248 
56862712Snn35248 	err = parse_and_run(argc - optind, &argv[optind]);
56872712Snn35248 
56882712Snn35248 	return (err);
56890Sstevel@tonic-gate }
5690