xref: /titanic_54/usr/src/cmd/zoneadmd/zoneadmd.c (revision 0dc2366f7b9f9f36e10909b1e95edbf2a261c2ac)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5ffbafc53Scomay  * Common Development and Distribution License (the "License").
6ffbafc53Scomay  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
21ffbafc53Scomay 
227c478bd9Sstevel@tonic-gate /*
23*0dc2366fSVenugopal Iyer  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate /*
287c478bd9Sstevel@tonic-gate  * zoneadmd manages zones; one zoneadmd process is launched for each
297c478bd9Sstevel@tonic-gate  * non-global zone on the system.  This daemon juggles four jobs:
307c478bd9Sstevel@tonic-gate  *
317c478bd9Sstevel@tonic-gate  * - Implement setup and teardown of the zone "virtual platform": mount and
327c478bd9Sstevel@tonic-gate  *   unmount filesystems; create and destroy network interfaces; communicate
337c478bd9Sstevel@tonic-gate  *   with devfsadmd to lay out devices for the zone; instantiate the zone
347c478bd9Sstevel@tonic-gate  *   console device; configure process runtime attributes such as resource
357c478bd9Sstevel@tonic-gate  *   controls, pool bindings, fine-grained privileges.
367c478bd9Sstevel@tonic-gate  *
377c478bd9Sstevel@tonic-gate  * - Launch the zone's init(1M) process.
387c478bd9Sstevel@tonic-gate  *
397c478bd9Sstevel@tonic-gate  * - Implement a door server; clients (like zoneadm) connect to the door
407c478bd9Sstevel@tonic-gate  *   server and request zone state changes.  The kernel is also a client of
417c478bd9Sstevel@tonic-gate  *   this door server.  A request to halt or reboot the zone which originates
427c478bd9Sstevel@tonic-gate  *   *inside* the zone results in a door upcall from the kernel into zoneadmd.
437c478bd9Sstevel@tonic-gate  *
447c478bd9Sstevel@tonic-gate  *   One minor problem is that messages emitted by zoneadmd need to be passed
457c478bd9Sstevel@tonic-gate  *   back to the zoneadm process making the request.  These messages need to
467c478bd9Sstevel@tonic-gate  *   be rendered in the client's locale; so, this is passed in as part of the
477c478bd9Sstevel@tonic-gate  *   request.  The exception is the kernel upcall to zoneadmd, in which case
487c478bd9Sstevel@tonic-gate  *   messages are syslog'd.
497c478bd9Sstevel@tonic-gate  *
507c478bd9Sstevel@tonic-gate  *   To make all of this work, the Makefile adds -a to xgettext to extract *all*
517c478bd9Sstevel@tonic-gate  *   strings, and an exclusion file (zoneadmd.xcl) is used to exclude those
527c478bd9Sstevel@tonic-gate  *   strings which do not need to be translated.
537c478bd9Sstevel@tonic-gate  *
547c478bd9Sstevel@tonic-gate  * - Act as a console server for zlogin -C processes; see comments in zcons.c
557c478bd9Sstevel@tonic-gate  *   for more information about the zone console architecture.
567c478bd9Sstevel@tonic-gate  *
577c478bd9Sstevel@tonic-gate  * DESIGN NOTES
587c478bd9Sstevel@tonic-gate  *
597c478bd9Sstevel@tonic-gate  * Restart:
607c478bd9Sstevel@tonic-gate  *   A chief design constraint of zoneadmd is that it should be restartable in
617c478bd9Sstevel@tonic-gate  *   the case that the administrator kills it off, or it suffers a fatal error,
627c478bd9Sstevel@tonic-gate  *   without the running zone being impacted; this is akin to being able to
637c478bd9Sstevel@tonic-gate  *   reboot the service processor of a server without affecting the OS instance.
647c478bd9Sstevel@tonic-gate  */
657c478bd9Sstevel@tonic-gate 
667c478bd9Sstevel@tonic-gate #include <sys/param.h>
677c478bd9Sstevel@tonic-gate #include <sys/mman.h>
687c478bd9Sstevel@tonic-gate #include <sys/types.h>
697c478bd9Sstevel@tonic-gate #include <sys/stat.h>
707c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
717c478bd9Sstevel@tonic-gate 
727c478bd9Sstevel@tonic-gate #include <bsm/adt.h>
737c478bd9Sstevel@tonic-gate #include <bsm/adt_event.h>
747c478bd9Sstevel@tonic-gate 
757c478bd9Sstevel@tonic-gate #include <alloca.h>
767c478bd9Sstevel@tonic-gate #include <assert.h>
777c478bd9Sstevel@tonic-gate #include <errno.h>
787c478bd9Sstevel@tonic-gate #include <door.h>
797c478bd9Sstevel@tonic-gate #include <fcntl.h>
807c478bd9Sstevel@tonic-gate #include <locale.h>
817c478bd9Sstevel@tonic-gate #include <signal.h>
827c478bd9Sstevel@tonic-gate #include <stdarg.h>
837c478bd9Sstevel@tonic-gate #include <stdio.h>
847c478bd9Sstevel@tonic-gate #include <stdlib.h>
857c478bd9Sstevel@tonic-gate #include <string.h>
867c478bd9Sstevel@tonic-gate #include <strings.h>
877c478bd9Sstevel@tonic-gate #include <synch.h>
887c478bd9Sstevel@tonic-gate #include <syslog.h>
897c478bd9Sstevel@tonic-gate #include <thread.h>
907c478bd9Sstevel@tonic-gate #include <unistd.h>
917c478bd9Sstevel@tonic-gate #include <wait.h>
927c478bd9Sstevel@tonic-gate #include <limits.h>
937c478bd9Sstevel@tonic-gate #include <zone.h>
949acbbeafSnn35248 #include <libbrand.h>
950679f794S #include <sys/brand.h>
967c478bd9Sstevel@tonic-gate #include <libcontract.h>
977c478bd9Sstevel@tonic-gate #include <libcontract_priv.h>
9862868012SSteve Lawrence #include <sys/brand.h>
997c478bd9Sstevel@tonic-gate #include <sys/contract/process.h>
1007c478bd9Sstevel@tonic-gate #include <sys/ctfs.h>
1012b24ab6bSSebastien Roy #include <libdladm.h>
1022b24ab6bSSebastien Roy #include <sys/dls_mgmt.h>
1037c478bd9Sstevel@tonic-gate 
1047c478bd9Sstevel@tonic-gate #include <libzonecfg.h>
1057c478bd9Sstevel@tonic-gate #include "zoneadmd.h"
1067c478bd9Sstevel@tonic-gate 
1077c478bd9Sstevel@tonic-gate static char *progname;
1087c478bd9Sstevel@tonic-gate char *zone_name;	/* zone which we are managing */
109*0dc2366fSVenugopal Iyer char *pool_name;
110e5816e35SEdward Pilatowicz char default_brand[MAXNAMELEN];
1119acbbeafSnn35248 char brand_name[MAXNAMELEN];
1129acbbeafSnn35248 boolean_t zone_isnative;
11384561e8cStd153743 boolean_t zone_iscluster;
1149a5d73e0SRic Aleshire boolean_t zone_islabeled;
115108322fbScarlsonj static zoneid_t zone_id;
1164ac67f02SAnurag S. Maskey dladm_handle_t dld_handle = NULL;
1177c478bd9Sstevel@tonic-gate 
118c5cd6260S static char pre_statechg_hook[2 * MAXPATHLEN];
119c5cd6260S static char post_statechg_hook[2 * MAXPATHLEN];
120c5cd6260S char query_hook[2 * MAXPATHLEN];
121c5cd6260S 
12222321485Svp157776 zlog_t logsys;
1237c478bd9Sstevel@tonic-gate 
1247c478bd9Sstevel@tonic-gate mutex_t	lock = DEFAULTMUTEX;	/* to serialize stuff */
1257c478bd9Sstevel@tonic-gate mutex_t	msglock = DEFAULTMUTEX;	/* for calling setlocale() */
1267c478bd9Sstevel@tonic-gate 
127108322fbScarlsonj static sema_t scratch_sem;	/* for scratch zones */
128108322fbScarlsonj 
1297c478bd9Sstevel@tonic-gate static char	zone_door_path[MAXPATHLEN];
1307c478bd9Sstevel@tonic-gate static int	zone_door = -1;
1317c478bd9Sstevel@tonic-gate 
1327c478bd9Sstevel@tonic-gate boolean_t in_death_throes = B_FALSE;	/* daemon is dying */
1337c478bd9Sstevel@tonic-gate boolean_t bringup_failure_recovery = B_FALSE; /* ignore certain failures */
1347c478bd9Sstevel@tonic-gate 
1357c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)		/* should be defined by cc -D */
1367c478bd9Sstevel@tonic-gate #define	TEXT_DOMAIN	"SYS_TEST"	/* Use this only if it wasn't */
1377c478bd9Sstevel@tonic-gate #endif
1387c478bd9Sstevel@tonic-gate 
1397c478bd9Sstevel@tonic-gate #define	DEFAULT_LOCALE	"C"
1407c478bd9Sstevel@tonic-gate 
141108322fbScarlsonj static const char *
142108322fbScarlsonj z_cmd_name(zone_cmd_t zcmd)
143108322fbScarlsonj {
144108322fbScarlsonj 	/* This list needs to match the enum in sys/zone.h */
145108322fbScarlsonj 	static const char *zcmdstr[] = {
1469acbbeafSnn35248 		"ready", "boot", "forceboot", "reboot", "halt",
1479acbbeafSnn35248 		"note_uninstalling", "mount", "forcemount", "unmount"
148108322fbScarlsonj 	};
149108322fbScarlsonj 
150108322fbScarlsonj 	if (zcmd >= sizeof (zcmdstr) / sizeof (*zcmdstr))
151108322fbScarlsonj 		return ("unknown");
152108322fbScarlsonj 	else
153108322fbScarlsonj 		return (zcmdstr[(int)zcmd]);
154108322fbScarlsonj }
155108322fbScarlsonj 
1567c478bd9Sstevel@tonic-gate static char *
1577c478bd9Sstevel@tonic-gate get_execbasename(char *execfullname)
1587c478bd9Sstevel@tonic-gate {
1597c478bd9Sstevel@tonic-gate 	char *last_slash, *execbasename;
1607c478bd9Sstevel@tonic-gate 
1617c478bd9Sstevel@tonic-gate 	/* guard against '/' at end of command invocation */
1627c478bd9Sstevel@tonic-gate 	for (;;) {
1637c478bd9Sstevel@tonic-gate 		last_slash = strrchr(execfullname, '/');
1647c478bd9Sstevel@tonic-gate 		if (last_slash == NULL) {
1657c478bd9Sstevel@tonic-gate 			execbasename = execfullname;
1667c478bd9Sstevel@tonic-gate 			break;
1677c478bd9Sstevel@tonic-gate 		} else {
1687c478bd9Sstevel@tonic-gate 			execbasename = last_slash + 1;
1697c478bd9Sstevel@tonic-gate 			if (*execbasename == '\0') {
1707c478bd9Sstevel@tonic-gate 				*last_slash = '\0';
1717c478bd9Sstevel@tonic-gate 				continue;
1727c478bd9Sstevel@tonic-gate 			}
1737c478bd9Sstevel@tonic-gate 			break;
1747c478bd9Sstevel@tonic-gate 		}
1757c478bd9Sstevel@tonic-gate 	}
1767c478bd9Sstevel@tonic-gate 	return (execbasename);
1777c478bd9Sstevel@tonic-gate }
1787c478bd9Sstevel@tonic-gate 
1797c478bd9Sstevel@tonic-gate static void
1807c478bd9Sstevel@tonic-gate usage(void)
1817c478bd9Sstevel@tonic-gate {
1827c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, gettext("Usage: %s -z zonename\n"), progname);
1837c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr,
1847c478bd9Sstevel@tonic-gate 	    gettext("\tNote: %s should not be run directly.\n"), progname);
1857c478bd9Sstevel@tonic-gate 	exit(2);
1867c478bd9Sstevel@tonic-gate }
1877c478bd9Sstevel@tonic-gate 
1887c478bd9Sstevel@tonic-gate /* ARGSUSED */
1897c478bd9Sstevel@tonic-gate static void
1907c478bd9Sstevel@tonic-gate sigchld(int sig)
1917c478bd9Sstevel@tonic-gate {
1927c478bd9Sstevel@tonic-gate }
1937c478bd9Sstevel@tonic-gate 
1947c478bd9Sstevel@tonic-gate char *
1957c478bd9Sstevel@tonic-gate localize_msg(char *locale, const char *msg)
1967c478bd9Sstevel@tonic-gate {
1977c478bd9Sstevel@tonic-gate 	char *out;
1987c478bd9Sstevel@tonic-gate 
1997c478bd9Sstevel@tonic-gate 	(void) mutex_lock(&msglock);
2007c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_MESSAGES, locale);
2017c478bd9Sstevel@tonic-gate 	out = gettext(msg);
2027c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_MESSAGES, DEFAULT_LOCALE);
2037c478bd9Sstevel@tonic-gate 	(void) mutex_unlock(&msglock);
2047c478bd9Sstevel@tonic-gate 	return (out);
2057c478bd9Sstevel@tonic-gate }
2067c478bd9Sstevel@tonic-gate 
2077c478bd9Sstevel@tonic-gate /* PRINTFLIKE3 */
2087c478bd9Sstevel@tonic-gate void
2097c478bd9Sstevel@tonic-gate zerror(zlog_t *zlogp, boolean_t use_strerror, const char *fmt, ...)
2107c478bd9Sstevel@tonic-gate {
2117c478bd9Sstevel@tonic-gate 	va_list alist;
2127c478bd9Sstevel@tonic-gate 	char buf[MAXPATHLEN * 2]; /* enough space for err msg with a path */
2137c478bd9Sstevel@tonic-gate 	char *bp;
2147c478bd9Sstevel@tonic-gate 	int saved_errno = errno;
2157c478bd9Sstevel@tonic-gate 
2167c478bd9Sstevel@tonic-gate 	if (zlogp == NULL)
2177c478bd9Sstevel@tonic-gate 		return;
2187c478bd9Sstevel@tonic-gate 	if (zlogp == &logsys)
2197c478bd9Sstevel@tonic-gate 		(void) snprintf(buf, sizeof (buf), "[zone '%s'] ",
2207c478bd9Sstevel@tonic-gate 		    zone_name);
2217c478bd9Sstevel@tonic-gate 	else
2227c478bd9Sstevel@tonic-gate 		buf[0] = '\0';
2237c478bd9Sstevel@tonic-gate 	bp = &(buf[strlen(buf)]);
2247c478bd9Sstevel@tonic-gate 
2257c478bd9Sstevel@tonic-gate 	/*
2267c478bd9Sstevel@tonic-gate 	 * In theory, the locale pointer should be set to either "C" or a
2277c478bd9Sstevel@tonic-gate 	 * char array, so it should never be NULL
2287c478bd9Sstevel@tonic-gate 	 */
2297c478bd9Sstevel@tonic-gate 	assert(zlogp->locale != NULL);
2307c478bd9Sstevel@tonic-gate 	/* Locale is per process, but we are multi-threaded... */
2317c478bd9Sstevel@tonic-gate 	fmt = localize_msg(zlogp->locale, fmt);
2327c478bd9Sstevel@tonic-gate 
2337c478bd9Sstevel@tonic-gate 	va_start(alist, fmt);
2347c478bd9Sstevel@tonic-gate 	(void) vsnprintf(bp, sizeof (buf) - (bp - buf), fmt, alist);
2357c478bd9Sstevel@tonic-gate 	va_end(alist);
2367c478bd9Sstevel@tonic-gate 	bp = &(buf[strlen(buf)]);
2377c478bd9Sstevel@tonic-gate 	if (use_strerror)
2387c478bd9Sstevel@tonic-gate 		(void) snprintf(bp, sizeof (buf) - (bp - buf), ": %s",
2397c478bd9Sstevel@tonic-gate 		    strerror(saved_errno));
2407c478bd9Sstevel@tonic-gate 	if (zlogp == &logsys) {
2417c478bd9Sstevel@tonic-gate 		(void) syslog(LOG_ERR, "%s", buf);
2427c478bd9Sstevel@tonic-gate 	} else if (zlogp->logfile != NULL) {
2437c478bd9Sstevel@tonic-gate 		(void) fprintf(zlogp->logfile, "%s\n", buf);
2447c478bd9Sstevel@tonic-gate 	} else {
2457c478bd9Sstevel@tonic-gate 		size_t buflen;
2467c478bd9Sstevel@tonic-gate 		size_t copylen;
2477c478bd9Sstevel@tonic-gate 
2487c478bd9Sstevel@tonic-gate 		buflen = snprintf(zlogp->log, zlogp->loglen, "%s\n", buf);
2497c478bd9Sstevel@tonic-gate 		copylen = MIN(buflen, zlogp->loglen);
2507c478bd9Sstevel@tonic-gate 		zlogp->log += copylen;
2517c478bd9Sstevel@tonic-gate 		zlogp->loglen -= copylen;
2527c478bd9Sstevel@tonic-gate 	}
2537c478bd9Sstevel@tonic-gate }
2547c478bd9Sstevel@tonic-gate 
2553f2f09c1Sdp /*
2563f2f09c1Sdp  * Emit a warning for any boot arguments which are unrecognized.  Since
2573f2f09c1Sdp  * Solaris boot arguments are getopt(3c) compatible (see kernel(1m)), we
2583f2f09c1Sdp  * put the arguments into an argv style array, use getopt to process them,
2593f2f09c1Sdp  * and put the resultant argument string back into outargs.
2603f2f09c1Sdp  *
2613f2f09c1Sdp  * During the filtering, we pull out any arguments which are truly "boot"
2623f2f09c1Sdp  * arguments, leaving only those which are to be passed intact to the
2633f2f09c1Sdp  * progenitor process.  The one we support at the moment is -i, which
2643f2f09c1Sdp  * indicates to the kernel which program should be launched as 'init'.
2653f2f09c1Sdp  *
2663f2f09c1Sdp  * A return of Z_INVAL indicates specifically that the arguments are
2673f2f09c1Sdp  * not valid; this is a non-fatal error.  Except for Z_OK, all other return
2683f2f09c1Sdp  * values are treated as fatal.
2693f2f09c1Sdp  */
2703f2f09c1Sdp static int
2713f2f09c1Sdp filter_bootargs(zlog_t *zlogp, const char *inargs, char *outargs,
2723f2f09c1Sdp     char *init_file, char *badarg)
2733f2f09c1Sdp {
2743f2f09c1Sdp 	int argc = 0, argc_save;
2753f2f09c1Sdp 	int i;
2763f2f09c1Sdp 	int err;
2773f2f09c1Sdp 	char *arg, *lasts, **argv = NULL, **argv_save;
2783f2f09c1Sdp 	char zonecfg_args[BOOTARGS_MAX];
2793f2f09c1Sdp 	char scratchargs[BOOTARGS_MAX], *sargs;
2803f2f09c1Sdp 	char c;
2813f2f09c1Sdp 
2823f2f09c1Sdp 	bzero(outargs, BOOTARGS_MAX);
2833f2f09c1Sdp 	bzero(badarg, BOOTARGS_MAX);
2843f2f09c1Sdp 
2853f2f09c1Sdp 	/*
2863f2f09c1Sdp 	 * If the user didn't specify transient boot arguments, check
2873f2f09c1Sdp 	 * to see if there were any specified in the zone configuration,
2883f2f09c1Sdp 	 * and use them if applicable.
2893f2f09c1Sdp 	 */
2903f2f09c1Sdp 	if (inargs == NULL || inargs[0] == '\0')  {
2913f2f09c1Sdp 		zone_dochandle_t handle;
2923f2f09c1Sdp 		if ((handle = zonecfg_init_handle()) == NULL) {
2933f2f09c1Sdp 			zerror(zlogp, B_TRUE,
2943f2f09c1Sdp 			    "getting zone configuration handle");
2953f2f09c1Sdp 			return (Z_BAD_HANDLE);
2963f2f09c1Sdp 		}
2973f2f09c1Sdp 		err = zonecfg_get_snapshot_handle(zone_name, handle);
2983f2f09c1Sdp 		if (err != Z_OK) {
2993f2f09c1Sdp 			zerror(zlogp, B_FALSE,
3003f2f09c1Sdp 			    "invalid configuration snapshot");
3013f2f09c1Sdp 			zonecfg_fini_handle(handle);
3023f2f09c1Sdp 			return (Z_BAD_HANDLE);
3033f2f09c1Sdp 		}
3043f2f09c1Sdp 
3053f2f09c1Sdp 		bzero(zonecfg_args, sizeof (zonecfg_args));
3063f2f09c1Sdp 		(void) zonecfg_get_bootargs(handle, zonecfg_args,
3073f2f09c1Sdp 		    sizeof (zonecfg_args));
3083f2f09c1Sdp 		inargs = zonecfg_args;
3093f2f09c1Sdp 		zonecfg_fini_handle(handle);
3103f2f09c1Sdp 	}
3113f2f09c1Sdp 
3123f2f09c1Sdp 	if (strlen(inargs) >= BOOTARGS_MAX) {
3133f2f09c1Sdp 		zerror(zlogp, B_FALSE, "boot argument string too long");
3143f2f09c1Sdp 		return (Z_INVAL);
3153f2f09c1Sdp 	}
3163f2f09c1Sdp 
3173f2f09c1Sdp 	(void) strlcpy(scratchargs, inargs, sizeof (scratchargs));
3183f2f09c1Sdp 	sargs = scratchargs;
3193f2f09c1Sdp 	while ((arg = strtok_r(sargs, " \t", &lasts)) != NULL) {
3203f2f09c1Sdp 		sargs = NULL;
3213f2f09c1Sdp 		argc++;
3223f2f09c1Sdp 	}
3233f2f09c1Sdp 
3243f2f09c1Sdp 	if ((argv = calloc(argc + 1, sizeof (char *))) == NULL) {
3253f2f09c1Sdp 		zerror(zlogp, B_FALSE, "memory allocation failed");
3263f2f09c1Sdp 		return (Z_NOMEM);
3273f2f09c1Sdp 	}
3283f2f09c1Sdp 
3293f2f09c1Sdp 	argv_save = argv;
3303f2f09c1Sdp 	argc_save = argc;
3313f2f09c1Sdp 
3323f2f09c1Sdp 	(void) strlcpy(scratchargs, inargs, sizeof (scratchargs));
3333f2f09c1Sdp 	sargs = scratchargs;
3343f2f09c1Sdp 	i = 0;
3353f2f09c1Sdp 	while ((arg = strtok_r(sargs, " \t", &lasts)) != NULL) {
3363f2f09c1Sdp 		sargs = NULL;
3373f2f09c1Sdp 		if ((argv[i] = strdup(arg)) == NULL) {
3383f2f09c1Sdp 			err = Z_NOMEM;
3393f2f09c1Sdp 			zerror(zlogp, B_FALSE, "memory allocation failed");
3403f2f09c1Sdp 			goto done;
3413f2f09c1Sdp 		}
3423f2f09c1Sdp 		i++;
3433f2f09c1Sdp 	}
3443f2f09c1Sdp 
3453f2f09c1Sdp 	/*
3463f2f09c1Sdp 	 * We preserve compatibility with the Solaris system boot behavior,
3473f2f09c1Sdp 	 * which allows:
3483f2f09c1Sdp 	 *
3493f2f09c1Sdp 	 * 	# reboot kernel/unix -s -m verbose
3503f2f09c1Sdp 	 *
3513f2f09c1Sdp 	 * In this example, kernel/unix tells the booter what file to
3523f2f09c1Sdp 	 * boot.  We don't want reboot in a zone to be gratuitously different,
3533f2f09c1Sdp 	 * so we silently ignore the boot file, if necessary.
3543f2f09c1Sdp 	 */
3553f2f09c1Sdp 	if (argv[0] == NULL)
3563f2f09c1Sdp 		goto done;
3573f2f09c1Sdp 
3583f2f09c1Sdp 	assert(argv[0][0] != ' ');
3593f2f09c1Sdp 	assert(argv[0][0] != '\t');
3603f2f09c1Sdp 
3613f2f09c1Sdp 	if (argv[0][0] != '-' && argv[0][0] != '\0') {
3623f2f09c1Sdp 		argv = &argv[1];
3633f2f09c1Sdp 		argc--;
3643f2f09c1Sdp 	}
3653f2f09c1Sdp 
3663f2f09c1Sdp 	optind = 0;
3673f2f09c1Sdp 	opterr = 0;
3683f2f09c1Sdp 	err = Z_OK;
3699acbbeafSnn35248 	while ((c = getopt(argc, argv, "fi:m:s")) != -1) {
3703f2f09c1Sdp 		switch (c) {
3713f2f09c1Sdp 		case 'i':
3723f2f09c1Sdp 			/*
3733f2f09c1Sdp 			 * -i is handled by the runtime and is not passed
3743f2f09c1Sdp 			 * along to userland
3753f2f09c1Sdp 			 */
3763f2f09c1Sdp 			(void) strlcpy(init_file, optarg, MAXPATHLEN);
3773f2f09c1Sdp 			break;
3789acbbeafSnn35248 		case 'f':
3799acbbeafSnn35248 			/* This has already been processed by zoneadm */
3809acbbeafSnn35248 			break;
3813f2f09c1Sdp 		case 'm':
3823f2f09c1Sdp 		case 's':
3833f2f09c1Sdp 			/* These pass through unmolested */
3843f2f09c1Sdp 			(void) snprintf(outargs, BOOTARGS_MAX,
3853f2f09c1Sdp 			    "%s -%c %s ", outargs, c, optarg ? optarg : "");
3863f2f09c1Sdp 			break;
3873f2f09c1Sdp 		case '?':
3883f2f09c1Sdp 			/*
3893f2f09c1Sdp 			 * We warn about unknown arguments but pass them
3903f2f09c1Sdp 			 * along anyway-- if someone wants to develop their
3913f2f09c1Sdp 			 * own init replacement, they can pass it whatever
3923f2f09c1Sdp 			 * args they want.
3933f2f09c1Sdp 			 */
3943f2f09c1Sdp 			err = Z_INVAL;
3953f2f09c1Sdp 			(void) snprintf(outargs, BOOTARGS_MAX,
3963f2f09c1Sdp 			    "%s -%c", outargs, optopt);
3973f2f09c1Sdp 			(void) snprintf(badarg, BOOTARGS_MAX,
3983f2f09c1Sdp 			    "%s -%c", badarg, optopt);
3993f2f09c1Sdp 			break;
4003f2f09c1Sdp 		}
4013f2f09c1Sdp 	}
4023f2f09c1Sdp 
4033f2f09c1Sdp 	/*
4043f2f09c1Sdp 	 * For Solaris Zones we warn about and discard non-option arguments.
4053f2f09c1Sdp 	 * Hence 'boot foo bar baz gub' --> 'boot'.  However, to be similar
4063f2f09c1Sdp 	 * to the kernel, we concat up all the other remaining boot args.
4073f2f09c1Sdp 	 * and warn on them as a group.
4083f2f09c1Sdp 	 */
4093f2f09c1Sdp 	if (optind < argc) {
4103f2f09c1Sdp 		err = Z_INVAL;
4113f2f09c1Sdp 		while (optind < argc) {
4123f2f09c1Sdp 			(void) snprintf(badarg, BOOTARGS_MAX, "%s%s%s",
4133f2f09c1Sdp 			    badarg, strlen(badarg) > 0 ? " " : "",
4143f2f09c1Sdp 			    argv[optind]);
4153f2f09c1Sdp 			optind++;
4163f2f09c1Sdp 		}
4173f2f09c1Sdp 		zerror(zlogp, B_FALSE, "WARNING: Unused or invalid boot "
4183f2f09c1Sdp 		    "arguments `%s'.", badarg);
4193f2f09c1Sdp 	}
4203f2f09c1Sdp 
4213f2f09c1Sdp done:
4223f2f09c1Sdp 	for (i = 0; i < argc_save; i++) {
4233f2f09c1Sdp 		if (argv_save[i] != NULL)
4243f2f09c1Sdp 			free(argv_save[i]);
4253f2f09c1Sdp 	}
4263f2f09c1Sdp 	free(argv_save);
4273f2f09c1Sdp 	return (err);
4283f2f09c1Sdp }
4293f2f09c1Sdp 
4303f2f09c1Sdp 
4317c478bd9Sstevel@tonic-gate static int
4327c478bd9Sstevel@tonic-gate mkzonedir(zlog_t *zlogp)
4337c478bd9Sstevel@tonic-gate {
4347c478bd9Sstevel@tonic-gate 	struct stat st;
4357c478bd9Sstevel@tonic-gate 	/*
4367c478bd9Sstevel@tonic-gate 	 * We must create and lock everyone but root out of ZONES_TMPDIR
4377c478bd9Sstevel@tonic-gate 	 * since anyone can open any UNIX domain socket, regardless of
4387c478bd9Sstevel@tonic-gate 	 * its file system permissions.  Sigh...
4397c478bd9Sstevel@tonic-gate 	 */
4407c478bd9Sstevel@tonic-gate 	if (mkdir(ZONES_TMPDIR, S_IRWXU) < 0 && errno != EEXIST) {
4417c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "could not mkdir '%s'", ZONES_TMPDIR);
4427c478bd9Sstevel@tonic-gate 		return (-1);
4437c478bd9Sstevel@tonic-gate 	}
4447c478bd9Sstevel@tonic-gate 	/* paranoia */
4454bc0a2efScasper 	if ((stat(ZONES_TMPDIR, &st) < 0) || !S_ISDIR(st.st_mode)) {
4467c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "'%s' is not a directory", ZONES_TMPDIR);
4477c478bd9Sstevel@tonic-gate 		return (-1);
4487c478bd9Sstevel@tonic-gate 	}
4497c478bd9Sstevel@tonic-gate 	(void) chmod(ZONES_TMPDIR, S_IRWXU);
4507c478bd9Sstevel@tonic-gate 	return (0);
4517c478bd9Sstevel@tonic-gate }
4527c478bd9Sstevel@tonic-gate 
453108322fbScarlsonj /*
454c5cd6260S  * Run the brand's pre-state change callback, if it exists.
455c5cd6260S  */
456c5cd6260S static int
457c5cd6260S brand_prestatechg(zlog_t *zlogp, int state, int cmd)
458c5cd6260S {
459c5cd6260S 	char cmdbuf[2 * MAXPATHLEN];
460c5cd6260S 
461c5cd6260S 	if (pre_statechg_hook[0] == '\0')
462c5cd6260S 		return (0);
463c5cd6260S 
464c5cd6260S 	if (snprintf(cmdbuf, sizeof (cmdbuf), "%s %d %d", pre_statechg_hook,
465c5cd6260S 	    state, cmd) > sizeof (cmdbuf))
466c5cd6260S 		return (-1);
467c5cd6260S 
468c5cd6260S 	if (do_subproc(zlogp, cmdbuf, NULL) != 0)
469c5cd6260S 		return (-1);
470c5cd6260S 
471c5cd6260S 	return (0);
472c5cd6260S }
473c5cd6260S 
474c5cd6260S /*
475c5cd6260S  * Run the brand's post-state change callback, if it exists.
476c5cd6260S  */
477c5cd6260S static int
478c5cd6260S brand_poststatechg(zlog_t *zlogp, int state, int cmd)
479c5cd6260S {
480c5cd6260S 	char cmdbuf[2 * MAXPATHLEN];
481c5cd6260S 
482c5cd6260S 	if (post_statechg_hook[0] == '\0')
483c5cd6260S 		return (0);
484c5cd6260S 
485c5cd6260S 	if (snprintf(cmdbuf, sizeof (cmdbuf), "%s %d %d", post_statechg_hook,
486c5cd6260S 	    state, cmd) > sizeof (cmdbuf))
487c5cd6260S 		return (-1);
488c5cd6260S 
489c5cd6260S 	if (do_subproc(zlogp, cmdbuf, NULL) != 0)
490c5cd6260S 		return (-1);
491c5cd6260S 
492c5cd6260S 	return (0);
493c5cd6260S }
494c5cd6260S 
495c5cd6260S /*
496108322fbScarlsonj  * Bring a zone up to the pre-boot "ready" stage.  The mount_cmd argument is
497108322fbScarlsonj  * 'true' if this is being invoked as part of the processing for the "mount"
498108322fbScarlsonj  * subcommand.
499108322fbScarlsonj  */
500108322fbScarlsonj static int
501c5cd6260S zone_ready(zlog_t *zlogp, zone_mnt_t mount_cmd, int zstate)
5027c478bd9Sstevel@tonic-gate {
5037c478bd9Sstevel@tonic-gate 	int err;
5047c478bd9Sstevel@tonic-gate 
505c5cd6260S 	if (brand_prestatechg(zlogp, zstate, Z_READY) != 0)
506c5cd6260S 		return (-1);
507c5cd6260S 
5087c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_create_snapshot(zone_name)) != Z_OK) {
5097c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "unable to create snapshot: %s",
5107c478bd9Sstevel@tonic-gate 		    zonecfg_strerror(err));
5118fe9fdf2SDan Price 		goto bad;
5127c478bd9Sstevel@tonic-gate 	}
5137c478bd9Sstevel@tonic-gate 
514108322fbScarlsonj 	if ((zone_id = vplat_create(zlogp, mount_cmd)) == -1) {
5157c478bd9Sstevel@tonic-gate 		if ((err = zonecfg_destroy_snapshot(zone_name)) != Z_OK)
5167c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_FALSE, "destroying snapshot: %s",
5177c478bd9Sstevel@tonic-gate 			    zonecfg_strerror(err));
5188fe9fdf2SDan Price 		goto bad;
5197c478bd9Sstevel@tonic-gate 	}
520555afedfScarlsonj 	if (vplat_bringup(zlogp, mount_cmd, zone_id) != 0) {
5217c478bd9Sstevel@tonic-gate 		bringup_failure_recovery = B_TRUE;
5226cfd72c6Sgjelinek 		(void) vplat_teardown(NULL, (mount_cmd != Z_MNT_BOOT), B_FALSE);
5237c478bd9Sstevel@tonic-gate 		if ((err = zonecfg_destroy_snapshot(zone_name)) != Z_OK)
5247c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_FALSE, "destroying snapshot: %s",
5257c478bd9Sstevel@tonic-gate 			    zonecfg_strerror(err));
5268fe9fdf2SDan Price 		goto bad;
5277c478bd9Sstevel@tonic-gate 	}
5287c478bd9Sstevel@tonic-gate 
529c5cd6260S 	if (brand_poststatechg(zlogp, zstate, Z_READY) != 0)
5308fe9fdf2SDan Price 		goto bad;
531c5cd6260S 
5327c478bd9Sstevel@tonic-gate 	return (0);
5338fe9fdf2SDan Price 
5348fe9fdf2SDan Price bad:
5358fe9fdf2SDan Price 	/*
5368fe9fdf2SDan Price 	 * If something goes wrong, we up the zones's state to the target
5378fe9fdf2SDan Price 	 * state, READY, and then invoke the hook as if we're halting.
5388fe9fdf2SDan Price 	 */
5398fe9fdf2SDan Price 	(void) brand_poststatechg(zlogp, ZONE_STATE_READY, Z_HALT);
5408fe9fdf2SDan Price 	return (-1);
5417c478bd9Sstevel@tonic-gate }
5427c478bd9Sstevel@tonic-gate 
543555afedfScarlsonj int
544555afedfScarlsonj init_template(void)
5457c478bd9Sstevel@tonic-gate {
5467c478bd9Sstevel@tonic-gate 	int fd;
5477c478bd9Sstevel@tonic-gate 	int err = 0;
5487c478bd9Sstevel@tonic-gate 
5497c478bd9Sstevel@tonic-gate 	fd = open64(CTFS_ROOT "/process/template", O_RDWR);
5507c478bd9Sstevel@tonic-gate 	if (fd == -1)
5517c478bd9Sstevel@tonic-gate 		return (-1);
5527c478bd9Sstevel@tonic-gate 
5537c478bd9Sstevel@tonic-gate 	/*
5547c478bd9Sstevel@tonic-gate 	 * For now, zoneadmd doesn't do anything with the contract.
5557c478bd9Sstevel@tonic-gate 	 * Deliver no events, don't inherit, and allow it to be orphaned.
5567c478bd9Sstevel@tonic-gate 	 */
5577c478bd9Sstevel@tonic-gate 	err |= ct_tmpl_set_critical(fd, 0);
5587c478bd9Sstevel@tonic-gate 	err |= ct_tmpl_set_informative(fd, 0);
5597c478bd9Sstevel@tonic-gate 	err |= ct_pr_tmpl_set_fatal(fd, CT_PR_EV_HWERR);
5607c478bd9Sstevel@tonic-gate 	err |= ct_pr_tmpl_set_param(fd, CT_PR_PGRPONLY | CT_PR_REGENT);
5617c478bd9Sstevel@tonic-gate 	if (err || ct_tmpl_activate(fd)) {
5627c478bd9Sstevel@tonic-gate 		(void) close(fd);
5637c478bd9Sstevel@tonic-gate 		return (-1);
5647c478bd9Sstevel@tonic-gate 	}
5657c478bd9Sstevel@tonic-gate 
5667c478bd9Sstevel@tonic-gate 	return (fd);
5677c478bd9Sstevel@tonic-gate }
5687c478bd9Sstevel@tonic-gate 
5699acbbeafSnn35248 typedef struct fs_callback {
5709acbbeafSnn35248 	zlog_t		*zlogp;
5719acbbeafSnn35248 	zoneid_t	zoneid;
572910f48daSedp 	boolean_t	mount_cmd;
5739acbbeafSnn35248 } fs_callback_t;
5749acbbeafSnn35248 
5757c478bd9Sstevel@tonic-gate static int
5769acbbeafSnn35248 mount_early_fs(void *data, const char *spec, const char *dir,
5779acbbeafSnn35248     const char *fstype, const char *opt)
5787c478bd9Sstevel@tonic-gate {
5799acbbeafSnn35248 	zlog_t *zlogp = ((fs_callback_t *)data)->zlogp;
5809acbbeafSnn35248 	zoneid_t zoneid = ((fs_callback_t *)data)->zoneid;
581910f48daSedp 	boolean_t mount_cmd = ((fs_callback_t *)data)->mount_cmd;
582d314f035Sedp 	char rootpath[MAXPATHLEN];
5837c478bd9Sstevel@tonic-gate 	pid_t child;
5847c478bd9Sstevel@tonic-gate 	int child_status;
5857c478bd9Sstevel@tonic-gate 	int tmpl_fd;
586d314f035Sedp 	int rv;
5877c478bd9Sstevel@tonic-gate 	ctid_t ct;
5887c478bd9Sstevel@tonic-gate 
589910f48daSedp 	/* determine the zone rootpath */
590910f48daSedp 	if (mount_cmd) {
591910f48daSedp 		char zonepath[MAXPATHLEN];
592910f48daSedp 		char luroot[MAXPATHLEN];
593910f48daSedp 
594910f48daSedp 		if (zone_get_zonepath(zone_name,
595910f48daSedp 		    zonepath, sizeof (zonepath)) != Z_OK) {
596910f48daSedp 			zerror(zlogp, B_FALSE, "unable to determine zone path");
597910f48daSedp 			return (-1);
598910f48daSedp 		}
599910f48daSedp 
600910f48daSedp 		(void) snprintf(luroot, sizeof (luroot), "%s/lu", zonepath);
601910f48daSedp 		resolve_lofs(zlogp, luroot, sizeof (luroot));
602910f48daSedp 		(void) strlcpy(rootpath, luroot, sizeof (rootpath));
603910f48daSedp 	} else {
604910f48daSedp 		if (zone_get_rootpath(zone_name,
605910f48daSedp 		    rootpath, sizeof (rootpath)) != Z_OK) {
606d314f035Sedp 			zerror(zlogp, B_FALSE, "unable to determine zone root");
607d314f035Sedp 			return (-1);
608d314f035Sedp 		}
609910f48daSedp 	}
610d314f035Sedp 
611d314f035Sedp 	if ((rv = valid_mount_path(zlogp, rootpath, spec, dir, fstype)) < 0) {
612d314f035Sedp 		zerror(zlogp, B_FALSE, "%s%s is not a valid mount point",
613d314f035Sedp 		    rootpath, dir);
614d314f035Sedp 		return (-1);
615d314f035Sedp 	} else if (rv > 0) {
616d314f035Sedp 		/* The mount point path doesn't exist, create it now. */
617d314f035Sedp 		if (make_one_dir(zlogp, rootpath, dir,
618d314f035Sedp 		    DEFAULT_DIR_MODE, DEFAULT_DIR_USER,
619d314f035Sedp 		    DEFAULT_DIR_GROUP) != 0) {
620d314f035Sedp 			zerror(zlogp, B_FALSE, "failed to create mount point");
621d314f035Sedp 			return (-1);
622d314f035Sedp 		}
623d314f035Sedp 
624d314f035Sedp 		/*
625d314f035Sedp 		 * Now this might seem weird, but we need to invoke
626d314f035Sedp 		 * valid_mount_path() again.  Why?  Because it checks
627d314f035Sedp 		 * to make sure that the mount point path is canonical,
628d314f035Sedp 		 * which it can only do if the path exists, so now that
629d314f035Sedp 		 * we've created the path we have to verify it again.
630d314f035Sedp 		 */
631d314f035Sedp 		if ((rv = valid_mount_path(zlogp, rootpath, spec, dir,
632d314f035Sedp 		    fstype)) < 0) {
633d314f035Sedp 			zerror(zlogp, B_FALSE,
634d314f035Sedp 			    "%s%s is not a valid mount point", rootpath, dir);
635d314f035Sedp 			return (-1);
636d314f035Sedp 		}
637d314f035Sedp 	}
638d314f035Sedp 
6397c478bd9Sstevel@tonic-gate 	if ((tmpl_fd = init_template()) == -1) {
6407c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "failed to create contract");
6417c478bd9Sstevel@tonic-gate 		return (-1);
6427c478bd9Sstevel@tonic-gate 	}
6437c478bd9Sstevel@tonic-gate 
6447c478bd9Sstevel@tonic-gate 	if ((child = fork()) == -1) {
6457c478bd9Sstevel@tonic-gate 		(void) ct_tmpl_clear(tmpl_fd);
6467c478bd9Sstevel@tonic-gate 		(void) close(tmpl_fd);
6477c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "failed to fork");
6487c478bd9Sstevel@tonic-gate 		return (-1);
6497c478bd9Sstevel@tonic-gate 
6507c478bd9Sstevel@tonic-gate 	} else if (child == 0) {	/* child */
6519acbbeafSnn35248 		char opt_buf[MAX_MNTOPT_STR];
6529acbbeafSnn35248 		int optlen = 0;
6539acbbeafSnn35248 		int mflag = MS_DATA;
6549acbbeafSnn35248 
6557c478bd9Sstevel@tonic-gate 		(void) ct_tmpl_clear(tmpl_fd);
6567c478bd9Sstevel@tonic-gate 		/*
6577c478bd9Sstevel@tonic-gate 		 * Even though there are no procs running in the zone, we
6587c478bd9Sstevel@tonic-gate 		 * do this for paranoia's sake.
6597c478bd9Sstevel@tonic-gate 		 */
6607c478bd9Sstevel@tonic-gate 		(void) closefrom(0);
6617c478bd9Sstevel@tonic-gate 
6627c478bd9Sstevel@tonic-gate 		if (zone_enter(zoneid) == -1) {
6637c478bd9Sstevel@tonic-gate 			_exit(errno);
6647c478bd9Sstevel@tonic-gate 		}
6659acbbeafSnn35248 		if (opt != NULL) {
6669acbbeafSnn35248 			/*
6679acbbeafSnn35248 			 * The mount() system call is incredibly annoying.
6689acbbeafSnn35248 			 * If options are specified, we need to copy them
6699acbbeafSnn35248 			 * into a temporary buffer since the mount() system
6709acbbeafSnn35248 			 * call will overwrite the options string.  It will
6719acbbeafSnn35248 			 * also fail if the new option string it wants to
6729acbbeafSnn35248 			 * write is bigger than the one we passed in, so
6739acbbeafSnn35248 			 * you must pass in a buffer of the maximum possible
6749acbbeafSnn35248 			 * option string length.  sigh.
6759acbbeafSnn35248 			 */
6769acbbeafSnn35248 			(void) strlcpy(opt_buf, opt, sizeof (opt_buf));
6779acbbeafSnn35248 			opt = opt_buf;
6789acbbeafSnn35248 			optlen = MAX_MNTOPT_STR;
6799acbbeafSnn35248 			mflag = MS_OPTIONSTR;
6809acbbeafSnn35248 		}
6819acbbeafSnn35248 		if (mount(spec, dir, mflag, fstype, NULL, 0, opt, optlen) != 0)
6827c478bd9Sstevel@tonic-gate 			_exit(errno);
6837c478bd9Sstevel@tonic-gate 		_exit(0);
6847c478bd9Sstevel@tonic-gate 	}
6857c478bd9Sstevel@tonic-gate 
6867c478bd9Sstevel@tonic-gate 	/* parent */
6877c478bd9Sstevel@tonic-gate 	if (contract_latest(&ct) == -1)
6887c478bd9Sstevel@tonic-gate 		ct = -1;
6897c478bd9Sstevel@tonic-gate 	(void) ct_tmpl_clear(tmpl_fd);
6907c478bd9Sstevel@tonic-gate 	(void) close(tmpl_fd);
6917c478bd9Sstevel@tonic-gate 	if (waitpid(child, &child_status, 0) != child) {
6927c478bd9Sstevel@tonic-gate 		/* unexpected: we must have been signalled */
6937c478bd9Sstevel@tonic-gate 		(void) contract_abandon_id(ct);
6947c478bd9Sstevel@tonic-gate 		return (-1);
6957c478bd9Sstevel@tonic-gate 	}
6967c478bd9Sstevel@tonic-gate 	(void) contract_abandon_id(ct);
6977c478bd9Sstevel@tonic-gate 	if (WEXITSTATUS(child_status) != 0) {
6987c478bd9Sstevel@tonic-gate 		errno = WEXITSTATUS(child_status);
6997c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "mount of %s failed", dir);
7007c478bd9Sstevel@tonic-gate 		return (-1);
7017c478bd9Sstevel@tonic-gate 	}
7027c478bd9Sstevel@tonic-gate 
7037c478bd9Sstevel@tonic-gate 	return (0);
7047c478bd9Sstevel@tonic-gate }
7057c478bd9Sstevel@tonic-gate 
706c5cd6260S /*
707c5cd6260S  * If retstr is not NULL, the output of the subproc is returned in the str,
708c5cd6260S  * otherwise it is output using zerror().  Any memory allocated for retstr
709c5cd6260S  * should be freed by the caller.
710c5cd6260S  */
7119acbbeafSnn35248 int
712c5cd6260S do_subproc(zlog_t *zlogp, char *cmdbuf, char **retstr)
713108322fbScarlsonj {
714c5cd6260S 	char buf[1024];		/* arbitrary large amount */
715c5cd6260S 	char *inbuf;
7169acbbeafSnn35248 	FILE *file;
7179acbbeafSnn35248 	int status;
718c5cd6260S 	int rd_cnt;
719c5cd6260S 
720c5cd6260S 	if (retstr != NULL) {
721c5cd6260S 		if ((*retstr = malloc(1024)) == NULL) {
722c5cd6260S 			zerror(zlogp, B_FALSE, "out of memory");
723c5cd6260S 			return (-1);
724c5cd6260S 		}
725c5cd6260S 		inbuf = *retstr;
726c5cd6260S 		rd_cnt = 0;
727c5cd6260S 	} else {
728c5cd6260S 		inbuf = buf;
729c5cd6260S 	}
730108322fbScarlsonj 
7319acbbeafSnn35248 	file = popen(cmdbuf, "r");
7329acbbeafSnn35248 	if (file == NULL) {
7339acbbeafSnn35248 		zerror(zlogp, B_TRUE, "could not launch: %s", cmdbuf);
734108322fbScarlsonj 		return (-1);
7359acbbeafSnn35248 	}
736108322fbScarlsonj 
737c5cd6260S 	while (fgets(inbuf, 1024, file) != NULL) {
7381f314719S 		if (retstr == NULL) {
7391f314719S 			if (zlogp != &logsys)
7409acbbeafSnn35248 				zerror(zlogp, B_FALSE, "%s", inbuf);
741c5cd6260S 		} else {
742c5cd6260S 			char *p;
743c5cd6260S 
744c5cd6260S 			rd_cnt += 1024 - 1;
745c5cd6260S 			if ((p = realloc(*retstr, rd_cnt + 1024)) == NULL) {
746c5cd6260S 				zerror(zlogp, B_FALSE, "out of memory");
747c5cd6260S 				(void) pclose(file);
748c5cd6260S 				return (-1);
749c5cd6260S 			}
750c5cd6260S 
751c5cd6260S 			*retstr = p;
752c5cd6260S 			inbuf = *retstr + rd_cnt;
753c5cd6260S 		}
754c5cd6260S 	}
7559acbbeafSnn35248 	status = pclose(file);
7569acbbeafSnn35248 
7579acbbeafSnn35248 	if (WIFSIGNALED(status)) {
7589acbbeafSnn35248 		zerror(zlogp, B_FALSE, "%s unexpectedly terminated due to "
7599acbbeafSnn35248 		    "signal %d", cmdbuf, WTERMSIG(status));
7605518d15bSdp 		return (-1);
7619acbbeafSnn35248 	}
7629acbbeafSnn35248 	assert(WIFEXITED(status));
7639acbbeafSnn35248 	if (WEXITSTATUS(status) == ZEXIT_EXEC) {
7649acbbeafSnn35248 		zerror(zlogp, B_FALSE, "failed to exec %s", cmdbuf);
765108322fbScarlsonj 		return (-1);
7669acbbeafSnn35248 	}
7679acbbeafSnn35248 	return (WEXITSTATUS(status));
768108322fbScarlsonj }
769108322fbScarlsonj 
770108322fbScarlsonj static int
771c5cd6260S zone_bootup(zlog_t *zlogp, const char *bootargs, int zstate)
7727c478bd9Sstevel@tonic-gate {
7737c478bd9Sstevel@tonic-gate 	zoneid_t zoneid;
7747c478bd9Sstevel@tonic-gate 	struct stat st;
775ff17c8bfSgjelinek 	char zpath[MAXPATHLEN], initpath[MAXPATHLEN], init_file[MAXPATHLEN];
7763f2f09c1Sdp 	char nbootargs[BOOTARGS_MAX];
7779acbbeafSnn35248 	char cmdbuf[MAXPATHLEN];
7789acbbeafSnn35248 	fs_callback_t cb;
779123807fbSedp 	brand_handle_t bh;
7802b24ab6bSSebastien Roy 	zone_iptype_t iptype;
7812b24ab6bSSebastien Roy 	boolean_t links_loaded = B_FALSE;
7822b24ab6bSSebastien Roy 	dladm_status_t status;
7832b24ab6bSSebastien Roy 	char errmsg[DLADM_STRSIZE];
7843f2f09c1Sdp 	int err;
7857c478bd9Sstevel@tonic-gate 
786c5cd6260S 	if (brand_prestatechg(zlogp, zstate, Z_BOOT) != 0)
787c5cd6260S 		return (-1);
788c5cd6260S 
7897c478bd9Sstevel@tonic-gate 	if ((zoneid = getzoneidbyname(zone_name)) == -1) {
7907c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "unable to get zoneid");
7918fe9fdf2SDan Price 		goto bad;
7927c478bd9Sstevel@tonic-gate 	}
7937c478bd9Sstevel@tonic-gate 
7949acbbeafSnn35248 	cb.zlogp = zlogp;
7959acbbeafSnn35248 	cb.zoneid = zoneid;
796910f48daSedp 	cb.mount_cmd = B_FALSE;
7979acbbeafSnn35248 
7989acbbeafSnn35248 	/* Get a handle to the brand info for this zone */
799123807fbSedp 	if ((bh = brand_open(brand_name)) == NULL) {
8009acbbeafSnn35248 		zerror(zlogp, B_FALSE, "unable to determine zone brand");
8018fe9fdf2SDan Price 		goto bad;
8029acbbeafSnn35248 	}
8039acbbeafSnn35248 
8049acbbeafSnn35248 	/*
8059acbbeafSnn35248 	 * Get the list of filesystems to mount from the brand
8069acbbeafSnn35248 	 * configuration.  These mounts are done via a thread that will
8079acbbeafSnn35248 	 * enter the zone, so they are done from within the context of the
8089acbbeafSnn35248 	 * zone.
8099acbbeafSnn35248 	 */
810123807fbSedp 	if (brand_platform_iter_mounts(bh, mount_early_fs, &cb) != 0) {
8119acbbeafSnn35248 		zerror(zlogp, B_FALSE, "unable to mount filesystems");
812123807fbSedp 		brand_close(bh);
8138fe9fdf2SDan Price 		goto bad;
8149acbbeafSnn35248 	}
8159acbbeafSnn35248 
8169acbbeafSnn35248 	/*
8179acbbeafSnn35248 	 * Get the brand's boot callback if it exists.
8189acbbeafSnn35248 	 */
819ff17c8bfSgjelinek 	if (zone_get_zonepath(zone_name, zpath, sizeof (zpath)) != Z_OK) {
820ff17c8bfSgjelinek 		zerror(zlogp, B_FALSE, "unable to determine zone path");
821e767a340Sgjelinek 		brand_close(bh);
8228fe9fdf2SDan Price 		goto bad;
8239acbbeafSnn35248 	}
8249acbbeafSnn35248 	(void) strcpy(cmdbuf, EXEC_PREFIX);
825ff17c8bfSgjelinek 	if (brand_get_boot(bh, zone_name, zpath, cmdbuf + EXEC_LEN,
826ff17c8bfSgjelinek 	    sizeof (cmdbuf) - EXEC_LEN) != 0) {
8279acbbeafSnn35248 		zerror(zlogp, B_FALSE,
8289acbbeafSnn35248 		    "unable to determine branded zone's boot callback");
829123807fbSedp 		brand_close(bh);
8308fe9fdf2SDan Price 		goto bad;
8319acbbeafSnn35248 	}
8329acbbeafSnn35248 
8339acbbeafSnn35248 	/* Get the path for this zone's init(1M) (or equivalent) process.  */
834123807fbSedp 	if (brand_get_initname(bh, init_file, MAXPATHLEN) != 0) {
8359acbbeafSnn35248 		zerror(zlogp, B_FALSE,
8369acbbeafSnn35248 		    "unable to determine zone's init(1M) location");
837123807fbSedp 		brand_close(bh);
8388fe9fdf2SDan Price 		goto bad;
8399acbbeafSnn35248 	}
8409acbbeafSnn35248 
841123807fbSedp 	brand_close(bh);
8427c478bd9Sstevel@tonic-gate 
8433f2f09c1Sdp 	err = filter_bootargs(zlogp, bootargs, nbootargs, init_file,
8443f2f09c1Sdp 	    bad_boot_arg);
8453f2f09c1Sdp 	if (err == Z_INVAL)
8463f2f09c1Sdp 		eventstream_write(Z_EVT_ZONE_BADARGS);
8473f2f09c1Sdp 	else if (err != Z_OK)
8488fe9fdf2SDan Price 		goto bad;
8493f2f09c1Sdp 
8503f2f09c1Sdp 	assert(init_file[0] != '\0');
8513f2f09c1Sdp 
8529acbbeafSnn35248 	/* Try to anticipate possible problems: Make sure init is executable. */
853ff17c8bfSgjelinek 	if (zone_get_rootpath(zone_name, zpath, sizeof (zpath)) != Z_OK) {
8547c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "unable to determine zone root");
8558fe9fdf2SDan Price 		goto bad;
8567c478bd9Sstevel@tonic-gate 	}
8579acbbeafSnn35248 
858ff17c8bfSgjelinek 	(void) snprintf(initpath, sizeof (initpath), "%s%s", zpath, init_file);
8597c478bd9Sstevel@tonic-gate 
8607c478bd9Sstevel@tonic-gate 	if (stat(initpath, &st) == -1) {
8617c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "could not stat %s", initpath);
8628fe9fdf2SDan Price 		goto bad;
8637c478bd9Sstevel@tonic-gate 	}
8647c478bd9Sstevel@tonic-gate 
8657c478bd9Sstevel@tonic-gate 	if ((st.st_mode & S_IXUSR) == 0) {
8667c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "%s is not executable", initpath);
8678fe9fdf2SDan Price 		goto bad;
8687c478bd9Sstevel@tonic-gate 	}
8697c478bd9Sstevel@tonic-gate 
8709acbbeafSnn35248 	/*
8712b24ab6bSSebastien Roy 	 * Exclusive stack zones interact with the dlmgmtd running in the
8722b24ab6bSSebastien Roy 	 * global zone.  dladm_zone_boot() tells dlmgmtd that this zone is
8732b24ab6bSSebastien Roy 	 * booting, and loads its datalinks from the zone's datalink
8742b24ab6bSSebastien Roy 	 * configuration file.
8752b24ab6bSSebastien Roy 	 */
8762b24ab6bSSebastien Roy 	if (vplat_get_iptype(zlogp, &iptype) == 0 && iptype == ZS_EXCLUSIVE) {
8772b24ab6bSSebastien Roy 		status = dladm_zone_boot(dld_handle, zoneid);
8782b24ab6bSSebastien Roy 		if (status != DLADM_STATUS_OK) {
8792b24ab6bSSebastien Roy 			zerror(zlogp, B_FALSE, "unable to load zone datalinks: "
8802b24ab6bSSebastien Roy 			    " %s", dladm_status2str(status, errmsg));
8812b24ab6bSSebastien Roy 			goto bad;
8822b24ab6bSSebastien Roy 		}
8832b24ab6bSSebastien Roy 		links_loaded = B_TRUE;
8842b24ab6bSSebastien Roy 	}
8852b24ab6bSSebastien Roy 
8862b24ab6bSSebastien Roy 	/*
8879acbbeafSnn35248 	 * If there is a brand 'boot' callback, execute it now to give the
8889acbbeafSnn35248 	 * brand one last chance to do any additional setup before the zone
8899acbbeafSnn35248 	 * is booted.
8909acbbeafSnn35248 	 */
8919acbbeafSnn35248 	if ((strlen(cmdbuf) > EXEC_LEN) &&
892c5cd6260S 	    (do_subproc(zlogp, cmdbuf, NULL) != Z_OK)) {
8939acbbeafSnn35248 		zerror(zlogp, B_FALSE, "%s failed", cmdbuf);
8948fe9fdf2SDan Price 		goto bad;
8959acbbeafSnn35248 	}
8969acbbeafSnn35248 
8973f2f09c1Sdp 	if (zone_setattr(zoneid, ZONE_ATTR_INITNAME, init_file, 0) == -1) {
8983f2f09c1Sdp 		zerror(zlogp, B_TRUE, "could not set zone boot file");
8998fe9fdf2SDan Price 		goto bad;
9003f2f09c1Sdp 	}
9013f2f09c1Sdp 
9023f2f09c1Sdp 	if (zone_setattr(zoneid, ZONE_ATTR_BOOTARGS, nbootargs, 0) == -1) {
9033f2f09c1Sdp 		zerror(zlogp, B_TRUE, "could not set zone boot arguments");
9048fe9fdf2SDan Price 		goto bad;
9053f2f09c1Sdp 	}
9063f2f09c1Sdp 
9073f2f09c1Sdp 	if (zone_boot(zoneid) == -1) {
9087c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "unable to boot zone");
9098fe9fdf2SDan Price 		goto bad;
9107c478bd9Sstevel@tonic-gate 	}
9117c478bd9Sstevel@tonic-gate 
912c5cd6260S 	if (brand_poststatechg(zlogp, zstate, Z_BOOT) != 0)
9138fe9fdf2SDan Price 		goto bad;
914c5cd6260S 
9157c478bd9Sstevel@tonic-gate 	return (0);
9168fe9fdf2SDan Price 
9178fe9fdf2SDan Price bad:
9188fe9fdf2SDan Price 	/*
9198fe9fdf2SDan Price 	 * If something goes wrong, we up the zones's state to the target
9208fe9fdf2SDan Price 	 * state, RUNNING, and then invoke the hook as if we're halting.
9218fe9fdf2SDan Price 	 */
9228fe9fdf2SDan Price 	(void) brand_poststatechg(zlogp, ZONE_STATE_RUNNING, Z_HALT);
9232b24ab6bSSebastien Roy 	if (links_loaded)
9242b24ab6bSSebastien Roy 		(void) dladm_zone_halt(dld_handle, zoneid);
9258fe9fdf2SDan Price 	return (-1);
9267c478bd9Sstevel@tonic-gate }
9277c478bd9Sstevel@tonic-gate 
9287c478bd9Sstevel@tonic-gate static int
929c5cd6260S zone_halt(zlog_t *zlogp, boolean_t unmount_cmd, boolean_t rebooting, int zstate)
9307c478bd9Sstevel@tonic-gate {
9317c478bd9Sstevel@tonic-gate 	int err;
9327c478bd9Sstevel@tonic-gate 
933c5cd6260S 	if (brand_prestatechg(zlogp, zstate, Z_HALT) != 0)
934c5cd6260S 		return (-1);
935c5cd6260S 
9360209230bSgjelinek 	if (vplat_teardown(zlogp, unmount_cmd, rebooting) != 0) {
9377c478bd9Sstevel@tonic-gate 		if (!bringup_failure_recovery)
9387c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_FALSE, "unable to destroy zone");
9397c478bd9Sstevel@tonic-gate 		return (-1);
9407c478bd9Sstevel@tonic-gate 	}
9417c478bd9Sstevel@tonic-gate 
9427c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_destroy_snapshot(zone_name)) != Z_OK)
9437c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "destroying snapshot: %s",
9447c478bd9Sstevel@tonic-gate 		    zonecfg_strerror(err));
9457c478bd9Sstevel@tonic-gate 
946c5cd6260S 	if (brand_poststatechg(zlogp, zstate, Z_HALT) != 0)
947c5cd6260S 		return (-1);
948c5cd6260S 
9497c478bd9Sstevel@tonic-gate 	return (0);
9507c478bd9Sstevel@tonic-gate }
9517c478bd9Sstevel@tonic-gate 
9527c478bd9Sstevel@tonic-gate /*
9537c478bd9Sstevel@tonic-gate  * Generate AUE_zone_state for a command that boots a zone.
9547c478bd9Sstevel@tonic-gate  */
9557c478bd9Sstevel@tonic-gate static void
9567c478bd9Sstevel@tonic-gate audit_put_record(zlog_t *zlogp, ucred_t *uc, int return_val,
9577c478bd9Sstevel@tonic-gate     char *new_state)
9587c478bd9Sstevel@tonic-gate {
9597c478bd9Sstevel@tonic-gate 	adt_session_data_t	*ah;
9607c478bd9Sstevel@tonic-gate 	adt_event_data_t	*event;
9617c478bd9Sstevel@tonic-gate 	int			pass_fail, fail_reason;
9627c478bd9Sstevel@tonic-gate 
9637c478bd9Sstevel@tonic-gate 	if (!adt_audit_enabled())
9647c478bd9Sstevel@tonic-gate 		return;
9657c478bd9Sstevel@tonic-gate 
9667c478bd9Sstevel@tonic-gate 	if (return_val == 0) {
9677c478bd9Sstevel@tonic-gate 		pass_fail = ADT_SUCCESS;
9687c478bd9Sstevel@tonic-gate 		fail_reason = ADT_SUCCESS;
9697c478bd9Sstevel@tonic-gate 	} else {
9707c478bd9Sstevel@tonic-gate 		pass_fail = ADT_FAILURE;
9717c478bd9Sstevel@tonic-gate 		fail_reason = ADT_FAIL_VALUE_PROGRAM;
9727c478bd9Sstevel@tonic-gate 	}
9737c478bd9Sstevel@tonic-gate 
9747c478bd9Sstevel@tonic-gate 	if (adt_start_session(&ah, NULL, 0)) {
9757c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, gettext("audit failure."));
9767c478bd9Sstevel@tonic-gate 		return;
9777c478bd9Sstevel@tonic-gate 	}
9787c478bd9Sstevel@tonic-gate 	if (adt_set_from_ucred(ah, uc, ADT_NEW)) {
9797c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, gettext("audit failure."));
9807c478bd9Sstevel@tonic-gate 		(void) adt_end_session(ah);
9817c478bd9Sstevel@tonic-gate 		return;
9827c478bd9Sstevel@tonic-gate 	}
9837c478bd9Sstevel@tonic-gate 
9847c478bd9Sstevel@tonic-gate 	event = adt_alloc_event(ah, ADT_zone_state);
9857c478bd9Sstevel@tonic-gate 	if (event == NULL) {
9867c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, gettext("audit failure."));
9877c478bd9Sstevel@tonic-gate 		(void) adt_end_session(ah);
9887c478bd9Sstevel@tonic-gate 		return;
9897c478bd9Sstevel@tonic-gate 	}
9907c478bd9Sstevel@tonic-gate 	event->adt_zone_state.zonename = zone_name;
9917c478bd9Sstevel@tonic-gate 	event->adt_zone_state.new_state = new_state;
9927c478bd9Sstevel@tonic-gate 
9937c478bd9Sstevel@tonic-gate 	if (adt_put_event(event, pass_fail, fail_reason))
9947c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, gettext("audit failure."));
9957c478bd9Sstevel@tonic-gate 
9967c478bd9Sstevel@tonic-gate 	adt_free_event(event);
9977c478bd9Sstevel@tonic-gate 
9987c478bd9Sstevel@tonic-gate 	(void) adt_end_session(ah);
9997c478bd9Sstevel@tonic-gate }
10007c478bd9Sstevel@tonic-gate 
10017c478bd9Sstevel@tonic-gate /*
10027c478bd9Sstevel@tonic-gate  * The main routine for the door server that deals with zone state transitions.
10037c478bd9Sstevel@tonic-gate  */
10047c478bd9Sstevel@tonic-gate /* ARGSUSED */
10057c478bd9Sstevel@tonic-gate static void
10067c478bd9Sstevel@tonic-gate server(void *cookie, char *args, size_t alen, door_desc_t *dp,
10077c478bd9Sstevel@tonic-gate     uint_t n_desc)
10087c478bd9Sstevel@tonic-gate {
10097c478bd9Sstevel@tonic-gate 	ucred_t *uc = NULL;
10107c478bd9Sstevel@tonic-gate 	const priv_set_t *eset;
10117c478bd9Sstevel@tonic-gate 
10127c478bd9Sstevel@tonic-gate 	zone_state_t zstate;
10137c478bd9Sstevel@tonic-gate 	zone_cmd_t cmd;
10147c478bd9Sstevel@tonic-gate 	zone_cmd_arg_t *zargp;
10157c478bd9Sstevel@tonic-gate 
10167c478bd9Sstevel@tonic-gate 	boolean_t kernelcall;
10177c478bd9Sstevel@tonic-gate 
10187c478bd9Sstevel@tonic-gate 	int rval = -1;
10197c478bd9Sstevel@tonic-gate 	uint64_t uniqid;
10207c478bd9Sstevel@tonic-gate 	zoneid_t zoneid = -1;
10217c478bd9Sstevel@tonic-gate 	zlog_t zlog;
10227c478bd9Sstevel@tonic-gate 	zlog_t *zlogp;
10237c478bd9Sstevel@tonic-gate 	zone_cmd_rval_t *rvalp;
10247c478bd9Sstevel@tonic-gate 	size_t rlen = getpagesize(); /* conservative */
10259acbbeafSnn35248 	fs_callback_t cb;
1026123807fbSedp 	brand_handle_t bh;
10277c478bd9Sstevel@tonic-gate 
10287c478bd9Sstevel@tonic-gate 	/* LINTED E_BAD_PTR_CAST_ALIGN */
10297c478bd9Sstevel@tonic-gate 	zargp = (zone_cmd_arg_t *)args;
10307c478bd9Sstevel@tonic-gate 
10317c478bd9Sstevel@tonic-gate 	/*
10327c478bd9Sstevel@tonic-gate 	 * When we get the door unref message, we've fdetach'd the door, and
10337c478bd9Sstevel@tonic-gate 	 * it is time for us to shut down zoneadmd.
10347c478bd9Sstevel@tonic-gate 	 */
10357c478bd9Sstevel@tonic-gate 	if (zargp == DOOR_UNREF_DATA) {
10367c478bd9Sstevel@tonic-gate 		/*
10377c478bd9Sstevel@tonic-gate 		 * See comment at end of main() for info on the last rites.
10387c478bd9Sstevel@tonic-gate 		 */
10397c478bd9Sstevel@tonic-gate 		exit(0);
10407c478bd9Sstevel@tonic-gate 	}
10417c478bd9Sstevel@tonic-gate 
10427c478bd9Sstevel@tonic-gate 	if (zargp == NULL) {
10437c478bd9Sstevel@tonic-gate 		(void) door_return(NULL, 0, 0, 0);
10447c478bd9Sstevel@tonic-gate 	}
10457c478bd9Sstevel@tonic-gate 
10467c478bd9Sstevel@tonic-gate 	rvalp = alloca(rlen);
10477c478bd9Sstevel@tonic-gate 	bzero(rvalp, rlen);
10487c478bd9Sstevel@tonic-gate 	zlog.logfile = NULL;
10497c478bd9Sstevel@tonic-gate 	zlog.buflen = zlog.loglen = rlen - sizeof (zone_cmd_rval_t) + 1;
10507c478bd9Sstevel@tonic-gate 	zlog.buf = rvalp->errbuf;
10517c478bd9Sstevel@tonic-gate 	zlog.log = zlog.buf;
10527c478bd9Sstevel@tonic-gate 	/* defer initialization of zlog.locale until after credential check */
10537c478bd9Sstevel@tonic-gate 	zlogp = &zlog;
10547c478bd9Sstevel@tonic-gate 
10557c478bd9Sstevel@tonic-gate 	if (alen != sizeof (zone_cmd_arg_t)) {
10567c478bd9Sstevel@tonic-gate 		/*
10577c478bd9Sstevel@tonic-gate 		 * This really shouldn't be happening.
10587c478bd9Sstevel@tonic-gate 		 */
10593f2f09c1Sdp 		zerror(&logsys, B_FALSE, "argument size (%d bytes) "
10603f2f09c1Sdp 		    "unexpected (expected %d bytes)", alen,
10613f2f09c1Sdp 		    sizeof (zone_cmd_arg_t));
10627c478bd9Sstevel@tonic-gate 		goto out;
10637c478bd9Sstevel@tonic-gate 	}
10647c478bd9Sstevel@tonic-gate 	cmd = zargp->cmd;
10657c478bd9Sstevel@tonic-gate 
10667c478bd9Sstevel@tonic-gate 	if (door_ucred(&uc) != 0) {
10677c478bd9Sstevel@tonic-gate 		zerror(&logsys, B_TRUE, "door_ucred");
10687c478bd9Sstevel@tonic-gate 		goto out;
10697c478bd9Sstevel@tonic-gate 	}
10707c478bd9Sstevel@tonic-gate 	eset = ucred_getprivset(uc, PRIV_EFFECTIVE);
10717c478bd9Sstevel@tonic-gate 	if (ucred_getzoneid(uc) != GLOBAL_ZONEID ||
10727c478bd9Sstevel@tonic-gate 	    (eset != NULL ? !priv_ismember(eset, PRIV_SYS_CONFIG) :
10737c478bd9Sstevel@tonic-gate 	    ucred_geteuid(uc) != 0)) {
10747c478bd9Sstevel@tonic-gate 		zerror(&logsys, B_FALSE, "insufficient privileges");
10757c478bd9Sstevel@tonic-gate 		goto out;
10767c478bd9Sstevel@tonic-gate 	}
10777c478bd9Sstevel@tonic-gate 
10787c478bd9Sstevel@tonic-gate 	kernelcall = ucred_getpid(uc) == 0;
10797c478bd9Sstevel@tonic-gate 
10807c478bd9Sstevel@tonic-gate 	/*
10817c478bd9Sstevel@tonic-gate 	 * This is safe because we only use a zlog_t throughout the
10827c478bd9Sstevel@tonic-gate 	 * duration of a door call; i.e., by the time the pointer
10837c478bd9Sstevel@tonic-gate 	 * might become invalid, the door call would be over.
10847c478bd9Sstevel@tonic-gate 	 */
10857c478bd9Sstevel@tonic-gate 	zlog.locale = kernelcall ? DEFAULT_LOCALE : zargp->locale;
10867c478bd9Sstevel@tonic-gate 
10877c478bd9Sstevel@tonic-gate 	(void) mutex_lock(&lock);
10887c478bd9Sstevel@tonic-gate 
10897c478bd9Sstevel@tonic-gate 	/*
10907c478bd9Sstevel@tonic-gate 	 * Once we start to really die off, we don't want more connections.
10917c478bd9Sstevel@tonic-gate 	 */
10927c478bd9Sstevel@tonic-gate 	if (in_death_throes) {
10937c478bd9Sstevel@tonic-gate 		(void) mutex_unlock(&lock);
10947c478bd9Sstevel@tonic-gate 		ucred_free(uc);
10957c478bd9Sstevel@tonic-gate 		(void) door_return(NULL, 0, 0, 0);
10967c478bd9Sstevel@tonic-gate 		thr_exit(NULL);
10977c478bd9Sstevel@tonic-gate 	}
10987c478bd9Sstevel@tonic-gate 
10997c478bd9Sstevel@tonic-gate 	/*
11007c478bd9Sstevel@tonic-gate 	 * Check for validity of command.
11017c478bd9Sstevel@tonic-gate 	 */
11029acbbeafSnn35248 	if (cmd != Z_READY && cmd != Z_BOOT && cmd != Z_FORCEBOOT &&
11039acbbeafSnn35248 	    cmd != Z_REBOOT && cmd != Z_HALT && cmd != Z_NOTE_UNINSTALLING &&
11049acbbeafSnn35248 	    cmd != Z_MOUNT && cmd != Z_FORCEMOUNT && cmd != Z_UNMOUNT) {
1105108322fbScarlsonj 		zerror(&logsys, B_FALSE, "invalid command %d", (int)cmd);
11067c478bd9Sstevel@tonic-gate 		goto out;
11077c478bd9Sstevel@tonic-gate 	}
11087c478bd9Sstevel@tonic-gate 
11097c478bd9Sstevel@tonic-gate 	if (kernelcall && (cmd != Z_HALT && cmd != Z_REBOOT)) {
11107c478bd9Sstevel@tonic-gate 		/*
11117c478bd9Sstevel@tonic-gate 		 * Can't happen
11127c478bd9Sstevel@tonic-gate 		 */
11137c478bd9Sstevel@tonic-gate 		zerror(&logsys, B_FALSE, "received unexpected kernel upcall %d",
11147c478bd9Sstevel@tonic-gate 		    cmd);
11157c478bd9Sstevel@tonic-gate 		goto out;
11167c478bd9Sstevel@tonic-gate 	}
11177c478bd9Sstevel@tonic-gate 	/*
11187c478bd9Sstevel@tonic-gate 	 * We ignore the possibility of someone calling zone_create(2)
11197c478bd9Sstevel@tonic-gate 	 * explicitly; all requests must come through zoneadmd.
11207c478bd9Sstevel@tonic-gate 	 */
11217c478bd9Sstevel@tonic-gate 	if (zone_get_state(zone_name, &zstate) != Z_OK) {
11227c478bd9Sstevel@tonic-gate 		/*
11237c478bd9Sstevel@tonic-gate 		 * Something terribly wrong happened
11247c478bd9Sstevel@tonic-gate 		 */
11257c478bd9Sstevel@tonic-gate 		zerror(&logsys, B_FALSE, "unable to determine state of zone");
11267c478bd9Sstevel@tonic-gate 		goto out;
11277c478bd9Sstevel@tonic-gate 	}
11287c478bd9Sstevel@tonic-gate 
11297c478bd9Sstevel@tonic-gate 	if (kernelcall) {
11307c478bd9Sstevel@tonic-gate 		/*
11317c478bd9Sstevel@tonic-gate 		 * Kernel-initiated requests may lose their validity if the
11327c478bd9Sstevel@tonic-gate 		 * zone_t the kernel was referring to has gone away.
11337c478bd9Sstevel@tonic-gate 		 */
11347c478bd9Sstevel@tonic-gate 		if ((zoneid = getzoneidbyname(zone_name)) == -1 ||
11357c478bd9Sstevel@tonic-gate 		    zone_getattr(zoneid, ZONE_ATTR_UNIQID, &uniqid,
11367c478bd9Sstevel@tonic-gate 		    sizeof (uniqid)) == -1 || uniqid != zargp->uniqid) {
11377c478bd9Sstevel@tonic-gate 			/*
11387c478bd9Sstevel@tonic-gate 			 * We're not talking about the same zone. The request
11397c478bd9Sstevel@tonic-gate 			 * must have arrived too late.  Return error.
11407c478bd9Sstevel@tonic-gate 			 */
11417c478bd9Sstevel@tonic-gate 			rval = -1;
11427c478bd9Sstevel@tonic-gate 			goto out;
11437c478bd9Sstevel@tonic-gate 		}
11447c478bd9Sstevel@tonic-gate 		zlogp = &logsys;	/* Log errors to syslog */
11457c478bd9Sstevel@tonic-gate 	}
11467c478bd9Sstevel@tonic-gate 
11479acbbeafSnn35248 	/*
11489acbbeafSnn35248 	 * If we are being asked to forcibly mount or boot a zone, we
11499acbbeafSnn35248 	 * pretend that an INCOMPLETE zone is actually INSTALLED.
11509acbbeafSnn35248 	 */
11519acbbeafSnn35248 	if (zstate == ZONE_STATE_INCOMPLETE &&
11529acbbeafSnn35248 	    (cmd == Z_FORCEBOOT || cmd == Z_FORCEMOUNT))
11539acbbeafSnn35248 		zstate = ZONE_STATE_INSTALLED;
11549acbbeafSnn35248 
11557c478bd9Sstevel@tonic-gate 	switch (zstate) {
11567c478bd9Sstevel@tonic-gate 	case ZONE_STATE_CONFIGURED:
11577c478bd9Sstevel@tonic-gate 	case ZONE_STATE_INCOMPLETE:
11587c478bd9Sstevel@tonic-gate 		/*
11597c478bd9Sstevel@tonic-gate 		 * Not our area of expertise; we just print a nice message
11607c478bd9Sstevel@tonic-gate 		 * and die off.
11617c478bd9Sstevel@tonic-gate 		 */
11627c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE,
11637c478bd9Sstevel@tonic-gate 		    "%s operation is invalid for zones in state '%s'",
1164108322fbScarlsonj 		    z_cmd_name(cmd), zone_state_str(zstate));
11657c478bd9Sstevel@tonic-gate 		break;
11667c478bd9Sstevel@tonic-gate 
11677c478bd9Sstevel@tonic-gate 	case ZONE_STATE_INSTALLED:
11687c478bd9Sstevel@tonic-gate 		switch (cmd) {
11697c478bd9Sstevel@tonic-gate 		case Z_READY:
1170c5cd6260S 			rval = zone_ready(zlogp, Z_MNT_BOOT, zstate);
11717c478bd9Sstevel@tonic-gate 			if (rval == 0)
11727c478bd9Sstevel@tonic-gate 				eventstream_write(Z_EVT_ZONE_READIED);
11737c478bd9Sstevel@tonic-gate 			break;
11747c478bd9Sstevel@tonic-gate 		case Z_BOOT:
11759acbbeafSnn35248 		case Z_FORCEBOOT:
11767c478bd9Sstevel@tonic-gate 			eventstream_write(Z_EVT_ZONE_BOOTING);
1177c5cd6260S 			if ((rval = zone_ready(zlogp, Z_MNT_BOOT, zstate))
1178c5cd6260S 			    == 0) {
1179c5cd6260S 				rval = zone_bootup(zlogp, zargp->bootbuf,
1180c5cd6260S 				    zstate);
1181c5cd6260S 			}
11827c478bd9Sstevel@tonic-gate 			audit_put_record(zlogp, uc, rval, "boot");
11837c478bd9Sstevel@tonic-gate 			if (rval != 0) {
11847c478bd9Sstevel@tonic-gate 				bringup_failure_recovery = B_TRUE;
1185c5cd6260S 				(void) zone_halt(zlogp, B_FALSE, B_FALSE,
1186c5cd6260S 				    zstate);
1187ffbafc53Scomay 				eventstream_write(Z_EVT_ZONE_BOOTFAILED);
11887c478bd9Sstevel@tonic-gate 			}
11897c478bd9Sstevel@tonic-gate 			break;
11907c478bd9Sstevel@tonic-gate 		case Z_HALT:
11917c478bd9Sstevel@tonic-gate 			if (kernelcall)	/* Invalid; can't happen */
11927c478bd9Sstevel@tonic-gate 				abort();
11937c478bd9Sstevel@tonic-gate 			/*
11947c478bd9Sstevel@tonic-gate 			 * We could have two clients racing to halt this
11957c478bd9Sstevel@tonic-gate 			 * zone; the second client loses, but his request
11967c478bd9Sstevel@tonic-gate 			 * doesn't fail, since the zone is now in the desired
11977c478bd9Sstevel@tonic-gate 			 * state.
11987c478bd9Sstevel@tonic-gate 			 */
11997c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_FALSE, "zone is already halted");
12007c478bd9Sstevel@tonic-gate 			rval = 0;
12017c478bd9Sstevel@tonic-gate 			break;
12027c478bd9Sstevel@tonic-gate 		case Z_REBOOT:
12037c478bd9Sstevel@tonic-gate 			if (kernelcall)	/* Invalid; can't happen */
12047c478bd9Sstevel@tonic-gate 				abort();
12057c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_FALSE, "%s operation is invalid "
1206108322fbScarlsonj 			    "for zones in state '%s'", z_cmd_name(cmd),
12077c478bd9Sstevel@tonic-gate 			    zone_state_str(zstate));
12087c478bd9Sstevel@tonic-gate 			rval = -1;
12097c478bd9Sstevel@tonic-gate 			break;
12107c478bd9Sstevel@tonic-gate 		case Z_NOTE_UNINSTALLING:
12117c478bd9Sstevel@tonic-gate 			if (kernelcall)	/* Invalid; can't happen */
12127c478bd9Sstevel@tonic-gate 				abort();
12137c478bd9Sstevel@tonic-gate 			/*
12147c478bd9Sstevel@tonic-gate 			 * Tell the console to print out a message about this.
12157c478bd9Sstevel@tonic-gate 			 * Once it does, we will be in_death_throes.
12167c478bd9Sstevel@tonic-gate 			 */
12177c478bd9Sstevel@tonic-gate 			eventstream_write(Z_EVT_ZONE_UNINSTALLING);
12187c478bd9Sstevel@tonic-gate 			break;
1219108322fbScarlsonj 		case Z_MOUNT:
12209acbbeafSnn35248 		case Z_FORCEMOUNT:
1221108322fbScarlsonj 			if (kernelcall)	/* Invalid; can't happen */
1222108322fbScarlsonj 				abort();
12239a5d73e0SRic Aleshire 			if (!zone_isnative && !zone_iscluster &&
12249a5d73e0SRic Aleshire 			    !zone_islabeled) {
12250679f794S 				/*
12260679f794S 				 * -U mounts the zone without lofs mounting
12270679f794S 				 * zone file systems back into the scratch
12280679f794S 				 * zone.  This is required when mounting
12290679f794S 				 * non-native branded zones.
12300679f794S 				 */
12310679f794S 				(void) strlcpy(zargp->bootbuf, "-U",
12320679f794S 				    BOOTARGS_MAX);
1233ffbafc53Scomay 			}
1234ffbafc53Scomay 
12356cfd72c6Sgjelinek 			rval = zone_ready(zlogp,
12366cfd72c6Sgjelinek 			    strcmp(zargp->bootbuf, "-U") == 0 ?
1237c5cd6260S 			    Z_MNT_UPDATE : Z_MNT_SCRATCH, zstate);
12389acbbeafSnn35248 			if (rval != 0)
12399acbbeafSnn35248 				break;
12409acbbeafSnn35248 
12419acbbeafSnn35248 			eventstream_write(Z_EVT_ZONE_READIED);
12429acbbeafSnn35248 
12430679f794S 			/*
1244e5816e35SEdward Pilatowicz 			 * Get a handle to the default brand info.
1245e5816e35SEdward Pilatowicz 			 * We must always use the default brand file system
12460679f794S 			 * list when mounting the zone.
12470679f794S 			 */
1248e5816e35SEdward Pilatowicz 			if ((bh = brand_open(default_brand)) == NULL) {
12499acbbeafSnn35248 				rval = -1;
12509acbbeafSnn35248 				break;
12519acbbeafSnn35248 			}
12529acbbeafSnn35248 
12539acbbeafSnn35248 			/*
12549acbbeafSnn35248 			 * Get the list of filesystems to mount from
12559acbbeafSnn35248 			 * the brand configuration.  These mounts are done
12569acbbeafSnn35248 			 * via a thread that will enter the zone, so they
12579acbbeafSnn35248 			 * are done from within the context of the zone.
12589acbbeafSnn35248 			 */
12599acbbeafSnn35248 			cb.zlogp = zlogp;
12609acbbeafSnn35248 			cb.zoneid = zone_id;
1261910f48daSedp 			cb.mount_cmd = B_TRUE;
1262123807fbSedp 			rval = brand_platform_iter_mounts(bh,
12639acbbeafSnn35248 			    mount_early_fs, &cb);
12649acbbeafSnn35248 
1265123807fbSedp 			brand_close(bh);
12669acbbeafSnn35248 
1267108322fbScarlsonj 			/*
1268108322fbScarlsonj 			 * Ordinarily, /dev/fd would be mounted inside the zone
1269108322fbScarlsonj 			 * by svc:/system/filesystem/usr:default, but since
1270108322fbScarlsonj 			 * we're not booting the zone, we need to do this
1271108322fbScarlsonj 			 * manually.
1272108322fbScarlsonj 			 */
1273108322fbScarlsonj 			if (rval == 0)
12749acbbeafSnn35248 				rval = mount_early_fs(&cb,
12759acbbeafSnn35248 				    "fd", "/dev/fd", "fd", NULL);
1276108322fbScarlsonj 			break;
1277108322fbScarlsonj 		case Z_UNMOUNT:
1278108322fbScarlsonj 			if (kernelcall)	/* Invalid; can't happen */
1279108322fbScarlsonj 				abort();
1280108322fbScarlsonj 			zerror(zlogp, B_FALSE, "zone is already unmounted");
1281108322fbScarlsonj 			rval = 0;
1282108322fbScarlsonj 			break;
12837c478bd9Sstevel@tonic-gate 		}
12847c478bd9Sstevel@tonic-gate 		break;
12857c478bd9Sstevel@tonic-gate 
12867c478bd9Sstevel@tonic-gate 	case ZONE_STATE_READY:
12877c478bd9Sstevel@tonic-gate 		switch (cmd) {
12887c478bd9Sstevel@tonic-gate 		case Z_READY:
12897c478bd9Sstevel@tonic-gate 			/*
12907c478bd9Sstevel@tonic-gate 			 * We could have two clients racing to ready this
12917c478bd9Sstevel@tonic-gate 			 * zone; the second client loses, but his request
12927c478bd9Sstevel@tonic-gate 			 * doesn't fail, since the zone is now in the desired
12937c478bd9Sstevel@tonic-gate 			 * state.
12947c478bd9Sstevel@tonic-gate 			 */
12957c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_FALSE, "zone is already ready");
12967c478bd9Sstevel@tonic-gate 			rval = 0;
12977c478bd9Sstevel@tonic-gate 			break;
12987c478bd9Sstevel@tonic-gate 		case Z_BOOT:
12993f2f09c1Sdp 			(void) strlcpy(boot_args, zargp->bootbuf,
13003f2f09c1Sdp 			    sizeof (boot_args));
13017c478bd9Sstevel@tonic-gate 			eventstream_write(Z_EVT_ZONE_BOOTING);
1302c5cd6260S 			rval = zone_bootup(zlogp, zargp->bootbuf, zstate);
13037c478bd9Sstevel@tonic-gate 			audit_put_record(zlogp, uc, rval, "boot");
13047c478bd9Sstevel@tonic-gate 			if (rval != 0) {
13057c478bd9Sstevel@tonic-gate 				bringup_failure_recovery = B_TRUE;
1306c5cd6260S 				(void) zone_halt(zlogp, B_FALSE, B_TRUE,
1307c5cd6260S 				    zstate);
1308ffbafc53Scomay 				eventstream_write(Z_EVT_ZONE_BOOTFAILED);
13097c478bd9Sstevel@tonic-gate 			}
13103f2f09c1Sdp 			boot_args[0] = '\0';
13117c478bd9Sstevel@tonic-gate 			break;
13127c478bd9Sstevel@tonic-gate 		case Z_HALT:
13137c478bd9Sstevel@tonic-gate 			if (kernelcall)	/* Invalid; can't happen */
13147c478bd9Sstevel@tonic-gate 				abort();
1315c5cd6260S 			if ((rval = zone_halt(zlogp, B_FALSE, B_FALSE, zstate))
1316c5cd6260S 			    != 0)
13177c478bd9Sstevel@tonic-gate 				break;
13187c478bd9Sstevel@tonic-gate 			eventstream_write(Z_EVT_ZONE_HALTED);
13197c478bd9Sstevel@tonic-gate 			break;
13207c478bd9Sstevel@tonic-gate 		case Z_REBOOT:
1321108322fbScarlsonj 		case Z_NOTE_UNINSTALLING:
1322108322fbScarlsonj 		case Z_MOUNT:
1323108322fbScarlsonj 		case Z_UNMOUNT:
13247c478bd9Sstevel@tonic-gate 			if (kernelcall)	/* Invalid; can't happen */
13257c478bd9Sstevel@tonic-gate 				abort();
13267c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_FALSE, "%s operation is invalid "
1327108322fbScarlsonj 			    "for zones in state '%s'", z_cmd_name(cmd),
13287c478bd9Sstevel@tonic-gate 			    zone_state_str(zstate));
13297c478bd9Sstevel@tonic-gate 			rval = -1;
13307c478bd9Sstevel@tonic-gate 			break;
1331108322fbScarlsonj 		}
1332108322fbScarlsonj 		break;
1333108322fbScarlsonj 
1334108322fbScarlsonj 	case ZONE_STATE_MOUNTED:
1335108322fbScarlsonj 		switch (cmd) {
1336108322fbScarlsonj 		case Z_UNMOUNT:
13377c478bd9Sstevel@tonic-gate 			if (kernelcall)	/* Invalid; can't happen */
13387c478bd9Sstevel@tonic-gate 				abort();
1339c5cd6260S 			rval = zone_halt(zlogp, B_TRUE, B_FALSE, zstate);
1340ffbafc53Scomay 			if (rval == 0) {
1341ffbafc53Scomay 				eventstream_write(Z_EVT_ZONE_HALTED);
1342108322fbScarlsonj 				(void) sema_post(&scratch_sem);
1343ffbafc53Scomay 			}
1344108322fbScarlsonj 			break;
1345108322fbScarlsonj 		default:
1346108322fbScarlsonj 			if (kernelcall)	/* Invalid; can't happen */
1347108322fbScarlsonj 				abort();
1348108322fbScarlsonj 			zerror(zlogp, B_FALSE, "%s operation is invalid "
1349108322fbScarlsonj 			    "for zones in state '%s'", z_cmd_name(cmd),
1350108322fbScarlsonj 			    zone_state_str(zstate));
13517c478bd9Sstevel@tonic-gate 			rval = -1;
13527c478bd9Sstevel@tonic-gate 			break;
13537c478bd9Sstevel@tonic-gate 		}
13547c478bd9Sstevel@tonic-gate 		break;
13557c478bd9Sstevel@tonic-gate 
13567c478bd9Sstevel@tonic-gate 	case ZONE_STATE_RUNNING:
13577c478bd9Sstevel@tonic-gate 	case ZONE_STATE_SHUTTING_DOWN:
13587c478bd9Sstevel@tonic-gate 	case ZONE_STATE_DOWN:
13597c478bd9Sstevel@tonic-gate 		switch (cmd) {
13607c478bd9Sstevel@tonic-gate 		case Z_READY:
1361c5cd6260S 			if ((rval = zone_halt(zlogp, B_FALSE, B_TRUE, zstate))
1362c5cd6260S 			    != 0)
13637c478bd9Sstevel@tonic-gate 				break;
1364c5cd6260S 			if ((rval = zone_ready(zlogp, Z_MNT_BOOT, zstate)) == 0)
13657c478bd9Sstevel@tonic-gate 				eventstream_write(Z_EVT_ZONE_READIED);
1366ffbafc53Scomay 			else
1367ffbafc53Scomay 				eventstream_write(Z_EVT_ZONE_HALTED);
13687c478bd9Sstevel@tonic-gate 			break;
13697c478bd9Sstevel@tonic-gate 		case Z_BOOT:
13707c478bd9Sstevel@tonic-gate 			/*
13717c478bd9Sstevel@tonic-gate 			 * We could have two clients racing to boot this
13727c478bd9Sstevel@tonic-gate 			 * zone; the second client loses, but his request
13737c478bd9Sstevel@tonic-gate 			 * doesn't fail, since the zone is now in the desired
13747c478bd9Sstevel@tonic-gate 			 * state.
13757c478bd9Sstevel@tonic-gate 			 */
13767c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_FALSE, "zone is already booted");
13777c478bd9Sstevel@tonic-gate 			rval = 0;
13787c478bd9Sstevel@tonic-gate 			break;
13797c478bd9Sstevel@tonic-gate 		case Z_HALT:
1380c5cd6260S 			if ((rval = zone_halt(zlogp, B_FALSE, B_FALSE, zstate))
1381c5cd6260S 			    != 0)
13827c478bd9Sstevel@tonic-gate 				break;
13837c478bd9Sstevel@tonic-gate 			eventstream_write(Z_EVT_ZONE_HALTED);
13847c478bd9Sstevel@tonic-gate 			break;
13857c478bd9Sstevel@tonic-gate 		case Z_REBOOT:
13863f2f09c1Sdp 			(void) strlcpy(boot_args, zargp->bootbuf,
13873f2f09c1Sdp 			    sizeof (boot_args));
13887c478bd9Sstevel@tonic-gate 			eventstream_write(Z_EVT_ZONE_REBOOTING);
1389c5cd6260S 			if ((rval = zone_halt(zlogp, B_FALSE, B_TRUE, zstate))
1390c5cd6260S 			    != 0) {
1391ffbafc53Scomay 				eventstream_write(Z_EVT_ZONE_BOOTFAILED);
13923f2f09c1Sdp 				boot_args[0] = '\0';
13937c478bd9Sstevel@tonic-gate 				break;
1394ffbafc53Scomay 			}
1395c5cd6260S 			if ((rval = zone_ready(zlogp, Z_MNT_BOOT, zstate))
1396c5cd6260S 			    != 0) {
1397ffbafc53Scomay 				eventstream_write(Z_EVT_ZONE_BOOTFAILED);
13983f2f09c1Sdp 				boot_args[0] = '\0';
1399ffbafc53Scomay 				break;
1400ffbafc53Scomay 			}
1401c5cd6260S 			rval = zone_bootup(zlogp, zargp->bootbuf, zstate);
14027c478bd9Sstevel@tonic-gate 			audit_put_record(zlogp, uc, rval, "reboot");
1403ffbafc53Scomay 			if (rval != 0) {
1404c5cd6260S 				(void) zone_halt(zlogp, B_FALSE, B_TRUE,
1405c5cd6260S 				    zstate);
1406ffbafc53Scomay 				eventstream_write(Z_EVT_ZONE_BOOTFAILED);
14077c478bd9Sstevel@tonic-gate 			}
14083f2f09c1Sdp 			boot_args[0] = '\0';
14097c478bd9Sstevel@tonic-gate 			break;
14107c478bd9Sstevel@tonic-gate 		case Z_NOTE_UNINSTALLING:
1411108322fbScarlsonj 		case Z_MOUNT:
1412108322fbScarlsonj 		case Z_UNMOUNT:
1413108322fbScarlsonj 			zerror(zlogp, B_FALSE, "%s operation is invalid "
1414108322fbScarlsonj 			    "for zones in state '%s'", z_cmd_name(cmd),
1415108322fbScarlsonj 			    zone_state_str(zstate));
14167c478bd9Sstevel@tonic-gate 			rval = -1;
14177c478bd9Sstevel@tonic-gate 			break;
14187c478bd9Sstevel@tonic-gate 		}
14197c478bd9Sstevel@tonic-gate 		break;
14207c478bd9Sstevel@tonic-gate 	default:
14217c478bd9Sstevel@tonic-gate 		abort();
14227c478bd9Sstevel@tonic-gate 	}
14237c478bd9Sstevel@tonic-gate 
14247c478bd9Sstevel@tonic-gate 	/*
14257c478bd9Sstevel@tonic-gate 	 * Because the state of the zone may have changed, we make sure
14267c478bd9Sstevel@tonic-gate 	 * to wake the console poller, which is in charge of initiating
14277c478bd9Sstevel@tonic-gate 	 * the shutdown procedure as necessary.
14287c478bd9Sstevel@tonic-gate 	 */
14297c478bd9Sstevel@tonic-gate 	eventstream_write(Z_EVT_NULL);
14307c478bd9Sstevel@tonic-gate 
14317c478bd9Sstevel@tonic-gate out:
14327c478bd9Sstevel@tonic-gate 	(void) mutex_unlock(&lock);
14337c478bd9Sstevel@tonic-gate 	if (kernelcall) {
14347c478bd9Sstevel@tonic-gate 		rvalp = NULL;
14357c478bd9Sstevel@tonic-gate 		rlen = 0;
14367c478bd9Sstevel@tonic-gate 	} else {
14377c478bd9Sstevel@tonic-gate 		rvalp->rval = rval;
14387c478bd9Sstevel@tonic-gate 	}
14397c478bd9Sstevel@tonic-gate 	if (uc != NULL)
14407c478bd9Sstevel@tonic-gate 		ucred_free(uc);
14417c478bd9Sstevel@tonic-gate 	(void) door_return((char *)rvalp, rlen, NULL, 0);
14427c478bd9Sstevel@tonic-gate 	thr_exit(NULL);
14437c478bd9Sstevel@tonic-gate }
14447c478bd9Sstevel@tonic-gate 
14457c478bd9Sstevel@tonic-gate static int
14467c478bd9Sstevel@tonic-gate setup_door(zlog_t *zlogp)
14477c478bd9Sstevel@tonic-gate {
14487c478bd9Sstevel@tonic-gate 	if ((zone_door = door_create(server, NULL,
14497c478bd9Sstevel@tonic-gate 	    DOOR_UNREF | DOOR_REFUSE_DESC | DOOR_NO_CANCEL)) < 0) {
14507c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "%s failed", "door_create");
14517c478bd9Sstevel@tonic-gate 		return (-1);
14527c478bd9Sstevel@tonic-gate 	}
14537c478bd9Sstevel@tonic-gate 	(void) fdetach(zone_door_path);
14547c478bd9Sstevel@tonic-gate 
14557c478bd9Sstevel@tonic-gate 	if (fattach(zone_door, zone_door_path) != 0) {
14567c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "fattach to %s failed", zone_door_path);
14577c478bd9Sstevel@tonic-gate 		(void) door_revoke(zone_door);
14587c478bd9Sstevel@tonic-gate 		(void) fdetach(zone_door_path);
14597c478bd9Sstevel@tonic-gate 		zone_door = -1;
14607c478bd9Sstevel@tonic-gate 		return (-1);
14617c478bd9Sstevel@tonic-gate 	}
14627c478bd9Sstevel@tonic-gate 	return (0);
14637c478bd9Sstevel@tonic-gate }
14647c478bd9Sstevel@tonic-gate 
14657c478bd9Sstevel@tonic-gate /*
14667c478bd9Sstevel@tonic-gate  * zoneadm(1m) will start zoneadmd if it thinks it isn't running; this
14677c478bd9Sstevel@tonic-gate  * is where zoneadmd itself will check to see that another instance of
14687c478bd9Sstevel@tonic-gate  * zoneadmd isn't already controlling this zone.
14697c478bd9Sstevel@tonic-gate  *
14707c478bd9Sstevel@tonic-gate  * The idea here is that we want to open the path to which we will
14717c478bd9Sstevel@tonic-gate  * attach our door, lock it, and then make sure that no-one has beat us
14727c478bd9Sstevel@tonic-gate  * to fattach(3c)ing onto it.
14737c478bd9Sstevel@tonic-gate  *
14747c478bd9Sstevel@tonic-gate  * fattach(3c) is really a mount, so there are actually two possible
14757c478bd9Sstevel@tonic-gate  * vnodes we could be dealing with.  Our strategy is as follows:
14767c478bd9Sstevel@tonic-gate  *
14777c478bd9Sstevel@tonic-gate  * - If the file we opened is a regular file (common case):
14787c478bd9Sstevel@tonic-gate  * 	There is no fattach(3c)ed door, so we have a chance of becoming
14797c478bd9Sstevel@tonic-gate  * 	the managing zoneadmd. We attempt to lock the file: if it is
14807c478bd9Sstevel@tonic-gate  * 	already locked, that means someone else raced us here, so we
14817c478bd9Sstevel@tonic-gate  * 	lose and give up.  zoneadm(1m) will try to contact the zoneadmd
14827c478bd9Sstevel@tonic-gate  * 	that beat us to it.
14837c478bd9Sstevel@tonic-gate  *
14847c478bd9Sstevel@tonic-gate  * - If the file we opened is a namefs file:
14857c478bd9Sstevel@tonic-gate  * 	This means there is already an established door fattach(3c)'ed
14867c478bd9Sstevel@tonic-gate  * 	to the rendezvous path.  We've lost the race, so we give up.
14877c478bd9Sstevel@tonic-gate  * 	Note that in this case we also try to grab the file lock, and
14887c478bd9Sstevel@tonic-gate  * 	will succeed in acquiring it since the vnode locked by the
14897c478bd9Sstevel@tonic-gate  * 	"winning" zoneadmd was a regular one, and the one we locked was
14907c478bd9Sstevel@tonic-gate  * 	the fattach(3c)'ed door node.  At any rate, no harm is done, and
14917c478bd9Sstevel@tonic-gate  * 	we just return to zoneadm(1m) which knows to retry.
14927c478bd9Sstevel@tonic-gate  */
14937c478bd9Sstevel@tonic-gate static int
14947c478bd9Sstevel@tonic-gate make_daemon_exclusive(zlog_t *zlogp)
14957c478bd9Sstevel@tonic-gate {
14967c478bd9Sstevel@tonic-gate 	int doorfd = -1;
14977c478bd9Sstevel@tonic-gate 	int err, ret = -1;
14987c478bd9Sstevel@tonic-gate 	struct stat st;
14997c478bd9Sstevel@tonic-gate 	struct flock flock;
15007c478bd9Sstevel@tonic-gate 	zone_state_t zstate;
15017c478bd9Sstevel@tonic-gate 
15027c478bd9Sstevel@tonic-gate top:
15037c478bd9Sstevel@tonic-gate 	if ((err = zone_get_state(zone_name, &zstate)) != Z_OK) {
15043f2f09c1Sdp 		zerror(zlogp, B_FALSE, "failed to get zone state: %s",
15057c478bd9Sstevel@tonic-gate 		    zonecfg_strerror(err));
15067c478bd9Sstevel@tonic-gate 		goto out;
15077c478bd9Sstevel@tonic-gate 	}
15087c478bd9Sstevel@tonic-gate 	if ((doorfd = open(zone_door_path, O_CREAT|O_RDWR,
15097c478bd9Sstevel@tonic-gate 	    S_IREAD|S_IWRITE)) < 0) {
15107c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "failed to open %s", zone_door_path);
15117c478bd9Sstevel@tonic-gate 		goto out;
15127c478bd9Sstevel@tonic-gate 	}
15137c478bd9Sstevel@tonic-gate 	if (fstat(doorfd, &st) < 0) {
15147c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "failed to stat %s", zone_door_path);
15157c478bd9Sstevel@tonic-gate 		goto out;
15167c478bd9Sstevel@tonic-gate 	}
15177c478bd9Sstevel@tonic-gate 	/*
15187c478bd9Sstevel@tonic-gate 	 * Lock the file to synchronize with other zoneadmd
15197c478bd9Sstevel@tonic-gate 	 */
15207c478bd9Sstevel@tonic-gate 	flock.l_type = F_WRLCK;
15217c478bd9Sstevel@tonic-gate 	flock.l_whence = SEEK_SET;
15227c478bd9Sstevel@tonic-gate 	flock.l_start = (off_t)0;
15237c478bd9Sstevel@tonic-gate 	flock.l_len = (off_t)0;
15247c478bd9Sstevel@tonic-gate 	if (fcntl(doorfd, F_SETLK, &flock) < 0) {
15257c478bd9Sstevel@tonic-gate 		/*
15267c478bd9Sstevel@tonic-gate 		 * Someone else raced us here and grabbed the lock file
15277c478bd9Sstevel@tonic-gate 		 * first.  A warning here is inappropriate since nothing
15287c478bd9Sstevel@tonic-gate 		 * went wrong.
15297c478bd9Sstevel@tonic-gate 		 */
15307c478bd9Sstevel@tonic-gate 		goto out;
15317c478bd9Sstevel@tonic-gate 	}
15327c478bd9Sstevel@tonic-gate 
15337c478bd9Sstevel@tonic-gate 	if (strcmp(st.st_fstype, "namefs") == 0) {
15347c478bd9Sstevel@tonic-gate 		struct door_info info;
15357c478bd9Sstevel@tonic-gate 
15367c478bd9Sstevel@tonic-gate 		/*
15377c478bd9Sstevel@tonic-gate 		 * There is already something fattach()'ed to this file.
15387c478bd9Sstevel@tonic-gate 		 * Lets see what the door is up to.
15397c478bd9Sstevel@tonic-gate 		 */
15407c478bd9Sstevel@tonic-gate 		if (door_info(doorfd, &info) == 0 && info.di_target != -1) {
15417c478bd9Sstevel@tonic-gate 			/*
15427c478bd9Sstevel@tonic-gate 			 * Another zoneadmd process seems to be in
15437c478bd9Sstevel@tonic-gate 			 * control of the situation and we don't need to
15447c478bd9Sstevel@tonic-gate 			 * be here.  A warning here is inappropriate
15457c478bd9Sstevel@tonic-gate 			 * since nothing went wrong.
15467c478bd9Sstevel@tonic-gate 			 *
15477c478bd9Sstevel@tonic-gate 			 * If the door has been revoked, the zoneadmd
15487c478bd9Sstevel@tonic-gate 			 * process currently managing the zone is going
15497c478bd9Sstevel@tonic-gate 			 * away.  We'll return control to zoneadm(1m)
15507c478bd9Sstevel@tonic-gate 			 * which will try again (by which time zoneadmd
15517c478bd9Sstevel@tonic-gate 			 * will hopefully have exited).
15527c478bd9Sstevel@tonic-gate 			 */
15537c478bd9Sstevel@tonic-gate 			goto out;
15547c478bd9Sstevel@tonic-gate 		}
15557c478bd9Sstevel@tonic-gate 
15567c478bd9Sstevel@tonic-gate 		/*
15577c478bd9Sstevel@tonic-gate 		 * If we got this far, there's a fattach(3c)'ed door
15587c478bd9Sstevel@tonic-gate 		 * that belongs to a process that has exited, which can
15597c478bd9Sstevel@tonic-gate 		 * happen if the previous zoneadmd died unexpectedly.
15607c478bd9Sstevel@tonic-gate 		 *
15617c478bd9Sstevel@tonic-gate 		 * Let user know that something is amiss, but that we can
15627c478bd9Sstevel@tonic-gate 		 * recover; if the zone is in the installed state, then don't
15637c478bd9Sstevel@tonic-gate 		 * message, since having a running zoneadmd isn't really
15647c478bd9Sstevel@tonic-gate 		 * expected/needed.  We want to keep occurences of this message
15657c478bd9Sstevel@tonic-gate 		 * limited to times when zoneadmd is picking back up from a
15667c478bd9Sstevel@tonic-gate 		 * zoneadmd that died while the zone was in some non-trivial
15677c478bd9Sstevel@tonic-gate 		 * state.
15687c478bd9Sstevel@tonic-gate 		 */
15697c478bd9Sstevel@tonic-gate 		if (zstate > ZONE_STATE_INSTALLED) {
15707c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_FALSE,
15717c478bd9Sstevel@tonic-gate 			    "zone '%s': WARNING: zone is in state '%s', but "
15727c478bd9Sstevel@tonic-gate 			    "zoneadmd does not appear to be available; "
15737c478bd9Sstevel@tonic-gate 			    "restarted zoneadmd to recover.",
15747c478bd9Sstevel@tonic-gate 			    zone_name, zone_state_str(zstate));
15757c478bd9Sstevel@tonic-gate 		}
15767c478bd9Sstevel@tonic-gate 
15777c478bd9Sstevel@tonic-gate 		(void) fdetach(zone_door_path);
15787c478bd9Sstevel@tonic-gate 		(void) close(doorfd);
15797c478bd9Sstevel@tonic-gate 		goto top;
15807c478bd9Sstevel@tonic-gate 	}
15817c478bd9Sstevel@tonic-gate 	ret = 0;
15827c478bd9Sstevel@tonic-gate out:
15837c478bd9Sstevel@tonic-gate 	(void) close(doorfd);
15847c478bd9Sstevel@tonic-gate 	return (ret);
15857c478bd9Sstevel@tonic-gate }
15867c478bd9Sstevel@tonic-gate 
1587c5cd6260S /*
1588c5cd6260S  * Setup the brand's pre and post state change callbacks, as well as the
1589c5cd6260S  * query callback, if any of these exist.
1590c5cd6260S  */
1591c5cd6260S static int
1592c5cd6260S brand_callback_init(brand_handle_t bh, char *zone_name)
1593c5cd6260S {
1594c5cd6260S 	char zpath[MAXPATHLEN];
1595c5cd6260S 
1596c5cd6260S 	if (zone_get_zonepath(zone_name, zpath, sizeof (zpath)) != Z_OK)
1597c5cd6260S 		return (-1);
1598c5cd6260S 
1599c5cd6260S 	(void) strlcpy(pre_statechg_hook, EXEC_PREFIX,
1600c5cd6260S 	    sizeof (pre_statechg_hook));
1601c5cd6260S 
1602c5cd6260S 	if (brand_get_prestatechange(bh, zone_name, zpath,
1603c5cd6260S 	    pre_statechg_hook + EXEC_LEN,
1604c5cd6260S 	    sizeof (pre_statechg_hook) - EXEC_LEN) != 0)
1605c5cd6260S 		return (-1);
1606c5cd6260S 
1607c5cd6260S 	if (strlen(pre_statechg_hook) <= EXEC_LEN)
1608c5cd6260S 		pre_statechg_hook[0] = '\0';
1609c5cd6260S 
1610c5cd6260S 	(void) strlcpy(post_statechg_hook, EXEC_PREFIX,
1611c5cd6260S 	    sizeof (post_statechg_hook));
1612c5cd6260S 
1613c5cd6260S 	if (brand_get_poststatechange(bh, zone_name, zpath,
1614c5cd6260S 	    post_statechg_hook + EXEC_LEN,
1615c5cd6260S 	    sizeof (post_statechg_hook) - EXEC_LEN) != 0)
1616c5cd6260S 		return (-1);
1617c5cd6260S 
1618c5cd6260S 	if (strlen(post_statechg_hook) <= EXEC_LEN)
1619c5cd6260S 		post_statechg_hook[0] = '\0';
1620c5cd6260S 
1621c5cd6260S 	(void) strlcpy(query_hook, EXEC_PREFIX,
1622c5cd6260S 	    sizeof (query_hook));
1623c5cd6260S 
1624c5cd6260S 	if (brand_get_query(bh, zone_name, zpath, query_hook + EXEC_LEN,
1625c5cd6260S 	    sizeof (query_hook) - EXEC_LEN) != 0)
1626c5cd6260S 		return (-1);
1627c5cd6260S 
1628c5cd6260S 	if (strlen(query_hook) <= EXEC_LEN)
1629c5cd6260S 		query_hook[0] = '\0';
1630c5cd6260S 
1631c5cd6260S 	return (0);
1632c5cd6260S }
1633c5cd6260S 
16347c478bd9Sstevel@tonic-gate int
16357c478bd9Sstevel@tonic-gate main(int argc, char *argv[])
16367c478bd9Sstevel@tonic-gate {
16377c478bd9Sstevel@tonic-gate 	int opt;
16387c478bd9Sstevel@tonic-gate 	zoneid_t zid;
16397c478bd9Sstevel@tonic-gate 	priv_set_t *privset;
16407c478bd9Sstevel@tonic-gate 	zone_state_t zstate;
16417c478bd9Sstevel@tonic-gate 	char parents_locale[MAXPATHLEN];
1642123807fbSedp 	brand_handle_t bh;
16437c478bd9Sstevel@tonic-gate 	int err;
16447c478bd9Sstevel@tonic-gate 
16457c478bd9Sstevel@tonic-gate 	pid_t pid;
16467c478bd9Sstevel@tonic-gate 	sigset_t blockset;
16477c478bd9Sstevel@tonic-gate 	sigset_t block_cld;
16487c478bd9Sstevel@tonic-gate 
16497c478bd9Sstevel@tonic-gate 	struct {
16507c478bd9Sstevel@tonic-gate 		sema_t sem;
16517c478bd9Sstevel@tonic-gate 		int status;
16527c478bd9Sstevel@tonic-gate 		zlog_t log;
16537c478bd9Sstevel@tonic-gate 	} *shstate;
16547c478bd9Sstevel@tonic-gate 	size_t shstatelen = getpagesize();
16557c478bd9Sstevel@tonic-gate 
16567c478bd9Sstevel@tonic-gate 	zlog_t errlog;
16577c478bd9Sstevel@tonic-gate 	zlog_t *zlogp;
16587c478bd9Sstevel@tonic-gate 
16595cb4571dSdp 	int ctfd;
16605cb4571dSdp 
16617c478bd9Sstevel@tonic-gate 	progname = get_execbasename(argv[0]);
16627c478bd9Sstevel@tonic-gate 
16637c478bd9Sstevel@tonic-gate 	/*
16647c478bd9Sstevel@tonic-gate 	 * Make sure stderr is unbuffered
16657c478bd9Sstevel@tonic-gate 	 */
16667c478bd9Sstevel@tonic-gate 	(void) setbuffer(stderr, NULL, 0);
16677c478bd9Sstevel@tonic-gate 
16687c478bd9Sstevel@tonic-gate 	/*
16697c478bd9Sstevel@tonic-gate 	 * Get out of the way of mounted filesystems, since we will daemonize
16707c478bd9Sstevel@tonic-gate 	 * soon.
16717c478bd9Sstevel@tonic-gate 	 */
16727c478bd9Sstevel@tonic-gate 	(void) chdir("/");
16737c478bd9Sstevel@tonic-gate 
16747c478bd9Sstevel@tonic-gate 	/*
16757c478bd9Sstevel@tonic-gate 	 * Use the default system umask per PSARC 1998/110 rather than
16767c478bd9Sstevel@tonic-gate 	 * anything that may have been set by the caller.
16777c478bd9Sstevel@tonic-gate 	 */
16787c478bd9Sstevel@tonic-gate 	(void) umask(CMASK);
16797c478bd9Sstevel@tonic-gate 
16807c478bd9Sstevel@tonic-gate 	/*
16817c478bd9Sstevel@tonic-gate 	 * Initially we want to use our parent's locale.
16827c478bd9Sstevel@tonic-gate 	 */
16837c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
16847c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
16857c478bd9Sstevel@tonic-gate 	(void) strlcpy(parents_locale, setlocale(LC_MESSAGES, NULL),
16867c478bd9Sstevel@tonic-gate 	    sizeof (parents_locale));
16877c478bd9Sstevel@tonic-gate 
16887c478bd9Sstevel@tonic-gate 	/*
16897c478bd9Sstevel@tonic-gate 	 * This zlog_t is used for writing to stderr
16907c478bd9Sstevel@tonic-gate 	 */
16917c478bd9Sstevel@tonic-gate 	errlog.logfile = stderr;
16927c478bd9Sstevel@tonic-gate 	errlog.buflen = errlog.loglen = 0;
16937c478bd9Sstevel@tonic-gate 	errlog.buf = errlog.log = NULL;
16947c478bd9Sstevel@tonic-gate 	errlog.locale = parents_locale;
16957c478bd9Sstevel@tonic-gate 
16967c478bd9Sstevel@tonic-gate 	/*
16977c478bd9Sstevel@tonic-gate 	 * We start off writing to stderr until we're ready to daemonize.
16987c478bd9Sstevel@tonic-gate 	 */
16997c478bd9Sstevel@tonic-gate 	zlogp = &errlog;
17007c478bd9Sstevel@tonic-gate 
17017c478bd9Sstevel@tonic-gate 	/*
17027c478bd9Sstevel@tonic-gate 	 * Process options.
17037c478bd9Sstevel@tonic-gate 	 */
1704108322fbScarlsonj 	while ((opt = getopt(argc, argv, "R:z:")) != EOF) {
17057c478bd9Sstevel@tonic-gate 		switch (opt) {
1706108322fbScarlsonj 		case 'R':
1707108322fbScarlsonj 			zonecfg_set_root(optarg);
1708108322fbScarlsonj 			break;
17097c478bd9Sstevel@tonic-gate 		case 'z':
17107c478bd9Sstevel@tonic-gate 			zone_name = optarg;
17117c478bd9Sstevel@tonic-gate 			break;
17127c478bd9Sstevel@tonic-gate 		default:
17137c478bd9Sstevel@tonic-gate 			usage();
17147c478bd9Sstevel@tonic-gate 		}
17157c478bd9Sstevel@tonic-gate 	}
17167c478bd9Sstevel@tonic-gate 
17177c478bd9Sstevel@tonic-gate 	if (zone_name == NULL)
17187c478bd9Sstevel@tonic-gate 		usage();
17197c478bd9Sstevel@tonic-gate 
17207c478bd9Sstevel@tonic-gate 	/*
17217c478bd9Sstevel@tonic-gate 	 * Because usage() prints directly to stderr, it has gettext()
17227c478bd9Sstevel@tonic-gate 	 * wrapping, which depends on the locale.  But since zerror() calls
17237c478bd9Sstevel@tonic-gate 	 * localize() which tweaks the locale, it is not safe to call zerror()
17247c478bd9Sstevel@tonic-gate 	 * until after the last call to usage().  Fortunately, the last call
17257c478bd9Sstevel@tonic-gate 	 * to usage() is just above and the first call to zerror() is just
17267c478bd9Sstevel@tonic-gate 	 * below.  Don't mess this up.
17277c478bd9Sstevel@tonic-gate 	 */
17287c478bd9Sstevel@tonic-gate 	if (strcmp(zone_name, GLOBAL_ZONENAME) == 0) {
17297c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "cannot manage the %s zone",
17307c478bd9Sstevel@tonic-gate 		    GLOBAL_ZONENAME);
17317c478bd9Sstevel@tonic-gate 		return (1);
17327c478bd9Sstevel@tonic-gate 	}
17337c478bd9Sstevel@tonic-gate 
17347c478bd9Sstevel@tonic-gate 	if (zone_get_id(zone_name, &zid) != 0) {
17353f2f09c1Sdp 		zerror(zlogp, B_FALSE, "could not manage %s: %s", zone_name,
17367c478bd9Sstevel@tonic-gate 		    zonecfg_strerror(Z_NO_ZONE));
17377c478bd9Sstevel@tonic-gate 		return (1);
17387c478bd9Sstevel@tonic-gate 	}
17397c478bd9Sstevel@tonic-gate 
17407c478bd9Sstevel@tonic-gate 	if ((err = zone_get_state(zone_name, &zstate)) != Z_OK) {
17413f2f09c1Sdp 		zerror(zlogp, B_FALSE, "failed to get zone state: %s",
17427c478bd9Sstevel@tonic-gate 		    zonecfg_strerror(err));
17437c478bd9Sstevel@tonic-gate 		return (1);
17447c478bd9Sstevel@tonic-gate 	}
17459acbbeafSnn35248 	if (zstate < ZONE_STATE_INCOMPLETE) {
17467c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE,
17477c478bd9Sstevel@tonic-gate 		    "cannot manage a zone which is in state '%s'",
17487c478bd9Sstevel@tonic-gate 		    zone_state_str(zstate));
17497c478bd9Sstevel@tonic-gate 		return (1);
17507c478bd9Sstevel@tonic-gate 	}
17517c478bd9Sstevel@tonic-gate 
1752e5816e35SEdward Pilatowicz 	if (zonecfg_default_brand(default_brand,
1753e5816e35SEdward Pilatowicz 	    sizeof (default_brand)) != Z_OK) {
1754e5816e35SEdward Pilatowicz 		zerror(zlogp, B_FALSE, "unable to determine default brand");
1755e5816e35SEdward Pilatowicz 		return (1);
1756e5816e35SEdward Pilatowicz 	}
1757e5816e35SEdward Pilatowicz 
17589acbbeafSnn35248 	/* Get a handle to the brand info for this zone */
175962868012SSteve Lawrence 	if (zone_get_brand(zone_name, brand_name, sizeof (brand_name))
176062868012SSteve Lawrence 	    != Z_OK) {
17619acbbeafSnn35248 		zerror(zlogp, B_FALSE, "unable to determine zone brand");
17629acbbeafSnn35248 		return (1);
17639acbbeafSnn35248 	}
176462868012SSteve Lawrence 	zone_isnative = (strcmp(brand_name, NATIVE_BRAND_NAME) == 0);
17659a5d73e0SRic Aleshire 	zone_islabeled = (strcmp(brand_name, LABELED_BRAND_NAME) == 0);
1766c5cd6260S 
176762868012SSteve Lawrence 	/*
176862868012SSteve Lawrence 	 * In the alternate root environment, the only supported
176962868012SSteve Lawrence 	 * operations are mount and unmount.  In this case, just treat
177062868012SSteve Lawrence 	 * the zone as native if it is cluster.  Cluster zones can be
177162868012SSteve Lawrence 	 * native for the purpose of LU or upgrade, and the cluster
177262868012SSteve Lawrence 	 * brand may not exist in the miniroot (such as in net install
177362868012SSteve Lawrence 	 * upgrade).
177462868012SSteve Lawrence 	 */
177562868012SSteve Lawrence 	if (strcmp(brand_name, CLUSTER_BRAND_NAME) == 0) {
177662868012SSteve Lawrence 		zone_iscluster = B_TRUE;
177762868012SSteve Lawrence 		if (zonecfg_in_alt_root()) {
1778e5816e35SEdward Pilatowicz 			(void) strlcpy(brand_name, default_brand,
177962868012SSteve Lawrence 			    sizeof (brand_name));
178062868012SSteve Lawrence 		}
178162868012SSteve Lawrence 	} else {
178262868012SSteve Lawrence 		zone_iscluster = B_FALSE;
178362868012SSteve Lawrence 	}
178462868012SSteve Lawrence 
178562868012SSteve Lawrence 	if ((bh = brand_open(brand_name)) == NULL) {
178662868012SSteve Lawrence 		zerror(zlogp, B_FALSE, "unable to open zone brand");
178762868012SSteve Lawrence 		return (1);
178862868012SSteve Lawrence 	}
178962868012SSteve Lawrence 
1790c5cd6260S 	/* Get state change brand hooks. */
1791c5cd6260S 	if (brand_callback_init(bh, zone_name) == -1) {
1792c5cd6260S 		zerror(zlogp, B_TRUE,
1793c5cd6260S 		    "failed to initialize brand state change hooks");
1794c5cd6260S 		brand_close(bh);
1795c5cd6260S 		return (1);
1796c5cd6260S 	}
1797c5cd6260S 
1798123807fbSedp 	brand_close(bh);
17999acbbeafSnn35248 
18007c478bd9Sstevel@tonic-gate 	/*
18017c478bd9Sstevel@tonic-gate 	 * Check that we have all privileges.  It would be nice to pare
18027c478bd9Sstevel@tonic-gate 	 * this down, but this is at least a first cut.
18037c478bd9Sstevel@tonic-gate 	 */
18047c478bd9Sstevel@tonic-gate 	if ((privset = priv_allocset()) == NULL) {
18057c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "%s failed", "priv_allocset");
18067c478bd9Sstevel@tonic-gate 		return (1);
18077c478bd9Sstevel@tonic-gate 	}
18087c478bd9Sstevel@tonic-gate 
18097c478bd9Sstevel@tonic-gate 	if (getppriv(PRIV_EFFECTIVE, privset) != 0) {
18107c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "%s failed", "getppriv");
18117c478bd9Sstevel@tonic-gate 		priv_freeset(privset);
18127c478bd9Sstevel@tonic-gate 		return (1);
18137c478bd9Sstevel@tonic-gate 	}
18147c478bd9Sstevel@tonic-gate 
18157c478bd9Sstevel@tonic-gate 	if (priv_isfullset(privset) == B_FALSE) {
18167c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "You lack sufficient privilege to "
18173f2f09c1Sdp 		    "run this command (all privs required)");
18187c478bd9Sstevel@tonic-gate 		priv_freeset(privset);
18197c478bd9Sstevel@tonic-gate 		return (1);
18207c478bd9Sstevel@tonic-gate 	}
18217c478bd9Sstevel@tonic-gate 	priv_freeset(privset);
18227c478bd9Sstevel@tonic-gate 
18237c478bd9Sstevel@tonic-gate 	if (mkzonedir(zlogp) != 0)
18247c478bd9Sstevel@tonic-gate 		return (1);
18257c478bd9Sstevel@tonic-gate 
18267c478bd9Sstevel@tonic-gate 	/*
18277c478bd9Sstevel@tonic-gate 	 * Pre-fork: setup shared state
18287c478bd9Sstevel@tonic-gate 	 */
18297c478bd9Sstevel@tonic-gate 	if ((shstate = (void *)mmap(NULL, shstatelen,
18307c478bd9Sstevel@tonic-gate 	    PROT_READ|PROT_WRITE, MAP_SHARED|MAP_ANON, -1, (off_t)0)) ==
18317c478bd9Sstevel@tonic-gate 	    MAP_FAILED) {
18327c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "%s failed", "mmap");
18337c478bd9Sstevel@tonic-gate 		return (1);
18347c478bd9Sstevel@tonic-gate 	}
18357c478bd9Sstevel@tonic-gate 	if (sema_init(&shstate->sem, 0, USYNC_PROCESS, NULL) != 0) {
18367c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "%s failed", "sema_init()");
18377c478bd9Sstevel@tonic-gate 		(void) munmap((char *)shstate, shstatelen);
18387c478bd9Sstevel@tonic-gate 		return (1);
18397c478bd9Sstevel@tonic-gate 	}
18407c478bd9Sstevel@tonic-gate 	shstate->log.logfile = NULL;
18417c478bd9Sstevel@tonic-gate 	shstate->log.buflen = shstatelen - sizeof (*shstate);
18427c478bd9Sstevel@tonic-gate 	shstate->log.loglen = shstate->log.buflen;
18437c478bd9Sstevel@tonic-gate 	shstate->log.buf = (char *)shstate + sizeof (*shstate);
18447c478bd9Sstevel@tonic-gate 	shstate->log.log = shstate->log.buf;
18457c478bd9Sstevel@tonic-gate 	shstate->log.locale = parents_locale;
18467c478bd9Sstevel@tonic-gate 	shstate->status = -1;
18477c478bd9Sstevel@tonic-gate 
18487c478bd9Sstevel@tonic-gate 	/*
18497c478bd9Sstevel@tonic-gate 	 * We need a SIGCHLD handler so the sema_wait() below will wake
18507c478bd9Sstevel@tonic-gate 	 * up if the child dies without doing a sema_post().
18517c478bd9Sstevel@tonic-gate 	 */
18527c478bd9Sstevel@tonic-gate 	(void) sigset(SIGCHLD, sigchld);
18537c478bd9Sstevel@tonic-gate 	/*
18547c478bd9Sstevel@tonic-gate 	 * We must mask SIGCHLD until after we've coped with the fork
18557c478bd9Sstevel@tonic-gate 	 * sufficiently to deal with it; otherwise we can race and
18567c478bd9Sstevel@tonic-gate 	 * receive the signal before pid has been initialized
18577c478bd9Sstevel@tonic-gate 	 * (yes, this really happens).
18587c478bd9Sstevel@tonic-gate 	 */
18597c478bd9Sstevel@tonic-gate 	(void) sigemptyset(&block_cld);
18607c478bd9Sstevel@tonic-gate 	(void) sigaddset(&block_cld, SIGCHLD);
18617c478bd9Sstevel@tonic-gate 	(void) sigprocmask(SIG_BLOCK, &block_cld, NULL);
18627c478bd9Sstevel@tonic-gate 
18635cb4571dSdp 	if ((ctfd = init_template()) == -1) {
18645cb4571dSdp 		zerror(zlogp, B_TRUE, "failed to create contract");
18655cb4571dSdp 		return (1);
18665cb4571dSdp 	}
18675cb4571dSdp 
18687c478bd9Sstevel@tonic-gate 	/*
18697c478bd9Sstevel@tonic-gate 	 * Do not let another thread localize a message while we are forking.
18707c478bd9Sstevel@tonic-gate 	 */
18717c478bd9Sstevel@tonic-gate 	(void) mutex_lock(&msglock);
18727c478bd9Sstevel@tonic-gate 	pid = fork();
18737c478bd9Sstevel@tonic-gate 	(void) mutex_unlock(&msglock);
18745cb4571dSdp 
18755cb4571dSdp 	/*
18765cb4571dSdp 	 * In all cases (parent, child, and in the event of an error) we
18775cb4571dSdp 	 * don't want to cause creation of contracts on subsequent fork()s.
18785cb4571dSdp 	 */
18795cb4571dSdp 	(void) ct_tmpl_clear(ctfd);
18805cb4571dSdp 	(void) close(ctfd);
18815cb4571dSdp 
18827c478bd9Sstevel@tonic-gate 	if (pid == -1) {
18837c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "could not fork");
18847c478bd9Sstevel@tonic-gate 		return (1);
18857c478bd9Sstevel@tonic-gate 
18867c478bd9Sstevel@tonic-gate 	} else if (pid > 0) { /* parent */
18877c478bd9Sstevel@tonic-gate 		(void) sigprocmask(SIG_UNBLOCK, &block_cld, NULL);
18887c478bd9Sstevel@tonic-gate 		/*
18897c478bd9Sstevel@tonic-gate 		 * This marks a window of vulnerability in which we receive
18907c478bd9Sstevel@tonic-gate 		 * the SIGCLD before falling into sema_wait (normally we would
18917c478bd9Sstevel@tonic-gate 		 * get woken up from sema_wait with EINTR upon receipt of
18927c478bd9Sstevel@tonic-gate 		 * SIGCLD).  So we may need to use some other scheme like
18937c478bd9Sstevel@tonic-gate 		 * sema_posting in the sigcld handler.
18947c478bd9Sstevel@tonic-gate 		 * blech
18957c478bd9Sstevel@tonic-gate 		 */
18967c478bd9Sstevel@tonic-gate 		(void) sema_wait(&shstate->sem);
18977c478bd9Sstevel@tonic-gate 		(void) sema_destroy(&shstate->sem);
18987c478bd9Sstevel@tonic-gate 		if (shstate->status != 0)
18997c478bd9Sstevel@tonic-gate 			(void) waitpid(pid, NULL, WNOHANG);
19007c478bd9Sstevel@tonic-gate 		/*
19017c478bd9Sstevel@tonic-gate 		 * It's ok if we die with SIGPIPE.  It's not like we could have
19027c478bd9Sstevel@tonic-gate 		 * done anything about it.
19037c478bd9Sstevel@tonic-gate 		 */
19047c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "%s", shstate->log.buf);
19057c478bd9Sstevel@tonic-gate 		_exit(shstate->status == 0 ? 0 : 1);
19067c478bd9Sstevel@tonic-gate 	}
19077c478bd9Sstevel@tonic-gate 
19087c478bd9Sstevel@tonic-gate 	/*
19097c478bd9Sstevel@tonic-gate 	 * The child charges on.
19107c478bd9Sstevel@tonic-gate 	 */
19117c478bd9Sstevel@tonic-gate 	(void) sigset(SIGCHLD, SIG_DFL);
19127c478bd9Sstevel@tonic-gate 	(void) sigprocmask(SIG_UNBLOCK, &block_cld, NULL);
19137c478bd9Sstevel@tonic-gate 
19147c478bd9Sstevel@tonic-gate 	/*
19157c478bd9Sstevel@tonic-gate 	 * SIGPIPE can be delivered if we write to a socket for which the
19167c478bd9Sstevel@tonic-gate 	 * peer endpoint is gone.  That can lead to too-early termination
19177c478bd9Sstevel@tonic-gate 	 * of zoneadmd, and that's not good eats.
19187c478bd9Sstevel@tonic-gate 	 */
19197c478bd9Sstevel@tonic-gate 	(void) sigset(SIGPIPE, SIG_IGN);
19207c478bd9Sstevel@tonic-gate 	/*
19217c478bd9Sstevel@tonic-gate 	 * Stop using stderr
19227c478bd9Sstevel@tonic-gate 	 */
19237c478bd9Sstevel@tonic-gate 	zlogp = &shstate->log;
19247c478bd9Sstevel@tonic-gate 
19257c478bd9Sstevel@tonic-gate 	/*
19267c478bd9Sstevel@tonic-gate 	 * We don't need stdout/stderr from now on.
19277c478bd9Sstevel@tonic-gate 	 */
19287c478bd9Sstevel@tonic-gate 	closefrom(0);
19297c478bd9Sstevel@tonic-gate 
19307c478bd9Sstevel@tonic-gate 	/*
19317c478bd9Sstevel@tonic-gate 	 * Initialize the syslog zlog_t.  This needs to be done after
19327c478bd9Sstevel@tonic-gate 	 * the call to closefrom().
19337c478bd9Sstevel@tonic-gate 	 */
19347c478bd9Sstevel@tonic-gate 	logsys.buf = logsys.log = NULL;
19357c478bd9Sstevel@tonic-gate 	logsys.buflen = logsys.loglen = 0;
19367c478bd9Sstevel@tonic-gate 	logsys.logfile = NULL;
19377c478bd9Sstevel@tonic-gate 	logsys.locale = DEFAULT_LOCALE;
19387c478bd9Sstevel@tonic-gate 
19397c478bd9Sstevel@tonic-gate 	openlog("zoneadmd", LOG_PID, LOG_DAEMON);
19407c478bd9Sstevel@tonic-gate 
19417c478bd9Sstevel@tonic-gate 	/*
19427c478bd9Sstevel@tonic-gate 	 * The eventstream is used to publish state changes in the zone
19437c478bd9Sstevel@tonic-gate 	 * from the door threads to the console I/O poller.
19447c478bd9Sstevel@tonic-gate 	 */
19457c478bd9Sstevel@tonic-gate 	if (eventstream_init() == -1) {
19467c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "unable to create eventstream");
19477c478bd9Sstevel@tonic-gate 		goto child_out;
19487c478bd9Sstevel@tonic-gate 	}
19497c478bd9Sstevel@tonic-gate 
19507c478bd9Sstevel@tonic-gate 	(void) snprintf(zone_door_path, sizeof (zone_door_path),
1951108322fbScarlsonj 	    "%s" ZONE_DOOR_PATH, zonecfg_get_root(), zone_name);
19527c478bd9Sstevel@tonic-gate 
19537c478bd9Sstevel@tonic-gate 	/*
19547c478bd9Sstevel@tonic-gate 	 * See if another zoneadmd is running for this zone.  If not, then we
19557c478bd9Sstevel@tonic-gate 	 * can now modify system state.
19567c478bd9Sstevel@tonic-gate 	 */
19577c478bd9Sstevel@tonic-gate 	if (make_daemon_exclusive(zlogp) == -1)
19587c478bd9Sstevel@tonic-gate 		goto child_out;
19597c478bd9Sstevel@tonic-gate 
19607c478bd9Sstevel@tonic-gate 
19617c478bd9Sstevel@tonic-gate 	/*
19627c478bd9Sstevel@tonic-gate 	 * Create/join a new session; we need to be careful of what we do with
19637c478bd9Sstevel@tonic-gate 	 * the console from now on so we don't end up being the session leader
19647c478bd9Sstevel@tonic-gate 	 * for the terminal we're going to be handing out.
19657c478bd9Sstevel@tonic-gate 	 */
19667c478bd9Sstevel@tonic-gate 	(void) setsid();
19677c478bd9Sstevel@tonic-gate 
19687c478bd9Sstevel@tonic-gate 	/*
19697c478bd9Sstevel@tonic-gate 	 * This thread shouldn't be receiving any signals; in particular,
19707c478bd9Sstevel@tonic-gate 	 * SIGCHLD should be received by the thread doing the fork().
19717c478bd9Sstevel@tonic-gate 	 */
19727c478bd9Sstevel@tonic-gate 	(void) sigfillset(&blockset);
19737c478bd9Sstevel@tonic-gate 	(void) thr_sigsetmask(SIG_BLOCK, &blockset, NULL);
19747c478bd9Sstevel@tonic-gate 
19757c478bd9Sstevel@tonic-gate 	/*
19767c478bd9Sstevel@tonic-gate 	 * Setup the console device and get ready to serve the console;
19777c478bd9Sstevel@tonic-gate 	 * once this has completed, we're ready to let console clients
19787c478bd9Sstevel@tonic-gate 	 * make an attempt to connect (they will block until
19797c478bd9Sstevel@tonic-gate 	 * serve_console_sock() below gets called, and any pending
19807c478bd9Sstevel@tonic-gate 	 * connection is accept()ed).
19817c478bd9Sstevel@tonic-gate 	 */
19829d5056eaSjv227347 	if (!zonecfg_in_alt_root() && init_console(zlogp) < 0)
19837c478bd9Sstevel@tonic-gate 		goto child_out;
19847c478bd9Sstevel@tonic-gate 
19857c478bd9Sstevel@tonic-gate 	/*
19867c478bd9Sstevel@tonic-gate 	 * Take the lock now, so that when the door server gets going, we
19877c478bd9Sstevel@tonic-gate 	 * are guaranteed that it won't take a request until we are sure
19887c478bd9Sstevel@tonic-gate 	 * that everything is completely set up.  See the child_out: label
19897c478bd9Sstevel@tonic-gate 	 * below to see why this matters.
19907c478bd9Sstevel@tonic-gate 	 */
19917c478bd9Sstevel@tonic-gate 	(void) mutex_lock(&lock);
19927c478bd9Sstevel@tonic-gate 
1993108322fbScarlsonj 	/* Init semaphore for scratch zones. */
1994108322fbScarlsonj 	if (sema_init(&scratch_sem, 0, USYNC_THREAD, NULL) == -1) {
1995108322fbScarlsonj 		zerror(zlogp, B_TRUE,
1996108322fbScarlsonj 		    "failed to initialize semaphore for scratch zone");
1997108322fbScarlsonj 		goto child_out;
1998108322fbScarlsonj 	}
1999108322fbScarlsonj 
20004ac67f02SAnurag S. Maskey 	/* open the dladm handle */
20014ac67f02SAnurag S. Maskey 	if (dladm_open(&dld_handle) != DLADM_STATUS_OK) {
20024ac67f02SAnurag S. Maskey 		zerror(zlogp, B_FALSE, "failed to open dladm handle");
20034ac67f02SAnurag S. Maskey 		goto child_out;
20044ac67f02SAnurag S. Maskey 	}
20054ac67f02SAnurag S. Maskey 
20067c478bd9Sstevel@tonic-gate 	/*
20077c478bd9Sstevel@tonic-gate 	 * Note: door setup must occur *after* the console is setup.
20087c478bd9Sstevel@tonic-gate 	 * This is so that as zlogin tests the door to see if zoneadmd
20097c478bd9Sstevel@tonic-gate 	 * is ready yet, we know that the console will get serviced
20107c478bd9Sstevel@tonic-gate 	 * once door_info() indicates that the door is "up".
20117c478bd9Sstevel@tonic-gate 	 */
20127c478bd9Sstevel@tonic-gate 	if (setup_door(zlogp) == -1)
20137c478bd9Sstevel@tonic-gate 		goto child_out;
20147c478bd9Sstevel@tonic-gate 
20157c478bd9Sstevel@tonic-gate 	/*
20167c478bd9Sstevel@tonic-gate 	 * Things seem OK so far; tell the parent process that we're done
20177c478bd9Sstevel@tonic-gate 	 * with setup tasks.  This will cause the parent to exit, signalling
20187c478bd9Sstevel@tonic-gate 	 * to zoneadm, zlogin, or whatever forked it that we are ready to
20197c478bd9Sstevel@tonic-gate 	 * service requests.
20207c478bd9Sstevel@tonic-gate 	 */
20217c478bd9Sstevel@tonic-gate 	shstate->status = 0;
20227c478bd9Sstevel@tonic-gate 	(void) sema_post(&shstate->sem);
20237c478bd9Sstevel@tonic-gate 	(void) munmap((char *)shstate, shstatelen);
20247c478bd9Sstevel@tonic-gate 	shstate = NULL;
20257c478bd9Sstevel@tonic-gate 
20267c478bd9Sstevel@tonic-gate 	(void) mutex_unlock(&lock);
20277c478bd9Sstevel@tonic-gate 
20287c478bd9Sstevel@tonic-gate 	/*
20297c478bd9Sstevel@tonic-gate 	 * zlogp is now invalid, so reset it to the syslog logger.
20307c478bd9Sstevel@tonic-gate 	 */
20317c478bd9Sstevel@tonic-gate 	zlogp = &logsys;
20327c478bd9Sstevel@tonic-gate 
20337c478bd9Sstevel@tonic-gate 	/*
20347c478bd9Sstevel@tonic-gate 	 * Now that we are free of any parents, switch to the default locale.
20357c478bd9Sstevel@tonic-gate 	 */
20367c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, DEFAULT_LOCALE);
20377c478bd9Sstevel@tonic-gate 
20387c478bd9Sstevel@tonic-gate 	/*
20397c478bd9Sstevel@tonic-gate 	 * At this point the setup portion of main() is basically done, so
20407c478bd9Sstevel@tonic-gate 	 * we reuse this thread to manage the zone console.  When
20417c478bd9Sstevel@tonic-gate 	 * serve_console() has returned, we are past the point of no return
20427c478bd9Sstevel@tonic-gate 	 * in the life of this zoneadmd.
20437c478bd9Sstevel@tonic-gate 	 */
2044108322fbScarlsonj 	if (zonecfg_in_alt_root()) {
2045108322fbScarlsonj 		/*
2046108322fbScarlsonj 		 * This is just awful, but mounted scratch zones don't (and
2047108322fbScarlsonj 		 * can't) have consoles.  We just wait for unmount instead.
2048108322fbScarlsonj 		 */
2049108322fbScarlsonj 		while (sema_wait(&scratch_sem) == EINTR)
2050108322fbScarlsonj 			;
2051108322fbScarlsonj 	} else {
20527c478bd9Sstevel@tonic-gate 		serve_console(zlogp);
20537c478bd9Sstevel@tonic-gate 		assert(in_death_throes);
2054108322fbScarlsonj 	}
20557c478bd9Sstevel@tonic-gate 
20567c478bd9Sstevel@tonic-gate 	/*
20577c478bd9Sstevel@tonic-gate 	 * This is the next-to-last part of the exit interlock.  Upon calling
20587c478bd9Sstevel@tonic-gate 	 * fdetach(), the door will go unreferenced; once any
20597c478bd9Sstevel@tonic-gate 	 * outstanding requests (like the door thread doing Z_HALT) are
20607c478bd9Sstevel@tonic-gate 	 * done, the door will get an UNREF notification; when it handles
2061f594b34cSEdward Pilatowicz 	 * the UNREF, the door server will cause the exit.  It's possible
2062f594b34cSEdward Pilatowicz 	 * that fdetach() can fail because the file is in use, in which
2063f594b34cSEdward Pilatowicz 	 * case we'll retry the operation.
20647c478bd9Sstevel@tonic-gate 	 */
20657c478bd9Sstevel@tonic-gate 	assert(!MUTEX_HELD(&lock));
2066f594b34cSEdward Pilatowicz 	for (;;) {
2067f594b34cSEdward Pilatowicz 		if ((fdetach(zone_door_path) == 0) || (errno != EBUSY))
2068f594b34cSEdward Pilatowicz 			break;
2069f594b34cSEdward Pilatowicz 		yield();
2070f594b34cSEdward Pilatowicz 	}
20714ac67f02SAnurag S. Maskey 
20727c478bd9Sstevel@tonic-gate 	for (;;)
20737c478bd9Sstevel@tonic-gate 		(void) pause();
20747c478bd9Sstevel@tonic-gate 
20757c478bd9Sstevel@tonic-gate child_out:
20767c478bd9Sstevel@tonic-gate 	assert(pid == 0);
20777c478bd9Sstevel@tonic-gate 	if (shstate != NULL) {
20787c478bd9Sstevel@tonic-gate 		shstate->status = -1;
20797c478bd9Sstevel@tonic-gate 		(void) sema_post(&shstate->sem);
20807c478bd9Sstevel@tonic-gate 		(void) munmap((char *)shstate, shstatelen);
20817c478bd9Sstevel@tonic-gate 	}
20827c478bd9Sstevel@tonic-gate 
20837c478bd9Sstevel@tonic-gate 	/*
20847c478bd9Sstevel@tonic-gate 	 * This might trigger an unref notification, but if so,
20857c478bd9Sstevel@tonic-gate 	 * we are still holding the lock, so our call to exit will
20867c478bd9Sstevel@tonic-gate 	 * ultimately win the race and will publish the right exit
20877c478bd9Sstevel@tonic-gate 	 * code.
20887c478bd9Sstevel@tonic-gate 	 */
20897c478bd9Sstevel@tonic-gate 	if (zone_door != -1) {
20907c478bd9Sstevel@tonic-gate 		assert(MUTEX_HELD(&lock));
20917c478bd9Sstevel@tonic-gate 		(void) door_revoke(zone_door);
20927c478bd9Sstevel@tonic-gate 		(void) fdetach(zone_door_path);
20937c478bd9Sstevel@tonic-gate 	}
20944ac67f02SAnurag S. Maskey 
20954ac67f02SAnurag S. Maskey 	if (dld_handle != NULL)
20964ac67f02SAnurag S. Maskey 		dladm_close(dld_handle);
20974ac67f02SAnurag S. Maskey 
20987c478bd9Sstevel@tonic-gate 	return (1); /* return from main() forcibly exits an MT process */
20997c478bd9Sstevel@tonic-gate }
2100