xref: /onnv-gate/usr/src/cmd/zoneadmd/zoneadmd.c (revision 766:c521de78a32f)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
50Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
70Sstevel@tonic-gate  * with the License.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate  * See the License for the specific language governing permissions
120Sstevel@tonic-gate  * and limitations under the License.
130Sstevel@tonic-gate  *
140Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate  *
200Sstevel@tonic-gate  * CDDL HEADER END
210Sstevel@tonic-gate  */
220Sstevel@tonic-gate /*
230Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
280Sstevel@tonic-gate 
290Sstevel@tonic-gate /*
300Sstevel@tonic-gate  * zoneadmd manages zones; one zoneadmd process is launched for each
310Sstevel@tonic-gate  * non-global zone on the system.  This daemon juggles four jobs:
320Sstevel@tonic-gate  *
330Sstevel@tonic-gate  * - Implement setup and teardown of the zone "virtual platform": mount and
340Sstevel@tonic-gate  *   unmount filesystems; create and destroy network interfaces; communicate
350Sstevel@tonic-gate  *   with devfsadmd to lay out devices for the zone; instantiate the zone
360Sstevel@tonic-gate  *   console device; configure process runtime attributes such as resource
370Sstevel@tonic-gate  *   controls, pool bindings, fine-grained privileges.
380Sstevel@tonic-gate  *
390Sstevel@tonic-gate  * - Launch the zone's init(1M) process.
400Sstevel@tonic-gate  *
410Sstevel@tonic-gate  * - Implement a door server; clients (like zoneadm) connect to the door
420Sstevel@tonic-gate  *   server and request zone state changes.  The kernel is also a client of
430Sstevel@tonic-gate  *   this door server.  A request to halt or reboot the zone which originates
440Sstevel@tonic-gate  *   *inside* the zone results in a door upcall from the kernel into zoneadmd.
450Sstevel@tonic-gate  *
460Sstevel@tonic-gate  *   One minor problem is that messages emitted by zoneadmd need to be passed
470Sstevel@tonic-gate  *   back to the zoneadm process making the request.  These messages need to
480Sstevel@tonic-gate  *   be rendered in the client's locale; so, this is passed in as part of the
490Sstevel@tonic-gate  *   request.  The exception is the kernel upcall to zoneadmd, in which case
500Sstevel@tonic-gate  *   messages are syslog'd.
510Sstevel@tonic-gate  *
520Sstevel@tonic-gate  *   To make all of this work, the Makefile adds -a to xgettext to extract *all*
530Sstevel@tonic-gate  *   strings, and an exclusion file (zoneadmd.xcl) is used to exclude those
540Sstevel@tonic-gate  *   strings which do not need to be translated.
550Sstevel@tonic-gate  *
560Sstevel@tonic-gate  * - Act as a console server for zlogin -C processes; see comments in zcons.c
570Sstevel@tonic-gate  *   for more information about the zone console architecture.
580Sstevel@tonic-gate  *
590Sstevel@tonic-gate  * DESIGN NOTES
600Sstevel@tonic-gate  *
610Sstevel@tonic-gate  * Restart:
620Sstevel@tonic-gate  *   A chief design constraint of zoneadmd is that it should be restartable in
630Sstevel@tonic-gate  *   the case that the administrator kills it off, or it suffers a fatal error,
640Sstevel@tonic-gate  *   without the running zone being impacted; this is akin to being able to
650Sstevel@tonic-gate  *   reboot the service processor of a server without affecting the OS instance.
660Sstevel@tonic-gate  */
670Sstevel@tonic-gate 
680Sstevel@tonic-gate #include <sys/param.h>
690Sstevel@tonic-gate #include <sys/mman.h>
700Sstevel@tonic-gate #include <sys/types.h>
710Sstevel@tonic-gate #include <sys/stat.h>
720Sstevel@tonic-gate #include <sys/sysmacros.h>
730Sstevel@tonic-gate 
740Sstevel@tonic-gate #include <bsm/adt.h>
750Sstevel@tonic-gate #include <bsm/adt_event.h>
760Sstevel@tonic-gate 
770Sstevel@tonic-gate #include <alloca.h>
780Sstevel@tonic-gate #include <assert.h>
790Sstevel@tonic-gate #include <errno.h>
800Sstevel@tonic-gate #include <door.h>
810Sstevel@tonic-gate #include <fcntl.h>
820Sstevel@tonic-gate #include <locale.h>
830Sstevel@tonic-gate #include <signal.h>
840Sstevel@tonic-gate #include <stdarg.h>
850Sstevel@tonic-gate #include <stdio.h>
860Sstevel@tonic-gate #include <stdlib.h>
870Sstevel@tonic-gate #include <string.h>
880Sstevel@tonic-gate #include <strings.h>
890Sstevel@tonic-gate #include <synch.h>
900Sstevel@tonic-gate #include <syslog.h>
910Sstevel@tonic-gate #include <thread.h>
920Sstevel@tonic-gate #include <unistd.h>
930Sstevel@tonic-gate #include <wait.h>
940Sstevel@tonic-gate #include <limits.h>
950Sstevel@tonic-gate #include <zone.h>
960Sstevel@tonic-gate #include <libcontract.h>
970Sstevel@tonic-gate #include <libcontract_priv.h>
980Sstevel@tonic-gate #include <sys/contract/process.h>
990Sstevel@tonic-gate #include <sys/ctfs.h>
1000Sstevel@tonic-gate 
1010Sstevel@tonic-gate #include <libzonecfg.h>
1020Sstevel@tonic-gate #include "zoneadmd.h"
1030Sstevel@tonic-gate 
1040Sstevel@tonic-gate static char *progname;
1050Sstevel@tonic-gate char *zone_name;	/* zone which we are managing */
106*766Scarlsonj static zoneid_t zone_id;
1070Sstevel@tonic-gate 
1080Sstevel@tonic-gate static zlog_t logsys;
1090Sstevel@tonic-gate 
1100Sstevel@tonic-gate mutex_t	lock = DEFAULTMUTEX;	/* to serialize stuff */
1110Sstevel@tonic-gate mutex_t	msglock = DEFAULTMUTEX;	/* for calling setlocale() */
1120Sstevel@tonic-gate 
113*766Scarlsonj static sema_t scratch_sem;	/* for scratch zones */
114*766Scarlsonj 
1150Sstevel@tonic-gate static char	zone_door_path[MAXPATHLEN];
1160Sstevel@tonic-gate static int	zone_door = -1;
1170Sstevel@tonic-gate 
1180Sstevel@tonic-gate boolean_t in_death_throes = B_FALSE;	/* daemon is dying */
1190Sstevel@tonic-gate boolean_t bringup_failure_recovery = B_FALSE; /* ignore certain failures */
1200Sstevel@tonic-gate 
1210Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)		/* should be defined by cc -D */
1220Sstevel@tonic-gate #define	TEXT_DOMAIN	"SYS_TEST"	/* Use this only if it wasn't */
1230Sstevel@tonic-gate #endif
1240Sstevel@tonic-gate 
1250Sstevel@tonic-gate #define	PATH_TO_INIT	"/sbin/init"
1260Sstevel@tonic-gate 
1270Sstevel@tonic-gate #define	DEFAULT_LOCALE	"C"
1280Sstevel@tonic-gate 
129*766Scarlsonj static const char *
130*766Scarlsonj z_cmd_name(zone_cmd_t zcmd)
131*766Scarlsonj {
132*766Scarlsonj 	/* This list needs to match the enum in sys/zone.h */
133*766Scarlsonj 	static const char *zcmdstr[] = {
134*766Scarlsonj 		"ready", "boot", "reboot", "halt", "note_uninstalling",
135*766Scarlsonj 		"mount", "unmount"
136*766Scarlsonj 	};
137*766Scarlsonj 
138*766Scarlsonj 	if (zcmd >= sizeof (zcmdstr) / sizeof (*zcmdstr))
139*766Scarlsonj 		return ("unknown");
140*766Scarlsonj 	else
141*766Scarlsonj 		return (zcmdstr[(int)zcmd]);
142*766Scarlsonj }
143*766Scarlsonj 
1440Sstevel@tonic-gate static char *
1450Sstevel@tonic-gate get_execbasename(char *execfullname)
1460Sstevel@tonic-gate {
1470Sstevel@tonic-gate 	char *last_slash, *execbasename;
1480Sstevel@tonic-gate 
1490Sstevel@tonic-gate 	/* guard against '/' at end of command invocation */
1500Sstevel@tonic-gate 	for (;;) {
1510Sstevel@tonic-gate 		last_slash = strrchr(execfullname, '/');
1520Sstevel@tonic-gate 		if (last_slash == NULL) {
1530Sstevel@tonic-gate 			execbasename = execfullname;
1540Sstevel@tonic-gate 			break;
1550Sstevel@tonic-gate 		} else {
1560Sstevel@tonic-gate 			execbasename = last_slash + 1;
1570Sstevel@tonic-gate 			if (*execbasename == '\0') {
1580Sstevel@tonic-gate 				*last_slash = '\0';
1590Sstevel@tonic-gate 				continue;
1600Sstevel@tonic-gate 			}
1610Sstevel@tonic-gate 			break;
1620Sstevel@tonic-gate 		}
1630Sstevel@tonic-gate 	}
1640Sstevel@tonic-gate 	return (execbasename);
1650Sstevel@tonic-gate }
1660Sstevel@tonic-gate 
1670Sstevel@tonic-gate static void
1680Sstevel@tonic-gate usage(void)
1690Sstevel@tonic-gate {
1700Sstevel@tonic-gate 	(void) fprintf(stderr, gettext("Usage: %s -z zonename\n"), progname);
1710Sstevel@tonic-gate 	(void) fprintf(stderr,
1720Sstevel@tonic-gate 	    gettext("\tNote: %s should not be run directly.\n"), progname);
1730Sstevel@tonic-gate 	exit(2);
1740Sstevel@tonic-gate }
1750Sstevel@tonic-gate 
1760Sstevel@tonic-gate /* ARGSUSED */
1770Sstevel@tonic-gate static void
1780Sstevel@tonic-gate sigchld(int sig)
1790Sstevel@tonic-gate {
1800Sstevel@tonic-gate }
1810Sstevel@tonic-gate 
1820Sstevel@tonic-gate char *
1830Sstevel@tonic-gate localize_msg(char *locale, const char *msg)
1840Sstevel@tonic-gate {
1850Sstevel@tonic-gate 	char *out;
1860Sstevel@tonic-gate 
1870Sstevel@tonic-gate 	(void) mutex_lock(&msglock);
1880Sstevel@tonic-gate 	(void) setlocale(LC_MESSAGES, locale);
1890Sstevel@tonic-gate 	out = gettext(msg);
1900Sstevel@tonic-gate 	(void) setlocale(LC_MESSAGES, DEFAULT_LOCALE);
1910Sstevel@tonic-gate 	(void) mutex_unlock(&msglock);
1920Sstevel@tonic-gate 	return (out);
1930Sstevel@tonic-gate }
1940Sstevel@tonic-gate 
1950Sstevel@tonic-gate /* PRINTFLIKE3 */
1960Sstevel@tonic-gate void
1970Sstevel@tonic-gate zerror(zlog_t *zlogp, boolean_t use_strerror, const char *fmt, ...)
1980Sstevel@tonic-gate {
1990Sstevel@tonic-gate 	va_list alist;
2000Sstevel@tonic-gate 	char buf[MAXPATHLEN * 2]; /* enough space for err msg with a path */
2010Sstevel@tonic-gate 	char *bp;
2020Sstevel@tonic-gate 	int saved_errno = errno;
2030Sstevel@tonic-gate 
2040Sstevel@tonic-gate 	if (zlogp == NULL)
2050Sstevel@tonic-gate 		return;
2060Sstevel@tonic-gate 	if (zlogp == &logsys)
2070Sstevel@tonic-gate 		(void) snprintf(buf, sizeof (buf), "[zone '%s'] ",
2080Sstevel@tonic-gate 		    zone_name);
2090Sstevel@tonic-gate 	else
2100Sstevel@tonic-gate 		buf[0] = '\0';
2110Sstevel@tonic-gate 	bp = &(buf[strlen(buf)]);
2120Sstevel@tonic-gate 
2130Sstevel@tonic-gate 	/*
2140Sstevel@tonic-gate 	 * In theory, the locale pointer should be set to either "C" or a
2150Sstevel@tonic-gate 	 * char array, so it should never be NULL
2160Sstevel@tonic-gate 	 */
2170Sstevel@tonic-gate 	assert(zlogp->locale != NULL);
2180Sstevel@tonic-gate 	/* Locale is per process, but we are multi-threaded... */
2190Sstevel@tonic-gate 	fmt = localize_msg(zlogp->locale, fmt);
2200Sstevel@tonic-gate 
2210Sstevel@tonic-gate 	va_start(alist, fmt);
2220Sstevel@tonic-gate 	(void) vsnprintf(bp, sizeof (buf) - (bp - buf), fmt, alist);
2230Sstevel@tonic-gate 	va_end(alist);
2240Sstevel@tonic-gate 	bp = &(buf[strlen(buf)]);
2250Sstevel@tonic-gate 	if (use_strerror)
2260Sstevel@tonic-gate 		(void) snprintf(bp, sizeof (buf) - (bp - buf), ": %s",
2270Sstevel@tonic-gate 		    strerror(saved_errno));
2280Sstevel@tonic-gate 	if (zlogp == &logsys) {
2290Sstevel@tonic-gate 		(void) syslog(LOG_ERR, "%s", buf);
2300Sstevel@tonic-gate 	} else if (zlogp->logfile != NULL) {
2310Sstevel@tonic-gate 		(void) fprintf(zlogp->logfile, "%s\n", buf);
2320Sstevel@tonic-gate 	} else {
2330Sstevel@tonic-gate 		size_t buflen;
2340Sstevel@tonic-gate 		size_t copylen;
2350Sstevel@tonic-gate 
2360Sstevel@tonic-gate 		buflen = snprintf(zlogp->log, zlogp->loglen, "%s\n", buf);
2370Sstevel@tonic-gate 		copylen = MIN(buflen, zlogp->loglen);
2380Sstevel@tonic-gate 		zlogp->log += copylen;
2390Sstevel@tonic-gate 		zlogp->loglen -= copylen;
2400Sstevel@tonic-gate 	}
2410Sstevel@tonic-gate }
2420Sstevel@tonic-gate 
2430Sstevel@tonic-gate static int
2440Sstevel@tonic-gate mkzonedir(zlog_t *zlogp)
2450Sstevel@tonic-gate {
2460Sstevel@tonic-gate 	struct stat st;
2470Sstevel@tonic-gate 	/*
2480Sstevel@tonic-gate 	 * We must create and lock everyone but root out of ZONES_TMPDIR
2490Sstevel@tonic-gate 	 * since anyone can open any UNIX domain socket, regardless of
2500Sstevel@tonic-gate 	 * its file system permissions.  Sigh...
2510Sstevel@tonic-gate 	 */
2520Sstevel@tonic-gate 	if (mkdir(ZONES_TMPDIR, S_IRWXU) < 0 && errno != EEXIST) {
2530Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "could not mkdir '%s'", ZONES_TMPDIR);
2540Sstevel@tonic-gate 		return (-1);
2550Sstevel@tonic-gate 	}
2560Sstevel@tonic-gate 	/* paranoia */
2570Sstevel@tonic-gate 	if ((stat(ZONES_TMPDIR, &st) < 0) || ((st.st_mode & S_IFDIR) == 0)) {
2580Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "'%s' is not a directory", ZONES_TMPDIR);
2590Sstevel@tonic-gate 		return (-1);
2600Sstevel@tonic-gate 	}
2610Sstevel@tonic-gate 	(void) chmod(ZONES_TMPDIR, S_IRWXU);
2620Sstevel@tonic-gate 	return (0);
2630Sstevel@tonic-gate }
2640Sstevel@tonic-gate 
265*766Scarlsonj /*
266*766Scarlsonj  * Bring a zone up to the pre-boot "ready" stage.  The mount_cmd argument is
267*766Scarlsonj  * 'true' if this is being invoked as part of the processing for the "mount"
268*766Scarlsonj  * subcommand.
269*766Scarlsonj  */
270*766Scarlsonj static int
271*766Scarlsonj zone_ready(zlog_t *zlogp, boolean_t mount_cmd)
2720Sstevel@tonic-gate {
2730Sstevel@tonic-gate 	int err;
2740Sstevel@tonic-gate 
2750Sstevel@tonic-gate 	if ((err = zonecfg_create_snapshot(zone_name)) != Z_OK) {
2760Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "unable to create snapshot: %s",
2770Sstevel@tonic-gate 		    zonecfg_strerror(err));
2780Sstevel@tonic-gate 		return (-1);
2790Sstevel@tonic-gate 	}
2800Sstevel@tonic-gate 
281*766Scarlsonj 	if ((zone_id = vplat_create(zlogp, mount_cmd)) == -1) {
2820Sstevel@tonic-gate 		if ((err = zonecfg_destroy_snapshot(zone_name)) != Z_OK)
2830Sstevel@tonic-gate 			zerror(zlogp, B_FALSE, "destroying snapshot: %s",
2840Sstevel@tonic-gate 			    zonecfg_strerror(err));
2850Sstevel@tonic-gate 		return (-1);
2860Sstevel@tonic-gate 	}
287*766Scarlsonj 	if (vplat_bringup(zlogp, mount_cmd) != 0) {
2880Sstevel@tonic-gate 		bringup_failure_recovery = B_TRUE;
289*766Scarlsonj 		(void) vplat_teardown(NULL, mount_cmd);
2900Sstevel@tonic-gate 		if ((err = zonecfg_destroy_snapshot(zone_name)) != Z_OK)
2910Sstevel@tonic-gate 			zerror(zlogp, B_FALSE, "destroying snapshot: %s",
2920Sstevel@tonic-gate 			    zonecfg_strerror(err));
2930Sstevel@tonic-gate 		return (-1);
2940Sstevel@tonic-gate 	}
2950Sstevel@tonic-gate 
2960Sstevel@tonic-gate 	return (0);
2970Sstevel@tonic-gate }
2980Sstevel@tonic-gate 
2990Sstevel@tonic-gate static int
3000Sstevel@tonic-gate init_template()
3010Sstevel@tonic-gate {
3020Sstevel@tonic-gate 	int fd;
3030Sstevel@tonic-gate 	int err = 0;
3040Sstevel@tonic-gate 
3050Sstevel@tonic-gate 	fd = open64(CTFS_ROOT "/process/template", O_RDWR);
3060Sstevel@tonic-gate 	if (fd == -1)
3070Sstevel@tonic-gate 		return (-1);
3080Sstevel@tonic-gate 
3090Sstevel@tonic-gate 	/*
3100Sstevel@tonic-gate 	 * For now, zoneadmd doesn't do anything with the contract.
3110Sstevel@tonic-gate 	 * Deliver no events, don't inherit, and allow it to be orphaned.
3120Sstevel@tonic-gate 	 */
3130Sstevel@tonic-gate 	err |= ct_tmpl_set_critical(fd, 0);
3140Sstevel@tonic-gate 	err |= ct_tmpl_set_informative(fd, 0);
3150Sstevel@tonic-gate 	err |= ct_pr_tmpl_set_fatal(fd, CT_PR_EV_HWERR);
3160Sstevel@tonic-gate 	err |= ct_pr_tmpl_set_param(fd, CT_PR_PGRPONLY | CT_PR_REGENT);
3170Sstevel@tonic-gate 	if (err || ct_tmpl_activate(fd)) {
3180Sstevel@tonic-gate 		(void) close(fd);
3190Sstevel@tonic-gate 		return (-1);
3200Sstevel@tonic-gate 	}
3210Sstevel@tonic-gate 
3220Sstevel@tonic-gate 	return (fd);
3230Sstevel@tonic-gate }
3240Sstevel@tonic-gate 
3250Sstevel@tonic-gate static int
3260Sstevel@tonic-gate mount_early_fs(zlog_t *zlogp, zoneid_t zoneid, const char *spec,
3270Sstevel@tonic-gate     const char *dir, char *fstype)
3280Sstevel@tonic-gate {
3290Sstevel@tonic-gate 	pid_t child;
3300Sstevel@tonic-gate 	int child_status;
3310Sstevel@tonic-gate 	int tmpl_fd;
3320Sstevel@tonic-gate 	ctid_t ct;
3330Sstevel@tonic-gate 
3340Sstevel@tonic-gate 	if ((tmpl_fd = init_template()) == -1) {
3350Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "failed to create contract");
3360Sstevel@tonic-gate 		return (-1);
3370Sstevel@tonic-gate 	}
3380Sstevel@tonic-gate 
3390Sstevel@tonic-gate 	if ((child = fork()) == -1) {
3400Sstevel@tonic-gate 		(void) ct_tmpl_clear(tmpl_fd);
3410Sstevel@tonic-gate 		(void) close(tmpl_fd);
3420Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "failed to fork");
3430Sstevel@tonic-gate 		return (-1);
3440Sstevel@tonic-gate 
3450Sstevel@tonic-gate 	} else if (child == 0) {	/* child */
3460Sstevel@tonic-gate 		(void) ct_tmpl_clear(tmpl_fd);
3470Sstevel@tonic-gate 		/*
3480Sstevel@tonic-gate 		 * Even though there are no procs running in the zone, we
3490Sstevel@tonic-gate 		 * do this for paranoia's sake.
3500Sstevel@tonic-gate 		 */
3510Sstevel@tonic-gate 		(void) closefrom(0);
3520Sstevel@tonic-gate 
3530Sstevel@tonic-gate 		if (zone_enter(zoneid) == -1) {
3540Sstevel@tonic-gate 			_exit(errno);
3550Sstevel@tonic-gate 		}
3560Sstevel@tonic-gate 		if (mount(spec, dir, MS_DATA, fstype, NULL, 0, NULL, 0) != 0)
3570Sstevel@tonic-gate 			_exit(errno);
3580Sstevel@tonic-gate 		_exit(0);
3590Sstevel@tonic-gate 	}
3600Sstevel@tonic-gate 
3610Sstevel@tonic-gate 	/* parent */
3620Sstevel@tonic-gate 	if (contract_latest(&ct) == -1)
3630Sstevel@tonic-gate 		ct = -1;
3640Sstevel@tonic-gate 	(void) ct_tmpl_clear(tmpl_fd);
3650Sstevel@tonic-gate 	(void) close(tmpl_fd);
3660Sstevel@tonic-gate 	if (waitpid(child, &child_status, 0) != child) {
3670Sstevel@tonic-gate 		/* unexpected: we must have been signalled */
3680Sstevel@tonic-gate 		(void) contract_abandon_id(ct);
3690Sstevel@tonic-gate 		return (-1);
3700Sstevel@tonic-gate 	}
3710Sstevel@tonic-gate 	(void) contract_abandon_id(ct);
3720Sstevel@tonic-gate 	if (WEXITSTATUS(child_status) != 0) {
3730Sstevel@tonic-gate 		errno = WEXITSTATUS(child_status);
3740Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "mount of %s failed", dir);
3750Sstevel@tonic-gate 		return (-1);
3760Sstevel@tonic-gate 	}
3770Sstevel@tonic-gate 
3780Sstevel@tonic-gate 	return (0);
3790Sstevel@tonic-gate }
3800Sstevel@tonic-gate 
3810Sstevel@tonic-gate static int
382*766Scarlsonj zone_mount_early(zlog_t *zlogp, zoneid_t zoneid)
383*766Scarlsonj {
384*766Scarlsonj 	if (mount_early_fs(zlogp, zoneid, "/proc", "/proc", "proc") != 0)
385*766Scarlsonj 		return (-1);
386*766Scarlsonj 
387*766Scarlsonj 	if (mount_early_fs(zlogp, zoneid, "ctfs", CTFS_ROOT, "ctfs") != 0)
388*766Scarlsonj 		return (-1);
389*766Scarlsonj 
390*766Scarlsonj 	if (mount_early_fs(zlogp, zoneid, "swap", "/etc/svc/volatile",
391*766Scarlsonj 	    "tmpfs") != 0)
392*766Scarlsonj 		return (-1);
393*766Scarlsonj 
394*766Scarlsonj 	if (mount_early_fs(zlogp, zoneid, "mnttab", "/etc/mnttab",
395*766Scarlsonj 	    "mntfs") != 0)
396*766Scarlsonj 		return (-1);
397*766Scarlsonj 
398*766Scarlsonj 	return (0);
399*766Scarlsonj }
400*766Scarlsonj 
401*766Scarlsonj static int
4020Sstevel@tonic-gate zone_bootup(zlog_t *zlogp, const char *bootargs)
4030Sstevel@tonic-gate {
4040Sstevel@tonic-gate 	zoneid_t zoneid;
4050Sstevel@tonic-gate 	struct stat st;
4060Sstevel@tonic-gate 	char zroot[MAXPATHLEN], initpath[MAXPATHLEN];
4070Sstevel@tonic-gate 
4080Sstevel@tonic-gate 	if (init_console_slave(zlogp) != 0)
4090Sstevel@tonic-gate 		return (-1);
4100Sstevel@tonic-gate 	reset_slave_terminal(zlogp);
4110Sstevel@tonic-gate 
4120Sstevel@tonic-gate 	if ((zoneid = getzoneidbyname(zone_name)) == -1) {
4130Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "unable to get zoneid");
4140Sstevel@tonic-gate 		return (-1);
4150Sstevel@tonic-gate 	}
4160Sstevel@tonic-gate 
417*766Scarlsonj 	if (zone_mount_early(zlogp, zoneid) != 0)
4180Sstevel@tonic-gate 		return (-1);
4190Sstevel@tonic-gate 
4200Sstevel@tonic-gate 	/*
4210Sstevel@tonic-gate 	 * Try to anticipate possible problems: Make sure init is executable.
4220Sstevel@tonic-gate 	 */
4230Sstevel@tonic-gate 	if (zone_get_rootpath(zone_name, zroot, sizeof (zroot)) != Z_OK) {
4240Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "unable to determine zone root");
4250Sstevel@tonic-gate 		return (-1);
4260Sstevel@tonic-gate 	}
4270Sstevel@tonic-gate 	(void) snprintf(initpath, sizeof (initpath), "%s%s", zroot,
4280Sstevel@tonic-gate 	    PATH_TO_INIT);
4290Sstevel@tonic-gate 
4300Sstevel@tonic-gate 	if (stat(initpath, &st) == -1) {
4310Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "could not stat %s", initpath);
4320Sstevel@tonic-gate 		return (-1);
4330Sstevel@tonic-gate 	}
4340Sstevel@tonic-gate 
4350Sstevel@tonic-gate 	if ((st.st_mode & S_IXUSR) == 0) {
4360Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "%s is not executable", initpath);
4370Sstevel@tonic-gate 		return (-1);
4380Sstevel@tonic-gate 	}
4390Sstevel@tonic-gate 
4400Sstevel@tonic-gate 	if (zone_boot(zoneid, bootargs) == -1) {
4410Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "unable to boot zone");
4420Sstevel@tonic-gate 		return (-1);
4430Sstevel@tonic-gate 	}
4440Sstevel@tonic-gate 
4450Sstevel@tonic-gate 	return (0);
4460Sstevel@tonic-gate }
4470Sstevel@tonic-gate 
4480Sstevel@tonic-gate static int
449*766Scarlsonj zone_halt(zlog_t *zlogp, boolean_t unmount_cmd)
4500Sstevel@tonic-gate {
4510Sstevel@tonic-gate 	int err;
4520Sstevel@tonic-gate 
453*766Scarlsonj 	if (vplat_teardown(zlogp, unmount_cmd) != 0) {
4540Sstevel@tonic-gate 		if (!bringup_failure_recovery)
4550Sstevel@tonic-gate 			zerror(zlogp, B_FALSE, "unable to destroy zone");
4560Sstevel@tonic-gate 		return (-1);
4570Sstevel@tonic-gate 	}
4580Sstevel@tonic-gate 
4590Sstevel@tonic-gate 	if ((err = zonecfg_destroy_snapshot(zone_name)) != Z_OK)
4600Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "destroying snapshot: %s",
4610Sstevel@tonic-gate 		    zonecfg_strerror(err));
4620Sstevel@tonic-gate 
4630Sstevel@tonic-gate 	return (0);
4640Sstevel@tonic-gate }
4650Sstevel@tonic-gate 
4660Sstevel@tonic-gate /*
4670Sstevel@tonic-gate  * Generate AUE_zone_state for a command that boots a zone.
4680Sstevel@tonic-gate  */
4690Sstevel@tonic-gate static void
4700Sstevel@tonic-gate audit_put_record(zlog_t *zlogp, ucred_t *uc, int return_val,
4710Sstevel@tonic-gate     char *new_state)
4720Sstevel@tonic-gate {
4730Sstevel@tonic-gate 	adt_session_data_t	*ah;
4740Sstevel@tonic-gate 	adt_event_data_t	*event;
4750Sstevel@tonic-gate 	int			pass_fail, fail_reason;
4760Sstevel@tonic-gate 
4770Sstevel@tonic-gate 	if (!adt_audit_enabled())
4780Sstevel@tonic-gate 		return;
4790Sstevel@tonic-gate 
4800Sstevel@tonic-gate 	if (return_val == 0) {
4810Sstevel@tonic-gate 		pass_fail = ADT_SUCCESS;
4820Sstevel@tonic-gate 		fail_reason = ADT_SUCCESS;
4830Sstevel@tonic-gate 	} else {
4840Sstevel@tonic-gate 		pass_fail = ADT_FAILURE;
4850Sstevel@tonic-gate 		fail_reason = ADT_FAIL_VALUE_PROGRAM;
4860Sstevel@tonic-gate 	}
4870Sstevel@tonic-gate 
4880Sstevel@tonic-gate 	if (adt_start_session(&ah, NULL, 0)) {
4890Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, gettext("audit failure."));
4900Sstevel@tonic-gate 		return;
4910Sstevel@tonic-gate 	}
4920Sstevel@tonic-gate 	if (adt_set_from_ucred(ah, uc, ADT_NEW)) {
4930Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, gettext("audit failure."));
4940Sstevel@tonic-gate 		(void) adt_end_session(ah);
4950Sstevel@tonic-gate 		return;
4960Sstevel@tonic-gate 	}
4970Sstevel@tonic-gate 
4980Sstevel@tonic-gate 	event = adt_alloc_event(ah, ADT_zone_state);
4990Sstevel@tonic-gate 	if (event == NULL) {
5000Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, gettext("audit failure."));
5010Sstevel@tonic-gate 		(void) adt_end_session(ah);
5020Sstevel@tonic-gate 		return;
5030Sstevel@tonic-gate 	}
5040Sstevel@tonic-gate 	event->adt_zone_state.zonename = zone_name;
5050Sstevel@tonic-gate 	event->adt_zone_state.new_state = new_state;
5060Sstevel@tonic-gate 
5070Sstevel@tonic-gate 	if (adt_put_event(event, pass_fail, fail_reason))
5080Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, gettext("audit failure."));
5090Sstevel@tonic-gate 
5100Sstevel@tonic-gate 	adt_free_event(event);
5110Sstevel@tonic-gate 
5120Sstevel@tonic-gate 	(void) adt_end_session(ah);
5130Sstevel@tonic-gate }
5140Sstevel@tonic-gate 
5150Sstevel@tonic-gate /*
5160Sstevel@tonic-gate  * The main routine for the door server that deals with zone state transitions.
5170Sstevel@tonic-gate  */
5180Sstevel@tonic-gate /* ARGSUSED */
5190Sstevel@tonic-gate static void
5200Sstevel@tonic-gate server(void *cookie, char *args, size_t alen, door_desc_t *dp,
5210Sstevel@tonic-gate     uint_t n_desc)
5220Sstevel@tonic-gate {
5230Sstevel@tonic-gate 	ucred_t *uc = NULL;
5240Sstevel@tonic-gate 	const priv_set_t *eset;
5250Sstevel@tonic-gate 
5260Sstevel@tonic-gate 	zone_state_t zstate;
5270Sstevel@tonic-gate 	zone_cmd_t cmd;
5280Sstevel@tonic-gate 	zone_cmd_arg_t *zargp;
5290Sstevel@tonic-gate 
5300Sstevel@tonic-gate 	boolean_t kernelcall;
5310Sstevel@tonic-gate 
5320Sstevel@tonic-gate 	int rval = -1;
5330Sstevel@tonic-gate 	uint64_t uniqid;
5340Sstevel@tonic-gate 	zoneid_t zoneid = -1;
5350Sstevel@tonic-gate 	zlog_t zlog;
5360Sstevel@tonic-gate 	zlog_t *zlogp;
5370Sstevel@tonic-gate 	zone_cmd_rval_t *rvalp;
5380Sstevel@tonic-gate 	size_t rlen = getpagesize(); /* conservative */
5390Sstevel@tonic-gate 
5400Sstevel@tonic-gate 	/* LINTED E_BAD_PTR_CAST_ALIGN */
5410Sstevel@tonic-gate 	zargp = (zone_cmd_arg_t *)args;
5420Sstevel@tonic-gate 
5430Sstevel@tonic-gate 	/*
5440Sstevel@tonic-gate 	 * When we get the door unref message, we've fdetach'd the door, and
5450Sstevel@tonic-gate 	 * it is time for us to shut down zoneadmd.
5460Sstevel@tonic-gate 	 */
5470Sstevel@tonic-gate 	if (zargp == DOOR_UNREF_DATA) {
5480Sstevel@tonic-gate 		/*
5490Sstevel@tonic-gate 		 * See comment at end of main() for info on the last rites.
5500Sstevel@tonic-gate 		 */
5510Sstevel@tonic-gate 		exit(0);
5520Sstevel@tonic-gate 	}
5530Sstevel@tonic-gate 
5540Sstevel@tonic-gate 	if (zargp == NULL) {
5550Sstevel@tonic-gate 		(void) door_return(NULL, 0, 0, 0);
5560Sstevel@tonic-gate 	}
5570Sstevel@tonic-gate 
5580Sstevel@tonic-gate 	rvalp = alloca(rlen);
5590Sstevel@tonic-gate 	bzero(rvalp, rlen);
5600Sstevel@tonic-gate 	zlog.logfile = NULL;
5610Sstevel@tonic-gate 	zlog.buflen = zlog.loglen = rlen - sizeof (zone_cmd_rval_t) + 1;
5620Sstevel@tonic-gate 	zlog.buf = rvalp->errbuf;
5630Sstevel@tonic-gate 	zlog.log = zlog.buf;
5640Sstevel@tonic-gate 	/* defer initialization of zlog.locale until after credential check */
5650Sstevel@tonic-gate 	zlogp = &zlog;
5660Sstevel@tonic-gate 
5670Sstevel@tonic-gate 	if (alen != sizeof (zone_cmd_arg_t)) {
5680Sstevel@tonic-gate 		/*
5690Sstevel@tonic-gate 		 * This really shouldn't be happening.
5700Sstevel@tonic-gate 		 */
5710Sstevel@tonic-gate 		zerror(&logsys, B_FALSE, "invalid argument");
5720Sstevel@tonic-gate 		goto out;
5730Sstevel@tonic-gate 	}
5740Sstevel@tonic-gate 	cmd = zargp->cmd;
5750Sstevel@tonic-gate 
5760Sstevel@tonic-gate 	if (door_ucred(&uc) != 0) {
5770Sstevel@tonic-gate 		zerror(&logsys, B_TRUE, "door_ucred");
5780Sstevel@tonic-gate 		goto out;
5790Sstevel@tonic-gate 	}
5800Sstevel@tonic-gate 	eset = ucred_getprivset(uc, PRIV_EFFECTIVE);
5810Sstevel@tonic-gate 	if (ucred_getzoneid(uc) != GLOBAL_ZONEID ||
5820Sstevel@tonic-gate 	    (eset != NULL ? !priv_ismember(eset, PRIV_SYS_CONFIG) :
5830Sstevel@tonic-gate 	    ucred_geteuid(uc) != 0)) {
5840Sstevel@tonic-gate 		zerror(&logsys, B_FALSE, "insufficient privileges");
5850Sstevel@tonic-gate 		goto out;
5860Sstevel@tonic-gate 	}
5870Sstevel@tonic-gate 
5880Sstevel@tonic-gate 	kernelcall = ucred_getpid(uc) == 0;
5890Sstevel@tonic-gate 
5900Sstevel@tonic-gate 	/*
5910Sstevel@tonic-gate 	 * This is safe because we only use a zlog_t throughout the
5920Sstevel@tonic-gate 	 * duration of a door call; i.e., by the time the pointer
5930Sstevel@tonic-gate 	 * might become invalid, the door call would be over.
5940Sstevel@tonic-gate 	 */
5950Sstevel@tonic-gate 	zlog.locale = kernelcall ? DEFAULT_LOCALE : zargp->locale;
5960Sstevel@tonic-gate 
5970Sstevel@tonic-gate 	(void) mutex_lock(&lock);
5980Sstevel@tonic-gate 
5990Sstevel@tonic-gate 	/*
6000Sstevel@tonic-gate 	 * Once we start to really die off, we don't want more connections.
6010Sstevel@tonic-gate 	 */
6020Sstevel@tonic-gate 	if (in_death_throes) {
6030Sstevel@tonic-gate 		(void) mutex_unlock(&lock);
6040Sstevel@tonic-gate 		ucred_free(uc);
6050Sstevel@tonic-gate 		(void) door_return(NULL, 0, 0, 0);
6060Sstevel@tonic-gate 		thr_exit(NULL);
6070Sstevel@tonic-gate 	}
6080Sstevel@tonic-gate 
6090Sstevel@tonic-gate 	/*
6100Sstevel@tonic-gate 	 * Check for validity of command.
6110Sstevel@tonic-gate 	 */
6120Sstevel@tonic-gate 	if (cmd != Z_READY && cmd != Z_BOOT && cmd != Z_REBOOT &&
613*766Scarlsonj 	    cmd != Z_HALT && cmd != Z_NOTE_UNINSTALLING && cmd != Z_MOUNT &&
614*766Scarlsonj 	    cmd != Z_UNMOUNT) {
615*766Scarlsonj 		zerror(&logsys, B_FALSE, "invalid command %d", (int)cmd);
6160Sstevel@tonic-gate 		goto out;
6170Sstevel@tonic-gate 	}
6180Sstevel@tonic-gate 
6190Sstevel@tonic-gate 	if (kernelcall && (cmd != Z_HALT && cmd != Z_REBOOT)) {
6200Sstevel@tonic-gate 		/*
6210Sstevel@tonic-gate 		 * Can't happen
6220Sstevel@tonic-gate 		 */
6230Sstevel@tonic-gate 		zerror(&logsys, B_FALSE, "received unexpected kernel upcall %d",
6240Sstevel@tonic-gate 		    cmd);
6250Sstevel@tonic-gate 		goto out;
6260Sstevel@tonic-gate 	}
6270Sstevel@tonic-gate 	/*
6280Sstevel@tonic-gate 	 * We ignore the possibility of someone calling zone_create(2)
6290Sstevel@tonic-gate 	 * explicitly; all requests must come through zoneadmd.
6300Sstevel@tonic-gate 	 */
6310Sstevel@tonic-gate 	if (zone_get_state(zone_name, &zstate) != Z_OK) {
6320Sstevel@tonic-gate 		/*
6330Sstevel@tonic-gate 		 * Something terribly wrong happened
6340Sstevel@tonic-gate 		 */
6350Sstevel@tonic-gate 		zerror(&logsys, B_FALSE, "unable to determine state of zone");
6360Sstevel@tonic-gate 		goto out;
6370Sstevel@tonic-gate 	}
6380Sstevel@tonic-gate 
6390Sstevel@tonic-gate 	if (kernelcall) {
6400Sstevel@tonic-gate 		/*
6410Sstevel@tonic-gate 		 * Kernel-initiated requests may lose their validity if the
6420Sstevel@tonic-gate 		 * zone_t the kernel was referring to has gone away.
6430Sstevel@tonic-gate 		 */
6440Sstevel@tonic-gate 		if ((zoneid = getzoneidbyname(zone_name)) == -1 ||
6450Sstevel@tonic-gate 		    zone_getattr(zoneid, ZONE_ATTR_UNIQID, &uniqid,
6460Sstevel@tonic-gate 		    sizeof (uniqid)) == -1 || uniqid != zargp->uniqid) {
6470Sstevel@tonic-gate 			/*
6480Sstevel@tonic-gate 			 * We're not talking about the same zone. The request
6490Sstevel@tonic-gate 			 * must have arrived too late.  Return error.
6500Sstevel@tonic-gate 			 */
6510Sstevel@tonic-gate 			rval = -1;
6520Sstevel@tonic-gate 			goto out;
6530Sstevel@tonic-gate 		}
6540Sstevel@tonic-gate 		zlogp = &logsys;	/* Log errors to syslog */
6550Sstevel@tonic-gate 	}
6560Sstevel@tonic-gate 
6570Sstevel@tonic-gate 	switch (zstate) {
6580Sstevel@tonic-gate 	case ZONE_STATE_CONFIGURED:
6590Sstevel@tonic-gate 	case ZONE_STATE_INCOMPLETE:
6600Sstevel@tonic-gate 		/*
6610Sstevel@tonic-gate 		 * Not our area of expertise; we just print a nice message
6620Sstevel@tonic-gate 		 * and die off.
6630Sstevel@tonic-gate 		 */
6640Sstevel@tonic-gate 		zerror(zlogp, B_FALSE,
6650Sstevel@tonic-gate 		    "%s operation is invalid for zones in state '%s'",
666*766Scarlsonj 		    z_cmd_name(cmd), zone_state_str(zstate));
6670Sstevel@tonic-gate 		break;
6680Sstevel@tonic-gate 
6690Sstevel@tonic-gate 	case ZONE_STATE_INSTALLED:
6700Sstevel@tonic-gate 		switch (cmd) {
6710Sstevel@tonic-gate 		case Z_READY:
672*766Scarlsonj 			rval = zone_ready(zlogp, B_FALSE);
6730Sstevel@tonic-gate 			if (rval == 0)
6740Sstevel@tonic-gate 				eventstream_write(Z_EVT_ZONE_READIED);
6750Sstevel@tonic-gate 			break;
6760Sstevel@tonic-gate 		case Z_BOOT:
6770Sstevel@tonic-gate 			eventstream_write(Z_EVT_ZONE_BOOTING);
678*766Scarlsonj 			if ((rval = zone_ready(zlogp, B_FALSE)) == 0)
6790Sstevel@tonic-gate 				rval = zone_bootup(zlogp, zargp->bootbuf);
6800Sstevel@tonic-gate 			audit_put_record(zlogp, uc, rval, "boot");
6810Sstevel@tonic-gate 			if (rval != 0) {
6820Sstevel@tonic-gate 				bringup_failure_recovery = B_TRUE;
683*766Scarlsonj 				(void) zone_halt(zlogp, B_FALSE);
6840Sstevel@tonic-gate 			}
6850Sstevel@tonic-gate 			break;
6860Sstevel@tonic-gate 		case Z_HALT:
6870Sstevel@tonic-gate 			if (kernelcall)	/* Invalid; can't happen */
6880Sstevel@tonic-gate 				abort();
6890Sstevel@tonic-gate 			/*
6900Sstevel@tonic-gate 			 * We could have two clients racing to halt this
6910Sstevel@tonic-gate 			 * zone; the second client loses, but his request
6920Sstevel@tonic-gate 			 * doesn't fail, since the zone is now in the desired
6930Sstevel@tonic-gate 			 * state.
6940Sstevel@tonic-gate 			 */
6950Sstevel@tonic-gate 			zerror(zlogp, B_FALSE, "zone is already halted");
6960Sstevel@tonic-gate 			rval = 0;
6970Sstevel@tonic-gate 			break;
6980Sstevel@tonic-gate 		case Z_REBOOT:
6990Sstevel@tonic-gate 			if (kernelcall)	/* Invalid; can't happen */
7000Sstevel@tonic-gate 				abort();
7010Sstevel@tonic-gate 			zerror(zlogp, B_FALSE, "%s operation is invalid "
702*766Scarlsonj 			    "for zones in state '%s'", z_cmd_name(cmd),
7030Sstevel@tonic-gate 			    zone_state_str(zstate));
7040Sstevel@tonic-gate 			rval = -1;
7050Sstevel@tonic-gate 			break;
7060Sstevel@tonic-gate 		case Z_NOTE_UNINSTALLING:
7070Sstevel@tonic-gate 			if (kernelcall)	/* Invalid; can't happen */
7080Sstevel@tonic-gate 				abort();
7090Sstevel@tonic-gate 			/*
7100Sstevel@tonic-gate 			 * Tell the console to print out a message about this.
7110Sstevel@tonic-gate 			 * Once it does, we will be in_death_throes.
7120Sstevel@tonic-gate 			 */
7130Sstevel@tonic-gate 			eventstream_write(Z_EVT_ZONE_UNINSTALLING);
7140Sstevel@tonic-gate 			break;
715*766Scarlsonj 		case Z_MOUNT:
716*766Scarlsonj 			if (kernelcall)	/* Invalid; can't happen */
717*766Scarlsonj 				abort();
718*766Scarlsonj 			rval = zone_ready(zlogp, B_TRUE);
719*766Scarlsonj 			if (rval == 0)
720*766Scarlsonj 				rval = zone_mount_early(zlogp, zone_id);
721*766Scarlsonj 			/*
722*766Scarlsonj 			 * Ordinarily, /dev/fd would be mounted inside the zone
723*766Scarlsonj 			 * by svc:/system/filesystem/usr:default, but since
724*766Scarlsonj 			 * we're not booting the zone, we need to do this
725*766Scarlsonj 			 * manually.
726*766Scarlsonj 			 */
727*766Scarlsonj 			if (rval == 0)
728*766Scarlsonj 				rval = mount_early_fs(zlogp, zone_id, "fd",
729*766Scarlsonj 				    "/dev/fd", "fd");
730*766Scarlsonj 			break;
731*766Scarlsonj 		case Z_UNMOUNT:
732*766Scarlsonj 			if (kernelcall)	/* Invalid; can't happen */
733*766Scarlsonj 				abort();
734*766Scarlsonj 			zerror(zlogp, B_FALSE, "zone is already unmounted");
735*766Scarlsonj 			rval = 0;
736*766Scarlsonj 			break;
7370Sstevel@tonic-gate 		}
7380Sstevel@tonic-gate 		break;
7390Sstevel@tonic-gate 
7400Sstevel@tonic-gate 	case ZONE_STATE_READY:
7410Sstevel@tonic-gate 		switch (cmd) {
7420Sstevel@tonic-gate 		case Z_READY:
7430Sstevel@tonic-gate 			/*
7440Sstevel@tonic-gate 			 * We could have two clients racing to ready this
7450Sstevel@tonic-gate 			 * zone; the second client loses, but his request
7460Sstevel@tonic-gate 			 * doesn't fail, since the zone is now in the desired
7470Sstevel@tonic-gate 			 * state.
7480Sstevel@tonic-gate 			 */
7490Sstevel@tonic-gate 			zerror(zlogp, B_FALSE, "zone is already ready");
7500Sstevel@tonic-gate 			rval = 0;
7510Sstevel@tonic-gate 			break;
7520Sstevel@tonic-gate 		case Z_BOOT:
7530Sstevel@tonic-gate 			eventstream_write(Z_EVT_ZONE_BOOTING);
7540Sstevel@tonic-gate 			rval = zone_bootup(zlogp, zargp->bootbuf);
7550Sstevel@tonic-gate 			audit_put_record(zlogp, uc, rval, "boot");
7560Sstevel@tonic-gate 			if (rval != 0) {
7570Sstevel@tonic-gate 				bringup_failure_recovery = B_TRUE;
758*766Scarlsonj 				(void) zone_halt(zlogp, B_FALSE);
7590Sstevel@tonic-gate 			}
7600Sstevel@tonic-gate 			break;
7610Sstevel@tonic-gate 		case Z_HALT:
7620Sstevel@tonic-gate 			if (kernelcall)	/* Invalid; can't happen */
7630Sstevel@tonic-gate 				abort();
764*766Scarlsonj 			if ((rval = zone_halt(zlogp, B_FALSE)) != 0)
7650Sstevel@tonic-gate 				break;
7660Sstevel@tonic-gate 			eventstream_write(Z_EVT_ZONE_HALTED);
7670Sstevel@tonic-gate 			break;
7680Sstevel@tonic-gate 		case Z_REBOOT:
769*766Scarlsonj 		case Z_NOTE_UNINSTALLING:
770*766Scarlsonj 		case Z_MOUNT:
771*766Scarlsonj 		case Z_UNMOUNT:
7720Sstevel@tonic-gate 			if (kernelcall)	/* Invalid; can't happen */
7730Sstevel@tonic-gate 				abort();
7740Sstevel@tonic-gate 			zerror(zlogp, B_FALSE, "%s operation is invalid "
775*766Scarlsonj 			    "for zones in state '%s'", z_cmd_name(cmd),
7760Sstevel@tonic-gate 			    zone_state_str(zstate));
7770Sstevel@tonic-gate 			rval = -1;
7780Sstevel@tonic-gate 			break;
779*766Scarlsonj 		}
780*766Scarlsonj 		break;
781*766Scarlsonj 
782*766Scarlsonj 	case ZONE_STATE_MOUNTED:
783*766Scarlsonj 		switch (cmd) {
784*766Scarlsonj 		case Z_UNMOUNT:
7850Sstevel@tonic-gate 			if (kernelcall)	/* Invalid; can't happen */
7860Sstevel@tonic-gate 				abort();
787*766Scarlsonj 			rval = zone_halt(zlogp, B_TRUE);
788*766Scarlsonj 			if (rval == 0)
789*766Scarlsonj 				(void) sema_post(&scratch_sem);
790*766Scarlsonj 			break;
791*766Scarlsonj 		default:
792*766Scarlsonj 			if (kernelcall)	/* Invalid; can't happen */
793*766Scarlsonj 				abort();
794*766Scarlsonj 			zerror(zlogp, B_FALSE, "%s operation is invalid "
795*766Scarlsonj 			    "for zones in state '%s'", z_cmd_name(cmd),
796*766Scarlsonj 			    zone_state_str(zstate));
7970Sstevel@tonic-gate 			rval = -1;
7980Sstevel@tonic-gate 			break;
7990Sstevel@tonic-gate 		}
8000Sstevel@tonic-gate 		break;
8010Sstevel@tonic-gate 
8020Sstevel@tonic-gate 	case ZONE_STATE_RUNNING:
8030Sstevel@tonic-gate 	case ZONE_STATE_SHUTTING_DOWN:
8040Sstevel@tonic-gate 	case ZONE_STATE_DOWN:
8050Sstevel@tonic-gate 		switch (cmd) {
8060Sstevel@tonic-gate 		case Z_READY:
807*766Scarlsonj 			if ((rval = zone_halt(zlogp, B_FALSE)) != 0)
8080Sstevel@tonic-gate 				break;
809*766Scarlsonj 			if ((rval = zone_ready(zlogp, B_FALSE)) == 0)
8100Sstevel@tonic-gate 				eventstream_write(Z_EVT_ZONE_READIED);
8110Sstevel@tonic-gate 			break;
8120Sstevel@tonic-gate 		case Z_BOOT:
8130Sstevel@tonic-gate 			/*
8140Sstevel@tonic-gate 			 * We could have two clients racing to boot this
8150Sstevel@tonic-gate 			 * zone; the second client loses, but his request
8160Sstevel@tonic-gate 			 * doesn't fail, since the zone is now in the desired
8170Sstevel@tonic-gate 			 * state.
8180Sstevel@tonic-gate 			 */
8190Sstevel@tonic-gate 			zerror(zlogp, B_FALSE, "zone is already booted");
8200Sstevel@tonic-gate 			rval = 0;
8210Sstevel@tonic-gate 			break;
8220Sstevel@tonic-gate 		case Z_HALT:
823*766Scarlsonj 			if ((rval = zone_halt(zlogp, B_FALSE)) != 0)
8240Sstevel@tonic-gate 				break;
8250Sstevel@tonic-gate 			eventstream_write(Z_EVT_ZONE_HALTED);
8260Sstevel@tonic-gate 			break;
8270Sstevel@tonic-gate 		case Z_REBOOT:
8280Sstevel@tonic-gate 			eventstream_write(Z_EVT_ZONE_REBOOTING);
829*766Scarlsonj 			if ((rval = zone_halt(zlogp, B_FALSE)) != 0)
8300Sstevel@tonic-gate 				break;
831*766Scarlsonj 			if ((rval = zone_ready(zlogp, B_FALSE)) == 0) {
8320Sstevel@tonic-gate 				rval = zone_bootup(zlogp, "");
8330Sstevel@tonic-gate 				audit_put_record(zlogp, uc, rval, "reboot");
8340Sstevel@tonic-gate 				if (rval != 0)
835*766Scarlsonj 					(void) zone_halt(zlogp, B_FALSE);
8360Sstevel@tonic-gate 			}
8370Sstevel@tonic-gate 			break;
8380Sstevel@tonic-gate 		case Z_NOTE_UNINSTALLING:
839*766Scarlsonj 		case Z_MOUNT:
840*766Scarlsonj 		case Z_UNMOUNT:
841*766Scarlsonj 			zerror(zlogp, B_FALSE, "%s operation is invalid "
842*766Scarlsonj 			    "for zones in state '%s'", z_cmd_name(cmd),
843*766Scarlsonj 			    zone_state_str(zstate));
8440Sstevel@tonic-gate 			rval = -1;
8450Sstevel@tonic-gate 			break;
8460Sstevel@tonic-gate 		}
8470Sstevel@tonic-gate 		break;
8480Sstevel@tonic-gate 	default:
8490Sstevel@tonic-gate 		abort();
8500Sstevel@tonic-gate 	}
8510Sstevel@tonic-gate 
8520Sstevel@tonic-gate 	/*
8530Sstevel@tonic-gate 	 * Because the state of the zone may have changed, we make sure
8540Sstevel@tonic-gate 	 * to wake the console poller, which is in charge of initiating
8550Sstevel@tonic-gate 	 * the shutdown procedure as necessary.
8560Sstevel@tonic-gate 	 */
8570Sstevel@tonic-gate 	eventstream_write(Z_EVT_NULL);
8580Sstevel@tonic-gate 
8590Sstevel@tonic-gate out:
8600Sstevel@tonic-gate 	(void) mutex_unlock(&lock);
8610Sstevel@tonic-gate 	if (kernelcall) {
8620Sstevel@tonic-gate 		rvalp = NULL;
8630Sstevel@tonic-gate 		rlen = 0;
8640Sstevel@tonic-gate 	} else {
8650Sstevel@tonic-gate 		rvalp->rval = rval;
8660Sstevel@tonic-gate 	}
8670Sstevel@tonic-gate 	if (uc != NULL)
8680Sstevel@tonic-gate 		ucred_free(uc);
8690Sstevel@tonic-gate 	(void) door_return((char *)rvalp, rlen, NULL, 0);
8700Sstevel@tonic-gate 	thr_exit(NULL);
8710Sstevel@tonic-gate }
8720Sstevel@tonic-gate 
8730Sstevel@tonic-gate static int
8740Sstevel@tonic-gate setup_door(zlog_t *zlogp)
8750Sstevel@tonic-gate {
8760Sstevel@tonic-gate 	if ((zone_door = door_create(server, NULL,
8770Sstevel@tonic-gate 	    DOOR_UNREF | DOOR_REFUSE_DESC | DOOR_NO_CANCEL)) < 0) {
8780Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "%s failed", "door_create");
8790Sstevel@tonic-gate 		return (-1);
8800Sstevel@tonic-gate 	}
8810Sstevel@tonic-gate 	(void) fdetach(zone_door_path);
8820Sstevel@tonic-gate 
8830Sstevel@tonic-gate 	if (fattach(zone_door, zone_door_path) != 0) {
8840Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "fattach to %s failed", zone_door_path);
8850Sstevel@tonic-gate 		(void) door_revoke(zone_door);
8860Sstevel@tonic-gate 		(void) fdetach(zone_door_path);
8870Sstevel@tonic-gate 		zone_door = -1;
8880Sstevel@tonic-gate 		return (-1);
8890Sstevel@tonic-gate 	}
8900Sstevel@tonic-gate 	return (0);
8910Sstevel@tonic-gate }
8920Sstevel@tonic-gate 
8930Sstevel@tonic-gate /*
8940Sstevel@tonic-gate  * zoneadm(1m) will start zoneadmd if it thinks it isn't running; this
8950Sstevel@tonic-gate  * is where zoneadmd itself will check to see that another instance of
8960Sstevel@tonic-gate  * zoneadmd isn't already controlling this zone.
8970Sstevel@tonic-gate  *
8980Sstevel@tonic-gate  * The idea here is that we want to open the path to which we will
8990Sstevel@tonic-gate  * attach our door, lock it, and then make sure that no-one has beat us
9000Sstevel@tonic-gate  * to fattach(3c)ing onto it.
9010Sstevel@tonic-gate  *
9020Sstevel@tonic-gate  * fattach(3c) is really a mount, so there are actually two possible
9030Sstevel@tonic-gate  * vnodes we could be dealing with.  Our strategy is as follows:
9040Sstevel@tonic-gate  *
9050Sstevel@tonic-gate  * - If the file we opened is a regular file (common case):
9060Sstevel@tonic-gate  * 	There is no fattach(3c)ed door, so we have a chance of becoming
9070Sstevel@tonic-gate  * 	the managing zoneadmd. We attempt to lock the file: if it is
9080Sstevel@tonic-gate  * 	already locked, that means someone else raced us here, so we
9090Sstevel@tonic-gate  * 	lose and give up.  zoneadm(1m) will try to contact the zoneadmd
9100Sstevel@tonic-gate  * 	that beat us to it.
9110Sstevel@tonic-gate  *
9120Sstevel@tonic-gate  * - If the file we opened is a namefs file:
9130Sstevel@tonic-gate  * 	This means there is already an established door fattach(3c)'ed
9140Sstevel@tonic-gate  * 	to the rendezvous path.  We've lost the race, so we give up.
9150Sstevel@tonic-gate  * 	Note that in this case we also try to grab the file lock, and
9160Sstevel@tonic-gate  * 	will succeed in acquiring it since the vnode locked by the
9170Sstevel@tonic-gate  * 	"winning" zoneadmd was a regular one, and the one we locked was
9180Sstevel@tonic-gate  * 	the fattach(3c)'ed door node.  At any rate, no harm is done, and
9190Sstevel@tonic-gate  * 	we just return to zoneadm(1m) which knows to retry.
9200Sstevel@tonic-gate  */
9210Sstevel@tonic-gate static int
9220Sstevel@tonic-gate make_daemon_exclusive(zlog_t *zlogp)
9230Sstevel@tonic-gate {
9240Sstevel@tonic-gate 	int doorfd = -1;
9250Sstevel@tonic-gate 	int err, ret = -1;
9260Sstevel@tonic-gate 	struct stat st;
9270Sstevel@tonic-gate 	struct flock flock;
9280Sstevel@tonic-gate 	zone_state_t zstate;
9290Sstevel@tonic-gate 
9300Sstevel@tonic-gate top:
9310Sstevel@tonic-gate 	if ((err = zone_get_state(zone_name, &zstate)) != Z_OK) {
9320Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "failed to get zone state: %s\n",
9330Sstevel@tonic-gate 		    zonecfg_strerror(err));
9340Sstevel@tonic-gate 		goto out;
9350Sstevel@tonic-gate 	}
9360Sstevel@tonic-gate 	if ((doorfd = open(zone_door_path, O_CREAT|O_RDWR,
9370Sstevel@tonic-gate 	    S_IREAD|S_IWRITE)) < 0) {
9380Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "failed to open %s", zone_door_path);
9390Sstevel@tonic-gate 		goto out;
9400Sstevel@tonic-gate 	}
9410Sstevel@tonic-gate 	if (fstat(doorfd, &st) < 0) {
9420Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "failed to stat %s", zone_door_path);
9430Sstevel@tonic-gate 		goto out;
9440Sstevel@tonic-gate 	}
9450Sstevel@tonic-gate 	/*
9460Sstevel@tonic-gate 	 * Lock the file to synchronize with other zoneadmd
9470Sstevel@tonic-gate 	 */
9480Sstevel@tonic-gate 	flock.l_type = F_WRLCK;
9490Sstevel@tonic-gate 	flock.l_whence = SEEK_SET;
9500Sstevel@tonic-gate 	flock.l_start = (off_t)0;
9510Sstevel@tonic-gate 	flock.l_len = (off_t)0;
9520Sstevel@tonic-gate 	if (fcntl(doorfd, F_SETLK, &flock) < 0) {
9530Sstevel@tonic-gate 		/*
9540Sstevel@tonic-gate 		 * Someone else raced us here and grabbed the lock file
9550Sstevel@tonic-gate 		 * first.  A warning here is inappropriate since nothing
9560Sstevel@tonic-gate 		 * went wrong.
9570Sstevel@tonic-gate 		 */
9580Sstevel@tonic-gate 		goto out;
9590Sstevel@tonic-gate 	}
9600Sstevel@tonic-gate 
9610Sstevel@tonic-gate 	if (strcmp(st.st_fstype, "namefs") == 0) {
9620Sstevel@tonic-gate 		struct door_info info;
9630Sstevel@tonic-gate 
9640Sstevel@tonic-gate 		/*
9650Sstevel@tonic-gate 		 * There is already something fattach()'ed to this file.
9660Sstevel@tonic-gate 		 * Lets see what the door is up to.
9670Sstevel@tonic-gate 		 */
9680Sstevel@tonic-gate 		if (door_info(doorfd, &info) == 0 && info.di_target != -1) {
9690Sstevel@tonic-gate 			/*
9700Sstevel@tonic-gate 			 * Another zoneadmd process seems to be in
9710Sstevel@tonic-gate 			 * control of the situation and we don't need to
9720Sstevel@tonic-gate 			 * be here.  A warning here is inappropriate
9730Sstevel@tonic-gate 			 * since nothing went wrong.
9740Sstevel@tonic-gate 			 *
9750Sstevel@tonic-gate 			 * If the door has been revoked, the zoneadmd
9760Sstevel@tonic-gate 			 * process currently managing the zone is going
9770Sstevel@tonic-gate 			 * away.  We'll return control to zoneadm(1m)
9780Sstevel@tonic-gate 			 * which will try again (by which time zoneadmd
9790Sstevel@tonic-gate 			 * will hopefully have exited).
9800Sstevel@tonic-gate 			 */
9810Sstevel@tonic-gate 			goto out;
9820Sstevel@tonic-gate 		}
9830Sstevel@tonic-gate 
9840Sstevel@tonic-gate 		/*
9850Sstevel@tonic-gate 		 * If we got this far, there's a fattach(3c)'ed door
9860Sstevel@tonic-gate 		 * that belongs to a process that has exited, which can
9870Sstevel@tonic-gate 		 * happen if the previous zoneadmd died unexpectedly.
9880Sstevel@tonic-gate 		 *
9890Sstevel@tonic-gate 		 * Let user know that something is amiss, but that we can
9900Sstevel@tonic-gate 		 * recover; if the zone is in the installed state, then don't
9910Sstevel@tonic-gate 		 * message, since having a running zoneadmd isn't really
9920Sstevel@tonic-gate 		 * expected/needed.  We want to keep occurences of this message
9930Sstevel@tonic-gate 		 * limited to times when zoneadmd is picking back up from a
9940Sstevel@tonic-gate 		 * zoneadmd that died while the zone was in some non-trivial
9950Sstevel@tonic-gate 		 * state.
9960Sstevel@tonic-gate 		 */
9970Sstevel@tonic-gate 		if (zstate > ZONE_STATE_INSTALLED) {
9980Sstevel@tonic-gate 			zerror(zlogp, B_FALSE,
9990Sstevel@tonic-gate 			    "zone '%s': WARNING: zone is in state '%s', but "
10000Sstevel@tonic-gate 			    "zoneadmd does not appear to be available; "
10010Sstevel@tonic-gate 			    "restarted zoneadmd to recover.",
10020Sstevel@tonic-gate 			    zone_name, zone_state_str(zstate));
10030Sstevel@tonic-gate 		}
10040Sstevel@tonic-gate 
10050Sstevel@tonic-gate 		(void) fdetach(zone_door_path);
10060Sstevel@tonic-gate 		(void) close(doorfd);
10070Sstevel@tonic-gate 		goto top;
10080Sstevel@tonic-gate 	}
10090Sstevel@tonic-gate 	ret = 0;
10100Sstevel@tonic-gate out:
10110Sstevel@tonic-gate 	(void) close(doorfd);
10120Sstevel@tonic-gate 	return (ret);
10130Sstevel@tonic-gate }
10140Sstevel@tonic-gate 
10150Sstevel@tonic-gate int
10160Sstevel@tonic-gate main(int argc, char *argv[])
10170Sstevel@tonic-gate {
10180Sstevel@tonic-gate 	int opt;
10190Sstevel@tonic-gate 	zoneid_t zid;
10200Sstevel@tonic-gate 	priv_set_t *privset;
10210Sstevel@tonic-gate 	zone_state_t zstate;
10220Sstevel@tonic-gate 	char parents_locale[MAXPATHLEN];
10230Sstevel@tonic-gate 	int err;
10240Sstevel@tonic-gate 
10250Sstevel@tonic-gate 	pid_t pid;
10260Sstevel@tonic-gate 	sigset_t blockset;
10270Sstevel@tonic-gate 	sigset_t block_cld;
10280Sstevel@tonic-gate 
10290Sstevel@tonic-gate 	struct {
10300Sstevel@tonic-gate 		sema_t sem;
10310Sstevel@tonic-gate 		int status;
10320Sstevel@tonic-gate 		zlog_t log;
10330Sstevel@tonic-gate 	} *shstate;
10340Sstevel@tonic-gate 	size_t shstatelen = getpagesize();
10350Sstevel@tonic-gate 
10360Sstevel@tonic-gate 	zlog_t errlog;
10370Sstevel@tonic-gate 	zlog_t *zlogp;
10380Sstevel@tonic-gate 
10390Sstevel@tonic-gate 	progname = get_execbasename(argv[0]);
10400Sstevel@tonic-gate 
10410Sstevel@tonic-gate 	/*
10420Sstevel@tonic-gate 	 * Make sure stderr is unbuffered
10430Sstevel@tonic-gate 	 */
10440Sstevel@tonic-gate 	(void) setbuffer(stderr, NULL, 0);
10450Sstevel@tonic-gate 
10460Sstevel@tonic-gate 	/*
10470Sstevel@tonic-gate 	 * Get out of the way of mounted filesystems, since we will daemonize
10480Sstevel@tonic-gate 	 * soon.
10490Sstevel@tonic-gate 	 */
10500Sstevel@tonic-gate 	(void) chdir("/");
10510Sstevel@tonic-gate 
10520Sstevel@tonic-gate 	/*
10530Sstevel@tonic-gate 	 * Use the default system umask per PSARC 1998/110 rather than
10540Sstevel@tonic-gate 	 * anything that may have been set by the caller.
10550Sstevel@tonic-gate 	 */
10560Sstevel@tonic-gate 	(void) umask(CMASK);
10570Sstevel@tonic-gate 
10580Sstevel@tonic-gate 	/*
10590Sstevel@tonic-gate 	 * Initially we want to use our parent's locale.
10600Sstevel@tonic-gate 	 */
10610Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
10620Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
10630Sstevel@tonic-gate 	(void) strlcpy(parents_locale, setlocale(LC_MESSAGES, NULL),
10640Sstevel@tonic-gate 	    sizeof (parents_locale));
10650Sstevel@tonic-gate 
10660Sstevel@tonic-gate 	/*
10670Sstevel@tonic-gate 	 * This zlog_t is used for writing to stderr
10680Sstevel@tonic-gate 	 */
10690Sstevel@tonic-gate 	errlog.logfile = stderr;
10700Sstevel@tonic-gate 	errlog.buflen = errlog.loglen = 0;
10710Sstevel@tonic-gate 	errlog.buf = errlog.log = NULL;
10720Sstevel@tonic-gate 	errlog.locale = parents_locale;
10730Sstevel@tonic-gate 
10740Sstevel@tonic-gate 	/*
10750Sstevel@tonic-gate 	 * We start off writing to stderr until we're ready to daemonize.
10760Sstevel@tonic-gate 	 */
10770Sstevel@tonic-gate 	zlogp = &errlog;
10780Sstevel@tonic-gate 
10790Sstevel@tonic-gate 	/*
10800Sstevel@tonic-gate 	 * Process options.
10810Sstevel@tonic-gate 	 */
1082*766Scarlsonj 	while ((opt = getopt(argc, argv, "R:z:")) != EOF) {
10830Sstevel@tonic-gate 		switch (opt) {
1084*766Scarlsonj 		case 'R':
1085*766Scarlsonj 			zonecfg_set_root(optarg);
1086*766Scarlsonj 			break;
10870Sstevel@tonic-gate 		case 'z':
10880Sstevel@tonic-gate 			zone_name = optarg;
10890Sstevel@tonic-gate 			break;
10900Sstevel@tonic-gate 		default:
10910Sstevel@tonic-gate 			usage();
10920Sstevel@tonic-gate 		}
10930Sstevel@tonic-gate 	}
10940Sstevel@tonic-gate 
10950Sstevel@tonic-gate 	if (zone_name == NULL)
10960Sstevel@tonic-gate 		usage();
10970Sstevel@tonic-gate 
10980Sstevel@tonic-gate 	/*
10990Sstevel@tonic-gate 	 * Because usage() prints directly to stderr, it has gettext()
11000Sstevel@tonic-gate 	 * wrapping, which depends on the locale.  But since zerror() calls
11010Sstevel@tonic-gate 	 * localize() which tweaks the locale, it is not safe to call zerror()
11020Sstevel@tonic-gate 	 * until after the last call to usage().  Fortunately, the last call
11030Sstevel@tonic-gate 	 * to usage() is just above and the first call to zerror() is just
11040Sstevel@tonic-gate 	 * below.  Don't mess this up.
11050Sstevel@tonic-gate 	 */
11060Sstevel@tonic-gate 	if (strcmp(zone_name, GLOBAL_ZONENAME) == 0) {
11070Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "cannot manage the %s zone",
11080Sstevel@tonic-gate 		    GLOBAL_ZONENAME);
11090Sstevel@tonic-gate 		return (1);
11100Sstevel@tonic-gate 	}
11110Sstevel@tonic-gate 
11120Sstevel@tonic-gate 	if (zone_get_id(zone_name, &zid) != 0) {
11130Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "could not manage %s: %s\n", zone_name,
11140Sstevel@tonic-gate 		    zonecfg_strerror(Z_NO_ZONE));
11150Sstevel@tonic-gate 		return (1);
11160Sstevel@tonic-gate 	}
11170Sstevel@tonic-gate 
11180Sstevel@tonic-gate 	if ((err = zone_get_state(zone_name, &zstate)) != Z_OK) {
11190Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "failed to get zone state: %s\n",
11200Sstevel@tonic-gate 		    zonecfg_strerror(err));
11210Sstevel@tonic-gate 		return (1);
11220Sstevel@tonic-gate 	}
11230Sstevel@tonic-gate 	if (zstate < ZONE_STATE_INSTALLED) {
11240Sstevel@tonic-gate 		zerror(zlogp, B_FALSE,
11250Sstevel@tonic-gate 		    "cannot manage a zone which is in state '%s'",
11260Sstevel@tonic-gate 		    zone_state_str(zstate));
11270Sstevel@tonic-gate 		return (1);
11280Sstevel@tonic-gate 	}
11290Sstevel@tonic-gate 
11300Sstevel@tonic-gate 	/*
11310Sstevel@tonic-gate 	 * Check that we have all privileges.  It would be nice to pare
11320Sstevel@tonic-gate 	 * this down, but this is at least a first cut.
11330Sstevel@tonic-gate 	 */
11340Sstevel@tonic-gate 	if ((privset = priv_allocset()) == NULL) {
11350Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "%s failed", "priv_allocset");
11360Sstevel@tonic-gate 		return (1);
11370Sstevel@tonic-gate 	}
11380Sstevel@tonic-gate 
11390Sstevel@tonic-gate 	if (getppriv(PRIV_EFFECTIVE, privset) != 0) {
11400Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "%s failed", "getppriv");
11410Sstevel@tonic-gate 		priv_freeset(privset);
11420Sstevel@tonic-gate 		return (1);
11430Sstevel@tonic-gate 	}
11440Sstevel@tonic-gate 
11450Sstevel@tonic-gate 	if (priv_isfullset(privset) == B_FALSE) {
11460Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "You lack sufficient privilege to "
11470Sstevel@tonic-gate 		    "run this command (all privs required)\n");
11480Sstevel@tonic-gate 		priv_freeset(privset);
11490Sstevel@tonic-gate 		return (1);
11500Sstevel@tonic-gate 	}
11510Sstevel@tonic-gate 	priv_freeset(privset);
11520Sstevel@tonic-gate 
11530Sstevel@tonic-gate 	if (mkzonedir(zlogp) != 0)
11540Sstevel@tonic-gate 		return (1);
11550Sstevel@tonic-gate 
11560Sstevel@tonic-gate 	/*
11570Sstevel@tonic-gate 	 * Pre-fork: setup shared state
11580Sstevel@tonic-gate 	 */
11590Sstevel@tonic-gate 	if ((shstate = (void *)mmap(NULL, shstatelen,
11600Sstevel@tonic-gate 	    PROT_READ|PROT_WRITE, MAP_SHARED|MAP_ANON, -1, (off_t)0)) ==
11610Sstevel@tonic-gate 	    MAP_FAILED) {
11620Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "%s failed", "mmap");
11630Sstevel@tonic-gate 		return (1);
11640Sstevel@tonic-gate 	}
11650Sstevel@tonic-gate 	if (sema_init(&shstate->sem, 0, USYNC_PROCESS, NULL) != 0) {
11660Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "%s failed", "sema_init()");
11670Sstevel@tonic-gate 		(void) munmap((char *)shstate, shstatelen);
11680Sstevel@tonic-gate 		return (1);
11690Sstevel@tonic-gate 	}
11700Sstevel@tonic-gate 	shstate->log.logfile = NULL;
11710Sstevel@tonic-gate 	shstate->log.buflen = shstatelen - sizeof (*shstate);
11720Sstevel@tonic-gate 	shstate->log.loglen = shstate->log.buflen;
11730Sstevel@tonic-gate 	shstate->log.buf = (char *)shstate + sizeof (*shstate);
11740Sstevel@tonic-gate 	shstate->log.log = shstate->log.buf;
11750Sstevel@tonic-gate 	shstate->log.locale = parents_locale;
11760Sstevel@tonic-gate 	shstate->status = -1;
11770Sstevel@tonic-gate 
11780Sstevel@tonic-gate 	/*
11790Sstevel@tonic-gate 	 * We need a SIGCHLD handler so the sema_wait() below will wake
11800Sstevel@tonic-gate 	 * up if the child dies without doing a sema_post().
11810Sstevel@tonic-gate 	 */
11820Sstevel@tonic-gate 	(void) sigset(SIGCHLD, sigchld);
11830Sstevel@tonic-gate 	/*
11840Sstevel@tonic-gate 	 * We must mask SIGCHLD until after we've coped with the fork
11850Sstevel@tonic-gate 	 * sufficiently to deal with it; otherwise we can race and
11860Sstevel@tonic-gate 	 * receive the signal before pid has been initialized
11870Sstevel@tonic-gate 	 * (yes, this really happens).
11880Sstevel@tonic-gate 	 */
11890Sstevel@tonic-gate 	(void) sigemptyset(&block_cld);
11900Sstevel@tonic-gate 	(void) sigaddset(&block_cld, SIGCHLD);
11910Sstevel@tonic-gate 	(void) sigprocmask(SIG_BLOCK, &block_cld, NULL);
11920Sstevel@tonic-gate 
11930Sstevel@tonic-gate 	/*
11940Sstevel@tonic-gate 	 * Do not let another thread localize a message while we are forking.
11950Sstevel@tonic-gate 	 */
11960Sstevel@tonic-gate 	(void) mutex_lock(&msglock);
11970Sstevel@tonic-gate 	pid = fork();
11980Sstevel@tonic-gate 	(void) mutex_unlock(&msglock);
11990Sstevel@tonic-gate 	if (pid == -1) {
12000Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "could not fork");
12010Sstevel@tonic-gate 		return (1);
12020Sstevel@tonic-gate 
12030Sstevel@tonic-gate 	} else if (pid > 0) { /* parent */
12040Sstevel@tonic-gate 		(void) sigprocmask(SIG_UNBLOCK, &block_cld, NULL);
12050Sstevel@tonic-gate 		/*
12060Sstevel@tonic-gate 		 * This marks a window of vulnerability in which we receive
12070Sstevel@tonic-gate 		 * the SIGCLD before falling into sema_wait (normally we would
12080Sstevel@tonic-gate 		 * get woken up from sema_wait with EINTR upon receipt of
12090Sstevel@tonic-gate 		 * SIGCLD).  So we may need to use some other scheme like
12100Sstevel@tonic-gate 		 * sema_posting in the sigcld handler.
12110Sstevel@tonic-gate 		 * blech
12120Sstevel@tonic-gate 		 */
12130Sstevel@tonic-gate 		(void) sema_wait(&shstate->sem);
12140Sstevel@tonic-gate 		(void) sema_destroy(&shstate->sem);
12150Sstevel@tonic-gate 		if (shstate->status != 0)
12160Sstevel@tonic-gate 			(void) waitpid(pid, NULL, WNOHANG);
12170Sstevel@tonic-gate 		/*
12180Sstevel@tonic-gate 		 * It's ok if we die with SIGPIPE.  It's not like we could have
12190Sstevel@tonic-gate 		 * done anything about it.
12200Sstevel@tonic-gate 		 */
12210Sstevel@tonic-gate 		(void) fprintf(stderr, "%s", shstate->log.buf);
12220Sstevel@tonic-gate 		_exit(shstate->status == 0 ? 0 : 1);
12230Sstevel@tonic-gate 	}
12240Sstevel@tonic-gate 
12250Sstevel@tonic-gate 	/*
12260Sstevel@tonic-gate 	 * The child charges on.
12270Sstevel@tonic-gate 	 */
12280Sstevel@tonic-gate 	(void) sigset(SIGCHLD, SIG_DFL);
12290Sstevel@tonic-gate 	(void) sigprocmask(SIG_UNBLOCK, &block_cld, NULL);
12300Sstevel@tonic-gate 
12310Sstevel@tonic-gate 	/*
12320Sstevel@tonic-gate 	 * SIGPIPE can be delivered if we write to a socket for which the
12330Sstevel@tonic-gate 	 * peer endpoint is gone.  That can lead to too-early termination
12340Sstevel@tonic-gate 	 * of zoneadmd, and that's not good eats.
12350Sstevel@tonic-gate 	 */
12360Sstevel@tonic-gate 	(void) sigset(SIGPIPE, SIG_IGN);
12370Sstevel@tonic-gate 	/*
12380Sstevel@tonic-gate 	 * Stop using stderr
12390Sstevel@tonic-gate 	 */
12400Sstevel@tonic-gate 	zlogp = &shstate->log;
12410Sstevel@tonic-gate 
12420Sstevel@tonic-gate 	/*
12430Sstevel@tonic-gate 	 * We don't need stdout/stderr from now on.
12440Sstevel@tonic-gate 	 */
12450Sstevel@tonic-gate 	closefrom(0);
12460Sstevel@tonic-gate 
12470Sstevel@tonic-gate 	/*
12480Sstevel@tonic-gate 	 * Initialize the syslog zlog_t.  This needs to be done after
12490Sstevel@tonic-gate 	 * the call to closefrom().
12500Sstevel@tonic-gate 	 */
12510Sstevel@tonic-gate 	logsys.buf = logsys.log = NULL;
12520Sstevel@tonic-gate 	logsys.buflen = logsys.loglen = 0;
12530Sstevel@tonic-gate 	logsys.logfile = NULL;
12540Sstevel@tonic-gate 	logsys.locale = DEFAULT_LOCALE;
12550Sstevel@tonic-gate 
12560Sstevel@tonic-gate 	openlog("zoneadmd", LOG_PID, LOG_DAEMON);
12570Sstevel@tonic-gate 
12580Sstevel@tonic-gate 	/*
12590Sstevel@tonic-gate 	 * The eventstream is used to publish state changes in the zone
12600Sstevel@tonic-gate 	 * from the door threads to the console I/O poller.
12610Sstevel@tonic-gate 	 */
12620Sstevel@tonic-gate 	if (eventstream_init() == -1) {
12630Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "unable to create eventstream");
12640Sstevel@tonic-gate 		goto child_out;
12650Sstevel@tonic-gate 	}
12660Sstevel@tonic-gate 
12670Sstevel@tonic-gate 	(void) snprintf(zone_door_path, sizeof (zone_door_path),
1268*766Scarlsonj 	    "%s" ZONE_DOOR_PATH, zonecfg_get_root(), zone_name);
12690Sstevel@tonic-gate 
12700Sstevel@tonic-gate 	/*
12710Sstevel@tonic-gate 	 * See if another zoneadmd is running for this zone.  If not, then we
12720Sstevel@tonic-gate 	 * can now modify system state.
12730Sstevel@tonic-gate 	 */
12740Sstevel@tonic-gate 	if (make_daemon_exclusive(zlogp) == -1)
12750Sstevel@tonic-gate 		goto child_out;
12760Sstevel@tonic-gate 
12770Sstevel@tonic-gate 
12780Sstevel@tonic-gate 	/*
12790Sstevel@tonic-gate 	 * Create/join a new session; we need to be careful of what we do with
12800Sstevel@tonic-gate 	 * the console from now on so we don't end up being the session leader
12810Sstevel@tonic-gate 	 * for the terminal we're going to be handing out.
12820Sstevel@tonic-gate 	 */
12830Sstevel@tonic-gate 	(void) setsid();
12840Sstevel@tonic-gate 
12850Sstevel@tonic-gate 	/*
12860Sstevel@tonic-gate 	 * This thread shouldn't be receiving any signals; in particular,
12870Sstevel@tonic-gate 	 * SIGCHLD should be received by the thread doing the fork().
12880Sstevel@tonic-gate 	 */
12890Sstevel@tonic-gate 	(void) sigfillset(&blockset);
12900Sstevel@tonic-gate 	(void) thr_sigsetmask(SIG_BLOCK, &blockset, NULL);
12910Sstevel@tonic-gate 
12920Sstevel@tonic-gate 	/*
12930Sstevel@tonic-gate 	 * Setup the console device and get ready to serve the console;
12940Sstevel@tonic-gate 	 * once this has completed, we're ready to let console clients
12950Sstevel@tonic-gate 	 * make an attempt to connect (they will block until
12960Sstevel@tonic-gate 	 * serve_console_sock() below gets called, and any pending
12970Sstevel@tonic-gate 	 * connection is accept()ed).
12980Sstevel@tonic-gate 	 */
1299*766Scarlsonj 	if (!zonecfg_in_alt_root() && init_console(zlogp) == -1)
13000Sstevel@tonic-gate 		goto child_out;
13010Sstevel@tonic-gate 
13020Sstevel@tonic-gate 	/*
13030Sstevel@tonic-gate 	 * Take the lock now, so that when the door server gets going, we
13040Sstevel@tonic-gate 	 * are guaranteed that it won't take a request until we are sure
13050Sstevel@tonic-gate 	 * that everything is completely set up.  See the child_out: label
13060Sstevel@tonic-gate 	 * below to see why this matters.
13070Sstevel@tonic-gate 	 */
13080Sstevel@tonic-gate 	(void) mutex_lock(&lock);
13090Sstevel@tonic-gate 
1310*766Scarlsonj 	/* Init semaphore for scratch zones. */
1311*766Scarlsonj 	if (sema_init(&scratch_sem, 0, USYNC_THREAD, NULL) == -1) {
1312*766Scarlsonj 		zerror(zlogp, B_TRUE,
1313*766Scarlsonj 		    "failed to initialize semaphore for scratch zone");
1314*766Scarlsonj 		goto child_out;
1315*766Scarlsonj 	}
1316*766Scarlsonj 
13170Sstevel@tonic-gate 	/*
13180Sstevel@tonic-gate 	 * Note: door setup must occur *after* the console is setup.
13190Sstevel@tonic-gate 	 * This is so that as zlogin tests the door to see if zoneadmd
13200Sstevel@tonic-gate 	 * is ready yet, we know that the console will get serviced
13210Sstevel@tonic-gate 	 * once door_info() indicates that the door is "up".
13220Sstevel@tonic-gate 	 */
13230Sstevel@tonic-gate 	if (setup_door(zlogp) == -1)
13240Sstevel@tonic-gate 		goto child_out;
13250Sstevel@tonic-gate 
13260Sstevel@tonic-gate 	/*
13270Sstevel@tonic-gate 	 * Things seem OK so far; tell the parent process that we're done
13280Sstevel@tonic-gate 	 * with setup tasks.  This will cause the parent to exit, signalling
13290Sstevel@tonic-gate 	 * to zoneadm, zlogin, or whatever forked it that we are ready to
13300Sstevel@tonic-gate 	 * service requests.
13310Sstevel@tonic-gate 	 */
13320Sstevel@tonic-gate 	shstate->status = 0;
13330Sstevel@tonic-gate 	(void) sema_post(&shstate->sem);
13340Sstevel@tonic-gate 	(void) munmap((char *)shstate, shstatelen);
13350Sstevel@tonic-gate 	shstate = NULL;
13360Sstevel@tonic-gate 
13370Sstevel@tonic-gate 	(void) mutex_unlock(&lock);
13380Sstevel@tonic-gate 
13390Sstevel@tonic-gate 	/*
13400Sstevel@tonic-gate 	 * zlogp is now invalid, so reset it to the syslog logger.
13410Sstevel@tonic-gate 	 */
13420Sstevel@tonic-gate 	zlogp = &logsys;
13430Sstevel@tonic-gate 
13440Sstevel@tonic-gate 	/*
13450Sstevel@tonic-gate 	 * Now that we are free of any parents, switch to the default locale.
13460Sstevel@tonic-gate 	 */
13470Sstevel@tonic-gate 	(void) setlocale(LC_ALL, DEFAULT_LOCALE);
13480Sstevel@tonic-gate 
13490Sstevel@tonic-gate 	/*
13500Sstevel@tonic-gate 	 * At this point the setup portion of main() is basically done, so
13510Sstevel@tonic-gate 	 * we reuse this thread to manage the zone console.  When
13520Sstevel@tonic-gate 	 * serve_console() has returned, we are past the point of no return
13530Sstevel@tonic-gate 	 * in the life of this zoneadmd.
13540Sstevel@tonic-gate 	 */
1355*766Scarlsonj 	if (zonecfg_in_alt_root()) {
1356*766Scarlsonj 		/*
1357*766Scarlsonj 		 * This is just awful, but mounted scratch zones don't (and
1358*766Scarlsonj 		 * can't) have consoles.  We just wait for unmount instead.
1359*766Scarlsonj 		 */
1360*766Scarlsonj 		while (sema_wait(&scratch_sem) == EINTR)
1361*766Scarlsonj 			;
1362*766Scarlsonj 	} else {
1363*766Scarlsonj 		serve_console(zlogp);
1364*766Scarlsonj 		assert(in_death_throes);
1365*766Scarlsonj 	}
13660Sstevel@tonic-gate 
13670Sstevel@tonic-gate 	/*
13680Sstevel@tonic-gate 	 * This is the next-to-last part of the exit interlock.  Upon calling
13690Sstevel@tonic-gate 	 * fdetach(), the door will go unreferenced; once any
13700Sstevel@tonic-gate 	 * outstanding requests (like the door thread doing Z_HALT) are
13710Sstevel@tonic-gate 	 * done, the door will get an UNREF notification; when it handles
13720Sstevel@tonic-gate 	 * the UNREF, the door server will cause the exit.
13730Sstevel@tonic-gate 	 */
13740Sstevel@tonic-gate 	assert(!MUTEX_HELD(&lock));
13750Sstevel@tonic-gate 	(void) fdetach(zone_door_path);
13760Sstevel@tonic-gate 	for (;;)
13770Sstevel@tonic-gate 		(void) pause();
13780Sstevel@tonic-gate 
13790Sstevel@tonic-gate child_out:
13800Sstevel@tonic-gate 	assert(pid == 0);
13810Sstevel@tonic-gate 	if (shstate != NULL) {
13820Sstevel@tonic-gate 		shstate->status = -1;
13830Sstevel@tonic-gate 		(void) sema_post(&shstate->sem);
13840Sstevel@tonic-gate 		(void) munmap((char *)shstate, shstatelen);
13850Sstevel@tonic-gate 	}
13860Sstevel@tonic-gate 
13870Sstevel@tonic-gate 	/*
13880Sstevel@tonic-gate 	 * This might trigger an unref notification, but if so,
13890Sstevel@tonic-gate 	 * we are still holding the lock, so our call to exit will
13900Sstevel@tonic-gate 	 * ultimately win the race and will publish the right exit
13910Sstevel@tonic-gate 	 * code.
13920Sstevel@tonic-gate 	 */
13930Sstevel@tonic-gate 	if (zone_door != -1) {
13940Sstevel@tonic-gate 		assert(MUTEX_HELD(&lock));
13950Sstevel@tonic-gate 		(void) door_revoke(zone_door);
13960Sstevel@tonic-gate 		(void) fdetach(zone_door_path);
13970Sstevel@tonic-gate 	}
13980Sstevel@tonic-gate 	return (1); /* return from main() forcibly exits an MT process */
13990Sstevel@tonic-gate }
1400