xref: /onnv-gate/usr/src/cmd/zoneadm/zoneadm.c (revision 222:dd716cb7b870)
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
50Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
70Sstevel@tonic-gate  * with the License.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate  * See the License for the specific language governing permissions
120Sstevel@tonic-gate  * and limitations under the License.
130Sstevel@tonic-gate  *
140Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate  *
200Sstevel@tonic-gate  * CDDL HEADER END
210Sstevel@tonic-gate  */
22*222Scomay 
230Sstevel@tonic-gate /*
240Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
250Sstevel@tonic-gate  * Use is subject to license terms.
260Sstevel@tonic-gate  */
270Sstevel@tonic-gate 
280Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
290Sstevel@tonic-gate 
300Sstevel@tonic-gate /*
310Sstevel@tonic-gate  * zoneadm is a command interpreter for zone administration.  It is all in
320Sstevel@tonic-gate  * C (i.e., no lex/yacc), and all the argument passing is argc/argv based.
330Sstevel@tonic-gate  * main() calls parse_and_run() which calls cmd_match(), then invokes the
340Sstevel@tonic-gate  * appropriate command's handler function.  The rest of the program is the
350Sstevel@tonic-gate  * handler functions and their helper functions.
360Sstevel@tonic-gate  *
370Sstevel@tonic-gate  * Some of the helper functions are used largely to simplify I18N: reducing
380Sstevel@tonic-gate  * the need for translation notes.  This is particularly true of many of
390Sstevel@tonic-gate  * the zerror() calls: doing e.g. zerror(gettext("%s failed"), "foo") rather
400Sstevel@tonic-gate  * than zerror(gettext("foo failed")) with a translation note indicating
410Sstevel@tonic-gate  * that "foo" need not be translated.
420Sstevel@tonic-gate  */
430Sstevel@tonic-gate 
440Sstevel@tonic-gate #include <stdio.h>
450Sstevel@tonic-gate #include <errno.h>
460Sstevel@tonic-gate #include <unistd.h>
470Sstevel@tonic-gate #include <signal.h>
480Sstevel@tonic-gate #include <stdarg.h>
490Sstevel@tonic-gate #include <ctype.h>
500Sstevel@tonic-gate #include <stdlib.h>
510Sstevel@tonic-gate #include <string.h>
520Sstevel@tonic-gate #include <wait.h>
530Sstevel@tonic-gate #include <zone.h>
540Sstevel@tonic-gate #include <priv.h>
550Sstevel@tonic-gate #include <locale.h>
560Sstevel@tonic-gate #include <libintl.h>
570Sstevel@tonic-gate #include <libzonecfg.h>
580Sstevel@tonic-gate #include <bsm/adt.h>
590Sstevel@tonic-gate #include <sys/utsname.h>
600Sstevel@tonic-gate #include <sys/param.h>
610Sstevel@tonic-gate #include <sys/types.h>
620Sstevel@tonic-gate #include <sys/stat.h>
630Sstevel@tonic-gate #include <sys/statvfs.h>
640Sstevel@tonic-gate #include <assert.h>
650Sstevel@tonic-gate #include <sys/sockio.h>
660Sstevel@tonic-gate #include <sys/mntent.h>
670Sstevel@tonic-gate #include <limits.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>
730Sstevel@tonic-gate 
740Sstevel@tonic-gate #include <pool.h>
750Sstevel@tonic-gate #include <sys/pool.h>
760Sstevel@tonic-gate 
770Sstevel@tonic-gate #define	MAXARGS	8
780Sstevel@tonic-gate 
790Sstevel@tonic-gate /* Reflects kernel zone entries */
800Sstevel@tonic-gate typedef struct zone_entry {
810Sstevel@tonic-gate 	zoneid_t	zid;
820Sstevel@tonic-gate 	char		zname[ZONENAME_MAX];
830Sstevel@tonic-gate 	char		*zstate_str;
840Sstevel@tonic-gate 	zone_state_t	zstate_num;
850Sstevel@tonic-gate 	char		zroot[MAXPATHLEN];
860Sstevel@tonic-gate } zone_entry_t;
870Sstevel@tonic-gate 
880Sstevel@tonic-gate static zone_entry_t *zents;
890Sstevel@tonic-gate static size_t nzents;
900Sstevel@tonic-gate 
910Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)		/* should be defined by cc -D */
920Sstevel@tonic-gate #define	TEXT_DOMAIN	"SYS_TEST"	/* Use this only if it wasn't */
930Sstevel@tonic-gate #endif
940Sstevel@tonic-gate 
950Sstevel@tonic-gate #define	Z_ERR	1
960Sstevel@tonic-gate #define	Z_USAGE	2
970Sstevel@tonic-gate 
980Sstevel@tonic-gate /* 0755 is the default directory mode. */
990Sstevel@tonic-gate #define	DEFAULT_DIR_MODE \
1000Sstevel@tonic-gate 	(S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH)
1010Sstevel@tonic-gate 
1020Sstevel@tonic-gate #define	CMD_HELP	0
1030Sstevel@tonic-gate #define	CMD_BOOT	1
1040Sstevel@tonic-gate #define	CMD_HALT	2
1050Sstevel@tonic-gate #define	CMD_READY	3
1060Sstevel@tonic-gate #define	CMD_REBOOT	4
1070Sstevel@tonic-gate #define	CMD_LIST	5
1080Sstevel@tonic-gate #define	CMD_VERIFY	6
1090Sstevel@tonic-gate #define	CMD_INSTALL	7
1100Sstevel@tonic-gate #define	CMD_UNINSTALL	8
1110Sstevel@tonic-gate 
1120Sstevel@tonic-gate #define	CMD_MIN		CMD_HELP
1130Sstevel@tonic-gate #define	CMD_MAX		CMD_UNINSTALL
1140Sstevel@tonic-gate 
1150Sstevel@tonic-gate struct cmd {
1160Sstevel@tonic-gate 	uint_t	cmd_num;				/* command number */
1170Sstevel@tonic-gate 	char	*cmd_name;				/* command name */
1180Sstevel@tonic-gate 	char	*short_usage;				/* short form help */
1190Sstevel@tonic-gate 	int	(*handler)(int argc, char *argv[]);	/* function to call */
1200Sstevel@tonic-gate 
1210Sstevel@tonic-gate };
1220Sstevel@tonic-gate 
1230Sstevel@tonic-gate #define	SHELP_HELP	"help"
1240Sstevel@tonic-gate #define	SHELP_BOOT	"boot [-s]"
1250Sstevel@tonic-gate #define	SHELP_HALT	"halt"
1260Sstevel@tonic-gate #define	SHELP_READY	"ready"
1270Sstevel@tonic-gate #define	SHELP_REBOOT	"reboot"
1280Sstevel@tonic-gate #define	SHELP_LIST	"list [-cipv]"
1290Sstevel@tonic-gate #define	SHELP_VERIFY	"verify"
1300Sstevel@tonic-gate #define	SHELP_INSTALL	"install"
1310Sstevel@tonic-gate #define	SHELP_UNINSTALL	"uninstall [-F]"
1320Sstevel@tonic-gate 
1330Sstevel@tonic-gate static int help_func(int argc, char *argv[]);
1340Sstevel@tonic-gate static int ready_func(int argc, char *argv[]);
1350Sstevel@tonic-gate static int boot_func(int argc, char *argv[]);
1360Sstevel@tonic-gate static int halt_func(int argc, char *argv[]);
1370Sstevel@tonic-gate static int reboot_func(int argc, char *argv[]);
1380Sstevel@tonic-gate static int list_func(int argc, char *argv[]);
1390Sstevel@tonic-gate static int verify_func(int argc, char *argv[]);
1400Sstevel@tonic-gate static int install_func(int argc, char *argv[]);
1410Sstevel@tonic-gate static int uninstall_func(int argc, char *argv[]);
1420Sstevel@tonic-gate static int sanity_check(char *zone, int cmd_num, boolean_t running,
1430Sstevel@tonic-gate     boolean_t unsafe_when_running);
1440Sstevel@tonic-gate static int cmd_match(char *cmd);
1450Sstevel@tonic-gate static int verify_details(int);
1460Sstevel@tonic-gate 
1470Sstevel@tonic-gate static struct cmd cmdtab[] = {
1480Sstevel@tonic-gate 	{ CMD_HELP,		"help",		SHELP_HELP,	help_func },
1490Sstevel@tonic-gate 	{ CMD_BOOT,		"boot",		SHELP_BOOT,	boot_func },
1500Sstevel@tonic-gate 	{ CMD_HALT,		"halt",		SHELP_HALT,	halt_func },
1510Sstevel@tonic-gate 	{ CMD_READY,		"ready",	SHELP_READY,	ready_func },
1520Sstevel@tonic-gate 	{ CMD_REBOOT,		"reboot",	SHELP_REBOOT,	reboot_func },
1530Sstevel@tonic-gate 	{ CMD_LIST,		"list",		SHELP_LIST,	list_func },
1540Sstevel@tonic-gate 	{ CMD_VERIFY,		"verify",	SHELP_VERIFY,	verify_func },
1550Sstevel@tonic-gate 	{ CMD_INSTALL,		"install",	SHELP_INSTALL,	install_func },
1560Sstevel@tonic-gate 	{ CMD_UNINSTALL,	"uninstall",	SHELP_UNINSTALL,
1570Sstevel@tonic-gate 	    uninstall_func }
1580Sstevel@tonic-gate };
1590Sstevel@tonic-gate 
1600Sstevel@tonic-gate /* global variables */
1610Sstevel@tonic-gate 
1620Sstevel@tonic-gate /* set early in main(), never modified thereafter, used all over the place */
1630Sstevel@tonic-gate static char *execname;
1640Sstevel@tonic-gate static char *target_zone;
1650Sstevel@tonic-gate static char *locale;
1660Sstevel@tonic-gate 
1670Sstevel@tonic-gate /* used in do_subproc() and signal handler */
1680Sstevel@tonic-gate static volatile boolean_t child_killed;
1690Sstevel@tonic-gate 
1700Sstevel@tonic-gate static char *
1710Sstevel@tonic-gate cmd_to_str(int cmd_num)
1720Sstevel@tonic-gate {
1730Sstevel@tonic-gate 	assert(cmd_num >= CMD_MIN && cmd_num <= CMD_MAX);
1740Sstevel@tonic-gate 	return (cmdtab[cmd_num].cmd_name);
1750Sstevel@tonic-gate }
1760Sstevel@tonic-gate 
1770Sstevel@tonic-gate /* This is a separate function because of gettext() wrapping. */
1780Sstevel@tonic-gate static char *
1790Sstevel@tonic-gate long_help(int cmd_num)
1800Sstevel@tonic-gate {
181*222Scomay 	assert(cmd_num >= CMD_MIN && cmd_num <= CMD_MAX);
1820Sstevel@tonic-gate 	switch (cmd_num) {
1830Sstevel@tonic-gate 		case CMD_HELP:
1840Sstevel@tonic-gate 			return (gettext("Print usage message."));
1850Sstevel@tonic-gate 		case CMD_BOOT:
1860Sstevel@tonic-gate 			return (gettext("Activates (boots) specified zone.  "
1870Sstevel@tonic-gate 			    "The -s flag can be used\n\tto boot the zone in "
1880Sstevel@tonic-gate 			    "the single-user state."));
1890Sstevel@tonic-gate 		case CMD_HALT:
1900Sstevel@tonic-gate 			return (gettext("Halts specified zone, bypassing "
1910Sstevel@tonic-gate 			    "shutdown scripts and removing runtime\n\t"
1920Sstevel@tonic-gate 			    "resources of the zone."));
1930Sstevel@tonic-gate 		case CMD_READY:
1940Sstevel@tonic-gate 			return (gettext("Prepares a zone for running "
1950Sstevel@tonic-gate 			    "applications but does not start any user\n\t"
1960Sstevel@tonic-gate 			    "processes in the zone."));
1970Sstevel@tonic-gate 		case CMD_REBOOT:
1980Sstevel@tonic-gate 			return (gettext("Restarts the zone (equivalent to a "
1990Sstevel@tonic-gate 			    "halt / boot sequence).\n\tFails if the zone is "
2000Sstevel@tonic-gate 			    "not active."));
2010Sstevel@tonic-gate 		case CMD_LIST:
2020Sstevel@tonic-gate 			return (gettext("Lists the current zones, or a "
2030Sstevel@tonic-gate 			    "specific zone if indicated.  By default,\n\tall "
2040Sstevel@tonic-gate 			    "running zones are listed, though this can be "
2050Sstevel@tonic-gate 			    "expanded to all\n\tinstalled zones with the -i "
2060Sstevel@tonic-gate 			    "option or all configured zones with the\n\t-c "
2070Sstevel@tonic-gate 			    "option.  When used with the general -z <zone> "
2080Sstevel@tonic-gate 			    "option, lists only the\n\tspecified zone, but "
2090Sstevel@tonic-gate 			    "lists it regardless of its state, and the -i "
2100Sstevel@tonic-gate 			    "and -c\n\toptions are disallowed.  The -v option "
2110Sstevel@tonic-gate 			    "can be used to display verbose\n\tinformation: "
2120Sstevel@tonic-gate 			    "zone name, id, current state, root directory and "
2130Sstevel@tonic-gate 			    "options.\n\tThe -p option can be used to request "
2140Sstevel@tonic-gate 			    "machine-parsable output.  The -v\n\tand -p "
2150Sstevel@tonic-gate 			    "options are mutually exclusive.  If neither -v "
2160Sstevel@tonic-gate 			    "nor -p is used,\n\tjust the zone name is "
2170Sstevel@tonic-gate 			    "listed."));
2180Sstevel@tonic-gate 		case CMD_VERIFY:
2190Sstevel@tonic-gate 			return (gettext("Check to make sure the configuration "
2200Sstevel@tonic-gate 			    "can safely be instantiated\n\ton the machine: "
2210Sstevel@tonic-gate 			    "physical network interfaces exist, etc."));
2220Sstevel@tonic-gate 		case CMD_INSTALL:
2230Sstevel@tonic-gate 			return (gettext("Install the configuration on to the "
2240Sstevel@tonic-gate 			    "system."));
2250Sstevel@tonic-gate 		case CMD_UNINSTALL:
2260Sstevel@tonic-gate 			return (gettext("Uninstall the configuration from the "
2270Sstevel@tonic-gate 			    "system.  The -F flag can be used\n\tto force the "
2280Sstevel@tonic-gate 			    "action."));
2290Sstevel@tonic-gate 	}
2300Sstevel@tonic-gate 	/* NOTREACHED */
231*222Scomay 	return (NULL);
2320Sstevel@tonic-gate }
2330Sstevel@tonic-gate 
2340Sstevel@tonic-gate /*
2350Sstevel@tonic-gate  * Called with explicit B_TRUE when help is explicitly requested, B_FALSE for
2360Sstevel@tonic-gate  * unexpected errors.
2370Sstevel@tonic-gate  */
2380Sstevel@tonic-gate 
2390Sstevel@tonic-gate static int
2400Sstevel@tonic-gate usage(boolean_t explicit)
2410Sstevel@tonic-gate {
2420Sstevel@tonic-gate 	int i;
2430Sstevel@tonic-gate 	FILE *fd = explicit ? stdout : stderr;
2440Sstevel@tonic-gate 
2450Sstevel@tonic-gate 	(void) fprintf(fd, "%s:\t%s help\n", gettext("usage"), execname);
2460Sstevel@tonic-gate 	(void) fprintf(fd, "\t%s [-z <zone>] list\n", execname);
2470Sstevel@tonic-gate 	(void) fprintf(fd, "\t%s -z <zone> <%s>\n", execname,
2480Sstevel@tonic-gate 	    gettext("subcommand"));
2490Sstevel@tonic-gate 	(void) fprintf(fd, "\n%s:\n\n", gettext("Subcommands"));
2500Sstevel@tonic-gate 	for (i = CMD_MIN; i <= CMD_MAX; i++) {
2510Sstevel@tonic-gate 		(void) fprintf(fd, "%s\n", cmdtab[i].short_usage);
2520Sstevel@tonic-gate 		if (explicit)
2530Sstevel@tonic-gate 			(void) fprintf(fd, "\t%s\n\n", long_help(i));
2540Sstevel@tonic-gate 	}
2550Sstevel@tonic-gate 	if (!explicit)
2560Sstevel@tonic-gate 		(void) fputs("\n", fd);
2570Sstevel@tonic-gate 	return (Z_USAGE);
2580Sstevel@tonic-gate }
2590Sstevel@tonic-gate 
2600Sstevel@tonic-gate static void
2610Sstevel@tonic-gate sub_usage(char *short_usage, int cmd_num)
2620Sstevel@tonic-gate {
2630Sstevel@tonic-gate 	(void) fprintf(stderr, "%s:\t%s\n", gettext("usage"), short_usage);
2640Sstevel@tonic-gate 	(void) fprintf(stderr, "\t%s\n", long_help(cmd_num));
2650Sstevel@tonic-gate }
2660Sstevel@tonic-gate 
2670Sstevel@tonic-gate /*
2680Sstevel@tonic-gate  * zperror() is like perror(3c) except that this also prints the executable
2690Sstevel@tonic-gate  * name at the start of the message, and takes a boolean indicating whether
2700Sstevel@tonic-gate  * to call libc'c strerror() or that from libzonecfg.
2710Sstevel@tonic-gate  */
2720Sstevel@tonic-gate 
2730Sstevel@tonic-gate static void
2740Sstevel@tonic-gate zperror(const char *str, boolean_t zonecfg_error)
2750Sstevel@tonic-gate {
2760Sstevel@tonic-gate 	(void) fprintf(stderr, "%s: %s: %s\n", execname, str,
2770Sstevel@tonic-gate 	    zonecfg_error ? zonecfg_strerror(errno) : strerror(errno));
2780Sstevel@tonic-gate }
2790Sstevel@tonic-gate 
2800Sstevel@tonic-gate /*
2810Sstevel@tonic-gate  * zperror2() is very similar to zperror() above, except it also prints a
2820Sstevel@tonic-gate  * supplied zone name after the executable.
2830Sstevel@tonic-gate  *
2840Sstevel@tonic-gate  * All current consumers of this function want libzonecfg's strerror() rather
2850Sstevel@tonic-gate  * than libc's; if this ever changes, this function can be made more generic
2860Sstevel@tonic-gate  * like zperror() above.
2870Sstevel@tonic-gate  */
2880Sstevel@tonic-gate 
2890Sstevel@tonic-gate static void
2900Sstevel@tonic-gate zperror2(const char *zone, const char *str)
2910Sstevel@tonic-gate {
2920Sstevel@tonic-gate 	(void) fprintf(stderr, "%s: %s: %s: %s\n", execname, zone, str,
2930Sstevel@tonic-gate 	    zonecfg_strerror(errno));
2940Sstevel@tonic-gate }
2950Sstevel@tonic-gate 
2960Sstevel@tonic-gate /* PRINTFLIKE1 */
2970Sstevel@tonic-gate static void
2980Sstevel@tonic-gate zerror(const char *fmt, ...)
2990Sstevel@tonic-gate {
3000Sstevel@tonic-gate 	va_list alist;
3010Sstevel@tonic-gate 
3020Sstevel@tonic-gate 	va_start(alist, fmt);
3030Sstevel@tonic-gate 	(void) fprintf(stderr, "%s: ", execname);
3040Sstevel@tonic-gate 	if (target_zone != NULL)
3050Sstevel@tonic-gate 		(void) fprintf(stderr, "zone '%s': ", target_zone);
3060Sstevel@tonic-gate 	(void) vfprintf(stderr, fmt, alist);
3070Sstevel@tonic-gate 	(void) fprintf(stderr, "\n");
3080Sstevel@tonic-gate 	va_end(alist);
3090Sstevel@tonic-gate }
3100Sstevel@tonic-gate 
3110Sstevel@tonic-gate static void *
3120Sstevel@tonic-gate safe_calloc(size_t nelem, size_t elsize)
3130Sstevel@tonic-gate {
3140Sstevel@tonic-gate 	void *r = calloc(nelem, elsize);
3150Sstevel@tonic-gate 
3160Sstevel@tonic-gate 	if (r == NULL) {
3170Sstevel@tonic-gate 		zerror(gettext("failed to allocate %lu bytes: %s"),
3180Sstevel@tonic-gate 		    (ulong_t)nelem * elsize, strerror(errno));
3190Sstevel@tonic-gate 		exit(Z_ERR);
3200Sstevel@tonic-gate 	}
3210Sstevel@tonic-gate 	return (r);
3220Sstevel@tonic-gate }
3230Sstevel@tonic-gate 
3240Sstevel@tonic-gate static void
3250Sstevel@tonic-gate zone_print(zone_entry_t *zent, boolean_t verbose, boolean_t parsable)
3260Sstevel@tonic-gate {
3270Sstevel@tonic-gate 	static boolean_t firsttime = B_TRUE;
3280Sstevel@tonic-gate 
3290Sstevel@tonic-gate 	assert(!(verbose && parsable));
3300Sstevel@tonic-gate 	if (firsttime && verbose) {
3310Sstevel@tonic-gate 		firsttime = B_FALSE;
3320Sstevel@tonic-gate 		(void) printf("%*s %-16s %-14s %-30s\n", ZONEID_WIDTH, "ID",
3330Sstevel@tonic-gate 		    "NAME", "STATUS", "PATH");
3340Sstevel@tonic-gate 	}
3350Sstevel@tonic-gate 	if (!verbose) {
3360Sstevel@tonic-gate 		if (!parsable) {
3370Sstevel@tonic-gate 			(void) printf("%s\n", zent->zname);
3380Sstevel@tonic-gate 			return;
3390Sstevel@tonic-gate 		}
3400Sstevel@tonic-gate 		if (zent->zid == ZONE_ID_UNDEFINED)
3410Sstevel@tonic-gate 			(void) printf("-");
3420Sstevel@tonic-gate 		else
3430Sstevel@tonic-gate 			(void) printf("%lu", zent->zid);
3440Sstevel@tonic-gate 		(void) printf(":%s:%s:%s\n", zent->zname, zent->zstate_str,
3450Sstevel@tonic-gate 		    zent->zroot);
3460Sstevel@tonic-gate 		return;
3470Sstevel@tonic-gate 	}
3480Sstevel@tonic-gate 	if (zent->zstate_str != NULL) {
3490Sstevel@tonic-gate 		if (zent->zid == ZONE_ID_UNDEFINED)
3500Sstevel@tonic-gate 			(void) printf("%*s", ZONEID_WIDTH, "-");
3510Sstevel@tonic-gate 		else
3520Sstevel@tonic-gate 			(void) printf("%*lu", ZONEID_WIDTH, zent->zid);
3530Sstevel@tonic-gate 		(void) printf(" %-16s %-14s %-30s\n", zent->zname,
3540Sstevel@tonic-gate 		    zent->zstate_str, zent->zroot);
3550Sstevel@tonic-gate 	}
3560Sstevel@tonic-gate }
3570Sstevel@tonic-gate 
3580Sstevel@tonic-gate static int
3590Sstevel@tonic-gate lookup_zone_info(char *zone_name, zone_entry_t *zent)
3600Sstevel@tonic-gate {
3610Sstevel@tonic-gate 	char root[MAXPATHLEN];
3620Sstevel@tonic-gate 	int err;
3630Sstevel@tonic-gate 
3640Sstevel@tonic-gate 	(void) strlcpy(zent->zname, zone_name, sizeof (zent->zname));
3650Sstevel@tonic-gate 	(void) strlcpy(zent->zroot, "???", sizeof (zent->zroot));
3660Sstevel@tonic-gate 	zent->zstate_str = "???";
3670Sstevel@tonic-gate 
3680Sstevel@tonic-gate 	if ((zent->zid = getzoneidbyname(zone_name)) == -1)
3690Sstevel@tonic-gate 		zent->zid = ZONE_ID_UNDEFINED;
3700Sstevel@tonic-gate 
3710Sstevel@tonic-gate 	if ((err = zone_get_zonepath(zent->zname, root, sizeof (root))) !=
3720Sstevel@tonic-gate 	    Z_OK) {
3730Sstevel@tonic-gate 		errno = err;
3740Sstevel@tonic-gate 		zperror2(zent->zname, gettext("could not get zone path"));
3750Sstevel@tonic-gate 		return (Z_ERR);
3760Sstevel@tonic-gate 	}
3770Sstevel@tonic-gate 	(void) strlcpy(zent->zroot, root, sizeof (zent->zroot));
3780Sstevel@tonic-gate 
3790Sstevel@tonic-gate 	if ((err = zone_get_state(zent->zname, &zent->zstate_num)) != Z_OK) {
3800Sstevel@tonic-gate 		errno = err;
3810Sstevel@tonic-gate 		zperror2(zent->zname, gettext("could not get state"));
3820Sstevel@tonic-gate 		return (Z_ERR);
3830Sstevel@tonic-gate 	}
3840Sstevel@tonic-gate 	zent->zstate_str = zone_state_str(zent->zstate_num);
3850Sstevel@tonic-gate 
3860Sstevel@tonic-gate 	return (Z_OK);
3870Sstevel@tonic-gate }
3880Sstevel@tonic-gate 
3890Sstevel@tonic-gate /*
3900Sstevel@tonic-gate  * fetch_zents() calls zone_list(2) to find out how many zones are running
3910Sstevel@tonic-gate  * (which is stored in the global nzents), then calls zone_list(2) again
3920Sstevel@tonic-gate  * to fetch the list of running zones (stored in the global zents).  This
3930Sstevel@tonic-gate  * function may be called multiple times, so if zents is already set, we
3940Sstevel@tonic-gate  * return immediately to save work.
3950Sstevel@tonic-gate  */
3960Sstevel@tonic-gate 
3970Sstevel@tonic-gate static int
3980Sstevel@tonic-gate fetch_zents()
3990Sstevel@tonic-gate {
4000Sstevel@tonic-gate 	zoneid_t *zids = NULL;
4010Sstevel@tonic-gate 	uint_t nzents_saved;
4020Sstevel@tonic-gate 	int i;
4030Sstevel@tonic-gate 
4040Sstevel@tonic-gate 	if (nzents > 0)
4050Sstevel@tonic-gate 		return (Z_OK);
4060Sstevel@tonic-gate 
4070Sstevel@tonic-gate 	if (zone_list(NULL, &nzents) != 0) {
4080Sstevel@tonic-gate 		zperror(gettext("failed to get zoneid list"), B_FALSE);
4090Sstevel@tonic-gate 		return (Z_ERR);
4100Sstevel@tonic-gate 	}
4110Sstevel@tonic-gate 
4120Sstevel@tonic-gate again:
4130Sstevel@tonic-gate 	if (nzents == 0)
4140Sstevel@tonic-gate 		return (Z_OK);
4150Sstevel@tonic-gate 
4160Sstevel@tonic-gate 	zids = safe_calloc(nzents, sizeof (zoneid_t));
4170Sstevel@tonic-gate 	nzents_saved = nzents;
4180Sstevel@tonic-gate 
4190Sstevel@tonic-gate 	if (zone_list(zids, &nzents) != 0) {
4200Sstevel@tonic-gate 		zperror(gettext("failed to get zone list"), B_FALSE);
4210Sstevel@tonic-gate 		free(zids);
4220Sstevel@tonic-gate 		return (Z_ERR);
4230Sstevel@tonic-gate 	}
4240Sstevel@tonic-gate 	if (nzents != nzents_saved) {
4250Sstevel@tonic-gate 		/* list changed, try again */
4260Sstevel@tonic-gate 		free(zids);
4270Sstevel@tonic-gate 		goto again;
4280Sstevel@tonic-gate 	}
4290Sstevel@tonic-gate 
4300Sstevel@tonic-gate 	zents = safe_calloc(nzents, sizeof (zone_entry_t));
4310Sstevel@tonic-gate 
4320Sstevel@tonic-gate 	for (i = 0; i < nzents; i++) {
4330Sstevel@tonic-gate 		char name[ZONENAME_MAX];
4340Sstevel@tonic-gate 
4350Sstevel@tonic-gate 		if (getzonenamebyid(zids[i], name, sizeof (name)) < 0)
4360Sstevel@tonic-gate 			zperror(gettext("failed to get zone name"), B_FALSE);
4370Sstevel@tonic-gate 		else if (lookup_zone_info(name, &zents[i]) != Z_OK)
4380Sstevel@tonic-gate 			zerror(gettext("failed to get zone list"));
4390Sstevel@tonic-gate 	}
4400Sstevel@tonic-gate 
4410Sstevel@tonic-gate 	free(zids);
4420Sstevel@tonic-gate 	return (Z_OK);
4430Sstevel@tonic-gate }
4440Sstevel@tonic-gate 
4450Sstevel@tonic-gate static void
4460Sstevel@tonic-gate zone_print_list(zone_state_t min_state, boolean_t verbose, boolean_t parsable)
4470Sstevel@tonic-gate {
4480Sstevel@tonic-gate 	int i;
4490Sstevel@tonic-gate 	zone_entry_t zent;
4500Sstevel@tonic-gate 	FILE *cookie;
4510Sstevel@tonic-gate 	char *name;
4520Sstevel@tonic-gate 
4530Sstevel@tonic-gate 	/*
4540Sstevel@tonic-gate 	 * First get the list of running zones from the kernel and print them.
4550Sstevel@tonic-gate 	 * If that is all we need, then return.
4560Sstevel@tonic-gate 	 */
4570Sstevel@tonic-gate 	if (fetch_zents() != Z_OK) {
4580Sstevel@tonic-gate 		/*
4590Sstevel@tonic-gate 		 * No need for error messages; fetch_zents() has already taken
4600Sstevel@tonic-gate 		 * care of this.
4610Sstevel@tonic-gate 		 */
4620Sstevel@tonic-gate 		return;
4630Sstevel@tonic-gate 	}
4640Sstevel@tonic-gate 	for (i = 0; i < nzents; i++) {
4650Sstevel@tonic-gate 		if (!verbose && !parsable) {
4660Sstevel@tonic-gate 			zone_print(&zents[i], verbose, parsable);
4670Sstevel@tonic-gate 			continue;
4680Sstevel@tonic-gate 		}
4690Sstevel@tonic-gate 		zone_print(&zents[i], verbose, parsable);
4700Sstevel@tonic-gate 	}
4710Sstevel@tonic-gate 	if (min_state >= ZONE_STATE_RUNNING)
4720Sstevel@tonic-gate 		return;
4730Sstevel@tonic-gate 	/*
4740Sstevel@tonic-gate 	 * Next, get the full list of zones from the configuration, skipping
4750Sstevel@tonic-gate 	 * any we have already printed.
4760Sstevel@tonic-gate 	 */
4770Sstevel@tonic-gate 	cookie = setzoneent();
4780Sstevel@tonic-gate 	while ((name = getzoneent(cookie)) != NULL) {
4790Sstevel@tonic-gate 		for (i = 0; i < nzents; i++) {
4800Sstevel@tonic-gate 			if (strcmp(zents[i].zname, name) == 0)
4810Sstevel@tonic-gate 				break;
4820Sstevel@tonic-gate 		}
4830Sstevel@tonic-gate 		if (i < nzents) {
4840Sstevel@tonic-gate 			free(name);
4850Sstevel@tonic-gate 			continue;
4860Sstevel@tonic-gate 		}
4870Sstevel@tonic-gate 		if (lookup_zone_info(name, &zent) != Z_OK) {
4880Sstevel@tonic-gate 			free(name);
4890Sstevel@tonic-gate 			continue;
4900Sstevel@tonic-gate 		}
4910Sstevel@tonic-gate 		free(name);
4920Sstevel@tonic-gate 		if (zent.zstate_num >= min_state)
4930Sstevel@tonic-gate 			zone_print(&zent, verbose, parsable);
4940Sstevel@tonic-gate 	}
4950Sstevel@tonic-gate 	endzoneent(cookie);
4960Sstevel@tonic-gate }
4970Sstevel@tonic-gate 
4980Sstevel@tonic-gate static zone_entry_t *
4990Sstevel@tonic-gate lookup_running_zone(char *str)
5000Sstevel@tonic-gate {
5010Sstevel@tonic-gate 	zoneid_t zoneid;
5020Sstevel@tonic-gate 	char *cp;
5030Sstevel@tonic-gate 	int i;
5040Sstevel@tonic-gate 
5050Sstevel@tonic-gate 	if (fetch_zents() != Z_OK)
5060Sstevel@tonic-gate 		return (NULL);
5070Sstevel@tonic-gate 
5080Sstevel@tonic-gate 	for (i = 0; i < nzents; i++) {
5090Sstevel@tonic-gate 		if (strcmp(str, zents[i].zname) == 0)
5100Sstevel@tonic-gate 			return (&zents[i]);
5110Sstevel@tonic-gate 	}
5120Sstevel@tonic-gate 	errno = 0;
5130Sstevel@tonic-gate 	zoneid = strtol(str, &cp, 0);
5140Sstevel@tonic-gate 	if (zoneid < MIN_ZONEID || zoneid > MAX_ZONEID ||
5150Sstevel@tonic-gate 	    errno != 0 || *cp != '\0')
5160Sstevel@tonic-gate 		return (NULL);
5170Sstevel@tonic-gate 	for (i = 0; i < nzents; i++) {
5180Sstevel@tonic-gate 		if (zoneid == zents[i].zid)
5190Sstevel@tonic-gate 			return (&zents[i]);
5200Sstevel@tonic-gate 	}
5210Sstevel@tonic-gate 	return (NULL);
5220Sstevel@tonic-gate }
5230Sstevel@tonic-gate 
5240Sstevel@tonic-gate /*
5250Sstevel@tonic-gate  * Check a bit in a mode_t: if on is B_TRUE, that bit should be on; if
5260Sstevel@tonic-gate  * B_FALSE, it should be off.  Return B_TRUE if the mode is bad (incorrect).
5270Sstevel@tonic-gate  */
5280Sstevel@tonic-gate static boolean_t
5290Sstevel@tonic-gate bad_mode_bit(mode_t mode, mode_t bit, boolean_t on, char *file)
5300Sstevel@tonic-gate {
5310Sstevel@tonic-gate 	char *str;
5320Sstevel@tonic-gate 
5330Sstevel@tonic-gate 	assert(bit == S_IRUSR || bit == S_IWUSR || bit == S_IXUSR ||
5340Sstevel@tonic-gate 	    bit == S_IRGRP || bit == S_IWGRP || bit == S_IXGRP ||
5350Sstevel@tonic-gate 	    bit == S_IROTH || bit == S_IWOTH || bit == S_IXOTH);
5360Sstevel@tonic-gate 	/*
5370Sstevel@tonic-gate 	 * TRANSLATION_NOTE
5380Sstevel@tonic-gate 	 * The strings below will be used as part of a larger message,
5390Sstevel@tonic-gate 	 * either:
5400Sstevel@tonic-gate 	 * (file name) must be (owner|group|world) (read|writ|execut)able
5410Sstevel@tonic-gate 	 * or
5420Sstevel@tonic-gate 	 * (file name) must not be (owner|group|world) (read|writ|execut)able
5430Sstevel@tonic-gate 	 */
5440Sstevel@tonic-gate 	switch (bit) {
5450Sstevel@tonic-gate 	case S_IRUSR:
5460Sstevel@tonic-gate 		str = gettext("owner readable");
5470Sstevel@tonic-gate 		break;
5480Sstevel@tonic-gate 	case S_IWUSR:
5490Sstevel@tonic-gate 		str = gettext("owner writable");
5500Sstevel@tonic-gate 		break;
5510Sstevel@tonic-gate 	case S_IXUSR:
5520Sstevel@tonic-gate 		str = gettext("owner executable");
5530Sstevel@tonic-gate 		break;
5540Sstevel@tonic-gate 	case S_IRGRP:
5550Sstevel@tonic-gate 		str = gettext("group readable");
5560Sstevel@tonic-gate 		break;
5570Sstevel@tonic-gate 	case S_IWGRP:
5580Sstevel@tonic-gate 		str = gettext("group writable");
5590Sstevel@tonic-gate 		break;
5600Sstevel@tonic-gate 	case S_IXGRP:
5610Sstevel@tonic-gate 		str = gettext("group executable");
5620Sstevel@tonic-gate 		break;
5630Sstevel@tonic-gate 	case S_IROTH:
5640Sstevel@tonic-gate 		str = gettext("world readable");
5650Sstevel@tonic-gate 		break;
5660Sstevel@tonic-gate 	case S_IWOTH:
5670Sstevel@tonic-gate 		str = gettext("world writable");
5680Sstevel@tonic-gate 		break;
5690Sstevel@tonic-gate 	case S_IXOTH:
5700Sstevel@tonic-gate 		str = gettext("world executable");
5710Sstevel@tonic-gate 		break;
5720Sstevel@tonic-gate 	}
5730Sstevel@tonic-gate 	if ((mode & bit) == (on ? 0 : bit)) {
5740Sstevel@tonic-gate 		/*
5750Sstevel@tonic-gate 		 * TRANSLATION_NOTE
5760Sstevel@tonic-gate 		 * The first parameter below is a file name; the second
5770Sstevel@tonic-gate 		 * is one of the "(owner|group|world) (read|writ|execut)able"
5780Sstevel@tonic-gate 		 * strings from above.
5790Sstevel@tonic-gate 		 */
5800Sstevel@tonic-gate 		/*
5810Sstevel@tonic-gate 		 * The code below could be simplified but not in a way
5820Sstevel@tonic-gate 		 * that would easily translate to non-English locales.
5830Sstevel@tonic-gate 		 */
5840Sstevel@tonic-gate 		if (on) {
5850Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("%s must be %s.\n"),
5860Sstevel@tonic-gate 			    file, str);
5870Sstevel@tonic-gate 		} else {
5880Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("%s must not be %s.\n"),
5890Sstevel@tonic-gate 			    file, str);
5900Sstevel@tonic-gate 		}
5910Sstevel@tonic-gate 		return (B_TRUE);
5920Sstevel@tonic-gate 	}
5930Sstevel@tonic-gate 	return (B_FALSE);
5940Sstevel@tonic-gate }
5950Sstevel@tonic-gate 
5960Sstevel@tonic-gate /*
5970Sstevel@tonic-gate  * We want to make sure that no zone has its zone path as a child node
5980Sstevel@tonic-gate  * (in the directory sense) of any other.  We do that by comparing this
5990Sstevel@tonic-gate  * zone's path to the path of all other (non-global) zones.  The comparison
6000Sstevel@tonic-gate  * in each case is simple: add '/' to the end of the path, then do a
6010Sstevel@tonic-gate  * strncmp() of the two paths, using the length of the shorter one.
6020Sstevel@tonic-gate  */
6030Sstevel@tonic-gate 
6040Sstevel@tonic-gate static int
6050Sstevel@tonic-gate crosscheck_zonepaths(char *path)
6060Sstevel@tonic-gate {
6070Sstevel@tonic-gate 	char rpath[MAXPATHLEN];		/* resolved path */
6080Sstevel@tonic-gate 	char path_copy[MAXPATHLEN];	/* copy of original path */
6090Sstevel@tonic-gate 	char rpath_copy[MAXPATHLEN];	/* copy of original rpath */
6100Sstevel@tonic-gate 	struct zoneent *ze;
6110Sstevel@tonic-gate 	int res, err;
6120Sstevel@tonic-gate 	FILE *cookie;
6130Sstevel@tonic-gate 
6140Sstevel@tonic-gate 	cookie = setzoneent();
6150Sstevel@tonic-gate 	while ((ze = getzoneent_private(cookie)) != NULL) {
6160Sstevel@tonic-gate 		/* Skip zones which are not installed. */
6170Sstevel@tonic-gate 		if (ze->zone_state < ZONE_STATE_INSTALLED) {
6180Sstevel@tonic-gate 			free(ze);
6190Sstevel@tonic-gate 			continue;
6200Sstevel@tonic-gate 		}
6210Sstevel@tonic-gate 		/* Skip the global zone and the current target zone. */
6220Sstevel@tonic-gate 		if (strcmp(ze->zone_name, GLOBAL_ZONENAME) == 0 ||
6230Sstevel@tonic-gate 		    strcmp(ze->zone_name, target_zone) == 0) {
6240Sstevel@tonic-gate 			free(ze);
6250Sstevel@tonic-gate 			continue;
6260Sstevel@tonic-gate 		}
6270Sstevel@tonic-gate 		if (strlen(ze->zone_path) == 0) {
6280Sstevel@tonic-gate 			/* old index file without path, fall back */
6290Sstevel@tonic-gate 			if ((err = zone_get_zonepath(ze->zone_name,
6300Sstevel@tonic-gate 			    ze->zone_path, sizeof (ze->zone_path))) != Z_OK) {
6310Sstevel@tonic-gate 				errno = err;
6320Sstevel@tonic-gate 				zperror2(ze->zone_name,
6330Sstevel@tonic-gate 				    gettext("could not get zone path"));
6340Sstevel@tonic-gate 				free(ze);
6350Sstevel@tonic-gate 				continue;
6360Sstevel@tonic-gate 			}
6370Sstevel@tonic-gate 		}
6380Sstevel@tonic-gate 		res = resolvepath(ze->zone_path, rpath, sizeof (rpath));
6390Sstevel@tonic-gate 		if (res == -1) {
6400Sstevel@tonic-gate 			if (errno != ENOENT) {
6410Sstevel@tonic-gate 				zperror(ze->zone_path, B_FALSE);
6420Sstevel@tonic-gate 				free(ze);
6430Sstevel@tonic-gate 				return (Z_ERR);
6440Sstevel@tonic-gate 			}
6450Sstevel@tonic-gate 			(void) printf(gettext("WARNING: zone %s is installed, "
6460Sstevel@tonic-gate 			    "but its %s %s does not exist.\n"), ze->zone_name,
6470Sstevel@tonic-gate 			    "zonepath", ze->zone_path);
6480Sstevel@tonic-gate 			free(ze);
6490Sstevel@tonic-gate 			continue;
6500Sstevel@tonic-gate 		}
6510Sstevel@tonic-gate 		rpath[res] = '\0';
6520Sstevel@tonic-gate 		(void) snprintf(path_copy, sizeof (path_copy), "%s/", path);
6530Sstevel@tonic-gate 		(void) snprintf(rpath_copy, sizeof (rpath_copy), "%s/", rpath);
6540Sstevel@tonic-gate 		if (strncmp(path_copy, rpath_copy,
6550Sstevel@tonic-gate 		    min(strlen(path_copy), strlen(rpath_copy))) == 0) {
6560Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("%s zonepath (%s) and "
6570Sstevel@tonic-gate 			    "%s zonepath (%s) overlap.\n"),
6580Sstevel@tonic-gate 			    target_zone, path, ze->zone_name, rpath);
6590Sstevel@tonic-gate 			free(ze);
6600Sstevel@tonic-gate 			return (Z_ERR);
6610Sstevel@tonic-gate 		}
6620Sstevel@tonic-gate 		free(ze);
6630Sstevel@tonic-gate 	}
6640Sstevel@tonic-gate 	endzoneent(cookie);
6650Sstevel@tonic-gate 	return (Z_OK);
6660Sstevel@tonic-gate }
6670Sstevel@tonic-gate 
6680Sstevel@tonic-gate static int
6690Sstevel@tonic-gate validate_zonepath(char *path, int cmd_num)
6700Sstevel@tonic-gate {
6710Sstevel@tonic-gate 	int res;			/* result of last library/system call */
6720Sstevel@tonic-gate 	boolean_t err = B_FALSE;	/* have we run into an error? */
6730Sstevel@tonic-gate 	struct stat stbuf;
6740Sstevel@tonic-gate 	struct statvfs vfsbuf;
6750Sstevel@tonic-gate 	char rpath[MAXPATHLEN];		/* resolved path */
6760Sstevel@tonic-gate 	char ppath[MAXPATHLEN];		/* parent path */
6770Sstevel@tonic-gate 	char rppath[MAXPATHLEN];	/* resolved parent path */
6780Sstevel@tonic-gate 	char rootpath[MAXPATHLEN];	/* root path */
6790Sstevel@tonic-gate 	zone_state_t state;
6800Sstevel@tonic-gate 
6810Sstevel@tonic-gate 	if (path[0] != '/') {
6820Sstevel@tonic-gate 		(void) fprintf(stderr,
6830Sstevel@tonic-gate 		    gettext("%s is not an absolute path.\n"), path);
6840Sstevel@tonic-gate 		return (Z_ERR);
6850Sstevel@tonic-gate 	}
6860Sstevel@tonic-gate 	if ((res = resolvepath(path, rpath, sizeof (rpath))) == -1) {
6870Sstevel@tonic-gate 		if ((errno != ENOENT) ||
6880Sstevel@tonic-gate 		    (cmd_num != CMD_VERIFY && cmd_num != CMD_INSTALL)) {
6890Sstevel@tonic-gate 			zperror(path, B_FALSE);
6900Sstevel@tonic-gate 			return (Z_ERR);
6910Sstevel@tonic-gate 		}
6920Sstevel@tonic-gate 		if (cmd_num == CMD_VERIFY) {
6930Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("WARNING: %s does not "
6940Sstevel@tonic-gate 			    "exist, so it cannot be verified.\nWhen 'zoneadm "
6950Sstevel@tonic-gate 			    "%s' is run, '%s' will try to create\n%s, and '%s' "
6960Sstevel@tonic-gate 			    "will be tried again,\nbut the '%s' may fail if:\n"
6970Sstevel@tonic-gate 			    "the parent directory of %s is group- or other-"
6980Sstevel@tonic-gate 			    "writable\nor\n%s overlaps with any other "
6990Sstevel@tonic-gate 			    "installed zones.\n"), path,
7000Sstevel@tonic-gate 			    cmd_to_str(CMD_INSTALL), cmd_to_str(CMD_INSTALL),
7010Sstevel@tonic-gate 			    path, cmd_to_str(CMD_VERIFY),
7020Sstevel@tonic-gate 			    cmd_to_str(CMD_VERIFY), path, path);
7030Sstevel@tonic-gate 			return (Z_OK);
7040Sstevel@tonic-gate 		}
7050Sstevel@tonic-gate 		/*
7060Sstevel@tonic-gate 		 * The zonepath is supposed to be mode 700 but its
7070Sstevel@tonic-gate 		 * parent(s) 755.  So use 755 on the mkdirp() then
7080Sstevel@tonic-gate 		 * chmod() the zonepath itself to 700.
7090Sstevel@tonic-gate 		 */
7100Sstevel@tonic-gate 		if (mkdirp(path, DEFAULT_DIR_MODE) < 0) {
7110Sstevel@tonic-gate 			zperror(path, B_FALSE);
7120Sstevel@tonic-gate 			return (Z_ERR);
7130Sstevel@tonic-gate 		}
7140Sstevel@tonic-gate 		/*
7150Sstevel@tonic-gate 		 * If the chmod() fails, report the error, but might
7160Sstevel@tonic-gate 		 * as well continue the verify procedure.
7170Sstevel@tonic-gate 		 */
7180Sstevel@tonic-gate 		if (chmod(path, S_IRWXU) != 0)
7190Sstevel@tonic-gate 			zperror(path, B_FALSE);
7200Sstevel@tonic-gate 		/*
7210Sstevel@tonic-gate 		 * Since the mkdir() succeeded, we should not have to
7220Sstevel@tonic-gate 		 * worry about a subsequent ENOENT, thus this should
7230Sstevel@tonic-gate 		 * only recurse once.
7240Sstevel@tonic-gate 		 */
7250Sstevel@tonic-gate 		return (validate_zonepath(path, CMD_INSTALL));
7260Sstevel@tonic-gate 	}
7270Sstevel@tonic-gate 	rpath[res] = '\0';
7280Sstevel@tonic-gate 	if (strcmp(path, rpath) != 0) {
7290Sstevel@tonic-gate 		errno = Z_RESOLVED_PATH;
7300Sstevel@tonic-gate 		zperror(path, B_TRUE);
7310Sstevel@tonic-gate 		return (Z_ERR);
7320Sstevel@tonic-gate 	}
7330Sstevel@tonic-gate 	if ((res = stat(rpath, &stbuf)) != 0) {
7340Sstevel@tonic-gate 		zperror(rpath, B_FALSE);
7350Sstevel@tonic-gate 		return (Z_ERR);
7360Sstevel@tonic-gate 	}
7370Sstevel@tonic-gate 	if (!S_ISDIR(stbuf.st_mode)) {
7380Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("%s is not a directory.\n"),
7390Sstevel@tonic-gate 		    rpath);
7400Sstevel@tonic-gate 		return (Z_ERR);
7410Sstevel@tonic-gate 	}
7420Sstevel@tonic-gate 	if ((strcmp(stbuf.st_fstype, MNTTYPE_TMPFS) == 0) ||
7430Sstevel@tonic-gate 	    (strcmp(stbuf.st_fstype, MNTTYPE_XMEMFS) == 0)) {
7440Sstevel@tonic-gate 		(void) printf(gettext("WARNING: %s is on a temporary "
7450Sstevel@tonic-gate 		    "file-system.\n"), rpath);
7460Sstevel@tonic-gate 	}
7470Sstevel@tonic-gate 	if (crosscheck_zonepaths(rpath) != Z_OK)
7480Sstevel@tonic-gate 		return (Z_ERR);
7490Sstevel@tonic-gate 	/*
7500Sstevel@tonic-gate 	 * Try to collect and report as many minor errors as possible
7510Sstevel@tonic-gate 	 * before returning, so the user can learn everything that needs
7520Sstevel@tonic-gate 	 * to be fixed up front.
7530Sstevel@tonic-gate 	 */
7540Sstevel@tonic-gate 	if (stbuf.st_uid != 0) {
7550Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("%s is not owned by root.\n"),
7560Sstevel@tonic-gate 		    rpath);
7570Sstevel@tonic-gate 		err = B_TRUE;
7580Sstevel@tonic-gate 	}
7590Sstevel@tonic-gate 	err |= bad_mode_bit(stbuf.st_mode, S_IRUSR, B_TRUE, rpath);
7600Sstevel@tonic-gate 	err |= bad_mode_bit(stbuf.st_mode, S_IWUSR, B_TRUE, rpath);
7610Sstevel@tonic-gate 	err |= bad_mode_bit(stbuf.st_mode, S_IXUSR, B_TRUE, rpath);
7620Sstevel@tonic-gate 	err |= bad_mode_bit(stbuf.st_mode, S_IRGRP, B_FALSE, rpath);
7630Sstevel@tonic-gate 	err |= bad_mode_bit(stbuf.st_mode, S_IWGRP, B_FALSE, rpath);
7640Sstevel@tonic-gate 	err |= bad_mode_bit(stbuf.st_mode, S_IXGRP, B_FALSE, rpath);
7650Sstevel@tonic-gate 	err |= bad_mode_bit(stbuf.st_mode, S_IROTH, B_FALSE, rpath);
7660Sstevel@tonic-gate 	err |= bad_mode_bit(stbuf.st_mode, S_IWOTH, B_FALSE, rpath);
7670Sstevel@tonic-gate 	err |= bad_mode_bit(stbuf.st_mode, S_IXOTH, B_FALSE, rpath);
7680Sstevel@tonic-gate 
7690Sstevel@tonic-gate 	(void) snprintf(ppath, sizeof (ppath), "%s/..", path);
7700Sstevel@tonic-gate 	if ((res = resolvepath(ppath, rppath, sizeof (rppath))) == -1) {
7710Sstevel@tonic-gate 		zperror(ppath, B_FALSE);
7720Sstevel@tonic-gate 		return (Z_ERR);
7730Sstevel@tonic-gate 	}
7740Sstevel@tonic-gate 	rppath[res] = '\0';
7750Sstevel@tonic-gate 	if ((res = stat(rppath, &stbuf)) != 0) {
7760Sstevel@tonic-gate 		zperror(rppath, B_FALSE);
7770Sstevel@tonic-gate 		return (Z_ERR);
7780Sstevel@tonic-gate 	}
7790Sstevel@tonic-gate 	/* theoretically impossible */
7800Sstevel@tonic-gate 	if (!S_ISDIR(stbuf.st_mode)) {
7810Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("%s is not a directory.\n"),
7820Sstevel@tonic-gate 		    rppath);
7830Sstevel@tonic-gate 		return (Z_ERR);
7840Sstevel@tonic-gate 	}
7850Sstevel@tonic-gate 	if (stbuf.st_uid != 0) {
7860Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("%s is not owned by root.\n"),
7870Sstevel@tonic-gate 		    rppath);
7880Sstevel@tonic-gate 		err = B_TRUE;
7890Sstevel@tonic-gate 	}
7900Sstevel@tonic-gate 	err |= bad_mode_bit(stbuf.st_mode, S_IRUSR, B_TRUE, rppath);
7910Sstevel@tonic-gate 	err |= bad_mode_bit(stbuf.st_mode, S_IWUSR, B_TRUE, rppath);
7920Sstevel@tonic-gate 	err |= bad_mode_bit(stbuf.st_mode, S_IXUSR, B_TRUE, rppath);
7930Sstevel@tonic-gate 	err |= bad_mode_bit(stbuf.st_mode, S_IWGRP, B_FALSE, rppath);
7940Sstevel@tonic-gate 	err |= bad_mode_bit(stbuf.st_mode, S_IWOTH, B_FALSE, rppath);
7950Sstevel@tonic-gate 	if (strcmp(rpath, rppath) == 0) {
7960Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("%s is its own parent.\n"),
7970Sstevel@tonic-gate 		    rppath);
7980Sstevel@tonic-gate 		err = B_TRUE;
7990Sstevel@tonic-gate 	}
8000Sstevel@tonic-gate 
8010Sstevel@tonic-gate 	if (statvfs(rpath, &vfsbuf) != 0) {
8020Sstevel@tonic-gate 		zperror(rpath, B_FALSE);
8030Sstevel@tonic-gate 		return (Z_ERR);
8040Sstevel@tonic-gate 	}
8050Sstevel@tonic-gate 	if (strncmp(vfsbuf.f_basetype, "nfs", 3) == 0) {
8060Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("Zonepath %s is over NFS, "
8070Sstevel@tonic-gate 		    "which is not currently supported.\n"), rpath);
8080Sstevel@tonic-gate 		return (Z_ERR);
8090Sstevel@tonic-gate 	}
8100Sstevel@tonic-gate 
8110Sstevel@tonic-gate 	if ((res = zone_get_state(target_zone, &state)) != Z_OK) {
8120Sstevel@tonic-gate 		errno = res;
8130Sstevel@tonic-gate 		zperror2(target_zone, gettext("could not get state"));
8140Sstevel@tonic-gate 		return (Z_ERR);
8150Sstevel@tonic-gate 	}
8160Sstevel@tonic-gate 	/*
8170Sstevel@tonic-gate 	 * The existence of the root path is only bad in the configured state,
8180Sstevel@tonic-gate 	 * as it is *supposed* to be there at the installed and later states.
8190Sstevel@tonic-gate 	 * State/command mismatches are caught earlier in verify_details().
8200Sstevel@tonic-gate 	 */
8210Sstevel@tonic-gate 	if (state == ZONE_STATE_CONFIGURED) {
8220Sstevel@tonic-gate 		if (snprintf(rootpath, sizeof (rootpath), "%s/root", rpath) >=
8230Sstevel@tonic-gate 		    sizeof (rootpath)) {
8240Sstevel@tonic-gate 			(void) fprintf(stderr,
8250Sstevel@tonic-gate 			    gettext("Zonepath %s is too long.\n"), rpath);
8260Sstevel@tonic-gate 			return (Z_ERR);
8270Sstevel@tonic-gate 		}
8280Sstevel@tonic-gate 		if ((res = stat(rootpath, &stbuf)) == 0) {
8290Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("Rootpath %s exists; "
8300Sstevel@tonic-gate 			    "remove or move aside prior to %s.\n"), rootpath,
8310Sstevel@tonic-gate 			    cmd_to_str(CMD_INSTALL));
8320Sstevel@tonic-gate 			return (Z_ERR);
8330Sstevel@tonic-gate 		}
8340Sstevel@tonic-gate 	}
8350Sstevel@tonic-gate 
8360Sstevel@tonic-gate 	return (err ? Z_ERR : Z_OK);
8370Sstevel@tonic-gate }
8380Sstevel@tonic-gate 
8390Sstevel@tonic-gate static void
8400Sstevel@tonic-gate release_lock_file(int lockfd)
8410Sstevel@tonic-gate {
8420Sstevel@tonic-gate 	(void) close(lockfd);
8430Sstevel@tonic-gate }
8440Sstevel@tonic-gate 
8450Sstevel@tonic-gate static int
8460Sstevel@tonic-gate grab_lock_file(const char *zone_name, int *lockfd)
8470Sstevel@tonic-gate {
8480Sstevel@tonic-gate 	char pathbuf[PATH_MAX];
8490Sstevel@tonic-gate 	struct flock flock;
8500Sstevel@tonic-gate 
8510Sstevel@tonic-gate 	if (mkdir(ZONES_TMPDIR, S_IRWXU) < 0 && errno != EEXIST) {
8520Sstevel@tonic-gate 		zerror(gettext("could not mkdir %s: %s"), ZONES_TMPDIR,
8530Sstevel@tonic-gate 		    strerror(errno));
8540Sstevel@tonic-gate 		return (Z_ERR);
8550Sstevel@tonic-gate 	}
8560Sstevel@tonic-gate 	(void) chmod(ZONES_TMPDIR, S_IRWXU);
8570Sstevel@tonic-gate 
8580Sstevel@tonic-gate 	/*
8590Sstevel@tonic-gate 	 * One of these lock files is created for each zone (when needed).
8600Sstevel@tonic-gate 	 * The lock files are not cleaned up (except on system reboot),
8610Sstevel@tonic-gate 	 * but since there is only one per zone, there is no resource
8620Sstevel@tonic-gate 	 * starvation issue.
8630Sstevel@tonic-gate 	 */
8640Sstevel@tonic-gate 	(void) snprintf(pathbuf, sizeof (pathbuf), "%s/%s.zoneadm.lock",
8650Sstevel@tonic-gate 	    ZONES_TMPDIR, zone_name);
8660Sstevel@tonic-gate 	if ((*lockfd = open(pathbuf, O_RDWR|O_CREAT, S_IRUSR|S_IWUSR)) < 0) {
8670Sstevel@tonic-gate 		zerror(gettext("could not open %s: %s"), pathbuf,
8680Sstevel@tonic-gate 		    strerror(errno));
8690Sstevel@tonic-gate 		return (Z_ERR);
8700Sstevel@tonic-gate 	}
8710Sstevel@tonic-gate 	/*
8720Sstevel@tonic-gate 	 * Lock the file to synchronize with other zoneadmds
8730Sstevel@tonic-gate 	 */
8740Sstevel@tonic-gate 	flock.l_type = F_WRLCK;
8750Sstevel@tonic-gate 	flock.l_whence = SEEK_SET;
8760Sstevel@tonic-gate 	flock.l_start = (off_t)0;
8770Sstevel@tonic-gate 	flock.l_len = (off_t)0;
8780Sstevel@tonic-gate 	if (fcntl(*lockfd, F_SETLKW, &flock) < 0) {
8790Sstevel@tonic-gate 		zerror(gettext("unable to lock %s: %s"), pathbuf,
8800Sstevel@tonic-gate 		    strerror(errno));
8810Sstevel@tonic-gate 		release_lock_file(*lockfd);
8820Sstevel@tonic-gate 		return (Z_ERR);
8830Sstevel@tonic-gate 	}
8840Sstevel@tonic-gate 	return (Z_OK);
8850Sstevel@tonic-gate }
8860Sstevel@tonic-gate 
8870Sstevel@tonic-gate static void
8880Sstevel@tonic-gate get_doorname(const char *zone_name, char *buffer)
8890Sstevel@tonic-gate {
8900Sstevel@tonic-gate 	(void) snprintf(buffer, PATH_MAX, ZONE_DOOR_PATH, zone_name);
8910Sstevel@tonic-gate }
8920Sstevel@tonic-gate 
8930Sstevel@tonic-gate /*
8940Sstevel@tonic-gate  * system daemons are not audited.  For the global zone, this occurs
8950Sstevel@tonic-gate  * "naturally" since init is started with the default audit
8960Sstevel@tonic-gate  * characteristics.  Since zoneadmd is a system daemon and it starts
8970Sstevel@tonic-gate  * init for a zone, it is necessary to clear out the audit
8980Sstevel@tonic-gate  * characteristics inherited from whomever started zoneadmd.  This is
8990Sstevel@tonic-gate  * indicated by the audit id, which is set from the ruid parameter of
9000Sstevel@tonic-gate  * adt_set_user(), below.
9010Sstevel@tonic-gate  */
9020Sstevel@tonic-gate 
9030Sstevel@tonic-gate static void
9040Sstevel@tonic-gate prepare_audit_context()
9050Sstevel@tonic-gate {
9060Sstevel@tonic-gate 	adt_session_data_t	*ah;
9070Sstevel@tonic-gate 	char			*failure = gettext("audit failure: %s");
9080Sstevel@tonic-gate 
9090Sstevel@tonic-gate 	if (adt_start_session(&ah, NULL, 0)) {
9100Sstevel@tonic-gate 		zerror(failure, strerror(errno));
9110Sstevel@tonic-gate 		return;
9120Sstevel@tonic-gate 	}
9130Sstevel@tonic-gate 	if (adt_set_user(ah, ADT_NO_AUDIT, ADT_NO_AUDIT,
9140Sstevel@tonic-gate 	    ADT_NO_AUDIT, ADT_NO_AUDIT, NULL, ADT_NEW)) {
9150Sstevel@tonic-gate 		zerror(failure, strerror(errno));
9160Sstevel@tonic-gate 		(void) adt_end_session(ah);
9170Sstevel@tonic-gate 		return;
9180Sstevel@tonic-gate 	}
9190Sstevel@tonic-gate 	if (adt_set_proc(ah))
9200Sstevel@tonic-gate 		zerror(failure, strerror(errno));
9210Sstevel@tonic-gate 
9220Sstevel@tonic-gate 	(void) adt_end_session(ah);
9230Sstevel@tonic-gate }
9240Sstevel@tonic-gate 
9250Sstevel@tonic-gate static int
9260Sstevel@tonic-gate start_zoneadmd(const char *zone_name)
9270Sstevel@tonic-gate {
9280Sstevel@tonic-gate 	char doorpath[PATH_MAX];
9290Sstevel@tonic-gate 	pid_t child_pid;
9300Sstevel@tonic-gate 	int error = Z_ERR;
9310Sstevel@tonic-gate 	int doorfd, lockfd;
9320Sstevel@tonic-gate 	struct door_info info;
9330Sstevel@tonic-gate 
9340Sstevel@tonic-gate 	get_doorname(zone_name, doorpath);
9350Sstevel@tonic-gate 
9360Sstevel@tonic-gate 	if (grab_lock_file(zone_name, &lockfd) != Z_OK)
9370Sstevel@tonic-gate 		return (Z_ERR);
9380Sstevel@tonic-gate 
9390Sstevel@tonic-gate 	/*
9400Sstevel@tonic-gate 	 * Now that we have the lock, re-confirm that the daemon is
9410Sstevel@tonic-gate 	 * *not* up and working fine.  If it is still down, we have a green
9420Sstevel@tonic-gate 	 * light to start it.
9430Sstevel@tonic-gate 	 */
9440Sstevel@tonic-gate 	if ((doorfd = open(doorpath, O_RDONLY)) < 0) {
9450Sstevel@tonic-gate 		if (errno != ENOENT) {
9460Sstevel@tonic-gate 			zperror(doorpath, B_FALSE);
9470Sstevel@tonic-gate 			goto out;
9480Sstevel@tonic-gate 		}
9490Sstevel@tonic-gate 	} else {
9500Sstevel@tonic-gate 		if (door_info(doorfd, &info) == 0 &&
9510Sstevel@tonic-gate 		    ((info.di_attributes & DOOR_REVOKED) == 0)) {
9520Sstevel@tonic-gate 			error = Z_OK;
9530Sstevel@tonic-gate 			(void) close(doorfd);
9540Sstevel@tonic-gate 			goto out;
9550Sstevel@tonic-gate 		}
9560Sstevel@tonic-gate 		(void) close(doorfd);
9570Sstevel@tonic-gate 	}
9580Sstevel@tonic-gate 
9590Sstevel@tonic-gate 	if ((child_pid = fork()) == -1) {
9600Sstevel@tonic-gate 		zperror(gettext("could not fork"), B_FALSE);
9610Sstevel@tonic-gate 		goto out;
9620Sstevel@tonic-gate 	} else if (child_pid == 0) {
9630Sstevel@tonic-gate 		/* child process */
9640Sstevel@tonic-gate 
9650Sstevel@tonic-gate 		prepare_audit_context();
9660Sstevel@tonic-gate 
9670Sstevel@tonic-gate 		(void) execl("/usr/lib/zones/zoneadmd", "zoneadmd", "-z",
9680Sstevel@tonic-gate 		    zone_name, NULL);
9690Sstevel@tonic-gate 		zperror(gettext("could not exec zoneadmd"), B_FALSE);
9700Sstevel@tonic-gate 		_exit(Z_ERR);
9710Sstevel@tonic-gate 	} else {
9720Sstevel@tonic-gate 		/* parent process */
9730Sstevel@tonic-gate 		pid_t retval;
9740Sstevel@tonic-gate 		int pstatus = 0;
9750Sstevel@tonic-gate 
9760Sstevel@tonic-gate 		do {
9770Sstevel@tonic-gate 			retval = waitpid(child_pid, &pstatus, 0);
9780Sstevel@tonic-gate 		} while (retval != child_pid);
9790Sstevel@tonic-gate 		if (WIFSIGNALED(pstatus) || (WIFEXITED(pstatus) &&
9800Sstevel@tonic-gate 		    WEXITSTATUS(pstatus) != 0)) {
9810Sstevel@tonic-gate 			zerror(gettext("could not start %s"), "zoneadmd");
9820Sstevel@tonic-gate 			goto out;
9830Sstevel@tonic-gate 		}
9840Sstevel@tonic-gate 	}
9850Sstevel@tonic-gate 	error = Z_OK;
9860Sstevel@tonic-gate out:
9870Sstevel@tonic-gate 	release_lock_file(lockfd);
9880Sstevel@tonic-gate 	return (error);
9890Sstevel@tonic-gate }
9900Sstevel@tonic-gate 
9910Sstevel@tonic-gate static int
9920Sstevel@tonic-gate ping_zoneadmd(const char *zone_name)
9930Sstevel@tonic-gate {
9940Sstevel@tonic-gate 	char doorpath[PATH_MAX];
9950Sstevel@tonic-gate 	int doorfd;
9960Sstevel@tonic-gate 	struct door_info info;
9970Sstevel@tonic-gate 
9980Sstevel@tonic-gate 	get_doorname(zone_name, doorpath);
9990Sstevel@tonic-gate 
10000Sstevel@tonic-gate 	if ((doorfd = open(doorpath, O_RDONLY)) < 0) {
10010Sstevel@tonic-gate 		return (Z_ERR);
10020Sstevel@tonic-gate 	}
10030Sstevel@tonic-gate 	if (door_info(doorfd, &info) == 0 &&
10040Sstevel@tonic-gate 	    ((info.di_attributes & DOOR_REVOKED) == 0)) {
10050Sstevel@tonic-gate 		(void) close(doorfd);
10060Sstevel@tonic-gate 		return (Z_OK);
10070Sstevel@tonic-gate 	}
10080Sstevel@tonic-gate 	(void) close(doorfd);
10090Sstevel@tonic-gate 	return (Z_ERR);
10100Sstevel@tonic-gate }
10110Sstevel@tonic-gate 
10120Sstevel@tonic-gate static int
10130Sstevel@tonic-gate call_zoneadmd(const char *zone_name, zone_cmd_arg_t *arg)
10140Sstevel@tonic-gate {
10150Sstevel@tonic-gate 	char doorpath[PATH_MAX];
10160Sstevel@tonic-gate 	int doorfd, result;
10170Sstevel@tonic-gate 	door_arg_t darg;
10180Sstevel@tonic-gate 
10190Sstevel@tonic-gate 	zoneid_t zoneid;
10200Sstevel@tonic-gate 	uint64_t uniqid = 0;
10210Sstevel@tonic-gate 
10220Sstevel@tonic-gate 	zone_cmd_rval_t *rvalp;
10230Sstevel@tonic-gate 	size_t rlen;
10240Sstevel@tonic-gate 	char *cp, *errbuf;
10250Sstevel@tonic-gate 
10260Sstevel@tonic-gate 	rlen = getpagesize();
10270Sstevel@tonic-gate 	if ((rvalp = malloc(rlen)) == NULL) {
10280Sstevel@tonic-gate 		zerror(gettext("failed to allocate %lu bytes: %s"), rlen,
10290Sstevel@tonic-gate 		    strerror(errno));
10300Sstevel@tonic-gate 		return (-1);
10310Sstevel@tonic-gate 	}
10320Sstevel@tonic-gate 
10330Sstevel@tonic-gate 	if ((zoneid = getzoneidbyname(zone_name)) != ZONE_ID_UNDEFINED) {
10340Sstevel@tonic-gate 		(void) zone_getattr(zoneid, ZONE_ATTR_UNIQID, &uniqid,
10350Sstevel@tonic-gate 		    sizeof (uniqid));
10360Sstevel@tonic-gate 	}
10370Sstevel@tonic-gate 	arg->uniqid = uniqid;
10380Sstevel@tonic-gate 	(void) strlcpy(arg->locale, locale, sizeof (arg->locale));
10390Sstevel@tonic-gate 	get_doorname(zone_name, doorpath);
10400Sstevel@tonic-gate 
10410Sstevel@tonic-gate 	/*
10420Sstevel@tonic-gate 	 * Loop trying to start zoneadmd; if something goes seriously
10430Sstevel@tonic-gate 	 * wrong we break out and fail.
10440Sstevel@tonic-gate 	 */
10450Sstevel@tonic-gate 	for (;;) {
10460Sstevel@tonic-gate 		if (start_zoneadmd(zone_name) != Z_OK)
10470Sstevel@tonic-gate 			break;
10480Sstevel@tonic-gate 
10490Sstevel@tonic-gate 		if ((doorfd = open(doorpath, O_RDONLY)) < 0) {
10500Sstevel@tonic-gate 			zperror(gettext("failed to open zone door"), B_FALSE);
10510Sstevel@tonic-gate 			break;
10520Sstevel@tonic-gate 		}
10530Sstevel@tonic-gate 
10540Sstevel@tonic-gate 		darg.data_ptr = (char *)arg;
10550Sstevel@tonic-gate 		darg.data_size = sizeof (*arg);
10560Sstevel@tonic-gate 		darg.desc_ptr = NULL;
10570Sstevel@tonic-gate 		darg.desc_num = 0;
10580Sstevel@tonic-gate 		darg.rbuf = (char *)rvalp;
10590Sstevel@tonic-gate 		darg.rsize = rlen;
10600Sstevel@tonic-gate 		if (door_call(doorfd, &darg) != 0) {
10610Sstevel@tonic-gate 			(void) close(doorfd);
10620Sstevel@tonic-gate 			/*
10630Sstevel@tonic-gate 			 * We'll get EBADF if the door has been revoked.
10640Sstevel@tonic-gate 			 */
10650Sstevel@tonic-gate 			if (errno != EBADF) {
10660Sstevel@tonic-gate 				zperror(gettext("door_call failed"), B_FALSE);
10670Sstevel@tonic-gate 				break;
10680Sstevel@tonic-gate 			}
10690Sstevel@tonic-gate 			continue;	/* take another lap */
10700Sstevel@tonic-gate 		}
10710Sstevel@tonic-gate 		(void) close(doorfd);
10720Sstevel@tonic-gate 
10730Sstevel@tonic-gate 		if (darg.data_size == 0) {
10740Sstevel@tonic-gate 			/* Door server is going away; kick it again. */
10750Sstevel@tonic-gate 			continue;
10760Sstevel@tonic-gate 		}
10770Sstevel@tonic-gate 
10780Sstevel@tonic-gate 		errbuf = rvalp->errbuf;
10790Sstevel@tonic-gate 		while (*errbuf != '\0') {
10800Sstevel@tonic-gate 			/*
10810Sstevel@tonic-gate 			 * Remove any newlines since zerror()
10820Sstevel@tonic-gate 			 * will append one automatically.
10830Sstevel@tonic-gate 			 */
10840Sstevel@tonic-gate 			cp = strchr(errbuf, '\n');
10850Sstevel@tonic-gate 			if (cp != NULL)
10860Sstevel@tonic-gate 				*cp = '\0';
10870Sstevel@tonic-gate 			zerror("%s", errbuf);
10880Sstevel@tonic-gate 			if (cp == NULL)
10890Sstevel@tonic-gate 				break;
10900Sstevel@tonic-gate 			errbuf = cp + 1;
10910Sstevel@tonic-gate 		}
10920Sstevel@tonic-gate 		result = rvalp->rval == 0 ? 0 : -1;
10930Sstevel@tonic-gate 		free(rvalp);
10940Sstevel@tonic-gate 		return (result);
10950Sstevel@tonic-gate 	}
10960Sstevel@tonic-gate 
10970Sstevel@tonic-gate 	free(rvalp);
10980Sstevel@tonic-gate 	return (-1);
10990Sstevel@tonic-gate }
11000Sstevel@tonic-gate 
11010Sstevel@tonic-gate static int
11020Sstevel@tonic-gate ready_func(int argc, char *argv[])
11030Sstevel@tonic-gate {
11040Sstevel@tonic-gate 	zone_cmd_arg_t zarg;
11050Sstevel@tonic-gate 	int arg;
11060Sstevel@tonic-gate 
11070Sstevel@tonic-gate 	optind = 0;
11080Sstevel@tonic-gate 	if ((arg = getopt(argc, argv, "?")) != EOF) {
11090Sstevel@tonic-gate 		switch (arg) {
11100Sstevel@tonic-gate 		case '?':
11110Sstevel@tonic-gate 			sub_usage(SHELP_READY, CMD_READY);
11120Sstevel@tonic-gate 			return (optopt == '?' ? Z_OK : Z_USAGE);
11130Sstevel@tonic-gate 		default:
11140Sstevel@tonic-gate 			sub_usage(SHELP_READY, CMD_READY);
11150Sstevel@tonic-gate 			return (Z_USAGE);
11160Sstevel@tonic-gate 		}
11170Sstevel@tonic-gate 	}
11180Sstevel@tonic-gate 	if (argc > optind) {
11190Sstevel@tonic-gate 		sub_usage(SHELP_READY, CMD_READY);
11200Sstevel@tonic-gate 		return (Z_USAGE);
11210Sstevel@tonic-gate 	}
11220Sstevel@tonic-gate 	if (sanity_check(target_zone, CMD_READY, B_FALSE, B_FALSE) != Z_OK)
11230Sstevel@tonic-gate 		return (Z_ERR);
11240Sstevel@tonic-gate 	if (verify_details(CMD_READY) != Z_OK)
11250Sstevel@tonic-gate 		return (Z_ERR);
11260Sstevel@tonic-gate 
11270Sstevel@tonic-gate 	zarg.cmd = Z_READY;
11280Sstevel@tonic-gate 	if (call_zoneadmd(target_zone, &zarg) != 0) {
11290Sstevel@tonic-gate 		zerror(gettext("call to %s failed"), "zoneadmd");
11300Sstevel@tonic-gate 		return (Z_ERR);
11310Sstevel@tonic-gate 	}
11320Sstevel@tonic-gate 	return (Z_OK);
11330Sstevel@tonic-gate }
11340Sstevel@tonic-gate 
11350Sstevel@tonic-gate static int
11360Sstevel@tonic-gate boot_func(int argc, char *argv[])
11370Sstevel@tonic-gate {
11380Sstevel@tonic-gate 	zone_cmd_arg_t zarg;
11390Sstevel@tonic-gate 	int arg;
11400Sstevel@tonic-gate 
11410Sstevel@tonic-gate 	zarg.bootbuf[0] = '\0';
11420Sstevel@tonic-gate 
11430Sstevel@tonic-gate 	/*
11440Sstevel@tonic-gate 	 * At the current time, the only supported subargument to the
11450Sstevel@tonic-gate 	 * "boot" subcommand is "-s" which specifies a single-user boot.
11460Sstevel@tonic-gate 	 * In the future, other boot arguments should be supported
11470Sstevel@tonic-gate 	 * including "-m" for specifying alternate smf(5) milestones.
11480Sstevel@tonic-gate 	 */
11490Sstevel@tonic-gate 	optind = 0;
11500Sstevel@tonic-gate 	if ((arg = getopt(argc, argv, "?s")) != EOF) {
11510Sstevel@tonic-gate 		switch (arg) {
11520Sstevel@tonic-gate 		case '?':
11530Sstevel@tonic-gate 			sub_usage(SHELP_BOOT, CMD_BOOT);
11540Sstevel@tonic-gate 			return (optopt == '?' ? Z_OK : Z_USAGE);
11550Sstevel@tonic-gate 		case 's':
11560Sstevel@tonic-gate 			(void) strlcpy(zarg.bootbuf, "-s",
11570Sstevel@tonic-gate 			    sizeof (zarg.bootbuf));
11580Sstevel@tonic-gate 			break;
11590Sstevel@tonic-gate 		default:
11600Sstevel@tonic-gate 			sub_usage(SHELP_BOOT, CMD_BOOT);
11610Sstevel@tonic-gate 			return (Z_USAGE);
11620Sstevel@tonic-gate 		}
11630Sstevel@tonic-gate 	}
11640Sstevel@tonic-gate 	if (argc > optind) {
11650Sstevel@tonic-gate 		sub_usage(SHELP_BOOT, CMD_BOOT);
11660Sstevel@tonic-gate 		return (Z_USAGE);
11670Sstevel@tonic-gate 	}
11680Sstevel@tonic-gate 	if (sanity_check(target_zone, CMD_BOOT, B_FALSE, B_FALSE) != Z_OK)
11690Sstevel@tonic-gate 		return (Z_ERR);
11700Sstevel@tonic-gate 	if (verify_details(CMD_BOOT) != Z_OK)
11710Sstevel@tonic-gate 		return (Z_ERR);
11720Sstevel@tonic-gate 	zarg.cmd = Z_BOOT;
11730Sstevel@tonic-gate 	if (call_zoneadmd(target_zone, &zarg) != 0) {
11740Sstevel@tonic-gate 		zerror(gettext("call to %s failed"), "zoneadmd");
11750Sstevel@tonic-gate 		return (Z_ERR);
11760Sstevel@tonic-gate 	}
11770Sstevel@tonic-gate 	return (Z_OK);
11780Sstevel@tonic-gate }
11790Sstevel@tonic-gate 
11800Sstevel@tonic-gate static void
11810Sstevel@tonic-gate fake_up_local_zone(zoneid_t zid, zone_entry_t *zeptr)
11820Sstevel@tonic-gate {
11830Sstevel@tonic-gate 	ssize_t result;
11840Sstevel@tonic-gate 
11850Sstevel@tonic-gate 	zeptr->zid = zid;
11860Sstevel@tonic-gate 	/*
11870Sstevel@tonic-gate 	 * Since we're looking up our own (non-global) zone name,
11880Sstevel@tonic-gate 	 * we can be assured that it will succeed.
11890Sstevel@tonic-gate 	 */
11900Sstevel@tonic-gate 	result = getzonenamebyid(zid, zeptr->zname, sizeof (zeptr->zname));
11910Sstevel@tonic-gate 	assert(result >= 0);
11920Sstevel@tonic-gate 	(void) strlcpy(zeptr->zroot, "/", sizeof (zeptr->zroot));
11930Sstevel@tonic-gate 	zeptr->zstate_str = "running";
11940Sstevel@tonic-gate }
11950Sstevel@tonic-gate 
11960Sstevel@tonic-gate static int
11970Sstevel@tonic-gate list_func(int argc, char *argv[])
11980Sstevel@tonic-gate {
11990Sstevel@tonic-gate 	zone_entry_t *zentp, zent;
12000Sstevel@tonic-gate 	int arg;
12010Sstevel@tonic-gate 	boolean_t output = B_FALSE, verbose = B_FALSE, parsable = B_FALSE;
12020Sstevel@tonic-gate 	zone_state_t min_state = ZONE_STATE_RUNNING;
12030Sstevel@tonic-gate 	zoneid_t zone_id = getzoneid();
12040Sstevel@tonic-gate 
12050Sstevel@tonic-gate 	if (target_zone == NULL) {
12060Sstevel@tonic-gate 		/* all zones: default view to running but allow override */
12070Sstevel@tonic-gate 		optind = 0;
12080Sstevel@tonic-gate 		while ((arg = getopt(argc, argv, "?cipv")) != EOF) {
12090Sstevel@tonic-gate 			switch (arg) {
12100Sstevel@tonic-gate 			case '?':
12110Sstevel@tonic-gate 				sub_usage(SHELP_LIST, CMD_LIST);
12120Sstevel@tonic-gate 				return (optopt == '?' ? Z_OK : Z_USAGE);
12130Sstevel@tonic-gate 			case 'c':
12140Sstevel@tonic-gate 				min_state = ZONE_STATE_CONFIGURED;
12150Sstevel@tonic-gate 				break;
12160Sstevel@tonic-gate 			case 'i':
12170Sstevel@tonic-gate 				min_state = ZONE_STATE_INSTALLED;
12180Sstevel@tonic-gate 				break;
12190Sstevel@tonic-gate 			case 'p':
12200Sstevel@tonic-gate 				parsable = B_TRUE;
12210Sstevel@tonic-gate 				break;
12220Sstevel@tonic-gate 			case 'v':
12230Sstevel@tonic-gate 				verbose = B_TRUE;
12240Sstevel@tonic-gate 				break;
12250Sstevel@tonic-gate 			default:
12260Sstevel@tonic-gate 				sub_usage(SHELP_LIST, CMD_LIST);
12270Sstevel@tonic-gate 				return (Z_USAGE);
12280Sstevel@tonic-gate 			}
12290Sstevel@tonic-gate 		}
12300Sstevel@tonic-gate 		if (parsable && verbose) {
12310Sstevel@tonic-gate 			zerror(gettext("%s -p and -v are mutually exclusive."),
12320Sstevel@tonic-gate 			    cmd_to_str(CMD_LIST));
12330Sstevel@tonic-gate 			return (Z_ERR);
12340Sstevel@tonic-gate 		}
12350Sstevel@tonic-gate 		if (zone_id == GLOBAL_ZONEID) {
12360Sstevel@tonic-gate 			zone_print_list(min_state, verbose, parsable);
12370Sstevel@tonic-gate 		} else {
12380Sstevel@tonic-gate 			fake_up_local_zone(zone_id, &zent);
12390Sstevel@tonic-gate 			zone_print(&zent, verbose, parsable);
12400Sstevel@tonic-gate 		}
12410Sstevel@tonic-gate 		return (Z_OK);
12420Sstevel@tonic-gate 	}
12430Sstevel@tonic-gate 
12440Sstevel@tonic-gate 	/*
12450Sstevel@tonic-gate 	 * Specific target zone: disallow -i/-c suboptions.
12460Sstevel@tonic-gate 	 */
12470Sstevel@tonic-gate 	optind = 0;
12480Sstevel@tonic-gate 	while ((arg = getopt(argc, argv, "?pv")) != EOF) {
12490Sstevel@tonic-gate 		switch (arg) {
12500Sstevel@tonic-gate 		case '?':
12510Sstevel@tonic-gate 			sub_usage(SHELP_LIST, CMD_LIST);
12520Sstevel@tonic-gate 			return (optopt == '?' ? Z_OK : Z_USAGE);
12530Sstevel@tonic-gate 		case 'p':
12540Sstevel@tonic-gate 			parsable = B_TRUE;
12550Sstevel@tonic-gate 			break;
12560Sstevel@tonic-gate 		case 'v':
12570Sstevel@tonic-gate 			verbose = B_TRUE;
12580Sstevel@tonic-gate 			break;
12590Sstevel@tonic-gate 		default:
12600Sstevel@tonic-gate 			sub_usage(SHELP_LIST, CMD_LIST);
12610Sstevel@tonic-gate 			return (Z_USAGE);
12620Sstevel@tonic-gate 		}
12630Sstevel@tonic-gate 	}
12640Sstevel@tonic-gate 	if (parsable && verbose) {
12650Sstevel@tonic-gate 		zerror(gettext("%s -p and -v are mutually exclusive."),
12660Sstevel@tonic-gate 		    cmd_to_str(CMD_LIST));
12670Sstevel@tonic-gate 		return (Z_ERR);
12680Sstevel@tonic-gate 	}
12690Sstevel@tonic-gate 	if (argc > optind) {
12700Sstevel@tonic-gate 		sub_usage(SHELP_LIST, CMD_LIST);
12710Sstevel@tonic-gate 		return (Z_USAGE);
12720Sstevel@tonic-gate 	}
12730Sstevel@tonic-gate 	if (zone_id != GLOBAL_ZONEID) {
12740Sstevel@tonic-gate 		fake_up_local_zone(zone_id, &zent);
12750Sstevel@tonic-gate 		/*
12760Sstevel@tonic-gate 		 * main() will issue a Z_NO_ZONE error if it cannot get an
12770Sstevel@tonic-gate 		 * id for target_zone, which in a non-global zone should
12780Sstevel@tonic-gate 		 * happen for any zone name except `zonename`.  Thus we
12790Sstevel@tonic-gate 		 * assert() that here but don't otherwise check.
12800Sstevel@tonic-gate 		 */
12810Sstevel@tonic-gate 		assert(strcmp(zent.zname, target_zone) == 0);
12820Sstevel@tonic-gate 		zone_print(&zent, verbose, parsable);
12830Sstevel@tonic-gate 		output = B_TRUE;
12840Sstevel@tonic-gate 	} else if ((zentp = lookup_running_zone(target_zone)) != NULL) {
12850Sstevel@tonic-gate 		zone_print(zentp, verbose, parsable);
12860Sstevel@tonic-gate 		output = B_TRUE;
12870Sstevel@tonic-gate 	} else if (lookup_zone_info(target_zone, &zent) == Z_OK) {
12880Sstevel@tonic-gate 		zone_print(&zent, verbose, parsable);
12890Sstevel@tonic-gate 		output = B_TRUE;
12900Sstevel@tonic-gate 	}
12910Sstevel@tonic-gate 	return (output ? Z_OK : Z_ERR);
12920Sstevel@tonic-gate }
12930Sstevel@tonic-gate 
12940Sstevel@tonic-gate static void
12950Sstevel@tonic-gate sigterm(int sig)
12960Sstevel@tonic-gate {
12970Sstevel@tonic-gate 	/*
12980Sstevel@tonic-gate 	 * Ignore SIG{INT,TERM}, so we don't end up in an infinite loop,
12990Sstevel@tonic-gate 	 * then propagate the signal to our process group.
13000Sstevel@tonic-gate 	 */
13010Sstevel@tonic-gate 	(void) sigset(SIGINT, SIG_IGN);
13020Sstevel@tonic-gate 	(void) sigset(SIGTERM, SIG_IGN);
13030Sstevel@tonic-gate 	(void) kill(0, sig);
13040Sstevel@tonic-gate 	child_killed = B_TRUE;
13050Sstevel@tonic-gate }
13060Sstevel@tonic-gate 
13070Sstevel@tonic-gate static int
13080Sstevel@tonic-gate do_subproc(char *cmdbuf)
13090Sstevel@tonic-gate {
13100Sstevel@tonic-gate 	char inbuf[1024];	/* arbitrary large amount */
13110Sstevel@tonic-gate 	FILE *file;
13120Sstevel@tonic-gate 
13130Sstevel@tonic-gate 	child_killed = B_FALSE;
13140Sstevel@tonic-gate 	/*
13150Sstevel@tonic-gate 	 * We use popen(3c) to launch child processes for [un]install;
13160Sstevel@tonic-gate 	 * this library call does not return a PID, so we have to kill
13170Sstevel@tonic-gate 	 * the whole process group.  To avoid killing our parent, we
13180Sstevel@tonic-gate 	 * become a process group leader here.  But doing so can wreak
13190Sstevel@tonic-gate 	 * havoc with reading from stdin when launched by a non-job-control
13200Sstevel@tonic-gate 	 * shell, so we close stdin and reopen it as /dev/null first.
13210Sstevel@tonic-gate 	 */
13220Sstevel@tonic-gate 	(void) close(STDIN_FILENO);
13230Sstevel@tonic-gate 	(void) open("/dev/null", O_RDONLY);
13240Sstevel@tonic-gate 	(void) setpgid(0, 0);
13250Sstevel@tonic-gate 	(void) sigset(SIGINT, sigterm);
13260Sstevel@tonic-gate 	(void) sigset(SIGTERM, sigterm);
13270Sstevel@tonic-gate 	file = popen(cmdbuf, "r");
13280Sstevel@tonic-gate 	for (;;) {
13290Sstevel@tonic-gate 		if (child_killed || fgets(inbuf, sizeof (inbuf), file) == NULL)
13300Sstevel@tonic-gate 			break;
13310Sstevel@tonic-gate 		(void) fputs(inbuf, stdout);
13320Sstevel@tonic-gate 	}
13330Sstevel@tonic-gate 	(void) sigset(SIGINT, SIG_DFL);
13340Sstevel@tonic-gate 	(void) sigset(SIGTERM, SIG_DFL);
13350Sstevel@tonic-gate 	return (pclose(file));
13360Sstevel@tonic-gate }
13370Sstevel@tonic-gate 
13380Sstevel@tonic-gate static int
13390Sstevel@tonic-gate subproc_status(const char *cmd, int status)
13400Sstevel@tonic-gate {
13410Sstevel@tonic-gate 	if (WIFEXITED(status)) {
13420Sstevel@tonic-gate 		int exit_code = WEXITSTATUS(status);
13430Sstevel@tonic-gate 
13440Sstevel@tonic-gate 		if (exit_code == 0)
13450Sstevel@tonic-gate 			return (Z_OK);
13460Sstevel@tonic-gate 		zerror(gettext("'%s' failed with exit code %d."), cmd,
13470Sstevel@tonic-gate 		    exit_code);
13480Sstevel@tonic-gate 	} else if (WIFSIGNALED(status)) {
13490Sstevel@tonic-gate 		int signal = WTERMSIG(status);
13500Sstevel@tonic-gate 		char sigstr[SIG2STR_MAX];
13510Sstevel@tonic-gate 
13520Sstevel@tonic-gate 		if (sig2str(signal, sigstr) == 0) {
13530Sstevel@tonic-gate 			zerror(gettext("'%s' terminated by signal SIG%s."), cmd,
13540Sstevel@tonic-gate 			    sigstr);
13550Sstevel@tonic-gate 		} else {
13560Sstevel@tonic-gate 			zerror(gettext("'%s' terminated by an unknown signal."),
13570Sstevel@tonic-gate 			    cmd);
13580Sstevel@tonic-gate 		}
13590Sstevel@tonic-gate 	} else {
13600Sstevel@tonic-gate 		zerror(gettext("'%s' failed for unknown reasons."), cmd);
13610Sstevel@tonic-gate 	}
13620Sstevel@tonic-gate 	return (Z_ERR);
13630Sstevel@tonic-gate }
13640Sstevel@tonic-gate 
13650Sstevel@tonic-gate /*
13660Sstevel@tonic-gate  * Various sanity checks; make sure:
13670Sstevel@tonic-gate  * 1. We're in the global zone.
13680Sstevel@tonic-gate  * 2. The calling user has sufficient privilege.
13690Sstevel@tonic-gate  * 3. The target zone is neither the global zone nor anything starting with
13700Sstevel@tonic-gate  *    "SUNW".
13710Sstevel@tonic-gate  * 4a. If we're looking for a 'not running' (i.e., configured or installed)
13720Sstevel@tonic-gate  *     zone, the name service knows about it.
13730Sstevel@tonic-gate  * 4b. For some operations which expect a zone not to be running, that it is
13740Sstevel@tonic-gate  *     not already running (or ready).
13750Sstevel@tonic-gate  */
13760Sstevel@tonic-gate static int
13770Sstevel@tonic-gate sanity_check(char *zone, int cmd_num, boolean_t running,
13780Sstevel@tonic-gate     boolean_t unsafe_when_running)
13790Sstevel@tonic-gate {
13800Sstevel@tonic-gate 	zone_entry_t *zent;
13810Sstevel@tonic-gate 	priv_set_t *privset;
13820Sstevel@tonic-gate 	zone_state_t state;
13830Sstevel@tonic-gate 
13840Sstevel@tonic-gate 	if (getzoneid() != GLOBAL_ZONEID) {
13850Sstevel@tonic-gate 		zerror(gettext("must be in the global zone to %s a zone."),
13860Sstevel@tonic-gate 		    cmd_to_str(cmd_num));
13870Sstevel@tonic-gate 		return (Z_ERR);
13880Sstevel@tonic-gate 	}
13890Sstevel@tonic-gate 
13900Sstevel@tonic-gate 	if ((privset = priv_allocset()) == NULL) {
13910Sstevel@tonic-gate 		zerror(gettext("%s failed"), "priv_allocset");
13920Sstevel@tonic-gate 		return (Z_ERR);
13930Sstevel@tonic-gate 	}
13940Sstevel@tonic-gate 
13950Sstevel@tonic-gate 	if (getppriv(PRIV_EFFECTIVE, privset) != 0) {
13960Sstevel@tonic-gate 		zerror(gettext("%s failed"), "getppriv");
13970Sstevel@tonic-gate 		priv_freeset(privset);
13980Sstevel@tonic-gate 		return (Z_ERR);
13990Sstevel@tonic-gate 	}
14000Sstevel@tonic-gate 
14010Sstevel@tonic-gate 	if (priv_isfullset(privset) == B_FALSE) {
14020Sstevel@tonic-gate 		zerror(gettext("only a privileged user may %s a zone."),
14030Sstevel@tonic-gate 		    cmd_to_str(cmd_num));
14040Sstevel@tonic-gate 		priv_freeset(privset);
14050Sstevel@tonic-gate 		return (Z_ERR);
14060Sstevel@tonic-gate 	}
14070Sstevel@tonic-gate 	priv_freeset(privset);
14080Sstevel@tonic-gate 
14090Sstevel@tonic-gate 	if (zone == NULL) {
14100Sstevel@tonic-gate 		zerror(gettext("no zone specified"));
14110Sstevel@tonic-gate 		return (Z_ERR);
14120Sstevel@tonic-gate 	}
14130Sstevel@tonic-gate 
14140Sstevel@tonic-gate 	zent = lookup_running_zone(zone);
14150Sstevel@tonic-gate 
14160Sstevel@tonic-gate 	if (strcmp(zone, GLOBAL_ZONENAME) == 0) {
14170Sstevel@tonic-gate 		assert((zent != NULL) && (zent->zid == GLOBAL_ZONEID));
14180Sstevel@tonic-gate 		zerror(gettext("%s operation is invalid for the global zone."),
14190Sstevel@tonic-gate 		    cmd_to_str(cmd_num));
14200Sstevel@tonic-gate 		return (Z_ERR);
14210Sstevel@tonic-gate 	}
14220Sstevel@tonic-gate 
14230Sstevel@tonic-gate 	if (strncmp(zone, "SUNW", 4) == 0) {
14240Sstevel@tonic-gate 		zerror(gettext("%s operation is invalid for zones starting "
14250Sstevel@tonic-gate 		    "with SUNW."), cmd_to_str(cmd_num));
14260Sstevel@tonic-gate 		return (Z_ERR);
14270Sstevel@tonic-gate 	}
14280Sstevel@tonic-gate 
14290Sstevel@tonic-gate 	/*
14300Sstevel@tonic-gate 	 * Look up from the kernel for 'running' zones.
14310Sstevel@tonic-gate 	 */
14320Sstevel@tonic-gate 	if (running) {
14330Sstevel@tonic-gate 		if (zent == NULL) {
14340Sstevel@tonic-gate 			zerror(gettext("not running"));
14350Sstevel@tonic-gate 			return (Z_ERR);
14360Sstevel@tonic-gate 		}
14370Sstevel@tonic-gate 	} else {
14380Sstevel@tonic-gate 		int err;
14390Sstevel@tonic-gate 
14400Sstevel@tonic-gate 		if (unsafe_when_running && zent != NULL) {
14410Sstevel@tonic-gate 			/* check whether the zone is ready or running */
14420Sstevel@tonic-gate 			if ((err = zone_get_state(zent->zname,
14430Sstevel@tonic-gate 			    &zent->zstate_num)) != Z_OK) {
14440Sstevel@tonic-gate 				errno = err;
14450Sstevel@tonic-gate 				zperror2(zent->zname,
14460Sstevel@tonic-gate 				    gettext("could not get state"));
14470Sstevel@tonic-gate 				/* can't tell, so hedge */
14480Sstevel@tonic-gate 				zent->zstate_str = "ready/running";
14490Sstevel@tonic-gate 			} else {
14500Sstevel@tonic-gate 				zent->zstate_str =
14510Sstevel@tonic-gate 				    zone_state_str(zent->zstate_num);
14520Sstevel@tonic-gate 			}
14530Sstevel@tonic-gate 			zerror(gettext("%s operation is invalid for %s zones."),
14540Sstevel@tonic-gate 			    cmd_to_str(cmd_num), zent->zstate_str);
14550Sstevel@tonic-gate 			return (Z_ERR);
14560Sstevel@tonic-gate 		}
14570Sstevel@tonic-gate 		if ((err = zone_get_state(zone, &state)) != Z_OK) {
14580Sstevel@tonic-gate 			errno = err;
14590Sstevel@tonic-gate 			zperror2(zone, gettext("could not get state"));
14600Sstevel@tonic-gate 			return (Z_ERR);
14610Sstevel@tonic-gate 		}
14620Sstevel@tonic-gate 		switch (cmd_num) {
14630Sstevel@tonic-gate 		case CMD_UNINSTALL:
14640Sstevel@tonic-gate 			if (state == ZONE_STATE_CONFIGURED) {
14650Sstevel@tonic-gate 				zerror(gettext("is already in state '%s'."),
14660Sstevel@tonic-gate 				    zone_state_str(ZONE_STATE_CONFIGURED));
14670Sstevel@tonic-gate 				return (Z_ERR);
14680Sstevel@tonic-gate 			}
14690Sstevel@tonic-gate 			break;
14700Sstevel@tonic-gate 		case CMD_INSTALL:
14710Sstevel@tonic-gate 			if (state == ZONE_STATE_INSTALLED) {
14720Sstevel@tonic-gate 				zerror(gettext("is already %s."),
14730Sstevel@tonic-gate 				    zone_state_str(ZONE_STATE_INSTALLED));
14740Sstevel@tonic-gate 				return (Z_ERR);
14750Sstevel@tonic-gate 			} else if (state == ZONE_STATE_INCOMPLETE) {
14760Sstevel@tonic-gate 				zerror(gettext("zone is %s; %s required."),
14770Sstevel@tonic-gate 				    zone_state_str(ZONE_STATE_INCOMPLETE),
14780Sstevel@tonic-gate 				    cmd_to_str(CMD_UNINSTALL));
14790Sstevel@tonic-gate 				return (Z_ERR);
14800Sstevel@tonic-gate 			}
14810Sstevel@tonic-gate 			break;
14820Sstevel@tonic-gate 		case CMD_READY:
14830Sstevel@tonic-gate 		case CMD_BOOT:
14840Sstevel@tonic-gate 			if (state < ZONE_STATE_INSTALLED) {
14850Sstevel@tonic-gate 				zerror(gettext("must be %s before %s."),
14860Sstevel@tonic-gate 				    zone_state_str(ZONE_STATE_INSTALLED),
14870Sstevel@tonic-gate 				    cmd_to_str(cmd_num));
14880Sstevel@tonic-gate 				return (Z_ERR);
14890Sstevel@tonic-gate 			}
14900Sstevel@tonic-gate 			break;
14910Sstevel@tonic-gate 		case CMD_VERIFY:
14920Sstevel@tonic-gate 			if (state == ZONE_STATE_INCOMPLETE) {
14930Sstevel@tonic-gate 				zerror(gettext("zone is %s; %s required."),
14940Sstevel@tonic-gate 				    zone_state_str(ZONE_STATE_INCOMPLETE),
14950Sstevel@tonic-gate 				    cmd_to_str(CMD_UNINSTALL));
14960Sstevel@tonic-gate 				return (Z_ERR);
14970Sstevel@tonic-gate 			}
14980Sstevel@tonic-gate 			break;
14990Sstevel@tonic-gate 		}
15000Sstevel@tonic-gate 	}
15010Sstevel@tonic-gate 	return (Z_OK);
15020Sstevel@tonic-gate }
15030Sstevel@tonic-gate 
15040Sstevel@tonic-gate static int
15050Sstevel@tonic-gate halt_func(int argc, char *argv[])
15060Sstevel@tonic-gate {
15070Sstevel@tonic-gate 	zone_cmd_arg_t zarg;
15080Sstevel@tonic-gate 	int arg;
15090Sstevel@tonic-gate 
15100Sstevel@tonic-gate 	optind = 0;
15110Sstevel@tonic-gate 	if ((arg = getopt(argc, argv, "?")) != EOF) {
15120Sstevel@tonic-gate 		switch (arg) {
15130Sstevel@tonic-gate 		case '?':
15140Sstevel@tonic-gate 			sub_usage(SHELP_HALT, CMD_HALT);
15150Sstevel@tonic-gate 			return (optopt == '?' ? Z_OK : Z_USAGE);
15160Sstevel@tonic-gate 		default:
15170Sstevel@tonic-gate 			sub_usage(SHELP_HALT, CMD_HALT);
15180Sstevel@tonic-gate 			return (Z_USAGE);
15190Sstevel@tonic-gate 		}
15200Sstevel@tonic-gate 	}
15210Sstevel@tonic-gate 	if (argc > optind) {
15220Sstevel@tonic-gate 		sub_usage(SHELP_HALT, CMD_HALT);
15230Sstevel@tonic-gate 		return (Z_USAGE);
15240Sstevel@tonic-gate 	}
15250Sstevel@tonic-gate 	/*
15260Sstevel@tonic-gate 	 * zoneadmd should be the one to decide whether or not to proceed,
15270Sstevel@tonic-gate 	 * so even though it seems that the fourth parameter below should
15280Sstevel@tonic-gate 	 * perhaps be B_TRUE, it really shouldn't be.
15290Sstevel@tonic-gate 	 */
15300Sstevel@tonic-gate 	if (sanity_check(target_zone, CMD_HALT, B_FALSE, B_FALSE) != Z_OK)
15310Sstevel@tonic-gate 		return (Z_ERR);
15320Sstevel@tonic-gate 
15330Sstevel@tonic-gate 	zarg.cmd = Z_HALT;
15340Sstevel@tonic-gate 	return ((call_zoneadmd(target_zone, &zarg) == 0) ? Z_OK : Z_ERR);
15350Sstevel@tonic-gate }
15360Sstevel@tonic-gate 
15370Sstevel@tonic-gate static int
15380Sstevel@tonic-gate reboot_func(int argc, char *argv[])
15390Sstevel@tonic-gate {
15400Sstevel@tonic-gate 	zone_cmd_arg_t zarg;
15410Sstevel@tonic-gate 	int arg;
15420Sstevel@tonic-gate 
15430Sstevel@tonic-gate 	optind = 0;
15440Sstevel@tonic-gate 	if ((arg = getopt(argc, argv, "?")) != EOF) {
15450Sstevel@tonic-gate 		switch (arg) {
15460Sstevel@tonic-gate 		case '?':
15470Sstevel@tonic-gate 			sub_usage(SHELP_REBOOT, CMD_REBOOT);
15480Sstevel@tonic-gate 			return (optopt == '?' ? Z_OK : Z_USAGE);
15490Sstevel@tonic-gate 		default:
15500Sstevel@tonic-gate 			sub_usage(SHELP_REBOOT, CMD_REBOOT);
15510Sstevel@tonic-gate 			return (Z_USAGE);
15520Sstevel@tonic-gate 		}
15530Sstevel@tonic-gate 	}
15540Sstevel@tonic-gate 	if (argc > 0) {
15550Sstevel@tonic-gate 		sub_usage(SHELP_REBOOT, CMD_REBOOT);
15560Sstevel@tonic-gate 		return (Z_USAGE);
15570Sstevel@tonic-gate 	}
15580Sstevel@tonic-gate 	/*
15590Sstevel@tonic-gate 	 * zoneadmd should be the one to decide whether or not to proceed,
15600Sstevel@tonic-gate 	 * so even though it seems that the fourth parameter below should
15610Sstevel@tonic-gate 	 * perhaps be B_TRUE, it really shouldn't be.
15620Sstevel@tonic-gate 	 */
15630Sstevel@tonic-gate 	if (sanity_check(target_zone, CMD_REBOOT, B_TRUE, B_FALSE) != Z_OK)
15640Sstevel@tonic-gate 		return (Z_ERR);
15650Sstevel@tonic-gate 
15660Sstevel@tonic-gate 	zarg.cmd = Z_REBOOT;
15670Sstevel@tonic-gate 	return ((call_zoneadmd(target_zone, &zarg) == 0) ? Z_OK : Z_ERR);
15680Sstevel@tonic-gate }
15690Sstevel@tonic-gate 
15700Sstevel@tonic-gate static int
15710Sstevel@tonic-gate verify_rctls(zone_dochandle_t handle)
15720Sstevel@tonic-gate {
15730Sstevel@tonic-gate 	struct zone_rctltab rctltab;
15740Sstevel@tonic-gate 	size_t rbs = rctlblk_size();
15750Sstevel@tonic-gate 	rctlblk_t *rctlblk;
15760Sstevel@tonic-gate 	int error = Z_INVAL;
15770Sstevel@tonic-gate 
15780Sstevel@tonic-gate 	if ((rctlblk = malloc(rbs)) == NULL) {
15790Sstevel@tonic-gate 		zerror(gettext("failed to allocate %lu bytes: %s"), rbs,
15800Sstevel@tonic-gate 		    strerror(errno));
15810Sstevel@tonic-gate 		return (Z_NOMEM);
15820Sstevel@tonic-gate 	}
15830Sstevel@tonic-gate 
15840Sstevel@tonic-gate 	if (zonecfg_setrctlent(handle) != Z_OK) {
15850Sstevel@tonic-gate 		zerror(gettext("zonecfg_setrctlent failed"));
15860Sstevel@tonic-gate 		free(rctlblk);
15870Sstevel@tonic-gate 		return (error);
15880Sstevel@tonic-gate 	}
15890Sstevel@tonic-gate 
15900Sstevel@tonic-gate 	rctltab.zone_rctl_valptr = NULL;
15910Sstevel@tonic-gate 	while (zonecfg_getrctlent(handle, &rctltab) == Z_OK) {
15920Sstevel@tonic-gate 		struct zone_rctlvaltab *rctlval;
15930Sstevel@tonic-gate 		const char *name = rctltab.zone_rctl_name;
15940Sstevel@tonic-gate 
15950Sstevel@tonic-gate 		if (!zonecfg_is_rctl(name)) {
15960Sstevel@tonic-gate 			zerror(gettext("WARNING: Ignoring unrecognized rctl "
15970Sstevel@tonic-gate 			    "'%s'."),  name);
15980Sstevel@tonic-gate 			zonecfg_free_rctl_value_list(rctltab.zone_rctl_valptr);
15990Sstevel@tonic-gate 			rctltab.zone_rctl_valptr = NULL;
16000Sstevel@tonic-gate 			continue;
16010Sstevel@tonic-gate 		}
16020Sstevel@tonic-gate 
16030Sstevel@tonic-gate 		for (rctlval = rctltab.zone_rctl_valptr; rctlval != NULL;
16040Sstevel@tonic-gate 		    rctlval = rctlval->zone_rctlval_next) {
16050Sstevel@tonic-gate 			if (zonecfg_construct_rctlblk(rctlval, rctlblk)
16060Sstevel@tonic-gate 			    != Z_OK) {
16070Sstevel@tonic-gate 				zerror(gettext("invalid rctl value: "
16080Sstevel@tonic-gate 				    "(priv=%s,limit=%s,action%s)"),
16090Sstevel@tonic-gate 				    rctlval->zone_rctlval_priv,
16100Sstevel@tonic-gate 				    rctlval->zone_rctlval_limit,
16110Sstevel@tonic-gate 				    rctlval->zone_rctlval_action);
16120Sstevel@tonic-gate 				goto out;
16130Sstevel@tonic-gate 			}
16140Sstevel@tonic-gate 			if (!zonecfg_valid_rctl(name, rctlblk)) {
16150Sstevel@tonic-gate 				zerror(gettext("(priv=%s,limit=%s,action=%s) "
16160Sstevel@tonic-gate 				    "is not a valid value for rctl '%s'"),
16170Sstevel@tonic-gate 				    rctlval->zone_rctlval_priv,
16180Sstevel@tonic-gate 				    rctlval->zone_rctlval_limit,
16190Sstevel@tonic-gate 				    rctlval->zone_rctlval_action,
16200Sstevel@tonic-gate 				    name);
16210Sstevel@tonic-gate 				goto out;
16220Sstevel@tonic-gate 			}
16230Sstevel@tonic-gate 		}
16240Sstevel@tonic-gate 		zonecfg_free_rctl_value_list(rctltab.zone_rctl_valptr);
16250Sstevel@tonic-gate 	}
16260Sstevel@tonic-gate 	rctltab.zone_rctl_valptr = NULL;
16270Sstevel@tonic-gate 	error = Z_OK;
16280Sstevel@tonic-gate out:
16290Sstevel@tonic-gate 	zonecfg_free_rctl_value_list(rctltab.zone_rctl_valptr);
16300Sstevel@tonic-gate 	(void) zonecfg_endrctlent(handle);
16310Sstevel@tonic-gate 	free(rctlblk);
16320Sstevel@tonic-gate 	return (error);
16330Sstevel@tonic-gate }
16340Sstevel@tonic-gate 
16350Sstevel@tonic-gate static int
16360Sstevel@tonic-gate verify_pool(zone_dochandle_t handle)
16370Sstevel@tonic-gate {
16380Sstevel@tonic-gate 	char poolname[MAXPATHLEN];
16390Sstevel@tonic-gate 	pool_conf_t *poolconf;
16400Sstevel@tonic-gate 	pool_t *pool;
16410Sstevel@tonic-gate 	int status;
16420Sstevel@tonic-gate 	int error;
16430Sstevel@tonic-gate 
16440Sstevel@tonic-gate 	/*
16450Sstevel@tonic-gate 	 * This ends up being very similar to the check done in zoneadmd.
16460Sstevel@tonic-gate 	 */
16470Sstevel@tonic-gate 	error = zonecfg_get_pool(handle, poolname, sizeof (poolname));
16480Sstevel@tonic-gate 	if (error == Z_NO_ENTRY || (error == Z_OK && strlen(poolname) == 0)) {
16490Sstevel@tonic-gate 		/*
16500Sstevel@tonic-gate 		 * No pool specified.
16510Sstevel@tonic-gate 		 */
16520Sstevel@tonic-gate 		return (0);
16530Sstevel@tonic-gate 	}
16540Sstevel@tonic-gate 	if (error != Z_OK) {
16550Sstevel@tonic-gate 		zperror(gettext("Unable to retrieve pool name from "
16560Sstevel@tonic-gate 		    "configuration"), B_TRUE);
16570Sstevel@tonic-gate 		return (error);
16580Sstevel@tonic-gate 	}
16590Sstevel@tonic-gate 	/*
16600Sstevel@tonic-gate 	 * Don't do anything if pools aren't enabled.
16610Sstevel@tonic-gate 	 */
16620Sstevel@tonic-gate 	if (pool_get_status(&status) != PO_SUCCESS || status != POOL_ENABLED) {
16630Sstevel@tonic-gate 		zerror(gettext("WARNING: pools facility not active; "
16640Sstevel@tonic-gate 		    "zone will not be bound to pool '%s'."), poolname);
16650Sstevel@tonic-gate 		return (Z_OK);
16660Sstevel@tonic-gate 	}
16670Sstevel@tonic-gate 	/*
16680Sstevel@tonic-gate 	 * Try to provide a sane error message if the requested pool doesn't
16690Sstevel@tonic-gate 	 * exist.  It isn't clear that pools-related failures should
16700Sstevel@tonic-gate 	 * necessarily translate to a failure to verify the zone configuration,
16710Sstevel@tonic-gate 	 * hence they are not considered errors.
16720Sstevel@tonic-gate 	 */
16730Sstevel@tonic-gate 	if ((poolconf = pool_conf_alloc()) == NULL) {
16740Sstevel@tonic-gate 		zerror(gettext("WARNING: pool_conf_alloc failed; "
16750Sstevel@tonic-gate 		    "using default pool"));
16760Sstevel@tonic-gate 		return (Z_OK);
16770Sstevel@tonic-gate 	}
16780Sstevel@tonic-gate 	if (pool_conf_open(poolconf, pool_dynamic_location(), PO_RDONLY) !=
16790Sstevel@tonic-gate 	    PO_SUCCESS) {
16800Sstevel@tonic-gate 		zerror(gettext("WARNING: pool_conf_open failed; "
16810Sstevel@tonic-gate 		    "using default pool"));
16820Sstevel@tonic-gate 		pool_conf_free(poolconf);
16830Sstevel@tonic-gate 		return (Z_OK);
16840Sstevel@tonic-gate 	}
16850Sstevel@tonic-gate 	pool = pool_get_pool(poolconf, poolname);
16860Sstevel@tonic-gate 	(void) pool_conf_close(poolconf);
16870Sstevel@tonic-gate 	pool_conf_free(poolconf);
16880Sstevel@tonic-gate 	if (pool == NULL) {
16890Sstevel@tonic-gate 		zerror(gettext("WARNING: pool '%s' not found. "
16900Sstevel@tonic-gate 		    "using default pool"), poolname);
16910Sstevel@tonic-gate 	}
16920Sstevel@tonic-gate 
16930Sstevel@tonic-gate 	return (Z_OK);
16940Sstevel@tonic-gate }
16950Sstevel@tonic-gate 
16960Sstevel@tonic-gate static int
16970Sstevel@tonic-gate verify_filesystems(zone_dochandle_t handle)
16980Sstevel@tonic-gate {
16990Sstevel@tonic-gate 	int return_code = Z_OK;
17000Sstevel@tonic-gate 	struct zone_fstab fstab;
17010Sstevel@tonic-gate 	char cmdbuf[MAXPATHLEN];
17020Sstevel@tonic-gate 	struct stat st;
17030Sstevel@tonic-gate 
17040Sstevel@tonic-gate 	/*
17050Sstevel@tonic-gate 	 * No need to verify inherit-pkg-dir fs types, as their type is
17060Sstevel@tonic-gate 	 * implicitly lofs, which is known.  Therefore, the types are only
17070Sstevel@tonic-gate 	 * verified for regular filesystems below.
17080Sstevel@tonic-gate 	 *
17090Sstevel@tonic-gate 	 * Since the actual mount point is not known until the dependent mounts
17100Sstevel@tonic-gate 	 * are performed, we don't attempt any path validation here: that will
17110Sstevel@tonic-gate 	 * happen later when zoneadmd actually does the mounts.
17120Sstevel@tonic-gate 	 */
17130Sstevel@tonic-gate 	if (zonecfg_setfsent(handle) != Z_OK) {
17140Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("cannot verify file-systems: "
17150Sstevel@tonic-gate 		    "unable to enumerate mounts\n"));
17160Sstevel@tonic-gate 		return (Z_ERR);
17170Sstevel@tonic-gate 	}
17180Sstevel@tonic-gate 	while (zonecfg_getfsent(handle, &fstab) == Z_OK) {
17190Sstevel@tonic-gate 		if (!zonecfg_valid_fs_type(fstab.zone_fs_type)) {
17200Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("cannot verify fs %s: "
17210Sstevel@tonic-gate 			    "type %s is not allowed.\n"), fstab.zone_fs_dir,
17220Sstevel@tonic-gate 			    fstab.zone_fs_type);
17230Sstevel@tonic-gate 			return_code = Z_ERR;
17240Sstevel@tonic-gate 			goto next_fs;
17250Sstevel@tonic-gate 		}
17260Sstevel@tonic-gate 		/*
17270Sstevel@tonic-gate 		 * Verify /usr/lib/fs/<fstype>/mount exists.
17280Sstevel@tonic-gate 		 */
17290Sstevel@tonic-gate 		if (snprintf(cmdbuf, sizeof (cmdbuf), "/usr/lib/fs/%s/mount",
17300Sstevel@tonic-gate 		    fstab.zone_fs_type) > sizeof (cmdbuf)) {
17310Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("cannot verify fs %s: "
17320Sstevel@tonic-gate 			    "type %s is too long.\n"), fstab.zone_fs_dir,
17330Sstevel@tonic-gate 			    fstab.zone_fs_type);
17340Sstevel@tonic-gate 			return_code = Z_ERR;
17350Sstevel@tonic-gate 			goto next_fs;
17360Sstevel@tonic-gate 		}
17370Sstevel@tonic-gate 		if (stat(cmdbuf, &st) != 0) {
17380Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("cannot verify fs %s: "
17390Sstevel@tonic-gate 			    "can't access %s: %s\n"), fstab.zone_fs_dir,
17400Sstevel@tonic-gate 			    cmdbuf, strerror(errno));
17410Sstevel@tonic-gate 			return_code = Z_ERR;
17420Sstevel@tonic-gate 			goto next_fs;
17430Sstevel@tonic-gate 		}
17440Sstevel@tonic-gate 		if (!S_ISREG(st.st_mode)) {
17450Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("cannot verify fs %s: "
17460Sstevel@tonic-gate 			    "%s is not a regular file\n"), fstab.zone_fs_dir,
17470Sstevel@tonic-gate 			    cmdbuf);
17480Sstevel@tonic-gate 			return_code = Z_ERR;
17490Sstevel@tonic-gate 			goto next_fs;
17500Sstevel@tonic-gate 		}
17510Sstevel@tonic-gate 		/*
17520Sstevel@tonic-gate 		 * Verify /usr/lib/fs/<fstype>/fsck exists iff zone_fs_raw is
17530Sstevel@tonic-gate 		 * set.
17540Sstevel@tonic-gate 		 */
17550Sstevel@tonic-gate 		if (snprintf(cmdbuf, sizeof (cmdbuf), "/usr/lib/fs/%s/fsck",
17560Sstevel@tonic-gate 		    fstab.zone_fs_type) > sizeof (cmdbuf)) {
17570Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("cannot verify fs %s: "
17580Sstevel@tonic-gate 			    "type %s is too long.\n"), fstab.zone_fs_dir,
17590Sstevel@tonic-gate 			    fstab.zone_fs_type);
17600Sstevel@tonic-gate 			return_code = Z_ERR;
17610Sstevel@tonic-gate 			goto next_fs;
17620Sstevel@tonic-gate 		}
17630Sstevel@tonic-gate 		if (fstab.zone_fs_raw[0] == '\0' && stat(cmdbuf, &st) == 0) {
17640Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("cannot verify fs %s: "
17650Sstevel@tonic-gate 			    "must specify 'raw' device for %s file-systems\n"),
17660Sstevel@tonic-gate 			    fstab.zone_fs_dir, fstab.zone_fs_type);
17670Sstevel@tonic-gate 			return_code = Z_ERR;
17680Sstevel@tonic-gate 			goto next_fs;
17690Sstevel@tonic-gate 		}
17700Sstevel@tonic-gate 		if (fstab.zone_fs_raw[0] != '\0' &&
17710Sstevel@tonic-gate 		    (stat(cmdbuf, &st) != 0 || !S_ISREG(st.st_mode))) {
17720Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("cannot verify fs %s: "
17730Sstevel@tonic-gate 			    "'raw' device specified but "
17740Sstevel@tonic-gate 			    "no fsck executable exists for %s\n"),
17750Sstevel@tonic-gate 			    fstab.zone_fs_dir, fstab.zone_fs_type);
17760Sstevel@tonic-gate 			return_code = Z_ERR;
17770Sstevel@tonic-gate 			goto next_fs;
17780Sstevel@tonic-gate 		}
17790Sstevel@tonic-gate next_fs:
17800Sstevel@tonic-gate 		zonecfg_free_fs_option_list(fstab.zone_fs_options);
17810Sstevel@tonic-gate 	}
17820Sstevel@tonic-gate 	(void) zonecfg_endfsent(handle);
17830Sstevel@tonic-gate 
17840Sstevel@tonic-gate 	return (return_code);
17850Sstevel@tonic-gate }
17860Sstevel@tonic-gate 
17870Sstevel@tonic-gate static int
17880Sstevel@tonic-gate verify_details(int cmd_num)
17890Sstevel@tonic-gate {
17900Sstevel@tonic-gate 	zone_dochandle_t handle;
17910Sstevel@tonic-gate 	struct zone_nwiftab nwiftab;
17920Sstevel@tonic-gate 	char zonepath[MAXPATHLEN], checkpath[MAXPATHLEN];
17930Sstevel@tonic-gate 	int return_code = Z_OK;
17940Sstevel@tonic-gate 	int err;
17950Sstevel@tonic-gate 
17960Sstevel@tonic-gate 	if ((handle = zonecfg_init_handle()) == NULL) {
17970Sstevel@tonic-gate 		zperror(cmd_to_str(cmd_num), B_TRUE);
17980Sstevel@tonic-gate 		return (Z_ERR);
17990Sstevel@tonic-gate 	}
18000Sstevel@tonic-gate 	if ((err = zonecfg_get_handle(target_zone, handle)) != Z_OK) {
18010Sstevel@tonic-gate 		errno = err;
18020Sstevel@tonic-gate 		zperror(cmd_to_str(cmd_num), B_TRUE);
18030Sstevel@tonic-gate 		zonecfg_fini_handle(handle);
18040Sstevel@tonic-gate 		return (Z_ERR);
18050Sstevel@tonic-gate 	}
18060Sstevel@tonic-gate 	if ((err = zonecfg_get_zonepath(handle, zonepath, sizeof (zonepath))) !=
18070Sstevel@tonic-gate 	    Z_OK) {
18080Sstevel@tonic-gate 		errno = err;
18090Sstevel@tonic-gate 		zperror(cmd_to_str(cmd_num), B_TRUE);
18100Sstevel@tonic-gate 		zonecfg_fini_handle(handle);
18110Sstevel@tonic-gate 		return (Z_ERR);
18120Sstevel@tonic-gate 	}
18130Sstevel@tonic-gate 	/*
18140Sstevel@tonic-gate 	 * zonecfg_get_zonepath() gets its data from the XML repository.
18150Sstevel@tonic-gate 	 * Verify this against the index file, which is checked first by
18160Sstevel@tonic-gate 	 * zone_get_zonepath().  If they don't match, bail out.
18170Sstevel@tonic-gate 	 */
18180Sstevel@tonic-gate 	if ((err = zone_get_zonepath(target_zone, checkpath,
18190Sstevel@tonic-gate 	    sizeof (checkpath))) != Z_OK) {
18200Sstevel@tonic-gate 		errno = err;
18210Sstevel@tonic-gate 		zperror2(target_zone, gettext("could not get zone path"));
18220Sstevel@tonic-gate 		return (Z_ERR);
18230Sstevel@tonic-gate 	}
18240Sstevel@tonic-gate 	if (strcmp(zonepath, checkpath) != 0) {
18250Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("The XML repository has "
18260Sstevel@tonic-gate 		    "zonepath '%s',\nbut the index file has zonepath '%s'.\n"
18270Sstevel@tonic-gate 		    "These must match, so fix the incorrect entry.\n"),
18280Sstevel@tonic-gate 		    zonepath, checkpath);
18290Sstevel@tonic-gate 		return (Z_ERR);
18300Sstevel@tonic-gate 	}
18310Sstevel@tonic-gate 	if (validate_zonepath(zonepath, cmd_num) != Z_OK) {
18320Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("could not verify zonepath %s "
18330Sstevel@tonic-gate 		    "because of the above errors.\n"), zonepath);
18340Sstevel@tonic-gate 		return_code = Z_ERR;
18350Sstevel@tonic-gate 	}
18360Sstevel@tonic-gate 
18370Sstevel@tonic-gate 	if ((err = zonecfg_setnwifent(handle)) != Z_OK) {
18380Sstevel@tonic-gate 		errno = err;
18390Sstevel@tonic-gate 		zperror(cmd_to_str(cmd_num), B_TRUE);
18400Sstevel@tonic-gate 		zonecfg_fini_handle(handle);
18410Sstevel@tonic-gate 		return (Z_ERR);
18420Sstevel@tonic-gate 	}
18430Sstevel@tonic-gate 	while (zonecfg_getnwifent(handle, &nwiftab) == Z_OK) {
18440Sstevel@tonic-gate 		struct lifreq lifr;
18450Sstevel@tonic-gate 		sa_family_t af;
18460Sstevel@tonic-gate 		int so, res;
18470Sstevel@tonic-gate 
18480Sstevel@tonic-gate 		/* skip any loopback interfaces */
18490Sstevel@tonic-gate 		if (strcmp(nwiftab.zone_nwif_physical, "lo0") == 0)
18500Sstevel@tonic-gate 			continue;
18510Sstevel@tonic-gate 		if ((res = zonecfg_valid_net_address(nwiftab.zone_nwif_address,
18520Sstevel@tonic-gate 		    &lifr)) != Z_OK) {
18530Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("could not verify %s "
18540Sstevel@tonic-gate 			    "%s=%s %s=%s: %s\n"), "net", "address",
18550Sstevel@tonic-gate 			    nwiftab.zone_nwif_address, "physical",
18560Sstevel@tonic-gate 			    nwiftab.zone_nwif_physical, zonecfg_strerror(res));
18570Sstevel@tonic-gate 			return_code = Z_ERR;
18580Sstevel@tonic-gate 			continue;
18590Sstevel@tonic-gate 		}
18600Sstevel@tonic-gate 		af = lifr.lifr_addr.ss_family;
18610Sstevel@tonic-gate 		(void) memset(&lifr, 0, sizeof (lifr));
18620Sstevel@tonic-gate 		(void) strlcpy(lifr.lifr_name, nwiftab.zone_nwif_physical,
18630Sstevel@tonic-gate 		    sizeof (lifr.lifr_name));
18640Sstevel@tonic-gate 		lifr.lifr_addr.ss_family = af;
18650Sstevel@tonic-gate 		if ((so = socket(af, SOCK_DGRAM, 0)) < 0) {
18660Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("could not verify %s "
18670Sstevel@tonic-gate 			    "%s=%s %s=%s: could not get socket: %s\n"), "net",
18680Sstevel@tonic-gate 			    "address", nwiftab.zone_nwif_address, "physical",
18690Sstevel@tonic-gate 			    nwiftab.zone_nwif_physical, strerror(errno));
18700Sstevel@tonic-gate 			return_code = Z_ERR;
18710Sstevel@tonic-gate 			continue;
18720Sstevel@tonic-gate 		}
18730Sstevel@tonic-gate 		if (ioctl(so, SIOCGLIFFLAGS, (caddr_t)&lifr) < 0) {
18740Sstevel@tonic-gate 			(void) fprintf(stderr,
18750Sstevel@tonic-gate 			    gettext("could not verify %s %s=%s %s=%s: %s\n"),
18760Sstevel@tonic-gate 			    "net", "address", nwiftab.zone_nwif_address,
18770Sstevel@tonic-gate 			    "physical", nwiftab.zone_nwif_physical,
18780Sstevel@tonic-gate 			    strerror(errno));
18790Sstevel@tonic-gate 			return_code = Z_ERR;
18800Sstevel@tonic-gate 		}
18810Sstevel@tonic-gate 		(void) close(so);
18820Sstevel@tonic-gate 	}
18830Sstevel@tonic-gate 	(void) zonecfg_endnwifent(handle);
18840Sstevel@tonic-gate 
18850Sstevel@tonic-gate 	if (verify_filesystems(handle) != Z_OK)
18860Sstevel@tonic-gate 		return_code = Z_ERR;
18870Sstevel@tonic-gate 	if (verify_rctls(handle) != Z_OK)
18880Sstevel@tonic-gate 		return_code = Z_ERR;
18890Sstevel@tonic-gate 	if (verify_pool(handle) != Z_OK)
18900Sstevel@tonic-gate 		return_code = Z_ERR;
18910Sstevel@tonic-gate 	zonecfg_fini_handle(handle);
18920Sstevel@tonic-gate 	if (return_code == Z_ERR)
18930Sstevel@tonic-gate 		(void) fprintf(stderr,
18940Sstevel@tonic-gate 		    gettext("%s: zone %s failed to verify\n"),
18950Sstevel@tonic-gate 		    execname, target_zone);
18960Sstevel@tonic-gate 	return (return_code);
18970Sstevel@tonic-gate }
18980Sstevel@tonic-gate 
18990Sstevel@tonic-gate static int
19000Sstevel@tonic-gate verify_func(int argc, char *argv[])
19010Sstevel@tonic-gate {
19020Sstevel@tonic-gate 	int arg;
19030Sstevel@tonic-gate 
19040Sstevel@tonic-gate 	optind = 0;
19050Sstevel@tonic-gate 	if ((arg = getopt(argc, argv, "?")) != EOF) {
19060Sstevel@tonic-gate 		switch (arg) {
19070Sstevel@tonic-gate 		case '?':
19080Sstevel@tonic-gate 			sub_usage(SHELP_VERIFY, CMD_VERIFY);
19090Sstevel@tonic-gate 			return (optopt == '?' ? Z_OK : Z_USAGE);
19100Sstevel@tonic-gate 		default:
19110Sstevel@tonic-gate 			sub_usage(SHELP_VERIFY, CMD_VERIFY);
19120Sstevel@tonic-gate 			return (Z_USAGE);
19130Sstevel@tonic-gate 		}
19140Sstevel@tonic-gate 	}
19150Sstevel@tonic-gate 	if (argc > optind) {
19160Sstevel@tonic-gate 		sub_usage(SHELP_VERIFY, CMD_VERIFY);
19170Sstevel@tonic-gate 		return (Z_USAGE);
19180Sstevel@tonic-gate 	}
19190Sstevel@tonic-gate 	if (sanity_check(target_zone, CMD_VERIFY, B_FALSE, B_FALSE) != Z_OK)
19200Sstevel@tonic-gate 		return (Z_ERR);
19210Sstevel@tonic-gate 	return (verify_details(CMD_VERIFY));
19220Sstevel@tonic-gate }
19230Sstevel@tonic-gate 
19240Sstevel@tonic-gate #define	LUCREATEZONE	"/usr/lib/lu/lucreatezone"
19250Sstevel@tonic-gate 
19260Sstevel@tonic-gate static int
19270Sstevel@tonic-gate install_func(int argc, char *argv[])
19280Sstevel@tonic-gate {
19290Sstevel@tonic-gate 	/* 9: "exec " and " -z " */
19300Sstevel@tonic-gate 	char cmdbuf[sizeof (LUCREATEZONE) + ZONENAME_MAX + 9];
19310Sstevel@tonic-gate 	int lockfd;
19320Sstevel@tonic-gate 	int err, arg;
19330Sstevel@tonic-gate 	char zonepath[MAXPATHLEN];
19340Sstevel@tonic-gate 	int status;
19350Sstevel@tonic-gate 
19360Sstevel@tonic-gate 	optind = 0;
19370Sstevel@tonic-gate 	if ((arg = getopt(argc, argv, "?")) != EOF) {
19380Sstevel@tonic-gate 		switch (arg) {
19390Sstevel@tonic-gate 		case '?':
19400Sstevel@tonic-gate 			sub_usage(SHELP_INSTALL, CMD_INSTALL);
19410Sstevel@tonic-gate 			return (optopt == '?' ? Z_OK : Z_USAGE);
19420Sstevel@tonic-gate 		default:
19430Sstevel@tonic-gate 			sub_usage(SHELP_INSTALL, CMD_INSTALL);
19440Sstevel@tonic-gate 			return (Z_USAGE);
19450Sstevel@tonic-gate 		}
19460Sstevel@tonic-gate 	}
19470Sstevel@tonic-gate 	if (argc > optind) {
19480Sstevel@tonic-gate 		sub_usage(SHELP_INSTALL, CMD_INSTALL);
19490Sstevel@tonic-gate 		return (Z_USAGE);
19500Sstevel@tonic-gate 	}
19510Sstevel@tonic-gate 	if (sanity_check(target_zone, CMD_INSTALL, B_FALSE, B_TRUE) != Z_OK)
19520Sstevel@tonic-gate 		return (Z_ERR);
19530Sstevel@tonic-gate 	if (verify_details(CMD_INSTALL) != Z_OK)
19540Sstevel@tonic-gate 		return (Z_ERR);
19550Sstevel@tonic-gate 
19560Sstevel@tonic-gate 	if (grab_lock_file(target_zone, &lockfd) != Z_OK) {
19570Sstevel@tonic-gate 		zerror(gettext("another %s may have an operation in progress."),
19580Sstevel@tonic-gate 		    "zoneadmd");
19590Sstevel@tonic-gate 		return (Z_ERR);
19600Sstevel@tonic-gate 	}
19610Sstevel@tonic-gate 	err = zone_set_state(target_zone, ZONE_STATE_INCOMPLETE);
19620Sstevel@tonic-gate 	if (err != Z_OK) {
19630Sstevel@tonic-gate 		errno = err;
19640Sstevel@tonic-gate 		zperror2(target_zone, gettext("could not set state"));
19650Sstevel@tonic-gate 		goto done;
19660Sstevel@tonic-gate 	}
19670Sstevel@tonic-gate 
19680Sstevel@tonic-gate 	/*
19690Sstevel@tonic-gate 	 * According to the Application Packaging Developer's Guide, a
19700Sstevel@tonic-gate 	 * "checkinstall" script when included in a package is executed as
19710Sstevel@tonic-gate 	 * the user "install", if such a user exists, or by the user
19720Sstevel@tonic-gate 	 * "nobody".  In order to support this dubious behavior, the path
19730Sstevel@tonic-gate 	 * to the zone being constructed is opened up during the life of
19740Sstevel@tonic-gate 	 * the command laying down the zone's root file system.  Once this
19750Sstevel@tonic-gate 	 * has completed, regardless of whether it was successful, the
19760Sstevel@tonic-gate 	 * path to the zone is again restricted.
19770Sstevel@tonic-gate 	 */
19780Sstevel@tonic-gate 	if ((err = zone_get_zonepath(target_zone, zonepath,
19790Sstevel@tonic-gate 	    sizeof (zonepath))) != Z_OK) {
19800Sstevel@tonic-gate 		errno = err;
19810Sstevel@tonic-gate 		zperror2(target_zone, gettext("could not get zone path"));
19820Sstevel@tonic-gate 		goto done;
19830Sstevel@tonic-gate 	}
19840Sstevel@tonic-gate 	if (chmod(zonepath, DEFAULT_DIR_MODE) != 0) {
19850Sstevel@tonic-gate 		zperror(zonepath, B_FALSE);
19860Sstevel@tonic-gate 		err = Z_ERR;
19870Sstevel@tonic-gate 		goto done;
19880Sstevel@tonic-gate 	}
19890Sstevel@tonic-gate 
19900Sstevel@tonic-gate 	/*
19910Sstevel@tonic-gate 	 * "exec" the command so that the returned status is that of
19920Sstevel@tonic-gate 	 * LUCREATEZONE and not the shell.
19930Sstevel@tonic-gate 	 */
19940Sstevel@tonic-gate 	(void) snprintf(cmdbuf, sizeof (cmdbuf), "exec " LUCREATEZONE " -z %s",
19950Sstevel@tonic-gate 	    target_zone);
19960Sstevel@tonic-gate 	status = do_subproc(cmdbuf);
19970Sstevel@tonic-gate 	if (chmod(zonepath, S_IRWXU) != 0) {
19980Sstevel@tonic-gate 		zperror(zonepath, B_FALSE);
19990Sstevel@tonic-gate 		err = Z_ERR;
20000Sstevel@tonic-gate 		goto done;
20010Sstevel@tonic-gate 	}
20020Sstevel@tonic-gate 	if ((err = subproc_status(LUCREATEZONE, status)) != Z_OK)
20030Sstevel@tonic-gate 		goto done;
20040Sstevel@tonic-gate 
20050Sstevel@tonic-gate 	if ((err = zone_set_state(target_zone, ZONE_STATE_INSTALLED)) != Z_OK) {
20060Sstevel@tonic-gate 		errno = err;
20070Sstevel@tonic-gate 		zperror2(target_zone, gettext("could not set state"));
20080Sstevel@tonic-gate 		goto done;
20090Sstevel@tonic-gate 	}
20100Sstevel@tonic-gate 
20110Sstevel@tonic-gate done:
20120Sstevel@tonic-gate 	release_lock_file(lockfd);
20130Sstevel@tonic-gate 	return ((err == Z_OK) ? Z_OK : Z_ERR);
20140Sstevel@tonic-gate }
20150Sstevel@tonic-gate 
20160Sstevel@tonic-gate /*
20170Sstevel@tonic-gate  * On input, TRUE => yes, FALSE => no.
20180Sstevel@tonic-gate  * On return, TRUE => 1, FALSE => 0, could not ask => -1.
20190Sstevel@tonic-gate  */
20200Sstevel@tonic-gate 
20210Sstevel@tonic-gate static int
20220Sstevel@tonic-gate ask_yesno(boolean_t default_answer, const char *question)
20230Sstevel@tonic-gate {
20240Sstevel@tonic-gate 	char line[64];	/* should be large enough to answer yes or no */
20250Sstevel@tonic-gate 
20260Sstevel@tonic-gate 	if (!isatty(STDIN_FILENO))
20270Sstevel@tonic-gate 		return (-1);
20280Sstevel@tonic-gate 	for (;;) {
20290Sstevel@tonic-gate 		(void) printf("%s (%s)? ", question,
20300Sstevel@tonic-gate 		    default_answer ? "[y]/n" : "y/[n]");
20310Sstevel@tonic-gate 		if (fgets(line, sizeof (line), stdin) == NULL ||
20320Sstevel@tonic-gate 		    line[0] == '\n')
20330Sstevel@tonic-gate 			return (default_answer ? 1 : 0);
20340Sstevel@tonic-gate 		if (tolower(line[0]) == 'y')
20350Sstevel@tonic-gate 			return (1);
20360Sstevel@tonic-gate 		if (tolower(line[0]) == 'n')
20370Sstevel@tonic-gate 			return (0);
20380Sstevel@tonic-gate 	}
20390Sstevel@tonic-gate }
20400Sstevel@tonic-gate 
20410Sstevel@tonic-gate #define	RMCOMMAND	"/usr/bin/rm -rf"
20420Sstevel@tonic-gate 
20430Sstevel@tonic-gate /* ARGSUSED */
20440Sstevel@tonic-gate int
20450Sstevel@tonic-gate zfm_print(const char *p, void *r) {
20460Sstevel@tonic-gate 	zerror("  %s\n", p);
20470Sstevel@tonic-gate 	return (0);
20480Sstevel@tonic-gate }
20490Sstevel@tonic-gate 
20500Sstevel@tonic-gate static int
20510Sstevel@tonic-gate uninstall_func(int argc, char *argv[])
20520Sstevel@tonic-gate {
20530Sstevel@tonic-gate 	/* 6: "exec " and " " */
20540Sstevel@tonic-gate 	char cmdbuf[sizeof (RMCOMMAND) + MAXPATHLEN + 6];
20550Sstevel@tonic-gate 	char line[ZONENAME_MAX + 128];	/* Enough for "Are you sure ..." */
20560Sstevel@tonic-gate 	char rootpath[MAXPATHLEN], devpath[MAXPATHLEN];
20570Sstevel@tonic-gate 	boolean_t force = B_FALSE;
20580Sstevel@tonic-gate 	int lockfd, answer;
20590Sstevel@tonic-gate 	int err, arg;
20600Sstevel@tonic-gate 	int status;
20610Sstevel@tonic-gate 
20620Sstevel@tonic-gate 	optind = 0;
20630Sstevel@tonic-gate 	while ((arg = getopt(argc, argv, "?F")) != EOF) {
20640Sstevel@tonic-gate 		switch (arg) {
20650Sstevel@tonic-gate 		case '?':
20660Sstevel@tonic-gate 			sub_usage(SHELP_UNINSTALL, CMD_UNINSTALL);
20670Sstevel@tonic-gate 			return (optopt == '?' ? Z_OK : Z_USAGE);
20680Sstevel@tonic-gate 		case 'F':
20690Sstevel@tonic-gate 			force = B_TRUE;
20700Sstevel@tonic-gate 			break;
20710Sstevel@tonic-gate 		default:
20720Sstevel@tonic-gate 			sub_usage(SHELP_UNINSTALL, CMD_UNINSTALL);
20730Sstevel@tonic-gate 			return (Z_USAGE);
20740Sstevel@tonic-gate 		}
20750Sstevel@tonic-gate 	}
20760Sstevel@tonic-gate 	if (argc > optind) {
20770Sstevel@tonic-gate 		sub_usage(SHELP_UNINSTALL, CMD_UNINSTALL);
20780Sstevel@tonic-gate 		return (Z_USAGE);
20790Sstevel@tonic-gate 	}
20800Sstevel@tonic-gate 
20810Sstevel@tonic-gate 	if (sanity_check(target_zone, CMD_UNINSTALL, B_FALSE, B_TRUE) != Z_OK)
20820Sstevel@tonic-gate 		return (Z_ERR);
20830Sstevel@tonic-gate 
20840Sstevel@tonic-gate 	if (!force) {
20850Sstevel@tonic-gate 		(void) snprintf(line, sizeof (line),
20860Sstevel@tonic-gate 		    gettext("Are you sure you want to %s zone %s"),
20870Sstevel@tonic-gate 		    cmd_to_str(CMD_UNINSTALL), target_zone);
20880Sstevel@tonic-gate 		if ((answer = ask_yesno(B_FALSE, line)) == 0) {
20890Sstevel@tonic-gate 			return (Z_OK);
20900Sstevel@tonic-gate 		} else if (answer == -1) {
20910Sstevel@tonic-gate 			zerror(gettext("Input not from terminal and -F "
20920Sstevel@tonic-gate 			    "not specified: %s not done."),
20930Sstevel@tonic-gate 			    cmd_to_str(CMD_UNINSTALL));
20940Sstevel@tonic-gate 			return (Z_ERR);
20950Sstevel@tonic-gate 		}
20960Sstevel@tonic-gate 	}
20970Sstevel@tonic-gate 
20980Sstevel@tonic-gate 	if ((err = zone_get_zonepath(target_zone, devpath,
20990Sstevel@tonic-gate 	    sizeof (devpath))) != Z_OK) {
21000Sstevel@tonic-gate 		errno = err;
21010Sstevel@tonic-gate 		zperror2(target_zone, gettext("could not get zone path"));
21020Sstevel@tonic-gate 		return (Z_ERR);
21030Sstevel@tonic-gate 	}
21040Sstevel@tonic-gate 	(void) strlcat(devpath, "/dev", sizeof (devpath));
21050Sstevel@tonic-gate 	if ((err = zone_get_rootpath(target_zone, rootpath,
21060Sstevel@tonic-gate 	    sizeof (rootpath))) != Z_OK) {
21070Sstevel@tonic-gate 		errno = err;
21080Sstevel@tonic-gate 		zperror2(target_zone, gettext("could not get root path"));
21090Sstevel@tonic-gate 		return (Z_ERR);
21100Sstevel@tonic-gate 	}
21110Sstevel@tonic-gate 
21120Sstevel@tonic-gate 	/*
21130Sstevel@tonic-gate 	 * If there seems to be a zoneadmd running for this zone, call it
21140Sstevel@tonic-gate 	 * to tell it that an uninstall is happening; if all goes well it
21150Sstevel@tonic-gate 	 * will then shut itself down.
21160Sstevel@tonic-gate 	 */
21170Sstevel@tonic-gate 	if (ping_zoneadmd(target_zone) == Z_OK) {
21180Sstevel@tonic-gate 		zone_cmd_arg_t zarg;
21190Sstevel@tonic-gate 		zarg.cmd = Z_NOTE_UNINSTALLING;
21200Sstevel@tonic-gate 		/* we don't care too much if this fails... just plow on */
21210Sstevel@tonic-gate 		(void) call_zoneadmd(target_zone, &zarg);
21220Sstevel@tonic-gate 	}
21230Sstevel@tonic-gate 
21240Sstevel@tonic-gate 	if (grab_lock_file(target_zone, &lockfd) != Z_OK) {
21250Sstevel@tonic-gate 		zerror(gettext("another %s may have an operation in progress."),
21260Sstevel@tonic-gate 		    "zoneadmd");
21270Sstevel@tonic-gate 		return (Z_ERR);
21280Sstevel@tonic-gate 	}
21290Sstevel@tonic-gate 
21300Sstevel@tonic-gate 	/* Don't uninstall the zone if anything is mounted there */
21310Sstevel@tonic-gate 	err = zonecfg_find_mounts(rootpath, NULL, NULL);
21320Sstevel@tonic-gate 	if (err) {
21330Sstevel@tonic-gate 		zerror(gettext("These file-systems are mounted on "
21340Sstevel@tonic-gate 			"subdirectories of %s.\n"), rootpath);
21350Sstevel@tonic-gate 		(void) zonecfg_find_mounts(rootpath, zfm_print, NULL);
21360Sstevel@tonic-gate 		return (Z_ERR);
21370Sstevel@tonic-gate 	}
21380Sstevel@tonic-gate 
21390Sstevel@tonic-gate 	err = zone_set_state(target_zone, ZONE_STATE_INCOMPLETE);
21400Sstevel@tonic-gate 	if (err != Z_OK) {
21410Sstevel@tonic-gate 		errno = err;
21420Sstevel@tonic-gate 		zperror2(target_zone, gettext("could not set state"));
21430Sstevel@tonic-gate 		goto bad;
21440Sstevel@tonic-gate 	}
21450Sstevel@tonic-gate 
21460Sstevel@tonic-gate 	/*
21470Sstevel@tonic-gate 	 * "exec" the command so that the returned status is that of
21480Sstevel@tonic-gate 	 * RMCOMMAND and not the shell.
21490Sstevel@tonic-gate 	 */
21500Sstevel@tonic-gate 	(void) snprintf(cmdbuf, sizeof (cmdbuf), "exec " RMCOMMAND " %s",
21510Sstevel@tonic-gate 	    devpath);
21520Sstevel@tonic-gate 	status = do_subproc(cmdbuf);
21530Sstevel@tonic-gate 	if ((err = subproc_status(RMCOMMAND, status)) != Z_OK)
21540Sstevel@tonic-gate 		goto bad;
21550Sstevel@tonic-gate 	(void) snprintf(cmdbuf, sizeof (cmdbuf), "exec " RMCOMMAND " %s",
21560Sstevel@tonic-gate 	    rootpath);
21570Sstevel@tonic-gate 	status = do_subproc(cmdbuf);
21580Sstevel@tonic-gate 	if ((err = subproc_status(RMCOMMAND, status)) != Z_OK)
21590Sstevel@tonic-gate 		goto bad;
21600Sstevel@tonic-gate 	err = zone_set_state(target_zone, ZONE_STATE_CONFIGURED);
21610Sstevel@tonic-gate 	if (err != Z_OK) {
21620Sstevel@tonic-gate 		errno = err;
21630Sstevel@tonic-gate 		zperror2(target_zone, gettext("could not reset state"));
21640Sstevel@tonic-gate 	}
21650Sstevel@tonic-gate bad:
21660Sstevel@tonic-gate 	release_lock_file(lockfd);
21670Sstevel@tonic-gate 	return (err);
21680Sstevel@tonic-gate }
21690Sstevel@tonic-gate 
21700Sstevel@tonic-gate static int
21710Sstevel@tonic-gate help_func(int argc, char *argv[])
21720Sstevel@tonic-gate {
21730Sstevel@tonic-gate 	int arg, cmd_num;
21740Sstevel@tonic-gate 
21750Sstevel@tonic-gate 	if (argc == 0) {
21760Sstevel@tonic-gate 		(void) usage(B_TRUE);
21770Sstevel@tonic-gate 		return (Z_OK);
21780Sstevel@tonic-gate 	}
21790Sstevel@tonic-gate 	optind = 0;
21800Sstevel@tonic-gate 	if ((arg = getopt(argc, argv, "?")) != EOF) {
21810Sstevel@tonic-gate 		switch (arg) {
21820Sstevel@tonic-gate 		case '?':
21830Sstevel@tonic-gate 			sub_usage(SHELP_HELP, CMD_HELP);
21840Sstevel@tonic-gate 			return (optopt == '?' ? Z_OK : Z_USAGE);
21850Sstevel@tonic-gate 		default:
21860Sstevel@tonic-gate 			sub_usage(SHELP_HELP, CMD_HELP);
21870Sstevel@tonic-gate 			return (Z_USAGE);
21880Sstevel@tonic-gate 		}
21890Sstevel@tonic-gate 	}
21900Sstevel@tonic-gate 	while (optind < argc) {
21910Sstevel@tonic-gate 		if ((cmd_num = cmd_match(argv[optind])) < 0) {
21920Sstevel@tonic-gate 			sub_usage(SHELP_HELP, CMD_HELP);
21930Sstevel@tonic-gate 			return (Z_USAGE);
21940Sstevel@tonic-gate 		}
21950Sstevel@tonic-gate 		sub_usage(cmdtab[cmd_num].short_usage, cmd_num);
21960Sstevel@tonic-gate 		optind++;
21970Sstevel@tonic-gate 	}
21980Sstevel@tonic-gate 	return (Z_OK);
21990Sstevel@tonic-gate }
22000Sstevel@tonic-gate 
22010Sstevel@tonic-gate /*
22020Sstevel@tonic-gate  * Returns: CMD_MIN thru CMD_MAX on success, -1 on error
22030Sstevel@tonic-gate  */
22040Sstevel@tonic-gate 
22050Sstevel@tonic-gate static int
22060Sstevel@tonic-gate cmd_match(char *cmd)
22070Sstevel@tonic-gate {
22080Sstevel@tonic-gate 	int i;
22090Sstevel@tonic-gate 
22100Sstevel@tonic-gate 	for (i = CMD_MIN; i <= CMD_MAX; i++) {
22110Sstevel@tonic-gate 		/* return only if there is an exact match */
22120Sstevel@tonic-gate 		if (strcmp(cmd, cmdtab[i].cmd_name) == 0)
22130Sstevel@tonic-gate 			return (cmdtab[i].cmd_num);
22140Sstevel@tonic-gate 	}
22150Sstevel@tonic-gate 	return (-1);
22160Sstevel@tonic-gate }
22170Sstevel@tonic-gate 
22180Sstevel@tonic-gate static int
22190Sstevel@tonic-gate parse_and_run(int argc, char *argv[])
22200Sstevel@tonic-gate {
22210Sstevel@tonic-gate 	int i = cmd_match(argv[0]);
22220Sstevel@tonic-gate 
22230Sstevel@tonic-gate 	if (i < 0)
22240Sstevel@tonic-gate 		return (usage(B_FALSE));
22250Sstevel@tonic-gate 	return (cmdtab[i].handler(argc - 1, &(argv[1])));
22260Sstevel@tonic-gate }
22270Sstevel@tonic-gate 
22280Sstevel@tonic-gate static char *
22290Sstevel@tonic-gate get_execbasename(char *execfullname)
22300Sstevel@tonic-gate {
22310Sstevel@tonic-gate 	char *last_slash, *execbasename;
22320Sstevel@tonic-gate 
22330Sstevel@tonic-gate 	/* guard against '/' at end of command invocation */
22340Sstevel@tonic-gate 	for (;;) {
22350Sstevel@tonic-gate 		last_slash = strrchr(execfullname, '/');
22360Sstevel@tonic-gate 		if (last_slash == NULL) {
22370Sstevel@tonic-gate 			execbasename = execfullname;
22380Sstevel@tonic-gate 			break;
22390Sstevel@tonic-gate 		} else {
22400Sstevel@tonic-gate 			execbasename = last_slash + 1;
22410Sstevel@tonic-gate 			if (*execbasename == '\0') {
22420Sstevel@tonic-gate 				*last_slash = '\0';
22430Sstevel@tonic-gate 				continue;
22440Sstevel@tonic-gate 			}
22450Sstevel@tonic-gate 			break;
22460Sstevel@tonic-gate 		}
22470Sstevel@tonic-gate 	}
22480Sstevel@tonic-gate 	return (execbasename);
22490Sstevel@tonic-gate }
22500Sstevel@tonic-gate 
22510Sstevel@tonic-gate int
22520Sstevel@tonic-gate main(int argc, char **argv)
22530Sstevel@tonic-gate {
22540Sstevel@tonic-gate 	int arg;
22550Sstevel@tonic-gate 	zoneid_t zid;
22560Sstevel@tonic-gate 
22570Sstevel@tonic-gate 	if ((locale = setlocale(LC_ALL, "")) == NULL)
22580Sstevel@tonic-gate 		locale = "C";
22590Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
22600Sstevel@tonic-gate 	setbuf(stdout, NULL);
22610Sstevel@tonic-gate 	(void) sigset(SIGHUP, SIG_IGN);
22620Sstevel@tonic-gate 	execname = get_execbasename(argv[0]);
22630Sstevel@tonic-gate 	target_zone = NULL;
22640Sstevel@tonic-gate 	if (chdir("/") != 0) {
22650Sstevel@tonic-gate 		zerror(gettext("could not change directory to /."));
22660Sstevel@tonic-gate 		exit(Z_ERR);
22670Sstevel@tonic-gate 	}
22680Sstevel@tonic-gate 
22690Sstevel@tonic-gate 	while ((arg = getopt(argc, argv, "?z:")) != EOF) {
22700Sstevel@tonic-gate 		switch (arg) {
22710Sstevel@tonic-gate 		case '?':
22720Sstevel@tonic-gate 			return (usage(B_TRUE));
22730Sstevel@tonic-gate 		case 'z':
22740Sstevel@tonic-gate 			target_zone = optarg;
22750Sstevel@tonic-gate 			break;
22760Sstevel@tonic-gate 		default:
22770Sstevel@tonic-gate 			return (usage(B_FALSE));
22780Sstevel@tonic-gate 		}
22790Sstevel@tonic-gate 	}
22800Sstevel@tonic-gate 
22810Sstevel@tonic-gate 	if (optind >= argc)
22820Sstevel@tonic-gate 		return (usage(B_FALSE));
22830Sstevel@tonic-gate 	if (target_zone != NULL && zone_get_id(target_zone, &zid) != 0) {
22840Sstevel@tonic-gate 		errno = Z_NO_ZONE;
22850Sstevel@tonic-gate 		zperror(target_zone, B_TRUE);
22860Sstevel@tonic-gate 		exit(Z_ERR);
22870Sstevel@tonic-gate 	}
22880Sstevel@tonic-gate 	return (parse_and_run(argc - optind, &argv[optind]));
22890Sstevel@tonic-gate }
2290