xref: /onnv-gate/usr/src/cmd/zoneadm/zoneadm.c (revision 3542:58d9bc6ff10c)
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 /*
233352Sgjelinek  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
280Sstevel@tonic-gate 
290Sstevel@tonic-gate /*
300Sstevel@tonic-gate  * zoneadm is a command interpreter for zone administration.  It is all in
310Sstevel@tonic-gate  * C (i.e., no lex/yacc), and all the argument passing is argc/argv based.
320Sstevel@tonic-gate  * main() calls parse_and_run() which calls cmd_match(), then invokes the
330Sstevel@tonic-gate  * appropriate command's handler function.  The rest of the program is the
340Sstevel@tonic-gate  * handler functions and their helper functions.
350Sstevel@tonic-gate  *
360Sstevel@tonic-gate  * Some of the helper functions are used largely to simplify I18N: reducing
370Sstevel@tonic-gate  * the need for translation notes.  This is particularly true of many of
380Sstevel@tonic-gate  * the zerror() calls: doing e.g. zerror(gettext("%s failed"), "foo") rather
390Sstevel@tonic-gate  * than zerror(gettext("foo failed")) with a translation note indicating
400Sstevel@tonic-gate  * that "foo" need not be translated.
410Sstevel@tonic-gate  */
420Sstevel@tonic-gate 
430Sstevel@tonic-gate #include <stdio.h>
440Sstevel@tonic-gate #include <errno.h>
450Sstevel@tonic-gate #include <unistd.h>
460Sstevel@tonic-gate #include <signal.h>
470Sstevel@tonic-gate #include <stdarg.h>
480Sstevel@tonic-gate #include <ctype.h>
490Sstevel@tonic-gate #include <stdlib.h>
500Sstevel@tonic-gate #include <string.h>
510Sstevel@tonic-gate #include <wait.h>
520Sstevel@tonic-gate #include <zone.h>
530Sstevel@tonic-gate #include <priv.h>
540Sstevel@tonic-gate #include <locale.h>
550Sstevel@tonic-gate #include <libintl.h>
560Sstevel@tonic-gate #include <libzonecfg.h>
570Sstevel@tonic-gate #include <bsm/adt.h>
582712Snn35248 #include <sys/brand.h>
590Sstevel@tonic-gate #include <sys/param.h>
600Sstevel@tonic-gate #include <sys/types.h>
610Sstevel@tonic-gate #include <sys/stat.h>
620Sstevel@tonic-gate #include <sys/statvfs.h>
630Sstevel@tonic-gate #include <assert.h>
640Sstevel@tonic-gate #include <sys/sockio.h>
650Sstevel@tonic-gate #include <sys/mntent.h>
660Sstevel@tonic-gate #include <limits.h>
671867Sgjelinek #include <dirent.h>
682303Scarlsonj #include <uuid/uuid.h>
690Sstevel@tonic-gate 
700Sstevel@tonic-gate #include <fcntl.h>
710Sstevel@tonic-gate #include <door.h>
720Sstevel@tonic-gate #include <macros.h>
730Sstevel@tonic-gate #include <libgen.h>
741300Sgjelinek #include <fnmatch.h>
751931Sgjelinek #include <sys/modctl.h>
762712Snn35248 #include <libbrand.h>
773247Sgjelinek #include <libscf.h>
783352Sgjelinek #include <procfs.h>
790Sstevel@tonic-gate 
800Sstevel@tonic-gate #include <pool.h>
810Sstevel@tonic-gate #include <sys/pool.h>
823247Sgjelinek #include <sys/priocntl.h>
833247Sgjelinek #include <sys/fsspriocntl.h>
840Sstevel@tonic-gate 
851867Sgjelinek #include "zoneadm.h"
861867Sgjelinek 
870Sstevel@tonic-gate #define	MAXARGS	8
880Sstevel@tonic-gate 
890Sstevel@tonic-gate /* Reflects kernel zone entries */
900Sstevel@tonic-gate typedef struct zone_entry {
910Sstevel@tonic-gate 	zoneid_t	zid;
920Sstevel@tonic-gate 	char		zname[ZONENAME_MAX];
930Sstevel@tonic-gate 	char		*zstate_str;
940Sstevel@tonic-gate 	zone_state_t	zstate_num;
952712Snn35248 	char		zbrand[MAXNAMELEN];
960Sstevel@tonic-gate 	char		zroot[MAXPATHLEN];
972303Scarlsonj 	char		zuuid[UUID_PRINTABLE_STRING_LENGTH];
983448Sdh155122 	zone_iptype_t	ziptype;
990Sstevel@tonic-gate } zone_entry_t;
1000Sstevel@tonic-gate 
1010Sstevel@tonic-gate static zone_entry_t *zents;
1020Sstevel@tonic-gate static size_t nzents;
1032712Snn35248 static boolean_t is_native_zone = B_TRUE;
1040Sstevel@tonic-gate 
1051915Sgjelinek #define	LOOPBACK_IF	"lo0"
1061915Sgjelinek #define	SOCKET_AF(af)	(((af) == AF_UNSPEC) ? AF_INET : (af))
1071915Sgjelinek 
1081915Sgjelinek struct net_if {
1091915Sgjelinek 	char	*name;
1101915Sgjelinek 	int	af;
1111915Sgjelinek };
1121915Sgjelinek 
1130Sstevel@tonic-gate /* 0755 is the default directory mode. */
1140Sstevel@tonic-gate #define	DEFAULT_DIR_MODE \
1150Sstevel@tonic-gate 	(S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH)
1160Sstevel@tonic-gate 
1170Sstevel@tonic-gate struct cmd {
1180Sstevel@tonic-gate 	uint_t	cmd_num;				/* command number */
1190Sstevel@tonic-gate 	char	*cmd_name;				/* command name */
1200Sstevel@tonic-gate 	char	*short_usage;				/* short form help */
1210Sstevel@tonic-gate 	int	(*handler)(int argc, char *argv[]);	/* function to call */
1220Sstevel@tonic-gate 
1230Sstevel@tonic-gate };
1240Sstevel@tonic-gate 
1250Sstevel@tonic-gate #define	SHELP_HELP	"help"
1262267Sdp #define	SHELP_BOOT	"boot [-- boot_arguments]"
1270Sstevel@tonic-gate #define	SHELP_HALT	"halt"
1280Sstevel@tonic-gate #define	SHELP_READY	"ready"
1292267Sdp #define	SHELP_REBOOT	"reboot [-- boot_arguments]"
1300Sstevel@tonic-gate #define	SHELP_LIST	"list [-cipv]"
1310Sstevel@tonic-gate #define	SHELP_VERIFY	"verify"
1322712Snn35248 #define	SHELP_INSTALL	"install [-x nodataset] [brand-specific args]"
1330Sstevel@tonic-gate #define	SHELP_UNINSTALL	"uninstall [-F]"
1341867Sgjelinek #define	SHELP_CLONE	"clone [-m method] [-s <ZFS snapshot>] zonename"
1351300Sgjelinek #define	SHELP_MOVE	"move zonepath"
1362078Sgjelinek #define	SHELP_DETACH	"detach [-n]"
1372078Sgjelinek #define	SHELP_ATTACH	"attach [-F] [-n <path>]"
1382303Scarlsonj #define	SHELP_MARK	"mark incomplete"
1390Sstevel@tonic-gate 
1402712Snn35248 #define	EXEC_PREFIX	"exec "
1412712Snn35248 #define	EXEC_LEN	(strlen(EXEC_PREFIX))
1422712Snn35248 #define	RMCOMMAND	"/usr/bin/rm -rf"
1432712Snn35248 
1442712Snn35248 static int cleanup_zonepath(char *, boolean_t);
1452712Snn35248 
1463448Sdh155122 extern int ifname_open(char *);
1473448Sdh155122 
1480Sstevel@tonic-gate static int help_func(int argc, char *argv[]);
1490Sstevel@tonic-gate static int ready_func(int argc, char *argv[]);
1500Sstevel@tonic-gate static int boot_func(int argc, char *argv[]);
1510Sstevel@tonic-gate static int halt_func(int argc, char *argv[]);
1520Sstevel@tonic-gate static int reboot_func(int argc, char *argv[]);
1530Sstevel@tonic-gate static int list_func(int argc, char *argv[]);
1540Sstevel@tonic-gate static int verify_func(int argc, char *argv[]);
1550Sstevel@tonic-gate static int install_func(int argc, char *argv[]);
1560Sstevel@tonic-gate static int uninstall_func(int argc, char *argv[]);
157766Scarlsonj static int mount_func(int argc, char *argv[]);
158766Scarlsonj static int unmount_func(int argc, char *argv[]);
1591300Sgjelinek static int clone_func(int argc, char *argv[]);
1601300Sgjelinek static int move_func(int argc, char *argv[]);
1611507Sgjelinek static int detach_func(int argc, char *argv[]);
1621507Sgjelinek static int attach_func(int argc, char *argv[]);
1632303Scarlsonj static int mark_func(int argc, char *argv[]);
1643247Sgjelinek static int apply_func(int argc, char *argv[]);
1650Sstevel@tonic-gate static int sanity_check(char *zone, int cmd_num, boolean_t running,
1662712Snn35248     boolean_t unsafe_when_running, boolean_t force);
1670Sstevel@tonic-gate static int cmd_match(char *cmd);
1683339Szt129084 static int verify_details(int, char *argv[]);
1693339Szt129084 static int verify_brand(zone_dochandle_t, int, char *argv[]);
1703339Szt129084 static int invoke_brand_handler(int, char *argv[]);
1710Sstevel@tonic-gate 
1720Sstevel@tonic-gate static struct cmd cmdtab[] = {
1730Sstevel@tonic-gate 	{ CMD_HELP,		"help",		SHELP_HELP,	help_func },
1740Sstevel@tonic-gate 	{ CMD_BOOT,		"boot",		SHELP_BOOT,	boot_func },
1750Sstevel@tonic-gate 	{ CMD_HALT,		"halt",		SHELP_HALT,	halt_func },
1760Sstevel@tonic-gate 	{ CMD_READY,		"ready",	SHELP_READY,	ready_func },
1770Sstevel@tonic-gate 	{ CMD_REBOOT,		"reboot",	SHELP_REBOOT,	reboot_func },
1780Sstevel@tonic-gate 	{ CMD_LIST,		"list",		SHELP_LIST,	list_func },
1790Sstevel@tonic-gate 	{ CMD_VERIFY,		"verify",	SHELP_VERIFY,	verify_func },
1800Sstevel@tonic-gate 	{ CMD_INSTALL,		"install",	SHELP_INSTALL,	install_func },
1810Sstevel@tonic-gate 	{ CMD_UNINSTALL,	"uninstall",	SHELP_UNINSTALL,
182766Scarlsonj 	    uninstall_func },
1831300Sgjelinek 	/* mount and unmount are private commands for admin/install */
184766Scarlsonj 	{ CMD_MOUNT,		"mount",	NULL,		mount_func },
1851300Sgjelinek 	{ CMD_UNMOUNT,		"unmount",	NULL,		unmount_func },
1861300Sgjelinek 	{ CMD_CLONE,		"clone",	SHELP_CLONE,	clone_func },
1871507Sgjelinek 	{ CMD_MOVE,		"move",		SHELP_MOVE,	move_func },
1881507Sgjelinek 	{ CMD_DETACH,		"detach",	SHELP_DETACH,	detach_func },
1892303Scarlsonj 	{ CMD_ATTACH,		"attach",	SHELP_ATTACH,	attach_func },
1903247Sgjelinek 	{ CMD_MARK,		"mark",		SHELP_MARK,	mark_func },
1913247Sgjelinek 	{ CMD_APPLY,		"apply",	NULL,		apply_func }
1920Sstevel@tonic-gate };
1930Sstevel@tonic-gate 
1940Sstevel@tonic-gate /* global variables */
1950Sstevel@tonic-gate 
1960Sstevel@tonic-gate /* set early in main(), never modified thereafter, used all over the place */
1970Sstevel@tonic-gate static char *execname;
1982712Snn35248 static char target_brand[MAXNAMELEN];
1990Sstevel@tonic-gate static char *locale;
2001867Sgjelinek char *target_zone;
2012303Scarlsonj static char *target_uuid;
2020Sstevel@tonic-gate 
2030Sstevel@tonic-gate /* used in do_subproc() and signal handler */
2040Sstevel@tonic-gate static volatile boolean_t child_killed;
2052712Snn35248 static int do_subproc_cnt = 0;
2062712Snn35248 
2072712Snn35248 /*
2082712Snn35248  * Used to indicate whether this zoneadm instance has another zoneadm
2092712Snn35248  * instance in its ancestry.
2102712Snn35248  */
2112712Snn35248 static boolean_t zoneadm_is_nested = B_FALSE;
2122712Snn35248 
2132712Snn35248 /* used to track nested zone-lock operations */
2142712Snn35248 static int zone_lock_cnt = 0;
2152712Snn35248 
2162712Snn35248 /* used to communicate lock status to children */
2172712Snn35248 #define	LOCK_ENV_VAR	"_ZONEADM_LOCK_HELD"
2182712Snn35248 static char zoneadm_lock_held[] = LOCK_ENV_VAR"=1";
2192712Snn35248 static char zoneadm_lock_not_held[] = LOCK_ENV_VAR"=0";
2200Sstevel@tonic-gate 
2211867Sgjelinek char *
2220Sstevel@tonic-gate cmd_to_str(int cmd_num)
2230Sstevel@tonic-gate {
2240Sstevel@tonic-gate 	assert(cmd_num >= CMD_MIN && cmd_num <= CMD_MAX);
2250Sstevel@tonic-gate 	return (cmdtab[cmd_num].cmd_name);
2260Sstevel@tonic-gate }
2270Sstevel@tonic-gate 
2280Sstevel@tonic-gate /* This is a separate function because of gettext() wrapping. */
2290Sstevel@tonic-gate static char *
2300Sstevel@tonic-gate long_help(int cmd_num)
2310Sstevel@tonic-gate {
232222Scomay 	assert(cmd_num >= CMD_MIN && cmd_num <= CMD_MAX);
2330Sstevel@tonic-gate 	switch (cmd_num) {
2341634Sgjelinek 	case CMD_HELP:
2351634Sgjelinek 		return (gettext("Print usage message."));
2361634Sgjelinek 	case CMD_BOOT:
2372267Sdp 		return (gettext("Activates (boots) specified zone.  See "
2382267Sdp 		    "zoneadm(1m) for valid boot\n\targuments."));
2391634Sgjelinek 	case CMD_HALT:
2401634Sgjelinek 		return (gettext("Halts specified zone, bypassing shutdown "
2411634Sgjelinek 		    "scripts and removing runtime\n\tresources of the zone."));
2421634Sgjelinek 	case CMD_READY:
2431634Sgjelinek 		return (gettext("Prepares a zone for running applications but "
2441634Sgjelinek 		    "does not start any user\n\tprocesses in the zone."));
2451634Sgjelinek 	case CMD_REBOOT:
2461634Sgjelinek 		return (gettext("Restarts the zone (equivalent to a halt / "
2472267Sdp 		    "boot sequence).\n\tFails if the zone is not active.  "
2482267Sdp 		    "See zoneadm(1m) for valid boot\n\targuments."));
2491634Sgjelinek 	case CMD_LIST:
2501634Sgjelinek 		return (gettext("Lists the current zones, or a "
2511634Sgjelinek 		    "specific zone if indicated.  By default,\n\tall "
2521634Sgjelinek 		    "running zones are listed, though this can be "
2531634Sgjelinek 		    "expanded to all\n\tinstalled zones with the -i "
2541634Sgjelinek 		    "option or all configured zones with the\n\t-c "
2552303Scarlsonj 		    "option.  When used with the general -z <zone> and/or -u "
2562303Scarlsonj 		    "<uuid-match>\n\toptions, lists only the specified "
2572303Scarlsonj 		    "matching zone, but lists it\n\tregardless of its state, "
2582303Scarlsonj 		    "and the -i and -c options are disallowed.  The\n\t-v "
2592303Scarlsonj 		    "option can be used to display verbose information: zone "
2602303Scarlsonj 		    "name, id,\n\tcurrent state, root directory and options.  "
2612303Scarlsonj 		    "The -p option can be used\n\tto request machine-parsable "
2622303Scarlsonj 		    "output.  The -v and -p options are mutually\n\texclusive."
2632303Scarlsonj 		    "  If neither -v nor -p is used, just the zone name is "
2642303Scarlsonj 		    "listed."));
2651634Sgjelinek 	case CMD_VERIFY:
2661634Sgjelinek 		return (gettext("Check to make sure the configuration "
2671634Sgjelinek 		    "can safely be instantiated\n\ton the machine: "
2681634Sgjelinek 		    "physical network interfaces exist, etc."));
2691634Sgjelinek 	case CMD_INSTALL:
2701867Sgjelinek 		return (gettext("Install the configuration on to the system.  "
2711867Sgjelinek 		    "The -x nodataset option\n\tcan be used to prevent the "
2721867Sgjelinek 		    "creation of a new ZFS file system for the\n\tzone "
2732712Snn35248 		    "(assuming the zonepath is within a ZFS file system).\n\t"
2742712Snn35248 		    "All other arguments are passed to the brand installation "
2752712Snn35248 		    "function;\n\tsee brand(4) for more information."));
2761634Sgjelinek 	case CMD_UNINSTALL:
2771634Sgjelinek 		return (gettext("Uninstall the configuration from the system.  "
2781634Sgjelinek 		    "The -F flag can be used\n\tto force the action."));
2791634Sgjelinek 	case CMD_CLONE:
2801867Sgjelinek 		return (gettext("Clone the installation of another zone.  "
2811867Sgjelinek 		    "The -m option can be used to\n\tspecify 'copy' which "
2821867Sgjelinek 		    "forces a copy of the source zone.  The -s option\n\t"
2831867Sgjelinek 		    "can be used to specify the name of a ZFS snapshot "
2841867Sgjelinek 		    "that was taken from\n\ta previous clone command.  The "
2851867Sgjelinek 		    "snapshot will be used as the source\n\tinstead of "
2861867Sgjelinek 		    "creating a new ZFS snapshot."));
2871634Sgjelinek 	case CMD_MOVE:
2881634Sgjelinek 		return (gettext("Move the zone to a new zonepath."));
2891634Sgjelinek 	case CMD_DETACH:
2901634Sgjelinek 		return (gettext("Detach the zone from the system. The zone "
2911634Sgjelinek 		    "state is changed to\n\t'configured' (but the files under "
2921634Sgjelinek 		    "the zonepath are untouched).\n\tThe zone can subsequently "
2931634Sgjelinek 		    "be attached, or can be moved to another\n\tsystem and "
2942078Sgjelinek 		    "attached there.  The -n option can be used to specify\n\t"
2952078Sgjelinek 		    "'no-execute' mode.  When -n is used, the information "
2962078Sgjelinek 		    "needed to attach\n\tthe zone is sent to standard output "
2972078Sgjelinek 		    "but the zone is not actually\n\tdetached."));
2981634Sgjelinek 	case CMD_ATTACH:
2991634Sgjelinek 		return (gettext("Attach the zone to the system.  The zone "
3001634Sgjelinek 		    "state must be 'configured'\n\tprior to attach; upon "
3011634Sgjelinek 		    "successful completion, the zone state will be\n\t"
3021634Sgjelinek 		    "'installed'.  The system software on the current "
3031634Sgjelinek 		    "system must be\n\tcompatible with the software on the "
3041634Sgjelinek 		    "zone's original system.\n\tSpecify -F to force the attach "
3052078Sgjelinek 		    "and skip software compatibility tests.\n\tThe -n option "
3062078Sgjelinek 		    "can be used to specify 'no-execute' mode.  When -n is\n\t"
3072078Sgjelinek 		    "used, the information needed to attach the zone is read "
3082078Sgjelinek 		    "from the\n\tspecified path and the configuration is only "
3092078Sgjelinek 		    "validated.  The path can\n\tbe '-' to specify standard "
3102078Sgjelinek 		    "input."));
3112303Scarlsonj 	case CMD_MARK:
3122303Scarlsonj 		return (gettext("Set the state of the zone.  This can be used "
3132303Scarlsonj 		    "to force the zone\n\tstate to 'incomplete' "
3142303Scarlsonj 		    "administratively if some activity has rendered\n\tthe "
3152303Scarlsonj 		    "zone permanently unusable.  The only valid state that "
3162303Scarlsonj 		    "may be\n\tspecified is 'incomplete'."));
3171634Sgjelinek 	default:
3181634Sgjelinek 		return ("");
3190Sstevel@tonic-gate 	}
3200Sstevel@tonic-gate 	/* NOTREACHED */
321222Scomay 	return (NULL);
3220Sstevel@tonic-gate }
3230Sstevel@tonic-gate 
3240Sstevel@tonic-gate /*
3250Sstevel@tonic-gate  * Called with explicit B_TRUE when help is explicitly requested, B_FALSE for
3260Sstevel@tonic-gate  * unexpected errors.
3270Sstevel@tonic-gate  */
3280Sstevel@tonic-gate 
3290Sstevel@tonic-gate static int
3300Sstevel@tonic-gate usage(boolean_t explicit)
3310Sstevel@tonic-gate {
3320Sstevel@tonic-gate 	int i;
3330Sstevel@tonic-gate 	FILE *fd = explicit ? stdout : stderr;
3340Sstevel@tonic-gate 
3350Sstevel@tonic-gate 	(void) fprintf(fd, "%s:\t%s help\n", gettext("usage"), execname);
3362303Scarlsonj 	(void) fprintf(fd, "\t%s [-z <zone>] [-u <uuid-match>] list\n",
3372303Scarlsonj 	    execname);
3382303Scarlsonj 	(void) fprintf(fd, "\t%s {-z <zone>|-u <uuid-match>} <%s>\n", execname,
3390Sstevel@tonic-gate 	    gettext("subcommand"));
3400Sstevel@tonic-gate 	(void) fprintf(fd, "\n%s:\n\n", gettext("Subcommands"));
3410Sstevel@tonic-gate 	for (i = CMD_MIN; i <= CMD_MAX; i++) {
342766Scarlsonj 		if (cmdtab[i].short_usage == NULL)
343766Scarlsonj 			continue;
3440Sstevel@tonic-gate 		(void) fprintf(fd, "%s\n", cmdtab[i].short_usage);
3450Sstevel@tonic-gate 		if (explicit)
3460Sstevel@tonic-gate 			(void) fprintf(fd, "\t%s\n\n", long_help(i));
3470Sstevel@tonic-gate 	}
3480Sstevel@tonic-gate 	if (!explicit)
3490Sstevel@tonic-gate 		(void) fputs("\n", fd);
3500Sstevel@tonic-gate 	return (Z_USAGE);
3510Sstevel@tonic-gate }
3520Sstevel@tonic-gate 
3530Sstevel@tonic-gate static void
3540Sstevel@tonic-gate sub_usage(char *short_usage, int cmd_num)
3550Sstevel@tonic-gate {
3560Sstevel@tonic-gate 	(void) fprintf(stderr, "%s:\t%s\n", gettext("usage"), short_usage);
3570Sstevel@tonic-gate 	(void) fprintf(stderr, "\t%s\n", long_help(cmd_num));
3580Sstevel@tonic-gate }
3590Sstevel@tonic-gate 
3600Sstevel@tonic-gate /*
3610Sstevel@tonic-gate  * zperror() is like perror(3c) except that this also prints the executable
3620Sstevel@tonic-gate  * name at the start of the message, and takes a boolean indicating whether
3630Sstevel@tonic-gate  * to call libc'c strerror() or that from libzonecfg.
3640Sstevel@tonic-gate  */
3650Sstevel@tonic-gate 
3661867Sgjelinek void
3670Sstevel@tonic-gate zperror(const char *str, boolean_t zonecfg_error)
3680Sstevel@tonic-gate {
3690Sstevel@tonic-gate 	(void) fprintf(stderr, "%s: %s: %s\n", execname, str,
3700Sstevel@tonic-gate 	    zonecfg_error ? zonecfg_strerror(errno) : strerror(errno));
3710Sstevel@tonic-gate }
3720Sstevel@tonic-gate 
3730Sstevel@tonic-gate /*
3740Sstevel@tonic-gate  * zperror2() is very similar to zperror() above, except it also prints a
3750Sstevel@tonic-gate  * supplied zone name after the executable.
3760Sstevel@tonic-gate  *
3770Sstevel@tonic-gate  * All current consumers of this function want libzonecfg's strerror() rather
3780Sstevel@tonic-gate  * than libc's; if this ever changes, this function can be made more generic
3790Sstevel@tonic-gate  * like zperror() above.
3800Sstevel@tonic-gate  */
3810Sstevel@tonic-gate 
3821867Sgjelinek void
3830Sstevel@tonic-gate zperror2(const char *zone, const char *str)
3840Sstevel@tonic-gate {
3850Sstevel@tonic-gate 	(void) fprintf(stderr, "%s: %s: %s: %s\n", execname, zone, str,
3860Sstevel@tonic-gate 	    zonecfg_strerror(errno));
3870Sstevel@tonic-gate }
3880Sstevel@tonic-gate 
3890Sstevel@tonic-gate /* PRINTFLIKE1 */
3901867Sgjelinek void
3910Sstevel@tonic-gate zerror(const char *fmt, ...)
3920Sstevel@tonic-gate {
3930Sstevel@tonic-gate 	va_list alist;
3940Sstevel@tonic-gate 
3950Sstevel@tonic-gate 	va_start(alist, fmt);
3960Sstevel@tonic-gate 	(void) fprintf(stderr, "%s: ", execname);
3970Sstevel@tonic-gate 	if (target_zone != NULL)
3980Sstevel@tonic-gate 		(void) fprintf(stderr, "zone '%s': ", target_zone);
3990Sstevel@tonic-gate 	(void) vfprintf(stderr, fmt, alist);
4000Sstevel@tonic-gate 	(void) fprintf(stderr, "\n");
4010Sstevel@tonic-gate 	va_end(alist);
4020Sstevel@tonic-gate }
4030Sstevel@tonic-gate 
4040Sstevel@tonic-gate static void *
4050Sstevel@tonic-gate safe_calloc(size_t nelem, size_t elsize)
4060Sstevel@tonic-gate {
4070Sstevel@tonic-gate 	void *r = calloc(nelem, elsize);
4080Sstevel@tonic-gate 
4090Sstevel@tonic-gate 	if (r == NULL) {
4100Sstevel@tonic-gate 		zerror(gettext("failed to allocate %lu bytes: %s"),
4110Sstevel@tonic-gate 		    (ulong_t)nelem * elsize, strerror(errno));
4120Sstevel@tonic-gate 		exit(Z_ERR);
4130Sstevel@tonic-gate 	}
4140Sstevel@tonic-gate 	return (r);
4150Sstevel@tonic-gate }
4160Sstevel@tonic-gate 
4170Sstevel@tonic-gate static void
4180Sstevel@tonic-gate zone_print(zone_entry_t *zent, boolean_t verbose, boolean_t parsable)
4190Sstevel@tonic-gate {
4200Sstevel@tonic-gate 	static boolean_t firsttime = B_TRUE;
4213448Sdh155122 	char *ip_type_str;
4223448Sdh155122 
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
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;
4720Sstevel@tonic-gate 
4730Sstevel@tonic-gate 	(void) strlcpy(zent->zname, zone_name, sizeof (zent->zname));
4740Sstevel@tonic-gate 	(void) strlcpy(zent->zroot, "???", sizeof (zent->zroot));
4752712Snn35248 	(void) strlcpy(zent->zbrand, "???", sizeof (zent->zbrand));
4760Sstevel@tonic-gate 	zent->zstate_str = "???";
4770Sstevel@tonic-gate 
478766Scarlsonj 	zent->zid = zid;
4790Sstevel@tonic-gate 
4802303Scarlsonj 	if (zonecfg_get_uuid(zone_name, uuid) == Z_OK &&
4812303Scarlsonj 	    !uuid_is_null(uuid))
4822303Scarlsonj 		uuid_unparse(uuid, zent->zuuid);
4832303Scarlsonj 	else
4842303Scarlsonj 		zent->zuuid[0] = '\0';
4852303Scarlsonj 
4861676Sjpk 	/*
4871676Sjpk 	 * For labeled zones which query the zone path of lower-level
4881676Sjpk 	 * zones, the path needs to be adjusted to drop the final
4891676Sjpk 	 * "/root" component. This adjusted path is then useful
4901676Sjpk 	 * for reading down any exported directories from the
4911676Sjpk 	 * lower-level zone.
4921676Sjpk 	 */
4931676Sjpk 	if (is_system_labeled() && zent->zid != ZONE_ID_UNDEFINED) {
4941676Sjpk 		if (zone_getattr(zent->zid, ZONE_ATTR_ROOT, zent->zroot,
4951676Sjpk 		    sizeof (zent->zroot)) == -1) {
4961676Sjpk 			zperror2(zent->zname,
4971676Sjpk 			    gettext("could not get zone path."));
4981676Sjpk 			return (Z_ERR);
4991676Sjpk 		}
5001676Sjpk 		cp = zent->zroot + strlen(zent->zroot) - 5;
5011676Sjpk 		if (cp > zent->zroot && strcmp(cp, "/root") == 0)
5021676Sjpk 			*cp = 0;
5031676Sjpk 	} else {
5041676Sjpk 		if ((err = zone_get_zonepath(zent->zname, root,
5051676Sjpk 		    sizeof (root))) != Z_OK) {
5061676Sjpk 			errno = err;
5071676Sjpk 			zperror2(zent->zname,
5081676Sjpk 			    gettext("could not get zone path."));
5091676Sjpk 			return (Z_ERR);
5101676Sjpk 		}
5111676Sjpk 		(void) strlcpy(zent->zroot, root, sizeof (zent->zroot));
5121676Sjpk 	}
5130Sstevel@tonic-gate 
5140Sstevel@tonic-gate 	if ((err = zone_get_state(zent->zname, &zent->zstate_num)) != Z_OK) {
5150Sstevel@tonic-gate 		errno = err;
5160Sstevel@tonic-gate 		zperror2(zent->zname, gettext("could not get state"));
5170Sstevel@tonic-gate 		return (Z_ERR);
5180Sstevel@tonic-gate 	}
5190Sstevel@tonic-gate 	zent->zstate_str = zone_state_str(zent->zstate_num);
5203009Snn35248 
5213009Snn35248 	/*
5223009Snn35248 	 * A zone's brand is only available in the .xml file describing it,
5233009Snn35248 	 * which is only visible to the global zone.  This causes
5243009Snn35248 	 * zone_get_brand() to fail when called from within a non-global
5253009Snn35248 	 * zone.  Fortunately we only do this on labeled systems, where we
5263009Snn35248 	 * know all zones are native.
5273009Snn35248 	 */
5283009Snn35248 	if (getzoneid() != GLOBAL_ZONEID) {
5293009Snn35248 		assert(is_system_labeled() != 0);
5303009Snn35248 		(void) strlcpy(zent->zbrand, NATIVE_BRAND_NAME,
5313009Snn35248 		    sizeof (zent->zbrand));
5323009Snn35248 	} else if (zone_get_brand(zent->zname, zent->zbrand,
5332712Snn35248 	    sizeof (zent->zbrand)) != Z_OK) {
5342712Snn35248 		zperror2(zent->zname, gettext("could not get brand name"));
5352712Snn35248 		return (Z_ERR);
5362712Snn35248 	}
5370Sstevel@tonic-gate 
5383448Sdh155122 	/*
5393448Sdh155122 	 * Get ip type of the zone.
5403448Sdh155122 	 * Note for global zone, ZS_SHARED is set always.
5413448Sdh155122 	 */
5423448Sdh155122 	if (zid == GLOBAL_ZONEID) {
5433448Sdh155122 		zent->ziptype = ZS_SHARED;
5443448Sdh155122 	} else {
5453448Sdh155122 
5463448Sdh155122 		if (zent->zstate_num == ZONE_STATE_RUNNING) {
5473448Sdh155122 			ushort_t flags;
5483448Sdh155122 
5493448Sdh155122 			if (zone_getattr(zid, ZONE_ATTR_FLAGS, &flags,
5503448Sdh155122 			    sizeof (flags)) < 0) {
5513448Sdh155122 				zperror2(zent->zname,
5523448Sdh155122 				    gettext("could not get zone flags"));
5533448Sdh155122 				return (Z_ERR);
5543448Sdh155122 			}
5553448Sdh155122 			if (flags & ZF_NET_EXCL)
5563448Sdh155122 				zent->ziptype = ZS_EXCLUSIVE;
5573448Sdh155122 			else
5583448Sdh155122 				zent->ziptype = ZS_SHARED;
5593448Sdh155122 		} else {
5603448Sdh155122 			zone_dochandle_t handle;
5613448Sdh155122 
5623448Sdh155122 			if ((handle = zonecfg_init_handle()) == NULL) {
5633448Sdh155122 				zperror2(zent->zname,
5643448Sdh155122 				    gettext("could not init handle"));
5653448Sdh155122 				return (Z_ERR);
5663448Sdh155122 			}
5673448Sdh155122 			if ((err = zonecfg_get_handle(zent->zname, handle))
5683448Sdh155122 			    != Z_OK) {
5693448Sdh155122 				zperror2(zent->zname,
5703448Sdh155122 				    gettext("could not get handle"));
5713448Sdh155122 				zonecfg_fini_handle(handle);
5723448Sdh155122 				return (Z_ERR);
5733448Sdh155122 			}
5743448Sdh155122 
5753448Sdh155122 			if ((err = zonecfg_get_iptype(handle, &zent->ziptype))
5763448Sdh155122 			    != Z_OK) {
5773448Sdh155122 				zperror2(zent->zname,
5783448Sdh155122 				    gettext("could not get ip-type"));
5793448Sdh155122 				zonecfg_fini_handle(handle);
5803448Sdh155122 				return (Z_ERR);
5813448Sdh155122 			}
5823448Sdh155122 			zonecfg_fini_handle(handle);
5833448Sdh155122 		}
5843448Sdh155122 	}
5853448Sdh155122 
5860Sstevel@tonic-gate 	return (Z_OK);
5870Sstevel@tonic-gate }
5880Sstevel@tonic-gate 
5890Sstevel@tonic-gate /*
5900Sstevel@tonic-gate  * fetch_zents() calls zone_list(2) to find out how many zones are running
5910Sstevel@tonic-gate  * (which is stored in the global nzents), then calls zone_list(2) again
5920Sstevel@tonic-gate  * to fetch the list of running zones (stored in the global zents).  This
5930Sstevel@tonic-gate  * function may be called multiple times, so if zents is already set, we
5940Sstevel@tonic-gate  * return immediately to save work.
5950Sstevel@tonic-gate  */
5960Sstevel@tonic-gate 
5970Sstevel@tonic-gate static int
598766Scarlsonj fetch_zents(void)
5990Sstevel@tonic-gate {
6000Sstevel@tonic-gate 	zoneid_t *zids = NULL;
6010Sstevel@tonic-gate 	uint_t nzents_saved;
602766Scarlsonj 	int i, retv;
603766Scarlsonj 	FILE *fp;
604766Scarlsonj 	boolean_t inaltroot;
605766Scarlsonj 	zone_entry_t *zentp;
6060Sstevel@tonic-gate 
6070Sstevel@tonic-gate 	if (nzents > 0)
6080Sstevel@tonic-gate 		return (Z_OK);
6090Sstevel@tonic-gate 
6100Sstevel@tonic-gate 	if (zone_list(NULL, &nzents) != 0) {
6110Sstevel@tonic-gate 		zperror(gettext("failed to get zoneid list"), B_FALSE);
6120Sstevel@tonic-gate 		return (Z_ERR);
6130Sstevel@tonic-gate 	}
6140Sstevel@tonic-gate 
6150Sstevel@tonic-gate again:
6160Sstevel@tonic-gate 	if (nzents == 0)
6170Sstevel@tonic-gate 		return (Z_OK);
6180Sstevel@tonic-gate 
6190Sstevel@tonic-gate 	zids = safe_calloc(nzents, sizeof (zoneid_t));
6200Sstevel@tonic-gate 	nzents_saved = nzents;
6210Sstevel@tonic-gate 
6220Sstevel@tonic-gate 	if (zone_list(zids, &nzents) != 0) {
6230Sstevel@tonic-gate 		zperror(gettext("failed to get zone list"), B_FALSE);
6240Sstevel@tonic-gate 		free(zids);
6250Sstevel@tonic-gate 		return (Z_ERR);
6260Sstevel@tonic-gate 	}
6270Sstevel@tonic-gate 	if (nzents != nzents_saved) {
6280Sstevel@tonic-gate 		/* list changed, try again */
6290Sstevel@tonic-gate 		free(zids);
6300Sstevel@tonic-gate 		goto again;
6310Sstevel@tonic-gate 	}
6320Sstevel@tonic-gate 
6330Sstevel@tonic-gate 	zents = safe_calloc(nzents, sizeof (zone_entry_t));
6340Sstevel@tonic-gate 
635766Scarlsonj 	inaltroot = zonecfg_in_alt_root();
636766Scarlsonj 	if (inaltroot)
637766Scarlsonj 		fp = zonecfg_open_scratch("", B_FALSE);
638766Scarlsonj 	else
639766Scarlsonj 		fp = NULL;
640766Scarlsonj 	zentp = zents;
641766Scarlsonj 	retv = Z_OK;
6420Sstevel@tonic-gate 	for (i = 0; i < nzents; i++) {
6430Sstevel@tonic-gate 		char name[ZONENAME_MAX];
644766Scarlsonj 		char altname[ZONENAME_MAX];
6450Sstevel@tonic-gate 
646766Scarlsonj 		if (getzonenamebyid(zids[i], name, sizeof (name)) < 0) {
6470Sstevel@tonic-gate 			zperror(gettext("failed to get zone name"), B_FALSE);
648766Scarlsonj 			retv = Z_ERR;
649766Scarlsonj 			continue;
650766Scarlsonj 		}
651766Scarlsonj 		if (zonecfg_is_scratch(name)) {
652766Scarlsonj 			/* Ignore scratch zones by default */
653766Scarlsonj 			if (!inaltroot)
654766Scarlsonj 				continue;
655766Scarlsonj 			if (fp == NULL ||
656766Scarlsonj 			    zonecfg_reverse_scratch(fp, name, altname,
657766Scarlsonj 			    sizeof (altname), NULL, 0) == -1) {
658924Sgjelinek 				zerror(gettext("could not resolve scratch "
659766Scarlsonj 				    "zone %s"), name);
660766Scarlsonj 				retv = Z_ERR;
661766Scarlsonj 				continue;
662766Scarlsonj 			}
663766Scarlsonj 			(void) strcpy(name, altname);
664766Scarlsonj 		} else {
665766Scarlsonj 			/* Ignore non-scratch when in an alternate root */
666766Scarlsonj 			if (inaltroot && strcmp(name, GLOBAL_ZONENAME) != 0)
667766Scarlsonj 				continue;
668766Scarlsonj 		}
669766Scarlsonj 		if (lookup_zone_info(name, zids[i], zentp) != Z_OK) {
670766Scarlsonj 			zerror(gettext("failed to get zone data"));
671766Scarlsonj 			retv = Z_ERR;
672766Scarlsonj 			continue;
673766Scarlsonj 		}
674766Scarlsonj 		zentp++;
6750Sstevel@tonic-gate 	}
676766Scarlsonj 	nzents = zentp - zents;
677766Scarlsonj 	if (fp != NULL)
678766Scarlsonj 		zonecfg_close_scratch(fp);
6790Sstevel@tonic-gate 
6800Sstevel@tonic-gate 	free(zids);
681766Scarlsonj 	return (retv);
6820Sstevel@tonic-gate }
6830Sstevel@tonic-gate 
684766Scarlsonj static int
6850Sstevel@tonic-gate zone_print_list(zone_state_t min_state, boolean_t verbose, boolean_t parsable)
6860Sstevel@tonic-gate {
6870Sstevel@tonic-gate 	int i;
6880Sstevel@tonic-gate 	zone_entry_t zent;
6890Sstevel@tonic-gate 	FILE *cookie;
6900Sstevel@tonic-gate 	char *name;
6910Sstevel@tonic-gate 
6920Sstevel@tonic-gate 	/*
6930Sstevel@tonic-gate 	 * First get the list of running zones from the kernel and print them.
6940Sstevel@tonic-gate 	 * If that is all we need, then return.
6950Sstevel@tonic-gate 	 */
696766Scarlsonj 	if ((i = fetch_zents()) != Z_OK) {
6970Sstevel@tonic-gate 		/*
6980Sstevel@tonic-gate 		 * No need for error messages; fetch_zents() has already taken
6990Sstevel@tonic-gate 		 * care of this.
7000Sstevel@tonic-gate 		 */
701766Scarlsonj 		return (i);
7020Sstevel@tonic-gate 	}
703766Scarlsonj 	for (i = 0; i < nzents; i++)
7040Sstevel@tonic-gate 		zone_print(&zents[i], verbose, parsable);
7050Sstevel@tonic-gate 	if (min_state >= ZONE_STATE_RUNNING)
706766Scarlsonj 		return (Z_OK);
7070Sstevel@tonic-gate 	/*
7080Sstevel@tonic-gate 	 * Next, get the full list of zones from the configuration, skipping
7090Sstevel@tonic-gate 	 * any we have already printed.
7100Sstevel@tonic-gate 	 */
7110Sstevel@tonic-gate 	cookie = setzoneent();
7120Sstevel@tonic-gate 	while ((name = getzoneent(cookie)) != NULL) {
7130Sstevel@tonic-gate 		for (i = 0; i < nzents; i++) {
7140Sstevel@tonic-gate 			if (strcmp(zents[i].zname, name) == 0)
7150Sstevel@tonic-gate 				break;
7160Sstevel@tonic-gate 		}
7170Sstevel@tonic-gate 		if (i < nzents) {
7180Sstevel@tonic-gate 			free(name);
7190Sstevel@tonic-gate 			continue;
7200Sstevel@tonic-gate 		}
721766Scarlsonj 		if (lookup_zone_info(name, ZONE_ID_UNDEFINED, &zent) != Z_OK) {
7220Sstevel@tonic-gate 			free(name);
7230Sstevel@tonic-gate 			continue;
7240Sstevel@tonic-gate 		}
7250Sstevel@tonic-gate 		free(name);
7260Sstevel@tonic-gate 		if (zent.zstate_num >= min_state)
7270Sstevel@tonic-gate 			zone_print(&zent, verbose, parsable);
7280Sstevel@tonic-gate 	}
7290Sstevel@tonic-gate 	endzoneent(cookie);
730766Scarlsonj 	return (Z_OK);
7310Sstevel@tonic-gate }
7320Sstevel@tonic-gate 
7330Sstevel@tonic-gate static zone_entry_t *
7340Sstevel@tonic-gate lookup_running_zone(char *str)
7350Sstevel@tonic-gate {
7360Sstevel@tonic-gate 	zoneid_t zoneid;
7370Sstevel@tonic-gate 	char *cp;
7380Sstevel@tonic-gate 	int i;
7390Sstevel@tonic-gate 
7400Sstevel@tonic-gate 	if (fetch_zents() != Z_OK)
7410Sstevel@tonic-gate 		return (NULL);
7420Sstevel@tonic-gate 
7430Sstevel@tonic-gate 	for (i = 0; i < nzents; i++) {
7440Sstevel@tonic-gate 		if (strcmp(str, zents[i].zname) == 0)
7450Sstevel@tonic-gate 			return (&zents[i]);
7460Sstevel@tonic-gate 	}
7470Sstevel@tonic-gate 	errno = 0;
7480Sstevel@tonic-gate 	zoneid = strtol(str, &cp, 0);
7490Sstevel@tonic-gate 	if (zoneid < MIN_ZONEID || zoneid > MAX_ZONEID ||
7500Sstevel@tonic-gate 	    errno != 0 || *cp != '\0')
7510Sstevel@tonic-gate 		return (NULL);
7520Sstevel@tonic-gate 	for (i = 0; i < nzents; i++) {
7530Sstevel@tonic-gate 		if (zoneid == zents[i].zid)
7540Sstevel@tonic-gate 			return (&zents[i]);
7550Sstevel@tonic-gate 	}
7560Sstevel@tonic-gate 	return (NULL);
7570Sstevel@tonic-gate }
7580Sstevel@tonic-gate 
7590Sstevel@tonic-gate /*
7600Sstevel@tonic-gate  * Check a bit in a mode_t: if on is B_TRUE, that bit should be on; if
7610Sstevel@tonic-gate  * B_FALSE, it should be off.  Return B_TRUE if the mode is bad (incorrect).
7620Sstevel@tonic-gate  */
7630Sstevel@tonic-gate static boolean_t
7640Sstevel@tonic-gate bad_mode_bit(mode_t mode, mode_t bit, boolean_t on, char *file)
7650Sstevel@tonic-gate {
7660Sstevel@tonic-gate 	char *str;
7670Sstevel@tonic-gate 
7680Sstevel@tonic-gate 	assert(bit == S_IRUSR || bit == S_IWUSR || bit == S_IXUSR ||
7690Sstevel@tonic-gate 	    bit == S_IRGRP || bit == S_IWGRP || bit == S_IXGRP ||
7700Sstevel@tonic-gate 	    bit == S_IROTH || bit == S_IWOTH || bit == S_IXOTH);
7710Sstevel@tonic-gate 	/*
7720Sstevel@tonic-gate 	 * TRANSLATION_NOTE
7730Sstevel@tonic-gate 	 * The strings below will be used as part of a larger message,
7740Sstevel@tonic-gate 	 * either:
7750Sstevel@tonic-gate 	 * (file name) must be (owner|group|world) (read|writ|execut)able
7760Sstevel@tonic-gate 	 * or
7770Sstevel@tonic-gate 	 * (file name) must not be (owner|group|world) (read|writ|execut)able
7780Sstevel@tonic-gate 	 */
7790Sstevel@tonic-gate 	switch (bit) {
7800Sstevel@tonic-gate 	case S_IRUSR:
7810Sstevel@tonic-gate 		str = gettext("owner readable");
7820Sstevel@tonic-gate 		break;
7830Sstevel@tonic-gate 	case S_IWUSR:
7840Sstevel@tonic-gate 		str = gettext("owner writable");
7850Sstevel@tonic-gate 		break;
7860Sstevel@tonic-gate 	case S_IXUSR:
7870Sstevel@tonic-gate 		str = gettext("owner executable");
7880Sstevel@tonic-gate 		break;
7890Sstevel@tonic-gate 	case S_IRGRP:
7900Sstevel@tonic-gate 		str = gettext("group readable");
7910Sstevel@tonic-gate 		break;
7920Sstevel@tonic-gate 	case S_IWGRP:
7930Sstevel@tonic-gate 		str = gettext("group writable");
7940Sstevel@tonic-gate 		break;
7950Sstevel@tonic-gate 	case S_IXGRP:
7960Sstevel@tonic-gate 		str = gettext("group executable");
7970Sstevel@tonic-gate 		break;
7980Sstevel@tonic-gate 	case S_IROTH:
7990Sstevel@tonic-gate 		str = gettext("world readable");
8000Sstevel@tonic-gate 		break;
8010Sstevel@tonic-gate 	case S_IWOTH:
8020Sstevel@tonic-gate 		str = gettext("world writable");
8030Sstevel@tonic-gate 		break;
8040Sstevel@tonic-gate 	case S_IXOTH:
8050Sstevel@tonic-gate 		str = gettext("world executable");
8060Sstevel@tonic-gate 		break;
8070Sstevel@tonic-gate 	}
8080Sstevel@tonic-gate 	if ((mode & bit) == (on ? 0 : bit)) {
8090Sstevel@tonic-gate 		/*
8100Sstevel@tonic-gate 		 * TRANSLATION_NOTE
8110Sstevel@tonic-gate 		 * The first parameter below is a file name; the second
8120Sstevel@tonic-gate 		 * is one of the "(owner|group|world) (read|writ|execut)able"
8130Sstevel@tonic-gate 		 * strings from above.
8140Sstevel@tonic-gate 		 */
8150Sstevel@tonic-gate 		/*
8160Sstevel@tonic-gate 		 * The code below could be simplified but not in a way
8170Sstevel@tonic-gate 		 * that would easily translate to non-English locales.
8180Sstevel@tonic-gate 		 */
8190Sstevel@tonic-gate 		if (on) {
8200Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("%s must be %s.\n"),
8210Sstevel@tonic-gate 			    file, str);
8220Sstevel@tonic-gate 		} else {
8230Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("%s must not be %s.\n"),
8240Sstevel@tonic-gate 			    file, str);
8250Sstevel@tonic-gate 		}
8260Sstevel@tonic-gate 		return (B_TRUE);
8270Sstevel@tonic-gate 	}
8280Sstevel@tonic-gate 	return (B_FALSE);
8290Sstevel@tonic-gate }
8300Sstevel@tonic-gate 
8310Sstevel@tonic-gate /*
8320Sstevel@tonic-gate  * We want to make sure that no zone has its zone path as a child node
8330Sstevel@tonic-gate  * (in the directory sense) of any other.  We do that by comparing this
8340Sstevel@tonic-gate  * zone's path to the path of all other (non-global) zones.  The comparison
8350Sstevel@tonic-gate  * in each case is simple: add '/' to the end of the path, then do a
8360Sstevel@tonic-gate  * strncmp() of the two paths, using the length of the shorter one.
8370Sstevel@tonic-gate  */
8380Sstevel@tonic-gate 
8390Sstevel@tonic-gate static int
8400Sstevel@tonic-gate crosscheck_zonepaths(char *path)
8410Sstevel@tonic-gate {
8420Sstevel@tonic-gate 	char rpath[MAXPATHLEN];		/* resolved path */
8430Sstevel@tonic-gate 	char path_copy[MAXPATHLEN];	/* copy of original path */
8440Sstevel@tonic-gate 	char rpath_copy[MAXPATHLEN];	/* copy of original rpath */
8450Sstevel@tonic-gate 	struct zoneent *ze;
8460Sstevel@tonic-gate 	int res, err;
8470Sstevel@tonic-gate 	FILE *cookie;
8480Sstevel@tonic-gate 
8490Sstevel@tonic-gate 	cookie = setzoneent();
8500Sstevel@tonic-gate 	while ((ze = getzoneent_private(cookie)) != NULL) {
8510Sstevel@tonic-gate 		/* Skip zones which are not installed. */
8520Sstevel@tonic-gate 		if (ze->zone_state < ZONE_STATE_INSTALLED) {
8530Sstevel@tonic-gate 			free(ze);
8540Sstevel@tonic-gate 			continue;
8550Sstevel@tonic-gate 		}
8560Sstevel@tonic-gate 		/* Skip the global zone and the current target zone. */
8570Sstevel@tonic-gate 		if (strcmp(ze->zone_name, GLOBAL_ZONENAME) == 0 ||
8580Sstevel@tonic-gate 		    strcmp(ze->zone_name, target_zone) == 0) {
8590Sstevel@tonic-gate 			free(ze);
8600Sstevel@tonic-gate 			continue;
8610Sstevel@tonic-gate 		}
8620Sstevel@tonic-gate 		if (strlen(ze->zone_path) == 0) {
8630Sstevel@tonic-gate 			/* old index file without path, fall back */
8640Sstevel@tonic-gate 			if ((err = zone_get_zonepath(ze->zone_name,
8650Sstevel@tonic-gate 			    ze->zone_path, sizeof (ze->zone_path))) != Z_OK) {
8660Sstevel@tonic-gate 				errno = err;
8670Sstevel@tonic-gate 				zperror2(ze->zone_name,
8680Sstevel@tonic-gate 				    gettext("could not get zone path"));
8690Sstevel@tonic-gate 				free(ze);
8700Sstevel@tonic-gate 				continue;
8710Sstevel@tonic-gate 			}
8720Sstevel@tonic-gate 		}
873766Scarlsonj 		(void) snprintf(path_copy, sizeof (path_copy), "%s%s",
874766Scarlsonj 		    zonecfg_get_root(), ze->zone_path);
875766Scarlsonj 		res = resolvepath(path_copy, rpath, sizeof (rpath));
8760Sstevel@tonic-gate 		if (res == -1) {
8770Sstevel@tonic-gate 			if (errno != ENOENT) {
878766Scarlsonj 				zperror(path_copy, B_FALSE);
8790Sstevel@tonic-gate 				free(ze);
8800Sstevel@tonic-gate 				return (Z_ERR);
8810Sstevel@tonic-gate 			}
8820Sstevel@tonic-gate 			(void) printf(gettext("WARNING: zone %s is installed, "
8830Sstevel@tonic-gate 			    "but its %s %s does not exist.\n"), ze->zone_name,
884766Scarlsonj 			    "zonepath", path_copy);
8850Sstevel@tonic-gate 			free(ze);
8860Sstevel@tonic-gate 			continue;
8870Sstevel@tonic-gate 		}
8880Sstevel@tonic-gate 		rpath[res] = '\0';
8890Sstevel@tonic-gate 		(void) snprintf(path_copy, sizeof (path_copy), "%s/", path);
8900Sstevel@tonic-gate 		(void) snprintf(rpath_copy, sizeof (rpath_copy), "%s/", rpath);
8910Sstevel@tonic-gate 		if (strncmp(path_copy, rpath_copy,
8920Sstevel@tonic-gate 		    min(strlen(path_copy), strlen(rpath_copy))) == 0) {
893924Sgjelinek 			/*
894924Sgjelinek 			 * TRANSLATION_NOTE
895924Sgjelinek 			 * zonepath is a literal that should not be translated.
896924Sgjelinek 			 */
8970Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("%s zonepath (%s) and "
8980Sstevel@tonic-gate 			    "%s zonepath (%s) overlap.\n"),
8990Sstevel@tonic-gate 			    target_zone, path, ze->zone_name, rpath);
9000Sstevel@tonic-gate 			free(ze);
9010Sstevel@tonic-gate 			return (Z_ERR);
9020Sstevel@tonic-gate 		}
9030Sstevel@tonic-gate 		free(ze);
9040Sstevel@tonic-gate 	}
9050Sstevel@tonic-gate 	endzoneent(cookie);
9060Sstevel@tonic-gate 	return (Z_OK);
9070Sstevel@tonic-gate }
9080Sstevel@tonic-gate 
9090Sstevel@tonic-gate static int
9100Sstevel@tonic-gate validate_zonepath(char *path, int cmd_num)
9110Sstevel@tonic-gate {
9120Sstevel@tonic-gate 	int res;			/* result of last library/system call */
9130Sstevel@tonic-gate 	boolean_t err = B_FALSE;	/* have we run into an error? */
9140Sstevel@tonic-gate 	struct stat stbuf;
9152267Sdp 	struct statvfs64 vfsbuf;
9160Sstevel@tonic-gate 	char rpath[MAXPATHLEN];		/* resolved path */
9170Sstevel@tonic-gate 	char ppath[MAXPATHLEN];		/* parent path */
9180Sstevel@tonic-gate 	char rppath[MAXPATHLEN];	/* resolved parent path */
9190Sstevel@tonic-gate 	char rootpath[MAXPATHLEN];	/* root path */
9200Sstevel@tonic-gate 	zone_state_t state;
9210Sstevel@tonic-gate 
9220Sstevel@tonic-gate 	if (path[0] != '/') {
9230Sstevel@tonic-gate 		(void) fprintf(stderr,
9240Sstevel@tonic-gate 		    gettext("%s is not an absolute path.\n"), path);
9250Sstevel@tonic-gate 		return (Z_ERR);
9260Sstevel@tonic-gate 	}
9270Sstevel@tonic-gate 	if ((res = resolvepath(path, rpath, sizeof (rpath))) == -1) {
9280Sstevel@tonic-gate 		if ((errno != ENOENT) ||
9291300Sgjelinek 		    (cmd_num != CMD_VERIFY && cmd_num != CMD_INSTALL &&
9301300Sgjelinek 		    cmd_num != CMD_CLONE && cmd_num != CMD_MOVE)) {
9310Sstevel@tonic-gate 			zperror(path, B_FALSE);
9320Sstevel@tonic-gate 			return (Z_ERR);
9330Sstevel@tonic-gate 		}
9340Sstevel@tonic-gate 		if (cmd_num == CMD_VERIFY) {
935924Sgjelinek 			/*
936924Sgjelinek 			 * TRANSLATION_NOTE
937924Sgjelinek 			 * zoneadm is a literal that should not be translated.
938924Sgjelinek 			 */
9390Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("WARNING: %s does not "
940924Sgjelinek 			    "exist, so it could not be verified.\nWhen "
941924Sgjelinek 			    "'zoneadm %s' is run, '%s' will try to create\n%s, "
942924Sgjelinek 			    "and '%s' will be tried again,\nbut the '%s' may "
943924Sgjelinek 			    "fail if:\nthe parent directory of %s is group- or "
944924Sgjelinek 			    "other-writable\nor\n%s overlaps with any other "
9450Sstevel@tonic-gate 			    "installed zones.\n"), path,
9460Sstevel@tonic-gate 			    cmd_to_str(CMD_INSTALL), cmd_to_str(CMD_INSTALL),
9470Sstevel@tonic-gate 			    path, cmd_to_str(CMD_VERIFY),
9480Sstevel@tonic-gate 			    cmd_to_str(CMD_VERIFY), path, path);
9490Sstevel@tonic-gate 			return (Z_OK);
9500Sstevel@tonic-gate 		}
9510Sstevel@tonic-gate 		/*
9520Sstevel@tonic-gate 		 * The zonepath is supposed to be mode 700 but its
9530Sstevel@tonic-gate 		 * parent(s) 755.  So use 755 on the mkdirp() then
9540Sstevel@tonic-gate 		 * chmod() the zonepath itself to 700.
9550Sstevel@tonic-gate 		 */
9560Sstevel@tonic-gate 		if (mkdirp(path, DEFAULT_DIR_MODE) < 0) {
9570Sstevel@tonic-gate 			zperror(path, B_FALSE);
9580Sstevel@tonic-gate 			return (Z_ERR);
9590Sstevel@tonic-gate 		}
9600Sstevel@tonic-gate 		/*
9610Sstevel@tonic-gate 		 * If the chmod() fails, report the error, but might
9620Sstevel@tonic-gate 		 * as well continue the verify procedure.
9630Sstevel@tonic-gate 		 */
9640Sstevel@tonic-gate 		if (chmod(path, S_IRWXU) != 0)
9650Sstevel@tonic-gate 			zperror(path, B_FALSE);
9660Sstevel@tonic-gate 		/*
9670Sstevel@tonic-gate 		 * Since the mkdir() succeeded, we should not have to
9680Sstevel@tonic-gate 		 * worry about a subsequent ENOENT, thus this should
9690Sstevel@tonic-gate 		 * only recurse once.
9700Sstevel@tonic-gate 		 */
9711300Sgjelinek 		return (validate_zonepath(path, cmd_num));
9720Sstevel@tonic-gate 	}
9730Sstevel@tonic-gate 	rpath[res] = '\0';
9740Sstevel@tonic-gate 	if (strcmp(path, rpath) != 0) {
9750Sstevel@tonic-gate 		errno = Z_RESOLVED_PATH;
9760Sstevel@tonic-gate 		zperror(path, B_TRUE);
9770Sstevel@tonic-gate 		return (Z_ERR);
9780Sstevel@tonic-gate 	}
9790Sstevel@tonic-gate 	if ((res = stat(rpath, &stbuf)) != 0) {
9800Sstevel@tonic-gate 		zperror(rpath, B_FALSE);
9810Sstevel@tonic-gate 		return (Z_ERR);
9820Sstevel@tonic-gate 	}
9830Sstevel@tonic-gate 	if (!S_ISDIR(stbuf.st_mode)) {
9840Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("%s is not a directory.\n"),
9850Sstevel@tonic-gate 		    rpath);
9860Sstevel@tonic-gate 		return (Z_ERR);
9870Sstevel@tonic-gate 	}
9883445Sblakej 	if (strcmp(stbuf.st_fstype, MNTTYPE_TMPFS) == 0) {
9890Sstevel@tonic-gate 		(void) printf(gettext("WARNING: %s is on a temporary "
9901867Sgjelinek 		    "file system.\n"), rpath);
9910Sstevel@tonic-gate 	}
9920Sstevel@tonic-gate 	if (crosscheck_zonepaths(rpath) != Z_OK)
9930Sstevel@tonic-gate 		return (Z_ERR);
9940Sstevel@tonic-gate 	/*
9950Sstevel@tonic-gate 	 * Try to collect and report as many minor errors as possible
9960Sstevel@tonic-gate 	 * before returning, so the user can learn everything that needs
9970Sstevel@tonic-gate 	 * to be fixed up front.
9980Sstevel@tonic-gate 	 */
9990Sstevel@tonic-gate 	if (stbuf.st_uid != 0) {
10000Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("%s is not owned by root.\n"),
10010Sstevel@tonic-gate 		    rpath);
10020Sstevel@tonic-gate 		err = B_TRUE;
10030Sstevel@tonic-gate 	}
10040Sstevel@tonic-gate 	err |= bad_mode_bit(stbuf.st_mode, S_IRUSR, B_TRUE, rpath);
10050Sstevel@tonic-gate 	err |= bad_mode_bit(stbuf.st_mode, S_IWUSR, B_TRUE, rpath);
10060Sstevel@tonic-gate 	err |= bad_mode_bit(stbuf.st_mode, S_IXUSR, B_TRUE, rpath);
10070Sstevel@tonic-gate 	err |= bad_mode_bit(stbuf.st_mode, S_IRGRP, B_FALSE, rpath);
10080Sstevel@tonic-gate 	err |= bad_mode_bit(stbuf.st_mode, S_IWGRP, B_FALSE, rpath);
10090Sstevel@tonic-gate 	err |= bad_mode_bit(stbuf.st_mode, S_IXGRP, B_FALSE, rpath);
10100Sstevel@tonic-gate 	err |= bad_mode_bit(stbuf.st_mode, S_IROTH, B_FALSE, rpath);
10110Sstevel@tonic-gate 	err |= bad_mode_bit(stbuf.st_mode, S_IWOTH, B_FALSE, rpath);
10120Sstevel@tonic-gate 	err |= bad_mode_bit(stbuf.st_mode, S_IXOTH, B_FALSE, rpath);
10130Sstevel@tonic-gate 
10140Sstevel@tonic-gate 	(void) snprintf(ppath, sizeof (ppath), "%s/..", path);
10150Sstevel@tonic-gate 	if ((res = resolvepath(ppath, rppath, sizeof (rppath))) == -1) {
10160Sstevel@tonic-gate 		zperror(ppath, B_FALSE);
10170Sstevel@tonic-gate 		return (Z_ERR);
10180Sstevel@tonic-gate 	}
10190Sstevel@tonic-gate 	rppath[res] = '\0';
10200Sstevel@tonic-gate 	if ((res = stat(rppath, &stbuf)) != 0) {
10210Sstevel@tonic-gate 		zperror(rppath, B_FALSE);
10220Sstevel@tonic-gate 		return (Z_ERR);
10230Sstevel@tonic-gate 	}
10240Sstevel@tonic-gate 	/* theoretically impossible */
10250Sstevel@tonic-gate 	if (!S_ISDIR(stbuf.st_mode)) {
10260Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("%s is not a directory.\n"),
10270Sstevel@tonic-gate 		    rppath);
10280Sstevel@tonic-gate 		return (Z_ERR);
10290Sstevel@tonic-gate 	}
10300Sstevel@tonic-gate 	if (stbuf.st_uid != 0) {
10310Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("%s is not owned by root.\n"),
10320Sstevel@tonic-gate 		    rppath);
10330Sstevel@tonic-gate 		err = B_TRUE;
10340Sstevel@tonic-gate 	}
10350Sstevel@tonic-gate 	err |= bad_mode_bit(stbuf.st_mode, S_IRUSR, B_TRUE, rppath);
10360Sstevel@tonic-gate 	err |= bad_mode_bit(stbuf.st_mode, S_IWUSR, B_TRUE, rppath);
10370Sstevel@tonic-gate 	err |= bad_mode_bit(stbuf.st_mode, S_IXUSR, B_TRUE, rppath);
10380Sstevel@tonic-gate 	err |= bad_mode_bit(stbuf.st_mode, S_IWGRP, B_FALSE, rppath);
10390Sstevel@tonic-gate 	err |= bad_mode_bit(stbuf.st_mode, S_IWOTH, B_FALSE, rppath);
10400Sstevel@tonic-gate 	if (strcmp(rpath, rppath) == 0) {
10410Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("%s is its own parent.\n"),
10420Sstevel@tonic-gate 		    rppath);
10430Sstevel@tonic-gate 		err = B_TRUE;
10440Sstevel@tonic-gate 	}
10450Sstevel@tonic-gate 
10462267Sdp 	if (statvfs64(rpath, &vfsbuf) != 0) {
10470Sstevel@tonic-gate 		zperror(rpath, B_FALSE);
10480Sstevel@tonic-gate 		return (Z_ERR);
10490Sstevel@tonic-gate 	}
1050823Sgjelinek 	if (strcmp(vfsbuf.f_basetype, MNTTYPE_NFS) == 0) {
1051924Sgjelinek 		/*
1052924Sgjelinek 		 * TRANSLATION_NOTE
1053924Sgjelinek 		 * Zonepath and NFS are literals that should not be translated.
1054924Sgjelinek 		 */
1055924Sgjelinek 		(void) fprintf(stderr, gettext("Zonepath %s is on an NFS "
10561867Sgjelinek 		    "mounted file system.\n"
10571867Sgjelinek 		    "\tA local file system must be used.\n"), rpath);
1058823Sgjelinek 		return (Z_ERR);
1059823Sgjelinek 	}
1060823Sgjelinek 	if (vfsbuf.f_flag & ST_NOSUID) {
1061924Sgjelinek 		/*
1062924Sgjelinek 		 * TRANSLATION_NOTE
1063924Sgjelinek 		 * Zonepath and nosuid are literals that should not be
1064924Sgjelinek 		 * translated.
1065924Sgjelinek 		 */
1066823Sgjelinek 		(void) fprintf(stderr, gettext("Zonepath %s is on a nosuid "
10671867Sgjelinek 		    "file system.\n"), rpath);
10680Sstevel@tonic-gate 		return (Z_ERR);
10690Sstevel@tonic-gate 	}
10700Sstevel@tonic-gate 
10710Sstevel@tonic-gate 	if ((res = zone_get_state(target_zone, &state)) != Z_OK) {
10720Sstevel@tonic-gate 		errno = res;
10730Sstevel@tonic-gate 		zperror2(target_zone, gettext("could not get state"));
10740Sstevel@tonic-gate 		return (Z_ERR);
10750Sstevel@tonic-gate 	}
10760Sstevel@tonic-gate 	/*
10770Sstevel@tonic-gate 	 * The existence of the root path is only bad in the configured state,
10780Sstevel@tonic-gate 	 * as it is *supposed* to be there at the installed and later states.
10791507Sgjelinek 	 * However, the root path is expected to be there if the zone is
10801507Sgjelinek 	 * detached.
10810Sstevel@tonic-gate 	 * State/command mismatches are caught earlier in verify_details().
10820Sstevel@tonic-gate 	 */
10831507Sgjelinek 	if (state == ZONE_STATE_CONFIGURED && cmd_num != CMD_ATTACH) {
10840Sstevel@tonic-gate 		if (snprintf(rootpath, sizeof (rootpath), "%s/root", rpath) >=
10850Sstevel@tonic-gate 		    sizeof (rootpath)) {
1086924Sgjelinek 			/*
1087924Sgjelinek 			 * TRANSLATION_NOTE
1088924Sgjelinek 			 * Zonepath is a literal that should not be translated.
1089924Sgjelinek 			 */
10900Sstevel@tonic-gate 			(void) fprintf(stderr,
10910Sstevel@tonic-gate 			    gettext("Zonepath %s is too long.\n"), rpath);
10920Sstevel@tonic-gate 			return (Z_ERR);
10930Sstevel@tonic-gate 		}
10940Sstevel@tonic-gate 		if ((res = stat(rootpath, &stbuf)) == 0) {
10951507Sgjelinek 			if (zonecfg_detached(rpath))
10961507Sgjelinek 				(void) fprintf(stderr,
10971507Sgjelinek 				    gettext("Cannot %s detached "
10981507Sgjelinek 				    "zone.\nUse attach or remove %s "
10991507Sgjelinek 				    "directory.\n"), cmd_to_str(cmd_num),
11001507Sgjelinek 				    rpath);
11011507Sgjelinek 			else
11021507Sgjelinek 				(void) fprintf(stderr,
11031507Sgjelinek 				    gettext("Rootpath %s exists; "
11041507Sgjelinek 				    "remove or move aside prior to %s.\n"),
11051507Sgjelinek 				    rootpath, cmd_to_str(cmd_num));
11060Sstevel@tonic-gate 			return (Z_ERR);
11070Sstevel@tonic-gate 		}
11080Sstevel@tonic-gate 	}
11090Sstevel@tonic-gate 
11100Sstevel@tonic-gate 	return (err ? Z_ERR : Z_OK);
11110Sstevel@tonic-gate }
11120Sstevel@tonic-gate 
11132712Snn35248 /*
11142712Snn35248  * The following two routines implement a simple locking mechanism to
11152712Snn35248  * ensure that only one instance of zoneadm at a time is able to manipulate
11162712Snn35248  * a given zone.  The lock is built on top of an fcntl(2) lock of
11172712Snn35248  * [<altroot>]/var/run/zones/<zonename>.zoneadm.lock.  If a zoneadm instance
11182712Snn35248  * can grab that lock, it is allowed to manipulate the zone.
11192712Snn35248  *
11202712Snn35248  * Since zoneadm may call external applications which in turn invoke
11212712Snn35248  * zoneadm again, we introduce the notion of "lock inheritance".  Any
11222712Snn35248  * instance of zoneadm that has another instance in its ancestry is assumed
11232712Snn35248  * to be acting on behalf of the original zoneadm, and is thus allowed to
11242712Snn35248  * manipulate its zone.
11252712Snn35248  *
11262712Snn35248  * This inheritance is implemented via the _ZONEADM_LOCK_HELD environment
11272712Snn35248  * variable.  When zoneadm is granted a lock on its zone, this environment
11282712Snn35248  * variable is set to 1.  When it releases the lock, the variable is set to
11292712Snn35248  * 0.  Since a child process inherits its parent's environment, checking
11302712Snn35248  * the state of this variable indicates whether or not any ancestor owns
11312712Snn35248  * the lock.
11322712Snn35248  */
11330Sstevel@tonic-gate static void
11340Sstevel@tonic-gate release_lock_file(int lockfd)
11350Sstevel@tonic-gate {
11362712Snn35248 	/*
11372712Snn35248 	 * If we are cleaning up from a failed attempt to lock the zone for
11382712Snn35248 	 * the first time, we might have a zone_lock_cnt of 0.  In that
11392712Snn35248 	 * error case, we don't want to do anything but close the lock
11402712Snn35248 	 * file.
11412712Snn35248 	 */
11422712Snn35248 	assert(zone_lock_cnt >= 0);
11432712Snn35248 	if (zone_lock_cnt > 0) {
11442712Snn35248 		assert(getenv(LOCK_ENV_VAR) != NULL);
11452712Snn35248 		assert(atoi(getenv(LOCK_ENV_VAR)) == 1);
11462712Snn35248 		if (--zone_lock_cnt > 0) {
11472712Snn35248 			assert(lockfd == -1);
11482712Snn35248 			return;
11492712Snn35248 		}
11502712Snn35248 		if (putenv(zoneadm_lock_not_held) != 0) {
11512712Snn35248 			zperror(target_zone, B_TRUE);
11522712Snn35248 			exit(Z_ERR);
11532712Snn35248 		}
11542712Snn35248 	}
11552712Snn35248 	assert(lockfd >= 0);
11560Sstevel@tonic-gate 	(void) close(lockfd);
11570Sstevel@tonic-gate }
11580Sstevel@tonic-gate 
11590Sstevel@tonic-gate static int
11600Sstevel@tonic-gate grab_lock_file(const char *zone_name, int *lockfd)
11610Sstevel@tonic-gate {
11620Sstevel@tonic-gate 	char pathbuf[PATH_MAX];
11630Sstevel@tonic-gate 	struct flock flock;
11640Sstevel@tonic-gate 
11652712Snn35248 	/*
11662712Snn35248 	 * If we already have the lock, we can skip this expensive song
11672712Snn35248 	 * and dance.
11682712Snn35248 	 */
11692712Snn35248 	if (zone_lock_cnt > 0) {
11702712Snn35248 		zone_lock_cnt++;
11712712Snn35248 		*lockfd = -1;
11722712Snn35248 		return (Z_OK);
11732712Snn35248 	}
11742712Snn35248 	assert(getenv(LOCK_ENV_VAR) != NULL);
11752712Snn35248 	assert(atoi(getenv(LOCK_ENV_VAR)) == 0);
11762712Snn35248 
1177766Scarlsonj 	if (snprintf(pathbuf, sizeof (pathbuf), "%s%s", zonecfg_get_root(),
1178766Scarlsonj 	    ZONES_TMPDIR) >= sizeof (pathbuf)) {
1179766Scarlsonj 		zerror(gettext("alternate root path is too long"));
1180766Scarlsonj 		return (Z_ERR);
1181766Scarlsonj 	}
1182766Scarlsonj 	if (mkdir(pathbuf, S_IRWXU) < 0 && errno != EEXIST) {
1183766Scarlsonj 		zerror(gettext("could not mkdir %s: %s"), pathbuf,
11840Sstevel@tonic-gate 		    strerror(errno));
11850Sstevel@tonic-gate 		return (Z_ERR);
11860Sstevel@tonic-gate 	}
1187766Scarlsonj 	(void) chmod(pathbuf, S_IRWXU);
11880Sstevel@tonic-gate 
11890Sstevel@tonic-gate 	/*
11900Sstevel@tonic-gate 	 * One of these lock files is created for each zone (when needed).
11910Sstevel@tonic-gate 	 * The lock files are not cleaned up (except on system reboot),
11920Sstevel@tonic-gate 	 * but since there is only one per zone, there is no resource
11930Sstevel@tonic-gate 	 * starvation issue.
11940Sstevel@tonic-gate 	 */
1195766Scarlsonj 	if (snprintf(pathbuf, sizeof (pathbuf), "%s%s/%s.zoneadm.lock",
1196766Scarlsonj 	    zonecfg_get_root(), ZONES_TMPDIR, zone_name) >= sizeof (pathbuf)) {
1197766Scarlsonj 		zerror(gettext("alternate root path is too long"));
1198766Scarlsonj 		return (Z_ERR);
1199766Scarlsonj 	}
12000Sstevel@tonic-gate 	if ((*lockfd = open(pathbuf, O_RDWR|O_CREAT, S_IRUSR|S_IWUSR)) < 0) {
12010Sstevel@tonic-gate 		zerror(gettext("could not open %s: %s"), pathbuf,
12020Sstevel@tonic-gate 		    strerror(errno));
12030Sstevel@tonic-gate 		return (Z_ERR);
12040Sstevel@tonic-gate 	}
12050Sstevel@tonic-gate 	/*
12060Sstevel@tonic-gate 	 * Lock the file to synchronize with other zoneadmds
12070Sstevel@tonic-gate 	 */
12080Sstevel@tonic-gate 	flock.l_type = F_WRLCK;
12090Sstevel@tonic-gate 	flock.l_whence = SEEK_SET;
12100Sstevel@tonic-gate 	flock.l_start = (off_t)0;
12110Sstevel@tonic-gate 	flock.l_len = (off_t)0;
12122712Snn35248 	if ((fcntl(*lockfd, F_SETLKW, &flock) < 0) ||
12132712Snn35248 	    (putenv(zoneadm_lock_held) != 0)) {
12140Sstevel@tonic-gate 		zerror(gettext("unable to lock %s: %s"), pathbuf,
12150Sstevel@tonic-gate 		    strerror(errno));
12160Sstevel@tonic-gate 		release_lock_file(*lockfd);
12170Sstevel@tonic-gate 		return (Z_ERR);
12180Sstevel@tonic-gate 	}
12192712Snn35248 	zone_lock_cnt = 1;
12200Sstevel@tonic-gate 	return (Z_OK);
12210Sstevel@tonic-gate }
12220Sstevel@tonic-gate 
1223766Scarlsonj static boolean_t
12240Sstevel@tonic-gate get_doorname(const char *zone_name, char *buffer)
12250Sstevel@tonic-gate {
1226766Scarlsonj 	return (snprintf(buffer, PATH_MAX, "%s" ZONE_DOOR_PATH,
1227766Scarlsonj 	    zonecfg_get_root(), zone_name) < PATH_MAX);
12280Sstevel@tonic-gate }
12290Sstevel@tonic-gate 
12300Sstevel@tonic-gate /*
12310Sstevel@tonic-gate  * system daemons are not audited.  For the global zone, this occurs
12320Sstevel@tonic-gate  * "naturally" since init is started with the default audit
12330Sstevel@tonic-gate  * characteristics.  Since zoneadmd is a system daemon and it starts
12340Sstevel@tonic-gate  * init for a zone, it is necessary to clear out the audit
12350Sstevel@tonic-gate  * characteristics inherited from whomever started zoneadmd.  This is
12360Sstevel@tonic-gate  * indicated by the audit id, which is set from the ruid parameter of
12370Sstevel@tonic-gate  * adt_set_user(), below.
12380Sstevel@tonic-gate  */
12390Sstevel@tonic-gate 
12400Sstevel@tonic-gate static void
12410Sstevel@tonic-gate prepare_audit_context()
12420Sstevel@tonic-gate {
12430Sstevel@tonic-gate 	adt_session_data_t	*ah;
12440Sstevel@tonic-gate 	char			*failure = gettext("audit failure: %s");
12450Sstevel@tonic-gate 
12460Sstevel@tonic-gate 	if (adt_start_session(&ah, NULL, 0)) {
12470Sstevel@tonic-gate 		zerror(failure, strerror(errno));
12480Sstevel@tonic-gate 		return;
12490Sstevel@tonic-gate 	}
12500Sstevel@tonic-gate 	if (adt_set_user(ah, ADT_NO_AUDIT, ADT_NO_AUDIT,
12510Sstevel@tonic-gate 	    ADT_NO_AUDIT, ADT_NO_AUDIT, NULL, ADT_NEW)) {
12520Sstevel@tonic-gate 		zerror(failure, strerror(errno));
12530Sstevel@tonic-gate 		(void) adt_end_session(ah);
12540Sstevel@tonic-gate 		return;
12550Sstevel@tonic-gate 	}
12560Sstevel@tonic-gate 	if (adt_set_proc(ah))
12570Sstevel@tonic-gate 		zerror(failure, strerror(errno));
12580Sstevel@tonic-gate 
12590Sstevel@tonic-gate 	(void) adt_end_session(ah);
12600Sstevel@tonic-gate }
12610Sstevel@tonic-gate 
12620Sstevel@tonic-gate static int
12630Sstevel@tonic-gate start_zoneadmd(const char *zone_name)
12640Sstevel@tonic-gate {
12650Sstevel@tonic-gate 	char doorpath[PATH_MAX];
12660Sstevel@tonic-gate 	pid_t child_pid;
12670Sstevel@tonic-gate 	int error = Z_ERR;
12680Sstevel@tonic-gate 	int doorfd, lockfd;
12690Sstevel@tonic-gate 	struct door_info info;
12700Sstevel@tonic-gate 
1271766Scarlsonj 	if (!get_doorname(zone_name, doorpath))
1272766Scarlsonj 		return (Z_ERR);
12730Sstevel@tonic-gate 
12740Sstevel@tonic-gate 	if (grab_lock_file(zone_name, &lockfd) != Z_OK)
12750Sstevel@tonic-gate 		return (Z_ERR);
12760Sstevel@tonic-gate 
12770Sstevel@tonic-gate 	/*
12780Sstevel@tonic-gate 	 * Now that we have the lock, re-confirm that the daemon is
12790Sstevel@tonic-gate 	 * *not* up and working fine.  If it is still down, we have a green
12800Sstevel@tonic-gate 	 * light to start it.
12810Sstevel@tonic-gate 	 */
12820Sstevel@tonic-gate 	if ((doorfd = open(doorpath, O_RDONLY)) < 0) {
12830Sstevel@tonic-gate 		if (errno != ENOENT) {
12840Sstevel@tonic-gate 			zperror(doorpath, B_FALSE);
12850Sstevel@tonic-gate 			goto out;
12860Sstevel@tonic-gate 		}
12870Sstevel@tonic-gate 	} else {
12880Sstevel@tonic-gate 		if (door_info(doorfd, &info) == 0 &&
12890Sstevel@tonic-gate 		    ((info.di_attributes & DOOR_REVOKED) == 0)) {
12900Sstevel@tonic-gate 			error = Z_OK;
12910Sstevel@tonic-gate 			(void) close(doorfd);
12920Sstevel@tonic-gate 			goto out;
12930Sstevel@tonic-gate 		}
12940Sstevel@tonic-gate 		(void) close(doorfd);
12950Sstevel@tonic-gate 	}
12960Sstevel@tonic-gate 
12970Sstevel@tonic-gate 	if ((child_pid = fork()) == -1) {
12980Sstevel@tonic-gate 		zperror(gettext("could not fork"), B_FALSE);
12990Sstevel@tonic-gate 		goto out;
13000Sstevel@tonic-gate 	} else if (child_pid == 0) {
1301766Scarlsonj 		const char *argv[6], **ap;
1302766Scarlsonj 
13030Sstevel@tonic-gate 		/* child process */
13040Sstevel@tonic-gate 		prepare_audit_context();
13050Sstevel@tonic-gate 
1306766Scarlsonj 		ap = argv;
1307766Scarlsonj 		*ap++ = "zoneadmd";
1308766Scarlsonj 		*ap++ = "-z";
1309766Scarlsonj 		*ap++ = zone_name;
1310766Scarlsonj 		if (zonecfg_in_alt_root()) {
1311766Scarlsonj 			*ap++ = "-R";
1312766Scarlsonj 			*ap++ = zonecfg_get_root();
1313766Scarlsonj 		}
1314766Scarlsonj 		*ap = NULL;
1315766Scarlsonj 
1316766Scarlsonj 		(void) execv("/usr/lib/zones/zoneadmd", (char * const *)argv);
1317924Sgjelinek 		/*
1318924Sgjelinek 		 * TRANSLATION_NOTE
1319924Sgjelinek 		 * zoneadmd is a literal that should not be translated.
1320924Sgjelinek 		 */
13210Sstevel@tonic-gate 		zperror(gettext("could not exec zoneadmd"), B_FALSE);
13220Sstevel@tonic-gate 		_exit(Z_ERR);
13230Sstevel@tonic-gate 	} else {
13240Sstevel@tonic-gate 		/* parent process */
13250Sstevel@tonic-gate 		pid_t retval;
13260Sstevel@tonic-gate 		int pstatus = 0;
13270Sstevel@tonic-gate 
13280Sstevel@tonic-gate 		do {
13290Sstevel@tonic-gate 			retval = waitpid(child_pid, &pstatus, 0);
13300Sstevel@tonic-gate 		} while (retval != child_pid);
13310Sstevel@tonic-gate 		if (WIFSIGNALED(pstatus) || (WIFEXITED(pstatus) &&
13320Sstevel@tonic-gate 		    WEXITSTATUS(pstatus) != 0)) {
13330Sstevel@tonic-gate 			zerror(gettext("could not start %s"), "zoneadmd");
13340Sstevel@tonic-gate 			goto out;
13350Sstevel@tonic-gate 		}
13360Sstevel@tonic-gate 	}
13370Sstevel@tonic-gate 	error = Z_OK;
13380Sstevel@tonic-gate out:
13390Sstevel@tonic-gate 	release_lock_file(lockfd);
13400Sstevel@tonic-gate 	return (error);
13410Sstevel@tonic-gate }
13420Sstevel@tonic-gate 
13430Sstevel@tonic-gate static int
13440Sstevel@tonic-gate ping_zoneadmd(const char *zone_name)
13450Sstevel@tonic-gate {
13460Sstevel@tonic-gate 	char doorpath[PATH_MAX];
13470Sstevel@tonic-gate 	int doorfd;
13480Sstevel@tonic-gate 	struct door_info info;
13490Sstevel@tonic-gate 
1350766Scarlsonj 	if (!get_doorname(zone_name, doorpath))
1351766Scarlsonj 		return (Z_ERR);
13520Sstevel@tonic-gate 
13530Sstevel@tonic-gate 	if ((doorfd = open(doorpath, O_RDONLY)) < 0) {
13540Sstevel@tonic-gate 		return (Z_ERR);
13550Sstevel@tonic-gate 	}
13560Sstevel@tonic-gate 	if (door_info(doorfd, &info) == 0 &&
13570Sstevel@tonic-gate 	    ((info.di_attributes & DOOR_REVOKED) == 0)) {
13580Sstevel@tonic-gate 		(void) close(doorfd);
13590Sstevel@tonic-gate 		return (Z_OK);
13600Sstevel@tonic-gate 	}
13610Sstevel@tonic-gate 	(void) close(doorfd);
13620Sstevel@tonic-gate 	return (Z_ERR);
13630Sstevel@tonic-gate }
13640Sstevel@tonic-gate 
13650Sstevel@tonic-gate static int
13660Sstevel@tonic-gate call_zoneadmd(const char *zone_name, zone_cmd_arg_t *arg)
13670Sstevel@tonic-gate {
13680Sstevel@tonic-gate 	char doorpath[PATH_MAX];
13690Sstevel@tonic-gate 	int doorfd, result;
13700Sstevel@tonic-gate 	door_arg_t darg;
13710Sstevel@tonic-gate 
13720Sstevel@tonic-gate 	zoneid_t zoneid;
13730Sstevel@tonic-gate 	uint64_t uniqid = 0;
13740Sstevel@tonic-gate 
13750Sstevel@tonic-gate 	zone_cmd_rval_t *rvalp;
13760Sstevel@tonic-gate 	size_t rlen;
13770Sstevel@tonic-gate 	char *cp, *errbuf;
13780Sstevel@tonic-gate 
13790Sstevel@tonic-gate 	rlen = getpagesize();
13800Sstevel@tonic-gate 	if ((rvalp = malloc(rlen)) == NULL) {
13810Sstevel@tonic-gate 		zerror(gettext("failed to allocate %lu bytes: %s"), rlen,
13820Sstevel@tonic-gate 		    strerror(errno));
13830Sstevel@tonic-gate 		return (-1);
13840Sstevel@tonic-gate 	}
13850Sstevel@tonic-gate 
13860Sstevel@tonic-gate 	if ((zoneid = getzoneidbyname(zone_name)) != ZONE_ID_UNDEFINED) {
13870Sstevel@tonic-gate 		(void) zone_getattr(zoneid, ZONE_ATTR_UNIQID, &uniqid,
13880Sstevel@tonic-gate 		    sizeof (uniqid));
13890Sstevel@tonic-gate 	}
13900Sstevel@tonic-gate 	arg->uniqid = uniqid;
13910Sstevel@tonic-gate 	(void) strlcpy(arg->locale, locale, sizeof (arg->locale));
1392766Scarlsonj 	if (!get_doorname(zone_name, doorpath)) {
1393766Scarlsonj 		zerror(gettext("alternate root path is too long"));
1394766Scarlsonj 		free(rvalp);
1395766Scarlsonj 		return (-1);
1396766Scarlsonj 	}
13970Sstevel@tonic-gate 
13980Sstevel@tonic-gate 	/*
13990Sstevel@tonic-gate 	 * Loop trying to start zoneadmd; if something goes seriously
14000Sstevel@tonic-gate 	 * wrong we break out and fail.
14010Sstevel@tonic-gate 	 */
14020Sstevel@tonic-gate 	for (;;) {
14030Sstevel@tonic-gate 		if (start_zoneadmd(zone_name) != Z_OK)
14040Sstevel@tonic-gate 			break;
14050Sstevel@tonic-gate 
14060Sstevel@tonic-gate 		if ((doorfd = open(doorpath, O_RDONLY)) < 0) {
14070Sstevel@tonic-gate 			zperror(gettext("failed to open zone door"), B_FALSE);
14080Sstevel@tonic-gate 			break;
14090Sstevel@tonic-gate 		}
14100Sstevel@tonic-gate 
14110Sstevel@tonic-gate 		darg.data_ptr = (char *)arg;
14120Sstevel@tonic-gate 		darg.data_size = sizeof (*arg);
14130Sstevel@tonic-gate 		darg.desc_ptr = NULL;
14140Sstevel@tonic-gate 		darg.desc_num = 0;
14150Sstevel@tonic-gate 		darg.rbuf = (char *)rvalp;
14160Sstevel@tonic-gate 		darg.rsize = rlen;
14170Sstevel@tonic-gate 		if (door_call(doorfd, &darg) != 0) {
14180Sstevel@tonic-gate 			(void) close(doorfd);
14190Sstevel@tonic-gate 			/*
14200Sstevel@tonic-gate 			 * We'll get EBADF if the door has been revoked.
14210Sstevel@tonic-gate 			 */
14220Sstevel@tonic-gate 			if (errno != EBADF) {
14230Sstevel@tonic-gate 				zperror(gettext("door_call failed"), B_FALSE);
14240Sstevel@tonic-gate 				break;
14250Sstevel@tonic-gate 			}
14260Sstevel@tonic-gate 			continue;	/* take another lap */
14270Sstevel@tonic-gate 		}
14280Sstevel@tonic-gate 		(void) close(doorfd);
14290Sstevel@tonic-gate 
14300Sstevel@tonic-gate 		if (darg.data_size == 0) {
14310Sstevel@tonic-gate 			/* Door server is going away; kick it again. */
14320Sstevel@tonic-gate 			continue;
14330Sstevel@tonic-gate 		}
14340Sstevel@tonic-gate 
14350Sstevel@tonic-gate 		errbuf = rvalp->errbuf;
14360Sstevel@tonic-gate 		while (*errbuf != '\0') {
14370Sstevel@tonic-gate 			/*
14380Sstevel@tonic-gate 			 * Remove any newlines since zerror()
14390Sstevel@tonic-gate 			 * will append one automatically.
14400Sstevel@tonic-gate 			 */
14410Sstevel@tonic-gate 			cp = strchr(errbuf, '\n');
14420Sstevel@tonic-gate 			if (cp != NULL)
14430Sstevel@tonic-gate 				*cp = '\0';
14440Sstevel@tonic-gate 			zerror("%s", errbuf);
14450Sstevel@tonic-gate 			if (cp == NULL)
14460Sstevel@tonic-gate 				break;
14470Sstevel@tonic-gate 			errbuf = cp + 1;
14480Sstevel@tonic-gate 		}
14490Sstevel@tonic-gate 		result = rvalp->rval == 0 ? 0 : -1;
14500Sstevel@tonic-gate 		free(rvalp);
14510Sstevel@tonic-gate 		return (result);
14520Sstevel@tonic-gate 	}
14530Sstevel@tonic-gate 
14540Sstevel@tonic-gate 	free(rvalp);
14550Sstevel@tonic-gate 	return (-1);
14560Sstevel@tonic-gate }
14570Sstevel@tonic-gate 
14580Sstevel@tonic-gate static int
14593339Szt129084 invoke_brand_handler(int cmd_num, char *argv[])
14603339Szt129084 {
14613339Szt129084 	zone_dochandle_t handle;
14623339Szt129084 	int err;
14633339Szt129084 
14643339Szt129084 	if ((handle = zonecfg_init_handle()) == NULL) {
14653339Szt129084 		zperror(cmd_to_str(cmd_num), B_TRUE);
14663339Szt129084 		return (Z_ERR);
14673339Szt129084 	}
14683339Szt129084 	if ((err = zonecfg_get_handle(target_zone, handle)) != Z_OK) {
14693339Szt129084 		errno = err;
14703339Szt129084 		zperror(cmd_to_str(cmd_num), B_TRUE);
14713339Szt129084 		zonecfg_fini_handle(handle);
14723339Szt129084 		return (Z_ERR);
14733339Szt129084 	}
14743339Szt129084 	if (verify_brand(handle, cmd_num, argv) != Z_OK) {
14753339Szt129084 		zonecfg_fini_handle(handle);
14763339Szt129084 		return (Z_ERR);
14773339Szt129084 	}
14783339Szt129084 	zonecfg_fini_handle(handle);
14793339Szt129084 	return (Z_OK);
14803339Szt129084 }
14813339Szt129084 
14823339Szt129084 static int
14830Sstevel@tonic-gate ready_func(int argc, char *argv[])
14840Sstevel@tonic-gate {
14850Sstevel@tonic-gate 	zone_cmd_arg_t zarg;
14860Sstevel@tonic-gate 	int arg;
14870Sstevel@tonic-gate 
1488766Scarlsonj 	if (zonecfg_in_alt_root()) {
1489766Scarlsonj 		zerror(gettext("cannot ready zone in alternate root"));
1490766Scarlsonj 		return (Z_ERR);
1491766Scarlsonj 	}
1492766Scarlsonj 
14930Sstevel@tonic-gate 	optind = 0;
14940Sstevel@tonic-gate 	if ((arg = getopt(argc, argv, "?")) != EOF) {
14950Sstevel@tonic-gate 		switch (arg) {
14960Sstevel@tonic-gate 		case '?':
14970Sstevel@tonic-gate 			sub_usage(SHELP_READY, CMD_READY);
14980Sstevel@tonic-gate 			return (optopt == '?' ? Z_OK : Z_USAGE);
14990Sstevel@tonic-gate 		default:
15000Sstevel@tonic-gate 			sub_usage(SHELP_READY, CMD_READY);
15010Sstevel@tonic-gate 			return (Z_USAGE);
15020Sstevel@tonic-gate 		}
15030Sstevel@tonic-gate 	}
15040Sstevel@tonic-gate 	if (argc > optind) {
15050Sstevel@tonic-gate 		sub_usage(SHELP_READY, CMD_READY);
15060Sstevel@tonic-gate 		return (Z_USAGE);
15070Sstevel@tonic-gate 	}
15082712Snn35248 	if (sanity_check(target_zone, CMD_READY, B_FALSE, B_FALSE, B_FALSE)
15092712Snn35248 	    != Z_OK)
15100Sstevel@tonic-gate 		return (Z_ERR);
15113339Szt129084 	if (verify_details(CMD_READY, argv) != Z_OK)
15120Sstevel@tonic-gate 		return (Z_ERR);
15130Sstevel@tonic-gate 
15140Sstevel@tonic-gate 	zarg.cmd = Z_READY;
15150Sstevel@tonic-gate 	if (call_zoneadmd(target_zone, &zarg) != 0) {
15160Sstevel@tonic-gate 		zerror(gettext("call to %s failed"), "zoneadmd");
15170Sstevel@tonic-gate 		return (Z_ERR);
15180Sstevel@tonic-gate 	}
15190Sstevel@tonic-gate 	return (Z_OK);
15200Sstevel@tonic-gate }
15210Sstevel@tonic-gate 
15220Sstevel@tonic-gate static int
15230Sstevel@tonic-gate boot_func(int argc, char *argv[])
15240Sstevel@tonic-gate {
15250Sstevel@tonic-gate 	zone_cmd_arg_t zarg;
15262712Snn35248 	boolean_t force = B_FALSE;
15270Sstevel@tonic-gate 	int arg;
15280Sstevel@tonic-gate 
1529766Scarlsonj 	if (zonecfg_in_alt_root()) {
1530766Scarlsonj 		zerror(gettext("cannot boot zone in alternate root"));
1531766Scarlsonj 		return (Z_ERR);
1532766Scarlsonj 	}
1533766Scarlsonj 
15340Sstevel@tonic-gate 	zarg.bootbuf[0] = '\0';
15350Sstevel@tonic-gate 
15360Sstevel@tonic-gate 	/*
15372267Sdp 	 * The following getopt processes arguments to zone boot; that
15382267Sdp 	 * is to say, the [here] portion of the argument string:
15392267Sdp 	 *
15402267Sdp 	 *	zoneadm -z myzone boot [here] -- -v -m verbose
15412267Sdp 	 *
15422267Sdp 	 * Where [here] can either be nothing, -? (in which case we bail
15432712Snn35248 	 * and print usage), -f (a private option to indicate that the
15442712Snn35248 	 * boot operation should be 'forced'), or -s.  Support for -s is
15452712Snn35248 	 * vestigal and obsolete, but is retained because it was a
15462712Snn35248 	 * documented interface and there are known consumers including
15472712Snn35248 	 * admin/install; the proper way to specify boot arguments like -s
15482712Snn35248 	 * is:
15492267Sdp 	 *
15502267Sdp 	 *	zoneadm -z myzone boot -- -s -v -m verbose.
15510Sstevel@tonic-gate 	 */
15520Sstevel@tonic-gate 	optind = 0;
15532712Snn35248 	while ((arg = getopt(argc, argv, "?fs")) != EOF) {
15540Sstevel@tonic-gate 		switch (arg) {
15550Sstevel@tonic-gate 		case '?':
15560Sstevel@tonic-gate 			sub_usage(SHELP_BOOT, CMD_BOOT);
15570Sstevel@tonic-gate 			return (optopt == '?' ? Z_OK : Z_USAGE);
15580Sstevel@tonic-gate 		case 's':
15590Sstevel@tonic-gate 			(void) strlcpy(zarg.bootbuf, "-s",
15600Sstevel@tonic-gate 			    sizeof (zarg.bootbuf));
15610Sstevel@tonic-gate 			break;
15622712Snn35248 		case 'f':
15632712Snn35248 			force = B_TRUE;
15642712Snn35248 			break;
15650Sstevel@tonic-gate 		default:
15660Sstevel@tonic-gate 			sub_usage(SHELP_BOOT, CMD_BOOT);
15670Sstevel@tonic-gate 			return (Z_USAGE);
15680Sstevel@tonic-gate 		}
15690Sstevel@tonic-gate 	}
15702267Sdp 
15712267Sdp 	for (; optind < argc; optind++) {
15722267Sdp 		if (strlcat(zarg.bootbuf, argv[optind],
15732267Sdp 		    sizeof (zarg.bootbuf)) >= sizeof (zarg.bootbuf)) {
15742267Sdp 			zerror(gettext("Boot argument list too long"));
15752267Sdp 			return (Z_ERR);
15762267Sdp 		}
15772267Sdp 		if (optind < argc - 1)
15782267Sdp 			if (strlcat(zarg.bootbuf, " ", sizeof (zarg.bootbuf)) >=
15792267Sdp 			    sizeof (zarg.bootbuf)) {
15802267Sdp 				zerror(gettext("Boot argument list too long"));
15812267Sdp 				return (Z_ERR);
15822267Sdp 			}
15832267Sdp 	}
15842712Snn35248 	if (sanity_check(target_zone, CMD_BOOT, B_FALSE, B_FALSE, force)
15852712Snn35248 	    != Z_OK)
15860Sstevel@tonic-gate 		return (Z_ERR);
15873339Szt129084 	if (verify_details(CMD_BOOT, argv) != Z_OK)
15880Sstevel@tonic-gate 		return (Z_ERR);
15892712Snn35248 	zarg.cmd = force ? Z_FORCEBOOT : Z_BOOT;
15900Sstevel@tonic-gate 	if (call_zoneadmd(target_zone, &zarg) != 0) {
15910Sstevel@tonic-gate 		zerror(gettext("call to %s failed"), "zoneadmd");
15920Sstevel@tonic-gate 		return (Z_ERR);
15930Sstevel@tonic-gate 	}
15943247Sgjelinek 
15950Sstevel@tonic-gate 	return (Z_OK);
15960Sstevel@tonic-gate }
15970Sstevel@tonic-gate 
15980Sstevel@tonic-gate static void
15990Sstevel@tonic-gate fake_up_local_zone(zoneid_t zid, zone_entry_t *zeptr)
16000Sstevel@tonic-gate {
16010Sstevel@tonic-gate 	ssize_t result;
16022303Scarlsonj 	uuid_t uuid;
16032303Scarlsonj 	FILE *fp;
16043448Sdh155122 	ushort_t flags;
16052303Scarlsonj 
16062303Scarlsonj 	(void) memset(zeptr, 0, sizeof (*zeptr));
16070Sstevel@tonic-gate 
16080Sstevel@tonic-gate 	zeptr->zid = zid;
16092303Scarlsonj 
16100Sstevel@tonic-gate 	/*
16110Sstevel@tonic-gate 	 * Since we're looking up our own (non-global) zone name,
16120Sstevel@tonic-gate 	 * we can be assured that it will succeed.
16130Sstevel@tonic-gate 	 */
16140Sstevel@tonic-gate 	result = getzonenamebyid(zid, zeptr->zname, sizeof (zeptr->zname));
16150Sstevel@tonic-gate 	assert(result >= 0);
16162303Scarlsonj 	if (zonecfg_is_scratch(zeptr->zname) &&
16172303Scarlsonj 	    (fp = zonecfg_open_scratch("", B_FALSE)) != NULL) {
16182303Scarlsonj 		(void) zonecfg_reverse_scratch(fp, zeptr->zname, zeptr->zname,
16192303Scarlsonj 		    sizeof (zeptr->zname), NULL, 0);
16202303Scarlsonj 		zonecfg_close_scratch(fp);
16212303Scarlsonj 	}
16222303Scarlsonj 
16232303Scarlsonj 	if (is_system_labeled()) {
16241676Sjpk 		(void) zone_getattr(zid, ZONE_ATTR_ROOT, zeptr->zroot,
16251676Sjpk 		    sizeof (zeptr->zroot));
16262712Snn35248 		(void) strlcpy(zeptr->zbrand, NATIVE_BRAND_NAME,
16272712Snn35248 			    sizeof (zeptr->zbrand));
16282303Scarlsonj 	} else {
16292303Scarlsonj 		(void) strlcpy(zeptr->zroot, "/", sizeof (zeptr->zroot));
16302712Snn35248 		(void) zone_getattr(zid, ZONE_ATTR_BRAND, zeptr->zbrand,
16312712Snn35248 		    sizeof (zeptr->zbrand));
16322303Scarlsonj 	}
16332303Scarlsonj 
16340Sstevel@tonic-gate 	zeptr->zstate_str = "running";
16352303Scarlsonj 	if (zonecfg_get_uuid(zeptr->zname, uuid) == Z_OK &&
16362303Scarlsonj 	    !uuid_is_null(uuid))
16372303Scarlsonj 		uuid_unparse(uuid, zeptr->zuuid);
16383448Sdh155122 
16393448Sdh155122 	if (zone_getattr(zid, ZONE_ATTR_FLAGS, &flags, sizeof (flags)) < 0) {
16403448Sdh155122 		zperror2(zeptr->zname, gettext("could not get zone flags"));
16413448Sdh155122 		exit(Z_ERR);
16423448Sdh155122 	}
16433448Sdh155122 	if (flags & ZF_NET_EXCL)
16443448Sdh155122 		zeptr->ziptype = ZS_EXCLUSIVE;
16453448Sdh155122 	else
16463448Sdh155122 		zeptr->ziptype = ZS_SHARED;
16470Sstevel@tonic-gate }
16480Sstevel@tonic-gate 
16490Sstevel@tonic-gate static int
16500Sstevel@tonic-gate list_func(int argc, char *argv[])
16510Sstevel@tonic-gate {
16520Sstevel@tonic-gate 	zone_entry_t *zentp, zent;
1653766Scarlsonj 	int arg, retv;
16540Sstevel@tonic-gate 	boolean_t output = B_FALSE, verbose = B_FALSE, parsable = B_FALSE;
16550Sstevel@tonic-gate 	zone_state_t min_state = ZONE_STATE_RUNNING;
16560Sstevel@tonic-gate 	zoneid_t zone_id = getzoneid();
16570Sstevel@tonic-gate 
16580Sstevel@tonic-gate 	if (target_zone == NULL) {
16590Sstevel@tonic-gate 		/* all zones: default view to running but allow override */
16600Sstevel@tonic-gate 		optind = 0;
16610Sstevel@tonic-gate 		while ((arg = getopt(argc, argv, "?cipv")) != EOF) {
16620Sstevel@tonic-gate 			switch (arg) {
16630Sstevel@tonic-gate 			case '?':
16640Sstevel@tonic-gate 				sub_usage(SHELP_LIST, CMD_LIST);
16650Sstevel@tonic-gate 				return (optopt == '?' ? Z_OK : Z_USAGE);
16661339Sjonb 				/*
16671339Sjonb 				 * The 'i' and 'c' options are not mutually
16681339Sjonb 				 * exclusive so if 'c' is given, then min_state
16691339Sjonb 				 * is set to 0 (ZONE_STATE_CONFIGURED) which is
16701339Sjonb 				 * the lowest possible state.  If 'i' is given,
16711339Sjonb 				 * then min_state is set to be the lowest state
16721339Sjonb 				 * so far.
16731339Sjonb 				 */
16740Sstevel@tonic-gate 			case 'c':
16750Sstevel@tonic-gate 				min_state = ZONE_STATE_CONFIGURED;
16760Sstevel@tonic-gate 				break;
16770Sstevel@tonic-gate 			case 'i':
16781339Sjonb 				min_state = min(ZONE_STATE_INSTALLED,
16791339Sjonb 				    min_state);
16801339Sjonb 
16810Sstevel@tonic-gate 				break;
16820Sstevel@tonic-gate 			case 'p':
16830Sstevel@tonic-gate 				parsable = B_TRUE;
16840Sstevel@tonic-gate 				break;
16850Sstevel@tonic-gate 			case 'v':
16860Sstevel@tonic-gate 				verbose = B_TRUE;
16870Sstevel@tonic-gate 				break;
16880Sstevel@tonic-gate 			default:
16890Sstevel@tonic-gate 				sub_usage(SHELP_LIST, CMD_LIST);
16900Sstevel@tonic-gate 				return (Z_USAGE);
16910Sstevel@tonic-gate 			}
16920Sstevel@tonic-gate 		}
16930Sstevel@tonic-gate 		if (parsable && verbose) {
16940Sstevel@tonic-gate 			zerror(gettext("%s -p and -v are mutually exclusive."),
16950Sstevel@tonic-gate 			    cmd_to_str(CMD_LIST));
16960Sstevel@tonic-gate 			return (Z_ERR);
16970Sstevel@tonic-gate 		}
16981676Sjpk 		if (zone_id == GLOBAL_ZONEID || is_system_labeled()) {
1699766Scarlsonj 			retv = zone_print_list(min_state, verbose, parsable);
17000Sstevel@tonic-gate 		} else {
17012712Snn35248 			fake_up_local_zone(zone_id, &zent);
1702766Scarlsonj 			retv = Z_OK;
17030Sstevel@tonic-gate 			zone_print(&zent, verbose, parsable);
17040Sstevel@tonic-gate 		}
1705766Scarlsonj 		return (retv);
17060Sstevel@tonic-gate 	}
17070Sstevel@tonic-gate 
17080Sstevel@tonic-gate 	/*
17090Sstevel@tonic-gate 	 * Specific target zone: disallow -i/-c suboptions.
17100Sstevel@tonic-gate 	 */
17110Sstevel@tonic-gate 	optind = 0;
17120Sstevel@tonic-gate 	while ((arg = getopt(argc, argv, "?pv")) != EOF) {
17130Sstevel@tonic-gate 		switch (arg) {
17140Sstevel@tonic-gate 		case '?':
17150Sstevel@tonic-gate 			sub_usage(SHELP_LIST, CMD_LIST);
17160Sstevel@tonic-gate 			return (optopt == '?' ? Z_OK : Z_USAGE);
17170Sstevel@tonic-gate 		case 'p':
17180Sstevel@tonic-gate 			parsable = B_TRUE;
17190Sstevel@tonic-gate 			break;
17200Sstevel@tonic-gate 		case 'v':
17210Sstevel@tonic-gate 			verbose = B_TRUE;
17220Sstevel@tonic-gate 			break;
17230Sstevel@tonic-gate 		default:
17240Sstevel@tonic-gate 			sub_usage(SHELP_LIST, CMD_LIST);
17250Sstevel@tonic-gate 			return (Z_USAGE);
17260Sstevel@tonic-gate 		}
17270Sstevel@tonic-gate 	}
17280Sstevel@tonic-gate 	if (parsable && verbose) {
17290Sstevel@tonic-gate 		zerror(gettext("%s -p and -v are mutually exclusive."),
17300Sstevel@tonic-gate 		    cmd_to_str(CMD_LIST));
17310Sstevel@tonic-gate 		return (Z_ERR);
17320Sstevel@tonic-gate 	}
17330Sstevel@tonic-gate 	if (argc > optind) {
17340Sstevel@tonic-gate 		sub_usage(SHELP_LIST, CMD_LIST);
17350Sstevel@tonic-gate 		return (Z_USAGE);
17360Sstevel@tonic-gate 	}
17370Sstevel@tonic-gate 	if (zone_id != GLOBAL_ZONEID) {
17380Sstevel@tonic-gate 		fake_up_local_zone(zone_id, &zent);
17390Sstevel@tonic-gate 		/*
17400Sstevel@tonic-gate 		 * main() will issue a Z_NO_ZONE error if it cannot get an
17410Sstevel@tonic-gate 		 * id for target_zone, which in a non-global zone should
17420Sstevel@tonic-gate 		 * happen for any zone name except `zonename`.  Thus we
17430Sstevel@tonic-gate 		 * assert() that here but don't otherwise check.
17440Sstevel@tonic-gate 		 */
17450Sstevel@tonic-gate 		assert(strcmp(zent.zname, target_zone) == 0);
17460Sstevel@tonic-gate 		zone_print(&zent, verbose, parsable);
17470Sstevel@tonic-gate 		output = B_TRUE;
17480Sstevel@tonic-gate 	} else if ((zentp = lookup_running_zone(target_zone)) != NULL) {
17490Sstevel@tonic-gate 		zone_print(zentp, verbose, parsable);
17500Sstevel@tonic-gate 		output = B_TRUE;
1751766Scarlsonj 	} else if (lookup_zone_info(target_zone, ZONE_ID_UNDEFINED,
1752766Scarlsonj 	    &zent) == Z_OK) {
17530Sstevel@tonic-gate 		zone_print(&zent, verbose, parsable);
17540Sstevel@tonic-gate 		output = B_TRUE;
17550Sstevel@tonic-gate 	}
17563339Szt129084 
17573339Szt129084 	/*
17583339Szt129084 	 * Invoke brand-specific handler. Note that we do this
1759*3542Sgjelinek 	 * only if we're in the global zone, and target_zone is specified
1760*3542Sgjelinek 	 * and it is not the global zone.
17613339Szt129084 	 */
1762*3542Sgjelinek 	if (zone_id == GLOBAL_ZONEID && target_zone != NULL &&
1763*3542Sgjelinek 	    strcmp(target_zone, GLOBAL_ZONENAME) != 0)
17643339Szt129084 		if (invoke_brand_handler(CMD_LIST, argv) != Z_OK)
17653339Szt129084 			return (Z_ERR);
17663339Szt129084 
17670Sstevel@tonic-gate 	return (output ? Z_OK : Z_ERR);
17680Sstevel@tonic-gate }
17690Sstevel@tonic-gate 
17700Sstevel@tonic-gate static void
17710Sstevel@tonic-gate sigterm(int sig)
17720Sstevel@tonic-gate {
17730Sstevel@tonic-gate 	/*
17740Sstevel@tonic-gate 	 * Ignore SIG{INT,TERM}, so we don't end up in an infinite loop,
17750Sstevel@tonic-gate 	 * then propagate the signal to our process group.
17760Sstevel@tonic-gate 	 */
17772712Snn35248 	assert(sig == SIGINT || sig == SIGTERM);
17780Sstevel@tonic-gate 	(void) sigset(SIGINT, SIG_IGN);
17790Sstevel@tonic-gate 	(void) sigset(SIGTERM, SIG_IGN);
17800Sstevel@tonic-gate 	(void) kill(0, sig);
17810Sstevel@tonic-gate 	child_killed = B_TRUE;
17820Sstevel@tonic-gate }
17830Sstevel@tonic-gate 
17840Sstevel@tonic-gate static int
17850Sstevel@tonic-gate do_subproc(char *cmdbuf)
17860Sstevel@tonic-gate {
17870Sstevel@tonic-gate 	char inbuf[1024];	/* arbitrary large amount */
17880Sstevel@tonic-gate 	FILE *file;
17890Sstevel@tonic-gate 
17902712Snn35248 	do_subproc_cnt++;
17910Sstevel@tonic-gate 	child_killed = B_FALSE;
17920Sstevel@tonic-gate 	/*
17930Sstevel@tonic-gate 	 * We use popen(3c) to launch child processes for [un]install;
17940Sstevel@tonic-gate 	 * this library call does not return a PID, so we have to kill
17950Sstevel@tonic-gate 	 * the whole process group.  To avoid killing our parent, we
17960Sstevel@tonic-gate 	 * become a process group leader here.  But doing so can wreak
17970Sstevel@tonic-gate 	 * havoc with reading from stdin when launched by a non-job-control
17980Sstevel@tonic-gate 	 * shell, so we close stdin and reopen it as /dev/null first.
17990Sstevel@tonic-gate 	 */
18000Sstevel@tonic-gate 	(void) close(STDIN_FILENO);
18012712Snn35248 	(void) openat(STDIN_FILENO, "/dev/null", O_RDONLY);
18022712Snn35248 	if (!zoneadm_is_nested)
18032712Snn35248 		(void) setpgid(0, 0);
18040Sstevel@tonic-gate 	(void) sigset(SIGINT, sigterm);
18050Sstevel@tonic-gate 	(void) sigset(SIGTERM, sigterm);
18060Sstevel@tonic-gate 	file = popen(cmdbuf, "r");
18070Sstevel@tonic-gate 	for (;;) {
18080Sstevel@tonic-gate 		if (child_killed || fgets(inbuf, sizeof (inbuf), file) == NULL)
18090Sstevel@tonic-gate 			break;
18100Sstevel@tonic-gate 		(void) fputs(inbuf, stdout);
18110Sstevel@tonic-gate 	}
18120Sstevel@tonic-gate 	(void) sigset(SIGINT, SIG_DFL);
18130Sstevel@tonic-gate 	(void) sigset(SIGTERM, SIG_DFL);
18140Sstevel@tonic-gate 	return (pclose(file));
18150Sstevel@tonic-gate }
18160Sstevel@tonic-gate 
18170Sstevel@tonic-gate static int
18182712Snn35248 do_subproc_interactive(char *cmdbuf)
18192712Snn35248 {
18202712Snn35248 	void (*saveint)(int);
18212712Snn35248 	void (*saveterm)(int);
18222712Snn35248 	void (*savequit)(int);
18232712Snn35248 	void (*savehup)(int);
18242712Snn35248 	int pid, child, status;
18252712Snn35248 
18262712Snn35248 	/*
18272712Snn35248 	 * do_subproc() links stdin to /dev/null, which would break any
18282712Snn35248 	 * interactive subprocess we try to launch here.  Similarly, we
18292712Snn35248 	 * can't have been launched as a subprocess ourselves.
18302712Snn35248 	 */
18312712Snn35248 	assert(do_subproc_cnt == 0 && !zoneadm_is_nested);
18322712Snn35248 
18332712Snn35248 	if ((child = vfork()) == 0) {
18342712Snn35248 		(void) execl("/bin/sh", "sh", "-c", cmdbuf, (char *)NULL);
18352712Snn35248 	}
18362712Snn35248 
18372712Snn35248 	if (child == -1)
18382712Snn35248 		return (-1);
18392712Snn35248 
18402712Snn35248 	saveint = sigset(SIGINT, SIG_IGN);
18412712Snn35248 	saveterm = sigset(SIGTERM, SIG_IGN);
18422712Snn35248 	savequit = sigset(SIGQUIT, SIG_IGN);
18432712Snn35248 	savehup = sigset(SIGHUP, SIG_IGN);
18442712Snn35248 
18452712Snn35248 	while ((pid = waitpid(child, &status, 0)) != child && pid != -1)
18462712Snn35248 		;
18472712Snn35248 
18482712Snn35248 	(void) sigset(SIGINT, saveint);
18492712Snn35248 	(void) sigset(SIGTERM, saveterm);
18502712Snn35248 	(void) sigset(SIGQUIT, savequit);
18512712Snn35248 	(void) sigset(SIGHUP, savehup);
18522712Snn35248 
18532712Snn35248 	return (pid == -1 ? -1 : status);
18542712Snn35248 }
18552712Snn35248 
18562712Snn35248 static int
18572712Snn35248 subproc_status(const char *cmd, int status, boolean_t verbose_failure)
18580Sstevel@tonic-gate {
18590Sstevel@tonic-gate 	if (WIFEXITED(status)) {
18600Sstevel@tonic-gate 		int exit_code = WEXITSTATUS(status);
18610Sstevel@tonic-gate 
18622712Snn35248 		if ((verbose_failure) && (exit_code != ZONE_SUBPROC_OK))
18632712Snn35248 			zerror(gettext("'%s' failed with exit code %d."), cmd,
18642712Snn35248 			    exit_code);
18652712Snn35248 
18662712Snn35248 		return (exit_code);
18670Sstevel@tonic-gate 	} else if (WIFSIGNALED(status)) {
18680Sstevel@tonic-gate 		int signal = WTERMSIG(status);
18690Sstevel@tonic-gate 		char sigstr[SIG2STR_MAX];
18700Sstevel@tonic-gate 
18710Sstevel@tonic-gate 		if (sig2str(signal, sigstr) == 0) {
18720Sstevel@tonic-gate 			zerror(gettext("'%s' terminated by signal SIG%s."), cmd,
18730Sstevel@tonic-gate 			    sigstr);
18740Sstevel@tonic-gate 		} else {
18750Sstevel@tonic-gate 			zerror(gettext("'%s' terminated by an unknown signal."),
18760Sstevel@tonic-gate 			    cmd);
18770Sstevel@tonic-gate 		}
18780Sstevel@tonic-gate 	} else {
18790Sstevel@tonic-gate 		zerror(gettext("'%s' failed for unknown reasons."), cmd);
18800Sstevel@tonic-gate 	}
18812712Snn35248 
18822712Snn35248 	/*
18832712Snn35248 	 * Assume a subprocess that died due to a signal or an unknown error
18842712Snn35248 	 * should be considered an exit code of ZONE_SUBPROC_FATAL, as the
18852712Snn35248 	 * user will likely need to do some manual cleanup.
18862712Snn35248 	 */
18872712Snn35248 	return (ZONE_SUBPROC_FATAL);
18880Sstevel@tonic-gate }
18890Sstevel@tonic-gate 
18900Sstevel@tonic-gate /*
18910Sstevel@tonic-gate  * Various sanity checks; make sure:
18920Sstevel@tonic-gate  * 1. We're in the global zone.
18930Sstevel@tonic-gate  * 2. The calling user has sufficient privilege.
18940Sstevel@tonic-gate  * 3. The target zone is neither the global zone nor anything starting with
18950Sstevel@tonic-gate  *    "SUNW".
18960Sstevel@tonic-gate  * 4a. If we're looking for a 'not running' (i.e., configured or installed)
18970Sstevel@tonic-gate  *     zone, the name service knows about it.
18980Sstevel@tonic-gate  * 4b. For some operations which expect a zone not to be running, that it is
18990Sstevel@tonic-gate  *     not already running (or ready).
19000Sstevel@tonic-gate  */
19010Sstevel@tonic-gate static int
19020Sstevel@tonic-gate sanity_check(char *zone, int cmd_num, boolean_t running,
19032712Snn35248     boolean_t unsafe_when_running, boolean_t force)
19040Sstevel@tonic-gate {
19050Sstevel@tonic-gate 	zone_entry_t *zent;
19060Sstevel@tonic-gate 	priv_set_t *privset;
19072712Snn35248 	zone_state_t state, min_state;
1908766Scarlsonj 	char kernzone[ZONENAME_MAX];
1909766Scarlsonj 	FILE *fp;
19100Sstevel@tonic-gate 
19110Sstevel@tonic-gate 	if (getzoneid() != GLOBAL_ZONEID) {
19121645Scomay 		switch (cmd_num) {
19131645Scomay 		case CMD_HALT:
19141645Scomay 			zerror(gettext("use %s to %s this zone."), "halt(1M)",
19151645Scomay 			    cmd_to_str(cmd_num));
19161645Scomay 			break;
19171645Scomay 		case CMD_REBOOT:
19181645Scomay 			zerror(gettext("use %s to %s this zone."),
19191645Scomay 			    "reboot(1M)", cmd_to_str(cmd_num));
19201645Scomay 			break;
19211645Scomay 		default:
19221645Scomay 			zerror(gettext("must be in the global zone to %s a "
19231645Scomay 			    "zone."), cmd_to_str(cmd_num));
19241645Scomay 			break;
19251645Scomay 		}
19260Sstevel@tonic-gate 		return (Z_ERR);
19270Sstevel@tonic-gate 	}
19280Sstevel@tonic-gate 
19290Sstevel@tonic-gate 	if ((privset = priv_allocset()) == NULL) {
19300Sstevel@tonic-gate 		zerror(gettext("%s failed"), "priv_allocset");
19310Sstevel@tonic-gate 		return (Z_ERR);
19320Sstevel@tonic-gate 	}
19330Sstevel@tonic-gate 
19340Sstevel@tonic-gate 	if (getppriv(PRIV_EFFECTIVE, privset) != 0) {
19350Sstevel@tonic-gate 		zerror(gettext("%s failed"), "getppriv");
19360Sstevel@tonic-gate 		priv_freeset(privset);
19370Sstevel@tonic-gate 		return (Z_ERR);
19380Sstevel@tonic-gate 	}
19390Sstevel@tonic-gate 
19400Sstevel@tonic-gate 	if (priv_isfullset(privset) == B_FALSE) {
19410Sstevel@tonic-gate 		zerror(gettext("only a privileged user may %s a zone."),
19420Sstevel@tonic-gate 		    cmd_to_str(cmd_num));
19430Sstevel@tonic-gate 		priv_freeset(privset);
19440Sstevel@tonic-gate 		return (Z_ERR);
19450Sstevel@tonic-gate 	}
19460Sstevel@tonic-gate 	priv_freeset(privset);
19470Sstevel@tonic-gate 
19480Sstevel@tonic-gate 	if (zone == NULL) {
19490Sstevel@tonic-gate 		zerror(gettext("no zone specified"));
19500Sstevel@tonic-gate 		return (Z_ERR);
19510Sstevel@tonic-gate 	}
19520Sstevel@tonic-gate 
19530Sstevel@tonic-gate 	if (strcmp(zone, GLOBAL_ZONENAME) == 0) {
19540Sstevel@tonic-gate 		zerror(gettext("%s operation is invalid for the global zone."),
19550Sstevel@tonic-gate 		    cmd_to_str(cmd_num));
19560Sstevel@tonic-gate 		return (Z_ERR);
19570Sstevel@tonic-gate 	}
19580Sstevel@tonic-gate 
19590Sstevel@tonic-gate 	if (strncmp(zone, "SUNW", 4) == 0) {
19600Sstevel@tonic-gate 		zerror(gettext("%s operation is invalid for zones starting "
19610Sstevel@tonic-gate 		    "with SUNW."), cmd_to_str(cmd_num));
19620Sstevel@tonic-gate 		return (Z_ERR);
19630Sstevel@tonic-gate 	}
19640Sstevel@tonic-gate 
19652712Snn35248 	if (!is_native_zone && cmd_num == CMD_MOUNT) {
19662712Snn35248 		zerror(gettext("%s operation is invalid for branded zones."),
19672712Snn35248 		    cmd_to_str(cmd_num));
19682712Snn35248 			return (Z_ERR);
19692712Snn35248 	}
19702712Snn35248 
1971766Scarlsonj 	if (!zonecfg_in_alt_root()) {
1972766Scarlsonj 		zent = lookup_running_zone(zone);
1973766Scarlsonj 	} else if ((fp = zonecfg_open_scratch("", B_FALSE)) == NULL) {
1974766Scarlsonj 		zent = NULL;
1975766Scarlsonj 	} else {
1976766Scarlsonj 		if (zonecfg_find_scratch(fp, zone, zonecfg_get_root(),
1977766Scarlsonj 		    kernzone, sizeof (kernzone)) == 0)
1978766Scarlsonj 			zent = lookup_running_zone(kernzone);
1979766Scarlsonj 		else
1980766Scarlsonj 			zent = NULL;
1981766Scarlsonj 		zonecfg_close_scratch(fp);
1982766Scarlsonj 	}
1983766Scarlsonj 
19840Sstevel@tonic-gate 	/*
19850Sstevel@tonic-gate 	 * Look up from the kernel for 'running' zones.
19860Sstevel@tonic-gate 	 */
19872712Snn35248 	if (running && !force) {
19880Sstevel@tonic-gate 		if (zent == NULL) {
19890Sstevel@tonic-gate 			zerror(gettext("not running"));
19900Sstevel@tonic-gate 			return (Z_ERR);
19910Sstevel@tonic-gate 		}
19920Sstevel@tonic-gate 	} else {
19930Sstevel@tonic-gate 		int err;
19940Sstevel@tonic-gate 
19950Sstevel@tonic-gate 		if (unsafe_when_running && zent != NULL) {
19960Sstevel@tonic-gate 			/* check whether the zone is ready or running */
19970Sstevel@tonic-gate 			if ((err = zone_get_state(zent->zname,
19980Sstevel@tonic-gate 			    &zent->zstate_num)) != Z_OK) {
19990Sstevel@tonic-gate 				errno = err;
20000Sstevel@tonic-gate 				zperror2(zent->zname,
20010Sstevel@tonic-gate 				    gettext("could not get state"));
20020Sstevel@tonic-gate 				/* can't tell, so hedge */
20030Sstevel@tonic-gate 				zent->zstate_str = "ready/running";
20040Sstevel@tonic-gate 			} else {
20050Sstevel@tonic-gate 				zent->zstate_str =
20060Sstevel@tonic-gate 				    zone_state_str(zent->zstate_num);
20070Sstevel@tonic-gate 			}
20080Sstevel@tonic-gate 			zerror(gettext("%s operation is invalid for %s zones."),
20090Sstevel@tonic-gate 			    cmd_to_str(cmd_num), zent->zstate_str);
20100Sstevel@tonic-gate 			return (Z_ERR);
20110Sstevel@tonic-gate 		}
20120Sstevel@tonic-gate 		if ((err = zone_get_state(zone, &state)) != Z_OK) {
20130Sstevel@tonic-gate 			errno = err;
20140Sstevel@tonic-gate 			zperror2(zone, gettext("could not get state"));
20150Sstevel@tonic-gate 			return (Z_ERR);
20160Sstevel@tonic-gate 		}
20170Sstevel@tonic-gate 		switch (cmd_num) {
20180Sstevel@tonic-gate 		case CMD_UNINSTALL:
20190Sstevel@tonic-gate 			if (state == ZONE_STATE_CONFIGURED) {
20200Sstevel@tonic-gate 				zerror(gettext("is already in state '%s'."),
20210Sstevel@tonic-gate 				    zone_state_str(ZONE_STATE_CONFIGURED));
20220Sstevel@tonic-gate 				return (Z_ERR);
20230Sstevel@tonic-gate 			}
20240Sstevel@tonic-gate 			break;
20251507Sgjelinek 		case CMD_ATTACH:
20261300Sgjelinek 		case CMD_CLONE:
20270Sstevel@tonic-gate 		case CMD_INSTALL:
20280Sstevel@tonic-gate 			if (state == ZONE_STATE_INSTALLED) {
20290Sstevel@tonic-gate 				zerror(gettext("is already %s."),
20300Sstevel@tonic-gate 				    zone_state_str(ZONE_STATE_INSTALLED));
20310Sstevel@tonic-gate 				return (Z_ERR);
20320Sstevel@tonic-gate 			} else if (state == ZONE_STATE_INCOMPLETE) {
20330Sstevel@tonic-gate 				zerror(gettext("zone is %s; %s required."),
20340Sstevel@tonic-gate 				    zone_state_str(ZONE_STATE_INCOMPLETE),
20350Sstevel@tonic-gate 				    cmd_to_str(CMD_UNINSTALL));
20360Sstevel@tonic-gate 				return (Z_ERR);
20370Sstevel@tonic-gate 			}
20380Sstevel@tonic-gate 			break;
20391507Sgjelinek 		case CMD_DETACH:
20401300Sgjelinek 		case CMD_MOVE:
20410Sstevel@tonic-gate 		case CMD_READY:
20420Sstevel@tonic-gate 		case CMD_BOOT:
2043766Scarlsonj 		case CMD_MOUNT:
20442303Scarlsonj 		case CMD_MARK:
20452712Snn35248 			if ((cmd_num == CMD_BOOT || cmd_num == CMD_MOUNT) &&
20462712Snn35248 			    force)
20472712Snn35248 				min_state = ZONE_STATE_INCOMPLETE;
20482712Snn35248 			else
20492712Snn35248 				min_state = ZONE_STATE_INSTALLED;
20502712Snn35248 
20512712Snn35248 			if (force && cmd_num == CMD_BOOT && is_native_zone) {
20522712Snn35248 				zerror(gettext("Only branded zones may be "
20532712Snn35248 				    "force-booted."));
20542712Snn35248 				return (Z_ERR);
20552712Snn35248 			}
20562712Snn35248 
20572712Snn35248 			if (state < min_state) {
20580Sstevel@tonic-gate 				zerror(gettext("must be %s before %s."),
20592712Snn35248 				    zone_state_str(min_state),
20600Sstevel@tonic-gate 				    cmd_to_str(cmd_num));
20610Sstevel@tonic-gate 				return (Z_ERR);
20620Sstevel@tonic-gate 			}
20630Sstevel@tonic-gate 			break;
20640Sstevel@tonic-gate 		case CMD_VERIFY:
20650Sstevel@tonic-gate 			if (state == ZONE_STATE_INCOMPLETE) {
20660Sstevel@tonic-gate 				zerror(gettext("zone is %s; %s required."),
20670Sstevel@tonic-gate 				    zone_state_str(ZONE_STATE_INCOMPLETE),
20680Sstevel@tonic-gate 				    cmd_to_str(CMD_UNINSTALL));
20690Sstevel@tonic-gate 				return (Z_ERR);
20700Sstevel@tonic-gate 			}
20710Sstevel@tonic-gate 			break;
2072766Scarlsonj 		case CMD_UNMOUNT:
2073766Scarlsonj 			if (state != ZONE_STATE_MOUNTED) {
2074766Scarlsonj 				zerror(gettext("must be %s before %s."),
2075766Scarlsonj 				    zone_state_str(ZONE_STATE_MOUNTED),
2076766Scarlsonj 				    cmd_to_str(cmd_num));
2077766Scarlsonj 				return (Z_ERR);
2078766Scarlsonj 			}
2079766Scarlsonj 			break;
20800Sstevel@tonic-gate 		}
20810Sstevel@tonic-gate 	}
20820Sstevel@tonic-gate 	return (Z_OK);
20830Sstevel@tonic-gate }
20840Sstevel@tonic-gate 
20850Sstevel@tonic-gate static int
20860Sstevel@tonic-gate halt_func(int argc, char *argv[])
20870Sstevel@tonic-gate {
20880Sstevel@tonic-gate 	zone_cmd_arg_t zarg;
20890Sstevel@tonic-gate 	int arg;
20900Sstevel@tonic-gate 
2091766Scarlsonj 	if (zonecfg_in_alt_root()) {
2092766Scarlsonj 		zerror(gettext("cannot halt zone in alternate root"));
2093766Scarlsonj 		return (Z_ERR);
2094766Scarlsonj 	}
2095766Scarlsonj 
20960Sstevel@tonic-gate 	optind = 0;
20970Sstevel@tonic-gate 	if ((arg = getopt(argc, argv, "?")) != EOF) {
20980Sstevel@tonic-gate 		switch (arg) {
20990Sstevel@tonic-gate 		case '?':
21000Sstevel@tonic-gate 			sub_usage(SHELP_HALT, CMD_HALT);
21010Sstevel@tonic-gate 			return (optopt == '?' ? Z_OK : Z_USAGE);
21020Sstevel@tonic-gate 		default:
21030Sstevel@tonic-gate 			sub_usage(SHELP_HALT, CMD_HALT);
21040Sstevel@tonic-gate 			return (Z_USAGE);
21050Sstevel@tonic-gate 		}
21060Sstevel@tonic-gate 	}
21070Sstevel@tonic-gate 	if (argc > optind) {
21080Sstevel@tonic-gate 		sub_usage(SHELP_HALT, CMD_HALT);
21090Sstevel@tonic-gate 		return (Z_USAGE);
21100Sstevel@tonic-gate 	}
21110Sstevel@tonic-gate 	/*
21120Sstevel@tonic-gate 	 * zoneadmd should be the one to decide whether or not to proceed,
21130Sstevel@tonic-gate 	 * so even though it seems that the fourth parameter below should
21140Sstevel@tonic-gate 	 * perhaps be B_TRUE, it really shouldn't be.
21150Sstevel@tonic-gate 	 */
21162712Snn35248 	if (sanity_check(target_zone, CMD_HALT, B_FALSE, B_FALSE, B_FALSE)
21172712Snn35248 	    != Z_OK)
21180Sstevel@tonic-gate 		return (Z_ERR);
21190Sstevel@tonic-gate 
21203339Szt129084 	/*
21213339Szt129084 	 * Invoke brand-specific handler.
21223339Szt129084 	 */
21233339Szt129084 	if (invoke_brand_handler(CMD_HALT, argv) != Z_OK)
21243339Szt129084 		return (Z_ERR);
21253339Szt129084 
21260Sstevel@tonic-gate 	zarg.cmd = Z_HALT;
21270Sstevel@tonic-gate 	return ((call_zoneadmd(target_zone, &zarg) == 0) ? Z_OK : Z_ERR);
21280Sstevel@tonic-gate }
21290Sstevel@tonic-gate 
21300Sstevel@tonic-gate static int
21310Sstevel@tonic-gate reboot_func(int argc, char *argv[])
21320Sstevel@tonic-gate {
21330Sstevel@tonic-gate 	zone_cmd_arg_t zarg;
21340Sstevel@tonic-gate 	int arg;
21350Sstevel@tonic-gate 
2136766Scarlsonj 	if (zonecfg_in_alt_root()) {
2137766Scarlsonj 		zerror(gettext("cannot reboot zone in alternate root"));
2138766Scarlsonj 		return (Z_ERR);
2139766Scarlsonj 	}
2140766Scarlsonj 
21410Sstevel@tonic-gate 	optind = 0;
21420Sstevel@tonic-gate 	if ((arg = getopt(argc, argv, "?")) != EOF) {
21430Sstevel@tonic-gate 		switch (arg) {
21440Sstevel@tonic-gate 		case '?':
21450Sstevel@tonic-gate 			sub_usage(SHELP_REBOOT, CMD_REBOOT);
21460Sstevel@tonic-gate 			return (optopt == '?' ? Z_OK : Z_USAGE);
21470Sstevel@tonic-gate 		default:
21480Sstevel@tonic-gate 			sub_usage(SHELP_REBOOT, CMD_REBOOT);
21490Sstevel@tonic-gate 			return (Z_USAGE);
21500Sstevel@tonic-gate 		}
21510Sstevel@tonic-gate 	}
21522267Sdp 
21532267Sdp 	zarg.bootbuf[0] = '\0';
21542267Sdp 	for (; optind < argc; optind++) {
21552267Sdp 		if (strlcat(zarg.bootbuf, argv[optind],
21562267Sdp 		    sizeof (zarg.bootbuf)) >= sizeof (zarg.bootbuf)) {
21572267Sdp 			zerror(gettext("Boot argument list too long"));
21582267Sdp 			return (Z_ERR);
21592267Sdp 		}
21602267Sdp 		if (optind < argc - 1)
21612267Sdp 			if (strlcat(zarg.bootbuf, " ", sizeof (zarg.bootbuf)) >=
21622267Sdp 			    sizeof (zarg.bootbuf)) {
21632267Sdp 				zerror(gettext("Boot argument list too long"));
21642267Sdp 				return (Z_ERR);
21652267Sdp 			}
21662267Sdp 	}
21672267Sdp 
21682267Sdp 
21690Sstevel@tonic-gate 	/*
21700Sstevel@tonic-gate 	 * zoneadmd should be the one to decide whether or not to proceed,
21710Sstevel@tonic-gate 	 * so even though it seems that the fourth parameter below should
21720Sstevel@tonic-gate 	 * perhaps be B_TRUE, it really shouldn't be.
21730Sstevel@tonic-gate 	 */
21742712Snn35248 	if (sanity_check(target_zone, CMD_REBOOT, B_TRUE, B_FALSE, B_FALSE)
21752712Snn35248 	    != Z_OK)
21760Sstevel@tonic-gate 		return (Z_ERR);
21773339Szt129084 	if (verify_details(CMD_REBOOT, argv) != Z_OK)
2178823Sgjelinek 		return (Z_ERR);
21790Sstevel@tonic-gate 
21800Sstevel@tonic-gate 	zarg.cmd = Z_REBOOT;
21810Sstevel@tonic-gate 	return ((call_zoneadmd(target_zone, &zarg) == 0) ? Z_OK : Z_ERR);
21820Sstevel@tonic-gate }
21830Sstevel@tonic-gate 
21840Sstevel@tonic-gate static int
21853339Szt129084 verify_brand(zone_dochandle_t handle, int cmd_num, char *argv[])
21862712Snn35248 {
21872712Snn35248 	char cmdbuf[MAXPATHLEN];
21882712Snn35248 	int err;
21892712Snn35248 	char zonepath[MAXPATHLEN];
21902727Sedp 	brand_handle_t bh = NULL;
21913339Szt129084 	int status, i;
21922712Snn35248 
21932712Snn35248 	/*
21942712Snn35248 	 * Fetch the verify command from the brand configuration.
21952712Snn35248 	 * "exec" the command so that the returned status is that of
21962712Snn35248 	 * the command and not the shell.
21972712Snn35248 	 */
21982712Snn35248 	if ((err = zonecfg_get_zonepath(handle, zonepath, sizeof (zonepath))) !=
21992712Snn35248 	    Z_OK) {
22002712Snn35248 		errno = err;
22013339Szt129084 		zperror(cmd_to_str(cmd_num), B_TRUE);
22022712Snn35248 		return (Z_ERR);
22032712Snn35248 	}
22042727Sedp 	if ((bh = brand_open(target_brand)) == NULL) {
22052712Snn35248 		zerror(gettext("missing or invalid brand"));
22062712Snn35248 		return (Z_ERR);
22072712Snn35248 	}
22082712Snn35248 
22092712Snn35248 	/*
22102712Snn35248 	 * If the brand has its own verification routine, execute it now.
22113339Szt129084 	 * The verification routine validates the intended zoneadm
22123339Szt129084 	 * operation for the specific brand. The zoneadm subcommand and
22133339Szt129084 	 * all its arguments are passed to the routine.
22142712Snn35248 	 */
22152712Snn35248 	(void) strcpy(cmdbuf, EXEC_PREFIX);
22162727Sedp 	err = brand_get_verify_adm(bh, target_zone, zonepath,
22172712Snn35248 	    cmdbuf + EXEC_LEN, sizeof (cmdbuf) - EXEC_LEN, 0, NULL);
22182727Sedp 	brand_close(bh);
22193339Szt129084 	if (err != 0)
22203339Szt129084 		return (Z_BRAND_ERROR);
22213339Szt129084 	if (strlen(cmdbuf) <= EXEC_LEN)
22223339Szt129084 		return (Z_OK);
22233339Szt129084 
22243339Szt129084 	if (strlcat(cmdbuf, cmd_to_str(cmd_num),
22253339Szt129084 	    sizeof (cmdbuf)) >= sizeof (cmdbuf))
22263339Szt129084 		return (Z_ERR);
22273339Szt129084 
22283339Szt129084 	/* Build the argv string */
22293339Szt129084 	i = 0;
22303339Szt129084 	while (argv[i] != NULL) {
22313339Szt129084 		if ((strlcat(cmdbuf, " ",
22323339Szt129084 		    sizeof (cmdbuf)) >= sizeof (cmdbuf)) ||
22333339Szt129084 		    (strlcat(cmdbuf, argv[i++],
22343339Szt129084 		    sizeof (cmdbuf)) >= sizeof (cmdbuf)))
22353339Szt129084 			return (Z_ERR);
22363339Szt129084 	}
22373339Szt129084 
22383339Szt129084 	status = do_subproc_interactive(cmdbuf);
22393339Szt129084 	err = subproc_status(gettext("brand-specific verification"),
22403339Szt129084 	    status, B_FALSE);
22413339Szt129084 
22423339Szt129084 	return ((err == ZONE_SUBPROC_OK) ? Z_OK : Z_BRAND_ERROR);
22432712Snn35248 }
22442712Snn35248 
22452712Snn35248 static int
22460Sstevel@tonic-gate verify_rctls(zone_dochandle_t handle)
22470Sstevel@tonic-gate {
22480Sstevel@tonic-gate 	struct zone_rctltab rctltab;
22490Sstevel@tonic-gate 	size_t rbs = rctlblk_size();
22500Sstevel@tonic-gate 	rctlblk_t *rctlblk;
22510Sstevel@tonic-gate 	int error = Z_INVAL;
22520Sstevel@tonic-gate 
22530Sstevel@tonic-gate 	if ((rctlblk = malloc(rbs)) == NULL) {
22540Sstevel@tonic-gate 		zerror(gettext("failed to allocate %lu bytes: %s"), rbs,
22550Sstevel@tonic-gate 		    strerror(errno));
22560Sstevel@tonic-gate 		return (Z_NOMEM);
22570Sstevel@tonic-gate 	}
22580Sstevel@tonic-gate 
22590Sstevel@tonic-gate 	if (zonecfg_setrctlent(handle) != Z_OK) {
22600Sstevel@tonic-gate 		zerror(gettext("zonecfg_setrctlent failed"));
22610Sstevel@tonic-gate 		free(rctlblk);
22620Sstevel@tonic-gate 		return (error);
22630Sstevel@tonic-gate 	}
22640Sstevel@tonic-gate 
22650Sstevel@tonic-gate 	rctltab.zone_rctl_valptr = NULL;
22660Sstevel@tonic-gate 	while (zonecfg_getrctlent(handle, &rctltab) == Z_OK) {
22670Sstevel@tonic-gate 		struct zone_rctlvaltab *rctlval;
22680Sstevel@tonic-gate 		const char *name = rctltab.zone_rctl_name;
22690Sstevel@tonic-gate 
22700Sstevel@tonic-gate 		if (!zonecfg_is_rctl(name)) {
22710Sstevel@tonic-gate 			zerror(gettext("WARNING: Ignoring unrecognized rctl "
22720Sstevel@tonic-gate 			    "'%s'."),  name);
22730Sstevel@tonic-gate 			zonecfg_free_rctl_value_list(rctltab.zone_rctl_valptr);
22740Sstevel@tonic-gate 			rctltab.zone_rctl_valptr = NULL;
22750Sstevel@tonic-gate 			continue;
22760Sstevel@tonic-gate 		}
22770Sstevel@tonic-gate 
22780Sstevel@tonic-gate 		for (rctlval = rctltab.zone_rctl_valptr; rctlval != NULL;
22790Sstevel@tonic-gate 		    rctlval = rctlval->zone_rctlval_next) {
22800Sstevel@tonic-gate 			if (zonecfg_construct_rctlblk(rctlval, rctlblk)
22810Sstevel@tonic-gate 			    != Z_OK) {
22820Sstevel@tonic-gate 				zerror(gettext("invalid rctl value: "
22830Sstevel@tonic-gate 				    "(priv=%s,limit=%s,action%s)"),
22840Sstevel@tonic-gate 				    rctlval->zone_rctlval_priv,
22850Sstevel@tonic-gate 				    rctlval->zone_rctlval_limit,
22860Sstevel@tonic-gate 				    rctlval->zone_rctlval_action);
22870Sstevel@tonic-gate 				goto out;
22880Sstevel@tonic-gate 			}
22890Sstevel@tonic-gate 			if (!zonecfg_valid_rctl(name, rctlblk)) {
22900Sstevel@tonic-gate 				zerror(gettext("(priv=%s,limit=%s,action=%s) "
22910Sstevel@tonic-gate 				    "is not a valid value for rctl '%s'"),
22920Sstevel@tonic-gate 				    rctlval->zone_rctlval_priv,
22930Sstevel@tonic-gate 				    rctlval->zone_rctlval_limit,
22940Sstevel@tonic-gate 				    rctlval->zone_rctlval_action,
22950Sstevel@tonic-gate 				    name);
22960Sstevel@tonic-gate 				goto out;
22970Sstevel@tonic-gate 			}
22980Sstevel@tonic-gate 		}
22990Sstevel@tonic-gate 		zonecfg_free_rctl_value_list(rctltab.zone_rctl_valptr);
23000Sstevel@tonic-gate 	}
23010Sstevel@tonic-gate 	rctltab.zone_rctl_valptr = NULL;
23020Sstevel@tonic-gate 	error = Z_OK;
23030Sstevel@tonic-gate out:
23040Sstevel@tonic-gate 	zonecfg_free_rctl_value_list(rctltab.zone_rctl_valptr);
23050Sstevel@tonic-gate 	(void) zonecfg_endrctlent(handle);
23060Sstevel@tonic-gate 	free(rctlblk);
23070Sstevel@tonic-gate 	return (error);
23080Sstevel@tonic-gate }
23090Sstevel@tonic-gate 
23100Sstevel@tonic-gate static int
23110Sstevel@tonic-gate verify_pool(zone_dochandle_t handle)
23120Sstevel@tonic-gate {
23130Sstevel@tonic-gate 	char poolname[MAXPATHLEN];
23140Sstevel@tonic-gate 	pool_conf_t *poolconf;
23150Sstevel@tonic-gate 	pool_t *pool;
23160Sstevel@tonic-gate 	int status;
23170Sstevel@tonic-gate 	int error;
23180Sstevel@tonic-gate 
23190Sstevel@tonic-gate 	/*
23200Sstevel@tonic-gate 	 * This ends up being very similar to the check done in zoneadmd.
23210Sstevel@tonic-gate 	 */
23220Sstevel@tonic-gate 	error = zonecfg_get_pool(handle, poolname, sizeof (poolname));
23230Sstevel@tonic-gate 	if (error == Z_NO_ENTRY || (error == Z_OK && strlen(poolname) == 0)) {
23240Sstevel@tonic-gate 		/*
23250Sstevel@tonic-gate 		 * No pool specified.
23260Sstevel@tonic-gate 		 */
23270Sstevel@tonic-gate 		return (0);
23280Sstevel@tonic-gate 	}
23290Sstevel@tonic-gate 	if (error != Z_OK) {
23300Sstevel@tonic-gate 		zperror(gettext("Unable to retrieve pool name from "
23310Sstevel@tonic-gate 		    "configuration"), B_TRUE);
23320Sstevel@tonic-gate 		return (error);
23330Sstevel@tonic-gate 	}
23340Sstevel@tonic-gate 	/*
23350Sstevel@tonic-gate 	 * Don't do anything if pools aren't enabled.
23360Sstevel@tonic-gate 	 */
23370Sstevel@tonic-gate 	if (pool_get_status(&status) != PO_SUCCESS || status != POOL_ENABLED) {
23380Sstevel@tonic-gate 		zerror(gettext("WARNING: pools facility not active; "
23390Sstevel@tonic-gate 		    "zone will not be bound to pool '%s'."), poolname);
23400Sstevel@tonic-gate 		return (Z_OK);
23410Sstevel@tonic-gate 	}
23420Sstevel@tonic-gate 	/*
23430Sstevel@tonic-gate 	 * Try to provide a sane error message if the requested pool doesn't
23440Sstevel@tonic-gate 	 * exist.  It isn't clear that pools-related failures should
23450Sstevel@tonic-gate 	 * necessarily translate to a failure to verify the zone configuration,
23460Sstevel@tonic-gate 	 * hence they are not considered errors.
23470Sstevel@tonic-gate 	 */
23480Sstevel@tonic-gate 	if ((poolconf = pool_conf_alloc()) == NULL) {
23490Sstevel@tonic-gate 		zerror(gettext("WARNING: pool_conf_alloc failed; "
23500Sstevel@tonic-gate 		    "using default pool"));
23510Sstevel@tonic-gate 		return (Z_OK);
23520Sstevel@tonic-gate 	}
23530Sstevel@tonic-gate 	if (pool_conf_open(poolconf, pool_dynamic_location(), PO_RDONLY) !=
23540Sstevel@tonic-gate 	    PO_SUCCESS) {
23550Sstevel@tonic-gate 		zerror(gettext("WARNING: pool_conf_open failed; "
23560Sstevel@tonic-gate 		    "using default pool"));
23570Sstevel@tonic-gate 		pool_conf_free(poolconf);
23580Sstevel@tonic-gate 		return (Z_OK);
23590Sstevel@tonic-gate 	}
23600Sstevel@tonic-gate 	pool = pool_get_pool(poolconf, poolname);
23610Sstevel@tonic-gate 	(void) pool_conf_close(poolconf);
23620Sstevel@tonic-gate 	pool_conf_free(poolconf);
23630Sstevel@tonic-gate 	if (pool == NULL) {
23640Sstevel@tonic-gate 		zerror(gettext("WARNING: pool '%s' not found. "
23650Sstevel@tonic-gate 		    "using default pool"), poolname);
23660Sstevel@tonic-gate 	}
23670Sstevel@tonic-gate 
23680Sstevel@tonic-gate 	return (Z_OK);
23690Sstevel@tonic-gate }
23700Sstevel@tonic-gate 
23710Sstevel@tonic-gate static int
2372823Sgjelinek verify_ipd(zone_dochandle_t handle)
2373823Sgjelinek {
2374823Sgjelinek 	int return_code = Z_OK;
2375823Sgjelinek 	struct zone_fstab fstab;
2376823Sgjelinek 	struct stat st;
2377823Sgjelinek 	char specdir[MAXPATHLEN];
2378823Sgjelinek 
2379823Sgjelinek 	if (zonecfg_setipdent(handle) != Z_OK) {
2380924Sgjelinek 		/*
2381924Sgjelinek 		 * TRANSLATION_NOTE
2382924Sgjelinek 		 * inherit-pkg-dirs is a literal that should not be translated.
2383924Sgjelinek 		 */
2384924Sgjelinek 		(void) fprintf(stderr, gettext("could not verify "
2385823Sgjelinek 		    "inherit-pkg-dirs: unable to enumerate mounts\n"));
2386823Sgjelinek 		return (Z_ERR);
2387823Sgjelinek 	}
2388823Sgjelinek 	while (zonecfg_getipdent(handle, &fstab) == Z_OK) {
2389823Sgjelinek 		/*
2390823Sgjelinek 		 * Verify fs_dir exists.
2391823Sgjelinek 		 */
2392823Sgjelinek 		(void) snprintf(specdir, sizeof (specdir), "%s%s",
2393823Sgjelinek 		    zonecfg_get_root(), fstab.zone_fs_dir);
2394823Sgjelinek 		if (stat(specdir, &st) != 0) {
2395924Sgjelinek 			/*
2396924Sgjelinek 			 * TRANSLATION_NOTE
2397924Sgjelinek 			 * inherit-pkg-dir is a literal that should not be
2398924Sgjelinek 			 * translated.
2399924Sgjelinek 			 */
2400924Sgjelinek 			(void) fprintf(stderr, gettext("could not verify "
2401823Sgjelinek 			    "inherit-pkg-dir %s: %s\n"),
2402823Sgjelinek 			    fstab.zone_fs_dir, strerror(errno));
2403823Sgjelinek 			return_code = Z_ERR;
2404823Sgjelinek 		}
2405823Sgjelinek 		if (strcmp(st.st_fstype, MNTTYPE_NFS) == 0) {
2406924Sgjelinek 			/*
2407924Sgjelinek 			 * TRANSLATION_NOTE
2408924Sgjelinek 			 * inherit-pkg-dir and NFS are literals that should
2409924Sgjelinek 			 * not be translated.
2410924Sgjelinek 			 */
2411823Sgjelinek 			(void) fprintf(stderr, gettext("cannot verify "
24121867Sgjelinek 			    "inherit-pkg-dir %s: NFS mounted file system.\n"
24131867Sgjelinek 			    "\tA local file system must be used.\n"),
2414823Sgjelinek 			    fstab.zone_fs_dir);
2415823Sgjelinek 			return_code = Z_ERR;
2416823Sgjelinek 		}
2417823Sgjelinek 	}
2418823Sgjelinek 	(void) zonecfg_endipdent(handle);
2419823Sgjelinek 
2420823Sgjelinek 	return (return_code);
2421823Sgjelinek }
2422823Sgjelinek 
24231393Slling /*
24241867Sgjelinek  * Verify that the special device/file system exists and is valid.
24251393Slling  */
24261393Slling static int
24271393Slling verify_fs_special(struct zone_fstab *fstab)
24281393Slling {
24291393Slling 	struct stat st;
24301393Slling 
24312971Sgjelinek 	/*
24322971Sgjelinek 	 * This validation is really intended for standard zone administration.
24332971Sgjelinek 	 * If we are in a mini-root or some other upgrade situation where
24342971Sgjelinek 	 * we are using the scratch zone, just by-pass this.
24352971Sgjelinek 	 */
24362971Sgjelinek 	if (zonecfg_in_alt_root())
24372971Sgjelinek 		return (Z_OK);
24382971Sgjelinek 
24391393Slling 	if (strcmp(fstab->zone_fs_type, MNTTYPE_ZFS) == 0)
24401393Slling 		return (verify_fs_zfs(fstab));
24411393Slling 
24421393Slling 	if (stat(fstab->zone_fs_special, &st) != 0) {
24431397Slling 		(void) fprintf(stderr, gettext("could not verify fs "
24441393Slling 		    "%s: could not access %s: %s\n"), fstab->zone_fs_dir,
24451393Slling 		    fstab->zone_fs_special, strerror(errno));
24461393Slling 		return (Z_ERR);
24471393Slling 	}
24481393Slling 
24491393Slling 	if (strcmp(st.st_fstype, MNTTYPE_NFS) == 0) {
24501393Slling 		/*
24511393Slling 		 * TRANSLATION_NOTE
24521393Slling 		 * fs and NFS are literals that should
24531393Slling 		 * not be translated.
24541393Slling 		 */
24551393Slling 		(void) fprintf(stderr, gettext("cannot verify "
24561867Sgjelinek 		    "fs %s: NFS mounted file system.\n"
24571867Sgjelinek 		    "\tA local file system must be used.\n"),
24581393Slling 		    fstab->zone_fs_special);
24591393Slling 		return (Z_ERR);
24601393Slling 	}
24611393Slling 
24621393Slling 	return (Z_OK);
24631393Slling }
24641393Slling 
2465823Sgjelinek static int
24660Sstevel@tonic-gate verify_filesystems(zone_dochandle_t handle)
24670Sstevel@tonic-gate {
24680Sstevel@tonic-gate 	int return_code = Z_OK;
24690Sstevel@tonic-gate 	struct zone_fstab fstab;
24700Sstevel@tonic-gate 	char cmdbuf[MAXPATHLEN];
24710Sstevel@tonic-gate 	struct stat st;
24720Sstevel@tonic-gate 
24730Sstevel@tonic-gate 	/*
24740Sstevel@tonic-gate 	 * No need to verify inherit-pkg-dir fs types, as their type is
24750Sstevel@tonic-gate 	 * implicitly lofs, which is known.  Therefore, the types are only
24761867Sgjelinek 	 * verified for regular file systems below.
24770Sstevel@tonic-gate 	 *
24780Sstevel@tonic-gate 	 * Since the actual mount point is not known until the dependent mounts
24790Sstevel@tonic-gate 	 * are performed, we don't attempt any path validation here: that will
24800Sstevel@tonic-gate 	 * happen later when zoneadmd actually does the mounts.
24810Sstevel@tonic-gate 	 */
24820Sstevel@tonic-gate 	if (zonecfg_setfsent(handle) != Z_OK) {
24831867Sgjelinek 		(void) fprintf(stderr, gettext("could not verify file systems: "
24840Sstevel@tonic-gate 		    "unable to enumerate mounts\n"));
24850Sstevel@tonic-gate 		return (Z_ERR);
24860Sstevel@tonic-gate 	}
24870Sstevel@tonic-gate 	while (zonecfg_getfsent(handle, &fstab) == Z_OK) {
24880Sstevel@tonic-gate 		if (!zonecfg_valid_fs_type(fstab.zone_fs_type)) {
24890Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("cannot verify fs %s: "
24900Sstevel@tonic-gate 			    "type %s is not allowed.\n"), fstab.zone_fs_dir,
24910Sstevel@tonic-gate 			    fstab.zone_fs_type);
24920Sstevel@tonic-gate 			return_code = Z_ERR;
24930Sstevel@tonic-gate 			goto next_fs;
24940Sstevel@tonic-gate 		}
24950Sstevel@tonic-gate 		/*
24960Sstevel@tonic-gate 		 * Verify /usr/lib/fs/<fstype>/mount exists.
24970Sstevel@tonic-gate 		 */
24980Sstevel@tonic-gate 		if (snprintf(cmdbuf, sizeof (cmdbuf), "/usr/lib/fs/%s/mount",
24990Sstevel@tonic-gate 		    fstab.zone_fs_type) > sizeof (cmdbuf)) {
25000Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("cannot verify fs %s: "
25010Sstevel@tonic-gate 			    "type %s is too long.\n"), fstab.zone_fs_dir,
25020Sstevel@tonic-gate 			    fstab.zone_fs_type);
25030Sstevel@tonic-gate 			return_code = Z_ERR;
25040Sstevel@tonic-gate 			goto next_fs;
25050Sstevel@tonic-gate 		}
25060Sstevel@tonic-gate 		if (stat(cmdbuf, &st) != 0) {
2507924Sgjelinek 			(void) fprintf(stderr, gettext("could not verify fs "
2508924Sgjelinek 			    "%s: could not access %s: %s\n"), fstab.zone_fs_dir,
25090Sstevel@tonic-gate 			    cmdbuf, strerror(errno));
25100Sstevel@tonic-gate 			return_code = Z_ERR;
25110Sstevel@tonic-gate 			goto next_fs;
25120Sstevel@tonic-gate 		}
25130Sstevel@tonic-gate 		if (!S_ISREG(st.st_mode)) {
2514924Sgjelinek 			(void) fprintf(stderr, gettext("could not verify fs "
2515924Sgjelinek 			    "%s: %s is not a regular file\n"),
2516924Sgjelinek 			    fstab.zone_fs_dir, cmdbuf);
25170Sstevel@tonic-gate 			return_code = Z_ERR;
25180Sstevel@tonic-gate 			goto next_fs;
25190Sstevel@tonic-gate 		}
25200Sstevel@tonic-gate 		/*
25210Sstevel@tonic-gate 		 * Verify /usr/lib/fs/<fstype>/fsck exists iff zone_fs_raw is
25220Sstevel@tonic-gate 		 * set.
25230Sstevel@tonic-gate 		 */
25240Sstevel@tonic-gate 		if (snprintf(cmdbuf, sizeof (cmdbuf), "/usr/lib/fs/%s/fsck",
25250Sstevel@tonic-gate 		    fstab.zone_fs_type) > sizeof (cmdbuf)) {
25260Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("cannot verify fs %s: "
25270Sstevel@tonic-gate 			    "type %s is too long.\n"), fstab.zone_fs_dir,
25280Sstevel@tonic-gate 			    fstab.zone_fs_type);
25290Sstevel@tonic-gate 			return_code = Z_ERR;
25300Sstevel@tonic-gate 			goto next_fs;
25310Sstevel@tonic-gate 		}
25320Sstevel@tonic-gate 		if (fstab.zone_fs_raw[0] == '\0' && stat(cmdbuf, &st) == 0) {
2533924Sgjelinek 			(void) fprintf(stderr, gettext("could not verify fs "
2534924Sgjelinek 			    "%s: must specify 'raw' device for %s "
25351867Sgjelinek 			    "file systems\n"),
25360Sstevel@tonic-gate 			    fstab.zone_fs_dir, fstab.zone_fs_type);
25370Sstevel@tonic-gate 			return_code = Z_ERR;
25380Sstevel@tonic-gate 			goto next_fs;
25390Sstevel@tonic-gate 		}
25400Sstevel@tonic-gate 		if (fstab.zone_fs_raw[0] != '\0' &&
25410Sstevel@tonic-gate 		    (stat(cmdbuf, &st) != 0 || !S_ISREG(st.st_mode))) {
25420Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("cannot verify fs %s: "
25430Sstevel@tonic-gate 			    "'raw' device specified but "
25440Sstevel@tonic-gate 			    "no fsck executable exists for %s\n"),
25450Sstevel@tonic-gate 			    fstab.zone_fs_dir, fstab.zone_fs_type);
25460Sstevel@tonic-gate 			return_code = Z_ERR;
25470Sstevel@tonic-gate 			goto next_fs;
25480Sstevel@tonic-gate 		}
25491393Slling 
25501393Slling 		/* Verify fs_special. */
25511393Slling 		if ((return_code = verify_fs_special(&fstab)) != Z_OK)
2552823Sgjelinek 			goto next_fs;
25531393Slling 
25541393Slling 		/* Verify fs_raw. */
2555823Sgjelinek 		if (fstab.zone_fs_raw[0] != '\0' &&
2556823Sgjelinek 		    stat(fstab.zone_fs_raw, &st) != 0) {
2557924Sgjelinek 			/*
2558924Sgjelinek 			 * TRANSLATION_NOTE
2559924Sgjelinek 			 * fs is a literal that should not be translated.
2560924Sgjelinek 			 */
2561924Sgjelinek 			(void) fprintf(stderr, gettext("could not verify fs "
2562924Sgjelinek 			    "%s: could not access %s: %s\n"), fstab.zone_fs_dir,
2563823Sgjelinek 			    fstab.zone_fs_raw, strerror(errno));
2564823Sgjelinek 			return_code = Z_ERR;
2565823Sgjelinek 			goto next_fs;
2566823Sgjelinek 		}
25670Sstevel@tonic-gate next_fs:
25680Sstevel@tonic-gate 		zonecfg_free_fs_option_list(fstab.zone_fs_options);
25690Sstevel@tonic-gate 	}
25700Sstevel@tonic-gate 	(void) zonecfg_endfsent(handle);
25710Sstevel@tonic-gate 
25720Sstevel@tonic-gate 	return (return_code);
25730Sstevel@tonic-gate }
25740Sstevel@tonic-gate 
25750Sstevel@tonic-gate static int
25761645Scomay verify_limitpriv(zone_dochandle_t handle)
25771645Scomay {
25781645Scomay 	char *privname = NULL;
25791645Scomay 	int err;
25801645Scomay 	priv_set_t *privs;
25811645Scomay 
25821645Scomay 	if ((privs = priv_allocset()) == NULL) {
25831645Scomay 		zperror(gettext("failed to allocate privilege set"), B_FALSE);
25841645Scomay 		return (Z_NOMEM);
25851645Scomay 	}
25861645Scomay 	err = zonecfg_get_privset(handle, privs, &privname);
25871645Scomay 	switch (err) {
25881645Scomay 	case Z_OK:
25891645Scomay 		break;
25901645Scomay 	case Z_PRIV_PROHIBITED:
25911645Scomay 		(void) fprintf(stderr, gettext("privilege \"%s\" is not "
25921645Scomay 		    "permitted within the zone's privilege set\n"), privname);
25931645Scomay 		break;
25941645Scomay 	case Z_PRIV_REQUIRED:
25951645Scomay 		(void) fprintf(stderr, gettext("required privilege \"%s\" is "
25961645Scomay 		    "missing from the zone's privilege set\n"), privname);
25971645Scomay 		break;
25981645Scomay 	case Z_PRIV_UNKNOWN:
25991645Scomay 		(void) fprintf(stderr, gettext("unknown privilege \"%s\" "
26001645Scomay 		    "specified in the zone's privilege set\n"), privname);
26011645Scomay 		break;
26021645Scomay 	default:
26031645Scomay 		zperror(
26041645Scomay 		    gettext("failed to determine the zone's privilege set"),
26051645Scomay 		    B_TRUE);
26061645Scomay 		break;
26071645Scomay 	}
26081645Scomay 	free(privname);
26091645Scomay 	priv_freeset(privs);
26101645Scomay 	return (err);
26111645Scomay }
26121645Scomay 
26131915Sgjelinek static void
26141915Sgjelinek free_local_netifs(int if_cnt, struct net_if **if_list)
26151915Sgjelinek {
26161915Sgjelinek 	int		i;
26171915Sgjelinek 
26181915Sgjelinek 	for (i = 0; i < if_cnt; i++) {
26191915Sgjelinek 		free(if_list[i]->name);
26201915Sgjelinek 		free(if_list[i]);
26211915Sgjelinek 	}
26221915Sgjelinek 	free(if_list);
26231915Sgjelinek }
26241915Sgjelinek 
26251915Sgjelinek /*
26261915Sgjelinek  * Get a list of the network interfaces, along with their address families,
26271915Sgjelinek  * that are plumbed in the global zone.  See if_tcp(7p) for a description
26281915Sgjelinek  * of the ioctls used here.
26291915Sgjelinek  */
26301915Sgjelinek static int
26311915Sgjelinek get_local_netifs(int *if_cnt, struct net_if ***if_list)
26321915Sgjelinek {
26331915Sgjelinek 	int		s;
26341915Sgjelinek 	int		i;
26351915Sgjelinek 	int		res = Z_OK;
26361915Sgjelinek 	int		space_needed;
26371915Sgjelinek 	int		cnt = 0;
26381915Sgjelinek 	struct		lifnum if_num;
26391915Sgjelinek 	struct		lifconf if_conf;
26401915Sgjelinek 	struct		lifreq *if_reqp;
26411915Sgjelinek 	char		*if_buf;
26421915Sgjelinek 	struct net_if	**local_ifs = NULL;
26431915Sgjelinek 
26441915Sgjelinek 	*if_cnt = 0;
26451915Sgjelinek 	*if_list = NULL;
26461915Sgjelinek 
26471915Sgjelinek 	if ((s = socket(SOCKET_AF(AF_INET), SOCK_DGRAM, 0)) < 0)
26481915Sgjelinek 		return (Z_ERR);
26491915Sgjelinek 
26501915Sgjelinek 	/*
26511915Sgjelinek 	 * Come back here in the unlikely event that the number of interfaces
26521915Sgjelinek 	 * increases between the time we get the count and the time we do the
26531915Sgjelinek 	 * SIOCGLIFCONF ioctl.
26541915Sgjelinek 	 */
26551915Sgjelinek retry:
26561915Sgjelinek 	/* Get the number of interfaces. */
26571915Sgjelinek 	if_num.lifn_family = AF_UNSPEC;
26581915Sgjelinek 	if_num.lifn_flags = LIFC_NOXMIT;
26591915Sgjelinek 	if (ioctl(s, SIOCGLIFNUM, &if_num) < 0) {
26601915Sgjelinek 		(void) close(s);
26611915Sgjelinek 		return (Z_ERR);
26621915Sgjelinek 	}
26631915Sgjelinek 
26641915Sgjelinek 	/* Get the interface configuration list. */
26651915Sgjelinek 	space_needed = if_num.lifn_count * sizeof (struct lifreq);
26661915Sgjelinek 	if ((if_buf = malloc(space_needed)) == NULL) {
26671915Sgjelinek 		(void) close(s);
26681915Sgjelinek 		return (Z_ERR);
26691915Sgjelinek 	}
26701915Sgjelinek 	if_conf.lifc_family = AF_UNSPEC;
26711915Sgjelinek 	if_conf.lifc_flags = LIFC_NOXMIT;
26721915Sgjelinek 	if_conf.lifc_len = space_needed;
26731915Sgjelinek 	if_conf.lifc_buf = if_buf;
26741915Sgjelinek 	if (ioctl(s, SIOCGLIFCONF, &if_conf) < 0) {
26751915Sgjelinek 		free(if_buf);
26761915Sgjelinek 		/*
26771915Sgjelinek 		 * SIOCGLIFCONF returns EINVAL if the buffer we passed in is
26781915Sgjelinek 		 * too small.  In this case go back and get the new if cnt.
26791915Sgjelinek 		 */
26801915Sgjelinek 		if (errno == EINVAL)
26811915Sgjelinek 			goto retry;
26821915Sgjelinek 
26831915Sgjelinek 		(void) close(s);
26841915Sgjelinek 		return (Z_ERR);
26851915Sgjelinek 	}
26861915Sgjelinek 	(void) close(s);
26871915Sgjelinek 
26881915Sgjelinek 	/* Get the name and address family for each interface. */
26891915Sgjelinek 	if_reqp = if_conf.lifc_req;
26901915Sgjelinek 	for (i = 0; i < (if_conf.lifc_len / sizeof (struct lifreq)); i++) {
26911915Sgjelinek 		struct net_if	**p;
26921915Sgjelinek 		struct lifreq	req;
26931915Sgjelinek 
26941915Sgjelinek 		if (strcmp(LOOPBACK_IF, if_reqp->lifr_name) == 0) {
26951915Sgjelinek 			if_reqp++;
26961915Sgjelinek 			continue;
26971915Sgjelinek 		}
26981915Sgjelinek 
26991915Sgjelinek 		if ((s = socket(SOCKET_AF(if_reqp->lifr_addr.ss_family),
27001915Sgjelinek 		    SOCK_DGRAM, 0)) == -1) {
27011915Sgjelinek 			res = Z_ERR;
27021915Sgjelinek 			break;
27031915Sgjelinek 		}
27041915Sgjelinek 
27051915Sgjelinek 		(void) strncpy(req.lifr_name, if_reqp->lifr_name,
27061915Sgjelinek 		    sizeof (req.lifr_name));
27071915Sgjelinek 		if (ioctl(s, SIOCGLIFADDR, &req) < 0) {
27081915Sgjelinek 			(void) close(s);
27091915Sgjelinek 			if_reqp++;
27101915Sgjelinek 			continue;
27111915Sgjelinek 		}
27121915Sgjelinek 
27131915Sgjelinek 		if ((p = (struct net_if **)realloc(local_ifs,
27141915Sgjelinek 		    sizeof (struct net_if *) * (cnt + 1))) == NULL) {
27151915Sgjelinek 			res = Z_ERR;
27161915Sgjelinek 			break;
27171915Sgjelinek 		}
27181915Sgjelinek 		local_ifs = p;
27191915Sgjelinek 
27201915Sgjelinek 		if ((local_ifs[cnt] = malloc(sizeof (struct net_if))) == NULL) {
27211915Sgjelinek 			res = Z_ERR;
27221915Sgjelinek 			break;
27231915Sgjelinek 		}
27241915Sgjelinek 
27251915Sgjelinek 		if ((local_ifs[cnt]->name = strdup(if_reqp->lifr_name))
27261915Sgjelinek 		    == NULL) {
27271915Sgjelinek 			free(local_ifs[cnt]);
27281915Sgjelinek 			res = Z_ERR;
27291915Sgjelinek 			break;
27301915Sgjelinek 		}
27311915Sgjelinek 		local_ifs[cnt]->af = req.lifr_addr.ss_family;
27321915Sgjelinek 		cnt++;
27331915Sgjelinek 
27341915Sgjelinek 		(void) close(s);
27351915Sgjelinek 		if_reqp++;
27361915Sgjelinek 	}
27371915Sgjelinek 
27381915Sgjelinek 	free(if_buf);
27391915Sgjelinek 
27401915Sgjelinek 	if (res != Z_OK) {
27411915Sgjelinek 		free_local_netifs(cnt, local_ifs);
27421915Sgjelinek 	} else {
27431915Sgjelinek 		*if_cnt = cnt;
27441915Sgjelinek 		*if_list = local_ifs;
27451915Sgjelinek 	}
27461915Sgjelinek 
27471915Sgjelinek 	return (res);
27481915Sgjelinek }
27491915Sgjelinek 
27501915Sgjelinek static char *
27511915Sgjelinek af2str(int af)
27521915Sgjelinek {
27531915Sgjelinek 	switch (af) {
27541915Sgjelinek 	case AF_INET:
27551915Sgjelinek 		return ("IPv4");
27561915Sgjelinek 	case AF_INET6:
27571915Sgjelinek 		return ("IPv6");
27581915Sgjelinek 	default:
27591915Sgjelinek 		return ("Unknown");
27601915Sgjelinek 	}
27611915Sgjelinek }
27621915Sgjelinek 
27631915Sgjelinek /*
27641915Sgjelinek  * Cross check the network interface name and address family with the
27651915Sgjelinek  * interfaces that are set up in the global zone so that we can print the
27661915Sgjelinek  * appropriate error message.
27671915Sgjelinek  */
27681915Sgjelinek static void
27691915Sgjelinek print_net_err(char *phys, char *addr, int af, char *msg)
27701915Sgjelinek {
27711915Sgjelinek 	int		i;
27721915Sgjelinek 	int		local_if_cnt = 0;
27731915Sgjelinek 	struct net_if	**local_ifs = NULL;
27741915Sgjelinek 	boolean_t	found_if = B_FALSE;
27751915Sgjelinek 	boolean_t	found_af = B_FALSE;
27761915Sgjelinek 
27771915Sgjelinek 	if (get_local_netifs(&local_if_cnt, &local_ifs) != Z_OK) {
27781915Sgjelinek 		(void) fprintf(stderr,
27791915Sgjelinek 		    gettext("could not verify %s %s=%s %s=%s\n\t%s\n"),
27801915Sgjelinek 		    "net", "address", addr, "physical", phys, msg);
27811915Sgjelinek 		return;
27821915Sgjelinek 	}
27831915Sgjelinek 
27841915Sgjelinek 	for (i = 0; i < local_if_cnt; i++) {
27851915Sgjelinek 		if (strcmp(phys, local_ifs[i]->name) == 0) {
27861915Sgjelinek 			found_if = B_TRUE;
27871915Sgjelinek 			if (af == local_ifs[i]->af) {
27881915Sgjelinek 				found_af = B_TRUE;
27891915Sgjelinek 				break;
27901915Sgjelinek 			}
27911915Sgjelinek 		}
27921915Sgjelinek 	}
27931915Sgjelinek 
27941915Sgjelinek 	free_local_netifs(local_if_cnt, local_ifs);
27951915Sgjelinek 
27961915Sgjelinek 	if (!found_if) {
27971915Sgjelinek 		(void) fprintf(stderr,
27981915Sgjelinek 		    gettext("could not verify %s %s=%s\n\t"
27991915Sgjelinek 		    "network interface %s is not plumbed in the global zone\n"),
28001915Sgjelinek 		    "net", "physical", phys, phys);
28011915Sgjelinek 		return;
28021915Sgjelinek 	}
28031915Sgjelinek 
28041915Sgjelinek 	/*
28051915Sgjelinek 	 * Print this error if we were unable to find the address family
28061915Sgjelinek 	 * for this interface.  If the af variable is not initialized to
28071915Sgjelinek 	 * to something meaningful by the caller (not AF_UNSPEC) then we
28081915Sgjelinek 	 * also skip this message since it wouldn't be informative.
28091915Sgjelinek 	 */
28101915Sgjelinek 	if (!found_af && af != AF_UNSPEC) {
28111915Sgjelinek 		(void) fprintf(stderr,
28121915Sgjelinek 		    gettext("could not verify %s %s=%s %s=%s\n\tthe %s address "
28133448Sdh155122 		    "family is not configured on this network interface in "
28143448Sdh155122 		    "the\n\tglobal zone\n"),
28151915Sgjelinek 		    "net", "address", addr, "physical", phys, af2str(af));
28161915Sgjelinek 		return;
28171915Sgjelinek 	}
28181915Sgjelinek 
28191915Sgjelinek 	(void) fprintf(stderr,
28201915Sgjelinek 	    gettext("could not verify %s %s=%s %s=%s\n\t%s\n"),
28211915Sgjelinek 	    "net", "address", addr, "physical", phys, msg);
28221915Sgjelinek }
28231915Sgjelinek 
28241645Scomay static int
28253339Szt129084 verify_handle(int cmd_num, zone_dochandle_t handle, char *argv[])
28260Sstevel@tonic-gate {
28270Sstevel@tonic-gate 	struct zone_nwiftab nwiftab;
28280Sstevel@tonic-gate 	int return_code = Z_OK;
28290Sstevel@tonic-gate 	int err;
2830766Scarlsonj 	boolean_t in_alt_root;
28313448Sdh155122 	zone_iptype_t iptype;
28323448Sdh155122 	int fd;
28330Sstevel@tonic-gate 
2834766Scarlsonj 	in_alt_root = zonecfg_in_alt_root();
2835766Scarlsonj 	if (in_alt_root)
2836766Scarlsonj 		goto no_net;
2837766Scarlsonj 
28383448Sdh155122 	if ((err = zonecfg_get_iptype(handle, &iptype)) != Z_OK) {
28393448Sdh155122 		errno = err;
28403448Sdh155122 		zperror(cmd_to_str(cmd_num), B_TRUE);
28413448Sdh155122 		zonecfg_fini_handle(handle);
28423448Sdh155122 		return (Z_ERR);
28433448Sdh155122 	}
28440Sstevel@tonic-gate 	if ((err = zonecfg_setnwifent(handle)) != Z_OK) {
28450Sstevel@tonic-gate 		errno = err;
28460Sstevel@tonic-gate 		zperror(cmd_to_str(cmd_num), B_TRUE);
28470Sstevel@tonic-gate 		zonecfg_fini_handle(handle);
28480Sstevel@tonic-gate 		return (Z_ERR);
28490Sstevel@tonic-gate 	}
28500Sstevel@tonic-gate 	while (zonecfg_getnwifent(handle, &nwiftab) == Z_OK) {
28510Sstevel@tonic-gate 		struct lifreq lifr;
28521915Sgjelinek 		sa_family_t af = AF_UNSPEC;
28533448Sdh155122 		char dl_owner_zname[ZONENAME_MAX];
28543448Sdh155122 		zoneid_t dl_owner_zid;
28553448Sdh155122 		zoneid_t target_zid;
28563448Sdh155122 		int res;
28570Sstevel@tonic-gate 
28580Sstevel@tonic-gate 		/* skip any loopback interfaces */
28590Sstevel@tonic-gate 		if (strcmp(nwiftab.zone_nwif_physical, "lo0") == 0)
28600Sstevel@tonic-gate 			continue;
28613448Sdh155122 		switch (iptype) {
28623448Sdh155122 		case ZS_SHARED:
28633448Sdh155122 			if ((res = zonecfg_valid_net_address(
28643448Sdh155122 			    nwiftab.zone_nwif_address, &lifr)) != Z_OK) {
28653448Sdh155122 				print_net_err(nwiftab.zone_nwif_physical,
28663448Sdh155122 				    nwiftab.zone_nwif_address, af,
28673448Sdh155122 				    zonecfg_strerror(res));
28683448Sdh155122 			    return_code = Z_ERR;
28693448Sdh155122 			    continue;
28703448Sdh155122 			}
28713448Sdh155122 			af = lifr.lifr_addr.ss_family;
28723448Sdh155122 			if (!zonecfg_ifname_exists(af,
28733448Sdh155122 			    nwiftab.zone_nwif_physical)) {
28743448Sdh155122 				/*
28753448Sdh155122 				 * The interface failed to come up. We continue
28763448Sdh155122 				 * on anyway for the sake of consistency: a
28773448Sdh155122 				 * zone is not shut down if the interface fails
28783448Sdh155122 				 * any time after boot, nor does the global zone
28793448Sdh155122 				 * fail to boot if an interface fails.
28803448Sdh155122 				 */
28813448Sdh155122 				(void) fprintf(stderr,
28823448Sdh155122 				    gettext("WARNING: skipping network "
28833448Sdh155122 					"interface '%s' which may not be "
28843448Sdh155122 					"present/plumbed in the global "
28853448Sdh155122 					"zone.\n"),
28863448Sdh155122 				    nwiftab.zone_nwif_physical);
28873448Sdh155122 			}
28883448Sdh155122 			break;
28893448Sdh155122 		case ZS_EXCLUSIVE:
28903448Sdh155122 			/* Warning if it exists for either IPv4 or IPv6 */
28913448Sdh155122 
28923448Sdh155122 			if (zonecfg_ifname_exists(AF_INET,
28933448Sdh155122 			    nwiftab.zone_nwif_physical) ||
28943448Sdh155122 			    zonecfg_ifname_exists(AF_INET6,
28953448Sdh155122 			    nwiftab.zone_nwif_physical)) {
28963448Sdh155122 				(void) fprintf(stderr,
28973448Sdh155122 				    gettext("WARNING: skipping network "
28983448Sdh155122 				    "interface '%s' which is used in the "
28993448Sdh155122 				    "global zone.\n"),
29003448Sdh155122 				    nwiftab.zone_nwif_physical);
29013448Sdh155122 				break;
29023448Sdh155122 			}
29032611Svp157776 			/*
29043448Sdh155122 			 * Verify that the physical interface can
29053448Sdh155122 			 * be opened
29063448Sdh155122 			 */
29073448Sdh155122 			fd = ifname_open(nwiftab.zone_nwif_physical);
29083448Sdh155122 			if (fd == -1) {
29093448Sdh155122 				(void) fprintf(stderr,
29103448Sdh155122 				    gettext("WARNING: skipping network "
29113448Sdh155122 				    "interface '%s' which cannot be opened.\n"),
29123448Sdh155122 				    nwiftab.zone_nwif_physical);
29133448Sdh155122 				break;
29143448Sdh155122 			} else {
29153448Sdh155122 				(void) close(fd);
29163448Sdh155122 			}
29173448Sdh155122 			/*
29183448Sdh155122 			 * Verify whether the physical interface is already
29193448Sdh155122 			 * used by a zone.
29203448Sdh155122 			 */
29213448Sdh155122 			dl_owner_zid = ALL_ZONES;
29223448Sdh155122 			if (zone_check_datalink(&dl_owner_zid,
29233448Sdh155122 			    nwiftab.zone_nwif_physical) != 0)
29243448Sdh155122 				break;
29253448Sdh155122 
29263448Sdh155122 			/*
29273448Sdh155122 			 * If the zone being verified is
29283448Sdh155122 			 * running and owns the interface
29293448Sdh155122 			 */
29303448Sdh155122 			target_zid = getzoneidbyname(target_zone);
29313448Sdh155122 			if (target_zid == dl_owner_zid)
29323448Sdh155122 				break;
29333448Sdh155122 
29343448Sdh155122 			/* Zone id match failed, use name to check */
29353448Sdh155122 			if (getzonenamebyid(dl_owner_zid, dl_owner_zname,
29363448Sdh155122 			    ZONENAME_MAX) < 0) {
29373448Sdh155122 				/* No name, show ID instead */
29383448Sdh155122 				(void) snprintf(dl_owner_zname, ZONENAME_MAX,
29393448Sdh155122 				    "<%d>", dl_owner_zid);
29403448Sdh155122 			} else if (strcmp(dl_owner_zname, target_zone) == 0)
29413448Sdh155122 				break;
29423448Sdh155122 
29433448Sdh155122 			/*
29443448Sdh155122 			 * Note here we only report a warning that
29453448Sdh155122 			 * the interface is already in use by another
29463448Sdh155122 			 * running zone, and the verify process just
29473448Sdh155122 			 * goes on, if the interface is still in use
29483448Sdh155122 			 * when this zone really boots up, zoneadmd
29493448Sdh155122 			 * will find it. If the name of the zone which
29503448Sdh155122 			 * owns this interface cannot be determined,
29513448Sdh155122 			 * then it is not possible to determine if there
29523448Sdh155122 			 * is a conflict so just report it as a warning.
29532611Svp157776 			 */
29542611Svp157776 			(void) fprintf(stderr,
29553448Sdh155122 			    gettext("WARNING: skipping network interface "
29563448Sdh155122 			    "'%s' which is used by the non-global zone "
29573448Sdh155122 			    "'%s'.\n"), nwiftab.zone_nwif_physical,
29583448Sdh155122 			    dl_owner_zname);
29593448Sdh155122 			break;
29600Sstevel@tonic-gate 		}
29610Sstevel@tonic-gate 	}
29620Sstevel@tonic-gate 	(void) zonecfg_endnwifent(handle);
2963766Scarlsonj no_net:
29640Sstevel@tonic-gate 
29651931Sgjelinek 	/* verify that lofs has not been excluded from the kernel */
29662078Sgjelinek 	if (!(cmd_num == CMD_DETACH || cmd_num == CMD_ATTACH ||
29672078Sgjelinek 	    cmd_num == CMD_MOVE || cmd_num == CMD_CLONE) &&
29682078Sgjelinek 	    modctl(MODLOAD, 1, "fs/lofs", NULL) != 0) {
29691931Sgjelinek 		if (errno == ENXIO)
29701931Sgjelinek 			(void) fprintf(stderr, gettext("could not verify "
29711931Sgjelinek 			    "lofs(7FS): possibly excluded in /etc/system\n"));
29721931Sgjelinek 		else
29731931Sgjelinek 			(void) fprintf(stderr, gettext("could not verify "
29741931Sgjelinek 			    "lofs(7FS): %s\n"), strerror(errno));
29751931Sgjelinek 		return_code = Z_ERR;
29761931Sgjelinek 	}
29771931Sgjelinek 
29780Sstevel@tonic-gate 	if (verify_filesystems(handle) != Z_OK)
29790Sstevel@tonic-gate 		return_code = Z_ERR;
2980823Sgjelinek 	if (verify_ipd(handle) != Z_OK)
2981823Sgjelinek 		return_code = Z_ERR;
2982766Scarlsonj 	if (!in_alt_root && verify_rctls(handle) != Z_OK)
29830Sstevel@tonic-gate 		return_code = Z_ERR;
2984766Scarlsonj 	if (!in_alt_root && verify_pool(handle) != Z_OK)
29850Sstevel@tonic-gate 		return_code = Z_ERR;
29863339Szt129084 	if (!in_alt_root && verify_brand(handle, cmd_num, argv) != Z_OK)
29872712Snn35248 		return_code = Z_ERR;
2988789Sahrens 	if (!in_alt_root && verify_datasets(handle) != Z_OK)
2989789Sahrens 		return_code = Z_ERR;
29901645Scomay 
29911645Scomay 	/*
29921645Scomay 	 * As the "mount" command is used for patching/upgrading of zones
29931645Scomay 	 * or other maintenance processes, the zone's privilege set is not
29941645Scomay 	 * checked in this case.  Instead, the default, safe set of
29951645Scomay 	 * privileges will be used when this zone is created in the
29961645Scomay 	 * kernel.
29971645Scomay 	 */
29981645Scomay 	if (!in_alt_root && cmd_num != CMD_MOUNT &&
29991645Scomay 	    verify_limitpriv(handle) != Z_OK)
30001645Scomay 		return_code = Z_ERR;
30012078Sgjelinek 
30022078Sgjelinek 	return (return_code);
30032078Sgjelinek }
30042078Sgjelinek 
30052078Sgjelinek static int
30063339Szt129084 verify_details(int cmd_num, char *argv[])
30072078Sgjelinek {
30082078Sgjelinek 	zone_dochandle_t handle;
30092078Sgjelinek 	char zonepath[MAXPATHLEN], checkpath[MAXPATHLEN];
30102078Sgjelinek 	int return_code = Z_OK;
30112078Sgjelinek 	int err;
30122078Sgjelinek 
30132078Sgjelinek 	if ((handle = zonecfg_init_handle()) == NULL) {
30142078Sgjelinek 		zperror(cmd_to_str(cmd_num), B_TRUE);
30152078Sgjelinek 		return (Z_ERR);
30162078Sgjelinek 	}
30172078Sgjelinek 	if ((err = zonecfg_get_handle(target_zone, handle)) != Z_OK) {
30182078Sgjelinek 		errno = err;
30192078Sgjelinek 		zperror(cmd_to_str(cmd_num), B_TRUE);
30202078Sgjelinek 		zonecfg_fini_handle(handle);
30212078Sgjelinek 		return (Z_ERR);
30222078Sgjelinek 	}
30232078Sgjelinek 	if ((err = zonecfg_get_zonepath(handle, zonepath, sizeof (zonepath))) !=
30242078Sgjelinek 	    Z_OK) {
30252078Sgjelinek 		errno = err;
30262078Sgjelinek 		zperror(cmd_to_str(cmd_num), B_TRUE);
30272078Sgjelinek 		zonecfg_fini_handle(handle);
30282078Sgjelinek 		return (Z_ERR);
30292078Sgjelinek 	}
30302078Sgjelinek 	/*
30312078Sgjelinek 	 * zonecfg_get_zonepath() gets its data from the XML repository.
30322078Sgjelinek 	 * Verify this against the index file, which is checked first by
30332078Sgjelinek 	 * zone_get_zonepath().  If they don't match, bail out.
30342078Sgjelinek 	 */
30352078Sgjelinek 	if ((err = zone_get_zonepath(target_zone, checkpath,
30362078Sgjelinek 	    sizeof (checkpath))) != Z_OK) {
30372078Sgjelinek 		errno = err;
30382078Sgjelinek 		zperror2(target_zone, gettext("could not get zone path"));
30392078Sgjelinek 		return (Z_ERR);
30402078Sgjelinek 	}
30412078Sgjelinek 	if (strcmp(zonepath, checkpath) != 0) {
30422078Sgjelinek 		/*
30432078Sgjelinek 		 * TRANSLATION_NOTE
30442078Sgjelinek 		 * XML and zonepath are literals that should not be translated.
30452078Sgjelinek 		 */
30462078Sgjelinek 		(void) fprintf(stderr, gettext("The XML repository has "
30472078Sgjelinek 		    "zonepath '%s',\nbut the index file has zonepath '%s'.\n"
30482078Sgjelinek 		    "These must match, so fix the incorrect entry.\n"),
30492078Sgjelinek 		    zonepath, checkpath);
30502078Sgjelinek 		return (Z_ERR);
30512078Sgjelinek 	}
30522078Sgjelinek 	if (validate_zonepath(zonepath, cmd_num) != Z_OK) {
30532078Sgjelinek 		(void) fprintf(stderr, gettext("could not verify zonepath %s "
30542078Sgjelinek 		    "because of the above errors.\n"), zonepath);
30552078Sgjelinek 		return_code = Z_ERR;
30562078Sgjelinek 	}
30572078Sgjelinek 
30583339Szt129084 	if (verify_handle(cmd_num, handle, argv) != Z_OK)
30592078Sgjelinek 		return_code = Z_ERR;
30602078Sgjelinek 
30610Sstevel@tonic-gate 	zonecfg_fini_handle(handle);
30620Sstevel@tonic-gate 	if (return_code == Z_ERR)
30630Sstevel@tonic-gate 		(void) fprintf(stderr,
30640Sstevel@tonic-gate 		    gettext("%s: zone %s failed to verify\n"),
30650Sstevel@tonic-gate 		    execname, target_zone);
30660Sstevel@tonic-gate 	return (return_code);
30670Sstevel@tonic-gate }
30680Sstevel@tonic-gate 
30690Sstevel@tonic-gate static int
30700Sstevel@tonic-gate verify_func(int argc, char *argv[])
30710Sstevel@tonic-gate {
30720Sstevel@tonic-gate 	int arg;
30730Sstevel@tonic-gate 
30740Sstevel@tonic-gate 	optind = 0;
30750Sstevel@tonic-gate 	if ((arg = getopt(argc, argv, "?")) != EOF) {
30760Sstevel@tonic-gate 		switch (arg) {
30770Sstevel@tonic-gate 		case '?':
30780Sstevel@tonic-gate 			sub_usage(SHELP_VERIFY, CMD_VERIFY);
30790Sstevel@tonic-gate 			return (optopt == '?' ? Z_OK : Z_USAGE);
30800Sstevel@tonic-gate 		default:
30810Sstevel@tonic-gate 			sub_usage(SHELP_VERIFY, CMD_VERIFY);
30820Sstevel@tonic-gate 			return (Z_USAGE);
30830Sstevel@tonic-gate 		}
30840Sstevel@tonic-gate 	}
30850Sstevel@tonic-gate 	if (argc > optind) {
30860Sstevel@tonic-gate 		sub_usage(SHELP_VERIFY, CMD_VERIFY);
30870Sstevel@tonic-gate 		return (Z_USAGE);
30880Sstevel@tonic-gate 	}
30892712Snn35248 	if (sanity_check(target_zone, CMD_VERIFY, B_FALSE, B_FALSE, B_FALSE)
30902712Snn35248 	    != Z_OK)
30910Sstevel@tonic-gate 		return (Z_ERR);
30923339Szt129084 	return (verify_details(CMD_VERIFY, argv));
30930Sstevel@tonic-gate }
30940Sstevel@tonic-gate 
30952712Snn35248 static int
30962712Snn35248 addopt(char *buf, int opt, char *optarg, size_t bufsize)
30972712Snn35248 {
30982712Snn35248 	char optstring[4];
30992712Snn35248 
31002712Snn35248 	if (opt > 0)
31012712Snn35248 		(void) sprintf(optstring, " -%c", opt);
31022712Snn35248 	else
31032712Snn35248 		(void) strcpy(optstring, " ");
31042712Snn35248 
31052712Snn35248 	if ((strlcat(buf, optstring, bufsize) > bufsize))
31062712Snn35248 		return (Z_ERR);
31072712Snn35248 	if ((optarg != NULL) && (strlcat(buf, optarg, bufsize) > bufsize))
31082712Snn35248 		return (Z_ERR);
31092712Snn35248 	return (Z_OK);
31102712Snn35248 }
31110Sstevel@tonic-gate 
31120Sstevel@tonic-gate static int
31130Sstevel@tonic-gate install_func(int argc, char *argv[])
31140Sstevel@tonic-gate {
31152712Snn35248 	char cmdbuf[MAXPATHLEN];
31160Sstevel@tonic-gate 	int lockfd;
31172712Snn35248 	int arg, err, subproc_err;
31180Sstevel@tonic-gate 	char zonepath[MAXPATHLEN];
31192727Sedp 	brand_handle_t bh = NULL;
31200Sstevel@tonic-gate 	int status;
31211867Sgjelinek 	boolean_t nodataset = B_FALSE;
31222712Snn35248 	char opts[128];
31232712Snn35248 
31242712Snn35248 	if (target_zone == NULL) {
31252712Snn35248 		sub_usage(SHELP_INSTALL, CMD_INSTALL);
31262712Snn35248 		return (Z_USAGE);
31272712Snn35248 	}
31280Sstevel@tonic-gate 
3129766Scarlsonj 	if (zonecfg_in_alt_root()) {
3130766Scarlsonj 		zerror(gettext("cannot install zone in alternate root"));
3131766Scarlsonj 		return (Z_ERR);
3132766Scarlsonj 	}
3133766Scarlsonj 
31342712Snn35248 	if ((err = zone_get_zonepath(target_zone, zonepath,
31352712Snn35248 	    sizeof (zonepath))) != Z_OK) {
31362712Snn35248 		errno = err;
31372712Snn35248 		zperror2(target_zone, gettext("could not get zone path"));
31382712Snn35248 		return (Z_ERR);
31392712Snn35248 	}
31402712Snn35248 
31412712Snn35248 	/* Fetch the install command from the brand configuration.  */
31422727Sedp 	if ((bh = brand_open(target_brand)) == NULL) {
31432712Snn35248 		zerror(gettext("missing or invalid brand"));
31442712Snn35248 		return (Z_ERR);
31452712Snn35248 	}
31462712Snn35248 
31472712Snn35248 	(void) strcpy(cmdbuf, EXEC_PREFIX);
31482727Sedp 	if (brand_get_install(bh, target_zone, zonepath, cmdbuf + EXEC_LEN,
31492712Snn35248 	    sizeof (cmdbuf) - EXEC_LEN, 0, NULL) != 0) {
31502712Snn35248 		zerror("invalid brand configuration: missing install resource");
31512727Sedp 		brand_close(bh);
31522712Snn35248 		return (Z_ERR);
31532712Snn35248 	}
31542712Snn35248 
31552712Snn35248 	(void) strcpy(opts, "?x:");
31562712Snn35248 	if (!is_native_zone) {
31572712Snn35248 		/*
31582712Snn35248 		 * Fetch the list of recognized command-line options from
31592712Snn35248 		 * the brand configuration file.
31602712Snn35248 		 */
31612727Sedp 		if (brand_get_installopts(bh, opts + strlen(opts),
31622712Snn35248 		    sizeof (opts) - strlen(opts)) != 0) {
31632712Snn35248 			zerror("invalid brand configuration: missing "
31642712Snn35248 			    "install options resource");
31652727Sedp 			brand_close(bh);
31662712Snn35248 			return (Z_ERR);
31672712Snn35248 		}
31682712Snn35248 	}
31692727Sedp 	brand_close(bh);
31702712Snn35248 
31710Sstevel@tonic-gate 	optind = 0;
31722712Snn35248 	while ((arg = getopt(argc, argv, opts)) != EOF) {
31730Sstevel@tonic-gate 		switch (arg) {
31740Sstevel@tonic-gate 		case '?':
31750Sstevel@tonic-gate 			sub_usage(SHELP_INSTALL, CMD_INSTALL);
31760Sstevel@tonic-gate 			return (optopt == '?' ? Z_OK : Z_USAGE);
31771867Sgjelinek 		case 'x':
31781867Sgjelinek 			if (strcmp(optarg, "nodataset") != 0) {
31791867Sgjelinek 				sub_usage(SHELP_INSTALL, CMD_INSTALL);
31801867Sgjelinek 				return (Z_USAGE);
31811867Sgjelinek 			}
31821867Sgjelinek 			nodataset = B_TRUE;
31831867Sgjelinek 			break;
31840Sstevel@tonic-gate 		default:
31852712Snn35248 			if (is_native_zone) {
31862712Snn35248 				sub_usage(SHELP_INSTALL, CMD_INSTALL);
31872712Snn35248 				return (Z_USAGE);
31882712Snn35248 			}
31892712Snn35248 
31902712Snn35248 			/*
31912712Snn35248 			 * This option isn't for zoneadm, so append it to
31922712Snn35248 			 * the command line passed to the brand-specific
31932712Snn35248 			 * install routine.
31942712Snn35248 			 */
31952712Snn35248 			if (addopt(cmdbuf, optopt, optarg,
31962712Snn35248 			    sizeof (cmdbuf)) != Z_OK) {
31972712Snn35248 				zerror("Install command line too long");
31982712Snn35248 				return (Z_ERR);
31992712Snn35248 			}
32002712Snn35248 			break;
32010Sstevel@tonic-gate 		}
32020Sstevel@tonic-gate 	}
32032712Snn35248 
32042712Snn35248 	if (!is_native_zone) {
32052712Snn35248 		for (; optind < argc; optind++) {
32062712Snn35248 			if (addopt(cmdbuf, 0, argv[optind],
32072712Snn35248 			    sizeof (cmdbuf)) != Z_OK) {
32082712Snn35248 				zerror("Install command line too long");
32092712Snn35248 				return (Z_ERR);
32102712Snn35248 			}
32112712Snn35248 		}
32122712Snn35248 	}
32132712Snn35248 
32142712Snn35248 	if (sanity_check(target_zone, CMD_INSTALL, B_FALSE, B_TRUE, B_FALSE)
32152712Snn35248 	    != Z_OK)
32160Sstevel@tonic-gate 		return (Z_ERR);
32173339Szt129084 	if (verify_details(CMD_INSTALL, argv) != Z_OK)
32180Sstevel@tonic-gate 		return (Z_ERR);
32190Sstevel@tonic-gate 
32200Sstevel@tonic-gate 	if (grab_lock_file(target_zone, &lockfd) != Z_OK) {
32210Sstevel@tonic-gate 		zerror(gettext("another %s may have an operation in progress."),
32221300Sgjelinek 		    "zoneadm");
32230Sstevel@tonic-gate 		return (Z_ERR);
32240Sstevel@tonic-gate 	}
32250Sstevel@tonic-gate 	err = zone_set_state(target_zone, ZONE_STATE_INCOMPLETE);
32260Sstevel@tonic-gate 	if (err != Z_OK) {
32270Sstevel@tonic-gate 		errno = err;
32280Sstevel@tonic-gate 		zperror2(target_zone, gettext("could not set state"));
32290Sstevel@tonic-gate 		goto done;
32300Sstevel@tonic-gate 	}
32310Sstevel@tonic-gate 
32322712Snn35248 	if (!nodataset)
32332712Snn35248 		create_zfs_zonepath(zonepath);
32342712Snn35248 
32350Sstevel@tonic-gate 	/*
32360Sstevel@tonic-gate 	 * According to the Application Packaging Developer's Guide, a
32370Sstevel@tonic-gate 	 * "checkinstall" script when included in a package is executed as
32380Sstevel@tonic-gate 	 * the user "install", if such a user exists, or by the user
32390Sstevel@tonic-gate 	 * "nobody".  In order to support this dubious behavior, the path
32400Sstevel@tonic-gate 	 * to the zone being constructed is opened up during the life of
32410Sstevel@tonic-gate 	 * the command laying down the zone's root file system.  Once this
32420Sstevel@tonic-gate 	 * has completed, regardless of whether it was successful, the
32430Sstevel@tonic-gate 	 * path to the zone is again restricted.
32440Sstevel@tonic-gate 	 */
32450Sstevel@tonic-gate 	if (chmod(zonepath, DEFAULT_DIR_MODE) != 0) {
32460Sstevel@tonic-gate 		zperror(zonepath, B_FALSE);
32470Sstevel@tonic-gate 		err = Z_ERR;
32480Sstevel@tonic-gate 		goto done;
32490Sstevel@tonic-gate 	}
32500Sstevel@tonic-gate 
32512712Snn35248 	if (is_native_zone)
32522712Snn35248 		status = do_subproc(cmdbuf);
32532712Snn35248 	else
32542712Snn35248 		status = do_subproc_interactive(cmdbuf);
32552712Snn35248 
32560Sstevel@tonic-gate 	if (chmod(zonepath, S_IRWXU) != 0) {
32570Sstevel@tonic-gate 		zperror(zonepath, B_FALSE);
32580Sstevel@tonic-gate 		err = Z_ERR;
32590Sstevel@tonic-gate 		goto done;
32600Sstevel@tonic-gate 	}
32612712Snn35248 	if ((subproc_err =
32622712Snn35248 	    subproc_status(gettext("brand-specific installation"), status,
32632712Snn35248 	    B_FALSE)) != ZONE_SUBPROC_OK) {
32642712Snn35248 		err = Z_ERR;
32650Sstevel@tonic-gate 		goto done;
32662712Snn35248 	}
32670Sstevel@tonic-gate 
32680Sstevel@tonic-gate 	if ((err = zone_set_state(target_zone, ZONE_STATE_INSTALLED)) != Z_OK) {
32690Sstevel@tonic-gate 		errno = err;
32700Sstevel@tonic-gate 		zperror2(target_zone, gettext("could not set state"));
32710Sstevel@tonic-gate 		goto done;
32720Sstevel@tonic-gate 	}
32730Sstevel@tonic-gate 
32740Sstevel@tonic-gate done:
32752712Snn35248 	/*
32762712Snn35248 	 * If the install script exited with ZONE_SUBPROC_USAGE or
32772712Snn35248 	 * ZONE_SUBPROC_NOTCOMPLETE, try to clean up the zone and leave the
32782712Snn35248 	 * zone in the CONFIGURED state so that another install can be
32792712Snn35248 	 * attempted without requiring an uninstall first.
32802712Snn35248 	 */
32812712Snn35248 	if ((subproc_err == ZONE_SUBPROC_USAGE) ||
32822712Snn35248 	    (subproc_err == ZONE_SUBPROC_NOTCOMPLETE)) {
32832712Snn35248 		if ((err = cleanup_zonepath(zonepath, B_FALSE)) != Z_OK) {
32842712Snn35248 			errno = err;
32852712Snn35248 			zperror2(target_zone,
32862712Snn35248 			    gettext("cleaning up zonepath failed"));
32872712Snn35248 		} else if ((err = zone_set_state(target_zone,
32882712Snn35248 		    ZONE_STATE_CONFIGURED)) != Z_OK) {
32892712Snn35248 			errno = err;
32902712Snn35248 			zperror2(target_zone, gettext("could not set state"));
32912712Snn35248 		}
32922712Snn35248 	}
32932712Snn35248 
32940Sstevel@tonic-gate 	release_lock_file(lockfd);
32950Sstevel@tonic-gate 	return ((err == Z_OK) ? Z_OK : Z_ERR);
32960Sstevel@tonic-gate }
32970Sstevel@tonic-gate 
32980Sstevel@tonic-gate /*
32991300Sgjelinek  * Check that the inherited pkg dirs are the same for the clone and its source.
33001300Sgjelinek  * The easiest way to do that is check that the list of ipds is the same
33011300Sgjelinek  * by matching each one against the other.  This algorithm should be fine since
33021300Sgjelinek  * the list of ipds should not be that long.
33031300Sgjelinek  */
33041300Sgjelinek static int
33051300Sgjelinek valid_ipd_clone(zone_dochandle_t s_handle, char *source_zone,
33061300Sgjelinek 	zone_dochandle_t t_handle, char *target_zone)
33071300Sgjelinek {
33081300Sgjelinek 	int err;
33091300Sgjelinek 	int res = Z_OK;
33101300Sgjelinek 	int s_cnt = 0;
33111300Sgjelinek 	int t_cnt = 0;
33121300Sgjelinek 	struct zone_fstab s_fstab;
33131300Sgjelinek 	struct zone_fstab t_fstab;
33141300Sgjelinek 
33151300Sgjelinek 	/*
33161300Sgjelinek 	 * First check the source of the clone against the target.
33171300Sgjelinek 	 */
33181300Sgjelinek 	if ((err = zonecfg_setipdent(s_handle)) != Z_OK) {
33191300Sgjelinek 		errno = err;
33201300Sgjelinek 		zperror2(source_zone, gettext("could not enumerate "
33211300Sgjelinek 		    "inherit-pkg-dirs"));
33221300Sgjelinek 		return (Z_ERR);
33231300Sgjelinek 	}
33241300Sgjelinek 
33251300Sgjelinek 	while (zonecfg_getipdent(s_handle, &s_fstab) == Z_OK) {
33261300Sgjelinek 		boolean_t match = B_FALSE;
33271300Sgjelinek 
33281300Sgjelinek 		s_cnt++;
33291300Sgjelinek 
33301300Sgjelinek 		if ((err = zonecfg_setipdent(t_handle)) != Z_OK) {
33311300Sgjelinek 			errno = err;
33321300Sgjelinek 			zperror2(target_zone, gettext("could not enumerate "
33331300Sgjelinek 			    "inherit-pkg-dirs"));
33341300Sgjelinek 			(void) zonecfg_endipdent(s_handle);
33351300Sgjelinek 			return (Z_ERR);
33361300Sgjelinek 		}
33371300Sgjelinek 
33381300Sgjelinek 		while (zonecfg_getipdent(t_handle, &t_fstab) == Z_OK) {
33391300Sgjelinek 			if (strcmp(s_fstab.zone_fs_dir, t_fstab.zone_fs_dir)
33401300Sgjelinek 			    == 0) {
33411300Sgjelinek 				match = B_TRUE;
33421300Sgjelinek 				break;
33431300Sgjelinek 			}
33441300Sgjelinek 		}
33451300Sgjelinek 		(void) zonecfg_endipdent(t_handle);
33461300Sgjelinek 
33471300Sgjelinek 		if (!match) {
33481300Sgjelinek 			(void) fprintf(stderr, gettext("inherit-pkg-dir "
33491300Sgjelinek 			    "'%s' is not configured in zone %s.\n"),
33501300Sgjelinek 			    s_fstab.zone_fs_dir, target_zone);
33511300Sgjelinek 			res = Z_ERR;
33521300Sgjelinek 		}
33531300Sgjelinek 	}
33541300Sgjelinek 
33551300Sgjelinek 	(void) zonecfg_endipdent(s_handle);
33561300Sgjelinek 
33571300Sgjelinek 	/* skip the next check if we already have errors */
33581300Sgjelinek 	if (res == Z_ERR)
33591300Sgjelinek 		return (res);
33601300Sgjelinek 
33611300Sgjelinek 	/*
33621300Sgjelinek 	 * Now check the number of ipds in the target so we can verify
33631300Sgjelinek 	 * that the source is not a subset of the target.
33641300Sgjelinek 	 */
33651300Sgjelinek 	if ((err = zonecfg_setipdent(t_handle)) != Z_OK) {
33661300Sgjelinek 		errno = err;
33671300Sgjelinek 		zperror2(target_zone, gettext("could not enumerate "
33681300Sgjelinek 		    "inherit-pkg-dirs"));
33691300Sgjelinek 		return (Z_ERR);
33701300Sgjelinek 	}
33711300Sgjelinek 
33721300Sgjelinek 	while (zonecfg_getipdent(t_handle, &t_fstab) == Z_OK)
33731300Sgjelinek 		t_cnt++;
33741300Sgjelinek 
33751300Sgjelinek 	(void) zonecfg_endipdent(t_handle);
33761300Sgjelinek 
33771300Sgjelinek 	if (t_cnt != s_cnt) {
33781300Sgjelinek 		(void) fprintf(stderr, gettext("Zone %s is configured "
33791300Sgjelinek 		    "with inherit-pkg-dirs that are not configured in zone "
33801300Sgjelinek 		    "%s.\n"), target_zone, source_zone);
33811300Sgjelinek 		res = Z_ERR;
33821300Sgjelinek 	}
33831300Sgjelinek 
33841300Sgjelinek 	return (res);
33851300Sgjelinek }
33861300Sgjelinek 
33871300Sgjelinek static void
33881300Sgjelinek warn_dev_match(zone_dochandle_t s_handle, char *source_zone,
33891300Sgjelinek 	zone_dochandle_t t_handle, char *target_zone)
33901300Sgjelinek {
33911300Sgjelinek 	int err;
33921300Sgjelinek 	struct zone_devtab s_devtab;
33931300Sgjelinek 	struct zone_devtab t_devtab;
33941300Sgjelinek 
33951300Sgjelinek 	if ((err = zonecfg_setdevent(t_handle)) != Z_OK) {
33961300Sgjelinek 		errno = err;
33971300Sgjelinek 		zperror2(target_zone, gettext("could not enumerate devices"));
33981300Sgjelinek 		return;
33991300Sgjelinek 	}
34001300Sgjelinek 
34011300Sgjelinek 	while (zonecfg_getdevent(t_handle, &t_devtab) == Z_OK) {
34021300Sgjelinek 		if ((err = zonecfg_setdevent(s_handle)) != Z_OK) {
34031300Sgjelinek 			errno = err;
34041300Sgjelinek 			zperror2(source_zone,
34051300Sgjelinek 			    gettext("could not enumerate devices"));
34061300Sgjelinek 			(void) zonecfg_enddevent(t_handle);
34071300Sgjelinek 			return;
34081300Sgjelinek 		}
34091300Sgjelinek 
34101300Sgjelinek 		while (zonecfg_getdevent(s_handle, &s_devtab) == Z_OK) {
34111300Sgjelinek 			/*
34121300Sgjelinek 			 * Use fnmatch to catch the case where wildcards
34131300Sgjelinek 			 * were used in one zone and the other has an
34141300Sgjelinek 			 * explicit entry (e.g. /dev/dsk/c0t0d0s6 vs.
34151300Sgjelinek 			 * /dev/\*dsk/c0t0d0s6).
34161300Sgjelinek 			 */
34171300Sgjelinek 			if (fnmatch(t_devtab.zone_dev_match,
34181300Sgjelinek 			    s_devtab.zone_dev_match, FNM_PATHNAME) == 0 ||
34191300Sgjelinek 			    fnmatch(s_devtab.zone_dev_match,
34201300Sgjelinek 			    t_devtab.zone_dev_match, FNM_PATHNAME) == 0) {
34211300Sgjelinek 				(void) fprintf(stderr,
34221300Sgjelinek 				    gettext("WARNING: device '%s' "
34231300Sgjelinek 				    "is configured in both zones.\n"),
34241300Sgjelinek 				    t_devtab.zone_dev_match);
34251300Sgjelinek 				break;
34261300Sgjelinek 			}
34271300Sgjelinek 		}
34281300Sgjelinek 		(void) zonecfg_enddevent(s_handle);
34291300Sgjelinek 	}
34301300Sgjelinek 
34311300Sgjelinek 	(void) zonecfg_enddevent(t_handle);
34321300Sgjelinek }
34331300Sgjelinek 
34341300Sgjelinek /*
34351300Sgjelinek  * Check if the specified mount option (opt) is contained within the
34361300Sgjelinek  * options string.
34371300Sgjelinek  */
34381300Sgjelinek static boolean_t
34391300Sgjelinek opt_match(char *opt, char *options)
34401300Sgjelinek {
34411300Sgjelinek 	char *p;
34421300Sgjelinek 	char *lastp;
34431300Sgjelinek 
34441300Sgjelinek 	if ((p = strtok_r(options, ",", &lastp)) != NULL) {
34451300Sgjelinek 		if (strcmp(p, opt) == 0)
34461300Sgjelinek 			return (B_TRUE);
34471300Sgjelinek 		while ((p = strtok_r(NULL, ",", &lastp)) != NULL) {
34481300Sgjelinek 			if (strcmp(p, opt) == 0)
34491300Sgjelinek 				return (B_TRUE);
34501300Sgjelinek 		}
34511300Sgjelinek 	}
34521300Sgjelinek 
34531300Sgjelinek 	return (B_FALSE);
34541300Sgjelinek }
34551300Sgjelinek 
34561867Sgjelinek #define	RW_LOFS	"WARNING: read-write lofs file system on '%s' is configured " \
34571300Sgjelinek 	"in both zones.\n"
34581300Sgjelinek 
34591300Sgjelinek static void
34601300Sgjelinek print_fs_warnings(struct zone_fstab *s_fstab, struct zone_fstab *t_fstab)
34611300Sgjelinek {
34621300Sgjelinek 	/*
34631300Sgjelinek 	 * It is ok to have shared lofs mounted fs but we want to warn if
34641300Sgjelinek 	 * either is rw since this will effect the other zone.
34651300Sgjelinek 	 */
34661300Sgjelinek 	if (strcmp(t_fstab->zone_fs_type, "lofs") == 0) {
34671300Sgjelinek 		zone_fsopt_t *optp;
34681300Sgjelinek 
34691300Sgjelinek 		/* The default is rw so no options means rw */
34701300Sgjelinek 		if (t_fstab->zone_fs_options == NULL ||
34711300Sgjelinek 		    s_fstab->zone_fs_options == NULL) {
34721300Sgjelinek 			(void) fprintf(stderr, gettext(RW_LOFS),
34731300Sgjelinek 			    t_fstab->zone_fs_special);
34741300Sgjelinek 			return;
34751300Sgjelinek 		}
34761300Sgjelinek 
34771300Sgjelinek 		for (optp = s_fstab->zone_fs_options; optp != NULL;
34781300Sgjelinek 		    optp = optp->zone_fsopt_next) {
34791300Sgjelinek 			if (opt_match("rw", optp->zone_fsopt_opt)) {
34801300Sgjelinek 				(void) fprintf(stderr, gettext(RW_LOFS),
34811300Sgjelinek 				    s_fstab->zone_fs_special);
34821300Sgjelinek 				return;
34831300Sgjelinek 			}
34841300Sgjelinek 		}
34851300Sgjelinek 
34861300Sgjelinek 		for (optp = t_fstab->zone_fs_options; optp != NULL;
34871300Sgjelinek 		    optp = optp->zone_fsopt_next) {
34881300Sgjelinek 			if (opt_match("rw", optp->zone_fsopt_opt)) {
34891300Sgjelinek 				(void) fprintf(stderr, gettext(RW_LOFS),
34901300Sgjelinek 				    t_fstab->zone_fs_special);
34911300Sgjelinek 				return;
34921300Sgjelinek 			}
34931300Sgjelinek 		}
34941300Sgjelinek 
34951300Sgjelinek 		return;
34961300Sgjelinek 	}
34971300Sgjelinek 
34981300Sgjelinek 	/*
34991300Sgjelinek 	 * TRANSLATION_NOTE
35001867Sgjelinek 	 * The first variable is the file system type and the second is
35011867Sgjelinek 	 * the file system special device.  For example,
35021867Sgjelinek 	 * WARNING: ufs file system on '/dev/dsk/c0t0d0s0' ...
35031300Sgjelinek 	 */
35041867Sgjelinek 	(void) fprintf(stderr, gettext("WARNING: %s file system on '%s' "
35051300Sgjelinek 	    "is configured in both zones.\n"), t_fstab->zone_fs_type,
35061300Sgjelinek 	    t_fstab->zone_fs_special);
35071300Sgjelinek }
35081300Sgjelinek 
35091300Sgjelinek static void
35101300Sgjelinek warn_fs_match(zone_dochandle_t s_handle, char *source_zone,
35111300Sgjelinek 	zone_dochandle_t t_handle, char *target_zone)
35121300Sgjelinek {
35131300Sgjelinek 	int err;
35141300Sgjelinek 	struct zone_fstab s_fstab;
35151300Sgjelinek 	struct zone_fstab t_fstab;
35161300Sgjelinek 
35171300Sgjelinek 	if ((err = zonecfg_setfsent(t_handle)) != Z_OK) {
35181300Sgjelinek 		errno = err;
35191300Sgjelinek 		zperror2(target_zone,
35201867Sgjelinek 		    gettext("could not enumerate file systems"));
35211300Sgjelinek 		return;
35221300Sgjelinek 	}
35231300Sgjelinek 
35241300Sgjelinek 	while (zonecfg_getfsent(t_handle, &t_fstab) == Z_OK) {
35251300Sgjelinek 		if ((err = zonecfg_setfsent(s_handle)) != Z_OK) {
35261300Sgjelinek 			errno = err;
35271300Sgjelinek 			zperror2(source_zone,
35281867Sgjelinek 			    gettext("could not enumerate file systems"));
35291300Sgjelinek 			(void) zonecfg_endfsent(t_handle);
35301300Sgjelinek 			return;
35311300Sgjelinek 		}
35321300Sgjelinek 
35331300Sgjelinek 		while (zonecfg_getfsent(s_handle, &s_fstab) == Z_OK) {
35341300Sgjelinek 			if (strcmp(t_fstab.zone_fs_special,
35351300Sgjelinek 			    s_fstab.zone_fs_special) == 0) {
35361300Sgjelinek 				print_fs_warnings(&s_fstab, &t_fstab);
35371300Sgjelinek 				break;
35381300Sgjelinek 			}
35391300Sgjelinek 		}
35401300Sgjelinek 		(void) zonecfg_endfsent(s_handle);
35411300Sgjelinek 	}
35421300Sgjelinek 
35431300Sgjelinek 	(void) zonecfg_endfsent(t_handle);
35441300Sgjelinek }
35451300Sgjelinek 
35461300Sgjelinek /*
35471300Sgjelinek  * We don't catch the case where you used the same IP address but
35481300Sgjelinek  * it is not an exact string match.  For example, 192.9.0.128 vs. 192.09.0.128.
35491300Sgjelinek  * However, we're not going to worry about that but we will check for
35501300Sgjelinek  * a possible netmask on one of the addresses (e.g. 10.0.0.1 and 10.0.0.1/24)
35511300Sgjelinek  * and handle that case as a match.
35521300Sgjelinek  */
35531300Sgjelinek static void
35541300Sgjelinek warn_ip_match(zone_dochandle_t s_handle, char *source_zone,
35551300Sgjelinek 	zone_dochandle_t t_handle, char *target_zone)
35561300Sgjelinek {
35571300Sgjelinek 	int err;
35581300Sgjelinek 	struct zone_nwiftab s_nwiftab;
35591300Sgjelinek 	struct zone_nwiftab t_nwiftab;
35601300Sgjelinek 
35611300Sgjelinek 	if ((err = zonecfg_setnwifent(t_handle)) != Z_OK) {
35621300Sgjelinek 		errno = err;
35631300Sgjelinek 		zperror2(target_zone,
35641300Sgjelinek 		    gettext("could not enumerate network interfaces"));
35651300Sgjelinek 		return;
35661300Sgjelinek 	}
35671300Sgjelinek 
35681300Sgjelinek 	while (zonecfg_getnwifent(t_handle, &t_nwiftab) == Z_OK) {
35691300Sgjelinek 		char *p;
35701300Sgjelinek 
35711300Sgjelinek 		/* remove an (optional) netmask from the address */
35721300Sgjelinek 		if ((p = strchr(t_nwiftab.zone_nwif_address, '/')) != NULL)
35731300Sgjelinek 			*p = '\0';
35741300Sgjelinek 
35751300Sgjelinek 		if ((err = zonecfg_setnwifent(s_handle)) != Z_OK) {
35761300Sgjelinek 			errno = err;
35771300Sgjelinek 			zperror2(source_zone,
35781300Sgjelinek 			    gettext("could not enumerate network interfaces"));
35791300Sgjelinek 			(void) zonecfg_endnwifent(t_handle);
35801300Sgjelinek 			return;
35811300Sgjelinek 		}
35821300Sgjelinek 
35831300Sgjelinek 		while (zonecfg_getnwifent(s_handle, &s_nwiftab) == Z_OK) {
35841300Sgjelinek 			/* remove an (optional) netmask from the address */
35851300Sgjelinek 			if ((p = strchr(s_nwiftab.zone_nwif_address, '/'))
35861300Sgjelinek 			    != NULL)
35871300Sgjelinek 				*p = '\0';
35881300Sgjelinek 
35893448Sdh155122 			/* For exclusive-IP zones, address is not specified. */
35903448Sdh155122 			if (strlen(s_nwiftab.zone_nwif_address) == 0)
35913448Sdh155122 				continue;
35923448Sdh155122 
35931300Sgjelinek 			if (strcmp(t_nwiftab.zone_nwif_address,
35941300Sgjelinek 			    s_nwiftab.zone_nwif_address) == 0) {
35951300Sgjelinek 				(void) fprintf(stderr,
35961300Sgjelinek 				    gettext("WARNING: network address '%s' "
35971300Sgjelinek 				    "is configured in both zones.\n"),
35981300Sgjelinek 				    t_nwiftab.zone_nwif_address);
35991300Sgjelinek 				break;
36001300Sgjelinek 			}
36011300Sgjelinek 		}
36021300Sgjelinek 		(void) zonecfg_endnwifent(s_handle);
36031300Sgjelinek 	}
36041300Sgjelinek 
36051300Sgjelinek 	(void) zonecfg_endnwifent(t_handle);
36061300Sgjelinek }
36071300Sgjelinek 
36081300Sgjelinek static void
36092712Snn35248 warn_dataset_match(zone_dochandle_t s_handle, char *source,
36102712Snn35248 	zone_dochandle_t t_handle, char *target)
36111300Sgjelinek {
36121300Sgjelinek 	int err;
36131300Sgjelinek 	struct zone_dstab s_dstab;
36141300Sgjelinek 	struct zone_dstab t_dstab;
36151300Sgjelinek 
36161300Sgjelinek 	if ((err = zonecfg_setdsent(t_handle)) != Z_OK) {
36171300Sgjelinek 		errno = err;
36182712Snn35248 		zperror2(target, gettext("could not enumerate datasets"));
36191300Sgjelinek 		return;
36201300Sgjelinek 	}
36211300Sgjelinek 
36221300Sgjelinek 	while (zonecfg_getdsent(t_handle, &t_dstab) == Z_OK) {
36231300Sgjelinek 		if ((err = zonecfg_setdsent(s_handle)) != Z_OK) {
36241300Sgjelinek 			errno = err;
36252712Snn35248 			zperror2(source,
36261300Sgjelinek 			    gettext("could not enumerate datasets"));
36271300Sgjelinek 			(void) zonecfg_enddsent(t_handle);
36281300Sgjelinek 			return;
36291300Sgjelinek 		}
36301300Sgjelinek 
36311300Sgjelinek 		while (zonecfg_getdsent(s_handle, &s_dstab) == Z_OK) {
36321300Sgjelinek 			if (strcmp(t_dstab.zone_dataset_name,
36331300Sgjelinek 			    s_dstab.zone_dataset_name) == 0) {
36342712Snn35248 				target_zone = source;
36352712Snn35248 				zerror(gettext("WARNING: dataset '%s' "
36361300Sgjelinek 				    "is configured in both zones.\n"),
36371300Sgjelinek 				    t_dstab.zone_dataset_name);
36381300Sgjelinek 				break;
36391300Sgjelinek 			}
36401300Sgjelinek 		}
36411300Sgjelinek 		(void) zonecfg_enddsent(s_handle);
36421300Sgjelinek 	}
36431300Sgjelinek 
36441300Sgjelinek 	(void) zonecfg_enddsent(t_handle);
36451300Sgjelinek }
36461300Sgjelinek 
36472712Snn35248 /*
36482712Snn35248  * Check that the clone and its source have the same brand type.
36492712Snn35248  */
36502712Snn35248 static int
36512712Snn35248 valid_brand_clone(char *source_zone, char *target_zone)
36522712Snn35248 {
36532727Sedp 	brand_handle_t bh;
36542712Snn35248 	char source_brand[MAXNAMELEN];
36552712Snn35248 
36562712Snn35248 	if ((zone_get_brand(source_zone, source_brand,
36572712Snn35248 	    sizeof (source_brand))) != Z_OK) {
36582712Snn35248 		(void) fprintf(stderr, "%s: zone '%s': %s\n",
36592712Snn35248 		    execname, source_zone, gettext("missing or invalid brand"));
36602712Snn35248 		return (Z_ERR);
36612712Snn35248 	}
36622712Snn35248 
36632712Snn35248 	if (strcmp(source_brand, target_brand) != NULL) {
36642712Snn35248 		(void) fprintf(stderr,
36652712Snn35248 		    gettext("%s: Zones '%s' and '%s' have different brand "
36662712Snn35248 		    "types.\n"), execname, source_zone, target_zone);
36672712Snn35248 		return (Z_ERR);
36682712Snn35248 	}
36692712Snn35248 
36702727Sedp 	if ((bh = brand_open(target_brand)) == NULL) {
36712712Snn35248 		zerror(gettext("missing or invalid brand"));
36722712Snn35248 		return (Z_ERR);
36732712Snn35248 	}
36742727Sedp 	brand_close(bh);
36752712Snn35248 	return (Z_OK);
36762712Snn35248 }
36772712Snn35248 
36781300Sgjelinek static int
36791300Sgjelinek validate_clone(char *source_zone, char *target_zone)
36801300Sgjelinek {
36811300Sgjelinek 	int err = Z_OK;
36821300Sgjelinek 	zone_dochandle_t s_handle;
36831300Sgjelinek 	zone_dochandle_t t_handle;
36841300Sgjelinek 
36851300Sgjelinek 	if ((t_handle = zonecfg_init_handle()) == NULL) {
36861300Sgjelinek 		zperror(cmd_to_str(CMD_CLONE), B_TRUE);
36871300Sgjelinek 		return (Z_ERR);
36881300Sgjelinek 	}
36891300Sgjelinek 	if ((err = zonecfg_get_handle(target_zone, t_handle)) != Z_OK) {
36901300Sgjelinek 		errno = err;
36911300Sgjelinek 		zperror(cmd_to_str(CMD_CLONE), B_TRUE);
36921300Sgjelinek 		zonecfg_fini_handle(t_handle);
36931300Sgjelinek 		return (Z_ERR);
36941300Sgjelinek 	}
36951300Sgjelinek 
36961300Sgjelinek 	if ((s_handle = zonecfg_init_handle()) == NULL) {
36971300Sgjelinek 		zperror(cmd_to_str(CMD_CLONE), B_TRUE);
36981300Sgjelinek 		zonecfg_fini_handle(t_handle);
36991300Sgjelinek 		return (Z_ERR);
37001300Sgjelinek 	}
37011300Sgjelinek 	if ((err = zonecfg_get_handle(source_zone, s_handle)) != Z_OK) {
37021300Sgjelinek 		errno = err;
37031300Sgjelinek 		zperror(cmd_to_str(CMD_CLONE), B_TRUE);
37041300Sgjelinek 		goto done;
37051300Sgjelinek 	}
37061300Sgjelinek 
37072712Snn35248 	/* verify new zone has same brand type */
37082712Snn35248 	err = valid_brand_clone(source_zone, target_zone);
37092712Snn35248 	if (err != Z_OK)
37102712Snn35248 		goto done;
37112712Snn35248 
37121300Sgjelinek 	/* verify new zone has same inherit-pkg-dirs */
37131300Sgjelinek 	err = valid_ipd_clone(s_handle, source_zone, t_handle, target_zone);
37141300Sgjelinek 
37151300Sgjelinek 	/* warn about imported fs's which are the same */
37161300Sgjelinek 	warn_fs_match(s_handle, source_zone, t_handle, target_zone);
37171300Sgjelinek 
37181300Sgjelinek 	/* warn about imported IP addresses which are the same */
37191300Sgjelinek 	warn_ip_match(s_handle, source_zone, t_handle, target_zone);
37201300Sgjelinek 
37211300Sgjelinek 	/* warn about imported devices which are the same */
37221300Sgjelinek 	warn_dev_match(s_handle, source_zone, t_handle, target_zone);
37231300Sgjelinek 
37241300Sgjelinek 	/* warn about imported datasets which are the same */
37251300Sgjelinek 	warn_dataset_match(s_handle, source_zone, t_handle, target_zone);
37261300Sgjelinek 
37271300Sgjelinek done:
37281300Sgjelinek 	zonecfg_fini_handle(t_handle);
37291300Sgjelinek 	zonecfg_fini_handle(s_handle);
37301300Sgjelinek 
37311300Sgjelinek 	return ((err == Z_OK) ? Z_OK : Z_ERR);
37321300Sgjelinek }
37331300Sgjelinek 
37341300Sgjelinek static int
37351300Sgjelinek copy_zone(char *src, char *dst)
37361300Sgjelinek {
37371300Sgjelinek 	boolean_t out_null = B_FALSE;
37381300Sgjelinek 	int status;
37391300Sgjelinek 	char *outfile;
37401300Sgjelinek 	char cmdbuf[MAXPATHLEN * 2 + 128];
37411300Sgjelinek 
37421300Sgjelinek 	if ((outfile = tempnam("/var/log", "zone")) == NULL) {
37431300Sgjelinek 		outfile = "/dev/null";
37441300Sgjelinek 		out_null = B_TRUE;
37451300Sgjelinek 	}
37461300Sgjelinek 
37471867Sgjelinek 	/*
37481867Sgjelinek 	 * Use find to get the list of files to copy.  We need to skip
37491867Sgjelinek 	 * files of type "socket" since cpio can't handle those but that
37501867Sgjelinek 	 * should be ok since the app will recreate the socket when it runs.
37511867Sgjelinek 	 * We also need to filter out anything under the .zfs subdir.  Since
37521867Sgjelinek 	 * find is running depth-first, we need the extra egrep to filter .zfs.
37531867Sgjelinek 	 */
37541300Sgjelinek 	(void) snprintf(cmdbuf, sizeof (cmdbuf),
37551867Sgjelinek 	    "cd %s && /usr/bin/find . -type s -prune -o -depth -print | "
37561607Sgjelinek 	    "/usr/bin/egrep -v '^\\./\\.zfs$|^\\./\\.zfs/' | "
37571300Sgjelinek 	    "/usr/bin/cpio -pdmuP@ %s > %s 2>&1",
37581300Sgjelinek 	    src, dst, outfile);
37591300Sgjelinek 
37601300Sgjelinek 	status = do_subproc(cmdbuf);
37611300Sgjelinek 
37622712Snn35248 	if (subproc_status("copy", status, B_TRUE) != ZONE_SUBPROC_OK) {
37631300Sgjelinek 		if (!out_null)
37641300Sgjelinek 			(void) fprintf(stderr, gettext("\nThe copy failed.\n"
37651300Sgjelinek 			    "More information can be found in %s\n"), outfile);
37662712Snn35248 		return (Z_ERR);
37671300Sgjelinek 	}
37681300Sgjelinek 
37691300Sgjelinek 	if (!out_null)
37701300Sgjelinek 		(void) unlink(outfile);
37711300Sgjelinek 
37721300Sgjelinek 	return (Z_OK);
37731300Sgjelinek }
37741300Sgjelinek 
37751300Sgjelinek static int
37762712Snn35248 zone_postclone(char *zonepath)
37771300Sgjelinek {
37782712Snn35248 	char cmdbuf[MAXPATHLEN];
37792712Snn35248 	int status;
37802727Sedp 	brand_handle_t bh;
37812712Snn35248 	int err = Z_OK;
37821676Sjpk 
37831676Sjpk 	/*
37842712Snn35248 	 * Fetch the post-clone command, if any, from the brand
37852712Snn35248 	 * configuration.
37861568Sgjelinek 	 */
37872727Sedp 	if ((bh = brand_open(target_brand)) == NULL) {
37882712Snn35248 		zerror(gettext("missing or invalid brand"));
37891568Sgjelinek 		return (Z_ERR);
37901568Sgjelinek 	}
37912712Snn35248 	(void) strcpy(cmdbuf, EXEC_PREFIX);
37922727Sedp 	err = brand_get_postclone(bh, target_zone, zonepath, cmdbuf + EXEC_LEN,
37932712Snn35248 	    sizeof (cmdbuf) - EXEC_LEN, 0, NULL);
37942727Sedp 	brand_close(bh);
37952712Snn35248 
37962712Snn35248 	if (err == 0 && strlen(cmdbuf) > EXEC_LEN) {
37972712Snn35248 		status = do_subproc(cmdbuf);
37982712Snn35248 		if ((err = subproc_status("postclone", status, B_FALSE))
37992712Snn35248 		    != ZONE_SUBPROC_OK) {
38002712Snn35248 			zerror(gettext("post-clone configuration failed."));
38012712Snn35248 			err = Z_ERR;
38022712Snn35248 		}
38032712Snn35248 	}
38042712Snn35248 
38052712Snn35248 	return (err);
38061300Sgjelinek }
38071300Sgjelinek 
38081300Sgjelinek /* ARGSUSED */
38091867Sgjelinek static int
38101300Sgjelinek zfm_print(const char *p, void *r) {
38111300Sgjelinek 	zerror("  %s\n", p);
38121300Sgjelinek 	return (0);
38131300Sgjelinek }
38141300Sgjelinek 
38151867Sgjelinek int
38161867Sgjelinek clone_copy(char *source_zonepath, char *zonepath)
38171867Sgjelinek {
38181867Sgjelinek 	int err;
38191867Sgjelinek 
38201867Sgjelinek 	/* Don't clone the zone if anything is still mounted there */
38211867Sgjelinek 	if (zonecfg_find_mounts(source_zonepath, NULL, NULL)) {
38221867Sgjelinek 		zerror(gettext("These file systems are mounted on "
38231867Sgjelinek 		    "subdirectories of %s.\n"), source_zonepath);
38241867Sgjelinek 		(void) zonecfg_find_mounts(source_zonepath, zfm_print, NULL);
38251867Sgjelinek 		return (Z_ERR);
38261867Sgjelinek 	}
38271867Sgjelinek 
38281867Sgjelinek 	/*
38291867Sgjelinek 	 * Attempt to create a ZFS fs for the zonepath.  As usual, we don't
38301867Sgjelinek 	 * care if this works or not since we always have the default behavior
38311867Sgjelinek 	 * of a simple directory for the zonepath.
38321867Sgjelinek 	 */
38331867Sgjelinek 	create_zfs_zonepath(zonepath);
38341867Sgjelinek 
38351867Sgjelinek 	(void) printf(gettext("Copying %s..."), source_zonepath);
38361867Sgjelinek 	(void) fflush(stdout);
38371867Sgjelinek 
38381867Sgjelinek 	err = copy_zone(source_zonepath, zonepath);
38391867Sgjelinek 
38401867Sgjelinek 	(void) printf("\n");
38411867Sgjelinek 
38421867Sgjelinek 	return (err);
38431867Sgjelinek }
38441867Sgjelinek 
38451300Sgjelinek static int
38461300Sgjelinek clone_func(int argc, char *argv[])
38471300Sgjelinek {
38481300Sgjelinek 	char *source_zone = NULL;
38491300Sgjelinek 	int lockfd;
38501300Sgjelinek 	int err, arg;
38511300Sgjelinek 	char zonepath[MAXPATHLEN];
38521300Sgjelinek 	char source_zonepath[MAXPATHLEN];
38531300Sgjelinek 	zone_state_t state;
38541300Sgjelinek 	zone_entry_t *zent;
38551867Sgjelinek 	char *method = NULL;
38561867Sgjelinek 	char *snapshot = NULL;
38571300Sgjelinek 
38581300Sgjelinek 	if (zonecfg_in_alt_root()) {
38591300Sgjelinek 		zerror(gettext("cannot clone zone in alternate root"));
38601300Sgjelinek 		return (Z_ERR);
38611300Sgjelinek 	}
38621300Sgjelinek 
38631300Sgjelinek 	optind = 0;
38641867Sgjelinek 	if ((arg = getopt(argc, argv, "?m:s:")) != EOF) {
38651300Sgjelinek 		switch (arg) {
38661300Sgjelinek 		case '?':
38671300Sgjelinek 			sub_usage(SHELP_CLONE, CMD_CLONE);
38681300Sgjelinek 			return (optopt == '?' ? Z_OK : Z_USAGE);
38691300Sgjelinek 		case 'm':
38701300Sgjelinek 			method = optarg;
38711300Sgjelinek 			break;
38721867Sgjelinek 		case 's':
38731867Sgjelinek 			snapshot = optarg;
38741867Sgjelinek 			break;
38751300Sgjelinek 		default:
38761300Sgjelinek 			sub_usage(SHELP_CLONE, CMD_CLONE);
38771300Sgjelinek 			return (Z_USAGE);
38781300Sgjelinek 		}
38791300Sgjelinek 	}
38801867Sgjelinek 	if (argc != (optind + 1) ||
38811867Sgjelinek 	    (method != NULL && strcmp(method, "copy") != 0)) {
38821300Sgjelinek 		sub_usage(SHELP_CLONE, CMD_CLONE);
38831300Sgjelinek 		return (Z_USAGE);
38841300Sgjelinek 	}
38851300Sgjelinek 	source_zone = argv[optind];
38862712Snn35248 	if (sanity_check(target_zone, CMD_CLONE, B_FALSE, B_TRUE, B_FALSE)
38872712Snn35248 	    != Z_OK)
38881300Sgjelinek 		return (Z_ERR);
38893339Szt129084 	if (verify_details(CMD_CLONE, argv) != Z_OK)
38901300Sgjelinek 		return (Z_ERR);
38911300Sgjelinek 
38921300Sgjelinek 	/*
38931300Sgjelinek 	 * We also need to do some extra validation on the source zone.
38941300Sgjelinek 	 */
38951300Sgjelinek 	if (strcmp(source_zone, GLOBAL_ZONENAME) == 0) {
38961300Sgjelinek 		zerror(gettext("%s operation is invalid for the global zone."),
38971300Sgjelinek 		    cmd_to_str(CMD_CLONE));
38981300Sgjelinek 		return (Z_ERR);
38991300Sgjelinek 	}
39001300Sgjelinek 
39011300Sgjelinek 	if (strncmp(source_zone, "SUNW", 4) == 0) {
39021300Sgjelinek 		zerror(gettext("%s operation is invalid for zones starting "
39031300Sgjelinek 		    "with SUNW."), cmd_to_str(CMD_CLONE));
39041300Sgjelinek 		return (Z_ERR);
39051300Sgjelinek 	}
39061300Sgjelinek 
39071300Sgjelinek 	zent = lookup_running_zone(source_zone);
39081300Sgjelinek 	if (zent != NULL) {
39091300Sgjelinek 		/* check whether the zone is ready or running */
39101300Sgjelinek 		if ((err = zone_get_state(zent->zname, &zent->zstate_num))
39111300Sgjelinek 		    != Z_OK) {
39121300Sgjelinek 			errno = err;
39131300Sgjelinek 			zperror2(zent->zname, gettext("could not get state"));
39141300Sgjelinek 			/* can't tell, so hedge */
39151300Sgjelinek 			zent->zstate_str = "ready/running";
39161300Sgjelinek 		} else {
39171300Sgjelinek 			zent->zstate_str = zone_state_str(zent->zstate_num);
39181300Sgjelinek 		}
39191300Sgjelinek 		zerror(gettext("%s operation is invalid for %s zones."),
39201300Sgjelinek 		    cmd_to_str(CMD_CLONE), zent->zstate_str);
39211300Sgjelinek 		return (Z_ERR);
39221300Sgjelinek 	}
39231300Sgjelinek 
39241300Sgjelinek 	if ((err = zone_get_state(source_zone, &state)) != Z_OK) {
39251300Sgjelinek 		errno = err;
39261300Sgjelinek 		zperror2(source_zone, gettext("could not get state"));
39271300Sgjelinek 		return (Z_ERR);
39281300Sgjelinek 	}
39291300Sgjelinek 	if (state != ZONE_STATE_INSTALLED) {
39301300Sgjelinek 		(void) fprintf(stderr,
39311300Sgjelinek 		    gettext("%s: zone %s is %s; %s is required.\n"),
39321300Sgjelinek 		    execname, source_zone, zone_state_str(state),
39331300Sgjelinek 		    zone_state_str(ZONE_STATE_INSTALLED));
39341300Sgjelinek 		return (Z_ERR);
39351300Sgjelinek 	}
39361300Sgjelinek 
39371300Sgjelinek 	/*
39381300Sgjelinek 	 * The source zone checks out ok, continue with the clone.
39391300Sgjelinek 	 */
39401300Sgjelinek 
39411300Sgjelinek 	if (validate_clone(source_zone, target_zone) != Z_OK)
39421300Sgjelinek 		return (Z_ERR);
39431300Sgjelinek 
39441300Sgjelinek 	if (grab_lock_file(target_zone, &lockfd) != Z_OK) {
39451300Sgjelinek 		zerror(gettext("another %s may have an operation in progress."),
39461300Sgjelinek 		    "zoneadm");
39471300Sgjelinek 		return (Z_ERR);
39481300Sgjelinek 	}
39491300Sgjelinek 
39501300Sgjelinek 	if ((err = zone_get_zonepath(source_zone, source_zonepath,
39511300Sgjelinek 	    sizeof (source_zonepath))) != Z_OK) {
39521300Sgjelinek 		errno = err;
39531300Sgjelinek 		zperror2(source_zone, gettext("could not get zone path"));
39541300Sgjelinek 		goto done;
39551300Sgjelinek 	}
39561300Sgjelinek 
39571300Sgjelinek 	if ((err = zone_get_zonepath(target_zone, zonepath, sizeof (zonepath)))
39581300Sgjelinek 	    != Z_OK) {
39591300Sgjelinek 		errno = err;
39601300Sgjelinek 		zperror2(target_zone, gettext("could not get zone path"));
39611300Sgjelinek 		goto done;
39621300Sgjelinek 	}
39631300Sgjelinek 
39641300Sgjelinek 	if ((err = zone_set_state(target_zone, ZONE_STATE_INCOMPLETE))
39651300Sgjelinek 	    != Z_OK) {
39661300Sgjelinek 		errno = err;
39671300Sgjelinek 		zperror2(target_zone, gettext("could not set state"));
39681300Sgjelinek 		goto done;
39691300Sgjelinek 	}
39701300Sgjelinek 
39711867Sgjelinek 	if (snapshot != NULL) {
39721867Sgjelinek 		err = clone_snapshot_zfs(snapshot, zonepath);
39731867Sgjelinek 	} else {
39741867Sgjelinek 		/*
39751867Sgjelinek 		 * We always copy the clone unless the source is ZFS and a
39761867Sgjelinek 		 * ZFS clone worked.  We fallback to copying if the ZFS clone
39771867Sgjelinek 		 * fails for some reason.
39781867Sgjelinek 		 */
39791867Sgjelinek 		err = Z_ERR;
39801867Sgjelinek 		if (method == NULL && is_zonepath_zfs(source_zonepath))
39811867Sgjelinek 			err = clone_zfs(source_zone, source_zonepath, zonepath);
39821867Sgjelinek 
39831867Sgjelinek 		if (err != Z_OK)
39841867Sgjelinek 			err = clone_copy(source_zonepath, zonepath);
39851867Sgjelinek 	}
39861867Sgjelinek 
39872712Snn35248 	/*
39882712Snn35248 	 * Trusted Extensions requires that cloned zones use the same sysid
39892712Snn35248 	 * configuration, so it is not appropriate to perform any
39902712Snn35248 	 * post-clone reconfiguration.
39912712Snn35248 	 */
39922712Snn35248 	if ((err == Z_OK) && !is_system_labeled())
39932712Snn35248 		err = zone_postclone(zonepath);
39941300Sgjelinek 
39951300Sgjelinek done:
39962712Snn35248 	/*
39972712Snn35248 	 * If everything went well, we mark the zone as installed.
39982712Snn35248 	 */
39992712Snn35248 	if (err == Z_OK) {
40002712Snn35248 		err = zone_set_state(target_zone, ZONE_STATE_INSTALLED);
40012712Snn35248 		if (err != Z_OK) {
40022712Snn35248 			errno = err;
40032712Snn35248 			zperror2(target_zone, gettext("could not set state"));
40042712Snn35248 		}
40052712Snn35248 	}
40061300Sgjelinek 	release_lock_file(lockfd);
40071300Sgjelinek 	return ((err == Z_OK) ? Z_OK : Z_ERR);
40081300Sgjelinek }
40091300Sgjelinek 
40101607Sgjelinek /*
40111867Sgjelinek  * Used when removing a zonepath after uninstalling or cleaning up after
40121867Sgjelinek  * the move subcommand.  This handles a zonepath that has non-standard
40131867Sgjelinek  * contents so that we will only cleanup the stuff we know about and leave
40141867Sgjelinek  * any user data alone.
40151607Sgjelinek  *
40161867Sgjelinek  * If the "all" parameter is true then we should remove the whole zonepath
40171867Sgjelinek  * even if it has non-standard files/directories in it.  This can be used when
40181867Sgjelinek  * we need to cleanup after moving the zonepath across file systems.
40191867Sgjelinek  *
40201867Sgjelinek  * We "exec" the RMCOMMAND so that the returned status is that of RMCOMMAND
40211867Sgjelinek  * and not the shell.
40221607Sgjelinek  */
40231607Sgjelinek static int
40241867Sgjelinek cleanup_zonepath(char *zonepath, boolean_t all)
40251607Sgjelinek {
40261867Sgjelinek 	int		status;
40271867Sgjelinek 	int		i;
40281867Sgjelinek 	boolean_t	non_std = B_FALSE;
40291867Sgjelinek 	struct dirent	*dp;
40301867Sgjelinek 	DIR		*dirp;
40311867Sgjelinek 	char		*std_entries[] = {"dev", "lu", "root", NULL};
40321867Sgjelinek 			/* (MAXPATHLEN * 3) is for the 3 std_entries dirs */
40331867Sgjelinek 	char		cmdbuf[sizeof (RMCOMMAND) + (MAXPATHLEN * 3) + 64];
40341867Sgjelinek 
40351867Sgjelinek 	/*
40361867Sgjelinek 	 * We shouldn't need these checks but lets be paranoid since we
40371867Sgjelinek 	 * could blow away the whole system here if we got the wrong zonepath.
40381867Sgjelinek 	 */
40391867Sgjelinek 	if (*zonepath == NULL || strcmp(zonepath, "/") == 0) {
40401867Sgjelinek 		(void) fprintf(stderr, "invalid zonepath '%s'\n", zonepath);
40411867Sgjelinek 		return (Z_INVAL);
40421867Sgjelinek 	}
40431867Sgjelinek 
40441867Sgjelinek 	/*
40451867Sgjelinek 	 * If the dirpath is already gone (maybe it was manually removed) then
40461867Sgjelinek 	 * we just return Z_OK so that the cleanup is successful.
40471867Sgjelinek 	 */
40481867Sgjelinek 	if ((dirp = opendir(zonepath)) == NULL)
40491867Sgjelinek 		return (Z_OK);
40501867Sgjelinek 
40511867Sgjelinek 	/*
40521867Sgjelinek 	 * Look through the zonepath directory to see if there are any
40531867Sgjelinek 	 * non-standard files/dirs.  Also skip .zfs since that might be
40541867Sgjelinek 	 * there but we'll handle ZFS file systems as a special case.
40551867Sgjelinek 	 */
40561867Sgjelinek 	while ((dp = readdir(dirp)) != NULL) {
40571867Sgjelinek 		if (strcmp(dp->d_name, ".") == 0 ||
40581867Sgjelinek 		    strcmp(dp->d_name, "..") == 0 ||
40591867Sgjelinek 		    strcmp(dp->d_name, ".zfs") == 0)
40601867Sgjelinek 			continue;
40611867Sgjelinek 
40621867Sgjelinek 		for (i = 0; std_entries[i] != NULL; i++)
40631867Sgjelinek 			if (strcmp(dp->d_name, std_entries[i]) == 0)
40641867Sgjelinek 				break;
40651867Sgjelinek 
40661867Sgjelinek 		if (std_entries[i] == NULL)
40671867Sgjelinek 			non_std = B_TRUE;
40681867Sgjelinek 	}
40691867Sgjelinek 	(void) closedir(dirp);
40701867Sgjelinek 
40711867Sgjelinek 	if (!all && non_std) {
40721607Sgjelinek 		/*
40731867Sgjelinek 		 * There are extra, non-standard directories/files in the
40741867Sgjelinek 		 * zonepath so we don't want to remove the zonepath.  We
40751867Sgjelinek 		 * just want to remove the standard directories and leave
40761867Sgjelinek 		 * the user data alone.
40771607Sgjelinek 		 */
40781867Sgjelinek 		(void) snprintf(cmdbuf, sizeof (cmdbuf), "exec " RMCOMMAND);
40791867Sgjelinek 
40801867Sgjelinek 		for (i = 0; std_entries[i] != NULL; i++) {
40811867Sgjelinek 			char tmpbuf[MAXPATHLEN];
40821867Sgjelinek 
40831867Sgjelinek 			if (snprintf(tmpbuf, sizeof (tmpbuf), " %s/%s",
40841867Sgjelinek 			    zonepath, std_entries[i]) >= sizeof (tmpbuf) ||
40851867Sgjelinek 			    strlcat(cmdbuf, tmpbuf, sizeof (cmdbuf)) >=
40861867Sgjelinek 			    sizeof (cmdbuf)) {
40871867Sgjelinek 				(void) fprintf(stderr,
40881867Sgjelinek 				    gettext("path is too long\n"));
40891867Sgjelinek 				return (Z_INVAL);
40901867Sgjelinek 			}
40911867Sgjelinek 		}
40921867Sgjelinek 
40931867Sgjelinek 		status = do_subproc(cmdbuf);
40941867Sgjelinek 
40951867Sgjelinek 		(void) fprintf(stderr, gettext("WARNING: Unable to completely "
40961867Sgjelinek 		    "remove %s\nbecause it contains additional user data.  "
40971867Sgjelinek 		    "Only the standard directory\nentries have been "
40981867Sgjelinek 		    "removed.\n"),
40991867Sgjelinek 		    zonepath);
41001867Sgjelinek 
41012712Snn35248 		return ((subproc_status(RMCOMMAND, status, B_TRUE) ==
41022712Snn35248 		    ZONE_SUBPROC_OK) ? Z_OK : Z_ERR);
41031607Sgjelinek 	}
41041607Sgjelinek 
41051867Sgjelinek 	/*
41061867Sgjelinek 	 * There is nothing unexpected in the zonepath, try to get rid of the
41071867Sgjelinek 	 * whole zonepath directory.
41081867Sgjelinek 	 *
41091867Sgjelinek 	 * If the zonepath is its own zfs file system, try to destroy the
41101867Sgjelinek 	 * file system.  If that fails for some reason (e.g. it has clones)
41111867Sgjelinek 	 * then we'll just remove the contents of the zonepath.
41121867Sgjelinek 	 */
41131867Sgjelinek 	if (is_zonepath_zfs(zonepath)) {
41141867Sgjelinek 		if (destroy_zfs(zonepath) == Z_OK)
41151867Sgjelinek 			return (Z_OK);
41161867Sgjelinek 		(void) snprintf(cmdbuf, sizeof (cmdbuf), "exec " RMCOMMAND
41171867Sgjelinek 		    " %s/*", zonepath);
41181867Sgjelinek 		status = do_subproc(cmdbuf);
41192712Snn35248 		return ((subproc_status(RMCOMMAND, status, B_TRUE) ==
41202712Snn35248 		    ZONE_SUBPROC_OK) ? Z_OK : Z_ERR);
41211867Sgjelinek 	}
41221867Sgjelinek 
41231867Sgjelinek 	(void) snprintf(cmdbuf, sizeof (cmdbuf), "exec " RMCOMMAND " %s",
41241867Sgjelinek 	    zonepath);
41251607Sgjelinek 	status = do_subproc(cmdbuf);
41262712Snn35248 
41272712Snn35248 	return ((subproc_status(RMCOMMAND, status, B_TRUE) == ZONE_SUBPROC_OK)
41282712Snn35248 	    ? Z_OK : Z_ERR);
41291607Sgjelinek }
41301607Sgjelinek 
41311300Sgjelinek static int
41321300Sgjelinek move_func(int argc, char *argv[])
41331300Sgjelinek {
41341300Sgjelinek 	char *new_zonepath = NULL;
41351300Sgjelinek 	int lockfd;
41361300Sgjelinek 	int err, arg;
41371300Sgjelinek 	char zonepath[MAXPATHLEN];
41381300Sgjelinek 	zone_dochandle_t handle;
41391300Sgjelinek 	boolean_t fast;
41401867Sgjelinek 	boolean_t is_zfs = B_FALSE;
41411867Sgjelinek 	struct dirent *dp;
41421867Sgjelinek 	DIR *dirp;
41431867Sgjelinek 	boolean_t empty = B_TRUE;
41441300Sgjelinek 	boolean_t revert;
41451300Sgjelinek 	struct stat zonepath_buf;
41461300Sgjelinek 	struct stat new_zonepath_buf;
41471300Sgjelinek 
41481300Sgjelinek 	if (zonecfg_in_alt_root()) {
41491300Sgjelinek 		zerror(gettext("cannot move zone in alternate root"));
41501300Sgjelinek 		return (Z_ERR);
41511300Sgjelinek 	}
41521300Sgjelinek 
41531300Sgjelinek 	optind = 0;
41541300Sgjelinek 	if ((arg = getopt(argc, argv, "?")) != EOF) {
41551300Sgjelinek 		switch (arg) {
41561300Sgjelinek 		case '?':
41571300Sgjelinek 			sub_usage(SHELP_MOVE, CMD_MOVE);
41581300Sgjelinek 			return (optopt == '?' ? Z_OK : Z_USAGE);
41591300Sgjelinek 		default:
41601300Sgjelinek 			sub_usage(SHELP_MOVE, CMD_MOVE);
41611300Sgjelinek 			return (Z_USAGE);
41621300Sgjelinek 		}
41631300Sgjelinek 	}
41641300Sgjelinek 	if (argc != (optind + 1)) {
41651300Sgjelinek 		sub_usage(SHELP_MOVE, CMD_MOVE);
41661300Sgjelinek 		return (Z_USAGE);
41671300Sgjelinek 	}
41681300Sgjelinek 	new_zonepath = argv[optind];
41692712Snn35248 	if (sanity_check(target_zone, CMD_MOVE, B_FALSE, B_TRUE, B_FALSE)
41702712Snn35248 	    != Z_OK)
41711300Sgjelinek 		return (Z_ERR);
41723339Szt129084 	if (verify_details(CMD_MOVE, argv) != Z_OK)
41731300Sgjelinek 		return (Z_ERR);
41741300Sgjelinek 
41751300Sgjelinek 	/*
41761300Sgjelinek 	 * Check out the new zonepath.  This has the side effect of creating
41771300Sgjelinek 	 * a directory for the new zonepath.  We depend on this later when we
41781867Sgjelinek 	 * stat to see if we are doing a cross file system move or not.
41791300Sgjelinek 	 */
41801300Sgjelinek 	if (validate_zonepath(new_zonepath, CMD_MOVE) != Z_OK)
41811300Sgjelinek 		return (Z_ERR);
41821300Sgjelinek 
41831300Sgjelinek 	if ((err = zone_get_zonepath(target_zone, zonepath, sizeof (zonepath)))
41841300Sgjelinek 	    != Z_OK) {
41851300Sgjelinek 		errno = err;
41861300Sgjelinek 		zperror2(target_zone, gettext("could not get zone path"));
41871300Sgjelinek 		return (Z_ERR);
41881300Sgjelinek 	}
41891300Sgjelinek 
41901300Sgjelinek 	if (stat(zonepath, &zonepath_buf) == -1) {
41911300Sgjelinek 		zperror(gettext("could not stat zone path"), B_FALSE);
41921300Sgjelinek 		return (Z_ERR);
41931300Sgjelinek 	}
41941300Sgjelinek 
41951300Sgjelinek 	if (stat(new_zonepath, &new_zonepath_buf) == -1) {
41961300Sgjelinek 		zperror(gettext("could not stat new zone path"), B_FALSE);
41971300Sgjelinek 		return (Z_ERR);
41981300Sgjelinek 	}
41991300Sgjelinek 
42001867Sgjelinek 	/*
42011867Sgjelinek 	 * Check if the destination directory is empty.
42021867Sgjelinek 	 */
42031867Sgjelinek 	if ((dirp = opendir(new_zonepath)) == NULL) {
42041867Sgjelinek 		zperror(gettext("could not open new zone path"), B_FALSE);
42051867Sgjelinek 		return (Z_ERR);
42061867Sgjelinek 	}
42071867Sgjelinek 	while ((dp = readdir(dirp)) != (struct dirent *)0) {
42081867Sgjelinek 		if (strcmp(dp->d_name, ".") == 0 ||
42091867Sgjelinek 		    strcmp(dp->d_name, "..") == 0)
42101867Sgjelinek 			continue;
42111867Sgjelinek 		empty = B_FALSE;
42121867Sgjelinek 		break;
42131867Sgjelinek 	}
42141867Sgjelinek 	(void) closedir(dirp);
42151867Sgjelinek 
42161867Sgjelinek 	/* Error if there is anything in the destination directory. */
42171867Sgjelinek 	if (!empty) {
42181867Sgjelinek 		(void) fprintf(stderr, gettext("could not move zone to %s: "
42191867Sgjelinek 		    "directory not empty\n"), new_zonepath);
42201867Sgjelinek 		return (Z_ERR);
42211867Sgjelinek 	}
42221867Sgjelinek 
42231300Sgjelinek 	/* Don't move the zone if anything is still mounted there */
42241300Sgjelinek 	if (zonecfg_find_mounts(zonepath, NULL, NULL)) {
42251867Sgjelinek 		zerror(gettext("These file systems are mounted on "
42261300Sgjelinek 		    "subdirectories of %s.\n"), zonepath);
42271300Sgjelinek 		(void) zonecfg_find_mounts(zonepath, zfm_print, NULL);
42281300Sgjelinek 		return (Z_ERR);
42291300Sgjelinek 	}
42301300Sgjelinek 
42311300Sgjelinek 	/*
42321867Sgjelinek 	 * Check if we are moving in the same file system and can do a fast
42331867Sgjelinek 	 * move or if we are crossing file systems and have to copy the data.
42341300Sgjelinek 	 */
42351300Sgjelinek 	fast = (zonepath_buf.st_dev == new_zonepath_buf.st_dev);
42361300Sgjelinek 
42371300Sgjelinek 	if ((handle = zonecfg_init_handle()) == NULL) {
42381300Sgjelinek 		zperror(cmd_to_str(CMD_MOVE), B_TRUE);
42391300Sgjelinek 		return (Z_ERR);
42401300Sgjelinek 	}
42411300Sgjelinek 
42421300Sgjelinek 	if ((err = zonecfg_get_handle(target_zone, handle)) != Z_OK) {
42431300Sgjelinek 		errno = err;
42441300Sgjelinek 		zperror(cmd_to_str(CMD_MOVE), B_TRUE);
42451300Sgjelinek 		zonecfg_fini_handle(handle);
42461300Sgjelinek 		return (Z_ERR);
42471300Sgjelinek 	}
42481300Sgjelinek 
42491300Sgjelinek 	if (grab_lock_file(target_zone, &lockfd) != Z_OK) {
42501300Sgjelinek 		zerror(gettext("another %s may have an operation in progress."),
42511300Sgjelinek 		    "zoneadm");
42521300Sgjelinek 		zonecfg_fini_handle(handle);
42531300Sgjelinek 		return (Z_ERR);
42541300Sgjelinek 	}
42551300Sgjelinek 
42561300Sgjelinek 	/*
42571867Sgjelinek 	 * We're making some file system changes now so we have to clean up
42581867Sgjelinek 	 * the file system before we are done.  This will either clean up the
42591300Sgjelinek 	 * new zonepath if the zonecfg update failed or it will clean up the
42601300Sgjelinek 	 * old zonepath if everything is ok.
42611300Sgjelinek 	 */
42621300Sgjelinek 	revert = B_TRUE;
42631300Sgjelinek 
42641867Sgjelinek 	if (is_zonepath_zfs(zonepath) &&
42651867Sgjelinek 	    move_zfs(zonepath, new_zonepath) != Z_ERR) {
42661867Sgjelinek 		is_zfs = B_TRUE;
42671867Sgjelinek 
42681867Sgjelinek 	} else if (fast) {
42691867Sgjelinek 		/* same file system, use rename for a quick move */
42701300Sgjelinek 
42711300Sgjelinek 		/*
42721300Sgjelinek 		 * Remove the new_zonepath directory that got created above
42731300Sgjelinek 		 * during the validation.  It gets in the way of the rename.
42741300Sgjelinek 		 */
42751300Sgjelinek 		if (rmdir(new_zonepath) != 0) {
42761300Sgjelinek 			zperror(gettext("could not rmdir new zone path"),
42771300Sgjelinek 			    B_FALSE);
42781300Sgjelinek 			zonecfg_fini_handle(handle);
42791300Sgjelinek 			release_lock_file(lockfd);
42801300Sgjelinek 			return (Z_ERR);
42811300Sgjelinek 		}
42821300Sgjelinek 
42831300Sgjelinek 		if (rename(zonepath, new_zonepath) != 0) {
42841300Sgjelinek 			/*
42851300Sgjelinek 			 * If this fails we don't need to do all of the
42861300Sgjelinek 			 * cleanup that happens for the rest of the code
42871300Sgjelinek 			 * so just return from this error.
42881300Sgjelinek 			 */
42891300Sgjelinek 			zperror(gettext("could not move zone"), B_FALSE);
42901300Sgjelinek 			zonecfg_fini_handle(handle);
42911300Sgjelinek 			release_lock_file(lockfd);
42921300Sgjelinek 			return (Z_ERR);
42931300Sgjelinek 		}
42941300Sgjelinek 
42951300Sgjelinek 	} else {
42961867Sgjelinek 		/*
42971867Sgjelinek 		 * Attempt to create a ZFS fs for the new zonepath.  As usual,
42981867Sgjelinek 		 * we don't care if this works or not since we always have the
42991867Sgjelinek 		 * default behavior of a simple directory for the zonepath.
43001867Sgjelinek 		 */
43011867Sgjelinek 		create_zfs_zonepath(new_zonepath);
43021867Sgjelinek 
43031300Sgjelinek 		(void) printf(gettext(
43041867Sgjelinek 		    "Moving across file systems; copying zonepath %s..."),
43051300Sgjelinek 		    zonepath);
43061300Sgjelinek 		(void) fflush(stdout);
43071300Sgjelinek 
43081300Sgjelinek 		err = copy_zone(zonepath, new_zonepath);
43091300Sgjelinek 
43101300Sgjelinek 		(void) printf("\n");
43111300Sgjelinek 		if (err != Z_OK)
43121300Sgjelinek 			goto done;
43131300Sgjelinek 	}
43141300Sgjelinek 
43151300Sgjelinek 	if ((err = zonecfg_set_zonepath(handle, new_zonepath)) != Z_OK) {
43161300Sgjelinek 		errno = err;
43171300Sgjelinek 		zperror(gettext("could not set new zonepath"), B_TRUE);
43181300Sgjelinek 		goto done;
43191300Sgjelinek 	}
43201300Sgjelinek 
43211300Sgjelinek 	if ((err = zonecfg_save(handle)) != Z_OK) {
43221300Sgjelinek 		errno = err;
43231300Sgjelinek 		zperror(gettext("zonecfg save failed"), B_TRUE);
43241300Sgjelinek 		goto done;
43251300Sgjelinek 	}
43261300Sgjelinek 
43271300Sgjelinek 	revert = B_FALSE;
43281300Sgjelinek 
43291300Sgjelinek done:
43301300Sgjelinek 	zonecfg_fini_handle(handle);
43311300Sgjelinek 	release_lock_file(lockfd);
43321300Sgjelinek 
43331300Sgjelinek 	/*
43341867Sgjelinek 	 * Clean up the file system based on how things went.  We either
43351300Sgjelinek 	 * clean up the new zonepath if the operation failed for some reason
43361300Sgjelinek 	 * or we clean up the old zonepath if everything is ok.
43371300Sgjelinek 	 */
43381300Sgjelinek 	if (revert) {
43391300Sgjelinek 		/* The zonecfg update failed, cleanup the new zonepath. */
43401867Sgjelinek 		if (is_zfs) {
43411867Sgjelinek 			if (move_zfs(new_zonepath, zonepath) == Z_ERR) {
43421867Sgjelinek 				(void) fprintf(stderr, gettext("could not "
43431867Sgjelinek 				    "restore zonepath, the zfs mountpoint is "
43441867Sgjelinek 				    "set as:\n%s\n"), new_zonepath);
43451867Sgjelinek 				/*
43461867Sgjelinek 				 * err is already != Z_OK since we're reverting
43471867Sgjelinek 				 */
43481867Sgjelinek 			}
43491867Sgjelinek 
43501867Sgjelinek 		} else if (fast) {
43511300Sgjelinek 			if (rename(new_zonepath, zonepath) != 0) {
43521300Sgjelinek 				zperror(gettext("could not restore zonepath"),
43531300Sgjelinek 				    B_FALSE);
43541300Sgjelinek 				/*
43551300Sgjelinek 				 * err is already != Z_OK since we're reverting
43561300Sgjelinek 				 */
43571300Sgjelinek 			}
43581300Sgjelinek 		} else {
43591300Sgjelinek 			(void) printf(gettext("Cleaning up zonepath %s..."),
43601300Sgjelinek 			    new_zonepath);
43611300Sgjelinek 			(void) fflush(stdout);
43621867Sgjelinek 			err = cleanup_zonepath(new_zonepath, B_TRUE);
43631300Sgjelinek 			(void) printf("\n");
43641300Sgjelinek 
43651607Sgjelinek 			if (err != Z_OK) {
43661300Sgjelinek 				errno = err;
43671300Sgjelinek 				zperror(gettext("could not remove new "
43681300Sgjelinek 				    "zonepath"), B_TRUE);
43691300Sgjelinek 			} else {
43701300Sgjelinek 				/*
43711300Sgjelinek 				 * Because we're reverting we know the mainline
43721300Sgjelinek 				 * code failed but we just reused the err
43731300Sgjelinek 				 * variable so we reset it back to Z_ERR.
43741300Sgjelinek 				 */
43751300Sgjelinek 				err = Z_ERR;
43761300Sgjelinek 			}
43771300Sgjelinek 		}
43781300Sgjelinek 
43791300Sgjelinek 	} else {
43801300Sgjelinek 		/* The move was successful, cleanup the old zonepath. */
43811867Sgjelinek 		if (!is_zfs && !fast) {
43821300Sgjelinek 			(void) printf(
43831300Sgjelinek 			    gettext("Cleaning up zonepath %s..."), zonepath);
43841300Sgjelinek 			(void) fflush(stdout);
43851867Sgjelinek 			err = cleanup_zonepath(zonepath, B_TRUE);
43861300Sgjelinek 			(void) printf("\n");
43871300Sgjelinek 
43881607Sgjelinek 			if (err != Z_OK) {
43891300Sgjelinek 				errno = err;
43901300Sgjelinek 				zperror(gettext("could not remove zonepath"),
43911300Sgjelinek 				    B_TRUE);
43921300Sgjelinek 			}
43931300Sgjelinek 		}
43941300Sgjelinek 	}
43951300Sgjelinek 
43961300Sgjelinek 	return ((err == Z_OK) ? Z_OK : Z_ERR);
43971300Sgjelinek }
43981300Sgjelinek 
43991507Sgjelinek static int
44001507Sgjelinek detach_func(int argc, char *argv[])
44011507Sgjelinek {
44021507Sgjelinek 	int lockfd;
44031507Sgjelinek 	int err, arg;
44041507Sgjelinek 	char zonepath[MAXPATHLEN];
44051507Sgjelinek 	zone_dochandle_t handle;
44062078Sgjelinek 	boolean_t execute = B_TRUE;
44071507Sgjelinek 
44081507Sgjelinek 	if (zonecfg_in_alt_root()) {
44091507Sgjelinek 		zerror(gettext("cannot detach zone in alternate root"));
44101507Sgjelinek 		return (Z_ERR);
44111507Sgjelinek 	}
44121507Sgjelinek 
44131507Sgjelinek 	optind = 0;
44142078Sgjelinek 	if ((arg = getopt(argc, argv, "?n")) != EOF) {
44151507Sgjelinek 		switch (arg) {
44161507Sgjelinek 		case '?':
44171507Sgjelinek 			sub_usage(SHELP_DETACH, CMD_DETACH);
44181507Sgjelinek 			return (optopt == '?' ? Z_OK : Z_USAGE);
44192078Sgjelinek 		case 'n':
44202078Sgjelinek 			execute = B_FALSE;
44212078Sgjelinek 			break;
44221507Sgjelinek 		default:
44231507Sgjelinek 			sub_usage(SHELP_DETACH, CMD_DETACH);
44241507Sgjelinek 			return (Z_USAGE);
44251507Sgjelinek 		}
44261507Sgjelinek 	}
44272712Snn35248 
44282078Sgjelinek 	if (execute) {
44292712Snn35248 		if (sanity_check(target_zone, CMD_DETACH, B_FALSE, B_TRUE,
44302712Snn35248 		    B_FALSE) != Z_OK)
44312078Sgjelinek 			return (Z_ERR);
44323339Szt129084 		if (verify_details(CMD_DETACH, argv) != Z_OK)
44332078Sgjelinek 			return (Z_ERR);
44342078Sgjelinek 	} else {
44352078Sgjelinek 		/*
44362078Sgjelinek 		 * We want a dry-run to work for a non-privileged user so we
44372078Sgjelinek 		 * only do minimal validation.
44382078Sgjelinek 		 */
44392078Sgjelinek 		if (getzoneid() != GLOBAL_ZONEID) {
44402078Sgjelinek 			zerror(gettext("must be in the global zone to %s a "
44412078Sgjelinek 			    "zone."), cmd_to_str(CMD_DETACH));
44422078Sgjelinek 			return (Z_ERR);
44432078Sgjelinek 		}
44442078Sgjelinek 
44452078Sgjelinek 		if (target_zone == NULL) {
44462078Sgjelinek 			zerror(gettext("no zone specified"));
44472078Sgjelinek 			return (Z_ERR);
44482078Sgjelinek 		}
44492078Sgjelinek 
44502078Sgjelinek 		if (strcmp(target_zone, GLOBAL_ZONENAME) == 0) {
44512078Sgjelinek 			zerror(gettext("%s operation is invalid for the "
44522078Sgjelinek 			    "global zone."), cmd_to_str(CMD_DETACH));
44532078Sgjelinek 			return (Z_ERR);
44542078Sgjelinek 		}
44552078Sgjelinek 	}
44561507Sgjelinek 
44571507Sgjelinek 	if ((err = zone_get_zonepath(target_zone, zonepath, sizeof (zonepath)))
44581507Sgjelinek 	    != Z_OK) {
44591507Sgjelinek 		errno = err;
44601507Sgjelinek 		zperror2(target_zone, gettext("could not get zone path"));
44611507Sgjelinek 		return (Z_ERR);
44621507Sgjelinek 	}
44631507Sgjelinek 
44641507Sgjelinek 	/* Don't detach the zone if anything is still mounted there */
44652078Sgjelinek 	if (execute && zonecfg_find_mounts(zonepath, NULL, NULL)) {
44661867Sgjelinek 		zerror(gettext("These file systems are mounted on "
44671507Sgjelinek 		    "subdirectories of %s.\n"), zonepath);
44681507Sgjelinek 		(void) zonecfg_find_mounts(zonepath, zfm_print, NULL);
44691507Sgjelinek 		return (Z_ERR);
44701507Sgjelinek 	}
44711507Sgjelinek 
44721507Sgjelinek 	if ((handle = zonecfg_init_handle()) == NULL) {
44731507Sgjelinek 		zperror(cmd_to_str(CMD_DETACH), B_TRUE);
44741507Sgjelinek 		return (Z_ERR);
44751507Sgjelinek 	}
44761507Sgjelinek 
44771507Sgjelinek 	if ((err = zonecfg_get_handle(target_zone, handle)) != Z_OK) {
44781507Sgjelinek 		errno = err;
44791507Sgjelinek 		zperror(cmd_to_str(CMD_DETACH), B_TRUE);
44801507Sgjelinek 		zonecfg_fini_handle(handle);
44811507Sgjelinek 		return (Z_ERR);
44821507Sgjelinek 	}
44831507Sgjelinek 
44842078Sgjelinek 	if (execute && grab_lock_file(target_zone, &lockfd) != Z_OK) {
44851507Sgjelinek 		zerror(gettext("another %s may have an operation in progress."),
44861507Sgjelinek 		    "zoneadm");
44871507Sgjelinek 		zonecfg_fini_handle(handle);
44881507Sgjelinek 		return (Z_ERR);
44891507Sgjelinek 	}
44901507Sgjelinek 
44911507Sgjelinek 	if ((err = zonecfg_get_detach_info(handle, B_TRUE)) != Z_OK) {
44921507Sgjelinek 		errno = err;
44931507Sgjelinek 		zperror(gettext("getting the detach information failed"),
44941507Sgjelinek 		    B_TRUE);
44951507Sgjelinek 		goto done;
44961507Sgjelinek 	}
44971507Sgjelinek 
44982078Sgjelinek 	if ((err = zonecfg_detach_save(handle, (execute ? 0 : ZONE_DRY_RUN)))
44992078Sgjelinek 	    != Z_OK) {
45001507Sgjelinek 		errno = err;
45011507Sgjelinek 		zperror(gettext("saving the detach manifest failed"), B_TRUE);
45021507Sgjelinek 		goto done;
45031507Sgjelinek 	}
45041507Sgjelinek 
45052078Sgjelinek 	/*
45062078Sgjelinek 	 * Set the zone state back to configured unless we are running with the
45072078Sgjelinek 	 * no-execute option.
45082078Sgjelinek 	 */
45092078Sgjelinek 	if (execute && (err = zone_set_state(target_zone,
45102078Sgjelinek 	    ZONE_STATE_CONFIGURED)) != Z_OK) {
45111507Sgjelinek 		errno = err;
45121507Sgjelinek 		zperror(gettext("could not reset state"), B_TRUE);
45131507Sgjelinek 	}
45141507Sgjelinek 
45151507Sgjelinek done:
45161507Sgjelinek 	zonecfg_fini_handle(handle);
45172078Sgjelinek 	if (execute)
45182078Sgjelinek 		release_lock_file(lockfd);
45191507Sgjelinek 
45201507Sgjelinek 	return ((err == Z_OK) ? Z_OK : Z_ERR);
45211507Sgjelinek }
45221507Sgjelinek 
45231507Sgjelinek /*
45241507Sgjelinek  * During attach we go through and fix up the /dev entries for the zone
45251507Sgjelinek  * we are attaching.  In order to regenerate /dev with the correct devices,
45261507Sgjelinek  * the old /dev will be removed, the zone readied (which generates a new
45271507Sgjelinek  * /dev) then halted, then we use the info from the manifest to update
45281507Sgjelinek  * the modes, owners, etc. on the new /dev.
45291507Sgjelinek  */
45301507Sgjelinek static int
45311507Sgjelinek dev_fix(zone_dochandle_t handle)
45321507Sgjelinek {
45331507Sgjelinek 	int			res;
45341507Sgjelinek 	int			err;
45351507Sgjelinek 	int			status;
45361507Sgjelinek 	struct zone_devpermtab	devtab;
45371507Sgjelinek 	zone_cmd_arg_t		zarg;
45381507Sgjelinek 	char			devpath[MAXPATHLEN];
45391507Sgjelinek 				/* 6: "exec " and " " */
45401507Sgjelinek 	char			cmdbuf[sizeof (RMCOMMAND) + MAXPATHLEN + 6];
45411507Sgjelinek 
45421507Sgjelinek 	if ((res = zonecfg_get_zonepath(handle, devpath, sizeof (devpath)))
45431507Sgjelinek 	    != Z_OK)
45441507Sgjelinek 		return (res);
45451507Sgjelinek 
45461507Sgjelinek 	if (strlcat(devpath, "/dev", sizeof (devpath)) >= sizeof (devpath))
45471507Sgjelinek 		return (Z_TOO_BIG);
45481507Sgjelinek 
45491507Sgjelinek 	/*
45501507Sgjelinek 	 * "exec" the command so that the returned status is that of
45511507Sgjelinek 	 * RMCOMMAND and not the shell.
45521507Sgjelinek 	 */
45532712Snn35248 	(void) snprintf(cmdbuf, sizeof (cmdbuf), EXEC_PREFIX RMCOMMAND " %s",
45541507Sgjelinek 	    devpath);
45551507Sgjelinek 	status = do_subproc(cmdbuf);
45562712Snn35248 	if ((err = subproc_status(RMCOMMAND, status, B_TRUE)) !=
45572712Snn35248 	    ZONE_SUBPROC_OK) {
45581507Sgjelinek 		(void) fprintf(stderr,
45591507Sgjelinek 		    gettext("could not remove existing /dev\n"));
45601507Sgjelinek 		return (Z_ERR);
45611507Sgjelinek 	}
45621507Sgjelinek 
45631507Sgjelinek 	/* In order to ready the zone, it must be in the installed state */
45641507Sgjelinek 	if ((err = zone_set_state(target_zone, ZONE_STATE_INSTALLED)) != Z_OK) {
45651507Sgjelinek 		errno = err;
45661507Sgjelinek 		zperror(gettext("could not reset state"), B_TRUE);
45671507Sgjelinek 		return (Z_ERR);
45681507Sgjelinek 	}
45691507Sgjelinek 
45701507Sgjelinek 	/* We have to ready the zone to regen the dev tree */
45711507Sgjelinek 	zarg.cmd = Z_READY;
45721507Sgjelinek 	if (call_zoneadmd(target_zone, &zarg) != 0) {
45731507Sgjelinek 		zerror(gettext("call to %s failed"), "zoneadmd");
45743247Sgjelinek 		/* attempt to restore zone to configured state */
45753247Sgjelinek 		(void) zone_set_state(target_zone, ZONE_STATE_CONFIGURED);
45761507Sgjelinek 		return (Z_ERR);
45771507Sgjelinek 	}
45781507Sgjelinek 
45791507Sgjelinek 	zarg.cmd = Z_HALT;
45801507Sgjelinek 	if (call_zoneadmd(target_zone, &zarg) != 0) {
45811507Sgjelinek 		zerror(gettext("call to %s failed"), "zoneadmd");
45823247Sgjelinek 		/* attempt to restore zone to configured state */
45833247Sgjelinek 		(void) zone_set_state(target_zone, ZONE_STATE_CONFIGURED);
45841507Sgjelinek 		return (Z_ERR);
45851507Sgjelinek 	}
45861507Sgjelinek 
45873247Sgjelinek 	/* attempt to restore zone to configured state */
45883247Sgjelinek 	(void) zone_set_state(target_zone, ZONE_STATE_CONFIGURED);
45893247Sgjelinek 
45901507Sgjelinek 	if (zonecfg_setdevperment(handle) != Z_OK) {
45911507Sgjelinek 		(void) fprintf(stderr,
45921507Sgjelinek 		    gettext("unable to enumerate device entries\n"));
45931507Sgjelinek 		return (Z_ERR);
45941507Sgjelinek 	}
45951507Sgjelinek 
45961507Sgjelinek 	while (zonecfg_getdevperment(handle, &devtab) == Z_OK) {
45971507Sgjelinek 		int err;
45981507Sgjelinek 
45991507Sgjelinek 		if ((err = zonecfg_devperms_apply(handle,
46001507Sgjelinek 		    devtab.zone_devperm_name, devtab.zone_devperm_uid,
46011507Sgjelinek 		    devtab.zone_devperm_gid, devtab.zone_devperm_mode,
46021507Sgjelinek 		    devtab.zone_devperm_acl)) != Z_OK && err != Z_INVAL)
46031507Sgjelinek 			(void) fprintf(stderr, gettext("error updating device "
46041507Sgjelinek 			    "%s: %s\n"), devtab.zone_devperm_name,
46051507Sgjelinek 			    zonecfg_strerror(err));
46061507Sgjelinek 
46071507Sgjelinek 		free(devtab.zone_devperm_acl);
46081507Sgjelinek 	}
46091507Sgjelinek 
46101507Sgjelinek 	(void) zonecfg_enddevperment(handle);
46111507Sgjelinek 
46121507Sgjelinek 	return (Z_OK);
46131507Sgjelinek }
46141507Sgjelinek 
46152078Sgjelinek /*
46162078Sgjelinek  * Validate attaching a zone but don't actually do the work.  The zone
46172078Sgjelinek  * does not have to exist, so there is some complexity getting a new zone
46182078Sgjelinek  * configuration set up so that we can perform the validation.  This is
46192078Sgjelinek  * handled within zonecfg_attach_manifest() which returns two handles; one
46202078Sgjelinek  * for the the full configuration to validate (rem_handle) and the other
46212078Sgjelinek  * (local_handle) containing only the zone configuration derived from the
46222078Sgjelinek  * manifest.
46232078Sgjelinek  */
46242078Sgjelinek static int
46253339Szt129084 dryrun_attach(char *manifest_path, char *argv[])
46262078Sgjelinek {
46272078Sgjelinek 	int fd;
46282078Sgjelinek 	int err;
46292078Sgjelinek 	int res;
46302078Sgjelinek 	zone_dochandle_t local_handle;
46312078Sgjelinek 	zone_dochandle_t rem_handle = NULL;
46322078Sgjelinek 
46332078Sgjelinek 	if (strcmp(manifest_path, "-") == 0) {
46342078Sgjelinek 		fd = 0;
46352078Sgjelinek 	} else if ((fd = open(manifest_path, O_RDONLY)) < 0) {
46362078Sgjelinek 		zperror(gettext("could not open manifest path"), B_FALSE);
46372078Sgjelinek 		return (Z_ERR);
46382078Sgjelinek 	}
46392078Sgjelinek 
46402078Sgjelinek 	if ((local_handle = zonecfg_init_handle()) == NULL) {
46412078Sgjelinek 		zperror(cmd_to_str(CMD_ATTACH), B_TRUE);
46422078Sgjelinek 		res = Z_ERR;
46432078Sgjelinek 		goto done;
46442078Sgjelinek 	}
46452078Sgjelinek 
46462078Sgjelinek 	if ((rem_handle = zonecfg_init_handle()) == NULL) {
46472078Sgjelinek 		zperror(cmd_to_str(CMD_ATTACH), B_TRUE);
46482078Sgjelinek 		res = Z_ERR;
46492078Sgjelinek 		goto done;
46502078Sgjelinek 	}
46512078Sgjelinek 
46522078Sgjelinek 	if ((err = zonecfg_attach_manifest(fd, local_handle, rem_handle))
46532078Sgjelinek 	    != Z_OK) {
46542078Sgjelinek 		if (err == Z_INVALID_DOCUMENT)
46552078Sgjelinek 			zerror(gettext("Cannot attach to an earlier release "
46562078Sgjelinek 			    "of the operating system"));
46572078Sgjelinek 		else
46582078Sgjelinek 			zperror(cmd_to_str(CMD_ATTACH), B_TRUE);
46592078Sgjelinek 		res = Z_ERR;
46602078Sgjelinek 		goto done;
46612078Sgjelinek 	}
46622078Sgjelinek 
46633172Sgjelinek 	/*
46643172Sgjelinek 	 * Retrieve remote handle brand type and determine whether it is
46653172Sgjelinek 	 * native or not.
46663172Sgjelinek 	 */
46673172Sgjelinek 	if (zonecfg_get_brand(rem_handle, target_brand, sizeof (target_brand))
46683172Sgjelinek 	    != Z_OK) {
46693172Sgjelinek 		zerror(gettext("missing or invalid brand"));
46703172Sgjelinek 		exit(Z_ERR);
46713172Sgjelinek 	}
46723172Sgjelinek 	is_native_zone = (strcmp(target_brand, NATIVE_BRAND_NAME) == 0);
46733172Sgjelinek 
46743339Szt129084 	res = verify_handle(CMD_ATTACH, local_handle, argv);
46752078Sgjelinek 
46762078Sgjelinek 	/* Get the detach information for the locally defined zone. */
46772078Sgjelinek 	if ((err = zonecfg_get_detach_info(local_handle, B_FALSE)) != Z_OK) {
46782078Sgjelinek 		errno = err;
46792078Sgjelinek 		zperror(gettext("getting the attach information failed"),
46802078Sgjelinek 		    B_TRUE);
46812078Sgjelinek 		res = Z_ERR;
46822078Sgjelinek 	} else {
46832078Sgjelinek 		/* sw_cmp prints error msgs as necessary */
46842078Sgjelinek 		if (sw_cmp(local_handle, rem_handle, SW_CMP_NONE) != Z_OK)
46852078Sgjelinek 			res = Z_ERR;
46862078Sgjelinek 	}
46872078Sgjelinek 
46882078Sgjelinek done:
46892078Sgjelinek 	if (strcmp(manifest_path, "-") != 0)
46902078Sgjelinek 		(void) close(fd);
46912078Sgjelinek 
46922078Sgjelinek 	zonecfg_fini_handle(local_handle);
46932078Sgjelinek 	zonecfg_fini_handle(rem_handle);
46942078Sgjelinek 
46952078Sgjelinek 	return ((res == Z_OK) ? Z_OK : Z_ERR);
46962078Sgjelinek }
46972078Sgjelinek 
46981507Sgjelinek static int
46991507Sgjelinek attach_func(int argc, char *argv[])
47001507Sgjelinek {
47011507Sgjelinek 	int lockfd;
47021507Sgjelinek 	int err, arg;
47031507Sgjelinek 	boolean_t force = B_FALSE;
47041507Sgjelinek 	zone_dochandle_t handle;
47051507Sgjelinek 	zone_dochandle_t athandle = NULL;
47061507Sgjelinek 	char zonepath[MAXPATHLEN];
47072712Snn35248 	char brand[MAXNAMELEN], atbrand[MAXNAMELEN];
47082078Sgjelinek 	boolean_t execute = B_TRUE;
47092078Sgjelinek 	char *manifest_path;
47101507Sgjelinek 
47111507Sgjelinek 	if (zonecfg_in_alt_root()) {
47121507Sgjelinek 		zerror(gettext("cannot attach zone in alternate root"));
47131507Sgjelinek 		return (Z_ERR);
47141507Sgjelinek 	}
47151507Sgjelinek 
47161507Sgjelinek 	optind = 0;
47172078Sgjelinek 	if ((arg = getopt(argc, argv, "?Fn:")) != EOF) {
47181507Sgjelinek 		switch (arg) {
47191507Sgjelinek 		case '?':
47201507Sgjelinek 			sub_usage(SHELP_ATTACH, CMD_ATTACH);
47211507Sgjelinek 			return (optopt == '?' ? Z_OK : Z_USAGE);
47221507Sgjelinek 		case 'F':
47231507Sgjelinek 			force = B_TRUE;
47241507Sgjelinek 			break;
47252078Sgjelinek 		case 'n':
47262078Sgjelinek 			execute = B_FALSE;
47272078Sgjelinek 			manifest_path = optarg;
47282078Sgjelinek 			break;
47291507Sgjelinek 		default:
47301507Sgjelinek 			sub_usage(SHELP_ATTACH, CMD_ATTACH);
47311507Sgjelinek 			return (Z_USAGE);
47321507Sgjelinek 		}
47331507Sgjelinek 	}
47342078Sgjelinek 
47352078Sgjelinek 	/*
47362078Sgjelinek 	 * If the no-execute option was specified, we need to branch down
47372078Sgjelinek 	 * a completely different path since there is no zone required to be
47382078Sgjelinek 	 * configured for this option.
47392078Sgjelinek 	 */
47402078Sgjelinek 	if (!execute)
47413339Szt129084 		return (dryrun_attach(manifest_path, argv));
47422078Sgjelinek 
47432712Snn35248 	if (sanity_check(target_zone, CMD_ATTACH, B_FALSE, B_TRUE, B_FALSE)
47442712Snn35248 	    != Z_OK)
47451507Sgjelinek 		return (Z_ERR);
47463339Szt129084 	if (verify_details(CMD_ATTACH, argv) != Z_OK)
47471507Sgjelinek 		return (Z_ERR);
47481507Sgjelinek 
47491507Sgjelinek 	if ((err = zone_get_zonepath(target_zone, zonepath, sizeof (zonepath)))
47501507Sgjelinek 	    != Z_OK) {
47511507Sgjelinek 		errno = err;
47521507Sgjelinek 		zperror2(target_zone, gettext("could not get zone path"));
47531507Sgjelinek 		return (Z_ERR);
47541507Sgjelinek 	}
47551507Sgjelinek 
47561507Sgjelinek 	if ((handle = zonecfg_init_handle()) == NULL) {
47571507Sgjelinek 		zperror(cmd_to_str(CMD_ATTACH), B_TRUE);
47581507Sgjelinek 		return (Z_ERR);
47591507Sgjelinek 	}
47601507Sgjelinek 
47611507Sgjelinek 	if ((err = zonecfg_get_handle(target_zone, handle)) != Z_OK) {
47621507Sgjelinek 		errno = err;
47631507Sgjelinek 		zperror(cmd_to_str(CMD_ATTACH), B_TRUE);
47641507Sgjelinek 		zonecfg_fini_handle(handle);
47651507Sgjelinek 		return (Z_ERR);
47661507Sgjelinek 	}
47671507Sgjelinek 
47681507Sgjelinek 	if (grab_lock_file(target_zone, &lockfd) != Z_OK) {
47691507Sgjelinek 		zerror(gettext("another %s may have an operation in progress."),
47701507Sgjelinek 		    "zoneadm");
47711507Sgjelinek 		zonecfg_fini_handle(handle);
47721507Sgjelinek 		return (Z_ERR);
47731507Sgjelinek 	}
47741507Sgjelinek 
47751507Sgjelinek 	if (force)
47761507Sgjelinek 		goto forced;
47771507Sgjelinek 
47781507Sgjelinek 	if ((athandle = zonecfg_init_handle()) == NULL) {
47791507Sgjelinek 		zperror(cmd_to_str(CMD_ATTACH), B_TRUE);
47801507Sgjelinek 		goto done;
47811507Sgjelinek 	}
47821507Sgjelinek 
47831507Sgjelinek 	if ((err = zonecfg_get_attach_handle(zonepath, target_zone, B_TRUE,
47841507Sgjelinek 	    athandle)) != Z_OK) {
47851507Sgjelinek 		if (err == Z_NO_ZONE)
47861507Sgjelinek 			zerror(gettext("Not a detached zone"));
47871507Sgjelinek 		else if (err == Z_INVALID_DOCUMENT)
47881507Sgjelinek 			zerror(gettext("Cannot attach to an earlier release "
47891507Sgjelinek 			    "of the operating system"));
47901507Sgjelinek 		else
47911507Sgjelinek 			zperror(cmd_to_str(CMD_ATTACH), B_TRUE);
47921507Sgjelinek 		goto done;
47931507Sgjelinek 	}
47941507Sgjelinek 
47951507Sgjelinek 	/* Get the detach information for the locally defined zone. */
47961507Sgjelinek 	if ((err = zonecfg_get_detach_info(handle, B_FALSE)) != Z_OK) {
47971507Sgjelinek 		errno = err;
47981507Sgjelinek 		zperror(gettext("getting the attach information failed"),
47991507Sgjelinek 		    B_TRUE);
48001507Sgjelinek 		goto done;
48011507Sgjelinek 	}
48021507Sgjelinek 
48032712Snn35248 	/*
48042712Snn35248 	 * Ensure that the detached and locally defined zones are both of
48052712Snn35248 	 * the same brand.
48062712Snn35248 	 */
48072712Snn35248 	if ((zonecfg_get_brand(handle, brand, sizeof (brand)) != 0) ||
48082712Snn35248 	    (zonecfg_get_brand(athandle, atbrand, sizeof (atbrand)) != 0)) {
48092712Snn35248 		err = Z_ERR;
48102712Snn35248 		zerror(gettext("missing or invalid brand"));
48112712Snn35248 		goto done;
48122712Snn35248 	}
48132712Snn35248 
48142712Snn35248 	if (strcmp(atbrand, brand) != NULL) {
48152712Snn35248 		err = Z_ERR;
48162712Snn35248 		zerror(gettext("Trying to attach a '%s' zone to a '%s' "
48172712Snn35248 		    "configuration."), atbrand, brand);
48182712Snn35248 		goto done;
48192712Snn35248 	}
48202712Snn35248 
48211507Sgjelinek 	/* sw_cmp prints error msgs as necessary */
48221867Sgjelinek 	if ((err = sw_cmp(handle, athandle, SW_CMP_NONE)) != Z_OK)
48231507Sgjelinek 		goto done;
48241507Sgjelinek 
48251507Sgjelinek 	if ((err = dev_fix(athandle)) != Z_OK)
48261507Sgjelinek 		goto done;
48271507Sgjelinek 
48281507Sgjelinek forced:
48291507Sgjelinek 
48301507Sgjelinek 	zonecfg_rm_detached(handle, force);
48311507Sgjelinek 
48321507Sgjelinek 	if ((err = zone_set_state(target_zone, ZONE_STATE_INSTALLED)) != Z_OK) {
48331507Sgjelinek 		errno = err;
48341507Sgjelinek 		zperror(gettext("could not reset state"), B_TRUE);
48351507Sgjelinek 	}
48361507Sgjelinek 
48371507Sgjelinek done:
48381507Sgjelinek 	zonecfg_fini_handle(handle);
48391507Sgjelinek 	release_lock_file(lockfd);
48401507Sgjelinek 	if (athandle != NULL)
48411507Sgjelinek 		zonecfg_fini_handle(athandle);
48421507Sgjelinek 
48431507Sgjelinek 	return ((err == Z_OK) ? Z_OK : Z_ERR);
48441507Sgjelinek }
48451507Sgjelinek 
48461300Sgjelinek /*
48470Sstevel@tonic-gate  * On input, TRUE => yes, FALSE => no.
48480Sstevel@tonic-gate  * On return, TRUE => 1, FALSE => 0, could not ask => -1.
48490Sstevel@tonic-gate  */
48500Sstevel@tonic-gate 
48510Sstevel@tonic-gate static int
48520Sstevel@tonic-gate ask_yesno(boolean_t default_answer, const char *question)
48530Sstevel@tonic-gate {
48540Sstevel@tonic-gate 	char line[64];	/* should be large enough to answer yes or no */
48550Sstevel@tonic-gate 
48560Sstevel@tonic-gate 	if (!isatty(STDIN_FILENO))
48570Sstevel@tonic-gate 		return (-1);
48580Sstevel@tonic-gate 	for (;;) {
48590Sstevel@tonic-gate 		(void) printf("%s (%s)? ", question,
48600Sstevel@tonic-gate 		    default_answer ? "[y]/n" : "y/[n]");
48610Sstevel@tonic-gate 		if (fgets(line, sizeof (line), stdin) == NULL ||
48620Sstevel@tonic-gate 		    line[0] == '\n')
48630Sstevel@tonic-gate 			return (default_answer ? 1 : 0);
48640Sstevel@tonic-gate 		if (tolower(line[0]) == 'y')
48650Sstevel@tonic-gate 			return (1);
48660Sstevel@tonic-gate 		if (tolower(line[0]) == 'n')
48670Sstevel@tonic-gate 			return (0);
48680Sstevel@tonic-gate 	}
48690Sstevel@tonic-gate }
48700Sstevel@tonic-gate 
48710Sstevel@tonic-gate static int
48720Sstevel@tonic-gate uninstall_func(int argc, char *argv[])
48730Sstevel@tonic-gate {
48740Sstevel@tonic-gate 	char line[ZONENAME_MAX + 128];	/* Enough for "Are you sure ..." */
48751867Sgjelinek 	char rootpath[MAXPATHLEN], zonepath[MAXPATHLEN];
48760Sstevel@tonic-gate 	boolean_t force = B_FALSE;
48770Sstevel@tonic-gate 	int lockfd, answer;
48780Sstevel@tonic-gate 	int err, arg;
48790Sstevel@tonic-gate 
4880766Scarlsonj 	if (zonecfg_in_alt_root()) {
4881766Scarlsonj 		zerror(gettext("cannot uninstall zone in alternate root"));
4882766Scarlsonj 		return (Z_ERR);
4883766Scarlsonj 	}
4884766Scarlsonj 
48850Sstevel@tonic-gate 	optind = 0;
48860Sstevel@tonic-gate 	while ((arg = getopt(argc, argv, "?F")) != EOF) {
48870Sstevel@tonic-gate 		switch (arg) {
48880Sstevel@tonic-gate 		case '?':
48890Sstevel@tonic-gate 			sub_usage(SHELP_UNINSTALL, CMD_UNINSTALL);
48900Sstevel@tonic-gate 			return (optopt == '?' ? Z_OK : Z_USAGE);
48910Sstevel@tonic-gate 		case 'F':
48920Sstevel@tonic-gate 			force = B_TRUE;
48930Sstevel@tonic-gate 			break;
48940Sstevel@tonic-gate 		default:
48950Sstevel@tonic-gate 			sub_usage(SHELP_UNINSTALL, CMD_UNINSTALL);
48960Sstevel@tonic-gate 			return (Z_USAGE);
48970Sstevel@tonic-gate 		}
48980Sstevel@tonic-gate 	}
48990Sstevel@tonic-gate 	if (argc > optind) {
49000Sstevel@tonic-gate 		sub_usage(SHELP_UNINSTALL, CMD_UNINSTALL);
49010Sstevel@tonic-gate 		return (Z_USAGE);
49020Sstevel@tonic-gate 	}
49030Sstevel@tonic-gate 
49042712Snn35248 	if (sanity_check(target_zone, CMD_UNINSTALL, B_FALSE, B_TRUE, B_FALSE)
49052712Snn35248 	    != Z_OK)
49060Sstevel@tonic-gate 		return (Z_ERR);
49070Sstevel@tonic-gate 
49083339Szt129084 	/*
49093339Szt129084 	 * Invoke brand-specific handler.
49103339Szt129084 	 */
49113339Szt129084 	if (invoke_brand_handler(CMD_UNINSTALL, argv) != Z_OK)
49123339Szt129084 		return (Z_ERR);
49133339Szt129084 
49140Sstevel@tonic-gate 	if (!force) {
49150Sstevel@tonic-gate 		(void) snprintf(line, sizeof (line),
49160Sstevel@tonic-gate 		    gettext("Are you sure you want to %s zone %s"),
49170Sstevel@tonic-gate 		    cmd_to_str(CMD_UNINSTALL), target_zone);
49180Sstevel@tonic-gate 		if ((answer = ask_yesno(B_FALSE, line)) == 0) {
49190Sstevel@tonic-gate 			return (Z_OK);
49200Sstevel@tonic-gate 		} else if (answer == -1) {
49210Sstevel@tonic-gate 			zerror(gettext("Input not from terminal and -F "
49220Sstevel@tonic-gate 			    "not specified: %s not done."),
49230Sstevel@tonic-gate 			    cmd_to_str(CMD_UNINSTALL));
49240Sstevel@tonic-gate 			return (Z_ERR);
49250Sstevel@tonic-gate 		}
49260Sstevel@tonic-gate 	}
49270Sstevel@tonic-gate 
49281867Sgjelinek 	if ((err = zone_get_zonepath(target_zone, zonepath,
49291867Sgjelinek 	    sizeof (zonepath))) != Z_OK) {
49300Sstevel@tonic-gate 		errno = err;
49310Sstevel@tonic-gate 		zperror2(target_zone, gettext("could not get zone path"));
49320Sstevel@tonic-gate 		return (Z_ERR);
49330Sstevel@tonic-gate 	}
49340Sstevel@tonic-gate 	if ((err = zone_get_rootpath(target_zone, rootpath,
49350Sstevel@tonic-gate 	    sizeof (rootpath))) != Z_OK) {
49360Sstevel@tonic-gate 		errno = err;
49370Sstevel@tonic-gate 		zperror2(target_zone, gettext("could not get root path"));
49380Sstevel@tonic-gate 		return (Z_ERR);
49390Sstevel@tonic-gate 	}
49400Sstevel@tonic-gate 
49410Sstevel@tonic-gate 	/*
49420Sstevel@tonic-gate 	 * If there seems to be a zoneadmd running for this zone, call it
49430Sstevel@tonic-gate 	 * to tell it that an uninstall is happening; if all goes well it
49440Sstevel@tonic-gate 	 * will then shut itself down.
49450Sstevel@tonic-gate 	 */
49460Sstevel@tonic-gate 	if (ping_zoneadmd(target_zone) == Z_OK) {
49470Sstevel@tonic-gate 		zone_cmd_arg_t zarg;
49480Sstevel@tonic-gate 		zarg.cmd = Z_NOTE_UNINSTALLING;
49490Sstevel@tonic-gate 		/* we don't care too much if this fails... just plow on */
49500Sstevel@tonic-gate 		(void) call_zoneadmd(target_zone, &zarg);
49510Sstevel@tonic-gate 	}
49520Sstevel@tonic-gate 
49530Sstevel@tonic-gate 	if (grab_lock_file(target_zone, &lockfd) != Z_OK) {
49540Sstevel@tonic-gate 		zerror(gettext("another %s may have an operation in progress."),
49551300Sgjelinek 		    "zoneadm");
49560Sstevel@tonic-gate 		return (Z_ERR);
49570Sstevel@tonic-gate 	}
49580Sstevel@tonic-gate 
49590Sstevel@tonic-gate 	/* Don't uninstall the zone if anything is mounted there */
49600Sstevel@tonic-gate 	err = zonecfg_find_mounts(rootpath, NULL, NULL);
49610Sstevel@tonic-gate 	if (err) {
49621867Sgjelinek 		zerror(gettext("These file systems are mounted on "
49631645Scomay 		    "subdirectories of %s.\n"), rootpath);
49640Sstevel@tonic-gate 		(void) zonecfg_find_mounts(rootpath, zfm_print, NULL);
49650Sstevel@tonic-gate 		return (Z_ERR);
49660Sstevel@tonic-gate 	}
49670Sstevel@tonic-gate 
49680Sstevel@tonic-gate 	err = zone_set_state(target_zone, ZONE_STATE_INCOMPLETE);
49690Sstevel@tonic-gate 	if (err != Z_OK) {
49700Sstevel@tonic-gate 		errno = err;
49710Sstevel@tonic-gate 		zperror2(target_zone, gettext("could not set state"));
49720Sstevel@tonic-gate 		goto bad;
49730Sstevel@tonic-gate 	}
49740Sstevel@tonic-gate 
49751867Sgjelinek 	if ((err = cleanup_zonepath(zonepath, B_FALSE)) != Z_OK) {
49761867Sgjelinek 		errno = err;
49771867Sgjelinek 		zperror2(target_zone, gettext("cleaning up zonepath failed"));
49780Sstevel@tonic-gate 		goto bad;
49791867Sgjelinek 	}
49801867Sgjelinek 
49810Sstevel@tonic-gate 	err = zone_set_state(target_zone, ZONE_STATE_CONFIGURED);
49820Sstevel@tonic-gate 	if (err != Z_OK) {
49830Sstevel@tonic-gate 		errno = err;
49840Sstevel@tonic-gate 		zperror2(target_zone, gettext("could not reset state"));
49850Sstevel@tonic-gate 	}
49860Sstevel@tonic-gate bad:
49870Sstevel@tonic-gate 	release_lock_file(lockfd);
49880Sstevel@tonic-gate 	return (err);
49890Sstevel@tonic-gate }
49900Sstevel@tonic-gate 
4991766Scarlsonj /* ARGSUSED */
4992766Scarlsonj static int
4993766Scarlsonj mount_func(int argc, char *argv[])
4994766Scarlsonj {
4995766Scarlsonj 	zone_cmd_arg_t zarg;
49962712Snn35248 	boolean_t force = B_FALSE;
49972712Snn35248 	int arg;
49982712Snn35248 
49992712Snn35248 	/*
50002712Snn35248 	 * The only supported subargument to the "mount" subcommand is
50012712Snn35248 	 * "-f", which forces us to mount a zone in the INCOMPLETE state.
50022712Snn35248 	 */
50032712Snn35248 	optind = 0;
50042712Snn35248 	if ((arg = getopt(argc, argv, "f")) != EOF) {
50052712Snn35248 		switch (arg) {
50062712Snn35248 		case 'f':
50072712Snn35248 			force = B_TRUE;
50082712Snn35248 			break;
50092712Snn35248 		default:
50102712Snn35248 			return (Z_USAGE);
50112712Snn35248 		}
50122712Snn35248 	}
50132712Snn35248 	if (argc > optind)
5014766Scarlsonj 		return (Z_USAGE);
50152712Snn35248 
50162712Snn35248 	if (sanity_check(target_zone, CMD_MOUNT, B_FALSE, B_FALSE, force)
50172712Snn35248 	    != Z_OK)
5018766Scarlsonj 		return (Z_ERR);
50193339Szt129084 	if (verify_details(CMD_MOUNT, argv) != Z_OK)
5020766Scarlsonj 		return (Z_ERR);
5021766Scarlsonj 
50222712Snn35248 	zarg.cmd = force ? Z_FORCEMOUNT : Z_MOUNT;
5023766Scarlsonj 	if (call_zoneadmd(target_zone, &zarg) != 0) {
5024766Scarlsonj 		zerror(gettext("call to %s failed"), "zoneadmd");
5025766Scarlsonj 		return (Z_ERR);
5026766Scarlsonj 	}
5027766Scarlsonj 	return (Z_OK);
5028766Scarlsonj }
5029766Scarlsonj 
5030766Scarlsonj /* ARGSUSED */
5031766Scarlsonj static int
5032766Scarlsonj unmount_func(int argc, char *argv[])
5033766Scarlsonj {
5034766Scarlsonj 	zone_cmd_arg_t zarg;
5035766Scarlsonj 
5036766Scarlsonj 	if (argc > 0)
5037766Scarlsonj 		return (Z_USAGE);
50382712Snn35248 	if (sanity_check(target_zone, CMD_UNMOUNT, B_FALSE, B_FALSE, B_FALSE)
50392712Snn35248 	    != Z_OK)
5040766Scarlsonj 		return (Z_ERR);
5041766Scarlsonj 
5042766Scarlsonj 	zarg.cmd = Z_UNMOUNT;
5043766Scarlsonj 	if (call_zoneadmd(target_zone, &zarg) != 0) {
5044766Scarlsonj 		zerror(gettext("call to %s failed"), "zoneadmd");
5045766Scarlsonj 		return (Z_ERR);
5046766Scarlsonj 	}
5047766Scarlsonj 	return (Z_OK);
5048766Scarlsonj }
5049766Scarlsonj 
50500Sstevel@tonic-gate static int
50512303Scarlsonj mark_func(int argc, char *argv[])
50522303Scarlsonj {
50532303Scarlsonj 	int err, lockfd;
50542303Scarlsonj 
50552303Scarlsonj 	if (argc != 1 || strcmp(argv[0], "incomplete") != 0)
50562303Scarlsonj 		return (Z_USAGE);
50572712Snn35248 	if (sanity_check(target_zone, CMD_MARK, B_FALSE, B_FALSE, B_FALSE)
50582712Snn35248 	    != Z_OK)
50592303Scarlsonj 		return (Z_ERR);
50602303Scarlsonj 
50613339Szt129084 	/*
50623339Szt129084 	 * Invoke brand-specific handler.
50633339Szt129084 	 */
50643339Szt129084 	if (invoke_brand_handler(CMD_MARK, argv) != Z_OK)
50653339Szt129084 		return (Z_ERR);
50663339Szt129084 
50672303Scarlsonj 	if (grab_lock_file(target_zone, &lockfd) != Z_OK) {
50682303Scarlsonj 		zerror(gettext("another %s may have an operation in progress."),
50692303Scarlsonj 		    "zoneadm");
50702303Scarlsonj 		return (Z_ERR);
50712303Scarlsonj 	}
50722303Scarlsonj 
50732303Scarlsonj 	err = zone_set_state(target_zone, ZONE_STATE_INCOMPLETE);
50742303Scarlsonj 	if (err != Z_OK) {
50752303Scarlsonj 		errno = err;
50762303Scarlsonj 		zperror2(target_zone, gettext("could not set state"));
50772303Scarlsonj 	}
50782303Scarlsonj 	release_lock_file(lockfd);
50792303Scarlsonj 
50802303Scarlsonj 	return (err);
50812303Scarlsonj }
50822303Scarlsonj 
50833247Sgjelinek /*
50843247Sgjelinek  * Check what scheduling class we're running under and print a warning if
50853247Sgjelinek  * we're not using FSS.
50863247Sgjelinek  */
50873247Sgjelinek static int
50883247Sgjelinek check_sched_fss(zone_dochandle_t handle)
50893247Sgjelinek {
50903247Sgjelinek 	char class_name[PC_CLNMSZ];
50913247Sgjelinek 
50923247Sgjelinek 	if (zonecfg_get_dflt_sched_class(handle, class_name,
50933247Sgjelinek 	    sizeof (class_name)) != Z_OK) {
50943247Sgjelinek 		zerror(gettext("WARNING: unable to determine the zone's "
50953247Sgjelinek 		    "scheduling class"));
50963247Sgjelinek 	} else if (strcmp("FSS", class_name) != 0) {
50973247Sgjelinek 		zerror(gettext("WARNING: The zone.cpu-shares rctl is set but\n"
50983247Sgjelinek 		    "FSS is not the default scheduling class for this zone.  "
50993247Sgjelinek 		    "FSS will be\nused for processes in the zone but to get "
51003247Sgjelinek 		    "the full benefit of FSS,\nit should be the default "
51013247Sgjelinek 		    "scheduling class.  See dispadmin(1M) for\nmore details."));
51023247Sgjelinek 		return (Z_SYSTEM);
51033247Sgjelinek 	}
51043247Sgjelinek 
51053247Sgjelinek 	return (Z_OK);
51063247Sgjelinek }
51073247Sgjelinek 
51083247Sgjelinek static int
51093247Sgjelinek check_cpu_shares_sched(zone_dochandle_t handle)
51103247Sgjelinek {
51113247Sgjelinek 	int err;
51123247Sgjelinek 	int res = Z_OK;
51133247Sgjelinek 	struct zone_rctltab rctl;
51143247Sgjelinek 
51153247Sgjelinek 	if ((err = zonecfg_setrctlent(handle)) != Z_OK) {
51163247Sgjelinek 		errno = err;
51173247Sgjelinek 		zperror(cmd_to_str(CMD_APPLY), B_TRUE);
51183247Sgjelinek 		return (err);
51193247Sgjelinek 	}
51203247Sgjelinek 
51213247Sgjelinek 	while (zonecfg_getrctlent(handle, &rctl) == Z_OK) {
51223247Sgjelinek 		if (strcmp(rctl.zone_rctl_name, "zone.cpu-shares") == 0) {
51233247Sgjelinek 			if (check_sched_fss(handle) != Z_OK)
51243247Sgjelinek 				res = Z_SYSTEM;
51253247Sgjelinek 			break;
51263247Sgjelinek 		}
51273247Sgjelinek 	}
51283247Sgjelinek 
51293247Sgjelinek 	(void) zonecfg_endrctlent(handle);
51303247Sgjelinek 
51313247Sgjelinek 	return (res);
51323247Sgjelinek }
51333247Sgjelinek 
51343247Sgjelinek /*
51353352Sgjelinek  * Check if there is a mix of processes running in different pools within the
51363352Sgjelinek  * zone.  This is currently only going to be called for the global zone from
51373352Sgjelinek  * apply_func but that could be generalized in the future.
51383352Sgjelinek  */
51393352Sgjelinek static boolean_t
51403352Sgjelinek mixed_pools(zoneid_t zoneid)
51413352Sgjelinek {
51423352Sgjelinek 	DIR *dirp;
51433352Sgjelinek 	dirent_t *dent;
51443352Sgjelinek 	boolean_t mixed = B_FALSE;
51453352Sgjelinek 	boolean_t poolid_set = B_FALSE;
51463352Sgjelinek 	poolid_t last_poolid = 0;
51473352Sgjelinek 
51483352Sgjelinek 	if ((dirp = opendir("/proc")) == NULL) {
51493352Sgjelinek 		zerror(gettext("could not open /proc"));
51503352Sgjelinek 		return (B_FALSE);
51513352Sgjelinek 	}
51523352Sgjelinek 
51533352Sgjelinek 	while ((dent = readdir(dirp)) != NULL) {
51543352Sgjelinek 		int procfd;
51553352Sgjelinek 		psinfo_t ps;
51563352Sgjelinek 		char procpath[MAXPATHLEN];
51573352Sgjelinek 
51583352Sgjelinek 		if (dent->d_name[0] == '.')
51593352Sgjelinek 			continue;
51603352Sgjelinek 
51613352Sgjelinek 		(void) snprintf(procpath, sizeof (procpath), "/proc/%s/psinfo",
51623352Sgjelinek 		    dent->d_name);
51633352Sgjelinek 
51643352Sgjelinek 		if ((procfd = open(procpath, O_RDONLY)) == -1)
51653352Sgjelinek 			continue;
51663352Sgjelinek 
51673352Sgjelinek 		if (read(procfd, &ps, sizeof (ps)) == sizeof (psinfo_t)) {
51683352Sgjelinek 			/* skip processes in other zones and system processes */
51693352Sgjelinek 			if (zoneid != ps.pr_zoneid || ps.pr_flag & SSYS) {
51703352Sgjelinek 				(void) close(procfd);
51713352Sgjelinek 				continue;
51723352Sgjelinek 			}
51733352Sgjelinek 
51743352Sgjelinek 			if (poolid_set) {
51753352Sgjelinek 				if (ps.pr_poolid != last_poolid)
51763352Sgjelinek 					mixed = B_TRUE;
51773352Sgjelinek 			} else {
51783352Sgjelinek 				last_poolid = ps.pr_poolid;
51793352Sgjelinek 				poolid_set = B_TRUE;
51803352Sgjelinek 			}
51813352Sgjelinek 		}
51823352Sgjelinek 
51833352Sgjelinek 		(void) close(procfd);
51843352Sgjelinek 
51853352Sgjelinek 		if (mixed)
51863352Sgjelinek 			break;
51873352Sgjelinek 	}
51883352Sgjelinek 
51893352Sgjelinek 	(void) closedir(dirp);
51903352Sgjelinek 
51913352Sgjelinek 	return (mixed);
51923352Sgjelinek }
51933352Sgjelinek 
51943352Sgjelinek /*
51953352Sgjelinek  * Check if a persistent or temporary pool is configured for the zone.
51963352Sgjelinek  * This is currently only going to be called for the global zone from
51973352Sgjelinek  * apply_func but that could be generalized in the future.
51983352Sgjelinek  */
51993352Sgjelinek static boolean_t
52003352Sgjelinek pool_configured(zone_dochandle_t handle)
52013352Sgjelinek {
52023352Sgjelinek 	int err1, err2;
52033352Sgjelinek 	struct zone_psettab pset_tab;
52043352Sgjelinek 	char poolname[MAXPATHLEN];
52053352Sgjelinek 
52063352Sgjelinek 	err1 = zonecfg_lookup_pset(handle, &pset_tab);
52073352Sgjelinek 	err2 = zonecfg_get_pool(handle, poolname, sizeof (poolname));
52083352Sgjelinek 
52093352Sgjelinek 	if (err1 == Z_NO_ENTRY &&
52103352Sgjelinek 	    (err2 == Z_NO_ENTRY || (err2 == Z_OK && strlen(poolname) == 0)))
52113352Sgjelinek 		return (B_FALSE);
52123352Sgjelinek 
52133352Sgjelinek 	return (B_TRUE);
52143352Sgjelinek }
52153352Sgjelinek 
52163352Sgjelinek /*
52173247Sgjelinek  * This is an undocumented interface which is currently only used to apply
52183247Sgjelinek  * the global zone resource management settings when the system boots.
52193247Sgjelinek  * This function does not yet properly handle updating a running system so
52203247Sgjelinek  * any projects running in the zone would be trashed if this function
52213247Sgjelinek  * were to run after the zone had booted.  It also does not reset any
52223247Sgjelinek  * rctl settings that were removed from zonecfg.  There is still work to be
52233247Sgjelinek  * done before we can properly support dynamically updating the resource
52243247Sgjelinek  * management settings for a running zone (global or non-global).  Thus, this
52253247Sgjelinek  * functionality is undocumented for now.
52263247Sgjelinek  */
52273247Sgjelinek /* ARGSUSED */
52283247Sgjelinek static int
52293247Sgjelinek apply_func(int argc, char *argv[])
52303247Sgjelinek {
52313247Sgjelinek 	int err;
52323247Sgjelinek 	int res = Z_OK;
52333247Sgjelinek 	priv_set_t *privset;
52343247Sgjelinek 	zoneid_t zoneid;
52353247Sgjelinek 	zone_dochandle_t handle;
52363247Sgjelinek 	struct zone_mcaptab mcap;
52373247Sgjelinek 	char pool_err[128];
52383247Sgjelinek 
52393247Sgjelinek 	zoneid = getzoneid();
52403247Sgjelinek 
52413247Sgjelinek 	if (zonecfg_in_alt_root() || zoneid != GLOBAL_ZONEID ||
52423247Sgjelinek 	    target_zone == NULL || strcmp(target_zone, GLOBAL_ZONENAME) != 0)
52433247Sgjelinek 		return (usage(B_FALSE));
52443247Sgjelinek 
52453247Sgjelinek 	if ((privset = priv_allocset()) == NULL) {
52463247Sgjelinek 		zerror(gettext("%s failed"), "priv_allocset");
52473247Sgjelinek 		return (Z_ERR);
52483247Sgjelinek 	}
52493247Sgjelinek 
52503247Sgjelinek 	if (getppriv(PRIV_EFFECTIVE, privset) != 0) {
52513247Sgjelinek 		zerror(gettext("%s failed"), "getppriv");
52523247Sgjelinek 		priv_freeset(privset);
52533247Sgjelinek 		return (Z_ERR);
52543247Sgjelinek 	}
52553247Sgjelinek 
52563247Sgjelinek 	if (priv_isfullset(privset) == B_FALSE) {
52573247Sgjelinek 		(void) usage(B_FALSE);
52583247Sgjelinek 		priv_freeset(privset);
52593247Sgjelinek 		return (Z_ERR);
52603247Sgjelinek 	}
52613247Sgjelinek 	priv_freeset(privset);
52623247Sgjelinek 
52633247Sgjelinek 	if ((handle = zonecfg_init_handle()) == NULL) {
52643247Sgjelinek 		zperror(cmd_to_str(CMD_APPLY), B_TRUE);
52653247Sgjelinek 		return (Z_ERR);
52663247Sgjelinek 	}
52673247Sgjelinek 
52683247Sgjelinek 	if ((err = zonecfg_get_handle(target_zone, handle)) != Z_OK) {
52693247Sgjelinek 		errno = err;
52703247Sgjelinek 		zperror(cmd_to_str(CMD_APPLY), B_TRUE);
52713247Sgjelinek 		zonecfg_fini_handle(handle);
52723247Sgjelinek 		return (Z_ERR);
52733247Sgjelinek 	}
52743247Sgjelinek 
52753247Sgjelinek 	/* specific error msgs are printed within apply_rctls */
52763247Sgjelinek 	if ((err = zonecfg_apply_rctls(target_zone, handle)) != Z_OK) {
52773247Sgjelinek 		errno = err;
52783247Sgjelinek 		zperror(cmd_to_str(CMD_APPLY), B_TRUE);
52793247Sgjelinek 		res = Z_ERR;
52803247Sgjelinek 	}
52813247Sgjelinek 
52823247Sgjelinek 	if ((err = check_cpu_shares_sched(handle)) != Z_OK)
52833247Sgjelinek 		res = Z_ERR;
52843247Sgjelinek 
52853352Sgjelinek 	if (pool_configured(handle)) {
52863352Sgjelinek 		if (mixed_pools(zoneid)) {
52873352Sgjelinek 			zerror(gettext("Zone is using multiple resource "
52883352Sgjelinek 			    "pools.  The pool\nconfiguration cannot be "
52893352Sgjelinek 			    "applied without rebooting."));
52903352Sgjelinek 			res = Z_ERR;
52913352Sgjelinek 		} else {
52923352Sgjelinek 
52933352Sgjelinek 			/*
52943352Sgjelinek 			 * The next two blocks of code attempt to set up
52953352Sgjelinek 			 * temporary pools as well as persistent pools.  In
52963352Sgjelinek 			 * both cases we call the functions unconditionally.
52973352Sgjelinek 			 * Within each funtion the code will check if the zone
52983352Sgjelinek 			 * is actually configured for a temporary pool or
52993352Sgjelinek 			 * persistent pool and just return if there is nothing
53003352Sgjelinek 			 * to do.
53013352Sgjelinek 			 */
53023352Sgjelinek 			if ((err = zonecfg_bind_tmp_pool(handle, zoneid,
53033352Sgjelinek 			    pool_err, sizeof (pool_err))) != Z_OK) {
53043352Sgjelinek 				if (err == Z_POOL || err == Z_POOL_CREATE ||
53053352Sgjelinek 				    err == Z_POOL_BIND)
53063352Sgjelinek 					zerror("%s: %s", zonecfg_strerror(err),
53073352Sgjelinek 					    pool_err);
53083352Sgjelinek 				else
53093352Sgjelinek 					zerror(gettext("could not bind zone to "
53103352Sgjelinek 					    "temporary pool: %s"),
53113352Sgjelinek 					    zonecfg_strerror(err));
53123352Sgjelinek 				res = Z_ERR;
53133352Sgjelinek 			}
53143352Sgjelinek 
53153352Sgjelinek 			if ((err = zonecfg_bind_pool(handle, zoneid, pool_err,
53163352Sgjelinek 			    sizeof (pool_err))) != Z_OK) {
53173352Sgjelinek 				if (err == Z_POOL || err == Z_POOL_BIND)
53183352Sgjelinek 					zerror("%s: %s", zonecfg_strerror(err),
53193352Sgjelinek 					    pool_err);
53203352Sgjelinek 				else
53213352Sgjelinek 					zerror("%s", zonecfg_strerror(err));
53223352Sgjelinek 			}
53233352Sgjelinek 		}
53243247Sgjelinek 	}
53253247Sgjelinek 
53263247Sgjelinek 	/*
53273247Sgjelinek 	 * If a memory cap is configured, set the cap in the kernel using
53283247Sgjelinek 	 * zone_setattr() and make sure the rcapd SMF service is enabled.
53293247Sgjelinek 	 */
53303247Sgjelinek 	if (zonecfg_getmcapent(handle, &mcap) == Z_OK) {
53313247Sgjelinek 		uint64_t num;
53323247Sgjelinek 		char smf_err[128];
53333247Sgjelinek 
53343247Sgjelinek 		num = (uint64_t)strtoll(mcap.zone_physmem_cap, NULL, 10);
53353247Sgjelinek 		if (zone_setattr(zoneid, ZONE_ATTR_PHYS_MCAP, &num, 0) == -1) {
53363247Sgjelinek 			zerror(gettext("could not set zone memory cap"));
53373247Sgjelinek 			res = Z_ERR;
53383247Sgjelinek 		}
53393247Sgjelinek 
53403247Sgjelinek 		if (zonecfg_enable_rcapd(smf_err, sizeof (smf_err)) != Z_OK) {
53413247Sgjelinek 			zerror(gettext("enabling system/rcap service failed: "
53423247Sgjelinek 			    "%s"), smf_err);
53433247Sgjelinek 			res = Z_ERR;
53443247Sgjelinek 		}
53453247Sgjelinek 	}
53463247Sgjelinek 
53473247Sgjelinek 	zonecfg_fini_handle(handle);
53483247Sgjelinek 
53493247Sgjelinek 	return (res);
53503247Sgjelinek }
53513247Sgjelinek 
53522303Scarlsonj static int
53530Sstevel@tonic-gate help_func(int argc, char *argv[])
53540Sstevel@tonic-gate {
53550Sstevel@tonic-gate 	int arg, cmd_num;
53560Sstevel@tonic-gate 
53570Sstevel@tonic-gate 	if (argc == 0) {
53580Sstevel@tonic-gate 		(void) usage(B_TRUE);
53590Sstevel@tonic-gate 		return (Z_OK);
53600Sstevel@tonic-gate 	}
53610Sstevel@tonic-gate 	optind = 0;
53620Sstevel@tonic-gate 	if ((arg = getopt(argc, argv, "?")) != EOF) {
53630Sstevel@tonic-gate 		switch (arg) {
53640Sstevel@tonic-gate 		case '?':
53650Sstevel@tonic-gate 			sub_usage(SHELP_HELP, CMD_HELP);
53660Sstevel@tonic-gate 			return (optopt == '?' ? Z_OK : Z_USAGE);
53670Sstevel@tonic-gate 		default:
53680Sstevel@tonic-gate 			sub_usage(SHELP_HELP, CMD_HELP);
53690Sstevel@tonic-gate 			return (Z_USAGE);
53700Sstevel@tonic-gate 		}
53710Sstevel@tonic-gate 	}
53720Sstevel@tonic-gate 	while (optind < argc) {
5373988Scarlsonj 		/* Private commands have NULL short_usage; omit them */
5374988Scarlsonj 		if ((cmd_num = cmd_match(argv[optind])) < 0 ||
5375988Scarlsonj 		    cmdtab[cmd_num].short_usage == NULL) {
53760Sstevel@tonic-gate 			sub_usage(SHELP_HELP, CMD_HELP);
53770Sstevel@tonic-gate 			return (Z_USAGE);
53780Sstevel@tonic-gate 		}
53790Sstevel@tonic-gate 		sub_usage(cmdtab[cmd_num].short_usage, cmd_num);
53800Sstevel@tonic-gate 		optind++;
53810Sstevel@tonic-gate 	}
53820Sstevel@tonic-gate 	return (Z_OK);
53830Sstevel@tonic-gate }
53840Sstevel@tonic-gate 
53850Sstevel@tonic-gate /*
53860Sstevel@tonic-gate  * Returns: CMD_MIN thru CMD_MAX on success, -1 on error
53870Sstevel@tonic-gate  */
53880Sstevel@tonic-gate 
53890Sstevel@tonic-gate static int
53900Sstevel@tonic-gate cmd_match(char *cmd)
53910Sstevel@tonic-gate {
53920Sstevel@tonic-gate 	int i;
53930Sstevel@tonic-gate 
53940Sstevel@tonic-gate 	for (i = CMD_MIN; i <= CMD_MAX; i++) {
53950Sstevel@tonic-gate 		/* return only if there is an exact match */
53960Sstevel@tonic-gate 		if (strcmp(cmd, cmdtab[i].cmd_name) == 0)
53970Sstevel@tonic-gate 			return (cmdtab[i].cmd_num);
53980Sstevel@tonic-gate 	}
53990Sstevel@tonic-gate 	return (-1);
54000Sstevel@tonic-gate }
54010Sstevel@tonic-gate 
54020Sstevel@tonic-gate static int
54030Sstevel@tonic-gate parse_and_run(int argc, char *argv[])
54040Sstevel@tonic-gate {
54050Sstevel@tonic-gate 	int i = cmd_match(argv[0]);
54060Sstevel@tonic-gate 
54070Sstevel@tonic-gate 	if (i < 0)
54080Sstevel@tonic-gate 		return (usage(B_FALSE));
54090Sstevel@tonic-gate 	return (cmdtab[i].handler(argc - 1, &(argv[1])));
54100Sstevel@tonic-gate }
54110Sstevel@tonic-gate 
54120Sstevel@tonic-gate static char *
54130Sstevel@tonic-gate get_execbasename(char *execfullname)
54140Sstevel@tonic-gate {
54150Sstevel@tonic-gate 	char *last_slash, *execbasename;
54160Sstevel@tonic-gate 
54170Sstevel@tonic-gate 	/* guard against '/' at end of command invocation */
54180Sstevel@tonic-gate 	for (;;) {
54190Sstevel@tonic-gate 		last_slash = strrchr(execfullname, '/');
54200Sstevel@tonic-gate 		if (last_slash == NULL) {
54210Sstevel@tonic-gate 			execbasename = execfullname;
54220Sstevel@tonic-gate 			break;
54230Sstevel@tonic-gate 		} else {
54240Sstevel@tonic-gate 			execbasename = last_slash + 1;
54250Sstevel@tonic-gate 			if (*execbasename == '\0') {
54260Sstevel@tonic-gate 				*last_slash = '\0';
54270Sstevel@tonic-gate 				continue;
54280Sstevel@tonic-gate 			}
54290Sstevel@tonic-gate 			break;
54300Sstevel@tonic-gate 		}
54310Sstevel@tonic-gate 	}
54320Sstevel@tonic-gate 	return (execbasename);
54330Sstevel@tonic-gate }
54340Sstevel@tonic-gate 
54350Sstevel@tonic-gate int
54360Sstevel@tonic-gate main(int argc, char **argv)
54370Sstevel@tonic-gate {
54380Sstevel@tonic-gate 	int arg;
54390Sstevel@tonic-gate 	zoneid_t zid;
5440766Scarlsonj 	struct stat st;
54412712Snn35248 	char *zone_lock_env;
54422712Snn35248 	int err;
54430Sstevel@tonic-gate 
54440Sstevel@tonic-gate 	if ((locale = setlocale(LC_ALL, "")) == NULL)
54450Sstevel@tonic-gate 		locale = "C";
54460Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
54470Sstevel@tonic-gate 	setbuf(stdout, NULL);
54480Sstevel@tonic-gate 	(void) sigset(SIGHUP, SIG_IGN);
54490Sstevel@tonic-gate 	execname = get_execbasename(argv[0]);
54500Sstevel@tonic-gate 	target_zone = NULL;
54510Sstevel@tonic-gate 	if (chdir("/") != 0) {
54520Sstevel@tonic-gate 		zerror(gettext("could not change directory to /."));
54530Sstevel@tonic-gate 		exit(Z_ERR);
54540Sstevel@tonic-gate 	}
54550Sstevel@tonic-gate 
54562082Seschrock 	if (init_zfs() != Z_OK)
54572082Seschrock 		exit(Z_ERR);
54582082Seschrock 
54592303Scarlsonj 	while ((arg = getopt(argc, argv, "?u:z:R:")) != EOF) {
54600Sstevel@tonic-gate 		switch (arg) {
54610Sstevel@tonic-gate 		case '?':
54620Sstevel@tonic-gate 			return (usage(B_TRUE));
54632303Scarlsonj 		case 'u':
54642303Scarlsonj 			target_uuid = optarg;
54652303Scarlsonj 			break;
54660Sstevel@tonic-gate 		case 'z':
54670Sstevel@tonic-gate 			target_zone = optarg;
54680Sstevel@tonic-gate 			break;
5469766Scarlsonj 		case 'R':	/* private option for admin/install use */
5470766Scarlsonj 			if (*optarg != '/') {
5471766Scarlsonj 				zerror(gettext("root path must be absolute."));
5472766Scarlsonj 				exit(Z_ERR);
5473766Scarlsonj 			}
5474766Scarlsonj 			if (stat(optarg, &st) == -1 || !S_ISDIR(st.st_mode)) {
5475766Scarlsonj 				zerror(
5476766Scarlsonj 				    gettext("root path must be a directory."));
5477766Scarlsonj 				exit(Z_ERR);
5478766Scarlsonj 			}
5479766Scarlsonj 			zonecfg_set_root(optarg);
5480766Scarlsonj 			break;
54810Sstevel@tonic-gate 		default:
54820Sstevel@tonic-gate 			return (usage(B_FALSE));
54830Sstevel@tonic-gate 		}
54840Sstevel@tonic-gate 	}
54850Sstevel@tonic-gate 
54860Sstevel@tonic-gate 	if (optind >= argc)
54870Sstevel@tonic-gate 		return (usage(B_FALSE));
54882303Scarlsonj 
54892303Scarlsonj 	if (target_uuid != NULL && *target_uuid != '\0') {
54902303Scarlsonj 		uuid_t uuid;
54912303Scarlsonj 		static char newtarget[ZONENAME_MAX];
54922303Scarlsonj 
54932303Scarlsonj 		if (uuid_parse(target_uuid, uuid) == -1) {
54942303Scarlsonj 			zerror(gettext("illegal UUID value specified"));
54952303Scarlsonj 			exit(Z_ERR);
54962303Scarlsonj 		}
54972303Scarlsonj 		if (zonecfg_get_name_by_uuid(uuid, newtarget,
54982303Scarlsonj 		    sizeof (newtarget)) == Z_OK)
54992303Scarlsonj 			target_zone = newtarget;
55002303Scarlsonj 	}
55012303Scarlsonj 
55020Sstevel@tonic-gate 	if (target_zone != NULL && zone_get_id(target_zone, &zid) != 0) {
55030Sstevel@tonic-gate 		errno = Z_NO_ZONE;
55040Sstevel@tonic-gate 		zperror(target_zone, B_TRUE);
55050Sstevel@tonic-gate 		exit(Z_ERR);
55060Sstevel@tonic-gate 	}
55072712Snn35248 
55082712Snn35248 	/*
55092712Snn35248 	 * See if we have inherited the right to manipulate this zone from
55102712Snn35248 	 * a zoneadm instance in our ancestry.  If so, set zone_lock_cnt to
55112712Snn35248 	 * indicate it.  If not, make that explicit in our environment.
55122712Snn35248 	 */
55132712Snn35248 	zone_lock_env = getenv(LOCK_ENV_VAR);
55142712Snn35248 	if (zone_lock_env == NULL) {
55152712Snn35248 		if (putenv(zoneadm_lock_not_held) != 0) {
55162712Snn35248 			zperror(target_zone, B_TRUE);
55172712Snn35248 			exit(Z_ERR);
55182712Snn35248 		}
55192712Snn35248 	} else {
55202712Snn35248 		zoneadm_is_nested = B_TRUE;
55212712Snn35248 		if (atoi(zone_lock_env) == 1)
55222712Snn35248 			zone_lock_cnt = 1;
55232712Snn35248 	}
55242712Snn35248 
55252712Snn35248 	/*
55262712Snn35248 	 * If we are going to be operating on a single zone, retrieve its
55272712Snn35248 	 * brand type and determine whether it is native or not.
55282712Snn35248 	 */
55292712Snn35248 	if ((target_zone != NULL) &&
55302712Snn35248 	    (strcmp(target_zone, GLOBAL_ZONENAME) != NULL)) {
55312712Snn35248 		if (zone_get_brand(target_zone, target_brand,
55322712Snn35248 		    sizeof (target_brand)) != Z_OK) {
55332712Snn35248 			zerror(gettext("missing or invalid brand"));
55342712Snn35248 			exit(Z_ERR);
55352712Snn35248 		}
55362712Snn35248 		is_native_zone = (strcmp(target_brand, NATIVE_BRAND_NAME) == 0);
55372712Snn35248 	}
55382712Snn35248 
55392712Snn35248 	err = parse_and_run(argc - optind, &argv[optind]);
55402712Snn35248 
55412712Snn35248 	return (err);
55420Sstevel@tonic-gate }
5543