xref: /onnv-gate/usr/src/cmd/zoneadm/zoneadm.c (revision 924:0366bf445df6)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
50Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
70Sstevel@tonic-gate  * with the License.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate  * See the License for the specific language governing permissions
120Sstevel@tonic-gate  * and limitations under the License.
130Sstevel@tonic-gate  *
140Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate  *
200Sstevel@tonic-gate  * CDDL HEADER END
210Sstevel@tonic-gate  */
22222Scomay 
230Sstevel@tonic-gate /*
240Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
250Sstevel@tonic-gate  * Use is subject to license terms.
260Sstevel@tonic-gate  */
270Sstevel@tonic-gate 
280Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
290Sstevel@tonic-gate 
300Sstevel@tonic-gate /*
310Sstevel@tonic-gate  * zoneadm is a command interpreter for zone administration.  It is all in
320Sstevel@tonic-gate  * C (i.e., no lex/yacc), and all the argument passing is argc/argv based.
330Sstevel@tonic-gate  * main() calls parse_and_run() which calls cmd_match(), then invokes the
340Sstevel@tonic-gate  * appropriate command's handler function.  The rest of the program is the
350Sstevel@tonic-gate  * handler functions and their helper functions.
360Sstevel@tonic-gate  *
370Sstevel@tonic-gate  * Some of the helper functions are used largely to simplify I18N: reducing
380Sstevel@tonic-gate  * the need for translation notes.  This is particularly true of many of
390Sstevel@tonic-gate  * the zerror() calls: doing e.g. zerror(gettext("%s failed"), "foo") rather
400Sstevel@tonic-gate  * than zerror(gettext("foo failed")) with a translation note indicating
410Sstevel@tonic-gate  * that "foo" need not be translated.
420Sstevel@tonic-gate  */
430Sstevel@tonic-gate 
440Sstevel@tonic-gate #include <stdio.h>
450Sstevel@tonic-gate #include <errno.h>
460Sstevel@tonic-gate #include <unistd.h>
470Sstevel@tonic-gate #include <signal.h>
480Sstevel@tonic-gate #include <stdarg.h>
490Sstevel@tonic-gate #include <ctype.h>
500Sstevel@tonic-gate #include <stdlib.h>
510Sstevel@tonic-gate #include <string.h>
520Sstevel@tonic-gate #include <wait.h>
530Sstevel@tonic-gate #include <zone.h>
540Sstevel@tonic-gate #include <priv.h>
550Sstevel@tonic-gate #include <locale.h>
560Sstevel@tonic-gate #include <libintl.h>
570Sstevel@tonic-gate #include <libzonecfg.h>
580Sstevel@tonic-gate #include <bsm/adt.h>
590Sstevel@tonic-gate #include <sys/utsname.h>
600Sstevel@tonic-gate #include <sys/param.h>
610Sstevel@tonic-gate #include <sys/types.h>
620Sstevel@tonic-gate #include <sys/stat.h>
630Sstevel@tonic-gate #include <sys/statvfs.h>
640Sstevel@tonic-gate #include <assert.h>
650Sstevel@tonic-gate #include <sys/sockio.h>
660Sstevel@tonic-gate #include <sys/mntent.h>
670Sstevel@tonic-gate #include <limits.h>
68789Sahrens #include <libzfs.h>
690Sstevel@tonic-gate 
700Sstevel@tonic-gate #include <fcntl.h>
710Sstevel@tonic-gate #include <door.h>
720Sstevel@tonic-gate #include <macros.h>
730Sstevel@tonic-gate #include <libgen.h>
740Sstevel@tonic-gate 
750Sstevel@tonic-gate #include <pool.h>
760Sstevel@tonic-gate #include <sys/pool.h>
770Sstevel@tonic-gate 
780Sstevel@tonic-gate #define	MAXARGS	8
790Sstevel@tonic-gate 
800Sstevel@tonic-gate /* Reflects kernel zone entries */
810Sstevel@tonic-gate typedef struct zone_entry {
820Sstevel@tonic-gate 	zoneid_t	zid;
830Sstevel@tonic-gate 	char		zname[ZONENAME_MAX];
840Sstevel@tonic-gate 	char		*zstate_str;
850Sstevel@tonic-gate 	zone_state_t	zstate_num;
860Sstevel@tonic-gate 	char		zroot[MAXPATHLEN];
870Sstevel@tonic-gate } zone_entry_t;
880Sstevel@tonic-gate 
890Sstevel@tonic-gate static zone_entry_t *zents;
900Sstevel@tonic-gate static size_t nzents;
910Sstevel@tonic-gate 
920Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)		/* should be defined by cc -D */
930Sstevel@tonic-gate #define	TEXT_DOMAIN	"SYS_TEST"	/* Use this only if it wasn't */
940Sstevel@tonic-gate #endif
950Sstevel@tonic-gate 
960Sstevel@tonic-gate #define	Z_ERR	1
970Sstevel@tonic-gate #define	Z_USAGE	2
980Sstevel@tonic-gate 
990Sstevel@tonic-gate /* 0755 is the default directory mode. */
1000Sstevel@tonic-gate #define	DEFAULT_DIR_MODE \
1010Sstevel@tonic-gate 	(S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH)
1020Sstevel@tonic-gate 
1030Sstevel@tonic-gate #define	CMD_HELP	0
1040Sstevel@tonic-gate #define	CMD_BOOT	1
1050Sstevel@tonic-gate #define	CMD_HALT	2
1060Sstevel@tonic-gate #define	CMD_READY	3
1070Sstevel@tonic-gate #define	CMD_REBOOT	4
1080Sstevel@tonic-gate #define	CMD_LIST	5
1090Sstevel@tonic-gate #define	CMD_VERIFY	6
1100Sstevel@tonic-gate #define	CMD_INSTALL	7
1110Sstevel@tonic-gate #define	CMD_UNINSTALL	8
112766Scarlsonj #define	CMD_MOUNT	9
113766Scarlsonj #define	CMD_UNMOUNT	10
1140Sstevel@tonic-gate 
1150Sstevel@tonic-gate #define	CMD_MIN		CMD_HELP
116766Scarlsonj #define	CMD_MAX		CMD_UNMOUNT
1170Sstevel@tonic-gate 
1180Sstevel@tonic-gate struct cmd {
1190Sstevel@tonic-gate 	uint_t	cmd_num;				/* command number */
1200Sstevel@tonic-gate 	char	*cmd_name;				/* command name */
1210Sstevel@tonic-gate 	char	*short_usage;				/* short form help */
1220Sstevel@tonic-gate 	int	(*handler)(int argc, char *argv[]);	/* function to call */
1230Sstevel@tonic-gate 
1240Sstevel@tonic-gate };
1250Sstevel@tonic-gate 
1260Sstevel@tonic-gate #define	SHELP_HELP	"help"
1270Sstevel@tonic-gate #define	SHELP_BOOT	"boot [-s]"
1280Sstevel@tonic-gate #define	SHELP_HALT	"halt"
1290Sstevel@tonic-gate #define	SHELP_READY	"ready"
1300Sstevel@tonic-gate #define	SHELP_REBOOT	"reboot"
1310Sstevel@tonic-gate #define	SHELP_LIST	"list [-cipv]"
1320Sstevel@tonic-gate #define	SHELP_VERIFY	"verify"
1330Sstevel@tonic-gate #define	SHELP_INSTALL	"install"
1340Sstevel@tonic-gate #define	SHELP_UNINSTALL	"uninstall [-F]"
1350Sstevel@tonic-gate 
1360Sstevel@tonic-gate static int help_func(int argc, char *argv[]);
1370Sstevel@tonic-gate static int ready_func(int argc, char *argv[]);
1380Sstevel@tonic-gate static int boot_func(int argc, char *argv[]);
1390Sstevel@tonic-gate static int halt_func(int argc, char *argv[]);
1400Sstevel@tonic-gate static int reboot_func(int argc, char *argv[]);
1410Sstevel@tonic-gate static int list_func(int argc, char *argv[]);
1420Sstevel@tonic-gate static int verify_func(int argc, char *argv[]);
1430Sstevel@tonic-gate static int install_func(int argc, char *argv[]);
1440Sstevel@tonic-gate static int uninstall_func(int argc, char *argv[]);
145766Scarlsonj static int mount_func(int argc, char *argv[]);
146766Scarlsonj static int unmount_func(int argc, char *argv[]);
1470Sstevel@tonic-gate static int sanity_check(char *zone, int cmd_num, boolean_t running,
1480Sstevel@tonic-gate     boolean_t unsafe_when_running);
1490Sstevel@tonic-gate static int cmd_match(char *cmd);
1500Sstevel@tonic-gate static int verify_details(int);
1510Sstevel@tonic-gate 
1520Sstevel@tonic-gate static struct cmd cmdtab[] = {
1530Sstevel@tonic-gate 	{ CMD_HELP,		"help",		SHELP_HELP,	help_func },
1540Sstevel@tonic-gate 	{ CMD_BOOT,		"boot",		SHELP_BOOT,	boot_func },
1550Sstevel@tonic-gate 	{ CMD_HALT,		"halt",		SHELP_HALT,	halt_func },
1560Sstevel@tonic-gate 	{ CMD_READY,		"ready",	SHELP_READY,	ready_func },
1570Sstevel@tonic-gate 	{ CMD_REBOOT,		"reboot",	SHELP_REBOOT,	reboot_func },
1580Sstevel@tonic-gate 	{ CMD_LIST,		"list",		SHELP_LIST,	list_func },
1590Sstevel@tonic-gate 	{ CMD_VERIFY,		"verify",	SHELP_VERIFY,	verify_func },
1600Sstevel@tonic-gate 	{ CMD_INSTALL,		"install",	SHELP_INSTALL,	install_func },
1610Sstevel@tonic-gate 	{ CMD_UNINSTALL,	"uninstall",	SHELP_UNINSTALL,
162766Scarlsonj 	    uninstall_func },
163766Scarlsonj 	/* Private commands for admin/install */
164766Scarlsonj 	{ CMD_MOUNT,		"mount",	NULL,		mount_func },
165766Scarlsonj 	{ CMD_UNMOUNT,		"unmount",	NULL,		unmount_func }
1660Sstevel@tonic-gate };
1670Sstevel@tonic-gate 
1680Sstevel@tonic-gate /* global variables */
1690Sstevel@tonic-gate 
1700Sstevel@tonic-gate /* set early in main(), never modified thereafter, used all over the place */
1710Sstevel@tonic-gate static char *execname;
1720Sstevel@tonic-gate static char *target_zone;
1730Sstevel@tonic-gate static char *locale;
1740Sstevel@tonic-gate 
1750Sstevel@tonic-gate /* used in do_subproc() and signal handler */
1760Sstevel@tonic-gate static volatile boolean_t child_killed;
1770Sstevel@tonic-gate 
1780Sstevel@tonic-gate static char *
1790Sstevel@tonic-gate cmd_to_str(int cmd_num)
1800Sstevel@tonic-gate {
1810Sstevel@tonic-gate 	assert(cmd_num >= CMD_MIN && cmd_num <= CMD_MAX);
1820Sstevel@tonic-gate 	return (cmdtab[cmd_num].cmd_name);
1830Sstevel@tonic-gate }
1840Sstevel@tonic-gate 
1850Sstevel@tonic-gate /* This is a separate function because of gettext() wrapping. */
1860Sstevel@tonic-gate static char *
1870Sstevel@tonic-gate long_help(int cmd_num)
1880Sstevel@tonic-gate {
189222Scomay 	assert(cmd_num >= CMD_MIN && cmd_num <= CMD_MAX);
1900Sstevel@tonic-gate 	switch (cmd_num) {
1910Sstevel@tonic-gate 		case CMD_HELP:
1920Sstevel@tonic-gate 			return (gettext("Print usage message."));
1930Sstevel@tonic-gate 		case CMD_BOOT:
1940Sstevel@tonic-gate 			return (gettext("Activates (boots) specified zone.  "
1950Sstevel@tonic-gate 			    "The -s flag can be used\n\tto boot the zone in "
1960Sstevel@tonic-gate 			    "the single-user state."));
1970Sstevel@tonic-gate 		case CMD_HALT:
1980Sstevel@tonic-gate 			return (gettext("Halts specified zone, bypassing "
1990Sstevel@tonic-gate 			    "shutdown scripts and removing runtime\n\t"
2000Sstevel@tonic-gate 			    "resources of the zone."));
2010Sstevel@tonic-gate 		case CMD_READY:
2020Sstevel@tonic-gate 			return (gettext("Prepares a zone for running "
2030Sstevel@tonic-gate 			    "applications but does not start any user\n\t"
2040Sstevel@tonic-gate 			    "processes in the zone."));
2050Sstevel@tonic-gate 		case CMD_REBOOT:
2060Sstevel@tonic-gate 			return (gettext("Restarts the zone (equivalent to a "
2070Sstevel@tonic-gate 			    "halt / boot sequence).\n\tFails if the zone is "
2080Sstevel@tonic-gate 			    "not active."));
2090Sstevel@tonic-gate 		case CMD_LIST:
2100Sstevel@tonic-gate 			return (gettext("Lists the current zones, or a "
2110Sstevel@tonic-gate 			    "specific zone if indicated.  By default,\n\tall "
2120Sstevel@tonic-gate 			    "running zones are listed, though this can be "
2130Sstevel@tonic-gate 			    "expanded to all\n\tinstalled zones with the -i "
2140Sstevel@tonic-gate 			    "option or all configured zones with the\n\t-c "
2150Sstevel@tonic-gate 			    "option.  When used with the general -z <zone> "
2160Sstevel@tonic-gate 			    "option, lists only the\n\tspecified zone, but "
2170Sstevel@tonic-gate 			    "lists it regardless of its state, and the -i "
2180Sstevel@tonic-gate 			    "and -c\n\toptions are disallowed.  The -v option "
2190Sstevel@tonic-gate 			    "can be used to display verbose\n\tinformation: "
2200Sstevel@tonic-gate 			    "zone name, id, current state, root directory and "
2210Sstevel@tonic-gate 			    "options.\n\tThe -p option can be used to request "
2220Sstevel@tonic-gate 			    "machine-parsable output.  The -v\n\tand -p "
2230Sstevel@tonic-gate 			    "options are mutually exclusive.  If neither -v "
2240Sstevel@tonic-gate 			    "nor -p is used,\n\tjust the zone name is "
2250Sstevel@tonic-gate 			    "listed."));
2260Sstevel@tonic-gate 		case CMD_VERIFY:
2270Sstevel@tonic-gate 			return (gettext("Check to make sure the configuration "
2280Sstevel@tonic-gate 			    "can safely be instantiated\n\ton the machine: "
2290Sstevel@tonic-gate 			    "physical network interfaces exist, etc."));
2300Sstevel@tonic-gate 		case CMD_INSTALL:
2310Sstevel@tonic-gate 			return (gettext("Install the configuration on to the "
2320Sstevel@tonic-gate 			    "system."));
2330Sstevel@tonic-gate 		case CMD_UNINSTALL:
2340Sstevel@tonic-gate 			return (gettext("Uninstall the configuration from the "
2350Sstevel@tonic-gate 			    "system.  The -F flag can be used\n\tto force the "
2360Sstevel@tonic-gate 			    "action."));
237766Scarlsonj 		default:
238766Scarlsonj 			return ("");
2390Sstevel@tonic-gate 	}
2400Sstevel@tonic-gate 	/* NOTREACHED */
241222Scomay 	return (NULL);
2420Sstevel@tonic-gate }
2430Sstevel@tonic-gate 
2440Sstevel@tonic-gate /*
2450Sstevel@tonic-gate  * Called with explicit B_TRUE when help is explicitly requested, B_FALSE for
2460Sstevel@tonic-gate  * unexpected errors.
2470Sstevel@tonic-gate  */
2480Sstevel@tonic-gate 
2490Sstevel@tonic-gate static int
2500Sstevel@tonic-gate usage(boolean_t explicit)
2510Sstevel@tonic-gate {
2520Sstevel@tonic-gate 	int i;
2530Sstevel@tonic-gate 	FILE *fd = explicit ? stdout : stderr;
2540Sstevel@tonic-gate 
2550Sstevel@tonic-gate 	(void) fprintf(fd, "%s:\t%s help\n", gettext("usage"), execname);
2560Sstevel@tonic-gate 	(void) fprintf(fd, "\t%s [-z <zone>] list\n", execname);
2570Sstevel@tonic-gate 	(void) fprintf(fd, "\t%s -z <zone> <%s>\n", execname,
2580Sstevel@tonic-gate 	    gettext("subcommand"));
2590Sstevel@tonic-gate 	(void) fprintf(fd, "\n%s:\n\n", gettext("Subcommands"));
2600Sstevel@tonic-gate 	for (i = CMD_MIN; i <= CMD_MAX; i++) {
261766Scarlsonj 		if (cmdtab[i].short_usage == NULL)
262766Scarlsonj 			continue;
2630Sstevel@tonic-gate 		(void) fprintf(fd, "%s\n", cmdtab[i].short_usage);
2640Sstevel@tonic-gate 		if (explicit)
2650Sstevel@tonic-gate 			(void) fprintf(fd, "\t%s\n\n", long_help(i));
2660Sstevel@tonic-gate 	}
2670Sstevel@tonic-gate 	if (!explicit)
2680Sstevel@tonic-gate 		(void) fputs("\n", fd);
2690Sstevel@tonic-gate 	return (Z_USAGE);
2700Sstevel@tonic-gate }
2710Sstevel@tonic-gate 
2720Sstevel@tonic-gate static void
2730Sstevel@tonic-gate sub_usage(char *short_usage, int cmd_num)
2740Sstevel@tonic-gate {
2750Sstevel@tonic-gate 	(void) fprintf(stderr, "%s:\t%s\n", gettext("usage"), short_usage);
2760Sstevel@tonic-gate 	(void) fprintf(stderr, "\t%s\n", long_help(cmd_num));
2770Sstevel@tonic-gate }
2780Sstevel@tonic-gate 
2790Sstevel@tonic-gate /*
2800Sstevel@tonic-gate  * zperror() is like perror(3c) except that this also prints the executable
2810Sstevel@tonic-gate  * name at the start of the message, and takes a boolean indicating whether
2820Sstevel@tonic-gate  * to call libc'c strerror() or that from libzonecfg.
2830Sstevel@tonic-gate  */
2840Sstevel@tonic-gate 
2850Sstevel@tonic-gate static void
2860Sstevel@tonic-gate zperror(const char *str, boolean_t zonecfg_error)
2870Sstevel@tonic-gate {
2880Sstevel@tonic-gate 	(void) fprintf(stderr, "%s: %s: %s\n", execname, str,
2890Sstevel@tonic-gate 	    zonecfg_error ? zonecfg_strerror(errno) : strerror(errno));
2900Sstevel@tonic-gate }
2910Sstevel@tonic-gate 
2920Sstevel@tonic-gate /*
2930Sstevel@tonic-gate  * zperror2() is very similar to zperror() above, except it also prints a
2940Sstevel@tonic-gate  * supplied zone name after the executable.
2950Sstevel@tonic-gate  *
2960Sstevel@tonic-gate  * All current consumers of this function want libzonecfg's strerror() rather
2970Sstevel@tonic-gate  * than libc's; if this ever changes, this function can be made more generic
2980Sstevel@tonic-gate  * like zperror() above.
2990Sstevel@tonic-gate  */
3000Sstevel@tonic-gate 
3010Sstevel@tonic-gate static void
3020Sstevel@tonic-gate zperror2(const char *zone, const char *str)
3030Sstevel@tonic-gate {
3040Sstevel@tonic-gate 	(void) fprintf(stderr, "%s: %s: %s: %s\n", execname, zone, str,
3050Sstevel@tonic-gate 	    zonecfg_strerror(errno));
3060Sstevel@tonic-gate }
3070Sstevel@tonic-gate 
3080Sstevel@tonic-gate /* PRINTFLIKE1 */
3090Sstevel@tonic-gate static void
3100Sstevel@tonic-gate zerror(const char *fmt, ...)
3110Sstevel@tonic-gate {
3120Sstevel@tonic-gate 	va_list alist;
3130Sstevel@tonic-gate 
3140Sstevel@tonic-gate 	va_start(alist, fmt);
3150Sstevel@tonic-gate 	(void) fprintf(stderr, "%s: ", execname);
3160Sstevel@tonic-gate 	if (target_zone != NULL)
3170Sstevel@tonic-gate 		(void) fprintf(stderr, "zone '%s': ", target_zone);
3180Sstevel@tonic-gate 	(void) vfprintf(stderr, fmt, alist);
3190Sstevel@tonic-gate 	(void) fprintf(stderr, "\n");
3200Sstevel@tonic-gate 	va_end(alist);
3210Sstevel@tonic-gate }
3220Sstevel@tonic-gate 
3230Sstevel@tonic-gate static void *
3240Sstevel@tonic-gate safe_calloc(size_t nelem, size_t elsize)
3250Sstevel@tonic-gate {
3260Sstevel@tonic-gate 	void *r = calloc(nelem, elsize);
3270Sstevel@tonic-gate 
3280Sstevel@tonic-gate 	if (r == NULL) {
3290Sstevel@tonic-gate 		zerror(gettext("failed to allocate %lu bytes: %s"),
3300Sstevel@tonic-gate 		    (ulong_t)nelem * elsize, strerror(errno));
3310Sstevel@tonic-gate 		exit(Z_ERR);
3320Sstevel@tonic-gate 	}
3330Sstevel@tonic-gate 	return (r);
3340Sstevel@tonic-gate }
3350Sstevel@tonic-gate 
3360Sstevel@tonic-gate static void
3370Sstevel@tonic-gate zone_print(zone_entry_t *zent, boolean_t verbose, boolean_t parsable)
3380Sstevel@tonic-gate {
3390Sstevel@tonic-gate 	static boolean_t firsttime = B_TRUE;
3400Sstevel@tonic-gate 
3410Sstevel@tonic-gate 	assert(!(verbose && parsable));
3420Sstevel@tonic-gate 	if (firsttime && verbose) {
3430Sstevel@tonic-gate 		firsttime = B_FALSE;
3440Sstevel@tonic-gate 		(void) printf("%*s %-16s %-14s %-30s\n", ZONEID_WIDTH, "ID",
3450Sstevel@tonic-gate 		    "NAME", "STATUS", "PATH");
3460Sstevel@tonic-gate 	}
3470Sstevel@tonic-gate 	if (!verbose) {
3480Sstevel@tonic-gate 		if (!parsable) {
3490Sstevel@tonic-gate 			(void) printf("%s\n", zent->zname);
3500Sstevel@tonic-gate 			return;
3510Sstevel@tonic-gate 		}
3520Sstevel@tonic-gate 		if (zent->zid == ZONE_ID_UNDEFINED)
3530Sstevel@tonic-gate 			(void) printf("-");
3540Sstevel@tonic-gate 		else
3550Sstevel@tonic-gate 			(void) printf("%lu", zent->zid);
3560Sstevel@tonic-gate 		(void) printf(":%s:%s:%s\n", zent->zname, zent->zstate_str,
3570Sstevel@tonic-gate 		    zent->zroot);
3580Sstevel@tonic-gate 		return;
3590Sstevel@tonic-gate 	}
3600Sstevel@tonic-gate 	if (zent->zstate_str != NULL) {
3610Sstevel@tonic-gate 		if (zent->zid == ZONE_ID_UNDEFINED)
3620Sstevel@tonic-gate 			(void) printf("%*s", ZONEID_WIDTH, "-");
3630Sstevel@tonic-gate 		else
3640Sstevel@tonic-gate 			(void) printf("%*lu", ZONEID_WIDTH, zent->zid);
3650Sstevel@tonic-gate 		(void) printf(" %-16s %-14s %-30s\n", zent->zname,
3660Sstevel@tonic-gate 		    zent->zstate_str, zent->zroot);
3670Sstevel@tonic-gate 	}
3680Sstevel@tonic-gate }
3690Sstevel@tonic-gate 
3700Sstevel@tonic-gate static int
371766Scarlsonj lookup_zone_info(const char *zone_name, zoneid_t zid, zone_entry_t *zent)
3720Sstevel@tonic-gate {
3730Sstevel@tonic-gate 	char root[MAXPATHLEN];
3740Sstevel@tonic-gate 	int err;
3750Sstevel@tonic-gate 
3760Sstevel@tonic-gate 	(void) strlcpy(zent->zname, zone_name, sizeof (zent->zname));
3770Sstevel@tonic-gate 	(void) strlcpy(zent->zroot, "???", sizeof (zent->zroot));
3780Sstevel@tonic-gate 	zent->zstate_str = "???";
3790Sstevel@tonic-gate 
380766Scarlsonj 	zent->zid = zid;
3810Sstevel@tonic-gate 
3820Sstevel@tonic-gate 	if ((err = zone_get_zonepath(zent->zname, root, sizeof (root))) !=
3830Sstevel@tonic-gate 	    Z_OK) {
3840Sstevel@tonic-gate 		errno = err;
3850Sstevel@tonic-gate 		zperror2(zent->zname, gettext("could not get zone path"));
3860Sstevel@tonic-gate 		return (Z_ERR);
3870Sstevel@tonic-gate 	}
3880Sstevel@tonic-gate 	(void) strlcpy(zent->zroot, root, sizeof (zent->zroot));
3890Sstevel@tonic-gate 
3900Sstevel@tonic-gate 	if ((err = zone_get_state(zent->zname, &zent->zstate_num)) != Z_OK) {
3910Sstevel@tonic-gate 		errno = err;
3920Sstevel@tonic-gate 		zperror2(zent->zname, gettext("could not get state"));
3930Sstevel@tonic-gate 		return (Z_ERR);
3940Sstevel@tonic-gate 	}
3950Sstevel@tonic-gate 	zent->zstate_str = zone_state_str(zent->zstate_num);
3960Sstevel@tonic-gate 
3970Sstevel@tonic-gate 	return (Z_OK);
3980Sstevel@tonic-gate }
3990Sstevel@tonic-gate 
4000Sstevel@tonic-gate /*
4010Sstevel@tonic-gate  * fetch_zents() calls zone_list(2) to find out how many zones are running
4020Sstevel@tonic-gate  * (which is stored in the global nzents), then calls zone_list(2) again
4030Sstevel@tonic-gate  * to fetch the list of running zones (stored in the global zents).  This
4040Sstevel@tonic-gate  * function may be called multiple times, so if zents is already set, we
4050Sstevel@tonic-gate  * return immediately to save work.
4060Sstevel@tonic-gate  */
4070Sstevel@tonic-gate 
4080Sstevel@tonic-gate static int
409766Scarlsonj fetch_zents(void)
4100Sstevel@tonic-gate {
4110Sstevel@tonic-gate 	zoneid_t *zids = NULL;
4120Sstevel@tonic-gate 	uint_t nzents_saved;
413766Scarlsonj 	int i, retv;
414766Scarlsonj 	FILE *fp;
415766Scarlsonj 	boolean_t inaltroot;
416766Scarlsonj 	zone_entry_t *zentp;
4170Sstevel@tonic-gate 
4180Sstevel@tonic-gate 	if (nzents > 0)
4190Sstevel@tonic-gate 		return (Z_OK);
4200Sstevel@tonic-gate 
4210Sstevel@tonic-gate 	if (zone_list(NULL, &nzents) != 0) {
4220Sstevel@tonic-gate 		zperror(gettext("failed to get zoneid list"), B_FALSE);
4230Sstevel@tonic-gate 		return (Z_ERR);
4240Sstevel@tonic-gate 	}
4250Sstevel@tonic-gate 
4260Sstevel@tonic-gate again:
4270Sstevel@tonic-gate 	if (nzents == 0)
4280Sstevel@tonic-gate 		return (Z_OK);
4290Sstevel@tonic-gate 
4300Sstevel@tonic-gate 	zids = safe_calloc(nzents, sizeof (zoneid_t));
4310Sstevel@tonic-gate 	nzents_saved = nzents;
4320Sstevel@tonic-gate 
4330Sstevel@tonic-gate 	if (zone_list(zids, &nzents) != 0) {
4340Sstevel@tonic-gate 		zperror(gettext("failed to get zone list"), B_FALSE);
4350Sstevel@tonic-gate 		free(zids);
4360Sstevel@tonic-gate 		return (Z_ERR);
4370Sstevel@tonic-gate 	}
4380Sstevel@tonic-gate 	if (nzents != nzents_saved) {
4390Sstevel@tonic-gate 		/* list changed, try again */
4400Sstevel@tonic-gate 		free(zids);
4410Sstevel@tonic-gate 		goto again;
4420Sstevel@tonic-gate 	}
4430Sstevel@tonic-gate 
4440Sstevel@tonic-gate 	zents = safe_calloc(nzents, sizeof (zone_entry_t));
4450Sstevel@tonic-gate 
446766Scarlsonj 	inaltroot = zonecfg_in_alt_root();
447766Scarlsonj 	if (inaltroot)
448766Scarlsonj 		fp = zonecfg_open_scratch("", B_FALSE);
449766Scarlsonj 	else
450766Scarlsonj 		fp = NULL;
451766Scarlsonj 	zentp = zents;
452766Scarlsonj 	retv = Z_OK;
4530Sstevel@tonic-gate 	for (i = 0; i < nzents; i++) {
4540Sstevel@tonic-gate 		char name[ZONENAME_MAX];
455766Scarlsonj 		char altname[ZONENAME_MAX];
4560Sstevel@tonic-gate 
457766Scarlsonj 		if (getzonenamebyid(zids[i], name, sizeof (name)) < 0) {
4580Sstevel@tonic-gate 			zperror(gettext("failed to get zone name"), B_FALSE);
459766Scarlsonj 			retv = Z_ERR;
460766Scarlsonj 			continue;
461766Scarlsonj 		}
462766Scarlsonj 		if (zonecfg_is_scratch(name)) {
463766Scarlsonj 			/* Ignore scratch zones by default */
464766Scarlsonj 			if (!inaltroot)
465766Scarlsonj 				continue;
466766Scarlsonj 			if (fp == NULL ||
467766Scarlsonj 			    zonecfg_reverse_scratch(fp, name, altname,
468766Scarlsonj 			    sizeof (altname), NULL, 0) == -1) {
469*924Sgjelinek 				zerror(gettext("could not resolve scratch "
470766Scarlsonj 				    "zone %s"), name);
471766Scarlsonj 				retv = Z_ERR;
472766Scarlsonj 				continue;
473766Scarlsonj 			}
474766Scarlsonj 			(void) strcpy(name, altname);
475766Scarlsonj 		} else {
476766Scarlsonj 			/* Ignore non-scratch when in an alternate root */
477766Scarlsonj 			if (inaltroot && strcmp(name, GLOBAL_ZONENAME) != 0)
478766Scarlsonj 				continue;
479766Scarlsonj 		}
480766Scarlsonj 		if (lookup_zone_info(name, zids[i], zentp) != Z_OK) {
481766Scarlsonj 			zerror(gettext("failed to get zone data"));
482766Scarlsonj 			retv = Z_ERR;
483766Scarlsonj 			continue;
484766Scarlsonj 		}
485766Scarlsonj 		zentp++;
4860Sstevel@tonic-gate 	}
487766Scarlsonj 	nzents = zentp - zents;
488766Scarlsonj 	if (fp != NULL)
489766Scarlsonj 		zonecfg_close_scratch(fp);
4900Sstevel@tonic-gate 
4910Sstevel@tonic-gate 	free(zids);
492766Scarlsonj 	return (retv);
4930Sstevel@tonic-gate }
4940Sstevel@tonic-gate 
495766Scarlsonj static int
4960Sstevel@tonic-gate zone_print_list(zone_state_t min_state, boolean_t verbose, boolean_t parsable)
4970Sstevel@tonic-gate {
4980Sstevel@tonic-gate 	int i;
4990Sstevel@tonic-gate 	zone_entry_t zent;
5000Sstevel@tonic-gate 	FILE *cookie;
5010Sstevel@tonic-gate 	char *name;
5020Sstevel@tonic-gate 
5030Sstevel@tonic-gate 	/*
5040Sstevel@tonic-gate 	 * First get the list of running zones from the kernel and print them.
5050Sstevel@tonic-gate 	 * If that is all we need, then return.
5060Sstevel@tonic-gate 	 */
507766Scarlsonj 	if ((i = fetch_zents()) != Z_OK) {
5080Sstevel@tonic-gate 		/*
5090Sstevel@tonic-gate 		 * No need for error messages; fetch_zents() has already taken
5100Sstevel@tonic-gate 		 * care of this.
5110Sstevel@tonic-gate 		 */
512766Scarlsonj 		return (i);
5130Sstevel@tonic-gate 	}
514766Scarlsonj 	for (i = 0; i < nzents; i++)
5150Sstevel@tonic-gate 		zone_print(&zents[i], verbose, parsable);
5160Sstevel@tonic-gate 	if (min_state >= ZONE_STATE_RUNNING)
517766Scarlsonj 		return (Z_OK);
5180Sstevel@tonic-gate 	/*
5190Sstevel@tonic-gate 	 * Next, get the full list of zones from the configuration, skipping
5200Sstevel@tonic-gate 	 * any we have already printed.
5210Sstevel@tonic-gate 	 */
5220Sstevel@tonic-gate 	cookie = setzoneent();
5230Sstevel@tonic-gate 	while ((name = getzoneent(cookie)) != NULL) {
5240Sstevel@tonic-gate 		for (i = 0; i < nzents; i++) {
5250Sstevel@tonic-gate 			if (strcmp(zents[i].zname, name) == 0)
5260Sstevel@tonic-gate 				break;
5270Sstevel@tonic-gate 		}
5280Sstevel@tonic-gate 		if (i < nzents) {
5290Sstevel@tonic-gate 			free(name);
5300Sstevel@tonic-gate 			continue;
5310Sstevel@tonic-gate 		}
532766Scarlsonj 		if (lookup_zone_info(name, ZONE_ID_UNDEFINED, &zent) != Z_OK) {
5330Sstevel@tonic-gate 			free(name);
5340Sstevel@tonic-gate 			continue;
5350Sstevel@tonic-gate 		}
5360Sstevel@tonic-gate 		free(name);
5370Sstevel@tonic-gate 		if (zent.zstate_num >= min_state)
5380Sstevel@tonic-gate 			zone_print(&zent, verbose, parsable);
5390Sstevel@tonic-gate 	}
5400Sstevel@tonic-gate 	endzoneent(cookie);
541766Scarlsonj 	return (Z_OK);
5420Sstevel@tonic-gate }
5430Sstevel@tonic-gate 
5440Sstevel@tonic-gate static zone_entry_t *
5450Sstevel@tonic-gate lookup_running_zone(char *str)
5460Sstevel@tonic-gate {
5470Sstevel@tonic-gate 	zoneid_t zoneid;
5480Sstevel@tonic-gate 	char *cp;
5490Sstevel@tonic-gate 	int i;
5500Sstevel@tonic-gate 
5510Sstevel@tonic-gate 	if (fetch_zents() != Z_OK)
5520Sstevel@tonic-gate 		return (NULL);
5530Sstevel@tonic-gate 
5540Sstevel@tonic-gate 	for (i = 0; i < nzents; i++) {
5550Sstevel@tonic-gate 		if (strcmp(str, zents[i].zname) == 0)
5560Sstevel@tonic-gate 			return (&zents[i]);
5570Sstevel@tonic-gate 	}
5580Sstevel@tonic-gate 	errno = 0;
5590Sstevel@tonic-gate 	zoneid = strtol(str, &cp, 0);
5600Sstevel@tonic-gate 	if (zoneid < MIN_ZONEID || zoneid > MAX_ZONEID ||
5610Sstevel@tonic-gate 	    errno != 0 || *cp != '\0')
5620Sstevel@tonic-gate 		return (NULL);
5630Sstevel@tonic-gate 	for (i = 0; i < nzents; i++) {
5640Sstevel@tonic-gate 		if (zoneid == zents[i].zid)
5650Sstevel@tonic-gate 			return (&zents[i]);
5660Sstevel@tonic-gate 	}
5670Sstevel@tonic-gate 	return (NULL);
5680Sstevel@tonic-gate }
5690Sstevel@tonic-gate 
5700Sstevel@tonic-gate /*
5710Sstevel@tonic-gate  * Check a bit in a mode_t: if on is B_TRUE, that bit should be on; if
5720Sstevel@tonic-gate  * B_FALSE, it should be off.  Return B_TRUE if the mode is bad (incorrect).
5730Sstevel@tonic-gate  */
5740Sstevel@tonic-gate static boolean_t
5750Sstevel@tonic-gate bad_mode_bit(mode_t mode, mode_t bit, boolean_t on, char *file)
5760Sstevel@tonic-gate {
5770Sstevel@tonic-gate 	char *str;
5780Sstevel@tonic-gate 
5790Sstevel@tonic-gate 	assert(bit == S_IRUSR || bit == S_IWUSR || bit == S_IXUSR ||
5800Sstevel@tonic-gate 	    bit == S_IRGRP || bit == S_IWGRP || bit == S_IXGRP ||
5810Sstevel@tonic-gate 	    bit == S_IROTH || bit == S_IWOTH || bit == S_IXOTH);
5820Sstevel@tonic-gate 	/*
5830Sstevel@tonic-gate 	 * TRANSLATION_NOTE
5840Sstevel@tonic-gate 	 * The strings below will be used as part of a larger message,
5850Sstevel@tonic-gate 	 * either:
5860Sstevel@tonic-gate 	 * (file name) must be (owner|group|world) (read|writ|execut)able
5870Sstevel@tonic-gate 	 * or
5880Sstevel@tonic-gate 	 * (file name) must not be (owner|group|world) (read|writ|execut)able
5890Sstevel@tonic-gate 	 */
5900Sstevel@tonic-gate 	switch (bit) {
5910Sstevel@tonic-gate 	case S_IRUSR:
5920Sstevel@tonic-gate 		str = gettext("owner readable");
5930Sstevel@tonic-gate 		break;
5940Sstevel@tonic-gate 	case S_IWUSR:
5950Sstevel@tonic-gate 		str = gettext("owner writable");
5960Sstevel@tonic-gate 		break;
5970Sstevel@tonic-gate 	case S_IXUSR:
5980Sstevel@tonic-gate 		str = gettext("owner executable");
5990Sstevel@tonic-gate 		break;
6000Sstevel@tonic-gate 	case S_IRGRP:
6010Sstevel@tonic-gate 		str = gettext("group readable");
6020Sstevel@tonic-gate 		break;
6030Sstevel@tonic-gate 	case S_IWGRP:
6040Sstevel@tonic-gate 		str = gettext("group writable");
6050Sstevel@tonic-gate 		break;
6060Sstevel@tonic-gate 	case S_IXGRP:
6070Sstevel@tonic-gate 		str = gettext("group executable");
6080Sstevel@tonic-gate 		break;
6090Sstevel@tonic-gate 	case S_IROTH:
6100Sstevel@tonic-gate 		str = gettext("world readable");
6110Sstevel@tonic-gate 		break;
6120Sstevel@tonic-gate 	case S_IWOTH:
6130Sstevel@tonic-gate 		str = gettext("world writable");
6140Sstevel@tonic-gate 		break;
6150Sstevel@tonic-gate 	case S_IXOTH:
6160Sstevel@tonic-gate 		str = gettext("world executable");
6170Sstevel@tonic-gate 		break;
6180Sstevel@tonic-gate 	}
6190Sstevel@tonic-gate 	if ((mode & bit) == (on ? 0 : bit)) {
6200Sstevel@tonic-gate 		/*
6210Sstevel@tonic-gate 		 * TRANSLATION_NOTE
6220Sstevel@tonic-gate 		 * The first parameter below is a file name; the second
6230Sstevel@tonic-gate 		 * is one of the "(owner|group|world) (read|writ|execut)able"
6240Sstevel@tonic-gate 		 * strings from above.
6250Sstevel@tonic-gate 		 */
6260Sstevel@tonic-gate 		/*
6270Sstevel@tonic-gate 		 * The code below could be simplified but not in a way
6280Sstevel@tonic-gate 		 * that would easily translate to non-English locales.
6290Sstevel@tonic-gate 		 */
6300Sstevel@tonic-gate 		if (on) {
6310Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("%s must be %s.\n"),
6320Sstevel@tonic-gate 			    file, str);
6330Sstevel@tonic-gate 		} else {
6340Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("%s must not be %s.\n"),
6350Sstevel@tonic-gate 			    file, str);
6360Sstevel@tonic-gate 		}
6370Sstevel@tonic-gate 		return (B_TRUE);
6380Sstevel@tonic-gate 	}
6390Sstevel@tonic-gate 	return (B_FALSE);
6400Sstevel@tonic-gate }
6410Sstevel@tonic-gate 
6420Sstevel@tonic-gate /*
6430Sstevel@tonic-gate  * We want to make sure that no zone has its zone path as a child node
6440Sstevel@tonic-gate  * (in the directory sense) of any other.  We do that by comparing this
6450Sstevel@tonic-gate  * zone's path to the path of all other (non-global) zones.  The comparison
6460Sstevel@tonic-gate  * in each case is simple: add '/' to the end of the path, then do a
6470Sstevel@tonic-gate  * strncmp() of the two paths, using the length of the shorter one.
6480Sstevel@tonic-gate  */
6490Sstevel@tonic-gate 
6500Sstevel@tonic-gate static int
6510Sstevel@tonic-gate crosscheck_zonepaths(char *path)
6520Sstevel@tonic-gate {
6530Sstevel@tonic-gate 	char rpath[MAXPATHLEN];		/* resolved path */
6540Sstevel@tonic-gate 	char path_copy[MAXPATHLEN];	/* copy of original path */
6550Sstevel@tonic-gate 	char rpath_copy[MAXPATHLEN];	/* copy of original rpath */
6560Sstevel@tonic-gate 	struct zoneent *ze;
6570Sstevel@tonic-gate 	int res, err;
6580Sstevel@tonic-gate 	FILE *cookie;
6590Sstevel@tonic-gate 
6600Sstevel@tonic-gate 	cookie = setzoneent();
6610Sstevel@tonic-gate 	while ((ze = getzoneent_private(cookie)) != NULL) {
6620Sstevel@tonic-gate 		/* Skip zones which are not installed. */
6630Sstevel@tonic-gate 		if (ze->zone_state < ZONE_STATE_INSTALLED) {
6640Sstevel@tonic-gate 			free(ze);
6650Sstevel@tonic-gate 			continue;
6660Sstevel@tonic-gate 		}
6670Sstevel@tonic-gate 		/* Skip the global zone and the current target zone. */
6680Sstevel@tonic-gate 		if (strcmp(ze->zone_name, GLOBAL_ZONENAME) == 0 ||
6690Sstevel@tonic-gate 		    strcmp(ze->zone_name, target_zone) == 0) {
6700Sstevel@tonic-gate 			free(ze);
6710Sstevel@tonic-gate 			continue;
6720Sstevel@tonic-gate 		}
6730Sstevel@tonic-gate 		if (strlen(ze->zone_path) == 0) {
6740Sstevel@tonic-gate 			/* old index file without path, fall back */
6750Sstevel@tonic-gate 			if ((err = zone_get_zonepath(ze->zone_name,
6760Sstevel@tonic-gate 			    ze->zone_path, sizeof (ze->zone_path))) != Z_OK) {
6770Sstevel@tonic-gate 				errno = err;
6780Sstevel@tonic-gate 				zperror2(ze->zone_name,
6790Sstevel@tonic-gate 				    gettext("could not get zone path"));
6800Sstevel@tonic-gate 				free(ze);
6810Sstevel@tonic-gate 				continue;
6820Sstevel@tonic-gate 			}
6830Sstevel@tonic-gate 		}
684766Scarlsonj 		(void) snprintf(path_copy, sizeof (path_copy), "%s%s",
685766Scarlsonj 		    zonecfg_get_root(), ze->zone_path);
686766Scarlsonj 		res = resolvepath(path_copy, rpath, sizeof (rpath));
6870Sstevel@tonic-gate 		if (res == -1) {
6880Sstevel@tonic-gate 			if (errno != ENOENT) {
689766Scarlsonj 				zperror(path_copy, B_FALSE);
6900Sstevel@tonic-gate 				free(ze);
6910Sstevel@tonic-gate 				return (Z_ERR);
6920Sstevel@tonic-gate 			}
6930Sstevel@tonic-gate 			(void) printf(gettext("WARNING: zone %s is installed, "
6940Sstevel@tonic-gate 			    "but its %s %s does not exist.\n"), ze->zone_name,
695766Scarlsonj 			    "zonepath", path_copy);
6960Sstevel@tonic-gate 			free(ze);
6970Sstevel@tonic-gate 			continue;
6980Sstevel@tonic-gate 		}
6990Sstevel@tonic-gate 		rpath[res] = '\0';
7000Sstevel@tonic-gate 		(void) snprintf(path_copy, sizeof (path_copy), "%s/", path);
7010Sstevel@tonic-gate 		(void) snprintf(rpath_copy, sizeof (rpath_copy), "%s/", rpath);
7020Sstevel@tonic-gate 		if (strncmp(path_copy, rpath_copy,
7030Sstevel@tonic-gate 		    min(strlen(path_copy), strlen(rpath_copy))) == 0) {
704*924Sgjelinek 			/*
705*924Sgjelinek 			 * TRANSLATION_NOTE
706*924Sgjelinek 			 * zonepath is a literal that should not be translated.
707*924Sgjelinek 			 */
7080Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("%s zonepath (%s) and "
7090Sstevel@tonic-gate 			    "%s zonepath (%s) overlap.\n"),
7100Sstevel@tonic-gate 			    target_zone, path, ze->zone_name, rpath);
7110Sstevel@tonic-gate 			free(ze);
7120Sstevel@tonic-gate 			return (Z_ERR);
7130Sstevel@tonic-gate 		}
7140Sstevel@tonic-gate 		free(ze);
7150Sstevel@tonic-gate 	}
7160Sstevel@tonic-gate 	endzoneent(cookie);
7170Sstevel@tonic-gate 	return (Z_OK);
7180Sstevel@tonic-gate }
7190Sstevel@tonic-gate 
7200Sstevel@tonic-gate static int
7210Sstevel@tonic-gate validate_zonepath(char *path, int cmd_num)
7220Sstevel@tonic-gate {
7230Sstevel@tonic-gate 	int res;			/* result of last library/system call */
7240Sstevel@tonic-gate 	boolean_t err = B_FALSE;	/* have we run into an error? */
7250Sstevel@tonic-gate 	struct stat stbuf;
7260Sstevel@tonic-gate 	struct statvfs vfsbuf;
7270Sstevel@tonic-gate 	char rpath[MAXPATHLEN];		/* resolved path */
7280Sstevel@tonic-gate 	char ppath[MAXPATHLEN];		/* parent path */
7290Sstevel@tonic-gate 	char rppath[MAXPATHLEN];	/* resolved parent path */
7300Sstevel@tonic-gate 	char rootpath[MAXPATHLEN];	/* root path */
7310Sstevel@tonic-gate 	zone_state_t state;
7320Sstevel@tonic-gate 
7330Sstevel@tonic-gate 	if (path[0] != '/') {
7340Sstevel@tonic-gate 		(void) fprintf(stderr,
7350Sstevel@tonic-gate 		    gettext("%s is not an absolute path.\n"), path);
7360Sstevel@tonic-gate 		return (Z_ERR);
7370Sstevel@tonic-gate 	}
7380Sstevel@tonic-gate 	if ((res = resolvepath(path, rpath, sizeof (rpath))) == -1) {
7390Sstevel@tonic-gate 		if ((errno != ENOENT) ||
7400Sstevel@tonic-gate 		    (cmd_num != CMD_VERIFY && cmd_num != CMD_INSTALL)) {
7410Sstevel@tonic-gate 			zperror(path, B_FALSE);
7420Sstevel@tonic-gate 			return (Z_ERR);
7430Sstevel@tonic-gate 		}
7440Sstevel@tonic-gate 		if (cmd_num == CMD_VERIFY) {
745*924Sgjelinek 			/*
746*924Sgjelinek 			 * TRANSLATION_NOTE
747*924Sgjelinek 			 * zoneadm is a literal that should not be translated.
748*924Sgjelinek 			 */
7490Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("WARNING: %s does not "
750*924Sgjelinek 			    "exist, so it could not be verified.\nWhen "
751*924Sgjelinek 			    "'zoneadm %s' is run, '%s' will try to create\n%s, "
752*924Sgjelinek 			    "and '%s' will be tried again,\nbut the '%s' may "
753*924Sgjelinek 			    "fail if:\nthe parent directory of %s is group- or "
754*924Sgjelinek 			    "other-writable\nor\n%s overlaps with any other "
7550Sstevel@tonic-gate 			    "installed zones.\n"), path,
7560Sstevel@tonic-gate 			    cmd_to_str(CMD_INSTALL), cmd_to_str(CMD_INSTALL),
7570Sstevel@tonic-gate 			    path, cmd_to_str(CMD_VERIFY),
7580Sstevel@tonic-gate 			    cmd_to_str(CMD_VERIFY), path, path);
7590Sstevel@tonic-gate 			return (Z_OK);
7600Sstevel@tonic-gate 		}
7610Sstevel@tonic-gate 		/*
7620Sstevel@tonic-gate 		 * The zonepath is supposed to be mode 700 but its
7630Sstevel@tonic-gate 		 * parent(s) 755.  So use 755 on the mkdirp() then
7640Sstevel@tonic-gate 		 * chmod() the zonepath itself to 700.
7650Sstevel@tonic-gate 		 */
7660Sstevel@tonic-gate 		if (mkdirp(path, DEFAULT_DIR_MODE) < 0) {
7670Sstevel@tonic-gate 			zperror(path, B_FALSE);
7680Sstevel@tonic-gate 			return (Z_ERR);
7690Sstevel@tonic-gate 		}
7700Sstevel@tonic-gate 		/*
7710Sstevel@tonic-gate 		 * If the chmod() fails, report the error, but might
7720Sstevel@tonic-gate 		 * as well continue the verify procedure.
7730Sstevel@tonic-gate 		 */
7740Sstevel@tonic-gate 		if (chmod(path, S_IRWXU) != 0)
7750Sstevel@tonic-gate 			zperror(path, B_FALSE);
7760Sstevel@tonic-gate 		/*
7770Sstevel@tonic-gate 		 * Since the mkdir() succeeded, we should not have to
7780Sstevel@tonic-gate 		 * worry about a subsequent ENOENT, thus this should
7790Sstevel@tonic-gate 		 * only recurse once.
7800Sstevel@tonic-gate 		 */
7810Sstevel@tonic-gate 		return (validate_zonepath(path, CMD_INSTALL));
7820Sstevel@tonic-gate 	}
7830Sstevel@tonic-gate 	rpath[res] = '\0';
7840Sstevel@tonic-gate 	if (strcmp(path, rpath) != 0) {
7850Sstevel@tonic-gate 		errno = Z_RESOLVED_PATH;
7860Sstevel@tonic-gate 		zperror(path, B_TRUE);
7870Sstevel@tonic-gate 		return (Z_ERR);
7880Sstevel@tonic-gate 	}
7890Sstevel@tonic-gate 	if ((res = stat(rpath, &stbuf)) != 0) {
7900Sstevel@tonic-gate 		zperror(rpath, B_FALSE);
7910Sstevel@tonic-gate 		return (Z_ERR);
7920Sstevel@tonic-gate 	}
7930Sstevel@tonic-gate 	if (!S_ISDIR(stbuf.st_mode)) {
7940Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("%s is not a directory.\n"),
7950Sstevel@tonic-gate 		    rpath);
7960Sstevel@tonic-gate 		return (Z_ERR);
7970Sstevel@tonic-gate 	}
7980Sstevel@tonic-gate 	if ((strcmp(stbuf.st_fstype, MNTTYPE_TMPFS) == 0) ||
7990Sstevel@tonic-gate 	    (strcmp(stbuf.st_fstype, MNTTYPE_XMEMFS) == 0)) {
8000Sstevel@tonic-gate 		(void) printf(gettext("WARNING: %s is on a temporary "
8010Sstevel@tonic-gate 		    "file-system.\n"), rpath);
8020Sstevel@tonic-gate 	}
8030Sstevel@tonic-gate 	if (crosscheck_zonepaths(rpath) != Z_OK)
8040Sstevel@tonic-gate 		return (Z_ERR);
8050Sstevel@tonic-gate 	/*
8060Sstevel@tonic-gate 	 * Try to collect and report as many minor errors as possible
8070Sstevel@tonic-gate 	 * before returning, so the user can learn everything that needs
8080Sstevel@tonic-gate 	 * to be fixed up front.
8090Sstevel@tonic-gate 	 */
8100Sstevel@tonic-gate 	if (stbuf.st_uid != 0) {
8110Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("%s is not owned by root.\n"),
8120Sstevel@tonic-gate 		    rpath);
8130Sstevel@tonic-gate 		err = B_TRUE;
8140Sstevel@tonic-gate 	}
8150Sstevel@tonic-gate 	err |= bad_mode_bit(stbuf.st_mode, S_IRUSR, B_TRUE, rpath);
8160Sstevel@tonic-gate 	err |= bad_mode_bit(stbuf.st_mode, S_IWUSR, B_TRUE, rpath);
8170Sstevel@tonic-gate 	err |= bad_mode_bit(stbuf.st_mode, S_IXUSR, B_TRUE, rpath);
8180Sstevel@tonic-gate 	err |= bad_mode_bit(stbuf.st_mode, S_IRGRP, B_FALSE, rpath);
8190Sstevel@tonic-gate 	err |= bad_mode_bit(stbuf.st_mode, S_IWGRP, B_FALSE, rpath);
8200Sstevel@tonic-gate 	err |= bad_mode_bit(stbuf.st_mode, S_IXGRP, B_FALSE, rpath);
8210Sstevel@tonic-gate 	err |= bad_mode_bit(stbuf.st_mode, S_IROTH, B_FALSE, rpath);
8220Sstevel@tonic-gate 	err |= bad_mode_bit(stbuf.st_mode, S_IWOTH, B_FALSE, rpath);
8230Sstevel@tonic-gate 	err |= bad_mode_bit(stbuf.st_mode, S_IXOTH, B_FALSE, rpath);
8240Sstevel@tonic-gate 
8250Sstevel@tonic-gate 	(void) snprintf(ppath, sizeof (ppath), "%s/..", path);
8260Sstevel@tonic-gate 	if ((res = resolvepath(ppath, rppath, sizeof (rppath))) == -1) {
8270Sstevel@tonic-gate 		zperror(ppath, B_FALSE);
8280Sstevel@tonic-gate 		return (Z_ERR);
8290Sstevel@tonic-gate 	}
8300Sstevel@tonic-gate 	rppath[res] = '\0';
8310Sstevel@tonic-gate 	if ((res = stat(rppath, &stbuf)) != 0) {
8320Sstevel@tonic-gate 		zperror(rppath, B_FALSE);
8330Sstevel@tonic-gate 		return (Z_ERR);
8340Sstevel@tonic-gate 	}
8350Sstevel@tonic-gate 	/* theoretically impossible */
8360Sstevel@tonic-gate 	if (!S_ISDIR(stbuf.st_mode)) {
8370Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("%s is not a directory.\n"),
8380Sstevel@tonic-gate 		    rppath);
8390Sstevel@tonic-gate 		return (Z_ERR);
8400Sstevel@tonic-gate 	}
8410Sstevel@tonic-gate 	if (stbuf.st_uid != 0) {
8420Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("%s is not owned by root.\n"),
8430Sstevel@tonic-gate 		    rppath);
8440Sstevel@tonic-gate 		err = B_TRUE;
8450Sstevel@tonic-gate 	}
8460Sstevel@tonic-gate 	err |= bad_mode_bit(stbuf.st_mode, S_IRUSR, B_TRUE, rppath);
8470Sstevel@tonic-gate 	err |= bad_mode_bit(stbuf.st_mode, S_IWUSR, B_TRUE, rppath);
8480Sstevel@tonic-gate 	err |= bad_mode_bit(stbuf.st_mode, S_IXUSR, B_TRUE, rppath);
8490Sstevel@tonic-gate 	err |= bad_mode_bit(stbuf.st_mode, S_IWGRP, B_FALSE, rppath);
8500Sstevel@tonic-gate 	err |= bad_mode_bit(stbuf.st_mode, S_IWOTH, B_FALSE, rppath);
8510Sstevel@tonic-gate 	if (strcmp(rpath, rppath) == 0) {
8520Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("%s is its own parent.\n"),
8530Sstevel@tonic-gate 		    rppath);
8540Sstevel@tonic-gate 		err = B_TRUE;
8550Sstevel@tonic-gate 	}
8560Sstevel@tonic-gate 
8570Sstevel@tonic-gate 	if (statvfs(rpath, &vfsbuf) != 0) {
8580Sstevel@tonic-gate 		zperror(rpath, B_FALSE);
8590Sstevel@tonic-gate 		return (Z_ERR);
8600Sstevel@tonic-gate 	}
861823Sgjelinek 	if (strcmp(vfsbuf.f_basetype, MNTTYPE_NFS) == 0) {
862*924Sgjelinek 		/*
863*924Sgjelinek 		 * TRANSLATION_NOTE
864*924Sgjelinek 		 * Zonepath and NFS are literals that should not be translated.
865*924Sgjelinek 		 */
866*924Sgjelinek 		(void) fprintf(stderr, gettext("Zonepath %s is on an NFS "
867*924Sgjelinek 		    "mounted file-system.\n"
868*924Sgjelinek 		    "\tA local file-system must be used.\n"), rpath);
869823Sgjelinek 		return (Z_ERR);
870823Sgjelinek 	}
871823Sgjelinek 	if (vfsbuf.f_flag & ST_NOSUID) {
872*924Sgjelinek 		/*
873*924Sgjelinek 		 * TRANSLATION_NOTE
874*924Sgjelinek 		 * Zonepath and nosuid are literals that should not be
875*924Sgjelinek 		 * translated.
876*924Sgjelinek 		 */
877823Sgjelinek 		(void) fprintf(stderr, gettext("Zonepath %s is on a nosuid "
878*924Sgjelinek 		    "file-system.\n"), rpath);
8790Sstevel@tonic-gate 		return (Z_ERR);
8800Sstevel@tonic-gate 	}
8810Sstevel@tonic-gate 
8820Sstevel@tonic-gate 	if ((res = zone_get_state(target_zone, &state)) != Z_OK) {
8830Sstevel@tonic-gate 		errno = res;
8840Sstevel@tonic-gate 		zperror2(target_zone, gettext("could not get state"));
8850Sstevel@tonic-gate 		return (Z_ERR);
8860Sstevel@tonic-gate 	}
8870Sstevel@tonic-gate 	/*
8880Sstevel@tonic-gate 	 * The existence of the root path is only bad in the configured state,
8890Sstevel@tonic-gate 	 * as it is *supposed* to be there at the installed and later states.
8900Sstevel@tonic-gate 	 * State/command mismatches are caught earlier in verify_details().
8910Sstevel@tonic-gate 	 */
8920Sstevel@tonic-gate 	if (state == ZONE_STATE_CONFIGURED) {
8930Sstevel@tonic-gate 		if (snprintf(rootpath, sizeof (rootpath), "%s/root", rpath) >=
8940Sstevel@tonic-gate 		    sizeof (rootpath)) {
895*924Sgjelinek 			/*
896*924Sgjelinek 			 * TRANSLATION_NOTE
897*924Sgjelinek 			 * Zonepath is a literal that should not be translated.
898*924Sgjelinek 			 */
8990Sstevel@tonic-gate 			(void) fprintf(stderr,
9000Sstevel@tonic-gate 			    gettext("Zonepath %s is too long.\n"), rpath);
9010Sstevel@tonic-gate 			return (Z_ERR);
9020Sstevel@tonic-gate 		}
9030Sstevel@tonic-gate 		if ((res = stat(rootpath, &stbuf)) == 0) {
9040Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("Rootpath %s exists; "
9050Sstevel@tonic-gate 			    "remove or move aside prior to %s.\n"), rootpath,
9060Sstevel@tonic-gate 			    cmd_to_str(CMD_INSTALL));
9070Sstevel@tonic-gate 			return (Z_ERR);
9080Sstevel@tonic-gate 		}
9090Sstevel@tonic-gate 	}
9100Sstevel@tonic-gate 
9110Sstevel@tonic-gate 	return (err ? Z_ERR : Z_OK);
9120Sstevel@tonic-gate }
9130Sstevel@tonic-gate 
9140Sstevel@tonic-gate static void
9150Sstevel@tonic-gate release_lock_file(int lockfd)
9160Sstevel@tonic-gate {
9170Sstevel@tonic-gate 	(void) close(lockfd);
9180Sstevel@tonic-gate }
9190Sstevel@tonic-gate 
9200Sstevel@tonic-gate static int
9210Sstevel@tonic-gate grab_lock_file(const char *zone_name, int *lockfd)
9220Sstevel@tonic-gate {
9230Sstevel@tonic-gate 	char pathbuf[PATH_MAX];
9240Sstevel@tonic-gate 	struct flock flock;
9250Sstevel@tonic-gate 
926766Scarlsonj 	if (snprintf(pathbuf, sizeof (pathbuf), "%s%s", zonecfg_get_root(),
927766Scarlsonj 	    ZONES_TMPDIR) >= sizeof (pathbuf)) {
928766Scarlsonj 		zerror(gettext("alternate root path is too long"));
929766Scarlsonj 		return (Z_ERR);
930766Scarlsonj 	}
931766Scarlsonj 	if (mkdir(pathbuf, S_IRWXU) < 0 && errno != EEXIST) {
932766Scarlsonj 		zerror(gettext("could not mkdir %s: %s"), pathbuf,
9330Sstevel@tonic-gate 		    strerror(errno));
9340Sstevel@tonic-gate 		return (Z_ERR);
9350Sstevel@tonic-gate 	}
936766Scarlsonj 	(void) chmod(pathbuf, S_IRWXU);
9370Sstevel@tonic-gate 
9380Sstevel@tonic-gate 	/*
9390Sstevel@tonic-gate 	 * One of these lock files is created for each zone (when needed).
9400Sstevel@tonic-gate 	 * The lock files are not cleaned up (except on system reboot),
9410Sstevel@tonic-gate 	 * but since there is only one per zone, there is no resource
9420Sstevel@tonic-gate 	 * starvation issue.
9430Sstevel@tonic-gate 	 */
944766Scarlsonj 	if (snprintf(pathbuf, sizeof (pathbuf), "%s%s/%s.zoneadm.lock",
945766Scarlsonj 	    zonecfg_get_root(), ZONES_TMPDIR, zone_name) >= sizeof (pathbuf)) {
946766Scarlsonj 		zerror(gettext("alternate root path is too long"));
947766Scarlsonj 		return (Z_ERR);
948766Scarlsonj 	}
9490Sstevel@tonic-gate 	if ((*lockfd = open(pathbuf, O_RDWR|O_CREAT, S_IRUSR|S_IWUSR)) < 0) {
9500Sstevel@tonic-gate 		zerror(gettext("could not open %s: %s"), pathbuf,
9510Sstevel@tonic-gate 		    strerror(errno));
9520Sstevel@tonic-gate 		return (Z_ERR);
9530Sstevel@tonic-gate 	}
9540Sstevel@tonic-gate 	/*
9550Sstevel@tonic-gate 	 * Lock the file to synchronize with other zoneadmds
9560Sstevel@tonic-gate 	 */
9570Sstevel@tonic-gate 	flock.l_type = F_WRLCK;
9580Sstevel@tonic-gate 	flock.l_whence = SEEK_SET;
9590Sstevel@tonic-gate 	flock.l_start = (off_t)0;
9600Sstevel@tonic-gate 	flock.l_len = (off_t)0;
9610Sstevel@tonic-gate 	if (fcntl(*lockfd, F_SETLKW, &flock) < 0) {
9620Sstevel@tonic-gate 		zerror(gettext("unable to lock %s: %s"), pathbuf,
9630Sstevel@tonic-gate 		    strerror(errno));
9640Sstevel@tonic-gate 		release_lock_file(*lockfd);
9650Sstevel@tonic-gate 		return (Z_ERR);
9660Sstevel@tonic-gate 	}
9670Sstevel@tonic-gate 	return (Z_OK);
9680Sstevel@tonic-gate }
9690Sstevel@tonic-gate 
970766Scarlsonj static boolean_t
9710Sstevel@tonic-gate get_doorname(const char *zone_name, char *buffer)
9720Sstevel@tonic-gate {
973766Scarlsonj 	return (snprintf(buffer, PATH_MAX, "%s" ZONE_DOOR_PATH,
974766Scarlsonj 	    zonecfg_get_root(), zone_name) < PATH_MAX);
9750Sstevel@tonic-gate }
9760Sstevel@tonic-gate 
9770Sstevel@tonic-gate /*
9780Sstevel@tonic-gate  * system daemons are not audited.  For the global zone, this occurs
9790Sstevel@tonic-gate  * "naturally" since init is started with the default audit
9800Sstevel@tonic-gate  * characteristics.  Since zoneadmd is a system daemon and it starts
9810Sstevel@tonic-gate  * init for a zone, it is necessary to clear out the audit
9820Sstevel@tonic-gate  * characteristics inherited from whomever started zoneadmd.  This is
9830Sstevel@tonic-gate  * indicated by the audit id, which is set from the ruid parameter of
9840Sstevel@tonic-gate  * adt_set_user(), below.
9850Sstevel@tonic-gate  */
9860Sstevel@tonic-gate 
9870Sstevel@tonic-gate static void
9880Sstevel@tonic-gate prepare_audit_context()
9890Sstevel@tonic-gate {
9900Sstevel@tonic-gate 	adt_session_data_t	*ah;
9910Sstevel@tonic-gate 	char			*failure = gettext("audit failure: %s");
9920Sstevel@tonic-gate 
9930Sstevel@tonic-gate 	if (adt_start_session(&ah, NULL, 0)) {
9940Sstevel@tonic-gate 		zerror(failure, strerror(errno));
9950Sstevel@tonic-gate 		return;
9960Sstevel@tonic-gate 	}
9970Sstevel@tonic-gate 	if (adt_set_user(ah, ADT_NO_AUDIT, ADT_NO_AUDIT,
9980Sstevel@tonic-gate 	    ADT_NO_AUDIT, ADT_NO_AUDIT, NULL, ADT_NEW)) {
9990Sstevel@tonic-gate 		zerror(failure, strerror(errno));
10000Sstevel@tonic-gate 		(void) adt_end_session(ah);
10010Sstevel@tonic-gate 		return;
10020Sstevel@tonic-gate 	}
10030Sstevel@tonic-gate 	if (adt_set_proc(ah))
10040Sstevel@tonic-gate 		zerror(failure, strerror(errno));
10050Sstevel@tonic-gate 
10060Sstevel@tonic-gate 	(void) adt_end_session(ah);
10070Sstevel@tonic-gate }
10080Sstevel@tonic-gate 
10090Sstevel@tonic-gate static int
10100Sstevel@tonic-gate start_zoneadmd(const char *zone_name)
10110Sstevel@tonic-gate {
10120Sstevel@tonic-gate 	char doorpath[PATH_MAX];
10130Sstevel@tonic-gate 	pid_t child_pid;
10140Sstevel@tonic-gate 	int error = Z_ERR;
10150Sstevel@tonic-gate 	int doorfd, lockfd;
10160Sstevel@tonic-gate 	struct door_info info;
10170Sstevel@tonic-gate 
1018766Scarlsonj 	if (!get_doorname(zone_name, doorpath))
1019766Scarlsonj 		return (Z_ERR);
10200Sstevel@tonic-gate 
10210Sstevel@tonic-gate 	if (grab_lock_file(zone_name, &lockfd) != Z_OK)
10220Sstevel@tonic-gate 		return (Z_ERR);
10230Sstevel@tonic-gate 
10240Sstevel@tonic-gate 	/*
10250Sstevel@tonic-gate 	 * Now that we have the lock, re-confirm that the daemon is
10260Sstevel@tonic-gate 	 * *not* up and working fine.  If it is still down, we have a green
10270Sstevel@tonic-gate 	 * light to start it.
10280Sstevel@tonic-gate 	 */
10290Sstevel@tonic-gate 	if ((doorfd = open(doorpath, O_RDONLY)) < 0) {
10300Sstevel@tonic-gate 		if (errno != ENOENT) {
10310Sstevel@tonic-gate 			zperror(doorpath, B_FALSE);
10320Sstevel@tonic-gate 			goto out;
10330Sstevel@tonic-gate 		}
10340Sstevel@tonic-gate 	} else {
10350Sstevel@tonic-gate 		if (door_info(doorfd, &info) == 0 &&
10360Sstevel@tonic-gate 		    ((info.di_attributes & DOOR_REVOKED) == 0)) {
10370Sstevel@tonic-gate 			error = Z_OK;
10380Sstevel@tonic-gate 			(void) close(doorfd);
10390Sstevel@tonic-gate 			goto out;
10400Sstevel@tonic-gate 		}
10410Sstevel@tonic-gate 		(void) close(doorfd);
10420Sstevel@tonic-gate 	}
10430Sstevel@tonic-gate 
10440Sstevel@tonic-gate 	if ((child_pid = fork()) == -1) {
10450Sstevel@tonic-gate 		zperror(gettext("could not fork"), B_FALSE);
10460Sstevel@tonic-gate 		goto out;
10470Sstevel@tonic-gate 	} else if (child_pid == 0) {
1048766Scarlsonj 		const char *argv[6], **ap;
1049766Scarlsonj 
10500Sstevel@tonic-gate 		/* child process */
10510Sstevel@tonic-gate 		prepare_audit_context();
10520Sstevel@tonic-gate 
1053766Scarlsonj 		ap = argv;
1054766Scarlsonj 		*ap++ = "zoneadmd";
1055766Scarlsonj 		*ap++ = "-z";
1056766Scarlsonj 		*ap++ = zone_name;
1057766Scarlsonj 		if (zonecfg_in_alt_root()) {
1058766Scarlsonj 			*ap++ = "-R";
1059766Scarlsonj 			*ap++ = zonecfg_get_root();
1060766Scarlsonj 		}
1061766Scarlsonj 		*ap = NULL;
1062766Scarlsonj 
1063766Scarlsonj 		(void) execv("/usr/lib/zones/zoneadmd", (char * const *)argv);
1064*924Sgjelinek 		/*
1065*924Sgjelinek 		 * TRANSLATION_NOTE
1066*924Sgjelinek 		 * zoneadmd is a literal that should not be translated.
1067*924Sgjelinek 		 */
10680Sstevel@tonic-gate 		zperror(gettext("could not exec zoneadmd"), B_FALSE);
10690Sstevel@tonic-gate 		_exit(Z_ERR);
10700Sstevel@tonic-gate 	} else {
10710Sstevel@tonic-gate 		/* parent process */
10720Sstevel@tonic-gate 		pid_t retval;
10730Sstevel@tonic-gate 		int pstatus = 0;
10740Sstevel@tonic-gate 
10750Sstevel@tonic-gate 		do {
10760Sstevel@tonic-gate 			retval = waitpid(child_pid, &pstatus, 0);
10770Sstevel@tonic-gate 		} while (retval != child_pid);
10780Sstevel@tonic-gate 		if (WIFSIGNALED(pstatus) || (WIFEXITED(pstatus) &&
10790Sstevel@tonic-gate 		    WEXITSTATUS(pstatus) != 0)) {
10800Sstevel@tonic-gate 			zerror(gettext("could not start %s"), "zoneadmd");
10810Sstevel@tonic-gate 			goto out;
10820Sstevel@tonic-gate 		}
10830Sstevel@tonic-gate 	}
10840Sstevel@tonic-gate 	error = Z_OK;
10850Sstevel@tonic-gate out:
10860Sstevel@tonic-gate 	release_lock_file(lockfd);
10870Sstevel@tonic-gate 	return (error);
10880Sstevel@tonic-gate }
10890Sstevel@tonic-gate 
10900Sstevel@tonic-gate static int
10910Sstevel@tonic-gate ping_zoneadmd(const char *zone_name)
10920Sstevel@tonic-gate {
10930Sstevel@tonic-gate 	char doorpath[PATH_MAX];
10940Sstevel@tonic-gate 	int doorfd;
10950Sstevel@tonic-gate 	struct door_info info;
10960Sstevel@tonic-gate 
1097766Scarlsonj 	if (!get_doorname(zone_name, doorpath))
1098766Scarlsonj 		return (Z_ERR);
10990Sstevel@tonic-gate 
11000Sstevel@tonic-gate 	if ((doorfd = open(doorpath, O_RDONLY)) < 0) {
11010Sstevel@tonic-gate 		return (Z_ERR);
11020Sstevel@tonic-gate 	}
11030Sstevel@tonic-gate 	if (door_info(doorfd, &info) == 0 &&
11040Sstevel@tonic-gate 	    ((info.di_attributes & DOOR_REVOKED) == 0)) {
11050Sstevel@tonic-gate 		(void) close(doorfd);
11060Sstevel@tonic-gate 		return (Z_OK);
11070Sstevel@tonic-gate 	}
11080Sstevel@tonic-gate 	(void) close(doorfd);
11090Sstevel@tonic-gate 	return (Z_ERR);
11100Sstevel@tonic-gate }
11110Sstevel@tonic-gate 
11120Sstevel@tonic-gate static int
11130Sstevel@tonic-gate call_zoneadmd(const char *zone_name, zone_cmd_arg_t *arg)
11140Sstevel@tonic-gate {
11150Sstevel@tonic-gate 	char doorpath[PATH_MAX];
11160Sstevel@tonic-gate 	int doorfd, result;
11170Sstevel@tonic-gate 	door_arg_t darg;
11180Sstevel@tonic-gate 
11190Sstevel@tonic-gate 	zoneid_t zoneid;
11200Sstevel@tonic-gate 	uint64_t uniqid = 0;
11210Sstevel@tonic-gate 
11220Sstevel@tonic-gate 	zone_cmd_rval_t *rvalp;
11230Sstevel@tonic-gate 	size_t rlen;
11240Sstevel@tonic-gate 	char *cp, *errbuf;
11250Sstevel@tonic-gate 
11260Sstevel@tonic-gate 	rlen = getpagesize();
11270Sstevel@tonic-gate 	if ((rvalp = malloc(rlen)) == NULL) {
11280Sstevel@tonic-gate 		zerror(gettext("failed to allocate %lu bytes: %s"), rlen,
11290Sstevel@tonic-gate 		    strerror(errno));
11300Sstevel@tonic-gate 		return (-1);
11310Sstevel@tonic-gate 	}
11320Sstevel@tonic-gate 
11330Sstevel@tonic-gate 	if ((zoneid = getzoneidbyname(zone_name)) != ZONE_ID_UNDEFINED) {
11340Sstevel@tonic-gate 		(void) zone_getattr(zoneid, ZONE_ATTR_UNIQID, &uniqid,
11350Sstevel@tonic-gate 		    sizeof (uniqid));
11360Sstevel@tonic-gate 	}
11370Sstevel@tonic-gate 	arg->uniqid = uniqid;
11380Sstevel@tonic-gate 	(void) strlcpy(arg->locale, locale, sizeof (arg->locale));
1139766Scarlsonj 	if (!get_doorname(zone_name, doorpath)) {
1140766Scarlsonj 		zerror(gettext("alternate root path is too long"));
1141766Scarlsonj 		free(rvalp);
1142766Scarlsonj 		return (-1);
1143766Scarlsonj 	}
11440Sstevel@tonic-gate 
11450Sstevel@tonic-gate 	/*
11460Sstevel@tonic-gate 	 * Loop trying to start zoneadmd; if something goes seriously
11470Sstevel@tonic-gate 	 * wrong we break out and fail.
11480Sstevel@tonic-gate 	 */
11490Sstevel@tonic-gate 	for (;;) {
11500Sstevel@tonic-gate 		if (start_zoneadmd(zone_name) != Z_OK)
11510Sstevel@tonic-gate 			break;
11520Sstevel@tonic-gate 
11530Sstevel@tonic-gate 		if ((doorfd = open(doorpath, O_RDONLY)) < 0) {
11540Sstevel@tonic-gate 			zperror(gettext("failed to open zone door"), B_FALSE);
11550Sstevel@tonic-gate 			break;
11560Sstevel@tonic-gate 		}
11570Sstevel@tonic-gate 
11580Sstevel@tonic-gate 		darg.data_ptr = (char *)arg;
11590Sstevel@tonic-gate 		darg.data_size = sizeof (*arg);
11600Sstevel@tonic-gate 		darg.desc_ptr = NULL;
11610Sstevel@tonic-gate 		darg.desc_num = 0;
11620Sstevel@tonic-gate 		darg.rbuf = (char *)rvalp;
11630Sstevel@tonic-gate 		darg.rsize = rlen;
11640Sstevel@tonic-gate 		if (door_call(doorfd, &darg) != 0) {
11650Sstevel@tonic-gate 			(void) close(doorfd);
11660Sstevel@tonic-gate 			/*
11670Sstevel@tonic-gate 			 * We'll get EBADF if the door has been revoked.
11680Sstevel@tonic-gate 			 */
11690Sstevel@tonic-gate 			if (errno != EBADF) {
11700Sstevel@tonic-gate 				zperror(gettext("door_call failed"), B_FALSE);
11710Sstevel@tonic-gate 				break;
11720Sstevel@tonic-gate 			}
11730Sstevel@tonic-gate 			continue;	/* take another lap */
11740Sstevel@tonic-gate 		}
11750Sstevel@tonic-gate 		(void) close(doorfd);
11760Sstevel@tonic-gate 
11770Sstevel@tonic-gate 		if (darg.data_size == 0) {
11780Sstevel@tonic-gate 			/* Door server is going away; kick it again. */
11790Sstevel@tonic-gate 			continue;
11800Sstevel@tonic-gate 		}
11810Sstevel@tonic-gate 
11820Sstevel@tonic-gate 		errbuf = rvalp->errbuf;
11830Sstevel@tonic-gate 		while (*errbuf != '\0') {
11840Sstevel@tonic-gate 			/*
11850Sstevel@tonic-gate 			 * Remove any newlines since zerror()
11860Sstevel@tonic-gate 			 * will append one automatically.
11870Sstevel@tonic-gate 			 */
11880Sstevel@tonic-gate 			cp = strchr(errbuf, '\n');
11890Sstevel@tonic-gate 			if (cp != NULL)
11900Sstevel@tonic-gate 				*cp = '\0';
11910Sstevel@tonic-gate 			zerror("%s", errbuf);
11920Sstevel@tonic-gate 			if (cp == NULL)
11930Sstevel@tonic-gate 				break;
11940Sstevel@tonic-gate 			errbuf = cp + 1;
11950Sstevel@tonic-gate 		}
11960Sstevel@tonic-gate 		result = rvalp->rval == 0 ? 0 : -1;
11970Sstevel@tonic-gate 		free(rvalp);
11980Sstevel@tonic-gate 		return (result);
11990Sstevel@tonic-gate 	}
12000Sstevel@tonic-gate 
12010Sstevel@tonic-gate 	free(rvalp);
12020Sstevel@tonic-gate 	return (-1);
12030Sstevel@tonic-gate }
12040Sstevel@tonic-gate 
12050Sstevel@tonic-gate static int
12060Sstevel@tonic-gate ready_func(int argc, char *argv[])
12070Sstevel@tonic-gate {
12080Sstevel@tonic-gate 	zone_cmd_arg_t zarg;
12090Sstevel@tonic-gate 	int arg;
12100Sstevel@tonic-gate 
1211766Scarlsonj 	if (zonecfg_in_alt_root()) {
1212766Scarlsonj 		zerror(gettext("cannot ready zone in alternate root"));
1213766Scarlsonj 		return (Z_ERR);
1214766Scarlsonj 	}
1215766Scarlsonj 
12160Sstevel@tonic-gate 	optind = 0;
12170Sstevel@tonic-gate 	if ((arg = getopt(argc, argv, "?")) != EOF) {
12180Sstevel@tonic-gate 		switch (arg) {
12190Sstevel@tonic-gate 		case '?':
12200Sstevel@tonic-gate 			sub_usage(SHELP_READY, CMD_READY);
12210Sstevel@tonic-gate 			return (optopt == '?' ? Z_OK : Z_USAGE);
12220Sstevel@tonic-gate 		default:
12230Sstevel@tonic-gate 			sub_usage(SHELP_READY, CMD_READY);
12240Sstevel@tonic-gate 			return (Z_USAGE);
12250Sstevel@tonic-gate 		}
12260Sstevel@tonic-gate 	}
12270Sstevel@tonic-gate 	if (argc > optind) {
12280Sstevel@tonic-gate 		sub_usage(SHELP_READY, CMD_READY);
12290Sstevel@tonic-gate 		return (Z_USAGE);
12300Sstevel@tonic-gate 	}
12310Sstevel@tonic-gate 	if (sanity_check(target_zone, CMD_READY, B_FALSE, B_FALSE) != Z_OK)
12320Sstevel@tonic-gate 		return (Z_ERR);
12330Sstevel@tonic-gate 	if (verify_details(CMD_READY) != Z_OK)
12340Sstevel@tonic-gate 		return (Z_ERR);
12350Sstevel@tonic-gate 
12360Sstevel@tonic-gate 	zarg.cmd = Z_READY;
12370Sstevel@tonic-gate 	if (call_zoneadmd(target_zone, &zarg) != 0) {
12380Sstevel@tonic-gate 		zerror(gettext("call to %s failed"), "zoneadmd");
12390Sstevel@tonic-gate 		return (Z_ERR);
12400Sstevel@tonic-gate 	}
12410Sstevel@tonic-gate 	return (Z_OK);
12420Sstevel@tonic-gate }
12430Sstevel@tonic-gate 
12440Sstevel@tonic-gate static int
12450Sstevel@tonic-gate boot_func(int argc, char *argv[])
12460Sstevel@tonic-gate {
12470Sstevel@tonic-gate 	zone_cmd_arg_t zarg;
12480Sstevel@tonic-gate 	int arg;
12490Sstevel@tonic-gate 
1250766Scarlsonj 	if (zonecfg_in_alt_root()) {
1251766Scarlsonj 		zerror(gettext("cannot boot zone in alternate root"));
1252766Scarlsonj 		return (Z_ERR);
1253766Scarlsonj 	}
1254766Scarlsonj 
12550Sstevel@tonic-gate 	zarg.bootbuf[0] = '\0';
12560Sstevel@tonic-gate 
12570Sstevel@tonic-gate 	/*
12580Sstevel@tonic-gate 	 * At the current time, the only supported subargument to the
12590Sstevel@tonic-gate 	 * "boot" subcommand is "-s" which specifies a single-user boot.
12600Sstevel@tonic-gate 	 * In the future, other boot arguments should be supported
12610Sstevel@tonic-gate 	 * including "-m" for specifying alternate smf(5) milestones.
12620Sstevel@tonic-gate 	 */
12630Sstevel@tonic-gate 	optind = 0;
12640Sstevel@tonic-gate 	if ((arg = getopt(argc, argv, "?s")) != EOF) {
12650Sstevel@tonic-gate 		switch (arg) {
12660Sstevel@tonic-gate 		case '?':
12670Sstevel@tonic-gate 			sub_usage(SHELP_BOOT, CMD_BOOT);
12680Sstevel@tonic-gate 			return (optopt == '?' ? Z_OK : Z_USAGE);
12690Sstevel@tonic-gate 		case 's':
12700Sstevel@tonic-gate 			(void) strlcpy(zarg.bootbuf, "-s",
12710Sstevel@tonic-gate 			    sizeof (zarg.bootbuf));
12720Sstevel@tonic-gate 			break;
12730Sstevel@tonic-gate 		default:
12740Sstevel@tonic-gate 			sub_usage(SHELP_BOOT, CMD_BOOT);
12750Sstevel@tonic-gate 			return (Z_USAGE);
12760Sstevel@tonic-gate 		}
12770Sstevel@tonic-gate 	}
12780Sstevel@tonic-gate 	if (argc > optind) {
12790Sstevel@tonic-gate 		sub_usage(SHELP_BOOT, CMD_BOOT);
12800Sstevel@tonic-gate 		return (Z_USAGE);
12810Sstevel@tonic-gate 	}
12820Sstevel@tonic-gate 	if (sanity_check(target_zone, CMD_BOOT, B_FALSE, B_FALSE) != Z_OK)
12830Sstevel@tonic-gate 		return (Z_ERR);
12840Sstevel@tonic-gate 	if (verify_details(CMD_BOOT) != Z_OK)
12850Sstevel@tonic-gate 		return (Z_ERR);
12860Sstevel@tonic-gate 	zarg.cmd = Z_BOOT;
12870Sstevel@tonic-gate 	if (call_zoneadmd(target_zone, &zarg) != 0) {
12880Sstevel@tonic-gate 		zerror(gettext("call to %s failed"), "zoneadmd");
12890Sstevel@tonic-gate 		return (Z_ERR);
12900Sstevel@tonic-gate 	}
12910Sstevel@tonic-gate 	return (Z_OK);
12920Sstevel@tonic-gate }
12930Sstevel@tonic-gate 
12940Sstevel@tonic-gate static void
12950Sstevel@tonic-gate fake_up_local_zone(zoneid_t zid, zone_entry_t *zeptr)
12960Sstevel@tonic-gate {
12970Sstevel@tonic-gate 	ssize_t result;
12980Sstevel@tonic-gate 
12990Sstevel@tonic-gate 	zeptr->zid = zid;
13000Sstevel@tonic-gate 	/*
13010Sstevel@tonic-gate 	 * Since we're looking up our own (non-global) zone name,
13020Sstevel@tonic-gate 	 * we can be assured that it will succeed.
13030Sstevel@tonic-gate 	 */
13040Sstevel@tonic-gate 	result = getzonenamebyid(zid, zeptr->zname, sizeof (zeptr->zname));
13050Sstevel@tonic-gate 	assert(result >= 0);
13060Sstevel@tonic-gate 	(void) strlcpy(zeptr->zroot, "/", sizeof (zeptr->zroot));
13070Sstevel@tonic-gate 	zeptr->zstate_str = "running";
13080Sstevel@tonic-gate }
13090Sstevel@tonic-gate 
13100Sstevel@tonic-gate static int
13110Sstevel@tonic-gate list_func(int argc, char *argv[])
13120Sstevel@tonic-gate {
13130Sstevel@tonic-gate 	zone_entry_t *zentp, zent;
1314766Scarlsonj 	int arg, retv;
13150Sstevel@tonic-gate 	boolean_t output = B_FALSE, verbose = B_FALSE, parsable = B_FALSE;
13160Sstevel@tonic-gate 	zone_state_t min_state = ZONE_STATE_RUNNING;
13170Sstevel@tonic-gate 	zoneid_t zone_id = getzoneid();
13180Sstevel@tonic-gate 
13190Sstevel@tonic-gate 	if (target_zone == NULL) {
13200Sstevel@tonic-gate 		/* all zones: default view to running but allow override */
13210Sstevel@tonic-gate 		optind = 0;
13220Sstevel@tonic-gate 		while ((arg = getopt(argc, argv, "?cipv")) != EOF) {
13230Sstevel@tonic-gate 			switch (arg) {
13240Sstevel@tonic-gate 			case '?':
13250Sstevel@tonic-gate 				sub_usage(SHELP_LIST, CMD_LIST);
13260Sstevel@tonic-gate 				return (optopt == '?' ? Z_OK : Z_USAGE);
13270Sstevel@tonic-gate 			case 'c':
13280Sstevel@tonic-gate 				min_state = ZONE_STATE_CONFIGURED;
13290Sstevel@tonic-gate 				break;
13300Sstevel@tonic-gate 			case 'i':
13310Sstevel@tonic-gate 				min_state = ZONE_STATE_INSTALLED;
13320Sstevel@tonic-gate 				break;
13330Sstevel@tonic-gate 			case 'p':
13340Sstevel@tonic-gate 				parsable = B_TRUE;
13350Sstevel@tonic-gate 				break;
13360Sstevel@tonic-gate 			case 'v':
13370Sstevel@tonic-gate 				verbose = B_TRUE;
13380Sstevel@tonic-gate 				break;
13390Sstevel@tonic-gate 			default:
13400Sstevel@tonic-gate 				sub_usage(SHELP_LIST, CMD_LIST);
13410Sstevel@tonic-gate 				return (Z_USAGE);
13420Sstevel@tonic-gate 			}
13430Sstevel@tonic-gate 		}
13440Sstevel@tonic-gate 		if (parsable && verbose) {
13450Sstevel@tonic-gate 			zerror(gettext("%s -p and -v are mutually exclusive."),
13460Sstevel@tonic-gate 			    cmd_to_str(CMD_LIST));
13470Sstevel@tonic-gate 			return (Z_ERR);
13480Sstevel@tonic-gate 		}
13490Sstevel@tonic-gate 		if (zone_id == GLOBAL_ZONEID) {
1350766Scarlsonj 			retv = zone_print_list(min_state, verbose, parsable);
13510Sstevel@tonic-gate 		} else {
1352766Scarlsonj 			retv = Z_OK;
13530Sstevel@tonic-gate 			fake_up_local_zone(zone_id, &zent);
13540Sstevel@tonic-gate 			zone_print(&zent, verbose, parsable);
13550Sstevel@tonic-gate 		}
1356766Scarlsonj 		return (retv);
13570Sstevel@tonic-gate 	}
13580Sstevel@tonic-gate 
13590Sstevel@tonic-gate 	/*
13600Sstevel@tonic-gate 	 * Specific target zone: disallow -i/-c suboptions.
13610Sstevel@tonic-gate 	 */
13620Sstevel@tonic-gate 	optind = 0;
13630Sstevel@tonic-gate 	while ((arg = getopt(argc, argv, "?pv")) != EOF) {
13640Sstevel@tonic-gate 		switch (arg) {
13650Sstevel@tonic-gate 		case '?':
13660Sstevel@tonic-gate 			sub_usage(SHELP_LIST, CMD_LIST);
13670Sstevel@tonic-gate 			return (optopt == '?' ? Z_OK : Z_USAGE);
13680Sstevel@tonic-gate 		case 'p':
13690Sstevel@tonic-gate 			parsable = B_TRUE;
13700Sstevel@tonic-gate 			break;
13710Sstevel@tonic-gate 		case 'v':
13720Sstevel@tonic-gate 			verbose = B_TRUE;
13730Sstevel@tonic-gate 			break;
13740Sstevel@tonic-gate 		default:
13750Sstevel@tonic-gate 			sub_usage(SHELP_LIST, CMD_LIST);
13760Sstevel@tonic-gate 			return (Z_USAGE);
13770Sstevel@tonic-gate 		}
13780Sstevel@tonic-gate 	}
13790Sstevel@tonic-gate 	if (parsable && verbose) {
13800Sstevel@tonic-gate 		zerror(gettext("%s -p and -v are mutually exclusive."),
13810Sstevel@tonic-gate 		    cmd_to_str(CMD_LIST));
13820Sstevel@tonic-gate 		return (Z_ERR);
13830Sstevel@tonic-gate 	}
13840Sstevel@tonic-gate 	if (argc > optind) {
13850Sstevel@tonic-gate 		sub_usage(SHELP_LIST, CMD_LIST);
13860Sstevel@tonic-gate 		return (Z_USAGE);
13870Sstevel@tonic-gate 	}
13880Sstevel@tonic-gate 	if (zone_id != GLOBAL_ZONEID) {
13890Sstevel@tonic-gate 		fake_up_local_zone(zone_id, &zent);
13900Sstevel@tonic-gate 		/*
13910Sstevel@tonic-gate 		 * main() will issue a Z_NO_ZONE error if it cannot get an
13920Sstevel@tonic-gate 		 * id for target_zone, which in a non-global zone should
13930Sstevel@tonic-gate 		 * happen for any zone name except `zonename`.  Thus we
13940Sstevel@tonic-gate 		 * assert() that here but don't otherwise check.
13950Sstevel@tonic-gate 		 */
13960Sstevel@tonic-gate 		assert(strcmp(zent.zname, target_zone) == 0);
13970Sstevel@tonic-gate 		zone_print(&zent, verbose, parsable);
13980Sstevel@tonic-gate 		output = B_TRUE;
13990Sstevel@tonic-gate 	} else if ((zentp = lookup_running_zone(target_zone)) != NULL) {
14000Sstevel@tonic-gate 		zone_print(zentp, verbose, parsable);
14010Sstevel@tonic-gate 		output = B_TRUE;
1402766Scarlsonj 	} else if (lookup_zone_info(target_zone, ZONE_ID_UNDEFINED,
1403766Scarlsonj 	    &zent) == Z_OK) {
14040Sstevel@tonic-gate 		zone_print(&zent, verbose, parsable);
14050Sstevel@tonic-gate 		output = B_TRUE;
14060Sstevel@tonic-gate 	}
14070Sstevel@tonic-gate 	return (output ? Z_OK : Z_ERR);
14080Sstevel@tonic-gate }
14090Sstevel@tonic-gate 
14100Sstevel@tonic-gate static void
14110Sstevel@tonic-gate sigterm(int sig)
14120Sstevel@tonic-gate {
14130Sstevel@tonic-gate 	/*
14140Sstevel@tonic-gate 	 * Ignore SIG{INT,TERM}, so we don't end up in an infinite loop,
14150Sstevel@tonic-gate 	 * then propagate the signal to our process group.
14160Sstevel@tonic-gate 	 */
14170Sstevel@tonic-gate 	(void) sigset(SIGINT, SIG_IGN);
14180Sstevel@tonic-gate 	(void) sigset(SIGTERM, SIG_IGN);
14190Sstevel@tonic-gate 	(void) kill(0, sig);
14200Sstevel@tonic-gate 	child_killed = B_TRUE;
14210Sstevel@tonic-gate }
14220Sstevel@tonic-gate 
14230Sstevel@tonic-gate static int
14240Sstevel@tonic-gate do_subproc(char *cmdbuf)
14250Sstevel@tonic-gate {
14260Sstevel@tonic-gate 	char inbuf[1024];	/* arbitrary large amount */
14270Sstevel@tonic-gate 	FILE *file;
14280Sstevel@tonic-gate 
14290Sstevel@tonic-gate 	child_killed = B_FALSE;
14300Sstevel@tonic-gate 	/*
14310Sstevel@tonic-gate 	 * We use popen(3c) to launch child processes for [un]install;
14320Sstevel@tonic-gate 	 * this library call does not return a PID, so we have to kill
14330Sstevel@tonic-gate 	 * the whole process group.  To avoid killing our parent, we
14340Sstevel@tonic-gate 	 * become a process group leader here.  But doing so can wreak
14350Sstevel@tonic-gate 	 * havoc with reading from stdin when launched by a non-job-control
14360Sstevel@tonic-gate 	 * shell, so we close stdin and reopen it as /dev/null first.
14370Sstevel@tonic-gate 	 */
14380Sstevel@tonic-gate 	(void) close(STDIN_FILENO);
14390Sstevel@tonic-gate 	(void) open("/dev/null", O_RDONLY);
14400Sstevel@tonic-gate 	(void) setpgid(0, 0);
14410Sstevel@tonic-gate 	(void) sigset(SIGINT, sigterm);
14420Sstevel@tonic-gate 	(void) sigset(SIGTERM, sigterm);
14430Sstevel@tonic-gate 	file = popen(cmdbuf, "r");
14440Sstevel@tonic-gate 	for (;;) {
14450Sstevel@tonic-gate 		if (child_killed || fgets(inbuf, sizeof (inbuf), file) == NULL)
14460Sstevel@tonic-gate 			break;
14470Sstevel@tonic-gate 		(void) fputs(inbuf, stdout);
14480Sstevel@tonic-gate 	}
14490Sstevel@tonic-gate 	(void) sigset(SIGINT, SIG_DFL);
14500Sstevel@tonic-gate 	(void) sigset(SIGTERM, SIG_DFL);
14510Sstevel@tonic-gate 	return (pclose(file));
14520Sstevel@tonic-gate }
14530Sstevel@tonic-gate 
14540Sstevel@tonic-gate static int
14550Sstevel@tonic-gate subproc_status(const char *cmd, int status)
14560Sstevel@tonic-gate {
14570Sstevel@tonic-gate 	if (WIFEXITED(status)) {
14580Sstevel@tonic-gate 		int exit_code = WEXITSTATUS(status);
14590Sstevel@tonic-gate 
14600Sstevel@tonic-gate 		if (exit_code == 0)
14610Sstevel@tonic-gate 			return (Z_OK);
14620Sstevel@tonic-gate 		zerror(gettext("'%s' failed with exit code %d."), cmd,
14630Sstevel@tonic-gate 		    exit_code);
14640Sstevel@tonic-gate 	} else if (WIFSIGNALED(status)) {
14650Sstevel@tonic-gate 		int signal = WTERMSIG(status);
14660Sstevel@tonic-gate 		char sigstr[SIG2STR_MAX];
14670Sstevel@tonic-gate 
14680Sstevel@tonic-gate 		if (sig2str(signal, sigstr) == 0) {
14690Sstevel@tonic-gate 			zerror(gettext("'%s' terminated by signal SIG%s."), cmd,
14700Sstevel@tonic-gate 			    sigstr);
14710Sstevel@tonic-gate 		} else {
14720Sstevel@tonic-gate 			zerror(gettext("'%s' terminated by an unknown signal."),
14730Sstevel@tonic-gate 			    cmd);
14740Sstevel@tonic-gate 		}
14750Sstevel@tonic-gate 	} else {
14760Sstevel@tonic-gate 		zerror(gettext("'%s' failed for unknown reasons."), cmd);
14770Sstevel@tonic-gate 	}
14780Sstevel@tonic-gate 	return (Z_ERR);
14790Sstevel@tonic-gate }
14800Sstevel@tonic-gate 
14810Sstevel@tonic-gate /*
14820Sstevel@tonic-gate  * Various sanity checks; make sure:
14830Sstevel@tonic-gate  * 1. We're in the global zone.
14840Sstevel@tonic-gate  * 2. The calling user has sufficient privilege.
14850Sstevel@tonic-gate  * 3. The target zone is neither the global zone nor anything starting with
14860Sstevel@tonic-gate  *    "SUNW".
14870Sstevel@tonic-gate  * 4a. If we're looking for a 'not running' (i.e., configured or installed)
14880Sstevel@tonic-gate  *     zone, the name service knows about it.
14890Sstevel@tonic-gate  * 4b. For some operations which expect a zone not to be running, that it is
14900Sstevel@tonic-gate  *     not already running (or ready).
14910Sstevel@tonic-gate  */
14920Sstevel@tonic-gate static int
14930Sstevel@tonic-gate sanity_check(char *zone, int cmd_num, boolean_t running,
14940Sstevel@tonic-gate     boolean_t unsafe_when_running)
14950Sstevel@tonic-gate {
14960Sstevel@tonic-gate 	zone_entry_t *zent;
14970Sstevel@tonic-gate 	priv_set_t *privset;
14980Sstevel@tonic-gate 	zone_state_t state;
1499766Scarlsonj 	char kernzone[ZONENAME_MAX];
1500766Scarlsonj 	FILE *fp;
15010Sstevel@tonic-gate 
15020Sstevel@tonic-gate 	if (getzoneid() != GLOBAL_ZONEID) {
15030Sstevel@tonic-gate 		zerror(gettext("must be in the global zone to %s a zone."),
15040Sstevel@tonic-gate 		    cmd_to_str(cmd_num));
15050Sstevel@tonic-gate 		return (Z_ERR);
15060Sstevel@tonic-gate 	}
15070Sstevel@tonic-gate 
15080Sstevel@tonic-gate 	if ((privset = priv_allocset()) == NULL) {
15090Sstevel@tonic-gate 		zerror(gettext("%s failed"), "priv_allocset");
15100Sstevel@tonic-gate 		return (Z_ERR);
15110Sstevel@tonic-gate 	}
15120Sstevel@tonic-gate 
15130Sstevel@tonic-gate 	if (getppriv(PRIV_EFFECTIVE, privset) != 0) {
15140Sstevel@tonic-gate 		zerror(gettext("%s failed"), "getppriv");
15150Sstevel@tonic-gate 		priv_freeset(privset);
15160Sstevel@tonic-gate 		return (Z_ERR);
15170Sstevel@tonic-gate 	}
15180Sstevel@tonic-gate 
15190Sstevel@tonic-gate 	if (priv_isfullset(privset) == B_FALSE) {
15200Sstevel@tonic-gate 		zerror(gettext("only a privileged user may %s a zone."),
15210Sstevel@tonic-gate 		    cmd_to_str(cmd_num));
15220Sstevel@tonic-gate 		priv_freeset(privset);
15230Sstevel@tonic-gate 		return (Z_ERR);
15240Sstevel@tonic-gate 	}
15250Sstevel@tonic-gate 	priv_freeset(privset);
15260Sstevel@tonic-gate 
15270Sstevel@tonic-gate 	if (zone == NULL) {
15280Sstevel@tonic-gate 		zerror(gettext("no zone specified"));
15290Sstevel@tonic-gate 		return (Z_ERR);
15300Sstevel@tonic-gate 	}
15310Sstevel@tonic-gate 
15320Sstevel@tonic-gate 	if (strcmp(zone, GLOBAL_ZONENAME) == 0) {
15330Sstevel@tonic-gate 		zerror(gettext("%s operation is invalid for the global zone."),
15340Sstevel@tonic-gate 		    cmd_to_str(cmd_num));
15350Sstevel@tonic-gate 		return (Z_ERR);
15360Sstevel@tonic-gate 	}
15370Sstevel@tonic-gate 
15380Sstevel@tonic-gate 	if (strncmp(zone, "SUNW", 4) == 0) {
15390Sstevel@tonic-gate 		zerror(gettext("%s operation is invalid for zones starting "
15400Sstevel@tonic-gate 		    "with SUNW."), cmd_to_str(cmd_num));
15410Sstevel@tonic-gate 		return (Z_ERR);
15420Sstevel@tonic-gate 	}
15430Sstevel@tonic-gate 
1544766Scarlsonj 	if (!zonecfg_in_alt_root()) {
1545766Scarlsonj 		zent = lookup_running_zone(zone);
1546766Scarlsonj 	} else if ((fp = zonecfg_open_scratch("", B_FALSE)) == NULL) {
1547766Scarlsonj 		zent = NULL;
1548766Scarlsonj 	} else {
1549766Scarlsonj 		if (zonecfg_find_scratch(fp, zone, zonecfg_get_root(),
1550766Scarlsonj 		    kernzone, sizeof (kernzone)) == 0)
1551766Scarlsonj 			zent = lookup_running_zone(kernzone);
1552766Scarlsonj 		else
1553766Scarlsonj 			zent = NULL;
1554766Scarlsonj 		zonecfg_close_scratch(fp);
1555766Scarlsonj 	}
1556766Scarlsonj 
15570Sstevel@tonic-gate 	/*
15580Sstevel@tonic-gate 	 * Look up from the kernel for 'running' zones.
15590Sstevel@tonic-gate 	 */
15600Sstevel@tonic-gate 	if (running) {
15610Sstevel@tonic-gate 		if (zent == NULL) {
15620Sstevel@tonic-gate 			zerror(gettext("not running"));
15630Sstevel@tonic-gate 			return (Z_ERR);
15640Sstevel@tonic-gate 		}
15650Sstevel@tonic-gate 	} else {
15660Sstevel@tonic-gate 		int err;
15670Sstevel@tonic-gate 
15680Sstevel@tonic-gate 		if (unsafe_when_running && zent != NULL) {
15690Sstevel@tonic-gate 			/* check whether the zone is ready or running */
15700Sstevel@tonic-gate 			if ((err = zone_get_state(zent->zname,
15710Sstevel@tonic-gate 			    &zent->zstate_num)) != Z_OK) {
15720Sstevel@tonic-gate 				errno = err;
15730Sstevel@tonic-gate 				zperror2(zent->zname,
15740Sstevel@tonic-gate 				    gettext("could not get state"));
15750Sstevel@tonic-gate 				/* can't tell, so hedge */
15760Sstevel@tonic-gate 				zent->zstate_str = "ready/running";
15770Sstevel@tonic-gate 			} else {
15780Sstevel@tonic-gate 				zent->zstate_str =
15790Sstevel@tonic-gate 				    zone_state_str(zent->zstate_num);
15800Sstevel@tonic-gate 			}
15810Sstevel@tonic-gate 			zerror(gettext("%s operation is invalid for %s zones."),
15820Sstevel@tonic-gate 			    cmd_to_str(cmd_num), zent->zstate_str);
15830Sstevel@tonic-gate 			return (Z_ERR);
15840Sstevel@tonic-gate 		}
15850Sstevel@tonic-gate 		if ((err = zone_get_state(zone, &state)) != Z_OK) {
15860Sstevel@tonic-gate 			errno = err;
15870Sstevel@tonic-gate 			zperror2(zone, gettext("could not get state"));
15880Sstevel@tonic-gate 			return (Z_ERR);
15890Sstevel@tonic-gate 		}
15900Sstevel@tonic-gate 		switch (cmd_num) {
15910Sstevel@tonic-gate 		case CMD_UNINSTALL:
15920Sstevel@tonic-gate 			if (state == ZONE_STATE_CONFIGURED) {
15930Sstevel@tonic-gate 				zerror(gettext("is already in state '%s'."),
15940Sstevel@tonic-gate 				    zone_state_str(ZONE_STATE_CONFIGURED));
15950Sstevel@tonic-gate 				return (Z_ERR);
15960Sstevel@tonic-gate 			}
15970Sstevel@tonic-gate 			break;
15980Sstevel@tonic-gate 		case CMD_INSTALL:
15990Sstevel@tonic-gate 			if (state == ZONE_STATE_INSTALLED) {
16000Sstevel@tonic-gate 				zerror(gettext("is already %s."),
16010Sstevel@tonic-gate 				    zone_state_str(ZONE_STATE_INSTALLED));
16020Sstevel@tonic-gate 				return (Z_ERR);
16030Sstevel@tonic-gate 			} else if (state == ZONE_STATE_INCOMPLETE) {
16040Sstevel@tonic-gate 				zerror(gettext("zone is %s; %s required."),
16050Sstevel@tonic-gate 				    zone_state_str(ZONE_STATE_INCOMPLETE),
16060Sstevel@tonic-gate 				    cmd_to_str(CMD_UNINSTALL));
16070Sstevel@tonic-gate 				return (Z_ERR);
16080Sstevel@tonic-gate 			}
16090Sstevel@tonic-gate 			break;
16100Sstevel@tonic-gate 		case CMD_READY:
16110Sstevel@tonic-gate 		case CMD_BOOT:
1612766Scarlsonj 		case CMD_MOUNT:
16130Sstevel@tonic-gate 			if (state < ZONE_STATE_INSTALLED) {
16140Sstevel@tonic-gate 				zerror(gettext("must be %s before %s."),
16150Sstevel@tonic-gate 				    zone_state_str(ZONE_STATE_INSTALLED),
16160Sstevel@tonic-gate 				    cmd_to_str(cmd_num));
16170Sstevel@tonic-gate 				return (Z_ERR);
16180Sstevel@tonic-gate 			}
16190Sstevel@tonic-gate 			break;
16200Sstevel@tonic-gate 		case CMD_VERIFY:
16210Sstevel@tonic-gate 			if (state == ZONE_STATE_INCOMPLETE) {
16220Sstevel@tonic-gate 				zerror(gettext("zone is %s; %s required."),
16230Sstevel@tonic-gate 				    zone_state_str(ZONE_STATE_INCOMPLETE),
16240Sstevel@tonic-gate 				    cmd_to_str(CMD_UNINSTALL));
16250Sstevel@tonic-gate 				return (Z_ERR);
16260Sstevel@tonic-gate 			}
16270Sstevel@tonic-gate 			break;
1628766Scarlsonj 		case CMD_UNMOUNT:
1629766Scarlsonj 			if (state != ZONE_STATE_MOUNTED) {
1630766Scarlsonj 				zerror(gettext("must be %s before %s."),
1631766Scarlsonj 				    zone_state_str(ZONE_STATE_MOUNTED),
1632766Scarlsonj 				    cmd_to_str(cmd_num));
1633766Scarlsonj 				return (Z_ERR);
1634766Scarlsonj 			}
1635766Scarlsonj 			break;
16360Sstevel@tonic-gate 		}
16370Sstevel@tonic-gate 	}
16380Sstevel@tonic-gate 	return (Z_OK);
16390Sstevel@tonic-gate }
16400Sstevel@tonic-gate 
16410Sstevel@tonic-gate static int
16420Sstevel@tonic-gate halt_func(int argc, char *argv[])
16430Sstevel@tonic-gate {
16440Sstevel@tonic-gate 	zone_cmd_arg_t zarg;
16450Sstevel@tonic-gate 	int arg;
16460Sstevel@tonic-gate 
1647766Scarlsonj 	if (zonecfg_in_alt_root()) {
1648766Scarlsonj 		zerror(gettext("cannot halt zone in alternate root"));
1649766Scarlsonj 		return (Z_ERR);
1650766Scarlsonj 	}
1651766Scarlsonj 
16520Sstevel@tonic-gate 	optind = 0;
16530Sstevel@tonic-gate 	if ((arg = getopt(argc, argv, "?")) != EOF) {
16540Sstevel@tonic-gate 		switch (arg) {
16550Sstevel@tonic-gate 		case '?':
16560Sstevel@tonic-gate 			sub_usage(SHELP_HALT, CMD_HALT);
16570Sstevel@tonic-gate 			return (optopt == '?' ? Z_OK : Z_USAGE);
16580Sstevel@tonic-gate 		default:
16590Sstevel@tonic-gate 			sub_usage(SHELP_HALT, CMD_HALT);
16600Sstevel@tonic-gate 			return (Z_USAGE);
16610Sstevel@tonic-gate 		}
16620Sstevel@tonic-gate 	}
16630Sstevel@tonic-gate 	if (argc > optind) {
16640Sstevel@tonic-gate 		sub_usage(SHELP_HALT, CMD_HALT);
16650Sstevel@tonic-gate 		return (Z_USAGE);
16660Sstevel@tonic-gate 	}
16670Sstevel@tonic-gate 	/*
16680Sstevel@tonic-gate 	 * zoneadmd should be the one to decide whether or not to proceed,
16690Sstevel@tonic-gate 	 * so even though it seems that the fourth parameter below should
16700Sstevel@tonic-gate 	 * perhaps be B_TRUE, it really shouldn't be.
16710Sstevel@tonic-gate 	 */
16720Sstevel@tonic-gate 	if (sanity_check(target_zone, CMD_HALT, B_FALSE, B_FALSE) != Z_OK)
16730Sstevel@tonic-gate 		return (Z_ERR);
16740Sstevel@tonic-gate 
16750Sstevel@tonic-gate 	zarg.cmd = Z_HALT;
16760Sstevel@tonic-gate 	return ((call_zoneadmd(target_zone, &zarg) == 0) ? Z_OK : Z_ERR);
16770Sstevel@tonic-gate }
16780Sstevel@tonic-gate 
16790Sstevel@tonic-gate static int
16800Sstevel@tonic-gate reboot_func(int argc, char *argv[])
16810Sstevel@tonic-gate {
16820Sstevel@tonic-gate 	zone_cmd_arg_t zarg;
16830Sstevel@tonic-gate 	int arg;
16840Sstevel@tonic-gate 
1685766Scarlsonj 	if (zonecfg_in_alt_root()) {
1686766Scarlsonj 		zerror(gettext("cannot reboot zone in alternate root"));
1687766Scarlsonj 		return (Z_ERR);
1688766Scarlsonj 	}
1689766Scarlsonj 
16900Sstevel@tonic-gate 	optind = 0;
16910Sstevel@tonic-gate 	if ((arg = getopt(argc, argv, "?")) != EOF) {
16920Sstevel@tonic-gate 		switch (arg) {
16930Sstevel@tonic-gate 		case '?':
16940Sstevel@tonic-gate 			sub_usage(SHELP_REBOOT, CMD_REBOOT);
16950Sstevel@tonic-gate 			return (optopt == '?' ? Z_OK : Z_USAGE);
16960Sstevel@tonic-gate 		default:
16970Sstevel@tonic-gate 			sub_usage(SHELP_REBOOT, CMD_REBOOT);
16980Sstevel@tonic-gate 			return (Z_USAGE);
16990Sstevel@tonic-gate 		}
17000Sstevel@tonic-gate 	}
17010Sstevel@tonic-gate 	if (argc > 0) {
17020Sstevel@tonic-gate 		sub_usage(SHELP_REBOOT, CMD_REBOOT);
17030Sstevel@tonic-gate 		return (Z_USAGE);
17040Sstevel@tonic-gate 	}
17050Sstevel@tonic-gate 	/*
17060Sstevel@tonic-gate 	 * zoneadmd should be the one to decide whether or not to proceed,
17070Sstevel@tonic-gate 	 * so even though it seems that the fourth parameter below should
17080Sstevel@tonic-gate 	 * perhaps be B_TRUE, it really shouldn't be.
17090Sstevel@tonic-gate 	 */
17100Sstevel@tonic-gate 	if (sanity_check(target_zone, CMD_REBOOT, B_TRUE, B_FALSE) != Z_OK)
17110Sstevel@tonic-gate 		return (Z_ERR);
1712823Sgjelinek 	if (verify_details(CMD_REBOOT) != Z_OK)
1713823Sgjelinek 		return (Z_ERR);
17140Sstevel@tonic-gate 
17150Sstevel@tonic-gate 	zarg.cmd = Z_REBOOT;
17160Sstevel@tonic-gate 	return ((call_zoneadmd(target_zone, &zarg) == 0) ? Z_OK : Z_ERR);
17170Sstevel@tonic-gate }
17180Sstevel@tonic-gate 
17190Sstevel@tonic-gate static int
17200Sstevel@tonic-gate verify_rctls(zone_dochandle_t handle)
17210Sstevel@tonic-gate {
17220Sstevel@tonic-gate 	struct zone_rctltab rctltab;
17230Sstevel@tonic-gate 	size_t rbs = rctlblk_size();
17240Sstevel@tonic-gate 	rctlblk_t *rctlblk;
17250Sstevel@tonic-gate 	int error = Z_INVAL;
17260Sstevel@tonic-gate 
17270Sstevel@tonic-gate 	if ((rctlblk = malloc(rbs)) == NULL) {
17280Sstevel@tonic-gate 		zerror(gettext("failed to allocate %lu bytes: %s"), rbs,
17290Sstevel@tonic-gate 		    strerror(errno));
17300Sstevel@tonic-gate 		return (Z_NOMEM);
17310Sstevel@tonic-gate 	}
17320Sstevel@tonic-gate 
17330Sstevel@tonic-gate 	if (zonecfg_setrctlent(handle) != Z_OK) {
17340Sstevel@tonic-gate 		zerror(gettext("zonecfg_setrctlent failed"));
17350Sstevel@tonic-gate 		free(rctlblk);
17360Sstevel@tonic-gate 		return (error);
17370Sstevel@tonic-gate 	}
17380Sstevel@tonic-gate 
17390Sstevel@tonic-gate 	rctltab.zone_rctl_valptr = NULL;
17400Sstevel@tonic-gate 	while (zonecfg_getrctlent(handle, &rctltab) == Z_OK) {
17410Sstevel@tonic-gate 		struct zone_rctlvaltab *rctlval;
17420Sstevel@tonic-gate 		const char *name = rctltab.zone_rctl_name;
17430Sstevel@tonic-gate 
17440Sstevel@tonic-gate 		if (!zonecfg_is_rctl(name)) {
17450Sstevel@tonic-gate 			zerror(gettext("WARNING: Ignoring unrecognized rctl "
17460Sstevel@tonic-gate 			    "'%s'."),  name);
17470Sstevel@tonic-gate 			zonecfg_free_rctl_value_list(rctltab.zone_rctl_valptr);
17480Sstevel@tonic-gate 			rctltab.zone_rctl_valptr = NULL;
17490Sstevel@tonic-gate 			continue;
17500Sstevel@tonic-gate 		}
17510Sstevel@tonic-gate 
17520Sstevel@tonic-gate 		for (rctlval = rctltab.zone_rctl_valptr; rctlval != NULL;
17530Sstevel@tonic-gate 		    rctlval = rctlval->zone_rctlval_next) {
17540Sstevel@tonic-gate 			if (zonecfg_construct_rctlblk(rctlval, rctlblk)
17550Sstevel@tonic-gate 			    != Z_OK) {
17560Sstevel@tonic-gate 				zerror(gettext("invalid rctl value: "
17570Sstevel@tonic-gate 				    "(priv=%s,limit=%s,action%s)"),
17580Sstevel@tonic-gate 				    rctlval->zone_rctlval_priv,
17590Sstevel@tonic-gate 				    rctlval->zone_rctlval_limit,
17600Sstevel@tonic-gate 				    rctlval->zone_rctlval_action);
17610Sstevel@tonic-gate 				goto out;
17620Sstevel@tonic-gate 			}
17630Sstevel@tonic-gate 			if (!zonecfg_valid_rctl(name, rctlblk)) {
17640Sstevel@tonic-gate 				zerror(gettext("(priv=%s,limit=%s,action=%s) "
17650Sstevel@tonic-gate 				    "is not a valid value for rctl '%s'"),
17660Sstevel@tonic-gate 				    rctlval->zone_rctlval_priv,
17670Sstevel@tonic-gate 				    rctlval->zone_rctlval_limit,
17680Sstevel@tonic-gate 				    rctlval->zone_rctlval_action,
17690Sstevel@tonic-gate 				    name);
17700Sstevel@tonic-gate 				goto out;
17710Sstevel@tonic-gate 			}
17720Sstevel@tonic-gate 		}
17730Sstevel@tonic-gate 		zonecfg_free_rctl_value_list(rctltab.zone_rctl_valptr);
17740Sstevel@tonic-gate 	}
17750Sstevel@tonic-gate 	rctltab.zone_rctl_valptr = NULL;
17760Sstevel@tonic-gate 	error = Z_OK;
17770Sstevel@tonic-gate out:
17780Sstevel@tonic-gate 	zonecfg_free_rctl_value_list(rctltab.zone_rctl_valptr);
17790Sstevel@tonic-gate 	(void) zonecfg_endrctlent(handle);
17800Sstevel@tonic-gate 	free(rctlblk);
17810Sstevel@tonic-gate 	return (error);
17820Sstevel@tonic-gate }
17830Sstevel@tonic-gate 
17840Sstevel@tonic-gate static int
17850Sstevel@tonic-gate verify_pool(zone_dochandle_t handle)
17860Sstevel@tonic-gate {
17870Sstevel@tonic-gate 	char poolname[MAXPATHLEN];
17880Sstevel@tonic-gate 	pool_conf_t *poolconf;
17890Sstevel@tonic-gate 	pool_t *pool;
17900Sstevel@tonic-gate 	int status;
17910Sstevel@tonic-gate 	int error;
17920Sstevel@tonic-gate 
17930Sstevel@tonic-gate 	/*
17940Sstevel@tonic-gate 	 * This ends up being very similar to the check done in zoneadmd.
17950Sstevel@tonic-gate 	 */
17960Sstevel@tonic-gate 	error = zonecfg_get_pool(handle, poolname, sizeof (poolname));
17970Sstevel@tonic-gate 	if (error == Z_NO_ENTRY || (error == Z_OK && strlen(poolname) == 0)) {
17980Sstevel@tonic-gate 		/*
17990Sstevel@tonic-gate 		 * No pool specified.
18000Sstevel@tonic-gate 		 */
18010Sstevel@tonic-gate 		return (0);
18020Sstevel@tonic-gate 	}
18030Sstevel@tonic-gate 	if (error != Z_OK) {
18040Sstevel@tonic-gate 		zperror(gettext("Unable to retrieve pool name from "
18050Sstevel@tonic-gate 		    "configuration"), B_TRUE);
18060Sstevel@tonic-gate 		return (error);
18070Sstevel@tonic-gate 	}
18080Sstevel@tonic-gate 	/*
18090Sstevel@tonic-gate 	 * Don't do anything if pools aren't enabled.
18100Sstevel@tonic-gate 	 */
18110Sstevel@tonic-gate 	if (pool_get_status(&status) != PO_SUCCESS || status != POOL_ENABLED) {
18120Sstevel@tonic-gate 		zerror(gettext("WARNING: pools facility not active; "
18130Sstevel@tonic-gate 		    "zone will not be bound to pool '%s'."), poolname);
18140Sstevel@tonic-gate 		return (Z_OK);
18150Sstevel@tonic-gate 	}
18160Sstevel@tonic-gate 	/*
18170Sstevel@tonic-gate 	 * Try to provide a sane error message if the requested pool doesn't
18180Sstevel@tonic-gate 	 * exist.  It isn't clear that pools-related failures should
18190Sstevel@tonic-gate 	 * necessarily translate to a failure to verify the zone configuration,
18200Sstevel@tonic-gate 	 * hence they are not considered errors.
18210Sstevel@tonic-gate 	 */
18220Sstevel@tonic-gate 	if ((poolconf = pool_conf_alloc()) == NULL) {
18230Sstevel@tonic-gate 		zerror(gettext("WARNING: pool_conf_alloc failed; "
18240Sstevel@tonic-gate 		    "using default pool"));
18250Sstevel@tonic-gate 		return (Z_OK);
18260Sstevel@tonic-gate 	}
18270Sstevel@tonic-gate 	if (pool_conf_open(poolconf, pool_dynamic_location(), PO_RDONLY) !=
18280Sstevel@tonic-gate 	    PO_SUCCESS) {
18290Sstevel@tonic-gate 		zerror(gettext("WARNING: pool_conf_open failed; "
18300Sstevel@tonic-gate 		    "using default pool"));
18310Sstevel@tonic-gate 		pool_conf_free(poolconf);
18320Sstevel@tonic-gate 		return (Z_OK);
18330Sstevel@tonic-gate 	}
18340Sstevel@tonic-gate 	pool = pool_get_pool(poolconf, poolname);
18350Sstevel@tonic-gate 	(void) pool_conf_close(poolconf);
18360Sstevel@tonic-gate 	pool_conf_free(poolconf);
18370Sstevel@tonic-gate 	if (pool == NULL) {
18380Sstevel@tonic-gate 		zerror(gettext("WARNING: pool '%s' not found. "
18390Sstevel@tonic-gate 		    "using default pool"), poolname);
18400Sstevel@tonic-gate 	}
18410Sstevel@tonic-gate 
18420Sstevel@tonic-gate 	return (Z_OK);
18430Sstevel@tonic-gate }
18440Sstevel@tonic-gate 
18450Sstevel@tonic-gate static int
1846823Sgjelinek verify_ipd(zone_dochandle_t handle)
1847823Sgjelinek {
1848823Sgjelinek 	int return_code = Z_OK;
1849823Sgjelinek 	struct zone_fstab fstab;
1850823Sgjelinek 	struct stat st;
1851823Sgjelinek 	char specdir[MAXPATHLEN];
1852823Sgjelinek 
1853823Sgjelinek 	if (zonecfg_setipdent(handle) != Z_OK) {
1854*924Sgjelinek 		/*
1855*924Sgjelinek 		 * TRANSLATION_NOTE
1856*924Sgjelinek 		 * inherit-pkg-dirs is a literal that should not be translated.
1857*924Sgjelinek 		 */
1858*924Sgjelinek 		(void) fprintf(stderr, gettext("could not verify "
1859823Sgjelinek 		    "inherit-pkg-dirs: unable to enumerate mounts\n"));
1860823Sgjelinek 		return (Z_ERR);
1861823Sgjelinek 	}
1862823Sgjelinek 	while (zonecfg_getipdent(handle, &fstab) == Z_OK) {
1863823Sgjelinek 		/*
1864823Sgjelinek 		 * Verify fs_dir exists.
1865823Sgjelinek 		 */
1866823Sgjelinek 		(void) snprintf(specdir, sizeof (specdir), "%s%s",
1867823Sgjelinek 		    zonecfg_get_root(), fstab.zone_fs_dir);
1868823Sgjelinek 		if (stat(specdir, &st) != 0) {
1869*924Sgjelinek 			/*
1870*924Sgjelinek 			 * TRANSLATION_NOTE
1871*924Sgjelinek 			 * inherit-pkg-dir is a literal that should not be
1872*924Sgjelinek 			 * translated.
1873*924Sgjelinek 			 */
1874*924Sgjelinek 			(void) fprintf(stderr, gettext("could not verify "
1875823Sgjelinek 			    "inherit-pkg-dir %s: %s\n"),
1876823Sgjelinek 			    fstab.zone_fs_dir, strerror(errno));
1877823Sgjelinek 			return_code = Z_ERR;
1878823Sgjelinek 		}
1879823Sgjelinek 		if (strcmp(st.st_fstype, MNTTYPE_NFS) == 0) {
1880*924Sgjelinek 			/*
1881*924Sgjelinek 			 * TRANSLATION_NOTE
1882*924Sgjelinek 			 * inherit-pkg-dir and NFS are literals that should
1883*924Sgjelinek 			 * not be translated.
1884*924Sgjelinek 			 */
1885823Sgjelinek 			(void) fprintf(stderr, gettext("cannot verify "
1886*924Sgjelinek 			    "inherit-pkg-dir %s: NFS mounted file-system.\n"
1887*924Sgjelinek 			    "\tA local file-system must be used.\n"),
1888823Sgjelinek 			    fstab.zone_fs_dir);
1889823Sgjelinek 			return_code = Z_ERR;
1890823Sgjelinek 		}
1891823Sgjelinek 	}
1892823Sgjelinek 	(void) zonecfg_endipdent(handle);
1893823Sgjelinek 
1894823Sgjelinek 	return (return_code);
1895823Sgjelinek }
1896823Sgjelinek 
1897823Sgjelinek static int
18980Sstevel@tonic-gate verify_filesystems(zone_dochandle_t handle)
18990Sstevel@tonic-gate {
19000Sstevel@tonic-gate 	int return_code = Z_OK;
19010Sstevel@tonic-gate 	struct zone_fstab fstab;
19020Sstevel@tonic-gate 	char cmdbuf[MAXPATHLEN];
19030Sstevel@tonic-gate 	struct stat st;
19040Sstevel@tonic-gate 
19050Sstevel@tonic-gate 	/*
19060Sstevel@tonic-gate 	 * No need to verify inherit-pkg-dir fs types, as their type is
19070Sstevel@tonic-gate 	 * implicitly lofs, which is known.  Therefore, the types are only
19080Sstevel@tonic-gate 	 * verified for regular filesystems below.
19090Sstevel@tonic-gate 	 *
19100Sstevel@tonic-gate 	 * Since the actual mount point is not known until the dependent mounts
19110Sstevel@tonic-gate 	 * are performed, we don't attempt any path validation here: that will
19120Sstevel@tonic-gate 	 * happen later when zoneadmd actually does the mounts.
19130Sstevel@tonic-gate 	 */
19140Sstevel@tonic-gate 	if (zonecfg_setfsent(handle) != Z_OK) {
1915*924Sgjelinek 		(void) fprintf(stderr, gettext("could not verify file-systems: "
19160Sstevel@tonic-gate 		    "unable to enumerate mounts\n"));
19170Sstevel@tonic-gate 		return (Z_ERR);
19180Sstevel@tonic-gate 	}
19190Sstevel@tonic-gate 	while (zonecfg_getfsent(handle, &fstab) == Z_OK) {
19200Sstevel@tonic-gate 		if (!zonecfg_valid_fs_type(fstab.zone_fs_type)) {
19210Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("cannot verify fs %s: "
19220Sstevel@tonic-gate 			    "type %s is not allowed.\n"), fstab.zone_fs_dir,
19230Sstevel@tonic-gate 			    fstab.zone_fs_type);
19240Sstevel@tonic-gate 			return_code = Z_ERR;
19250Sstevel@tonic-gate 			goto next_fs;
19260Sstevel@tonic-gate 		}
19270Sstevel@tonic-gate 		/*
19280Sstevel@tonic-gate 		 * Verify /usr/lib/fs/<fstype>/mount exists.
19290Sstevel@tonic-gate 		 */
19300Sstevel@tonic-gate 		if (snprintf(cmdbuf, sizeof (cmdbuf), "/usr/lib/fs/%s/mount",
19310Sstevel@tonic-gate 		    fstab.zone_fs_type) > sizeof (cmdbuf)) {
19320Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("cannot verify fs %s: "
19330Sstevel@tonic-gate 			    "type %s is too long.\n"), fstab.zone_fs_dir,
19340Sstevel@tonic-gate 			    fstab.zone_fs_type);
19350Sstevel@tonic-gate 			return_code = Z_ERR;
19360Sstevel@tonic-gate 			goto next_fs;
19370Sstevel@tonic-gate 		}
19380Sstevel@tonic-gate 		if (stat(cmdbuf, &st) != 0) {
1939*924Sgjelinek 			(void) fprintf(stderr, gettext("could not verify fs "
1940*924Sgjelinek 			    "%s: could not access %s: %s\n"), fstab.zone_fs_dir,
19410Sstevel@tonic-gate 			    cmdbuf, strerror(errno));
19420Sstevel@tonic-gate 			return_code = Z_ERR;
19430Sstevel@tonic-gate 			goto next_fs;
19440Sstevel@tonic-gate 		}
19450Sstevel@tonic-gate 		if (!S_ISREG(st.st_mode)) {
1946*924Sgjelinek 			(void) fprintf(stderr, gettext("could not verify fs "
1947*924Sgjelinek 			    "%s: %s is not a regular file\n"),
1948*924Sgjelinek 			    fstab.zone_fs_dir, cmdbuf);
19490Sstevel@tonic-gate 			return_code = Z_ERR;
19500Sstevel@tonic-gate 			goto next_fs;
19510Sstevel@tonic-gate 		}
19520Sstevel@tonic-gate 		/*
19530Sstevel@tonic-gate 		 * Verify /usr/lib/fs/<fstype>/fsck exists iff zone_fs_raw is
19540Sstevel@tonic-gate 		 * set.
19550Sstevel@tonic-gate 		 */
19560Sstevel@tonic-gate 		if (snprintf(cmdbuf, sizeof (cmdbuf), "/usr/lib/fs/%s/fsck",
19570Sstevel@tonic-gate 		    fstab.zone_fs_type) > sizeof (cmdbuf)) {
19580Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("cannot verify fs %s: "
19590Sstevel@tonic-gate 			    "type %s is too long.\n"), fstab.zone_fs_dir,
19600Sstevel@tonic-gate 			    fstab.zone_fs_type);
19610Sstevel@tonic-gate 			return_code = Z_ERR;
19620Sstevel@tonic-gate 			goto next_fs;
19630Sstevel@tonic-gate 		}
19640Sstevel@tonic-gate 		if (fstab.zone_fs_raw[0] == '\0' && stat(cmdbuf, &st) == 0) {
1965*924Sgjelinek 			(void) fprintf(stderr, gettext("could not verify fs "
1966*924Sgjelinek 			    "%s: must specify 'raw' device for %s "
1967*924Sgjelinek 			    "file-systems\n"),
19680Sstevel@tonic-gate 			    fstab.zone_fs_dir, fstab.zone_fs_type);
19690Sstevel@tonic-gate 			return_code = Z_ERR;
19700Sstevel@tonic-gate 			goto next_fs;
19710Sstevel@tonic-gate 		}
19720Sstevel@tonic-gate 		if (fstab.zone_fs_raw[0] != '\0' &&
19730Sstevel@tonic-gate 		    (stat(cmdbuf, &st) != 0 || !S_ISREG(st.st_mode))) {
19740Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("cannot verify fs %s: "
19750Sstevel@tonic-gate 			    "'raw' device specified but "
19760Sstevel@tonic-gate 			    "no fsck executable exists for %s\n"),
19770Sstevel@tonic-gate 			    fstab.zone_fs_dir, fstab.zone_fs_type);
19780Sstevel@tonic-gate 			return_code = Z_ERR;
19790Sstevel@tonic-gate 			goto next_fs;
19800Sstevel@tonic-gate 		}
1981823Sgjelinek 		/*
1982823Sgjelinek 		 * Verify fs_special and optionally fs_raw, exists.
1983823Sgjelinek 		 */
1984823Sgjelinek 		if (stat(fstab.zone_fs_special, &st) != 0) {
1985*924Sgjelinek 			(void) fprintf(stderr, gettext("could not verify fs "
1986*924Sgjelinek 			    "%s: could not access %s: %s\n"), fstab.zone_fs_dir,
1987823Sgjelinek 			    fstab.zone_fs_special, strerror(errno));
1988823Sgjelinek 			return_code = Z_ERR;
1989823Sgjelinek 			goto next_fs;
1990823Sgjelinek 		}
1991823Sgjelinek 		if (strcmp(st.st_fstype, MNTTYPE_NFS) == 0) {
1992*924Sgjelinek 			/*
1993*924Sgjelinek 			 * TRANSLATION_NOTE
1994*924Sgjelinek 			 * fs and NFS are literals that should
1995*924Sgjelinek 			 * not be translated.
1996*924Sgjelinek 			 */
1997823Sgjelinek 			(void) fprintf(stderr, gettext("cannot verify "
1998*924Sgjelinek 			    "fs %s: NFS mounted file-system.\n"
1999*924Sgjelinek 			    "\tA local file-system must be used.\n"),
2000823Sgjelinek 			    fstab.zone_fs_special);
2001823Sgjelinek 			return_code = Z_ERR;
2002823Sgjelinek 			goto next_fs;
2003823Sgjelinek 		}
2004823Sgjelinek 		if (fstab.zone_fs_raw[0] != '\0' &&
2005823Sgjelinek 		    stat(fstab.zone_fs_raw, &st) != 0) {
2006*924Sgjelinek 			/*
2007*924Sgjelinek 			 * TRANSLATION_NOTE
2008*924Sgjelinek 			 * fs is a literal that should not be translated.
2009*924Sgjelinek 			 */
2010*924Sgjelinek 			(void) fprintf(stderr, gettext("could not verify fs "
2011*924Sgjelinek 			    "%s: could not access %s: %s\n"), fstab.zone_fs_dir,
2012823Sgjelinek 			    fstab.zone_fs_raw, strerror(errno));
2013823Sgjelinek 			return_code = Z_ERR;
2014823Sgjelinek 			goto next_fs;
2015823Sgjelinek 		}
20160Sstevel@tonic-gate next_fs:
20170Sstevel@tonic-gate 		zonecfg_free_fs_option_list(fstab.zone_fs_options);
20180Sstevel@tonic-gate 	}
20190Sstevel@tonic-gate 	(void) zonecfg_endfsent(handle);
20200Sstevel@tonic-gate 
20210Sstevel@tonic-gate 	return (return_code);
20220Sstevel@tonic-gate }
20230Sstevel@tonic-gate 
2024789Sahrens const char *current_dataset;
2025789Sahrens 
2026789Sahrens /*
2027789Sahrens  * Custom error handler for errors incurred as part of the checks below.  We
2028789Sahrens  * want to trim off the leading 'cannot open ...' to create a better error
2029789Sahrens  * message.  The only other way this can fail is if we fail to set the 'zoned'
2030789Sahrens  * property.  In this case we just pass the error on verbatim.
2031789Sahrens  */
2032789Sahrens static void
2033789Sahrens zfs_error_handler(const char *fmt, va_list ap)
2034789Sahrens {
2035789Sahrens 	char buf[1024];
2036789Sahrens 
2037789Sahrens 	(void) vsnprintf(buf, sizeof (buf), fmt, ap);
2038789Sahrens 
2039789Sahrens 	if (strncmp(gettext("cannot open "), buf,
2040789Sahrens 	    strlen(gettext("cannot open "))) == 0)
2041*924Sgjelinek 		/*
2042*924Sgjelinek 		 * TRANSLATION_NOTE
2043*924Sgjelinek 		 * zfs and dataset are literals that should not be translated.
2044*924Sgjelinek 		 */
2045*924Sgjelinek 		(void) fprintf(stderr, gettext("could not verify zfs "
2046789Sahrens 		    "dataset %s%s\n"), current_dataset, strchr(buf, ':'));
2047789Sahrens 	else
2048*924Sgjelinek 		(void) fprintf(stderr, gettext("could not verify zfs dataset "
2049789Sahrens 		    "%s: %s\n"), current_dataset, buf);
2050789Sahrens }
2051789Sahrens 
2052789Sahrens /* ARGSUSED */
2053789Sahrens static int
2054789Sahrens check_zvol(zfs_handle_t *zhp, void *unused)
2055789Sahrens {
2056789Sahrens 	int ret;
2057789Sahrens 
2058789Sahrens 	if (zfs_get_type(zhp) == ZFS_TYPE_VOLUME) {
2059*924Sgjelinek 		/*
2060*924Sgjelinek 		 * TRANSLATION_NOTE
2061*924Sgjelinek 		 * zfs and dataset are literals that should not be translated.
2062*924Sgjelinek 		 */
2063789Sahrens 		(void) fprintf(stderr, gettext("cannot verify zfs dataset %s: "
2064789Sahrens 		    "volumes cannot be specified as a zone dataset resource\n"),
2065789Sahrens 		    zfs_get_name(zhp));
2066789Sahrens 		ret = -1;
2067789Sahrens 	} else {
2068789Sahrens 		ret = zfs_iter_children(zhp, check_zvol, NULL);
2069789Sahrens 	}
2070789Sahrens 
2071789Sahrens 	zfs_close(zhp);
2072789Sahrens 
2073789Sahrens 	return (ret);
2074789Sahrens }
2075789Sahrens 
2076789Sahrens /*
2077789Sahrens  * Validate that the given dataset exists on the system, and that neither it nor
2078789Sahrens  * its children are zvols.
2079789Sahrens  *
2080789Sahrens  * Note that we don't do anything with the 'zoned' property here.  All
2081789Sahrens  * management is done in zoneadmd when the zone is actually rebooted.  This
2082789Sahrens  * allows us to automatically set the zoned property even when a zone is
2083789Sahrens  * rebooted by the administrator.
2084789Sahrens  */
2085789Sahrens static int
2086789Sahrens verify_datasets(zone_dochandle_t handle)
2087789Sahrens {
2088789Sahrens 	int return_code = Z_OK;
2089789Sahrens 	struct zone_dstab dstab;
2090789Sahrens 	zfs_handle_t *zhp;
2091789Sahrens 	char propbuf[ZFS_MAXPROPLEN];
2092789Sahrens 	char source[ZFS_MAXNAMELEN];
2093789Sahrens 	zfs_source_t srctype;
2094789Sahrens 
2095789Sahrens 	if (zonecfg_setdsent(handle) != Z_OK) {
2096*924Sgjelinek 		/*
2097*924Sgjelinek 		 * TRANSLATION_NOTE
2098*924Sgjelinek 		 * zfs and dataset are literals that should not be translated.
2099*924Sgjelinek 		 */
2100*924Sgjelinek 		(void) fprintf(stderr, gettext("could not verify zfs datasets: "
2101789Sahrens 		    "unable to enumerate datasets\n"));
2102789Sahrens 		return (Z_ERR);
2103789Sahrens 	}
2104789Sahrens 
2105789Sahrens 	zfs_set_error_handler(zfs_error_handler);
2106789Sahrens 
2107789Sahrens 	while (zonecfg_getdsent(handle, &dstab) == Z_OK) {
2108789Sahrens 
2109789Sahrens 		current_dataset = dstab.zone_dataset_name;
2110789Sahrens 
2111789Sahrens 		if ((zhp = zfs_open(dstab.zone_dataset_name,
2112789Sahrens 		    ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME)) == NULL) {
2113789Sahrens 			return_code = Z_ERR;
2114789Sahrens 			continue;
2115789Sahrens 		}
2116789Sahrens 
2117789Sahrens 		if (zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, propbuf,
2118789Sahrens 		    sizeof (propbuf), &srctype, source,
2119789Sahrens 		    sizeof (source), 0) == 0 &&
2120789Sahrens 		    (srctype == ZFS_SRC_INHERITED)) {
2121*924Sgjelinek 			(void) fprintf(stderr, gettext("could not verify zfs "
2122789Sahrens 			    "dataset %s: mountpoint cannot be inherited\n"),
2123789Sahrens 			    dstab.zone_dataset_name);
2124789Sahrens 			return_code = Z_ERR;
2125789Sahrens 			zfs_close(zhp);
2126789Sahrens 			continue;
2127789Sahrens 		}
2128789Sahrens 
2129789Sahrens 		if (zfs_get_type(zhp) == ZFS_TYPE_VOLUME) {
2130789Sahrens 			(void) fprintf(stderr, gettext("cannot verify zfs "
2131789Sahrens 			    "dataset %s: volumes cannot be specified as a "
2132789Sahrens 			    "zone dataset resource\n"),
2133789Sahrens 			    dstab.zone_dataset_name);
2134789Sahrens 			return_code = Z_ERR;
2135789Sahrens 		}
2136789Sahrens 
2137789Sahrens 		if (zfs_iter_children(zhp, check_zvol, NULL) != 0)
2138789Sahrens 			return_code = Z_ERR;
2139789Sahrens 
2140789Sahrens 		zfs_close(zhp);
2141789Sahrens 	}
2142789Sahrens 	(void) zonecfg_enddsent(handle);
2143789Sahrens 
2144789Sahrens 	return (return_code);
2145789Sahrens }
2146789Sahrens 
21470Sstevel@tonic-gate static int
21480Sstevel@tonic-gate verify_details(int cmd_num)
21490Sstevel@tonic-gate {
21500Sstevel@tonic-gate 	zone_dochandle_t handle;
21510Sstevel@tonic-gate 	struct zone_nwiftab nwiftab;
21520Sstevel@tonic-gate 	char zonepath[MAXPATHLEN], checkpath[MAXPATHLEN];
21530Sstevel@tonic-gate 	int return_code = Z_OK;
21540Sstevel@tonic-gate 	int err;
2155766Scarlsonj 	boolean_t in_alt_root;
21560Sstevel@tonic-gate 
21570Sstevel@tonic-gate 	if ((handle = zonecfg_init_handle()) == NULL) {
21580Sstevel@tonic-gate 		zperror(cmd_to_str(cmd_num), B_TRUE);
21590Sstevel@tonic-gate 		return (Z_ERR);
21600Sstevel@tonic-gate 	}
21610Sstevel@tonic-gate 	if ((err = zonecfg_get_handle(target_zone, handle)) != Z_OK) {
21620Sstevel@tonic-gate 		errno = err;
21630Sstevel@tonic-gate 		zperror(cmd_to_str(cmd_num), B_TRUE);
21640Sstevel@tonic-gate 		zonecfg_fini_handle(handle);
21650Sstevel@tonic-gate 		return (Z_ERR);
21660Sstevel@tonic-gate 	}
21670Sstevel@tonic-gate 	if ((err = zonecfg_get_zonepath(handle, zonepath, sizeof (zonepath))) !=
21680Sstevel@tonic-gate 	    Z_OK) {
21690Sstevel@tonic-gate 		errno = err;
21700Sstevel@tonic-gate 		zperror(cmd_to_str(cmd_num), B_TRUE);
21710Sstevel@tonic-gate 		zonecfg_fini_handle(handle);
21720Sstevel@tonic-gate 		return (Z_ERR);
21730Sstevel@tonic-gate 	}
21740Sstevel@tonic-gate 	/*
21750Sstevel@tonic-gate 	 * zonecfg_get_zonepath() gets its data from the XML repository.
21760Sstevel@tonic-gate 	 * Verify this against the index file, which is checked first by
21770Sstevel@tonic-gate 	 * zone_get_zonepath().  If they don't match, bail out.
21780Sstevel@tonic-gate 	 */
21790Sstevel@tonic-gate 	if ((err = zone_get_zonepath(target_zone, checkpath,
21800Sstevel@tonic-gate 	    sizeof (checkpath))) != Z_OK) {
21810Sstevel@tonic-gate 		errno = err;
21820Sstevel@tonic-gate 		zperror2(target_zone, gettext("could not get zone path"));
21830Sstevel@tonic-gate 		return (Z_ERR);
21840Sstevel@tonic-gate 	}
21850Sstevel@tonic-gate 	if (strcmp(zonepath, checkpath) != 0) {
2186*924Sgjelinek 		/*
2187*924Sgjelinek 		 * TRANSLATION_NOTE
2188*924Sgjelinek 		 * XML and zonepath are literals that should not be translated.
2189*924Sgjelinek 		 */
21900Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("The XML repository has "
21910Sstevel@tonic-gate 		    "zonepath '%s',\nbut the index file has zonepath '%s'.\n"
21920Sstevel@tonic-gate 		    "These must match, so fix the incorrect entry.\n"),
21930Sstevel@tonic-gate 		    zonepath, checkpath);
21940Sstevel@tonic-gate 		return (Z_ERR);
21950Sstevel@tonic-gate 	}
21960Sstevel@tonic-gate 	if (validate_zonepath(zonepath, cmd_num) != Z_OK) {
21970Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("could not verify zonepath %s "
21980Sstevel@tonic-gate 		    "because of the above errors.\n"), zonepath);
21990Sstevel@tonic-gate 		return_code = Z_ERR;
22000Sstevel@tonic-gate 	}
22010Sstevel@tonic-gate 
2202766Scarlsonj 	in_alt_root = zonecfg_in_alt_root();
2203766Scarlsonj 	if (in_alt_root)
2204766Scarlsonj 		goto no_net;
2205766Scarlsonj 
22060Sstevel@tonic-gate 	if ((err = zonecfg_setnwifent(handle)) != Z_OK) {
22070Sstevel@tonic-gate 		errno = err;
22080Sstevel@tonic-gate 		zperror(cmd_to_str(cmd_num), B_TRUE);
22090Sstevel@tonic-gate 		zonecfg_fini_handle(handle);
22100Sstevel@tonic-gate 		return (Z_ERR);
22110Sstevel@tonic-gate 	}
22120Sstevel@tonic-gate 	while (zonecfg_getnwifent(handle, &nwiftab) == Z_OK) {
22130Sstevel@tonic-gate 		struct lifreq lifr;
22140Sstevel@tonic-gate 		sa_family_t af;
22150Sstevel@tonic-gate 		int so, res;
22160Sstevel@tonic-gate 
22170Sstevel@tonic-gate 		/* skip any loopback interfaces */
22180Sstevel@tonic-gate 		if (strcmp(nwiftab.zone_nwif_physical, "lo0") == 0)
22190Sstevel@tonic-gate 			continue;
22200Sstevel@tonic-gate 		if ((res = zonecfg_valid_net_address(nwiftab.zone_nwif_address,
22210Sstevel@tonic-gate 		    &lifr)) != Z_OK) {
22220Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("could not verify %s "
22230Sstevel@tonic-gate 			    "%s=%s %s=%s: %s\n"), "net", "address",
22240Sstevel@tonic-gate 			    nwiftab.zone_nwif_address, "physical",
22250Sstevel@tonic-gate 			    nwiftab.zone_nwif_physical, zonecfg_strerror(res));
22260Sstevel@tonic-gate 			return_code = Z_ERR;
22270Sstevel@tonic-gate 			continue;
22280Sstevel@tonic-gate 		}
22290Sstevel@tonic-gate 		af = lifr.lifr_addr.ss_family;
22300Sstevel@tonic-gate 		(void) memset(&lifr, 0, sizeof (lifr));
22310Sstevel@tonic-gate 		(void) strlcpy(lifr.lifr_name, nwiftab.zone_nwif_physical,
22320Sstevel@tonic-gate 		    sizeof (lifr.lifr_name));
22330Sstevel@tonic-gate 		lifr.lifr_addr.ss_family = af;
22340Sstevel@tonic-gate 		if ((so = socket(af, SOCK_DGRAM, 0)) < 0) {
22350Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("could not verify %s "
22360Sstevel@tonic-gate 			    "%s=%s %s=%s: could not get socket: %s\n"), "net",
22370Sstevel@tonic-gate 			    "address", nwiftab.zone_nwif_address, "physical",
22380Sstevel@tonic-gate 			    nwiftab.zone_nwif_physical, strerror(errno));
22390Sstevel@tonic-gate 			return_code = Z_ERR;
22400Sstevel@tonic-gate 			continue;
22410Sstevel@tonic-gate 		}
22420Sstevel@tonic-gate 		if (ioctl(so, SIOCGLIFFLAGS, (caddr_t)&lifr) < 0) {
22430Sstevel@tonic-gate 			(void) fprintf(stderr,
22440Sstevel@tonic-gate 			    gettext("could not verify %s %s=%s %s=%s: %s\n"),
22450Sstevel@tonic-gate 			    "net", "address", nwiftab.zone_nwif_address,
22460Sstevel@tonic-gate 			    "physical", nwiftab.zone_nwif_physical,
22470Sstevel@tonic-gate 			    strerror(errno));
22480Sstevel@tonic-gate 			return_code = Z_ERR;
22490Sstevel@tonic-gate 		}
22500Sstevel@tonic-gate 		(void) close(so);
22510Sstevel@tonic-gate 	}
22520Sstevel@tonic-gate 	(void) zonecfg_endnwifent(handle);
2253766Scarlsonj no_net:
22540Sstevel@tonic-gate 
22550Sstevel@tonic-gate 	if (verify_filesystems(handle) != Z_OK)
22560Sstevel@tonic-gate 		return_code = Z_ERR;
2257823Sgjelinek 	if (verify_ipd(handle) != Z_OK)
2258823Sgjelinek 		return_code = Z_ERR;
2259766Scarlsonj 	if (!in_alt_root && verify_rctls(handle) != Z_OK)
22600Sstevel@tonic-gate 		return_code = Z_ERR;
2261766Scarlsonj 	if (!in_alt_root && verify_pool(handle) != Z_OK)
22620Sstevel@tonic-gate 		return_code = Z_ERR;
2263789Sahrens 	if (!in_alt_root && verify_datasets(handle) != Z_OK)
2264789Sahrens 		return_code = Z_ERR;
22650Sstevel@tonic-gate 	zonecfg_fini_handle(handle);
22660Sstevel@tonic-gate 	if (return_code == Z_ERR)
22670Sstevel@tonic-gate 		(void) fprintf(stderr,
22680Sstevel@tonic-gate 		    gettext("%s: zone %s failed to verify\n"),
22690Sstevel@tonic-gate 		    execname, target_zone);
22700Sstevel@tonic-gate 	return (return_code);
22710Sstevel@tonic-gate }
22720Sstevel@tonic-gate 
22730Sstevel@tonic-gate static int
22740Sstevel@tonic-gate verify_func(int argc, char *argv[])
22750Sstevel@tonic-gate {
22760Sstevel@tonic-gate 	int arg;
22770Sstevel@tonic-gate 
22780Sstevel@tonic-gate 	optind = 0;
22790Sstevel@tonic-gate 	if ((arg = getopt(argc, argv, "?")) != EOF) {
22800Sstevel@tonic-gate 		switch (arg) {
22810Sstevel@tonic-gate 		case '?':
22820Sstevel@tonic-gate 			sub_usage(SHELP_VERIFY, CMD_VERIFY);
22830Sstevel@tonic-gate 			return (optopt == '?' ? Z_OK : Z_USAGE);
22840Sstevel@tonic-gate 		default:
22850Sstevel@tonic-gate 			sub_usage(SHELP_VERIFY, CMD_VERIFY);
22860Sstevel@tonic-gate 			return (Z_USAGE);
22870Sstevel@tonic-gate 		}
22880Sstevel@tonic-gate 	}
22890Sstevel@tonic-gate 	if (argc > optind) {
22900Sstevel@tonic-gate 		sub_usage(SHELP_VERIFY, CMD_VERIFY);
22910Sstevel@tonic-gate 		return (Z_USAGE);
22920Sstevel@tonic-gate 	}
22930Sstevel@tonic-gate 	if (sanity_check(target_zone, CMD_VERIFY, B_FALSE, B_FALSE) != Z_OK)
22940Sstevel@tonic-gate 		return (Z_ERR);
22950Sstevel@tonic-gate 	return (verify_details(CMD_VERIFY));
22960Sstevel@tonic-gate }
22970Sstevel@tonic-gate 
22980Sstevel@tonic-gate #define	LUCREATEZONE	"/usr/lib/lu/lucreatezone"
22990Sstevel@tonic-gate 
23000Sstevel@tonic-gate static int
23010Sstevel@tonic-gate install_func(int argc, char *argv[])
23020Sstevel@tonic-gate {
23030Sstevel@tonic-gate 	/* 9: "exec " and " -z " */
23040Sstevel@tonic-gate 	char cmdbuf[sizeof (LUCREATEZONE) + ZONENAME_MAX + 9];
23050Sstevel@tonic-gate 	int lockfd;
23060Sstevel@tonic-gate 	int err, arg;
23070Sstevel@tonic-gate 	char zonepath[MAXPATHLEN];
23080Sstevel@tonic-gate 	int status;
23090Sstevel@tonic-gate 
2310766Scarlsonj 	if (zonecfg_in_alt_root()) {
2311766Scarlsonj 		zerror(gettext("cannot install zone in alternate root"));
2312766Scarlsonj 		return (Z_ERR);
2313766Scarlsonj 	}
2314766Scarlsonj 
23150Sstevel@tonic-gate 	optind = 0;
23160Sstevel@tonic-gate 	if ((arg = getopt(argc, argv, "?")) != EOF) {
23170Sstevel@tonic-gate 		switch (arg) {
23180Sstevel@tonic-gate 		case '?':
23190Sstevel@tonic-gate 			sub_usage(SHELP_INSTALL, CMD_INSTALL);
23200Sstevel@tonic-gate 			return (optopt == '?' ? Z_OK : Z_USAGE);
23210Sstevel@tonic-gate 		default:
23220Sstevel@tonic-gate 			sub_usage(SHELP_INSTALL, CMD_INSTALL);
23230Sstevel@tonic-gate 			return (Z_USAGE);
23240Sstevel@tonic-gate 		}
23250Sstevel@tonic-gate 	}
23260Sstevel@tonic-gate 	if (argc > optind) {
23270Sstevel@tonic-gate 		sub_usage(SHELP_INSTALL, CMD_INSTALL);
23280Sstevel@tonic-gate 		return (Z_USAGE);
23290Sstevel@tonic-gate 	}
23300Sstevel@tonic-gate 	if (sanity_check(target_zone, CMD_INSTALL, B_FALSE, B_TRUE) != Z_OK)
23310Sstevel@tonic-gate 		return (Z_ERR);
23320Sstevel@tonic-gate 	if (verify_details(CMD_INSTALL) != Z_OK)
23330Sstevel@tonic-gate 		return (Z_ERR);
23340Sstevel@tonic-gate 
23350Sstevel@tonic-gate 	if (grab_lock_file(target_zone, &lockfd) != Z_OK) {
23360Sstevel@tonic-gate 		zerror(gettext("another %s may have an operation in progress."),
23370Sstevel@tonic-gate 		    "zoneadmd");
23380Sstevel@tonic-gate 		return (Z_ERR);
23390Sstevel@tonic-gate 	}
23400Sstevel@tonic-gate 	err = zone_set_state(target_zone, ZONE_STATE_INCOMPLETE);
23410Sstevel@tonic-gate 	if (err != Z_OK) {
23420Sstevel@tonic-gate 		errno = err;
23430Sstevel@tonic-gate 		zperror2(target_zone, gettext("could not set state"));
23440Sstevel@tonic-gate 		goto done;
23450Sstevel@tonic-gate 	}
23460Sstevel@tonic-gate 
23470Sstevel@tonic-gate 	/*
23480Sstevel@tonic-gate 	 * According to the Application Packaging Developer's Guide, a
23490Sstevel@tonic-gate 	 * "checkinstall" script when included in a package is executed as
23500Sstevel@tonic-gate 	 * the user "install", if such a user exists, or by the user
23510Sstevel@tonic-gate 	 * "nobody".  In order to support this dubious behavior, the path
23520Sstevel@tonic-gate 	 * to the zone being constructed is opened up during the life of
23530Sstevel@tonic-gate 	 * the command laying down the zone's root file system.  Once this
23540Sstevel@tonic-gate 	 * has completed, regardless of whether it was successful, the
23550Sstevel@tonic-gate 	 * path to the zone is again restricted.
23560Sstevel@tonic-gate 	 */
23570Sstevel@tonic-gate 	if ((err = zone_get_zonepath(target_zone, zonepath,
23580Sstevel@tonic-gate 	    sizeof (zonepath))) != Z_OK) {
23590Sstevel@tonic-gate 		errno = err;
23600Sstevel@tonic-gate 		zperror2(target_zone, gettext("could not get zone path"));
23610Sstevel@tonic-gate 		goto done;
23620Sstevel@tonic-gate 	}
23630Sstevel@tonic-gate 	if (chmod(zonepath, DEFAULT_DIR_MODE) != 0) {
23640Sstevel@tonic-gate 		zperror(zonepath, B_FALSE);
23650Sstevel@tonic-gate 		err = Z_ERR;
23660Sstevel@tonic-gate 		goto done;
23670Sstevel@tonic-gate 	}
23680Sstevel@tonic-gate 
23690Sstevel@tonic-gate 	/*
23700Sstevel@tonic-gate 	 * "exec" the command so that the returned status is that of
23710Sstevel@tonic-gate 	 * LUCREATEZONE and not the shell.
23720Sstevel@tonic-gate 	 */
23730Sstevel@tonic-gate 	(void) snprintf(cmdbuf, sizeof (cmdbuf), "exec " LUCREATEZONE " -z %s",
23740Sstevel@tonic-gate 	    target_zone);
23750Sstevel@tonic-gate 	status = do_subproc(cmdbuf);
23760Sstevel@tonic-gate 	if (chmod(zonepath, S_IRWXU) != 0) {
23770Sstevel@tonic-gate 		zperror(zonepath, B_FALSE);
23780Sstevel@tonic-gate 		err = Z_ERR;
23790Sstevel@tonic-gate 		goto done;
23800Sstevel@tonic-gate 	}
23810Sstevel@tonic-gate 	if ((err = subproc_status(LUCREATEZONE, status)) != Z_OK)
23820Sstevel@tonic-gate 		goto done;
23830Sstevel@tonic-gate 
23840Sstevel@tonic-gate 	if ((err = zone_set_state(target_zone, ZONE_STATE_INSTALLED)) != Z_OK) {
23850Sstevel@tonic-gate 		errno = err;
23860Sstevel@tonic-gate 		zperror2(target_zone, gettext("could not set state"));
23870Sstevel@tonic-gate 		goto done;
23880Sstevel@tonic-gate 	}
23890Sstevel@tonic-gate 
23900Sstevel@tonic-gate done:
23910Sstevel@tonic-gate 	release_lock_file(lockfd);
23920Sstevel@tonic-gate 	return ((err == Z_OK) ? Z_OK : Z_ERR);
23930Sstevel@tonic-gate }
23940Sstevel@tonic-gate 
23950Sstevel@tonic-gate /*
23960Sstevel@tonic-gate  * On input, TRUE => yes, FALSE => no.
23970Sstevel@tonic-gate  * On return, TRUE => 1, FALSE => 0, could not ask => -1.
23980Sstevel@tonic-gate  */
23990Sstevel@tonic-gate 
24000Sstevel@tonic-gate static int
24010Sstevel@tonic-gate ask_yesno(boolean_t default_answer, const char *question)
24020Sstevel@tonic-gate {
24030Sstevel@tonic-gate 	char line[64];	/* should be large enough to answer yes or no */
24040Sstevel@tonic-gate 
24050Sstevel@tonic-gate 	if (!isatty(STDIN_FILENO))
24060Sstevel@tonic-gate 		return (-1);
24070Sstevel@tonic-gate 	for (;;) {
24080Sstevel@tonic-gate 		(void) printf("%s (%s)? ", question,
24090Sstevel@tonic-gate 		    default_answer ? "[y]/n" : "y/[n]");
24100Sstevel@tonic-gate 		if (fgets(line, sizeof (line), stdin) == NULL ||
24110Sstevel@tonic-gate 		    line[0] == '\n')
24120Sstevel@tonic-gate 			return (default_answer ? 1 : 0);
24130Sstevel@tonic-gate 		if (tolower(line[0]) == 'y')
24140Sstevel@tonic-gate 			return (1);
24150Sstevel@tonic-gate 		if (tolower(line[0]) == 'n')
24160Sstevel@tonic-gate 			return (0);
24170Sstevel@tonic-gate 	}
24180Sstevel@tonic-gate }
24190Sstevel@tonic-gate 
24200Sstevel@tonic-gate #define	RMCOMMAND	"/usr/bin/rm -rf"
24210Sstevel@tonic-gate 
24220Sstevel@tonic-gate /* ARGSUSED */
24230Sstevel@tonic-gate int
24240Sstevel@tonic-gate zfm_print(const char *p, void *r) {
24250Sstevel@tonic-gate 	zerror("  %s\n", p);
24260Sstevel@tonic-gate 	return (0);
24270Sstevel@tonic-gate }
24280Sstevel@tonic-gate 
24290Sstevel@tonic-gate static int
24300Sstevel@tonic-gate uninstall_func(int argc, char *argv[])
24310Sstevel@tonic-gate {
24320Sstevel@tonic-gate 	/* 6: "exec " and " " */
24330Sstevel@tonic-gate 	char cmdbuf[sizeof (RMCOMMAND) + MAXPATHLEN + 6];
24340Sstevel@tonic-gate 	char line[ZONENAME_MAX + 128];	/* Enough for "Are you sure ..." */
24350Sstevel@tonic-gate 	char rootpath[MAXPATHLEN], devpath[MAXPATHLEN];
24360Sstevel@tonic-gate 	boolean_t force = B_FALSE;
24370Sstevel@tonic-gate 	int lockfd, answer;
24380Sstevel@tonic-gate 	int err, arg;
24390Sstevel@tonic-gate 	int status;
24400Sstevel@tonic-gate 
2441766Scarlsonj 	if (zonecfg_in_alt_root()) {
2442766Scarlsonj 		zerror(gettext("cannot uninstall zone in alternate root"));
2443766Scarlsonj 		return (Z_ERR);
2444766Scarlsonj 	}
2445766Scarlsonj 
24460Sstevel@tonic-gate 	optind = 0;
24470Sstevel@tonic-gate 	while ((arg = getopt(argc, argv, "?F")) != EOF) {
24480Sstevel@tonic-gate 		switch (arg) {
24490Sstevel@tonic-gate 		case '?':
24500Sstevel@tonic-gate 			sub_usage(SHELP_UNINSTALL, CMD_UNINSTALL);
24510Sstevel@tonic-gate 			return (optopt == '?' ? Z_OK : Z_USAGE);
24520Sstevel@tonic-gate 		case 'F':
24530Sstevel@tonic-gate 			force = B_TRUE;
24540Sstevel@tonic-gate 			break;
24550Sstevel@tonic-gate 		default:
24560Sstevel@tonic-gate 			sub_usage(SHELP_UNINSTALL, CMD_UNINSTALL);
24570Sstevel@tonic-gate 			return (Z_USAGE);
24580Sstevel@tonic-gate 		}
24590Sstevel@tonic-gate 	}
24600Sstevel@tonic-gate 	if (argc > optind) {
24610Sstevel@tonic-gate 		sub_usage(SHELP_UNINSTALL, CMD_UNINSTALL);
24620Sstevel@tonic-gate 		return (Z_USAGE);
24630Sstevel@tonic-gate 	}
24640Sstevel@tonic-gate 
24650Sstevel@tonic-gate 	if (sanity_check(target_zone, CMD_UNINSTALL, B_FALSE, B_TRUE) != Z_OK)
24660Sstevel@tonic-gate 		return (Z_ERR);
24670Sstevel@tonic-gate 
24680Sstevel@tonic-gate 	if (!force) {
24690Sstevel@tonic-gate 		(void) snprintf(line, sizeof (line),
24700Sstevel@tonic-gate 		    gettext("Are you sure you want to %s zone %s"),
24710Sstevel@tonic-gate 		    cmd_to_str(CMD_UNINSTALL), target_zone);
24720Sstevel@tonic-gate 		if ((answer = ask_yesno(B_FALSE, line)) == 0) {
24730Sstevel@tonic-gate 			return (Z_OK);
24740Sstevel@tonic-gate 		} else if (answer == -1) {
24750Sstevel@tonic-gate 			zerror(gettext("Input not from terminal and -F "
24760Sstevel@tonic-gate 			    "not specified: %s not done."),
24770Sstevel@tonic-gate 			    cmd_to_str(CMD_UNINSTALL));
24780Sstevel@tonic-gate 			return (Z_ERR);
24790Sstevel@tonic-gate 		}
24800Sstevel@tonic-gate 	}
24810Sstevel@tonic-gate 
24820Sstevel@tonic-gate 	if ((err = zone_get_zonepath(target_zone, devpath,
24830Sstevel@tonic-gate 	    sizeof (devpath))) != Z_OK) {
24840Sstevel@tonic-gate 		errno = err;
24850Sstevel@tonic-gate 		zperror2(target_zone, gettext("could not get zone path"));
24860Sstevel@tonic-gate 		return (Z_ERR);
24870Sstevel@tonic-gate 	}
24880Sstevel@tonic-gate 	(void) strlcat(devpath, "/dev", sizeof (devpath));
24890Sstevel@tonic-gate 	if ((err = zone_get_rootpath(target_zone, rootpath,
24900Sstevel@tonic-gate 	    sizeof (rootpath))) != Z_OK) {
24910Sstevel@tonic-gate 		errno = err;
24920Sstevel@tonic-gate 		zperror2(target_zone, gettext("could not get root path"));
24930Sstevel@tonic-gate 		return (Z_ERR);
24940Sstevel@tonic-gate 	}
24950Sstevel@tonic-gate 
24960Sstevel@tonic-gate 	/*
24970Sstevel@tonic-gate 	 * If there seems to be a zoneadmd running for this zone, call it
24980Sstevel@tonic-gate 	 * to tell it that an uninstall is happening; if all goes well it
24990Sstevel@tonic-gate 	 * will then shut itself down.
25000Sstevel@tonic-gate 	 */
25010Sstevel@tonic-gate 	if (ping_zoneadmd(target_zone) == Z_OK) {
25020Sstevel@tonic-gate 		zone_cmd_arg_t zarg;
25030Sstevel@tonic-gate 		zarg.cmd = Z_NOTE_UNINSTALLING;
25040Sstevel@tonic-gate 		/* we don't care too much if this fails... just plow on */
25050Sstevel@tonic-gate 		(void) call_zoneadmd(target_zone, &zarg);
25060Sstevel@tonic-gate 	}
25070Sstevel@tonic-gate 
25080Sstevel@tonic-gate 	if (grab_lock_file(target_zone, &lockfd) != Z_OK) {
25090Sstevel@tonic-gate 		zerror(gettext("another %s may have an operation in progress."),
25100Sstevel@tonic-gate 		    "zoneadmd");
25110Sstevel@tonic-gate 		return (Z_ERR);
25120Sstevel@tonic-gate 	}
25130Sstevel@tonic-gate 
25140Sstevel@tonic-gate 	/* Don't uninstall the zone if anything is mounted there */
25150Sstevel@tonic-gate 	err = zonecfg_find_mounts(rootpath, NULL, NULL);
25160Sstevel@tonic-gate 	if (err) {
25170Sstevel@tonic-gate 		zerror(gettext("These file-systems are mounted on "
25180Sstevel@tonic-gate 			"subdirectories of %s.\n"), rootpath);
25190Sstevel@tonic-gate 		(void) zonecfg_find_mounts(rootpath, zfm_print, NULL);
25200Sstevel@tonic-gate 		return (Z_ERR);
25210Sstevel@tonic-gate 	}
25220Sstevel@tonic-gate 
25230Sstevel@tonic-gate 	err = zone_set_state(target_zone, ZONE_STATE_INCOMPLETE);
25240Sstevel@tonic-gate 	if (err != Z_OK) {
25250Sstevel@tonic-gate 		errno = err;
25260Sstevel@tonic-gate 		zperror2(target_zone, gettext("could not set state"));
25270Sstevel@tonic-gate 		goto bad;
25280Sstevel@tonic-gate 	}
25290Sstevel@tonic-gate 
25300Sstevel@tonic-gate 	/*
25310Sstevel@tonic-gate 	 * "exec" the command so that the returned status is that of
25320Sstevel@tonic-gate 	 * RMCOMMAND and not the shell.
25330Sstevel@tonic-gate 	 */
25340Sstevel@tonic-gate 	(void) snprintf(cmdbuf, sizeof (cmdbuf), "exec " RMCOMMAND " %s",
25350Sstevel@tonic-gate 	    devpath);
25360Sstevel@tonic-gate 	status = do_subproc(cmdbuf);
25370Sstevel@tonic-gate 	if ((err = subproc_status(RMCOMMAND, status)) != Z_OK)
25380Sstevel@tonic-gate 		goto bad;
25390Sstevel@tonic-gate 	(void) snprintf(cmdbuf, sizeof (cmdbuf), "exec " RMCOMMAND " %s",
25400Sstevel@tonic-gate 	    rootpath);
25410Sstevel@tonic-gate 	status = do_subproc(cmdbuf);
25420Sstevel@tonic-gate 	if ((err = subproc_status(RMCOMMAND, status)) != Z_OK)
25430Sstevel@tonic-gate 		goto bad;
25440Sstevel@tonic-gate 	err = zone_set_state(target_zone, ZONE_STATE_CONFIGURED);
25450Sstevel@tonic-gate 	if (err != Z_OK) {
25460Sstevel@tonic-gate 		errno = err;
25470Sstevel@tonic-gate 		zperror2(target_zone, gettext("could not reset state"));
25480Sstevel@tonic-gate 	}
25490Sstevel@tonic-gate bad:
25500Sstevel@tonic-gate 	release_lock_file(lockfd);
25510Sstevel@tonic-gate 	return (err);
25520Sstevel@tonic-gate }
25530Sstevel@tonic-gate 
2554766Scarlsonj /* ARGSUSED */
2555766Scarlsonj static int
2556766Scarlsonj mount_func(int argc, char *argv[])
2557766Scarlsonj {
2558766Scarlsonj 	zone_cmd_arg_t zarg;
2559766Scarlsonj 
2560766Scarlsonj 	if (argc > 0)
2561766Scarlsonj 		return (Z_USAGE);
2562766Scarlsonj 	if (sanity_check(target_zone, CMD_MOUNT, B_FALSE, B_FALSE) != Z_OK)
2563766Scarlsonj 		return (Z_ERR);
2564766Scarlsonj 	if (verify_details(CMD_MOUNT) != Z_OK)
2565766Scarlsonj 		return (Z_ERR);
2566766Scarlsonj 
2567766Scarlsonj 	zarg.cmd = Z_MOUNT;
2568766Scarlsonj 	if (call_zoneadmd(target_zone, &zarg) != 0) {
2569766Scarlsonj 		zerror(gettext("call to %s failed"), "zoneadmd");
2570766Scarlsonj 		return (Z_ERR);
2571766Scarlsonj 	}
2572766Scarlsonj 	return (Z_OK);
2573766Scarlsonj }
2574766Scarlsonj 
2575766Scarlsonj /* ARGSUSED */
2576766Scarlsonj static int
2577766Scarlsonj unmount_func(int argc, char *argv[])
2578766Scarlsonj {
2579766Scarlsonj 	zone_cmd_arg_t zarg;
2580766Scarlsonj 
2581766Scarlsonj 	if (argc > 0)
2582766Scarlsonj 		return (Z_USAGE);
2583766Scarlsonj 	if (sanity_check(target_zone, CMD_UNMOUNT, B_FALSE, B_FALSE) != Z_OK)
2584766Scarlsonj 		return (Z_ERR);
2585766Scarlsonj 
2586766Scarlsonj 	zarg.cmd = Z_UNMOUNT;
2587766Scarlsonj 	if (call_zoneadmd(target_zone, &zarg) != 0) {
2588766Scarlsonj 		zerror(gettext("call to %s failed"), "zoneadmd");
2589766Scarlsonj 		return (Z_ERR);
2590766Scarlsonj 	}
2591766Scarlsonj 	return (Z_OK);
2592766Scarlsonj }
2593766Scarlsonj 
25940Sstevel@tonic-gate static int
25950Sstevel@tonic-gate help_func(int argc, char *argv[])
25960Sstevel@tonic-gate {
25970Sstevel@tonic-gate 	int arg, cmd_num;
25980Sstevel@tonic-gate 
25990Sstevel@tonic-gate 	if (argc == 0) {
26000Sstevel@tonic-gate 		(void) usage(B_TRUE);
26010Sstevel@tonic-gate 		return (Z_OK);
26020Sstevel@tonic-gate 	}
26030Sstevel@tonic-gate 	optind = 0;
26040Sstevel@tonic-gate 	if ((arg = getopt(argc, argv, "?")) != EOF) {
26050Sstevel@tonic-gate 		switch (arg) {
26060Sstevel@tonic-gate 		case '?':
26070Sstevel@tonic-gate 			sub_usage(SHELP_HELP, CMD_HELP);
26080Sstevel@tonic-gate 			return (optopt == '?' ? Z_OK : Z_USAGE);
26090Sstevel@tonic-gate 		default:
26100Sstevel@tonic-gate 			sub_usage(SHELP_HELP, CMD_HELP);
26110Sstevel@tonic-gate 			return (Z_USAGE);
26120Sstevel@tonic-gate 		}
26130Sstevel@tonic-gate 	}
26140Sstevel@tonic-gate 	while (optind < argc) {
26150Sstevel@tonic-gate 		if ((cmd_num = cmd_match(argv[optind])) < 0) {
26160Sstevel@tonic-gate 			sub_usage(SHELP_HELP, CMD_HELP);
26170Sstevel@tonic-gate 			return (Z_USAGE);
26180Sstevel@tonic-gate 		}
26190Sstevel@tonic-gate 		sub_usage(cmdtab[cmd_num].short_usage, cmd_num);
26200Sstevel@tonic-gate 		optind++;
26210Sstevel@tonic-gate 	}
26220Sstevel@tonic-gate 	return (Z_OK);
26230Sstevel@tonic-gate }
26240Sstevel@tonic-gate 
26250Sstevel@tonic-gate /*
26260Sstevel@tonic-gate  * Returns: CMD_MIN thru CMD_MAX on success, -1 on error
26270Sstevel@tonic-gate  */
26280Sstevel@tonic-gate 
26290Sstevel@tonic-gate static int
26300Sstevel@tonic-gate cmd_match(char *cmd)
26310Sstevel@tonic-gate {
26320Sstevel@tonic-gate 	int i;
26330Sstevel@tonic-gate 
26340Sstevel@tonic-gate 	for (i = CMD_MIN; i <= CMD_MAX; i++) {
26350Sstevel@tonic-gate 		/* return only if there is an exact match */
26360Sstevel@tonic-gate 		if (strcmp(cmd, cmdtab[i].cmd_name) == 0)
26370Sstevel@tonic-gate 			return (cmdtab[i].cmd_num);
26380Sstevel@tonic-gate 	}
26390Sstevel@tonic-gate 	return (-1);
26400Sstevel@tonic-gate }
26410Sstevel@tonic-gate 
26420Sstevel@tonic-gate static int
26430Sstevel@tonic-gate parse_and_run(int argc, char *argv[])
26440Sstevel@tonic-gate {
26450Sstevel@tonic-gate 	int i = cmd_match(argv[0]);
26460Sstevel@tonic-gate 
26470Sstevel@tonic-gate 	if (i < 0)
26480Sstevel@tonic-gate 		return (usage(B_FALSE));
26490Sstevel@tonic-gate 	return (cmdtab[i].handler(argc - 1, &(argv[1])));
26500Sstevel@tonic-gate }
26510Sstevel@tonic-gate 
26520Sstevel@tonic-gate static char *
26530Sstevel@tonic-gate get_execbasename(char *execfullname)
26540Sstevel@tonic-gate {
26550Sstevel@tonic-gate 	char *last_slash, *execbasename;
26560Sstevel@tonic-gate 
26570Sstevel@tonic-gate 	/* guard against '/' at end of command invocation */
26580Sstevel@tonic-gate 	for (;;) {
26590Sstevel@tonic-gate 		last_slash = strrchr(execfullname, '/');
26600Sstevel@tonic-gate 		if (last_slash == NULL) {
26610Sstevel@tonic-gate 			execbasename = execfullname;
26620Sstevel@tonic-gate 			break;
26630Sstevel@tonic-gate 		} else {
26640Sstevel@tonic-gate 			execbasename = last_slash + 1;
26650Sstevel@tonic-gate 			if (*execbasename == '\0') {
26660Sstevel@tonic-gate 				*last_slash = '\0';
26670Sstevel@tonic-gate 				continue;
26680Sstevel@tonic-gate 			}
26690Sstevel@tonic-gate 			break;
26700Sstevel@tonic-gate 		}
26710Sstevel@tonic-gate 	}
26720Sstevel@tonic-gate 	return (execbasename);
26730Sstevel@tonic-gate }
26740Sstevel@tonic-gate 
26750Sstevel@tonic-gate int
26760Sstevel@tonic-gate main(int argc, char **argv)
26770Sstevel@tonic-gate {
26780Sstevel@tonic-gate 	int arg;
26790Sstevel@tonic-gate 	zoneid_t zid;
2680766Scarlsonj 	struct stat st;
26810Sstevel@tonic-gate 
26820Sstevel@tonic-gate 	if ((locale = setlocale(LC_ALL, "")) == NULL)
26830Sstevel@tonic-gate 		locale = "C";
26840Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
26850Sstevel@tonic-gate 	setbuf(stdout, NULL);
26860Sstevel@tonic-gate 	(void) sigset(SIGHUP, SIG_IGN);
26870Sstevel@tonic-gate 	execname = get_execbasename(argv[0]);
26880Sstevel@tonic-gate 	target_zone = NULL;
26890Sstevel@tonic-gate 	if (chdir("/") != 0) {
26900Sstevel@tonic-gate 		zerror(gettext("could not change directory to /."));
26910Sstevel@tonic-gate 		exit(Z_ERR);
26920Sstevel@tonic-gate 	}
26930Sstevel@tonic-gate 
2694766Scarlsonj 	while ((arg = getopt(argc, argv, "?z:R:")) != EOF) {
26950Sstevel@tonic-gate 		switch (arg) {
26960Sstevel@tonic-gate 		case '?':
26970Sstevel@tonic-gate 			return (usage(B_TRUE));
26980Sstevel@tonic-gate 		case 'z':
26990Sstevel@tonic-gate 			target_zone = optarg;
27000Sstevel@tonic-gate 			break;
2701766Scarlsonj 		case 'R':	/* private option for admin/install use */
2702766Scarlsonj 			if (*optarg != '/') {
2703766Scarlsonj 				zerror(gettext("root path must be absolute."));
2704766Scarlsonj 				exit(Z_ERR);
2705766Scarlsonj 			}
2706766Scarlsonj 			if (stat(optarg, &st) == -1 || !S_ISDIR(st.st_mode)) {
2707766Scarlsonj 				zerror(
2708766Scarlsonj 				    gettext("root path must be a directory."));
2709766Scarlsonj 				exit(Z_ERR);
2710766Scarlsonj 			}
2711766Scarlsonj 			zonecfg_set_root(optarg);
2712766Scarlsonj 			break;
27130Sstevel@tonic-gate 		default:
27140Sstevel@tonic-gate 			return (usage(B_FALSE));
27150Sstevel@tonic-gate 		}
27160Sstevel@tonic-gate 	}
27170Sstevel@tonic-gate 
27180Sstevel@tonic-gate 	if (optind >= argc)
27190Sstevel@tonic-gate 		return (usage(B_FALSE));
27200Sstevel@tonic-gate 	if (target_zone != NULL && zone_get_id(target_zone, &zid) != 0) {
27210Sstevel@tonic-gate 		errno = Z_NO_ZONE;
27220Sstevel@tonic-gate 		zperror(target_zone, B_TRUE);
27230Sstevel@tonic-gate 		exit(Z_ERR);
27240Sstevel@tonic-gate 	}
27250Sstevel@tonic-gate 	return (parse_and_run(argc - optind, &argv[optind]));
27260Sstevel@tonic-gate }
2727