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> 95*7655Sgerald.jelinek@sun.com #include <sys/brand.h> 960Sstevel@tonic-gate #include <libcontract.h> 970Sstevel@tonic-gate #include <libcontract_priv.h> 980Sstevel@tonic-gate #include <sys/contract/process.h> 990Sstevel@tonic-gate #include <sys/ctfs.h> 1000Sstevel@tonic-gate 1010Sstevel@tonic-gate #include <libzonecfg.h> 1020Sstevel@tonic-gate #include "zoneadmd.h" 1030Sstevel@tonic-gate 1040Sstevel@tonic-gate static char *progname; 1050Sstevel@tonic-gate char *zone_name; /* zone which we are managing */ 1062712Snn35248 char brand_name[MAXNAMELEN]; 1072712Snn35248 boolean_t zone_isnative; 1084350Std153743 boolean_t zone_iscluster; 109766Scarlsonj static zoneid_t zone_id; 1100Sstevel@tonic-gate 1117370S<Gerald Jelinek> static char pre_statechg_hook[2 * MAXPATHLEN]; 1127370S<Gerald Jelinek> static char post_statechg_hook[2 * MAXPATHLEN]; 1137370S<Gerald Jelinek> char query_hook[2 * MAXPATHLEN]; 1147370S<Gerald Jelinek> 1152611Svp157776 zlog_t logsys; 1160Sstevel@tonic-gate 1170Sstevel@tonic-gate mutex_t lock = DEFAULTMUTEX; /* to serialize stuff */ 1180Sstevel@tonic-gate mutex_t msglock = DEFAULTMUTEX; /* for calling setlocale() */ 1190Sstevel@tonic-gate 120766Scarlsonj static sema_t scratch_sem; /* for scratch zones */ 121766Scarlsonj 1220Sstevel@tonic-gate static char zone_door_path[MAXPATHLEN]; 1230Sstevel@tonic-gate static int zone_door = -1; 1240Sstevel@tonic-gate 1250Sstevel@tonic-gate boolean_t in_death_throes = B_FALSE; /* daemon is dying */ 1260Sstevel@tonic-gate boolean_t bringup_failure_recovery = B_FALSE; /* ignore certain failures */ 1270Sstevel@tonic-gate 1280Sstevel@tonic-gate #if !defined(TEXT_DOMAIN) /* should be defined by cc -D */ 1290Sstevel@tonic-gate #define TEXT_DOMAIN "SYS_TEST" /* Use this only if it wasn't */ 1300Sstevel@tonic-gate #endif 1310Sstevel@tonic-gate 1320Sstevel@tonic-gate #define DEFAULT_LOCALE "C" 1330Sstevel@tonic-gate 134766Scarlsonj static const char * 135766Scarlsonj z_cmd_name(zone_cmd_t zcmd) 136766Scarlsonj { 137766Scarlsonj /* This list needs to match the enum in sys/zone.h */ 138766Scarlsonj static const char *zcmdstr[] = { 1392712Snn35248 "ready", "boot", "forceboot", "reboot", "halt", 1402712Snn35248 "note_uninstalling", "mount", "forcemount", "unmount" 141766Scarlsonj }; 142766Scarlsonj 143766Scarlsonj if (zcmd >= sizeof (zcmdstr) / sizeof (*zcmdstr)) 144766Scarlsonj return ("unknown"); 145766Scarlsonj else 146766Scarlsonj return (zcmdstr[(int)zcmd]); 147766Scarlsonj } 148766Scarlsonj 1490Sstevel@tonic-gate static char * 1500Sstevel@tonic-gate get_execbasename(char *execfullname) 1510Sstevel@tonic-gate { 1520Sstevel@tonic-gate char *last_slash, *execbasename; 1530Sstevel@tonic-gate 1540Sstevel@tonic-gate /* guard against '/' at end of command invocation */ 1550Sstevel@tonic-gate for (;;) { 1560Sstevel@tonic-gate last_slash = strrchr(execfullname, '/'); 1570Sstevel@tonic-gate if (last_slash == NULL) { 1580Sstevel@tonic-gate execbasename = execfullname; 1590Sstevel@tonic-gate break; 1600Sstevel@tonic-gate } else { 1610Sstevel@tonic-gate execbasename = last_slash + 1; 1620Sstevel@tonic-gate if (*execbasename == '\0') { 1630Sstevel@tonic-gate *last_slash = '\0'; 1640Sstevel@tonic-gate continue; 1650Sstevel@tonic-gate } 1660Sstevel@tonic-gate break; 1670Sstevel@tonic-gate } 1680Sstevel@tonic-gate } 1690Sstevel@tonic-gate return (execbasename); 1700Sstevel@tonic-gate } 1710Sstevel@tonic-gate 1720Sstevel@tonic-gate static void 1730Sstevel@tonic-gate usage(void) 1740Sstevel@tonic-gate { 1750Sstevel@tonic-gate (void) fprintf(stderr, gettext("Usage: %s -z zonename\n"), progname); 1760Sstevel@tonic-gate (void) fprintf(stderr, 1770Sstevel@tonic-gate gettext("\tNote: %s should not be run directly.\n"), progname); 1780Sstevel@tonic-gate exit(2); 1790Sstevel@tonic-gate } 1800Sstevel@tonic-gate 1810Sstevel@tonic-gate /* ARGSUSED */ 1820Sstevel@tonic-gate static void 1830Sstevel@tonic-gate sigchld(int sig) 1840Sstevel@tonic-gate { 1850Sstevel@tonic-gate } 1860Sstevel@tonic-gate 1870Sstevel@tonic-gate char * 1880Sstevel@tonic-gate localize_msg(char *locale, const char *msg) 1890Sstevel@tonic-gate { 1900Sstevel@tonic-gate char *out; 1910Sstevel@tonic-gate 1920Sstevel@tonic-gate (void) mutex_lock(&msglock); 1930Sstevel@tonic-gate (void) setlocale(LC_MESSAGES, locale); 1940Sstevel@tonic-gate out = gettext(msg); 1950Sstevel@tonic-gate (void) setlocale(LC_MESSAGES, DEFAULT_LOCALE); 1960Sstevel@tonic-gate (void) mutex_unlock(&msglock); 1970Sstevel@tonic-gate return (out); 1980Sstevel@tonic-gate } 1990Sstevel@tonic-gate 2000Sstevel@tonic-gate /* PRINTFLIKE3 */ 2010Sstevel@tonic-gate void 2020Sstevel@tonic-gate zerror(zlog_t *zlogp, boolean_t use_strerror, const char *fmt, ...) 2030Sstevel@tonic-gate { 2040Sstevel@tonic-gate va_list alist; 2050Sstevel@tonic-gate char buf[MAXPATHLEN * 2]; /* enough space for err msg with a path */ 2060Sstevel@tonic-gate char *bp; 2070Sstevel@tonic-gate int saved_errno = errno; 2080Sstevel@tonic-gate 2090Sstevel@tonic-gate if (zlogp == NULL) 2100Sstevel@tonic-gate return; 2110Sstevel@tonic-gate if (zlogp == &logsys) 2120Sstevel@tonic-gate (void) snprintf(buf, sizeof (buf), "[zone '%s'] ", 2130Sstevel@tonic-gate zone_name); 2140Sstevel@tonic-gate else 2150Sstevel@tonic-gate buf[0] = '\0'; 2160Sstevel@tonic-gate bp = &(buf[strlen(buf)]); 2170Sstevel@tonic-gate 2180Sstevel@tonic-gate /* 2190Sstevel@tonic-gate * In theory, the locale pointer should be set to either "C" or a 2200Sstevel@tonic-gate * char array, so it should never be NULL 2210Sstevel@tonic-gate */ 2220Sstevel@tonic-gate assert(zlogp->locale != NULL); 2230Sstevel@tonic-gate /* Locale is per process, but we are multi-threaded... */ 2240Sstevel@tonic-gate fmt = localize_msg(zlogp->locale, fmt); 2250Sstevel@tonic-gate 2260Sstevel@tonic-gate va_start(alist, fmt); 2270Sstevel@tonic-gate (void) vsnprintf(bp, sizeof (buf) - (bp - buf), fmt, alist); 2280Sstevel@tonic-gate va_end(alist); 2290Sstevel@tonic-gate bp = &(buf[strlen(buf)]); 2300Sstevel@tonic-gate if (use_strerror) 2310Sstevel@tonic-gate (void) snprintf(bp, sizeof (buf) - (bp - buf), ": %s", 2320Sstevel@tonic-gate strerror(saved_errno)); 2330Sstevel@tonic-gate if (zlogp == &logsys) { 2340Sstevel@tonic-gate (void) syslog(LOG_ERR, "%s", buf); 2350Sstevel@tonic-gate } else if (zlogp->logfile != NULL) { 2360Sstevel@tonic-gate (void) fprintf(zlogp->logfile, "%s\n", buf); 2370Sstevel@tonic-gate } else { 2380Sstevel@tonic-gate size_t buflen; 2390Sstevel@tonic-gate size_t copylen; 2400Sstevel@tonic-gate 2410Sstevel@tonic-gate buflen = snprintf(zlogp->log, zlogp->loglen, "%s\n", buf); 2420Sstevel@tonic-gate copylen = MIN(buflen, zlogp->loglen); 2430Sstevel@tonic-gate zlogp->log += copylen; 2440Sstevel@tonic-gate zlogp->loglen -= copylen; 2450Sstevel@tonic-gate } 2460Sstevel@tonic-gate } 2470Sstevel@tonic-gate 2482267Sdp /* 2492267Sdp * Emit a warning for any boot arguments which are unrecognized. Since 2502267Sdp * Solaris boot arguments are getopt(3c) compatible (see kernel(1m)), we 2512267Sdp * put the arguments into an argv style array, use getopt to process them, 2522267Sdp * and put the resultant argument string back into outargs. 2532267Sdp * 2542267Sdp * During the filtering, we pull out any arguments which are truly "boot" 2552267Sdp * arguments, leaving only those which are to be passed intact to the 2562267Sdp * progenitor process. The one we support at the moment is -i, which 2572267Sdp * indicates to the kernel which program should be launched as 'init'. 2582267Sdp * 2592267Sdp * A return of Z_INVAL indicates specifically that the arguments are 2602267Sdp * not valid; this is a non-fatal error. Except for Z_OK, all other return 2612267Sdp * values are treated as fatal. 2622267Sdp */ 2632267Sdp static int 2642267Sdp filter_bootargs(zlog_t *zlogp, const char *inargs, char *outargs, 2652267Sdp char *init_file, char *badarg) 2662267Sdp { 2672267Sdp int argc = 0, argc_save; 2682267Sdp int i; 2692267Sdp int err; 2702267Sdp char *arg, *lasts, **argv = NULL, **argv_save; 2712267Sdp char zonecfg_args[BOOTARGS_MAX]; 2722267Sdp char scratchargs[BOOTARGS_MAX], *sargs; 2732267Sdp char c; 2742267Sdp 2752267Sdp bzero(outargs, BOOTARGS_MAX); 2762267Sdp bzero(badarg, BOOTARGS_MAX); 2772267Sdp 2782267Sdp /* 2792267Sdp * If the user didn't specify transient boot arguments, check 2802267Sdp * to see if there were any specified in the zone configuration, 2812267Sdp * and use them if applicable. 2822267Sdp */ 2832267Sdp if (inargs == NULL || inargs[0] == '\0') { 2842267Sdp zone_dochandle_t handle; 2852267Sdp if ((handle = zonecfg_init_handle()) == NULL) { 2862267Sdp zerror(zlogp, B_TRUE, 2872267Sdp "getting zone configuration handle"); 2882267Sdp return (Z_BAD_HANDLE); 2892267Sdp } 2902267Sdp err = zonecfg_get_snapshot_handle(zone_name, handle); 2912267Sdp if (err != Z_OK) { 2922267Sdp zerror(zlogp, B_FALSE, 2932267Sdp "invalid configuration snapshot"); 2942267Sdp zonecfg_fini_handle(handle); 2952267Sdp return (Z_BAD_HANDLE); 2962267Sdp } 2972267Sdp 2982267Sdp bzero(zonecfg_args, sizeof (zonecfg_args)); 2992267Sdp (void) zonecfg_get_bootargs(handle, zonecfg_args, 3002267Sdp sizeof (zonecfg_args)); 3012267Sdp inargs = zonecfg_args; 3022267Sdp zonecfg_fini_handle(handle); 3032267Sdp } 3042267Sdp 3052267Sdp if (strlen(inargs) >= BOOTARGS_MAX) { 3062267Sdp zerror(zlogp, B_FALSE, "boot argument string too long"); 3072267Sdp return (Z_INVAL); 3082267Sdp } 3092267Sdp 3102267Sdp (void) strlcpy(scratchargs, inargs, sizeof (scratchargs)); 3112267Sdp sargs = scratchargs; 3122267Sdp while ((arg = strtok_r(sargs, " \t", &lasts)) != NULL) { 3132267Sdp sargs = NULL; 3142267Sdp argc++; 3152267Sdp } 3162267Sdp 3172267Sdp if ((argv = calloc(argc + 1, sizeof (char *))) == NULL) { 3182267Sdp zerror(zlogp, B_FALSE, "memory allocation failed"); 3192267Sdp return (Z_NOMEM); 3202267Sdp } 3212267Sdp 3222267Sdp argv_save = argv; 3232267Sdp argc_save = argc; 3242267Sdp 3252267Sdp (void) strlcpy(scratchargs, inargs, sizeof (scratchargs)); 3262267Sdp sargs = scratchargs; 3272267Sdp i = 0; 3282267Sdp while ((arg = strtok_r(sargs, " \t", &lasts)) != NULL) { 3292267Sdp sargs = NULL; 3302267Sdp if ((argv[i] = strdup(arg)) == NULL) { 3312267Sdp err = Z_NOMEM; 3322267Sdp zerror(zlogp, B_FALSE, "memory allocation failed"); 3332267Sdp goto done; 3342267Sdp } 3352267Sdp i++; 3362267Sdp } 3372267Sdp 3382267Sdp /* 3392267Sdp * We preserve compatibility with the Solaris system boot behavior, 3402267Sdp * which allows: 3412267Sdp * 3422267Sdp * # reboot kernel/unix -s -m verbose 3432267Sdp * 3442267Sdp * In this example, kernel/unix tells the booter what file to 3452267Sdp * boot. We don't want reboot in a zone to be gratuitously different, 3462267Sdp * so we silently ignore the boot file, if necessary. 3472267Sdp */ 3482267Sdp if (argv[0] == NULL) 3492267Sdp goto done; 3502267Sdp 3512267Sdp assert(argv[0][0] != ' '); 3522267Sdp assert(argv[0][0] != '\t'); 3532267Sdp 3542267Sdp if (argv[0][0] != '-' && argv[0][0] != '\0') { 3552267Sdp argv = &argv[1]; 3562267Sdp argc--; 3572267Sdp } 3582267Sdp 3592267Sdp optind = 0; 3602267Sdp opterr = 0; 3612267Sdp err = Z_OK; 3622712Snn35248 while ((c = getopt(argc, argv, "fi:m:s")) != -1) { 3632267Sdp switch (c) { 3642267Sdp case 'i': 3652267Sdp /* 3662267Sdp * -i is handled by the runtime and is not passed 3672267Sdp * along to userland 3682267Sdp */ 3692267Sdp (void) strlcpy(init_file, optarg, MAXPATHLEN); 3702267Sdp break; 3712712Snn35248 case 'f': 3722712Snn35248 /* This has already been processed by zoneadm */ 3732712Snn35248 break; 3742267Sdp case 'm': 3752267Sdp case 's': 3762267Sdp /* These pass through unmolested */ 3772267Sdp (void) snprintf(outargs, BOOTARGS_MAX, 3782267Sdp "%s -%c %s ", outargs, c, optarg ? optarg : ""); 3792267Sdp break; 3802267Sdp case '?': 3812267Sdp /* 3822267Sdp * We warn about unknown arguments but pass them 3832267Sdp * along anyway-- if someone wants to develop their 3842267Sdp * own init replacement, they can pass it whatever 3852267Sdp * args they want. 3862267Sdp */ 3872267Sdp err = Z_INVAL; 3882267Sdp (void) snprintf(outargs, BOOTARGS_MAX, 3892267Sdp "%s -%c", outargs, optopt); 3902267Sdp (void) snprintf(badarg, BOOTARGS_MAX, 3912267Sdp "%s -%c", badarg, optopt); 3922267Sdp break; 3932267Sdp } 3942267Sdp } 3952267Sdp 3962267Sdp /* 3972267Sdp * For Solaris Zones we warn about and discard non-option arguments. 3982267Sdp * Hence 'boot foo bar baz gub' --> 'boot'. However, to be similar 3992267Sdp * to the kernel, we concat up all the other remaining boot args. 4002267Sdp * and warn on them as a group. 4012267Sdp */ 4022267Sdp if (optind < argc) { 4032267Sdp err = Z_INVAL; 4042267Sdp while (optind < argc) { 4052267Sdp (void) snprintf(badarg, BOOTARGS_MAX, "%s%s%s", 4062267Sdp badarg, strlen(badarg) > 0 ? " " : "", 4072267Sdp argv[optind]); 4082267Sdp optind++; 4092267Sdp } 4102267Sdp zerror(zlogp, B_FALSE, "WARNING: Unused or invalid boot " 4112267Sdp "arguments `%s'.", badarg); 4122267Sdp } 4132267Sdp 4142267Sdp done: 4152267Sdp for (i = 0; i < argc_save; i++) { 4162267Sdp if (argv_save[i] != NULL) 4172267Sdp free(argv_save[i]); 4182267Sdp } 4192267Sdp free(argv_save); 4202267Sdp return (err); 4212267Sdp } 4222267Sdp 4232267Sdp 4240Sstevel@tonic-gate static int 4250Sstevel@tonic-gate mkzonedir(zlog_t *zlogp) 4260Sstevel@tonic-gate { 4270Sstevel@tonic-gate struct stat st; 4280Sstevel@tonic-gate /* 4290Sstevel@tonic-gate * We must create and lock everyone but root out of ZONES_TMPDIR 4300Sstevel@tonic-gate * since anyone can open any UNIX domain socket, regardless of 4310Sstevel@tonic-gate * its file system permissions. Sigh... 4320Sstevel@tonic-gate */ 4330Sstevel@tonic-gate if (mkdir(ZONES_TMPDIR, S_IRWXU) < 0 && errno != EEXIST) { 4340Sstevel@tonic-gate zerror(zlogp, B_TRUE, "could not mkdir '%s'", ZONES_TMPDIR); 4350Sstevel@tonic-gate return (-1); 4360Sstevel@tonic-gate } 4370Sstevel@tonic-gate /* paranoia */ 438871Scasper if ((stat(ZONES_TMPDIR, &st) < 0) || !S_ISDIR(st.st_mode)) { 4390Sstevel@tonic-gate zerror(zlogp, B_TRUE, "'%s' is not a directory", ZONES_TMPDIR); 4400Sstevel@tonic-gate return (-1); 4410Sstevel@tonic-gate } 4420Sstevel@tonic-gate (void) chmod(ZONES_TMPDIR, S_IRWXU); 4430Sstevel@tonic-gate return (0); 4440Sstevel@tonic-gate } 4450Sstevel@tonic-gate 446766Scarlsonj /* 4477370S<Gerald Jelinek> * Run the brand's pre-state change callback, if it exists. 4487370S<Gerald Jelinek> */ 4497370S<Gerald Jelinek> static int 4507370S<Gerald Jelinek> brand_prestatechg(zlog_t *zlogp, int state, int cmd) 4517370S<Gerald Jelinek> { 4527370S<Gerald Jelinek> char cmdbuf[2 * MAXPATHLEN]; 4537370S<Gerald Jelinek> 4547370S<Gerald Jelinek> if (pre_statechg_hook[0] == '\0') 4557370S<Gerald Jelinek> return (0); 4567370S<Gerald Jelinek> 4577370S<Gerald Jelinek> if (snprintf(cmdbuf, sizeof (cmdbuf), "%s %d %d", pre_statechg_hook, 4587370S<Gerald Jelinek> state, cmd) > sizeof (cmdbuf)) 4597370S<Gerald Jelinek> return (-1); 4607370S<Gerald Jelinek> 4617370S<Gerald Jelinek> if (do_subproc(zlogp, cmdbuf, NULL) != 0) 4627370S<Gerald Jelinek> return (-1); 4637370S<Gerald Jelinek> 4647370S<Gerald Jelinek> return (0); 4657370S<Gerald Jelinek> } 4667370S<Gerald Jelinek> 4677370S<Gerald Jelinek> /* 4687370S<Gerald Jelinek> * Run the brand's post-state change callback, if it exists. 4697370S<Gerald Jelinek> */ 4707370S<Gerald Jelinek> static int 4717370S<Gerald Jelinek> brand_poststatechg(zlog_t *zlogp, int state, int cmd) 4727370S<Gerald Jelinek> { 4737370S<Gerald Jelinek> char cmdbuf[2 * MAXPATHLEN]; 4747370S<Gerald Jelinek> 4757370S<Gerald Jelinek> if (post_statechg_hook[0] == '\0') 4767370S<Gerald Jelinek> return (0); 4777370S<Gerald Jelinek> 4787370S<Gerald Jelinek> if (snprintf(cmdbuf, sizeof (cmdbuf), "%s %d %d", post_statechg_hook, 4797370S<Gerald Jelinek> state, cmd) > sizeof (cmdbuf)) 4807370S<Gerald Jelinek> return (-1); 4817370S<Gerald Jelinek> 4827370S<Gerald Jelinek> if (do_subproc(zlogp, cmdbuf, NULL) != 0) 4837370S<Gerald Jelinek> return (-1); 4847370S<Gerald Jelinek> 4857370S<Gerald Jelinek> return (0); 4867370S<Gerald Jelinek> } 4877370S<Gerald Jelinek> 4887370S<Gerald Jelinek> /* 489766Scarlsonj * Bring a zone up to the pre-boot "ready" stage. The mount_cmd argument is 490766Scarlsonj * 'true' if this is being invoked as part of the processing for the "mount" 491766Scarlsonj * subcommand. 492766Scarlsonj */ 493766Scarlsonj static int 4947370S<Gerald Jelinek> zone_ready(zlog_t *zlogp, zone_mnt_t mount_cmd, int zstate) 4950Sstevel@tonic-gate { 4960Sstevel@tonic-gate int err; 4970Sstevel@tonic-gate 4987370S<Gerald Jelinek> if (brand_prestatechg(zlogp, zstate, Z_READY) != 0) 4997370S<Gerald Jelinek> return (-1); 5007370S<Gerald Jelinek> 5010Sstevel@tonic-gate if ((err = zonecfg_create_snapshot(zone_name)) != Z_OK) { 5020Sstevel@tonic-gate zerror(zlogp, B_FALSE, "unable to create snapshot: %s", 5030Sstevel@tonic-gate zonecfg_strerror(err)); 5040Sstevel@tonic-gate return (-1); 5050Sstevel@tonic-gate } 5060Sstevel@tonic-gate 507766Scarlsonj if ((zone_id = vplat_create(zlogp, mount_cmd)) == -1) { 5080Sstevel@tonic-gate if ((err = zonecfg_destroy_snapshot(zone_name)) != Z_OK) 5090Sstevel@tonic-gate zerror(zlogp, B_FALSE, "destroying snapshot: %s", 5100Sstevel@tonic-gate zonecfg_strerror(err)); 5110Sstevel@tonic-gate return (-1); 5120Sstevel@tonic-gate } 5132303Scarlsonj if (vplat_bringup(zlogp, mount_cmd, zone_id) != 0) { 5140Sstevel@tonic-gate bringup_failure_recovery = B_TRUE; 5155829Sgjelinek (void) vplat_teardown(NULL, (mount_cmd != Z_MNT_BOOT), B_FALSE); 5160Sstevel@tonic-gate if ((err = zonecfg_destroy_snapshot(zone_name)) != Z_OK) 5170Sstevel@tonic-gate zerror(zlogp, B_FALSE, "destroying snapshot: %s", 5180Sstevel@tonic-gate zonecfg_strerror(err)); 5190Sstevel@tonic-gate return (-1); 5200Sstevel@tonic-gate } 5210Sstevel@tonic-gate 5227370S<Gerald Jelinek> if (brand_poststatechg(zlogp, zstate, Z_READY) != 0) 5237370S<Gerald Jelinek> return (-1); 5247370S<Gerald Jelinek> 5250Sstevel@tonic-gate return (0); 5260Sstevel@tonic-gate } 5270Sstevel@tonic-gate 5282303Scarlsonj int 5292303Scarlsonj init_template(void) 5300Sstevel@tonic-gate { 5310Sstevel@tonic-gate int fd; 5320Sstevel@tonic-gate int err = 0; 5330Sstevel@tonic-gate 5340Sstevel@tonic-gate fd = open64(CTFS_ROOT "/process/template", O_RDWR); 5350Sstevel@tonic-gate if (fd == -1) 5360Sstevel@tonic-gate return (-1); 5370Sstevel@tonic-gate 5380Sstevel@tonic-gate /* 5390Sstevel@tonic-gate * For now, zoneadmd doesn't do anything with the contract. 5400Sstevel@tonic-gate * Deliver no events, don't inherit, and allow it to be orphaned. 5410Sstevel@tonic-gate */ 5420Sstevel@tonic-gate err |= ct_tmpl_set_critical(fd, 0); 5430Sstevel@tonic-gate err |= ct_tmpl_set_informative(fd, 0); 5440Sstevel@tonic-gate err |= ct_pr_tmpl_set_fatal(fd, CT_PR_EV_HWERR); 5450Sstevel@tonic-gate err |= ct_pr_tmpl_set_param(fd, CT_PR_PGRPONLY | CT_PR_REGENT); 5460Sstevel@tonic-gate if (err || ct_tmpl_activate(fd)) { 5470Sstevel@tonic-gate (void) close(fd); 5480Sstevel@tonic-gate return (-1); 5490Sstevel@tonic-gate } 5500Sstevel@tonic-gate 5510Sstevel@tonic-gate return (fd); 5520Sstevel@tonic-gate } 5530Sstevel@tonic-gate 5542712Snn35248 typedef struct fs_callback { 5552712Snn35248 zlog_t *zlogp; 5562712Snn35248 zoneid_t zoneid; 5575576Sedp boolean_t mount_cmd; 5582712Snn35248 } fs_callback_t; 5592712Snn35248 5600Sstevel@tonic-gate static int 5612712Snn35248 mount_early_fs(void *data, const char *spec, const char *dir, 5622712Snn35248 const char *fstype, const char *opt) 5630Sstevel@tonic-gate { 5642712Snn35248 zlog_t *zlogp = ((fs_callback_t *)data)->zlogp; 5652712Snn35248 zoneid_t zoneid = ((fs_callback_t *)data)->zoneid; 5665576Sedp boolean_t mount_cmd = ((fs_callback_t *)data)->mount_cmd; 5675182Sedp char rootpath[MAXPATHLEN]; 5680Sstevel@tonic-gate pid_t child; 5690Sstevel@tonic-gate int child_status; 5700Sstevel@tonic-gate int tmpl_fd; 5715182Sedp int rv; 5720Sstevel@tonic-gate ctid_t ct; 5730Sstevel@tonic-gate 5745576Sedp /* determine the zone rootpath */ 5755576Sedp if (mount_cmd) { 5765576Sedp char zonepath[MAXPATHLEN]; 5775576Sedp char luroot[MAXPATHLEN]; 5785576Sedp 5795576Sedp if (zone_get_zonepath(zone_name, 5805576Sedp zonepath, sizeof (zonepath)) != Z_OK) { 5815576Sedp zerror(zlogp, B_FALSE, "unable to determine zone path"); 5825576Sedp return (-1); 5835576Sedp } 5845576Sedp 5855576Sedp (void) snprintf(luroot, sizeof (luroot), "%s/lu", zonepath); 5865576Sedp resolve_lofs(zlogp, luroot, sizeof (luroot)); 5875576Sedp (void) strlcpy(rootpath, luroot, sizeof (rootpath)); 5885576Sedp } else { 5895576Sedp if (zone_get_rootpath(zone_name, 5905576Sedp rootpath, sizeof (rootpath)) != Z_OK) { 5915576Sedp zerror(zlogp, B_FALSE, "unable to determine zone root"); 5925576Sedp return (-1); 5935576Sedp } 5945182Sedp } 5955182Sedp 5965182Sedp if ((rv = valid_mount_path(zlogp, rootpath, spec, dir, fstype)) < 0) { 5975182Sedp zerror(zlogp, B_FALSE, "%s%s is not a valid mount point", 5985182Sedp rootpath, dir); 5995182Sedp return (-1); 6005182Sedp } else if (rv > 0) { 6015182Sedp /* The mount point path doesn't exist, create it now. */ 6025182Sedp if (make_one_dir(zlogp, rootpath, dir, 6035182Sedp DEFAULT_DIR_MODE, DEFAULT_DIR_USER, 6045182Sedp DEFAULT_DIR_GROUP) != 0) { 6055182Sedp zerror(zlogp, B_FALSE, "failed to create mount point"); 6065182Sedp return (-1); 6075182Sedp } 6085182Sedp 6095182Sedp /* 6105182Sedp * Now this might seem weird, but we need to invoke 6115182Sedp * valid_mount_path() again. Why? Because it checks 6125182Sedp * to make sure that the mount point path is canonical, 6135182Sedp * which it can only do if the path exists, so now that 6145182Sedp * we've created the path we have to verify it again. 6155182Sedp */ 6165182Sedp if ((rv = valid_mount_path(zlogp, rootpath, spec, dir, 6175182Sedp fstype)) < 0) { 6185182Sedp zerror(zlogp, B_FALSE, 6195182Sedp "%s%s is not a valid mount point", rootpath, dir); 6205182Sedp return (-1); 6215182Sedp } 6225182Sedp } 6235182Sedp 6240Sstevel@tonic-gate if ((tmpl_fd = init_template()) == -1) { 6250Sstevel@tonic-gate zerror(zlogp, B_TRUE, "failed to create contract"); 6260Sstevel@tonic-gate return (-1); 6270Sstevel@tonic-gate } 6280Sstevel@tonic-gate 6290Sstevel@tonic-gate if ((child = fork()) == -1) { 6300Sstevel@tonic-gate (void) ct_tmpl_clear(tmpl_fd); 6310Sstevel@tonic-gate (void) close(tmpl_fd); 6320Sstevel@tonic-gate zerror(zlogp, B_TRUE, "failed to fork"); 6330Sstevel@tonic-gate return (-1); 6340Sstevel@tonic-gate 6350Sstevel@tonic-gate } else if (child == 0) { /* child */ 6362712Snn35248 char opt_buf[MAX_MNTOPT_STR]; 6372712Snn35248 int optlen = 0; 6382712Snn35248 int mflag = MS_DATA; 6392712Snn35248 6400Sstevel@tonic-gate (void) ct_tmpl_clear(tmpl_fd); 6410Sstevel@tonic-gate /* 6420Sstevel@tonic-gate * Even though there are no procs running in the zone, we 6430Sstevel@tonic-gate * do this for paranoia's sake. 6440Sstevel@tonic-gate */ 6450Sstevel@tonic-gate (void) closefrom(0); 6460Sstevel@tonic-gate 6470Sstevel@tonic-gate if (zone_enter(zoneid) == -1) { 6480Sstevel@tonic-gate _exit(errno); 6490Sstevel@tonic-gate } 6502712Snn35248 if (opt != NULL) { 6512712Snn35248 /* 6522712Snn35248 * The mount() system call is incredibly annoying. 6532712Snn35248 * If options are specified, we need to copy them 6542712Snn35248 * into a temporary buffer since the mount() system 6552712Snn35248 * call will overwrite the options string. It will 6562712Snn35248 * also fail if the new option string it wants to 6572712Snn35248 * write is bigger than the one we passed in, so 6582712Snn35248 * you must pass in a buffer of the maximum possible 6592712Snn35248 * option string length. sigh. 6602712Snn35248 */ 6612712Snn35248 (void) strlcpy(opt_buf, opt, sizeof (opt_buf)); 6622712Snn35248 opt = opt_buf; 6632712Snn35248 optlen = MAX_MNTOPT_STR; 6642712Snn35248 mflag = MS_OPTIONSTR; 6652712Snn35248 } 6662712Snn35248 if (mount(spec, dir, mflag, fstype, NULL, 0, opt, optlen) != 0) 6670Sstevel@tonic-gate _exit(errno); 6680Sstevel@tonic-gate _exit(0); 6690Sstevel@tonic-gate } 6700Sstevel@tonic-gate 6710Sstevel@tonic-gate /* parent */ 6720Sstevel@tonic-gate if (contract_latest(&ct) == -1) 6730Sstevel@tonic-gate ct = -1; 6740Sstevel@tonic-gate (void) ct_tmpl_clear(tmpl_fd); 6750Sstevel@tonic-gate (void) close(tmpl_fd); 6760Sstevel@tonic-gate if (waitpid(child, &child_status, 0) != child) { 6770Sstevel@tonic-gate /* unexpected: we must have been signalled */ 6780Sstevel@tonic-gate (void) contract_abandon_id(ct); 6790Sstevel@tonic-gate return (-1); 6800Sstevel@tonic-gate } 6810Sstevel@tonic-gate (void) contract_abandon_id(ct); 6820Sstevel@tonic-gate if (WEXITSTATUS(child_status) != 0) { 6830Sstevel@tonic-gate errno = WEXITSTATUS(child_status); 6840Sstevel@tonic-gate zerror(zlogp, B_TRUE, "mount of %s failed", dir); 6850Sstevel@tonic-gate return (-1); 6860Sstevel@tonic-gate } 6870Sstevel@tonic-gate 6880Sstevel@tonic-gate return (0); 6890Sstevel@tonic-gate } 6900Sstevel@tonic-gate 6917370S<Gerald Jelinek> /* 6927370S<Gerald Jelinek> * If retstr is not NULL, the output of the subproc is returned in the str, 6937370S<Gerald Jelinek> * otherwise it is output using zerror(). Any memory allocated for retstr 6947370S<Gerald Jelinek> * should be freed by the caller. 6957370S<Gerald Jelinek> */ 6962712Snn35248 int 6977370S<Gerald Jelinek> do_subproc(zlog_t *zlogp, char *cmdbuf, char **retstr) 698766Scarlsonj { 6997370S<Gerald Jelinek> char buf[1024]; /* arbitrary large amount */ 7007370S<Gerald Jelinek> char *inbuf; 7012712Snn35248 FILE *file; 7022712Snn35248 int status; 7037370S<Gerald Jelinek> int rd_cnt; 7047370S<Gerald Jelinek> 7057370S<Gerald Jelinek> if (retstr != NULL) { 7067370S<Gerald Jelinek> if ((*retstr = malloc(1024)) == NULL) { 7077370S<Gerald Jelinek> zerror(zlogp, B_FALSE, "out of memory"); 7087370S<Gerald Jelinek> return (-1); 7097370S<Gerald Jelinek> } 7107370S<Gerald Jelinek> inbuf = *retstr; 7117370S<Gerald Jelinek> rd_cnt = 0; 7127370S<Gerald Jelinek> } else { 7137370S<Gerald Jelinek> inbuf = buf; 7147370S<Gerald Jelinek> } 715766Scarlsonj 7162712Snn35248 file = popen(cmdbuf, "r"); 7172712Snn35248 if (file == NULL) { 7182712Snn35248 zerror(zlogp, B_TRUE, "could not launch: %s", cmdbuf); 7192525Sdp return (-1); 7202712Snn35248 } 7212525Sdp 7227370S<Gerald Jelinek> while (fgets(inbuf, 1024, file) != NULL) { 7237370S<Gerald Jelinek> if (retstr == NULL && zlogp != &logsys) { 7242712Snn35248 zerror(zlogp, B_FALSE, "%s", inbuf); 7257370S<Gerald Jelinek> } else { 7267370S<Gerald Jelinek> char *p; 7277370S<Gerald Jelinek> 7287370S<Gerald Jelinek> rd_cnt += 1024 - 1; 7297370S<Gerald Jelinek> if ((p = realloc(*retstr, rd_cnt + 1024)) == NULL) { 7307370S<Gerald Jelinek> zerror(zlogp, B_FALSE, "out of memory"); 7317370S<Gerald Jelinek> (void) pclose(file); 7327370S<Gerald Jelinek> return (-1); 7337370S<Gerald Jelinek> } 7347370S<Gerald Jelinek> 7357370S<Gerald Jelinek> *retstr = p; 7367370S<Gerald Jelinek> inbuf = *retstr + rd_cnt; 7377370S<Gerald Jelinek> } 7387370S<Gerald Jelinek> } 7392712Snn35248 status = pclose(file); 740766Scarlsonj 7412712Snn35248 if (WIFSIGNALED(status)) { 7422712Snn35248 zerror(zlogp, B_FALSE, "%s unexpectedly terminated due to " 7432712Snn35248 "signal %d", cmdbuf, WTERMSIG(status)); 744766Scarlsonj return (-1); 7452712Snn35248 } 7462712Snn35248 assert(WIFEXITED(status)); 7472712Snn35248 if (WEXITSTATUS(status) == ZEXIT_EXEC) { 7482712Snn35248 zerror(zlogp, B_FALSE, "failed to exec %s", cmdbuf); 7492712Snn35248 return (-1); 7502712Snn35248 } 7512712Snn35248 return (WEXITSTATUS(status)); 752766Scarlsonj } 753766Scarlsonj 754766Scarlsonj static int 7557370S<Gerald Jelinek> zone_bootup(zlog_t *zlogp, const char *bootargs, int zstate) 7560Sstevel@tonic-gate { 7570Sstevel@tonic-gate zoneid_t zoneid; 7580Sstevel@tonic-gate struct stat st; 7597089Sgjelinek char zpath[MAXPATHLEN], initpath[MAXPATHLEN], init_file[MAXPATHLEN]; 7602267Sdp char nbootargs[BOOTARGS_MAX]; 7612712Snn35248 char cmdbuf[MAXPATHLEN]; 7622712Snn35248 fs_callback_t cb; 7632727Sedp brand_handle_t bh; 7642267Sdp int err; 7650Sstevel@tonic-gate 7667370S<Gerald Jelinek> if (brand_prestatechg(zlogp, zstate, Z_BOOT) != 0) 7677370S<Gerald Jelinek> return (-1); 7687370S<Gerald Jelinek> 7690Sstevel@tonic-gate if (init_console_slave(zlogp) != 0) 7700Sstevel@tonic-gate return (-1); 7710Sstevel@tonic-gate reset_slave_terminal(zlogp); 7720Sstevel@tonic-gate 7730Sstevel@tonic-gate if ((zoneid = getzoneidbyname(zone_name)) == -1) { 7740Sstevel@tonic-gate zerror(zlogp, B_TRUE, "unable to get zoneid"); 7750Sstevel@tonic-gate return (-1); 7760Sstevel@tonic-gate } 7770Sstevel@tonic-gate 7782712Snn35248 cb.zlogp = zlogp; 7792712Snn35248 cb.zoneid = zoneid; 7805576Sedp cb.mount_cmd = B_FALSE; 7812712Snn35248 7822712Snn35248 /* Get a handle to the brand info for this zone */ 7832727Sedp if ((bh = brand_open(brand_name)) == NULL) { 7842712Snn35248 zerror(zlogp, B_FALSE, "unable to determine zone brand"); 7852712Snn35248 return (-1); 7862712Snn35248 } 7872712Snn35248 7882712Snn35248 /* 7892712Snn35248 * Get the list of filesystems to mount from the brand 7902712Snn35248 * configuration. These mounts are done via a thread that will 7912712Snn35248 * enter the zone, so they are done from within the context of the 7922712Snn35248 * zone. 7932712Snn35248 */ 7942727Sedp if (brand_platform_iter_mounts(bh, mount_early_fs, &cb) != 0) { 7952712Snn35248 zerror(zlogp, B_FALSE, "unable to mount filesystems"); 7962727Sedp brand_close(bh); 7970Sstevel@tonic-gate return (-1); 7982712Snn35248 } 7992712Snn35248 8002712Snn35248 /* 8012712Snn35248 * Get the brand's boot callback if it exists. 8022712Snn35248 */ 8037089Sgjelinek if (zone_get_zonepath(zone_name, zpath, sizeof (zpath)) != Z_OK) { 8047089Sgjelinek zerror(zlogp, B_FALSE, "unable to determine zone path"); 8053716Sgjelinek brand_close(bh); 8062712Snn35248 return (-1); 8072712Snn35248 } 8082712Snn35248 (void) strcpy(cmdbuf, EXEC_PREFIX); 8097089Sgjelinek if (brand_get_boot(bh, zone_name, zpath, cmdbuf + EXEC_LEN, 8107089Sgjelinek sizeof (cmdbuf) - EXEC_LEN) != 0) { 8112712Snn35248 zerror(zlogp, B_FALSE, 8122712Snn35248 "unable to determine branded zone's boot callback"); 8132727Sedp brand_close(bh); 8142712Snn35248 return (-1); 8152712Snn35248 } 8162712Snn35248 8172712Snn35248 /* Get the path for this zone's init(1M) (or equivalent) process. */ 8182727Sedp if (brand_get_initname(bh, init_file, MAXPATHLEN) != 0) { 8192712Snn35248 zerror(zlogp, B_FALSE, 8202712Snn35248 "unable to determine zone's init(1M) location"); 8212727Sedp brand_close(bh); 8222712Snn35248 return (-1); 8232712Snn35248 } 8242712Snn35248 8252727Sedp brand_close(bh); 8260Sstevel@tonic-gate 8272267Sdp err = filter_bootargs(zlogp, bootargs, nbootargs, init_file, 8282267Sdp bad_boot_arg); 8292267Sdp if (err == Z_INVAL) 8302267Sdp eventstream_write(Z_EVT_ZONE_BADARGS); 8312267Sdp else if (err != Z_OK) 8322267Sdp return (-1); 8332267Sdp 8342267Sdp assert(init_file[0] != '\0'); 8352267Sdp 8362712Snn35248 /* Try to anticipate possible problems: Make sure init is executable. */ 8377089Sgjelinek if (zone_get_rootpath(zone_name, zpath, sizeof (zpath)) != Z_OK) { 8380Sstevel@tonic-gate zerror(zlogp, B_FALSE, "unable to determine zone root"); 8390Sstevel@tonic-gate return (-1); 8400Sstevel@tonic-gate } 8412712Snn35248 8427089Sgjelinek (void) snprintf(initpath, sizeof (initpath), "%s%s", zpath, init_file); 8430Sstevel@tonic-gate 8440Sstevel@tonic-gate if (stat(initpath, &st) == -1) { 8450Sstevel@tonic-gate zerror(zlogp, B_TRUE, "could not stat %s", initpath); 8460Sstevel@tonic-gate return (-1); 8470Sstevel@tonic-gate } 8480Sstevel@tonic-gate 8490Sstevel@tonic-gate if ((st.st_mode & S_IXUSR) == 0) { 8500Sstevel@tonic-gate zerror(zlogp, B_FALSE, "%s is not executable", initpath); 8510Sstevel@tonic-gate return (-1); 8520Sstevel@tonic-gate } 8530Sstevel@tonic-gate 8542712Snn35248 /* 8552712Snn35248 * If there is a brand 'boot' callback, execute it now to give the 8562712Snn35248 * brand one last chance to do any additional setup before the zone 8572712Snn35248 * is booted. 8582712Snn35248 */ 8592712Snn35248 if ((strlen(cmdbuf) > EXEC_LEN) && 8607370S<Gerald Jelinek> (do_subproc(zlogp, cmdbuf, NULL) != Z_OK)) { 8612712Snn35248 zerror(zlogp, B_FALSE, "%s failed", cmdbuf); 8622712Snn35248 return (-1); 8632712Snn35248 } 8642712Snn35248 8652267Sdp if (zone_setattr(zoneid, ZONE_ATTR_INITNAME, init_file, 0) == -1) { 8662267Sdp zerror(zlogp, B_TRUE, "could not set zone boot file"); 8672267Sdp return (-1); 8682267Sdp } 8692267Sdp 8702267Sdp if (zone_setattr(zoneid, ZONE_ATTR_BOOTARGS, nbootargs, 0) == -1) { 8712267Sdp zerror(zlogp, B_TRUE, "could not set zone boot arguments"); 8722267Sdp return (-1); 8732267Sdp } 8742267Sdp 8752267Sdp if (zone_boot(zoneid) == -1) { 8760Sstevel@tonic-gate zerror(zlogp, B_TRUE, "unable to boot zone"); 8770Sstevel@tonic-gate return (-1); 8780Sstevel@tonic-gate } 8790Sstevel@tonic-gate 8807370S<Gerald Jelinek> if (brand_poststatechg(zlogp, zstate, Z_BOOT) != 0) 8817370S<Gerald Jelinek> return (-1); 8827370S<Gerald Jelinek> 8830Sstevel@tonic-gate return (0); 8840Sstevel@tonic-gate } 8850Sstevel@tonic-gate 8860Sstevel@tonic-gate static int 8877370S<Gerald Jelinek> zone_halt(zlog_t *zlogp, boolean_t unmount_cmd, boolean_t rebooting, int zstate) 8880Sstevel@tonic-gate { 8890Sstevel@tonic-gate int err; 8900Sstevel@tonic-gate 8917370S<Gerald Jelinek> if (brand_prestatechg(zlogp, zstate, Z_HALT) != 0) 8927370S<Gerald Jelinek> return (-1); 8937370S<Gerald Jelinek> 8943247Sgjelinek if (vplat_teardown(zlogp, unmount_cmd, rebooting) != 0) { 8950Sstevel@tonic-gate if (!bringup_failure_recovery) 8960Sstevel@tonic-gate zerror(zlogp, B_FALSE, "unable to destroy zone"); 8970Sstevel@tonic-gate return (-1); 8980Sstevel@tonic-gate } 8990Sstevel@tonic-gate 9000Sstevel@tonic-gate if ((err = zonecfg_destroy_snapshot(zone_name)) != Z_OK) 9010Sstevel@tonic-gate zerror(zlogp, B_FALSE, "destroying snapshot: %s", 9020Sstevel@tonic-gate zonecfg_strerror(err)); 9030Sstevel@tonic-gate 9047370S<Gerald Jelinek> if (brand_poststatechg(zlogp, zstate, Z_HALT) != 0) 9057370S<Gerald Jelinek> return (-1); 9067370S<Gerald Jelinek> 9070Sstevel@tonic-gate return (0); 9080Sstevel@tonic-gate } 9090Sstevel@tonic-gate 9100Sstevel@tonic-gate /* 9110Sstevel@tonic-gate * Generate AUE_zone_state for a command that boots a zone. 9120Sstevel@tonic-gate */ 9130Sstevel@tonic-gate static void 9140Sstevel@tonic-gate audit_put_record(zlog_t *zlogp, ucred_t *uc, int return_val, 9150Sstevel@tonic-gate char *new_state) 9160Sstevel@tonic-gate { 9170Sstevel@tonic-gate adt_session_data_t *ah; 9180Sstevel@tonic-gate adt_event_data_t *event; 9190Sstevel@tonic-gate int pass_fail, fail_reason; 9200Sstevel@tonic-gate 9210Sstevel@tonic-gate if (!adt_audit_enabled()) 9220Sstevel@tonic-gate return; 9230Sstevel@tonic-gate 9240Sstevel@tonic-gate if (return_val == 0) { 9250Sstevel@tonic-gate pass_fail = ADT_SUCCESS; 9260Sstevel@tonic-gate fail_reason = ADT_SUCCESS; 9270Sstevel@tonic-gate } else { 9280Sstevel@tonic-gate pass_fail = ADT_FAILURE; 9290Sstevel@tonic-gate fail_reason = ADT_FAIL_VALUE_PROGRAM; 9300Sstevel@tonic-gate } 9310Sstevel@tonic-gate 9320Sstevel@tonic-gate if (adt_start_session(&ah, NULL, 0)) { 9330Sstevel@tonic-gate zerror(zlogp, B_TRUE, gettext("audit failure.")); 9340Sstevel@tonic-gate return; 9350Sstevel@tonic-gate } 9360Sstevel@tonic-gate if (adt_set_from_ucred(ah, uc, ADT_NEW)) { 9370Sstevel@tonic-gate zerror(zlogp, B_TRUE, gettext("audit failure.")); 9380Sstevel@tonic-gate (void) adt_end_session(ah); 9390Sstevel@tonic-gate return; 9400Sstevel@tonic-gate } 9410Sstevel@tonic-gate 9420Sstevel@tonic-gate event = adt_alloc_event(ah, ADT_zone_state); 9430Sstevel@tonic-gate if (event == NULL) { 9440Sstevel@tonic-gate zerror(zlogp, B_TRUE, gettext("audit failure.")); 9450Sstevel@tonic-gate (void) adt_end_session(ah); 9460Sstevel@tonic-gate return; 9470Sstevel@tonic-gate } 9480Sstevel@tonic-gate event->adt_zone_state.zonename = zone_name; 9490Sstevel@tonic-gate event->adt_zone_state.new_state = new_state; 9500Sstevel@tonic-gate 9510Sstevel@tonic-gate if (adt_put_event(event, pass_fail, fail_reason)) 9520Sstevel@tonic-gate zerror(zlogp, B_TRUE, gettext("audit failure.")); 9530Sstevel@tonic-gate 9540Sstevel@tonic-gate adt_free_event(event); 9550Sstevel@tonic-gate 9560Sstevel@tonic-gate (void) adt_end_session(ah); 9570Sstevel@tonic-gate } 9580Sstevel@tonic-gate 9590Sstevel@tonic-gate /* 9600Sstevel@tonic-gate * The main routine for the door server that deals with zone state transitions. 9610Sstevel@tonic-gate */ 9620Sstevel@tonic-gate /* ARGSUSED */ 9630Sstevel@tonic-gate static void 9640Sstevel@tonic-gate server(void *cookie, char *args, size_t alen, door_desc_t *dp, 9650Sstevel@tonic-gate uint_t n_desc) 9660Sstevel@tonic-gate { 9670Sstevel@tonic-gate ucred_t *uc = NULL; 9680Sstevel@tonic-gate const priv_set_t *eset; 9690Sstevel@tonic-gate 9700Sstevel@tonic-gate zone_state_t zstate; 9710Sstevel@tonic-gate zone_cmd_t cmd; 9720Sstevel@tonic-gate zone_cmd_arg_t *zargp; 9730Sstevel@tonic-gate 9740Sstevel@tonic-gate boolean_t kernelcall; 9750Sstevel@tonic-gate 9760Sstevel@tonic-gate int rval = -1; 9770Sstevel@tonic-gate uint64_t uniqid; 9780Sstevel@tonic-gate zoneid_t zoneid = -1; 9790Sstevel@tonic-gate zlog_t zlog; 9800Sstevel@tonic-gate zlog_t *zlogp; 9810Sstevel@tonic-gate zone_cmd_rval_t *rvalp; 9820Sstevel@tonic-gate size_t rlen = getpagesize(); /* conservative */ 9832712Snn35248 fs_callback_t cb; 9842727Sedp brand_handle_t bh; 9850Sstevel@tonic-gate 9860Sstevel@tonic-gate /* LINTED E_BAD_PTR_CAST_ALIGN */ 9870Sstevel@tonic-gate zargp = (zone_cmd_arg_t *)args; 9880Sstevel@tonic-gate 9890Sstevel@tonic-gate /* 9900Sstevel@tonic-gate * When we get the door unref message, we've fdetach'd the door, and 9910Sstevel@tonic-gate * it is time for us to shut down zoneadmd. 9920Sstevel@tonic-gate */ 9930Sstevel@tonic-gate if (zargp == DOOR_UNREF_DATA) { 9940Sstevel@tonic-gate /* 9950Sstevel@tonic-gate * See comment at end of main() for info on the last rites. 9960Sstevel@tonic-gate */ 9970Sstevel@tonic-gate exit(0); 9980Sstevel@tonic-gate } 9990Sstevel@tonic-gate 10000Sstevel@tonic-gate if (zargp == NULL) { 10010Sstevel@tonic-gate (void) door_return(NULL, 0, 0, 0); 10020Sstevel@tonic-gate } 10030Sstevel@tonic-gate 10040Sstevel@tonic-gate rvalp = alloca(rlen); 10050Sstevel@tonic-gate bzero(rvalp, rlen); 10060Sstevel@tonic-gate zlog.logfile = NULL; 10070Sstevel@tonic-gate zlog.buflen = zlog.loglen = rlen - sizeof (zone_cmd_rval_t) + 1; 10080Sstevel@tonic-gate zlog.buf = rvalp->errbuf; 10090Sstevel@tonic-gate zlog.log = zlog.buf; 10100Sstevel@tonic-gate /* defer initialization of zlog.locale until after credential check */ 10110Sstevel@tonic-gate zlogp = &zlog; 10120Sstevel@tonic-gate 10130Sstevel@tonic-gate if (alen != sizeof (zone_cmd_arg_t)) { 10140Sstevel@tonic-gate /* 10150Sstevel@tonic-gate * This really shouldn't be happening. 10160Sstevel@tonic-gate */ 10172267Sdp zerror(&logsys, B_FALSE, "argument size (%d bytes) " 10182267Sdp "unexpected (expected %d bytes)", alen, 10192267Sdp sizeof (zone_cmd_arg_t)); 10200Sstevel@tonic-gate goto out; 10210Sstevel@tonic-gate } 10220Sstevel@tonic-gate cmd = zargp->cmd; 10230Sstevel@tonic-gate 10240Sstevel@tonic-gate if (door_ucred(&uc) != 0) { 10250Sstevel@tonic-gate zerror(&logsys, B_TRUE, "door_ucred"); 10260Sstevel@tonic-gate goto out; 10270Sstevel@tonic-gate } 10280Sstevel@tonic-gate eset = ucred_getprivset(uc, PRIV_EFFECTIVE); 10290Sstevel@tonic-gate if (ucred_getzoneid(uc) != GLOBAL_ZONEID || 10300Sstevel@tonic-gate (eset != NULL ? !priv_ismember(eset, PRIV_SYS_CONFIG) : 10310Sstevel@tonic-gate ucred_geteuid(uc) != 0)) { 10320Sstevel@tonic-gate zerror(&logsys, B_FALSE, "insufficient privileges"); 10330Sstevel@tonic-gate goto out; 10340Sstevel@tonic-gate } 10350Sstevel@tonic-gate 10360Sstevel@tonic-gate kernelcall = ucred_getpid(uc) == 0; 10370Sstevel@tonic-gate 10380Sstevel@tonic-gate /* 10390Sstevel@tonic-gate * This is safe because we only use a zlog_t throughout the 10400Sstevel@tonic-gate * duration of a door call; i.e., by the time the pointer 10410Sstevel@tonic-gate * might become invalid, the door call would be over. 10420Sstevel@tonic-gate */ 10430Sstevel@tonic-gate zlog.locale = kernelcall ? DEFAULT_LOCALE : zargp->locale; 10440Sstevel@tonic-gate 10450Sstevel@tonic-gate (void) mutex_lock(&lock); 10460Sstevel@tonic-gate 10470Sstevel@tonic-gate /* 10480Sstevel@tonic-gate * Once we start to really die off, we don't want more connections. 10490Sstevel@tonic-gate */ 10500Sstevel@tonic-gate if (in_death_throes) { 10510Sstevel@tonic-gate (void) mutex_unlock(&lock); 10520Sstevel@tonic-gate ucred_free(uc); 10530Sstevel@tonic-gate (void) door_return(NULL, 0, 0, 0); 10540Sstevel@tonic-gate thr_exit(NULL); 10550Sstevel@tonic-gate } 10560Sstevel@tonic-gate 10570Sstevel@tonic-gate /* 10580Sstevel@tonic-gate * Check for validity of command. 10590Sstevel@tonic-gate */ 10602712Snn35248 if (cmd != Z_READY && cmd != Z_BOOT && cmd != Z_FORCEBOOT && 10612712Snn35248 cmd != Z_REBOOT && cmd != Z_HALT && cmd != Z_NOTE_UNINSTALLING && 10622712Snn35248 cmd != Z_MOUNT && cmd != Z_FORCEMOUNT && cmd != Z_UNMOUNT) { 1063766Scarlsonj zerror(&logsys, B_FALSE, "invalid command %d", (int)cmd); 10640Sstevel@tonic-gate goto out; 10650Sstevel@tonic-gate } 10660Sstevel@tonic-gate 10670Sstevel@tonic-gate if (kernelcall && (cmd != Z_HALT && cmd != Z_REBOOT)) { 10680Sstevel@tonic-gate /* 10690Sstevel@tonic-gate * Can't happen 10700Sstevel@tonic-gate */ 10710Sstevel@tonic-gate zerror(&logsys, B_FALSE, "received unexpected kernel upcall %d", 10720Sstevel@tonic-gate cmd); 10730Sstevel@tonic-gate goto out; 10740Sstevel@tonic-gate } 10750Sstevel@tonic-gate /* 10760Sstevel@tonic-gate * We ignore the possibility of someone calling zone_create(2) 10770Sstevel@tonic-gate * explicitly; all requests must come through zoneadmd. 10780Sstevel@tonic-gate */ 10790Sstevel@tonic-gate if (zone_get_state(zone_name, &zstate) != Z_OK) { 10800Sstevel@tonic-gate /* 10810Sstevel@tonic-gate * Something terribly wrong happened 10820Sstevel@tonic-gate */ 10830Sstevel@tonic-gate zerror(&logsys, B_FALSE, "unable to determine state of zone"); 10840Sstevel@tonic-gate goto out; 10850Sstevel@tonic-gate } 10860Sstevel@tonic-gate 10870Sstevel@tonic-gate if (kernelcall) { 10880Sstevel@tonic-gate /* 10890Sstevel@tonic-gate * Kernel-initiated requests may lose their validity if the 10900Sstevel@tonic-gate * zone_t the kernel was referring to has gone away. 10910Sstevel@tonic-gate */ 10920Sstevel@tonic-gate if ((zoneid = getzoneidbyname(zone_name)) == -1 || 10930Sstevel@tonic-gate zone_getattr(zoneid, ZONE_ATTR_UNIQID, &uniqid, 10940Sstevel@tonic-gate sizeof (uniqid)) == -1 || uniqid != zargp->uniqid) { 10950Sstevel@tonic-gate /* 10960Sstevel@tonic-gate * We're not talking about the same zone. The request 10970Sstevel@tonic-gate * must have arrived too late. Return error. 10980Sstevel@tonic-gate */ 10990Sstevel@tonic-gate rval = -1; 11000Sstevel@tonic-gate goto out; 11010Sstevel@tonic-gate } 11020Sstevel@tonic-gate zlogp = &logsys; /* Log errors to syslog */ 11030Sstevel@tonic-gate } 11040Sstevel@tonic-gate 11052712Snn35248 /* 11062712Snn35248 * If we are being asked to forcibly mount or boot a zone, we 11072712Snn35248 * pretend that an INCOMPLETE zone is actually INSTALLED. 11082712Snn35248 */ 11092712Snn35248 if (zstate == ZONE_STATE_INCOMPLETE && 11102712Snn35248 (cmd == Z_FORCEBOOT || cmd == Z_FORCEMOUNT)) 11112712Snn35248 zstate = ZONE_STATE_INSTALLED; 11122712Snn35248 11130Sstevel@tonic-gate switch (zstate) { 11140Sstevel@tonic-gate case ZONE_STATE_CONFIGURED: 11150Sstevel@tonic-gate case ZONE_STATE_INCOMPLETE: 11160Sstevel@tonic-gate /* 11170Sstevel@tonic-gate * Not our area of expertise; we just print a nice message 11180Sstevel@tonic-gate * and die off. 11190Sstevel@tonic-gate */ 11200Sstevel@tonic-gate zerror(zlogp, B_FALSE, 11210Sstevel@tonic-gate "%s operation is invalid for zones in state '%s'", 1122766Scarlsonj z_cmd_name(cmd), zone_state_str(zstate)); 11230Sstevel@tonic-gate break; 11240Sstevel@tonic-gate 11250Sstevel@tonic-gate case ZONE_STATE_INSTALLED: 11260Sstevel@tonic-gate switch (cmd) { 11270Sstevel@tonic-gate case Z_READY: 11287370S<Gerald Jelinek> rval = zone_ready(zlogp, Z_MNT_BOOT, zstate); 11290Sstevel@tonic-gate if (rval == 0) 11300Sstevel@tonic-gate eventstream_write(Z_EVT_ZONE_READIED); 11310Sstevel@tonic-gate break; 11320Sstevel@tonic-gate case Z_BOOT: 11332712Snn35248 case Z_FORCEBOOT: 11340Sstevel@tonic-gate eventstream_write(Z_EVT_ZONE_BOOTING); 11357370S<Gerald Jelinek> if ((rval = zone_ready(zlogp, Z_MNT_BOOT, zstate)) 11367370S<Gerald Jelinek> == 0) { 11377370S<Gerald Jelinek> rval = zone_bootup(zlogp, zargp->bootbuf, 11387370S<Gerald Jelinek> zstate); 11397370S<Gerald Jelinek> } 11400Sstevel@tonic-gate audit_put_record(zlogp, uc, rval, "boot"); 11410Sstevel@tonic-gate if (rval != 0) { 11420Sstevel@tonic-gate bringup_failure_recovery = B_TRUE; 11437370S<Gerald Jelinek> (void) zone_halt(zlogp, B_FALSE, B_FALSE, 11447370S<Gerald Jelinek> zstate); 11451645Scomay eventstream_write(Z_EVT_ZONE_BOOTFAILED); 11460Sstevel@tonic-gate } 11470Sstevel@tonic-gate break; 11480Sstevel@tonic-gate case Z_HALT: 11490Sstevel@tonic-gate if (kernelcall) /* Invalid; can't happen */ 11500Sstevel@tonic-gate abort(); 11510Sstevel@tonic-gate /* 11520Sstevel@tonic-gate * We could have two clients racing to halt this 11530Sstevel@tonic-gate * zone; the second client loses, but his request 11540Sstevel@tonic-gate * doesn't fail, since the zone is now in the desired 11550Sstevel@tonic-gate * state. 11560Sstevel@tonic-gate */ 11570Sstevel@tonic-gate zerror(zlogp, B_FALSE, "zone is already halted"); 11580Sstevel@tonic-gate rval = 0; 11590Sstevel@tonic-gate break; 11600Sstevel@tonic-gate case Z_REBOOT: 11610Sstevel@tonic-gate if (kernelcall) /* Invalid; can't happen */ 11620Sstevel@tonic-gate abort(); 11630Sstevel@tonic-gate zerror(zlogp, B_FALSE, "%s operation is invalid " 1164766Scarlsonj "for zones in state '%s'", z_cmd_name(cmd), 11650Sstevel@tonic-gate zone_state_str(zstate)); 11660Sstevel@tonic-gate rval = -1; 11670Sstevel@tonic-gate break; 11680Sstevel@tonic-gate case Z_NOTE_UNINSTALLING: 11690Sstevel@tonic-gate if (kernelcall) /* Invalid; can't happen */ 11700Sstevel@tonic-gate abort(); 11710Sstevel@tonic-gate /* 11720Sstevel@tonic-gate * Tell the console to print out a message about this. 11730Sstevel@tonic-gate * Once it does, we will be in_death_throes. 11740Sstevel@tonic-gate */ 11750Sstevel@tonic-gate eventstream_write(Z_EVT_ZONE_UNINSTALLING); 11760Sstevel@tonic-gate break; 1177766Scarlsonj case Z_MOUNT: 11782712Snn35248 case Z_FORCEMOUNT: 1179766Scarlsonj if (kernelcall) /* Invalid; can't happen */ 1180766Scarlsonj abort(); 11814350Std153743 if (!zone_isnative && !zone_iscluster) { 1182*7655Sgerald.jelinek@sun.com /* 1183*7655Sgerald.jelinek@sun.com * -U mounts the zone without lofs mounting 1184*7655Sgerald.jelinek@sun.com * zone file systems back into the scratch 1185*7655Sgerald.jelinek@sun.com * zone. This is required when mounting 1186*7655Sgerald.jelinek@sun.com * non-native branded zones. 1187*7655Sgerald.jelinek@sun.com */ 1188*7655Sgerald.jelinek@sun.com (void) strlcpy(zargp->bootbuf, "-U", 1189*7655Sgerald.jelinek@sun.com BOOTARGS_MAX); 11902712Snn35248 } 11912712Snn35248 11925829Sgjelinek rval = zone_ready(zlogp, 11935829Sgjelinek strcmp(zargp->bootbuf, "-U") == 0 ? 11947370S<Gerald Jelinek> Z_MNT_UPDATE : Z_MNT_SCRATCH, zstate); 11952712Snn35248 if (rval != 0) 11962712Snn35248 break; 11972712Snn35248 11982712Snn35248 eventstream_write(Z_EVT_ZONE_READIED); 11992712Snn35248 1200*7655Sgerald.jelinek@sun.com /* 1201*7655Sgerald.jelinek@sun.com * Get a handle to the native brand info. 1202*7655Sgerald.jelinek@sun.com * We must always use the native brand file system 1203*7655Sgerald.jelinek@sun.com * list when mounting the zone. 1204*7655Sgerald.jelinek@sun.com */ 1205*7655Sgerald.jelinek@sun.com if ((bh = brand_open(NATIVE_BRAND_NAME)) == NULL) { 12062712Snn35248 rval = -1; 12072712Snn35248 break; 12081645Scomay } 12091645Scomay 1210766Scarlsonj /* 12112712Snn35248 * Get the list of filesystems to mount from 12122712Snn35248 * the brand configuration. These mounts are done 12132712Snn35248 * via a thread that will enter the zone, so they 12142712Snn35248 * are done from within the context of the zone. 12152712Snn35248 */ 12162712Snn35248 cb.zlogp = zlogp; 12172712Snn35248 cb.zoneid = zone_id; 12185576Sedp cb.mount_cmd = B_TRUE; 12192727Sedp rval = brand_platform_iter_mounts(bh, 12202712Snn35248 mount_early_fs, &cb); 12212712Snn35248 12222727Sedp brand_close(bh); 12232712Snn35248 12242712Snn35248 /* 1225766Scarlsonj * Ordinarily, /dev/fd would be mounted inside the zone 1226766Scarlsonj * by svc:/system/filesystem/usr:default, but since 1227766Scarlsonj * we're not booting the zone, we need to do this 1228766Scarlsonj * manually. 1229766Scarlsonj */ 1230766Scarlsonj if (rval == 0) 12312712Snn35248 rval = mount_early_fs(&cb, 12322712Snn35248 "fd", "/dev/fd", "fd", NULL); 1233766Scarlsonj break; 1234766Scarlsonj case Z_UNMOUNT: 1235766Scarlsonj if (kernelcall) /* Invalid; can't happen */ 1236766Scarlsonj abort(); 1237766Scarlsonj zerror(zlogp, B_FALSE, "zone is already unmounted"); 1238766Scarlsonj rval = 0; 1239766Scarlsonj break; 12400Sstevel@tonic-gate } 12410Sstevel@tonic-gate break; 12420Sstevel@tonic-gate 12430Sstevel@tonic-gate case ZONE_STATE_READY: 12440Sstevel@tonic-gate switch (cmd) { 12450Sstevel@tonic-gate case Z_READY: 12460Sstevel@tonic-gate /* 12470Sstevel@tonic-gate * We could have two clients racing to ready this 12480Sstevel@tonic-gate * zone; the second client loses, but his request 12490Sstevel@tonic-gate * doesn't fail, since the zone is now in the desired 12500Sstevel@tonic-gate * state. 12510Sstevel@tonic-gate */ 12520Sstevel@tonic-gate zerror(zlogp, B_FALSE, "zone is already ready"); 12530Sstevel@tonic-gate rval = 0; 12540Sstevel@tonic-gate break; 12550Sstevel@tonic-gate case Z_BOOT: 12562267Sdp (void) strlcpy(boot_args, zargp->bootbuf, 12572267Sdp sizeof (boot_args)); 12580Sstevel@tonic-gate eventstream_write(Z_EVT_ZONE_BOOTING); 12597370S<Gerald Jelinek> rval = zone_bootup(zlogp, zargp->bootbuf, zstate); 12600Sstevel@tonic-gate audit_put_record(zlogp, uc, rval, "boot"); 12610Sstevel@tonic-gate if (rval != 0) { 12620Sstevel@tonic-gate bringup_failure_recovery = B_TRUE; 12637370S<Gerald Jelinek> (void) zone_halt(zlogp, B_FALSE, B_TRUE, 12647370S<Gerald Jelinek> zstate); 12651645Scomay eventstream_write(Z_EVT_ZONE_BOOTFAILED); 12660Sstevel@tonic-gate } 12672267Sdp boot_args[0] = '\0'; 12680Sstevel@tonic-gate break; 12690Sstevel@tonic-gate case Z_HALT: 12700Sstevel@tonic-gate if (kernelcall) /* Invalid; can't happen */ 12710Sstevel@tonic-gate abort(); 12727370S<Gerald Jelinek> if ((rval = zone_halt(zlogp, B_FALSE, B_FALSE, zstate)) 12737370S<Gerald Jelinek> != 0) 12740Sstevel@tonic-gate break; 12750Sstevel@tonic-gate eventstream_write(Z_EVT_ZONE_HALTED); 12760Sstevel@tonic-gate break; 12770Sstevel@tonic-gate case Z_REBOOT: 1278766Scarlsonj case Z_NOTE_UNINSTALLING: 1279766Scarlsonj case Z_MOUNT: 1280766Scarlsonj case Z_UNMOUNT: 12810Sstevel@tonic-gate if (kernelcall) /* Invalid; can't happen */ 12820Sstevel@tonic-gate abort(); 12830Sstevel@tonic-gate zerror(zlogp, B_FALSE, "%s operation is invalid " 1284766Scarlsonj "for zones in state '%s'", z_cmd_name(cmd), 12850Sstevel@tonic-gate zone_state_str(zstate)); 12860Sstevel@tonic-gate rval = -1; 12870Sstevel@tonic-gate break; 1288766Scarlsonj } 1289766Scarlsonj break; 1290766Scarlsonj 1291766Scarlsonj case ZONE_STATE_MOUNTED: 1292766Scarlsonj switch (cmd) { 1293766Scarlsonj case Z_UNMOUNT: 12940Sstevel@tonic-gate if (kernelcall) /* Invalid; can't happen */ 12950Sstevel@tonic-gate abort(); 12967370S<Gerald Jelinek> rval = zone_halt(zlogp, B_TRUE, B_FALSE, zstate); 12971645Scomay if (rval == 0) { 12981645Scomay eventstream_write(Z_EVT_ZONE_HALTED); 1299766Scarlsonj (void) sema_post(&scratch_sem); 13001645Scomay } 1301766Scarlsonj break; 1302766Scarlsonj default: 1303766Scarlsonj if (kernelcall) /* Invalid; can't happen */ 1304766Scarlsonj abort(); 1305766Scarlsonj zerror(zlogp, B_FALSE, "%s operation is invalid " 1306766Scarlsonj "for zones in state '%s'", z_cmd_name(cmd), 1307766Scarlsonj zone_state_str(zstate)); 13080Sstevel@tonic-gate rval = -1; 13090Sstevel@tonic-gate break; 13100Sstevel@tonic-gate } 13110Sstevel@tonic-gate break; 13120Sstevel@tonic-gate 13130Sstevel@tonic-gate case ZONE_STATE_RUNNING: 13140Sstevel@tonic-gate case ZONE_STATE_SHUTTING_DOWN: 13150Sstevel@tonic-gate case ZONE_STATE_DOWN: 13160Sstevel@tonic-gate switch (cmd) { 13170Sstevel@tonic-gate case Z_READY: 13187370S<Gerald Jelinek> if ((rval = zone_halt(zlogp, B_FALSE, B_TRUE, zstate)) 13197370S<Gerald Jelinek> != 0) 13200Sstevel@tonic-gate break; 13217370S<Gerald Jelinek> if ((rval = zone_ready(zlogp, Z_MNT_BOOT, zstate)) == 0) 13220Sstevel@tonic-gate eventstream_write(Z_EVT_ZONE_READIED); 13231645Scomay else 13241645Scomay eventstream_write(Z_EVT_ZONE_HALTED); 13250Sstevel@tonic-gate break; 13260Sstevel@tonic-gate case Z_BOOT: 13270Sstevel@tonic-gate /* 13280Sstevel@tonic-gate * We could have two clients racing to boot this 13290Sstevel@tonic-gate * zone; the second client loses, but his request 13300Sstevel@tonic-gate * doesn't fail, since the zone is now in the desired 13310Sstevel@tonic-gate * state. 13320Sstevel@tonic-gate */ 13330Sstevel@tonic-gate zerror(zlogp, B_FALSE, "zone is already booted"); 13340Sstevel@tonic-gate rval = 0; 13350Sstevel@tonic-gate break; 13360Sstevel@tonic-gate case Z_HALT: 13377370S<Gerald Jelinek> if ((rval = zone_halt(zlogp, B_FALSE, B_FALSE, zstate)) 13387370S<Gerald Jelinek> != 0) 13390Sstevel@tonic-gate break; 13400Sstevel@tonic-gate eventstream_write(Z_EVT_ZONE_HALTED); 13410Sstevel@tonic-gate break; 13420Sstevel@tonic-gate case Z_REBOOT: 13432267Sdp (void) strlcpy(boot_args, zargp->bootbuf, 13442267Sdp sizeof (boot_args)); 13450Sstevel@tonic-gate eventstream_write(Z_EVT_ZONE_REBOOTING); 13467370S<Gerald Jelinek> if ((rval = zone_halt(zlogp, B_FALSE, B_TRUE, zstate)) 13477370S<Gerald Jelinek> != 0) { 13481645Scomay eventstream_write(Z_EVT_ZONE_BOOTFAILED); 13492267Sdp boot_args[0] = '\0'; 13501645Scomay break; 13511645Scomay } 13527370S<Gerald Jelinek> if ((rval = zone_ready(zlogp, Z_MNT_BOOT, zstate)) 13537370S<Gerald Jelinek> != 0) { 13541645Scomay eventstream_write(Z_EVT_ZONE_BOOTFAILED); 13552267Sdp boot_args[0] = '\0'; 13560Sstevel@tonic-gate break; 13571645Scomay } 13587370S<Gerald Jelinek> rval = zone_bootup(zlogp, zargp->bootbuf, zstate); 13591645Scomay audit_put_record(zlogp, uc, rval, "reboot"); 13601645Scomay if (rval != 0) { 13617370S<Gerald Jelinek> (void) zone_halt(zlogp, B_FALSE, B_TRUE, 13627370S<Gerald Jelinek> zstate); 13631645Scomay eventstream_write(Z_EVT_ZONE_BOOTFAILED); 13640Sstevel@tonic-gate } 13652267Sdp boot_args[0] = '\0'; 13660Sstevel@tonic-gate break; 13670Sstevel@tonic-gate case Z_NOTE_UNINSTALLING: 1368766Scarlsonj case Z_MOUNT: 1369766Scarlsonj case Z_UNMOUNT: 1370766Scarlsonj zerror(zlogp, B_FALSE, "%s operation is invalid " 1371766Scarlsonj "for zones in state '%s'", z_cmd_name(cmd), 1372766Scarlsonj zone_state_str(zstate)); 13730Sstevel@tonic-gate rval = -1; 13740Sstevel@tonic-gate break; 13750Sstevel@tonic-gate } 13760Sstevel@tonic-gate break; 13770Sstevel@tonic-gate default: 13780Sstevel@tonic-gate abort(); 13790Sstevel@tonic-gate } 13800Sstevel@tonic-gate 13810Sstevel@tonic-gate /* 13820Sstevel@tonic-gate * Because the state of the zone may have changed, we make sure 13830Sstevel@tonic-gate * to wake the console poller, which is in charge of initiating 13840Sstevel@tonic-gate * the shutdown procedure as necessary. 13850Sstevel@tonic-gate */ 13860Sstevel@tonic-gate eventstream_write(Z_EVT_NULL); 13870Sstevel@tonic-gate 13880Sstevel@tonic-gate out: 13890Sstevel@tonic-gate (void) mutex_unlock(&lock); 13900Sstevel@tonic-gate if (kernelcall) { 13910Sstevel@tonic-gate rvalp = NULL; 13920Sstevel@tonic-gate rlen = 0; 13930Sstevel@tonic-gate } else { 13940Sstevel@tonic-gate rvalp->rval = rval; 13950Sstevel@tonic-gate } 13960Sstevel@tonic-gate if (uc != NULL) 13970Sstevel@tonic-gate ucred_free(uc); 13980Sstevel@tonic-gate (void) door_return((char *)rvalp, rlen, NULL, 0); 13990Sstevel@tonic-gate thr_exit(NULL); 14000Sstevel@tonic-gate } 14010Sstevel@tonic-gate 14020Sstevel@tonic-gate static int 14030Sstevel@tonic-gate setup_door(zlog_t *zlogp) 14040Sstevel@tonic-gate { 14050Sstevel@tonic-gate if ((zone_door = door_create(server, NULL, 14060Sstevel@tonic-gate DOOR_UNREF | DOOR_REFUSE_DESC | DOOR_NO_CANCEL)) < 0) { 14070Sstevel@tonic-gate zerror(zlogp, B_TRUE, "%s failed", "door_create"); 14080Sstevel@tonic-gate return (-1); 14090Sstevel@tonic-gate } 14100Sstevel@tonic-gate (void) fdetach(zone_door_path); 14110Sstevel@tonic-gate 14120Sstevel@tonic-gate if (fattach(zone_door, zone_door_path) != 0) { 14130Sstevel@tonic-gate zerror(zlogp, B_TRUE, "fattach to %s failed", zone_door_path); 14140Sstevel@tonic-gate (void) door_revoke(zone_door); 14150Sstevel@tonic-gate (void) fdetach(zone_door_path); 14160Sstevel@tonic-gate zone_door = -1; 14170Sstevel@tonic-gate return (-1); 14180Sstevel@tonic-gate } 14190Sstevel@tonic-gate return (0); 14200Sstevel@tonic-gate } 14210Sstevel@tonic-gate 14220Sstevel@tonic-gate /* 14230Sstevel@tonic-gate * zoneadm(1m) will start zoneadmd if it thinks it isn't running; this 14240Sstevel@tonic-gate * is where zoneadmd itself will check to see that another instance of 14250Sstevel@tonic-gate * zoneadmd isn't already controlling this zone. 14260Sstevel@tonic-gate * 14270Sstevel@tonic-gate * The idea here is that we want to open the path to which we will 14280Sstevel@tonic-gate * attach our door, lock it, and then make sure that no-one has beat us 14290Sstevel@tonic-gate * to fattach(3c)ing onto it. 14300Sstevel@tonic-gate * 14310Sstevel@tonic-gate * fattach(3c) is really a mount, so there are actually two possible 14320Sstevel@tonic-gate * vnodes we could be dealing with. Our strategy is as follows: 14330Sstevel@tonic-gate * 14340Sstevel@tonic-gate * - If the file we opened is a regular file (common case): 14350Sstevel@tonic-gate * There is no fattach(3c)ed door, so we have a chance of becoming 14360Sstevel@tonic-gate * the managing zoneadmd. We attempt to lock the file: if it is 14370Sstevel@tonic-gate * already locked, that means someone else raced us here, so we 14380Sstevel@tonic-gate * lose and give up. zoneadm(1m) will try to contact the zoneadmd 14390Sstevel@tonic-gate * that beat us to it. 14400Sstevel@tonic-gate * 14410Sstevel@tonic-gate * - If the file we opened is a namefs file: 14420Sstevel@tonic-gate * This means there is already an established door fattach(3c)'ed 14430Sstevel@tonic-gate * to the rendezvous path. We've lost the race, so we give up. 14440Sstevel@tonic-gate * Note that in this case we also try to grab the file lock, and 14450Sstevel@tonic-gate * will succeed in acquiring it since the vnode locked by the 14460Sstevel@tonic-gate * "winning" zoneadmd was a regular one, and the one we locked was 14470Sstevel@tonic-gate * the fattach(3c)'ed door node. At any rate, no harm is done, and 14480Sstevel@tonic-gate * we just return to zoneadm(1m) which knows to retry. 14490Sstevel@tonic-gate */ 14500Sstevel@tonic-gate static int 14510Sstevel@tonic-gate make_daemon_exclusive(zlog_t *zlogp) 14520Sstevel@tonic-gate { 14530Sstevel@tonic-gate int doorfd = -1; 14540Sstevel@tonic-gate int err, ret = -1; 14550Sstevel@tonic-gate struct stat st; 14560Sstevel@tonic-gate struct flock flock; 14570Sstevel@tonic-gate zone_state_t zstate; 14580Sstevel@tonic-gate 14590Sstevel@tonic-gate top: 14600Sstevel@tonic-gate if ((err = zone_get_state(zone_name, &zstate)) != Z_OK) { 14612267Sdp zerror(zlogp, B_FALSE, "failed to get zone state: %s", 14620Sstevel@tonic-gate zonecfg_strerror(err)); 14630Sstevel@tonic-gate goto out; 14640Sstevel@tonic-gate } 14650Sstevel@tonic-gate if ((doorfd = open(zone_door_path, O_CREAT|O_RDWR, 14660Sstevel@tonic-gate S_IREAD|S_IWRITE)) < 0) { 14670Sstevel@tonic-gate zerror(zlogp, B_TRUE, "failed to open %s", zone_door_path); 14680Sstevel@tonic-gate goto out; 14690Sstevel@tonic-gate } 14700Sstevel@tonic-gate if (fstat(doorfd, &st) < 0) { 14710Sstevel@tonic-gate zerror(zlogp, B_TRUE, "failed to stat %s", zone_door_path); 14720Sstevel@tonic-gate goto out; 14730Sstevel@tonic-gate } 14740Sstevel@tonic-gate /* 14750Sstevel@tonic-gate * Lock the file to synchronize with other zoneadmd 14760Sstevel@tonic-gate */ 14770Sstevel@tonic-gate flock.l_type = F_WRLCK; 14780Sstevel@tonic-gate flock.l_whence = SEEK_SET; 14790Sstevel@tonic-gate flock.l_start = (off_t)0; 14800Sstevel@tonic-gate flock.l_len = (off_t)0; 14810Sstevel@tonic-gate if (fcntl(doorfd, F_SETLK, &flock) < 0) { 14820Sstevel@tonic-gate /* 14830Sstevel@tonic-gate * Someone else raced us here and grabbed the lock file 14840Sstevel@tonic-gate * first. A warning here is inappropriate since nothing 14850Sstevel@tonic-gate * went wrong. 14860Sstevel@tonic-gate */ 14870Sstevel@tonic-gate goto out; 14880Sstevel@tonic-gate } 14890Sstevel@tonic-gate 14900Sstevel@tonic-gate if (strcmp(st.st_fstype, "namefs") == 0) { 14910Sstevel@tonic-gate struct door_info info; 14920Sstevel@tonic-gate 14930Sstevel@tonic-gate /* 14940Sstevel@tonic-gate * There is already something fattach()'ed to this file. 14950Sstevel@tonic-gate * Lets see what the door is up to. 14960Sstevel@tonic-gate */ 14970Sstevel@tonic-gate if (door_info(doorfd, &info) == 0 && info.di_target != -1) { 14980Sstevel@tonic-gate /* 14990Sstevel@tonic-gate * Another zoneadmd process seems to be in 15000Sstevel@tonic-gate * control of the situation and we don't need to 15010Sstevel@tonic-gate * be here. A warning here is inappropriate 15020Sstevel@tonic-gate * since nothing went wrong. 15030Sstevel@tonic-gate * 15040Sstevel@tonic-gate * If the door has been revoked, the zoneadmd 15050Sstevel@tonic-gate * process currently managing the zone is going 15060Sstevel@tonic-gate * away. We'll return control to zoneadm(1m) 15070Sstevel@tonic-gate * which will try again (by which time zoneadmd 15080Sstevel@tonic-gate * will hopefully have exited). 15090Sstevel@tonic-gate */ 15100Sstevel@tonic-gate goto out; 15110Sstevel@tonic-gate } 15120Sstevel@tonic-gate 15130Sstevel@tonic-gate /* 15140Sstevel@tonic-gate * If we got this far, there's a fattach(3c)'ed door 15150Sstevel@tonic-gate * that belongs to a process that has exited, which can 15160Sstevel@tonic-gate * happen if the previous zoneadmd died unexpectedly. 15170Sstevel@tonic-gate * 15180Sstevel@tonic-gate * Let user know that something is amiss, but that we can 15190Sstevel@tonic-gate * recover; if the zone is in the installed state, then don't 15200Sstevel@tonic-gate * message, since having a running zoneadmd isn't really 15210Sstevel@tonic-gate * expected/needed. We want to keep occurences of this message 15220Sstevel@tonic-gate * limited to times when zoneadmd is picking back up from a 15230Sstevel@tonic-gate * zoneadmd that died while the zone was in some non-trivial 15240Sstevel@tonic-gate * state. 15250Sstevel@tonic-gate */ 15260Sstevel@tonic-gate if (zstate > ZONE_STATE_INSTALLED) { 15270Sstevel@tonic-gate zerror(zlogp, B_FALSE, 15280Sstevel@tonic-gate "zone '%s': WARNING: zone is in state '%s', but " 15290Sstevel@tonic-gate "zoneadmd does not appear to be available; " 15300Sstevel@tonic-gate "restarted zoneadmd to recover.", 15310Sstevel@tonic-gate zone_name, zone_state_str(zstate)); 15320Sstevel@tonic-gate } 15330Sstevel@tonic-gate 15340Sstevel@tonic-gate (void) fdetach(zone_door_path); 15350Sstevel@tonic-gate (void) close(doorfd); 15360Sstevel@tonic-gate goto top; 15370Sstevel@tonic-gate } 15380Sstevel@tonic-gate ret = 0; 15390Sstevel@tonic-gate out: 15400Sstevel@tonic-gate (void) close(doorfd); 15410Sstevel@tonic-gate return (ret); 15420Sstevel@tonic-gate } 15430Sstevel@tonic-gate 15447370S<Gerald Jelinek> /* 15457370S<Gerald Jelinek> * Setup the brand's pre and post state change callbacks, as well as the 15467370S<Gerald Jelinek> * query callback, if any of these exist. 15477370S<Gerald Jelinek> */ 15487370S<Gerald Jelinek> static int 15497370S<Gerald Jelinek> brand_callback_init(brand_handle_t bh, char *zone_name) 15507370S<Gerald Jelinek> { 15517370S<Gerald Jelinek> char zpath[MAXPATHLEN]; 15527370S<Gerald Jelinek> 15537370S<Gerald Jelinek> if (zone_get_zonepath(zone_name, zpath, sizeof (zpath)) != Z_OK) 15547370S<Gerald Jelinek> return (-1); 15557370S<Gerald Jelinek> 15567370S<Gerald Jelinek> (void) strlcpy(pre_statechg_hook, EXEC_PREFIX, 15577370S<Gerald Jelinek> sizeof (pre_statechg_hook)); 15587370S<Gerald Jelinek> 15597370S<Gerald Jelinek> if (brand_get_prestatechange(bh, zone_name, zpath, 15607370S<Gerald Jelinek> pre_statechg_hook + EXEC_LEN, 15617370S<Gerald Jelinek> sizeof (pre_statechg_hook) - EXEC_LEN) != 0) 15627370S<Gerald Jelinek> return (-1); 15637370S<Gerald Jelinek> 15647370S<Gerald Jelinek> if (strlen(pre_statechg_hook) <= EXEC_LEN) 15657370S<Gerald Jelinek> pre_statechg_hook[0] = '\0'; 15667370S<Gerald Jelinek> 15677370S<Gerald Jelinek> (void) strlcpy(post_statechg_hook, EXEC_PREFIX, 15687370S<Gerald Jelinek> sizeof (post_statechg_hook)); 15697370S<Gerald Jelinek> 15707370S<Gerald Jelinek> if (brand_get_poststatechange(bh, zone_name, zpath, 15717370S<Gerald Jelinek> post_statechg_hook + EXEC_LEN, 15727370S<Gerald Jelinek> sizeof (post_statechg_hook) - EXEC_LEN) != 0) 15737370S<Gerald Jelinek> return (-1); 15747370S<Gerald Jelinek> 15757370S<Gerald Jelinek> if (strlen(post_statechg_hook) <= EXEC_LEN) 15767370S<Gerald Jelinek> post_statechg_hook[0] = '\0'; 15777370S<Gerald Jelinek> 15787370S<Gerald Jelinek> (void) strlcpy(query_hook, EXEC_PREFIX, 15797370S<Gerald Jelinek> sizeof (query_hook)); 15807370S<Gerald Jelinek> 15817370S<Gerald Jelinek> if (brand_get_query(bh, zone_name, zpath, query_hook + EXEC_LEN, 15827370S<Gerald Jelinek> sizeof (query_hook) - EXEC_LEN) != 0) 15837370S<Gerald Jelinek> return (-1); 15847370S<Gerald Jelinek> 15857370S<Gerald Jelinek> if (strlen(query_hook) <= EXEC_LEN) 15867370S<Gerald Jelinek> query_hook[0] = '\0'; 15877370S<Gerald Jelinek> 15887370S<Gerald Jelinek> return (0); 15897370S<Gerald Jelinek> } 15907370S<Gerald Jelinek> 15910Sstevel@tonic-gate int 15920Sstevel@tonic-gate main(int argc, char *argv[]) 15930Sstevel@tonic-gate { 15940Sstevel@tonic-gate int opt; 15950Sstevel@tonic-gate zoneid_t zid; 15960Sstevel@tonic-gate priv_set_t *privset; 15970Sstevel@tonic-gate zone_state_t zstate; 15980Sstevel@tonic-gate char parents_locale[MAXPATHLEN]; 15992727Sedp brand_handle_t bh; 16000Sstevel@tonic-gate int err; 16010Sstevel@tonic-gate 16020Sstevel@tonic-gate pid_t pid; 16030Sstevel@tonic-gate sigset_t blockset; 16040Sstevel@tonic-gate sigset_t block_cld; 16050Sstevel@tonic-gate 16060Sstevel@tonic-gate struct { 16070Sstevel@tonic-gate sema_t sem; 16080Sstevel@tonic-gate int status; 16090Sstevel@tonic-gate zlog_t log; 16100Sstevel@tonic-gate } *shstate; 16110Sstevel@tonic-gate size_t shstatelen = getpagesize(); 16120Sstevel@tonic-gate 16130Sstevel@tonic-gate zlog_t errlog; 16140Sstevel@tonic-gate zlog_t *zlogp; 16150Sstevel@tonic-gate 16162382Sdp int ctfd; 16172382Sdp 16180Sstevel@tonic-gate progname = get_execbasename(argv[0]); 16190Sstevel@tonic-gate 16200Sstevel@tonic-gate /* 16210Sstevel@tonic-gate * Make sure stderr is unbuffered 16220Sstevel@tonic-gate */ 16230Sstevel@tonic-gate (void) setbuffer(stderr, NULL, 0); 16240Sstevel@tonic-gate 16250Sstevel@tonic-gate /* 16260Sstevel@tonic-gate * Get out of the way of mounted filesystems, since we will daemonize 16270Sstevel@tonic-gate * soon. 16280Sstevel@tonic-gate */ 16290Sstevel@tonic-gate (void) chdir("/"); 16300Sstevel@tonic-gate 16310Sstevel@tonic-gate /* 16320Sstevel@tonic-gate * Use the default system umask per PSARC 1998/110 rather than 16330Sstevel@tonic-gate * anything that may have been set by the caller. 16340Sstevel@tonic-gate */ 16350Sstevel@tonic-gate (void) umask(CMASK); 16360Sstevel@tonic-gate 16370Sstevel@tonic-gate /* 16380Sstevel@tonic-gate * Initially we want to use our parent's locale. 16390Sstevel@tonic-gate */ 16400Sstevel@tonic-gate (void) setlocale(LC_ALL, ""); 16410Sstevel@tonic-gate (void) textdomain(TEXT_DOMAIN); 16420Sstevel@tonic-gate (void) strlcpy(parents_locale, setlocale(LC_MESSAGES, NULL), 16430Sstevel@tonic-gate sizeof (parents_locale)); 16440Sstevel@tonic-gate 16450Sstevel@tonic-gate /* 16460Sstevel@tonic-gate * This zlog_t is used for writing to stderr 16470Sstevel@tonic-gate */ 16480Sstevel@tonic-gate errlog.logfile = stderr; 16490Sstevel@tonic-gate errlog.buflen = errlog.loglen = 0; 16500Sstevel@tonic-gate errlog.buf = errlog.log = NULL; 16510Sstevel@tonic-gate errlog.locale = parents_locale; 16520Sstevel@tonic-gate 16530Sstevel@tonic-gate /* 16540Sstevel@tonic-gate * We start off writing to stderr until we're ready to daemonize. 16550Sstevel@tonic-gate */ 16560Sstevel@tonic-gate zlogp = &errlog; 16570Sstevel@tonic-gate 16580Sstevel@tonic-gate /* 16590Sstevel@tonic-gate * Process options. 16600Sstevel@tonic-gate */ 1661766Scarlsonj while ((opt = getopt(argc, argv, "R:z:")) != EOF) { 16620Sstevel@tonic-gate switch (opt) { 1663766Scarlsonj case 'R': 1664766Scarlsonj zonecfg_set_root(optarg); 1665766Scarlsonj break; 16660Sstevel@tonic-gate case 'z': 16670Sstevel@tonic-gate zone_name = optarg; 16680Sstevel@tonic-gate break; 16690Sstevel@tonic-gate default: 16700Sstevel@tonic-gate usage(); 16710Sstevel@tonic-gate } 16720Sstevel@tonic-gate } 16730Sstevel@tonic-gate 16740Sstevel@tonic-gate if (zone_name == NULL) 16750Sstevel@tonic-gate usage(); 16760Sstevel@tonic-gate 16770Sstevel@tonic-gate /* 16780Sstevel@tonic-gate * Because usage() prints directly to stderr, it has gettext() 16790Sstevel@tonic-gate * wrapping, which depends on the locale. But since zerror() calls 16800Sstevel@tonic-gate * localize() which tweaks the locale, it is not safe to call zerror() 16810Sstevel@tonic-gate * until after the last call to usage(). Fortunately, the last call 16820Sstevel@tonic-gate * to usage() is just above and the first call to zerror() is just 16830Sstevel@tonic-gate * below. Don't mess this up. 16840Sstevel@tonic-gate */ 16850Sstevel@tonic-gate if (strcmp(zone_name, GLOBAL_ZONENAME) == 0) { 16860Sstevel@tonic-gate zerror(zlogp, B_FALSE, "cannot manage the %s zone", 16870Sstevel@tonic-gate GLOBAL_ZONENAME); 16880Sstevel@tonic-gate return (1); 16890Sstevel@tonic-gate } 16900Sstevel@tonic-gate 16910Sstevel@tonic-gate if (zone_get_id(zone_name, &zid) != 0) { 16922267Sdp zerror(zlogp, B_FALSE, "could not manage %s: %s", zone_name, 16930Sstevel@tonic-gate zonecfg_strerror(Z_NO_ZONE)); 16940Sstevel@tonic-gate return (1); 16950Sstevel@tonic-gate } 16960Sstevel@tonic-gate 16970Sstevel@tonic-gate if ((err = zone_get_state(zone_name, &zstate)) != Z_OK) { 16982267Sdp zerror(zlogp, B_FALSE, "failed to get zone state: %s", 16990Sstevel@tonic-gate zonecfg_strerror(err)); 17000Sstevel@tonic-gate return (1); 17010Sstevel@tonic-gate } 17022712Snn35248 if (zstate < ZONE_STATE_INCOMPLETE) { 17030Sstevel@tonic-gate zerror(zlogp, B_FALSE, 17040Sstevel@tonic-gate "cannot manage a zone which is in state '%s'", 17050Sstevel@tonic-gate zone_state_str(zstate)); 17060Sstevel@tonic-gate return (1); 17070Sstevel@tonic-gate } 17080Sstevel@tonic-gate 17092712Snn35248 /* Get a handle to the brand info for this zone */ 17102712Snn35248 if ((zone_get_brand(zone_name, brand_name, sizeof (brand_name)) 17112727Sedp != Z_OK) || (bh = brand_open(brand_name)) == NULL) { 17122712Snn35248 zerror(zlogp, B_FALSE, "unable to determine zone brand"); 17132712Snn35248 return (1); 17142712Snn35248 } 17152727Sedp zone_isnative = brand_is_native(bh); 17164350Std153743 zone_iscluster = (strcmp(brand_name, CLUSTER_BRAND_NAME) == 0); 17177370S<Gerald Jelinek> 17187370S<Gerald Jelinek> /* Get state change brand hooks. */ 17197370S<Gerald Jelinek> if (brand_callback_init(bh, zone_name) == -1) { 17207370S<Gerald Jelinek> zerror(zlogp, B_TRUE, 17217370S<Gerald Jelinek> "failed to initialize brand state change hooks"); 17227370S<Gerald Jelinek> brand_close(bh); 17237370S<Gerald Jelinek> return (1); 17247370S<Gerald Jelinek> } 17257370S<Gerald Jelinek> 17262727Sedp brand_close(bh); 17272712Snn35248 17280Sstevel@tonic-gate /* 17290Sstevel@tonic-gate * Check that we have all privileges. It would be nice to pare 17300Sstevel@tonic-gate * this down, but this is at least a first cut. 17310Sstevel@tonic-gate */ 17320Sstevel@tonic-gate if ((privset = priv_allocset()) == NULL) { 17330Sstevel@tonic-gate zerror(zlogp, B_TRUE, "%s failed", "priv_allocset"); 17340Sstevel@tonic-gate return (1); 17350Sstevel@tonic-gate } 17360Sstevel@tonic-gate 17370Sstevel@tonic-gate if (getppriv(PRIV_EFFECTIVE, privset) != 0) { 17380Sstevel@tonic-gate zerror(zlogp, B_TRUE, "%s failed", "getppriv"); 17390Sstevel@tonic-gate priv_freeset(privset); 17400Sstevel@tonic-gate return (1); 17410Sstevel@tonic-gate } 17420Sstevel@tonic-gate 17430Sstevel@tonic-gate if (priv_isfullset(privset) == B_FALSE) { 17440Sstevel@tonic-gate zerror(zlogp, B_FALSE, "You lack sufficient privilege to " 17452267Sdp "run this command (all privs required)"); 17460Sstevel@tonic-gate priv_freeset(privset); 17470Sstevel@tonic-gate return (1); 17480Sstevel@tonic-gate } 17490Sstevel@tonic-gate priv_freeset(privset); 17500Sstevel@tonic-gate 17510Sstevel@tonic-gate if (mkzonedir(zlogp) != 0) 17520Sstevel@tonic-gate return (1); 17530Sstevel@tonic-gate 17540Sstevel@tonic-gate /* 17550Sstevel@tonic-gate * Pre-fork: setup shared state 17560Sstevel@tonic-gate */ 17570Sstevel@tonic-gate if ((shstate = (void *)mmap(NULL, shstatelen, 17580Sstevel@tonic-gate PROT_READ|PROT_WRITE, MAP_SHARED|MAP_ANON, -1, (off_t)0)) == 17590Sstevel@tonic-gate MAP_FAILED) { 17600Sstevel@tonic-gate zerror(zlogp, B_TRUE, "%s failed", "mmap"); 17610Sstevel@tonic-gate return (1); 17620Sstevel@tonic-gate } 17630Sstevel@tonic-gate if (sema_init(&shstate->sem, 0, USYNC_PROCESS, NULL) != 0) { 17640Sstevel@tonic-gate zerror(zlogp, B_TRUE, "%s failed", "sema_init()"); 17650Sstevel@tonic-gate (void) munmap((char *)shstate, shstatelen); 17660Sstevel@tonic-gate return (1); 17670Sstevel@tonic-gate } 17680Sstevel@tonic-gate shstate->log.logfile = NULL; 17690Sstevel@tonic-gate shstate->log.buflen = shstatelen - sizeof (*shstate); 17700Sstevel@tonic-gate shstate->log.loglen = shstate->log.buflen; 17710Sstevel@tonic-gate shstate->log.buf = (char *)shstate + sizeof (*shstate); 17720Sstevel@tonic-gate shstate->log.log = shstate->log.buf; 17730Sstevel@tonic-gate shstate->log.locale = parents_locale; 17740Sstevel@tonic-gate shstate->status = -1; 17750Sstevel@tonic-gate 17760Sstevel@tonic-gate /* 17770Sstevel@tonic-gate * We need a SIGCHLD handler so the sema_wait() below will wake 17780Sstevel@tonic-gate * up if the child dies without doing a sema_post(). 17790Sstevel@tonic-gate */ 17800Sstevel@tonic-gate (void) sigset(SIGCHLD, sigchld); 17810Sstevel@tonic-gate /* 17820Sstevel@tonic-gate * We must mask SIGCHLD until after we've coped with the fork 17830Sstevel@tonic-gate * sufficiently to deal with it; otherwise we can race and 17840Sstevel@tonic-gate * receive the signal before pid has been initialized 17850Sstevel@tonic-gate * (yes, this really happens). 17860Sstevel@tonic-gate */ 17870Sstevel@tonic-gate (void) sigemptyset(&block_cld); 17880Sstevel@tonic-gate (void) sigaddset(&block_cld, SIGCHLD); 17890Sstevel@tonic-gate (void) sigprocmask(SIG_BLOCK, &block_cld, NULL); 17900Sstevel@tonic-gate 17912382Sdp if ((ctfd = init_template()) == -1) { 17922382Sdp zerror(zlogp, B_TRUE, "failed to create contract"); 17932382Sdp return (1); 17942382Sdp } 17952382Sdp 17960Sstevel@tonic-gate /* 17970Sstevel@tonic-gate * Do not let another thread localize a message while we are forking. 17980Sstevel@tonic-gate */ 17990Sstevel@tonic-gate (void) mutex_lock(&msglock); 18000Sstevel@tonic-gate pid = fork(); 18010Sstevel@tonic-gate (void) mutex_unlock(&msglock); 18022382Sdp 18032382Sdp /* 18042382Sdp * In all cases (parent, child, and in the event of an error) we 18052382Sdp * don't want to cause creation of contracts on subsequent fork()s. 18062382Sdp */ 18072382Sdp (void) ct_tmpl_clear(ctfd); 18082382Sdp (void) close(ctfd); 18092382Sdp 18100Sstevel@tonic-gate if (pid == -1) { 18110Sstevel@tonic-gate zerror(zlogp, B_TRUE, "could not fork"); 18120Sstevel@tonic-gate return (1); 18130Sstevel@tonic-gate 18140Sstevel@tonic-gate } else if (pid > 0) { /* parent */ 18150Sstevel@tonic-gate (void) sigprocmask(SIG_UNBLOCK, &block_cld, NULL); 18160Sstevel@tonic-gate /* 18170Sstevel@tonic-gate * This marks a window of vulnerability in which we receive 18180Sstevel@tonic-gate * the SIGCLD before falling into sema_wait (normally we would 18190Sstevel@tonic-gate * get woken up from sema_wait with EINTR upon receipt of 18200Sstevel@tonic-gate * SIGCLD). So we may need to use some other scheme like 18210Sstevel@tonic-gate * sema_posting in the sigcld handler. 18220Sstevel@tonic-gate * blech 18230Sstevel@tonic-gate */ 18240Sstevel@tonic-gate (void) sema_wait(&shstate->sem); 18250Sstevel@tonic-gate (void) sema_destroy(&shstate->sem); 18260Sstevel@tonic-gate if (shstate->status != 0) 18270Sstevel@tonic-gate (void) waitpid(pid, NULL, WNOHANG); 18280Sstevel@tonic-gate /* 18290Sstevel@tonic-gate * It's ok if we die with SIGPIPE. It's not like we could have 18300Sstevel@tonic-gate * done anything about it. 18310Sstevel@tonic-gate */ 18320Sstevel@tonic-gate (void) fprintf(stderr, "%s", shstate->log.buf); 18330Sstevel@tonic-gate _exit(shstate->status == 0 ? 0 : 1); 18340Sstevel@tonic-gate } 18350Sstevel@tonic-gate 18360Sstevel@tonic-gate /* 18370Sstevel@tonic-gate * The child charges on. 18380Sstevel@tonic-gate */ 18390Sstevel@tonic-gate (void) sigset(SIGCHLD, SIG_DFL); 18400Sstevel@tonic-gate (void) sigprocmask(SIG_UNBLOCK, &block_cld, NULL); 18410Sstevel@tonic-gate 18420Sstevel@tonic-gate /* 18430Sstevel@tonic-gate * SIGPIPE can be delivered if we write to a socket for which the 18440Sstevel@tonic-gate * peer endpoint is gone. That can lead to too-early termination 18450Sstevel@tonic-gate * of zoneadmd, and that's not good eats. 18460Sstevel@tonic-gate */ 18470Sstevel@tonic-gate (void) sigset(SIGPIPE, SIG_IGN); 18480Sstevel@tonic-gate /* 18490Sstevel@tonic-gate * Stop using stderr 18500Sstevel@tonic-gate */ 18510Sstevel@tonic-gate zlogp = &shstate->log; 18520Sstevel@tonic-gate 18530Sstevel@tonic-gate /* 18540Sstevel@tonic-gate * We don't need stdout/stderr from now on. 18550Sstevel@tonic-gate */ 18560Sstevel@tonic-gate closefrom(0); 18570Sstevel@tonic-gate 18580Sstevel@tonic-gate /* 18590Sstevel@tonic-gate * Initialize the syslog zlog_t. This needs to be done after 18600Sstevel@tonic-gate * the call to closefrom(). 18610Sstevel@tonic-gate */ 18620Sstevel@tonic-gate logsys.buf = logsys.log = NULL; 18630Sstevel@tonic-gate logsys.buflen = logsys.loglen = 0; 18640Sstevel@tonic-gate logsys.logfile = NULL; 18650Sstevel@tonic-gate logsys.locale = DEFAULT_LOCALE; 18660Sstevel@tonic-gate 18670Sstevel@tonic-gate openlog("zoneadmd", LOG_PID, LOG_DAEMON); 18680Sstevel@tonic-gate 18690Sstevel@tonic-gate /* 18700Sstevel@tonic-gate * The eventstream is used to publish state changes in the zone 18710Sstevel@tonic-gate * from the door threads to the console I/O poller. 18720Sstevel@tonic-gate */ 18730Sstevel@tonic-gate if (eventstream_init() == -1) { 18740Sstevel@tonic-gate zerror(zlogp, B_TRUE, "unable to create eventstream"); 18750Sstevel@tonic-gate goto child_out; 18760Sstevel@tonic-gate } 18770Sstevel@tonic-gate 18780Sstevel@tonic-gate (void) snprintf(zone_door_path, sizeof (zone_door_path), 1879766Scarlsonj "%s" ZONE_DOOR_PATH, zonecfg_get_root(), zone_name); 18800Sstevel@tonic-gate 18810Sstevel@tonic-gate /* 18820Sstevel@tonic-gate * See if another zoneadmd is running for this zone. If not, then we 18830Sstevel@tonic-gate * can now modify system state. 18840Sstevel@tonic-gate */ 18850Sstevel@tonic-gate if (make_daemon_exclusive(zlogp) == -1) 18860Sstevel@tonic-gate goto child_out; 18870Sstevel@tonic-gate 18880Sstevel@tonic-gate 18890Sstevel@tonic-gate /* 18900Sstevel@tonic-gate * Create/join a new session; we need to be careful of what we do with 18910Sstevel@tonic-gate * the console from now on so we don't end up being the session leader 18920Sstevel@tonic-gate * for the terminal we're going to be handing out. 18930Sstevel@tonic-gate */ 18940Sstevel@tonic-gate (void) setsid(); 18950Sstevel@tonic-gate 18960Sstevel@tonic-gate /* 18970Sstevel@tonic-gate * This thread shouldn't be receiving any signals; in particular, 18980Sstevel@tonic-gate * SIGCHLD should be received by the thread doing the fork(). 18990Sstevel@tonic-gate */ 19000Sstevel@tonic-gate (void) sigfillset(&blockset); 19010Sstevel@tonic-gate (void) thr_sigsetmask(SIG_BLOCK, &blockset, NULL); 19020Sstevel@tonic-gate 19030Sstevel@tonic-gate /* 19040Sstevel@tonic-gate * Setup the console device and get ready to serve the console; 19050Sstevel@tonic-gate * once this has completed, we're ready to let console clients 19060Sstevel@tonic-gate * make an attempt to connect (they will block until 19070Sstevel@tonic-gate * serve_console_sock() below gets called, and any pending 19080Sstevel@tonic-gate * connection is accept()ed). 19090Sstevel@tonic-gate */ 1910766Scarlsonj if (!zonecfg_in_alt_root() && init_console(zlogp) == -1) 19110Sstevel@tonic-gate goto child_out; 19120Sstevel@tonic-gate 19130Sstevel@tonic-gate /* 19140Sstevel@tonic-gate * Take the lock now, so that when the door server gets going, we 19150Sstevel@tonic-gate * are guaranteed that it won't take a request until we are sure 19160Sstevel@tonic-gate * that everything is completely set up. See the child_out: label 19170Sstevel@tonic-gate * below to see why this matters. 19180Sstevel@tonic-gate */ 19190Sstevel@tonic-gate (void) mutex_lock(&lock); 19200Sstevel@tonic-gate 1921766Scarlsonj /* Init semaphore for scratch zones. */ 1922766Scarlsonj if (sema_init(&scratch_sem, 0, USYNC_THREAD, NULL) == -1) { 1923766Scarlsonj zerror(zlogp, B_TRUE, 1924766Scarlsonj "failed to initialize semaphore for scratch zone"); 1925766Scarlsonj goto child_out; 1926766Scarlsonj } 1927766Scarlsonj 19280Sstevel@tonic-gate /* 19290Sstevel@tonic-gate * Note: door setup must occur *after* the console is setup. 19300Sstevel@tonic-gate * This is so that as zlogin tests the door to see if zoneadmd 19310Sstevel@tonic-gate * is ready yet, we know that the console will get serviced 19320Sstevel@tonic-gate * once door_info() indicates that the door is "up". 19330Sstevel@tonic-gate */ 19340Sstevel@tonic-gate if (setup_door(zlogp) == -1) 19350Sstevel@tonic-gate goto child_out; 19360Sstevel@tonic-gate 19370Sstevel@tonic-gate /* 19380Sstevel@tonic-gate * Things seem OK so far; tell the parent process that we're done 19390Sstevel@tonic-gate * with setup tasks. This will cause the parent to exit, signalling 19400Sstevel@tonic-gate * to zoneadm, zlogin, or whatever forked it that we are ready to 19410Sstevel@tonic-gate * service requests. 19420Sstevel@tonic-gate */ 19430Sstevel@tonic-gate shstate->status = 0; 19440Sstevel@tonic-gate (void) sema_post(&shstate->sem); 19450Sstevel@tonic-gate (void) munmap((char *)shstate, shstatelen); 19460Sstevel@tonic-gate shstate = NULL; 19470Sstevel@tonic-gate 19480Sstevel@tonic-gate (void) mutex_unlock(&lock); 19490Sstevel@tonic-gate 19500Sstevel@tonic-gate /* 19510Sstevel@tonic-gate * zlogp is now invalid, so reset it to the syslog logger. 19520Sstevel@tonic-gate */ 19530Sstevel@tonic-gate zlogp = &logsys; 19540Sstevel@tonic-gate 19550Sstevel@tonic-gate /* 19560Sstevel@tonic-gate * Now that we are free of any parents, switch to the default locale. 19570Sstevel@tonic-gate */ 19580Sstevel@tonic-gate (void) setlocale(LC_ALL, DEFAULT_LOCALE); 19590Sstevel@tonic-gate 19600Sstevel@tonic-gate /* 19610Sstevel@tonic-gate * At this point the setup portion of main() is basically done, so 19620Sstevel@tonic-gate * we reuse this thread to manage the zone console. When 19630Sstevel@tonic-gate * serve_console() has returned, we are past the point of no return 19640Sstevel@tonic-gate * in the life of this zoneadmd. 19650Sstevel@tonic-gate */ 1966766Scarlsonj if (zonecfg_in_alt_root()) { 1967766Scarlsonj /* 1968766Scarlsonj * This is just awful, but mounted scratch zones don't (and 1969766Scarlsonj * can't) have consoles. We just wait for unmount instead. 1970766Scarlsonj */ 1971766Scarlsonj while (sema_wait(&scratch_sem) == EINTR) 1972766Scarlsonj ; 1973766Scarlsonj } else { 1974766Scarlsonj serve_console(zlogp); 1975766Scarlsonj assert(in_death_throes); 1976766Scarlsonj } 19770Sstevel@tonic-gate 19780Sstevel@tonic-gate /* 19790Sstevel@tonic-gate * This is the next-to-last part of the exit interlock. Upon calling 19800Sstevel@tonic-gate * fdetach(), the door will go unreferenced; once any 19810Sstevel@tonic-gate * outstanding requests (like the door thread doing Z_HALT) are 19820Sstevel@tonic-gate * done, the door will get an UNREF notification; when it handles 19830Sstevel@tonic-gate * the UNREF, the door server will cause the exit. 19840Sstevel@tonic-gate */ 19850Sstevel@tonic-gate assert(!MUTEX_HELD(&lock)); 19860Sstevel@tonic-gate (void) fdetach(zone_door_path); 19870Sstevel@tonic-gate for (;;) 19880Sstevel@tonic-gate (void) pause(); 19890Sstevel@tonic-gate 19900Sstevel@tonic-gate child_out: 19910Sstevel@tonic-gate assert(pid == 0); 19920Sstevel@tonic-gate if (shstate != NULL) { 19930Sstevel@tonic-gate shstate->status = -1; 19940Sstevel@tonic-gate (void) sema_post(&shstate->sem); 19950Sstevel@tonic-gate (void) munmap((char *)shstate, shstatelen); 19960Sstevel@tonic-gate } 19970Sstevel@tonic-gate 19980Sstevel@tonic-gate /* 19990Sstevel@tonic-gate * This might trigger an unref notification, but if so, 20000Sstevel@tonic-gate * we are still holding the lock, so our call to exit will 20010Sstevel@tonic-gate * ultimately win the race and will publish the right exit 20020Sstevel@tonic-gate * code. 20030Sstevel@tonic-gate */ 20040Sstevel@tonic-gate if (zone_door != -1) { 20050Sstevel@tonic-gate assert(MUTEX_HELD(&lock)); 20060Sstevel@tonic-gate (void) door_revoke(zone_door); 20070Sstevel@tonic-gate (void) fdetach(zone_door_path); 20080Sstevel@tonic-gate } 20090Sstevel@tonic-gate return (1); /* return from main() forcibly exits an MT process */ 20100Sstevel@tonic-gate } 2011