xref: /titanic_53/usr/src/cmd/zoneadm/zoneadm.c (revision 99653d4ee642c6528e88224f12409a5f23060994)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5ee519a1fSgjelinek  * Common Development and Distribution License (the "License").
6ee519a1fSgjelinek  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217e362f58Scomay 
227c478bd9Sstevel@tonic-gate /*
23865e09a4Sgjelinek  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate /*
307c478bd9Sstevel@tonic-gate  * zoneadm is a command interpreter for zone administration.  It is all in
317c478bd9Sstevel@tonic-gate  * C (i.e., no lex/yacc), and all the argument passing is argc/argv based.
327c478bd9Sstevel@tonic-gate  * main() calls parse_and_run() which calls cmd_match(), then invokes the
337c478bd9Sstevel@tonic-gate  * appropriate command's handler function.  The rest of the program is the
347c478bd9Sstevel@tonic-gate  * handler functions and their helper functions.
357c478bd9Sstevel@tonic-gate  *
367c478bd9Sstevel@tonic-gate  * Some of the helper functions are used largely to simplify I18N: reducing
377c478bd9Sstevel@tonic-gate  * the need for translation notes.  This is particularly true of many of
387c478bd9Sstevel@tonic-gate  * the zerror() calls: doing e.g. zerror(gettext("%s failed"), "foo") rather
397c478bd9Sstevel@tonic-gate  * than zerror(gettext("foo failed")) with a translation note indicating
407c478bd9Sstevel@tonic-gate  * that "foo" need not be translated.
417c478bd9Sstevel@tonic-gate  */
427c478bd9Sstevel@tonic-gate 
437c478bd9Sstevel@tonic-gate #include <stdio.h>
447c478bd9Sstevel@tonic-gate #include <errno.h>
457c478bd9Sstevel@tonic-gate #include <unistd.h>
467c478bd9Sstevel@tonic-gate #include <signal.h>
477c478bd9Sstevel@tonic-gate #include <stdarg.h>
487c478bd9Sstevel@tonic-gate #include <ctype.h>
497c478bd9Sstevel@tonic-gate #include <stdlib.h>
507c478bd9Sstevel@tonic-gate #include <string.h>
517c478bd9Sstevel@tonic-gate #include <wait.h>
527c478bd9Sstevel@tonic-gate #include <zone.h>
537c478bd9Sstevel@tonic-gate #include <priv.h>
547c478bd9Sstevel@tonic-gate #include <locale.h>
557c478bd9Sstevel@tonic-gate #include <libintl.h>
567c478bd9Sstevel@tonic-gate #include <libzonecfg.h>
577c478bd9Sstevel@tonic-gate #include <bsm/adt.h>
587c478bd9Sstevel@tonic-gate #include <sys/param.h>
597c478bd9Sstevel@tonic-gate #include <sys/types.h>
607c478bd9Sstevel@tonic-gate #include <sys/stat.h>
617c478bd9Sstevel@tonic-gate #include <sys/statvfs.h>
627c478bd9Sstevel@tonic-gate #include <assert.h>
637c478bd9Sstevel@tonic-gate #include <sys/sockio.h>
647c478bd9Sstevel@tonic-gate #include <sys/mntent.h>
657c478bd9Sstevel@tonic-gate #include <limits.h>
660b5de56dSgjelinek #include <dirent.h>
677c478bd9Sstevel@tonic-gate 
687c478bd9Sstevel@tonic-gate #include <fcntl.h>
697c478bd9Sstevel@tonic-gate #include <door.h>
707c478bd9Sstevel@tonic-gate #include <macros.h>
717c478bd9Sstevel@tonic-gate #include <libgen.h>
72865e09a4Sgjelinek #include <fnmatch.h>
73e7f3c547Sgjelinek #include <sys/modctl.h>
747c478bd9Sstevel@tonic-gate 
757c478bd9Sstevel@tonic-gate #include <pool.h>
767c478bd9Sstevel@tonic-gate #include <sys/pool.h>
777c478bd9Sstevel@tonic-gate 
780b5de56dSgjelinek #include "zoneadm.h"
790b5de56dSgjelinek 
807c478bd9Sstevel@tonic-gate #define	MAXARGS	8
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate /* Reflects kernel zone entries */
837c478bd9Sstevel@tonic-gate typedef struct zone_entry {
847c478bd9Sstevel@tonic-gate 	zoneid_t	zid;
857c478bd9Sstevel@tonic-gate 	char		zname[ZONENAME_MAX];
867c478bd9Sstevel@tonic-gate 	char		*zstate_str;
877c478bd9Sstevel@tonic-gate 	zone_state_t	zstate_num;
887c478bd9Sstevel@tonic-gate 	char		zroot[MAXPATHLEN];
897c478bd9Sstevel@tonic-gate } zone_entry_t;
907c478bd9Sstevel@tonic-gate 
917c478bd9Sstevel@tonic-gate static zone_entry_t *zents;
927c478bd9Sstevel@tonic-gate static size_t nzents;
937c478bd9Sstevel@tonic-gate 
941390a385Sgjelinek #define	LOOPBACK_IF	"lo0"
951390a385Sgjelinek #define	SOCKET_AF(af)	(((af) == AF_UNSPEC) ? AF_INET : (af))
961390a385Sgjelinek 
971390a385Sgjelinek struct net_if {
981390a385Sgjelinek 	char	*name;
991390a385Sgjelinek 	int	af;
1001390a385Sgjelinek };
1011390a385Sgjelinek 
1027c478bd9Sstevel@tonic-gate /* 0755 is the default directory mode. */
1037c478bd9Sstevel@tonic-gate #define	DEFAULT_DIR_MODE \
1047c478bd9Sstevel@tonic-gate 	(S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH)
1057c478bd9Sstevel@tonic-gate 
1067c478bd9Sstevel@tonic-gate struct cmd {
1077c478bd9Sstevel@tonic-gate 	uint_t	cmd_num;				/* command number */
1087c478bd9Sstevel@tonic-gate 	char	*cmd_name;				/* command name */
1097c478bd9Sstevel@tonic-gate 	char	*short_usage;				/* short form help */
1107c478bd9Sstevel@tonic-gate 	int	(*handler)(int argc, char *argv[]);	/* function to call */
1117c478bd9Sstevel@tonic-gate 
1127c478bd9Sstevel@tonic-gate };
1137c478bd9Sstevel@tonic-gate 
1147c478bd9Sstevel@tonic-gate #define	SHELP_HELP	"help"
1157c478bd9Sstevel@tonic-gate #define	SHELP_BOOT	"boot [-s]"
1167c478bd9Sstevel@tonic-gate #define	SHELP_HALT	"halt"
1177c478bd9Sstevel@tonic-gate #define	SHELP_READY	"ready"
1187c478bd9Sstevel@tonic-gate #define	SHELP_REBOOT	"reboot"
1197c478bd9Sstevel@tonic-gate #define	SHELP_LIST	"list [-cipv]"
1207c478bd9Sstevel@tonic-gate #define	SHELP_VERIFY	"verify"
1210b5de56dSgjelinek #define	SHELP_INSTALL	"install [-x nodataset]"
1227c478bd9Sstevel@tonic-gate #define	SHELP_UNINSTALL	"uninstall [-F]"
1230b5de56dSgjelinek #define	SHELP_CLONE	"clone [-m method] [-s <ZFS snapshot>] zonename"
124865e09a4Sgjelinek #define	SHELP_MOVE	"move zonepath"
1258cd327d5Sgjelinek #define	SHELP_DETACH	"detach [-n]"
1268cd327d5Sgjelinek #define	SHELP_ATTACH	"attach [-F] [-n <path>]"
1277c478bd9Sstevel@tonic-gate 
1287c478bd9Sstevel@tonic-gate static int help_func(int argc, char *argv[]);
1297c478bd9Sstevel@tonic-gate static int ready_func(int argc, char *argv[]);
1307c478bd9Sstevel@tonic-gate static int boot_func(int argc, char *argv[]);
1317c478bd9Sstevel@tonic-gate static int halt_func(int argc, char *argv[]);
1327c478bd9Sstevel@tonic-gate static int reboot_func(int argc, char *argv[]);
1337c478bd9Sstevel@tonic-gate static int list_func(int argc, char *argv[]);
1347c478bd9Sstevel@tonic-gate static int verify_func(int argc, char *argv[]);
1357c478bd9Sstevel@tonic-gate static int install_func(int argc, char *argv[]);
1367c478bd9Sstevel@tonic-gate static int uninstall_func(int argc, char *argv[]);
137108322fbScarlsonj static int mount_func(int argc, char *argv[]);
138108322fbScarlsonj static int unmount_func(int argc, char *argv[]);
139865e09a4Sgjelinek static int clone_func(int argc, char *argv[]);
140865e09a4Sgjelinek static int move_func(int argc, char *argv[]);
141ee519a1fSgjelinek static int detach_func(int argc, char *argv[]);
142ee519a1fSgjelinek static int attach_func(int argc, char *argv[]);
1437c478bd9Sstevel@tonic-gate static int sanity_check(char *zone, int cmd_num, boolean_t running,
1447c478bd9Sstevel@tonic-gate     boolean_t unsafe_when_running);
1457c478bd9Sstevel@tonic-gate static int cmd_match(char *cmd);
1467c478bd9Sstevel@tonic-gate static int verify_details(int);
1477c478bd9Sstevel@tonic-gate 
1487c478bd9Sstevel@tonic-gate static struct cmd cmdtab[] = {
1497c478bd9Sstevel@tonic-gate 	{ CMD_HELP,		"help",		SHELP_HELP,	help_func },
1507c478bd9Sstevel@tonic-gate 	{ CMD_BOOT,		"boot",		SHELP_BOOT,	boot_func },
1517c478bd9Sstevel@tonic-gate 	{ CMD_HALT,		"halt",		SHELP_HALT,	halt_func },
1527c478bd9Sstevel@tonic-gate 	{ CMD_READY,		"ready",	SHELP_READY,	ready_func },
1537c478bd9Sstevel@tonic-gate 	{ CMD_REBOOT,		"reboot",	SHELP_REBOOT,	reboot_func },
1547c478bd9Sstevel@tonic-gate 	{ CMD_LIST,		"list",		SHELP_LIST,	list_func },
1557c478bd9Sstevel@tonic-gate 	{ CMD_VERIFY,		"verify",	SHELP_VERIFY,	verify_func },
1567c478bd9Sstevel@tonic-gate 	{ CMD_INSTALL,		"install",	SHELP_INSTALL,	install_func },
1577c478bd9Sstevel@tonic-gate 	{ CMD_UNINSTALL,	"uninstall",	SHELP_UNINSTALL,
158108322fbScarlsonj 	    uninstall_func },
159865e09a4Sgjelinek 	/* mount and unmount are private commands for admin/install */
160108322fbScarlsonj 	{ CMD_MOUNT,		"mount",	NULL,		mount_func },
161865e09a4Sgjelinek 	{ CMD_UNMOUNT,		"unmount",	NULL,		unmount_func },
162865e09a4Sgjelinek 	{ CMD_CLONE,		"clone",	SHELP_CLONE,	clone_func },
163ee519a1fSgjelinek 	{ CMD_MOVE,		"move",		SHELP_MOVE,	move_func },
164ee519a1fSgjelinek 	{ CMD_DETACH,		"detach",	SHELP_DETACH,	detach_func },
165ee519a1fSgjelinek 	{ CMD_ATTACH,		"attach",	SHELP_ATTACH,	attach_func }
1667c478bd9Sstevel@tonic-gate };
1677c478bd9Sstevel@tonic-gate 
1687c478bd9Sstevel@tonic-gate /* global variables */
1697c478bd9Sstevel@tonic-gate 
1707c478bd9Sstevel@tonic-gate /* set early in main(), never modified thereafter, used all over the place */
1717c478bd9Sstevel@tonic-gate static char *execname;
1727c478bd9Sstevel@tonic-gate static char *locale;
1730b5de56dSgjelinek char *target_zone;
1747c478bd9Sstevel@tonic-gate 
1757c478bd9Sstevel@tonic-gate /* used in do_subproc() and signal handler */
1767c478bd9Sstevel@tonic-gate static volatile boolean_t child_killed;
1777c478bd9Sstevel@tonic-gate 
1780b5de56dSgjelinek char *
1797c478bd9Sstevel@tonic-gate cmd_to_str(int cmd_num)
1807c478bd9Sstevel@tonic-gate {
1817c478bd9Sstevel@tonic-gate 	assert(cmd_num >= CMD_MIN && cmd_num <= CMD_MAX);
1827c478bd9Sstevel@tonic-gate 	return (cmdtab[cmd_num].cmd_name);
1837c478bd9Sstevel@tonic-gate }
1847c478bd9Sstevel@tonic-gate 
1857c478bd9Sstevel@tonic-gate /* This is a separate function because of gettext() wrapping. */
1867c478bd9Sstevel@tonic-gate static char *
1877c478bd9Sstevel@tonic-gate long_help(int cmd_num)
1887c478bd9Sstevel@tonic-gate {
1897e362f58Scomay 	assert(cmd_num >= CMD_MIN && cmd_num <= CMD_MAX);
1907c478bd9Sstevel@tonic-gate 	switch (cmd_num) {
1917c478bd9Sstevel@tonic-gate 	case CMD_HELP:
1927c478bd9Sstevel@tonic-gate 		return (gettext("Print usage message."));
1937c478bd9Sstevel@tonic-gate 	case CMD_BOOT:
1947c478bd9Sstevel@tonic-gate 		return (gettext("Activates (boots) specified zone.  "
1957c478bd9Sstevel@tonic-gate 		    "The -s flag can be used\n\tto boot the zone in "
1967c478bd9Sstevel@tonic-gate 		    "the single-user state."));
1977c478bd9Sstevel@tonic-gate 	case CMD_HALT:
1989e518655Sgjelinek 		return (gettext("Halts specified zone, bypassing shutdown "
1999e518655Sgjelinek 		    "scripts and removing runtime\n\tresources of the zone."));
2007c478bd9Sstevel@tonic-gate 	case CMD_READY:
2019e518655Sgjelinek 		return (gettext("Prepares a zone for running applications but "
2029e518655Sgjelinek 		    "does not start any user\n\tprocesses in the zone."));
2037c478bd9Sstevel@tonic-gate 	case CMD_REBOOT:
2049e518655Sgjelinek 		return (gettext("Restarts the zone (equivalent to a halt / "
2059e518655Sgjelinek 		    "boot sequence).\n\tFails if the zone is not active."));
2067c478bd9Sstevel@tonic-gate 	case CMD_LIST:
2077c478bd9Sstevel@tonic-gate 		return (gettext("Lists the current zones, or a "
2087c478bd9Sstevel@tonic-gate 		    "specific zone if indicated.  By default,\n\tall "
2097c478bd9Sstevel@tonic-gate 		    "running zones are listed, though this can be "
2107c478bd9Sstevel@tonic-gate 		    "expanded to all\n\tinstalled zones with the -i "
2117c478bd9Sstevel@tonic-gate 		    "option or all configured zones with the\n\t-c "
2127c478bd9Sstevel@tonic-gate 		    "option.  When used with the general -z <zone> "
2137c478bd9Sstevel@tonic-gate 		    "option, lists only the\n\tspecified zone, but "
2147c478bd9Sstevel@tonic-gate 		    "lists it regardless of its state, and the -i "
2157c478bd9Sstevel@tonic-gate 		    "and -c\n\toptions are disallowed.  The -v option "
2167c478bd9Sstevel@tonic-gate 		    "can be used to display verbose\n\tinformation: "
2177c478bd9Sstevel@tonic-gate 		    "zone name, id, current state, root directory and "
2187c478bd9Sstevel@tonic-gate 		    "options.\n\tThe -p option can be used to request "
2197c478bd9Sstevel@tonic-gate 		    "machine-parsable output.  The -v\n\tand -p "
2207c478bd9Sstevel@tonic-gate 		    "options are mutually exclusive.  If neither -v "
2219e518655Sgjelinek 		    "nor -p is used,\n\tjust the zone name is listed."));
2227c478bd9Sstevel@tonic-gate 	case CMD_VERIFY:
2237c478bd9Sstevel@tonic-gate 		return (gettext("Check to make sure the configuration "
2247c478bd9Sstevel@tonic-gate 		    "can safely be instantiated\n\ton the machine: "
2257c478bd9Sstevel@tonic-gate 		    "physical network interfaces exist, etc."));
2267c478bd9Sstevel@tonic-gate 	case CMD_INSTALL:
2270b5de56dSgjelinek 		return (gettext("Install the configuration on to the system.  "
2280b5de56dSgjelinek 		    "The -x nodataset option\n\tcan be used to prevent the "
2290b5de56dSgjelinek 		    "creation of a new ZFS file system for the\n\tzone "
2300b5de56dSgjelinek 		    "(assuming the zonepath is within a ZFS file system)."));
2317c478bd9Sstevel@tonic-gate 	case CMD_UNINSTALL:
2329e518655Sgjelinek 		return (gettext("Uninstall the configuration from the system.  "
2339e518655Sgjelinek 		    "The -F flag can be used\n\tto force the action."));
234865e09a4Sgjelinek 	case CMD_CLONE:
2350b5de56dSgjelinek 		return (gettext("Clone the installation of another zone.  "
2360b5de56dSgjelinek 		    "The -m option can be used to\n\tspecify 'copy' which "
2370b5de56dSgjelinek 		    "forces a copy of the source zone.  The -s option\n\t"
2380b5de56dSgjelinek 		    "can be used to specify the name of a ZFS snapshot "
2390b5de56dSgjelinek 		    "that was taken from\n\ta previous clone command.  The "
2400b5de56dSgjelinek 		    "snapshot will be used as the source\n\tinstead of "
2410b5de56dSgjelinek 		    "creating a new ZFS snapshot."));
242865e09a4Sgjelinek 	case CMD_MOVE:
243865e09a4Sgjelinek 		return (gettext("Move the zone to a new zonepath."));
2449e518655Sgjelinek 	case CMD_DETACH:
2459e518655Sgjelinek 		return (gettext("Detach the zone from the system. The zone "
2469e518655Sgjelinek 		    "state is changed to\n\t'configured' (but the files under "
2479e518655Sgjelinek 		    "the zonepath are untouched).\n\tThe zone can subsequently "
2489e518655Sgjelinek 		    "be attached, or can be moved to another\n\tsystem and "
2498cd327d5Sgjelinek 		    "attached there.  The -n option can be used to specify\n\t"
2508cd327d5Sgjelinek 		    "'no-execute' mode.  When -n is used, the information "
2518cd327d5Sgjelinek 		    "needed to attach\n\tthe zone is sent to standard output "
2528cd327d5Sgjelinek 		    "but the zone is not actually\n\tdetached."));
2539e518655Sgjelinek 	case CMD_ATTACH:
2549e518655Sgjelinek 		return (gettext("Attach the zone to the system.  The zone "
2559e518655Sgjelinek 		    "state must be 'configured'\n\tprior to attach; upon "
2569e518655Sgjelinek 		    "successful completion, the zone state will be\n\t"
2579e518655Sgjelinek 		    "'installed'.  The system software on the current "
2589e518655Sgjelinek 		    "system must be\n\tcompatible with the software on the "
2599e518655Sgjelinek 		    "zone's original system.\n\tSpecify -F to force the attach "
2608cd327d5Sgjelinek 		    "and skip software compatibility tests.\n\tThe -n option "
2618cd327d5Sgjelinek 		    "can be used to specify 'no-execute' mode.  When -n is\n\t"
2628cd327d5Sgjelinek 		    "used, the information needed to attach the zone is read "
2638cd327d5Sgjelinek 		    "from the\n\tspecified path and the configuration is only "
2648cd327d5Sgjelinek 		    "validated.  The path can\n\tbe '-' to specify standard "
2658cd327d5Sgjelinek 		    "input."));
266108322fbScarlsonj 	default:
267108322fbScarlsonj 		return ("");
2687c478bd9Sstevel@tonic-gate 	}
2697c478bd9Sstevel@tonic-gate 	/* NOTREACHED */
2707e362f58Scomay 	return (NULL);
2717c478bd9Sstevel@tonic-gate }
2727c478bd9Sstevel@tonic-gate 
2737c478bd9Sstevel@tonic-gate /*
2747c478bd9Sstevel@tonic-gate  * Called with explicit B_TRUE when help is explicitly requested, B_FALSE for
2757c478bd9Sstevel@tonic-gate  * unexpected errors.
2767c478bd9Sstevel@tonic-gate  */
2777c478bd9Sstevel@tonic-gate 
2787c478bd9Sstevel@tonic-gate static int
2797c478bd9Sstevel@tonic-gate usage(boolean_t explicit)
2807c478bd9Sstevel@tonic-gate {
2817c478bd9Sstevel@tonic-gate 	int i;
2827c478bd9Sstevel@tonic-gate 	FILE *fd = explicit ? stdout : stderr;
2837c478bd9Sstevel@tonic-gate 
2847c478bd9Sstevel@tonic-gate 	(void) fprintf(fd, "%s:\t%s help\n", gettext("usage"), execname);
2857c478bd9Sstevel@tonic-gate 	(void) fprintf(fd, "\t%s [-z <zone>] list\n", execname);
2867c478bd9Sstevel@tonic-gate 	(void) fprintf(fd, "\t%s -z <zone> <%s>\n", execname,
2877c478bd9Sstevel@tonic-gate 	    gettext("subcommand"));
2887c478bd9Sstevel@tonic-gate 	(void) fprintf(fd, "\n%s:\n\n", gettext("Subcommands"));
2897c478bd9Sstevel@tonic-gate 	for (i = CMD_MIN; i <= CMD_MAX; i++) {
290108322fbScarlsonj 		if (cmdtab[i].short_usage == NULL)
291108322fbScarlsonj 			continue;
2927c478bd9Sstevel@tonic-gate 		(void) fprintf(fd, "%s\n", cmdtab[i].short_usage);
2937c478bd9Sstevel@tonic-gate 		if (explicit)
2947c478bd9Sstevel@tonic-gate 			(void) fprintf(fd, "\t%s\n\n", long_help(i));
2957c478bd9Sstevel@tonic-gate 	}
2967c478bd9Sstevel@tonic-gate 	if (!explicit)
2977c478bd9Sstevel@tonic-gate 		(void) fputs("\n", fd);
2987c478bd9Sstevel@tonic-gate 	return (Z_USAGE);
2997c478bd9Sstevel@tonic-gate }
3007c478bd9Sstevel@tonic-gate 
3017c478bd9Sstevel@tonic-gate static void
3027c478bd9Sstevel@tonic-gate sub_usage(char *short_usage, int cmd_num)
3037c478bd9Sstevel@tonic-gate {
3047c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "%s:\t%s\n", gettext("usage"), short_usage);
3057c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "\t%s\n", long_help(cmd_num));
3067c478bd9Sstevel@tonic-gate }
3077c478bd9Sstevel@tonic-gate 
3087c478bd9Sstevel@tonic-gate /*
3097c478bd9Sstevel@tonic-gate  * zperror() is like perror(3c) except that this also prints the executable
3107c478bd9Sstevel@tonic-gate  * name at the start of the message, and takes a boolean indicating whether
3117c478bd9Sstevel@tonic-gate  * to call libc'c strerror() or that from libzonecfg.
3127c478bd9Sstevel@tonic-gate  */
3137c478bd9Sstevel@tonic-gate 
3140b5de56dSgjelinek void
3157c478bd9Sstevel@tonic-gate zperror(const char *str, boolean_t zonecfg_error)
3167c478bd9Sstevel@tonic-gate {
3177c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "%s: %s: %s\n", execname, str,
3187c478bd9Sstevel@tonic-gate 	    zonecfg_error ? zonecfg_strerror(errno) : strerror(errno));
3197c478bd9Sstevel@tonic-gate }
3207c478bd9Sstevel@tonic-gate 
3217c478bd9Sstevel@tonic-gate /*
3227c478bd9Sstevel@tonic-gate  * zperror2() is very similar to zperror() above, except it also prints a
3237c478bd9Sstevel@tonic-gate  * supplied zone name after the executable.
3247c478bd9Sstevel@tonic-gate  *
3257c478bd9Sstevel@tonic-gate  * All current consumers of this function want libzonecfg's strerror() rather
3267c478bd9Sstevel@tonic-gate  * than libc's; if this ever changes, this function can be made more generic
3277c478bd9Sstevel@tonic-gate  * like zperror() above.
3287c478bd9Sstevel@tonic-gate  */
3297c478bd9Sstevel@tonic-gate 
3300b5de56dSgjelinek void
3317c478bd9Sstevel@tonic-gate zperror2(const char *zone, const char *str)
3327c478bd9Sstevel@tonic-gate {
3337c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "%s: %s: %s: %s\n", execname, zone, str,
3347c478bd9Sstevel@tonic-gate 	    zonecfg_strerror(errno));
3357c478bd9Sstevel@tonic-gate }
3367c478bd9Sstevel@tonic-gate 
3377c478bd9Sstevel@tonic-gate /* PRINTFLIKE1 */
3380b5de56dSgjelinek void
3397c478bd9Sstevel@tonic-gate zerror(const char *fmt, ...)
3407c478bd9Sstevel@tonic-gate {
3417c478bd9Sstevel@tonic-gate 	va_list alist;
3427c478bd9Sstevel@tonic-gate 
3437c478bd9Sstevel@tonic-gate 	va_start(alist, fmt);
3447c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "%s: ", execname);
3457c478bd9Sstevel@tonic-gate 	if (target_zone != NULL)
3467c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "zone '%s': ", target_zone);
3477c478bd9Sstevel@tonic-gate 	(void) vfprintf(stderr, fmt, alist);
3487c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "\n");
3497c478bd9Sstevel@tonic-gate 	va_end(alist);
3507c478bd9Sstevel@tonic-gate }
3517c478bd9Sstevel@tonic-gate 
3527c478bd9Sstevel@tonic-gate static void *
3537c478bd9Sstevel@tonic-gate safe_calloc(size_t nelem, size_t elsize)
3547c478bd9Sstevel@tonic-gate {
3557c478bd9Sstevel@tonic-gate 	void *r = calloc(nelem, elsize);
3567c478bd9Sstevel@tonic-gate 
3577c478bd9Sstevel@tonic-gate 	if (r == NULL) {
3587c478bd9Sstevel@tonic-gate 		zerror(gettext("failed to allocate %lu bytes: %s"),
3597c478bd9Sstevel@tonic-gate 		    (ulong_t)nelem * elsize, strerror(errno));
3607c478bd9Sstevel@tonic-gate 		exit(Z_ERR);
3617c478bd9Sstevel@tonic-gate 	}
3627c478bd9Sstevel@tonic-gate 	return (r);
3637c478bd9Sstevel@tonic-gate }
3647c478bd9Sstevel@tonic-gate 
3657c478bd9Sstevel@tonic-gate static void
3667c478bd9Sstevel@tonic-gate zone_print(zone_entry_t *zent, boolean_t verbose, boolean_t parsable)
3677c478bd9Sstevel@tonic-gate {
3687c478bd9Sstevel@tonic-gate 	static boolean_t firsttime = B_TRUE;
3697c478bd9Sstevel@tonic-gate 
3707c478bd9Sstevel@tonic-gate 	assert(!(verbose && parsable));
3717c478bd9Sstevel@tonic-gate 	if (firsttime && verbose) {
3727c478bd9Sstevel@tonic-gate 		firsttime = B_FALSE;
3737c478bd9Sstevel@tonic-gate 		(void) printf("%*s %-16s %-14s %-30s\n", ZONEID_WIDTH, "ID",
3747c478bd9Sstevel@tonic-gate 		    "NAME", "STATUS", "PATH");
3757c478bd9Sstevel@tonic-gate 	}
3767c478bd9Sstevel@tonic-gate 	if (!verbose) {
3777c478bd9Sstevel@tonic-gate 		if (!parsable) {
3787c478bd9Sstevel@tonic-gate 			(void) printf("%s\n", zent->zname);
3797c478bd9Sstevel@tonic-gate 			return;
3807c478bd9Sstevel@tonic-gate 		}
3817c478bd9Sstevel@tonic-gate 		if (zent->zid == ZONE_ID_UNDEFINED)
3827c478bd9Sstevel@tonic-gate 			(void) printf("-");
3837c478bd9Sstevel@tonic-gate 		else
3847c478bd9Sstevel@tonic-gate 			(void) printf("%lu", zent->zid);
3857c478bd9Sstevel@tonic-gate 		(void) printf(":%s:%s:%s\n", zent->zname, zent->zstate_str,
3867c478bd9Sstevel@tonic-gate 		    zent->zroot);
3877c478bd9Sstevel@tonic-gate 		return;
3887c478bd9Sstevel@tonic-gate 	}
3897c478bd9Sstevel@tonic-gate 	if (zent->zstate_str != NULL) {
3907c478bd9Sstevel@tonic-gate 		if (zent->zid == ZONE_ID_UNDEFINED)
3917c478bd9Sstevel@tonic-gate 			(void) printf("%*s", ZONEID_WIDTH, "-");
3927c478bd9Sstevel@tonic-gate 		else
3937c478bd9Sstevel@tonic-gate 			(void) printf("%*lu", ZONEID_WIDTH, zent->zid);
3947c478bd9Sstevel@tonic-gate 		(void) printf(" %-16s %-14s %-30s\n", zent->zname,
3957c478bd9Sstevel@tonic-gate 		    zent->zstate_str, zent->zroot);
3967c478bd9Sstevel@tonic-gate 	}
3977c478bd9Sstevel@tonic-gate }
3987c478bd9Sstevel@tonic-gate 
3997c478bd9Sstevel@tonic-gate static int
400108322fbScarlsonj lookup_zone_info(const char *zone_name, zoneid_t zid, zone_entry_t *zent)
4017c478bd9Sstevel@tonic-gate {
40245916cd2Sjpk 	char root[MAXPATHLEN], *cp;
4037c478bd9Sstevel@tonic-gate 	int err;
4047c478bd9Sstevel@tonic-gate 
4057c478bd9Sstevel@tonic-gate 	(void) strlcpy(zent->zname, zone_name, sizeof (zent->zname));
4067c478bd9Sstevel@tonic-gate 	(void) strlcpy(zent->zroot, "???", sizeof (zent->zroot));
4077c478bd9Sstevel@tonic-gate 	zent->zstate_str = "???";
4087c478bd9Sstevel@tonic-gate 
409108322fbScarlsonj 	zent->zid = zid;
4107c478bd9Sstevel@tonic-gate 
41145916cd2Sjpk 	/*
41245916cd2Sjpk 	 * For labeled zones which query the zone path of lower-level
41345916cd2Sjpk 	 * zones, the path needs to be adjusted to drop the final
41445916cd2Sjpk 	 * "/root" component. This adjusted path is then useful
41545916cd2Sjpk 	 * for reading down any exported directories from the
41645916cd2Sjpk 	 * lower-level zone.
41745916cd2Sjpk 	 */
41845916cd2Sjpk 	if (is_system_labeled() && zent->zid != ZONE_ID_UNDEFINED) {
41945916cd2Sjpk 		if (zone_getattr(zent->zid, ZONE_ATTR_ROOT, zent->zroot,
42045916cd2Sjpk 		    sizeof (zent->zroot)) == -1) {
42145916cd2Sjpk 			zperror2(zent->zname,
42245916cd2Sjpk 			    gettext("could not get zone path."));
42345916cd2Sjpk 			return (Z_ERR);
42445916cd2Sjpk 		}
42545916cd2Sjpk 		cp = zent->zroot + strlen(zent->zroot) - 5;
42645916cd2Sjpk 		if (cp > zent->zroot && strcmp(cp, "/root") == 0)
42745916cd2Sjpk 			*cp = 0;
42845916cd2Sjpk 	} else {
42945916cd2Sjpk 		if ((err = zone_get_zonepath(zent->zname, root,
43045916cd2Sjpk 		    sizeof (root))) != Z_OK) {
4317c478bd9Sstevel@tonic-gate 			errno = err;
43245916cd2Sjpk 			zperror2(zent->zname,
43345916cd2Sjpk 			    gettext("could not get zone path."));
4347c478bd9Sstevel@tonic-gate 			return (Z_ERR);
4357c478bd9Sstevel@tonic-gate 		}
4367c478bd9Sstevel@tonic-gate 		(void) strlcpy(zent->zroot, root, sizeof (zent->zroot));
43745916cd2Sjpk 	}
4387c478bd9Sstevel@tonic-gate 
4397c478bd9Sstevel@tonic-gate 	if ((err = zone_get_state(zent->zname, &zent->zstate_num)) != Z_OK) {
4407c478bd9Sstevel@tonic-gate 		errno = err;
4417c478bd9Sstevel@tonic-gate 		zperror2(zent->zname, gettext("could not get state"));
4427c478bd9Sstevel@tonic-gate 		return (Z_ERR);
4437c478bd9Sstevel@tonic-gate 	}
4447c478bd9Sstevel@tonic-gate 	zent->zstate_str = zone_state_str(zent->zstate_num);
4457c478bd9Sstevel@tonic-gate 
4467c478bd9Sstevel@tonic-gate 	return (Z_OK);
4477c478bd9Sstevel@tonic-gate }
4487c478bd9Sstevel@tonic-gate 
4497c478bd9Sstevel@tonic-gate /*
4507c478bd9Sstevel@tonic-gate  * fetch_zents() calls zone_list(2) to find out how many zones are running
4517c478bd9Sstevel@tonic-gate  * (which is stored in the global nzents), then calls zone_list(2) again
4527c478bd9Sstevel@tonic-gate  * to fetch the list of running zones (stored in the global zents).  This
4537c478bd9Sstevel@tonic-gate  * function may be called multiple times, so if zents is already set, we
4547c478bd9Sstevel@tonic-gate  * return immediately to save work.
4557c478bd9Sstevel@tonic-gate  */
4567c478bd9Sstevel@tonic-gate 
4577c478bd9Sstevel@tonic-gate static int
458108322fbScarlsonj fetch_zents(void)
4597c478bd9Sstevel@tonic-gate {
4607c478bd9Sstevel@tonic-gate 	zoneid_t *zids = NULL;
4617c478bd9Sstevel@tonic-gate 	uint_t nzents_saved;
462108322fbScarlsonj 	int i, retv;
463108322fbScarlsonj 	FILE *fp;
464108322fbScarlsonj 	boolean_t inaltroot;
465108322fbScarlsonj 	zone_entry_t *zentp;
4667c478bd9Sstevel@tonic-gate 
4677c478bd9Sstevel@tonic-gate 	if (nzents > 0)
4687c478bd9Sstevel@tonic-gate 		return (Z_OK);
4697c478bd9Sstevel@tonic-gate 
4707c478bd9Sstevel@tonic-gate 	if (zone_list(NULL, &nzents) != 0) {
4717c478bd9Sstevel@tonic-gate 		zperror(gettext("failed to get zoneid list"), B_FALSE);
4727c478bd9Sstevel@tonic-gate 		return (Z_ERR);
4737c478bd9Sstevel@tonic-gate 	}
4747c478bd9Sstevel@tonic-gate 
4757c478bd9Sstevel@tonic-gate again:
4767c478bd9Sstevel@tonic-gate 	if (nzents == 0)
4777c478bd9Sstevel@tonic-gate 		return (Z_OK);
4787c478bd9Sstevel@tonic-gate 
4797c478bd9Sstevel@tonic-gate 	zids = safe_calloc(nzents, sizeof (zoneid_t));
4807c478bd9Sstevel@tonic-gate 	nzents_saved = nzents;
4817c478bd9Sstevel@tonic-gate 
4827c478bd9Sstevel@tonic-gate 	if (zone_list(zids, &nzents) != 0) {
4837c478bd9Sstevel@tonic-gate 		zperror(gettext("failed to get zone list"), B_FALSE);
4847c478bd9Sstevel@tonic-gate 		free(zids);
4857c478bd9Sstevel@tonic-gate 		return (Z_ERR);
4867c478bd9Sstevel@tonic-gate 	}
4877c478bd9Sstevel@tonic-gate 	if (nzents != nzents_saved) {
4887c478bd9Sstevel@tonic-gate 		/* list changed, try again */
4897c478bd9Sstevel@tonic-gate 		free(zids);
4907c478bd9Sstevel@tonic-gate 		goto again;
4917c478bd9Sstevel@tonic-gate 	}
4927c478bd9Sstevel@tonic-gate 
4937c478bd9Sstevel@tonic-gate 	zents = safe_calloc(nzents, sizeof (zone_entry_t));
4947c478bd9Sstevel@tonic-gate 
495108322fbScarlsonj 	inaltroot = zonecfg_in_alt_root();
496108322fbScarlsonj 	if (inaltroot)
497108322fbScarlsonj 		fp = zonecfg_open_scratch("", B_FALSE);
498108322fbScarlsonj 	else
499108322fbScarlsonj 		fp = NULL;
500108322fbScarlsonj 	zentp = zents;
501108322fbScarlsonj 	retv = Z_OK;
5027c478bd9Sstevel@tonic-gate 	for (i = 0; i < nzents; i++) {
5037c478bd9Sstevel@tonic-gate 		char name[ZONENAME_MAX];
504108322fbScarlsonj 		char altname[ZONENAME_MAX];
5057c478bd9Sstevel@tonic-gate 
506108322fbScarlsonj 		if (getzonenamebyid(zids[i], name, sizeof (name)) < 0) {
5077c478bd9Sstevel@tonic-gate 			zperror(gettext("failed to get zone name"), B_FALSE);
508108322fbScarlsonj 			retv = Z_ERR;
509108322fbScarlsonj 			continue;
5107c478bd9Sstevel@tonic-gate 		}
511108322fbScarlsonj 		if (zonecfg_is_scratch(name)) {
512108322fbScarlsonj 			/* Ignore scratch zones by default */
513108322fbScarlsonj 			if (!inaltroot)
514108322fbScarlsonj 				continue;
515108322fbScarlsonj 			if (fp == NULL ||
516108322fbScarlsonj 			    zonecfg_reverse_scratch(fp, name, altname,
517108322fbScarlsonj 			    sizeof (altname), NULL, 0) == -1) {
5185ee84fbdSgjelinek 				zerror(gettext("could not resolve scratch "
519108322fbScarlsonj 				    "zone %s"), name);
520108322fbScarlsonj 				retv = Z_ERR;
521108322fbScarlsonj 				continue;
522108322fbScarlsonj 			}
523108322fbScarlsonj 			(void) strcpy(name, altname);
524108322fbScarlsonj 		} else {
525108322fbScarlsonj 			/* Ignore non-scratch when in an alternate root */
526108322fbScarlsonj 			if (inaltroot && strcmp(name, GLOBAL_ZONENAME) != 0)
527108322fbScarlsonj 				continue;
528108322fbScarlsonj 		}
529108322fbScarlsonj 		if (lookup_zone_info(name, zids[i], zentp) != Z_OK) {
530108322fbScarlsonj 			zerror(gettext("failed to get zone data"));
531108322fbScarlsonj 			retv = Z_ERR;
532108322fbScarlsonj 			continue;
533108322fbScarlsonj 		}
534108322fbScarlsonj 		zentp++;
535108322fbScarlsonj 	}
536108322fbScarlsonj 	nzents = zentp - zents;
537108322fbScarlsonj 	if (fp != NULL)
538108322fbScarlsonj 		zonecfg_close_scratch(fp);
5397c478bd9Sstevel@tonic-gate 
5407c478bd9Sstevel@tonic-gate 	free(zids);
541108322fbScarlsonj 	return (retv);
5427c478bd9Sstevel@tonic-gate }
5437c478bd9Sstevel@tonic-gate 
544108322fbScarlsonj static int
5457c478bd9Sstevel@tonic-gate zone_print_list(zone_state_t min_state, boolean_t verbose, boolean_t parsable)
5467c478bd9Sstevel@tonic-gate {
5477c478bd9Sstevel@tonic-gate 	int i;
5487c478bd9Sstevel@tonic-gate 	zone_entry_t zent;
5497c478bd9Sstevel@tonic-gate 	FILE *cookie;
5507c478bd9Sstevel@tonic-gate 	char *name;
5517c478bd9Sstevel@tonic-gate 
5527c478bd9Sstevel@tonic-gate 	/*
5537c478bd9Sstevel@tonic-gate 	 * First get the list of running zones from the kernel and print them.
5547c478bd9Sstevel@tonic-gate 	 * If that is all we need, then return.
5557c478bd9Sstevel@tonic-gate 	 */
556108322fbScarlsonj 	if ((i = fetch_zents()) != Z_OK) {
5577c478bd9Sstevel@tonic-gate 		/*
5587c478bd9Sstevel@tonic-gate 		 * No need for error messages; fetch_zents() has already taken
5597c478bd9Sstevel@tonic-gate 		 * care of this.
5607c478bd9Sstevel@tonic-gate 		 */
561108322fbScarlsonj 		return (i);
5627c478bd9Sstevel@tonic-gate 	}
563108322fbScarlsonj 	for (i = 0; i < nzents; i++)
5647c478bd9Sstevel@tonic-gate 		zone_print(&zents[i], verbose, parsable);
5657c478bd9Sstevel@tonic-gate 	if (min_state >= ZONE_STATE_RUNNING)
566108322fbScarlsonj 		return (Z_OK);
5677c478bd9Sstevel@tonic-gate 	/*
5687c478bd9Sstevel@tonic-gate 	 * Next, get the full list of zones from the configuration, skipping
5697c478bd9Sstevel@tonic-gate 	 * any we have already printed.
5707c478bd9Sstevel@tonic-gate 	 */
5717c478bd9Sstevel@tonic-gate 	cookie = setzoneent();
5727c478bd9Sstevel@tonic-gate 	while ((name = getzoneent(cookie)) != NULL) {
5737c478bd9Sstevel@tonic-gate 		for (i = 0; i < nzents; i++) {
5747c478bd9Sstevel@tonic-gate 			if (strcmp(zents[i].zname, name) == 0)
5757c478bd9Sstevel@tonic-gate 				break;
5767c478bd9Sstevel@tonic-gate 		}
5777c478bd9Sstevel@tonic-gate 		if (i < nzents) {
5787c478bd9Sstevel@tonic-gate 			free(name);
5797c478bd9Sstevel@tonic-gate 			continue;
5807c478bd9Sstevel@tonic-gate 		}
581108322fbScarlsonj 		if (lookup_zone_info(name, ZONE_ID_UNDEFINED, &zent) != Z_OK) {
5827c478bd9Sstevel@tonic-gate 			free(name);
5837c478bd9Sstevel@tonic-gate 			continue;
5847c478bd9Sstevel@tonic-gate 		}
5857c478bd9Sstevel@tonic-gate 		free(name);
5867c478bd9Sstevel@tonic-gate 		if (zent.zstate_num >= min_state)
5877c478bd9Sstevel@tonic-gate 			zone_print(&zent, verbose, parsable);
5887c478bd9Sstevel@tonic-gate 	}
5897c478bd9Sstevel@tonic-gate 	endzoneent(cookie);
590108322fbScarlsonj 	return (Z_OK);
5917c478bd9Sstevel@tonic-gate }
5927c478bd9Sstevel@tonic-gate 
5937c478bd9Sstevel@tonic-gate static zone_entry_t *
5947c478bd9Sstevel@tonic-gate lookup_running_zone(char *str)
5957c478bd9Sstevel@tonic-gate {
5967c478bd9Sstevel@tonic-gate 	zoneid_t zoneid;
5977c478bd9Sstevel@tonic-gate 	char *cp;
5987c478bd9Sstevel@tonic-gate 	int i;
5997c478bd9Sstevel@tonic-gate 
6007c478bd9Sstevel@tonic-gate 	if (fetch_zents() != Z_OK)
6017c478bd9Sstevel@tonic-gate 		return (NULL);
6027c478bd9Sstevel@tonic-gate 
6037c478bd9Sstevel@tonic-gate 	for (i = 0; i < nzents; i++) {
6047c478bd9Sstevel@tonic-gate 		if (strcmp(str, zents[i].zname) == 0)
6057c478bd9Sstevel@tonic-gate 			return (&zents[i]);
6067c478bd9Sstevel@tonic-gate 	}
6077c478bd9Sstevel@tonic-gate 	errno = 0;
6087c478bd9Sstevel@tonic-gate 	zoneid = strtol(str, &cp, 0);
6097c478bd9Sstevel@tonic-gate 	if (zoneid < MIN_ZONEID || zoneid > MAX_ZONEID ||
6107c478bd9Sstevel@tonic-gate 	    errno != 0 || *cp != '\0')
6117c478bd9Sstevel@tonic-gate 		return (NULL);
6127c478bd9Sstevel@tonic-gate 	for (i = 0; i < nzents; i++) {
6137c478bd9Sstevel@tonic-gate 		if (zoneid == zents[i].zid)
6147c478bd9Sstevel@tonic-gate 			return (&zents[i]);
6157c478bd9Sstevel@tonic-gate 	}
6167c478bd9Sstevel@tonic-gate 	return (NULL);
6177c478bd9Sstevel@tonic-gate }
6187c478bd9Sstevel@tonic-gate 
6197c478bd9Sstevel@tonic-gate /*
6207c478bd9Sstevel@tonic-gate  * Check a bit in a mode_t: if on is B_TRUE, that bit should be on; if
6217c478bd9Sstevel@tonic-gate  * B_FALSE, it should be off.  Return B_TRUE if the mode is bad (incorrect).
6227c478bd9Sstevel@tonic-gate  */
6237c478bd9Sstevel@tonic-gate static boolean_t
6247c478bd9Sstevel@tonic-gate bad_mode_bit(mode_t mode, mode_t bit, boolean_t on, char *file)
6257c478bd9Sstevel@tonic-gate {
6267c478bd9Sstevel@tonic-gate 	char *str;
6277c478bd9Sstevel@tonic-gate 
6287c478bd9Sstevel@tonic-gate 	assert(bit == S_IRUSR || bit == S_IWUSR || bit == S_IXUSR ||
6297c478bd9Sstevel@tonic-gate 	    bit == S_IRGRP || bit == S_IWGRP || bit == S_IXGRP ||
6307c478bd9Sstevel@tonic-gate 	    bit == S_IROTH || bit == S_IWOTH || bit == S_IXOTH);
6317c478bd9Sstevel@tonic-gate 	/*
6327c478bd9Sstevel@tonic-gate 	 * TRANSLATION_NOTE
6337c478bd9Sstevel@tonic-gate 	 * The strings below will be used as part of a larger message,
6347c478bd9Sstevel@tonic-gate 	 * either:
6357c478bd9Sstevel@tonic-gate 	 * (file name) must be (owner|group|world) (read|writ|execut)able
6367c478bd9Sstevel@tonic-gate 	 * or
6377c478bd9Sstevel@tonic-gate 	 * (file name) must not be (owner|group|world) (read|writ|execut)able
6387c478bd9Sstevel@tonic-gate 	 */
6397c478bd9Sstevel@tonic-gate 	switch (bit) {
6407c478bd9Sstevel@tonic-gate 	case S_IRUSR:
6417c478bd9Sstevel@tonic-gate 		str = gettext("owner readable");
6427c478bd9Sstevel@tonic-gate 		break;
6437c478bd9Sstevel@tonic-gate 	case S_IWUSR:
6447c478bd9Sstevel@tonic-gate 		str = gettext("owner writable");
6457c478bd9Sstevel@tonic-gate 		break;
6467c478bd9Sstevel@tonic-gate 	case S_IXUSR:
6477c478bd9Sstevel@tonic-gate 		str = gettext("owner executable");
6487c478bd9Sstevel@tonic-gate 		break;
6497c478bd9Sstevel@tonic-gate 	case S_IRGRP:
6507c478bd9Sstevel@tonic-gate 		str = gettext("group readable");
6517c478bd9Sstevel@tonic-gate 		break;
6527c478bd9Sstevel@tonic-gate 	case S_IWGRP:
6537c478bd9Sstevel@tonic-gate 		str = gettext("group writable");
6547c478bd9Sstevel@tonic-gate 		break;
6557c478bd9Sstevel@tonic-gate 	case S_IXGRP:
6567c478bd9Sstevel@tonic-gate 		str = gettext("group executable");
6577c478bd9Sstevel@tonic-gate 		break;
6587c478bd9Sstevel@tonic-gate 	case S_IROTH:
6597c478bd9Sstevel@tonic-gate 		str = gettext("world readable");
6607c478bd9Sstevel@tonic-gate 		break;
6617c478bd9Sstevel@tonic-gate 	case S_IWOTH:
6627c478bd9Sstevel@tonic-gate 		str = gettext("world writable");
6637c478bd9Sstevel@tonic-gate 		break;
6647c478bd9Sstevel@tonic-gate 	case S_IXOTH:
6657c478bd9Sstevel@tonic-gate 		str = gettext("world executable");
6667c478bd9Sstevel@tonic-gate 		break;
6677c478bd9Sstevel@tonic-gate 	}
6687c478bd9Sstevel@tonic-gate 	if ((mode & bit) == (on ? 0 : bit)) {
6697c478bd9Sstevel@tonic-gate 		/*
6707c478bd9Sstevel@tonic-gate 		 * TRANSLATION_NOTE
6717c478bd9Sstevel@tonic-gate 		 * The first parameter below is a file name; the second
6727c478bd9Sstevel@tonic-gate 		 * is one of the "(owner|group|world) (read|writ|execut)able"
6737c478bd9Sstevel@tonic-gate 		 * strings from above.
6747c478bd9Sstevel@tonic-gate 		 */
6757c478bd9Sstevel@tonic-gate 		/*
6767c478bd9Sstevel@tonic-gate 		 * The code below could be simplified but not in a way
6777c478bd9Sstevel@tonic-gate 		 * that would easily translate to non-English locales.
6787c478bd9Sstevel@tonic-gate 		 */
6797c478bd9Sstevel@tonic-gate 		if (on) {
6807c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("%s must be %s.\n"),
6817c478bd9Sstevel@tonic-gate 			    file, str);
6827c478bd9Sstevel@tonic-gate 		} else {
6837c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("%s must not be %s.\n"),
6847c478bd9Sstevel@tonic-gate 			    file, str);
6857c478bd9Sstevel@tonic-gate 		}
6867c478bd9Sstevel@tonic-gate 		return (B_TRUE);
6877c478bd9Sstevel@tonic-gate 	}
6887c478bd9Sstevel@tonic-gate 	return (B_FALSE);
6897c478bd9Sstevel@tonic-gate }
6907c478bd9Sstevel@tonic-gate 
6917c478bd9Sstevel@tonic-gate /*
6927c478bd9Sstevel@tonic-gate  * We want to make sure that no zone has its zone path as a child node
6937c478bd9Sstevel@tonic-gate  * (in the directory sense) of any other.  We do that by comparing this
6947c478bd9Sstevel@tonic-gate  * zone's path to the path of all other (non-global) zones.  The comparison
6957c478bd9Sstevel@tonic-gate  * in each case is simple: add '/' to the end of the path, then do a
6967c478bd9Sstevel@tonic-gate  * strncmp() of the two paths, using the length of the shorter one.
6977c478bd9Sstevel@tonic-gate  */
6987c478bd9Sstevel@tonic-gate 
6997c478bd9Sstevel@tonic-gate static int
7007c478bd9Sstevel@tonic-gate crosscheck_zonepaths(char *path)
7017c478bd9Sstevel@tonic-gate {
7027c478bd9Sstevel@tonic-gate 	char rpath[MAXPATHLEN];		/* resolved path */
7037c478bd9Sstevel@tonic-gate 	char path_copy[MAXPATHLEN];	/* copy of original path */
7047c478bd9Sstevel@tonic-gate 	char rpath_copy[MAXPATHLEN];	/* copy of original rpath */
7057c478bd9Sstevel@tonic-gate 	struct zoneent *ze;
7067c478bd9Sstevel@tonic-gate 	int res, err;
7077c478bd9Sstevel@tonic-gate 	FILE *cookie;
7087c478bd9Sstevel@tonic-gate 
7097c478bd9Sstevel@tonic-gate 	cookie = setzoneent();
7107c478bd9Sstevel@tonic-gate 	while ((ze = getzoneent_private(cookie)) != NULL) {
7117c478bd9Sstevel@tonic-gate 		/* Skip zones which are not installed. */
7127c478bd9Sstevel@tonic-gate 		if (ze->zone_state < ZONE_STATE_INSTALLED) {
7137c478bd9Sstevel@tonic-gate 			free(ze);
7147c478bd9Sstevel@tonic-gate 			continue;
7157c478bd9Sstevel@tonic-gate 		}
7167c478bd9Sstevel@tonic-gate 		/* Skip the global zone and the current target zone. */
7177c478bd9Sstevel@tonic-gate 		if (strcmp(ze->zone_name, GLOBAL_ZONENAME) == 0 ||
7187c478bd9Sstevel@tonic-gate 		    strcmp(ze->zone_name, target_zone) == 0) {
7197c478bd9Sstevel@tonic-gate 			free(ze);
7207c478bd9Sstevel@tonic-gate 			continue;
7217c478bd9Sstevel@tonic-gate 		}
7227c478bd9Sstevel@tonic-gate 		if (strlen(ze->zone_path) == 0) {
7237c478bd9Sstevel@tonic-gate 			/* old index file without path, fall back */
7247c478bd9Sstevel@tonic-gate 			if ((err = zone_get_zonepath(ze->zone_name,
7257c478bd9Sstevel@tonic-gate 			    ze->zone_path, sizeof (ze->zone_path))) != Z_OK) {
7267c478bd9Sstevel@tonic-gate 				errno = err;
7277c478bd9Sstevel@tonic-gate 				zperror2(ze->zone_name,
7287c478bd9Sstevel@tonic-gate 				    gettext("could not get zone path"));
7297c478bd9Sstevel@tonic-gate 				free(ze);
7307c478bd9Sstevel@tonic-gate 				continue;
7317c478bd9Sstevel@tonic-gate 			}
7327c478bd9Sstevel@tonic-gate 		}
733108322fbScarlsonj 		(void) snprintf(path_copy, sizeof (path_copy), "%s%s",
734108322fbScarlsonj 		    zonecfg_get_root(), ze->zone_path);
735108322fbScarlsonj 		res = resolvepath(path_copy, rpath, sizeof (rpath));
7367c478bd9Sstevel@tonic-gate 		if (res == -1) {
7377c478bd9Sstevel@tonic-gate 			if (errno != ENOENT) {
738108322fbScarlsonj 				zperror(path_copy, B_FALSE);
7397c478bd9Sstevel@tonic-gate 				free(ze);
7407c478bd9Sstevel@tonic-gate 				return (Z_ERR);
7417c478bd9Sstevel@tonic-gate 			}
7427c478bd9Sstevel@tonic-gate 			(void) printf(gettext("WARNING: zone %s is installed, "
7437c478bd9Sstevel@tonic-gate 			    "but its %s %s does not exist.\n"), ze->zone_name,
744108322fbScarlsonj 			    "zonepath", path_copy);
7457c478bd9Sstevel@tonic-gate 			free(ze);
7467c478bd9Sstevel@tonic-gate 			continue;
7477c478bd9Sstevel@tonic-gate 		}
7487c478bd9Sstevel@tonic-gate 		rpath[res] = '\0';
7497c478bd9Sstevel@tonic-gate 		(void) snprintf(path_copy, sizeof (path_copy), "%s/", path);
7507c478bd9Sstevel@tonic-gate 		(void) snprintf(rpath_copy, sizeof (rpath_copy), "%s/", rpath);
7517c478bd9Sstevel@tonic-gate 		if (strncmp(path_copy, rpath_copy,
7527c478bd9Sstevel@tonic-gate 		    min(strlen(path_copy), strlen(rpath_copy))) == 0) {
7535ee84fbdSgjelinek 			/*
7545ee84fbdSgjelinek 			 * TRANSLATION_NOTE
7555ee84fbdSgjelinek 			 * zonepath is a literal that should not be translated.
7565ee84fbdSgjelinek 			 */
7577c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("%s zonepath (%s) and "
7587c478bd9Sstevel@tonic-gate 			    "%s zonepath (%s) overlap.\n"),
7597c478bd9Sstevel@tonic-gate 			    target_zone, path, ze->zone_name, rpath);
7607c478bd9Sstevel@tonic-gate 			free(ze);
7617c478bd9Sstevel@tonic-gate 			return (Z_ERR);
7627c478bd9Sstevel@tonic-gate 		}
7637c478bd9Sstevel@tonic-gate 		free(ze);
7647c478bd9Sstevel@tonic-gate 	}
7657c478bd9Sstevel@tonic-gate 	endzoneent(cookie);
7667c478bd9Sstevel@tonic-gate 	return (Z_OK);
7677c478bd9Sstevel@tonic-gate }
7687c478bd9Sstevel@tonic-gate 
7697c478bd9Sstevel@tonic-gate static int
7707c478bd9Sstevel@tonic-gate validate_zonepath(char *path, int cmd_num)
7717c478bd9Sstevel@tonic-gate {
7727c478bd9Sstevel@tonic-gate 	int res;			/* result of last library/system call */
7737c478bd9Sstevel@tonic-gate 	boolean_t err = B_FALSE;	/* have we run into an error? */
7747c478bd9Sstevel@tonic-gate 	struct stat stbuf;
7757c478bd9Sstevel@tonic-gate 	struct statvfs vfsbuf;
7767c478bd9Sstevel@tonic-gate 	char rpath[MAXPATHLEN];		/* resolved path */
7777c478bd9Sstevel@tonic-gate 	char ppath[MAXPATHLEN];		/* parent path */
7787c478bd9Sstevel@tonic-gate 	char rppath[MAXPATHLEN];	/* resolved parent path */
7797c478bd9Sstevel@tonic-gate 	char rootpath[MAXPATHLEN];	/* root path */
7807c478bd9Sstevel@tonic-gate 	zone_state_t state;
7817c478bd9Sstevel@tonic-gate 
7827c478bd9Sstevel@tonic-gate 	if (path[0] != '/') {
7837c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
7847c478bd9Sstevel@tonic-gate 		    gettext("%s is not an absolute path.\n"), path);
7857c478bd9Sstevel@tonic-gate 		return (Z_ERR);
7867c478bd9Sstevel@tonic-gate 	}
7877c478bd9Sstevel@tonic-gate 	if ((res = resolvepath(path, rpath, sizeof (rpath))) == -1) {
7887c478bd9Sstevel@tonic-gate 		if ((errno != ENOENT) ||
789865e09a4Sgjelinek 		    (cmd_num != CMD_VERIFY && cmd_num != CMD_INSTALL &&
790865e09a4Sgjelinek 		    cmd_num != CMD_CLONE && cmd_num != CMD_MOVE)) {
7917c478bd9Sstevel@tonic-gate 			zperror(path, B_FALSE);
7927c478bd9Sstevel@tonic-gate 			return (Z_ERR);
7937c478bd9Sstevel@tonic-gate 		}
7947c478bd9Sstevel@tonic-gate 		if (cmd_num == CMD_VERIFY) {
7955ee84fbdSgjelinek 			/*
7965ee84fbdSgjelinek 			 * TRANSLATION_NOTE
7975ee84fbdSgjelinek 			 * zoneadm is a literal that should not be translated.
7985ee84fbdSgjelinek 			 */
7997c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("WARNING: %s does not "
8005ee84fbdSgjelinek 			    "exist, so it could not be verified.\nWhen "
8015ee84fbdSgjelinek 			    "'zoneadm %s' is run, '%s' will try to create\n%s, "
8025ee84fbdSgjelinek 			    "and '%s' will be tried again,\nbut the '%s' may "
8035ee84fbdSgjelinek 			    "fail if:\nthe parent directory of %s is group- or "
8045ee84fbdSgjelinek 			    "other-writable\nor\n%s overlaps with any other "
8057c478bd9Sstevel@tonic-gate 			    "installed zones.\n"), path,
8067c478bd9Sstevel@tonic-gate 			    cmd_to_str(CMD_INSTALL), cmd_to_str(CMD_INSTALL),
8077c478bd9Sstevel@tonic-gate 			    path, cmd_to_str(CMD_VERIFY),
8087c478bd9Sstevel@tonic-gate 			    cmd_to_str(CMD_VERIFY), path, path);
8097c478bd9Sstevel@tonic-gate 			return (Z_OK);
8107c478bd9Sstevel@tonic-gate 		}
8117c478bd9Sstevel@tonic-gate 		/*
8127c478bd9Sstevel@tonic-gate 		 * The zonepath is supposed to be mode 700 but its
8137c478bd9Sstevel@tonic-gate 		 * parent(s) 755.  So use 755 on the mkdirp() then
8147c478bd9Sstevel@tonic-gate 		 * chmod() the zonepath itself to 700.
8157c478bd9Sstevel@tonic-gate 		 */
8167c478bd9Sstevel@tonic-gate 		if (mkdirp(path, DEFAULT_DIR_MODE) < 0) {
8177c478bd9Sstevel@tonic-gate 			zperror(path, B_FALSE);
8187c478bd9Sstevel@tonic-gate 			return (Z_ERR);
8197c478bd9Sstevel@tonic-gate 		}
8207c478bd9Sstevel@tonic-gate 		/*
8217c478bd9Sstevel@tonic-gate 		 * If the chmod() fails, report the error, but might
8227c478bd9Sstevel@tonic-gate 		 * as well continue the verify procedure.
8237c478bd9Sstevel@tonic-gate 		 */
8247c478bd9Sstevel@tonic-gate 		if (chmod(path, S_IRWXU) != 0)
8257c478bd9Sstevel@tonic-gate 			zperror(path, B_FALSE);
8267c478bd9Sstevel@tonic-gate 		/*
8277c478bd9Sstevel@tonic-gate 		 * Since the mkdir() succeeded, we should not have to
8287c478bd9Sstevel@tonic-gate 		 * worry about a subsequent ENOENT, thus this should
8297c478bd9Sstevel@tonic-gate 		 * only recurse once.
8307c478bd9Sstevel@tonic-gate 		 */
831865e09a4Sgjelinek 		return (validate_zonepath(path, cmd_num));
8327c478bd9Sstevel@tonic-gate 	}
8337c478bd9Sstevel@tonic-gate 	rpath[res] = '\0';
8347c478bd9Sstevel@tonic-gate 	if (strcmp(path, rpath) != 0) {
8357c478bd9Sstevel@tonic-gate 		errno = Z_RESOLVED_PATH;
8367c478bd9Sstevel@tonic-gate 		zperror(path, B_TRUE);
8377c478bd9Sstevel@tonic-gate 		return (Z_ERR);
8387c478bd9Sstevel@tonic-gate 	}
8397c478bd9Sstevel@tonic-gate 	if ((res = stat(rpath, &stbuf)) != 0) {
8407c478bd9Sstevel@tonic-gate 		zperror(rpath, B_FALSE);
8417c478bd9Sstevel@tonic-gate 		return (Z_ERR);
8427c478bd9Sstevel@tonic-gate 	}
8437c478bd9Sstevel@tonic-gate 	if (!S_ISDIR(stbuf.st_mode)) {
8447c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("%s is not a directory.\n"),
8457c478bd9Sstevel@tonic-gate 		    rpath);
8467c478bd9Sstevel@tonic-gate 		return (Z_ERR);
8477c478bd9Sstevel@tonic-gate 	}
8487c478bd9Sstevel@tonic-gate 	if ((strcmp(stbuf.st_fstype, MNTTYPE_TMPFS) == 0) ||
8497c478bd9Sstevel@tonic-gate 	    (strcmp(stbuf.st_fstype, MNTTYPE_XMEMFS) == 0)) {
8507c478bd9Sstevel@tonic-gate 		(void) printf(gettext("WARNING: %s is on a temporary "
8510b5de56dSgjelinek 		    "file system.\n"), rpath);
8527c478bd9Sstevel@tonic-gate 	}
8537c478bd9Sstevel@tonic-gate 	if (crosscheck_zonepaths(rpath) != Z_OK)
8547c478bd9Sstevel@tonic-gate 		return (Z_ERR);
8557c478bd9Sstevel@tonic-gate 	/*
8567c478bd9Sstevel@tonic-gate 	 * Try to collect and report as many minor errors as possible
8577c478bd9Sstevel@tonic-gate 	 * before returning, so the user can learn everything that needs
8587c478bd9Sstevel@tonic-gate 	 * to be fixed up front.
8597c478bd9Sstevel@tonic-gate 	 */
8607c478bd9Sstevel@tonic-gate 	if (stbuf.st_uid != 0) {
8617c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("%s is not owned by root.\n"),
8627c478bd9Sstevel@tonic-gate 		    rpath);
8637c478bd9Sstevel@tonic-gate 		err = B_TRUE;
8647c478bd9Sstevel@tonic-gate 	}
8657c478bd9Sstevel@tonic-gate 	err |= bad_mode_bit(stbuf.st_mode, S_IRUSR, B_TRUE, rpath);
8667c478bd9Sstevel@tonic-gate 	err |= bad_mode_bit(stbuf.st_mode, S_IWUSR, B_TRUE, rpath);
8677c478bd9Sstevel@tonic-gate 	err |= bad_mode_bit(stbuf.st_mode, S_IXUSR, B_TRUE, rpath);
8687c478bd9Sstevel@tonic-gate 	err |= bad_mode_bit(stbuf.st_mode, S_IRGRP, B_FALSE, rpath);
8697c478bd9Sstevel@tonic-gate 	err |= bad_mode_bit(stbuf.st_mode, S_IWGRP, B_FALSE, rpath);
8707c478bd9Sstevel@tonic-gate 	err |= bad_mode_bit(stbuf.st_mode, S_IXGRP, B_FALSE, rpath);
8717c478bd9Sstevel@tonic-gate 	err |= bad_mode_bit(stbuf.st_mode, S_IROTH, B_FALSE, rpath);
8727c478bd9Sstevel@tonic-gate 	err |= bad_mode_bit(stbuf.st_mode, S_IWOTH, B_FALSE, rpath);
8737c478bd9Sstevel@tonic-gate 	err |= bad_mode_bit(stbuf.st_mode, S_IXOTH, B_FALSE, rpath);
8747c478bd9Sstevel@tonic-gate 
8757c478bd9Sstevel@tonic-gate 	(void) snprintf(ppath, sizeof (ppath), "%s/..", path);
8767c478bd9Sstevel@tonic-gate 	if ((res = resolvepath(ppath, rppath, sizeof (rppath))) == -1) {
8777c478bd9Sstevel@tonic-gate 		zperror(ppath, B_FALSE);
8787c478bd9Sstevel@tonic-gate 		return (Z_ERR);
8797c478bd9Sstevel@tonic-gate 	}
8807c478bd9Sstevel@tonic-gate 	rppath[res] = '\0';
8817c478bd9Sstevel@tonic-gate 	if ((res = stat(rppath, &stbuf)) != 0) {
8827c478bd9Sstevel@tonic-gate 		zperror(rppath, B_FALSE);
8837c478bd9Sstevel@tonic-gate 		return (Z_ERR);
8847c478bd9Sstevel@tonic-gate 	}
8857c478bd9Sstevel@tonic-gate 	/* theoretically impossible */
8867c478bd9Sstevel@tonic-gate 	if (!S_ISDIR(stbuf.st_mode)) {
8877c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("%s is not a directory.\n"),
8887c478bd9Sstevel@tonic-gate 		    rppath);
8897c478bd9Sstevel@tonic-gate 		return (Z_ERR);
8907c478bd9Sstevel@tonic-gate 	}
8917c478bd9Sstevel@tonic-gate 	if (stbuf.st_uid != 0) {
8927c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("%s is not owned by root.\n"),
8937c478bd9Sstevel@tonic-gate 		    rppath);
8947c478bd9Sstevel@tonic-gate 		err = B_TRUE;
8957c478bd9Sstevel@tonic-gate 	}
8967c478bd9Sstevel@tonic-gate 	err |= bad_mode_bit(stbuf.st_mode, S_IRUSR, B_TRUE, rppath);
8977c478bd9Sstevel@tonic-gate 	err |= bad_mode_bit(stbuf.st_mode, S_IWUSR, B_TRUE, rppath);
8987c478bd9Sstevel@tonic-gate 	err |= bad_mode_bit(stbuf.st_mode, S_IXUSR, B_TRUE, rppath);
8997c478bd9Sstevel@tonic-gate 	err |= bad_mode_bit(stbuf.st_mode, S_IWGRP, B_FALSE, rppath);
9007c478bd9Sstevel@tonic-gate 	err |= bad_mode_bit(stbuf.st_mode, S_IWOTH, B_FALSE, rppath);
9017c478bd9Sstevel@tonic-gate 	if (strcmp(rpath, rppath) == 0) {
9027c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("%s is its own parent.\n"),
9037c478bd9Sstevel@tonic-gate 		    rppath);
9047c478bd9Sstevel@tonic-gate 		err = B_TRUE;
9057c478bd9Sstevel@tonic-gate 	}
9067c478bd9Sstevel@tonic-gate 
9077c478bd9Sstevel@tonic-gate 	if (statvfs(rpath, &vfsbuf) != 0) {
9087c478bd9Sstevel@tonic-gate 		zperror(rpath, B_FALSE);
9097c478bd9Sstevel@tonic-gate 		return (Z_ERR);
9107c478bd9Sstevel@tonic-gate 	}
911b5abaf04Sgjelinek 	if (strcmp(vfsbuf.f_basetype, MNTTYPE_NFS) == 0) {
9125ee84fbdSgjelinek 		/*
9135ee84fbdSgjelinek 		 * TRANSLATION_NOTE
9145ee84fbdSgjelinek 		 * Zonepath and NFS are literals that should not be translated.
9155ee84fbdSgjelinek 		 */
9165ee84fbdSgjelinek 		(void) fprintf(stderr, gettext("Zonepath %s is on an NFS "
9170b5de56dSgjelinek 		    "mounted file system.\n"
9180b5de56dSgjelinek 		    "\tA local file system must be used.\n"), rpath);
919b5abaf04Sgjelinek 		return (Z_ERR);
920b5abaf04Sgjelinek 	}
921b5abaf04Sgjelinek 	if (vfsbuf.f_flag & ST_NOSUID) {
9225ee84fbdSgjelinek 		/*
9235ee84fbdSgjelinek 		 * TRANSLATION_NOTE
9245ee84fbdSgjelinek 		 * Zonepath and nosuid are literals that should not be
9255ee84fbdSgjelinek 		 * translated.
9265ee84fbdSgjelinek 		 */
927b5abaf04Sgjelinek 		(void) fprintf(stderr, gettext("Zonepath %s is on a nosuid "
9280b5de56dSgjelinek 		    "file system.\n"), rpath);
9297c478bd9Sstevel@tonic-gate 		return (Z_ERR);
9307c478bd9Sstevel@tonic-gate 	}
9317c478bd9Sstevel@tonic-gate 
9327c478bd9Sstevel@tonic-gate 	if ((res = zone_get_state(target_zone, &state)) != Z_OK) {
9337c478bd9Sstevel@tonic-gate 		errno = res;
9347c478bd9Sstevel@tonic-gate 		zperror2(target_zone, gettext("could not get state"));
9357c478bd9Sstevel@tonic-gate 		return (Z_ERR);
9367c478bd9Sstevel@tonic-gate 	}
9377c478bd9Sstevel@tonic-gate 	/*
9387c478bd9Sstevel@tonic-gate 	 * The existence of the root path is only bad in the configured state,
9397c478bd9Sstevel@tonic-gate 	 * as it is *supposed* to be there at the installed and later states.
940ee519a1fSgjelinek 	 * However, the root path is expected to be there if the zone is
941ee519a1fSgjelinek 	 * detached.
9427c478bd9Sstevel@tonic-gate 	 * State/command mismatches are caught earlier in verify_details().
9437c478bd9Sstevel@tonic-gate 	 */
944ee519a1fSgjelinek 	if (state == ZONE_STATE_CONFIGURED && cmd_num != CMD_ATTACH) {
9457c478bd9Sstevel@tonic-gate 		if (snprintf(rootpath, sizeof (rootpath), "%s/root", rpath) >=
9467c478bd9Sstevel@tonic-gate 		    sizeof (rootpath)) {
9475ee84fbdSgjelinek 			/*
9485ee84fbdSgjelinek 			 * TRANSLATION_NOTE
9495ee84fbdSgjelinek 			 * Zonepath is a literal that should not be translated.
9505ee84fbdSgjelinek 			 */
9517c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
9527c478bd9Sstevel@tonic-gate 			    gettext("Zonepath %s is too long.\n"), rpath);
9537c478bd9Sstevel@tonic-gate 			return (Z_ERR);
9547c478bd9Sstevel@tonic-gate 		}
9557c478bd9Sstevel@tonic-gate 		if ((res = stat(rootpath, &stbuf)) == 0) {
956ee519a1fSgjelinek 			if (zonecfg_detached(rpath))
957ee519a1fSgjelinek 				(void) fprintf(stderr,
958ee519a1fSgjelinek 				    gettext("Cannot %s detached "
959ee519a1fSgjelinek 				    "zone.\nUse attach or remove %s "
960ee519a1fSgjelinek 				    "directory.\n"), cmd_to_str(cmd_num),
961ee519a1fSgjelinek 				    rpath);
962ee519a1fSgjelinek 			else
963ee519a1fSgjelinek 				(void) fprintf(stderr,
964ee519a1fSgjelinek 				    gettext("Rootpath %s exists; "
965ee519a1fSgjelinek 				    "remove or move aside prior to %s.\n"),
966ee519a1fSgjelinek 				    rootpath, cmd_to_str(cmd_num));
9677c478bd9Sstevel@tonic-gate 			return (Z_ERR);
9687c478bd9Sstevel@tonic-gate 		}
9697c478bd9Sstevel@tonic-gate 	}
9707c478bd9Sstevel@tonic-gate 
9717c478bd9Sstevel@tonic-gate 	return (err ? Z_ERR : Z_OK);
9727c478bd9Sstevel@tonic-gate }
9737c478bd9Sstevel@tonic-gate 
9747c478bd9Sstevel@tonic-gate static void
9757c478bd9Sstevel@tonic-gate release_lock_file(int lockfd)
9767c478bd9Sstevel@tonic-gate {
9777c478bd9Sstevel@tonic-gate 	(void) close(lockfd);
9787c478bd9Sstevel@tonic-gate }
9797c478bd9Sstevel@tonic-gate 
9807c478bd9Sstevel@tonic-gate static int
9817c478bd9Sstevel@tonic-gate grab_lock_file(const char *zone_name, int *lockfd)
9827c478bd9Sstevel@tonic-gate {
9837c478bd9Sstevel@tonic-gate 	char pathbuf[PATH_MAX];
9847c478bd9Sstevel@tonic-gate 	struct flock flock;
9857c478bd9Sstevel@tonic-gate 
986108322fbScarlsonj 	if (snprintf(pathbuf, sizeof (pathbuf), "%s%s", zonecfg_get_root(),
987108322fbScarlsonj 	    ZONES_TMPDIR) >= sizeof (pathbuf)) {
988108322fbScarlsonj 		zerror(gettext("alternate root path is too long"));
989108322fbScarlsonj 		return (Z_ERR);
990108322fbScarlsonj 	}
991108322fbScarlsonj 	if (mkdir(pathbuf, S_IRWXU) < 0 && errno != EEXIST) {
992108322fbScarlsonj 		zerror(gettext("could not mkdir %s: %s"), pathbuf,
9937c478bd9Sstevel@tonic-gate 		    strerror(errno));
9947c478bd9Sstevel@tonic-gate 		return (Z_ERR);
9957c478bd9Sstevel@tonic-gate 	}
996108322fbScarlsonj 	(void) chmod(pathbuf, S_IRWXU);
9977c478bd9Sstevel@tonic-gate 
9987c478bd9Sstevel@tonic-gate 	/*
9997c478bd9Sstevel@tonic-gate 	 * One of these lock files is created for each zone (when needed).
10007c478bd9Sstevel@tonic-gate 	 * The lock files are not cleaned up (except on system reboot),
10017c478bd9Sstevel@tonic-gate 	 * but since there is only one per zone, there is no resource
10027c478bd9Sstevel@tonic-gate 	 * starvation issue.
10037c478bd9Sstevel@tonic-gate 	 */
1004108322fbScarlsonj 	if (snprintf(pathbuf, sizeof (pathbuf), "%s%s/%s.zoneadm.lock",
1005108322fbScarlsonj 	    zonecfg_get_root(), ZONES_TMPDIR, zone_name) >= sizeof (pathbuf)) {
1006108322fbScarlsonj 		zerror(gettext("alternate root path is too long"));
1007108322fbScarlsonj 		return (Z_ERR);
1008108322fbScarlsonj 	}
10097c478bd9Sstevel@tonic-gate 	if ((*lockfd = open(pathbuf, O_RDWR|O_CREAT, S_IRUSR|S_IWUSR)) < 0) {
10107c478bd9Sstevel@tonic-gate 		zerror(gettext("could not open %s: %s"), pathbuf,
10117c478bd9Sstevel@tonic-gate 		    strerror(errno));
10127c478bd9Sstevel@tonic-gate 		return (Z_ERR);
10137c478bd9Sstevel@tonic-gate 	}
10147c478bd9Sstevel@tonic-gate 	/*
10157c478bd9Sstevel@tonic-gate 	 * Lock the file to synchronize with other zoneadmds
10167c478bd9Sstevel@tonic-gate 	 */
10177c478bd9Sstevel@tonic-gate 	flock.l_type = F_WRLCK;
10187c478bd9Sstevel@tonic-gate 	flock.l_whence = SEEK_SET;
10197c478bd9Sstevel@tonic-gate 	flock.l_start = (off_t)0;
10207c478bd9Sstevel@tonic-gate 	flock.l_len = (off_t)0;
10217c478bd9Sstevel@tonic-gate 	if (fcntl(*lockfd, F_SETLKW, &flock) < 0) {
10227c478bd9Sstevel@tonic-gate 		zerror(gettext("unable to lock %s: %s"), pathbuf,
10237c478bd9Sstevel@tonic-gate 		    strerror(errno));
10247c478bd9Sstevel@tonic-gate 		release_lock_file(*lockfd);
10257c478bd9Sstevel@tonic-gate 		return (Z_ERR);
10267c478bd9Sstevel@tonic-gate 	}
10277c478bd9Sstevel@tonic-gate 	return (Z_OK);
10287c478bd9Sstevel@tonic-gate }
10297c478bd9Sstevel@tonic-gate 
1030108322fbScarlsonj static boolean_t
10317c478bd9Sstevel@tonic-gate get_doorname(const char *zone_name, char *buffer)
10327c478bd9Sstevel@tonic-gate {
1033108322fbScarlsonj 	return (snprintf(buffer, PATH_MAX, "%s" ZONE_DOOR_PATH,
1034108322fbScarlsonj 	    zonecfg_get_root(), zone_name) < PATH_MAX);
10357c478bd9Sstevel@tonic-gate }
10367c478bd9Sstevel@tonic-gate 
10377c478bd9Sstevel@tonic-gate /*
10387c478bd9Sstevel@tonic-gate  * system daemons are not audited.  For the global zone, this occurs
10397c478bd9Sstevel@tonic-gate  * "naturally" since init is started with the default audit
10407c478bd9Sstevel@tonic-gate  * characteristics.  Since zoneadmd is a system daemon and it starts
10417c478bd9Sstevel@tonic-gate  * init for a zone, it is necessary to clear out the audit
10427c478bd9Sstevel@tonic-gate  * characteristics inherited from whomever started zoneadmd.  This is
10437c478bd9Sstevel@tonic-gate  * indicated by the audit id, which is set from the ruid parameter of
10447c478bd9Sstevel@tonic-gate  * adt_set_user(), below.
10457c478bd9Sstevel@tonic-gate  */
10467c478bd9Sstevel@tonic-gate 
10477c478bd9Sstevel@tonic-gate static void
10487c478bd9Sstevel@tonic-gate prepare_audit_context()
10497c478bd9Sstevel@tonic-gate {
10507c478bd9Sstevel@tonic-gate 	adt_session_data_t	*ah;
10517c478bd9Sstevel@tonic-gate 	char			*failure = gettext("audit failure: %s");
10527c478bd9Sstevel@tonic-gate 
10537c478bd9Sstevel@tonic-gate 	if (adt_start_session(&ah, NULL, 0)) {
10547c478bd9Sstevel@tonic-gate 		zerror(failure, strerror(errno));
10557c478bd9Sstevel@tonic-gate 		return;
10567c478bd9Sstevel@tonic-gate 	}
10577c478bd9Sstevel@tonic-gate 	if (adt_set_user(ah, ADT_NO_AUDIT, ADT_NO_AUDIT,
10587c478bd9Sstevel@tonic-gate 	    ADT_NO_AUDIT, ADT_NO_AUDIT, NULL, ADT_NEW)) {
10597c478bd9Sstevel@tonic-gate 		zerror(failure, strerror(errno));
10607c478bd9Sstevel@tonic-gate 		(void) adt_end_session(ah);
10617c478bd9Sstevel@tonic-gate 		return;
10627c478bd9Sstevel@tonic-gate 	}
10637c478bd9Sstevel@tonic-gate 	if (adt_set_proc(ah))
10647c478bd9Sstevel@tonic-gate 		zerror(failure, strerror(errno));
10657c478bd9Sstevel@tonic-gate 
10667c478bd9Sstevel@tonic-gate 	(void) adt_end_session(ah);
10677c478bd9Sstevel@tonic-gate }
10687c478bd9Sstevel@tonic-gate 
10697c478bd9Sstevel@tonic-gate static int
10707c478bd9Sstevel@tonic-gate start_zoneadmd(const char *zone_name)
10717c478bd9Sstevel@tonic-gate {
10727c478bd9Sstevel@tonic-gate 	char doorpath[PATH_MAX];
10737c478bd9Sstevel@tonic-gate 	pid_t child_pid;
10747c478bd9Sstevel@tonic-gate 	int error = Z_ERR;
10757c478bd9Sstevel@tonic-gate 	int doorfd, lockfd;
10767c478bd9Sstevel@tonic-gate 	struct door_info info;
10777c478bd9Sstevel@tonic-gate 
1078108322fbScarlsonj 	if (!get_doorname(zone_name, doorpath))
1079108322fbScarlsonj 		return (Z_ERR);
10807c478bd9Sstevel@tonic-gate 
10817c478bd9Sstevel@tonic-gate 	if (grab_lock_file(zone_name, &lockfd) != Z_OK)
10827c478bd9Sstevel@tonic-gate 		return (Z_ERR);
10837c478bd9Sstevel@tonic-gate 
10847c478bd9Sstevel@tonic-gate 	/*
10857c478bd9Sstevel@tonic-gate 	 * Now that we have the lock, re-confirm that the daemon is
10867c478bd9Sstevel@tonic-gate 	 * *not* up and working fine.  If it is still down, we have a green
10877c478bd9Sstevel@tonic-gate 	 * light to start it.
10887c478bd9Sstevel@tonic-gate 	 */
10897c478bd9Sstevel@tonic-gate 	if ((doorfd = open(doorpath, O_RDONLY)) < 0) {
10907c478bd9Sstevel@tonic-gate 		if (errno != ENOENT) {
10917c478bd9Sstevel@tonic-gate 			zperror(doorpath, B_FALSE);
10927c478bd9Sstevel@tonic-gate 			goto out;
10937c478bd9Sstevel@tonic-gate 		}
10947c478bd9Sstevel@tonic-gate 	} else {
10957c478bd9Sstevel@tonic-gate 		if (door_info(doorfd, &info) == 0 &&
10967c478bd9Sstevel@tonic-gate 		    ((info.di_attributes & DOOR_REVOKED) == 0)) {
10977c478bd9Sstevel@tonic-gate 			error = Z_OK;
10987c478bd9Sstevel@tonic-gate 			(void) close(doorfd);
10997c478bd9Sstevel@tonic-gate 			goto out;
11007c478bd9Sstevel@tonic-gate 		}
11017c478bd9Sstevel@tonic-gate 		(void) close(doorfd);
11027c478bd9Sstevel@tonic-gate 	}
11037c478bd9Sstevel@tonic-gate 
11047c478bd9Sstevel@tonic-gate 	if ((child_pid = fork()) == -1) {
11057c478bd9Sstevel@tonic-gate 		zperror(gettext("could not fork"), B_FALSE);
11067c478bd9Sstevel@tonic-gate 		goto out;
11077c478bd9Sstevel@tonic-gate 	} else if (child_pid == 0) {
1108108322fbScarlsonj 		const char *argv[6], **ap;
11097c478bd9Sstevel@tonic-gate 
1110108322fbScarlsonj 		/* child process */
11117c478bd9Sstevel@tonic-gate 		prepare_audit_context();
11127c478bd9Sstevel@tonic-gate 
1113108322fbScarlsonj 		ap = argv;
1114108322fbScarlsonj 		*ap++ = "zoneadmd";
1115108322fbScarlsonj 		*ap++ = "-z";
1116108322fbScarlsonj 		*ap++ = zone_name;
1117108322fbScarlsonj 		if (zonecfg_in_alt_root()) {
1118108322fbScarlsonj 			*ap++ = "-R";
1119108322fbScarlsonj 			*ap++ = zonecfg_get_root();
1120108322fbScarlsonj 		}
1121108322fbScarlsonj 		*ap = NULL;
1122108322fbScarlsonj 
1123108322fbScarlsonj 		(void) execv("/usr/lib/zones/zoneadmd", (char * const *)argv);
11245ee84fbdSgjelinek 		/*
11255ee84fbdSgjelinek 		 * TRANSLATION_NOTE
11265ee84fbdSgjelinek 		 * zoneadmd is a literal that should not be translated.
11275ee84fbdSgjelinek 		 */
11287c478bd9Sstevel@tonic-gate 		zperror(gettext("could not exec zoneadmd"), B_FALSE);
11297c478bd9Sstevel@tonic-gate 		_exit(Z_ERR);
11307c478bd9Sstevel@tonic-gate 	} else {
11317c478bd9Sstevel@tonic-gate 		/* parent process */
11327c478bd9Sstevel@tonic-gate 		pid_t retval;
11337c478bd9Sstevel@tonic-gate 		int pstatus = 0;
11347c478bd9Sstevel@tonic-gate 
11357c478bd9Sstevel@tonic-gate 		do {
11367c478bd9Sstevel@tonic-gate 			retval = waitpid(child_pid, &pstatus, 0);
11377c478bd9Sstevel@tonic-gate 		} while (retval != child_pid);
11387c478bd9Sstevel@tonic-gate 		if (WIFSIGNALED(pstatus) || (WIFEXITED(pstatus) &&
11397c478bd9Sstevel@tonic-gate 		    WEXITSTATUS(pstatus) != 0)) {
11407c478bd9Sstevel@tonic-gate 			zerror(gettext("could not start %s"), "zoneadmd");
11417c478bd9Sstevel@tonic-gate 			goto out;
11427c478bd9Sstevel@tonic-gate 		}
11437c478bd9Sstevel@tonic-gate 	}
11447c478bd9Sstevel@tonic-gate 	error = Z_OK;
11457c478bd9Sstevel@tonic-gate out:
11467c478bd9Sstevel@tonic-gate 	release_lock_file(lockfd);
11477c478bd9Sstevel@tonic-gate 	return (error);
11487c478bd9Sstevel@tonic-gate }
11497c478bd9Sstevel@tonic-gate 
11507c478bd9Sstevel@tonic-gate static int
11517c478bd9Sstevel@tonic-gate ping_zoneadmd(const char *zone_name)
11527c478bd9Sstevel@tonic-gate {
11537c478bd9Sstevel@tonic-gate 	char doorpath[PATH_MAX];
11547c478bd9Sstevel@tonic-gate 	int doorfd;
11557c478bd9Sstevel@tonic-gate 	struct door_info info;
11567c478bd9Sstevel@tonic-gate 
1157108322fbScarlsonj 	if (!get_doorname(zone_name, doorpath))
1158108322fbScarlsonj 		return (Z_ERR);
11597c478bd9Sstevel@tonic-gate 
11607c478bd9Sstevel@tonic-gate 	if ((doorfd = open(doorpath, O_RDONLY)) < 0) {
11617c478bd9Sstevel@tonic-gate 		return (Z_ERR);
11627c478bd9Sstevel@tonic-gate 	}
11637c478bd9Sstevel@tonic-gate 	if (door_info(doorfd, &info) == 0 &&
11647c478bd9Sstevel@tonic-gate 	    ((info.di_attributes & DOOR_REVOKED) == 0)) {
11657c478bd9Sstevel@tonic-gate 		(void) close(doorfd);
11667c478bd9Sstevel@tonic-gate 		return (Z_OK);
11677c478bd9Sstevel@tonic-gate 	}
11687c478bd9Sstevel@tonic-gate 	(void) close(doorfd);
11697c478bd9Sstevel@tonic-gate 	return (Z_ERR);
11707c478bd9Sstevel@tonic-gate }
11717c478bd9Sstevel@tonic-gate 
11727c478bd9Sstevel@tonic-gate static int
11737c478bd9Sstevel@tonic-gate call_zoneadmd(const char *zone_name, zone_cmd_arg_t *arg)
11747c478bd9Sstevel@tonic-gate {
11757c478bd9Sstevel@tonic-gate 	char doorpath[PATH_MAX];
11767c478bd9Sstevel@tonic-gate 	int doorfd, result;
11777c478bd9Sstevel@tonic-gate 	door_arg_t darg;
11787c478bd9Sstevel@tonic-gate 
11797c478bd9Sstevel@tonic-gate 	zoneid_t zoneid;
11807c478bd9Sstevel@tonic-gate 	uint64_t uniqid = 0;
11817c478bd9Sstevel@tonic-gate 
11827c478bd9Sstevel@tonic-gate 	zone_cmd_rval_t *rvalp;
11837c478bd9Sstevel@tonic-gate 	size_t rlen;
11847c478bd9Sstevel@tonic-gate 	char *cp, *errbuf;
11857c478bd9Sstevel@tonic-gate 
11867c478bd9Sstevel@tonic-gate 	rlen = getpagesize();
11877c478bd9Sstevel@tonic-gate 	if ((rvalp = malloc(rlen)) == NULL) {
11887c478bd9Sstevel@tonic-gate 		zerror(gettext("failed to allocate %lu bytes: %s"), rlen,
11897c478bd9Sstevel@tonic-gate 		    strerror(errno));
11907c478bd9Sstevel@tonic-gate 		return (-1);
11917c478bd9Sstevel@tonic-gate 	}
11927c478bd9Sstevel@tonic-gate 
11937c478bd9Sstevel@tonic-gate 	if ((zoneid = getzoneidbyname(zone_name)) != ZONE_ID_UNDEFINED) {
11947c478bd9Sstevel@tonic-gate 		(void) zone_getattr(zoneid, ZONE_ATTR_UNIQID, &uniqid,
11957c478bd9Sstevel@tonic-gate 		    sizeof (uniqid));
11967c478bd9Sstevel@tonic-gate 	}
11977c478bd9Sstevel@tonic-gate 	arg->uniqid = uniqid;
11987c478bd9Sstevel@tonic-gate 	(void) strlcpy(arg->locale, locale, sizeof (arg->locale));
1199108322fbScarlsonj 	if (!get_doorname(zone_name, doorpath)) {
1200108322fbScarlsonj 		zerror(gettext("alternate root path is too long"));
1201108322fbScarlsonj 		free(rvalp);
1202108322fbScarlsonj 		return (-1);
1203108322fbScarlsonj 	}
12047c478bd9Sstevel@tonic-gate 
12057c478bd9Sstevel@tonic-gate 	/*
12067c478bd9Sstevel@tonic-gate 	 * Loop trying to start zoneadmd; if something goes seriously
12077c478bd9Sstevel@tonic-gate 	 * wrong we break out and fail.
12087c478bd9Sstevel@tonic-gate 	 */
12097c478bd9Sstevel@tonic-gate 	for (;;) {
12107c478bd9Sstevel@tonic-gate 		if (start_zoneadmd(zone_name) != Z_OK)
12117c478bd9Sstevel@tonic-gate 			break;
12127c478bd9Sstevel@tonic-gate 
12137c478bd9Sstevel@tonic-gate 		if ((doorfd = open(doorpath, O_RDONLY)) < 0) {
12147c478bd9Sstevel@tonic-gate 			zperror(gettext("failed to open zone door"), B_FALSE);
12157c478bd9Sstevel@tonic-gate 			break;
12167c478bd9Sstevel@tonic-gate 		}
12177c478bd9Sstevel@tonic-gate 
12187c478bd9Sstevel@tonic-gate 		darg.data_ptr = (char *)arg;
12197c478bd9Sstevel@tonic-gate 		darg.data_size = sizeof (*arg);
12207c478bd9Sstevel@tonic-gate 		darg.desc_ptr = NULL;
12217c478bd9Sstevel@tonic-gate 		darg.desc_num = 0;
12227c478bd9Sstevel@tonic-gate 		darg.rbuf = (char *)rvalp;
12237c478bd9Sstevel@tonic-gate 		darg.rsize = rlen;
12247c478bd9Sstevel@tonic-gate 		if (door_call(doorfd, &darg) != 0) {
12257c478bd9Sstevel@tonic-gate 			(void) close(doorfd);
12267c478bd9Sstevel@tonic-gate 			/*
12277c478bd9Sstevel@tonic-gate 			 * We'll get EBADF if the door has been revoked.
12287c478bd9Sstevel@tonic-gate 			 */
12297c478bd9Sstevel@tonic-gate 			if (errno != EBADF) {
12307c478bd9Sstevel@tonic-gate 				zperror(gettext("door_call failed"), B_FALSE);
12317c478bd9Sstevel@tonic-gate 				break;
12327c478bd9Sstevel@tonic-gate 			}
12337c478bd9Sstevel@tonic-gate 			continue;	/* take another lap */
12347c478bd9Sstevel@tonic-gate 		}
12357c478bd9Sstevel@tonic-gate 		(void) close(doorfd);
12367c478bd9Sstevel@tonic-gate 
12377c478bd9Sstevel@tonic-gate 		if (darg.data_size == 0) {
12387c478bd9Sstevel@tonic-gate 			/* Door server is going away; kick it again. */
12397c478bd9Sstevel@tonic-gate 			continue;
12407c478bd9Sstevel@tonic-gate 		}
12417c478bd9Sstevel@tonic-gate 
12427c478bd9Sstevel@tonic-gate 		errbuf = rvalp->errbuf;
12437c478bd9Sstevel@tonic-gate 		while (*errbuf != '\0') {
12447c478bd9Sstevel@tonic-gate 			/*
12457c478bd9Sstevel@tonic-gate 			 * Remove any newlines since zerror()
12467c478bd9Sstevel@tonic-gate 			 * will append one automatically.
12477c478bd9Sstevel@tonic-gate 			 */
12487c478bd9Sstevel@tonic-gate 			cp = strchr(errbuf, '\n');
12497c478bd9Sstevel@tonic-gate 			if (cp != NULL)
12507c478bd9Sstevel@tonic-gate 				*cp = '\0';
12517c478bd9Sstevel@tonic-gate 			zerror("%s", errbuf);
12527c478bd9Sstevel@tonic-gate 			if (cp == NULL)
12537c478bd9Sstevel@tonic-gate 				break;
12547c478bd9Sstevel@tonic-gate 			errbuf = cp + 1;
12557c478bd9Sstevel@tonic-gate 		}
12567c478bd9Sstevel@tonic-gate 		result = rvalp->rval == 0 ? 0 : -1;
12577c478bd9Sstevel@tonic-gate 		free(rvalp);
12587c478bd9Sstevel@tonic-gate 		return (result);
12597c478bd9Sstevel@tonic-gate 	}
12607c478bd9Sstevel@tonic-gate 
12617c478bd9Sstevel@tonic-gate 	free(rvalp);
12627c478bd9Sstevel@tonic-gate 	return (-1);
12637c478bd9Sstevel@tonic-gate }
12647c478bd9Sstevel@tonic-gate 
12657c478bd9Sstevel@tonic-gate static int
12667c478bd9Sstevel@tonic-gate ready_func(int argc, char *argv[])
12677c478bd9Sstevel@tonic-gate {
12687c478bd9Sstevel@tonic-gate 	zone_cmd_arg_t zarg;
12697c478bd9Sstevel@tonic-gate 	int arg;
12707c478bd9Sstevel@tonic-gate 
1271108322fbScarlsonj 	if (zonecfg_in_alt_root()) {
1272108322fbScarlsonj 		zerror(gettext("cannot ready zone in alternate root"));
1273108322fbScarlsonj 		return (Z_ERR);
1274108322fbScarlsonj 	}
1275108322fbScarlsonj 
12767c478bd9Sstevel@tonic-gate 	optind = 0;
12777c478bd9Sstevel@tonic-gate 	if ((arg = getopt(argc, argv, "?")) != EOF) {
12787c478bd9Sstevel@tonic-gate 		switch (arg) {
12797c478bd9Sstevel@tonic-gate 		case '?':
12807c478bd9Sstevel@tonic-gate 			sub_usage(SHELP_READY, CMD_READY);
12817c478bd9Sstevel@tonic-gate 			return (optopt == '?' ? Z_OK : Z_USAGE);
12827c478bd9Sstevel@tonic-gate 		default:
12837c478bd9Sstevel@tonic-gate 			sub_usage(SHELP_READY, CMD_READY);
12847c478bd9Sstevel@tonic-gate 			return (Z_USAGE);
12857c478bd9Sstevel@tonic-gate 		}
12867c478bd9Sstevel@tonic-gate 	}
12877c478bd9Sstevel@tonic-gate 	if (argc > optind) {
12887c478bd9Sstevel@tonic-gate 		sub_usage(SHELP_READY, CMD_READY);
12897c478bd9Sstevel@tonic-gate 		return (Z_USAGE);
12907c478bd9Sstevel@tonic-gate 	}
12917c478bd9Sstevel@tonic-gate 	if (sanity_check(target_zone, CMD_READY, B_FALSE, B_FALSE) != Z_OK)
12927c478bd9Sstevel@tonic-gate 		return (Z_ERR);
12937c478bd9Sstevel@tonic-gate 	if (verify_details(CMD_READY) != Z_OK)
12947c478bd9Sstevel@tonic-gate 		return (Z_ERR);
12957c478bd9Sstevel@tonic-gate 
12967c478bd9Sstevel@tonic-gate 	zarg.cmd = Z_READY;
12977c478bd9Sstevel@tonic-gate 	if (call_zoneadmd(target_zone, &zarg) != 0) {
12987c478bd9Sstevel@tonic-gate 		zerror(gettext("call to %s failed"), "zoneadmd");
12997c478bd9Sstevel@tonic-gate 		return (Z_ERR);
13007c478bd9Sstevel@tonic-gate 	}
13017c478bd9Sstevel@tonic-gate 	return (Z_OK);
13027c478bd9Sstevel@tonic-gate }
13037c478bd9Sstevel@tonic-gate 
13047c478bd9Sstevel@tonic-gate static int
13057c478bd9Sstevel@tonic-gate boot_func(int argc, char *argv[])
13067c478bd9Sstevel@tonic-gate {
13077c478bd9Sstevel@tonic-gate 	zone_cmd_arg_t zarg;
13087c478bd9Sstevel@tonic-gate 	int arg;
13097c478bd9Sstevel@tonic-gate 
1310108322fbScarlsonj 	if (zonecfg_in_alt_root()) {
1311108322fbScarlsonj 		zerror(gettext("cannot boot zone in alternate root"));
1312108322fbScarlsonj 		return (Z_ERR);
1313108322fbScarlsonj 	}
1314108322fbScarlsonj 
13157c478bd9Sstevel@tonic-gate 	zarg.bootbuf[0] = '\0';
13167c478bd9Sstevel@tonic-gate 
13177c478bd9Sstevel@tonic-gate 	/*
13187c478bd9Sstevel@tonic-gate 	 * At the current time, the only supported subargument to the
13197c478bd9Sstevel@tonic-gate 	 * "boot" subcommand is "-s" which specifies a single-user boot.
13207c478bd9Sstevel@tonic-gate 	 * In the future, other boot arguments should be supported
13217c478bd9Sstevel@tonic-gate 	 * including "-m" for specifying alternate smf(5) milestones.
13227c478bd9Sstevel@tonic-gate 	 */
13237c478bd9Sstevel@tonic-gate 	optind = 0;
13247c478bd9Sstevel@tonic-gate 	if ((arg = getopt(argc, argv, "?s")) != EOF) {
13257c478bd9Sstevel@tonic-gate 		switch (arg) {
13267c478bd9Sstevel@tonic-gate 		case '?':
13277c478bd9Sstevel@tonic-gate 			sub_usage(SHELP_BOOT, CMD_BOOT);
13287c478bd9Sstevel@tonic-gate 			return (optopt == '?' ? Z_OK : Z_USAGE);
13297c478bd9Sstevel@tonic-gate 		case 's':
13307c478bd9Sstevel@tonic-gate 			(void) strlcpy(zarg.bootbuf, "-s",
13317c478bd9Sstevel@tonic-gate 			    sizeof (zarg.bootbuf));
13327c478bd9Sstevel@tonic-gate 			break;
13337c478bd9Sstevel@tonic-gate 		default:
13347c478bd9Sstevel@tonic-gate 			sub_usage(SHELP_BOOT, CMD_BOOT);
13357c478bd9Sstevel@tonic-gate 			return (Z_USAGE);
13367c478bd9Sstevel@tonic-gate 		}
13377c478bd9Sstevel@tonic-gate 	}
13387c478bd9Sstevel@tonic-gate 	if (argc > optind) {
13397c478bd9Sstevel@tonic-gate 		sub_usage(SHELP_BOOT, CMD_BOOT);
13407c478bd9Sstevel@tonic-gate 		return (Z_USAGE);
13417c478bd9Sstevel@tonic-gate 	}
13427c478bd9Sstevel@tonic-gate 	if (sanity_check(target_zone, CMD_BOOT, B_FALSE, B_FALSE) != Z_OK)
13437c478bd9Sstevel@tonic-gate 		return (Z_ERR);
13447c478bd9Sstevel@tonic-gate 	if (verify_details(CMD_BOOT) != Z_OK)
13457c478bd9Sstevel@tonic-gate 		return (Z_ERR);
13467c478bd9Sstevel@tonic-gate 	zarg.cmd = Z_BOOT;
13477c478bd9Sstevel@tonic-gate 	if (call_zoneadmd(target_zone, &zarg) != 0) {
13487c478bd9Sstevel@tonic-gate 		zerror(gettext("call to %s failed"), "zoneadmd");
13497c478bd9Sstevel@tonic-gate 		return (Z_ERR);
13507c478bd9Sstevel@tonic-gate 	}
13517c478bd9Sstevel@tonic-gate 	return (Z_OK);
13527c478bd9Sstevel@tonic-gate }
13537c478bd9Sstevel@tonic-gate 
13547c478bd9Sstevel@tonic-gate static void
13557c478bd9Sstevel@tonic-gate fake_up_local_zone(zoneid_t zid, zone_entry_t *zeptr)
13567c478bd9Sstevel@tonic-gate {
13577c478bd9Sstevel@tonic-gate 	ssize_t result;
13587c478bd9Sstevel@tonic-gate 
13597c478bd9Sstevel@tonic-gate 	zeptr->zid = zid;
13607c478bd9Sstevel@tonic-gate 	/*
13617c478bd9Sstevel@tonic-gate 	 * Since we're looking up our own (non-global) zone name,
13627c478bd9Sstevel@tonic-gate 	 * we can be assured that it will succeed.
13637c478bd9Sstevel@tonic-gate 	 */
13647c478bd9Sstevel@tonic-gate 	result = getzonenamebyid(zid, zeptr->zname, sizeof (zeptr->zname));
13657c478bd9Sstevel@tonic-gate 	assert(result >= 0);
136645916cd2Sjpk 	if (!is_system_labeled()) {
13677c478bd9Sstevel@tonic-gate 		(void) strlcpy(zeptr->zroot, "/", sizeof (zeptr->zroot));
136845916cd2Sjpk 	} else {
136945916cd2Sjpk 		(void) zone_getattr(zid, ZONE_ATTR_ROOT, zeptr->zroot,
137045916cd2Sjpk 		    sizeof (zeptr->zroot));
137145916cd2Sjpk 	}
13727c478bd9Sstevel@tonic-gate 	zeptr->zstate_str = "running";
13737c478bd9Sstevel@tonic-gate }
13747c478bd9Sstevel@tonic-gate 
13757c478bd9Sstevel@tonic-gate static int
13767c478bd9Sstevel@tonic-gate list_func(int argc, char *argv[])
13777c478bd9Sstevel@tonic-gate {
13787c478bd9Sstevel@tonic-gate 	zone_entry_t *zentp, zent;
1379108322fbScarlsonj 	int arg, retv;
13807c478bd9Sstevel@tonic-gate 	boolean_t output = B_FALSE, verbose = B_FALSE, parsable = B_FALSE;
13817c478bd9Sstevel@tonic-gate 	zone_state_t min_state = ZONE_STATE_RUNNING;
13827c478bd9Sstevel@tonic-gate 	zoneid_t zone_id = getzoneid();
13837c478bd9Sstevel@tonic-gate 
13847c478bd9Sstevel@tonic-gate 	if (target_zone == NULL) {
13857c478bd9Sstevel@tonic-gate 		/* all zones: default view to running but allow override */
13867c478bd9Sstevel@tonic-gate 		optind = 0;
13877c478bd9Sstevel@tonic-gate 		while ((arg = getopt(argc, argv, "?cipv")) != EOF) {
13887c478bd9Sstevel@tonic-gate 			switch (arg) {
13897c478bd9Sstevel@tonic-gate 			case '?':
13907c478bd9Sstevel@tonic-gate 				sub_usage(SHELP_LIST, CMD_LIST);
13917c478bd9Sstevel@tonic-gate 				return (optopt == '?' ? Z_OK : Z_USAGE);
1392475d78a4Sjonb 				/*
1393475d78a4Sjonb 				 * The 'i' and 'c' options are not mutually
1394475d78a4Sjonb 				 * exclusive so if 'c' is given, then min_state
1395475d78a4Sjonb 				 * is set to 0 (ZONE_STATE_CONFIGURED) which is
1396475d78a4Sjonb 				 * the lowest possible state.  If 'i' is given,
1397475d78a4Sjonb 				 * then min_state is set to be the lowest state
1398475d78a4Sjonb 				 * so far.
1399475d78a4Sjonb 				 */
14007c478bd9Sstevel@tonic-gate 			case 'c':
14017c478bd9Sstevel@tonic-gate 				min_state = ZONE_STATE_CONFIGURED;
14027c478bd9Sstevel@tonic-gate 				break;
14037c478bd9Sstevel@tonic-gate 			case 'i':
1404475d78a4Sjonb 				min_state = min(ZONE_STATE_INSTALLED,
1405475d78a4Sjonb 				    min_state);
1406475d78a4Sjonb 
14077c478bd9Sstevel@tonic-gate 				break;
14087c478bd9Sstevel@tonic-gate 			case 'p':
14097c478bd9Sstevel@tonic-gate 				parsable = B_TRUE;
14107c478bd9Sstevel@tonic-gate 				break;
14117c478bd9Sstevel@tonic-gate 			case 'v':
14127c478bd9Sstevel@tonic-gate 				verbose = B_TRUE;
14137c478bd9Sstevel@tonic-gate 				break;
14147c478bd9Sstevel@tonic-gate 			default:
14157c478bd9Sstevel@tonic-gate 				sub_usage(SHELP_LIST, CMD_LIST);
14167c478bd9Sstevel@tonic-gate 				return (Z_USAGE);
14177c478bd9Sstevel@tonic-gate 			}
14187c478bd9Sstevel@tonic-gate 		}
14197c478bd9Sstevel@tonic-gate 		if (parsable && verbose) {
14207c478bd9Sstevel@tonic-gate 			zerror(gettext("%s -p and -v are mutually exclusive."),
14217c478bd9Sstevel@tonic-gate 			    cmd_to_str(CMD_LIST));
14227c478bd9Sstevel@tonic-gate 			return (Z_ERR);
14237c478bd9Sstevel@tonic-gate 		}
142445916cd2Sjpk 		if (zone_id == GLOBAL_ZONEID || is_system_labeled()) {
1425108322fbScarlsonj 			retv = zone_print_list(min_state, verbose, parsable);
14267c478bd9Sstevel@tonic-gate 		} else {
1427108322fbScarlsonj 			retv = Z_OK;
14287c478bd9Sstevel@tonic-gate 			fake_up_local_zone(zone_id, &zent);
14297c478bd9Sstevel@tonic-gate 			zone_print(&zent, verbose, parsable);
14307c478bd9Sstevel@tonic-gate 		}
1431108322fbScarlsonj 		return (retv);
14327c478bd9Sstevel@tonic-gate 	}
14337c478bd9Sstevel@tonic-gate 
14347c478bd9Sstevel@tonic-gate 	/*
14357c478bd9Sstevel@tonic-gate 	 * Specific target zone: disallow -i/-c suboptions.
14367c478bd9Sstevel@tonic-gate 	 */
14377c478bd9Sstevel@tonic-gate 	optind = 0;
14387c478bd9Sstevel@tonic-gate 	while ((arg = getopt(argc, argv, "?pv")) != EOF) {
14397c478bd9Sstevel@tonic-gate 		switch (arg) {
14407c478bd9Sstevel@tonic-gate 		case '?':
14417c478bd9Sstevel@tonic-gate 			sub_usage(SHELP_LIST, CMD_LIST);
14427c478bd9Sstevel@tonic-gate 			return (optopt == '?' ? Z_OK : Z_USAGE);
14437c478bd9Sstevel@tonic-gate 		case 'p':
14447c478bd9Sstevel@tonic-gate 			parsable = B_TRUE;
14457c478bd9Sstevel@tonic-gate 			break;
14467c478bd9Sstevel@tonic-gate 		case 'v':
14477c478bd9Sstevel@tonic-gate 			verbose = B_TRUE;
14487c478bd9Sstevel@tonic-gate 			break;
14497c478bd9Sstevel@tonic-gate 		default:
14507c478bd9Sstevel@tonic-gate 			sub_usage(SHELP_LIST, CMD_LIST);
14517c478bd9Sstevel@tonic-gate 			return (Z_USAGE);
14527c478bd9Sstevel@tonic-gate 		}
14537c478bd9Sstevel@tonic-gate 	}
14547c478bd9Sstevel@tonic-gate 	if (parsable && verbose) {
14557c478bd9Sstevel@tonic-gate 		zerror(gettext("%s -p and -v are mutually exclusive."),
14567c478bd9Sstevel@tonic-gate 		    cmd_to_str(CMD_LIST));
14577c478bd9Sstevel@tonic-gate 		return (Z_ERR);
14587c478bd9Sstevel@tonic-gate 	}
14597c478bd9Sstevel@tonic-gate 	if (argc > optind) {
14607c478bd9Sstevel@tonic-gate 		sub_usage(SHELP_LIST, CMD_LIST);
14617c478bd9Sstevel@tonic-gate 		return (Z_USAGE);
14627c478bd9Sstevel@tonic-gate 	}
14637c478bd9Sstevel@tonic-gate 	if (zone_id != GLOBAL_ZONEID) {
14647c478bd9Sstevel@tonic-gate 		fake_up_local_zone(zone_id, &zent);
14657c478bd9Sstevel@tonic-gate 		/*
14667c478bd9Sstevel@tonic-gate 		 * main() will issue a Z_NO_ZONE error if it cannot get an
14677c478bd9Sstevel@tonic-gate 		 * id for target_zone, which in a non-global zone should
14687c478bd9Sstevel@tonic-gate 		 * happen for any zone name except `zonename`.  Thus we
14697c478bd9Sstevel@tonic-gate 		 * assert() that here but don't otherwise check.
14707c478bd9Sstevel@tonic-gate 		 */
14717c478bd9Sstevel@tonic-gate 		assert(strcmp(zent.zname, target_zone) == 0);
14727c478bd9Sstevel@tonic-gate 		zone_print(&zent, verbose, parsable);
14737c478bd9Sstevel@tonic-gate 		output = B_TRUE;
14747c478bd9Sstevel@tonic-gate 	} else if ((zentp = lookup_running_zone(target_zone)) != NULL) {
14757c478bd9Sstevel@tonic-gate 		zone_print(zentp, verbose, parsable);
14767c478bd9Sstevel@tonic-gate 		output = B_TRUE;
1477108322fbScarlsonj 	} else if (lookup_zone_info(target_zone, ZONE_ID_UNDEFINED,
1478108322fbScarlsonj 	    &zent) == Z_OK) {
14797c478bd9Sstevel@tonic-gate 		zone_print(&zent, verbose, parsable);
14807c478bd9Sstevel@tonic-gate 		output = B_TRUE;
14817c478bd9Sstevel@tonic-gate 	}
14827c478bd9Sstevel@tonic-gate 	return (output ? Z_OK : Z_ERR);
14837c478bd9Sstevel@tonic-gate }
14847c478bd9Sstevel@tonic-gate 
14857c478bd9Sstevel@tonic-gate static void
14867c478bd9Sstevel@tonic-gate sigterm(int sig)
14877c478bd9Sstevel@tonic-gate {
14887c478bd9Sstevel@tonic-gate 	/*
14897c478bd9Sstevel@tonic-gate 	 * Ignore SIG{INT,TERM}, so we don't end up in an infinite loop,
14907c478bd9Sstevel@tonic-gate 	 * then propagate the signal to our process group.
14917c478bd9Sstevel@tonic-gate 	 */
14927c478bd9Sstevel@tonic-gate 	(void) sigset(SIGINT, SIG_IGN);
14937c478bd9Sstevel@tonic-gate 	(void) sigset(SIGTERM, SIG_IGN);
14947c478bd9Sstevel@tonic-gate 	(void) kill(0, sig);
14957c478bd9Sstevel@tonic-gate 	child_killed = B_TRUE;
14967c478bd9Sstevel@tonic-gate }
14977c478bd9Sstevel@tonic-gate 
14987c478bd9Sstevel@tonic-gate static int
14997c478bd9Sstevel@tonic-gate do_subproc(char *cmdbuf)
15007c478bd9Sstevel@tonic-gate {
15017c478bd9Sstevel@tonic-gate 	char inbuf[1024];	/* arbitrary large amount */
15027c478bd9Sstevel@tonic-gate 	FILE *file;
15037c478bd9Sstevel@tonic-gate 
15047c478bd9Sstevel@tonic-gate 	child_killed = B_FALSE;
15057c478bd9Sstevel@tonic-gate 	/*
15067c478bd9Sstevel@tonic-gate 	 * We use popen(3c) to launch child processes for [un]install;
15077c478bd9Sstevel@tonic-gate 	 * this library call does not return a PID, so we have to kill
15087c478bd9Sstevel@tonic-gate 	 * the whole process group.  To avoid killing our parent, we
15097c478bd9Sstevel@tonic-gate 	 * become a process group leader here.  But doing so can wreak
15107c478bd9Sstevel@tonic-gate 	 * havoc with reading from stdin when launched by a non-job-control
15117c478bd9Sstevel@tonic-gate 	 * shell, so we close stdin and reopen it as /dev/null first.
15127c478bd9Sstevel@tonic-gate 	 */
15137c478bd9Sstevel@tonic-gate 	(void) close(STDIN_FILENO);
15147c478bd9Sstevel@tonic-gate 	(void) open("/dev/null", O_RDONLY);
15157c478bd9Sstevel@tonic-gate 	(void) setpgid(0, 0);
15167c478bd9Sstevel@tonic-gate 	(void) sigset(SIGINT, sigterm);
15177c478bd9Sstevel@tonic-gate 	(void) sigset(SIGTERM, sigterm);
15187c478bd9Sstevel@tonic-gate 	file = popen(cmdbuf, "r");
15197c478bd9Sstevel@tonic-gate 	for (;;) {
15207c478bd9Sstevel@tonic-gate 		if (child_killed || fgets(inbuf, sizeof (inbuf), file) == NULL)
15217c478bd9Sstevel@tonic-gate 			break;
15227c478bd9Sstevel@tonic-gate 		(void) fputs(inbuf, stdout);
15237c478bd9Sstevel@tonic-gate 	}
15247c478bd9Sstevel@tonic-gate 	(void) sigset(SIGINT, SIG_DFL);
15257c478bd9Sstevel@tonic-gate 	(void) sigset(SIGTERM, SIG_DFL);
15267c478bd9Sstevel@tonic-gate 	return (pclose(file));
15277c478bd9Sstevel@tonic-gate }
15287c478bd9Sstevel@tonic-gate 
15297c478bd9Sstevel@tonic-gate static int
15307c478bd9Sstevel@tonic-gate subproc_status(const char *cmd, int status)
15317c478bd9Sstevel@tonic-gate {
15327c478bd9Sstevel@tonic-gate 	if (WIFEXITED(status)) {
15337c478bd9Sstevel@tonic-gate 		int exit_code = WEXITSTATUS(status);
15347c478bd9Sstevel@tonic-gate 
15357c478bd9Sstevel@tonic-gate 		if (exit_code == 0)
15367c478bd9Sstevel@tonic-gate 			return (Z_OK);
15377c478bd9Sstevel@tonic-gate 		zerror(gettext("'%s' failed with exit code %d."), cmd,
15387c478bd9Sstevel@tonic-gate 		    exit_code);
15397c478bd9Sstevel@tonic-gate 	} else if (WIFSIGNALED(status)) {
15407c478bd9Sstevel@tonic-gate 		int signal = WTERMSIG(status);
15417c478bd9Sstevel@tonic-gate 		char sigstr[SIG2STR_MAX];
15427c478bd9Sstevel@tonic-gate 
15437c478bd9Sstevel@tonic-gate 		if (sig2str(signal, sigstr) == 0) {
15447c478bd9Sstevel@tonic-gate 			zerror(gettext("'%s' terminated by signal SIG%s."), cmd,
15457c478bd9Sstevel@tonic-gate 			    sigstr);
15467c478bd9Sstevel@tonic-gate 		} else {
15477c478bd9Sstevel@tonic-gate 			zerror(gettext("'%s' terminated by an unknown signal."),
15487c478bd9Sstevel@tonic-gate 			    cmd);
15497c478bd9Sstevel@tonic-gate 		}
15507c478bd9Sstevel@tonic-gate 	} else {
15517c478bd9Sstevel@tonic-gate 		zerror(gettext("'%s' failed for unknown reasons."), cmd);
15527c478bd9Sstevel@tonic-gate 	}
15537c478bd9Sstevel@tonic-gate 	return (Z_ERR);
15547c478bd9Sstevel@tonic-gate }
15557c478bd9Sstevel@tonic-gate 
15567c478bd9Sstevel@tonic-gate /*
15577c478bd9Sstevel@tonic-gate  * Various sanity checks; make sure:
15587c478bd9Sstevel@tonic-gate  * 1. We're in the global zone.
15597c478bd9Sstevel@tonic-gate  * 2. The calling user has sufficient privilege.
15607c478bd9Sstevel@tonic-gate  * 3. The target zone is neither the global zone nor anything starting with
15617c478bd9Sstevel@tonic-gate  *    "SUNW".
15627c478bd9Sstevel@tonic-gate  * 4a. If we're looking for a 'not running' (i.e., configured or installed)
15637c478bd9Sstevel@tonic-gate  *     zone, the name service knows about it.
15647c478bd9Sstevel@tonic-gate  * 4b. For some operations which expect a zone not to be running, that it is
15657c478bd9Sstevel@tonic-gate  *     not already running (or ready).
15667c478bd9Sstevel@tonic-gate  */
15677c478bd9Sstevel@tonic-gate static int
15687c478bd9Sstevel@tonic-gate sanity_check(char *zone, int cmd_num, boolean_t running,
15697c478bd9Sstevel@tonic-gate     boolean_t unsafe_when_running)
15707c478bd9Sstevel@tonic-gate {
15717c478bd9Sstevel@tonic-gate 	zone_entry_t *zent;
15727c478bd9Sstevel@tonic-gate 	priv_set_t *privset;
15737c478bd9Sstevel@tonic-gate 	zone_state_t state;
1574108322fbScarlsonj 	char kernzone[ZONENAME_MAX];
1575108322fbScarlsonj 	FILE *fp;
15767c478bd9Sstevel@tonic-gate 
15777c478bd9Sstevel@tonic-gate 	if (getzoneid() != GLOBAL_ZONEID) {
1578ffbafc53Scomay 		switch (cmd_num) {
1579ffbafc53Scomay 		case CMD_HALT:
1580ffbafc53Scomay 			zerror(gettext("use %s to %s this zone."), "halt(1M)",
15817c478bd9Sstevel@tonic-gate 			    cmd_to_str(cmd_num));
1582ffbafc53Scomay 			break;
1583ffbafc53Scomay 		case CMD_REBOOT:
1584ffbafc53Scomay 			zerror(gettext("use %s to %s this zone."),
1585ffbafc53Scomay 			    "reboot(1M)", cmd_to_str(cmd_num));
1586ffbafc53Scomay 			break;
1587ffbafc53Scomay 		default:
1588ffbafc53Scomay 			zerror(gettext("must be in the global zone to %s a "
1589ffbafc53Scomay 			    "zone."), cmd_to_str(cmd_num));
1590ffbafc53Scomay 			break;
1591ffbafc53Scomay 		}
15927c478bd9Sstevel@tonic-gate 		return (Z_ERR);
15937c478bd9Sstevel@tonic-gate 	}
15947c478bd9Sstevel@tonic-gate 
15957c478bd9Sstevel@tonic-gate 	if ((privset = priv_allocset()) == NULL) {
15967c478bd9Sstevel@tonic-gate 		zerror(gettext("%s failed"), "priv_allocset");
15977c478bd9Sstevel@tonic-gate 		return (Z_ERR);
15987c478bd9Sstevel@tonic-gate 	}
15997c478bd9Sstevel@tonic-gate 
16007c478bd9Sstevel@tonic-gate 	if (getppriv(PRIV_EFFECTIVE, privset) != 0) {
16017c478bd9Sstevel@tonic-gate 		zerror(gettext("%s failed"), "getppriv");
16027c478bd9Sstevel@tonic-gate 		priv_freeset(privset);
16037c478bd9Sstevel@tonic-gate 		return (Z_ERR);
16047c478bd9Sstevel@tonic-gate 	}
16057c478bd9Sstevel@tonic-gate 
16067c478bd9Sstevel@tonic-gate 	if (priv_isfullset(privset) == B_FALSE) {
16077c478bd9Sstevel@tonic-gate 		zerror(gettext("only a privileged user may %s a zone."),
16087c478bd9Sstevel@tonic-gate 		    cmd_to_str(cmd_num));
16097c478bd9Sstevel@tonic-gate 		priv_freeset(privset);
16107c478bd9Sstevel@tonic-gate 		return (Z_ERR);
16117c478bd9Sstevel@tonic-gate 	}
16127c478bd9Sstevel@tonic-gate 	priv_freeset(privset);
16137c478bd9Sstevel@tonic-gate 
16147c478bd9Sstevel@tonic-gate 	if (zone == NULL) {
16157c478bd9Sstevel@tonic-gate 		zerror(gettext("no zone specified"));
16167c478bd9Sstevel@tonic-gate 		return (Z_ERR);
16177c478bd9Sstevel@tonic-gate 	}
16187c478bd9Sstevel@tonic-gate 
16197c478bd9Sstevel@tonic-gate 	if (strcmp(zone, GLOBAL_ZONENAME) == 0) {
16207c478bd9Sstevel@tonic-gate 		zerror(gettext("%s operation is invalid for the global zone."),
16217c478bd9Sstevel@tonic-gate 		    cmd_to_str(cmd_num));
16227c478bd9Sstevel@tonic-gate 		return (Z_ERR);
16237c478bd9Sstevel@tonic-gate 	}
16247c478bd9Sstevel@tonic-gate 
16257c478bd9Sstevel@tonic-gate 	if (strncmp(zone, "SUNW", 4) == 0) {
16267c478bd9Sstevel@tonic-gate 		zerror(gettext("%s operation is invalid for zones starting "
16277c478bd9Sstevel@tonic-gate 		    "with SUNW."), cmd_to_str(cmd_num));
16287c478bd9Sstevel@tonic-gate 		return (Z_ERR);
16297c478bd9Sstevel@tonic-gate 	}
16307c478bd9Sstevel@tonic-gate 
1631108322fbScarlsonj 	if (!zonecfg_in_alt_root()) {
1632108322fbScarlsonj 		zent = lookup_running_zone(zone);
1633108322fbScarlsonj 	} else if ((fp = zonecfg_open_scratch("", B_FALSE)) == NULL) {
1634108322fbScarlsonj 		zent = NULL;
1635108322fbScarlsonj 	} else {
1636108322fbScarlsonj 		if (zonecfg_find_scratch(fp, zone, zonecfg_get_root(),
1637108322fbScarlsonj 		    kernzone, sizeof (kernzone)) == 0)
1638108322fbScarlsonj 			zent = lookup_running_zone(kernzone);
1639108322fbScarlsonj 		else
1640108322fbScarlsonj 			zent = NULL;
1641108322fbScarlsonj 		zonecfg_close_scratch(fp);
1642108322fbScarlsonj 	}
1643108322fbScarlsonj 
16447c478bd9Sstevel@tonic-gate 	/*
16457c478bd9Sstevel@tonic-gate 	 * Look up from the kernel for 'running' zones.
16467c478bd9Sstevel@tonic-gate 	 */
16477c478bd9Sstevel@tonic-gate 	if (running) {
16487c478bd9Sstevel@tonic-gate 		if (zent == NULL) {
16497c478bd9Sstevel@tonic-gate 			zerror(gettext("not running"));
16507c478bd9Sstevel@tonic-gate 			return (Z_ERR);
16517c478bd9Sstevel@tonic-gate 		}
16527c478bd9Sstevel@tonic-gate 	} else {
16537c478bd9Sstevel@tonic-gate 		int err;
16547c478bd9Sstevel@tonic-gate 
16557c478bd9Sstevel@tonic-gate 		if (unsafe_when_running && zent != NULL) {
16567c478bd9Sstevel@tonic-gate 			/* check whether the zone is ready or running */
16577c478bd9Sstevel@tonic-gate 			if ((err = zone_get_state(zent->zname,
16587c478bd9Sstevel@tonic-gate 			    &zent->zstate_num)) != Z_OK) {
16597c478bd9Sstevel@tonic-gate 				errno = err;
16607c478bd9Sstevel@tonic-gate 				zperror2(zent->zname,
16617c478bd9Sstevel@tonic-gate 				    gettext("could not get state"));
16627c478bd9Sstevel@tonic-gate 				/* can't tell, so hedge */
16637c478bd9Sstevel@tonic-gate 				zent->zstate_str = "ready/running";
16647c478bd9Sstevel@tonic-gate 			} else {
16657c478bd9Sstevel@tonic-gate 				zent->zstate_str =
16667c478bd9Sstevel@tonic-gate 				    zone_state_str(zent->zstate_num);
16677c478bd9Sstevel@tonic-gate 			}
16687c478bd9Sstevel@tonic-gate 			zerror(gettext("%s operation is invalid for %s zones."),
16697c478bd9Sstevel@tonic-gate 			    cmd_to_str(cmd_num), zent->zstate_str);
16707c478bd9Sstevel@tonic-gate 			return (Z_ERR);
16717c478bd9Sstevel@tonic-gate 		}
16727c478bd9Sstevel@tonic-gate 		if ((err = zone_get_state(zone, &state)) != Z_OK) {
16737c478bd9Sstevel@tonic-gate 			errno = err;
16747c478bd9Sstevel@tonic-gate 			zperror2(zone, gettext("could not get state"));
16757c478bd9Sstevel@tonic-gate 			return (Z_ERR);
16767c478bd9Sstevel@tonic-gate 		}
16777c478bd9Sstevel@tonic-gate 		switch (cmd_num) {
16787c478bd9Sstevel@tonic-gate 		case CMD_UNINSTALL:
16797c478bd9Sstevel@tonic-gate 			if (state == ZONE_STATE_CONFIGURED) {
16807c478bd9Sstevel@tonic-gate 				zerror(gettext("is already in state '%s'."),
16817c478bd9Sstevel@tonic-gate 				    zone_state_str(ZONE_STATE_CONFIGURED));
16827c478bd9Sstevel@tonic-gate 				return (Z_ERR);
16837c478bd9Sstevel@tonic-gate 			}
16847c478bd9Sstevel@tonic-gate 			break;
1685ee519a1fSgjelinek 		case CMD_ATTACH:
1686865e09a4Sgjelinek 		case CMD_CLONE:
16877c478bd9Sstevel@tonic-gate 		case CMD_INSTALL:
16887c478bd9Sstevel@tonic-gate 			if (state == ZONE_STATE_INSTALLED) {
16897c478bd9Sstevel@tonic-gate 				zerror(gettext("is already %s."),
16907c478bd9Sstevel@tonic-gate 				    zone_state_str(ZONE_STATE_INSTALLED));
16917c478bd9Sstevel@tonic-gate 				return (Z_ERR);
16927c478bd9Sstevel@tonic-gate 			} else if (state == ZONE_STATE_INCOMPLETE) {
16937c478bd9Sstevel@tonic-gate 				zerror(gettext("zone is %s; %s required."),
16947c478bd9Sstevel@tonic-gate 				    zone_state_str(ZONE_STATE_INCOMPLETE),
16957c478bd9Sstevel@tonic-gate 				    cmd_to_str(CMD_UNINSTALL));
16967c478bd9Sstevel@tonic-gate 				return (Z_ERR);
16977c478bd9Sstevel@tonic-gate 			}
16987c478bd9Sstevel@tonic-gate 			break;
1699ee519a1fSgjelinek 		case CMD_DETACH:
1700865e09a4Sgjelinek 		case CMD_MOVE:
17017c478bd9Sstevel@tonic-gate 		case CMD_READY:
17027c478bd9Sstevel@tonic-gate 		case CMD_BOOT:
1703108322fbScarlsonj 		case CMD_MOUNT:
17047c478bd9Sstevel@tonic-gate 			if (state < ZONE_STATE_INSTALLED) {
17057c478bd9Sstevel@tonic-gate 				zerror(gettext("must be %s before %s."),
17067c478bd9Sstevel@tonic-gate 				    zone_state_str(ZONE_STATE_INSTALLED),
17077c478bd9Sstevel@tonic-gate 				    cmd_to_str(cmd_num));
17087c478bd9Sstevel@tonic-gate 				return (Z_ERR);
17097c478bd9Sstevel@tonic-gate 			}
17107c478bd9Sstevel@tonic-gate 			break;
17117c478bd9Sstevel@tonic-gate 		case CMD_VERIFY:
17127c478bd9Sstevel@tonic-gate 			if (state == ZONE_STATE_INCOMPLETE) {
17137c478bd9Sstevel@tonic-gate 				zerror(gettext("zone is %s; %s required."),
17147c478bd9Sstevel@tonic-gate 				    zone_state_str(ZONE_STATE_INCOMPLETE),
17157c478bd9Sstevel@tonic-gate 				    cmd_to_str(CMD_UNINSTALL));
17167c478bd9Sstevel@tonic-gate 				return (Z_ERR);
17177c478bd9Sstevel@tonic-gate 			}
17187c478bd9Sstevel@tonic-gate 			break;
1719108322fbScarlsonj 		case CMD_UNMOUNT:
1720108322fbScarlsonj 			if (state != ZONE_STATE_MOUNTED) {
1721108322fbScarlsonj 				zerror(gettext("must be %s before %s."),
1722108322fbScarlsonj 				    zone_state_str(ZONE_STATE_MOUNTED),
1723108322fbScarlsonj 				    cmd_to_str(cmd_num));
1724108322fbScarlsonj 				return (Z_ERR);
1725108322fbScarlsonj 			}
1726108322fbScarlsonj 			break;
17277c478bd9Sstevel@tonic-gate 		}
17287c478bd9Sstevel@tonic-gate 	}
17297c478bd9Sstevel@tonic-gate 	return (Z_OK);
17307c478bd9Sstevel@tonic-gate }
17317c478bd9Sstevel@tonic-gate 
17327c478bd9Sstevel@tonic-gate static int
17337c478bd9Sstevel@tonic-gate halt_func(int argc, char *argv[])
17347c478bd9Sstevel@tonic-gate {
17357c478bd9Sstevel@tonic-gate 	zone_cmd_arg_t zarg;
17367c478bd9Sstevel@tonic-gate 	int arg;
17377c478bd9Sstevel@tonic-gate 
1738108322fbScarlsonj 	if (zonecfg_in_alt_root()) {
1739108322fbScarlsonj 		zerror(gettext("cannot halt zone in alternate root"));
1740108322fbScarlsonj 		return (Z_ERR);
1741108322fbScarlsonj 	}
1742108322fbScarlsonj 
17437c478bd9Sstevel@tonic-gate 	optind = 0;
17447c478bd9Sstevel@tonic-gate 	if ((arg = getopt(argc, argv, "?")) != EOF) {
17457c478bd9Sstevel@tonic-gate 		switch (arg) {
17467c478bd9Sstevel@tonic-gate 		case '?':
17477c478bd9Sstevel@tonic-gate 			sub_usage(SHELP_HALT, CMD_HALT);
17487c478bd9Sstevel@tonic-gate 			return (optopt == '?' ? Z_OK : Z_USAGE);
17497c478bd9Sstevel@tonic-gate 		default:
17507c478bd9Sstevel@tonic-gate 			sub_usage(SHELP_HALT, CMD_HALT);
17517c478bd9Sstevel@tonic-gate 			return (Z_USAGE);
17527c478bd9Sstevel@tonic-gate 		}
17537c478bd9Sstevel@tonic-gate 	}
17547c478bd9Sstevel@tonic-gate 	if (argc > optind) {
17557c478bd9Sstevel@tonic-gate 		sub_usage(SHELP_HALT, CMD_HALT);
17567c478bd9Sstevel@tonic-gate 		return (Z_USAGE);
17577c478bd9Sstevel@tonic-gate 	}
17587c478bd9Sstevel@tonic-gate 	/*
17597c478bd9Sstevel@tonic-gate 	 * zoneadmd should be the one to decide whether or not to proceed,
17607c478bd9Sstevel@tonic-gate 	 * so even though it seems that the fourth parameter below should
17617c478bd9Sstevel@tonic-gate 	 * perhaps be B_TRUE, it really shouldn't be.
17627c478bd9Sstevel@tonic-gate 	 */
17637c478bd9Sstevel@tonic-gate 	if (sanity_check(target_zone, CMD_HALT, B_FALSE, B_FALSE) != Z_OK)
17647c478bd9Sstevel@tonic-gate 		return (Z_ERR);
17657c478bd9Sstevel@tonic-gate 
17667c478bd9Sstevel@tonic-gate 	zarg.cmd = Z_HALT;
17677c478bd9Sstevel@tonic-gate 	return ((call_zoneadmd(target_zone, &zarg) == 0) ? Z_OK : Z_ERR);
17687c478bd9Sstevel@tonic-gate }
17697c478bd9Sstevel@tonic-gate 
17707c478bd9Sstevel@tonic-gate static int
17717c478bd9Sstevel@tonic-gate reboot_func(int argc, char *argv[])
17727c478bd9Sstevel@tonic-gate {
17737c478bd9Sstevel@tonic-gate 	zone_cmd_arg_t zarg;
17747c478bd9Sstevel@tonic-gate 	int arg;
17757c478bd9Sstevel@tonic-gate 
1776108322fbScarlsonj 	if (zonecfg_in_alt_root()) {
1777108322fbScarlsonj 		zerror(gettext("cannot reboot zone in alternate root"));
1778108322fbScarlsonj 		return (Z_ERR);
1779108322fbScarlsonj 	}
1780108322fbScarlsonj 
17817c478bd9Sstevel@tonic-gate 	optind = 0;
17827c478bd9Sstevel@tonic-gate 	if ((arg = getopt(argc, argv, "?")) != EOF) {
17837c478bd9Sstevel@tonic-gate 		switch (arg) {
17847c478bd9Sstevel@tonic-gate 		case '?':
17857c478bd9Sstevel@tonic-gate 			sub_usage(SHELP_REBOOT, CMD_REBOOT);
17867c478bd9Sstevel@tonic-gate 			return (optopt == '?' ? Z_OK : Z_USAGE);
17877c478bd9Sstevel@tonic-gate 		default:
17887c478bd9Sstevel@tonic-gate 			sub_usage(SHELP_REBOOT, CMD_REBOOT);
17897c478bd9Sstevel@tonic-gate 			return (Z_USAGE);
17907c478bd9Sstevel@tonic-gate 		}
17917c478bd9Sstevel@tonic-gate 	}
17927c478bd9Sstevel@tonic-gate 	if (argc > 0) {
17937c478bd9Sstevel@tonic-gate 		sub_usage(SHELP_REBOOT, CMD_REBOOT);
17947c478bd9Sstevel@tonic-gate 		return (Z_USAGE);
17957c478bd9Sstevel@tonic-gate 	}
17967c478bd9Sstevel@tonic-gate 	/*
17977c478bd9Sstevel@tonic-gate 	 * zoneadmd should be the one to decide whether or not to proceed,
17987c478bd9Sstevel@tonic-gate 	 * so even though it seems that the fourth parameter below should
17997c478bd9Sstevel@tonic-gate 	 * perhaps be B_TRUE, it really shouldn't be.
18007c478bd9Sstevel@tonic-gate 	 */
18017c478bd9Sstevel@tonic-gate 	if (sanity_check(target_zone, CMD_REBOOT, B_TRUE, B_FALSE) != Z_OK)
18027c478bd9Sstevel@tonic-gate 		return (Z_ERR);
1803b5abaf04Sgjelinek 	if (verify_details(CMD_REBOOT) != Z_OK)
1804b5abaf04Sgjelinek 		return (Z_ERR);
18057c478bd9Sstevel@tonic-gate 
18067c478bd9Sstevel@tonic-gate 	zarg.cmd = Z_REBOOT;
18077c478bd9Sstevel@tonic-gate 	return ((call_zoneadmd(target_zone, &zarg) == 0) ? Z_OK : Z_ERR);
18087c478bd9Sstevel@tonic-gate }
18097c478bd9Sstevel@tonic-gate 
18107c478bd9Sstevel@tonic-gate static int
18117c478bd9Sstevel@tonic-gate verify_rctls(zone_dochandle_t handle)
18127c478bd9Sstevel@tonic-gate {
18137c478bd9Sstevel@tonic-gate 	struct zone_rctltab rctltab;
18147c478bd9Sstevel@tonic-gate 	size_t rbs = rctlblk_size();
18157c478bd9Sstevel@tonic-gate 	rctlblk_t *rctlblk;
18167c478bd9Sstevel@tonic-gate 	int error = Z_INVAL;
18177c478bd9Sstevel@tonic-gate 
18187c478bd9Sstevel@tonic-gate 	if ((rctlblk = malloc(rbs)) == NULL) {
18197c478bd9Sstevel@tonic-gate 		zerror(gettext("failed to allocate %lu bytes: %s"), rbs,
18207c478bd9Sstevel@tonic-gate 		    strerror(errno));
18217c478bd9Sstevel@tonic-gate 		return (Z_NOMEM);
18227c478bd9Sstevel@tonic-gate 	}
18237c478bd9Sstevel@tonic-gate 
18247c478bd9Sstevel@tonic-gate 	if (zonecfg_setrctlent(handle) != Z_OK) {
18257c478bd9Sstevel@tonic-gate 		zerror(gettext("zonecfg_setrctlent failed"));
18267c478bd9Sstevel@tonic-gate 		free(rctlblk);
18277c478bd9Sstevel@tonic-gate 		return (error);
18287c478bd9Sstevel@tonic-gate 	}
18297c478bd9Sstevel@tonic-gate 
18307c478bd9Sstevel@tonic-gate 	rctltab.zone_rctl_valptr = NULL;
18317c478bd9Sstevel@tonic-gate 	while (zonecfg_getrctlent(handle, &rctltab) == Z_OK) {
18327c478bd9Sstevel@tonic-gate 		struct zone_rctlvaltab *rctlval;
18337c478bd9Sstevel@tonic-gate 		const char *name = rctltab.zone_rctl_name;
18347c478bd9Sstevel@tonic-gate 
18357c478bd9Sstevel@tonic-gate 		if (!zonecfg_is_rctl(name)) {
18367c478bd9Sstevel@tonic-gate 			zerror(gettext("WARNING: Ignoring unrecognized rctl "
18377c478bd9Sstevel@tonic-gate 			    "'%s'."),  name);
18387c478bd9Sstevel@tonic-gate 			zonecfg_free_rctl_value_list(rctltab.zone_rctl_valptr);
18397c478bd9Sstevel@tonic-gate 			rctltab.zone_rctl_valptr = NULL;
18407c478bd9Sstevel@tonic-gate 			continue;
18417c478bd9Sstevel@tonic-gate 		}
18427c478bd9Sstevel@tonic-gate 
18437c478bd9Sstevel@tonic-gate 		for (rctlval = rctltab.zone_rctl_valptr; rctlval != NULL;
18447c478bd9Sstevel@tonic-gate 		    rctlval = rctlval->zone_rctlval_next) {
18457c478bd9Sstevel@tonic-gate 			if (zonecfg_construct_rctlblk(rctlval, rctlblk)
18467c478bd9Sstevel@tonic-gate 			    != Z_OK) {
18477c478bd9Sstevel@tonic-gate 				zerror(gettext("invalid rctl value: "
18487c478bd9Sstevel@tonic-gate 				    "(priv=%s,limit=%s,action%s)"),
18497c478bd9Sstevel@tonic-gate 				    rctlval->zone_rctlval_priv,
18507c478bd9Sstevel@tonic-gate 				    rctlval->zone_rctlval_limit,
18517c478bd9Sstevel@tonic-gate 				    rctlval->zone_rctlval_action);
18527c478bd9Sstevel@tonic-gate 				goto out;
18537c478bd9Sstevel@tonic-gate 			}
18547c478bd9Sstevel@tonic-gate 			if (!zonecfg_valid_rctl(name, rctlblk)) {
18557c478bd9Sstevel@tonic-gate 				zerror(gettext("(priv=%s,limit=%s,action=%s) "
18567c478bd9Sstevel@tonic-gate 				    "is not a valid value for rctl '%s'"),
18577c478bd9Sstevel@tonic-gate 				    rctlval->zone_rctlval_priv,
18587c478bd9Sstevel@tonic-gate 				    rctlval->zone_rctlval_limit,
18597c478bd9Sstevel@tonic-gate 				    rctlval->zone_rctlval_action,
18607c478bd9Sstevel@tonic-gate 				    name);
18617c478bd9Sstevel@tonic-gate 				goto out;
18627c478bd9Sstevel@tonic-gate 			}
18637c478bd9Sstevel@tonic-gate 		}
18647c478bd9Sstevel@tonic-gate 		zonecfg_free_rctl_value_list(rctltab.zone_rctl_valptr);
18657c478bd9Sstevel@tonic-gate 	}
18667c478bd9Sstevel@tonic-gate 	rctltab.zone_rctl_valptr = NULL;
18677c478bd9Sstevel@tonic-gate 	error = Z_OK;
18687c478bd9Sstevel@tonic-gate out:
18697c478bd9Sstevel@tonic-gate 	zonecfg_free_rctl_value_list(rctltab.zone_rctl_valptr);
18707c478bd9Sstevel@tonic-gate 	(void) zonecfg_endrctlent(handle);
18717c478bd9Sstevel@tonic-gate 	free(rctlblk);
18727c478bd9Sstevel@tonic-gate 	return (error);
18737c478bd9Sstevel@tonic-gate }
18747c478bd9Sstevel@tonic-gate 
18757c478bd9Sstevel@tonic-gate static int
18767c478bd9Sstevel@tonic-gate verify_pool(zone_dochandle_t handle)
18777c478bd9Sstevel@tonic-gate {
18787c478bd9Sstevel@tonic-gate 	char poolname[MAXPATHLEN];
18797c478bd9Sstevel@tonic-gate 	pool_conf_t *poolconf;
18807c478bd9Sstevel@tonic-gate 	pool_t *pool;
18817c478bd9Sstevel@tonic-gate 	int status;
18827c478bd9Sstevel@tonic-gate 	int error;
18837c478bd9Sstevel@tonic-gate 
18847c478bd9Sstevel@tonic-gate 	/*
18857c478bd9Sstevel@tonic-gate 	 * This ends up being very similar to the check done in zoneadmd.
18867c478bd9Sstevel@tonic-gate 	 */
18877c478bd9Sstevel@tonic-gate 	error = zonecfg_get_pool(handle, poolname, sizeof (poolname));
18887c478bd9Sstevel@tonic-gate 	if (error == Z_NO_ENTRY || (error == Z_OK && strlen(poolname) == 0)) {
18897c478bd9Sstevel@tonic-gate 		/*
18907c478bd9Sstevel@tonic-gate 		 * No pool specified.
18917c478bd9Sstevel@tonic-gate 		 */
18927c478bd9Sstevel@tonic-gate 		return (0);
18937c478bd9Sstevel@tonic-gate 	}
18947c478bd9Sstevel@tonic-gate 	if (error != Z_OK) {
18957c478bd9Sstevel@tonic-gate 		zperror(gettext("Unable to retrieve pool name from "
18967c478bd9Sstevel@tonic-gate 		    "configuration"), B_TRUE);
18977c478bd9Sstevel@tonic-gate 		return (error);
18987c478bd9Sstevel@tonic-gate 	}
18997c478bd9Sstevel@tonic-gate 	/*
19007c478bd9Sstevel@tonic-gate 	 * Don't do anything if pools aren't enabled.
19017c478bd9Sstevel@tonic-gate 	 */
19027c478bd9Sstevel@tonic-gate 	if (pool_get_status(&status) != PO_SUCCESS || status != POOL_ENABLED) {
19037c478bd9Sstevel@tonic-gate 		zerror(gettext("WARNING: pools facility not active; "
19047c478bd9Sstevel@tonic-gate 		    "zone will not be bound to pool '%s'."), poolname);
19057c478bd9Sstevel@tonic-gate 		return (Z_OK);
19067c478bd9Sstevel@tonic-gate 	}
19077c478bd9Sstevel@tonic-gate 	/*
19087c478bd9Sstevel@tonic-gate 	 * Try to provide a sane error message if the requested pool doesn't
19097c478bd9Sstevel@tonic-gate 	 * exist.  It isn't clear that pools-related failures should
19107c478bd9Sstevel@tonic-gate 	 * necessarily translate to a failure to verify the zone configuration,
19117c478bd9Sstevel@tonic-gate 	 * hence they are not considered errors.
19127c478bd9Sstevel@tonic-gate 	 */
19137c478bd9Sstevel@tonic-gate 	if ((poolconf = pool_conf_alloc()) == NULL) {
19147c478bd9Sstevel@tonic-gate 		zerror(gettext("WARNING: pool_conf_alloc failed; "
19157c478bd9Sstevel@tonic-gate 		    "using default pool"));
19167c478bd9Sstevel@tonic-gate 		return (Z_OK);
19177c478bd9Sstevel@tonic-gate 	}
19187c478bd9Sstevel@tonic-gate 	if (pool_conf_open(poolconf, pool_dynamic_location(), PO_RDONLY) !=
19197c478bd9Sstevel@tonic-gate 	    PO_SUCCESS) {
19207c478bd9Sstevel@tonic-gate 		zerror(gettext("WARNING: pool_conf_open failed; "
19217c478bd9Sstevel@tonic-gate 		    "using default pool"));
19227c478bd9Sstevel@tonic-gate 		pool_conf_free(poolconf);
19237c478bd9Sstevel@tonic-gate 		return (Z_OK);
19247c478bd9Sstevel@tonic-gate 	}
19257c478bd9Sstevel@tonic-gate 	pool = pool_get_pool(poolconf, poolname);
19267c478bd9Sstevel@tonic-gate 	(void) pool_conf_close(poolconf);
19277c478bd9Sstevel@tonic-gate 	pool_conf_free(poolconf);
19287c478bd9Sstevel@tonic-gate 	if (pool == NULL) {
19297c478bd9Sstevel@tonic-gate 		zerror(gettext("WARNING: pool '%s' not found. "
19307c478bd9Sstevel@tonic-gate 		    "using default pool"), poolname);
19317c478bd9Sstevel@tonic-gate 	}
19327c478bd9Sstevel@tonic-gate 
19337c478bd9Sstevel@tonic-gate 	return (Z_OK);
19347c478bd9Sstevel@tonic-gate }
19357c478bd9Sstevel@tonic-gate 
19367c478bd9Sstevel@tonic-gate static int
1937b5abaf04Sgjelinek verify_ipd(zone_dochandle_t handle)
1938b5abaf04Sgjelinek {
1939b5abaf04Sgjelinek 	int return_code = Z_OK;
1940b5abaf04Sgjelinek 	struct zone_fstab fstab;
1941b5abaf04Sgjelinek 	struct stat st;
1942b5abaf04Sgjelinek 	char specdir[MAXPATHLEN];
1943b5abaf04Sgjelinek 
1944b5abaf04Sgjelinek 	if (zonecfg_setipdent(handle) != Z_OK) {
19455ee84fbdSgjelinek 		/*
19465ee84fbdSgjelinek 		 * TRANSLATION_NOTE
19475ee84fbdSgjelinek 		 * inherit-pkg-dirs is a literal that should not be translated.
19485ee84fbdSgjelinek 		 */
19495ee84fbdSgjelinek 		(void) fprintf(stderr, gettext("could not verify "
1950b5abaf04Sgjelinek 		    "inherit-pkg-dirs: unable to enumerate mounts\n"));
1951b5abaf04Sgjelinek 		return (Z_ERR);
1952b5abaf04Sgjelinek 	}
1953b5abaf04Sgjelinek 	while (zonecfg_getipdent(handle, &fstab) == Z_OK) {
1954b5abaf04Sgjelinek 		/*
1955b5abaf04Sgjelinek 		 * Verify fs_dir exists.
1956b5abaf04Sgjelinek 		 */
1957b5abaf04Sgjelinek 		(void) snprintf(specdir, sizeof (specdir), "%s%s",
1958b5abaf04Sgjelinek 		    zonecfg_get_root(), fstab.zone_fs_dir);
1959b5abaf04Sgjelinek 		if (stat(specdir, &st) != 0) {
19605ee84fbdSgjelinek 			/*
19615ee84fbdSgjelinek 			 * TRANSLATION_NOTE
19625ee84fbdSgjelinek 			 * inherit-pkg-dir is a literal that should not be
19635ee84fbdSgjelinek 			 * translated.
19645ee84fbdSgjelinek 			 */
19655ee84fbdSgjelinek 			(void) fprintf(stderr, gettext("could not verify "
1966b5abaf04Sgjelinek 			    "inherit-pkg-dir %s: %s\n"),
1967b5abaf04Sgjelinek 			    fstab.zone_fs_dir, strerror(errno));
1968b5abaf04Sgjelinek 			return_code = Z_ERR;
1969b5abaf04Sgjelinek 		}
1970b5abaf04Sgjelinek 		if (strcmp(st.st_fstype, MNTTYPE_NFS) == 0) {
19715ee84fbdSgjelinek 			/*
19725ee84fbdSgjelinek 			 * TRANSLATION_NOTE
19735ee84fbdSgjelinek 			 * inherit-pkg-dir and NFS are literals that should
19745ee84fbdSgjelinek 			 * not be translated.
19755ee84fbdSgjelinek 			 */
1976b5abaf04Sgjelinek 			(void) fprintf(stderr, gettext("cannot verify "
19770b5de56dSgjelinek 			    "inherit-pkg-dir %s: NFS mounted file system.\n"
19780b5de56dSgjelinek 			    "\tA local file system must be used.\n"),
1979b5abaf04Sgjelinek 			    fstab.zone_fs_dir);
1980b5abaf04Sgjelinek 			return_code = Z_ERR;
1981b5abaf04Sgjelinek 		}
1982b5abaf04Sgjelinek 	}
1983b5abaf04Sgjelinek 	(void) zonecfg_endipdent(handle);
1984b5abaf04Sgjelinek 
1985b5abaf04Sgjelinek 	return (return_code);
1986b5abaf04Sgjelinek }
1987b5abaf04Sgjelinek 
198820c8013fSlling /*
198920c8013fSlling  * Verify that the special device/file system exists and is valid.
199020c8013fSlling  */
199120c8013fSlling static int
199220c8013fSlling verify_fs_special(struct zone_fstab *fstab)
199320c8013fSlling {
199420c8013fSlling 	struct stat st;
199520c8013fSlling 
199620c8013fSlling 	if (strcmp(fstab->zone_fs_type, MNTTYPE_ZFS) == 0)
199720c8013fSlling 		return (verify_fs_zfs(fstab));
199820c8013fSlling 
199920c8013fSlling 	if (stat(fstab->zone_fs_special, &st) != 0) {
20005c358068Slling 		(void) fprintf(stderr, gettext("could not verify fs "
200120c8013fSlling 		    "%s: could not access %s: %s\n"), fstab->zone_fs_dir,
200220c8013fSlling 		    fstab->zone_fs_special, strerror(errno));
200320c8013fSlling 		return (Z_ERR);
200420c8013fSlling 	}
200520c8013fSlling 
200620c8013fSlling 	if (strcmp(st.st_fstype, MNTTYPE_NFS) == 0) {
200720c8013fSlling 		/*
200820c8013fSlling 		 * TRANSLATION_NOTE
200920c8013fSlling 		 * fs and NFS are literals that should
201020c8013fSlling 		 * not be translated.
201120c8013fSlling 		 */
201220c8013fSlling 		(void) fprintf(stderr, gettext("cannot verify "
20130b5de56dSgjelinek 		    "fs %s: NFS mounted file system.\n"
20140b5de56dSgjelinek 		    "\tA local file system must be used.\n"),
201520c8013fSlling 		    fstab->zone_fs_special);
201620c8013fSlling 		return (Z_ERR);
201720c8013fSlling 	}
201820c8013fSlling 
201920c8013fSlling 	return (Z_OK);
202020c8013fSlling }
202120c8013fSlling 
2022b5abaf04Sgjelinek static int
20237c478bd9Sstevel@tonic-gate verify_filesystems(zone_dochandle_t handle)
20247c478bd9Sstevel@tonic-gate {
20257c478bd9Sstevel@tonic-gate 	int return_code = Z_OK;
20267c478bd9Sstevel@tonic-gate 	struct zone_fstab fstab;
20277c478bd9Sstevel@tonic-gate 	char cmdbuf[MAXPATHLEN];
20287c478bd9Sstevel@tonic-gate 	struct stat st;
20297c478bd9Sstevel@tonic-gate 
20307c478bd9Sstevel@tonic-gate 	/*
20317c478bd9Sstevel@tonic-gate 	 * No need to verify inherit-pkg-dir fs types, as their type is
20327c478bd9Sstevel@tonic-gate 	 * implicitly lofs, which is known.  Therefore, the types are only
20337c478bd9Sstevel@tonic-gate 	 * verified for regular file systems below.
20347c478bd9Sstevel@tonic-gate 	 *
20357c478bd9Sstevel@tonic-gate 	 * Since the actual mount point is not known until the dependent mounts
20367c478bd9Sstevel@tonic-gate 	 * are performed, we don't attempt any path validation here: that will
20377c478bd9Sstevel@tonic-gate 	 * happen later when zoneadmd actually does the mounts.
20387c478bd9Sstevel@tonic-gate 	 */
20397c478bd9Sstevel@tonic-gate 	if (zonecfg_setfsent(handle) != Z_OK) {
20400b5de56dSgjelinek 		(void) fprintf(stderr, gettext("could not verify file systems: "
20417c478bd9Sstevel@tonic-gate 		    "unable to enumerate mounts\n"));
20427c478bd9Sstevel@tonic-gate 		return (Z_ERR);
20437c478bd9Sstevel@tonic-gate 	}
20447c478bd9Sstevel@tonic-gate 	while (zonecfg_getfsent(handle, &fstab) == Z_OK) {
20457c478bd9Sstevel@tonic-gate 		if (!zonecfg_valid_fs_type(fstab.zone_fs_type)) {
20467c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("cannot verify fs %s: "
20477c478bd9Sstevel@tonic-gate 			    "type %s is not allowed.\n"), fstab.zone_fs_dir,
20487c478bd9Sstevel@tonic-gate 			    fstab.zone_fs_type);
20497c478bd9Sstevel@tonic-gate 			return_code = Z_ERR;
20507c478bd9Sstevel@tonic-gate 			goto next_fs;
20517c478bd9Sstevel@tonic-gate 		}
20527c478bd9Sstevel@tonic-gate 		/*
20537c478bd9Sstevel@tonic-gate 		 * Verify /usr/lib/fs/<fstype>/mount exists.
20547c478bd9Sstevel@tonic-gate 		 */
20557c478bd9Sstevel@tonic-gate 		if (snprintf(cmdbuf, sizeof (cmdbuf), "/usr/lib/fs/%s/mount",
20567c478bd9Sstevel@tonic-gate 		    fstab.zone_fs_type) > sizeof (cmdbuf)) {
20577c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("cannot verify fs %s: "
20587c478bd9Sstevel@tonic-gate 			    "type %s is too long.\n"), fstab.zone_fs_dir,
20597c478bd9Sstevel@tonic-gate 			    fstab.zone_fs_type);
20607c478bd9Sstevel@tonic-gate 			return_code = Z_ERR;
20617c478bd9Sstevel@tonic-gate 			goto next_fs;
20627c478bd9Sstevel@tonic-gate 		}
20637c478bd9Sstevel@tonic-gate 		if (stat(cmdbuf, &st) != 0) {
20645ee84fbdSgjelinek 			(void) fprintf(stderr, gettext("could not verify fs "
20655ee84fbdSgjelinek 			    "%s: could not access %s: %s\n"), fstab.zone_fs_dir,
20667c478bd9Sstevel@tonic-gate 			    cmdbuf, strerror(errno));
20677c478bd9Sstevel@tonic-gate 			return_code = Z_ERR;
20687c478bd9Sstevel@tonic-gate 			goto next_fs;
20697c478bd9Sstevel@tonic-gate 		}
20707c478bd9Sstevel@tonic-gate 		if (!S_ISREG(st.st_mode)) {
20715ee84fbdSgjelinek 			(void) fprintf(stderr, gettext("could not verify fs "
20725ee84fbdSgjelinek 			    "%s: %s is not a regular file\n"),
20735ee84fbdSgjelinek 			    fstab.zone_fs_dir, cmdbuf);
20747c478bd9Sstevel@tonic-gate 			return_code = Z_ERR;
20757c478bd9Sstevel@tonic-gate 			goto next_fs;
20767c478bd9Sstevel@tonic-gate 		}
20777c478bd9Sstevel@tonic-gate 		/*
20787c478bd9Sstevel@tonic-gate 		 * Verify /usr/lib/fs/<fstype>/fsck exists iff zone_fs_raw is
20797c478bd9Sstevel@tonic-gate 		 * set.
20807c478bd9Sstevel@tonic-gate 		 */
20817c478bd9Sstevel@tonic-gate 		if (snprintf(cmdbuf, sizeof (cmdbuf), "/usr/lib/fs/%s/fsck",
20827c478bd9Sstevel@tonic-gate 		    fstab.zone_fs_type) > sizeof (cmdbuf)) {
20837c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("cannot verify fs %s: "
20847c478bd9Sstevel@tonic-gate 			    "type %s is too long.\n"), fstab.zone_fs_dir,
20857c478bd9Sstevel@tonic-gate 			    fstab.zone_fs_type);
20867c478bd9Sstevel@tonic-gate 			return_code = Z_ERR;
20877c478bd9Sstevel@tonic-gate 			goto next_fs;
20887c478bd9Sstevel@tonic-gate 		}
20897c478bd9Sstevel@tonic-gate 		if (fstab.zone_fs_raw[0] == '\0' && stat(cmdbuf, &st) == 0) {
20905ee84fbdSgjelinek 			(void) fprintf(stderr, gettext("could not verify fs "
20915ee84fbdSgjelinek 			    "%s: must specify 'raw' device for %s "
20920b5de56dSgjelinek 			    "file systems\n"),
20937c478bd9Sstevel@tonic-gate 			    fstab.zone_fs_dir, fstab.zone_fs_type);
20947c478bd9Sstevel@tonic-gate 			return_code = Z_ERR;
20957c478bd9Sstevel@tonic-gate 			goto next_fs;
20967c478bd9Sstevel@tonic-gate 		}
20977c478bd9Sstevel@tonic-gate 		if (fstab.zone_fs_raw[0] != '\0' &&
20987c478bd9Sstevel@tonic-gate 		    (stat(cmdbuf, &st) != 0 || !S_ISREG(st.st_mode))) {
20997c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("cannot verify fs %s: "
21007c478bd9Sstevel@tonic-gate 			    "'raw' device specified but "
21017c478bd9Sstevel@tonic-gate 			    "no fsck executable exists for %s\n"),
21027c478bd9Sstevel@tonic-gate 			    fstab.zone_fs_dir, fstab.zone_fs_type);
21037c478bd9Sstevel@tonic-gate 			return_code = Z_ERR;
21047c478bd9Sstevel@tonic-gate 			goto next_fs;
21057c478bd9Sstevel@tonic-gate 		}
210620c8013fSlling 
210720c8013fSlling 		/* Verify fs_special. */
210820c8013fSlling 		if ((return_code = verify_fs_special(&fstab)) != Z_OK)
2109b5abaf04Sgjelinek 			goto next_fs;
211020c8013fSlling 
211120c8013fSlling 		/* Verify fs_raw. */
2112b5abaf04Sgjelinek 		if (fstab.zone_fs_raw[0] != '\0' &&
2113b5abaf04Sgjelinek 		    stat(fstab.zone_fs_raw, &st) != 0) {
21145ee84fbdSgjelinek 			/*
21155ee84fbdSgjelinek 			 * TRANSLATION_NOTE
21165ee84fbdSgjelinek 			 * fs is a literal that should not be translated.
21175ee84fbdSgjelinek 			 */
21185ee84fbdSgjelinek 			(void) fprintf(stderr, gettext("could not verify fs "
21195ee84fbdSgjelinek 			    "%s: could not access %s: %s\n"), fstab.zone_fs_dir,
2120b5abaf04Sgjelinek 			    fstab.zone_fs_raw, strerror(errno));
2121b5abaf04Sgjelinek 			return_code = Z_ERR;
2122b5abaf04Sgjelinek 			goto next_fs;
2123b5abaf04Sgjelinek 		}
21247c478bd9Sstevel@tonic-gate next_fs:
21257c478bd9Sstevel@tonic-gate 		zonecfg_free_fs_option_list(fstab.zone_fs_options);
21267c478bd9Sstevel@tonic-gate 	}
21277c478bd9Sstevel@tonic-gate 	(void) zonecfg_endfsent(handle);
21287c478bd9Sstevel@tonic-gate 
21297c478bd9Sstevel@tonic-gate 	return (return_code);
21307c478bd9Sstevel@tonic-gate }
21317c478bd9Sstevel@tonic-gate 
21327c478bd9Sstevel@tonic-gate static int
2133ffbafc53Scomay verify_limitpriv(zone_dochandle_t handle)
2134ffbafc53Scomay {
2135ffbafc53Scomay 	char *privname = NULL;
2136ffbafc53Scomay 	int err;
2137ffbafc53Scomay 	priv_set_t *privs;
2138ffbafc53Scomay 
2139ffbafc53Scomay 	if ((privs = priv_allocset()) == NULL) {
2140ffbafc53Scomay 		zperror(gettext("failed to allocate privilege set"), B_FALSE);
2141ffbafc53Scomay 		return (Z_NOMEM);
2142ffbafc53Scomay 	}
2143ffbafc53Scomay 	err = zonecfg_get_privset(handle, privs, &privname);
2144ffbafc53Scomay 	switch (err) {
2145ffbafc53Scomay 	case Z_OK:
2146ffbafc53Scomay 		break;
2147ffbafc53Scomay 	case Z_PRIV_PROHIBITED:
2148ffbafc53Scomay 		(void) fprintf(stderr, gettext("privilege \"%s\" is not "
2149ffbafc53Scomay 		    "permitted within the zone's privilege set\n"), privname);
2150ffbafc53Scomay 		break;
2151ffbafc53Scomay 	case Z_PRIV_REQUIRED:
2152ffbafc53Scomay 		(void) fprintf(stderr, gettext("required privilege \"%s\" is "
2153ffbafc53Scomay 		    "missing from the zone's privilege set\n"), privname);
2154ffbafc53Scomay 		break;
2155ffbafc53Scomay 	case Z_PRIV_UNKNOWN:
2156ffbafc53Scomay 		(void) fprintf(stderr, gettext("unknown privilege \"%s\" "
2157ffbafc53Scomay 		    "specified in the zone's privilege set\n"), privname);
2158ffbafc53Scomay 		break;
2159ffbafc53Scomay 	default:
2160ffbafc53Scomay 		zperror(
2161ffbafc53Scomay 		    gettext("failed to determine the zone's privilege set"),
2162ffbafc53Scomay 		    B_TRUE);
2163ffbafc53Scomay 		break;
2164ffbafc53Scomay 	}
2165ffbafc53Scomay 	free(privname);
2166ffbafc53Scomay 	priv_freeset(privs);
2167ffbafc53Scomay 	return (err);
2168ffbafc53Scomay }
2169ffbafc53Scomay 
21701390a385Sgjelinek static void
21711390a385Sgjelinek free_local_netifs(int if_cnt, struct net_if **if_list)
21721390a385Sgjelinek {
21731390a385Sgjelinek 	int		i;
21741390a385Sgjelinek 
21751390a385Sgjelinek 	for (i = 0; i < if_cnt; i++) {
21761390a385Sgjelinek 		free(if_list[i]->name);
21771390a385Sgjelinek 		free(if_list[i]);
21781390a385Sgjelinek 	}
21791390a385Sgjelinek 	free(if_list);
21801390a385Sgjelinek }
21811390a385Sgjelinek 
21821390a385Sgjelinek /*
21831390a385Sgjelinek  * Get a list of the network interfaces, along with their address families,
21841390a385Sgjelinek  * that are plumbed in the global zone.  See if_tcp(7p) for a description
21851390a385Sgjelinek  * of the ioctls used here.
21861390a385Sgjelinek  */
21871390a385Sgjelinek static int
21881390a385Sgjelinek get_local_netifs(int *if_cnt, struct net_if ***if_list)
21891390a385Sgjelinek {
21901390a385Sgjelinek 	int		s;
21911390a385Sgjelinek 	int		i;
21921390a385Sgjelinek 	int		res = Z_OK;
21931390a385Sgjelinek 	int		space_needed;
21941390a385Sgjelinek 	int		cnt = 0;
21951390a385Sgjelinek 	struct		lifnum if_num;
21961390a385Sgjelinek 	struct		lifconf if_conf;
21971390a385Sgjelinek 	struct		lifreq *if_reqp;
21981390a385Sgjelinek 	char		*if_buf;
21991390a385Sgjelinek 	struct net_if	**local_ifs = NULL;
22001390a385Sgjelinek 
22011390a385Sgjelinek 	*if_cnt = 0;
22021390a385Sgjelinek 	*if_list = NULL;
22031390a385Sgjelinek 
22041390a385Sgjelinek 	if ((s = socket(SOCKET_AF(AF_INET), SOCK_DGRAM, 0)) < 0)
22051390a385Sgjelinek 		return (Z_ERR);
22061390a385Sgjelinek 
22071390a385Sgjelinek 	/*
22081390a385Sgjelinek 	 * Come back here in the unlikely event that the number of interfaces
22091390a385Sgjelinek 	 * increases between the time we get the count and the time we do the
22101390a385Sgjelinek 	 * SIOCGLIFCONF ioctl.
22111390a385Sgjelinek 	 */
22121390a385Sgjelinek retry:
22131390a385Sgjelinek 	/* Get the number of interfaces. */
22141390a385Sgjelinek 	if_num.lifn_family = AF_UNSPEC;
22151390a385Sgjelinek 	if_num.lifn_flags = LIFC_NOXMIT;
22161390a385Sgjelinek 	if (ioctl(s, SIOCGLIFNUM, &if_num) < 0) {
22171390a385Sgjelinek 		(void) close(s);
22181390a385Sgjelinek 		return (Z_ERR);
22191390a385Sgjelinek 	}
22201390a385Sgjelinek 
22211390a385Sgjelinek 	/* Get the interface configuration list. */
22221390a385Sgjelinek 	space_needed = if_num.lifn_count * sizeof (struct lifreq);
22231390a385Sgjelinek 	if ((if_buf = malloc(space_needed)) == NULL) {
22241390a385Sgjelinek 		(void) close(s);
22251390a385Sgjelinek 		return (Z_ERR);
22261390a385Sgjelinek 	}
22271390a385Sgjelinek 	if_conf.lifc_family = AF_UNSPEC;
22281390a385Sgjelinek 	if_conf.lifc_flags = LIFC_NOXMIT;
22291390a385Sgjelinek 	if_conf.lifc_len = space_needed;
22301390a385Sgjelinek 	if_conf.lifc_buf = if_buf;
22311390a385Sgjelinek 	if (ioctl(s, SIOCGLIFCONF, &if_conf) < 0) {
22321390a385Sgjelinek 		free(if_buf);
22331390a385Sgjelinek 		/*
22341390a385Sgjelinek 		 * SIOCGLIFCONF returns EINVAL if the buffer we passed in is
22351390a385Sgjelinek 		 * too small.  In this case go back and get the new if cnt.
22361390a385Sgjelinek 		 */
22371390a385Sgjelinek 		if (errno == EINVAL)
22381390a385Sgjelinek 			goto retry;
22391390a385Sgjelinek 
22401390a385Sgjelinek 		(void) close(s);
22411390a385Sgjelinek 		return (Z_ERR);
22421390a385Sgjelinek 	}
22431390a385Sgjelinek 	(void) close(s);
22441390a385Sgjelinek 
22451390a385Sgjelinek 	/* Get the name and address family for each interface. */
22461390a385Sgjelinek 	if_reqp = if_conf.lifc_req;
22471390a385Sgjelinek 	for (i = 0; i < (if_conf.lifc_len / sizeof (struct lifreq)); i++) {
22481390a385Sgjelinek 		struct net_if	**p;
22491390a385Sgjelinek 		struct lifreq	req;
22501390a385Sgjelinek 
22511390a385Sgjelinek 		if (strcmp(LOOPBACK_IF, if_reqp->lifr_name) == 0) {
22521390a385Sgjelinek 			if_reqp++;
22531390a385Sgjelinek 			continue;
22541390a385Sgjelinek 		}
22551390a385Sgjelinek 
22561390a385Sgjelinek 		if ((s = socket(SOCKET_AF(if_reqp->lifr_addr.ss_family),
22571390a385Sgjelinek 		    SOCK_DGRAM, 0)) == -1) {
22581390a385Sgjelinek 			res = Z_ERR;
22591390a385Sgjelinek 			break;
22601390a385Sgjelinek 		}
22611390a385Sgjelinek 
22621390a385Sgjelinek 		(void) strncpy(req.lifr_name, if_reqp->lifr_name,
22631390a385Sgjelinek 		    sizeof (req.lifr_name));
22641390a385Sgjelinek 		if (ioctl(s, SIOCGLIFADDR, &req) < 0) {
22651390a385Sgjelinek 			(void) close(s);
22661390a385Sgjelinek 			if_reqp++;
22671390a385Sgjelinek 			continue;
22681390a385Sgjelinek 		}
22691390a385Sgjelinek 
22701390a385Sgjelinek 		if ((p = (struct net_if **)realloc(local_ifs,
22711390a385Sgjelinek 		    sizeof (struct net_if *) * (cnt + 1))) == NULL) {
22721390a385Sgjelinek 			res = Z_ERR;
22731390a385Sgjelinek 			break;
22741390a385Sgjelinek 		}
22751390a385Sgjelinek 		local_ifs = p;
22761390a385Sgjelinek 
22771390a385Sgjelinek 		if ((local_ifs[cnt] = malloc(sizeof (struct net_if))) == NULL) {
22781390a385Sgjelinek 			res = Z_ERR;
22791390a385Sgjelinek 			break;
22801390a385Sgjelinek 		}
22811390a385Sgjelinek 
22821390a385Sgjelinek 		if ((local_ifs[cnt]->name = strdup(if_reqp->lifr_name))
22831390a385Sgjelinek 		    == NULL) {
22841390a385Sgjelinek 			free(local_ifs[cnt]);
22851390a385Sgjelinek 			res = Z_ERR;
22861390a385Sgjelinek 			break;
22871390a385Sgjelinek 		}
22881390a385Sgjelinek 		local_ifs[cnt]->af = req.lifr_addr.ss_family;
22891390a385Sgjelinek 		cnt++;
22901390a385Sgjelinek 
22911390a385Sgjelinek 		(void) close(s);
22921390a385Sgjelinek 		if_reqp++;
22931390a385Sgjelinek 	}
22941390a385Sgjelinek 
22951390a385Sgjelinek 	free(if_buf);
22961390a385Sgjelinek 
22971390a385Sgjelinek 	if (res != Z_OK) {
22981390a385Sgjelinek 		free_local_netifs(cnt, local_ifs);
22991390a385Sgjelinek 	} else {
23001390a385Sgjelinek 		*if_cnt = cnt;
23011390a385Sgjelinek 		*if_list = local_ifs;
23021390a385Sgjelinek 	}
23031390a385Sgjelinek 
23041390a385Sgjelinek 	return (res);
23051390a385Sgjelinek }
23061390a385Sgjelinek 
23071390a385Sgjelinek static char *
23081390a385Sgjelinek af2str(int af)
23091390a385Sgjelinek {
23101390a385Sgjelinek 	switch (af) {
23111390a385Sgjelinek 	case AF_INET:
23121390a385Sgjelinek 		return ("IPv4");
23131390a385Sgjelinek 	case AF_INET6:
23141390a385Sgjelinek 		return ("IPv6");
23151390a385Sgjelinek 	default:
23161390a385Sgjelinek 		return ("Unknown");
23171390a385Sgjelinek 	}
23181390a385Sgjelinek }
23191390a385Sgjelinek 
23201390a385Sgjelinek /*
23211390a385Sgjelinek  * Cross check the network interface name and address family with the
23221390a385Sgjelinek  * interfaces that are set up in the global zone so that we can print the
23231390a385Sgjelinek  * appropriate error message.
23241390a385Sgjelinek  */
23251390a385Sgjelinek static void
23261390a385Sgjelinek print_net_err(char *phys, char *addr, int af, char *msg)
23271390a385Sgjelinek {
23281390a385Sgjelinek 	int		i;
23291390a385Sgjelinek 	int		local_if_cnt = 0;
23301390a385Sgjelinek 	struct net_if	**local_ifs = NULL;
23311390a385Sgjelinek 	boolean_t	found_if = B_FALSE;
23321390a385Sgjelinek 	boolean_t	found_af = B_FALSE;
23331390a385Sgjelinek 
23341390a385Sgjelinek 	if (get_local_netifs(&local_if_cnt, &local_ifs) != Z_OK) {
23351390a385Sgjelinek 		(void) fprintf(stderr,
23361390a385Sgjelinek 		    gettext("could not verify %s %s=%s %s=%s\n\t%s\n"),
23371390a385Sgjelinek 		    "net", "address", addr, "physical", phys, msg);
23381390a385Sgjelinek 		return;
23391390a385Sgjelinek 	}
23401390a385Sgjelinek 
23411390a385Sgjelinek 	for (i = 0; i < local_if_cnt; i++) {
23421390a385Sgjelinek 		if (strcmp(phys, local_ifs[i]->name) == 0) {
23431390a385Sgjelinek 			found_if = B_TRUE;
23441390a385Sgjelinek 			if (af == local_ifs[i]->af) {
23451390a385Sgjelinek 				found_af = B_TRUE;
23461390a385Sgjelinek 				break;
23471390a385Sgjelinek 			}
23481390a385Sgjelinek 		}
23491390a385Sgjelinek 	}
23501390a385Sgjelinek 
23511390a385Sgjelinek 	free_local_netifs(local_if_cnt, local_ifs);
23521390a385Sgjelinek 
23531390a385Sgjelinek 	if (!found_if) {
23541390a385Sgjelinek 		(void) fprintf(stderr,
23551390a385Sgjelinek 		    gettext("could not verify %s %s=%s\n\t"
23561390a385Sgjelinek 		    "network interface %s is not plumbed in the global zone\n"),
23571390a385Sgjelinek 		    "net", "physical", phys, phys);
23581390a385Sgjelinek 		return;
23591390a385Sgjelinek 	}
23601390a385Sgjelinek 
23611390a385Sgjelinek 	/*
23621390a385Sgjelinek 	 * Print this error if we were unable to find the address family
23631390a385Sgjelinek 	 * for this interface.  If the af variable is not initialized to
23641390a385Sgjelinek 	 * to something meaningful by the caller (not AF_UNSPEC) then we
23651390a385Sgjelinek 	 * also skip this message since it wouldn't be informative.
23661390a385Sgjelinek 	 */
23671390a385Sgjelinek 	if (!found_af && af != AF_UNSPEC) {
23681390a385Sgjelinek 		(void) fprintf(stderr,
23691390a385Sgjelinek 		    gettext("could not verify %s %s=%s %s=%s\n\tthe %s address "
23701390a385Sgjelinek 		    "family is not configured on this interface in the\n\t"
23711390a385Sgjelinek 		    "global zone\n"),
23721390a385Sgjelinek 		    "net", "address", addr, "physical", phys, af2str(af));
23731390a385Sgjelinek 		return;
23741390a385Sgjelinek 	}
23751390a385Sgjelinek 
23761390a385Sgjelinek 	(void) fprintf(stderr,
23771390a385Sgjelinek 	    gettext("could not verify %s %s=%s %s=%s\n\t%s\n"),
23781390a385Sgjelinek 	    "net", "address", addr, "physical", phys, msg);
23791390a385Sgjelinek }
23801390a385Sgjelinek 
2381ffbafc53Scomay static int
23828cd327d5Sgjelinek verify_handle(int cmd_num, zone_dochandle_t handle)
23837c478bd9Sstevel@tonic-gate {
23847c478bd9Sstevel@tonic-gate 	struct zone_nwiftab nwiftab;
23857c478bd9Sstevel@tonic-gate 	int return_code = Z_OK;
23867c478bd9Sstevel@tonic-gate 	int err;
2387108322fbScarlsonj 	boolean_t in_alt_root;
23887c478bd9Sstevel@tonic-gate 
2389108322fbScarlsonj 	in_alt_root = zonecfg_in_alt_root();
2390108322fbScarlsonj 	if (in_alt_root)
2391108322fbScarlsonj 		goto no_net;
2392108322fbScarlsonj 
23937c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_setnwifent(handle)) != Z_OK) {
23947c478bd9Sstevel@tonic-gate 		errno = err;
23957c478bd9Sstevel@tonic-gate 		zperror(cmd_to_str(cmd_num), B_TRUE);
23967c478bd9Sstevel@tonic-gate 		zonecfg_fini_handle(handle);
23977c478bd9Sstevel@tonic-gate 		return (Z_ERR);
23987c478bd9Sstevel@tonic-gate 	}
23997c478bd9Sstevel@tonic-gate 	while (zonecfg_getnwifent(handle, &nwiftab) == Z_OK) {
24007c478bd9Sstevel@tonic-gate 		struct lifreq lifr;
24011390a385Sgjelinek 		sa_family_t af = AF_UNSPEC;
24027c478bd9Sstevel@tonic-gate 		int so, res;
24037c478bd9Sstevel@tonic-gate 
24047c478bd9Sstevel@tonic-gate 		/* skip any loopback interfaces */
24057c478bd9Sstevel@tonic-gate 		if (strcmp(nwiftab.zone_nwif_physical, "lo0") == 0)
24067c478bd9Sstevel@tonic-gate 			continue;
24077c478bd9Sstevel@tonic-gate 		if ((res = zonecfg_valid_net_address(nwiftab.zone_nwif_address,
24087c478bd9Sstevel@tonic-gate 		    &lifr)) != Z_OK) {
24091390a385Sgjelinek 			print_net_err(nwiftab.zone_nwif_physical,
24101390a385Sgjelinek 			    nwiftab.zone_nwif_address, af,
24111390a385Sgjelinek 			    zonecfg_strerror(res));
24127c478bd9Sstevel@tonic-gate 			return_code = Z_ERR;
24137c478bd9Sstevel@tonic-gate 			continue;
24147c478bd9Sstevel@tonic-gate 		}
24157c478bd9Sstevel@tonic-gate 		af = lifr.lifr_addr.ss_family;
24167c478bd9Sstevel@tonic-gate 		(void) memset(&lifr, 0, sizeof (lifr));
24177c478bd9Sstevel@tonic-gate 		(void) strlcpy(lifr.lifr_name, nwiftab.zone_nwif_physical,
24187c478bd9Sstevel@tonic-gate 		    sizeof (lifr.lifr_name));
24197c478bd9Sstevel@tonic-gate 		lifr.lifr_addr.ss_family = af;
24207c478bd9Sstevel@tonic-gate 		if ((so = socket(af, SOCK_DGRAM, 0)) < 0) {
24217c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("could not verify %s "
24227c478bd9Sstevel@tonic-gate 			    "%s=%s %s=%s: could not get socket: %s\n"), "net",
24237c478bd9Sstevel@tonic-gate 			    "address", nwiftab.zone_nwif_address, "physical",
24247c478bd9Sstevel@tonic-gate 			    nwiftab.zone_nwif_physical, strerror(errno));
24257c478bd9Sstevel@tonic-gate 			return_code = Z_ERR;
24267c478bd9Sstevel@tonic-gate 			continue;
24277c478bd9Sstevel@tonic-gate 		}
24281390a385Sgjelinek 		if (ioctl(so, SIOCGLIFFLAGS, &lifr) < 0) {
24291390a385Sgjelinek 			print_net_err(nwiftab.zone_nwif_physical,
24301390a385Sgjelinek 			    nwiftab.zone_nwif_address, af,
24317c478bd9Sstevel@tonic-gate 			    strerror(errno));
24327c478bd9Sstevel@tonic-gate 			return_code = Z_ERR;
24337c478bd9Sstevel@tonic-gate 		}
24347c478bd9Sstevel@tonic-gate 		(void) close(so);
24357c478bd9Sstevel@tonic-gate 	}
24367c478bd9Sstevel@tonic-gate 	(void) zonecfg_endnwifent(handle);
2437108322fbScarlsonj no_net:
24387c478bd9Sstevel@tonic-gate 
2439e7f3c547Sgjelinek 	/* verify that lofs has not been excluded from the kernel */
24408cd327d5Sgjelinek 	if (!(cmd_num == CMD_DETACH || cmd_num == CMD_ATTACH ||
24418cd327d5Sgjelinek 	    cmd_num == CMD_MOVE || cmd_num == CMD_CLONE) &&
24428cd327d5Sgjelinek 	    modctl(MODLOAD, 1, "fs/lofs", NULL) != 0) {
2443e7f3c547Sgjelinek 		if (errno == ENXIO)
2444e7f3c547Sgjelinek 			(void) fprintf(stderr, gettext("could not verify "
2445e7f3c547Sgjelinek 			    "lofs(7FS): possibly excluded in /etc/system\n"));
2446e7f3c547Sgjelinek 		else
2447e7f3c547Sgjelinek 			(void) fprintf(stderr, gettext("could not verify "
2448e7f3c547Sgjelinek 			    "lofs(7FS): %s\n"), strerror(errno));
2449e7f3c547Sgjelinek 		return_code = Z_ERR;
2450e7f3c547Sgjelinek 	}
2451e7f3c547Sgjelinek 
24527c478bd9Sstevel@tonic-gate 	if (verify_filesystems(handle) != Z_OK)
24537c478bd9Sstevel@tonic-gate 		return_code = Z_ERR;
2454b5abaf04Sgjelinek 	if (verify_ipd(handle) != Z_OK)
2455b5abaf04Sgjelinek 		return_code = Z_ERR;
2456108322fbScarlsonj 	if (!in_alt_root && verify_rctls(handle) != Z_OK)
24577c478bd9Sstevel@tonic-gate 		return_code = Z_ERR;
2458108322fbScarlsonj 	if (!in_alt_root && verify_pool(handle) != Z_OK)
24597c478bd9Sstevel@tonic-gate 		return_code = Z_ERR;
2460fa9e4066Sahrens 	if (!in_alt_root && verify_datasets(handle) != Z_OK)
2461fa9e4066Sahrens 		return_code = Z_ERR;
2462ffbafc53Scomay 
2463ffbafc53Scomay 	/*
2464ffbafc53Scomay 	 * As the "mount" command is used for patching/upgrading of zones
2465ffbafc53Scomay 	 * or other maintenance processes, the zone's privilege set is not
2466ffbafc53Scomay 	 * checked in this case.  Instead, the default, safe set of
2467ffbafc53Scomay 	 * privileges will be used when this zone is created in the
2468ffbafc53Scomay 	 * kernel.
2469ffbafc53Scomay 	 */
2470ffbafc53Scomay 	if (!in_alt_root && cmd_num != CMD_MOUNT &&
2471ffbafc53Scomay 	    verify_limitpriv(handle) != Z_OK)
2472ffbafc53Scomay 		return_code = Z_ERR;
24738cd327d5Sgjelinek 
24748cd327d5Sgjelinek 	return (return_code);
24758cd327d5Sgjelinek }
24768cd327d5Sgjelinek 
24778cd327d5Sgjelinek static int
24788cd327d5Sgjelinek verify_details(int cmd_num)
24798cd327d5Sgjelinek {
24808cd327d5Sgjelinek 	zone_dochandle_t handle;
24818cd327d5Sgjelinek 	char zonepath[MAXPATHLEN], checkpath[MAXPATHLEN];
24828cd327d5Sgjelinek 	int return_code = Z_OK;
24838cd327d5Sgjelinek 	int err;
24848cd327d5Sgjelinek 
24858cd327d5Sgjelinek 	if ((handle = zonecfg_init_handle()) == NULL) {
24868cd327d5Sgjelinek 		zperror(cmd_to_str(cmd_num), B_TRUE);
24878cd327d5Sgjelinek 		return (Z_ERR);
24888cd327d5Sgjelinek 	}
24898cd327d5Sgjelinek 	if ((err = zonecfg_get_handle(target_zone, handle)) != Z_OK) {
24908cd327d5Sgjelinek 		errno = err;
24918cd327d5Sgjelinek 		zperror(cmd_to_str(cmd_num), B_TRUE);
24928cd327d5Sgjelinek 		zonecfg_fini_handle(handle);
24938cd327d5Sgjelinek 		return (Z_ERR);
24948cd327d5Sgjelinek 	}
24958cd327d5Sgjelinek 	if ((err = zonecfg_get_zonepath(handle, zonepath, sizeof (zonepath))) !=
24968cd327d5Sgjelinek 	    Z_OK) {
24978cd327d5Sgjelinek 		errno = err;
24988cd327d5Sgjelinek 		zperror(cmd_to_str(cmd_num), B_TRUE);
24998cd327d5Sgjelinek 		zonecfg_fini_handle(handle);
25008cd327d5Sgjelinek 		return (Z_ERR);
25018cd327d5Sgjelinek 	}
25028cd327d5Sgjelinek 	/*
25038cd327d5Sgjelinek 	 * zonecfg_get_zonepath() gets its data from the XML repository.
25048cd327d5Sgjelinek 	 * Verify this against the index file, which is checked first by
25058cd327d5Sgjelinek 	 * zone_get_zonepath().  If they don't match, bail out.
25068cd327d5Sgjelinek 	 */
25078cd327d5Sgjelinek 	if ((err = zone_get_zonepath(target_zone, checkpath,
25088cd327d5Sgjelinek 	    sizeof (checkpath))) != Z_OK) {
25098cd327d5Sgjelinek 		errno = err;
25108cd327d5Sgjelinek 		zperror2(target_zone, gettext("could not get zone path"));
25118cd327d5Sgjelinek 		return (Z_ERR);
25128cd327d5Sgjelinek 	}
25138cd327d5Sgjelinek 	if (strcmp(zonepath, checkpath) != 0) {
25148cd327d5Sgjelinek 		/*
25158cd327d5Sgjelinek 		 * TRANSLATION_NOTE
25168cd327d5Sgjelinek 		 * XML and zonepath are literals that should not be translated.
25178cd327d5Sgjelinek 		 */
25188cd327d5Sgjelinek 		(void) fprintf(stderr, gettext("The XML repository has "
25198cd327d5Sgjelinek 		    "zonepath '%s',\nbut the index file has zonepath '%s'.\n"
25208cd327d5Sgjelinek 		    "These must match, so fix the incorrect entry.\n"),
25218cd327d5Sgjelinek 		    zonepath, checkpath);
25228cd327d5Sgjelinek 		return (Z_ERR);
25238cd327d5Sgjelinek 	}
25248cd327d5Sgjelinek 	if (validate_zonepath(zonepath, cmd_num) != Z_OK) {
25258cd327d5Sgjelinek 		(void) fprintf(stderr, gettext("could not verify zonepath %s "
25268cd327d5Sgjelinek 		    "because of the above errors.\n"), zonepath);
25278cd327d5Sgjelinek 		return_code = Z_ERR;
25288cd327d5Sgjelinek 	}
25298cd327d5Sgjelinek 
25308cd327d5Sgjelinek 	if (verify_handle(cmd_num, handle) != Z_OK)
25318cd327d5Sgjelinek 		return_code = Z_ERR;
25328cd327d5Sgjelinek 
25337c478bd9Sstevel@tonic-gate 	zonecfg_fini_handle(handle);
25347c478bd9Sstevel@tonic-gate 	if (return_code == Z_ERR)
25357c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
25367c478bd9Sstevel@tonic-gate 		    gettext("%s: zone %s failed to verify\n"),
25377c478bd9Sstevel@tonic-gate 		    execname, target_zone);
25387c478bd9Sstevel@tonic-gate 	return (return_code);
25397c478bd9Sstevel@tonic-gate }
25407c478bd9Sstevel@tonic-gate 
25417c478bd9Sstevel@tonic-gate static int
25427c478bd9Sstevel@tonic-gate verify_func(int argc, char *argv[])
25437c478bd9Sstevel@tonic-gate {
25447c478bd9Sstevel@tonic-gate 	int arg;
25457c478bd9Sstevel@tonic-gate 
25467c478bd9Sstevel@tonic-gate 	optind = 0;
25477c478bd9Sstevel@tonic-gate 	if ((arg = getopt(argc, argv, "?")) != EOF) {
25487c478bd9Sstevel@tonic-gate 		switch (arg) {
25497c478bd9Sstevel@tonic-gate 		case '?':
25507c478bd9Sstevel@tonic-gate 			sub_usage(SHELP_VERIFY, CMD_VERIFY);
25517c478bd9Sstevel@tonic-gate 			return (optopt == '?' ? Z_OK : Z_USAGE);
25527c478bd9Sstevel@tonic-gate 		default:
25537c478bd9Sstevel@tonic-gate 			sub_usage(SHELP_VERIFY, CMD_VERIFY);
25547c478bd9Sstevel@tonic-gate 			return (Z_USAGE);
25557c478bd9Sstevel@tonic-gate 		}
25567c478bd9Sstevel@tonic-gate 	}
25577c478bd9Sstevel@tonic-gate 	if (argc > optind) {
25587c478bd9Sstevel@tonic-gate 		sub_usage(SHELP_VERIFY, CMD_VERIFY);
25597c478bd9Sstevel@tonic-gate 		return (Z_USAGE);
25607c478bd9Sstevel@tonic-gate 	}
25617c478bd9Sstevel@tonic-gate 	if (sanity_check(target_zone, CMD_VERIFY, B_FALSE, B_FALSE) != Z_OK)
25627c478bd9Sstevel@tonic-gate 		return (Z_ERR);
25637c478bd9Sstevel@tonic-gate 	return (verify_details(CMD_VERIFY));
25647c478bd9Sstevel@tonic-gate }
25657c478bd9Sstevel@tonic-gate 
25667c478bd9Sstevel@tonic-gate #define	LUCREATEZONE	"/usr/lib/lu/lucreatezone"
25677c478bd9Sstevel@tonic-gate 
25687c478bd9Sstevel@tonic-gate static int
25697c478bd9Sstevel@tonic-gate install_func(int argc, char *argv[])
25707c478bd9Sstevel@tonic-gate {
25717c478bd9Sstevel@tonic-gate 	/* 9: "exec " and " -z " */
25727c478bd9Sstevel@tonic-gate 	char cmdbuf[sizeof (LUCREATEZONE) + ZONENAME_MAX + 9];
25737c478bd9Sstevel@tonic-gate 	int lockfd;
25747c478bd9Sstevel@tonic-gate 	int err, arg;
25757c478bd9Sstevel@tonic-gate 	char zonepath[MAXPATHLEN];
25767c478bd9Sstevel@tonic-gate 	int status;
25770b5de56dSgjelinek 	boolean_t nodataset = B_FALSE;
25787c478bd9Sstevel@tonic-gate 
2579108322fbScarlsonj 	if (zonecfg_in_alt_root()) {
2580108322fbScarlsonj 		zerror(gettext("cannot install zone in alternate root"));
2581108322fbScarlsonj 		return (Z_ERR);
2582108322fbScarlsonj 	}
2583108322fbScarlsonj 
25847c478bd9Sstevel@tonic-gate 	optind = 0;
25850b5de56dSgjelinek 	if ((arg = getopt(argc, argv, "?x:")) != EOF) {
25867c478bd9Sstevel@tonic-gate 		switch (arg) {
25877c478bd9Sstevel@tonic-gate 		case '?':
25887c478bd9Sstevel@tonic-gate 			sub_usage(SHELP_INSTALL, CMD_INSTALL);
25897c478bd9Sstevel@tonic-gate 			return (optopt == '?' ? Z_OK : Z_USAGE);
25900b5de56dSgjelinek 		case 'x':
25910b5de56dSgjelinek 			if (strcmp(optarg, "nodataset") != 0) {
25920b5de56dSgjelinek 				sub_usage(SHELP_INSTALL, CMD_INSTALL);
25930b5de56dSgjelinek 				return (Z_USAGE);
25940b5de56dSgjelinek 			}
25950b5de56dSgjelinek 			nodataset = B_TRUE;
25960b5de56dSgjelinek 			break;
25977c478bd9Sstevel@tonic-gate 		default:
25987c478bd9Sstevel@tonic-gate 			sub_usage(SHELP_INSTALL, CMD_INSTALL);
25997c478bd9Sstevel@tonic-gate 			return (Z_USAGE);
26007c478bd9Sstevel@tonic-gate 		}
26017c478bd9Sstevel@tonic-gate 	}
26027c478bd9Sstevel@tonic-gate 	if (argc > optind) {
26037c478bd9Sstevel@tonic-gate 		sub_usage(SHELP_INSTALL, CMD_INSTALL);
26047c478bd9Sstevel@tonic-gate 		return (Z_USAGE);
26057c478bd9Sstevel@tonic-gate 	}
26067c478bd9Sstevel@tonic-gate 	if (sanity_check(target_zone, CMD_INSTALL, B_FALSE, B_TRUE) != Z_OK)
26077c478bd9Sstevel@tonic-gate 		return (Z_ERR);
26087c478bd9Sstevel@tonic-gate 	if (verify_details(CMD_INSTALL) != Z_OK)
26097c478bd9Sstevel@tonic-gate 		return (Z_ERR);
26107c478bd9Sstevel@tonic-gate 
26117c478bd9Sstevel@tonic-gate 	if (grab_lock_file(target_zone, &lockfd) != Z_OK) {
26127c478bd9Sstevel@tonic-gate 		zerror(gettext("another %s may have an operation in progress."),
2613865e09a4Sgjelinek 		    "zoneadm");
26147c478bd9Sstevel@tonic-gate 		return (Z_ERR);
26157c478bd9Sstevel@tonic-gate 	}
26167c478bd9Sstevel@tonic-gate 	err = zone_set_state(target_zone, ZONE_STATE_INCOMPLETE);
26177c478bd9Sstevel@tonic-gate 	if (err != Z_OK) {
26187c478bd9Sstevel@tonic-gate 		errno = err;
26197c478bd9Sstevel@tonic-gate 		zperror2(target_zone, gettext("could not set state"));
26207c478bd9Sstevel@tonic-gate 		goto done;
26217c478bd9Sstevel@tonic-gate 	}
26227c478bd9Sstevel@tonic-gate 
26237c478bd9Sstevel@tonic-gate 	/*
26247c478bd9Sstevel@tonic-gate 	 * According to the Application Packaging Developer's Guide, a
26257c478bd9Sstevel@tonic-gate 	 * "checkinstall" script when included in a package is executed as
26267c478bd9Sstevel@tonic-gate 	 * the user "install", if such a user exists, or by the user
26277c478bd9Sstevel@tonic-gate 	 * "nobody".  In order to support this dubious behavior, the path
26287c478bd9Sstevel@tonic-gate 	 * to the zone being constructed is opened up during the life of
26297c478bd9Sstevel@tonic-gate 	 * the command laying down the zone's root file system.  Once this
26307c478bd9Sstevel@tonic-gate 	 * has completed, regardless of whether it was successful, the
26317c478bd9Sstevel@tonic-gate 	 * path to the zone is again restricted.
26327c478bd9Sstevel@tonic-gate 	 */
26337c478bd9Sstevel@tonic-gate 	if ((err = zone_get_zonepath(target_zone, zonepath,
26347c478bd9Sstevel@tonic-gate 	    sizeof (zonepath))) != Z_OK) {
26357c478bd9Sstevel@tonic-gate 		errno = err;
26367c478bd9Sstevel@tonic-gate 		zperror2(target_zone, gettext("could not get zone path"));
26377c478bd9Sstevel@tonic-gate 		goto done;
26387c478bd9Sstevel@tonic-gate 	}
26390b5de56dSgjelinek 
26400b5de56dSgjelinek 	if (!nodataset)
26410b5de56dSgjelinek 		create_zfs_zonepath(zonepath);
26420b5de56dSgjelinek 
26437c478bd9Sstevel@tonic-gate 	if (chmod(zonepath, DEFAULT_DIR_MODE) != 0) {
26447c478bd9Sstevel@tonic-gate 		zperror(zonepath, B_FALSE);
26457c478bd9Sstevel@tonic-gate 		err = Z_ERR;
26467c478bd9Sstevel@tonic-gate 		goto done;
26477c478bd9Sstevel@tonic-gate 	}
26487c478bd9Sstevel@tonic-gate 
26497c478bd9Sstevel@tonic-gate 	/*
26507c478bd9Sstevel@tonic-gate 	 * "exec" the command so that the returned status is that of
26517c478bd9Sstevel@tonic-gate 	 * LUCREATEZONE and not the shell.
26527c478bd9Sstevel@tonic-gate 	 */
26537c478bd9Sstevel@tonic-gate 	(void) snprintf(cmdbuf, sizeof (cmdbuf), "exec " LUCREATEZONE " -z %s",
26547c478bd9Sstevel@tonic-gate 	    target_zone);
26557c478bd9Sstevel@tonic-gate 	status = do_subproc(cmdbuf);
26567c478bd9Sstevel@tonic-gate 	if (chmod(zonepath, S_IRWXU) != 0) {
26577c478bd9Sstevel@tonic-gate 		zperror(zonepath, B_FALSE);
26587c478bd9Sstevel@tonic-gate 		err = Z_ERR;
26597c478bd9Sstevel@tonic-gate 		goto done;
26607c478bd9Sstevel@tonic-gate 	}
26617c478bd9Sstevel@tonic-gate 	if ((err = subproc_status(LUCREATEZONE, status)) != Z_OK)
26627c478bd9Sstevel@tonic-gate 		goto done;
26637c478bd9Sstevel@tonic-gate 
26647c478bd9Sstevel@tonic-gate 	if ((err = zone_set_state(target_zone, ZONE_STATE_INSTALLED)) != Z_OK) {
26657c478bd9Sstevel@tonic-gate 		errno = err;
26667c478bd9Sstevel@tonic-gate 		zperror2(target_zone, gettext("could not set state"));
26677c478bd9Sstevel@tonic-gate 		goto done;
26687c478bd9Sstevel@tonic-gate 	}
26697c478bd9Sstevel@tonic-gate 
26707c478bd9Sstevel@tonic-gate done:
26717c478bd9Sstevel@tonic-gate 	release_lock_file(lockfd);
26727c478bd9Sstevel@tonic-gate 	return ((err == Z_OK) ? Z_OK : Z_ERR);
26737c478bd9Sstevel@tonic-gate }
26747c478bd9Sstevel@tonic-gate 
26757c478bd9Sstevel@tonic-gate /*
2676865e09a4Sgjelinek  * Check that the inherited pkg dirs are the same for the clone and its source.
2677865e09a4Sgjelinek  * The easiest way to do that is check that the list of ipds is the same
2678865e09a4Sgjelinek  * by matching each one against the other.  This algorithm should be fine since
2679865e09a4Sgjelinek  * the list of ipds should not be that long.
2680865e09a4Sgjelinek  */
2681865e09a4Sgjelinek static int
2682865e09a4Sgjelinek valid_ipd_clone(zone_dochandle_t s_handle, char *source_zone,
2683865e09a4Sgjelinek 	zone_dochandle_t t_handle, char *target_zone)
2684865e09a4Sgjelinek {
2685865e09a4Sgjelinek 	int err;
2686865e09a4Sgjelinek 	int res = Z_OK;
2687865e09a4Sgjelinek 	int s_cnt = 0;
2688865e09a4Sgjelinek 	int t_cnt = 0;
2689865e09a4Sgjelinek 	struct zone_fstab s_fstab;
2690865e09a4Sgjelinek 	struct zone_fstab t_fstab;
2691865e09a4Sgjelinek 
2692865e09a4Sgjelinek 	/*
2693865e09a4Sgjelinek 	 * First check the source of the clone against the target.
2694865e09a4Sgjelinek 	 */
2695865e09a4Sgjelinek 	if ((err = zonecfg_setipdent(s_handle)) != Z_OK) {
2696865e09a4Sgjelinek 		errno = err;
2697865e09a4Sgjelinek 		zperror2(source_zone, gettext("could not enumerate "
2698865e09a4Sgjelinek 		    "inherit-pkg-dirs"));
2699865e09a4Sgjelinek 		return (Z_ERR);
2700865e09a4Sgjelinek 	}
2701865e09a4Sgjelinek 
2702865e09a4Sgjelinek 	while (zonecfg_getipdent(s_handle, &s_fstab) == Z_OK) {
2703865e09a4Sgjelinek 		boolean_t match = B_FALSE;
2704865e09a4Sgjelinek 
2705865e09a4Sgjelinek 		s_cnt++;
2706865e09a4Sgjelinek 
2707865e09a4Sgjelinek 		if ((err = zonecfg_setipdent(t_handle)) != Z_OK) {
2708865e09a4Sgjelinek 			errno = err;
2709865e09a4Sgjelinek 			zperror2(target_zone, gettext("could not enumerate "
2710865e09a4Sgjelinek 			    "inherit-pkg-dirs"));
2711865e09a4Sgjelinek 			(void) zonecfg_endipdent(s_handle);
2712865e09a4Sgjelinek 			return (Z_ERR);
2713865e09a4Sgjelinek 		}
2714865e09a4Sgjelinek 
2715865e09a4Sgjelinek 		while (zonecfg_getipdent(t_handle, &t_fstab) == Z_OK) {
2716865e09a4Sgjelinek 			if (strcmp(s_fstab.zone_fs_dir, t_fstab.zone_fs_dir)
2717865e09a4Sgjelinek 			    == 0) {
2718865e09a4Sgjelinek 				match = B_TRUE;
2719865e09a4Sgjelinek 				break;
2720865e09a4Sgjelinek 			}
2721865e09a4Sgjelinek 		}
2722865e09a4Sgjelinek 		(void) zonecfg_endipdent(t_handle);
2723865e09a4Sgjelinek 
2724865e09a4Sgjelinek 		if (!match) {
2725865e09a4Sgjelinek 			(void) fprintf(stderr, gettext("inherit-pkg-dir "
2726865e09a4Sgjelinek 			    "'%s' is not configured in zone %s.\n"),
2727865e09a4Sgjelinek 			    s_fstab.zone_fs_dir, target_zone);
2728865e09a4Sgjelinek 			res = Z_ERR;
2729865e09a4Sgjelinek 		}
2730865e09a4Sgjelinek 	}
2731865e09a4Sgjelinek 
2732865e09a4Sgjelinek 	(void) zonecfg_endipdent(s_handle);
2733865e09a4Sgjelinek 
2734865e09a4Sgjelinek 	/* skip the next check if we already have errors */
2735865e09a4Sgjelinek 	if (res == Z_ERR)
2736865e09a4Sgjelinek 		return (res);
2737865e09a4Sgjelinek 
2738865e09a4Sgjelinek 	/*
2739865e09a4Sgjelinek 	 * Now check the number of ipds in the target so we can verify
2740865e09a4Sgjelinek 	 * that the source is not a subset of the target.
2741865e09a4Sgjelinek 	 */
2742865e09a4Sgjelinek 	if ((err = zonecfg_setipdent(t_handle)) != Z_OK) {
2743865e09a4Sgjelinek 		errno = err;
2744865e09a4Sgjelinek 		zperror2(target_zone, gettext("could not enumerate "
2745865e09a4Sgjelinek 		    "inherit-pkg-dirs"));
2746865e09a4Sgjelinek 		return (Z_ERR);
2747865e09a4Sgjelinek 	}
2748865e09a4Sgjelinek 
2749865e09a4Sgjelinek 	while (zonecfg_getipdent(t_handle, &t_fstab) == Z_OK)
2750865e09a4Sgjelinek 		t_cnt++;
2751865e09a4Sgjelinek 
2752865e09a4Sgjelinek 	(void) zonecfg_endipdent(t_handle);
2753865e09a4Sgjelinek 
2754865e09a4Sgjelinek 	if (t_cnt != s_cnt) {
2755865e09a4Sgjelinek 		(void) fprintf(stderr, gettext("Zone %s is configured "
2756865e09a4Sgjelinek 		    "with inherit-pkg-dirs that are not configured in zone "
2757865e09a4Sgjelinek 		    "%s.\n"), target_zone, source_zone);
2758865e09a4Sgjelinek 		res = Z_ERR;
2759865e09a4Sgjelinek 	}
2760865e09a4Sgjelinek 
2761865e09a4Sgjelinek 	return (res);
2762865e09a4Sgjelinek }
2763865e09a4Sgjelinek 
2764865e09a4Sgjelinek static void
2765865e09a4Sgjelinek warn_dev_match(zone_dochandle_t s_handle, char *source_zone,
2766865e09a4Sgjelinek 	zone_dochandle_t t_handle, char *target_zone)
2767865e09a4Sgjelinek {
2768865e09a4Sgjelinek 	int err;
2769865e09a4Sgjelinek 	struct zone_devtab s_devtab;
2770865e09a4Sgjelinek 	struct zone_devtab t_devtab;
2771865e09a4Sgjelinek 
2772865e09a4Sgjelinek 	if ((err = zonecfg_setdevent(t_handle)) != Z_OK) {
2773865e09a4Sgjelinek 		errno = err;
2774865e09a4Sgjelinek 		zperror2(target_zone, gettext("could not enumerate devices"));
2775865e09a4Sgjelinek 		return;
2776865e09a4Sgjelinek 	}
2777865e09a4Sgjelinek 
2778865e09a4Sgjelinek 	while (zonecfg_getdevent(t_handle, &t_devtab) == Z_OK) {
2779865e09a4Sgjelinek 		if ((err = zonecfg_setdevent(s_handle)) != Z_OK) {
2780865e09a4Sgjelinek 			errno = err;
2781865e09a4Sgjelinek 			zperror2(source_zone,
2782865e09a4Sgjelinek 			    gettext("could not enumerate devices"));
2783865e09a4Sgjelinek 			(void) zonecfg_enddevent(t_handle);
2784865e09a4Sgjelinek 			return;
2785865e09a4Sgjelinek 		}
2786865e09a4Sgjelinek 
2787865e09a4Sgjelinek 		while (zonecfg_getdevent(s_handle, &s_devtab) == Z_OK) {
2788865e09a4Sgjelinek 			/*
2789865e09a4Sgjelinek 			 * Use fnmatch to catch the case where wildcards
2790865e09a4Sgjelinek 			 * were used in one zone and the other has an
2791865e09a4Sgjelinek 			 * explicit entry (e.g. /dev/dsk/c0t0d0s6 vs.
2792865e09a4Sgjelinek 			 * /dev/\*dsk/c0t0d0s6).
2793865e09a4Sgjelinek 			 */
2794865e09a4Sgjelinek 			if (fnmatch(t_devtab.zone_dev_match,
2795865e09a4Sgjelinek 			    s_devtab.zone_dev_match, FNM_PATHNAME) == 0 ||
2796865e09a4Sgjelinek 			    fnmatch(s_devtab.zone_dev_match,
2797865e09a4Sgjelinek 			    t_devtab.zone_dev_match, FNM_PATHNAME) == 0) {
2798865e09a4Sgjelinek 				(void) fprintf(stderr,
2799865e09a4Sgjelinek 				    gettext("WARNING: device '%s' "
2800865e09a4Sgjelinek 				    "is configured in both zones.\n"),
2801865e09a4Sgjelinek 				    t_devtab.zone_dev_match);
2802865e09a4Sgjelinek 				break;
2803865e09a4Sgjelinek 			}
2804865e09a4Sgjelinek 		}
2805865e09a4Sgjelinek 		(void) zonecfg_enddevent(s_handle);
2806865e09a4Sgjelinek 	}
2807865e09a4Sgjelinek 
2808865e09a4Sgjelinek 	(void) zonecfg_enddevent(t_handle);
2809865e09a4Sgjelinek }
2810865e09a4Sgjelinek 
2811865e09a4Sgjelinek /*
2812865e09a4Sgjelinek  * Check if the specified mount option (opt) is contained within the
2813865e09a4Sgjelinek  * options string.
2814865e09a4Sgjelinek  */
2815865e09a4Sgjelinek static boolean_t
2816865e09a4Sgjelinek opt_match(char *opt, char *options)
2817865e09a4Sgjelinek {
2818865e09a4Sgjelinek 	char *p;
2819865e09a4Sgjelinek 	char *lastp;
2820865e09a4Sgjelinek 
2821865e09a4Sgjelinek 	if ((p = strtok_r(options, ",", &lastp)) != NULL) {
2822865e09a4Sgjelinek 		if (strcmp(p, opt) == 0)
2823865e09a4Sgjelinek 			return (B_TRUE);
2824865e09a4Sgjelinek 		while ((p = strtok_r(NULL, ",", &lastp)) != NULL) {
2825865e09a4Sgjelinek 			if (strcmp(p, opt) == 0)
2826865e09a4Sgjelinek 				return (B_TRUE);
2827865e09a4Sgjelinek 		}
2828865e09a4Sgjelinek 	}
2829865e09a4Sgjelinek 
2830865e09a4Sgjelinek 	return (B_FALSE);
2831865e09a4Sgjelinek }
2832865e09a4Sgjelinek 
28330b5de56dSgjelinek #define	RW_LOFS	"WARNING: read-write lofs file system on '%s' is configured " \
2834865e09a4Sgjelinek 	"in both zones.\n"
2835865e09a4Sgjelinek 
2836865e09a4Sgjelinek static void
2837865e09a4Sgjelinek print_fs_warnings(struct zone_fstab *s_fstab, struct zone_fstab *t_fstab)
2838865e09a4Sgjelinek {
2839865e09a4Sgjelinek 	/*
2840865e09a4Sgjelinek 	 * It is ok to have shared lofs mounted fs but we want to warn if
2841865e09a4Sgjelinek 	 * either is rw since this will effect the other zone.
2842865e09a4Sgjelinek 	 */
2843865e09a4Sgjelinek 	if (strcmp(t_fstab->zone_fs_type, "lofs") == 0) {
2844865e09a4Sgjelinek 		zone_fsopt_t *optp;
2845865e09a4Sgjelinek 
2846865e09a4Sgjelinek 		/* The default is rw so no options means rw */
2847865e09a4Sgjelinek 		if (t_fstab->zone_fs_options == NULL ||
2848865e09a4Sgjelinek 		    s_fstab->zone_fs_options == NULL) {
2849865e09a4Sgjelinek 			(void) fprintf(stderr, gettext(RW_LOFS),
2850865e09a4Sgjelinek 			    t_fstab->zone_fs_special);
2851865e09a4Sgjelinek 			return;
2852865e09a4Sgjelinek 		}
2853865e09a4Sgjelinek 
2854865e09a4Sgjelinek 		for (optp = s_fstab->zone_fs_options; optp != NULL;
2855865e09a4Sgjelinek 		    optp = optp->zone_fsopt_next) {
2856865e09a4Sgjelinek 			if (opt_match("rw", optp->zone_fsopt_opt)) {
2857865e09a4Sgjelinek 				(void) fprintf(stderr, gettext(RW_LOFS),
2858865e09a4Sgjelinek 				    s_fstab->zone_fs_special);
2859865e09a4Sgjelinek 				return;
2860865e09a4Sgjelinek 			}
2861865e09a4Sgjelinek 		}
2862865e09a4Sgjelinek 
2863865e09a4Sgjelinek 		for (optp = t_fstab->zone_fs_options; optp != NULL;
2864865e09a4Sgjelinek 		    optp = optp->zone_fsopt_next) {
2865865e09a4Sgjelinek 			if (opt_match("rw", optp->zone_fsopt_opt)) {
2866865e09a4Sgjelinek 				(void) fprintf(stderr, gettext(RW_LOFS),
2867865e09a4Sgjelinek 				    t_fstab->zone_fs_special);
2868865e09a4Sgjelinek 				return;
2869865e09a4Sgjelinek 			}
2870865e09a4Sgjelinek 		}
2871865e09a4Sgjelinek 
2872865e09a4Sgjelinek 		return;
2873865e09a4Sgjelinek 	}
2874865e09a4Sgjelinek 
2875865e09a4Sgjelinek 	/*
2876865e09a4Sgjelinek 	 * TRANSLATION_NOTE
28770b5de56dSgjelinek 	 * The first variable is the file system type and the second is
28780b5de56dSgjelinek 	 * the file system special device.  For example,
28790b5de56dSgjelinek 	 * WARNING: ufs file system on '/dev/dsk/c0t0d0s0' ...
2880865e09a4Sgjelinek 	 */
28810b5de56dSgjelinek 	(void) fprintf(stderr, gettext("WARNING: %s file system on '%s' "
2882865e09a4Sgjelinek 	    "is configured in both zones.\n"), t_fstab->zone_fs_type,
2883865e09a4Sgjelinek 	    t_fstab->zone_fs_special);
2884865e09a4Sgjelinek }
2885865e09a4Sgjelinek 
2886865e09a4Sgjelinek static void
2887865e09a4Sgjelinek warn_fs_match(zone_dochandle_t s_handle, char *source_zone,
2888865e09a4Sgjelinek 	zone_dochandle_t t_handle, char *target_zone)
2889865e09a4Sgjelinek {
2890865e09a4Sgjelinek 	int err;
2891865e09a4Sgjelinek 	struct zone_fstab s_fstab;
2892865e09a4Sgjelinek 	struct zone_fstab t_fstab;
2893865e09a4Sgjelinek 
2894865e09a4Sgjelinek 	if ((err = zonecfg_setfsent(t_handle)) != Z_OK) {
2895865e09a4Sgjelinek 		errno = err;
2896865e09a4Sgjelinek 		zperror2(target_zone,
28970b5de56dSgjelinek 		    gettext("could not enumerate file systems"));
2898865e09a4Sgjelinek 		return;
2899865e09a4Sgjelinek 	}
2900865e09a4Sgjelinek 
2901865e09a4Sgjelinek 	while (zonecfg_getfsent(t_handle, &t_fstab) == Z_OK) {
2902865e09a4Sgjelinek 		if ((err = zonecfg_setfsent(s_handle)) != Z_OK) {
2903865e09a4Sgjelinek 			errno = err;
2904865e09a4Sgjelinek 			zperror2(source_zone,
29050b5de56dSgjelinek 			    gettext("could not enumerate file systems"));
2906865e09a4Sgjelinek 			(void) zonecfg_endfsent(t_handle);
2907865e09a4Sgjelinek 			return;
2908865e09a4Sgjelinek 		}
2909865e09a4Sgjelinek 
2910865e09a4Sgjelinek 		while (zonecfg_getfsent(s_handle, &s_fstab) == Z_OK) {
2911865e09a4Sgjelinek 			if (strcmp(t_fstab.zone_fs_special,
2912865e09a4Sgjelinek 			    s_fstab.zone_fs_special) == 0) {
2913865e09a4Sgjelinek 				print_fs_warnings(&s_fstab, &t_fstab);
2914865e09a4Sgjelinek 				break;
2915865e09a4Sgjelinek 			}
2916865e09a4Sgjelinek 		}
2917865e09a4Sgjelinek 		(void) zonecfg_endfsent(s_handle);
2918865e09a4Sgjelinek 	}
2919865e09a4Sgjelinek 
2920865e09a4Sgjelinek 	(void) zonecfg_endfsent(t_handle);
2921865e09a4Sgjelinek }
2922865e09a4Sgjelinek 
2923865e09a4Sgjelinek /*
2924865e09a4Sgjelinek  * We don't catch the case where you used the same IP address but
2925865e09a4Sgjelinek  * it is not an exact string match.  For example, 192.9.0.128 vs. 192.09.0.128.
2926865e09a4Sgjelinek  * However, we're not going to worry about that but we will check for
2927865e09a4Sgjelinek  * a possible netmask on one of the addresses (e.g. 10.0.0.1 and 10.0.0.1/24)
2928865e09a4Sgjelinek  * and handle that case as a match.
2929865e09a4Sgjelinek  */
2930865e09a4Sgjelinek static void
2931865e09a4Sgjelinek warn_ip_match(zone_dochandle_t s_handle, char *source_zone,
2932865e09a4Sgjelinek 	zone_dochandle_t t_handle, char *target_zone)
2933865e09a4Sgjelinek {
2934865e09a4Sgjelinek 	int err;
2935865e09a4Sgjelinek 	struct zone_nwiftab s_nwiftab;
2936865e09a4Sgjelinek 	struct zone_nwiftab t_nwiftab;
2937865e09a4Sgjelinek 
2938865e09a4Sgjelinek 	if ((err = zonecfg_setnwifent(t_handle)) != Z_OK) {
2939865e09a4Sgjelinek 		errno = err;
2940865e09a4Sgjelinek 		zperror2(target_zone,
2941865e09a4Sgjelinek 		    gettext("could not enumerate network interfaces"));
2942865e09a4Sgjelinek 		return;
2943865e09a4Sgjelinek 	}
2944865e09a4Sgjelinek 
2945865e09a4Sgjelinek 	while (zonecfg_getnwifent(t_handle, &t_nwiftab) == Z_OK) {
2946865e09a4Sgjelinek 		char *p;
2947865e09a4Sgjelinek 
2948865e09a4Sgjelinek 		/* remove an (optional) netmask from the address */
2949865e09a4Sgjelinek 		if ((p = strchr(t_nwiftab.zone_nwif_address, '/')) != NULL)
2950865e09a4Sgjelinek 			*p = '\0';
2951865e09a4Sgjelinek 
2952865e09a4Sgjelinek 		if ((err = zonecfg_setnwifent(s_handle)) != Z_OK) {
2953865e09a4Sgjelinek 			errno = err;
2954865e09a4Sgjelinek 			zperror2(source_zone,
2955865e09a4Sgjelinek 			    gettext("could not enumerate network interfaces"));
2956865e09a4Sgjelinek 			(void) zonecfg_endnwifent(t_handle);
2957865e09a4Sgjelinek 			return;
2958865e09a4Sgjelinek 		}
2959865e09a4Sgjelinek 
2960865e09a4Sgjelinek 		while (zonecfg_getnwifent(s_handle, &s_nwiftab) == Z_OK) {
2961865e09a4Sgjelinek 			/* remove an (optional) netmask from the address */
2962865e09a4Sgjelinek 			if ((p = strchr(s_nwiftab.zone_nwif_address, '/'))
2963865e09a4Sgjelinek 			    != NULL)
2964865e09a4Sgjelinek 				*p = '\0';
2965865e09a4Sgjelinek 
2966865e09a4Sgjelinek 			if (strcmp(t_nwiftab.zone_nwif_address,
2967865e09a4Sgjelinek 			    s_nwiftab.zone_nwif_address) == 0) {
2968865e09a4Sgjelinek 				(void) fprintf(stderr,
2969865e09a4Sgjelinek 				    gettext("WARNING: network address '%s' "
2970865e09a4Sgjelinek 				    "is configured in both zones.\n"),
2971865e09a4Sgjelinek 				    t_nwiftab.zone_nwif_address);
2972865e09a4Sgjelinek 				break;
2973865e09a4Sgjelinek 			}
2974865e09a4Sgjelinek 		}
2975865e09a4Sgjelinek 		(void) zonecfg_endnwifent(s_handle);
2976865e09a4Sgjelinek 	}
2977865e09a4Sgjelinek 
2978865e09a4Sgjelinek 	(void) zonecfg_endnwifent(t_handle);
2979865e09a4Sgjelinek }
2980865e09a4Sgjelinek 
2981865e09a4Sgjelinek static void
2982865e09a4Sgjelinek warn_dataset_match(zone_dochandle_t s_handle, char *source_zone,
2983865e09a4Sgjelinek 	zone_dochandle_t t_handle, char *target_zone)
2984865e09a4Sgjelinek {
2985865e09a4Sgjelinek 	int err;
2986865e09a4Sgjelinek 	struct zone_dstab s_dstab;
2987865e09a4Sgjelinek 	struct zone_dstab t_dstab;
2988865e09a4Sgjelinek 
2989865e09a4Sgjelinek 	if ((err = zonecfg_setdsent(t_handle)) != Z_OK) {
2990865e09a4Sgjelinek 		errno = err;
2991865e09a4Sgjelinek 		zperror2(target_zone, gettext("could not enumerate datasets"));
2992865e09a4Sgjelinek 		return;
2993865e09a4Sgjelinek 	}
2994865e09a4Sgjelinek 
2995865e09a4Sgjelinek 	while (zonecfg_getdsent(t_handle, &t_dstab) == Z_OK) {
2996865e09a4Sgjelinek 		if ((err = zonecfg_setdsent(s_handle)) != Z_OK) {
2997865e09a4Sgjelinek 			errno = err;
2998865e09a4Sgjelinek 			zperror2(source_zone,
2999865e09a4Sgjelinek 			    gettext("could not enumerate datasets"));
3000865e09a4Sgjelinek 			(void) zonecfg_enddsent(t_handle);
3001865e09a4Sgjelinek 			return;
3002865e09a4Sgjelinek 		}
3003865e09a4Sgjelinek 
3004865e09a4Sgjelinek 		while (zonecfg_getdsent(s_handle, &s_dstab) == Z_OK) {
3005865e09a4Sgjelinek 			if (strcmp(t_dstab.zone_dataset_name,
3006865e09a4Sgjelinek 			    s_dstab.zone_dataset_name) == 0) {
3007865e09a4Sgjelinek 				(void) fprintf(stderr,
3008865e09a4Sgjelinek 				    gettext("WARNING: dataset '%s' "
3009865e09a4Sgjelinek 				    "is configured in both zones.\n"),
3010865e09a4Sgjelinek 				    t_dstab.zone_dataset_name);
3011865e09a4Sgjelinek 				break;
3012865e09a4Sgjelinek 			}
3013865e09a4Sgjelinek 		}
3014865e09a4Sgjelinek 		(void) zonecfg_enddsent(s_handle);
3015865e09a4Sgjelinek 	}
3016865e09a4Sgjelinek 
3017865e09a4Sgjelinek 	(void) zonecfg_enddsent(t_handle);
3018865e09a4Sgjelinek }
3019865e09a4Sgjelinek 
3020865e09a4Sgjelinek static int
3021865e09a4Sgjelinek validate_clone(char *source_zone, char *target_zone)
3022865e09a4Sgjelinek {
3023865e09a4Sgjelinek 	int err = Z_OK;
3024865e09a4Sgjelinek 	zone_dochandle_t s_handle;
3025865e09a4Sgjelinek 	zone_dochandle_t t_handle;
3026865e09a4Sgjelinek 
3027865e09a4Sgjelinek 	if ((t_handle = zonecfg_init_handle()) == NULL) {
3028865e09a4Sgjelinek 		zperror(cmd_to_str(CMD_CLONE), B_TRUE);
3029865e09a4Sgjelinek 		return (Z_ERR);
3030865e09a4Sgjelinek 	}
3031865e09a4Sgjelinek 	if ((err = zonecfg_get_handle(target_zone, t_handle)) != Z_OK) {
3032865e09a4Sgjelinek 		errno = err;
3033865e09a4Sgjelinek 		zperror(cmd_to_str(CMD_CLONE), B_TRUE);
3034865e09a4Sgjelinek 		zonecfg_fini_handle(t_handle);
3035865e09a4Sgjelinek 		return (Z_ERR);
3036865e09a4Sgjelinek 	}
3037865e09a4Sgjelinek 
3038865e09a4Sgjelinek 	if ((s_handle = zonecfg_init_handle()) == NULL) {
3039865e09a4Sgjelinek 		zperror(cmd_to_str(CMD_CLONE), B_TRUE);
3040865e09a4Sgjelinek 		zonecfg_fini_handle(t_handle);
3041865e09a4Sgjelinek 		return (Z_ERR);
3042865e09a4Sgjelinek 	}
3043865e09a4Sgjelinek 	if ((err = zonecfg_get_handle(source_zone, s_handle)) != Z_OK) {
3044865e09a4Sgjelinek 		errno = err;
3045865e09a4Sgjelinek 		zperror(cmd_to_str(CMD_CLONE), B_TRUE);
3046865e09a4Sgjelinek 		goto done;
3047865e09a4Sgjelinek 	}
3048865e09a4Sgjelinek 
3049865e09a4Sgjelinek 	/* verify new zone has same inherit-pkg-dirs */
3050865e09a4Sgjelinek 	err = valid_ipd_clone(s_handle, source_zone, t_handle, target_zone);
3051865e09a4Sgjelinek 
3052865e09a4Sgjelinek 	/* warn about imported fs's which are the same */
3053865e09a4Sgjelinek 	warn_fs_match(s_handle, source_zone, t_handle, target_zone);
3054865e09a4Sgjelinek 
3055865e09a4Sgjelinek 	/* warn about imported IP addresses which are the same */
3056865e09a4Sgjelinek 	warn_ip_match(s_handle, source_zone, t_handle, target_zone);
3057865e09a4Sgjelinek 
3058865e09a4Sgjelinek 	/* warn about imported devices which are the same */
3059865e09a4Sgjelinek 	warn_dev_match(s_handle, source_zone, t_handle, target_zone);
3060865e09a4Sgjelinek 
3061865e09a4Sgjelinek 	/* warn about imported datasets which are the same */
3062865e09a4Sgjelinek 	warn_dataset_match(s_handle, source_zone, t_handle, target_zone);
3063865e09a4Sgjelinek 
3064865e09a4Sgjelinek done:
3065865e09a4Sgjelinek 	zonecfg_fini_handle(t_handle);
3066865e09a4Sgjelinek 	zonecfg_fini_handle(s_handle);
3067865e09a4Sgjelinek 
3068865e09a4Sgjelinek 	return ((err == Z_OK) ? Z_OK : Z_ERR);
3069865e09a4Sgjelinek }
3070865e09a4Sgjelinek 
3071865e09a4Sgjelinek static int
3072865e09a4Sgjelinek copy_zone(char *src, char *dst)
3073865e09a4Sgjelinek {
3074865e09a4Sgjelinek 	boolean_t out_null = B_FALSE;
3075865e09a4Sgjelinek 	int status;
3076865e09a4Sgjelinek 	int err;
3077865e09a4Sgjelinek 	char *outfile;
3078865e09a4Sgjelinek 	char cmdbuf[MAXPATHLEN * 2 + 128];
3079865e09a4Sgjelinek 
3080865e09a4Sgjelinek 	if ((outfile = tempnam("/var/log", "zone")) == NULL) {
3081865e09a4Sgjelinek 		outfile = "/dev/null";
3082865e09a4Sgjelinek 		out_null = B_TRUE;
3083865e09a4Sgjelinek 	}
3084865e09a4Sgjelinek 
30850b5de56dSgjelinek 	/*
30860b5de56dSgjelinek 	 * Use find to get the list of files to copy.  We need to skip
30870b5de56dSgjelinek 	 * files of type "socket" since cpio can't handle those but that
30880b5de56dSgjelinek 	 * should be ok since the app will recreate the socket when it runs.
30890b5de56dSgjelinek 	 * We also need to filter out anything under the .zfs subdir.  Since
30900b5de56dSgjelinek 	 * find is running depth-first, we need the extra egrep to filter .zfs.
30910b5de56dSgjelinek 	 */
3092865e09a4Sgjelinek 	(void) snprintf(cmdbuf, sizeof (cmdbuf),
30930b5de56dSgjelinek 	    "cd %s && /usr/bin/find . -type s -prune -o -depth -print | "
309407b574eeSgjelinek 	    "/usr/bin/egrep -v '^\\./\\.zfs$|^\\./\\.zfs/' | "
3095865e09a4Sgjelinek 	    "/usr/bin/cpio -pdmuP@ %s > %s 2>&1",
3096865e09a4Sgjelinek 	    src, dst, outfile);
3097865e09a4Sgjelinek 
3098865e09a4Sgjelinek 	status = do_subproc(cmdbuf);
3099865e09a4Sgjelinek 
3100865e09a4Sgjelinek 	if ((err = subproc_status("copy", status)) != Z_OK) {
3101865e09a4Sgjelinek 		if (!out_null)
3102865e09a4Sgjelinek 			(void) fprintf(stderr, gettext("\nThe copy failed.\n"
3103865e09a4Sgjelinek 			    "More information can be found in %s\n"), outfile);
3104865e09a4Sgjelinek 		return (err);
3105865e09a4Sgjelinek 	}
3106865e09a4Sgjelinek 
3107865e09a4Sgjelinek 	if (!out_null)
3108865e09a4Sgjelinek 		(void) unlink(outfile);
3109865e09a4Sgjelinek 
3110865e09a4Sgjelinek 	return (Z_OK);
3111865e09a4Sgjelinek }
3112865e09a4Sgjelinek 
3113865e09a4Sgjelinek /*
31145cd08338Sgjelinek  * Run sys-unconfig on a zone.  This will leave the zone in the installed
31155cd08338Sgjelinek  * state as long as there were no errors during the sys-unconfig.
3116865e09a4Sgjelinek  */
3117865e09a4Sgjelinek static int
31185cd08338Sgjelinek unconfigure_zone(char *zonepath)
3119865e09a4Sgjelinek {
31205cd08338Sgjelinek 	int		err;
3121865e09a4Sgjelinek 	int		status;
31225cd08338Sgjelinek 	struct stat	unconfig_buf;
31235cd08338Sgjelinek 	zone_cmd_arg_t	zarg;
31245cd08338Sgjelinek 	char		cmdbuf[MAXPATHLEN + 51];
3125865e09a4Sgjelinek 
31265cd08338Sgjelinek 	/* The zone has to be installed in order to mount the scratch zone. */
31275cd08338Sgjelinek 	if ((err = zone_set_state(target_zone, ZONE_STATE_INSTALLED)) != Z_OK) {
31285cd08338Sgjelinek 		errno = err;
31295cd08338Sgjelinek 		zperror2(target_zone, gettext("could not set state"));
31305cd08338Sgjelinek 		return (Z_ERR);
31315cd08338Sgjelinek 	}
31325cd08338Sgjelinek 
31335cd08338Sgjelinek 	/*
313445916cd2Sjpk 	 * Trusted Extensions requires that cloned zones use the
313545916cd2Sjpk 	 * same sysid configuration, so it is not appropriate to
313645916cd2Sjpk 	 * unconfigure the zone.
313745916cd2Sjpk 	 */
313845916cd2Sjpk 	if (is_system_labeled())
313945916cd2Sjpk 		return (Z_OK);
314045916cd2Sjpk 
314145916cd2Sjpk 	/*
31425cd08338Sgjelinek 	 * Check if the zone is already sys-unconfiged.  This saves us
31435cd08338Sgjelinek 	 * the work of bringing up the scratch zone so we can unconfigure it.
31445cd08338Sgjelinek 	 */
31455cd08338Sgjelinek 	(void) snprintf(cmdbuf, sizeof (cmdbuf), "%s/root/etc/.UNCONFIGURED",
31465cd08338Sgjelinek 	    zonepath);
31475cd08338Sgjelinek 	if (stat(cmdbuf, &unconfig_buf) == 0)
3148865e09a4Sgjelinek 		return (Z_OK);
3149865e09a4Sgjelinek 
31505cd08338Sgjelinek 	zarg.cmd = Z_MOUNT;
31515cd08338Sgjelinek 	if (call_zoneadmd(target_zone, &zarg) != 0) {
31525cd08338Sgjelinek 		zerror(gettext("call to %s failed"), "zoneadmd");
31535cd08338Sgjelinek 		(void) zone_set_state(target_zone, ZONE_STATE_INCOMPLETE);
31545cd08338Sgjelinek 		return (Z_ERR);
3155865e09a4Sgjelinek 	}
3156865e09a4Sgjelinek 
31575cd08338Sgjelinek 	(void) snprintf(cmdbuf, sizeof (cmdbuf),
31585cd08338Sgjelinek 	    "/usr/sbin/zlogin -S %s /usr/sbin/sys-unconfig -R /a", target_zone);
31595cd08338Sgjelinek 
31605cd08338Sgjelinek 	status = do_subproc(cmdbuf);
31615cd08338Sgjelinek 	if ((err = subproc_status("sys-unconfig", status)) != Z_OK) {
31625cd08338Sgjelinek 		errno = err;
31635cd08338Sgjelinek 		zperror2(target_zone, gettext("sys-unconfig failed\n"));
31645cd08338Sgjelinek 		(void) zone_set_state(target_zone, ZONE_STATE_INCOMPLETE);
3165865e09a4Sgjelinek 	}
3166865e09a4Sgjelinek 
31675cd08338Sgjelinek 	zarg.cmd = Z_UNMOUNT;
31685cd08338Sgjelinek 	if (call_zoneadmd(target_zone, &zarg) != 0) {
31695cd08338Sgjelinek 		zerror(gettext("call to %s failed"), "zoneadmd");
31705cd08338Sgjelinek 		(void) fprintf(stderr, gettext("could not unmount zone\n"));
31715cd08338Sgjelinek 		return (Z_ERR);
31725cd08338Sgjelinek 	}
31735cd08338Sgjelinek 
31745cd08338Sgjelinek 	return ((err == Z_OK) ? Z_OK : Z_ERR);
31755cd08338Sgjelinek }
3176865e09a4Sgjelinek 
3177865e09a4Sgjelinek /* ARGSUSED */
31780b5de56dSgjelinek static int
3179865e09a4Sgjelinek zfm_print(const char *p, void *r) {
3180865e09a4Sgjelinek 	zerror("  %s\n", p);
3181865e09a4Sgjelinek 	return (0);
3182865e09a4Sgjelinek }
3183865e09a4Sgjelinek 
31840b5de56dSgjelinek int
31850b5de56dSgjelinek clone_copy(char *source_zonepath, char *zonepath)
31860b5de56dSgjelinek {
31870b5de56dSgjelinek 	int err;
31880b5de56dSgjelinek 
31890b5de56dSgjelinek 	/* Don't clone the zone if anything is still mounted there */
31900b5de56dSgjelinek 	if (zonecfg_find_mounts(source_zonepath, NULL, NULL)) {
31910b5de56dSgjelinek 		zerror(gettext("These file systems are mounted on "
31920b5de56dSgjelinek 		    "subdirectories of %s.\n"), source_zonepath);
31930b5de56dSgjelinek 		(void) zonecfg_find_mounts(source_zonepath, zfm_print, NULL);
31940b5de56dSgjelinek 		return (Z_ERR);
31950b5de56dSgjelinek 	}
31960b5de56dSgjelinek 
31970b5de56dSgjelinek 	/*
31980b5de56dSgjelinek 	 * Attempt to create a ZFS fs for the zonepath.  As usual, we don't
31990b5de56dSgjelinek 	 * care if this works or not since we always have the default behavior
32000b5de56dSgjelinek 	 * of a simple directory for the zonepath.
32010b5de56dSgjelinek 	 */
32020b5de56dSgjelinek 	create_zfs_zonepath(zonepath);
32030b5de56dSgjelinek 
32040b5de56dSgjelinek 	(void) printf(gettext("Copying %s..."), source_zonepath);
32050b5de56dSgjelinek 	(void) fflush(stdout);
32060b5de56dSgjelinek 
32070b5de56dSgjelinek 	err = copy_zone(source_zonepath, zonepath);
32080b5de56dSgjelinek 
32090b5de56dSgjelinek 	(void) printf("\n");
32100b5de56dSgjelinek 
32110b5de56dSgjelinek 	return (err);
32120b5de56dSgjelinek }
32130b5de56dSgjelinek 
3214865e09a4Sgjelinek static int
3215865e09a4Sgjelinek clone_func(int argc, char *argv[])
3216865e09a4Sgjelinek {
3217865e09a4Sgjelinek 	char *source_zone = NULL;
3218865e09a4Sgjelinek 	int lockfd;
3219865e09a4Sgjelinek 	int err, arg;
3220865e09a4Sgjelinek 	char zonepath[MAXPATHLEN];
3221865e09a4Sgjelinek 	char source_zonepath[MAXPATHLEN];
3222865e09a4Sgjelinek 	zone_state_t state;
3223865e09a4Sgjelinek 	zone_entry_t *zent;
32240b5de56dSgjelinek 	char *method = NULL;
32250b5de56dSgjelinek 	char *snapshot = NULL;
3226865e09a4Sgjelinek 
3227865e09a4Sgjelinek 	if (zonecfg_in_alt_root()) {
3228865e09a4Sgjelinek 		zerror(gettext("cannot clone zone in alternate root"));
3229865e09a4Sgjelinek 		return (Z_ERR);
3230865e09a4Sgjelinek 	}
3231865e09a4Sgjelinek 
3232865e09a4Sgjelinek 	optind = 0;
32330b5de56dSgjelinek 	if ((arg = getopt(argc, argv, "?m:s:")) != EOF) {
3234865e09a4Sgjelinek 		switch (arg) {
3235865e09a4Sgjelinek 		case '?':
3236865e09a4Sgjelinek 			sub_usage(SHELP_CLONE, CMD_CLONE);
3237865e09a4Sgjelinek 			return (optopt == '?' ? Z_OK : Z_USAGE);
3238865e09a4Sgjelinek 		case 'm':
3239865e09a4Sgjelinek 			method = optarg;
3240865e09a4Sgjelinek 			break;
32410b5de56dSgjelinek 		case 's':
32420b5de56dSgjelinek 			snapshot = optarg;
32430b5de56dSgjelinek 			break;
3244865e09a4Sgjelinek 		default:
3245865e09a4Sgjelinek 			sub_usage(SHELP_CLONE, CMD_CLONE);
3246865e09a4Sgjelinek 			return (Z_USAGE);
3247865e09a4Sgjelinek 		}
3248865e09a4Sgjelinek 	}
32490b5de56dSgjelinek 	if (argc != (optind + 1) ||
32500b5de56dSgjelinek 	    (method != NULL && strcmp(method, "copy") != 0)) {
3251865e09a4Sgjelinek 		sub_usage(SHELP_CLONE, CMD_CLONE);
3252865e09a4Sgjelinek 		return (Z_USAGE);
3253865e09a4Sgjelinek 	}
3254865e09a4Sgjelinek 	source_zone = argv[optind];
3255865e09a4Sgjelinek 	if (sanity_check(target_zone, CMD_CLONE, B_FALSE, B_TRUE) != Z_OK)
3256865e09a4Sgjelinek 		return (Z_ERR);
3257865e09a4Sgjelinek 	if (verify_details(CMD_CLONE) != Z_OK)
3258865e09a4Sgjelinek 		return (Z_ERR);
3259865e09a4Sgjelinek 
3260865e09a4Sgjelinek 	/*
3261865e09a4Sgjelinek 	 * We also need to do some extra validation on the source zone.
3262865e09a4Sgjelinek 	 */
3263865e09a4Sgjelinek 	if (strcmp(source_zone, GLOBAL_ZONENAME) == 0) {
3264865e09a4Sgjelinek 		zerror(gettext("%s operation is invalid for the global zone."),
3265865e09a4Sgjelinek 		    cmd_to_str(CMD_CLONE));
3266865e09a4Sgjelinek 		return (Z_ERR);
3267865e09a4Sgjelinek 	}
3268865e09a4Sgjelinek 
3269865e09a4Sgjelinek 	if (strncmp(source_zone, "SUNW", 4) == 0) {
3270865e09a4Sgjelinek 		zerror(gettext("%s operation is invalid for zones starting "
3271865e09a4Sgjelinek 		    "with SUNW."), cmd_to_str(CMD_CLONE));
3272865e09a4Sgjelinek 		return (Z_ERR);
3273865e09a4Sgjelinek 	}
3274865e09a4Sgjelinek 
3275865e09a4Sgjelinek 	zent = lookup_running_zone(source_zone);
3276865e09a4Sgjelinek 	if (zent != NULL) {
3277865e09a4Sgjelinek 		/* check whether the zone is ready or running */
3278865e09a4Sgjelinek 		if ((err = zone_get_state(zent->zname, &zent->zstate_num))
3279865e09a4Sgjelinek 		    != Z_OK) {
3280865e09a4Sgjelinek 			errno = err;
3281865e09a4Sgjelinek 			zperror2(zent->zname, gettext("could not get state"));
3282865e09a4Sgjelinek 			/* can't tell, so hedge */
3283865e09a4Sgjelinek 			zent->zstate_str = "ready/running";
3284865e09a4Sgjelinek 		} else {
3285865e09a4Sgjelinek 			zent->zstate_str = zone_state_str(zent->zstate_num);
3286865e09a4Sgjelinek 		}
3287865e09a4Sgjelinek 		zerror(gettext("%s operation is invalid for %s zones."),
3288865e09a4Sgjelinek 		    cmd_to_str(CMD_CLONE), zent->zstate_str);
3289865e09a4Sgjelinek 		return (Z_ERR);
3290865e09a4Sgjelinek 	}
3291865e09a4Sgjelinek 
3292865e09a4Sgjelinek 	if ((err = zone_get_state(source_zone, &state)) != Z_OK) {
3293865e09a4Sgjelinek 		errno = err;
3294865e09a4Sgjelinek 		zperror2(source_zone, gettext("could not get state"));
3295865e09a4Sgjelinek 		return (Z_ERR);
3296865e09a4Sgjelinek 	}
3297865e09a4Sgjelinek 	if (state != ZONE_STATE_INSTALLED) {
3298865e09a4Sgjelinek 		(void) fprintf(stderr,
3299865e09a4Sgjelinek 		    gettext("%s: zone %s is %s; %s is required.\n"),
3300865e09a4Sgjelinek 		    execname, source_zone, zone_state_str(state),
3301865e09a4Sgjelinek 		    zone_state_str(ZONE_STATE_INSTALLED));
3302865e09a4Sgjelinek 		return (Z_ERR);
3303865e09a4Sgjelinek 	}
3304865e09a4Sgjelinek 
3305865e09a4Sgjelinek 	/*
3306865e09a4Sgjelinek 	 * The source zone checks out ok, continue with the clone.
3307865e09a4Sgjelinek 	 */
3308865e09a4Sgjelinek 
3309865e09a4Sgjelinek 	if (validate_clone(source_zone, target_zone) != Z_OK)
3310865e09a4Sgjelinek 		return (Z_ERR);
3311865e09a4Sgjelinek 
3312865e09a4Sgjelinek 	if (grab_lock_file(target_zone, &lockfd) != Z_OK) {
3313865e09a4Sgjelinek 		zerror(gettext("another %s may have an operation in progress."),
3314865e09a4Sgjelinek 		    "zoneadm");
3315865e09a4Sgjelinek 		return (Z_ERR);
3316865e09a4Sgjelinek 	}
3317865e09a4Sgjelinek 
3318865e09a4Sgjelinek 	if ((err = zone_get_zonepath(source_zone, source_zonepath,
3319865e09a4Sgjelinek 	    sizeof (source_zonepath))) != Z_OK) {
3320865e09a4Sgjelinek 		errno = err;
3321865e09a4Sgjelinek 		zperror2(source_zone, gettext("could not get zone path"));
3322865e09a4Sgjelinek 		goto done;
3323865e09a4Sgjelinek 	}
3324865e09a4Sgjelinek 
3325865e09a4Sgjelinek 	if ((err = zone_get_zonepath(target_zone, zonepath, sizeof (zonepath)))
3326865e09a4Sgjelinek 	    != Z_OK) {
3327865e09a4Sgjelinek 		errno = err;
3328865e09a4Sgjelinek 		zperror2(target_zone, gettext("could not get zone path"));
3329865e09a4Sgjelinek 		goto done;
3330865e09a4Sgjelinek 	}
3331865e09a4Sgjelinek 
3332865e09a4Sgjelinek 	if ((err = zone_set_state(target_zone, ZONE_STATE_INCOMPLETE))
3333865e09a4Sgjelinek 	    != Z_OK) {
3334865e09a4Sgjelinek 		errno = err;
3335865e09a4Sgjelinek 		zperror2(target_zone, gettext("could not set state"));
3336865e09a4Sgjelinek 		goto done;
3337865e09a4Sgjelinek 	}
3338865e09a4Sgjelinek 
33390b5de56dSgjelinek 	if (snapshot != NULL) {
33400b5de56dSgjelinek 		err = clone_snapshot_zfs(snapshot, zonepath);
33410b5de56dSgjelinek 	} else {
33420b5de56dSgjelinek 		/*
33430b5de56dSgjelinek 		 * We always copy the clone unless the source is ZFS and a
33440b5de56dSgjelinek 		 * ZFS clone worked.  We fallback to copying if the ZFS clone
33450b5de56dSgjelinek 		 * fails for some reason.
33460b5de56dSgjelinek 		 */
33470b5de56dSgjelinek 		err = Z_ERR;
33480b5de56dSgjelinek 		if (method == NULL && is_zonepath_zfs(source_zonepath))
33490b5de56dSgjelinek 			err = clone_zfs(source_zone, source_zonepath, zonepath);
3350865e09a4Sgjelinek 
33515cd08338Sgjelinek 		if (err != Z_OK)
33520b5de56dSgjelinek 			err = clone_copy(source_zonepath, zonepath);
33530b5de56dSgjelinek 	}
3354865e09a4Sgjelinek 
33550b5de56dSgjelinek 	if (err == Z_OK)
33565cd08338Sgjelinek 		err = unconfigure_zone(zonepath);
3357865e09a4Sgjelinek 
3358865e09a4Sgjelinek done:
3359865e09a4Sgjelinek 	release_lock_file(lockfd);
3360865e09a4Sgjelinek 	return ((err == Z_OK) ? Z_OK : Z_ERR);
3361865e09a4Sgjelinek }
3362865e09a4Sgjelinek 
3363865e09a4Sgjelinek #define	RMCOMMAND	"/usr/bin/rm -rf"
3364865e09a4Sgjelinek 
336507b574eeSgjelinek /*
33660b5de56dSgjelinek  * Used when removing a zonepath after uninstalling or cleaning up after
33670b5de56dSgjelinek  * the move subcommand.  This handles a zonepath that has non-standard
33680b5de56dSgjelinek  * contents so that we will only cleanup the stuff we know about and leave
33690b5de56dSgjelinek  * any user data alone.
337007b574eeSgjelinek  *
33710b5de56dSgjelinek  * If the "all" parameter is true then we should remove the whole zonepath
33720b5de56dSgjelinek  * even if it has non-standard files/directories in it.  This can be used when
33730b5de56dSgjelinek  * we need to cleanup after moving the zonepath across file systems.
33740b5de56dSgjelinek  *
33750b5de56dSgjelinek  * We "exec" the RMCOMMAND so that the returned status is that of RMCOMMAND
33760b5de56dSgjelinek  * and not the shell.
337707b574eeSgjelinek  */
337807b574eeSgjelinek static int
33790b5de56dSgjelinek cleanup_zonepath(char *zonepath, boolean_t all)
338007b574eeSgjelinek {
338107b574eeSgjelinek 	int		status;
33820b5de56dSgjelinek 	int		i;
33830b5de56dSgjelinek 	boolean_t	non_std = B_FALSE;
33840b5de56dSgjelinek 	struct dirent	*dp;
33850b5de56dSgjelinek 	DIR		*dirp;
33860b5de56dSgjelinek 	char		*std_entries[] = {"dev", "lu", "root", NULL};
33870b5de56dSgjelinek 			/* (MAXPATHLEN * 3) is for the 3 std_entries dirs */
33880b5de56dSgjelinek 	char		cmdbuf[sizeof (RMCOMMAND) + (MAXPATHLEN * 3) + 64];
338907b574eeSgjelinek 
339007b574eeSgjelinek 	/*
33910b5de56dSgjelinek 	 * We shouldn't need these checks but lets be paranoid since we
33920b5de56dSgjelinek 	 * could blow away the whole system here if we got the wrong zonepath.
339307b574eeSgjelinek 	 */
33940b5de56dSgjelinek 	if (*zonepath == NULL || strcmp(zonepath, "/") == 0) {
33950b5de56dSgjelinek 		(void) fprintf(stderr, "invalid zonepath '%s'\n", zonepath);
33960b5de56dSgjelinek 		return (Z_INVAL);
33970b5de56dSgjelinek 	}
33980b5de56dSgjelinek 
339907b574eeSgjelinek 	/*
34000b5de56dSgjelinek 	 * If the dirpath is already gone (maybe it was manually removed) then
34010b5de56dSgjelinek 	 * we just return Z_OK so that the cleanup is successful.
340207b574eeSgjelinek 	 */
34030b5de56dSgjelinek 	if ((dirp = opendir(zonepath)) == NULL)
34040b5de56dSgjelinek 		return (Z_OK);
34050b5de56dSgjelinek 
34060b5de56dSgjelinek 	/*
34070b5de56dSgjelinek 	 * Look through the zonepath directory to see if there are any
34080b5de56dSgjelinek 	 * non-standard files/dirs.  Also skip .zfs since that might be
34090b5de56dSgjelinek 	 * there but we'll handle ZFS file systems as a special case.
34100b5de56dSgjelinek 	 */
34110b5de56dSgjelinek 	while ((dp = readdir(dirp)) != NULL) {
34120b5de56dSgjelinek 		if (strcmp(dp->d_name, ".") == 0 ||
34130b5de56dSgjelinek 		    strcmp(dp->d_name, "..") == 0 ||
34140b5de56dSgjelinek 		    strcmp(dp->d_name, ".zfs") == 0)
34150b5de56dSgjelinek 			continue;
34160b5de56dSgjelinek 
34170b5de56dSgjelinek 		for (i = 0; std_entries[i] != NULL; i++)
34180b5de56dSgjelinek 			if (strcmp(dp->d_name, std_entries[i]) == 0)
34190b5de56dSgjelinek 				break;
34200b5de56dSgjelinek 
34210b5de56dSgjelinek 		if (std_entries[i] == NULL)
34220b5de56dSgjelinek 			non_std = B_TRUE;
34230b5de56dSgjelinek 	}
34240b5de56dSgjelinek 	(void) closedir(dirp);
34250b5de56dSgjelinek 
34260b5de56dSgjelinek 	if (!all && non_std) {
34270b5de56dSgjelinek 		/*
34280b5de56dSgjelinek 		 * There are extra, non-standard directories/files in the
34290b5de56dSgjelinek 		 * zonepath so we don't want to remove the zonepath.  We
34300b5de56dSgjelinek 		 * just want to remove the standard directories and leave
34310b5de56dSgjelinek 		 * the user data alone.
34320b5de56dSgjelinek 		 */
34330b5de56dSgjelinek 		(void) snprintf(cmdbuf, sizeof (cmdbuf), "exec " RMCOMMAND);
34340b5de56dSgjelinek 
34350b5de56dSgjelinek 		for (i = 0; std_entries[i] != NULL; i++) {
34360b5de56dSgjelinek 			char tmpbuf[MAXPATHLEN];
34370b5de56dSgjelinek 
34380b5de56dSgjelinek 			if (snprintf(tmpbuf, sizeof (tmpbuf), " %s/%s",
34390b5de56dSgjelinek 			    zonepath, std_entries[i]) >= sizeof (tmpbuf) ||
34400b5de56dSgjelinek 			    strlcat(cmdbuf, tmpbuf, sizeof (cmdbuf)) >=
34410b5de56dSgjelinek 			    sizeof (cmdbuf)) {
34420b5de56dSgjelinek 				(void) fprintf(stderr,
34430b5de56dSgjelinek 				    gettext("path is too long\n"));
34440b5de56dSgjelinek 				return (Z_INVAL);
34450b5de56dSgjelinek 			}
344607b574eeSgjelinek 		}
344707b574eeSgjelinek 
344807b574eeSgjelinek 		status = do_subproc(cmdbuf);
344907b574eeSgjelinek 
34500b5de56dSgjelinek 		(void) fprintf(stderr, gettext("WARNING: Unable to completely "
34510b5de56dSgjelinek 		    "remove %s\nbecause it contains additional user data.  "
34520b5de56dSgjelinek 		    "Only the standard directory\nentries have been "
34530b5de56dSgjelinek 		    "removed.\n"),
34540b5de56dSgjelinek 		    zonepath);
345507b574eeSgjelinek 
34560b5de56dSgjelinek 		return (subproc_status(RMCOMMAND, status));
34570b5de56dSgjelinek 	}
34580b5de56dSgjelinek 
34590b5de56dSgjelinek 	/*
34600b5de56dSgjelinek 	 * There is nothing unexpected in the zonepath, try to get rid of the
34610b5de56dSgjelinek 	 * whole zonepath directory.
34620b5de56dSgjelinek 	 *
34630b5de56dSgjelinek 	 * If the zonepath is its own zfs file system, try to destroy the
34640b5de56dSgjelinek 	 * file system.  If that fails for some reason (e.g. it has clones)
34650b5de56dSgjelinek 	 * then we'll just remove the contents of the zonepath.
34660b5de56dSgjelinek 	 */
34670b5de56dSgjelinek 	if (is_zonepath_zfs(zonepath)) {
34680b5de56dSgjelinek 		if (destroy_zfs(zonepath) == Z_OK)
34690b5de56dSgjelinek 			return (Z_OK);
34700b5de56dSgjelinek 		(void) snprintf(cmdbuf, sizeof (cmdbuf), "exec " RMCOMMAND
34710b5de56dSgjelinek 		    " %s/*", zonepath);
34720b5de56dSgjelinek 		status = do_subproc(cmdbuf);
34730b5de56dSgjelinek 		return (subproc_status(RMCOMMAND, status));
34740b5de56dSgjelinek 	}
34750b5de56dSgjelinek 
34760b5de56dSgjelinek 	(void) snprintf(cmdbuf, sizeof (cmdbuf), "exec " RMCOMMAND " %s",
34770b5de56dSgjelinek 	    zonepath);
34780b5de56dSgjelinek 	status = do_subproc(cmdbuf);
34790b5de56dSgjelinek 	return (subproc_status(RMCOMMAND, status));
348007b574eeSgjelinek }
348107b574eeSgjelinek 
3482865e09a4Sgjelinek static int
3483865e09a4Sgjelinek move_func(int argc, char *argv[])
3484865e09a4Sgjelinek {
3485865e09a4Sgjelinek 	char *new_zonepath = NULL;
3486865e09a4Sgjelinek 	int lockfd;
3487865e09a4Sgjelinek 	int err, arg;
3488865e09a4Sgjelinek 	char zonepath[MAXPATHLEN];
3489865e09a4Sgjelinek 	zone_dochandle_t handle;
3490865e09a4Sgjelinek 	boolean_t fast;
34910b5de56dSgjelinek 	boolean_t is_zfs = B_FALSE;
34920b5de56dSgjelinek 	struct dirent *dp;
34930b5de56dSgjelinek 	DIR *dirp;
34940b5de56dSgjelinek 	boolean_t empty = B_TRUE;
3495865e09a4Sgjelinek 	boolean_t revert;
3496865e09a4Sgjelinek 	struct stat zonepath_buf;
3497865e09a4Sgjelinek 	struct stat new_zonepath_buf;
3498865e09a4Sgjelinek 
3499865e09a4Sgjelinek 	if (zonecfg_in_alt_root()) {
3500865e09a4Sgjelinek 		zerror(gettext("cannot move zone in alternate root"));
3501865e09a4Sgjelinek 		return (Z_ERR);
3502865e09a4Sgjelinek 	}
3503865e09a4Sgjelinek 
3504865e09a4Sgjelinek 	optind = 0;
3505865e09a4Sgjelinek 	if ((arg = getopt(argc, argv, "?")) != EOF) {
3506865e09a4Sgjelinek 		switch (arg) {
3507865e09a4Sgjelinek 		case '?':
3508865e09a4Sgjelinek 			sub_usage(SHELP_MOVE, CMD_MOVE);
3509865e09a4Sgjelinek 			return (optopt == '?' ? Z_OK : Z_USAGE);
3510865e09a4Sgjelinek 		default:
3511865e09a4Sgjelinek 			sub_usage(SHELP_MOVE, CMD_MOVE);
3512865e09a4Sgjelinek 			return (Z_USAGE);
3513865e09a4Sgjelinek 		}
3514865e09a4Sgjelinek 	}
3515865e09a4Sgjelinek 	if (argc != (optind + 1)) {
3516865e09a4Sgjelinek 		sub_usage(SHELP_MOVE, CMD_MOVE);
3517865e09a4Sgjelinek 		return (Z_USAGE);
3518865e09a4Sgjelinek 	}
3519865e09a4Sgjelinek 	new_zonepath = argv[optind];
3520865e09a4Sgjelinek 	if (sanity_check(target_zone, CMD_MOVE, B_FALSE, B_TRUE) != Z_OK)
3521865e09a4Sgjelinek 		return (Z_ERR);
3522865e09a4Sgjelinek 	if (verify_details(CMD_MOVE) != Z_OK)
3523865e09a4Sgjelinek 		return (Z_ERR);
3524865e09a4Sgjelinek 
3525865e09a4Sgjelinek 	/*
3526865e09a4Sgjelinek 	 * Check out the new zonepath.  This has the side effect of creating
3527865e09a4Sgjelinek 	 * a directory for the new zonepath.  We depend on this later when we
35280b5de56dSgjelinek 	 * stat to see if we are doing a cross file system move or not.
3529865e09a4Sgjelinek 	 */
3530865e09a4Sgjelinek 	if (validate_zonepath(new_zonepath, CMD_MOVE) != Z_OK)
3531865e09a4Sgjelinek 		return (Z_ERR);
3532865e09a4Sgjelinek 
3533865e09a4Sgjelinek 	if ((err = zone_get_zonepath(target_zone, zonepath, sizeof (zonepath)))
3534865e09a4Sgjelinek 	    != Z_OK) {
3535865e09a4Sgjelinek 		errno = err;
3536865e09a4Sgjelinek 		zperror2(target_zone, gettext("could not get zone path"));
3537865e09a4Sgjelinek 		return (Z_ERR);
3538865e09a4Sgjelinek 	}
3539865e09a4Sgjelinek 
3540865e09a4Sgjelinek 	if (stat(zonepath, &zonepath_buf) == -1) {
3541865e09a4Sgjelinek 		zperror(gettext("could not stat zone path"), B_FALSE);
3542865e09a4Sgjelinek 		return (Z_ERR);
3543865e09a4Sgjelinek 	}
3544865e09a4Sgjelinek 
3545865e09a4Sgjelinek 	if (stat(new_zonepath, &new_zonepath_buf) == -1) {
3546865e09a4Sgjelinek 		zperror(gettext("could not stat new zone path"), B_FALSE);
3547865e09a4Sgjelinek 		return (Z_ERR);
3548865e09a4Sgjelinek 	}
3549865e09a4Sgjelinek 
35500b5de56dSgjelinek 	/*
35510b5de56dSgjelinek 	 * Check if the destination directory is empty.
35520b5de56dSgjelinek 	 */
35530b5de56dSgjelinek 	if ((dirp = opendir(new_zonepath)) == NULL) {
35540b5de56dSgjelinek 		zperror(gettext("could not open new zone path"), B_FALSE);
35550b5de56dSgjelinek 		return (Z_ERR);
35560b5de56dSgjelinek 	}
35570b5de56dSgjelinek 	while ((dp = readdir(dirp)) != (struct dirent *)0) {
35580b5de56dSgjelinek 		if (strcmp(dp->d_name, ".") == 0 ||
35590b5de56dSgjelinek 		    strcmp(dp->d_name, "..") == 0)
35600b5de56dSgjelinek 			continue;
35610b5de56dSgjelinek 		empty = B_FALSE;
35620b5de56dSgjelinek 		break;
35630b5de56dSgjelinek 	}
35640b5de56dSgjelinek 	(void) closedir(dirp);
35650b5de56dSgjelinek 
35660b5de56dSgjelinek 	/* Error if there is anything in the destination directory. */
35670b5de56dSgjelinek 	if (!empty) {
35680b5de56dSgjelinek 		(void) fprintf(stderr, gettext("could not move zone to %s: "
35690b5de56dSgjelinek 		    "directory not empty\n"), new_zonepath);
35700b5de56dSgjelinek 		return (Z_ERR);
35710b5de56dSgjelinek 	}
35720b5de56dSgjelinek 
3573865e09a4Sgjelinek 	/* Don't move the zone if anything is still mounted there */
3574865e09a4Sgjelinek 	if (zonecfg_find_mounts(zonepath, NULL, NULL)) {
35750b5de56dSgjelinek 		zerror(gettext("These file systems are mounted on "
3576865e09a4Sgjelinek 		    "subdirectories of %s.\n"), zonepath);
3577865e09a4Sgjelinek 		(void) zonecfg_find_mounts(zonepath, zfm_print, NULL);
3578865e09a4Sgjelinek 		return (Z_ERR);
3579865e09a4Sgjelinek 	}
3580865e09a4Sgjelinek 
3581865e09a4Sgjelinek 	/*
3582865e09a4Sgjelinek 	 * Check if we are moving in the same file system and can do a fast
3583865e09a4Sgjelinek 	 * move or if we are crossing file systems and have to copy the data.
3584865e09a4Sgjelinek 	 */
3585865e09a4Sgjelinek 	fast = (zonepath_buf.st_dev == new_zonepath_buf.st_dev);
3586865e09a4Sgjelinek 
3587865e09a4Sgjelinek 	if ((handle = zonecfg_init_handle()) == NULL) {
3588865e09a4Sgjelinek 		zperror(cmd_to_str(CMD_MOVE), B_TRUE);
3589865e09a4Sgjelinek 		return (Z_ERR);
3590865e09a4Sgjelinek 	}
3591865e09a4Sgjelinek 
3592865e09a4Sgjelinek 	if ((err = zonecfg_get_handle(target_zone, handle)) != Z_OK) {
3593865e09a4Sgjelinek 		errno = err;
3594865e09a4Sgjelinek 		zperror(cmd_to_str(CMD_MOVE), B_TRUE);
3595865e09a4Sgjelinek 		zonecfg_fini_handle(handle);
3596865e09a4Sgjelinek 		return (Z_ERR);
3597865e09a4Sgjelinek 	}
3598865e09a4Sgjelinek 
3599865e09a4Sgjelinek 	if (grab_lock_file(target_zone, &lockfd) != Z_OK) {
3600865e09a4Sgjelinek 		zerror(gettext("another %s may have an operation in progress."),
3601865e09a4Sgjelinek 		    "zoneadm");
3602865e09a4Sgjelinek 		zonecfg_fini_handle(handle);
3603865e09a4Sgjelinek 		return (Z_ERR);
3604865e09a4Sgjelinek 	}
3605865e09a4Sgjelinek 
3606865e09a4Sgjelinek 	/*
36070b5de56dSgjelinek 	 * We're making some file system changes now so we have to clean up
36080b5de56dSgjelinek 	 * the file system before we are done.  This will either clean up the
3609865e09a4Sgjelinek 	 * new zonepath if the zonecfg update failed or it will clean up the
3610865e09a4Sgjelinek 	 * old zonepath if everything is ok.
3611865e09a4Sgjelinek 	 */
3612865e09a4Sgjelinek 	revert = B_TRUE;
3613865e09a4Sgjelinek 
36140b5de56dSgjelinek 	if (is_zonepath_zfs(zonepath) &&
36150b5de56dSgjelinek 	    move_zfs(zonepath, new_zonepath) != Z_ERR) {
36160b5de56dSgjelinek 		is_zfs = B_TRUE;
36170b5de56dSgjelinek 
36180b5de56dSgjelinek 	} else if (fast) {
3619865e09a4Sgjelinek 		/* same file system, use rename for a quick move */
3620865e09a4Sgjelinek 
3621865e09a4Sgjelinek 		/*
3622865e09a4Sgjelinek 		 * Remove the new_zonepath directory that got created above
3623865e09a4Sgjelinek 		 * during the validation.  It gets in the way of the rename.
3624865e09a4Sgjelinek 		 */
3625865e09a4Sgjelinek 		if (rmdir(new_zonepath) != 0) {
3626865e09a4Sgjelinek 			zperror(gettext("could not rmdir new zone path"),
3627865e09a4Sgjelinek 			    B_FALSE);
3628865e09a4Sgjelinek 			zonecfg_fini_handle(handle);
3629865e09a4Sgjelinek 			release_lock_file(lockfd);
3630865e09a4Sgjelinek 			return (Z_ERR);
3631865e09a4Sgjelinek 		}
3632865e09a4Sgjelinek 
3633865e09a4Sgjelinek 		if (rename(zonepath, new_zonepath) != 0) {
3634865e09a4Sgjelinek 			/*
3635865e09a4Sgjelinek 			 * If this fails we don't need to do all of the
3636865e09a4Sgjelinek 			 * cleanup that happens for the rest of the code
3637865e09a4Sgjelinek 			 * so just return from this error.
3638865e09a4Sgjelinek 			 */
3639865e09a4Sgjelinek 			zperror(gettext("could not move zone"), B_FALSE);
3640865e09a4Sgjelinek 			zonecfg_fini_handle(handle);
3641865e09a4Sgjelinek 			release_lock_file(lockfd);
3642865e09a4Sgjelinek 			return (Z_ERR);
3643865e09a4Sgjelinek 		}
3644865e09a4Sgjelinek 
3645865e09a4Sgjelinek 	} else {
36460b5de56dSgjelinek 		/*
36470b5de56dSgjelinek 		 * Attempt to create a ZFS fs for the new zonepath.  As usual,
36480b5de56dSgjelinek 		 * we don't care if this works or not since we always have the
36490b5de56dSgjelinek 		 * default behavior of a simple directory for the zonepath.
36500b5de56dSgjelinek 		 */
36510b5de56dSgjelinek 		create_zfs_zonepath(new_zonepath);
36520b5de56dSgjelinek 
3653865e09a4Sgjelinek 		(void) printf(gettext(
36540b5de56dSgjelinek 		    "Moving across file systems; copying zonepath %s..."),
3655865e09a4Sgjelinek 		    zonepath);
3656865e09a4Sgjelinek 		(void) fflush(stdout);
3657865e09a4Sgjelinek 
3658865e09a4Sgjelinek 		err = copy_zone(zonepath, new_zonepath);
3659865e09a4Sgjelinek 
3660865e09a4Sgjelinek 		(void) printf("\n");
3661865e09a4Sgjelinek 		if (err != Z_OK)
3662865e09a4Sgjelinek 			goto done;
3663865e09a4Sgjelinek 	}
3664865e09a4Sgjelinek 
3665865e09a4Sgjelinek 	if ((err = zonecfg_set_zonepath(handle, new_zonepath)) != Z_OK) {
3666865e09a4Sgjelinek 		errno = err;
3667865e09a4Sgjelinek 		zperror(gettext("could not set new zonepath"), B_TRUE);
3668865e09a4Sgjelinek 		goto done;
3669865e09a4Sgjelinek 	}
3670865e09a4Sgjelinek 
3671865e09a4Sgjelinek 	if ((err = zonecfg_save(handle)) != Z_OK) {
3672865e09a4Sgjelinek 		errno = err;
3673865e09a4Sgjelinek 		zperror(gettext("zonecfg save failed"), B_TRUE);
3674865e09a4Sgjelinek 		goto done;
3675865e09a4Sgjelinek 	}
3676865e09a4Sgjelinek 
3677865e09a4Sgjelinek 	revert = B_FALSE;
3678865e09a4Sgjelinek 
3679865e09a4Sgjelinek done:
3680865e09a4Sgjelinek 	zonecfg_fini_handle(handle);
3681865e09a4Sgjelinek 	release_lock_file(lockfd);
3682865e09a4Sgjelinek 
3683865e09a4Sgjelinek 	/*
36840b5de56dSgjelinek 	 * Clean up the file system based on how things went.  We either
3685865e09a4Sgjelinek 	 * clean up the new zonepath if the operation failed for some reason
3686865e09a4Sgjelinek 	 * or we clean up the old zonepath if everything is ok.
3687865e09a4Sgjelinek 	 */
3688865e09a4Sgjelinek 	if (revert) {
3689865e09a4Sgjelinek 		/* The zonecfg update failed, cleanup the new zonepath. */
36900b5de56dSgjelinek 		if (is_zfs) {
36910b5de56dSgjelinek 			if (move_zfs(new_zonepath, zonepath) == Z_ERR) {
36920b5de56dSgjelinek 				(void) fprintf(stderr, gettext("could not "
36930b5de56dSgjelinek 				    "restore zonepath, the zfs mountpoint is "
36940b5de56dSgjelinek 				    "set as:\n%s\n"), new_zonepath);
36950b5de56dSgjelinek 				/*
36960b5de56dSgjelinek 				 * err is already != Z_OK since we're reverting
36970b5de56dSgjelinek 				 */
36980b5de56dSgjelinek 			}
36990b5de56dSgjelinek 
37000b5de56dSgjelinek 		} else if (fast) {
3701865e09a4Sgjelinek 			if (rename(new_zonepath, zonepath) != 0) {
3702865e09a4Sgjelinek 				zperror(gettext("could not restore zonepath"),
3703865e09a4Sgjelinek 				    B_FALSE);
3704865e09a4Sgjelinek 				/*
3705865e09a4Sgjelinek 				 * err is already != Z_OK since we're reverting
3706865e09a4Sgjelinek 				 */
3707865e09a4Sgjelinek 			}
3708865e09a4Sgjelinek 		} else {
3709865e09a4Sgjelinek 			(void) printf(gettext("Cleaning up zonepath %s..."),
3710865e09a4Sgjelinek 			    new_zonepath);
3711865e09a4Sgjelinek 			(void) fflush(stdout);
37120b5de56dSgjelinek 			err = cleanup_zonepath(new_zonepath, B_TRUE);
3713865e09a4Sgjelinek 			(void) printf("\n");
3714865e09a4Sgjelinek 
371507b574eeSgjelinek 			if (err != Z_OK) {
3716865e09a4Sgjelinek 				errno = err;
3717865e09a4Sgjelinek 				zperror(gettext("could not remove new "
3718865e09a4Sgjelinek 				    "zonepath"), B_TRUE);
3719865e09a4Sgjelinek 			} else {
3720865e09a4Sgjelinek 				/*
3721865e09a4Sgjelinek 				 * Because we're reverting we know the mainline
3722865e09a4Sgjelinek 				 * code failed but we just reused the err
3723865e09a4Sgjelinek 				 * variable so we reset it back to Z_ERR.
3724865e09a4Sgjelinek 				 */
3725865e09a4Sgjelinek 				err = Z_ERR;
3726865e09a4Sgjelinek 			}
3727865e09a4Sgjelinek 		}
3728865e09a4Sgjelinek 
3729865e09a4Sgjelinek 	} else {
3730865e09a4Sgjelinek 		/* The move was successful, cleanup the old zonepath. */
37310b5de56dSgjelinek 		if (!is_zfs && !fast) {
3732865e09a4Sgjelinek 			(void) printf(
3733865e09a4Sgjelinek 			    gettext("Cleaning up zonepath %s..."), zonepath);
3734865e09a4Sgjelinek 			(void) fflush(stdout);
37350b5de56dSgjelinek 			err = cleanup_zonepath(zonepath, B_TRUE);
3736865e09a4Sgjelinek 			(void) printf("\n");
3737865e09a4Sgjelinek 
373807b574eeSgjelinek 			if (err != Z_OK) {
3739865e09a4Sgjelinek 				errno = err;
3740865e09a4Sgjelinek 				zperror(gettext("could not remove zonepath"),
3741865e09a4Sgjelinek 				    B_TRUE);
3742865e09a4Sgjelinek 			}
3743865e09a4Sgjelinek 		}
3744865e09a4Sgjelinek 	}
3745865e09a4Sgjelinek 
3746865e09a4Sgjelinek 	return ((err == Z_OK) ? Z_OK : Z_ERR);
3747865e09a4Sgjelinek }
3748865e09a4Sgjelinek 
3749ee519a1fSgjelinek static int
3750ee519a1fSgjelinek detach_func(int argc, char *argv[])
3751ee519a1fSgjelinek {
3752ee519a1fSgjelinek 	int lockfd;
3753ee519a1fSgjelinek 	int err, arg;
3754ee519a1fSgjelinek 	char zonepath[MAXPATHLEN];
3755ee519a1fSgjelinek 	zone_dochandle_t handle;
37568cd327d5Sgjelinek 	boolean_t execute = B_TRUE;
3757ee519a1fSgjelinek 
3758ee519a1fSgjelinek 	if (zonecfg_in_alt_root()) {
3759ee519a1fSgjelinek 		zerror(gettext("cannot detach zone in alternate root"));
3760ee519a1fSgjelinek 		return (Z_ERR);
3761ee519a1fSgjelinek 	}
3762ee519a1fSgjelinek 
3763ee519a1fSgjelinek 	optind = 0;
37648cd327d5Sgjelinek 	if ((arg = getopt(argc, argv, "?n")) != EOF) {
3765ee519a1fSgjelinek 		switch (arg) {
3766ee519a1fSgjelinek 		case '?':
3767ee519a1fSgjelinek 			sub_usage(SHELP_DETACH, CMD_DETACH);
3768ee519a1fSgjelinek 			return (optopt == '?' ? Z_OK : Z_USAGE);
37698cd327d5Sgjelinek 		case 'n':
37708cd327d5Sgjelinek 			execute = B_FALSE;
37718cd327d5Sgjelinek 			break;
3772ee519a1fSgjelinek 		default:
3773ee519a1fSgjelinek 			sub_usage(SHELP_DETACH, CMD_DETACH);
3774ee519a1fSgjelinek 			return (Z_USAGE);
3775ee519a1fSgjelinek 		}
3776ee519a1fSgjelinek 	}
37778cd327d5Sgjelinek 	if (execute) {
37788cd327d5Sgjelinek 		if (sanity_check(target_zone, CMD_DETACH, B_FALSE, B_TRUE)
37798cd327d5Sgjelinek 		    != Z_OK)
3780ee519a1fSgjelinek 			return (Z_ERR);
3781ee519a1fSgjelinek 		if (verify_details(CMD_DETACH) != Z_OK)
3782ee519a1fSgjelinek 			return (Z_ERR);
37838cd327d5Sgjelinek 	} else {
37848cd327d5Sgjelinek 		/*
37858cd327d5Sgjelinek 		 * We want a dry-run to work for a non-privileged user so we
37868cd327d5Sgjelinek 		 * only do minimal validation.
37878cd327d5Sgjelinek 		 */
37888cd327d5Sgjelinek 		if (getzoneid() != GLOBAL_ZONEID) {
37898cd327d5Sgjelinek 			zerror(gettext("must be in the global zone to %s a "
37908cd327d5Sgjelinek 			    "zone."), cmd_to_str(CMD_DETACH));
37918cd327d5Sgjelinek 			return (Z_ERR);
37928cd327d5Sgjelinek 		}
37938cd327d5Sgjelinek 
37948cd327d5Sgjelinek 		if (target_zone == NULL) {
37958cd327d5Sgjelinek 			zerror(gettext("no zone specified"));
37968cd327d5Sgjelinek 			return (Z_ERR);
37978cd327d5Sgjelinek 		}
37988cd327d5Sgjelinek 
37998cd327d5Sgjelinek 		if (strcmp(target_zone, GLOBAL_ZONENAME) == 0) {
38008cd327d5Sgjelinek 			zerror(gettext("%s operation is invalid for the "
38018cd327d5Sgjelinek 			    "global zone."), cmd_to_str(CMD_DETACH));
38028cd327d5Sgjelinek 			return (Z_ERR);
38038cd327d5Sgjelinek 		}
38048cd327d5Sgjelinek 	}
3805ee519a1fSgjelinek 
3806ee519a1fSgjelinek 	if ((err = zone_get_zonepath(target_zone, zonepath, sizeof (zonepath)))
3807ee519a1fSgjelinek 	    != Z_OK) {
3808ee519a1fSgjelinek 		errno = err;
3809ee519a1fSgjelinek 		zperror2(target_zone, gettext("could not get zone path"));
3810ee519a1fSgjelinek 		return (Z_ERR);
3811ee519a1fSgjelinek 	}
3812ee519a1fSgjelinek 
3813ee519a1fSgjelinek 	/* Don't detach the zone if anything is still mounted there */
38148cd327d5Sgjelinek 	if (execute && zonecfg_find_mounts(zonepath, NULL, NULL)) {
38150b5de56dSgjelinek 		zerror(gettext("These file systems are mounted on "
3816ee519a1fSgjelinek 		    "subdirectories of %s.\n"), zonepath);
3817ee519a1fSgjelinek 		(void) zonecfg_find_mounts(zonepath, zfm_print, NULL);
3818ee519a1fSgjelinek 		return (Z_ERR);
3819ee519a1fSgjelinek 	}
3820ee519a1fSgjelinek 
3821ee519a1fSgjelinek 	if ((handle = zonecfg_init_handle()) == NULL) {
3822ee519a1fSgjelinek 		zperror(cmd_to_str(CMD_DETACH), B_TRUE);
3823ee519a1fSgjelinek 		return (Z_ERR);
3824ee519a1fSgjelinek 	}
3825ee519a1fSgjelinek 
3826ee519a1fSgjelinek 	if ((err = zonecfg_get_handle(target_zone, handle)) != Z_OK) {
3827ee519a1fSgjelinek 		errno = err;
3828ee519a1fSgjelinek 		zperror(cmd_to_str(CMD_DETACH), B_TRUE);
3829ee519a1fSgjelinek 		zonecfg_fini_handle(handle);
3830ee519a1fSgjelinek 		return (Z_ERR);
3831ee519a1fSgjelinek 	}
3832ee519a1fSgjelinek 
38338cd327d5Sgjelinek 	if (execute && grab_lock_file(target_zone, &lockfd) != Z_OK) {
3834ee519a1fSgjelinek 		zerror(gettext("another %s may have an operation in progress."),
3835ee519a1fSgjelinek 		    "zoneadm");
3836ee519a1fSgjelinek 		zonecfg_fini_handle(handle);
3837ee519a1fSgjelinek 		return (Z_ERR);
3838ee519a1fSgjelinek 	}
3839ee519a1fSgjelinek 
3840ee519a1fSgjelinek 	if ((err = zonecfg_get_detach_info(handle, B_TRUE)) != Z_OK) {
3841ee519a1fSgjelinek 		errno = err;
3842ee519a1fSgjelinek 		zperror(gettext("getting the detach information failed"),
3843ee519a1fSgjelinek 		    B_TRUE);
3844ee519a1fSgjelinek 		goto done;
3845ee519a1fSgjelinek 	}
3846ee519a1fSgjelinek 
38478cd327d5Sgjelinek 	if ((err = zonecfg_detach_save(handle, (execute ? 0 : ZONE_DRY_RUN)))
38488cd327d5Sgjelinek 	    != Z_OK) {
3849ee519a1fSgjelinek 		errno = err;
3850ee519a1fSgjelinek 		zperror(gettext("saving the detach manifest failed"), B_TRUE);
3851ee519a1fSgjelinek 		goto done;
3852ee519a1fSgjelinek 	}
3853ee519a1fSgjelinek 
38548cd327d5Sgjelinek 	/*
38558cd327d5Sgjelinek 	 * Set the zone state back to configured unless we are running with the
38568cd327d5Sgjelinek 	 * no-execute option.
38578cd327d5Sgjelinek 	 */
38588cd327d5Sgjelinek 	if (execute && (err = zone_set_state(target_zone,
38598cd327d5Sgjelinek 	    ZONE_STATE_CONFIGURED)) != Z_OK) {
3860ee519a1fSgjelinek 		errno = err;
3861ee519a1fSgjelinek 		zperror(gettext("could not reset state"), B_TRUE);
3862ee519a1fSgjelinek 	}
3863ee519a1fSgjelinek 
3864ee519a1fSgjelinek done:
3865ee519a1fSgjelinek 	zonecfg_fini_handle(handle);
38668cd327d5Sgjelinek 	if (execute)
3867ee519a1fSgjelinek 		release_lock_file(lockfd);
3868ee519a1fSgjelinek 
3869ee519a1fSgjelinek 	return ((err == Z_OK) ? Z_OK : Z_ERR);
3870ee519a1fSgjelinek }
3871ee519a1fSgjelinek 
3872ee519a1fSgjelinek /*
3873ee519a1fSgjelinek  * During attach we go through and fix up the /dev entries for the zone
3874ee519a1fSgjelinek  * we are attaching.  In order to regenerate /dev with the correct devices,
3875ee519a1fSgjelinek  * the old /dev will be removed, the zone readied (which generates a new
3876ee519a1fSgjelinek  * /dev) then halted, then we use the info from the manifest to update
3877ee519a1fSgjelinek  * the modes, owners, etc. on the new /dev.
3878ee519a1fSgjelinek  */
3879ee519a1fSgjelinek static int
3880ee519a1fSgjelinek dev_fix(zone_dochandle_t handle)
3881ee519a1fSgjelinek {
3882ee519a1fSgjelinek 	int			res;
3883ee519a1fSgjelinek 	int			err;
3884ee519a1fSgjelinek 	int			status;
3885ee519a1fSgjelinek 	struct zone_devpermtab	devtab;
3886ee519a1fSgjelinek 	zone_cmd_arg_t		zarg;
3887ee519a1fSgjelinek 	char			devpath[MAXPATHLEN];
3888ee519a1fSgjelinek 				/* 6: "exec " and " " */
3889ee519a1fSgjelinek 	char			cmdbuf[sizeof (RMCOMMAND) + MAXPATHLEN + 6];
3890ee519a1fSgjelinek 
3891ee519a1fSgjelinek 	if ((res = zonecfg_get_zonepath(handle, devpath, sizeof (devpath)))
3892ee519a1fSgjelinek 	    != Z_OK)
3893ee519a1fSgjelinek 		return (res);
3894ee519a1fSgjelinek 
3895ee519a1fSgjelinek 	if (strlcat(devpath, "/dev", sizeof (devpath)) >= sizeof (devpath))
3896ee519a1fSgjelinek 		return (Z_TOO_BIG);
3897ee519a1fSgjelinek 
3898ee519a1fSgjelinek 	/*
3899ee519a1fSgjelinek 	 * "exec" the command so that the returned status is that of
3900ee519a1fSgjelinek 	 * RMCOMMAND and not the shell.
3901ee519a1fSgjelinek 	 */
3902ee519a1fSgjelinek 	(void) snprintf(cmdbuf, sizeof (cmdbuf), "exec " RMCOMMAND " %s",
3903ee519a1fSgjelinek 	    devpath);
3904ee519a1fSgjelinek 	status = do_subproc(cmdbuf);
3905ee519a1fSgjelinek 	if ((err = subproc_status(RMCOMMAND, status)) != Z_OK) {
3906ee519a1fSgjelinek 		(void) fprintf(stderr,
3907ee519a1fSgjelinek 		    gettext("could not remove existing /dev\n"));
3908ee519a1fSgjelinek 		return (Z_ERR);
3909ee519a1fSgjelinek 	}
3910ee519a1fSgjelinek 
3911ee519a1fSgjelinek 	/* In order to ready the zone, it must be in the installed state */
3912ee519a1fSgjelinek 	if ((err = zone_set_state(target_zone, ZONE_STATE_INSTALLED)) != Z_OK) {
3913ee519a1fSgjelinek 		errno = err;
3914ee519a1fSgjelinek 		zperror(gettext("could not reset state"), B_TRUE);
3915ee519a1fSgjelinek 		return (Z_ERR);
3916ee519a1fSgjelinek 	}
3917ee519a1fSgjelinek 
3918ee519a1fSgjelinek 	/* We have to ready the zone to regen the dev tree */
3919ee519a1fSgjelinek 	zarg.cmd = Z_READY;
3920ee519a1fSgjelinek 	if (call_zoneadmd(target_zone, &zarg) != 0) {
3921ee519a1fSgjelinek 		zerror(gettext("call to %s failed"), "zoneadmd");
3922ee519a1fSgjelinek 		return (Z_ERR);
3923ee519a1fSgjelinek 	}
3924ee519a1fSgjelinek 
3925ee519a1fSgjelinek 	zarg.cmd = Z_HALT;
3926ee519a1fSgjelinek 	if (call_zoneadmd(target_zone, &zarg) != 0) {
3927ee519a1fSgjelinek 		zerror(gettext("call to %s failed"), "zoneadmd");
3928ee519a1fSgjelinek 		return (Z_ERR);
3929ee519a1fSgjelinek 	}
3930ee519a1fSgjelinek 
3931ee519a1fSgjelinek 	if (zonecfg_setdevperment(handle) != Z_OK) {
3932ee519a1fSgjelinek 		(void) fprintf(stderr,
3933ee519a1fSgjelinek 		    gettext("unable to enumerate device entries\n"));
3934ee519a1fSgjelinek 		return (Z_ERR);
3935ee519a1fSgjelinek 	}
3936ee519a1fSgjelinek 
3937ee519a1fSgjelinek 	while (zonecfg_getdevperment(handle, &devtab) == Z_OK) {
3938ee519a1fSgjelinek 		int err;
3939ee519a1fSgjelinek 
3940ee519a1fSgjelinek 		if ((err = zonecfg_devperms_apply(handle,
3941ee519a1fSgjelinek 		    devtab.zone_devperm_name, devtab.zone_devperm_uid,
3942ee519a1fSgjelinek 		    devtab.zone_devperm_gid, devtab.zone_devperm_mode,
3943ee519a1fSgjelinek 		    devtab.zone_devperm_acl)) != Z_OK && err != Z_INVAL)
3944ee519a1fSgjelinek 			(void) fprintf(stderr, gettext("error updating device "
3945ee519a1fSgjelinek 			    "%s: %s\n"), devtab.zone_devperm_name,
3946ee519a1fSgjelinek 			    zonecfg_strerror(err));
3947ee519a1fSgjelinek 
3948ee519a1fSgjelinek 		free(devtab.zone_devperm_acl);
3949ee519a1fSgjelinek 	}
3950ee519a1fSgjelinek 
3951ee519a1fSgjelinek 	(void) zonecfg_enddevperment(handle);
3952ee519a1fSgjelinek 
3953ee519a1fSgjelinek 	return (Z_OK);
3954ee519a1fSgjelinek }
3955ee519a1fSgjelinek 
39568cd327d5Sgjelinek /*
39578cd327d5Sgjelinek  * Validate attaching a zone but don't actually do the work.  The zone
39588cd327d5Sgjelinek  * does not have to exist, so there is some complexity getting a new zone
39598cd327d5Sgjelinek  * configuration set up so that we can perform the validation.  This is
39608cd327d5Sgjelinek  * handled within zonecfg_attach_manifest() which returns two handles; one
39618cd327d5Sgjelinek  * for the the full configuration to validate (rem_handle) and the other
39628cd327d5Sgjelinek  * (local_handle) containing only the zone configuration derived from the
39638cd327d5Sgjelinek  * manifest.
39648cd327d5Sgjelinek  */
39658cd327d5Sgjelinek static int
39668cd327d5Sgjelinek dryrun_attach(char *manifest_path)
39678cd327d5Sgjelinek {
39688cd327d5Sgjelinek 	int fd;
39698cd327d5Sgjelinek 	int err;
39708cd327d5Sgjelinek 	int res;
39718cd327d5Sgjelinek 	zone_dochandle_t local_handle;
39728cd327d5Sgjelinek 	zone_dochandle_t rem_handle = NULL;
39738cd327d5Sgjelinek 
39748cd327d5Sgjelinek 	if (strcmp(manifest_path, "-") == 0) {
39758cd327d5Sgjelinek 		fd = 0;
39768cd327d5Sgjelinek 	} else if ((fd = open(manifest_path, O_RDONLY)) < 0) {
39778cd327d5Sgjelinek 		zperror(gettext("could not open manifest path"), B_FALSE);
39788cd327d5Sgjelinek 		return (Z_ERR);
39798cd327d5Sgjelinek 	}
39808cd327d5Sgjelinek 
39818cd327d5Sgjelinek 	if ((local_handle = zonecfg_init_handle()) == NULL) {
39828cd327d5Sgjelinek 		zperror(cmd_to_str(CMD_ATTACH), B_TRUE);
39838cd327d5Sgjelinek 		res = Z_ERR;
39848cd327d5Sgjelinek 		goto done;
39858cd327d5Sgjelinek 	}
39868cd327d5Sgjelinek 
39878cd327d5Sgjelinek 	if ((rem_handle = zonecfg_init_handle()) == NULL) {
39888cd327d5Sgjelinek 		zperror(cmd_to_str(CMD_ATTACH), B_TRUE);
39898cd327d5Sgjelinek 		res = Z_ERR;
39908cd327d5Sgjelinek 		goto done;
39918cd327d5Sgjelinek 	}
39928cd327d5Sgjelinek 
39938cd327d5Sgjelinek 	if ((err = zonecfg_attach_manifest(fd, local_handle, rem_handle))
39948cd327d5Sgjelinek 	    != Z_OK) {
39958cd327d5Sgjelinek 		if (err == Z_INVALID_DOCUMENT)
39968cd327d5Sgjelinek 			zerror(gettext("Cannot attach to an earlier release "
39978cd327d5Sgjelinek 			    "of the operating system"));
39988cd327d5Sgjelinek 		else
39998cd327d5Sgjelinek 			zperror(cmd_to_str(CMD_ATTACH), B_TRUE);
40008cd327d5Sgjelinek 		res = Z_ERR;
40018cd327d5Sgjelinek 		goto done;
40028cd327d5Sgjelinek 	}
40038cd327d5Sgjelinek 
40048cd327d5Sgjelinek 	res = verify_handle(CMD_ATTACH, local_handle);
40058cd327d5Sgjelinek 
40068cd327d5Sgjelinek 	/* Get the detach information for the locally defined zone. */
40078cd327d5Sgjelinek 	if ((err = zonecfg_get_detach_info(local_handle, B_FALSE)) != Z_OK) {
40088cd327d5Sgjelinek 		errno = err;
40098cd327d5Sgjelinek 		zperror(gettext("getting the attach information failed"),
40108cd327d5Sgjelinek 		    B_TRUE);
40118cd327d5Sgjelinek 		res = Z_ERR;
40128cd327d5Sgjelinek 	} else {
40138cd327d5Sgjelinek 		/* sw_cmp prints error msgs as necessary */
40148cd327d5Sgjelinek 		if (sw_cmp(local_handle, rem_handle, SW_CMP_NONE) != Z_OK)
40158cd327d5Sgjelinek 			res = Z_ERR;
40168cd327d5Sgjelinek 	}
40178cd327d5Sgjelinek 
40188cd327d5Sgjelinek done:
40198cd327d5Sgjelinek 	if (strcmp(manifest_path, "-") != 0)
40208cd327d5Sgjelinek 		(void) close(fd);
40218cd327d5Sgjelinek 
40228cd327d5Sgjelinek 	zonecfg_fini_handle(local_handle);
40238cd327d5Sgjelinek 	zonecfg_fini_handle(rem_handle);
40248cd327d5Sgjelinek 
40258cd327d5Sgjelinek 	return ((res == Z_OK) ? Z_OK : Z_ERR);
40268cd327d5Sgjelinek }
40278cd327d5Sgjelinek 
4028ee519a1fSgjelinek static int
4029ee519a1fSgjelinek attach_func(int argc, char *argv[])
4030ee519a1fSgjelinek {
4031ee519a1fSgjelinek 	int lockfd;
4032ee519a1fSgjelinek 	int err, arg;
4033ee519a1fSgjelinek 	boolean_t force = B_FALSE;
4034ee519a1fSgjelinek 	zone_dochandle_t handle;
4035ee519a1fSgjelinek 	zone_dochandle_t athandle = NULL;
4036ee519a1fSgjelinek 	char zonepath[MAXPATHLEN];
40378cd327d5Sgjelinek 	boolean_t execute = B_TRUE;
40388cd327d5Sgjelinek 	char *manifest_path;
4039ee519a1fSgjelinek 
4040ee519a1fSgjelinek 	if (zonecfg_in_alt_root()) {
4041ee519a1fSgjelinek 		zerror(gettext("cannot attach zone in alternate root"));
4042ee519a1fSgjelinek 		return (Z_ERR);
4043ee519a1fSgjelinek 	}
4044ee519a1fSgjelinek 
4045ee519a1fSgjelinek 	optind = 0;
40468cd327d5Sgjelinek 	if ((arg = getopt(argc, argv, "?Fn:")) != EOF) {
4047ee519a1fSgjelinek 		switch (arg) {
4048ee519a1fSgjelinek 		case '?':
4049ee519a1fSgjelinek 			sub_usage(SHELP_ATTACH, CMD_ATTACH);
4050ee519a1fSgjelinek 			return (optopt == '?' ? Z_OK : Z_USAGE);
4051ee519a1fSgjelinek 		case 'F':
4052ee519a1fSgjelinek 			force = B_TRUE;
4053ee519a1fSgjelinek 			break;
40548cd327d5Sgjelinek 		case 'n':
40558cd327d5Sgjelinek 			execute = B_FALSE;
40568cd327d5Sgjelinek 			manifest_path = optarg;
40578cd327d5Sgjelinek 			break;
4058ee519a1fSgjelinek 		default:
4059ee519a1fSgjelinek 			sub_usage(SHELP_ATTACH, CMD_ATTACH);
4060ee519a1fSgjelinek 			return (Z_USAGE);
4061ee519a1fSgjelinek 		}
4062ee519a1fSgjelinek 	}
40638cd327d5Sgjelinek 
40648cd327d5Sgjelinek 	/*
40658cd327d5Sgjelinek 	 * If the no-execute option was specified, we need to branch down
40668cd327d5Sgjelinek 	 * a completely different path since there is no zone required to be
40678cd327d5Sgjelinek 	 * configured for this option.
40688cd327d5Sgjelinek 	 */
40698cd327d5Sgjelinek 	if (!execute)
40708cd327d5Sgjelinek 		return (dryrun_attach(manifest_path));
40718cd327d5Sgjelinek 
4072ee519a1fSgjelinek 	if (sanity_check(target_zone, CMD_ATTACH, B_FALSE, B_TRUE) != Z_OK)
4073ee519a1fSgjelinek 		return (Z_ERR);
4074ee519a1fSgjelinek 	if (verify_details(CMD_ATTACH) != Z_OK)
4075ee519a1fSgjelinek 		return (Z_ERR);
4076ee519a1fSgjelinek 
4077ee519a1fSgjelinek 	if ((err = zone_get_zonepath(target_zone, zonepath, sizeof (zonepath)))
4078ee519a1fSgjelinek 	    != Z_OK) {
4079ee519a1fSgjelinek 		errno = err;
4080ee519a1fSgjelinek 		zperror2(target_zone, gettext("could not get zone path"));
4081ee519a1fSgjelinek 		return (Z_ERR);
4082ee519a1fSgjelinek 	}
4083ee519a1fSgjelinek 
4084ee519a1fSgjelinek 	if ((handle = zonecfg_init_handle()) == NULL) {
4085ee519a1fSgjelinek 		zperror(cmd_to_str(CMD_ATTACH), B_TRUE);
4086ee519a1fSgjelinek 		return (Z_ERR);
4087ee519a1fSgjelinek 	}
4088ee519a1fSgjelinek 
4089ee519a1fSgjelinek 	if ((err = zonecfg_get_handle(target_zone, handle)) != Z_OK) {
4090ee519a1fSgjelinek 		errno = err;
4091ee519a1fSgjelinek 		zperror(cmd_to_str(CMD_ATTACH), B_TRUE);
4092ee519a1fSgjelinek 		zonecfg_fini_handle(handle);
4093ee519a1fSgjelinek 		return (Z_ERR);
4094ee519a1fSgjelinek 	}
4095ee519a1fSgjelinek 
4096ee519a1fSgjelinek 	if (grab_lock_file(target_zone, &lockfd) != Z_OK) {
4097ee519a1fSgjelinek 		zerror(gettext("another %s may have an operation in progress."),
4098ee519a1fSgjelinek 		    "zoneadm");
4099ee519a1fSgjelinek 		zonecfg_fini_handle(handle);
4100ee519a1fSgjelinek 		return (Z_ERR);
4101ee519a1fSgjelinek 	}
4102ee519a1fSgjelinek 
4103ee519a1fSgjelinek 	if (force)
4104ee519a1fSgjelinek 		goto forced;
4105ee519a1fSgjelinek 
4106ee519a1fSgjelinek 	if ((athandle = zonecfg_init_handle()) == NULL) {
4107ee519a1fSgjelinek 		zperror(cmd_to_str(CMD_ATTACH), B_TRUE);
4108ee519a1fSgjelinek 		goto done;
4109ee519a1fSgjelinek 	}
4110ee519a1fSgjelinek 
4111ee519a1fSgjelinek 	if ((err = zonecfg_get_attach_handle(zonepath, target_zone, B_TRUE,
4112ee519a1fSgjelinek 	    athandle)) != Z_OK) {
4113ee519a1fSgjelinek 		if (err == Z_NO_ZONE)
4114ee519a1fSgjelinek 			zerror(gettext("Not a detached zone"));
4115ee519a1fSgjelinek 		else if (err == Z_INVALID_DOCUMENT)
4116ee519a1fSgjelinek 			zerror(gettext("Cannot attach to an earlier release "
4117ee519a1fSgjelinek 			    "of the operating system"));
4118ee519a1fSgjelinek 		else
4119ee519a1fSgjelinek 			zperror(cmd_to_str(CMD_ATTACH), B_TRUE);
4120ee519a1fSgjelinek 		goto done;
4121ee519a1fSgjelinek 	}
4122ee519a1fSgjelinek 
4123ee519a1fSgjelinek 	/* Get the detach information for the locally defined zone. */
4124ee519a1fSgjelinek 	if ((err = zonecfg_get_detach_info(handle, B_FALSE)) != Z_OK) {
4125ee519a1fSgjelinek 		errno = err;
4126ee519a1fSgjelinek 		zperror(gettext("getting the attach information failed"),
4127ee519a1fSgjelinek 		    B_TRUE);
4128ee519a1fSgjelinek 		goto done;
4129ee519a1fSgjelinek 	}
4130ee519a1fSgjelinek 
4131ee519a1fSgjelinek 	/* sw_cmp prints error msgs as necessary */
41320b5de56dSgjelinek 	if ((err = sw_cmp(handle, athandle, SW_CMP_NONE)) != Z_OK)
4133ee519a1fSgjelinek 		goto done;
4134ee519a1fSgjelinek 
4135ee519a1fSgjelinek 	if ((err = dev_fix(athandle)) != Z_OK)
4136ee519a1fSgjelinek 		goto done;
4137ee519a1fSgjelinek 
4138ee519a1fSgjelinek forced:
4139ee519a1fSgjelinek 
4140ee519a1fSgjelinek 	zonecfg_rm_detached(handle, force);
4141ee519a1fSgjelinek 
4142ee519a1fSgjelinek 	if ((err = zone_set_state(target_zone, ZONE_STATE_INSTALLED)) != Z_OK) {
4143ee519a1fSgjelinek 		errno = err;
4144ee519a1fSgjelinek 		zperror(gettext("could not reset state"), B_TRUE);
4145ee519a1fSgjelinek 	}
4146ee519a1fSgjelinek 
4147ee519a1fSgjelinek done:
4148ee519a1fSgjelinek 	zonecfg_fini_handle(handle);
4149ee519a1fSgjelinek 	release_lock_file(lockfd);
4150ee519a1fSgjelinek 	if (athandle != NULL)
4151ee519a1fSgjelinek 		zonecfg_fini_handle(athandle);
4152ee519a1fSgjelinek 
4153ee519a1fSgjelinek 	return ((err == Z_OK) ? Z_OK : Z_ERR);
4154ee519a1fSgjelinek }
4155ee519a1fSgjelinek 
4156865e09a4Sgjelinek /*
41577c478bd9Sstevel@tonic-gate  * On input, TRUE => yes, FALSE => no.
41587c478bd9Sstevel@tonic-gate  * On return, TRUE => 1, FALSE => 0, could not ask => -1.
41597c478bd9Sstevel@tonic-gate  */
41607c478bd9Sstevel@tonic-gate 
41617c478bd9Sstevel@tonic-gate static int
41627c478bd9Sstevel@tonic-gate ask_yesno(boolean_t default_answer, const char *question)
41637c478bd9Sstevel@tonic-gate {
41647c478bd9Sstevel@tonic-gate 	char line[64];	/* should be large enough to answer yes or no */
41657c478bd9Sstevel@tonic-gate 
41667c478bd9Sstevel@tonic-gate 	if (!isatty(STDIN_FILENO))
41677c478bd9Sstevel@tonic-gate 		return (-1);
41687c478bd9Sstevel@tonic-gate 	for (;;) {
41697c478bd9Sstevel@tonic-gate 		(void) printf("%s (%s)? ", question,
41707c478bd9Sstevel@tonic-gate 		    default_answer ? "[y]/n" : "y/[n]");
41717c478bd9Sstevel@tonic-gate 		if (fgets(line, sizeof (line), stdin) == NULL ||
41727c478bd9Sstevel@tonic-gate 		    line[0] == '\n')
41737c478bd9Sstevel@tonic-gate 			return (default_answer ? 1 : 0);
41747c478bd9Sstevel@tonic-gate 		if (tolower(line[0]) == 'y')
41757c478bd9Sstevel@tonic-gate 			return (1);
41767c478bd9Sstevel@tonic-gate 		if (tolower(line[0]) == 'n')
41777c478bd9Sstevel@tonic-gate 			return (0);
41787c478bd9Sstevel@tonic-gate 	}
41797c478bd9Sstevel@tonic-gate }
41807c478bd9Sstevel@tonic-gate 
41817c478bd9Sstevel@tonic-gate static int
41827c478bd9Sstevel@tonic-gate uninstall_func(int argc, char *argv[])
41837c478bd9Sstevel@tonic-gate {
41847c478bd9Sstevel@tonic-gate 	char line[ZONENAME_MAX + 128];	/* Enough for "Are you sure ..." */
41850b5de56dSgjelinek 	char rootpath[MAXPATHLEN], zonepath[MAXPATHLEN];
41867c478bd9Sstevel@tonic-gate 	boolean_t force = B_FALSE;
41877c478bd9Sstevel@tonic-gate 	int lockfd, answer;
41887c478bd9Sstevel@tonic-gate 	int err, arg;
41897c478bd9Sstevel@tonic-gate 
4190108322fbScarlsonj 	if (zonecfg_in_alt_root()) {
4191108322fbScarlsonj 		zerror(gettext("cannot uninstall zone in alternate root"));
4192108322fbScarlsonj 		return (Z_ERR);
4193108322fbScarlsonj 	}
4194108322fbScarlsonj 
41957c478bd9Sstevel@tonic-gate 	optind = 0;
41967c478bd9Sstevel@tonic-gate 	while ((arg = getopt(argc, argv, "?F")) != EOF) {
41977c478bd9Sstevel@tonic-gate 		switch (arg) {
41987c478bd9Sstevel@tonic-gate 		case '?':
41997c478bd9Sstevel@tonic-gate 			sub_usage(SHELP_UNINSTALL, CMD_UNINSTALL);
42007c478bd9Sstevel@tonic-gate 			return (optopt == '?' ? Z_OK : Z_USAGE);
42017c478bd9Sstevel@tonic-gate 		case 'F':
42027c478bd9Sstevel@tonic-gate 			force = B_TRUE;
42037c478bd9Sstevel@tonic-gate 			break;
42047c478bd9Sstevel@tonic-gate 		default:
42057c478bd9Sstevel@tonic-gate 			sub_usage(SHELP_UNINSTALL, CMD_UNINSTALL);
42067c478bd9Sstevel@tonic-gate 			return (Z_USAGE);
42077c478bd9Sstevel@tonic-gate 		}
42087c478bd9Sstevel@tonic-gate 	}
42097c478bd9Sstevel@tonic-gate 	if (argc > optind) {
42107c478bd9Sstevel@tonic-gate 		sub_usage(SHELP_UNINSTALL, CMD_UNINSTALL);
42117c478bd9Sstevel@tonic-gate 		return (Z_USAGE);
42127c478bd9Sstevel@tonic-gate 	}
42137c478bd9Sstevel@tonic-gate 
42147c478bd9Sstevel@tonic-gate 	if (sanity_check(target_zone, CMD_UNINSTALL, B_FALSE, B_TRUE) != Z_OK)
42157c478bd9Sstevel@tonic-gate 		return (Z_ERR);
42167c478bd9Sstevel@tonic-gate 
42177c478bd9Sstevel@tonic-gate 	if (!force) {
42187c478bd9Sstevel@tonic-gate 		(void) snprintf(line, sizeof (line),
42197c478bd9Sstevel@tonic-gate 		    gettext("Are you sure you want to %s zone %s"),
42207c478bd9Sstevel@tonic-gate 		    cmd_to_str(CMD_UNINSTALL), target_zone);
42217c478bd9Sstevel@tonic-gate 		if ((answer = ask_yesno(B_FALSE, line)) == 0) {
42227c478bd9Sstevel@tonic-gate 			return (Z_OK);
42237c478bd9Sstevel@tonic-gate 		} else if (answer == -1) {
42247c478bd9Sstevel@tonic-gate 			zerror(gettext("Input not from terminal and -F "
42257c478bd9Sstevel@tonic-gate 			    "not specified: %s not done."),
42267c478bd9Sstevel@tonic-gate 			    cmd_to_str(CMD_UNINSTALL));
42277c478bd9Sstevel@tonic-gate 			return (Z_ERR);
42287c478bd9Sstevel@tonic-gate 		}
42297c478bd9Sstevel@tonic-gate 	}
42307c478bd9Sstevel@tonic-gate 
42310b5de56dSgjelinek 	if ((err = zone_get_zonepath(target_zone, zonepath,
42320b5de56dSgjelinek 	    sizeof (zonepath))) != Z_OK) {
42337c478bd9Sstevel@tonic-gate 		errno = err;
42347c478bd9Sstevel@tonic-gate 		zperror2(target_zone, gettext("could not get zone path"));
42357c478bd9Sstevel@tonic-gate 		return (Z_ERR);
42367c478bd9Sstevel@tonic-gate 	}
42377c478bd9Sstevel@tonic-gate 	if ((err = zone_get_rootpath(target_zone, rootpath,
42387c478bd9Sstevel@tonic-gate 	    sizeof (rootpath))) != Z_OK) {
42397c478bd9Sstevel@tonic-gate 		errno = err;
42407c478bd9Sstevel@tonic-gate 		zperror2(target_zone, gettext("could not get root path"));
42417c478bd9Sstevel@tonic-gate 		return (Z_ERR);
42427c478bd9Sstevel@tonic-gate 	}
42437c478bd9Sstevel@tonic-gate 
42447c478bd9Sstevel@tonic-gate 	/*
42457c478bd9Sstevel@tonic-gate 	 * If there seems to be a zoneadmd running for this zone, call it
42467c478bd9Sstevel@tonic-gate 	 * to tell it that an uninstall is happening; if all goes well it
42477c478bd9Sstevel@tonic-gate 	 * will then shut itself down.
42487c478bd9Sstevel@tonic-gate 	 */
42497c478bd9Sstevel@tonic-gate 	if (ping_zoneadmd(target_zone) == Z_OK) {
42507c478bd9Sstevel@tonic-gate 		zone_cmd_arg_t zarg;
42517c478bd9Sstevel@tonic-gate 		zarg.cmd = Z_NOTE_UNINSTALLING;
42527c478bd9Sstevel@tonic-gate 		/* we don't care too much if this fails... just plow on */
42537c478bd9Sstevel@tonic-gate 		(void) call_zoneadmd(target_zone, &zarg);
42547c478bd9Sstevel@tonic-gate 	}
42557c478bd9Sstevel@tonic-gate 
42567c478bd9Sstevel@tonic-gate 	if (grab_lock_file(target_zone, &lockfd) != Z_OK) {
42577c478bd9Sstevel@tonic-gate 		zerror(gettext("another %s may have an operation in progress."),
4258865e09a4Sgjelinek 		    "zoneadm");
42597c478bd9Sstevel@tonic-gate 		return (Z_ERR);
42607c478bd9Sstevel@tonic-gate 	}
42617c478bd9Sstevel@tonic-gate 
42627c478bd9Sstevel@tonic-gate 	/* Don't uninstall the zone if anything is mounted there */
42637c478bd9Sstevel@tonic-gate 	err = zonecfg_find_mounts(rootpath, NULL, NULL);
42647c478bd9Sstevel@tonic-gate 	if (err) {
42650b5de56dSgjelinek 		zerror(gettext("These file systems are mounted on "
42667c478bd9Sstevel@tonic-gate 		    "subdirectories of %s.\n"), rootpath);
42677c478bd9Sstevel@tonic-gate 		(void) zonecfg_find_mounts(rootpath, zfm_print, NULL);
42687c478bd9Sstevel@tonic-gate 		return (Z_ERR);
42697c478bd9Sstevel@tonic-gate 	}
42707c478bd9Sstevel@tonic-gate 
42717c478bd9Sstevel@tonic-gate 	err = zone_set_state(target_zone, ZONE_STATE_INCOMPLETE);
42727c478bd9Sstevel@tonic-gate 	if (err != Z_OK) {
42737c478bd9Sstevel@tonic-gate 		errno = err;
42747c478bd9Sstevel@tonic-gate 		zperror2(target_zone, gettext("could not set state"));
42757c478bd9Sstevel@tonic-gate 		goto bad;
42767c478bd9Sstevel@tonic-gate 	}
42777c478bd9Sstevel@tonic-gate 
42780b5de56dSgjelinek 	if ((err = cleanup_zonepath(zonepath, B_FALSE)) != Z_OK) {
42790b5de56dSgjelinek 		errno = err;
42800b5de56dSgjelinek 		zperror2(target_zone, gettext("cleaning up zonepath failed"));
42817c478bd9Sstevel@tonic-gate 		goto bad;
42820b5de56dSgjelinek 	}
42830b5de56dSgjelinek 
42847c478bd9Sstevel@tonic-gate 	err = zone_set_state(target_zone, ZONE_STATE_CONFIGURED);
42857c478bd9Sstevel@tonic-gate 	if (err != Z_OK) {
42867c478bd9Sstevel@tonic-gate 		errno = err;
42877c478bd9Sstevel@tonic-gate 		zperror2(target_zone, gettext("could not reset state"));
42887c478bd9Sstevel@tonic-gate 	}
42897c478bd9Sstevel@tonic-gate bad:
42907c478bd9Sstevel@tonic-gate 	release_lock_file(lockfd);
42917c478bd9Sstevel@tonic-gate 	return (err);
42927c478bd9Sstevel@tonic-gate }
42937c478bd9Sstevel@tonic-gate 
4294108322fbScarlsonj /* ARGSUSED */
4295108322fbScarlsonj static int
4296108322fbScarlsonj mount_func(int argc, char *argv[])
4297108322fbScarlsonj {
4298108322fbScarlsonj 	zone_cmd_arg_t zarg;
4299108322fbScarlsonj 
4300108322fbScarlsonj 	if (argc > 0)
4301108322fbScarlsonj 		return (Z_USAGE);
4302108322fbScarlsonj 	if (sanity_check(target_zone, CMD_MOUNT, B_FALSE, B_FALSE) != Z_OK)
4303108322fbScarlsonj 		return (Z_ERR);
4304108322fbScarlsonj 	if (verify_details(CMD_MOUNT) != Z_OK)
4305108322fbScarlsonj 		return (Z_ERR);
4306108322fbScarlsonj 
4307108322fbScarlsonj 	zarg.cmd = Z_MOUNT;
4308108322fbScarlsonj 	if (call_zoneadmd(target_zone, &zarg) != 0) {
4309108322fbScarlsonj 		zerror(gettext("call to %s failed"), "zoneadmd");
4310108322fbScarlsonj 		return (Z_ERR);
4311108322fbScarlsonj 	}
4312108322fbScarlsonj 	return (Z_OK);
4313108322fbScarlsonj }
4314108322fbScarlsonj 
4315108322fbScarlsonj /* ARGSUSED */
4316108322fbScarlsonj static int
4317108322fbScarlsonj unmount_func(int argc, char *argv[])
4318108322fbScarlsonj {
4319108322fbScarlsonj 	zone_cmd_arg_t zarg;
4320108322fbScarlsonj 
4321108322fbScarlsonj 	if (argc > 0)
4322108322fbScarlsonj 		return (Z_USAGE);
4323108322fbScarlsonj 	if (sanity_check(target_zone, CMD_UNMOUNT, B_FALSE, B_FALSE) != Z_OK)
4324108322fbScarlsonj 		return (Z_ERR);
4325108322fbScarlsonj 
4326108322fbScarlsonj 	zarg.cmd = Z_UNMOUNT;
4327108322fbScarlsonj 	if (call_zoneadmd(target_zone, &zarg) != 0) {
4328108322fbScarlsonj 		zerror(gettext("call to %s failed"), "zoneadmd");
4329108322fbScarlsonj 		return (Z_ERR);
4330108322fbScarlsonj 	}
4331108322fbScarlsonj 	return (Z_OK);
4332108322fbScarlsonj }
4333108322fbScarlsonj 
43347c478bd9Sstevel@tonic-gate static int
43357c478bd9Sstevel@tonic-gate help_func(int argc, char *argv[])
43367c478bd9Sstevel@tonic-gate {
43377c478bd9Sstevel@tonic-gate 	int arg, cmd_num;
43387c478bd9Sstevel@tonic-gate 
43397c478bd9Sstevel@tonic-gate 	if (argc == 0) {
43407c478bd9Sstevel@tonic-gate 		(void) usage(B_TRUE);
43417c478bd9Sstevel@tonic-gate 		return (Z_OK);
43427c478bd9Sstevel@tonic-gate 	}
43437c478bd9Sstevel@tonic-gate 	optind = 0;
43447c478bd9Sstevel@tonic-gate 	if ((arg = getopt(argc, argv, "?")) != EOF) {
43457c478bd9Sstevel@tonic-gate 		switch (arg) {
43467c478bd9Sstevel@tonic-gate 		case '?':
43477c478bd9Sstevel@tonic-gate 			sub_usage(SHELP_HELP, CMD_HELP);
43487c478bd9Sstevel@tonic-gate 			return (optopt == '?' ? Z_OK : Z_USAGE);
43497c478bd9Sstevel@tonic-gate 		default:
43507c478bd9Sstevel@tonic-gate 			sub_usage(SHELP_HELP, CMD_HELP);
43517c478bd9Sstevel@tonic-gate 			return (Z_USAGE);
43527c478bd9Sstevel@tonic-gate 		}
43537c478bd9Sstevel@tonic-gate 	}
43547c478bd9Sstevel@tonic-gate 	while (optind < argc) {
4355394a25e2Scarlsonj 		/* Private commands have NULL short_usage; omit them */
4356394a25e2Scarlsonj 		if ((cmd_num = cmd_match(argv[optind])) < 0 ||
4357394a25e2Scarlsonj 		    cmdtab[cmd_num].short_usage == NULL) {
43587c478bd9Sstevel@tonic-gate 			sub_usage(SHELP_HELP, CMD_HELP);
43597c478bd9Sstevel@tonic-gate 			return (Z_USAGE);
43607c478bd9Sstevel@tonic-gate 		}
43617c478bd9Sstevel@tonic-gate 		sub_usage(cmdtab[cmd_num].short_usage, cmd_num);
43627c478bd9Sstevel@tonic-gate 		optind++;
43637c478bd9Sstevel@tonic-gate 	}
43647c478bd9Sstevel@tonic-gate 	return (Z_OK);
43657c478bd9Sstevel@tonic-gate }
43667c478bd9Sstevel@tonic-gate 
43677c478bd9Sstevel@tonic-gate /*
43687c478bd9Sstevel@tonic-gate  * Returns: CMD_MIN thru CMD_MAX on success, -1 on error
43697c478bd9Sstevel@tonic-gate  */
43707c478bd9Sstevel@tonic-gate 
43717c478bd9Sstevel@tonic-gate static int
43727c478bd9Sstevel@tonic-gate cmd_match(char *cmd)
43737c478bd9Sstevel@tonic-gate {
43747c478bd9Sstevel@tonic-gate 	int i;
43757c478bd9Sstevel@tonic-gate 
43767c478bd9Sstevel@tonic-gate 	for (i = CMD_MIN; i <= CMD_MAX; i++) {
43777c478bd9Sstevel@tonic-gate 		/* return only if there is an exact match */
43787c478bd9Sstevel@tonic-gate 		if (strcmp(cmd, cmdtab[i].cmd_name) == 0)
43797c478bd9Sstevel@tonic-gate 			return (cmdtab[i].cmd_num);
43807c478bd9Sstevel@tonic-gate 	}
43817c478bd9Sstevel@tonic-gate 	return (-1);
43827c478bd9Sstevel@tonic-gate }
43837c478bd9Sstevel@tonic-gate 
43847c478bd9Sstevel@tonic-gate static int
43857c478bd9Sstevel@tonic-gate parse_and_run(int argc, char *argv[])
43867c478bd9Sstevel@tonic-gate {
43877c478bd9Sstevel@tonic-gate 	int i = cmd_match(argv[0]);
43887c478bd9Sstevel@tonic-gate 
43897c478bd9Sstevel@tonic-gate 	if (i < 0)
43907c478bd9Sstevel@tonic-gate 		return (usage(B_FALSE));
43917c478bd9Sstevel@tonic-gate 	return (cmdtab[i].handler(argc - 1, &(argv[1])));
43927c478bd9Sstevel@tonic-gate }
43937c478bd9Sstevel@tonic-gate 
43947c478bd9Sstevel@tonic-gate static char *
43957c478bd9Sstevel@tonic-gate get_execbasename(char *execfullname)
43967c478bd9Sstevel@tonic-gate {
43977c478bd9Sstevel@tonic-gate 	char *last_slash, *execbasename;
43987c478bd9Sstevel@tonic-gate 
43997c478bd9Sstevel@tonic-gate 	/* guard against '/' at end of command invocation */
44007c478bd9Sstevel@tonic-gate 	for (;;) {
44017c478bd9Sstevel@tonic-gate 		last_slash = strrchr(execfullname, '/');
44027c478bd9Sstevel@tonic-gate 		if (last_slash == NULL) {
44037c478bd9Sstevel@tonic-gate 			execbasename = execfullname;
44047c478bd9Sstevel@tonic-gate 			break;
44057c478bd9Sstevel@tonic-gate 		} else {
44067c478bd9Sstevel@tonic-gate 			execbasename = last_slash + 1;
44077c478bd9Sstevel@tonic-gate 			if (*execbasename == '\0') {
44087c478bd9Sstevel@tonic-gate 				*last_slash = '\0';
44097c478bd9Sstevel@tonic-gate 				continue;
44107c478bd9Sstevel@tonic-gate 			}
44117c478bd9Sstevel@tonic-gate 			break;
44127c478bd9Sstevel@tonic-gate 		}
44137c478bd9Sstevel@tonic-gate 	}
44147c478bd9Sstevel@tonic-gate 	return (execbasename);
44157c478bd9Sstevel@tonic-gate }
44167c478bd9Sstevel@tonic-gate 
44177c478bd9Sstevel@tonic-gate int
44187c478bd9Sstevel@tonic-gate main(int argc, char **argv)
44197c478bd9Sstevel@tonic-gate {
44207c478bd9Sstevel@tonic-gate 	int arg;
44217c478bd9Sstevel@tonic-gate 	zoneid_t zid;
4422108322fbScarlsonj 	struct stat st;
44237c478bd9Sstevel@tonic-gate 
44247c478bd9Sstevel@tonic-gate 	if ((locale = setlocale(LC_ALL, "")) == NULL)
44257c478bd9Sstevel@tonic-gate 		locale = "C";
44267c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
44277c478bd9Sstevel@tonic-gate 	setbuf(stdout, NULL);
44287c478bd9Sstevel@tonic-gate 	(void) sigset(SIGHUP, SIG_IGN);
44297c478bd9Sstevel@tonic-gate 	execname = get_execbasename(argv[0]);
44307c478bd9Sstevel@tonic-gate 	target_zone = NULL;
44317c478bd9Sstevel@tonic-gate 	if (chdir("/") != 0) {
44327c478bd9Sstevel@tonic-gate 		zerror(gettext("could not change directory to /."));
44337c478bd9Sstevel@tonic-gate 		exit(Z_ERR);
44347c478bd9Sstevel@tonic-gate 	}
44357c478bd9Sstevel@tonic-gate 
4436*99653d4eSeschrock 	if (init_zfs() != Z_OK)
4437*99653d4eSeschrock 		exit(Z_ERR);
4438*99653d4eSeschrock 
4439108322fbScarlsonj 	while ((arg = getopt(argc, argv, "?z:R:")) != EOF) {
44407c478bd9Sstevel@tonic-gate 		switch (arg) {
44417c478bd9Sstevel@tonic-gate 		case '?':
44427c478bd9Sstevel@tonic-gate 			return (usage(B_TRUE));
44437c478bd9Sstevel@tonic-gate 		case 'z':
44447c478bd9Sstevel@tonic-gate 			target_zone = optarg;
44457c478bd9Sstevel@tonic-gate 			break;
4446108322fbScarlsonj 		case 'R':	/* private option for admin/install use */
4447108322fbScarlsonj 			if (*optarg != '/') {
4448108322fbScarlsonj 				zerror(gettext("root path must be absolute."));
4449108322fbScarlsonj 				exit(Z_ERR);
4450108322fbScarlsonj 			}
4451108322fbScarlsonj 			if (stat(optarg, &st) == -1 || !S_ISDIR(st.st_mode)) {
4452108322fbScarlsonj 				zerror(
4453108322fbScarlsonj 				    gettext("root path must be a directory."));
4454108322fbScarlsonj 				exit(Z_ERR);
4455108322fbScarlsonj 			}
4456108322fbScarlsonj 			zonecfg_set_root(optarg);
4457108322fbScarlsonj 			break;
44587c478bd9Sstevel@tonic-gate 		default:
44597c478bd9Sstevel@tonic-gate 			return (usage(B_FALSE));
44607c478bd9Sstevel@tonic-gate 		}
44617c478bd9Sstevel@tonic-gate 	}
44627c478bd9Sstevel@tonic-gate 
44637c478bd9Sstevel@tonic-gate 	if (optind >= argc)
44647c478bd9Sstevel@tonic-gate 		return (usage(B_FALSE));
44657c478bd9Sstevel@tonic-gate 	if (target_zone != NULL && zone_get_id(target_zone, &zid) != 0) {
44667c478bd9Sstevel@tonic-gate 		errno = Z_NO_ZONE;
44677c478bd9Sstevel@tonic-gate 		zperror(target_zone, B_TRUE);
44687c478bd9Sstevel@tonic-gate 		exit(Z_ERR);
44697c478bd9Sstevel@tonic-gate 	}
44707c478bd9Sstevel@tonic-gate 	return (parse_and_run(argc - optind, &argv[optind]));
44717c478bd9Sstevel@tonic-gate }
4472