xref: /illumos-gate/usr/src/cmd/zoneadm/zoneadm.c (revision d9e728a2c2e62adeef072d782e4c8a7b34e7e8e8)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 /*
30  * zoneadm is a command interpreter for zone administration.  It is all in
31  * C (i.e., no lex/yacc), and all the argument passing is argc/argv based.
32  * main() calls parse_and_run() which calls cmd_match(), then invokes the
33  * appropriate command's handler function.  The rest of the program is the
34  * handler functions and their helper functions.
35  *
36  * Some of the helper functions are used largely to simplify I18N: reducing
37  * the need for translation notes.  This is particularly true of many of
38  * the zerror() calls: doing e.g. zerror(gettext("%s failed"), "foo") rather
39  * than zerror(gettext("foo failed")) with a translation note indicating
40  * that "foo" need not be translated.
41  */
42 
43 #include <stdio.h>
44 #include <errno.h>
45 #include <unistd.h>
46 #include <signal.h>
47 #include <stdarg.h>
48 #include <ctype.h>
49 #include <stdlib.h>
50 #include <string.h>
51 #include <wait.h>
52 #include <zone.h>
53 #include <priv.h>
54 #include <locale.h>
55 #include <libintl.h>
56 #include <libzonecfg.h>
57 #include <bsm/adt.h>
58 #include <sys/brand.h>
59 #include <sys/param.h>
60 #include <sys/types.h>
61 #include <sys/stat.h>
62 #include <sys/statvfs.h>
63 #include <assert.h>
64 #include <sys/sockio.h>
65 #include <sys/mntent.h>
66 #include <limits.h>
67 #include <dirent.h>
68 #include <uuid/uuid.h>
69 
70 #include <fcntl.h>
71 #include <door.h>
72 #include <macros.h>
73 #include <libgen.h>
74 #include <fnmatch.h>
75 #include <sys/modctl.h>
76 #include <libbrand.h>
77 #include <libscf.h>
78 #include <procfs.h>
79 #include <strings.h>
80 
81 #include <pool.h>
82 #include <sys/pool.h>
83 #include <sys/priocntl.h>
84 #include <sys/fsspriocntl.h>
85 
86 #include "zoneadm.h"
87 
88 #define	MAXARGS	8
89 
90 /* Reflects kernel zone entries */
91 typedef struct zone_entry {
92 	zoneid_t	zid;
93 	char		zname[ZONENAME_MAX];
94 	char		*zstate_str;
95 	zone_state_t	zstate_num;
96 	char		zbrand[MAXNAMELEN];
97 	char		zroot[MAXPATHLEN];
98 	char		zuuid[UUID_PRINTABLE_STRING_LENGTH];
99 	zone_iptype_t	ziptype;
100 } zone_entry_t;
101 
102 static zone_entry_t *zents;
103 static size_t nzents;
104 static boolean_t is_native_zone = B_TRUE;
105 
106 #define	LOOPBACK_IF	"lo0"
107 #define	SOCKET_AF(af)	(((af) == AF_UNSPEC) ? AF_INET : (af))
108 
109 struct net_if {
110 	char	*name;
111 	int	af;
112 };
113 
114 /* 0755 is the default directory mode. */
115 #define	DEFAULT_DIR_MODE \
116 	(S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH)
117 
118 struct cmd {
119 	uint_t	cmd_num;				/* command number */
120 	char	*cmd_name;				/* command name */
121 	char	*short_usage;				/* short form help */
122 	int	(*handler)(int argc, char *argv[]);	/* function to call */
123 
124 };
125 
126 #define	SHELP_HELP	"help"
127 #define	SHELP_BOOT	"boot [-- boot_arguments]"
128 #define	SHELP_HALT	"halt"
129 #define	SHELP_READY	"ready"
130 #define	SHELP_REBOOT	"reboot [-- boot_arguments]"
131 #define	SHELP_LIST	"list [-cipv]"
132 #define	SHELP_VERIFY	"verify"
133 #define	SHELP_INSTALL	"install [-x nodataset] [brand-specific args]"
134 #define	SHELP_UNINSTALL	"uninstall [-F]"
135 #define	SHELP_CLONE	"clone [-m method] [-s <ZFS snapshot>] zonename"
136 #define	SHELP_MOVE	"move zonepath"
137 #define	SHELP_DETACH	"detach [-n]"
138 #define	SHELP_ATTACH	"attach [-F] [-n <path>]"
139 #define	SHELP_MARK	"mark incomplete"
140 
141 #define	EXEC_PREFIX	"exec "
142 #define	EXEC_LEN	(strlen(EXEC_PREFIX))
143 #define	RMCOMMAND	"/usr/bin/rm -rf"
144 
145 static int cleanup_zonepath(char *, boolean_t);
146 
147 extern int ifname_open(char *);
148 
149 static int help_func(int argc, char *argv[]);
150 static int ready_func(int argc, char *argv[]);
151 static int boot_func(int argc, char *argv[]);
152 static int halt_func(int argc, char *argv[]);
153 static int reboot_func(int argc, char *argv[]);
154 static int list_func(int argc, char *argv[]);
155 static int verify_func(int argc, char *argv[]);
156 static int install_func(int argc, char *argv[]);
157 static int uninstall_func(int argc, char *argv[]);
158 static int mount_func(int argc, char *argv[]);
159 static int unmount_func(int argc, char *argv[]);
160 static int clone_func(int argc, char *argv[]);
161 static int move_func(int argc, char *argv[]);
162 static int detach_func(int argc, char *argv[]);
163 static int attach_func(int argc, char *argv[]);
164 static int mark_func(int argc, char *argv[]);
165 static int apply_func(int argc, char *argv[]);
166 static int sanity_check(char *zone, int cmd_num, boolean_t running,
167     boolean_t unsafe_when_running, boolean_t force);
168 static int cmd_match(char *cmd);
169 static int verify_details(int, char *argv[]);
170 static int verify_brand(zone_dochandle_t, int, char *argv[]);
171 static int invoke_brand_handler(int, char *argv[]);
172 
173 static struct cmd cmdtab[] = {
174 	{ CMD_HELP,		"help",		SHELP_HELP,	help_func },
175 	{ CMD_BOOT,		"boot",		SHELP_BOOT,	boot_func },
176 	{ CMD_HALT,		"halt",		SHELP_HALT,	halt_func },
177 	{ CMD_READY,		"ready",	SHELP_READY,	ready_func },
178 	{ CMD_REBOOT,		"reboot",	SHELP_REBOOT,	reboot_func },
179 	{ CMD_LIST,		"list",		SHELP_LIST,	list_func },
180 	{ CMD_VERIFY,		"verify",	SHELP_VERIFY,	verify_func },
181 	{ CMD_INSTALL,		"install",	SHELP_INSTALL,	install_func },
182 	{ CMD_UNINSTALL,	"uninstall",	SHELP_UNINSTALL,
183 	    uninstall_func },
184 	/* mount and unmount are private commands for admin/install */
185 	{ CMD_MOUNT,		"mount",	NULL,		mount_func },
186 	{ CMD_UNMOUNT,		"unmount",	NULL,		unmount_func },
187 	{ CMD_CLONE,		"clone",	SHELP_CLONE,	clone_func },
188 	{ CMD_MOVE,		"move",		SHELP_MOVE,	move_func },
189 	{ CMD_DETACH,		"detach",	SHELP_DETACH,	detach_func },
190 	{ CMD_ATTACH,		"attach",	SHELP_ATTACH,	attach_func },
191 	{ CMD_MARK,		"mark",		SHELP_MARK,	mark_func },
192 	{ CMD_APPLY,		"apply",	NULL,		apply_func }
193 };
194 
195 /* global variables */
196 
197 /* set early in main(), never modified thereafter, used all over the place */
198 static char *execname;
199 static char target_brand[MAXNAMELEN];
200 static char *locale;
201 char *target_zone;
202 static char *target_uuid;
203 
204 /* used in do_subproc() and signal handler */
205 static volatile boolean_t child_killed;
206 static int do_subproc_cnt = 0;
207 
208 /*
209  * Used to indicate whether this zoneadm instance has another zoneadm
210  * instance in its ancestry.
211  */
212 static boolean_t zoneadm_is_nested = B_FALSE;
213 
214 /* used to track nested zone-lock operations */
215 static int zone_lock_cnt = 0;
216 
217 /* used to communicate lock status to children */
218 #define	LOCK_ENV_VAR	"_ZONEADM_LOCK_HELD"
219 static char zoneadm_lock_held[] = LOCK_ENV_VAR"=1";
220 static char zoneadm_lock_not_held[] = LOCK_ENV_VAR"=0";
221 
222 char *
223 cmd_to_str(int cmd_num)
224 {
225 	assert(cmd_num >= CMD_MIN && cmd_num <= CMD_MAX);
226 	return (cmdtab[cmd_num].cmd_name);
227 }
228 
229 /* This is a separate function because of gettext() wrapping. */
230 static char *
231 long_help(int cmd_num)
232 {
233 	assert(cmd_num >= CMD_MIN && cmd_num <= CMD_MAX);
234 	switch (cmd_num) {
235 	case CMD_HELP:
236 		return (gettext("Print usage message."));
237 	case CMD_BOOT:
238 		return (gettext("Activates (boots) specified zone.  See "
239 		    "zoneadm(1m) for valid boot\n\targuments."));
240 	case CMD_HALT:
241 		return (gettext("Halts specified zone, bypassing shutdown "
242 		    "scripts and removing runtime\n\tresources of the zone."));
243 	case CMD_READY:
244 		return (gettext("Prepares a zone for running applications but "
245 		    "does not start any user\n\tprocesses in the zone."));
246 	case CMD_REBOOT:
247 		return (gettext("Restarts the zone (equivalent to a halt / "
248 		    "boot sequence).\n\tFails if the zone is not active.  "
249 		    "See zoneadm(1m) for valid boot\n\targuments."));
250 	case CMD_LIST:
251 		return (gettext("Lists the current zones, or a "
252 		    "specific zone if indicated.  By default,\n\tall "
253 		    "running zones are listed, though this can be "
254 		    "expanded to all\n\tinstalled zones with the -i "
255 		    "option or all configured zones with the\n\t-c "
256 		    "option.  When used with the general -z <zone> and/or -u "
257 		    "<uuid-match>\n\toptions, lists only the specified "
258 		    "matching zone, but lists it\n\tregardless of its state, "
259 		    "and the -i and -c options are disallowed.  The\n\t-v "
260 		    "option can be used to display verbose information: zone "
261 		    "name, id,\n\tcurrent state, root directory and options.  "
262 		    "The -p option can be used\n\tto request machine-parsable "
263 		    "output.  The -v and -p options are mutually\n\texclusive."
264 		    "  If neither -v nor -p is used, just the zone name is "
265 		    "listed."));
266 	case CMD_VERIFY:
267 		return (gettext("Check to make sure the configuration "
268 		    "can safely be instantiated\n\ton the machine: "
269 		    "physical network interfaces exist, etc."));
270 	case CMD_INSTALL:
271 		return (gettext("Install the configuration on to the system.  "
272 		    "The -x nodataset option\n\tcan be used to prevent the "
273 		    "creation of a new ZFS file system for the\n\tzone "
274 		    "(assuming the zonepath is within a ZFS file system).\n\t"
275 		    "All other arguments are passed to the brand installation "
276 		    "function;\n\tsee brand(4) for more information."));
277 	case CMD_UNINSTALL:
278 		return (gettext("Uninstall the configuration from the system.  "
279 		    "The -F flag can be used\n\tto force the action."));
280 	case CMD_CLONE:
281 		return (gettext("Clone the installation of another zone.  "
282 		    "The -m option can be used to\n\tspecify 'copy' which "
283 		    "forces a copy of the source zone.  The -s option\n\t"
284 		    "can be used to specify the name of a ZFS snapshot "
285 		    "that was taken from\n\ta previous clone command.  The "
286 		    "snapshot will be used as the source\n\tinstead of "
287 		    "creating a new ZFS snapshot."));
288 	case CMD_MOVE:
289 		return (gettext("Move the zone to a new zonepath."));
290 	case CMD_DETACH:
291 		return (gettext("Detach the zone from the system. The zone "
292 		    "state is changed to\n\t'configured' (but the files under "
293 		    "the zonepath are untouched).\n\tThe zone can subsequently "
294 		    "be attached, or can be moved to another\n\tsystem and "
295 		    "attached there.  The -n option can be used to specify\n\t"
296 		    "'no-execute' mode.  When -n is used, the information "
297 		    "needed to attach\n\tthe zone is sent to standard output "
298 		    "but the zone is not actually\n\tdetached."));
299 	case CMD_ATTACH:
300 		return (gettext("Attach the zone to the system.  The zone "
301 		    "state must be 'configured'\n\tprior to attach; upon "
302 		    "successful completion, the zone state will be\n\t"
303 		    "'installed'.  The system software on the current "
304 		    "system must be\n\tcompatible with the software on the "
305 		    "zone's original system.\n\tSpecify -F to force the attach "
306 		    "and skip software compatibility tests.\n\tThe -n option "
307 		    "can be used to specify 'no-execute' mode.  When -n is\n\t"
308 		    "used, the information needed to attach the zone is read "
309 		    "from the\n\tspecified path and the configuration is only "
310 		    "validated.  The path can\n\tbe '-' to specify standard "
311 		    "input."));
312 	case CMD_MARK:
313 		return (gettext("Set the state of the zone.  This can be used "
314 		    "to force the zone\n\tstate to 'incomplete' "
315 		    "administratively if some activity has rendered\n\tthe "
316 		    "zone permanently unusable.  The only valid state that "
317 		    "may be\n\tspecified is 'incomplete'."));
318 	default:
319 		return ("");
320 	}
321 	/* NOTREACHED */
322 	return (NULL);
323 }
324 
325 /*
326  * Called with explicit B_TRUE when help is explicitly requested, B_FALSE for
327  * unexpected errors.
328  */
329 
330 static int
331 usage(boolean_t explicit)
332 {
333 	int i;
334 	FILE *fd = explicit ? stdout : stderr;
335 
336 	(void) fprintf(fd, "%s:\t%s help\n", gettext("usage"), execname);
337 	(void) fprintf(fd, "\t%s [-z <zone>] [-u <uuid-match>] list\n",
338 	    execname);
339 	(void) fprintf(fd, "\t%s {-z <zone>|-u <uuid-match>} <%s>\n", execname,
340 	    gettext("subcommand"));
341 	(void) fprintf(fd, "\n%s:\n\n", gettext("Subcommands"));
342 	for (i = CMD_MIN; i <= CMD_MAX; i++) {
343 		if (cmdtab[i].short_usage == NULL)
344 			continue;
345 		(void) fprintf(fd, "%s\n", cmdtab[i].short_usage);
346 		if (explicit)
347 			(void) fprintf(fd, "\t%s\n\n", long_help(i));
348 	}
349 	if (!explicit)
350 		(void) fputs("\n", fd);
351 	return (Z_USAGE);
352 }
353 
354 static void
355 sub_usage(char *short_usage, int cmd_num)
356 {
357 	(void) fprintf(stderr, "%s:\t%s\n", gettext("usage"), short_usage);
358 	(void) fprintf(stderr, "\t%s\n", long_help(cmd_num));
359 }
360 
361 /*
362  * zperror() is like perror(3c) except that this also prints the executable
363  * name at the start of the message, and takes a boolean indicating whether
364  * to call libc'c strerror() or that from libzonecfg.
365  */
366 
367 void
368 zperror(const char *str, boolean_t zonecfg_error)
369 {
370 	(void) fprintf(stderr, "%s: %s: %s\n", execname, str,
371 	    zonecfg_error ? zonecfg_strerror(errno) : strerror(errno));
372 }
373 
374 /*
375  * zperror2() is very similar to zperror() above, except it also prints a
376  * supplied zone name after the executable.
377  *
378  * All current consumers of this function want libzonecfg's strerror() rather
379  * than libc's; if this ever changes, this function can be made more generic
380  * like zperror() above.
381  */
382 
383 void
384 zperror2(const char *zone, const char *str)
385 {
386 	(void) fprintf(stderr, "%s: %s: %s: %s\n", execname, zone, str,
387 	    zonecfg_strerror(errno));
388 }
389 
390 /* PRINTFLIKE1 */
391 void
392 zerror(const char *fmt, ...)
393 {
394 	va_list alist;
395 
396 	va_start(alist, fmt);
397 	(void) fprintf(stderr, "%s: ", execname);
398 	if (target_zone != NULL)
399 		(void) fprintf(stderr, "zone '%s': ", target_zone);
400 	(void) vfprintf(stderr, fmt, alist);
401 	(void) fprintf(stderr, "\n");
402 	va_end(alist);
403 }
404 
405 static void *
406 safe_calloc(size_t nelem, size_t elsize)
407 {
408 	void *r = calloc(nelem, elsize);
409 
410 	if (r == NULL) {
411 		zerror(gettext("failed to allocate %lu bytes: %s"),
412 		    (ulong_t)nelem * elsize, strerror(errno));
413 		exit(Z_ERR);
414 	}
415 	return (r);
416 }
417 
418 static void
419 zone_print(zone_entry_t *zent, boolean_t verbose, boolean_t parsable)
420 {
421 	static boolean_t firsttime = B_TRUE;
422 	char *ip_type_str;
423 
424 	if (zent->ziptype == ZS_EXCLUSIVE)
425 		ip_type_str = "excl";
426 	else
427 		ip_type_str = "shared";
428 
429 	assert(!(verbose && parsable));
430 	if (firsttime && verbose) {
431 		firsttime = B_FALSE;
432 		(void) printf("%*s %-16s %-10s %-30s %-8s %-6s\n",
433 		    ZONEID_WIDTH, "ID", "NAME", "STATUS", "PATH", "BRAND",
434 		    "IP");
435 	}
436 	if (!verbose) {
437 		char *cp, *clim;
438 
439 		if (!parsable) {
440 			(void) printf("%s\n", zent->zname);
441 			return;
442 		}
443 		if (zent->zid == ZONE_ID_UNDEFINED)
444 			(void) printf("-");
445 		else
446 			(void) printf("%lu", zent->zid);
447 		(void) printf(":%s:%s:", zent->zname, zent->zstate_str);
448 		cp = zent->zroot;
449 		while ((clim = strchr(cp, ':')) != NULL) {
450 			(void) printf("%.*s\\:", clim - cp, cp);
451 			cp = clim + 1;
452 		}
453 		(void) printf("%s:%s:%s:%s\n", cp, zent->zuuid, zent->zbrand,
454 		    ip_type_str);
455 		return;
456 	}
457 	if (zent->zstate_str != NULL) {
458 		if (zent->zid == ZONE_ID_UNDEFINED)
459 			(void) printf("%*s", ZONEID_WIDTH, "-");
460 		else
461 			(void) printf("%*lu", ZONEID_WIDTH, zent->zid);
462 		(void) printf(" %-16s %-10s %-30s %-8s %-6s\n", zent->zname,
463 		    zent->zstate_str, zent->zroot, zent->zbrand, ip_type_str);
464 	}
465 }
466 
467 static int
468 lookup_zone_info(const char *zone_name, zoneid_t zid, zone_entry_t *zent)
469 {
470 	char root[MAXPATHLEN], *cp;
471 	int err;
472 	uuid_t uuid;
473 
474 	(void) strlcpy(zent->zname, zone_name, sizeof (zent->zname));
475 	(void) strlcpy(zent->zroot, "???", sizeof (zent->zroot));
476 	(void) strlcpy(zent->zbrand, "???", sizeof (zent->zbrand));
477 	zent->zstate_str = "???";
478 
479 	zent->zid = zid;
480 
481 	if (zonecfg_get_uuid(zone_name, uuid) == Z_OK &&
482 	    !uuid_is_null(uuid))
483 		uuid_unparse(uuid, zent->zuuid);
484 	else
485 		zent->zuuid[0] = '\0';
486 
487 	/*
488 	 * For labeled zones which query the zone path of lower-level
489 	 * zones, the path needs to be adjusted to drop the final
490 	 * "/root" component. This adjusted path is then useful
491 	 * for reading down any exported directories from the
492 	 * lower-level zone.
493 	 */
494 	if (is_system_labeled() && zent->zid != ZONE_ID_UNDEFINED) {
495 		if (zone_getattr(zent->zid, ZONE_ATTR_ROOT, zent->zroot,
496 		    sizeof (zent->zroot)) == -1) {
497 			zperror2(zent->zname,
498 			    gettext("could not get zone path."));
499 			return (Z_ERR);
500 		}
501 		cp = zent->zroot + strlen(zent->zroot) - 5;
502 		if (cp > zent->zroot && strcmp(cp, "/root") == 0)
503 			*cp = 0;
504 	} else {
505 		if ((err = zone_get_zonepath(zent->zname, root,
506 		    sizeof (root))) != Z_OK) {
507 			errno = err;
508 			zperror2(zent->zname,
509 			    gettext("could not get zone path."));
510 			return (Z_ERR);
511 		}
512 		(void) strlcpy(zent->zroot, root, sizeof (zent->zroot));
513 	}
514 
515 	if ((err = zone_get_state(zent->zname, &zent->zstate_num)) != Z_OK) {
516 		errno = err;
517 		zperror2(zent->zname, gettext("could not get state"));
518 		return (Z_ERR);
519 	}
520 	zent->zstate_str = zone_state_str(zent->zstate_num);
521 
522 	/*
523 	 * A zone's brand is only available in the .xml file describing it,
524 	 * which is only visible to the global zone.  This causes
525 	 * zone_get_brand() to fail when called from within a non-global
526 	 * zone.  Fortunately we only do this on labeled systems, where we
527 	 * know all zones are native.
528 	 */
529 	if (getzoneid() != GLOBAL_ZONEID) {
530 		assert(is_system_labeled() != 0);
531 		(void) strlcpy(zent->zbrand, NATIVE_BRAND_NAME,
532 		    sizeof (zent->zbrand));
533 	} else if (zone_get_brand(zent->zname, zent->zbrand,
534 	    sizeof (zent->zbrand)) != Z_OK) {
535 		zperror2(zent->zname, gettext("could not get brand name"));
536 		return (Z_ERR);
537 	}
538 
539 	/*
540 	 * Get ip type of the zone.
541 	 * Note for global zone, ZS_SHARED is set always.
542 	 */
543 	if (zid == GLOBAL_ZONEID) {
544 		zent->ziptype = ZS_SHARED;
545 	} else {
546 
547 		if (zent->zstate_num == ZONE_STATE_RUNNING) {
548 			ushort_t flags;
549 
550 			if (zone_getattr(zid, ZONE_ATTR_FLAGS, &flags,
551 			    sizeof (flags)) < 0) {
552 				zperror2(zent->zname,
553 				    gettext("could not get zone flags"));
554 				return (Z_ERR);
555 			}
556 			if (flags & ZF_NET_EXCL)
557 				zent->ziptype = ZS_EXCLUSIVE;
558 			else
559 				zent->ziptype = ZS_SHARED;
560 		} else {
561 			zone_dochandle_t handle;
562 
563 			if ((handle = zonecfg_init_handle()) == NULL) {
564 				zperror2(zent->zname,
565 				    gettext("could not init handle"));
566 				return (Z_ERR);
567 			}
568 			if ((err = zonecfg_get_handle(zent->zname, handle))
569 			    != Z_OK) {
570 				zperror2(zent->zname,
571 				    gettext("could not get handle"));
572 				zonecfg_fini_handle(handle);
573 				return (Z_ERR);
574 			}
575 
576 			if ((err = zonecfg_get_iptype(handle, &zent->ziptype))
577 			    != Z_OK) {
578 				zperror2(zent->zname,
579 				    gettext("could not get ip-type"));
580 				zonecfg_fini_handle(handle);
581 				return (Z_ERR);
582 			}
583 			zonecfg_fini_handle(handle);
584 		}
585 	}
586 
587 	return (Z_OK);
588 }
589 
590 /*
591  * fetch_zents() calls zone_list(2) to find out how many zones are running
592  * (which is stored in the global nzents), then calls zone_list(2) again
593  * to fetch the list of running zones (stored in the global zents).  This
594  * function may be called multiple times, so if zents is already set, we
595  * return immediately to save work.
596  */
597 
598 static int
599 fetch_zents(void)
600 {
601 	zoneid_t *zids = NULL;
602 	uint_t nzents_saved;
603 	int i, retv;
604 	FILE *fp;
605 	boolean_t inaltroot;
606 	zone_entry_t *zentp;
607 
608 	if (nzents > 0)
609 		return (Z_OK);
610 
611 	if (zone_list(NULL, &nzents) != 0) {
612 		zperror(gettext("failed to get zoneid list"), B_FALSE);
613 		return (Z_ERR);
614 	}
615 
616 again:
617 	if (nzents == 0)
618 		return (Z_OK);
619 
620 	zids = safe_calloc(nzents, sizeof (zoneid_t));
621 	nzents_saved = nzents;
622 
623 	if (zone_list(zids, &nzents) != 0) {
624 		zperror(gettext("failed to get zone list"), B_FALSE);
625 		free(zids);
626 		return (Z_ERR);
627 	}
628 	if (nzents != nzents_saved) {
629 		/* list changed, try again */
630 		free(zids);
631 		goto again;
632 	}
633 
634 	zents = safe_calloc(nzents, sizeof (zone_entry_t));
635 
636 	inaltroot = zonecfg_in_alt_root();
637 	if (inaltroot)
638 		fp = zonecfg_open_scratch("", B_FALSE);
639 	else
640 		fp = NULL;
641 	zentp = zents;
642 	retv = Z_OK;
643 	for (i = 0; i < nzents; i++) {
644 		char name[ZONENAME_MAX];
645 		char altname[ZONENAME_MAX];
646 
647 		if (getzonenamebyid(zids[i], name, sizeof (name)) < 0) {
648 			zperror(gettext("failed to get zone name"), B_FALSE);
649 			retv = Z_ERR;
650 			continue;
651 		}
652 		if (zonecfg_is_scratch(name)) {
653 			/* Ignore scratch zones by default */
654 			if (!inaltroot)
655 				continue;
656 			if (fp == NULL ||
657 			    zonecfg_reverse_scratch(fp, name, altname,
658 			    sizeof (altname), NULL, 0) == -1) {
659 				zerror(gettext("could not resolve scratch "
660 				    "zone %s"), name);
661 				retv = Z_ERR;
662 				continue;
663 			}
664 			(void) strcpy(name, altname);
665 		} else {
666 			/* Ignore non-scratch when in an alternate root */
667 			if (inaltroot && strcmp(name, GLOBAL_ZONENAME) != 0)
668 				continue;
669 		}
670 		if (lookup_zone_info(name, zids[i], zentp) != Z_OK) {
671 			zerror(gettext("failed to get zone data"));
672 			retv = Z_ERR;
673 			continue;
674 		}
675 		zentp++;
676 	}
677 	nzents = zentp - zents;
678 	if (fp != NULL)
679 		zonecfg_close_scratch(fp);
680 
681 	free(zids);
682 	return (retv);
683 }
684 
685 static int
686 zone_print_list(zone_state_t min_state, boolean_t verbose, boolean_t parsable)
687 {
688 	int i;
689 	zone_entry_t zent;
690 	FILE *cookie;
691 	char *name;
692 
693 	/*
694 	 * First get the list of running zones from the kernel and print them.
695 	 * If that is all we need, then return.
696 	 */
697 	if ((i = fetch_zents()) != Z_OK) {
698 		/*
699 		 * No need for error messages; fetch_zents() has already taken
700 		 * care of this.
701 		 */
702 		return (i);
703 	}
704 	for (i = 0; i < nzents; i++)
705 		zone_print(&zents[i], verbose, parsable);
706 	if (min_state >= ZONE_STATE_RUNNING)
707 		return (Z_OK);
708 	/*
709 	 * Next, get the full list of zones from the configuration, skipping
710 	 * any we have already printed.
711 	 */
712 	cookie = setzoneent();
713 	while ((name = getzoneent(cookie)) != NULL) {
714 		for (i = 0; i < nzents; i++) {
715 			if (strcmp(zents[i].zname, name) == 0)
716 				break;
717 		}
718 		if (i < nzents) {
719 			free(name);
720 			continue;
721 		}
722 		if (lookup_zone_info(name, ZONE_ID_UNDEFINED, &zent) != Z_OK) {
723 			free(name);
724 			continue;
725 		}
726 		free(name);
727 		if (zent.zstate_num >= min_state)
728 			zone_print(&zent, verbose, parsable);
729 	}
730 	endzoneent(cookie);
731 	return (Z_OK);
732 }
733 
734 static zone_entry_t *
735 lookup_running_zone(char *str)
736 {
737 	zoneid_t zoneid;
738 	char *cp;
739 	int i;
740 
741 	if (fetch_zents() != Z_OK)
742 		return (NULL);
743 
744 	for (i = 0; i < nzents; i++) {
745 		if (strcmp(str, zents[i].zname) == 0)
746 			return (&zents[i]);
747 	}
748 	errno = 0;
749 	zoneid = strtol(str, &cp, 0);
750 	if (zoneid < MIN_ZONEID || zoneid > MAX_ZONEID ||
751 	    errno != 0 || *cp != '\0')
752 		return (NULL);
753 	for (i = 0; i < nzents; i++) {
754 		if (zoneid == zents[i].zid)
755 			return (&zents[i]);
756 	}
757 	return (NULL);
758 }
759 
760 /*
761  * Check a bit in a mode_t: if on is B_TRUE, that bit should be on; if
762  * B_FALSE, it should be off.  Return B_TRUE if the mode is bad (incorrect).
763  */
764 static boolean_t
765 bad_mode_bit(mode_t mode, mode_t bit, boolean_t on, char *file)
766 {
767 	char *str;
768 
769 	assert(bit == S_IRUSR || bit == S_IWUSR || bit == S_IXUSR ||
770 	    bit == S_IRGRP || bit == S_IWGRP || bit == S_IXGRP ||
771 	    bit == S_IROTH || bit == S_IWOTH || bit == S_IXOTH);
772 	/*
773 	 * TRANSLATION_NOTE
774 	 * The strings below will be used as part of a larger message,
775 	 * either:
776 	 * (file name) must be (owner|group|world) (read|writ|execut)able
777 	 * or
778 	 * (file name) must not be (owner|group|world) (read|writ|execut)able
779 	 */
780 	switch (bit) {
781 	case S_IRUSR:
782 		str = gettext("owner readable");
783 		break;
784 	case S_IWUSR:
785 		str = gettext("owner writable");
786 		break;
787 	case S_IXUSR:
788 		str = gettext("owner executable");
789 		break;
790 	case S_IRGRP:
791 		str = gettext("group readable");
792 		break;
793 	case S_IWGRP:
794 		str = gettext("group writable");
795 		break;
796 	case S_IXGRP:
797 		str = gettext("group executable");
798 		break;
799 	case S_IROTH:
800 		str = gettext("world readable");
801 		break;
802 	case S_IWOTH:
803 		str = gettext("world writable");
804 		break;
805 	case S_IXOTH:
806 		str = gettext("world executable");
807 		break;
808 	}
809 	if ((mode & bit) == (on ? 0 : bit)) {
810 		/*
811 		 * TRANSLATION_NOTE
812 		 * The first parameter below is a file name; the second
813 		 * is one of the "(owner|group|world) (read|writ|execut)able"
814 		 * strings from above.
815 		 */
816 		/*
817 		 * The code below could be simplified but not in a way
818 		 * that would easily translate to non-English locales.
819 		 */
820 		if (on) {
821 			(void) fprintf(stderr, gettext("%s must be %s.\n"),
822 			    file, str);
823 		} else {
824 			(void) fprintf(stderr, gettext("%s must not be %s.\n"),
825 			    file, str);
826 		}
827 		return (B_TRUE);
828 	}
829 	return (B_FALSE);
830 }
831 
832 /*
833  * We want to make sure that no zone has its zone path as a child node
834  * (in the directory sense) of any other.  We do that by comparing this
835  * zone's path to the path of all other (non-global) zones.  The comparison
836  * in each case is simple: add '/' to the end of the path, then do a
837  * strncmp() of the two paths, using the length of the shorter one.
838  */
839 
840 static int
841 crosscheck_zonepaths(char *path)
842 {
843 	char rpath[MAXPATHLEN];		/* resolved path */
844 	char path_copy[MAXPATHLEN];	/* copy of original path */
845 	char rpath_copy[MAXPATHLEN];	/* copy of original rpath */
846 	struct zoneent *ze;
847 	int res, err;
848 	FILE *cookie;
849 
850 	cookie = setzoneent();
851 	while ((ze = getzoneent_private(cookie)) != NULL) {
852 		/* Skip zones which are not installed. */
853 		if (ze->zone_state < ZONE_STATE_INSTALLED) {
854 			free(ze);
855 			continue;
856 		}
857 		/* Skip the global zone and the current target zone. */
858 		if (strcmp(ze->zone_name, GLOBAL_ZONENAME) == 0 ||
859 		    strcmp(ze->zone_name, target_zone) == 0) {
860 			free(ze);
861 			continue;
862 		}
863 		if (strlen(ze->zone_path) == 0) {
864 			/* old index file without path, fall back */
865 			if ((err = zone_get_zonepath(ze->zone_name,
866 			    ze->zone_path, sizeof (ze->zone_path))) != Z_OK) {
867 				errno = err;
868 				zperror2(ze->zone_name,
869 				    gettext("could not get zone path"));
870 				free(ze);
871 				continue;
872 			}
873 		}
874 		(void) snprintf(path_copy, sizeof (path_copy), "%s%s",
875 		    zonecfg_get_root(), ze->zone_path);
876 		res = resolvepath(path_copy, rpath, sizeof (rpath));
877 		if (res == -1) {
878 			if (errno != ENOENT) {
879 				zperror(path_copy, B_FALSE);
880 				free(ze);
881 				return (Z_ERR);
882 			}
883 			(void) printf(gettext("WARNING: zone %s is installed, "
884 			    "but its %s %s does not exist.\n"), ze->zone_name,
885 			    "zonepath", path_copy);
886 			free(ze);
887 			continue;
888 		}
889 		rpath[res] = '\0';
890 		(void) snprintf(path_copy, sizeof (path_copy), "%s/", path);
891 		(void) snprintf(rpath_copy, sizeof (rpath_copy), "%s/", rpath);
892 		if (strncmp(path_copy, rpath_copy,
893 		    min(strlen(path_copy), strlen(rpath_copy))) == 0) {
894 			/*
895 			 * TRANSLATION_NOTE
896 			 * zonepath is a literal that should not be translated.
897 			 */
898 			(void) fprintf(stderr, gettext("%s zonepath (%s) and "
899 			    "%s zonepath (%s) overlap.\n"),
900 			    target_zone, path, ze->zone_name, rpath);
901 			free(ze);
902 			return (Z_ERR);
903 		}
904 		free(ze);
905 	}
906 	endzoneent(cookie);
907 	return (Z_OK);
908 }
909 
910 static int
911 validate_zonepath(char *path, int cmd_num)
912 {
913 	int res;			/* result of last library/system call */
914 	boolean_t err = B_FALSE;	/* have we run into an error? */
915 	struct stat stbuf;
916 	struct statvfs64 vfsbuf;
917 	char rpath[MAXPATHLEN];		/* resolved path */
918 	char ppath[MAXPATHLEN];		/* parent path */
919 	char rppath[MAXPATHLEN];	/* resolved parent path */
920 	char rootpath[MAXPATHLEN];	/* root path */
921 	zone_state_t state;
922 
923 	if (path[0] != '/') {
924 		(void) fprintf(stderr,
925 		    gettext("%s is not an absolute path.\n"), path);
926 		return (Z_ERR);
927 	}
928 	if ((res = resolvepath(path, rpath, sizeof (rpath))) == -1) {
929 		if ((errno != ENOENT) ||
930 		    (cmd_num != CMD_VERIFY && cmd_num != CMD_INSTALL &&
931 		    cmd_num != CMD_CLONE && cmd_num != CMD_MOVE)) {
932 			zperror(path, B_FALSE);
933 			return (Z_ERR);
934 		}
935 		if (cmd_num == CMD_VERIFY) {
936 			/*
937 			 * TRANSLATION_NOTE
938 			 * zoneadm is a literal that should not be translated.
939 			 */
940 			(void) fprintf(stderr, gettext("WARNING: %s does not "
941 			    "exist, so it could not be verified.\nWhen "
942 			    "'zoneadm %s' is run, '%s' will try to create\n%s, "
943 			    "and '%s' will be tried again,\nbut the '%s' may "
944 			    "fail if:\nthe parent directory of %s is group- or "
945 			    "other-writable\nor\n%s overlaps with any other "
946 			    "installed zones.\n"), path,
947 			    cmd_to_str(CMD_INSTALL), cmd_to_str(CMD_INSTALL),
948 			    path, cmd_to_str(CMD_VERIFY),
949 			    cmd_to_str(CMD_VERIFY), path, path);
950 			return (Z_OK);
951 		}
952 		/*
953 		 * The zonepath is supposed to be mode 700 but its
954 		 * parent(s) 755.  So use 755 on the mkdirp() then
955 		 * chmod() the zonepath itself to 700.
956 		 */
957 		if (mkdirp(path, DEFAULT_DIR_MODE) < 0) {
958 			zperror(path, B_FALSE);
959 			return (Z_ERR);
960 		}
961 		/*
962 		 * If the chmod() fails, report the error, but might
963 		 * as well continue the verify procedure.
964 		 */
965 		if (chmod(path, S_IRWXU) != 0)
966 			zperror(path, B_FALSE);
967 		/*
968 		 * Since the mkdir() succeeded, we should not have to
969 		 * worry about a subsequent ENOENT, thus this should
970 		 * only recurse once.
971 		 */
972 		return (validate_zonepath(path, cmd_num));
973 	}
974 	rpath[res] = '\0';
975 	if (strcmp(path, rpath) != 0) {
976 		errno = Z_RESOLVED_PATH;
977 		zperror(path, B_TRUE);
978 		return (Z_ERR);
979 	}
980 	if ((res = stat(rpath, &stbuf)) != 0) {
981 		zperror(rpath, B_FALSE);
982 		return (Z_ERR);
983 	}
984 	if (!S_ISDIR(stbuf.st_mode)) {
985 		(void) fprintf(stderr, gettext("%s is not a directory.\n"),
986 		    rpath);
987 		return (Z_ERR);
988 	}
989 	if (strcmp(stbuf.st_fstype, MNTTYPE_TMPFS) == 0) {
990 		(void) printf(gettext("WARNING: %s is on a temporary "
991 		    "file system.\n"), rpath);
992 	}
993 	if (crosscheck_zonepaths(rpath) != Z_OK)
994 		return (Z_ERR);
995 	/*
996 	 * Try to collect and report as many minor errors as possible
997 	 * before returning, so the user can learn everything that needs
998 	 * to be fixed up front.
999 	 */
1000 	if (stbuf.st_uid != 0) {
1001 		(void) fprintf(stderr, gettext("%s is not owned by root.\n"),
1002 		    rpath);
1003 		err = B_TRUE;
1004 	}
1005 	err |= bad_mode_bit(stbuf.st_mode, S_IRUSR, B_TRUE, rpath);
1006 	err |= bad_mode_bit(stbuf.st_mode, S_IWUSR, B_TRUE, rpath);
1007 	err |= bad_mode_bit(stbuf.st_mode, S_IXUSR, B_TRUE, rpath);
1008 	err |= bad_mode_bit(stbuf.st_mode, S_IRGRP, B_FALSE, rpath);
1009 	err |= bad_mode_bit(stbuf.st_mode, S_IWGRP, B_FALSE, rpath);
1010 	err |= bad_mode_bit(stbuf.st_mode, S_IXGRP, B_FALSE, rpath);
1011 	err |= bad_mode_bit(stbuf.st_mode, S_IROTH, B_FALSE, rpath);
1012 	err |= bad_mode_bit(stbuf.st_mode, S_IWOTH, B_FALSE, rpath);
1013 	err |= bad_mode_bit(stbuf.st_mode, S_IXOTH, B_FALSE, rpath);
1014 
1015 	(void) snprintf(ppath, sizeof (ppath), "%s/..", path);
1016 	if ((res = resolvepath(ppath, rppath, sizeof (rppath))) == -1) {
1017 		zperror(ppath, B_FALSE);
1018 		return (Z_ERR);
1019 	}
1020 	rppath[res] = '\0';
1021 	if ((res = stat(rppath, &stbuf)) != 0) {
1022 		zperror(rppath, B_FALSE);
1023 		return (Z_ERR);
1024 	}
1025 	/* theoretically impossible */
1026 	if (!S_ISDIR(stbuf.st_mode)) {
1027 		(void) fprintf(stderr, gettext("%s is not a directory.\n"),
1028 		    rppath);
1029 		return (Z_ERR);
1030 	}
1031 	if (stbuf.st_uid != 0) {
1032 		(void) fprintf(stderr, gettext("%s is not owned by root.\n"),
1033 		    rppath);
1034 		err = B_TRUE;
1035 	}
1036 	err |= bad_mode_bit(stbuf.st_mode, S_IRUSR, B_TRUE, rppath);
1037 	err |= bad_mode_bit(stbuf.st_mode, S_IWUSR, B_TRUE, rppath);
1038 	err |= bad_mode_bit(stbuf.st_mode, S_IXUSR, B_TRUE, rppath);
1039 	err |= bad_mode_bit(stbuf.st_mode, S_IWGRP, B_FALSE, rppath);
1040 	err |= bad_mode_bit(stbuf.st_mode, S_IWOTH, B_FALSE, rppath);
1041 	if (strcmp(rpath, rppath) == 0) {
1042 		(void) fprintf(stderr, gettext("%s is its own parent.\n"),
1043 		    rppath);
1044 		err = B_TRUE;
1045 	}
1046 
1047 	if (statvfs64(rpath, &vfsbuf) != 0) {
1048 		zperror(rpath, B_FALSE);
1049 		return (Z_ERR);
1050 	}
1051 	if (strcmp(vfsbuf.f_basetype, MNTTYPE_NFS) == 0) {
1052 		/*
1053 		 * TRANSLATION_NOTE
1054 		 * Zonepath and NFS are literals that should not be translated.
1055 		 */
1056 		(void) fprintf(stderr, gettext("Zonepath %s is on an NFS "
1057 		    "mounted file system.\n"
1058 		    "\tA local file system must be used.\n"), rpath);
1059 		return (Z_ERR);
1060 	}
1061 	if (vfsbuf.f_flag & ST_NOSUID) {
1062 		/*
1063 		 * TRANSLATION_NOTE
1064 		 * Zonepath and nosuid are literals that should not be
1065 		 * translated.
1066 		 */
1067 		(void) fprintf(stderr, gettext("Zonepath %s is on a nosuid "
1068 		    "file system.\n"), rpath);
1069 		return (Z_ERR);
1070 	}
1071 
1072 	if ((res = zone_get_state(target_zone, &state)) != Z_OK) {
1073 		errno = res;
1074 		zperror2(target_zone, gettext("could not get state"));
1075 		return (Z_ERR);
1076 	}
1077 	/*
1078 	 * The existence of the root path is only bad in the configured state,
1079 	 * as it is *supposed* to be there at the installed and later states.
1080 	 * However, the root path is expected to be there if the zone is
1081 	 * detached.
1082 	 * State/command mismatches are caught earlier in verify_details().
1083 	 */
1084 	if (state == ZONE_STATE_CONFIGURED && cmd_num != CMD_ATTACH) {
1085 		if (snprintf(rootpath, sizeof (rootpath), "%s/root", rpath) >=
1086 		    sizeof (rootpath)) {
1087 			/*
1088 			 * TRANSLATION_NOTE
1089 			 * Zonepath is a literal that should not be translated.
1090 			 */
1091 			(void) fprintf(stderr,
1092 			    gettext("Zonepath %s is too long.\n"), rpath);
1093 			return (Z_ERR);
1094 		}
1095 		if ((res = stat(rootpath, &stbuf)) == 0) {
1096 			if (zonecfg_detached(rpath))
1097 				(void) fprintf(stderr,
1098 				    gettext("Cannot %s detached "
1099 				    "zone.\nUse attach or remove %s "
1100 				    "directory.\n"), cmd_to_str(cmd_num),
1101 				    rpath);
1102 			else
1103 				(void) fprintf(stderr,
1104 				    gettext("Rootpath %s exists; "
1105 				    "remove or move aside prior to %s.\n"),
1106 				    rootpath, cmd_to_str(cmd_num));
1107 			return (Z_ERR);
1108 		}
1109 	}
1110 
1111 	return (err ? Z_ERR : Z_OK);
1112 }
1113 
1114 /*
1115  * The following two routines implement a simple locking mechanism to
1116  * ensure that only one instance of zoneadm at a time is able to manipulate
1117  * a given zone.  The lock is built on top of an fcntl(2) lock of
1118  * [<altroot>]/var/run/zones/<zonename>.zoneadm.lock.  If a zoneadm instance
1119  * can grab that lock, it is allowed to manipulate the zone.
1120  *
1121  * Since zoneadm may call external applications which in turn invoke
1122  * zoneadm again, we introduce the notion of "lock inheritance".  Any
1123  * instance of zoneadm that has another instance in its ancestry is assumed
1124  * to be acting on behalf of the original zoneadm, and is thus allowed to
1125  * manipulate its zone.
1126  *
1127  * This inheritance is implemented via the _ZONEADM_LOCK_HELD environment
1128  * variable.  When zoneadm is granted a lock on its zone, this environment
1129  * variable is set to 1.  When it releases the lock, the variable is set to
1130  * 0.  Since a child process inherits its parent's environment, checking
1131  * the state of this variable indicates whether or not any ancestor owns
1132  * the lock.
1133  */
1134 static void
1135 release_lock_file(int lockfd)
1136 {
1137 	/*
1138 	 * If we are cleaning up from a failed attempt to lock the zone for
1139 	 * the first time, we might have a zone_lock_cnt of 0.  In that
1140 	 * error case, we don't want to do anything but close the lock
1141 	 * file.
1142 	 */
1143 	assert(zone_lock_cnt >= 0);
1144 	if (zone_lock_cnt > 0) {
1145 		assert(getenv(LOCK_ENV_VAR) != NULL);
1146 		assert(atoi(getenv(LOCK_ENV_VAR)) == 1);
1147 		if (--zone_lock_cnt > 0) {
1148 			assert(lockfd == -1);
1149 			return;
1150 		}
1151 		if (putenv(zoneadm_lock_not_held) != 0) {
1152 			zperror(target_zone, B_TRUE);
1153 			exit(Z_ERR);
1154 		}
1155 	}
1156 	assert(lockfd >= 0);
1157 	(void) close(lockfd);
1158 }
1159 
1160 static int
1161 grab_lock_file(const char *zone_name, int *lockfd)
1162 {
1163 	char pathbuf[PATH_MAX];
1164 	struct flock flock;
1165 
1166 	/*
1167 	 * If we already have the lock, we can skip this expensive song
1168 	 * and dance.
1169 	 */
1170 	if (zone_lock_cnt > 0) {
1171 		zone_lock_cnt++;
1172 		*lockfd = -1;
1173 		return (Z_OK);
1174 	}
1175 	assert(getenv(LOCK_ENV_VAR) != NULL);
1176 	assert(atoi(getenv(LOCK_ENV_VAR)) == 0);
1177 
1178 	if (snprintf(pathbuf, sizeof (pathbuf), "%s%s", zonecfg_get_root(),
1179 	    ZONES_TMPDIR) >= sizeof (pathbuf)) {
1180 		zerror(gettext("alternate root path is too long"));
1181 		return (Z_ERR);
1182 	}
1183 	if (mkdir(pathbuf, S_IRWXU) < 0 && errno != EEXIST) {
1184 		zerror(gettext("could not mkdir %s: %s"), pathbuf,
1185 		    strerror(errno));
1186 		return (Z_ERR);
1187 	}
1188 	(void) chmod(pathbuf, S_IRWXU);
1189 
1190 	/*
1191 	 * One of these lock files is created for each zone (when needed).
1192 	 * The lock files are not cleaned up (except on system reboot),
1193 	 * but since there is only one per zone, there is no resource
1194 	 * starvation issue.
1195 	 */
1196 	if (snprintf(pathbuf, sizeof (pathbuf), "%s%s/%s.zoneadm.lock",
1197 	    zonecfg_get_root(), ZONES_TMPDIR, zone_name) >= sizeof (pathbuf)) {
1198 		zerror(gettext("alternate root path is too long"));
1199 		return (Z_ERR);
1200 	}
1201 	if ((*lockfd = open(pathbuf, O_RDWR|O_CREAT, S_IRUSR|S_IWUSR)) < 0) {
1202 		zerror(gettext("could not open %s: %s"), pathbuf,
1203 		    strerror(errno));
1204 		return (Z_ERR);
1205 	}
1206 	/*
1207 	 * Lock the file to synchronize with other zoneadmds
1208 	 */
1209 	flock.l_type = F_WRLCK;
1210 	flock.l_whence = SEEK_SET;
1211 	flock.l_start = (off_t)0;
1212 	flock.l_len = (off_t)0;
1213 	if ((fcntl(*lockfd, F_SETLKW, &flock) < 0) ||
1214 	    (putenv(zoneadm_lock_held) != 0)) {
1215 		zerror(gettext("unable to lock %s: %s"), pathbuf,
1216 		    strerror(errno));
1217 		release_lock_file(*lockfd);
1218 		return (Z_ERR);
1219 	}
1220 	zone_lock_cnt = 1;
1221 	return (Z_OK);
1222 }
1223 
1224 static boolean_t
1225 get_doorname(const char *zone_name, char *buffer)
1226 {
1227 	return (snprintf(buffer, PATH_MAX, "%s" ZONE_DOOR_PATH,
1228 	    zonecfg_get_root(), zone_name) < PATH_MAX);
1229 }
1230 
1231 /*
1232  * system daemons are not audited.  For the global zone, this occurs
1233  * "naturally" since init is started with the default audit
1234  * characteristics.  Since zoneadmd is a system daemon and it starts
1235  * init for a zone, it is necessary to clear out the audit
1236  * characteristics inherited from whomever started zoneadmd.  This is
1237  * indicated by the audit id, which is set from the ruid parameter of
1238  * adt_set_user(), below.
1239  */
1240 
1241 static void
1242 prepare_audit_context()
1243 {
1244 	adt_session_data_t	*ah;
1245 	char			*failure = gettext("audit failure: %s");
1246 
1247 	if (adt_start_session(&ah, NULL, 0)) {
1248 		zerror(failure, strerror(errno));
1249 		return;
1250 	}
1251 	if (adt_set_user(ah, ADT_NO_AUDIT, ADT_NO_AUDIT,
1252 	    ADT_NO_AUDIT, ADT_NO_AUDIT, NULL, ADT_NEW)) {
1253 		zerror(failure, strerror(errno));
1254 		(void) adt_end_session(ah);
1255 		return;
1256 	}
1257 	if (adt_set_proc(ah))
1258 		zerror(failure, strerror(errno));
1259 
1260 	(void) adt_end_session(ah);
1261 }
1262 
1263 static int
1264 start_zoneadmd(const char *zone_name)
1265 {
1266 	char doorpath[PATH_MAX];
1267 	pid_t child_pid;
1268 	int error = Z_ERR;
1269 	int doorfd, lockfd;
1270 	struct door_info info;
1271 
1272 	if (!get_doorname(zone_name, doorpath))
1273 		return (Z_ERR);
1274 
1275 	if (grab_lock_file(zone_name, &lockfd) != Z_OK)
1276 		return (Z_ERR);
1277 
1278 	/*
1279 	 * Now that we have the lock, re-confirm that the daemon is
1280 	 * *not* up and working fine.  If it is still down, we have a green
1281 	 * light to start it.
1282 	 */
1283 	if ((doorfd = open(doorpath, O_RDONLY)) < 0) {
1284 		if (errno != ENOENT) {
1285 			zperror(doorpath, B_FALSE);
1286 			goto out;
1287 		}
1288 	} else {
1289 		if (door_info(doorfd, &info) == 0 &&
1290 		    ((info.di_attributes & DOOR_REVOKED) == 0)) {
1291 			error = Z_OK;
1292 			(void) close(doorfd);
1293 			goto out;
1294 		}
1295 		(void) close(doorfd);
1296 	}
1297 
1298 	if ((child_pid = fork()) == -1) {
1299 		zperror(gettext("could not fork"), B_FALSE);
1300 		goto out;
1301 	} else if (child_pid == 0) {
1302 		const char *argv[6], **ap;
1303 
1304 		/* child process */
1305 		prepare_audit_context();
1306 
1307 		ap = argv;
1308 		*ap++ = "zoneadmd";
1309 		*ap++ = "-z";
1310 		*ap++ = zone_name;
1311 		if (zonecfg_in_alt_root()) {
1312 			*ap++ = "-R";
1313 			*ap++ = zonecfg_get_root();
1314 		}
1315 		*ap = NULL;
1316 
1317 		(void) execv("/usr/lib/zones/zoneadmd", (char * const *)argv);
1318 		/*
1319 		 * TRANSLATION_NOTE
1320 		 * zoneadmd is a literal that should not be translated.
1321 		 */
1322 		zperror(gettext("could not exec zoneadmd"), B_FALSE);
1323 		_exit(Z_ERR);
1324 	} else {
1325 		/* parent process */
1326 		pid_t retval;
1327 		int pstatus = 0;
1328 
1329 		do {
1330 			retval = waitpid(child_pid, &pstatus, 0);
1331 		} while (retval != child_pid);
1332 		if (WIFSIGNALED(pstatus) || (WIFEXITED(pstatus) &&
1333 		    WEXITSTATUS(pstatus) != 0)) {
1334 			zerror(gettext("could not start %s"), "zoneadmd");
1335 			goto out;
1336 		}
1337 	}
1338 	error = Z_OK;
1339 out:
1340 	release_lock_file(lockfd);
1341 	return (error);
1342 }
1343 
1344 static int
1345 ping_zoneadmd(const char *zone_name)
1346 {
1347 	char doorpath[PATH_MAX];
1348 	int doorfd;
1349 	struct door_info info;
1350 
1351 	if (!get_doorname(zone_name, doorpath))
1352 		return (Z_ERR);
1353 
1354 	if ((doorfd = open(doorpath, O_RDONLY)) < 0) {
1355 		return (Z_ERR);
1356 	}
1357 	if (door_info(doorfd, &info) == 0 &&
1358 	    ((info.di_attributes & DOOR_REVOKED) == 0)) {
1359 		(void) close(doorfd);
1360 		return (Z_OK);
1361 	}
1362 	(void) close(doorfd);
1363 	return (Z_ERR);
1364 }
1365 
1366 static int
1367 call_zoneadmd(const char *zone_name, zone_cmd_arg_t *arg)
1368 {
1369 	char doorpath[PATH_MAX];
1370 	int doorfd, result;
1371 	door_arg_t darg;
1372 
1373 	zoneid_t zoneid;
1374 	uint64_t uniqid = 0;
1375 
1376 	zone_cmd_rval_t *rvalp;
1377 	size_t rlen;
1378 	char *cp, *errbuf;
1379 
1380 	rlen = getpagesize();
1381 	if ((rvalp = malloc(rlen)) == NULL) {
1382 		zerror(gettext("failed to allocate %lu bytes: %s"), rlen,
1383 		    strerror(errno));
1384 		return (-1);
1385 	}
1386 
1387 	if ((zoneid = getzoneidbyname(zone_name)) != ZONE_ID_UNDEFINED) {
1388 		(void) zone_getattr(zoneid, ZONE_ATTR_UNIQID, &uniqid,
1389 		    sizeof (uniqid));
1390 	}
1391 	arg->uniqid = uniqid;
1392 	(void) strlcpy(arg->locale, locale, sizeof (arg->locale));
1393 	if (!get_doorname(zone_name, doorpath)) {
1394 		zerror(gettext("alternate root path is too long"));
1395 		free(rvalp);
1396 		return (-1);
1397 	}
1398 
1399 	/*
1400 	 * Loop trying to start zoneadmd; if something goes seriously
1401 	 * wrong we break out and fail.
1402 	 */
1403 	for (;;) {
1404 		if (start_zoneadmd(zone_name) != Z_OK)
1405 			break;
1406 
1407 		if ((doorfd = open(doorpath, O_RDONLY)) < 0) {
1408 			zperror(gettext("failed to open zone door"), B_FALSE);
1409 			break;
1410 		}
1411 
1412 		darg.data_ptr = (char *)arg;
1413 		darg.data_size = sizeof (*arg);
1414 		darg.desc_ptr = NULL;
1415 		darg.desc_num = 0;
1416 		darg.rbuf = (char *)rvalp;
1417 		darg.rsize = rlen;
1418 		if (door_call(doorfd, &darg) != 0) {
1419 			(void) close(doorfd);
1420 			/*
1421 			 * We'll get EBADF if the door has been revoked.
1422 			 */
1423 			if (errno != EBADF) {
1424 				zperror(gettext("door_call failed"), B_FALSE);
1425 				break;
1426 			}
1427 			continue;	/* take another lap */
1428 		}
1429 		(void) close(doorfd);
1430 
1431 		if (darg.data_size == 0) {
1432 			/* Door server is going away; kick it again. */
1433 			continue;
1434 		}
1435 
1436 		errbuf = rvalp->errbuf;
1437 		while (*errbuf != '\0') {
1438 			/*
1439 			 * Remove any newlines since zerror()
1440 			 * will append one automatically.
1441 			 */
1442 			cp = strchr(errbuf, '\n');
1443 			if (cp != NULL)
1444 				*cp = '\0';
1445 			zerror("%s", errbuf);
1446 			if (cp == NULL)
1447 				break;
1448 			errbuf = cp + 1;
1449 		}
1450 		result = rvalp->rval == 0 ? 0 : -1;
1451 		free(rvalp);
1452 		return (result);
1453 	}
1454 
1455 	free(rvalp);
1456 	return (-1);
1457 }
1458 
1459 static int
1460 invoke_brand_handler(int cmd_num, char *argv[])
1461 {
1462 	zone_dochandle_t handle;
1463 	int err;
1464 
1465 	if ((handle = zonecfg_init_handle()) == NULL) {
1466 		zperror(cmd_to_str(cmd_num), B_TRUE);
1467 		return (Z_ERR);
1468 	}
1469 	if ((err = zonecfg_get_handle(target_zone, handle)) != Z_OK) {
1470 		errno = err;
1471 		zperror(cmd_to_str(cmd_num), B_TRUE);
1472 		zonecfg_fini_handle(handle);
1473 		return (Z_ERR);
1474 	}
1475 	if (verify_brand(handle, cmd_num, argv) != Z_OK) {
1476 		zonecfg_fini_handle(handle);
1477 		return (Z_ERR);
1478 	}
1479 	zonecfg_fini_handle(handle);
1480 	return (Z_OK);
1481 }
1482 
1483 static int
1484 ready_func(int argc, char *argv[])
1485 {
1486 	zone_cmd_arg_t zarg;
1487 	int arg;
1488 
1489 	if (zonecfg_in_alt_root()) {
1490 		zerror(gettext("cannot ready zone in alternate root"));
1491 		return (Z_ERR);
1492 	}
1493 
1494 	optind = 0;
1495 	if ((arg = getopt(argc, argv, "?")) != EOF) {
1496 		switch (arg) {
1497 		case '?':
1498 			sub_usage(SHELP_READY, CMD_READY);
1499 			return (optopt == '?' ? Z_OK : Z_USAGE);
1500 		default:
1501 			sub_usage(SHELP_READY, CMD_READY);
1502 			return (Z_USAGE);
1503 		}
1504 	}
1505 	if (argc > optind) {
1506 		sub_usage(SHELP_READY, CMD_READY);
1507 		return (Z_USAGE);
1508 	}
1509 	if (sanity_check(target_zone, CMD_READY, B_FALSE, B_FALSE, B_FALSE)
1510 	    != Z_OK)
1511 		return (Z_ERR);
1512 	if (verify_details(CMD_READY, argv) != Z_OK)
1513 		return (Z_ERR);
1514 
1515 	zarg.cmd = Z_READY;
1516 	if (call_zoneadmd(target_zone, &zarg) != 0) {
1517 		zerror(gettext("call to %s failed"), "zoneadmd");
1518 		return (Z_ERR);
1519 	}
1520 	return (Z_OK);
1521 }
1522 
1523 static int
1524 boot_func(int argc, char *argv[])
1525 {
1526 	zone_cmd_arg_t zarg;
1527 	boolean_t force = B_FALSE;
1528 	int arg;
1529 
1530 	if (zonecfg_in_alt_root()) {
1531 		zerror(gettext("cannot boot zone in alternate root"));
1532 		return (Z_ERR);
1533 	}
1534 
1535 	zarg.bootbuf[0] = '\0';
1536 
1537 	/*
1538 	 * The following getopt processes arguments to zone boot; that
1539 	 * is to say, the [here] portion of the argument string:
1540 	 *
1541 	 *	zoneadm -z myzone boot [here] -- -v -m verbose
1542 	 *
1543 	 * Where [here] can either be nothing, -? (in which case we bail
1544 	 * and print usage), -f (a private option to indicate that the
1545 	 * boot operation should be 'forced'), or -s.  Support for -s is
1546 	 * vestigal and obsolete, but is retained because it was a
1547 	 * documented interface and there are known consumers including
1548 	 * admin/install; the proper way to specify boot arguments like -s
1549 	 * is:
1550 	 *
1551 	 *	zoneadm -z myzone boot -- -s -v -m verbose.
1552 	 */
1553 	optind = 0;
1554 	while ((arg = getopt(argc, argv, "?fs")) != EOF) {
1555 		switch (arg) {
1556 		case '?':
1557 			sub_usage(SHELP_BOOT, CMD_BOOT);
1558 			return (optopt == '?' ? Z_OK : Z_USAGE);
1559 		case 's':
1560 			(void) strlcpy(zarg.bootbuf, "-s",
1561 			    sizeof (zarg.bootbuf));
1562 			break;
1563 		case 'f':
1564 			force = B_TRUE;
1565 			break;
1566 		default:
1567 			sub_usage(SHELP_BOOT, CMD_BOOT);
1568 			return (Z_USAGE);
1569 		}
1570 	}
1571 
1572 	for (; optind < argc; optind++) {
1573 		if (strlcat(zarg.bootbuf, argv[optind],
1574 		    sizeof (zarg.bootbuf)) >= sizeof (zarg.bootbuf)) {
1575 			zerror(gettext("Boot argument list too long"));
1576 			return (Z_ERR);
1577 		}
1578 		if (optind < argc - 1)
1579 			if (strlcat(zarg.bootbuf, " ", sizeof (zarg.bootbuf)) >=
1580 			    sizeof (zarg.bootbuf)) {
1581 				zerror(gettext("Boot argument list too long"));
1582 				return (Z_ERR);
1583 			}
1584 	}
1585 	if (sanity_check(target_zone, CMD_BOOT, B_FALSE, B_FALSE, force)
1586 	    != Z_OK)
1587 		return (Z_ERR);
1588 	if (verify_details(CMD_BOOT, argv) != Z_OK)
1589 		return (Z_ERR);
1590 	zarg.cmd = force ? Z_FORCEBOOT : Z_BOOT;
1591 	if (call_zoneadmd(target_zone, &zarg) != 0) {
1592 		zerror(gettext("call to %s failed"), "zoneadmd");
1593 		return (Z_ERR);
1594 	}
1595 
1596 	return (Z_OK);
1597 }
1598 
1599 static void
1600 fake_up_local_zone(zoneid_t zid, zone_entry_t *zeptr)
1601 {
1602 	ssize_t result;
1603 	uuid_t uuid;
1604 	FILE *fp;
1605 	ushort_t flags;
1606 
1607 	(void) memset(zeptr, 0, sizeof (*zeptr));
1608 
1609 	zeptr->zid = zid;
1610 
1611 	/*
1612 	 * Since we're looking up our own (non-global) zone name,
1613 	 * we can be assured that it will succeed.
1614 	 */
1615 	result = getzonenamebyid(zid, zeptr->zname, sizeof (zeptr->zname));
1616 	assert(result >= 0);
1617 	if (zonecfg_is_scratch(zeptr->zname) &&
1618 	    (fp = zonecfg_open_scratch("", B_FALSE)) != NULL) {
1619 		(void) zonecfg_reverse_scratch(fp, zeptr->zname, zeptr->zname,
1620 		    sizeof (zeptr->zname), NULL, 0);
1621 		zonecfg_close_scratch(fp);
1622 	}
1623 
1624 	if (is_system_labeled()) {
1625 		(void) zone_getattr(zid, ZONE_ATTR_ROOT, zeptr->zroot,
1626 		    sizeof (zeptr->zroot));
1627 		(void) strlcpy(zeptr->zbrand, NATIVE_BRAND_NAME,
1628 			    sizeof (zeptr->zbrand));
1629 	} else {
1630 		(void) strlcpy(zeptr->zroot, "/", sizeof (zeptr->zroot));
1631 		(void) zone_getattr(zid, ZONE_ATTR_BRAND, zeptr->zbrand,
1632 		    sizeof (zeptr->zbrand));
1633 	}
1634 
1635 	zeptr->zstate_str = "running";
1636 	if (zonecfg_get_uuid(zeptr->zname, uuid) == Z_OK &&
1637 	    !uuid_is_null(uuid))
1638 		uuid_unparse(uuid, zeptr->zuuid);
1639 
1640 	if (zone_getattr(zid, ZONE_ATTR_FLAGS, &flags, sizeof (flags)) < 0) {
1641 		zperror2(zeptr->zname, gettext("could not get zone flags"));
1642 		exit(Z_ERR);
1643 	}
1644 	if (flags & ZF_NET_EXCL)
1645 		zeptr->ziptype = ZS_EXCLUSIVE;
1646 	else
1647 		zeptr->ziptype = ZS_SHARED;
1648 }
1649 
1650 static int
1651 list_func(int argc, char *argv[])
1652 {
1653 	zone_entry_t *zentp, zent;
1654 	int arg, retv;
1655 	boolean_t output = B_FALSE, verbose = B_FALSE, parsable = B_FALSE;
1656 	zone_state_t min_state = ZONE_STATE_RUNNING;
1657 	zoneid_t zone_id = getzoneid();
1658 
1659 	if (target_zone == NULL) {
1660 		/* all zones: default view to running but allow override */
1661 		optind = 0;
1662 		while ((arg = getopt(argc, argv, "?cipv")) != EOF) {
1663 			switch (arg) {
1664 			case '?':
1665 				sub_usage(SHELP_LIST, CMD_LIST);
1666 				return (optopt == '?' ? Z_OK : Z_USAGE);
1667 				/*
1668 				 * The 'i' and 'c' options are not mutually
1669 				 * exclusive so if 'c' is given, then min_state
1670 				 * is set to 0 (ZONE_STATE_CONFIGURED) which is
1671 				 * the lowest possible state.  If 'i' is given,
1672 				 * then min_state is set to be the lowest state
1673 				 * so far.
1674 				 */
1675 			case 'c':
1676 				min_state = ZONE_STATE_CONFIGURED;
1677 				break;
1678 			case 'i':
1679 				min_state = min(ZONE_STATE_INSTALLED,
1680 				    min_state);
1681 
1682 				break;
1683 			case 'p':
1684 				parsable = B_TRUE;
1685 				break;
1686 			case 'v':
1687 				verbose = B_TRUE;
1688 				break;
1689 			default:
1690 				sub_usage(SHELP_LIST, CMD_LIST);
1691 				return (Z_USAGE);
1692 			}
1693 		}
1694 		if (parsable && verbose) {
1695 			zerror(gettext("%s -p and -v are mutually exclusive."),
1696 			    cmd_to_str(CMD_LIST));
1697 			return (Z_ERR);
1698 		}
1699 		if (zone_id == GLOBAL_ZONEID || is_system_labeled()) {
1700 			retv = zone_print_list(min_state, verbose, parsable);
1701 		} else {
1702 			fake_up_local_zone(zone_id, &zent);
1703 			retv = Z_OK;
1704 			zone_print(&zent, verbose, parsable);
1705 		}
1706 		return (retv);
1707 	}
1708 
1709 	/*
1710 	 * Specific target zone: disallow -i/-c suboptions.
1711 	 */
1712 	optind = 0;
1713 	while ((arg = getopt(argc, argv, "?pv")) != EOF) {
1714 		switch (arg) {
1715 		case '?':
1716 			sub_usage(SHELP_LIST, CMD_LIST);
1717 			return (optopt == '?' ? Z_OK : Z_USAGE);
1718 		case 'p':
1719 			parsable = B_TRUE;
1720 			break;
1721 		case 'v':
1722 			verbose = B_TRUE;
1723 			break;
1724 		default:
1725 			sub_usage(SHELP_LIST, CMD_LIST);
1726 			return (Z_USAGE);
1727 		}
1728 	}
1729 	if (parsable && verbose) {
1730 		zerror(gettext("%s -p and -v are mutually exclusive."),
1731 		    cmd_to_str(CMD_LIST));
1732 		return (Z_ERR);
1733 	}
1734 	if (argc > optind) {
1735 		sub_usage(SHELP_LIST, CMD_LIST);
1736 		return (Z_USAGE);
1737 	}
1738 	if (zone_id != GLOBAL_ZONEID) {
1739 		fake_up_local_zone(zone_id, &zent);
1740 		/*
1741 		 * main() will issue a Z_NO_ZONE error if it cannot get an
1742 		 * id for target_zone, which in a non-global zone should
1743 		 * happen for any zone name except `zonename`.  Thus we
1744 		 * assert() that here but don't otherwise check.
1745 		 */
1746 		assert(strcmp(zent.zname, target_zone) == 0);
1747 		zone_print(&zent, verbose, parsable);
1748 		output = B_TRUE;
1749 	} else if ((zentp = lookup_running_zone(target_zone)) != NULL) {
1750 		zone_print(zentp, verbose, parsable);
1751 		output = B_TRUE;
1752 	} else if (lookup_zone_info(target_zone, ZONE_ID_UNDEFINED,
1753 	    &zent) == Z_OK) {
1754 		zone_print(&zent, verbose, parsable);
1755 		output = B_TRUE;
1756 	}
1757 
1758 	/*
1759 	 * Invoke brand-specific handler. Note that we do this
1760 	 * only if we're in the global zone, and target_zone is specified
1761 	 * and it is not the global zone.
1762 	 */
1763 	if (zone_id == GLOBAL_ZONEID && target_zone != NULL &&
1764 	    strcmp(target_zone, GLOBAL_ZONENAME) != 0)
1765 		if (invoke_brand_handler(CMD_LIST, argv) != Z_OK)
1766 			return (Z_ERR);
1767 
1768 	return (output ? Z_OK : Z_ERR);
1769 }
1770 
1771 static void
1772 sigterm(int sig)
1773 {
1774 	/*
1775 	 * Ignore SIG{INT,TERM}, so we don't end up in an infinite loop,
1776 	 * then propagate the signal to our process group.
1777 	 */
1778 	assert(sig == SIGINT || sig == SIGTERM);
1779 	(void) sigset(SIGINT, SIG_IGN);
1780 	(void) sigset(SIGTERM, SIG_IGN);
1781 	(void) kill(0, sig);
1782 	child_killed = B_TRUE;
1783 }
1784 
1785 static int
1786 do_subproc(char *cmdbuf)
1787 {
1788 	char inbuf[1024];	/* arbitrary large amount */
1789 	FILE *file;
1790 
1791 	do_subproc_cnt++;
1792 	child_killed = B_FALSE;
1793 	/*
1794 	 * We use popen(3c) to launch child processes for [un]install;
1795 	 * this library call does not return a PID, so we have to kill
1796 	 * the whole process group.  To avoid killing our parent, we
1797 	 * become a process group leader here.  But doing so can wreak
1798 	 * havoc with reading from stdin when launched by a non-job-control
1799 	 * shell, so we close stdin and reopen it as /dev/null first.
1800 	 */
1801 	(void) close(STDIN_FILENO);
1802 	(void) openat(STDIN_FILENO, "/dev/null", O_RDONLY);
1803 	if (!zoneadm_is_nested)
1804 		(void) setpgid(0, 0);
1805 	(void) sigset(SIGINT, sigterm);
1806 	(void) sigset(SIGTERM, sigterm);
1807 	file = popen(cmdbuf, "r");
1808 	for (;;) {
1809 		if (child_killed || fgets(inbuf, sizeof (inbuf), file) == NULL)
1810 			break;
1811 		(void) fputs(inbuf, stdout);
1812 	}
1813 	(void) sigset(SIGINT, SIG_DFL);
1814 	(void) sigset(SIGTERM, SIG_DFL);
1815 	return (pclose(file));
1816 }
1817 
1818 static int
1819 do_subproc_interactive(char *cmdbuf)
1820 {
1821 	void (*saveint)(int);
1822 	void (*saveterm)(int);
1823 	void (*savequit)(int);
1824 	void (*savehup)(int);
1825 	int pid, child, status;
1826 
1827 	/*
1828 	 * do_subproc() links stdin to /dev/null, which would break any
1829 	 * interactive subprocess we try to launch here.  Similarly, we
1830 	 * can't have been launched as a subprocess ourselves.
1831 	 */
1832 	assert(do_subproc_cnt == 0 && !zoneadm_is_nested);
1833 
1834 	if ((child = vfork()) == 0) {
1835 		(void) execl("/bin/sh", "sh", "-c", cmdbuf, (char *)NULL);
1836 	}
1837 
1838 	if (child == -1)
1839 		return (-1);
1840 
1841 	saveint = sigset(SIGINT, SIG_IGN);
1842 	saveterm = sigset(SIGTERM, SIG_IGN);
1843 	savequit = sigset(SIGQUIT, SIG_IGN);
1844 	savehup = sigset(SIGHUP, SIG_IGN);
1845 
1846 	while ((pid = waitpid(child, &status, 0)) != child && pid != -1)
1847 		;
1848 
1849 	(void) sigset(SIGINT, saveint);
1850 	(void) sigset(SIGTERM, saveterm);
1851 	(void) sigset(SIGQUIT, savequit);
1852 	(void) sigset(SIGHUP, savehup);
1853 
1854 	return (pid == -1 ? -1 : status);
1855 }
1856 
1857 static int
1858 subproc_status(const char *cmd, int status, boolean_t verbose_failure)
1859 {
1860 	if (WIFEXITED(status)) {
1861 		int exit_code = WEXITSTATUS(status);
1862 
1863 		if ((verbose_failure) && (exit_code != ZONE_SUBPROC_OK))
1864 			zerror(gettext("'%s' failed with exit code %d."), cmd,
1865 			    exit_code);
1866 
1867 		return (exit_code);
1868 	} else if (WIFSIGNALED(status)) {
1869 		int signal = WTERMSIG(status);
1870 		char sigstr[SIG2STR_MAX];
1871 
1872 		if (sig2str(signal, sigstr) == 0) {
1873 			zerror(gettext("'%s' terminated by signal SIG%s."), cmd,
1874 			    sigstr);
1875 		} else {
1876 			zerror(gettext("'%s' terminated by an unknown signal."),
1877 			    cmd);
1878 		}
1879 	} else {
1880 		zerror(gettext("'%s' failed for unknown reasons."), cmd);
1881 	}
1882 
1883 	/*
1884 	 * Assume a subprocess that died due to a signal or an unknown error
1885 	 * should be considered an exit code of ZONE_SUBPROC_FATAL, as the
1886 	 * user will likely need to do some manual cleanup.
1887 	 */
1888 	return (ZONE_SUBPROC_FATAL);
1889 }
1890 
1891 /*
1892  * Various sanity checks; make sure:
1893  * 1. We're in the global zone.
1894  * 2. The calling user has sufficient privilege.
1895  * 3. The target zone is neither the global zone nor anything starting with
1896  *    "SUNW".
1897  * 4a. If we're looking for a 'not running' (i.e., configured or installed)
1898  *     zone, the name service knows about it.
1899  * 4b. For some operations which expect a zone not to be running, that it is
1900  *     not already running (or ready).
1901  */
1902 static int
1903 sanity_check(char *zone, int cmd_num, boolean_t running,
1904     boolean_t unsafe_when_running, boolean_t force)
1905 {
1906 	zone_entry_t *zent;
1907 	priv_set_t *privset;
1908 	zone_state_t state, min_state;
1909 	char kernzone[ZONENAME_MAX];
1910 	FILE *fp;
1911 
1912 	if (getzoneid() != GLOBAL_ZONEID) {
1913 		switch (cmd_num) {
1914 		case CMD_HALT:
1915 			zerror(gettext("use %s to %s this zone."), "halt(1M)",
1916 			    cmd_to_str(cmd_num));
1917 			break;
1918 		case CMD_REBOOT:
1919 			zerror(gettext("use %s to %s this zone."),
1920 			    "reboot(1M)", cmd_to_str(cmd_num));
1921 			break;
1922 		default:
1923 			zerror(gettext("must be in the global zone to %s a "
1924 			    "zone."), cmd_to_str(cmd_num));
1925 			break;
1926 		}
1927 		return (Z_ERR);
1928 	}
1929 
1930 	if ((privset = priv_allocset()) == NULL) {
1931 		zerror(gettext("%s failed"), "priv_allocset");
1932 		return (Z_ERR);
1933 	}
1934 
1935 	if (getppriv(PRIV_EFFECTIVE, privset) != 0) {
1936 		zerror(gettext("%s failed"), "getppriv");
1937 		priv_freeset(privset);
1938 		return (Z_ERR);
1939 	}
1940 
1941 	if (priv_isfullset(privset) == B_FALSE) {
1942 		zerror(gettext("only a privileged user may %s a zone."),
1943 		    cmd_to_str(cmd_num));
1944 		priv_freeset(privset);
1945 		return (Z_ERR);
1946 	}
1947 	priv_freeset(privset);
1948 
1949 	if (zone == NULL) {
1950 		zerror(gettext("no zone specified"));
1951 		return (Z_ERR);
1952 	}
1953 
1954 	if (strcmp(zone, GLOBAL_ZONENAME) == 0) {
1955 		zerror(gettext("%s operation is invalid for the global zone."),
1956 		    cmd_to_str(cmd_num));
1957 		return (Z_ERR);
1958 	}
1959 
1960 	if (strncmp(zone, "SUNW", 4) == 0) {
1961 		zerror(gettext("%s operation is invalid for zones starting "
1962 		    "with SUNW."), cmd_to_str(cmd_num));
1963 		return (Z_ERR);
1964 	}
1965 
1966 	if (!is_native_zone && cmd_num == CMD_MOUNT) {
1967 		zerror(gettext("%s operation is invalid for branded zones."),
1968 		    cmd_to_str(cmd_num));
1969 			return (Z_ERR);
1970 	}
1971 
1972 	if (!zonecfg_in_alt_root()) {
1973 		zent = lookup_running_zone(zone);
1974 	} else if ((fp = zonecfg_open_scratch("", B_FALSE)) == NULL) {
1975 		zent = NULL;
1976 	} else {
1977 		if (zonecfg_find_scratch(fp, zone, zonecfg_get_root(),
1978 		    kernzone, sizeof (kernzone)) == 0)
1979 			zent = lookup_running_zone(kernzone);
1980 		else
1981 			zent = NULL;
1982 		zonecfg_close_scratch(fp);
1983 	}
1984 
1985 	/*
1986 	 * Look up from the kernel for 'running' zones.
1987 	 */
1988 	if (running && !force) {
1989 		if (zent == NULL) {
1990 			zerror(gettext("not running"));
1991 			return (Z_ERR);
1992 		}
1993 	} else {
1994 		int err;
1995 
1996 		if (unsafe_when_running && zent != NULL) {
1997 			/* check whether the zone is ready or running */
1998 			if ((err = zone_get_state(zent->zname,
1999 			    &zent->zstate_num)) != Z_OK) {
2000 				errno = err;
2001 				zperror2(zent->zname,
2002 				    gettext("could not get state"));
2003 				/* can't tell, so hedge */
2004 				zent->zstate_str = "ready/running";
2005 			} else {
2006 				zent->zstate_str =
2007 				    zone_state_str(zent->zstate_num);
2008 			}
2009 			zerror(gettext("%s operation is invalid for %s zones."),
2010 			    cmd_to_str(cmd_num), zent->zstate_str);
2011 			return (Z_ERR);
2012 		}
2013 		if ((err = zone_get_state(zone, &state)) != Z_OK) {
2014 			errno = err;
2015 			zperror2(zone, gettext("could not get state"));
2016 			return (Z_ERR);
2017 		}
2018 		switch (cmd_num) {
2019 		case CMD_UNINSTALL:
2020 			if (state == ZONE_STATE_CONFIGURED) {
2021 				zerror(gettext("is already in state '%s'."),
2022 				    zone_state_str(ZONE_STATE_CONFIGURED));
2023 				return (Z_ERR);
2024 			}
2025 			break;
2026 		case CMD_ATTACH:
2027 		case CMD_CLONE:
2028 		case CMD_INSTALL:
2029 			if (state == ZONE_STATE_INSTALLED) {
2030 				zerror(gettext("is already %s."),
2031 				    zone_state_str(ZONE_STATE_INSTALLED));
2032 				return (Z_ERR);
2033 			} else if (state == ZONE_STATE_INCOMPLETE) {
2034 				zerror(gettext("zone is %s; %s required."),
2035 				    zone_state_str(ZONE_STATE_INCOMPLETE),
2036 				    cmd_to_str(CMD_UNINSTALL));
2037 				return (Z_ERR);
2038 			}
2039 			break;
2040 		case CMD_DETACH:
2041 		case CMD_MOVE:
2042 		case CMD_READY:
2043 		case CMD_BOOT:
2044 		case CMD_MOUNT:
2045 		case CMD_MARK:
2046 			if ((cmd_num == CMD_BOOT || cmd_num == CMD_MOUNT) &&
2047 			    force)
2048 				min_state = ZONE_STATE_INCOMPLETE;
2049 			else
2050 				min_state = ZONE_STATE_INSTALLED;
2051 
2052 			if (force && cmd_num == CMD_BOOT && is_native_zone) {
2053 				zerror(gettext("Only branded zones may be "
2054 				    "force-booted."));
2055 				return (Z_ERR);
2056 			}
2057 
2058 			if (state < min_state) {
2059 				zerror(gettext("must be %s before %s."),
2060 				    zone_state_str(min_state),
2061 				    cmd_to_str(cmd_num));
2062 				return (Z_ERR);
2063 			}
2064 			break;
2065 		case CMD_VERIFY:
2066 			if (state == ZONE_STATE_INCOMPLETE) {
2067 				zerror(gettext("zone is %s; %s required."),
2068 				    zone_state_str(ZONE_STATE_INCOMPLETE),
2069 				    cmd_to_str(CMD_UNINSTALL));
2070 				return (Z_ERR);
2071 			}
2072 			break;
2073 		case CMD_UNMOUNT:
2074 			if (state != ZONE_STATE_MOUNTED) {
2075 				zerror(gettext("must be %s before %s."),
2076 				    zone_state_str(ZONE_STATE_MOUNTED),
2077 				    cmd_to_str(cmd_num));
2078 				return (Z_ERR);
2079 			}
2080 			break;
2081 		}
2082 	}
2083 	return (Z_OK);
2084 }
2085 
2086 static int
2087 halt_func(int argc, char *argv[])
2088 {
2089 	zone_cmd_arg_t zarg;
2090 	int arg;
2091 
2092 	if (zonecfg_in_alt_root()) {
2093 		zerror(gettext("cannot halt zone in alternate root"));
2094 		return (Z_ERR);
2095 	}
2096 
2097 	optind = 0;
2098 	if ((arg = getopt(argc, argv, "?")) != EOF) {
2099 		switch (arg) {
2100 		case '?':
2101 			sub_usage(SHELP_HALT, CMD_HALT);
2102 			return (optopt == '?' ? Z_OK : Z_USAGE);
2103 		default:
2104 			sub_usage(SHELP_HALT, CMD_HALT);
2105 			return (Z_USAGE);
2106 		}
2107 	}
2108 	if (argc > optind) {
2109 		sub_usage(SHELP_HALT, CMD_HALT);
2110 		return (Z_USAGE);
2111 	}
2112 	/*
2113 	 * zoneadmd should be the one to decide whether or not to proceed,
2114 	 * so even though it seems that the fourth parameter below should
2115 	 * perhaps be B_TRUE, it really shouldn't be.
2116 	 */
2117 	if (sanity_check(target_zone, CMD_HALT, B_FALSE, B_FALSE, B_FALSE)
2118 	    != Z_OK)
2119 		return (Z_ERR);
2120 
2121 	/*
2122 	 * Invoke brand-specific handler.
2123 	 */
2124 	if (invoke_brand_handler(CMD_HALT, argv) != Z_OK)
2125 		return (Z_ERR);
2126 
2127 	zarg.cmd = Z_HALT;
2128 	return ((call_zoneadmd(target_zone, &zarg) == 0) ? Z_OK : Z_ERR);
2129 }
2130 
2131 static int
2132 reboot_func(int argc, char *argv[])
2133 {
2134 	zone_cmd_arg_t zarg;
2135 	int arg;
2136 
2137 	if (zonecfg_in_alt_root()) {
2138 		zerror(gettext("cannot reboot zone in alternate root"));
2139 		return (Z_ERR);
2140 	}
2141 
2142 	optind = 0;
2143 	if ((arg = getopt(argc, argv, "?")) != EOF) {
2144 		switch (arg) {
2145 		case '?':
2146 			sub_usage(SHELP_REBOOT, CMD_REBOOT);
2147 			return (optopt == '?' ? Z_OK : Z_USAGE);
2148 		default:
2149 			sub_usage(SHELP_REBOOT, CMD_REBOOT);
2150 			return (Z_USAGE);
2151 		}
2152 	}
2153 
2154 	zarg.bootbuf[0] = '\0';
2155 	for (; optind < argc; optind++) {
2156 		if (strlcat(zarg.bootbuf, argv[optind],
2157 		    sizeof (zarg.bootbuf)) >= sizeof (zarg.bootbuf)) {
2158 			zerror(gettext("Boot argument list too long"));
2159 			return (Z_ERR);
2160 		}
2161 		if (optind < argc - 1)
2162 			if (strlcat(zarg.bootbuf, " ", sizeof (zarg.bootbuf)) >=
2163 			    sizeof (zarg.bootbuf)) {
2164 				zerror(gettext("Boot argument list too long"));
2165 				return (Z_ERR);
2166 			}
2167 	}
2168 
2169 
2170 	/*
2171 	 * zoneadmd should be the one to decide whether or not to proceed,
2172 	 * so even though it seems that the fourth parameter below should
2173 	 * perhaps be B_TRUE, it really shouldn't be.
2174 	 */
2175 	if (sanity_check(target_zone, CMD_REBOOT, B_TRUE, B_FALSE, B_FALSE)
2176 	    != Z_OK)
2177 		return (Z_ERR);
2178 	if (verify_details(CMD_REBOOT, argv) != Z_OK)
2179 		return (Z_ERR);
2180 
2181 	zarg.cmd = Z_REBOOT;
2182 	return ((call_zoneadmd(target_zone, &zarg) == 0) ? Z_OK : Z_ERR);
2183 }
2184 
2185 static int
2186 verify_brand(zone_dochandle_t handle, int cmd_num, char *argv[])
2187 {
2188 	char cmdbuf[MAXPATHLEN];
2189 	int err;
2190 	char zonepath[MAXPATHLEN];
2191 	brand_handle_t bh = NULL;
2192 	int status, i;
2193 
2194 	/*
2195 	 * Fetch the verify command from the brand configuration.
2196 	 * "exec" the command so that the returned status is that of
2197 	 * the command and not the shell.
2198 	 */
2199 	if ((err = zonecfg_get_zonepath(handle, zonepath, sizeof (zonepath))) !=
2200 	    Z_OK) {
2201 		errno = err;
2202 		zperror(cmd_to_str(cmd_num), B_TRUE);
2203 		return (Z_ERR);
2204 	}
2205 	if ((bh = brand_open(target_brand)) == NULL) {
2206 		zerror(gettext("missing or invalid brand"));
2207 		return (Z_ERR);
2208 	}
2209 
2210 	/*
2211 	 * If the brand has its own verification routine, execute it now.
2212 	 * The verification routine validates the intended zoneadm
2213 	 * operation for the specific brand. The zoneadm subcommand and
2214 	 * all its arguments are passed to the routine.
2215 	 */
2216 	(void) strcpy(cmdbuf, EXEC_PREFIX);
2217 	err = brand_get_verify_adm(bh, target_zone, zonepath,
2218 	    cmdbuf + EXEC_LEN, sizeof (cmdbuf) - EXEC_LEN, 0, NULL);
2219 	brand_close(bh);
2220 	if (err != 0)
2221 		return (Z_BRAND_ERROR);
2222 	if (strlen(cmdbuf) <= EXEC_LEN)
2223 		return (Z_OK);
2224 
2225 	if (strlcat(cmdbuf, cmd_to_str(cmd_num),
2226 	    sizeof (cmdbuf)) >= sizeof (cmdbuf))
2227 		return (Z_ERR);
2228 
2229 	/* Build the argv string */
2230 	i = 0;
2231 	while (argv[i] != NULL) {
2232 		if ((strlcat(cmdbuf, " ",
2233 		    sizeof (cmdbuf)) >= sizeof (cmdbuf)) ||
2234 		    (strlcat(cmdbuf, argv[i++],
2235 		    sizeof (cmdbuf)) >= sizeof (cmdbuf)))
2236 			return (Z_ERR);
2237 	}
2238 
2239 	if (zoneadm_is_nested)
2240 		status = do_subproc(cmdbuf);
2241 	else
2242 		status = do_subproc_interactive(cmdbuf);
2243 	err = subproc_status(gettext("brand-specific verification"),
2244 	    status, B_FALSE);
2245 
2246 	return ((err == ZONE_SUBPROC_OK) ? Z_OK : Z_BRAND_ERROR);
2247 }
2248 
2249 static int
2250 verify_rctls(zone_dochandle_t handle)
2251 {
2252 	struct zone_rctltab rctltab;
2253 	size_t rbs = rctlblk_size();
2254 	rctlblk_t *rctlblk;
2255 	int error = Z_INVAL;
2256 
2257 	if ((rctlblk = malloc(rbs)) == NULL) {
2258 		zerror(gettext("failed to allocate %lu bytes: %s"), rbs,
2259 		    strerror(errno));
2260 		return (Z_NOMEM);
2261 	}
2262 
2263 	if (zonecfg_setrctlent(handle) != Z_OK) {
2264 		zerror(gettext("zonecfg_setrctlent failed"));
2265 		free(rctlblk);
2266 		return (error);
2267 	}
2268 
2269 	rctltab.zone_rctl_valptr = NULL;
2270 	while (zonecfg_getrctlent(handle, &rctltab) == Z_OK) {
2271 		struct zone_rctlvaltab *rctlval;
2272 		const char *name = rctltab.zone_rctl_name;
2273 
2274 		if (!zonecfg_is_rctl(name)) {
2275 			zerror(gettext("WARNING: Ignoring unrecognized rctl "
2276 			    "'%s'."),  name);
2277 			zonecfg_free_rctl_value_list(rctltab.zone_rctl_valptr);
2278 			rctltab.zone_rctl_valptr = NULL;
2279 			continue;
2280 		}
2281 
2282 		for (rctlval = rctltab.zone_rctl_valptr; rctlval != NULL;
2283 		    rctlval = rctlval->zone_rctlval_next) {
2284 			if (zonecfg_construct_rctlblk(rctlval, rctlblk)
2285 			    != Z_OK) {
2286 				zerror(gettext("invalid rctl value: "
2287 				    "(priv=%s,limit=%s,action%s)"),
2288 				    rctlval->zone_rctlval_priv,
2289 				    rctlval->zone_rctlval_limit,
2290 				    rctlval->zone_rctlval_action);
2291 				goto out;
2292 			}
2293 			if (!zonecfg_valid_rctl(name, rctlblk)) {
2294 				zerror(gettext("(priv=%s,limit=%s,action=%s) "
2295 				    "is not a valid value for rctl '%s'"),
2296 				    rctlval->zone_rctlval_priv,
2297 				    rctlval->zone_rctlval_limit,
2298 				    rctlval->zone_rctlval_action,
2299 				    name);
2300 				goto out;
2301 			}
2302 		}
2303 		zonecfg_free_rctl_value_list(rctltab.zone_rctl_valptr);
2304 	}
2305 	rctltab.zone_rctl_valptr = NULL;
2306 	error = Z_OK;
2307 out:
2308 	zonecfg_free_rctl_value_list(rctltab.zone_rctl_valptr);
2309 	(void) zonecfg_endrctlent(handle);
2310 	free(rctlblk);
2311 	return (error);
2312 }
2313 
2314 static int
2315 verify_pool(zone_dochandle_t handle)
2316 {
2317 	char poolname[MAXPATHLEN];
2318 	pool_conf_t *poolconf;
2319 	pool_t *pool;
2320 	int status;
2321 	int error;
2322 
2323 	/*
2324 	 * This ends up being very similar to the check done in zoneadmd.
2325 	 */
2326 	error = zonecfg_get_pool(handle, poolname, sizeof (poolname));
2327 	if (error == Z_NO_ENTRY || (error == Z_OK && strlen(poolname) == 0)) {
2328 		/*
2329 		 * No pool specified.
2330 		 */
2331 		return (0);
2332 	}
2333 	if (error != Z_OK) {
2334 		zperror(gettext("Unable to retrieve pool name from "
2335 		    "configuration"), B_TRUE);
2336 		return (error);
2337 	}
2338 	/*
2339 	 * Don't do anything if pools aren't enabled.
2340 	 */
2341 	if (pool_get_status(&status) != PO_SUCCESS || status != POOL_ENABLED) {
2342 		zerror(gettext("WARNING: pools facility not active; "
2343 		    "zone will not be bound to pool '%s'."), poolname);
2344 		return (Z_OK);
2345 	}
2346 	/*
2347 	 * Try to provide a sane error message if the requested pool doesn't
2348 	 * exist.  It isn't clear that pools-related failures should
2349 	 * necessarily translate to a failure to verify the zone configuration,
2350 	 * hence they are not considered errors.
2351 	 */
2352 	if ((poolconf = pool_conf_alloc()) == NULL) {
2353 		zerror(gettext("WARNING: pool_conf_alloc failed; "
2354 		    "using default pool"));
2355 		return (Z_OK);
2356 	}
2357 	if (pool_conf_open(poolconf, pool_dynamic_location(), PO_RDONLY) !=
2358 	    PO_SUCCESS) {
2359 		zerror(gettext("WARNING: pool_conf_open failed; "
2360 		    "using default pool"));
2361 		pool_conf_free(poolconf);
2362 		return (Z_OK);
2363 	}
2364 	pool = pool_get_pool(poolconf, poolname);
2365 	(void) pool_conf_close(poolconf);
2366 	pool_conf_free(poolconf);
2367 	if (pool == NULL) {
2368 		zerror(gettext("WARNING: pool '%s' not found. "
2369 		    "using default pool"), poolname);
2370 	}
2371 
2372 	return (Z_OK);
2373 }
2374 
2375 static int
2376 verify_ipd(zone_dochandle_t handle)
2377 {
2378 	int return_code = Z_OK;
2379 	struct zone_fstab fstab;
2380 	struct stat st;
2381 	char specdir[MAXPATHLEN];
2382 
2383 	if (zonecfg_setipdent(handle) != Z_OK) {
2384 		/*
2385 		 * TRANSLATION_NOTE
2386 		 * inherit-pkg-dirs is a literal that should not be translated.
2387 		 */
2388 		(void) fprintf(stderr, gettext("could not verify "
2389 		    "inherit-pkg-dirs: unable to enumerate mounts\n"));
2390 		return (Z_ERR);
2391 	}
2392 	while (zonecfg_getipdent(handle, &fstab) == Z_OK) {
2393 		/*
2394 		 * Verify fs_dir exists.
2395 		 */
2396 		(void) snprintf(specdir, sizeof (specdir), "%s%s",
2397 		    zonecfg_get_root(), fstab.zone_fs_dir);
2398 		if (stat(specdir, &st) != 0) {
2399 			/*
2400 			 * TRANSLATION_NOTE
2401 			 * inherit-pkg-dir is a literal that should not be
2402 			 * translated.
2403 			 */
2404 			(void) fprintf(stderr, gettext("could not verify "
2405 			    "inherit-pkg-dir %s: %s\n"),
2406 			    fstab.zone_fs_dir, strerror(errno));
2407 			return_code = Z_ERR;
2408 		}
2409 		if (strcmp(st.st_fstype, MNTTYPE_NFS) == 0) {
2410 			/*
2411 			 * TRANSLATION_NOTE
2412 			 * inherit-pkg-dir and NFS are literals that should
2413 			 * not be translated.
2414 			 */
2415 			(void) fprintf(stderr, gettext("cannot verify "
2416 			    "inherit-pkg-dir %s: NFS mounted file system.\n"
2417 			    "\tA local file system must be used.\n"),
2418 			    fstab.zone_fs_dir);
2419 			return_code = Z_ERR;
2420 		}
2421 	}
2422 	(void) zonecfg_endipdent(handle);
2423 
2424 	return (return_code);
2425 }
2426 
2427 /*
2428  * Verify that the special device/file system exists and is valid.
2429  */
2430 static int
2431 verify_fs_special(struct zone_fstab *fstab)
2432 {
2433 	struct stat st;
2434 
2435 	/*
2436 	 * This validation is really intended for standard zone administration.
2437 	 * If we are in a mini-root or some other upgrade situation where
2438 	 * we are using the scratch zone, just by-pass this.
2439 	 */
2440 	if (zonecfg_in_alt_root())
2441 		return (Z_OK);
2442 
2443 	if (strcmp(fstab->zone_fs_type, MNTTYPE_ZFS) == 0)
2444 		return (verify_fs_zfs(fstab));
2445 
2446 	if (stat(fstab->zone_fs_special, &st) != 0) {
2447 		(void) fprintf(stderr, gettext("could not verify fs "
2448 		    "%s: could not access %s: %s\n"), fstab->zone_fs_dir,
2449 		    fstab->zone_fs_special, strerror(errno));
2450 		return (Z_ERR);
2451 	}
2452 
2453 	if (strcmp(st.st_fstype, MNTTYPE_NFS) == 0) {
2454 		/*
2455 		 * TRANSLATION_NOTE
2456 		 * fs and NFS are literals that should
2457 		 * not be translated.
2458 		 */
2459 		(void) fprintf(stderr, gettext("cannot verify "
2460 		    "fs %s: NFS mounted file system.\n"
2461 		    "\tA local file system must be used.\n"),
2462 		    fstab->zone_fs_special);
2463 		return (Z_ERR);
2464 	}
2465 
2466 	return (Z_OK);
2467 }
2468 
2469 static int
2470 verify_filesystems(zone_dochandle_t handle)
2471 {
2472 	int return_code = Z_OK;
2473 	struct zone_fstab fstab;
2474 	char cmdbuf[MAXPATHLEN];
2475 	struct stat st;
2476 
2477 	/*
2478 	 * No need to verify inherit-pkg-dir fs types, as their type is
2479 	 * implicitly lofs, which is known.  Therefore, the types are only
2480 	 * verified for regular file systems below.
2481 	 *
2482 	 * Since the actual mount point is not known until the dependent mounts
2483 	 * are performed, we don't attempt any path validation here: that will
2484 	 * happen later when zoneadmd actually does the mounts.
2485 	 */
2486 	if (zonecfg_setfsent(handle) != Z_OK) {
2487 		(void) fprintf(stderr, gettext("could not verify file systems: "
2488 		    "unable to enumerate mounts\n"));
2489 		return (Z_ERR);
2490 	}
2491 	while (zonecfg_getfsent(handle, &fstab) == Z_OK) {
2492 		if (!zonecfg_valid_fs_type(fstab.zone_fs_type)) {
2493 			(void) fprintf(stderr, gettext("cannot verify fs %s: "
2494 			    "type %s is not allowed.\n"), fstab.zone_fs_dir,
2495 			    fstab.zone_fs_type);
2496 			return_code = Z_ERR;
2497 			goto next_fs;
2498 		}
2499 		/*
2500 		 * Verify /usr/lib/fs/<fstype>/mount exists.
2501 		 */
2502 		if (snprintf(cmdbuf, sizeof (cmdbuf), "/usr/lib/fs/%s/mount",
2503 		    fstab.zone_fs_type) > sizeof (cmdbuf)) {
2504 			(void) fprintf(stderr, gettext("cannot verify fs %s: "
2505 			    "type %s is too long.\n"), fstab.zone_fs_dir,
2506 			    fstab.zone_fs_type);
2507 			return_code = Z_ERR;
2508 			goto next_fs;
2509 		}
2510 		if (stat(cmdbuf, &st) != 0) {
2511 			(void) fprintf(stderr, gettext("could not verify fs "
2512 			    "%s: could not access %s: %s\n"), fstab.zone_fs_dir,
2513 			    cmdbuf, strerror(errno));
2514 			return_code = Z_ERR;
2515 			goto next_fs;
2516 		}
2517 		if (!S_ISREG(st.st_mode)) {
2518 			(void) fprintf(stderr, gettext("could not verify fs "
2519 			    "%s: %s is not a regular file\n"),
2520 			    fstab.zone_fs_dir, cmdbuf);
2521 			return_code = Z_ERR;
2522 			goto next_fs;
2523 		}
2524 		/*
2525 		 * Verify /usr/lib/fs/<fstype>/fsck exists iff zone_fs_raw is
2526 		 * set.
2527 		 */
2528 		if (snprintf(cmdbuf, sizeof (cmdbuf), "/usr/lib/fs/%s/fsck",
2529 		    fstab.zone_fs_type) > sizeof (cmdbuf)) {
2530 			(void) fprintf(stderr, gettext("cannot verify fs %s: "
2531 			    "type %s is too long.\n"), fstab.zone_fs_dir,
2532 			    fstab.zone_fs_type);
2533 			return_code = Z_ERR;
2534 			goto next_fs;
2535 		}
2536 		if (fstab.zone_fs_raw[0] == '\0' && stat(cmdbuf, &st) == 0) {
2537 			(void) fprintf(stderr, gettext("could not verify fs "
2538 			    "%s: must specify 'raw' device for %s "
2539 			    "file systems\n"),
2540 			    fstab.zone_fs_dir, fstab.zone_fs_type);
2541 			return_code = Z_ERR;
2542 			goto next_fs;
2543 		}
2544 		if (fstab.zone_fs_raw[0] != '\0' &&
2545 		    (stat(cmdbuf, &st) != 0 || !S_ISREG(st.st_mode))) {
2546 			(void) fprintf(stderr, gettext("cannot verify fs %s: "
2547 			    "'raw' device specified but "
2548 			    "no fsck executable exists for %s\n"),
2549 			    fstab.zone_fs_dir, fstab.zone_fs_type);
2550 			return_code = Z_ERR;
2551 			goto next_fs;
2552 		}
2553 
2554 		/* Verify fs_special. */
2555 		if ((return_code = verify_fs_special(&fstab)) != Z_OK)
2556 			goto next_fs;
2557 
2558 		/* Verify fs_raw. */
2559 		if (fstab.zone_fs_raw[0] != '\0' &&
2560 		    stat(fstab.zone_fs_raw, &st) != 0) {
2561 			/*
2562 			 * TRANSLATION_NOTE
2563 			 * fs is a literal that should not be translated.
2564 			 */
2565 			(void) fprintf(stderr, gettext("could not verify fs "
2566 			    "%s: could not access %s: %s\n"), fstab.zone_fs_dir,
2567 			    fstab.zone_fs_raw, strerror(errno));
2568 			return_code = Z_ERR;
2569 			goto next_fs;
2570 		}
2571 next_fs:
2572 		zonecfg_free_fs_option_list(fstab.zone_fs_options);
2573 	}
2574 	(void) zonecfg_endfsent(handle);
2575 
2576 	return (return_code);
2577 }
2578 
2579 static int
2580 verify_limitpriv(zone_dochandle_t handle)
2581 {
2582 	char *privname = NULL;
2583 	int err;
2584 	priv_set_t *privs;
2585 
2586 	if ((privs = priv_allocset()) == NULL) {
2587 		zperror(gettext("failed to allocate privilege set"), B_FALSE);
2588 		return (Z_NOMEM);
2589 	}
2590 	err = zonecfg_get_privset(handle, privs, &privname);
2591 	switch (err) {
2592 	case Z_OK:
2593 		break;
2594 	case Z_PRIV_PROHIBITED:
2595 		(void) fprintf(stderr, gettext("privilege \"%s\" is not "
2596 		    "permitted within the zone's privilege set\n"), privname);
2597 		break;
2598 	case Z_PRIV_REQUIRED:
2599 		(void) fprintf(stderr, gettext("required privilege \"%s\" is "
2600 		    "missing from the zone's privilege set\n"), privname);
2601 		break;
2602 	case Z_PRIV_UNKNOWN:
2603 		(void) fprintf(stderr, gettext("unknown privilege \"%s\" "
2604 		    "specified in the zone's privilege set\n"), privname);
2605 		break;
2606 	default:
2607 		zperror(
2608 		    gettext("failed to determine the zone's privilege set"),
2609 		    B_TRUE);
2610 		break;
2611 	}
2612 	free(privname);
2613 	priv_freeset(privs);
2614 	return (err);
2615 }
2616 
2617 static void
2618 free_local_netifs(int if_cnt, struct net_if **if_list)
2619 {
2620 	int		i;
2621 
2622 	for (i = 0; i < if_cnt; i++) {
2623 		free(if_list[i]->name);
2624 		free(if_list[i]);
2625 	}
2626 	free(if_list);
2627 }
2628 
2629 /*
2630  * Get a list of the network interfaces, along with their address families,
2631  * that are plumbed in the global zone.  See if_tcp(7p) for a description
2632  * of the ioctls used here.
2633  */
2634 static int
2635 get_local_netifs(int *if_cnt, struct net_if ***if_list)
2636 {
2637 	int		s;
2638 	int		i;
2639 	int		res = Z_OK;
2640 	int		space_needed;
2641 	int		cnt = 0;
2642 	struct		lifnum if_num;
2643 	struct		lifconf if_conf;
2644 	struct		lifreq *if_reqp;
2645 	char		*if_buf;
2646 	struct net_if	**local_ifs = NULL;
2647 
2648 	*if_cnt = 0;
2649 	*if_list = NULL;
2650 
2651 	if ((s = socket(SOCKET_AF(AF_INET), SOCK_DGRAM, 0)) < 0)
2652 		return (Z_ERR);
2653 
2654 	/*
2655 	 * Come back here in the unlikely event that the number of interfaces
2656 	 * increases between the time we get the count and the time we do the
2657 	 * SIOCGLIFCONF ioctl.
2658 	 */
2659 retry:
2660 	/* Get the number of interfaces. */
2661 	if_num.lifn_family = AF_UNSPEC;
2662 	if_num.lifn_flags = LIFC_NOXMIT;
2663 	if (ioctl(s, SIOCGLIFNUM, &if_num) < 0) {
2664 		(void) close(s);
2665 		return (Z_ERR);
2666 	}
2667 
2668 	/* Get the interface configuration list. */
2669 	space_needed = if_num.lifn_count * sizeof (struct lifreq);
2670 	if ((if_buf = malloc(space_needed)) == NULL) {
2671 		(void) close(s);
2672 		return (Z_ERR);
2673 	}
2674 	if_conf.lifc_family = AF_UNSPEC;
2675 	if_conf.lifc_flags = LIFC_NOXMIT;
2676 	if_conf.lifc_len = space_needed;
2677 	if_conf.lifc_buf = if_buf;
2678 	if (ioctl(s, SIOCGLIFCONF, &if_conf) < 0) {
2679 		free(if_buf);
2680 		/*
2681 		 * SIOCGLIFCONF returns EINVAL if the buffer we passed in is
2682 		 * too small.  In this case go back and get the new if cnt.
2683 		 */
2684 		if (errno == EINVAL)
2685 			goto retry;
2686 
2687 		(void) close(s);
2688 		return (Z_ERR);
2689 	}
2690 	(void) close(s);
2691 
2692 	/* Get the name and address family for each interface. */
2693 	if_reqp = if_conf.lifc_req;
2694 	for (i = 0; i < (if_conf.lifc_len / sizeof (struct lifreq)); i++) {
2695 		struct net_if	**p;
2696 		struct lifreq	req;
2697 
2698 		if (strcmp(LOOPBACK_IF, if_reqp->lifr_name) == 0) {
2699 			if_reqp++;
2700 			continue;
2701 		}
2702 
2703 		if ((s = socket(SOCKET_AF(if_reqp->lifr_addr.ss_family),
2704 		    SOCK_DGRAM, 0)) == -1) {
2705 			res = Z_ERR;
2706 			break;
2707 		}
2708 
2709 		(void) strncpy(req.lifr_name, if_reqp->lifr_name,
2710 		    sizeof (req.lifr_name));
2711 		if (ioctl(s, SIOCGLIFADDR, &req) < 0) {
2712 			(void) close(s);
2713 			if_reqp++;
2714 			continue;
2715 		}
2716 
2717 		if ((p = (struct net_if **)realloc(local_ifs,
2718 		    sizeof (struct net_if *) * (cnt + 1))) == NULL) {
2719 			res = Z_ERR;
2720 			break;
2721 		}
2722 		local_ifs = p;
2723 
2724 		if ((local_ifs[cnt] = malloc(sizeof (struct net_if))) == NULL) {
2725 			res = Z_ERR;
2726 			break;
2727 		}
2728 
2729 		if ((local_ifs[cnt]->name = strdup(if_reqp->lifr_name))
2730 		    == NULL) {
2731 			free(local_ifs[cnt]);
2732 			res = Z_ERR;
2733 			break;
2734 		}
2735 		local_ifs[cnt]->af = req.lifr_addr.ss_family;
2736 		cnt++;
2737 
2738 		(void) close(s);
2739 		if_reqp++;
2740 	}
2741 
2742 	free(if_buf);
2743 
2744 	if (res != Z_OK) {
2745 		free_local_netifs(cnt, local_ifs);
2746 	} else {
2747 		*if_cnt = cnt;
2748 		*if_list = local_ifs;
2749 	}
2750 
2751 	return (res);
2752 }
2753 
2754 static char *
2755 af2str(int af)
2756 {
2757 	switch (af) {
2758 	case AF_INET:
2759 		return ("IPv4");
2760 	case AF_INET6:
2761 		return ("IPv6");
2762 	default:
2763 		return ("Unknown");
2764 	}
2765 }
2766 
2767 /*
2768  * Cross check the network interface name and address family with the
2769  * interfaces that are set up in the global zone so that we can print the
2770  * appropriate error message.
2771  */
2772 static void
2773 print_net_err(char *phys, char *addr, int af, char *msg)
2774 {
2775 	int		i;
2776 	int		local_if_cnt = 0;
2777 	struct net_if	**local_ifs = NULL;
2778 	boolean_t	found_if = B_FALSE;
2779 	boolean_t	found_af = B_FALSE;
2780 
2781 	if (get_local_netifs(&local_if_cnt, &local_ifs) != Z_OK) {
2782 		(void) fprintf(stderr,
2783 		    gettext("could not verify %s %s=%s %s=%s\n\t%s\n"),
2784 		    "net", "address", addr, "physical", phys, msg);
2785 		return;
2786 	}
2787 
2788 	for (i = 0; i < local_if_cnt; i++) {
2789 		if (strcmp(phys, local_ifs[i]->name) == 0) {
2790 			found_if = B_TRUE;
2791 			if (af == local_ifs[i]->af) {
2792 				found_af = B_TRUE;
2793 				break;
2794 			}
2795 		}
2796 	}
2797 
2798 	free_local_netifs(local_if_cnt, local_ifs);
2799 
2800 	if (!found_if) {
2801 		(void) fprintf(stderr,
2802 		    gettext("could not verify %s %s=%s\n\t"
2803 		    "network interface %s is not plumbed in the global zone\n"),
2804 		    "net", "physical", phys, phys);
2805 		return;
2806 	}
2807 
2808 	/*
2809 	 * Print this error if we were unable to find the address family
2810 	 * for this interface.  If the af variable is not initialized to
2811 	 * to something meaningful by the caller (not AF_UNSPEC) then we
2812 	 * also skip this message since it wouldn't be informative.
2813 	 */
2814 	if (!found_af && af != AF_UNSPEC) {
2815 		(void) fprintf(stderr,
2816 		    gettext("could not verify %s %s=%s %s=%s\n\tthe %s address "
2817 		    "family is not configured on this network interface in "
2818 		    "the\n\tglobal zone\n"),
2819 		    "net", "address", addr, "physical", phys, af2str(af));
2820 		return;
2821 	}
2822 
2823 	(void) fprintf(stderr,
2824 	    gettext("could not verify %s %s=%s %s=%s\n\t%s\n"),
2825 	    "net", "address", addr, "physical", phys, msg);
2826 }
2827 
2828 static int
2829 verify_handle(int cmd_num, zone_dochandle_t handle, char *argv[])
2830 {
2831 	struct zone_nwiftab nwiftab;
2832 	int return_code = Z_OK;
2833 	int err;
2834 	boolean_t in_alt_root;
2835 	zone_iptype_t iptype;
2836 	int fd;
2837 
2838 	in_alt_root = zonecfg_in_alt_root();
2839 	if (in_alt_root)
2840 		goto no_net;
2841 
2842 	if ((err = zonecfg_get_iptype(handle, &iptype)) != Z_OK) {
2843 		errno = err;
2844 		zperror(cmd_to_str(cmd_num), B_TRUE);
2845 		zonecfg_fini_handle(handle);
2846 		return (Z_ERR);
2847 	}
2848 	if ((err = zonecfg_setnwifent(handle)) != Z_OK) {
2849 		errno = err;
2850 		zperror(cmd_to_str(cmd_num), B_TRUE);
2851 		zonecfg_fini_handle(handle);
2852 		return (Z_ERR);
2853 	}
2854 	while (zonecfg_getnwifent(handle, &nwiftab) == Z_OK) {
2855 		struct lifreq lifr;
2856 		sa_family_t af = AF_UNSPEC;
2857 		char dl_owner_zname[ZONENAME_MAX];
2858 		zoneid_t dl_owner_zid;
2859 		zoneid_t target_zid;
2860 		int res;
2861 
2862 		/* skip any loopback interfaces */
2863 		if (strcmp(nwiftab.zone_nwif_physical, "lo0") == 0)
2864 			continue;
2865 		switch (iptype) {
2866 		case ZS_SHARED:
2867 			if ((res = zonecfg_valid_net_address(
2868 			    nwiftab.zone_nwif_address, &lifr)) != Z_OK) {
2869 				print_net_err(nwiftab.zone_nwif_physical,
2870 				    nwiftab.zone_nwif_address, af,
2871 				    zonecfg_strerror(res));
2872 			    return_code = Z_ERR;
2873 			    continue;
2874 			}
2875 			af = lifr.lifr_addr.ss_family;
2876 			if (!zonecfg_ifname_exists(af,
2877 			    nwiftab.zone_nwif_physical)) {
2878 				/*
2879 				 * The interface failed to come up. We continue
2880 				 * on anyway for the sake of consistency: a
2881 				 * zone is not shut down if the interface fails
2882 				 * any time after boot, nor does the global zone
2883 				 * fail to boot if an interface fails.
2884 				 */
2885 				(void) fprintf(stderr,
2886 				    gettext("WARNING: skipping network "
2887 					"interface '%s' which may not be "
2888 					"present/plumbed in the global "
2889 					"zone.\n"),
2890 				    nwiftab.zone_nwif_physical);
2891 			}
2892 			break;
2893 		case ZS_EXCLUSIVE:
2894 			/* Warning if it exists for either IPv4 or IPv6 */
2895 
2896 			if (zonecfg_ifname_exists(AF_INET,
2897 			    nwiftab.zone_nwif_physical) ||
2898 			    zonecfg_ifname_exists(AF_INET6,
2899 			    nwiftab.zone_nwif_physical)) {
2900 				(void) fprintf(stderr,
2901 				    gettext("WARNING: skipping network "
2902 				    "interface '%s' which is used in the "
2903 				    "global zone.\n"),
2904 				    nwiftab.zone_nwif_physical);
2905 				break;
2906 			}
2907 			/*
2908 			 * Verify that the physical interface can
2909 			 * be opened
2910 			 */
2911 			fd = ifname_open(nwiftab.zone_nwif_physical);
2912 			if (fd == -1) {
2913 				(void) fprintf(stderr,
2914 				    gettext("WARNING: skipping network "
2915 				    "interface '%s' which cannot be opened.\n"),
2916 				    nwiftab.zone_nwif_physical);
2917 				break;
2918 			} else {
2919 				(void) close(fd);
2920 			}
2921 			/*
2922 			 * Verify whether the physical interface is already
2923 			 * used by a zone.
2924 			 */
2925 			dl_owner_zid = ALL_ZONES;
2926 			if (zone_check_datalink(&dl_owner_zid,
2927 			    nwiftab.zone_nwif_physical) != 0)
2928 				break;
2929 
2930 			/*
2931 			 * If the zone being verified is
2932 			 * running and owns the interface
2933 			 */
2934 			target_zid = getzoneidbyname(target_zone);
2935 			if (target_zid == dl_owner_zid)
2936 				break;
2937 
2938 			/* Zone id match failed, use name to check */
2939 			if (getzonenamebyid(dl_owner_zid, dl_owner_zname,
2940 			    ZONENAME_MAX) < 0) {
2941 				/* No name, show ID instead */
2942 				(void) snprintf(dl_owner_zname, ZONENAME_MAX,
2943 				    "<%d>", dl_owner_zid);
2944 			} else if (strcmp(dl_owner_zname, target_zone) == 0)
2945 				break;
2946 
2947 			/*
2948 			 * Note here we only report a warning that
2949 			 * the interface is already in use by another
2950 			 * running zone, and the verify process just
2951 			 * goes on, if the interface is still in use
2952 			 * when this zone really boots up, zoneadmd
2953 			 * will find it. If the name of the zone which
2954 			 * owns this interface cannot be determined,
2955 			 * then it is not possible to determine if there
2956 			 * is a conflict so just report it as a warning.
2957 			 */
2958 			(void) fprintf(stderr,
2959 			    gettext("WARNING: skipping network interface "
2960 			    "'%s' which is used by the non-global zone "
2961 			    "'%s'.\n"), nwiftab.zone_nwif_physical,
2962 			    dl_owner_zname);
2963 			break;
2964 		}
2965 	}
2966 	(void) zonecfg_endnwifent(handle);
2967 no_net:
2968 
2969 	/* verify that lofs has not been excluded from the kernel */
2970 	if (!(cmd_num == CMD_DETACH || cmd_num == CMD_ATTACH ||
2971 	    cmd_num == CMD_MOVE || cmd_num == CMD_CLONE) &&
2972 	    modctl(MODLOAD, 1, "fs/lofs", NULL) != 0) {
2973 		if (errno == ENXIO)
2974 			(void) fprintf(stderr, gettext("could not verify "
2975 			    "lofs(7FS): possibly excluded in /etc/system\n"));
2976 		else
2977 			(void) fprintf(stderr, gettext("could not verify "
2978 			    "lofs(7FS): %s\n"), strerror(errno));
2979 		return_code = Z_ERR;
2980 	}
2981 
2982 	if (verify_filesystems(handle) != Z_OK)
2983 		return_code = Z_ERR;
2984 	if (verify_ipd(handle) != Z_OK)
2985 		return_code = Z_ERR;
2986 	if (!in_alt_root && verify_rctls(handle) != Z_OK)
2987 		return_code = Z_ERR;
2988 	if (!in_alt_root && verify_pool(handle) != Z_OK)
2989 		return_code = Z_ERR;
2990 	if (!in_alt_root && verify_brand(handle, cmd_num, argv) != Z_OK)
2991 		return_code = Z_ERR;
2992 	if (!in_alt_root && verify_datasets(handle) != Z_OK)
2993 		return_code = Z_ERR;
2994 
2995 	/*
2996 	 * As the "mount" command is used for patching/upgrading of zones
2997 	 * or other maintenance processes, the zone's privilege set is not
2998 	 * checked in this case.  Instead, the default, safe set of
2999 	 * privileges will be used when this zone is created in the
3000 	 * kernel.
3001 	 */
3002 	if (!in_alt_root && cmd_num != CMD_MOUNT &&
3003 	    verify_limitpriv(handle) != Z_OK)
3004 		return_code = Z_ERR;
3005 
3006 	return (return_code);
3007 }
3008 
3009 static int
3010 verify_details(int cmd_num, char *argv[])
3011 {
3012 	zone_dochandle_t handle;
3013 	char zonepath[MAXPATHLEN], checkpath[MAXPATHLEN];
3014 	int return_code = Z_OK;
3015 	int err;
3016 
3017 	if ((handle = zonecfg_init_handle()) == NULL) {
3018 		zperror(cmd_to_str(cmd_num), B_TRUE);
3019 		return (Z_ERR);
3020 	}
3021 	if ((err = zonecfg_get_handle(target_zone, handle)) != Z_OK) {
3022 		errno = err;
3023 		zperror(cmd_to_str(cmd_num), B_TRUE);
3024 		zonecfg_fini_handle(handle);
3025 		return (Z_ERR);
3026 	}
3027 	if ((err = zonecfg_get_zonepath(handle, zonepath, sizeof (zonepath))) !=
3028 	    Z_OK) {
3029 		errno = err;
3030 		zperror(cmd_to_str(cmd_num), B_TRUE);
3031 		zonecfg_fini_handle(handle);
3032 		return (Z_ERR);
3033 	}
3034 	/*
3035 	 * zonecfg_get_zonepath() gets its data from the XML repository.
3036 	 * Verify this against the index file, which is checked first by
3037 	 * zone_get_zonepath().  If they don't match, bail out.
3038 	 */
3039 	if ((err = zone_get_zonepath(target_zone, checkpath,
3040 	    sizeof (checkpath))) != Z_OK) {
3041 		errno = err;
3042 		zperror2(target_zone, gettext("could not get zone path"));
3043 		return (Z_ERR);
3044 	}
3045 	if (strcmp(zonepath, checkpath) != 0) {
3046 		/*
3047 		 * TRANSLATION_NOTE
3048 		 * XML and zonepath are literals that should not be translated.
3049 		 */
3050 		(void) fprintf(stderr, gettext("The XML repository has "
3051 		    "zonepath '%s',\nbut the index file has zonepath '%s'.\n"
3052 		    "These must match, so fix the incorrect entry.\n"),
3053 		    zonepath, checkpath);
3054 		return (Z_ERR);
3055 	}
3056 	if (validate_zonepath(zonepath, cmd_num) != Z_OK) {
3057 		(void) fprintf(stderr, gettext("could not verify zonepath %s "
3058 		    "because of the above errors.\n"), zonepath);
3059 		return_code = Z_ERR;
3060 	}
3061 
3062 	if (verify_handle(cmd_num, handle, argv) != Z_OK)
3063 		return_code = Z_ERR;
3064 
3065 	zonecfg_fini_handle(handle);
3066 	if (return_code == Z_ERR)
3067 		(void) fprintf(stderr,
3068 		    gettext("%s: zone %s failed to verify\n"),
3069 		    execname, target_zone);
3070 	return (return_code);
3071 }
3072 
3073 static int
3074 verify_func(int argc, char *argv[])
3075 {
3076 	int arg;
3077 
3078 	optind = 0;
3079 	if ((arg = getopt(argc, argv, "?")) != EOF) {
3080 		switch (arg) {
3081 		case '?':
3082 			sub_usage(SHELP_VERIFY, CMD_VERIFY);
3083 			return (optopt == '?' ? Z_OK : Z_USAGE);
3084 		default:
3085 			sub_usage(SHELP_VERIFY, CMD_VERIFY);
3086 			return (Z_USAGE);
3087 		}
3088 	}
3089 	if (argc > optind) {
3090 		sub_usage(SHELP_VERIFY, CMD_VERIFY);
3091 		return (Z_USAGE);
3092 	}
3093 	if (sanity_check(target_zone, CMD_VERIFY, B_FALSE, B_FALSE, B_FALSE)
3094 	    != Z_OK)
3095 		return (Z_ERR);
3096 	return (verify_details(CMD_VERIFY, argv));
3097 }
3098 
3099 static int
3100 addopt(char *buf, int opt, char *optarg, size_t bufsize)
3101 {
3102 	char optstring[4];
3103 
3104 	if (opt > 0)
3105 		(void) sprintf(optstring, " -%c", opt);
3106 	else
3107 		(void) strcpy(optstring, " ");
3108 
3109 	if ((strlcat(buf, optstring, bufsize) > bufsize))
3110 		return (Z_ERR);
3111 	if ((optarg != NULL) && (strlcat(buf, optarg, bufsize) > bufsize))
3112 		return (Z_ERR);
3113 	return (Z_OK);
3114 }
3115 
3116 static int
3117 install_func(int argc, char *argv[])
3118 {
3119 	char cmdbuf[MAXPATHLEN];
3120 	int lockfd;
3121 	int arg, err, subproc_err;
3122 	char zonepath[MAXPATHLEN];
3123 	brand_handle_t bh = NULL;
3124 	int status;
3125 	boolean_t nodataset = B_FALSE;
3126 	char opts[128];
3127 
3128 	if (target_zone == NULL) {
3129 		sub_usage(SHELP_INSTALL, CMD_INSTALL);
3130 		return (Z_USAGE);
3131 	}
3132 
3133 	if (zonecfg_in_alt_root()) {
3134 		zerror(gettext("cannot install zone in alternate root"));
3135 		return (Z_ERR);
3136 	}
3137 
3138 	if ((err = zone_get_zonepath(target_zone, zonepath,
3139 	    sizeof (zonepath))) != Z_OK) {
3140 		errno = err;
3141 		zperror2(target_zone, gettext("could not get zone path"));
3142 		return (Z_ERR);
3143 	}
3144 
3145 	/* Fetch the install command from the brand configuration.  */
3146 	if ((bh = brand_open(target_brand)) == NULL) {
3147 		zerror(gettext("missing or invalid brand"));
3148 		return (Z_ERR);
3149 	}
3150 
3151 	(void) strcpy(cmdbuf, EXEC_PREFIX);
3152 	if (brand_get_install(bh, target_zone, zonepath, cmdbuf + EXEC_LEN,
3153 	    sizeof (cmdbuf) - EXEC_LEN, 0, NULL) != 0) {
3154 		zerror("invalid brand configuration: missing install resource");
3155 		brand_close(bh);
3156 		return (Z_ERR);
3157 	}
3158 
3159 	(void) strcpy(opts, "?x:");
3160 	if (!is_native_zone) {
3161 		/*
3162 		 * Fetch the list of recognized command-line options from
3163 		 * the brand configuration file.
3164 		 */
3165 		if (brand_get_installopts(bh, opts + strlen(opts),
3166 		    sizeof (opts) - strlen(opts)) != 0) {
3167 			zerror("invalid brand configuration: missing "
3168 			    "install options resource");
3169 			brand_close(bh);
3170 			return (Z_ERR);
3171 		}
3172 	}
3173 	brand_close(bh);
3174 
3175 	optind = 0;
3176 	while ((arg = getopt(argc, argv, opts)) != EOF) {
3177 		switch (arg) {
3178 		case '?':
3179 			sub_usage(SHELP_INSTALL, CMD_INSTALL);
3180 			return (optopt == '?' ? Z_OK : Z_USAGE);
3181 		case 'x':
3182 			if (strcmp(optarg, "nodataset") != 0) {
3183 				sub_usage(SHELP_INSTALL, CMD_INSTALL);
3184 				return (Z_USAGE);
3185 			}
3186 			nodataset = B_TRUE;
3187 			break;
3188 		default:
3189 			if (is_native_zone) {
3190 				sub_usage(SHELP_INSTALL, CMD_INSTALL);
3191 				return (Z_USAGE);
3192 			}
3193 
3194 			/*
3195 			 * This option isn't for zoneadm, so append it to
3196 			 * the command line passed to the brand-specific
3197 			 * install routine.
3198 			 */
3199 			if (addopt(cmdbuf, optopt, optarg,
3200 			    sizeof (cmdbuf)) != Z_OK) {
3201 				zerror("Install command line too long");
3202 				return (Z_ERR);
3203 			}
3204 			break;
3205 		}
3206 	}
3207 
3208 	if (!is_native_zone) {
3209 		for (; optind < argc; optind++) {
3210 			if (addopt(cmdbuf, 0, argv[optind],
3211 			    sizeof (cmdbuf)) != Z_OK) {
3212 				zerror("Install command line too long");
3213 				return (Z_ERR);
3214 			}
3215 		}
3216 	}
3217 
3218 	if (sanity_check(target_zone, CMD_INSTALL, B_FALSE, B_TRUE, B_FALSE)
3219 	    != Z_OK)
3220 		return (Z_ERR);
3221 	if (verify_details(CMD_INSTALL, argv) != Z_OK)
3222 		return (Z_ERR);
3223 
3224 	if (grab_lock_file(target_zone, &lockfd) != Z_OK) {
3225 		zerror(gettext("another %s may have an operation in progress."),
3226 		    "zoneadm");
3227 		return (Z_ERR);
3228 	}
3229 	err = zone_set_state(target_zone, ZONE_STATE_INCOMPLETE);
3230 	if (err != Z_OK) {
3231 		errno = err;
3232 		zperror2(target_zone, gettext("could not set state"));
3233 		goto done;
3234 	}
3235 
3236 	if (!nodataset)
3237 		create_zfs_zonepath(zonepath);
3238 
3239 	/*
3240 	 * According to the Application Packaging Developer's Guide, a
3241 	 * "checkinstall" script when included in a package is executed as
3242 	 * the user "install", if such a user exists, or by the user
3243 	 * "nobody".  In order to support this dubious behavior, the path
3244 	 * to the zone being constructed is opened up during the life of
3245 	 * the command laying down the zone's root file system.  Once this
3246 	 * has completed, regardless of whether it was successful, the
3247 	 * path to the zone is again restricted.
3248 	 */
3249 	if (chmod(zonepath, DEFAULT_DIR_MODE) != 0) {
3250 		zperror(zonepath, B_FALSE);
3251 		err = Z_ERR;
3252 		goto done;
3253 	}
3254 
3255 	if (is_native_zone)
3256 		status = do_subproc(cmdbuf);
3257 	else
3258 		status = do_subproc_interactive(cmdbuf);
3259 
3260 	if (chmod(zonepath, S_IRWXU) != 0) {
3261 		zperror(zonepath, B_FALSE);
3262 		err = Z_ERR;
3263 		goto done;
3264 	}
3265 	if ((subproc_err =
3266 	    subproc_status(gettext("brand-specific installation"), status,
3267 	    B_FALSE)) != ZONE_SUBPROC_OK) {
3268 		err = Z_ERR;
3269 		goto done;
3270 	}
3271 
3272 	if ((err = zone_set_state(target_zone, ZONE_STATE_INSTALLED)) != Z_OK) {
3273 		errno = err;
3274 		zperror2(target_zone, gettext("could not set state"));
3275 		goto done;
3276 	}
3277 
3278 done:
3279 	/*
3280 	 * If the install script exited with ZONE_SUBPROC_USAGE or
3281 	 * ZONE_SUBPROC_NOTCOMPLETE, try to clean up the zone and leave the
3282 	 * zone in the CONFIGURED state so that another install can be
3283 	 * attempted without requiring an uninstall first.
3284 	 */
3285 	if ((subproc_err == ZONE_SUBPROC_USAGE) ||
3286 	    (subproc_err == ZONE_SUBPROC_NOTCOMPLETE)) {
3287 		if ((err = cleanup_zonepath(zonepath, B_FALSE)) != Z_OK) {
3288 			errno = err;
3289 			zperror2(target_zone,
3290 			    gettext("cleaning up zonepath failed"));
3291 		} else if ((err = zone_set_state(target_zone,
3292 		    ZONE_STATE_CONFIGURED)) != Z_OK) {
3293 			errno = err;
3294 			zperror2(target_zone, gettext("could not set state"));
3295 		}
3296 	}
3297 
3298 	release_lock_file(lockfd);
3299 	return ((err == Z_OK) ? Z_OK : Z_ERR);
3300 }
3301 
3302 /*
3303  * Check that the inherited pkg dirs are the same for the clone and its source.
3304  * The easiest way to do that is check that the list of ipds is the same
3305  * by matching each one against the other.  This algorithm should be fine since
3306  * the list of ipds should not be that long.
3307  */
3308 static int
3309 valid_ipd_clone(zone_dochandle_t s_handle, char *source_zone,
3310 	zone_dochandle_t t_handle, char *target_zone)
3311 {
3312 	int err;
3313 	int res = Z_OK;
3314 	int s_cnt = 0;
3315 	int t_cnt = 0;
3316 	struct zone_fstab s_fstab;
3317 	struct zone_fstab t_fstab;
3318 
3319 	/*
3320 	 * First check the source of the clone against the target.
3321 	 */
3322 	if ((err = zonecfg_setipdent(s_handle)) != Z_OK) {
3323 		errno = err;
3324 		zperror2(source_zone, gettext("could not enumerate "
3325 		    "inherit-pkg-dirs"));
3326 		return (Z_ERR);
3327 	}
3328 
3329 	while (zonecfg_getipdent(s_handle, &s_fstab) == Z_OK) {
3330 		boolean_t match = B_FALSE;
3331 
3332 		s_cnt++;
3333 
3334 		if ((err = zonecfg_setipdent(t_handle)) != Z_OK) {
3335 			errno = err;
3336 			zperror2(target_zone, gettext("could not enumerate "
3337 			    "inherit-pkg-dirs"));
3338 			(void) zonecfg_endipdent(s_handle);
3339 			return (Z_ERR);
3340 		}
3341 
3342 		while (zonecfg_getipdent(t_handle, &t_fstab) == Z_OK) {
3343 			if (strcmp(s_fstab.zone_fs_dir, t_fstab.zone_fs_dir)
3344 			    == 0) {
3345 				match = B_TRUE;
3346 				break;
3347 			}
3348 		}
3349 		(void) zonecfg_endipdent(t_handle);
3350 
3351 		if (!match) {
3352 			(void) fprintf(stderr, gettext("inherit-pkg-dir "
3353 			    "'%s' is not configured in zone %s.\n"),
3354 			    s_fstab.zone_fs_dir, target_zone);
3355 			res = Z_ERR;
3356 		}
3357 	}
3358 
3359 	(void) zonecfg_endipdent(s_handle);
3360 
3361 	/* skip the next check if we already have errors */
3362 	if (res == Z_ERR)
3363 		return (res);
3364 
3365 	/*
3366 	 * Now check the number of ipds in the target so we can verify
3367 	 * that the source is not a subset of the target.
3368 	 */
3369 	if ((err = zonecfg_setipdent(t_handle)) != Z_OK) {
3370 		errno = err;
3371 		zperror2(target_zone, gettext("could not enumerate "
3372 		    "inherit-pkg-dirs"));
3373 		return (Z_ERR);
3374 	}
3375 
3376 	while (zonecfg_getipdent(t_handle, &t_fstab) == Z_OK)
3377 		t_cnt++;
3378 
3379 	(void) zonecfg_endipdent(t_handle);
3380 
3381 	if (t_cnt != s_cnt) {
3382 		(void) fprintf(stderr, gettext("Zone %s is configured "
3383 		    "with inherit-pkg-dirs that are not configured in zone "
3384 		    "%s.\n"), target_zone, source_zone);
3385 		res = Z_ERR;
3386 	}
3387 
3388 	return (res);
3389 }
3390 
3391 static void
3392 warn_dev_match(zone_dochandle_t s_handle, char *source_zone,
3393 	zone_dochandle_t t_handle, char *target_zone)
3394 {
3395 	int err;
3396 	struct zone_devtab s_devtab;
3397 	struct zone_devtab t_devtab;
3398 
3399 	if ((err = zonecfg_setdevent(t_handle)) != Z_OK) {
3400 		errno = err;
3401 		zperror2(target_zone, gettext("could not enumerate devices"));
3402 		return;
3403 	}
3404 
3405 	while (zonecfg_getdevent(t_handle, &t_devtab) == Z_OK) {
3406 		if ((err = zonecfg_setdevent(s_handle)) != Z_OK) {
3407 			errno = err;
3408 			zperror2(source_zone,
3409 			    gettext("could not enumerate devices"));
3410 			(void) zonecfg_enddevent(t_handle);
3411 			return;
3412 		}
3413 
3414 		while (zonecfg_getdevent(s_handle, &s_devtab) == Z_OK) {
3415 			/*
3416 			 * Use fnmatch to catch the case where wildcards
3417 			 * were used in one zone and the other has an
3418 			 * explicit entry (e.g. /dev/dsk/c0t0d0s6 vs.
3419 			 * /dev/\*dsk/c0t0d0s6).
3420 			 */
3421 			if (fnmatch(t_devtab.zone_dev_match,
3422 			    s_devtab.zone_dev_match, FNM_PATHNAME) == 0 ||
3423 			    fnmatch(s_devtab.zone_dev_match,
3424 			    t_devtab.zone_dev_match, FNM_PATHNAME) == 0) {
3425 				(void) fprintf(stderr,
3426 				    gettext("WARNING: device '%s' "
3427 				    "is configured in both zones.\n"),
3428 				    t_devtab.zone_dev_match);
3429 				break;
3430 			}
3431 		}
3432 		(void) zonecfg_enddevent(s_handle);
3433 	}
3434 
3435 	(void) zonecfg_enddevent(t_handle);
3436 }
3437 
3438 /*
3439  * Check if the specified mount option (opt) is contained within the
3440  * options string.
3441  */
3442 static boolean_t
3443 opt_match(char *opt, char *options)
3444 {
3445 	char *p;
3446 	char *lastp;
3447 
3448 	if ((p = strtok_r(options, ",", &lastp)) != NULL) {
3449 		if (strcmp(p, opt) == 0)
3450 			return (B_TRUE);
3451 		while ((p = strtok_r(NULL, ",", &lastp)) != NULL) {
3452 			if (strcmp(p, opt) == 0)
3453 				return (B_TRUE);
3454 		}
3455 	}
3456 
3457 	return (B_FALSE);
3458 }
3459 
3460 #define	RW_LOFS	"WARNING: read-write lofs file system on '%s' is configured " \
3461 	"in both zones.\n"
3462 
3463 static void
3464 print_fs_warnings(struct zone_fstab *s_fstab, struct zone_fstab *t_fstab)
3465 {
3466 	/*
3467 	 * It is ok to have shared lofs mounted fs but we want to warn if
3468 	 * either is rw since this will effect the other zone.
3469 	 */
3470 	if (strcmp(t_fstab->zone_fs_type, "lofs") == 0) {
3471 		zone_fsopt_t *optp;
3472 
3473 		/* The default is rw so no options means rw */
3474 		if (t_fstab->zone_fs_options == NULL ||
3475 		    s_fstab->zone_fs_options == NULL) {
3476 			(void) fprintf(stderr, gettext(RW_LOFS),
3477 			    t_fstab->zone_fs_special);
3478 			return;
3479 		}
3480 
3481 		for (optp = s_fstab->zone_fs_options; optp != NULL;
3482 		    optp = optp->zone_fsopt_next) {
3483 			if (opt_match("rw", optp->zone_fsopt_opt)) {
3484 				(void) fprintf(stderr, gettext(RW_LOFS),
3485 				    s_fstab->zone_fs_special);
3486 				return;
3487 			}
3488 		}
3489 
3490 		for (optp = t_fstab->zone_fs_options; optp != NULL;
3491 		    optp = optp->zone_fsopt_next) {
3492 			if (opt_match("rw", optp->zone_fsopt_opt)) {
3493 				(void) fprintf(stderr, gettext(RW_LOFS),
3494 				    t_fstab->zone_fs_special);
3495 				return;
3496 			}
3497 		}
3498 
3499 		return;
3500 	}
3501 
3502 	/*
3503 	 * TRANSLATION_NOTE
3504 	 * The first variable is the file system type and the second is
3505 	 * the file system special device.  For example,
3506 	 * WARNING: ufs file system on '/dev/dsk/c0t0d0s0' ...
3507 	 */
3508 	(void) fprintf(stderr, gettext("WARNING: %s file system on '%s' "
3509 	    "is configured in both zones.\n"), t_fstab->zone_fs_type,
3510 	    t_fstab->zone_fs_special);
3511 }
3512 
3513 static void
3514 warn_fs_match(zone_dochandle_t s_handle, char *source_zone,
3515 	zone_dochandle_t t_handle, char *target_zone)
3516 {
3517 	int err;
3518 	struct zone_fstab s_fstab;
3519 	struct zone_fstab t_fstab;
3520 
3521 	if ((err = zonecfg_setfsent(t_handle)) != Z_OK) {
3522 		errno = err;
3523 		zperror2(target_zone,
3524 		    gettext("could not enumerate file systems"));
3525 		return;
3526 	}
3527 
3528 	while (zonecfg_getfsent(t_handle, &t_fstab) == Z_OK) {
3529 		if ((err = zonecfg_setfsent(s_handle)) != Z_OK) {
3530 			errno = err;
3531 			zperror2(source_zone,
3532 			    gettext("could not enumerate file systems"));
3533 			(void) zonecfg_endfsent(t_handle);
3534 			return;
3535 		}
3536 
3537 		while (zonecfg_getfsent(s_handle, &s_fstab) == Z_OK) {
3538 			if (strcmp(t_fstab.zone_fs_special,
3539 			    s_fstab.zone_fs_special) == 0) {
3540 				print_fs_warnings(&s_fstab, &t_fstab);
3541 				break;
3542 			}
3543 		}
3544 		(void) zonecfg_endfsent(s_handle);
3545 	}
3546 
3547 	(void) zonecfg_endfsent(t_handle);
3548 }
3549 
3550 /*
3551  * We don't catch the case where you used the same IP address but
3552  * it is not an exact string match.  For example, 192.9.0.128 vs. 192.09.0.128.
3553  * However, we're not going to worry about that but we will check for
3554  * a possible netmask on one of the addresses (e.g. 10.0.0.1 and 10.0.0.1/24)
3555  * and handle that case as a match.
3556  */
3557 static void
3558 warn_ip_match(zone_dochandle_t s_handle, char *source_zone,
3559 	zone_dochandle_t t_handle, char *target_zone)
3560 {
3561 	int err;
3562 	struct zone_nwiftab s_nwiftab;
3563 	struct zone_nwiftab t_nwiftab;
3564 
3565 	if ((err = zonecfg_setnwifent(t_handle)) != Z_OK) {
3566 		errno = err;
3567 		zperror2(target_zone,
3568 		    gettext("could not enumerate network interfaces"));
3569 		return;
3570 	}
3571 
3572 	while (zonecfg_getnwifent(t_handle, &t_nwiftab) == Z_OK) {
3573 		char *p;
3574 
3575 		/* remove an (optional) netmask from the address */
3576 		if ((p = strchr(t_nwiftab.zone_nwif_address, '/')) != NULL)
3577 			*p = '\0';
3578 
3579 		if ((err = zonecfg_setnwifent(s_handle)) != Z_OK) {
3580 			errno = err;
3581 			zperror2(source_zone,
3582 			    gettext("could not enumerate network interfaces"));
3583 			(void) zonecfg_endnwifent(t_handle);
3584 			return;
3585 		}
3586 
3587 		while (zonecfg_getnwifent(s_handle, &s_nwiftab) == Z_OK) {
3588 			/* remove an (optional) netmask from the address */
3589 			if ((p = strchr(s_nwiftab.zone_nwif_address, '/'))
3590 			    != NULL)
3591 				*p = '\0';
3592 
3593 			/* For exclusive-IP zones, address is not specified. */
3594 			if (strlen(s_nwiftab.zone_nwif_address) == 0)
3595 				continue;
3596 
3597 			if (strcmp(t_nwiftab.zone_nwif_address,
3598 			    s_nwiftab.zone_nwif_address) == 0) {
3599 				(void) fprintf(stderr,
3600 				    gettext("WARNING: network address '%s' "
3601 				    "is configured in both zones.\n"),
3602 				    t_nwiftab.zone_nwif_address);
3603 				break;
3604 			}
3605 		}
3606 		(void) zonecfg_endnwifent(s_handle);
3607 	}
3608 
3609 	(void) zonecfg_endnwifent(t_handle);
3610 }
3611 
3612 static void
3613 warn_dataset_match(zone_dochandle_t s_handle, char *source,
3614 	zone_dochandle_t t_handle, char *target)
3615 {
3616 	int err;
3617 	struct zone_dstab s_dstab;
3618 	struct zone_dstab t_dstab;
3619 
3620 	if ((err = zonecfg_setdsent(t_handle)) != Z_OK) {
3621 		errno = err;
3622 		zperror2(target, gettext("could not enumerate datasets"));
3623 		return;
3624 	}
3625 
3626 	while (zonecfg_getdsent(t_handle, &t_dstab) == Z_OK) {
3627 		if ((err = zonecfg_setdsent(s_handle)) != Z_OK) {
3628 			errno = err;
3629 			zperror2(source,
3630 			    gettext("could not enumerate datasets"));
3631 			(void) zonecfg_enddsent(t_handle);
3632 			return;
3633 		}
3634 
3635 		while (zonecfg_getdsent(s_handle, &s_dstab) == Z_OK) {
3636 			if (strcmp(t_dstab.zone_dataset_name,
3637 			    s_dstab.zone_dataset_name) == 0) {
3638 				target_zone = source;
3639 				zerror(gettext("WARNING: dataset '%s' "
3640 				    "is configured in both zones.\n"),
3641 				    t_dstab.zone_dataset_name);
3642 				break;
3643 			}
3644 		}
3645 		(void) zonecfg_enddsent(s_handle);
3646 	}
3647 
3648 	(void) zonecfg_enddsent(t_handle);
3649 }
3650 
3651 /*
3652  * Check that the clone and its source have the same brand type.
3653  */
3654 static int
3655 valid_brand_clone(char *source_zone, char *target_zone)
3656 {
3657 	brand_handle_t bh;
3658 	char source_brand[MAXNAMELEN];
3659 
3660 	if ((zone_get_brand(source_zone, source_brand,
3661 	    sizeof (source_brand))) != Z_OK) {
3662 		(void) fprintf(stderr, "%s: zone '%s': %s\n",
3663 		    execname, source_zone, gettext("missing or invalid brand"));
3664 		return (Z_ERR);
3665 	}
3666 
3667 	if (strcmp(source_brand, target_brand) != NULL) {
3668 		(void) fprintf(stderr,
3669 		    gettext("%s: Zones '%s' and '%s' have different brand "
3670 		    "types.\n"), execname, source_zone, target_zone);
3671 		return (Z_ERR);
3672 	}
3673 
3674 	if ((bh = brand_open(target_brand)) == NULL) {
3675 		zerror(gettext("missing or invalid brand"));
3676 		return (Z_ERR);
3677 	}
3678 	brand_close(bh);
3679 	return (Z_OK);
3680 }
3681 
3682 static int
3683 validate_clone(char *source_zone, char *target_zone)
3684 {
3685 	int err = Z_OK;
3686 	zone_dochandle_t s_handle;
3687 	zone_dochandle_t t_handle;
3688 
3689 	if ((t_handle = zonecfg_init_handle()) == NULL) {
3690 		zperror(cmd_to_str(CMD_CLONE), B_TRUE);
3691 		return (Z_ERR);
3692 	}
3693 	if ((err = zonecfg_get_handle(target_zone, t_handle)) != Z_OK) {
3694 		errno = err;
3695 		zperror(cmd_to_str(CMD_CLONE), B_TRUE);
3696 		zonecfg_fini_handle(t_handle);
3697 		return (Z_ERR);
3698 	}
3699 
3700 	if ((s_handle = zonecfg_init_handle()) == NULL) {
3701 		zperror(cmd_to_str(CMD_CLONE), B_TRUE);
3702 		zonecfg_fini_handle(t_handle);
3703 		return (Z_ERR);
3704 	}
3705 	if ((err = zonecfg_get_handle(source_zone, s_handle)) != Z_OK) {
3706 		errno = err;
3707 		zperror(cmd_to_str(CMD_CLONE), B_TRUE);
3708 		goto done;
3709 	}
3710 
3711 	/* verify new zone has same brand type */
3712 	err = valid_brand_clone(source_zone, target_zone);
3713 	if (err != Z_OK)
3714 		goto done;
3715 
3716 	/* verify new zone has same inherit-pkg-dirs */
3717 	err = valid_ipd_clone(s_handle, source_zone, t_handle, target_zone);
3718 
3719 	/* warn about imported fs's which are the same */
3720 	warn_fs_match(s_handle, source_zone, t_handle, target_zone);
3721 
3722 	/* warn about imported IP addresses which are the same */
3723 	warn_ip_match(s_handle, source_zone, t_handle, target_zone);
3724 
3725 	/* warn about imported devices which are the same */
3726 	warn_dev_match(s_handle, source_zone, t_handle, target_zone);
3727 
3728 	/* warn about imported datasets which are the same */
3729 	warn_dataset_match(s_handle, source_zone, t_handle, target_zone);
3730 
3731 done:
3732 	zonecfg_fini_handle(t_handle);
3733 	zonecfg_fini_handle(s_handle);
3734 
3735 	return ((err == Z_OK) ? Z_OK : Z_ERR);
3736 }
3737 
3738 static int
3739 copy_zone(char *src, char *dst)
3740 {
3741 	boolean_t out_null = B_FALSE;
3742 	int status;
3743 	char *outfile;
3744 	char cmdbuf[MAXPATHLEN * 2 + 128];
3745 
3746 	if ((outfile = tempnam("/var/log", "zone")) == NULL) {
3747 		outfile = "/dev/null";
3748 		out_null = B_TRUE;
3749 	}
3750 
3751 	/*
3752 	 * Use find to get the list of files to copy.  We need to skip
3753 	 * files of type "socket" since cpio can't handle those but that
3754 	 * should be ok since the app will recreate the socket when it runs.
3755 	 * We also need to filter out anything under the .zfs subdir.  Since
3756 	 * find is running depth-first, we need the extra egrep to filter .zfs.
3757 	 */
3758 	(void) snprintf(cmdbuf, sizeof (cmdbuf),
3759 	    "cd %s && /usr/bin/find . -type s -prune -o -depth -print | "
3760 	    "/usr/bin/egrep -v '^\\./\\.zfs$|^\\./\\.zfs/' | "
3761 	    "/usr/bin/cpio -pdmuP@ %s > %s 2>&1",
3762 	    src, dst, outfile);
3763 
3764 	status = do_subproc(cmdbuf);
3765 
3766 	if (subproc_status("copy", status, B_TRUE) != ZONE_SUBPROC_OK) {
3767 		if (!out_null)
3768 			(void) fprintf(stderr, gettext("\nThe copy failed.\n"
3769 			    "More information can be found in %s\n"), outfile);
3770 		return (Z_ERR);
3771 	}
3772 
3773 	if (!out_null)
3774 		(void) unlink(outfile);
3775 
3776 	return (Z_OK);
3777 }
3778 
3779 static int
3780 zone_postclone(char *zonepath)
3781 {
3782 	char cmdbuf[MAXPATHLEN];
3783 	int status;
3784 	brand_handle_t bh;
3785 	int err = Z_OK;
3786 
3787 	/*
3788 	 * Fetch the post-clone command, if any, from the brand
3789 	 * configuration.
3790 	 */
3791 	if ((bh = brand_open(target_brand)) == NULL) {
3792 		zerror(gettext("missing or invalid brand"));
3793 		return (Z_ERR);
3794 	}
3795 	(void) strcpy(cmdbuf, EXEC_PREFIX);
3796 	err = brand_get_postclone(bh, target_zone, zonepath, cmdbuf + EXEC_LEN,
3797 	    sizeof (cmdbuf) - EXEC_LEN, 0, NULL);
3798 	brand_close(bh);
3799 
3800 	if (err == 0 && strlen(cmdbuf) > EXEC_LEN) {
3801 		status = do_subproc(cmdbuf);
3802 		if ((err = subproc_status("postclone", status, B_FALSE))
3803 		    != ZONE_SUBPROC_OK) {
3804 			zerror(gettext("post-clone configuration failed."));
3805 			err = Z_ERR;
3806 		}
3807 	}
3808 
3809 	return (err);
3810 }
3811 
3812 /* ARGSUSED */
3813 static int
3814 zfm_print(const char *p, void *r) {
3815 	zerror("  %s\n", p);
3816 	return (0);
3817 }
3818 
3819 int
3820 clone_copy(char *source_zonepath, char *zonepath)
3821 {
3822 	int err;
3823 
3824 	/* Don't clone the zone if anything is still mounted there */
3825 	if (zonecfg_find_mounts(source_zonepath, NULL, NULL)) {
3826 		zerror(gettext("These file systems are mounted on "
3827 		    "subdirectories of %s.\n"), source_zonepath);
3828 		(void) zonecfg_find_mounts(source_zonepath, zfm_print, NULL);
3829 		return (Z_ERR);
3830 	}
3831 
3832 	/*
3833 	 * Attempt to create a ZFS fs for the zonepath.  As usual, we don't
3834 	 * care if this works or not since we always have the default behavior
3835 	 * of a simple directory for the zonepath.
3836 	 */
3837 	create_zfs_zonepath(zonepath);
3838 
3839 	(void) printf(gettext("Copying %s..."), source_zonepath);
3840 	(void) fflush(stdout);
3841 
3842 	err = copy_zone(source_zonepath, zonepath);
3843 
3844 	(void) printf("\n");
3845 
3846 	return (err);
3847 }
3848 
3849 static int
3850 clone_func(int argc, char *argv[])
3851 {
3852 	char *source_zone = NULL;
3853 	int lockfd;
3854 	int err, arg;
3855 	char zonepath[MAXPATHLEN];
3856 	char source_zonepath[MAXPATHLEN];
3857 	zone_state_t state;
3858 	zone_entry_t *zent;
3859 	char *method = NULL;
3860 	char *snapshot = NULL;
3861 
3862 	if (zonecfg_in_alt_root()) {
3863 		zerror(gettext("cannot clone zone in alternate root"));
3864 		return (Z_ERR);
3865 	}
3866 
3867 	optind = 0;
3868 	if ((arg = getopt(argc, argv, "?m:s:")) != EOF) {
3869 		switch (arg) {
3870 		case '?':
3871 			sub_usage(SHELP_CLONE, CMD_CLONE);
3872 			return (optopt == '?' ? Z_OK : Z_USAGE);
3873 		case 'm':
3874 			method = optarg;
3875 			break;
3876 		case 's':
3877 			snapshot = optarg;
3878 			break;
3879 		default:
3880 			sub_usage(SHELP_CLONE, CMD_CLONE);
3881 			return (Z_USAGE);
3882 		}
3883 	}
3884 	if (argc != (optind + 1) ||
3885 	    (method != NULL && strcmp(method, "copy") != 0)) {
3886 		sub_usage(SHELP_CLONE, CMD_CLONE);
3887 		return (Z_USAGE);
3888 	}
3889 	source_zone = argv[optind];
3890 	if (sanity_check(target_zone, CMD_CLONE, B_FALSE, B_TRUE, B_FALSE)
3891 	    != Z_OK)
3892 		return (Z_ERR);
3893 	if (verify_details(CMD_CLONE, argv) != Z_OK)
3894 		return (Z_ERR);
3895 
3896 	/*
3897 	 * We also need to do some extra validation on the source zone.
3898 	 */
3899 	if (strcmp(source_zone, GLOBAL_ZONENAME) == 0) {
3900 		zerror(gettext("%s operation is invalid for the global zone."),
3901 		    cmd_to_str(CMD_CLONE));
3902 		return (Z_ERR);
3903 	}
3904 
3905 	if (strncmp(source_zone, "SUNW", 4) == 0) {
3906 		zerror(gettext("%s operation is invalid for zones starting "
3907 		    "with SUNW."), cmd_to_str(CMD_CLONE));
3908 		return (Z_ERR);
3909 	}
3910 
3911 	zent = lookup_running_zone(source_zone);
3912 	if (zent != NULL) {
3913 		/* check whether the zone is ready or running */
3914 		if ((err = zone_get_state(zent->zname, &zent->zstate_num))
3915 		    != Z_OK) {
3916 			errno = err;
3917 			zperror2(zent->zname, gettext("could not get state"));
3918 			/* can't tell, so hedge */
3919 			zent->zstate_str = "ready/running";
3920 		} else {
3921 			zent->zstate_str = zone_state_str(zent->zstate_num);
3922 		}
3923 		zerror(gettext("%s operation is invalid for %s zones."),
3924 		    cmd_to_str(CMD_CLONE), zent->zstate_str);
3925 		return (Z_ERR);
3926 	}
3927 
3928 	if ((err = zone_get_state(source_zone, &state)) != Z_OK) {
3929 		errno = err;
3930 		zperror2(source_zone, gettext("could not get state"));
3931 		return (Z_ERR);
3932 	}
3933 	if (state != ZONE_STATE_INSTALLED) {
3934 		(void) fprintf(stderr,
3935 		    gettext("%s: zone %s is %s; %s is required.\n"),
3936 		    execname, source_zone, zone_state_str(state),
3937 		    zone_state_str(ZONE_STATE_INSTALLED));
3938 		return (Z_ERR);
3939 	}
3940 
3941 	/*
3942 	 * The source zone checks out ok, continue with the clone.
3943 	 */
3944 
3945 	if (validate_clone(source_zone, target_zone) != Z_OK)
3946 		return (Z_ERR);
3947 
3948 	if (grab_lock_file(target_zone, &lockfd) != Z_OK) {
3949 		zerror(gettext("another %s may have an operation in progress."),
3950 		    "zoneadm");
3951 		return (Z_ERR);
3952 	}
3953 
3954 	if ((err = zone_get_zonepath(source_zone, source_zonepath,
3955 	    sizeof (source_zonepath))) != Z_OK) {
3956 		errno = err;
3957 		zperror2(source_zone, gettext("could not get zone path"));
3958 		goto done;
3959 	}
3960 
3961 	if ((err = zone_get_zonepath(target_zone, zonepath, sizeof (zonepath)))
3962 	    != Z_OK) {
3963 		errno = err;
3964 		zperror2(target_zone, gettext("could not get zone path"));
3965 		goto done;
3966 	}
3967 
3968 	if ((err = zone_set_state(target_zone, ZONE_STATE_INCOMPLETE))
3969 	    != Z_OK) {
3970 		errno = err;
3971 		zperror2(target_zone, gettext("could not set state"));
3972 		goto done;
3973 	}
3974 
3975 	if (snapshot != NULL) {
3976 		err = clone_snapshot_zfs(snapshot, zonepath);
3977 	} else {
3978 		/*
3979 		 * We always copy the clone unless the source is ZFS and a
3980 		 * ZFS clone worked.  We fallback to copying if the ZFS clone
3981 		 * fails for some reason.
3982 		 */
3983 		err = Z_ERR;
3984 		if (method == NULL && is_zonepath_zfs(source_zonepath))
3985 			err = clone_zfs(source_zone, source_zonepath, zonepath);
3986 
3987 		if (err != Z_OK)
3988 			err = clone_copy(source_zonepath, zonepath);
3989 	}
3990 
3991 	/*
3992 	 * Trusted Extensions requires that cloned zones use the same sysid
3993 	 * configuration, so it is not appropriate to perform any
3994 	 * post-clone reconfiguration.
3995 	 */
3996 	if ((err == Z_OK) && !is_system_labeled())
3997 		err = zone_postclone(zonepath);
3998 
3999 done:
4000 	/*
4001 	 * If everything went well, we mark the zone as installed.
4002 	 */
4003 	if (err == Z_OK) {
4004 		err = zone_set_state(target_zone, ZONE_STATE_INSTALLED);
4005 		if (err != Z_OK) {
4006 			errno = err;
4007 			zperror2(target_zone, gettext("could not set state"));
4008 		}
4009 	}
4010 	release_lock_file(lockfd);
4011 	return ((err == Z_OK) ? Z_OK : Z_ERR);
4012 }
4013 
4014 /*
4015  * Used when removing a zonepath after uninstalling or cleaning up after
4016  * the move subcommand.  This handles a zonepath that has non-standard
4017  * contents so that we will only cleanup the stuff we know about and leave
4018  * any user data alone.
4019  *
4020  * If the "all" parameter is true then we should remove the whole zonepath
4021  * even if it has non-standard files/directories in it.  This can be used when
4022  * we need to cleanup after moving the zonepath across file systems.
4023  *
4024  * We "exec" the RMCOMMAND so that the returned status is that of RMCOMMAND
4025  * and not the shell.
4026  */
4027 static int
4028 cleanup_zonepath(char *zonepath, boolean_t all)
4029 {
4030 	int		status;
4031 	int		i;
4032 	boolean_t	non_std = B_FALSE;
4033 	struct dirent	*dp;
4034 	DIR		*dirp;
4035 			/*
4036 			 * The SUNWattached.xml file is expected since it might
4037 			 * exist if the zone was force-attached after a
4038 			 * migration.
4039 			 */
4040 	char		*std_entries[] = {"dev", "lu", "root",
4041 			    "SUNWattached.xml", NULL};
4042 			/* (MAXPATHLEN * 3) is for the 3 std_entries dirs */
4043 	char		cmdbuf[sizeof (RMCOMMAND) + (MAXPATHLEN * 3) + 64];
4044 
4045 	/*
4046 	 * We shouldn't need these checks but lets be paranoid since we
4047 	 * could blow away the whole system here if we got the wrong zonepath.
4048 	 */
4049 	if (*zonepath == NULL || strcmp(zonepath, "/") == 0) {
4050 		(void) fprintf(stderr, "invalid zonepath '%s'\n", zonepath);
4051 		return (Z_INVAL);
4052 	}
4053 
4054 	/*
4055 	 * If the dirpath is already gone (maybe it was manually removed) then
4056 	 * we just return Z_OK so that the cleanup is successful.
4057 	 */
4058 	if ((dirp = opendir(zonepath)) == NULL)
4059 		return (Z_OK);
4060 
4061 	/*
4062 	 * Look through the zonepath directory to see if there are any
4063 	 * non-standard files/dirs.  Also skip .zfs since that might be
4064 	 * there but we'll handle ZFS file systems as a special case.
4065 	 */
4066 	while ((dp = readdir(dirp)) != NULL) {
4067 		if (strcmp(dp->d_name, ".") == 0 ||
4068 		    strcmp(dp->d_name, "..") == 0 ||
4069 		    strcmp(dp->d_name, ".zfs") == 0)
4070 			continue;
4071 
4072 		for (i = 0; std_entries[i] != NULL; i++)
4073 			if (strcmp(dp->d_name, std_entries[i]) == 0)
4074 				break;
4075 
4076 		if (std_entries[i] == NULL)
4077 			non_std = B_TRUE;
4078 	}
4079 	(void) closedir(dirp);
4080 
4081 	if (!all && non_std) {
4082 		/*
4083 		 * There are extra, non-standard directories/files in the
4084 		 * zonepath so we don't want to remove the zonepath.  We
4085 		 * just want to remove the standard directories and leave
4086 		 * the user data alone.
4087 		 */
4088 		(void) snprintf(cmdbuf, sizeof (cmdbuf), "exec " RMCOMMAND);
4089 
4090 		for (i = 0; std_entries[i] != NULL; i++) {
4091 			char tmpbuf[MAXPATHLEN];
4092 
4093 			if (snprintf(tmpbuf, sizeof (tmpbuf), " %s/%s",
4094 			    zonepath, std_entries[i]) >= sizeof (tmpbuf) ||
4095 			    strlcat(cmdbuf, tmpbuf, sizeof (cmdbuf)) >=
4096 			    sizeof (cmdbuf)) {
4097 				(void) fprintf(stderr,
4098 				    gettext("path is too long\n"));
4099 				return (Z_INVAL);
4100 			}
4101 		}
4102 
4103 		status = do_subproc(cmdbuf);
4104 
4105 		(void) fprintf(stderr, gettext("WARNING: Unable to completely "
4106 		    "remove %s\nbecause it contains additional user data.  "
4107 		    "Only the standard directory\nentries have been "
4108 		    "removed.\n"),
4109 		    zonepath);
4110 
4111 		return ((subproc_status(RMCOMMAND, status, B_TRUE) ==
4112 		    ZONE_SUBPROC_OK) ? Z_OK : Z_ERR);
4113 	}
4114 
4115 	/*
4116 	 * There is nothing unexpected in the zonepath, try to get rid of the
4117 	 * whole zonepath directory.
4118 	 *
4119 	 * If the zonepath is its own zfs file system, try to destroy the
4120 	 * file system.  If that fails for some reason (e.g. it has clones)
4121 	 * then we'll just remove the contents of the zonepath.
4122 	 */
4123 	if (is_zonepath_zfs(zonepath)) {
4124 		if (destroy_zfs(zonepath) == Z_OK)
4125 			return (Z_OK);
4126 		(void) snprintf(cmdbuf, sizeof (cmdbuf), "exec " RMCOMMAND
4127 		    " %s/*", zonepath);
4128 		status = do_subproc(cmdbuf);
4129 		return ((subproc_status(RMCOMMAND, status, B_TRUE) ==
4130 		    ZONE_SUBPROC_OK) ? Z_OK : Z_ERR);
4131 	}
4132 
4133 	(void) snprintf(cmdbuf, sizeof (cmdbuf), "exec " RMCOMMAND " %s",
4134 	    zonepath);
4135 	status = do_subproc(cmdbuf);
4136 
4137 	return ((subproc_status(RMCOMMAND, status, B_TRUE) == ZONE_SUBPROC_OK)
4138 	    ? Z_OK : Z_ERR);
4139 }
4140 
4141 static int
4142 move_func(int argc, char *argv[])
4143 {
4144 	char *new_zonepath = NULL;
4145 	int lockfd;
4146 	int err, arg;
4147 	char zonepath[MAXPATHLEN];
4148 	zone_dochandle_t handle;
4149 	boolean_t fast;
4150 	boolean_t is_zfs = B_FALSE;
4151 	struct dirent *dp;
4152 	DIR *dirp;
4153 	boolean_t empty = B_TRUE;
4154 	boolean_t revert;
4155 	struct stat zonepath_buf;
4156 	struct stat new_zonepath_buf;
4157 
4158 	if (zonecfg_in_alt_root()) {
4159 		zerror(gettext("cannot move zone in alternate root"));
4160 		return (Z_ERR);
4161 	}
4162 
4163 	optind = 0;
4164 	if ((arg = getopt(argc, argv, "?")) != EOF) {
4165 		switch (arg) {
4166 		case '?':
4167 			sub_usage(SHELP_MOVE, CMD_MOVE);
4168 			return (optopt == '?' ? Z_OK : Z_USAGE);
4169 		default:
4170 			sub_usage(SHELP_MOVE, CMD_MOVE);
4171 			return (Z_USAGE);
4172 		}
4173 	}
4174 	if (argc != (optind + 1)) {
4175 		sub_usage(SHELP_MOVE, CMD_MOVE);
4176 		return (Z_USAGE);
4177 	}
4178 	new_zonepath = argv[optind];
4179 	if (sanity_check(target_zone, CMD_MOVE, B_FALSE, B_TRUE, B_FALSE)
4180 	    != Z_OK)
4181 		return (Z_ERR);
4182 	if (verify_details(CMD_MOVE, argv) != Z_OK)
4183 		return (Z_ERR);
4184 
4185 	/*
4186 	 * Check out the new zonepath.  This has the side effect of creating
4187 	 * a directory for the new zonepath.  We depend on this later when we
4188 	 * stat to see if we are doing a cross file system move or not.
4189 	 */
4190 	if (validate_zonepath(new_zonepath, CMD_MOVE) != Z_OK)
4191 		return (Z_ERR);
4192 
4193 	if ((err = zone_get_zonepath(target_zone, zonepath, sizeof (zonepath)))
4194 	    != Z_OK) {
4195 		errno = err;
4196 		zperror2(target_zone, gettext("could not get zone path"));
4197 		return (Z_ERR);
4198 	}
4199 
4200 	if (stat(zonepath, &zonepath_buf) == -1) {
4201 		zperror(gettext("could not stat zone path"), B_FALSE);
4202 		return (Z_ERR);
4203 	}
4204 
4205 	if (stat(new_zonepath, &new_zonepath_buf) == -1) {
4206 		zperror(gettext("could not stat new zone path"), B_FALSE);
4207 		return (Z_ERR);
4208 	}
4209 
4210 	/*
4211 	 * Check if the destination directory is empty.
4212 	 */
4213 	if ((dirp = opendir(new_zonepath)) == NULL) {
4214 		zperror(gettext("could not open new zone path"), B_FALSE);
4215 		return (Z_ERR);
4216 	}
4217 	while ((dp = readdir(dirp)) != (struct dirent *)0) {
4218 		if (strcmp(dp->d_name, ".") == 0 ||
4219 		    strcmp(dp->d_name, "..") == 0)
4220 			continue;
4221 		empty = B_FALSE;
4222 		break;
4223 	}
4224 	(void) closedir(dirp);
4225 
4226 	/* Error if there is anything in the destination directory. */
4227 	if (!empty) {
4228 		(void) fprintf(stderr, gettext("could not move zone to %s: "
4229 		    "directory not empty\n"), new_zonepath);
4230 		return (Z_ERR);
4231 	}
4232 
4233 	/* Don't move the zone if anything is still mounted there */
4234 	if (zonecfg_find_mounts(zonepath, NULL, NULL)) {
4235 		zerror(gettext("These file systems are mounted on "
4236 		    "subdirectories of %s.\n"), zonepath);
4237 		(void) zonecfg_find_mounts(zonepath, zfm_print, NULL);
4238 		return (Z_ERR);
4239 	}
4240 
4241 	/*
4242 	 * Check if we are moving in the same file system and can do a fast
4243 	 * move or if we are crossing file systems and have to copy the data.
4244 	 */
4245 	fast = (zonepath_buf.st_dev == new_zonepath_buf.st_dev);
4246 
4247 	if ((handle = zonecfg_init_handle()) == NULL) {
4248 		zperror(cmd_to_str(CMD_MOVE), B_TRUE);
4249 		return (Z_ERR);
4250 	}
4251 
4252 	if ((err = zonecfg_get_handle(target_zone, handle)) != Z_OK) {
4253 		errno = err;
4254 		zperror(cmd_to_str(CMD_MOVE), B_TRUE);
4255 		zonecfg_fini_handle(handle);
4256 		return (Z_ERR);
4257 	}
4258 
4259 	if (grab_lock_file(target_zone, &lockfd) != Z_OK) {
4260 		zerror(gettext("another %s may have an operation in progress."),
4261 		    "zoneadm");
4262 		zonecfg_fini_handle(handle);
4263 		return (Z_ERR);
4264 	}
4265 
4266 	/*
4267 	 * We're making some file system changes now so we have to clean up
4268 	 * the file system before we are done.  This will either clean up the
4269 	 * new zonepath if the zonecfg update failed or it will clean up the
4270 	 * old zonepath if everything is ok.
4271 	 */
4272 	revert = B_TRUE;
4273 
4274 	if (is_zonepath_zfs(zonepath) &&
4275 	    move_zfs(zonepath, new_zonepath) != Z_ERR) {
4276 		is_zfs = B_TRUE;
4277 
4278 	} else if (fast) {
4279 		/* same file system, use rename for a quick move */
4280 
4281 		/*
4282 		 * Remove the new_zonepath directory that got created above
4283 		 * during the validation.  It gets in the way of the rename.
4284 		 */
4285 		if (rmdir(new_zonepath) != 0) {
4286 			zperror(gettext("could not rmdir new zone path"),
4287 			    B_FALSE);
4288 			zonecfg_fini_handle(handle);
4289 			release_lock_file(lockfd);
4290 			return (Z_ERR);
4291 		}
4292 
4293 		if (rename(zonepath, new_zonepath) != 0) {
4294 			/*
4295 			 * If this fails we don't need to do all of the
4296 			 * cleanup that happens for the rest of the code
4297 			 * so just return from this error.
4298 			 */
4299 			zperror(gettext("could not move zone"), B_FALSE);
4300 			zonecfg_fini_handle(handle);
4301 			release_lock_file(lockfd);
4302 			return (Z_ERR);
4303 		}
4304 
4305 	} else {
4306 		/*
4307 		 * Attempt to create a ZFS fs for the new zonepath.  As usual,
4308 		 * we don't care if this works or not since we always have the
4309 		 * default behavior of a simple directory for the zonepath.
4310 		 */
4311 		create_zfs_zonepath(new_zonepath);
4312 
4313 		(void) printf(gettext(
4314 		    "Moving across file systems; copying zonepath %s..."),
4315 		    zonepath);
4316 		(void) fflush(stdout);
4317 
4318 		err = copy_zone(zonepath, new_zonepath);
4319 
4320 		(void) printf("\n");
4321 		if (err != Z_OK)
4322 			goto done;
4323 	}
4324 
4325 	if ((err = zonecfg_set_zonepath(handle, new_zonepath)) != Z_OK) {
4326 		errno = err;
4327 		zperror(gettext("could not set new zonepath"), B_TRUE);
4328 		goto done;
4329 	}
4330 
4331 	if ((err = zonecfg_save(handle)) != Z_OK) {
4332 		errno = err;
4333 		zperror(gettext("zonecfg save failed"), B_TRUE);
4334 		goto done;
4335 	}
4336 
4337 	revert = B_FALSE;
4338 
4339 done:
4340 	zonecfg_fini_handle(handle);
4341 	release_lock_file(lockfd);
4342 
4343 	/*
4344 	 * Clean up the file system based on how things went.  We either
4345 	 * clean up the new zonepath if the operation failed for some reason
4346 	 * or we clean up the old zonepath if everything is ok.
4347 	 */
4348 	if (revert) {
4349 		/* The zonecfg update failed, cleanup the new zonepath. */
4350 		if (is_zfs) {
4351 			if (move_zfs(new_zonepath, zonepath) == Z_ERR) {
4352 				(void) fprintf(stderr, gettext("could not "
4353 				    "restore zonepath, the zfs mountpoint is "
4354 				    "set as:\n%s\n"), new_zonepath);
4355 				/*
4356 				 * err is already != Z_OK since we're reverting
4357 				 */
4358 			}
4359 
4360 		} else if (fast) {
4361 			if (rename(new_zonepath, zonepath) != 0) {
4362 				zperror(gettext("could not restore zonepath"),
4363 				    B_FALSE);
4364 				/*
4365 				 * err is already != Z_OK since we're reverting
4366 				 */
4367 			}
4368 		} else {
4369 			(void) printf(gettext("Cleaning up zonepath %s..."),
4370 			    new_zonepath);
4371 			(void) fflush(stdout);
4372 			err = cleanup_zonepath(new_zonepath, B_TRUE);
4373 			(void) printf("\n");
4374 
4375 			if (err != Z_OK) {
4376 				errno = err;
4377 				zperror(gettext("could not remove new "
4378 				    "zonepath"), B_TRUE);
4379 			} else {
4380 				/*
4381 				 * Because we're reverting we know the mainline
4382 				 * code failed but we just reused the err
4383 				 * variable so we reset it back to Z_ERR.
4384 				 */
4385 				err = Z_ERR;
4386 			}
4387 		}
4388 
4389 	} else {
4390 		/* The move was successful, cleanup the old zonepath. */
4391 		if (!is_zfs && !fast) {
4392 			(void) printf(
4393 			    gettext("Cleaning up zonepath %s..."), zonepath);
4394 			(void) fflush(stdout);
4395 			err = cleanup_zonepath(zonepath, B_TRUE);
4396 			(void) printf("\n");
4397 
4398 			if (err != Z_OK) {
4399 				errno = err;
4400 				zperror(gettext("could not remove zonepath"),
4401 				    B_TRUE);
4402 			}
4403 		}
4404 	}
4405 
4406 	return ((err == Z_OK) ? Z_OK : Z_ERR);
4407 }
4408 
4409 static int
4410 detach_func(int argc, char *argv[])
4411 {
4412 	int lockfd;
4413 	int err, arg;
4414 	char zonepath[MAXPATHLEN];
4415 	zone_dochandle_t handle;
4416 	boolean_t execute = B_TRUE;
4417 
4418 	if (zonecfg_in_alt_root()) {
4419 		zerror(gettext("cannot detach zone in alternate root"));
4420 		return (Z_ERR);
4421 	}
4422 
4423 	optind = 0;
4424 	if ((arg = getopt(argc, argv, "?n")) != EOF) {
4425 		switch (arg) {
4426 		case '?':
4427 			sub_usage(SHELP_DETACH, CMD_DETACH);
4428 			return (optopt == '?' ? Z_OK : Z_USAGE);
4429 		case 'n':
4430 			execute = B_FALSE;
4431 			break;
4432 		default:
4433 			sub_usage(SHELP_DETACH, CMD_DETACH);
4434 			return (Z_USAGE);
4435 		}
4436 	}
4437 
4438 	if (execute) {
4439 		if (sanity_check(target_zone, CMD_DETACH, B_FALSE, B_TRUE,
4440 		    B_FALSE) != Z_OK)
4441 			return (Z_ERR);
4442 		if (verify_details(CMD_DETACH, argv) != Z_OK)
4443 			return (Z_ERR);
4444 	} else {
4445 		/*
4446 		 * We want a dry-run to work for a non-privileged user so we
4447 		 * only do minimal validation.
4448 		 */
4449 		if (getzoneid() != GLOBAL_ZONEID) {
4450 			zerror(gettext("must be in the global zone to %s a "
4451 			    "zone."), cmd_to_str(CMD_DETACH));
4452 			return (Z_ERR);
4453 		}
4454 
4455 		if (target_zone == NULL) {
4456 			zerror(gettext("no zone specified"));
4457 			return (Z_ERR);
4458 		}
4459 
4460 		if (strcmp(target_zone, GLOBAL_ZONENAME) == 0) {
4461 			zerror(gettext("%s operation is invalid for the "
4462 			    "global zone."), cmd_to_str(CMD_DETACH));
4463 			return (Z_ERR);
4464 		}
4465 	}
4466 
4467 	if ((err = zone_get_zonepath(target_zone, zonepath, sizeof (zonepath)))
4468 	    != Z_OK) {
4469 		errno = err;
4470 		zperror2(target_zone, gettext("could not get zone path"));
4471 		return (Z_ERR);
4472 	}
4473 
4474 	/* Don't detach the zone if anything is still mounted there */
4475 	if (execute && zonecfg_find_mounts(zonepath, NULL, NULL)) {
4476 		zerror(gettext("These file systems are mounted on "
4477 		    "subdirectories of %s.\n"), zonepath);
4478 		(void) zonecfg_find_mounts(zonepath, zfm_print, NULL);
4479 		return (Z_ERR);
4480 	}
4481 
4482 	if ((handle = zonecfg_init_handle()) == NULL) {
4483 		zperror(cmd_to_str(CMD_DETACH), B_TRUE);
4484 		return (Z_ERR);
4485 	}
4486 
4487 	if ((err = zonecfg_get_handle(target_zone, handle)) != Z_OK) {
4488 		errno = err;
4489 		zperror(cmd_to_str(CMD_DETACH), B_TRUE);
4490 		zonecfg_fini_handle(handle);
4491 		return (Z_ERR);
4492 	}
4493 
4494 	if (execute && grab_lock_file(target_zone, &lockfd) != Z_OK) {
4495 		zerror(gettext("another %s may have an operation in progress."),
4496 		    "zoneadm");
4497 		zonecfg_fini_handle(handle);
4498 		return (Z_ERR);
4499 	}
4500 
4501 	if ((err = zonecfg_get_detach_info(handle, B_TRUE)) != Z_OK) {
4502 		errno = err;
4503 		zperror(gettext("getting the detach information failed"),
4504 		    B_TRUE);
4505 		goto done;
4506 	}
4507 
4508 	if ((err = zonecfg_detach_save(handle, (execute ? 0 : ZONE_DRY_RUN)))
4509 	    != Z_OK) {
4510 		errno = err;
4511 		zperror(gettext("saving the detach manifest failed"), B_TRUE);
4512 		goto done;
4513 	}
4514 
4515 	/*
4516 	 * Set the zone state back to configured unless we are running with the
4517 	 * no-execute option.
4518 	 */
4519 	if (execute && (err = zone_set_state(target_zone,
4520 	    ZONE_STATE_CONFIGURED)) != Z_OK) {
4521 		errno = err;
4522 		zperror(gettext("could not reset state"), B_TRUE);
4523 	}
4524 
4525 done:
4526 	zonecfg_fini_handle(handle);
4527 	if (execute)
4528 		release_lock_file(lockfd);
4529 
4530 	return ((err == Z_OK) ? Z_OK : Z_ERR);
4531 }
4532 
4533 /*
4534  * During attach we go through and fix up the /dev entries for the zone
4535  * we are attaching.  In order to regenerate /dev with the correct devices,
4536  * the old /dev will be removed, the zone readied (which generates a new
4537  * /dev) then halted, then we use the info from the manifest to update
4538  * the modes, owners, etc. on the new /dev.
4539  */
4540 static int
4541 dev_fix(zone_dochandle_t handle)
4542 {
4543 	int			res;
4544 	int			err;
4545 	int			status;
4546 	struct zone_devpermtab	devtab;
4547 	zone_cmd_arg_t		zarg;
4548 	char			devpath[MAXPATHLEN];
4549 				/* 6: "exec " and " " */
4550 	char			cmdbuf[sizeof (RMCOMMAND) + MAXPATHLEN + 6];
4551 
4552 	if ((res = zonecfg_get_zonepath(handle, devpath, sizeof (devpath)))
4553 	    != Z_OK)
4554 		return (res);
4555 
4556 	if (strlcat(devpath, "/dev", sizeof (devpath)) >= sizeof (devpath))
4557 		return (Z_TOO_BIG);
4558 
4559 	/*
4560 	 * "exec" the command so that the returned status is that of
4561 	 * RMCOMMAND and not the shell.
4562 	 */
4563 	(void) snprintf(cmdbuf, sizeof (cmdbuf), EXEC_PREFIX RMCOMMAND " %s",
4564 	    devpath);
4565 	status = do_subproc(cmdbuf);
4566 	if ((err = subproc_status(RMCOMMAND, status, B_TRUE)) !=
4567 	    ZONE_SUBPROC_OK) {
4568 		(void) fprintf(stderr,
4569 		    gettext("could not remove existing /dev\n"));
4570 		return (Z_ERR);
4571 	}
4572 
4573 	/* In order to ready the zone, it must be in the installed state */
4574 	if ((err = zone_set_state(target_zone, ZONE_STATE_INSTALLED)) != Z_OK) {
4575 		errno = err;
4576 		zperror(gettext("could not reset state"), B_TRUE);
4577 		return (Z_ERR);
4578 	}
4579 
4580 	/* We have to ready the zone to regen the dev tree */
4581 	zarg.cmd = Z_READY;
4582 	if (call_zoneadmd(target_zone, &zarg) != 0) {
4583 		zerror(gettext("call to %s failed"), "zoneadmd");
4584 		/* attempt to restore zone to configured state */
4585 		(void) zone_set_state(target_zone, ZONE_STATE_CONFIGURED);
4586 		return (Z_ERR);
4587 	}
4588 
4589 	zarg.cmd = Z_HALT;
4590 	if (call_zoneadmd(target_zone, &zarg) != 0) {
4591 		zerror(gettext("call to %s failed"), "zoneadmd");
4592 		/* attempt to restore zone to configured state */
4593 		(void) zone_set_state(target_zone, ZONE_STATE_CONFIGURED);
4594 		return (Z_ERR);
4595 	}
4596 
4597 	/* attempt to restore zone to configured state */
4598 	(void) zone_set_state(target_zone, ZONE_STATE_CONFIGURED);
4599 
4600 	if (zonecfg_setdevperment(handle) != Z_OK) {
4601 		(void) fprintf(stderr,
4602 		    gettext("unable to enumerate device entries\n"));
4603 		return (Z_ERR);
4604 	}
4605 
4606 	while (zonecfg_getdevperment(handle, &devtab) == Z_OK) {
4607 		int err;
4608 
4609 		if ((err = zonecfg_devperms_apply(handle,
4610 		    devtab.zone_devperm_name, devtab.zone_devperm_uid,
4611 		    devtab.zone_devperm_gid, devtab.zone_devperm_mode,
4612 		    devtab.zone_devperm_acl)) != Z_OK && err != Z_INVAL)
4613 			(void) fprintf(stderr, gettext("error updating device "
4614 			    "%s: %s\n"), devtab.zone_devperm_name,
4615 			    zonecfg_strerror(err));
4616 
4617 		free(devtab.zone_devperm_acl);
4618 	}
4619 
4620 	(void) zonecfg_enddevperment(handle);
4621 
4622 	return (Z_OK);
4623 }
4624 
4625 /*
4626  * Validate attaching a zone but don't actually do the work.  The zone
4627  * does not have to exist, so there is some complexity getting a new zone
4628  * configuration set up so that we can perform the validation.  This is
4629  * handled within zonecfg_attach_manifest() which returns two handles; one
4630  * for the the full configuration to validate (rem_handle) and the other
4631  * (local_handle) containing only the zone configuration derived from the
4632  * manifest.
4633  */
4634 static int
4635 dryrun_attach(char *manifest_path, char *argv[])
4636 {
4637 	int fd;
4638 	int err;
4639 	int res;
4640 	zone_dochandle_t local_handle;
4641 	zone_dochandle_t rem_handle = NULL;
4642 
4643 	if (strcmp(manifest_path, "-") == 0) {
4644 		fd = 0;
4645 	} else if ((fd = open(manifest_path, O_RDONLY)) < 0) {
4646 		zperror(gettext("could not open manifest path"), B_FALSE);
4647 		return (Z_ERR);
4648 	}
4649 
4650 	if ((local_handle = zonecfg_init_handle()) == NULL) {
4651 		zperror(cmd_to_str(CMD_ATTACH), B_TRUE);
4652 		res = Z_ERR;
4653 		goto done;
4654 	}
4655 
4656 	if ((rem_handle = zonecfg_init_handle()) == NULL) {
4657 		zperror(cmd_to_str(CMD_ATTACH), B_TRUE);
4658 		res = Z_ERR;
4659 		goto done;
4660 	}
4661 
4662 	if ((err = zonecfg_attach_manifest(fd, local_handle, rem_handle))
4663 	    != Z_OK) {
4664 		res = Z_ERR;
4665 
4666 		if (err == Z_INVALID_DOCUMENT) {
4667 			struct stat st;
4668 			char buf[6];
4669 
4670 			if (strcmp(manifest_path, "-") == 0) {
4671 				zerror(gettext("Input is not a valid XML "
4672 				    "file"));
4673 				goto done;
4674 			}
4675 
4676 			if (fstat(fd, &st) == -1 || !S_ISREG(st.st_mode)) {
4677 				zerror(gettext("%s is not an XML file"),
4678 				    manifest_path);
4679 				goto done;
4680 			}
4681 
4682 			bzero(buf, sizeof (buf));
4683 			(void) lseek(fd, 0L, SEEK_SET);
4684 			if (read(fd, buf, sizeof (buf) - 1) < 0 ||
4685 			    strncmp(buf, "<?xml", 5) != 0)
4686 				zerror(gettext("%s is not an XML file"),
4687 				    manifest_path);
4688 			else
4689 				zerror(gettext("Cannot attach to an earlier "
4690 				    "release of the operating system"));
4691 		} else {
4692 			zperror(cmd_to_str(CMD_ATTACH), B_TRUE);
4693 		}
4694 		goto done;
4695 	}
4696 
4697 	/*
4698 	 * Retrieve remote handle brand type and determine whether it is
4699 	 * native or not.
4700 	 */
4701 	if (zonecfg_get_brand(rem_handle, target_brand, sizeof (target_brand))
4702 	    != Z_OK) {
4703 		zerror(gettext("missing or invalid brand"));
4704 		exit(Z_ERR);
4705 	}
4706 	is_native_zone = (strcmp(target_brand, NATIVE_BRAND_NAME) == 0);
4707 
4708 	res = verify_handle(CMD_ATTACH, local_handle, argv);
4709 
4710 	/* Get the detach information for the locally defined zone. */
4711 	if ((err = zonecfg_get_detach_info(local_handle, B_FALSE)) != Z_OK) {
4712 		errno = err;
4713 		zperror(gettext("getting the attach information failed"),
4714 		    B_TRUE);
4715 		res = Z_ERR;
4716 	} else {
4717 		/* sw_cmp prints error msgs as necessary */
4718 		if (sw_cmp(local_handle, rem_handle, SW_CMP_NONE) != Z_OK)
4719 			res = Z_ERR;
4720 	}
4721 
4722 done:
4723 	if (strcmp(manifest_path, "-") != 0)
4724 		(void) close(fd);
4725 
4726 	zonecfg_fini_handle(local_handle);
4727 	zonecfg_fini_handle(rem_handle);
4728 
4729 	return ((res == Z_OK) ? Z_OK : Z_ERR);
4730 }
4731 
4732 static int
4733 attach_func(int argc, char *argv[])
4734 {
4735 	int lockfd;
4736 	int err, arg;
4737 	boolean_t force = B_FALSE;
4738 	zone_dochandle_t handle;
4739 	zone_dochandle_t athandle = NULL;
4740 	char zonepath[MAXPATHLEN];
4741 	char brand[MAXNAMELEN], atbrand[MAXNAMELEN];
4742 	boolean_t execute = B_TRUE;
4743 	char *manifest_path;
4744 
4745 	if (zonecfg_in_alt_root()) {
4746 		zerror(gettext("cannot attach zone in alternate root"));
4747 		return (Z_ERR);
4748 	}
4749 
4750 	optind = 0;
4751 	if ((arg = getopt(argc, argv, "?Fn:")) != EOF) {
4752 		switch (arg) {
4753 		case '?':
4754 			sub_usage(SHELP_ATTACH, CMD_ATTACH);
4755 			return (optopt == '?' ? Z_OK : Z_USAGE);
4756 		case 'F':
4757 			force = B_TRUE;
4758 			break;
4759 		case 'n':
4760 			execute = B_FALSE;
4761 			manifest_path = optarg;
4762 			break;
4763 		default:
4764 			sub_usage(SHELP_ATTACH, CMD_ATTACH);
4765 			return (Z_USAGE);
4766 		}
4767 	}
4768 
4769 	/*
4770 	 * If the no-execute option was specified, we need to branch down
4771 	 * a completely different path since there is no zone required to be
4772 	 * configured for this option.
4773 	 */
4774 	if (!execute)
4775 		return (dryrun_attach(manifest_path, argv));
4776 
4777 	if (sanity_check(target_zone, CMD_ATTACH, B_FALSE, B_TRUE, B_FALSE)
4778 	    != Z_OK)
4779 		return (Z_ERR);
4780 	if (verify_details(CMD_ATTACH, argv) != Z_OK)
4781 		return (Z_ERR);
4782 
4783 	if ((err = zone_get_zonepath(target_zone, zonepath, sizeof (zonepath)))
4784 	    != Z_OK) {
4785 		errno = err;
4786 		zperror2(target_zone, gettext("could not get zone path"));
4787 		return (Z_ERR);
4788 	}
4789 
4790 	if ((handle = zonecfg_init_handle()) == NULL) {
4791 		zperror(cmd_to_str(CMD_ATTACH), B_TRUE);
4792 		return (Z_ERR);
4793 	}
4794 
4795 	if ((err = zonecfg_get_handle(target_zone, handle)) != Z_OK) {
4796 		errno = err;
4797 		zperror(cmd_to_str(CMD_ATTACH), B_TRUE);
4798 		zonecfg_fini_handle(handle);
4799 		return (Z_ERR);
4800 	}
4801 
4802 	if (grab_lock_file(target_zone, &lockfd) != Z_OK) {
4803 		zerror(gettext("another %s may have an operation in progress."),
4804 		    "zoneadm");
4805 		zonecfg_fini_handle(handle);
4806 		return (Z_ERR);
4807 	}
4808 
4809 	if (force)
4810 		goto forced;
4811 
4812 	if ((athandle = zonecfg_init_handle()) == NULL) {
4813 		zperror(cmd_to_str(CMD_ATTACH), B_TRUE);
4814 		goto done;
4815 	}
4816 
4817 	if ((err = zonecfg_get_attach_handle(zonepath, target_zone, B_TRUE,
4818 	    athandle)) != Z_OK) {
4819 		if (err == Z_NO_ZONE)
4820 			zerror(gettext("Not a detached zone"));
4821 		else if (err == Z_INVALID_DOCUMENT)
4822 			zerror(gettext("Cannot attach to an earlier release "
4823 			    "of the operating system"));
4824 		else
4825 			zperror(cmd_to_str(CMD_ATTACH), B_TRUE);
4826 		goto done;
4827 	}
4828 
4829 	/* Get the detach information for the locally defined zone. */
4830 	if ((err = zonecfg_get_detach_info(handle, B_FALSE)) != Z_OK) {
4831 		errno = err;
4832 		zperror(gettext("getting the attach information failed"),
4833 		    B_TRUE);
4834 		goto done;
4835 	}
4836 
4837 	/*
4838 	 * Ensure that the detached and locally defined zones are both of
4839 	 * the same brand.
4840 	 */
4841 	if ((zonecfg_get_brand(handle, brand, sizeof (brand)) != 0) ||
4842 	    (zonecfg_get_brand(athandle, atbrand, sizeof (atbrand)) != 0)) {
4843 		err = Z_ERR;
4844 		zerror(gettext("missing or invalid brand"));
4845 		goto done;
4846 	}
4847 
4848 	if (strcmp(atbrand, brand) != NULL) {
4849 		err = Z_ERR;
4850 		zerror(gettext("Trying to attach a '%s' zone to a '%s' "
4851 		    "configuration."), atbrand, brand);
4852 		goto done;
4853 	}
4854 
4855 	/* sw_cmp prints error msgs as necessary */
4856 	if ((err = sw_cmp(handle, athandle, SW_CMP_NONE)) != Z_OK)
4857 		goto done;
4858 
4859 	if ((err = dev_fix(athandle)) != Z_OK)
4860 		goto done;
4861 
4862 forced:
4863 
4864 	zonecfg_rm_detached(handle, force);
4865 
4866 	if ((err = zone_set_state(target_zone, ZONE_STATE_INSTALLED)) != Z_OK) {
4867 		errno = err;
4868 		zperror(gettext("could not reset state"), B_TRUE);
4869 	}
4870 
4871 done:
4872 	zonecfg_fini_handle(handle);
4873 	release_lock_file(lockfd);
4874 	if (athandle != NULL)
4875 		zonecfg_fini_handle(athandle);
4876 
4877 	return ((err == Z_OK) ? Z_OK : Z_ERR);
4878 }
4879 
4880 /*
4881  * On input, TRUE => yes, FALSE => no.
4882  * On return, TRUE => 1, FALSE => 0, could not ask => -1.
4883  */
4884 
4885 static int
4886 ask_yesno(boolean_t default_answer, const char *question)
4887 {
4888 	char line[64];	/* should be large enough to answer yes or no */
4889 
4890 	if (!isatty(STDIN_FILENO))
4891 		return (-1);
4892 	for (;;) {
4893 		(void) printf("%s (%s)? ", question,
4894 		    default_answer ? "[y]/n" : "y/[n]");
4895 		if (fgets(line, sizeof (line), stdin) == NULL ||
4896 		    line[0] == '\n')
4897 			return (default_answer ? 1 : 0);
4898 		if (tolower(line[0]) == 'y')
4899 			return (1);
4900 		if (tolower(line[0]) == 'n')
4901 			return (0);
4902 	}
4903 }
4904 
4905 static int
4906 uninstall_func(int argc, char *argv[])
4907 {
4908 	char line[ZONENAME_MAX + 128];	/* Enough for "Are you sure ..." */
4909 	char rootpath[MAXPATHLEN], zonepath[MAXPATHLEN];
4910 	boolean_t force = B_FALSE;
4911 	int lockfd, answer;
4912 	int err, arg;
4913 
4914 	if (zonecfg_in_alt_root()) {
4915 		zerror(gettext("cannot uninstall zone in alternate root"));
4916 		return (Z_ERR);
4917 	}
4918 
4919 	optind = 0;
4920 	while ((arg = getopt(argc, argv, "?F")) != EOF) {
4921 		switch (arg) {
4922 		case '?':
4923 			sub_usage(SHELP_UNINSTALL, CMD_UNINSTALL);
4924 			return (optopt == '?' ? Z_OK : Z_USAGE);
4925 		case 'F':
4926 			force = B_TRUE;
4927 			break;
4928 		default:
4929 			sub_usage(SHELP_UNINSTALL, CMD_UNINSTALL);
4930 			return (Z_USAGE);
4931 		}
4932 	}
4933 	if (argc > optind) {
4934 		sub_usage(SHELP_UNINSTALL, CMD_UNINSTALL);
4935 		return (Z_USAGE);
4936 	}
4937 
4938 	if (sanity_check(target_zone, CMD_UNINSTALL, B_FALSE, B_TRUE, B_FALSE)
4939 	    != Z_OK)
4940 		return (Z_ERR);
4941 
4942 	/*
4943 	 * Invoke brand-specific handler.
4944 	 */
4945 	if (invoke_brand_handler(CMD_UNINSTALL, argv) != Z_OK)
4946 		return (Z_ERR);
4947 
4948 	if (!force) {
4949 		(void) snprintf(line, sizeof (line),
4950 		    gettext("Are you sure you want to %s zone %s"),
4951 		    cmd_to_str(CMD_UNINSTALL), target_zone);
4952 		if ((answer = ask_yesno(B_FALSE, line)) == 0) {
4953 			return (Z_OK);
4954 		} else if (answer == -1) {
4955 			zerror(gettext("Input not from terminal and -F "
4956 			    "not specified: %s not done."),
4957 			    cmd_to_str(CMD_UNINSTALL));
4958 			return (Z_ERR);
4959 		}
4960 	}
4961 
4962 	if ((err = zone_get_zonepath(target_zone, zonepath,
4963 	    sizeof (zonepath))) != Z_OK) {
4964 		errno = err;
4965 		zperror2(target_zone, gettext("could not get zone path"));
4966 		return (Z_ERR);
4967 	}
4968 	if ((err = zone_get_rootpath(target_zone, rootpath,
4969 	    sizeof (rootpath))) != Z_OK) {
4970 		errno = err;
4971 		zperror2(target_zone, gettext("could not get root path"));
4972 		return (Z_ERR);
4973 	}
4974 
4975 	/*
4976 	 * If there seems to be a zoneadmd running for this zone, call it
4977 	 * to tell it that an uninstall is happening; if all goes well it
4978 	 * will then shut itself down.
4979 	 */
4980 	if (ping_zoneadmd(target_zone) == Z_OK) {
4981 		zone_cmd_arg_t zarg;
4982 		zarg.cmd = Z_NOTE_UNINSTALLING;
4983 		/* we don't care too much if this fails... just plow on */
4984 		(void) call_zoneadmd(target_zone, &zarg);
4985 	}
4986 
4987 	if (grab_lock_file(target_zone, &lockfd) != Z_OK) {
4988 		zerror(gettext("another %s may have an operation in progress."),
4989 		    "zoneadm");
4990 		return (Z_ERR);
4991 	}
4992 
4993 	/* Don't uninstall the zone if anything is mounted there */
4994 	err = zonecfg_find_mounts(rootpath, NULL, NULL);
4995 	if (err) {
4996 		zerror(gettext("These file systems are mounted on "
4997 		    "subdirectories of %s.\n"), rootpath);
4998 		(void) zonecfg_find_mounts(rootpath, zfm_print, NULL);
4999 		return (Z_ERR);
5000 	}
5001 
5002 	err = zone_set_state(target_zone, ZONE_STATE_INCOMPLETE);
5003 	if (err != Z_OK) {
5004 		errno = err;
5005 		zperror2(target_zone, gettext("could not set state"));
5006 		goto bad;
5007 	}
5008 
5009 	if ((err = cleanup_zonepath(zonepath, B_FALSE)) != Z_OK) {
5010 		errno = err;
5011 		zperror2(target_zone, gettext("cleaning up zonepath failed"));
5012 		goto bad;
5013 	}
5014 
5015 	err = zone_set_state(target_zone, ZONE_STATE_CONFIGURED);
5016 	if (err != Z_OK) {
5017 		errno = err;
5018 		zperror2(target_zone, gettext("could not reset state"));
5019 	}
5020 bad:
5021 	release_lock_file(lockfd);
5022 	return (err);
5023 }
5024 
5025 /* ARGSUSED */
5026 static int
5027 mount_func(int argc, char *argv[])
5028 {
5029 	zone_cmd_arg_t zarg;
5030 	boolean_t force = B_FALSE;
5031 	int arg;
5032 
5033 	/*
5034 	 * The only supported subargument to the "mount" subcommand is
5035 	 * "-f", which forces us to mount a zone in the INCOMPLETE state.
5036 	 */
5037 	optind = 0;
5038 	if ((arg = getopt(argc, argv, "f")) != EOF) {
5039 		switch (arg) {
5040 		case 'f':
5041 			force = B_TRUE;
5042 			break;
5043 		default:
5044 			return (Z_USAGE);
5045 		}
5046 	}
5047 	if (argc > optind)
5048 		return (Z_USAGE);
5049 
5050 	if (sanity_check(target_zone, CMD_MOUNT, B_FALSE, B_FALSE, force)
5051 	    != Z_OK)
5052 		return (Z_ERR);
5053 	if (verify_details(CMD_MOUNT, argv) != Z_OK)
5054 		return (Z_ERR);
5055 
5056 	zarg.cmd = force ? Z_FORCEMOUNT : Z_MOUNT;
5057 	if (call_zoneadmd(target_zone, &zarg) != 0) {
5058 		zerror(gettext("call to %s failed"), "zoneadmd");
5059 		return (Z_ERR);
5060 	}
5061 	return (Z_OK);
5062 }
5063 
5064 /* ARGSUSED */
5065 static int
5066 unmount_func(int argc, char *argv[])
5067 {
5068 	zone_cmd_arg_t zarg;
5069 
5070 	if (argc > 0)
5071 		return (Z_USAGE);
5072 	if (sanity_check(target_zone, CMD_UNMOUNT, B_FALSE, B_FALSE, B_FALSE)
5073 	    != Z_OK)
5074 		return (Z_ERR);
5075 
5076 	zarg.cmd = Z_UNMOUNT;
5077 	if (call_zoneadmd(target_zone, &zarg) != 0) {
5078 		zerror(gettext("call to %s failed"), "zoneadmd");
5079 		return (Z_ERR);
5080 	}
5081 	return (Z_OK);
5082 }
5083 
5084 static int
5085 mark_func(int argc, char *argv[])
5086 {
5087 	int err, lockfd;
5088 
5089 	if (argc != 1 || strcmp(argv[0], "incomplete") != 0)
5090 		return (Z_USAGE);
5091 	if (sanity_check(target_zone, CMD_MARK, B_FALSE, B_FALSE, B_FALSE)
5092 	    != Z_OK)
5093 		return (Z_ERR);
5094 
5095 	/*
5096 	 * Invoke brand-specific handler.
5097 	 */
5098 	if (invoke_brand_handler(CMD_MARK, argv) != Z_OK)
5099 		return (Z_ERR);
5100 
5101 	if (grab_lock_file(target_zone, &lockfd) != Z_OK) {
5102 		zerror(gettext("another %s may have an operation in progress."),
5103 		    "zoneadm");
5104 		return (Z_ERR);
5105 	}
5106 
5107 	err = zone_set_state(target_zone, ZONE_STATE_INCOMPLETE);
5108 	if (err != Z_OK) {
5109 		errno = err;
5110 		zperror2(target_zone, gettext("could not set state"));
5111 	}
5112 	release_lock_file(lockfd);
5113 
5114 	return (err);
5115 }
5116 
5117 /*
5118  * Check what scheduling class we're running under and print a warning if
5119  * we're not using FSS.
5120  */
5121 static int
5122 check_sched_fss(zone_dochandle_t handle)
5123 {
5124 	char class_name[PC_CLNMSZ];
5125 
5126 	if (zonecfg_get_dflt_sched_class(handle, class_name,
5127 	    sizeof (class_name)) != Z_OK) {
5128 		zerror(gettext("WARNING: unable to determine the zone's "
5129 		    "scheduling class"));
5130 	} else if (strcmp("FSS", class_name) != 0) {
5131 		zerror(gettext("WARNING: The zone.cpu-shares rctl is set but\n"
5132 		    "FSS is not the default scheduling class for this zone.  "
5133 		    "FSS will be\nused for processes in the zone but to get "
5134 		    "the full benefit of FSS,\nit should be the default "
5135 		    "scheduling class.  See dispadmin(1M) for\nmore details."));
5136 		return (Z_SYSTEM);
5137 	}
5138 
5139 	return (Z_OK);
5140 }
5141 
5142 static int
5143 check_cpu_shares_sched(zone_dochandle_t handle)
5144 {
5145 	int err;
5146 	int res = Z_OK;
5147 	struct zone_rctltab rctl;
5148 
5149 	if ((err = zonecfg_setrctlent(handle)) != Z_OK) {
5150 		errno = err;
5151 		zperror(cmd_to_str(CMD_APPLY), B_TRUE);
5152 		return (err);
5153 	}
5154 
5155 	while (zonecfg_getrctlent(handle, &rctl) == Z_OK) {
5156 		if (strcmp(rctl.zone_rctl_name, "zone.cpu-shares") == 0) {
5157 			if (check_sched_fss(handle) != Z_OK)
5158 				res = Z_SYSTEM;
5159 			break;
5160 		}
5161 	}
5162 
5163 	(void) zonecfg_endrctlent(handle);
5164 
5165 	return (res);
5166 }
5167 
5168 /*
5169  * Check if there is a mix of processes running in different pools within the
5170  * zone.  This is currently only going to be called for the global zone from
5171  * apply_func but that could be generalized in the future.
5172  */
5173 static boolean_t
5174 mixed_pools(zoneid_t zoneid)
5175 {
5176 	DIR *dirp;
5177 	dirent_t *dent;
5178 	boolean_t mixed = B_FALSE;
5179 	boolean_t poolid_set = B_FALSE;
5180 	poolid_t last_poolid = 0;
5181 
5182 	if ((dirp = opendir("/proc")) == NULL) {
5183 		zerror(gettext("could not open /proc"));
5184 		return (B_FALSE);
5185 	}
5186 
5187 	while ((dent = readdir(dirp)) != NULL) {
5188 		int procfd;
5189 		psinfo_t ps;
5190 		char procpath[MAXPATHLEN];
5191 
5192 		if (dent->d_name[0] == '.')
5193 			continue;
5194 
5195 		(void) snprintf(procpath, sizeof (procpath), "/proc/%s/psinfo",
5196 		    dent->d_name);
5197 
5198 		if ((procfd = open(procpath, O_RDONLY)) == -1)
5199 			continue;
5200 
5201 		if (read(procfd, &ps, sizeof (ps)) == sizeof (psinfo_t)) {
5202 			/* skip processes in other zones and system processes */
5203 			if (zoneid != ps.pr_zoneid || ps.pr_flag & SSYS) {
5204 				(void) close(procfd);
5205 				continue;
5206 			}
5207 
5208 			if (poolid_set) {
5209 				if (ps.pr_poolid != last_poolid)
5210 					mixed = B_TRUE;
5211 			} else {
5212 				last_poolid = ps.pr_poolid;
5213 				poolid_set = B_TRUE;
5214 			}
5215 		}
5216 
5217 		(void) close(procfd);
5218 
5219 		if (mixed)
5220 			break;
5221 	}
5222 
5223 	(void) closedir(dirp);
5224 
5225 	return (mixed);
5226 }
5227 
5228 /*
5229  * Check if a persistent or temporary pool is configured for the zone.
5230  * This is currently only going to be called for the global zone from
5231  * apply_func but that could be generalized in the future.
5232  */
5233 static boolean_t
5234 pool_configured(zone_dochandle_t handle)
5235 {
5236 	int err1, err2;
5237 	struct zone_psettab pset_tab;
5238 	char poolname[MAXPATHLEN];
5239 
5240 	err1 = zonecfg_lookup_pset(handle, &pset_tab);
5241 	err2 = zonecfg_get_pool(handle, poolname, sizeof (poolname));
5242 
5243 	if (err1 == Z_NO_ENTRY &&
5244 	    (err2 == Z_NO_ENTRY || (err2 == Z_OK && strlen(poolname) == 0)))
5245 		return (B_FALSE);
5246 
5247 	return (B_TRUE);
5248 }
5249 
5250 /*
5251  * This is an undocumented interface which is currently only used to apply
5252  * the global zone resource management settings when the system boots.
5253  * This function does not yet properly handle updating a running system so
5254  * any projects running in the zone would be trashed if this function
5255  * were to run after the zone had booted.  It also does not reset any
5256  * rctl settings that were removed from zonecfg.  There is still work to be
5257  * done before we can properly support dynamically updating the resource
5258  * management settings for a running zone (global or non-global).  Thus, this
5259  * functionality is undocumented for now.
5260  */
5261 /* ARGSUSED */
5262 static int
5263 apply_func(int argc, char *argv[])
5264 {
5265 	int err;
5266 	int res = Z_OK;
5267 	priv_set_t *privset;
5268 	zoneid_t zoneid;
5269 	zone_dochandle_t handle;
5270 	struct zone_mcaptab mcap;
5271 	char pool_err[128];
5272 
5273 	zoneid = getzoneid();
5274 
5275 	if (zonecfg_in_alt_root() || zoneid != GLOBAL_ZONEID ||
5276 	    target_zone == NULL || strcmp(target_zone, GLOBAL_ZONENAME) != 0)
5277 		return (usage(B_FALSE));
5278 
5279 	if ((privset = priv_allocset()) == NULL) {
5280 		zerror(gettext("%s failed"), "priv_allocset");
5281 		return (Z_ERR);
5282 	}
5283 
5284 	if (getppriv(PRIV_EFFECTIVE, privset) != 0) {
5285 		zerror(gettext("%s failed"), "getppriv");
5286 		priv_freeset(privset);
5287 		return (Z_ERR);
5288 	}
5289 
5290 	if (priv_isfullset(privset) == B_FALSE) {
5291 		(void) usage(B_FALSE);
5292 		priv_freeset(privset);
5293 		return (Z_ERR);
5294 	}
5295 	priv_freeset(privset);
5296 
5297 	if ((handle = zonecfg_init_handle()) == NULL) {
5298 		zperror(cmd_to_str(CMD_APPLY), B_TRUE);
5299 		return (Z_ERR);
5300 	}
5301 
5302 	if ((err = zonecfg_get_handle(target_zone, handle)) != Z_OK) {
5303 		errno = err;
5304 		zperror(cmd_to_str(CMD_APPLY), B_TRUE);
5305 		zonecfg_fini_handle(handle);
5306 		return (Z_ERR);
5307 	}
5308 
5309 	/* specific error msgs are printed within apply_rctls */
5310 	if ((err = zonecfg_apply_rctls(target_zone, handle)) != Z_OK) {
5311 		errno = err;
5312 		zperror(cmd_to_str(CMD_APPLY), B_TRUE);
5313 		res = Z_ERR;
5314 	}
5315 
5316 	if ((err = check_cpu_shares_sched(handle)) != Z_OK)
5317 		res = Z_ERR;
5318 
5319 	if (pool_configured(handle)) {
5320 		if (mixed_pools(zoneid)) {
5321 			zerror(gettext("Zone is using multiple resource "
5322 			    "pools.  The pool\nconfiguration cannot be "
5323 			    "applied without rebooting."));
5324 			res = Z_ERR;
5325 		} else {
5326 
5327 			/*
5328 			 * The next two blocks of code attempt to set up
5329 			 * temporary pools as well as persistent pools.  In
5330 			 * both cases we call the functions unconditionally.
5331 			 * Within each funtion the code will check if the zone
5332 			 * is actually configured for a temporary pool or
5333 			 * persistent pool and just return if there is nothing
5334 			 * to do.
5335 			 */
5336 			if ((err = zonecfg_bind_tmp_pool(handle, zoneid,
5337 			    pool_err, sizeof (pool_err))) != Z_OK) {
5338 				if (err == Z_POOL || err == Z_POOL_CREATE ||
5339 				    err == Z_POOL_BIND)
5340 					zerror("%s: %s", zonecfg_strerror(err),
5341 					    pool_err);
5342 				else
5343 					zerror(gettext("could not bind zone to "
5344 					    "temporary pool: %s"),
5345 					    zonecfg_strerror(err));
5346 				res = Z_ERR;
5347 			}
5348 
5349 			if ((err = zonecfg_bind_pool(handle, zoneid, pool_err,
5350 			    sizeof (pool_err))) != Z_OK) {
5351 				if (err == Z_POOL || err == Z_POOL_BIND)
5352 					zerror("%s: %s", zonecfg_strerror(err),
5353 					    pool_err);
5354 				else
5355 					zerror("%s", zonecfg_strerror(err));
5356 			}
5357 		}
5358 	}
5359 
5360 	/*
5361 	 * If a memory cap is configured, set the cap in the kernel using
5362 	 * zone_setattr() and make sure the rcapd SMF service is enabled.
5363 	 */
5364 	if (zonecfg_getmcapent(handle, &mcap) == Z_OK) {
5365 		uint64_t num;
5366 		char smf_err[128];
5367 
5368 		num = (uint64_t)strtoll(mcap.zone_physmem_cap, NULL, 10);
5369 		if (zone_setattr(zoneid, ZONE_ATTR_PHYS_MCAP, &num, 0) == -1) {
5370 			zerror(gettext("could not set zone memory cap"));
5371 			res = Z_ERR;
5372 		}
5373 
5374 		if (zonecfg_enable_rcapd(smf_err, sizeof (smf_err)) != Z_OK) {
5375 			zerror(gettext("enabling system/rcap service failed: "
5376 			    "%s"), smf_err);
5377 			res = Z_ERR;
5378 		}
5379 	}
5380 
5381 	zonecfg_fini_handle(handle);
5382 
5383 	return (res);
5384 }
5385 
5386 static int
5387 help_func(int argc, char *argv[])
5388 {
5389 	int arg, cmd_num;
5390 
5391 	if (argc == 0) {
5392 		(void) usage(B_TRUE);
5393 		return (Z_OK);
5394 	}
5395 	optind = 0;
5396 	if ((arg = getopt(argc, argv, "?")) != EOF) {
5397 		switch (arg) {
5398 		case '?':
5399 			sub_usage(SHELP_HELP, CMD_HELP);
5400 			return (optopt == '?' ? Z_OK : Z_USAGE);
5401 		default:
5402 			sub_usage(SHELP_HELP, CMD_HELP);
5403 			return (Z_USAGE);
5404 		}
5405 	}
5406 	while (optind < argc) {
5407 		/* Private commands have NULL short_usage; omit them */
5408 		if ((cmd_num = cmd_match(argv[optind])) < 0 ||
5409 		    cmdtab[cmd_num].short_usage == NULL) {
5410 			sub_usage(SHELP_HELP, CMD_HELP);
5411 			return (Z_USAGE);
5412 		}
5413 		sub_usage(cmdtab[cmd_num].short_usage, cmd_num);
5414 		optind++;
5415 	}
5416 	return (Z_OK);
5417 }
5418 
5419 /*
5420  * Returns: CMD_MIN thru CMD_MAX on success, -1 on error
5421  */
5422 
5423 static int
5424 cmd_match(char *cmd)
5425 {
5426 	int i;
5427 
5428 	for (i = CMD_MIN; i <= CMD_MAX; i++) {
5429 		/* return only if there is an exact match */
5430 		if (strcmp(cmd, cmdtab[i].cmd_name) == 0)
5431 			return (cmdtab[i].cmd_num);
5432 	}
5433 	return (-1);
5434 }
5435 
5436 static int
5437 parse_and_run(int argc, char *argv[])
5438 {
5439 	int i = cmd_match(argv[0]);
5440 
5441 	if (i < 0)
5442 		return (usage(B_FALSE));
5443 	return (cmdtab[i].handler(argc - 1, &(argv[1])));
5444 }
5445 
5446 static char *
5447 get_execbasename(char *execfullname)
5448 {
5449 	char *last_slash, *execbasename;
5450 
5451 	/* guard against '/' at end of command invocation */
5452 	for (;;) {
5453 		last_slash = strrchr(execfullname, '/');
5454 		if (last_slash == NULL) {
5455 			execbasename = execfullname;
5456 			break;
5457 		} else {
5458 			execbasename = last_slash + 1;
5459 			if (*execbasename == '\0') {
5460 				*last_slash = '\0';
5461 				continue;
5462 			}
5463 			break;
5464 		}
5465 	}
5466 	return (execbasename);
5467 }
5468 
5469 int
5470 main(int argc, char **argv)
5471 {
5472 	int arg;
5473 	zoneid_t zid;
5474 	struct stat st;
5475 	char *zone_lock_env;
5476 	int err;
5477 
5478 	if ((locale = setlocale(LC_ALL, "")) == NULL)
5479 		locale = "C";
5480 	(void) textdomain(TEXT_DOMAIN);
5481 	setbuf(stdout, NULL);
5482 	(void) sigset(SIGHUP, SIG_IGN);
5483 	execname = get_execbasename(argv[0]);
5484 	target_zone = NULL;
5485 	if (chdir("/") != 0) {
5486 		zerror(gettext("could not change directory to /."));
5487 		exit(Z_ERR);
5488 	}
5489 
5490 	if (init_zfs() != Z_OK)
5491 		exit(Z_ERR);
5492 
5493 	while ((arg = getopt(argc, argv, "?u:z:R:")) != EOF) {
5494 		switch (arg) {
5495 		case '?':
5496 			return (usage(B_TRUE));
5497 		case 'u':
5498 			target_uuid = optarg;
5499 			break;
5500 		case 'z':
5501 			target_zone = optarg;
5502 			break;
5503 		case 'R':	/* private option for admin/install use */
5504 			if (*optarg != '/') {
5505 				zerror(gettext("root path must be absolute."));
5506 				exit(Z_ERR);
5507 			}
5508 			if (stat(optarg, &st) == -1 || !S_ISDIR(st.st_mode)) {
5509 				zerror(
5510 				    gettext("root path must be a directory."));
5511 				exit(Z_ERR);
5512 			}
5513 			zonecfg_set_root(optarg);
5514 			break;
5515 		default:
5516 			return (usage(B_FALSE));
5517 		}
5518 	}
5519 
5520 	if (optind >= argc)
5521 		return (usage(B_FALSE));
5522 
5523 	if (target_uuid != NULL && *target_uuid != '\0') {
5524 		uuid_t uuid;
5525 		static char newtarget[ZONENAME_MAX];
5526 
5527 		if (uuid_parse(target_uuid, uuid) == -1) {
5528 			zerror(gettext("illegal UUID value specified"));
5529 			exit(Z_ERR);
5530 		}
5531 		if (zonecfg_get_name_by_uuid(uuid, newtarget,
5532 		    sizeof (newtarget)) == Z_OK)
5533 			target_zone = newtarget;
5534 	}
5535 
5536 	if (target_zone != NULL && zone_get_id(target_zone, &zid) != 0) {
5537 		errno = Z_NO_ZONE;
5538 		zperror(target_zone, B_TRUE);
5539 		exit(Z_ERR);
5540 	}
5541 
5542 	/*
5543 	 * See if we have inherited the right to manipulate this zone from
5544 	 * a zoneadm instance in our ancestry.  If so, set zone_lock_cnt to
5545 	 * indicate it.  If not, make that explicit in our environment.
5546 	 */
5547 	zone_lock_env = getenv(LOCK_ENV_VAR);
5548 	if (zone_lock_env == NULL) {
5549 		if (putenv(zoneadm_lock_not_held) != 0) {
5550 			zperror(target_zone, B_TRUE);
5551 			exit(Z_ERR);
5552 		}
5553 	} else {
5554 		zoneadm_is_nested = B_TRUE;
5555 		if (atoi(zone_lock_env) == 1)
5556 			zone_lock_cnt = 1;
5557 	}
5558 
5559 	/*
5560 	 * If we are going to be operating on a single zone, retrieve its
5561 	 * brand type and determine whether it is native or not.
5562 	 */
5563 	if ((target_zone != NULL) &&
5564 	    (strcmp(target_zone, GLOBAL_ZONENAME) != NULL)) {
5565 		if (zone_get_brand(target_zone, target_brand,
5566 		    sizeof (target_brand)) != Z_OK) {
5567 			zerror(gettext("missing or invalid brand"));
5568 			exit(Z_ERR);
5569 		}
5570 		is_native_zone = (strcmp(target_brand, NATIVE_BRAND_NAME) == 0);
5571 	}
5572 
5573 	err = parse_and_run(argc - optind, &argv[optind]);
5574 
5575 	return (err);
5576 }
5577