xref: /titanic_50/usr/src/cmd/zoneadmd/zoneadmd.c (revision 84561e8cee2e3c5e119f4cbbbf4674985c8defac)
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 /*
23e767a340Sgjelinek  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate /*
307c478bd9Sstevel@tonic-gate  * zoneadmd manages zones; one zoneadmd process is launched for each
317c478bd9Sstevel@tonic-gate  * non-global zone on the system.  This daemon juggles four jobs:
327c478bd9Sstevel@tonic-gate  *
337c478bd9Sstevel@tonic-gate  * - Implement setup and teardown of the zone "virtual platform": mount and
347c478bd9Sstevel@tonic-gate  *   unmount filesystems; create and destroy network interfaces; communicate
357c478bd9Sstevel@tonic-gate  *   with devfsadmd to lay out devices for the zone; instantiate the zone
367c478bd9Sstevel@tonic-gate  *   console device; configure process runtime attributes such as resource
377c478bd9Sstevel@tonic-gate  *   controls, pool bindings, fine-grained privileges.
387c478bd9Sstevel@tonic-gate  *
397c478bd9Sstevel@tonic-gate  * - Launch the zone's init(1M) process.
407c478bd9Sstevel@tonic-gate  *
417c478bd9Sstevel@tonic-gate  * - Implement a door server; clients (like zoneadm) connect to the door
427c478bd9Sstevel@tonic-gate  *   server and request zone state changes.  The kernel is also a client of
437c478bd9Sstevel@tonic-gate  *   this door server.  A request to halt or reboot the zone which originates
447c478bd9Sstevel@tonic-gate  *   *inside* the zone results in a door upcall from the kernel into zoneadmd.
457c478bd9Sstevel@tonic-gate  *
467c478bd9Sstevel@tonic-gate  *   One minor problem is that messages emitted by zoneadmd need to be passed
477c478bd9Sstevel@tonic-gate  *   back to the zoneadm process making the request.  These messages need to
487c478bd9Sstevel@tonic-gate  *   be rendered in the client's locale; so, this is passed in as part of the
497c478bd9Sstevel@tonic-gate  *   request.  The exception is the kernel upcall to zoneadmd, in which case
507c478bd9Sstevel@tonic-gate  *   messages are syslog'd.
517c478bd9Sstevel@tonic-gate  *
527c478bd9Sstevel@tonic-gate  *   To make all of this work, the Makefile adds -a to xgettext to extract *all*
537c478bd9Sstevel@tonic-gate  *   strings, and an exclusion file (zoneadmd.xcl) is used to exclude those
547c478bd9Sstevel@tonic-gate  *   strings which do not need to be translated.
557c478bd9Sstevel@tonic-gate  *
567c478bd9Sstevel@tonic-gate  * - Act as a console server for zlogin -C processes; see comments in zcons.c
577c478bd9Sstevel@tonic-gate  *   for more information about the zone console architecture.
587c478bd9Sstevel@tonic-gate  *
597c478bd9Sstevel@tonic-gate  * DESIGN NOTES
607c478bd9Sstevel@tonic-gate  *
617c478bd9Sstevel@tonic-gate  * Restart:
627c478bd9Sstevel@tonic-gate  *   A chief design constraint of zoneadmd is that it should be restartable in
637c478bd9Sstevel@tonic-gate  *   the case that the administrator kills it off, or it suffers a fatal error,
647c478bd9Sstevel@tonic-gate  *   without the running zone being impacted; this is akin to being able to
657c478bd9Sstevel@tonic-gate  *   reboot the service processor of a server without affecting the OS instance.
667c478bd9Sstevel@tonic-gate  */
677c478bd9Sstevel@tonic-gate 
687c478bd9Sstevel@tonic-gate #include <sys/param.h>
697c478bd9Sstevel@tonic-gate #include <sys/mman.h>
707c478bd9Sstevel@tonic-gate #include <sys/types.h>
717c478bd9Sstevel@tonic-gate #include <sys/stat.h>
727c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
737c478bd9Sstevel@tonic-gate 
747c478bd9Sstevel@tonic-gate #include <bsm/adt.h>
757c478bd9Sstevel@tonic-gate #include <bsm/adt_event.h>
767c478bd9Sstevel@tonic-gate 
777c478bd9Sstevel@tonic-gate #include <alloca.h>
787c478bd9Sstevel@tonic-gate #include <assert.h>
797c478bd9Sstevel@tonic-gate #include <errno.h>
807c478bd9Sstevel@tonic-gate #include <door.h>
817c478bd9Sstevel@tonic-gate #include <fcntl.h>
827c478bd9Sstevel@tonic-gate #include <locale.h>
837c478bd9Sstevel@tonic-gate #include <signal.h>
847c478bd9Sstevel@tonic-gate #include <stdarg.h>
857c478bd9Sstevel@tonic-gate #include <stdio.h>
867c478bd9Sstevel@tonic-gate #include <stdlib.h>
877c478bd9Sstevel@tonic-gate #include <string.h>
887c478bd9Sstevel@tonic-gate #include <strings.h>
897c478bd9Sstevel@tonic-gate #include <synch.h>
907c478bd9Sstevel@tonic-gate #include <syslog.h>
917c478bd9Sstevel@tonic-gate #include <thread.h>
927c478bd9Sstevel@tonic-gate #include <unistd.h>
937c478bd9Sstevel@tonic-gate #include <wait.h>
947c478bd9Sstevel@tonic-gate #include <limits.h>
957c478bd9Sstevel@tonic-gate #include <zone.h>
969acbbeafSnn35248 #include <libbrand.h>
977c478bd9Sstevel@tonic-gate #include <libcontract.h>
987c478bd9Sstevel@tonic-gate #include <libcontract_priv.h>
997c478bd9Sstevel@tonic-gate #include <sys/contract/process.h>
1007c478bd9Sstevel@tonic-gate #include <sys/ctfs.h>
1017c478bd9Sstevel@tonic-gate 
1027c478bd9Sstevel@tonic-gate #include <libzonecfg.h>
1037c478bd9Sstevel@tonic-gate #include "zoneadmd.h"
1047c478bd9Sstevel@tonic-gate 
1057c478bd9Sstevel@tonic-gate static char *progname;
1067c478bd9Sstevel@tonic-gate char *zone_name;	/* zone which we are managing */
1079acbbeafSnn35248 char brand_name[MAXNAMELEN];
1089acbbeafSnn35248 boolean_t zone_isnative;
109*84561e8cStd153743 boolean_t zone_iscluster;
110108322fbScarlsonj static zoneid_t zone_id;
1117c478bd9Sstevel@tonic-gate 
11222321485Svp157776 zlog_t logsys;
1137c478bd9Sstevel@tonic-gate 
1147c478bd9Sstevel@tonic-gate mutex_t	lock = DEFAULTMUTEX;	/* to serialize stuff */
1157c478bd9Sstevel@tonic-gate mutex_t	msglock = DEFAULTMUTEX;	/* for calling setlocale() */
1167c478bd9Sstevel@tonic-gate 
117108322fbScarlsonj static sema_t scratch_sem;	/* for scratch zones */
118108322fbScarlsonj 
1197c478bd9Sstevel@tonic-gate static char	zone_door_path[MAXPATHLEN];
1207c478bd9Sstevel@tonic-gate static int	zone_door = -1;
1217c478bd9Sstevel@tonic-gate 
1227c478bd9Sstevel@tonic-gate boolean_t in_death_throes = B_FALSE;	/* daemon is dying */
1237c478bd9Sstevel@tonic-gate boolean_t bringup_failure_recovery = B_FALSE; /* ignore certain failures */
1247c478bd9Sstevel@tonic-gate 
1257c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)		/* should be defined by cc -D */
1267c478bd9Sstevel@tonic-gate #define	TEXT_DOMAIN	"SYS_TEST"	/* Use this only if it wasn't */
1277c478bd9Sstevel@tonic-gate #endif
1287c478bd9Sstevel@tonic-gate 
1297c478bd9Sstevel@tonic-gate #define	DEFAULT_LOCALE	"C"
1307c478bd9Sstevel@tonic-gate 
131108322fbScarlsonj static const char *
132108322fbScarlsonj z_cmd_name(zone_cmd_t zcmd)
133108322fbScarlsonj {
134108322fbScarlsonj 	/* This list needs to match the enum in sys/zone.h */
135108322fbScarlsonj 	static const char *zcmdstr[] = {
1369acbbeafSnn35248 		"ready", "boot", "forceboot", "reboot", "halt",
1379acbbeafSnn35248 		"note_uninstalling", "mount", "forcemount", "unmount"
138108322fbScarlsonj 	};
139108322fbScarlsonj 
140108322fbScarlsonj 	if (zcmd >= sizeof (zcmdstr) / sizeof (*zcmdstr))
141108322fbScarlsonj 		return ("unknown");
142108322fbScarlsonj 	else
143108322fbScarlsonj 		return (zcmdstr[(int)zcmd]);
144108322fbScarlsonj }
145108322fbScarlsonj 
1467c478bd9Sstevel@tonic-gate static char *
1477c478bd9Sstevel@tonic-gate get_execbasename(char *execfullname)
1487c478bd9Sstevel@tonic-gate {
1497c478bd9Sstevel@tonic-gate 	char *last_slash, *execbasename;
1507c478bd9Sstevel@tonic-gate 
1517c478bd9Sstevel@tonic-gate 	/* guard against '/' at end of command invocation */
1527c478bd9Sstevel@tonic-gate 	for (;;) {
1537c478bd9Sstevel@tonic-gate 		last_slash = strrchr(execfullname, '/');
1547c478bd9Sstevel@tonic-gate 		if (last_slash == NULL) {
1557c478bd9Sstevel@tonic-gate 			execbasename = execfullname;
1567c478bd9Sstevel@tonic-gate 			break;
1577c478bd9Sstevel@tonic-gate 		} else {
1587c478bd9Sstevel@tonic-gate 			execbasename = last_slash + 1;
1597c478bd9Sstevel@tonic-gate 			if (*execbasename == '\0') {
1607c478bd9Sstevel@tonic-gate 				*last_slash = '\0';
1617c478bd9Sstevel@tonic-gate 				continue;
1627c478bd9Sstevel@tonic-gate 			}
1637c478bd9Sstevel@tonic-gate 			break;
1647c478bd9Sstevel@tonic-gate 		}
1657c478bd9Sstevel@tonic-gate 	}
1667c478bd9Sstevel@tonic-gate 	return (execbasename);
1677c478bd9Sstevel@tonic-gate }
1687c478bd9Sstevel@tonic-gate 
1697c478bd9Sstevel@tonic-gate static void
1707c478bd9Sstevel@tonic-gate usage(void)
1717c478bd9Sstevel@tonic-gate {
1727c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, gettext("Usage: %s -z zonename\n"), progname);
1737c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr,
1747c478bd9Sstevel@tonic-gate 	    gettext("\tNote: %s should not be run directly.\n"), progname);
1757c478bd9Sstevel@tonic-gate 	exit(2);
1767c478bd9Sstevel@tonic-gate }
1777c478bd9Sstevel@tonic-gate 
1787c478bd9Sstevel@tonic-gate /* ARGSUSED */
1797c478bd9Sstevel@tonic-gate static void
1807c478bd9Sstevel@tonic-gate sigchld(int sig)
1817c478bd9Sstevel@tonic-gate {
1827c478bd9Sstevel@tonic-gate }
1837c478bd9Sstevel@tonic-gate 
1847c478bd9Sstevel@tonic-gate char *
1857c478bd9Sstevel@tonic-gate localize_msg(char *locale, const char *msg)
1867c478bd9Sstevel@tonic-gate {
1877c478bd9Sstevel@tonic-gate 	char *out;
1887c478bd9Sstevel@tonic-gate 
1897c478bd9Sstevel@tonic-gate 	(void) mutex_lock(&msglock);
1907c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_MESSAGES, locale);
1917c478bd9Sstevel@tonic-gate 	out = gettext(msg);
1927c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_MESSAGES, DEFAULT_LOCALE);
1937c478bd9Sstevel@tonic-gate 	(void) mutex_unlock(&msglock);
1947c478bd9Sstevel@tonic-gate 	return (out);
1957c478bd9Sstevel@tonic-gate }
1967c478bd9Sstevel@tonic-gate 
1977c478bd9Sstevel@tonic-gate /* PRINTFLIKE3 */
1987c478bd9Sstevel@tonic-gate void
1997c478bd9Sstevel@tonic-gate zerror(zlog_t *zlogp, boolean_t use_strerror, const char *fmt, ...)
2007c478bd9Sstevel@tonic-gate {
2017c478bd9Sstevel@tonic-gate 	va_list alist;
2027c478bd9Sstevel@tonic-gate 	char buf[MAXPATHLEN * 2]; /* enough space for err msg with a path */
2037c478bd9Sstevel@tonic-gate 	char *bp;
2047c478bd9Sstevel@tonic-gate 	int saved_errno = errno;
2057c478bd9Sstevel@tonic-gate 
2067c478bd9Sstevel@tonic-gate 	if (zlogp == NULL)
2077c478bd9Sstevel@tonic-gate 		return;
2087c478bd9Sstevel@tonic-gate 	if (zlogp == &logsys)
2097c478bd9Sstevel@tonic-gate 		(void) snprintf(buf, sizeof (buf), "[zone '%s'] ",
2107c478bd9Sstevel@tonic-gate 		    zone_name);
2117c478bd9Sstevel@tonic-gate 	else
2127c478bd9Sstevel@tonic-gate 		buf[0] = '\0';
2137c478bd9Sstevel@tonic-gate 	bp = &(buf[strlen(buf)]);
2147c478bd9Sstevel@tonic-gate 
2157c478bd9Sstevel@tonic-gate 	/*
2167c478bd9Sstevel@tonic-gate 	 * In theory, the locale pointer should be set to either "C" or a
2177c478bd9Sstevel@tonic-gate 	 * char array, so it should never be NULL
2187c478bd9Sstevel@tonic-gate 	 */
2197c478bd9Sstevel@tonic-gate 	assert(zlogp->locale != NULL);
2207c478bd9Sstevel@tonic-gate 	/* Locale is per process, but we are multi-threaded... */
2217c478bd9Sstevel@tonic-gate 	fmt = localize_msg(zlogp->locale, fmt);
2227c478bd9Sstevel@tonic-gate 
2237c478bd9Sstevel@tonic-gate 	va_start(alist, fmt);
2247c478bd9Sstevel@tonic-gate 	(void) vsnprintf(bp, sizeof (buf) - (bp - buf), fmt, alist);
2257c478bd9Sstevel@tonic-gate 	va_end(alist);
2267c478bd9Sstevel@tonic-gate 	bp = &(buf[strlen(buf)]);
2277c478bd9Sstevel@tonic-gate 	if (use_strerror)
2287c478bd9Sstevel@tonic-gate 		(void) snprintf(bp, sizeof (buf) - (bp - buf), ": %s",
2297c478bd9Sstevel@tonic-gate 		    strerror(saved_errno));
2307c478bd9Sstevel@tonic-gate 	if (zlogp == &logsys) {
2317c478bd9Sstevel@tonic-gate 		(void) syslog(LOG_ERR, "%s", buf);
2327c478bd9Sstevel@tonic-gate 	} else if (zlogp->logfile != NULL) {
2337c478bd9Sstevel@tonic-gate 		(void) fprintf(zlogp->logfile, "%s\n", buf);
2347c478bd9Sstevel@tonic-gate 	} else {
2357c478bd9Sstevel@tonic-gate 		size_t buflen;
2367c478bd9Sstevel@tonic-gate 		size_t copylen;
2377c478bd9Sstevel@tonic-gate 
2387c478bd9Sstevel@tonic-gate 		buflen = snprintf(zlogp->log, zlogp->loglen, "%s\n", buf);
2397c478bd9Sstevel@tonic-gate 		copylen = MIN(buflen, zlogp->loglen);
2407c478bd9Sstevel@tonic-gate 		zlogp->log += copylen;
2417c478bd9Sstevel@tonic-gate 		zlogp->loglen -= copylen;
2427c478bd9Sstevel@tonic-gate 	}
2437c478bd9Sstevel@tonic-gate }
2447c478bd9Sstevel@tonic-gate 
2453f2f09c1Sdp /*
2463f2f09c1Sdp  * Emit a warning for any boot arguments which are unrecognized.  Since
2473f2f09c1Sdp  * Solaris boot arguments are getopt(3c) compatible (see kernel(1m)), we
2483f2f09c1Sdp  * put the arguments into an argv style array, use getopt to process them,
2493f2f09c1Sdp  * and put the resultant argument string back into outargs.
2503f2f09c1Sdp  *
2513f2f09c1Sdp  * During the filtering, we pull out any arguments which are truly "boot"
2523f2f09c1Sdp  * arguments, leaving only those which are to be passed intact to the
2533f2f09c1Sdp  * progenitor process.  The one we support at the moment is -i, which
2543f2f09c1Sdp  * indicates to the kernel which program should be launched as 'init'.
2553f2f09c1Sdp  *
2563f2f09c1Sdp  * A return of Z_INVAL indicates specifically that the arguments are
2573f2f09c1Sdp  * not valid; this is a non-fatal error.  Except for Z_OK, all other return
2583f2f09c1Sdp  * values are treated as fatal.
2593f2f09c1Sdp  */
2603f2f09c1Sdp static int
2613f2f09c1Sdp filter_bootargs(zlog_t *zlogp, const char *inargs, char *outargs,
2623f2f09c1Sdp     char *init_file, char *badarg)
2633f2f09c1Sdp {
2643f2f09c1Sdp 	int argc = 0, argc_save;
2653f2f09c1Sdp 	int i;
2663f2f09c1Sdp 	int err;
2673f2f09c1Sdp 	char *arg, *lasts, **argv = NULL, **argv_save;
2683f2f09c1Sdp 	char zonecfg_args[BOOTARGS_MAX];
2693f2f09c1Sdp 	char scratchargs[BOOTARGS_MAX], *sargs;
2703f2f09c1Sdp 	char c;
2713f2f09c1Sdp 
2723f2f09c1Sdp 	bzero(outargs, BOOTARGS_MAX);
2733f2f09c1Sdp 	bzero(badarg, BOOTARGS_MAX);
2743f2f09c1Sdp 
2753f2f09c1Sdp 	/*
2763f2f09c1Sdp 	 * If the user didn't specify transient boot arguments, check
2773f2f09c1Sdp 	 * to see if there were any specified in the zone configuration,
2783f2f09c1Sdp 	 * and use them if applicable.
2793f2f09c1Sdp 	 */
2803f2f09c1Sdp 	if (inargs == NULL || inargs[0] == '\0')  {
2813f2f09c1Sdp 		zone_dochandle_t handle;
2823f2f09c1Sdp 		if ((handle = zonecfg_init_handle()) == NULL) {
2833f2f09c1Sdp 			zerror(zlogp, B_TRUE,
2843f2f09c1Sdp 			    "getting zone configuration handle");
2853f2f09c1Sdp 			return (Z_BAD_HANDLE);
2863f2f09c1Sdp 		}
2873f2f09c1Sdp 		err = zonecfg_get_snapshot_handle(zone_name, handle);
2883f2f09c1Sdp 		if (err != Z_OK) {
2893f2f09c1Sdp 			zerror(zlogp, B_FALSE,
2903f2f09c1Sdp 			    "invalid configuration snapshot");
2913f2f09c1Sdp 			zonecfg_fini_handle(handle);
2923f2f09c1Sdp 			return (Z_BAD_HANDLE);
2933f2f09c1Sdp 		}
2943f2f09c1Sdp 
2953f2f09c1Sdp 		bzero(zonecfg_args, sizeof (zonecfg_args));
2963f2f09c1Sdp 		(void) zonecfg_get_bootargs(handle, zonecfg_args,
2973f2f09c1Sdp 		    sizeof (zonecfg_args));
2983f2f09c1Sdp 		inargs = zonecfg_args;
2993f2f09c1Sdp 		zonecfg_fini_handle(handle);
3003f2f09c1Sdp 	}
3013f2f09c1Sdp 
3023f2f09c1Sdp 	if (strlen(inargs) >= BOOTARGS_MAX) {
3033f2f09c1Sdp 		zerror(zlogp, B_FALSE, "boot argument string too long");
3043f2f09c1Sdp 		return (Z_INVAL);
3053f2f09c1Sdp 	}
3063f2f09c1Sdp 
3073f2f09c1Sdp 	(void) strlcpy(scratchargs, inargs, sizeof (scratchargs));
3083f2f09c1Sdp 	sargs = scratchargs;
3093f2f09c1Sdp 	while ((arg = strtok_r(sargs, " \t", &lasts)) != NULL) {
3103f2f09c1Sdp 		sargs = NULL;
3113f2f09c1Sdp 		argc++;
3123f2f09c1Sdp 	}
3133f2f09c1Sdp 
3143f2f09c1Sdp 	if ((argv = calloc(argc + 1, sizeof (char *))) == NULL) {
3153f2f09c1Sdp 		zerror(zlogp, B_FALSE, "memory allocation failed");
3163f2f09c1Sdp 		return (Z_NOMEM);
3173f2f09c1Sdp 	}
3183f2f09c1Sdp 
3193f2f09c1Sdp 	argv_save = argv;
3203f2f09c1Sdp 	argc_save = argc;
3213f2f09c1Sdp 
3223f2f09c1Sdp 	(void) strlcpy(scratchargs, inargs, sizeof (scratchargs));
3233f2f09c1Sdp 	sargs = scratchargs;
3243f2f09c1Sdp 	i = 0;
3253f2f09c1Sdp 	while ((arg = strtok_r(sargs, " \t", &lasts)) != NULL) {
3263f2f09c1Sdp 		sargs = NULL;
3273f2f09c1Sdp 		if ((argv[i] = strdup(arg)) == NULL) {
3283f2f09c1Sdp 			err = Z_NOMEM;
3293f2f09c1Sdp 			zerror(zlogp, B_FALSE, "memory allocation failed");
3303f2f09c1Sdp 			goto done;
3313f2f09c1Sdp 		}
3323f2f09c1Sdp 		i++;
3333f2f09c1Sdp 	}
3343f2f09c1Sdp 
3353f2f09c1Sdp 	/*
3363f2f09c1Sdp 	 * We preserve compatibility with the Solaris system boot behavior,
3373f2f09c1Sdp 	 * which allows:
3383f2f09c1Sdp 	 *
3393f2f09c1Sdp 	 * 	# reboot kernel/unix -s -m verbose
3403f2f09c1Sdp 	 *
3413f2f09c1Sdp 	 * In this example, kernel/unix tells the booter what file to
3423f2f09c1Sdp 	 * boot.  We don't want reboot in a zone to be gratuitously different,
3433f2f09c1Sdp 	 * so we silently ignore the boot file, if necessary.
3443f2f09c1Sdp 	 */
3453f2f09c1Sdp 	if (argv[0] == NULL)
3463f2f09c1Sdp 		goto done;
3473f2f09c1Sdp 
3483f2f09c1Sdp 	assert(argv[0][0] != ' ');
3493f2f09c1Sdp 	assert(argv[0][0] != '\t');
3503f2f09c1Sdp 
3513f2f09c1Sdp 	if (argv[0][0] != '-' && argv[0][0] != '\0') {
3523f2f09c1Sdp 		argv = &argv[1];
3533f2f09c1Sdp 		argc--;
3543f2f09c1Sdp 	}
3553f2f09c1Sdp 
3563f2f09c1Sdp 	optind = 0;
3573f2f09c1Sdp 	opterr = 0;
3583f2f09c1Sdp 	err = Z_OK;
3599acbbeafSnn35248 	while ((c = getopt(argc, argv, "fi:m:s")) != -1) {
3603f2f09c1Sdp 		switch (c) {
3613f2f09c1Sdp 		case 'i':
3623f2f09c1Sdp 			/*
3633f2f09c1Sdp 			 * -i is handled by the runtime and is not passed
3643f2f09c1Sdp 			 * along to userland
3653f2f09c1Sdp 			 */
3663f2f09c1Sdp 			(void) strlcpy(init_file, optarg, MAXPATHLEN);
3673f2f09c1Sdp 			break;
3689acbbeafSnn35248 		case 'f':
3699acbbeafSnn35248 			/* This has already been processed by zoneadm */
3709acbbeafSnn35248 			break;
3713f2f09c1Sdp 		case 'm':
3723f2f09c1Sdp 		case 's':
3733f2f09c1Sdp 			/* These pass through unmolested */
3743f2f09c1Sdp 			(void) snprintf(outargs, BOOTARGS_MAX,
3753f2f09c1Sdp 			    "%s -%c %s ", outargs, c, optarg ? optarg : "");
3763f2f09c1Sdp 			break;
3773f2f09c1Sdp 		case '?':
3783f2f09c1Sdp 			/*
3793f2f09c1Sdp 			 * We warn about unknown arguments but pass them
3803f2f09c1Sdp 			 * along anyway-- if someone wants to develop their
3813f2f09c1Sdp 			 * own init replacement, they can pass it whatever
3823f2f09c1Sdp 			 * args they want.
3833f2f09c1Sdp 			 */
3843f2f09c1Sdp 			err = Z_INVAL;
3853f2f09c1Sdp 			(void) snprintf(outargs, BOOTARGS_MAX,
3863f2f09c1Sdp 			    "%s -%c", outargs, optopt);
3873f2f09c1Sdp 			(void) snprintf(badarg, BOOTARGS_MAX,
3883f2f09c1Sdp 			    "%s -%c", badarg, optopt);
3893f2f09c1Sdp 			break;
3903f2f09c1Sdp 		}
3913f2f09c1Sdp 	}
3923f2f09c1Sdp 
3933f2f09c1Sdp 	/*
3943f2f09c1Sdp 	 * For Solaris Zones we warn about and discard non-option arguments.
3953f2f09c1Sdp 	 * Hence 'boot foo bar baz gub' --> 'boot'.  However, to be similar
3963f2f09c1Sdp 	 * to the kernel, we concat up all the other remaining boot args.
3973f2f09c1Sdp 	 * and warn on them as a group.
3983f2f09c1Sdp 	 */
3993f2f09c1Sdp 	if (optind < argc) {
4003f2f09c1Sdp 		err = Z_INVAL;
4013f2f09c1Sdp 		while (optind < argc) {
4023f2f09c1Sdp 			(void) snprintf(badarg, BOOTARGS_MAX, "%s%s%s",
4033f2f09c1Sdp 			    badarg, strlen(badarg) > 0 ? " " : "",
4043f2f09c1Sdp 			    argv[optind]);
4053f2f09c1Sdp 			optind++;
4063f2f09c1Sdp 		}
4073f2f09c1Sdp 		zerror(zlogp, B_FALSE, "WARNING: Unused or invalid boot "
4083f2f09c1Sdp 		    "arguments `%s'.", badarg);
4093f2f09c1Sdp 	}
4103f2f09c1Sdp 
4113f2f09c1Sdp done:
4123f2f09c1Sdp 	for (i = 0; i < argc_save; i++) {
4133f2f09c1Sdp 		if (argv_save[i] != NULL)
4143f2f09c1Sdp 			free(argv_save[i]);
4153f2f09c1Sdp 	}
4163f2f09c1Sdp 	free(argv_save);
4173f2f09c1Sdp 	return (err);
4183f2f09c1Sdp }
4193f2f09c1Sdp 
4203f2f09c1Sdp 
4217c478bd9Sstevel@tonic-gate static int
4227c478bd9Sstevel@tonic-gate mkzonedir(zlog_t *zlogp)
4237c478bd9Sstevel@tonic-gate {
4247c478bd9Sstevel@tonic-gate 	struct stat st;
4257c478bd9Sstevel@tonic-gate 	/*
4267c478bd9Sstevel@tonic-gate 	 * We must create and lock everyone but root out of ZONES_TMPDIR
4277c478bd9Sstevel@tonic-gate 	 * since anyone can open any UNIX domain socket, regardless of
4287c478bd9Sstevel@tonic-gate 	 * its file system permissions.  Sigh...
4297c478bd9Sstevel@tonic-gate 	 */
4307c478bd9Sstevel@tonic-gate 	if (mkdir(ZONES_TMPDIR, S_IRWXU) < 0 && errno != EEXIST) {
4317c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "could not mkdir '%s'", ZONES_TMPDIR);
4327c478bd9Sstevel@tonic-gate 		return (-1);
4337c478bd9Sstevel@tonic-gate 	}
4347c478bd9Sstevel@tonic-gate 	/* paranoia */
4354bc0a2efScasper 	if ((stat(ZONES_TMPDIR, &st) < 0) || !S_ISDIR(st.st_mode)) {
4367c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "'%s' is not a directory", ZONES_TMPDIR);
4377c478bd9Sstevel@tonic-gate 		return (-1);
4387c478bd9Sstevel@tonic-gate 	}
4397c478bd9Sstevel@tonic-gate 	(void) chmod(ZONES_TMPDIR, S_IRWXU);
4407c478bd9Sstevel@tonic-gate 	return (0);
4417c478bd9Sstevel@tonic-gate }
4427c478bd9Sstevel@tonic-gate 
443108322fbScarlsonj /*
444108322fbScarlsonj  * Bring a zone up to the pre-boot "ready" stage.  The mount_cmd argument is
445108322fbScarlsonj  * 'true' if this is being invoked as part of the processing for the "mount"
446108322fbScarlsonj  * subcommand.
447108322fbScarlsonj  */
448108322fbScarlsonj static int
449108322fbScarlsonj zone_ready(zlog_t *zlogp, boolean_t mount_cmd)
4507c478bd9Sstevel@tonic-gate {
4517c478bd9Sstevel@tonic-gate 	int err;
4527c478bd9Sstevel@tonic-gate 
4537c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_create_snapshot(zone_name)) != Z_OK) {
4547c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "unable to create snapshot: %s",
4557c478bd9Sstevel@tonic-gate 		    zonecfg_strerror(err));
4567c478bd9Sstevel@tonic-gate 		return (-1);
4577c478bd9Sstevel@tonic-gate 	}
4587c478bd9Sstevel@tonic-gate 
459108322fbScarlsonj 	if ((zone_id = vplat_create(zlogp, mount_cmd)) == -1) {
4607c478bd9Sstevel@tonic-gate 		if ((err = zonecfg_destroy_snapshot(zone_name)) != Z_OK)
4617c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_FALSE, "destroying snapshot: %s",
4627c478bd9Sstevel@tonic-gate 			    zonecfg_strerror(err));
4637c478bd9Sstevel@tonic-gate 		return (-1);
4647c478bd9Sstevel@tonic-gate 	}
465555afedfScarlsonj 	if (vplat_bringup(zlogp, mount_cmd, zone_id) != 0) {
4667c478bd9Sstevel@tonic-gate 		bringup_failure_recovery = B_TRUE;
4670209230bSgjelinek 		(void) vplat_teardown(NULL, mount_cmd, B_FALSE);
4687c478bd9Sstevel@tonic-gate 		if ((err = zonecfg_destroy_snapshot(zone_name)) != Z_OK)
4697c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_FALSE, "destroying snapshot: %s",
4707c478bd9Sstevel@tonic-gate 			    zonecfg_strerror(err));
4717c478bd9Sstevel@tonic-gate 		return (-1);
4727c478bd9Sstevel@tonic-gate 	}
4737c478bd9Sstevel@tonic-gate 
4747c478bd9Sstevel@tonic-gate 	return (0);
4757c478bd9Sstevel@tonic-gate }
4767c478bd9Sstevel@tonic-gate 
477555afedfScarlsonj int
478555afedfScarlsonj init_template(void)
4797c478bd9Sstevel@tonic-gate {
4807c478bd9Sstevel@tonic-gate 	int fd;
4817c478bd9Sstevel@tonic-gate 	int err = 0;
4827c478bd9Sstevel@tonic-gate 
4837c478bd9Sstevel@tonic-gate 	fd = open64(CTFS_ROOT "/process/template", O_RDWR);
4847c478bd9Sstevel@tonic-gate 	if (fd == -1)
4857c478bd9Sstevel@tonic-gate 		return (-1);
4867c478bd9Sstevel@tonic-gate 
4877c478bd9Sstevel@tonic-gate 	/*
4887c478bd9Sstevel@tonic-gate 	 * For now, zoneadmd doesn't do anything with the contract.
4897c478bd9Sstevel@tonic-gate 	 * Deliver no events, don't inherit, and allow it to be orphaned.
4907c478bd9Sstevel@tonic-gate 	 */
4917c478bd9Sstevel@tonic-gate 	err |= ct_tmpl_set_critical(fd, 0);
4927c478bd9Sstevel@tonic-gate 	err |= ct_tmpl_set_informative(fd, 0);
4937c478bd9Sstevel@tonic-gate 	err |= ct_pr_tmpl_set_fatal(fd, CT_PR_EV_HWERR);
4947c478bd9Sstevel@tonic-gate 	err |= ct_pr_tmpl_set_param(fd, CT_PR_PGRPONLY | CT_PR_REGENT);
4957c478bd9Sstevel@tonic-gate 	if (err || ct_tmpl_activate(fd)) {
4967c478bd9Sstevel@tonic-gate 		(void) close(fd);
4977c478bd9Sstevel@tonic-gate 		return (-1);
4987c478bd9Sstevel@tonic-gate 	}
4997c478bd9Sstevel@tonic-gate 
5007c478bd9Sstevel@tonic-gate 	return (fd);
5017c478bd9Sstevel@tonic-gate }
5027c478bd9Sstevel@tonic-gate 
5039acbbeafSnn35248 typedef struct fs_callback {
5049acbbeafSnn35248 	zlog_t		*zlogp;
5059acbbeafSnn35248 	zoneid_t	zoneid;
5069acbbeafSnn35248 } fs_callback_t;
5079acbbeafSnn35248 
5087c478bd9Sstevel@tonic-gate static int
5099acbbeafSnn35248 mount_early_fs(void *data, const char *spec, const char *dir,
5109acbbeafSnn35248     const char *fstype, const char *opt)
5117c478bd9Sstevel@tonic-gate {
5129acbbeafSnn35248 	zlog_t *zlogp = ((fs_callback_t *)data)->zlogp;
5139acbbeafSnn35248 	zoneid_t zoneid = ((fs_callback_t *)data)->zoneid;
5147c478bd9Sstevel@tonic-gate 	pid_t child;
5157c478bd9Sstevel@tonic-gate 	int child_status;
5167c478bd9Sstevel@tonic-gate 	int tmpl_fd;
5177c478bd9Sstevel@tonic-gate 	ctid_t ct;
5187c478bd9Sstevel@tonic-gate 
5197c478bd9Sstevel@tonic-gate 	if ((tmpl_fd = init_template()) == -1) {
5207c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "failed to create contract");
5217c478bd9Sstevel@tonic-gate 		return (-1);
5227c478bd9Sstevel@tonic-gate 	}
5237c478bd9Sstevel@tonic-gate 
5247c478bd9Sstevel@tonic-gate 	if ((child = fork()) == -1) {
5257c478bd9Sstevel@tonic-gate 		(void) ct_tmpl_clear(tmpl_fd);
5267c478bd9Sstevel@tonic-gate 		(void) close(tmpl_fd);
5277c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "failed to fork");
5287c478bd9Sstevel@tonic-gate 		return (-1);
5297c478bd9Sstevel@tonic-gate 
5307c478bd9Sstevel@tonic-gate 	} else if (child == 0) {	/* child */
5319acbbeafSnn35248 		char opt_buf[MAX_MNTOPT_STR];
5329acbbeafSnn35248 		int optlen = 0;
5339acbbeafSnn35248 		int mflag = MS_DATA;
5349acbbeafSnn35248 
5357c478bd9Sstevel@tonic-gate 		(void) ct_tmpl_clear(tmpl_fd);
5367c478bd9Sstevel@tonic-gate 		/*
5377c478bd9Sstevel@tonic-gate 		 * Even though there are no procs running in the zone, we
5387c478bd9Sstevel@tonic-gate 		 * do this for paranoia's sake.
5397c478bd9Sstevel@tonic-gate 		 */
5407c478bd9Sstevel@tonic-gate 		(void) closefrom(0);
5417c478bd9Sstevel@tonic-gate 
5427c478bd9Sstevel@tonic-gate 		if (zone_enter(zoneid) == -1) {
5437c478bd9Sstevel@tonic-gate 			_exit(errno);
5447c478bd9Sstevel@tonic-gate 		}
5459acbbeafSnn35248 		if (opt != NULL) {
5469acbbeafSnn35248 			/*
5479acbbeafSnn35248 			 * The mount() system call is incredibly annoying.
5489acbbeafSnn35248 			 * If options are specified, we need to copy them
5499acbbeafSnn35248 			 * into a temporary buffer since the mount() system
5509acbbeafSnn35248 			 * call will overwrite the options string.  It will
5519acbbeafSnn35248 			 * also fail if the new option string it wants to
5529acbbeafSnn35248 			 * write is bigger than the one we passed in, so
5539acbbeafSnn35248 			 * you must pass in a buffer of the maximum possible
5549acbbeafSnn35248 			 * option string length.  sigh.
5559acbbeafSnn35248 			 */
5569acbbeafSnn35248 			(void) strlcpy(opt_buf, opt, sizeof (opt_buf));
5579acbbeafSnn35248 			opt = opt_buf;
5589acbbeafSnn35248 			optlen = MAX_MNTOPT_STR;
5599acbbeafSnn35248 			mflag = MS_OPTIONSTR;
5609acbbeafSnn35248 		}
5619acbbeafSnn35248 		if (mount(spec, dir, mflag, fstype, NULL, 0, opt, optlen) != 0)
5627c478bd9Sstevel@tonic-gate 			_exit(errno);
5637c478bd9Sstevel@tonic-gate 		_exit(0);
5647c478bd9Sstevel@tonic-gate 	}
5657c478bd9Sstevel@tonic-gate 
5667c478bd9Sstevel@tonic-gate 	/* parent */
5677c478bd9Sstevel@tonic-gate 	if (contract_latest(&ct) == -1)
5687c478bd9Sstevel@tonic-gate 		ct = -1;
5697c478bd9Sstevel@tonic-gate 	(void) ct_tmpl_clear(tmpl_fd);
5707c478bd9Sstevel@tonic-gate 	(void) close(tmpl_fd);
5717c478bd9Sstevel@tonic-gate 	if (waitpid(child, &child_status, 0) != child) {
5727c478bd9Sstevel@tonic-gate 		/* unexpected: we must have been signalled */
5737c478bd9Sstevel@tonic-gate 		(void) contract_abandon_id(ct);
5747c478bd9Sstevel@tonic-gate 		return (-1);
5757c478bd9Sstevel@tonic-gate 	}
5767c478bd9Sstevel@tonic-gate 	(void) contract_abandon_id(ct);
5777c478bd9Sstevel@tonic-gate 	if (WEXITSTATUS(child_status) != 0) {
5787c478bd9Sstevel@tonic-gate 		errno = WEXITSTATUS(child_status);
5797c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "mount of %s failed", dir);
5807c478bd9Sstevel@tonic-gate 		return (-1);
5817c478bd9Sstevel@tonic-gate 	}
5827c478bd9Sstevel@tonic-gate 
5837c478bd9Sstevel@tonic-gate 	return (0);
5847c478bd9Sstevel@tonic-gate }
5857c478bd9Sstevel@tonic-gate 
5869acbbeafSnn35248 int
5879acbbeafSnn35248 do_subproc(zlog_t *zlogp, char *cmdbuf)
588108322fbScarlsonj {
5899acbbeafSnn35248 	char inbuf[1024];	/* arbitrary large amount */
5909acbbeafSnn35248 	FILE *file;
5919acbbeafSnn35248 	int status;
592108322fbScarlsonj 
5939acbbeafSnn35248 	file = popen(cmdbuf, "r");
5949acbbeafSnn35248 	if (file == NULL) {
5959acbbeafSnn35248 		zerror(zlogp, B_TRUE, "could not launch: %s", cmdbuf);
596108322fbScarlsonj 		return (-1);
5979acbbeafSnn35248 	}
598108322fbScarlsonj 
5999acbbeafSnn35248 	while (fgets(inbuf, sizeof (inbuf), file) != NULL)
6009acbbeafSnn35248 		if (zlogp != &logsys)
6019acbbeafSnn35248 			zerror(zlogp, B_FALSE, "%s", inbuf);
6029acbbeafSnn35248 	status = pclose(file);
6039acbbeafSnn35248 
6049acbbeafSnn35248 	if (WIFSIGNALED(status)) {
6059acbbeafSnn35248 		zerror(zlogp, B_FALSE, "%s unexpectedly terminated due to "
6069acbbeafSnn35248 		    "signal %d", cmdbuf, WTERMSIG(status));
6075518d15bSdp 		return (-1);
6089acbbeafSnn35248 	}
6099acbbeafSnn35248 	assert(WIFEXITED(status));
6109acbbeafSnn35248 	if (WEXITSTATUS(status) == ZEXIT_EXEC) {
6119acbbeafSnn35248 		zerror(zlogp, B_FALSE, "failed to exec %s", cmdbuf);
612108322fbScarlsonj 		return (-1);
6139acbbeafSnn35248 	}
6149acbbeafSnn35248 	return (WEXITSTATUS(status));
615108322fbScarlsonj }
616108322fbScarlsonj 
617108322fbScarlsonj static int
6187c478bd9Sstevel@tonic-gate zone_bootup(zlog_t *zlogp, const char *bootargs)
6197c478bd9Sstevel@tonic-gate {
6207c478bd9Sstevel@tonic-gate 	zoneid_t zoneid;
6217c478bd9Sstevel@tonic-gate 	struct stat st;
6223f2f09c1Sdp 	char zroot[MAXPATHLEN], initpath[MAXPATHLEN], init_file[MAXPATHLEN];
6233f2f09c1Sdp 	char nbootargs[BOOTARGS_MAX];
6249acbbeafSnn35248 	char cmdbuf[MAXPATHLEN];
6259acbbeafSnn35248 	fs_callback_t cb;
626123807fbSedp 	brand_handle_t bh;
6273f2f09c1Sdp 	int err;
6287c478bd9Sstevel@tonic-gate 
6297c478bd9Sstevel@tonic-gate 	if (init_console_slave(zlogp) != 0)
6307c478bd9Sstevel@tonic-gate 		return (-1);
6317c478bd9Sstevel@tonic-gate 	reset_slave_terminal(zlogp);
6327c478bd9Sstevel@tonic-gate 
6337c478bd9Sstevel@tonic-gate 	if ((zoneid = getzoneidbyname(zone_name)) == -1) {
6347c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "unable to get zoneid");
6357c478bd9Sstevel@tonic-gate 		return (-1);
6367c478bd9Sstevel@tonic-gate 	}
6377c478bd9Sstevel@tonic-gate 
6389acbbeafSnn35248 	cb.zlogp = zlogp;
6399acbbeafSnn35248 	cb.zoneid = zoneid;
6409acbbeafSnn35248 
6419acbbeafSnn35248 	/* Get a handle to the brand info for this zone */
642123807fbSedp 	if ((bh = brand_open(brand_name)) == NULL) {
6439acbbeafSnn35248 		zerror(zlogp, B_FALSE, "unable to determine zone brand");
6447c478bd9Sstevel@tonic-gate 		return (-1);
6459acbbeafSnn35248 	}
6469acbbeafSnn35248 
6479acbbeafSnn35248 	/*
6489acbbeafSnn35248 	 * Get the list of filesystems to mount from the brand
6499acbbeafSnn35248 	 * configuration.  These mounts are done via a thread that will
6509acbbeafSnn35248 	 * enter the zone, so they are done from within the context of the
6519acbbeafSnn35248 	 * zone.
6529acbbeafSnn35248 	 */
653123807fbSedp 	if (brand_platform_iter_mounts(bh, mount_early_fs, &cb) != 0) {
6549acbbeafSnn35248 		zerror(zlogp, B_FALSE, "unable to mount filesystems");
655123807fbSedp 		brand_close(bh);
6569acbbeafSnn35248 		return (-1);
6579acbbeafSnn35248 	}
6589acbbeafSnn35248 
6599acbbeafSnn35248 	/*
6609acbbeafSnn35248 	 * Get the brand's boot callback if it exists.
6619acbbeafSnn35248 	 */
6629acbbeafSnn35248 	if (zone_get_zonepath(zone_name, zroot, sizeof (zroot)) != Z_OK) {
6639acbbeafSnn35248 		zerror(zlogp, B_FALSE, "unable to determine zone root");
664e767a340Sgjelinek 		brand_close(bh);
6659acbbeafSnn35248 		return (-1);
6669acbbeafSnn35248 	}
6679acbbeafSnn35248 	(void) strcpy(cmdbuf, EXEC_PREFIX);
668123807fbSedp 	if (brand_get_boot(bh, zone_name, zroot, cmdbuf + EXEC_LEN,
6699acbbeafSnn35248 	    sizeof (cmdbuf) - EXEC_LEN, 0, NULL) != 0) {
6709acbbeafSnn35248 		zerror(zlogp, B_FALSE,
6719acbbeafSnn35248 		    "unable to determine branded zone's boot callback");
672123807fbSedp 		brand_close(bh);
6739acbbeafSnn35248 		return (-1);
6749acbbeafSnn35248 	}
6759acbbeafSnn35248 
6769acbbeafSnn35248 	/* Get the path for this zone's init(1M) (or equivalent) process.  */
677123807fbSedp 	if (brand_get_initname(bh, init_file, MAXPATHLEN) != 0) {
6789acbbeafSnn35248 		zerror(zlogp, B_FALSE,
6799acbbeafSnn35248 		    "unable to determine zone's init(1M) location");
680123807fbSedp 		brand_close(bh);
6819acbbeafSnn35248 		return (-1);
6829acbbeafSnn35248 	}
6839acbbeafSnn35248 
684123807fbSedp 	brand_close(bh);
6857c478bd9Sstevel@tonic-gate 
6863f2f09c1Sdp 	err = filter_bootargs(zlogp, bootargs, nbootargs, init_file,
6873f2f09c1Sdp 	    bad_boot_arg);
6883f2f09c1Sdp 	if (err == Z_INVAL)
6893f2f09c1Sdp 		eventstream_write(Z_EVT_ZONE_BADARGS);
6903f2f09c1Sdp 	else if (err != Z_OK)
6913f2f09c1Sdp 		return (-1);
6923f2f09c1Sdp 
6933f2f09c1Sdp 	assert(init_file[0] != '\0');
6943f2f09c1Sdp 
6959acbbeafSnn35248 	/* Try to anticipate possible problems: Make sure init is executable. */
6967c478bd9Sstevel@tonic-gate 	if (zone_get_rootpath(zone_name, zroot, sizeof (zroot)) != Z_OK) {
6977c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "unable to determine zone root");
6987c478bd9Sstevel@tonic-gate 		return (-1);
6997c478bd9Sstevel@tonic-gate 	}
7009acbbeafSnn35248 
7013f2f09c1Sdp 	(void) snprintf(initpath, sizeof (initpath), "%s%s", zroot, init_file);
7027c478bd9Sstevel@tonic-gate 
7037c478bd9Sstevel@tonic-gate 	if (stat(initpath, &st) == -1) {
7047c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "could not stat %s", initpath);
7057c478bd9Sstevel@tonic-gate 		return (-1);
7067c478bd9Sstevel@tonic-gate 	}
7077c478bd9Sstevel@tonic-gate 
7087c478bd9Sstevel@tonic-gate 	if ((st.st_mode & S_IXUSR) == 0) {
7097c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "%s is not executable", initpath);
7107c478bd9Sstevel@tonic-gate 		return (-1);
7117c478bd9Sstevel@tonic-gate 	}
7127c478bd9Sstevel@tonic-gate 
7139acbbeafSnn35248 	/*
7149acbbeafSnn35248 	 * If there is a brand 'boot' callback, execute it now to give the
7159acbbeafSnn35248 	 * brand one last chance to do any additional setup before the zone
7169acbbeafSnn35248 	 * is booted.
7179acbbeafSnn35248 	 */
7189acbbeafSnn35248 	if ((strlen(cmdbuf) > EXEC_LEN) &&
7199acbbeafSnn35248 	    (do_subproc(zlogp, cmdbuf) != Z_OK)) {
7209acbbeafSnn35248 		zerror(zlogp, B_FALSE, "%s failed", cmdbuf);
7219acbbeafSnn35248 		return (-1);
7229acbbeafSnn35248 	}
7239acbbeafSnn35248 
7243f2f09c1Sdp 	if (zone_setattr(zoneid, ZONE_ATTR_INITNAME, init_file, 0) == -1) {
7253f2f09c1Sdp 		zerror(zlogp, B_TRUE, "could not set zone boot file");
7263f2f09c1Sdp 		return (-1);
7273f2f09c1Sdp 	}
7283f2f09c1Sdp 
7293f2f09c1Sdp 	if (zone_setattr(zoneid, ZONE_ATTR_BOOTARGS, nbootargs, 0) == -1) {
7303f2f09c1Sdp 		zerror(zlogp, B_TRUE, "could not set zone boot arguments");
7313f2f09c1Sdp 		return (-1);
7323f2f09c1Sdp 	}
7333f2f09c1Sdp 
7343f2f09c1Sdp 	if (zone_boot(zoneid) == -1) {
7357c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "unable to boot zone");
7367c478bd9Sstevel@tonic-gate 		return (-1);
7377c478bd9Sstevel@tonic-gate 	}
7387c478bd9Sstevel@tonic-gate 
7397c478bd9Sstevel@tonic-gate 	return (0);
7407c478bd9Sstevel@tonic-gate }
7417c478bd9Sstevel@tonic-gate 
7427c478bd9Sstevel@tonic-gate static int
7430209230bSgjelinek zone_halt(zlog_t *zlogp, boolean_t unmount_cmd, boolean_t rebooting)
7447c478bd9Sstevel@tonic-gate {
7457c478bd9Sstevel@tonic-gate 	int err;
7467c478bd9Sstevel@tonic-gate 
7470209230bSgjelinek 	if (vplat_teardown(zlogp, unmount_cmd, rebooting) != 0) {
7487c478bd9Sstevel@tonic-gate 		if (!bringup_failure_recovery)
7497c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_FALSE, "unable to destroy zone");
7507c478bd9Sstevel@tonic-gate 		return (-1);
7517c478bd9Sstevel@tonic-gate 	}
7527c478bd9Sstevel@tonic-gate 
7537c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_destroy_snapshot(zone_name)) != Z_OK)
7547c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "destroying snapshot: %s",
7557c478bd9Sstevel@tonic-gate 		    zonecfg_strerror(err));
7567c478bd9Sstevel@tonic-gate 
7577c478bd9Sstevel@tonic-gate 	return (0);
7587c478bd9Sstevel@tonic-gate }
7597c478bd9Sstevel@tonic-gate 
7607c478bd9Sstevel@tonic-gate /*
7617c478bd9Sstevel@tonic-gate  * Generate AUE_zone_state for a command that boots a zone.
7627c478bd9Sstevel@tonic-gate  */
7637c478bd9Sstevel@tonic-gate static void
7647c478bd9Sstevel@tonic-gate audit_put_record(zlog_t *zlogp, ucred_t *uc, int return_val,
7657c478bd9Sstevel@tonic-gate     char *new_state)
7667c478bd9Sstevel@tonic-gate {
7677c478bd9Sstevel@tonic-gate 	adt_session_data_t	*ah;
7687c478bd9Sstevel@tonic-gate 	adt_event_data_t	*event;
7697c478bd9Sstevel@tonic-gate 	int			pass_fail, fail_reason;
7707c478bd9Sstevel@tonic-gate 
7717c478bd9Sstevel@tonic-gate 	if (!adt_audit_enabled())
7727c478bd9Sstevel@tonic-gate 		return;
7737c478bd9Sstevel@tonic-gate 
7747c478bd9Sstevel@tonic-gate 	if (return_val == 0) {
7757c478bd9Sstevel@tonic-gate 		pass_fail = ADT_SUCCESS;
7767c478bd9Sstevel@tonic-gate 		fail_reason = ADT_SUCCESS;
7777c478bd9Sstevel@tonic-gate 	} else {
7787c478bd9Sstevel@tonic-gate 		pass_fail = ADT_FAILURE;
7797c478bd9Sstevel@tonic-gate 		fail_reason = ADT_FAIL_VALUE_PROGRAM;
7807c478bd9Sstevel@tonic-gate 	}
7817c478bd9Sstevel@tonic-gate 
7827c478bd9Sstevel@tonic-gate 	if (adt_start_session(&ah, NULL, 0)) {
7837c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, gettext("audit failure."));
7847c478bd9Sstevel@tonic-gate 		return;
7857c478bd9Sstevel@tonic-gate 	}
7867c478bd9Sstevel@tonic-gate 	if (adt_set_from_ucred(ah, uc, ADT_NEW)) {
7877c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, gettext("audit failure."));
7887c478bd9Sstevel@tonic-gate 		(void) adt_end_session(ah);
7897c478bd9Sstevel@tonic-gate 		return;
7907c478bd9Sstevel@tonic-gate 	}
7917c478bd9Sstevel@tonic-gate 
7927c478bd9Sstevel@tonic-gate 	event = adt_alloc_event(ah, ADT_zone_state);
7937c478bd9Sstevel@tonic-gate 	if (event == NULL) {
7947c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, gettext("audit failure."));
7957c478bd9Sstevel@tonic-gate 		(void) adt_end_session(ah);
7967c478bd9Sstevel@tonic-gate 		return;
7977c478bd9Sstevel@tonic-gate 	}
7987c478bd9Sstevel@tonic-gate 	event->adt_zone_state.zonename = zone_name;
7997c478bd9Sstevel@tonic-gate 	event->adt_zone_state.new_state = new_state;
8007c478bd9Sstevel@tonic-gate 
8017c478bd9Sstevel@tonic-gate 	if (adt_put_event(event, pass_fail, fail_reason))
8027c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, gettext("audit failure."));
8037c478bd9Sstevel@tonic-gate 
8047c478bd9Sstevel@tonic-gate 	adt_free_event(event);
8057c478bd9Sstevel@tonic-gate 
8067c478bd9Sstevel@tonic-gate 	(void) adt_end_session(ah);
8077c478bd9Sstevel@tonic-gate }
8087c478bd9Sstevel@tonic-gate 
8097c478bd9Sstevel@tonic-gate /*
8107c478bd9Sstevel@tonic-gate  * The main routine for the door server that deals with zone state transitions.
8117c478bd9Sstevel@tonic-gate  */
8127c478bd9Sstevel@tonic-gate /* ARGSUSED */
8137c478bd9Sstevel@tonic-gate static void
8147c478bd9Sstevel@tonic-gate server(void *cookie, char *args, size_t alen, door_desc_t *dp,
8157c478bd9Sstevel@tonic-gate     uint_t n_desc)
8167c478bd9Sstevel@tonic-gate {
8177c478bd9Sstevel@tonic-gate 	ucred_t *uc = NULL;
8187c478bd9Sstevel@tonic-gate 	const priv_set_t *eset;
8197c478bd9Sstevel@tonic-gate 
8207c478bd9Sstevel@tonic-gate 	zone_state_t zstate;
8217c478bd9Sstevel@tonic-gate 	zone_cmd_t cmd;
8227c478bd9Sstevel@tonic-gate 	zone_cmd_arg_t *zargp;
8237c478bd9Sstevel@tonic-gate 
8247c478bd9Sstevel@tonic-gate 	boolean_t kernelcall;
8257c478bd9Sstevel@tonic-gate 
8267c478bd9Sstevel@tonic-gate 	int rval = -1;
8277c478bd9Sstevel@tonic-gate 	uint64_t uniqid;
8287c478bd9Sstevel@tonic-gate 	zoneid_t zoneid = -1;
8297c478bd9Sstevel@tonic-gate 	zlog_t zlog;
8307c478bd9Sstevel@tonic-gate 	zlog_t *zlogp;
8317c478bd9Sstevel@tonic-gate 	zone_cmd_rval_t *rvalp;
8327c478bd9Sstevel@tonic-gate 	size_t rlen = getpagesize(); /* conservative */
8339acbbeafSnn35248 	fs_callback_t cb;
834123807fbSedp 	brand_handle_t bh;
8357c478bd9Sstevel@tonic-gate 
8367c478bd9Sstevel@tonic-gate 	/* LINTED E_BAD_PTR_CAST_ALIGN */
8377c478bd9Sstevel@tonic-gate 	zargp = (zone_cmd_arg_t *)args;
8387c478bd9Sstevel@tonic-gate 
8397c478bd9Sstevel@tonic-gate 	/*
8407c478bd9Sstevel@tonic-gate 	 * When we get the door unref message, we've fdetach'd the door, and
8417c478bd9Sstevel@tonic-gate 	 * it is time for us to shut down zoneadmd.
8427c478bd9Sstevel@tonic-gate 	 */
8437c478bd9Sstevel@tonic-gate 	if (zargp == DOOR_UNREF_DATA) {
8447c478bd9Sstevel@tonic-gate 		/*
8457c478bd9Sstevel@tonic-gate 		 * See comment at end of main() for info on the last rites.
8467c478bd9Sstevel@tonic-gate 		 */
8477c478bd9Sstevel@tonic-gate 		exit(0);
8487c478bd9Sstevel@tonic-gate 	}
8497c478bd9Sstevel@tonic-gate 
8507c478bd9Sstevel@tonic-gate 	if (zargp == NULL) {
8517c478bd9Sstevel@tonic-gate 		(void) door_return(NULL, 0, 0, 0);
8527c478bd9Sstevel@tonic-gate 	}
8537c478bd9Sstevel@tonic-gate 
8547c478bd9Sstevel@tonic-gate 	rvalp = alloca(rlen);
8557c478bd9Sstevel@tonic-gate 	bzero(rvalp, rlen);
8567c478bd9Sstevel@tonic-gate 	zlog.logfile = NULL;
8577c478bd9Sstevel@tonic-gate 	zlog.buflen = zlog.loglen = rlen - sizeof (zone_cmd_rval_t) + 1;
8587c478bd9Sstevel@tonic-gate 	zlog.buf = rvalp->errbuf;
8597c478bd9Sstevel@tonic-gate 	zlog.log = zlog.buf;
8607c478bd9Sstevel@tonic-gate 	/* defer initialization of zlog.locale until after credential check */
8617c478bd9Sstevel@tonic-gate 	zlogp = &zlog;
8627c478bd9Sstevel@tonic-gate 
8637c478bd9Sstevel@tonic-gate 	if (alen != sizeof (zone_cmd_arg_t)) {
8647c478bd9Sstevel@tonic-gate 		/*
8657c478bd9Sstevel@tonic-gate 		 * This really shouldn't be happening.
8667c478bd9Sstevel@tonic-gate 		 */
8673f2f09c1Sdp 		zerror(&logsys, B_FALSE, "argument size (%d bytes) "
8683f2f09c1Sdp 		    "unexpected (expected %d bytes)", alen,
8693f2f09c1Sdp 		    sizeof (zone_cmd_arg_t));
8707c478bd9Sstevel@tonic-gate 		goto out;
8717c478bd9Sstevel@tonic-gate 	}
8727c478bd9Sstevel@tonic-gate 	cmd = zargp->cmd;
8737c478bd9Sstevel@tonic-gate 
8747c478bd9Sstevel@tonic-gate 	if (door_ucred(&uc) != 0) {
8757c478bd9Sstevel@tonic-gate 		zerror(&logsys, B_TRUE, "door_ucred");
8767c478bd9Sstevel@tonic-gate 		goto out;
8777c478bd9Sstevel@tonic-gate 	}
8787c478bd9Sstevel@tonic-gate 	eset = ucred_getprivset(uc, PRIV_EFFECTIVE);
8797c478bd9Sstevel@tonic-gate 	if (ucred_getzoneid(uc) != GLOBAL_ZONEID ||
8807c478bd9Sstevel@tonic-gate 	    (eset != NULL ? !priv_ismember(eset, PRIV_SYS_CONFIG) :
8817c478bd9Sstevel@tonic-gate 	    ucred_geteuid(uc) != 0)) {
8827c478bd9Sstevel@tonic-gate 		zerror(&logsys, B_FALSE, "insufficient privileges");
8837c478bd9Sstevel@tonic-gate 		goto out;
8847c478bd9Sstevel@tonic-gate 	}
8857c478bd9Sstevel@tonic-gate 
8867c478bd9Sstevel@tonic-gate 	kernelcall = ucred_getpid(uc) == 0;
8877c478bd9Sstevel@tonic-gate 
8887c478bd9Sstevel@tonic-gate 	/*
8897c478bd9Sstevel@tonic-gate 	 * This is safe because we only use a zlog_t throughout the
8907c478bd9Sstevel@tonic-gate 	 * duration of a door call; i.e., by the time the pointer
8917c478bd9Sstevel@tonic-gate 	 * might become invalid, the door call would be over.
8927c478bd9Sstevel@tonic-gate 	 */
8937c478bd9Sstevel@tonic-gate 	zlog.locale = kernelcall ? DEFAULT_LOCALE : zargp->locale;
8947c478bd9Sstevel@tonic-gate 
8957c478bd9Sstevel@tonic-gate 	(void) mutex_lock(&lock);
8967c478bd9Sstevel@tonic-gate 
8977c478bd9Sstevel@tonic-gate 	/*
8987c478bd9Sstevel@tonic-gate 	 * Once we start to really die off, we don't want more connections.
8997c478bd9Sstevel@tonic-gate 	 */
9007c478bd9Sstevel@tonic-gate 	if (in_death_throes) {
9017c478bd9Sstevel@tonic-gate 		(void) mutex_unlock(&lock);
9027c478bd9Sstevel@tonic-gate 		ucred_free(uc);
9037c478bd9Sstevel@tonic-gate 		(void) door_return(NULL, 0, 0, 0);
9047c478bd9Sstevel@tonic-gate 		thr_exit(NULL);
9057c478bd9Sstevel@tonic-gate 	}
9067c478bd9Sstevel@tonic-gate 
9077c478bd9Sstevel@tonic-gate 	/*
9087c478bd9Sstevel@tonic-gate 	 * Check for validity of command.
9097c478bd9Sstevel@tonic-gate 	 */
9109acbbeafSnn35248 	if (cmd != Z_READY && cmd != Z_BOOT && cmd != Z_FORCEBOOT &&
9119acbbeafSnn35248 	    cmd != Z_REBOOT && cmd != Z_HALT && cmd != Z_NOTE_UNINSTALLING &&
9129acbbeafSnn35248 	    cmd != Z_MOUNT && cmd != Z_FORCEMOUNT && cmd != Z_UNMOUNT) {
913108322fbScarlsonj 		zerror(&logsys, B_FALSE, "invalid command %d", (int)cmd);
9147c478bd9Sstevel@tonic-gate 		goto out;
9157c478bd9Sstevel@tonic-gate 	}
9167c478bd9Sstevel@tonic-gate 
9177c478bd9Sstevel@tonic-gate 	if (kernelcall && (cmd != Z_HALT && cmd != Z_REBOOT)) {
9187c478bd9Sstevel@tonic-gate 		/*
9197c478bd9Sstevel@tonic-gate 		 * Can't happen
9207c478bd9Sstevel@tonic-gate 		 */
9217c478bd9Sstevel@tonic-gate 		zerror(&logsys, B_FALSE, "received unexpected kernel upcall %d",
9227c478bd9Sstevel@tonic-gate 		    cmd);
9237c478bd9Sstevel@tonic-gate 		goto out;
9247c478bd9Sstevel@tonic-gate 	}
9257c478bd9Sstevel@tonic-gate 	/*
9267c478bd9Sstevel@tonic-gate 	 * We ignore the possibility of someone calling zone_create(2)
9277c478bd9Sstevel@tonic-gate 	 * explicitly; all requests must come through zoneadmd.
9287c478bd9Sstevel@tonic-gate 	 */
9297c478bd9Sstevel@tonic-gate 	if (zone_get_state(zone_name, &zstate) != Z_OK) {
9307c478bd9Sstevel@tonic-gate 		/*
9317c478bd9Sstevel@tonic-gate 		 * Something terribly wrong happened
9327c478bd9Sstevel@tonic-gate 		 */
9337c478bd9Sstevel@tonic-gate 		zerror(&logsys, B_FALSE, "unable to determine state of zone");
9347c478bd9Sstevel@tonic-gate 		goto out;
9357c478bd9Sstevel@tonic-gate 	}
9367c478bd9Sstevel@tonic-gate 
9377c478bd9Sstevel@tonic-gate 	if (kernelcall) {
9387c478bd9Sstevel@tonic-gate 		/*
9397c478bd9Sstevel@tonic-gate 		 * Kernel-initiated requests may lose their validity if the
9407c478bd9Sstevel@tonic-gate 		 * zone_t the kernel was referring to has gone away.
9417c478bd9Sstevel@tonic-gate 		 */
9427c478bd9Sstevel@tonic-gate 		if ((zoneid = getzoneidbyname(zone_name)) == -1 ||
9437c478bd9Sstevel@tonic-gate 		    zone_getattr(zoneid, ZONE_ATTR_UNIQID, &uniqid,
9447c478bd9Sstevel@tonic-gate 		    sizeof (uniqid)) == -1 || uniqid != zargp->uniqid) {
9457c478bd9Sstevel@tonic-gate 			/*
9467c478bd9Sstevel@tonic-gate 			 * We're not talking about the same zone. The request
9477c478bd9Sstevel@tonic-gate 			 * must have arrived too late.  Return error.
9487c478bd9Sstevel@tonic-gate 			 */
9497c478bd9Sstevel@tonic-gate 			rval = -1;
9507c478bd9Sstevel@tonic-gate 			goto out;
9517c478bd9Sstevel@tonic-gate 		}
9527c478bd9Sstevel@tonic-gate 		zlogp = &logsys;	/* Log errors to syslog */
9537c478bd9Sstevel@tonic-gate 	}
9547c478bd9Sstevel@tonic-gate 
9559acbbeafSnn35248 	/*
9569acbbeafSnn35248 	 * If we are being asked to forcibly mount or boot a zone, we
9579acbbeafSnn35248 	 * pretend that an INCOMPLETE zone is actually INSTALLED.
9589acbbeafSnn35248 	 */
9599acbbeafSnn35248 	if (zstate == ZONE_STATE_INCOMPLETE &&
9609acbbeafSnn35248 	    (cmd == Z_FORCEBOOT || cmd == Z_FORCEMOUNT))
9619acbbeafSnn35248 		zstate = ZONE_STATE_INSTALLED;
9629acbbeafSnn35248 
9637c478bd9Sstevel@tonic-gate 	switch (zstate) {
9647c478bd9Sstevel@tonic-gate 	case ZONE_STATE_CONFIGURED:
9657c478bd9Sstevel@tonic-gate 	case ZONE_STATE_INCOMPLETE:
9667c478bd9Sstevel@tonic-gate 		/*
9677c478bd9Sstevel@tonic-gate 		 * Not our area of expertise; we just print a nice message
9687c478bd9Sstevel@tonic-gate 		 * and die off.
9697c478bd9Sstevel@tonic-gate 		 */
9707c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE,
9717c478bd9Sstevel@tonic-gate 		    "%s operation is invalid for zones in state '%s'",
972108322fbScarlsonj 		    z_cmd_name(cmd), zone_state_str(zstate));
9737c478bd9Sstevel@tonic-gate 		break;
9747c478bd9Sstevel@tonic-gate 
9757c478bd9Sstevel@tonic-gate 	case ZONE_STATE_INSTALLED:
9767c478bd9Sstevel@tonic-gate 		switch (cmd) {
9777c478bd9Sstevel@tonic-gate 		case Z_READY:
978108322fbScarlsonj 			rval = zone_ready(zlogp, B_FALSE);
9797c478bd9Sstevel@tonic-gate 			if (rval == 0)
9807c478bd9Sstevel@tonic-gate 				eventstream_write(Z_EVT_ZONE_READIED);
9817c478bd9Sstevel@tonic-gate 			break;
9827c478bd9Sstevel@tonic-gate 		case Z_BOOT:
9839acbbeafSnn35248 		case Z_FORCEBOOT:
9847c478bd9Sstevel@tonic-gate 			eventstream_write(Z_EVT_ZONE_BOOTING);
985108322fbScarlsonj 			if ((rval = zone_ready(zlogp, B_FALSE)) == 0)
9867c478bd9Sstevel@tonic-gate 				rval = zone_bootup(zlogp, zargp->bootbuf);
9877c478bd9Sstevel@tonic-gate 			audit_put_record(zlogp, uc, rval, "boot");
9887c478bd9Sstevel@tonic-gate 			if (rval != 0) {
9897c478bd9Sstevel@tonic-gate 				bringup_failure_recovery = B_TRUE;
9900209230bSgjelinek 				(void) zone_halt(zlogp, B_FALSE, B_FALSE);
991ffbafc53Scomay 				eventstream_write(Z_EVT_ZONE_BOOTFAILED);
9927c478bd9Sstevel@tonic-gate 			}
9937c478bd9Sstevel@tonic-gate 			break;
9947c478bd9Sstevel@tonic-gate 		case Z_HALT:
9957c478bd9Sstevel@tonic-gate 			if (kernelcall)	/* Invalid; can't happen */
9967c478bd9Sstevel@tonic-gate 				abort();
9977c478bd9Sstevel@tonic-gate 			/*
9987c478bd9Sstevel@tonic-gate 			 * We could have two clients racing to halt this
9997c478bd9Sstevel@tonic-gate 			 * zone; the second client loses, but his request
10007c478bd9Sstevel@tonic-gate 			 * doesn't fail, since the zone is now in the desired
10017c478bd9Sstevel@tonic-gate 			 * state.
10027c478bd9Sstevel@tonic-gate 			 */
10037c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_FALSE, "zone is already halted");
10047c478bd9Sstevel@tonic-gate 			rval = 0;
10057c478bd9Sstevel@tonic-gate 			break;
10067c478bd9Sstevel@tonic-gate 		case Z_REBOOT:
10077c478bd9Sstevel@tonic-gate 			if (kernelcall)	/* Invalid; can't happen */
10087c478bd9Sstevel@tonic-gate 				abort();
10097c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_FALSE, "%s operation is invalid "
1010108322fbScarlsonj 			    "for zones in state '%s'", z_cmd_name(cmd),
10117c478bd9Sstevel@tonic-gate 			    zone_state_str(zstate));
10127c478bd9Sstevel@tonic-gate 			rval = -1;
10137c478bd9Sstevel@tonic-gate 			break;
10147c478bd9Sstevel@tonic-gate 		case Z_NOTE_UNINSTALLING:
10157c478bd9Sstevel@tonic-gate 			if (kernelcall)	/* Invalid; can't happen */
10167c478bd9Sstevel@tonic-gate 				abort();
10177c478bd9Sstevel@tonic-gate 			/*
10187c478bd9Sstevel@tonic-gate 			 * Tell the console to print out a message about this.
10197c478bd9Sstevel@tonic-gate 			 * Once it does, we will be in_death_throes.
10207c478bd9Sstevel@tonic-gate 			 */
10217c478bd9Sstevel@tonic-gate 			eventstream_write(Z_EVT_ZONE_UNINSTALLING);
10227c478bd9Sstevel@tonic-gate 			break;
1023108322fbScarlsonj 		case Z_MOUNT:
10249acbbeafSnn35248 		case Z_FORCEMOUNT:
1025108322fbScarlsonj 			if (kernelcall)	/* Invalid; can't happen */
1026108322fbScarlsonj 				abort();
1027*84561e8cStd153743 			if (!zone_isnative && !zone_iscluster) {
10289acbbeafSnn35248 				zerror(zlogp, B_FALSE,
10299acbbeafSnn35248 				    "%s operation is invalid for branded "
10309acbbeafSnn35248 				    "zones", z_cmd_name(cmd));
10319acbbeafSnn35248 				rval = -1;
10329acbbeafSnn35248 				break;
1033ffbafc53Scomay 			}
1034ffbafc53Scomay 
10359acbbeafSnn35248 			rval = zone_ready(zlogp, B_TRUE);
10369acbbeafSnn35248 			if (rval != 0)
10379acbbeafSnn35248 				break;
10389acbbeafSnn35248 
10399acbbeafSnn35248 			eventstream_write(Z_EVT_ZONE_READIED);
10409acbbeafSnn35248 
10419acbbeafSnn35248 			/* Get a handle to the brand info for this zone */
1042123807fbSedp 			if ((bh = brand_open(brand_name)) == NULL) {
10439acbbeafSnn35248 				rval = -1;
10449acbbeafSnn35248 				break;
10459acbbeafSnn35248 			}
10469acbbeafSnn35248 
10479acbbeafSnn35248 			/*
10489acbbeafSnn35248 			 * Get the list of filesystems to mount from
10499acbbeafSnn35248 			 * the brand configuration.  These mounts are done
10509acbbeafSnn35248 			 * via a thread that will enter the zone, so they
10519acbbeafSnn35248 			 * are done from within the context of the zone.
10529acbbeafSnn35248 			 */
10539acbbeafSnn35248 			cb.zlogp = zlogp;
10549acbbeafSnn35248 			cb.zoneid = zone_id;
1055123807fbSedp 			rval = brand_platform_iter_mounts(bh,
10569acbbeafSnn35248 			    mount_early_fs, &cb);
10579acbbeafSnn35248 
1058123807fbSedp 			brand_close(bh);
10599acbbeafSnn35248 
1060108322fbScarlsonj 			/*
1061108322fbScarlsonj 			 * Ordinarily, /dev/fd would be mounted inside the zone
1062108322fbScarlsonj 			 * by svc:/system/filesystem/usr:default, but since
1063108322fbScarlsonj 			 * we're not booting the zone, we need to do this
1064108322fbScarlsonj 			 * manually.
1065108322fbScarlsonj 			 */
1066108322fbScarlsonj 			if (rval == 0)
10679acbbeafSnn35248 				rval = mount_early_fs(&cb,
10689acbbeafSnn35248 				    "fd", "/dev/fd", "fd", NULL);
1069108322fbScarlsonj 			break;
1070108322fbScarlsonj 		case Z_UNMOUNT:
1071108322fbScarlsonj 			if (kernelcall)	/* Invalid; can't happen */
1072108322fbScarlsonj 				abort();
1073108322fbScarlsonj 			zerror(zlogp, B_FALSE, "zone is already unmounted");
1074108322fbScarlsonj 			rval = 0;
1075108322fbScarlsonj 			break;
10767c478bd9Sstevel@tonic-gate 		}
10777c478bd9Sstevel@tonic-gate 		break;
10787c478bd9Sstevel@tonic-gate 
10797c478bd9Sstevel@tonic-gate 	case ZONE_STATE_READY:
10807c478bd9Sstevel@tonic-gate 		switch (cmd) {
10817c478bd9Sstevel@tonic-gate 		case Z_READY:
10827c478bd9Sstevel@tonic-gate 			/*
10837c478bd9Sstevel@tonic-gate 			 * We could have two clients racing to ready this
10847c478bd9Sstevel@tonic-gate 			 * zone; the second client loses, but his request
10857c478bd9Sstevel@tonic-gate 			 * doesn't fail, since the zone is now in the desired
10867c478bd9Sstevel@tonic-gate 			 * state.
10877c478bd9Sstevel@tonic-gate 			 */
10887c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_FALSE, "zone is already ready");
10897c478bd9Sstevel@tonic-gate 			rval = 0;
10907c478bd9Sstevel@tonic-gate 			break;
10917c478bd9Sstevel@tonic-gate 		case Z_BOOT:
10923f2f09c1Sdp 			(void) strlcpy(boot_args, zargp->bootbuf,
10933f2f09c1Sdp 			    sizeof (boot_args));
10947c478bd9Sstevel@tonic-gate 			eventstream_write(Z_EVT_ZONE_BOOTING);
10957c478bd9Sstevel@tonic-gate 			rval = zone_bootup(zlogp, zargp->bootbuf);
10967c478bd9Sstevel@tonic-gate 			audit_put_record(zlogp, uc, rval, "boot");
10977c478bd9Sstevel@tonic-gate 			if (rval != 0) {
10987c478bd9Sstevel@tonic-gate 				bringup_failure_recovery = B_TRUE;
10990209230bSgjelinek 				(void) zone_halt(zlogp, B_FALSE, B_TRUE);
1100ffbafc53Scomay 				eventstream_write(Z_EVT_ZONE_BOOTFAILED);
11017c478bd9Sstevel@tonic-gate 			}
11023f2f09c1Sdp 			boot_args[0] = '\0';
11037c478bd9Sstevel@tonic-gate 			break;
11047c478bd9Sstevel@tonic-gate 		case Z_HALT:
11057c478bd9Sstevel@tonic-gate 			if (kernelcall)	/* Invalid; can't happen */
11067c478bd9Sstevel@tonic-gate 				abort();
11070209230bSgjelinek 			if ((rval = zone_halt(zlogp, B_FALSE, B_FALSE)) != 0)
11087c478bd9Sstevel@tonic-gate 				break;
11097c478bd9Sstevel@tonic-gate 			eventstream_write(Z_EVT_ZONE_HALTED);
11107c478bd9Sstevel@tonic-gate 			break;
11117c478bd9Sstevel@tonic-gate 		case Z_REBOOT:
1112108322fbScarlsonj 		case Z_NOTE_UNINSTALLING:
1113108322fbScarlsonj 		case Z_MOUNT:
1114108322fbScarlsonj 		case Z_UNMOUNT:
11157c478bd9Sstevel@tonic-gate 			if (kernelcall)	/* Invalid; can't happen */
11167c478bd9Sstevel@tonic-gate 				abort();
11177c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_FALSE, "%s operation is invalid "
1118108322fbScarlsonj 			    "for zones in state '%s'", z_cmd_name(cmd),
11197c478bd9Sstevel@tonic-gate 			    zone_state_str(zstate));
11207c478bd9Sstevel@tonic-gate 			rval = -1;
11217c478bd9Sstevel@tonic-gate 			break;
1122108322fbScarlsonj 		}
1123108322fbScarlsonj 		break;
1124108322fbScarlsonj 
1125108322fbScarlsonj 	case ZONE_STATE_MOUNTED:
1126108322fbScarlsonj 		switch (cmd) {
1127108322fbScarlsonj 		case Z_UNMOUNT:
11287c478bd9Sstevel@tonic-gate 			if (kernelcall)	/* Invalid; can't happen */
11297c478bd9Sstevel@tonic-gate 				abort();
11300209230bSgjelinek 			rval = zone_halt(zlogp, B_TRUE, B_FALSE);
1131ffbafc53Scomay 			if (rval == 0) {
1132ffbafc53Scomay 				eventstream_write(Z_EVT_ZONE_HALTED);
1133108322fbScarlsonj 				(void) sema_post(&scratch_sem);
1134ffbafc53Scomay 			}
1135108322fbScarlsonj 			break;
1136108322fbScarlsonj 		default:
1137108322fbScarlsonj 			if (kernelcall)	/* Invalid; can't happen */
1138108322fbScarlsonj 				abort();
1139108322fbScarlsonj 			zerror(zlogp, B_FALSE, "%s operation is invalid "
1140108322fbScarlsonj 			    "for zones in state '%s'", z_cmd_name(cmd),
1141108322fbScarlsonj 			    zone_state_str(zstate));
11427c478bd9Sstevel@tonic-gate 			rval = -1;
11437c478bd9Sstevel@tonic-gate 			break;
11447c478bd9Sstevel@tonic-gate 		}
11457c478bd9Sstevel@tonic-gate 		break;
11467c478bd9Sstevel@tonic-gate 
11477c478bd9Sstevel@tonic-gate 	case ZONE_STATE_RUNNING:
11487c478bd9Sstevel@tonic-gate 	case ZONE_STATE_SHUTTING_DOWN:
11497c478bd9Sstevel@tonic-gate 	case ZONE_STATE_DOWN:
11507c478bd9Sstevel@tonic-gate 		switch (cmd) {
11517c478bd9Sstevel@tonic-gate 		case Z_READY:
11520209230bSgjelinek 			if ((rval = zone_halt(zlogp, B_FALSE, B_TRUE)) != 0)
11537c478bd9Sstevel@tonic-gate 				break;
1154108322fbScarlsonj 			if ((rval = zone_ready(zlogp, B_FALSE)) == 0)
11557c478bd9Sstevel@tonic-gate 				eventstream_write(Z_EVT_ZONE_READIED);
1156ffbafc53Scomay 			else
1157ffbafc53Scomay 				eventstream_write(Z_EVT_ZONE_HALTED);
11587c478bd9Sstevel@tonic-gate 			break;
11597c478bd9Sstevel@tonic-gate 		case Z_BOOT:
11607c478bd9Sstevel@tonic-gate 			/*
11617c478bd9Sstevel@tonic-gate 			 * We could have two clients racing to boot this
11627c478bd9Sstevel@tonic-gate 			 * zone; the second client loses, but his request
11637c478bd9Sstevel@tonic-gate 			 * doesn't fail, since the zone is now in the desired
11647c478bd9Sstevel@tonic-gate 			 * state.
11657c478bd9Sstevel@tonic-gate 			 */
11667c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_FALSE, "zone is already booted");
11677c478bd9Sstevel@tonic-gate 			rval = 0;
11687c478bd9Sstevel@tonic-gate 			break;
11697c478bd9Sstevel@tonic-gate 		case Z_HALT:
11700209230bSgjelinek 			if ((rval = zone_halt(zlogp, B_FALSE, B_FALSE)) != 0)
11717c478bd9Sstevel@tonic-gate 				break;
11727c478bd9Sstevel@tonic-gate 			eventstream_write(Z_EVT_ZONE_HALTED);
11737c478bd9Sstevel@tonic-gate 			break;
11747c478bd9Sstevel@tonic-gate 		case Z_REBOOT:
11753f2f09c1Sdp 			(void) strlcpy(boot_args, zargp->bootbuf,
11763f2f09c1Sdp 			    sizeof (boot_args));
11777c478bd9Sstevel@tonic-gate 			eventstream_write(Z_EVT_ZONE_REBOOTING);
11780209230bSgjelinek 			if ((rval = zone_halt(zlogp, B_FALSE, B_TRUE)) != 0) {
1179ffbafc53Scomay 				eventstream_write(Z_EVT_ZONE_BOOTFAILED);
11803f2f09c1Sdp 				boot_args[0] = '\0';
11817c478bd9Sstevel@tonic-gate 				break;
1182ffbafc53Scomay 			}
1183ffbafc53Scomay 			if ((rval = zone_ready(zlogp, B_FALSE)) != 0) {
1184ffbafc53Scomay 				eventstream_write(Z_EVT_ZONE_BOOTFAILED);
11853f2f09c1Sdp 				boot_args[0] = '\0';
1186ffbafc53Scomay 				break;
1187ffbafc53Scomay 			}
11883f2f09c1Sdp 			rval = zone_bootup(zlogp, zargp->bootbuf);
11897c478bd9Sstevel@tonic-gate 			audit_put_record(zlogp, uc, rval, "reboot");
1190ffbafc53Scomay 			if (rval != 0) {
11910209230bSgjelinek 				(void) zone_halt(zlogp, B_FALSE, B_TRUE);
1192ffbafc53Scomay 				eventstream_write(Z_EVT_ZONE_BOOTFAILED);
11937c478bd9Sstevel@tonic-gate 			}
11943f2f09c1Sdp 			boot_args[0] = '\0';
11957c478bd9Sstevel@tonic-gate 			break;
11967c478bd9Sstevel@tonic-gate 		case Z_NOTE_UNINSTALLING:
1197108322fbScarlsonj 		case Z_MOUNT:
1198108322fbScarlsonj 		case Z_UNMOUNT:
1199108322fbScarlsonj 			zerror(zlogp, B_FALSE, "%s operation is invalid "
1200108322fbScarlsonj 			    "for zones in state '%s'", z_cmd_name(cmd),
1201108322fbScarlsonj 			    zone_state_str(zstate));
12027c478bd9Sstevel@tonic-gate 			rval = -1;
12037c478bd9Sstevel@tonic-gate 			break;
12047c478bd9Sstevel@tonic-gate 		}
12057c478bd9Sstevel@tonic-gate 		break;
12067c478bd9Sstevel@tonic-gate 	default:
12077c478bd9Sstevel@tonic-gate 		abort();
12087c478bd9Sstevel@tonic-gate 	}
12097c478bd9Sstevel@tonic-gate 
12107c478bd9Sstevel@tonic-gate 	/*
12117c478bd9Sstevel@tonic-gate 	 * Because the state of the zone may have changed, we make sure
12127c478bd9Sstevel@tonic-gate 	 * to wake the console poller, which is in charge of initiating
12137c478bd9Sstevel@tonic-gate 	 * the shutdown procedure as necessary.
12147c478bd9Sstevel@tonic-gate 	 */
12157c478bd9Sstevel@tonic-gate 	eventstream_write(Z_EVT_NULL);
12167c478bd9Sstevel@tonic-gate 
12177c478bd9Sstevel@tonic-gate out:
12187c478bd9Sstevel@tonic-gate 	(void) mutex_unlock(&lock);
12197c478bd9Sstevel@tonic-gate 	if (kernelcall) {
12207c478bd9Sstevel@tonic-gate 		rvalp = NULL;
12217c478bd9Sstevel@tonic-gate 		rlen = 0;
12227c478bd9Sstevel@tonic-gate 	} else {
12237c478bd9Sstevel@tonic-gate 		rvalp->rval = rval;
12247c478bd9Sstevel@tonic-gate 	}
12257c478bd9Sstevel@tonic-gate 	if (uc != NULL)
12267c478bd9Sstevel@tonic-gate 		ucred_free(uc);
12277c478bd9Sstevel@tonic-gate 	(void) door_return((char *)rvalp, rlen, NULL, 0);
12287c478bd9Sstevel@tonic-gate 	thr_exit(NULL);
12297c478bd9Sstevel@tonic-gate }
12307c478bd9Sstevel@tonic-gate 
12317c478bd9Sstevel@tonic-gate static int
12327c478bd9Sstevel@tonic-gate setup_door(zlog_t *zlogp)
12337c478bd9Sstevel@tonic-gate {
12347c478bd9Sstevel@tonic-gate 	if ((zone_door = door_create(server, NULL,
12357c478bd9Sstevel@tonic-gate 	    DOOR_UNREF | DOOR_REFUSE_DESC | DOOR_NO_CANCEL)) < 0) {
12367c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "%s failed", "door_create");
12377c478bd9Sstevel@tonic-gate 		return (-1);
12387c478bd9Sstevel@tonic-gate 	}
12397c478bd9Sstevel@tonic-gate 	(void) fdetach(zone_door_path);
12407c478bd9Sstevel@tonic-gate 
12417c478bd9Sstevel@tonic-gate 	if (fattach(zone_door, zone_door_path) != 0) {
12427c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "fattach to %s failed", zone_door_path);
12437c478bd9Sstevel@tonic-gate 		(void) door_revoke(zone_door);
12447c478bd9Sstevel@tonic-gate 		(void) fdetach(zone_door_path);
12457c478bd9Sstevel@tonic-gate 		zone_door = -1;
12467c478bd9Sstevel@tonic-gate 		return (-1);
12477c478bd9Sstevel@tonic-gate 	}
12487c478bd9Sstevel@tonic-gate 	return (0);
12497c478bd9Sstevel@tonic-gate }
12507c478bd9Sstevel@tonic-gate 
12517c478bd9Sstevel@tonic-gate /*
12527c478bd9Sstevel@tonic-gate  * zoneadm(1m) will start zoneadmd if it thinks it isn't running; this
12537c478bd9Sstevel@tonic-gate  * is where zoneadmd itself will check to see that another instance of
12547c478bd9Sstevel@tonic-gate  * zoneadmd isn't already controlling this zone.
12557c478bd9Sstevel@tonic-gate  *
12567c478bd9Sstevel@tonic-gate  * The idea here is that we want to open the path to which we will
12577c478bd9Sstevel@tonic-gate  * attach our door, lock it, and then make sure that no-one has beat us
12587c478bd9Sstevel@tonic-gate  * to fattach(3c)ing onto it.
12597c478bd9Sstevel@tonic-gate  *
12607c478bd9Sstevel@tonic-gate  * fattach(3c) is really a mount, so there are actually two possible
12617c478bd9Sstevel@tonic-gate  * vnodes we could be dealing with.  Our strategy is as follows:
12627c478bd9Sstevel@tonic-gate  *
12637c478bd9Sstevel@tonic-gate  * - If the file we opened is a regular file (common case):
12647c478bd9Sstevel@tonic-gate  * 	There is no fattach(3c)ed door, so we have a chance of becoming
12657c478bd9Sstevel@tonic-gate  * 	the managing zoneadmd. We attempt to lock the file: if it is
12667c478bd9Sstevel@tonic-gate  * 	already locked, that means someone else raced us here, so we
12677c478bd9Sstevel@tonic-gate  * 	lose and give up.  zoneadm(1m) will try to contact the zoneadmd
12687c478bd9Sstevel@tonic-gate  * 	that beat us to it.
12697c478bd9Sstevel@tonic-gate  *
12707c478bd9Sstevel@tonic-gate  * - If the file we opened is a namefs file:
12717c478bd9Sstevel@tonic-gate  * 	This means there is already an established door fattach(3c)'ed
12727c478bd9Sstevel@tonic-gate  * 	to the rendezvous path.  We've lost the race, so we give up.
12737c478bd9Sstevel@tonic-gate  * 	Note that in this case we also try to grab the file lock, and
12747c478bd9Sstevel@tonic-gate  * 	will succeed in acquiring it since the vnode locked by the
12757c478bd9Sstevel@tonic-gate  * 	"winning" zoneadmd was a regular one, and the one we locked was
12767c478bd9Sstevel@tonic-gate  * 	the fattach(3c)'ed door node.  At any rate, no harm is done, and
12777c478bd9Sstevel@tonic-gate  * 	we just return to zoneadm(1m) which knows to retry.
12787c478bd9Sstevel@tonic-gate  */
12797c478bd9Sstevel@tonic-gate static int
12807c478bd9Sstevel@tonic-gate make_daemon_exclusive(zlog_t *zlogp)
12817c478bd9Sstevel@tonic-gate {
12827c478bd9Sstevel@tonic-gate 	int doorfd = -1;
12837c478bd9Sstevel@tonic-gate 	int err, ret = -1;
12847c478bd9Sstevel@tonic-gate 	struct stat st;
12857c478bd9Sstevel@tonic-gate 	struct flock flock;
12867c478bd9Sstevel@tonic-gate 	zone_state_t zstate;
12877c478bd9Sstevel@tonic-gate 
12887c478bd9Sstevel@tonic-gate top:
12897c478bd9Sstevel@tonic-gate 	if ((err = zone_get_state(zone_name, &zstate)) != Z_OK) {
12903f2f09c1Sdp 		zerror(zlogp, B_FALSE, "failed to get zone state: %s",
12917c478bd9Sstevel@tonic-gate 		    zonecfg_strerror(err));
12927c478bd9Sstevel@tonic-gate 		goto out;
12937c478bd9Sstevel@tonic-gate 	}
12947c478bd9Sstevel@tonic-gate 	if ((doorfd = open(zone_door_path, O_CREAT|O_RDWR,
12957c478bd9Sstevel@tonic-gate 	    S_IREAD|S_IWRITE)) < 0) {
12967c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "failed to open %s", zone_door_path);
12977c478bd9Sstevel@tonic-gate 		goto out;
12987c478bd9Sstevel@tonic-gate 	}
12997c478bd9Sstevel@tonic-gate 	if (fstat(doorfd, &st) < 0) {
13007c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "failed to stat %s", zone_door_path);
13017c478bd9Sstevel@tonic-gate 		goto out;
13027c478bd9Sstevel@tonic-gate 	}
13037c478bd9Sstevel@tonic-gate 	/*
13047c478bd9Sstevel@tonic-gate 	 * Lock the file to synchronize with other zoneadmd
13057c478bd9Sstevel@tonic-gate 	 */
13067c478bd9Sstevel@tonic-gate 	flock.l_type = F_WRLCK;
13077c478bd9Sstevel@tonic-gate 	flock.l_whence = SEEK_SET;
13087c478bd9Sstevel@tonic-gate 	flock.l_start = (off_t)0;
13097c478bd9Sstevel@tonic-gate 	flock.l_len = (off_t)0;
13107c478bd9Sstevel@tonic-gate 	if (fcntl(doorfd, F_SETLK, &flock) < 0) {
13117c478bd9Sstevel@tonic-gate 		/*
13127c478bd9Sstevel@tonic-gate 		 * Someone else raced us here and grabbed the lock file
13137c478bd9Sstevel@tonic-gate 		 * first.  A warning here is inappropriate since nothing
13147c478bd9Sstevel@tonic-gate 		 * went wrong.
13157c478bd9Sstevel@tonic-gate 		 */
13167c478bd9Sstevel@tonic-gate 		goto out;
13177c478bd9Sstevel@tonic-gate 	}
13187c478bd9Sstevel@tonic-gate 
13197c478bd9Sstevel@tonic-gate 	if (strcmp(st.st_fstype, "namefs") == 0) {
13207c478bd9Sstevel@tonic-gate 		struct door_info info;
13217c478bd9Sstevel@tonic-gate 
13227c478bd9Sstevel@tonic-gate 		/*
13237c478bd9Sstevel@tonic-gate 		 * There is already something fattach()'ed to this file.
13247c478bd9Sstevel@tonic-gate 		 * Lets see what the door is up to.
13257c478bd9Sstevel@tonic-gate 		 */
13267c478bd9Sstevel@tonic-gate 		if (door_info(doorfd, &info) == 0 && info.di_target != -1) {
13277c478bd9Sstevel@tonic-gate 			/*
13287c478bd9Sstevel@tonic-gate 			 * Another zoneadmd process seems to be in
13297c478bd9Sstevel@tonic-gate 			 * control of the situation and we don't need to
13307c478bd9Sstevel@tonic-gate 			 * be here.  A warning here is inappropriate
13317c478bd9Sstevel@tonic-gate 			 * since nothing went wrong.
13327c478bd9Sstevel@tonic-gate 			 *
13337c478bd9Sstevel@tonic-gate 			 * If the door has been revoked, the zoneadmd
13347c478bd9Sstevel@tonic-gate 			 * process currently managing the zone is going
13357c478bd9Sstevel@tonic-gate 			 * away.  We'll return control to zoneadm(1m)
13367c478bd9Sstevel@tonic-gate 			 * which will try again (by which time zoneadmd
13377c478bd9Sstevel@tonic-gate 			 * will hopefully have exited).
13387c478bd9Sstevel@tonic-gate 			 */
13397c478bd9Sstevel@tonic-gate 			goto out;
13407c478bd9Sstevel@tonic-gate 		}
13417c478bd9Sstevel@tonic-gate 
13427c478bd9Sstevel@tonic-gate 		/*
13437c478bd9Sstevel@tonic-gate 		 * If we got this far, there's a fattach(3c)'ed door
13447c478bd9Sstevel@tonic-gate 		 * that belongs to a process that has exited, which can
13457c478bd9Sstevel@tonic-gate 		 * happen if the previous zoneadmd died unexpectedly.
13467c478bd9Sstevel@tonic-gate 		 *
13477c478bd9Sstevel@tonic-gate 		 * Let user know that something is amiss, but that we can
13487c478bd9Sstevel@tonic-gate 		 * recover; if the zone is in the installed state, then don't
13497c478bd9Sstevel@tonic-gate 		 * message, since having a running zoneadmd isn't really
13507c478bd9Sstevel@tonic-gate 		 * expected/needed.  We want to keep occurences of this message
13517c478bd9Sstevel@tonic-gate 		 * limited to times when zoneadmd is picking back up from a
13527c478bd9Sstevel@tonic-gate 		 * zoneadmd that died while the zone was in some non-trivial
13537c478bd9Sstevel@tonic-gate 		 * state.
13547c478bd9Sstevel@tonic-gate 		 */
13557c478bd9Sstevel@tonic-gate 		if (zstate > ZONE_STATE_INSTALLED) {
13567c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_FALSE,
13577c478bd9Sstevel@tonic-gate 			    "zone '%s': WARNING: zone is in state '%s', but "
13587c478bd9Sstevel@tonic-gate 			    "zoneadmd does not appear to be available; "
13597c478bd9Sstevel@tonic-gate 			    "restarted zoneadmd to recover.",
13607c478bd9Sstevel@tonic-gate 			    zone_name, zone_state_str(zstate));
13617c478bd9Sstevel@tonic-gate 		}
13627c478bd9Sstevel@tonic-gate 
13637c478bd9Sstevel@tonic-gate 		(void) fdetach(zone_door_path);
13647c478bd9Sstevel@tonic-gate 		(void) close(doorfd);
13657c478bd9Sstevel@tonic-gate 		goto top;
13667c478bd9Sstevel@tonic-gate 	}
13677c478bd9Sstevel@tonic-gate 	ret = 0;
13687c478bd9Sstevel@tonic-gate out:
13697c478bd9Sstevel@tonic-gate 	(void) close(doorfd);
13707c478bd9Sstevel@tonic-gate 	return (ret);
13717c478bd9Sstevel@tonic-gate }
13727c478bd9Sstevel@tonic-gate 
13737c478bd9Sstevel@tonic-gate int
13747c478bd9Sstevel@tonic-gate main(int argc, char *argv[])
13757c478bd9Sstevel@tonic-gate {
13767c478bd9Sstevel@tonic-gate 	int opt;
13777c478bd9Sstevel@tonic-gate 	zoneid_t zid;
13787c478bd9Sstevel@tonic-gate 	priv_set_t *privset;
13797c478bd9Sstevel@tonic-gate 	zone_state_t zstate;
13807c478bd9Sstevel@tonic-gate 	char parents_locale[MAXPATHLEN];
1381123807fbSedp 	brand_handle_t bh;
13827c478bd9Sstevel@tonic-gate 	int err;
13837c478bd9Sstevel@tonic-gate 
13847c478bd9Sstevel@tonic-gate 	pid_t pid;
13857c478bd9Sstevel@tonic-gate 	sigset_t blockset;
13867c478bd9Sstevel@tonic-gate 	sigset_t block_cld;
13877c478bd9Sstevel@tonic-gate 
13887c478bd9Sstevel@tonic-gate 	struct {
13897c478bd9Sstevel@tonic-gate 		sema_t sem;
13907c478bd9Sstevel@tonic-gate 		int status;
13917c478bd9Sstevel@tonic-gate 		zlog_t log;
13927c478bd9Sstevel@tonic-gate 	} *shstate;
13937c478bd9Sstevel@tonic-gate 	size_t shstatelen = getpagesize();
13947c478bd9Sstevel@tonic-gate 
13957c478bd9Sstevel@tonic-gate 	zlog_t errlog;
13967c478bd9Sstevel@tonic-gate 	zlog_t *zlogp;
13977c478bd9Sstevel@tonic-gate 
13985cb4571dSdp 	int ctfd;
13995cb4571dSdp 
14007c478bd9Sstevel@tonic-gate 	progname = get_execbasename(argv[0]);
14017c478bd9Sstevel@tonic-gate 
14027c478bd9Sstevel@tonic-gate 	/*
14037c478bd9Sstevel@tonic-gate 	 * Make sure stderr is unbuffered
14047c478bd9Sstevel@tonic-gate 	 */
14057c478bd9Sstevel@tonic-gate 	(void) setbuffer(stderr, NULL, 0);
14067c478bd9Sstevel@tonic-gate 
14077c478bd9Sstevel@tonic-gate 	/*
14087c478bd9Sstevel@tonic-gate 	 * Get out of the way of mounted filesystems, since we will daemonize
14097c478bd9Sstevel@tonic-gate 	 * soon.
14107c478bd9Sstevel@tonic-gate 	 */
14117c478bd9Sstevel@tonic-gate 	(void) chdir("/");
14127c478bd9Sstevel@tonic-gate 
14137c478bd9Sstevel@tonic-gate 	/*
14147c478bd9Sstevel@tonic-gate 	 * Use the default system umask per PSARC 1998/110 rather than
14157c478bd9Sstevel@tonic-gate 	 * anything that may have been set by the caller.
14167c478bd9Sstevel@tonic-gate 	 */
14177c478bd9Sstevel@tonic-gate 	(void) umask(CMASK);
14187c478bd9Sstevel@tonic-gate 
14197c478bd9Sstevel@tonic-gate 	/*
14207c478bd9Sstevel@tonic-gate 	 * Initially we want to use our parent's locale.
14217c478bd9Sstevel@tonic-gate 	 */
14227c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
14237c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
14247c478bd9Sstevel@tonic-gate 	(void) strlcpy(parents_locale, setlocale(LC_MESSAGES, NULL),
14257c478bd9Sstevel@tonic-gate 	    sizeof (parents_locale));
14267c478bd9Sstevel@tonic-gate 
14277c478bd9Sstevel@tonic-gate 	/*
14287c478bd9Sstevel@tonic-gate 	 * This zlog_t is used for writing to stderr
14297c478bd9Sstevel@tonic-gate 	 */
14307c478bd9Sstevel@tonic-gate 	errlog.logfile = stderr;
14317c478bd9Sstevel@tonic-gate 	errlog.buflen = errlog.loglen = 0;
14327c478bd9Sstevel@tonic-gate 	errlog.buf = errlog.log = NULL;
14337c478bd9Sstevel@tonic-gate 	errlog.locale = parents_locale;
14347c478bd9Sstevel@tonic-gate 
14357c478bd9Sstevel@tonic-gate 	/*
14367c478bd9Sstevel@tonic-gate 	 * We start off writing to stderr until we're ready to daemonize.
14377c478bd9Sstevel@tonic-gate 	 */
14387c478bd9Sstevel@tonic-gate 	zlogp = &errlog;
14397c478bd9Sstevel@tonic-gate 
14407c478bd9Sstevel@tonic-gate 	/*
14417c478bd9Sstevel@tonic-gate 	 * Process options.
14427c478bd9Sstevel@tonic-gate 	 */
1443108322fbScarlsonj 	while ((opt = getopt(argc, argv, "R:z:")) != EOF) {
14447c478bd9Sstevel@tonic-gate 		switch (opt) {
1445108322fbScarlsonj 		case 'R':
1446108322fbScarlsonj 			zonecfg_set_root(optarg);
1447108322fbScarlsonj 			break;
14487c478bd9Sstevel@tonic-gate 		case 'z':
14497c478bd9Sstevel@tonic-gate 			zone_name = optarg;
14507c478bd9Sstevel@tonic-gate 			break;
14517c478bd9Sstevel@tonic-gate 		default:
14527c478bd9Sstevel@tonic-gate 			usage();
14537c478bd9Sstevel@tonic-gate 		}
14547c478bd9Sstevel@tonic-gate 	}
14557c478bd9Sstevel@tonic-gate 
14567c478bd9Sstevel@tonic-gate 	if (zone_name == NULL)
14577c478bd9Sstevel@tonic-gate 		usage();
14587c478bd9Sstevel@tonic-gate 
14597c478bd9Sstevel@tonic-gate 	/*
14607c478bd9Sstevel@tonic-gate 	 * Because usage() prints directly to stderr, it has gettext()
14617c478bd9Sstevel@tonic-gate 	 * wrapping, which depends on the locale.  But since zerror() calls
14627c478bd9Sstevel@tonic-gate 	 * localize() which tweaks the locale, it is not safe to call zerror()
14637c478bd9Sstevel@tonic-gate 	 * until after the last call to usage().  Fortunately, the last call
14647c478bd9Sstevel@tonic-gate 	 * to usage() is just above and the first call to zerror() is just
14657c478bd9Sstevel@tonic-gate 	 * below.  Don't mess this up.
14667c478bd9Sstevel@tonic-gate 	 */
14677c478bd9Sstevel@tonic-gate 	if (strcmp(zone_name, GLOBAL_ZONENAME) == 0) {
14687c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "cannot manage the %s zone",
14697c478bd9Sstevel@tonic-gate 		    GLOBAL_ZONENAME);
14707c478bd9Sstevel@tonic-gate 		return (1);
14717c478bd9Sstevel@tonic-gate 	}
14727c478bd9Sstevel@tonic-gate 
14737c478bd9Sstevel@tonic-gate 	if (zone_get_id(zone_name, &zid) != 0) {
14743f2f09c1Sdp 		zerror(zlogp, B_FALSE, "could not manage %s: %s", zone_name,
14757c478bd9Sstevel@tonic-gate 		    zonecfg_strerror(Z_NO_ZONE));
14767c478bd9Sstevel@tonic-gate 		return (1);
14777c478bd9Sstevel@tonic-gate 	}
14787c478bd9Sstevel@tonic-gate 
14797c478bd9Sstevel@tonic-gate 	if ((err = zone_get_state(zone_name, &zstate)) != Z_OK) {
14803f2f09c1Sdp 		zerror(zlogp, B_FALSE, "failed to get zone state: %s",
14817c478bd9Sstevel@tonic-gate 		    zonecfg_strerror(err));
14827c478bd9Sstevel@tonic-gate 		return (1);
14837c478bd9Sstevel@tonic-gate 	}
14849acbbeafSnn35248 	if (zstate < ZONE_STATE_INCOMPLETE) {
14857c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE,
14867c478bd9Sstevel@tonic-gate 		    "cannot manage a zone which is in state '%s'",
14877c478bd9Sstevel@tonic-gate 		    zone_state_str(zstate));
14887c478bd9Sstevel@tonic-gate 		return (1);
14897c478bd9Sstevel@tonic-gate 	}
14907c478bd9Sstevel@tonic-gate 
14919acbbeafSnn35248 	/* Get a handle to the brand info for this zone */
14929acbbeafSnn35248 	if ((zone_get_brand(zone_name, brand_name, sizeof (brand_name))
1493123807fbSedp 	    != Z_OK) || (bh = brand_open(brand_name)) == NULL) {
14949acbbeafSnn35248 		zerror(zlogp, B_FALSE, "unable to determine zone brand");
14959acbbeafSnn35248 		return (1);
14969acbbeafSnn35248 	}
1497123807fbSedp 	zone_isnative = brand_is_native(bh);
1498*84561e8cStd153743 	zone_iscluster = (strcmp(brand_name, CLUSTER_BRAND_NAME) == 0);
1499123807fbSedp 	brand_close(bh);
15009acbbeafSnn35248 
15017c478bd9Sstevel@tonic-gate 	/*
15027c478bd9Sstevel@tonic-gate 	 * Check that we have all privileges.  It would be nice to pare
15037c478bd9Sstevel@tonic-gate 	 * this down, but this is at least a first cut.
15047c478bd9Sstevel@tonic-gate 	 */
15057c478bd9Sstevel@tonic-gate 	if ((privset = priv_allocset()) == NULL) {
15067c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "%s failed", "priv_allocset");
15077c478bd9Sstevel@tonic-gate 		return (1);
15087c478bd9Sstevel@tonic-gate 	}
15097c478bd9Sstevel@tonic-gate 
15107c478bd9Sstevel@tonic-gate 	if (getppriv(PRIV_EFFECTIVE, privset) != 0) {
15117c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "%s failed", "getppriv");
15127c478bd9Sstevel@tonic-gate 		priv_freeset(privset);
15137c478bd9Sstevel@tonic-gate 		return (1);
15147c478bd9Sstevel@tonic-gate 	}
15157c478bd9Sstevel@tonic-gate 
15167c478bd9Sstevel@tonic-gate 	if (priv_isfullset(privset) == B_FALSE) {
15177c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "You lack sufficient privilege to "
15183f2f09c1Sdp 		    "run this command (all privs required)");
15197c478bd9Sstevel@tonic-gate 		priv_freeset(privset);
15207c478bd9Sstevel@tonic-gate 		return (1);
15217c478bd9Sstevel@tonic-gate 	}
15227c478bd9Sstevel@tonic-gate 	priv_freeset(privset);
15237c478bd9Sstevel@tonic-gate 
15247c478bd9Sstevel@tonic-gate 	if (mkzonedir(zlogp) != 0)
15257c478bd9Sstevel@tonic-gate 		return (1);
15267c478bd9Sstevel@tonic-gate 
15277c478bd9Sstevel@tonic-gate 	/*
15287c478bd9Sstevel@tonic-gate 	 * Pre-fork: setup shared state
15297c478bd9Sstevel@tonic-gate 	 */
15307c478bd9Sstevel@tonic-gate 	if ((shstate = (void *)mmap(NULL, shstatelen,
15317c478bd9Sstevel@tonic-gate 	    PROT_READ|PROT_WRITE, MAP_SHARED|MAP_ANON, -1, (off_t)0)) ==
15327c478bd9Sstevel@tonic-gate 	    MAP_FAILED) {
15337c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "%s failed", "mmap");
15347c478bd9Sstevel@tonic-gate 		return (1);
15357c478bd9Sstevel@tonic-gate 	}
15367c478bd9Sstevel@tonic-gate 	if (sema_init(&shstate->sem, 0, USYNC_PROCESS, NULL) != 0) {
15377c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "%s failed", "sema_init()");
15387c478bd9Sstevel@tonic-gate 		(void) munmap((char *)shstate, shstatelen);
15397c478bd9Sstevel@tonic-gate 		return (1);
15407c478bd9Sstevel@tonic-gate 	}
15417c478bd9Sstevel@tonic-gate 	shstate->log.logfile = NULL;
15427c478bd9Sstevel@tonic-gate 	shstate->log.buflen = shstatelen - sizeof (*shstate);
15437c478bd9Sstevel@tonic-gate 	shstate->log.loglen = shstate->log.buflen;
15447c478bd9Sstevel@tonic-gate 	shstate->log.buf = (char *)shstate + sizeof (*shstate);
15457c478bd9Sstevel@tonic-gate 	shstate->log.log = shstate->log.buf;
15467c478bd9Sstevel@tonic-gate 	shstate->log.locale = parents_locale;
15477c478bd9Sstevel@tonic-gate 	shstate->status = -1;
15487c478bd9Sstevel@tonic-gate 
15497c478bd9Sstevel@tonic-gate 	/*
15507c478bd9Sstevel@tonic-gate 	 * We need a SIGCHLD handler so the sema_wait() below will wake
15517c478bd9Sstevel@tonic-gate 	 * up if the child dies without doing a sema_post().
15527c478bd9Sstevel@tonic-gate 	 */
15537c478bd9Sstevel@tonic-gate 	(void) sigset(SIGCHLD, sigchld);
15547c478bd9Sstevel@tonic-gate 	/*
15557c478bd9Sstevel@tonic-gate 	 * We must mask SIGCHLD until after we've coped with the fork
15567c478bd9Sstevel@tonic-gate 	 * sufficiently to deal with it; otherwise we can race and
15577c478bd9Sstevel@tonic-gate 	 * receive the signal before pid has been initialized
15587c478bd9Sstevel@tonic-gate 	 * (yes, this really happens).
15597c478bd9Sstevel@tonic-gate 	 */
15607c478bd9Sstevel@tonic-gate 	(void) sigemptyset(&block_cld);
15617c478bd9Sstevel@tonic-gate 	(void) sigaddset(&block_cld, SIGCHLD);
15627c478bd9Sstevel@tonic-gate 	(void) sigprocmask(SIG_BLOCK, &block_cld, NULL);
15637c478bd9Sstevel@tonic-gate 
15645cb4571dSdp 	if ((ctfd = init_template()) == -1) {
15655cb4571dSdp 		zerror(zlogp, B_TRUE, "failed to create contract");
15665cb4571dSdp 		return (1);
15675cb4571dSdp 	}
15685cb4571dSdp 
15697c478bd9Sstevel@tonic-gate 	/*
15707c478bd9Sstevel@tonic-gate 	 * Do not let another thread localize a message while we are forking.
15717c478bd9Sstevel@tonic-gate 	 */
15727c478bd9Sstevel@tonic-gate 	(void) mutex_lock(&msglock);
15737c478bd9Sstevel@tonic-gate 	pid = fork();
15747c478bd9Sstevel@tonic-gate 	(void) mutex_unlock(&msglock);
15755cb4571dSdp 
15765cb4571dSdp 	/*
15775cb4571dSdp 	 * In all cases (parent, child, and in the event of an error) we
15785cb4571dSdp 	 * don't want to cause creation of contracts on subsequent fork()s.
15795cb4571dSdp 	 */
15805cb4571dSdp 	(void) ct_tmpl_clear(ctfd);
15815cb4571dSdp 	(void) close(ctfd);
15825cb4571dSdp 
15837c478bd9Sstevel@tonic-gate 	if (pid == -1) {
15847c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "could not fork");
15857c478bd9Sstevel@tonic-gate 		return (1);
15867c478bd9Sstevel@tonic-gate 
15877c478bd9Sstevel@tonic-gate 	} else if (pid > 0) { /* parent */
15887c478bd9Sstevel@tonic-gate 		(void) sigprocmask(SIG_UNBLOCK, &block_cld, NULL);
15897c478bd9Sstevel@tonic-gate 		/*
15907c478bd9Sstevel@tonic-gate 		 * This marks a window of vulnerability in which we receive
15917c478bd9Sstevel@tonic-gate 		 * the SIGCLD before falling into sema_wait (normally we would
15927c478bd9Sstevel@tonic-gate 		 * get woken up from sema_wait with EINTR upon receipt of
15937c478bd9Sstevel@tonic-gate 		 * SIGCLD).  So we may need to use some other scheme like
15947c478bd9Sstevel@tonic-gate 		 * sema_posting in the sigcld handler.
15957c478bd9Sstevel@tonic-gate 		 * blech
15967c478bd9Sstevel@tonic-gate 		 */
15977c478bd9Sstevel@tonic-gate 		(void) sema_wait(&shstate->sem);
15987c478bd9Sstevel@tonic-gate 		(void) sema_destroy(&shstate->sem);
15997c478bd9Sstevel@tonic-gate 		if (shstate->status != 0)
16007c478bd9Sstevel@tonic-gate 			(void) waitpid(pid, NULL, WNOHANG);
16017c478bd9Sstevel@tonic-gate 		/*
16027c478bd9Sstevel@tonic-gate 		 * It's ok if we die with SIGPIPE.  It's not like we could have
16037c478bd9Sstevel@tonic-gate 		 * done anything about it.
16047c478bd9Sstevel@tonic-gate 		 */
16057c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "%s", shstate->log.buf);
16067c478bd9Sstevel@tonic-gate 		_exit(shstate->status == 0 ? 0 : 1);
16077c478bd9Sstevel@tonic-gate 	}
16087c478bd9Sstevel@tonic-gate 
16097c478bd9Sstevel@tonic-gate 	/*
16107c478bd9Sstevel@tonic-gate 	 * The child charges on.
16117c478bd9Sstevel@tonic-gate 	 */
16127c478bd9Sstevel@tonic-gate 	(void) sigset(SIGCHLD, SIG_DFL);
16137c478bd9Sstevel@tonic-gate 	(void) sigprocmask(SIG_UNBLOCK, &block_cld, NULL);
16147c478bd9Sstevel@tonic-gate 
16157c478bd9Sstevel@tonic-gate 	/*
16167c478bd9Sstevel@tonic-gate 	 * SIGPIPE can be delivered if we write to a socket for which the
16177c478bd9Sstevel@tonic-gate 	 * peer endpoint is gone.  That can lead to too-early termination
16187c478bd9Sstevel@tonic-gate 	 * of zoneadmd, and that's not good eats.
16197c478bd9Sstevel@tonic-gate 	 */
16207c478bd9Sstevel@tonic-gate 	(void) sigset(SIGPIPE, SIG_IGN);
16217c478bd9Sstevel@tonic-gate 	/*
16227c478bd9Sstevel@tonic-gate 	 * Stop using stderr
16237c478bd9Sstevel@tonic-gate 	 */
16247c478bd9Sstevel@tonic-gate 	zlogp = &shstate->log;
16257c478bd9Sstevel@tonic-gate 
16267c478bd9Sstevel@tonic-gate 	/*
16277c478bd9Sstevel@tonic-gate 	 * We don't need stdout/stderr from now on.
16287c478bd9Sstevel@tonic-gate 	 */
16297c478bd9Sstevel@tonic-gate 	closefrom(0);
16307c478bd9Sstevel@tonic-gate 
16317c478bd9Sstevel@tonic-gate 	/*
16327c478bd9Sstevel@tonic-gate 	 * Initialize the syslog zlog_t.  This needs to be done after
16337c478bd9Sstevel@tonic-gate 	 * the call to closefrom().
16347c478bd9Sstevel@tonic-gate 	 */
16357c478bd9Sstevel@tonic-gate 	logsys.buf = logsys.log = NULL;
16367c478bd9Sstevel@tonic-gate 	logsys.buflen = logsys.loglen = 0;
16377c478bd9Sstevel@tonic-gate 	logsys.logfile = NULL;
16387c478bd9Sstevel@tonic-gate 	logsys.locale = DEFAULT_LOCALE;
16397c478bd9Sstevel@tonic-gate 
16407c478bd9Sstevel@tonic-gate 	openlog("zoneadmd", LOG_PID, LOG_DAEMON);
16417c478bd9Sstevel@tonic-gate 
16427c478bd9Sstevel@tonic-gate 	/*
16437c478bd9Sstevel@tonic-gate 	 * The eventstream is used to publish state changes in the zone
16447c478bd9Sstevel@tonic-gate 	 * from the door threads to the console I/O poller.
16457c478bd9Sstevel@tonic-gate 	 */
16467c478bd9Sstevel@tonic-gate 	if (eventstream_init() == -1) {
16477c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "unable to create eventstream");
16487c478bd9Sstevel@tonic-gate 		goto child_out;
16497c478bd9Sstevel@tonic-gate 	}
16507c478bd9Sstevel@tonic-gate 
16517c478bd9Sstevel@tonic-gate 	(void) snprintf(zone_door_path, sizeof (zone_door_path),
1652108322fbScarlsonj 	    "%s" ZONE_DOOR_PATH, zonecfg_get_root(), zone_name);
16537c478bd9Sstevel@tonic-gate 
16547c478bd9Sstevel@tonic-gate 	/*
16557c478bd9Sstevel@tonic-gate 	 * See if another zoneadmd is running for this zone.  If not, then we
16567c478bd9Sstevel@tonic-gate 	 * can now modify system state.
16577c478bd9Sstevel@tonic-gate 	 */
16587c478bd9Sstevel@tonic-gate 	if (make_daemon_exclusive(zlogp) == -1)
16597c478bd9Sstevel@tonic-gate 		goto child_out;
16607c478bd9Sstevel@tonic-gate 
16617c478bd9Sstevel@tonic-gate 
16627c478bd9Sstevel@tonic-gate 	/*
16637c478bd9Sstevel@tonic-gate 	 * Create/join a new session; we need to be careful of what we do with
16647c478bd9Sstevel@tonic-gate 	 * the console from now on so we don't end up being the session leader
16657c478bd9Sstevel@tonic-gate 	 * for the terminal we're going to be handing out.
16667c478bd9Sstevel@tonic-gate 	 */
16677c478bd9Sstevel@tonic-gate 	(void) setsid();
16687c478bd9Sstevel@tonic-gate 
16697c478bd9Sstevel@tonic-gate 	/*
16707c478bd9Sstevel@tonic-gate 	 * This thread shouldn't be receiving any signals; in particular,
16717c478bd9Sstevel@tonic-gate 	 * SIGCHLD should be received by the thread doing the fork().
16727c478bd9Sstevel@tonic-gate 	 */
16737c478bd9Sstevel@tonic-gate 	(void) sigfillset(&blockset);
16747c478bd9Sstevel@tonic-gate 	(void) thr_sigsetmask(SIG_BLOCK, &blockset, NULL);
16757c478bd9Sstevel@tonic-gate 
16767c478bd9Sstevel@tonic-gate 	/*
16777c478bd9Sstevel@tonic-gate 	 * Setup the console device and get ready to serve the console;
16787c478bd9Sstevel@tonic-gate 	 * once this has completed, we're ready to let console clients
16797c478bd9Sstevel@tonic-gate 	 * make an attempt to connect (they will block until
16807c478bd9Sstevel@tonic-gate 	 * serve_console_sock() below gets called, and any pending
16817c478bd9Sstevel@tonic-gate 	 * connection is accept()ed).
16827c478bd9Sstevel@tonic-gate 	 */
1683108322fbScarlsonj 	if (!zonecfg_in_alt_root() && init_console(zlogp) == -1)
16847c478bd9Sstevel@tonic-gate 		goto child_out;
16857c478bd9Sstevel@tonic-gate 
16867c478bd9Sstevel@tonic-gate 	/*
16877c478bd9Sstevel@tonic-gate 	 * Take the lock now, so that when the door server gets going, we
16887c478bd9Sstevel@tonic-gate 	 * are guaranteed that it won't take a request until we are sure
16897c478bd9Sstevel@tonic-gate 	 * that everything is completely set up.  See the child_out: label
16907c478bd9Sstevel@tonic-gate 	 * below to see why this matters.
16917c478bd9Sstevel@tonic-gate 	 */
16927c478bd9Sstevel@tonic-gate 	(void) mutex_lock(&lock);
16937c478bd9Sstevel@tonic-gate 
1694108322fbScarlsonj 	/* Init semaphore for scratch zones. */
1695108322fbScarlsonj 	if (sema_init(&scratch_sem, 0, USYNC_THREAD, NULL) == -1) {
1696108322fbScarlsonj 		zerror(zlogp, B_TRUE,
1697108322fbScarlsonj 		    "failed to initialize semaphore for scratch zone");
1698108322fbScarlsonj 		goto child_out;
1699108322fbScarlsonj 	}
1700108322fbScarlsonj 
17017c478bd9Sstevel@tonic-gate 	/*
17027c478bd9Sstevel@tonic-gate 	 * Note: door setup must occur *after* the console is setup.
17037c478bd9Sstevel@tonic-gate 	 * This is so that as zlogin tests the door to see if zoneadmd
17047c478bd9Sstevel@tonic-gate 	 * is ready yet, we know that the console will get serviced
17057c478bd9Sstevel@tonic-gate 	 * once door_info() indicates that the door is "up".
17067c478bd9Sstevel@tonic-gate 	 */
17077c478bd9Sstevel@tonic-gate 	if (setup_door(zlogp) == -1)
17087c478bd9Sstevel@tonic-gate 		goto child_out;
17097c478bd9Sstevel@tonic-gate 
17107c478bd9Sstevel@tonic-gate 	/*
17117c478bd9Sstevel@tonic-gate 	 * Things seem OK so far; tell the parent process that we're done
17127c478bd9Sstevel@tonic-gate 	 * with setup tasks.  This will cause the parent to exit, signalling
17137c478bd9Sstevel@tonic-gate 	 * to zoneadm, zlogin, or whatever forked it that we are ready to
17147c478bd9Sstevel@tonic-gate 	 * service requests.
17157c478bd9Sstevel@tonic-gate 	 */
17167c478bd9Sstevel@tonic-gate 	shstate->status = 0;
17177c478bd9Sstevel@tonic-gate 	(void) sema_post(&shstate->sem);
17187c478bd9Sstevel@tonic-gate 	(void) munmap((char *)shstate, shstatelen);
17197c478bd9Sstevel@tonic-gate 	shstate = NULL;
17207c478bd9Sstevel@tonic-gate 
17217c478bd9Sstevel@tonic-gate 	(void) mutex_unlock(&lock);
17227c478bd9Sstevel@tonic-gate 
17237c478bd9Sstevel@tonic-gate 	/*
17247c478bd9Sstevel@tonic-gate 	 * zlogp is now invalid, so reset it to the syslog logger.
17257c478bd9Sstevel@tonic-gate 	 */
17267c478bd9Sstevel@tonic-gate 	zlogp = &logsys;
17277c478bd9Sstevel@tonic-gate 
17287c478bd9Sstevel@tonic-gate 	/*
17297c478bd9Sstevel@tonic-gate 	 * Now that we are free of any parents, switch to the default locale.
17307c478bd9Sstevel@tonic-gate 	 */
17317c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, DEFAULT_LOCALE);
17327c478bd9Sstevel@tonic-gate 
17337c478bd9Sstevel@tonic-gate 	/*
17347c478bd9Sstevel@tonic-gate 	 * At this point the setup portion of main() is basically done, so
17357c478bd9Sstevel@tonic-gate 	 * we reuse this thread to manage the zone console.  When
17367c478bd9Sstevel@tonic-gate 	 * serve_console() has returned, we are past the point of no return
17377c478bd9Sstevel@tonic-gate 	 * in the life of this zoneadmd.
17387c478bd9Sstevel@tonic-gate 	 */
1739108322fbScarlsonj 	if (zonecfg_in_alt_root()) {
1740108322fbScarlsonj 		/*
1741108322fbScarlsonj 		 * This is just awful, but mounted scratch zones don't (and
1742108322fbScarlsonj 		 * can't) have consoles.  We just wait for unmount instead.
1743108322fbScarlsonj 		 */
1744108322fbScarlsonj 		while (sema_wait(&scratch_sem) == EINTR)
1745108322fbScarlsonj 			;
1746108322fbScarlsonj 	} else {
17477c478bd9Sstevel@tonic-gate 		serve_console(zlogp);
17487c478bd9Sstevel@tonic-gate 		assert(in_death_throes);
1749108322fbScarlsonj 	}
17507c478bd9Sstevel@tonic-gate 
17517c478bd9Sstevel@tonic-gate 	/*
17527c478bd9Sstevel@tonic-gate 	 * This is the next-to-last part of the exit interlock.  Upon calling
17537c478bd9Sstevel@tonic-gate 	 * fdetach(), the door will go unreferenced; once any
17547c478bd9Sstevel@tonic-gate 	 * outstanding requests (like the door thread doing Z_HALT) are
17557c478bd9Sstevel@tonic-gate 	 * done, the door will get an UNREF notification; when it handles
17567c478bd9Sstevel@tonic-gate 	 * the UNREF, the door server will cause the exit.
17577c478bd9Sstevel@tonic-gate 	 */
17587c478bd9Sstevel@tonic-gate 	assert(!MUTEX_HELD(&lock));
17597c478bd9Sstevel@tonic-gate 	(void) fdetach(zone_door_path);
17607c478bd9Sstevel@tonic-gate 	for (;;)
17617c478bd9Sstevel@tonic-gate 		(void) pause();
17627c478bd9Sstevel@tonic-gate 
17637c478bd9Sstevel@tonic-gate child_out:
17647c478bd9Sstevel@tonic-gate 	assert(pid == 0);
17657c478bd9Sstevel@tonic-gate 	if (shstate != NULL) {
17667c478bd9Sstevel@tonic-gate 		shstate->status = -1;
17677c478bd9Sstevel@tonic-gate 		(void) sema_post(&shstate->sem);
17687c478bd9Sstevel@tonic-gate 		(void) munmap((char *)shstate, shstatelen);
17697c478bd9Sstevel@tonic-gate 	}
17707c478bd9Sstevel@tonic-gate 
17717c478bd9Sstevel@tonic-gate 	/*
17727c478bd9Sstevel@tonic-gate 	 * This might trigger an unref notification, but if so,
17737c478bd9Sstevel@tonic-gate 	 * we are still holding the lock, so our call to exit will
17747c478bd9Sstevel@tonic-gate 	 * ultimately win the race and will publish the right exit
17757c478bd9Sstevel@tonic-gate 	 * code.
17767c478bd9Sstevel@tonic-gate 	 */
17777c478bd9Sstevel@tonic-gate 	if (zone_door != -1) {
17787c478bd9Sstevel@tonic-gate 		assert(MUTEX_HELD(&lock));
17797c478bd9Sstevel@tonic-gate 		(void) door_revoke(zone_door);
17807c478bd9Sstevel@tonic-gate 		(void) fdetach(zone_door_path);
17817c478bd9Sstevel@tonic-gate 	}
17827c478bd9Sstevel@tonic-gate 	return (1); /* return from main() forcibly exits an MT process */
17837c478bd9Sstevel@tonic-gate }
1784