xref: /illumos-gate/usr/src/cmd/zoneadm/zoneadm.c (revision 7e362f586af53d30ffc869a0ddc7d0f284ef8f82)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
22*7e362f58Scomay 
237c478bd9Sstevel@tonic-gate /*
247c478bd9Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
257c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
267c478bd9Sstevel@tonic-gate  */
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate /*
317c478bd9Sstevel@tonic-gate  * zoneadm is a command interpreter for zone administration.  It is all in
327c478bd9Sstevel@tonic-gate  * C (i.e., no lex/yacc), and all the argument passing is argc/argv based.
337c478bd9Sstevel@tonic-gate  * main() calls parse_and_run() which calls cmd_match(), then invokes the
347c478bd9Sstevel@tonic-gate  * appropriate command's handler function.  The rest of the program is the
357c478bd9Sstevel@tonic-gate  * handler functions and their helper functions.
367c478bd9Sstevel@tonic-gate  *
377c478bd9Sstevel@tonic-gate  * Some of the helper functions are used largely to simplify I18N: reducing
387c478bd9Sstevel@tonic-gate  * the need for translation notes.  This is particularly true of many of
397c478bd9Sstevel@tonic-gate  * the zerror() calls: doing e.g. zerror(gettext("%s failed"), "foo") rather
407c478bd9Sstevel@tonic-gate  * than zerror(gettext("foo failed")) with a translation note indicating
417c478bd9Sstevel@tonic-gate  * that "foo" need not be translated.
427c478bd9Sstevel@tonic-gate  */
437c478bd9Sstevel@tonic-gate 
447c478bd9Sstevel@tonic-gate #include <stdio.h>
457c478bd9Sstevel@tonic-gate #include <errno.h>
467c478bd9Sstevel@tonic-gate #include <unistd.h>
477c478bd9Sstevel@tonic-gate #include <signal.h>
487c478bd9Sstevel@tonic-gate #include <stdarg.h>
497c478bd9Sstevel@tonic-gate #include <ctype.h>
507c478bd9Sstevel@tonic-gate #include <stdlib.h>
517c478bd9Sstevel@tonic-gate #include <string.h>
527c478bd9Sstevel@tonic-gate #include <wait.h>
537c478bd9Sstevel@tonic-gate #include <zone.h>
547c478bd9Sstevel@tonic-gate #include <priv.h>
557c478bd9Sstevel@tonic-gate #include <locale.h>
567c478bd9Sstevel@tonic-gate #include <libintl.h>
577c478bd9Sstevel@tonic-gate #include <libzonecfg.h>
587c478bd9Sstevel@tonic-gate #include <bsm/adt.h>
597c478bd9Sstevel@tonic-gate #include <sys/utsname.h>
607c478bd9Sstevel@tonic-gate #include <sys/param.h>
617c478bd9Sstevel@tonic-gate #include <sys/types.h>
627c478bd9Sstevel@tonic-gate #include <sys/stat.h>
637c478bd9Sstevel@tonic-gate #include <sys/statvfs.h>
647c478bd9Sstevel@tonic-gate #include <assert.h>
657c478bd9Sstevel@tonic-gate #include <sys/sockio.h>
667c478bd9Sstevel@tonic-gate #include <sys/mntent.h>
677c478bd9Sstevel@tonic-gate #include <limits.h>
687c478bd9Sstevel@tonic-gate 
697c478bd9Sstevel@tonic-gate #include <fcntl.h>
707c478bd9Sstevel@tonic-gate #include <door.h>
717c478bd9Sstevel@tonic-gate #include <macros.h>
727c478bd9Sstevel@tonic-gate #include <libgen.h>
737c478bd9Sstevel@tonic-gate 
747c478bd9Sstevel@tonic-gate #include <pool.h>
757c478bd9Sstevel@tonic-gate #include <sys/pool.h>
767c478bd9Sstevel@tonic-gate 
777c478bd9Sstevel@tonic-gate #define	MAXARGS	8
787c478bd9Sstevel@tonic-gate 
797c478bd9Sstevel@tonic-gate /* Reflects kernel zone entries */
807c478bd9Sstevel@tonic-gate typedef struct zone_entry {
817c478bd9Sstevel@tonic-gate 	zoneid_t	zid;
827c478bd9Sstevel@tonic-gate 	char		zname[ZONENAME_MAX];
837c478bd9Sstevel@tonic-gate 	char		*zstate_str;
847c478bd9Sstevel@tonic-gate 	zone_state_t	zstate_num;
857c478bd9Sstevel@tonic-gate 	char		zroot[MAXPATHLEN];
867c478bd9Sstevel@tonic-gate } zone_entry_t;
877c478bd9Sstevel@tonic-gate 
887c478bd9Sstevel@tonic-gate static zone_entry_t *zents;
897c478bd9Sstevel@tonic-gate static size_t nzents;
907c478bd9Sstevel@tonic-gate 
917c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)		/* should be defined by cc -D */
927c478bd9Sstevel@tonic-gate #define	TEXT_DOMAIN	"SYS_TEST"	/* Use this only if it wasn't */
937c478bd9Sstevel@tonic-gate #endif
947c478bd9Sstevel@tonic-gate 
957c478bd9Sstevel@tonic-gate #define	Z_ERR	1
967c478bd9Sstevel@tonic-gate #define	Z_USAGE	2
977c478bd9Sstevel@tonic-gate 
987c478bd9Sstevel@tonic-gate /* 0755 is the default directory mode. */
997c478bd9Sstevel@tonic-gate #define	DEFAULT_DIR_MODE \
1007c478bd9Sstevel@tonic-gate 	(S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH)
1017c478bd9Sstevel@tonic-gate 
1027c478bd9Sstevel@tonic-gate #define	CMD_HELP	0
1037c478bd9Sstevel@tonic-gate #define	CMD_BOOT	1
1047c478bd9Sstevel@tonic-gate #define	CMD_HALT	2
1057c478bd9Sstevel@tonic-gate #define	CMD_READY	3
1067c478bd9Sstevel@tonic-gate #define	CMD_REBOOT	4
1077c478bd9Sstevel@tonic-gate #define	CMD_LIST	5
1087c478bd9Sstevel@tonic-gate #define	CMD_VERIFY	6
1097c478bd9Sstevel@tonic-gate #define	CMD_INSTALL	7
1107c478bd9Sstevel@tonic-gate #define	CMD_UNINSTALL	8
1117c478bd9Sstevel@tonic-gate 
1127c478bd9Sstevel@tonic-gate #define	CMD_MIN		CMD_HELP
1137c478bd9Sstevel@tonic-gate #define	CMD_MAX		CMD_UNINSTALL
1147c478bd9Sstevel@tonic-gate 
1157c478bd9Sstevel@tonic-gate struct cmd {
1167c478bd9Sstevel@tonic-gate 	uint_t	cmd_num;				/* command number */
1177c478bd9Sstevel@tonic-gate 	char	*cmd_name;				/* command name */
1187c478bd9Sstevel@tonic-gate 	char	*short_usage;				/* short form help */
1197c478bd9Sstevel@tonic-gate 	int	(*handler)(int argc, char *argv[]);	/* function to call */
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate };
1227c478bd9Sstevel@tonic-gate 
1237c478bd9Sstevel@tonic-gate #define	SHELP_HELP	"help"
1247c478bd9Sstevel@tonic-gate #define	SHELP_BOOT	"boot [-s]"
1257c478bd9Sstevel@tonic-gate #define	SHELP_HALT	"halt"
1267c478bd9Sstevel@tonic-gate #define	SHELP_READY	"ready"
1277c478bd9Sstevel@tonic-gate #define	SHELP_REBOOT	"reboot"
1287c478bd9Sstevel@tonic-gate #define	SHELP_LIST	"list [-cipv]"
1297c478bd9Sstevel@tonic-gate #define	SHELP_VERIFY	"verify"
1307c478bd9Sstevel@tonic-gate #define	SHELP_INSTALL	"install"
1317c478bd9Sstevel@tonic-gate #define	SHELP_UNINSTALL	"uninstall [-F]"
1327c478bd9Sstevel@tonic-gate 
1337c478bd9Sstevel@tonic-gate static int help_func(int argc, char *argv[]);
1347c478bd9Sstevel@tonic-gate static int ready_func(int argc, char *argv[]);
1357c478bd9Sstevel@tonic-gate static int boot_func(int argc, char *argv[]);
1367c478bd9Sstevel@tonic-gate static int halt_func(int argc, char *argv[]);
1377c478bd9Sstevel@tonic-gate static int reboot_func(int argc, char *argv[]);
1387c478bd9Sstevel@tonic-gate static int list_func(int argc, char *argv[]);
1397c478bd9Sstevel@tonic-gate static int verify_func(int argc, char *argv[]);
1407c478bd9Sstevel@tonic-gate static int install_func(int argc, char *argv[]);
1417c478bd9Sstevel@tonic-gate static int uninstall_func(int argc, char *argv[]);
1427c478bd9Sstevel@tonic-gate static int sanity_check(char *zone, int cmd_num, boolean_t running,
1437c478bd9Sstevel@tonic-gate     boolean_t unsafe_when_running);
1447c478bd9Sstevel@tonic-gate static int cmd_match(char *cmd);
1457c478bd9Sstevel@tonic-gate static int verify_details(int);
1467c478bd9Sstevel@tonic-gate 
1477c478bd9Sstevel@tonic-gate static struct cmd cmdtab[] = {
1487c478bd9Sstevel@tonic-gate 	{ CMD_HELP,		"help",		SHELP_HELP,	help_func },
1497c478bd9Sstevel@tonic-gate 	{ CMD_BOOT,		"boot",		SHELP_BOOT,	boot_func },
1507c478bd9Sstevel@tonic-gate 	{ CMD_HALT,		"halt",		SHELP_HALT,	halt_func },
1517c478bd9Sstevel@tonic-gate 	{ CMD_READY,		"ready",	SHELP_READY,	ready_func },
1527c478bd9Sstevel@tonic-gate 	{ CMD_REBOOT,		"reboot",	SHELP_REBOOT,	reboot_func },
1537c478bd9Sstevel@tonic-gate 	{ CMD_LIST,		"list",		SHELP_LIST,	list_func },
1547c478bd9Sstevel@tonic-gate 	{ CMD_VERIFY,		"verify",	SHELP_VERIFY,	verify_func },
1557c478bd9Sstevel@tonic-gate 	{ CMD_INSTALL,		"install",	SHELP_INSTALL,	install_func },
1567c478bd9Sstevel@tonic-gate 	{ CMD_UNINSTALL,	"uninstall",	SHELP_UNINSTALL,
1577c478bd9Sstevel@tonic-gate 	    uninstall_func }
1587c478bd9Sstevel@tonic-gate };
1597c478bd9Sstevel@tonic-gate 
1607c478bd9Sstevel@tonic-gate /* global variables */
1617c478bd9Sstevel@tonic-gate 
1627c478bd9Sstevel@tonic-gate /* set early in main(), never modified thereafter, used all over the place */
1637c478bd9Sstevel@tonic-gate static char *execname;
1647c478bd9Sstevel@tonic-gate static char *target_zone;
1657c478bd9Sstevel@tonic-gate static char *locale;
1667c478bd9Sstevel@tonic-gate 
1677c478bd9Sstevel@tonic-gate /* used in do_subproc() and signal handler */
1687c478bd9Sstevel@tonic-gate static volatile boolean_t child_killed;
1697c478bd9Sstevel@tonic-gate 
1707c478bd9Sstevel@tonic-gate static char *
1717c478bd9Sstevel@tonic-gate cmd_to_str(int cmd_num)
1727c478bd9Sstevel@tonic-gate {
1737c478bd9Sstevel@tonic-gate 	assert(cmd_num >= CMD_MIN && cmd_num <= CMD_MAX);
1747c478bd9Sstevel@tonic-gate 	return (cmdtab[cmd_num].cmd_name);
1757c478bd9Sstevel@tonic-gate }
1767c478bd9Sstevel@tonic-gate 
1777c478bd9Sstevel@tonic-gate /* This is a separate function because of gettext() wrapping. */
1787c478bd9Sstevel@tonic-gate static char *
1797c478bd9Sstevel@tonic-gate long_help(int cmd_num)
1807c478bd9Sstevel@tonic-gate {
181*7e362f58Scomay 	assert(cmd_num >= CMD_MIN && cmd_num <= CMD_MAX);
1827c478bd9Sstevel@tonic-gate 	switch (cmd_num) {
1837c478bd9Sstevel@tonic-gate 		case CMD_HELP:
1847c478bd9Sstevel@tonic-gate 			return (gettext("Print usage message."));
1857c478bd9Sstevel@tonic-gate 		case CMD_BOOT:
1867c478bd9Sstevel@tonic-gate 			return (gettext("Activates (boots) specified zone.  "
1877c478bd9Sstevel@tonic-gate 			    "The -s flag can be used\n\tto boot the zone in "
1887c478bd9Sstevel@tonic-gate 			    "the single-user state."));
1897c478bd9Sstevel@tonic-gate 		case CMD_HALT:
1907c478bd9Sstevel@tonic-gate 			return (gettext("Halts specified zone, bypassing "
1917c478bd9Sstevel@tonic-gate 			    "shutdown scripts and removing runtime\n\t"
1927c478bd9Sstevel@tonic-gate 			    "resources of the zone."));
1937c478bd9Sstevel@tonic-gate 		case CMD_READY:
1947c478bd9Sstevel@tonic-gate 			return (gettext("Prepares a zone for running "
1957c478bd9Sstevel@tonic-gate 			    "applications but does not start any user\n\t"
1967c478bd9Sstevel@tonic-gate 			    "processes in the zone."));
1977c478bd9Sstevel@tonic-gate 		case CMD_REBOOT:
1987c478bd9Sstevel@tonic-gate 			return (gettext("Restarts the zone (equivalent to a "
1997c478bd9Sstevel@tonic-gate 			    "halt / boot sequence).\n\tFails if the zone is "
2007c478bd9Sstevel@tonic-gate 			    "not active."));
2017c478bd9Sstevel@tonic-gate 		case CMD_LIST:
2027c478bd9Sstevel@tonic-gate 			return (gettext("Lists the current zones, or a "
2037c478bd9Sstevel@tonic-gate 			    "specific zone if indicated.  By default,\n\tall "
2047c478bd9Sstevel@tonic-gate 			    "running zones are listed, though this can be "
2057c478bd9Sstevel@tonic-gate 			    "expanded to all\n\tinstalled zones with the -i "
2067c478bd9Sstevel@tonic-gate 			    "option or all configured zones with the\n\t-c "
2077c478bd9Sstevel@tonic-gate 			    "option.  When used with the general -z <zone> "
2087c478bd9Sstevel@tonic-gate 			    "option, lists only the\n\tspecified zone, but "
2097c478bd9Sstevel@tonic-gate 			    "lists it regardless of its state, and the -i "
2107c478bd9Sstevel@tonic-gate 			    "and -c\n\toptions are disallowed.  The -v option "
2117c478bd9Sstevel@tonic-gate 			    "can be used to display verbose\n\tinformation: "
2127c478bd9Sstevel@tonic-gate 			    "zone name, id, current state, root directory and "
2137c478bd9Sstevel@tonic-gate 			    "options.\n\tThe -p option can be used to request "
2147c478bd9Sstevel@tonic-gate 			    "machine-parsable output.  The -v\n\tand -p "
2157c478bd9Sstevel@tonic-gate 			    "options are mutually exclusive.  If neither -v "
2167c478bd9Sstevel@tonic-gate 			    "nor -p is used,\n\tjust the zone name is "
2177c478bd9Sstevel@tonic-gate 			    "listed."));
2187c478bd9Sstevel@tonic-gate 		case CMD_VERIFY:
2197c478bd9Sstevel@tonic-gate 			return (gettext("Check to make sure the configuration "
2207c478bd9Sstevel@tonic-gate 			    "can safely be instantiated\n\ton the machine: "
2217c478bd9Sstevel@tonic-gate 			    "physical network interfaces exist, etc."));
2227c478bd9Sstevel@tonic-gate 		case CMD_INSTALL:
2237c478bd9Sstevel@tonic-gate 			return (gettext("Install the configuration on to the "
2247c478bd9Sstevel@tonic-gate 			    "system."));
2257c478bd9Sstevel@tonic-gate 		case CMD_UNINSTALL:
2267c478bd9Sstevel@tonic-gate 			return (gettext("Uninstall the configuration from the "
2277c478bd9Sstevel@tonic-gate 			    "system.  The -F flag can be used\n\tto force the "
2287c478bd9Sstevel@tonic-gate 			    "action."));
2297c478bd9Sstevel@tonic-gate 	}
2307c478bd9Sstevel@tonic-gate 	/* NOTREACHED */
231*7e362f58Scomay 	return (NULL);
2327c478bd9Sstevel@tonic-gate }
2337c478bd9Sstevel@tonic-gate 
2347c478bd9Sstevel@tonic-gate /*
2357c478bd9Sstevel@tonic-gate  * Called with explicit B_TRUE when help is explicitly requested, B_FALSE for
2367c478bd9Sstevel@tonic-gate  * unexpected errors.
2377c478bd9Sstevel@tonic-gate  */
2387c478bd9Sstevel@tonic-gate 
2397c478bd9Sstevel@tonic-gate static int
2407c478bd9Sstevel@tonic-gate usage(boolean_t explicit)
2417c478bd9Sstevel@tonic-gate {
2427c478bd9Sstevel@tonic-gate 	int i;
2437c478bd9Sstevel@tonic-gate 	FILE *fd = explicit ? stdout : stderr;
2447c478bd9Sstevel@tonic-gate 
2457c478bd9Sstevel@tonic-gate 	(void) fprintf(fd, "%s:\t%s help\n", gettext("usage"), execname);
2467c478bd9Sstevel@tonic-gate 	(void) fprintf(fd, "\t%s [-z <zone>] list\n", execname);
2477c478bd9Sstevel@tonic-gate 	(void) fprintf(fd, "\t%s -z <zone> <%s>\n", execname,
2487c478bd9Sstevel@tonic-gate 	    gettext("subcommand"));
2497c478bd9Sstevel@tonic-gate 	(void) fprintf(fd, "\n%s:\n\n", gettext("Subcommands"));
2507c478bd9Sstevel@tonic-gate 	for (i = CMD_MIN; i <= CMD_MAX; i++) {
2517c478bd9Sstevel@tonic-gate 		(void) fprintf(fd, "%s\n", cmdtab[i].short_usage);
2527c478bd9Sstevel@tonic-gate 		if (explicit)
2537c478bd9Sstevel@tonic-gate 			(void) fprintf(fd, "\t%s\n\n", long_help(i));
2547c478bd9Sstevel@tonic-gate 	}
2557c478bd9Sstevel@tonic-gate 	if (!explicit)
2567c478bd9Sstevel@tonic-gate 		(void) fputs("\n", fd);
2577c478bd9Sstevel@tonic-gate 	return (Z_USAGE);
2587c478bd9Sstevel@tonic-gate }
2597c478bd9Sstevel@tonic-gate 
2607c478bd9Sstevel@tonic-gate static void
2617c478bd9Sstevel@tonic-gate sub_usage(char *short_usage, int cmd_num)
2627c478bd9Sstevel@tonic-gate {
2637c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "%s:\t%s\n", gettext("usage"), short_usage);
2647c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "\t%s\n", long_help(cmd_num));
2657c478bd9Sstevel@tonic-gate }
2667c478bd9Sstevel@tonic-gate 
2677c478bd9Sstevel@tonic-gate /*
2687c478bd9Sstevel@tonic-gate  * zperror() is like perror(3c) except that this also prints the executable
2697c478bd9Sstevel@tonic-gate  * name at the start of the message, and takes a boolean indicating whether
2707c478bd9Sstevel@tonic-gate  * to call libc'c strerror() or that from libzonecfg.
2717c478bd9Sstevel@tonic-gate  */
2727c478bd9Sstevel@tonic-gate 
2737c478bd9Sstevel@tonic-gate static void
2747c478bd9Sstevel@tonic-gate zperror(const char *str, boolean_t zonecfg_error)
2757c478bd9Sstevel@tonic-gate {
2767c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "%s: %s: %s\n", execname, str,
2777c478bd9Sstevel@tonic-gate 	    zonecfg_error ? zonecfg_strerror(errno) : strerror(errno));
2787c478bd9Sstevel@tonic-gate }
2797c478bd9Sstevel@tonic-gate 
2807c478bd9Sstevel@tonic-gate /*
2817c478bd9Sstevel@tonic-gate  * zperror2() is very similar to zperror() above, except it also prints a
2827c478bd9Sstevel@tonic-gate  * supplied zone name after the executable.
2837c478bd9Sstevel@tonic-gate  *
2847c478bd9Sstevel@tonic-gate  * All current consumers of this function want libzonecfg's strerror() rather
2857c478bd9Sstevel@tonic-gate  * than libc's; if this ever changes, this function can be made more generic
2867c478bd9Sstevel@tonic-gate  * like zperror() above.
2877c478bd9Sstevel@tonic-gate  */
2887c478bd9Sstevel@tonic-gate 
2897c478bd9Sstevel@tonic-gate static void
2907c478bd9Sstevel@tonic-gate zperror2(const char *zone, const char *str)
2917c478bd9Sstevel@tonic-gate {
2927c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "%s: %s: %s: %s\n", execname, zone, str,
2937c478bd9Sstevel@tonic-gate 	    zonecfg_strerror(errno));
2947c478bd9Sstevel@tonic-gate }
2957c478bd9Sstevel@tonic-gate 
2967c478bd9Sstevel@tonic-gate /* PRINTFLIKE1 */
2977c478bd9Sstevel@tonic-gate static void
2987c478bd9Sstevel@tonic-gate zerror(const char *fmt, ...)
2997c478bd9Sstevel@tonic-gate {
3007c478bd9Sstevel@tonic-gate 	va_list alist;
3017c478bd9Sstevel@tonic-gate 
3027c478bd9Sstevel@tonic-gate 	va_start(alist, fmt);
3037c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "%s: ", execname);
3047c478bd9Sstevel@tonic-gate 	if (target_zone != NULL)
3057c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "zone '%s': ", target_zone);
3067c478bd9Sstevel@tonic-gate 	(void) vfprintf(stderr, fmt, alist);
3077c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "\n");
3087c478bd9Sstevel@tonic-gate 	va_end(alist);
3097c478bd9Sstevel@tonic-gate }
3107c478bd9Sstevel@tonic-gate 
3117c478bd9Sstevel@tonic-gate static void *
3127c478bd9Sstevel@tonic-gate safe_calloc(size_t nelem, size_t elsize)
3137c478bd9Sstevel@tonic-gate {
3147c478bd9Sstevel@tonic-gate 	void *r = calloc(nelem, elsize);
3157c478bd9Sstevel@tonic-gate 
3167c478bd9Sstevel@tonic-gate 	if (r == NULL) {
3177c478bd9Sstevel@tonic-gate 		zerror(gettext("failed to allocate %lu bytes: %s"),
3187c478bd9Sstevel@tonic-gate 		    (ulong_t)nelem * elsize, strerror(errno));
3197c478bd9Sstevel@tonic-gate 		exit(Z_ERR);
3207c478bd9Sstevel@tonic-gate 	}
3217c478bd9Sstevel@tonic-gate 	return (r);
3227c478bd9Sstevel@tonic-gate }
3237c478bd9Sstevel@tonic-gate 
3247c478bd9Sstevel@tonic-gate static void
3257c478bd9Sstevel@tonic-gate zone_print(zone_entry_t *zent, boolean_t verbose, boolean_t parsable)
3267c478bd9Sstevel@tonic-gate {
3277c478bd9Sstevel@tonic-gate 	static boolean_t firsttime = B_TRUE;
3287c478bd9Sstevel@tonic-gate 
3297c478bd9Sstevel@tonic-gate 	assert(!(verbose && parsable));
3307c478bd9Sstevel@tonic-gate 	if (firsttime && verbose) {
3317c478bd9Sstevel@tonic-gate 		firsttime = B_FALSE;
3327c478bd9Sstevel@tonic-gate 		(void) printf("%*s %-16s %-14s %-30s\n", ZONEID_WIDTH, "ID",
3337c478bd9Sstevel@tonic-gate 		    "NAME", "STATUS", "PATH");
3347c478bd9Sstevel@tonic-gate 	}
3357c478bd9Sstevel@tonic-gate 	if (!verbose) {
3367c478bd9Sstevel@tonic-gate 		if (!parsable) {
3377c478bd9Sstevel@tonic-gate 			(void) printf("%s\n", zent->zname);
3387c478bd9Sstevel@tonic-gate 			return;
3397c478bd9Sstevel@tonic-gate 		}
3407c478bd9Sstevel@tonic-gate 		if (zent->zid == ZONE_ID_UNDEFINED)
3417c478bd9Sstevel@tonic-gate 			(void) printf("-");
3427c478bd9Sstevel@tonic-gate 		else
3437c478bd9Sstevel@tonic-gate 			(void) printf("%lu", zent->zid);
3447c478bd9Sstevel@tonic-gate 		(void) printf(":%s:%s:%s\n", zent->zname, zent->zstate_str,
3457c478bd9Sstevel@tonic-gate 		    zent->zroot);
3467c478bd9Sstevel@tonic-gate 		return;
3477c478bd9Sstevel@tonic-gate 	}
3487c478bd9Sstevel@tonic-gate 	if (zent->zstate_str != NULL) {
3497c478bd9Sstevel@tonic-gate 		if (zent->zid == ZONE_ID_UNDEFINED)
3507c478bd9Sstevel@tonic-gate 			(void) printf("%*s", ZONEID_WIDTH, "-");
3517c478bd9Sstevel@tonic-gate 		else
3527c478bd9Sstevel@tonic-gate 			(void) printf("%*lu", ZONEID_WIDTH, zent->zid);
3537c478bd9Sstevel@tonic-gate 		(void) printf(" %-16s %-14s %-30s\n", zent->zname,
3547c478bd9Sstevel@tonic-gate 		    zent->zstate_str, zent->zroot);
3557c478bd9Sstevel@tonic-gate 	}
3567c478bd9Sstevel@tonic-gate }
3577c478bd9Sstevel@tonic-gate 
3587c478bd9Sstevel@tonic-gate static int
3597c478bd9Sstevel@tonic-gate lookup_zone_info(char *zone_name, zone_entry_t *zent)
3607c478bd9Sstevel@tonic-gate {
3617c478bd9Sstevel@tonic-gate 	char root[MAXPATHLEN];
3627c478bd9Sstevel@tonic-gate 	int err;
3637c478bd9Sstevel@tonic-gate 
3647c478bd9Sstevel@tonic-gate 	(void) strlcpy(zent->zname, zone_name, sizeof (zent->zname));
3657c478bd9Sstevel@tonic-gate 	(void) strlcpy(zent->zroot, "???", sizeof (zent->zroot));
3667c478bd9Sstevel@tonic-gate 	zent->zstate_str = "???";
3677c478bd9Sstevel@tonic-gate 
3687c478bd9Sstevel@tonic-gate 	if ((zent->zid = getzoneidbyname(zone_name)) == -1)
3697c478bd9Sstevel@tonic-gate 		zent->zid = ZONE_ID_UNDEFINED;
3707c478bd9Sstevel@tonic-gate 
3717c478bd9Sstevel@tonic-gate 	if ((err = zone_get_zonepath(zent->zname, root, sizeof (root))) !=
3727c478bd9Sstevel@tonic-gate 	    Z_OK) {
3737c478bd9Sstevel@tonic-gate 		errno = err;
3747c478bd9Sstevel@tonic-gate 		zperror2(zent->zname, gettext("could not get zone path"));
3757c478bd9Sstevel@tonic-gate 		return (Z_ERR);
3767c478bd9Sstevel@tonic-gate 	}
3777c478bd9Sstevel@tonic-gate 	(void) strlcpy(zent->zroot, root, sizeof (zent->zroot));
3787c478bd9Sstevel@tonic-gate 
3797c478bd9Sstevel@tonic-gate 	if ((err = zone_get_state(zent->zname, &zent->zstate_num)) != Z_OK) {
3807c478bd9Sstevel@tonic-gate 		errno = err;
3817c478bd9Sstevel@tonic-gate 		zperror2(zent->zname, gettext("could not get state"));
3827c478bd9Sstevel@tonic-gate 		return (Z_ERR);
3837c478bd9Sstevel@tonic-gate 	}
3847c478bd9Sstevel@tonic-gate 	zent->zstate_str = zone_state_str(zent->zstate_num);
3857c478bd9Sstevel@tonic-gate 
3867c478bd9Sstevel@tonic-gate 	return (Z_OK);
3877c478bd9Sstevel@tonic-gate }
3887c478bd9Sstevel@tonic-gate 
3897c478bd9Sstevel@tonic-gate /*
3907c478bd9Sstevel@tonic-gate  * fetch_zents() calls zone_list(2) to find out how many zones are running
3917c478bd9Sstevel@tonic-gate  * (which is stored in the global nzents), then calls zone_list(2) again
3927c478bd9Sstevel@tonic-gate  * to fetch the list of running zones (stored in the global zents).  This
3937c478bd9Sstevel@tonic-gate  * function may be called multiple times, so if zents is already set, we
3947c478bd9Sstevel@tonic-gate  * return immediately to save work.
3957c478bd9Sstevel@tonic-gate  */
3967c478bd9Sstevel@tonic-gate 
3977c478bd9Sstevel@tonic-gate static int
3987c478bd9Sstevel@tonic-gate fetch_zents()
3997c478bd9Sstevel@tonic-gate {
4007c478bd9Sstevel@tonic-gate 	zoneid_t *zids = NULL;
4017c478bd9Sstevel@tonic-gate 	uint_t nzents_saved;
4027c478bd9Sstevel@tonic-gate 	int i;
4037c478bd9Sstevel@tonic-gate 
4047c478bd9Sstevel@tonic-gate 	if (nzents > 0)
4057c478bd9Sstevel@tonic-gate 		return (Z_OK);
4067c478bd9Sstevel@tonic-gate 
4077c478bd9Sstevel@tonic-gate 	if (zone_list(NULL, &nzents) != 0) {
4087c478bd9Sstevel@tonic-gate 		zperror(gettext("failed to get zoneid list"), B_FALSE);
4097c478bd9Sstevel@tonic-gate 		return (Z_ERR);
4107c478bd9Sstevel@tonic-gate 	}
4117c478bd9Sstevel@tonic-gate 
4127c478bd9Sstevel@tonic-gate again:
4137c478bd9Sstevel@tonic-gate 	if (nzents == 0)
4147c478bd9Sstevel@tonic-gate 		return (Z_OK);
4157c478bd9Sstevel@tonic-gate 
4167c478bd9Sstevel@tonic-gate 	zids = safe_calloc(nzents, sizeof (zoneid_t));
4177c478bd9Sstevel@tonic-gate 	nzents_saved = nzents;
4187c478bd9Sstevel@tonic-gate 
4197c478bd9Sstevel@tonic-gate 	if (zone_list(zids, &nzents) != 0) {
4207c478bd9Sstevel@tonic-gate 		zperror(gettext("failed to get zone list"), B_FALSE);
4217c478bd9Sstevel@tonic-gate 		free(zids);
4227c478bd9Sstevel@tonic-gate 		return (Z_ERR);
4237c478bd9Sstevel@tonic-gate 	}
4247c478bd9Sstevel@tonic-gate 	if (nzents != nzents_saved) {
4257c478bd9Sstevel@tonic-gate 		/* list changed, try again */
4267c478bd9Sstevel@tonic-gate 		free(zids);
4277c478bd9Sstevel@tonic-gate 		goto again;
4287c478bd9Sstevel@tonic-gate 	}
4297c478bd9Sstevel@tonic-gate 
4307c478bd9Sstevel@tonic-gate 	zents = safe_calloc(nzents, sizeof (zone_entry_t));
4317c478bd9Sstevel@tonic-gate 
4327c478bd9Sstevel@tonic-gate 	for (i = 0; i < nzents; i++) {
4337c478bd9Sstevel@tonic-gate 		char name[ZONENAME_MAX];
4347c478bd9Sstevel@tonic-gate 
4357c478bd9Sstevel@tonic-gate 		if (getzonenamebyid(zids[i], name, sizeof (name)) < 0)
4367c478bd9Sstevel@tonic-gate 			zperror(gettext("failed to get zone name"), B_FALSE);
4377c478bd9Sstevel@tonic-gate 		else if (lookup_zone_info(name, &zents[i]) != Z_OK)
4387c478bd9Sstevel@tonic-gate 			zerror(gettext("failed to get zone list"));
4397c478bd9Sstevel@tonic-gate 	}
4407c478bd9Sstevel@tonic-gate 
4417c478bd9Sstevel@tonic-gate 	free(zids);
4427c478bd9Sstevel@tonic-gate 	return (Z_OK);
4437c478bd9Sstevel@tonic-gate }
4447c478bd9Sstevel@tonic-gate 
4457c478bd9Sstevel@tonic-gate static void
4467c478bd9Sstevel@tonic-gate zone_print_list(zone_state_t min_state, boolean_t verbose, boolean_t parsable)
4477c478bd9Sstevel@tonic-gate {
4487c478bd9Sstevel@tonic-gate 	int i;
4497c478bd9Sstevel@tonic-gate 	zone_entry_t zent;
4507c478bd9Sstevel@tonic-gate 	FILE *cookie;
4517c478bd9Sstevel@tonic-gate 	char *name;
4527c478bd9Sstevel@tonic-gate 
4537c478bd9Sstevel@tonic-gate 	/*
4547c478bd9Sstevel@tonic-gate 	 * First get the list of running zones from the kernel and print them.
4557c478bd9Sstevel@tonic-gate 	 * If that is all we need, then return.
4567c478bd9Sstevel@tonic-gate 	 */
4577c478bd9Sstevel@tonic-gate 	if (fetch_zents() != Z_OK) {
4587c478bd9Sstevel@tonic-gate 		/*
4597c478bd9Sstevel@tonic-gate 		 * No need for error messages; fetch_zents() has already taken
4607c478bd9Sstevel@tonic-gate 		 * care of this.
4617c478bd9Sstevel@tonic-gate 		 */
4627c478bd9Sstevel@tonic-gate 		return;
4637c478bd9Sstevel@tonic-gate 	}
4647c478bd9Sstevel@tonic-gate 	for (i = 0; i < nzents; i++) {
4657c478bd9Sstevel@tonic-gate 		if (!verbose && !parsable) {
4667c478bd9Sstevel@tonic-gate 			zone_print(&zents[i], verbose, parsable);
4677c478bd9Sstevel@tonic-gate 			continue;
4687c478bd9Sstevel@tonic-gate 		}
4697c478bd9Sstevel@tonic-gate 		zone_print(&zents[i], verbose, parsable);
4707c478bd9Sstevel@tonic-gate 	}
4717c478bd9Sstevel@tonic-gate 	if (min_state >= ZONE_STATE_RUNNING)
4727c478bd9Sstevel@tonic-gate 		return;
4737c478bd9Sstevel@tonic-gate 	/*
4747c478bd9Sstevel@tonic-gate 	 * Next, get the full list of zones from the configuration, skipping
4757c478bd9Sstevel@tonic-gate 	 * any we have already printed.
4767c478bd9Sstevel@tonic-gate 	 */
4777c478bd9Sstevel@tonic-gate 	cookie = setzoneent();
4787c478bd9Sstevel@tonic-gate 	while ((name = getzoneent(cookie)) != NULL) {
4797c478bd9Sstevel@tonic-gate 		for (i = 0; i < nzents; i++) {
4807c478bd9Sstevel@tonic-gate 			if (strcmp(zents[i].zname, name) == 0)
4817c478bd9Sstevel@tonic-gate 				break;
4827c478bd9Sstevel@tonic-gate 		}
4837c478bd9Sstevel@tonic-gate 		if (i < nzents) {
4847c478bd9Sstevel@tonic-gate 			free(name);
4857c478bd9Sstevel@tonic-gate 			continue;
4867c478bd9Sstevel@tonic-gate 		}
4877c478bd9Sstevel@tonic-gate 		if (lookup_zone_info(name, &zent) != Z_OK) {
4887c478bd9Sstevel@tonic-gate 			free(name);
4897c478bd9Sstevel@tonic-gate 			continue;
4907c478bd9Sstevel@tonic-gate 		}
4917c478bd9Sstevel@tonic-gate 		free(name);
4927c478bd9Sstevel@tonic-gate 		if (zent.zstate_num >= min_state)
4937c478bd9Sstevel@tonic-gate 			zone_print(&zent, verbose, parsable);
4947c478bd9Sstevel@tonic-gate 	}
4957c478bd9Sstevel@tonic-gate 	endzoneent(cookie);
4967c478bd9Sstevel@tonic-gate }
4977c478bd9Sstevel@tonic-gate 
4987c478bd9Sstevel@tonic-gate static zone_entry_t *
4997c478bd9Sstevel@tonic-gate lookup_running_zone(char *str)
5007c478bd9Sstevel@tonic-gate {
5017c478bd9Sstevel@tonic-gate 	zoneid_t zoneid;
5027c478bd9Sstevel@tonic-gate 	char *cp;
5037c478bd9Sstevel@tonic-gate 	int i;
5047c478bd9Sstevel@tonic-gate 
5057c478bd9Sstevel@tonic-gate 	if (fetch_zents() != Z_OK)
5067c478bd9Sstevel@tonic-gate 		return (NULL);
5077c478bd9Sstevel@tonic-gate 
5087c478bd9Sstevel@tonic-gate 	for (i = 0; i < nzents; i++) {
5097c478bd9Sstevel@tonic-gate 		if (strcmp(str, zents[i].zname) == 0)
5107c478bd9Sstevel@tonic-gate 			return (&zents[i]);
5117c478bd9Sstevel@tonic-gate 	}
5127c478bd9Sstevel@tonic-gate 	errno = 0;
5137c478bd9Sstevel@tonic-gate 	zoneid = strtol(str, &cp, 0);
5147c478bd9Sstevel@tonic-gate 	if (zoneid < MIN_ZONEID || zoneid > MAX_ZONEID ||
5157c478bd9Sstevel@tonic-gate 	    errno != 0 || *cp != '\0')
5167c478bd9Sstevel@tonic-gate 		return (NULL);
5177c478bd9Sstevel@tonic-gate 	for (i = 0; i < nzents; i++) {
5187c478bd9Sstevel@tonic-gate 		if (zoneid == zents[i].zid)
5197c478bd9Sstevel@tonic-gate 			return (&zents[i]);
5207c478bd9Sstevel@tonic-gate 	}
5217c478bd9Sstevel@tonic-gate 	return (NULL);
5227c478bd9Sstevel@tonic-gate }
5237c478bd9Sstevel@tonic-gate 
5247c478bd9Sstevel@tonic-gate /*
5257c478bd9Sstevel@tonic-gate  * Check a bit in a mode_t: if on is B_TRUE, that bit should be on; if
5267c478bd9Sstevel@tonic-gate  * B_FALSE, it should be off.  Return B_TRUE if the mode is bad (incorrect).
5277c478bd9Sstevel@tonic-gate  */
5287c478bd9Sstevel@tonic-gate static boolean_t
5297c478bd9Sstevel@tonic-gate bad_mode_bit(mode_t mode, mode_t bit, boolean_t on, char *file)
5307c478bd9Sstevel@tonic-gate {
5317c478bd9Sstevel@tonic-gate 	char *str;
5327c478bd9Sstevel@tonic-gate 
5337c478bd9Sstevel@tonic-gate 	assert(bit == S_IRUSR || bit == S_IWUSR || bit == S_IXUSR ||
5347c478bd9Sstevel@tonic-gate 	    bit == S_IRGRP || bit == S_IWGRP || bit == S_IXGRP ||
5357c478bd9Sstevel@tonic-gate 	    bit == S_IROTH || bit == S_IWOTH || bit == S_IXOTH);
5367c478bd9Sstevel@tonic-gate 	/*
5377c478bd9Sstevel@tonic-gate 	 * TRANSLATION_NOTE
5387c478bd9Sstevel@tonic-gate 	 * The strings below will be used as part of a larger message,
5397c478bd9Sstevel@tonic-gate 	 * either:
5407c478bd9Sstevel@tonic-gate 	 * (file name) must be (owner|group|world) (read|writ|execut)able
5417c478bd9Sstevel@tonic-gate 	 * or
5427c478bd9Sstevel@tonic-gate 	 * (file name) must not be (owner|group|world) (read|writ|execut)able
5437c478bd9Sstevel@tonic-gate 	 */
5447c478bd9Sstevel@tonic-gate 	switch (bit) {
5457c478bd9Sstevel@tonic-gate 	case S_IRUSR:
5467c478bd9Sstevel@tonic-gate 		str = gettext("owner readable");
5477c478bd9Sstevel@tonic-gate 		break;
5487c478bd9Sstevel@tonic-gate 	case S_IWUSR:
5497c478bd9Sstevel@tonic-gate 		str = gettext("owner writable");
5507c478bd9Sstevel@tonic-gate 		break;
5517c478bd9Sstevel@tonic-gate 	case S_IXUSR:
5527c478bd9Sstevel@tonic-gate 		str = gettext("owner executable");
5537c478bd9Sstevel@tonic-gate 		break;
5547c478bd9Sstevel@tonic-gate 	case S_IRGRP:
5557c478bd9Sstevel@tonic-gate 		str = gettext("group readable");
5567c478bd9Sstevel@tonic-gate 		break;
5577c478bd9Sstevel@tonic-gate 	case S_IWGRP:
5587c478bd9Sstevel@tonic-gate 		str = gettext("group writable");
5597c478bd9Sstevel@tonic-gate 		break;
5607c478bd9Sstevel@tonic-gate 	case S_IXGRP:
5617c478bd9Sstevel@tonic-gate 		str = gettext("group executable");
5627c478bd9Sstevel@tonic-gate 		break;
5637c478bd9Sstevel@tonic-gate 	case S_IROTH:
5647c478bd9Sstevel@tonic-gate 		str = gettext("world readable");
5657c478bd9Sstevel@tonic-gate 		break;
5667c478bd9Sstevel@tonic-gate 	case S_IWOTH:
5677c478bd9Sstevel@tonic-gate 		str = gettext("world writable");
5687c478bd9Sstevel@tonic-gate 		break;
5697c478bd9Sstevel@tonic-gate 	case S_IXOTH:
5707c478bd9Sstevel@tonic-gate 		str = gettext("world executable");
5717c478bd9Sstevel@tonic-gate 		break;
5727c478bd9Sstevel@tonic-gate 	}
5737c478bd9Sstevel@tonic-gate 	if ((mode & bit) == (on ? 0 : bit)) {
5747c478bd9Sstevel@tonic-gate 		/*
5757c478bd9Sstevel@tonic-gate 		 * TRANSLATION_NOTE
5767c478bd9Sstevel@tonic-gate 		 * The first parameter below is a file name; the second
5777c478bd9Sstevel@tonic-gate 		 * is one of the "(owner|group|world) (read|writ|execut)able"
5787c478bd9Sstevel@tonic-gate 		 * strings from above.
5797c478bd9Sstevel@tonic-gate 		 */
5807c478bd9Sstevel@tonic-gate 		/*
5817c478bd9Sstevel@tonic-gate 		 * The code below could be simplified but not in a way
5827c478bd9Sstevel@tonic-gate 		 * that would easily translate to non-English locales.
5837c478bd9Sstevel@tonic-gate 		 */
5847c478bd9Sstevel@tonic-gate 		if (on) {
5857c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("%s must be %s.\n"),
5867c478bd9Sstevel@tonic-gate 			    file, str);
5877c478bd9Sstevel@tonic-gate 		} else {
5887c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("%s must not be %s.\n"),
5897c478bd9Sstevel@tonic-gate 			    file, str);
5907c478bd9Sstevel@tonic-gate 		}
5917c478bd9Sstevel@tonic-gate 		return (B_TRUE);
5927c478bd9Sstevel@tonic-gate 	}
5937c478bd9Sstevel@tonic-gate 	return (B_FALSE);
5947c478bd9Sstevel@tonic-gate }
5957c478bd9Sstevel@tonic-gate 
5967c478bd9Sstevel@tonic-gate /*
5977c478bd9Sstevel@tonic-gate  * We want to make sure that no zone has its zone path as a child node
5987c478bd9Sstevel@tonic-gate  * (in the directory sense) of any other.  We do that by comparing this
5997c478bd9Sstevel@tonic-gate  * zone's path to the path of all other (non-global) zones.  The comparison
6007c478bd9Sstevel@tonic-gate  * in each case is simple: add '/' to the end of the path, then do a
6017c478bd9Sstevel@tonic-gate  * strncmp() of the two paths, using the length of the shorter one.
6027c478bd9Sstevel@tonic-gate  */
6037c478bd9Sstevel@tonic-gate 
6047c478bd9Sstevel@tonic-gate static int
6057c478bd9Sstevel@tonic-gate crosscheck_zonepaths(char *path)
6067c478bd9Sstevel@tonic-gate {
6077c478bd9Sstevel@tonic-gate 	char rpath[MAXPATHLEN];		/* resolved path */
6087c478bd9Sstevel@tonic-gate 	char path_copy[MAXPATHLEN];	/* copy of original path */
6097c478bd9Sstevel@tonic-gate 	char rpath_copy[MAXPATHLEN];	/* copy of original rpath */
6107c478bd9Sstevel@tonic-gate 	struct zoneent *ze;
6117c478bd9Sstevel@tonic-gate 	int res, err;
6127c478bd9Sstevel@tonic-gate 	FILE *cookie;
6137c478bd9Sstevel@tonic-gate 
6147c478bd9Sstevel@tonic-gate 	cookie = setzoneent();
6157c478bd9Sstevel@tonic-gate 	while ((ze = getzoneent_private(cookie)) != NULL) {
6167c478bd9Sstevel@tonic-gate 		/* Skip zones which are not installed. */
6177c478bd9Sstevel@tonic-gate 		if (ze->zone_state < ZONE_STATE_INSTALLED) {
6187c478bd9Sstevel@tonic-gate 			free(ze);
6197c478bd9Sstevel@tonic-gate 			continue;
6207c478bd9Sstevel@tonic-gate 		}
6217c478bd9Sstevel@tonic-gate 		/* Skip the global zone and the current target zone. */
6227c478bd9Sstevel@tonic-gate 		if (strcmp(ze->zone_name, GLOBAL_ZONENAME) == 0 ||
6237c478bd9Sstevel@tonic-gate 		    strcmp(ze->zone_name, target_zone) == 0) {
6247c478bd9Sstevel@tonic-gate 			free(ze);
6257c478bd9Sstevel@tonic-gate 			continue;
6267c478bd9Sstevel@tonic-gate 		}
6277c478bd9Sstevel@tonic-gate 		if (strlen(ze->zone_path) == 0) {
6287c478bd9Sstevel@tonic-gate 			/* old index file without path, fall back */
6297c478bd9Sstevel@tonic-gate 			if ((err = zone_get_zonepath(ze->zone_name,
6307c478bd9Sstevel@tonic-gate 			    ze->zone_path, sizeof (ze->zone_path))) != Z_OK) {
6317c478bd9Sstevel@tonic-gate 				errno = err;
6327c478bd9Sstevel@tonic-gate 				zperror2(ze->zone_name,
6337c478bd9Sstevel@tonic-gate 				    gettext("could not get zone path"));
6347c478bd9Sstevel@tonic-gate 				free(ze);
6357c478bd9Sstevel@tonic-gate 				continue;
6367c478bd9Sstevel@tonic-gate 			}
6377c478bd9Sstevel@tonic-gate 		}
6387c478bd9Sstevel@tonic-gate 		res = resolvepath(ze->zone_path, rpath, sizeof (rpath));
6397c478bd9Sstevel@tonic-gate 		if (res == -1) {
6407c478bd9Sstevel@tonic-gate 			if (errno != ENOENT) {
6417c478bd9Sstevel@tonic-gate 				zperror(ze->zone_path, B_FALSE);
6427c478bd9Sstevel@tonic-gate 				free(ze);
6437c478bd9Sstevel@tonic-gate 				return (Z_ERR);
6447c478bd9Sstevel@tonic-gate 			}
6457c478bd9Sstevel@tonic-gate 			(void) printf(gettext("WARNING: zone %s is installed, "
6467c478bd9Sstevel@tonic-gate 			    "but its %s %s does not exist.\n"), ze->zone_name,
6477c478bd9Sstevel@tonic-gate 			    "zonepath", ze->zone_path);
6487c478bd9Sstevel@tonic-gate 			free(ze);
6497c478bd9Sstevel@tonic-gate 			continue;
6507c478bd9Sstevel@tonic-gate 		}
6517c478bd9Sstevel@tonic-gate 		rpath[res] = '\0';
6527c478bd9Sstevel@tonic-gate 		(void) snprintf(path_copy, sizeof (path_copy), "%s/", path);
6537c478bd9Sstevel@tonic-gate 		(void) snprintf(rpath_copy, sizeof (rpath_copy), "%s/", rpath);
6547c478bd9Sstevel@tonic-gate 		if (strncmp(path_copy, rpath_copy,
6557c478bd9Sstevel@tonic-gate 		    min(strlen(path_copy), strlen(rpath_copy))) == 0) {
6567c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("%s zonepath (%s) and "
6577c478bd9Sstevel@tonic-gate 			    "%s zonepath (%s) overlap.\n"),
6587c478bd9Sstevel@tonic-gate 			    target_zone, path, ze->zone_name, rpath);
6597c478bd9Sstevel@tonic-gate 			free(ze);
6607c478bd9Sstevel@tonic-gate 			return (Z_ERR);
6617c478bd9Sstevel@tonic-gate 		}
6627c478bd9Sstevel@tonic-gate 		free(ze);
6637c478bd9Sstevel@tonic-gate 	}
6647c478bd9Sstevel@tonic-gate 	endzoneent(cookie);
6657c478bd9Sstevel@tonic-gate 	return (Z_OK);
6667c478bd9Sstevel@tonic-gate }
6677c478bd9Sstevel@tonic-gate 
6687c478bd9Sstevel@tonic-gate static int
6697c478bd9Sstevel@tonic-gate validate_zonepath(char *path, int cmd_num)
6707c478bd9Sstevel@tonic-gate {
6717c478bd9Sstevel@tonic-gate 	int res;			/* result of last library/system call */
6727c478bd9Sstevel@tonic-gate 	boolean_t err = B_FALSE;	/* have we run into an error? */
6737c478bd9Sstevel@tonic-gate 	struct stat stbuf;
6747c478bd9Sstevel@tonic-gate 	struct statvfs vfsbuf;
6757c478bd9Sstevel@tonic-gate 	char rpath[MAXPATHLEN];		/* resolved path */
6767c478bd9Sstevel@tonic-gate 	char ppath[MAXPATHLEN];		/* parent path */
6777c478bd9Sstevel@tonic-gate 	char rppath[MAXPATHLEN];	/* resolved parent path */
6787c478bd9Sstevel@tonic-gate 	char rootpath[MAXPATHLEN];	/* root path */
6797c478bd9Sstevel@tonic-gate 	zone_state_t state;
6807c478bd9Sstevel@tonic-gate 
6817c478bd9Sstevel@tonic-gate 	if (path[0] != '/') {
6827c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
6837c478bd9Sstevel@tonic-gate 		    gettext("%s is not an absolute path.\n"), path);
6847c478bd9Sstevel@tonic-gate 		return (Z_ERR);
6857c478bd9Sstevel@tonic-gate 	}
6867c478bd9Sstevel@tonic-gate 	if ((res = resolvepath(path, rpath, sizeof (rpath))) == -1) {
6877c478bd9Sstevel@tonic-gate 		if ((errno != ENOENT) ||
6887c478bd9Sstevel@tonic-gate 		    (cmd_num != CMD_VERIFY && cmd_num != CMD_INSTALL)) {
6897c478bd9Sstevel@tonic-gate 			zperror(path, B_FALSE);
6907c478bd9Sstevel@tonic-gate 			return (Z_ERR);
6917c478bd9Sstevel@tonic-gate 		}
6927c478bd9Sstevel@tonic-gate 		if (cmd_num == CMD_VERIFY) {
6937c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("WARNING: %s does not "
6947c478bd9Sstevel@tonic-gate 			    "exist, so it cannot be verified.\nWhen 'zoneadm "
6957c478bd9Sstevel@tonic-gate 			    "%s' is run, '%s' will try to create\n%s, and '%s' "
6967c478bd9Sstevel@tonic-gate 			    "will be tried again,\nbut the '%s' may fail if:\n"
6977c478bd9Sstevel@tonic-gate 			    "the parent directory of %s is group- or other-"
6987c478bd9Sstevel@tonic-gate 			    "writable\nor\n%s overlaps with any other "
6997c478bd9Sstevel@tonic-gate 			    "installed zones.\n"), path,
7007c478bd9Sstevel@tonic-gate 			    cmd_to_str(CMD_INSTALL), cmd_to_str(CMD_INSTALL),
7017c478bd9Sstevel@tonic-gate 			    path, cmd_to_str(CMD_VERIFY),
7027c478bd9Sstevel@tonic-gate 			    cmd_to_str(CMD_VERIFY), path, path);
7037c478bd9Sstevel@tonic-gate 			return (Z_OK);
7047c478bd9Sstevel@tonic-gate 		}
7057c478bd9Sstevel@tonic-gate 		/*
7067c478bd9Sstevel@tonic-gate 		 * The zonepath is supposed to be mode 700 but its
7077c478bd9Sstevel@tonic-gate 		 * parent(s) 755.  So use 755 on the mkdirp() then
7087c478bd9Sstevel@tonic-gate 		 * chmod() the zonepath itself to 700.
7097c478bd9Sstevel@tonic-gate 		 */
7107c478bd9Sstevel@tonic-gate 		if (mkdirp(path, DEFAULT_DIR_MODE) < 0) {
7117c478bd9Sstevel@tonic-gate 			zperror(path, B_FALSE);
7127c478bd9Sstevel@tonic-gate 			return (Z_ERR);
7137c478bd9Sstevel@tonic-gate 		}
7147c478bd9Sstevel@tonic-gate 		/*
7157c478bd9Sstevel@tonic-gate 		 * If the chmod() fails, report the error, but might
7167c478bd9Sstevel@tonic-gate 		 * as well continue the verify procedure.
7177c478bd9Sstevel@tonic-gate 		 */
7187c478bd9Sstevel@tonic-gate 		if (chmod(path, S_IRWXU) != 0)
7197c478bd9Sstevel@tonic-gate 			zperror(path, B_FALSE);
7207c478bd9Sstevel@tonic-gate 		/*
7217c478bd9Sstevel@tonic-gate 		 * Since the mkdir() succeeded, we should not have to
7227c478bd9Sstevel@tonic-gate 		 * worry about a subsequent ENOENT, thus this should
7237c478bd9Sstevel@tonic-gate 		 * only recurse once.
7247c478bd9Sstevel@tonic-gate 		 */
7257c478bd9Sstevel@tonic-gate 		return (validate_zonepath(path, CMD_INSTALL));
7267c478bd9Sstevel@tonic-gate 	}
7277c478bd9Sstevel@tonic-gate 	rpath[res] = '\0';
7287c478bd9Sstevel@tonic-gate 	if (strcmp(path, rpath) != 0) {
7297c478bd9Sstevel@tonic-gate 		errno = Z_RESOLVED_PATH;
7307c478bd9Sstevel@tonic-gate 		zperror(path, B_TRUE);
7317c478bd9Sstevel@tonic-gate 		return (Z_ERR);
7327c478bd9Sstevel@tonic-gate 	}
7337c478bd9Sstevel@tonic-gate 	if ((res = stat(rpath, &stbuf)) != 0) {
7347c478bd9Sstevel@tonic-gate 		zperror(rpath, B_FALSE);
7357c478bd9Sstevel@tonic-gate 		return (Z_ERR);
7367c478bd9Sstevel@tonic-gate 	}
7377c478bd9Sstevel@tonic-gate 	if (!S_ISDIR(stbuf.st_mode)) {
7387c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("%s is not a directory.\n"),
7397c478bd9Sstevel@tonic-gate 		    rpath);
7407c478bd9Sstevel@tonic-gate 		return (Z_ERR);
7417c478bd9Sstevel@tonic-gate 	}
7427c478bd9Sstevel@tonic-gate 	if ((strcmp(stbuf.st_fstype, MNTTYPE_TMPFS) == 0) ||
7437c478bd9Sstevel@tonic-gate 	    (strcmp(stbuf.st_fstype, MNTTYPE_XMEMFS) == 0)) {
7447c478bd9Sstevel@tonic-gate 		(void) printf(gettext("WARNING: %s is on a temporary "
7457c478bd9Sstevel@tonic-gate 		    "file-system.\n"), rpath);
7467c478bd9Sstevel@tonic-gate 	}
7477c478bd9Sstevel@tonic-gate 	if (crosscheck_zonepaths(rpath) != Z_OK)
7487c478bd9Sstevel@tonic-gate 		return (Z_ERR);
7497c478bd9Sstevel@tonic-gate 	/*
7507c478bd9Sstevel@tonic-gate 	 * Try to collect and report as many minor errors as possible
7517c478bd9Sstevel@tonic-gate 	 * before returning, so the user can learn everything that needs
7527c478bd9Sstevel@tonic-gate 	 * to be fixed up front.
7537c478bd9Sstevel@tonic-gate 	 */
7547c478bd9Sstevel@tonic-gate 	if (stbuf.st_uid != 0) {
7557c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("%s is not owned by root.\n"),
7567c478bd9Sstevel@tonic-gate 		    rpath);
7577c478bd9Sstevel@tonic-gate 		err = B_TRUE;
7587c478bd9Sstevel@tonic-gate 	}
7597c478bd9Sstevel@tonic-gate 	err |= bad_mode_bit(stbuf.st_mode, S_IRUSR, B_TRUE, rpath);
7607c478bd9Sstevel@tonic-gate 	err |= bad_mode_bit(stbuf.st_mode, S_IWUSR, B_TRUE, rpath);
7617c478bd9Sstevel@tonic-gate 	err |= bad_mode_bit(stbuf.st_mode, S_IXUSR, B_TRUE, rpath);
7627c478bd9Sstevel@tonic-gate 	err |= bad_mode_bit(stbuf.st_mode, S_IRGRP, B_FALSE, rpath);
7637c478bd9Sstevel@tonic-gate 	err |= bad_mode_bit(stbuf.st_mode, S_IWGRP, B_FALSE, rpath);
7647c478bd9Sstevel@tonic-gate 	err |= bad_mode_bit(stbuf.st_mode, S_IXGRP, B_FALSE, rpath);
7657c478bd9Sstevel@tonic-gate 	err |= bad_mode_bit(stbuf.st_mode, S_IROTH, B_FALSE, rpath);
7667c478bd9Sstevel@tonic-gate 	err |= bad_mode_bit(stbuf.st_mode, S_IWOTH, B_FALSE, rpath);
7677c478bd9Sstevel@tonic-gate 	err |= bad_mode_bit(stbuf.st_mode, S_IXOTH, B_FALSE, rpath);
7687c478bd9Sstevel@tonic-gate 
7697c478bd9Sstevel@tonic-gate 	(void) snprintf(ppath, sizeof (ppath), "%s/..", path);
7707c478bd9Sstevel@tonic-gate 	if ((res = resolvepath(ppath, rppath, sizeof (rppath))) == -1) {
7717c478bd9Sstevel@tonic-gate 		zperror(ppath, B_FALSE);
7727c478bd9Sstevel@tonic-gate 		return (Z_ERR);
7737c478bd9Sstevel@tonic-gate 	}
7747c478bd9Sstevel@tonic-gate 	rppath[res] = '\0';
7757c478bd9Sstevel@tonic-gate 	if ((res = stat(rppath, &stbuf)) != 0) {
7767c478bd9Sstevel@tonic-gate 		zperror(rppath, B_FALSE);
7777c478bd9Sstevel@tonic-gate 		return (Z_ERR);
7787c478bd9Sstevel@tonic-gate 	}
7797c478bd9Sstevel@tonic-gate 	/* theoretically impossible */
7807c478bd9Sstevel@tonic-gate 	if (!S_ISDIR(stbuf.st_mode)) {
7817c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("%s is not a directory.\n"),
7827c478bd9Sstevel@tonic-gate 		    rppath);
7837c478bd9Sstevel@tonic-gate 		return (Z_ERR);
7847c478bd9Sstevel@tonic-gate 	}
7857c478bd9Sstevel@tonic-gate 	if (stbuf.st_uid != 0) {
7867c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("%s is not owned by root.\n"),
7877c478bd9Sstevel@tonic-gate 		    rppath);
7887c478bd9Sstevel@tonic-gate 		err = B_TRUE;
7897c478bd9Sstevel@tonic-gate 	}
7907c478bd9Sstevel@tonic-gate 	err |= bad_mode_bit(stbuf.st_mode, S_IRUSR, B_TRUE, rppath);
7917c478bd9Sstevel@tonic-gate 	err |= bad_mode_bit(stbuf.st_mode, S_IWUSR, B_TRUE, rppath);
7927c478bd9Sstevel@tonic-gate 	err |= bad_mode_bit(stbuf.st_mode, S_IXUSR, B_TRUE, rppath);
7937c478bd9Sstevel@tonic-gate 	err |= bad_mode_bit(stbuf.st_mode, S_IWGRP, B_FALSE, rppath);
7947c478bd9Sstevel@tonic-gate 	err |= bad_mode_bit(stbuf.st_mode, S_IWOTH, B_FALSE, rppath);
7957c478bd9Sstevel@tonic-gate 	if (strcmp(rpath, rppath) == 0) {
7967c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("%s is its own parent.\n"),
7977c478bd9Sstevel@tonic-gate 		    rppath);
7987c478bd9Sstevel@tonic-gate 		err = B_TRUE;
7997c478bd9Sstevel@tonic-gate 	}
8007c478bd9Sstevel@tonic-gate 
8017c478bd9Sstevel@tonic-gate 	if (statvfs(rpath, &vfsbuf) != 0) {
8027c478bd9Sstevel@tonic-gate 		zperror(rpath, B_FALSE);
8037c478bd9Sstevel@tonic-gate 		return (Z_ERR);
8047c478bd9Sstevel@tonic-gate 	}
8057c478bd9Sstevel@tonic-gate 	if (strncmp(vfsbuf.f_basetype, "nfs", 3) == 0) {
8067c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("Zonepath %s is over NFS, "
8077c478bd9Sstevel@tonic-gate 		    "which is not currently supported.\n"), rpath);
8087c478bd9Sstevel@tonic-gate 		return (Z_ERR);
8097c478bd9Sstevel@tonic-gate 	}
8107c478bd9Sstevel@tonic-gate 
8117c478bd9Sstevel@tonic-gate 	if ((res = zone_get_state(target_zone, &state)) != Z_OK) {
8127c478bd9Sstevel@tonic-gate 		errno = res;
8137c478bd9Sstevel@tonic-gate 		zperror2(target_zone, gettext("could not get state"));
8147c478bd9Sstevel@tonic-gate 		return (Z_ERR);
8157c478bd9Sstevel@tonic-gate 	}
8167c478bd9Sstevel@tonic-gate 	/*
8177c478bd9Sstevel@tonic-gate 	 * The existence of the root path is only bad in the configured state,
8187c478bd9Sstevel@tonic-gate 	 * as it is *supposed* to be there at the installed and later states.
8197c478bd9Sstevel@tonic-gate 	 * State/command mismatches are caught earlier in verify_details().
8207c478bd9Sstevel@tonic-gate 	 */
8217c478bd9Sstevel@tonic-gate 	if (state == ZONE_STATE_CONFIGURED) {
8227c478bd9Sstevel@tonic-gate 		if (snprintf(rootpath, sizeof (rootpath), "%s/root", rpath) >=
8237c478bd9Sstevel@tonic-gate 		    sizeof (rootpath)) {
8247c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
8257c478bd9Sstevel@tonic-gate 			    gettext("Zonepath %s is too long.\n"), rpath);
8267c478bd9Sstevel@tonic-gate 			return (Z_ERR);
8277c478bd9Sstevel@tonic-gate 		}
8287c478bd9Sstevel@tonic-gate 		if ((res = stat(rootpath, &stbuf)) == 0) {
8297c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("Rootpath %s exists; "
8307c478bd9Sstevel@tonic-gate 			    "remove or move aside prior to %s.\n"), rootpath,
8317c478bd9Sstevel@tonic-gate 			    cmd_to_str(CMD_INSTALL));
8327c478bd9Sstevel@tonic-gate 			return (Z_ERR);
8337c478bd9Sstevel@tonic-gate 		}
8347c478bd9Sstevel@tonic-gate 	}
8357c478bd9Sstevel@tonic-gate 
8367c478bd9Sstevel@tonic-gate 	return (err ? Z_ERR : Z_OK);
8377c478bd9Sstevel@tonic-gate }
8387c478bd9Sstevel@tonic-gate 
8397c478bd9Sstevel@tonic-gate static void
8407c478bd9Sstevel@tonic-gate release_lock_file(int lockfd)
8417c478bd9Sstevel@tonic-gate {
8427c478bd9Sstevel@tonic-gate 	(void) close(lockfd);
8437c478bd9Sstevel@tonic-gate }
8447c478bd9Sstevel@tonic-gate 
8457c478bd9Sstevel@tonic-gate static int
8467c478bd9Sstevel@tonic-gate grab_lock_file(const char *zone_name, int *lockfd)
8477c478bd9Sstevel@tonic-gate {
8487c478bd9Sstevel@tonic-gate 	char pathbuf[PATH_MAX];
8497c478bd9Sstevel@tonic-gate 	struct flock flock;
8507c478bd9Sstevel@tonic-gate 
8517c478bd9Sstevel@tonic-gate 	if (mkdir(ZONES_TMPDIR, S_IRWXU) < 0 && errno != EEXIST) {
8527c478bd9Sstevel@tonic-gate 		zerror(gettext("could not mkdir %s: %s"), ZONES_TMPDIR,
8537c478bd9Sstevel@tonic-gate 		    strerror(errno));
8547c478bd9Sstevel@tonic-gate 		return (Z_ERR);
8557c478bd9Sstevel@tonic-gate 	}
8567c478bd9Sstevel@tonic-gate 	(void) chmod(ZONES_TMPDIR, S_IRWXU);
8577c478bd9Sstevel@tonic-gate 
8587c478bd9Sstevel@tonic-gate 	/*
8597c478bd9Sstevel@tonic-gate 	 * One of these lock files is created for each zone (when needed).
8607c478bd9Sstevel@tonic-gate 	 * The lock files are not cleaned up (except on system reboot),
8617c478bd9Sstevel@tonic-gate 	 * but since there is only one per zone, there is no resource
8627c478bd9Sstevel@tonic-gate 	 * starvation issue.
8637c478bd9Sstevel@tonic-gate 	 */
8647c478bd9Sstevel@tonic-gate 	(void) snprintf(pathbuf, sizeof (pathbuf), "%s/%s.zoneadm.lock",
8657c478bd9Sstevel@tonic-gate 	    ZONES_TMPDIR, zone_name);
8667c478bd9Sstevel@tonic-gate 	if ((*lockfd = open(pathbuf, O_RDWR|O_CREAT, S_IRUSR|S_IWUSR)) < 0) {
8677c478bd9Sstevel@tonic-gate 		zerror(gettext("could not open %s: %s"), pathbuf,
8687c478bd9Sstevel@tonic-gate 		    strerror(errno));
8697c478bd9Sstevel@tonic-gate 		return (Z_ERR);
8707c478bd9Sstevel@tonic-gate 	}
8717c478bd9Sstevel@tonic-gate 	/*
8727c478bd9Sstevel@tonic-gate 	 * Lock the file to synchronize with other zoneadmds
8737c478bd9Sstevel@tonic-gate 	 */
8747c478bd9Sstevel@tonic-gate 	flock.l_type = F_WRLCK;
8757c478bd9Sstevel@tonic-gate 	flock.l_whence = SEEK_SET;
8767c478bd9Sstevel@tonic-gate 	flock.l_start = (off_t)0;
8777c478bd9Sstevel@tonic-gate 	flock.l_len = (off_t)0;
8787c478bd9Sstevel@tonic-gate 	if (fcntl(*lockfd, F_SETLKW, &flock) < 0) {
8797c478bd9Sstevel@tonic-gate 		zerror(gettext("unable to lock %s: %s"), pathbuf,
8807c478bd9Sstevel@tonic-gate 		    strerror(errno));
8817c478bd9Sstevel@tonic-gate 		release_lock_file(*lockfd);
8827c478bd9Sstevel@tonic-gate 		return (Z_ERR);
8837c478bd9Sstevel@tonic-gate 	}
8847c478bd9Sstevel@tonic-gate 	return (Z_OK);
8857c478bd9Sstevel@tonic-gate }
8867c478bd9Sstevel@tonic-gate 
8877c478bd9Sstevel@tonic-gate static void
8887c478bd9Sstevel@tonic-gate get_doorname(const char *zone_name, char *buffer)
8897c478bd9Sstevel@tonic-gate {
8907c478bd9Sstevel@tonic-gate 	(void) snprintf(buffer, PATH_MAX, ZONE_DOOR_PATH, zone_name);
8917c478bd9Sstevel@tonic-gate }
8927c478bd9Sstevel@tonic-gate 
8937c478bd9Sstevel@tonic-gate /*
8947c478bd9Sstevel@tonic-gate  * system daemons are not audited.  For the global zone, this occurs
8957c478bd9Sstevel@tonic-gate  * "naturally" since init is started with the default audit
8967c478bd9Sstevel@tonic-gate  * characteristics.  Since zoneadmd is a system daemon and it starts
8977c478bd9Sstevel@tonic-gate  * init for a zone, it is necessary to clear out the audit
8987c478bd9Sstevel@tonic-gate  * characteristics inherited from whomever started zoneadmd.  This is
8997c478bd9Sstevel@tonic-gate  * indicated by the audit id, which is set from the ruid parameter of
9007c478bd9Sstevel@tonic-gate  * adt_set_user(), below.
9017c478bd9Sstevel@tonic-gate  */
9027c478bd9Sstevel@tonic-gate 
9037c478bd9Sstevel@tonic-gate static void
9047c478bd9Sstevel@tonic-gate prepare_audit_context()
9057c478bd9Sstevel@tonic-gate {
9067c478bd9Sstevel@tonic-gate 	adt_session_data_t	*ah;
9077c478bd9Sstevel@tonic-gate 	char			*failure = gettext("audit failure: %s");
9087c478bd9Sstevel@tonic-gate 
9097c478bd9Sstevel@tonic-gate 	if (adt_start_session(&ah, NULL, 0)) {
9107c478bd9Sstevel@tonic-gate 		zerror(failure, strerror(errno));
9117c478bd9Sstevel@tonic-gate 		return;
9127c478bd9Sstevel@tonic-gate 	}
9137c478bd9Sstevel@tonic-gate 	if (adt_set_user(ah, ADT_NO_AUDIT, ADT_NO_AUDIT,
9147c478bd9Sstevel@tonic-gate 	    ADT_NO_AUDIT, ADT_NO_AUDIT, NULL, ADT_NEW)) {
9157c478bd9Sstevel@tonic-gate 		zerror(failure, strerror(errno));
9167c478bd9Sstevel@tonic-gate 		(void) adt_end_session(ah);
9177c478bd9Sstevel@tonic-gate 		return;
9187c478bd9Sstevel@tonic-gate 	}
9197c478bd9Sstevel@tonic-gate 	if (adt_set_proc(ah))
9207c478bd9Sstevel@tonic-gate 		zerror(failure, strerror(errno));
9217c478bd9Sstevel@tonic-gate 
9227c478bd9Sstevel@tonic-gate 	(void) adt_end_session(ah);
9237c478bd9Sstevel@tonic-gate }
9247c478bd9Sstevel@tonic-gate 
9257c478bd9Sstevel@tonic-gate static int
9267c478bd9Sstevel@tonic-gate start_zoneadmd(const char *zone_name)
9277c478bd9Sstevel@tonic-gate {
9287c478bd9Sstevel@tonic-gate 	char doorpath[PATH_MAX];
9297c478bd9Sstevel@tonic-gate 	pid_t child_pid;
9307c478bd9Sstevel@tonic-gate 	int error = Z_ERR;
9317c478bd9Sstevel@tonic-gate 	int doorfd, lockfd;
9327c478bd9Sstevel@tonic-gate 	struct door_info info;
9337c478bd9Sstevel@tonic-gate 
9347c478bd9Sstevel@tonic-gate 	get_doorname(zone_name, doorpath);
9357c478bd9Sstevel@tonic-gate 
9367c478bd9Sstevel@tonic-gate 	if (grab_lock_file(zone_name, &lockfd) != Z_OK)
9377c478bd9Sstevel@tonic-gate 		return (Z_ERR);
9387c478bd9Sstevel@tonic-gate 
9397c478bd9Sstevel@tonic-gate 	/*
9407c478bd9Sstevel@tonic-gate 	 * Now that we have the lock, re-confirm that the daemon is
9417c478bd9Sstevel@tonic-gate 	 * *not* up and working fine.  If it is still down, we have a green
9427c478bd9Sstevel@tonic-gate 	 * light to start it.
9437c478bd9Sstevel@tonic-gate 	 */
9447c478bd9Sstevel@tonic-gate 	if ((doorfd = open(doorpath, O_RDONLY)) < 0) {
9457c478bd9Sstevel@tonic-gate 		if (errno != ENOENT) {
9467c478bd9Sstevel@tonic-gate 			zperror(doorpath, B_FALSE);
9477c478bd9Sstevel@tonic-gate 			goto out;
9487c478bd9Sstevel@tonic-gate 		}
9497c478bd9Sstevel@tonic-gate 	} else {
9507c478bd9Sstevel@tonic-gate 		if (door_info(doorfd, &info) == 0 &&
9517c478bd9Sstevel@tonic-gate 		    ((info.di_attributes & DOOR_REVOKED) == 0)) {
9527c478bd9Sstevel@tonic-gate 			error = Z_OK;
9537c478bd9Sstevel@tonic-gate 			(void) close(doorfd);
9547c478bd9Sstevel@tonic-gate 			goto out;
9557c478bd9Sstevel@tonic-gate 		}
9567c478bd9Sstevel@tonic-gate 		(void) close(doorfd);
9577c478bd9Sstevel@tonic-gate 	}
9587c478bd9Sstevel@tonic-gate 
9597c478bd9Sstevel@tonic-gate 	if ((child_pid = fork()) == -1) {
9607c478bd9Sstevel@tonic-gate 		zperror(gettext("could not fork"), B_FALSE);
9617c478bd9Sstevel@tonic-gate 		goto out;
9627c478bd9Sstevel@tonic-gate 	} else if (child_pid == 0) {
9637c478bd9Sstevel@tonic-gate 		/* child process */
9647c478bd9Sstevel@tonic-gate 
9657c478bd9Sstevel@tonic-gate 		prepare_audit_context();
9667c478bd9Sstevel@tonic-gate 
9677c478bd9Sstevel@tonic-gate 		(void) execl("/usr/lib/zones/zoneadmd", "zoneadmd", "-z",
9687c478bd9Sstevel@tonic-gate 		    zone_name, NULL);
9697c478bd9Sstevel@tonic-gate 		zperror(gettext("could not exec zoneadmd"), B_FALSE);
9707c478bd9Sstevel@tonic-gate 		_exit(Z_ERR);
9717c478bd9Sstevel@tonic-gate 	} else {
9727c478bd9Sstevel@tonic-gate 		/* parent process */
9737c478bd9Sstevel@tonic-gate 		pid_t retval;
9747c478bd9Sstevel@tonic-gate 		int pstatus = 0;
9757c478bd9Sstevel@tonic-gate 
9767c478bd9Sstevel@tonic-gate 		do {
9777c478bd9Sstevel@tonic-gate 			retval = waitpid(child_pid, &pstatus, 0);
9787c478bd9Sstevel@tonic-gate 		} while (retval != child_pid);
9797c478bd9Sstevel@tonic-gate 		if (WIFSIGNALED(pstatus) || (WIFEXITED(pstatus) &&
9807c478bd9Sstevel@tonic-gate 		    WEXITSTATUS(pstatus) != 0)) {
9817c478bd9Sstevel@tonic-gate 			zerror(gettext("could not start %s"), "zoneadmd");
9827c478bd9Sstevel@tonic-gate 			goto out;
9837c478bd9Sstevel@tonic-gate 		}
9847c478bd9Sstevel@tonic-gate 	}
9857c478bd9Sstevel@tonic-gate 	error = Z_OK;
9867c478bd9Sstevel@tonic-gate out:
9877c478bd9Sstevel@tonic-gate 	release_lock_file(lockfd);
9887c478bd9Sstevel@tonic-gate 	return (error);
9897c478bd9Sstevel@tonic-gate }
9907c478bd9Sstevel@tonic-gate 
9917c478bd9Sstevel@tonic-gate static int
9927c478bd9Sstevel@tonic-gate ping_zoneadmd(const char *zone_name)
9937c478bd9Sstevel@tonic-gate {
9947c478bd9Sstevel@tonic-gate 	char doorpath[PATH_MAX];
9957c478bd9Sstevel@tonic-gate 	int doorfd;
9967c478bd9Sstevel@tonic-gate 	struct door_info info;
9977c478bd9Sstevel@tonic-gate 
9987c478bd9Sstevel@tonic-gate 	get_doorname(zone_name, doorpath);
9997c478bd9Sstevel@tonic-gate 
10007c478bd9Sstevel@tonic-gate 	if ((doorfd = open(doorpath, O_RDONLY)) < 0) {
10017c478bd9Sstevel@tonic-gate 		return (Z_ERR);
10027c478bd9Sstevel@tonic-gate 	}
10037c478bd9Sstevel@tonic-gate 	if (door_info(doorfd, &info) == 0 &&
10047c478bd9Sstevel@tonic-gate 	    ((info.di_attributes & DOOR_REVOKED) == 0)) {
10057c478bd9Sstevel@tonic-gate 		(void) close(doorfd);
10067c478bd9Sstevel@tonic-gate 		return (Z_OK);
10077c478bd9Sstevel@tonic-gate 	}
10087c478bd9Sstevel@tonic-gate 	(void) close(doorfd);
10097c478bd9Sstevel@tonic-gate 	return (Z_ERR);
10107c478bd9Sstevel@tonic-gate }
10117c478bd9Sstevel@tonic-gate 
10127c478bd9Sstevel@tonic-gate static int
10137c478bd9Sstevel@tonic-gate call_zoneadmd(const char *zone_name, zone_cmd_arg_t *arg)
10147c478bd9Sstevel@tonic-gate {
10157c478bd9Sstevel@tonic-gate 	char doorpath[PATH_MAX];
10167c478bd9Sstevel@tonic-gate 	int doorfd, result;
10177c478bd9Sstevel@tonic-gate 	door_arg_t darg;
10187c478bd9Sstevel@tonic-gate 
10197c478bd9Sstevel@tonic-gate 	zoneid_t zoneid;
10207c478bd9Sstevel@tonic-gate 	uint64_t uniqid = 0;
10217c478bd9Sstevel@tonic-gate 
10227c478bd9Sstevel@tonic-gate 	zone_cmd_rval_t *rvalp;
10237c478bd9Sstevel@tonic-gate 	size_t rlen;
10247c478bd9Sstevel@tonic-gate 	char *cp, *errbuf;
10257c478bd9Sstevel@tonic-gate 
10267c478bd9Sstevel@tonic-gate 	rlen = getpagesize();
10277c478bd9Sstevel@tonic-gate 	if ((rvalp = malloc(rlen)) == NULL) {
10287c478bd9Sstevel@tonic-gate 		zerror(gettext("failed to allocate %lu bytes: %s"), rlen,
10297c478bd9Sstevel@tonic-gate 		    strerror(errno));
10307c478bd9Sstevel@tonic-gate 		return (-1);
10317c478bd9Sstevel@tonic-gate 	}
10327c478bd9Sstevel@tonic-gate 
10337c478bd9Sstevel@tonic-gate 	if ((zoneid = getzoneidbyname(zone_name)) != ZONE_ID_UNDEFINED) {
10347c478bd9Sstevel@tonic-gate 		(void) zone_getattr(zoneid, ZONE_ATTR_UNIQID, &uniqid,
10357c478bd9Sstevel@tonic-gate 		    sizeof (uniqid));
10367c478bd9Sstevel@tonic-gate 	}
10377c478bd9Sstevel@tonic-gate 	arg->uniqid = uniqid;
10387c478bd9Sstevel@tonic-gate 	(void) strlcpy(arg->locale, locale, sizeof (arg->locale));
10397c478bd9Sstevel@tonic-gate 	get_doorname(zone_name, doorpath);
10407c478bd9Sstevel@tonic-gate 
10417c478bd9Sstevel@tonic-gate 	/*
10427c478bd9Sstevel@tonic-gate 	 * Loop trying to start zoneadmd; if something goes seriously
10437c478bd9Sstevel@tonic-gate 	 * wrong we break out and fail.
10447c478bd9Sstevel@tonic-gate 	 */
10457c478bd9Sstevel@tonic-gate 	for (;;) {
10467c478bd9Sstevel@tonic-gate 		if (start_zoneadmd(zone_name) != Z_OK)
10477c478bd9Sstevel@tonic-gate 			break;
10487c478bd9Sstevel@tonic-gate 
10497c478bd9Sstevel@tonic-gate 		if ((doorfd = open(doorpath, O_RDONLY)) < 0) {
10507c478bd9Sstevel@tonic-gate 			zperror(gettext("failed to open zone door"), B_FALSE);
10517c478bd9Sstevel@tonic-gate 			break;
10527c478bd9Sstevel@tonic-gate 		}
10537c478bd9Sstevel@tonic-gate 
10547c478bd9Sstevel@tonic-gate 		darg.data_ptr = (char *)arg;
10557c478bd9Sstevel@tonic-gate 		darg.data_size = sizeof (*arg);
10567c478bd9Sstevel@tonic-gate 		darg.desc_ptr = NULL;
10577c478bd9Sstevel@tonic-gate 		darg.desc_num = 0;
10587c478bd9Sstevel@tonic-gate 		darg.rbuf = (char *)rvalp;
10597c478bd9Sstevel@tonic-gate 		darg.rsize = rlen;
10607c478bd9Sstevel@tonic-gate 		if (door_call(doorfd, &darg) != 0) {
10617c478bd9Sstevel@tonic-gate 			(void) close(doorfd);
10627c478bd9Sstevel@tonic-gate 			/*
10637c478bd9Sstevel@tonic-gate 			 * We'll get EBADF if the door has been revoked.
10647c478bd9Sstevel@tonic-gate 			 */
10657c478bd9Sstevel@tonic-gate 			if (errno != EBADF) {
10667c478bd9Sstevel@tonic-gate 				zperror(gettext("door_call failed"), B_FALSE);
10677c478bd9Sstevel@tonic-gate 				break;
10687c478bd9Sstevel@tonic-gate 			}
10697c478bd9Sstevel@tonic-gate 			continue;	/* take another lap */
10707c478bd9Sstevel@tonic-gate 		}
10717c478bd9Sstevel@tonic-gate 		(void) close(doorfd);
10727c478bd9Sstevel@tonic-gate 
10737c478bd9Sstevel@tonic-gate 		if (darg.data_size == 0) {
10747c478bd9Sstevel@tonic-gate 			/* Door server is going away; kick it again. */
10757c478bd9Sstevel@tonic-gate 			continue;
10767c478bd9Sstevel@tonic-gate 		}
10777c478bd9Sstevel@tonic-gate 
10787c478bd9Sstevel@tonic-gate 		errbuf = rvalp->errbuf;
10797c478bd9Sstevel@tonic-gate 		while (*errbuf != '\0') {
10807c478bd9Sstevel@tonic-gate 			/*
10817c478bd9Sstevel@tonic-gate 			 * Remove any newlines since zerror()
10827c478bd9Sstevel@tonic-gate 			 * will append one automatically.
10837c478bd9Sstevel@tonic-gate 			 */
10847c478bd9Sstevel@tonic-gate 			cp = strchr(errbuf, '\n');
10857c478bd9Sstevel@tonic-gate 			if (cp != NULL)
10867c478bd9Sstevel@tonic-gate 				*cp = '\0';
10877c478bd9Sstevel@tonic-gate 			zerror("%s", errbuf);
10887c478bd9Sstevel@tonic-gate 			if (cp == NULL)
10897c478bd9Sstevel@tonic-gate 				break;
10907c478bd9Sstevel@tonic-gate 			errbuf = cp + 1;
10917c478bd9Sstevel@tonic-gate 		}
10927c478bd9Sstevel@tonic-gate 		result = rvalp->rval == 0 ? 0 : -1;
10937c478bd9Sstevel@tonic-gate 		free(rvalp);
10947c478bd9Sstevel@tonic-gate 		return (result);
10957c478bd9Sstevel@tonic-gate 	}
10967c478bd9Sstevel@tonic-gate 
10977c478bd9Sstevel@tonic-gate 	free(rvalp);
10987c478bd9Sstevel@tonic-gate 	return (-1);
10997c478bd9Sstevel@tonic-gate }
11007c478bd9Sstevel@tonic-gate 
11017c478bd9Sstevel@tonic-gate static int
11027c478bd9Sstevel@tonic-gate ready_func(int argc, char *argv[])
11037c478bd9Sstevel@tonic-gate {
11047c478bd9Sstevel@tonic-gate 	zone_cmd_arg_t zarg;
11057c478bd9Sstevel@tonic-gate 	int arg;
11067c478bd9Sstevel@tonic-gate 
11077c478bd9Sstevel@tonic-gate 	optind = 0;
11087c478bd9Sstevel@tonic-gate 	if ((arg = getopt(argc, argv, "?")) != EOF) {
11097c478bd9Sstevel@tonic-gate 		switch (arg) {
11107c478bd9Sstevel@tonic-gate 		case '?':
11117c478bd9Sstevel@tonic-gate 			sub_usage(SHELP_READY, CMD_READY);
11127c478bd9Sstevel@tonic-gate 			return (optopt == '?' ? Z_OK : Z_USAGE);
11137c478bd9Sstevel@tonic-gate 		default:
11147c478bd9Sstevel@tonic-gate 			sub_usage(SHELP_READY, CMD_READY);
11157c478bd9Sstevel@tonic-gate 			return (Z_USAGE);
11167c478bd9Sstevel@tonic-gate 		}
11177c478bd9Sstevel@tonic-gate 	}
11187c478bd9Sstevel@tonic-gate 	if (argc > optind) {
11197c478bd9Sstevel@tonic-gate 		sub_usage(SHELP_READY, CMD_READY);
11207c478bd9Sstevel@tonic-gate 		return (Z_USAGE);
11217c478bd9Sstevel@tonic-gate 	}
11227c478bd9Sstevel@tonic-gate 	if (sanity_check(target_zone, CMD_READY, B_FALSE, B_FALSE) != Z_OK)
11237c478bd9Sstevel@tonic-gate 		return (Z_ERR);
11247c478bd9Sstevel@tonic-gate 	if (verify_details(CMD_READY) != Z_OK)
11257c478bd9Sstevel@tonic-gate 		return (Z_ERR);
11267c478bd9Sstevel@tonic-gate 
11277c478bd9Sstevel@tonic-gate 	zarg.cmd = Z_READY;
11287c478bd9Sstevel@tonic-gate 	if (call_zoneadmd(target_zone, &zarg) != 0) {
11297c478bd9Sstevel@tonic-gate 		zerror(gettext("call to %s failed"), "zoneadmd");
11307c478bd9Sstevel@tonic-gate 		return (Z_ERR);
11317c478bd9Sstevel@tonic-gate 	}
11327c478bd9Sstevel@tonic-gate 	return (Z_OK);
11337c478bd9Sstevel@tonic-gate }
11347c478bd9Sstevel@tonic-gate 
11357c478bd9Sstevel@tonic-gate static int
11367c478bd9Sstevel@tonic-gate boot_func(int argc, char *argv[])
11377c478bd9Sstevel@tonic-gate {
11387c478bd9Sstevel@tonic-gate 	zone_cmd_arg_t zarg;
11397c478bd9Sstevel@tonic-gate 	int arg;
11407c478bd9Sstevel@tonic-gate 
11417c478bd9Sstevel@tonic-gate 	zarg.bootbuf[0] = '\0';
11427c478bd9Sstevel@tonic-gate 
11437c478bd9Sstevel@tonic-gate 	/*
11447c478bd9Sstevel@tonic-gate 	 * At the current time, the only supported subargument to the
11457c478bd9Sstevel@tonic-gate 	 * "boot" subcommand is "-s" which specifies a single-user boot.
11467c478bd9Sstevel@tonic-gate 	 * In the future, other boot arguments should be supported
11477c478bd9Sstevel@tonic-gate 	 * including "-m" for specifying alternate smf(5) milestones.
11487c478bd9Sstevel@tonic-gate 	 */
11497c478bd9Sstevel@tonic-gate 	optind = 0;
11507c478bd9Sstevel@tonic-gate 	if ((arg = getopt(argc, argv, "?s")) != EOF) {
11517c478bd9Sstevel@tonic-gate 		switch (arg) {
11527c478bd9Sstevel@tonic-gate 		case '?':
11537c478bd9Sstevel@tonic-gate 			sub_usage(SHELP_BOOT, CMD_BOOT);
11547c478bd9Sstevel@tonic-gate 			return (optopt == '?' ? Z_OK : Z_USAGE);
11557c478bd9Sstevel@tonic-gate 		case 's':
11567c478bd9Sstevel@tonic-gate 			(void) strlcpy(zarg.bootbuf, "-s",
11577c478bd9Sstevel@tonic-gate 			    sizeof (zarg.bootbuf));
11587c478bd9Sstevel@tonic-gate 			break;
11597c478bd9Sstevel@tonic-gate 		default:
11607c478bd9Sstevel@tonic-gate 			sub_usage(SHELP_BOOT, CMD_BOOT);
11617c478bd9Sstevel@tonic-gate 			return (Z_USAGE);
11627c478bd9Sstevel@tonic-gate 		}
11637c478bd9Sstevel@tonic-gate 	}
11647c478bd9Sstevel@tonic-gate 	if (argc > optind) {
11657c478bd9Sstevel@tonic-gate 		sub_usage(SHELP_BOOT, CMD_BOOT);
11667c478bd9Sstevel@tonic-gate 		return (Z_USAGE);
11677c478bd9Sstevel@tonic-gate 	}
11687c478bd9Sstevel@tonic-gate 	if (sanity_check(target_zone, CMD_BOOT, B_FALSE, B_FALSE) != Z_OK)
11697c478bd9Sstevel@tonic-gate 		return (Z_ERR);
11707c478bd9Sstevel@tonic-gate 	if (verify_details(CMD_BOOT) != Z_OK)
11717c478bd9Sstevel@tonic-gate 		return (Z_ERR);
11727c478bd9Sstevel@tonic-gate 	zarg.cmd = Z_BOOT;
11737c478bd9Sstevel@tonic-gate 	if (call_zoneadmd(target_zone, &zarg) != 0) {
11747c478bd9Sstevel@tonic-gate 		zerror(gettext("call to %s failed"), "zoneadmd");
11757c478bd9Sstevel@tonic-gate 		return (Z_ERR);
11767c478bd9Sstevel@tonic-gate 	}
11777c478bd9Sstevel@tonic-gate 	return (Z_OK);
11787c478bd9Sstevel@tonic-gate }
11797c478bd9Sstevel@tonic-gate 
11807c478bd9Sstevel@tonic-gate static void
11817c478bd9Sstevel@tonic-gate fake_up_local_zone(zoneid_t zid, zone_entry_t *zeptr)
11827c478bd9Sstevel@tonic-gate {
11837c478bd9Sstevel@tonic-gate 	ssize_t result;
11847c478bd9Sstevel@tonic-gate 
11857c478bd9Sstevel@tonic-gate 	zeptr->zid = zid;
11867c478bd9Sstevel@tonic-gate 	/*
11877c478bd9Sstevel@tonic-gate 	 * Since we're looking up our own (non-global) zone name,
11887c478bd9Sstevel@tonic-gate 	 * we can be assured that it will succeed.
11897c478bd9Sstevel@tonic-gate 	 */
11907c478bd9Sstevel@tonic-gate 	result = getzonenamebyid(zid, zeptr->zname, sizeof (zeptr->zname));
11917c478bd9Sstevel@tonic-gate 	assert(result >= 0);
11927c478bd9Sstevel@tonic-gate 	(void) strlcpy(zeptr->zroot, "/", sizeof (zeptr->zroot));
11937c478bd9Sstevel@tonic-gate 	zeptr->zstate_str = "running";
11947c478bd9Sstevel@tonic-gate }
11957c478bd9Sstevel@tonic-gate 
11967c478bd9Sstevel@tonic-gate static int
11977c478bd9Sstevel@tonic-gate list_func(int argc, char *argv[])
11987c478bd9Sstevel@tonic-gate {
11997c478bd9Sstevel@tonic-gate 	zone_entry_t *zentp, zent;
12007c478bd9Sstevel@tonic-gate 	int arg;
12017c478bd9Sstevel@tonic-gate 	boolean_t output = B_FALSE, verbose = B_FALSE, parsable = B_FALSE;
12027c478bd9Sstevel@tonic-gate 	zone_state_t min_state = ZONE_STATE_RUNNING;
12037c478bd9Sstevel@tonic-gate 	zoneid_t zone_id = getzoneid();
12047c478bd9Sstevel@tonic-gate 
12057c478bd9Sstevel@tonic-gate 	if (target_zone == NULL) {
12067c478bd9Sstevel@tonic-gate 		/* all zones: default view to running but allow override */
12077c478bd9Sstevel@tonic-gate 		optind = 0;
12087c478bd9Sstevel@tonic-gate 		while ((arg = getopt(argc, argv, "?cipv")) != EOF) {
12097c478bd9Sstevel@tonic-gate 			switch (arg) {
12107c478bd9Sstevel@tonic-gate 			case '?':
12117c478bd9Sstevel@tonic-gate 				sub_usage(SHELP_LIST, CMD_LIST);
12127c478bd9Sstevel@tonic-gate 				return (optopt == '?' ? Z_OK : Z_USAGE);
12137c478bd9Sstevel@tonic-gate 			case 'c':
12147c478bd9Sstevel@tonic-gate 				min_state = ZONE_STATE_CONFIGURED;
12157c478bd9Sstevel@tonic-gate 				break;
12167c478bd9Sstevel@tonic-gate 			case 'i':
12177c478bd9Sstevel@tonic-gate 				min_state = ZONE_STATE_INSTALLED;
12187c478bd9Sstevel@tonic-gate 				break;
12197c478bd9Sstevel@tonic-gate 			case 'p':
12207c478bd9Sstevel@tonic-gate 				parsable = B_TRUE;
12217c478bd9Sstevel@tonic-gate 				break;
12227c478bd9Sstevel@tonic-gate 			case 'v':
12237c478bd9Sstevel@tonic-gate 				verbose = B_TRUE;
12247c478bd9Sstevel@tonic-gate 				break;
12257c478bd9Sstevel@tonic-gate 			default:
12267c478bd9Sstevel@tonic-gate 				sub_usage(SHELP_LIST, CMD_LIST);
12277c478bd9Sstevel@tonic-gate 				return (Z_USAGE);
12287c478bd9Sstevel@tonic-gate 			}
12297c478bd9Sstevel@tonic-gate 		}
12307c478bd9Sstevel@tonic-gate 		if (parsable && verbose) {
12317c478bd9Sstevel@tonic-gate 			zerror(gettext("%s -p and -v are mutually exclusive."),
12327c478bd9Sstevel@tonic-gate 			    cmd_to_str(CMD_LIST));
12337c478bd9Sstevel@tonic-gate 			return (Z_ERR);
12347c478bd9Sstevel@tonic-gate 		}
12357c478bd9Sstevel@tonic-gate 		if (zone_id == GLOBAL_ZONEID) {
12367c478bd9Sstevel@tonic-gate 			zone_print_list(min_state, verbose, parsable);
12377c478bd9Sstevel@tonic-gate 		} else {
12387c478bd9Sstevel@tonic-gate 			fake_up_local_zone(zone_id, &zent);
12397c478bd9Sstevel@tonic-gate 			zone_print(&zent, verbose, parsable);
12407c478bd9Sstevel@tonic-gate 		}
12417c478bd9Sstevel@tonic-gate 		return (Z_OK);
12427c478bd9Sstevel@tonic-gate 	}
12437c478bd9Sstevel@tonic-gate 
12447c478bd9Sstevel@tonic-gate 	/*
12457c478bd9Sstevel@tonic-gate 	 * Specific target zone: disallow -i/-c suboptions.
12467c478bd9Sstevel@tonic-gate 	 */
12477c478bd9Sstevel@tonic-gate 	optind = 0;
12487c478bd9Sstevel@tonic-gate 	while ((arg = getopt(argc, argv, "?pv")) != EOF) {
12497c478bd9Sstevel@tonic-gate 		switch (arg) {
12507c478bd9Sstevel@tonic-gate 		case '?':
12517c478bd9Sstevel@tonic-gate 			sub_usage(SHELP_LIST, CMD_LIST);
12527c478bd9Sstevel@tonic-gate 			return (optopt == '?' ? Z_OK : Z_USAGE);
12537c478bd9Sstevel@tonic-gate 		case 'p':
12547c478bd9Sstevel@tonic-gate 			parsable = B_TRUE;
12557c478bd9Sstevel@tonic-gate 			break;
12567c478bd9Sstevel@tonic-gate 		case 'v':
12577c478bd9Sstevel@tonic-gate 			verbose = B_TRUE;
12587c478bd9Sstevel@tonic-gate 			break;
12597c478bd9Sstevel@tonic-gate 		default:
12607c478bd9Sstevel@tonic-gate 			sub_usage(SHELP_LIST, CMD_LIST);
12617c478bd9Sstevel@tonic-gate 			return (Z_USAGE);
12627c478bd9Sstevel@tonic-gate 		}
12637c478bd9Sstevel@tonic-gate 	}
12647c478bd9Sstevel@tonic-gate 	if (parsable && verbose) {
12657c478bd9Sstevel@tonic-gate 		zerror(gettext("%s -p and -v are mutually exclusive."),
12667c478bd9Sstevel@tonic-gate 		    cmd_to_str(CMD_LIST));
12677c478bd9Sstevel@tonic-gate 		return (Z_ERR);
12687c478bd9Sstevel@tonic-gate 	}
12697c478bd9Sstevel@tonic-gate 	if (argc > optind) {
12707c478bd9Sstevel@tonic-gate 		sub_usage(SHELP_LIST, CMD_LIST);
12717c478bd9Sstevel@tonic-gate 		return (Z_USAGE);
12727c478bd9Sstevel@tonic-gate 	}
12737c478bd9Sstevel@tonic-gate 	if (zone_id != GLOBAL_ZONEID) {
12747c478bd9Sstevel@tonic-gate 		fake_up_local_zone(zone_id, &zent);
12757c478bd9Sstevel@tonic-gate 		/*
12767c478bd9Sstevel@tonic-gate 		 * main() will issue a Z_NO_ZONE error if it cannot get an
12777c478bd9Sstevel@tonic-gate 		 * id for target_zone, which in a non-global zone should
12787c478bd9Sstevel@tonic-gate 		 * happen for any zone name except `zonename`.  Thus we
12797c478bd9Sstevel@tonic-gate 		 * assert() that here but don't otherwise check.
12807c478bd9Sstevel@tonic-gate 		 */
12817c478bd9Sstevel@tonic-gate 		assert(strcmp(zent.zname, target_zone) == 0);
12827c478bd9Sstevel@tonic-gate 		zone_print(&zent, verbose, parsable);
12837c478bd9Sstevel@tonic-gate 		output = B_TRUE;
12847c478bd9Sstevel@tonic-gate 	} else if ((zentp = lookup_running_zone(target_zone)) != NULL) {
12857c478bd9Sstevel@tonic-gate 		zone_print(zentp, verbose, parsable);
12867c478bd9Sstevel@tonic-gate 		output = B_TRUE;
12877c478bd9Sstevel@tonic-gate 	} else if (lookup_zone_info(target_zone, &zent) == Z_OK) {
12887c478bd9Sstevel@tonic-gate 		zone_print(&zent, verbose, parsable);
12897c478bd9Sstevel@tonic-gate 		output = B_TRUE;
12907c478bd9Sstevel@tonic-gate 	}
12917c478bd9Sstevel@tonic-gate 	return (output ? Z_OK : Z_ERR);
12927c478bd9Sstevel@tonic-gate }
12937c478bd9Sstevel@tonic-gate 
12947c478bd9Sstevel@tonic-gate static void
12957c478bd9Sstevel@tonic-gate sigterm(int sig)
12967c478bd9Sstevel@tonic-gate {
12977c478bd9Sstevel@tonic-gate 	/*
12987c478bd9Sstevel@tonic-gate 	 * Ignore SIG{INT,TERM}, so we don't end up in an infinite loop,
12997c478bd9Sstevel@tonic-gate 	 * then propagate the signal to our process group.
13007c478bd9Sstevel@tonic-gate 	 */
13017c478bd9Sstevel@tonic-gate 	(void) sigset(SIGINT, SIG_IGN);
13027c478bd9Sstevel@tonic-gate 	(void) sigset(SIGTERM, SIG_IGN);
13037c478bd9Sstevel@tonic-gate 	(void) kill(0, sig);
13047c478bd9Sstevel@tonic-gate 	child_killed = B_TRUE;
13057c478bd9Sstevel@tonic-gate }
13067c478bd9Sstevel@tonic-gate 
13077c478bd9Sstevel@tonic-gate static int
13087c478bd9Sstevel@tonic-gate do_subproc(char *cmdbuf)
13097c478bd9Sstevel@tonic-gate {
13107c478bd9Sstevel@tonic-gate 	char inbuf[1024];	/* arbitrary large amount */
13117c478bd9Sstevel@tonic-gate 	FILE *file;
13127c478bd9Sstevel@tonic-gate 
13137c478bd9Sstevel@tonic-gate 	child_killed = B_FALSE;
13147c478bd9Sstevel@tonic-gate 	/*
13157c478bd9Sstevel@tonic-gate 	 * We use popen(3c) to launch child processes for [un]install;
13167c478bd9Sstevel@tonic-gate 	 * this library call does not return a PID, so we have to kill
13177c478bd9Sstevel@tonic-gate 	 * the whole process group.  To avoid killing our parent, we
13187c478bd9Sstevel@tonic-gate 	 * become a process group leader here.  But doing so can wreak
13197c478bd9Sstevel@tonic-gate 	 * havoc with reading from stdin when launched by a non-job-control
13207c478bd9Sstevel@tonic-gate 	 * shell, so we close stdin and reopen it as /dev/null first.
13217c478bd9Sstevel@tonic-gate 	 */
13227c478bd9Sstevel@tonic-gate 	(void) close(STDIN_FILENO);
13237c478bd9Sstevel@tonic-gate 	(void) open("/dev/null", O_RDONLY);
13247c478bd9Sstevel@tonic-gate 	(void) setpgid(0, 0);
13257c478bd9Sstevel@tonic-gate 	(void) sigset(SIGINT, sigterm);
13267c478bd9Sstevel@tonic-gate 	(void) sigset(SIGTERM, sigterm);
13277c478bd9Sstevel@tonic-gate 	file = popen(cmdbuf, "r");
13287c478bd9Sstevel@tonic-gate 	for (;;) {
13297c478bd9Sstevel@tonic-gate 		if (child_killed || fgets(inbuf, sizeof (inbuf), file) == NULL)
13307c478bd9Sstevel@tonic-gate 			break;
13317c478bd9Sstevel@tonic-gate 		(void) fputs(inbuf, stdout);
13327c478bd9Sstevel@tonic-gate 	}
13337c478bd9Sstevel@tonic-gate 	(void) sigset(SIGINT, SIG_DFL);
13347c478bd9Sstevel@tonic-gate 	(void) sigset(SIGTERM, SIG_DFL);
13357c478bd9Sstevel@tonic-gate 	return (pclose(file));
13367c478bd9Sstevel@tonic-gate }
13377c478bd9Sstevel@tonic-gate 
13387c478bd9Sstevel@tonic-gate static int
13397c478bd9Sstevel@tonic-gate subproc_status(const char *cmd, int status)
13407c478bd9Sstevel@tonic-gate {
13417c478bd9Sstevel@tonic-gate 	if (WIFEXITED(status)) {
13427c478bd9Sstevel@tonic-gate 		int exit_code = WEXITSTATUS(status);
13437c478bd9Sstevel@tonic-gate 
13447c478bd9Sstevel@tonic-gate 		if (exit_code == 0)
13457c478bd9Sstevel@tonic-gate 			return (Z_OK);
13467c478bd9Sstevel@tonic-gate 		zerror(gettext("'%s' failed with exit code %d."), cmd,
13477c478bd9Sstevel@tonic-gate 		    exit_code);
13487c478bd9Sstevel@tonic-gate 	} else if (WIFSIGNALED(status)) {
13497c478bd9Sstevel@tonic-gate 		int signal = WTERMSIG(status);
13507c478bd9Sstevel@tonic-gate 		char sigstr[SIG2STR_MAX];
13517c478bd9Sstevel@tonic-gate 
13527c478bd9Sstevel@tonic-gate 		if (sig2str(signal, sigstr) == 0) {
13537c478bd9Sstevel@tonic-gate 			zerror(gettext("'%s' terminated by signal SIG%s."), cmd,
13547c478bd9Sstevel@tonic-gate 			    sigstr);
13557c478bd9Sstevel@tonic-gate 		} else {
13567c478bd9Sstevel@tonic-gate 			zerror(gettext("'%s' terminated by an unknown signal."),
13577c478bd9Sstevel@tonic-gate 			    cmd);
13587c478bd9Sstevel@tonic-gate 		}
13597c478bd9Sstevel@tonic-gate 	} else {
13607c478bd9Sstevel@tonic-gate 		zerror(gettext("'%s' failed for unknown reasons."), cmd);
13617c478bd9Sstevel@tonic-gate 	}
13627c478bd9Sstevel@tonic-gate 	return (Z_ERR);
13637c478bd9Sstevel@tonic-gate }
13647c478bd9Sstevel@tonic-gate 
13657c478bd9Sstevel@tonic-gate /*
13667c478bd9Sstevel@tonic-gate  * Various sanity checks; make sure:
13677c478bd9Sstevel@tonic-gate  * 1. We're in the global zone.
13687c478bd9Sstevel@tonic-gate  * 2. The calling user has sufficient privilege.
13697c478bd9Sstevel@tonic-gate  * 3. The target zone is neither the global zone nor anything starting with
13707c478bd9Sstevel@tonic-gate  *    "SUNW".
13717c478bd9Sstevel@tonic-gate  * 4a. If we're looking for a 'not running' (i.e., configured or installed)
13727c478bd9Sstevel@tonic-gate  *     zone, the name service knows about it.
13737c478bd9Sstevel@tonic-gate  * 4b. For some operations which expect a zone not to be running, that it is
13747c478bd9Sstevel@tonic-gate  *     not already running (or ready).
13757c478bd9Sstevel@tonic-gate  */
13767c478bd9Sstevel@tonic-gate static int
13777c478bd9Sstevel@tonic-gate sanity_check(char *zone, int cmd_num, boolean_t running,
13787c478bd9Sstevel@tonic-gate     boolean_t unsafe_when_running)
13797c478bd9Sstevel@tonic-gate {
13807c478bd9Sstevel@tonic-gate 	zone_entry_t *zent;
13817c478bd9Sstevel@tonic-gate 	priv_set_t *privset;
13827c478bd9Sstevel@tonic-gate 	zone_state_t state;
13837c478bd9Sstevel@tonic-gate 
13847c478bd9Sstevel@tonic-gate 	if (getzoneid() != GLOBAL_ZONEID) {
13857c478bd9Sstevel@tonic-gate 		zerror(gettext("must be in the global zone to %s a zone."),
13867c478bd9Sstevel@tonic-gate 		    cmd_to_str(cmd_num));
13877c478bd9Sstevel@tonic-gate 		return (Z_ERR);
13887c478bd9Sstevel@tonic-gate 	}
13897c478bd9Sstevel@tonic-gate 
13907c478bd9Sstevel@tonic-gate 	if ((privset = priv_allocset()) == NULL) {
13917c478bd9Sstevel@tonic-gate 		zerror(gettext("%s failed"), "priv_allocset");
13927c478bd9Sstevel@tonic-gate 		return (Z_ERR);
13937c478bd9Sstevel@tonic-gate 	}
13947c478bd9Sstevel@tonic-gate 
13957c478bd9Sstevel@tonic-gate 	if (getppriv(PRIV_EFFECTIVE, privset) != 0) {
13967c478bd9Sstevel@tonic-gate 		zerror(gettext("%s failed"), "getppriv");
13977c478bd9Sstevel@tonic-gate 		priv_freeset(privset);
13987c478bd9Sstevel@tonic-gate 		return (Z_ERR);
13997c478bd9Sstevel@tonic-gate 	}
14007c478bd9Sstevel@tonic-gate 
14017c478bd9Sstevel@tonic-gate 	if (priv_isfullset(privset) == B_FALSE) {
14027c478bd9Sstevel@tonic-gate 		zerror(gettext("only a privileged user may %s a zone."),
14037c478bd9Sstevel@tonic-gate 		    cmd_to_str(cmd_num));
14047c478bd9Sstevel@tonic-gate 		priv_freeset(privset);
14057c478bd9Sstevel@tonic-gate 		return (Z_ERR);
14067c478bd9Sstevel@tonic-gate 	}
14077c478bd9Sstevel@tonic-gate 	priv_freeset(privset);
14087c478bd9Sstevel@tonic-gate 
14097c478bd9Sstevel@tonic-gate 	if (zone == NULL) {
14107c478bd9Sstevel@tonic-gate 		zerror(gettext("no zone specified"));
14117c478bd9Sstevel@tonic-gate 		return (Z_ERR);
14127c478bd9Sstevel@tonic-gate 	}
14137c478bd9Sstevel@tonic-gate 
14147c478bd9Sstevel@tonic-gate 	zent = lookup_running_zone(zone);
14157c478bd9Sstevel@tonic-gate 
14167c478bd9Sstevel@tonic-gate 	if (strcmp(zone, GLOBAL_ZONENAME) == 0) {
14177c478bd9Sstevel@tonic-gate 		assert((zent != NULL) && (zent->zid == GLOBAL_ZONEID));
14187c478bd9Sstevel@tonic-gate 		zerror(gettext("%s operation is invalid for the global zone."),
14197c478bd9Sstevel@tonic-gate 		    cmd_to_str(cmd_num));
14207c478bd9Sstevel@tonic-gate 		return (Z_ERR);
14217c478bd9Sstevel@tonic-gate 	}
14227c478bd9Sstevel@tonic-gate 
14237c478bd9Sstevel@tonic-gate 	if (strncmp(zone, "SUNW", 4) == 0) {
14247c478bd9Sstevel@tonic-gate 		zerror(gettext("%s operation is invalid for zones starting "
14257c478bd9Sstevel@tonic-gate 		    "with SUNW."), cmd_to_str(cmd_num));
14267c478bd9Sstevel@tonic-gate 		return (Z_ERR);
14277c478bd9Sstevel@tonic-gate 	}
14287c478bd9Sstevel@tonic-gate 
14297c478bd9Sstevel@tonic-gate 	/*
14307c478bd9Sstevel@tonic-gate 	 * Look up from the kernel for 'running' zones.
14317c478bd9Sstevel@tonic-gate 	 */
14327c478bd9Sstevel@tonic-gate 	if (running) {
14337c478bd9Sstevel@tonic-gate 		if (zent == NULL) {
14347c478bd9Sstevel@tonic-gate 			zerror(gettext("not running"));
14357c478bd9Sstevel@tonic-gate 			return (Z_ERR);
14367c478bd9Sstevel@tonic-gate 		}
14377c478bd9Sstevel@tonic-gate 	} else {
14387c478bd9Sstevel@tonic-gate 		int err;
14397c478bd9Sstevel@tonic-gate 
14407c478bd9Sstevel@tonic-gate 		if (unsafe_when_running && zent != NULL) {
14417c478bd9Sstevel@tonic-gate 			/* check whether the zone is ready or running */
14427c478bd9Sstevel@tonic-gate 			if ((err = zone_get_state(zent->zname,
14437c478bd9Sstevel@tonic-gate 			    &zent->zstate_num)) != Z_OK) {
14447c478bd9Sstevel@tonic-gate 				errno = err;
14457c478bd9Sstevel@tonic-gate 				zperror2(zent->zname,
14467c478bd9Sstevel@tonic-gate 				    gettext("could not get state"));
14477c478bd9Sstevel@tonic-gate 				/* can't tell, so hedge */
14487c478bd9Sstevel@tonic-gate 				zent->zstate_str = "ready/running";
14497c478bd9Sstevel@tonic-gate 			} else {
14507c478bd9Sstevel@tonic-gate 				zent->zstate_str =
14517c478bd9Sstevel@tonic-gate 				    zone_state_str(zent->zstate_num);
14527c478bd9Sstevel@tonic-gate 			}
14537c478bd9Sstevel@tonic-gate 			zerror(gettext("%s operation is invalid for %s zones."),
14547c478bd9Sstevel@tonic-gate 			    cmd_to_str(cmd_num), zent->zstate_str);
14557c478bd9Sstevel@tonic-gate 			return (Z_ERR);
14567c478bd9Sstevel@tonic-gate 		}
14577c478bd9Sstevel@tonic-gate 		if ((err = zone_get_state(zone, &state)) != Z_OK) {
14587c478bd9Sstevel@tonic-gate 			errno = err;
14597c478bd9Sstevel@tonic-gate 			zperror2(zone, gettext("could not get state"));
14607c478bd9Sstevel@tonic-gate 			return (Z_ERR);
14617c478bd9Sstevel@tonic-gate 		}
14627c478bd9Sstevel@tonic-gate 		switch (cmd_num) {
14637c478bd9Sstevel@tonic-gate 		case CMD_UNINSTALL:
14647c478bd9Sstevel@tonic-gate 			if (state == ZONE_STATE_CONFIGURED) {
14657c478bd9Sstevel@tonic-gate 				zerror(gettext("is already in state '%s'."),
14667c478bd9Sstevel@tonic-gate 				    zone_state_str(ZONE_STATE_CONFIGURED));
14677c478bd9Sstevel@tonic-gate 				return (Z_ERR);
14687c478bd9Sstevel@tonic-gate 			}
14697c478bd9Sstevel@tonic-gate 			break;
14707c478bd9Sstevel@tonic-gate 		case CMD_INSTALL:
14717c478bd9Sstevel@tonic-gate 			if (state == ZONE_STATE_INSTALLED) {
14727c478bd9Sstevel@tonic-gate 				zerror(gettext("is already %s."),
14737c478bd9Sstevel@tonic-gate 				    zone_state_str(ZONE_STATE_INSTALLED));
14747c478bd9Sstevel@tonic-gate 				return (Z_ERR);
14757c478bd9Sstevel@tonic-gate 			} else if (state == ZONE_STATE_INCOMPLETE) {
14767c478bd9Sstevel@tonic-gate 				zerror(gettext("zone is %s; %s required."),
14777c478bd9Sstevel@tonic-gate 				    zone_state_str(ZONE_STATE_INCOMPLETE),
14787c478bd9Sstevel@tonic-gate 				    cmd_to_str(CMD_UNINSTALL));
14797c478bd9Sstevel@tonic-gate 				return (Z_ERR);
14807c478bd9Sstevel@tonic-gate 			}
14817c478bd9Sstevel@tonic-gate 			break;
14827c478bd9Sstevel@tonic-gate 		case CMD_READY:
14837c478bd9Sstevel@tonic-gate 		case CMD_BOOT:
14847c478bd9Sstevel@tonic-gate 			if (state < ZONE_STATE_INSTALLED) {
14857c478bd9Sstevel@tonic-gate 				zerror(gettext("must be %s before %s."),
14867c478bd9Sstevel@tonic-gate 				    zone_state_str(ZONE_STATE_INSTALLED),
14877c478bd9Sstevel@tonic-gate 				    cmd_to_str(cmd_num));
14887c478bd9Sstevel@tonic-gate 				return (Z_ERR);
14897c478bd9Sstevel@tonic-gate 			}
14907c478bd9Sstevel@tonic-gate 			break;
14917c478bd9Sstevel@tonic-gate 		case CMD_VERIFY:
14927c478bd9Sstevel@tonic-gate 			if (state == ZONE_STATE_INCOMPLETE) {
14937c478bd9Sstevel@tonic-gate 				zerror(gettext("zone is %s; %s required."),
14947c478bd9Sstevel@tonic-gate 				    zone_state_str(ZONE_STATE_INCOMPLETE),
14957c478bd9Sstevel@tonic-gate 				    cmd_to_str(CMD_UNINSTALL));
14967c478bd9Sstevel@tonic-gate 				return (Z_ERR);
14977c478bd9Sstevel@tonic-gate 			}
14987c478bd9Sstevel@tonic-gate 			break;
14997c478bd9Sstevel@tonic-gate 		}
15007c478bd9Sstevel@tonic-gate 	}
15017c478bd9Sstevel@tonic-gate 	return (Z_OK);
15027c478bd9Sstevel@tonic-gate }
15037c478bd9Sstevel@tonic-gate 
15047c478bd9Sstevel@tonic-gate static int
15057c478bd9Sstevel@tonic-gate halt_func(int argc, char *argv[])
15067c478bd9Sstevel@tonic-gate {
15077c478bd9Sstevel@tonic-gate 	zone_cmd_arg_t zarg;
15087c478bd9Sstevel@tonic-gate 	int arg;
15097c478bd9Sstevel@tonic-gate 
15107c478bd9Sstevel@tonic-gate 	optind = 0;
15117c478bd9Sstevel@tonic-gate 	if ((arg = getopt(argc, argv, "?")) != EOF) {
15127c478bd9Sstevel@tonic-gate 		switch (arg) {
15137c478bd9Sstevel@tonic-gate 		case '?':
15147c478bd9Sstevel@tonic-gate 			sub_usage(SHELP_HALT, CMD_HALT);
15157c478bd9Sstevel@tonic-gate 			return (optopt == '?' ? Z_OK : Z_USAGE);
15167c478bd9Sstevel@tonic-gate 		default:
15177c478bd9Sstevel@tonic-gate 			sub_usage(SHELP_HALT, CMD_HALT);
15187c478bd9Sstevel@tonic-gate 			return (Z_USAGE);
15197c478bd9Sstevel@tonic-gate 		}
15207c478bd9Sstevel@tonic-gate 	}
15217c478bd9Sstevel@tonic-gate 	if (argc > optind) {
15227c478bd9Sstevel@tonic-gate 		sub_usage(SHELP_HALT, CMD_HALT);
15237c478bd9Sstevel@tonic-gate 		return (Z_USAGE);
15247c478bd9Sstevel@tonic-gate 	}
15257c478bd9Sstevel@tonic-gate 	/*
15267c478bd9Sstevel@tonic-gate 	 * zoneadmd should be the one to decide whether or not to proceed,
15277c478bd9Sstevel@tonic-gate 	 * so even though it seems that the fourth parameter below should
15287c478bd9Sstevel@tonic-gate 	 * perhaps be B_TRUE, it really shouldn't be.
15297c478bd9Sstevel@tonic-gate 	 */
15307c478bd9Sstevel@tonic-gate 	if (sanity_check(target_zone, CMD_HALT, B_FALSE, B_FALSE) != Z_OK)
15317c478bd9Sstevel@tonic-gate 		return (Z_ERR);
15327c478bd9Sstevel@tonic-gate 
15337c478bd9Sstevel@tonic-gate 	zarg.cmd = Z_HALT;
15347c478bd9Sstevel@tonic-gate 	return ((call_zoneadmd(target_zone, &zarg) == 0) ? Z_OK : Z_ERR);
15357c478bd9Sstevel@tonic-gate }
15367c478bd9Sstevel@tonic-gate 
15377c478bd9Sstevel@tonic-gate static int
15387c478bd9Sstevel@tonic-gate reboot_func(int argc, char *argv[])
15397c478bd9Sstevel@tonic-gate {
15407c478bd9Sstevel@tonic-gate 	zone_cmd_arg_t zarg;
15417c478bd9Sstevel@tonic-gate 	int arg;
15427c478bd9Sstevel@tonic-gate 
15437c478bd9Sstevel@tonic-gate 	optind = 0;
15447c478bd9Sstevel@tonic-gate 	if ((arg = getopt(argc, argv, "?")) != EOF) {
15457c478bd9Sstevel@tonic-gate 		switch (arg) {
15467c478bd9Sstevel@tonic-gate 		case '?':
15477c478bd9Sstevel@tonic-gate 			sub_usage(SHELP_REBOOT, CMD_REBOOT);
15487c478bd9Sstevel@tonic-gate 			return (optopt == '?' ? Z_OK : Z_USAGE);
15497c478bd9Sstevel@tonic-gate 		default:
15507c478bd9Sstevel@tonic-gate 			sub_usage(SHELP_REBOOT, CMD_REBOOT);
15517c478bd9Sstevel@tonic-gate 			return (Z_USAGE);
15527c478bd9Sstevel@tonic-gate 		}
15537c478bd9Sstevel@tonic-gate 	}
15547c478bd9Sstevel@tonic-gate 	if (argc > 0) {
15557c478bd9Sstevel@tonic-gate 		sub_usage(SHELP_REBOOT, CMD_REBOOT);
15567c478bd9Sstevel@tonic-gate 		return (Z_USAGE);
15577c478bd9Sstevel@tonic-gate 	}
15587c478bd9Sstevel@tonic-gate 	/*
15597c478bd9Sstevel@tonic-gate 	 * zoneadmd should be the one to decide whether or not to proceed,
15607c478bd9Sstevel@tonic-gate 	 * so even though it seems that the fourth parameter below should
15617c478bd9Sstevel@tonic-gate 	 * perhaps be B_TRUE, it really shouldn't be.
15627c478bd9Sstevel@tonic-gate 	 */
15637c478bd9Sstevel@tonic-gate 	if (sanity_check(target_zone, CMD_REBOOT, B_TRUE, B_FALSE) != Z_OK)
15647c478bd9Sstevel@tonic-gate 		return (Z_ERR);
15657c478bd9Sstevel@tonic-gate 
15667c478bd9Sstevel@tonic-gate 	zarg.cmd = Z_REBOOT;
15677c478bd9Sstevel@tonic-gate 	return ((call_zoneadmd(target_zone, &zarg) == 0) ? Z_OK : Z_ERR);
15687c478bd9Sstevel@tonic-gate }
15697c478bd9Sstevel@tonic-gate 
15707c478bd9Sstevel@tonic-gate static int
15717c478bd9Sstevel@tonic-gate verify_rctls(zone_dochandle_t handle)
15727c478bd9Sstevel@tonic-gate {
15737c478bd9Sstevel@tonic-gate 	struct zone_rctltab rctltab;
15747c478bd9Sstevel@tonic-gate 	size_t rbs = rctlblk_size();
15757c478bd9Sstevel@tonic-gate 	rctlblk_t *rctlblk;
15767c478bd9Sstevel@tonic-gate 	int error = Z_INVAL;
15777c478bd9Sstevel@tonic-gate 
15787c478bd9Sstevel@tonic-gate 	if ((rctlblk = malloc(rbs)) == NULL) {
15797c478bd9Sstevel@tonic-gate 		zerror(gettext("failed to allocate %lu bytes: %s"), rbs,
15807c478bd9Sstevel@tonic-gate 		    strerror(errno));
15817c478bd9Sstevel@tonic-gate 		return (Z_NOMEM);
15827c478bd9Sstevel@tonic-gate 	}
15837c478bd9Sstevel@tonic-gate 
15847c478bd9Sstevel@tonic-gate 	if (zonecfg_setrctlent(handle) != Z_OK) {
15857c478bd9Sstevel@tonic-gate 		zerror(gettext("zonecfg_setrctlent failed"));
15867c478bd9Sstevel@tonic-gate 		free(rctlblk);
15877c478bd9Sstevel@tonic-gate 		return (error);
15887c478bd9Sstevel@tonic-gate 	}
15897c478bd9Sstevel@tonic-gate 
15907c478bd9Sstevel@tonic-gate 	rctltab.zone_rctl_valptr = NULL;
15917c478bd9Sstevel@tonic-gate 	while (zonecfg_getrctlent(handle, &rctltab) == Z_OK) {
15927c478bd9Sstevel@tonic-gate 		struct zone_rctlvaltab *rctlval;
15937c478bd9Sstevel@tonic-gate 		const char *name = rctltab.zone_rctl_name;
15947c478bd9Sstevel@tonic-gate 
15957c478bd9Sstevel@tonic-gate 		if (!zonecfg_is_rctl(name)) {
15967c478bd9Sstevel@tonic-gate 			zerror(gettext("WARNING: Ignoring unrecognized rctl "
15977c478bd9Sstevel@tonic-gate 			    "'%s'."),  name);
15987c478bd9Sstevel@tonic-gate 			zonecfg_free_rctl_value_list(rctltab.zone_rctl_valptr);
15997c478bd9Sstevel@tonic-gate 			rctltab.zone_rctl_valptr = NULL;
16007c478bd9Sstevel@tonic-gate 			continue;
16017c478bd9Sstevel@tonic-gate 		}
16027c478bd9Sstevel@tonic-gate 
16037c478bd9Sstevel@tonic-gate 		for (rctlval = rctltab.zone_rctl_valptr; rctlval != NULL;
16047c478bd9Sstevel@tonic-gate 		    rctlval = rctlval->zone_rctlval_next) {
16057c478bd9Sstevel@tonic-gate 			if (zonecfg_construct_rctlblk(rctlval, rctlblk)
16067c478bd9Sstevel@tonic-gate 			    != Z_OK) {
16077c478bd9Sstevel@tonic-gate 				zerror(gettext("invalid rctl value: "
16087c478bd9Sstevel@tonic-gate 				    "(priv=%s,limit=%s,action%s)"),
16097c478bd9Sstevel@tonic-gate 				    rctlval->zone_rctlval_priv,
16107c478bd9Sstevel@tonic-gate 				    rctlval->zone_rctlval_limit,
16117c478bd9Sstevel@tonic-gate 				    rctlval->zone_rctlval_action);
16127c478bd9Sstevel@tonic-gate 				goto out;
16137c478bd9Sstevel@tonic-gate 			}
16147c478bd9Sstevel@tonic-gate 			if (!zonecfg_valid_rctl(name, rctlblk)) {
16157c478bd9Sstevel@tonic-gate 				zerror(gettext("(priv=%s,limit=%s,action=%s) "
16167c478bd9Sstevel@tonic-gate 				    "is not a valid value for rctl '%s'"),
16177c478bd9Sstevel@tonic-gate 				    rctlval->zone_rctlval_priv,
16187c478bd9Sstevel@tonic-gate 				    rctlval->zone_rctlval_limit,
16197c478bd9Sstevel@tonic-gate 				    rctlval->zone_rctlval_action,
16207c478bd9Sstevel@tonic-gate 				    name);
16217c478bd9Sstevel@tonic-gate 				goto out;
16227c478bd9Sstevel@tonic-gate 			}
16237c478bd9Sstevel@tonic-gate 		}
16247c478bd9Sstevel@tonic-gate 		zonecfg_free_rctl_value_list(rctltab.zone_rctl_valptr);
16257c478bd9Sstevel@tonic-gate 	}
16267c478bd9Sstevel@tonic-gate 	rctltab.zone_rctl_valptr = NULL;
16277c478bd9Sstevel@tonic-gate 	error = Z_OK;
16287c478bd9Sstevel@tonic-gate out:
16297c478bd9Sstevel@tonic-gate 	zonecfg_free_rctl_value_list(rctltab.zone_rctl_valptr);
16307c478bd9Sstevel@tonic-gate 	(void) zonecfg_endrctlent(handle);
16317c478bd9Sstevel@tonic-gate 	free(rctlblk);
16327c478bd9Sstevel@tonic-gate 	return (error);
16337c478bd9Sstevel@tonic-gate }
16347c478bd9Sstevel@tonic-gate 
16357c478bd9Sstevel@tonic-gate static int
16367c478bd9Sstevel@tonic-gate verify_pool(zone_dochandle_t handle)
16377c478bd9Sstevel@tonic-gate {
16387c478bd9Sstevel@tonic-gate 	char poolname[MAXPATHLEN];
16397c478bd9Sstevel@tonic-gate 	pool_conf_t *poolconf;
16407c478bd9Sstevel@tonic-gate 	pool_t *pool;
16417c478bd9Sstevel@tonic-gate 	int status;
16427c478bd9Sstevel@tonic-gate 	int error;
16437c478bd9Sstevel@tonic-gate 
16447c478bd9Sstevel@tonic-gate 	/*
16457c478bd9Sstevel@tonic-gate 	 * This ends up being very similar to the check done in zoneadmd.
16467c478bd9Sstevel@tonic-gate 	 */
16477c478bd9Sstevel@tonic-gate 	error = zonecfg_get_pool(handle, poolname, sizeof (poolname));
16487c478bd9Sstevel@tonic-gate 	if (error == Z_NO_ENTRY || (error == Z_OK && strlen(poolname) == 0)) {
16497c478bd9Sstevel@tonic-gate 		/*
16507c478bd9Sstevel@tonic-gate 		 * No pool specified.
16517c478bd9Sstevel@tonic-gate 		 */
16527c478bd9Sstevel@tonic-gate 		return (0);
16537c478bd9Sstevel@tonic-gate 	}
16547c478bd9Sstevel@tonic-gate 	if (error != Z_OK) {
16557c478bd9Sstevel@tonic-gate 		zperror(gettext("Unable to retrieve pool name from "
16567c478bd9Sstevel@tonic-gate 		    "configuration"), B_TRUE);
16577c478bd9Sstevel@tonic-gate 		return (error);
16587c478bd9Sstevel@tonic-gate 	}
16597c478bd9Sstevel@tonic-gate 	/*
16607c478bd9Sstevel@tonic-gate 	 * Don't do anything if pools aren't enabled.
16617c478bd9Sstevel@tonic-gate 	 */
16627c478bd9Sstevel@tonic-gate 	if (pool_get_status(&status) != PO_SUCCESS || status != POOL_ENABLED) {
16637c478bd9Sstevel@tonic-gate 		zerror(gettext("WARNING: pools facility not active; "
16647c478bd9Sstevel@tonic-gate 		    "zone will not be bound to pool '%s'."), poolname);
16657c478bd9Sstevel@tonic-gate 		return (Z_OK);
16667c478bd9Sstevel@tonic-gate 	}
16677c478bd9Sstevel@tonic-gate 	/*
16687c478bd9Sstevel@tonic-gate 	 * Try to provide a sane error message if the requested pool doesn't
16697c478bd9Sstevel@tonic-gate 	 * exist.  It isn't clear that pools-related failures should
16707c478bd9Sstevel@tonic-gate 	 * necessarily translate to a failure to verify the zone configuration,
16717c478bd9Sstevel@tonic-gate 	 * hence they are not considered errors.
16727c478bd9Sstevel@tonic-gate 	 */
16737c478bd9Sstevel@tonic-gate 	if ((poolconf = pool_conf_alloc()) == NULL) {
16747c478bd9Sstevel@tonic-gate 		zerror(gettext("WARNING: pool_conf_alloc failed; "
16757c478bd9Sstevel@tonic-gate 		    "using default pool"));
16767c478bd9Sstevel@tonic-gate 		return (Z_OK);
16777c478bd9Sstevel@tonic-gate 	}
16787c478bd9Sstevel@tonic-gate 	if (pool_conf_open(poolconf, pool_dynamic_location(), PO_RDONLY) !=
16797c478bd9Sstevel@tonic-gate 	    PO_SUCCESS) {
16807c478bd9Sstevel@tonic-gate 		zerror(gettext("WARNING: pool_conf_open failed; "
16817c478bd9Sstevel@tonic-gate 		    "using default pool"));
16827c478bd9Sstevel@tonic-gate 		pool_conf_free(poolconf);
16837c478bd9Sstevel@tonic-gate 		return (Z_OK);
16847c478bd9Sstevel@tonic-gate 	}
16857c478bd9Sstevel@tonic-gate 	pool = pool_get_pool(poolconf, poolname);
16867c478bd9Sstevel@tonic-gate 	(void) pool_conf_close(poolconf);
16877c478bd9Sstevel@tonic-gate 	pool_conf_free(poolconf);
16887c478bd9Sstevel@tonic-gate 	if (pool == NULL) {
16897c478bd9Sstevel@tonic-gate 		zerror(gettext("WARNING: pool '%s' not found. "
16907c478bd9Sstevel@tonic-gate 		    "using default pool"), poolname);
16917c478bd9Sstevel@tonic-gate 	}
16927c478bd9Sstevel@tonic-gate 
16937c478bd9Sstevel@tonic-gate 	return (Z_OK);
16947c478bd9Sstevel@tonic-gate }
16957c478bd9Sstevel@tonic-gate 
16967c478bd9Sstevel@tonic-gate static int
16977c478bd9Sstevel@tonic-gate verify_filesystems(zone_dochandle_t handle)
16987c478bd9Sstevel@tonic-gate {
16997c478bd9Sstevel@tonic-gate 	int return_code = Z_OK;
17007c478bd9Sstevel@tonic-gate 	struct zone_fstab fstab;
17017c478bd9Sstevel@tonic-gate 	char cmdbuf[MAXPATHLEN];
17027c478bd9Sstevel@tonic-gate 	struct stat st;
17037c478bd9Sstevel@tonic-gate 
17047c478bd9Sstevel@tonic-gate 	/*
17057c478bd9Sstevel@tonic-gate 	 * No need to verify inherit-pkg-dir fs types, as their type is
17067c478bd9Sstevel@tonic-gate 	 * implicitly lofs, which is known.  Therefore, the types are only
17077c478bd9Sstevel@tonic-gate 	 * verified for regular filesystems below.
17087c478bd9Sstevel@tonic-gate 	 *
17097c478bd9Sstevel@tonic-gate 	 * Since the actual mount point is not known until the dependent mounts
17107c478bd9Sstevel@tonic-gate 	 * are performed, we don't attempt any path validation here: that will
17117c478bd9Sstevel@tonic-gate 	 * happen later when zoneadmd actually does the mounts.
17127c478bd9Sstevel@tonic-gate 	 */
17137c478bd9Sstevel@tonic-gate 	if (zonecfg_setfsent(handle) != Z_OK) {
17147c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("cannot verify file-systems: "
17157c478bd9Sstevel@tonic-gate 		    "unable to enumerate mounts\n"));
17167c478bd9Sstevel@tonic-gate 		return (Z_ERR);
17177c478bd9Sstevel@tonic-gate 	}
17187c478bd9Sstevel@tonic-gate 	while (zonecfg_getfsent(handle, &fstab) == Z_OK) {
17197c478bd9Sstevel@tonic-gate 		if (!zonecfg_valid_fs_type(fstab.zone_fs_type)) {
17207c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("cannot verify fs %s: "
17217c478bd9Sstevel@tonic-gate 			    "type %s is not allowed.\n"), fstab.zone_fs_dir,
17227c478bd9Sstevel@tonic-gate 			    fstab.zone_fs_type);
17237c478bd9Sstevel@tonic-gate 			return_code = Z_ERR;
17247c478bd9Sstevel@tonic-gate 			goto next_fs;
17257c478bd9Sstevel@tonic-gate 		}
17267c478bd9Sstevel@tonic-gate 		/*
17277c478bd9Sstevel@tonic-gate 		 * Verify /usr/lib/fs/<fstype>/mount exists.
17287c478bd9Sstevel@tonic-gate 		 */
17297c478bd9Sstevel@tonic-gate 		if (snprintf(cmdbuf, sizeof (cmdbuf), "/usr/lib/fs/%s/mount",
17307c478bd9Sstevel@tonic-gate 		    fstab.zone_fs_type) > sizeof (cmdbuf)) {
17317c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("cannot verify fs %s: "
17327c478bd9Sstevel@tonic-gate 			    "type %s is too long.\n"), fstab.zone_fs_dir,
17337c478bd9Sstevel@tonic-gate 			    fstab.zone_fs_type);
17347c478bd9Sstevel@tonic-gate 			return_code = Z_ERR;
17357c478bd9Sstevel@tonic-gate 			goto next_fs;
17367c478bd9Sstevel@tonic-gate 		}
17377c478bd9Sstevel@tonic-gate 		if (stat(cmdbuf, &st) != 0) {
17387c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("cannot verify fs %s: "
17397c478bd9Sstevel@tonic-gate 			    "can't access %s: %s\n"), fstab.zone_fs_dir,
17407c478bd9Sstevel@tonic-gate 			    cmdbuf, strerror(errno));
17417c478bd9Sstevel@tonic-gate 			return_code = Z_ERR;
17427c478bd9Sstevel@tonic-gate 			goto next_fs;
17437c478bd9Sstevel@tonic-gate 		}
17447c478bd9Sstevel@tonic-gate 		if (!S_ISREG(st.st_mode)) {
17457c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("cannot verify fs %s: "
17467c478bd9Sstevel@tonic-gate 			    "%s is not a regular file\n"), fstab.zone_fs_dir,
17477c478bd9Sstevel@tonic-gate 			    cmdbuf);
17487c478bd9Sstevel@tonic-gate 			return_code = Z_ERR;
17497c478bd9Sstevel@tonic-gate 			goto next_fs;
17507c478bd9Sstevel@tonic-gate 		}
17517c478bd9Sstevel@tonic-gate 		/*
17527c478bd9Sstevel@tonic-gate 		 * Verify /usr/lib/fs/<fstype>/fsck exists iff zone_fs_raw is
17537c478bd9Sstevel@tonic-gate 		 * set.
17547c478bd9Sstevel@tonic-gate 		 */
17557c478bd9Sstevel@tonic-gate 		if (snprintf(cmdbuf, sizeof (cmdbuf), "/usr/lib/fs/%s/fsck",
17567c478bd9Sstevel@tonic-gate 		    fstab.zone_fs_type) > sizeof (cmdbuf)) {
17577c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("cannot verify fs %s: "
17587c478bd9Sstevel@tonic-gate 			    "type %s is too long.\n"), fstab.zone_fs_dir,
17597c478bd9Sstevel@tonic-gate 			    fstab.zone_fs_type);
17607c478bd9Sstevel@tonic-gate 			return_code = Z_ERR;
17617c478bd9Sstevel@tonic-gate 			goto next_fs;
17627c478bd9Sstevel@tonic-gate 		}
17637c478bd9Sstevel@tonic-gate 		if (fstab.zone_fs_raw[0] == '\0' && stat(cmdbuf, &st) == 0) {
17647c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("cannot verify fs %s: "
17657c478bd9Sstevel@tonic-gate 			    "must specify 'raw' device for %s file-systems\n"),
17667c478bd9Sstevel@tonic-gate 			    fstab.zone_fs_dir, fstab.zone_fs_type);
17677c478bd9Sstevel@tonic-gate 			return_code = Z_ERR;
17687c478bd9Sstevel@tonic-gate 			goto next_fs;
17697c478bd9Sstevel@tonic-gate 		}
17707c478bd9Sstevel@tonic-gate 		if (fstab.zone_fs_raw[0] != '\0' &&
17717c478bd9Sstevel@tonic-gate 		    (stat(cmdbuf, &st) != 0 || !S_ISREG(st.st_mode))) {
17727c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("cannot verify fs %s: "
17737c478bd9Sstevel@tonic-gate 			    "'raw' device specified but "
17747c478bd9Sstevel@tonic-gate 			    "no fsck executable exists for %s\n"),
17757c478bd9Sstevel@tonic-gate 			    fstab.zone_fs_dir, fstab.zone_fs_type);
17767c478bd9Sstevel@tonic-gate 			return_code = Z_ERR;
17777c478bd9Sstevel@tonic-gate 			goto next_fs;
17787c478bd9Sstevel@tonic-gate 		}
17797c478bd9Sstevel@tonic-gate next_fs:
17807c478bd9Sstevel@tonic-gate 		zonecfg_free_fs_option_list(fstab.zone_fs_options);
17817c478bd9Sstevel@tonic-gate 	}
17827c478bd9Sstevel@tonic-gate 	(void) zonecfg_endfsent(handle);
17837c478bd9Sstevel@tonic-gate 
17847c478bd9Sstevel@tonic-gate 	return (return_code);
17857c478bd9Sstevel@tonic-gate }
17867c478bd9Sstevel@tonic-gate 
17877c478bd9Sstevel@tonic-gate static int
17887c478bd9Sstevel@tonic-gate verify_details(int cmd_num)
17897c478bd9Sstevel@tonic-gate {
17907c478bd9Sstevel@tonic-gate 	zone_dochandle_t handle;
17917c478bd9Sstevel@tonic-gate 	struct zone_nwiftab nwiftab;
17927c478bd9Sstevel@tonic-gate 	char zonepath[MAXPATHLEN], checkpath[MAXPATHLEN];
17937c478bd9Sstevel@tonic-gate 	int return_code = Z_OK;
17947c478bd9Sstevel@tonic-gate 	int err;
17957c478bd9Sstevel@tonic-gate 
17967c478bd9Sstevel@tonic-gate 	if ((handle = zonecfg_init_handle()) == NULL) {
17977c478bd9Sstevel@tonic-gate 		zperror(cmd_to_str(cmd_num), B_TRUE);
17987c478bd9Sstevel@tonic-gate 		return (Z_ERR);
17997c478bd9Sstevel@tonic-gate 	}
18007c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_get_handle(target_zone, handle)) != Z_OK) {
18017c478bd9Sstevel@tonic-gate 		errno = err;
18027c478bd9Sstevel@tonic-gate 		zperror(cmd_to_str(cmd_num), B_TRUE);
18037c478bd9Sstevel@tonic-gate 		zonecfg_fini_handle(handle);
18047c478bd9Sstevel@tonic-gate 		return (Z_ERR);
18057c478bd9Sstevel@tonic-gate 	}
18067c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_get_zonepath(handle, zonepath, sizeof (zonepath))) !=
18077c478bd9Sstevel@tonic-gate 	    Z_OK) {
18087c478bd9Sstevel@tonic-gate 		errno = err;
18097c478bd9Sstevel@tonic-gate 		zperror(cmd_to_str(cmd_num), B_TRUE);
18107c478bd9Sstevel@tonic-gate 		zonecfg_fini_handle(handle);
18117c478bd9Sstevel@tonic-gate 		return (Z_ERR);
18127c478bd9Sstevel@tonic-gate 	}
18137c478bd9Sstevel@tonic-gate 	/*
18147c478bd9Sstevel@tonic-gate 	 * zonecfg_get_zonepath() gets its data from the XML repository.
18157c478bd9Sstevel@tonic-gate 	 * Verify this against the index file, which is checked first by
18167c478bd9Sstevel@tonic-gate 	 * zone_get_zonepath().  If they don't match, bail out.
18177c478bd9Sstevel@tonic-gate 	 */
18187c478bd9Sstevel@tonic-gate 	if ((err = zone_get_zonepath(target_zone, checkpath,
18197c478bd9Sstevel@tonic-gate 	    sizeof (checkpath))) != Z_OK) {
18207c478bd9Sstevel@tonic-gate 		errno = err;
18217c478bd9Sstevel@tonic-gate 		zperror2(target_zone, gettext("could not get zone path"));
18227c478bd9Sstevel@tonic-gate 		return (Z_ERR);
18237c478bd9Sstevel@tonic-gate 	}
18247c478bd9Sstevel@tonic-gate 	if (strcmp(zonepath, checkpath) != 0) {
18257c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("The XML repository has "
18267c478bd9Sstevel@tonic-gate 		    "zonepath '%s',\nbut the index file has zonepath '%s'.\n"
18277c478bd9Sstevel@tonic-gate 		    "These must match, so fix the incorrect entry.\n"),
18287c478bd9Sstevel@tonic-gate 		    zonepath, checkpath);
18297c478bd9Sstevel@tonic-gate 		return (Z_ERR);
18307c478bd9Sstevel@tonic-gate 	}
18317c478bd9Sstevel@tonic-gate 	if (validate_zonepath(zonepath, cmd_num) != Z_OK) {
18327c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("could not verify zonepath %s "
18337c478bd9Sstevel@tonic-gate 		    "because of the above errors.\n"), zonepath);
18347c478bd9Sstevel@tonic-gate 		return_code = Z_ERR;
18357c478bd9Sstevel@tonic-gate 	}
18367c478bd9Sstevel@tonic-gate 
18377c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_setnwifent(handle)) != Z_OK) {
18387c478bd9Sstevel@tonic-gate 		errno = err;
18397c478bd9Sstevel@tonic-gate 		zperror(cmd_to_str(cmd_num), B_TRUE);
18407c478bd9Sstevel@tonic-gate 		zonecfg_fini_handle(handle);
18417c478bd9Sstevel@tonic-gate 		return (Z_ERR);
18427c478bd9Sstevel@tonic-gate 	}
18437c478bd9Sstevel@tonic-gate 	while (zonecfg_getnwifent(handle, &nwiftab) == Z_OK) {
18447c478bd9Sstevel@tonic-gate 		struct lifreq lifr;
18457c478bd9Sstevel@tonic-gate 		sa_family_t af;
18467c478bd9Sstevel@tonic-gate 		int so, res;
18477c478bd9Sstevel@tonic-gate 
18487c478bd9Sstevel@tonic-gate 		/* skip any loopback interfaces */
18497c478bd9Sstevel@tonic-gate 		if (strcmp(nwiftab.zone_nwif_physical, "lo0") == 0)
18507c478bd9Sstevel@tonic-gate 			continue;
18517c478bd9Sstevel@tonic-gate 		if ((res = zonecfg_valid_net_address(nwiftab.zone_nwif_address,
18527c478bd9Sstevel@tonic-gate 		    &lifr)) != Z_OK) {
18537c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("could not verify %s "
18547c478bd9Sstevel@tonic-gate 			    "%s=%s %s=%s: %s\n"), "net", "address",
18557c478bd9Sstevel@tonic-gate 			    nwiftab.zone_nwif_address, "physical",
18567c478bd9Sstevel@tonic-gate 			    nwiftab.zone_nwif_physical, zonecfg_strerror(res));
18577c478bd9Sstevel@tonic-gate 			return_code = Z_ERR;
18587c478bd9Sstevel@tonic-gate 			continue;
18597c478bd9Sstevel@tonic-gate 		}
18607c478bd9Sstevel@tonic-gate 		af = lifr.lifr_addr.ss_family;
18617c478bd9Sstevel@tonic-gate 		(void) memset(&lifr, 0, sizeof (lifr));
18627c478bd9Sstevel@tonic-gate 		(void) strlcpy(lifr.lifr_name, nwiftab.zone_nwif_physical,
18637c478bd9Sstevel@tonic-gate 		    sizeof (lifr.lifr_name));
18647c478bd9Sstevel@tonic-gate 		lifr.lifr_addr.ss_family = af;
18657c478bd9Sstevel@tonic-gate 		if ((so = socket(af, SOCK_DGRAM, 0)) < 0) {
18667c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("could not verify %s "
18677c478bd9Sstevel@tonic-gate 			    "%s=%s %s=%s: could not get socket: %s\n"), "net",
18687c478bd9Sstevel@tonic-gate 			    "address", nwiftab.zone_nwif_address, "physical",
18697c478bd9Sstevel@tonic-gate 			    nwiftab.zone_nwif_physical, strerror(errno));
18707c478bd9Sstevel@tonic-gate 			return_code = Z_ERR;
18717c478bd9Sstevel@tonic-gate 			continue;
18727c478bd9Sstevel@tonic-gate 		}
18737c478bd9Sstevel@tonic-gate 		if (ioctl(so, SIOCGLIFFLAGS, (caddr_t)&lifr) < 0) {
18747c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
18757c478bd9Sstevel@tonic-gate 			    gettext("could not verify %s %s=%s %s=%s: %s\n"),
18767c478bd9Sstevel@tonic-gate 			    "net", "address", nwiftab.zone_nwif_address,
18777c478bd9Sstevel@tonic-gate 			    "physical", nwiftab.zone_nwif_physical,
18787c478bd9Sstevel@tonic-gate 			    strerror(errno));
18797c478bd9Sstevel@tonic-gate 			return_code = Z_ERR;
18807c478bd9Sstevel@tonic-gate 		}
18817c478bd9Sstevel@tonic-gate 		(void) close(so);
18827c478bd9Sstevel@tonic-gate 	}
18837c478bd9Sstevel@tonic-gate 	(void) zonecfg_endnwifent(handle);
18847c478bd9Sstevel@tonic-gate 
18857c478bd9Sstevel@tonic-gate 	if (verify_filesystems(handle) != Z_OK)
18867c478bd9Sstevel@tonic-gate 		return_code = Z_ERR;
18877c478bd9Sstevel@tonic-gate 	if (verify_rctls(handle) != Z_OK)
18887c478bd9Sstevel@tonic-gate 		return_code = Z_ERR;
18897c478bd9Sstevel@tonic-gate 	if (verify_pool(handle) != Z_OK)
18907c478bd9Sstevel@tonic-gate 		return_code = Z_ERR;
18917c478bd9Sstevel@tonic-gate 	zonecfg_fini_handle(handle);
18927c478bd9Sstevel@tonic-gate 	if (return_code == Z_ERR)
18937c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
18947c478bd9Sstevel@tonic-gate 		    gettext("%s: zone %s failed to verify\n"),
18957c478bd9Sstevel@tonic-gate 		    execname, target_zone);
18967c478bd9Sstevel@tonic-gate 	return (return_code);
18977c478bd9Sstevel@tonic-gate }
18987c478bd9Sstevel@tonic-gate 
18997c478bd9Sstevel@tonic-gate static int
19007c478bd9Sstevel@tonic-gate verify_func(int argc, char *argv[])
19017c478bd9Sstevel@tonic-gate {
19027c478bd9Sstevel@tonic-gate 	int arg;
19037c478bd9Sstevel@tonic-gate 
19047c478bd9Sstevel@tonic-gate 	optind = 0;
19057c478bd9Sstevel@tonic-gate 	if ((arg = getopt(argc, argv, "?")) != EOF) {
19067c478bd9Sstevel@tonic-gate 		switch (arg) {
19077c478bd9Sstevel@tonic-gate 		case '?':
19087c478bd9Sstevel@tonic-gate 			sub_usage(SHELP_VERIFY, CMD_VERIFY);
19097c478bd9Sstevel@tonic-gate 			return (optopt == '?' ? Z_OK : Z_USAGE);
19107c478bd9Sstevel@tonic-gate 		default:
19117c478bd9Sstevel@tonic-gate 			sub_usage(SHELP_VERIFY, CMD_VERIFY);
19127c478bd9Sstevel@tonic-gate 			return (Z_USAGE);
19137c478bd9Sstevel@tonic-gate 		}
19147c478bd9Sstevel@tonic-gate 	}
19157c478bd9Sstevel@tonic-gate 	if (argc > optind) {
19167c478bd9Sstevel@tonic-gate 		sub_usage(SHELP_VERIFY, CMD_VERIFY);
19177c478bd9Sstevel@tonic-gate 		return (Z_USAGE);
19187c478bd9Sstevel@tonic-gate 	}
19197c478bd9Sstevel@tonic-gate 	if (sanity_check(target_zone, CMD_VERIFY, B_FALSE, B_FALSE) != Z_OK)
19207c478bd9Sstevel@tonic-gate 		return (Z_ERR);
19217c478bd9Sstevel@tonic-gate 	return (verify_details(CMD_VERIFY));
19227c478bd9Sstevel@tonic-gate }
19237c478bd9Sstevel@tonic-gate 
19247c478bd9Sstevel@tonic-gate #define	LUCREATEZONE	"/usr/lib/lu/lucreatezone"
19257c478bd9Sstevel@tonic-gate 
19267c478bd9Sstevel@tonic-gate static int
19277c478bd9Sstevel@tonic-gate install_func(int argc, char *argv[])
19287c478bd9Sstevel@tonic-gate {
19297c478bd9Sstevel@tonic-gate 	/* 9: "exec " and " -z " */
19307c478bd9Sstevel@tonic-gate 	char cmdbuf[sizeof (LUCREATEZONE) + ZONENAME_MAX + 9];
19317c478bd9Sstevel@tonic-gate 	int lockfd;
19327c478bd9Sstevel@tonic-gate 	int err, arg;
19337c478bd9Sstevel@tonic-gate 	char zonepath[MAXPATHLEN];
19347c478bd9Sstevel@tonic-gate 	int status;
19357c478bd9Sstevel@tonic-gate 
19367c478bd9Sstevel@tonic-gate 	optind = 0;
19377c478bd9Sstevel@tonic-gate 	if ((arg = getopt(argc, argv, "?")) != EOF) {
19387c478bd9Sstevel@tonic-gate 		switch (arg) {
19397c478bd9Sstevel@tonic-gate 		case '?':
19407c478bd9Sstevel@tonic-gate 			sub_usage(SHELP_INSTALL, CMD_INSTALL);
19417c478bd9Sstevel@tonic-gate 			return (optopt == '?' ? Z_OK : Z_USAGE);
19427c478bd9Sstevel@tonic-gate 		default:
19437c478bd9Sstevel@tonic-gate 			sub_usage(SHELP_INSTALL, CMD_INSTALL);
19447c478bd9Sstevel@tonic-gate 			return (Z_USAGE);
19457c478bd9Sstevel@tonic-gate 		}
19467c478bd9Sstevel@tonic-gate 	}
19477c478bd9Sstevel@tonic-gate 	if (argc > optind) {
19487c478bd9Sstevel@tonic-gate 		sub_usage(SHELP_INSTALL, CMD_INSTALL);
19497c478bd9Sstevel@tonic-gate 		return (Z_USAGE);
19507c478bd9Sstevel@tonic-gate 	}
19517c478bd9Sstevel@tonic-gate 	if (sanity_check(target_zone, CMD_INSTALL, B_FALSE, B_TRUE) != Z_OK)
19527c478bd9Sstevel@tonic-gate 		return (Z_ERR);
19537c478bd9Sstevel@tonic-gate 	if (verify_details(CMD_INSTALL) != Z_OK)
19547c478bd9Sstevel@tonic-gate 		return (Z_ERR);
19557c478bd9Sstevel@tonic-gate 
19567c478bd9Sstevel@tonic-gate 	if (grab_lock_file(target_zone, &lockfd) != Z_OK) {
19577c478bd9Sstevel@tonic-gate 		zerror(gettext("another %s may have an operation in progress."),
19587c478bd9Sstevel@tonic-gate 		    "zoneadmd");
19597c478bd9Sstevel@tonic-gate 		return (Z_ERR);
19607c478bd9Sstevel@tonic-gate 	}
19617c478bd9Sstevel@tonic-gate 	err = zone_set_state(target_zone, ZONE_STATE_INCOMPLETE);
19627c478bd9Sstevel@tonic-gate 	if (err != Z_OK) {
19637c478bd9Sstevel@tonic-gate 		errno = err;
19647c478bd9Sstevel@tonic-gate 		zperror2(target_zone, gettext("could not set state"));
19657c478bd9Sstevel@tonic-gate 		goto done;
19667c478bd9Sstevel@tonic-gate 	}
19677c478bd9Sstevel@tonic-gate 
19687c478bd9Sstevel@tonic-gate 	/*
19697c478bd9Sstevel@tonic-gate 	 * According to the Application Packaging Developer's Guide, a
19707c478bd9Sstevel@tonic-gate 	 * "checkinstall" script when included in a package is executed as
19717c478bd9Sstevel@tonic-gate 	 * the user "install", if such a user exists, or by the user
19727c478bd9Sstevel@tonic-gate 	 * "nobody".  In order to support this dubious behavior, the path
19737c478bd9Sstevel@tonic-gate 	 * to the zone being constructed is opened up during the life of
19747c478bd9Sstevel@tonic-gate 	 * the command laying down the zone's root file system.  Once this
19757c478bd9Sstevel@tonic-gate 	 * has completed, regardless of whether it was successful, the
19767c478bd9Sstevel@tonic-gate 	 * path to the zone is again restricted.
19777c478bd9Sstevel@tonic-gate 	 */
19787c478bd9Sstevel@tonic-gate 	if ((err = zone_get_zonepath(target_zone, zonepath,
19797c478bd9Sstevel@tonic-gate 	    sizeof (zonepath))) != Z_OK) {
19807c478bd9Sstevel@tonic-gate 		errno = err;
19817c478bd9Sstevel@tonic-gate 		zperror2(target_zone, gettext("could not get zone path"));
19827c478bd9Sstevel@tonic-gate 		goto done;
19837c478bd9Sstevel@tonic-gate 	}
19847c478bd9Sstevel@tonic-gate 	if (chmod(zonepath, DEFAULT_DIR_MODE) != 0) {
19857c478bd9Sstevel@tonic-gate 		zperror(zonepath, B_FALSE);
19867c478bd9Sstevel@tonic-gate 		err = Z_ERR;
19877c478bd9Sstevel@tonic-gate 		goto done;
19887c478bd9Sstevel@tonic-gate 	}
19897c478bd9Sstevel@tonic-gate 
19907c478bd9Sstevel@tonic-gate 	/*
19917c478bd9Sstevel@tonic-gate 	 * "exec" the command so that the returned status is that of
19927c478bd9Sstevel@tonic-gate 	 * LUCREATEZONE and not the shell.
19937c478bd9Sstevel@tonic-gate 	 */
19947c478bd9Sstevel@tonic-gate 	(void) snprintf(cmdbuf, sizeof (cmdbuf), "exec " LUCREATEZONE " -z %s",
19957c478bd9Sstevel@tonic-gate 	    target_zone);
19967c478bd9Sstevel@tonic-gate 	status = do_subproc(cmdbuf);
19977c478bd9Sstevel@tonic-gate 	if (chmod(zonepath, S_IRWXU) != 0) {
19987c478bd9Sstevel@tonic-gate 		zperror(zonepath, B_FALSE);
19997c478bd9Sstevel@tonic-gate 		err = Z_ERR;
20007c478bd9Sstevel@tonic-gate 		goto done;
20017c478bd9Sstevel@tonic-gate 	}
20027c478bd9Sstevel@tonic-gate 	if ((err = subproc_status(LUCREATEZONE, status)) != Z_OK)
20037c478bd9Sstevel@tonic-gate 		goto done;
20047c478bd9Sstevel@tonic-gate 
20057c478bd9Sstevel@tonic-gate 	if ((err = zone_set_state(target_zone, ZONE_STATE_INSTALLED)) != Z_OK) {
20067c478bd9Sstevel@tonic-gate 		errno = err;
20077c478bd9Sstevel@tonic-gate 		zperror2(target_zone, gettext("could not set state"));
20087c478bd9Sstevel@tonic-gate 		goto done;
20097c478bd9Sstevel@tonic-gate 	}
20107c478bd9Sstevel@tonic-gate 
20117c478bd9Sstevel@tonic-gate done:
20127c478bd9Sstevel@tonic-gate 	release_lock_file(lockfd);
20137c478bd9Sstevel@tonic-gate 	return ((err == Z_OK) ? Z_OK : Z_ERR);
20147c478bd9Sstevel@tonic-gate }
20157c478bd9Sstevel@tonic-gate 
20167c478bd9Sstevel@tonic-gate /*
20177c478bd9Sstevel@tonic-gate  * On input, TRUE => yes, FALSE => no.
20187c478bd9Sstevel@tonic-gate  * On return, TRUE => 1, FALSE => 0, could not ask => -1.
20197c478bd9Sstevel@tonic-gate  */
20207c478bd9Sstevel@tonic-gate 
20217c478bd9Sstevel@tonic-gate static int
20227c478bd9Sstevel@tonic-gate ask_yesno(boolean_t default_answer, const char *question)
20237c478bd9Sstevel@tonic-gate {
20247c478bd9Sstevel@tonic-gate 	char line[64];	/* should be large enough to answer yes or no */
20257c478bd9Sstevel@tonic-gate 
20267c478bd9Sstevel@tonic-gate 	if (!isatty(STDIN_FILENO))
20277c478bd9Sstevel@tonic-gate 		return (-1);
20287c478bd9Sstevel@tonic-gate 	for (;;) {
20297c478bd9Sstevel@tonic-gate 		(void) printf("%s (%s)? ", question,
20307c478bd9Sstevel@tonic-gate 		    default_answer ? "[y]/n" : "y/[n]");
20317c478bd9Sstevel@tonic-gate 		if (fgets(line, sizeof (line), stdin) == NULL ||
20327c478bd9Sstevel@tonic-gate 		    line[0] == '\n')
20337c478bd9Sstevel@tonic-gate 			return (default_answer ? 1 : 0);
20347c478bd9Sstevel@tonic-gate 		if (tolower(line[0]) == 'y')
20357c478bd9Sstevel@tonic-gate 			return (1);
20367c478bd9Sstevel@tonic-gate 		if (tolower(line[0]) == 'n')
20377c478bd9Sstevel@tonic-gate 			return (0);
20387c478bd9Sstevel@tonic-gate 	}
20397c478bd9Sstevel@tonic-gate }
20407c478bd9Sstevel@tonic-gate 
20417c478bd9Sstevel@tonic-gate #define	RMCOMMAND	"/usr/bin/rm -rf"
20427c478bd9Sstevel@tonic-gate 
20437c478bd9Sstevel@tonic-gate /* ARGSUSED */
20447c478bd9Sstevel@tonic-gate int
20457c478bd9Sstevel@tonic-gate zfm_print(const char *p, void *r) {
20467c478bd9Sstevel@tonic-gate 	zerror("  %s\n", p);
20477c478bd9Sstevel@tonic-gate 	return (0);
20487c478bd9Sstevel@tonic-gate }
20497c478bd9Sstevel@tonic-gate 
20507c478bd9Sstevel@tonic-gate static int
20517c478bd9Sstevel@tonic-gate uninstall_func(int argc, char *argv[])
20527c478bd9Sstevel@tonic-gate {
20537c478bd9Sstevel@tonic-gate 	/* 6: "exec " and " " */
20547c478bd9Sstevel@tonic-gate 	char cmdbuf[sizeof (RMCOMMAND) + MAXPATHLEN + 6];
20557c478bd9Sstevel@tonic-gate 	char line[ZONENAME_MAX + 128];	/* Enough for "Are you sure ..." */
20567c478bd9Sstevel@tonic-gate 	char rootpath[MAXPATHLEN], devpath[MAXPATHLEN];
20577c478bd9Sstevel@tonic-gate 	boolean_t force = B_FALSE;
20587c478bd9Sstevel@tonic-gate 	int lockfd, answer;
20597c478bd9Sstevel@tonic-gate 	int err, arg;
20607c478bd9Sstevel@tonic-gate 	int status;
20617c478bd9Sstevel@tonic-gate 
20627c478bd9Sstevel@tonic-gate 	optind = 0;
20637c478bd9Sstevel@tonic-gate 	while ((arg = getopt(argc, argv, "?F")) != EOF) {
20647c478bd9Sstevel@tonic-gate 		switch (arg) {
20657c478bd9Sstevel@tonic-gate 		case '?':
20667c478bd9Sstevel@tonic-gate 			sub_usage(SHELP_UNINSTALL, CMD_UNINSTALL);
20677c478bd9Sstevel@tonic-gate 			return (optopt == '?' ? Z_OK : Z_USAGE);
20687c478bd9Sstevel@tonic-gate 		case 'F':
20697c478bd9Sstevel@tonic-gate 			force = B_TRUE;
20707c478bd9Sstevel@tonic-gate 			break;
20717c478bd9Sstevel@tonic-gate 		default:
20727c478bd9Sstevel@tonic-gate 			sub_usage(SHELP_UNINSTALL, CMD_UNINSTALL);
20737c478bd9Sstevel@tonic-gate 			return (Z_USAGE);
20747c478bd9Sstevel@tonic-gate 		}
20757c478bd9Sstevel@tonic-gate 	}
20767c478bd9Sstevel@tonic-gate 	if (argc > optind) {
20777c478bd9Sstevel@tonic-gate 		sub_usage(SHELP_UNINSTALL, CMD_UNINSTALL);
20787c478bd9Sstevel@tonic-gate 		return (Z_USAGE);
20797c478bd9Sstevel@tonic-gate 	}
20807c478bd9Sstevel@tonic-gate 
20817c478bd9Sstevel@tonic-gate 	if (sanity_check(target_zone, CMD_UNINSTALL, B_FALSE, B_TRUE) != Z_OK)
20827c478bd9Sstevel@tonic-gate 		return (Z_ERR);
20837c478bd9Sstevel@tonic-gate 
20847c478bd9Sstevel@tonic-gate 	if (!force) {
20857c478bd9Sstevel@tonic-gate 		(void) snprintf(line, sizeof (line),
20867c478bd9Sstevel@tonic-gate 		    gettext("Are you sure you want to %s zone %s"),
20877c478bd9Sstevel@tonic-gate 		    cmd_to_str(CMD_UNINSTALL), target_zone);
20887c478bd9Sstevel@tonic-gate 		if ((answer = ask_yesno(B_FALSE, line)) == 0) {
20897c478bd9Sstevel@tonic-gate 			return (Z_OK);
20907c478bd9Sstevel@tonic-gate 		} else if (answer == -1) {
20917c478bd9Sstevel@tonic-gate 			zerror(gettext("Input not from terminal and -F "
20927c478bd9Sstevel@tonic-gate 			    "not specified: %s not done."),
20937c478bd9Sstevel@tonic-gate 			    cmd_to_str(CMD_UNINSTALL));
20947c478bd9Sstevel@tonic-gate 			return (Z_ERR);
20957c478bd9Sstevel@tonic-gate 		}
20967c478bd9Sstevel@tonic-gate 	}
20977c478bd9Sstevel@tonic-gate 
20987c478bd9Sstevel@tonic-gate 	if ((err = zone_get_zonepath(target_zone, devpath,
20997c478bd9Sstevel@tonic-gate 	    sizeof (devpath))) != Z_OK) {
21007c478bd9Sstevel@tonic-gate 		errno = err;
21017c478bd9Sstevel@tonic-gate 		zperror2(target_zone, gettext("could not get zone path"));
21027c478bd9Sstevel@tonic-gate 		return (Z_ERR);
21037c478bd9Sstevel@tonic-gate 	}
21047c478bd9Sstevel@tonic-gate 	(void) strlcat(devpath, "/dev", sizeof (devpath));
21057c478bd9Sstevel@tonic-gate 	if ((err = zone_get_rootpath(target_zone, rootpath,
21067c478bd9Sstevel@tonic-gate 	    sizeof (rootpath))) != Z_OK) {
21077c478bd9Sstevel@tonic-gate 		errno = err;
21087c478bd9Sstevel@tonic-gate 		zperror2(target_zone, gettext("could not get root path"));
21097c478bd9Sstevel@tonic-gate 		return (Z_ERR);
21107c478bd9Sstevel@tonic-gate 	}
21117c478bd9Sstevel@tonic-gate 
21127c478bd9Sstevel@tonic-gate 	/*
21137c478bd9Sstevel@tonic-gate 	 * If there seems to be a zoneadmd running for this zone, call it
21147c478bd9Sstevel@tonic-gate 	 * to tell it that an uninstall is happening; if all goes well it
21157c478bd9Sstevel@tonic-gate 	 * will then shut itself down.
21167c478bd9Sstevel@tonic-gate 	 */
21177c478bd9Sstevel@tonic-gate 	if (ping_zoneadmd(target_zone) == Z_OK) {
21187c478bd9Sstevel@tonic-gate 		zone_cmd_arg_t zarg;
21197c478bd9Sstevel@tonic-gate 		zarg.cmd = Z_NOTE_UNINSTALLING;
21207c478bd9Sstevel@tonic-gate 		/* we don't care too much if this fails... just plow on */
21217c478bd9Sstevel@tonic-gate 		(void) call_zoneadmd(target_zone, &zarg);
21227c478bd9Sstevel@tonic-gate 	}
21237c478bd9Sstevel@tonic-gate 
21247c478bd9Sstevel@tonic-gate 	if (grab_lock_file(target_zone, &lockfd) != Z_OK) {
21257c478bd9Sstevel@tonic-gate 		zerror(gettext("another %s may have an operation in progress."),
21267c478bd9Sstevel@tonic-gate 		    "zoneadmd");
21277c478bd9Sstevel@tonic-gate 		return (Z_ERR);
21287c478bd9Sstevel@tonic-gate 	}
21297c478bd9Sstevel@tonic-gate 
21307c478bd9Sstevel@tonic-gate 	/* Don't uninstall the zone if anything is mounted there */
21317c478bd9Sstevel@tonic-gate 	err = zonecfg_find_mounts(rootpath, NULL, NULL);
21327c478bd9Sstevel@tonic-gate 	if (err) {
21337c478bd9Sstevel@tonic-gate 		zerror(gettext("These file-systems are mounted on "
21347c478bd9Sstevel@tonic-gate 			"subdirectories of %s.\n"), rootpath);
21357c478bd9Sstevel@tonic-gate 		(void) zonecfg_find_mounts(rootpath, zfm_print, NULL);
21367c478bd9Sstevel@tonic-gate 		return (Z_ERR);
21377c478bd9Sstevel@tonic-gate 	}
21387c478bd9Sstevel@tonic-gate 
21397c478bd9Sstevel@tonic-gate 	err = zone_set_state(target_zone, ZONE_STATE_INCOMPLETE);
21407c478bd9Sstevel@tonic-gate 	if (err != Z_OK) {
21417c478bd9Sstevel@tonic-gate 		errno = err;
21427c478bd9Sstevel@tonic-gate 		zperror2(target_zone, gettext("could not set state"));
21437c478bd9Sstevel@tonic-gate 		goto bad;
21447c478bd9Sstevel@tonic-gate 	}
21457c478bd9Sstevel@tonic-gate 
21467c478bd9Sstevel@tonic-gate 	/*
21477c478bd9Sstevel@tonic-gate 	 * "exec" the command so that the returned status is that of
21487c478bd9Sstevel@tonic-gate 	 * RMCOMMAND and not the shell.
21497c478bd9Sstevel@tonic-gate 	 */
21507c478bd9Sstevel@tonic-gate 	(void) snprintf(cmdbuf, sizeof (cmdbuf), "exec " RMCOMMAND " %s",
21517c478bd9Sstevel@tonic-gate 	    devpath);
21527c478bd9Sstevel@tonic-gate 	status = do_subproc(cmdbuf);
21537c478bd9Sstevel@tonic-gate 	if ((err = subproc_status(RMCOMMAND, status)) != Z_OK)
21547c478bd9Sstevel@tonic-gate 		goto bad;
21557c478bd9Sstevel@tonic-gate 	(void) snprintf(cmdbuf, sizeof (cmdbuf), "exec " RMCOMMAND " %s",
21567c478bd9Sstevel@tonic-gate 	    rootpath);
21577c478bd9Sstevel@tonic-gate 	status = do_subproc(cmdbuf);
21587c478bd9Sstevel@tonic-gate 	if ((err = subproc_status(RMCOMMAND, status)) != Z_OK)
21597c478bd9Sstevel@tonic-gate 		goto bad;
21607c478bd9Sstevel@tonic-gate 	err = zone_set_state(target_zone, ZONE_STATE_CONFIGURED);
21617c478bd9Sstevel@tonic-gate 	if (err != Z_OK) {
21627c478bd9Sstevel@tonic-gate 		errno = err;
21637c478bd9Sstevel@tonic-gate 		zperror2(target_zone, gettext("could not reset state"));
21647c478bd9Sstevel@tonic-gate 	}
21657c478bd9Sstevel@tonic-gate bad:
21667c478bd9Sstevel@tonic-gate 	release_lock_file(lockfd);
21677c478bd9Sstevel@tonic-gate 	return (err);
21687c478bd9Sstevel@tonic-gate }
21697c478bd9Sstevel@tonic-gate 
21707c478bd9Sstevel@tonic-gate static int
21717c478bd9Sstevel@tonic-gate help_func(int argc, char *argv[])
21727c478bd9Sstevel@tonic-gate {
21737c478bd9Sstevel@tonic-gate 	int arg, cmd_num;
21747c478bd9Sstevel@tonic-gate 
21757c478bd9Sstevel@tonic-gate 	if (argc == 0) {
21767c478bd9Sstevel@tonic-gate 		(void) usage(B_TRUE);
21777c478bd9Sstevel@tonic-gate 		return (Z_OK);
21787c478bd9Sstevel@tonic-gate 	}
21797c478bd9Sstevel@tonic-gate 	optind = 0;
21807c478bd9Sstevel@tonic-gate 	if ((arg = getopt(argc, argv, "?")) != EOF) {
21817c478bd9Sstevel@tonic-gate 		switch (arg) {
21827c478bd9Sstevel@tonic-gate 		case '?':
21837c478bd9Sstevel@tonic-gate 			sub_usage(SHELP_HELP, CMD_HELP);
21847c478bd9Sstevel@tonic-gate 			return (optopt == '?' ? Z_OK : Z_USAGE);
21857c478bd9Sstevel@tonic-gate 		default:
21867c478bd9Sstevel@tonic-gate 			sub_usage(SHELP_HELP, CMD_HELP);
21877c478bd9Sstevel@tonic-gate 			return (Z_USAGE);
21887c478bd9Sstevel@tonic-gate 		}
21897c478bd9Sstevel@tonic-gate 	}
21907c478bd9Sstevel@tonic-gate 	while (optind < argc) {
21917c478bd9Sstevel@tonic-gate 		if ((cmd_num = cmd_match(argv[optind])) < 0) {
21927c478bd9Sstevel@tonic-gate 			sub_usage(SHELP_HELP, CMD_HELP);
21937c478bd9Sstevel@tonic-gate 			return (Z_USAGE);
21947c478bd9Sstevel@tonic-gate 		}
21957c478bd9Sstevel@tonic-gate 		sub_usage(cmdtab[cmd_num].short_usage, cmd_num);
21967c478bd9Sstevel@tonic-gate 		optind++;
21977c478bd9Sstevel@tonic-gate 	}
21987c478bd9Sstevel@tonic-gate 	return (Z_OK);
21997c478bd9Sstevel@tonic-gate }
22007c478bd9Sstevel@tonic-gate 
22017c478bd9Sstevel@tonic-gate /*
22027c478bd9Sstevel@tonic-gate  * Returns: CMD_MIN thru CMD_MAX on success, -1 on error
22037c478bd9Sstevel@tonic-gate  */
22047c478bd9Sstevel@tonic-gate 
22057c478bd9Sstevel@tonic-gate static int
22067c478bd9Sstevel@tonic-gate cmd_match(char *cmd)
22077c478bd9Sstevel@tonic-gate {
22087c478bd9Sstevel@tonic-gate 	int i;
22097c478bd9Sstevel@tonic-gate 
22107c478bd9Sstevel@tonic-gate 	for (i = CMD_MIN; i <= CMD_MAX; i++) {
22117c478bd9Sstevel@tonic-gate 		/* return only if there is an exact match */
22127c478bd9Sstevel@tonic-gate 		if (strcmp(cmd, cmdtab[i].cmd_name) == 0)
22137c478bd9Sstevel@tonic-gate 			return (cmdtab[i].cmd_num);
22147c478bd9Sstevel@tonic-gate 	}
22157c478bd9Sstevel@tonic-gate 	return (-1);
22167c478bd9Sstevel@tonic-gate }
22177c478bd9Sstevel@tonic-gate 
22187c478bd9Sstevel@tonic-gate static int
22197c478bd9Sstevel@tonic-gate parse_and_run(int argc, char *argv[])
22207c478bd9Sstevel@tonic-gate {
22217c478bd9Sstevel@tonic-gate 	int i = cmd_match(argv[0]);
22227c478bd9Sstevel@tonic-gate 
22237c478bd9Sstevel@tonic-gate 	if (i < 0)
22247c478bd9Sstevel@tonic-gate 		return (usage(B_FALSE));
22257c478bd9Sstevel@tonic-gate 	return (cmdtab[i].handler(argc - 1, &(argv[1])));
22267c478bd9Sstevel@tonic-gate }
22277c478bd9Sstevel@tonic-gate 
22287c478bd9Sstevel@tonic-gate static char *
22297c478bd9Sstevel@tonic-gate get_execbasename(char *execfullname)
22307c478bd9Sstevel@tonic-gate {
22317c478bd9Sstevel@tonic-gate 	char *last_slash, *execbasename;
22327c478bd9Sstevel@tonic-gate 
22337c478bd9Sstevel@tonic-gate 	/* guard against '/' at end of command invocation */
22347c478bd9Sstevel@tonic-gate 	for (;;) {
22357c478bd9Sstevel@tonic-gate 		last_slash = strrchr(execfullname, '/');
22367c478bd9Sstevel@tonic-gate 		if (last_slash == NULL) {
22377c478bd9Sstevel@tonic-gate 			execbasename = execfullname;
22387c478bd9Sstevel@tonic-gate 			break;
22397c478bd9Sstevel@tonic-gate 		} else {
22407c478bd9Sstevel@tonic-gate 			execbasename = last_slash + 1;
22417c478bd9Sstevel@tonic-gate 			if (*execbasename == '\0') {
22427c478bd9Sstevel@tonic-gate 				*last_slash = '\0';
22437c478bd9Sstevel@tonic-gate 				continue;
22447c478bd9Sstevel@tonic-gate 			}
22457c478bd9Sstevel@tonic-gate 			break;
22467c478bd9Sstevel@tonic-gate 		}
22477c478bd9Sstevel@tonic-gate 	}
22487c478bd9Sstevel@tonic-gate 	return (execbasename);
22497c478bd9Sstevel@tonic-gate }
22507c478bd9Sstevel@tonic-gate 
22517c478bd9Sstevel@tonic-gate int
22527c478bd9Sstevel@tonic-gate main(int argc, char **argv)
22537c478bd9Sstevel@tonic-gate {
22547c478bd9Sstevel@tonic-gate 	int arg;
22557c478bd9Sstevel@tonic-gate 	zoneid_t zid;
22567c478bd9Sstevel@tonic-gate 
22577c478bd9Sstevel@tonic-gate 	if ((locale = setlocale(LC_ALL, "")) == NULL)
22587c478bd9Sstevel@tonic-gate 		locale = "C";
22597c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
22607c478bd9Sstevel@tonic-gate 	setbuf(stdout, NULL);
22617c478bd9Sstevel@tonic-gate 	(void) sigset(SIGHUP, SIG_IGN);
22627c478bd9Sstevel@tonic-gate 	execname = get_execbasename(argv[0]);
22637c478bd9Sstevel@tonic-gate 	target_zone = NULL;
22647c478bd9Sstevel@tonic-gate 	if (chdir("/") != 0) {
22657c478bd9Sstevel@tonic-gate 		zerror(gettext("could not change directory to /."));
22667c478bd9Sstevel@tonic-gate 		exit(Z_ERR);
22677c478bd9Sstevel@tonic-gate 	}
22687c478bd9Sstevel@tonic-gate 
22697c478bd9Sstevel@tonic-gate 	while ((arg = getopt(argc, argv, "?z:")) != EOF) {
22707c478bd9Sstevel@tonic-gate 		switch (arg) {
22717c478bd9Sstevel@tonic-gate 		case '?':
22727c478bd9Sstevel@tonic-gate 			return (usage(B_TRUE));
22737c478bd9Sstevel@tonic-gate 		case 'z':
22747c478bd9Sstevel@tonic-gate 			target_zone = optarg;
22757c478bd9Sstevel@tonic-gate 			break;
22767c478bd9Sstevel@tonic-gate 		default:
22777c478bd9Sstevel@tonic-gate 			return (usage(B_FALSE));
22787c478bd9Sstevel@tonic-gate 		}
22797c478bd9Sstevel@tonic-gate 	}
22807c478bd9Sstevel@tonic-gate 
22817c478bd9Sstevel@tonic-gate 	if (optind >= argc)
22827c478bd9Sstevel@tonic-gate 		return (usage(B_FALSE));
22837c478bd9Sstevel@tonic-gate 	if (target_zone != NULL && zone_get_id(target_zone, &zid) != 0) {
22847c478bd9Sstevel@tonic-gate 		errno = Z_NO_ZONE;
22857c478bd9Sstevel@tonic-gate 		zperror(target_zone, B_TRUE);
22867c478bd9Sstevel@tonic-gate 		exit(Z_ERR);
22877c478bd9Sstevel@tonic-gate 	}
22887c478bd9Sstevel@tonic-gate 	return (parse_and_run(argc - optind, &argv[optind]));
22897c478bd9Sstevel@tonic-gate }
2290