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 /* 235829Sgjelinek * Copyright 2008 Sun Microsystems, Inc. All rights reserved. 240Sstevel@tonic-gate * Use is subject to license terms. 250Sstevel@tonic-gate */ 260Sstevel@tonic-gate 270Sstevel@tonic-gate /* 280Sstevel@tonic-gate * zoneadmd manages zones; one zoneadmd process is launched for each 290Sstevel@tonic-gate * non-global zone on the system. This daemon juggles four jobs: 300Sstevel@tonic-gate * 310Sstevel@tonic-gate * - Implement setup and teardown of the zone "virtual platform": mount and 320Sstevel@tonic-gate * unmount filesystems; create and destroy network interfaces; communicate 330Sstevel@tonic-gate * with devfsadmd to lay out devices for the zone; instantiate the zone 340Sstevel@tonic-gate * console device; configure process runtime attributes such as resource 350Sstevel@tonic-gate * controls, pool bindings, fine-grained privileges. 360Sstevel@tonic-gate * 370Sstevel@tonic-gate * - Launch the zone's init(1M) process. 380Sstevel@tonic-gate * 390Sstevel@tonic-gate * - Implement a door server; clients (like zoneadm) connect to the door 400Sstevel@tonic-gate * server and request zone state changes. The kernel is also a client of 410Sstevel@tonic-gate * this door server. A request to halt or reboot the zone which originates 420Sstevel@tonic-gate * *inside* the zone results in a door upcall from the kernel into zoneadmd. 430Sstevel@tonic-gate * 440Sstevel@tonic-gate * One minor problem is that messages emitted by zoneadmd need to be passed 450Sstevel@tonic-gate * back to the zoneadm process making the request. These messages need to 460Sstevel@tonic-gate * be rendered in the client's locale; so, this is passed in as part of the 470Sstevel@tonic-gate * request. The exception is the kernel upcall to zoneadmd, in which case 480Sstevel@tonic-gate * messages are syslog'd. 490Sstevel@tonic-gate * 500Sstevel@tonic-gate * To make all of this work, the Makefile adds -a to xgettext to extract *all* 510Sstevel@tonic-gate * strings, and an exclusion file (zoneadmd.xcl) is used to exclude those 520Sstevel@tonic-gate * strings which do not need to be translated. 530Sstevel@tonic-gate * 540Sstevel@tonic-gate * - Act as a console server for zlogin -C processes; see comments in zcons.c 550Sstevel@tonic-gate * for more information about the zone console architecture. 560Sstevel@tonic-gate * 570Sstevel@tonic-gate * DESIGN NOTES 580Sstevel@tonic-gate * 590Sstevel@tonic-gate * Restart: 600Sstevel@tonic-gate * A chief design constraint of zoneadmd is that it should be restartable in 610Sstevel@tonic-gate * the case that the administrator kills it off, or it suffers a fatal error, 620Sstevel@tonic-gate * without the running zone being impacted; this is akin to being able to 630Sstevel@tonic-gate * reboot the service processor of a server without affecting the OS instance. 640Sstevel@tonic-gate */ 650Sstevel@tonic-gate 660Sstevel@tonic-gate #include <sys/param.h> 670Sstevel@tonic-gate #include <sys/mman.h> 680Sstevel@tonic-gate #include <sys/types.h> 690Sstevel@tonic-gate #include <sys/stat.h> 700Sstevel@tonic-gate #include <sys/sysmacros.h> 710Sstevel@tonic-gate 720Sstevel@tonic-gate #include <bsm/adt.h> 730Sstevel@tonic-gate #include <bsm/adt_event.h> 740Sstevel@tonic-gate 750Sstevel@tonic-gate #include <alloca.h> 760Sstevel@tonic-gate #include <assert.h> 770Sstevel@tonic-gate #include <errno.h> 780Sstevel@tonic-gate #include <door.h> 790Sstevel@tonic-gate #include <fcntl.h> 800Sstevel@tonic-gate #include <locale.h> 810Sstevel@tonic-gate #include <signal.h> 820Sstevel@tonic-gate #include <stdarg.h> 830Sstevel@tonic-gate #include <stdio.h> 840Sstevel@tonic-gate #include <stdlib.h> 850Sstevel@tonic-gate #include <string.h> 860Sstevel@tonic-gate #include <strings.h> 870Sstevel@tonic-gate #include <synch.h> 880Sstevel@tonic-gate #include <syslog.h> 890Sstevel@tonic-gate #include <thread.h> 900Sstevel@tonic-gate #include <unistd.h> 910Sstevel@tonic-gate #include <wait.h> 920Sstevel@tonic-gate #include <limits.h> 930Sstevel@tonic-gate #include <zone.h> 942712Snn35248 #include <libbrand.h> 950Sstevel@tonic-gate #include <libcontract.h> 960Sstevel@tonic-gate #include <libcontract_priv.h> 970Sstevel@tonic-gate #include <sys/contract/process.h> 980Sstevel@tonic-gate #include <sys/ctfs.h> 990Sstevel@tonic-gate 1000Sstevel@tonic-gate #include <libzonecfg.h> 1010Sstevel@tonic-gate #include "zoneadmd.h" 1020Sstevel@tonic-gate 1030Sstevel@tonic-gate static char *progname; 1040Sstevel@tonic-gate char *zone_name; /* zone which we are managing */ 1052712Snn35248 char brand_name[MAXNAMELEN]; 1062712Snn35248 boolean_t zone_isnative; 1074350Std153743 boolean_t zone_iscluster; 108766Scarlsonj static zoneid_t zone_id; 1090Sstevel@tonic-gate 110*7370S<Gerald Jelinek> static char pre_statechg_hook[2 * MAXPATHLEN]; 111*7370S<Gerald Jelinek> static char post_statechg_hook[2 * MAXPATHLEN]; 112*7370S<Gerald Jelinek> char query_hook[2 * MAXPATHLEN]; 113*7370S<Gerald Jelinek> 1142611Svp157776 zlog_t logsys; 1150Sstevel@tonic-gate 1160Sstevel@tonic-gate mutex_t lock = DEFAULTMUTEX; /* to serialize stuff */ 1170Sstevel@tonic-gate mutex_t msglock = DEFAULTMUTEX; /* for calling setlocale() */ 1180Sstevel@tonic-gate 119766Scarlsonj static sema_t scratch_sem; /* for scratch zones */ 120766Scarlsonj 1210Sstevel@tonic-gate static char zone_door_path[MAXPATHLEN]; 1220Sstevel@tonic-gate static int zone_door = -1; 1230Sstevel@tonic-gate 1240Sstevel@tonic-gate boolean_t in_death_throes = B_FALSE; /* daemon is dying */ 1250Sstevel@tonic-gate boolean_t bringup_failure_recovery = B_FALSE; /* ignore certain failures */ 1260Sstevel@tonic-gate 1270Sstevel@tonic-gate #if !defined(TEXT_DOMAIN) /* should be defined by cc -D */ 1280Sstevel@tonic-gate #define TEXT_DOMAIN "SYS_TEST" /* Use this only if it wasn't */ 1290Sstevel@tonic-gate #endif 1300Sstevel@tonic-gate 1310Sstevel@tonic-gate #define DEFAULT_LOCALE "C" 1320Sstevel@tonic-gate 133766Scarlsonj static const char * 134766Scarlsonj z_cmd_name(zone_cmd_t zcmd) 135766Scarlsonj { 136766Scarlsonj /* This list needs to match the enum in sys/zone.h */ 137766Scarlsonj static const char *zcmdstr[] = { 1382712Snn35248 "ready", "boot", "forceboot", "reboot", "halt", 1392712Snn35248 "note_uninstalling", "mount", "forcemount", "unmount" 140766Scarlsonj }; 141766Scarlsonj 142766Scarlsonj if (zcmd >= sizeof (zcmdstr) / sizeof (*zcmdstr)) 143766Scarlsonj return ("unknown"); 144766Scarlsonj else 145766Scarlsonj return (zcmdstr[(int)zcmd]); 146766Scarlsonj } 147766Scarlsonj 1480Sstevel@tonic-gate static char * 1490Sstevel@tonic-gate get_execbasename(char *execfullname) 1500Sstevel@tonic-gate { 1510Sstevel@tonic-gate char *last_slash, *execbasename; 1520Sstevel@tonic-gate 1530Sstevel@tonic-gate /* guard against '/' at end of command invocation */ 1540Sstevel@tonic-gate for (;;) { 1550Sstevel@tonic-gate last_slash = strrchr(execfullname, '/'); 1560Sstevel@tonic-gate if (last_slash == NULL) { 1570Sstevel@tonic-gate execbasename = execfullname; 1580Sstevel@tonic-gate break; 1590Sstevel@tonic-gate } else { 1600Sstevel@tonic-gate execbasename = last_slash + 1; 1610Sstevel@tonic-gate if (*execbasename == '\0') { 1620Sstevel@tonic-gate *last_slash = '\0'; 1630Sstevel@tonic-gate continue; 1640Sstevel@tonic-gate } 1650Sstevel@tonic-gate break; 1660Sstevel@tonic-gate } 1670Sstevel@tonic-gate } 1680Sstevel@tonic-gate return (execbasename); 1690Sstevel@tonic-gate } 1700Sstevel@tonic-gate 1710Sstevel@tonic-gate static void 1720Sstevel@tonic-gate usage(void) 1730Sstevel@tonic-gate { 1740Sstevel@tonic-gate (void) fprintf(stderr, gettext("Usage: %s -z zonename\n"), progname); 1750Sstevel@tonic-gate (void) fprintf(stderr, 1760Sstevel@tonic-gate gettext("\tNote: %s should not be run directly.\n"), progname); 1770Sstevel@tonic-gate exit(2); 1780Sstevel@tonic-gate } 1790Sstevel@tonic-gate 1800Sstevel@tonic-gate /* ARGSUSED */ 1810Sstevel@tonic-gate static void 1820Sstevel@tonic-gate sigchld(int sig) 1830Sstevel@tonic-gate { 1840Sstevel@tonic-gate } 1850Sstevel@tonic-gate 1860Sstevel@tonic-gate char * 1870Sstevel@tonic-gate localize_msg(char *locale, const char *msg) 1880Sstevel@tonic-gate { 1890Sstevel@tonic-gate char *out; 1900Sstevel@tonic-gate 1910Sstevel@tonic-gate (void) mutex_lock(&msglock); 1920Sstevel@tonic-gate (void) setlocale(LC_MESSAGES, locale); 1930Sstevel@tonic-gate out = gettext(msg); 1940Sstevel@tonic-gate (void) setlocale(LC_MESSAGES, DEFAULT_LOCALE); 1950Sstevel@tonic-gate (void) mutex_unlock(&msglock); 1960Sstevel@tonic-gate return (out); 1970Sstevel@tonic-gate } 1980Sstevel@tonic-gate 1990Sstevel@tonic-gate /* PRINTFLIKE3 */ 2000Sstevel@tonic-gate void 2010Sstevel@tonic-gate zerror(zlog_t *zlogp, boolean_t use_strerror, const char *fmt, ...) 2020Sstevel@tonic-gate { 2030Sstevel@tonic-gate va_list alist; 2040Sstevel@tonic-gate char buf[MAXPATHLEN * 2]; /* enough space for err msg with a path */ 2050Sstevel@tonic-gate char *bp; 2060Sstevel@tonic-gate int saved_errno = errno; 2070Sstevel@tonic-gate 2080Sstevel@tonic-gate if (zlogp == NULL) 2090Sstevel@tonic-gate return; 2100Sstevel@tonic-gate if (zlogp == &logsys) 2110Sstevel@tonic-gate (void) snprintf(buf, sizeof (buf), "[zone '%s'] ", 2120Sstevel@tonic-gate zone_name); 2130Sstevel@tonic-gate else 2140Sstevel@tonic-gate buf[0] = '\0'; 2150Sstevel@tonic-gate bp = &(buf[strlen(buf)]); 2160Sstevel@tonic-gate 2170Sstevel@tonic-gate /* 2180Sstevel@tonic-gate * In theory, the locale pointer should be set to either "C" or a 2190Sstevel@tonic-gate * char array, so it should never be NULL 2200Sstevel@tonic-gate */ 2210Sstevel@tonic-gate assert(zlogp->locale != NULL); 2220Sstevel@tonic-gate /* Locale is per process, but we are multi-threaded... */ 2230Sstevel@tonic-gate fmt = localize_msg(zlogp->locale, fmt); 2240Sstevel@tonic-gate 2250Sstevel@tonic-gate va_start(alist, fmt); 2260Sstevel@tonic-gate (void) vsnprintf(bp, sizeof (buf) - (bp - buf), fmt, alist); 2270Sstevel@tonic-gate va_end(alist); 2280Sstevel@tonic-gate bp = &(buf[strlen(buf)]); 2290Sstevel@tonic-gate if (use_strerror) 2300Sstevel@tonic-gate (void) snprintf(bp, sizeof (buf) - (bp - buf), ": %s", 2310Sstevel@tonic-gate strerror(saved_errno)); 2320Sstevel@tonic-gate if (zlogp == &logsys) { 2330Sstevel@tonic-gate (void) syslog(LOG_ERR, "%s", buf); 2340Sstevel@tonic-gate } else if (zlogp->logfile != NULL) { 2350Sstevel@tonic-gate (void) fprintf(zlogp->logfile, "%s\n", buf); 2360Sstevel@tonic-gate } else { 2370Sstevel@tonic-gate size_t buflen; 2380Sstevel@tonic-gate size_t copylen; 2390Sstevel@tonic-gate 2400Sstevel@tonic-gate buflen = snprintf(zlogp->log, zlogp->loglen, "%s\n", buf); 2410Sstevel@tonic-gate copylen = MIN(buflen, zlogp->loglen); 2420Sstevel@tonic-gate zlogp->log += copylen; 2430Sstevel@tonic-gate zlogp->loglen -= copylen; 2440Sstevel@tonic-gate } 2450Sstevel@tonic-gate } 2460Sstevel@tonic-gate 2472267Sdp /* 2482267Sdp * Emit a warning for any boot arguments which are unrecognized. Since 2492267Sdp * Solaris boot arguments are getopt(3c) compatible (see kernel(1m)), we 2502267Sdp * put the arguments into an argv style array, use getopt to process them, 2512267Sdp * and put the resultant argument string back into outargs. 2522267Sdp * 2532267Sdp * During the filtering, we pull out any arguments which are truly "boot" 2542267Sdp * arguments, leaving only those which are to be passed intact to the 2552267Sdp * progenitor process. The one we support at the moment is -i, which 2562267Sdp * indicates to the kernel which program should be launched as 'init'. 2572267Sdp * 2582267Sdp * A return of Z_INVAL indicates specifically that the arguments are 2592267Sdp * not valid; this is a non-fatal error. Except for Z_OK, all other return 2602267Sdp * values are treated as fatal. 2612267Sdp */ 2622267Sdp static int 2632267Sdp filter_bootargs(zlog_t *zlogp, const char *inargs, char *outargs, 2642267Sdp char *init_file, char *badarg) 2652267Sdp { 2662267Sdp int argc = 0, argc_save; 2672267Sdp int i; 2682267Sdp int err; 2692267Sdp char *arg, *lasts, **argv = NULL, **argv_save; 2702267Sdp char zonecfg_args[BOOTARGS_MAX]; 2712267Sdp char scratchargs[BOOTARGS_MAX], *sargs; 2722267Sdp char c; 2732267Sdp 2742267Sdp bzero(outargs, BOOTARGS_MAX); 2752267Sdp bzero(badarg, BOOTARGS_MAX); 2762267Sdp 2772267Sdp /* 2782267Sdp * If the user didn't specify transient boot arguments, check 2792267Sdp * to see if there were any specified in the zone configuration, 2802267Sdp * and use them if applicable. 2812267Sdp */ 2822267Sdp if (inargs == NULL || inargs[0] == '\0') { 2832267Sdp zone_dochandle_t handle; 2842267Sdp if ((handle = zonecfg_init_handle()) == NULL) { 2852267Sdp zerror(zlogp, B_TRUE, 2862267Sdp "getting zone configuration handle"); 2872267Sdp return (Z_BAD_HANDLE); 2882267Sdp } 2892267Sdp err = zonecfg_get_snapshot_handle(zone_name, handle); 2902267Sdp if (err != Z_OK) { 2912267Sdp zerror(zlogp, B_FALSE, 2922267Sdp "invalid configuration snapshot"); 2932267Sdp zonecfg_fini_handle(handle); 2942267Sdp return (Z_BAD_HANDLE); 2952267Sdp } 2962267Sdp 2972267Sdp bzero(zonecfg_args, sizeof (zonecfg_args)); 2982267Sdp (void) zonecfg_get_bootargs(handle, zonecfg_args, 2992267Sdp sizeof (zonecfg_args)); 3002267Sdp inargs = zonecfg_args; 3012267Sdp zonecfg_fini_handle(handle); 3022267Sdp } 3032267Sdp 3042267Sdp if (strlen(inargs) >= BOOTARGS_MAX) { 3052267Sdp zerror(zlogp, B_FALSE, "boot argument string too long"); 3062267Sdp return (Z_INVAL); 3072267Sdp } 3082267Sdp 3092267Sdp (void) strlcpy(scratchargs, inargs, sizeof (scratchargs)); 3102267Sdp sargs = scratchargs; 3112267Sdp while ((arg = strtok_r(sargs, " \t", &lasts)) != NULL) { 3122267Sdp sargs = NULL; 3132267Sdp argc++; 3142267Sdp } 3152267Sdp 3162267Sdp if ((argv = calloc(argc + 1, sizeof (char *))) == NULL) { 3172267Sdp zerror(zlogp, B_FALSE, "memory allocation failed"); 3182267Sdp return (Z_NOMEM); 3192267Sdp } 3202267Sdp 3212267Sdp argv_save = argv; 3222267Sdp argc_save = argc; 3232267Sdp 3242267Sdp (void) strlcpy(scratchargs, inargs, sizeof (scratchargs)); 3252267Sdp sargs = scratchargs; 3262267Sdp i = 0; 3272267Sdp while ((arg = strtok_r(sargs, " \t", &lasts)) != NULL) { 3282267Sdp sargs = NULL; 3292267Sdp if ((argv[i] = strdup(arg)) == NULL) { 3302267Sdp err = Z_NOMEM; 3312267Sdp zerror(zlogp, B_FALSE, "memory allocation failed"); 3322267Sdp goto done; 3332267Sdp } 3342267Sdp i++; 3352267Sdp } 3362267Sdp 3372267Sdp /* 3382267Sdp * We preserve compatibility with the Solaris system boot behavior, 3392267Sdp * which allows: 3402267Sdp * 3412267Sdp * # reboot kernel/unix -s -m verbose 3422267Sdp * 3432267Sdp * In this example, kernel/unix tells the booter what file to 3442267Sdp * boot. We don't want reboot in a zone to be gratuitously different, 3452267Sdp * so we silently ignore the boot file, if necessary. 3462267Sdp */ 3472267Sdp if (argv[0] == NULL) 3482267Sdp goto done; 3492267Sdp 3502267Sdp assert(argv[0][0] != ' '); 3512267Sdp assert(argv[0][0] != '\t'); 3522267Sdp 3532267Sdp if (argv[0][0] != '-' && argv[0][0] != '\0') { 3542267Sdp argv = &argv[1]; 3552267Sdp argc--; 3562267Sdp } 3572267Sdp 3582267Sdp optind = 0; 3592267Sdp opterr = 0; 3602267Sdp err = Z_OK; 3612712Snn35248 while ((c = getopt(argc, argv, "fi:m:s")) != -1) { 3622267Sdp switch (c) { 3632267Sdp case 'i': 3642267Sdp /* 3652267Sdp * -i is handled by the runtime and is not passed 3662267Sdp * along to userland 3672267Sdp */ 3682267Sdp (void) strlcpy(init_file, optarg, MAXPATHLEN); 3692267Sdp break; 3702712Snn35248 case 'f': 3712712Snn35248 /* This has already been processed by zoneadm */ 3722712Snn35248 break; 3732267Sdp case 'm': 3742267Sdp case 's': 3752267Sdp /* These pass through unmolested */ 3762267Sdp (void) snprintf(outargs, BOOTARGS_MAX, 3772267Sdp "%s -%c %s ", outargs, c, optarg ? optarg : ""); 3782267Sdp break; 3792267Sdp case '?': 3802267Sdp /* 3812267Sdp * We warn about unknown arguments but pass them 3822267Sdp * along anyway-- if someone wants to develop their 3832267Sdp * own init replacement, they can pass it whatever 3842267Sdp * args they want. 3852267Sdp */ 3862267Sdp err = Z_INVAL; 3872267Sdp (void) snprintf(outargs, BOOTARGS_MAX, 3882267Sdp "%s -%c", outargs, optopt); 3892267Sdp (void) snprintf(badarg, BOOTARGS_MAX, 3902267Sdp "%s -%c", badarg, optopt); 3912267Sdp break; 3922267Sdp } 3932267Sdp } 3942267Sdp 3952267Sdp /* 3962267Sdp * For Solaris Zones we warn about and discard non-option arguments. 3972267Sdp * Hence 'boot foo bar baz gub' --> 'boot'. However, to be similar 3982267Sdp * to the kernel, we concat up all the other remaining boot args. 3992267Sdp * and warn on them as a group. 4002267Sdp */ 4012267Sdp if (optind < argc) { 4022267Sdp err = Z_INVAL; 4032267Sdp while (optind < argc) { 4042267Sdp (void) snprintf(badarg, BOOTARGS_MAX, "%s%s%s", 4052267Sdp badarg, strlen(badarg) > 0 ? " " : "", 4062267Sdp argv[optind]); 4072267Sdp optind++; 4082267Sdp } 4092267Sdp zerror(zlogp, B_FALSE, "WARNING: Unused or invalid boot " 4102267Sdp "arguments `%s'.", badarg); 4112267Sdp } 4122267Sdp 4132267Sdp done: 4142267Sdp for (i = 0; i < argc_save; i++) { 4152267Sdp if (argv_save[i] != NULL) 4162267Sdp free(argv_save[i]); 4172267Sdp } 4182267Sdp free(argv_save); 4192267Sdp return (err); 4202267Sdp } 4212267Sdp 4222267Sdp 4230Sstevel@tonic-gate static int 4240Sstevel@tonic-gate mkzonedir(zlog_t *zlogp) 4250Sstevel@tonic-gate { 4260Sstevel@tonic-gate struct stat st; 4270Sstevel@tonic-gate /* 4280Sstevel@tonic-gate * We must create and lock everyone but root out of ZONES_TMPDIR 4290Sstevel@tonic-gate * since anyone can open any UNIX domain socket, regardless of 4300Sstevel@tonic-gate * its file system permissions. Sigh... 4310Sstevel@tonic-gate */ 4320Sstevel@tonic-gate if (mkdir(ZONES_TMPDIR, S_IRWXU) < 0 && errno != EEXIST) { 4330Sstevel@tonic-gate zerror(zlogp, B_TRUE, "could not mkdir '%s'", ZONES_TMPDIR); 4340Sstevel@tonic-gate return (-1); 4350Sstevel@tonic-gate } 4360Sstevel@tonic-gate /* paranoia */ 437871Scasper if ((stat(ZONES_TMPDIR, &st) < 0) || !S_ISDIR(st.st_mode)) { 4380Sstevel@tonic-gate zerror(zlogp, B_TRUE, "'%s' is not a directory", ZONES_TMPDIR); 4390Sstevel@tonic-gate return (-1); 4400Sstevel@tonic-gate } 4410Sstevel@tonic-gate (void) chmod(ZONES_TMPDIR, S_IRWXU); 4420Sstevel@tonic-gate return (0); 4430Sstevel@tonic-gate } 4440Sstevel@tonic-gate 445766Scarlsonj /* 446*7370S<Gerald Jelinek> * Run the brand's pre-state change callback, if it exists. 447*7370S<Gerald Jelinek> */ 448*7370S<Gerald Jelinek> static int 449*7370S<Gerald Jelinek> brand_prestatechg(zlog_t *zlogp, int state, int cmd) 450*7370S<Gerald Jelinek> { 451*7370S<Gerald Jelinek> char cmdbuf[2 * MAXPATHLEN]; 452*7370S<Gerald Jelinek> 453*7370S<Gerald Jelinek> if (pre_statechg_hook[0] == '\0') 454*7370S<Gerald Jelinek> return (0); 455*7370S<Gerald Jelinek> 456*7370S<Gerald Jelinek> if (snprintf(cmdbuf, sizeof (cmdbuf), "%s %d %d", pre_statechg_hook, 457*7370S<Gerald Jelinek> state, cmd) > sizeof (cmdbuf)) 458*7370S<Gerald Jelinek> return (-1); 459*7370S<Gerald Jelinek> 460*7370S<Gerald Jelinek> if (do_subproc(zlogp, cmdbuf, NULL) != 0) 461*7370S<Gerald Jelinek> return (-1); 462*7370S<Gerald Jelinek> 463*7370S<Gerald Jelinek> return (0); 464*7370S<Gerald Jelinek> } 465*7370S<Gerald Jelinek> 466*7370S<Gerald Jelinek> /* 467*7370S<Gerald Jelinek> * Run the brand's post-state change callback, if it exists. 468*7370S<Gerald Jelinek> */ 469*7370S<Gerald Jelinek> static int 470*7370S<Gerald Jelinek> brand_poststatechg(zlog_t *zlogp, int state, int cmd) 471*7370S<Gerald Jelinek> { 472*7370S<Gerald Jelinek> char cmdbuf[2 * MAXPATHLEN]; 473*7370S<Gerald Jelinek> 474*7370S<Gerald Jelinek> if (post_statechg_hook[0] == '\0') 475*7370S<Gerald Jelinek> return (0); 476*7370S<Gerald Jelinek> 477*7370S<Gerald Jelinek> if (snprintf(cmdbuf, sizeof (cmdbuf), "%s %d %d", post_statechg_hook, 478*7370S<Gerald Jelinek> state, cmd) > sizeof (cmdbuf)) 479*7370S<Gerald Jelinek> return (-1); 480*7370S<Gerald Jelinek> 481*7370S<Gerald Jelinek> if (do_subproc(zlogp, cmdbuf, NULL) != 0) 482*7370S<Gerald Jelinek> return (-1); 483*7370S<Gerald Jelinek> 484*7370S<Gerald Jelinek> return (0); 485*7370S<Gerald Jelinek> } 486*7370S<Gerald Jelinek> 487*7370S<Gerald Jelinek> /* 488766Scarlsonj * Bring a zone up to the pre-boot "ready" stage. The mount_cmd argument is 489766Scarlsonj * 'true' if this is being invoked as part of the processing for the "mount" 490766Scarlsonj * subcommand. 491766Scarlsonj */ 492766Scarlsonj static int 493*7370S<Gerald Jelinek> zone_ready(zlog_t *zlogp, zone_mnt_t mount_cmd, int zstate) 4940Sstevel@tonic-gate { 4950Sstevel@tonic-gate int err; 4960Sstevel@tonic-gate 497*7370S<Gerald Jelinek> if (brand_prestatechg(zlogp, zstate, Z_READY) != 0) 498*7370S<Gerald Jelinek> return (-1); 499*7370S<Gerald Jelinek> 5000Sstevel@tonic-gate if ((err = zonecfg_create_snapshot(zone_name)) != Z_OK) { 5010Sstevel@tonic-gate zerror(zlogp, B_FALSE, "unable to create snapshot: %s", 5020Sstevel@tonic-gate zonecfg_strerror(err)); 5030Sstevel@tonic-gate return (-1); 5040Sstevel@tonic-gate } 5050Sstevel@tonic-gate 506766Scarlsonj if ((zone_id = vplat_create(zlogp, mount_cmd)) == -1) { 5070Sstevel@tonic-gate if ((err = zonecfg_destroy_snapshot(zone_name)) != Z_OK) 5080Sstevel@tonic-gate zerror(zlogp, B_FALSE, "destroying snapshot: %s", 5090Sstevel@tonic-gate zonecfg_strerror(err)); 5100Sstevel@tonic-gate return (-1); 5110Sstevel@tonic-gate } 5122303Scarlsonj if (vplat_bringup(zlogp, mount_cmd, zone_id) != 0) { 5130Sstevel@tonic-gate bringup_failure_recovery = B_TRUE; 5145829Sgjelinek (void) vplat_teardown(NULL, (mount_cmd != Z_MNT_BOOT), B_FALSE); 5150Sstevel@tonic-gate if ((err = zonecfg_destroy_snapshot(zone_name)) != Z_OK) 5160Sstevel@tonic-gate zerror(zlogp, B_FALSE, "destroying snapshot: %s", 5170Sstevel@tonic-gate zonecfg_strerror(err)); 5180Sstevel@tonic-gate return (-1); 5190Sstevel@tonic-gate } 5200Sstevel@tonic-gate 521*7370S<Gerald Jelinek> if (brand_poststatechg(zlogp, zstate, Z_READY) != 0) 522*7370S<Gerald Jelinek> return (-1); 523*7370S<Gerald Jelinek> 5240Sstevel@tonic-gate return (0); 5250Sstevel@tonic-gate } 5260Sstevel@tonic-gate 5272303Scarlsonj int 5282303Scarlsonj init_template(void) 5290Sstevel@tonic-gate { 5300Sstevel@tonic-gate int fd; 5310Sstevel@tonic-gate int err = 0; 5320Sstevel@tonic-gate 5330Sstevel@tonic-gate fd = open64(CTFS_ROOT "/process/template", O_RDWR); 5340Sstevel@tonic-gate if (fd == -1) 5350Sstevel@tonic-gate return (-1); 5360Sstevel@tonic-gate 5370Sstevel@tonic-gate /* 5380Sstevel@tonic-gate * For now, zoneadmd doesn't do anything with the contract. 5390Sstevel@tonic-gate * Deliver no events, don't inherit, and allow it to be orphaned. 5400Sstevel@tonic-gate */ 5410Sstevel@tonic-gate err |= ct_tmpl_set_critical(fd, 0); 5420Sstevel@tonic-gate err |= ct_tmpl_set_informative(fd, 0); 5430Sstevel@tonic-gate err |= ct_pr_tmpl_set_fatal(fd, CT_PR_EV_HWERR); 5440Sstevel@tonic-gate err |= ct_pr_tmpl_set_param(fd, CT_PR_PGRPONLY | CT_PR_REGENT); 5450Sstevel@tonic-gate if (err || ct_tmpl_activate(fd)) { 5460Sstevel@tonic-gate (void) close(fd); 5470Sstevel@tonic-gate return (-1); 5480Sstevel@tonic-gate } 5490Sstevel@tonic-gate 5500Sstevel@tonic-gate return (fd); 5510Sstevel@tonic-gate } 5520Sstevel@tonic-gate 5532712Snn35248 typedef struct fs_callback { 5542712Snn35248 zlog_t *zlogp; 5552712Snn35248 zoneid_t zoneid; 5565576Sedp boolean_t mount_cmd; 5572712Snn35248 } fs_callback_t; 5582712Snn35248 5590Sstevel@tonic-gate static int 5602712Snn35248 mount_early_fs(void *data, const char *spec, const char *dir, 5612712Snn35248 const char *fstype, const char *opt) 5620Sstevel@tonic-gate { 5632712Snn35248 zlog_t *zlogp = ((fs_callback_t *)data)->zlogp; 5642712Snn35248 zoneid_t zoneid = ((fs_callback_t *)data)->zoneid; 5655576Sedp boolean_t mount_cmd = ((fs_callback_t *)data)->mount_cmd; 5665182Sedp char rootpath[MAXPATHLEN]; 5670Sstevel@tonic-gate pid_t child; 5680Sstevel@tonic-gate int child_status; 5690Sstevel@tonic-gate int tmpl_fd; 5705182Sedp int rv; 5710Sstevel@tonic-gate ctid_t ct; 5720Sstevel@tonic-gate 5735576Sedp /* determine the zone rootpath */ 5745576Sedp if (mount_cmd) { 5755576Sedp char zonepath[MAXPATHLEN]; 5765576Sedp char luroot[MAXPATHLEN]; 5775576Sedp 5785576Sedp assert(zone_isnative || zone_iscluster); 5795576Sedp 5805576Sedp if (zone_get_zonepath(zone_name, 5815576Sedp zonepath, sizeof (zonepath)) != Z_OK) { 5825576Sedp zerror(zlogp, B_FALSE, "unable to determine zone path"); 5835576Sedp return (-1); 5845576Sedp } 5855576Sedp 5865576Sedp (void) snprintf(luroot, sizeof (luroot), "%s/lu", zonepath); 5875576Sedp resolve_lofs(zlogp, luroot, sizeof (luroot)); 5885576Sedp (void) strlcpy(rootpath, luroot, sizeof (rootpath)); 5895576Sedp } else { 5905576Sedp if (zone_get_rootpath(zone_name, 5915576Sedp rootpath, sizeof (rootpath)) != Z_OK) { 5925576Sedp zerror(zlogp, B_FALSE, "unable to determine zone root"); 5935576Sedp return (-1); 5945576Sedp } 5955182Sedp } 5965182Sedp 5975182Sedp if ((rv = valid_mount_path(zlogp, rootpath, spec, dir, fstype)) < 0) { 5985182Sedp zerror(zlogp, B_FALSE, "%s%s is not a valid mount point", 5995182Sedp rootpath, dir); 6005182Sedp return (-1); 6015182Sedp } else if (rv > 0) { 6025182Sedp /* The mount point path doesn't exist, create it now. */ 6035182Sedp if (make_one_dir(zlogp, rootpath, dir, 6045182Sedp DEFAULT_DIR_MODE, DEFAULT_DIR_USER, 6055182Sedp DEFAULT_DIR_GROUP) != 0) { 6065182Sedp zerror(zlogp, B_FALSE, "failed to create mount point"); 6075182Sedp return (-1); 6085182Sedp } 6095182Sedp 6105182Sedp /* 6115182Sedp * Now this might seem weird, but we need to invoke 6125182Sedp * valid_mount_path() again. Why? Because it checks 6135182Sedp * to make sure that the mount point path is canonical, 6145182Sedp * which it can only do if the path exists, so now that 6155182Sedp * we've created the path we have to verify it again. 6165182Sedp */ 6175182Sedp if ((rv = valid_mount_path(zlogp, rootpath, spec, dir, 6185182Sedp fstype)) < 0) { 6195182Sedp zerror(zlogp, B_FALSE, 6205182Sedp "%s%s is not a valid mount point", rootpath, dir); 6215182Sedp return (-1); 6225182Sedp } 6235182Sedp } 6245182Sedp 6250Sstevel@tonic-gate if ((tmpl_fd = init_template()) == -1) { 6260Sstevel@tonic-gate zerror(zlogp, B_TRUE, "failed to create contract"); 6270Sstevel@tonic-gate return (-1); 6280Sstevel@tonic-gate } 6290Sstevel@tonic-gate 6300Sstevel@tonic-gate if ((child = fork()) == -1) { 6310Sstevel@tonic-gate (void) ct_tmpl_clear(tmpl_fd); 6320Sstevel@tonic-gate (void) close(tmpl_fd); 6330Sstevel@tonic-gate zerror(zlogp, B_TRUE, "failed to fork"); 6340Sstevel@tonic-gate return (-1); 6350Sstevel@tonic-gate 6360Sstevel@tonic-gate } else if (child == 0) { /* child */ 6372712Snn35248 char opt_buf[MAX_MNTOPT_STR]; 6382712Snn35248 int optlen = 0; 6392712Snn35248 int mflag = MS_DATA; 6402712Snn35248 6410Sstevel@tonic-gate (void) ct_tmpl_clear(tmpl_fd); 6420Sstevel@tonic-gate /* 6430Sstevel@tonic-gate * Even though there are no procs running in the zone, we 6440Sstevel@tonic-gate * do this for paranoia's sake. 6450Sstevel@tonic-gate */ 6460Sstevel@tonic-gate (void) closefrom(0); 6470Sstevel@tonic-gate 6480Sstevel@tonic-gate if (zone_enter(zoneid) == -1) { 6490Sstevel@tonic-gate _exit(errno); 6500Sstevel@tonic-gate } 6512712Snn35248 if (opt != NULL) { 6522712Snn35248 /* 6532712Snn35248 * The mount() system call is incredibly annoying. 6542712Snn35248 * If options are specified, we need to copy them 6552712Snn35248 * into a temporary buffer since the mount() system 6562712Snn35248 * call will overwrite the options string. It will 6572712Snn35248 * also fail if the new option string it wants to 6582712Snn35248 * write is bigger than the one we passed in, so 6592712Snn35248 * you must pass in a buffer of the maximum possible 6602712Snn35248 * option string length. sigh. 6612712Snn35248 */ 6622712Snn35248 (void) strlcpy(opt_buf, opt, sizeof (opt_buf)); 6632712Snn35248 opt = opt_buf; 6642712Snn35248 optlen = MAX_MNTOPT_STR; 6652712Snn35248 mflag = MS_OPTIONSTR; 6662712Snn35248 } 6672712Snn35248 if (mount(spec, dir, mflag, fstype, NULL, 0, opt, optlen) != 0) 6680Sstevel@tonic-gate _exit(errno); 6690Sstevel@tonic-gate _exit(0); 6700Sstevel@tonic-gate } 6710Sstevel@tonic-gate 6720Sstevel@tonic-gate /* parent */ 6730Sstevel@tonic-gate if (contract_latest(&ct) == -1) 6740Sstevel@tonic-gate ct = -1; 6750Sstevel@tonic-gate (void) ct_tmpl_clear(tmpl_fd); 6760Sstevel@tonic-gate (void) close(tmpl_fd); 6770Sstevel@tonic-gate if (waitpid(child, &child_status, 0) != child) { 6780Sstevel@tonic-gate /* unexpected: we must have been signalled */ 6790Sstevel@tonic-gate (void) contract_abandon_id(ct); 6800Sstevel@tonic-gate return (-1); 6810Sstevel@tonic-gate } 6820Sstevel@tonic-gate (void) contract_abandon_id(ct); 6830Sstevel@tonic-gate if (WEXITSTATUS(child_status) != 0) { 6840Sstevel@tonic-gate errno = WEXITSTATUS(child_status); 6850Sstevel@tonic-gate zerror(zlogp, B_TRUE, "mount of %s failed", dir); 6860Sstevel@tonic-gate return (-1); 6870Sstevel@tonic-gate } 6880Sstevel@tonic-gate 6890Sstevel@tonic-gate return (0); 6900Sstevel@tonic-gate } 6910Sstevel@tonic-gate 692*7370S<Gerald Jelinek> /* 693*7370S<Gerald Jelinek> * If retstr is not NULL, the output of the subproc is returned in the str, 694*7370S<Gerald Jelinek> * otherwise it is output using zerror(). Any memory allocated for retstr 695*7370S<Gerald Jelinek> * should be freed by the caller. 696*7370S<Gerald Jelinek> */ 6972712Snn35248 int 698*7370S<Gerald Jelinek> do_subproc(zlog_t *zlogp, char *cmdbuf, char **retstr) 699766Scarlsonj { 700*7370S<Gerald Jelinek> char buf[1024]; /* arbitrary large amount */ 701*7370S<Gerald Jelinek> char *inbuf; 7022712Snn35248 FILE *file; 7032712Snn35248 int status; 704*7370S<Gerald Jelinek> int rd_cnt; 705*7370S<Gerald Jelinek> 706*7370S<Gerald Jelinek> if (retstr != NULL) { 707*7370S<Gerald Jelinek> if ((*retstr = malloc(1024)) == NULL) { 708*7370S<Gerald Jelinek> zerror(zlogp, B_FALSE, "out of memory"); 709*7370S<Gerald Jelinek> return (-1); 710*7370S<Gerald Jelinek> } 711*7370S<Gerald Jelinek> inbuf = *retstr; 712*7370S<Gerald Jelinek> rd_cnt = 0; 713*7370S<Gerald Jelinek> } else { 714*7370S<Gerald Jelinek> inbuf = buf; 715*7370S<Gerald Jelinek> } 716766Scarlsonj 7172712Snn35248 file = popen(cmdbuf, "r"); 7182712Snn35248 if (file == NULL) { 7192712Snn35248 zerror(zlogp, B_TRUE, "could not launch: %s", cmdbuf); 7202525Sdp return (-1); 7212712Snn35248 } 7222525Sdp 723*7370S<Gerald Jelinek> while (fgets(inbuf, 1024, file) != NULL) { 724*7370S<Gerald Jelinek> if (retstr == NULL && zlogp != &logsys) { 7252712Snn35248 zerror(zlogp, B_FALSE, "%s", inbuf); 726*7370S<Gerald Jelinek> } else { 727*7370S<Gerald Jelinek> char *p; 728*7370S<Gerald Jelinek> 729*7370S<Gerald Jelinek> rd_cnt += 1024 - 1; 730*7370S<Gerald Jelinek> if ((p = realloc(*retstr, rd_cnt + 1024)) == NULL) { 731*7370S<Gerald Jelinek> zerror(zlogp, B_FALSE, "out of memory"); 732*7370S<Gerald Jelinek> (void) pclose(file); 733*7370S<Gerald Jelinek> return (-1); 734*7370S<Gerald Jelinek> } 735*7370S<Gerald Jelinek> 736*7370S<Gerald Jelinek> *retstr = p; 737*7370S<Gerald Jelinek> inbuf = *retstr + rd_cnt; 738*7370S<Gerald Jelinek> } 739*7370S<Gerald Jelinek> } 7402712Snn35248 status = pclose(file); 741766Scarlsonj 7422712Snn35248 if (WIFSIGNALED(status)) { 7432712Snn35248 zerror(zlogp, B_FALSE, "%s unexpectedly terminated due to " 7442712Snn35248 "signal %d", cmdbuf, WTERMSIG(status)); 745766Scarlsonj return (-1); 7462712Snn35248 } 7472712Snn35248 assert(WIFEXITED(status)); 7482712Snn35248 if (WEXITSTATUS(status) == ZEXIT_EXEC) { 7492712Snn35248 zerror(zlogp, B_FALSE, "failed to exec %s", cmdbuf); 7502712Snn35248 return (-1); 7512712Snn35248 } 7522712Snn35248 return (WEXITSTATUS(status)); 753766Scarlsonj } 754766Scarlsonj 755766Scarlsonj static int 756*7370S<Gerald Jelinek> zone_bootup(zlog_t *zlogp, const char *bootargs, int zstate) 7570Sstevel@tonic-gate { 7580Sstevel@tonic-gate zoneid_t zoneid; 7590Sstevel@tonic-gate struct stat st; 7607089Sgjelinek char zpath[MAXPATHLEN], initpath[MAXPATHLEN], init_file[MAXPATHLEN]; 7612267Sdp char nbootargs[BOOTARGS_MAX]; 7622712Snn35248 char cmdbuf[MAXPATHLEN]; 7632712Snn35248 fs_callback_t cb; 7642727Sedp brand_handle_t bh; 7652267Sdp int err; 7660Sstevel@tonic-gate 767*7370S<Gerald Jelinek> if (brand_prestatechg(zlogp, zstate, Z_BOOT) != 0) 768*7370S<Gerald Jelinek> return (-1); 769*7370S<Gerald Jelinek> 7700Sstevel@tonic-gate if (init_console_slave(zlogp) != 0) 7710Sstevel@tonic-gate return (-1); 7720Sstevel@tonic-gate reset_slave_terminal(zlogp); 7730Sstevel@tonic-gate 7740Sstevel@tonic-gate if ((zoneid = getzoneidbyname(zone_name)) == -1) { 7750Sstevel@tonic-gate zerror(zlogp, B_TRUE, "unable to get zoneid"); 7760Sstevel@tonic-gate return (-1); 7770Sstevel@tonic-gate } 7780Sstevel@tonic-gate 7792712Snn35248 cb.zlogp = zlogp; 7802712Snn35248 cb.zoneid = zoneid; 7815576Sedp cb.mount_cmd = B_FALSE; 7822712Snn35248 7832712Snn35248 /* Get a handle to the brand info for this zone */ 7842727Sedp if ((bh = brand_open(brand_name)) == NULL) { 7852712Snn35248 zerror(zlogp, B_FALSE, "unable to determine zone brand"); 7862712Snn35248 return (-1); 7872712Snn35248 } 7882712Snn35248 7892712Snn35248 /* 7902712Snn35248 * Get the list of filesystems to mount from the brand 7912712Snn35248 * configuration. These mounts are done via a thread that will 7922712Snn35248 * enter the zone, so they are done from within the context of the 7932712Snn35248 * zone. 7942712Snn35248 */ 7952727Sedp if (brand_platform_iter_mounts(bh, mount_early_fs, &cb) != 0) { 7962712Snn35248 zerror(zlogp, B_FALSE, "unable to mount filesystems"); 7972727Sedp brand_close(bh); 7980Sstevel@tonic-gate return (-1); 7992712Snn35248 } 8002712Snn35248 8012712Snn35248 /* 8022712Snn35248 * Get the brand's boot callback if it exists. 8032712Snn35248 */ 8047089Sgjelinek if (zone_get_zonepath(zone_name, zpath, sizeof (zpath)) != Z_OK) { 8057089Sgjelinek zerror(zlogp, B_FALSE, "unable to determine zone path"); 8063716Sgjelinek brand_close(bh); 8072712Snn35248 return (-1); 8082712Snn35248 } 8092712Snn35248 (void) strcpy(cmdbuf, EXEC_PREFIX); 8107089Sgjelinek if (brand_get_boot(bh, zone_name, zpath, cmdbuf + EXEC_LEN, 8117089Sgjelinek sizeof (cmdbuf) - EXEC_LEN) != 0) { 8122712Snn35248 zerror(zlogp, B_FALSE, 8132712Snn35248 "unable to determine branded zone's boot callback"); 8142727Sedp brand_close(bh); 8152712Snn35248 return (-1); 8162712Snn35248 } 8172712Snn35248 8182712Snn35248 /* Get the path for this zone's init(1M) (or equivalent) process. */ 8192727Sedp if (brand_get_initname(bh, init_file, MAXPATHLEN) != 0) { 8202712Snn35248 zerror(zlogp, B_FALSE, 8212712Snn35248 "unable to determine zone's init(1M) location"); 8222727Sedp brand_close(bh); 8232712Snn35248 return (-1); 8242712Snn35248 } 8252712Snn35248 8262727Sedp brand_close(bh); 8270Sstevel@tonic-gate 8282267Sdp err = filter_bootargs(zlogp, bootargs, nbootargs, init_file, 8292267Sdp bad_boot_arg); 8302267Sdp if (err == Z_INVAL) 8312267Sdp eventstream_write(Z_EVT_ZONE_BADARGS); 8322267Sdp else if (err != Z_OK) 8332267Sdp return (-1); 8342267Sdp 8352267Sdp assert(init_file[0] != '\0'); 8362267Sdp 8372712Snn35248 /* Try to anticipate possible problems: Make sure init is executable. */ 8387089Sgjelinek if (zone_get_rootpath(zone_name, zpath, sizeof (zpath)) != Z_OK) { 8390Sstevel@tonic-gate zerror(zlogp, B_FALSE, "unable to determine zone root"); 8400Sstevel@tonic-gate return (-1); 8410Sstevel@tonic-gate } 8422712Snn35248 8437089Sgjelinek (void) snprintf(initpath, sizeof (initpath), "%s%s", zpath, init_file); 8440Sstevel@tonic-gate 8450Sstevel@tonic-gate if (stat(initpath, &st) == -1) { 8460Sstevel@tonic-gate zerror(zlogp, B_TRUE, "could not stat %s", initpath); 8470Sstevel@tonic-gate return (-1); 8480Sstevel@tonic-gate } 8490Sstevel@tonic-gate 8500Sstevel@tonic-gate if ((st.st_mode & S_IXUSR) == 0) { 8510Sstevel@tonic-gate zerror(zlogp, B_FALSE, "%s is not executable", initpath); 8520Sstevel@tonic-gate return (-1); 8530Sstevel@tonic-gate } 8540Sstevel@tonic-gate 8552712Snn35248 /* 8562712Snn35248 * If there is a brand 'boot' callback, execute it now to give the 8572712Snn35248 * brand one last chance to do any additional setup before the zone 8582712Snn35248 * is booted. 8592712Snn35248 */ 8602712Snn35248 if ((strlen(cmdbuf) > EXEC_LEN) && 861*7370S<Gerald Jelinek> (do_subproc(zlogp, cmdbuf, NULL) != Z_OK)) { 8622712Snn35248 zerror(zlogp, B_FALSE, "%s failed", cmdbuf); 8632712Snn35248 return (-1); 8642712Snn35248 } 8652712Snn35248 8662267Sdp if (zone_setattr(zoneid, ZONE_ATTR_INITNAME, init_file, 0) == -1) { 8672267Sdp zerror(zlogp, B_TRUE, "could not set zone boot file"); 8682267Sdp return (-1); 8692267Sdp } 8702267Sdp 8712267Sdp if (zone_setattr(zoneid, ZONE_ATTR_BOOTARGS, nbootargs, 0) == -1) { 8722267Sdp zerror(zlogp, B_TRUE, "could not set zone boot arguments"); 8732267Sdp return (-1); 8742267Sdp } 8752267Sdp 8762267Sdp if (zone_boot(zoneid) == -1) { 8770Sstevel@tonic-gate zerror(zlogp, B_TRUE, "unable to boot zone"); 8780Sstevel@tonic-gate return (-1); 8790Sstevel@tonic-gate } 8800Sstevel@tonic-gate 881*7370S<Gerald Jelinek> if (brand_poststatechg(zlogp, zstate, Z_BOOT) != 0) 882*7370S<Gerald Jelinek> return (-1); 883*7370S<Gerald Jelinek> 8840Sstevel@tonic-gate return (0); 8850Sstevel@tonic-gate } 8860Sstevel@tonic-gate 8870Sstevel@tonic-gate static int 888*7370S<Gerald Jelinek> zone_halt(zlog_t *zlogp, boolean_t unmount_cmd, boolean_t rebooting, int zstate) 8890Sstevel@tonic-gate { 8900Sstevel@tonic-gate int err; 8910Sstevel@tonic-gate 892*7370S<Gerald Jelinek> if (brand_prestatechg(zlogp, zstate, Z_HALT) != 0) 893*7370S<Gerald Jelinek> return (-1); 894*7370S<Gerald Jelinek> 8953247Sgjelinek if (vplat_teardown(zlogp, unmount_cmd, rebooting) != 0) { 8960Sstevel@tonic-gate if (!bringup_failure_recovery) 8970Sstevel@tonic-gate zerror(zlogp, B_FALSE, "unable to destroy zone"); 8980Sstevel@tonic-gate return (-1); 8990Sstevel@tonic-gate } 9000Sstevel@tonic-gate 9010Sstevel@tonic-gate if ((err = zonecfg_destroy_snapshot(zone_name)) != Z_OK) 9020Sstevel@tonic-gate zerror(zlogp, B_FALSE, "destroying snapshot: %s", 9030Sstevel@tonic-gate zonecfg_strerror(err)); 9040Sstevel@tonic-gate 905*7370S<Gerald Jelinek> if (brand_poststatechg(zlogp, zstate, Z_HALT) != 0) 906*7370S<Gerald Jelinek> return (-1); 907*7370S<Gerald Jelinek> 9080Sstevel@tonic-gate return (0); 9090Sstevel@tonic-gate } 9100Sstevel@tonic-gate 9110Sstevel@tonic-gate /* 9120Sstevel@tonic-gate * Generate AUE_zone_state for a command that boots a zone. 9130Sstevel@tonic-gate */ 9140Sstevel@tonic-gate static void 9150Sstevel@tonic-gate audit_put_record(zlog_t *zlogp, ucred_t *uc, int return_val, 9160Sstevel@tonic-gate char *new_state) 9170Sstevel@tonic-gate { 9180Sstevel@tonic-gate adt_session_data_t *ah; 9190Sstevel@tonic-gate adt_event_data_t *event; 9200Sstevel@tonic-gate int pass_fail, fail_reason; 9210Sstevel@tonic-gate 9220Sstevel@tonic-gate if (!adt_audit_enabled()) 9230Sstevel@tonic-gate return; 9240Sstevel@tonic-gate 9250Sstevel@tonic-gate if (return_val == 0) { 9260Sstevel@tonic-gate pass_fail = ADT_SUCCESS; 9270Sstevel@tonic-gate fail_reason = ADT_SUCCESS; 9280Sstevel@tonic-gate } else { 9290Sstevel@tonic-gate pass_fail = ADT_FAILURE; 9300Sstevel@tonic-gate fail_reason = ADT_FAIL_VALUE_PROGRAM; 9310Sstevel@tonic-gate } 9320Sstevel@tonic-gate 9330Sstevel@tonic-gate if (adt_start_session(&ah, NULL, 0)) { 9340Sstevel@tonic-gate zerror(zlogp, B_TRUE, gettext("audit failure.")); 9350Sstevel@tonic-gate return; 9360Sstevel@tonic-gate } 9370Sstevel@tonic-gate if (adt_set_from_ucred(ah, uc, ADT_NEW)) { 9380Sstevel@tonic-gate zerror(zlogp, B_TRUE, gettext("audit failure.")); 9390Sstevel@tonic-gate (void) adt_end_session(ah); 9400Sstevel@tonic-gate return; 9410Sstevel@tonic-gate } 9420Sstevel@tonic-gate 9430Sstevel@tonic-gate event = adt_alloc_event(ah, ADT_zone_state); 9440Sstevel@tonic-gate if (event == NULL) { 9450Sstevel@tonic-gate zerror(zlogp, B_TRUE, gettext("audit failure.")); 9460Sstevel@tonic-gate (void) adt_end_session(ah); 9470Sstevel@tonic-gate return; 9480Sstevel@tonic-gate } 9490Sstevel@tonic-gate event->adt_zone_state.zonename = zone_name; 9500Sstevel@tonic-gate event->adt_zone_state.new_state = new_state; 9510Sstevel@tonic-gate 9520Sstevel@tonic-gate if (adt_put_event(event, pass_fail, fail_reason)) 9530Sstevel@tonic-gate zerror(zlogp, B_TRUE, gettext("audit failure.")); 9540Sstevel@tonic-gate 9550Sstevel@tonic-gate adt_free_event(event); 9560Sstevel@tonic-gate 9570Sstevel@tonic-gate (void) adt_end_session(ah); 9580Sstevel@tonic-gate } 9590Sstevel@tonic-gate 9600Sstevel@tonic-gate /* 9610Sstevel@tonic-gate * The main routine for the door server that deals with zone state transitions. 9620Sstevel@tonic-gate */ 9630Sstevel@tonic-gate /* ARGSUSED */ 9640Sstevel@tonic-gate static void 9650Sstevel@tonic-gate server(void *cookie, char *args, size_t alen, door_desc_t *dp, 9660Sstevel@tonic-gate uint_t n_desc) 9670Sstevel@tonic-gate { 9680Sstevel@tonic-gate ucred_t *uc = NULL; 9690Sstevel@tonic-gate const priv_set_t *eset; 9700Sstevel@tonic-gate 9710Sstevel@tonic-gate zone_state_t zstate; 9720Sstevel@tonic-gate zone_cmd_t cmd; 9730Sstevel@tonic-gate zone_cmd_arg_t *zargp; 9740Sstevel@tonic-gate 9750Sstevel@tonic-gate boolean_t kernelcall; 9760Sstevel@tonic-gate 9770Sstevel@tonic-gate int rval = -1; 9780Sstevel@tonic-gate uint64_t uniqid; 9790Sstevel@tonic-gate zoneid_t zoneid = -1; 9800Sstevel@tonic-gate zlog_t zlog; 9810Sstevel@tonic-gate zlog_t *zlogp; 9820Sstevel@tonic-gate zone_cmd_rval_t *rvalp; 9830Sstevel@tonic-gate size_t rlen = getpagesize(); /* conservative */ 9842712Snn35248 fs_callback_t cb; 9852727Sedp brand_handle_t bh; 9860Sstevel@tonic-gate 9870Sstevel@tonic-gate /* LINTED E_BAD_PTR_CAST_ALIGN */ 9880Sstevel@tonic-gate zargp = (zone_cmd_arg_t *)args; 9890Sstevel@tonic-gate 9900Sstevel@tonic-gate /* 9910Sstevel@tonic-gate * When we get the door unref message, we've fdetach'd the door, and 9920Sstevel@tonic-gate * it is time for us to shut down zoneadmd. 9930Sstevel@tonic-gate */ 9940Sstevel@tonic-gate if (zargp == DOOR_UNREF_DATA) { 9950Sstevel@tonic-gate /* 9960Sstevel@tonic-gate * See comment at end of main() for info on the last rites. 9970Sstevel@tonic-gate */ 9980Sstevel@tonic-gate exit(0); 9990Sstevel@tonic-gate } 10000Sstevel@tonic-gate 10010Sstevel@tonic-gate if (zargp == NULL) { 10020Sstevel@tonic-gate (void) door_return(NULL, 0, 0, 0); 10030Sstevel@tonic-gate } 10040Sstevel@tonic-gate 10050Sstevel@tonic-gate rvalp = alloca(rlen); 10060Sstevel@tonic-gate bzero(rvalp, rlen); 10070Sstevel@tonic-gate zlog.logfile = NULL; 10080Sstevel@tonic-gate zlog.buflen = zlog.loglen = rlen - sizeof (zone_cmd_rval_t) + 1; 10090Sstevel@tonic-gate zlog.buf = rvalp->errbuf; 10100Sstevel@tonic-gate zlog.log = zlog.buf; 10110Sstevel@tonic-gate /* defer initialization of zlog.locale until after credential check */ 10120Sstevel@tonic-gate zlogp = &zlog; 10130Sstevel@tonic-gate 10140Sstevel@tonic-gate if (alen != sizeof (zone_cmd_arg_t)) { 10150Sstevel@tonic-gate /* 10160Sstevel@tonic-gate * This really shouldn't be happening. 10170Sstevel@tonic-gate */ 10182267Sdp zerror(&logsys, B_FALSE, "argument size (%d bytes) " 10192267Sdp "unexpected (expected %d bytes)", alen, 10202267Sdp sizeof (zone_cmd_arg_t)); 10210Sstevel@tonic-gate goto out; 10220Sstevel@tonic-gate } 10230Sstevel@tonic-gate cmd = zargp->cmd; 10240Sstevel@tonic-gate 10250Sstevel@tonic-gate if (door_ucred(&uc) != 0) { 10260Sstevel@tonic-gate zerror(&logsys, B_TRUE, "door_ucred"); 10270Sstevel@tonic-gate goto out; 10280Sstevel@tonic-gate } 10290Sstevel@tonic-gate eset = ucred_getprivset(uc, PRIV_EFFECTIVE); 10300Sstevel@tonic-gate if (ucred_getzoneid(uc) != GLOBAL_ZONEID || 10310Sstevel@tonic-gate (eset != NULL ? !priv_ismember(eset, PRIV_SYS_CONFIG) : 10320Sstevel@tonic-gate ucred_geteuid(uc) != 0)) { 10330Sstevel@tonic-gate zerror(&logsys, B_FALSE, "insufficient privileges"); 10340Sstevel@tonic-gate goto out; 10350Sstevel@tonic-gate } 10360Sstevel@tonic-gate 10370Sstevel@tonic-gate kernelcall = ucred_getpid(uc) == 0; 10380Sstevel@tonic-gate 10390Sstevel@tonic-gate /* 10400Sstevel@tonic-gate * This is safe because we only use a zlog_t throughout the 10410Sstevel@tonic-gate * duration of a door call; i.e., by the time the pointer 10420Sstevel@tonic-gate * might become invalid, the door call would be over. 10430Sstevel@tonic-gate */ 10440Sstevel@tonic-gate zlog.locale = kernelcall ? DEFAULT_LOCALE : zargp->locale; 10450Sstevel@tonic-gate 10460Sstevel@tonic-gate (void) mutex_lock(&lock); 10470Sstevel@tonic-gate 10480Sstevel@tonic-gate /* 10490Sstevel@tonic-gate * Once we start to really die off, we don't want more connections. 10500Sstevel@tonic-gate */ 10510Sstevel@tonic-gate if (in_death_throes) { 10520Sstevel@tonic-gate (void) mutex_unlock(&lock); 10530Sstevel@tonic-gate ucred_free(uc); 10540Sstevel@tonic-gate (void) door_return(NULL, 0, 0, 0); 10550Sstevel@tonic-gate thr_exit(NULL); 10560Sstevel@tonic-gate } 10570Sstevel@tonic-gate 10580Sstevel@tonic-gate /* 10590Sstevel@tonic-gate * Check for validity of command. 10600Sstevel@tonic-gate */ 10612712Snn35248 if (cmd != Z_READY && cmd != Z_BOOT && cmd != Z_FORCEBOOT && 10622712Snn35248 cmd != Z_REBOOT && cmd != Z_HALT && cmd != Z_NOTE_UNINSTALLING && 10632712Snn35248 cmd != Z_MOUNT && cmd != Z_FORCEMOUNT && cmd != Z_UNMOUNT) { 1064766Scarlsonj zerror(&logsys, B_FALSE, "invalid command %d", (int)cmd); 10650Sstevel@tonic-gate goto out; 10660Sstevel@tonic-gate } 10670Sstevel@tonic-gate 10680Sstevel@tonic-gate if (kernelcall && (cmd != Z_HALT && cmd != Z_REBOOT)) { 10690Sstevel@tonic-gate /* 10700Sstevel@tonic-gate * Can't happen 10710Sstevel@tonic-gate */ 10720Sstevel@tonic-gate zerror(&logsys, B_FALSE, "received unexpected kernel upcall %d", 10730Sstevel@tonic-gate cmd); 10740Sstevel@tonic-gate goto out; 10750Sstevel@tonic-gate } 10760Sstevel@tonic-gate /* 10770Sstevel@tonic-gate * We ignore the possibility of someone calling zone_create(2) 10780Sstevel@tonic-gate * explicitly; all requests must come through zoneadmd. 10790Sstevel@tonic-gate */ 10800Sstevel@tonic-gate if (zone_get_state(zone_name, &zstate) != Z_OK) { 10810Sstevel@tonic-gate /* 10820Sstevel@tonic-gate * Something terribly wrong happened 10830Sstevel@tonic-gate */ 10840Sstevel@tonic-gate zerror(&logsys, B_FALSE, "unable to determine state of zone"); 10850Sstevel@tonic-gate goto out; 10860Sstevel@tonic-gate } 10870Sstevel@tonic-gate 10880Sstevel@tonic-gate if (kernelcall) { 10890Sstevel@tonic-gate /* 10900Sstevel@tonic-gate * Kernel-initiated requests may lose their validity if the 10910Sstevel@tonic-gate * zone_t the kernel was referring to has gone away. 10920Sstevel@tonic-gate */ 10930Sstevel@tonic-gate if ((zoneid = getzoneidbyname(zone_name)) == -1 || 10940Sstevel@tonic-gate zone_getattr(zoneid, ZONE_ATTR_UNIQID, &uniqid, 10950Sstevel@tonic-gate sizeof (uniqid)) == -1 || uniqid != zargp->uniqid) { 10960Sstevel@tonic-gate /* 10970Sstevel@tonic-gate * We're not talking about the same zone. The request 10980Sstevel@tonic-gate * must have arrived too late. Return error. 10990Sstevel@tonic-gate */ 11000Sstevel@tonic-gate rval = -1; 11010Sstevel@tonic-gate goto out; 11020Sstevel@tonic-gate } 11030Sstevel@tonic-gate zlogp = &logsys; /* Log errors to syslog */ 11040Sstevel@tonic-gate } 11050Sstevel@tonic-gate 11062712Snn35248 /* 11072712Snn35248 * If we are being asked to forcibly mount or boot a zone, we 11082712Snn35248 * pretend that an INCOMPLETE zone is actually INSTALLED. 11092712Snn35248 */ 11102712Snn35248 if (zstate == ZONE_STATE_INCOMPLETE && 11112712Snn35248 (cmd == Z_FORCEBOOT || cmd == Z_FORCEMOUNT)) 11122712Snn35248 zstate = ZONE_STATE_INSTALLED; 11132712Snn35248 11140Sstevel@tonic-gate switch (zstate) { 11150Sstevel@tonic-gate case ZONE_STATE_CONFIGURED: 11160Sstevel@tonic-gate case ZONE_STATE_INCOMPLETE: 11170Sstevel@tonic-gate /* 11180Sstevel@tonic-gate * Not our area of expertise; we just print a nice message 11190Sstevel@tonic-gate * and die off. 11200Sstevel@tonic-gate */ 11210Sstevel@tonic-gate zerror(zlogp, B_FALSE, 11220Sstevel@tonic-gate "%s operation is invalid for zones in state '%s'", 1123766Scarlsonj z_cmd_name(cmd), zone_state_str(zstate)); 11240Sstevel@tonic-gate break; 11250Sstevel@tonic-gate 11260Sstevel@tonic-gate case ZONE_STATE_INSTALLED: 11270Sstevel@tonic-gate switch (cmd) { 11280Sstevel@tonic-gate case Z_READY: 1129*7370S<Gerald Jelinek> rval = zone_ready(zlogp, Z_MNT_BOOT, zstate); 11300Sstevel@tonic-gate if (rval == 0) 11310Sstevel@tonic-gate eventstream_write(Z_EVT_ZONE_READIED); 11320Sstevel@tonic-gate break; 11330Sstevel@tonic-gate case Z_BOOT: 11342712Snn35248 case Z_FORCEBOOT: 11350Sstevel@tonic-gate eventstream_write(Z_EVT_ZONE_BOOTING); 1136*7370S<Gerald Jelinek> if ((rval = zone_ready(zlogp, Z_MNT_BOOT, zstate)) 1137*7370S<Gerald Jelinek> == 0) { 1138*7370S<Gerald Jelinek> rval = zone_bootup(zlogp, zargp->bootbuf, 1139*7370S<Gerald Jelinek> zstate); 1140*7370S<Gerald Jelinek> } 11410Sstevel@tonic-gate audit_put_record(zlogp, uc, rval, "boot"); 11420Sstevel@tonic-gate if (rval != 0) { 11430Sstevel@tonic-gate bringup_failure_recovery = B_TRUE; 1144*7370S<Gerald Jelinek> (void) zone_halt(zlogp, B_FALSE, B_FALSE, 1145*7370S<Gerald Jelinek> zstate); 11461645Scomay eventstream_write(Z_EVT_ZONE_BOOTFAILED); 11470Sstevel@tonic-gate } 11480Sstevel@tonic-gate break; 11490Sstevel@tonic-gate case Z_HALT: 11500Sstevel@tonic-gate if (kernelcall) /* Invalid; can't happen */ 11510Sstevel@tonic-gate abort(); 11520Sstevel@tonic-gate /* 11530Sstevel@tonic-gate * We could have two clients racing to halt this 11540Sstevel@tonic-gate * zone; the second client loses, but his request 11550Sstevel@tonic-gate * doesn't fail, since the zone is now in the desired 11560Sstevel@tonic-gate * state. 11570Sstevel@tonic-gate */ 11580Sstevel@tonic-gate zerror(zlogp, B_FALSE, "zone is already halted"); 11590Sstevel@tonic-gate rval = 0; 11600Sstevel@tonic-gate break; 11610Sstevel@tonic-gate case Z_REBOOT: 11620Sstevel@tonic-gate if (kernelcall) /* Invalid; can't happen */ 11630Sstevel@tonic-gate abort(); 11640Sstevel@tonic-gate zerror(zlogp, B_FALSE, "%s operation is invalid " 1165766Scarlsonj "for zones in state '%s'", z_cmd_name(cmd), 11660Sstevel@tonic-gate zone_state_str(zstate)); 11670Sstevel@tonic-gate rval = -1; 11680Sstevel@tonic-gate break; 11690Sstevel@tonic-gate case Z_NOTE_UNINSTALLING: 11700Sstevel@tonic-gate if (kernelcall) /* Invalid; can't happen */ 11710Sstevel@tonic-gate abort(); 11720Sstevel@tonic-gate /* 11730Sstevel@tonic-gate * Tell the console to print out a message about this. 11740Sstevel@tonic-gate * Once it does, we will be in_death_throes. 11750Sstevel@tonic-gate */ 11760Sstevel@tonic-gate eventstream_write(Z_EVT_ZONE_UNINSTALLING); 11770Sstevel@tonic-gate break; 1178766Scarlsonj case Z_MOUNT: 11792712Snn35248 case Z_FORCEMOUNT: 1180766Scarlsonj if (kernelcall) /* Invalid; can't happen */ 1181766Scarlsonj abort(); 11824350Std153743 if (!zone_isnative && !zone_iscluster) { 11832712Snn35248 zerror(zlogp, B_FALSE, 11842712Snn35248 "%s operation is invalid for branded " 11852712Snn35248 "zones", z_cmd_name(cmd)); 11862712Snn35248 rval = -1; 11872712Snn35248 break; 11882712Snn35248 } 11892712Snn35248 11905829Sgjelinek rval = zone_ready(zlogp, 11915829Sgjelinek strcmp(zargp->bootbuf, "-U") == 0 ? 1192*7370S<Gerald Jelinek> Z_MNT_UPDATE : Z_MNT_SCRATCH, zstate); 11932712Snn35248 if (rval != 0) 11942712Snn35248 break; 11952712Snn35248 11962712Snn35248 eventstream_write(Z_EVT_ZONE_READIED); 11972712Snn35248 11982712Snn35248 /* Get a handle to the brand info for this zone */ 11992727Sedp if ((bh = brand_open(brand_name)) == NULL) { 12002712Snn35248 rval = -1; 12012712Snn35248 break; 12021645Scomay } 12031645Scomay 1204766Scarlsonj /* 12052712Snn35248 * Get the list of filesystems to mount from 12062712Snn35248 * the brand configuration. These mounts are done 12072712Snn35248 * via a thread that will enter the zone, so they 12082712Snn35248 * are done from within the context of the zone. 12092712Snn35248 */ 12102712Snn35248 cb.zlogp = zlogp; 12112712Snn35248 cb.zoneid = zone_id; 12125576Sedp cb.mount_cmd = B_TRUE; 12132727Sedp rval = brand_platform_iter_mounts(bh, 12142712Snn35248 mount_early_fs, &cb); 12152712Snn35248 12162727Sedp brand_close(bh); 12172712Snn35248 12182712Snn35248 /* 1219766Scarlsonj * Ordinarily, /dev/fd would be mounted inside the zone 1220766Scarlsonj * by svc:/system/filesystem/usr:default, but since 1221766Scarlsonj * we're not booting the zone, we need to do this 1222766Scarlsonj * manually. 1223766Scarlsonj */ 1224766Scarlsonj if (rval == 0) 12252712Snn35248 rval = mount_early_fs(&cb, 12262712Snn35248 "fd", "/dev/fd", "fd", NULL); 1227766Scarlsonj break; 1228766Scarlsonj case Z_UNMOUNT: 1229766Scarlsonj if (kernelcall) /* Invalid; can't happen */ 1230766Scarlsonj abort(); 1231766Scarlsonj zerror(zlogp, B_FALSE, "zone is already unmounted"); 1232766Scarlsonj rval = 0; 1233766Scarlsonj break; 12340Sstevel@tonic-gate } 12350Sstevel@tonic-gate break; 12360Sstevel@tonic-gate 12370Sstevel@tonic-gate case ZONE_STATE_READY: 12380Sstevel@tonic-gate switch (cmd) { 12390Sstevel@tonic-gate case Z_READY: 12400Sstevel@tonic-gate /* 12410Sstevel@tonic-gate * We could have two clients racing to ready this 12420Sstevel@tonic-gate * zone; the second client loses, but his request 12430Sstevel@tonic-gate * doesn't fail, since the zone is now in the desired 12440Sstevel@tonic-gate * state. 12450Sstevel@tonic-gate */ 12460Sstevel@tonic-gate zerror(zlogp, B_FALSE, "zone is already ready"); 12470Sstevel@tonic-gate rval = 0; 12480Sstevel@tonic-gate break; 12490Sstevel@tonic-gate case Z_BOOT: 12502267Sdp (void) strlcpy(boot_args, zargp->bootbuf, 12512267Sdp sizeof (boot_args)); 12520Sstevel@tonic-gate eventstream_write(Z_EVT_ZONE_BOOTING); 1253*7370S<Gerald Jelinek> rval = zone_bootup(zlogp, zargp->bootbuf, zstate); 12540Sstevel@tonic-gate audit_put_record(zlogp, uc, rval, "boot"); 12550Sstevel@tonic-gate if (rval != 0) { 12560Sstevel@tonic-gate bringup_failure_recovery = B_TRUE; 1257*7370S<Gerald Jelinek> (void) zone_halt(zlogp, B_FALSE, B_TRUE, 1258*7370S<Gerald Jelinek> zstate); 12591645Scomay eventstream_write(Z_EVT_ZONE_BOOTFAILED); 12600Sstevel@tonic-gate } 12612267Sdp boot_args[0] = '\0'; 12620Sstevel@tonic-gate break; 12630Sstevel@tonic-gate case Z_HALT: 12640Sstevel@tonic-gate if (kernelcall) /* Invalid; can't happen */ 12650Sstevel@tonic-gate abort(); 1266*7370S<Gerald Jelinek> if ((rval = zone_halt(zlogp, B_FALSE, B_FALSE, zstate)) 1267*7370S<Gerald Jelinek> != 0) 12680Sstevel@tonic-gate break; 12690Sstevel@tonic-gate eventstream_write(Z_EVT_ZONE_HALTED); 12700Sstevel@tonic-gate break; 12710Sstevel@tonic-gate case Z_REBOOT: 1272766Scarlsonj case Z_NOTE_UNINSTALLING: 1273766Scarlsonj case Z_MOUNT: 1274766Scarlsonj case Z_UNMOUNT: 12750Sstevel@tonic-gate if (kernelcall) /* Invalid; can't happen */ 12760Sstevel@tonic-gate abort(); 12770Sstevel@tonic-gate zerror(zlogp, B_FALSE, "%s operation is invalid " 1278766Scarlsonj "for zones in state '%s'", z_cmd_name(cmd), 12790Sstevel@tonic-gate zone_state_str(zstate)); 12800Sstevel@tonic-gate rval = -1; 12810Sstevel@tonic-gate break; 1282766Scarlsonj } 1283766Scarlsonj break; 1284766Scarlsonj 1285766Scarlsonj case ZONE_STATE_MOUNTED: 1286766Scarlsonj switch (cmd) { 1287766Scarlsonj case Z_UNMOUNT: 12880Sstevel@tonic-gate if (kernelcall) /* Invalid; can't happen */ 12890Sstevel@tonic-gate abort(); 1290*7370S<Gerald Jelinek> rval = zone_halt(zlogp, B_TRUE, B_FALSE, zstate); 12911645Scomay if (rval == 0) { 12921645Scomay eventstream_write(Z_EVT_ZONE_HALTED); 1293766Scarlsonj (void) sema_post(&scratch_sem); 12941645Scomay } 1295766Scarlsonj break; 1296766Scarlsonj default: 1297766Scarlsonj if (kernelcall) /* Invalid; can't happen */ 1298766Scarlsonj abort(); 1299766Scarlsonj zerror(zlogp, B_FALSE, "%s operation is invalid " 1300766Scarlsonj "for zones in state '%s'", z_cmd_name(cmd), 1301766Scarlsonj zone_state_str(zstate)); 13020Sstevel@tonic-gate rval = -1; 13030Sstevel@tonic-gate break; 13040Sstevel@tonic-gate } 13050Sstevel@tonic-gate break; 13060Sstevel@tonic-gate 13070Sstevel@tonic-gate case ZONE_STATE_RUNNING: 13080Sstevel@tonic-gate case ZONE_STATE_SHUTTING_DOWN: 13090Sstevel@tonic-gate case ZONE_STATE_DOWN: 13100Sstevel@tonic-gate switch (cmd) { 13110Sstevel@tonic-gate case Z_READY: 1312*7370S<Gerald Jelinek> if ((rval = zone_halt(zlogp, B_FALSE, B_TRUE, zstate)) 1313*7370S<Gerald Jelinek> != 0) 13140Sstevel@tonic-gate break; 1315*7370S<Gerald Jelinek> if ((rval = zone_ready(zlogp, Z_MNT_BOOT, zstate)) == 0) 13160Sstevel@tonic-gate eventstream_write(Z_EVT_ZONE_READIED); 13171645Scomay else 13181645Scomay eventstream_write(Z_EVT_ZONE_HALTED); 13190Sstevel@tonic-gate break; 13200Sstevel@tonic-gate case Z_BOOT: 13210Sstevel@tonic-gate /* 13220Sstevel@tonic-gate * We could have two clients racing to boot this 13230Sstevel@tonic-gate * zone; the second client loses, but his request 13240Sstevel@tonic-gate * doesn't fail, since the zone is now in the desired 13250Sstevel@tonic-gate * state. 13260Sstevel@tonic-gate */ 13270Sstevel@tonic-gate zerror(zlogp, B_FALSE, "zone is already booted"); 13280Sstevel@tonic-gate rval = 0; 13290Sstevel@tonic-gate break; 13300Sstevel@tonic-gate case Z_HALT: 1331*7370S<Gerald Jelinek> if ((rval = zone_halt(zlogp, B_FALSE, B_FALSE, zstate)) 1332*7370S<Gerald Jelinek> != 0) 13330Sstevel@tonic-gate break; 13340Sstevel@tonic-gate eventstream_write(Z_EVT_ZONE_HALTED); 13350Sstevel@tonic-gate break; 13360Sstevel@tonic-gate case Z_REBOOT: 13372267Sdp (void) strlcpy(boot_args, zargp->bootbuf, 13382267Sdp sizeof (boot_args)); 13390Sstevel@tonic-gate eventstream_write(Z_EVT_ZONE_REBOOTING); 1340*7370S<Gerald Jelinek> if ((rval = zone_halt(zlogp, B_FALSE, B_TRUE, zstate)) 1341*7370S<Gerald Jelinek> != 0) { 13421645Scomay eventstream_write(Z_EVT_ZONE_BOOTFAILED); 13432267Sdp boot_args[0] = '\0'; 13441645Scomay break; 13451645Scomay } 1346*7370S<Gerald Jelinek> if ((rval = zone_ready(zlogp, Z_MNT_BOOT, zstate)) 1347*7370S<Gerald Jelinek> != 0) { 13481645Scomay eventstream_write(Z_EVT_ZONE_BOOTFAILED); 13492267Sdp boot_args[0] = '\0'; 13500Sstevel@tonic-gate break; 13511645Scomay } 1352*7370S<Gerald Jelinek> rval = zone_bootup(zlogp, zargp->bootbuf, zstate); 13531645Scomay audit_put_record(zlogp, uc, rval, "reboot"); 13541645Scomay if (rval != 0) { 1355*7370S<Gerald Jelinek> (void) zone_halt(zlogp, B_FALSE, B_TRUE, 1356*7370S<Gerald Jelinek> zstate); 13571645Scomay eventstream_write(Z_EVT_ZONE_BOOTFAILED); 13580Sstevel@tonic-gate } 13592267Sdp boot_args[0] = '\0'; 13600Sstevel@tonic-gate break; 13610Sstevel@tonic-gate case Z_NOTE_UNINSTALLING: 1362766Scarlsonj case Z_MOUNT: 1363766Scarlsonj case Z_UNMOUNT: 1364766Scarlsonj zerror(zlogp, B_FALSE, "%s operation is invalid " 1365766Scarlsonj "for zones in state '%s'", z_cmd_name(cmd), 1366766Scarlsonj zone_state_str(zstate)); 13670Sstevel@tonic-gate rval = -1; 13680Sstevel@tonic-gate break; 13690Sstevel@tonic-gate } 13700Sstevel@tonic-gate break; 13710Sstevel@tonic-gate default: 13720Sstevel@tonic-gate abort(); 13730Sstevel@tonic-gate } 13740Sstevel@tonic-gate 13750Sstevel@tonic-gate /* 13760Sstevel@tonic-gate * Because the state of the zone may have changed, we make sure 13770Sstevel@tonic-gate * to wake the console poller, which is in charge of initiating 13780Sstevel@tonic-gate * the shutdown procedure as necessary. 13790Sstevel@tonic-gate */ 13800Sstevel@tonic-gate eventstream_write(Z_EVT_NULL); 13810Sstevel@tonic-gate 13820Sstevel@tonic-gate out: 13830Sstevel@tonic-gate (void) mutex_unlock(&lock); 13840Sstevel@tonic-gate if (kernelcall) { 13850Sstevel@tonic-gate rvalp = NULL; 13860Sstevel@tonic-gate rlen = 0; 13870Sstevel@tonic-gate } else { 13880Sstevel@tonic-gate rvalp->rval = rval; 13890Sstevel@tonic-gate } 13900Sstevel@tonic-gate if (uc != NULL) 13910Sstevel@tonic-gate ucred_free(uc); 13920Sstevel@tonic-gate (void) door_return((char *)rvalp, rlen, NULL, 0); 13930Sstevel@tonic-gate thr_exit(NULL); 13940Sstevel@tonic-gate } 13950Sstevel@tonic-gate 13960Sstevel@tonic-gate static int 13970Sstevel@tonic-gate setup_door(zlog_t *zlogp) 13980Sstevel@tonic-gate { 13990Sstevel@tonic-gate if ((zone_door = door_create(server, NULL, 14000Sstevel@tonic-gate DOOR_UNREF | DOOR_REFUSE_DESC | DOOR_NO_CANCEL)) < 0) { 14010Sstevel@tonic-gate zerror(zlogp, B_TRUE, "%s failed", "door_create"); 14020Sstevel@tonic-gate return (-1); 14030Sstevel@tonic-gate } 14040Sstevel@tonic-gate (void) fdetach(zone_door_path); 14050Sstevel@tonic-gate 14060Sstevel@tonic-gate if (fattach(zone_door, zone_door_path) != 0) { 14070Sstevel@tonic-gate zerror(zlogp, B_TRUE, "fattach to %s failed", zone_door_path); 14080Sstevel@tonic-gate (void) door_revoke(zone_door); 14090Sstevel@tonic-gate (void) fdetach(zone_door_path); 14100Sstevel@tonic-gate zone_door = -1; 14110Sstevel@tonic-gate return (-1); 14120Sstevel@tonic-gate } 14130Sstevel@tonic-gate return (0); 14140Sstevel@tonic-gate } 14150Sstevel@tonic-gate 14160Sstevel@tonic-gate /* 14170Sstevel@tonic-gate * zoneadm(1m) will start zoneadmd if it thinks it isn't running; this 14180Sstevel@tonic-gate * is where zoneadmd itself will check to see that another instance of 14190Sstevel@tonic-gate * zoneadmd isn't already controlling this zone. 14200Sstevel@tonic-gate * 14210Sstevel@tonic-gate * The idea here is that we want to open the path to which we will 14220Sstevel@tonic-gate * attach our door, lock it, and then make sure that no-one has beat us 14230Sstevel@tonic-gate * to fattach(3c)ing onto it. 14240Sstevel@tonic-gate * 14250Sstevel@tonic-gate * fattach(3c) is really a mount, so there are actually two possible 14260Sstevel@tonic-gate * vnodes we could be dealing with. Our strategy is as follows: 14270Sstevel@tonic-gate * 14280Sstevel@tonic-gate * - If the file we opened is a regular file (common case): 14290Sstevel@tonic-gate * There is no fattach(3c)ed door, so we have a chance of becoming 14300Sstevel@tonic-gate * the managing zoneadmd. We attempt to lock the file: if it is 14310Sstevel@tonic-gate * already locked, that means someone else raced us here, so we 14320Sstevel@tonic-gate * lose and give up. zoneadm(1m) will try to contact the zoneadmd 14330Sstevel@tonic-gate * that beat us to it. 14340Sstevel@tonic-gate * 14350Sstevel@tonic-gate * - If the file we opened is a namefs file: 14360Sstevel@tonic-gate * This means there is already an established door fattach(3c)'ed 14370Sstevel@tonic-gate * to the rendezvous path. We've lost the race, so we give up. 14380Sstevel@tonic-gate * Note that in this case we also try to grab the file lock, and 14390Sstevel@tonic-gate * will succeed in acquiring it since the vnode locked by the 14400Sstevel@tonic-gate * "winning" zoneadmd was a regular one, and the one we locked was 14410Sstevel@tonic-gate * the fattach(3c)'ed door node. At any rate, no harm is done, and 14420Sstevel@tonic-gate * we just return to zoneadm(1m) which knows to retry. 14430Sstevel@tonic-gate */ 14440Sstevel@tonic-gate static int 14450Sstevel@tonic-gate make_daemon_exclusive(zlog_t *zlogp) 14460Sstevel@tonic-gate { 14470Sstevel@tonic-gate int doorfd = -1; 14480Sstevel@tonic-gate int err, ret = -1; 14490Sstevel@tonic-gate struct stat st; 14500Sstevel@tonic-gate struct flock flock; 14510Sstevel@tonic-gate zone_state_t zstate; 14520Sstevel@tonic-gate 14530Sstevel@tonic-gate top: 14540Sstevel@tonic-gate if ((err = zone_get_state(zone_name, &zstate)) != Z_OK) { 14552267Sdp zerror(zlogp, B_FALSE, "failed to get zone state: %s", 14560Sstevel@tonic-gate zonecfg_strerror(err)); 14570Sstevel@tonic-gate goto out; 14580Sstevel@tonic-gate } 14590Sstevel@tonic-gate if ((doorfd = open(zone_door_path, O_CREAT|O_RDWR, 14600Sstevel@tonic-gate S_IREAD|S_IWRITE)) < 0) { 14610Sstevel@tonic-gate zerror(zlogp, B_TRUE, "failed to open %s", zone_door_path); 14620Sstevel@tonic-gate goto out; 14630Sstevel@tonic-gate } 14640Sstevel@tonic-gate if (fstat(doorfd, &st) < 0) { 14650Sstevel@tonic-gate zerror(zlogp, B_TRUE, "failed to stat %s", zone_door_path); 14660Sstevel@tonic-gate goto out; 14670Sstevel@tonic-gate } 14680Sstevel@tonic-gate /* 14690Sstevel@tonic-gate * Lock the file to synchronize with other zoneadmd 14700Sstevel@tonic-gate */ 14710Sstevel@tonic-gate flock.l_type = F_WRLCK; 14720Sstevel@tonic-gate flock.l_whence = SEEK_SET; 14730Sstevel@tonic-gate flock.l_start = (off_t)0; 14740Sstevel@tonic-gate flock.l_len = (off_t)0; 14750Sstevel@tonic-gate if (fcntl(doorfd, F_SETLK, &flock) < 0) { 14760Sstevel@tonic-gate /* 14770Sstevel@tonic-gate * Someone else raced us here and grabbed the lock file 14780Sstevel@tonic-gate * first. A warning here is inappropriate since nothing 14790Sstevel@tonic-gate * went wrong. 14800Sstevel@tonic-gate */ 14810Sstevel@tonic-gate goto out; 14820Sstevel@tonic-gate } 14830Sstevel@tonic-gate 14840Sstevel@tonic-gate if (strcmp(st.st_fstype, "namefs") == 0) { 14850Sstevel@tonic-gate struct door_info info; 14860Sstevel@tonic-gate 14870Sstevel@tonic-gate /* 14880Sstevel@tonic-gate * There is already something fattach()'ed to this file. 14890Sstevel@tonic-gate * Lets see what the door is up to. 14900Sstevel@tonic-gate */ 14910Sstevel@tonic-gate if (door_info(doorfd, &info) == 0 && info.di_target != -1) { 14920Sstevel@tonic-gate /* 14930Sstevel@tonic-gate * Another zoneadmd process seems to be in 14940Sstevel@tonic-gate * control of the situation and we don't need to 14950Sstevel@tonic-gate * be here. A warning here is inappropriate 14960Sstevel@tonic-gate * since nothing went wrong. 14970Sstevel@tonic-gate * 14980Sstevel@tonic-gate * If the door has been revoked, the zoneadmd 14990Sstevel@tonic-gate * process currently managing the zone is going 15000Sstevel@tonic-gate * away. We'll return control to zoneadm(1m) 15010Sstevel@tonic-gate * which will try again (by which time zoneadmd 15020Sstevel@tonic-gate * will hopefully have exited). 15030Sstevel@tonic-gate */ 15040Sstevel@tonic-gate goto out; 15050Sstevel@tonic-gate } 15060Sstevel@tonic-gate 15070Sstevel@tonic-gate /* 15080Sstevel@tonic-gate * If we got this far, there's a fattach(3c)'ed door 15090Sstevel@tonic-gate * that belongs to a process that has exited, which can 15100Sstevel@tonic-gate * happen if the previous zoneadmd died unexpectedly. 15110Sstevel@tonic-gate * 15120Sstevel@tonic-gate * Let user know that something is amiss, but that we can 15130Sstevel@tonic-gate * recover; if the zone is in the installed state, then don't 15140Sstevel@tonic-gate * message, since having a running zoneadmd isn't really 15150Sstevel@tonic-gate * expected/needed. We want to keep occurences of this message 15160Sstevel@tonic-gate * limited to times when zoneadmd is picking back up from a 15170Sstevel@tonic-gate * zoneadmd that died while the zone was in some non-trivial 15180Sstevel@tonic-gate * state. 15190Sstevel@tonic-gate */ 15200Sstevel@tonic-gate if (zstate > ZONE_STATE_INSTALLED) { 15210Sstevel@tonic-gate zerror(zlogp, B_FALSE, 15220Sstevel@tonic-gate "zone '%s': WARNING: zone is in state '%s', but " 15230Sstevel@tonic-gate "zoneadmd does not appear to be available; " 15240Sstevel@tonic-gate "restarted zoneadmd to recover.", 15250Sstevel@tonic-gate zone_name, zone_state_str(zstate)); 15260Sstevel@tonic-gate } 15270Sstevel@tonic-gate 15280Sstevel@tonic-gate (void) fdetach(zone_door_path); 15290Sstevel@tonic-gate (void) close(doorfd); 15300Sstevel@tonic-gate goto top; 15310Sstevel@tonic-gate } 15320Sstevel@tonic-gate ret = 0; 15330Sstevel@tonic-gate out: 15340Sstevel@tonic-gate (void) close(doorfd); 15350Sstevel@tonic-gate return (ret); 15360Sstevel@tonic-gate } 15370Sstevel@tonic-gate 1538*7370S<Gerald Jelinek> /* 1539*7370S<Gerald Jelinek> * Setup the brand's pre and post state change callbacks, as well as the 1540*7370S<Gerald Jelinek> * query callback, if any of these exist. 1541*7370S<Gerald Jelinek> */ 1542*7370S<Gerald Jelinek> static int 1543*7370S<Gerald Jelinek> brand_callback_init(brand_handle_t bh, char *zone_name) 1544*7370S<Gerald Jelinek> { 1545*7370S<Gerald Jelinek> char zpath[MAXPATHLEN]; 1546*7370S<Gerald Jelinek> 1547*7370S<Gerald Jelinek> if (zone_get_zonepath(zone_name, zpath, sizeof (zpath)) != Z_OK) 1548*7370S<Gerald Jelinek> return (-1); 1549*7370S<Gerald Jelinek> 1550*7370S<Gerald Jelinek> (void) strlcpy(pre_statechg_hook, EXEC_PREFIX, 1551*7370S<Gerald Jelinek> sizeof (pre_statechg_hook)); 1552*7370S<Gerald Jelinek> 1553*7370S<Gerald Jelinek> if (brand_get_prestatechange(bh, zone_name, zpath, 1554*7370S<Gerald Jelinek> pre_statechg_hook + EXEC_LEN, 1555*7370S<Gerald Jelinek> sizeof (pre_statechg_hook) - EXEC_LEN) != 0) 1556*7370S<Gerald Jelinek> return (-1); 1557*7370S<Gerald Jelinek> 1558*7370S<Gerald Jelinek> if (strlen(pre_statechg_hook) <= EXEC_LEN) 1559*7370S<Gerald Jelinek> pre_statechg_hook[0] = '\0'; 1560*7370S<Gerald Jelinek> 1561*7370S<Gerald Jelinek> (void) strlcpy(post_statechg_hook, EXEC_PREFIX, 1562*7370S<Gerald Jelinek> sizeof (post_statechg_hook)); 1563*7370S<Gerald Jelinek> 1564*7370S<Gerald Jelinek> if (brand_get_poststatechange(bh, zone_name, zpath, 1565*7370S<Gerald Jelinek> post_statechg_hook + EXEC_LEN, 1566*7370S<Gerald Jelinek> sizeof (post_statechg_hook) - EXEC_LEN) != 0) 1567*7370S<Gerald Jelinek> return (-1); 1568*7370S<Gerald Jelinek> 1569*7370S<Gerald Jelinek> if (strlen(post_statechg_hook) <= EXEC_LEN) 1570*7370S<Gerald Jelinek> post_statechg_hook[0] = '\0'; 1571*7370S<Gerald Jelinek> 1572*7370S<Gerald Jelinek> (void) strlcpy(query_hook, EXEC_PREFIX, 1573*7370S<Gerald Jelinek> sizeof (query_hook)); 1574*7370S<Gerald Jelinek> 1575*7370S<Gerald Jelinek> if (brand_get_query(bh, zone_name, zpath, query_hook + EXEC_LEN, 1576*7370S<Gerald Jelinek> sizeof (query_hook) - EXEC_LEN) != 0) 1577*7370S<Gerald Jelinek> return (-1); 1578*7370S<Gerald Jelinek> 1579*7370S<Gerald Jelinek> if (strlen(query_hook) <= EXEC_LEN) 1580*7370S<Gerald Jelinek> query_hook[0] = '\0'; 1581*7370S<Gerald Jelinek> 1582*7370S<Gerald Jelinek> return (0); 1583*7370S<Gerald Jelinek> } 1584*7370S<Gerald Jelinek> 15850Sstevel@tonic-gate int 15860Sstevel@tonic-gate main(int argc, char *argv[]) 15870Sstevel@tonic-gate { 15880Sstevel@tonic-gate int opt; 15890Sstevel@tonic-gate zoneid_t zid; 15900Sstevel@tonic-gate priv_set_t *privset; 15910Sstevel@tonic-gate zone_state_t zstate; 15920Sstevel@tonic-gate char parents_locale[MAXPATHLEN]; 15932727Sedp brand_handle_t bh; 15940Sstevel@tonic-gate int err; 15950Sstevel@tonic-gate 15960Sstevel@tonic-gate pid_t pid; 15970Sstevel@tonic-gate sigset_t blockset; 15980Sstevel@tonic-gate sigset_t block_cld; 15990Sstevel@tonic-gate 16000Sstevel@tonic-gate struct { 16010Sstevel@tonic-gate sema_t sem; 16020Sstevel@tonic-gate int status; 16030Sstevel@tonic-gate zlog_t log; 16040Sstevel@tonic-gate } *shstate; 16050Sstevel@tonic-gate size_t shstatelen = getpagesize(); 16060Sstevel@tonic-gate 16070Sstevel@tonic-gate zlog_t errlog; 16080Sstevel@tonic-gate zlog_t *zlogp; 16090Sstevel@tonic-gate 16102382Sdp int ctfd; 16112382Sdp 16120Sstevel@tonic-gate progname = get_execbasename(argv[0]); 16130Sstevel@tonic-gate 16140Sstevel@tonic-gate /* 16150Sstevel@tonic-gate * Make sure stderr is unbuffered 16160Sstevel@tonic-gate */ 16170Sstevel@tonic-gate (void) setbuffer(stderr, NULL, 0); 16180Sstevel@tonic-gate 16190Sstevel@tonic-gate /* 16200Sstevel@tonic-gate * Get out of the way of mounted filesystems, since we will daemonize 16210Sstevel@tonic-gate * soon. 16220Sstevel@tonic-gate */ 16230Sstevel@tonic-gate (void) chdir("/"); 16240Sstevel@tonic-gate 16250Sstevel@tonic-gate /* 16260Sstevel@tonic-gate * Use the default system umask per PSARC 1998/110 rather than 16270Sstevel@tonic-gate * anything that may have been set by the caller. 16280Sstevel@tonic-gate */ 16290Sstevel@tonic-gate (void) umask(CMASK); 16300Sstevel@tonic-gate 16310Sstevel@tonic-gate /* 16320Sstevel@tonic-gate * Initially we want to use our parent's locale. 16330Sstevel@tonic-gate */ 16340Sstevel@tonic-gate (void) setlocale(LC_ALL, ""); 16350Sstevel@tonic-gate (void) textdomain(TEXT_DOMAIN); 16360Sstevel@tonic-gate (void) strlcpy(parents_locale, setlocale(LC_MESSAGES, NULL), 16370Sstevel@tonic-gate sizeof (parents_locale)); 16380Sstevel@tonic-gate 16390Sstevel@tonic-gate /* 16400Sstevel@tonic-gate * This zlog_t is used for writing to stderr 16410Sstevel@tonic-gate */ 16420Sstevel@tonic-gate errlog.logfile = stderr; 16430Sstevel@tonic-gate errlog.buflen = errlog.loglen = 0; 16440Sstevel@tonic-gate errlog.buf = errlog.log = NULL; 16450Sstevel@tonic-gate errlog.locale = parents_locale; 16460Sstevel@tonic-gate 16470Sstevel@tonic-gate /* 16480Sstevel@tonic-gate * We start off writing to stderr until we're ready to daemonize. 16490Sstevel@tonic-gate */ 16500Sstevel@tonic-gate zlogp = &errlog; 16510Sstevel@tonic-gate 16520Sstevel@tonic-gate /* 16530Sstevel@tonic-gate * Process options. 16540Sstevel@tonic-gate */ 1655766Scarlsonj while ((opt = getopt(argc, argv, "R:z:")) != EOF) { 16560Sstevel@tonic-gate switch (opt) { 1657766Scarlsonj case 'R': 1658766Scarlsonj zonecfg_set_root(optarg); 1659766Scarlsonj break; 16600Sstevel@tonic-gate case 'z': 16610Sstevel@tonic-gate zone_name = optarg; 16620Sstevel@tonic-gate break; 16630Sstevel@tonic-gate default: 16640Sstevel@tonic-gate usage(); 16650Sstevel@tonic-gate } 16660Sstevel@tonic-gate } 16670Sstevel@tonic-gate 16680Sstevel@tonic-gate if (zone_name == NULL) 16690Sstevel@tonic-gate usage(); 16700Sstevel@tonic-gate 16710Sstevel@tonic-gate /* 16720Sstevel@tonic-gate * Because usage() prints directly to stderr, it has gettext() 16730Sstevel@tonic-gate * wrapping, which depends on the locale. But since zerror() calls 16740Sstevel@tonic-gate * localize() which tweaks the locale, it is not safe to call zerror() 16750Sstevel@tonic-gate * until after the last call to usage(). Fortunately, the last call 16760Sstevel@tonic-gate * to usage() is just above and the first call to zerror() is just 16770Sstevel@tonic-gate * below. Don't mess this up. 16780Sstevel@tonic-gate */ 16790Sstevel@tonic-gate if (strcmp(zone_name, GLOBAL_ZONENAME) == 0) { 16800Sstevel@tonic-gate zerror(zlogp, B_FALSE, "cannot manage the %s zone", 16810Sstevel@tonic-gate GLOBAL_ZONENAME); 16820Sstevel@tonic-gate return (1); 16830Sstevel@tonic-gate } 16840Sstevel@tonic-gate 16850Sstevel@tonic-gate if (zone_get_id(zone_name, &zid) != 0) { 16862267Sdp zerror(zlogp, B_FALSE, "could not manage %s: %s", zone_name, 16870Sstevel@tonic-gate zonecfg_strerror(Z_NO_ZONE)); 16880Sstevel@tonic-gate return (1); 16890Sstevel@tonic-gate } 16900Sstevel@tonic-gate 16910Sstevel@tonic-gate if ((err = zone_get_state(zone_name, &zstate)) != Z_OK) { 16922267Sdp zerror(zlogp, B_FALSE, "failed to get zone state: %s", 16930Sstevel@tonic-gate zonecfg_strerror(err)); 16940Sstevel@tonic-gate return (1); 16950Sstevel@tonic-gate } 16962712Snn35248 if (zstate < ZONE_STATE_INCOMPLETE) { 16970Sstevel@tonic-gate zerror(zlogp, B_FALSE, 16980Sstevel@tonic-gate "cannot manage a zone which is in state '%s'", 16990Sstevel@tonic-gate zone_state_str(zstate)); 17000Sstevel@tonic-gate return (1); 17010Sstevel@tonic-gate } 17020Sstevel@tonic-gate 17032712Snn35248 /* Get a handle to the brand info for this zone */ 17042712Snn35248 if ((zone_get_brand(zone_name, brand_name, sizeof (brand_name)) 17052727Sedp != Z_OK) || (bh = brand_open(brand_name)) == NULL) { 17062712Snn35248 zerror(zlogp, B_FALSE, "unable to determine zone brand"); 17072712Snn35248 return (1); 17082712Snn35248 } 17092727Sedp zone_isnative = brand_is_native(bh); 17104350Std153743 zone_iscluster = (strcmp(brand_name, CLUSTER_BRAND_NAME) == 0); 1711*7370S<Gerald Jelinek> 1712*7370S<Gerald Jelinek> /* Get state change brand hooks. */ 1713*7370S<Gerald Jelinek> if (brand_callback_init(bh, zone_name) == -1) { 1714*7370S<Gerald Jelinek> zerror(zlogp, B_TRUE, 1715*7370S<Gerald Jelinek> "failed to initialize brand state change hooks"); 1716*7370S<Gerald Jelinek> brand_close(bh); 1717*7370S<Gerald Jelinek> return (1); 1718*7370S<Gerald Jelinek> } 1719*7370S<Gerald Jelinek> 17202727Sedp brand_close(bh); 17212712Snn35248 17220Sstevel@tonic-gate /* 17230Sstevel@tonic-gate * Check that we have all privileges. It would be nice to pare 17240Sstevel@tonic-gate * this down, but this is at least a first cut. 17250Sstevel@tonic-gate */ 17260Sstevel@tonic-gate if ((privset = priv_allocset()) == NULL) { 17270Sstevel@tonic-gate zerror(zlogp, B_TRUE, "%s failed", "priv_allocset"); 17280Sstevel@tonic-gate return (1); 17290Sstevel@tonic-gate } 17300Sstevel@tonic-gate 17310Sstevel@tonic-gate if (getppriv(PRIV_EFFECTIVE, privset) != 0) { 17320Sstevel@tonic-gate zerror(zlogp, B_TRUE, "%s failed", "getppriv"); 17330Sstevel@tonic-gate priv_freeset(privset); 17340Sstevel@tonic-gate return (1); 17350Sstevel@tonic-gate } 17360Sstevel@tonic-gate 17370Sstevel@tonic-gate if (priv_isfullset(privset) == B_FALSE) { 17380Sstevel@tonic-gate zerror(zlogp, B_FALSE, "You lack sufficient privilege to " 17392267Sdp "run this command (all privs required)"); 17400Sstevel@tonic-gate priv_freeset(privset); 17410Sstevel@tonic-gate return (1); 17420Sstevel@tonic-gate } 17430Sstevel@tonic-gate priv_freeset(privset); 17440Sstevel@tonic-gate 17450Sstevel@tonic-gate if (mkzonedir(zlogp) != 0) 17460Sstevel@tonic-gate return (1); 17470Sstevel@tonic-gate 17480Sstevel@tonic-gate /* 17490Sstevel@tonic-gate * Pre-fork: setup shared state 17500Sstevel@tonic-gate */ 17510Sstevel@tonic-gate if ((shstate = (void *)mmap(NULL, shstatelen, 17520Sstevel@tonic-gate PROT_READ|PROT_WRITE, MAP_SHARED|MAP_ANON, -1, (off_t)0)) == 17530Sstevel@tonic-gate MAP_FAILED) { 17540Sstevel@tonic-gate zerror(zlogp, B_TRUE, "%s failed", "mmap"); 17550Sstevel@tonic-gate return (1); 17560Sstevel@tonic-gate } 17570Sstevel@tonic-gate if (sema_init(&shstate->sem, 0, USYNC_PROCESS, NULL) != 0) { 17580Sstevel@tonic-gate zerror(zlogp, B_TRUE, "%s failed", "sema_init()"); 17590Sstevel@tonic-gate (void) munmap((char *)shstate, shstatelen); 17600Sstevel@tonic-gate return (1); 17610Sstevel@tonic-gate } 17620Sstevel@tonic-gate shstate->log.logfile = NULL; 17630Sstevel@tonic-gate shstate->log.buflen = shstatelen - sizeof (*shstate); 17640Sstevel@tonic-gate shstate->log.loglen = shstate->log.buflen; 17650Sstevel@tonic-gate shstate->log.buf = (char *)shstate + sizeof (*shstate); 17660Sstevel@tonic-gate shstate->log.log = shstate->log.buf; 17670Sstevel@tonic-gate shstate->log.locale = parents_locale; 17680Sstevel@tonic-gate shstate->status = -1; 17690Sstevel@tonic-gate 17700Sstevel@tonic-gate /* 17710Sstevel@tonic-gate * We need a SIGCHLD handler so the sema_wait() below will wake 17720Sstevel@tonic-gate * up if the child dies without doing a sema_post(). 17730Sstevel@tonic-gate */ 17740Sstevel@tonic-gate (void) sigset(SIGCHLD, sigchld); 17750Sstevel@tonic-gate /* 17760Sstevel@tonic-gate * We must mask SIGCHLD until after we've coped with the fork 17770Sstevel@tonic-gate * sufficiently to deal with it; otherwise we can race and 17780Sstevel@tonic-gate * receive the signal before pid has been initialized 17790Sstevel@tonic-gate * (yes, this really happens). 17800Sstevel@tonic-gate */ 17810Sstevel@tonic-gate (void) sigemptyset(&block_cld); 17820Sstevel@tonic-gate (void) sigaddset(&block_cld, SIGCHLD); 17830Sstevel@tonic-gate (void) sigprocmask(SIG_BLOCK, &block_cld, NULL); 17840Sstevel@tonic-gate 17852382Sdp if ((ctfd = init_template()) == -1) { 17862382Sdp zerror(zlogp, B_TRUE, "failed to create contract"); 17872382Sdp return (1); 17882382Sdp } 17892382Sdp 17900Sstevel@tonic-gate /* 17910Sstevel@tonic-gate * Do not let another thread localize a message while we are forking. 17920Sstevel@tonic-gate */ 17930Sstevel@tonic-gate (void) mutex_lock(&msglock); 17940Sstevel@tonic-gate pid = fork(); 17950Sstevel@tonic-gate (void) mutex_unlock(&msglock); 17962382Sdp 17972382Sdp /* 17982382Sdp * In all cases (parent, child, and in the event of an error) we 17992382Sdp * don't want to cause creation of contracts on subsequent fork()s. 18002382Sdp */ 18012382Sdp (void) ct_tmpl_clear(ctfd); 18022382Sdp (void) close(ctfd); 18032382Sdp 18040Sstevel@tonic-gate if (pid == -1) { 18050Sstevel@tonic-gate zerror(zlogp, B_TRUE, "could not fork"); 18060Sstevel@tonic-gate return (1); 18070Sstevel@tonic-gate 18080Sstevel@tonic-gate } else if (pid > 0) { /* parent */ 18090Sstevel@tonic-gate (void) sigprocmask(SIG_UNBLOCK, &block_cld, NULL); 18100Sstevel@tonic-gate /* 18110Sstevel@tonic-gate * This marks a window of vulnerability in which we receive 18120Sstevel@tonic-gate * the SIGCLD before falling into sema_wait (normally we would 18130Sstevel@tonic-gate * get woken up from sema_wait with EINTR upon receipt of 18140Sstevel@tonic-gate * SIGCLD). So we may need to use some other scheme like 18150Sstevel@tonic-gate * sema_posting in the sigcld handler. 18160Sstevel@tonic-gate * blech 18170Sstevel@tonic-gate */ 18180Sstevel@tonic-gate (void) sema_wait(&shstate->sem); 18190Sstevel@tonic-gate (void) sema_destroy(&shstate->sem); 18200Sstevel@tonic-gate if (shstate->status != 0) 18210Sstevel@tonic-gate (void) waitpid(pid, NULL, WNOHANG); 18220Sstevel@tonic-gate /* 18230Sstevel@tonic-gate * It's ok if we die with SIGPIPE. It's not like we could have 18240Sstevel@tonic-gate * done anything about it. 18250Sstevel@tonic-gate */ 18260Sstevel@tonic-gate (void) fprintf(stderr, "%s", shstate->log.buf); 18270Sstevel@tonic-gate _exit(shstate->status == 0 ? 0 : 1); 18280Sstevel@tonic-gate } 18290Sstevel@tonic-gate 18300Sstevel@tonic-gate /* 18310Sstevel@tonic-gate * The child charges on. 18320Sstevel@tonic-gate */ 18330Sstevel@tonic-gate (void) sigset(SIGCHLD, SIG_DFL); 18340Sstevel@tonic-gate (void) sigprocmask(SIG_UNBLOCK, &block_cld, NULL); 18350Sstevel@tonic-gate 18360Sstevel@tonic-gate /* 18370Sstevel@tonic-gate * SIGPIPE can be delivered if we write to a socket for which the 18380Sstevel@tonic-gate * peer endpoint is gone. That can lead to too-early termination 18390Sstevel@tonic-gate * of zoneadmd, and that's not good eats. 18400Sstevel@tonic-gate */ 18410Sstevel@tonic-gate (void) sigset(SIGPIPE, SIG_IGN); 18420Sstevel@tonic-gate /* 18430Sstevel@tonic-gate * Stop using stderr 18440Sstevel@tonic-gate */ 18450Sstevel@tonic-gate zlogp = &shstate->log; 18460Sstevel@tonic-gate 18470Sstevel@tonic-gate /* 18480Sstevel@tonic-gate * We don't need stdout/stderr from now on. 18490Sstevel@tonic-gate */ 18500Sstevel@tonic-gate closefrom(0); 18510Sstevel@tonic-gate 18520Sstevel@tonic-gate /* 18530Sstevel@tonic-gate * Initialize the syslog zlog_t. This needs to be done after 18540Sstevel@tonic-gate * the call to closefrom(). 18550Sstevel@tonic-gate */ 18560Sstevel@tonic-gate logsys.buf = logsys.log = NULL; 18570Sstevel@tonic-gate logsys.buflen = logsys.loglen = 0; 18580Sstevel@tonic-gate logsys.logfile = NULL; 18590Sstevel@tonic-gate logsys.locale = DEFAULT_LOCALE; 18600Sstevel@tonic-gate 18610Sstevel@tonic-gate openlog("zoneadmd", LOG_PID, LOG_DAEMON); 18620Sstevel@tonic-gate 18630Sstevel@tonic-gate /* 18640Sstevel@tonic-gate * The eventstream is used to publish state changes in the zone 18650Sstevel@tonic-gate * from the door threads to the console I/O poller. 18660Sstevel@tonic-gate */ 18670Sstevel@tonic-gate if (eventstream_init() == -1) { 18680Sstevel@tonic-gate zerror(zlogp, B_TRUE, "unable to create eventstream"); 18690Sstevel@tonic-gate goto child_out; 18700Sstevel@tonic-gate } 18710Sstevel@tonic-gate 18720Sstevel@tonic-gate (void) snprintf(zone_door_path, sizeof (zone_door_path), 1873766Scarlsonj "%s" ZONE_DOOR_PATH, zonecfg_get_root(), zone_name); 18740Sstevel@tonic-gate 18750Sstevel@tonic-gate /* 18760Sstevel@tonic-gate * See if another zoneadmd is running for this zone. If not, then we 18770Sstevel@tonic-gate * can now modify system state. 18780Sstevel@tonic-gate */ 18790Sstevel@tonic-gate if (make_daemon_exclusive(zlogp) == -1) 18800Sstevel@tonic-gate goto child_out; 18810Sstevel@tonic-gate 18820Sstevel@tonic-gate 18830Sstevel@tonic-gate /* 18840Sstevel@tonic-gate * Create/join a new session; we need to be careful of what we do with 18850Sstevel@tonic-gate * the console from now on so we don't end up being the session leader 18860Sstevel@tonic-gate * for the terminal we're going to be handing out. 18870Sstevel@tonic-gate */ 18880Sstevel@tonic-gate (void) setsid(); 18890Sstevel@tonic-gate 18900Sstevel@tonic-gate /* 18910Sstevel@tonic-gate * This thread shouldn't be receiving any signals; in particular, 18920Sstevel@tonic-gate * SIGCHLD should be received by the thread doing the fork(). 18930Sstevel@tonic-gate */ 18940Sstevel@tonic-gate (void) sigfillset(&blockset); 18950Sstevel@tonic-gate (void) thr_sigsetmask(SIG_BLOCK, &blockset, NULL); 18960Sstevel@tonic-gate 18970Sstevel@tonic-gate /* 18980Sstevel@tonic-gate * Setup the console device and get ready to serve the console; 18990Sstevel@tonic-gate * once this has completed, we're ready to let console clients 19000Sstevel@tonic-gate * make an attempt to connect (they will block until 19010Sstevel@tonic-gate * serve_console_sock() below gets called, and any pending 19020Sstevel@tonic-gate * connection is accept()ed). 19030Sstevel@tonic-gate */ 1904766Scarlsonj if (!zonecfg_in_alt_root() && init_console(zlogp) == -1) 19050Sstevel@tonic-gate goto child_out; 19060Sstevel@tonic-gate 19070Sstevel@tonic-gate /* 19080Sstevel@tonic-gate * Take the lock now, so that when the door server gets going, we 19090Sstevel@tonic-gate * are guaranteed that it won't take a request until we are sure 19100Sstevel@tonic-gate * that everything is completely set up. See the child_out: label 19110Sstevel@tonic-gate * below to see why this matters. 19120Sstevel@tonic-gate */ 19130Sstevel@tonic-gate (void) mutex_lock(&lock); 19140Sstevel@tonic-gate 1915766Scarlsonj /* Init semaphore for scratch zones. */ 1916766Scarlsonj if (sema_init(&scratch_sem, 0, USYNC_THREAD, NULL) == -1) { 1917766Scarlsonj zerror(zlogp, B_TRUE, 1918766Scarlsonj "failed to initialize semaphore for scratch zone"); 1919766Scarlsonj goto child_out; 1920766Scarlsonj } 1921766Scarlsonj 19220Sstevel@tonic-gate /* 19230Sstevel@tonic-gate * Note: door setup must occur *after* the console is setup. 19240Sstevel@tonic-gate * This is so that as zlogin tests the door to see if zoneadmd 19250Sstevel@tonic-gate * is ready yet, we know that the console will get serviced 19260Sstevel@tonic-gate * once door_info() indicates that the door is "up". 19270Sstevel@tonic-gate */ 19280Sstevel@tonic-gate if (setup_door(zlogp) == -1) 19290Sstevel@tonic-gate goto child_out; 19300Sstevel@tonic-gate 19310Sstevel@tonic-gate /* 19320Sstevel@tonic-gate * Things seem OK so far; tell the parent process that we're done 19330Sstevel@tonic-gate * with setup tasks. This will cause the parent to exit, signalling 19340Sstevel@tonic-gate * to zoneadm, zlogin, or whatever forked it that we are ready to 19350Sstevel@tonic-gate * service requests. 19360Sstevel@tonic-gate */ 19370Sstevel@tonic-gate shstate->status = 0; 19380Sstevel@tonic-gate (void) sema_post(&shstate->sem); 19390Sstevel@tonic-gate (void) munmap((char *)shstate, shstatelen); 19400Sstevel@tonic-gate shstate = NULL; 19410Sstevel@tonic-gate 19420Sstevel@tonic-gate (void) mutex_unlock(&lock); 19430Sstevel@tonic-gate 19440Sstevel@tonic-gate /* 19450Sstevel@tonic-gate * zlogp is now invalid, so reset it to the syslog logger. 19460Sstevel@tonic-gate */ 19470Sstevel@tonic-gate zlogp = &logsys; 19480Sstevel@tonic-gate 19490Sstevel@tonic-gate /* 19500Sstevel@tonic-gate * Now that we are free of any parents, switch to the default locale. 19510Sstevel@tonic-gate */ 19520Sstevel@tonic-gate (void) setlocale(LC_ALL, DEFAULT_LOCALE); 19530Sstevel@tonic-gate 19540Sstevel@tonic-gate /* 19550Sstevel@tonic-gate * At this point the setup portion of main() is basically done, so 19560Sstevel@tonic-gate * we reuse this thread to manage the zone console. When 19570Sstevel@tonic-gate * serve_console() has returned, we are past the point of no return 19580Sstevel@tonic-gate * in the life of this zoneadmd. 19590Sstevel@tonic-gate */ 1960766Scarlsonj if (zonecfg_in_alt_root()) { 1961766Scarlsonj /* 1962766Scarlsonj * This is just awful, but mounted scratch zones don't (and 1963766Scarlsonj * can't) have consoles. We just wait for unmount instead. 1964766Scarlsonj */ 1965766Scarlsonj while (sema_wait(&scratch_sem) == EINTR) 1966766Scarlsonj ; 1967766Scarlsonj } else { 1968766Scarlsonj serve_console(zlogp); 1969766Scarlsonj assert(in_death_throes); 1970766Scarlsonj } 19710Sstevel@tonic-gate 19720Sstevel@tonic-gate /* 19730Sstevel@tonic-gate * This is the next-to-last part of the exit interlock. Upon calling 19740Sstevel@tonic-gate * fdetach(), the door will go unreferenced; once any 19750Sstevel@tonic-gate * outstanding requests (like the door thread doing Z_HALT) are 19760Sstevel@tonic-gate * done, the door will get an UNREF notification; when it handles 19770Sstevel@tonic-gate * the UNREF, the door server will cause the exit. 19780Sstevel@tonic-gate */ 19790Sstevel@tonic-gate assert(!MUTEX_HELD(&lock)); 19800Sstevel@tonic-gate (void) fdetach(zone_door_path); 19810Sstevel@tonic-gate for (;;) 19820Sstevel@tonic-gate (void) pause(); 19830Sstevel@tonic-gate 19840Sstevel@tonic-gate child_out: 19850Sstevel@tonic-gate assert(pid == 0); 19860Sstevel@tonic-gate if (shstate != NULL) { 19870Sstevel@tonic-gate shstate->status = -1; 19880Sstevel@tonic-gate (void) sema_post(&shstate->sem); 19890Sstevel@tonic-gate (void) munmap((char *)shstate, shstatelen); 19900Sstevel@tonic-gate } 19910Sstevel@tonic-gate 19920Sstevel@tonic-gate /* 19930Sstevel@tonic-gate * This might trigger an unref notification, but if so, 19940Sstevel@tonic-gate * we are still holding the lock, so our call to exit will 19950Sstevel@tonic-gate * ultimately win the race and will publish the right exit 19960Sstevel@tonic-gate * code. 19970Sstevel@tonic-gate */ 19980Sstevel@tonic-gate if (zone_door != -1) { 19990Sstevel@tonic-gate assert(MUTEX_HELD(&lock)); 20000Sstevel@tonic-gate (void) door_revoke(zone_door); 20010Sstevel@tonic-gate (void) fdetach(zone_door_path); 20020Sstevel@tonic-gate } 20030Sstevel@tonic-gate return (1); /* return from main() forcibly exits an MT process */ 20040Sstevel@tonic-gate } 2005