xref: /illumos-gate/usr/src/cmd/zoneadmd/zoneadmd.c (revision ef150c2b133e57069ae0f58d5d80cef8980ac9c3)
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 /*
23efd4c9b6SSteve Lawrence  * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
243c7284bdSAlexander Eremin  * Copyright 2014 Nexenta Systems, Inc. All rights reserved.
2548bbca81SDaniel Hoffman  * Copyright (c) 2016 by Delphix. All rights reserved.
267c478bd9Sstevel@tonic-gate  */
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate /*
297c478bd9Sstevel@tonic-gate  * zoneadmd manages zones; one zoneadmd process is launched for each
307c478bd9Sstevel@tonic-gate  * non-global zone on the system.  This daemon juggles four jobs:
317c478bd9Sstevel@tonic-gate  *
327c478bd9Sstevel@tonic-gate  * - Implement setup and teardown of the zone "virtual platform": mount and
337c478bd9Sstevel@tonic-gate  *   unmount filesystems; create and destroy network interfaces; communicate
347c478bd9Sstevel@tonic-gate  *   with devfsadmd to lay out devices for the zone; instantiate the zone
357c478bd9Sstevel@tonic-gate  *   console device; configure process runtime attributes such as resource
367c478bd9Sstevel@tonic-gate  *   controls, pool bindings, fine-grained privileges.
377c478bd9Sstevel@tonic-gate  *
38bbf21555SRichard Lowe  * - Launch the zone's init(8) process.
397c478bd9Sstevel@tonic-gate  *
407c478bd9Sstevel@tonic-gate  * - Implement a door server; clients (like zoneadm) connect to the door
417c478bd9Sstevel@tonic-gate  *   server and request zone state changes.  The kernel is also a client of
427c478bd9Sstevel@tonic-gate  *   this door server.  A request to halt or reboot the zone which originates
437c478bd9Sstevel@tonic-gate  *   *inside* the zone results in a door upcall from the kernel into zoneadmd.
447c478bd9Sstevel@tonic-gate  *
457c478bd9Sstevel@tonic-gate  *   One minor problem is that messages emitted by zoneadmd need to be passed
467c478bd9Sstevel@tonic-gate  *   back to the zoneadm process making the request.  These messages need to
477c478bd9Sstevel@tonic-gate  *   be rendered in the client's locale; so, this is passed in as part of the
487c478bd9Sstevel@tonic-gate  *   request.  The exception is the kernel upcall to zoneadmd, in which case
497c478bd9Sstevel@tonic-gate  *   messages are syslog'd.
507c478bd9Sstevel@tonic-gate  *
517c478bd9Sstevel@tonic-gate  *   To make all of this work, the Makefile adds -a to xgettext to extract *all*
527c478bd9Sstevel@tonic-gate  *   strings, and an exclusion file (zoneadmd.xcl) is used to exclude those
537c478bd9Sstevel@tonic-gate  *   strings which do not need to be translated.
547c478bd9Sstevel@tonic-gate  *
557c478bd9Sstevel@tonic-gate  * - Act as a console server for zlogin -C processes; see comments in zcons.c
567c478bd9Sstevel@tonic-gate  *   for more information about the zone console architecture.
577c478bd9Sstevel@tonic-gate  *
587c478bd9Sstevel@tonic-gate  * DESIGN NOTES
597c478bd9Sstevel@tonic-gate  *
607c478bd9Sstevel@tonic-gate  * Restart:
617c478bd9Sstevel@tonic-gate  *   A chief design constraint of zoneadmd is that it should be restartable in
627c478bd9Sstevel@tonic-gate  *   the case that the administrator kills it off, or it suffers a fatal error,
637c478bd9Sstevel@tonic-gate  *   without the running zone being impacted; this is akin to being able to
647c478bd9Sstevel@tonic-gate  *   reboot the service processor of a server without affecting the OS instance.
657c478bd9Sstevel@tonic-gate  */
667c478bd9Sstevel@tonic-gate 
677c478bd9Sstevel@tonic-gate #include <sys/param.h>
687c478bd9Sstevel@tonic-gate #include <sys/mman.h>
697c478bd9Sstevel@tonic-gate #include <sys/types.h>
707c478bd9Sstevel@tonic-gate #include <sys/stat.h>
717c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
727c478bd9Sstevel@tonic-gate 
737c478bd9Sstevel@tonic-gate #include <bsm/adt.h>
747c478bd9Sstevel@tonic-gate #include <bsm/adt_event.h>
757c478bd9Sstevel@tonic-gate 
767c478bd9Sstevel@tonic-gate #include <alloca.h>
777c478bd9Sstevel@tonic-gate #include <assert.h>
787c478bd9Sstevel@tonic-gate #include <errno.h>
797c478bd9Sstevel@tonic-gate #include <door.h>
807c478bd9Sstevel@tonic-gate #include <fcntl.h>
817c478bd9Sstevel@tonic-gate #include <locale.h>
827c478bd9Sstevel@tonic-gate #include <signal.h>
837c478bd9Sstevel@tonic-gate #include <stdarg.h>
847c478bd9Sstevel@tonic-gate #include <stdio.h>
857c478bd9Sstevel@tonic-gate #include <stdlib.h>
867c478bd9Sstevel@tonic-gate #include <string.h>
877c478bd9Sstevel@tonic-gate #include <strings.h>
887c478bd9Sstevel@tonic-gate #include <synch.h>
897c478bd9Sstevel@tonic-gate #include <syslog.h>
907c478bd9Sstevel@tonic-gate #include <thread.h>
917c478bd9Sstevel@tonic-gate #include <unistd.h>
927c478bd9Sstevel@tonic-gate #include <wait.h>
937c478bd9Sstevel@tonic-gate #include <limits.h>
947c478bd9Sstevel@tonic-gate #include <zone.h>
959acbbeafSnn35248 #include <libbrand.h>
960679f794S #include <sys/brand.h>
977c478bd9Sstevel@tonic-gate #include <libcontract.h>
987c478bd9Sstevel@tonic-gate #include <libcontract_priv.h>
9962868012SSteve Lawrence #include <sys/brand.h>
1007c478bd9Sstevel@tonic-gate #include <sys/contract/process.h>
1017c478bd9Sstevel@tonic-gate #include <sys/ctfs.h>
1022b24ab6bSSebastien Roy #include <libdladm.h>
1032b24ab6bSSebastien Roy #include <sys/dls_mgmt.h>
1043c7284bdSAlexander Eremin #include <libscf.h>
1057c478bd9Sstevel@tonic-gate 
1067c478bd9Sstevel@tonic-gate #include <libzonecfg.h>
107efd4c9b6SSteve Lawrence #include <zonestat_impl.h>
1087c478bd9Sstevel@tonic-gate #include "zoneadmd.h"
1097c478bd9Sstevel@tonic-gate 
1107c478bd9Sstevel@tonic-gate static char *progname;
1117c478bd9Sstevel@tonic-gate char *zone_name;	/* zone which we are managing */
112efd4c9b6SSteve Lawrence char pool_name[MAXNAMELEN];
113e5816e35SEdward Pilatowicz char default_brand[MAXNAMELEN];
1149acbbeafSnn35248 char brand_name[MAXNAMELEN];
1159acbbeafSnn35248 boolean_t zone_isnative;
11684561e8cStd153743 boolean_t zone_iscluster;
1179a5d73e0SRic Aleshire boolean_t zone_islabeled;
1183c7284bdSAlexander Eremin boolean_t shutdown_in_progress;
119108322fbScarlsonj static zoneid_t zone_id;
1204ac67f02SAnurag S. Maskey dladm_handle_t dld_handle = NULL;
1217c478bd9Sstevel@tonic-gate 
122c5cd6260S static char pre_statechg_hook[2 * MAXPATHLEN];
123c5cd6260S static char post_statechg_hook[2 * MAXPATHLEN];
124c5cd6260S char query_hook[2 * MAXPATHLEN];
125c5cd6260S 
12622321485Svp157776 zlog_t logsys;
1277c478bd9Sstevel@tonic-gate 
1287c478bd9Sstevel@tonic-gate mutex_t	lock = DEFAULTMUTEX;	/* to serialize stuff */
1297c478bd9Sstevel@tonic-gate mutex_t	msglock = DEFAULTMUTEX;	/* for calling setlocale() */
1307c478bd9Sstevel@tonic-gate 
131108322fbScarlsonj static sema_t scratch_sem;	/* for scratch zones */
132108322fbScarlsonj 
1337c478bd9Sstevel@tonic-gate static char	zone_door_path[MAXPATHLEN];
1347c478bd9Sstevel@tonic-gate static int	zone_door = -1;
1357c478bd9Sstevel@tonic-gate 
1367c478bd9Sstevel@tonic-gate boolean_t in_death_throes = B_FALSE;	/* daemon is dying */
1377c478bd9Sstevel@tonic-gate boolean_t bringup_failure_recovery = B_FALSE; /* ignore certain failures */
1387c478bd9Sstevel@tonic-gate 
1397c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)		/* should be defined by cc -D */
1407c478bd9Sstevel@tonic-gate #define	TEXT_DOMAIN	"SYS_TEST"	/* Use this only if it wasn't */
1417c478bd9Sstevel@tonic-gate #endif
1427c478bd9Sstevel@tonic-gate 
1437c478bd9Sstevel@tonic-gate #define	DEFAULT_LOCALE	"C"
1447c478bd9Sstevel@tonic-gate 
145108322fbScarlsonj static const char *
z_cmd_name(zone_cmd_t zcmd)146108322fbScarlsonj z_cmd_name(zone_cmd_t zcmd)
147108322fbScarlsonj {
148108322fbScarlsonj 	/* This list needs to match the enum in sys/zone.h */
149108322fbScarlsonj 	static const char *zcmdstr[] = {
1509acbbeafSnn35248 		"ready", "boot", "forceboot", "reboot", "halt",
1513c7284bdSAlexander Eremin 		"note_uninstalling", "mount", "forcemount", "unmount",
1523c7284bdSAlexander Eremin 		"shutdown"
153108322fbScarlsonj 	};
154108322fbScarlsonj 
155108322fbScarlsonj 	if (zcmd >= sizeof (zcmdstr) / sizeof (*zcmdstr))
156108322fbScarlsonj 		return ("unknown");
157108322fbScarlsonj 	else
158108322fbScarlsonj 		return (zcmdstr[(int)zcmd]);
159108322fbScarlsonj }
160108322fbScarlsonj 
1617c478bd9Sstevel@tonic-gate static char *
get_execbasename(char * execfullname)1627c478bd9Sstevel@tonic-gate get_execbasename(char *execfullname)
1637c478bd9Sstevel@tonic-gate {
1647c478bd9Sstevel@tonic-gate 	char *last_slash, *execbasename;
1657c478bd9Sstevel@tonic-gate 
1667c478bd9Sstevel@tonic-gate 	/* guard against '/' at end of command invocation */
1677c478bd9Sstevel@tonic-gate 	for (;;) {
1687c478bd9Sstevel@tonic-gate 		last_slash = strrchr(execfullname, '/');
1697c478bd9Sstevel@tonic-gate 		if (last_slash == NULL) {
1707c478bd9Sstevel@tonic-gate 			execbasename = execfullname;
1717c478bd9Sstevel@tonic-gate 			break;
1727c478bd9Sstevel@tonic-gate 		} else {
1737c478bd9Sstevel@tonic-gate 			execbasename = last_slash + 1;
1747c478bd9Sstevel@tonic-gate 			if (*execbasename == '\0') {
1757c478bd9Sstevel@tonic-gate 				*last_slash = '\0';
1767c478bd9Sstevel@tonic-gate 				continue;
1777c478bd9Sstevel@tonic-gate 			}
1787c478bd9Sstevel@tonic-gate 			break;
1797c478bd9Sstevel@tonic-gate 		}
1807c478bd9Sstevel@tonic-gate 	}
1817c478bd9Sstevel@tonic-gate 	return (execbasename);
1827c478bd9Sstevel@tonic-gate }
1837c478bd9Sstevel@tonic-gate 
1847c478bd9Sstevel@tonic-gate static void
usage(void)1857c478bd9Sstevel@tonic-gate usage(void)
1867c478bd9Sstevel@tonic-gate {
1877c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, gettext("Usage: %s -z zonename\n"), progname);
1887c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr,
1897c478bd9Sstevel@tonic-gate 	    gettext("\tNote: %s should not be run directly.\n"), progname);
1907c478bd9Sstevel@tonic-gate 	exit(2);
1917c478bd9Sstevel@tonic-gate }
1927c478bd9Sstevel@tonic-gate 
1937c478bd9Sstevel@tonic-gate /* ARGSUSED */
1947c478bd9Sstevel@tonic-gate static void
sigchld(int sig)1957c478bd9Sstevel@tonic-gate sigchld(int sig)
1967c478bd9Sstevel@tonic-gate {
1977c478bd9Sstevel@tonic-gate }
1987c478bd9Sstevel@tonic-gate 
1997c478bd9Sstevel@tonic-gate char *
localize_msg(char * locale,const char * msg)2007c478bd9Sstevel@tonic-gate localize_msg(char *locale, const char *msg)
2017c478bd9Sstevel@tonic-gate {
2027c478bd9Sstevel@tonic-gate 	char *out;
2037c478bd9Sstevel@tonic-gate 
2047c478bd9Sstevel@tonic-gate 	(void) mutex_lock(&msglock);
2057c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_MESSAGES, locale);
2067c478bd9Sstevel@tonic-gate 	out = gettext(msg);
2077c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_MESSAGES, DEFAULT_LOCALE);
2087c478bd9Sstevel@tonic-gate 	(void) mutex_unlock(&msglock);
2097c478bd9Sstevel@tonic-gate 	return (out);
2107c478bd9Sstevel@tonic-gate }
2117c478bd9Sstevel@tonic-gate 
2127c478bd9Sstevel@tonic-gate /* PRINTFLIKE3 */
2137c478bd9Sstevel@tonic-gate void
zerror(zlog_t * zlogp,boolean_t use_strerror,const char * fmt,...)2147c478bd9Sstevel@tonic-gate zerror(zlog_t *zlogp, boolean_t use_strerror, const char *fmt, ...)
2157c478bd9Sstevel@tonic-gate {
2167c478bd9Sstevel@tonic-gate 	va_list alist;
2177c478bd9Sstevel@tonic-gate 	char buf[MAXPATHLEN * 2]; /* enough space for err msg with a path */
2187c478bd9Sstevel@tonic-gate 	char *bp;
2197c478bd9Sstevel@tonic-gate 	int saved_errno = errno;
2207c478bd9Sstevel@tonic-gate 
2217c478bd9Sstevel@tonic-gate 	if (zlogp == NULL)
2227c478bd9Sstevel@tonic-gate 		return;
2237c478bd9Sstevel@tonic-gate 	if (zlogp == &logsys)
2247c478bd9Sstevel@tonic-gate 		(void) snprintf(buf, sizeof (buf), "[zone '%s'] ",
2257c478bd9Sstevel@tonic-gate 		    zone_name);
2267c478bd9Sstevel@tonic-gate 	else
2277c478bd9Sstevel@tonic-gate 		buf[0] = '\0';
2287c478bd9Sstevel@tonic-gate 	bp = &(buf[strlen(buf)]);
2297c478bd9Sstevel@tonic-gate 
2307c478bd9Sstevel@tonic-gate 	/*
2317c478bd9Sstevel@tonic-gate 	 * In theory, the locale pointer should be set to either "C" or a
2327c478bd9Sstevel@tonic-gate 	 * char array, so it should never be NULL
2337c478bd9Sstevel@tonic-gate 	 */
2347c478bd9Sstevel@tonic-gate 	assert(zlogp->locale != NULL);
2357c478bd9Sstevel@tonic-gate 	/* Locale is per process, but we are multi-threaded... */
2367c478bd9Sstevel@tonic-gate 	fmt = localize_msg(zlogp->locale, fmt);
2377c478bd9Sstevel@tonic-gate 
2387c478bd9Sstevel@tonic-gate 	va_start(alist, fmt);
2397c478bd9Sstevel@tonic-gate 	(void) vsnprintf(bp, sizeof (buf) - (bp - buf), fmt, alist);
2407c478bd9Sstevel@tonic-gate 	va_end(alist);
2417c478bd9Sstevel@tonic-gate 	bp = &(buf[strlen(buf)]);
2427c478bd9Sstevel@tonic-gate 	if (use_strerror)
2437c478bd9Sstevel@tonic-gate 		(void) snprintf(bp, sizeof (buf) - (bp - buf), ": %s",
2447c478bd9Sstevel@tonic-gate 		    strerror(saved_errno));
2457c478bd9Sstevel@tonic-gate 	if (zlogp == &logsys) {
2467c478bd9Sstevel@tonic-gate 		(void) syslog(LOG_ERR, "%s", buf);
2477c478bd9Sstevel@tonic-gate 	} else if (zlogp->logfile != NULL) {
2487c478bd9Sstevel@tonic-gate 		(void) fprintf(zlogp->logfile, "%s\n", buf);
2497c478bd9Sstevel@tonic-gate 	} else {
2507c478bd9Sstevel@tonic-gate 		size_t buflen;
2517c478bd9Sstevel@tonic-gate 		size_t copylen;
2527c478bd9Sstevel@tonic-gate 
2537c478bd9Sstevel@tonic-gate 		buflen = snprintf(zlogp->log, zlogp->loglen, "%s\n", buf);
2547c478bd9Sstevel@tonic-gate 		copylen = MIN(buflen, zlogp->loglen);
2557c478bd9Sstevel@tonic-gate 		zlogp->log += copylen;
2567c478bd9Sstevel@tonic-gate 		zlogp->loglen -= copylen;
2577c478bd9Sstevel@tonic-gate 	}
2587c478bd9Sstevel@tonic-gate }
2597c478bd9Sstevel@tonic-gate 
2603f2f09c1Sdp /*
2613f2f09c1Sdp  * Emit a warning for any boot arguments which are unrecognized.  Since
262bbf21555SRichard Lowe  * Solaris boot arguments are getopt(3c) compatible (see kernel(8)), we
2633f2f09c1Sdp  * put the arguments into an argv style array, use getopt to process them,
2643f2f09c1Sdp  * and put the resultant argument string back into outargs.
2653f2f09c1Sdp  *
2663f2f09c1Sdp  * During the filtering, we pull out any arguments which are truly "boot"
2673f2f09c1Sdp  * arguments, leaving only those which are to be passed intact to the
2683f2f09c1Sdp  * progenitor process.  The one we support at the moment is -i, which
2693f2f09c1Sdp  * indicates to the kernel which program should be launched as 'init'.
2703f2f09c1Sdp  *
2713f2f09c1Sdp  * A return of Z_INVAL indicates specifically that the arguments are
2723f2f09c1Sdp  * not valid; this is a non-fatal error.  Except for Z_OK, all other return
2733f2f09c1Sdp  * values are treated as fatal.
2743f2f09c1Sdp  */
2753f2f09c1Sdp static int
filter_bootargs(zlog_t * zlogp,const char * inargs,char * outargs,char * init_file,char * badarg)2763f2f09c1Sdp filter_bootargs(zlog_t *zlogp, const char *inargs, char *outargs,
2773f2f09c1Sdp     char *init_file, char *badarg)
2783f2f09c1Sdp {
2793f2f09c1Sdp 	int argc = 0, argc_save;
280*ef150c2bSRichard Lowe 	int i, c;
2813f2f09c1Sdp 	int err;
2823f2f09c1Sdp 	char *arg, *lasts, **argv = NULL, **argv_save;
2833f2f09c1Sdp 	char zonecfg_args[BOOTARGS_MAX];
2843f2f09c1Sdp 	char scratchargs[BOOTARGS_MAX], *sargs;
285*ef150c2bSRichard Lowe 	char argsw[5];
2863f2f09c1Sdp 
2873f2f09c1Sdp 	bzero(outargs, BOOTARGS_MAX);
2883f2f09c1Sdp 	bzero(badarg, BOOTARGS_MAX);
2893f2f09c1Sdp 
2903f2f09c1Sdp 	/*
2913f2f09c1Sdp 	 * If the user didn't specify transient boot arguments, check
2923f2f09c1Sdp 	 * to see if there were any specified in the zone configuration,
2933f2f09c1Sdp 	 * and use them if applicable.
2943f2f09c1Sdp 	 */
2953f2f09c1Sdp 	if (inargs == NULL || inargs[0] == '\0')  {
2963f2f09c1Sdp 		zone_dochandle_t handle;
2973f2f09c1Sdp 		if ((handle = zonecfg_init_handle()) == NULL) {
2983f2f09c1Sdp 			zerror(zlogp, B_TRUE,
2993f2f09c1Sdp 			    "getting zone configuration handle");
3003f2f09c1Sdp 			return (Z_BAD_HANDLE);
3013f2f09c1Sdp 		}
3023f2f09c1Sdp 		err = zonecfg_get_snapshot_handle(zone_name, handle);
3033f2f09c1Sdp 		if (err != Z_OK) {
3043f2f09c1Sdp 			zerror(zlogp, B_FALSE,
3053f2f09c1Sdp 			    "invalid configuration snapshot");
3063f2f09c1Sdp 			zonecfg_fini_handle(handle);
3073f2f09c1Sdp 			return (Z_BAD_HANDLE);
3083f2f09c1Sdp 		}
3093f2f09c1Sdp 
3103f2f09c1Sdp 		bzero(zonecfg_args, sizeof (zonecfg_args));
3113f2f09c1Sdp 		(void) zonecfg_get_bootargs(handle, zonecfg_args,
3123f2f09c1Sdp 		    sizeof (zonecfg_args));
3133f2f09c1Sdp 		inargs = zonecfg_args;
3143f2f09c1Sdp 		zonecfg_fini_handle(handle);
3153f2f09c1Sdp 	}
3163f2f09c1Sdp 
3173f2f09c1Sdp 	if (strlen(inargs) >= BOOTARGS_MAX) {
3183f2f09c1Sdp 		zerror(zlogp, B_FALSE, "boot argument string too long");
3193f2f09c1Sdp 		return (Z_INVAL);
3203f2f09c1Sdp 	}
3213f2f09c1Sdp 
3223f2f09c1Sdp 	(void) strlcpy(scratchargs, inargs, sizeof (scratchargs));
3233f2f09c1Sdp 	sargs = scratchargs;
3243f2f09c1Sdp 	while ((arg = strtok_r(sargs, " \t", &lasts)) != NULL) {
3253f2f09c1Sdp 		sargs = NULL;
3263f2f09c1Sdp 		argc++;
3273f2f09c1Sdp 	}
3283f2f09c1Sdp 
3293f2f09c1Sdp 	if ((argv = calloc(argc + 1, sizeof (char *))) == NULL) {
3303f2f09c1Sdp 		zerror(zlogp, B_FALSE, "memory allocation failed");
3313f2f09c1Sdp 		return (Z_NOMEM);
3323f2f09c1Sdp 	}
3333f2f09c1Sdp 
3343f2f09c1Sdp 	argv_save = argv;
3353f2f09c1Sdp 	argc_save = argc;
3363f2f09c1Sdp 
3373f2f09c1Sdp 	(void) strlcpy(scratchargs, inargs, sizeof (scratchargs));
3383f2f09c1Sdp 	sargs = scratchargs;
3393f2f09c1Sdp 	i = 0;
3403f2f09c1Sdp 	while ((arg = strtok_r(sargs, " \t", &lasts)) != NULL) {
3413f2f09c1Sdp 		sargs = NULL;
3423f2f09c1Sdp 		if ((argv[i] = strdup(arg)) == NULL) {
3433f2f09c1Sdp 			err = Z_NOMEM;
3443f2f09c1Sdp 			zerror(zlogp, B_FALSE, "memory allocation failed");
3453f2f09c1Sdp 			goto done;
3463f2f09c1Sdp 		}
3473f2f09c1Sdp 		i++;
3483f2f09c1Sdp 	}
3493f2f09c1Sdp 
3503f2f09c1Sdp 	/*
3513f2f09c1Sdp 	 * We preserve compatibility with the Solaris system boot behavior,
3523f2f09c1Sdp 	 * which allows:
3533f2f09c1Sdp 	 *
3543f2f09c1Sdp 	 *	# reboot kernel/unix -s -m verbose
3553f2f09c1Sdp 	 *
3563f2f09c1Sdp 	 * In this example, kernel/unix tells the booter what file to
3573f2f09c1Sdp 	 * boot.  We don't want reboot in a zone to be gratuitously different,
3583f2f09c1Sdp 	 * so we silently ignore the boot file, if necessary.
3593f2f09c1Sdp 	 */
3603f2f09c1Sdp 	if (argv[0] == NULL)
3613f2f09c1Sdp 		goto done;
3623f2f09c1Sdp 
3633f2f09c1Sdp 	assert(argv[0][0] != ' ');
3643f2f09c1Sdp 	assert(argv[0][0] != '\t');
3653f2f09c1Sdp 
3663f2f09c1Sdp 	if (argv[0][0] != '-' && argv[0][0] != '\0') {
3673f2f09c1Sdp 		argv = &argv[1];
3683f2f09c1Sdp 		argc--;
3693f2f09c1Sdp 	}
3703f2f09c1Sdp 
3713f2f09c1Sdp 	optind = 0;
3723f2f09c1Sdp 	opterr = 0;
3733f2f09c1Sdp 	err = Z_OK;
3749acbbeafSnn35248 	while ((c = getopt(argc, argv, "fi:m:s")) != -1) {
3753f2f09c1Sdp 		switch (c) {
3763f2f09c1Sdp 		case 'i':
3773f2f09c1Sdp 			/*
3783f2f09c1Sdp 			 * -i is handled by the runtime and is not passed
3793f2f09c1Sdp 			 * along to userland
3803f2f09c1Sdp 			 */
3813f2f09c1Sdp 			(void) strlcpy(init_file, optarg, MAXPATHLEN);
3823f2f09c1Sdp 			break;
3839acbbeafSnn35248 		case 'f':
3849acbbeafSnn35248 			/* This has already been processed by zoneadm */
3859acbbeafSnn35248 			break;
3863f2f09c1Sdp 		case 'm':
3873f2f09c1Sdp 		case 's':
3883f2f09c1Sdp 			/* These pass through unmolested */
389de278259SToomas Soome 			(void) snprintf(argsw, sizeof (argsw), " -%c ", c);
390de278259SToomas Soome 			(void) strlcat(outargs, argsw, BOOTARGS_MAX);
391de278259SToomas Soome 			if (optarg)
392de278259SToomas Soome 				(void) strlcat(outargs, optarg, BOOTARGS_MAX);
3933f2f09c1Sdp 			break;
3943f2f09c1Sdp 		case '?':
3953f2f09c1Sdp 			/*
3963f2f09c1Sdp 			 * We warn about unknown arguments but pass them
3973f2f09c1Sdp 			 * along anyway-- if someone wants to develop their
3983f2f09c1Sdp 			 * own init replacement, they can pass it whatever
3993f2f09c1Sdp 			 * args they want.
4003f2f09c1Sdp 			 */
4013f2f09c1Sdp 			err = Z_INVAL;
402de278259SToomas Soome 			(void) snprintf(argsw, sizeof (argsw), " -%c", optopt);
403de278259SToomas Soome 			(void) strlcat(outargs, argsw, BOOTARGS_MAX);
404de278259SToomas Soome 			(void) strlcat(badarg, argsw, BOOTARGS_MAX);
4053f2f09c1Sdp 			break;
4063f2f09c1Sdp 		}
4073f2f09c1Sdp 	}
4083f2f09c1Sdp 
4093f2f09c1Sdp 	/*
4103f2f09c1Sdp 	 * For Solaris Zones we warn about and discard non-option arguments.
4113f2f09c1Sdp 	 * Hence 'boot foo bar baz gub' --> 'boot'.  However, to be similar
4123f2f09c1Sdp 	 * to the kernel, we concat up all the other remaining boot args.
4133f2f09c1Sdp 	 * and warn on them as a group.
4143f2f09c1Sdp 	 */
4153f2f09c1Sdp 	if (optind < argc) {
416de278259SToomas Soome 		const char *prefix = "";
417de278259SToomas Soome 
4183f2f09c1Sdp 		err = Z_INVAL;
419de278259SToomas Soome 		do {
420de278259SToomas Soome 			(void) strlcat(badarg, prefix, BOOTARGS_MAX);
421de278259SToomas Soome 			(void) strlcat(badarg, argv[optind], BOOTARGS_MAX);
422de278259SToomas Soome 			prefix = " ";
423de278259SToomas Soome 		} while (++optind < argc);
4243f2f09c1Sdp 		zerror(zlogp, B_FALSE, "WARNING: Unused or invalid boot "
4253f2f09c1Sdp 		    "arguments `%s'.", badarg);
4263f2f09c1Sdp 	}
4273f2f09c1Sdp 
4283f2f09c1Sdp done:
4293f2f09c1Sdp 	for (i = 0; i < argc_save; i++) {
4303f2f09c1Sdp 		if (argv_save[i] != NULL)
4313f2f09c1Sdp 			free(argv_save[i]);
4323f2f09c1Sdp 	}
4333f2f09c1Sdp 	free(argv_save);
4343f2f09c1Sdp 	return (err);
4353f2f09c1Sdp }
4363f2f09c1Sdp 
4373f2f09c1Sdp 
4387c478bd9Sstevel@tonic-gate static int
mkzonedir(zlog_t * zlogp)4397c478bd9Sstevel@tonic-gate mkzonedir(zlog_t *zlogp)
4407c478bd9Sstevel@tonic-gate {
4417c478bd9Sstevel@tonic-gate 	struct stat st;
4427c478bd9Sstevel@tonic-gate 	/*
4437c478bd9Sstevel@tonic-gate 	 * We must create and lock everyone but root out of ZONES_TMPDIR
4447c478bd9Sstevel@tonic-gate 	 * since anyone can open any UNIX domain socket, regardless of
4457c478bd9Sstevel@tonic-gate 	 * its file system permissions.  Sigh...
4467c478bd9Sstevel@tonic-gate 	 */
4477c478bd9Sstevel@tonic-gate 	if (mkdir(ZONES_TMPDIR, S_IRWXU) < 0 && errno != EEXIST) {
4487c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "could not mkdir '%s'", ZONES_TMPDIR);
4497c478bd9Sstevel@tonic-gate 		return (-1);
4507c478bd9Sstevel@tonic-gate 	}
4517c478bd9Sstevel@tonic-gate 	/* paranoia */
4524bc0a2efScasper 	if ((stat(ZONES_TMPDIR, &st) < 0) || !S_ISDIR(st.st_mode)) {
4537c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "'%s' is not a directory", ZONES_TMPDIR);
4547c478bd9Sstevel@tonic-gate 		return (-1);
4557c478bd9Sstevel@tonic-gate 	}
4567c478bd9Sstevel@tonic-gate 	(void) chmod(ZONES_TMPDIR, S_IRWXU);
4577c478bd9Sstevel@tonic-gate 	return (0);
4587c478bd9Sstevel@tonic-gate }
4597c478bd9Sstevel@tonic-gate 
460108322fbScarlsonj /*
461c5cd6260S  * Run the brand's pre-state change callback, if it exists.
462c5cd6260S  */
463c5cd6260S static int
brand_prestatechg(zlog_t * zlogp,int state,int cmd)464c5cd6260S brand_prestatechg(zlog_t *zlogp, int state, int cmd)
465c5cd6260S {
466c5cd6260S 	char cmdbuf[2 * MAXPATHLEN];
4672a8b76b2SSusan Kamm-Worrell 	const char *altroot;
468c5cd6260S 
469c5cd6260S 	if (pre_statechg_hook[0] == '\0')
470c5cd6260S 		return (0);
471c5cd6260S 
4722a8b76b2SSusan Kamm-Worrell 	altroot = zonecfg_get_root();
4732a8b76b2SSusan Kamm-Worrell 	if (snprintf(cmdbuf, sizeof (cmdbuf), "%s %d %d %s", pre_statechg_hook,
4742a8b76b2SSusan Kamm-Worrell 	    state, cmd, altroot) > sizeof (cmdbuf))
475c5cd6260S 		return (-1);
476c5cd6260S 
477c5cd6260S 	if (do_subproc(zlogp, cmdbuf, NULL) != 0)
478c5cd6260S 		return (-1);
479c5cd6260S 
480c5cd6260S 	return (0);
481c5cd6260S }
482c5cd6260S 
483c5cd6260S /*
484c5cd6260S  * Run the brand's post-state change callback, if it exists.
485c5cd6260S  */
486c5cd6260S static int
brand_poststatechg(zlog_t * zlogp,int state,int cmd)487c5cd6260S brand_poststatechg(zlog_t *zlogp, int state, int cmd)
488c5cd6260S {
489c5cd6260S 	char cmdbuf[2 * MAXPATHLEN];
4902a8b76b2SSusan Kamm-Worrell 	const char *altroot;
491c5cd6260S 
492c5cd6260S 	if (post_statechg_hook[0] == '\0')
493c5cd6260S 		return (0);
494c5cd6260S 
4952a8b76b2SSusan Kamm-Worrell 	altroot = zonecfg_get_root();
4962a8b76b2SSusan Kamm-Worrell 	if (snprintf(cmdbuf, sizeof (cmdbuf), "%s %d %d %s", post_statechg_hook,
4972a8b76b2SSusan Kamm-Worrell 	    state, cmd, altroot) > sizeof (cmdbuf))
498c5cd6260S 		return (-1);
499c5cd6260S 
500c5cd6260S 	if (do_subproc(zlogp, cmdbuf, NULL) != 0)
501c5cd6260S 		return (-1);
502c5cd6260S 
503c5cd6260S 	return (0);
504c5cd6260S }
505c5cd6260S 
506c5cd6260S /*
507efd4c9b6SSteve Lawrence  * Notify zonestatd of the new zone.  If zonestatd is not running, this
508efd4c9b6SSteve Lawrence  * will do nothing.
509efd4c9b6SSteve Lawrence  */
510efd4c9b6SSteve Lawrence static void
notify_zonestatd(zoneid_t zoneid)511efd4c9b6SSteve Lawrence notify_zonestatd(zoneid_t zoneid)
512efd4c9b6SSteve Lawrence {
513efd4c9b6SSteve Lawrence 	int cmd[2];
514efd4c9b6SSteve Lawrence 	int fd;
515efd4c9b6SSteve Lawrence 	door_arg_t params;
516efd4c9b6SSteve Lawrence 
517efd4c9b6SSteve Lawrence 	fd = open(ZS_DOOR_PATH, O_RDONLY);
518efd4c9b6SSteve Lawrence 	if (fd < 0)
519efd4c9b6SSteve Lawrence 		return;
520efd4c9b6SSteve Lawrence 
521efd4c9b6SSteve Lawrence 	cmd[0] = ZSD_CMD_NEW_ZONE;
522efd4c9b6SSteve Lawrence 	cmd[1] = zoneid;
523efd4c9b6SSteve Lawrence 	params.data_ptr = (char *)&cmd;
524efd4c9b6SSteve Lawrence 	params.data_size = sizeof (cmd);
525efd4c9b6SSteve Lawrence 	params.desc_ptr = NULL;
526efd4c9b6SSteve Lawrence 	params.desc_num = 0;
527efd4c9b6SSteve Lawrence 	params.rbuf = NULL;
52835c005f2SToomas Soome 	params.rsize = 0;
529efd4c9b6SSteve Lawrence 	(void) door_call(fd, &params);
530efd4c9b6SSteve Lawrence 	(void) close(fd);
531efd4c9b6SSteve Lawrence }
532efd4c9b6SSteve Lawrence 
533efd4c9b6SSteve Lawrence /*
534108322fbScarlsonj  * Bring a zone up to the pre-boot "ready" stage.  The mount_cmd argument is
535108322fbScarlsonj  * 'true' if this is being invoked as part of the processing for the "mount"
536108322fbScarlsonj  * subcommand.
537108322fbScarlsonj  */
538108322fbScarlsonj static int
zone_ready(zlog_t * zlogp,zone_mnt_t mount_cmd,int zstate)539c5cd6260S zone_ready(zlog_t *zlogp, zone_mnt_t mount_cmd, int zstate)
5407c478bd9Sstevel@tonic-gate {
5417c478bd9Sstevel@tonic-gate 	int err;
5427c478bd9Sstevel@tonic-gate 
543c5cd6260S 	if (brand_prestatechg(zlogp, zstate, Z_READY) != 0)
544c5cd6260S 		return (-1);
545c5cd6260S 
5467c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_create_snapshot(zone_name)) != Z_OK) {
5477c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "unable to create snapshot: %s",
5487c478bd9Sstevel@tonic-gate 		    zonecfg_strerror(err));
5498fe9fdf2SDan Price 		goto bad;
5507c478bd9Sstevel@tonic-gate 	}
5517c478bd9Sstevel@tonic-gate 
552108322fbScarlsonj 	if ((zone_id = vplat_create(zlogp, mount_cmd)) == -1) {
5537c478bd9Sstevel@tonic-gate 		if ((err = zonecfg_destroy_snapshot(zone_name)) != Z_OK)
5547c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_FALSE, "destroying snapshot: %s",
5557c478bd9Sstevel@tonic-gate 			    zonecfg_strerror(err));
5568fe9fdf2SDan Price 		goto bad;
5577c478bd9Sstevel@tonic-gate 	}
558555afedfScarlsonj 	if (vplat_bringup(zlogp, mount_cmd, zone_id) != 0) {
5597c478bd9Sstevel@tonic-gate 		bringup_failure_recovery = B_TRUE;
5606cfd72c6Sgjelinek 		(void) vplat_teardown(NULL, (mount_cmd != Z_MNT_BOOT), B_FALSE);
5617c478bd9Sstevel@tonic-gate 		if ((err = zonecfg_destroy_snapshot(zone_name)) != Z_OK)
5627c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_FALSE, "destroying snapshot: %s",
5637c478bd9Sstevel@tonic-gate 			    zonecfg_strerror(err));
5648fe9fdf2SDan Price 		goto bad;
5657c478bd9Sstevel@tonic-gate 	}
5667c478bd9Sstevel@tonic-gate 
567c5cd6260S 	if (brand_poststatechg(zlogp, zstate, Z_READY) != 0)
5688fe9fdf2SDan Price 		goto bad;
569c5cd6260S 
5707c478bd9Sstevel@tonic-gate 	return (0);
5718fe9fdf2SDan Price 
5728fe9fdf2SDan Price bad:
5738fe9fdf2SDan Price 	/*
5748fe9fdf2SDan Price 	 * If something goes wrong, we up the zones's state to the target
5758fe9fdf2SDan Price 	 * state, READY, and then invoke the hook as if we're halting.
5768fe9fdf2SDan Price 	 */
5778fe9fdf2SDan Price 	(void) brand_poststatechg(zlogp, ZONE_STATE_READY, Z_HALT);
5788fe9fdf2SDan Price 	return (-1);
5797c478bd9Sstevel@tonic-gate }
5807c478bd9Sstevel@tonic-gate 
581555afedfScarlsonj int
init_template(void)582555afedfScarlsonj init_template(void)
5837c478bd9Sstevel@tonic-gate {
5847c478bd9Sstevel@tonic-gate 	int fd;
5857c478bd9Sstevel@tonic-gate 	int err = 0;
5867c478bd9Sstevel@tonic-gate 
5877c478bd9Sstevel@tonic-gate 	fd = open64(CTFS_ROOT "/process/template", O_RDWR);
5887c478bd9Sstevel@tonic-gate 	if (fd == -1)
5897c478bd9Sstevel@tonic-gate 		return (-1);
5907c478bd9Sstevel@tonic-gate 
5917c478bd9Sstevel@tonic-gate 	/*
5927c478bd9Sstevel@tonic-gate 	 * For now, zoneadmd doesn't do anything with the contract.
5937c478bd9Sstevel@tonic-gate 	 * Deliver no events, don't inherit, and allow it to be orphaned.
5947c478bd9Sstevel@tonic-gate 	 */
5957c478bd9Sstevel@tonic-gate 	err |= ct_tmpl_set_critical(fd, 0);
5967c478bd9Sstevel@tonic-gate 	err |= ct_tmpl_set_informative(fd, 0);
5977c478bd9Sstevel@tonic-gate 	err |= ct_pr_tmpl_set_fatal(fd, CT_PR_EV_HWERR);
5987c478bd9Sstevel@tonic-gate 	err |= ct_pr_tmpl_set_param(fd, CT_PR_PGRPONLY | CT_PR_REGENT);
5997c478bd9Sstevel@tonic-gate 	if (err || ct_tmpl_activate(fd)) {
6007c478bd9Sstevel@tonic-gate 		(void) close(fd);
6017c478bd9Sstevel@tonic-gate 		return (-1);
6027c478bd9Sstevel@tonic-gate 	}
6037c478bd9Sstevel@tonic-gate 
6047c478bd9Sstevel@tonic-gate 	return (fd);
6057c478bd9Sstevel@tonic-gate }
6067c478bd9Sstevel@tonic-gate 
6079acbbeafSnn35248 typedef struct fs_callback {
6089acbbeafSnn35248 	zlog_t		*zlogp;
6099acbbeafSnn35248 	zoneid_t	zoneid;
610910f48daSedp 	boolean_t	mount_cmd;
6119acbbeafSnn35248 } fs_callback_t;
6129acbbeafSnn35248 
6137c478bd9Sstevel@tonic-gate static int
mount_early_fs(void * data,const char * spec,const char * dir,const char * fstype,const char * opt)6149acbbeafSnn35248 mount_early_fs(void *data, const char *spec, const char *dir,
6159acbbeafSnn35248     const char *fstype, const char *opt)
6167c478bd9Sstevel@tonic-gate {
6179acbbeafSnn35248 	zlog_t *zlogp = ((fs_callback_t *)data)->zlogp;
6189acbbeafSnn35248 	zoneid_t zoneid = ((fs_callback_t *)data)->zoneid;
619910f48daSedp 	boolean_t mount_cmd = ((fs_callback_t *)data)->mount_cmd;
620d314f035Sedp 	char rootpath[MAXPATHLEN];
6217c478bd9Sstevel@tonic-gate 	pid_t child;
6227c478bd9Sstevel@tonic-gate 	int child_status;
6237c478bd9Sstevel@tonic-gate 	int tmpl_fd;
624d314f035Sedp 	int rv;
6257c478bd9Sstevel@tonic-gate 	ctid_t ct;
6267c478bd9Sstevel@tonic-gate 
627910f48daSedp 	/* determine the zone rootpath */
628910f48daSedp 	if (mount_cmd) {
629910f48daSedp 		char zonepath[MAXPATHLEN];
630910f48daSedp 		char luroot[MAXPATHLEN];
631910f48daSedp 
632910f48daSedp 		if (zone_get_zonepath(zone_name,
633910f48daSedp 		    zonepath, sizeof (zonepath)) != Z_OK) {
634910f48daSedp 			zerror(zlogp, B_FALSE, "unable to determine zone path");
635910f48daSedp 			return (-1);
636910f48daSedp 		}
637910f48daSedp 
638910f48daSedp 		(void) snprintf(luroot, sizeof (luroot), "%s/lu", zonepath);
639910f48daSedp 		resolve_lofs(zlogp, luroot, sizeof (luroot));
640910f48daSedp 		(void) strlcpy(rootpath, luroot, sizeof (rootpath));
641910f48daSedp 	} else {
642910f48daSedp 		if (zone_get_rootpath(zone_name,
643910f48daSedp 		    rootpath, sizeof (rootpath)) != Z_OK) {
644d314f035Sedp 			zerror(zlogp, B_FALSE, "unable to determine zone root");
645d314f035Sedp 			return (-1);
646d314f035Sedp 		}
647910f48daSedp 	}
648d314f035Sedp 
649d314f035Sedp 	if ((rv = valid_mount_path(zlogp, rootpath, spec, dir, fstype)) < 0) {
650d314f035Sedp 		zerror(zlogp, B_FALSE, "%s%s is not a valid mount point",
651d314f035Sedp 		    rootpath, dir);
652d314f035Sedp 		return (-1);
653d314f035Sedp 	} else if (rv > 0) {
654d314f035Sedp 		/* The mount point path doesn't exist, create it now. */
655d314f035Sedp 		if (make_one_dir(zlogp, rootpath, dir,
656d314f035Sedp 		    DEFAULT_DIR_MODE, DEFAULT_DIR_USER,
657d314f035Sedp 		    DEFAULT_DIR_GROUP) != 0) {
658d314f035Sedp 			zerror(zlogp, B_FALSE, "failed to create mount point");
659d314f035Sedp 			return (-1);
660d314f035Sedp 		}
661d314f035Sedp 
662d314f035Sedp 		/*
663d314f035Sedp 		 * Now this might seem weird, but we need to invoke
664d314f035Sedp 		 * valid_mount_path() again.  Why?  Because it checks
665d314f035Sedp 		 * to make sure that the mount point path is canonical,
666d314f035Sedp 		 * which it can only do if the path exists, so now that
667d314f035Sedp 		 * we've created the path we have to verify it again.
668d314f035Sedp 		 */
669d314f035Sedp 		if ((rv = valid_mount_path(zlogp, rootpath, spec, dir,
670d314f035Sedp 		    fstype)) < 0) {
671d314f035Sedp 			zerror(zlogp, B_FALSE,
672d314f035Sedp 			    "%s%s is not a valid mount point", rootpath, dir);
673d314f035Sedp 			return (-1);
674d314f035Sedp 		}
675d314f035Sedp 	}
676d314f035Sedp 
6777c478bd9Sstevel@tonic-gate 	if ((tmpl_fd = init_template()) == -1) {
6787c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "failed to create contract");
6797c478bd9Sstevel@tonic-gate 		return (-1);
6807c478bd9Sstevel@tonic-gate 	}
6817c478bd9Sstevel@tonic-gate 
6827c478bd9Sstevel@tonic-gate 	if ((child = fork()) == -1) {
6837c478bd9Sstevel@tonic-gate 		(void) ct_tmpl_clear(tmpl_fd);
6847c478bd9Sstevel@tonic-gate 		(void) close(tmpl_fd);
6857c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "failed to fork");
6867c478bd9Sstevel@tonic-gate 		return (-1);
6877c478bd9Sstevel@tonic-gate 
6887c478bd9Sstevel@tonic-gate 	} else if (child == 0) {	/* child */
6899acbbeafSnn35248 		char opt_buf[MAX_MNTOPT_STR];
6909acbbeafSnn35248 		int optlen = 0;
6919acbbeafSnn35248 		int mflag = MS_DATA;
6929acbbeafSnn35248 
6937c478bd9Sstevel@tonic-gate 		(void) ct_tmpl_clear(tmpl_fd);
6947c478bd9Sstevel@tonic-gate 		/*
6957c478bd9Sstevel@tonic-gate 		 * Even though there are no procs running in the zone, we
6967c478bd9Sstevel@tonic-gate 		 * do this for paranoia's sake.
6977c478bd9Sstevel@tonic-gate 		 */
6987c478bd9Sstevel@tonic-gate 		(void) closefrom(0);
6997c478bd9Sstevel@tonic-gate 
7007c478bd9Sstevel@tonic-gate 		if (zone_enter(zoneid) == -1) {
7017c478bd9Sstevel@tonic-gate 			_exit(errno);
7027c478bd9Sstevel@tonic-gate 		}
7039acbbeafSnn35248 		if (opt != NULL) {
7049acbbeafSnn35248 			/*
7059acbbeafSnn35248 			 * The mount() system call is incredibly annoying.
7069acbbeafSnn35248 			 * If options are specified, we need to copy them
7079acbbeafSnn35248 			 * into a temporary buffer since the mount() system
7089acbbeafSnn35248 			 * call will overwrite the options string.  It will
7099acbbeafSnn35248 			 * also fail if the new option string it wants to
7109acbbeafSnn35248 			 * write is bigger than the one we passed in, so
7119acbbeafSnn35248 			 * you must pass in a buffer of the maximum possible
7129acbbeafSnn35248 			 * option string length.  sigh.
7139acbbeafSnn35248 			 */
7149acbbeafSnn35248 			(void) strlcpy(opt_buf, opt, sizeof (opt_buf));
7159acbbeafSnn35248 			opt = opt_buf;
7169acbbeafSnn35248 			optlen = MAX_MNTOPT_STR;
7179acbbeafSnn35248 			mflag = MS_OPTIONSTR;
7189acbbeafSnn35248 		}
7199acbbeafSnn35248 		if (mount(spec, dir, mflag, fstype, NULL, 0, opt, optlen) != 0)
7207c478bd9Sstevel@tonic-gate 			_exit(errno);
7217c478bd9Sstevel@tonic-gate 		_exit(0);
7227c478bd9Sstevel@tonic-gate 	}
7237c478bd9Sstevel@tonic-gate 
7247c478bd9Sstevel@tonic-gate 	/* parent */
7257c478bd9Sstevel@tonic-gate 	if (contract_latest(&ct) == -1)
7267c478bd9Sstevel@tonic-gate 		ct = -1;
7277c478bd9Sstevel@tonic-gate 	(void) ct_tmpl_clear(tmpl_fd);
7287c478bd9Sstevel@tonic-gate 	(void) close(tmpl_fd);
7297c478bd9Sstevel@tonic-gate 	if (waitpid(child, &child_status, 0) != child) {
7307c478bd9Sstevel@tonic-gate 		/* unexpected: we must have been signalled */
7317c478bd9Sstevel@tonic-gate 		(void) contract_abandon_id(ct);
7327c478bd9Sstevel@tonic-gate 		return (-1);
7337c478bd9Sstevel@tonic-gate 	}
7347c478bd9Sstevel@tonic-gate 	(void) contract_abandon_id(ct);
7357c478bd9Sstevel@tonic-gate 	if (WEXITSTATUS(child_status) != 0) {
7367c478bd9Sstevel@tonic-gate 		errno = WEXITSTATUS(child_status);
7377c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "mount of %s failed", dir);
7387c478bd9Sstevel@tonic-gate 		return (-1);
7397c478bd9Sstevel@tonic-gate 	}
7407c478bd9Sstevel@tonic-gate 
7417c478bd9Sstevel@tonic-gate 	return (0);
7427c478bd9Sstevel@tonic-gate }
7437c478bd9Sstevel@tonic-gate 
744c5cd6260S /*
745c5cd6260S  * If retstr is not NULL, the output of the subproc is returned in the str,
746c5cd6260S  * otherwise it is output using zerror().  Any memory allocated for retstr
747c5cd6260S  * should be freed by the caller.
748c5cd6260S  */
7499acbbeafSnn35248 int
do_subproc(zlog_t * zlogp,char * cmdbuf,char ** retstr)750c5cd6260S do_subproc(zlog_t *zlogp, char *cmdbuf, char **retstr)
751108322fbScarlsonj {
752c5cd6260S 	char buf[1024];		/* arbitrary large amount */
753c5cd6260S 	char *inbuf;
7549acbbeafSnn35248 	FILE *file;
7559acbbeafSnn35248 	int status;
756c5cd6260S 	int rd_cnt;
757c5cd6260S 
758c5cd6260S 	if (retstr != NULL) {
759c5cd6260S 		if ((*retstr = malloc(1024)) == NULL) {
760c5cd6260S 			zerror(zlogp, B_FALSE, "out of memory");
761c5cd6260S 			return (-1);
762c5cd6260S 		}
763c5cd6260S 		inbuf = *retstr;
764c5cd6260S 		rd_cnt = 0;
765c5cd6260S 	} else {
766c5cd6260S 		inbuf = buf;
767c5cd6260S 	}
768108322fbScarlsonj 
7699acbbeafSnn35248 	file = popen(cmdbuf, "r");
7709acbbeafSnn35248 	if (file == NULL) {
7719acbbeafSnn35248 		zerror(zlogp, B_TRUE, "could not launch: %s", cmdbuf);
772108322fbScarlsonj 		return (-1);
7739acbbeafSnn35248 	}
774108322fbScarlsonj 
775c5cd6260S 	while (fgets(inbuf, 1024, file) != NULL) {
7761f314719S 		if (retstr == NULL) {
77785dff7a0SAndy Fiddaman 			if (zlogp != &logsys) {
77885dff7a0SAndy Fiddaman 				int last = strlen(inbuf) - 1;
77985dff7a0SAndy Fiddaman 
78085dff7a0SAndy Fiddaman 				if (inbuf[last] == '\n')
78185dff7a0SAndy Fiddaman 					inbuf[last] = '\0';
7829acbbeafSnn35248 				zerror(zlogp, B_FALSE, "%s", inbuf);
78385dff7a0SAndy Fiddaman 			}
784c5cd6260S 		} else {
785c5cd6260S 			char *p;
786c5cd6260S 
787c5cd6260S 			rd_cnt += 1024 - 1;
788c5cd6260S 			if ((p = realloc(*retstr, rd_cnt + 1024)) == NULL) {
789c5cd6260S 				zerror(zlogp, B_FALSE, "out of memory");
790c5cd6260S 				(void) pclose(file);
791c5cd6260S 				return (-1);
792c5cd6260S 			}
793c5cd6260S 
794c5cd6260S 			*retstr = p;
795c5cd6260S 			inbuf = *retstr + rd_cnt;
796c5cd6260S 		}
797c5cd6260S 	}
7989acbbeafSnn35248 	status = pclose(file);
7999acbbeafSnn35248 
8009acbbeafSnn35248 	if (WIFSIGNALED(status)) {
8019acbbeafSnn35248 		zerror(zlogp, B_FALSE, "%s unexpectedly terminated due to "
8029acbbeafSnn35248 		    "signal %d", cmdbuf, WTERMSIG(status));
8035518d15bSdp 		return (-1);
8049acbbeafSnn35248 	}
8059acbbeafSnn35248 	assert(WIFEXITED(status));
8069acbbeafSnn35248 	if (WEXITSTATUS(status) == ZEXIT_EXEC) {
8079acbbeafSnn35248 		zerror(zlogp, B_FALSE, "failed to exec %s", cmdbuf);
808108322fbScarlsonj 		return (-1);
8099acbbeafSnn35248 	}
8109acbbeafSnn35248 	return (WEXITSTATUS(status));
811108322fbScarlsonj }
812108322fbScarlsonj 
813108322fbScarlsonj static int
zone_bootup(zlog_t * zlogp,const char * bootargs,int zstate)814c5cd6260S zone_bootup(zlog_t *zlogp, const char *bootargs, int zstate)
8157c478bd9Sstevel@tonic-gate {
8167c478bd9Sstevel@tonic-gate 	zoneid_t zoneid;
8177c478bd9Sstevel@tonic-gate 	struct stat st;
818ff17c8bfSgjelinek 	char zpath[MAXPATHLEN], initpath[MAXPATHLEN], init_file[MAXPATHLEN];
8193f2f09c1Sdp 	char nbootargs[BOOTARGS_MAX];
8209acbbeafSnn35248 	char cmdbuf[MAXPATHLEN];
8219acbbeafSnn35248 	fs_callback_t cb;
822123807fbSedp 	brand_handle_t bh;
8232b24ab6bSSebastien Roy 	zone_iptype_t iptype;
8242b24ab6bSSebastien Roy 	boolean_t links_loaded = B_FALSE;
8252b24ab6bSSebastien Roy 	dladm_status_t status;
8262b24ab6bSSebastien Roy 	char errmsg[DLADM_STRSIZE];
8273f2f09c1Sdp 	int err;
82855fcd84fSJerry Jelinek 	boolean_t restart_init, restart_init0, restart_initreboot;
8297c478bd9Sstevel@tonic-gate 
830c5cd6260S 	if (brand_prestatechg(zlogp, zstate, Z_BOOT) != 0)
831c5cd6260S 		return (-1);
832c5cd6260S 
8337c478bd9Sstevel@tonic-gate 	if ((zoneid = getzoneidbyname(zone_name)) == -1) {
8347c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "unable to get zoneid");
8358fe9fdf2SDan Price 		goto bad;
8367c478bd9Sstevel@tonic-gate 	}
8377c478bd9Sstevel@tonic-gate 
8389acbbeafSnn35248 	cb.zlogp = zlogp;
8399acbbeafSnn35248 	cb.zoneid = zoneid;
840910f48daSedp 	cb.mount_cmd = B_FALSE;
8419acbbeafSnn35248 
8429acbbeafSnn35248 	/* Get a handle to the brand info for this zone */
843123807fbSedp 	if ((bh = brand_open(brand_name)) == NULL) {
8449acbbeafSnn35248 		zerror(zlogp, B_FALSE, "unable to determine zone brand");
8458fe9fdf2SDan Price 		goto bad;
8469acbbeafSnn35248 	}
8479acbbeafSnn35248 
8489acbbeafSnn35248 	/*
8499acbbeafSnn35248 	 * Get the list of filesystems to mount from the brand
8509acbbeafSnn35248 	 * configuration.  These mounts are done via a thread that will
8519acbbeafSnn35248 	 * enter the zone, so they are done from within the context of the
8529acbbeafSnn35248 	 * zone.
8539acbbeafSnn35248 	 */
854123807fbSedp 	if (brand_platform_iter_mounts(bh, mount_early_fs, &cb) != 0) {
8559acbbeafSnn35248 		zerror(zlogp, B_FALSE, "unable to mount filesystems");
856123807fbSedp 		brand_close(bh);
8578fe9fdf2SDan Price 		goto bad;
8589acbbeafSnn35248 	}
8599acbbeafSnn35248 
8609acbbeafSnn35248 	/*
8619acbbeafSnn35248 	 * Get the brand's boot callback if it exists.
8629acbbeafSnn35248 	 */
863ff17c8bfSgjelinek 	if (zone_get_zonepath(zone_name, zpath, sizeof (zpath)) != Z_OK) {
864ff17c8bfSgjelinek 		zerror(zlogp, B_FALSE, "unable to determine zone path");
865e767a340Sgjelinek 		brand_close(bh);
8668fe9fdf2SDan Price 		goto bad;
8679acbbeafSnn35248 	}
8689acbbeafSnn35248 	(void) strcpy(cmdbuf, EXEC_PREFIX);
869ff17c8bfSgjelinek 	if (brand_get_boot(bh, zone_name, zpath, cmdbuf + EXEC_LEN,
870ff17c8bfSgjelinek 	    sizeof (cmdbuf) - EXEC_LEN) != 0) {
8719acbbeafSnn35248 		zerror(zlogp, B_FALSE,
8729acbbeafSnn35248 		    "unable to determine branded zone's boot callback");
873123807fbSedp 		brand_close(bh);
8748fe9fdf2SDan Price 		goto bad;
8759acbbeafSnn35248 	}
8769acbbeafSnn35248 
877bbf21555SRichard Lowe 	/* Get the path for this zone's init(8) (or equivalent) process.  */
878123807fbSedp 	if (brand_get_initname(bh, init_file, MAXPATHLEN) != 0) {
8799acbbeafSnn35248 		zerror(zlogp, B_FALSE,
880bbf21555SRichard Lowe 		    "unable to determine zone's init(8) location");
881123807fbSedp 		brand_close(bh);
8828fe9fdf2SDan Price 		goto bad;
8839acbbeafSnn35248 	}
8849acbbeafSnn35248 
885bafd1f14SJerry Jelinek 	/* See if this zone's brand should restart init if it dies. */
886bafd1f14SJerry Jelinek 	restart_init = brand_restartinit(bh);
88755fcd84fSJerry Jelinek 	restart_init0 = brand_restartinit0(bh);
88855fcd84fSJerry Jelinek 	restart_initreboot = brand_restartinitreboot(bh);
889bafd1f14SJerry Jelinek 
890123807fbSedp 	brand_close(bh);
8917c478bd9Sstevel@tonic-gate 
8923f2f09c1Sdp 	err = filter_bootargs(zlogp, bootargs, nbootargs, init_file,
8933f2f09c1Sdp 	    bad_boot_arg);
8943f2f09c1Sdp 	if (err == Z_INVAL)
8953f2f09c1Sdp 		eventstream_write(Z_EVT_ZONE_BADARGS);
8963f2f09c1Sdp 	else if (err != Z_OK)
8978fe9fdf2SDan Price 		goto bad;
8983f2f09c1Sdp 
8993f2f09c1Sdp 	assert(init_file[0] != '\0');
9003f2f09c1Sdp 
9019acbbeafSnn35248 	/* Try to anticipate possible problems: Make sure init is executable. */
902ff17c8bfSgjelinek 	if (zone_get_rootpath(zone_name, zpath, sizeof (zpath)) != Z_OK) {
9037c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "unable to determine zone root");
9048fe9fdf2SDan Price 		goto bad;
9057c478bd9Sstevel@tonic-gate 	}
9069acbbeafSnn35248 
907ff17c8bfSgjelinek 	(void) snprintf(initpath, sizeof (initpath), "%s%s", zpath, init_file);
9087c478bd9Sstevel@tonic-gate 
9097c478bd9Sstevel@tonic-gate 	if (stat(initpath, &st) == -1) {
9107c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "could not stat %s", initpath);
9118fe9fdf2SDan Price 		goto bad;
9127c478bd9Sstevel@tonic-gate 	}
9137c478bd9Sstevel@tonic-gate 
9147c478bd9Sstevel@tonic-gate 	if ((st.st_mode & S_IXUSR) == 0) {
9157c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "%s is not executable", initpath);
9168fe9fdf2SDan Price 		goto bad;
9177c478bd9Sstevel@tonic-gate 	}
9187c478bd9Sstevel@tonic-gate 
9199acbbeafSnn35248 	/*
9202b24ab6bSSebastien Roy 	 * Exclusive stack zones interact with the dlmgmtd running in the
9212b24ab6bSSebastien Roy 	 * global zone.  dladm_zone_boot() tells dlmgmtd that this zone is
9222b24ab6bSSebastien Roy 	 * booting, and loads its datalinks from the zone's datalink
9232b24ab6bSSebastien Roy 	 * configuration file.
9242b24ab6bSSebastien Roy 	 */
9252b24ab6bSSebastien Roy 	if (vplat_get_iptype(zlogp, &iptype) == 0 && iptype == ZS_EXCLUSIVE) {
9262b24ab6bSSebastien Roy 		status = dladm_zone_boot(dld_handle, zoneid);
9272b24ab6bSSebastien Roy 		if (status != DLADM_STATUS_OK) {
9282b24ab6bSSebastien Roy 			zerror(zlogp, B_FALSE, "unable to load zone datalinks: "
9292b24ab6bSSebastien Roy 			    " %s", dladm_status2str(status, errmsg));
9302b24ab6bSSebastien Roy 			goto bad;
9312b24ab6bSSebastien Roy 		}
9322b24ab6bSSebastien Roy 		links_loaded = B_TRUE;
9332b24ab6bSSebastien Roy 	}
9342b24ab6bSSebastien Roy 
9352b24ab6bSSebastien Roy 	/*
9369acbbeafSnn35248 	 * If there is a brand 'boot' callback, execute it now to give the
9379acbbeafSnn35248 	 * brand one last chance to do any additional setup before the zone
9389acbbeafSnn35248 	 * is booted.
9399acbbeafSnn35248 	 */
9409acbbeafSnn35248 	if ((strlen(cmdbuf) > EXEC_LEN) &&
941c5cd6260S 	    (do_subproc(zlogp, cmdbuf, NULL) != Z_OK)) {
9429acbbeafSnn35248 		zerror(zlogp, B_FALSE, "%s failed", cmdbuf);
9438fe9fdf2SDan Price 		goto bad;
9449acbbeafSnn35248 	}
9459acbbeafSnn35248 
9463f2f09c1Sdp 	if (zone_setattr(zoneid, ZONE_ATTR_INITNAME, init_file, 0) == -1) {
9473f2f09c1Sdp 		zerror(zlogp, B_TRUE, "could not set zone boot file");
9488fe9fdf2SDan Price 		goto bad;
9493f2f09c1Sdp 	}
9503f2f09c1Sdp 
9513f2f09c1Sdp 	if (zone_setattr(zoneid, ZONE_ATTR_BOOTARGS, nbootargs, 0) == -1) {
9523f2f09c1Sdp 		zerror(zlogp, B_TRUE, "could not set zone boot arguments");
9538fe9fdf2SDan Price 		goto bad;
9543f2f09c1Sdp 	}
9553f2f09c1Sdp 
956bafd1f14SJerry Jelinek 	if (!restart_init && zone_setattr(zoneid, ZONE_ATTR_INITNORESTART,
957bafd1f14SJerry Jelinek 	    NULL, 0) == -1) {
958bafd1f14SJerry Jelinek 		zerror(zlogp, B_TRUE, "could not set zone init-no-restart");
959bafd1f14SJerry Jelinek 		goto bad;
960bafd1f14SJerry Jelinek 	}
96155fcd84fSJerry Jelinek 	if (restart_init0 && zone_setattr(zoneid, ZONE_ATTR_INITRESTART0,
96255fcd84fSJerry Jelinek 	    NULL, 0) == -1) {
96355fcd84fSJerry Jelinek 		zerror(zlogp, B_TRUE,
96455fcd84fSJerry Jelinek 		    "could not set zone init-restart-on-exit-0");
96555fcd84fSJerry Jelinek 		goto bad;
96655fcd84fSJerry Jelinek 	}
96755fcd84fSJerry Jelinek 	if (restart_initreboot && zone_setattr(zoneid, ZONE_ATTR_INITREBOOT,
96855fcd84fSJerry Jelinek 	    NULL, 0) == -1) {
96955fcd84fSJerry Jelinek 		zerror(zlogp, B_TRUE, "could not set zone reboot-on-init-exit");
97055fcd84fSJerry Jelinek 		goto bad;
97155fcd84fSJerry Jelinek 	}
972bafd1f14SJerry Jelinek 
973efd4c9b6SSteve Lawrence 	/*
974efd4c9b6SSteve Lawrence 	 * Inform zonestatd of a new zone so that it can install a door for
975efd4c9b6SSteve Lawrence 	 * the zone to contact it.
976efd4c9b6SSteve Lawrence 	 */
977efd4c9b6SSteve Lawrence 	notify_zonestatd(zone_id);
978efd4c9b6SSteve Lawrence 
9793f2f09c1Sdp 	if (zone_boot(zoneid) == -1) {
9807c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "unable to boot zone");
9818fe9fdf2SDan Price 		goto bad;
9827c478bd9Sstevel@tonic-gate 	}
9837c478bd9Sstevel@tonic-gate 
984c5cd6260S 	if (brand_poststatechg(zlogp, zstate, Z_BOOT) != 0)
9858fe9fdf2SDan Price 		goto bad;
986c5cd6260S 
9877c478bd9Sstevel@tonic-gate 	return (0);
9888fe9fdf2SDan Price 
9898fe9fdf2SDan Price bad:
9908fe9fdf2SDan Price 	/*
9918fe9fdf2SDan Price 	 * If something goes wrong, we up the zones's state to the target
9928fe9fdf2SDan Price 	 * state, RUNNING, and then invoke the hook as if we're halting.
9938fe9fdf2SDan Price 	 */
9948fe9fdf2SDan Price 	(void) brand_poststatechg(zlogp, ZONE_STATE_RUNNING, Z_HALT);
9952b24ab6bSSebastien Roy 	if (links_loaded)
9962b24ab6bSSebastien Roy 		(void) dladm_zone_halt(dld_handle, zoneid);
9978fe9fdf2SDan Price 	return (-1);
9987c478bd9Sstevel@tonic-gate }
9997c478bd9Sstevel@tonic-gate 
10007c478bd9Sstevel@tonic-gate static int
zone_halt(zlog_t * zlogp,boolean_t unmount_cmd,boolean_t rebooting,int zstate)1001c5cd6260S zone_halt(zlog_t *zlogp, boolean_t unmount_cmd, boolean_t rebooting, int zstate)
10027c478bd9Sstevel@tonic-gate {
10037c478bd9Sstevel@tonic-gate 	int err;
10047c478bd9Sstevel@tonic-gate 
1005c5cd6260S 	if (brand_prestatechg(zlogp, zstate, Z_HALT) != 0)
1006c5cd6260S 		return (-1);
1007c5cd6260S 
10080209230bSgjelinek 	if (vplat_teardown(zlogp, unmount_cmd, rebooting) != 0) {
10097c478bd9Sstevel@tonic-gate 		if (!bringup_failure_recovery)
10107c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_FALSE, "unable to destroy zone");
10117c478bd9Sstevel@tonic-gate 		return (-1);
10127c478bd9Sstevel@tonic-gate 	}
10137c478bd9Sstevel@tonic-gate 
10147c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_destroy_snapshot(zone_name)) != Z_OK)
10157c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "destroying snapshot: %s",
10167c478bd9Sstevel@tonic-gate 		    zonecfg_strerror(err));
10177c478bd9Sstevel@tonic-gate 
1018c5cd6260S 	if (brand_poststatechg(zlogp, zstate, Z_HALT) != 0)
1019c5cd6260S 		return (-1);
1020c5cd6260S 
10217c478bd9Sstevel@tonic-gate 	return (0);
10227c478bd9Sstevel@tonic-gate }
10237c478bd9Sstevel@tonic-gate 
10243c7284bdSAlexander Eremin static int
zone_graceful_shutdown(zlog_t * zlogp)10253c7284bdSAlexander Eremin zone_graceful_shutdown(zlog_t *zlogp)
10263c7284bdSAlexander Eremin {
10273c7284bdSAlexander Eremin 	zoneid_t zoneid;
10283c7284bdSAlexander Eremin 	pid_t child;
10293c7284bdSAlexander Eremin 	char cmdbuf[MAXPATHLEN];
10303c7284bdSAlexander Eremin 	brand_handle_t bh = NULL;
10313c7284bdSAlexander Eremin 	char zpath[MAXPATHLEN];
10323c7284bdSAlexander Eremin 	ctid_t ct;
10333c7284bdSAlexander Eremin 	int tmpl_fd;
10343c7284bdSAlexander Eremin 	int child_status;
10353c7284bdSAlexander Eremin 
10363c7284bdSAlexander Eremin 	if (shutdown_in_progress) {
10373c7284bdSAlexander Eremin 		zerror(zlogp, B_FALSE, "shutdown already in progress");
10383c7284bdSAlexander Eremin 		return (-1);
10393c7284bdSAlexander Eremin 	}
10403c7284bdSAlexander Eremin 
10413c7284bdSAlexander Eremin 	if ((zoneid = getzoneidbyname(zone_name)) == -1) {
10423c7284bdSAlexander Eremin 		zerror(zlogp, B_TRUE, "unable to get zoneid");
10433c7284bdSAlexander Eremin 		return (-1);
10443c7284bdSAlexander Eremin 	}
10453c7284bdSAlexander Eremin 
10463c7284bdSAlexander Eremin 	/* Get a handle to the brand info for this zone */
10473c7284bdSAlexander Eremin 	if ((bh = brand_open(brand_name)) == NULL) {
10483c7284bdSAlexander Eremin 		zerror(zlogp, B_FALSE, "unable to determine zone brand");
10493c7284bdSAlexander Eremin 		return (-1);
10503c7284bdSAlexander Eremin 	}
10513c7284bdSAlexander Eremin 
10523c7284bdSAlexander Eremin 	if (zone_get_zonepath(zone_name, zpath, sizeof (zpath)) != Z_OK) {
10533c7284bdSAlexander Eremin 		zerror(zlogp, B_FALSE, "unable to determine zone path");
10543c7284bdSAlexander Eremin 		brand_close(bh);
10553c7284bdSAlexander Eremin 		return (-1);
10563c7284bdSAlexander Eremin 	}
10573c7284bdSAlexander Eremin 
10583c7284bdSAlexander Eremin 	/*
10593c7284bdSAlexander Eremin 	 * If there is a brand 'shutdown' callback, execute it now to give the
10603c7284bdSAlexander Eremin 	 * brand a chance to cleanup any custom configuration.
10613c7284bdSAlexander Eremin 	 */
10623c7284bdSAlexander Eremin 	(void) strcpy(cmdbuf, EXEC_PREFIX);
10633c7284bdSAlexander Eremin 	if (brand_get_shutdown(bh, zone_name, zpath, cmdbuf + EXEC_LEN,
10643c7284bdSAlexander Eremin 	    sizeof (cmdbuf) - EXEC_LEN) != 0 || strlen(cmdbuf) <= EXEC_LEN) {
10653c7284bdSAlexander Eremin 		(void) strcat(cmdbuf, SHUTDOWN_DEFAULT);
10663c7284bdSAlexander Eremin 	}
10673c7284bdSAlexander Eremin 	brand_close(bh);
10683c7284bdSAlexander Eremin 
10693c7284bdSAlexander Eremin 	if ((tmpl_fd = init_template()) == -1) {
10703c7284bdSAlexander Eremin 		zerror(zlogp, B_TRUE, "failed to create contract");
10713c7284bdSAlexander Eremin 		return (-1);
10723c7284bdSAlexander Eremin 	}
10733c7284bdSAlexander Eremin 
10743c7284bdSAlexander Eremin 	if ((child = fork()) == -1) {
10753c7284bdSAlexander Eremin 		(void) ct_tmpl_clear(tmpl_fd);
10763c7284bdSAlexander Eremin 		(void) close(tmpl_fd);
10773c7284bdSAlexander Eremin 		zerror(zlogp, B_TRUE, "failed to fork");
10783c7284bdSAlexander Eremin 		return (-1);
10793c7284bdSAlexander Eremin 	} else if (child == 0) {
10803c7284bdSAlexander Eremin 		(void) ct_tmpl_clear(tmpl_fd);
10813c7284bdSAlexander Eremin 		if (zone_enter(zoneid) == -1) {
10823c7284bdSAlexander Eremin 			_exit(errno);
10833c7284bdSAlexander Eremin 		}
10843c7284bdSAlexander Eremin 		_exit(execl("/bin/sh", "sh", "-c", cmdbuf, (char *)NULL));
10853c7284bdSAlexander Eremin 	}
10863c7284bdSAlexander Eremin 
10873c7284bdSAlexander Eremin 	if (contract_latest(&ct) == -1)
10883c7284bdSAlexander Eremin 		ct = -1;
10893c7284bdSAlexander Eremin 	(void) ct_tmpl_clear(tmpl_fd);
10903c7284bdSAlexander Eremin 	(void) close(tmpl_fd);
10913c7284bdSAlexander Eremin 
10923c7284bdSAlexander Eremin 	if (waitpid(child, &child_status, 0) != child) {
10933c7284bdSAlexander Eremin 		/* unexpected: we must have been signalled */
10943c7284bdSAlexander Eremin 		(void) contract_abandon_id(ct);
10953c7284bdSAlexander Eremin 		return (-1);
10963c7284bdSAlexander Eremin 	}
10973c7284bdSAlexander Eremin 
10983c7284bdSAlexander Eremin 	(void) contract_abandon_id(ct);
10993c7284bdSAlexander Eremin 	if (WEXITSTATUS(child_status) != 0) {
11003c7284bdSAlexander Eremin 		errno = WEXITSTATUS(child_status);
11013c7284bdSAlexander Eremin 		zerror(zlogp, B_FALSE, "unable to shutdown zone");
11023c7284bdSAlexander Eremin 		return (-1);
11033c7284bdSAlexander Eremin 	}
11043c7284bdSAlexander Eremin 
11053c7284bdSAlexander Eremin 	shutdown_in_progress = B_TRUE;
11063c7284bdSAlexander Eremin 
11073c7284bdSAlexander Eremin 	return (0);
11083c7284bdSAlexander Eremin }
11093c7284bdSAlexander Eremin 
11103c7284bdSAlexander Eremin static int
zone_wait_shutdown(zlog_t * zlogp)11113c7284bdSAlexander Eremin zone_wait_shutdown(zlog_t *zlogp)
11123c7284bdSAlexander Eremin {
11133c7284bdSAlexander Eremin 	zone_state_t zstate;
11143c7284bdSAlexander Eremin 	uint64_t *tm = NULL;
11153c7284bdSAlexander Eremin 	scf_simple_prop_t *prop = NULL;
11163c7284bdSAlexander Eremin 	int timeout;
11173c7284bdSAlexander Eremin 	int tries;
11183c7284bdSAlexander Eremin 	int rc = -1;
11193c7284bdSAlexander Eremin 
11203c7284bdSAlexander Eremin 	/* Get default stop timeout from SMF framework */
11213c7284bdSAlexander Eremin 	timeout = SHUTDOWN_WAIT;
11223c7284bdSAlexander Eremin 	if ((prop = scf_simple_prop_get(NULL, SHUTDOWN_FMRI, "stop",
11233c7284bdSAlexander Eremin 	    SCF_PROPERTY_TIMEOUT)) != NULL) {
11243c7284bdSAlexander Eremin 		if ((tm = scf_simple_prop_next_count(prop)) != NULL) {
11253c7284bdSAlexander Eremin 			if (tm != 0)
11263c7284bdSAlexander Eremin 				timeout = *tm;
11273c7284bdSAlexander Eremin 		}
11283c7284bdSAlexander Eremin 		scf_simple_prop_free(prop);
11293c7284bdSAlexander Eremin 	}
11303c7284bdSAlexander Eremin 
11313c7284bdSAlexander Eremin 	/* allow time for zone to shutdown cleanly */
11323c7284bdSAlexander Eremin 	for (tries = 0; tries < timeout; tries ++) {
11333c7284bdSAlexander Eremin 		(void) sleep(1);
11343c7284bdSAlexander Eremin 		if (zone_get_state(zone_name, &zstate) == Z_OK &&
11353c7284bdSAlexander Eremin 		    zstate == ZONE_STATE_INSTALLED) {
11363c7284bdSAlexander Eremin 			rc = 0;
11373c7284bdSAlexander Eremin 			break;
11383c7284bdSAlexander Eremin 		}
11393c7284bdSAlexander Eremin 	}
11403c7284bdSAlexander Eremin 
11413c7284bdSAlexander Eremin 	if (rc != 0)
11423c7284bdSAlexander Eremin 		zerror(zlogp, B_FALSE, "unable to shutdown zone");
11433c7284bdSAlexander Eremin 
11443c7284bdSAlexander Eremin 	shutdown_in_progress = B_FALSE;
11453c7284bdSAlexander Eremin 
11463c7284bdSAlexander Eremin 	return (rc);
11473c7284bdSAlexander Eremin }
11483c7284bdSAlexander Eremin 
11493c7284bdSAlexander Eremin 
11503c7284bdSAlexander Eremin 
11517c478bd9Sstevel@tonic-gate /*
11527c478bd9Sstevel@tonic-gate  * Generate AUE_zone_state for a command that boots a zone.
11537c478bd9Sstevel@tonic-gate  */
11547c478bd9Sstevel@tonic-gate static void
audit_put_record(zlog_t * zlogp,ucred_t * uc,int return_val,char * new_state)11557c478bd9Sstevel@tonic-gate audit_put_record(zlog_t *zlogp, ucred_t *uc, int return_val,
11567c478bd9Sstevel@tonic-gate     char *new_state)
11577c478bd9Sstevel@tonic-gate {
11587c478bd9Sstevel@tonic-gate 	adt_session_data_t	*ah;
11597c478bd9Sstevel@tonic-gate 	adt_event_data_t	*event;
11607c478bd9Sstevel@tonic-gate 	int			pass_fail, fail_reason;
11617c478bd9Sstevel@tonic-gate 
11627c478bd9Sstevel@tonic-gate 	if (!adt_audit_enabled())
11637c478bd9Sstevel@tonic-gate 		return;
11647c478bd9Sstevel@tonic-gate 
11657c478bd9Sstevel@tonic-gate 	if (return_val == 0) {
11667c478bd9Sstevel@tonic-gate 		pass_fail = ADT_SUCCESS;
11677c478bd9Sstevel@tonic-gate 		fail_reason = ADT_SUCCESS;
11687c478bd9Sstevel@tonic-gate 	} else {
11697c478bd9Sstevel@tonic-gate 		pass_fail = ADT_FAILURE;
11707c478bd9Sstevel@tonic-gate 		fail_reason = ADT_FAIL_VALUE_PROGRAM;
11717c478bd9Sstevel@tonic-gate 	}
11727c478bd9Sstevel@tonic-gate 
11737c478bd9Sstevel@tonic-gate 	if (adt_start_session(&ah, NULL, 0)) {
11747c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, gettext("audit failure."));
11757c478bd9Sstevel@tonic-gate 		return;
11767c478bd9Sstevel@tonic-gate 	}
11777c478bd9Sstevel@tonic-gate 	if (adt_set_from_ucred(ah, uc, ADT_NEW)) {
11787c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, gettext("audit failure."));
11797c478bd9Sstevel@tonic-gate 		(void) adt_end_session(ah);
11807c478bd9Sstevel@tonic-gate 		return;
11817c478bd9Sstevel@tonic-gate 	}
11827c478bd9Sstevel@tonic-gate 
11837c478bd9Sstevel@tonic-gate 	event = adt_alloc_event(ah, ADT_zone_state);
11847c478bd9Sstevel@tonic-gate 	if (event == NULL) {
11857c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, gettext("audit failure."));
11867c478bd9Sstevel@tonic-gate 		(void) adt_end_session(ah);
11877c478bd9Sstevel@tonic-gate 		return;
11887c478bd9Sstevel@tonic-gate 	}
11897c478bd9Sstevel@tonic-gate 	event->adt_zone_state.zonename = zone_name;
11907c478bd9Sstevel@tonic-gate 	event->adt_zone_state.new_state = new_state;
11917c478bd9Sstevel@tonic-gate 
11927c478bd9Sstevel@tonic-gate 	if (adt_put_event(event, pass_fail, fail_reason))
11937c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, gettext("audit failure."));
11947c478bd9Sstevel@tonic-gate 
11957c478bd9Sstevel@tonic-gate 	adt_free_event(event);
11967c478bd9Sstevel@tonic-gate 
11977c478bd9Sstevel@tonic-gate 	(void) adt_end_session(ah);
11987c478bd9Sstevel@tonic-gate }
11997c478bd9Sstevel@tonic-gate 
12007c478bd9Sstevel@tonic-gate /*
12017c478bd9Sstevel@tonic-gate  * The main routine for the door server that deals with zone state transitions.
12027c478bd9Sstevel@tonic-gate  */
12037c478bd9Sstevel@tonic-gate /* ARGSUSED */
12047c478bd9Sstevel@tonic-gate static void
server(void * cookie,char * args,size_t alen,door_desc_t * dp,uint_t n_desc)12057c478bd9Sstevel@tonic-gate server(void *cookie, char *args, size_t alen, door_desc_t *dp,
12067c478bd9Sstevel@tonic-gate     uint_t n_desc)
12077c478bd9Sstevel@tonic-gate {
12087c478bd9Sstevel@tonic-gate 	ucred_t *uc = NULL;
12097c478bd9Sstevel@tonic-gate 	const priv_set_t *eset;
12107c478bd9Sstevel@tonic-gate 
12117c478bd9Sstevel@tonic-gate 	zone_state_t zstate;
12127c478bd9Sstevel@tonic-gate 	zone_cmd_t cmd;
12137c478bd9Sstevel@tonic-gate 	zone_cmd_arg_t *zargp;
12147c478bd9Sstevel@tonic-gate 
12152a22bccaSToomas Soome 	boolean_t kernelcall = B_FALSE;
12167c478bd9Sstevel@tonic-gate 
12177c478bd9Sstevel@tonic-gate 	int rval = -1;
12187c478bd9Sstevel@tonic-gate 	uint64_t uniqid;
12197c478bd9Sstevel@tonic-gate 	zoneid_t zoneid = -1;
12207c478bd9Sstevel@tonic-gate 	zlog_t zlog;
12217c478bd9Sstevel@tonic-gate 	zlog_t *zlogp;
12227c478bd9Sstevel@tonic-gate 	zone_cmd_rval_t *rvalp;
12237c478bd9Sstevel@tonic-gate 	size_t rlen = getpagesize(); /* conservative */
12249acbbeafSnn35248 	fs_callback_t cb;
1225123807fbSedp 	brand_handle_t bh;
12263c7284bdSAlexander Eremin 	boolean_t wait_shut = B_FALSE;
12277c478bd9Sstevel@tonic-gate 
12287c478bd9Sstevel@tonic-gate 	/* LINTED E_BAD_PTR_CAST_ALIGN */
12297c478bd9Sstevel@tonic-gate 	zargp = (zone_cmd_arg_t *)args;
12307c478bd9Sstevel@tonic-gate 
12317c478bd9Sstevel@tonic-gate 	/*
12327c478bd9Sstevel@tonic-gate 	 * When we get the door unref message, we've fdetach'd the door, and
12337c478bd9Sstevel@tonic-gate 	 * it is time for us to shut down zoneadmd.
12347c478bd9Sstevel@tonic-gate 	 */
12357c478bd9Sstevel@tonic-gate 	if (zargp == DOOR_UNREF_DATA) {
12367c478bd9Sstevel@tonic-gate 		/*
12377c478bd9Sstevel@tonic-gate 		 * See comment at end of main() for info on the last rites.
12387c478bd9Sstevel@tonic-gate 		 */
12397c478bd9Sstevel@tonic-gate 		exit(0);
12407c478bd9Sstevel@tonic-gate 	}
12417c478bd9Sstevel@tonic-gate 
12427c478bd9Sstevel@tonic-gate 	if (zargp == NULL) {
12437c478bd9Sstevel@tonic-gate 		(void) door_return(NULL, 0, 0, 0);
12447c478bd9Sstevel@tonic-gate 	}
12457c478bd9Sstevel@tonic-gate 
12467c478bd9Sstevel@tonic-gate 	rvalp = alloca(rlen);
12477c478bd9Sstevel@tonic-gate 	bzero(rvalp, rlen);
12487c478bd9Sstevel@tonic-gate 	zlog.logfile = NULL;
12497c478bd9Sstevel@tonic-gate 	zlog.buflen = zlog.loglen = rlen - sizeof (zone_cmd_rval_t) + 1;
12507c478bd9Sstevel@tonic-gate 	zlog.buf = rvalp->errbuf;
12517c478bd9Sstevel@tonic-gate 	zlog.log = zlog.buf;
12527c478bd9Sstevel@tonic-gate 	/* defer initialization of zlog.locale until after credential check */
12537c478bd9Sstevel@tonic-gate 	zlogp = &zlog;
12547c478bd9Sstevel@tonic-gate 
12557c478bd9Sstevel@tonic-gate 	if (alen != sizeof (zone_cmd_arg_t)) {
12567c478bd9Sstevel@tonic-gate 		/*
12577c478bd9Sstevel@tonic-gate 		 * This really shouldn't be happening.
12587c478bd9Sstevel@tonic-gate 		 */
12593f2f09c1Sdp 		zerror(&logsys, B_FALSE, "argument size (%d bytes) "
12603f2f09c1Sdp 		    "unexpected (expected %d bytes)", alen,
12613f2f09c1Sdp 		    sizeof (zone_cmd_arg_t));
12627c478bd9Sstevel@tonic-gate 		goto out;
12637c478bd9Sstevel@tonic-gate 	}
12647c478bd9Sstevel@tonic-gate 	cmd = zargp->cmd;
12657c478bd9Sstevel@tonic-gate 
12667c478bd9Sstevel@tonic-gate 	if (door_ucred(&uc) != 0) {
12677c478bd9Sstevel@tonic-gate 		zerror(&logsys, B_TRUE, "door_ucred");
12687c478bd9Sstevel@tonic-gate 		goto out;
12697c478bd9Sstevel@tonic-gate 	}
12707c478bd9Sstevel@tonic-gate 	eset = ucred_getprivset(uc, PRIV_EFFECTIVE);
12717c478bd9Sstevel@tonic-gate 	if (ucred_getzoneid(uc) != GLOBAL_ZONEID ||
12727c478bd9Sstevel@tonic-gate 	    (eset != NULL ? !priv_ismember(eset, PRIV_SYS_CONFIG) :
12737c478bd9Sstevel@tonic-gate 	    ucred_geteuid(uc) != 0)) {
12747c478bd9Sstevel@tonic-gate 		zerror(&logsys, B_FALSE, "insufficient privileges");
12757c478bd9Sstevel@tonic-gate 		goto out;
12767c478bd9Sstevel@tonic-gate 	}
12777c478bd9Sstevel@tonic-gate 
12787c478bd9Sstevel@tonic-gate 	kernelcall = ucred_getpid(uc) == 0;
12797c478bd9Sstevel@tonic-gate 
12807c478bd9Sstevel@tonic-gate 	/*
12817c478bd9Sstevel@tonic-gate 	 * This is safe because we only use a zlog_t throughout the
12827c478bd9Sstevel@tonic-gate 	 * duration of a door call; i.e., by the time the pointer
12837c478bd9Sstevel@tonic-gate 	 * might become invalid, the door call would be over.
12847c478bd9Sstevel@tonic-gate 	 */
12857c478bd9Sstevel@tonic-gate 	zlog.locale = kernelcall ? DEFAULT_LOCALE : zargp->locale;
12867c478bd9Sstevel@tonic-gate 
12877c478bd9Sstevel@tonic-gate 	(void) mutex_lock(&lock);
12887c478bd9Sstevel@tonic-gate 
12897c478bd9Sstevel@tonic-gate 	/*
12907c478bd9Sstevel@tonic-gate 	 * Once we start to really die off, we don't want more connections.
12917c478bd9Sstevel@tonic-gate 	 */
12927c478bd9Sstevel@tonic-gate 	if (in_death_throes) {
12937c478bd9Sstevel@tonic-gate 		(void) mutex_unlock(&lock);
12947c478bd9Sstevel@tonic-gate 		ucred_free(uc);
12957c478bd9Sstevel@tonic-gate 		(void) door_return(NULL, 0, 0, 0);
12967c478bd9Sstevel@tonic-gate 		thr_exit(NULL);
12977c478bd9Sstevel@tonic-gate 	}
12987c478bd9Sstevel@tonic-gate 
12997c478bd9Sstevel@tonic-gate 	/*
13007c478bd9Sstevel@tonic-gate 	 * Check for validity of command.
13017c478bd9Sstevel@tonic-gate 	 */
13029acbbeafSnn35248 	if (cmd != Z_READY && cmd != Z_BOOT && cmd != Z_FORCEBOOT &&
13033c7284bdSAlexander Eremin 	    cmd != Z_REBOOT && cmd != Z_SHUTDOWN && cmd != Z_HALT &&
13043c7284bdSAlexander Eremin 	    cmd != Z_NOTE_UNINSTALLING && cmd != Z_MOUNT &&
13053c7284bdSAlexander Eremin 	    cmd != Z_FORCEMOUNT && cmd != Z_UNMOUNT) {
1306108322fbScarlsonj 		zerror(&logsys, B_FALSE, "invalid command %d", (int)cmd);
13077c478bd9Sstevel@tonic-gate 		goto out;
13087c478bd9Sstevel@tonic-gate 	}
13097c478bd9Sstevel@tonic-gate 
13107c478bd9Sstevel@tonic-gate 	if (kernelcall && (cmd != Z_HALT && cmd != Z_REBOOT)) {
13117c478bd9Sstevel@tonic-gate 		/*
13127c478bd9Sstevel@tonic-gate 		 * Can't happen
13137c478bd9Sstevel@tonic-gate 		 */
13147c478bd9Sstevel@tonic-gate 		zerror(&logsys, B_FALSE, "received unexpected kernel upcall %d",
13157c478bd9Sstevel@tonic-gate 		    cmd);
13167c478bd9Sstevel@tonic-gate 		goto out;
13177c478bd9Sstevel@tonic-gate 	}
13187c478bd9Sstevel@tonic-gate 	/*
13197c478bd9Sstevel@tonic-gate 	 * We ignore the possibility of someone calling zone_create(2)
13207c478bd9Sstevel@tonic-gate 	 * explicitly; all requests must come through zoneadmd.
13217c478bd9Sstevel@tonic-gate 	 */
13227c478bd9Sstevel@tonic-gate 	if (zone_get_state(zone_name, &zstate) != Z_OK) {
13237c478bd9Sstevel@tonic-gate 		/*
13247c478bd9Sstevel@tonic-gate 		 * Something terribly wrong happened
13257c478bd9Sstevel@tonic-gate 		 */
13267c478bd9Sstevel@tonic-gate 		zerror(&logsys, B_FALSE, "unable to determine state of zone");
13277c478bd9Sstevel@tonic-gate 		goto out;
13287c478bd9Sstevel@tonic-gate 	}
13297c478bd9Sstevel@tonic-gate 
13307c478bd9Sstevel@tonic-gate 	if (kernelcall) {
13317c478bd9Sstevel@tonic-gate 		/*
13327c478bd9Sstevel@tonic-gate 		 * Kernel-initiated requests may lose their validity if the
13337c478bd9Sstevel@tonic-gate 		 * zone_t the kernel was referring to has gone away.
13347c478bd9Sstevel@tonic-gate 		 */
13357c478bd9Sstevel@tonic-gate 		if ((zoneid = getzoneidbyname(zone_name)) == -1 ||
13367c478bd9Sstevel@tonic-gate 		    zone_getattr(zoneid, ZONE_ATTR_UNIQID, &uniqid,
13377c478bd9Sstevel@tonic-gate 		    sizeof (uniqid)) == -1 || uniqid != zargp->uniqid) {
13387c478bd9Sstevel@tonic-gate 			/*
13397c478bd9Sstevel@tonic-gate 			 * We're not talking about the same zone. The request
13407c478bd9Sstevel@tonic-gate 			 * must have arrived too late.  Return error.
13417c478bd9Sstevel@tonic-gate 			 */
13427c478bd9Sstevel@tonic-gate 			rval = -1;
13437c478bd9Sstevel@tonic-gate 			goto out;
13447c478bd9Sstevel@tonic-gate 		}
13457c478bd9Sstevel@tonic-gate 		zlogp = &logsys;	/* Log errors to syslog */
13467c478bd9Sstevel@tonic-gate 	}
13477c478bd9Sstevel@tonic-gate 
13489acbbeafSnn35248 	/*
13499acbbeafSnn35248 	 * If we are being asked to forcibly mount or boot a zone, we
13509acbbeafSnn35248 	 * pretend that an INCOMPLETE zone is actually INSTALLED.
13519acbbeafSnn35248 	 */
13529acbbeafSnn35248 	if (zstate == ZONE_STATE_INCOMPLETE &&
13539acbbeafSnn35248 	    (cmd == Z_FORCEBOOT || cmd == Z_FORCEMOUNT))
13549acbbeafSnn35248 		zstate = ZONE_STATE_INSTALLED;
13559acbbeafSnn35248 
13567c478bd9Sstevel@tonic-gate 	switch (zstate) {
13577c478bd9Sstevel@tonic-gate 	case ZONE_STATE_CONFIGURED:
13587c478bd9Sstevel@tonic-gate 	case ZONE_STATE_INCOMPLETE:
13597c478bd9Sstevel@tonic-gate 		/*
13607c478bd9Sstevel@tonic-gate 		 * Not our area of expertise; we just print a nice message
13617c478bd9Sstevel@tonic-gate 		 * and die off.
13627c478bd9Sstevel@tonic-gate 		 */
13637c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE,
13647c478bd9Sstevel@tonic-gate 		    "%s operation is invalid for zones in state '%s'",
1365108322fbScarlsonj 		    z_cmd_name(cmd), zone_state_str(zstate));
13667c478bd9Sstevel@tonic-gate 		break;
13677c478bd9Sstevel@tonic-gate 
13687c478bd9Sstevel@tonic-gate 	case ZONE_STATE_INSTALLED:
13697c478bd9Sstevel@tonic-gate 		switch (cmd) {
13707c478bd9Sstevel@tonic-gate 		case Z_READY:
1371c5cd6260S 			rval = zone_ready(zlogp, Z_MNT_BOOT, zstate);
13727c478bd9Sstevel@tonic-gate 			if (rval == 0)
13737c478bd9Sstevel@tonic-gate 				eventstream_write(Z_EVT_ZONE_READIED);
13747c478bd9Sstevel@tonic-gate 			break;
13757c478bd9Sstevel@tonic-gate 		case Z_BOOT:
13769acbbeafSnn35248 		case Z_FORCEBOOT:
13777c478bd9Sstevel@tonic-gate 			eventstream_write(Z_EVT_ZONE_BOOTING);
1378c5cd6260S 			if ((rval = zone_ready(zlogp, Z_MNT_BOOT, zstate))
1379c5cd6260S 			    == 0) {
1380c5cd6260S 				rval = zone_bootup(zlogp, zargp->bootbuf,
1381c5cd6260S 				    zstate);
1382c5cd6260S 			}
13837c478bd9Sstevel@tonic-gate 			audit_put_record(zlogp, uc, rval, "boot");
13847c478bd9Sstevel@tonic-gate 			if (rval != 0) {
13857c478bd9Sstevel@tonic-gate 				bringup_failure_recovery = B_TRUE;
1386c5cd6260S 				(void) zone_halt(zlogp, B_FALSE, B_FALSE,
1387c5cd6260S 				    zstate);
1388ffbafc53Scomay 				eventstream_write(Z_EVT_ZONE_BOOTFAILED);
13897c478bd9Sstevel@tonic-gate 			}
13907c478bd9Sstevel@tonic-gate 			break;
13913c7284bdSAlexander Eremin 		case Z_SHUTDOWN:
13927c478bd9Sstevel@tonic-gate 		case Z_HALT:
13937c478bd9Sstevel@tonic-gate 			if (kernelcall)	/* Invalid; can't happen */
13947c478bd9Sstevel@tonic-gate 				abort();
13957c478bd9Sstevel@tonic-gate 			/*
13967c478bd9Sstevel@tonic-gate 			 * We could have two clients racing to halt this
139748bbca81SDaniel Hoffman 			 * zone; the second client loses, but its request
13987c478bd9Sstevel@tonic-gate 			 * doesn't fail, since the zone is now in the desired
13997c478bd9Sstevel@tonic-gate 			 * state.
14007c478bd9Sstevel@tonic-gate 			 */
14017c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_FALSE, "zone is already halted");
14027c478bd9Sstevel@tonic-gate 			rval = 0;
14037c478bd9Sstevel@tonic-gate 			break;
14047c478bd9Sstevel@tonic-gate 		case Z_REBOOT:
14057c478bd9Sstevel@tonic-gate 			if (kernelcall)	/* Invalid; can't happen */
14067c478bd9Sstevel@tonic-gate 				abort();
14077c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_FALSE, "%s operation is invalid "
1408108322fbScarlsonj 			    "for zones in state '%s'", z_cmd_name(cmd),
14097c478bd9Sstevel@tonic-gate 			    zone_state_str(zstate));
14107c478bd9Sstevel@tonic-gate 			rval = -1;
14117c478bd9Sstevel@tonic-gate 			break;
14127c478bd9Sstevel@tonic-gate 		case Z_NOTE_UNINSTALLING:
14137c478bd9Sstevel@tonic-gate 			if (kernelcall)	/* Invalid; can't happen */
14147c478bd9Sstevel@tonic-gate 				abort();
14157c478bd9Sstevel@tonic-gate 			/*
14167c478bd9Sstevel@tonic-gate 			 * Tell the console to print out a message about this.
14177c478bd9Sstevel@tonic-gate 			 * Once it does, we will be in_death_throes.
14187c478bd9Sstevel@tonic-gate 			 */
14197c478bd9Sstevel@tonic-gate 			eventstream_write(Z_EVT_ZONE_UNINSTALLING);
14207c478bd9Sstevel@tonic-gate 			break;
1421108322fbScarlsonj 		case Z_MOUNT:
14229acbbeafSnn35248 		case Z_FORCEMOUNT:
1423108322fbScarlsonj 			if (kernelcall)	/* Invalid; can't happen */
1424108322fbScarlsonj 				abort();
14259a5d73e0SRic Aleshire 			if (!zone_isnative && !zone_iscluster &&
14269a5d73e0SRic Aleshire 			    !zone_islabeled) {
14270679f794S 				/*
14280679f794S 				 * -U mounts the zone without lofs mounting
14290679f794S 				 * zone file systems back into the scratch
14300679f794S 				 * zone.  This is required when mounting
14310679f794S 				 * non-native branded zones.
14320679f794S 				 */
14330679f794S 				(void) strlcpy(zargp->bootbuf, "-U",
14340679f794S 				    BOOTARGS_MAX);
1435ffbafc53Scomay 			}
1436ffbafc53Scomay 
14376cfd72c6Sgjelinek 			rval = zone_ready(zlogp,
14386cfd72c6Sgjelinek 			    strcmp(zargp->bootbuf, "-U") == 0 ?
1439c5cd6260S 			    Z_MNT_UPDATE : Z_MNT_SCRATCH, zstate);
14409acbbeafSnn35248 			if (rval != 0)
14419acbbeafSnn35248 				break;
14429acbbeafSnn35248 
14439acbbeafSnn35248 			eventstream_write(Z_EVT_ZONE_READIED);
14449acbbeafSnn35248 
14450679f794S 			/*
1446e5816e35SEdward Pilatowicz 			 * Get a handle to the default brand info.
1447e5816e35SEdward Pilatowicz 			 * We must always use the default brand file system
14480679f794S 			 * list when mounting the zone.
14490679f794S 			 */
1450e5816e35SEdward Pilatowicz 			if ((bh = brand_open(default_brand)) == NULL) {
14519acbbeafSnn35248 				rval = -1;
14529acbbeafSnn35248 				break;
14539acbbeafSnn35248 			}
14549acbbeafSnn35248 
14559acbbeafSnn35248 			/*
14569acbbeafSnn35248 			 * Get the list of filesystems to mount from
14579acbbeafSnn35248 			 * the brand configuration.  These mounts are done
14589acbbeafSnn35248 			 * via a thread that will enter the zone, so they
14599acbbeafSnn35248 			 * are done from within the context of the zone.
14609acbbeafSnn35248 			 */
14619acbbeafSnn35248 			cb.zlogp = zlogp;
14629acbbeafSnn35248 			cb.zoneid = zone_id;
1463910f48daSedp 			cb.mount_cmd = B_TRUE;
1464123807fbSedp 			rval = brand_platform_iter_mounts(bh,
14659acbbeafSnn35248 			    mount_early_fs, &cb);
14669acbbeafSnn35248 
1467123807fbSedp 			brand_close(bh);
14689acbbeafSnn35248 
1469108322fbScarlsonj 			/*
1470108322fbScarlsonj 			 * Ordinarily, /dev/fd would be mounted inside the zone
1471108322fbScarlsonj 			 * by svc:/system/filesystem/usr:default, but since
1472108322fbScarlsonj 			 * we're not booting the zone, we need to do this
1473108322fbScarlsonj 			 * manually.
1474108322fbScarlsonj 			 */
1475108322fbScarlsonj 			if (rval == 0)
14769acbbeafSnn35248 				rval = mount_early_fs(&cb,
14779acbbeafSnn35248 				    "fd", "/dev/fd", "fd", NULL);
1478108322fbScarlsonj 			break;
1479108322fbScarlsonj 		case Z_UNMOUNT:
1480108322fbScarlsonj 			if (kernelcall)	/* Invalid; can't happen */
1481108322fbScarlsonj 				abort();
1482108322fbScarlsonj 			zerror(zlogp, B_FALSE, "zone is already unmounted");
1483108322fbScarlsonj 			rval = 0;
1484108322fbScarlsonj 			break;
14857c478bd9Sstevel@tonic-gate 		}
14867c478bd9Sstevel@tonic-gate 		break;
14877c478bd9Sstevel@tonic-gate 
14887c478bd9Sstevel@tonic-gate 	case ZONE_STATE_READY:
14897c478bd9Sstevel@tonic-gate 		switch (cmd) {
14907c478bd9Sstevel@tonic-gate 		case Z_READY:
14917c478bd9Sstevel@tonic-gate 			/*
14927c478bd9Sstevel@tonic-gate 			 * We could have two clients racing to ready this
149348bbca81SDaniel Hoffman 			 * zone; the second client loses, but its request
14947c478bd9Sstevel@tonic-gate 			 * doesn't fail, since the zone is now in the desired
14957c478bd9Sstevel@tonic-gate 			 * state.
14967c478bd9Sstevel@tonic-gate 			 */
14977c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_FALSE, "zone is already ready");
14987c478bd9Sstevel@tonic-gate 			rval = 0;
14997c478bd9Sstevel@tonic-gate 			break;
15002a22bccaSToomas Soome 		case Z_FORCEBOOT:
15017c478bd9Sstevel@tonic-gate 		case Z_BOOT:
15023f2f09c1Sdp 			(void) strlcpy(boot_args, zargp->bootbuf,
15033f2f09c1Sdp 			    sizeof (boot_args));
15047c478bd9Sstevel@tonic-gate 			eventstream_write(Z_EVT_ZONE_BOOTING);
1505c5cd6260S 			rval = zone_bootup(zlogp, zargp->bootbuf, zstate);
15067c478bd9Sstevel@tonic-gate 			audit_put_record(zlogp, uc, rval, "boot");
15077c478bd9Sstevel@tonic-gate 			if (rval != 0) {
15087c478bd9Sstevel@tonic-gate 				bringup_failure_recovery = B_TRUE;
1509c5cd6260S 				(void) zone_halt(zlogp, B_FALSE, B_TRUE,
1510c5cd6260S 				    zstate);
1511ffbafc53Scomay 				eventstream_write(Z_EVT_ZONE_BOOTFAILED);
15127c478bd9Sstevel@tonic-gate 			}
15133f2f09c1Sdp 			boot_args[0] = '\0';
15147c478bd9Sstevel@tonic-gate 			break;
15157c478bd9Sstevel@tonic-gate 		case Z_HALT:
15167c478bd9Sstevel@tonic-gate 			if (kernelcall)	/* Invalid; can't happen */
15177c478bd9Sstevel@tonic-gate 				abort();
1518c5cd6260S 			if ((rval = zone_halt(zlogp, B_FALSE, B_FALSE, zstate))
1519c5cd6260S 			    != 0)
15207c478bd9Sstevel@tonic-gate 				break;
15217c478bd9Sstevel@tonic-gate 			eventstream_write(Z_EVT_ZONE_HALTED);
15227c478bd9Sstevel@tonic-gate 			break;
15233c7284bdSAlexander Eremin 		case Z_SHUTDOWN:
15247c478bd9Sstevel@tonic-gate 		case Z_REBOOT:
1525108322fbScarlsonj 		case Z_NOTE_UNINSTALLING:
15262a22bccaSToomas Soome 		case Z_FORCEMOUNT:
1527108322fbScarlsonj 		case Z_MOUNT:
1528108322fbScarlsonj 		case Z_UNMOUNT:
15297c478bd9Sstevel@tonic-gate 			if (kernelcall)	/* Invalid; can't happen */
15307c478bd9Sstevel@tonic-gate 				abort();
15317c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_FALSE, "%s operation is invalid "
1532108322fbScarlsonj 			    "for zones in state '%s'", z_cmd_name(cmd),
15337c478bd9Sstevel@tonic-gate 			    zone_state_str(zstate));
15347c478bd9Sstevel@tonic-gate 			rval = -1;
15357c478bd9Sstevel@tonic-gate 			break;
1536108322fbScarlsonj 		}
1537108322fbScarlsonj 		break;
1538108322fbScarlsonj 
1539108322fbScarlsonj 	case ZONE_STATE_MOUNTED:
1540108322fbScarlsonj 		switch (cmd) {
1541108322fbScarlsonj 		case Z_UNMOUNT:
15427c478bd9Sstevel@tonic-gate 			if (kernelcall)	/* Invalid; can't happen */
15437c478bd9Sstevel@tonic-gate 				abort();
1544c5cd6260S 			rval = zone_halt(zlogp, B_TRUE, B_FALSE, zstate);
1545ffbafc53Scomay 			if (rval == 0) {
1546ffbafc53Scomay 				eventstream_write(Z_EVT_ZONE_HALTED);
1547108322fbScarlsonj 				(void) sema_post(&scratch_sem);
1548ffbafc53Scomay 			}
1549108322fbScarlsonj 			break;
1550108322fbScarlsonj 		default:
1551108322fbScarlsonj 			if (kernelcall)	/* Invalid; can't happen */
1552108322fbScarlsonj 				abort();
1553108322fbScarlsonj 			zerror(zlogp, B_FALSE, "%s operation is invalid "
1554108322fbScarlsonj 			    "for zones in state '%s'", z_cmd_name(cmd),
1555108322fbScarlsonj 			    zone_state_str(zstate));
15567c478bd9Sstevel@tonic-gate 			rval = -1;
15577c478bd9Sstevel@tonic-gate 			break;
15587c478bd9Sstevel@tonic-gate 		}
15597c478bd9Sstevel@tonic-gate 		break;
15607c478bd9Sstevel@tonic-gate 
15617c478bd9Sstevel@tonic-gate 	case ZONE_STATE_RUNNING:
15627c478bd9Sstevel@tonic-gate 	case ZONE_STATE_SHUTTING_DOWN:
15637c478bd9Sstevel@tonic-gate 	case ZONE_STATE_DOWN:
15647c478bd9Sstevel@tonic-gate 		switch (cmd) {
15657c478bd9Sstevel@tonic-gate 		case Z_READY:
1566c5cd6260S 			if ((rval = zone_halt(zlogp, B_FALSE, B_TRUE, zstate))
1567c5cd6260S 			    != 0)
15687c478bd9Sstevel@tonic-gate 				break;
1569c5cd6260S 			if ((rval = zone_ready(zlogp, Z_MNT_BOOT, zstate)) == 0)
15707c478bd9Sstevel@tonic-gate 				eventstream_write(Z_EVT_ZONE_READIED);
1571ffbafc53Scomay 			else
1572ffbafc53Scomay 				eventstream_write(Z_EVT_ZONE_HALTED);
15737c478bd9Sstevel@tonic-gate 			break;
15742a22bccaSToomas Soome 		case Z_FORCEBOOT:
15757c478bd9Sstevel@tonic-gate 		case Z_BOOT:
15767c478bd9Sstevel@tonic-gate 			/*
15777c478bd9Sstevel@tonic-gate 			 * We could have two clients racing to boot this
157848bbca81SDaniel Hoffman 			 * zone; the second client loses, but its request
15797c478bd9Sstevel@tonic-gate 			 * doesn't fail, since the zone is now in the desired
15807c478bd9Sstevel@tonic-gate 			 * state.
15817c478bd9Sstevel@tonic-gate 			 */
15827c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_FALSE, "zone is already booted");
15837c478bd9Sstevel@tonic-gate 			rval = 0;
15847c478bd9Sstevel@tonic-gate 			break;
15857c478bd9Sstevel@tonic-gate 		case Z_HALT:
1586c5cd6260S 			if ((rval = zone_halt(zlogp, B_FALSE, B_FALSE, zstate))
1587c5cd6260S 			    != 0)
15887c478bd9Sstevel@tonic-gate 				break;
15897c478bd9Sstevel@tonic-gate 			eventstream_write(Z_EVT_ZONE_HALTED);
15907c478bd9Sstevel@tonic-gate 			break;
15917c478bd9Sstevel@tonic-gate 		case Z_REBOOT:
15923f2f09c1Sdp 			(void) strlcpy(boot_args, zargp->bootbuf,
15933f2f09c1Sdp 			    sizeof (boot_args));
15947c478bd9Sstevel@tonic-gate 			eventstream_write(Z_EVT_ZONE_REBOOTING);
1595c5cd6260S 			if ((rval = zone_halt(zlogp, B_FALSE, B_TRUE, zstate))
1596c5cd6260S 			    != 0) {
1597ffbafc53Scomay 				eventstream_write(Z_EVT_ZONE_BOOTFAILED);
15983f2f09c1Sdp 				boot_args[0] = '\0';
15997c478bd9Sstevel@tonic-gate 				break;
1600ffbafc53Scomay 			}
1601c5cd6260S 			if ((rval = zone_ready(zlogp, Z_MNT_BOOT, zstate))
1602c5cd6260S 			    != 0) {
1603ffbafc53Scomay 				eventstream_write(Z_EVT_ZONE_BOOTFAILED);
16043f2f09c1Sdp 				boot_args[0] = '\0';
1605ffbafc53Scomay 				break;
1606ffbafc53Scomay 			}
1607c5cd6260S 			rval = zone_bootup(zlogp, zargp->bootbuf, zstate);
16087c478bd9Sstevel@tonic-gate 			audit_put_record(zlogp, uc, rval, "reboot");
1609ffbafc53Scomay 			if (rval != 0) {
1610c5cd6260S 				(void) zone_halt(zlogp, B_FALSE, B_TRUE,
1611c5cd6260S 				    zstate);
1612ffbafc53Scomay 				eventstream_write(Z_EVT_ZONE_BOOTFAILED);
16137c478bd9Sstevel@tonic-gate 			}
16143f2f09c1Sdp 			boot_args[0] = '\0';
16157c478bd9Sstevel@tonic-gate 			break;
16163c7284bdSAlexander Eremin 		case Z_SHUTDOWN:
16173c7284bdSAlexander Eremin 			if ((rval = zone_graceful_shutdown(zlogp)) == 0) {
16183c7284bdSAlexander Eremin 				wait_shut = B_TRUE;
16193c7284bdSAlexander Eremin 			}
16203c7284bdSAlexander Eremin 			break;
16217c478bd9Sstevel@tonic-gate 		case Z_NOTE_UNINSTALLING:
16222a22bccaSToomas Soome 		case Z_FORCEMOUNT:
1623108322fbScarlsonj 		case Z_MOUNT:
1624108322fbScarlsonj 		case Z_UNMOUNT:
1625108322fbScarlsonj 			zerror(zlogp, B_FALSE, "%s operation is invalid "
1626108322fbScarlsonj 			    "for zones in state '%s'", z_cmd_name(cmd),
1627108322fbScarlsonj 			    zone_state_str(zstate));
16287c478bd9Sstevel@tonic-gate 			rval = -1;
16297c478bd9Sstevel@tonic-gate 			break;
16307c478bd9Sstevel@tonic-gate 		}
16317c478bd9Sstevel@tonic-gate 		break;
16327c478bd9Sstevel@tonic-gate 	default:
16337c478bd9Sstevel@tonic-gate 		abort();
16347c478bd9Sstevel@tonic-gate 	}
16357c478bd9Sstevel@tonic-gate 
16367c478bd9Sstevel@tonic-gate 	/*
16377c478bd9Sstevel@tonic-gate 	 * Because the state of the zone may have changed, we make sure
16387c478bd9Sstevel@tonic-gate 	 * to wake the console poller, which is in charge of initiating
16397c478bd9Sstevel@tonic-gate 	 * the shutdown procedure as necessary.
16407c478bd9Sstevel@tonic-gate 	 */
16417c478bd9Sstevel@tonic-gate 	eventstream_write(Z_EVT_NULL);
16427c478bd9Sstevel@tonic-gate 
16437c478bd9Sstevel@tonic-gate out:
16447c478bd9Sstevel@tonic-gate 	(void) mutex_unlock(&lock);
16453c7284bdSAlexander Eremin 
16463c7284bdSAlexander Eremin 	/* Wait for the Z_SHUTDOWN commands to complete */
16473c7284bdSAlexander Eremin 	if (wait_shut)
16483c7284bdSAlexander Eremin 		rval = zone_wait_shutdown(zlogp);
16493c7284bdSAlexander Eremin 
16507c478bd9Sstevel@tonic-gate 	if (kernelcall) {
16517c478bd9Sstevel@tonic-gate 		rvalp = NULL;
16527c478bd9Sstevel@tonic-gate 		rlen = 0;
16537c478bd9Sstevel@tonic-gate 	} else {
16547c478bd9Sstevel@tonic-gate 		rvalp->rval = rval;
16557c478bd9Sstevel@tonic-gate 	}
16567c478bd9Sstevel@tonic-gate 	if (uc != NULL)
16577c478bd9Sstevel@tonic-gate 		ucred_free(uc);
16587c478bd9Sstevel@tonic-gate 	(void) door_return((char *)rvalp, rlen, NULL, 0);
16597c478bd9Sstevel@tonic-gate 	thr_exit(NULL);
16607c478bd9Sstevel@tonic-gate }
16617c478bd9Sstevel@tonic-gate 
16627c478bd9Sstevel@tonic-gate static int
setup_door(zlog_t * zlogp)16637c478bd9Sstevel@tonic-gate setup_door(zlog_t *zlogp)
16647c478bd9Sstevel@tonic-gate {
16657c478bd9Sstevel@tonic-gate 	if ((zone_door = door_create(server, NULL,
16667c478bd9Sstevel@tonic-gate 	    DOOR_UNREF | DOOR_REFUSE_DESC | DOOR_NO_CANCEL)) < 0) {
16677c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "%s failed", "door_create");
16687c478bd9Sstevel@tonic-gate 		return (-1);
16697c478bd9Sstevel@tonic-gate 	}
16707c478bd9Sstevel@tonic-gate 	(void) fdetach(zone_door_path);
16717c478bd9Sstevel@tonic-gate 
16727c478bd9Sstevel@tonic-gate 	if (fattach(zone_door, zone_door_path) != 0) {
16737c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "fattach to %s failed", zone_door_path);
16747c478bd9Sstevel@tonic-gate 		(void) door_revoke(zone_door);
16757c478bd9Sstevel@tonic-gate 		(void) fdetach(zone_door_path);
16767c478bd9Sstevel@tonic-gate 		zone_door = -1;
16777c478bd9Sstevel@tonic-gate 		return (-1);
16787c478bd9Sstevel@tonic-gate 	}
16797c478bd9Sstevel@tonic-gate 	return (0);
16807c478bd9Sstevel@tonic-gate }
16817c478bd9Sstevel@tonic-gate 
16827c478bd9Sstevel@tonic-gate /*
1683bbf21555SRichard Lowe  * zoneadm(8) will start zoneadmd if it thinks it isn't running; this
16847c478bd9Sstevel@tonic-gate  * is where zoneadmd itself will check to see that another instance of
16857c478bd9Sstevel@tonic-gate  * zoneadmd isn't already controlling this zone.
16867c478bd9Sstevel@tonic-gate  *
16877c478bd9Sstevel@tonic-gate  * The idea here is that we want to open the path to which we will
16887c478bd9Sstevel@tonic-gate  * attach our door, lock it, and then make sure that no-one has beat us
16897c478bd9Sstevel@tonic-gate  * to fattach(3c)ing onto it.
16907c478bd9Sstevel@tonic-gate  *
16917c478bd9Sstevel@tonic-gate  * fattach(3c) is really a mount, so there are actually two possible
16927c478bd9Sstevel@tonic-gate  * vnodes we could be dealing with.  Our strategy is as follows:
16937c478bd9Sstevel@tonic-gate  *
16947c478bd9Sstevel@tonic-gate  * - If the file we opened is a regular file (common case):
16957c478bd9Sstevel@tonic-gate  *	There is no fattach(3c)ed door, so we have a chance of becoming
16967c478bd9Sstevel@tonic-gate  *	the managing zoneadmd. We attempt to lock the file: if it is
16977c478bd9Sstevel@tonic-gate  *	already locked, that means someone else raced us here, so we
1698bbf21555SRichard Lowe  *	lose and give up.  zoneadm(8) will try to contact the zoneadmd
16997c478bd9Sstevel@tonic-gate  *	that beat us to it.
17007c478bd9Sstevel@tonic-gate  *
17017c478bd9Sstevel@tonic-gate  * - If the file we opened is a namefs file:
17027c478bd9Sstevel@tonic-gate  *	This means there is already an established door fattach(3c)'ed
17037c478bd9Sstevel@tonic-gate  *	to the rendezvous path.  We've lost the race, so we give up.
17047c478bd9Sstevel@tonic-gate  *	Note that in this case we also try to grab the file lock, and
17057c478bd9Sstevel@tonic-gate  *	will succeed in acquiring it since the vnode locked by the
17067c478bd9Sstevel@tonic-gate  *	"winning" zoneadmd was a regular one, and the one we locked was
17077c478bd9Sstevel@tonic-gate  *	the fattach(3c)'ed door node.  At any rate, no harm is done, and
1708bbf21555SRichard Lowe  *	we just return to zoneadm(8) which knows to retry.
17097c478bd9Sstevel@tonic-gate  */
17107c478bd9Sstevel@tonic-gate static int
make_daemon_exclusive(zlog_t * zlogp)17117c478bd9Sstevel@tonic-gate make_daemon_exclusive(zlog_t *zlogp)
17127c478bd9Sstevel@tonic-gate {
17137c478bd9Sstevel@tonic-gate 	int doorfd = -1;
17147c478bd9Sstevel@tonic-gate 	int err, ret = -1;
17157c478bd9Sstevel@tonic-gate 	struct stat st;
17167c478bd9Sstevel@tonic-gate 	struct flock flock;
17177c478bd9Sstevel@tonic-gate 	zone_state_t zstate;
17187c478bd9Sstevel@tonic-gate 
17197c478bd9Sstevel@tonic-gate top:
17207c478bd9Sstevel@tonic-gate 	if ((err = zone_get_state(zone_name, &zstate)) != Z_OK) {
17213f2f09c1Sdp 		zerror(zlogp, B_FALSE, "failed to get zone state: %s",
17227c478bd9Sstevel@tonic-gate 		    zonecfg_strerror(err));
17237c478bd9Sstevel@tonic-gate 		goto out;
17247c478bd9Sstevel@tonic-gate 	}
17257c478bd9Sstevel@tonic-gate 	if ((doorfd = open(zone_door_path, O_CREAT|O_RDWR,
17267c478bd9Sstevel@tonic-gate 	    S_IREAD|S_IWRITE)) < 0) {
17277c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "failed to open %s", zone_door_path);
17287c478bd9Sstevel@tonic-gate 		goto out;
17297c478bd9Sstevel@tonic-gate 	}
17307c478bd9Sstevel@tonic-gate 	if (fstat(doorfd, &st) < 0) {
17317c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "failed to stat %s", zone_door_path);
17327c478bd9Sstevel@tonic-gate 		goto out;
17337c478bd9Sstevel@tonic-gate 	}
17347c478bd9Sstevel@tonic-gate 	/*
17357c478bd9Sstevel@tonic-gate 	 * Lock the file to synchronize with other zoneadmd
17367c478bd9Sstevel@tonic-gate 	 */
17377c478bd9Sstevel@tonic-gate 	flock.l_type = F_WRLCK;
17387c478bd9Sstevel@tonic-gate 	flock.l_whence = SEEK_SET;
17397c478bd9Sstevel@tonic-gate 	flock.l_start = (off_t)0;
17407c478bd9Sstevel@tonic-gate 	flock.l_len = (off_t)0;
17417c478bd9Sstevel@tonic-gate 	if (fcntl(doorfd, F_SETLK, &flock) < 0) {
17427c478bd9Sstevel@tonic-gate 		/*
17437c478bd9Sstevel@tonic-gate 		 * Someone else raced us here and grabbed the lock file
17447c478bd9Sstevel@tonic-gate 		 * first.  A warning here is inappropriate since nothing
17457c478bd9Sstevel@tonic-gate 		 * went wrong.
17467c478bd9Sstevel@tonic-gate 		 */
17477c478bd9Sstevel@tonic-gate 		goto out;
17487c478bd9Sstevel@tonic-gate 	}
17497c478bd9Sstevel@tonic-gate 
17507c478bd9Sstevel@tonic-gate 	if (strcmp(st.st_fstype, "namefs") == 0) {
17517c478bd9Sstevel@tonic-gate 		struct door_info info;
17527c478bd9Sstevel@tonic-gate 
17537c478bd9Sstevel@tonic-gate 		/*
17547c478bd9Sstevel@tonic-gate 		 * There is already something fattach()'ed to this file.
17557c478bd9Sstevel@tonic-gate 		 * Lets see what the door is up to.
17567c478bd9Sstevel@tonic-gate 		 */
17577c478bd9Sstevel@tonic-gate 		if (door_info(doorfd, &info) == 0 && info.di_target != -1) {
17587c478bd9Sstevel@tonic-gate 			/*
17597c478bd9Sstevel@tonic-gate 			 * Another zoneadmd process seems to be in
17607c478bd9Sstevel@tonic-gate 			 * control of the situation and we don't need to
17617c478bd9Sstevel@tonic-gate 			 * be here.  A warning here is inappropriate
17627c478bd9Sstevel@tonic-gate 			 * since nothing went wrong.
17637c478bd9Sstevel@tonic-gate 			 *
17647c478bd9Sstevel@tonic-gate 			 * If the door has been revoked, the zoneadmd
17657c478bd9Sstevel@tonic-gate 			 * process currently managing the zone is going
1766bbf21555SRichard Lowe 			 * away.  We'll return control to zoneadm(8)
17677c478bd9Sstevel@tonic-gate 			 * which will try again (by which time zoneadmd
17687c478bd9Sstevel@tonic-gate 			 * will hopefully have exited).
17697c478bd9Sstevel@tonic-gate 			 */
17707c478bd9Sstevel@tonic-gate 			goto out;
17717c478bd9Sstevel@tonic-gate 		}
17727c478bd9Sstevel@tonic-gate 
17737c478bd9Sstevel@tonic-gate 		/*
17747c478bd9Sstevel@tonic-gate 		 * If we got this far, there's a fattach(3c)'ed door
17757c478bd9Sstevel@tonic-gate 		 * that belongs to a process that has exited, which can
17767c478bd9Sstevel@tonic-gate 		 * happen if the previous zoneadmd died unexpectedly.
17777c478bd9Sstevel@tonic-gate 		 *
17787c478bd9Sstevel@tonic-gate 		 * Let user know that something is amiss, but that we can
17797c478bd9Sstevel@tonic-gate 		 * recover; if the zone is in the installed state, then don't
17807c478bd9Sstevel@tonic-gate 		 * message, since having a running zoneadmd isn't really
17817c478bd9Sstevel@tonic-gate 		 * expected/needed.  We want to keep occurences of this message
17827c478bd9Sstevel@tonic-gate 		 * limited to times when zoneadmd is picking back up from a
17837c478bd9Sstevel@tonic-gate 		 * zoneadmd that died while the zone was in some non-trivial
17847c478bd9Sstevel@tonic-gate 		 * state.
17857c478bd9Sstevel@tonic-gate 		 */
17867c478bd9Sstevel@tonic-gate 		if (zstate > ZONE_STATE_INSTALLED) {
17877c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_FALSE,
17887c478bd9Sstevel@tonic-gate 			    "zone '%s': WARNING: zone is in state '%s', but "
17897c478bd9Sstevel@tonic-gate 			    "zoneadmd does not appear to be available; "
17907c478bd9Sstevel@tonic-gate 			    "restarted zoneadmd to recover.",
17917c478bd9Sstevel@tonic-gate 			    zone_name, zone_state_str(zstate));
17927c478bd9Sstevel@tonic-gate 		}
17937c478bd9Sstevel@tonic-gate 
17947c478bd9Sstevel@tonic-gate 		(void) fdetach(zone_door_path);
17957c478bd9Sstevel@tonic-gate 		(void) close(doorfd);
17967c478bd9Sstevel@tonic-gate 		goto top;
17977c478bd9Sstevel@tonic-gate 	}
17987c478bd9Sstevel@tonic-gate 	ret = 0;
17997c478bd9Sstevel@tonic-gate out:
18007c478bd9Sstevel@tonic-gate 	(void) close(doorfd);
18017c478bd9Sstevel@tonic-gate 	return (ret);
18027c478bd9Sstevel@tonic-gate }
18037c478bd9Sstevel@tonic-gate 
1804c5cd6260S /*
1805c5cd6260S  * Setup the brand's pre and post state change callbacks, as well as the
1806c5cd6260S  * query callback, if any of these exist.
1807c5cd6260S  */
1808c5cd6260S static int
brand_callback_init(brand_handle_t bh,char * zone_name)1809c5cd6260S brand_callback_init(brand_handle_t bh, char *zone_name)
1810c5cd6260S {
1811c5cd6260S 	char zpath[MAXPATHLEN];
1812c5cd6260S 
1813c5cd6260S 	if (zone_get_zonepath(zone_name, zpath, sizeof (zpath)) != Z_OK)
1814c5cd6260S 		return (-1);
1815c5cd6260S 
1816c5cd6260S 	(void) strlcpy(pre_statechg_hook, EXEC_PREFIX,
1817c5cd6260S 	    sizeof (pre_statechg_hook));
1818c5cd6260S 
1819c5cd6260S 	if (brand_get_prestatechange(bh, zone_name, zpath,
1820c5cd6260S 	    pre_statechg_hook + EXEC_LEN,
1821c5cd6260S 	    sizeof (pre_statechg_hook) - EXEC_LEN) != 0)
1822c5cd6260S 		return (-1);
1823c5cd6260S 
1824c5cd6260S 	if (strlen(pre_statechg_hook) <= EXEC_LEN)
1825c5cd6260S 		pre_statechg_hook[0] = '\0';
1826c5cd6260S 
1827c5cd6260S 	(void) strlcpy(post_statechg_hook, EXEC_PREFIX,
1828c5cd6260S 	    sizeof (post_statechg_hook));
1829c5cd6260S 
1830c5cd6260S 	if (brand_get_poststatechange(bh, zone_name, zpath,
1831c5cd6260S 	    post_statechg_hook + EXEC_LEN,
1832c5cd6260S 	    sizeof (post_statechg_hook) - EXEC_LEN) != 0)
1833c5cd6260S 		return (-1);
1834c5cd6260S 
1835c5cd6260S 	if (strlen(post_statechg_hook) <= EXEC_LEN)
1836c5cd6260S 		post_statechg_hook[0] = '\0';
1837c5cd6260S 
1838c5cd6260S 	(void) strlcpy(query_hook, EXEC_PREFIX,
1839c5cd6260S 	    sizeof (query_hook));
1840c5cd6260S 
1841c5cd6260S 	if (brand_get_query(bh, zone_name, zpath, query_hook + EXEC_LEN,
1842c5cd6260S 	    sizeof (query_hook) - EXEC_LEN) != 0)
1843c5cd6260S 		return (-1);
1844c5cd6260S 
1845c5cd6260S 	if (strlen(query_hook) <= EXEC_LEN)
1846c5cd6260S 		query_hook[0] = '\0';
1847c5cd6260S 
1848c5cd6260S 	return (0);
1849c5cd6260S }
1850c5cd6260S 
18517c478bd9Sstevel@tonic-gate int
main(int argc,char * argv[])18527c478bd9Sstevel@tonic-gate main(int argc, char *argv[])
18537c478bd9Sstevel@tonic-gate {
18547c478bd9Sstevel@tonic-gate 	int opt;
18557c478bd9Sstevel@tonic-gate 	zoneid_t zid;
18567c478bd9Sstevel@tonic-gate 	priv_set_t *privset;
18577c478bd9Sstevel@tonic-gate 	zone_state_t zstate;
18587c478bd9Sstevel@tonic-gate 	char parents_locale[MAXPATHLEN];
1859123807fbSedp 	brand_handle_t bh;
18607c478bd9Sstevel@tonic-gate 	int err;
18617c478bd9Sstevel@tonic-gate 
18627c478bd9Sstevel@tonic-gate 	pid_t pid;
18637c478bd9Sstevel@tonic-gate 	sigset_t blockset;
18647c478bd9Sstevel@tonic-gate 	sigset_t block_cld;
18657c478bd9Sstevel@tonic-gate 
18667c478bd9Sstevel@tonic-gate 	struct {
18677c478bd9Sstevel@tonic-gate 		sema_t sem;
18687c478bd9Sstevel@tonic-gate 		int status;
18697c478bd9Sstevel@tonic-gate 		zlog_t log;
18707c478bd9Sstevel@tonic-gate 	} *shstate;
18717c478bd9Sstevel@tonic-gate 	size_t shstatelen = getpagesize();
18727c478bd9Sstevel@tonic-gate 
18737c478bd9Sstevel@tonic-gate 	zlog_t errlog;
18747c478bd9Sstevel@tonic-gate 	zlog_t *zlogp;
18757c478bd9Sstevel@tonic-gate 
18765cb4571dSdp 	int ctfd;
18775cb4571dSdp 
18787c478bd9Sstevel@tonic-gate 	progname = get_execbasename(argv[0]);
18797c478bd9Sstevel@tonic-gate 
18807c478bd9Sstevel@tonic-gate 	/*
18817c478bd9Sstevel@tonic-gate 	 * Make sure stderr is unbuffered
18827c478bd9Sstevel@tonic-gate 	 */
18837c478bd9Sstevel@tonic-gate 	(void) setbuffer(stderr, NULL, 0);
18847c478bd9Sstevel@tonic-gate 
18857c478bd9Sstevel@tonic-gate 	/*
18867c478bd9Sstevel@tonic-gate 	 * Get out of the way of mounted filesystems, since we will daemonize
18877c478bd9Sstevel@tonic-gate 	 * soon.
18887c478bd9Sstevel@tonic-gate 	 */
18897c478bd9Sstevel@tonic-gate 	(void) chdir("/");
18907c478bd9Sstevel@tonic-gate 
18917c478bd9Sstevel@tonic-gate 	/*
18927c478bd9Sstevel@tonic-gate 	 * Use the default system umask per PSARC 1998/110 rather than
18937c478bd9Sstevel@tonic-gate 	 * anything that may have been set by the caller.
18947c478bd9Sstevel@tonic-gate 	 */
18957c478bd9Sstevel@tonic-gate 	(void) umask(CMASK);
18967c478bd9Sstevel@tonic-gate 
18977c478bd9Sstevel@tonic-gate 	/*
18987c478bd9Sstevel@tonic-gate 	 * Initially we want to use our parent's locale.
18997c478bd9Sstevel@tonic-gate 	 */
19007c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
19017c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
19027c478bd9Sstevel@tonic-gate 	(void) strlcpy(parents_locale, setlocale(LC_MESSAGES, NULL),
19037c478bd9Sstevel@tonic-gate 	    sizeof (parents_locale));
19047c478bd9Sstevel@tonic-gate 
19057c478bd9Sstevel@tonic-gate 	/*
19067c478bd9Sstevel@tonic-gate 	 * This zlog_t is used for writing to stderr
19077c478bd9Sstevel@tonic-gate 	 */
19087c478bd9Sstevel@tonic-gate 	errlog.logfile = stderr;
19097c478bd9Sstevel@tonic-gate 	errlog.buflen = errlog.loglen = 0;
19107c478bd9Sstevel@tonic-gate 	errlog.buf = errlog.log = NULL;
19117c478bd9Sstevel@tonic-gate 	errlog.locale = parents_locale;
19127c478bd9Sstevel@tonic-gate 
19137c478bd9Sstevel@tonic-gate 	/*
19147c478bd9Sstevel@tonic-gate 	 * We start off writing to stderr until we're ready to daemonize.
19157c478bd9Sstevel@tonic-gate 	 */
19167c478bd9Sstevel@tonic-gate 	zlogp = &errlog;
19177c478bd9Sstevel@tonic-gate 
19187c478bd9Sstevel@tonic-gate 	/*
19197c478bd9Sstevel@tonic-gate 	 * Process options.
19207c478bd9Sstevel@tonic-gate 	 */
1921108322fbScarlsonj 	while ((opt = getopt(argc, argv, "R:z:")) != EOF) {
19227c478bd9Sstevel@tonic-gate 		switch (opt) {
1923108322fbScarlsonj 		case 'R':
1924108322fbScarlsonj 			zonecfg_set_root(optarg);
1925108322fbScarlsonj 			break;
19267c478bd9Sstevel@tonic-gate 		case 'z':
19277c478bd9Sstevel@tonic-gate 			zone_name = optarg;
19287c478bd9Sstevel@tonic-gate 			break;
19297c478bd9Sstevel@tonic-gate 		default:
19307c478bd9Sstevel@tonic-gate 			usage();
19317c478bd9Sstevel@tonic-gate 		}
19327c478bd9Sstevel@tonic-gate 	}
19337c478bd9Sstevel@tonic-gate 
19347c478bd9Sstevel@tonic-gate 	if (zone_name == NULL)
19357c478bd9Sstevel@tonic-gate 		usage();
19367c478bd9Sstevel@tonic-gate 
19377c478bd9Sstevel@tonic-gate 	/*
19387c478bd9Sstevel@tonic-gate 	 * Because usage() prints directly to stderr, it has gettext()
19397c478bd9Sstevel@tonic-gate 	 * wrapping, which depends on the locale.  But since zerror() calls
19407c478bd9Sstevel@tonic-gate 	 * localize() which tweaks the locale, it is not safe to call zerror()
19417c478bd9Sstevel@tonic-gate 	 * until after the last call to usage().  Fortunately, the last call
19427c478bd9Sstevel@tonic-gate 	 * to usage() is just above and the first call to zerror() is just
19437c478bd9Sstevel@tonic-gate 	 * below.  Don't mess this up.
19447c478bd9Sstevel@tonic-gate 	 */
19457c478bd9Sstevel@tonic-gate 	if (strcmp(zone_name, GLOBAL_ZONENAME) == 0) {
19467c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "cannot manage the %s zone",
19477c478bd9Sstevel@tonic-gate 		    GLOBAL_ZONENAME);
19487c478bd9Sstevel@tonic-gate 		return (1);
19497c478bd9Sstevel@tonic-gate 	}
19507c478bd9Sstevel@tonic-gate 
19517c478bd9Sstevel@tonic-gate 	if (zone_get_id(zone_name, &zid) != 0) {
19523f2f09c1Sdp 		zerror(zlogp, B_FALSE, "could not manage %s: %s", zone_name,
19537c478bd9Sstevel@tonic-gate 		    zonecfg_strerror(Z_NO_ZONE));
19547c478bd9Sstevel@tonic-gate 		return (1);
19557c478bd9Sstevel@tonic-gate 	}
19567c478bd9Sstevel@tonic-gate 
19577c478bd9Sstevel@tonic-gate 	if ((err = zone_get_state(zone_name, &zstate)) != Z_OK) {
19583f2f09c1Sdp 		zerror(zlogp, B_FALSE, "failed to get zone state: %s",
19597c478bd9Sstevel@tonic-gate 		    zonecfg_strerror(err));
19607c478bd9Sstevel@tonic-gate 		return (1);
19617c478bd9Sstevel@tonic-gate 	}
19629acbbeafSnn35248 	if (zstate < ZONE_STATE_INCOMPLETE) {
19637c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE,
19647c478bd9Sstevel@tonic-gate 		    "cannot manage a zone which is in state '%s'",
19657c478bd9Sstevel@tonic-gate 		    zone_state_str(zstate));
19667c478bd9Sstevel@tonic-gate 		return (1);
19677c478bd9Sstevel@tonic-gate 	}
19687c478bd9Sstevel@tonic-gate 
1969e5816e35SEdward Pilatowicz 	if (zonecfg_default_brand(default_brand,
1970e5816e35SEdward Pilatowicz 	    sizeof (default_brand)) != Z_OK) {
1971e5816e35SEdward Pilatowicz 		zerror(zlogp, B_FALSE, "unable to determine default brand");
1972e5816e35SEdward Pilatowicz 		return (1);
1973e5816e35SEdward Pilatowicz 	}
1974e5816e35SEdward Pilatowicz 
19759acbbeafSnn35248 	/* Get a handle to the brand info for this zone */
197662868012SSteve Lawrence 	if (zone_get_brand(zone_name, brand_name, sizeof (brand_name))
197762868012SSteve Lawrence 	    != Z_OK) {
19789acbbeafSnn35248 		zerror(zlogp, B_FALSE, "unable to determine zone brand");
19799acbbeafSnn35248 		return (1);
19809acbbeafSnn35248 	}
198162868012SSteve Lawrence 	zone_isnative = (strcmp(brand_name, NATIVE_BRAND_NAME) == 0);
19829a5d73e0SRic Aleshire 	zone_islabeled = (strcmp(brand_name, LABELED_BRAND_NAME) == 0);
1983c5cd6260S 
198462868012SSteve Lawrence 	/*
198562868012SSteve Lawrence 	 * In the alternate root environment, the only supported
198662868012SSteve Lawrence 	 * operations are mount and unmount.  In this case, just treat
198762868012SSteve Lawrence 	 * the zone as native if it is cluster.  Cluster zones can be
198862868012SSteve Lawrence 	 * native for the purpose of LU or upgrade, and the cluster
198962868012SSteve Lawrence 	 * brand may not exist in the miniroot (such as in net install
199062868012SSteve Lawrence 	 * upgrade).
199162868012SSteve Lawrence 	 */
199262868012SSteve Lawrence 	if (strcmp(brand_name, CLUSTER_BRAND_NAME) == 0) {
199362868012SSteve Lawrence 		zone_iscluster = B_TRUE;
199462868012SSteve Lawrence 		if (zonecfg_in_alt_root()) {
1995e5816e35SEdward Pilatowicz 			(void) strlcpy(brand_name, default_brand,
199662868012SSteve Lawrence 			    sizeof (brand_name));
199762868012SSteve Lawrence 		}
199862868012SSteve Lawrence 	} else {
199962868012SSteve Lawrence 		zone_iscluster = B_FALSE;
200062868012SSteve Lawrence 	}
200162868012SSteve Lawrence 
200262868012SSteve Lawrence 	if ((bh = brand_open(brand_name)) == NULL) {
200362868012SSteve Lawrence 		zerror(zlogp, B_FALSE, "unable to open zone brand");
200462868012SSteve Lawrence 		return (1);
200562868012SSteve Lawrence 	}
200662868012SSteve Lawrence 
2007c5cd6260S 	/* Get state change brand hooks. */
2008c5cd6260S 	if (brand_callback_init(bh, zone_name) == -1) {
2009c5cd6260S 		zerror(zlogp, B_TRUE,
2010c5cd6260S 		    "failed to initialize brand state change hooks");
2011c5cd6260S 		brand_close(bh);
2012c5cd6260S 		return (1);
2013c5cd6260S 	}
2014c5cd6260S 
2015123807fbSedp 	brand_close(bh);
20169acbbeafSnn35248 
20177c478bd9Sstevel@tonic-gate 	/*
20187c478bd9Sstevel@tonic-gate 	 * Check that we have all privileges.  It would be nice to pare
20197c478bd9Sstevel@tonic-gate 	 * this down, but this is at least a first cut.
20207c478bd9Sstevel@tonic-gate 	 */
20217c478bd9Sstevel@tonic-gate 	if ((privset = priv_allocset()) == NULL) {
20227c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "%s failed", "priv_allocset");
20237c478bd9Sstevel@tonic-gate 		return (1);
20247c478bd9Sstevel@tonic-gate 	}
20257c478bd9Sstevel@tonic-gate 
20267c478bd9Sstevel@tonic-gate 	if (getppriv(PRIV_EFFECTIVE, privset) != 0) {
20277c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "%s failed", "getppriv");
20287c478bd9Sstevel@tonic-gate 		priv_freeset(privset);
20297c478bd9Sstevel@tonic-gate 		return (1);
20307c478bd9Sstevel@tonic-gate 	}
20317c478bd9Sstevel@tonic-gate 
20327c478bd9Sstevel@tonic-gate 	if (priv_isfullset(privset) == B_FALSE) {
20337c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "You lack sufficient privilege to "
20343f2f09c1Sdp 		    "run this command (all privs required)");
20357c478bd9Sstevel@tonic-gate 		priv_freeset(privset);
20367c478bd9Sstevel@tonic-gate 		return (1);
20377c478bd9Sstevel@tonic-gate 	}
20387c478bd9Sstevel@tonic-gate 	priv_freeset(privset);
20397c478bd9Sstevel@tonic-gate 
20407c478bd9Sstevel@tonic-gate 	if (mkzonedir(zlogp) != 0)
20417c478bd9Sstevel@tonic-gate 		return (1);
20427c478bd9Sstevel@tonic-gate 
20437c478bd9Sstevel@tonic-gate 	/*
20447c478bd9Sstevel@tonic-gate 	 * Pre-fork: setup shared state
20457c478bd9Sstevel@tonic-gate 	 */
20467c478bd9Sstevel@tonic-gate 	if ((shstate = (void *)mmap(NULL, shstatelen,
20477c478bd9Sstevel@tonic-gate 	    PROT_READ|PROT_WRITE, MAP_SHARED|MAP_ANON, -1, (off_t)0)) ==
20487c478bd9Sstevel@tonic-gate 	    MAP_FAILED) {
20497c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "%s failed", "mmap");
20507c478bd9Sstevel@tonic-gate 		return (1);
20517c478bd9Sstevel@tonic-gate 	}
20527c478bd9Sstevel@tonic-gate 	if (sema_init(&shstate->sem, 0, USYNC_PROCESS, NULL) != 0) {
20537c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "%s failed", "sema_init()");
20547c478bd9Sstevel@tonic-gate 		(void) munmap((char *)shstate, shstatelen);
20557c478bd9Sstevel@tonic-gate 		return (1);
20567c478bd9Sstevel@tonic-gate 	}
20577c478bd9Sstevel@tonic-gate 	shstate->log.logfile = NULL;
20587c478bd9Sstevel@tonic-gate 	shstate->log.buflen = shstatelen - sizeof (*shstate);
20597c478bd9Sstevel@tonic-gate 	shstate->log.loglen = shstate->log.buflen;
20607c478bd9Sstevel@tonic-gate 	shstate->log.buf = (char *)shstate + sizeof (*shstate);
20617c478bd9Sstevel@tonic-gate 	shstate->log.log = shstate->log.buf;
20627c478bd9Sstevel@tonic-gate 	shstate->log.locale = parents_locale;
20637c478bd9Sstevel@tonic-gate 	shstate->status = -1;
20647c478bd9Sstevel@tonic-gate 
20657c478bd9Sstevel@tonic-gate 	/*
20667c478bd9Sstevel@tonic-gate 	 * We need a SIGCHLD handler so the sema_wait() below will wake
20677c478bd9Sstevel@tonic-gate 	 * up if the child dies without doing a sema_post().
20687c478bd9Sstevel@tonic-gate 	 */
20697c478bd9Sstevel@tonic-gate 	(void) sigset(SIGCHLD, sigchld);
20707c478bd9Sstevel@tonic-gate 	/*
20717c478bd9Sstevel@tonic-gate 	 * We must mask SIGCHLD until after we've coped with the fork
20727c478bd9Sstevel@tonic-gate 	 * sufficiently to deal with it; otherwise we can race and
20737c478bd9Sstevel@tonic-gate 	 * receive the signal before pid has been initialized
20747c478bd9Sstevel@tonic-gate 	 * (yes, this really happens).
20757c478bd9Sstevel@tonic-gate 	 */
20767c478bd9Sstevel@tonic-gate 	(void) sigemptyset(&block_cld);
20777c478bd9Sstevel@tonic-gate 	(void) sigaddset(&block_cld, SIGCHLD);
20787c478bd9Sstevel@tonic-gate 	(void) sigprocmask(SIG_BLOCK, &block_cld, NULL);
20797c478bd9Sstevel@tonic-gate 
2080056d3a7dSJerry Jelinek 	/*
2081056d3a7dSJerry Jelinek 	 * The parent only needs stderr after the fork, so close other fd's
2082056d3a7dSJerry Jelinek 	 * that we inherited from zoneadm so that the parent doesn't have those
2083056d3a7dSJerry Jelinek 	 * open while waiting. The child will close the rest after the fork.
2084056d3a7dSJerry Jelinek 	 */
2085056d3a7dSJerry Jelinek 	closefrom(3);
2086056d3a7dSJerry Jelinek 
20875cb4571dSdp 	if ((ctfd = init_template()) == -1) {
20885cb4571dSdp 		zerror(zlogp, B_TRUE, "failed to create contract");
20895cb4571dSdp 		return (1);
20905cb4571dSdp 	}
20915cb4571dSdp 
20927c478bd9Sstevel@tonic-gate 	/*
20937c478bd9Sstevel@tonic-gate 	 * Do not let another thread localize a message while we are forking.
20947c478bd9Sstevel@tonic-gate 	 */
20957c478bd9Sstevel@tonic-gate 	(void) mutex_lock(&msglock);
20967c478bd9Sstevel@tonic-gate 	pid = fork();
20977c478bd9Sstevel@tonic-gate 	(void) mutex_unlock(&msglock);
20985cb4571dSdp 
20995cb4571dSdp 	/*
21005cb4571dSdp 	 * In all cases (parent, child, and in the event of an error) we
21015cb4571dSdp 	 * don't want to cause creation of contracts on subsequent fork()s.
21025cb4571dSdp 	 */
21035cb4571dSdp 	(void) ct_tmpl_clear(ctfd);
21045cb4571dSdp 	(void) close(ctfd);
21055cb4571dSdp 
21067c478bd9Sstevel@tonic-gate 	if (pid == -1) {
21077c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "could not fork");
21087c478bd9Sstevel@tonic-gate 		return (1);
21097c478bd9Sstevel@tonic-gate 
21107c478bd9Sstevel@tonic-gate 	} else if (pid > 0) { /* parent */
21117c478bd9Sstevel@tonic-gate 		(void) sigprocmask(SIG_UNBLOCK, &block_cld, NULL);
21127c478bd9Sstevel@tonic-gate 		/*
21137c478bd9Sstevel@tonic-gate 		 * This marks a window of vulnerability in which we receive
21147c478bd9Sstevel@tonic-gate 		 * the SIGCLD before falling into sema_wait (normally we would
21157c478bd9Sstevel@tonic-gate 		 * get woken up from sema_wait with EINTR upon receipt of
21167c478bd9Sstevel@tonic-gate 		 * SIGCLD).  So we may need to use some other scheme like
21177c478bd9Sstevel@tonic-gate 		 * sema_posting in the sigcld handler.
21187c478bd9Sstevel@tonic-gate 		 * blech
21197c478bd9Sstevel@tonic-gate 		 */
21207c478bd9Sstevel@tonic-gate 		(void) sema_wait(&shstate->sem);
21217c478bd9Sstevel@tonic-gate 		(void) sema_destroy(&shstate->sem);
21227c478bd9Sstevel@tonic-gate 		if (shstate->status != 0)
21237c478bd9Sstevel@tonic-gate 			(void) waitpid(pid, NULL, WNOHANG);
21247c478bd9Sstevel@tonic-gate 		/*
21257c478bd9Sstevel@tonic-gate 		 * It's ok if we die with SIGPIPE.  It's not like we could have
21267c478bd9Sstevel@tonic-gate 		 * done anything about it.
21277c478bd9Sstevel@tonic-gate 		 */
21287c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "%s", shstate->log.buf);
21297c478bd9Sstevel@tonic-gate 		_exit(shstate->status == 0 ? 0 : 1);
21307c478bd9Sstevel@tonic-gate 	}
21317c478bd9Sstevel@tonic-gate 
21327c478bd9Sstevel@tonic-gate 	/*
21337c478bd9Sstevel@tonic-gate 	 * The child charges on.
21347c478bd9Sstevel@tonic-gate 	 */
21357c478bd9Sstevel@tonic-gate 	(void) sigset(SIGCHLD, SIG_DFL);
21367c478bd9Sstevel@tonic-gate 	(void) sigprocmask(SIG_UNBLOCK, &block_cld, NULL);
21377c478bd9Sstevel@tonic-gate 
21387c478bd9Sstevel@tonic-gate 	/*
21397c478bd9Sstevel@tonic-gate 	 * SIGPIPE can be delivered if we write to a socket for which the
21407c478bd9Sstevel@tonic-gate 	 * peer endpoint is gone.  That can lead to too-early termination
21417c478bd9Sstevel@tonic-gate 	 * of zoneadmd, and that's not good eats.
21427c478bd9Sstevel@tonic-gate 	 */
21437c478bd9Sstevel@tonic-gate 	(void) sigset(SIGPIPE, SIG_IGN);
21447c478bd9Sstevel@tonic-gate 	/*
21457c478bd9Sstevel@tonic-gate 	 * Stop using stderr
21467c478bd9Sstevel@tonic-gate 	 */
21477c478bd9Sstevel@tonic-gate 	zlogp = &shstate->log;
21487c478bd9Sstevel@tonic-gate 
21497c478bd9Sstevel@tonic-gate 	/*
21507c478bd9Sstevel@tonic-gate 	 * We don't need stdout/stderr from now on.
21517c478bd9Sstevel@tonic-gate 	 */
21527c478bd9Sstevel@tonic-gate 	closefrom(0);
21537c478bd9Sstevel@tonic-gate 
21547c478bd9Sstevel@tonic-gate 	/*
21557c478bd9Sstevel@tonic-gate 	 * Initialize the syslog zlog_t.  This needs to be done after
21567c478bd9Sstevel@tonic-gate 	 * the call to closefrom().
21577c478bd9Sstevel@tonic-gate 	 */
21587c478bd9Sstevel@tonic-gate 	logsys.buf = logsys.log = NULL;
21597c478bd9Sstevel@tonic-gate 	logsys.buflen = logsys.loglen = 0;
21607c478bd9Sstevel@tonic-gate 	logsys.logfile = NULL;
21617c478bd9Sstevel@tonic-gate 	logsys.locale = DEFAULT_LOCALE;
21627c478bd9Sstevel@tonic-gate 
21637c478bd9Sstevel@tonic-gate 	openlog("zoneadmd", LOG_PID, LOG_DAEMON);
21647c478bd9Sstevel@tonic-gate 
21657c478bd9Sstevel@tonic-gate 	/*
21667c478bd9Sstevel@tonic-gate 	 * The eventstream is used to publish state changes in the zone
21677c478bd9Sstevel@tonic-gate 	 * from the door threads to the console I/O poller.
21687c478bd9Sstevel@tonic-gate 	 */
21697c478bd9Sstevel@tonic-gate 	if (eventstream_init() == -1) {
21707c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "unable to create eventstream");
21717c478bd9Sstevel@tonic-gate 		goto child_out;
21727c478bd9Sstevel@tonic-gate 	}
21737c478bd9Sstevel@tonic-gate 
21747c478bd9Sstevel@tonic-gate 	(void) snprintf(zone_door_path, sizeof (zone_door_path),
2175108322fbScarlsonj 	    "%s" ZONE_DOOR_PATH, zonecfg_get_root(), zone_name);
21767c478bd9Sstevel@tonic-gate 
21777c478bd9Sstevel@tonic-gate 	/*
21787c478bd9Sstevel@tonic-gate 	 * See if another zoneadmd is running for this zone.  If not, then we
21797c478bd9Sstevel@tonic-gate 	 * can now modify system state.
21807c478bd9Sstevel@tonic-gate 	 */
21817c478bd9Sstevel@tonic-gate 	if (make_daemon_exclusive(zlogp) == -1)
21827c478bd9Sstevel@tonic-gate 		goto child_out;
21837c478bd9Sstevel@tonic-gate 
21847c478bd9Sstevel@tonic-gate 
21857c478bd9Sstevel@tonic-gate 	/*
21867c478bd9Sstevel@tonic-gate 	 * Create/join a new session; we need to be careful of what we do with
21877c478bd9Sstevel@tonic-gate 	 * the console from now on so we don't end up being the session leader
21887c478bd9Sstevel@tonic-gate 	 * for the terminal we're going to be handing out.
21897c478bd9Sstevel@tonic-gate 	 */
21907c478bd9Sstevel@tonic-gate 	(void) setsid();
21917c478bd9Sstevel@tonic-gate 
21927c478bd9Sstevel@tonic-gate 	/*
21937c478bd9Sstevel@tonic-gate 	 * This thread shouldn't be receiving any signals; in particular,
21947c478bd9Sstevel@tonic-gate 	 * SIGCHLD should be received by the thread doing the fork().
21957c478bd9Sstevel@tonic-gate 	 */
21967c478bd9Sstevel@tonic-gate 	(void) sigfillset(&blockset);
21977c478bd9Sstevel@tonic-gate 	(void) thr_sigsetmask(SIG_BLOCK, &blockset, NULL);
21987c478bd9Sstevel@tonic-gate 
21997c478bd9Sstevel@tonic-gate 	/*
22007c478bd9Sstevel@tonic-gate 	 * Setup the console device and get ready to serve the console;
22017c478bd9Sstevel@tonic-gate 	 * once this has completed, we're ready to let console clients
22027c478bd9Sstevel@tonic-gate 	 * make an attempt to connect (they will block until
22037c478bd9Sstevel@tonic-gate 	 * serve_console_sock() below gets called, and any pending
22047c478bd9Sstevel@tonic-gate 	 * connection is accept()ed).
22057c478bd9Sstevel@tonic-gate 	 */
22069d5056eaSjv227347 	if (!zonecfg_in_alt_root() && init_console(zlogp) < 0)
22077c478bd9Sstevel@tonic-gate 		goto child_out;
22087c478bd9Sstevel@tonic-gate 
22097c478bd9Sstevel@tonic-gate 	/*
22107c478bd9Sstevel@tonic-gate 	 * Take the lock now, so that when the door server gets going, we
22117c478bd9Sstevel@tonic-gate 	 * are guaranteed that it won't take a request until we are sure
22127c478bd9Sstevel@tonic-gate 	 * that everything is completely set up.  See the child_out: label
22137c478bd9Sstevel@tonic-gate 	 * below to see why this matters.
22147c478bd9Sstevel@tonic-gate 	 */
22157c478bd9Sstevel@tonic-gate 	(void) mutex_lock(&lock);
22167c478bd9Sstevel@tonic-gate 
2217108322fbScarlsonj 	/* Init semaphore for scratch zones. */
2218108322fbScarlsonj 	if (sema_init(&scratch_sem, 0, USYNC_THREAD, NULL) == -1) {
2219108322fbScarlsonj 		zerror(zlogp, B_TRUE,
2220108322fbScarlsonj 		    "failed to initialize semaphore for scratch zone");
2221108322fbScarlsonj 		goto child_out;
2222108322fbScarlsonj 	}
2223108322fbScarlsonj 
22244ac67f02SAnurag S. Maskey 	/* open the dladm handle */
22254ac67f02SAnurag S. Maskey 	if (dladm_open(&dld_handle) != DLADM_STATUS_OK) {
22264ac67f02SAnurag S. Maskey 		zerror(zlogp, B_FALSE, "failed to open dladm handle");
22274ac67f02SAnurag S. Maskey 		goto child_out;
22284ac67f02SAnurag S. Maskey 	}
22294ac67f02SAnurag S. Maskey 
22307c478bd9Sstevel@tonic-gate 	/*
22317c478bd9Sstevel@tonic-gate 	 * Note: door setup must occur *after* the console is setup.
22327c478bd9Sstevel@tonic-gate 	 * This is so that as zlogin tests the door to see if zoneadmd
22337c478bd9Sstevel@tonic-gate 	 * is ready yet, we know that the console will get serviced
22347c478bd9Sstevel@tonic-gate 	 * once door_info() indicates that the door is "up".
22357c478bd9Sstevel@tonic-gate 	 */
22367c478bd9Sstevel@tonic-gate 	if (setup_door(zlogp) == -1)
22377c478bd9Sstevel@tonic-gate 		goto child_out;
22387c478bd9Sstevel@tonic-gate 
22397c478bd9Sstevel@tonic-gate 	/*
22407c478bd9Sstevel@tonic-gate 	 * Things seem OK so far; tell the parent process that we're done
22417c478bd9Sstevel@tonic-gate 	 * with setup tasks.  This will cause the parent to exit, signalling
22427c478bd9Sstevel@tonic-gate 	 * to zoneadm, zlogin, or whatever forked it that we are ready to
22437c478bd9Sstevel@tonic-gate 	 * service requests.
22447c478bd9Sstevel@tonic-gate 	 */
22457c478bd9Sstevel@tonic-gate 	shstate->status = 0;
22467c478bd9Sstevel@tonic-gate 	(void) sema_post(&shstate->sem);
22477c478bd9Sstevel@tonic-gate 	(void) munmap((char *)shstate, shstatelen);
22487c478bd9Sstevel@tonic-gate 	shstate = NULL;
22497c478bd9Sstevel@tonic-gate 
22507c478bd9Sstevel@tonic-gate 	(void) mutex_unlock(&lock);
22517c478bd9Sstevel@tonic-gate 
22527c478bd9Sstevel@tonic-gate 	/*
22537c478bd9Sstevel@tonic-gate 	 * zlogp is now invalid, so reset it to the syslog logger.
22547c478bd9Sstevel@tonic-gate 	 */
22557c478bd9Sstevel@tonic-gate 	zlogp = &logsys;
22567c478bd9Sstevel@tonic-gate 
22577c478bd9Sstevel@tonic-gate 	/*
22587c478bd9Sstevel@tonic-gate 	 * Now that we are free of any parents, switch to the default locale.
22597c478bd9Sstevel@tonic-gate 	 */
22607c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, DEFAULT_LOCALE);
22617c478bd9Sstevel@tonic-gate 
22627c478bd9Sstevel@tonic-gate 	/*
22637c478bd9Sstevel@tonic-gate 	 * At this point the setup portion of main() is basically done, so
22647c478bd9Sstevel@tonic-gate 	 * we reuse this thread to manage the zone console.  When
22657c478bd9Sstevel@tonic-gate 	 * serve_console() has returned, we are past the point of no return
22667c478bd9Sstevel@tonic-gate 	 * in the life of this zoneadmd.
22677c478bd9Sstevel@tonic-gate 	 */
2268108322fbScarlsonj 	if (zonecfg_in_alt_root()) {
2269108322fbScarlsonj 		/*
2270108322fbScarlsonj 		 * This is just awful, but mounted scratch zones don't (and
2271108322fbScarlsonj 		 * can't) have consoles.  We just wait for unmount instead.
2272108322fbScarlsonj 		 */
2273108322fbScarlsonj 		while (sema_wait(&scratch_sem) == EINTR)
2274108322fbScarlsonj 			;
2275108322fbScarlsonj 	} else {
22767c478bd9Sstevel@tonic-gate 		serve_console(zlogp);
22777c478bd9Sstevel@tonic-gate 		assert(in_death_throes);
2278108322fbScarlsonj 	}
22797c478bd9Sstevel@tonic-gate 
22807c478bd9Sstevel@tonic-gate 	/*
22817c478bd9Sstevel@tonic-gate 	 * This is the next-to-last part of the exit interlock.  Upon calling
22827c478bd9Sstevel@tonic-gate 	 * fdetach(), the door will go unreferenced; once any
22837c478bd9Sstevel@tonic-gate 	 * outstanding requests (like the door thread doing Z_HALT) are
22847c478bd9Sstevel@tonic-gate 	 * done, the door will get an UNREF notification; when it handles
2285f594b34cSEdward Pilatowicz 	 * the UNREF, the door server will cause the exit.  It's possible
2286f594b34cSEdward Pilatowicz 	 * that fdetach() can fail because the file is in use, in which
2287f594b34cSEdward Pilatowicz 	 * case we'll retry the operation.
22887c478bd9Sstevel@tonic-gate 	 */
22897c478bd9Sstevel@tonic-gate 	assert(!MUTEX_HELD(&lock));
2290f594b34cSEdward Pilatowicz 	for (;;) {
2291f594b34cSEdward Pilatowicz 		if ((fdetach(zone_door_path) == 0) || (errno != EBUSY))
2292f594b34cSEdward Pilatowicz 			break;
2293f594b34cSEdward Pilatowicz 		yield();
2294f594b34cSEdward Pilatowicz 	}
22954ac67f02SAnurag S. Maskey 
22967c478bd9Sstevel@tonic-gate 	for (;;)
22977c478bd9Sstevel@tonic-gate 		(void) pause();
22987c478bd9Sstevel@tonic-gate 
22997c478bd9Sstevel@tonic-gate child_out:
23007c478bd9Sstevel@tonic-gate 	assert(pid == 0);
2301933ae53fSJohn Levon 
23027c478bd9Sstevel@tonic-gate 	shstate->status = -1;
23037c478bd9Sstevel@tonic-gate 	(void) sema_post(&shstate->sem);
23047c478bd9Sstevel@tonic-gate 	(void) munmap((char *)shstate, shstatelen);
23057c478bd9Sstevel@tonic-gate 
23067c478bd9Sstevel@tonic-gate 	/*
23077c478bd9Sstevel@tonic-gate 	 * This might trigger an unref notification, but if so,
23087c478bd9Sstevel@tonic-gate 	 * we are still holding the lock, so our call to exit will
23097c478bd9Sstevel@tonic-gate 	 * ultimately win the race and will publish the right exit
23107c478bd9Sstevel@tonic-gate 	 * code.
23117c478bd9Sstevel@tonic-gate 	 */
23127c478bd9Sstevel@tonic-gate 	if (zone_door != -1) {
23137c478bd9Sstevel@tonic-gate 		assert(MUTEX_HELD(&lock));
23147c478bd9Sstevel@tonic-gate 		(void) door_revoke(zone_door);
23157c478bd9Sstevel@tonic-gate 		(void) fdetach(zone_door_path);
23167c478bd9Sstevel@tonic-gate 	}
23174ac67f02SAnurag S. Maskey 
23184ac67f02SAnurag S. Maskey 	if (dld_handle != NULL)
23194ac67f02SAnurag S. Maskey 		dladm_close(dld_handle);
23204ac67f02SAnurag S. Maskey 
23217c478bd9Sstevel@tonic-gate 	return (1); /* return from main() forcibly exits an MT process */
23227c478bd9Sstevel@tonic-gate }
2323