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 /* 231645Scomay * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 240Sstevel@tonic-gate * Use is subject to license terms. 250Sstevel@tonic-gate */ 260Sstevel@tonic-gate 270Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 280Sstevel@tonic-gate 290Sstevel@tonic-gate /* 300Sstevel@tonic-gate * zoneadmd manages zones; one zoneadmd process is launched for each 310Sstevel@tonic-gate * non-global zone on the system. This daemon juggles four jobs: 320Sstevel@tonic-gate * 330Sstevel@tonic-gate * - Implement setup and teardown of the zone "virtual platform": mount and 340Sstevel@tonic-gate * unmount filesystems; create and destroy network interfaces; communicate 350Sstevel@tonic-gate * with devfsadmd to lay out devices for the zone; instantiate the zone 360Sstevel@tonic-gate * console device; configure process runtime attributes such as resource 370Sstevel@tonic-gate * controls, pool bindings, fine-grained privileges. 380Sstevel@tonic-gate * 390Sstevel@tonic-gate * - Launch the zone's init(1M) process. 400Sstevel@tonic-gate * 410Sstevel@tonic-gate * - Implement a door server; clients (like zoneadm) connect to the door 420Sstevel@tonic-gate * server and request zone state changes. The kernel is also a client of 430Sstevel@tonic-gate * this door server. A request to halt or reboot the zone which originates 440Sstevel@tonic-gate * *inside* the zone results in a door upcall from the kernel into zoneadmd. 450Sstevel@tonic-gate * 460Sstevel@tonic-gate * One minor problem is that messages emitted by zoneadmd need to be passed 470Sstevel@tonic-gate * back to the zoneadm process making the request. These messages need to 480Sstevel@tonic-gate * be rendered in the client's locale; so, this is passed in as part of the 490Sstevel@tonic-gate * request. The exception is the kernel upcall to zoneadmd, in which case 500Sstevel@tonic-gate * messages are syslog'd. 510Sstevel@tonic-gate * 520Sstevel@tonic-gate * To make all of this work, the Makefile adds -a to xgettext to extract *all* 530Sstevel@tonic-gate * strings, and an exclusion file (zoneadmd.xcl) is used to exclude those 540Sstevel@tonic-gate * strings which do not need to be translated. 550Sstevel@tonic-gate * 560Sstevel@tonic-gate * - Act as a console server for zlogin -C processes; see comments in zcons.c 570Sstevel@tonic-gate * for more information about the zone console architecture. 580Sstevel@tonic-gate * 590Sstevel@tonic-gate * DESIGN NOTES 600Sstevel@tonic-gate * 610Sstevel@tonic-gate * Restart: 620Sstevel@tonic-gate * A chief design constraint of zoneadmd is that it should be restartable in 630Sstevel@tonic-gate * the case that the administrator kills it off, or it suffers a fatal error, 640Sstevel@tonic-gate * without the running zone being impacted; this is akin to being able to 650Sstevel@tonic-gate * reboot the service processor of a server without affecting the OS instance. 660Sstevel@tonic-gate */ 670Sstevel@tonic-gate 680Sstevel@tonic-gate #include <sys/param.h> 690Sstevel@tonic-gate #include <sys/mman.h> 700Sstevel@tonic-gate #include <sys/types.h> 710Sstevel@tonic-gate #include <sys/stat.h> 720Sstevel@tonic-gate #include <sys/sysmacros.h> 730Sstevel@tonic-gate 740Sstevel@tonic-gate #include <bsm/adt.h> 750Sstevel@tonic-gate #include <bsm/adt_event.h> 760Sstevel@tonic-gate 770Sstevel@tonic-gate #include <alloca.h> 780Sstevel@tonic-gate #include <assert.h> 790Sstevel@tonic-gate #include <errno.h> 800Sstevel@tonic-gate #include <door.h> 810Sstevel@tonic-gate #include <fcntl.h> 820Sstevel@tonic-gate #include <locale.h> 830Sstevel@tonic-gate #include <signal.h> 840Sstevel@tonic-gate #include <stdarg.h> 850Sstevel@tonic-gate #include <stdio.h> 860Sstevel@tonic-gate #include <stdlib.h> 870Sstevel@tonic-gate #include <string.h> 880Sstevel@tonic-gate #include <strings.h> 890Sstevel@tonic-gate #include <synch.h> 900Sstevel@tonic-gate #include <syslog.h> 910Sstevel@tonic-gate #include <thread.h> 920Sstevel@tonic-gate #include <unistd.h> 930Sstevel@tonic-gate #include <wait.h> 940Sstevel@tonic-gate #include <limits.h> 950Sstevel@tonic-gate #include <zone.h> 962712Snn35248 #include <libbrand.h> 970Sstevel@tonic-gate #include <libcontract.h> 980Sstevel@tonic-gate #include <libcontract_priv.h> 990Sstevel@tonic-gate #include <sys/contract/process.h> 1000Sstevel@tonic-gate #include <sys/ctfs.h> 1010Sstevel@tonic-gate 1020Sstevel@tonic-gate #include <libzonecfg.h> 1030Sstevel@tonic-gate #include "zoneadmd.h" 1040Sstevel@tonic-gate 1050Sstevel@tonic-gate static char *progname; 1060Sstevel@tonic-gate char *zone_name; /* zone which we are managing */ 1072712Snn35248 char brand_name[MAXNAMELEN]; 1082712Snn35248 boolean_t zone_isnative; 109766Scarlsonj static zoneid_t zone_id; 1100Sstevel@tonic-gate 1112611Svp157776 zlog_t logsys; 1120Sstevel@tonic-gate 1130Sstevel@tonic-gate mutex_t lock = DEFAULTMUTEX; /* to serialize stuff */ 1140Sstevel@tonic-gate mutex_t msglock = DEFAULTMUTEX; /* for calling setlocale() */ 1150Sstevel@tonic-gate 116766Scarlsonj static sema_t scratch_sem; /* for scratch zones */ 117766Scarlsonj 1180Sstevel@tonic-gate static char zone_door_path[MAXPATHLEN]; 1190Sstevel@tonic-gate static int zone_door = -1; 1200Sstevel@tonic-gate 1210Sstevel@tonic-gate boolean_t in_death_throes = B_FALSE; /* daemon is dying */ 1220Sstevel@tonic-gate boolean_t bringup_failure_recovery = B_FALSE; /* ignore certain failures */ 1230Sstevel@tonic-gate 1240Sstevel@tonic-gate #if !defined(TEXT_DOMAIN) /* should be defined by cc -D */ 1250Sstevel@tonic-gate #define TEXT_DOMAIN "SYS_TEST" /* Use this only if it wasn't */ 1260Sstevel@tonic-gate #endif 1270Sstevel@tonic-gate 1280Sstevel@tonic-gate #define DEFAULT_LOCALE "C" 1290Sstevel@tonic-gate 130766Scarlsonj static const char * 131766Scarlsonj z_cmd_name(zone_cmd_t zcmd) 132766Scarlsonj { 133766Scarlsonj /* This list needs to match the enum in sys/zone.h */ 134766Scarlsonj static const char *zcmdstr[] = { 1352712Snn35248 "ready", "boot", "forceboot", "reboot", "halt", 1362712Snn35248 "note_uninstalling", "mount", "forcemount", "unmount" 137766Scarlsonj }; 138766Scarlsonj 139766Scarlsonj if (zcmd >= sizeof (zcmdstr) / sizeof (*zcmdstr)) 140766Scarlsonj return ("unknown"); 141766Scarlsonj else 142766Scarlsonj return (zcmdstr[(int)zcmd]); 143766Scarlsonj } 144766Scarlsonj 1450Sstevel@tonic-gate static char * 1460Sstevel@tonic-gate get_execbasename(char *execfullname) 1470Sstevel@tonic-gate { 1480Sstevel@tonic-gate char *last_slash, *execbasename; 1490Sstevel@tonic-gate 1500Sstevel@tonic-gate /* guard against '/' at end of command invocation */ 1510Sstevel@tonic-gate for (;;) { 1520Sstevel@tonic-gate last_slash = strrchr(execfullname, '/'); 1530Sstevel@tonic-gate if (last_slash == NULL) { 1540Sstevel@tonic-gate execbasename = execfullname; 1550Sstevel@tonic-gate break; 1560Sstevel@tonic-gate } else { 1570Sstevel@tonic-gate execbasename = last_slash + 1; 1580Sstevel@tonic-gate if (*execbasename == '\0') { 1590Sstevel@tonic-gate *last_slash = '\0'; 1600Sstevel@tonic-gate continue; 1610Sstevel@tonic-gate } 1620Sstevel@tonic-gate break; 1630Sstevel@tonic-gate } 1640Sstevel@tonic-gate } 1650Sstevel@tonic-gate return (execbasename); 1660Sstevel@tonic-gate } 1670Sstevel@tonic-gate 1680Sstevel@tonic-gate static void 1690Sstevel@tonic-gate usage(void) 1700Sstevel@tonic-gate { 1710Sstevel@tonic-gate (void) fprintf(stderr, gettext("Usage: %s -z zonename\n"), progname); 1720Sstevel@tonic-gate (void) fprintf(stderr, 1730Sstevel@tonic-gate gettext("\tNote: %s should not be run directly.\n"), progname); 1740Sstevel@tonic-gate exit(2); 1750Sstevel@tonic-gate } 1760Sstevel@tonic-gate 1770Sstevel@tonic-gate /* ARGSUSED */ 1780Sstevel@tonic-gate static void 1790Sstevel@tonic-gate sigchld(int sig) 1800Sstevel@tonic-gate { 1810Sstevel@tonic-gate } 1820Sstevel@tonic-gate 1830Sstevel@tonic-gate char * 1840Sstevel@tonic-gate localize_msg(char *locale, const char *msg) 1850Sstevel@tonic-gate { 1860Sstevel@tonic-gate char *out; 1870Sstevel@tonic-gate 1880Sstevel@tonic-gate (void) mutex_lock(&msglock); 1890Sstevel@tonic-gate (void) setlocale(LC_MESSAGES, locale); 1900Sstevel@tonic-gate out = gettext(msg); 1910Sstevel@tonic-gate (void) setlocale(LC_MESSAGES, DEFAULT_LOCALE); 1920Sstevel@tonic-gate (void) mutex_unlock(&msglock); 1930Sstevel@tonic-gate return (out); 1940Sstevel@tonic-gate } 1950Sstevel@tonic-gate 1960Sstevel@tonic-gate /* PRINTFLIKE3 */ 1970Sstevel@tonic-gate void 1980Sstevel@tonic-gate zerror(zlog_t *zlogp, boolean_t use_strerror, const char *fmt, ...) 1990Sstevel@tonic-gate { 2000Sstevel@tonic-gate va_list alist; 2010Sstevel@tonic-gate char buf[MAXPATHLEN * 2]; /* enough space for err msg with a path */ 2020Sstevel@tonic-gate char *bp; 2030Sstevel@tonic-gate int saved_errno = errno; 2040Sstevel@tonic-gate 2050Sstevel@tonic-gate if (zlogp == NULL) 2060Sstevel@tonic-gate return; 2070Sstevel@tonic-gate if (zlogp == &logsys) 2080Sstevel@tonic-gate (void) snprintf(buf, sizeof (buf), "[zone '%s'] ", 2090Sstevel@tonic-gate zone_name); 2100Sstevel@tonic-gate else 2110Sstevel@tonic-gate buf[0] = '\0'; 2120Sstevel@tonic-gate bp = &(buf[strlen(buf)]); 2130Sstevel@tonic-gate 2140Sstevel@tonic-gate /* 2150Sstevel@tonic-gate * In theory, the locale pointer should be set to either "C" or a 2160Sstevel@tonic-gate * char array, so it should never be NULL 2170Sstevel@tonic-gate */ 2180Sstevel@tonic-gate assert(zlogp->locale != NULL); 2190Sstevel@tonic-gate /* Locale is per process, but we are multi-threaded... */ 2200Sstevel@tonic-gate fmt = localize_msg(zlogp->locale, fmt); 2210Sstevel@tonic-gate 2220Sstevel@tonic-gate va_start(alist, fmt); 2230Sstevel@tonic-gate (void) vsnprintf(bp, sizeof (buf) - (bp - buf), fmt, alist); 2240Sstevel@tonic-gate va_end(alist); 2250Sstevel@tonic-gate bp = &(buf[strlen(buf)]); 2260Sstevel@tonic-gate if (use_strerror) 2270Sstevel@tonic-gate (void) snprintf(bp, sizeof (buf) - (bp - buf), ": %s", 2280Sstevel@tonic-gate strerror(saved_errno)); 2290Sstevel@tonic-gate if (zlogp == &logsys) { 2300Sstevel@tonic-gate (void) syslog(LOG_ERR, "%s", buf); 2310Sstevel@tonic-gate } else if (zlogp->logfile != NULL) { 2320Sstevel@tonic-gate (void) fprintf(zlogp->logfile, "%s\n", buf); 2330Sstevel@tonic-gate } else { 2340Sstevel@tonic-gate size_t buflen; 2350Sstevel@tonic-gate size_t copylen; 2360Sstevel@tonic-gate 2370Sstevel@tonic-gate buflen = snprintf(zlogp->log, zlogp->loglen, "%s\n", buf); 2380Sstevel@tonic-gate copylen = MIN(buflen, zlogp->loglen); 2390Sstevel@tonic-gate zlogp->log += copylen; 2400Sstevel@tonic-gate zlogp->loglen -= copylen; 2410Sstevel@tonic-gate } 2420Sstevel@tonic-gate } 2430Sstevel@tonic-gate 2442267Sdp /* 2452267Sdp * Emit a warning for any boot arguments which are unrecognized. Since 2462267Sdp * Solaris boot arguments are getopt(3c) compatible (see kernel(1m)), we 2472267Sdp * put the arguments into an argv style array, use getopt to process them, 2482267Sdp * and put the resultant argument string back into outargs. 2492267Sdp * 2502267Sdp * During the filtering, we pull out any arguments which are truly "boot" 2512267Sdp * arguments, leaving only those which are to be passed intact to the 2522267Sdp * progenitor process. The one we support at the moment is -i, which 2532267Sdp * indicates to the kernel which program should be launched as 'init'. 2542267Sdp * 2552267Sdp * A return of Z_INVAL indicates specifically that the arguments are 2562267Sdp * not valid; this is a non-fatal error. Except for Z_OK, all other return 2572267Sdp * values are treated as fatal. 2582267Sdp */ 2592267Sdp static int 2602267Sdp filter_bootargs(zlog_t *zlogp, const char *inargs, char *outargs, 2612267Sdp char *init_file, char *badarg) 2622267Sdp { 2632267Sdp int argc = 0, argc_save; 2642267Sdp int i; 2652267Sdp int err; 2662267Sdp char *arg, *lasts, **argv = NULL, **argv_save; 2672267Sdp char zonecfg_args[BOOTARGS_MAX]; 2682267Sdp char scratchargs[BOOTARGS_MAX], *sargs; 2692267Sdp char c; 2702267Sdp 2712267Sdp bzero(outargs, BOOTARGS_MAX); 2722267Sdp bzero(badarg, BOOTARGS_MAX); 2732267Sdp 2742267Sdp /* 2752267Sdp * If the user didn't specify transient boot arguments, check 2762267Sdp * to see if there were any specified in the zone configuration, 2772267Sdp * and use them if applicable. 2782267Sdp */ 2792267Sdp if (inargs == NULL || inargs[0] == '\0') { 2802267Sdp zone_dochandle_t handle; 2812267Sdp if ((handle = zonecfg_init_handle()) == NULL) { 2822267Sdp zerror(zlogp, B_TRUE, 2832267Sdp "getting zone configuration handle"); 2842267Sdp return (Z_BAD_HANDLE); 2852267Sdp } 2862267Sdp err = zonecfg_get_snapshot_handle(zone_name, handle); 2872267Sdp if (err != Z_OK) { 2882267Sdp zerror(zlogp, B_FALSE, 2892267Sdp "invalid configuration snapshot"); 2902267Sdp zonecfg_fini_handle(handle); 2912267Sdp return (Z_BAD_HANDLE); 2922267Sdp } 2932267Sdp 2942267Sdp bzero(zonecfg_args, sizeof (zonecfg_args)); 2952267Sdp (void) zonecfg_get_bootargs(handle, zonecfg_args, 2962267Sdp sizeof (zonecfg_args)); 2972267Sdp inargs = zonecfg_args; 2982267Sdp zonecfg_fini_handle(handle); 2992267Sdp } 3002267Sdp 3012267Sdp if (strlen(inargs) >= BOOTARGS_MAX) { 3022267Sdp zerror(zlogp, B_FALSE, "boot argument string too long"); 3032267Sdp return (Z_INVAL); 3042267Sdp } 3052267Sdp 3062267Sdp (void) strlcpy(scratchargs, inargs, sizeof (scratchargs)); 3072267Sdp sargs = scratchargs; 3082267Sdp while ((arg = strtok_r(sargs, " \t", &lasts)) != NULL) { 3092267Sdp sargs = NULL; 3102267Sdp argc++; 3112267Sdp } 3122267Sdp 3132267Sdp if ((argv = calloc(argc + 1, sizeof (char *))) == NULL) { 3142267Sdp zerror(zlogp, B_FALSE, "memory allocation failed"); 3152267Sdp return (Z_NOMEM); 3162267Sdp } 3172267Sdp 3182267Sdp argv_save = argv; 3192267Sdp argc_save = argc; 3202267Sdp 3212267Sdp (void) strlcpy(scratchargs, inargs, sizeof (scratchargs)); 3222267Sdp sargs = scratchargs; 3232267Sdp i = 0; 3242267Sdp while ((arg = strtok_r(sargs, " \t", &lasts)) != NULL) { 3252267Sdp sargs = NULL; 3262267Sdp if ((argv[i] = strdup(arg)) == NULL) { 3272267Sdp err = Z_NOMEM; 3282267Sdp zerror(zlogp, B_FALSE, "memory allocation failed"); 3292267Sdp goto done; 3302267Sdp } 3312267Sdp i++; 3322267Sdp } 3332267Sdp 3342267Sdp /* 3352267Sdp * We preserve compatibility with the Solaris system boot behavior, 3362267Sdp * which allows: 3372267Sdp * 3382267Sdp * # reboot kernel/unix -s -m verbose 3392267Sdp * 3402267Sdp * In this example, kernel/unix tells the booter what file to 3412267Sdp * boot. We don't want reboot in a zone to be gratuitously different, 3422267Sdp * so we silently ignore the boot file, if necessary. 3432267Sdp */ 3442267Sdp if (argv[0] == NULL) 3452267Sdp goto done; 3462267Sdp 3472267Sdp assert(argv[0][0] != ' '); 3482267Sdp assert(argv[0][0] != '\t'); 3492267Sdp 3502267Sdp if (argv[0][0] != '-' && argv[0][0] != '\0') { 3512267Sdp argv = &argv[1]; 3522267Sdp argc--; 3532267Sdp } 3542267Sdp 3552267Sdp optind = 0; 3562267Sdp opterr = 0; 3572267Sdp err = Z_OK; 3582712Snn35248 while ((c = getopt(argc, argv, "fi:m:s")) != -1) { 3592267Sdp switch (c) { 3602267Sdp case 'i': 3612267Sdp /* 3622267Sdp * -i is handled by the runtime and is not passed 3632267Sdp * along to userland 3642267Sdp */ 3652267Sdp (void) strlcpy(init_file, optarg, MAXPATHLEN); 3662267Sdp break; 3672712Snn35248 case 'f': 3682712Snn35248 /* This has already been processed by zoneadm */ 3692712Snn35248 break; 3702267Sdp case 'm': 3712267Sdp case 's': 3722267Sdp /* These pass through unmolested */ 3732267Sdp (void) snprintf(outargs, BOOTARGS_MAX, 3742267Sdp "%s -%c %s ", outargs, c, optarg ? optarg : ""); 3752267Sdp break; 3762267Sdp case '?': 3772267Sdp /* 3782267Sdp * We warn about unknown arguments but pass them 3792267Sdp * along anyway-- if someone wants to develop their 3802267Sdp * own init replacement, they can pass it whatever 3812267Sdp * args they want. 3822267Sdp */ 3832267Sdp err = Z_INVAL; 3842267Sdp (void) snprintf(outargs, BOOTARGS_MAX, 3852267Sdp "%s -%c", outargs, optopt); 3862267Sdp (void) snprintf(badarg, BOOTARGS_MAX, 3872267Sdp "%s -%c", badarg, optopt); 3882267Sdp break; 3892267Sdp } 3902267Sdp } 3912267Sdp 3922267Sdp /* 3932267Sdp * For Solaris Zones we warn about and discard non-option arguments. 3942267Sdp * Hence 'boot foo bar baz gub' --> 'boot'. However, to be similar 3952267Sdp * to the kernel, we concat up all the other remaining boot args. 3962267Sdp * and warn on them as a group. 3972267Sdp */ 3982267Sdp if (optind < argc) { 3992267Sdp err = Z_INVAL; 4002267Sdp while (optind < argc) { 4012267Sdp (void) snprintf(badarg, BOOTARGS_MAX, "%s%s%s", 4022267Sdp badarg, strlen(badarg) > 0 ? " " : "", 4032267Sdp argv[optind]); 4042267Sdp optind++; 4052267Sdp } 4062267Sdp zerror(zlogp, B_FALSE, "WARNING: Unused or invalid boot " 4072267Sdp "arguments `%s'.", badarg); 4082267Sdp } 4092267Sdp 4102267Sdp done: 4112267Sdp for (i = 0; i < argc_save; i++) { 4122267Sdp if (argv_save[i] != NULL) 4132267Sdp free(argv_save[i]); 4142267Sdp } 4152267Sdp free(argv_save); 4162267Sdp return (err); 4172267Sdp } 4182267Sdp 4192267Sdp 4200Sstevel@tonic-gate static int 4210Sstevel@tonic-gate mkzonedir(zlog_t *zlogp) 4220Sstevel@tonic-gate { 4230Sstevel@tonic-gate struct stat st; 4240Sstevel@tonic-gate /* 4250Sstevel@tonic-gate * We must create and lock everyone but root out of ZONES_TMPDIR 4260Sstevel@tonic-gate * since anyone can open any UNIX domain socket, regardless of 4270Sstevel@tonic-gate * its file system permissions. Sigh... 4280Sstevel@tonic-gate */ 4290Sstevel@tonic-gate if (mkdir(ZONES_TMPDIR, S_IRWXU) < 0 && errno != EEXIST) { 4300Sstevel@tonic-gate zerror(zlogp, B_TRUE, "could not mkdir '%s'", ZONES_TMPDIR); 4310Sstevel@tonic-gate return (-1); 4320Sstevel@tonic-gate } 4330Sstevel@tonic-gate /* paranoia */ 434871Scasper if ((stat(ZONES_TMPDIR, &st) < 0) || !S_ISDIR(st.st_mode)) { 4350Sstevel@tonic-gate zerror(zlogp, B_TRUE, "'%s' is not a directory", ZONES_TMPDIR); 4360Sstevel@tonic-gate return (-1); 4370Sstevel@tonic-gate } 4380Sstevel@tonic-gate (void) chmod(ZONES_TMPDIR, S_IRWXU); 4390Sstevel@tonic-gate return (0); 4400Sstevel@tonic-gate } 4410Sstevel@tonic-gate 442766Scarlsonj /* 443766Scarlsonj * Bring a zone up to the pre-boot "ready" stage. The mount_cmd argument is 444766Scarlsonj * 'true' if this is being invoked as part of the processing for the "mount" 445766Scarlsonj * subcommand. 446766Scarlsonj */ 447766Scarlsonj static int 448766Scarlsonj zone_ready(zlog_t *zlogp, boolean_t mount_cmd) 4490Sstevel@tonic-gate { 4500Sstevel@tonic-gate int err; 4510Sstevel@tonic-gate 4520Sstevel@tonic-gate if ((err = zonecfg_create_snapshot(zone_name)) != Z_OK) { 4530Sstevel@tonic-gate zerror(zlogp, B_FALSE, "unable to create snapshot: %s", 4540Sstevel@tonic-gate zonecfg_strerror(err)); 4550Sstevel@tonic-gate return (-1); 4560Sstevel@tonic-gate } 4570Sstevel@tonic-gate 458766Scarlsonj if ((zone_id = vplat_create(zlogp, mount_cmd)) == -1) { 4590Sstevel@tonic-gate if ((err = zonecfg_destroy_snapshot(zone_name)) != Z_OK) 4600Sstevel@tonic-gate zerror(zlogp, B_FALSE, "destroying snapshot: %s", 4610Sstevel@tonic-gate zonecfg_strerror(err)); 4620Sstevel@tonic-gate return (-1); 4630Sstevel@tonic-gate } 4642303Scarlsonj if (vplat_bringup(zlogp, mount_cmd, zone_id) != 0) { 4650Sstevel@tonic-gate bringup_failure_recovery = B_TRUE; 466*3247Sgjelinek (void) vplat_teardown(NULL, mount_cmd, B_FALSE); 4670Sstevel@tonic-gate if ((err = zonecfg_destroy_snapshot(zone_name)) != Z_OK) 4680Sstevel@tonic-gate zerror(zlogp, B_FALSE, "destroying snapshot: %s", 4690Sstevel@tonic-gate zonecfg_strerror(err)); 4700Sstevel@tonic-gate return (-1); 4710Sstevel@tonic-gate } 4720Sstevel@tonic-gate 4730Sstevel@tonic-gate return (0); 4740Sstevel@tonic-gate } 4750Sstevel@tonic-gate 4762303Scarlsonj int 4772303Scarlsonj init_template(void) 4780Sstevel@tonic-gate { 4790Sstevel@tonic-gate int fd; 4800Sstevel@tonic-gate int err = 0; 4810Sstevel@tonic-gate 4820Sstevel@tonic-gate fd = open64(CTFS_ROOT "/process/template", O_RDWR); 4830Sstevel@tonic-gate if (fd == -1) 4840Sstevel@tonic-gate return (-1); 4850Sstevel@tonic-gate 4860Sstevel@tonic-gate /* 4870Sstevel@tonic-gate * For now, zoneadmd doesn't do anything with the contract. 4880Sstevel@tonic-gate * Deliver no events, don't inherit, and allow it to be orphaned. 4890Sstevel@tonic-gate */ 4900Sstevel@tonic-gate err |= ct_tmpl_set_critical(fd, 0); 4910Sstevel@tonic-gate err |= ct_tmpl_set_informative(fd, 0); 4920Sstevel@tonic-gate err |= ct_pr_tmpl_set_fatal(fd, CT_PR_EV_HWERR); 4930Sstevel@tonic-gate err |= ct_pr_tmpl_set_param(fd, CT_PR_PGRPONLY | CT_PR_REGENT); 4940Sstevel@tonic-gate if (err || ct_tmpl_activate(fd)) { 4950Sstevel@tonic-gate (void) close(fd); 4960Sstevel@tonic-gate return (-1); 4970Sstevel@tonic-gate } 4980Sstevel@tonic-gate 4990Sstevel@tonic-gate return (fd); 5000Sstevel@tonic-gate } 5010Sstevel@tonic-gate 5022712Snn35248 typedef struct fs_callback { 5032712Snn35248 zlog_t *zlogp; 5042712Snn35248 zoneid_t zoneid; 5052712Snn35248 } fs_callback_t; 5062712Snn35248 5070Sstevel@tonic-gate static int 5082712Snn35248 mount_early_fs(void *data, const char *spec, const char *dir, 5092712Snn35248 const char *fstype, const char *opt) 5100Sstevel@tonic-gate { 5112712Snn35248 zlog_t *zlogp = ((fs_callback_t *)data)->zlogp; 5122712Snn35248 zoneid_t zoneid = ((fs_callback_t *)data)->zoneid; 5130Sstevel@tonic-gate pid_t child; 5140Sstevel@tonic-gate int child_status; 5150Sstevel@tonic-gate int tmpl_fd; 5160Sstevel@tonic-gate ctid_t ct; 5170Sstevel@tonic-gate 5180Sstevel@tonic-gate if ((tmpl_fd = init_template()) == -1) { 5190Sstevel@tonic-gate zerror(zlogp, B_TRUE, "failed to create contract"); 5200Sstevel@tonic-gate return (-1); 5210Sstevel@tonic-gate } 5220Sstevel@tonic-gate 5230Sstevel@tonic-gate if ((child = fork()) == -1) { 5240Sstevel@tonic-gate (void) ct_tmpl_clear(tmpl_fd); 5250Sstevel@tonic-gate (void) close(tmpl_fd); 5260Sstevel@tonic-gate zerror(zlogp, B_TRUE, "failed to fork"); 5270Sstevel@tonic-gate return (-1); 5280Sstevel@tonic-gate 5290Sstevel@tonic-gate } else if (child == 0) { /* child */ 5302712Snn35248 char opt_buf[MAX_MNTOPT_STR]; 5312712Snn35248 int optlen = 0; 5322712Snn35248 int mflag = MS_DATA; 5332712Snn35248 5340Sstevel@tonic-gate (void) ct_tmpl_clear(tmpl_fd); 5350Sstevel@tonic-gate /* 5360Sstevel@tonic-gate * Even though there are no procs running in the zone, we 5370Sstevel@tonic-gate * do this for paranoia's sake. 5380Sstevel@tonic-gate */ 5390Sstevel@tonic-gate (void) closefrom(0); 5400Sstevel@tonic-gate 5410Sstevel@tonic-gate if (zone_enter(zoneid) == -1) { 5420Sstevel@tonic-gate _exit(errno); 5430Sstevel@tonic-gate } 5442712Snn35248 if (opt != NULL) { 5452712Snn35248 /* 5462712Snn35248 * The mount() system call is incredibly annoying. 5472712Snn35248 * If options are specified, we need to copy them 5482712Snn35248 * into a temporary buffer since the mount() system 5492712Snn35248 * call will overwrite the options string. It will 5502712Snn35248 * also fail if the new option string it wants to 5512712Snn35248 * write is bigger than the one we passed in, so 5522712Snn35248 * you must pass in a buffer of the maximum possible 5532712Snn35248 * option string length. sigh. 5542712Snn35248 */ 5552712Snn35248 (void) strlcpy(opt_buf, opt, sizeof (opt_buf)); 5562712Snn35248 opt = opt_buf; 5572712Snn35248 optlen = MAX_MNTOPT_STR; 5582712Snn35248 mflag = MS_OPTIONSTR; 5592712Snn35248 } 5602712Snn35248 if (mount(spec, dir, mflag, fstype, NULL, 0, opt, optlen) != 0) 5610Sstevel@tonic-gate _exit(errno); 5620Sstevel@tonic-gate _exit(0); 5630Sstevel@tonic-gate } 5640Sstevel@tonic-gate 5650Sstevel@tonic-gate /* parent */ 5660Sstevel@tonic-gate if (contract_latest(&ct) == -1) 5670Sstevel@tonic-gate ct = -1; 5680Sstevel@tonic-gate (void) ct_tmpl_clear(tmpl_fd); 5690Sstevel@tonic-gate (void) close(tmpl_fd); 5700Sstevel@tonic-gate if (waitpid(child, &child_status, 0) != child) { 5710Sstevel@tonic-gate /* unexpected: we must have been signalled */ 5720Sstevel@tonic-gate (void) contract_abandon_id(ct); 5730Sstevel@tonic-gate return (-1); 5740Sstevel@tonic-gate } 5750Sstevel@tonic-gate (void) contract_abandon_id(ct); 5760Sstevel@tonic-gate if (WEXITSTATUS(child_status) != 0) { 5770Sstevel@tonic-gate errno = WEXITSTATUS(child_status); 5780Sstevel@tonic-gate zerror(zlogp, B_TRUE, "mount of %s failed", dir); 5790Sstevel@tonic-gate return (-1); 5800Sstevel@tonic-gate } 5810Sstevel@tonic-gate 5820Sstevel@tonic-gate return (0); 5830Sstevel@tonic-gate } 5840Sstevel@tonic-gate 5852712Snn35248 int 5862712Snn35248 do_subproc(zlog_t *zlogp, char *cmdbuf) 587766Scarlsonj { 5882712Snn35248 char inbuf[1024]; /* arbitrary large amount */ 5892712Snn35248 FILE *file; 5902712Snn35248 int status; 591766Scarlsonj 5922712Snn35248 file = popen(cmdbuf, "r"); 5932712Snn35248 if (file == NULL) { 5942712Snn35248 zerror(zlogp, B_TRUE, "could not launch: %s", cmdbuf); 5952525Sdp return (-1); 5962712Snn35248 } 5972525Sdp 5982712Snn35248 while (fgets(inbuf, sizeof (inbuf), file) != NULL) 5992712Snn35248 if (zlogp != &logsys) 6002712Snn35248 zerror(zlogp, B_FALSE, "%s", inbuf); 6012712Snn35248 status = pclose(file); 602766Scarlsonj 6032712Snn35248 if (WIFSIGNALED(status)) { 6042712Snn35248 zerror(zlogp, B_FALSE, "%s unexpectedly terminated due to " 6052712Snn35248 "signal %d", cmdbuf, WTERMSIG(status)); 606766Scarlsonj return (-1); 6072712Snn35248 } 6082712Snn35248 assert(WIFEXITED(status)); 6092712Snn35248 if (WEXITSTATUS(status) == ZEXIT_EXEC) { 6102712Snn35248 zerror(zlogp, B_FALSE, "failed to exec %s", cmdbuf); 6112712Snn35248 return (-1); 6122712Snn35248 } 6132712Snn35248 return (WEXITSTATUS(status)); 614766Scarlsonj } 615766Scarlsonj 616766Scarlsonj static int 6170Sstevel@tonic-gate zone_bootup(zlog_t *zlogp, const char *bootargs) 6180Sstevel@tonic-gate { 6190Sstevel@tonic-gate zoneid_t zoneid; 6200Sstevel@tonic-gate struct stat st; 6212267Sdp char zroot[MAXPATHLEN], initpath[MAXPATHLEN], init_file[MAXPATHLEN]; 6222267Sdp char nbootargs[BOOTARGS_MAX]; 6232712Snn35248 char cmdbuf[MAXPATHLEN]; 6242712Snn35248 fs_callback_t cb; 6252727Sedp brand_handle_t bh; 6262267Sdp int err; 6270Sstevel@tonic-gate 6280Sstevel@tonic-gate if (init_console_slave(zlogp) != 0) 6290Sstevel@tonic-gate return (-1); 6300Sstevel@tonic-gate reset_slave_terminal(zlogp); 6310Sstevel@tonic-gate 6320Sstevel@tonic-gate if ((zoneid = getzoneidbyname(zone_name)) == -1) { 6330Sstevel@tonic-gate zerror(zlogp, B_TRUE, "unable to get zoneid"); 6340Sstevel@tonic-gate return (-1); 6350Sstevel@tonic-gate } 6360Sstevel@tonic-gate 6372712Snn35248 cb.zlogp = zlogp; 6382712Snn35248 cb.zoneid = zoneid; 6392712Snn35248 6402712Snn35248 /* Get a handle to the brand info for this zone */ 6412727Sedp if ((bh = brand_open(brand_name)) == NULL) { 6422712Snn35248 zerror(zlogp, B_FALSE, "unable to determine zone brand"); 6432712Snn35248 return (-1); 6442712Snn35248 } 6452712Snn35248 6462712Snn35248 /* 6472712Snn35248 * Get the list of filesystems to mount from the brand 6482712Snn35248 * configuration. These mounts are done via a thread that will 6492712Snn35248 * enter the zone, so they are done from within the context of the 6502712Snn35248 * zone. 6512712Snn35248 */ 6522727Sedp if (brand_platform_iter_mounts(bh, mount_early_fs, &cb) != 0) { 6532712Snn35248 zerror(zlogp, B_FALSE, "unable to mount filesystems"); 6542727Sedp brand_close(bh); 6550Sstevel@tonic-gate return (-1); 6562712Snn35248 } 6572712Snn35248 6582712Snn35248 /* 6592712Snn35248 * Get the brand's boot callback if it exists. 6602712Snn35248 */ 6612712Snn35248 if (zone_get_zonepath(zone_name, zroot, sizeof (zroot)) != Z_OK) { 6622712Snn35248 zerror(zlogp, B_FALSE, "unable to determine zone root"); 6632712Snn35248 return (-1); 6642712Snn35248 } 6652712Snn35248 (void) strcpy(cmdbuf, EXEC_PREFIX); 6662727Sedp if (brand_get_boot(bh, zone_name, zroot, cmdbuf + EXEC_LEN, 6672712Snn35248 sizeof (cmdbuf) - EXEC_LEN, 0, NULL) != 0) { 6682712Snn35248 zerror(zlogp, B_FALSE, 6692712Snn35248 "unable to determine branded zone's boot callback"); 6702727Sedp brand_close(bh); 6712712Snn35248 return (-1); 6722712Snn35248 } 6732712Snn35248 6742712Snn35248 /* Get the path for this zone's init(1M) (or equivalent) process. */ 6752727Sedp if (brand_get_initname(bh, init_file, MAXPATHLEN) != 0) { 6762712Snn35248 zerror(zlogp, B_FALSE, 6772712Snn35248 "unable to determine zone's init(1M) location"); 6782727Sedp brand_close(bh); 6792712Snn35248 return (-1); 6802712Snn35248 } 6812712Snn35248 6822727Sedp brand_close(bh); 6830Sstevel@tonic-gate 6842267Sdp err = filter_bootargs(zlogp, bootargs, nbootargs, init_file, 6852267Sdp bad_boot_arg); 6862267Sdp if (err == Z_INVAL) 6872267Sdp eventstream_write(Z_EVT_ZONE_BADARGS); 6882267Sdp else if (err != Z_OK) 6892267Sdp return (-1); 6902267Sdp 6912267Sdp assert(init_file[0] != '\0'); 6922267Sdp 6932712Snn35248 /* Try to anticipate possible problems: Make sure init is executable. */ 6940Sstevel@tonic-gate if (zone_get_rootpath(zone_name, zroot, sizeof (zroot)) != Z_OK) { 6950Sstevel@tonic-gate zerror(zlogp, B_FALSE, "unable to determine zone root"); 6960Sstevel@tonic-gate return (-1); 6970Sstevel@tonic-gate } 6982712Snn35248 6992267Sdp (void) snprintf(initpath, sizeof (initpath), "%s%s", zroot, init_file); 7000Sstevel@tonic-gate 7010Sstevel@tonic-gate if (stat(initpath, &st) == -1) { 7020Sstevel@tonic-gate zerror(zlogp, B_TRUE, "could not stat %s", initpath); 7030Sstevel@tonic-gate return (-1); 7040Sstevel@tonic-gate } 7050Sstevel@tonic-gate 7060Sstevel@tonic-gate if ((st.st_mode & S_IXUSR) == 0) { 7070Sstevel@tonic-gate zerror(zlogp, B_FALSE, "%s is not executable", initpath); 7080Sstevel@tonic-gate return (-1); 7090Sstevel@tonic-gate } 7100Sstevel@tonic-gate 7112712Snn35248 /* 7122712Snn35248 * If there is a brand 'boot' callback, execute it now to give the 7132712Snn35248 * brand one last chance to do any additional setup before the zone 7142712Snn35248 * is booted. 7152712Snn35248 */ 7162712Snn35248 if ((strlen(cmdbuf) > EXEC_LEN) && 7172712Snn35248 (do_subproc(zlogp, cmdbuf) != Z_OK)) { 7182712Snn35248 zerror(zlogp, B_FALSE, "%s failed", cmdbuf); 7192712Snn35248 return (-1); 7202712Snn35248 } 7212712Snn35248 7222267Sdp if (zone_setattr(zoneid, ZONE_ATTR_INITNAME, init_file, 0) == -1) { 7232267Sdp zerror(zlogp, B_TRUE, "could not set zone boot file"); 7242267Sdp return (-1); 7252267Sdp } 7262267Sdp 7272267Sdp if (zone_setattr(zoneid, ZONE_ATTR_BOOTARGS, nbootargs, 0) == -1) { 7282267Sdp zerror(zlogp, B_TRUE, "could not set zone boot arguments"); 7292267Sdp return (-1); 7302267Sdp } 7312267Sdp 7322267Sdp if (zone_boot(zoneid) == -1) { 7330Sstevel@tonic-gate zerror(zlogp, B_TRUE, "unable to boot zone"); 7340Sstevel@tonic-gate return (-1); 7350Sstevel@tonic-gate } 7360Sstevel@tonic-gate 7370Sstevel@tonic-gate return (0); 7380Sstevel@tonic-gate } 7390Sstevel@tonic-gate 7400Sstevel@tonic-gate static int 741*3247Sgjelinek zone_halt(zlog_t *zlogp, boolean_t unmount_cmd, boolean_t rebooting) 7420Sstevel@tonic-gate { 7430Sstevel@tonic-gate int err; 7440Sstevel@tonic-gate 745*3247Sgjelinek if (vplat_teardown(zlogp, unmount_cmd, rebooting) != 0) { 7460Sstevel@tonic-gate if (!bringup_failure_recovery) 7470Sstevel@tonic-gate zerror(zlogp, B_FALSE, "unable to destroy zone"); 7480Sstevel@tonic-gate return (-1); 7490Sstevel@tonic-gate } 7500Sstevel@tonic-gate 7510Sstevel@tonic-gate if ((err = zonecfg_destroy_snapshot(zone_name)) != Z_OK) 7520Sstevel@tonic-gate zerror(zlogp, B_FALSE, "destroying snapshot: %s", 7530Sstevel@tonic-gate zonecfg_strerror(err)); 7540Sstevel@tonic-gate 7550Sstevel@tonic-gate return (0); 7560Sstevel@tonic-gate } 7570Sstevel@tonic-gate 7580Sstevel@tonic-gate /* 7590Sstevel@tonic-gate * Generate AUE_zone_state for a command that boots a zone. 7600Sstevel@tonic-gate */ 7610Sstevel@tonic-gate static void 7620Sstevel@tonic-gate audit_put_record(zlog_t *zlogp, ucred_t *uc, int return_val, 7630Sstevel@tonic-gate char *new_state) 7640Sstevel@tonic-gate { 7650Sstevel@tonic-gate adt_session_data_t *ah; 7660Sstevel@tonic-gate adt_event_data_t *event; 7670Sstevel@tonic-gate int pass_fail, fail_reason; 7680Sstevel@tonic-gate 7690Sstevel@tonic-gate if (!adt_audit_enabled()) 7700Sstevel@tonic-gate return; 7710Sstevel@tonic-gate 7720Sstevel@tonic-gate if (return_val == 0) { 7730Sstevel@tonic-gate pass_fail = ADT_SUCCESS; 7740Sstevel@tonic-gate fail_reason = ADT_SUCCESS; 7750Sstevel@tonic-gate } else { 7760Sstevel@tonic-gate pass_fail = ADT_FAILURE; 7770Sstevel@tonic-gate fail_reason = ADT_FAIL_VALUE_PROGRAM; 7780Sstevel@tonic-gate } 7790Sstevel@tonic-gate 7800Sstevel@tonic-gate if (adt_start_session(&ah, NULL, 0)) { 7810Sstevel@tonic-gate zerror(zlogp, B_TRUE, gettext("audit failure.")); 7820Sstevel@tonic-gate return; 7830Sstevel@tonic-gate } 7840Sstevel@tonic-gate if (adt_set_from_ucred(ah, uc, ADT_NEW)) { 7850Sstevel@tonic-gate zerror(zlogp, B_TRUE, gettext("audit failure.")); 7860Sstevel@tonic-gate (void) adt_end_session(ah); 7870Sstevel@tonic-gate return; 7880Sstevel@tonic-gate } 7890Sstevel@tonic-gate 7900Sstevel@tonic-gate event = adt_alloc_event(ah, ADT_zone_state); 7910Sstevel@tonic-gate if (event == NULL) { 7920Sstevel@tonic-gate zerror(zlogp, B_TRUE, gettext("audit failure.")); 7930Sstevel@tonic-gate (void) adt_end_session(ah); 7940Sstevel@tonic-gate return; 7950Sstevel@tonic-gate } 7960Sstevel@tonic-gate event->adt_zone_state.zonename = zone_name; 7970Sstevel@tonic-gate event->adt_zone_state.new_state = new_state; 7980Sstevel@tonic-gate 7990Sstevel@tonic-gate if (adt_put_event(event, pass_fail, fail_reason)) 8000Sstevel@tonic-gate zerror(zlogp, B_TRUE, gettext("audit failure.")); 8010Sstevel@tonic-gate 8020Sstevel@tonic-gate adt_free_event(event); 8030Sstevel@tonic-gate 8040Sstevel@tonic-gate (void) adt_end_session(ah); 8050Sstevel@tonic-gate } 8060Sstevel@tonic-gate 8070Sstevel@tonic-gate /* 8080Sstevel@tonic-gate * The main routine for the door server that deals with zone state transitions. 8090Sstevel@tonic-gate */ 8100Sstevel@tonic-gate /* ARGSUSED */ 8110Sstevel@tonic-gate static void 8120Sstevel@tonic-gate server(void *cookie, char *args, size_t alen, door_desc_t *dp, 8130Sstevel@tonic-gate uint_t n_desc) 8140Sstevel@tonic-gate { 8150Sstevel@tonic-gate ucred_t *uc = NULL; 8160Sstevel@tonic-gate const priv_set_t *eset; 8170Sstevel@tonic-gate 8180Sstevel@tonic-gate zone_state_t zstate; 8190Sstevel@tonic-gate zone_cmd_t cmd; 8200Sstevel@tonic-gate zone_cmd_arg_t *zargp; 8210Sstevel@tonic-gate 8220Sstevel@tonic-gate boolean_t kernelcall; 8230Sstevel@tonic-gate 8240Sstevel@tonic-gate int rval = -1; 8250Sstevel@tonic-gate uint64_t uniqid; 8260Sstevel@tonic-gate zoneid_t zoneid = -1; 8270Sstevel@tonic-gate zlog_t zlog; 8280Sstevel@tonic-gate zlog_t *zlogp; 8290Sstevel@tonic-gate zone_cmd_rval_t *rvalp; 8300Sstevel@tonic-gate size_t rlen = getpagesize(); /* conservative */ 8312712Snn35248 fs_callback_t cb; 8322727Sedp brand_handle_t bh; 8330Sstevel@tonic-gate 8340Sstevel@tonic-gate /* LINTED E_BAD_PTR_CAST_ALIGN */ 8350Sstevel@tonic-gate zargp = (zone_cmd_arg_t *)args; 8360Sstevel@tonic-gate 8370Sstevel@tonic-gate /* 8380Sstevel@tonic-gate * When we get the door unref message, we've fdetach'd the door, and 8390Sstevel@tonic-gate * it is time for us to shut down zoneadmd. 8400Sstevel@tonic-gate */ 8410Sstevel@tonic-gate if (zargp == DOOR_UNREF_DATA) { 8420Sstevel@tonic-gate /* 8430Sstevel@tonic-gate * See comment at end of main() for info on the last rites. 8440Sstevel@tonic-gate */ 8450Sstevel@tonic-gate exit(0); 8460Sstevel@tonic-gate } 8470Sstevel@tonic-gate 8480Sstevel@tonic-gate if (zargp == NULL) { 8490Sstevel@tonic-gate (void) door_return(NULL, 0, 0, 0); 8500Sstevel@tonic-gate } 8510Sstevel@tonic-gate 8520Sstevel@tonic-gate rvalp = alloca(rlen); 8530Sstevel@tonic-gate bzero(rvalp, rlen); 8540Sstevel@tonic-gate zlog.logfile = NULL; 8550Sstevel@tonic-gate zlog.buflen = zlog.loglen = rlen - sizeof (zone_cmd_rval_t) + 1; 8560Sstevel@tonic-gate zlog.buf = rvalp->errbuf; 8570Sstevel@tonic-gate zlog.log = zlog.buf; 8580Sstevel@tonic-gate /* defer initialization of zlog.locale until after credential check */ 8590Sstevel@tonic-gate zlogp = &zlog; 8600Sstevel@tonic-gate 8610Sstevel@tonic-gate if (alen != sizeof (zone_cmd_arg_t)) { 8620Sstevel@tonic-gate /* 8630Sstevel@tonic-gate * This really shouldn't be happening. 8640Sstevel@tonic-gate */ 8652267Sdp zerror(&logsys, B_FALSE, "argument size (%d bytes) " 8662267Sdp "unexpected (expected %d bytes)", alen, 8672267Sdp sizeof (zone_cmd_arg_t)); 8680Sstevel@tonic-gate goto out; 8690Sstevel@tonic-gate } 8700Sstevel@tonic-gate cmd = zargp->cmd; 8710Sstevel@tonic-gate 8720Sstevel@tonic-gate if (door_ucred(&uc) != 0) { 8730Sstevel@tonic-gate zerror(&logsys, B_TRUE, "door_ucred"); 8740Sstevel@tonic-gate goto out; 8750Sstevel@tonic-gate } 8760Sstevel@tonic-gate eset = ucred_getprivset(uc, PRIV_EFFECTIVE); 8770Sstevel@tonic-gate if (ucred_getzoneid(uc) != GLOBAL_ZONEID || 8780Sstevel@tonic-gate (eset != NULL ? !priv_ismember(eset, PRIV_SYS_CONFIG) : 8790Sstevel@tonic-gate ucred_geteuid(uc) != 0)) { 8800Sstevel@tonic-gate zerror(&logsys, B_FALSE, "insufficient privileges"); 8810Sstevel@tonic-gate goto out; 8820Sstevel@tonic-gate } 8830Sstevel@tonic-gate 8840Sstevel@tonic-gate kernelcall = ucred_getpid(uc) == 0; 8850Sstevel@tonic-gate 8860Sstevel@tonic-gate /* 8870Sstevel@tonic-gate * This is safe because we only use a zlog_t throughout the 8880Sstevel@tonic-gate * duration of a door call; i.e., by the time the pointer 8890Sstevel@tonic-gate * might become invalid, the door call would be over. 8900Sstevel@tonic-gate */ 8910Sstevel@tonic-gate zlog.locale = kernelcall ? DEFAULT_LOCALE : zargp->locale; 8920Sstevel@tonic-gate 8930Sstevel@tonic-gate (void) mutex_lock(&lock); 8940Sstevel@tonic-gate 8950Sstevel@tonic-gate /* 8960Sstevel@tonic-gate * Once we start to really die off, we don't want more connections. 8970Sstevel@tonic-gate */ 8980Sstevel@tonic-gate if (in_death_throes) { 8990Sstevel@tonic-gate (void) mutex_unlock(&lock); 9000Sstevel@tonic-gate ucred_free(uc); 9010Sstevel@tonic-gate (void) door_return(NULL, 0, 0, 0); 9020Sstevel@tonic-gate thr_exit(NULL); 9030Sstevel@tonic-gate } 9040Sstevel@tonic-gate 9050Sstevel@tonic-gate /* 9060Sstevel@tonic-gate * Check for validity of command. 9070Sstevel@tonic-gate */ 9082712Snn35248 if (cmd != Z_READY && cmd != Z_BOOT && cmd != Z_FORCEBOOT && 9092712Snn35248 cmd != Z_REBOOT && cmd != Z_HALT && cmd != Z_NOTE_UNINSTALLING && 9102712Snn35248 cmd != Z_MOUNT && cmd != Z_FORCEMOUNT && cmd != Z_UNMOUNT) { 911766Scarlsonj zerror(&logsys, B_FALSE, "invalid command %d", (int)cmd); 9120Sstevel@tonic-gate goto out; 9130Sstevel@tonic-gate } 9140Sstevel@tonic-gate 9150Sstevel@tonic-gate if (kernelcall && (cmd != Z_HALT && cmd != Z_REBOOT)) { 9160Sstevel@tonic-gate /* 9170Sstevel@tonic-gate * Can't happen 9180Sstevel@tonic-gate */ 9190Sstevel@tonic-gate zerror(&logsys, B_FALSE, "received unexpected kernel upcall %d", 9200Sstevel@tonic-gate cmd); 9210Sstevel@tonic-gate goto out; 9220Sstevel@tonic-gate } 9230Sstevel@tonic-gate /* 9240Sstevel@tonic-gate * We ignore the possibility of someone calling zone_create(2) 9250Sstevel@tonic-gate * explicitly; all requests must come through zoneadmd. 9260Sstevel@tonic-gate */ 9270Sstevel@tonic-gate if (zone_get_state(zone_name, &zstate) != Z_OK) { 9280Sstevel@tonic-gate /* 9290Sstevel@tonic-gate * Something terribly wrong happened 9300Sstevel@tonic-gate */ 9310Sstevel@tonic-gate zerror(&logsys, B_FALSE, "unable to determine state of zone"); 9320Sstevel@tonic-gate goto out; 9330Sstevel@tonic-gate } 9340Sstevel@tonic-gate 9350Sstevel@tonic-gate if (kernelcall) { 9360Sstevel@tonic-gate /* 9370Sstevel@tonic-gate * Kernel-initiated requests may lose their validity if the 9380Sstevel@tonic-gate * zone_t the kernel was referring to has gone away. 9390Sstevel@tonic-gate */ 9400Sstevel@tonic-gate if ((zoneid = getzoneidbyname(zone_name)) == -1 || 9410Sstevel@tonic-gate zone_getattr(zoneid, ZONE_ATTR_UNIQID, &uniqid, 9420Sstevel@tonic-gate sizeof (uniqid)) == -1 || uniqid != zargp->uniqid) { 9430Sstevel@tonic-gate /* 9440Sstevel@tonic-gate * We're not talking about the same zone. The request 9450Sstevel@tonic-gate * must have arrived too late. Return error. 9460Sstevel@tonic-gate */ 9470Sstevel@tonic-gate rval = -1; 9480Sstevel@tonic-gate goto out; 9490Sstevel@tonic-gate } 9500Sstevel@tonic-gate zlogp = &logsys; /* Log errors to syslog */ 9510Sstevel@tonic-gate } 9520Sstevel@tonic-gate 9532712Snn35248 /* 9542712Snn35248 * If we are being asked to forcibly mount or boot a zone, we 9552712Snn35248 * pretend that an INCOMPLETE zone is actually INSTALLED. 9562712Snn35248 */ 9572712Snn35248 if (zstate == ZONE_STATE_INCOMPLETE && 9582712Snn35248 (cmd == Z_FORCEBOOT || cmd == Z_FORCEMOUNT)) 9592712Snn35248 zstate = ZONE_STATE_INSTALLED; 9602712Snn35248 9610Sstevel@tonic-gate switch (zstate) { 9620Sstevel@tonic-gate case ZONE_STATE_CONFIGURED: 9630Sstevel@tonic-gate case ZONE_STATE_INCOMPLETE: 9640Sstevel@tonic-gate /* 9650Sstevel@tonic-gate * Not our area of expertise; we just print a nice message 9660Sstevel@tonic-gate * and die off. 9670Sstevel@tonic-gate */ 9680Sstevel@tonic-gate zerror(zlogp, B_FALSE, 9690Sstevel@tonic-gate "%s operation is invalid for zones in state '%s'", 970766Scarlsonj z_cmd_name(cmd), zone_state_str(zstate)); 9710Sstevel@tonic-gate break; 9720Sstevel@tonic-gate 9730Sstevel@tonic-gate case ZONE_STATE_INSTALLED: 9740Sstevel@tonic-gate switch (cmd) { 9750Sstevel@tonic-gate case Z_READY: 976766Scarlsonj rval = zone_ready(zlogp, B_FALSE); 9770Sstevel@tonic-gate if (rval == 0) 9780Sstevel@tonic-gate eventstream_write(Z_EVT_ZONE_READIED); 9790Sstevel@tonic-gate break; 9800Sstevel@tonic-gate case Z_BOOT: 9812712Snn35248 case Z_FORCEBOOT: 9820Sstevel@tonic-gate eventstream_write(Z_EVT_ZONE_BOOTING); 983766Scarlsonj if ((rval = zone_ready(zlogp, B_FALSE)) == 0) 9840Sstevel@tonic-gate rval = zone_bootup(zlogp, zargp->bootbuf); 9850Sstevel@tonic-gate audit_put_record(zlogp, uc, rval, "boot"); 9860Sstevel@tonic-gate if (rval != 0) { 9870Sstevel@tonic-gate bringup_failure_recovery = B_TRUE; 988*3247Sgjelinek (void) zone_halt(zlogp, B_FALSE, B_FALSE); 9891645Scomay eventstream_write(Z_EVT_ZONE_BOOTFAILED); 9900Sstevel@tonic-gate } 9910Sstevel@tonic-gate break; 9920Sstevel@tonic-gate case Z_HALT: 9930Sstevel@tonic-gate if (kernelcall) /* Invalid; can't happen */ 9940Sstevel@tonic-gate abort(); 9950Sstevel@tonic-gate /* 9960Sstevel@tonic-gate * We could have two clients racing to halt this 9970Sstevel@tonic-gate * zone; the second client loses, but his request 9980Sstevel@tonic-gate * doesn't fail, since the zone is now in the desired 9990Sstevel@tonic-gate * state. 10000Sstevel@tonic-gate */ 10010Sstevel@tonic-gate zerror(zlogp, B_FALSE, "zone is already halted"); 10020Sstevel@tonic-gate rval = 0; 10030Sstevel@tonic-gate break; 10040Sstevel@tonic-gate case Z_REBOOT: 10050Sstevel@tonic-gate if (kernelcall) /* Invalid; can't happen */ 10060Sstevel@tonic-gate abort(); 10070Sstevel@tonic-gate zerror(zlogp, B_FALSE, "%s operation is invalid " 1008766Scarlsonj "for zones in state '%s'", z_cmd_name(cmd), 10090Sstevel@tonic-gate zone_state_str(zstate)); 10100Sstevel@tonic-gate rval = -1; 10110Sstevel@tonic-gate break; 10120Sstevel@tonic-gate case Z_NOTE_UNINSTALLING: 10130Sstevel@tonic-gate if (kernelcall) /* Invalid; can't happen */ 10140Sstevel@tonic-gate abort(); 10150Sstevel@tonic-gate /* 10160Sstevel@tonic-gate * Tell the console to print out a message about this. 10170Sstevel@tonic-gate * Once it does, we will be in_death_throes. 10180Sstevel@tonic-gate */ 10190Sstevel@tonic-gate eventstream_write(Z_EVT_ZONE_UNINSTALLING); 10200Sstevel@tonic-gate break; 1021766Scarlsonj case Z_MOUNT: 10222712Snn35248 case Z_FORCEMOUNT: 1023766Scarlsonj if (kernelcall) /* Invalid; can't happen */ 1024766Scarlsonj abort(); 10252712Snn35248 if (!zone_isnative) { 10262712Snn35248 zerror(zlogp, B_FALSE, 10272712Snn35248 "%s operation is invalid for branded " 10282712Snn35248 "zones", z_cmd_name(cmd)); 10292712Snn35248 rval = -1; 10302712Snn35248 break; 10312712Snn35248 } 10322712Snn35248 1033766Scarlsonj rval = zone_ready(zlogp, B_TRUE); 10342712Snn35248 if (rval != 0) 10352712Snn35248 break; 10362712Snn35248 10372712Snn35248 eventstream_write(Z_EVT_ZONE_READIED); 10382712Snn35248 10392712Snn35248 /* Get a handle to the brand info for this zone */ 10402727Sedp if ((bh = brand_open(brand_name)) == NULL) { 10412712Snn35248 rval = -1; 10422712Snn35248 break; 10431645Scomay } 10441645Scomay 1045766Scarlsonj /* 10462712Snn35248 * Get the list of filesystems to mount from 10472712Snn35248 * the brand configuration. These mounts are done 10482712Snn35248 * via a thread that will enter the zone, so they 10492712Snn35248 * are done from within the context of the zone. 10502712Snn35248 */ 10512712Snn35248 cb.zlogp = zlogp; 10522712Snn35248 cb.zoneid = zone_id; 10532727Sedp rval = brand_platform_iter_mounts(bh, 10542712Snn35248 mount_early_fs, &cb); 10552712Snn35248 10562727Sedp brand_close(bh); 10572712Snn35248 10582712Snn35248 /* 1059766Scarlsonj * Ordinarily, /dev/fd would be mounted inside the zone 1060766Scarlsonj * by svc:/system/filesystem/usr:default, but since 1061766Scarlsonj * we're not booting the zone, we need to do this 1062766Scarlsonj * manually. 1063766Scarlsonj */ 1064766Scarlsonj if (rval == 0) 10652712Snn35248 rval = mount_early_fs(&cb, 10662712Snn35248 "fd", "/dev/fd", "fd", NULL); 1067766Scarlsonj break; 1068766Scarlsonj case Z_UNMOUNT: 1069766Scarlsonj if (kernelcall) /* Invalid; can't happen */ 1070766Scarlsonj abort(); 1071766Scarlsonj zerror(zlogp, B_FALSE, "zone is already unmounted"); 1072766Scarlsonj rval = 0; 1073766Scarlsonj break; 10740Sstevel@tonic-gate } 10750Sstevel@tonic-gate break; 10760Sstevel@tonic-gate 10770Sstevel@tonic-gate case ZONE_STATE_READY: 10780Sstevel@tonic-gate switch (cmd) { 10790Sstevel@tonic-gate case Z_READY: 10800Sstevel@tonic-gate /* 10810Sstevel@tonic-gate * We could have two clients racing to ready this 10820Sstevel@tonic-gate * zone; the second client loses, but his request 10830Sstevel@tonic-gate * doesn't fail, since the zone is now in the desired 10840Sstevel@tonic-gate * state. 10850Sstevel@tonic-gate */ 10860Sstevel@tonic-gate zerror(zlogp, B_FALSE, "zone is already ready"); 10870Sstevel@tonic-gate rval = 0; 10880Sstevel@tonic-gate break; 10890Sstevel@tonic-gate case Z_BOOT: 10902267Sdp (void) strlcpy(boot_args, zargp->bootbuf, 10912267Sdp sizeof (boot_args)); 10920Sstevel@tonic-gate eventstream_write(Z_EVT_ZONE_BOOTING); 10930Sstevel@tonic-gate rval = zone_bootup(zlogp, zargp->bootbuf); 10940Sstevel@tonic-gate audit_put_record(zlogp, uc, rval, "boot"); 10950Sstevel@tonic-gate if (rval != 0) { 10960Sstevel@tonic-gate bringup_failure_recovery = B_TRUE; 1097*3247Sgjelinek (void) zone_halt(zlogp, B_FALSE, B_TRUE); 10981645Scomay eventstream_write(Z_EVT_ZONE_BOOTFAILED); 10990Sstevel@tonic-gate } 11002267Sdp boot_args[0] = '\0'; 11010Sstevel@tonic-gate break; 11020Sstevel@tonic-gate case Z_HALT: 11030Sstevel@tonic-gate if (kernelcall) /* Invalid; can't happen */ 11040Sstevel@tonic-gate abort(); 1105*3247Sgjelinek if ((rval = zone_halt(zlogp, B_FALSE, B_FALSE)) != 0) 11060Sstevel@tonic-gate break; 11070Sstevel@tonic-gate eventstream_write(Z_EVT_ZONE_HALTED); 11080Sstevel@tonic-gate break; 11090Sstevel@tonic-gate case Z_REBOOT: 1110766Scarlsonj case Z_NOTE_UNINSTALLING: 1111766Scarlsonj case Z_MOUNT: 1112766Scarlsonj case Z_UNMOUNT: 11130Sstevel@tonic-gate if (kernelcall) /* Invalid; can't happen */ 11140Sstevel@tonic-gate abort(); 11150Sstevel@tonic-gate zerror(zlogp, B_FALSE, "%s operation is invalid " 1116766Scarlsonj "for zones in state '%s'", z_cmd_name(cmd), 11170Sstevel@tonic-gate zone_state_str(zstate)); 11180Sstevel@tonic-gate rval = -1; 11190Sstevel@tonic-gate break; 1120766Scarlsonj } 1121766Scarlsonj break; 1122766Scarlsonj 1123766Scarlsonj case ZONE_STATE_MOUNTED: 1124766Scarlsonj switch (cmd) { 1125766Scarlsonj case Z_UNMOUNT: 11260Sstevel@tonic-gate if (kernelcall) /* Invalid; can't happen */ 11270Sstevel@tonic-gate abort(); 1128*3247Sgjelinek rval = zone_halt(zlogp, B_TRUE, B_FALSE); 11291645Scomay if (rval == 0) { 11301645Scomay eventstream_write(Z_EVT_ZONE_HALTED); 1131766Scarlsonj (void) sema_post(&scratch_sem); 11321645Scomay } 1133766Scarlsonj break; 1134766Scarlsonj default: 1135766Scarlsonj if (kernelcall) /* Invalid; can't happen */ 1136766Scarlsonj abort(); 1137766Scarlsonj zerror(zlogp, B_FALSE, "%s operation is invalid " 1138766Scarlsonj "for zones in state '%s'", z_cmd_name(cmd), 1139766Scarlsonj zone_state_str(zstate)); 11400Sstevel@tonic-gate rval = -1; 11410Sstevel@tonic-gate break; 11420Sstevel@tonic-gate } 11430Sstevel@tonic-gate break; 11440Sstevel@tonic-gate 11450Sstevel@tonic-gate case ZONE_STATE_RUNNING: 11460Sstevel@tonic-gate case ZONE_STATE_SHUTTING_DOWN: 11470Sstevel@tonic-gate case ZONE_STATE_DOWN: 11480Sstevel@tonic-gate switch (cmd) { 11490Sstevel@tonic-gate case Z_READY: 1150*3247Sgjelinek if ((rval = zone_halt(zlogp, B_FALSE, B_TRUE)) != 0) 11510Sstevel@tonic-gate break; 1152766Scarlsonj if ((rval = zone_ready(zlogp, B_FALSE)) == 0) 11530Sstevel@tonic-gate eventstream_write(Z_EVT_ZONE_READIED); 11541645Scomay else 11551645Scomay eventstream_write(Z_EVT_ZONE_HALTED); 11560Sstevel@tonic-gate break; 11570Sstevel@tonic-gate case Z_BOOT: 11580Sstevel@tonic-gate /* 11590Sstevel@tonic-gate * We could have two clients racing to boot this 11600Sstevel@tonic-gate * zone; the second client loses, but his request 11610Sstevel@tonic-gate * doesn't fail, since the zone is now in the desired 11620Sstevel@tonic-gate * state. 11630Sstevel@tonic-gate */ 11640Sstevel@tonic-gate zerror(zlogp, B_FALSE, "zone is already booted"); 11650Sstevel@tonic-gate rval = 0; 11660Sstevel@tonic-gate break; 11670Sstevel@tonic-gate case Z_HALT: 1168*3247Sgjelinek if ((rval = zone_halt(zlogp, B_FALSE, B_FALSE)) != 0) 11690Sstevel@tonic-gate break; 11700Sstevel@tonic-gate eventstream_write(Z_EVT_ZONE_HALTED); 11710Sstevel@tonic-gate break; 11720Sstevel@tonic-gate case Z_REBOOT: 11732267Sdp (void) strlcpy(boot_args, zargp->bootbuf, 11742267Sdp sizeof (boot_args)); 11750Sstevel@tonic-gate eventstream_write(Z_EVT_ZONE_REBOOTING); 1176*3247Sgjelinek if ((rval = zone_halt(zlogp, B_FALSE, B_TRUE)) != 0) { 11771645Scomay eventstream_write(Z_EVT_ZONE_BOOTFAILED); 11782267Sdp boot_args[0] = '\0'; 11791645Scomay break; 11801645Scomay } 11811645Scomay if ((rval = zone_ready(zlogp, B_FALSE)) != 0) { 11821645Scomay eventstream_write(Z_EVT_ZONE_BOOTFAILED); 11832267Sdp boot_args[0] = '\0'; 11840Sstevel@tonic-gate break; 11851645Scomay } 11862267Sdp rval = zone_bootup(zlogp, zargp->bootbuf); 11871645Scomay audit_put_record(zlogp, uc, rval, "reboot"); 11881645Scomay if (rval != 0) { 1189*3247Sgjelinek (void) zone_halt(zlogp, B_FALSE, B_TRUE); 11901645Scomay eventstream_write(Z_EVT_ZONE_BOOTFAILED); 11910Sstevel@tonic-gate } 11922267Sdp boot_args[0] = '\0'; 11930Sstevel@tonic-gate break; 11940Sstevel@tonic-gate case Z_NOTE_UNINSTALLING: 1195766Scarlsonj case Z_MOUNT: 1196766Scarlsonj case Z_UNMOUNT: 1197766Scarlsonj zerror(zlogp, B_FALSE, "%s operation is invalid " 1198766Scarlsonj "for zones in state '%s'", z_cmd_name(cmd), 1199766Scarlsonj zone_state_str(zstate)); 12000Sstevel@tonic-gate rval = -1; 12010Sstevel@tonic-gate break; 12020Sstevel@tonic-gate } 12030Sstevel@tonic-gate break; 12040Sstevel@tonic-gate default: 12050Sstevel@tonic-gate abort(); 12060Sstevel@tonic-gate } 12070Sstevel@tonic-gate 12080Sstevel@tonic-gate /* 12090Sstevel@tonic-gate * Because the state of the zone may have changed, we make sure 12100Sstevel@tonic-gate * to wake the console poller, which is in charge of initiating 12110Sstevel@tonic-gate * the shutdown procedure as necessary. 12120Sstevel@tonic-gate */ 12130Sstevel@tonic-gate eventstream_write(Z_EVT_NULL); 12140Sstevel@tonic-gate 12150Sstevel@tonic-gate out: 12160Sstevel@tonic-gate (void) mutex_unlock(&lock); 12170Sstevel@tonic-gate if (kernelcall) { 12180Sstevel@tonic-gate rvalp = NULL; 12190Sstevel@tonic-gate rlen = 0; 12200Sstevel@tonic-gate } else { 12210Sstevel@tonic-gate rvalp->rval = rval; 12220Sstevel@tonic-gate } 12230Sstevel@tonic-gate if (uc != NULL) 12240Sstevel@tonic-gate ucred_free(uc); 12250Sstevel@tonic-gate (void) door_return((char *)rvalp, rlen, NULL, 0); 12260Sstevel@tonic-gate thr_exit(NULL); 12270Sstevel@tonic-gate } 12280Sstevel@tonic-gate 12290Sstevel@tonic-gate static int 12300Sstevel@tonic-gate setup_door(zlog_t *zlogp) 12310Sstevel@tonic-gate { 12320Sstevel@tonic-gate if ((zone_door = door_create(server, NULL, 12330Sstevel@tonic-gate DOOR_UNREF | DOOR_REFUSE_DESC | DOOR_NO_CANCEL)) < 0) { 12340Sstevel@tonic-gate zerror(zlogp, B_TRUE, "%s failed", "door_create"); 12350Sstevel@tonic-gate return (-1); 12360Sstevel@tonic-gate } 12370Sstevel@tonic-gate (void) fdetach(zone_door_path); 12380Sstevel@tonic-gate 12390Sstevel@tonic-gate if (fattach(zone_door, zone_door_path) != 0) { 12400Sstevel@tonic-gate zerror(zlogp, B_TRUE, "fattach to %s failed", zone_door_path); 12410Sstevel@tonic-gate (void) door_revoke(zone_door); 12420Sstevel@tonic-gate (void) fdetach(zone_door_path); 12430Sstevel@tonic-gate zone_door = -1; 12440Sstevel@tonic-gate return (-1); 12450Sstevel@tonic-gate } 12460Sstevel@tonic-gate return (0); 12470Sstevel@tonic-gate } 12480Sstevel@tonic-gate 12490Sstevel@tonic-gate /* 12500Sstevel@tonic-gate * zoneadm(1m) will start zoneadmd if it thinks it isn't running; this 12510Sstevel@tonic-gate * is where zoneadmd itself will check to see that another instance of 12520Sstevel@tonic-gate * zoneadmd isn't already controlling this zone. 12530Sstevel@tonic-gate * 12540Sstevel@tonic-gate * The idea here is that we want to open the path to which we will 12550Sstevel@tonic-gate * attach our door, lock it, and then make sure that no-one has beat us 12560Sstevel@tonic-gate * to fattach(3c)ing onto it. 12570Sstevel@tonic-gate * 12580Sstevel@tonic-gate * fattach(3c) is really a mount, so there are actually two possible 12590Sstevel@tonic-gate * vnodes we could be dealing with. Our strategy is as follows: 12600Sstevel@tonic-gate * 12610Sstevel@tonic-gate * - If the file we opened is a regular file (common case): 12620Sstevel@tonic-gate * There is no fattach(3c)ed door, so we have a chance of becoming 12630Sstevel@tonic-gate * the managing zoneadmd. We attempt to lock the file: if it is 12640Sstevel@tonic-gate * already locked, that means someone else raced us here, so we 12650Sstevel@tonic-gate * lose and give up. zoneadm(1m) will try to contact the zoneadmd 12660Sstevel@tonic-gate * that beat us to it. 12670Sstevel@tonic-gate * 12680Sstevel@tonic-gate * - If the file we opened is a namefs file: 12690Sstevel@tonic-gate * This means there is already an established door fattach(3c)'ed 12700Sstevel@tonic-gate * to the rendezvous path. We've lost the race, so we give up. 12710Sstevel@tonic-gate * Note that in this case we also try to grab the file lock, and 12720Sstevel@tonic-gate * will succeed in acquiring it since the vnode locked by the 12730Sstevel@tonic-gate * "winning" zoneadmd was a regular one, and the one we locked was 12740Sstevel@tonic-gate * the fattach(3c)'ed door node. At any rate, no harm is done, and 12750Sstevel@tonic-gate * we just return to zoneadm(1m) which knows to retry. 12760Sstevel@tonic-gate */ 12770Sstevel@tonic-gate static int 12780Sstevel@tonic-gate make_daemon_exclusive(zlog_t *zlogp) 12790Sstevel@tonic-gate { 12800Sstevel@tonic-gate int doorfd = -1; 12810Sstevel@tonic-gate int err, ret = -1; 12820Sstevel@tonic-gate struct stat st; 12830Sstevel@tonic-gate struct flock flock; 12840Sstevel@tonic-gate zone_state_t zstate; 12850Sstevel@tonic-gate 12860Sstevel@tonic-gate top: 12870Sstevel@tonic-gate if ((err = zone_get_state(zone_name, &zstate)) != Z_OK) { 12882267Sdp zerror(zlogp, B_FALSE, "failed to get zone state: %s", 12890Sstevel@tonic-gate zonecfg_strerror(err)); 12900Sstevel@tonic-gate goto out; 12910Sstevel@tonic-gate } 12920Sstevel@tonic-gate if ((doorfd = open(zone_door_path, O_CREAT|O_RDWR, 12930Sstevel@tonic-gate S_IREAD|S_IWRITE)) < 0) { 12940Sstevel@tonic-gate zerror(zlogp, B_TRUE, "failed to open %s", zone_door_path); 12950Sstevel@tonic-gate goto out; 12960Sstevel@tonic-gate } 12970Sstevel@tonic-gate if (fstat(doorfd, &st) < 0) { 12980Sstevel@tonic-gate zerror(zlogp, B_TRUE, "failed to stat %s", zone_door_path); 12990Sstevel@tonic-gate goto out; 13000Sstevel@tonic-gate } 13010Sstevel@tonic-gate /* 13020Sstevel@tonic-gate * Lock the file to synchronize with other zoneadmd 13030Sstevel@tonic-gate */ 13040Sstevel@tonic-gate flock.l_type = F_WRLCK; 13050Sstevel@tonic-gate flock.l_whence = SEEK_SET; 13060Sstevel@tonic-gate flock.l_start = (off_t)0; 13070Sstevel@tonic-gate flock.l_len = (off_t)0; 13080Sstevel@tonic-gate if (fcntl(doorfd, F_SETLK, &flock) < 0) { 13090Sstevel@tonic-gate /* 13100Sstevel@tonic-gate * Someone else raced us here and grabbed the lock file 13110Sstevel@tonic-gate * first. A warning here is inappropriate since nothing 13120Sstevel@tonic-gate * went wrong. 13130Sstevel@tonic-gate */ 13140Sstevel@tonic-gate goto out; 13150Sstevel@tonic-gate } 13160Sstevel@tonic-gate 13170Sstevel@tonic-gate if (strcmp(st.st_fstype, "namefs") == 0) { 13180Sstevel@tonic-gate struct door_info info; 13190Sstevel@tonic-gate 13200Sstevel@tonic-gate /* 13210Sstevel@tonic-gate * There is already something fattach()'ed to this file. 13220Sstevel@tonic-gate * Lets see what the door is up to. 13230Sstevel@tonic-gate */ 13240Sstevel@tonic-gate if (door_info(doorfd, &info) == 0 && info.di_target != -1) { 13250Sstevel@tonic-gate /* 13260Sstevel@tonic-gate * Another zoneadmd process seems to be in 13270Sstevel@tonic-gate * control of the situation and we don't need to 13280Sstevel@tonic-gate * be here. A warning here is inappropriate 13290Sstevel@tonic-gate * since nothing went wrong. 13300Sstevel@tonic-gate * 13310Sstevel@tonic-gate * If the door has been revoked, the zoneadmd 13320Sstevel@tonic-gate * process currently managing the zone is going 13330Sstevel@tonic-gate * away. We'll return control to zoneadm(1m) 13340Sstevel@tonic-gate * which will try again (by which time zoneadmd 13350Sstevel@tonic-gate * will hopefully have exited). 13360Sstevel@tonic-gate */ 13370Sstevel@tonic-gate goto out; 13380Sstevel@tonic-gate } 13390Sstevel@tonic-gate 13400Sstevel@tonic-gate /* 13410Sstevel@tonic-gate * If we got this far, there's a fattach(3c)'ed door 13420Sstevel@tonic-gate * that belongs to a process that has exited, which can 13430Sstevel@tonic-gate * happen if the previous zoneadmd died unexpectedly. 13440Sstevel@tonic-gate * 13450Sstevel@tonic-gate * Let user know that something is amiss, but that we can 13460Sstevel@tonic-gate * recover; if the zone is in the installed state, then don't 13470Sstevel@tonic-gate * message, since having a running zoneadmd isn't really 13480Sstevel@tonic-gate * expected/needed. We want to keep occurences of this message 13490Sstevel@tonic-gate * limited to times when zoneadmd is picking back up from a 13500Sstevel@tonic-gate * zoneadmd that died while the zone was in some non-trivial 13510Sstevel@tonic-gate * state. 13520Sstevel@tonic-gate */ 13530Sstevel@tonic-gate if (zstate > ZONE_STATE_INSTALLED) { 13540Sstevel@tonic-gate zerror(zlogp, B_FALSE, 13550Sstevel@tonic-gate "zone '%s': WARNING: zone is in state '%s', but " 13560Sstevel@tonic-gate "zoneadmd does not appear to be available; " 13570Sstevel@tonic-gate "restarted zoneadmd to recover.", 13580Sstevel@tonic-gate zone_name, zone_state_str(zstate)); 13590Sstevel@tonic-gate } 13600Sstevel@tonic-gate 13610Sstevel@tonic-gate (void) fdetach(zone_door_path); 13620Sstevel@tonic-gate (void) close(doorfd); 13630Sstevel@tonic-gate goto top; 13640Sstevel@tonic-gate } 13650Sstevel@tonic-gate ret = 0; 13660Sstevel@tonic-gate out: 13670Sstevel@tonic-gate (void) close(doorfd); 13680Sstevel@tonic-gate return (ret); 13690Sstevel@tonic-gate } 13700Sstevel@tonic-gate 13710Sstevel@tonic-gate int 13720Sstevel@tonic-gate main(int argc, char *argv[]) 13730Sstevel@tonic-gate { 13740Sstevel@tonic-gate int opt; 13750Sstevel@tonic-gate zoneid_t zid; 13760Sstevel@tonic-gate priv_set_t *privset; 13770Sstevel@tonic-gate zone_state_t zstate; 13780Sstevel@tonic-gate char parents_locale[MAXPATHLEN]; 13792727Sedp brand_handle_t bh; 13800Sstevel@tonic-gate int err; 13810Sstevel@tonic-gate 13820Sstevel@tonic-gate pid_t pid; 13830Sstevel@tonic-gate sigset_t blockset; 13840Sstevel@tonic-gate sigset_t block_cld; 13850Sstevel@tonic-gate 13860Sstevel@tonic-gate struct { 13870Sstevel@tonic-gate sema_t sem; 13880Sstevel@tonic-gate int status; 13890Sstevel@tonic-gate zlog_t log; 13900Sstevel@tonic-gate } *shstate; 13910Sstevel@tonic-gate size_t shstatelen = getpagesize(); 13920Sstevel@tonic-gate 13930Sstevel@tonic-gate zlog_t errlog; 13940Sstevel@tonic-gate zlog_t *zlogp; 13950Sstevel@tonic-gate 13962382Sdp int ctfd; 13972382Sdp 13980Sstevel@tonic-gate progname = get_execbasename(argv[0]); 13990Sstevel@tonic-gate 14000Sstevel@tonic-gate /* 14010Sstevel@tonic-gate * Make sure stderr is unbuffered 14020Sstevel@tonic-gate */ 14030Sstevel@tonic-gate (void) setbuffer(stderr, NULL, 0); 14040Sstevel@tonic-gate 14050Sstevel@tonic-gate /* 14060Sstevel@tonic-gate * Get out of the way of mounted filesystems, since we will daemonize 14070Sstevel@tonic-gate * soon. 14080Sstevel@tonic-gate */ 14090Sstevel@tonic-gate (void) chdir("/"); 14100Sstevel@tonic-gate 14110Sstevel@tonic-gate /* 14120Sstevel@tonic-gate * Use the default system umask per PSARC 1998/110 rather than 14130Sstevel@tonic-gate * anything that may have been set by the caller. 14140Sstevel@tonic-gate */ 14150Sstevel@tonic-gate (void) umask(CMASK); 14160Sstevel@tonic-gate 14170Sstevel@tonic-gate /* 14180Sstevel@tonic-gate * Initially we want to use our parent's locale. 14190Sstevel@tonic-gate */ 14200Sstevel@tonic-gate (void) setlocale(LC_ALL, ""); 14210Sstevel@tonic-gate (void) textdomain(TEXT_DOMAIN); 14220Sstevel@tonic-gate (void) strlcpy(parents_locale, setlocale(LC_MESSAGES, NULL), 14230Sstevel@tonic-gate sizeof (parents_locale)); 14240Sstevel@tonic-gate 14250Sstevel@tonic-gate /* 14260Sstevel@tonic-gate * This zlog_t is used for writing to stderr 14270Sstevel@tonic-gate */ 14280Sstevel@tonic-gate errlog.logfile = stderr; 14290Sstevel@tonic-gate errlog.buflen = errlog.loglen = 0; 14300Sstevel@tonic-gate errlog.buf = errlog.log = NULL; 14310Sstevel@tonic-gate errlog.locale = parents_locale; 14320Sstevel@tonic-gate 14330Sstevel@tonic-gate /* 14340Sstevel@tonic-gate * We start off writing to stderr until we're ready to daemonize. 14350Sstevel@tonic-gate */ 14360Sstevel@tonic-gate zlogp = &errlog; 14370Sstevel@tonic-gate 14380Sstevel@tonic-gate /* 14390Sstevel@tonic-gate * Process options. 14400Sstevel@tonic-gate */ 1441766Scarlsonj while ((opt = getopt(argc, argv, "R:z:")) != EOF) { 14420Sstevel@tonic-gate switch (opt) { 1443766Scarlsonj case 'R': 1444766Scarlsonj zonecfg_set_root(optarg); 1445766Scarlsonj break; 14460Sstevel@tonic-gate case 'z': 14470Sstevel@tonic-gate zone_name = optarg; 14480Sstevel@tonic-gate break; 14490Sstevel@tonic-gate default: 14500Sstevel@tonic-gate usage(); 14510Sstevel@tonic-gate } 14520Sstevel@tonic-gate } 14530Sstevel@tonic-gate 14540Sstevel@tonic-gate if (zone_name == NULL) 14550Sstevel@tonic-gate usage(); 14560Sstevel@tonic-gate 14570Sstevel@tonic-gate /* 14580Sstevel@tonic-gate * Because usage() prints directly to stderr, it has gettext() 14590Sstevel@tonic-gate * wrapping, which depends on the locale. But since zerror() calls 14600Sstevel@tonic-gate * localize() which tweaks the locale, it is not safe to call zerror() 14610Sstevel@tonic-gate * until after the last call to usage(). Fortunately, the last call 14620Sstevel@tonic-gate * to usage() is just above and the first call to zerror() is just 14630Sstevel@tonic-gate * below. Don't mess this up. 14640Sstevel@tonic-gate */ 14650Sstevel@tonic-gate if (strcmp(zone_name, GLOBAL_ZONENAME) == 0) { 14660Sstevel@tonic-gate zerror(zlogp, B_FALSE, "cannot manage the %s zone", 14670Sstevel@tonic-gate GLOBAL_ZONENAME); 14680Sstevel@tonic-gate return (1); 14690Sstevel@tonic-gate } 14700Sstevel@tonic-gate 14710Sstevel@tonic-gate if (zone_get_id(zone_name, &zid) != 0) { 14722267Sdp zerror(zlogp, B_FALSE, "could not manage %s: %s", zone_name, 14730Sstevel@tonic-gate zonecfg_strerror(Z_NO_ZONE)); 14740Sstevel@tonic-gate return (1); 14750Sstevel@tonic-gate } 14760Sstevel@tonic-gate 14770Sstevel@tonic-gate if ((err = zone_get_state(zone_name, &zstate)) != Z_OK) { 14782267Sdp zerror(zlogp, B_FALSE, "failed to get zone state: %s", 14790Sstevel@tonic-gate zonecfg_strerror(err)); 14800Sstevel@tonic-gate return (1); 14810Sstevel@tonic-gate } 14822712Snn35248 if (zstate < ZONE_STATE_INCOMPLETE) { 14830Sstevel@tonic-gate zerror(zlogp, B_FALSE, 14840Sstevel@tonic-gate "cannot manage a zone which is in state '%s'", 14850Sstevel@tonic-gate zone_state_str(zstate)); 14860Sstevel@tonic-gate return (1); 14870Sstevel@tonic-gate } 14880Sstevel@tonic-gate 14892712Snn35248 /* Get a handle to the brand info for this zone */ 14902712Snn35248 if ((zone_get_brand(zone_name, brand_name, sizeof (brand_name)) 14912727Sedp != Z_OK) || (bh = brand_open(brand_name)) == NULL) { 14922712Snn35248 zerror(zlogp, B_FALSE, "unable to determine zone brand"); 14932712Snn35248 return (1); 14942712Snn35248 } 14952727Sedp zone_isnative = brand_is_native(bh); 14962727Sedp brand_close(bh); 14972712Snn35248 14980Sstevel@tonic-gate /* 14990Sstevel@tonic-gate * Check that we have all privileges. It would be nice to pare 15000Sstevel@tonic-gate * this down, but this is at least a first cut. 15010Sstevel@tonic-gate */ 15020Sstevel@tonic-gate if ((privset = priv_allocset()) == NULL) { 15030Sstevel@tonic-gate zerror(zlogp, B_TRUE, "%s failed", "priv_allocset"); 15040Sstevel@tonic-gate return (1); 15050Sstevel@tonic-gate } 15060Sstevel@tonic-gate 15070Sstevel@tonic-gate if (getppriv(PRIV_EFFECTIVE, privset) != 0) { 15080Sstevel@tonic-gate zerror(zlogp, B_TRUE, "%s failed", "getppriv"); 15090Sstevel@tonic-gate priv_freeset(privset); 15100Sstevel@tonic-gate return (1); 15110Sstevel@tonic-gate } 15120Sstevel@tonic-gate 15130Sstevel@tonic-gate if (priv_isfullset(privset) == B_FALSE) { 15140Sstevel@tonic-gate zerror(zlogp, B_FALSE, "You lack sufficient privilege to " 15152267Sdp "run this command (all privs required)"); 15160Sstevel@tonic-gate priv_freeset(privset); 15170Sstevel@tonic-gate return (1); 15180Sstevel@tonic-gate } 15190Sstevel@tonic-gate priv_freeset(privset); 15200Sstevel@tonic-gate 15210Sstevel@tonic-gate if (mkzonedir(zlogp) != 0) 15220Sstevel@tonic-gate return (1); 15230Sstevel@tonic-gate 15240Sstevel@tonic-gate /* 15250Sstevel@tonic-gate * Pre-fork: setup shared state 15260Sstevel@tonic-gate */ 15270Sstevel@tonic-gate if ((shstate = (void *)mmap(NULL, shstatelen, 15280Sstevel@tonic-gate PROT_READ|PROT_WRITE, MAP_SHARED|MAP_ANON, -1, (off_t)0)) == 15290Sstevel@tonic-gate MAP_FAILED) { 15300Sstevel@tonic-gate zerror(zlogp, B_TRUE, "%s failed", "mmap"); 15310Sstevel@tonic-gate return (1); 15320Sstevel@tonic-gate } 15330Sstevel@tonic-gate if (sema_init(&shstate->sem, 0, USYNC_PROCESS, NULL) != 0) { 15340Sstevel@tonic-gate zerror(zlogp, B_TRUE, "%s failed", "sema_init()"); 15350Sstevel@tonic-gate (void) munmap((char *)shstate, shstatelen); 15360Sstevel@tonic-gate return (1); 15370Sstevel@tonic-gate } 15380Sstevel@tonic-gate shstate->log.logfile = NULL; 15390Sstevel@tonic-gate shstate->log.buflen = shstatelen - sizeof (*shstate); 15400Sstevel@tonic-gate shstate->log.loglen = shstate->log.buflen; 15410Sstevel@tonic-gate shstate->log.buf = (char *)shstate + sizeof (*shstate); 15420Sstevel@tonic-gate shstate->log.log = shstate->log.buf; 15430Sstevel@tonic-gate shstate->log.locale = parents_locale; 15440Sstevel@tonic-gate shstate->status = -1; 15450Sstevel@tonic-gate 15460Sstevel@tonic-gate /* 15470Sstevel@tonic-gate * We need a SIGCHLD handler so the sema_wait() below will wake 15480Sstevel@tonic-gate * up if the child dies without doing a sema_post(). 15490Sstevel@tonic-gate */ 15500Sstevel@tonic-gate (void) sigset(SIGCHLD, sigchld); 15510Sstevel@tonic-gate /* 15520Sstevel@tonic-gate * We must mask SIGCHLD until after we've coped with the fork 15530Sstevel@tonic-gate * sufficiently to deal with it; otherwise we can race and 15540Sstevel@tonic-gate * receive the signal before pid has been initialized 15550Sstevel@tonic-gate * (yes, this really happens). 15560Sstevel@tonic-gate */ 15570Sstevel@tonic-gate (void) sigemptyset(&block_cld); 15580Sstevel@tonic-gate (void) sigaddset(&block_cld, SIGCHLD); 15590Sstevel@tonic-gate (void) sigprocmask(SIG_BLOCK, &block_cld, NULL); 15600Sstevel@tonic-gate 15612382Sdp if ((ctfd = init_template()) == -1) { 15622382Sdp zerror(zlogp, B_TRUE, "failed to create contract"); 15632382Sdp return (1); 15642382Sdp } 15652382Sdp 15660Sstevel@tonic-gate /* 15670Sstevel@tonic-gate * Do not let another thread localize a message while we are forking. 15680Sstevel@tonic-gate */ 15690Sstevel@tonic-gate (void) mutex_lock(&msglock); 15700Sstevel@tonic-gate pid = fork(); 15710Sstevel@tonic-gate (void) mutex_unlock(&msglock); 15722382Sdp 15732382Sdp /* 15742382Sdp * In all cases (parent, child, and in the event of an error) we 15752382Sdp * don't want to cause creation of contracts on subsequent fork()s. 15762382Sdp */ 15772382Sdp (void) ct_tmpl_clear(ctfd); 15782382Sdp (void) close(ctfd); 15792382Sdp 15800Sstevel@tonic-gate if (pid == -1) { 15810Sstevel@tonic-gate zerror(zlogp, B_TRUE, "could not fork"); 15820Sstevel@tonic-gate return (1); 15830Sstevel@tonic-gate 15840Sstevel@tonic-gate } else if (pid > 0) { /* parent */ 15850Sstevel@tonic-gate (void) sigprocmask(SIG_UNBLOCK, &block_cld, NULL); 15860Sstevel@tonic-gate /* 15870Sstevel@tonic-gate * This marks a window of vulnerability in which we receive 15880Sstevel@tonic-gate * the SIGCLD before falling into sema_wait (normally we would 15890Sstevel@tonic-gate * get woken up from sema_wait with EINTR upon receipt of 15900Sstevel@tonic-gate * SIGCLD). So we may need to use some other scheme like 15910Sstevel@tonic-gate * sema_posting in the sigcld handler. 15920Sstevel@tonic-gate * blech 15930Sstevel@tonic-gate */ 15940Sstevel@tonic-gate (void) sema_wait(&shstate->sem); 15950Sstevel@tonic-gate (void) sema_destroy(&shstate->sem); 15960Sstevel@tonic-gate if (shstate->status != 0) 15970Sstevel@tonic-gate (void) waitpid(pid, NULL, WNOHANG); 15980Sstevel@tonic-gate /* 15990Sstevel@tonic-gate * It's ok if we die with SIGPIPE. It's not like we could have 16000Sstevel@tonic-gate * done anything about it. 16010Sstevel@tonic-gate */ 16020Sstevel@tonic-gate (void) fprintf(stderr, "%s", shstate->log.buf); 16030Sstevel@tonic-gate _exit(shstate->status == 0 ? 0 : 1); 16040Sstevel@tonic-gate } 16050Sstevel@tonic-gate 16060Sstevel@tonic-gate /* 16070Sstevel@tonic-gate * The child charges on. 16080Sstevel@tonic-gate */ 16090Sstevel@tonic-gate (void) sigset(SIGCHLD, SIG_DFL); 16100Sstevel@tonic-gate (void) sigprocmask(SIG_UNBLOCK, &block_cld, NULL); 16110Sstevel@tonic-gate 16120Sstevel@tonic-gate /* 16130Sstevel@tonic-gate * SIGPIPE can be delivered if we write to a socket for which the 16140Sstevel@tonic-gate * peer endpoint is gone. That can lead to too-early termination 16150Sstevel@tonic-gate * of zoneadmd, and that's not good eats. 16160Sstevel@tonic-gate */ 16170Sstevel@tonic-gate (void) sigset(SIGPIPE, SIG_IGN); 16180Sstevel@tonic-gate /* 16190Sstevel@tonic-gate * Stop using stderr 16200Sstevel@tonic-gate */ 16210Sstevel@tonic-gate zlogp = &shstate->log; 16220Sstevel@tonic-gate 16230Sstevel@tonic-gate /* 16240Sstevel@tonic-gate * We don't need stdout/stderr from now on. 16250Sstevel@tonic-gate */ 16260Sstevel@tonic-gate closefrom(0); 16270Sstevel@tonic-gate 16280Sstevel@tonic-gate /* 16290Sstevel@tonic-gate * Initialize the syslog zlog_t. This needs to be done after 16300Sstevel@tonic-gate * the call to closefrom(). 16310Sstevel@tonic-gate */ 16320Sstevel@tonic-gate logsys.buf = logsys.log = NULL; 16330Sstevel@tonic-gate logsys.buflen = logsys.loglen = 0; 16340Sstevel@tonic-gate logsys.logfile = NULL; 16350Sstevel@tonic-gate logsys.locale = DEFAULT_LOCALE; 16360Sstevel@tonic-gate 16370Sstevel@tonic-gate openlog("zoneadmd", LOG_PID, LOG_DAEMON); 16380Sstevel@tonic-gate 16390Sstevel@tonic-gate /* 16400Sstevel@tonic-gate * The eventstream is used to publish state changes in the zone 16410Sstevel@tonic-gate * from the door threads to the console I/O poller. 16420Sstevel@tonic-gate */ 16430Sstevel@tonic-gate if (eventstream_init() == -1) { 16440Sstevel@tonic-gate zerror(zlogp, B_TRUE, "unable to create eventstream"); 16450Sstevel@tonic-gate goto child_out; 16460Sstevel@tonic-gate } 16470Sstevel@tonic-gate 16480Sstevel@tonic-gate (void) snprintf(zone_door_path, sizeof (zone_door_path), 1649766Scarlsonj "%s" ZONE_DOOR_PATH, zonecfg_get_root(), zone_name); 16500Sstevel@tonic-gate 16510Sstevel@tonic-gate /* 16520Sstevel@tonic-gate * See if another zoneadmd is running for this zone. If not, then we 16530Sstevel@tonic-gate * can now modify system state. 16540Sstevel@tonic-gate */ 16550Sstevel@tonic-gate if (make_daemon_exclusive(zlogp) == -1) 16560Sstevel@tonic-gate goto child_out; 16570Sstevel@tonic-gate 16580Sstevel@tonic-gate 16590Sstevel@tonic-gate /* 16600Sstevel@tonic-gate * Create/join a new session; we need to be careful of what we do with 16610Sstevel@tonic-gate * the console from now on so we don't end up being the session leader 16620Sstevel@tonic-gate * for the terminal we're going to be handing out. 16630Sstevel@tonic-gate */ 16640Sstevel@tonic-gate (void) setsid(); 16650Sstevel@tonic-gate 16660Sstevel@tonic-gate /* 16670Sstevel@tonic-gate * This thread shouldn't be receiving any signals; in particular, 16680Sstevel@tonic-gate * SIGCHLD should be received by the thread doing the fork(). 16690Sstevel@tonic-gate */ 16700Sstevel@tonic-gate (void) sigfillset(&blockset); 16710Sstevel@tonic-gate (void) thr_sigsetmask(SIG_BLOCK, &blockset, NULL); 16720Sstevel@tonic-gate 16730Sstevel@tonic-gate /* 16740Sstevel@tonic-gate * Setup the console device and get ready to serve the console; 16750Sstevel@tonic-gate * once this has completed, we're ready to let console clients 16760Sstevel@tonic-gate * make an attempt to connect (they will block until 16770Sstevel@tonic-gate * serve_console_sock() below gets called, and any pending 16780Sstevel@tonic-gate * connection is accept()ed). 16790Sstevel@tonic-gate */ 1680766Scarlsonj if (!zonecfg_in_alt_root() && init_console(zlogp) == -1) 16810Sstevel@tonic-gate goto child_out; 16820Sstevel@tonic-gate 16830Sstevel@tonic-gate /* 16840Sstevel@tonic-gate * Take the lock now, so that when the door server gets going, we 16850Sstevel@tonic-gate * are guaranteed that it won't take a request until we are sure 16860Sstevel@tonic-gate * that everything is completely set up. See the child_out: label 16870Sstevel@tonic-gate * below to see why this matters. 16880Sstevel@tonic-gate */ 16890Sstevel@tonic-gate (void) mutex_lock(&lock); 16900Sstevel@tonic-gate 1691766Scarlsonj /* Init semaphore for scratch zones. */ 1692766Scarlsonj if (sema_init(&scratch_sem, 0, USYNC_THREAD, NULL) == -1) { 1693766Scarlsonj zerror(zlogp, B_TRUE, 1694766Scarlsonj "failed to initialize semaphore for scratch zone"); 1695766Scarlsonj goto child_out; 1696766Scarlsonj } 1697766Scarlsonj 16980Sstevel@tonic-gate /* 16990Sstevel@tonic-gate * Note: door setup must occur *after* the console is setup. 17000Sstevel@tonic-gate * This is so that as zlogin tests the door to see if zoneadmd 17010Sstevel@tonic-gate * is ready yet, we know that the console will get serviced 17020Sstevel@tonic-gate * once door_info() indicates that the door is "up". 17030Sstevel@tonic-gate */ 17040Sstevel@tonic-gate if (setup_door(zlogp) == -1) 17050Sstevel@tonic-gate goto child_out; 17060Sstevel@tonic-gate 17070Sstevel@tonic-gate /* 17080Sstevel@tonic-gate * Things seem OK so far; tell the parent process that we're done 17090Sstevel@tonic-gate * with setup tasks. This will cause the parent to exit, signalling 17100Sstevel@tonic-gate * to zoneadm, zlogin, or whatever forked it that we are ready to 17110Sstevel@tonic-gate * service requests. 17120Sstevel@tonic-gate */ 17130Sstevel@tonic-gate shstate->status = 0; 17140Sstevel@tonic-gate (void) sema_post(&shstate->sem); 17150Sstevel@tonic-gate (void) munmap((char *)shstate, shstatelen); 17160Sstevel@tonic-gate shstate = NULL; 17170Sstevel@tonic-gate 17180Sstevel@tonic-gate (void) mutex_unlock(&lock); 17190Sstevel@tonic-gate 17200Sstevel@tonic-gate /* 17210Sstevel@tonic-gate * zlogp is now invalid, so reset it to the syslog logger. 17220Sstevel@tonic-gate */ 17230Sstevel@tonic-gate zlogp = &logsys; 17240Sstevel@tonic-gate 17250Sstevel@tonic-gate /* 17260Sstevel@tonic-gate * Now that we are free of any parents, switch to the default locale. 17270Sstevel@tonic-gate */ 17280Sstevel@tonic-gate (void) setlocale(LC_ALL, DEFAULT_LOCALE); 17290Sstevel@tonic-gate 17300Sstevel@tonic-gate /* 17310Sstevel@tonic-gate * At this point the setup portion of main() is basically done, so 17320Sstevel@tonic-gate * we reuse this thread to manage the zone console. When 17330Sstevel@tonic-gate * serve_console() has returned, we are past the point of no return 17340Sstevel@tonic-gate * in the life of this zoneadmd. 17350Sstevel@tonic-gate */ 1736766Scarlsonj if (zonecfg_in_alt_root()) { 1737766Scarlsonj /* 1738766Scarlsonj * This is just awful, but mounted scratch zones don't (and 1739766Scarlsonj * can't) have consoles. We just wait for unmount instead. 1740766Scarlsonj */ 1741766Scarlsonj while (sema_wait(&scratch_sem) == EINTR) 1742766Scarlsonj ; 1743766Scarlsonj } else { 1744766Scarlsonj serve_console(zlogp); 1745766Scarlsonj assert(in_death_throes); 1746766Scarlsonj } 17470Sstevel@tonic-gate 17480Sstevel@tonic-gate /* 17490Sstevel@tonic-gate * This is the next-to-last part of the exit interlock. Upon calling 17500Sstevel@tonic-gate * fdetach(), the door will go unreferenced; once any 17510Sstevel@tonic-gate * outstanding requests (like the door thread doing Z_HALT) are 17520Sstevel@tonic-gate * done, the door will get an UNREF notification; when it handles 17530Sstevel@tonic-gate * the UNREF, the door server will cause the exit. 17540Sstevel@tonic-gate */ 17550Sstevel@tonic-gate assert(!MUTEX_HELD(&lock)); 17560Sstevel@tonic-gate (void) fdetach(zone_door_path); 17570Sstevel@tonic-gate for (;;) 17580Sstevel@tonic-gate (void) pause(); 17590Sstevel@tonic-gate 17600Sstevel@tonic-gate child_out: 17610Sstevel@tonic-gate assert(pid == 0); 17620Sstevel@tonic-gate if (shstate != NULL) { 17630Sstevel@tonic-gate shstate->status = -1; 17640Sstevel@tonic-gate (void) sema_post(&shstate->sem); 17650Sstevel@tonic-gate (void) munmap((char *)shstate, shstatelen); 17660Sstevel@tonic-gate } 17670Sstevel@tonic-gate 17680Sstevel@tonic-gate /* 17690Sstevel@tonic-gate * This might trigger an unref notification, but if so, 17700Sstevel@tonic-gate * we are still holding the lock, so our call to exit will 17710Sstevel@tonic-gate * ultimately win the race and will publish the right exit 17720Sstevel@tonic-gate * code. 17730Sstevel@tonic-gate */ 17740Sstevel@tonic-gate if (zone_door != -1) { 17750Sstevel@tonic-gate assert(MUTEX_HELD(&lock)); 17760Sstevel@tonic-gate (void) door_revoke(zone_door); 17770Sstevel@tonic-gate (void) fdetach(zone_door_path); 17780Sstevel@tonic-gate } 17790Sstevel@tonic-gate return (1); /* return from main() forcibly exits an MT process */ 17800Sstevel@tonic-gate } 1781