10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
51645Scomay * Common Development and Distribution License (the "License").
61645Scomay * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate * See the License for the specific language governing permissions
110Sstevel@tonic-gate * and limitations under the License.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate *
190Sstevel@tonic-gate * CDDL HEADER END
200Sstevel@tonic-gate */
211645Scomay
220Sstevel@tonic-gate /*
23*13122SStephen.Lawrence@oracle.COM * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
240Sstevel@tonic-gate */
250Sstevel@tonic-gate
260Sstevel@tonic-gate /*
270Sstevel@tonic-gate * zoneadmd manages zones; one zoneadmd process is launched for each
280Sstevel@tonic-gate * non-global zone on the system. This daemon juggles four jobs:
290Sstevel@tonic-gate *
300Sstevel@tonic-gate * - Implement setup and teardown of the zone "virtual platform": mount and
310Sstevel@tonic-gate * unmount filesystems; create and destroy network interfaces; communicate
320Sstevel@tonic-gate * with devfsadmd to lay out devices for the zone; instantiate the zone
330Sstevel@tonic-gate * console device; configure process runtime attributes such as resource
340Sstevel@tonic-gate * controls, pool bindings, fine-grained privileges.
350Sstevel@tonic-gate *
360Sstevel@tonic-gate * - Launch the zone's init(1M) process.
370Sstevel@tonic-gate *
380Sstevel@tonic-gate * - Implement a door server; clients (like zoneadm) connect to the door
390Sstevel@tonic-gate * server and request zone state changes. The kernel is also a client of
400Sstevel@tonic-gate * this door server. A request to halt or reboot the zone which originates
410Sstevel@tonic-gate * *inside* the zone results in a door upcall from the kernel into zoneadmd.
420Sstevel@tonic-gate *
430Sstevel@tonic-gate * One minor problem is that messages emitted by zoneadmd need to be passed
440Sstevel@tonic-gate * back to the zoneadm process making the request. These messages need to
450Sstevel@tonic-gate * be rendered in the client's locale; so, this is passed in as part of the
460Sstevel@tonic-gate * request. The exception is the kernel upcall to zoneadmd, in which case
470Sstevel@tonic-gate * messages are syslog'd.
480Sstevel@tonic-gate *
490Sstevel@tonic-gate * To make all of this work, the Makefile adds -a to xgettext to extract *all*
500Sstevel@tonic-gate * strings, and an exclusion file (zoneadmd.xcl) is used to exclude those
510Sstevel@tonic-gate * strings which do not need to be translated.
520Sstevel@tonic-gate *
530Sstevel@tonic-gate * - Act as a console server for zlogin -C processes; see comments in zcons.c
540Sstevel@tonic-gate * for more information about the zone console architecture.
550Sstevel@tonic-gate *
560Sstevel@tonic-gate * DESIGN NOTES
570Sstevel@tonic-gate *
580Sstevel@tonic-gate * Restart:
590Sstevel@tonic-gate * A chief design constraint of zoneadmd is that it should be restartable in
600Sstevel@tonic-gate * the case that the administrator kills it off, or it suffers a fatal error,
610Sstevel@tonic-gate * without the running zone being impacted; this is akin to being able to
620Sstevel@tonic-gate * reboot the service processor of a server without affecting the OS instance.
630Sstevel@tonic-gate */
640Sstevel@tonic-gate
650Sstevel@tonic-gate #include <sys/param.h>
660Sstevel@tonic-gate #include <sys/mman.h>
670Sstevel@tonic-gate #include <sys/types.h>
680Sstevel@tonic-gate #include <sys/stat.h>
690Sstevel@tonic-gate #include <sys/sysmacros.h>
700Sstevel@tonic-gate
710Sstevel@tonic-gate #include <bsm/adt.h>
720Sstevel@tonic-gate #include <bsm/adt_event.h>
730Sstevel@tonic-gate
740Sstevel@tonic-gate #include <alloca.h>
750Sstevel@tonic-gate #include <assert.h>
760Sstevel@tonic-gate #include <errno.h>
770Sstevel@tonic-gate #include <door.h>
780Sstevel@tonic-gate #include <fcntl.h>
790Sstevel@tonic-gate #include <locale.h>
800Sstevel@tonic-gate #include <signal.h>
810Sstevel@tonic-gate #include <stdarg.h>
820Sstevel@tonic-gate #include <stdio.h>
830Sstevel@tonic-gate #include <stdlib.h>
840Sstevel@tonic-gate #include <string.h>
850Sstevel@tonic-gate #include <strings.h>
860Sstevel@tonic-gate #include <synch.h>
870Sstevel@tonic-gate #include <syslog.h>
880Sstevel@tonic-gate #include <thread.h>
890Sstevel@tonic-gate #include <unistd.h>
900Sstevel@tonic-gate #include <wait.h>
910Sstevel@tonic-gate #include <limits.h>
920Sstevel@tonic-gate #include <zone.h>
932712Snn35248 #include <libbrand.h>
947655Sgerald.jelinek@sun.com #include <sys/brand.h>
950Sstevel@tonic-gate #include <libcontract.h>
960Sstevel@tonic-gate #include <libcontract_priv.h>
9710796SStephen.Lawrence@Sun.COM #include <sys/brand.h>
980Sstevel@tonic-gate #include <sys/contract/process.h>
990Sstevel@tonic-gate #include <sys/ctfs.h>
10010616SSebastien.Roy@Sun.COM #include <libdladm.h>
10110616SSebastien.Roy@Sun.COM #include <sys/dls_mgmt.h>
1020Sstevel@tonic-gate
1030Sstevel@tonic-gate #include <libzonecfg.h>
104*13122SStephen.Lawrence@oracle.COM #include <zonestat_impl.h>
1050Sstevel@tonic-gate #include "zoneadmd.h"
1060Sstevel@tonic-gate
1070Sstevel@tonic-gate static char *progname;
1080Sstevel@tonic-gate char *zone_name; /* zone which we are managing */
109*13122SStephen.Lawrence@oracle.COM char pool_name[MAXNAMELEN];
11010943SEdward.Pilatowicz@Sun.COM char default_brand[MAXNAMELEN];
1112712Snn35248 char brand_name[MAXNAMELEN];
1122712Snn35248 boolean_t zone_isnative;
1134350Std153743 boolean_t zone_iscluster;
1148905SRic.Aleshire@Sun.COM boolean_t zone_islabeled;
115766Scarlsonj static zoneid_t zone_id;
1168453SAnurag.Maskey@Sun.COM dladm_handle_t dld_handle = NULL;
1170Sstevel@tonic-gate
1187370S<Gerald Jelinek> static char pre_statechg_hook[2 * MAXPATHLEN];
1197370S<Gerald Jelinek> static char post_statechg_hook[2 * MAXPATHLEN];
1207370S<Gerald Jelinek> char query_hook[2 * MAXPATHLEN];
1217370S<Gerald Jelinek>
1222611Svp157776 zlog_t logsys;
1230Sstevel@tonic-gate
1240Sstevel@tonic-gate mutex_t lock = DEFAULTMUTEX; /* to serialize stuff */
1250Sstevel@tonic-gate mutex_t msglock = DEFAULTMUTEX; /* for calling setlocale() */
1260Sstevel@tonic-gate
127766Scarlsonj static sema_t scratch_sem; /* for scratch zones */
128766Scarlsonj
1290Sstevel@tonic-gate static char zone_door_path[MAXPATHLEN];
1300Sstevel@tonic-gate static int zone_door = -1;
1310Sstevel@tonic-gate
1320Sstevel@tonic-gate boolean_t in_death_throes = B_FALSE; /* daemon is dying */
1330Sstevel@tonic-gate boolean_t bringup_failure_recovery = B_FALSE; /* ignore certain failures */
1340Sstevel@tonic-gate
1350Sstevel@tonic-gate #if !defined(TEXT_DOMAIN) /* should be defined by cc -D */
1360Sstevel@tonic-gate #define TEXT_DOMAIN "SYS_TEST" /* Use this only if it wasn't */
1370Sstevel@tonic-gate #endif
1380Sstevel@tonic-gate
1390Sstevel@tonic-gate #define DEFAULT_LOCALE "C"
1400Sstevel@tonic-gate
141766Scarlsonj static const char *
z_cmd_name(zone_cmd_t zcmd)142766Scarlsonj z_cmd_name(zone_cmd_t zcmd)
143766Scarlsonj {
144766Scarlsonj /* This list needs to match the enum in sys/zone.h */
145766Scarlsonj static const char *zcmdstr[] = {
1462712Snn35248 "ready", "boot", "forceboot", "reboot", "halt",
1472712Snn35248 "note_uninstalling", "mount", "forcemount", "unmount"
148766Scarlsonj };
149766Scarlsonj
150766Scarlsonj if (zcmd >= sizeof (zcmdstr) / sizeof (*zcmdstr))
151766Scarlsonj return ("unknown");
152766Scarlsonj else
153766Scarlsonj return (zcmdstr[(int)zcmd]);
154766Scarlsonj }
155766Scarlsonj
1560Sstevel@tonic-gate static char *
get_execbasename(char * execfullname)1570Sstevel@tonic-gate get_execbasename(char *execfullname)
1580Sstevel@tonic-gate {
1590Sstevel@tonic-gate char *last_slash, *execbasename;
1600Sstevel@tonic-gate
1610Sstevel@tonic-gate /* guard against '/' at end of command invocation */
1620Sstevel@tonic-gate for (;;) {
1630Sstevel@tonic-gate last_slash = strrchr(execfullname, '/');
1640Sstevel@tonic-gate if (last_slash == NULL) {
1650Sstevel@tonic-gate execbasename = execfullname;
1660Sstevel@tonic-gate break;
1670Sstevel@tonic-gate } else {
1680Sstevel@tonic-gate execbasename = last_slash + 1;
1690Sstevel@tonic-gate if (*execbasename == '\0') {
1700Sstevel@tonic-gate *last_slash = '\0';
1710Sstevel@tonic-gate continue;
1720Sstevel@tonic-gate }
1730Sstevel@tonic-gate break;
1740Sstevel@tonic-gate }
1750Sstevel@tonic-gate }
1760Sstevel@tonic-gate return (execbasename);
1770Sstevel@tonic-gate }
1780Sstevel@tonic-gate
1790Sstevel@tonic-gate static void
usage(void)1800Sstevel@tonic-gate usage(void)
1810Sstevel@tonic-gate {
1820Sstevel@tonic-gate (void) fprintf(stderr, gettext("Usage: %s -z zonename\n"), progname);
1830Sstevel@tonic-gate (void) fprintf(stderr,
1840Sstevel@tonic-gate gettext("\tNote: %s should not be run directly.\n"), progname);
1850Sstevel@tonic-gate exit(2);
1860Sstevel@tonic-gate }
1870Sstevel@tonic-gate
1880Sstevel@tonic-gate /* ARGSUSED */
1890Sstevel@tonic-gate static void
sigchld(int sig)1900Sstevel@tonic-gate sigchld(int sig)
1910Sstevel@tonic-gate {
1920Sstevel@tonic-gate }
1930Sstevel@tonic-gate
1940Sstevel@tonic-gate char *
localize_msg(char * locale,const char * msg)1950Sstevel@tonic-gate localize_msg(char *locale, const char *msg)
1960Sstevel@tonic-gate {
1970Sstevel@tonic-gate char *out;
1980Sstevel@tonic-gate
1990Sstevel@tonic-gate (void) mutex_lock(&msglock);
2000Sstevel@tonic-gate (void) setlocale(LC_MESSAGES, locale);
2010Sstevel@tonic-gate out = gettext(msg);
2020Sstevel@tonic-gate (void) setlocale(LC_MESSAGES, DEFAULT_LOCALE);
2030Sstevel@tonic-gate (void) mutex_unlock(&msglock);
2040Sstevel@tonic-gate return (out);
2050Sstevel@tonic-gate }
2060Sstevel@tonic-gate
2070Sstevel@tonic-gate /* PRINTFLIKE3 */
2080Sstevel@tonic-gate void
zerror(zlog_t * zlogp,boolean_t use_strerror,const char * fmt,...)2090Sstevel@tonic-gate zerror(zlog_t *zlogp, boolean_t use_strerror, const char *fmt, ...)
2100Sstevel@tonic-gate {
2110Sstevel@tonic-gate va_list alist;
2120Sstevel@tonic-gate char buf[MAXPATHLEN * 2]; /* enough space for err msg with a path */
2130Sstevel@tonic-gate char *bp;
2140Sstevel@tonic-gate int saved_errno = errno;
2150Sstevel@tonic-gate
2160Sstevel@tonic-gate if (zlogp == NULL)
2170Sstevel@tonic-gate return;
2180Sstevel@tonic-gate if (zlogp == &logsys)
2190Sstevel@tonic-gate (void) snprintf(buf, sizeof (buf), "[zone '%s'] ",
2200Sstevel@tonic-gate zone_name);
2210Sstevel@tonic-gate else
2220Sstevel@tonic-gate buf[0] = '\0';
2230Sstevel@tonic-gate bp = &(buf[strlen(buf)]);
2240Sstevel@tonic-gate
2250Sstevel@tonic-gate /*
2260Sstevel@tonic-gate * In theory, the locale pointer should be set to either "C" or a
2270Sstevel@tonic-gate * char array, so it should never be NULL
2280Sstevel@tonic-gate */
2290Sstevel@tonic-gate assert(zlogp->locale != NULL);
2300Sstevel@tonic-gate /* Locale is per process, but we are multi-threaded... */
2310Sstevel@tonic-gate fmt = localize_msg(zlogp->locale, fmt);
2320Sstevel@tonic-gate
2330Sstevel@tonic-gate va_start(alist, fmt);
2340Sstevel@tonic-gate (void) vsnprintf(bp, sizeof (buf) - (bp - buf), fmt, alist);
2350Sstevel@tonic-gate va_end(alist);
2360Sstevel@tonic-gate bp = &(buf[strlen(buf)]);
2370Sstevel@tonic-gate if (use_strerror)
2380Sstevel@tonic-gate (void) snprintf(bp, sizeof (buf) - (bp - buf), ": %s",
2390Sstevel@tonic-gate strerror(saved_errno));
2400Sstevel@tonic-gate if (zlogp == &logsys) {
2410Sstevel@tonic-gate (void) syslog(LOG_ERR, "%s", buf);
2420Sstevel@tonic-gate } else if (zlogp->logfile != NULL) {
2430Sstevel@tonic-gate (void) fprintf(zlogp->logfile, "%s\n", buf);
2440Sstevel@tonic-gate } else {
2450Sstevel@tonic-gate size_t buflen;
2460Sstevel@tonic-gate size_t copylen;
2470Sstevel@tonic-gate
2480Sstevel@tonic-gate buflen = snprintf(zlogp->log, zlogp->loglen, "%s\n", buf);
2490Sstevel@tonic-gate copylen = MIN(buflen, zlogp->loglen);
2500Sstevel@tonic-gate zlogp->log += copylen;
2510Sstevel@tonic-gate zlogp->loglen -= copylen;
2520Sstevel@tonic-gate }
2530Sstevel@tonic-gate }
2540Sstevel@tonic-gate
2552267Sdp /*
2562267Sdp * Emit a warning for any boot arguments which are unrecognized. Since
2572267Sdp * Solaris boot arguments are getopt(3c) compatible (see kernel(1m)), we
2582267Sdp * put the arguments into an argv style array, use getopt to process them,
2592267Sdp * and put the resultant argument string back into outargs.
2602267Sdp *
2612267Sdp * During the filtering, we pull out any arguments which are truly "boot"
2622267Sdp * arguments, leaving only those which are to be passed intact to the
2632267Sdp * progenitor process. The one we support at the moment is -i, which
2642267Sdp * indicates to the kernel which program should be launched as 'init'.
2652267Sdp *
2662267Sdp * A return of Z_INVAL indicates specifically that the arguments are
2672267Sdp * not valid; this is a non-fatal error. Except for Z_OK, all other return
2682267Sdp * values are treated as fatal.
2692267Sdp */
2702267Sdp static int
filter_bootargs(zlog_t * zlogp,const char * inargs,char * outargs,char * init_file,char * badarg)2712267Sdp filter_bootargs(zlog_t *zlogp, const char *inargs, char *outargs,
2722267Sdp char *init_file, char *badarg)
2732267Sdp {
2742267Sdp int argc = 0, argc_save;
2752267Sdp int i;
2762267Sdp int err;
2772267Sdp char *arg, *lasts, **argv = NULL, **argv_save;
2782267Sdp char zonecfg_args[BOOTARGS_MAX];
2792267Sdp char scratchargs[BOOTARGS_MAX], *sargs;
2802267Sdp char c;
2812267Sdp
2822267Sdp bzero(outargs, BOOTARGS_MAX);
2832267Sdp bzero(badarg, BOOTARGS_MAX);
2842267Sdp
2852267Sdp /*
2862267Sdp * If the user didn't specify transient boot arguments, check
2872267Sdp * to see if there were any specified in the zone configuration,
2882267Sdp * and use them if applicable.
2892267Sdp */
2902267Sdp if (inargs == NULL || inargs[0] == '\0') {
2912267Sdp zone_dochandle_t handle;
2922267Sdp if ((handle = zonecfg_init_handle()) == NULL) {
2932267Sdp zerror(zlogp, B_TRUE,
2942267Sdp "getting zone configuration handle");
2952267Sdp return (Z_BAD_HANDLE);
2962267Sdp }
2972267Sdp err = zonecfg_get_snapshot_handle(zone_name, handle);
2982267Sdp if (err != Z_OK) {
2992267Sdp zerror(zlogp, B_FALSE,
3002267Sdp "invalid configuration snapshot");
3012267Sdp zonecfg_fini_handle(handle);
3022267Sdp return (Z_BAD_HANDLE);
3032267Sdp }
3042267Sdp
3052267Sdp bzero(zonecfg_args, sizeof (zonecfg_args));
3062267Sdp (void) zonecfg_get_bootargs(handle, zonecfg_args,
3072267Sdp sizeof (zonecfg_args));
3082267Sdp inargs = zonecfg_args;
3092267Sdp zonecfg_fini_handle(handle);
3102267Sdp }
3112267Sdp
3122267Sdp if (strlen(inargs) >= BOOTARGS_MAX) {
3132267Sdp zerror(zlogp, B_FALSE, "boot argument string too long");
3142267Sdp return (Z_INVAL);
3152267Sdp }
3162267Sdp
3172267Sdp (void) strlcpy(scratchargs, inargs, sizeof (scratchargs));
3182267Sdp sargs = scratchargs;
3192267Sdp while ((arg = strtok_r(sargs, " \t", &lasts)) != NULL) {
3202267Sdp sargs = NULL;
3212267Sdp argc++;
3222267Sdp }
3232267Sdp
3242267Sdp if ((argv = calloc(argc + 1, sizeof (char *))) == NULL) {
3252267Sdp zerror(zlogp, B_FALSE, "memory allocation failed");
3262267Sdp return (Z_NOMEM);
3272267Sdp }
3282267Sdp
3292267Sdp argv_save = argv;
3302267Sdp argc_save = argc;
3312267Sdp
3322267Sdp (void) strlcpy(scratchargs, inargs, sizeof (scratchargs));
3332267Sdp sargs = scratchargs;
3342267Sdp i = 0;
3352267Sdp while ((arg = strtok_r(sargs, " \t", &lasts)) != NULL) {
3362267Sdp sargs = NULL;
3372267Sdp if ((argv[i] = strdup(arg)) == NULL) {
3382267Sdp err = Z_NOMEM;
3392267Sdp zerror(zlogp, B_FALSE, "memory allocation failed");
3402267Sdp goto done;
3412267Sdp }
3422267Sdp i++;
3432267Sdp }
3442267Sdp
3452267Sdp /*
3462267Sdp * We preserve compatibility with the Solaris system boot behavior,
3472267Sdp * which allows:
3482267Sdp *
3492267Sdp * # reboot kernel/unix -s -m verbose
3502267Sdp *
3512267Sdp * In this example, kernel/unix tells the booter what file to
3522267Sdp * boot. We don't want reboot in a zone to be gratuitously different,
3532267Sdp * so we silently ignore the boot file, if necessary.
3542267Sdp */
3552267Sdp if (argv[0] == NULL)
3562267Sdp goto done;
3572267Sdp
3582267Sdp assert(argv[0][0] != ' ');
3592267Sdp assert(argv[0][0] != '\t');
3602267Sdp
3612267Sdp if (argv[0][0] != '-' && argv[0][0] != '\0') {
3622267Sdp argv = &argv[1];
3632267Sdp argc--;
3642267Sdp }
3652267Sdp
3662267Sdp optind = 0;
3672267Sdp opterr = 0;
3682267Sdp err = Z_OK;
3692712Snn35248 while ((c = getopt(argc, argv, "fi:m:s")) != -1) {
3702267Sdp switch (c) {
3712267Sdp case 'i':
3722267Sdp /*
3732267Sdp * -i is handled by the runtime and is not passed
3742267Sdp * along to userland
3752267Sdp */
3762267Sdp (void) strlcpy(init_file, optarg, MAXPATHLEN);
3772267Sdp break;
3782712Snn35248 case 'f':
3792712Snn35248 /* This has already been processed by zoneadm */
3802712Snn35248 break;
3812267Sdp case 'm':
3822267Sdp case 's':
3832267Sdp /* These pass through unmolested */
3842267Sdp (void) snprintf(outargs, BOOTARGS_MAX,
3852267Sdp "%s -%c %s ", outargs, c, optarg ? optarg : "");
3862267Sdp break;
3872267Sdp case '?':
3882267Sdp /*
3892267Sdp * We warn about unknown arguments but pass them
3902267Sdp * along anyway-- if someone wants to develop their
3912267Sdp * own init replacement, they can pass it whatever
3922267Sdp * args they want.
3932267Sdp */
3942267Sdp err = Z_INVAL;
3952267Sdp (void) snprintf(outargs, BOOTARGS_MAX,
3962267Sdp "%s -%c", outargs, optopt);
3972267Sdp (void) snprintf(badarg, BOOTARGS_MAX,
3982267Sdp "%s -%c", badarg, optopt);
3992267Sdp break;
4002267Sdp }
4012267Sdp }
4022267Sdp
4032267Sdp /*
4042267Sdp * For Solaris Zones we warn about and discard non-option arguments.
4052267Sdp * Hence 'boot foo bar baz gub' --> 'boot'. However, to be similar
4062267Sdp * to the kernel, we concat up all the other remaining boot args.
4072267Sdp * and warn on them as a group.
4082267Sdp */
4092267Sdp if (optind < argc) {
4102267Sdp err = Z_INVAL;
4112267Sdp while (optind < argc) {
4122267Sdp (void) snprintf(badarg, BOOTARGS_MAX, "%s%s%s",
4132267Sdp badarg, strlen(badarg) > 0 ? " " : "",
4142267Sdp argv[optind]);
4152267Sdp optind++;
4162267Sdp }
4172267Sdp zerror(zlogp, B_FALSE, "WARNING: Unused or invalid boot "
4182267Sdp "arguments `%s'.", badarg);
4192267Sdp }
4202267Sdp
4212267Sdp done:
4222267Sdp for (i = 0; i < argc_save; i++) {
4232267Sdp if (argv_save[i] != NULL)
4242267Sdp free(argv_save[i]);
4252267Sdp }
4262267Sdp free(argv_save);
4272267Sdp return (err);
4282267Sdp }
4292267Sdp
4302267Sdp
4310Sstevel@tonic-gate static int
mkzonedir(zlog_t * zlogp)4320Sstevel@tonic-gate mkzonedir(zlog_t *zlogp)
4330Sstevel@tonic-gate {
4340Sstevel@tonic-gate struct stat st;
4350Sstevel@tonic-gate /*
4360Sstevel@tonic-gate * We must create and lock everyone but root out of ZONES_TMPDIR
4370Sstevel@tonic-gate * since anyone can open any UNIX domain socket, regardless of
4380Sstevel@tonic-gate * its file system permissions. Sigh...
4390Sstevel@tonic-gate */
4400Sstevel@tonic-gate if (mkdir(ZONES_TMPDIR, S_IRWXU) < 0 && errno != EEXIST) {
4410Sstevel@tonic-gate zerror(zlogp, B_TRUE, "could not mkdir '%s'", ZONES_TMPDIR);
4420Sstevel@tonic-gate return (-1);
4430Sstevel@tonic-gate }
4440Sstevel@tonic-gate /* paranoia */
445871Scasper if ((stat(ZONES_TMPDIR, &st) < 0) || !S_ISDIR(st.st_mode)) {
4460Sstevel@tonic-gate zerror(zlogp, B_TRUE, "'%s' is not a directory", ZONES_TMPDIR);
4470Sstevel@tonic-gate return (-1);
4480Sstevel@tonic-gate }
4490Sstevel@tonic-gate (void) chmod(ZONES_TMPDIR, S_IRWXU);
4500Sstevel@tonic-gate return (0);
4510Sstevel@tonic-gate }
4520Sstevel@tonic-gate
453766Scarlsonj /*
4547370S<Gerald Jelinek> * Run the brand's pre-state change callback, if it exists.
4557370S<Gerald Jelinek> */
4567370S<Gerald Jelinek> static int
brand_prestatechg(zlog_t * zlogp,int state,int cmd)4577370S<Gerald Jelinek> brand_prestatechg(zlog_t *zlogp, int state, int cmd)
4587370S<Gerald Jelinek> {
4597370S<Gerald Jelinek> char cmdbuf[2 * MAXPATHLEN];
46013117SSusan.Kamm-Worrell@Sun.COM const char *altroot;
4617370S<Gerald Jelinek>
4627370S<Gerald Jelinek> if (pre_statechg_hook[0] == '\0')
4637370S<Gerald Jelinek> return (0);
4647370S<Gerald Jelinek>
46513117SSusan.Kamm-Worrell@Sun.COM altroot = zonecfg_get_root();
46613117SSusan.Kamm-Worrell@Sun.COM if (snprintf(cmdbuf, sizeof (cmdbuf), "%s %d %d %s", pre_statechg_hook,
46713117SSusan.Kamm-Worrell@Sun.COM state, cmd, altroot) > sizeof (cmdbuf))
4687370S<Gerald Jelinek> return (-1);
4697370S<Gerald Jelinek>
4707370S<Gerald Jelinek> if (do_subproc(zlogp, cmdbuf, NULL) != 0)
4717370S<Gerald Jelinek> return (-1);
4727370S<Gerald Jelinek>
4737370S<Gerald Jelinek> return (0);
4747370S<Gerald Jelinek> }
4757370S<Gerald Jelinek>
4767370S<Gerald Jelinek> /*
4777370S<Gerald Jelinek> * Run the brand's post-state change callback, if it exists.
4787370S<Gerald Jelinek> */
4797370S<Gerald Jelinek> static int
brand_poststatechg(zlog_t * zlogp,int state,int cmd)4807370S<Gerald Jelinek> brand_poststatechg(zlog_t *zlogp, int state, int cmd)
4817370S<Gerald Jelinek> {
4827370S<Gerald Jelinek> char cmdbuf[2 * MAXPATHLEN];
48313117SSusan.Kamm-Worrell@Sun.COM const char *altroot;
4847370S<Gerald Jelinek>
4857370S<Gerald Jelinek> if (post_statechg_hook[0] == '\0')
4867370S<Gerald Jelinek> return (0);
4877370S<Gerald Jelinek>
48813117SSusan.Kamm-Worrell@Sun.COM altroot = zonecfg_get_root();
48913117SSusan.Kamm-Worrell@Sun.COM if (snprintf(cmdbuf, sizeof (cmdbuf), "%s %d %d %s", post_statechg_hook,
49013117SSusan.Kamm-Worrell@Sun.COM state, cmd, altroot) > sizeof (cmdbuf))
4917370S<Gerald Jelinek> return (-1);
4927370S<Gerald Jelinek>
4937370S<Gerald Jelinek> if (do_subproc(zlogp, cmdbuf, NULL) != 0)
4947370S<Gerald Jelinek> return (-1);
4957370S<Gerald Jelinek>
4967370S<Gerald Jelinek> return (0);
4977370S<Gerald Jelinek> }
4987370S<Gerald Jelinek>
4997370S<Gerald Jelinek> /*
500*13122SStephen.Lawrence@oracle.COM * Notify zonestatd of the new zone. If zonestatd is not running, this
501*13122SStephen.Lawrence@oracle.COM * will do nothing.
502*13122SStephen.Lawrence@oracle.COM */
503*13122SStephen.Lawrence@oracle.COM static void
notify_zonestatd(zoneid_t zoneid)504*13122SStephen.Lawrence@oracle.COM notify_zonestatd(zoneid_t zoneid)
505*13122SStephen.Lawrence@oracle.COM {
506*13122SStephen.Lawrence@oracle.COM int cmd[2];
507*13122SStephen.Lawrence@oracle.COM int fd;
508*13122SStephen.Lawrence@oracle.COM door_arg_t params;
509*13122SStephen.Lawrence@oracle.COM
510*13122SStephen.Lawrence@oracle.COM fd = open(ZS_DOOR_PATH, O_RDONLY);
511*13122SStephen.Lawrence@oracle.COM if (fd < 0)
512*13122SStephen.Lawrence@oracle.COM return;
513*13122SStephen.Lawrence@oracle.COM
514*13122SStephen.Lawrence@oracle.COM cmd[0] = ZSD_CMD_NEW_ZONE;
515*13122SStephen.Lawrence@oracle.COM cmd[1] = zoneid;
516*13122SStephen.Lawrence@oracle.COM params.data_ptr = (char *)&cmd;
517*13122SStephen.Lawrence@oracle.COM params.data_size = sizeof (cmd);
518*13122SStephen.Lawrence@oracle.COM params.desc_ptr = NULL;
519*13122SStephen.Lawrence@oracle.COM params.desc_num = 0;
520*13122SStephen.Lawrence@oracle.COM params.rbuf = NULL;
521*13122SStephen.Lawrence@oracle.COM params.rsize = NULL;
522*13122SStephen.Lawrence@oracle.COM (void) door_call(fd, ¶ms);
523*13122SStephen.Lawrence@oracle.COM (void) close(fd);
524*13122SStephen.Lawrence@oracle.COM }
525*13122SStephen.Lawrence@oracle.COM
526*13122SStephen.Lawrence@oracle.COM /*
527766Scarlsonj * Bring a zone up to the pre-boot "ready" stage. The mount_cmd argument is
528766Scarlsonj * 'true' if this is being invoked as part of the processing for the "mount"
529766Scarlsonj * subcommand.
530766Scarlsonj */
531766Scarlsonj static int
zone_ready(zlog_t * zlogp,zone_mnt_t mount_cmd,int zstate)5327370S<Gerald Jelinek> zone_ready(zlog_t *zlogp, zone_mnt_t mount_cmd, int zstate)
5330Sstevel@tonic-gate {
5340Sstevel@tonic-gate int err;
5350Sstevel@tonic-gate
5367370S<Gerald Jelinek> if (brand_prestatechg(zlogp, zstate, Z_READY) != 0)
5377370S<Gerald Jelinek> return (-1);
5387370S<Gerald Jelinek>
5390Sstevel@tonic-gate if ((err = zonecfg_create_snapshot(zone_name)) != Z_OK) {
5400Sstevel@tonic-gate zerror(zlogp, B_FALSE, "unable to create snapshot: %s",
5410Sstevel@tonic-gate zonecfg_strerror(err));
5428958Sdp@eng.sun.com goto bad;
5430Sstevel@tonic-gate }
5440Sstevel@tonic-gate
545766Scarlsonj if ((zone_id = vplat_create(zlogp, mount_cmd)) == -1) {
5460Sstevel@tonic-gate if ((err = zonecfg_destroy_snapshot(zone_name)) != Z_OK)
5470Sstevel@tonic-gate zerror(zlogp, B_FALSE, "destroying snapshot: %s",
5480Sstevel@tonic-gate zonecfg_strerror(err));
5498958Sdp@eng.sun.com goto bad;
5500Sstevel@tonic-gate }
5512303Scarlsonj if (vplat_bringup(zlogp, mount_cmd, zone_id) != 0) {
5520Sstevel@tonic-gate bringup_failure_recovery = B_TRUE;
5535829Sgjelinek (void) vplat_teardown(NULL, (mount_cmd != Z_MNT_BOOT), B_FALSE);
5540Sstevel@tonic-gate if ((err = zonecfg_destroy_snapshot(zone_name)) != Z_OK)
5550Sstevel@tonic-gate zerror(zlogp, B_FALSE, "destroying snapshot: %s",
5560Sstevel@tonic-gate zonecfg_strerror(err));
5578958Sdp@eng.sun.com goto bad;
5580Sstevel@tonic-gate }
5590Sstevel@tonic-gate
5607370S<Gerald Jelinek> if (brand_poststatechg(zlogp, zstate, Z_READY) != 0)
5618958Sdp@eng.sun.com goto bad;
5627370S<Gerald Jelinek>
5630Sstevel@tonic-gate return (0);
5648958Sdp@eng.sun.com
5658958Sdp@eng.sun.com bad:
5668958Sdp@eng.sun.com /*
5678958Sdp@eng.sun.com * If something goes wrong, we up the zones's state to the target
5688958Sdp@eng.sun.com * state, READY, and then invoke the hook as if we're halting.
5698958Sdp@eng.sun.com */
5708958Sdp@eng.sun.com (void) brand_poststatechg(zlogp, ZONE_STATE_READY, Z_HALT);
5718958Sdp@eng.sun.com return (-1);
5720Sstevel@tonic-gate }
5730Sstevel@tonic-gate
5742303Scarlsonj int
init_template(void)5752303Scarlsonj init_template(void)
5760Sstevel@tonic-gate {
5770Sstevel@tonic-gate int fd;
5780Sstevel@tonic-gate int err = 0;
5790Sstevel@tonic-gate
5800Sstevel@tonic-gate fd = open64(CTFS_ROOT "/process/template", O_RDWR);
5810Sstevel@tonic-gate if (fd == -1)
5820Sstevel@tonic-gate return (-1);
5830Sstevel@tonic-gate
5840Sstevel@tonic-gate /*
5850Sstevel@tonic-gate * For now, zoneadmd doesn't do anything with the contract.
5860Sstevel@tonic-gate * Deliver no events, don't inherit, and allow it to be orphaned.
5870Sstevel@tonic-gate */
5880Sstevel@tonic-gate err |= ct_tmpl_set_critical(fd, 0);
5890Sstevel@tonic-gate err |= ct_tmpl_set_informative(fd, 0);
5900Sstevel@tonic-gate err |= ct_pr_tmpl_set_fatal(fd, CT_PR_EV_HWERR);
5910Sstevel@tonic-gate err |= ct_pr_tmpl_set_param(fd, CT_PR_PGRPONLY | CT_PR_REGENT);
5920Sstevel@tonic-gate if (err || ct_tmpl_activate(fd)) {
5930Sstevel@tonic-gate (void) close(fd);
5940Sstevel@tonic-gate return (-1);
5950Sstevel@tonic-gate }
5960Sstevel@tonic-gate
5970Sstevel@tonic-gate return (fd);
5980Sstevel@tonic-gate }
5990Sstevel@tonic-gate
6002712Snn35248 typedef struct fs_callback {
6012712Snn35248 zlog_t *zlogp;
6022712Snn35248 zoneid_t zoneid;
6035576Sedp boolean_t mount_cmd;
6042712Snn35248 } fs_callback_t;
6052712Snn35248
6060Sstevel@tonic-gate static int
mount_early_fs(void * data,const char * spec,const char * dir,const char * fstype,const char * opt)6072712Snn35248 mount_early_fs(void *data, const char *spec, const char *dir,
6082712Snn35248 const char *fstype, const char *opt)
6090Sstevel@tonic-gate {
6102712Snn35248 zlog_t *zlogp = ((fs_callback_t *)data)->zlogp;
6112712Snn35248 zoneid_t zoneid = ((fs_callback_t *)data)->zoneid;
6125576Sedp boolean_t mount_cmd = ((fs_callback_t *)data)->mount_cmd;
6135182Sedp char rootpath[MAXPATHLEN];
6140Sstevel@tonic-gate pid_t child;
6150Sstevel@tonic-gate int child_status;
6160Sstevel@tonic-gate int tmpl_fd;
6175182Sedp int rv;
6180Sstevel@tonic-gate ctid_t ct;
6190Sstevel@tonic-gate
6205576Sedp /* determine the zone rootpath */
6215576Sedp if (mount_cmd) {
6225576Sedp char zonepath[MAXPATHLEN];
6235576Sedp char luroot[MAXPATHLEN];
6245576Sedp
6255576Sedp if (zone_get_zonepath(zone_name,
6265576Sedp zonepath, sizeof (zonepath)) != Z_OK) {
6275576Sedp zerror(zlogp, B_FALSE, "unable to determine zone path");
6285576Sedp return (-1);
6295576Sedp }
6305576Sedp
6315576Sedp (void) snprintf(luroot, sizeof (luroot), "%s/lu", zonepath);
6325576Sedp resolve_lofs(zlogp, luroot, sizeof (luroot));
6335576Sedp (void) strlcpy(rootpath, luroot, sizeof (rootpath));
6345576Sedp } else {
6355576Sedp if (zone_get_rootpath(zone_name,
6365576Sedp rootpath, sizeof (rootpath)) != Z_OK) {
6375576Sedp zerror(zlogp, B_FALSE, "unable to determine zone root");
6385576Sedp return (-1);
6395576Sedp }
6405182Sedp }
6415182Sedp
6425182Sedp if ((rv = valid_mount_path(zlogp, rootpath, spec, dir, fstype)) < 0) {
6435182Sedp zerror(zlogp, B_FALSE, "%s%s is not a valid mount point",
6445182Sedp rootpath, dir);
6455182Sedp return (-1);
6465182Sedp } else if (rv > 0) {
6475182Sedp /* The mount point path doesn't exist, create it now. */
6485182Sedp if (make_one_dir(zlogp, rootpath, dir,
6495182Sedp DEFAULT_DIR_MODE, DEFAULT_DIR_USER,
6505182Sedp DEFAULT_DIR_GROUP) != 0) {
6515182Sedp zerror(zlogp, B_FALSE, "failed to create mount point");
6525182Sedp return (-1);
6535182Sedp }
6545182Sedp
6555182Sedp /*
6565182Sedp * Now this might seem weird, but we need to invoke
6575182Sedp * valid_mount_path() again. Why? Because it checks
6585182Sedp * to make sure that the mount point path is canonical,
6595182Sedp * which it can only do if the path exists, so now that
6605182Sedp * we've created the path we have to verify it again.
6615182Sedp */
6625182Sedp if ((rv = valid_mount_path(zlogp, rootpath, spec, dir,
6635182Sedp fstype)) < 0) {
6645182Sedp zerror(zlogp, B_FALSE,
6655182Sedp "%s%s is not a valid mount point", rootpath, dir);
6665182Sedp return (-1);
6675182Sedp }
6685182Sedp }
6695182Sedp
6700Sstevel@tonic-gate if ((tmpl_fd = init_template()) == -1) {
6710Sstevel@tonic-gate zerror(zlogp, B_TRUE, "failed to create contract");
6720Sstevel@tonic-gate return (-1);
6730Sstevel@tonic-gate }
6740Sstevel@tonic-gate
6750Sstevel@tonic-gate if ((child = fork()) == -1) {
6760Sstevel@tonic-gate (void) ct_tmpl_clear(tmpl_fd);
6770Sstevel@tonic-gate (void) close(tmpl_fd);
6780Sstevel@tonic-gate zerror(zlogp, B_TRUE, "failed to fork");
6790Sstevel@tonic-gate return (-1);
6800Sstevel@tonic-gate
6810Sstevel@tonic-gate } else if (child == 0) { /* child */
6822712Snn35248 char opt_buf[MAX_MNTOPT_STR];
6832712Snn35248 int optlen = 0;
6842712Snn35248 int mflag = MS_DATA;
6852712Snn35248
6860Sstevel@tonic-gate (void) ct_tmpl_clear(tmpl_fd);
6870Sstevel@tonic-gate /*
6880Sstevel@tonic-gate * Even though there are no procs running in the zone, we
6890Sstevel@tonic-gate * do this for paranoia's sake.
6900Sstevel@tonic-gate */
6910Sstevel@tonic-gate (void) closefrom(0);
6920Sstevel@tonic-gate
6930Sstevel@tonic-gate if (zone_enter(zoneid) == -1) {
6940Sstevel@tonic-gate _exit(errno);
6950Sstevel@tonic-gate }
6962712Snn35248 if (opt != NULL) {
6972712Snn35248 /*
6982712Snn35248 * The mount() system call is incredibly annoying.
6992712Snn35248 * If options are specified, we need to copy them
7002712Snn35248 * into a temporary buffer since the mount() system
7012712Snn35248 * call will overwrite the options string. It will
7022712Snn35248 * also fail if the new option string it wants to
7032712Snn35248 * write is bigger than the one we passed in, so
7042712Snn35248 * you must pass in a buffer of the maximum possible
7052712Snn35248 * option string length. sigh.
7062712Snn35248 */
7072712Snn35248 (void) strlcpy(opt_buf, opt, sizeof (opt_buf));
7082712Snn35248 opt = opt_buf;
7092712Snn35248 optlen = MAX_MNTOPT_STR;
7102712Snn35248 mflag = MS_OPTIONSTR;
7112712Snn35248 }
7122712Snn35248 if (mount(spec, dir, mflag, fstype, NULL, 0, opt, optlen) != 0)
7130Sstevel@tonic-gate _exit(errno);
7140Sstevel@tonic-gate _exit(0);
7150Sstevel@tonic-gate }
7160Sstevel@tonic-gate
7170Sstevel@tonic-gate /* parent */
7180Sstevel@tonic-gate if (contract_latest(&ct) == -1)
7190Sstevel@tonic-gate ct = -1;
7200Sstevel@tonic-gate (void) ct_tmpl_clear(tmpl_fd);
7210Sstevel@tonic-gate (void) close(tmpl_fd);
7220Sstevel@tonic-gate if (waitpid(child, &child_status, 0) != child) {
7230Sstevel@tonic-gate /* unexpected: we must have been signalled */
7240Sstevel@tonic-gate (void) contract_abandon_id(ct);
7250Sstevel@tonic-gate return (-1);
7260Sstevel@tonic-gate }
7270Sstevel@tonic-gate (void) contract_abandon_id(ct);
7280Sstevel@tonic-gate if (WEXITSTATUS(child_status) != 0) {
7290Sstevel@tonic-gate errno = WEXITSTATUS(child_status);
7300Sstevel@tonic-gate zerror(zlogp, B_TRUE, "mount of %s failed", dir);
7310Sstevel@tonic-gate return (-1);
7320Sstevel@tonic-gate }
7330Sstevel@tonic-gate
7340Sstevel@tonic-gate return (0);
7350Sstevel@tonic-gate }
7360Sstevel@tonic-gate
7377370S<Gerald Jelinek> /*
7387370S<Gerald Jelinek> * If retstr is not NULL, the output of the subproc is returned in the str,
7397370S<Gerald Jelinek> * otherwise it is output using zerror(). Any memory allocated for retstr
7407370S<Gerald Jelinek> * should be freed by the caller.
7417370S<Gerald Jelinek> */
7422712Snn35248 int
do_subproc(zlog_t * zlogp,char * cmdbuf,char ** retstr)7437370S<Gerald Jelinek> do_subproc(zlog_t *zlogp, char *cmdbuf, char **retstr)
744766Scarlsonj {
7457370S<Gerald Jelinek> char buf[1024]; /* arbitrary large amount */
7467370S<Gerald Jelinek> char *inbuf;
7472712Snn35248 FILE *file;
7482712Snn35248 int status;
7497370S<Gerald Jelinek> int rd_cnt;
7507370S<Gerald Jelinek>
7517370S<Gerald Jelinek> if (retstr != NULL) {
7527370S<Gerald Jelinek> if ((*retstr = malloc(1024)) == NULL) {
7537370S<Gerald Jelinek> zerror(zlogp, B_FALSE, "out of memory");
7547370S<Gerald Jelinek> return (-1);
7557370S<Gerald Jelinek> }
7567370S<Gerald Jelinek> inbuf = *retstr;
7577370S<Gerald Jelinek> rd_cnt = 0;
7587370S<Gerald Jelinek> } else {
7597370S<Gerald Jelinek> inbuf = buf;
7607370S<Gerald Jelinek> }
761766Scarlsonj
7622712Snn35248 file = popen(cmdbuf, "r");
7632712Snn35248 if (file == NULL) {
7642712Snn35248 zerror(zlogp, B_TRUE, "could not launch: %s", cmdbuf);
7652525Sdp return (-1);
7662712Snn35248 }
7672525Sdp
7687370S<Gerald Jelinek> while (fgets(inbuf, 1024, file) != NULL) {
76910521Sgerald.jelinek@sun.com if (retstr == NULL) {
77010521Sgerald.jelinek@sun.com if (zlogp != &logsys)
77110521Sgerald.jelinek@sun.com zerror(zlogp, B_FALSE, "%s", inbuf);
7727370S<Gerald Jelinek> } else {
7737370S<Gerald Jelinek> char *p;
7747370S<Gerald Jelinek>
7757370S<Gerald Jelinek> rd_cnt += 1024 - 1;
7767370S<Gerald Jelinek> if ((p = realloc(*retstr, rd_cnt + 1024)) == NULL) {
7777370S<Gerald Jelinek> zerror(zlogp, B_FALSE, "out of memory");
7787370S<Gerald Jelinek> (void) pclose(file);
7797370S<Gerald Jelinek> return (-1);
7807370S<Gerald Jelinek> }
7817370S<Gerald Jelinek>
7827370S<Gerald Jelinek> *retstr = p;
7837370S<Gerald Jelinek> inbuf = *retstr + rd_cnt;
7847370S<Gerald Jelinek> }
7857370S<Gerald Jelinek> }
7862712Snn35248 status = pclose(file);
787766Scarlsonj
7882712Snn35248 if (WIFSIGNALED(status)) {
7892712Snn35248 zerror(zlogp, B_FALSE, "%s unexpectedly terminated due to "
7902712Snn35248 "signal %d", cmdbuf, WTERMSIG(status));
791766Scarlsonj return (-1);
7922712Snn35248 }
7932712Snn35248 assert(WIFEXITED(status));
7942712Snn35248 if (WEXITSTATUS(status) == ZEXIT_EXEC) {
7952712Snn35248 zerror(zlogp, B_FALSE, "failed to exec %s", cmdbuf);
7962712Snn35248 return (-1);
7972712Snn35248 }
7982712Snn35248 return (WEXITSTATUS(status));
799766Scarlsonj }
800766Scarlsonj
801766Scarlsonj static int
zone_bootup(zlog_t * zlogp,const char * bootargs,int zstate)8027370S<Gerald Jelinek> zone_bootup(zlog_t *zlogp, const char *bootargs, int zstate)
8030Sstevel@tonic-gate {
8040Sstevel@tonic-gate zoneid_t zoneid;
8050Sstevel@tonic-gate struct stat st;
8067089Sgjelinek char zpath[MAXPATHLEN], initpath[MAXPATHLEN], init_file[MAXPATHLEN];
8072267Sdp char nbootargs[BOOTARGS_MAX];
8082712Snn35248 char cmdbuf[MAXPATHLEN];
8092712Snn35248 fs_callback_t cb;
8102727Sedp brand_handle_t bh;
81110616SSebastien.Roy@Sun.COM zone_iptype_t iptype;
81210616SSebastien.Roy@Sun.COM boolean_t links_loaded = B_FALSE;
81310616SSebastien.Roy@Sun.COM dladm_status_t status;
81410616SSebastien.Roy@Sun.COM char errmsg[DLADM_STRSIZE];
8152267Sdp int err;
8160Sstevel@tonic-gate
8177370S<Gerald Jelinek> if (brand_prestatechg(zlogp, zstate, Z_BOOT) != 0)
8187370S<Gerald Jelinek> return (-1);
8197370S<Gerald Jelinek>
8200Sstevel@tonic-gate if ((zoneid = getzoneidbyname(zone_name)) == -1) {
8210Sstevel@tonic-gate zerror(zlogp, B_TRUE, "unable to get zoneid");
8228958Sdp@eng.sun.com goto bad;
8230Sstevel@tonic-gate }
8240Sstevel@tonic-gate
8252712Snn35248 cb.zlogp = zlogp;
8262712Snn35248 cb.zoneid = zoneid;
8275576Sedp cb.mount_cmd = B_FALSE;
8282712Snn35248
8292712Snn35248 /* Get a handle to the brand info for this zone */
8302727Sedp if ((bh = brand_open(brand_name)) == NULL) {
8312712Snn35248 zerror(zlogp, B_FALSE, "unable to determine zone brand");
8328958Sdp@eng.sun.com goto bad;
8332712Snn35248 }
8342712Snn35248
8352712Snn35248 /*
8362712Snn35248 * Get the list of filesystems to mount from the brand
8372712Snn35248 * configuration. These mounts are done via a thread that will
8382712Snn35248 * enter the zone, so they are done from within the context of the
8392712Snn35248 * zone.
8402712Snn35248 */
8412727Sedp if (brand_platform_iter_mounts(bh, mount_early_fs, &cb) != 0) {
8422712Snn35248 zerror(zlogp, B_FALSE, "unable to mount filesystems");
8432727Sedp brand_close(bh);
8448958Sdp@eng.sun.com goto bad;
8452712Snn35248 }
8462712Snn35248
8472712Snn35248 /*
8482712Snn35248 * Get the brand's boot callback if it exists.
8492712Snn35248 */
8507089Sgjelinek if (zone_get_zonepath(zone_name, zpath, sizeof (zpath)) != Z_OK) {
8517089Sgjelinek zerror(zlogp, B_FALSE, "unable to determine zone path");
8523716Sgjelinek brand_close(bh);
8538958Sdp@eng.sun.com goto bad;
8542712Snn35248 }
8552712Snn35248 (void) strcpy(cmdbuf, EXEC_PREFIX);
8567089Sgjelinek if (brand_get_boot(bh, zone_name, zpath, cmdbuf + EXEC_LEN,
8577089Sgjelinek sizeof (cmdbuf) - EXEC_LEN) != 0) {
8582712Snn35248 zerror(zlogp, B_FALSE,
8592712Snn35248 "unable to determine branded zone's boot callback");
8602727Sedp brand_close(bh);
8618958Sdp@eng.sun.com goto bad;
8622712Snn35248 }
8632712Snn35248
8642712Snn35248 /* Get the path for this zone's init(1M) (or equivalent) process. */
8652727Sedp if (brand_get_initname(bh, init_file, MAXPATHLEN) != 0) {
8662712Snn35248 zerror(zlogp, B_FALSE,
8672712Snn35248 "unable to determine zone's init(1M) location");
8682727Sedp brand_close(bh);
8698958Sdp@eng.sun.com goto bad;
8702712Snn35248 }
8712712Snn35248
8722727Sedp brand_close(bh);
8730Sstevel@tonic-gate
8742267Sdp err = filter_bootargs(zlogp, bootargs, nbootargs, init_file,
8752267Sdp bad_boot_arg);
8762267Sdp if (err == Z_INVAL)
8772267Sdp eventstream_write(Z_EVT_ZONE_BADARGS);
8782267Sdp else if (err != Z_OK)
8798958Sdp@eng.sun.com goto bad;
8802267Sdp
8812267Sdp assert(init_file[0] != '\0');
8822267Sdp
8832712Snn35248 /* Try to anticipate possible problems: Make sure init is executable. */
8847089Sgjelinek if (zone_get_rootpath(zone_name, zpath, sizeof (zpath)) != Z_OK) {
8850Sstevel@tonic-gate zerror(zlogp, B_FALSE, "unable to determine zone root");
8868958Sdp@eng.sun.com goto bad;
8870Sstevel@tonic-gate }
8882712Snn35248
8897089Sgjelinek (void) snprintf(initpath, sizeof (initpath), "%s%s", zpath, init_file);
8900Sstevel@tonic-gate
8910Sstevel@tonic-gate if (stat(initpath, &st) == -1) {
8920Sstevel@tonic-gate zerror(zlogp, B_TRUE, "could not stat %s", initpath);
8938958Sdp@eng.sun.com goto bad;
8940Sstevel@tonic-gate }
8950Sstevel@tonic-gate
8960Sstevel@tonic-gate if ((st.st_mode & S_IXUSR) == 0) {
8970Sstevel@tonic-gate zerror(zlogp, B_FALSE, "%s is not executable", initpath);
8988958Sdp@eng.sun.com goto bad;
8990Sstevel@tonic-gate }
9000Sstevel@tonic-gate
9012712Snn35248 /*
90210616SSebastien.Roy@Sun.COM * Exclusive stack zones interact with the dlmgmtd running in the
90310616SSebastien.Roy@Sun.COM * global zone. dladm_zone_boot() tells dlmgmtd that this zone is
90410616SSebastien.Roy@Sun.COM * booting, and loads its datalinks from the zone's datalink
90510616SSebastien.Roy@Sun.COM * configuration file.
90610616SSebastien.Roy@Sun.COM */
90710616SSebastien.Roy@Sun.COM if (vplat_get_iptype(zlogp, &iptype) == 0 && iptype == ZS_EXCLUSIVE) {
90810616SSebastien.Roy@Sun.COM status = dladm_zone_boot(dld_handle, zoneid);
90910616SSebastien.Roy@Sun.COM if (status != DLADM_STATUS_OK) {
91010616SSebastien.Roy@Sun.COM zerror(zlogp, B_FALSE, "unable to load zone datalinks: "
91110616SSebastien.Roy@Sun.COM " %s", dladm_status2str(status, errmsg));
91210616SSebastien.Roy@Sun.COM goto bad;
91310616SSebastien.Roy@Sun.COM }
91410616SSebastien.Roy@Sun.COM links_loaded = B_TRUE;
91510616SSebastien.Roy@Sun.COM }
91610616SSebastien.Roy@Sun.COM
91710616SSebastien.Roy@Sun.COM /*
9182712Snn35248 * If there is a brand 'boot' callback, execute it now to give the
9192712Snn35248 * brand one last chance to do any additional setup before the zone
9202712Snn35248 * is booted.
9212712Snn35248 */
9222712Snn35248 if ((strlen(cmdbuf) > EXEC_LEN) &&
9237370S<Gerald Jelinek> (do_subproc(zlogp, cmdbuf, NULL) != Z_OK)) {
9242712Snn35248 zerror(zlogp, B_FALSE, "%s failed", cmdbuf);
9258958Sdp@eng.sun.com goto bad;
9262712Snn35248 }
9272712Snn35248
9282267Sdp if (zone_setattr(zoneid, ZONE_ATTR_INITNAME, init_file, 0) == -1) {
9292267Sdp zerror(zlogp, B_TRUE, "could not set zone boot file");
9308958Sdp@eng.sun.com goto bad;
9312267Sdp }
9322267Sdp
9332267Sdp if (zone_setattr(zoneid, ZONE_ATTR_BOOTARGS, nbootargs, 0) == -1) {
9342267Sdp zerror(zlogp, B_TRUE, "could not set zone boot arguments");
9358958Sdp@eng.sun.com goto bad;
9362267Sdp }
9372267Sdp
938*13122SStephen.Lawrence@oracle.COM /*
939*13122SStephen.Lawrence@oracle.COM * Inform zonestatd of a new zone so that it can install a door for
940*13122SStephen.Lawrence@oracle.COM * the zone to contact it.
941*13122SStephen.Lawrence@oracle.COM */
942*13122SStephen.Lawrence@oracle.COM notify_zonestatd(zone_id);
943*13122SStephen.Lawrence@oracle.COM
9442267Sdp if (zone_boot(zoneid) == -1) {
9450Sstevel@tonic-gate zerror(zlogp, B_TRUE, "unable to boot zone");
9468958Sdp@eng.sun.com goto bad;
9470Sstevel@tonic-gate }
9480Sstevel@tonic-gate
9497370S<Gerald Jelinek> if (brand_poststatechg(zlogp, zstate, Z_BOOT) != 0)
9508958Sdp@eng.sun.com goto bad;
9517370S<Gerald Jelinek>
9520Sstevel@tonic-gate return (0);
9538958Sdp@eng.sun.com
9548958Sdp@eng.sun.com bad:
9558958Sdp@eng.sun.com /*
9568958Sdp@eng.sun.com * If something goes wrong, we up the zones's state to the target
9578958Sdp@eng.sun.com * state, RUNNING, and then invoke the hook as if we're halting.
9588958Sdp@eng.sun.com */
9598958Sdp@eng.sun.com (void) brand_poststatechg(zlogp, ZONE_STATE_RUNNING, Z_HALT);
96010616SSebastien.Roy@Sun.COM if (links_loaded)
96110616SSebastien.Roy@Sun.COM (void) dladm_zone_halt(dld_handle, zoneid);
9628958Sdp@eng.sun.com return (-1);
9630Sstevel@tonic-gate }
9640Sstevel@tonic-gate
9650Sstevel@tonic-gate static int
zone_halt(zlog_t * zlogp,boolean_t unmount_cmd,boolean_t rebooting,int zstate)9667370S<Gerald Jelinek> zone_halt(zlog_t *zlogp, boolean_t unmount_cmd, boolean_t rebooting, int zstate)
9670Sstevel@tonic-gate {
9680Sstevel@tonic-gate int err;
9690Sstevel@tonic-gate
9707370S<Gerald Jelinek> if (brand_prestatechg(zlogp, zstate, Z_HALT) != 0)
9717370S<Gerald Jelinek> return (-1);
9727370S<Gerald Jelinek>
9733247Sgjelinek if (vplat_teardown(zlogp, unmount_cmd, rebooting) != 0) {
9740Sstevel@tonic-gate if (!bringup_failure_recovery)
9750Sstevel@tonic-gate zerror(zlogp, B_FALSE, "unable to destroy zone");
9760Sstevel@tonic-gate return (-1);
9770Sstevel@tonic-gate }
9780Sstevel@tonic-gate
9790Sstevel@tonic-gate if ((err = zonecfg_destroy_snapshot(zone_name)) != Z_OK)
9800Sstevel@tonic-gate zerror(zlogp, B_FALSE, "destroying snapshot: %s",
9810Sstevel@tonic-gate zonecfg_strerror(err));
9820Sstevel@tonic-gate
9837370S<Gerald Jelinek> if (brand_poststatechg(zlogp, zstate, Z_HALT) != 0)
9847370S<Gerald Jelinek> return (-1);
9857370S<Gerald Jelinek>
9860Sstevel@tonic-gate return (0);
9870Sstevel@tonic-gate }
9880Sstevel@tonic-gate
9890Sstevel@tonic-gate /*
9900Sstevel@tonic-gate * Generate AUE_zone_state for a command that boots a zone.
9910Sstevel@tonic-gate */
9920Sstevel@tonic-gate static void
audit_put_record(zlog_t * zlogp,ucred_t * uc,int return_val,char * new_state)9930Sstevel@tonic-gate audit_put_record(zlog_t *zlogp, ucred_t *uc, int return_val,
9940Sstevel@tonic-gate char *new_state)
9950Sstevel@tonic-gate {
9960Sstevel@tonic-gate adt_session_data_t *ah;
9970Sstevel@tonic-gate adt_event_data_t *event;
9980Sstevel@tonic-gate int pass_fail, fail_reason;
9990Sstevel@tonic-gate
10000Sstevel@tonic-gate if (!adt_audit_enabled())
10010Sstevel@tonic-gate return;
10020Sstevel@tonic-gate
10030Sstevel@tonic-gate if (return_val == 0) {
10040Sstevel@tonic-gate pass_fail = ADT_SUCCESS;
10050Sstevel@tonic-gate fail_reason = ADT_SUCCESS;
10060Sstevel@tonic-gate } else {
10070Sstevel@tonic-gate pass_fail = ADT_FAILURE;
10080Sstevel@tonic-gate fail_reason = ADT_FAIL_VALUE_PROGRAM;
10090Sstevel@tonic-gate }
10100Sstevel@tonic-gate
10110Sstevel@tonic-gate if (adt_start_session(&ah, NULL, 0)) {
10120Sstevel@tonic-gate zerror(zlogp, B_TRUE, gettext("audit failure."));
10130Sstevel@tonic-gate return;
10140Sstevel@tonic-gate }
10150Sstevel@tonic-gate if (adt_set_from_ucred(ah, uc, ADT_NEW)) {
10160Sstevel@tonic-gate zerror(zlogp, B_TRUE, gettext("audit failure."));
10170Sstevel@tonic-gate (void) adt_end_session(ah);
10180Sstevel@tonic-gate return;
10190Sstevel@tonic-gate }
10200Sstevel@tonic-gate
10210Sstevel@tonic-gate event = adt_alloc_event(ah, ADT_zone_state);
10220Sstevel@tonic-gate if (event == NULL) {
10230Sstevel@tonic-gate zerror(zlogp, B_TRUE, gettext("audit failure."));
10240Sstevel@tonic-gate (void) adt_end_session(ah);
10250Sstevel@tonic-gate return;
10260Sstevel@tonic-gate }
10270Sstevel@tonic-gate event->adt_zone_state.zonename = zone_name;
10280Sstevel@tonic-gate event->adt_zone_state.new_state = new_state;
10290Sstevel@tonic-gate
10300Sstevel@tonic-gate if (adt_put_event(event, pass_fail, fail_reason))
10310Sstevel@tonic-gate zerror(zlogp, B_TRUE, gettext("audit failure."));
10320Sstevel@tonic-gate
10330Sstevel@tonic-gate adt_free_event(event);
10340Sstevel@tonic-gate
10350Sstevel@tonic-gate (void) adt_end_session(ah);
10360Sstevel@tonic-gate }
10370Sstevel@tonic-gate
10380Sstevel@tonic-gate /*
10390Sstevel@tonic-gate * The main routine for the door server that deals with zone state transitions.
10400Sstevel@tonic-gate */
10410Sstevel@tonic-gate /* ARGSUSED */
10420Sstevel@tonic-gate static void
server(void * cookie,char * args,size_t alen,door_desc_t * dp,uint_t n_desc)10430Sstevel@tonic-gate server(void *cookie, char *args, size_t alen, door_desc_t *dp,
10440Sstevel@tonic-gate uint_t n_desc)
10450Sstevel@tonic-gate {
10460Sstevel@tonic-gate ucred_t *uc = NULL;
10470Sstevel@tonic-gate const priv_set_t *eset;
10480Sstevel@tonic-gate
10490Sstevel@tonic-gate zone_state_t zstate;
10500Sstevel@tonic-gate zone_cmd_t cmd;
10510Sstevel@tonic-gate zone_cmd_arg_t *zargp;
10520Sstevel@tonic-gate
10530Sstevel@tonic-gate boolean_t kernelcall;
10540Sstevel@tonic-gate
10550Sstevel@tonic-gate int rval = -1;
10560Sstevel@tonic-gate uint64_t uniqid;
10570Sstevel@tonic-gate zoneid_t zoneid = -1;
10580Sstevel@tonic-gate zlog_t zlog;
10590Sstevel@tonic-gate zlog_t *zlogp;
10600Sstevel@tonic-gate zone_cmd_rval_t *rvalp;
10610Sstevel@tonic-gate size_t rlen = getpagesize(); /* conservative */
10622712Snn35248 fs_callback_t cb;
10632727Sedp brand_handle_t bh;
10640Sstevel@tonic-gate
10650Sstevel@tonic-gate /* LINTED E_BAD_PTR_CAST_ALIGN */
10660Sstevel@tonic-gate zargp = (zone_cmd_arg_t *)args;
10670Sstevel@tonic-gate
10680Sstevel@tonic-gate /*
10690Sstevel@tonic-gate * When we get the door unref message, we've fdetach'd the door, and
10700Sstevel@tonic-gate * it is time for us to shut down zoneadmd.
10710Sstevel@tonic-gate */
10720Sstevel@tonic-gate if (zargp == DOOR_UNREF_DATA) {
10730Sstevel@tonic-gate /*
10740Sstevel@tonic-gate * See comment at end of main() for info on the last rites.
10750Sstevel@tonic-gate */
10760Sstevel@tonic-gate exit(0);
10770Sstevel@tonic-gate }
10780Sstevel@tonic-gate
10790Sstevel@tonic-gate if (zargp == NULL) {
10800Sstevel@tonic-gate (void) door_return(NULL, 0, 0, 0);
10810Sstevel@tonic-gate }
10820Sstevel@tonic-gate
10830Sstevel@tonic-gate rvalp = alloca(rlen);
10840Sstevel@tonic-gate bzero(rvalp, rlen);
10850Sstevel@tonic-gate zlog.logfile = NULL;
10860Sstevel@tonic-gate zlog.buflen = zlog.loglen = rlen - sizeof (zone_cmd_rval_t) + 1;
10870Sstevel@tonic-gate zlog.buf = rvalp->errbuf;
10880Sstevel@tonic-gate zlog.log = zlog.buf;
10890Sstevel@tonic-gate /* defer initialization of zlog.locale until after credential check */
10900Sstevel@tonic-gate zlogp = &zlog;
10910Sstevel@tonic-gate
10920Sstevel@tonic-gate if (alen != sizeof (zone_cmd_arg_t)) {
10930Sstevel@tonic-gate /*
10940Sstevel@tonic-gate * This really shouldn't be happening.
10950Sstevel@tonic-gate */
10962267Sdp zerror(&logsys, B_FALSE, "argument size (%d bytes) "
10972267Sdp "unexpected (expected %d bytes)", alen,
10982267Sdp sizeof (zone_cmd_arg_t));
10990Sstevel@tonic-gate goto out;
11000Sstevel@tonic-gate }
11010Sstevel@tonic-gate cmd = zargp->cmd;
11020Sstevel@tonic-gate
11030Sstevel@tonic-gate if (door_ucred(&uc) != 0) {
11040Sstevel@tonic-gate zerror(&logsys, B_TRUE, "door_ucred");
11050Sstevel@tonic-gate goto out;
11060Sstevel@tonic-gate }
11070Sstevel@tonic-gate eset = ucred_getprivset(uc, PRIV_EFFECTIVE);
11080Sstevel@tonic-gate if (ucred_getzoneid(uc) != GLOBAL_ZONEID ||
11090Sstevel@tonic-gate (eset != NULL ? !priv_ismember(eset, PRIV_SYS_CONFIG) :
11100Sstevel@tonic-gate ucred_geteuid(uc) != 0)) {
11110Sstevel@tonic-gate zerror(&logsys, B_FALSE, "insufficient privileges");
11120Sstevel@tonic-gate goto out;
11130Sstevel@tonic-gate }
11140Sstevel@tonic-gate
11150Sstevel@tonic-gate kernelcall = ucred_getpid(uc) == 0;
11160Sstevel@tonic-gate
11170Sstevel@tonic-gate /*
11180Sstevel@tonic-gate * This is safe because we only use a zlog_t throughout the
11190Sstevel@tonic-gate * duration of a door call; i.e., by the time the pointer
11200Sstevel@tonic-gate * might become invalid, the door call would be over.
11210Sstevel@tonic-gate */
11220Sstevel@tonic-gate zlog.locale = kernelcall ? DEFAULT_LOCALE : zargp->locale;
11230Sstevel@tonic-gate
11240Sstevel@tonic-gate (void) mutex_lock(&lock);
11250Sstevel@tonic-gate
11260Sstevel@tonic-gate /*
11270Sstevel@tonic-gate * Once we start to really die off, we don't want more connections.
11280Sstevel@tonic-gate */
11290Sstevel@tonic-gate if (in_death_throes) {
11300Sstevel@tonic-gate (void) mutex_unlock(&lock);
11310Sstevel@tonic-gate ucred_free(uc);
11320Sstevel@tonic-gate (void) door_return(NULL, 0, 0, 0);
11330Sstevel@tonic-gate thr_exit(NULL);
11340Sstevel@tonic-gate }
11350Sstevel@tonic-gate
11360Sstevel@tonic-gate /*
11370Sstevel@tonic-gate * Check for validity of command.
11380Sstevel@tonic-gate */
11392712Snn35248 if (cmd != Z_READY && cmd != Z_BOOT && cmd != Z_FORCEBOOT &&
11402712Snn35248 cmd != Z_REBOOT && cmd != Z_HALT && cmd != Z_NOTE_UNINSTALLING &&
11412712Snn35248 cmd != Z_MOUNT && cmd != Z_FORCEMOUNT && cmd != Z_UNMOUNT) {
1142766Scarlsonj zerror(&logsys, B_FALSE, "invalid command %d", (int)cmd);
11430Sstevel@tonic-gate goto out;
11440Sstevel@tonic-gate }
11450Sstevel@tonic-gate
11460Sstevel@tonic-gate if (kernelcall && (cmd != Z_HALT && cmd != Z_REBOOT)) {
11470Sstevel@tonic-gate /*
11480Sstevel@tonic-gate * Can't happen
11490Sstevel@tonic-gate */
11500Sstevel@tonic-gate zerror(&logsys, B_FALSE, "received unexpected kernel upcall %d",
11510Sstevel@tonic-gate cmd);
11520Sstevel@tonic-gate goto out;
11530Sstevel@tonic-gate }
11540Sstevel@tonic-gate /*
11550Sstevel@tonic-gate * We ignore the possibility of someone calling zone_create(2)
11560Sstevel@tonic-gate * explicitly; all requests must come through zoneadmd.
11570Sstevel@tonic-gate */
11580Sstevel@tonic-gate if (zone_get_state(zone_name, &zstate) != Z_OK) {
11590Sstevel@tonic-gate /*
11600Sstevel@tonic-gate * Something terribly wrong happened
11610Sstevel@tonic-gate */
11620Sstevel@tonic-gate zerror(&logsys, B_FALSE, "unable to determine state of zone");
11630Sstevel@tonic-gate goto out;
11640Sstevel@tonic-gate }
11650Sstevel@tonic-gate
11660Sstevel@tonic-gate if (kernelcall) {
11670Sstevel@tonic-gate /*
11680Sstevel@tonic-gate * Kernel-initiated requests may lose their validity if the
11690Sstevel@tonic-gate * zone_t the kernel was referring to has gone away.
11700Sstevel@tonic-gate */
11710Sstevel@tonic-gate if ((zoneid = getzoneidbyname(zone_name)) == -1 ||
11720Sstevel@tonic-gate zone_getattr(zoneid, ZONE_ATTR_UNIQID, &uniqid,
11730Sstevel@tonic-gate sizeof (uniqid)) == -1 || uniqid != zargp->uniqid) {
11740Sstevel@tonic-gate /*
11750Sstevel@tonic-gate * We're not talking about the same zone. The request
11760Sstevel@tonic-gate * must have arrived too late. Return error.
11770Sstevel@tonic-gate */
11780Sstevel@tonic-gate rval = -1;
11790Sstevel@tonic-gate goto out;
11800Sstevel@tonic-gate }
11810Sstevel@tonic-gate zlogp = &logsys; /* Log errors to syslog */
11820Sstevel@tonic-gate }
11830Sstevel@tonic-gate
11842712Snn35248 /*
11852712Snn35248 * If we are being asked to forcibly mount or boot a zone, we
11862712Snn35248 * pretend that an INCOMPLETE zone is actually INSTALLED.
11872712Snn35248 */
11882712Snn35248 if (zstate == ZONE_STATE_INCOMPLETE &&
11892712Snn35248 (cmd == Z_FORCEBOOT || cmd == Z_FORCEMOUNT))
11902712Snn35248 zstate = ZONE_STATE_INSTALLED;
11912712Snn35248
11920Sstevel@tonic-gate switch (zstate) {
11930Sstevel@tonic-gate case ZONE_STATE_CONFIGURED:
11940Sstevel@tonic-gate case ZONE_STATE_INCOMPLETE:
11950Sstevel@tonic-gate /*
11960Sstevel@tonic-gate * Not our area of expertise; we just print a nice message
11970Sstevel@tonic-gate * and die off.
11980Sstevel@tonic-gate */
11990Sstevel@tonic-gate zerror(zlogp, B_FALSE,
12000Sstevel@tonic-gate "%s operation is invalid for zones in state '%s'",
1201766Scarlsonj z_cmd_name(cmd), zone_state_str(zstate));
12020Sstevel@tonic-gate break;
12030Sstevel@tonic-gate
12040Sstevel@tonic-gate case ZONE_STATE_INSTALLED:
12050Sstevel@tonic-gate switch (cmd) {
12060Sstevel@tonic-gate case Z_READY:
12077370S<Gerald Jelinek> rval = zone_ready(zlogp, Z_MNT_BOOT, zstate);
12080Sstevel@tonic-gate if (rval == 0)
12090Sstevel@tonic-gate eventstream_write(Z_EVT_ZONE_READIED);
12100Sstevel@tonic-gate break;
12110Sstevel@tonic-gate case Z_BOOT:
12122712Snn35248 case Z_FORCEBOOT:
12130Sstevel@tonic-gate eventstream_write(Z_EVT_ZONE_BOOTING);
12147370S<Gerald Jelinek> if ((rval = zone_ready(zlogp, Z_MNT_BOOT, zstate))
12157370S<Gerald Jelinek> == 0) {
12167370S<Gerald Jelinek> rval = zone_bootup(zlogp, zargp->bootbuf,
12177370S<Gerald Jelinek> zstate);
12187370S<Gerald Jelinek> }
12190Sstevel@tonic-gate audit_put_record(zlogp, uc, rval, "boot");
12200Sstevel@tonic-gate if (rval != 0) {
12210Sstevel@tonic-gate bringup_failure_recovery = B_TRUE;
12227370S<Gerald Jelinek> (void) zone_halt(zlogp, B_FALSE, B_FALSE,
12237370S<Gerald Jelinek> zstate);
12241645Scomay eventstream_write(Z_EVT_ZONE_BOOTFAILED);
12250Sstevel@tonic-gate }
12260Sstevel@tonic-gate break;
12270Sstevel@tonic-gate case Z_HALT:
12280Sstevel@tonic-gate if (kernelcall) /* Invalid; can't happen */
12290Sstevel@tonic-gate abort();
12300Sstevel@tonic-gate /*
12310Sstevel@tonic-gate * We could have two clients racing to halt this
12320Sstevel@tonic-gate * zone; the second client loses, but his request
12330Sstevel@tonic-gate * doesn't fail, since the zone is now in the desired
12340Sstevel@tonic-gate * state.
12350Sstevel@tonic-gate */
12360Sstevel@tonic-gate zerror(zlogp, B_FALSE, "zone is already halted");
12370Sstevel@tonic-gate rval = 0;
12380Sstevel@tonic-gate break;
12390Sstevel@tonic-gate case Z_REBOOT:
12400Sstevel@tonic-gate if (kernelcall) /* Invalid; can't happen */
12410Sstevel@tonic-gate abort();
12420Sstevel@tonic-gate zerror(zlogp, B_FALSE, "%s operation is invalid "
1243766Scarlsonj "for zones in state '%s'", z_cmd_name(cmd),
12440Sstevel@tonic-gate zone_state_str(zstate));
12450Sstevel@tonic-gate rval = -1;
12460Sstevel@tonic-gate break;
12470Sstevel@tonic-gate case Z_NOTE_UNINSTALLING:
12480Sstevel@tonic-gate if (kernelcall) /* Invalid; can't happen */
12490Sstevel@tonic-gate abort();
12500Sstevel@tonic-gate /*
12510Sstevel@tonic-gate * Tell the console to print out a message about this.
12520Sstevel@tonic-gate * Once it does, we will be in_death_throes.
12530Sstevel@tonic-gate */
12540Sstevel@tonic-gate eventstream_write(Z_EVT_ZONE_UNINSTALLING);
12550Sstevel@tonic-gate break;
1256766Scarlsonj case Z_MOUNT:
12572712Snn35248 case Z_FORCEMOUNT:
1258766Scarlsonj if (kernelcall) /* Invalid; can't happen */
1259766Scarlsonj abort();
12608905SRic.Aleshire@Sun.COM if (!zone_isnative && !zone_iscluster &&
12618905SRic.Aleshire@Sun.COM !zone_islabeled) {
12627655Sgerald.jelinek@sun.com /*
12637655Sgerald.jelinek@sun.com * -U mounts the zone without lofs mounting
12647655Sgerald.jelinek@sun.com * zone file systems back into the scratch
12657655Sgerald.jelinek@sun.com * zone. This is required when mounting
12667655Sgerald.jelinek@sun.com * non-native branded zones.
12677655Sgerald.jelinek@sun.com */
12687655Sgerald.jelinek@sun.com (void) strlcpy(zargp->bootbuf, "-U",
12697655Sgerald.jelinek@sun.com BOOTARGS_MAX);
12702712Snn35248 }
12712712Snn35248
12725829Sgjelinek rval = zone_ready(zlogp,
12735829Sgjelinek strcmp(zargp->bootbuf, "-U") == 0 ?
12747370S<Gerald Jelinek> Z_MNT_UPDATE : Z_MNT_SCRATCH, zstate);
12752712Snn35248 if (rval != 0)
12762712Snn35248 break;
12772712Snn35248
12782712Snn35248 eventstream_write(Z_EVT_ZONE_READIED);
12792712Snn35248
12807655Sgerald.jelinek@sun.com /*
128110943SEdward.Pilatowicz@Sun.COM * Get a handle to the default brand info.
128210943SEdward.Pilatowicz@Sun.COM * We must always use the default brand file system
12837655Sgerald.jelinek@sun.com * list when mounting the zone.
12847655Sgerald.jelinek@sun.com */
128510943SEdward.Pilatowicz@Sun.COM if ((bh = brand_open(default_brand)) == NULL) {
12862712Snn35248 rval = -1;
12872712Snn35248 break;
12881645Scomay }
12891645Scomay
1290766Scarlsonj /*
12912712Snn35248 * Get the list of filesystems to mount from
12922712Snn35248 * the brand configuration. These mounts are done
12932712Snn35248 * via a thread that will enter the zone, so they
12942712Snn35248 * are done from within the context of the zone.
12952712Snn35248 */
12962712Snn35248 cb.zlogp = zlogp;
12972712Snn35248 cb.zoneid = zone_id;
12985576Sedp cb.mount_cmd = B_TRUE;
12992727Sedp rval = brand_platform_iter_mounts(bh,
13002712Snn35248 mount_early_fs, &cb);
13012712Snn35248
13022727Sedp brand_close(bh);
13032712Snn35248
13042712Snn35248 /*
1305766Scarlsonj * Ordinarily, /dev/fd would be mounted inside the zone
1306766Scarlsonj * by svc:/system/filesystem/usr:default, but since
1307766Scarlsonj * we're not booting the zone, we need to do this
1308766Scarlsonj * manually.
1309766Scarlsonj */
1310766Scarlsonj if (rval == 0)
13112712Snn35248 rval = mount_early_fs(&cb,
13122712Snn35248 "fd", "/dev/fd", "fd", NULL);
1313766Scarlsonj break;
1314766Scarlsonj case Z_UNMOUNT:
1315766Scarlsonj if (kernelcall) /* Invalid; can't happen */
1316766Scarlsonj abort();
1317766Scarlsonj zerror(zlogp, B_FALSE, "zone is already unmounted");
1318766Scarlsonj rval = 0;
1319766Scarlsonj break;
13200Sstevel@tonic-gate }
13210Sstevel@tonic-gate break;
13220Sstevel@tonic-gate
13230Sstevel@tonic-gate case ZONE_STATE_READY:
13240Sstevel@tonic-gate switch (cmd) {
13250Sstevel@tonic-gate case Z_READY:
13260Sstevel@tonic-gate /*
13270Sstevel@tonic-gate * We could have two clients racing to ready this
13280Sstevel@tonic-gate * zone; the second client loses, but his request
13290Sstevel@tonic-gate * doesn't fail, since the zone is now in the desired
13300Sstevel@tonic-gate * state.
13310Sstevel@tonic-gate */
13320Sstevel@tonic-gate zerror(zlogp, B_FALSE, "zone is already ready");
13330Sstevel@tonic-gate rval = 0;
13340Sstevel@tonic-gate break;
13350Sstevel@tonic-gate case Z_BOOT:
13362267Sdp (void) strlcpy(boot_args, zargp->bootbuf,
13372267Sdp sizeof (boot_args));
13380Sstevel@tonic-gate eventstream_write(Z_EVT_ZONE_BOOTING);
13397370S<Gerald Jelinek> rval = zone_bootup(zlogp, zargp->bootbuf, zstate);
13400Sstevel@tonic-gate audit_put_record(zlogp, uc, rval, "boot");
13410Sstevel@tonic-gate if (rval != 0) {
13420Sstevel@tonic-gate bringup_failure_recovery = B_TRUE;
13437370S<Gerald Jelinek> (void) zone_halt(zlogp, B_FALSE, B_TRUE,
13447370S<Gerald Jelinek> zstate);
13451645Scomay eventstream_write(Z_EVT_ZONE_BOOTFAILED);
13460Sstevel@tonic-gate }
13472267Sdp boot_args[0] = '\0';
13480Sstevel@tonic-gate break;
13490Sstevel@tonic-gate case Z_HALT:
13500Sstevel@tonic-gate if (kernelcall) /* Invalid; can't happen */
13510Sstevel@tonic-gate abort();
13527370S<Gerald Jelinek> if ((rval = zone_halt(zlogp, B_FALSE, B_FALSE, zstate))
13537370S<Gerald Jelinek> != 0)
13540Sstevel@tonic-gate break;
13550Sstevel@tonic-gate eventstream_write(Z_EVT_ZONE_HALTED);
13560Sstevel@tonic-gate break;
13570Sstevel@tonic-gate case Z_REBOOT:
1358766Scarlsonj case Z_NOTE_UNINSTALLING:
1359766Scarlsonj case Z_MOUNT:
1360766Scarlsonj case Z_UNMOUNT:
13610Sstevel@tonic-gate if (kernelcall) /* Invalid; can't happen */
13620Sstevel@tonic-gate abort();
13630Sstevel@tonic-gate zerror(zlogp, B_FALSE, "%s operation is invalid "
1364766Scarlsonj "for zones in state '%s'", z_cmd_name(cmd),
13650Sstevel@tonic-gate zone_state_str(zstate));
13660Sstevel@tonic-gate rval = -1;
13670Sstevel@tonic-gate break;
1368766Scarlsonj }
1369766Scarlsonj break;
1370766Scarlsonj
1371766Scarlsonj case ZONE_STATE_MOUNTED:
1372766Scarlsonj switch (cmd) {
1373766Scarlsonj case Z_UNMOUNT:
13740Sstevel@tonic-gate if (kernelcall) /* Invalid; can't happen */
13750Sstevel@tonic-gate abort();
13767370S<Gerald Jelinek> rval = zone_halt(zlogp, B_TRUE, B_FALSE, zstate);
13771645Scomay if (rval == 0) {
13781645Scomay eventstream_write(Z_EVT_ZONE_HALTED);
1379766Scarlsonj (void) sema_post(&scratch_sem);
13801645Scomay }
1381766Scarlsonj break;
1382766Scarlsonj default:
1383766Scarlsonj if (kernelcall) /* Invalid; can't happen */
1384766Scarlsonj abort();
1385766Scarlsonj zerror(zlogp, B_FALSE, "%s operation is invalid "
1386766Scarlsonj "for zones in state '%s'", z_cmd_name(cmd),
1387766Scarlsonj zone_state_str(zstate));
13880Sstevel@tonic-gate rval = -1;
13890Sstevel@tonic-gate break;
13900Sstevel@tonic-gate }
13910Sstevel@tonic-gate break;
13920Sstevel@tonic-gate
13930Sstevel@tonic-gate case ZONE_STATE_RUNNING:
13940Sstevel@tonic-gate case ZONE_STATE_SHUTTING_DOWN:
13950Sstevel@tonic-gate case ZONE_STATE_DOWN:
13960Sstevel@tonic-gate switch (cmd) {
13970Sstevel@tonic-gate case Z_READY:
13987370S<Gerald Jelinek> if ((rval = zone_halt(zlogp, B_FALSE, B_TRUE, zstate))
13997370S<Gerald Jelinek> != 0)
14000Sstevel@tonic-gate break;
14017370S<Gerald Jelinek> if ((rval = zone_ready(zlogp, Z_MNT_BOOT, zstate)) == 0)
14020Sstevel@tonic-gate eventstream_write(Z_EVT_ZONE_READIED);
14031645Scomay else
14041645Scomay eventstream_write(Z_EVT_ZONE_HALTED);
14050Sstevel@tonic-gate break;
14060Sstevel@tonic-gate case Z_BOOT:
14070Sstevel@tonic-gate /*
14080Sstevel@tonic-gate * We could have two clients racing to boot this
14090Sstevel@tonic-gate * zone; the second client loses, but his request
14100Sstevel@tonic-gate * doesn't fail, since the zone is now in the desired
14110Sstevel@tonic-gate * state.
14120Sstevel@tonic-gate */
14130Sstevel@tonic-gate zerror(zlogp, B_FALSE, "zone is already booted");
14140Sstevel@tonic-gate rval = 0;
14150Sstevel@tonic-gate break;
14160Sstevel@tonic-gate case Z_HALT:
14177370S<Gerald Jelinek> if ((rval = zone_halt(zlogp, B_FALSE, B_FALSE, zstate))
14187370S<Gerald Jelinek> != 0)
14190Sstevel@tonic-gate break;
14200Sstevel@tonic-gate eventstream_write(Z_EVT_ZONE_HALTED);
14210Sstevel@tonic-gate break;
14220Sstevel@tonic-gate case Z_REBOOT:
14232267Sdp (void) strlcpy(boot_args, zargp->bootbuf,
14242267Sdp sizeof (boot_args));
14250Sstevel@tonic-gate eventstream_write(Z_EVT_ZONE_REBOOTING);
14267370S<Gerald Jelinek> if ((rval = zone_halt(zlogp, B_FALSE, B_TRUE, zstate))
14277370S<Gerald Jelinek> != 0) {
14281645Scomay eventstream_write(Z_EVT_ZONE_BOOTFAILED);
14292267Sdp boot_args[0] = '\0';
14301645Scomay break;
14311645Scomay }
14327370S<Gerald Jelinek> if ((rval = zone_ready(zlogp, Z_MNT_BOOT, zstate))
14337370S<Gerald Jelinek> != 0) {
14341645Scomay eventstream_write(Z_EVT_ZONE_BOOTFAILED);
14352267Sdp boot_args[0] = '\0';
14360Sstevel@tonic-gate break;
14371645Scomay }
14387370S<Gerald Jelinek> rval = zone_bootup(zlogp, zargp->bootbuf, zstate);
14391645Scomay audit_put_record(zlogp, uc, rval, "reboot");
14401645Scomay if (rval != 0) {
14417370S<Gerald Jelinek> (void) zone_halt(zlogp, B_FALSE, B_TRUE,
14427370S<Gerald Jelinek> zstate);
14431645Scomay eventstream_write(Z_EVT_ZONE_BOOTFAILED);
14440Sstevel@tonic-gate }
14452267Sdp boot_args[0] = '\0';
14460Sstevel@tonic-gate break;
14470Sstevel@tonic-gate case Z_NOTE_UNINSTALLING:
1448766Scarlsonj case Z_MOUNT:
1449766Scarlsonj case Z_UNMOUNT:
1450766Scarlsonj zerror(zlogp, B_FALSE, "%s operation is invalid "
1451766Scarlsonj "for zones in state '%s'", z_cmd_name(cmd),
1452766Scarlsonj zone_state_str(zstate));
14530Sstevel@tonic-gate rval = -1;
14540Sstevel@tonic-gate break;
14550Sstevel@tonic-gate }
14560Sstevel@tonic-gate break;
14570Sstevel@tonic-gate default:
14580Sstevel@tonic-gate abort();
14590Sstevel@tonic-gate }
14600Sstevel@tonic-gate
14610Sstevel@tonic-gate /*
14620Sstevel@tonic-gate * Because the state of the zone may have changed, we make sure
14630Sstevel@tonic-gate * to wake the console poller, which is in charge of initiating
14640Sstevel@tonic-gate * the shutdown procedure as necessary.
14650Sstevel@tonic-gate */
14660Sstevel@tonic-gate eventstream_write(Z_EVT_NULL);
14670Sstevel@tonic-gate
14680Sstevel@tonic-gate out:
14690Sstevel@tonic-gate (void) mutex_unlock(&lock);
14700Sstevel@tonic-gate if (kernelcall) {
14710Sstevel@tonic-gate rvalp = NULL;
14720Sstevel@tonic-gate rlen = 0;
14730Sstevel@tonic-gate } else {
14740Sstevel@tonic-gate rvalp->rval = rval;
14750Sstevel@tonic-gate }
14760Sstevel@tonic-gate if (uc != NULL)
14770Sstevel@tonic-gate ucred_free(uc);
14780Sstevel@tonic-gate (void) door_return((char *)rvalp, rlen, NULL, 0);
14790Sstevel@tonic-gate thr_exit(NULL);
14800Sstevel@tonic-gate }
14810Sstevel@tonic-gate
14820Sstevel@tonic-gate static int
setup_door(zlog_t * zlogp)14830Sstevel@tonic-gate setup_door(zlog_t *zlogp)
14840Sstevel@tonic-gate {
14850Sstevel@tonic-gate if ((zone_door = door_create(server, NULL,
14860Sstevel@tonic-gate DOOR_UNREF | DOOR_REFUSE_DESC | DOOR_NO_CANCEL)) < 0) {
14870Sstevel@tonic-gate zerror(zlogp, B_TRUE, "%s failed", "door_create");
14880Sstevel@tonic-gate return (-1);
14890Sstevel@tonic-gate }
14900Sstevel@tonic-gate (void) fdetach(zone_door_path);
14910Sstevel@tonic-gate
14920Sstevel@tonic-gate if (fattach(zone_door, zone_door_path) != 0) {
14930Sstevel@tonic-gate zerror(zlogp, B_TRUE, "fattach to %s failed", zone_door_path);
14940Sstevel@tonic-gate (void) door_revoke(zone_door);
14950Sstevel@tonic-gate (void) fdetach(zone_door_path);
14960Sstevel@tonic-gate zone_door = -1;
14970Sstevel@tonic-gate return (-1);
14980Sstevel@tonic-gate }
14990Sstevel@tonic-gate return (0);
15000Sstevel@tonic-gate }
15010Sstevel@tonic-gate
15020Sstevel@tonic-gate /*
15030Sstevel@tonic-gate * zoneadm(1m) will start zoneadmd if it thinks it isn't running; this
15040Sstevel@tonic-gate * is where zoneadmd itself will check to see that another instance of
15050Sstevel@tonic-gate * zoneadmd isn't already controlling this zone.
15060Sstevel@tonic-gate *
15070Sstevel@tonic-gate * The idea here is that we want to open the path to which we will
15080Sstevel@tonic-gate * attach our door, lock it, and then make sure that no-one has beat us
15090Sstevel@tonic-gate * to fattach(3c)ing onto it.
15100Sstevel@tonic-gate *
15110Sstevel@tonic-gate * fattach(3c) is really a mount, so there are actually two possible
15120Sstevel@tonic-gate * vnodes we could be dealing with. Our strategy is as follows:
15130Sstevel@tonic-gate *
15140Sstevel@tonic-gate * - If the file we opened is a regular file (common case):
15150Sstevel@tonic-gate * There is no fattach(3c)ed door, so we have a chance of becoming
15160Sstevel@tonic-gate * the managing zoneadmd. We attempt to lock the file: if it is
15170Sstevel@tonic-gate * already locked, that means someone else raced us here, so we
15180Sstevel@tonic-gate * lose and give up. zoneadm(1m) will try to contact the zoneadmd
15190Sstevel@tonic-gate * that beat us to it.
15200Sstevel@tonic-gate *
15210Sstevel@tonic-gate * - If the file we opened is a namefs file:
15220Sstevel@tonic-gate * This means there is already an established door fattach(3c)'ed
15230Sstevel@tonic-gate * to the rendezvous path. We've lost the race, so we give up.
15240Sstevel@tonic-gate * Note that in this case we also try to grab the file lock, and
15250Sstevel@tonic-gate * will succeed in acquiring it since the vnode locked by the
15260Sstevel@tonic-gate * "winning" zoneadmd was a regular one, and the one we locked was
15270Sstevel@tonic-gate * the fattach(3c)'ed door node. At any rate, no harm is done, and
15280Sstevel@tonic-gate * we just return to zoneadm(1m) which knows to retry.
15290Sstevel@tonic-gate */
15300Sstevel@tonic-gate static int
make_daemon_exclusive(zlog_t * zlogp)15310Sstevel@tonic-gate make_daemon_exclusive(zlog_t *zlogp)
15320Sstevel@tonic-gate {
15330Sstevel@tonic-gate int doorfd = -1;
15340Sstevel@tonic-gate int err, ret = -1;
15350Sstevel@tonic-gate struct stat st;
15360Sstevel@tonic-gate struct flock flock;
15370Sstevel@tonic-gate zone_state_t zstate;
15380Sstevel@tonic-gate
15390Sstevel@tonic-gate top:
15400Sstevel@tonic-gate if ((err = zone_get_state(zone_name, &zstate)) != Z_OK) {
15412267Sdp zerror(zlogp, B_FALSE, "failed to get zone state: %s",
15420Sstevel@tonic-gate zonecfg_strerror(err));
15430Sstevel@tonic-gate goto out;
15440Sstevel@tonic-gate }
15450Sstevel@tonic-gate if ((doorfd = open(zone_door_path, O_CREAT|O_RDWR,
15460Sstevel@tonic-gate S_IREAD|S_IWRITE)) < 0) {
15470Sstevel@tonic-gate zerror(zlogp, B_TRUE, "failed to open %s", zone_door_path);
15480Sstevel@tonic-gate goto out;
15490Sstevel@tonic-gate }
15500Sstevel@tonic-gate if (fstat(doorfd, &st) < 0) {
15510Sstevel@tonic-gate zerror(zlogp, B_TRUE, "failed to stat %s", zone_door_path);
15520Sstevel@tonic-gate goto out;
15530Sstevel@tonic-gate }
15540Sstevel@tonic-gate /*
15550Sstevel@tonic-gate * Lock the file to synchronize with other zoneadmd
15560Sstevel@tonic-gate */
15570Sstevel@tonic-gate flock.l_type = F_WRLCK;
15580Sstevel@tonic-gate flock.l_whence = SEEK_SET;
15590Sstevel@tonic-gate flock.l_start = (off_t)0;
15600Sstevel@tonic-gate flock.l_len = (off_t)0;
15610Sstevel@tonic-gate if (fcntl(doorfd, F_SETLK, &flock) < 0) {
15620Sstevel@tonic-gate /*
15630Sstevel@tonic-gate * Someone else raced us here and grabbed the lock file
15640Sstevel@tonic-gate * first. A warning here is inappropriate since nothing
15650Sstevel@tonic-gate * went wrong.
15660Sstevel@tonic-gate */
15670Sstevel@tonic-gate goto out;
15680Sstevel@tonic-gate }
15690Sstevel@tonic-gate
15700Sstevel@tonic-gate if (strcmp(st.st_fstype, "namefs") == 0) {
15710Sstevel@tonic-gate struct door_info info;
15720Sstevel@tonic-gate
15730Sstevel@tonic-gate /*
15740Sstevel@tonic-gate * There is already something fattach()'ed to this file.
15750Sstevel@tonic-gate * Lets see what the door is up to.
15760Sstevel@tonic-gate */
15770Sstevel@tonic-gate if (door_info(doorfd, &info) == 0 && info.di_target != -1) {
15780Sstevel@tonic-gate /*
15790Sstevel@tonic-gate * Another zoneadmd process seems to be in
15800Sstevel@tonic-gate * control of the situation and we don't need to
15810Sstevel@tonic-gate * be here. A warning here is inappropriate
15820Sstevel@tonic-gate * since nothing went wrong.
15830Sstevel@tonic-gate *
15840Sstevel@tonic-gate * If the door has been revoked, the zoneadmd
15850Sstevel@tonic-gate * process currently managing the zone is going
15860Sstevel@tonic-gate * away. We'll return control to zoneadm(1m)
15870Sstevel@tonic-gate * which will try again (by which time zoneadmd
15880Sstevel@tonic-gate * will hopefully have exited).
15890Sstevel@tonic-gate */
15900Sstevel@tonic-gate goto out;
15910Sstevel@tonic-gate }
15920Sstevel@tonic-gate
15930Sstevel@tonic-gate /*
15940Sstevel@tonic-gate * If we got this far, there's a fattach(3c)'ed door
15950Sstevel@tonic-gate * that belongs to a process that has exited, which can
15960Sstevel@tonic-gate * happen if the previous zoneadmd died unexpectedly.
15970Sstevel@tonic-gate *
15980Sstevel@tonic-gate * Let user know that something is amiss, but that we can
15990Sstevel@tonic-gate * recover; if the zone is in the installed state, then don't
16000Sstevel@tonic-gate * message, since having a running zoneadmd isn't really
16010Sstevel@tonic-gate * expected/needed. We want to keep occurences of this message
16020Sstevel@tonic-gate * limited to times when zoneadmd is picking back up from a
16030Sstevel@tonic-gate * zoneadmd that died while the zone was in some non-trivial
16040Sstevel@tonic-gate * state.
16050Sstevel@tonic-gate */
16060Sstevel@tonic-gate if (zstate > ZONE_STATE_INSTALLED) {
16070Sstevel@tonic-gate zerror(zlogp, B_FALSE,
16080Sstevel@tonic-gate "zone '%s': WARNING: zone is in state '%s', but "
16090Sstevel@tonic-gate "zoneadmd does not appear to be available; "
16100Sstevel@tonic-gate "restarted zoneadmd to recover.",
16110Sstevel@tonic-gate zone_name, zone_state_str(zstate));
16120Sstevel@tonic-gate }
16130Sstevel@tonic-gate
16140Sstevel@tonic-gate (void) fdetach(zone_door_path);
16150Sstevel@tonic-gate (void) close(doorfd);
16160Sstevel@tonic-gate goto top;
16170Sstevel@tonic-gate }
16180Sstevel@tonic-gate ret = 0;
16190Sstevel@tonic-gate out:
16200Sstevel@tonic-gate (void) close(doorfd);
16210Sstevel@tonic-gate return (ret);
16220Sstevel@tonic-gate }
16230Sstevel@tonic-gate
16247370S<Gerald Jelinek> /*
16257370S<Gerald Jelinek> * Setup the brand's pre and post state change callbacks, as well as the
16267370S<Gerald Jelinek> * query callback, if any of these exist.
16277370S<Gerald Jelinek> */
16287370S<Gerald Jelinek> static int
brand_callback_init(brand_handle_t bh,char * zone_name)16297370S<Gerald Jelinek> brand_callback_init(brand_handle_t bh, char *zone_name)
16307370S<Gerald Jelinek> {
16317370S<Gerald Jelinek> char zpath[MAXPATHLEN];
16327370S<Gerald Jelinek>
16337370S<Gerald Jelinek> if (zone_get_zonepath(zone_name, zpath, sizeof (zpath)) != Z_OK)
16347370S<Gerald Jelinek> return (-1);
16357370S<Gerald Jelinek>
16367370S<Gerald Jelinek> (void) strlcpy(pre_statechg_hook, EXEC_PREFIX,
16377370S<Gerald Jelinek> sizeof (pre_statechg_hook));
16387370S<Gerald Jelinek>
16397370S<Gerald Jelinek> if (brand_get_prestatechange(bh, zone_name, zpath,
16407370S<Gerald Jelinek> pre_statechg_hook + EXEC_LEN,
16417370S<Gerald Jelinek> sizeof (pre_statechg_hook) - EXEC_LEN) != 0)
16427370S<Gerald Jelinek> return (-1);
16437370S<Gerald Jelinek>
16447370S<Gerald Jelinek> if (strlen(pre_statechg_hook) <= EXEC_LEN)
16457370S<Gerald Jelinek> pre_statechg_hook[0] = '\0';
16467370S<Gerald Jelinek>
16477370S<Gerald Jelinek> (void) strlcpy(post_statechg_hook, EXEC_PREFIX,
16487370S<Gerald Jelinek> sizeof (post_statechg_hook));
16497370S<Gerald Jelinek>
16507370S<Gerald Jelinek> if (brand_get_poststatechange(bh, zone_name, zpath,
16517370S<Gerald Jelinek> post_statechg_hook + EXEC_LEN,
16527370S<Gerald Jelinek> sizeof (post_statechg_hook) - EXEC_LEN) != 0)
16537370S<Gerald Jelinek> return (-1);
16547370S<Gerald Jelinek>
16557370S<Gerald Jelinek> if (strlen(post_statechg_hook) <= EXEC_LEN)
16567370S<Gerald Jelinek> post_statechg_hook[0] = '\0';
16577370S<Gerald Jelinek>
16587370S<Gerald Jelinek> (void) strlcpy(query_hook, EXEC_PREFIX,
16597370S<Gerald Jelinek> sizeof (query_hook));
16607370S<Gerald Jelinek>
16617370S<Gerald Jelinek> if (brand_get_query(bh, zone_name, zpath, query_hook + EXEC_LEN,
16627370S<Gerald Jelinek> sizeof (query_hook) - EXEC_LEN) != 0)
16637370S<Gerald Jelinek> return (-1);
16647370S<Gerald Jelinek>
16657370S<Gerald Jelinek> if (strlen(query_hook) <= EXEC_LEN)
16667370S<Gerald Jelinek> query_hook[0] = '\0';
16677370S<Gerald Jelinek>
16687370S<Gerald Jelinek> return (0);
16697370S<Gerald Jelinek> }
16707370S<Gerald Jelinek>
16710Sstevel@tonic-gate int
main(int argc,char * argv[])16720Sstevel@tonic-gate main(int argc, char *argv[])
16730Sstevel@tonic-gate {
16740Sstevel@tonic-gate int opt;
16750Sstevel@tonic-gate zoneid_t zid;
16760Sstevel@tonic-gate priv_set_t *privset;
16770Sstevel@tonic-gate zone_state_t zstate;
16780Sstevel@tonic-gate char parents_locale[MAXPATHLEN];
16792727Sedp brand_handle_t bh;
16800Sstevel@tonic-gate int err;
16810Sstevel@tonic-gate
16820Sstevel@tonic-gate pid_t pid;
16830Sstevel@tonic-gate sigset_t blockset;
16840Sstevel@tonic-gate sigset_t block_cld;
16850Sstevel@tonic-gate
16860Sstevel@tonic-gate struct {
16870Sstevel@tonic-gate sema_t sem;
16880Sstevel@tonic-gate int status;
16890Sstevel@tonic-gate zlog_t log;
16900Sstevel@tonic-gate } *shstate;
16910Sstevel@tonic-gate size_t shstatelen = getpagesize();
16920Sstevel@tonic-gate
16930Sstevel@tonic-gate zlog_t errlog;
16940Sstevel@tonic-gate zlog_t *zlogp;
16950Sstevel@tonic-gate
16962382Sdp int ctfd;
16972382Sdp
16980Sstevel@tonic-gate progname = get_execbasename(argv[0]);
16990Sstevel@tonic-gate
17000Sstevel@tonic-gate /*
17010Sstevel@tonic-gate * Make sure stderr is unbuffered
17020Sstevel@tonic-gate */
17030Sstevel@tonic-gate (void) setbuffer(stderr, NULL, 0);
17040Sstevel@tonic-gate
17050Sstevel@tonic-gate /*
17060Sstevel@tonic-gate * Get out of the way of mounted filesystems, since we will daemonize
17070Sstevel@tonic-gate * soon.
17080Sstevel@tonic-gate */
17090Sstevel@tonic-gate (void) chdir("/");
17100Sstevel@tonic-gate
17110Sstevel@tonic-gate /*
17120Sstevel@tonic-gate * Use the default system umask per PSARC 1998/110 rather than
17130Sstevel@tonic-gate * anything that may have been set by the caller.
17140Sstevel@tonic-gate */
17150Sstevel@tonic-gate (void) umask(CMASK);
17160Sstevel@tonic-gate
17170Sstevel@tonic-gate /*
17180Sstevel@tonic-gate * Initially we want to use our parent's locale.
17190Sstevel@tonic-gate */
17200Sstevel@tonic-gate (void) setlocale(LC_ALL, "");
17210Sstevel@tonic-gate (void) textdomain(TEXT_DOMAIN);
17220Sstevel@tonic-gate (void) strlcpy(parents_locale, setlocale(LC_MESSAGES, NULL),
17230Sstevel@tonic-gate sizeof (parents_locale));
17240Sstevel@tonic-gate
17250Sstevel@tonic-gate /*
17260Sstevel@tonic-gate * This zlog_t is used for writing to stderr
17270Sstevel@tonic-gate */
17280Sstevel@tonic-gate errlog.logfile = stderr;
17290Sstevel@tonic-gate errlog.buflen = errlog.loglen = 0;
17300Sstevel@tonic-gate errlog.buf = errlog.log = NULL;
17310Sstevel@tonic-gate errlog.locale = parents_locale;
17320Sstevel@tonic-gate
17330Sstevel@tonic-gate /*
17340Sstevel@tonic-gate * We start off writing to stderr until we're ready to daemonize.
17350Sstevel@tonic-gate */
17360Sstevel@tonic-gate zlogp = &errlog;
17370Sstevel@tonic-gate
17380Sstevel@tonic-gate /*
17390Sstevel@tonic-gate * Process options.
17400Sstevel@tonic-gate */
1741766Scarlsonj while ((opt = getopt(argc, argv, "R:z:")) != EOF) {
17420Sstevel@tonic-gate switch (opt) {
1743766Scarlsonj case 'R':
1744766Scarlsonj zonecfg_set_root(optarg);
1745766Scarlsonj break;
17460Sstevel@tonic-gate case 'z':
17470Sstevel@tonic-gate zone_name = optarg;
17480Sstevel@tonic-gate break;
17490Sstevel@tonic-gate default:
17500Sstevel@tonic-gate usage();
17510Sstevel@tonic-gate }
17520Sstevel@tonic-gate }
17530Sstevel@tonic-gate
17540Sstevel@tonic-gate if (zone_name == NULL)
17550Sstevel@tonic-gate usage();
17560Sstevel@tonic-gate
17570Sstevel@tonic-gate /*
17580Sstevel@tonic-gate * Because usage() prints directly to stderr, it has gettext()
17590Sstevel@tonic-gate * wrapping, which depends on the locale. But since zerror() calls
17600Sstevel@tonic-gate * localize() which tweaks the locale, it is not safe to call zerror()
17610Sstevel@tonic-gate * until after the last call to usage(). Fortunately, the last call
17620Sstevel@tonic-gate * to usage() is just above and the first call to zerror() is just
17630Sstevel@tonic-gate * below. Don't mess this up.
17640Sstevel@tonic-gate */
17650Sstevel@tonic-gate if (strcmp(zone_name, GLOBAL_ZONENAME) == 0) {
17660Sstevel@tonic-gate zerror(zlogp, B_FALSE, "cannot manage the %s zone",
17670Sstevel@tonic-gate GLOBAL_ZONENAME);
17680Sstevel@tonic-gate return (1);
17690Sstevel@tonic-gate }
17700Sstevel@tonic-gate
17710Sstevel@tonic-gate if (zone_get_id(zone_name, &zid) != 0) {
17722267Sdp zerror(zlogp, B_FALSE, "could not manage %s: %s", zone_name,
17730Sstevel@tonic-gate zonecfg_strerror(Z_NO_ZONE));
17740Sstevel@tonic-gate return (1);
17750Sstevel@tonic-gate }
17760Sstevel@tonic-gate
17770Sstevel@tonic-gate if ((err = zone_get_state(zone_name, &zstate)) != Z_OK) {
17782267Sdp zerror(zlogp, B_FALSE, "failed to get zone state: %s",
17790Sstevel@tonic-gate zonecfg_strerror(err));
17800Sstevel@tonic-gate return (1);
17810Sstevel@tonic-gate }
17822712Snn35248 if (zstate < ZONE_STATE_INCOMPLETE) {
17830Sstevel@tonic-gate zerror(zlogp, B_FALSE,
17840Sstevel@tonic-gate "cannot manage a zone which is in state '%s'",
17850Sstevel@tonic-gate zone_state_str(zstate));
17860Sstevel@tonic-gate return (1);
17870Sstevel@tonic-gate }
17880Sstevel@tonic-gate
178910943SEdward.Pilatowicz@Sun.COM if (zonecfg_default_brand(default_brand,
179010943SEdward.Pilatowicz@Sun.COM sizeof (default_brand)) != Z_OK) {
179110943SEdward.Pilatowicz@Sun.COM zerror(zlogp, B_FALSE, "unable to determine default brand");
179210943SEdward.Pilatowicz@Sun.COM return (1);
179310943SEdward.Pilatowicz@Sun.COM }
179410943SEdward.Pilatowicz@Sun.COM
17952712Snn35248 /* Get a handle to the brand info for this zone */
179610796SStephen.Lawrence@Sun.COM if (zone_get_brand(zone_name, brand_name, sizeof (brand_name))
179710796SStephen.Lawrence@Sun.COM != Z_OK) {
17982712Snn35248 zerror(zlogp, B_FALSE, "unable to determine zone brand");
17992712Snn35248 return (1);
18002712Snn35248 }
180110796SStephen.Lawrence@Sun.COM zone_isnative = (strcmp(brand_name, NATIVE_BRAND_NAME) == 0);
18028905SRic.Aleshire@Sun.COM zone_islabeled = (strcmp(brand_name, LABELED_BRAND_NAME) == 0);
18037370S<Gerald Jelinek>
180410796SStephen.Lawrence@Sun.COM /*
180510796SStephen.Lawrence@Sun.COM * In the alternate root environment, the only supported
180610796SStephen.Lawrence@Sun.COM * operations are mount and unmount. In this case, just treat
180710796SStephen.Lawrence@Sun.COM * the zone as native if it is cluster. Cluster zones can be
180810796SStephen.Lawrence@Sun.COM * native for the purpose of LU or upgrade, and the cluster
180910796SStephen.Lawrence@Sun.COM * brand may not exist in the miniroot (such as in net install
181010796SStephen.Lawrence@Sun.COM * upgrade).
181110796SStephen.Lawrence@Sun.COM */
181210796SStephen.Lawrence@Sun.COM if (strcmp(brand_name, CLUSTER_BRAND_NAME) == 0) {
181310796SStephen.Lawrence@Sun.COM zone_iscluster = B_TRUE;
181410796SStephen.Lawrence@Sun.COM if (zonecfg_in_alt_root()) {
181510943SEdward.Pilatowicz@Sun.COM (void) strlcpy(brand_name, default_brand,
181610796SStephen.Lawrence@Sun.COM sizeof (brand_name));
181710796SStephen.Lawrence@Sun.COM }
181810796SStephen.Lawrence@Sun.COM } else {
181910796SStephen.Lawrence@Sun.COM zone_iscluster = B_FALSE;
182010796SStephen.Lawrence@Sun.COM }
182110796SStephen.Lawrence@Sun.COM
182210796SStephen.Lawrence@Sun.COM if ((bh = brand_open(brand_name)) == NULL) {
182310796SStephen.Lawrence@Sun.COM zerror(zlogp, B_FALSE, "unable to open zone brand");
182410796SStephen.Lawrence@Sun.COM return (1);
182510796SStephen.Lawrence@Sun.COM }
182610796SStephen.Lawrence@Sun.COM
18277370S<Gerald Jelinek> /* Get state change brand hooks. */
18287370S<Gerald Jelinek> if (brand_callback_init(bh, zone_name) == -1) {
18297370S<Gerald Jelinek> zerror(zlogp, B_TRUE,
18307370S<Gerald Jelinek> "failed to initialize brand state change hooks");
18317370S<Gerald Jelinek> brand_close(bh);
18327370S<Gerald Jelinek> return (1);
18337370S<Gerald Jelinek> }
18347370S<Gerald Jelinek>
18352727Sedp brand_close(bh);
18362712Snn35248
18370Sstevel@tonic-gate /*
18380Sstevel@tonic-gate * Check that we have all privileges. It would be nice to pare
18390Sstevel@tonic-gate * this down, but this is at least a first cut.
18400Sstevel@tonic-gate */
18410Sstevel@tonic-gate if ((privset = priv_allocset()) == NULL) {
18420Sstevel@tonic-gate zerror(zlogp, B_TRUE, "%s failed", "priv_allocset");
18430Sstevel@tonic-gate return (1);
18440Sstevel@tonic-gate }
18450Sstevel@tonic-gate
18460Sstevel@tonic-gate if (getppriv(PRIV_EFFECTIVE, privset) != 0) {
18470Sstevel@tonic-gate zerror(zlogp, B_TRUE, "%s failed", "getppriv");
18480Sstevel@tonic-gate priv_freeset(privset);
18490Sstevel@tonic-gate return (1);
18500Sstevel@tonic-gate }
18510Sstevel@tonic-gate
18520Sstevel@tonic-gate if (priv_isfullset(privset) == B_FALSE) {
18530Sstevel@tonic-gate zerror(zlogp, B_FALSE, "You lack sufficient privilege to "
18542267Sdp "run this command (all privs required)");
18550Sstevel@tonic-gate priv_freeset(privset);
18560Sstevel@tonic-gate return (1);
18570Sstevel@tonic-gate }
18580Sstevel@tonic-gate priv_freeset(privset);
18590Sstevel@tonic-gate
18600Sstevel@tonic-gate if (mkzonedir(zlogp) != 0)
18610Sstevel@tonic-gate return (1);
18620Sstevel@tonic-gate
18630Sstevel@tonic-gate /*
18640Sstevel@tonic-gate * Pre-fork: setup shared state
18650Sstevel@tonic-gate */
18660Sstevel@tonic-gate if ((shstate = (void *)mmap(NULL, shstatelen,
18670Sstevel@tonic-gate PROT_READ|PROT_WRITE, MAP_SHARED|MAP_ANON, -1, (off_t)0)) ==
18680Sstevel@tonic-gate MAP_FAILED) {
18690Sstevel@tonic-gate zerror(zlogp, B_TRUE, "%s failed", "mmap");
18700Sstevel@tonic-gate return (1);
18710Sstevel@tonic-gate }
18720Sstevel@tonic-gate if (sema_init(&shstate->sem, 0, USYNC_PROCESS, NULL) != 0) {
18730Sstevel@tonic-gate zerror(zlogp, B_TRUE, "%s failed", "sema_init()");
18740Sstevel@tonic-gate (void) munmap((char *)shstate, shstatelen);
18750Sstevel@tonic-gate return (1);
18760Sstevel@tonic-gate }
18770Sstevel@tonic-gate shstate->log.logfile = NULL;
18780Sstevel@tonic-gate shstate->log.buflen = shstatelen - sizeof (*shstate);
18790Sstevel@tonic-gate shstate->log.loglen = shstate->log.buflen;
18800Sstevel@tonic-gate shstate->log.buf = (char *)shstate + sizeof (*shstate);
18810Sstevel@tonic-gate shstate->log.log = shstate->log.buf;
18820Sstevel@tonic-gate shstate->log.locale = parents_locale;
18830Sstevel@tonic-gate shstate->status = -1;
18840Sstevel@tonic-gate
18850Sstevel@tonic-gate /*
18860Sstevel@tonic-gate * We need a SIGCHLD handler so the sema_wait() below will wake
18870Sstevel@tonic-gate * up if the child dies without doing a sema_post().
18880Sstevel@tonic-gate */
18890Sstevel@tonic-gate (void) sigset(SIGCHLD, sigchld);
18900Sstevel@tonic-gate /*
18910Sstevel@tonic-gate * We must mask SIGCHLD until after we've coped with the fork
18920Sstevel@tonic-gate * sufficiently to deal with it; otherwise we can race and
18930Sstevel@tonic-gate * receive the signal before pid has been initialized
18940Sstevel@tonic-gate * (yes, this really happens).
18950Sstevel@tonic-gate */
18960Sstevel@tonic-gate (void) sigemptyset(&block_cld);
18970Sstevel@tonic-gate (void) sigaddset(&block_cld, SIGCHLD);
18980Sstevel@tonic-gate (void) sigprocmask(SIG_BLOCK, &block_cld, NULL);
18990Sstevel@tonic-gate
19002382Sdp if ((ctfd = init_template()) == -1) {
19012382Sdp zerror(zlogp, B_TRUE, "failed to create contract");
19022382Sdp return (1);
19032382Sdp }
19042382Sdp
19050Sstevel@tonic-gate /*
19060Sstevel@tonic-gate * Do not let another thread localize a message while we are forking.
19070Sstevel@tonic-gate */
19080Sstevel@tonic-gate (void) mutex_lock(&msglock);
19090Sstevel@tonic-gate pid = fork();
19100Sstevel@tonic-gate (void) mutex_unlock(&msglock);
19112382Sdp
19122382Sdp /*
19132382Sdp * In all cases (parent, child, and in the event of an error) we
19142382Sdp * don't want to cause creation of contracts on subsequent fork()s.
19152382Sdp */
19162382Sdp (void) ct_tmpl_clear(ctfd);
19172382Sdp (void) close(ctfd);
19182382Sdp
19190Sstevel@tonic-gate if (pid == -1) {
19200Sstevel@tonic-gate zerror(zlogp, B_TRUE, "could not fork");
19210Sstevel@tonic-gate return (1);
19220Sstevel@tonic-gate
19230Sstevel@tonic-gate } else if (pid > 0) { /* parent */
19240Sstevel@tonic-gate (void) sigprocmask(SIG_UNBLOCK, &block_cld, NULL);
19250Sstevel@tonic-gate /*
19260Sstevel@tonic-gate * This marks a window of vulnerability in which we receive
19270Sstevel@tonic-gate * the SIGCLD before falling into sema_wait (normally we would
19280Sstevel@tonic-gate * get woken up from sema_wait with EINTR upon receipt of
19290Sstevel@tonic-gate * SIGCLD). So we may need to use some other scheme like
19300Sstevel@tonic-gate * sema_posting in the sigcld handler.
19310Sstevel@tonic-gate * blech
19320Sstevel@tonic-gate */
19330Sstevel@tonic-gate (void) sema_wait(&shstate->sem);
19340Sstevel@tonic-gate (void) sema_destroy(&shstate->sem);
19350Sstevel@tonic-gate if (shstate->status != 0)
19360Sstevel@tonic-gate (void) waitpid(pid, NULL, WNOHANG);
19370Sstevel@tonic-gate /*
19380Sstevel@tonic-gate * It's ok if we die with SIGPIPE. It's not like we could have
19390Sstevel@tonic-gate * done anything about it.
19400Sstevel@tonic-gate */
19410Sstevel@tonic-gate (void) fprintf(stderr, "%s", shstate->log.buf);
19420Sstevel@tonic-gate _exit(shstate->status == 0 ? 0 : 1);
19430Sstevel@tonic-gate }
19440Sstevel@tonic-gate
19450Sstevel@tonic-gate /*
19460Sstevel@tonic-gate * The child charges on.
19470Sstevel@tonic-gate */
19480Sstevel@tonic-gate (void) sigset(SIGCHLD, SIG_DFL);
19490Sstevel@tonic-gate (void) sigprocmask(SIG_UNBLOCK, &block_cld, NULL);
19500Sstevel@tonic-gate
19510Sstevel@tonic-gate /*
19520Sstevel@tonic-gate * SIGPIPE can be delivered if we write to a socket for which the
19530Sstevel@tonic-gate * peer endpoint is gone. That can lead to too-early termination
19540Sstevel@tonic-gate * of zoneadmd, and that's not good eats.
19550Sstevel@tonic-gate */
19560Sstevel@tonic-gate (void) sigset(SIGPIPE, SIG_IGN);
19570Sstevel@tonic-gate /*
19580Sstevel@tonic-gate * Stop using stderr
19590Sstevel@tonic-gate */
19600Sstevel@tonic-gate zlogp = &shstate->log;
19610Sstevel@tonic-gate
19620Sstevel@tonic-gate /*
19630Sstevel@tonic-gate * We don't need stdout/stderr from now on.
19640Sstevel@tonic-gate */
19650Sstevel@tonic-gate closefrom(0);
19660Sstevel@tonic-gate
19670Sstevel@tonic-gate /*
19680Sstevel@tonic-gate * Initialize the syslog zlog_t. This needs to be done after
19690Sstevel@tonic-gate * the call to closefrom().
19700Sstevel@tonic-gate */
19710Sstevel@tonic-gate logsys.buf = logsys.log = NULL;
19720Sstevel@tonic-gate logsys.buflen = logsys.loglen = 0;
19730Sstevel@tonic-gate logsys.logfile = NULL;
19740Sstevel@tonic-gate logsys.locale = DEFAULT_LOCALE;
19750Sstevel@tonic-gate
19760Sstevel@tonic-gate openlog("zoneadmd", LOG_PID, LOG_DAEMON);
19770Sstevel@tonic-gate
19780Sstevel@tonic-gate /*
19790Sstevel@tonic-gate * The eventstream is used to publish state changes in the zone
19800Sstevel@tonic-gate * from the door threads to the console I/O poller.
19810Sstevel@tonic-gate */
19820Sstevel@tonic-gate if (eventstream_init() == -1) {
19830Sstevel@tonic-gate zerror(zlogp, B_TRUE, "unable to create eventstream");
19840Sstevel@tonic-gate goto child_out;
19850Sstevel@tonic-gate }
19860Sstevel@tonic-gate
19870Sstevel@tonic-gate (void) snprintf(zone_door_path, sizeof (zone_door_path),
1988766Scarlsonj "%s" ZONE_DOOR_PATH, zonecfg_get_root(), zone_name);
19890Sstevel@tonic-gate
19900Sstevel@tonic-gate /*
19910Sstevel@tonic-gate * See if another zoneadmd is running for this zone. If not, then we
19920Sstevel@tonic-gate * can now modify system state.
19930Sstevel@tonic-gate */
19940Sstevel@tonic-gate if (make_daemon_exclusive(zlogp) == -1)
19950Sstevel@tonic-gate goto child_out;
19960Sstevel@tonic-gate
19970Sstevel@tonic-gate
19980Sstevel@tonic-gate /*
19990Sstevel@tonic-gate * Create/join a new session; we need to be careful of what we do with
20000Sstevel@tonic-gate * the console from now on so we don't end up being the session leader
20010Sstevel@tonic-gate * for the terminal we're going to be handing out.
20020Sstevel@tonic-gate */
20030Sstevel@tonic-gate (void) setsid();
20040Sstevel@tonic-gate
20050Sstevel@tonic-gate /*
20060Sstevel@tonic-gate * This thread shouldn't be receiving any signals; in particular,
20070Sstevel@tonic-gate * SIGCHLD should be received by the thread doing the fork().
20080Sstevel@tonic-gate */
20090Sstevel@tonic-gate (void) sigfillset(&blockset);
20100Sstevel@tonic-gate (void) thr_sigsetmask(SIG_BLOCK, &blockset, NULL);
20110Sstevel@tonic-gate
20120Sstevel@tonic-gate /*
20130Sstevel@tonic-gate * Setup the console device and get ready to serve the console;
20140Sstevel@tonic-gate * once this has completed, we're ready to let console clients
20150Sstevel@tonic-gate * make an attempt to connect (they will block until
20160Sstevel@tonic-gate * serve_console_sock() below gets called, and any pending
20170Sstevel@tonic-gate * connection is accept()ed).
20180Sstevel@tonic-gate */
20198770SJordan.Vaughan@Sun.com if (!zonecfg_in_alt_root() && init_console(zlogp) < 0)
20200Sstevel@tonic-gate goto child_out;
20210Sstevel@tonic-gate
20220Sstevel@tonic-gate /*
20230Sstevel@tonic-gate * Take the lock now, so that when the door server gets going, we
20240Sstevel@tonic-gate * are guaranteed that it won't take a request until we are sure
20250Sstevel@tonic-gate * that everything is completely set up. See the child_out: label
20260Sstevel@tonic-gate * below to see why this matters.
20270Sstevel@tonic-gate */
20280Sstevel@tonic-gate (void) mutex_lock(&lock);
20290Sstevel@tonic-gate
2030766Scarlsonj /* Init semaphore for scratch zones. */
2031766Scarlsonj if (sema_init(&scratch_sem, 0, USYNC_THREAD, NULL) == -1) {
2032766Scarlsonj zerror(zlogp, B_TRUE,
2033766Scarlsonj "failed to initialize semaphore for scratch zone");
2034766Scarlsonj goto child_out;
2035766Scarlsonj }
2036766Scarlsonj
20378453SAnurag.Maskey@Sun.COM /* open the dladm handle */
20388453SAnurag.Maskey@Sun.COM if (dladm_open(&dld_handle) != DLADM_STATUS_OK) {
20398453SAnurag.Maskey@Sun.COM zerror(zlogp, B_FALSE, "failed to open dladm handle");
20408453SAnurag.Maskey@Sun.COM goto child_out;
20418453SAnurag.Maskey@Sun.COM }
20428453SAnurag.Maskey@Sun.COM
20430Sstevel@tonic-gate /*
20440Sstevel@tonic-gate * Note: door setup must occur *after* the console is setup.
20450Sstevel@tonic-gate * This is so that as zlogin tests the door to see if zoneadmd
20460Sstevel@tonic-gate * is ready yet, we know that the console will get serviced
20470Sstevel@tonic-gate * once door_info() indicates that the door is "up".
20480Sstevel@tonic-gate */
20490Sstevel@tonic-gate if (setup_door(zlogp) == -1)
20500Sstevel@tonic-gate goto child_out;
20510Sstevel@tonic-gate
20520Sstevel@tonic-gate /*
20530Sstevel@tonic-gate * Things seem OK so far; tell the parent process that we're done
20540Sstevel@tonic-gate * with setup tasks. This will cause the parent to exit, signalling
20550Sstevel@tonic-gate * to zoneadm, zlogin, or whatever forked it that we are ready to
20560Sstevel@tonic-gate * service requests.
20570Sstevel@tonic-gate */
20580Sstevel@tonic-gate shstate->status = 0;
20590Sstevel@tonic-gate (void) sema_post(&shstate->sem);
20600Sstevel@tonic-gate (void) munmap((char *)shstate, shstatelen);
20610Sstevel@tonic-gate shstate = NULL;
20620Sstevel@tonic-gate
20630Sstevel@tonic-gate (void) mutex_unlock(&lock);
20640Sstevel@tonic-gate
20650Sstevel@tonic-gate /*
20660Sstevel@tonic-gate * zlogp is now invalid, so reset it to the syslog logger.
20670Sstevel@tonic-gate */
20680Sstevel@tonic-gate zlogp = &logsys;
20690Sstevel@tonic-gate
20700Sstevel@tonic-gate /*
20710Sstevel@tonic-gate * Now that we are free of any parents, switch to the default locale.
20720Sstevel@tonic-gate */
20730Sstevel@tonic-gate (void) setlocale(LC_ALL, DEFAULT_LOCALE);
20740Sstevel@tonic-gate
20750Sstevel@tonic-gate /*
20760Sstevel@tonic-gate * At this point the setup portion of main() is basically done, so
20770Sstevel@tonic-gate * we reuse this thread to manage the zone console. When
20780Sstevel@tonic-gate * serve_console() has returned, we are past the point of no return
20790Sstevel@tonic-gate * in the life of this zoneadmd.
20800Sstevel@tonic-gate */
2081766Scarlsonj if (zonecfg_in_alt_root()) {
2082766Scarlsonj /*
2083766Scarlsonj * This is just awful, but mounted scratch zones don't (and
2084766Scarlsonj * can't) have consoles. We just wait for unmount instead.
2085766Scarlsonj */
2086766Scarlsonj while (sema_wait(&scratch_sem) == EINTR)
2087766Scarlsonj ;
2088766Scarlsonj } else {
2089766Scarlsonj serve_console(zlogp);
2090766Scarlsonj assert(in_death_throes);
2091766Scarlsonj }
20920Sstevel@tonic-gate
20930Sstevel@tonic-gate /*
20940Sstevel@tonic-gate * This is the next-to-last part of the exit interlock. Upon calling
20950Sstevel@tonic-gate * fdetach(), the door will go unreferenced; once any
20960Sstevel@tonic-gate * outstanding requests (like the door thread doing Z_HALT) are
20970Sstevel@tonic-gate * done, the door will get an UNREF notification; when it handles
209810944SEdward.Pilatowicz@Sun.COM * the UNREF, the door server will cause the exit. It's possible
209910944SEdward.Pilatowicz@Sun.COM * that fdetach() can fail because the file is in use, in which
210010944SEdward.Pilatowicz@Sun.COM * case we'll retry the operation.
21010Sstevel@tonic-gate */
21020Sstevel@tonic-gate assert(!MUTEX_HELD(&lock));
210310944SEdward.Pilatowicz@Sun.COM for (;;) {
210410944SEdward.Pilatowicz@Sun.COM if ((fdetach(zone_door_path) == 0) || (errno != EBUSY))
210510944SEdward.Pilatowicz@Sun.COM break;
210610944SEdward.Pilatowicz@Sun.COM yield();
210710944SEdward.Pilatowicz@Sun.COM }
21088453SAnurag.Maskey@Sun.COM
21090Sstevel@tonic-gate for (;;)
21100Sstevel@tonic-gate (void) pause();
21110Sstevel@tonic-gate
21120Sstevel@tonic-gate child_out:
21130Sstevel@tonic-gate assert(pid == 0);
21140Sstevel@tonic-gate if (shstate != NULL) {
21150Sstevel@tonic-gate shstate->status = -1;
21160Sstevel@tonic-gate (void) sema_post(&shstate->sem);
21170Sstevel@tonic-gate (void) munmap((char *)shstate, shstatelen);
21180Sstevel@tonic-gate }
21190Sstevel@tonic-gate
21200Sstevel@tonic-gate /*
21210Sstevel@tonic-gate * This might trigger an unref notification, but if so,
21220Sstevel@tonic-gate * we are still holding the lock, so our call to exit will
21230Sstevel@tonic-gate * ultimately win the race and will publish the right exit
21240Sstevel@tonic-gate * code.
21250Sstevel@tonic-gate */
21260Sstevel@tonic-gate if (zone_door != -1) {
21270Sstevel@tonic-gate assert(MUTEX_HELD(&lock));
21280Sstevel@tonic-gate (void) door_revoke(zone_door);
21290Sstevel@tonic-gate (void) fdetach(zone_door_path);
21300Sstevel@tonic-gate }
21318453SAnurag.Maskey@Sun.COM
21328453SAnurag.Maskey@Sun.COM if (dld_handle != NULL)
21338453SAnurag.Maskey@Sun.COM dladm_close(dld_handle);
21348453SAnurag.Maskey@Sun.COM
21350Sstevel@tonic-gate return (1); /* return from main() forcibly exits an MT process */
21360Sstevel@tonic-gate }
2137