xref: /titanic_53/usr/src/cmd/zoneadm/zoneadm.c (revision 5ee84fbde352945ab7b4739c6cb8e251a9dd27e6)
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  */
227e362f58Scomay 
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>
68fa9e4066Sahrens #include <libzfs.h>
697c478bd9Sstevel@tonic-gate 
707c478bd9Sstevel@tonic-gate #include <fcntl.h>
717c478bd9Sstevel@tonic-gate #include <door.h>
727c478bd9Sstevel@tonic-gate #include <macros.h>
737c478bd9Sstevel@tonic-gate #include <libgen.h>
747c478bd9Sstevel@tonic-gate 
757c478bd9Sstevel@tonic-gate #include <pool.h>
767c478bd9Sstevel@tonic-gate #include <sys/pool.h>
777c478bd9Sstevel@tonic-gate 
787c478bd9Sstevel@tonic-gate #define	MAXARGS	8
797c478bd9Sstevel@tonic-gate 
807c478bd9Sstevel@tonic-gate /* Reflects kernel zone entries */
817c478bd9Sstevel@tonic-gate typedef struct zone_entry {
827c478bd9Sstevel@tonic-gate 	zoneid_t	zid;
837c478bd9Sstevel@tonic-gate 	char		zname[ZONENAME_MAX];
847c478bd9Sstevel@tonic-gate 	char		*zstate_str;
857c478bd9Sstevel@tonic-gate 	zone_state_t	zstate_num;
867c478bd9Sstevel@tonic-gate 	char		zroot[MAXPATHLEN];
877c478bd9Sstevel@tonic-gate } zone_entry_t;
887c478bd9Sstevel@tonic-gate 
897c478bd9Sstevel@tonic-gate static zone_entry_t *zents;
907c478bd9Sstevel@tonic-gate static size_t nzents;
917c478bd9Sstevel@tonic-gate 
927c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)		/* should be defined by cc -D */
937c478bd9Sstevel@tonic-gate #define	TEXT_DOMAIN	"SYS_TEST"	/* Use this only if it wasn't */
947c478bd9Sstevel@tonic-gate #endif
957c478bd9Sstevel@tonic-gate 
967c478bd9Sstevel@tonic-gate #define	Z_ERR	1
977c478bd9Sstevel@tonic-gate #define	Z_USAGE	2
987c478bd9Sstevel@tonic-gate 
997c478bd9Sstevel@tonic-gate /* 0755 is the default directory mode. */
1007c478bd9Sstevel@tonic-gate #define	DEFAULT_DIR_MODE \
1017c478bd9Sstevel@tonic-gate 	(S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH)
1027c478bd9Sstevel@tonic-gate 
1037c478bd9Sstevel@tonic-gate #define	CMD_HELP	0
1047c478bd9Sstevel@tonic-gate #define	CMD_BOOT	1
1057c478bd9Sstevel@tonic-gate #define	CMD_HALT	2
1067c478bd9Sstevel@tonic-gate #define	CMD_READY	3
1077c478bd9Sstevel@tonic-gate #define	CMD_REBOOT	4
1087c478bd9Sstevel@tonic-gate #define	CMD_LIST	5
1097c478bd9Sstevel@tonic-gate #define	CMD_VERIFY	6
1107c478bd9Sstevel@tonic-gate #define	CMD_INSTALL	7
1117c478bd9Sstevel@tonic-gate #define	CMD_UNINSTALL	8
112108322fbScarlsonj #define	CMD_MOUNT	9
113108322fbScarlsonj #define	CMD_UNMOUNT	10
1147c478bd9Sstevel@tonic-gate 
1157c478bd9Sstevel@tonic-gate #define	CMD_MIN		CMD_HELP
116108322fbScarlsonj #define	CMD_MAX		CMD_UNMOUNT
1177c478bd9Sstevel@tonic-gate 
1187c478bd9Sstevel@tonic-gate struct cmd {
1197c478bd9Sstevel@tonic-gate 	uint_t	cmd_num;				/* command number */
1207c478bd9Sstevel@tonic-gate 	char	*cmd_name;				/* command name */
1217c478bd9Sstevel@tonic-gate 	char	*short_usage;				/* short form help */
1227c478bd9Sstevel@tonic-gate 	int	(*handler)(int argc, char *argv[]);	/* function to call */
1237c478bd9Sstevel@tonic-gate 
1247c478bd9Sstevel@tonic-gate };
1257c478bd9Sstevel@tonic-gate 
1267c478bd9Sstevel@tonic-gate #define	SHELP_HELP	"help"
1277c478bd9Sstevel@tonic-gate #define	SHELP_BOOT	"boot [-s]"
1287c478bd9Sstevel@tonic-gate #define	SHELP_HALT	"halt"
1297c478bd9Sstevel@tonic-gate #define	SHELP_READY	"ready"
1307c478bd9Sstevel@tonic-gate #define	SHELP_REBOOT	"reboot"
1317c478bd9Sstevel@tonic-gate #define	SHELP_LIST	"list [-cipv]"
1327c478bd9Sstevel@tonic-gate #define	SHELP_VERIFY	"verify"
1337c478bd9Sstevel@tonic-gate #define	SHELP_INSTALL	"install"
1347c478bd9Sstevel@tonic-gate #define	SHELP_UNINSTALL	"uninstall [-F]"
1357c478bd9Sstevel@tonic-gate 
1367c478bd9Sstevel@tonic-gate static int help_func(int argc, char *argv[]);
1377c478bd9Sstevel@tonic-gate static int ready_func(int argc, char *argv[]);
1387c478bd9Sstevel@tonic-gate static int boot_func(int argc, char *argv[]);
1397c478bd9Sstevel@tonic-gate static int halt_func(int argc, char *argv[]);
1407c478bd9Sstevel@tonic-gate static int reboot_func(int argc, char *argv[]);
1417c478bd9Sstevel@tonic-gate static int list_func(int argc, char *argv[]);
1427c478bd9Sstevel@tonic-gate static int verify_func(int argc, char *argv[]);
1437c478bd9Sstevel@tonic-gate static int install_func(int argc, char *argv[]);
1447c478bd9Sstevel@tonic-gate static int uninstall_func(int argc, char *argv[]);
145108322fbScarlsonj static int mount_func(int argc, char *argv[]);
146108322fbScarlsonj static int unmount_func(int argc, char *argv[]);
1477c478bd9Sstevel@tonic-gate static int sanity_check(char *zone, int cmd_num, boolean_t running,
1487c478bd9Sstevel@tonic-gate     boolean_t unsafe_when_running);
1497c478bd9Sstevel@tonic-gate static int cmd_match(char *cmd);
1507c478bd9Sstevel@tonic-gate static int verify_details(int);
1517c478bd9Sstevel@tonic-gate 
1527c478bd9Sstevel@tonic-gate static struct cmd cmdtab[] = {
1537c478bd9Sstevel@tonic-gate 	{ CMD_HELP,		"help",		SHELP_HELP,	help_func },
1547c478bd9Sstevel@tonic-gate 	{ CMD_BOOT,		"boot",		SHELP_BOOT,	boot_func },
1557c478bd9Sstevel@tonic-gate 	{ CMD_HALT,		"halt",		SHELP_HALT,	halt_func },
1567c478bd9Sstevel@tonic-gate 	{ CMD_READY,		"ready",	SHELP_READY,	ready_func },
1577c478bd9Sstevel@tonic-gate 	{ CMD_REBOOT,		"reboot",	SHELP_REBOOT,	reboot_func },
1587c478bd9Sstevel@tonic-gate 	{ CMD_LIST,		"list",		SHELP_LIST,	list_func },
1597c478bd9Sstevel@tonic-gate 	{ CMD_VERIFY,		"verify",	SHELP_VERIFY,	verify_func },
1607c478bd9Sstevel@tonic-gate 	{ CMD_INSTALL,		"install",	SHELP_INSTALL,	install_func },
1617c478bd9Sstevel@tonic-gate 	{ CMD_UNINSTALL,	"uninstall",	SHELP_UNINSTALL,
162108322fbScarlsonj 	    uninstall_func },
163108322fbScarlsonj 	/* Private commands for admin/install */
164108322fbScarlsonj 	{ CMD_MOUNT,		"mount",	NULL,		mount_func },
165108322fbScarlsonj 	{ CMD_UNMOUNT,		"unmount",	NULL,		unmount_func }
1667c478bd9Sstevel@tonic-gate };
1677c478bd9Sstevel@tonic-gate 
1687c478bd9Sstevel@tonic-gate /* global variables */
1697c478bd9Sstevel@tonic-gate 
1707c478bd9Sstevel@tonic-gate /* set early in main(), never modified thereafter, used all over the place */
1717c478bd9Sstevel@tonic-gate static char *execname;
1727c478bd9Sstevel@tonic-gate static char *target_zone;
1737c478bd9Sstevel@tonic-gate static char *locale;
1747c478bd9Sstevel@tonic-gate 
1757c478bd9Sstevel@tonic-gate /* used in do_subproc() and signal handler */
1767c478bd9Sstevel@tonic-gate static volatile boolean_t child_killed;
1777c478bd9Sstevel@tonic-gate 
1787c478bd9Sstevel@tonic-gate static char *
1797c478bd9Sstevel@tonic-gate cmd_to_str(int cmd_num)
1807c478bd9Sstevel@tonic-gate {
1817c478bd9Sstevel@tonic-gate 	assert(cmd_num >= CMD_MIN && cmd_num <= CMD_MAX);
1827c478bd9Sstevel@tonic-gate 	return (cmdtab[cmd_num].cmd_name);
1837c478bd9Sstevel@tonic-gate }
1847c478bd9Sstevel@tonic-gate 
1857c478bd9Sstevel@tonic-gate /* This is a separate function because of gettext() wrapping. */
1867c478bd9Sstevel@tonic-gate static char *
1877c478bd9Sstevel@tonic-gate long_help(int cmd_num)
1887c478bd9Sstevel@tonic-gate {
1897e362f58Scomay 	assert(cmd_num >= CMD_MIN && cmd_num <= CMD_MAX);
1907c478bd9Sstevel@tonic-gate 	switch (cmd_num) {
1917c478bd9Sstevel@tonic-gate 		case CMD_HELP:
1927c478bd9Sstevel@tonic-gate 			return (gettext("Print usage message."));
1937c478bd9Sstevel@tonic-gate 		case CMD_BOOT:
1947c478bd9Sstevel@tonic-gate 			return (gettext("Activates (boots) specified zone.  "
1957c478bd9Sstevel@tonic-gate 			    "The -s flag can be used\n\tto boot the zone in "
1967c478bd9Sstevel@tonic-gate 			    "the single-user state."));
1977c478bd9Sstevel@tonic-gate 		case CMD_HALT:
1987c478bd9Sstevel@tonic-gate 			return (gettext("Halts specified zone, bypassing "
1997c478bd9Sstevel@tonic-gate 			    "shutdown scripts and removing runtime\n\t"
2007c478bd9Sstevel@tonic-gate 			    "resources of the zone."));
2017c478bd9Sstevel@tonic-gate 		case CMD_READY:
2027c478bd9Sstevel@tonic-gate 			return (gettext("Prepares a zone for running "
2037c478bd9Sstevel@tonic-gate 			    "applications but does not start any user\n\t"
2047c478bd9Sstevel@tonic-gate 			    "processes in the zone."));
2057c478bd9Sstevel@tonic-gate 		case CMD_REBOOT:
2067c478bd9Sstevel@tonic-gate 			return (gettext("Restarts the zone (equivalent to a "
2077c478bd9Sstevel@tonic-gate 			    "halt / boot sequence).\n\tFails if the zone is "
2087c478bd9Sstevel@tonic-gate 			    "not active."));
2097c478bd9Sstevel@tonic-gate 		case CMD_LIST:
2107c478bd9Sstevel@tonic-gate 			return (gettext("Lists the current zones, or a "
2117c478bd9Sstevel@tonic-gate 			    "specific zone if indicated.  By default,\n\tall "
2127c478bd9Sstevel@tonic-gate 			    "running zones are listed, though this can be "
2137c478bd9Sstevel@tonic-gate 			    "expanded to all\n\tinstalled zones with the -i "
2147c478bd9Sstevel@tonic-gate 			    "option or all configured zones with the\n\t-c "
2157c478bd9Sstevel@tonic-gate 			    "option.  When used with the general -z <zone> "
2167c478bd9Sstevel@tonic-gate 			    "option, lists only the\n\tspecified zone, but "
2177c478bd9Sstevel@tonic-gate 			    "lists it regardless of its state, and the -i "
2187c478bd9Sstevel@tonic-gate 			    "and -c\n\toptions are disallowed.  The -v option "
2197c478bd9Sstevel@tonic-gate 			    "can be used to display verbose\n\tinformation: "
2207c478bd9Sstevel@tonic-gate 			    "zone name, id, current state, root directory and "
2217c478bd9Sstevel@tonic-gate 			    "options.\n\tThe -p option can be used to request "
2227c478bd9Sstevel@tonic-gate 			    "machine-parsable output.  The -v\n\tand -p "
2237c478bd9Sstevel@tonic-gate 			    "options are mutually exclusive.  If neither -v "
2247c478bd9Sstevel@tonic-gate 			    "nor -p is used,\n\tjust the zone name is "
2257c478bd9Sstevel@tonic-gate 			    "listed."));
2267c478bd9Sstevel@tonic-gate 		case CMD_VERIFY:
2277c478bd9Sstevel@tonic-gate 			return (gettext("Check to make sure the configuration "
2287c478bd9Sstevel@tonic-gate 			    "can safely be instantiated\n\ton the machine: "
2297c478bd9Sstevel@tonic-gate 			    "physical network interfaces exist, etc."));
2307c478bd9Sstevel@tonic-gate 		case CMD_INSTALL:
2317c478bd9Sstevel@tonic-gate 			return (gettext("Install the configuration on to the "
2327c478bd9Sstevel@tonic-gate 			    "system."));
2337c478bd9Sstevel@tonic-gate 		case CMD_UNINSTALL:
2347c478bd9Sstevel@tonic-gate 			return (gettext("Uninstall the configuration from the "
2357c478bd9Sstevel@tonic-gate 			    "system.  The -F flag can be used\n\tto force the "
2367c478bd9Sstevel@tonic-gate 			    "action."));
237108322fbScarlsonj 		default:
238108322fbScarlsonj 			return ("");
2397c478bd9Sstevel@tonic-gate 	}
2407c478bd9Sstevel@tonic-gate 	/* NOTREACHED */
2417e362f58Scomay 	return (NULL);
2427c478bd9Sstevel@tonic-gate }
2437c478bd9Sstevel@tonic-gate 
2447c478bd9Sstevel@tonic-gate /*
2457c478bd9Sstevel@tonic-gate  * Called with explicit B_TRUE when help is explicitly requested, B_FALSE for
2467c478bd9Sstevel@tonic-gate  * unexpected errors.
2477c478bd9Sstevel@tonic-gate  */
2487c478bd9Sstevel@tonic-gate 
2497c478bd9Sstevel@tonic-gate static int
2507c478bd9Sstevel@tonic-gate usage(boolean_t explicit)
2517c478bd9Sstevel@tonic-gate {
2527c478bd9Sstevel@tonic-gate 	int i;
2537c478bd9Sstevel@tonic-gate 	FILE *fd = explicit ? stdout : stderr;
2547c478bd9Sstevel@tonic-gate 
2557c478bd9Sstevel@tonic-gate 	(void) fprintf(fd, "%s:\t%s help\n", gettext("usage"), execname);
2567c478bd9Sstevel@tonic-gate 	(void) fprintf(fd, "\t%s [-z <zone>] list\n", execname);
2577c478bd9Sstevel@tonic-gate 	(void) fprintf(fd, "\t%s -z <zone> <%s>\n", execname,
2587c478bd9Sstevel@tonic-gate 	    gettext("subcommand"));
2597c478bd9Sstevel@tonic-gate 	(void) fprintf(fd, "\n%s:\n\n", gettext("Subcommands"));
2607c478bd9Sstevel@tonic-gate 	for (i = CMD_MIN; i <= CMD_MAX; i++) {
261108322fbScarlsonj 		if (cmdtab[i].short_usage == NULL)
262108322fbScarlsonj 			continue;
2637c478bd9Sstevel@tonic-gate 		(void) fprintf(fd, "%s\n", cmdtab[i].short_usage);
2647c478bd9Sstevel@tonic-gate 		if (explicit)
2657c478bd9Sstevel@tonic-gate 			(void) fprintf(fd, "\t%s\n\n", long_help(i));
2667c478bd9Sstevel@tonic-gate 	}
2677c478bd9Sstevel@tonic-gate 	if (!explicit)
2687c478bd9Sstevel@tonic-gate 		(void) fputs("\n", fd);
2697c478bd9Sstevel@tonic-gate 	return (Z_USAGE);
2707c478bd9Sstevel@tonic-gate }
2717c478bd9Sstevel@tonic-gate 
2727c478bd9Sstevel@tonic-gate static void
2737c478bd9Sstevel@tonic-gate sub_usage(char *short_usage, int cmd_num)
2747c478bd9Sstevel@tonic-gate {
2757c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "%s:\t%s\n", gettext("usage"), short_usage);
2767c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "\t%s\n", long_help(cmd_num));
2777c478bd9Sstevel@tonic-gate }
2787c478bd9Sstevel@tonic-gate 
2797c478bd9Sstevel@tonic-gate /*
2807c478bd9Sstevel@tonic-gate  * zperror() is like perror(3c) except that this also prints the executable
2817c478bd9Sstevel@tonic-gate  * name at the start of the message, and takes a boolean indicating whether
2827c478bd9Sstevel@tonic-gate  * to call libc'c strerror() or that from libzonecfg.
2837c478bd9Sstevel@tonic-gate  */
2847c478bd9Sstevel@tonic-gate 
2857c478bd9Sstevel@tonic-gate static void
2867c478bd9Sstevel@tonic-gate zperror(const char *str, boolean_t zonecfg_error)
2877c478bd9Sstevel@tonic-gate {
2887c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "%s: %s: %s\n", execname, str,
2897c478bd9Sstevel@tonic-gate 	    zonecfg_error ? zonecfg_strerror(errno) : strerror(errno));
2907c478bd9Sstevel@tonic-gate }
2917c478bd9Sstevel@tonic-gate 
2927c478bd9Sstevel@tonic-gate /*
2937c478bd9Sstevel@tonic-gate  * zperror2() is very similar to zperror() above, except it also prints a
2947c478bd9Sstevel@tonic-gate  * supplied zone name after the executable.
2957c478bd9Sstevel@tonic-gate  *
2967c478bd9Sstevel@tonic-gate  * All current consumers of this function want libzonecfg's strerror() rather
2977c478bd9Sstevel@tonic-gate  * than libc's; if this ever changes, this function can be made more generic
2987c478bd9Sstevel@tonic-gate  * like zperror() above.
2997c478bd9Sstevel@tonic-gate  */
3007c478bd9Sstevel@tonic-gate 
3017c478bd9Sstevel@tonic-gate static void
3027c478bd9Sstevel@tonic-gate zperror2(const char *zone, const char *str)
3037c478bd9Sstevel@tonic-gate {
3047c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "%s: %s: %s: %s\n", execname, zone, str,
3057c478bd9Sstevel@tonic-gate 	    zonecfg_strerror(errno));
3067c478bd9Sstevel@tonic-gate }
3077c478bd9Sstevel@tonic-gate 
3087c478bd9Sstevel@tonic-gate /* PRINTFLIKE1 */
3097c478bd9Sstevel@tonic-gate static void
3107c478bd9Sstevel@tonic-gate zerror(const char *fmt, ...)
3117c478bd9Sstevel@tonic-gate {
3127c478bd9Sstevel@tonic-gate 	va_list alist;
3137c478bd9Sstevel@tonic-gate 
3147c478bd9Sstevel@tonic-gate 	va_start(alist, fmt);
3157c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "%s: ", execname);
3167c478bd9Sstevel@tonic-gate 	if (target_zone != NULL)
3177c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "zone '%s': ", target_zone);
3187c478bd9Sstevel@tonic-gate 	(void) vfprintf(stderr, fmt, alist);
3197c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "\n");
3207c478bd9Sstevel@tonic-gate 	va_end(alist);
3217c478bd9Sstevel@tonic-gate }
3227c478bd9Sstevel@tonic-gate 
3237c478bd9Sstevel@tonic-gate static void *
3247c478bd9Sstevel@tonic-gate safe_calloc(size_t nelem, size_t elsize)
3257c478bd9Sstevel@tonic-gate {
3267c478bd9Sstevel@tonic-gate 	void *r = calloc(nelem, elsize);
3277c478bd9Sstevel@tonic-gate 
3287c478bd9Sstevel@tonic-gate 	if (r == NULL) {
3297c478bd9Sstevel@tonic-gate 		zerror(gettext("failed to allocate %lu bytes: %s"),
3307c478bd9Sstevel@tonic-gate 		    (ulong_t)nelem * elsize, strerror(errno));
3317c478bd9Sstevel@tonic-gate 		exit(Z_ERR);
3327c478bd9Sstevel@tonic-gate 	}
3337c478bd9Sstevel@tonic-gate 	return (r);
3347c478bd9Sstevel@tonic-gate }
3357c478bd9Sstevel@tonic-gate 
3367c478bd9Sstevel@tonic-gate static void
3377c478bd9Sstevel@tonic-gate zone_print(zone_entry_t *zent, boolean_t verbose, boolean_t parsable)
3387c478bd9Sstevel@tonic-gate {
3397c478bd9Sstevel@tonic-gate 	static boolean_t firsttime = B_TRUE;
3407c478bd9Sstevel@tonic-gate 
3417c478bd9Sstevel@tonic-gate 	assert(!(verbose && parsable));
3427c478bd9Sstevel@tonic-gate 	if (firsttime && verbose) {
3437c478bd9Sstevel@tonic-gate 		firsttime = B_FALSE;
3447c478bd9Sstevel@tonic-gate 		(void) printf("%*s %-16s %-14s %-30s\n", ZONEID_WIDTH, "ID",
3457c478bd9Sstevel@tonic-gate 		    "NAME", "STATUS", "PATH");
3467c478bd9Sstevel@tonic-gate 	}
3477c478bd9Sstevel@tonic-gate 	if (!verbose) {
3487c478bd9Sstevel@tonic-gate 		if (!parsable) {
3497c478bd9Sstevel@tonic-gate 			(void) printf("%s\n", zent->zname);
3507c478bd9Sstevel@tonic-gate 			return;
3517c478bd9Sstevel@tonic-gate 		}
3527c478bd9Sstevel@tonic-gate 		if (zent->zid == ZONE_ID_UNDEFINED)
3537c478bd9Sstevel@tonic-gate 			(void) printf("-");
3547c478bd9Sstevel@tonic-gate 		else
3557c478bd9Sstevel@tonic-gate 			(void) printf("%lu", zent->zid);
3567c478bd9Sstevel@tonic-gate 		(void) printf(":%s:%s:%s\n", zent->zname, zent->zstate_str,
3577c478bd9Sstevel@tonic-gate 		    zent->zroot);
3587c478bd9Sstevel@tonic-gate 		return;
3597c478bd9Sstevel@tonic-gate 	}
3607c478bd9Sstevel@tonic-gate 	if (zent->zstate_str != NULL) {
3617c478bd9Sstevel@tonic-gate 		if (zent->zid == ZONE_ID_UNDEFINED)
3627c478bd9Sstevel@tonic-gate 			(void) printf("%*s", ZONEID_WIDTH, "-");
3637c478bd9Sstevel@tonic-gate 		else
3647c478bd9Sstevel@tonic-gate 			(void) printf("%*lu", ZONEID_WIDTH, zent->zid);
3657c478bd9Sstevel@tonic-gate 		(void) printf(" %-16s %-14s %-30s\n", zent->zname,
3667c478bd9Sstevel@tonic-gate 		    zent->zstate_str, zent->zroot);
3677c478bd9Sstevel@tonic-gate 	}
3687c478bd9Sstevel@tonic-gate }
3697c478bd9Sstevel@tonic-gate 
3707c478bd9Sstevel@tonic-gate static int
371108322fbScarlsonj lookup_zone_info(const char *zone_name, zoneid_t zid, zone_entry_t *zent)
3727c478bd9Sstevel@tonic-gate {
3737c478bd9Sstevel@tonic-gate 	char root[MAXPATHLEN];
3747c478bd9Sstevel@tonic-gate 	int err;
3757c478bd9Sstevel@tonic-gate 
3767c478bd9Sstevel@tonic-gate 	(void) strlcpy(zent->zname, zone_name, sizeof (zent->zname));
3777c478bd9Sstevel@tonic-gate 	(void) strlcpy(zent->zroot, "???", sizeof (zent->zroot));
3787c478bd9Sstevel@tonic-gate 	zent->zstate_str = "???";
3797c478bd9Sstevel@tonic-gate 
380108322fbScarlsonj 	zent->zid = zid;
3817c478bd9Sstevel@tonic-gate 
3827c478bd9Sstevel@tonic-gate 	if ((err = zone_get_zonepath(zent->zname, root, sizeof (root))) !=
3837c478bd9Sstevel@tonic-gate 	    Z_OK) {
3847c478bd9Sstevel@tonic-gate 		errno = err;
3857c478bd9Sstevel@tonic-gate 		zperror2(zent->zname, gettext("could not get zone path"));
3867c478bd9Sstevel@tonic-gate 		return (Z_ERR);
3877c478bd9Sstevel@tonic-gate 	}
3887c478bd9Sstevel@tonic-gate 	(void) strlcpy(zent->zroot, root, sizeof (zent->zroot));
3897c478bd9Sstevel@tonic-gate 
3907c478bd9Sstevel@tonic-gate 	if ((err = zone_get_state(zent->zname, &zent->zstate_num)) != Z_OK) {
3917c478bd9Sstevel@tonic-gate 		errno = err;
3927c478bd9Sstevel@tonic-gate 		zperror2(zent->zname, gettext("could not get state"));
3937c478bd9Sstevel@tonic-gate 		return (Z_ERR);
3947c478bd9Sstevel@tonic-gate 	}
3957c478bd9Sstevel@tonic-gate 	zent->zstate_str = zone_state_str(zent->zstate_num);
3967c478bd9Sstevel@tonic-gate 
3977c478bd9Sstevel@tonic-gate 	return (Z_OK);
3987c478bd9Sstevel@tonic-gate }
3997c478bd9Sstevel@tonic-gate 
4007c478bd9Sstevel@tonic-gate /*
4017c478bd9Sstevel@tonic-gate  * fetch_zents() calls zone_list(2) to find out how many zones are running
4027c478bd9Sstevel@tonic-gate  * (which is stored in the global nzents), then calls zone_list(2) again
4037c478bd9Sstevel@tonic-gate  * to fetch the list of running zones (stored in the global zents).  This
4047c478bd9Sstevel@tonic-gate  * function may be called multiple times, so if zents is already set, we
4057c478bd9Sstevel@tonic-gate  * return immediately to save work.
4067c478bd9Sstevel@tonic-gate  */
4077c478bd9Sstevel@tonic-gate 
4087c478bd9Sstevel@tonic-gate static int
409108322fbScarlsonj fetch_zents(void)
4107c478bd9Sstevel@tonic-gate {
4117c478bd9Sstevel@tonic-gate 	zoneid_t *zids = NULL;
4127c478bd9Sstevel@tonic-gate 	uint_t nzents_saved;
413108322fbScarlsonj 	int i, retv;
414108322fbScarlsonj 	FILE *fp;
415108322fbScarlsonj 	boolean_t inaltroot;
416108322fbScarlsonj 	zone_entry_t *zentp;
4177c478bd9Sstevel@tonic-gate 
4187c478bd9Sstevel@tonic-gate 	if (nzents > 0)
4197c478bd9Sstevel@tonic-gate 		return (Z_OK);
4207c478bd9Sstevel@tonic-gate 
4217c478bd9Sstevel@tonic-gate 	if (zone_list(NULL, &nzents) != 0) {
4227c478bd9Sstevel@tonic-gate 		zperror(gettext("failed to get zoneid list"), B_FALSE);
4237c478bd9Sstevel@tonic-gate 		return (Z_ERR);
4247c478bd9Sstevel@tonic-gate 	}
4257c478bd9Sstevel@tonic-gate 
4267c478bd9Sstevel@tonic-gate again:
4277c478bd9Sstevel@tonic-gate 	if (nzents == 0)
4287c478bd9Sstevel@tonic-gate 		return (Z_OK);
4297c478bd9Sstevel@tonic-gate 
4307c478bd9Sstevel@tonic-gate 	zids = safe_calloc(nzents, sizeof (zoneid_t));
4317c478bd9Sstevel@tonic-gate 	nzents_saved = nzents;
4327c478bd9Sstevel@tonic-gate 
4337c478bd9Sstevel@tonic-gate 	if (zone_list(zids, &nzents) != 0) {
4347c478bd9Sstevel@tonic-gate 		zperror(gettext("failed to get zone list"), B_FALSE);
4357c478bd9Sstevel@tonic-gate 		free(zids);
4367c478bd9Sstevel@tonic-gate 		return (Z_ERR);
4377c478bd9Sstevel@tonic-gate 	}
4387c478bd9Sstevel@tonic-gate 	if (nzents != nzents_saved) {
4397c478bd9Sstevel@tonic-gate 		/* list changed, try again */
4407c478bd9Sstevel@tonic-gate 		free(zids);
4417c478bd9Sstevel@tonic-gate 		goto again;
4427c478bd9Sstevel@tonic-gate 	}
4437c478bd9Sstevel@tonic-gate 
4447c478bd9Sstevel@tonic-gate 	zents = safe_calloc(nzents, sizeof (zone_entry_t));
4457c478bd9Sstevel@tonic-gate 
446108322fbScarlsonj 	inaltroot = zonecfg_in_alt_root();
447108322fbScarlsonj 	if (inaltroot)
448108322fbScarlsonj 		fp = zonecfg_open_scratch("", B_FALSE);
449108322fbScarlsonj 	else
450108322fbScarlsonj 		fp = NULL;
451108322fbScarlsonj 	zentp = zents;
452108322fbScarlsonj 	retv = Z_OK;
4537c478bd9Sstevel@tonic-gate 	for (i = 0; i < nzents; i++) {
4547c478bd9Sstevel@tonic-gate 		char name[ZONENAME_MAX];
455108322fbScarlsonj 		char altname[ZONENAME_MAX];
4567c478bd9Sstevel@tonic-gate 
457108322fbScarlsonj 		if (getzonenamebyid(zids[i], name, sizeof (name)) < 0) {
4587c478bd9Sstevel@tonic-gate 			zperror(gettext("failed to get zone name"), B_FALSE);
459108322fbScarlsonj 			retv = Z_ERR;
460108322fbScarlsonj 			continue;
4617c478bd9Sstevel@tonic-gate 		}
462108322fbScarlsonj 		if (zonecfg_is_scratch(name)) {
463108322fbScarlsonj 			/* Ignore scratch zones by default */
464108322fbScarlsonj 			if (!inaltroot)
465108322fbScarlsonj 				continue;
466108322fbScarlsonj 			if (fp == NULL ||
467108322fbScarlsonj 			    zonecfg_reverse_scratch(fp, name, altname,
468108322fbScarlsonj 			    sizeof (altname), NULL, 0) == -1) {
469*5ee84fbdSgjelinek 				zerror(gettext("could not resolve scratch "
470108322fbScarlsonj 				    "zone %s"), name);
471108322fbScarlsonj 				retv = Z_ERR;
472108322fbScarlsonj 				continue;
473108322fbScarlsonj 			}
474108322fbScarlsonj 			(void) strcpy(name, altname);
475108322fbScarlsonj 		} else {
476108322fbScarlsonj 			/* Ignore non-scratch when in an alternate root */
477108322fbScarlsonj 			if (inaltroot && strcmp(name, GLOBAL_ZONENAME) != 0)
478108322fbScarlsonj 				continue;
479108322fbScarlsonj 		}
480108322fbScarlsonj 		if (lookup_zone_info(name, zids[i], zentp) != Z_OK) {
481108322fbScarlsonj 			zerror(gettext("failed to get zone data"));
482108322fbScarlsonj 			retv = Z_ERR;
483108322fbScarlsonj 			continue;
484108322fbScarlsonj 		}
485108322fbScarlsonj 		zentp++;
486108322fbScarlsonj 	}
487108322fbScarlsonj 	nzents = zentp - zents;
488108322fbScarlsonj 	if (fp != NULL)
489108322fbScarlsonj 		zonecfg_close_scratch(fp);
4907c478bd9Sstevel@tonic-gate 
4917c478bd9Sstevel@tonic-gate 	free(zids);
492108322fbScarlsonj 	return (retv);
4937c478bd9Sstevel@tonic-gate }
4947c478bd9Sstevel@tonic-gate 
495108322fbScarlsonj static int
4967c478bd9Sstevel@tonic-gate zone_print_list(zone_state_t min_state, boolean_t verbose, boolean_t parsable)
4977c478bd9Sstevel@tonic-gate {
4987c478bd9Sstevel@tonic-gate 	int i;
4997c478bd9Sstevel@tonic-gate 	zone_entry_t zent;
5007c478bd9Sstevel@tonic-gate 	FILE *cookie;
5017c478bd9Sstevel@tonic-gate 	char *name;
5027c478bd9Sstevel@tonic-gate 
5037c478bd9Sstevel@tonic-gate 	/*
5047c478bd9Sstevel@tonic-gate 	 * First get the list of running zones from the kernel and print them.
5057c478bd9Sstevel@tonic-gate 	 * If that is all we need, then return.
5067c478bd9Sstevel@tonic-gate 	 */
507108322fbScarlsonj 	if ((i = fetch_zents()) != Z_OK) {
5087c478bd9Sstevel@tonic-gate 		/*
5097c478bd9Sstevel@tonic-gate 		 * No need for error messages; fetch_zents() has already taken
5107c478bd9Sstevel@tonic-gate 		 * care of this.
5117c478bd9Sstevel@tonic-gate 		 */
512108322fbScarlsonj 		return (i);
5137c478bd9Sstevel@tonic-gate 	}
514108322fbScarlsonj 	for (i = 0; i < nzents; i++)
5157c478bd9Sstevel@tonic-gate 		zone_print(&zents[i], verbose, parsable);
5167c478bd9Sstevel@tonic-gate 	if (min_state >= ZONE_STATE_RUNNING)
517108322fbScarlsonj 		return (Z_OK);
5187c478bd9Sstevel@tonic-gate 	/*
5197c478bd9Sstevel@tonic-gate 	 * Next, get the full list of zones from the configuration, skipping
5207c478bd9Sstevel@tonic-gate 	 * any we have already printed.
5217c478bd9Sstevel@tonic-gate 	 */
5227c478bd9Sstevel@tonic-gate 	cookie = setzoneent();
5237c478bd9Sstevel@tonic-gate 	while ((name = getzoneent(cookie)) != NULL) {
5247c478bd9Sstevel@tonic-gate 		for (i = 0; i < nzents; i++) {
5257c478bd9Sstevel@tonic-gate 			if (strcmp(zents[i].zname, name) == 0)
5267c478bd9Sstevel@tonic-gate 				break;
5277c478bd9Sstevel@tonic-gate 		}
5287c478bd9Sstevel@tonic-gate 		if (i < nzents) {
5297c478bd9Sstevel@tonic-gate 			free(name);
5307c478bd9Sstevel@tonic-gate 			continue;
5317c478bd9Sstevel@tonic-gate 		}
532108322fbScarlsonj 		if (lookup_zone_info(name, ZONE_ID_UNDEFINED, &zent) != Z_OK) {
5337c478bd9Sstevel@tonic-gate 			free(name);
5347c478bd9Sstevel@tonic-gate 			continue;
5357c478bd9Sstevel@tonic-gate 		}
5367c478bd9Sstevel@tonic-gate 		free(name);
5377c478bd9Sstevel@tonic-gate 		if (zent.zstate_num >= min_state)
5387c478bd9Sstevel@tonic-gate 			zone_print(&zent, verbose, parsable);
5397c478bd9Sstevel@tonic-gate 	}
5407c478bd9Sstevel@tonic-gate 	endzoneent(cookie);
541108322fbScarlsonj 	return (Z_OK);
5427c478bd9Sstevel@tonic-gate }
5437c478bd9Sstevel@tonic-gate 
5447c478bd9Sstevel@tonic-gate static zone_entry_t *
5457c478bd9Sstevel@tonic-gate lookup_running_zone(char *str)
5467c478bd9Sstevel@tonic-gate {
5477c478bd9Sstevel@tonic-gate 	zoneid_t zoneid;
5487c478bd9Sstevel@tonic-gate 	char *cp;
5497c478bd9Sstevel@tonic-gate 	int i;
5507c478bd9Sstevel@tonic-gate 
5517c478bd9Sstevel@tonic-gate 	if (fetch_zents() != Z_OK)
5527c478bd9Sstevel@tonic-gate 		return (NULL);
5537c478bd9Sstevel@tonic-gate 
5547c478bd9Sstevel@tonic-gate 	for (i = 0; i < nzents; i++) {
5557c478bd9Sstevel@tonic-gate 		if (strcmp(str, zents[i].zname) == 0)
5567c478bd9Sstevel@tonic-gate 			return (&zents[i]);
5577c478bd9Sstevel@tonic-gate 	}
5587c478bd9Sstevel@tonic-gate 	errno = 0;
5597c478bd9Sstevel@tonic-gate 	zoneid = strtol(str, &cp, 0);
5607c478bd9Sstevel@tonic-gate 	if (zoneid < MIN_ZONEID || zoneid > MAX_ZONEID ||
5617c478bd9Sstevel@tonic-gate 	    errno != 0 || *cp != '\0')
5627c478bd9Sstevel@tonic-gate 		return (NULL);
5637c478bd9Sstevel@tonic-gate 	for (i = 0; i < nzents; i++) {
5647c478bd9Sstevel@tonic-gate 		if (zoneid == zents[i].zid)
5657c478bd9Sstevel@tonic-gate 			return (&zents[i]);
5667c478bd9Sstevel@tonic-gate 	}
5677c478bd9Sstevel@tonic-gate 	return (NULL);
5687c478bd9Sstevel@tonic-gate }
5697c478bd9Sstevel@tonic-gate 
5707c478bd9Sstevel@tonic-gate /*
5717c478bd9Sstevel@tonic-gate  * Check a bit in a mode_t: if on is B_TRUE, that bit should be on; if
5727c478bd9Sstevel@tonic-gate  * B_FALSE, it should be off.  Return B_TRUE if the mode is bad (incorrect).
5737c478bd9Sstevel@tonic-gate  */
5747c478bd9Sstevel@tonic-gate static boolean_t
5757c478bd9Sstevel@tonic-gate bad_mode_bit(mode_t mode, mode_t bit, boolean_t on, char *file)
5767c478bd9Sstevel@tonic-gate {
5777c478bd9Sstevel@tonic-gate 	char *str;
5787c478bd9Sstevel@tonic-gate 
5797c478bd9Sstevel@tonic-gate 	assert(bit == S_IRUSR || bit == S_IWUSR || bit == S_IXUSR ||
5807c478bd9Sstevel@tonic-gate 	    bit == S_IRGRP || bit == S_IWGRP || bit == S_IXGRP ||
5817c478bd9Sstevel@tonic-gate 	    bit == S_IROTH || bit == S_IWOTH || bit == S_IXOTH);
5827c478bd9Sstevel@tonic-gate 	/*
5837c478bd9Sstevel@tonic-gate 	 * TRANSLATION_NOTE
5847c478bd9Sstevel@tonic-gate 	 * The strings below will be used as part of a larger message,
5857c478bd9Sstevel@tonic-gate 	 * either:
5867c478bd9Sstevel@tonic-gate 	 * (file name) must be (owner|group|world) (read|writ|execut)able
5877c478bd9Sstevel@tonic-gate 	 * or
5887c478bd9Sstevel@tonic-gate 	 * (file name) must not be (owner|group|world) (read|writ|execut)able
5897c478bd9Sstevel@tonic-gate 	 */
5907c478bd9Sstevel@tonic-gate 	switch (bit) {
5917c478bd9Sstevel@tonic-gate 	case S_IRUSR:
5927c478bd9Sstevel@tonic-gate 		str = gettext("owner readable");
5937c478bd9Sstevel@tonic-gate 		break;
5947c478bd9Sstevel@tonic-gate 	case S_IWUSR:
5957c478bd9Sstevel@tonic-gate 		str = gettext("owner writable");
5967c478bd9Sstevel@tonic-gate 		break;
5977c478bd9Sstevel@tonic-gate 	case S_IXUSR:
5987c478bd9Sstevel@tonic-gate 		str = gettext("owner executable");
5997c478bd9Sstevel@tonic-gate 		break;
6007c478bd9Sstevel@tonic-gate 	case S_IRGRP:
6017c478bd9Sstevel@tonic-gate 		str = gettext("group readable");
6027c478bd9Sstevel@tonic-gate 		break;
6037c478bd9Sstevel@tonic-gate 	case S_IWGRP:
6047c478bd9Sstevel@tonic-gate 		str = gettext("group writable");
6057c478bd9Sstevel@tonic-gate 		break;
6067c478bd9Sstevel@tonic-gate 	case S_IXGRP:
6077c478bd9Sstevel@tonic-gate 		str = gettext("group executable");
6087c478bd9Sstevel@tonic-gate 		break;
6097c478bd9Sstevel@tonic-gate 	case S_IROTH:
6107c478bd9Sstevel@tonic-gate 		str = gettext("world readable");
6117c478bd9Sstevel@tonic-gate 		break;
6127c478bd9Sstevel@tonic-gate 	case S_IWOTH:
6137c478bd9Sstevel@tonic-gate 		str = gettext("world writable");
6147c478bd9Sstevel@tonic-gate 		break;
6157c478bd9Sstevel@tonic-gate 	case S_IXOTH:
6167c478bd9Sstevel@tonic-gate 		str = gettext("world executable");
6177c478bd9Sstevel@tonic-gate 		break;
6187c478bd9Sstevel@tonic-gate 	}
6197c478bd9Sstevel@tonic-gate 	if ((mode & bit) == (on ? 0 : bit)) {
6207c478bd9Sstevel@tonic-gate 		/*
6217c478bd9Sstevel@tonic-gate 		 * TRANSLATION_NOTE
6227c478bd9Sstevel@tonic-gate 		 * The first parameter below is a file name; the second
6237c478bd9Sstevel@tonic-gate 		 * is one of the "(owner|group|world) (read|writ|execut)able"
6247c478bd9Sstevel@tonic-gate 		 * strings from above.
6257c478bd9Sstevel@tonic-gate 		 */
6267c478bd9Sstevel@tonic-gate 		/*
6277c478bd9Sstevel@tonic-gate 		 * The code below could be simplified but not in a way
6287c478bd9Sstevel@tonic-gate 		 * that would easily translate to non-English locales.
6297c478bd9Sstevel@tonic-gate 		 */
6307c478bd9Sstevel@tonic-gate 		if (on) {
6317c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("%s must be %s.\n"),
6327c478bd9Sstevel@tonic-gate 			    file, str);
6337c478bd9Sstevel@tonic-gate 		} else {
6347c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("%s must not be %s.\n"),
6357c478bd9Sstevel@tonic-gate 			    file, str);
6367c478bd9Sstevel@tonic-gate 		}
6377c478bd9Sstevel@tonic-gate 		return (B_TRUE);
6387c478bd9Sstevel@tonic-gate 	}
6397c478bd9Sstevel@tonic-gate 	return (B_FALSE);
6407c478bd9Sstevel@tonic-gate }
6417c478bd9Sstevel@tonic-gate 
6427c478bd9Sstevel@tonic-gate /*
6437c478bd9Sstevel@tonic-gate  * We want to make sure that no zone has its zone path as a child node
6447c478bd9Sstevel@tonic-gate  * (in the directory sense) of any other.  We do that by comparing this
6457c478bd9Sstevel@tonic-gate  * zone's path to the path of all other (non-global) zones.  The comparison
6467c478bd9Sstevel@tonic-gate  * in each case is simple: add '/' to the end of the path, then do a
6477c478bd9Sstevel@tonic-gate  * strncmp() of the two paths, using the length of the shorter one.
6487c478bd9Sstevel@tonic-gate  */
6497c478bd9Sstevel@tonic-gate 
6507c478bd9Sstevel@tonic-gate static int
6517c478bd9Sstevel@tonic-gate crosscheck_zonepaths(char *path)
6527c478bd9Sstevel@tonic-gate {
6537c478bd9Sstevel@tonic-gate 	char rpath[MAXPATHLEN];		/* resolved path */
6547c478bd9Sstevel@tonic-gate 	char path_copy[MAXPATHLEN];	/* copy of original path */
6557c478bd9Sstevel@tonic-gate 	char rpath_copy[MAXPATHLEN];	/* copy of original rpath */
6567c478bd9Sstevel@tonic-gate 	struct zoneent *ze;
6577c478bd9Sstevel@tonic-gate 	int res, err;
6587c478bd9Sstevel@tonic-gate 	FILE *cookie;
6597c478bd9Sstevel@tonic-gate 
6607c478bd9Sstevel@tonic-gate 	cookie = setzoneent();
6617c478bd9Sstevel@tonic-gate 	while ((ze = getzoneent_private(cookie)) != NULL) {
6627c478bd9Sstevel@tonic-gate 		/* Skip zones which are not installed. */
6637c478bd9Sstevel@tonic-gate 		if (ze->zone_state < ZONE_STATE_INSTALLED) {
6647c478bd9Sstevel@tonic-gate 			free(ze);
6657c478bd9Sstevel@tonic-gate 			continue;
6667c478bd9Sstevel@tonic-gate 		}
6677c478bd9Sstevel@tonic-gate 		/* Skip the global zone and the current target zone. */
6687c478bd9Sstevel@tonic-gate 		if (strcmp(ze->zone_name, GLOBAL_ZONENAME) == 0 ||
6697c478bd9Sstevel@tonic-gate 		    strcmp(ze->zone_name, target_zone) == 0) {
6707c478bd9Sstevel@tonic-gate 			free(ze);
6717c478bd9Sstevel@tonic-gate 			continue;
6727c478bd9Sstevel@tonic-gate 		}
6737c478bd9Sstevel@tonic-gate 		if (strlen(ze->zone_path) == 0) {
6747c478bd9Sstevel@tonic-gate 			/* old index file without path, fall back */
6757c478bd9Sstevel@tonic-gate 			if ((err = zone_get_zonepath(ze->zone_name,
6767c478bd9Sstevel@tonic-gate 			    ze->zone_path, sizeof (ze->zone_path))) != Z_OK) {
6777c478bd9Sstevel@tonic-gate 				errno = err;
6787c478bd9Sstevel@tonic-gate 				zperror2(ze->zone_name,
6797c478bd9Sstevel@tonic-gate 				    gettext("could not get zone path"));
6807c478bd9Sstevel@tonic-gate 				free(ze);
6817c478bd9Sstevel@tonic-gate 				continue;
6827c478bd9Sstevel@tonic-gate 			}
6837c478bd9Sstevel@tonic-gate 		}
684108322fbScarlsonj 		(void) snprintf(path_copy, sizeof (path_copy), "%s%s",
685108322fbScarlsonj 		    zonecfg_get_root(), ze->zone_path);
686108322fbScarlsonj 		res = resolvepath(path_copy, rpath, sizeof (rpath));
6877c478bd9Sstevel@tonic-gate 		if (res == -1) {
6887c478bd9Sstevel@tonic-gate 			if (errno != ENOENT) {
689108322fbScarlsonj 				zperror(path_copy, B_FALSE);
6907c478bd9Sstevel@tonic-gate 				free(ze);
6917c478bd9Sstevel@tonic-gate 				return (Z_ERR);
6927c478bd9Sstevel@tonic-gate 			}
6937c478bd9Sstevel@tonic-gate 			(void) printf(gettext("WARNING: zone %s is installed, "
6947c478bd9Sstevel@tonic-gate 			    "but its %s %s does not exist.\n"), ze->zone_name,
695108322fbScarlsonj 			    "zonepath", path_copy);
6967c478bd9Sstevel@tonic-gate 			free(ze);
6977c478bd9Sstevel@tonic-gate 			continue;
6987c478bd9Sstevel@tonic-gate 		}
6997c478bd9Sstevel@tonic-gate 		rpath[res] = '\0';
7007c478bd9Sstevel@tonic-gate 		(void) snprintf(path_copy, sizeof (path_copy), "%s/", path);
7017c478bd9Sstevel@tonic-gate 		(void) snprintf(rpath_copy, sizeof (rpath_copy), "%s/", rpath);
7027c478bd9Sstevel@tonic-gate 		if (strncmp(path_copy, rpath_copy,
7037c478bd9Sstevel@tonic-gate 		    min(strlen(path_copy), strlen(rpath_copy))) == 0) {
704*5ee84fbdSgjelinek 			/*
705*5ee84fbdSgjelinek 			 * TRANSLATION_NOTE
706*5ee84fbdSgjelinek 			 * zonepath is a literal that should not be translated.
707*5ee84fbdSgjelinek 			 */
7087c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("%s zonepath (%s) and "
7097c478bd9Sstevel@tonic-gate 			    "%s zonepath (%s) overlap.\n"),
7107c478bd9Sstevel@tonic-gate 			    target_zone, path, ze->zone_name, rpath);
7117c478bd9Sstevel@tonic-gate 			free(ze);
7127c478bd9Sstevel@tonic-gate 			return (Z_ERR);
7137c478bd9Sstevel@tonic-gate 		}
7147c478bd9Sstevel@tonic-gate 		free(ze);
7157c478bd9Sstevel@tonic-gate 	}
7167c478bd9Sstevel@tonic-gate 	endzoneent(cookie);
7177c478bd9Sstevel@tonic-gate 	return (Z_OK);
7187c478bd9Sstevel@tonic-gate }
7197c478bd9Sstevel@tonic-gate 
7207c478bd9Sstevel@tonic-gate static int
7217c478bd9Sstevel@tonic-gate validate_zonepath(char *path, int cmd_num)
7227c478bd9Sstevel@tonic-gate {
7237c478bd9Sstevel@tonic-gate 	int res;			/* result of last library/system call */
7247c478bd9Sstevel@tonic-gate 	boolean_t err = B_FALSE;	/* have we run into an error? */
7257c478bd9Sstevel@tonic-gate 	struct stat stbuf;
7267c478bd9Sstevel@tonic-gate 	struct statvfs vfsbuf;
7277c478bd9Sstevel@tonic-gate 	char rpath[MAXPATHLEN];		/* resolved path */
7287c478bd9Sstevel@tonic-gate 	char ppath[MAXPATHLEN];		/* parent path */
7297c478bd9Sstevel@tonic-gate 	char rppath[MAXPATHLEN];	/* resolved parent path */
7307c478bd9Sstevel@tonic-gate 	char rootpath[MAXPATHLEN];	/* root path */
7317c478bd9Sstevel@tonic-gate 	zone_state_t state;
7327c478bd9Sstevel@tonic-gate 
7337c478bd9Sstevel@tonic-gate 	if (path[0] != '/') {
7347c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
7357c478bd9Sstevel@tonic-gate 		    gettext("%s is not an absolute path.\n"), path);
7367c478bd9Sstevel@tonic-gate 		return (Z_ERR);
7377c478bd9Sstevel@tonic-gate 	}
7387c478bd9Sstevel@tonic-gate 	if ((res = resolvepath(path, rpath, sizeof (rpath))) == -1) {
7397c478bd9Sstevel@tonic-gate 		if ((errno != ENOENT) ||
7407c478bd9Sstevel@tonic-gate 		    (cmd_num != CMD_VERIFY && cmd_num != CMD_INSTALL)) {
7417c478bd9Sstevel@tonic-gate 			zperror(path, B_FALSE);
7427c478bd9Sstevel@tonic-gate 			return (Z_ERR);
7437c478bd9Sstevel@tonic-gate 		}
7447c478bd9Sstevel@tonic-gate 		if (cmd_num == CMD_VERIFY) {
745*5ee84fbdSgjelinek 			/*
746*5ee84fbdSgjelinek 			 * TRANSLATION_NOTE
747*5ee84fbdSgjelinek 			 * zoneadm is a literal that should not be translated.
748*5ee84fbdSgjelinek 			 */
7497c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("WARNING: %s does not "
750*5ee84fbdSgjelinek 			    "exist, so it could not be verified.\nWhen "
751*5ee84fbdSgjelinek 			    "'zoneadm %s' is run, '%s' will try to create\n%s, "
752*5ee84fbdSgjelinek 			    "and '%s' will be tried again,\nbut the '%s' may "
753*5ee84fbdSgjelinek 			    "fail if:\nthe parent directory of %s is group- or "
754*5ee84fbdSgjelinek 			    "other-writable\nor\n%s overlaps with any other "
7557c478bd9Sstevel@tonic-gate 			    "installed zones.\n"), path,
7567c478bd9Sstevel@tonic-gate 			    cmd_to_str(CMD_INSTALL), cmd_to_str(CMD_INSTALL),
7577c478bd9Sstevel@tonic-gate 			    path, cmd_to_str(CMD_VERIFY),
7587c478bd9Sstevel@tonic-gate 			    cmd_to_str(CMD_VERIFY), path, path);
7597c478bd9Sstevel@tonic-gate 			return (Z_OK);
7607c478bd9Sstevel@tonic-gate 		}
7617c478bd9Sstevel@tonic-gate 		/*
7627c478bd9Sstevel@tonic-gate 		 * The zonepath is supposed to be mode 700 but its
7637c478bd9Sstevel@tonic-gate 		 * parent(s) 755.  So use 755 on the mkdirp() then
7647c478bd9Sstevel@tonic-gate 		 * chmod() the zonepath itself to 700.
7657c478bd9Sstevel@tonic-gate 		 */
7667c478bd9Sstevel@tonic-gate 		if (mkdirp(path, DEFAULT_DIR_MODE) < 0) {
7677c478bd9Sstevel@tonic-gate 			zperror(path, B_FALSE);
7687c478bd9Sstevel@tonic-gate 			return (Z_ERR);
7697c478bd9Sstevel@tonic-gate 		}
7707c478bd9Sstevel@tonic-gate 		/*
7717c478bd9Sstevel@tonic-gate 		 * If the chmod() fails, report the error, but might
7727c478bd9Sstevel@tonic-gate 		 * as well continue the verify procedure.
7737c478bd9Sstevel@tonic-gate 		 */
7747c478bd9Sstevel@tonic-gate 		if (chmod(path, S_IRWXU) != 0)
7757c478bd9Sstevel@tonic-gate 			zperror(path, B_FALSE);
7767c478bd9Sstevel@tonic-gate 		/*
7777c478bd9Sstevel@tonic-gate 		 * Since the mkdir() succeeded, we should not have to
7787c478bd9Sstevel@tonic-gate 		 * worry about a subsequent ENOENT, thus this should
7797c478bd9Sstevel@tonic-gate 		 * only recurse once.
7807c478bd9Sstevel@tonic-gate 		 */
7817c478bd9Sstevel@tonic-gate 		return (validate_zonepath(path, CMD_INSTALL));
7827c478bd9Sstevel@tonic-gate 	}
7837c478bd9Sstevel@tonic-gate 	rpath[res] = '\0';
7847c478bd9Sstevel@tonic-gate 	if (strcmp(path, rpath) != 0) {
7857c478bd9Sstevel@tonic-gate 		errno = Z_RESOLVED_PATH;
7867c478bd9Sstevel@tonic-gate 		zperror(path, B_TRUE);
7877c478bd9Sstevel@tonic-gate 		return (Z_ERR);
7887c478bd9Sstevel@tonic-gate 	}
7897c478bd9Sstevel@tonic-gate 	if ((res = stat(rpath, &stbuf)) != 0) {
7907c478bd9Sstevel@tonic-gate 		zperror(rpath, B_FALSE);
7917c478bd9Sstevel@tonic-gate 		return (Z_ERR);
7927c478bd9Sstevel@tonic-gate 	}
7937c478bd9Sstevel@tonic-gate 	if (!S_ISDIR(stbuf.st_mode)) {
7947c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("%s is not a directory.\n"),
7957c478bd9Sstevel@tonic-gate 		    rpath);
7967c478bd9Sstevel@tonic-gate 		return (Z_ERR);
7977c478bd9Sstevel@tonic-gate 	}
7987c478bd9Sstevel@tonic-gate 	if ((strcmp(stbuf.st_fstype, MNTTYPE_TMPFS) == 0) ||
7997c478bd9Sstevel@tonic-gate 	    (strcmp(stbuf.st_fstype, MNTTYPE_XMEMFS) == 0)) {
8007c478bd9Sstevel@tonic-gate 		(void) printf(gettext("WARNING: %s is on a temporary "
8017c478bd9Sstevel@tonic-gate 		    "file-system.\n"), rpath);
8027c478bd9Sstevel@tonic-gate 	}
8037c478bd9Sstevel@tonic-gate 	if (crosscheck_zonepaths(rpath) != Z_OK)
8047c478bd9Sstevel@tonic-gate 		return (Z_ERR);
8057c478bd9Sstevel@tonic-gate 	/*
8067c478bd9Sstevel@tonic-gate 	 * Try to collect and report as many minor errors as possible
8077c478bd9Sstevel@tonic-gate 	 * before returning, so the user can learn everything that needs
8087c478bd9Sstevel@tonic-gate 	 * to be fixed up front.
8097c478bd9Sstevel@tonic-gate 	 */
8107c478bd9Sstevel@tonic-gate 	if (stbuf.st_uid != 0) {
8117c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("%s is not owned by root.\n"),
8127c478bd9Sstevel@tonic-gate 		    rpath);
8137c478bd9Sstevel@tonic-gate 		err = B_TRUE;
8147c478bd9Sstevel@tonic-gate 	}
8157c478bd9Sstevel@tonic-gate 	err |= bad_mode_bit(stbuf.st_mode, S_IRUSR, B_TRUE, rpath);
8167c478bd9Sstevel@tonic-gate 	err |= bad_mode_bit(stbuf.st_mode, S_IWUSR, B_TRUE, rpath);
8177c478bd9Sstevel@tonic-gate 	err |= bad_mode_bit(stbuf.st_mode, S_IXUSR, B_TRUE, rpath);
8187c478bd9Sstevel@tonic-gate 	err |= bad_mode_bit(stbuf.st_mode, S_IRGRP, B_FALSE, rpath);
8197c478bd9Sstevel@tonic-gate 	err |= bad_mode_bit(stbuf.st_mode, S_IWGRP, B_FALSE, rpath);
8207c478bd9Sstevel@tonic-gate 	err |= bad_mode_bit(stbuf.st_mode, S_IXGRP, B_FALSE, rpath);
8217c478bd9Sstevel@tonic-gate 	err |= bad_mode_bit(stbuf.st_mode, S_IROTH, B_FALSE, rpath);
8227c478bd9Sstevel@tonic-gate 	err |= bad_mode_bit(stbuf.st_mode, S_IWOTH, B_FALSE, rpath);
8237c478bd9Sstevel@tonic-gate 	err |= bad_mode_bit(stbuf.st_mode, S_IXOTH, B_FALSE, rpath);
8247c478bd9Sstevel@tonic-gate 
8257c478bd9Sstevel@tonic-gate 	(void) snprintf(ppath, sizeof (ppath), "%s/..", path);
8267c478bd9Sstevel@tonic-gate 	if ((res = resolvepath(ppath, rppath, sizeof (rppath))) == -1) {
8277c478bd9Sstevel@tonic-gate 		zperror(ppath, B_FALSE);
8287c478bd9Sstevel@tonic-gate 		return (Z_ERR);
8297c478bd9Sstevel@tonic-gate 	}
8307c478bd9Sstevel@tonic-gate 	rppath[res] = '\0';
8317c478bd9Sstevel@tonic-gate 	if ((res = stat(rppath, &stbuf)) != 0) {
8327c478bd9Sstevel@tonic-gate 		zperror(rppath, B_FALSE);
8337c478bd9Sstevel@tonic-gate 		return (Z_ERR);
8347c478bd9Sstevel@tonic-gate 	}
8357c478bd9Sstevel@tonic-gate 	/* theoretically impossible */
8367c478bd9Sstevel@tonic-gate 	if (!S_ISDIR(stbuf.st_mode)) {
8377c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("%s is not a directory.\n"),
8387c478bd9Sstevel@tonic-gate 		    rppath);
8397c478bd9Sstevel@tonic-gate 		return (Z_ERR);
8407c478bd9Sstevel@tonic-gate 	}
8417c478bd9Sstevel@tonic-gate 	if (stbuf.st_uid != 0) {
8427c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("%s is not owned by root.\n"),
8437c478bd9Sstevel@tonic-gate 		    rppath);
8447c478bd9Sstevel@tonic-gate 		err = B_TRUE;
8457c478bd9Sstevel@tonic-gate 	}
8467c478bd9Sstevel@tonic-gate 	err |= bad_mode_bit(stbuf.st_mode, S_IRUSR, B_TRUE, rppath);
8477c478bd9Sstevel@tonic-gate 	err |= bad_mode_bit(stbuf.st_mode, S_IWUSR, B_TRUE, rppath);
8487c478bd9Sstevel@tonic-gate 	err |= bad_mode_bit(stbuf.st_mode, S_IXUSR, B_TRUE, rppath);
8497c478bd9Sstevel@tonic-gate 	err |= bad_mode_bit(stbuf.st_mode, S_IWGRP, B_FALSE, rppath);
8507c478bd9Sstevel@tonic-gate 	err |= bad_mode_bit(stbuf.st_mode, S_IWOTH, B_FALSE, rppath);
8517c478bd9Sstevel@tonic-gate 	if (strcmp(rpath, rppath) == 0) {
8527c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("%s is its own parent.\n"),
8537c478bd9Sstevel@tonic-gate 		    rppath);
8547c478bd9Sstevel@tonic-gate 		err = B_TRUE;
8557c478bd9Sstevel@tonic-gate 	}
8567c478bd9Sstevel@tonic-gate 
8577c478bd9Sstevel@tonic-gate 	if (statvfs(rpath, &vfsbuf) != 0) {
8587c478bd9Sstevel@tonic-gate 		zperror(rpath, B_FALSE);
8597c478bd9Sstevel@tonic-gate 		return (Z_ERR);
8607c478bd9Sstevel@tonic-gate 	}
861b5abaf04Sgjelinek 	if (strcmp(vfsbuf.f_basetype, MNTTYPE_NFS) == 0) {
862*5ee84fbdSgjelinek 		/*
863*5ee84fbdSgjelinek 		 * TRANSLATION_NOTE
864*5ee84fbdSgjelinek 		 * Zonepath and NFS are literals that should not be translated.
865*5ee84fbdSgjelinek 		 */
866*5ee84fbdSgjelinek 		(void) fprintf(stderr, gettext("Zonepath %s is on an NFS "
867*5ee84fbdSgjelinek 		    "mounted file-system.\n"
868*5ee84fbdSgjelinek 		    "\tA local file-system must be used.\n"), rpath);
869b5abaf04Sgjelinek 		return (Z_ERR);
870b5abaf04Sgjelinek 	}
871b5abaf04Sgjelinek 	if (vfsbuf.f_flag & ST_NOSUID) {
872*5ee84fbdSgjelinek 		/*
873*5ee84fbdSgjelinek 		 * TRANSLATION_NOTE
874*5ee84fbdSgjelinek 		 * Zonepath and nosuid are literals that should not be
875*5ee84fbdSgjelinek 		 * translated.
876*5ee84fbdSgjelinek 		 */
877b5abaf04Sgjelinek 		(void) fprintf(stderr, gettext("Zonepath %s is on a nosuid "
878*5ee84fbdSgjelinek 		    "file-system.\n"), rpath);
8797c478bd9Sstevel@tonic-gate 		return (Z_ERR);
8807c478bd9Sstevel@tonic-gate 	}
8817c478bd9Sstevel@tonic-gate 
8827c478bd9Sstevel@tonic-gate 	if ((res = zone_get_state(target_zone, &state)) != Z_OK) {
8837c478bd9Sstevel@tonic-gate 		errno = res;
8847c478bd9Sstevel@tonic-gate 		zperror2(target_zone, gettext("could not get state"));
8857c478bd9Sstevel@tonic-gate 		return (Z_ERR);
8867c478bd9Sstevel@tonic-gate 	}
8877c478bd9Sstevel@tonic-gate 	/*
8887c478bd9Sstevel@tonic-gate 	 * The existence of the root path is only bad in the configured state,
8897c478bd9Sstevel@tonic-gate 	 * as it is *supposed* to be there at the installed and later states.
8907c478bd9Sstevel@tonic-gate 	 * State/command mismatches are caught earlier in verify_details().
8917c478bd9Sstevel@tonic-gate 	 */
8927c478bd9Sstevel@tonic-gate 	if (state == ZONE_STATE_CONFIGURED) {
8937c478bd9Sstevel@tonic-gate 		if (snprintf(rootpath, sizeof (rootpath), "%s/root", rpath) >=
8947c478bd9Sstevel@tonic-gate 		    sizeof (rootpath)) {
895*5ee84fbdSgjelinek 			/*
896*5ee84fbdSgjelinek 			 * TRANSLATION_NOTE
897*5ee84fbdSgjelinek 			 * Zonepath is a literal that should not be translated.
898*5ee84fbdSgjelinek 			 */
8997c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
9007c478bd9Sstevel@tonic-gate 			    gettext("Zonepath %s is too long.\n"), rpath);
9017c478bd9Sstevel@tonic-gate 			return (Z_ERR);
9027c478bd9Sstevel@tonic-gate 		}
9037c478bd9Sstevel@tonic-gate 		if ((res = stat(rootpath, &stbuf)) == 0) {
9047c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("Rootpath %s exists; "
9057c478bd9Sstevel@tonic-gate 			    "remove or move aside prior to %s.\n"), rootpath,
9067c478bd9Sstevel@tonic-gate 			    cmd_to_str(CMD_INSTALL));
9077c478bd9Sstevel@tonic-gate 			return (Z_ERR);
9087c478bd9Sstevel@tonic-gate 		}
9097c478bd9Sstevel@tonic-gate 	}
9107c478bd9Sstevel@tonic-gate 
9117c478bd9Sstevel@tonic-gate 	return (err ? Z_ERR : Z_OK);
9127c478bd9Sstevel@tonic-gate }
9137c478bd9Sstevel@tonic-gate 
9147c478bd9Sstevel@tonic-gate static void
9157c478bd9Sstevel@tonic-gate release_lock_file(int lockfd)
9167c478bd9Sstevel@tonic-gate {
9177c478bd9Sstevel@tonic-gate 	(void) close(lockfd);
9187c478bd9Sstevel@tonic-gate }
9197c478bd9Sstevel@tonic-gate 
9207c478bd9Sstevel@tonic-gate static int
9217c478bd9Sstevel@tonic-gate grab_lock_file(const char *zone_name, int *lockfd)
9227c478bd9Sstevel@tonic-gate {
9237c478bd9Sstevel@tonic-gate 	char pathbuf[PATH_MAX];
9247c478bd9Sstevel@tonic-gate 	struct flock flock;
9257c478bd9Sstevel@tonic-gate 
926108322fbScarlsonj 	if (snprintf(pathbuf, sizeof (pathbuf), "%s%s", zonecfg_get_root(),
927108322fbScarlsonj 	    ZONES_TMPDIR) >= sizeof (pathbuf)) {
928108322fbScarlsonj 		zerror(gettext("alternate root path is too long"));
929108322fbScarlsonj 		return (Z_ERR);
930108322fbScarlsonj 	}
931108322fbScarlsonj 	if (mkdir(pathbuf, S_IRWXU) < 0 && errno != EEXIST) {
932108322fbScarlsonj 		zerror(gettext("could not mkdir %s: %s"), pathbuf,
9337c478bd9Sstevel@tonic-gate 		    strerror(errno));
9347c478bd9Sstevel@tonic-gate 		return (Z_ERR);
9357c478bd9Sstevel@tonic-gate 	}
936108322fbScarlsonj 	(void) chmod(pathbuf, S_IRWXU);
9377c478bd9Sstevel@tonic-gate 
9387c478bd9Sstevel@tonic-gate 	/*
9397c478bd9Sstevel@tonic-gate 	 * One of these lock files is created for each zone (when needed).
9407c478bd9Sstevel@tonic-gate 	 * The lock files are not cleaned up (except on system reboot),
9417c478bd9Sstevel@tonic-gate 	 * but since there is only one per zone, there is no resource
9427c478bd9Sstevel@tonic-gate 	 * starvation issue.
9437c478bd9Sstevel@tonic-gate 	 */
944108322fbScarlsonj 	if (snprintf(pathbuf, sizeof (pathbuf), "%s%s/%s.zoneadm.lock",
945108322fbScarlsonj 	    zonecfg_get_root(), ZONES_TMPDIR, zone_name) >= sizeof (pathbuf)) {
946108322fbScarlsonj 		zerror(gettext("alternate root path is too long"));
947108322fbScarlsonj 		return (Z_ERR);
948108322fbScarlsonj 	}
9497c478bd9Sstevel@tonic-gate 	if ((*lockfd = open(pathbuf, O_RDWR|O_CREAT, S_IRUSR|S_IWUSR)) < 0) {
9507c478bd9Sstevel@tonic-gate 		zerror(gettext("could not open %s: %s"), pathbuf,
9517c478bd9Sstevel@tonic-gate 		    strerror(errno));
9527c478bd9Sstevel@tonic-gate 		return (Z_ERR);
9537c478bd9Sstevel@tonic-gate 	}
9547c478bd9Sstevel@tonic-gate 	/*
9557c478bd9Sstevel@tonic-gate 	 * Lock the file to synchronize with other zoneadmds
9567c478bd9Sstevel@tonic-gate 	 */
9577c478bd9Sstevel@tonic-gate 	flock.l_type = F_WRLCK;
9587c478bd9Sstevel@tonic-gate 	flock.l_whence = SEEK_SET;
9597c478bd9Sstevel@tonic-gate 	flock.l_start = (off_t)0;
9607c478bd9Sstevel@tonic-gate 	flock.l_len = (off_t)0;
9617c478bd9Sstevel@tonic-gate 	if (fcntl(*lockfd, F_SETLKW, &flock) < 0) {
9627c478bd9Sstevel@tonic-gate 		zerror(gettext("unable to lock %s: %s"), pathbuf,
9637c478bd9Sstevel@tonic-gate 		    strerror(errno));
9647c478bd9Sstevel@tonic-gate 		release_lock_file(*lockfd);
9657c478bd9Sstevel@tonic-gate 		return (Z_ERR);
9667c478bd9Sstevel@tonic-gate 	}
9677c478bd9Sstevel@tonic-gate 	return (Z_OK);
9687c478bd9Sstevel@tonic-gate }
9697c478bd9Sstevel@tonic-gate 
970108322fbScarlsonj static boolean_t
9717c478bd9Sstevel@tonic-gate get_doorname(const char *zone_name, char *buffer)
9727c478bd9Sstevel@tonic-gate {
973108322fbScarlsonj 	return (snprintf(buffer, PATH_MAX, "%s" ZONE_DOOR_PATH,
974108322fbScarlsonj 	    zonecfg_get_root(), zone_name) < PATH_MAX);
9757c478bd9Sstevel@tonic-gate }
9767c478bd9Sstevel@tonic-gate 
9777c478bd9Sstevel@tonic-gate /*
9787c478bd9Sstevel@tonic-gate  * system daemons are not audited.  For the global zone, this occurs
9797c478bd9Sstevel@tonic-gate  * "naturally" since init is started with the default audit
9807c478bd9Sstevel@tonic-gate  * characteristics.  Since zoneadmd is a system daemon and it starts
9817c478bd9Sstevel@tonic-gate  * init for a zone, it is necessary to clear out the audit
9827c478bd9Sstevel@tonic-gate  * characteristics inherited from whomever started zoneadmd.  This is
9837c478bd9Sstevel@tonic-gate  * indicated by the audit id, which is set from the ruid parameter of
9847c478bd9Sstevel@tonic-gate  * adt_set_user(), below.
9857c478bd9Sstevel@tonic-gate  */
9867c478bd9Sstevel@tonic-gate 
9877c478bd9Sstevel@tonic-gate static void
9887c478bd9Sstevel@tonic-gate prepare_audit_context()
9897c478bd9Sstevel@tonic-gate {
9907c478bd9Sstevel@tonic-gate 	adt_session_data_t	*ah;
9917c478bd9Sstevel@tonic-gate 	char			*failure = gettext("audit failure: %s");
9927c478bd9Sstevel@tonic-gate 
9937c478bd9Sstevel@tonic-gate 	if (adt_start_session(&ah, NULL, 0)) {
9947c478bd9Sstevel@tonic-gate 		zerror(failure, strerror(errno));
9957c478bd9Sstevel@tonic-gate 		return;
9967c478bd9Sstevel@tonic-gate 	}
9977c478bd9Sstevel@tonic-gate 	if (adt_set_user(ah, ADT_NO_AUDIT, ADT_NO_AUDIT,
9987c478bd9Sstevel@tonic-gate 	    ADT_NO_AUDIT, ADT_NO_AUDIT, NULL, ADT_NEW)) {
9997c478bd9Sstevel@tonic-gate 		zerror(failure, strerror(errno));
10007c478bd9Sstevel@tonic-gate 		(void) adt_end_session(ah);
10017c478bd9Sstevel@tonic-gate 		return;
10027c478bd9Sstevel@tonic-gate 	}
10037c478bd9Sstevel@tonic-gate 	if (adt_set_proc(ah))
10047c478bd9Sstevel@tonic-gate 		zerror(failure, strerror(errno));
10057c478bd9Sstevel@tonic-gate 
10067c478bd9Sstevel@tonic-gate 	(void) adt_end_session(ah);
10077c478bd9Sstevel@tonic-gate }
10087c478bd9Sstevel@tonic-gate 
10097c478bd9Sstevel@tonic-gate static int
10107c478bd9Sstevel@tonic-gate start_zoneadmd(const char *zone_name)
10117c478bd9Sstevel@tonic-gate {
10127c478bd9Sstevel@tonic-gate 	char doorpath[PATH_MAX];
10137c478bd9Sstevel@tonic-gate 	pid_t child_pid;
10147c478bd9Sstevel@tonic-gate 	int error = Z_ERR;
10157c478bd9Sstevel@tonic-gate 	int doorfd, lockfd;
10167c478bd9Sstevel@tonic-gate 	struct door_info info;
10177c478bd9Sstevel@tonic-gate 
1018108322fbScarlsonj 	if (!get_doorname(zone_name, doorpath))
1019108322fbScarlsonj 		return (Z_ERR);
10207c478bd9Sstevel@tonic-gate 
10217c478bd9Sstevel@tonic-gate 	if (grab_lock_file(zone_name, &lockfd) != Z_OK)
10227c478bd9Sstevel@tonic-gate 		return (Z_ERR);
10237c478bd9Sstevel@tonic-gate 
10247c478bd9Sstevel@tonic-gate 	/*
10257c478bd9Sstevel@tonic-gate 	 * Now that we have the lock, re-confirm that the daemon is
10267c478bd9Sstevel@tonic-gate 	 * *not* up and working fine.  If it is still down, we have a green
10277c478bd9Sstevel@tonic-gate 	 * light to start it.
10287c478bd9Sstevel@tonic-gate 	 */
10297c478bd9Sstevel@tonic-gate 	if ((doorfd = open(doorpath, O_RDONLY)) < 0) {
10307c478bd9Sstevel@tonic-gate 		if (errno != ENOENT) {
10317c478bd9Sstevel@tonic-gate 			zperror(doorpath, B_FALSE);
10327c478bd9Sstevel@tonic-gate 			goto out;
10337c478bd9Sstevel@tonic-gate 		}
10347c478bd9Sstevel@tonic-gate 	} else {
10357c478bd9Sstevel@tonic-gate 		if (door_info(doorfd, &info) == 0 &&
10367c478bd9Sstevel@tonic-gate 		    ((info.di_attributes & DOOR_REVOKED) == 0)) {
10377c478bd9Sstevel@tonic-gate 			error = Z_OK;
10387c478bd9Sstevel@tonic-gate 			(void) close(doorfd);
10397c478bd9Sstevel@tonic-gate 			goto out;
10407c478bd9Sstevel@tonic-gate 		}
10417c478bd9Sstevel@tonic-gate 		(void) close(doorfd);
10427c478bd9Sstevel@tonic-gate 	}
10437c478bd9Sstevel@tonic-gate 
10447c478bd9Sstevel@tonic-gate 	if ((child_pid = fork()) == -1) {
10457c478bd9Sstevel@tonic-gate 		zperror(gettext("could not fork"), B_FALSE);
10467c478bd9Sstevel@tonic-gate 		goto out;
10477c478bd9Sstevel@tonic-gate 	} else if (child_pid == 0) {
1048108322fbScarlsonj 		const char *argv[6], **ap;
10497c478bd9Sstevel@tonic-gate 
1050108322fbScarlsonj 		/* child process */
10517c478bd9Sstevel@tonic-gate 		prepare_audit_context();
10527c478bd9Sstevel@tonic-gate 
1053108322fbScarlsonj 		ap = argv;
1054108322fbScarlsonj 		*ap++ = "zoneadmd";
1055108322fbScarlsonj 		*ap++ = "-z";
1056108322fbScarlsonj 		*ap++ = zone_name;
1057108322fbScarlsonj 		if (zonecfg_in_alt_root()) {
1058108322fbScarlsonj 			*ap++ = "-R";
1059108322fbScarlsonj 			*ap++ = zonecfg_get_root();
1060108322fbScarlsonj 		}
1061108322fbScarlsonj 		*ap = NULL;
1062108322fbScarlsonj 
1063108322fbScarlsonj 		(void) execv("/usr/lib/zones/zoneadmd", (char * const *)argv);
1064*5ee84fbdSgjelinek 		/*
1065*5ee84fbdSgjelinek 		 * TRANSLATION_NOTE
1066*5ee84fbdSgjelinek 		 * zoneadmd is a literal that should not be translated.
1067*5ee84fbdSgjelinek 		 */
10687c478bd9Sstevel@tonic-gate 		zperror(gettext("could not exec zoneadmd"), B_FALSE);
10697c478bd9Sstevel@tonic-gate 		_exit(Z_ERR);
10707c478bd9Sstevel@tonic-gate 	} else {
10717c478bd9Sstevel@tonic-gate 		/* parent process */
10727c478bd9Sstevel@tonic-gate 		pid_t retval;
10737c478bd9Sstevel@tonic-gate 		int pstatus = 0;
10747c478bd9Sstevel@tonic-gate 
10757c478bd9Sstevel@tonic-gate 		do {
10767c478bd9Sstevel@tonic-gate 			retval = waitpid(child_pid, &pstatus, 0);
10777c478bd9Sstevel@tonic-gate 		} while (retval != child_pid);
10787c478bd9Sstevel@tonic-gate 		if (WIFSIGNALED(pstatus) || (WIFEXITED(pstatus) &&
10797c478bd9Sstevel@tonic-gate 		    WEXITSTATUS(pstatus) != 0)) {
10807c478bd9Sstevel@tonic-gate 			zerror(gettext("could not start %s"), "zoneadmd");
10817c478bd9Sstevel@tonic-gate 			goto out;
10827c478bd9Sstevel@tonic-gate 		}
10837c478bd9Sstevel@tonic-gate 	}
10847c478bd9Sstevel@tonic-gate 	error = Z_OK;
10857c478bd9Sstevel@tonic-gate out:
10867c478bd9Sstevel@tonic-gate 	release_lock_file(lockfd);
10877c478bd9Sstevel@tonic-gate 	return (error);
10887c478bd9Sstevel@tonic-gate }
10897c478bd9Sstevel@tonic-gate 
10907c478bd9Sstevel@tonic-gate static int
10917c478bd9Sstevel@tonic-gate ping_zoneadmd(const char *zone_name)
10927c478bd9Sstevel@tonic-gate {
10937c478bd9Sstevel@tonic-gate 	char doorpath[PATH_MAX];
10947c478bd9Sstevel@tonic-gate 	int doorfd;
10957c478bd9Sstevel@tonic-gate 	struct door_info info;
10967c478bd9Sstevel@tonic-gate 
1097108322fbScarlsonj 	if (!get_doorname(zone_name, doorpath))
1098108322fbScarlsonj 		return (Z_ERR);
10997c478bd9Sstevel@tonic-gate 
11007c478bd9Sstevel@tonic-gate 	if ((doorfd = open(doorpath, O_RDONLY)) < 0) {
11017c478bd9Sstevel@tonic-gate 		return (Z_ERR);
11027c478bd9Sstevel@tonic-gate 	}
11037c478bd9Sstevel@tonic-gate 	if (door_info(doorfd, &info) == 0 &&
11047c478bd9Sstevel@tonic-gate 	    ((info.di_attributes & DOOR_REVOKED) == 0)) {
11057c478bd9Sstevel@tonic-gate 		(void) close(doorfd);
11067c478bd9Sstevel@tonic-gate 		return (Z_OK);
11077c478bd9Sstevel@tonic-gate 	}
11087c478bd9Sstevel@tonic-gate 	(void) close(doorfd);
11097c478bd9Sstevel@tonic-gate 	return (Z_ERR);
11107c478bd9Sstevel@tonic-gate }
11117c478bd9Sstevel@tonic-gate 
11127c478bd9Sstevel@tonic-gate static int
11137c478bd9Sstevel@tonic-gate call_zoneadmd(const char *zone_name, zone_cmd_arg_t *arg)
11147c478bd9Sstevel@tonic-gate {
11157c478bd9Sstevel@tonic-gate 	char doorpath[PATH_MAX];
11167c478bd9Sstevel@tonic-gate 	int doorfd, result;
11177c478bd9Sstevel@tonic-gate 	door_arg_t darg;
11187c478bd9Sstevel@tonic-gate 
11197c478bd9Sstevel@tonic-gate 	zoneid_t zoneid;
11207c478bd9Sstevel@tonic-gate 	uint64_t uniqid = 0;
11217c478bd9Sstevel@tonic-gate 
11227c478bd9Sstevel@tonic-gate 	zone_cmd_rval_t *rvalp;
11237c478bd9Sstevel@tonic-gate 	size_t rlen;
11247c478bd9Sstevel@tonic-gate 	char *cp, *errbuf;
11257c478bd9Sstevel@tonic-gate 
11267c478bd9Sstevel@tonic-gate 	rlen = getpagesize();
11277c478bd9Sstevel@tonic-gate 	if ((rvalp = malloc(rlen)) == NULL) {
11287c478bd9Sstevel@tonic-gate 		zerror(gettext("failed to allocate %lu bytes: %s"), rlen,
11297c478bd9Sstevel@tonic-gate 		    strerror(errno));
11307c478bd9Sstevel@tonic-gate 		return (-1);
11317c478bd9Sstevel@tonic-gate 	}
11327c478bd9Sstevel@tonic-gate 
11337c478bd9Sstevel@tonic-gate 	if ((zoneid = getzoneidbyname(zone_name)) != ZONE_ID_UNDEFINED) {
11347c478bd9Sstevel@tonic-gate 		(void) zone_getattr(zoneid, ZONE_ATTR_UNIQID, &uniqid,
11357c478bd9Sstevel@tonic-gate 		    sizeof (uniqid));
11367c478bd9Sstevel@tonic-gate 	}
11377c478bd9Sstevel@tonic-gate 	arg->uniqid = uniqid;
11387c478bd9Sstevel@tonic-gate 	(void) strlcpy(arg->locale, locale, sizeof (arg->locale));
1139108322fbScarlsonj 	if (!get_doorname(zone_name, doorpath)) {
1140108322fbScarlsonj 		zerror(gettext("alternate root path is too long"));
1141108322fbScarlsonj 		free(rvalp);
1142108322fbScarlsonj 		return (-1);
1143108322fbScarlsonj 	}
11447c478bd9Sstevel@tonic-gate 
11457c478bd9Sstevel@tonic-gate 	/*
11467c478bd9Sstevel@tonic-gate 	 * Loop trying to start zoneadmd; if something goes seriously
11477c478bd9Sstevel@tonic-gate 	 * wrong we break out and fail.
11487c478bd9Sstevel@tonic-gate 	 */
11497c478bd9Sstevel@tonic-gate 	for (;;) {
11507c478bd9Sstevel@tonic-gate 		if (start_zoneadmd(zone_name) != Z_OK)
11517c478bd9Sstevel@tonic-gate 			break;
11527c478bd9Sstevel@tonic-gate 
11537c478bd9Sstevel@tonic-gate 		if ((doorfd = open(doorpath, O_RDONLY)) < 0) {
11547c478bd9Sstevel@tonic-gate 			zperror(gettext("failed to open zone door"), B_FALSE);
11557c478bd9Sstevel@tonic-gate 			break;
11567c478bd9Sstevel@tonic-gate 		}
11577c478bd9Sstevel@tonic-gate 
11587c478bd9Sstevel@tonic-gate 		darg.data_ptr = (char *)arg;
11597c478bd9Sstevel@tonic-gate 		darg.data_size = sizeof (*arg);
11607c478bd9Sstevel@tonic-gate 		darg.desc_ptr = NULL;
11617c478bd9Sstevel@tonic-gate 		darg.desc_num = 0;
11627c478bd9Sstevel@tonic-gate 		darg.rbuf = (char *)rvalp;
11637c478bd9Sstevel@tonic-gate 		darg.rsize = rlen;
11647c478bd9Sstevel@tonic-gate 		if (door_call(doorfd, &darg) != 0) {
11657c478bd9Sstevel@tonic-gate 			(void) close(doorfd);
11667c478bd9Sstevel@tonic-gate 			/*
11677c478bd9Sstevel@tonic-gate 			 * We'll get EBADF if the door has been revoked.
11687c478bd9Sstevel@tonic-gate 			 */
11697c478bd9Sstevel@tonic-gate 			if (errno != EBADF) {
11707c478bd9Sstevel@tonic-gate 				zperror(gettext("door_call failed"), B_FALSE);
11717c478bd9Sstevel@tonic-gate 				break;
11727c478bd9Sstevel@tonic-gate 			}
11737c478bd9Sstevel@tonic-gate 			continue;	/* take another lap */
11747c478bd9Sstevel@tonic-gate 		}
11757c478bd9Sstevel@tonic-gate 		(void) close(doorfd);
11767c478bd9Sstevel@tonic-gate 
11777c478bd9Sstevel@tonic-gate 		if (darg.data_size == 0) {
11787c478bd9Sstevel@tonic-gate 			/* Door server is going away; kick it again. */
11797c478bd9Sstevel@tonic-gate 			continue;
11807c478bd9Sstevel@tonic-gate 		}
11817c478bd9Sstevel@tonic-gate 
11827c478bd9Sstevel@tonic-gate 		errbuf = rvalp->errbuf;
11837c478bd9Sstevel@tonic-gate 		while (*errbuf != '\0') {
11847c478bd9Sstevel@tonic-gate 			/*
11857c478bd9Sstevel@tonic-gate 			 * Remove any newlines since zerror()
11867c478bd9Sstevel@tonic-gate 			 * will append one automatically.
11877c478bd9Sstevel@tonic-gate 			 */
11887c478bd9Sstevel@tonic-gate 			cp = strchr(errbuf, '\n');
11897c478bd9Sstevel@tonic-gate 			if (cp != NULL)
11907c478bd9Sstevel@tonic-gate 				*cp = '\0';
11917c478bd9Sstevel@tonic-gate 			zerror("%s", errbuf);
11927c478bd9Sstevel@tonic-gate 			if (cp == NULL)
11937c478bd9Sstevel@tonic-gate 				break;
11947c478bd9Sstevel@tonic-gate 			errbuf = cp + 1;
11957c478bd9Sstevel@tonic-gate 		}
11967c478bd9Sstevel@tonic-gate 		result = rvalp->rval == 0 ? 0 : -1;
11977c478bd9Sstevel@tonic-gate 		free(rvalp);
11987c478bd9Sstevel@tonic-gate 		return (result);
11997c478bd9Sstevel@tonic-gate 	}
12007c478bd9Sstevel@tonic-gate 
12017c478bd9Sstevel@tonic-gate 	free(rvalp);
12027c478bd9Sstevel@tonic-gate 	return (-1);
12037c478bd9Sstevel@tonic-gate }
12047c478bd9Sstevel@tonic-gate 
12057c478bd9Sstevel@tonic-gate static int
12067c478bd9Sstevel@tonic-gate ready_func(int argc, char *argv[])
12077c478bd9Sstevel@tonic-gate {
12087c478bd9Sstevel@tonic-gate 	zone_cmd_arg_t zarg;
12097c478bd9Sstevel@tonic-gate 	int arg;
12107c478bd9Sstevel@tonic-gate 
1211108322fbScarlsonj 	if (zonecfg_in_alt_root()) {
1212108322fbScarlsonj 		zerror(gettext("cannot ready zone in alternate root"));
1213108322fbScarlsonj 		return (Z_ERR);
1214108322fbScarlsonj 	}
1215108322fbScarlsonj 
12167c478bd9Sstevel@tonic-gate 	optind = 0;
12177c478bd9Sstevel@tonic-gate 	if ((arg = getopt(argc, argv, "?")) != EOF) {
12187c478bd9Sstevel@tonic-gate 		switch (arg) {
12197c478bd9Sstevel@tonic-gate 		case '?':
12207c478bd9Sstevel@tonic-gate 			sub_usage(SHELP_READY, CMD_READY);
12217c478bd9Sstevel@tonic-gate 			return (optopt == '?' ? Z_OK : Z_USAGE);
12227c478bd9Sstevel@tonic-gate 		default:
12237c478bd9Sstevel@tonic-gate 			sub_usage(SHELP_READY, CMD_READY);
12247c478bd9Sstevel@tonic-gate 			return (Z_USAGE);
12257c478bd9Sstevel@tonic-gate 		}
12267c478bd9Sstevel@tonic-gate 	}
12277c478bd9Sstevel@tonic-gate 	if (argc > optind) {
12287c478bd9Sstevel@tonic-gate 		sub_usage(SHELP_READY, CMD_READY);
12297c478bd9Sstevel@tonic-gate 		return (Z_USAGE);
12307c478bd9Sstevel@tonic-gate 	}
12317c478bd9Sstevel@tonic-gate 	if (sanity_check(target_zone, CMD_READY, B_FALSE, B_FALSE) != Z_OK)
12327c478bd9Sstevel@tonic-gate 		return (Z_ERR);
12337c478bd9Sstevel@tonic-gate 	if (verify_details(CMD_READY) != Z_OK)
12347c478bd9Sstevel@tonic-gate 		return (Z_ERR);
12357c478bd9Sstevel@tonic-gate 
12367c478bd9Sstevel@tonic-gate 	zarg.cmd = Z_READY;
12377c478bd9Sstevel@tonic-gate 	if (call_zoneadmd(target_zone, &zarg) != 0) {
12387c478bd9Sstevel@tonic-gate 		zerror(gettext("call to %s failed"), "zoneadmd");
12397c478bd9Sstevel@tonic-gate 		return (Z_ERR);
12407c478bd9Sstevel@tonic-gate 	}
12417c478bd9Sstevel@tonic-gate 	return (Z_OK);
12427c478bd9Sstevel@tonic-gate }
12437c478bd9Sstevel@tonic-gate 
12447c478bd9Sstevel@tonic-gate static int
12457c478bd9Sstevel@tonic-gate boot_func(int argc, char *argv[])
12467c478bd9Sstevel@tonic-gate {
12477c478bd9Sstevel@tonic-gate 	zone_cmd_arg_t zarg;
12487c478bd9Sstevel@tonic-gate 	int arg;
12497c478bd9Sstevel@tonic-gate 
1250108322fbScarlsonj 	if (zonecfg_in_alt_root()) {
1251108322fbScarlsonj 		zerror(gettext("cannot boot zone in alternate root"));
1252108322fbScarlsonj 		return (Z_ERR);
1253108322fbScarlsonj 	}
1254108322fbScarlsonj 
12557c478bd9Sstevel@tonic-gate 	zarg.bootbuf[0] = '\0';
12567c478bd9Sstevel@tonic-gate 
12577c478bd9Sstevel@tonic-gate 	/*
12587c478bd9Sstevel@tonic-gate 	 * At the current time, the only supported subargument to the
12597c478bd9Sstevel@tonic-gate 	 * "boot" subcommand is "-s" which specifies a single-user boot.
12607c478bd9Sstevel@tonic-gate 	 * In the future, other boot arguments should be supported
12617c478bd9Sstevel@tonic-gate 	 * including "-m" for specifying alternate smf(5) milestones.
12627c478bd9Sstevel@tonic-gate 	 */
12637c478bd9Sstevel@tonic-gate 	optind = 0;
12647c478bd9Sstevel@tonic-gate 	if ((arg = getopt(argc, argv, "?s")) != EOF) {
12657c478bd9Sstevel@tonic-gate 		switch (arg) {
12667c478bd9Sstevel@tonic-gate 		case '?':
12677c478bd9Sstevel@tonic-gate 			sub_usage(SHELP_BOOT, CMD_BOOT);
12687c478bd9Sstevel@tonic-gate 			return (optopt == '?' ? Z_OK : Z_USAGE);
12697c478bd9Sstevel@tonic-gate 		case 's':
12707c478bd9Sstevel@tonic-gate 			(void) strlcpy(zarg.bootbuf, "-s",
12717c478bd9Sstevel@tonic-gate 			    sizeof (zarg.bootbuf));
12727c478bd9Sstevel@tonic-gate 			break;
12737c478bd9Sstevel@tonic-gate 		default:
12747c478bd9Sstevel@tonic-gate 			sub_usage(SHELP_BOOT, CMD_BOOT);
12757c478bd9Sstevel@tonic-gate 			return (Z_USAGE);
12767c478bd9Sstevel@tonic-gate 		}
12777c478bd9Sstevel@tonic-gate 	}
12787c478bd9Sstevel@tonic-gate 	if (argc > optind) {
12797c478bd9Sstevel@tonic-gate 		sub_usage(SHELP_BOOT, CMD_BOOT);
12807c478bd9Sstevel@tonic-gate 		return (Z_USAGE);
12817c478bd9Sstevel@tonic-gate 	}
12827c478bd9Sstevel@tonic-gate 	if (sanity_check(target_zone, CMD_BOOT, B_FALSE, B_FALSE) != Z_OK)
12837c478bd9Sstevel@tonic-gate 		return (Z_ERR);
12847c478bd9Sstevel@tonic-gate 	if (verify_details(CMD_BOOT) != Z_OK)
12857c478bd9Sstevel@tonic-gate 		return (Z_ERR);
12867c478bd9Sstevel@tonic-gate 	zarg.cmd = Z_BOOT;
12877c478bd9Sstevel@tonic-gate 	if (call_zoneadmd(target_zone, &zarg) != 0) {
12887c478bd9Sstevel@tonic-gate 		zerror(gettext("call to %s failed"), "zoneadmd");
12897c478bd9Sstevel@tonic-gate 		return (Z_ERR);
12907c478bd9Sstevel@tonic-gate 	}
12917c478bd9Sstevel@tonic-gate 	return (Z_OK);
12927c478bd9Sstevel@tonic-gate }
12937c478bd9Sstevel@tonic-gate 
12947c478bd9Sstevel@tonic-gate static void
12957c478bd9Sstevel@tonic-gate fake_up_local_zone(zoneid_t zid, zone_entry_t *zeptr)
12967c478bd9Sstevel@tonic-gate {
12977c478bd9Sstevel@tonic-gate 	ssize_t result;
12987c478bd9Sstevel@tonic-gate 
12997c478bd9Sstevel@tonic-gate 	zeptr->zid = zid;
13007c478bd9Sstevel@tonic-gate 	/*
13017c478bd9Sstevel@tonic-gate 	 * Since we're looking up our own (non-global) zone name,
13027c478bd9Sstevel@tonic-gate 	 * we can be assured that it will succeed.
13037c478bd9Sstevel@tonic-gate 	 */
13047c478bd9Sstevel@tonic-gate 	result = getzonenamebyid(zid, zeptr->zname, sizeof (zeptr->zname));
13057c478bd9Sstevel@tonic-gate 	assert(result >= 0);
13067c478bd9Sstevel@tonic-gate 	(void) strlcpy(zeptr->zroot, "/", sizeof (zeptr->zroot));
13077c478bd9Sstevel@tonic-gate 	zeptr->zstate_str = "running";
13087c478bd9Sstevel@tonic-gate }
13097c478bd9Sstevel@tonic-gate 
13107c478bd9Sstevel@tonic-gate static int
13117c478bd9Sstevel@tonic-gate list_func(int argc, char *argv[])
13127c478bd9Sstevel@tonic-gate {
13137c478bd9Sstevel@tonic-gate 	zone_entry_t *zentp, zent;
1314108322fbScarlsonj 	int arg, retv;
13157c478bd9Sstevel@tonic-gate 	boolean_t output = B_FALSE, verbose = B_FALSE, parsable = B_FALSE;
13167c478bd9Sstevel@tonic-gate 	zone_state_t min_state = ZONE_STATE_RUNNING;
13177c478bd9Sstevel@tonic-gate 	zoneid_t zone_id = getzoneid();
13187c478bd9Sstevel@tonic-gate 
13197c478bd9Sstevel@tonic-gate 	if (target_zone == NULL) {
13207c478bd9Sstevel@tonic-gate 		/* all zones: default view to running but allow override */
13217c478bd9Sstevel@tonic-gate 		optind = 0;
13227c478bd9Sstevel@tonic-gate 		while ((arg = getopt(argc, argv, "?cipv")) != EOF) {
13237c478bd9Sstevel@tonic-gate 			switch (arg) {
13247c478bd9Sstevel@tonic-gate 			case '?':
13257c478bd9Sstevel@tonic-gate 				sub_usage(SHELP_LIST, CMD_LIST);
13267c478bd9Sstevel@tonic-gate 				return (optopt == '?' ? Z_OK : Z_USAGE);
13277c478bd9Sstevel@tonic-gate 			case 'c':
13287c478bd9Sstevel@tonic-gate 				min_state = ZONE_STATE_CONFIGURED;
13297c478bd9Sstevel@tonic-gate 				break;
13307c478bd9Sstevel@tonic-gate 			case 'i':
13317c478bd9Sstevel@tonic-gate 				min_state = ZONE_STATE_INSTALLED;
13327c478bd9Sstevel@tonic-gate 				break;
13337c478bd9Sstevel@tonic-gate 			case 'p':
13347c478bd9Sstevel@tonic-gate 				parsable = B_TRUE;
13357c478bd9Sstevel@tonic-gate 				break;
13367c478bd9Sstevel@tonic-gate 			case 'v':
13377c478bd9Sstevel@tonic-gate 				verbose = B_TRUE;
13387c478bd9Sstevel@tonic-gate 				break;
13397c478bd9Sstevel@tonic-gate 			default:
13407c478bd9Sstevel@tonic-gate 				sub_usage(SHELP_LIST, CMD_LIST);
13417c478bd9Sstevel@tonic-gate 				return (Z_USAGE);
13427c478bd9Sstevel@tonic-gate 			}
13437c478bd9Sstevel@tonic-gate 		}
13447c478bd9Sstevel@tonic-gate 		if (parsable && verbose) {
13457c478bd9Sstevel@tonic-gate 			zerror(gettext("%s -p and -v are mutually exclusive."),
13467c478bd9Sstevel@tonic-gate 			    cmd_to_str(CMD_LIST));
13477c478bd9Sstevel@tonic-gate 			return (Z_ERR);
13487c478bd9Sstevel@tonic-gate 		}
13497c478bd9Sstevel@tonic-gate 		if (zone_id == GLOBAL_ZONEID) {
1350108322fbScarlsonj 			retv = zone_print_list(min_state, verbose, parsable);
13517c478bd9Sstevel@tonic-gate 		} else {
1352108322fbScarlsonj 			retv = Z_OK;
13537c478bd9Sstevel@tonic-gate 			fake_up_local_zone(zone_id, &zent);
13547c478bd9Sstevel@tonic-gate 			zone_print(&zent, verbose, parsable);
13557c478bd9Sstevel@tonic-gate 		}
1356108322fbScarlsonj 		return (retv);
13577c478bd9Sstevel@tonic-gate 	}
13587c478bd9Sstevel@tonic-gate 
13597c478bd9Sstevel@tonic-gate 	/*
13607c478bd9Sstevel@tonic-gate 	 * Specific target zone: disallow -i/-c suboptions.
13617c478bd9Sstevel@tonic-gate 	 */
13627c478bd9Sstevel@tonic-gate 	optind = 0;
13637c478bd9Sstevel@tonic-gate 	while ((arg = getopt(argc, argv, "?pv")) != EOF) {
13647c478bd9Sstevel@tonic-gate 		switch (arg) {
13657c478bd9Sstevel@tonic-gate 		case '?':
13667c478bd9Sstevel@tonic-gate 			sub_usage(SHELP_LIST, CMD_LIST);
13677c478bd9Sstevel@tonic-gate 			return (optopt == '?' ? Z_OK : Z_USAGE);
13687c478bd9Sstevel@tonic-gate 		case 'p':
13697c478bd9Sstevel@tonic-gate 			parsable = B_TRUE;
13707c478bd9Sstevel@tonic-gate 			break;
13717c478bd9Sstevel@tonic-gate 		case 'v':
13727c478bd9Sstevel@tonic-gate 			verbose = B_TRUE;
13737c478bd9Sstevel@tonic-gate 			break;
13747c478bd9Sstevel@tonic-gate 		default:
13757c478bd9Sstevel@tonic-gate 			sub_usage(SHELP_LIST, CMD_LIST);
13767c478bd9Sstevel@tonic-gate 			return (Z_USAGE);
13777c478bd9Sstevel@tonic-gate 		}
13787c478bd9Sstevel@tonic-gate 	}
13797c478bd9Sstevel@tonic-gate 	if (parsable && verbose) {
13807c478bd9Sstevel@tonic-gate 		zerror(gettext("%s -p and -v are mutually exclusive."),
13817c478bd9Sstevel@tonic-gate 		    cmd_to_str(CMD_LIST));
13827c478bd9Sstevel@tonic-gate 		return (Z_ERR);
13837c478bd9Sstevel@tonic-gate 	}
13847c478bd9Sstevel@tonic-gate 	if (argc > optind) {
13857c478bd9Sstevel@tonic-gate 		sub_usage(SHELP_LIST, CMD_LIST);
13867c478bd9Sstevel@tonic-gate 		return (Z_USAGE);
13877c478bd9Sstevel@tonic-gate 	}
13887c478bd9Sstevel@tonic-gate 	if (zone_id != GLOBAL_ZONEID) {
13897c478bd9Sstevel@tonic-gate 		fake_up_local_zone(zone_id, &zent);
13907c478bd9Sstevel@tonic-gate 		/*
13917c478bd9Sstevel@tonic-gate 		 * main() will issue a Z_NO_ZONE error if it cannot get an
13927c478bd9Sstevel@tonic-gate 		 * id for target_zone, which in a non-global zone should
13937c478bd9Sstevel@tonic-gate 		 * happen for any zone name except `zonename`.  Thus we
13947c478bd9Sstevel@tonic-gate 		 * assert() that here but don't otherwise check.
13957c478bd9Sstevel@tonic-gate 		 */
13967c478bd9Sstevel@tonic-gate 		assert(strcmp(zent.zname, target_zone) == 0);
13977c478bd9Sstevel@tonic-gate 		zone_print(&zent, verbose, parsable);
13987c478bd9Sstevel@tonic-gate 		output = B_TRUE;
13997c478bd9Sstevel@tonic-gate 	} else if ((zentp = lookup_running_zone(target_zone)) != NULL) {
14007c478bd9Sstevel@tonic-gate 		zone_print(zentp, verbose, parsable);
14017c478bd9Sstevel@tonic-gate 		output = B_TRUE;
1402108322fbScarlsonj 	} else if (lookup_zone_info(target_zone, ZONE_ID_UNDEFINED,
1403108322fbScarlsonj 	    &zent) == Z_OK) {
14047c478bd9Sstevel@tonic-gate 		zone_print(&zent, verbose, parsable);
14057c478bd9Sstevel@tonic-gate 		output = B_TRUE;
14067c478bd9Sstevel@tonic-gate 	}
14077c478bd9Sstevel@tonic-gate 	return (output ? Z_OK : Z_ERR);
14087c478bd9Sstevel@tonic-gate }
14097c478bd9Sstevel@tonic-gate 
14107c478bd9Sstevel@tonic-gate static void
14117c478bd9Sstevel@tonic-gate sigterm(int sig)
14127c478bd9Sstevel@tonic-gate {
14137c478bd9Sstevel@tonic-gate 	/*
14147c478bd9Sstevel@tonic-gate 	 * Ignore SIG{INT,TERM}, so we don't end up in an infinite loop,
14157c478bd9Sstevel@tonic-gate 	 * then propagate the signal to our process group.
14167c478bd9Sstevel@tonic-gate 	 */
14177c478bd9Sstevel@tonic-gate 	(void) sigset(SIGINT, SIG_IGN);
14187c478bd9Sstevel@tonic-gate 	(void) sigset(SIGTERM, SIG_IGN);
14197c478bd9Sstevel@tonic-gate 	(void) kill(0, sig);
14207c478bd9Sstevel@tonic-gate 	child_killed = B_TRUE;
14217c478bd9Sstevel@tonic-gate }
14227c478bd9Sstevel@tonic-gate 
14237c478bd9Sstevel@tonic-gate static int
14247c478bd9Sstevel@tonic-gate do_subproc(char *cmdbuf)
14257c478bd9Sstevel@tonic-gate {
14267c478bd9Sstevel@tonic-gate 	char inbuf[1024];	/* arbitrary large amount */
14277c478bd9Sstevel@tonic-gate 	FILE *file;
14287c478bd9Sstevel@tonic-gate 
14297c478bd9Sstevel@tonic-gate 	child_killed = B_FALSE;
14307c478bd9Sstevel@tonic-gate 	/*
14317c478bd9Sstevel@tonic-gate 	 * We use popen(3c) to launch child processes for [un]install;
14327c478bd9Sstevel@tonic-gate 	 * this library call does not return a PID, so we have to kill
14337c478bd9Sstevel@tonic-gate 	 * the whole process group.  To avoid killing our parent, we
14347c478bd9Sstevel@tonic-gate 	 * become a process group leader here.  But doing so can wreak
14357c478bd9Sstevel@tonic-gate 	 * havoc with reading from stdin when launched by a non-job-control
14367c478bd9Sstevel@tonic-gate 	 * shell, so we close stdin and reopen it as /dev/null first.
14377c478bd9Sstevel@tonic-gate 	 */
14387c478bd9Sstevel@tonic-gate 	(void) close(STDIN_FILENO);
14397c478bd9Sstevel@tonic-gate 	(void) open("/dev/null", O_RDONLY);
14407c478bd9Sstevel@tonic-gate 	(void) setpgid(0, 0);
14417c478bd9Sstevel@tonic-gate 	(void) sigset(SIGINT, sigterm);
14427c478bd9Sstevel@tonic-gate 	(void) sigset(SIGTERM, sigterm);
14437c478bd9Sstevel@tonic-gate 	file = popen(cmdbuf, "r");
14447c478bd9Sstevel@tonic-gate 	for (;;) {
14457c478bd9Sstevel@tonic-gate 		if (child_killed || fgets(inbuf, sizeof (inbuf), file) == NULL)
14467c478bd9Sstevel@tonic-gate 			break;
14477c478bd9Sstevel@tonic-gate 		(void) fputs(inbuf, stdout);
14487c478bd9Sstevel@tonic-gate 	}
14497c478bd9Sstevel@tonic-gate 	(void) sigset(SIGINT, SIG_DFL);
14507c478bd9Sstevel@tonic-gate 	(void) sigset(SIGTERM, SIG_DFL);
14517c478bd9Sstevel@tonic-gate 	return (pclose(file));
14527c478bd9Sstevel@tonic-gate }
14537c478bd9Sstevel@tonic-gate 
14547c478bd9Sstevel@tonic-gate static int
14557c478bd9Sstevel@tonic-gate subproc_status(const char *cmd, int status)
14567c478bd9Sstevel@tonic-gate {
14577c478bd9Sstevel@tonic-gate 	if (WIFEXITED(status)) {
14587c478bd9Sstevel@tonic-gate 		int exit_code = WEXITSTATUS(status);
14597c478bd9Sstevel@tonic-gate 
14607c478bd9Sstevel@tonic-gate 		if (exit_code == 0)
14617c478bd9Sstevel@tonic-gate 			return (Z_OK);
14627c478bd9Sstevel@tonic-gate 		zerror(gettext("'%s' failed with exit code %d."), cmd,
14637c478bd9Sstevel@tonic-gate 		    exit_code);
14647c478bd9Sstevel@tonic-gate 	} else if (WIFSIGNALED(status)) {
14657c478bd9Sstevel@tonic-gate 		int signal = WTERMSIG(status);
14667c478bd9Sstevel@tonic-gate 		char sigstr[SIG2STR_MAX];
14677c478bd9Sstevel@tonic-gate 
14687c478bd9Sstevel@tonic-gate 		if (sig2str(signal, sigstr) == 0) {
14697c478bd9Sstevel@tonic-gate 			zerror(gettext("'%s' terminated by signal SIG%s."), cmd,
14707c478bd9Sstevel@tonic-gate 			    sigstr);
14717c478bd9Sstevel@tonic-gate 		} else {
14727c478bd9Sstevel@tonic-gate 			zerror(gettext("'%s' terminated by an unknown signal."),
14737c478bd9Sstevel@tonic-gate 			    cmd);
14747c478bd9Sstevel@tonic-gate 		}
14757c478bd9Sstevel@tonic-gate 	} else {
14767c478bd9Sstevel@tonic-gate 		zerror(gettext("'%s' failed for unknown reasons."), cmd);
14777c478bd9Sstevel@tonic-gate 	}
14787c478bd9Sstevel@tonic-gate 	return (Z_ERR);
14797c478bd9Sstevel@tonic-gate }
14807c478bd9Sstevel@tonic-gate 
14817c478bd9Sstevel@tonic-gate /*
14827c478bd9Sstevel@tonic-gate  * Various sanity checks; make sure:
14837c478bd9Sstevel@tonic-gate  * 1. We're in the global zone.
14847c478bd9Sstevel@tonic-gate  * 2. The calling user has sufficient privilege.
14857c478bd9Sstevel@tonic-gate  * 3. The target zone is neither the global zone nor anything starting with
14867c478bd9Sstevel@tonic-gate  *    "SUNW".
14877c478bd9Sstevel@tonic-gate  * 4a. If we're looking for a 'not running' (i.e., configured or installed)
14887c478bd9Sstevel@tonic-gate  *     zone, the name service knows about it.
14897c478bd9Sstevel@tonic-gate  * 4b. For some operations which expect a zone not to be running, that it is
14907c478bd9Sstevel@tonic-gate  *     not already running (or ready).
14917c478bd9Sstevel@tonic-gate  */
14927c478bd9Sstevel@tonic-gate static int
14937c478bd9Sstevel@tonic-gate sanity_check(char *zone, int cmd_num, boolean_t running,
14947c478bd9Sstevel@tonic-gate     boolean_t unsafe_when_running)
14957c478bd9Sstevel@tonic-gate {
14967c478bd9Sstevel@tonic-gate 	zone_entry_t *zent;
14977c478bd9Sstevel@tonic-gate 	priv_set_t *privset;
14987c478bd9Sstevel@tonic-gate 	zone_state_t state;
1499108322fbScarlsonj 	char kernzone[ZONENAME_MAX];
1500108322fbScarlsonj 	FILE *fp;
15017c478bd9Sstevel@tonic-gate 
15027c478bd9Sstevel@tonic-gate 	if (getzoneid() != GLOBAL_ZONEID) {
15037c478bd9Sstevel@tonic-gate 		zerror(gettext("must be in the global zone to %s a zone."),
15047c478bd9Sstevel@tonic-gate 		    cmd_to_str(cmd_num));
15057c478bd9Sstevel@tonic-gate 		return (Z_ERR);
15067c478bd9Sstevel@tonic-gate 	}
15077c478bd9Sstevel@tonic-gate 
15087c478bd9Sstevel@tonic-gate 	if ((privset = priv_allocset()) == NULL) {
15097c478bd9Sstevel@tonic-gate 		zerror(gettext("%s failed"), "priv_allocset");
15107c478bd9Sstevel@tonic-gate 		return (Z_ERR);
15117c478bd9Sstevel@tonic-gate 	}
15127c478bd9Sstevel@tonic-gate 
15137c478bd9Sstevel@tonic-gate 	if (getppriv(PRIV_EFFECTIVE, privset) != 0) {
15147c478bd9Sstevel@tonic-gate 		zerror(gettext("%s failed"), "getppriv");
15157c478bd9Sstevel@tonic-gate 		priv_freeset(privset);
15167c478bd9Sstevel@tonic-gate 		return (Z_ERR);
15177c478bd9Sstevel@tonic-gate 	}
15187c478bd9Sstevel@tonic-gate 
15197c478bd9Sstevel@tonic-gate 	if (priv_isfullset(privset) == B_FALSE) {
15207c478bd9Sstevel@tonic-gate 		zerror(gettext("only a privileged user may %s a zone."),
15217c478bd9Sstevel@tonic-gate 		    cmd_to_str(cmd_num));
15227c478bd9Sstevel@tonic-gate 		priv_freeset(privset);
15237c478bd9Sstevel@tonic-gate 		return (Z_ERR);
15247c478bd9Sstevel@tonic-gate 	}
15257c478bd9Sstevel@tonic-gate 	priv_freeset(privset);
15267c478bd9Sstevel@tonic-gate 
15277c478bd9Sstevel@tonic-gate 	if (zone == NULL) {
15287c478bd9Sstevel@tonic-gate 		zerror(gettext("no zone specified"));
15297c478bd9Sstevel@tonic-gate 		return (Z_ERR);
15307c478bd9Sstevel@tonic-gate 	}
15317c478bd9Sstevel@tonic-gate 
15327c478bd9Sstevel@tonic-gate 	if (strcmp(zone, GLOBAL_ZONENAME) == 0) {
15337c478bd9Sstevel@tonic-gate 		zerror(gettext("%s operation is invalid for the global zone."),
15347c478bd9Sstevel@tonic-gate 		    cmd_to_str(cmd_num));
15357c478bd9Sstevel@tonic-gate 		return (Z_ERR);
15367c478bd9Sstevel@tonic-gate 	}
15377c478bd9Sstevel@tonic-gate 
15387c478bd9Sstevel@tonic-gate 	if (strncmp(zone, "SUNW", 4) == 0) {
15397c478bd9Sstevel@tonic-gate 		zerror(gettext("%s operation is invalid for zones starting "
15407c478bd9Sstevel@tonic-gate 		    "with SUNW."), cmd_to_str(cmd_num));
15417c478bd9Sstevel@tonic-gate 		return (Z_ERR);
15427c478bd9Sstevel@tonic-gate 	}
15437c478bd9Sstevel@tonic-gate 
1544108322fbScarlsonj 	if (!zonecfg_in_alt_root()) {
1545108322fbScarlsonj 		zent = lookup_running_zone(zone);
1546108322fbScarlsonj 	} else if ((fp = zonecfg_open_scratch("", B_FALSE)) == NULL) {
1547108322fbScarlsonj 		zent = NULL;
1548108322fbScarlsonj 	} else {
1549108322fbScarlsonj 		if (zonecfg_find_scratch(fp, zone, zonecfg_get_root(),
1550108322fbScarlsonj 		    kernzone, sizeof (kernzone)) == 0)
1551108322fbScarlsonj 			zent = lookup_running_zone(kernzone);
1552108322fbScarlsonj 		else
1553108322fbScarlsonj 			zent = NULL;
1554108322fbScarlsonj 		zonecfg_close_scratch(fp);
1555108322fbScarlsonj 	}
1556108322fbScarlsonj 
15577c478bd9Sstevel@tonic-gate 	/*
15587c478bd9Sstevel@tonic-gate 	 * Look up from the kernel for 'running' zones.
15597c478bd9Sstevel@tonic-gate 	 */
15607c478bd9Sstevel@tonic-gate 	if (running) {
15617c478bd9Sstevel@tonic-gate 		if (zent == NULL) {
15627c478bd9Sstevel@tonic-gate 			zerror(gettext("not running"));
15637c478bd9Sstevel@tonic-gate 			return (Z_ERR);
15647c478bd9Sstevel@tonic-gate 		}
15657c478bd9Sstevel@tonic-gate 	} else {
15667c478bd9Sstevel@tonic-gate 		int err;
15677c478bd9Sstevel@tonic-gate 
15687c478bd9Sstevel@tonic-gate 		if (unsafe_when_running && zent != NULL) {
15697c478bd9Sstevel@tonic-gate 			/* check whether the zone is ready or running */
15707c478bd9Sstevel@tonic-gate 			if ((err = zone_get_state(zent->zname,
15717c478bd9Sstevel@tonic-gate 			    &zent->zstate_num)) != Z_OK) {
15727c478bd9Sstevel@tonic-gate 				errno = err;
15737c478bd9Sstevel@tonic-gate 				zperror2(zent->zname,
15747c478bd9Sstevel@tonic-gate 				    gettext("could not get state"));
15757c478bd9Sstevel@tonic-gate 				/* can't tell, so hedge */
15767c478bd9Sstevel@tonic-gate 				zent->zstate_str = "ready/running";
15777c478bd9Sstevel@tonic-gate 			} else {
15787c478bd9Sstevel@tonic-gate 				zent->zstate_str =
15797c478bd9Sstevel@tonic-gate 				    zone_state_str(zent->zstate_num);
15807c478bd9Sstevel@tonic-gate 			}
15817c478bd9Sstevel@tonic-gate 			zerror(gettext("%s operation is invalid for %s zones."),
15827c478bd9Sstevel@tonic-gate 			    cmd_to_str(cmd_num), zent->zstate_str);
15837c478bd9Sstevel@tonic-gate 			return (Z_ERR);
15847c478bd9Sstevel@tonic-gate 		}
15857c478bd9Sstevel@tonic-gate 		if ((err = zone_get_state(zone, &state)) != Z_OK) {
15867c478bd9Sstevel@tonic-gate 			errno = err;
15877c478bd9Sstevel@tonic-gate 			zperror2(zone, gettext("could not get state"));
15887c478bd9Sstevel@tonic-gate 			return (Z_ERR);
15897c478bd9Sstevel@tonic-gate 		}
15907c478bd9Sstevel@tonic-gate 		switch (cmd_num) {
15917c478bd9Sstevel@tonic-gate 		case CMD_UNINSTALL:
15927c478bd9Sstevel@tonic-gate 			if (state == ZONE_STATE_CONFIGURED) {
15937c478bd9Sstevel@tonic-gate 				zerror(gettext("is already in state '%s'."),
15947c478bd9Sstevel@tonic-gate 				    zone_state_str(ZONE_STATE_CONFIGURED));
15957c478bd9Sstevel@tonic-gate 				return (Z_ERR);
15967c478bd9Sstevel@tonic-gate 			}
15977c478bd9Sstevel@tonic-gate 			break;
15987c478bd9Sstevel@tonic-gate 		case CMD_INSTALL:
15997c478bd9Sstevel@tonic-gate 			if (state == ZONE_STATE_INSTALLED) {
16007c478bd9Sstevel@tonic-gate 				zerror(gettext("is already %s."),
16017c478bd9Sstevel@tonic-gate 				    zone_state_str(ZONE_STATE_INSTALLED));
16027c478bd9Sstevel@tonic-gate 				return (Z_ERR);
16037c478bd9Sstevel@tonic-gate 			} else if (state == ZONE_STATE_INCOMPLETE) {
16047c478bd9Sstevel@tonic-gate 				zerror(gettext("zone is %s; %s required."),
16057c478bd9Sstevel@tonic-gate 				    zone_state_str(ZONE_STATE_INCOMPLETE),
16067c478bd9Sstevel@tonic-gate 				    cmd_to_str(CMD_UNINSTALL));
16077c478bd9Sstevel@tonic-gate 				return (Z_ERR);
16087c478bd9Sstevel@tonic-gate 			}
16097c478bd9Sstevel@tonic-gate 			break;
16107c478bd9Sstevel@tonic-gate 		case CMD_READY:
16117c478bd9Sstevel@tonic-gate 		case CMD_BOOT:
1612108322fbScarlsonj 		case CMD_MOUNT:
16137c478bd9Sstevel@tonic-gate 			if (state < ZONE_STATE_INSTALLED) {
16147c478bd9Sstevel@tonic-gate 				zerror(gettext("must be %s before %s."),
16157c478bd9Sstevel@tonic-gate 				    zone_state_str(ZONE_STATE_INSTALLED),
16167c478bd9Sstevel@tonic-gate 				    cmd_to_str(cmd_num));
16177c478bd9Sstevel@tonic-gate 				return (Z_ERR);
16187c478bd9Sstevel@tonic-gate 			}
16197c478bd9Sstevel@tonic-gate 			break;
16207c478bd9Sstevel@tonic-gate 		case CMD_VERIFY:
16217c478bd9Sstevel@tonic-gate 			if (state == ZONE_STATE_INCOMPLETE) {
16227c478bd9Sstevel@tonic-gate 				zerror(gettext("zone is %s; %s required."),
16237c478bd9Sstevel@tonic-gate 				    zone_state_str(ZONE_STATE_INCOMPLETE),
16247c478bd9Sstevel@tonic-gate 				    cmd_to_str(CMD_UNINSTALL));
16257c478bd9Sstevel@tonic-gate 				return (Z_ERR);
16267c478bd9Sstevel@tonic-gate 			}
16277c478bd9Sstevel@tonic-gate 			break;
1628108322fbScarlsonj 		case CMD_UNMOUNT:
1629108322fbScarlsonj 			if (state != ZONE_STATE_MOUNTED) {
1630108322fbScarlsonj 				zerror(gettext("must be %s before %s."),
1631108322fbScarlsonj 				    zone_state_str(ZONE_STATE_MOUNTED),
1632108322fbScarlsonj 				    cmd_to_str(cmd_num));
1633108322fbScarlsonj 				return (Z_ERR);
1634108322fbScarlsonj 			}
1635108322fbScarlsonj 			break;
16367c478bd9Sstevel@tonic-gate 		}
16377c478bd9Sstevel@tonic-gate 	}
16387c478bd9Sstevel@tonic-gate 	return (Z_OK);
16397c478bd9Sstevel@tonic-gate }
16407c478bd9Sstevel@tonic-gate 
16417c478bd9Sstevel@tonic-gate static int
16427c478bd9Sstevel@tonic-gate halt_func(int argc, char *argv[])
16437c478bd9Sstevel@tonic-gate {
16447c478bd9Sstevel@tonic-gate 	zone_cmd_arg_t zarg;
16457c478bd9Sstevel@tonic-gate 	int arg;
16467c478bd9Sstevel@tonic-gate 
1647108322fbScarlsonj 	if (zonecfg_in_alt_root()) {
1648108322fbScarlsonj 		zerror(gettext("cannot halt zone in alternate root"));
1649108322fbScarlsonj 		return (Z_ERR);
1650108322fbScarlsonj 	}
1651108322fbScarlsonj 
16527c478bd9Sstevel@tonic-gate 	optind = 0;
16537c478bd9Sstevel@tonic-gate 	if ((arg = getopt(argc, argv, "?")) != EOF) {
16547c478bd9Sstevel@tonic-gate 		switch (arg) {
16557c478bd9Sstevel@tonic-gate 		case '?':
16567c478bd9Sstevel@tonic-gate 			sub_usage(SHELP_HALT, CMD_HALT);
16577c478bd9Sstevel@tonic-gate 			return (optopt == '?' ? Z_OK : Z_USAGE);
16587c478bd9Sstevel@tonic-gate 		default:
16597c478bd9Sstevel@tonic-gate 			sub_usage(SHELP_HALT, CMD_HALT);
16607c478bd9Sstevel@tonic-gate 			return (Z_USAGE);
16617c478bd9Sstevel@tonic-gate 		}
16627c478bd9Sstevel@tonic-gate 	}
16637c478bd9Sstevel@tonic-gate 	if (argc > optind) {
16647c478bd9Sstevel@tonic-gate 		sub_usage(SHELP_HALT, CMD_HALT);
16657c478bd9Sstevel@tonic-gate 		return (Z_USAGE);
16667c478bd9Sstevel@tonic-gate 	}
16677c478bd9Sstevel@tonic-gate 	/*
16687c478bd9Sstevel@tonic-gate 	 * zoneadmd should be the one to decide whether or not to proceed,
16697c478bd9Sstevel@tonic-gate 	 * so even though it seems that the fourth parameter below should
16707c478bd9Sstevel@tonic-gate 	 * perhaps be B_TRUE, it really shouldn't be.
16717c478bd9Sstevel@tonic-gate 	 */
16727c478bd9Sstevel@tonic-gate 	if (sanity_check(target_zone, CMD_HALT, B_FALSE, B_FALSE) != Z_OK)
16737c478bd9Sstevel@tonic-gate 		return (Z_ERR);
16747c478bd9Sstevel@tonic-gate 
16757c478bd9Sstevel@tonic-gate 	zarg.cmd = Z_HALT;
16767c478bd9Sstevel@tonic-gate 	return ((call_zoneadmd(target_zone, &zarg) == 0) ? Z_OK : Z_ERR);
16777c478bd9Sstevel@tonic-gate }
16787c478bd9Sstevel@tonic-gate 
16797c478bd9Sstevel@tonic-gate static int
16807c478bd9Sstevel@tonic-gate reboot_func(int argc, char *argv[])
16817c478bd9Sstevel@tonic-gate {
16827c478bd9Sstevel@tonic-gate 	zone_cmd_arg_t zarg;
16837c478bd9Sstevel@tonic-gate 	int arg;
16847c478bd9Sstevel@tonic-gate 
1685108322fbScarlsonj 	if (zonecfg_in_alt_root()) {
1686108322fbScarlsonj 		zerror(gettext("cannot reboot zone in alternate root"));
1687108322fbScarlsonj 		return (Z_ERR);
1688108322fbScarlsonj 	}
1689108322fbScarlsonj 
16907c478bd9Sstevel@tonic-gate 	optind = 0;
16917c478bd9Sstevel@tonic-gate 	if ((arg = getopt(argc, argv, "?")) != EOF) {
16927c478bd9Sstevel@tonic-gate 		switch (arg) {
16937c478bd9Sstevel@tonic-gate 		case '?':
16947c478bd9Sstevel@tonic-gate 			sub_usage(SHELP_REBOOT, CMD_REBOOT);
16957c478bd9Sstevel@tonic-gate 			return (optopt == '?' ? Z_OK : Z_USAGE);
16967c478bd9Sstevel@tonic-gate 		default:
16977c478bd9Sstevel@tonic-gate 			sub_usage(SHELP_REBOOT, CMD_REBOOT);
16987c478bd9Sstevel@tonic-gate 			return (Z_USAGE);
16997c478bd9Sstevel@tonic-gate 		}
17007c478bd9Sstevel@tonic-gate 	}
17017c478bd9Sstevel@tonic-gate 	if (argc > 0) {
17027c478bd9Sstevel@tonic-gate 		sub_usage(SHELP_REBOOT, CMD_REBOOT);
17037c478bd9Sstevel@tonic-gate 		return (Z_USAGE);
17047c478bd9Sstevel@tonic-gate 	}
17057c478bd9Sstevel@tonic-gate 	/*
17067c478bd9Sstevel@tonic-gate 	 * zoneadmd should be the one to decide whether or not to proceed,
17077c478bd9Sstevel@tonic-gate 	 * so even though it seems that the fourth parameter below should
17087c478bd9Sstevel@tonic-gate 	 * perhaps be B_TRUE, it really shouldn't be.
17097c478bd9Sstevel@tonic-gate 	 */
17107c478bd9Sstevel@tonic-gate 	if (sanity_check(target_zone, CMD_REBOOT, B_TRUE, B_FALSE) != Z_OK)
17117c478bd9Sstevel@tonic-gate 		return (Z_ERR);
1712b5abaf04Sgjelinek 	if (verify_details(CMD_REBOOT) != Z_OK)
1713b5abaf04Sgjelinek 		return (Z_ERR);
17147c478bd9Sstevel@tonic-gate 
17157c478bd9Sstevel@tonic-gate 	zarg.cmd = Z_REBOOT;
17167c478bd9Sstevel@tonic-gate 	return ((call_zoneadmd(target_zone, &zarg) == 0) ? Z_OK : Z_ERR);
17177c478bd9Sstevel@tonic-gate }
17187c478bd9Sstevel@tonic-gate 
17197c478bd9Sstevel@tonic-gate static int
17207c478bd9Sstevel@tonic-gate verify_rctls(zone_dochandle_t handle)
17217c478bd9Sstevel@tonic-gate {
17227c478bd9Sstevel@tonic-gate 	struct zone_rctltab rctltab;
17237c478bd9Sstevel@tonic-gate 	size_t rbs = rctlblk_size();
17247c478bd9Sstevel@tonic-gate 	rctlblk_t *rctlblk;
17257c478bd9Sstevel@tonic-gate 	int error = Z_INVAL;
17267c478bd9Sstevel@tonic-gate 
17277c478bd9Sstevel@tonic-gate 	if ((rctlblk = malloc(rbs)) == NULL) {
17287c478bd9Sstevel@tonic-gate 		zerror(gettext("failed to allocate %lu bytes: %s"), rbs,
17297c478bd9Sstevel@tonic-gate 		    strerror(errno));
17307c478bd9Sstevel@tonic-gate 		return (Z_NOMEM);
17317c478bd9Sstevel@tonic-gate 	}
17327c478bd9Sstevel@tonic-gate 
17337c478bd9Sstevel@tonic-gate 	if (zonecfg_setrctlent(handle) != Z_OK) {
17347c478bd9Sstevel@tonic-gate 		zerror(gettext("zonecfg_setrctlent failed"));
17357c478bd9Sstevel@tonic-gate 		free(rctlblk);
17367c478bd9Sstevel@tonic-gate 		return (error);
17377c478bd9Sstevel@tonic-gate 	}
17387c478bd9Sstevel@tonic-gate 
17397c478bd9Sstevel@tonic-gate 	rctltab.zone_rctl_valptr = NULL;
17407c478bd9Sstevel@tonic-gate 	while (zonecfg_getrctlent(handle, &rctltab) == Z_OK) {
17417c478bd9Sstevel@tonic-gate 		struct zone_rctlvaltab *rctlval;
17427c478bd9Sstevel@tonic-gate 		const char *name = rctltab.zone_rctl_name;
17437c478bd9Sstevel@tonic-gate 
17447c478bd9Sstevel@tonic-gate 		if (!zonecfg_is_rctl(name)) {
17457c478bd9Sstevel@tonic-gate 			zerror(gettext("WARNING: Ignoring unrecognized rctl "
17467c478bd9Sstevel@tonic-gate 			    "'%s'."),  name);
17477c478bd9Sstevel@tonic-gate 			zonecfg_free_rctl_value_list(rctltab.zone_rctl_valptr);
17487c478bd9Sstevel@tonic-gate 			rctltab.zone_rctl_valptr = NULL;
17497c478bd9Sstevel@tonic-gate 			continue;
17507c478bd9Sstevel@tonic-gate 		}
17517c478bd9Sstevel@tonic-gate 
17527c478bd9Sstevel@tonic-gate 		for (rctlval = rctltab.zone_rctl_valptr; rctlval != NULL;
17537c478bd9Sstevel@tonic-gate 		    rctlval = rctlval->zone_rctlval_next) {
17547c478bd9Sstevel@tonic-gate 			if (zonecfg_construct_rctlblk(rctlval, rctlblk)
17557c478bd9Sstevel@tonic-gate 			    != Z_OK) {
17567c478bd9Sstevel@tonic-gate 				zerror(gettext("invalid rctl value: "
17577c478bd9Sstevel@tonic-gate 				    "(priv=%s,limit=%s,action%s)"),
17587c478bd9Sstevel@tonic-gate 				    rctlval->zone_rctlval_priv,
17597c478bd9Sstevel@tonic-gate 				    rctlval->zone_rctlval_limit,
17607c478bd9Sstevel@tonic-gate 				    rctlval->zone_rctlval_action);
17617c478bd9Sstevel@tonic-gate 				goto out;
17627c478bd9Sstevel@tonic-gate 			}
17637c478bd9Sstevel@tonic-gate 			if (!zonecfg_valid_rctl(name, rctlblk)) {
17647c478bd9Sstevel@tonic-gate 				zerror(gettext("(priv=%s,limit=%s,action=%s) "
17657c478bd9Sstevel@tonic-gate 				    "is not a valid value for rctl '%s'"),
17667c478bd9Sstevel@tonic-gate 				    rctlval->zone_rctlval_priv,
17677c478bd9Sstevel@tonic-gate 				    rctlval->zone_rctlval_limit,
17687c478bd9Sstevel@tonic-gate 				    rctlval->zone_rctlval_action,
17697c478bd9Sstevel@tonic-gate 				    name);
17707c478bd9Sstevel@tonic-gate 				goto out;
17717c478bd9Sstevel@tonic-gate 			}
17727c478bd9Sstevel@tonic-gate 		}
17737c478bd9Sstevel@tonic-gate 		zonecfg_free_rctl_value_list(rctltab.zone_rctl_valptr);
17747c478bd9Sstevel@tonic-gate 	}
17757c478bd9Sstevel@tonic-gate 	rctltab.zone_rctl_valptr = NULL;
17767c478bd9Sstevel@tonic-gate 	error = Z_OK;
17777c478bd9Sstevel@tonic-gate out:
17787c478bd9Sstevel@tonic-gate 	zonecfg_free_rctl_value_list(rctltab.zone_rctl_valptr);
17797c478bd9Sstevel@tonic-gate 	(void) zonecfg_endrctlent(handle);
17807c478bd9Sstevel@tonic-gate 	free(rctlblk);
17817c478bd9Sstevel@tonic-gate 	return (error);
17827c478bd9Sstevel@tonic-gate }
17837c478bd9Sstevel@tonic-gate 
17847c478bd9Sstevel@tonic-gate static int
17857c478bd9Sstevel@tonic-gate verify_pool(zone_dochandle_t handle)
17867c478bd9Sstevel@tonic-gate {
17877c478bd9Sstevel@tonic-gate 	char poolname[MAXPATHLEN];
17887c478bd9Sstevel@tonic-gate 	pool_conf_t *poolconf;
17897c478bd9Sstevel@tonic-gate 	pool_t *pool;
17907c478bd9Sstevel@tonic-gate 	int status;
17917c478bd9Sstevel@tonic-gate 	int error;
17927c478bd9Sstevel@tonic-gate 
17937c478bd9Sstevel@tonic-gate 	/*
17947c478bd9Sstevel@tonic-gate 	 * This ends up being very similar to the check done in zoneadmd.
17957c478bd9Sstevel@tonic-gate 	 */
17967c478bd9Sstevel@tonic-gate 	error = zonecfg_get_pool(handle, poolname, sizeof (poolname));
17977c478bd9Sstevel@tonic-gate 	if (error == Z_NO_ENTRY || (error == Z_OK && strlen(poolname) == 0)) {
17987c478bd9Sstevel@tonic-gate 		/*
17997c478bd9Sstevel@tonic-gate 		 * No pool specified.
18007c478bd9Sstevel@tonic-gate 		 */
18017c478bd9Sstevel@tonic-gate 		return (0);
18027c478bd9Sstevel@tonic-gate 	}
18037c478bd9Sstevel@tonic-gate 	if (error != Z_OK) {
18047c478bd9Sstevel@tonic-gate 		zperror(gettext("Unable to retrieve pool name from "
18057c478bd9Sstevel@tonic-gate 		    "configuration"), B_TRUE);
18067c478bd9Sstevel@tonic-gate 		return (error);
18077c478bd9Sstevel@tonic-gate 	}
18087c478bd9Sstevel@tonic-gate 	/*
18097c478bd9Sstevel@tonic-gate 	 * Don't do anything if pools aren't enabled.
18107c478bd9Sstevel@tonic-gate 	 */
18117c478bd9Sstevel@tonic-gate 	if (pool_get_status(&status) != PO_SUCCESS || status != POOL_ENABLED) {
18127c478bd9Sstevel@tonic-gate 		zerror(gettext("WARNING: pools facility not active; "
18137c478bd9Sstevel@tonic-gate 		    "zone will not be bound to pool '%s'."), poolname);
18147c478bd9Sstevel@tonic-gate 		return (Z_OK);
18157c478bd9Sstevel@tonic-gate 	}
18167c478bd9Sstevel@tonic-gate 	/*
18177c478bd9Sstevel@tonic-gate 	 * Try to provide a sane error message if the requested pool doesn't
18187c478bd9Sstevel@tonic-gate 	 * exist.  It isn't clear that pools-related failures should
18197c478bd9Sstevel@tonic-gate 	 * necessarily translate to a failure to verify the zone configuration,
18207c478bd9Sstevel@tonic-gate 	 * hence they are not considered errors.
18217c478bd9Sstevel@tonic-gate 	 */
18227c478bd9Sstevel@tonic-gate 	if ((poolconf = pool_conf_alloc()) == NULL) {
18237c478bd9Sstevel@tonic-gate 		zerror(gettext("WARNING: pool_conf_alloc failed; "
18247c478bd9Sstevel@tonic-gate 		    "using default pool"));
18257c478bd9Sstevel@tonic-gate 		return (Z_OK);
18267c478bd9Sstevel@tonic-gate 	}
18277c478bd9Sstevel@tonic-gate 	if (pool_conf_open(poolconf, pool_dynamic_location(), PO_RDONLY) !=
18287c478bd9Sstevel@tonic-gate 	    PO_SUCCESS) {
18297c478bd9Sstevel@tonic-gate 		zerror(gettext("WARNING: pool_conf_open failed; "
18307c478bd9Sstevel@tonic-gate 		    "using default pool"));
18317c478bd9Sstevel@tonic-gate 		pool_conf_free(poolconf);
18327c478bd9Sstevel@tonic-gate 		return (Z_OK);
18337c478bd9Sstevel@tonic-gate 	}
18347c478bd9Sstevel@tonic-gate 	pool = pool_get_pool(poolconf, poolname);
18357c478bd9Sstevel@tonic-gate 	(void) pool_conf_close(poolconf);
18367c478bd9Sstevel@tonic-gate 	pool_conf_free(poolconf);
18377c478bd9Sstevel@tonic-gate 	if (pool == NULL) {
18387c478bd9Sstevel@tonic-gate 		zerror(gettext("WARNING: pool '%s' not found. "
18397c478bd9Sstevel@tonic-gate 		    "using default pool"), poolname);
18407c478bd9Sstevel@tonic-gate 	}
18417c478bd9Sstevel@tonic-gate 
18427c478bd9Sstevel@tonic-gate 	return (Z_OK);
18437c478bd9Sstevel@tonic-gate }
18447c478bd9Sstevel@tonic-gate 
18457c478bd9Sstevel@tonic-gate static int
1846b5abaf04Sgjelinek verify_ipd(zone_dochandle_t handle)
1847b5abaf04Sgjelinek {
1848b5abaf04Sgjelinek 	int return_code = Z_OK;
1849b5abaf04Sgjelinek 	struct zone_fstab fstab;
1850b5abaf04Sgjelinek 	struct stat st;
1851b5abaf04Sgjelinek 	char specdir[MAXPATHLEN];
1852b5abaf04Sgjelinek 
1853b5abaf04Sgjelinek 	if (zonecfg_setipdent(handle) != Z_OK) {
1854*5ee84fbdSgjelinek 		/*
1855*5ee84fbdSgjelinek 		 * TRANSLATION_NOTE
1856*5ee84fbdSgjelinek 		 * inherit-pkg-dirs is a literal that should not be translated.
1857*5ee84fbdSgjelinek 		 */
1858*5ee84fbdSgjelinek 		(void) fprintf(stderr, gettext("could not verify "
1859b5abaf04Sgjelinek 		    "inherit-pkg-dirs: unable to enumerate mounts\n"));
1860b5abaf04Sgjelinek 		return (Z_ERR);
1861b5abaf04Sgjelinek 	}
1862b5abaf04Sgjelinek 	while (zonecfg_getipdent(handle, &fstab) == Z_OK) {
1863b5abaf04Sgjelinek 		/*
1864b5abaf04Sgjelinek 		 * Verify fs_dir exists.
1865b5abaf04Sgjelinek 		 */
1866b5abaf04Sgjelinek 		(void) snprintf(specdir, sizeof (specdir), "%s%s",
1867b5abaf04Sgjelinek 		    zonecfg_get_root(), fstab.zone_fs_dir);
1868b5abaf04Sgjelinek 		if (stat(specdir, &st) != 0) {
1869*5ee84fbdSgjelinek 			/*
1870*5ee84fbdSgjelinek 			 * TRANSLATION_NOTE
1871*5ee84fbdSgjelinek 			 * inherit-pkg-dir is a literal that should not be
1872*5ee84fbdSgjelinek 			 * translated.
1873*5ee84fbdSgjelinek 			 */
1874*5ee84fbdSgjelinek 			(void) fprintf(stderr, gettext("could not verify "
1875b5abaf04Sgjelinek 			    "inherit-pkg-dir %s: %s\n"),
1876b5abaf04Sgjelinek 			    fstab.zone_fs_dir, strerror(errno));
1877b5abaf04Sgjelinek 			return_code = Z_ERR;
1878b5abaf04Sgjelinek 		}
1879b5abaf04Sgjelinek 		if (strcmp(st.st_fstype, MNTTYPE_NFS) == 0) {
1880*5ee84fbdSgjelinek 			/*
1881*5ee84fbdSgjelinek 			 * TRANSLATION_NOTE
1882*5ee84fbdSgjelinek 			 * inherit-pkg-dir and NFS are literals that should
1883*5ee84fbdSgjelinek 			 * not be translated.
1884*5ee84fbdSgjelinek 			 */
1885b5abaf04Sgjelinek 			(void) fprintf(stderr, gettext("cannot verify "
1886*5ee84fbdSgjelinek 			    "inherit-pkg-dir %s: NFS mounted file-system.\n"
1887*5ee84fbdSgjelinek 			    "\tA local file-system must be used.\n"),
1888b5abaf04Sgjelinek 			    fstab.zone_fs_dir);
1889b5abaf04Sgjelinek 			return_code = Z_ERR;
1890b5abaf04Sgjelinek 		}
1891b5abaf04Sgjelinek 	}
1892b5abaf04Sgjelinek 	(void) zonecfg_endipdent(handle);
1893b5abaf04Sgjelinek 
1894b5abaf04Sgjelinek 	return (return_code);
1895b5abaf04Sgjelinek }
1896b5abaf04Sgjelinek 
1897b5abaf04Sgjelinek static int
18987c478bd9Sstevel@tonic-gate verify_filesystems(zone_dochandle_t handle)
18997c478bd9Sstevel@tonic-gate {
19007c478bd9Sstevel@tonic-gate 	int return_code = Z_OK;
19017c478bd9Sstevel@tonic-gate 	struct zone_fstab fstab;
19027c478bd9Sstevel@tonic-gate 	char cmdbuf[MAXPATHLEN];
19037c478bd9Sstevel@tonic-gate 	struct stat st;
19047c478bd9Sstevel@tonic-gate 
19057c478bd9Sstevel@tonic-gate 	/*
19067c478bd9Sstevel@tonic-gate 	 * No need to verify inherit-pkg-dir fs types, as their type is
19077c478bd9Sstevel@tonic-gate 	 * implicitly lofs, which is known.  Therefore, the types are only
19087c478bd9Sstevel@tonic-gate 	 * verified for regular filesystems below.
19097c478bd9Sstevel@tonic-gate 	 *
19107c478bd9Sstevel@tonic-gate 	 * Since the actual mount point is not known until the dependent mounts
19117c478bd9Sstevel@tonic-gate 	 * are performed, we don't attempt any path validation here: that will
19127c478bd9Sstevel@tonic-gate 	 * happen later when zoneadmd actually does the mounts.
19137c478bd9Sstevel@tonic-gate 	 */
19147c478bd9Sstevel@tonic-gate 	if (zonecfg_setfsent(handle) != Z_OK) {
1915*5ee84fbdSgjelinek 		(void) fprintf(stderr, gettext("could not verify file-systems: "
19167c478bd9Sstevel@tonic-gate 		    "unable to enumerate mounts\n"));
19177c478bd9Sstevel@tonic-gate 		return (Z_ERR);
19187c478bd9Sstevel@tonic-gate 	}
19197c478bd9Sstevel@tonic-gate 	while (zonecfg_getfsent(handle, &fstab) == Z_OK) {
19207c478bd9Sstevel@tonic-gate 		if (!zonecfg_valid_fs_type(fstab.zone_fs_type)) {
19217c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("cannot verify fs %s: "
19227c478bd9Sstevel@tonic-gate 			    "type %s is not allowed.\n"), fstab.zone_fs_dir,
19237c478bd9Sstevel@tonic-gate 			    fstab.zone_fs_type);
19247c478bd9Sstevel@tonic-gate 			return_code = Z_ERR;
19257c478bd9Sstevel@tonic-gate 			goto next_fs;
19267c478bd9Sstevel@tonic-gate 		}
19277c478bd9Sstevel@tonic-gate 		/*
19287c478bd9Sstevel@tonic-gate 		 * Verify /usr/lib/fs/<fstype>/mount exists.
19297c478bd9Sstevel@tonic-gate 		 */
19307c478bd9Sstevel@tonic-gate 		if (snprintf(cmdbuf, sizeof (cmdbuf), "/usr/lib/fs/%s/mount",
19317c478bd9Sstevel@tonic-gate 		    fstab.zone_fs_type) > sizeof (cmdbuf)) {
19327c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("cannot verify fs %s: "
19337c478bd9Sstevel@tonic-gate 			    "type %s is too long.\n"), fstab.zone_fs_dir,
19347c478bd9Sstevel@tonic-gate 			    fstab.zone_fs_type);
19357c478bd9Sstevel@tonic-gate 			return_code = Z_ERR;
19367c478bd9Sstevel@tonic-gate 			goto next_fs;
19377c478bd9Sstevel@tonic-gate 		}
19387c478bd9Sstevel@tonic-gate 		if (stat(cmdbuf, &st) != 0) {
1939*5ee84fbdSgjelinek 			(void) fprintf(stderr, gettext("could not verify fs "
1940*5ee84fbdSgjelinek 			    "%s: could not access %s: %s\n"), fstab.zone_fs_dir,
19417c478bd9Sstevel@tonic-gate 			    cmdbuf, strerror(errno));
19427c478bd9Sstevel@tonic-gate 			return_code = Z_ERR;
19437c478bd9Sstevel@tonic-gate 			goto next_fs;
19447c478bd9Sstevel@tonic-gate 		}
19457c478bd9Sstevel@tonic-gate 		if (!S_ISREG(st.st_mode)) {
1946*5ee84fbdSgjelinek 			(void) fprintf(stderr, gettext("could not verify fs "
1947*5ee84fbdSgjelinek 			    "%s: %s is not a regular file\n"),
1948*5ee84fbdSgjelinek 			    fstab.zone_fs_dir, cmdbuf);
19497c478bd9Sstevel@tonic-gate 			return_code = Z_ERR;
19507c478bd9Sstevel@tonic-gate 			goto next_fs;
19517c478bd9Sstevel@tonic-gate 		}
19527c478bd9Sstevel@tonic-gate 		/*
19537c478bd9Sstevel@tonic-gate 		 * Verify /usr/lib/fs/<fstype>/fsck exists iff zone_fs_raw is
19547c478bd9Sstevel@tonic-gate 		 * set.
19557c478bd9Sstevel@tonic-gate 		 */
19567c478bd9Sstevel@tonic-gate 		if (snprintf(cmdbuf, sizeof (cmdbuf), "/usr/lib/fs/%s/fsck",
19577c478bd9Sstevel@tonic-gate 		    fstab.zone_fs_type) > sizeof (cmdbuf)) {
19587c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("cannot verify fs %s: "
19597c478bd9Sstevel@tonic-gate 			    "type %s is too long.\n"), fstab.zone_fs_dir,
19607c478bd9Sstevel@tonic-gate 			    fstab.zone_fs_type);
19617c478bd9Sstevel@tonic-gate 			return_code = Z_ERR;
19627c478bd9Sstevel@tonic-gate 			goto next_fs;
19637c478bd9Sstevel@tonic-gate 		}
19647c478bd9Sstevel@tonic-gate 		if (fstab.zone_fs_raw[0] == '\0' && stat(cmdbuf, &st) == 0) {
1965*5ee84fbdSgjelinek 			(void) fprintf(stderr, gettext("could not verify fs "
1966*5ee84fbdSgjelinek 			    "%s: must specify 'raw' device for %s "
1967*5ee84fbdSgjelinek 			    "file-systems\n"),
19687c478bd9Sstevel@tonic-gate 			    fstab.zone_fs_dir, fstab.zone_fs_type);
19697c478bd9Sstevel@tonic-gate 			return_code = Z_ERR;
19707c478bd9Sstevel@tonic-gate 			goto next_fs;
19717c478bd9Sstevel@tonic-gate 		}
19727c478bd9Sstevel@tonic-gate 		if (fstab.zone_fs_raw[0] != '\0' &&
19737c478bd9Sstevel@tonic-gate 		    (stat(cmdbuf, &st) != 0 || !S_ISREG(st.st_mode))) {
19747c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("cannot verify fs %s: "
19757c478bd9Sstevel@tonic-gate 			    "'raw' device specified but "
19767c478bd9Sstevel@tonic-gate 			    "no fsck executable exists for %s\n"),
19777c478bd9Sstevel@tonic-gate 			    fstab.zone_fs_dir, fstab.zone_fs_type);
19787c478bd9Sstevel@tonic-gate 			return_code = Z_ERR;
19797c478bd9Sstevel@tonic-gate 			goto next_fs;
19807c478bd9Sstevel@tonic-gate 		}
1981b5abaf04Sgjelinek 		/*
1982b5abaf04Sgjelinek 		 * Verify fs_special and optionally fs_raw, exists.
1983b5abaf04Sgjelinek 		 */
1984b5abaf04Sgjelinek 		if (stat(fstab.zone_fs_special, &st) != 0) {
1985*5ee84fbdSgjelinek 			(void) fprintf(stderr, gettext("could not verify fs "
1986*5ee84fbdSgjelinek 			    "%s: could not access %s: %s\n"), fstab.zone_fs_dir,
1987b5abaf04Sgjelinek 			    fstab.zone_fs_special, strerror(errno));
1988b5abaf04Sgjelinek 			return_code = Z_ERR;
1989b5abaf04Sgjelinek 			goto next_fs;
1990b5abaf04Sgjelinek 		}
1991b5abaf04Sgjelinek 		if (strcmp(st.st_fstype, MNTTYPE_NFS) == 0) {
1992*5ee84fbdSgjelinek 			/*
1993*5ee84fbdSgjelinek 			 * TRANSLATION_NOTE
1994*5ee84fbdSgjelinek 			 * fs and NFS are literals that should
1995*5ee84fbdSgjelinek 			 * not be translated.
1996*5ee84fbdSgjelinek 			 */
1997b5abaf04Sgjelinek 			(void) fprintf(stderr, gettext("cannot verify "
1998*5ee84fbdSgjelinek 			    "fs %s: NFS mounted file-system.\n"
1999*5ee84fbdSgjelinek 			    "\tA local file-system must be used.\n"),
2000b5abaf04Sgjelinek 			    fstab.zone_fs_special);
2001b5abaf04Sgjelinek 			return_code = Z_ERR;
2002b5abaf04Sgjelinek 			goto next_fs;
2003b5abaf04Sgjelinek 		}
2004b5abaf04Sgjelinek 		if (fstab.zone_fs_raw[0] != '\0' &&
2005b5abaf04Sgjelinek 		    stat(fstab.zone_fs_raw, &st) != 0) {
2006*5ee84fbdSgjelinek 			/*
2007*5ee84fbdSgjelinek 			 * TRANSLATION_NOTE
2008*5ee84fbdSgjelinek 			 * fs is a literal that should not be translated.
2009*5ee84fbdSgjelinek 			 */
2010*5ee84fbdSgjelinek 			(void) fprintf(stderr, gettext("could not verify fs "
2011*5ee84fbdSgjelinek 			    "%s: could not access %s: %s\n"), fstab.zone_fs_dir,
2012b5abaf04Sgjelinek 			    fstab.zone_fs_raw, strerror(errno));
2013b5abaf04Sgjelinek 			return_code = Z_ERR;
2014b5abaf04Sgjelinek 			goto next_fs;
2015b5abaf04Sgjelinek 		}
20167c478bd9Sstevel@tonic-gate next_fs:
20177c478bd9Sstevel@tonic-gate 		zonecfg_free_fs_option_list(fstab.zone_fs_options);
20187c478bd9Sstevel@tonic-gate 	}
20197c478bd9Sstevel@tonic-gate 	(void) zonecfg_endfsent(handle);
20207c478bd9Sstevel@tonic-gate 
20217c478bd9Sstevel@tonic-gate 	return (return_code);
20227c478bd9Sstevel@tonic-gate }
20237c478bd9Sstevel@tonic-gate 
2024fa9e4066Sahrens const char *current_dataset;
2025fa9e4066Sahrens 
2026fa9e4066Sahrens /*
2027fa9e4066Sahrens  * Custom error handler for errors incurred as part of the checks below.  We
2028fa9e4066Sahrens  * want to trim off the leading 'cannot open ...' to create a better error
2029fa9e4066Sahrens  * message.  The only other way this can fail is if we fail to set the 'zoned'
2030fa9e4066Sahrens  * property.  In this case we just pass the error on verbatim.
2031fa9e4066Sahrens  */
2032fa9e4066Sahrens static void
2033fa9e4066Sahrens zfs_error_handler(const char *fmt, va_list ap)
2034fa9e4066Sahrens {
2035fa9e4066Sahrens 	char buf[1024];
2036fa9e4066Sahrens 
2037fa9e4066Sahrens 	(void) vsnprintf(buf, sizeof (buf), fmt, ap);
2038fa9e4066Sahrens 
2039fa9e4066Sahrens 	if (strncmp(gettext("cannot open "), buf,
2040fa9e4066Sahrens 	    strlen(gettext("cannot open "))) == 0)
2041*5ee84fbdSgjelinek 		/*
2042*5ee84fbdSgjelinek 		 * TRANSLATION_NOTE
2043*5ee84fbdSgjelinek 		 * zfs and dataset are literals that should not be translated.
2044*5ee84fbdSgjelinek 		 */
2045*5ee84fbdSgjelinek 		(void) fprintf(stderr, gettext("could not verify zfs "
2046fa9e4066Sahrens 		    "dataset %s%s\n"), current_dataset, strchr(buf, ':'));
2047fa9e4066Sahrens 	else
2048*5ee84fbdSgjelinek 		(void) fprintf(stderr, gettext("could not verify zfs dataset "
2049fa9e4066Sahrens 		    "%s: %s\n"), current_dataset, buf);
2050fa9e4066Sahrens }
2051fa9e4066Sahrens 
2052fa9e4066Sahrens /* ARGSUSED */
2053fa9e4066Sahrens static int
2054fa9e4066Sahrens check_zvol(zfs_handle_t *zhp, void *unused)
2055fa9e4066Sahrens {
2056fa9e4066Sahrens 	int ret;
2057fa9e4066Sahrens 
2058fa9e4066Sahrens 	if (zfs_get_type(zhp) == ZFS_TYPE_VOLUME) {
2059*5ee84fbdSgjelinek 		/*
2060*5ee84fbdSgjelinek 		 * TRANSLATION_NOTE
2061*5ee84fbdSgjelinek 		 * zfs and dataset are literals that should not be translated.
2062*5ee84fbdSgjelinek 		 */
2063fa9e4066Sahrens 		(void) fprintf(stderr, gettext("cannot verify zfs dataset %s: "
2064fa9e4066Sahrens 		    "volumes cannot be specified as a zone dataset resource\n"),
2065fa9e4066Sahrens 		    zfs_get_name(zhp));
2066fa9e4066Sahrens 		ret = -1;
2067fa9e4066Sahrens 	} else {
2068fa9e4066Sahrens 		ret = zfs_iter_children(zhp, check_zvol, NULL);
2069fa9e4066Sahrens 	}
2070fa9e4066Sahrens 
2071fa9e4066Sahrens 	zfs_close(zhp);
2072fa9e4066Sahrens 
2073fa9e4066Sahrens 	return (ret);
2074fa9e4066Sahrens }
2075fa9e4066Sahrens 
2076fa9e4066Sahrens /*
2077fa9e4066Sahrens  * Validate that the given dataset exists on the system, and that neither it nor
2078fa9e4066Sahrens  * its children are zvols.
2079fa9e4066Sahrens  *
2080fa9e4066Sahrens  * Note that we don't do anything with the 'zoned' property here.  All
2081fa9e4066Sahrens  * management is done in zoneadmd when the zone is actually rebooted.  This
2082fa9e4066Sahrens  * allows us to automatically set the zoned property even when a zone is
2083fa9e4066Sahrens  * rebooted by the administrator.
2084fa9e4066Sahrens  */
2085fa9e4066Sahrens static int
2086fa9e4066Sahrens verify_datasets(zone_dochandle_t handle)
2087fa9e4066Sahrens {
2088fa9e4066Sahrens 	int return_code = Z_OK;
2089fa9e4066Sahrens 	struct zone_dstab dstab;
2090fa9e4066Sahrens 	zfs_handle_t *zhp;
2091fa9e4066Sahrens 	char propbuf[ZFS_MAXPROPLEN];
2092fa9e4066Sahrens 	char source[ZFS_MAXNAMELEN];
2093fa9e4066Sahrens 	zfs_source_t srctype;
2094fa9e4066Sahrens 
2095fa9e4066Sahrens 	if (zonecfg_setdsent(handle) != Z_OK) {
2096*5ee84fbdSgjelinek 		/*
2097*5ee84fbdSgjelinek 		 * TRANSLATION_NOTE
2098*5ee84fbdSgjelinek 		 * zfs and dataset are literals that should not be translated.
2099*5ee84fbdSgjelinek 		 */
2100*5ee84fbdSgjelinek 		(void) fprintf(stderr, gettext("could not verify zfs datasets: "
2101fa9e4066Sahrens 		    "unable to enumerate datasets\n"));
2102fa9e4066Sahrens 		return (Z_ERR);
2103fa9e4066Sahrens 	}
2104fa9e4066Sahrens 
2105fa9e4066Sahrens 	zfs_set_error_handler(zfs_error_handler);
2106fa9e4066Sahrens 
2107fa9e4066Sahrens 	while (zonecfg_getdsent(handle, &dstab) == Z_OK) {
2108fa9e4066Sahrens 
2109fa9e4066Sahrens 		current_dataset = dstab.zone_dataset_name;
2110fa9e4066Sahrens 
2111fa9e4066Sahrens 		if ((zhp = zfs_open(dstab.zone_dataset_name,
2112fa9e4066Sahrens 		    ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME)) == NULL) {
2113fa9e4066Sahrens 			return_code = Z_ERR;
2114fa9e4066Sahrens 			continue;
2115fa9e4066Sahrens 		}
2116fa9e4066Sahrens 
2117fa9e4066Sahrens 		if (zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, propbuf,
2118fa9e4066Sahrens 		    sizeof (propbuf), &srctype, source,
2119fa9e4066Sahrens 		    sizeof (source), 0) == 0 &&
2120fa9e4066Sahrens 		    (srctype == ZFS_SRC_INHERITED)) {
2121*5ee84fbdSgjelinek 			(void) fprintf(stderr, gettext("could not verify zfs "
2122fa9e4066Sahrens 			    "dataset %s: mountpoint cannot be inherited\n"),
2123fa9e4066Sahrens 			    dstab.zone_dataset_name);
2124fa9e4066Sahrens 			return_code = Z_ERR;
2125fa9e4066Sahrens 			zfs_close(zhp);
2126fa9e4066Sahrens 			continue;
2127fa9e4066Sahrens 		}
2128fa9e4066Sahrens 
2129fa9e4066Sahrens 		if (zfs_get_type(zhp) == ZFS_TYPE_VOLUME) {
2130fa9e4066Sahrens 			(void) fprintf(stderr, gettext("cannot verify zfs "
2131fa9e4066Sahrens 			    "dataset %s: volumes cannot be specified as a "
2132fa9e4066Sahrens 			    "zone dataset resource\n"),
2133fa9e4066Sahrens 			    dstab.zone_dataset_name);
2134fa9e4066Sahrens 			return_code = Z_ERR;
2135fa9e4066Sahrens 		}
2136fa9e4066Sahrens 
2137fa9e4066Sahrens 		if (zfs_iter_children(zhp, check_zvol, NULL) != 0)
2138fa9e4066Sahrens 			return_code = Z_ERR;
2139fa9e4066Sahrens 
2140fa9e4066Sahrens 		zfs_close(zhp);
2141fa9e4066Sahrens 	}
2142fa9e4066Sahrens 	(void) zonecfg_enddsent(handle);
2143fa9e4066Sahrens 
2144fa9e4066Sahrens 	return (return_code);
2145fa9e4066Sahrens }
2146fa9e4066Sahrens 
21477c478bd9Sstevel@tonic-gate static int
21487c478bd9Sstevel@tonic-gate verify_details(int cmd_num)
21497c478bd9Sstevel@tonic-gate {
21507c478bd9Sstevel@tonic-gate 	zone_dochandle_t handle;
21517c478bd9Sstevel@tonic-gate 	struct zone_nwiftab nwiftab;
21527c478bd9Sstevel@tonic-gate 	char zonepath[MAXPATHLEN], checkpath[MAXPATHLEN];
21537c478bd9Sstevel@tonic-gate 	int return_code = Z_OK;
21547c478bd9Sstevel@tonic-gate 	int err;
2155108322fbScarlsonj 	boolean_t in_alt_root;
21567c478bd9Sstevel@tonic-gate 
21577c478bd9Sstevel@tonic-gate 	if ((handle = zonecfg_init_handle()) == NULL) {
21587c478bd9Sstevel@tonic-gate 		zperror(cmd_to_str(cmd_num), B_TRUE);
21597c478bd9Sstevel@tonic-gate 		return (Z_ERR);
21607c478bd9Sstevel@tonic-gate 	}
21617c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_get_handle(target_zone, handle)) != Z_OK) {
21627c478bd9Sstevel@tonic-gate 		errno = err;
21637c478bd9Sstevel@tonic-gate 		zperror(cmd_to_str(cmd_num), B_TRUE);
21647c478bd9Sstevel@tonic-gate 		zonecfg_fini_handle(handle);
21657c478bd9Sstevel@tonic-gate 		return (Z_ERR);
21667c478bd9Sstevel@tonic-gate 	}
21677c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_get_zonepath(handle, zonepath, sizeof (zonepath))) !=
21687c478bd9Sstevel@tonic-gate 	    Z_OK) {
21697c478bd9Sstevel@tonic-gate 		errno = err;
21707c478bd9Sstevel@tonic-gate 		zperror(cmd_to_str(cmd_num), B_TRUE);
21717c478bd9Sstevel@tonic-gate 		zonecfg_fini_handle(handle);
21727c478bd9Sstevel@tonic-gate 		return (Z_ERR);
21737c478bd9Sstevel@tonic-gate 	}
21747c478bd9Sstevel@tonic-gate 	/*
21757c478bd9Sstevel@tonic-gate 	 * zonecfg_get_zonepath() gets its data from the XML repository.
21767c478bd9Sstevel@tonic-gate 	 * Verify this against the index file, which is checked first by
21777c478bd9Sstevel@tonic-gate 	 * zone_get_zonepath().  If they don't match, bail out.
21787c478bd9Sstevel@tonic-gate 	 */
21797c478bd9Sstevel@tonic-gate 	if ((err = zone_get_zonepath(target_zone, checkpath,
21807c478bd9Sstevel@tonic-gate 	    sizeof (checkpath))) != Z_OK) {
21817c478bd9Sstevel@tonic-gate 		errno = err;
21827c478bd9Sstevel@tonic-gate 		zperror2(target_zone, gettext("could not get zone path"));
21837c478bd9Sstevel@tonic-gate 		return (Z_ERR);
21847c478bd9Sstevel@tonic-gate 	}
21857c478bd9Sstevel@tonic-gate 	if (strcmp(zonepath, checkpath) != 0) {
2186*5ee84fbdSgjelinek 		/*
2187*5ee84fbdSgjelinek 		 * TRANSLATION_NOTE
2188*5ee84fbdSgjelinek 		 * XML and zonepath are literals that should not be translated.
2189*5ee84fbdSgjelinek 		 */
21907c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("The XML repository has "
21917c478bd9Sstevel@tonic-gate 		    "zonepath '%s',\nbut the index file has zonepath '%s'.\n"
21927c478bd9Sstevel@tonic-gate 		    "These must match, so fix the incorrect entry.\n"),
21937c478bd9Sstevel@tonic-gate 		    zonepath, checkpath);
21947c478bd9Sstevel@tonic-gate 		return (Z_ERR);
21957c478bd9Sstevel@tonic-gate 	}
21967c478bd9Sstevel@tonic-gate 	if (validate_zonepath(zonepath, cmd_num) != Z_OK) {
21977c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("could not verify zonepath %s "
21987c478bd9Sstevel@tonic-gate 		    "because of the above errors.\n"), zonepath);
21997c478bd9Sstevel@tonic-gate 		return_code = Z_ERR;
22007c478bd9Sstevel@tonic-gate 	}
22017c478bd9Sstevel@tonic-gate 
2202108322fbScarlsonj 	in_alt_root = zonecfg_in_alt_root();
2203108322fbScarlsonj 	if (in_alt_root)
2204108322fbScarlsonj 		goto no_net;
2205108322fbScarlsonj 
22067c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_setnwifent(handle)) != Z_OK) {
22077c478bd9Sstevel@tonic-gate 		errno = err;
22087c478bd9Sstevel@tonic-gate 		zperror(cmd_to_str(cmd_num), B_TRUE);
22097c478bd9Sstevel@tonic-gate 		zonecfg_fini_handle(handle);
22107c478bd9Sstevel@tonic-gate 		return (Z_ERR);
22117c478bd9Sstevel@tonic-gate 	}
22127c478bd9Sstevel@tonic-gate 	while (zonecfg_getnwifent(handle, &nwiftab) == Z_OK) {
22137c478bd9Sstevel@tonic-gate 		struct lifreq lifr;
22147c478bd9Sstevel@tonic-gate 		sa_family_t af;
22157c478bd9Sstevel@tonic-gate 		int so, res;
22167c478bd9Sstevel@tonic-gate 
22177c478bd9Sstevel@tonic-gate 		/* skip any loopback interfaces */
22187c478bd9Sstevel@tonic-gate 		if (strcmp(nwiftab.zone_nwif_physical, "lo0") == 0)
22197c478bd9Sstevel@tonic-gate 			continue;
22207c478bd9Sstevel@tonic-gate 		if ((res = zonecfg_valid_net_address(nwiftab.zone_nwif_address,
22217c478bd9Sstevel@tonic-gate 		    &lifr)) != Z_OK) {
22227c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("could not verify %s "
22237c478bd9Sstevel@tonic-gate 			    "%s=%s %s=%s: %s\n"), "net", "address",
22247c478bd9Sstevel@tonic-gate 			    nwiftab.zone_nwif_address, "physical",
22257c478bd9Sstevel@tonic-gate 			    nwiftab.zone_nwif_physical, zonecfg_strerror(res));
22267c478bd9Sstevel@tonic-gate 			return_code = Z_ERR;
22277c478bd9Sstevel@tonic-gate 			continue;
22287c478bd9Sstevel@tonic-gate 		}
22297c478bd9Sstevel@tonic-gate 		af = lifr.lifr_addr.ss_family;
22307c478bd9Sstevel@tonic-gate 		(void) memset(&lifr, 0, sizeof (lifr));
22317c478bd9Sstevel@tonic-gate 		(void) strlcpy(lifr.lifr_name, nwiftab.zone_nwif_physical,
22327c478bd9Sstevel@tonic-gate 		    sizeof (lifr.lifr_name));
22337c478bd9Sstevel@tonic-gate 		lifr.lifr_addr.ss_family = af;
22347c478bd9Sstevel@tonic-gate 		if ((so = socket(af, SOCK_DGRAM, 0)) < 0) {
22357c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("could not verify %s "
22367c478bd9Sstevel@tonic-gate 			    "%s=%s %s=%s: could not get socket: %s\n"), "net",
22377c478bd9Sstevel@tonic-gate 			    "address", nwiftab.zone_nwif_address, "physical",
22387c478bd9Sstevel@tonic-gate 			    nwiftab.zone_nwif_physical, strerror(errno));
22397c478bd9Sstevel@tonic-gate 			return_code = Z_ERR;
22407c478bd9Sstevel@tonic-gate 			continue;
22417c478bd9Sstevel@tonic-gate 		}
22427c478bd9Sstevel@tonic-gate 		if (ioctl(so, SIOCGLIFFLAGS, (caddr_t)&lifr) < 0) {
22437c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
22447c478bd9Sstevel@tonic-gate 			    gettext("could not verify %s %s=%s %s=%s: %s\n"),
22457c478bd9Sstevel@tonic-gate 			    "net", "address", nwiftab.zone_nwif_address,
22467c478bd9Sstevel@tonic-gate 			    "physical", nwiftab.zone_nwif_physical,
22477c478bd9Sstevel@tonic-gate 			    strerror(errno));
22487c478bd9Sstevel@tonic-gate 			return_code = Z_ERR;
22497c478bd9Sstevel@tonic-gate 		}
22507c478bd9Sstevel@tonic-gate 		(void) close(so);
22517c478bd9Sstevel@tonic-gate 	}
22527c478bd9Sstevel@tonic-gate 	(void) zonecfg_endnwifent(handle);
2253108322fbScarlsonj no_net:
22547c478bd9Sstevel@tonic-gate 
22557c478bd9Sstevel@tonic-gate 	if (verify_filesystems(handle) != Z_OK)
22567c478bd9Sstevel@tonic-gate 		return_code = Z_ERR;
2257b5abaf04Sgjelinek 	if (verify_ipd(handle) != Z_OK)
2258b5abaf04Sgjelinek 		return_code = Z_ERR;
2259108322fbScarlsonj 	if (!in_alt_root && verify_rctls(handle) != Z_OK)
22607c478bd9Sstevel@tonic-gate 		return_code = Z_ERR;
2261108322fbScarlsonj 	if (!in_alt_root && verify_pool(handle) != Z_OK)
22627c478bd9Sstevel@tonic-gate 		return_code = Z_ERR;
2263fa9e4066Sahrens 	if (!in_alt_root && verify_datasets(handle) != Z_OK)
2264fa9e4066Sahrens 		return_code = Z_ERR;
22657c478bd9Sstevel@tonic-gate 	zonecfg_fini_handle(handle);
22667c478bd9Sstevel@tonic-gate 	if (return_code == Z_ERR)
22677c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
22687c478bd9Sstevel@tonic-gate 		    gettext("%s: zone %s failed to verify\n"),
22697c478bd9Sstevel@tonic-gate 		    execname, target_zone);
22707c478bd9Sstevel@tonic-gate 	return (return_code);
22717c478bd9Sstevel@tonic-gate }
22727c478bd9Sstevel@tonic-gate 
22737c478bd9Sstevel@tonic-gate static int
22747c478bd9Sstevel@tonic-gate verify_func(int argc, char *argv[])
22757c478bd9Sstevel@tonic-gate {
22767c478bd9Sstevel@tonic-gate 	int arg;
22777c478bd9Sstevel@tonic-gate 
22787c478bd9Sstevel@tonic-gate 	optind = 0;
22797c478bd9Sstevel@tonic-gate 	if ((arg = getopt(argc, argv, "?")) != EOF) {
22807c478bd9Sstevel@tonic-gate 		switch (arg) {
22817c478bd9Sstevel@tonic-gate 		case '?':
22827c478bd9Sstevel@tonic-gate 			sub_usage(SHELP_VERIFY, CMD_VERIFY);
22837c478bd9Sstevel@tonic-gate 			return (optopt == '?' ? Z_OK : Z_USAGE);
22847c478bd9Sstevel@tonic-gate 		default:
22857c478bd9Sstevel@tonic-gate 			sub_usage(SHELP_VERIFY, CMD_VERIFY);
22867c478bd9Sstevel@tonic-gate 			return (Z_USAGE);
22877c478bd9Sstevel@tonic-gate 		}
22887c478bd9Sstevel@tonic-gate 	}
22897c478bd9Sstevel@tonic-gate 	if (argc > optind) {
22907c478bd9Sstevel@tonic-gate 		sub_usage(SHELP_VERIFY, CMD_VERIFY);
22917c478bd9Sstevel@tonic-gate 		return (Z_USAGE);
22927c478bd9Sstevel@tonic-gate 	}
22937c478bd9Sstevel@tonic-gate 	if (sanity_check(target_zone, CMD_VERIFY, B_FALSE, B_FALSE) != Z_OK)
22947c478bd9Sstevel@tonic-gate 		return (Z_ERR);
22957c478bd9Sstevel@tonic-gate 	return (verify_details(CMD_VERIFY));
22967c478bd9Sstevel@tonic-gate }
22977c478bd9Sstevel@tonic-gate 
22987c478bd9Sstevel@tonic-gate #define	LUCREATEZONE	"/usr/lib/lu/lucreatezone"
22997c478bd9Sstevel@tonic-gate 
23007c478bd9Sstevel@tonic-gate static int
23017c478bd9Sstevel@tonic-gate install_func(int argc, char *argv[])
23027c478bd9Sstevel@tonic-gate {
23037c478bd9Sstevel@tonic-gate 	/* 9: "exec " and " -z " */
23047c478bd9Sstevel@tonic-gate 	char cmdbuf[sizeof (LUCREATEZONE) + ZONENAME_MAX + 9];
23057c478bd9Sstevel@tonic-gate 	int lockfd;
23067c478bd9Sstevel@tonic-gate 	int err, arg;
23077c478bd9Sstevel@tonic-gate 	char zonepath[MAXPATHLEN];
23087c478bd9Sstevel@tonic-gate 	int status;
23097c478bd9Sstevel@tonic-gate 
2310108322fbScarlsonj 	if (zonecfg_in_alt_root()) {
2311108322fbScarlsonj 		zerror(gettext("cannot install zone in alternate root"));
2312108322fbScarlsonj 		return (Z_ERR);
2313108322fbScarlsonj 	}
2314108322fbScarlsonj 
23157c478bd9Sstevel@tonic-gate 	optind = 0;
23167c478bd9Sstevel@tonic-gate 	if ((arg = getopt(argc, argv, "?")) != EOF) {
23177c478bd9Sstevel@tonic-gate 		switch (arg) {
23187c478bd9Sstevel@tonic-gate 		case '?':
23197c478bd9Sstevel@tonic-gate 			sub_usage(SHELP_INSTALL, CMD_INSTALL);
23207c478bd9Sstevel@tonic-gate 			return (optopt == '?' ? Z_OK : Z_USAGE);
23217c478bd9Sstevel@tonic-gate 		default:
23227c478bd9Sstevel@tonic-gate 			sub_usage(SHELP_INSTALL, CMD_INSTALL);
23237c478bd9Sstevel@tonic-gate 			return (Z_USAGE);
23247c478bd9Sstevel@tonic-gate 		}
23257c478bd9Sstevel@tonic-gate 	}
23267c478bd9Sstevel@tonic-gate 	if (argc > optind) {
23277c478bd9Sstevel@tonic-gate 		sub_usage(SHELP_INSTALL, CMD_INSTALL);
23287c478bd9Sstevel@tonic-gate 		return (Z_USAGE);
23297c478bd9Sstevel@tonic-gate 	}
23307c478bd9Sstevel@tonic-gate 	if (sanity_check(target_zone, CMD_INSTALL, B_FALSE, B_TRUE) != Z_OK)
23317c478bd9Sstevel@tonic-gate 		return (Z_ERR);
23327c478bd9Sstevel@tonic-gate 	if (verify_details(CMD_INSTALL) != Z_OK)
23337c478bd9Sstevel@tonic-gate 		return (Z_ERR);
23347c478bd9Sstevel@tonic-gate 
23357c478bd9Sstevel@tonic-gate 	if (grab_lock_file(target_zone, &lockfd) != Z_OK) {
23367c478bd9Sstevel@tonic-gate 		zerror(gettext("another %s may have an operation in progress."),
23377c478bd9Sstevel@tonic-gate 		    "zoneadmd");
23387c478bd9Sstevel@tonic-gate 		return (Z_ERR);
23397c478bd9Sstevel@tonic-gate 	}
23407c478bd9Sstevel@tonic-gate 	err = zone_set_state(target_zone, ZONE_STATE_INCOMPLETE);
23417c478bd9Sstevel@tonic-gate 	if (err != Z_OK) {
23427c478bd9Sstevel@tonic-gate 		errno = err;
23437c478bd9Sstevel@tonic-gate 		zperror2(target_zone, gettext("could not set state"));
23447c478bd9Sstevel@tonic-gate 		goto done;
23457c478bd9Sstevel@tonic-gate 	}
23467c478bd9Sstevel@tonic-gate 
23477c478bd9Sstevel@tonic-gate 	/*
23487c478bd9Sstevel@tonic-gate 	 * According to the Application Packaging Developer's Guide, a
23497c478bd9Sstevel@tonic-gate 	 * "checkinstall" script when included in a package is executed as
23507c478bd9Sstevel@tonic-gate 	 * the user "install", if such a user exists, or by the user
23517c478bd9Sstevel@tonic-gate 	 * "nobody".  In order to support this dubious behavior, the path
23527c478bd9Sstevel@tonic-gate 	 * to the zone being constructed is opened up during the life of
23537c478bd9Sstevel@tonic-gate 	 * the command laying down the zone's root file system.  Once this
23547c478bd9Sstevel@tonic-gate 	 * has completed, regardless of whether it was successful, the
23557c478bd9Sstevel@tonic-gate 	 * path to the zone is again restricted.
23567c478bd9Sstevel@tonic-gate 	 */
23577c478bd9Sstevel@tonic-gate 	if ((err = zone_get_zonepath(target_zone, zonepath,
23587c478bd9Sstevel@tonic-gate 	    sizeof (zonepath))) != Z_OK) {
23597c478bd9Sstevel@tonic-gate 		errno = err;
23607c478bd9Sstevel@tonic-gate 		zperror2(target_zone, gettext("could not get zone path"));
23617c478bd9Sstevel@tonic-gate 		goto done;
23627c478bd9Sstevel@tonic-gate 	}
23637c478bd9Sstevel@tonic-gate 	if (chmod(zonepath, DEFAULT_DIR_MODE) != 0) {
23647c478bd9Sstevel@tonic-gate 		zperror(zonepath, B_FALSE);
23657c478bd9Sstevel@tonic-gate 		err = Z_ERR;
23667c478bd9Sstevel@tonic-gate 		goto done;
23677c478bd9Sstevel@tonic-gate 	}
23687c478bd9Sstevel@tonic-gate 
23697c478bd9Sstevel@tonic-gate 	/*
23707c478bd9Sstevel@tonic-gate 	 * "exec" the command so that the returned status is that of
23717c478bd9Sstevel@tonic-gate 	 * LUCREATEZONE and not the shell.
23727c478bd9Sstevel@tonic-gate 	 */
23737c478bd9Sstevel@tonic-gate 	(void) snprintf(cmdbuf, sizeof (cmdbuf), "exec " LUCREATEZONE " -z %s",
23747c478bd9Sstevel@tonic-gate 	    target_zone);
23757c478bd9Sstevel@tonic-gate 	status = do_subproc(cmdbuf);
23767c478bd9Sstevel@tonic-gate 	if (chmod(zonepath, S_IRWXU) != 0) {
23777c478bd9Sstevel@tonic-gate 		zperror(zonepath, B_FALSE);
23787c478bd9Sstevel@tonic-gate 		err = Z_ERR;
23797c478bd9Sstevel@tonic-gate 		goto done;
23807c478bd9Sstevel@tonic-gate 	}
23817c478bd9Sstevel@tonic-gate 	if ((err = subproc_status(LUCREATEZONE, status)) != Z_OK)
23827c478bd9Sstevel@tonic-gate 		goto done;
23837c478bd9Sstevel@tonic-gate 
23847c478bd9Sstevel@tonic-gate 	if ((err = zone_set_state(target_zone, ZONE_STATE_INSTALLED)) != Z_OK) {
23857c478bd9Sstevel@tonic-gate 		errno = err;
23867c478bd9Sstevel@tonic-gate 		zperror2(target_zone, gettext("could not set state"));
23877c478bd9Sstevel@tonic-gate 		goto done;
23887c478bd9Sstevel@tonic-gate 	}
23897c478bd9Sstevel@tonic-gate 
23907c478bd9Sstevel@tonic-gate done:
23917c478bd9Sstevel@tonic-gate 	release_lock_file(lockfd);
23927c478bd9Sstevel@tonic-gate 	return ((err == Z_OK) ? Z_OK : Z_ERR);
23937c478bd9Sstevel@tonic-gate }
23947c478bd9Sstevel@tonic-gate 
23957c478bd9Sstevel@tonic-gate /*
23967c478bd9Sstevel@tonic-gate  * On input, TRUE => yes, FALSE => no.
23977c478bd9Sstevel@tonic-gate  * On return, TRUE => 1, FALSE => 0, could not ask => -1.
23987c478bd9Sstevel@tonic-gate  */
23997c478bd9Sstevel@tonic-gate 
24007c478bd9Sstevel@tonic-gate static int
24017c478bd9Sstevel@tonic-gate ask_yesno(boolean_t default_answer, const char *question)
24027c478bd9Sstevel@tonic-gate {
24037c478bd9Sstevel@tonic-gate 	char line[64];	/* should be large enough to answer yes or no */
24047c478bd9Sstevel@tonic-gate 
24057c478bd9Sstevel@tonic-gate 	if (!isatty(STDIN_FILENO))
24067c478bd9Sstevel@tonic-gate 		return (-1);
24077c478bd9Sstevel@tonic-gate 	for (;;) {
24087c478bd9Sstevel@tonic-gate 		(void) printf("%s (%s)? ", question,
24097c478bd9Sstevel@tonic-gate 		    default_answer ? "[y]/n" : "y/[n]");
24107c478bd9Sstevel@tonic-gate 		if (fgets(line, sizeof (line), stdin) == NULL ||
24117c478bd9Sstevel@tonic-gate 		    line[0] == '\n')
24127c478bd9Sstevel@tonic-gate 			return (default_answer ? 1 : 0);
24137c478bd9Sstevel@tonic-gate 		if (tolower(line[0]) == 'y')
24147c478bd9Sstevel@tonic-gate 			return (1);
24157c478bd9Sstevel@tonic-gate 		if (tolower(line[0]) == 'n')
24167c478bd9Sstevel@tonic-gate 			return (0);
24177c478bd9Sstevel@tonic-gate 	}
24187c478bd9Sstevel@tonic-gate }
24197c478bd9Sstevel@tonic-gate 
24207c478bd9Sstevel@tonic-gate #define	RMCOMMAND	"/usr/bin/rm -rf"
24217c478bd9Sstevel@tonic-gate 
24227c478bd9Sstevel@tonic-gate /* ARGSUSED */
24237c478bd9Sstevel@tonic-gate int
24247c478bd9Sstevel@tonic-gate zfm_print(const char *p, void *r) {
24257c478bd9Sstevel@tonic-gate 	zerror("  %s\n", p);
24267c478bd9Sstevel@tonic-gate 	return (0);
24277c478bd9Sstevel@tonic-gate }
24287c478bd9Sstevel@tonic-gate 
24297c478bd9Sstevel@tonic-gate static int
24307c478bd9Sstevel@tonic-gate uninstall_func(int argc, char *argv[])
24317c478bd9Sstevel@tonic-gate {
24327c478bd9Sstevel@tonic-gate 	/* 6: "exec " and " " */
24337c478bd9Sstevel@tonic-gate 	char cmdbuf[sizeof (RMCOMMAND) + MAXPATHLEN + 6];
24347c478bd9Sstevel@tonic-gate 	char line[ZONENAME_MAX + 128];	/* Enough for "Are you sure ..." */
24357c478bd9Sstevel@tonic-gate 	char rootpath[MAXPATHLEN], devpath[MAXPATHLEN];
24367c478bd9Sstevel@tonic-gate 	boolean_t force = B_FALSE;
24377c478bd9Sstevel@tonic-gate 	int lockfd, answer;
24387c478bd9Sstevel@tonic-gate 	int err, arg;
24397c478bd9Sstevel@tonic-gate 	int status;
24407c478bd9Sstevel@tonic-gate 
2441108322fbScarlsonj 	if (zonecfg_in_alt_root()) {
2442108322fbScarlsonj 		zerror(gettext("cannot uninstall zone in alternate root"));
2443108322fbScarlsonj 		return (Z_ERR);
2444108322fbScarlsonj 	}
2445108322fbScarlsonj 
24467c478bd9Sstevel@tonic-gate 	optind = 0;
24477c478bd9Sstevel@tonic-gate 	while ((arg = getopt(argc, argv, "?F")) != EOF) {
24487c478bd9Sstevel@tonic-gate 		switch (arg) {
24497c478bd9Sstevel@tonic-gate 		case '?':
24507c478bd9Sstevel@tonic-gate 			sub_usage(SHELP_UNINSTALL, CMD_UNINSTALL);
24517c478bd9Sstevel@tonic-gate 			return (optopt == '?' ? Z_OK : Z_USAGE);
24527c478bd9Sstevel@tonic-gate 		case 'F':
24537c478bd9Sstevel@tonic-gate 			force = B_TRUE;
24547c478bd9Sstevel@tonic-gate 			break;
24557c478bd9Sstevel@tonic-gate 		default:
24567c478bd9Sstevel@tonic-gate 			sub_usage(SHELP_UNINSTALL, CMD_UNINSTALL);
24577c478bd9Sstevel@tonic-gate 			return (Z_USAGE);
24587c478bd9Sstevel@tonic-gate 		}
24597c478bd9Sstevel@tonic-gate 	}
24607c478bd9Sstevel@tonic-gate 	if (argc > optind) {
24617c478bd9Sstevel@tonic-gate 		sub_usage(SHELP_UNINSTALL, CMD_UNINSTALL);
24627c478bd9Sstevel@tonic-gate 		return (Z_USAGE);
24637c478bd9Sstevel@tonic-gate 	}
24647c478bd9Sstevel@tonic-gate 
24657c478bd9Sstevel@tonic-gate 	if (sanity_check(target_zone, CMD_UNINSTALL, B_FALSE, B_TRUE) != Z_OK)
24667c478bd9Sstevel@tonic-gate 		return (Z_ERR);
24677c478bd9Sstevel@tonic-gate 
24687c478bd9Sstevel@tonic-gate 	if (!force) {
24697c478bd9Sstevel@tonic-gate 		(void) snprintf(line, sizeof (line),
24707c478bd9Sstevel@tonic-gate 		    gettext("Are you sure you want to %s zone %s"),
24717c478bd9Sstevel@tonic-gate 		    cmd_to_str(CMD_UNINSTALL), target_zone);
24727c478bd9Sstevel@tonic-gate 		if ((answer = ask_yesno(B_FALSE, line)) == 0) {
24737c478bd9Sstevel@tonic-gate 			return (Z_OK);
24747c478bd9Sstevel@tonic-gate 		} else if (answer == -1) {
24757c478bd9Sstevel@tonic-gate 			zerror(gettext("Input not from terminal and -F "
24767c478bd9Sstevel@tonic-gate 			    "not specified: %s not done."),
24777c478bd9Sstevel@tonic-gate 			    cmd_to_str(CMD_UNINSTALL));
24787c478bd9Sstevel@tonic-gate 			return (Z_ERR);
24797c478bd9Sstevel@tonic-gate 		}
24807c478bd9Sstevel@tonic-gate 	}
24817c478bd9Sstevel@tonic-gate 
24827c478bd9Sstevel@tonic-gate 	if ((err = zone_get_zonepath(target_zone, devpath,
24837c478bd9Sstevel@tonic-gate 	    sizeof (devpath))) != Z_OK) {
24847c478bd9Sstevel@tonic-gate 		errno = err;
24857c478bd9Sstevel@tonic-gate 		zperror2(target_zone, gettext("could not get zone path"));
24867c478bd9Sstevel@tonic-gate 		return (Z_ERR);
24877c478bd9Sstevel@tonic-gate 	}
24887c478bd9Sstevel@tonic-gate 	(void) strlcat(devpath, "/dev", sizeof (devpath));
24897c478bd9Sstevel@tonic-gate 	if ((err = zone_get_rootpath(target_zone, rootpath,
24907c478bd9Sstevel@tonic-gate 	    sizeof (rootpath))) != Z_OK) {
24917c478bd9Sstevel@tonic-gate 		errno = err;
24927c478bd9Sstevel@tonic-gate 		zperror2(target_zone, gettext("could not get root path"));
24937c478bd9Sstevel@tonic-gate 		return (Z_ERR);
24947c478bd9Sstevel@tonic-gate 	}
24957c478bd9Sstevel@tonic-gate 
24967c478bd9Sstevel@tonic-gate 	/*
24977c478bd9Sstevel@tonic-gate 	 * If there seems to be a zoneadmd running for this zone, call it
24987c478bd9Sstevel@tonic-gate 	 * to tell it that an uninstall is happening; if all goes well it
24997c478bd9Sstevel@tonic-gate 	 * will then shut itself down.
25007c478bd9Sstevel@tonic-gate 	 */
25017c478bd9Sstevel@tonic-gate 	if (ping_zoneadmd(target_zone) == Z_OK) {
25027c478bd9Sstevel@tonic-gate 		zone_cmd_arg_t zarg;
25037c478bd9Sstevel@tonic-gate 		zarg.cmd = Z_NOTE_UNINSTALLING;
25047c478bd9Sstevel@tonic-gate 		/* we don't care too much if this fails... just plow on */
25057c478bd9Sstevel@tonic-gate 		(void) call_zoneadmd(target_zone, &zarg);
25067c478bd9Sstevel@tonic-gate 	}
25077c478bd9Sstevel@tonic-gate 
25087c478bd9Sstevel@tonic-gate 	if (grab_lock_file(target_zone, &lockfd) != Z_OK) {
25097c478bd9Sstevel@tonic-gate 		zerror(gettext("another %s may have an operation in progress."),
25107c478bd9Sstevel@tonic-gate 		    "zoneadmd");
25117c478bd9Sstevel@tonic-gate 		return (Z_ERR);
25127c478bd9Sstevel@tonic-gate 	}
25137c478bd9Sstevel@tonic-gate 
25147c478bd9Sstevel@tonic-gate 	/* Don't uninstall the zone if anything is mounted there */
25157c478bd9Sstevel@tonic-gate 	err = zonecfg_find_mounts(rootpath, NULL, NULL);
25167c478bd9Sstevel@tonic-gate 	if (err) {
25177c478bd9Sstevel@tonic-gate 		zerror(gettext("These file-systems are mounted on "
25187c478bd9Sstevel@tonic-gate 			"subdirectories of %s.\n"), rootpath);
25197c478bd9Sstevel@tonic-gate 		(void) zonecfg_find_mounts(rootpath, zfm_print, NULL);
25207c478bd9Sstevel@tonic-gate 		return (Z_ERR);
25217c478bd9Sstevel@tonic-gate 	}
25227c478bd9Sstevel@tonic-gate 
25237c478bd9Sstevel@tonic-gate 	err = zone_set_state(target_zone, ZONE_STATE_INCOMPLETE);
25247c478bd9Sstevel@tonic-gate 	if (err != Z_OK) {
25257c478bd9Sstevel@tonic-gate 		errno = err;
25267c478bd9Sstevel@tonic-gate 		zperror2(target_zone, gettext("could not set state"));
25277c478bd9Sstevel@tonic-gate 		goto bad;
25287c478bd9Sstevel@tonic-gate 	}
25297c478bd9Sstevel@tonic-gate 
25307c478bd9Sstevel@tonic-gate 	/*
25317c478bd9Sstevel@tonic-gate 	 * "exec" the command so that the returned status is that of
25327c478bd9Sstevel@tonic-gate 	 * RMCOMMAND and not the shell.
25337c478bd9Sstevel@tonic-gate 	 */
25347c478bd9Sstevel@tonic-gate 	(void) snprintf(cmdbuf, sizeof (cmdbuf), "exec " RMCOMMAND " %s",
25357c478bd9Sstevel@tonic-gate 	    devpath);
25367c478bd9Sstevel@tonic-gate 	status = do_subproc(cmdbuf);
25377c478bd9Sstevel@tonic-gate 	if ((err = subproc_status(RMCOMMAND, status)) != Z_OK)
25387c478bd9Sstevel@tonic-gate 		goto bad;
25397c478bd9Sstevel@tonic-gate 	(void) snprintf(cmdbuf, sizeof (cmdbuf), "exec " RMCOMMAND " %s",
25407c478bd9Sstevel@tonic-gate 	    rootpath);
25417c478bd9Sstevel@tonic-gate 	status = do_subproc(cmdbuf);
25427c478bd9Sstevel@tonic-gate 	if ((err = subproc_status(RMCOMMAND, status)) != Z_OK)
25437c478bd9Sstevel@tonic-gate 		goto bad;
25447c478bd9Sstevel@tonic-gate 	err = zone_set_state(target_zone, ZONE_STATE_CONFIGURED);
25457c478bd9Sstevel@tonic-gate 	if (err != Z_OK) {
25467c478bd9Sstevel@tonic-gate 		errno = err;
25477c478bd9Sstevel@tonic-gate 		zperror2(target_zone, gettext("could not reset state"));
25487c478bd9Sstevel@tonic-gate 	}
25497c478bd9Sstevel@tonic-gate bad:
25507c478bd9Sstevel@tonic-gate 	release_lock_file(lockfd);
25517c478bd9Sstevel@tonic-gate 	return (err);
25527c478bd9Sstevel@tonic-gate }
25537c478bd9Sstevel@tonic-gate 
2554108322fbScarlsonj /* ARGSUSED */
2555108322fbScarlsonj static int
2556108322fbScarlsonj mount_func(int argc, char *argv[])
2557108322fbScarlsonj {
2558108322fbScarlsonj 	zone_cmd_arg_t zarg;
2559108322fbScarlsonj 
2560108322fbScarlsonj 	if (argc > 0)
2561108322fbScarlsonj 		return (Z_USAGE);
2562108322fbScarlsonj 	if (sanity_check(target_zone, CMD_MOUNT, B_FALSE, B_FALSE) != Z_OK)
2563108322fbScarlsonj 		return (Z_ERR);
2564108322fbScarlsonj 	if (verify_details(CMD_MOUNT) != Z_OK)
2565108322fbScarlsonj 		return (Z_ERR);
2566108322fbScarlsonj 
2567108322fbScarlsonj 	zarg.cmd = Z_MOUNT;
2568108322fbScarlsonj 	if (call_zoneadmd(target_zone, &zarg) != 0) {
2569108322fbScarlsonj 		zerror(gettext("call to %s failed"), "zoneadmd");
2570108322fbScarlsonj 		return (Z_ERR);
2571108322fbScarlsonj 	}
2572108322fbScarlsonj 	return (Z_OK);
2573108322fbScarlsonj }
2574108322fbScarlsonj 
2575108322fbScarlsonj /* ARGSUSED */
2576108322fbScarlsonj static int
2577108322fbScarlsonj unmount_func(int argc, char *argv[])
2578108322fbScarlsonj {
2579108322fbScarlsonj 	zone_cmd_arg_t zarg;
2580108322fbScarlsonj 
2581108322fbScarlsonj 	if (argc > 0)
2582108322fbScarlsonj 		return (Z_USAGE);
2583108322fbScarlsonj 	if (sanity_check(target_zone, CMD_UNMOUNT, B_FALSE, B_FALSE) != Z_OK)
2584108322fbScarlsonj 		return (Z_ERR);
2585108322fbScarlsonj 
2586108322fbScarlsonj 	zarg.cmd = Z_UNMOUNT;
2587108322fbScarlsonj 	if (call_zoneadmd(target_zone, &zarg) != 0) {
2588108322fbScarlsonj 		zerror(gettext("call to %s failed"), "zoneadmd");
2589108322fbScarlsonj 		return (Z_ERR);
2590108322fbScarlsonj 	}
2591108322fbScarlsonj 	return (Z_OK);
2592108322fbScarlsonj }
2593108322fbScarlsonj 
25947c478bd9Sstevel@tonic-gate static int
25957c478bd9Sstevel@tonic-gate help_func(int argc, char *argv[])
25967c478bd9Sstevel@tonic-gate {
25977c478bd9Sstevel@tonic-gate 	int arg, cmd_num;
25987c478bd9Sstevel@tonic-gate 
25997c478bd9Sstevel@tonic-gate 	if (argc == 0) {
26007c478bd9Sstevel@tonic-gate 		(void) usage(B_TRUE);
26017c478bd9Sstevel@tonic-gate 		return (Z_OK);
26027c478bd9Sstevel@tonic-gate 	}
26037c478bd9Sstevel@tonic-gate 	optind = 0;
26047c478bd9Sstevel@tonic-gate 	if ((arg = getopt(argc, argv, "?")) != EOF) {
26057c478bd9Sstevel@tonic-gate 		switch (arg) {
26067c478bd9Sstevel@tonic-gate 		case '?':
26077c478bd9Sstevel@tonic-gate 			sub_usage(SHELP_HELP, CMD_HELP);
26087c478bd9Sstevel@tonic-gate 			return (optopt == '?' ? Z_OK : Z_USAGE);
26097c478bd9Sstevel@tonic-gate 		default:
26107c478bd9Sstevel@tonic-gate 			sub_usage(SHELP_HELP, CMD_HELP);
26117c478bd9Sstevel@tonic-gate 			return (Z_USAGE);
26127c478bd9Sstevel@tonic-gate 		}
26137c478bd9Sstevel@tonic-gate 	}
26147c478bd9Sstevel@tonic-gate 	while (optind < argc) {
26157c478bd9Sstevel@tonic-gate 		if ((cmd_num = cmd_match(argv[optind])) < 0) {
26167c478bd9Sstevel@tonic-gate 			sub_usage(SHELP_HELP, CMD_HELP);
26177c478bd9Sstevel@tonic-gate 			return (Z_USAGE);
26187c478bd9Sstevel@tonic-gate 		}
26197c478bd9Sstevel@tonic-gate 		sub_usage(cmdtab[cmd_num].short_usage, cmd_num);
26207c478bd9Sstevel@tonic-gate 		optind++;
26217c478bd9Sstevel@tonic-gate 	}
26227c478bd9Sstevel@tonic-gate 	return (Z_OK);
26237c478bd9Sstevel@tonic-gate }
26247c478bd9Sstevel@tonic-gate 
26257c478bd9Sstevel@tonic-gate /*
26267c478bd9Sstevel@tonic-gate  * Returns: CMD_MIN thru CMD_MAX on success, -1 on error
26277c478bd9Sstevel@tonic-gate  */
26287c478bd9Sstevel@tonic-gate 
26297c478bd9Sstevel@tonic-gate static int
26307c478bd9Sstevel@tonic-gate cmd_match(char *cmd)
26317c478bd9Sstevel@tonic-gate {
26327c478bd9Sstevel@tonic-gate 	int i;
26337c478bd9Sstevel@tonic-gate 
26347c478bd9Sstevel@tonic-gate 	for (i = CMD_MIN; i <= CMD_MAX; i++) {
26357c478bd9Sstevel@tonic-gate 		/* return only if there is an exact match */
26367c478bd9Sstevel@tonic-gate 		if (strcmp(cmd, cmdtab[i].cmd_name) == 0)
26377c478bd9Sstevel@tonic-gate 			return (cmdtab[i].cmd_num);
26387c478bd9Sstevel@tonic-gate 	}
26397c478bd9Sstevel@tonic-gate 	return (-1);
26407c478bd9Sstevel@tonic-gate }
26417c478bd9Sstevel@tonic-gate 
26427c478bd9Sstevel@tonic-gate static int
26437c478bd9Sstevel@tonic-gate parse_and_run(int argc, char *argv[])
26447c478bd9Sstevel@tonic-gate {
26457c478bd9Sstevel@tonic-gate 	int i = cmd_match(argv[0]);
26467c478bd9Sstevel@tonic-gate 
26477c478bd9Sstevel@tonic-gate 	if (i < 0)
26487c478bd9Sstevel@tonic-gate 		return (usage(B_FALSE));
26497c478bd9Sstevel@tonic-gate 	return (cmdtab[i].handler(argc - 1, &(argv[1])));
26507c478bd9Sstevel@tonic-gate }
26517c478bd9Sstevel@tonic-gate 
26527c478bd9Sstevel@tonic-gate static char *
26537c478bd9Sstevel@tonic-gate get_execbasename(char *execfullname)
26547c478bd9Sstevel@tonic-gate {
26557c478bd9Sstevel@tonic-gate 	char *last_slash, *execbasename;
26567c478bd9Sstevel@tonic-gate 
26577c478bd9Sstevel@tonic-gate 	/* guard against '/' at end of command invocation */
26587c478bd9Sstevel@tonic-gate 	for (;;) {
26597c478bd9Sstevel@tonic-gate 		last_slash = strrchr(execfullname, '/');
26607c478bd9Sstevel@tonic-gate 		if (last_slash == NULL) {
26617c478bd9Sstevel@tonic-gate 			execbasename = execfullname;
26627c478bd9Sstevel@tonic-gate 			break;
26637c478bd9Sstevel@tonic-gate 		} else {
26647c478bd9Sstevel@tonic-gate 			execbasename = last_slash + 1;
26657c478bd9Sstevel@tonic-gate 			if (*execbasename == '\0') {
26667c478bd9Sstevel@tonic-gate 				*last_slash = '\0';
26677c478bd9Sstevel@tonic-gate 				continue;
26687c478bd9Sstevel@tonic-gate 			}
26697c478bd9Sstevel@tonic-gate 			break;
26707c478bd9Sstevel@tonic-gate 		}
26717c478bd9Sstevel@tonic-gate 	}
26727c478bd9Sstevel@tonic-gate 	return (execbasename);
26737c478bd9Sstevel@tonic-gate }
26747c478bd9Sstevel@tonic-gate 
26757c478bd9Sstevel@tonic-gate int
26767c478bd9Sstevel@tonic-gate main(int argc, char **argv)
26777c478bd9Sstevel@tonic-gate {
26787c478bd9Sstevel@tonic-gate 	int arg;
26797c478bd9Sstevel@tonic-gate 	zoneid_t zid;
2680108322fbScarlsonj 	struct stat st;
26817c478bd9Sstevel@tonic-gate 
26827c478bd9Sstevel@tonic-gate 	if ((locale = setlocale(LC_ALL, "")) == NULL)
26837c478bd9Sstevel@tonic-gate 		locale = "C";
26847c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
26857c478bd9Sstevel@tonic-gate 	setbuf(stdout, NULL);
26867c478bd9Sstevel@tonic-gate 	(void) sigset(SIGHUP, SIG_IGN);
26877c478bd9Sstevel@tonic-gate 	execname = get_execbasename(argv[0]);
26887c478bd9Sstevel@tonic-gate 	target_zone = NULL;
26897c478bd9Sstevel@tonic-gate 	if (chdir("/") != 0) {
26907c478bd9Sstevel@tonic-gate 		zerror(gettext("could not change directory to /."));
26917c478bd9Sstevel@tonic-gate 		exit(Z_ERR);
26927c478bd9Sstevel@tonic-gate 	}
26937c478bd9Sstevel@tonic-gate 
2694108322fbScarlsonj 	while ((arg = getopt(argc, argv, "?z:R:")) != EOF) {
26957c478bd9Sstevel@tonic-gate 		switch (arg) {
26967c478bd9Sstevel@tonic-gate 		case '?':
26977c478bd9Sstevel@tonic-gate 			return (usage(B_TRUE));
26987c478bd9Sstevel@tonic-gate 		case 'z':
26997c478bd9Sstevel@tonic-gate 			target_zone = optarg;
27007c478bd9Sstevel@tonic-gate 			break;
2701108322fbScarlsonj 		case 'R':	/* private option for admin/install use */
2702108322fbScarlsonj 			if (*optarg != '/') {
2703108322fbScarlsonj 				zerror(gettext("root path must be absolute."));
2704108322fbScarlsonj 				exit(Z_ERR);
2705108322fbScarlsonj 			}
2706108322fbScarlsonj 			if (stat(optarg, &st) == -1 || !S_ISDIR(st.st_mode)) {
2707108322fbScarlsonj 				zerror(
2708108322fbScarlsonj 				    gettext("root path must be a directory."));
2709108322fbScarlsonj 				exit(Z_ERR);
2710108322fbScarlsonj 			}
2711108322fbScarlsonj 			zonecfg_set_root(optarg);
2712108322fbScarlsonj 			break;
27137c478bd9Sstevel@tonic-gate 		default:
27147c478bd9Sstevel@tonic-gate 			return (usage(B_FALSE));
27157c478bd9Sstevel@tonic-gate 		}
27167c478bd9Sstevel@tonic-gate 	}
27177c478bd9Sstevel@tonic-gate 
27187c478bd9Sstevel@tonic-gate 	if (optind >= argc)
27197c478bd9Sstevel@tonic-gate 		return (usage(B_FALSE));
27207c478bd9Sstevel@tonic-gate 	if (target_zone != NULL && zone_get_id(target_zone, &zid) != 0) {
27217c478bd9Sstevel@tonic-gate 		errno = Z_NO_ZONE;
27227c478bd9Sstevel@tonic-gate 		zperror(target_zone, B_TRUE);
27237c478bd9Sstevel@tonic-gate 		exit(Z_ERR);
27247c478bd9Sstevel@tonic-gate 	}
27257c478bd9Sstevel@tonic-gate 	return (parse_and_run(argc - optind, &argv[optind]));
27267c478bd9Sstevel@tonic-gate }
2727