xref: /onnv-gate/usr/src/cmd/zoneadm/zoneadm.c (revision 1607:0588fe5166db)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
51507Sgjelinek  * Common Development and Distribution License (the "License").
61507Sgjelinek  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
21222Scomay 
220Sstevel@tonic-gate /*
231300Sgjelinek  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
280Sstevel@tonic-gate 
290Sstevel@tonic-gate /*
300Sstevel@tonic-gate  * zoneadm is a command interpreter for zone administration.  It is all in
310Sstevel@tonic-gate  * C (i.e., no lex/yacc), and all the argument passing is argc/argv based.
320Sstevel@tonic-gate  * main() calls parse_and_run() which calls cmd_match(), then invokes the
330Sstevel@tonic-gate  * appropriate command's handler function.  The rest of the program is the
340Sstevel@tonic-gate  * handler functions and their helper functions.
350Sstevel@tonic-gate  *
360Sstevel@tonic-gate  * Some of the helper functions are used largely to simplify I18N: reducing
370Sstevel@tonic-gate  * the need for translation notes.  This is particularly true of many of
380Sstevel@tonic-gate  * the zerror() calls: doing e.g. zerror(gettext("%s failed"), "foo") rather
390Sstevel@tonic-gate  * than zerror(gettext("foo failed")) with a translation note indicating
400Sstevel@tonic-gate  * that "foo" need not be translated.
410Sstevel@tonic-gate  */
420Sstevel@tonic-gate 
430Sstevel@tonic-gate #include <stdio.h>
440Sstevel@tonic-gate #include <errno.h>
450Sstevel@tonic-gate #include <unistd.h>
460Sstevel@tonic-gate #include <signal.h>
470Sstevel@tonic-gate #include <stdarg.h>
480Sstevel@tonic-gate #include <ctype.h>
490Sstevel@tonic-gate #include <stdlib.h>
500Sstevel@tonic-gate #include <string.h>
510Sstevel@tonic-gate #include <wait.h>
520Sstevel@tonic-gate #include <zone.h>
530Sstevel@tonic-gate #include <priv.h>
540Sstevel@tonic-gate #include <locale.h>
550Sstevel@tonic-gate #include <libintl.h>
560Sstevel@tonic-gate #include <libzonecfg.h>
570Sstevel@tonic-gate #include <bsm/adt.h>
580Sstevel@tonic-gate #include <sys/utsname.h>
590Sstevel@tonic-gate #include <sys/param.h>
600Sstevel@tonic-gate #include <sys/types.h>
610Sstevel@tonic-gate #include <sys/stat.h>
620Sstevel@tonic-gate #include <sys/statvfs.h>
630Sstevel@tonic-gate #include <assert.h>
640Sstevel@tonic-gate #include <sys/sockio.h>
650Sstevel@tonic-gate #include <sys/mntent.h>
660Sstevel@tonic-gate #include <limits.h>
67789Sahrens #include <libzfs.h>
680Sstevel@tonic-gate 
690Sstevel@tonic-gate #include <fcntl.h>
700Sstevel@tonic-gate #include <door.h>
710Sstevel@tonic-gate #include <macros.h>
720Sstevel@tonic-gate #include <libgen.h>
731300Sgjelinek #include <fnmatch.h>
740Sstevel@tonic-gate 
750Sstevel@tonic-gate #include <pool.h>
760Sstevel@tonic-gate #include <sys/pool.h>
770Sstevel@tonic-gate 
780Sstevel@tonic-gate #define	MAXARGS	8
790Sstevel@tonic-gate 
800Sstevel@tonic-gate /* Reflects kernel zone entries */
810Sstevel@tonic-gate typedef struct zone_entry {
820Sstevel@tonic-gate 	zoneid_t	zid;
830Sstevel@tonic-gate 	char		zname[ZONENAME_MAX];
840Sstevel@tonic-gate 	char		*zstate_str;
850Sstevel@tonic-gate 	zone_state_t	zstate_num;
860Sstevel@tonic-gate 	char		zroot[MAXPATHLEN];
870Sstevel@tonic-gate } zone_entry_t;
880Sstevel@tonic-gate 
890Sstevel@tonic-gate static zone_entry_t *zents;
900Sstevel@tonic-gate static size_t nzents;
910Sstevel@tonic-gate 
920Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)		/* should be defined by cc -D */
930Sstevel@tonic-gate #define	TEXT_DOMAIN	"SYS_TEST"	/* Use this only if it wasn't */
940Sstevel@tonic-gate #endif
950Sstevel@tonic-gate 
960Sstevel@tonic-gate #define	Z_ERR	1
970Sstevel@tonic-gate #define	Z_USAGE	2
980Sstevel@tonic-gate 
990Sstevel@tonic-gate /* 0755 is the default directory mode. */
1000Sstevel@tonic-gate #define	DEFAULT_DIR_MODE \
1010Sstevel@tonic-gate 	(S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH)
1020Sstevel@tonic-gate 
1030Sstevel@tonic-gate #define	CMD_HELP	0
1040Sstevel@tonic-gate #define	CMD_BOOT	1
1050Sstevel@tonic-gate #define	CMD_HALT	2
1060Sstevel@tonic-gate #define	CMD_READY	3
1070Sstevel@tonic-gate #define	CMD_REBOOT	4
1080Sstevel@tonic-gate #define	CMD_LIST	5
1090Sstevel@tonic-gate #define	CMD_VERIFY	6
1100Sstevel@tonic-gate #define	CMD_INSTALL	7
1110Sstevel@tonic-gate #define	CMD_UNINSTALL	8
112766Scarlsonj #define	CMD_MOUNT	9
113766Scarlsonj #define	CMD_UNMOUNT	10
1141300Sgjelinek #define	CMD_CLONE	11
1151300Sgjelinek #define	CMD_MOVE	12
1161507Sgjelinek #define	CMD_DETACH	13
1171507Sgjelinek #define	CMD_ATTACH	14
1180Sstevel@tonic-gate 
1190Sstevel@tonic-gate #define	CMD_MIN		CMD_HELP
1201507Sgjelinek #define	CMD_MAX		CMD_ATTACH
1211300Sgjelinek 
1220Sstevel@tonic-gate struct cmd {
1230Sstevel@tonic-gate 	uint_t	cmd_num;				/* command number */
1240Sstevel@tonic-gate 	char	*cmd_name;				/* command name */
1250Sstevel@tonic-gate 	char	*short_usage;				/* short form help */
1260Sstevel@tonic-gate 	int	(*handler)(int argc, char *argv[]);	/* function to call */
1270Sstevel@tonic-gate 
1280Sstevel@tonic-gate };
1290Sstevel@tonic-gate 
1300Sstevel@tonic-gate #define	SHELP_HELP	"help"
1310Sstevel@tonic-gate #define	SHELP_BOOT	"boot [-s]"
1320Sstevel@tonic-gate #define	SHELP_HALT	"halt"
1330Sstevel@tonic-gate #define	SHELP_READY	"ready"
1340Sstevel@tonic-gate #define	SHELP_REBOOT	"reboot"
1350Sstevel@tonic-gate #define	SHELP_LIST	"list [-cipv]"
1360Sstevel@tonic-gate #define	SHELP_VERIFY	"verify"
1370Sstevel@tonic-gate #define	SHELP_INSTALL	"install"
1380Sstevel@tonic-gate #define	SHELP_UNINSTALL	"uninstall [-F]"
1391300Sgjelinek #define	SHELP_CLONE	"clone [-m method] zonename"
1401300Sgjelinek #define	SHELP_MOVE	"move zonepath"
1411507Sgjelinek #define	SHELP_DETACH	"detach"
1421507Sgjelinek #define	SHELP_ATTACH	"attach [-F]"
1430Sstevel@tonic-gate 
1440Sstevel@tonic-gate static int help_func(int argc, char *argv[]);
1450Sstevel@tonic-gate static int ready_func(int argc, char *argv[]);
1460Sstevel@tonic-gate static int boot_func(int argc, char *argv[]);
1470Sstevel@tonic-gate static int halt_func(int argc, char *argv[]);
1480Sstevel@tonic-gate static int reboot_func(int argc, char *argv[]);
1490Sstevel@tonic-gate static int list_func(int argc, char *argv[]);
1500Sstevel@tonic-gate static int verify_func(int argc, char *argv[]);
1510Sstevel@tonic-gate static int install_func(int argc, char *argv[]);
1520Sstevel@tonic-gate static int uninstall_func(int argc, char *argv[]);
153766Scarlsonj static int mount_func(int argc, char *argv[]);
154766Scarlsonj static int unmount_func(int argc, char *argv[]);
1551300Sgjelinek static int clone_func(int argc, char *argv[]);
1561300Sgjelinek static int move_func(int argc, char *argv[]);
1571507Sgjelinek static int detach_func(int argc, char *argv[]);
1581507Sgjelinek static int attach_func(int argc, char *argv[]);
1590Sstevel@tonic-gate static int sanity_check(char *zone, int cmd_num, boolean_t running,
1600Sstevel@tonic-gate     boolean_t unsafe_when_running);
1610Sstevel@tonic-gate static int cmd_match(char *cmd);
1620Sstevel@tonic-gate static int verify_details(int);
1630Sstevel@tonic-gate 
1640Sstevel@tonic-gate static struct cmd cmdtab[] = {
1650Sstevel@tonic-gate 	{ CMD_HELP,		"help",		SHELP_HELP,	help_func },
1660Sstevel@tonic-gate 	{ CMD_BOOT,		"boot",		SHELP_BOOT,	boot_func },
1670Sstevel@tonic-gate 	{ CMD_HALT,		"halt",		SHELP_HALT,	halt_func },
1680Sstevel@tonic-gate 	{ CMD_READY,		"ready",	SHELP_READY,	ready_func },
1690Sstevel@tonic-gate 	{ CMD_REBOOT,		"reboot",	SHELP_REBOOT,	reboot_func },
1700Sstevel@tonic-gate 	{ CMD_LIST,		"list",		SHELP_LIST,	list_func },
1710Sstevel@tonic-gate 	{ CMD_VERIFY,		"verify",	SHELP_VERIFY,	verify_func },
1720Sstevel@tonic-gate 	{ CMD_INSTALL,		"install",	SHELP_INSTALL,	install_func },
1730Sstevel@tonic-gate 	{ CMD_UNINSTALL,	"uninstall",	SHELP_UNINSTALL,
174766Scarlsonj 	    uninstall_func },
1751300Sgjelinek 	/* mount and unmount are private commands for admin/install */
176766Scarlsonj 	{ CMD_MOUNT,		"mount",	NULL,		mount_func },
1771300Sgjelinek 	{ CMD_UNMOUNT,		"unmount",	NULL,		unmount_func },
1781300Sgjelinek 	{ CMD_CLONE,		"clone",	SHELP_CLONE,	clone_func },
1791507Sgjelinek 	{ CMD_MOVE,		"move",		SHELP_MOVE,	move_func },
1801507Sgjelinek 	{ CMD_DETACH,		"detach",	SHELP_DETACH,	detach_func },
1811507Sgjelinek 	{ CMD_ATTACH,		"attach",	SHELP_ATTACH,	attach_func }
1820Sstevel@tonic-gate };
1830Sstevel@tonic-gate 
1840Sstevel@tonic-gate /* global variables */
1850Sstevel@tonic-gate 
1860Sstevel@tonic-gate /* set early in main(), never modified thereafter, used all over the place */
1870Sstevel@tonic-gate static char *execname;
1880Sstevel@tonic-gate static char *target_zone;
1890Sstevel@tonic-gate static char *locale;
1900Sstevel@tonic-gate 
1910Sstevel@tonic-gate /* used in do_subproc() and signal handler */
1920Sstevel@tonic-gate static volatile boolean_t child_killed;
1930Sstevel@tonic-gate 
1940Sstevel@tonic-gate static char *
1950Sstevel@tonic-gate cmd_to_str(int cmd_num)
1960Sstevel@tonic-gate {
1970Sstevel@tonic-gate 	assert(cmd_num >= CMD_MIN && cmd_num <= CMD_MAX);
1980Sstevel@tonic-gate 	return (cmdtab[cmd_num].cmd_name);
1990Sstevel@tonic-gate }
2000Sstevel@tonic-gate 
2010Sstevel@tonic-gate /* This is a separate function because of gettext() wrapping. */
2020Sstevel@tonic-gate static char *
2030Sstevel@tonic-gate long_help(int cmd_num)
2040Sstevel@tonic-gate {
205222Scomay 	assert(cmd_num >= CMD_MIN && cmd_num <= CMD_MAX);
2060Sstevel@tonic-gate 	switch (cmd_num) {
2070Sstevel@tonic-gate 		case CMD_HELP:
2080Sstevel@tonic-gate 			return (gettext("Print usage message."));
2090Sstevel@tonic-gate 		case CMD_BOOT:
2100Sstevel@tonic-gate 			return (gettext("Activates (boots) specified zone.  "
2110Sstevel@tonic-gate 			    "The -s flag can be used\n\tto boot the zone in "
2120Sstevel@tonic-gate 			    "the single-user state."));
2130Sstevel@tonic-gate 		case CMD_HALT:
2140Sstevel@tonic-gate 			return (gettext("Halts specified zone, bypassing "
2150Sstevel@tonic-gate 			    "shutdown scripts and removing runtime\n\t"
2160Sstevel@tonic-gate 			    "resources of the zone."));
2170Sstevel@tonic-gate 		case CMD_READY:
2180Sstevel@tonic-gate 			return (gettext("Prepares a zone for running "
2190Sstevel@tonic-gate 			    "applications but does not start any user\n\t"
2200Sstevel@tonic-gate 			    "processes in the zone."));
2210Sstevel@tonic-gate 		case CMD_REBOOT:
2220Sstevel@tonic-gate 			return (gettext("Restarts the zone (equivalent to a "
2230Sstevel@tonic-gate 			    "halt / boot sequence).\n\tFails if the zone is "
2240Sstevel@tonic-gate 			    "not active."));
2250Sstevel@tonic-gate 		case CMD_LIST:
2260Sstevel@tonic-gate 			return (gettext("Lists the current zones, or a "
2270Sstevel@tonic-gate 			    "specific zone if indicated.  By default,\n\tall "
2280Sstevel@tonic-gate 			    "running zones are listed, though this can be "
2290Sstevel@tonic-gate 			    "expanded to all\n\tinstalled zones with the -i "
2300Sstevel@tonic-gate 			    "option or all configured zones with the\n\t-c "
2310Sstevel@tonic-gate 			    "option.  When used with the general -z <zone> "
2320Sstevel@tonic-gate 			    "option, lists only the\n\tspecified zone, but "
2330Sstevel@tonic-gate 			    "lists it regardless of its state, and the -i "
2340Sstevel@tonic-gate 			    "and -c\n\toptions are disallowed.  The -v option "
2350Sstevel@tonic-gate 			    "can be used to display verbose\n\tinformation: "
2360Sstevel@tonic-gate 			    "zone name, id, current state, root directory and "
2370Sstevel@tonic-gate 			    "options.\n\tThe -p option can be used to request "
2380Sstevel@tonic-gate 			    "machine-parsable output.  The -v\n\tand -p "
2390Sstevel@tonic-gate 			    "options are mutually exclusive.  If neither -v "
2400Sstevel@tonic-gate 			    "nor -p is used,\n\tjust the zone name is "
2410Sstevel@tonic-gate 			    "listed."));
2420Sstevel@tonic-gate 		case CMD_VERIFY:
2430Sstevel@tonic-gate 			return (gettext("Check to make sure the configuration "
2440Sstevel@tonic-gate 			    "can safely be instantiated\n\ton the machine: "
2450Sstevel@tonic-gate 			    "physical network interfaces exist, etc."));
2460Sstevel@tonic-gate 		case CMD_INSTALL:
2470Sstevel@tonic-gate 			return (gettext("Install the configuration on to the "
2480Sstevel@tonic-gate 			    "system."));
2490Sstevel@tonic-gate 		case CMD_UNINSTALL:
2500Sstevel@tonic-gate 			return (gettext("Uninstall the configuration from the "
2510Sstevel@tonic-gate 			    "system.  The -F flag can be used\n\tto force the "
2520Sstevel@tonic-gate 			    "action."));
2531300Sgjelinek 		case CMD_CLONE:
2541300Sgjelinek 			return (gettext("Clone the installation of another "
2551300Sgjelinek 			    "zone."));
2561300Sgjelinek 		case CMD_MOVE:
2571300Sgjelinek 			return (gettext("Move the zone to a new zonepath."));
258766Scarlsonj 		default:
259766Scarlsonj 			return ("");
2600Sstevel@tonic-gate 	}
2610Sstevel@tonic-gate 	/* NOTREACHED */
262222Scomay 	return (NULL);
2630Sstevel@tonic-gate }
2640Sstevel@tonic-gate 
2650Sstevel@tonic-gate /*
2660Sstevel@tonic-gate  * Called with explicit B_TRUE when help is explicitly requested, B_FALSE for
2670Sstevel@tonic-gate  * unexpected errors.
2680Sstevel@tonic-gate  */
2690Sstevel@tonic-gate 
2700Sstevel@tonic-gate static int
2710Sstevel@tonic-gate usage(boolean_t explicit)
2720Sstevel@tonic-gate {
2730Sstevel@tonic-gate 	int i;
2740Sstevel@tonic-gate 	FILE *fd = explicit ? stdout : stderr;
2750Sstevel@tonic-gate 
2760Sstevel@tonic-gate 	(void) fprintf(fd, "%s:\t%s help\n", gettext("usage"), execname);
2770Sstevel@tonic-gate 	(void) fprintf(fd, "\t%s [-z <zone>] list\n", execname);
2780Sstevel@tonic-gate 	(void) fprintf(fd, "\t%s -z <zone> <%s>\n", execname,
2790Sstevel@tonic-gate 	    gettext("subcommand"));
2800Sstevel@tonic-gate 	(void) fprintf(fd, "\n%s:\n\n", gettext("Subcommands"));
2810Sstevel@tonic-gate 	for (i = CMD_MIN; i <= CMD_MAX; i++) {
282766Scarlsonj 		if (cmdtab[i].short_usage == NULL)
283766Scarlsonj 			continue;
2840Sstevel@tonic-gate 		(void) fprintf(fd, "%s\n", cmdtab[i].short_usage);
2850Sstevel@tonic-gate 		if (explicit)
2860Sstevel@tonic-gate 			(void) fprintf(fd, "\t%s\n\n", long_help(i));
2870Sstevel@tonic-gate 	}
2880Sstevel@tonic-gate 	if (!explicit)
2890Sstevel@tonic-gate 		(void) fputs("\n", fd);
2900Sstevel@tonic-gate 	return (Z_USAGE);
2910Sstevel@tonic-gate }
2920Sstevel@tonic-gate 
2930Sstevel@tonic-gate static void
2940Sstevel@tonic-gate sub_usage(char *short_usage, int cmd_num)
2950Sstevel@tonic-gate {
2960Sstevel@tonic-gate 	(void) fprintf(stderr, "%s:\t%s\n", gettext("usage"), short_usage);
2970Sstevel@tonic-gate 	(void) fprintf(stderr, "\t%s\n", long_help(cmd_num));
2980Sstevel@tonic-gate }
2990Sstevel@tonic-gate 
3000Sstevel@tonic-gate /*
3010Sstevel@tonic-gate  * zperror() is like perror(3c) except that this also prints the executable
3020Sstevel@tonic-gate  * name at the start of the message, and takes a boolean indicating whether
3030Sstevel@tonic-gate  * to call libc'c strerror() or that from libzonecfg.
3040Sstevel@tonic-gate  */
3050Sstevel@tonic-gate 
3060Sstevel@tonic-gate static void
3070Sstevel@tonic-gate zperror(const char *str, boolean_t zonecfg_error)
3080Sstevel@tonic-gate {
3090Sstevel@tonic-gate 	(void) fprintf(stderr, "%s: %s: %s\n", execname, str,
3100Sstevel@tonic-gate 	    zonecfg_error ? zonecfg_strerror(errno) : strerror(errno));
3110Sstevel@tonic-gate }
3120Sstevel@tonic-gate 
3130Sstevel@tonic-gate /*
3140Sstevel@tonic-gate  * zperror2() is very similar to zperror() above, except it also prints a
3150Sstevel@tonic-gate  * supplied zone name after the executable.
3160Sstevel@tonic-gate  *
3170Sstevel@tonic-gate  * All current consumers of this function want libzonecfg's strerror() rather
3180Sstevel@tonic-gate  * than libc's; if this ever changes, this function can be made more generic
3190Sstevel@tonic-gate  * like zperror() above.
3200Sstevel@tonic-gate  */
3210Sstevel@tonic-gate 
3220Sstevel@tonic-gate static void
3230Sstevel@tonic-gate zperror2(const char *zone, const char *str)
3240Sstevel@tonic-gate {
3250Sstevel@tonic-gate 	(void) fprintf(stderr, "%s: %s: %s: %s\n", execname, zone, str,
3260Sstevel@tonic-gate 	    zonecfg_strerror(errno));
3270Sstevel@tonic-gate }
3280Sstevel@tonic-gate 
3290Sstevel@tonic-gate /* PRINTFLIKE1 */
3300Sstevel@tonic-gate static void
3310Sstevel@tonic-gate zerror(const char *fmt, ...)
3320Sstevel@tonic-gate {
3330Sstevel@tonic-gate 	va_list alist;
3340Sstevel@tonic-gate 
3350Sstevel@tonic-gate 	va_start(alist, fmt);
3360Sstevel@tonic-gate 	(void) fprintf(stderr, "%s: ", execname);
3370Sstevel@tonic-gate 	if (target_zone != NULL)
3380Sstevel@tonic-gate 		(void) fprintf(stderr, "zone '%s': ", target_zone);
3390Sstevel@tonic-gate 	(void) vfprintf(stderr, fmt, alist);
3400Sstevel@tonic-gate 	(void) fprintf(stderr, "\n");
3410Sstevel@tonic-gate 	va_end(alist);
3420Sstevel@tonic-gate }
3430Sstevel@tonic-gate 
3440Sstevel@tonic-gate static void *
3450Sstevel@tonic-gate safe_calloc(size_t nelem, size_t elsize)
3460Sstevel@tonic-gate {
3470Sstevel@tonic-gate 	void *r = calloc(nelem, elsize);
3480Sstevel@tonic-gate 
3490Sstevel@tonic-gate 	if (r == NULL) {
3500Sstevel@tonic-gate 		zerror(gettext("failed to allocate %lu bytes: %s"),
3510Sstevel@tonic-gate 		    (ulong_t)nelem * elsize, strerror(errno));
3520Sstevel@tonic-gate 		exit(Z_ERR);
3530Sstevel@tonic-gate 	}
3540Sstevel@tonic-gate 	return (r);
3550Sstevel@tonic-gate }
3560Sstevel@tonic-gate 
3570Sstevel@tonic-gate static void
3580Sstevel@tonic-gate zone_print(zone_entry_t *zent, boolean_t verbose, boolean_t parsable)
3590Sstevel@tonic-gate {
3600Sstevel@tonic-gate 	static boolean_t firsttime = B_TRUE;
3610Sstevel@tonic-gate 
3620Sstevel@tonic-gate 	assert(!(verbose && parsable));
3630Sstevel@tonic-gate 	if (firsttime && verbose) {
3640Sstevel@tonic-gate 		firsttime = B_FALSE;
3650Sstevel@tonic-gate 		(void) printf("%*s %-16s %-14s %-30s\n", ZONEID_WIDTH, "ID",
3660Sstevel@tonic-gate 		    "NAME", "STATUS", "PATH");
3670Sstevel@tonic-gate 	}
3680Sstevel@tonic-gate 	if (!verbose) {
3690Sstevel@tonic-gate 		if (!parsable) {
3700Sstevel@tonic-gate 			(void) printf("%s\n", zent->zname);
3710Sstevel@tonic-gate 			return;
3720Sstevel@tonic-gate 		}
3730Sstevel@tonic-gate 		if (zent->zid == ZONE_ID_UNDEFINED)
3740Sstevel@tonic-gate 			(void) printf("-");
3750Sstevel@tonic-gate 		else
3760Sstevel@tonic-gate 			(void) printf("%lu", zent->zid);
3770Sstevel@tonic-gate 		(void) printf(":%s:%s:%s\n", zent->zname, zent->zstate_str,
3780Sstevel@tonic-gate 		    zent->zroot);
3790Sstevel@tonic-gate 		return;
3800Sstevel@tonic-gate 	}
3810Sstevel@tonic-gate 	if (zent->zstate_str != NULL) {
3820Sstevel@tonic-gate 		if (zent->zid == ZONE_ID_UNDEFINED)
3830Sstevel@tonic-gate 			(void) printf("%*s", ZONEID_WIDTH, "-");
3840Sstevel@tonic-gate 		else
3850Sstevel@tonic-gate 			(void) printf("%*lu", ZONEID_WIDTH, zent->zid);
3860Sstevel@tonic-gate 		(void) printf(" %-16s %-14s %-30s\n", zent->zname,
3870Sstevel@tonic-gate 		    zent->zstate_str, zent->zroot);
3880Sstevel@tonic-gate 	}
3890Sstevel@tonic-gate }
3900Sstevel@tonic-gate 
3910Sstevel@tonic-gate static int
392766Scarlsonj lookup_zone_info(const char *zone_name, zoneid_t zid, zone_entry_t *zent)
3930Sstevel@tonic-gate {
3940Sstevel@tonic-gate 	char root[MAXPATHLEN];
3950Sstevel@tonic-gate 	int err;
3960Sstevel@tonic-gate 
3970Sstevel@tonic-gate 	(void) strlcpy(zent->zname, zone_name, sizeof (zent->zname));
3980Sstevel@tonic-gate 	(void) strlcpy(zent->zroot, "???", sizeof (zent->zroot));
3990Sstevel@tonic-gate 	zent->zstate_str = "???";
4000Sstevel@tonic-gate 
401766Scarlsonj 	zent->zid = zid;
4020Sstevel@tonic-gate 
4030Sstevel@tonic-gate 	if ((err = zone_get_zonepath(zent->zname, root, sizeof (root))) !=
4040Sstevel@tonic-gate 	    Z_OK) {
4050Sstevel@tonic-gate 		errno = err;
4060Sstevel@tonic-gate 		zperror2(zent->zname, gettext("could not get zone path"));
4070Sstevel@tonic-gate 		return (Z_ERR);
4080Sstevel@tonic-gate 	}
4090Sstevel@tonic-gate 	(void) strlcpy(zent->zroot, root, sizeof (zent->zroot));
4100Sstevel@tonic-gate 
4110Sstevel@tonic-gate 	if ((err = zone_get_state(zent->zname, &zent->zstate_num)) != Z_OK) {
4120Sstevel@tonic-gate 		errno = err;
4130Sstevel@tonic-gate 		zperror2(zent->zname, gettext("could not get state"));
4140Sstevel@tonic-gate 		return (Z_ERR);
4150Sstevel@tonic-gate 	}
4160Sstevel@tonic-gate 	zent->zstate_str = zone_state_str(zent->zstate_num);
4170Sstevel@tonic-gate 
4180Sstevel@tonic-gate 	return (Z_OK);
4190Sstevel@tonic-gate }
4200Sstevel@tonic-gate 
4210Sstevel@tonic-gate /*
4220Sstevel@tonic-gate  * fetch_zents() calls zone_list(2) to find out how many zones are running
4230Sstevel@tonic-gate  * (which is stored in the global nzents), then calls zone_list(2) again
4240Sstevel@tonic-gate  * to fetch the list of running zones (stored in the global zents).  This
4250Sstevel@tonic-gate  * function may be called multiple times, so if zents is already set, we
4260Sstevel@tonic-gate  * return immediately to save work.
4270Sstevel@tonic-gate  */
4280Sstevel@tonic-gate 
4290Sstevel@tonic-gate static int
430766Scarlsonj fetch_zents(void)
4310Sstevel@tonic-gate {
4320Sstevel@tonic-gate 	zoneid_t *zids = NULL;
4330Sstevel@tonic-gate 	uint_t nzents_saved;
434766Scarlsonj 	int i, retv;
435766Scarlsonj 	FILE *fp;
436766Scarlsonj 	boolean_t inaltroot;
437766Scarlsonj 	zone_entry_t *zentp;
4380Sstevel@tonic-gate 
4390Sstevel@tonic-gate 	if (nzents > 0)
4400Sstevel@tonic-gate 		return (Z_OK);
4410Sstevel@tonic-gate 
4420Sstevel@tonic-gate 	if (zone_list(NULL, &nzents) != 0) {
4430Sstevel@tonic-gate 		zperror(gettext("failed to get zoneid list"), B_FALSE);
4440Sstevel@tonic-gate 		return (Z_ERR);
4450Sstevel@tonic-gate 	}
4460Sstevel@tonic-gate 
4470Sstevel@tonic-gate again:
4480Sstevel@tonic-gate 	if (nzents == 0)
4490Sstevel@tonic-gate 		return (Z_OK);
4500Sstevel@tonic-gate 
4510Sstevel@tonic-gate 	zids = safe_calloc(nzents, sizeof (zoneid_t));
4520Sstevel@tonic-gate 	nzents_saved = nzents;
4530Sstevel@tonic-gate 
4540Sstevel@tonic-gate 	if (zone_list(zids, &nzents) != 0) {
4550Sstevel@tonic-gate 		zperror(gettext("failed to get zone list"), B_FALSE);
4560Sstevel@tonic-gate 		free(zids);
4570Sstevel@tonic-gate 		return (Z_ERR);
4580Sstevel@tonic-gate 	}
4590Sstevel@tonic-gate 	if (nzents != nzents_saved) {
4600Sstevel@tonic-gate 		/* list changed, try again */
4610Sstevel@tonic-gate 		free(zids);
4620Sstevel@tonic-gate 		goto again;
4630Sstevel@tonic-gate 	}
4640Sstevel@tonic-gate 
4650Sstevel@tonic-gate 	zents = safe_calloc(nzents, sizeof (zone_entry_t));
4660Sstevel@tonic-gate 
467766Scarlsonj 	inaltroot = zonecfg_in_alt_root();
468766Scarlsonj 	if (inaltroot)
469766Scarlsonj 		fp = zonecfg_open_scratch("", B_FALSE);
470766Scarlsonj 	else
471766Scarlsonj 		fp = NULL;
472766Scarlsonj 	zentp = zents;
473766Scarlsonj 	retv = Z_OK;
4740Sstevel@tonic-gate 	for (i = 0; i < nzents; i++) {
4750Sstevel@tonic-gate 		char name[ZONENAME_MAX];
476766Scarlsonj 		char altname[ZONENAME_MAX];
4770Sstevel@tonic-gate 
478766Scarlsonj 		if (getzonenamebyid(zids[i], name, sizeof (name)) < 0) {
4790Sstevel@tonic-gate 			zperror(gettext("failed to get zone name"), B_FALSE);
480766Scarlsonj 			retv = Z_ERR;
481766Scarlsonj 			continue;
482766Scarlsonj 		}
483766Scarlsonj 		if (zonecfg_is_scratch(name)) {
484766Scarlsonj 			/* Ignore scratch zones by default */
485766Scarlsonj 			if (!inaltroot)
486766Scarlsonj 				continue;
487766Scarlsonj 			if (fp == NULL ||
488766Scarlsonj 			    zonecfg_reverse_scratch(fp, name, altname,
489766Scarlsonj 			    sizeof (altname), NULL, 0) == -1) {
490924Sgjelinek 				zerror(gettext("could not resolve scratch "
491766Scarlsonj 				    "zone %s"), name);
492766Scarlsonj 				retv = Z_ERR;
493766Scarlsonj 				continue;
494766Scarlsonj 			}
495766Scarlsonj 			(void) strcpy(name, altname);
496766Scarlsonj 		} else {
497766Scarlsonj 			/* Ignore non-scratch when in an alternate root */
498766Scarlsonj 			if (inaltroot && strcmp(name, GLOBAL_ZONENAME) != 0)
499766Scarlsonj 				continue;
500766Scarlsonj 		}
501766Scarlsonj 		if (lookup_zone_info(name, zids[i], zentp) != Z_OK) {
502766Scarlsonj 			zerror(gettext("failed to get zone data"));
503766Scarlsonj 			retv = Z_ERR;
504766Scarlsonj 			continue;
505766Scarlsonj 		}
506766Scarlsonj 		zentp++;
5070Sstevel@tonic-gate 	}
508766Scarlsonj 	nzents = zentp - zents;
509766Scarlsonj 	if (fp != NULL)
510766Scarlsonj 		zonecfg_close_scratch(fp);
5110Sstevel@tonic-gate 
5120Sstevel@tonic-gate 	free(zids);
513766Scarlsonj 	return (retv);
5140Sstevel@tonic-gate }
5150Sstevel@tonic-gate 
516766Scarlsonj static int
5170Sstevel@tonic-gate zone_print_list(zone_state_t min_state, boolean_t verbose, boolean_t parsable)
5180Sstevel@tonic-gate {
5190Sstevel@tonic-gate 	int i;
5200Sstevel@tonic-gate 	zone_entry_t zent;
5210Sstevel@tonic-gate 	FILE *cookie;
5220Sstevel@tonic-gate 	char *name;
5230Sstevel@tonic-gate 
5240Sstevel@tonic-gate 	/*
5250Sstevel@tonic-gate 	 * First get the list of running zones from the kernel and print them.
5260Sstevel@tonic-gate 	 * If that is all we need, then return.
5270Sstevel@tonic-gate 	 */
528766Scarlsonj 	if ((i = fetch_zents()) != Z_OK) {
5290Sstevel@tonic-gate 		/*
5300Sstevel@tonic-gate 		 * No need for error messages; fetch_zents() has already taken
5310Sstevel@tonic-gate 		 * care of this.
5320Sstevel@tonic-gate 		 */
533766Scarlsonj 		return (i);
5340Sstevel@tonic-gate 	}
535766Scarlsonj 	for (i = 0; i < nzents; i++)
5360Sstevel@tonic-gate 		zone_print(&zents[i], verbose, parsable);
5370Sstevel@tonic-gate 	if (min_state >= ZONE_STATE_RUNNING)
538766Scarlsonj 		return (Z_OK);
5390Sstevel@tonic-gate 	/*
5400Sstevel@tonic-gate 	 * Next, get the full list of zones from the configuration, skipping
5410Sstevel@tonic-gate 	 * any we have already printed.
5420Sstevel@tonic-gate 	 */
5430Sstevel@tonic-gate 	cookie = setzoneent();
5440Sstevel@tonic-gate 	while ((name = getzoneent(cookie)) != NULL) {
5450Sstevel@tonic-gate 		for (i = 0; i < nzents; i++) {
5460Sstevel@tonic-gate 			if (strcmp(zents[i].zname, name) == 0)
5470Sstevel@tonic-gate 				break;
5480Sstevel@tonic-gate 		}
5490Sstevel@tonic-gate 		if (i < nzents) {
5500Sstevel@tonic-gate 			free(name);
5510Sstevel@tonic-gate 			continue;
5520Sstevel@tonic-gate 		}
553766Scarlsonj 		if (lookup_zone_info(name, ZONE_ID_UNDEFINED, &zent) != Z_OK) {
5540Sstevel@tonic-gate 			free(name);
5550Sstevel@tonic-gate 			continue;
5560Sstevel@tonic-gate 		}
5570Sstevel@tonic-gate 		free(name);
5580Sstevel@tonic-gate 		if (zent.zstate_num >= min_state)
5590Sstevel@tonic-gate 			zone_print(&zent, verbose, parsable);
5600Sstevel@tonic-gate 	}
5610Sstevel@tonic-gate 	endzoneent(cookie);
562766Scarlsonj 	return (Z_OK);
5630Sstevel@tonic-gate }
5640Sstevel@tonic-gate 
5650Sstevel@tonic-gate static zone_entry_t *
5660Sstevel@tonic-gate lookup_running_zone(char *str)
5670Sstevel@tonic-gate {
5680Sstevel@tonic-gate 	zoneid_t zoneid;
5690Sstevel@tonic-gate 	char *cp;
5700Sstevel@tonic-gate 	int i;
5710Sstevel@tonic-gate 
5720Sstevel@tonic-gate 	if (fetch_zents() != Z_OK)
5730Sstevel@tonic-gate 		return (NULL);
5740Sstevel@tonic-gate 
5750Sstevel@tonic-gate 	for (i = 0; i < nzents; i++) {
5760Sstevel@tonic-gate 		if (strcmp(str, zents[i].zname) == 0)
5770Sstevel@tonic-gate 			return (&zents[i]);
5780Sstevel@tonic-gate 	}
5790Sstevel@tonic-gate 	errno = 0;
5800Sstevel@tonic-gate 	zoneid = strtol(str, &cp, 0);
5810Sstevel@tonic-gate 	if (zoneid < MIN_ZONEID || zoneid > MAX_ZONEID ||
5820Sstevel@tonic-gate 	    errno != 0 || *cp != '\0')
5830Sstevel@tonic-gate 		return (NULL);
5840Sstevel@tonic-gate 	for (i = 0; i < nzents; i++) {
5850Sstevel@tonic-gate 		if (zoneid == zents[i].zid)
5860Sstevel@tonic-gate 			return (&zents[i]);
5870Sstevel@tonic-gate 	}
5880Sstevel@tonic-gate 	return (NULL);
5890Sstevel@tonic-gate }
5900Sstevel@tonic-gate 
5910Sstevel@tonic-gate /*
5920Sstevel@tonic-gate  * Check a bit in a mode_t: if on is B_TRUE, that bit should be on; if
5930Sstevel@tonic-gate  * B_FALSE, it should be off.  Return B_TRUE if the mode is bad (incorrect).
5940Sstevel@tonic-gate  */
5950Sstevel@tonic-gate static boolean_t
5960Sstevel@tonic-gate bad_mode_bit(mode_t mode, mode_t bit, boolean_t on, char *file)
5970Sstevel@tonic-gate {
5980Sstevel@tonic-gate 	char *str;
5990Sstevel@tonic-gate 
6000Sstevel@tonic-gate 	assert(bit == S_IRUSR || bit == S_IWUSR || bit == S_IXUSR ||
6010Sstevel@tonic-gate 	    bit == S_IRGRP || bit == S_IWGRP || bit == S_IXGRP ||
6020Sstevel@tonic-gate 	    bit == S_IROTH || bit == S_IWOTH || bit == S_IXOTH);
6030Sstevel@tonic-gate 	/*
6040Sstevel@tonic-gate 	 * TRANSLATION_NOTE
6050Sstevel@tonic-gate 	 * The strings below will be used as part of a larger message,
6060Sstevel@tonic-gate 	 * either:
6070Sstevel@tonic-gate 	 * (file name) must be (owner|group|world) (read|writ|execut)able
6080Sstevel@tonic-gate 	 * or
6090Sstevel@tonic-gate 	 * (file name) must not be (owner|group|world) (read|writ|execut)able
6100Sstevel@tonic-gate 	 */
6110Sstevel@tonic-gate 	switch (bit) {
6120Sstevel@tonic-gate 	case S_IRUSR:
6130Sstevel@tonic-gate 		str = gettext("owner readable");
6140Sstevel@tonic-gate 		break;
6150Sstevel@tonic-gate 	case S_IWUSR:
6160Sstevel@tonic-gate 		str = gettext("owner writable");
6170Sstevel@tonic-gate 		break;
6180Sstevel@tonic-gate 	case S_IXUSR:
6190Sstevel@tonic-gate 		str = gettext("owner executable");
6200Sstevel@tonic-gate 		break;
6210Sstevel@tonic-gate 	case S_IRGRP:
6220Sstevel@tonic-gate 		str = gettext("group readable");
6230Sstevel@tonic-gate 		break;
6240Sstevel@tonic-gate 	case S_IWGRP:
6250Sstevel@tonic-gate 		str = gettext("group writable");
6260Sstevel@tonic-gate 		break;
6270Sstevel@tonic-gate 	case S_IXGRP:
6280Sstevel@tonic-gate 		str = gettext("group executable");
6290Sstevel@tonic-gate 		break;
6300Sstevel@tonic-gate 	case S_IROTH:
6310Sstevel@tonic-gate 		str = gettext("world readable");
6320Sstevel@tonic-gate 		break;
6330Sstevel@tonic-gate 	case S_IWOTH:
6340Sstevel@tonic-gate 		str = gettext("world writable");
6350Sstevel@tonic-gate 		break;
6360Sstevel@tonic-gate 	case S_IXOTH:
6370Sstevel@tonic-gate 		str = gettext("world executable");
6380Sstevel@tonic-gate 		break;
6390Sstevel@tonic-gate 	}
6400Sstevel@tonic-gate 	if ((mode & bit) == (on ? 0 : bit)) {
6410Sstevel@tonic-gate 		/*
6420Sstevel@tonic-gate 		 * TRANSLATION_NOTE
6430Sstevel@tonic-gate 		 * The first parameter below is a file name; the second
6440Sstevel@tonic-gate 		 * is one of the "(owner|group|world) (read|writ|execut)able"
6450Sstevel@tonic-gate 		 * strings from above.
6460Sstevel@tonic-gate 		 */
6470Sstevel@tonic-gate 		/*
6480Sstevel@tonic-gate 		 * The code below could be simplified but not in a way
6490Sstevel@tonic-gate 		 * that would easily translate to non-English locales.
6500Sstevel@tonic-gate 		 */
6510Sstevel@tonic-gate 		if (on) {
6520Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("%s must be %s.\n"),
6530Sstevel@tonic-gate 			    file, str);
6540Sstevel@tonic-gate 		} else {
6550Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("%s must not be %s.\n"),
6560Sstevel@tonic-gate 			    file, str);
6570Sstevel@tonic-gate 		}
6580Sstevel@tonic-gate 		return (B_TRUE);
6590Sstevel@tonic-gate 	}
6600Sstevel@tonic-gate 	return (B_FALSE);
6610Sstevel@tonic-gate }
6620Sstevel@tonic-gate 
6630Sstevel@tonic-gate /*
6640Sstevel@tonic-gate  * We want to make sure that no zone has its zone path as a child node
6650Sstevel@tonic-gate  * (in the directory sense) of any other.  We do that by comparing this
6660Sstevel@tonic-gate  * zone's path to the path of all other (non-global) zones.  The comparison
6670Sstevel@tonic-gate  * in each case is simple: add '/' to the end of the path, then do a
6680Sstevel@tonic-gate  * strncmp() of the two paths, using the length of the shorter one.
6690Sstevel@tonic-gate  */
6700Sstevel@tonic-gate 
6710Sstevel@tonic-gate static int
6720Sstevel@tonic-gate crosscheck_zonepaths(char *path)
6730Sstevel@tonic-gate {
6740Sstevel@tonic-gate 	char rpath[MAXPATHLEN];		/* resolved path */
6750Sstevel@tonic-gate 	char path_copy[MAXPATHLEN];	/* copy of original path */
6760Sstevel@tonic-gate 	char rpath_copy[MAXPATHLEN];	/* copy of original rpath */
6770Sstevel@tonic-gate 	struct zoneent *ze;
6780Sstevel@tonic-gate 	int res, err;
6790Sstevel@tonic-gate 	FILE *cookie;
6800Sstevel@tonic-gate 
6810Sstevel@tonic-gate 	cookie = setzoneent();
6820Sstevel@tonic-gate 	while ((ze = getzoneent_private(cookie)) != NULL) {
6830Sstevel@tonic-gate 		/* Skip zones which are not installed. */
6840Sstevel@tonic-gate 		if (ze->zone_state < ZONE_STATE_INSTALLED) {
6850Sstevel@tonic-gate 			free(ze);
6860Sstevel@tonic-gate 			continue;
6870Sstevel@tonic-gate 		}
6880Sstevel@tonic-gate 		/* Skip the global zone and the current target zone. */
6890Sstevel@tonic-gate 		if (strcmp(ze->zone_name, GLOBAL_ZONENAME) == 0 ||
6900Sstevel@tonic-gate 		    strcmp(ze->zone_name, target_zone) == 0) {
6910Sstevel@tonic-gate 			free(ze);
6920Sstevel@tonic-gate 			continue;
6930Sstevel@tonic-gate 		}
6940Sstevel@tonic-gate 		if (strlen(ze->zone_path) == 0) {
6950Sstevel@tonic-gate 			/* old index file without path, fall back */
6960Sstevel@tonic-gate 			if ((err = zone_get_zonepath(ze->zone_name,
6970Sstevel@tonic-gate 			    ze->zone_path, sizeof (ze->zone_path))) != Z_OK) {
6980Sstevel@tonic-gate 				errno = err;
6990Sstevel@tonic-gate 				zperror2(ze->zone_name,
7000Sstevel@tonic-gate 				    gettext("could not get zone path"));
7010Sstevel@tonic-gate 				free(ze);
7020Sstevel@tonic-gate 				continue;
7030Sstevel@tonic-gate 			}
7040Sstevel@tonic-gate 		}
705766Scarlsonj 		(void) snprintf(path_copy, sizeof (path_copy), "%s%s",
706766Scarlsonj 		    zonecfg_get_root(), ze->zone_path);
707766Scarlsonj 		res = resolvepath(path_copy, rpath, sizeof (rpath));
7080Sstevel@tonic-gate 		if (res == -1) {
7090Sstevel@tonic-gate 			if (errno != ENOENT) {
710766Scarlsonj 				zperror(path_copy, B_FALSE);
7110Sstevel@tonic-gate 				free(ze);
7120Sstevel@tonic-gate 				return (Z_ERR);
7130Sstevel@tonic-gate 			}
7140Sstevel@tonic-gate 			(void) printf(gettext("WARNING: zone %s is installed, "
7150Sstevel@tonic-gate 			    "but its %s %s does not exist.\n"), ze->zone_name,
716766Scarlsonj 			    "zonepath", path_copy);
7170Sstevel@tonic-gate 			free(ze);
7180Sstevel@tonic-gate 			continue;
7190Sstevel@tonic-gate 		}
7200Sstevel@tonic-gate 		rpath[res] = '\0';
7210Sstevel@tonic-gate 		(void) snprintf(path_copy, sizeof (path_copy), "%s/", path);
7220Sstevel@tonic-gate 		(void) snprintf(rpath_copy, sizeof (rpath_copy), "%s/", rpath);
7230Sstevel@tonic-gate 		if (strncmp(path_copy, rpath_copy,
7240Sstevel@tonic-gate 		    min(strlen(path_copy), strlen(rpath_copy))) == 0) {
725924Sgjelinek 			/*
726924Sgjelinek 			 * TRANSLATION_NOTE
727924Sgjelinek 			 * zonepath is a literal that should not be translated.
728924Sgjelinek 			 */
7290Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("%s zonepath (%s) and "
7300Sstevel@tonic-gate 			    "%s zonepath (%s) overlap.\n"),
7310Sstevel@tonic-gate 			    target_zone, path, ze->zone_name, rpath);
7320Sstevel@tonic-gate 			free(ze);
7330Sstevel@tonic-gate 			return (Z_ERR);
7340Sstevel@tonic-gate 		}
7350Sstevel@tonic-gate 		free(ze);
7360Sstevel@tonic-gate 	}
7370Sstevel@tonic-gate 	endzoneent(cookie);
7380Sstevel@tonic-gate 	return (Z_OK);
7390Sstevel@tonic-gate }
7400Sstevel@tonic-gate 
7410Sstevel@tonic-gate static int
7420Sstevel@tonic-gate validate_zonepath(char *path, int cmd_num)
7430Sstevel@tonic-gate {
7440Sstevel@tonic-gate 	int res;			/* result of last library/system call */
7450Sstevel@tonic-gate 	boolean_t err = B_FALSE;	/* have we run into an error? */
7460Sstevel@tonic-gate 	struct stat stbuf;
7470Sstevel@tonic-gate 	struct statvfs vfsbuf;
7480Sstevel@tonic-gate 	char rpath[MAXPATHLEN];		/* resolved path */
7490Sstevel@tonic-gate 	char ppath[MAXPATHLEN];		/* parent path */
7500Sstevel@tonic-gate 	char rppath[MAXPATHLEN];	/* resolved parent path */
7510Sstevel@tonic-gate 	char rootpath[MAXPATHLEN];	/* root path */
7520Sstevel@tonic-gate 	zone_state_t state;
7530Sstevel@tonic-gate 
7540Sstevel@tonic-gate 	if (path[0] != '/') {
7550Sstevel@tonic-gate 		(void) fprintf(stderr,
7560Sstevel@tonic-gate 		    gettext("%s is not an absolute path.\n"), path);
7570Sstevel@tonic-gate 		return (Z_ERR);
7580Sstevel@tonic-gate 	}
7590Sstevel@tonic-gate 	if ((res = resolvepath(path, rpath, sizeof (rpath))) == -1) {
7600Sstevel@tonic-gate 		if ((errno != ENOENT) ||
7611300Sgjelinek 		    (cmd_num != CMD_VERIFY && cmd_num != CMD_INSTALL &&
7621300Sgjelinek 		    cmd_num != CMD_CLONE && cmd_num != CMD_MOVE)) {
7630Sstevel@tonic-gate 			zperror(path, B_FALSE);
7640Sstevel@tonic-gate 			return (Z_ERR);
7650Sstevel@tonic-gate 		}
7660Sstevel@tonic-gate 		if (cmd_num == CMD_VERIFY) {
767924Sgjelinek 			/*
768924Sgjelinek 			 * TRANSLATION_NOTE
769924Sgjelinek 			 * zoneadm is a literal that should not be translated.
770924Sgjelinek 			 */
7710Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("WARNING: %s does not "
772924Sgjelinek 			    "exist, so it could not be verified.\nWhen "
773924Sgjelinek 			    "'zoneadm %s' is run, '%s' will try to create\n%s, "
774924Sgjelinek 			    "and '%s' will be tried again,\nbut the '%s' may "
775924Sgjelinek 			    "fail if:\nthe parent directory of %s is group- or "
776924Sgjelinek 			    "other-writable\nor\n%s overlaps with any other "
7770Sstevel@tonic-gate 			    "installed zones.\n"), path,
7780Sstevel@tonic-gate 			    cmd_to_str(CMD_INSTALL), cmd_to_str(CMD_INSTALL),
7790Sstevel@tonic-gate 			    path, cmd_to_str(CMD_VERIFY),
7800Sstevel@tonic-gate 			    cmd_to_str(CMD_VERIFY), path, path);
7810Sstevel@tonic-gate 			return (Z_OK);
7820Sstevel@tonic-gate 		}
7830Sstevel@tonic-gate 		/*
7840Sstevel@tonic-gate 		 * The zonepath is supposed to be mode 700 but its
7850Sstevel@tonic-gate 		 * parent(s) 755.  So use 755 on the mkdirp() then
7860Sstevel@tonic-gate 		 * chmod() the zonepath itself to 700.
7870Sstevel@tonic-gate 		 */
7880Sstevel@tonic-gate 		if (mkdirp(path, DEFAULT_DIR_MODE) < 0) {
7890Sstevel@tonic-gate 			zperror(path, B_FALSE);
7900Sstevel@tonic-gate 			return (Z_ERR);
7910Sstevel@tonic-gate 		}
7920Sstevel@tonic-gate 		/*
7930Sstevel@tonic-gate 		 * If the chmod() fails, report the error, but might
7940Sstevel@tonic-gate 		 * as well continue the verify procedure.
7950Sstevel@tonic-gate 		 */
7960Sstevel@tonic-gate 		if (chmod(path, S_IRWXU) != 0)
7970Sstevel@tonic-gate 			zperror(path, B_FALSE);
7980Sstevel@tonic-gate 		/*
7990Sstevel@tonic-gate 		 * Since the mkdir() succeeded, we should not have to
8000Sstevel@tonic-gate 		 * worry about a subsequent ENOENT, thus this should
8010Sstevel@tonic-gate 		 * only recurse once.
8020Sstevel@tonic-gate 		 */
8031300Sgjelinek 		return (validate_zonepath(path, cmd_num));
8040Sstevel@tonic-gate 	}
8050Sstevel@tonic-gate 	rpath[res] = '\0';
8060Sstevel@tonic-gate 	if (strcmp(path, rpath) != 0) {
8070Sstevel@tonic-gate 		errno = Z_RESOLVED_PATH;
8080Sstevel@tonic-gate 		zperror(path, B_TRUE);
8090Sstevel@tonic-gate 		return (Z_ERR);
8100Sstevel@tonic-gate 	}
8110Sstevel@tonic-gate 	if ((res = stat(rpath, &stbuf)) != 0) {
8120Sstevel@tonic-gate 		zperror(rpath, B_FALSE);
8130Sstevel@tonic-gate 		return (Z_ERR);
8140Sstevel@tonic-gate 	}
8150Sstevel@tonic-gate 	if (!S_ISDIR(stbuf.st_mode)) {
8160Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("%s is not a directory.\n"),
8170Sstevel@tonic-gate 		    rpath);
8180Sstevel@tonic-gate 		return (Z_ERR);
8190Sstevel@tonic-gate 	}
8200Sstevel@tonic-gate 	if ((strcmp(stbuf.st_fstype, MNTTYPE_TMPFS) == 0) ||
8210Sstevel@tonic-gate 	    (strcmp(stbuf.st_fstype, MNTTYPE_XMEMFS) == 0)) {
8220Sstevel@tonic-gate 		(void) printf(gettext("WARNING: %s is on a temporary "
8230Sstevel@tonic-gate 		    "file-system.\n"), rpath);
8240Sstevel@tonic-gate 	}
8250Sstevel@tonic-gate 	if (crosscheck_zonepaths(rpath) != Z_OK)
8260Sstevel@tonic-gate 		return (Z_ERR);
8270Sstevel@tonic-gate 	/*
8280Sstevel@tonic-gate 	 * Try to collect and report as many minor errors as possible
8290Sstevel@tonic-gate 	 * before returning, so the user can learn everything that needs
8300Sstevel@tonic-gate 	 * to be fixed up front.
8310Sstevel@tonic-gate 	 */
8320Sstevel@tonic-gate 	if (stbuf.st_uid != 0) {
8330Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("%s is not owned by root.\n"),
8340Sstevel@tonic-gate 		    rpath);
8350Sstevel@tonic-gate 		err = B_TRUE;
8360Sstevel@tonic-gate 	}
8370Sstevel@tonic-gate 	err |= bad_mode_bit(stbuf.st_mode, S_IRUSR, B_TRUE, rpath);
8380Sstevel@tonic-gate 	err |= bad_mode_bit(stbuf.st_mode, S_IWUSR, B_TRUE, rpath);
8390Sstevel@tonic-gate 	err |= bad_mode_bit(stbuf.st_mode, S_IXUSR, B_TRUE, rpath);
8400Sstevel@tonic-gate 	err |= bad_mode_bit(stbuf.st_mode, S_IRGRP, B_FALSE, rpath);
8410Sstevel@tonic-gate 	err |= bad_mode_bit(stbuf.st_mode, S_IWGRP, B_FALSE, rpath);
8420Sstevel@tonic-gate 	err |= bad_mode_bit(stbuf.st_mode, S_IXGRP, B_FALSE, rpath);
8430Sstevel@tonic-gate 	err |= bad_mode_bit(stbuf.st_mode, S_IROTH, B_FALSE, rpath);
8440Sstevel@tonic-gate 	err |= bad_mode_bit(stbuf.st_mode, S_IWOTH, B_FALSE, rpath);
8450Sstevel@tonic-gate 	err |= bad_mode_bit(stbuf.st_mode, S_IXOTH, B_FALSE, rpath);
8460Sstevel@tonic-gate 
8470Sstevel@tonic-gate 	(void) snprintf(ppath, sizeof (ppath), "%s/..", path);
8480Sstevel@tonic-gate 	if ((res = resolvepath(ppath, rppath, sizeof (rppath))) == -1) {
8490Sstevel@tonic-gate 		zperror(ppath, B_FALSE);
8500Sstevel@tonic-gate 		return (Z_ERR);
8510Sstevel@tonic-gate 	}
8520Sstevel@tonic-gate 	rppath[res] = '\0';
8530Sstevel@tonic-gate 	if ((res = stat(rppath, &stbuf)) != 0) {
8540Sstevel@tonic-gate 		zperror(rppath, B_FALSE);
8550Sstevel@tonic-gate 		return (Z_ERR);
8560Sstevel@tonic-gate 	}
8570Sstevel@tonic-gate 	/* theoretically impossible */
8580Sstevel@tonic-gate 	if (!S_ISDIR(stbuf.st_mode)) {
8590Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("%s is not a directory.\n"),
8600Sstevel@tonic-gate 		    rppath);
8610Sstevel@tonic-gate 		return (Z_ERR);
8620Sstevel@tonic-gate 	}
8630Sstevel@tonic-gate 	if (stbuf.st_uid != 0) {
8640Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("%s is not owned by root.\n"),
8650Sstevel@tonic-gate 		    rppath);
8660Sstevel@tonic-gate 		err = B_TRUE;
8670Sstevel@tonic-gate 	}
8680Sstevel@tonic-gate 	err |= bad_mode_bit(stbuf.st_mode, S_IRUSR, B_TRUE, rppath);
8690Sstevel@tonic-gate 	err |= bad_mode_bit(stbuf.st_mode, S_IWUSR, B_TRUE, rppath);
8700Sstevel@tonic-gate 	err |= bad_mode_bit(stbuf.st_mode, S_IXUSR, B_TRUE, rppath);
8710Sstevel@tonic-gate 	err |= bad_mode_bit(stbuf.st_mode, S_IWGRP, B_FALSE, rppath);
8720Sstevel@tonic-gate 	err |= bad_mode_bit(stbuf.st_mode, S_IWOTH, B_FALSE, rppath);
8730Sstevel@tonic-gate 	if (strcmp(rpath, rppath) == 0) {
8740Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("%s is its own parent.\n"),
8750Sstevel@tonic-gate 		    rppath);
8760Sstevel@tonic-gate 		err = B_TRUE;
8770Sstevel@tonic-gate 	}
8780Sstevel@tonic-gate 
8790Sstevel@tonic-gate 	if (statvfs(rpath, &vfsbuf) != 0) {
8800Sstevel@tonic-gate 		zperror(rpath, B_FALSE);
8810Sstevel@tonic-gate 		return (Z_ERR);
8820Sstevel@tonic-gate 	}
883823Sgjelinek 	if (strcmp(vfsbuf.f_basetype, MNTTYPE_NFS) == 0) {
884924Sgjelinek 		/*
885924Sgjelinek 		 * TRANSLATION_NOTE
886924Sgjelinek 		 * Zonepath and NFS are literals that should not be translated.
887924Sgjelinek 		 */
888924Sgjelinek 		(void) fprintf(stderr, gettext("Zonepath %s is on an NFS "
889924Sgjelinek 		    "mounted file-system.\n"
890924Sgjelinek 		    "\tA local file-system must be used.\n"), rpath);
891823Sgjelinek 		return (Z_ERR);
892823Sgjelinek 	}
893823Sgjelinek 	if (vfsbuf.f_flag & ST_NOSUID) {
894924Sgjelinek 		/*
895924Sgjelinek 		 * TRANSLATION_NOTE
896924Sgjelinek 		 * Zonepath and nosuid are literals that should not be
897924Sgjelinek 		 * translated.
898924Sgjelinek 		 */
899823Sgjelinek 		(void) fprintf(stderr, gettext("Zonepath %s is on a nosuid "
900924Sgjelinek 		    "file-system.\n"), rpath);
9010Sstevel@tonic-gate 		return (Z_ERR);
9020Sstevel@tonic-gate 	}
9030Sstevel@tonic-gate 
9040Sstevel@tonic-gate 	if ((res = zone_get_state(target_zone, &state)) != Z_OK) {
9050Sstevel@tonic-gate 		errno = res;
9060Sstevel@tonic-gate 		zperror2(target_zone, gettext("could not get state"));
9070Sstevel@tonic-gate 		return (Z_ERR);
9080Sstevel@tonic-gate 	}
9090Sstevel@tonic-gate 	/*
9100Sstevel@tonic-gate 	 * The existence of the root path is only bad in the configured state,
9110Sstevel@tonic-gate 	 * as it is *supposed* to be there at the installed and later states.
9121507Sgjelinek 	 * However, the root path is expected to be there if the zone is
9131507Sgjelinek 	 * detached.
9140Sstevel@tonic-gate 	 * State/command mismatches are caught earlier in verify_details().
9150Sstevel@tonic-gate 	 */
9161507Sgjelinek 	if (state == ZONE_STATE_CONFIGURED && cmd_num != CMD_ATTACH) {
9170Sstevel@tonic-gate 		if (snprintf(rootpath, sizeof (rootpath), "%s/root", rpath) >=
9180Sstevel@tonic-gate 		    sizeof (rootpath)) {
919924Sgjelinek 			/*
920924Sgjelinek 			 * TRANSLATION_NOTE
921924Sgjelinek 			 * Zonepath is a literal that should not be translated.
922924Sgjelinek 			 */
9230Sstevel@tonic-gate 			(void) fprintf(stderr,
9240Sstevel@tonic-gate 			    gettext("Zonepath %s is too long.\n"), rpath);
9250Sstevel@tonic-gate 			return (Z_ERR);
9260Sstevel@tonic-gate 		}
9270Sstevel@tonic-gate 		if ((res = stat(rootpath, &stbuf)) == 0) {
9281507Sgjelinek 			if (zonecfg_detached(rpath))
9291507Sgjelinek 				(void) fprintf(stderr,
9301507Sgjelinek 				    gettext("Cannot %s detached "
9311507Sgjelinek 				    "zone.\nUse attach or remove %s "
9321507Sgjelinek 				    "directory.\n"), cmd_to_str(cmd_num),
9331507Sgjelinek 				    rpath);
9341507Sgjelinek 			else
9351507Sgjelinek 				(void) fprintf(stderr,
9361507Sgjelinek 				    gettext("Rootpath %s exists; "
9371507Sgjelinek 				    "remove or move aside prior to %s.\n"),
9381507Sgjelinek 				    rootpath, cmd_to_str(cmd_num));
9390Sstevel@tonic-gate 			return (Z_ERR);
9400Sstevel@tonic-gate 		}
9410Sstevel@tonic-gate 	}
9420Sstevel@tonic-gate 
9430Sstevel@tonic-gate 	return (err ? Z_ERR : Z_OK);
9440Sstevel@tonic-gate }
9450Sstevel@tonic-gate 
9460Sstevel@tonic-gate static void
9470Sstevel@tonic-gate release_lock_file(int lockfd)
9480Sstevel@tonic-gate {
9490Sstevel@tonic-gate 	(void) close(lockfd);
9500Sstevel@tonic-gate }
9510Sstevel@tonic-gate 
9520Sstevel@tonic-gate static int
9530Sstevel@tonic-gate grab_lock_file(const char *zone_name, int *lockfd)
9540Sstevel@tonic-gate {
9550Sstevel@tonic-gate 	char pathbuf[PATH_MAX];
9560Sstevel@tonic-gate 	struct flock flock;
9570Sstevel@tonic-gate 
958766Scarlsonj 	if (snprintf(pathbuf, sizeof (pathbuf), "%s%s", zonecfg_get_root(),
959766Scarlsonj 	    ZONES_TMPDIR) >= sizeof (pathbuf)) {
960766Scarlsonj 		zerror(gettext("alternate root path is too long"));
961766Scarlsonj 		return (Z_ERR);
962766Scarlsonj 	}
963766Scarlsonj 	if (mkdir(pathbuf, S_IRWXU) < 0 && errno != EEXIST) {
964766Scarlsonj 		zerror(gettext("could not mkdir %s: %s"), pathbuf,
9650Sstevel@tonic-gate 		    strerror(errno));
9660Sstevel@tonic-gate 		return (Z_ERR);
9670Sstevel@tonic-gate 	}
968766Scarlsonj 	(void) chmod(pathbuf, S_IRWXU);
9690Sstevel@tonic-gate 
9700Sstevel@tonic-gate 	/*
9710Sstevel@tonic-gate 	 * One of these lock files is created for each zone (when needed).
9720Sstevel@tonic-gate 	 * The lock files are not cleaned up (except on system reboot),
9730Sstevel@tonic-gate 	 * but since there is only one per zone, there is no resource
9740Sstevel@tonic-gate 	 * starvation issue.
9750Sstevel@tonic-gate 	 */
976766Scarlsonj 	if (snprintf(pathbuf, sizeof (pathbuf), "%s%s/%s.zoneadm.lock",
977766Scarlsonj 	    zonecfg_get_root(), ZONES_TMPDIR, zone_name) >= sizeof (pathbuf)) {
978766Scarlsonj 		zerror(gettext("alternate root path is too long"));
979766Scarlsonj 		return (Z_ERR);
980766Scarlsonj 	}
9810Sstevel@tonic-gate 	if ((*lockfd = open(pathbuf, O_RDWR|O_CREAT, S_IRUSR|S_IWUSR)) < 0) {
9820Sstevel@tonic-gate 		zerror(gettext("could not open %s: %s"), pathbuf,
9830Sstevel@tonic-gate 		    strerror(errno));
9840Sstevel@tonic-gate 		return (Z_ERR);
9850Sstevel@tonic-gate 	}
9860Sstevel@tonic-gate 	/*
9870Sstevel@tonic-gate 	 * Lock the file to synchronize with other zoneadmds
9880Sstevel@tonic-gate 	 */
9890Sstevel@tonic-gate 	flock.l_type = F_WRLCK;
9900Sstevel@tonic-gate 	flock.l_whence = SEEK_SET;
9910Sstevel@tonic-gate 	flock.l_start = (off_t)0;
9920Sstevel@tonic-gate 	flock.l_len = (off_t)0;
9930Sstevel@tonic-gate 	if (fcntl(*lockfd, F_SETLKW, &flock) < 0) {
9940Sstevel@tonic-gate 		zerror(gettext("unable to lock %s: %s"), pathbuf,
9950Sstevel@tonic-gate 		    strerror(errno));
9960Sstevel@tonic-gate 		release_lock_file(*lockfd);
9970Sstevel@tonic-gate 		return (Z_ERR);
9980Sstevel@tonic-gate 	}
9990Sstevel@tonic-gate 	return (Z_OK);
10000Sstevel@tonic-gate }
10010Sstevel@tonic-gate 
1002766Scarlsonj static boolean_t
10030Sstevel@tonic-gate get_doorname(const char *zone_name, char *buffer)
10040Sstevel@tonic-gate {
1005766Scarlsonj 	return (snprintf(buffer, PATH_MAX, "%s" ZONE_DOOR_PATH,
1006766Scarlsonj 	    zonecfg_get_root(), zone_name) < PATH_MAX);
10070Sstevel@tonic-gate }
10080Sstevel@tonic-gate 
10090Sstevel@tonic-gate /*
10100Sstevel@tonic-gate  * system daemons are not audited.  For the global zone, this occurs
10110Sstevel@tonic-gate  * "naturally" since init is started with the default audit
10120Sstevel@tonic-gate  * characteristics.  Since zoneadmd is a system daemon and it starts
10130Sstevel@tonic-gate  * init for a zone, it is necessary to clear out the audit
10140Sstevel@tonic-gate  * characteristics inherited from whomever started zoneadmd.  This is
10150Sstevel@tonic-gate  * indicated by the audit id, which is set from the ruid parameter of
10160Sstevel@tonic-gate  * adt_set_user(), below.
10170Sstevel@tonic-gate  */
10180Sstevel@tonic-gate 
10190Sstevel@tonic-gate static void
10200Sstevel@tonic-gate prepare_audit_context()
10210Sstevel@tonic-gate {
10220Sstevel@tonic-gate 	adt_session_data_t	*ah;
10230Sstevel@tonic-gate 	char			*failure = gettext("audit failure: %s");
10240Sstevel@tonic-gate 
10250Sstevel@tonic-gate 	if (adt_start_session(&ah, NULL, 0)) {
10260Sstevel@tonic-gate 		zerror(failure, strerror(errno));
10270Sstevel@tonic-gate 		return;
10280Sstevel@tonic-gate 	}
10290Sstevel@tonic-gate 	if (adt_set_user(ah, ADT_NO_AUDIT, ADT_NO_AUDIT,
10300Sstevel@tonic-gate 	    ADT_NO_AUDIT, ADT_NO_AUDIT, NULL, ADT_NEW)) {
10310Sstevel@tonic-gate 		zerror(failure, strerror(errno));
10320Sstevel@tonic-gate 		(void) adt_end_session(ah);
10330Sstevel@tonic-gate 		return;
10340Sstevel@tonic-gate 	}
10350Sstevel@tonic-gate 	if (adt_set_proc(ah))
10360Sstevel@tonic-gate 		zerror(failure, strerror(errno));
10370Sstevel@tonic-gate 
10380Sstevel@tonic-gate 	(void) adt_end_session(ah);
10390Sstevel@tonic-gate }
10400Sstevel@tonic-gate 
10410Sstevel@tonic-gate static int
10420Sstevel@tonic-gate start_zoneadmd(const char *zone_name)
10430Sstevel@tonic-gate {
10440Sstevel@tonic-gate 	char doorpath[PATH_MAX];
10450Sstevel@tonic-gate 	pid_t child_pid;
10460Sstevel@tonic-gate 	int error = Z_ERR;
10470Sstevel@tonic-gate 	int doorfd, lockfd;
10480Sstevel@tonic-gate 	struct door_info info;
10490Sstevel@tonic-gate 
1050766Scarlsonj 	if (!get_doorname(zone_name, doorpath))
1051766Scarlsonj 		return (Z_ERR);
10520Sstevel@tonic-gate 
10530Sstevel@tonic-gate 	if (grab_lock_file(zone_name, &lockfd) != Z_OK)
10540Sstevel@tonic-gate 		return (Z_ERR);
10550Sstevel@tonic-gate 
10560Sstevel@tonic-gate 	/*
10570Sstevel@tonic-gate 	 * Now that we have the lock, re-confirm that the daemon is
10580Sstevel@tonic-gate 	 * *not* up and working fine.  If it is still down, we have a green
10590Sstevel@tonic-gate 	 * light to start it.
10600Sstevel@tonic-gate 	 */
10610Sstevel@tonic-gate 	if ((doorfd = open(doorpath, O_RDONLY)) < 0) {
10620Sstevel@tonic-gate 		if (errno != ENOENT) {
10630Sstevel@tonic-gate 			zperror(doorpath, B_FALSE);
10640Sstevel@tonic-gate 			goto out;
10650Sstevel@tonic-gate 		}
10660Sstevel@tonic-gate 	} else {
10670Sstevel@tonic-gate 		if (door_info(doorfd, &info) == 0 &&
10680Sstevel@tonic-gate 		    ((info.di_attributes & DOOR_REVOKED) == 0)) {
10690Sstevel@tonic-gate 			error = Z_OK;
10700Sstevel@tonic-gate 			(void) close(doorfd);
10710Sstevel@tonic-gate 			goto out;
10720Sstevel@tonic-gate 		}
10730Sstevel@tonic-gate 		(void) close(doorfd);
10740Sstevel@tonic-gate 	}
10750Sstevel@tonic-gate 
10760Sstevel@tonic-gate 	if ((child_pid = fork()) == -1) {
10770Sstevel@tonic-gate 		zperror(gettext("could not fork"), B_FALSE);
10780Sstevel@tonic-gate 		goto out;
10790Sstevel@tonic-gate 	} else if (child_pid == 0) {
1080766Scarlsonj 		const char *argv[6], **ap;
1081766Scarlsonj 
10820Sstevel@tonic-gate 		/* child process */
10830Sstevel@tonic-gate 		prepare_audit_context();
10840Sstevel@tonic-gate 
1085766Scarlsonj 		ap = argv;
1086766Scarlsonj 		*ap++ = "zoneadmd";
1087766Scarlsonj 		*ap++ = "-z";
1088766Scarlsonj 		*ap++ = zone_name;
1089766Scarlsonj 		if (zonecfg_in_alt_root()) {
1090766Scarlsonj 			*ap++ = "-R";
1091766Scarlsonj 			*ap++ = zonecfg_get_root();
1092766Scarlsonj 		}
1093766Scarlsonj 		*ap = NULL;
1094766Scarlsonj 
1095766Scarlsonj 		(void) execv("/usr/lib/zones/zoneadmd", (char * const *)argv);
1096924Sgjelinek 		/*
1097924Sgjelinek 		 * TRANSLATION_NOTE
1098924Sgjelinek 		 * zoneadmd is a literal that should not be translated.
1099924Sgjelinek 		 */
11000Sstevel@tonic-gate 		zperror(gettext("could not exec zoneadmd"), B_FALSE);
11010Sstevel@tonic-gate 		_exit(Z_ERR);
11020Sstevel@tonic-gate 	} else {
11030Sstevel@tonic-gate 		/* parent process */
11040Sstevel@tonic-gate 		pid_t retval;
11050Sstevel@tonic-gate 		int pstatus = 0;
11060Sstevel@tonic-gate 
11070Sstevel@tonic-gate 		do {
11080Sstevel@tonic-gate 			retval = waitpid(child_pid, &pstatus, 0);
11090Sstevel@tonic-gate 		} while (retval != child_pid);
11100Sstevel@tonic-gate 		if (WIFSIGNALED(pstatus) || (WIFEXITED(pstatus) &&
11110Sstevel@tonic-gate 		    WEXITSTATUS(pstatus) != 0)) {
11120Sstevel@tonic-gate 			zerror(gettext("could not start %s"), "zoneadmd");
11130Sstevel@tonic-gate 			goto out;
11140Sstevel@tonic-gate 		}
11150Sstevel@tonic-gate 	}
11160Sstevel@tonic-gate 	error = Z_OK;
11170Sstevel@tonic-gate out:
11180Sstevel@tonic-gate 	release_lock_file(lockfd);
11190Sstevel@tonic-gate 	return (error);
11200Sstevel@tonic-gate }
11210Sstevel@tonic-gate 
11220Sstevel@tonic-gate static int
11230Sstevel@tonic-gate ping_zoneadmd(const char *zone_name)
11240Sstevel@tonic-gate {
11250Sstevel@tonic-gate 	char doorpath[PATH_MAX];
11260Sstevel@tonic-gate 	int doorfd;
11270Sstevel@tonic-gate 	struct door_info info;
11280Sstevel@tonic-gate 
1129766Scarlsonj 	if (!get_doorname(zone_name, doorpath))
1130766Scarlsonj 		return (Z_ERR);
11310Sstevel@tonic-gate 
11320Sstevel@tonic-gate 	if ((doorfd = open(doorpath, O_RDONLY)) < 0) {
11330Sstevel@tonic-gate 		return (Z_ERR);
11340Sstevel@tonic-gate 	}
11350Sstevel@tonic-gate 	if (door_info(doorfd, &info) == 0 &&
11360Sstevel@tonic-gate 	    ((info.di_attributes & DOOR_REVOKED) == 0)) {
11370Sstevel@tonic-gate 		(void) close(doorfd);
11380Sstevel@tonic-gate 		return (Z_OK);
11390Sstevel@tonic-gate 	}
11400Sstevel@tonic-gate 	(void) close(doorfd);
11410Sstevel@tonic-gate 	return (Z_ERR);
11420Sstevel@tonic-gate }
11430Sstevel@tonic-gate 
11440Sstevel@tonic-gate static int
11450Sstevel@tonic-gate call_zoneadmd(const char *zone_name, zone_cmd_arg_t *arg)
11460Sstevel@tonic-gate {
11470Sstevel@tonic-gate 	char doorpath[PATH_MAX];
11480Sstevel@tonic-gate 	int doorfd, result;
11490Sstevel@tonic-gate 	door_arg_t darg;
11500Sstevel@tonic-gate 
11510Sstevel@tonic-gate 	zoneid_t zoneid;
11520Sstevel@tonic-gate 	uint64_t uniqid = 0;
11530Sstevel@tonic-gate 
11540Sstevel@tonic-gate 	zone_cmd_rval_t *rvalp;
11550Sstevel@tonic-gate 	size_t rlen;
11560Sstevel@tonic-gate 	char *cp, *errbuf;
11570Sstevel@tonic-gate 
11580Sstevel@tonic-gate 	rlen = getpagesize();
11590Sstevel@tonic-gate 	if ((rvalp = malloc(rlen)) == NULL) {
11600Sstevel@tonic-gate 		zerror(gettext("failed to allocate %lu bytes: %s"), rlen,
11610Sstevel@tonic-gate 		    strerror(errno));
11620Sstevel@tonic-gate 		return (-1);
11630Sstevel@tonic-gate 	}
11640Sstevel@tonic-gate 
11650Sstevel@tonic-gate 	if ((zoneid = getzoneidbyname(zone_name)) != ZONE_ID_UNDEFINED) {
11660Sstevel@tonic-gate 		(void) zone_getattr(zoneid, ZONE_ATTR_UNIQID, &uniqid,
11670Sstevel@tonic-gate 		    sizeof (uniqid));
11680Sstevel@tonic-gate 	}
11690Sstevel@tonic-gate 	arg->uniqid = uniqid;
11700Sstevel@tonic-gate 	(void) strlcpy(arg->locale, locale, sizeof (arg->locale));
1171766Scarlsonj 	if (!get_doorname(zone_name, doorpath)) {
1172766Scarlsonj 		zerror(gettext("alternate root path is too long"));
1173766Scarlsonj 		free(rvalp);
1174766Scarlsonj 		return (-1);
1175766Scarlsonj 	}
11760Sstevel@tonic-gate 
11770Sstevel@tonic-gate 	/*
11780Sstevel@tonic-gate 	 * Loop trying to start zoneadmd; if something goes seriously
11790Sstevel@tonic-gate 	 * wrong we break out and fail.
11800Sstevel@tonic-gate 	 */
11810Sstevel@tonic-gate 	for (;;) {
11820Sstevel@tonic-gate 		if (start_zoneadmd(zone_name) != Z_OK)
11830Sstevel@tonic-gate 			break;
11840Sstevel@tonic-gate 
11850Sstevel@tonic-gate 		if ((doorfd = open(doorpath, O_RDONLY)) < 0) {
11860Sstevel@tonic-gate 			zperror(gettext("failed to open zone door"), B_FALSE);
11870Sstevel@tonic-gate 			break;
11880Sstevel@tonic-gate 		}
11890Sstevel@tonic-gate 
11900Sstevel@tonic-gate 		darg.data_ptr = (char *)arg;
11910Sstevel@tonic-gate 		darg.data_size = sizeof (*arg);
11920Sstevel@tonic-gate 		darg.desc_ptr = NULL;
11930Sstevel@tonic-gate 		darg.desc_num = 0;
11940Sstevel@tonic-gate 		darg.rbuf = (char *)rvalp;
11950Sstevel@tonic-gate 		darg.rsize = rlen;
11960Sstevel@tonic-gate 		if (door_call(doorfd, &darg) != 0) {
11970Sstevel@tonic-gate 			(void) close(doorfd);
11980Sstevel@tonic-gate 			/*
11990Sstevel@tonic-gate 			 * We'll get EBADF if the door has been revoked.
12000Sstevel@tonic-gate 			 */
12010Sstevel@tonic-gate 			if (errno != EBADF) {
12020Sstevel@tonic-gate 				zperror(gettext("door_call failed"), B_FALSE);
12030Sstevel@tonic-gate 				break;
12040Sstevel@tonic-gate 			}
12050Sstevel@tonic-gate 			continue;	/* take another lap */
12060Sstevel@tonic-gate 		}
12070Sstevel@tonic-gate 		(void) close(doorfd);
12080Sstevel@tonic-gate 
12090Sstevel@tonic-gate 		if (darg.data_size == 0) {
12100Sstevel@tonic-gate 			/* Door server is going away; kick it again. */
12110Sstevel@tonic-gate 			continue;
12120Sstevel@tonic-gate 		}
12130Sstevel@tonic-gate 
12140Sstevel@tonic-gate 		errbuf = rvalp->errbuf;
12150Sstevel@tonic-gate 		while (*errbuf != '\0') {
12160Sstevel@tonic-gate 			/*
12170Sstevel@tonic-gate 			 * Remove any newlines since zerror()
12180Sstevel@tonic-gate 			 * will append one automatically.
12190Sstevel@tonic-gate 			 */
12200Sstevel@tonic-gate 			cp = strchr(errbuf, '\n');
12210Sstevel@tonic-gate 			if (cp != NULL)
12220Sstevel@tonic-gate 				*cp = '\0';
12230Sstevel@tonic-gate 			zerror("%s", errbuf);
12240Sstevel@tonic-gate 			if (cp == NULL)
12250Sstevel@tonic-gate 				break;
12260Sstevel@tonic-gate 			errbuf = cp + 1;
12270Sstevel@tonic-gate 		}
12280Sstevel@tonic-gate 		result = rvalp->rval == 0 ? 0 : -1;
12290Sstevel@tonic-gate 		free(rvalp);
12300Sstevel@tonic-gate 		return (result);
12310Sstevel@tonic-gate 	}
12320Sstevel@tonic-gate 
12330Sstevel@tonic-gate 	free(rvalp);
12340Sstevel@tonic-gate 	return (-1);
12350Sstevel@tonic-gate }
12360Sstevel@tonic-gate 
12370Sstevel@tonic-gate static int
12380Sstevel@tonic-gate ready_func(int argc, char *argv[])
12390Sstevel@tonic-gate {
12400Sstevel@tonic-gate 	zone_cmd_arg_t zarg;
12410Sstevel@tonic-gate 	int arg;
12420Sstevel@tonic-gate 
1243766Scarlsonj 	if (zonecfg_in_alt_root()) {
1244766Scarlsonj 		zerror(gettext("cannot ready zone in alternate root"));
1245766Scarlsonj 		return (Z_ERR);
1246766Scarlsonj 	}
1247766Scarlsonj 
12480Sstevel@tonic-gate 	optind = 0;
12490Sstevel@tonic-gate 	if ((arg = getopt(argc, argv, "?")) != EOF) {
12500Sstevel@tonic-gate 		switch (arg) {
12510Sstevel@tonic-gate 		case '?':
12520Sstevel@tonic-gate 			sub_usage(SHELP_READY, CMD_READY);
12530Sstevel@tonic-gate 			return (optopt == '?' ? Z_OK : Z_USAGE);
12540Sstevel@tonic-gate 		default:
12550Sstevel@tonic-gate 			sub_usage(SHELP_READY, CMD_READY);
12560Sstevel@tonic-gate 			return (Z_USAGE);
12570Sstevel@tonic-gate 		}
12580Sstevel@tonic-gate 	}
12590Sstevel@tonic-gate 	if (argc > optind) {
12600Sstevel@tonic-gate 		sub_usage(SHELP_READY, CMD_READY);
12610Sstevel@tonic-gate 		return (Z_USAGE);
12620Sstevel@tonic-gate 	}
12630Sstevel@tonic-gate 	if (sanity_check(target_zone, CMD_READY, B_FALSE, B_FALSE) != Z_OK)
12640Sstevel@tonic-gate 		return (Z_ERR);
12650Sstevel@tonic-gate 	if (verify_details(CMD_READY) != Z_OK)
12660Sstevel@tonic-gate 		return (Z_ERR);
12670Sstevel@tonic-gate 
12680Sstevel@tonic-gate 	zarg.cmd = Z_READY;
12690Sstevel@tonic-gate 	if (call_zoneadmd(target_zone, &zarg) != 0) {
12700Sstevel@tonic-gate 		zerror(gettext("call to %s failed"), "zoneadmd");
12710Sstevel@tonic-gate 		return (Z_ERR);
12720Sstevel@tonic-gate 	}
12730Sstevel@tonic-gate 	return (Z_OK);
12740Sstevel@tonic-gate }
12750Sstevel@tonic-gate 
12760Sstevel@tonic-gate static int
12770Sstevel@tonic-gate boot_func(int argc, char *argv[])
12780Sstevel@tonic-gate {
12790Sstevel@tonic-gate 	zone_cmd_arg_t zarg;
12800Sstevel@tonic-gate 	int arg;
12810Sstevel@tonic-gate 
1282766Scarlsonj 	if (zonecfg_in_alt_root()) {
1283766Scarlsonj 		zerror(gettext("cannot boot zone in alternate root"));
1284766Scarlsonj 		return (Z_ERR);
1285766Scarlsonj 	}
1286766Scarlsonj 
12870Sstevel@tonic-gate 	zarg.bootbuf[0] = '\0';
12880Sstevel@tonic-gate 
12890Sstevel@tonic-gate 	/*
12900Sstevel@tonic-gate 	 * At the current time, the only supported subargument to the
12910Sstevel@tonic-gate 	 * "boot" subcommand is "-s" which specifies a single-user boot.
12920Sstevel@tonic-gate 	 * In the future, other boot arguments should be supported
12930Sstevel@tonic-gate 	 * including "-m" for specifying alternate smf(5) milestones.
12940Sstevel@tonic-gate 	 */
12950Sstevel@tonic-gate 	optind = 0;
12960Sstevel@tonic-gate 	if ((arg = getopt(argc, argv, "?s")) != EOF) {
12970Sstevel@tonic-gate 		switch (arg) {
12980Sstevel@tonic-gate 		case '?':
12990Sstevel@tonic-gate 			sub_usage(SHELP_BOOT, CMD_BOOT);
13000Sstevel@tonic-gate 			return (optopt == '?' ? Z_OK : Z_USAGE);
13010Sstevel@tonic-gate 		case 's':
13020Sstevel@tonic-gate 			(void) strlcpy(zarg.bootbuf, "-s",
13030Sstevel@tonic-gate 			    sizeof (zarg.bootbuf));
13040Sstevel@tonic-gate 			break;
13050Sstevel@tonic-gate 		default:
13060Sstevel@tonic-gate 			sub_usage(SHELP_BOOT, CMD_BOOT);
13070Sstevel@tonic-gate 			return (Z_USAGE);
13080Sstevel@tonic-gate 		}
13090Sstevel@tonic-gate 	}
13100Sstevel@tonic-gate 	if (argc > optind) {
13110Sstevel@tonic-gate 		sub_usage(SHELP_BOOT, CMD_BOOT);
13120Sstevel@tonic-gate 		return (Z_USAGE);
13130Sstevel@tonic-gate 	}
13140Sstevel@tonic-gate 	if (sanity_check(target_zone, CMD_BOOT, B_FALSE, B_FALSE) != Z_OK)
13150Sstevel@tonic-gate 		return (Z_ERR);
13160Sstevel@tonic-gate 	if (verify_details(CMD_BOOT) != Z_OK)
13170Sstevel@tonic-gate 		return (Z_ERR);
13180Sstevel@tonic-gate 	zarg.cmd = Z_BOOT;
13190Sstevel@tonic-gate 	if (call_zoneadmd(target_zone, &zarg) != 0) {
13200Sstevel@tonic-gate 		zerror(gettext("call to %s failed"), "zoneadmd");
13210Sstevel@tonic-gate 		return (Z_ERR);
13220Sstevel@tonic-gate 	}
13230Sstevel@tonic-gate 	return (Z_OK);
13240Sstevel@tonic-gate }
13250Sstevel@tonic-gate 
13260Sstevel@tonic-gate static void
13270Sstevel@tonic-gate fake_up_local_zone(zoneid_t zid, zone_entry_t *zeptr)
13280Sstevel@tonic-gate {
13290Sstevel@tonic-gate 	ssize_t result;
13300Sstevel@tonic-gate 
13310Sstevel@tonic-gate 	zeptr->zid = zid;
13320Sstevel@tonic-gate 	/*
13330Sstevel@tonic-gate 	 * Since we're looking up our own (non-global) zone name,
13340Sstevel@tonic-gate 	 * we can be assured that it will succeed.
13350Sstevel@tonic-gate 	 */
13360Sstevel@tonic-gate 	result = getzonenamebyid(zid, zeptr->zname, sizeof (zeptr->zname));
13370Sstevel@tonic-gate 	assert(result >= 0);
13380Sstevel@tonic-gate 	(void) strlcpy(zeptr->zroot, "/", sizeof (zeptr->zroot));
13390Sstevel@tonic-gate 	zeptr->zstate_str = "running";
13400Sstevel@tonic-gate }
13410Sstevel@tonic-gate 
13420Sstevel@tonic-gate static int
13430Sstevel@tonic-gate list_func(int argc, char *argv[])
13440Sstevel@tonic-gate {
13450Sstevel@tonic-gate 	zone_entry_t *zentp, zent;
1346766Scarlsonj 	int arg, retv;
13470Sstevel@tonic-gate 	boolean_t output = B_FALSE, verbose = B_FALSE, parsable = B_FALSE;
13480Sstevel@tonic-gate 	zone_state_t min_state = ZONE_STATE_RUNNING;
13490Sstevel@tonic-gate 	zoneid_t zone_id = getzoneid();
13500Sstevel@tonic-gate 
13510Sstevel@tonic-gate 	if (target_zone == NULL) {
13520Sstevel@tonic-gate 		/* all zones: default view to running but allow override */
13530Sstevel@tonic-gate 		optind = 0;
13540Sstevel@tonic-gate 		while ((arg = getopt(argc, argv, "?cipv")) != EOF) {
13550Sstevel@tonic-gate 			switch (arg) {
13560Sstevel@tonic-gate 			case '?':
13570Sstevel@tonic-gate 				sub_usage(SHELP_LIST, CMD_LIST);
13580Sstevel@tonic-gate 				return (optopt == '?' ? Z_OK : Z_USAGE);
13591339Sjonb 				/*
13601339Sjonb 				 * The 'i' and 'c' options are not mutually
13611339Sjonb 				 * exclusive so if 'c' is given, then min_state
13621339Sjonb 				 * is set to 0 (ZONE_STATE_CONFIGURED) which is
13631339Sjonb 				 * the lowest possible state.  If 'i' is given,
13641339Sjonb 				 * then min_state is set to be the lowest state
13651339Sjonb 				 * so far.
13661339Sjonb 				 */
13670Sstevel@tonic-gate 			case 'c':
13680Sstevel@tonic-gate 				min_state = ZONE_STATE_CONFIGURED;
13690Sstevel@tonic-gate 				break;
13700Sstevel@tonic-gate 			case 'i':
13711339Sjonb 				min_state = min(ZONE_STATE_INSTALLED,
13721339Sjonb 				    min_state);
13731339Sjonb 
13740Sstevel@tonic-gate 				break;
13750Sstevel@tonic-gate 			case 'p':
13760Sstevel@tonic-gate 				parsable = B_TRUE;
13770Sstevel@tonic-gate 				break;
13780Sstevel@tonic-gate 			case 'v':
13790Sstevel@tonic-gate 				verbose = B_TRUE;
13800Sstevel@tonic-gate 				break;
13810Sstevel@tonic-gate 			default:
13820Sstevel@tonic-gate 				sub_usage(SHELP_LIST, CMD_LIST);
13830Sstevel@tonic-gate 				return (Z_USAGE);
13840Sstevel@tonic-gate 			}
13850Sstevel@tonic-gate 		}
13860Sstevel@tonic-gate 		if (parsable && verbose) {
13870Sstevel@tonic-gate 			zerror(gettext("%s -p and -v are mutually exclusive."),
13880Sstevel@tonic-gate 			    cmd_to_str(CMD_LIST));
13890Sstevel@tonic-gate 			return (Z_ERR);
13900Sstevel@tonic-gate 		}
13910Sstevel@tonic-gate 		if (zone_id == GLOBAL_ZONEID) {
1392766Scarlsonj 			retv = zone_print_list(min_state, verbose, parsable);
13930Sstevel@tonic-gate 		} else {
1394766Scarlsonj 			retv = Z_OK;
13950Sstevel@tonic-gate 			fake_up_local_zone(zone_id, &zent);
13960Sstevel@tonic-gate 			zone_print(&zent, verbose, parsable);
13970Sstevel@tonic-gate 		}
1398766Scarlsonj 		return (retv);
13990Sstevel@tonic-gate 	}
14000Sstevel@tonic-gate 
14010Sstevel@tonic-gate 	/*
14020Sstevel@tonic-gate 	 * Specific target zone: disallow -i/-c suboptions.
14030Sstevel@tonic-gate 	 */
14040Sstevel@tonic-gate 	optind = 0;
14050Sstevel@tonic-gate 	while ((arg = getopt(argc, argv, "?pv")) != EOF) {
14060Sstevel@tonic-gate 		switch (arg) {
14070Sstevel@tonic-gate 		case '?':
14080Sstevel@tonic-gate 			sub_usage(SHELP_LIST, CMD_LIST);
14090Sstevel@tonic-gate 			return (optopt == '?' ? Z_OK : Z_USAGE);
14100Sstevel@tonic-gate 		case 'p':
14110Sstevel@tonic-gate 			parsable = B_TRUE;
14120Sstevel@tonic-gate 			break;
14130Sstevel@tonic-gate 		case 'v':
14140Sstevel@tonic-gate 			verbose = B_TRUE;
14150Sstevel@tonic-gate 			break;
14160Sstevel@tonic-gate 		default:
14170Sstevel@tonic-gate 			sub_usage(SHELP_LIST, CMD_LIST);
14180Sstevel@tonic-gate 			return (Z_USAGE);
14190Sstevel@tonic-gate 		}
14200Sstevel@tonic-gate 	}
14210Sstevel@tonic-gate 	if (parsable && verbose) {
14220Sstevel@tonic-gate 		zerror(gettext("%s -p and -v are mutually exclusive."),
14230Sstevel@tonic-gate 		    cmd_to_str(CMD_LIST));
14240Sstevel@tonic-gate 		return (Z_ERR);
14250Sstevel@tonic-gate 	}
14260Sstevel@tonic-gate 	if (argc > optind) {
14270Sstevel@tonic-gate 		sub_usage(SHELP_LIST, CMD_LIST);
14280Sstevel@tonic-gate 		return (Z_USAGE);
14290Sstevel@tonic-gate 	}
14300Sstevel@tonic-gate 	if (zone_id != GLOBAL_ZONEID) {
14310Sstevel@tonic-gate 		fake_up_local_zone(zone_id, &zent);
14320Sstevel@tonic-gate 		/*
14330Sstevel@tonic-gate 		 * main() will issue a Z_NO_ZONE error if it cannot get an
14340Sstevel@tonic-gate 		 * id for target_zone, which in a non-global zone should
14350Sstevel@tonic-gate 		 * happen for any zone name except `zonename`.  Thus we
14360Sstevel@tonic-gate 		 * assert() that here but don't otherwise check.
14370Sstevel@tonic-gate 		 */
14380Sstevel@tonic-gate 		assert(strcmp(zent.zname, target_zone) == 0);
14390Sstevel@tonic-gate 		zone_print(&zent, verbose, parsable);
14400Sstevel@tonic-gate 		output = B_TRUE;
14410Sstevel@tonic-gate 	} else if ((zentp = lookup_running_zone(target_zone)) != NULL) {
14420Sstevel@tonic-gate 		zone_print(zentp, verbose, parsable);
14430Sstevel@tonic-gate 		output = B_TRUE;
1444766Scarlsonj 	} else if (lookup_zone_info(target_zone, ZONE_ID_UNDEFINED,
1445766Scarlsonj 	    &zent) == Z_OK) {
14460Sstevel@tonic-gate 		zone_print(&zent, verbose, parsable);
14470Sstevel@tonic-gate 		output = B_TRUE;
14480Sstevel@tonic-gate 	}
14490Sstevel@tonic-gate 	return (output ? Z_OK : Z_ERR);
14500Sstevel@tonic-gate }
14510Sstevel@tonic-gate 
14520Sstevel@tonic-gate static void
14530Sstevel@tonic-gate sigterm(int sig)
14540Sstevel@tonic-gate {
14550Sstevel@tonic-gate 	/*
14560Sstevel@tonic-gate 	 * Ignore SIG{INT,TERM}, so we don't end up in an infinite loop,
14570Sstevel@tonic-gate 	 * then propagate the signal to our process group.
14580Sstevel@tonic-gate 	 */
14590Sstevel@tonic-gate 	(void) sigset(SIGINT, SIG_IGN);
14600Sstevel@tonic-gate 	(void) sigset(SIGTERM, SIG_IGN);
14610Sstevel@tonic-gate 	(void) kill(0, sig);
14620Sstevel@tonic-gate 	child_killed = B_TRUE;
14630Sstevel@tonic-gate }
14640Sstevel@tonic-gate 
14650Sstevel@tonic-gate static int
14660Sstevel@tonic-gate do_subproc(char *cmdbuf)
14670Sstevel@tonic-gate {
14680Sstevel@tonic-gate 	char inbuf[1024];	/* arbitrary large amount */
14690Sstevel@tonic-gate 	FILE *file;
14700Sstevel@tonic-gate 
14710Sstevel@tonic-gate 	child_killed = B_FALSE;
14720Sstevel@tonic-gate 	/*
14730Sstevel@tonic-gate 	 * We use popen(3c) to launch child processes for [un]install;
14740Sstevel@tonic-gate 	 * this library call does not return a PID, so we have to kill
14750Sstevel@tonic-gate 	 * the whole process group.  To avoid killing our parent, we
14760Sstevel@tonic-gate 	 * become a process group leader here.  But doing so can wreak
14770Sstevel@tonic-gate 	 * havoc with reading from stdin when launched by a non-job-control
14780Sstevel@tonic-gate 	 * shell, so we close stdin and reopen it as /dev/null first.
14790Sstevel@tonic-gate 	 */
14800Sstevel@tonic-gate 	(void) close(STDIN_FILENO);
14810Sstevel@tonic-gate 	(void) open("/dev/null", O_RDONLY);
14820Sstevel@tonic-gate 	(void) setpgid(0, 0);
14830Sstevel@tonic-gate 	(void) sigset(SIGINT, sigterm);
14840Sstevel@tonic-gate 	(void) sigset(SIGTERM, sigterm);
14850Sstevel@tonic-gate 	file = popen(cmdbuf, "r");
14860Sstevel@tonic-gate 	for (;;) {
14870Sstevel@tonic-gate 		if (child_killed || fgets(inbuf, sizeof (inbuf), file) == NULL)
14880Sstevel@tonic-gate 			break;
14890Sstevel@tonic-gate 		(void) fputs(inbuf, stdout);
14900Sstevel@tonic-gate 	}
14910Sstevel@tonic-gate 	(void) sigset(SIGINT, SIG_DFL);
14920Sstevel@tonic-gate 	(void) sigset(SIGTERM, SIG_DFL);
14930Sstevel@tonic-gate 	return (pclose(file));
14940Sstevel@tonic-gate }
14950Sstevel@tonic-gate 
14960Sstevel@tonic-gate static int
14970Sstevel@tonic-gate subproc_status(const char *cmd, int status)
14980Sstevel@tonic-gate {
14990Sstevel@tonic-gate 	if (WIFEXITED(status)) {
15000Sstevel@tonic-gate 		int exit_code = WEXITSTATUS(status);
15010Sstevel@tonic-gate 
15020Sstevel@tonic-gate 		if (exit_code == 0)
15030Sstevel@tonic-gate 			return (Z_OK);
15040Sstevel@tonic-gate 		zerror(gettext("'%s' failed with exit code %d."), cmd,
15050Sstevel@tonic-gate 		    exit_code);
15060Sstevel@tonic-gate 	} else if (WIFSIGNALED(status)) {
15070Sstevel@tonic-gate 		int signal = WTERMSIG(status);
15080Sstevel@tonic-gate 		char sigstr[SIG2STR_MAX];
15090Sstevel@tonic-gate 
15100Sstevel@tonic-gate 		if (sig2str(signal, sigstr) == 0) {
15110Sstevel@tonic-gate 			zerror(gettext("'%s' terminated by signal SIG%s."), cmd,
15120Sstevel@tonic-gate 			    sigstr);
15130Sstevel@tonic-gate 		} else {
15140Sstevel@tonic-gate 			zerror(gettext("'%s' terminated by an unknown signal."),
15150Sstevel@tonic-gate 			    cmd);
15160Sstevel@tonic-gate 		}
15170Sstevel@tonic-gate 	} else {
15180Sstevel@tonic-gate 		zerror(gettext("'%s' failed for unknown reasons."), cmd);
15190Sstevel@tonic-gate 	}
15200Sstevel@tonic-gate 	return (Z_ERR);
15210Sstevel@tonic-gate }
15220Sstevel@tonic-gate 
15230Sstevel@tonic-gate /*
15240Sstevel@tonic-gate  * Various sanity checks; make sure:
15250Sstevel@tonic-gate  * 1. We're in the global zone.
15260Sstevel@tonic-gate  * 2. The calling user has sufficient privilege.
15270Sstevel@tonic-gate  * 3. The target zone is neither the global zone nor anything starting with
15280Sstevel@tonic-gate  *    "SUNW".
15290Sstevel@tonic-gate  * 4a. If we're looking for a 'not running' (i.e., configured or installed)
15300Sstevel@tonic-gate  *     zone, the name service knows about it.
15310Sstevel@tonic-gate  * 4b. For some operations which expect a zone not to be running, that it is
15320Sstevel@tonic-gate  *     not already running (or ready).
15330Sstevel@tonic-gate  */
15340Sstevel@tonic-gate static int
15350Sstevel@tonic-gate sanity_check(char *zone, int cmd_num, boolean_t running,
15360Sstevel@tonic-gate     boolean_t unsafe_when_running)
15370Sstevel@tonic-gate {
15380Sstevel@tonic-gate 	zone_entry_t *zent;
15390Sstevel@tonic-gate 	priv_set_t *privset;
15400Sstevel@tonic-gate 	zone_state_t state;
1541766Scarlsonj 	char kernzone[ZONENAME_MAX];
1542766Scarlsonj 	FILE *fp;
15430Sstevel@tonic-gate 
15440Sstevel@tonic-gate 	if (getzoneid() != GLOBAL_ZONEID) {
15450Sstevel@tonic-gate 		zerror(gettext("must be in the global zone to %s a zone."),
15460Sstevel@tonic-gate 		    cmd_to_str(cmd_num));
15470Sstevel@tonic-gate 		return (Z_ERR);
15480Sstevel@tonic-gate 	}
15490Sstevel@tonic-gate 
15500Sstevel@tonic-gate 	if ((privset = priv_allocset()) == NULL) {
15510Sstevel@tonic-gate 		zerror(gettext("%s failed"), "priv_allocset");
15520Sstevel@tonic-gate 		return (Z_ERR);
15530Sstevel@tonic-gate 	}
15540Sstevel@tonic-gate 
15550Sstevel@tonic-gate 	if (getppriv(PRIV_EFFECTIVE, privset) != 0) {
15560Sstevel@tonic-gate 		zerror(gettext("%s failed"), "getppriv");
15570Sstevel@tonic-gate 		priv_freeset(privset);
15580Sstevel@tonic-gate 		return (Z_ERR);
15590Sstevel@tonic-gate 	}
15600Sstevel@tonic-gate 
15610Sstevel@tonic-gate 	if (priv_isfullset(privset) == B_FALSE) {
15620Sstevel@tonic-gate 		zerror(gettext("only a privileged user may %s a zone."),
15630Sstevel@tonic-gate 		    cmd_to_str(cmd_num));
15640Sstevel@tonic-gate 		priv_freeset(privset);
15650Sstevel@tonic-gate 		return (Z_ERR);
15660Sstevel@tonic-gate 	}
15670Sstevel@tonic-gate 	priv_freeset(privset);
15680Sstevel@tonic-gate 
15690Sstevel@tonic-gate 	if (zone == NULL) {
15700Sstevel@tonic-gate 		zerror(gettext("no zone specified"));
15710Sstevel@tonic-gate 		return (Z_ERR);
15720Sstevel@tonic-gate 	}
15730Sstevel@tonic-gate 
15740Sstevel@tonic-gate 	if (strcmp(zone, GLOBAL_ZONENAME) == 0) {
15750Sstevel@tonic-gate 		zerror(gettext("%s operation is invalid for the global zone."),
15760Sstevel@tonic-gate 		    cmd_to_str(cmd_num));
15770Sstevel@tonic-gate 		return (Z_ERR);
15780Sstevel@tonic-gate 	}
15790Sstevel@tonic-gate 
15800Sstevel@tonic-gate 	if (strncmp(zone, "SUNW", 4) == 0) {
15810Sstevel@tonic-gate 		zerror(gettext("%s operation is invalid for zones starting "
15820Sstevel@tonic-gate 		    "with SUNW."), cmd_to_str(cmd_num));
15830Sstevel@tonic-gate 		return (Z_ERR);
15840Sstevel@tonic-gate 	}
15850Sstevel@tonic-gate 
1586766Scarlsonj 	if (!zonecfg_in_alt_root()) {
1587766Scarlsonj 		zent = lookup_running_zone(zone);
1588766Scarlsonj 	} else if ((fp = zonecfg_open_scratch("", B_FALSE)) == NULL) {
1589766Scarlsonj 		zent = NULL;
1590766Scarlsonj 	} else {
1591766Scarlsonj 		if (zonecfg_find_scratch(fp, zone, zonecfg_get_root(),
1592766Scarlsonj 		    kernzone, sizeof (kernzone)) == 0)
1593766Scarlsonj 			zent = lookup_running_zone(kernzone);
1594766Scarlsonj 		else
1595766Scarlsonj 			zent = NULL;
1596766Scarlsonj 		zonecfg_close_scratch(fp);
1597766Scarlsonj 	}
1598766Scarlsonj 
15990Sstevel@tonic-gate 	/*
16000Sstevel@tonic-gate 	 * Look up from the kernel for 'running' zones.
16010Sstevel@tonic-gate 	 */
16020Sstevel@tonic-gate 	if (running) {
16030Sstevel@tonic-gate 		if (zent == NULL) {
16040Sstevel@tonic-gate 			zerror(gettext("not running"));
16050Sstevel@tonic-gate 			return (Z_ERR);
16060Sstevel@tonic-gate 		}
16070Sstevel@tonic-gate 	} else {
16080Sstevel@tonic-gate 		int err;
16090Sstevel@tonic-gate 
16100Sstevel@tonic-gate 		if (unsafe_when_running && zent != NULL) {
16110Sstevel@tonic-gate 			/* check whether the zone is ready or running */
16120Sstevel@tonic-gate 			if ((err = zone_get_state(zent->zname,
16130Sstevel@tonic-gate 			    &zent->zstate_num)) != Z_OK) {
16140Sstevel@tonic-gate 				errno = err;
16150Sstevel@tonic-gate 				zperror2(zent->zname,
16160Sstevel@tonic-gate 				    gettext("could not get state"));
16170Sstevel@tonic-gate 				/* can't tell, so hedge */
16180Sstevel@tonic-gate 				zent->zstate_str = "ready/running";
16190Sstevel@tonic-gate 			} else {
16200Sstevel@tonic-gate 				zent->zstate_str =
16210Sstevel@tonic-gate 				    zone_state_str(zent->zstate_num);
16220Sstevel@tonic-gate 			}
16230Sstevel@tonic-gate 			zerror(gettext("%s operation is invalid for %s zones."),
16240Sstevel@tonic-gate 			    cmd_to_str(cmd_num), zent->zstate_str);
16250Sstevel@tonic-gate 			return (Z_ERR);
16260Sstevel@tonic-gate 		}
16270Sstevel@tonic-gate 		if ((err = zone_get_state(zone, &state)) != Z_OK) {
16280Sstevel@tonic-gate 			errno = err;
16290Sstevel@tonic-gate 			zperror2(zone, gettext("could not get state"));
16300Sstevel@tonic-gate 			return (Z_ERR);
16310Sstevel@tonic-gate 		}
16320Sstevel@tonic-gate 		switch (cmd_num) {
16330Sstevel@tonic-gate 		case CMD_UNINSTALL:
16340Sstevel@tonic-gate 			if (state == ZONE_STATE_CONFIGURED) {
16350Sstevel@tonic-gate 				zerror(gettext("is already in state '%s'."),
16360Sstevel@tonic-gate 				    zone_state_str(ZONE_STATE_CONFIGURED));
16370Sstevel@tonic-gate 				return (Z_ERR);
16380Sstevel@tonic-gate 			}
16390Sstevel@tonic-gate 			break;
16401507Sgjelinek 		case CMD_ATTACH:
16411300Sgjelinek 		case CMD_CLONE:
16420Sstevel@tonic-gate 		case CMD_INSTALL:
16430Sstevel@tonic-gate 			if (state == ZONE_STATE_INSTALLED) {
16440Sstevel@tonic-gate 				zerror(gettext("is already %s."),
16450Sstevel@tonic-gate 				    zone_state_str(ZONE_STATE_INSTALLED));
16460Sstevel@tonic-gate 				return (Z_ERR);
16470Sstevel@tonic-gate 			} else if (state == ZONE_STATE_INCOMPLETE) {
16480Sstevel@tonic-gate 				zerror(gettext("zone is %s; %s required."),
16490Sstevel@tonic-gate 				    zone_state_str(ZONE_STATE_INCOMPLETE),
16500Sstevel@tonic-gate 				    cmd_to_str(CMD_UNINSTALL));
16510Sstevel@tonic-gate 				return (Z_ERR);
16520Sstevel@tonic-gate 			}
16530Sstevel@tonic-gate 			break;
16541507Sgjelinek 		case CMD_DETACH:
16551300Sgjelinek 		case CMD_MOVE:
16560Sstevel@tonic-gate 		case CMD_READY:
16570Sstevel@tonic-gate 		case CMD_BOOT:
1658766Scarlsonj 		case CMD_MOUNT:
16590Sstevel@tonic-gate 			if (state < ZONE_STATE_INSTALLED) {
16600Sstevel@tonic-gate 				zerror(gettext("must be %s before %s."),
16610Sstevel@tonic-gate 				    zone_state_str(ZONE_STATE_INSTALLED),
16620Sstevel@tonic-gate 				    cmd_to_str(cmd_num));
16630Sstevel@tonic-gate 				return (Z_ERR);
16640Sstevel@tonic-gate 			}
16650Sstevel@tonic-gate 			break;
16660Sstevel@tonic-gate 		case CMD_VERIFY:
16670Sstevel@tonic-gate 			if (state == ZONE_STATE_INCOMPLETE) {
16680Sstevel@tonic-gate 				zerror(gettext("zone is %s; %s required."),
16690Sstevel@tonic-gate 				    zone_state_str(ZONE_STATE_INCOMPLETE),
16700Sstevel@tonic-gate 				    cmd_to_str(CMD_UNINSTALL));
16710Sstevel@tonic-gate 				return (Z_ERR);
16720Sstevel@tonic-gate 			}
16730Sstevel@tonic-gate 			break;
1674766Scarlsonj 		case CMD_UNMOUNT:
1675766Scarlsonj 			if (state != ZONE_STATE_MOUNTED) {
1676766Scarlsonj 				zerror(gettext("must be %s before %s."),
1677766Scarlsonj 				    zone_state_str(ZONE_STATE_MOUNTED),
1678766Scarlsonj 				    cmd_to_str(cmd_num));
1679766Scarlsonj 				return (Z_ERR);
1680766Scarlsonj 			}
1681766Scarlsonj 			break;
16820Sstevel@tonic-gate 		}
16830Sstevel@tonic-gate 	}
16840Sstevel@tonic-gate 	return (Z_OK);
16850Sstevel@tonic-gate }
16860Sstevel@tonic-gate 
16870Sstevel@tonic-gate static int
16880Sstevel@tonic-gate halt_func(int argc, char *argv[])
16890Sstevel@tonic-gate {
16900Sstevel@tonic-gate 	zone_cmd_arg_t zarg;
16910Sstevel@tonic-gate 	int arg;
16920Sstevel@tonic-gate 
1693766Scarlsonj 	if (zonecfg_in_alt_root()) {
1694766Scarlsonj 		zerror(gettext("cannot halt zone in alternate root"));
1695766Scarlsonj 		return (Z_ERR);
1696766Scarlsonj 	}
1697766Scarlsonj 
16980Sstevel@tonic-gate 	optind = 0;
16990Sstevel@tonic-gate 	if ((arg = getopt(argc, argv, "?")) != EOF) {
17000Sstevel@tonic-gate 		switch (arg) {
17010Sstevel@tonic-gate 		case '?':
17020Sstevel@tonic-gate 			sub_usage(SHELP_HALT, CMD_HALT);
17030Sstevel@tonic-gate 			return (optopt == '?' ? Z_OK : Z_USAGE);
17040Sstevel@tonic-gate 		default:
17050Sstevel@tonic-gate 			sub_usage(SHELP_HALT, CMD_HALT);
17060Sstevel@tonic-gate 			return (Z_USAGE);
17070Sstevel@tonic-gate 		}
17080Sstevel@tonic-gate 	}
17090Sstevel@tonic-gate 	if (argc > optind) {
17100Sstevel@tonic-gate 		sub_usage(SHELP_HALT, CMD_HALT);
17110Sstevel@tonic-gate 		return (Z_USAGE);
17120Sstevel@tonic-gate 	}
17130Sstevel@tonic-gate 	/*
17140Sstevel@tonic-gate 	 * zoneadmd should be the one to decide whether or not to proceed,
17150Sstevel@tonic-gate 	 * so even though it seems that the fourth parameter below should
17160Sstevel@tonic-gate 	 * perhaps be B_TRUE, it really shouldn't be.
17170Sstevel@tonic-gate 	 */
17180Sstevel@tonic-gate 	if (sanity_check(target_zone, CMD_HALT, B_FALSE, B_FALSE) != Z_OK)
17190Sstevel@tonic-gate 		return (Z_ERR);
17200Sstevel@tonic-gate 
17210Sstevel@tonic-gate 	zarg.cmd = Z_HALT;
17220Sstevel@tonic-gate 	return ((call_zoneadmd(target_zone, &zarg) == 0) ? Z_OK : Z_ERR);
17230Sstevel@tonic-gate }
17240Sstevel@tonic-gate 
17250Sstevel@tonic-gate static int
17260Sstevel@tonic-gate reboot_func(int argc, char *argv[])
17270Sstevel@tonic-gate {
17280Sstevel@tonic-gate 	zone_cmd_arg_t zarg;
17290Sstevel@tonic-gate 	int arg;
17300Sstevel@tonic-gate 
1731766Scarlsonj 	if (zonecfg_in_alt_root()) {
1732766Scarlsonj 		zerror(gettext("cannot reboot zone in alternate root"));
1733766Scarlsonj 		return (Z_ERR);
1734766Scarlsonj 	}
1735766Scarlsonj 
17360Sstevel@tonic-gate 	optind = 0;
17370Sstevel@tonic-gate 	if ((arg = getopt(argc, argv, "?")) != EOF) {
17380Sstevel@tonic-gate 		switch (arg) {
17390Sstevel@tonic-gate 		case '?':
17400Sstevel@tonic-gate 			sub_usage(SHELP_REBOOT, CMD_REBOOT);
17410Sstevel@tonic-gate 			return (optopt == '?' ? Z_OK : Z_USAGE);
17420Sstevel@tonic-gate 		default:
17430Sstevel@tonic-gate 			sub_usage(SHELP_REBOOT, CMD_REBOOT);
17440Sstevel@tonic-gate 			return (Z_USAGE);
17450Sstevel@tonic-gate 		}
17460Sstevel@tonic-gate 	}
17470Sstevel@tonic-gate 	if (argc > 0) {
17480Sstevel@tonic-gate 		sub_usage(SHELP_REBOOT, CMD_REBOOT);
17490Sstevel@tonic-gate 		return (Z_USAGE);
17500Sstevel@tonic-gate 	}
17510Sstevel@tonic-gate 	/*
17520Sstevel@tonic-gate 	 * zoneadmd should be the one to decide whether or not to proceed,
17530Sstevel@tonic-gate 	 * so even though it seems that the fourth parameter below should
17540Sstevel@tonic-gate 	 * perhaps be B_TRUE, it really shouldn't be.
17550Sstevel@tonic-gate 	 */
17560Sstevel@tonic-gate 	if (sanity_check(target_zone, CMD_REBOOT, B_TRUE, B_FALSE) != Z_OK)
17570Sstevel@tonic-gate 		return (Z_ERR);
1758823Sgjelinek 	if (verify_details(CMD_REBOOT) != Z_OK)
1759823Sgjelinek 		return (Z_ERR);
17600Sstevel@tonic-gate 
17610Sstevel@tonic-gate 	zarg.cmd = Z_REBOOT;
17620Sstevel@tonic-gate 	return ((call_zoneadmd(target_zone, &zarg) == 0) ? Z_OK : Z_ERR);
17630Sstevel@tonic-gate }
17640Sstevel@tonic-gate 
17650Sstevel@tonic-gate static int
17660Sstevel@tonic-gate verify_rctls(zone_dochandle_t handle)
17670Sstevel@tonic-gate {
17680Sstevel@tonic-gate 	struct zone_rctltab rctltab;
17690Sstevel@tonic-gate 	size_t rbs = rctlblk_size();
17700Sstevel@tonic-gate 	rctlblk_t *rctlblk;
17710Sstevel@tonic-gate 	int error = Z_INVAL;
17720Sstevel@tonic-gate 
17730Sstevel@tonic-gate 	if ((rctlblk = malloc(rbs)) == NULL) {
17740Sstevel@tonic-gate 		zerror(gettext("failed to allocate %lu bytes: %s"), rbs,
17750Sstevel@tonic-gate 		    strerror(errno));
17760Sstevel@tonic-gate 		return (Z_NOMEM);
17770Sstevel@tonic-gate 	}
17780Sstevel@tonic-gate 
17790Sstevel@tonic-gate 	if (zonecfg_setrctlent(handle) != Z_OK) {
17800Sstevel@tonic-gate 		zerror(gettext("zonecfg_setrctlent failed"));
17810Sstevel@tonic-gate 		free(rctlblk);
17820Sstevel@tonic-gate 		return (error);
17830Sstevel@tonic-gate 	}
17840Sstevel@tonic-gate 
17850Sstevel@tonic-gate 	rctltab.zone_rctl_valptr = NULL;
17860Sstevel@tonic-gate 	while (zonecfg_getrctlent(handle, &rctltab) == Z_OK) {
17870Sstevel@tonic-gate 		struct zone_rctlvaltab *rctlval;
17880Sstevel@tonic-gate 		const char *name = rctltab.zone_rctl_name;
17890Sstevel@tonic-gate 
17900Sstevel@tonic-gate 		if (!zonecfg_is_rctl(name)) {
17910Sstevel@tonic-gate 			zerror(gettext("WARNING: Ignoring unrecognized rctl "
17920Sstevel@tonic-gate 			    "'%s'."),  name);
17930Sstevel@tonic-gate 			zonecfg_free_rctl_value_list(rctltab.zone_rctl_valptr);
17940Sstevel@tonic-gate 			rctltab.zone_rctl_valptr = NULL;
17950Sstevel@tonic-gate 			continue;
17960Sstevel@tonic-gate 		}
17970Sstevel@tonic-gate 
17980Sstevel@tonic-gate 		for (rctlval = rctltab.zone_rctl_valptr; rctlval != NULL;
17990Sstevel@tonic-gate 		    rctlval = rctlval->zone_rctlval_next) {
18000Sstevel@tonic-gate 			if (zonecfg_construct_rctlblk(rctlval, rctlblk)
18010Sstevel@tonic-gate 			    != Z_OK) {
18020Sstevel@tonic-gate 				zerror(gettext("invalid rctl value: "
18030Sstevel@tonic-gate 				    "(priv=%s,limit=%s,action%s)"),
18040Sstevel@tonic-gate 				    rctlval->zone_rctlval_priv,
18050Sstevel@tonic-gate 				    rctlval->zone_rctlval_limit,
18060Sstevel@tonic-gate 				    rctlval->zone_rctlval_action);
18070Sstevel@tonic-gate 				goto out;
18080Sstevel@tonic-gate 			}
18090Sstevel@tonic-gate 			if (!zonecfg_valid_rctl(name, rctlblk)) {
18100Sstevel@tonic-gate 				zerror(gettext("(priv=%s,limit=%s,action=%s) "
18110Sstevel@tonic-gate 				    "is not a valid value for rctl '%s'"),
18120Sstevel@tonic-gate 				    rctlval->zone_rctlval_priv,
18130Sstevel@tonic-gate 				    rctlval->zone_rctlval_limit,
18140Sstevel@tonic-gate 				    rctlval->zone_rctlval_action,
18150Sstevel@tonic-gate 				    name);
18160Sstevel@tonic-gate 				goto out;
18170Sstevel@tonic-gate 			}
18180Sstevel@tonic-gate 		}
18190Sstevel@tonic-gate 		zonecfg_free_rctl_value_list(rctltab.zone_rctl_valptr);
18200Sstevel@tonic-gate 	}
18210Sstevel@tonic-gate 	rctltab.zone_rctl_valptr = NULL;
18220Sstevel@tonic-gate 	error = Z_OK;
18230Sstevel@tonic-gate out:
18240Sstevel@tonic-gate 	zonecfg_free_rctl_value_list(rctltab.zone_rctl_valptr);
18250Sstevel@tonic-gate 	(void) zonecfg_endrctlent(handle);
18260Sstevel@tonic-gate 	free(rctlblk);
18270Sstevel@tonic-gate 	return (error);
18280Sstevel@tonic-gate }
18290Sstevel@tonic-gate 
18300Sstevel@tonic-gate static int
18310Sstevel@tonic-gate verify_pool(zone_dochandle_t handle)
18320Sstevel@tonic-gate {
18330Sstevel@tonic-gate 	char poolname[MAXPATHLEN];
18340Sstevel@tonic-gate 	pool_conf_t *poolconf;
18350Sstevel@tonic-gate 	pool_t *pool;
18360Sstevel@tonic-gate 	int status;
18370Sstevel@tonic-gate 	int error;
18380Sstevel@tonic-gate 
18390Sstevel@tonic-gate 	/*
18400Sstevel@tonic-gate 	 * This ends up being very similar to the check done in zoneadmd.
18410Sstevel@tonic-gate 	 */
18420Sstevel@tonic-gate 	error = zonecfg_get_pool(handle, poolname, sizeof (poolname));
18430Sstevel@tonic-gate 	if (error == Z_NO_ENTRY || (error == Z_OK && strlen(poolname) == 0)) {
18440Sstevel@tonic-gate 		/*
18450Sstevel@tonic-gate 		 * No pool specified.
18460Sstevel@tonic-gate 		 */
18470Sstevel@tonic-gate 		return (0);
18480Sstevel@tonic-gate 	}
18490Sstevel@tonic-gate 	if (error != Z_OK) {
18500Sstevel@tonic-gate 		zperror(gettext("Unable to retrieve pool name from "
18510Sstevel@tonic-gate 		    "configuration"), B_TRUE);
18520Sstevel@tonic-gate 		return (error);
18530Sstevel@tonic-gate 	}
18540Sstevel@tonic-gate 	/*
18550Sstevel@tonic-gate 	 * Don't do anything if pools aren't enabled.
18560Sstevel@tonic-gate 	 */
18570Sstevel@tonic-gate 	if (pool_get_status(&status) != PO_SUCCESS || status != POOL_ENABLED) {
18580Sstevel@tonic-gate 		zerror(gettext("WARNING: pools facility not active; "
18590Sstevel@tonic-gate 		    "zone will not be bound to pool '%s'."), poolname);
18600Sstevel@tonic-gate 		return (Z_OK);
18610Sstevel@tonic-gate 	}
18620Sstevel@tonic-gate 	/*
18630Sstevel@tonic-gate 	 * Try to provide a sane error message if the requested pool doesn't
18640Sstevel@tonic-gate 	 * exist.  It isn't clear that pools-related failures should
18650Sstevel@tonic-gate 	 * necessarily translate to a failure to verify the zone configuration,
18660Sstevel@tonic-gate 	 * hence they are not considered errors.
18670Sstevel@tonic-gate 	 */
18680Sstevel@tonic-gate 	if ((poolconf = pool_conf_alloc()) == NULL) {
18690Sstevel@tonic-gate 		zerror(gettext("WARNING: pool_conf_alloc failed; "
18700Sstevel@tonic-gate 		    "using default pool"));
18710Sstevel@tonic-gate 		return (Z_OK);
18720Sstevel@tonic-gate 	}
18730Sstevel@tonic-gate 	if (pool_conf_open(poolconf, pool_dynamic_location(), PO_RDONLY) !=
18740Sstevel@tonic-gate 	    PO_SUCCESS) {
18750Sstevel@tonic-gate 		zerror(gettext("WARNING: pool_conf_open failed; "
18760Sstevel@tonic-gate 		    "using default pool"));
18770Sstevel@tonic-gate 		pool_conf_free(poolconf);
18780Sstevel@tonic-gate 		return (Z_OK);
18790Sstevel@tonic-gate 	}
18800Sstevel@tonic-gate 	pool = pool_get_pool(poolconf, poolname);
18810Sstevel@tonic-gate 	(void) pool_conf_close(poolconf);
18820Sstevel@tonic-gate 	pool_conf_free(poolconf);
18830Sstevel@tonic-gate 	if (pool == NULL) {
18840Sstevel@tonic-gate 		zerror(gettext("WARNING: pool '%s' not found. "
18850Sstevel@tonic-gate 		    "using default pool"), poolname);
18860Sstevel@tonic-gate 	}
18870Sstevel@tonic-gate 
18880Sstevel@tonic-gate 	return (Z_OK);
18890Sstevel@tonic-gate }
18900Sstevel@tonic-gate 
18910Sstevel@tonic-gate static int
1892823Sgjelinek verify_ipd(zone_dochandle_t handle)
1893823Sgjelinek {
1894823Sgjelinek 	int return_code = Z_OK;
1895823Sgjelinek 	struct zone_fstab fstab;
1896823Sgjelinek 	struct stat st;
1897823Sgjelinek 	char specdir[MAXPATHLEN];
1898823Sgjelinek 
1899823Sgjelinek 	if (zonecfg_setipdent(handle) != Z_OK) {
1900924Sgjelinek 		/*
1901924Sgjelinek 		 * TRANSLATION_NOTE
1902924Sgjelinek 		 * inherit-pkg-dirs is a literal that should not be translated.
1903924Sgjelinek 		 */
1904924Sgjelinek 		(void) fprintf(stderr, gettext("could not verify "
1905823Sgjelinek 		    "inherit-pkg-dirs: unable to enumerate mounts\n"));
1906823Sgjelinek 		return (Z_ERR);
1907823Sgjelinek 	}
1908823Sgjelinek 	while (zonecfg_getipdent(handle, &fstab) == Z_OK) {
1909823Sgjelinek 		/*
1910823Sgjelinek 		 * Verify fs_dir exists.
1911823Sgjelinek 		 */
1912823Sgjelinek 		(void) snprintf(specdir, sizeof (specdir), "%s%s",
1913823Sgjelinek 		    zonecfg_get_root(), fstab.zone_fs_dir);
1914823Sgjelinek 		if (stat(specdir, &st) != 0) {
1915924Sgjelinek 			/*
1916924Sgjelinek 			 * TRANSLATION_NOTE
1917924Sgjelinek 			 * inherit-pkg-dir is a literal that should not be
1918924Sgjelinek 			 * translated.
1919924Sgjelinek 			 */
1920924Sgjelinek 			(void) fprintf(stderr, gettext("could not verify "
1921823Sgjelinek 			    "inherit-pkg-dir %s: %s\n"),
1922823Sgjelinek 			    fstab.zone_fs_dir, strerror(errno));
1923823Sgjelinek 			return_code = Z_ERR;
1924823Sgjelinek 		}
1925823Sgjelinek 		if (strcmp(st.st_fstype, MNTTYPE_NFS) == 0) {
1926924Sgjelinek 			/*
1927924Sgjelinek 			 * TRANSLATION_NOTE
1928924Sgjelinek 			 * inherit-pkg-dir and NFS are literals that should
1929924Sgjelinek 			 * not be translated.
1930924Sgjelinek 			 */
1931823Sgjelinek 			(void) fprintf(stderr, gettext("cannot verify "
1932924Sgjelinek 			    "inherit-pkg-dir %s: NFS mounted file-system.\n"
1933924Sgjelinek 			    "\tA local file-system must be used.\n"),
1934823Sgjelinek 			    fstab.zone_fs_dir);
1935823Sgjelinek 			return_code = Z_ERR;
1936823Sgjelinek 		}
1937823Sgjelinek 	}
1938823Sgjelinek 	(void) zonecfg_endipdent(handle);
1939823Sgjelinek 
1940823Sgjelinek 	return (return_code);
1941823Sgjelinek }
1942823Sgjelinek 
19431393Slling /* ARGSUSED */
19441393Slling static void
19451393Slling zfs_fs_err_handler(const char *fmt, va_list ap)
19461393Slling {
19471393Slling 	/*
19481393Slling 	 * Do nothing - do not print the libzfs error messages.
19491393Slling 	 */
19501393Slling }
19511393Slling 
19521393Slling /*
19531393Slling  * Verify that the ZFS dataset exists, and its mountpoint
19541393Slling  * property is set to "legacy".
19551393Slling  */
19561393Slling static int
19571393Slling verify_fs_zfs(struct zone_fstab *fstab)
19581393Slling {
19591393Slling 	zfs_handle_t *zhp;
19601393Slling 	char propbuf[ZFS_MAXPROPLEN];
19611393Slling 
19621393Slling 	zfs_set_error_handler(zfs_fs_err_handler);
19631393Slling 
19641393Slling 	if ((zhp = zfs_open(fstab->zone_fs_special, ZFS_TYPE_ANY)) == NULL) {
19651397Slling 		(void) fprintf(stderr, gettext("could not verify fs %s: "
19661393Slling 			"could not access zfs dataset '%s'\n"),
19671393Slling 			fstab->zone_fs_dir, fstab->zone_fs_special);
19681393Slling 		return (Z_ERR);
19691393Slling 	}
19701393Slling 
19711393Slling 	if (zfs_get_type(zhp) != ZFS_TYPE_FILESYSTEM) {
19721393Slling 		(void) fprintf(stderr, gettext("cannot verify fs %s: "
19731393Slling 			"'%s' is not a filesystem\n"),
19741393Slling 			fstab->zone_fs_dir, fstab->zone_fs_special);
19751393Slling 		zfs_close(zhp);
19761393Slling 		return (Z_ERR);
19771393Slling 	}
19781393Slling 
19791393Slling 	if (zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, propbuf, sizeof (propbuf),
19801393Slling 	    NULL, NULL, 0, 0) != 0 || strcmp(propbuf, "legacy") != 0) {
19811397Slling 		(void) fprintf(stderr, gettext("could not verify fs %s: "
19821397Slling 			"zfs '%s' mountpoint is not \"legacy\"\n"),
19831393Slling 			fstab->zone_fs_dir, fstab->zone_fs_special);
19841393Slling 		zfs_close(zhp);
19851393Slling 		return (Z_ERR);
19861393Slling 	}
19871393Slling 
19881393Slling 	zfs_close(zhp);
19891393Slling 	return (Z_OK);
19901393Slling }
19911393Slling 
19921393Slling /*
19931393Slling  * Verify that the special device/filesystem exists and is valid.
19941393Slling  */
19951393Slling static int
19961393Slling verify_fs_special(struct zone_fstab *fstab)
19971393Slling {
19981393Slling 	struct stat st;
19991393Slling 
20001393Slling 	if (strcmp(fstab->zone_fs_type, MNTTYPE_ZFS) == 0)
20011393Slling 		return (verify_fs_zfs(fstab));
20021393Slling 
20031393Slling 	if (stat(fstab->zone_fs_special, &st) != 0) {
20041397Slling 		(void) fprintf(stderr, gettext("could not verify fs "
20051393Slling 		    "%s: could not access %s: %s\n"), fstab->zone_fs_dir,
20061393Slling 		    fstab->zone_fs_special, strerror(errno));
20071393Slling 		return (Z_ERR);
20081393Slling 	}
20091393Slling 
20101393Slling 	if (strcmp(st.st_fstype, MNTTYPE_NFS) == 0) {
20111393Slling 		/*
20121393Slling 		 * TRANSLATION_NOTE
20131393Slling 		 * fs and NFS are literals that should
20141393Slling 		 * not be translated.
20151393Slling 		 */
20161393Slling 		(void) fprintf(stderr, gettext("cannot verify "
20171393Slling 		    "fs %s: NFS mounted file-system.\n"
20181393Slling 		    "\tA local file-system must be used.\n"),
20191393Slling 		    fstab->zone_fs_special);
20201393Slling 		return (Z_ERR);
20211393Slling 	}
20221393Slling 
20231393Slling 	return (Z_OK);
20241393Slling }
20251393Slling 
2026823Sgjelinek static int
20270Sstevel@tonic-gate verify_filesystems(zone_dochandle_t handle)
20280Sstevel@tonic-gate {
20290Sstevel@tonic-gate 	int return_code = Z_OK;
20300Sstevel@tonic-gate 	struct zone_fstab fstab;
20310Sstevel@tonic-gate 	char cmdbuf[MAXPATHLEN];
20320Sstevel@tonic-gate 	struct stat st;
20330Sstevel@tonic-gate 
20340Sstevel@tonic-gate 	/*
20350Sstevel@tonic-gate 	 * No need to verify inherit-pkg-dir fs types, as their type is
20360Sstevel@tonic-gate 	 * implicitly lofs, which is known.  Therefore, the types are only
20370Sstevel@tonic-gate 	 * verified for regular filesystems below.
20380Sstevel@tonic-gate 	 *
20390Sstevel@tonic-gate 	 * Since the actual mount point is not known until the dependent mounts
20400Sstevel@tonic-gate 	 * are performed, we don't attempt any path validation here: that will
20410Sstevel@tonic-gate 	 * happen later when zoneadmd actually does the mounts.
20420Sstevel@tonic-gate 	 */
20430Sstevel@tonic-gate 	if (zonecfg_setfsent(handle) != Z_OK) {
2044924Sgjelinek 		(void) fprintf(stderr, gettext("could not verify file-systems: "
20450Sstevel@tonic-gate 		    "unable to enumerate mounts\n"));
20460Sstevel@tonic-gate 		return (Z_ERR);
20470Sstevel@tonic-gate 	}
20480Sstevel@tonic-gate 	while (zonecfg_getfsent(handle, &fstab) == Z_OK) {
20490Sstevel@tonic-gate 		if (!zonecfg_valid_fs_type(fstab.zone_fs_type)) {
20500Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("cannot verify fs %s: "
20510Sstevel@tonic-gate 			    "type %s is not allowed.\n"), fstab.zone_fs_dir,
20520Sstevel@tonic-gate 			    fstab.zone_fs_type);
20530Sstevel@tonic-gate 			return_code = Z_ERR;
20540Sstevel@tonic-gate 			goto next_fs;
20550Sstevel@tonic-gate 		}
20560Sstevel@tonic-gate 		/*
20570Sstevel@tonic-gate 		 * Verify /usr/lib/fs/<fstype>/mount exists.
20580Sstevel@tonic-gate 		 */
20590Sstevel@tonic-gate 		if (snprintf(cmdbuf, sizeof (cmdbuf), "/usr/lib/fs/%s/mount",
20600Sstevel@tonic-gate 		    fstab.zone_fs_type) > sizeof (cmdbuf)) {
20610Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("cannot verify fs %s: "
20620Sstevel@tonic-gate 			    "type %s is too long.\n"), fstab.zone_fs_dir,
20630Sstevel@tonic-gate 			    fstab.zone_fs_type);
20640Sstevel@tonic-gate 			return_code = Z_ERR;
20650Sstevel@tonic-gate 			goto next_fs;
20660Sstevel@tonic-gate 		}
20670Sstevel@tonic-gate 		if (stat(cmdbuf, &st) != 0) {
2068924Sgjelinek 			(void) fprintf(stderr, gettext("could not verify fs "
2069924Sgjelinek 			    "%s: could not access %s: %s\n"), fstab.zone_fs_dir,
20700Sstevel@tonic-gate 			    cmdbuf, strerror(errno));
20710Sstevel@tonic-gate 			return_code = Z_ERR;
20720Sstevel@tonic-gate 			goto next_fs;
20730Sstevel@tonic-gate 		}
20740Sstevel@tonic-gate 		if (!S_ISREG(st.st_mode)) {
2075924Sgjelinek 			(void) fprintf(stderr, gettext("could not verify fs "
2076924Sgjelinek 			    "%s: %s is not a regular file\n"),
2077924Sgjelinek 			    fstab.zone_fs_dir, cmdbuf);
20780Sstevel@tonic-gate 			return_code = Z_ERR;
20790Sstevel@tonic-gate 			goto next_fs;
20800Sstevel@tonic-gate 		}
20810Sstevel@tonic-gate 		/*
20820Sstevel@tonic-gate 		 * Verify /usr/lib/fs/<fstype>/fsck exists iff zone_fs_raw is
20830Sstevel@tonic-gate 		 * set.
20840Sstevel@tonic-gate 		 */
20850Sstevel@tonic-gate 		if (snprintf(cmdbuf, sizeof (cmdbuf), "/usr/lib/fs/%s/fsck",
20860Sstevel@tonic-gate 		    fstab.zone_fs_type) > sizeof (cmdbuf)) {
20870Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("cannot verify fs %s: "
20880Sstevel@tonic-gate 			    "type %s is too long.\n"), fstab.zone_fs_dir,
20890Sstevel@tonic-gate 			    fstab.zone_fs_type);
20900Sstevel@tonic-gate 			return_code = Z_ERR;
20910Sstevel@tonic-gate 			goto next_fs;
20920Sstevel@tonic-gate 		}
20930Sstevel@tonic-gate 		if (fstab.zone_fs_raw[0] == '\0' && stat(cmdbuf, &st) == 0) {
2094924Sgjelinek 			(void) fprintf(stderr, gettext("could not verify fs "
2095924Sgjelinek 			    "%s: must specify 'raw' device for %s "
2096924Sgjelinek 			    "file-systems\n"),
20970Sstevel@tonic-gate 			    fstab.zone_fs_dir, fstab.zone_fs_type);
20980Sstevel@tonic-gate 			return_code = Z_ERR;
20990Sstevel@tonic-gate 			goto next_fs;
21000Sstevel@tonic-gate 		}
21010Sstevel@tonic-gate 		if (fstab.zone_fs_raw[0] != '\0' &&
21020Sstevel@tonic-gate 		    (stat(cmdbuf, &st) != 0 || !S_ISREG(st.st_mode))) {
21030Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("cannot verify fs %s: "
21040Sstevel@tonic-gate 			    "'raw' device specified but "
21050Sstevel@tonic-gate 			    "no fsck executable exists for %s\n"),
21060Sstevel@tonic-gate 			    fstab.zone_fs_dir, fstab.zone_fs_type);
21070Sstevel@tonic-gate 			return_code = Z_ERR;
21080Sstevel@tonic-gate 			goto next_fs;
21090Sstevel@tonic-gate 		}
21101393Slling 
21111393Slling 		/* Verify fs_special. */
21121393Slling 		if ((return_code = verify_fs_special(&fstab)) != Z_OK)
2113823Sgjelinek 			goto next_fs;
21141393Slling 
21151393Slling 		/* Verify fs_raw. */
2116823Sgjelinek 		if (fstab.zone_fs_raw[0] != '\0' &&
2117823Sgjelinek 		    stat(fstab.zone_fs_raw, &st) != 0) {
2118924Sgjelinek 			/*
2119924Sgjelinek 			 * TRANSLATION_NOTE
2120924Sgjelinek 			 * fs is a literal that should not be translated.
2121924Sgjelinek 			 */
2122924Sgjelinek 			(void) fprintf(stderr, gettext("could not verify fs "
2123924Sgjelinek 			    "%s: could not access %s: %s\n"), fstab.zone_fs_dir,
2124823Sgjelinek 			    fstab.zone_fs_raw, strerror(errno));
2125823Sgjelinek 			return_code = Z_ERR;
2126823Sgjelinek 			goto next_fs;
2127823Sgjelinek 		}
21280Sstevel@tonic-gate next_fs:
21290Sstevel@tonic-gate 		zonecfg_free_fs_option_list(fstab.zone_fs_options);
21300Sstevel@tonic-gate 	}
21310Sstevel@tonic-gate 	(void) zonecfg_endfsent(handle);
21320Sstevel@tonic-gate 
21330Sstevel@tonic-gate 	return (return_code);
21340Sstevel@tonic-gate }
21350Sstevel@tonic-gate 
2136789Sahrens const char *current_dataset;
2137789Sahrens 
2138789Sahrens /*
2139789Sahrens  * Custom error handler for errors incurred as part of the checks below.  We
2140789Sahrens  * want to trim off the leading 'cannot open ...' to create a better error
2141789Sahrens  * message.  The only other way this can fail is if we fail to set the 'zoned'
2142789Sahrens  * property.  In this case we just pass the error on verbatim.
2143789Sahrens  */
2144789Sahrens static void
2145789Sahrens zfs_error_handler(const char *fmt, va_list ap)
2146789Sahrens {
2147789Sahrens 	char buf[1024];
2148789Sahrens 
2149789Sahrens 	(void) vsnprintf(buf, sizeof (buf), fmt, ap);
2150789Sahrens 
2151789Sahrens 	if (strncmp(gettext("cannot open "), buf,
2152789Sahrens 	    strlen(gettext("cannot open "))) == 0)
2153924Sgjelinek 		/*
2154924Sgjelinek 		 * TRANSLATION_NOTE
2155924Sgjelinek 		 * zfs and dataset are literals that should not be translated.
2156924Sgjelinek 		 */
2157924Sgjelinek 		(void) fprintf(stderr, gettext("could not verify zfs "
2158789Sahrens 		    "dataset %s%s\n"), current_dataset, strchr(buf, ':'));
2159789Sahrens 	else
2160924Sgjelinek 		(void) fprintf(stderr, gettext("could not verify zfs dataset "
2161789Sahrens 		    "%s: %s\n"), current_dataset, buf);
2162789Sahrens }
2163789Sahrens 
2164789Sahrens /* ARGSUSED */
2165789Sahrens static int
2166789Sahrens check_zvol(zfs_handle_t *zhp, void *unused)
2167789Sahrens {
2168789Sahrens 	int ret;
2169789Sahrens 
2170789Sahrens 	if (zfs_get_type(zhp) == ZFS_TYPE_VOLUME) {
2171924Sgjelinek 		/*
2172924Sgjelinek 		 * TRANSLATION_NOTE
2173924Sgjelinek 		 * zfs and dataset are literals that should not be translated.
2174924Sgjelinek 		 */
2175789Sahrens 		(void) fprintf(stderr, gettext("cannot verify zfs dataset %s: "
2176789Sahrens 		    "volumes cannot be specified as a zone dataset resource\n"),
2177789Sahrens 		    zfs_get_name(zhp));
2178789Sahrens 		ret = -1;
2179789Sahrens 	} else {
2180789Sahrens 		ret = zfs_iter_children(zhp, check_zvol, NULL);
2181789Sahrens 	}
2182789Sahrens 
2183789Sahrens 	zfs_close(zhp);
2184789Sahrens 
2185789Sahrens 	return (ret);
2186789Sahrens }
2187789Sahrens 
2188789Sahrens /*
2189789Sahrens  * Validate that the given dataset exists on the system, and that neither it nor
2190789Sahrens  * its children are zvols.
2191789Sahrens  *
2192789Sahrens  * Note that we don't do anything with the 'zoned' property here.  All
2193789Sahrens  * management is done in zoneadmd when the zone is actually rebooted.  This
2194789Sahrens  * allows us to automatically set the zoned property even when a zone is
2195789Sahrens  * rebooted by the administrator.
2196789Sahrens  */
2197789Sahrens static int
2198789Sahrens verify_datasets(zone_dochandle_t handle)
2199789Sahrens {
2200789Sahrens 	int return_code = Z_OK;
2201789Sahrens 	struct zone_dstab dstab;
2202789Sahrens 	zfs_handle_t *zhp;
2203789Sahrens 	char propbuf[ZFS_MAXPROPLEN];
2204789Sahrens 	char source[ZFS_MAXNAMELEN];
2205789Sahrens 	zfs_source_t srctype;
2206789Sahrens 
2207789Sahrens 	if (zonecfg_setdsent(handle) != Z_OK) {
2208924Sgjelinek 		/*
2209924Sgjelinek 		 * TRANSLATION_NOTE
2210924Sgjelinek 		 * zfs and dataset are literals that should not be translated.
2211924Sgjelinek 		 */
2212924Sgjelinek 		(void) fprintf(stderr, gettext("could not verify zfs datasets: "
2213789Sahrens 		    "unable to enumerate datasets\n"));
2214789Sahrens 		return (Z_ERR);
2215789Sahrens 	}
2216789Sahrens 
2217789Sahrens 	zfs_set_error_handler(zfs_error_handler);
2218789Sahrens 
2219789Sahrens 	while (zonecfg_getdsent(handle, &dstab) == Z_OK) {
2220789Sahrens 
2221789Sahrens 		current_dataset = dstab.zone_dataset_name;
2222789Sahrens 
2223789Sahrens 		if ((zhp = zfs_open(dstab.zone_dataset_name,
2224789Sahrens 		    ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME)) == NULL) {
2225789Sahrens 			return_code = Z_ERR;
2226789Sahrens 			continue;
2227789Sahrens 		}
2228789Sahrens 
2229789Sahrens 		if (zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, propbuf,
2230789Sahrens 		    sizeof (propbuf), &srctype, source,
2231789Sahrens 		    sizeof (source), 0) == 0 &&
2232789Sahrens 		    (srctype == ZFS_SRC_INHERITED)) {
2233924Sgjelinek 			(void) fprintf(stderr, gettext("could not verify zfs "
2234789Sahrens 			    "dataset %s: mountpoint cannot be inherited\n"),
2235789Sahrens 			    dstab.zone_dataset_name);
2236789Sahrens 			return_code = Z_ERR;
2237789Sahrens 			zfs_close(zhp);
2238789Sahrens 			continue;
2239789Sahrens 		}
2240789Sahrens 
2241789Sahrens 		if (zfs_get_type(zhp) == ZFS_TYPE_VOLUME) {
2242789Sahrens 			(void) fprintf(stderr, gettext("cannot verify zfs "
2243789Sahrens 			    "dataset %s: volumes cannot be specified as a "
2244789Sahrens 			    "zone dataset resource\n"),
2245789Sahrens 			    dstab.zone_dataset_name);
2246789Sahrens 			return_code = Z_ERR;
2247789Sahrens 		}
2248789Sahrens 
2249789Sahrens 		if (zfs_iter_children(zhp, check_zvol, NULL) != 0)
2250789Sahrens 			return_code = Z_ERR;
2251789Sahrens 
2252789Sahrens 		zfs_close(zhp);
2253789Sahrens 	}
2254789Sahrens 	(void) zonecfg_enddsent(handle);
2255789Sahrens 
2256789Sahrens 	return (return_code);
2257789Sahrens }
2258789Sahrens 
22590Sstevel@tonic-gate static int
22600Sstevel@tonic-gate verify_details(int cmd_num)
22610Sstevel@tonic-gate {
22620Sstevel@tonic-gate 	zone_dochandle_t handle;
22630Sstevel@tonic-gate 	struct zone_nwiftab nwiftab;
22640Sstevel@tonic-gate 	char zonepath[MAXPATHLEN], checkpath[MAXPATHLEN];
22650Sstevel@tonic-gate 	int return_code = Z_OK;
22660Sstevel@tonic-gate 	int err;
2267766Scarlsonj 	boolean_t in_alt_root;
22680Sstevel@tonic-gate 
22690Sstevel@tonic-gate 	if ((handle = zonecfg_init_handle()) == NULL) {
22700Sstevel@tonic-gate 		zperror(cmd_to_str(cmd_num), B_TRUE);
22710Sstevel@tonic-gate 		return (Z_ERR);
22720Sstevel@tonic-gate 	}
22730Sstevel@tonic-gate 	if ((err = zonecfg_get_handle(target_zone, handle)) != Z_OK) {
22740Sstevel@tonic-gate 		errno = err;
22750Sstevel@tonic-gate 		zperror(cmd_to_str(cmd_num), B_TRUE);
22760Sstevel@tonic-gate 		zonecfg_fini_handle(handle);
22770Sstevel@tonic-gate 		return (Z_ERR);
22780Sstevel@tonic-gate 	}
22790Sstevel@tonic-gate 	if ((err = zonecfg_get_zonepath(handle, zonepath, sizeof (zonepath))) !=
22800Sstevel@tonic-gate 	    Z_OK) {
22810Sstevel@tonic-gate 		errno = err;
22820Sstevel@tonic-gate 		zperror(cmd_to_str(cmd_num), B_TRUE);
22830Sstevel@tonic-gate 		zonecfg_fini_handle(handle);
22840Sstevel@tonic-gate 		return (Z_ERR);
22850Sstevel@tonic-gate 	}
22860Sstevel@tonic-gate 	/*
22870Sstevel@tonic-gate 	 * zonecfg_get_zonepath() gets its data from the XML repository.
22880Sstevel@tonic-gate 	 * Verify this against the index file, which is checked first by
22890Sstevel@tonic-gate 	 * zone_get_zonepath().  If they don't match, bail out.
22900Sstevel@tonic-gate 	 */
22910Sstevel@tonic-gate 	if ((err = zone_get_zonepath(target_zone, checkpath,
22920Sstevel@tonic-gate 	    sizeof (checkpath))) != Z_OK) {
22930Sstevel@tonic-gate 		errno = err;
22940Sstevel@tonic-gate 		zperror2(target_zone, gettext("could not get zone path"));
22950Sstevel@tonic-gate 		return (Z_ERR);
22960Sstevel@tonic-gate 	}
22970Sstevel@tonic-gate 	if (strcmp(zonepath, checkpath) != 0) {
2298924Sgjelinek 		/*
2299924Sgjelinek 		 * TRANSLATION_NOTE
2300924Sgjelinek 		 * XML and zonepath are literals that should not be translated.
2301924Sgjelinek 		 */
23020Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("The XML repository has "
23030Sstevel@tonic-gate 		    "zonepath '%s',\nbut the index file has zonepath '%s'.\n"
23040Sstevel@tonic-gate 		    "These must match, so fix the incorrect entry.\n"),
23050Sstevel@tonic-gate 		    zonepath, checkpath);
23060Sstevel@tonic-gate 		return (Z_ERR);
23070Sstevel@tonic-gate 	}
23080Sstevel@tonic-gate 	if (validate_zonepath(zonepath, cmd_num) != Z_OK) {
23090Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("could not verify zonepath %s "
23100Sstevel@tonic-gate 		    "because of the above errors.\n"), zonepath);
23110Sstevel@tonic-gate 		return_code = Z_ERR;
23120Sstevel@tonic-gate 	}
23130Sstevel@tonic-gate 
2314766Scarlsonj 	in_alt_root = zonecfg_in_alt_root();
2315766Scarlsonj 	if (in_alt_root)
2316766Scarlsonj 		goto no_net;
2317766Scarlsonj 
23180Sstevel@tonic-gate 	if ((err = zonecfg_setnwifent(handle)) != Z_OK) {
23190Sstevel@tonic-gate 		errno = err;
23200Sstevel@tonic-gate 		zperror(cmd_to_str(cmd_num), B_TRUE);
23210Sstevel@tonic-gate 		zonecfg_fini_handle(handle);
23220Sstevel@tonic-gate 		return (Z_ERR);
23230Sstevel@tonic-gate 	}
23240Sstevel@tonic-gate 	while (zonecfg_getnwifent(handle, &nwiftab) == Z_OK) {
23250Sstevel@tonic-gate 		struct lifreq lifr;
23260Sstevel@tonic-gate 		sa_family_t af;
23270Sstevel@tonic-gate 		int so, res;
23280Sstevel@tonic-gate 
23290Sstevel@tonic-gate 		/* skip any loopback interfaces */
23300Sstevel@tonic-gate 		if (strcmp(nwiftab.zone_nwif_physical, "lo0") == 0)
23310Sstevel@tonic-gate 			continue;
23320Sstevel@tonic-gate 		if ((res = zonecfg_valid_net_address(nwiftab.zone_nwif_address,
23330Sstevel@tonic-gate 		    &lifr)) != Z_OK) {
23340Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("could not verify %s "
23350Sstevel@tonic-gate 			    "%s=%s %s=%s: %s\n"), "net", "address",
23360Sstevel@tonic-gate 			    nwiftab.zone_nwif_address, "physical",
23370Sstevel@tonic-gate 			    nwiftab.zone_nwif_physical, zonecfg_strerror(res));
23380Sstevel@tonic-gate 			return_code = Z_ERR;
23390Sstevel@tonic-gate 			continue;
23400Sstevel@tonic-gate 		}
23410Sstevel@tonic-gate 		af = lifr.lifr_addr.ss_family;
23420Sstevel@tonic-gate 		(void) memset(&lifr, 0, sizeof (lifr));
23430Sstevel@tonic-gate 		(void) strlcpy(lifr.lifr_name, nwiftab.zone_nwif_physical,
23440Sstevel@tonic-gate 		    sizeof (lifr.lifr_name));
23450Sstevel@tonic-gate 		lifr.lifr_addr.ss_family = af;
23460Sstevel@tonic-gate 		if ((so = socket(af, SOCK_DGRAM, 0)) < 0) {
23470Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("could not verify %s "
23480Sstevel@tonic-gate 			    "%s=%s %s=%s: could not get socket: %s\n"), "net",
23490Sstevel@tonic-gate 			    "address", nwiftab.zone_nwif_address, "physical",
23500Sstevel@tonic-gate 			    nwiftab.zone_nwif_physical, strerror(errno));
23510Sstevel@tonic-gate 			return_code = Z_ERR;
23520Sstevel@tonic-gate 			continue;
23530Sstevel@tonic-gate 		}
23540Sstevel@tonic-gate 		if (ioctl(so, SIOCGLIFFLAGS, (caddr_t)&lifr) < 0) {
23550Sstevel@tonic-gate 			(void) fprintf(stderr,
23560Sstevel@tonic-gate 			    gettext("could not verify %s %s=%s %s=%s: %s\n"),
23570Sstevel@tonic-gate 			    "net", "address", nwiftab.zone_nwif_address,
23580Sstevel@tonic-gate 			    "physical", nwiftab.zone_nwif_physical,
23590Sstevel@tonic-gate 			    strerror(errno));
23600Sstevel@tonic-gate 			return_code = Z_ERR;
23610Sstevel@tonic-gate 		}
23620Sstevel@tonic-gate 		(void) close(so);
23630Sstevel@tonic-gate 	}
23640Sstevel@tonic-gate 	(void) zonecfg_endnwifent(handle);
2365766Scarlsonj no_net:
23660Sstevel@tonic-gate 
23670Sstevel@tonic-gate 	if (verify_filesystems(handle) != Z_OK)
23680Sstevel@tonic-gate 		return_code = Z_ERR;
2369823Sgjelinek 	if (verify_ipd(handle) != Z_OK)
2370823Sgjelinek 		return_code = Z_ERR;
2371766Scarlsonj 	if (!in_alt_root && verify_rctls(handle) != Z_OK)
23720Sstevel@tonic-gate 		return_code = Z_ERR;
2373766Scarlsonj 	if (!in_alt_root && verify_pool(handle) != Z_OK)
23740Sstevel@tonic-gate 		return_code = Z_ERR;
2375789Sahrens 	if (!in_alt_root && verify_datasets(handle) != Z_OK)
2376789Sahrens 		return_code = Z_ERR;
23770Sstevel@tonic-gate 	zonecfg_fini_handle(handle);
23780Sstevel@tonic-gate 	if (return_code == Z_ERR)
23790Sstevel@tonic-gate 		(void) fprintf(stderr,
23800Sstevel@tonic-gate 		    gettext("%s: zone %s failed to verify\n"),
23810Sstevel@tonic-gate 		    execname, target_zone);
23820Sstevel@tonic-gate 	return (return_code);
23830Sstevel@tonic-gate }
23840Sstevel@tonic-gate 
23850Sstevel@tonic-gate static int
23860Sstevel@tonic-gate verify_func(int argc, char *argv[])
23870Sstevel@tonic-gate {
23880Sstevel@tonic-gate 	int arg;
23890Sstevel@tonic-gate 
23900Sstevel@tonic-gate 	optind = 0;
23910Sstevel@tonic-gate 	if ((arg = getopt(argc, argv, "?")) != EOF) {
23920Sstevel@tonic-gate 		switch (arg) {
23930Sstevel@tonic-gate 		case '?':
23940Sstevel@tonic-gate 			sub_usage(SHELP_VERIFY, CMD_VERIFY);
23950Sstevel@tonic-gate 			return (optopt == '?' ? Z_OK : Z_USAGE);
23960Sstevel@tonic-gate 		default:
23970Sstevel@tonic-gate 			sub_usage(SHELP_VERIFY, CMD_VERIFY);
23980Sstevel@tonic-gate 			return (Z_USAGE);
23990Sstevel@tonic-gate 		}
24000Sstevel@tonic-gate 	}
24010Sstevel@tonic-gate 	if (argc > optind) {
24020Sstevel@tonic-gate 		sub_usage(SHELP_VERIFY, CMD_VERIFY);
24030Sstevel@tonic-gate 		return (Z_USAGE);
24040Sstevel@tonic-gate 	}
24050Sstevel@tonic-gate 	if (sanity_check(target_zone, CMD_VERIFY, B_FALSE, B_FALSE) != Z_OK)
24060Sstevel@tonic-gate 		return (Z_ERR);
24070Sstevel@tonic-gate 	return (verify_details(CMD_VERIFY));
24080Sstevel@tonic-gate }
24090Sstevel@tonic-gate 
24100Sstevel@tonic-gate #define	LUCREATEZONE	"/usr/lib/lu/lucreatezone"
24110Sstevel@tonic-gate 
24120Sstevel@tonic-gate static int
24130Sstevel@tonic-gate install_func(int argc, char *argv[])
24140Sstevel@tonic-gate {
24150Sstevel@tonic-gate 	/* 9: "exec " and " -z " */
24160Sstevel@tonic-gate 	char cmdbuf[sizeof (LUCREATEZONE) + ZONENAME_MAX + 9];
24170Sstevel@tonic-gate 	int lockfd;
24180Sstevel@tonic-gate 	int err, arg;
24190Sstevel@tonic-gate 	char zonepath[MAXPATHLEN];
24200Sstevel@tonic-gate 	int status;
24210Sstevel@tonic-gate 
2422766Scarlsonj 	if (zonecfg_in_alt_root()) {
2423766Scarlsonj 		zerror(gettext("cannot install zone in alternate root"));
2424766Scarlsonj 		return (Z_ERR);
2425766Scarlsonj 	}
2426766Scarlsonj 
24270Sstevel@tonic-gate 	optind = 0;
24280Sstevel@tonic-gate 	if ((arg = getopt(argc, argv, "?")) != EOF) {
24290Sstevel@tonic-gate 		switch (arg) {
24300Sstevel@tonic-gate 		case '?':
24310Sstevel@tonic-gate 			sub_usage(SHELP_INSTALL, CMD_INSTALL);
24320Sstevel@tonic-gate 			return (optopt == '?' ? Z_OK : Z_USAGE);
24330Sstevel@tonic-gate 		default:
24340Sstevel@tonic-gate 			sub_usage(SHELP_INSTALL, CMD_INSTALL);
24350Sstevel@tonic-gate 			return (Z_USAGE);
24360Sstevel@tonic-gate 		}
24370Sstevel@tonic-gate 	}
24380Sstevel@tonic-gate 	if (argc > optind) {
24390Sstevel@tonic-gate 		sub_usage(SHELP_INSTALL, CMD_INSTALL);
24400Sstevel@tonic-gate 		return (Z_USAGE);
24410Sstevel@tonic-gate 	}
24420Sstevel@tonic-gate 	if (sanity_check(target_zone, CMD_INSTALL, B_FALSE, B_TRUE) != Z_OK)
24430Sstevel@tonic-gate 		return (Z_ERR);
24440Sstevel@tonic-gate 	if (verify_details(CMD_INSTALL) != Z_OK)
24450Sstevel@tonic-gate 		return (Z_ERR);
24460Sstevel@tonic-gate 
24470Sstevel@tonic-gate 	if (grab_lock_file(target_zone, &lockfd) != Z_OK) {
24480Sstevel@tonic-gate 		zerror(gettext("another %s may have an operation in progress."),
24491300Sgjelinek 		    "zoneadm");
24500Sstevel@tonic-gate 		return (Z_ERR);
24510Sstevel@tonic-gate 	}
24520Sstevel@tonic-gate 	err = zone_set_state(target_zone, ZONE_STATE_INCOMPLETE);
24530Sstevel@tonic-gate 	if (err != Z_OK) {
24540Sstevel@tonic-gate 		errno = err;
24550Sstevel@tonic-gate 		zperror2(target_zone, gettext("could not set state"));
24560Sstevel@tonic-gate 		goto done;
24570Sstevel@tonic-gate 	}
24580Sstevel@tonic-gate 
24590Sstevel@tonic-gate 	/*
24600Sstevel@tonic-gate 	 * According to the Application Packaging Developer's Guide, a
24610Sstevel@tonic-gate 	 * "checkinstall" script when included in a package is executed as
24620Sstevel@tonic-gate 	 * the user "install", if such a user exists, or by the user
24630Sstevel@tonic-gate 	 * "nobody".  In order to support this dubious behavior, the path
24640Sstevel@tonic-gate 	 * to the zone being constructed is opened up during the life of
24650Sstevel@tonic-gate 	 * the command laying down the zone's root file system.  Once this
24660Sstevel@tonic-gate 	 * has completed, regardless of whether it was successful, the
24670Sstevel@tonic-gate 	 * path to the zone is again restricted.
24680Sstevel@tonic-gate 	 */
24690Sstevel@tonic-gate 	if ((err = zone_get_zonepath(target_zone, zonepath,
24700Sstevel@tonic-gate 	    sizeof (zonepath))) != Z_OK) {
24710Sstevel@tonic-gate 		errno = err;
24720Sstevel@tonic-gate 		zperror2(target_zone, gettext("could not get zone path"));
24730Sstevel@tonic-gate 		goto done;
24740Sstevel@tonic-gate 	}
24750Sstevel@tonic-gate 	if (chmod(zonepath, DEFAULT_DIR_MODE) != 0) {
24760Sstevel@tonic-gate 		zperror(zonepath, B_FALSE);
24770Sstevel@tonic-gate 		err = Z_ERR;
24780Sstevel@tonic-gate 		goto done;
24790Sstevel@tonic-gate 	}
24800Sstevel@tonic-gate 
24810Sstevel@tonic-gate 	/*
24820Sstevel@tonic-gate 	 * "exec" the command so that the returned status is that of
24830Sstevel@tonic-gate 	 * LUCREATEZONE and not the shell.
24840Sstevel@tonic-gate 	 */
24850Sstevel@tonic-gate 	(void) snprintf(cmdbuf, sizeof (cmdbuf), "exec " LUCREATEZONE " -z %s",
24860Sstevel@tonic-gate 	    target_zone);
24870Sstevel@tonic-gate 	status = do_subproc(cmdbuf);
24880Sstevel@tonic-gate 	if (chmod(zonepath, S_IRWXU) != 0) {
24890Sstevel@tonic-gate 		zperror(zonepath, B_FALSE);
24900Sstevel@tonic-gate 		err = Z_ERR;
24910Sstevel@tonic-gate 		goto done;
24920Sstevel@tonic-gate 	}
24930Sstevel@tonic-gate 	if ((err = subproc_status(LUCREATEZONE, status)) != Z_OK)
24940Sstevel@tonic-gate 		goto done;
24950Sstevel@tonic-gate 
24960Sstevel@tonic-gate 	if ((err = zone_set_state(target_zone, ZONE_STATE_INSTALLED)) != Z_OK) {
24970Sstevel@tonic-gate 		errno = err;
24980Sstevel@tonic-gate 		zperror2(target_zone, gettext("could not set state"));
24990Sstevel@tonic-gate 		goto done;
25000Sstevel@tonic-gate 	}
25010Sstevel@tonic-gate 
25020Sstevel@tonic-gate done:
25030Sstevel@tonic-gate 	release_lock_file(lockfd);
25040Sstevel@tonic-gate 	return ((err == Z_OK) ? Z_OK : Z_ERR);
25050Sstevel@tonic-gate }
25060Sstevel@tonic-gate 
25070Sstevel@tonic-gate /*
25081300Sgjelinek  * Check that the inherited pkg dirs are the same for the clone and its source.
25091300Sgjelinek  * The easiest way to do that is check that the list of ipds is the same
25101300Sgjelinek  * by matching each one against the other.  This algorithm should be fine since
25111300Sgjelinek  * the list of ipds should not be that long.
25121300Sgjelinek  */
25131300Sgjelinek static int
25141300Sgjelinek valid_ipd_clone(zone_dochandle_t s_handle, char *source_zone,
25151300Sgjelinek 	zone_dochandle_t t_handle, char *target_zone)
25161300Sgjelinek {
25171300Sgjelinek 	int err;
25181300Sgjelinek 	int res = Z_OK;
25191300Sgjelinek 	int s_cnt = 0;
25201300Sgjelinek 	int t_cnt = 0;
25211300Sgjelinek 	struct zone_fstab s_fstab;
25221300Sgjelinek 	struct zone_fstab t_fstab;
25231300Sgjelinek 
25241300Sgjelinek 	/*
25251300Sgjelinek 	 * First check the source of the clone against the target.
25261300Sgjelinek 	 */
25271300Sgjelinek 	if ((err = zonecfg_setipdent(s_handle)) != Z_OK) {
25281300Sgjelinek 		errno = err;
25291300Sgjelinek 		zperror2(source_zone, gettext("could not enumerate "
25301300Sgjelinek 		    "inherit-pkg-dirs"));
25311300Sgjelinek 		return (Z_ERR);
25321300Sgjelinek 	}
25331300Sgjelinek 
25341300Sgjelinek 	while (zonecfg_getipdent(s_handle, &s_fstab) == Z_OK) {
25351300Sgjelinek 		boolean_t match = B_FALSE;
25361300Sgjelinek 
25371300Sgjelinek 		s_cnt++;
25381300Sgjelinek 
25391300Sgjelinek 		if ((err = zonecfg_setipdent(t_handle)) != Z_OK) {
25401300Sgjelinek 			errno = err;
25411300Sgjelinek 			zperror2(target_zone, gettext("could not enumerate "
25421300Sgjelinek 			    "inherit-pkg-dirs"));
25431300Sgjelinek 			(void) zonecfg_endipdent(s_handle);
25441300Sgjelinek 			return (Z_ERR);
25451300Sgjelinek 		}
25461300Sgjelinek 
25471300Sgjelinek 		while (zonecfg_getipdent(t_handle, &t_fstab) == Z_OK) {
25481300Sgjelinek 			if (strcmp(s_fstab.zone_fs_dir, t_fstab.zone_fs_dir)
25491300Sgjelinek 			    == 0) {
25501300Sgjelinek 				match = B_TRUE;
25511300Sgjelinek 				break;
25521300Sgjelinek 			}
25531300Sgjelinek 		}
25541300Sgjelinek 		(void) zonecfg_endipdent(t_handle);
25551300Sgjelinek 
25561300Sgjelinek 		if (!match) {
25571300Sgjelinek 			(void) fprintf(stderr, gettext("inherit-pkg-dir "
25581300Sgjelinek 			    "'%s' is not configured in zone %s.\n"),
25591300Sgjelinek 			    s_fstab.zone_fs_dir, target_zone);
25601300Sgjelinek 			res = Z_ERR;
25611300Sgjelinek 		}
25621300Sgjelinek 	}
25631300Sgjelinek 
25641300Sgjelinek 	(void) zonecfg_endipdent(s_handle);
25651300Sgjelinek 
25661300Sgjelinek 	/* skip the next check if we already have errors */
25671300Sgjelinek 	if (res == Z_ERR)
25681300Sgjelinek 		return (res);
25691300Sgjelinek 
25701300Sgjelinek 	/*
25711300Sgjelinek 	 * Now check the number of ipds in the target so we can verify
25721300Sgjelinek 	 * that the source is not a subset of the target.
25731300Sgjelinek 	 */
25741300Sgjelinek 	if ((err = zonecfg_setipdent(t_handle)) != Z_OK) {
25751300Sgjelinek 		errno = err;
25761300Sgjelinek 		zperror2(target_zone, gettext("could not enumerate "
25771300Sgjelinek 		    "inherit-pkg-dirs"));
25781300Sgjelinek 		return (Z_ERR);
25791300Sgjelinek 	}
25801300Sgjelinek 
25811300Sgjelinek 	while (zonecfg_getipdent(t_handle, &t_fstab) == Z_OK)
25821300Sgjelinek 		t_cnt++;
25831300Sgjelinek 
25841300Sgjelinek 	(void) zonecfg_endipdent(t_handle);
25851300Sgjelinek 
25861300Sgjelinek 	if (t_cnt != s_cnt) {
25871300Sgjelinek 		(void) fprintf(stderr, gettext("Zone %s is configured "
25881300Sgjelinek 		    "with inherit-pkg-dirs that are not configured in zone "
25891300Sgjelinek 		    "%s.\n"), target_zone, source_zone);
25901300Sgjelinek 		res = Z_ERR;
25911300Sgjelinek 	}
25921300Sgjelinek 
25931300Sgjelinek 	return (res);
25941300Sgjelinek }
25951300Sgjelinek 
25961300Sgjelinek static void
25971300Sgjelinek warn_dev_match(zone_dochandle_t s_handle, char *source_zone,
25981300Sgjelinek 	zone_dochandle_t t_handle, char *target_zone)
25991300Sgjelinek {
26001300Sgjelinek 	int err;
26011300Sgjelinek 	struct zone_devtab s_devtab;
26021300Sgjelinek 	struct zone_devtab t_devtab;
26031300Sgjelinek 
26041300Sgjelinek 	if ((err = zonecfg_setdevent(t_handle)) != Z_OK) {
26051300Sgjelinek 		errno = err;
26061300Sgjelinek 		zperror2(target_zone, gettext("could not enumerate devices"));
26071300Sgjelinek 		return;
26081300Sgjelinek 	}
26091300Sgjelinek 
26101300Sgjelinek 	while (zonecfg_getdevent(t_handle, &t_devtab) == Z_OK) {
26111300Sgjelinek 		if ((err = zonecfg_setdevent(s_handle)) != Z_OK) {
26121300Sgjelinek 			errno = err;
26131300Sgjelinek 			zperror2(source_zone,
26141300Sgjelinek 			    gettext("could not enumerate devices"));
26151300Sgjelinek 			(void) zonecfg_enddevent(t_handle);
26161300Sgjelinek 			return;
26171300Sgjelinek 		}
26181300Sgjelinek 
26191300Sgjelinek 		while (zonecfg_getdevent(s_handle, &s_devtab) == Z_OK) {
26201300Sgjelinek 			/*
26211300Sgjelinek 			 * Use fnmatch to catch the case where wildcards
26221300Sgjelinek 			 * were used in one zone and the other has an
26231300Sgjelinek 			 * explicit entry (e.g. /dev/dsk/c0t0d0s6 vs.
26241300Sgjelinek 			 * /dev/\*dsk/c0t0d0s6).
26251300Sgjelinek 			 */
26261300Sgjelinek 			if (fnmatch(t_devtab.zone_dev_match,
26271300Sgjelinek 			    s_devtab.zone_dev_match, FNM_PATHNAME) == 0 ||
26281300Sgjelinek 			    fnmatch(s_devtab.zone_dev_match,
26291300Sgjelinek 			    t_devtab.zone_dev_match, FNM_PATHNAME) == 0) {
26301300Sgjelinek 				(void) fprintf(stderr,
26311300Sgjelinek 				    gettext("WARNING: device '%s' "
26321300Sgjelinek 				    "is configured in both zones.\n"),
26331300Sgjelinek 				    t_devtab.zone_dev_match);
26341300Sgjelinek 				break;
26351300Sgjelinek 			}
26361300Sgjelinek 		}
26371300Sgjelinek 		(void) zonecfg_enddevent(s_handle);
26381300Sgjelinek 	}
26391300Sgjelinek 
26401300Sgjelinek 	(void) zonecfg_enddevent(t_handle);
26411300Sgjelinek }
26421300Sgjelinek 
26431300Sgjelinek /*
26441300Sgjelinek  * Check if the specified mount option (opt) is contained within the
26451300Sgjelinek  * options string.
26461300Sgjelinek  */
26471300Sgjelinek static boolean_t
26481300Sgjelinek opt_match(char *opt, char *options)
26491300Sgjelinek {
26501300Sgjelinek 	char *p;
26511300Sgjelinek 	char *lastp;
26521300Sgjelinek 
26531300Sgjelinek 	if ((p = strtok_r(options, ",", &lastp)) != NULL) {
26541300Sgjelinek 		if (strcmp(p, opt) == 0)
26551300Sgjelinek 			return (B_TRUE);
26561300Sgjelinek 		while ((p = strtok_r(NULL, ",", &lastp)) != NULL) {
26571300Sgjelinek 			if (strcmp(p, opt) == 0)
26581300Sgjelinek 				return (B_TRUE);
26591300Sgjelinek 		}
26601300Sgjelinek 	}
26611300Sgjelinek 
26621300Sgjelinek 	return (B_FALSE);
26631300Sgjelinek }
26641300Sgjelinek 
26651300Sgjelinek #define	RW_LOFS	"WARNING: read-write lofs file-system on '%s' is configured " \
26661300Sgjelinek 	"in both zones.\n"
26671300Sgjelinek 
26681300Sgjelinek static void
26691300Sgjelinek print_fs_warnings(struct zone_fstab *s_fstab, struct zone_fstab *t_fstab)
26701300Sgjelinek {
26711300Sgjelinek 	/*
26721300Sgjelinek 	 * It is ok to have shared lofs mounted fs but we want to warn if
26731300Sgjelinek 	 * either is rw since this will effect the other zone.
26741300Sgjelinek 	 */
26751300Sgjelinek 	if (strcmp(t_fstab->zone_fs_type, "lofs") == 0) {
26761300Sgjelinek 		zone_fsopt_t *optp;
26771300Sgjelinek 
26781300Sgjelinek 		/* The default is rw so no options means rw */
26791300Sgjelinek 		if (t_fstab->zone_fs_options == NULL ||
26801300Sgjelinek 		    s_fstab->zone_fs_options == NULL) {
26811300Sgjelinek 			(void) fprintf(stderr, gettext(RW_LOFS),
26821300Sgjelinek 			    t_fstab->zone_fs_special);
26831300Sgjelinek 			return;
26841300Sgjelinek 		}
26851300Sgjelinek 
26861300Sgjelinek 		for (optp = s_fstab->zone_fs_options; optp != NULL;
26871300Sgjelinek 		    optp = optp->zone_fsopt_next) {
26881300Sgjelinek 			if (opt_match("rw", optp->zone_fsopt_opt)) {
26891300Sgjelinek 				(void) fprintf(stderr, gettext(RW_LOFS),
26901300Sgjelinek 				    s_fstab->zone_fs_special);
26911300Sgjelinek 				return;
26921300Sgjelinek 			}
26931300Sgjelinek 		}
26941300Sgjelinek 
26951300Sgjelinek 		for (optp = t_fstab->zone_fs_options; optp != NULL;
26961300Sgjelinek 		    optp = optp->zone_fsopt_next) {
26971300Sgjelinek 			if (opt_match("rw", optp->zone_fsopt_opt)) {
26981300Sgjelinek 				(void) fprintf(stderr, gettext(RW_LOFS),
26991300Sgjelinek 				    t_fstab->zone_fs_special);
27001300Sgjelinek 				return;
27011300Sgjelinek 			}
27021300Sgjelinek 		}
27031300Sgjelinek 
27041300Sgjelinek 		return;
27051300Sgjelinek 	}
27061300Sgjelinek 
27071300Sgjelinek 	/*
27081300Sgjelinek 	 * TRANSLATION_NOTE
27091300Sgjelinek 	 * The first variable is the file-system type and the second is
27101300Sgjelinek 	 * the file-system special device.  For example,
27111300Sgjelinek 	 * WARNING: ufs file-system on '/dev/dsk/c0t0d0s0' ...
27121300Sgjelinek 	 */
27131300Sgjelinek 	(void) fprintf(stderr, gettext("WARNING: %s file-system on '%s' "
27141300Sgjelinek 	    "is configured in both zones.\n"), t_fstab->zone_fs_type,
27151300Sgjelinek 	    t_fstab->zone_fs_special);
27161300Sgjelinek }
27171300Sgjelinek 
27181300Sgjelinek static void
27191300Sgjelinek warn_fs_match(zone_dochandle_t s_handle, char *source_zone,
27201300Sgjelinek 	zone_dochandle_t t_handle, char *target_zone)
27211300Sgjelinek {
27221300Sgjelinek 	int err;
27231300Sgjelinek 	struct zone_fstab s_fstab;
27241300Sgjelinek 	struct zone_fstab t_fstab;
27251300Sgjelinek 
27261300Sgjelinek 	if ((err = zonecfg_setfsent(t_handle)) != Z_OK) {
27271300Sgjelinek 		errno = err;
27281300Sgjelinek 		zperror2(target_zone,
27291300Sgjelinek 		    gettext("could not enumerate file-systems"));
27301300Sgjelinek 		return;
27311300Sgjelinek 	}
27321300Sgjelinek 
27331300Sgjelinek 	while (zonecfg_getfsent(t_handle, &t_fstab) == Z_OK) {
27341300Sgjelinek 		if ((err = zonecfg_setfsent(s_handle)) != Z_OK) {
27351300Sgjelinek 			errno = err;
27361300Sgjelinek 			zperror2(source_zone,
27371300Sgjelinek 			    gettext("could not enumerate file-systems"));
27381300Sgjelinek 			(void) zonecfg_endfsent(t_handle);
27391300Sgjelinek 			return;
27401300Sgjelinek 		}
27411300Sgjelinek 
27421300Sgjelinek 		while (zonecfg_getfsent(s_handle, &s_fstab) == Z_OK) {
27431300Sgjelinek 			if (strcmp(t_fstab.zone_fs_special,
27441300Sgjelinek 			    s_fstab.zone_fs_special) == 0) {
27451300Sgjelinek 				print_fs_warnings(&s_fstab, &t_fstab);
27461300Sgjelinek 				break;
27471300Sgjelinek 			}
27481300Sgjelinek 		}
27491300Sgjelinek 		(void) zonecfg_endfsent(s_handle);
27501300Sgjelinek 	}
27511300Sgjelinek 
27521300Sgjelinek 	(void) zonecfg_endfsent(t_handle);
27531300Sgjelinek }
27541300Sgjelinek 
27551300Sgjelinek /*
27561300Sgjelinek  * We don't catch the case where you used the same IP address but
27571300Sgjelinek  * it is not an exact string match.  For example, 192.9.0.128 vs. 192.09.0.128.
27581300Sgjelinek  * However, we're not going to worry about that but we will check for
27591300Sgjelinek  * a possible netmask on one of the addresses (e.g. 10.0.0.1 and 10.0.0.1/24)
27601300Sgjelinek  * and handle that case as a match.
27611300Sgjelinek  */
27621300Sgjelinek static void
27631300Sgjelinek warn_ip_match(zone_dochandle_t s_handle, char *source_zone,
27641300Sgjelinek 	zone_dochandle_t t_handle, char *target_zone)
27651300Sgjelinek {
27661300Sgjelinek 	int err;
27671300Sgjelinek 	struct zone_nwiftab s_nwiftab;
27681300Sgjelinek 	struct zone_nwiftab t_nwiftab;
27691300Sgjelinek 
27701300Sgjelinek 	if ((err = zonecfg_setnwifent(t_handle)) != Z_OK) {
27711300Sgjelinek 		errno = err;
27721300Sgjelinek 		zperror2(target_zone,
27731300Sgjelinek 		    gettext("could not enumerate network interfaces"));
27741300Sgjelinek 		return;
27751300Sgjelinek 	}
27761300Sgjelinek 
27771300Sgjelinek 	while (zonecfg_getnwifent(t_handle, &t_nwiftab) == Z_OK) {
27781300Sgjelinek 		char *p;
27791300Sgjelinek 
27801300Sgjelinek 		/* remove an (optional) netmask from the address */
27811300Sgjelinek 		if ((p = strchr(t_nwiftab.zone_nwif_address, '/')) != NULL)
27821300Sgjelinek 			*p = '\0';
27831300Sgjelinek 
27841300Sgjelinek 		if ((err = zonecfg_setnwifent(s_handle)) != Z_OK) {
27851300Sgjelinek 			errno = err;
27861300Sgjelinek 			zperror2(source_zone,
27871300Sgjelinek 			    gettext("could not enumerate network interfaces"));
27881300Sgjelinek 			(void) zonecfg_endnwifent(t_handle);
27891300Sgjelinek 			return;
27901300Sgjelinek 		}
27911300Sgjelinek 
27921300Sgjelinek 		while (zonecfg_getnwifent(s_handle, &s_nwiftab) == Z_OK) {
27931300Sgjelinek 			/* remove an (optional) netmask from the address */
27941300Sgjelinek 			if ((p = strchr(s_nwiftab.zone_nwif_address, '/'))
27951300Sgjelinek 			    != NULL)
27961300Sgjelinek 				*p = '\0';
27971300Sgjelinek 
27981300Sgjelinek 			if (strcmp(t_nwiftab.zone_nwif_address,
27991300Sgjelinek 			    s_nwiftab.zone_nwif_address) == 0) {
28001300Sgjelinek 				(void) fprintf(stderr,
28011300Sgjelinek 				    gettext("WARNING: network address '%s' "
28021300Sgjelinek 				    "is configured in both zones.\n"),
28031300Sgjelinek 				    t_nwiftab.zone_nwif_address);
28041300Sgjelinek 				break;
28051300Sgjelinek 			}
28061300Sgjelinek 		}
28071300Sgjelinek 		(void) zonecfg_endnwifent(s_handle);
28081300Sgjelinek 	}
28091300Sgjelinek 
28101300Sgjelinek 	(void) zonecfg_endnwifent(t_handle);
28111300Sgjelinek }
28121300Sgjelinek 
28131300Sgjelinek static void
28141300Sgjelinek warn_dataset_match(zone_dochandle_t s_handle, char *source_zone,
28151300Sgjelinek 	zone_dochandle_t t_handle, char *target_zone)
28161300Sgjelinek {
28171300Sgjelinek 	int err;
28181300Sgjelinek 	struct zone_dstab s_dstab;
28191300Sgjelinek 	struct zone_dstab t_dstab;
28201300Sgjelinek 
28211300Sgjelinek 	if ((err = zonecfg_setdsent(t_handle)) != Z_OK) {
28221300Sgjelinek 		errno = err;
28231300Sgjelinek 		zperror2(target_zone, gettext("could not enumerate datasets"));
28241300Sgjelinek 		return;
28251300Sgjelinek 	}
28261300Sgjelinek 
28271300Sgjelinek 	while (zonecfg_getdsent(t_handle, &t_dstab) == Z_OK) {
28281300Sgjelinek 		if ((err = zonecfg_setdsent(s_handle)) != Z_OK) {
28291300Sgjelinek 			errno = err;
28301300Sgjelinek 			zperror2(source_zone,
28311300Sgjelinek 			    gettext("could not enumerate datasets"));
28321300Sgjelinek 			(void) zonecfg_enddsent(t_handle);
28331300Sgjelinek 			return;
28341300Sgjelinek 		}
28351300Sgjelinek 
28361300Sgjelinek 		while (zonecfg_getdsent(s_handle, &s_dstab) == Z_OK) {
28371300Sgjelinek 			if (strcmp(t_dstab.zone_dataset_name,
28381300Sgjelinek 			    s_dstab.zone_dataset_name) == 0) {
28391300Sgjelinek 				(void) fprintf(stderr,
28401300Sgjelinek 				    gettext("WARNING: dataset '%s' "
28411300Sgjelinek 				    "is configured in both zones.\n"),
28421300Sgjelinek 				    t_dstab.zone_dataset_name);
28431300Sgjelinek 				break;
28441300Sgjelinek 			}
28451300Sgjelinek 		}
28461300Sgjelinek 		(void) zonecfg_enddsent(s_handle);
28471300Sgjelinek 	}
28481300Sgjelinek 
28491300Sgjelinek 	(void) zonecfg_enddsent(t_handle);
28501300Sgjelinek }
28511300Sgjelinek 
28521300Sgjelinek static int
28531300Sgjelinek validate_clone(char *source_zone, char *target_zone)
28541300Sgjelinek {
28551300Sgjelinek 	int err = Z_OK;
28561300Sgjelinek 	zone_dochandle_t s_handle;
28571300Sgjelinek 	zone_dochandle_t t_handle;
28581300Sgjelinek 
28591300Sgjelinek 	if ((t_handle = zonecfg_init_handle()) == NULL) {
28601300Sgjelinek 		zperror(cmd_to_str(CMD_CLONE), B_TRUE);
28611300Sgjelinek 		return (Z_ERR);
28621300Sgjelinek 	}
28631300Sgjelinek 	if ((err = zonecfg_get_handle(target_zone, t_handle)) != Z_OK) {
28641300Sgjelinek 		errno = err;
28651300Sgjelinek 		zperror(cmd_to_str(CMD_CLONE), B_TRUE);
28661300Sgjelinek 		zonecfg_fini_handle(t_handle);
28671300Sgjelinek 		return (Z_ERR);
28681300Sgjelinek 	}
28691300Sgjelinek 
28701300Sgjelinek 	if ((s_handle = zonecfg_init_handle()) == NULL) {
28711300Sgjelinek 		zperror(cmd_to_str(CMD_CLONE), B_TRUE);
28721300Sgjelinek 		zonecfg_fini_handle(t_handle);
28731300Sgjelinek 		return (Z_ERR);
28741300Sgjelinek 	}
28751300Sgjelinek 	if ((err = zonecfg_get_handle(source_zone, s_handle)) != Z_OK) {
28761300Sgjelinek 		errno = err;
28771300Sgjelinek 		zperror(cmd_to_str(CMD_CLONE), B_TRUE);
28781300Sgjelinek 		goto done;
28791300Sgjelinek 	}
28801300Sgjelinek 
28811300Sgjelinek 	/* verify new zone has same inherit-pkg-dirs */
28821300Sgjelinek 	err = valid_ipd_clone(s_handle, source_zone, t_handle, target_zone);
28831300Sgjelinek 
28841300Sgjelinek 	/* warn about imported fs's which are the same */
28851300Sgjelinek 	warn_fs_match(s_handle, source_zone, t_handle, target_zone);
28861300Sgjelinek 
28871300Sgjelinek 	/* warn about imported IP addresses which are the same */
28881300Sgjelinek 	warn_ip_match(s_handle, source_zone, t_handle, target_zone);
28891300Sgjelinek 
28901300Sgjelinek 	/* warn about imported devices which are the same */
28911300Sgjelinek 	warn_dev_match(s_handle, source_zone, t_handle, target_zone);
28921300Sgjelinek 
28931300Sgjelinek 	/* warn about imported datasets which are the same */
28941300Sgjelinek 	warn_dataset_match(s_handle, source_zone, t_handle, target_zone);
28951300Sgjelinek 
28961300Sgjelinek done:
28971300Sgjelinek 	zonecfg_fini_handle(t_handle);
28981300Sgjelinek 	zonecfg_fini_handle(s_handle);
28991300Sgjelinek 
29001300Sgjelinek 	return ((err == Z_OK) ? Z_OK : Z_ERR);
29011300Sgjelinek }
29021300Sgjelinek 
29031300Sgjelinek static int
29041300Sgjelinek copy_zone(char *src, char *dst)
29051300Sgjelinek {
29061300Sgjelinek 	boolean_t out_null = B_FALSE;
29071300Sgjelinek 	int status;
29081300Sgjelinek 	int err;
29091300Sgjelinek 	char *outfile;
29101300Sgjelinek 	char cmdbuf[MAXPATHLEN * 2 + 128];
29111300Sgjelinek 
29121300Sgjelinek 	if ((outfile = tempnam("/var/log", "zone")) == NULL) {
29131300Sgjelinek 		outfile = "/dev/null";
29141300Sgjelinek 		out_null = B_TRUE;
29151300Sgjelinek 	}
29161300Sgjelinek 
29171300Sgjelinek 	(void) snprintf(cmdbuf, sizeof (cmdbuf),
29181300Sgjelinek 	    "cd %s && /usr/bin/find . -depth -print | "
2919*1607Sgjelinek 	    "/usr/bin/egrep -v '^\\./\\.zfs$|^\\./\\.zfs/' | "
29201300Sgjelinek 	    "/usr/bin/cpio -pdmuP@ %s > %s 2>&1",
29211300Sgjelinek 	    src, dst, outfile);
29221300Sgjelinek 
29231300Sgjelinek 	status = do_subproc(cmdbuf);
29241300Sgjelinek 
29251300Sgjelinek 	if ((err = subproc_status("copy", status)) != Z_OK) {
29261300Sgjelinek 		if (!out_null)
29271300Sgjelinek 			(void) fprintf(stderr, gettext("\nThe copy failed.\n"
29281300Sgjelinek 			    "More information can be found in %s\n"), outfile);
29291300Sgjelinek 		return (err);
29301300Sgjelinek 	}
29311300Sgjelinek 
29321300Sgjelinek 	if (!out_null)
29331300Sgjelinek 		(void) unlink(outfile);
29341300Sgjelinek 
29351300Sgjelinek 	return (Z_OK);
29361300Sgjelinek }
29371300Sgjelinek 
29381300Sgjelinek /*
29391568Sgjelinek  * Run sys-unconfig on a zone.  This will leave the zone in the installed
29401568Sgjelinek  * state as long as there were no errors during the sys-unconfig.
29411300Sgjelinek  */
29421300Sgjelinek static int
29431568Sgjelinek unconfigure_zone(char *zonepath)
29441300Sgjelinek {
29451568Sgjelinek 	int		err;
29461568Sgjelinek 	int		status;
29471568Sgjelinek 	struct stat	unconfig_buf;
29481568Sgjelinek 	zone_cmd_arg_t	zarg;
29491568Sgjelinek 	char		cmdbuf[MAXPATHLEN + 51];
29501568Sgjelinek 
29511568Sgjelinek 	/* The zone has to be installed in order to mount the scratch zone. */
29521568Sgjelinek 	if ((err = zone_set_state(target_zone, ZONE_STATE_INSTALLED)) != Z_OK) {
29531568Sgjelinek 		errno = err;
29541568Sgjelinek 		zperror2(target_zone, gettext("could not set state"));
29551568Sgjelinek 		return (Z_ERR);
29561568Sgjelinek 	}
29571568Sgjelinek 
29581568Sgjelinek 	/*
29591568Sgjelinek 	 * Check if the zone is already sys-unconfiged.  This saves us
29601568Sgjelinek 	 * the work of bringing up the scratch zone so we can unconfigure it.
29611568Sgjelinek 	 */
29621568Sgjelinek 	(void) snprintf(cmdbuf, sizeof (cmdbuf), "%s/root/etc/.UNCONFIGURED",
29631568Sgjelinek 	    zonepath);
29641568Sgjelinek 	if (stat(cmdbuf, &unconfig_buf) == 0)
29651568Sgjelinek 		return (Z_OK);
29661568Sgjelinek 
29671568Sgjelinek 	zarg.cmd = Z_MOUNT;
29681568Sgjelinek 	if (call_zoneadmd(target_zone, &zarg) != 0) {
29691568Sgjelinek 		zerror(gettext("call to %s failed"), "zoneadmd");
29701568Sgjelinek 		(void) zone_set_state(target_zone, ZONE_STATE_INCOMPLETE);
29711568Sgjelinek 		return (Z_ERR);
29721568Sgjelinek 	}
29731300Sgjelinek 
29741300Sgjelinek 	(void) snprintf(cmdbuf, sizeof (cmdbuf),
29751568Sgjelinek 	    "/usr/sbin/zlogin -S %s /usr/sbin/sys-unconfig -R /a", target_zone);
29761568Sgjelinek 
29771568Sgjelinek 	status = do_subproc(cmdbuf);
29781568Sgjelinek 	if ((err = subproc_status("sys-unconfig", status)) != Z_OK) {
29791568Sgjelinek 		errno = err;
29801568Sgjelinek 		zperror2(target_zone, gettext("sys-unconfig failed\n"));
29811568Sgjelinek 		(void) zone_set_state(target_zone, ZONE_STATE_INCOMPLETE);
29821300Sgjelinek 	}
29831300Sgjelinek 
29841568Sgjelinek 	zarg.cmd = Z_UNMOUNT;
29851568Sgjelinek 	if (call_zoneadmd(target_zone, &zarg) != 0) {
29861568Sgjelinek 		zerror(gettext("call to %s failed"), "zoneadmd");
29871568Sgjelinek 		(void) fprintf(stderr, gettext("could not unmount zone\n"));
29881568Sgjelinek 		return (Z_ERR);
29891568Sgjelinek 	}
29901568Sgjelinek 
29911568Sgjelinek 	return ((err == Z_OK) ? Z_OK : Z_ERR);
29921300Sgjelinek }
29931300Sgjelinek 
29941300Sgjelinek /* ARGSUSED */
29951300Sgjelinek int
29961300Sgjelinek zfm_print(const char *p, void *r) {
29971300Sgjelinek 	zerror("  %s\n", p);
29981300Sgjelinek 	return (0);
29991300Sgjelinek }
30001300Sgjelinek 
30011300Sgjelinek static int
30021300Sgjelinek clone_func(int argc, char *argv[])
30031300Sgjelinek {
30041300Sgjelinek 	char *source_zone = NULL;
30051300Sgjelinek 	int lockfd;
30061300Sgjelinek 	int err, arg;
30071300Sgjelinek 	char zonepath[MAXPATHLEN];
30081300Sgjelinek 	char source_zonepath[MAXPATHLEN];
30091300Sgjelinek 	zone_state_t state;
30101300Sgjelinek 	zone_entry_t *zent;
30111300Sgjelinek 	char *method = "copy";
30121300Sgjelinek 
30131300Sgjelinek 	if (zonecfg_in_alt_root()) {
30141300Sgjelinek 		zerror(gettext("cannot clone zone in alternate root"));
30151300Sgjelinek 		return (Z_ERR);
30161300Sgjelinek 	}
30171300Sgjelinek 
30181300Sgjelinek 	optind = 0;
30191300Sgjelinek 	if ((arg = getopt(argc, argv, "?m:")) != EOF) {
30201300Sgjelinek 		switch (arg) {
30211300Sgjelinek 		case '?':
30221300Sgjelinek 			sub_usage(SHELP_CLONE, CMD_CLONE);
30231300Sgjelinek 			return (optopt == '?' ? Z_OK : Z_USAGE);
30241300Sgjelinek 		case 'm':
30251300Sgjelinek 			method = optarg;
30261300Sgjelinek 			break;
30271300Sgjelinek 		default:
30281300Sgjelinek 			sub_usage(SHELP_CLONE, CMD_CLONE);
30291300Sgjelinek 			return (Z_USAGE);
30301300Sgjelinek 		}
30311300Sgjelinek 	}
30321300Sgjelinek 	if (argc != (optind + 1) || strcmp(method, "copy") != 0) {
30331300Sgjelinek 		sub_usage(SHELP_CLONE, CMD_CLONE);
30341300Sgjelinek 		return (Z_USAGE);
30351300Sgjelinek 	}
30361300Sgjelinek 	source_zone = argv[optind];
30371300Sgjelinek 	if (sanity_check(target_zone, CMD_CLONE, B_FALSE, B_TRUE) != Z_OK)
30381300Sgjelinek 		return (Z_ERR);
30391300Sgjelinek 	if (verify_details(CMD_CLONE) != Z_OK)
30401300Sgjelinek 		return (Z_ERR);
30411300Sgjelinek 
30421300Sgjelinek 	/*
30431300Sgjelinek 	 * We also need to do some extra validation on the source zone.
30441300Sgjelinek 	 */
30451300Sgjelinek 
30461300Sgjelinek 	if (strcmp(source_zone, GLOBAL_ZONENAME) == 0) {
30471300Sgjelinek 		zerror(gettext("%s operation is invalid for the global zone."),
30481300Sgjelinek 		    cmd_to_str(CMD_CLONE));
30491300Sgjelinek 		return (Z_ERR);
30501300Sgjelinek 	}
30511300Sgjelinek 
30521300Sgjelinek 	if (strncmp(source_zone, "SUNW", 4) == 0) {
30531300Sgjelinek 		zerror(gettext("%s operation is invalid for zones starting "
30541300Sgjelinek 		    "with SUNW."), cmd_to_str(CMD_CLONE));
30551300Sgjelinek 		return (Z_ERR);
30561300Sgjelinek 	}
30571300Sgjelinek 
30581300Sgjelinek 	zent = lookup_running_zone(source_zone);
30591300Sgjelinek 	if (zent != NULL) {
30601300Sgjelinek 		/* check whether the zone is ready or running */
30611300Sgjelinek 		if ((err = zone_get_state(zent->zname, &zent->zstate_num))
30621300Sgjelinek 		    != Z_OK) {
30631300Sgjelinek 			errno = err;
30641300Sgjelinek 			zperror2(zent->zname, gettext("could not get state"));
30651300Sgjelinek 			/* can't tell, so hedge */
30661300Sgjelinek 			zent->zstate_str = "ready/running";
30671300Sgjelinek 		} else {
30681300Sgjelinek 			zent->zstate_str = zone_state_str(zent->zstate_num);
30691300Sgjelinek 		}
30701300Sgjelinek 		zerror(gettext("%s operation is invalid for %s zones."),
30711300Sgjelinek 		    cmd_to_str(CMD_CLONE), zent->zstate_str);
30721300Sgjelinek 		return (Z_ERR);
30731300Sgjelinek 	}
30741300Sgjelinek 
30751300Sgjelinek 	if ((err = zone_get_state(source_zone, &state)) != Z_OK) {
30761300Sgjelinek 		errno = err;
30771300Sgjelinek 		zperror2(source_zone, gettext("could not get state"));
30781300Sgjelinek 		return (Z_ERR);
30791300Sgjelinek 	}
30801300Sgjelinek 	if (state != ZONE_STATE_INSTALLED) {
30811300Sgjelinek 		(void) fprintf(stderr,
30821300Sgjelinek 		    gettext("%s: zone %s is %s; %s is required.\n"),
30831300Sgjelinek 		    execname, source_zone, zone_state_str(state),
30841300Sgjelinek 		    zone_state_str(ZONE_STATE_INSTALLED));
30851300Sgjelinek 		return (Z_ERR);
30861300Sgjelinek 	}
30871300Sgjelinek 
30881300Sgjelinek 	/*
30891300Sgjelinek 	 * The source zone checks out ok, continue with the clone.
30901300Sgjelinek 	 */
30911300Sgjelinek 
30921300Sgjelinek 	if (validate_clone(source_zone, target_zone) != Z_OK)
30931300Sgjelinek 		return (Z_ERR);
30941300Sgjelinek 
30951300Sgjelinek 	if (grab_lock_file(target_zone, &lockfd) != Z_OK) {
30961300Sgjelinek 		zerror(gettext("another %s may have an operation in progress."),
30971300Sgjelinek 		    "zoneadm");
30981300Sgjelinek 		return (Z_ERR);
30991300Sgjelinek 	}
31001300Sgjelinek 
31011300Sgjelinek 	if ((err = zone_get_zonepath(source_zone, source_zonepath,
31021300Sgjelinek 	    sizeof (source_zonepath))) != Z_OK) {
31031300Sgjelinek 		errno = err;
31041300Sgjelinek 		zperror2(source_zone, gettext("could not get zone path"));
31051300Sgjelinek 		goto done;
31061300Sgjelinek 	}
31071300Sgjelinek 
31081300Sgjelinek 	if ((err = zone_get_zonepath(target_zone, zonepath, sizeof (zonepath)))
31091300Sgjelinek 	    != Z_OK) {
31101300Sgjelinek 		errno = err;
31111300Sgjelinek 		zperror2(target_zone, gettext("could not get zone path"));
31121300Sgjelinek 		goto done;
31131300Sgjelinek 	}
31141300Sgjelinek 
31151300Sgjelinek 	/* Don't clone the zone if anything is still mounted there */
31161300Sgjelinek 	if (zonecfg_find_mounts(source_zonepath, NULL, NULL)) {
31171300Sgjelinek 		zerror(gettext("These file-systems are mounted on "
31181300Sgjelinek 		    "subdirectories of %s.\n"), source_zonepath);
31191300Sgjelinek 		(void) zonecfg_find_mounts(source_zonepath, zfm_print, NULL);
31201300Sgjelinek 		err = Z_ERR;
31211300Sgjelinek 		goto done;
31221300Sgjelinek 	}
31231300Sgjelinek 
31241300Sgjelinek 	if ((err = zone_set_state(target_zone, ZONE_STATE_INCOMPLETE))
31251300Sgjelinek 	    != Z_OK) {
31261300Sgjelinek 		errno = err;
31271300Sgjelinek 		zperror2(target_zone, gettext("could not set state"));
31281300Sgjelinek 		goto done;
31291300Sgjelinek 	}
31301300Sgjelinek 
31311300Sgjelinek 	(void) printf(gettext("Cloning zonepath %s..."), source_zonepath);
31321300Sgjelinek 	(void) fflush(stdout);
31331300Sgjelinek 
31341568Sgjelinek 	err = copy_zone(source_zonepath, zonepath);
31351568Sgjelinek 	(void) printf("\n");
31361568Sgjelinek 	if (err != Z_OK)
31371300Sgjelinek 		goto done;
31381568Sgjelinek 
31391568Sgjelinek 	err = unconfigure_zone(zonepath);
31401300Sgjelinek 
31411300Sgjelinek done:
31421300Sgjelinek 	release_lock_file(lockfd);
31431300Sgjelinek 	return ((err == Z_OK) ? Z_OK : Z_ERR);
31441300Sgjelinek }
31451300Sgjelinek 
31461300Sgjelinek #define	RMCOMMAND	"/usr/bin/rm -rf"
31471300Sgjelinek 
3148*1607Sgjelinek /*
3149*1607Sgjelinek  * Used when moving a zonepath (via copying) to clean up the old path or
3150*1607Sgjelinek  * the new path if there was an error.
3151*1607Sgjelinek  *
3152*1607Sgjelinek  * This function handles the case of a zonepath being a zfs filesystem.
3153*1607Sgjelinek  * If it is a zfs filesystem, we cannot just remove the whole zonepath
3154*1607Sgjelinek  * since we can't remove the filesystem itself.  Instead, we have to remove
3155*1607Sgjelinek  * the contents of the filesystem, but not the .zfs directory.
3156*1607Sgjelinek  */
3157*1607Sgjelinek static int
3158*1607Sgjelinek remove_zonepath(char *zonepath)
3159*1607Sgjelinek {
3160*1607Sgjelinek 	int status;
3161*1607Sgjelinek 	boolean_t is_zfs = B_FALSE;
3162*1607Sgjelinek 	struct stat buf;
3163*1607Sgjelinek 	char cmdbuf[sizeof (RMCOMMAND) + MAXPATHLEN + 128];
3164*1607Sgjelinek 
3165*1607Sgjelinek 	(void) snprintf(cmdbuf, sizeof (cmdbuf), "%s/.zfs", zonepath);
3166*1607Sgjelinek 
3167*1607Sgjelinek 	if (stat(cmdbuf, &buf) == 0 && S_ISDIR(buf.st_mode))
3168*1607Sgjelinek 		is_zfs = B_TRUE;
3169*1607Sgjelinek 
3170*1607Sgjelinek 	if (is_zfs) {
3171*1607Sgjelinek 		/*
3172*1607Sgjelinek 		 * This doesn't handle the (unlikely) case that there are
3173*1607Sgjelinek 		 * directories or files in the top-level zonepath with white
3174*1607Sgjelinek 		 * space in the names.
3175*1607Sgjelinek 		 */
3176*1607Sgjelinek 		(void) snprintf(cmdbuf, sizeof (cmdbuf),
3177*1607Sgjelinek 		    "cd %s && /usr/bin/ls -A | /usr/bin/egrep -v '^\\.zfs$' | "
3178*1607Sgjelinek 		    "/usr/bin/xargs " RMCOMMAND, zonepath);
3179*1607Sgjelinek 	} else {
3180*1607Sgjelinek 		/*
3181*1607Sgjelinek 		 * "exec" the command so that the returned status is
3182*1607Sgjelinek 		 * that of rm and not the shell.
3183*1607Sgjelinek 		 */
3184*1607Sgjelinek 		(void) snprintf(cmdbuf, sizeof (cmdbuf),
3185*1607Sgjelinek 		    "exec " RMCOMMAND " %s", zonepath);
3186*1607Sgjelinek 	}
3187*1607Sgjelinek 
3188*1607Sgjelinek 	status = do_subproc(cmdbuf);
3189*1607Sgjelinek 
3190*1607Sgjelinek 	return (subproc_status("rm", status));
3191*1607Sgjelinek 
3192*1607Sgjelinek }
3193*1607Sgjelinek 
31941300Sgjelinek static int
31951300Sgjelinek move_func(int argc, char *argv[])
31961300Sgjelinek {
31971300Sgjelinek 	char *new_zonepath = NULL;
31981300Sgjelinek 	int lockfd;
31991300Sgjelinek 	int err, arg;
32001300Sgjelinek 	char zonepath[MAXPATHLEN];
32011300Sgjelinek 	zone_dochandle_t handle;
32021300Sgjelinek 	boolean_t fast;
32031300Sgjelinek 	boolean_t revert;
32041300Sgjelinek 	struct stat zonepath_buf;
32051300Sgjelinek 	struct stat new_zonepath_buf;
32061300Sgjelinek 
32071300Sgjelinek 	if (zonecfg_in_alt_root()) {
32081300Sgjelinek 		zerror(gettext("cannot move zone in alternate root"));
32091300Sgjelinek 		return (Z_ERR);
32101300Sgjelinek 	}
32111300Sgjelinek 
32121300Sgjelinek 	optind = 0;
32131300Sgjelinek 	if ((arg = getopt(argc, argv, "?")) != EOF) {
32141300Sgjelinek 		switch (arg) {
32151300Sgjelinek 		case '?':
32161300Sgjelinek 			sub_usage(SHELP_MOVE, CMD_MOVE);
32171300Sgjelinek 			return (optopt == '?' ? Z_OK : Z_USAGE);
32181300Sgjelinek 		default:
32191300Sgjelinek 			sub_usage(SHELP_MOVE, CMD_MOVE);
32201300Sgjelinek 			return (Z_USAGE);
32211300Sgjelinek 		}
32221300Sgjelinek 	}
32231300Sgjelinek 	if (argc != (optind + 1)) {
32241300Sgjelinek 		sub_usage(SHELP_MOVE, CMD_MOVE);
32251300Sgjelinek 		return (Z_USAGE);
32261300Sgjelinek 	}
32271300Sgjelinek 	new_zonepath = argv[optind];
32281300Sgjelinek 	if (sanity_check(target_zone, CMD_MOVE, B_FALSE, B_TRUE) != Z_OK)
32291300Sgjelinek 		return (Z_ERR);
32301300Sgjelinek 	if (verify_details(CMD_MOVE) != Z_OK)
32311300Sgjelinek 		return (Z_ERR);
32321300Sgjelinek 
32331300Sgjelinek 	/*
32341300Sgjelinek 	 * Check out the new zonepath.  This has the side effect of creating
32351300Sgjelinek 	 * a directory for the new zonepath.  We depend on this later when we
32361300Sgjelinek 	 * stat to see if we are doing a cross file-system move or not.
32371300Sgjelinek 	 */
32381300Sgjelinek 	if (validate_zonepath(new_zonepath, CMD_MOVE) != Z_OK)
32391300Sgjelinek 		return (Z_ERR);
32401300Sgjelinek 
32411300Sgjelinek 	if ((err = zone_get_zonepath(target_zone, zonepath, sizeof (zonepath)))
32421300Sgjelinek 	    != Z_OK) {
32431300Sgjelinek 		errno = err;
32441300Sgjelinek 		zperror2(target_zone, gettext("could not get zone path"));
32451300Sgjelinek 		return (Z_ERR);
32461300Sgjelinek 	}
32471300Sgjelinek 
32481300Sgjelinek 	if (stat(zonepath, &zonepath_buf) == -1) {
32491300Sgjelinek 		zperror(gettext("could not stat zone path"), B_FALSE);
32501300Sgjelinek 		return (Z_ERR);
32511300Sgjelinek 	}
32521300Sgjelinek 
32531300Sgjelinek 	if (stat(new_zonepath, &new_zonepath_buf) == -1) {
32541300Sgjelinek 		zperror(gettext("could not stat new zone path"), B_FALSE);
32551300Sgjelinek 		return (Z_ERR);
32561300Sgjelinek 	}
32571300Sgjelinek 
32581300Sgjelinek 	/* Don't move the zone if anything is still mounted there */
32591300Sgjelinek 	if (zonecfg_find_mounts(zonepath, NULL, NULL)) {
32601300Sgjelinek 		zerror(gettext("These file-systems are mounted on "
32611300Sgjelinek 		    "subdirectories of %s.\n"), zonepath);
32621300Sgjelinek 		(void) zonecfg_find_mounts(zonepath, zfm_print, NULL);
32631300Sgjelinek 		return (Z_ERR);
32641300Sgjelinek 	}
32651300Sgjelinek 
32661300Sgjelinek 	/*
32671300Sgjelinek 	 * Check if we are moving in the same filesystem and can do a fast
32681300Sgjelinek 	 * move or if we are crossing filesystems and have to copy the data.
32691300Sgjelinek 	 */
32701300Sgjelinek 	fast = (zonepath_buf.st_dev == new_zonepath_buf.st_dev);
32711300Sgjelinek 
32721300Sgjelinek 	if ((handle = zonecfg_init_handle()) == NULL) {
32731300Sgjelinek 		zperror(cmd_to_str(CMD_MOVE), B_TRUE);
32741300Sgjelinek 		return (Z_ERR);
32751300Sgjelinek 	}
32761300Sgjelinek 
32771300Sgjelinek 	if ((err = zonecfg_get_handle(target_zone, handle)) != Z_OK) {
32781300Sgjelinek 		errno = err;
32791300Sgjelinek 		zperror(cmd_to_str(CMD_MOVE), B_TRUE);
32801300Sgjelinek 		zonecfg_fini_handle(handle);
32811300Sgjelinek 		return (Z_ERR);
32821300Sgjelinek 	}
32831300Sgjelinek 
32841300Sgjelinek 	if (grab_lock_file(target_zone, &lockfd) != Z_OK) {
32851300Sgjelinek 		zerror(gettext("another %s may have an operation in progress."),
32861300Sgjelinek 		    "zoneadm");
32871300Sgjelinek 		zonecfg_fini_handle(handle);
32881300Sgjelinek 		return (Z_ERR);
32891300Sgjelinek 	}
32901300Sgjelinek 
32911300Sgjelinek 	/*
32921300Sgjelinek 	 * We're making some file-system changes now so we have to clean up
32931300Sgjelinek 	 * the file-system before we are done.  This will either clean up the
32941300Sgjelinek 	 * new zonepath if the zonecfg update failed or it will clean up the
32951300Sgjelinek 	 * old zonepath if everything is ok.
32961300Sgjelinek 	 */
32971300Sgjelinek 	revert = B_TRUE;
32981300Sgjelinek 
32991300Sgjelinek 	if (fast) {
33001300Sgjelinek 		/* same filesystem, use rename for a quick move */
33011300Sgjelinek 
33021300Sgjelinek 		/*
33031300Sgjelinek 		 * Remove the new_zonepath directory that got created above
33041300Sgjelinek 		 * during the validation.  It gets in the way of the rename.
33051300Sgjelinek 		 */
33061300Sgjelinek 		if (rmdir(new_zonepath) != 0) {
33071300Sgjelinek 			zperror(gettext("could not rmdir new zone path"),
33081300Sgjelinek 			    B_FALSE);
33091300Sgjelinek 			zonecfg_fini_handle(handle);
33101300Sgjelinek 			release_lock_file(lockfd);
33111300Sgjelinek 			return (Z_ERR);
33121300Sgjelinek 		}
33131300Sgjelinek 
33141300Sgjelinek 		if (rename(zonepath, new_zonepath) != 0) {
33151300Sgjelinek 			/*
33161300Sgjelinek 			 * If this fails we don't need to do all of the
33171300Sgjelinek 			 * cleanup that happens for the rest of the code
33181300Sgjelinek 			 * so just return from this error.
33191300Sgjelinek 			 */
33201300Sgjelinek 			zperror(gettext("could not move zone"), B_FALSE);
33211300Sgjelinek 			zonecfg_fini_handle(handle);
33221300Sgjelinek 			release_lock_file(lockfd);
33231300Sgjelinek 			return (Z_ERR);
33241300Sgjelinek 		}
33251300Sgjelinek 
33261300Sgjelinek 	} else {
33271300Sgjelinek 		(void) printf(gettext(
33281300Sgjelinek 		    "Moving across file-systems; copying zonepath %s..."),
33291300Sgjelinek 		    zonepath);
33301300Sgjelinek 		(void) fflush(stdout);
33311300Sgjelinek 
33321300Sgjelinek 		err = copy_zone(zonepath, new_zonepath);
33331300Sgjelinek 
33341300Sgjelinek 		(void) printf("\n");
33351300Sgjelinek 		if (err != Z_OK)
33361300Sgjelinek 			goto done;
33371300Sgjelinek 	}
33381300Sgjelinek 
33391300Sgjelinek 	if ((err = zonecfg_set_zonepath(handle, new_zonepath)) != Z_OK) {
33401300Sgjelinek 		errno = err;
33411300Sgjelinek 		zperror(gettext("could not set new zonepath"), B_TRUE);
33421300Sgjelinek 		goto done;
33431300Sgjelinek 	}
33441300Sgjelinek 
33451300Sgjelinek 	if ((err = zonecfg_save(handle)) != Z_OK) {
33461300Sgjelinek 		errno = err;
33471300Sgjelinek 		zperror(gettext("zonecfg save failed"), B_TRUE);
33481300Sgjelinek 		goto done;
33491300Sgjelinek 	}
33501300Sgjelinek 
33511300Sgjelinek 	revert = B_FALSE;
33521300Sgjelinek 
33531300Sgjelinek done:
33541300Sgjelinek 	zonecfg_fini_handle(handle);
33551300Sgjelinek 	release_lock_file(lockfd);
33561300Sgjelinek 
33571300Sgjelinek 	/*
33581300Sgjelinek 	 * Clean up the file-system based on how things went.  We either
33591300Sgjelinek 	 * clean up the new zonepath if the operation failed for some reason
33601300Sgjelinek 	 * or we clean up the old zonepath if everything is ok.
33611300Sgjelinek 	 */
33621300Sgjelinek 	if (revert) {
33631300Sgjelinek 		/* The zonecfg update failed, cleanup the new zonepath. */
33641300Sgjelinek 		if (fast) {
33651300Sgjelinek 			if (rename(new_zonepath, zonepath) != 0) {
33661300Sgjelinek 				zperror(gettext("could not restore zonepath"),
33671300Sgjelinek 				    B_FALSE);
33681300Sgjelinek 				/*
33691300Sgjelinek 				 * err is already != Z_OK since we're reverting
33701300Sgjelinek 				 */
33711300Sgjelinek 			}
33721300Sgjelinek 		} else {
33731300Sgjelinek 			(void) printf(gettext("Cleaning up zonepath %s..."),
33741300Sgjelinek 			    new_zonepath);
33751300Sgjelinek 			(void) fflush(stdout);
3376*1607Sgjelinek 			err = remove_zonepath(new_zonepath);
33771300Sgjelinek 			(void) printf("\n");
33781300Sgjelinek 
3379*1607Sgjelinek 			if (err != Z_OK) {
33801300Sgjelinek 				errno = err;
33811300Sgjelinek 				zperror(gettext("could not remove new "
33821300Sgjelinek 				    "zonepath"), B_TRUE);
33831300Sgjelinek 			} else {
33841300Sgjelinek 				/*
33851300Sgjelinek 				 * Because we're reverting we know the mainline
33861300Sgjelinek 				 * code failed but we just reused the err
33871300Sgjelinek 				 * variable so we reset it back to Z_ERR.
33881300Sgjelinek 				 */
33891300Sgjelinek 				err = Z_ERR;
33901300Sgjelinek 			}
33911300Sgjelinek 		}
33921300Sgjelinek 
33931300Sgjelinek 	} else {
33941300Sgjelinek 		/* The move was successful, cleanup the old zonepath. */
33951300Sgjelinek 		if (!fast) {
33961300Sgjelinek 			(void) printf(
33971300Sgjelinek 			    gettext("Cleaning up zonepath %s..."), zonepath);
33981300Sgjelinek 			(void) fflush(stdout);
3399*1607Sgjelinek 			err = remove_zonepath(zonepath);
34001300Sgjelinek 			(void) printf("\n");
34011300Sgjelinek 
3402*1607Sgjelinek 			if (err != Z_OK) {
34031300Sgjelinek 				errno = err;
34041300Sgjelinek 				zperror(gettext("could not remove zonepath"),
34051300Sgjelinek 				    B_TRUE);
34061300Sgjelinek 			}
34071300Sgjelinek 		}
34081300Sgjelinek 	}
34091300Sgjelinek 
34101300Sgjelinek 	return ((err == Z_OK) ? Z_OK : Z_ERR);
34111300Sgjelinek }
34121300Sgjelinek 
34131507Sgjelinek static int
34141507Sgjelinek detach_func(int argc, char *argv[])
34151507Sgjelinek {
34161507Sgjelinek 	int lockfd;
34171507Sgjelinek 	int err, arg;
34181507Sgjelinek 	char zonepath[MAXPATHLEN];
34191507Sgjelinek 	zone_dochandle_t handle;
34201507Sgjelinek 
34211507Sgjelinek 	if (zonecfg_in_alt_root()) {
34221507Sgjelinek 		zerror(gettext("cannot detach zone in alternate root"));
34231507Sgjelinek 		return (Z_ERR);
34241507Sgjelinek 	}
34251507Sgjelinek 
34261507Sgjelinek 	optind = 0;
34271507Sgjelinek 	if ((arg = getopt(argc, argv, "?")) != EOF) {
34281507Sgjelinek 		switch (arg) {
34291507Sgjelinek 		case '?':
34301507Sgjelinek 			sub_usage(SHELP_DETACH, CMD_DETACH);
34311507Sgjelinek 			return (optopt == '?' ? Z_OK : Z_USAGE);
34321507Sgjelinek 		default:
34331507Sgjelinek 			sub_usage(SHELP_DETACH, CMD_DETACH);
34341507Sgjelinek 			return (Z_USAGE);
34351507Sgjelinek 		}
34361507Sgjelinek 	}
34371507Sgjelinek 	if (sanity_check(target_zone, CMD_DETACH, B_FALSE, B_TRUE) != Z_OK)
34381507Sgjelinek 		return (Z_ERR);
34391507Sgjelinek 	if (verify_details(CMD_DETACH) != Z_OK)
34401507Sgjelinek 		return (Z_ERR);
34411507Sgjelinek 
34421507Sgjelinek 	if ((err = zone_get_zonepath(target_zone, zonepath, sizeof (zonepath)))
34431507Sgjelinek 	    != Z_OK) {
34441507Sgjelinek 		errno = err;
34451507Sgjelinek 		zperror2(target_zone, gettext("could not get zone path"));
34461507Sgjelinek 		return (Z_ERR);
34471507Sgjelinek 	}
34481507Sgjelinek 
34491507Sgjelinek 	/* Don't detach the zone if anything is still mounted there */
34501507Sgjelinek 	if (zonecfg_find_mounts(zonepath, NULL, NULL)) {
34511507Sgjelinek 		zerror(gettext("These file-systems are mounted on "
34521507Sgjelinek 		    "subdirectories of %s.\n"), zonepath);
34531507Sgjelinek 		(void) zonecfg_find_mounts(zonepath, zfm_print, NULL);
34541507Sgjelinek 		return (Z_ERR);
34551507Sgjelinek 	}
34561507Sgjelinek 
34571507Sgjelinek 	if ((handle = zonecfg_init_handle()) == NULL) {
34581507Sgjelinek 		zperror(cmd_to_str(CMD_DETACH), B_TRUE);
34591507Sgjelinek 		return (Z_ERR);
34601507Sgjelinek 	}
34611507Sgjelinek 
34621507Sgjelinek 	if ((err = zonecfg_get_handle(target_zone, handle)) != Z_OK) {
34631507Sgjelinek 		errno = err;
34641507Sgjelinek 		zperror(cmd_to_str(CMD_DETACH), B_TRUE);
34651507Sgjelinek 		zonecfg_fini_handle(handle);
34661507Sgjelinek 		return (Z_ERR);
34671507Sgjelinek 	}
34681507Sgjelinek 
34691507Sgjelinek 	if (grab_lock_file(target_zone, &lockfd) != Z_OK) {
34701507Sgjelinek 		zerror(gettext("another %s may have an operation in progress."),
34711507Sgjelinek 		    "zoneadm");
34721507Sgjelinek 		zonecfg_fini_handle(handle);
34731507Sgjelinek 		return (Z_ERR);
34741507Sgjelinek 	}
34751507Sgjelinek 
34761507Sgjelinek 	if ((err = zonecfg_get_detach_info(handle, B_TRUE)) != Z_OK) {
34771507Sgjelinek 		errno = err;
34781507Sgjelinek 		zperror(gettext("getting the detach information failed"),
34791507Sgjelinek 		    B_TRUE);
34801507Sgjelinek 		goto done;
34811507Sgjelinek 	}
34821507Sgjelinek 
34831507Sgjelinek 	if ((err = zonecfg_detach_save(handle)) != Z_OK) {
34841507Sgjelinek 		errno = err;
34851507Sgjelinek 		zperror(gettext("saving the detach manifest failed"), B_TRUE);
34861507Sgjelinek 		goto done;
34871507Sgjelinek 	}
34881507Sgjelinek 
34891507Sgjelinek 	if ((err = zone_set_state(target_zone, ZONE_STATE_CONFIGURED))
34901507Sgjelinek 	    != Z_OK) {
34911507Sgjelinek 		errno = err;
34921507Sgjelinek 		zperror(gettext("could not reset state"), B_TRUE);
34931507Sgjelinek 	}
34941507Sgjelinek 
34951507Sgjelinek done:
34961507Sgjelinek 	zonecfg_fini_handle(handle);
34971507Sgjelinek 	release_lock_file(lockfd);
34981507Sgjelinek 
34991507Sgjelinek 	return ((err == Z_OK) ? Z_OK : Z_ERR);
35001507Sgjelinek }
35011507Sgjelinek 
35021507Sgjelinek /*
35031507Sgjelinek  * Find the specified package in the sw inventory on the handle and check
35041507Sgjelinek  * if the version matches what is passed in.
35051507Sgjelinek  * Return 0 if the packages match
35061507Sgjelinek  *        1 if the package is found but we have a version mismatch
35071507Sgjelinek  *        -1 if the package is not found
35081507Sgjelinek  */
35091507Sgjelinek static int
35101507Sgjelinek pkg_cmp(zone_dochandle_t handle, char *pkg_name, char *pkg_vers,
35111507Sgjelinek     char *return_vers, int vers_size)
35121507Sgjelinek {
35131507Sgjelinek 	int res = -1;
35141507Sgjelinek 	struct zone_pkgtab pkgtab;
35151507Sgjelinek 
35161507Sgjelinek 	if (zonecfg_setpkgent(handle) != Z_OK) {
35171507Sgjelinek 		(void) fprintf(stderr,
35181507Sgjelinek 		    gettext("unable to enumerate packages\n"));
35191507Sgjelinek 		return (Z_ERR);
35201507Sgjelinek 	}
35211507Sgjelinek 
35221507Sgjelinek 	while (zonecfg_getpkgent(handle, &pkgtab) == Z_OK) {
35231507Sgjelinek 		if (strcmp(pkg_name, pkgtab.zone_pkg_name) != 0)
35241507Sgjelinek 			continue;
35251507Sgjelinek 
35261507Sgjelinek 		if (strcmp(pkg_vers, pkgtab.zone_pkg_version) == 0) {
35271507Sgjelinek 			res = 0;
35281507Sgjelinek 			break;
35291507Sgjelinek 		}
35301507Sgjelinek 
35311507Sgjelinek 		(void) strlcpy(return_vers, pkgtab.zone_pkg_version, vers_size);
35321507Sgjelinek 		res = 1;
35331507Sgjelinek 		break;
35341507Sgjelinek 	}
35351507Sgjelinek 
35361507Sgjelinek 	(void) zonecfg_endpkgent(handle);
35371507Sgjelinek 	return (res);
35381507Sgjelinek }
35391507Sgjelinek 
35401507Sgjelinek /*
35411507Sgjelinek  * Used in software comparisons to check the packages between the two zone
35421507Sgjelinek  * handles.  The packages have to match or we print a message telling the
35431507Sgjelinek  * user what is out of sync.  The src_cmp flag tells us if the first handle
35441507Sgjelinek  * is the source machine global zone or not.  This is used to enable the
35451507Sgjelinek  * right messages to be printed and also to enable extra version checking
35461507Sgjelinek  * that is not needed for the opposite comparison.
35471507Sgjelinek  */
35481507Sgjelinek static int
35491507Sgjelinek pkg_check(char *header, zone_dochandle_t handle1, zone_dochandle_t handle2,
35501507Sgjelinek     boolean_t src_cmp)
35511507Sgjelinek {
35521507Sgjelinek 	int			err;
35531507Sgjelinek 	int			res = Z_OK;
35541507Sgjelinek 	boolean_t		do_header = B_TRUE;
35551507Sgjelinek 	char			other_vers[ZONE_PKG_VERSMAX];
35561507Sgjelinek 	struct zone_pkgtab	pkgtab;
35571507Sgjelinek 
35581507Sgjelinek 	if (zonecfg_setpkgent(handle1) != Z_OK) {
35591507Sgjelinek 		(void) fprintf(stderr,
35601507Sgjelinek 		    gettext("unable to enumerate packages\n"));
35611507Sgjelinek 		return (Z_ERR);
35621507Sgjelinek 	}
35631507Sgjelinek 
35641507Sgjelinek 	while (zonecfg_getpkgent(handle1, &pkgtab) == Z_OK) {
35651507Sgjelinek 		if ((err = pkg_cmp(handle2, pkgtab.zone_pkg_name,
35661507Sgjelinek 		    pkgtab.zone_pkg_version, other_vers, sizeof (other_vers)))
35671507Sgjelinek 		    != 0) {
35681507Sgjelinek 			if (do_header && (err < 0 || src_cmp)) {
35691507Sgjelinek 				/* LINTED E_SEC_PRINTF_VAR_FMT */
35701507Sgjelinek 				(void) fprintf(stderr, header);
35711507Sgjelinek 				do_header = B_FALSE;
35721507Sgjelinek 			}
35731507Sgjelinek 			if (err < 0) {
35741507Sgjelinek 				(void) fprintf(stderr,
35751507Sgjelinek 				    (src_cmp == B_TRUE) ?
35761507Sgjelinek 				    gettext("\t%s: not installed\n\t\t(%s)\n") :
35771507Sgjelinek 				    gettext("\t%s (%s)\n"),
35781507Sgjelinek 				    pkgtab.zone_pkg_name,
35791507Sgjelinek 				    pkgtab.zone_pkg_version);
35801507Sgjelinek 				res = Z_ERR;
35811507Sgjelinek 			} else if (src_cmp) {
35821507Sgjelinek 				(void) fprintf(stderr, gettext(
35831507Sgjelinek 				    "\t%s: version mismatch\n\t\t(%s)"
35841507Sgjelinek 				    "\n\t\t(%s)\n"),
35851507Sgjelinek 				    pkgtab.zone_pkg_name,
35861507Sgjelinek 				    pkgtab.zone_pkg_version, other_vers);
35871507Sgjelinek 				res = Z_ERR;
35881507Sgjelinek 			}
35891507Sgjelinek 		}
35901507Sgjelinek 	}
35911507Sgjelinek 
35921507Sgjelinek 	(void) zonecfg_endpkgent(handle1);
35931507Sgjelinek 
35941507Sgjelinek 	return (res);
35951507Sgjelinek }
35961507Sgjelinek 
35971507Sgjelinek /*
35981507Sgjelinek  * Find the specified patch in the sw inventory on the handle and check
35991507Sgjelinek  * if the version matches what is passed in.
36001507Sgjelinek  * Return 0 if the patches match
36011507Sgjelinek  *        1 if the patches is found but we have a version mismatch
36021507Sgjelinek  *        -1 if the patches is not found
36031507Sgjelinek  */
36041507Sgjelinek static int
36051507Sgjelinek patch_cmp(zone_dochandle_t handle, char *patch_id, char *patch_vers,
36061507Sgjelinek     char *return_vers, int vers_size)
36071507Sgjelinek {
36081507Sgjelinek 	int			res = -1;
36091507Sgjelinek 	struct zone_patchtab	patchtab;
36101507Sgjelinek 
36111507Sgjelinek 	if (zonecfg_setpatchent(handle) != Z_OK) {
36121507Sgjelinek 		(void) fprintf(stderr,
36131507Sgjelinek 		    gettext("unable to enumerate patches\n"));
36141507Sgjelinek 		return (Z_ERR);
36151507Sgjelinek 	}
36161507Sgjelinek 
36171507Sgjelinek 	while (zonecfg_getpatchent(handle, &patchtab) == Z_OK) {
36181507Sgjelinek 		char *p;
36191507Sgjelinek 
36201507Sgjelinek 		if ((p = strchr(patchtab.zone_patch_id, '-')) != NULL)
36211507Sgjelinek 			*p++ = '\0';
36221507Sgjelinek 		else
36231507Sgjelinek 			p = "";
36241507Sgjelinek 
36251507Sgjelinek 		if (strcmp(patch_id, patchtab.zone_patch_id) != 0)
36261507Sgjelinek 			continue;
36271507Sgjelinek 
36281507Sgjelinek 		if (strcmp(patch_vers, p) == 0) {
36291507Sgjelinek 			res = 0;
36301507Sgjelinek 			break;
36311507Sgjelinek 		}
36321507Sgjelinek 
36331507Sgjelinek 		(void) strlcpy(return_vers, p, vers_size);
36341507Sgjelinek 		/*
36351507Sgjelinek 		 * Keep checking.  This handles the case where multiple
36361507Sgjelinek 		 * versions of the same patch is installed.
36371507Sgjelinek 		 */
36381507Sgjelinek 		res = 1;
36391507Sgjelinek 	}
36401507Sgjelinek 
36411507Sgjelinek 	(void) zonecfg_endpatchent(handle);
36421507Sgjelinek 	return (res);
36431507Sgjelinek }
36441507Sgjelinek 
36451507Sgjelinek /*
36461507Sgjelinek  * Used in software comparisons to check the patches between the two zone
36471507Sgjelinek  * handles.  The patches have to match or we print a message telling the
36481507Sgjelinek  * user what is out of sync.  The src_cmp flag tells us if the first handle
36491507Sgjelinek  * is the source machine global zone or not.  This is used to enable the
36501507Sgjelinek  * right messages to be printed and also to enable extra version checking
36511507Sgjelinek  * that is not needed for the opposite comparison.
36521507Sgjelinek  */
36531507Sgjelinek static int
36541507Sgjelinek patch_check(char *header, zone_dochandle_t handle1, zone_dochandle_t handle2,
36551507Sgjelinek     boolean_t src_cmp)
36561507Sgjelinek {
36571507Sgjelinek 	int			err;
36581507Sgjelinek 	int			res = Z_OK;
36591507Sgjelinek 	boolean_t		do_header = B_TRUE;
36601507Sgjelinek 	char			other_vers[MAXNAMELEN];
36611507Sgjelinek 	struct zone_patchtab	patchtab;
36621507Sgjelinek 
36631507Sgjelinek 	if (zonecfg_setpatchent(handle1) != Z_OK) {
36641507Sgjelinek 		(void) fprintf(stderr,
36651507Sgjelinek 		    gettext("unable to enumerate patches\n"));
36661507Sgjelinek 		return (Z_ERR);
36671507Sgjelinek 	}
36681507Sgjelinek 
36691507Sgjelinek 	while (zonecfg_getpatchent(handle1, &patchtab) == Z_OK) {
36701507Sgjelinek 		char *patch_vers;
36711507Sgjelinek 
36721507Sgjelinek 		if ((patch_vers = strchr(patchtab.zone_patch_id, '-')) != NULL)
36731507Sgjelinek 			*patch_vers++ = '\0';
36741507Sgjelinek 		else
36751507Sgjelinek 			patch_vers = "";
36761507Sgjelinek 
36771507Sgjelinek 		if ((err = patch_cmp(handle2, patchtab.zone_patch_id,
36781507Sgjelinek 		    patch_vers, other_vers, sizeof (other_vers))) != 0) {
36791507Sgjelinek 			if (do_header && (err < 0 || src_cmp)) {
36801507Sgjelinek 				/* LINTED E_SEC_PRINTF_VAR_FMT */
36811507Sgjelinek 				(void) fprintf(stderr, header);
36821507Sgjelinek 				do_header = B_FALSE;
36831507Sgjelinek 			}
36841507Sgjelinek 			if (err < 0) {
36851507Sgjelinek 				(void) fprintf(stderr,
36861507Sgjelinek 				    (src_cmp == B_TRUE) ?
36871507Sgjelinek 				    gettext("\t%s: not installed\n") :
36881507Sgjelinek 				    gettext("\t%s\n"),
36891507Sgjelinek 				    patchtab.zone_patch_id);
36901507Sgjelinek 				res = Z_ERR;
36911507Sgjelinek 			} else if (src_cmp) {
36921507Sgjelinek 				(void) fprintf(stderr,
36931507Sgjelinek 				    gettext("\t%s: version mismatch\n\t\t(%s) "
36941507Sgjelinek 				    "(%s)\n"), patchtab.zone_patch_id,
36951507Sgjelinek 				    patch_vers, other_vers);
36961507Sgjelinek 				res = Z_ERR;
36971507Sgjelinek 			}
36981507Sgjelinek 		}
36991507Sgjelinek 	}
37001507Sgjelinek 
37011507Sgjelinek 	(void) zonecfg_endpatchent(handle1);
37021507Sgjelinek 
37031507Sgjelinek 	return (res);
37041507Sgjelinek }
37051507Sgjelinek 
37061507Sgjelinek /*
37071507Sgjelinek  * Compare the software on the local global zone and source system global
37081507Sgjelinek  * zone.  Used when we are trying to attach a zone during migration.
37091507Sgjelinek  * l_handle is for the local system and s_handle is for the source system.
37101507Sgjelinek  * These have a snapshot of the appropriate packages and patches in the global
37111507Sgjelinek  * zone for the two machines.
37121507Sgjelinek  * The functions called here will print any messages that are needed to
37131507Sgjelinek  * inform the user about package or patch problems.
37141507Sgjelinek  */
37151507Sgjelinek static int
37161507Sgjelinek sw_cmp(zone_dochandle_t l_handle, zone_dochandle_t s_handle)
37171507Sgjelinek {
37181507Sgjelinek 	char		*hdr;
37191507Sgjelinek 	int		res = Z_OK;
37201507Sgjelinek 
37211507Sgjelinek 	/*
37221507Sgjelinek 	 * Check the source host for pkgs (and versions) that are not on the
37231507Sgjelinek 	 * local host.
37241507Sgjelinek 	 */
37251507Sgjelinek 	hdr = gettext("These packages installed on the source system are "
37261507Sgjelinek 	    "inconsistent with this system:\n");
37271507Sgjelinek 	if (pkg_check(hdr, s_handle, l_handle, B_TRUE) != Z_OK)
37281507Sgjelinek 		res = Z_ERR;
37291507Sgjelinek 
37301507Sgjelinek 	/*
37311507Sgjelinek 	 * Now check the local host for pkgs that were not on the source host.
37321507Sgjelinek 	 * We already handled version mismatches in the loop above.
37331507Sgjelinek 	 */
37341507Sgjelinek 	hdr = gettext("These packages installed on this system were "
37351507Sgjelinek 	    "not installed on the source system:\n");
37361507Sgjelinek 	if (pkg_check(hdr, l_handle, s_handle, B_FALSE) != Z_OK)
37371507Sgjelinek 		res = Z_ERR;
37381507Sgjelinek 
37391507Sgjelinek 	/*
37401507Sgjelinek 	 * Check the source host for patches that are not on the local host.
37411507Sgjelinek 	 */
37421507Sgjelinek 	hdr = gettext("These patches installed on the source system are "
37431507Sgjelinek 	    "inconsistent with this system:\n");
37441507Sgjelinek 	if (patch_check(hdr, s_handle, l_handle, B_TRUE) != Z_OK)
37451507Sgjelinek 		res = Z_ERR;
37461507Sgjelinek 
37471507Sgjelinek 	/*
37481507Sgjelinek 	 * Check the local host for patches that were not on the source host.
37491507Sgjelinek 	 * We already handled version mismatches in the loop above.
37501507Sgjelinek 	 */
37511507Sgjelinek 	hdr = gettext("These patches installed on this system were "
37521507Sgjelinek 	    "not installed on the source system:\n");
37531507Sgjelinek 	if (patch_check(hdr, l_handle, s_handle, B_FALSE) != Z_OK)
37541507Sgjelinek 		res = Z_ERR;
37551507Sgjelinek 
37561507Sgjelinek 	return (res);
37571507Sgjelinek }
37581507Sgjelinek 
37591507Sgjelinek /*
37601507Sgjelinek  * During attach we go through and fix up the /dev entries for the zone
37611507Sgjelinek  * we are attaching.  In order to regenerate /dev with the correct devices,
37621507Sgjelinek  * the old /dev will be removed, the zone readied (which generates a new
37631507Sgjelinek  * /dev) then halted, then we use the info from the manifest to update
37641507Sgjelinek  * the modes, owners, etc. on the new /dev.
37651507Sgjelinek  */
37661507Sgjelinek static int
37671507Sgjelinek dev_fix(zone_dochandle_t handle)
37681507Sgjelinek {
37691507Sgjelinek 	int			res;
37701507Sgjelinek 	int			err;
37711507Sgjelinek 	int			status;
37721507Sgjelinek 	struct zone_devpermtab	devtab;
37731507Sgjelinek 	zone_cmd_arg_t		zarg;
37741507Sgjelinek 	char			devpath[MAXPATHLEN];
37751507Sgjelinek 				/* 6: "exec " and " " */
37761507Sgjelinek 	char			cmdbuf[sizeof (RMCOMMAND) + MAXPATHLEN + 6];
37771507Sgjelinek 
37781507Sgjelinek 	if ((res = zonecfg_get_zonepath(handle, devpath, sizeof (devpath)))
37791507Sgjelinek 	    != Z_OK)
37801507Sgjelinek 		return (res);
37811507Sgjelinek 
37821507Sgjelinek 	if (strlcat(devpath, "/dev", sizeof (devpath)) >= sizeof (devpath))
37831507Sgjelinek 		return (Z_TOO_BIG);
37841507Sgjelinek 
37851507Sgjelinek 	/*
37861507Sgjelinek 	 * "exec" the command so that the returned status is that of
37871507Sgjelinek 	 * RMCOMMAND and not the shell.
37881507Sgjelinek 	 */
37891507Sgjelinek 	(void) snprintf(cmdbuf, sizeof (cmdbuf), "exec " RMCOMMAND " %s",
37901507Sgjelinek 	    devpath);
37911507Sgjelinek 	status = do_subproc(cmdbuf);
37921507Sgjelinek 	if ((err = subproc_status(RMCOMMAND, status)) != Z_OK) {
37931507Sgjelinek 		(void) fprintf(stderr,
37941507Sgjelinek 		    gettext("could not remove existing /dev\n"));
37951507Sgjelinek 		return (Z_ERR);
37961507Sgjelinek 	}
37971507Sgjelinek 
37981507Sgjelinek 	/* In order to ready the zone, it must be in the installed state */
37991507Sgjelinek 	if ((err = zone_set_state(target_zone, ZONE_STATE_INSTALLED)) != Z_OK) {
38001507Sgjelinek 		errno = err;
38011507Sgjelinek 		zperror(gettext("could not reset state"), B_TRUE);
38021507Sgjelinek 		return (Z_ERR);
38031507Sgjelinek 	}
38041507Sgjelinek 
38051507Sgjelinek 	/* We have to ready the zone to regen the dev tree */
38061507Sgjelinek 	zarg.cmd = Z_READY;
38071507Sgjelinek 	if (call_zoneadmd(target_zone, &zarg) != 0) {
38081507Sgjelinek 		zerror(gettext("call to %s failed"), "zoneadmd");
38091507Sgjelinek 		return (Z_ERR);
38101507Sgjelinek 	}
38111507Sgjelinek 
38121507Sgjelinek 	zarg.cmd = Z_HALT;
38131507Sgjelinek 	if (call_zoneadmd(target_zone, &zarg) != 0) {
38141507Sgjelinek 		zerror(gettext("call to %s failed"), "zoneadmd");
38151507Sgjelinek 		return (Z_ERR);
38161507Sgjelinek 	}
38171507Sgjelinek 
38181507Sgjelinek 	if (zonecfg_setdevperment(handle) != Z_OK) {
38191507Sgjelinek 		(void) fprintf(stderr,
38201507Sgjelinek 		    gettext("unable to enumerate device entries\n"));
38211507Sgjelinek 		return (Z_ERR);
38221507Sgjelinek 	}
38231507Sgjelinek 
38241507Sgjelinek 	while (zonecfg_getdevperment(handle, &devtab) == Z_OK) {
38251507Sgjelinek 		int err;
38261507Sgjelinek 
38271507Sgjelinek 		if ((err = zonecfg_devperms_apply(handle,
38281507Sgjelinek 		    devtab.zone_devperm_name, devtab.zone_devperm_uid,
38291507Sgjelinek 		    devtab.zone_devperm_gid, devtab.zone_devperm_mode,
38301507Sgjelinek 		    devtab.zone_devperm_acl)) != Z_OK && err != Z_INVAL)
38311507Sgjelinek 			(void) fprintf(stderr, gettext("error updating device "
38321507Sgjelinek 			    "%s: %s\n"), devtab.zone_devperm_name,
38331507Sgjelinek 			    zonecfg_strerror(err));
38341507Sgjelinek 
38351507Sgjelinek 		free(devtab.zone_devperm_acl);
38361507Sgjelinek 	}
38371507Sgjelinek 
38381507Sgjelinek 	(void) zonecfg_enddevperment(handle);
38391507Sgjelinek 
38401507Sgjelinek 	return (Z_OK);
38411507Sgjelinek }
38421507Sgjelinek 
38431507Sgjelinek static int
38441507Sgjelinek attach_func(int argc, char *argv[])
38451507Sgjelinek {
38461507Sgjelinek 	int lockfd;
38471507Sgjelinek 	int err, arg;
38481507Sgjelinek 	boolean_t force = B_FALSE;
38491507Sgjelinek 	zone_dochandle_t handle;
38501507Sgjelinek 	zone_dochandle_t athandle = NULL;
38511507Sgjelinek 	char zonepath[MAXPATHLEN];
38521507Sgjelinek 
38531507Sgjelinek 	if (zonecfg_in_alt_root()) {
38541507Sgjelinek 		zerror(gettext("cannot attach zone in alternate root"));
38551507Sgjelinek 		return (Z_ERR);
38561507Sgjelinek 	}
38571507Sgjelinek 
38581507Sgjelinek 	optind = 0;
38591507Sgjelinek 	if ((arg = getopt(argc, argv, "?F")) != EOF) {
38601507Sgjelinek 		switch (arg) {
38611507Sgjelinek 		case '?':
38621507Sgjelinek 			sub_usage(SHELP_ATTACH, CMD_ATTACH);
38631507Sgjelinek 			return (optopt == '?' ? Z_OK : Z_USAGE);
38641507Sgjelinek 		case 'F':
38651507Sgjelinek 			force = B_TRUE;
38661507Sgjelinek 			break;
38671507Sgjelinek 		default:
38681507Sgjelinek 			sub_usage(SHELP_ATTACH, CMD_ATTACH);
38691507Sgjelinek 			return (Z_USAGE);
38701507Sgjelinek 		}
38711507Sgjelinek 	}
38721507Sgjelinek 	if (sanity_check(target_zone, CMD_ATTACH, B_FALSE, B_TRUE) != Z_OK)
38731507Sgjelinek 		return (Z_ERR);
38741507Sgjelinek 	if (verify_details(CMD_ATTACH) != Z_OK)
38751507Sgjelinek 		return (Z_ERR);
38761507Sgjelinek 
38771507Sgjelinek 	if ((err = zone_get_zonepath(target_zone, zonepath, sizeof (zonepath)))
38781507Sgjelinek 	    != Z_OK) {
38791507Sgjelinek 		errno = err;
38801507Sgjelinek 		zperror2(target_zone, gettext("could not get zone path"));
38811507Sgjelinek 		return (Z_ERR);
38821507Sgjelinek 	}
38831507Sgjelinek 
38841507Sgjelinek 	if ((handle = zonecfg_init_handle()) == NULL) {
38851507Sgjelinek 		zperror(cmd_to_str(CMD_ATTACH), B_TRUE);
38861507Sgjelinek 		return (Z_ERR);
38871507Sgjelinek 	}
38881507Sgjelinek 
38891507Sgjelinek 	if ((err = zonecfg_get_handle(target_zone, handle)) != Z_OK) {
38901507Sgjelinek 		errno = err;
38911507Sgjelinek 		zperror(cmd_to_str(CMD_ATTACH), B_TRUE);
38921507Sgjelinek 		zonecfg_fini_handle(handle);
38931507Sgjelinek 		return (Z_ERR);
38941507Sgjelinek 	}
38951507Sgjelinek 
38961507Sgjelinek 	if (grab_lock_file(target_zone, &lockfd) != Z_OK) {
38971507Sgjelinek 		zerror(gettext("another %s may have an operation in progress."),
38981507Sgjelinek 		    "zoneadm");
38991507Sgjelinek 		zonecfg_fini_handle(handle);
39001507Sgjelinek 		return (Z_ERR);
39011507Sgjelinek 	}
39021507Sgjelinek 
39031507Sgjelinek 	if (force)
39041507Sgjelinek 		goto forced;
39051507Sgjelinek 
39061507Sgjelinek 	if ((athandle = zonecfg_init_handle()) == NULL) {
39071507Sgjelinek 		zperror(cmd_to_str(CMD_ATTACH), B_TRUE);
39081507Sgjelinek 		goto done;
39091507Sgjelinek 	}
39101507Sgjelinek 
39111507Sgjelinek 	if ((err = zonecfg_get_attach_handle(zonepath, target_zone, B_TRUE,
39121507Sgjelinek 	    athandle)) != Z_OK) {
39131507Sgjelinek 		if (err == Z_NO_ZONE)
39141507Sgjelinek 			zerror(gettext("Not a detached zone"));
39151507Sgjelinek 		else if (err == Z_INVALID_DOCUMENT)
39161507Sgjelinek 			zerror(gettext("Cannot attach to an earlier release "
39171507Sgjelinek 			    "of the operating system"));
39181507Sgjelinek 		else
39191507Sgjelinek 			zperror(cmd_to_str(CMD_ATTACH), B_TRUE);
39201507Sgjelinek 		goto done;
39211507Sgjelinek 	}
39221507Sgjelinek 
39231507Sgjelinek 	/* Get the detach information for the locally defined zone. */
39241507Sgjelinek 	if ((err = zonecfg_get_detach_info(handle, B_FALSE)) != Z_OK) {
39251507Sgjelinek 		errno = err;
39261507Sgjelinek 		zperror(gettext("getting the attach information failed"),
39271507Sgjelinek 		    B_TRUE);
39281507Sgjelinek 		goto done;
39291507Sgjelinek 	}
39301507Sgjelinek 
39311507Sgjelinek 	/* sw_cmp prints error msgs as necessary */
39321507Sgjelinek 	if ((err = sw_cmp(handle, athandle)) != Z_OK)
39331507Sgjelinek 		goto done;
39341507Sgjelinek 
39351507Sgjelinek 	if ((err = dev_fix(athandle)) != Z_OK)
39361507Sgjelinek 		goto done;
39371507Sgjelinek 
39381507Sgjelinek forced:
39391507Sgjelinek 
39401507Sgjelinek 	zonecfg_rm_detached(handle, force);
39411507Sgjelinek 
39421507Sgjelinek 	if ((err = zone_set_state(target_zone, ZONE_STATE_INSTALLED)) != Z_OK) {
39431507Sgjelinek 		errno = err;
39441507Sgjelinek 		zperror(gettext("could not reset state"), B_TRUE);
39451507Sgjelinek 	}
39461507Sgjelinek 
39471507Sgjelinek done:
39481507Sgjelinek 	zonecfg_fini_handle(handle);
39491507Sgjelinek 	release_lock_file(lockfd);
39501507Sgjelinek 	if (athandle != NULL)
39511507Sgjelinek 		zonecfg_fini_handle(athandle);
39521507Sgjelinek 
39531507Sgjelinek 	return ((err == Z_OK) ? Z_OK : Z_ERR);
39541507Sgjelinek }
39551507Sgjelinek 
39561300Sgjelinek /*
39570Sstevel@tonic-gate  * On input, TRUE => yes, FALSE => no.
39580Sstevel@tonic-gate  * On return, TRUE => 1, FALSE => 0, could not ask => -1.
39590Sstevel@tonic-gate  */
39600Sstevel@tonic-gate 
39610Sstevel@tonic-gate static int
39620Sstevel@tonic-gate ask_yesno(boolean_t default_answer, const char *question)
39630Sstevel@tonic-gate {
39640Sstevel@tonic-gate 	char line[64];	/* should be large enough to answer yes or no */
39650Sstevel@tonic-gate 
39660Sstevel@tonic-gate 	if (!isatty(STDIN_FILENO))
39670Sstevel@tonic-gate 		return (-1);
39680Sstevel@tonic-gate 	for (;;) {
39690Sstevel@tonic-gate 		(void) printf("%s (%s)? ", question,
39700Sstevel@tonic-gate 		    default_answer ? "[y]/n" : "y/[n]");
39710Sstevel@tonic-gate 		if (fgets(line, sizeof (line), stdin) == NULL ||
39720Sstevel@tonic-gate 		    line[0] == '\n')
39730Sstevel@tonic-gate 			return (default_answer ? 1 : 0);
39740Sstevel@tonic-gate 		if (tolower(line[0]) == 'y')
39750Sstevel@tonic-gate 			return (1);
39760Sstevel@tonic-gate 		if (tolower(line[0]) == 'n')
39770Sstevel@tonic-gate 			return (0);
39780Sstevel@tonic-gate 	}
39790Sstevel@tonic-gate }
39800Sstevel@tonic-gate 
39810Sstevel@tonic-gate static int
39820Sstevel@tonic-gate uninstall_func(int argc, char *argv[])
39830Sstevel@tonic-gate {
39840Sstevel@tonic-gate 	/* 6: "exec " and " " */
39850Sstevel@tonic-gate 	char cmdbuf[sizeof (RMCOMMAND) + MAXPATHLEN + 6];
39860Sstevel@tonic-gate 	char line[ZONENAME_MAX + 128];	/* Enough for "Are you sure ..." */
39870Sstevel@tonic-gate 	char rootpath[MAXPATHLEN], devpath[MAXPATHLEN];
39880Sstevel@tonic-gate 	boolean_t force = B_FALSE;
39890Sstevel@tonic-gate 	int lockfd, answer;
39900Sstevel@tonic-gate 	int err, arg;
39910Sstevel@tonic-gate 	int status;
39920Sstevel@tonic-gate 
3993766Scarlsonj 	if (zonecfg_in_alt_root()) {
3994766Scarlsonj 		zerror(gettext("cannot uninstall zone in alternate root"));
3995766Scarlsonj 		return (Z_ERR);
3996766Scarlsonj 	}
3997766Scarlsonj 
39980Sstevel@tonic-gate 	optind = 0;
39990Sstevel@tonic-gate 	while ((arg = getopt(argc, argv, "?F")) != EOF) {
40000Sstevel@tonic-gate 		switch (arg) {
40010Sstevel@tonic-gate 		case '?':
40020Sstevel@tonic-gate 			sub_usage(SHELP_UNINSTALL, CMD_UNINSTALL);
40030Sstevel@tonic-gate 			return (optopt == '?' ? Z_OK : Z_USAGE);
40040Sstevel@tonic-gate 		case 'F':
40050Sstevel@tonic-gate 			force = B_TRUE;
40060Sstevel@tonic-gate 			break;
40070Sstevel@tonic-gate 		default:
40080Sstevel@tonic-gate 			sub_usage(SHELP_UNINSTALL, CMD_UNINSTALL);
40090Sstevel@tonic-gate 			return (Z_USAGE);
40100Sstevel@tonic-gate 		}
40110Sstevel@tonic-gate 	}
40120Sstevel@tonic-gate 	if (argc > optind) {
40130Sstevel@tonic-gate 		sub_usage(SHELP_UNINSTALL, CMD_UNINSTALL);
40140Sstevel@tonic-gate 		return (Z_USAGE);
40150Sstevel@tonic-gate 	}
40160Sstevel@tonic-gate 
40170Sstevel@tonic-gate 	if (sanity_check(target_zone, CMD_UNINSTALL, B_FALSE, B_TRUE) != Z_OK)
40180Sstevel@tonic-gate 		return (Z_ERR);
40190Sstevel@tonic-gate 
40200Sstevel@tonic-gate 	if (!force) {
40210Sstevel@tonic-gate 		(void) snprintf(line, sizeof (line),
40220Sstevel@tonic-gate 		    gettext("Are you sure you want to %s zone %s"),
40230Sstevel@tonic-gate 		    cmd_to_str(CMD_UNINSTALL), target_zone);
40240Sstevel@tonic-gate 		if ((answer = ask_yesno(B_FALSE, line)) == 0) {
40250Sstevel@tonic-gate 			return (Z_OK);
40260Sstevel@tonic-gate 		} else if (answer == -1) {
40270Sstevel@tonic-gate 			zerror(gettext("Input not from terminal and -F "
40280Sstevel@tonic-gate 			    "not specified: %s not done."),
40290Sstevel@tonic-gate 			    cmd_to_str(CMD_UNINSTALL));
40300Sstevel@tonic-gate 			return (Z_ERR);
40310Sstevel@tonic-gate 		}
40320Sstevel@tonic-gate 	}
40330Sstevel@tonic-gate 
40340Sstevel@tonic-gate 	if ((err = zone_get_zonepath(target_zone, devpath,
40350Sstevel@tonic-gate 	    sizeof (devpath))) != Z_OK) {
40360Sstevel@tonic-gate 		errno = err;
40370Sstevel@tonic-gate 		zperror2(target_zone, gettext("could not get zone path"));
40380Sstevel@tonic-gate 		return (Z_ERR);
40390Sstevel@tonic-gate 	}
40400Sstevel@tonic-gate 	(void) strlcat(devpath, "/dev", sizeof (devpath));
40410Sstevel@tonic-gate 	if ((err = zone_get_rootpath(target_zone, rootpath,
40420Sstevel@tonic-gate 	    sizeof (rootpath))) != Z_OK) {
40430Sstevel@tonic-gate 		errno = err;
40440Sstevel@tonic-gate 		zperror2(target_zone, gettext("could not get root path"));
40450Sstevel@tonic-gate 		return (Z_ERR);
40460Sstevel@tonic-gate 	}
40470Sstevel@tonic-gate 
40480Sstevel@tonic-gate 	/*
40490Sstevel@tonic-gate 	 * If there seems to be a zoneadmd running for this zone, call it
40500Sstevel@tonic-gate 	 * to tell it that an uninstall is happening; if all goes well it
40510Sstevel@tonic-gate 	 * will then shut itself down.
40520Sstevel@tonic-gate 	 */
40530Sstevel@tonic-gate 	if (ping_zoneadmd(target_zone) == Z_OK) {
40540Sstevel@tonic-gate 		zone_cmd_arg_t zarg;
40550Sstevel@tonic-gate 		zarg.cmd = Z_NOTE_UNINSTALLING;
40560Sstevel@tonic-gate 		/* we don't care too much if this fails... just plow on */
40570Sstevel@tonic-gate 		(void) call_zoneadmd(target_zone, &zarg);
40580Sstevel@tonic-gate 	}
40590Sstevel@tonic-gate 
40600Sstevel@tonic-gate 	if (grab_lock_file(target_zone, &lockfd) != Z_OK) {
40610Sstevel@tonic-gate 		zerror(gettext("another %s may have an operation in progress."),
40621300Sgjelinek 		    "zoneadm");
40630Sstevel@tonic-gate 		return (Z_ERR);
40640Sstevel@tonic-gate 	}
40650Sstevel@tonic-gate 
40660Sstevel@tonic-gate 	/* Don't uninstall the zone if anything is mounted there */
40670Sstevel@tonic-gate 	err = zonecfg_find_mounts(rootpath, NULL, NULL);
40680Sstevel@tonic-gate 	if (err) {
40690Sstevel@tonic-gate 		zerror(gettext("These file-systems are mounted on "
40700Sstevel@tonic-gate 			"subdirectories of %s.\n"), rootpath);
40710Sstevel@tonic-gate 		(void) zonecfg_find_mounts(rootpath, zfm_print, NULL);
40720Sstevel@tonic-gate 		return (Z_ERR);
40730Sstevel@tonic-gate 	}
40740Sstevel@tonic-gate 
40750Sstevel@tonic-gate 	err = zone_set_state(target_zone, ZONE_STATE_INCOMPLETE);
40760Sstevel@tonic-gate 	if (err != Z_OK) {
40770Sstevel@tonic-gate 		errno = err;
40780Sstevel@tonic-gate 		zperror2(target_zone, gettext("could not set state"));
40790Sstevel@tonic-gate 		goto bad;
40800Sstevel@tonic-gate 	}
40810Sstevel@tonic-gate 
40820Sstevel@tonic-gate 	/*
40830Sstevel@tonic-gate 	 * "exec" the command so that the returned status is that of
40840Sstevel@tonic-gate 	 * RMCOMMAND and not the shell.
40850Sstevel@tonic-gate 	 */
40860Sstevel@tonic-gate 	(void) snprintf(cmdbuf, sizeof (cmdbuf), "exec " RMCOMMAND " %s",
40870Sstevel@tonic-gate 	    devpath);
40880Sstevel@tonic-gate 	status = do_subproc(cmdbuf);
40890Sstevel@tonic-gate 	if ((err = subproc_status(RMCOMMAND, status)) != Z_OK)
40900Sstevel@tonic-gate 		goto bad;
40910Sstevel@tonic-gate 	(void) snprintf(cmdbuf, sizeof (cmdbuf), "exec " RMCOMMAND " %s",
40920Sstevel@tonic-gate 	    rootpath);
40930Sstevel@tonic-gate 	status = do_subproc(cmdbuf);
40940Sstevel@tonic-gate 	if ((err = subproc_status(RMCOMMAND, status)) != Z_OK)
40950Sstevel@tonic-gate 		goto bad;
40960Sstevel@tonic-gate 	err = zone_set_state(target_zone, ZONE_STATE_CONFIGURED);
40970Sstevel@tonic-gate 	if (err != Z_OK) {
40980Sstevel@tonic-gate 		errno = err;
40990Sstevel@tonic-gate 		zperror2(target_zone, gettext("could not reset state"));
41000Sstevel@tonic-gate 	}
41010Sstevel@tonic-gate bad:
41020Sstevel@tonic-gate 	release_lock_file(lockfd);
41030Sstevel@tonic-gate 	return (err);
41040Sstevel@tonic-gate }
41050Sstevel@tonic-gate 
4106766Scarlsonj /* ARGSUSED */
4107766Scarlsonj static int
4108766Scarlsonj mount_func(int argc, char *argv[])
4109766Scarlsonj {
4110766Scarlsonj 	zone_cmd_arg_t zarg;
4111766Scarlsonj 
4112766Scarlsonj 	if (argc > 0)
4113766Scarlsonj 		return (Z_USAGE);
4114766Scarlsonj 	if (sanity_check(target_zone, CMD_MOUNT, B_FALSE, B_FALSE) != Z_OK)
4115766Scarlsonj 		return (Z_ERR);
4116766Scarlsonj 	if (verify_details(CMD_MOUNT) != Z_OK)
4117766Scarlsonj 		return (Z_ERR);
4118766Scarlsonj 
4119766Scarlsonj 	zarg.cmd = Z_MOUNT;
4120766Scarlsonj 	if (call_zoneadmd(target_zone, &zarg) != 0) {
4121766Scarlsonj 		zerror(gettext("call to %s failed"), "zoneadmd");
4122766Scarlsonj 		return (Z_ERR);
4123766Scarlsonj 	}
4124766Scarlsonj 	return (Z_OK);
4125766Scarlsonj }
4126766Scarlsonj 
4127766Scarlsonj /* ARGSUSED */
4128766Scarlsonj static int
4129766Scarlsonj unmount_func(int argc, char *argv[])
4130766Scarlsonj {
4131766Scarlsonj 	zone_cmd_arg_t zarg;
4132766Scarlsonj 
4133766Scarlsonj 	if (argc > 0)
4134766Scarlsonj 		return (Z_USAGE);
4135766Scarlsonj 	if (sanity_check(target_zone, CMD_UNMOUNT, B_FALSE, B_FALSE) != Z_OK)
4136766Scarlsonj 		return (Z_ERR);
4137766Scarlsonj 
4138766Scarlsonj 	zarg.cmd = Z_UNMOUNT;
4139766Scarlsonj 	if (call_zoneadmd(target_zone, &zarg) != 0) {
4140766Scarlsonj 		zerror(gettext("call to %s failed"), "zoneadmd");
4141766Scarlsonj 		return (Z_ERR);
4142766Scarlsonj 	}
4143766Scarlsonj 	return (Z_OK);
4144766Scarlsonj }
4145766Scarlsonj 
41460Sstevel@tonic-gate static int
41470Sstevel@tonic-gate help_func(int argc, char *argv[])
41480Sstevel@tonic-gate {
41490Sstevel@tonic-gate 	int arg, cmd_num;
41500Sstevel@tonic-gate 
41510Sstevel@tonic-gate 	if (argc == 0) {
41520Sstevel@tonic-gate 		(void) usage(B_TRUE);
41530Sstevel@tonic-gate 		return (Z_OK);
41540Sstevel@tonic-gate 	}
41550Sstevel@tonic-gate 	optind = 0;
41560Sstevel@tonic-gate 	if ((arg = getopt(argc, argv, "?")) != EOF) {
41570Sstevel@tonic-gate 		switch (arg) {
41580Sstevel@tonic-gate 		case '?':
41590Sstevel@tonic-gate 			sub_usage(SHELP_HELP, CMD_HELP);
41600Sstevel@tonic-gate 			return (optopt == '?' ? Z_OK : Z_USAGE);
41610Sstevel@tonic-gate 		default:
41620Sstevel@tonic-gate 			sub_usage(SHELP_HELP, CMD_HELP);
41630Sstevel@tonic-gate 			return (Z_USAGE);
41640Sstevel@tonic-gate 		}
41650Sstevel@tonic-gate 	}
41660Sstevel@tonic-gate 	while (optind < argc) {
4167988Scarlsonj 		/* Private commands have NULL short_usage; omit them */
4168988Scarlsonj 		if ((cmd_num = cmd_match(argv[optind])) < 0 ||
4169988Scarlsonj 		    cmdtab[cmd_num].short_usage == NULL) {
41700Sstevel@tonic-gate 			sub_usage(SHELP_HELP, CMD_HELP);
41710Sstevel@tonic-gate 			return (Z_USAGE);
41720Sstevel@tonic-gate 		}
41730Sstevel@tonic-gate 		sub_usage(cmdtab[cmd_num].short_usage, cmd_num);
41740Sstevel@tonic-gate 		optind++;
41750Sstevel@tonic-gate 	}
41760Sstevel@tonic-gate 	return (Z_OK);
41770Sstevel@tonic-gate }
41780Sstevel@tonic-gate 
41790Sstevel@tonic-gate /*
41800Sstevel@tonic-gate  * Returns: CMD_MIN thru CMD_MAX on success, -1 on error
41810Sstevel@tonic-gate  */
41820Sstevel@tonic-gate 
41830Sstevel@tonic-gate static int
41840Sstevel@tonic-gate cmd_match(char *cmd)
41850Sstevel@tonic-gate {
41860Sstevel@tonic-gate 	int i;
41870Sstevel@tonic-gate 
41880Sstevel@tonic-gate 	for (i = CMD_MIN; i <= CMD_MAX; i++) {
41890Sstevel@tonic-gate 		/* return only if there is an exact match */
41900Sstevel@tonic-gate 		if (strcmp(cmd, cmdtab[i].cmd_name) == 0)
41910Sstevel@tonic-gate 			return (cmdtab[i].cmd_num);
41920Sstevel@tonic-gate 	}
41930Sstevel@tonic-gate 	return (-1);
41940Sstevel@tonic-gate }
41950Sstevel@tonic-gate 
41960Sstevel@tonic-gate static int
41970Sstevel@tonic-gate parse_and_run(int argc, char *argv[])
41980Sstevel@tonic-gate {
41990Sstevel@tonic-gate 	int i = cmd_match(argv[0]);
42000Sstevel@tonic-gate 
42010Sstevel@tonic-gate 	if (i < 0)
42020Sstevel@tonic-gate 		return (usage(B_FALSE));
42030Sstevel@tonic-gate 	return (cmdtab[i].handler(argc - 1, &(argv[1])));
42040Sstevel@tonic-gate }
42050Sstevel@tonic-gate 
42060Sstevel@tonic-gate static char *
42070Sstevel@tonic-gate get_execbasename(char *execfullname)
42080Sstevel@tonic-gate {
42090Sstevel@tonic-gate 	char *last_slash, *execbasename;
42100Sstevel@tonic-gate 
42110Sstevel@tonic-gate 	/* guard against '/' at end of command invocation */
42120Sstevel@tonic-gate 	for (;;) {
42130Sstevel@tonic-gate 		last_slash = strrchr(execfullname, '/');
42140Sstevel@tonic-gate 		if (last_slash == NULL) {
42150Sstevel@tonic-gate 			execbasename = execfullname;
42160Sstevel@tonic-gate 			break;
42170Sstevel@tonic-gate 		} else {
42180Sstevel@tonic-gate 			execbasename = last_slash + 1;
42190Sstevel@tonic-gate 			if (*execbasename == '\0') {
42200Sstevel@tonic-gate 				*last_slash = '\0';
42210Sstevel@tonic-gate 				continue;
42220Sstevel@tonic-gate 			}
42230Sstevel@tonic-gate 			break;
42240Sstevel@tonic-gate 		}
42250Sstevel@tonic-gate 	}
42260Sstevel@tonic-gate 	return (execbasename);
42270Sstevel@tonic-gate }
42280Sstevel@tonic-gate 
42290Sstevel@tonic-gate int
42300Sstevel@tonic-gate main(int argc, char **argv)
42310Sstevel@tonic-gate {
42320Sstevel@tonic-gate 	int arg;
42330Sstevel@tonic-gate 	zoneid_t zid;
4234766Scarlsonj 	struct stat st;
42350Sstevel@tonic-gate 
42360Sstevel@tonic-gate 	if ((locale = setlocale(LC_ALL, "")) == NULL)
42370Sstevel@tonic-gate 		locale = "C";
42380Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
42390Sstevel@tonic-gate 	setbuf(stdout, NULL);
42400Sstevel@tonic-gate 	(void) sigset(SIGHUP, SIG_IGN);
42410Sstevel@tonic-gate 	execname = get_execbasename(argv[0]);
42420Sstevel@tonic-gate 	target_zone = NULL;
42430Sstevel@tonic-gate 	if (chdir("/") != 0) {
42440Sstevel@tonic-gate 		zerror(gettext("could not change directory to /."));
42450Sstevel@tonic-gate 		exit(Z_ERR);
42460Sstevel@tonic-gate 	}
42470Sstevel@tonic-gate 
4248766Scarlsonj 	while ((arg = getopt(argc, argv, "?z:R:")) != EOF) {
42490Sstevel@tonic-gate 		switch (arg) {
42500Sstevel@tonic-gate 		case '?':
42510Sstevel@tonic-gate 			return (usage(B_TRUE));
42520Sstevel@tonic-gate 		case 'z':
42530Sstevel@tonic-gate 			target_zone = optarg;
42540Sstevel@tonic-gate 			break;
4255766Scarlsonj 		case 'R':	/* private option for admin/install use */
4256766Scarlsonj 			if (*optarg != '/') {
4257766Scarlsonj 				zerror(gettext("root path must be absolute."));
4258766Scarlsonj 				exit(Z_ERR);
4259766Scarlsonj 			}
4260766Scarlsonj 			if (stat(optarg, &st) == -1 || !S_ISDIR(st.st_mode)) {
4261766Scarlsonj 				zerror(
4262766Scarlsonj 				    gettext("root path must be a directory."));
4263766Scarlsonj 				exit(Z_ERR);
4264766Scarlsonj 			}
4265766Scarlsonj 			zonecfg_set_root(optarg);
4266766Scarlsonj 			break;
42670Sstevel@tonic-gate 		default:
42680Sstevel@tonic-gate 			return (usage(B_FALSE));
42690Sstevel@tonic-gate 		}
42700Sstevel@tonic-gate 	}
42710Sstevel@tonic-gate 
42720Sstevel@tonic-gate 	if (optind >= argc)
42730Sstevel@tonic-gate 		return (usage(B_FALSE));
42740Sstevel@tonic-gate 	if (target_zone != NULL && zone_get_id(target_zone, &zid) != 0) {
42750Sstevel@tonic-gate 		errno = Z_NO_ZONE;
42760Sstevel@tonic-gate 		zperror(target_zone, B_TRUE);
42770Sstevel@tonic-gate 		exit(Z_ERR);
42780Sstevel@tonic-gate 	}
42790Sstevel@tonic-gate 	return (parse_and_run(argc - optind, &argv[optind]));
42800Sstevel@tonic-gate }
4281