xref: /illumos-gate/usr/src/cmd/zoneadmd/zoneadmd.c (revision 2a347f24)
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 (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Copyright 2014 Nexenta Systems, Inc. All rights reserved.
25  * Copyright (c) 2016 by Delphix. All rights reserved.
26  */
27 
28 /*
29  * zoneadmd manages zones; one zoneadmd process is launched for each
30  * non-global zone on the system.  This daemon juggles four jobs:
31  *
32  * - Implement setup and teardown of the zone "virtual platform": mount and
33  *   unmount filesystems; create and destroy network interfaces; communicate
34  *   with devfsadmd to lay out devices for the zone; instantiate the zone
35  *   console device; configure process runtime attributes such as resource
36  *   controls, pool bindings, fine-grained privileges.
37  *
38  * - Launch the zone's init(8) process.
39  *
40  * - Implement a door server; clients (like zoneadm) connect to the door
41  *   server and request zone state changes.  The kernel is also a client of
42  *   this door server.  A request to halt or reboot the zone which originates
43  *   *inside* the zone results in a door upcall from the kernel into zoneadmd.
44  *
45  *   One minor problem is that messages emitted by zoneadmd need to be passed
46  *   back to the zoneadm process making the request.  These messages need to
47  *   be rendered in the client's locale; so, this is passed in as part of the
48  *   request.  The exception is the kernel upcall to zoneadmd, in which case
49  *   messages are syslog'd.
50  *
51  *   To make all of this work, the Makefile adds -a to xgettext to extract *all*
52  *   strings, and an exclusion file (zoneadmd.xcl) is used to exclude those
53  *   strings which do not need to be translated.
54  *
55  * - Act as a console server for zlogin -C processes; see comments in zcons.c
56  *   for more information about the zone console architecture.
57  *
58  * DESIGN NOTES
59  *
60  * Restart:
61  *   A chief design constraint of zoneadmd is that it should be restartable in
62  *   the case that the administrator kills it off, or it suffers a fatal error,
63  *   without the running zone being impacted; this is akin to being able to
64  *   reboot the service processor of a server without affecting the OS instance.
65  */
66 
67 #include <sys/param.h>
68 #include <sys/mman.h>
69 #include <sys/types.h>
70 #include <sys/stat.h>
71 #include <sys/sysmacros.h>
72 
73 #include <bsm/adt.h>
74 #include <bsm/adt_event.h>
75 
76 #include <alloca.h>
77 #include <assert.h>
78 #include <errno.h>
79 #include <door.h>
80 #include <fcntl.h>
81 #include <locale.h>
82 #include <signal.h>
83 #include <stdarg.h>
84 #include <stdio.h>
85 #include <stdlib.h>
86 #include <string.h>
87 #include <strings.h>
88 #include <synch.h>
89 #include <syslog.h>
90 #include <thread.h>
91 #include <unistd.h>
92 #include <wait.h>
93 #include <limits.h>
94 #include <zone.h>
95 #include <libbrand.h>
96 #include <sys/brand.h>
97 #include <libcontract.h>
98 #include <libcontract_priv.h>
99 #include <sys/brand.h>
100 #include <sys/contract/process.h>
101 #include <sys/ctfs.h>
102 #include <libdladm.h>
103 #include <sys/dls_mgmt.h>
104 #include <libscf.h>
105 
106 #include <libzonecfg.h>
107 #include <zonestat_impl.h>
108 #include "zoneadmd.h"
109 
110 static char *progname;
111 char *zone_name;	/* zone which we are managing */
112 char pool_name[MAXNAMELEN];
113 char default_brand[MAXNAMELEN];
114 char brand_name[MAXNAMELEN];
115 boolean_t zone_isnative;
116 boolean_t zone_iscluster;
117 boolean_t zone_islabeled;
118 boolean_t shutdown_in_progress;
119 static zoneid_t zone_id;
120 dladm_handle_t dld_handle = NULL;
121 
122 static char pre_statechg_hook[2 * MAXPATHLEN];
123 static char post_statechg_hook[2 * MAXPATHLEN];
124 char query_hook[2 * MAXPATHLEN];
125 
126 zlog_t logsys;
127 
128 mutex_t	lock = DEFAULTMUTEX;	/* to serialize stuff */
129 mutex_t	msglock = DEFAULTMUTEX;	/* for calling setlocale() */
130 
131 static sema_t scratch_sem;	/* for scratch zones */
132 
133 static char	zone_door_path[MAXPATHLEN];
134 static int	zone_door = -1;
135 
136 boolean_t in_death_throes = B_FALSE;	/* daemon is dying */
137 boolean_t bringup_failure_recovery = B_FALSE; /* ignore certain failures */
138 
139 #if !defined(TEXT_DOMAIN)		/* should be defined by cc -D */
140 #define	TEXT_DOMAIN	"SYS_TEST"	/* Use this only if it wasn't */
141 #endif
142 
143 #define	DEFAULT_LOCALE	"C"
144 
145 static const char *
146 z_cmd_name(zone_cmd_t zcmd)
147 {
148 	/* This list needs to match the enum in sys/zone.h */
149 	static const char *zcmdstr[] = {
150 		"ready", "boot", "forceboot", "reboot", "halt",
151 		"note_uninstalling", "mount", "forcemount", "unmount",
152 		"shutdown"
153 	};
154 
155 	if (zcmd >= sizeof (zcmdstr) / sizeof (*zcmdstr))
156 		return ("unknown");
157 	else
158 		return (zcmdstr[(int)zcmd]);
159 }
160 
161 static char *
162 get_execbasename(char *execfullname)
163 {
164 	char *last_slash, *execbasename;
165 
166 	/* guard against '/' at end of command invocation */
167 	for (;;) {
168 		last_slash = strrchr(execfullname, '/');
169 		if (last_slash == NULL) {
170 			execbasename = execfullname;
171 			break;
172 		} else {
173 			execbasename = last_slash + 1;
174 			if (*execbasename == '\0') {
175 				*last_slash = '\0';
176 				continue;
177 			}
178 			break;
179 		}
180 	}
181 	return (execbasename);
182 }
183 
184 static void
185 usage(void)
186 {
187 	(void) fprintf(stderr, gettext("Usage: %s -z zonename\n"), progname);
188 	(void) fprintf(stderr,
189 	    gettext("\tNote: %s should not be run directly.\n"), progname);
190 	exit(2);
191 }
192 
193 /* ARGSUSED */
194 static void
195 sigchld(int sig)
196 {
197 }
198 
199 char *
200 localize_msg(char *locale, const char *msg)
201 {
202 	char *out;
203 
204 	(void) mutex_lock(&msglock);
205 	(void) setlocale(LC_MESSAGES, locale);
206 	out = gettext(msg);
207 	(void) setlocale(LC_MESSAGES, DEFAULT_LOCALE);
208 	(void) mutex_unlock(&msglock);
209 	return (out);
210 }
211 
212 /* PRINTFLIKE3 */
213 void
214 zerror(zlog_t *zlogp, boolean_t use_strerror, const char *fmt, ...)
215 {
216 	va_list alist;
217 	char buf[MAXPATHLEN * 2]; /* enough space for err msg with a path */
218 	char *bp;
219 	int saved_errno = errno;
220 
221 	if (zlogp == NULL)
222 		return;
223 	if (zlogp == &logsys)
224 		(void) snprintf(buf, sizeof (buf), "[zone '%s'] ",
225 		    zone_name);
226 	else
227 		buf[0] = '\0';
228 	bp = &(buf[strlen(buf)]);
229 
230 	/*
231 	 * In theory, the locale pointer should be set to either "C" or a
232 	 * char array, so it should never be NULL
233 	 */
234 	assert(zlogp->locale != NULL);
235 	/* Locale is per process, but we are multi-threaded... */
236 	fmt = localize_msg(zlogp->locale, fmt);
237 
238 	va_start(alist, fmt);
239 	(void) vsnprintf(bp, sizeof (buf) - (bp - buf), fmt, alist);
240 	va_end(alist);
241 	bp = &(buf[strlen(buf)]);
242 	if (use_strerror)
243 		(void) snprintf(bp, sizeof (buf) - (bp - buf), ": %s",
244 		    strerror(saved_errno));
245 	if (zlogp == &logsys) {
246 		(void) syslog(LOG_ERR, "%s", buf);
247 	} else if (zlogp->logfile != NULL) {
248 		(void) fprintf(zlogp->logfile, "%s\n", buf);
249 	} else {
250 		size_t buflen;
251 		size_t copylen;
252 
253 		buflen = snprintf(zlogp->log, zlogp->loglen, "%s\n", buf);
254 		copylen = MIN(buflen, zlogp->loglen);
255 		zlogp->log += copylen;
256 		zlogp->loglen -= copylen;
257 	}
258 }
259 
260 /*
261  * Emit a warning for any boot arguments which are unrecognized.  Since
262  * Solaris boot arguments are getopt(3c) compatible (see kernel(8)), we
263  * put the arguments into an argv style array, use getopt to process them,
264  * and put the resultant argument string back into outargs.
265  *
266  * During the filtering, we pull out any arguments which are truly "boot"
267  * arguments, leaving only those which are to be passed intact to the
268  * progenitor process.  The one we support at the moment is -i, which
269  * indicates to the kernel which program should be launched as 'init'.
270  *
271  * A return of Z_INVAL indicates specifically that the arguments are
272  * not valid; this is a non-fatal error.  Except for Z_OK, all other return
273  * values are treated as fatal.
274  */
275 static int
276 filter_bootargs(zlog_t *zlogp, const char *inargs, char *outargs,
277     char *init_file, char *badarg)
278 {
279 	int argc = 0, argc_save;
280 	int i;
281 	int err;
282 	char *arg, *lasts, **argv = NULL, **argv_save;
283 	char zonecfg_args[BOOTARGS_MAX];
284 	char scratchargs[BOOTARGS_MAX], *sargs;
285 	char c;
286 
287 	bzero(outargs, BOOTARGS_MAX);
288 	bzero(badarg, BOOTARGS_MAX);
289 
290 	/*
291 	 * If the user didn't specify transient boot arguments, check
292 	 * to see if there were any specified in the zone configuration,
293 	 * and use them if applicable.
294 	 */
295 	if (inargs == NULL || inargs[0] == '\0')  {
296 		zone_dochandle_t handle;
297 		if ((handle = zonecfg_init_handle()) == NULL) {
298 			zerror(zlogp, B_TRUE,
299 			    "getting zone configuration handle");
300 			return (Z_BAD_HANDLE);
301 		}
302 		err = zonecfg_get_snapshot_handle(zone_name, handle);
303 		if (err != Z_OK) {
304 			zerror(zlogp, B_FALSE,
305 			    "invalid configuration snapshot");
306 			zonecfg_fini_handle(handle);
307 			return (Z_BAD_HANDLE);
308 		}
309 
310 		bzero(zonecfg_args, sizeof (zonecfg_args));
311 		(void) zonecfg_get_bootargs(handle, zonecfg_args,
312 		    sizeof (zonecfg_args));
313 		inargs = zonecfg_args;
314 		zonecfg_fini_handle(handle);
315 	}
316 
317 	if (strlen(inargs) >= BOOTARGS_MAX) {
318 		zerror(zlogp, B_FALSE, "boot argument string too long");
319 		return (Z_INVAL);
320 	}
321 
322 	(void) strlcpy(scratchargs, inargs, sizeof (scratchargs));
323 	sargs = scratchargs;
324 	while ((arg = strtok_r(sargs, " \t", &lasts)) != NULL) {
325 		sargs = NULL;
326 		argc++;
327 	}
328 
329 	if ((argv = calloc(argc + 1, sizeof (char *))) == NULL) {
330 		zerror(zlogp, B_FALSE, "memory allocation failed");
331 		return (Z_NOMEM);
332 	}
333 
334 	argv_save = argv;
335 	argc_save = argc;
336 
337 	(void) strlcpy(scratchargs, inargs, sizeof (scratchargs));
338 	sargs = scratchargs;
339 	i = 0;
340 	while ((arg = strtok_r(sargs, " \t", &lasts)) != NULL) {
341 		sargs = NULL;
342 		if ((argv[i] = strdup(arg)) == NULL) {
343 			err = Z_NOMEM;
344 			zerror(zlogp, B_FALSE, "memory allocation failed");
345 			goto done;
346 		}
347 		i++;
348 	}
349 
350 	/*
351 	 * We preserve compatibility with the Solaris system boot behavior,
352 	 * which allows:
353 	 *
354 	 *	# reboot kernel/unix -s -m verbose
355 	 *
356 	 * In this example, kernel/unix tells the booter what file to
357 	 * boot.  We don't want reboot in a zone to be gratuitously different,
358 	 * so we silently ignore the boot file, if necessary.
359 	 */
360 	if (argv[0] == NULL)
361 		goto done;
362 
363 	assert(argv[0][0] != ' ');
364 	assert(argv[0][0] != '\t');
365 
366 	if (argv[0][0] != '-' && argv[0][0] != '\0') {
367 		argv = &argv[1];
368 		argc--;
369 	}
370 
371 	optind = 0;
372 	opterr = 0;
373 	err = Z_OK;
374 	while ((c = getopt(argc, argv, "fi:m:s")) != -1) {
375 		switch (c) {
376 		case 'i':
377 			/*
378 			 * -i is handled by the runtime and is not passed
379 			 * along to userland
380 			 */
381 			(void) strlcpy(init_file, optarg, MAXPATHLEN);
382 			break;
383 		case 'f':
384 			/* This has already been processed by zoneadm */
385 			break;
386 		case 'm':
387 		case 's':
388 			/* These pass through unmolested */
389 			(void) snprintf(outargs, BOOTARGS_MAX,
390 			    "%s -%c %s ", outargs, c, optarg ? optarg : "");
391 			break;
392 		case '?':
393 			/*
394 			 * We warn about unknown arguments but pass them
395 			 * along anyway-- if someone wants to develop their
396 			 * own init replacement, they can pass it whatever
397 			 * args they want.
398 			 */
399 			err = Z_INVAL;
400 			(void) snprintf(outargs, BOOTARGS_MAX,
401 			    "%s -%c", outargs, optopt);
402 			(void) snprintf(badarg, BOOTARGS_MAX,
403 			    "%s -%c", badarg, optopt);
404 			break;
405 		}
406 	}
407 
408 	/*
409 	 * For Solaris Zones we warn about and discard non-option arguments.
410 	 * Hence 'boot foo bar baz gub' --> 'boot'.  However, to be similar
411 	 * to the kernel, we concat up all the other remaining boot args.
412 	 * and warn on them as a group.
413 	 */
414 	if (optind < argc) {
415 		err = Z_INVAL;
416 		while (optind < argc) {
417 			(void) snprintf(badarg, BOOTARGS_MAX, "%s%s%s",
418 			    badarg, strlen(badarg) > 0 ? " " : "",
419 			    argv[optind]);
420 			optind++;
421 		}
422 		zerror(zlogp, B_FALSE, "WARNING: Unused or invalid boot "
423 		    "arguments `%s'.", badarg);
424 	}
425 
426 done:
427 	for (i = 0; i < argc_save; i++) {
428 		if (argv_save[i] != NULL)
429 			free(argv_save[i]);
430 	}
431 	free(argv_save);
432 	return (err);
433 }
434 
435 
436 static int
437 mkzonedir(zlog_t *zlogp)
438 {
439 	struct stat st;
440 	/*
441 	 * We must create and lock everyone but root out of ZONES_TMPDIR
442 	 * since anyone can open any UNIX domain socket, regardless of
443 	 * its file system permissions.  Sigh...
444 	 */
445 	if (mkdir(ZONES_TMPDIR, S_IRWXU) < 0 && errno != EEXIST) {
446 		zerror(zlogp, B_TRUE, "could not mkdir '%s'", ZONES_TMPDIR);
447 		return (-1);
448 	}
449 	/* paranoia */
450 	if ((stat(ZONES_TMPDIR, &st) < 0) || !S_ISDIR(st.st_mode)) {
451 		zerror(zlogp, B_TRUE, "'%s' is not a directory", ZONES_TMPDIR);
452 		return (-1);
453 	}
454 	(void) chmod(ZONES_TMPDIR, S_IRWXU);
455 	return (0);
456 }
457 
458 /*
459  * Run the brand's pre-state change callback, if it exists.
460  */
461 static int
462 brand_prestatechg(zlog_t *zlogp, int state, int cmd)
463 {
464 	char cmdbuf[2 * MAXPATHLEN];
465 	const char *altroot;
466 
467 	if (pre_statechg_hook[0] == '\0')
468 		return (0);
469 
470 	altroot = zonecfg_get_root();
471 	if (snprintf(cmdbuf, sizeof (cmdbuf), "%s %d %d %s", pre_statechg_hook,
472 	    state, cmd, altroot) > sizeof (cmdbuf))
473 		return (-1);
474 
475 	if (do_subproc(zlogp, cmdbuf, NULL) != 0)
476 		return (-1);
477 
478 	return (0);
479 }
480 
481 /*
482  * Run the brand's post-state change callback, if it exists.
483  */
484 static int
485 brand_poststatechg(zlog_t *zlogp, int state, int cmd)
486 {
487 	char cmdbuf[2 * MAXPATHLEN];
488 	const char *altroot;
489 
490 	if (post_statechg_hook[0] == '\0')
491 		return (0);
492 
493 	altroot = zonecfg_get_root();
494 	if (snprintf(cmdbuf, sizeof (cmdbuf), "%s %d %d %s", post_statechg_hook,
495 	    state, cmd, altroot) > sizeof (cmdbuf))
496 		return (-1);
497 
498 	if (do_subproc(zlogp, cmdbuf, NULL) != 0)
499 		return (-1);
500 
501 	return (0);
502 }
503 
504 /*
505  * Notify zonestatd of the new zone.  If zonestatd is not running, this
506  * will do nothing.
507  */
508 static void
509 notify_zonestatd(zoneid_t zoneid)
510 {
511 	int cmd[2];
512 	int fd;
513 	door_arg_t params;
514 
515 	fd = open(ZS_DOOR_PATH, O_RDONLY);
516 	if (fd < 0)
517 		return;
518 
519 	cmd[0] = ZSD_CMD_NEW_ZONE;
520 	cmd[1] = zoneid;
521 	params.data_ptr = (char *)&cmd;
522 	params.data_size = sizeof (cmd);
523 	params.desc_ptr = NULL;
524 	params.desc_num = 0;
525 	params.rbuf = NULL;
526 	params.rsize = 0;
527 	(void) door_call(fd, &params);
528 	(void) close(fd);
529 }
530 
531 /*
532  * Bring a zone up to the pre-boot "ready" stage.  The mount_cmd argument is
533  * 'true' if this is being invoked as part of the processing for the "mount"
534  * subcommand.
535  */
536 static int
537 zone_ready(zlog_t *zlogp, zone_mnt_t mount_cmd, int zstate)
538 {
539 	int err;
540 
541 	if (brand_prestatechg(zlogp, zstate, Z_READY) != 0)
542 		return (-1);
543 
544 	if ((err = zonecfg_create_snapshot(zone_name)) != Z_OK) {
545 		zerror(zlogp, B_FALSE, "unable to create snapshot: %s",
546 		    zonecfg_strerror(err));
547 		goto bad;
548 	}
549 
550 	if ((zone_id = vplat_create(zlogp, mount_cmd)) == -1) {
551 		if ((err = zonecfg_destroy_snapshot(zone_name)) != Z_OK)
552 			zerror(zlogp, B_FALSE, "destroying snapshot: %s",
553 			    zonecfg_strerror(err));
554 		goto bad;
555 	}
556 	if (vplat_bringup(zlogp, mount_cmd, zone_id) != 0) {
557 		bringup_failure_recovery = B_TRUE;
558 		(void) vplat_teardown(NULL, (mount_cmd != Z_MNT_BOOT), B_FALSE);
559 		if ((err = zonecfg_destroy_snapshot(zone_name)) != Z_OK)
560 			zerror(zlogp, B_FALSE, "destroying snapshot: %s",
561 			    zonecfg_strerror(err));
562 		goto bad;
563 	}
564 
565 	if (brand_poststatechg(zlogp, zstate, Z_READY) != 0)
566 		goto bad;
567 
568 	return (0);
569 
570 bad:
571 	/*
572 	 * If something goes wrong, we up the zones's state to the target
573 	 * state, READY, and then invoke the hook as if we're halting.
574 	 */
575 	(void) brand_poststatechg(zlogp, ZONE_STATE_READY, Z_HALT);
576 	return (-1);
577 }
578 
579 int
580 init_template(void)
581 {
582 	int fd;
583 	int err = 0;
584 
585 	fd = open64(CTFS_ROOT "/process/template", O_RDWR);
586 	if (fd == -1)
587 		return (-1);
588 
589 	/*
590 	 * For now, zoneadmd doesn't do anything with the contract.
591 	 * Deliver no events, don't inherit, and allow it to be orphaned.
592 	 */
593 	err |= ct_tmpl_set_critical(fd, 0);
594 	err |= ct_tmpl_set_informative(fd, 0);
595 	err |= ct_pr_tmpl_set_fatal(fd, CT_PR_EV_HWERR);
596 	err |= ct_pr_tmpl_set_param(fd, CT_PR_PGRPONLY | CT_PR_REGENT);
597 	if (err || ct_tmpl_activate(fd)) {
598 		(void) close(fd);
599 		return (-1);
600 	}
601 
602 	return (fd);
603 }
604 
605 typedef struct fs_callback {
606 	zlog_t		*zlogp;
607 	zoneid_t	zoneid;
608 	boolean_t	mount_cmd;
609 } fs_callback_t;
610 
611 static int
612 mount_early_fs(void *data, const char *spec, const char *dir,
613     const char *fstype, const char *opt)
614 {
615 	zlog_t *zlogp = ((fs_callback_t *)data)->zlogp;
616 	zoneid_t zoneid = ((fs_callback_t *)data)->zoneid;
617 	boolean_t mount_cmd = ((fs_callback_t *)data)->mount_cmd;
618 	char rootpath[MAXPATHLEN];
619 	pid_t child;
620 	int child_status;
621 	int tmpl_fd;
622 	int rv;
623 	ctid_t ct;
624 
625 	/* determine the zone rootpath */
626 	if (mount_cmd) {
627 		char zonepath[MAXPATHLEN];
628 		char luroot[MAXPATHLEN];
629 
630 		if (zone_get_zonepath(zone_name,
631 		    zonepath, sizeof (zonepath)) != Z_OK) {
632 			zerror(zlogp, B_FALSE, "unable to determine zone path");
633 			return (-1);
634 		}
635 
636 		(void) snprintf(luroot, sizeof (luroot), "%s/lu", zonepath);
637 		resolve_lofs(zlogp, luroot, sizeof (luroot));
638 		(void) strlcpy(rootpath, luroot, sizeof (rootpath));
639 	} else {
640 		if (zone_get_rootpath(zone_name,
641 		    rootpath, sizeof (rootpath)) != Z_OK) {
642 			zerror(zlogp, B_FALSE, "unable to determine zone root");
643 			return (-1);
644 		}
645 	}
646 
647 	if ((rv = valid_mount_path(zlogp, rootpath, spec, dir, fstype)) < 0) {
648 		zerror(zlogp, B_FALSE, "%s%s is not a valid mount point",
649 		    rootpath, dir);
650 		return (-1);
651 	} else if (rv > 0) {
652 		/* The mount point path doesn't exist, create it now. */
653 		if (make_one_dir(zlogp, rootpath, dir,
654 		    DEFAULT_DIR_MODE, DEFAULT_DIR_USER,
655 		    DEFAULT_DIR_GROUP) != 0) {
656 			zerror(zlogp, B_FALSE, "failed to create mount point");
657 			return (-1);
658 		}
659 
660 		/*
661 		 * Now this might seem weird, but we need to invoke
662 		 * valid_mount_path() again.  Why?  Because it checks
663 		 * to make sure that the mount point path is canonical,
664 		 * which it can only do if the path exists, so now that
665 		 * we've created the path we have to verify it again.
666 		 */
667 		if ((rv = valid_mount_path(zlogp, rootpath, spec, dir,
668 		    fstype)) < 0) {
669 			zerror(zlogp, B_FALSE,
670 			    "%s%s is not a valid mount point", rootpath, dir);
671 			return (-1);
672 		}
673 	}
674 
675 	if ((tmpl_fd = init_template()) == -1) {
676 		zerror(zlogp, B_TRUE, "failed to create contract");
677 		return (-1);
678 	}
679 
680 	if ((child = fork()) == -1) {
681 		(void) ct_tmpl_clear(tmpl_fd);
682 		(void) close(tmpl_fd);
683 		zerror(zlogp, B_TRUE, "failed to fork");
684 		return (-1);
685 
686 	} else if (child == 0) {	/* child */
687 		char opt_buf[MAX_MNTOPT_STR];
688 		int optlen = 0;
689 		int mflag = MS_DATA;
690 
691 		(void) ct_tmpl_clear(tmpl_fd);
692 		/*
693 		 * Even though there are no procs running in the zone, we
694 		 * do this for paranoia's sake.
695 		 */
696 		(void) closefrom(0);
697 
698 		if (zone_enter(zoneid) == -1) {
699 			_exit(errno);
700 		}
701 		if (opt != NULL) {
702 			/*
703 			 * The mount() system call is incredibly annoying.
704 			 * If options are specified, we need to copy them
705 			 * into a temporary buffer since the mount() system
706 			 * call will overwrite the options string.  It will
707 			 * also fail if the new option string it wants to
708 			 * write is bigger than the one we passed in, so
709 			 * you must pass in a buffer of the maximum possible
710 			 * option string length.  sigh.
711 			 */
712 			(void) strlcpy(opt_buf, opt, sizeof (opt_buf));
713 			opt = opt_buf;
714 			optlen = MAX_MNTOPT_STR;
715 			mflag = MS_OPTIONSTR;
716 		}
717 		if (mount(spec, dir, mflag, fstype, NULL, 0, opt, optlen) != 0)
718 			_exit(errno);
719 		_exit(0);
720 	}
721 
722 	/* parent */
723 	if (contract_latest(&ct) == -1)
724 		ct = -1;
725 	(void) ct_tmpl_clear(tmpl_fd);
726 	(void) close(tmpl_fd);
727 	if (waitpid(child, &child_status, 0) != child) {
728 		/* unexpected: we must have been signalled */
729 		(void) contract_abandon_id(ct);
730 		return (-1);
731 	}
732 	(void) contract_abandon_id(ct);
733 	if (WEXITSTATUS(child_status) != 0) {
734 		errno = WEXITSTATUS(child_status);
735 		zerror(zlogp, B_TRUE, "mount of %s failed", dir);
736 		return (-1);
737 	}
738 
739 	return (0);
740 }
741 
742 /*
743  * If retstr is not NULL, the output of the subproc is returned in the str,
744  * otherwise it is output using zerror().  Any memory allocated for retstr
745  * should be freed by the caller.
746  */
747 int
748 do_subproc(zlog_t *zlogp, char *cmdbuf, char **retstr)
749 {
750 	char buf[1024];		/* arbitrary large amount */
751 	char *inbuf;
752 	FILE *file;
753 	int status;
754 	int rd_cnt;
755 
756 	if (retstr != NULL) {
757 		if ((*retstr = malloc(1024)) == NULL) {
758 			zerror(zlogp, B_FALSE, "out of memory");
759 			return (-1);
760 		}
761 		inbuf = *retstr;
762 		rd_cnt = 0;
763 	} else {
764 		inbuf = buf;
765 	}
766 
767 	file = popen(cmdbuf, "r");
768 	if (file == NULL) {
769 		zerror(zlogp, B_TRUE, "could not launch: %s", cmdbuf);
770 		return (-1);
771 	}
772 
773 	while (fgets(inbuf, 1024, file) != NULL) {
774 		if (retstr == NULL) {
775 			if (zlogp != &logsys) {
776 				int last = strlen(inbuf) - 1;
777 
778 				if (inbuf[last] == '\n')
779 					inbuf[last] = '\0';
780 				zerror(zlogp, B_FALSE, "%s", inbuf);
781 			}
782 		} else {
783 			char *p;
784 
785 			rd_cnt += 1024 - 1;
786 			if ((p = realloc(*retstr, rd_cnt + 1024)) == NULL) {
787 				zerror(zlogp, B_FALSE, "out of memory");
788 				(void) pclose(file);
789 				return (-1);
790 			}
791 
792 			*retstr = p;
793 			inbuf = *retstr + rd_cnt;
794 		}
795 	}
796 	status = pclose(file);
797 
798 	if (WIFSIGNALED(status)) {
799 		zerror(zlogp, B_FALSE, "%s unexpectedly terminated due to "
800 		    "signal %d", cmdbuf, WTERMSIG(status));
801 		return (-1);
802 	}
803 	assert(WIFEXITED(status));
804 	if (WEXITSTATUS(status) == ZEXIT_EXEC) {
805 		zerror(zlogp, B_FALSE, "failed to exec %s", cmdbuf);
806 		return (-1);
807 	}
808 	return (WEXITSTATUS(status));
809 }
810 
811 static int
812 zone_bootup(zlog_t *zlogp, const char *bootargs, int zstate)
813 {
814 	zoneid_t zoneid;
815 	struct stat st;
816 	char zpath[MAXPATHLEN], initpath[MAXPATHLEN], init_file[MAXPATHLEN];
817 	char nbootargs[BOOTARGS_MAX];
818 	char cmdbuf[MAXPATHLEN];
819 	fs_callback_t cb;
820 	brand_handle_t bh;
821 	zone_iptype_t iptype;
822 	boolean_t links_loaded = B_FALSE;
823 	dladm_status_t status;
824 	char errmsg[DLADM_STRSIZE];
825 	int err;
826 	boolean_t restart_init, restart_init0, restart_initreboot;
827 
828 	if (brand_prestatechg(zlogp, zstate, Z_BOOT) != 0)
829 		return (-1);
830 
831 	if ((zoneid = getzoneidbyname(zone_name)) == -1) {
832 		zerror(zlogp, B_TRUE, "unable to get zoneid");
833 		goto bad;
834 	}
835 
836 	cb.zlogp = zlogp;
837 	cb.zoneid = zoneid;
838 	cb.mount_cmd = B_FALSE;
839 
840 	/* Get a handle to the brand info for this zone */
841 	if ((bh = brand_open(brand_name)) == NULL) {
842 		zerror(zlogp, B_FALSE, "unable to determine zone brand");
843 		goto bad;
844 	}
845 
846 	/*
847 	 * Get the list of filesystems to mount from the brand
848 	 * configuration.  These mounts are done via a thread that will
849 	 * enter the zone, so they are done from within the context of the
850 	 * zone.
851 	 */
852 	if (brand_platform_iter_mounts(bh, mount_early_fs, &cb) != 0) {
853 		zerror(zlogp, B_FALSE, "unable to mount filesystems");
854 		brand_close(bh);
855 		goto bad;
856 	}
857 
858 	/*
859 	 * Get the brand's boot callback if it exists.
860 	 */
861 	if (zone_get_zonepath(zone_name, zpath, sizeof (zpath)) != Z_OK) {
862 		zerror(zlogp, B_FALSE, "unable to determine zone path");
863 		brand_close(bh);
864 		goto bad;
865 	}
866 	(void) strcpy(cmdbuf, EXEC_PREFIX);
867 	if (brand_get_boot(bh, zone_name, zpath, cmdbuf + EXEC_LEN,
868 	    sizeof (cmdbuf) - EXEC_LEN) != 0) {
869 		zerror(zlogp, B_FALSE,
870 		    "unable to determine branded zone's boot callback");
871 		brand_close(bh);
872 		goto bad;
873 	}
874 
875 	/* Get the path for this zone's init(8) (or equivalent) process.  */
876 	if (brand_get_initname(bh, init_file, MAXPATHLEN) != 0) {
877 		zerror(zlogp, B_FALSE,
878 		    "unable to determine zone's init(8) location");
879 		brand_close(bh);
880 		goto bad;
881 	}
882 
883 	/* See if this zone's brand should restart init if it dies. */
884 	restart_init = brand_restartinit(bh);
885 	restart_init0 = brand_restartinit0(bh);
886 	restart_initreboot = brand_restartinitreboot(bh);
887 
888 	brand_close(bh);
889 
890 	err = filter_bootargs(zlogp, bootargs, nbootargs, init_file,
891 	    bad_boot_arg);
892 	if (err == Z_INVAL)
893 		eventstream_write(Z_EVT_ZONE_BADARGS);
894 	else if (err != Z_OK)
895 		goto bad;
896 
897 	assert(init_file[0] != '\0');
898 
899 	/* Try to anticipate possible problems: Make sure init is executable. */
900 	if (zone_get_rootpath(zone_name, zpath, sizeof (zpath)) != Z_OK) {
901 		zerror(zlogp, B_FALSE, "unable to determine zone root");
902 		goto bad;
903 	}
904 
905 	(void) snprintf(initpath, sizeof (initpath), "%s%s", zpath, init_file);
906 
907 	if (stat(initpath, &st) == -1) {
908 		zerror(zlogp, B_TRUE, "could not stat %s", initpath);
909 		goto bad;
910 	}
911 
912 	if ((st.st_mode & S_IXUSR) == 0) {
913 		zerror(zlogp, B_FALSE, "%s is not executable", initpath);
914 		goto bad;
915 	}
916 
917 	/*
918 	 * Exclusive stack zones interact with the dlmgmtd running in the
919 	 * global zone.  dladm_zone_boot() tells dlmgmtd that this zone is
920 	 * booting, and loads its datalinks from the zone's datalink
921 	 * configuration file.
922 	 */
923 	if (vplat_get_iptype(zlogp, &iptype) == 0 && iptype == ZS_EXCLUSIVE) {
924 		status = dladm_zone_boot(dld_handle, zoneid);
925 		if (status != DLADM_STATUS_OK) {
926 			zerror(zlogp, B_FALSE, "unable to load zone datalinks: "
927 			    " %s", dladm_status2str(status, errmsg));
928 			goto bad;
929 		}
930 		links_loaded = B_TRUE;
931 	}
932 
933 	/*
934 	 * If there is a brand 'boot' callback, execute it now to give the
935 	 * brand one last chance to do any additional setup before the zone
936 	 * is booted.
937 	 */
938 	if ((strlen(cmdbuf) > EXEC_LEN) &&
939 	    (do_subproc(zlogp, cmdbuf, NULL) != Z_OK)) {
940 		zerror(zlogp, B_FALSE, "%s failed", cmdbuf);
941 		goto bad;
942 	}
943 
944 	if (zone_setattr(zoneid, ZONE_ATTR_INITNAME, init_file, 0) == -1) {
945 		zerror(zlogp, B_TRUE, "could not set zone boot file");
946 		goto bad;
947 	}
948 
949 	if (zone_setattr(zoneid, ZONE_ATTR_BOOTARGS, nbootargs, 0) == -1) {
950 		zerror(zlogp, B_TRUE, "could not set zone boot arguments");
951 		goto bad;
952 	}
953 
954 	if (!restart_init && zone_setattr(zoneid, ZONE_ATTR_INITNORESTART,
955 	    NULL, 0) == -1) {
956 		zerror(zlogp, B_TRUE, "could not set zone init-no-restart");
957 		goto bad;
958 	}
959 	if (restart_init0 && zone_setattr(zoneid, ZONE_ATTR_INITRESTART0,
960 	    NULL, 0) == -1) {
961 		zerror(zlogp, B_TRUE,
962 		    "could not set zone init-restart-on-exit-0");
963 		goto bad;
964 	}
965 	if (restart_initreboot && zone_setattr(zoneid, ZONE_ATTR_INITREBOOT,
966 	    NULL, 0) == -1) {
967 		zerror(zlogp, B_TRUE, "could not set zone reboot-on-init-exit");
968 		goto bad;
969 	}
970 
971 	/*
972 	 * Inform zonestatd of a new zone so that it can install a door for
973 	 * the zone to contact it.
974 	 */
975 	notify_zonestatd(zone_id);
976 
977 	if (zone_boot(zoneid) == -1) {
978 		zerror(zlogp, B_TRUE, "unable to boot zone");
979 		goto bad;
980 	}
981 
982 	if (brand_poststatechg(zlogp, zstate, Z_BOOT) != 0)
983 		goto bad;
984 
985 	return (0);
986 
987 bad:
988 	/*
989 	 * If something goes wrong, we up the zones's state to the target
990 	 * state, RUNNING, and then invoke the hook as if we're halting.
991 	 */
992 	(void) brand_poststatechg(zlogp, ZONE_STATE_RUNNING, Z_HALT);
993 	if (links_loaded)
994 		(void) dladm_zone_halt(dld_handle, zoneid);
995 	return (-1);
996 }
997 
998 static int
999 zone_halt(zlog_t *zlogp, boolean_t unmount_cmd, boolean_t rebooting, int zstate)
1000 {
1001 	int err;
1002 
1003 	if (brand_prestatechg(zlogp, zstate, Z_HALT) != 0)
1004 		return (-1);
1005 
1006 	if (vplat_teardown(zlogp, unmount_cmd, rebooting) != 0) {
1007 		if (!bringup_failure_recovery)
1008 			zerror(zlogp, B_FALSE, "unable to destroy zone");
1009 		return (-1);
1010 	}
1011 
1012 	if ((err = zonecfg_destroy_snapshot(zone_name)) != Z_OK)
1013 		zerror(zlogp, B_FALSE, "destroying snapshot: %s",
1014 		    zonecfg_strerror(err));
1015 
1016 	if (brand_poststatechg(zlogp, zstate, Z_HALT) != 0)
1017 		return (-1);
1018 
1019 	return (0);
1020 }
1021 
1022 static int
1023 zone_graceful_shutdown(zlog_t *zlogp)
1024 {
1025 	zoneid_t zoneid;
1026 	pid_t child;
1027 	char cmdbuf[MAXPATHLEN];
1028 	brand_handle_t bh = NULL;
1029 	char zpath[MAXPATHLEN];
1030 	ctid_t ct;
1031 	int tmpl_fd;
1032 	int child_status;
1033 
1034 	if (shutdown_in_progress) {
1035 		zerror(zlogp, B_FALSE, "shutdown already in progress");
1036 		return (-1);
1037 	}
1038 
1039 	if ((zoneid = getzoneidbyname(zone_name)) == -1) {
1040 		zerror(zlogp, B_TRUE, "unable to get zoneid");
1041 		return (-1);
1042 	}
1043 
1044 	/* Get a handle to the brand info for this zone */
1045 	if ((bh = brand_open(brand_name)) == NULL) {
1046 		zerror(zlogp, B_FALSE, "unable to determine zone brand");
1047 		return (-1);
1048 	}
1049 
1050 	if (zone_get_zonepath(zone_name, zpath, sizeof (zpath)) != Z_OK) {
1051 		zerror(zlogp, B_FALSE, "unable to determine zone path");
1052 		brand_close(bh);
1053 		return (-1);
1054 	}
1055 
1056 	/*
1057 	 * If there is a brand 'shutdown' callback, execute it now to give the
1058 	 * brand a chance to cleanup any custom configuration.
1059 	 */
1060 	(void) strcpy(cmdbuf, EXEC_PREFIX);
1061 	if (brand_get_shutdown(bh, zone_name, zpath, cmdbuf + EXEC_LEN,
1062 	    sizeof (cmdbuf) - EXEC_LEN) != 0 || strlen(cmdbuf) <= EXEC_LEN) {
1063 		(void) strcat(cmdbuf, SHUTDOWN_DEFAULT);
1064 	}
1065 	brand_close(bh);
1066 
1067 	if ((tmpl_fd = init_template()) == -1) {
1068 		zerror(zlogp, B_TRUE, "failed to create contract");
1069 		return (-1);
1070 	}
1071 
1072 	if ((child = fork()) == -1) {
1073 		(void) ct_tmpl_clear(tmpl_fd);
1074 		(void) close(tmpl_fd);
1075 		zerror(zlogp, B_TRUE, "failed to fork");
1076 		return (-1);
1077 	} else if (child == 0) {
1078 		(void) ct_tmpl_clear(tmpl_fd);
1079 		if (zone_enter(zoneid) == -1) {
1080 			_exit(errno);
1081 		}
1082 		_exit(execl("/bin/sh", "sh", "-c", cmdbuf, (char *)NULL));
1083 	}
1084 
1085 	if (contract_latest(&ct) == -1)
1086 		ct = -1;
1087 	(void) ct_tmpl_clear(tmpl_fd);
1088 	(void) close(tmpl_fd);
1089 
1090 	if (waitpid(child, &child_status, 0) != child) {
1091 		/* unexpected: we must have been signalled */
1092 		(void) contract_abandon_id(ct);
1093 		return (-1);
1094 	}
1095 
1096 	(void) contract_abandon_id(ct);
1097 	if (WEXITSTATUS(child_status) != 0) {
1098 		errno = WEXITSTATUS(child_status);
1099 		zerror(zlogp, B_FALSE, "unable to shutdown zone");
1100 		return (-1);
1101 	}
1102 
1103 	shutdown_in_progress = B_TRUE;
1104 
1105 	return (0);
1106 }
1107 
1108 static int
1109 zone_wait_shutdown(zlog_t *zlogp)
1110 {
1111 	zone_state_t zstate;
1112 	uint64_t *tm = NULL;
1113 	scf_simple_prop_t *prop = NULL;
1114 	int timeout;
1115 	int tries;
1116 	int rc = -1;
1117 
1118 	/* Get default stop timeout from SMF framework */
1119 	timeout = SHUTDOWN_WAIT;
1120 	if ((prop = scf_simple_prop_get(NULL, SHUTDOWN_FMRI, "stop",
1121 	    SCF_PROPERTY_TIMEOUT)) != NULL) {
1122 		if ((tm = scf_simple_prop_next_count(prop)) != NULL) {
1123 			if (tm != 0)
1124 				timeout = *tm;
1125 		}
1126 		scf_simple_prop_free(prop);
1127 	}
1128 
1129 	/* allow time for zone to shutdown cleanly */
1130 	for (tries = 0; tries < timeout; tries ++) {
1131 		(void) sleep(1);
1132 		if (zone_get_state(zone_name, &zstate) == Z_OK &&
1133 		    zstate == ZONE_STATE_INSTALLED) {
1134 			rc = 0;
1135 			break;
1136 		}
1137 	}
1138 
1139 	if (rc != 0)
1140 		zerror(zlogp, B_FALSE, "unable to shutdown zone");
1141 
1142 	shutdown_in_progress = B_FALSE;
1143 
1144 	return (rc);
1145 }
1146 
1147 
1148 
1149 /*
1150  * Generate AUE_zone_state for a command that boots a zone.
1151  */
1152 static void
1153 audit_put_record(zlog_t *zlogp, ucred_t *uc, int return_val,
1154     char *new_state)
1155 {
1156 	adt_session_data_t	*ah;
1157 	adt_event_data_t	*event;
1158 	int			pass_fail, fail_reason;
1159 
1160 	if (!adt_audit_enabled())
1161 		return;
1162 
1163 	if (return_val == 0) {
1164 		pass_fail = ADT_SUCCESS;
1165 		fail_reason = ADT_SUCCESS;
1166 	} else {
1167 		pass_fail = ADT_FAILURE;
1168 		fail_reason = ADT_FAIL_VALUE_PROGRAM;
1169 	}
1170 
1171 	if (adt_start_session(&ah, NULL, 0)) {
1172 		zerror(zlogp, B_TRUE, gettext("audit failure."));
1173 		return;
1174 	}
1175 	if (adt_set_from_ucred(ah, uc, ADT_NEW)) {
1176 		zerror(zlogp, B_TRUE, gettext("audit failure."));
1177 		(void) adt_end_session(ah);
1178 		return;
1179 	}
1180 
1181 	event = adt_alloc_event(ah, ADT_zone_state);
1182 	if (event == NULL) {
1183 		zerror(zlogp, B_TRUE, gettext("audit failure."));
1184 		(void) adt_end_session(ah);
1185 		return;
1186 	}
1187 	event->adt_zone_state.zonename = zone_name;
1188 	event->adt_zone_state.new_state = new_state;
1189 
1190 	if (adt_put_event(event, pass_fail, fail_reason))
1191 		zerror(zlogp, B_TRUE, gettext("audit failure."));
1192 
1193 	adt_free_event(event);
1194 
1195 	(void) adt_end_session(ah);
1196 }
1197 
1198 /*
1199  * The main routine for the door server that deals with zone state transitions.
1200  */
1201 /* ARGSUSED */
1202 static void
1203 server(void *cookie, char *args, size_t alen, door_desc_t *dp,
1204     uint_t n_desc)
1205 {
1206 	ucred_t *uc = NULL;
1207 	const priv_set_t *eset;
1208 
1209 	zone_state_t zstate;
1210 	zone_cmd_t cmd;
1211 	zone_cmd_arg_t *zargp;
1212 
1213 	boolean_t kernelcall = B_FALSE;
1214 
1215 	int rval = -1;
1216 	uint64_t uniqid;
1217 	zoneid_t zoneid = -1;
1218 	zlog_t zlog;
1219 	zlog_t *zlogp;
1220 	zone_cmd_rval_t *rvalp;
1221 	size_t rlen = getpagesize(); /* conservative */
1222 	fs_callback_t cb;
1223 	brand_handle_t bh;
1224 	boolean_t wait_shut = B_FALSE;
1225 
1226 	/* LINTED E_BAD_PTR_CAST_ALIGN */
1227 	zargp = (zone_cmd_arg_t *)args;
1228 
1229 	/*
1230 	 * When we get the door unref message, we've fdetach'd the door, and
1231 	 * it is time for us to shut down zoneadmd.
1232 	 */
1233 	if (zargp == DOOR_UNREF_DATA) {
1234 		/*
1235 		 * See comment at end of main() for info on the last rites.
1236 		 */
1237 		exit(0);
1238 	}
1239 
1240 	if (zargp == NULL) {
1241 		(void) door_return(NULL, 0, 0, 0);
1242 	}
1243 
1244 	rvalp = alloca(rlen);
1245 	bzero(rvalp, rlen);
1246 	zlog.logfile = NULL;
1247 	zlog.buflen = zlog.loglen = rlen - sizeof (zone_cmd_rval_t) + 1;
1248 	zlog.buf = rvalp->errbuf;
1249 	zlog.log = zlog.buf;
1250 	/* defer initialization of zlog.locale until after credential check */
1251 	zlogp = &zlog;
1252 
1253 	if (alen != sizeof (zone_cmd_arg_t)) {
1254 		/*
1255 		 * This really shouldn't be happening.
1256 		 */
1257 		zerror(&logsys, B_FALSE, "argument size (%d bytes) "
1258 		    "unexpected (expected %d bytes)", alen,
1259 		    sizeof (zone_cmd_arg_t));
1260 		goto out;
1261 	}
1262 	cmd = zargp->cmd;
1263 
1264 	if (door_ucred(&uc) != 0) {
1265 		zerror(&logsys, B_TRUE, "door_ucred");
1266 		goto out;
1267 	}
1268 	eset = ucred_getprivset(uc, PRIV_EFFECTIVE);
1269 	if (ucred_getzoneid(uc) != GLOBAL_ZONEID ||
1270 	    (eset != NULL ? !priv_ismember(eset, PRIV_SYS_CONFIG) :
1271 	    ucred_geteuid(uc) != 0)) {
1272 		zerror(&logsys, B_FALSE, "insufficient privileges");
1273 		goto out;
1274 	}
1275 
1276 	kernelcall = ucred_getpid(uc) == 0;
1277 
1278 	/*
1279 	 * This is safe because we only use a zlog_t throughout the
1280 	 * duration of a door call; i.e., by the time the pointer
1281 	 * might become invalid, the door call would be over.
1282 	 */
1283 	zlog.locale = kernelcall ? DEFAULT_LOCALE : zargp->locale;
1284 
1285 	(void) mutex_lock(&lock);
1286 
1287 	/*
1288 	 * Once we start to really die off, we don't want more connections.
1289 	 */
1290 	if (in_death_throes) {
1291 		(void) mutex_unlock(&lock);
1292 		ucred_free(uc);
1293 		(void) door_return(NULL, 0, 0, 0);
1294 		thr_exit(NULL);
1295 	}
1296 
1297 	/*
1298 	 * Check for validity of command.
1299 	 */
1300 	if (cmd != Z_READY && cmd != Z_BOOT && cmd != Z_FORCEBOOT &&
1301 	    cmd != Z_REBOOT && cmd != Z_SHUTDOWN && cmd != Z_HALT &&
1302 	    cmd != Z_NOTE_UNINSTALLING && cmd != Z_MOUNT &&
1303 	    cmd != Z_FORCEMOUNT && cmd != Z_UNMOUNT) {
1304 		zerror(&logsys, B_FALSE, "invalid command %d", (int)cmd);
1305 		goto out;
1306 	}
1307 
1308 	if (kernelcall && (cmd != Z_HALT && cmd != Z_REBOOT)) {
1309 		/*
1310 		 * Can't happen
1311 		 */
1312 		zerror(&logsys, B_FALSE, "received unexpected kernel upcall %d",
1313 		    cmd);
1314 		goto out;
1315 	}
1316 	/*
1317 	 * We ignore the possibility of someone calling zone_create(2)
1318 	 * explicitly; all requests must come through zoneadmd.
1319 	 */
1320 	if (zone_get_state(zone_name, &zstate) != Z_OK) {
1321 		/*
1322 		 * Something terribly wrong happened
1323 		 */
1324 		zerror(&logsys, B_FALSE, "unable to determine state of zone");
1325 		goto out;
1326 	}
1327 
1328 	if (kernelcall) {
1329 		/*
1330 		 * Kernel-initiated requests may lose their validity if the
1331 		 * zone_t the kernel was referring to has gone away.
1332 		 */
1333 		if ((zoneid = getzoneidbyname(zone_name)) == -1 ||
1334 		    zone_getattr(zoneid, ZONE_ATTR_UNIQID, &uniqid,
1335 		    sizeof (uniqid)) == -1 || uniqid != zargp->uniqid) {
1336 			/*
1337 			 * We're not talking about the same zone. The request
1338 			 * must have arrived too late.  Return error.
1339 			 */
1340 			rval = -1;
1341 			goto out;
1342 		}
1343 		zlogp = &logsys;	/* Log errors to syslog */
1344 	}
1345 
1346 	/*
1347 	 * If we are being asked to forcibly mount or boot a zone, we
1348 	 * pretend that an INCOMPLETE zone is actually INSTALLED.
1349 	 */
1350 	if (zstate == ZONE_STATE_INCOMPLETE &&
1351 	    (cmd == Z_FORCEBOOT || cmd == Z_FORCEMOUNT))
1352 		zstate = ZONE_STATE_INSTALLED;
1353 
1354 	switch (zstate) {
1355 	case ZONE_STATE_CONFIGURED:
1356 	case ZONE_STATE_INCOMPLETE:
1357 		/*
1358 		 * Not our area of expertise; we just print a nice message
1359 		 * and die off.
1360 		 */
1361 		zerror(zlogp, B_FALSE,
1362 		    "%s operation is invalid for zones in state '%s'",
1363 		    z_cmd_name(cmd), zone_state_str(zstate));
1364 		break;
1365 
1366 	case ZONE_STATE_INSTALLED:
1367 		switch (cmd) {
1368 		case Z_READY:
1369 			rval = zone_ready(zlogp, Z_MNT_BOOT, zstate);
1370 			if (rval == 0)
1371 				eventstream_write(Z_EVT_ZONE_READIED);
1372 			break;
1373 		case Z_BOOT:
1374 		case Z_FORCEBOOT:
1375 			eventstream_write(Z_EVT_ZONE_BOOTING);
1376 			if ((rval = zone_ready(zlogp, Z_MNT_BOOT, zstate))
1377 			    == 0) {
1378 				rval = zone_bootup(zlogp, zargp->bootbuf,
1379 				    zstate);
1380 			}
1381 			audit_put_record(zlogp, uc, rval, "boot");
1382 			if (rval != 0) {
1383 				bringup_failure_recovery = B_TRUE;
1384 				(void) zone_halt(zlogp, B_FALSE, B_FALSE,
1385 				    zstate);
1386 				eventstream_write(Z_EVT_ZONE_BOOTFAILED);
1387 			}
1388 			break;
1389 		case Z_SHUTDOWN:
1390 		case Z_HALT:
1391 			if (kernelcall)	/* Invalid; can't happen */
1392 				abort();
1393 			/*
1394 			 * We could have two clients racing to halt this
1395 			 * zone; the second client loses, but its request
1396 			 * doesn't fail, since the zone is now in the desired
1397 			 * state.
1398 			 */
1399 			zerror(zlogp, B_FALSE, "zone is already halted");
1400 			rval = 0;
1401 			break;
1402 		case Z_REBOOT:
1403 			if (kernelcall)	/* Invalid; can't happen */
1404 				abort();
1405 			zerror(zlogp, B_FALSE, "%s operation is invalid "
1406 			    "for zones in state '%s'", z_cmd_name(cmd),
1407 			    zone_state_str(zstate));
1408 			rval = -1;
1409 			break;
1410 		case Z_NOTE_UNINSTALLING:
1411 			if (kernelcall)	/* Invalid; can't happen */
1412 				abort();
1413 			/*
1414 			 * Tell the console to print out a message about this.
1415 			 * Once it does, we will be in_death_throes.
1416 			 */
1417 			eventstream_write(Z_EVT_ZONE_UNINSTALLING);
1418 			break;
1419 		case Z_MOUNT:
1420 		case Z_FORCEMOUNT:
1421 			if (kernelcall)	/* Invalid; can't happen */
1422 				abort();
1423 			if (!zone_isnative && !zone_iscluster &&
1424 			    !zone_islabeled) {
1425 				/*
1426 				 * -U mounts the zone without lofs mounting
1427 				 * zone file systems back into the scratch
1428 				 * zone.  This is required when mounting
1429 				 * non-native branded zones.
1430 				 */
1431 				(void) strlcpy(zargp->bootbuf, "-U",
1432 				    BOOTARGS_MAX);
1433 			}
1434 
1435 			rval = zone_ready(zlogp,
1436 			    strcmp(zargp->bootbuf, "-U") == 0 ?
1437 			    Z_MNT_UPDATE : Z_MNT_SCRATCH, zstate);
1438 			if (rval != 0)
1439 				break;
1440 
1441 			eventstream_write(Z_EVT_ZONE_READIED);
1442 
1443 			/*
1444 			 * Get a handle to the default brand info.
1445 			 * We must always use the default brand file system
1446 			 * list when mounting the zone.
1447 			 */
1448 			if ((bh = brand_open(default_brand)) == NULL) {
1449 				rval = -1;
1450 				break;
1451 			}
1452 
1453 			/*
1454 			 * Get the list of filesystems to mount from
1455 			 * the brand configuration.  These mounts are done
1456 			 * via a thread that will enter the zone, so they
1457 			 * are done from within the context of the zone.
1458 			 */
1459 			cb.zlogp = zlogp;
1460 			cb.zoneid = zone_id;
1461 			cb.mount_cmd = B_TRUE;
1462 			rval = brand_platform_iter_mounts(bh,
1463 			    mount_early_fs, &cb);
1464 
1465 			brand_close(bh);
1466 
1467 			/*
1468 			 * Ordinarily, /dev/fd would be mounted inside the zone
1469 			 * by svc:/system/filesystem/usr:default, but since
1470 			 * we're not booting the zone, we need to do this
1471 			 * manually.
1472 			 */
1473 			if (rval == 0)
1474 				rval = mount_early_fs(&cb,
1475 				    "fd", "/dev/fd", "fd", NULL);
1476 			break;
1477 		case Z_UNMOUNT:
1478 			if (kernelcall)	/* Invalid; can't happen */
1479 				abort();
1480 			zerror(zlogp, B_FALSE, "zone is already unmounted");
1481 			rval = 0;
1482 			break;
1483 		}
1484 		break;
1485 
1486 	case ZONE_STATE_READY:
1487 		switch (cmd) {
1488 		case Z_READY:
1489 			/*
1490 			 * We could have two clients racing to ready this
1491 			 * zone; the second client loses, but its request
1492 			 * doesn't fail, since the zone is now in the desired
1493 			 * state.
1494 			 */
1495 			zerror(zlogp, B_FALSE, "zone is already ready");
1496 			rval = 0;
1497 			break;
1498 		case Z_FORCEBOOT:
1499 		case Z_BOOT:
1500 			(void) strlcpy(boot_args, zargp->bootbuf,
1501 			    sizeof (boot_args));
1502 			eventstream_write(Z_EVT_ZONE_BOOTING);
1503 			rval = zone_bootup(zlogp, zargp->bootbuf, zstate);
1504 			audit_put_record(zlogp, uc, rval, "boot");
1505 			if (rval != 0) {
1506 				bringup_failure_recovery = B_TRUE;
1507 				(void) zone_halt(zlogp, B_FALSE, B_TRUE,
1508 				    zstate);
1509 				eventstream_write(Z_EVT_ZONE_BOOTFAILED);
1510 			}
1511 			boot_args[0] = '\0';
1512 			break;
1513 		case Z_HALT:
1514 			if (kernelcall)	/* Invalid; can't happen */
1515 				abort();
1516 			if ((rval = zone_halt(zlogp, B_FALSE, B_FALSE, zstate))
1517 			    != 0)
1518 				break;
1519 			eventstream_write(Z_EVT_ZONE_HALTED);
1520 			break;
1521 		case Z_SHUTDOWN:
1522 		case Z_REBOOT:
1523 		case Z_NOTE_UNINSTALLING:
1524 		case Z_FORCEMOUNT:
1525 		case Z_MOUNT:
1526 		case Z_UNMOUNT:
1527 			if (kernelcall)	/* Invalid; can't happen */
1528 				abort();
1529 			zerror(zlogp, B_FALSE, "%s operation is invalid "
1530 			    "for zones in state '%s'", z_cmd_name(cmd),
1531 			    zone_state_str(zstate));
1532 			rval = -1;
1533 			break;
1534 		}
1535 		break;
1536 
1537 	case ZONE_STATE_MOUNTED:
1538 		switch (cmd) {
1539 		case Z_UNMOUNT:
1540 			if (kernelcall)	/* Invalid; can't happen */
1541 				abort();
1542 			rval = zone_halt(zlogp, B_TRUE, B_FALSE, zstate);
1543 			if (rval == 0) {
1544 				eventstream_write(Z_EVT_ZONE_HALTED);
1545 				(void) sema_post(&scratch_sem);
1546 			}
1547 			break;
1548 		default:
1549 			if (kernelcall)	/* Invalid; can't happen */
1550 				abort();
1551 			zerror(zlogp, B_FALSE, "%s operation is invalid "
1552 			    "for zones in state '%s'", z_cmd_name(cmd),
1553 			    zone_state_str(zstate));
1554 			rval = -1;
1555 			break;
1556 		}
1557 		break;
1558 
1559 	case ZONE_STATE_RUNNING:
1560 	case ZONE_STATE_SHUTTING_DOWN:
1561 	case ZONE_STATE_DOWN:
1562 		switch (cmd) {
1563 		case Z_READY:
1564 			if ((rval = zone_halt(zlogp, B_FALSE, B_TRUE, zstate))
1565 			    != 0)
1566 				break;
1567 			if ((rval = zone_ready(zlogp, Z_MNT_BOOT, zstate)) == 0)
1568 				eventstream_write(Z_EVT_ZONE_READIED);
1569 			else
1570 				eventstream_write(Z_EVT_ZONE_HALTED);
1571 			break;
1572 		case Z_FORCEBOOT:
1573 		case Z_BOOT:
1574 			/*
1575 			 * We could have two clients racing to boot this
1576 			 * zone; the second client loses, but its request
1577 			 * doesn't fail, since the zone is now in the desired
1578 			 * state.
1579 			 */
1580 			zerror(zlogp, B_FALSE, "zone is already booted");
1581 			rval = 0;
1582 			break;
1583 		case Z_HALT:
1584 			if ((rval = zone_halt(zlogp, B_FALSE, B_FALSE, zstate))
1585 			    != 0)
1586 				break;
1587 			eventstream_write(Z_EVT_ZONE_HALTED);
1588 			break;
1589 		case Z_REBOOT:
1590 			(void) strlcpy(boot_args, zargp->bootbuf,
1591 			    sizeof (boot_args));
1592 			eventstream_write(Z_EVT_ZONE_REBOOTING);
1593 			if ((rval = zone_halt(zlogp, B_FALSE, B_TRUE, zstate))
1594 			    != 0) {
1595 				eventstream_write(Z_EVT_ZONE_BOOTFAILED);
1596 				boot_args[0] = '\0';
1597 				break;
1598 			}
1599 			if ((rval = zone_ready(zlogp, Z_MNT_BOOT, zstate))
1600 			    != 0) {
1601 				eventstream_write(Z_EVT_ZONE_BOOTFAILED);
1602 				boot_args[0] = '\0';
1603 				break;
1604 			}
1605 			rval = zone_bootup(zlogp, zargp->bootbuf, zstate);
1606 			audit_put_record(zlogp, uc, rval, "reboot");
1607 			if (rval != 0) {
1608 				(void) zone_halt(zlogp, B_FALSE, B_TRUE,
1609 				    zstate);
1610 				eventstream_write(Z_EVT_ZONE_BOOTFAILED);
1611 			}
1612 			boot_args[0] = '\0';
1613 			break;
1614 		case Z_SHUTDOWN:
1615 			if ((rval = zone_graceful_shutdown(zlogp)) == 0) {
1616 				wait_shut = B_TRUE;
1617 			}
1618 			break;
1619 		case Z_NOTE_UNINSTALLING:
1620 		case Z_FORCEMOUNT:
1621 		case Z_MOUNT:
1622 		case Z_UNMOUNT:
1623 			zerror(zlogp, B_FALSE, "%s operation is invalid "
1624 			    "for zones in state '%s'", z_cmd_name(cmd),
1625 			    zone_state_str(zstate));
1626 			rval = -1;
1627 			break;
1628 		}
1629 		break;
1630 	default:
1631 		abort();
1632 	}
1633 
1634 	/*
1635 	 * Because the state of the zone may have changed, we make sure
1636 	 * to wake the console poller, which is in charge of initiating
1637 	 * the shutdown procedure as necessary.
1638 	 */
1639 	eventstream_write(Z_EVT_NULL);
1640 
1641 out:
1642 	(void) mutex_unlock(&lock);
1643 
1644 	/* Wait for the Z_SHUTDOWN commands to complete */
1645 	if (wait_shut)
1646 		rval = zone_wait_shutdown(zlogp);
1647 
1648 	if (kernelcall) {
1649 		rvalp = NULL;
1650 		rlen = 0;
1651 	} else {
1652 		rvalp->rval = rval;
1653 	}
1654 	if (uc != NULL)
1655 		ucred_free(uc);
1656 	(void) door_return((char *)rvalp, rlen, NULL, 0);
1657 	thr_exit(NULL);
1658 }
1659 
1660 static int
1661 setup_door(zlog_t *zlogp)
1662 {
1663 	if ((zone_door = door_create(server, NULL,
1664 	    DOOR_UNREF | DOOR_REFUSE_DESC | DOOR_NO_CANCEL)) < 0) {
1665 		zerror(zlogp, B_TRUE, "%s failed", "door_create");
1666 		return (-1);
1667 	}
1668 	(void) fdetach(zone_door_path);
1669 
1670 	if (fattach(zone_door, zone_door_path) != 0) {
1671 		zerror(zlogp, B_TRUE, "fattach to %s failed", zone_door_path);
1672 		(void) door_revoke(zone_door);
1673 		(void) fdetach(zone_door_path);
1674 		zone_door = -1;
1675 		return (-1);
1676 	}
1677 	return (0);
1678 }
1679 
1680 /*
1681  * zoneadm(8) will start zoneadmd if it thinks it isn't running; this
1682  * is where zoneadmd itself will check to see that another instance of
1683  * zoneadmd isn't already controlling this zone.
1684  *
1685  * The idea here is that we want to open the path to which we will
1686  * attach our door, lock it, and then make sure that no-one has beat us
1687  * to fattach(3c)ing onto it.
1688  *
1689  * fattach(3c) is really a mount, so there are actually two possible
1690  * vnodes we could be dealing with.  Our strategy is as follows:
1691  *
1692  * - If the file we opened is a regular file (common case):
1693  *	There is no fattach(3c)ed door, so we have a chance of becoming
1694  *	the managing zoneadmd. We attempt to lock the file: if it is
1695  *	already locked, that means someone else raced us here, so we
1696  *	lose and give up.  zoneadm(8) will try to contact the zoneadmd
1697  *	that beat us to it.
1698  *
1699  * - If the file we opened is a namefs file:
1700  *	This means there is already an established door fattach(3c)'ed
1701  *	to the rendezvous path.  We've lost the race, so we give up.
1702  *	Note that in this case we also try to grab the file lock, and
1703  *	will succeed in acquiring it since the vnode locked by the
1704  *	"winning" zoneadmd was a regular one, and the one we locked was
1705  *	the fattach(3c)'ed door node.  At any rate, no harm is done, and
1706  *	we just return to zoneadm(8) which knows to retry.
1707  */
1708 static int
1709 make_daemon_exclusive(zlog_t *zlogp)
1710 {
1711 	int doorfd = -1;
1712 	int err, ret = -1;
1713 	struct stat st;
1714 	struct flock flock;
1715 	zone_state_t zstate;
1716 
1717 top:
1718 	if ((err = zone_get_state(zone_name, &zstate)) != Z_OK) {
1719 		zerror(zlogp, B_FALSE, "failed to get zone state: %s",
1720 		    zonecfg_strerror(err));
1721 		goto out;
1722 	}
1723 	if ((doorfd = open(zone_door_path, O_CREAT|O_RDWR,
1724 	    S_IREAD|S_IWRITE)) < 0) {
1725 		zerror(zlogp, B_TRUE, "failed to open %s", zone_door_path);
1726 		goto out;
1727 	}
1728 	if (fstat(doorfd, &st) < 0) {
1729 		zerror(zlogp, B_TRUE, "failed to stat %s", zone_door_path);
1730 		goto out;
1731 	}
1732 	/*
1733 	 * Lock the file to synchronize with other zoneadmd
1734 	 */
1735 	flock.l_type = F_WRLCK;
1736 	flock.l_whence = SEEK_SET;
1737 	flock.l_start = (off_t)0;
1738 	flock.l_len = (off_t)0;
1739 	if (fcntl(doorfd, F_SETLK, &flock) < 0) {
1740 		/*
1741 		 * Someone else raced us here and grabbed the lock file
1742 		 * first.  A warning here is inappropriate since nothing
1743 		 * went wrong.
1744 		 */
1745 		goto out;
1746 	}
1747 
1748 	if (strcmp(st.st_fstype, "namefs") == 0) {
1749 		struct door_info info;
1750 
1751 		/*
1752 		 * There is already something fattach()'ed to this file.
1753 		 * Lets see what the door is up to.
1754 		 */
1755 		if (door_info(doorfd, &info) == 0 && info.di_target != -1) {
1756 			/*
1757 			 * Another zoneadmd process seems to be in
1758 			 * control of the situation and we don't need to
1759 			 * be here.  A warning here is inappropriate
1760 			 * since nothing went wrong.
1761 			 *
1762 			 * If the door has been revoked, the zoneadmd
1763 			 * process currently managing the zone is going
1764 			 * away.  We'll return control to zoneadm(8)
1765 			 * which will try again (by which time zoneadmd
1766 			 * will hopefully have exited).
1767 			 */
1768 			goto out;
1769 		}
1770 
1771 		/*
1772 		 * If we got this far, there's a fattach(3c)'ed door
1773 		 * that belongs to a process that has exited, which can
1774 		 * happen if the previous zoneadmd died unexpectedly.
1775 		 *
1776 		 * Let user know that something is amiss, but that we can
1777 		 * recover; if the zone is in the installed state, then don't
1778 		 * message, since having a running zoneadmd isn't really
1779 		 * expected/needed.  We want to keep occurences of this message
1780 		 * limited to times when zoneadmd is picking back up from a
1781 		 * zoneadmd that died while the zone was in some non-trivial
1782 		 * state.
1783 		 */
1784 		if (zstate > ZONE_STATE_INSTALLED) {
1785 			zerror(zlogp, B_FALSE,
1786 			    "zone '%s': WARNING: zone is in state '%s', but "
1787 			    "zoneadmd does not appear to be available; "
1788 			    "restarted zoneadmd to recover.",
1789 			    zone_name, zone_state_str(zstate));
1790 		}
1791 
1792 		(void) fdetach(zone_door_path);
1793 		(void) close(doorfd);
1794 		goto top;
1795 	}
1796 	ret = 0;
1797 out:
1798 	(void) close(doorfd);
1799 	return (ret);
1800 }
1801 
1802 /*
1803  * Setup the brand's pre and post state change callbacks, as well as the
1804  * query callback, if any of these exist.
1805  */
1806 static int
1807 brand_callback_init(brand_handle_t bh, char *zone_name)
1808 {
1809 	char zpath[MAXPATHLEN];
1810 
1811 	if (zone_get_zonepath(zone_name, zpath, sizeof (zpath)) != Z_OK)
1812 		return (-1);
1813 
1814 	(void) strlcpy(pre_statechg_hook, EXEC_PREFIX,
1815 	    sizeof (pre_statechg_hook));
1816 
1817 	if (brand_get_prestatechange(bh, zone_name, zpath,
1818 	    pre_statechg_hook + EXEC_LEN,
1819 	    sizeof (pre_statechg_hook) - EXEC_LEN) != 0)
1820 		return (-1);
1821 
1822 	if (strlen(pre_statechg_hook) <= EXEC_LEN)
1823 		pre_statechg_hook[0] = '\0';
1824 
1825 	(void) strlcpy(post_statechg_hook, EXEC_PREFIX,
1826 	    sizeof (post_statechg_hook));
1827 
1828 	if (brand_get_poststatechange(bh, zone_name, zpath,
1829 	    post_statechg_hook + EXEC_LEN,
1830 	    sizeof (post_statechg_hook) - EXEC_LEN) != 0)
1831 		return (-1);
1832 
1833 	if (strlen(post_statechg_hook) <= EXEC_LEN)
1834 		post_statechg_hook[0] = '\0';
1835 
1836 	(void) strlcpy(query_hook, EXEC_PREFIX,
1837 	    sizeof (query_hook));
1838 
1839 	if (brand_get_query(bh, zone_name, zpath, query_hook + EXEC_LEN,
1840 	    sizeof (query_hook) - EXEC_LEN) != 0)
1841 		return (-1);
1842 
1843 	if (strlen(query_hook) <= EXEC_LEN)
1844 		query_hook[0] = '\0';
1845 
1846 	return (0);
1847 }
1848 
1849 int
1850 main(int argc, char *argv[])
1851 {
1852 	int opt;
1853 	zoneid_t zid;
1854 	priv_set_t *privset;
1855 	zone_state_t zstate;
1856 	char parents_locale[MAXPATHLEN];
1857 	brand_handle_t bh;
1858 	int err;
1859 
1860 	pid_t pid;
1861 	sigset_t blockset;
1862 	sigset_t block_cld;
1863 
1864 	struct {
1865 		sema_t sem;
1866 		int status;
1867 		zlog_t log;
1868 	} *shstate;
1869 	size_t shstatelen = getpagesize();
1870 
1871 	zlog_t errlog;
1872 	zlog_t *zlogp;
1873 
1874 	int ctfd;
1875 
1876 	progname = get_execbasename(argv[0]);
1877 
1878 	/*
1879 	 * Make sure stderr is unbuffered
1880 	 */
1881 	(void) setbuffer(stderr, NULL, 0);
1882 
1883 	/*
1884 	 * Get out of the way of mounted filesystems, since we will daemonize
1885 	 * soon.
1886 	 */
1887 	(void) chdir("/");
1888 
1889 	/*
1890 	 * Use the default system umask per PSARC 1998/110 rather than
1891 	 * anything that may have been set by the caller.
1892 	 */
1893 	(void) umask(CMASK);
1894 
1895 	/*
1896 	 * Initially we want to use our parent's locale.
1897 	 */
1898 	(void) setlocale(LC_ALL, "");
1899 	(void) textdomain(TEXT_DOMAIN);
1900 	(void) strlcpy(parents_locale, setlocale(LC_MESSAGES, NULL),
1901 	    sizeof (parents_locale));
1902 
1903 	/*
1904 	 * This zlog_t is used for writing to stderr
1905 	 */
1906 	errlog.logfile = stderr;
1907 	errlog.buflen = errlog.loglen = 0;
1908 	errlog.buf = errlog.log = NULL;
1909 	errlog.locale = parents_locale;
1910 
1911 	/*
1912 	 * We start off writing to stderr until we're ready to daemonize.
1913 	 */
1914 	zlogp = &errlog;
1915 
1916 	/*
1917 	 * Process options.
1918 	 */
1919 	while ((opt = getopt(argc, argv, "R:z:")) != EOF) {
1920 		switch (opt) {
1921 		case 'R':
1922 			zonecfg_set_root(optarg);
1923 			break;
1924 		case 'z':
1925 			zone_name = optarg;
1926 			break;
1927 		default:
1928 			usage();
1929 		}
1930 	}
1931 
1932 	if (zone_name == NULL)
1933 		usage();
1934 
1935 	/*
1936 	 * Because usage() prints directly to stderr, it has gettext()
1937 	 * wrapping, which depends on the locale.  But since zerror() calls
1938 	 * localize() which tweaks the locale, it is not safe to call zerror()
1939 	 * until after the last call to usage().  Fortunately, the last call
1940 	 * to usage() is just above and the first call to zerror() is just
1941 	 * below.  Don't mess this up.
1942 	 */
1943 	if (strcmp(zone_name, GLOBAL_ZONENAME) == 0) {
1944 		zerror(zlogp, B_FALSE, "cannot manage the %s zone",
1945 		    GLOBAL_ZONENAME);
1946 		return (1);
1947 	}
1948 
1949 	if (zone_get_id(zone_name, &zid) != 0) {
1950 		zerror(zlogp, B_FALSE, "could not manage %s: %s", zone_name,
1951 		    zonecfg_strerror(Z_NO_ZONE));
1952 		return (1);
1953 	}
1954 
1955 	if ((err = zone_get_state(zone_name, &zstate)) != Z_OK) {
1956 		zerror(zlogp, B_FALSE, "failed to get zone state: %s",
1957 		    zonecfg_strerror(err));
1958 		return (1);
1959 	}
1960 	if (zstate < ZONE_STATE_INCOMPLETE) {
1961 		zerror(zlogp, B_FALSE,
1962 		    "cannot manage a zone which is in state '%s'",
1963 		    zone_state_str(zstate));
1964 		return (1);
1965 	}
1966 
1967 	if (zonecfg_default_brand(default_brand,
1968 	    sizeof (default_brand)) != Z_OK) {
1969 		zerror(zlogp, B_FALSE, "unable to determine default brand");
1970 		return (1);
1971 	}
1972 
1973 	/* Get a handle to the brand info for this zone */
1974 	if (zone_get_brand(zone_name, brand_name, sizeof (brand_name))
1975 	    != Z_OK) {
1976 		zerror(zlogp, B_FALSE, "unable to determine zone brand");
1977 		return (1);
1978 	}
1979 	zone_isnative = (strcmp(brand_name, NATIVE_BRAND_NAME) == 0);
1980 	zone_islabeled = (strcmp(brand_name, LABELED_BRAND_NAME) == 0);
1981 
1982 	/*
1983 	 * In the alternate root environment, the only supported
1984 	 * operations are mount and unmount.  In this case, just treat
1985 	 * the zone as native if it is cluster.  Cluster zones can be
1986 	 * native for the purpose of LU or upgrade, and the cluster
1987 	 * brand may not exist in the miniroot (such as in net install
1988 	 * upgrade).
1989 	 */
1990 	if (strcmp(brand_name, CLUSTER_BRAND_NAME) == 0) {
1991 		zone_iscluster = B_TRUE;
1992 		if (zonecfg_in_alt_root()) {
1993 			(void) strlcpy(brand_name, default_brand,
1994 			    sizeof (brand_name));
1995 		}
1996 	} else {
1997 		zone_iscluster = B_FALSE;
1998 	}
1999 
2000 	if ((bh = brand_open(brand_name)) == NULL) {
2001 		zerror(zlogp, B_FALSE, "unable to open zone brand");
2002 		return (1);
2003 	}
2004 
2005 	/* Get state change brand hooks. */
2006 	if (brand_callback_init(bh, zone_name) == -1) {
2007 		zerror(zlogp, B_TRUE,
2008 		    "failed to initialize brand state change hooks");
2009 		brand_close(bh);
2010 		return (1);
2011 	}
2012 
2013 	brand_close(bh);
2014 
2015 	/*
2016 	 * Check that we have all privileges.  It would be nice to pare
2017 	 * this down, but this is at least a first cut.
2018 	 */
2019 	if ((privset = priv_allocset()) == NULL) {
2020 		zerror(zlogp, B_TRUE, "%s failed", "priv_allocset");
2021 		return (1);
2022 	}
2023 
2024 	if (getppriv(PRIV_EFFECTIVE, privset) != 0) {
2025 		zerror(zlogp, B_TRUE, "%s failed", "getppriv");
2026 		priv_freeset(privset);
2027 		return (1);
2028 	}
2029 
2030 	if (priv_isfullset(privset) == B_FALSE) {
2031 		zerror(zlogp, B_FALSE, "You lack sufficient privilege to "
2032 		    "run this command (all privs required)");
2033 		priv_freeset(privset);
2034 		return (1);
2035 	}
2036 	priv_freeset(privset);
2037 
2038 	if (mkzonedir(zlogp) != 0)
2039 		return (1);
2040 
2041 	/*
2042 	 * Pre-fork: setup shared state
2043 	 */
2044 	if ((shstate = (void *)mmap(NULL, shstatelen,
2045 	    PROT_READ|PROT_WRITE, MAP_SHARED|MAP_ANON, -1, (off_t)0)) ==
2046 	    MAP_FAILED) {
2047 		zerror(zlogp, B_TRUE, "%s failed", "mmap");
2048 		return (1);
2049 	}
2050 	if (sema_init(&shstate->sem, 0, USYNC_PROCESS, NULL) != 0) {
2051 		zerror(zlogp, B_TRUE, "%s failed", "sema_init()");
2052 		(void) munmap((char *)shstate, shstatelen);
2053 		return (1);
2054 	}
2055 	shstate->log.logfile = NULL;
2056 	shstate->log.buflen = shstatelen - sizeof (*shstate);
2057 	shstate->log.loglen = shstate->log.buflen;
2058 	shstate->log.buf = (char *)shstate + sizeof (*shstate);
2059 	shstate->log.log = shstate->log.buf;
2060 	shstate->log.locale = parents_locale;
2061 	shstate->status = -1;
2062 
2063 	/*
2064 	 * We need a SIGCHLD handler so the sema_wait() below will wake
2065 	 * up if the child dies without doing a sema_post().
2066 	 */
2067 	(void) sigset(SIGCHLD, sigchld);
2068 	/*
2069 	 * We must mask SIGCHLD until after we've coped with the fork
2070 	 * sufficiently to deal with it; otherwise we can race and
2071 	 * receive the signal before pid has been initialized
2072 	 * (yes, this really happens).
2073 	 */
2074 	(void) sigemptyset(&block_cld);
2075 	(void) sigaddset(&block_cld, SIGCHLD);
2076 	(void) sigprocmask(SIG_BLOCK, &block_cld, NULL);
2077 
2078 	/*
2079 	 * The parent only needs stderr after the fork, so close other fd's
2080 	 * that we inherited from zoneadm so that the parent doesn't have those
2081 	 * open while waiting. The child will close the rest after the fork.
2082 	 */
2083 	closefrom(3);
2084 
2085 	if ((ctfd = init_template()) == -1) {
2086 		zerror(zlogp, B_TRUE, "failed to create contract");
2087 		return (1);
2088 	}
2089 
2090 	/*
2091 	 * Do not let another thread localize a message while we are forking.
2092 	 */
2093 	(void) mutex_lock(&msglock);
2094 	pid = fork();
2095 	(void) mutex_unlock(&msglock);
2096 
2097 	/*
2098 	 * In all cases (parent, child, and in the event of an error) we
2099 	 * don't want to cause creation of contracts on subsequent fork()s.
2100 	 */
2101 	(void) ct_tmpl_clear(ctfd);
2102 	(void) close(ctfd);
2103 
2104 	if (pid == -1) {
2105 		zerror(zlogp, B_TRUE, "could not fork");
2106 		return (1);
2107 
2108 	} else if (pid > 0) { /* parent */
2109 		(void) sigprocmask(SIG_UNBLOCK, &block_cld, NULL);
2110 		/*
2111 		 * This marks a window of vulnerability in which we receive
2112 		 * the SIGCLD before falling into sema_wait (normally we would
2113 		 * get woken up from sema_wait with EINTR upon receipt of
2114 		 * SIGCLD).  So we may need to use some other scheme like
2115 		 * sema_posting in the sigcld handler.
2116 		 * blech
2117 		 */
2118 		(void) sema_wait(&shstate->sem);
2119 		(void) sema_destroy(&shstate->sem);
2120 		if (shstate->status != 0)
2121 			(void) waitpid(pid, NULL, WNOHANG);
2122 		/*
2123 		 * It's ok if we die with SIGPIPE.  It's not like we could have
2124 		 * done anything about it.
2125 		 */
2126 		(void) fprintf(stderr, "%s", shstate->log.buf);
2127 		_exit(shstate->status == 0 ? 0 : 1);
2128 	}
2129 
2130 	/*
2131 	 * The child charges on.
2132 	 */
2133 	(void) sigset(SIGCHLD, SIG_DFL);
2134 	(void) sigprocmask(SIG_UNBLOCK, &block_cld, NULL);
2135 
2136 	/*
2137 	 * SIGPIPE can be delivered if we write to a socket for which the
2138 	 * peer endpoint is gone.  That can lead to too-early termination
2139 	 * of zoneadmd, and that's not good eats.
2140 	 */
2141 	(void) sigset(SIGPIPE, SIG_IGN);
2142 	/*
2143 	 * Stop using stderr
2144 	 */
2145 	zlogp = &shstate->log;
2146 
2147 	/*
2148 	 * We don't need stdout/stderr from now on.
2149 	 */
2150 	closefrom(0);
2151 
2152 	/*
2153 	 * Initialize the syslog zlog_t.  This needs to be done after
2154 	 * the call to closefrom().
2155 	 */
2156 	logsys.buf = logsys.log = NULL;
2157 	logsys.buflen = logsys.loglen = 0;
2158 	logsys.logfile = NULL;
2159 	logsys.locale = DEFAULT_LOCALE;
2160 
2161 	openlog("zoneadmd", LOG_PID, LOG_DAEMON);
2162 
2163 	/*
2164 	 * The eventstream is used to publish state changes in the zone
2165 	 * from the door threads to the console I/O poller.
2166 	 */
2167 	if (eventstream_init() == -1) {
2168 		zerror(zlogp, B_TRUE, "unable to create eventstream");
2169 		goto child_out;
2170 	}
2171 
2172 	(void) snprintf(zone_door_path, sizeof (zone_door_path),
2173 	    "%s" ZONE_DOOR_PATH, zonecfg_get_root(), zone_name);
2174 
2175 	/*
2176 	 * See if another zoneadmd is running for this zone.  If not, then we
2177 	 * can now modify system state.
2178 	 */
2179 	if (make_daemon_exclusive(zlogp) == -1)
2180 		goto child_out;
2181 
2182 
2183 	/*
2184 	 * Create/join a new session; we need to be careful of what we do with
2185 	 * the console from now on so we don't end up being the session leader
2186 	 * for the terminal we're going to be handing out.
2187 	 */
2188 	(void) setsid();
2189 
2190 	/*
2191 	 * This thread shouldn't be receiving any signals; in particular,
2192 	 * SIGCHLD should be received by the thread doing the fork().
2193 	 */
2194 	(void) sigfillset(&blockset);
2195 	(void) thr_sigsetmask(SIG_BLOCK, &blockset, NULL);
2196 
2197 	/*
2198 	 * Setup the console device and get ready to serve the console;
2199 	 * once this has completed, we're ready to let console clients
2200 	 * make an attempt to connect (they will block until
2201 	 * serve_console_sock() below gets called, and any pending
2202 	 * connection is accept()ed).
2203 	 */
2204 	if (!zonecfg_in_alt_root() && init_console(zlogp) < 0)
2205 		goto child_out;
2206 
2207 	/*
2208 	 * Take the lock now, so that when the door server gets going, we
2209 	 * are guaranteed that it won't take a request until we are sure
2210 	 * that everything is completely set up.  See the child_out: label
2211 	 * below to see why this matters.
2212 	 */
2213 	(void) mutex_lock(&lock);
2214 
2215 	/* Init semaphore for scratch zones. */
2216 	if (sema_init(&scratch_sem, 0, USYNC_THREAD, NULL) == -1) {
2217 		zerror(zlogp, B_TRUE,
2218 		    "failed to initialize semaphore for scratch zone");
2219 		goto child_out;
2220 	}
2221 
2222 	/* open the dladm handle */
2223 	if (dladm_open(&dld_handle) != DLADM_STATUS_OK) {
2224 		zerror(zlogp, B_FALSE, "failed to open dladm handle");
2225 		goto child_out;
2226 	}
2227 
2228 	/*
2229 	 * Note: door setup must occur *after* the console is setup.
2230 	 * This is so that as zlogin tests the door to see if zoneadmd
2231 	 * is ready yet, we know that the console will get serviced
2232 	 * once door_info() indicates that the door is "up".
2233 	 */
2234 	if (setup_door(zlogp) == -1)
2235 		goto child_out;
2236 
2237 	/*
2238 	 * Things seem OK so far; tell the parent process that we're done
2239 	 * with setup tasks.  This will cause the parent to exit, signalling
2240 	 * to zoneadm, zlogin, or whatever forked it that we are ready to
2241 	 * service requests.
2242 	 */
2243 	shstate->status = 0;
2244 	(void) sema_post(&shstate->sem);
2245 	(void) munmap((char *)shstate, shstatelen);
2246 	shstate = NULL;
2247 
2248 	(void) mutex_unlock(&lock);
2249 
2250 	/*
2251 	 * zlogp is now invalid, so reset it to the syslog logger.
2252 	 */
2253 	zlogp = &logsys;
2254 
2255 	/*
2256 	 * Now that we are free of any parents, switch to the default locale.
2257 	 */
2258 	(void) setlocale(LC_ALL, DEFAULT_LOCALE);
2259 
2260 	/*
2261 	 * At this point the setup portion of main() is basically done, so
2262 	 * we reuse this thread to manage the zone console.  When
2263 	 * serve_console() has returned, we are past the point of no return
2264 	 * in the life of this zoneadmd.
2265 	 */
2266 	if (zonecfg_in_alt_root()) {
2267 		/*
2268 		 * This is just awful, but mounted scratch zones don't (and
2269 		 * can't) have consoles.  We just wait for unmount instead.
2270 		 */
2271 		while (sema_wait(&scratch_sem) == EINTR)
2272 			;
2273 	} else {
2274 		serve_console(zlogp);
2275 		assert(in_death_throes);
2276 	}
2277 
2278 	/*
2279 	 * This is the next-to-last part of the exit interlock.  Upon calling
2280 	 * fdetach(), the door will go unreferenced; once any
2281 	 * outstanding requests (like the door thread doing Z_HALT) are
2282 	 * done, the door will get an UNREF notification; when it handles
2283 	 * the UNREF, the door server will cause the exit.  It's possible
2284 	 * that fdetach() can fail because the file is in use, in which
2285 	 * case we'll retry the operation.
2286 	 */
2287 	assert(!MUTEX_HELD(&lock));
2288 	for (;;) {
2289 		if ((fdetach(zone_door_path) == 0) || (errno != EBUSY))
2290 			break;
2291 		yield();
2292 	}
2293 
2294 	for (;;)
2295 		(void) pause();
2296 
2297 child_out:
2298 	assert(pid == 0);
2299 
2300 	shstate->status = -1;
2301 	(void) sema_post(&shstate->sem);
2302 	(void) munmap((char *)shstate, shstatelen);
2303 
2304 	/*
2305 	 * This might trigger an unref notification, but if so,
2306 	 * we are still holding the lock, so our call to exit will
2307 	 * ultimately win the race and will publish the right exit
2308 	 * code.
2309 	 */
2310 	if (zone_door != -1) {
2311 		assert(MUTEX_HELD(&lock));
2312 		(void) door_revoke(zone_door);
2313 		(void) fdetach(zone_door_path);
2314 	}
2315 
2316 	if (dld_handle != NULL)
2317 		dladm_close(dld_handle);
2318 
2319 	return (1); /* return from main() forcibly exits an MT process */
2320 }
2321