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 /* 238770SJordan.Vaughan@Sun.com * Copyright 2009 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> 957655Sgerald.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; 1098905SRic.Aleshire@Sun.COM boolean_t zone_islabeled; 110766Scarlsonj static zoneid_t zone_id; 1118453SAnurag.Maskey@Sun.COM dladm_handle_t dld_handle = NULL; 1120Sstevel@tonic-gate 1137370S<Gerald Jelinek> static char pre_statechg_hook[2 * MAXPATHLEN]; 1147370S<Gerald Jelinek> static char post_statechg_hook[2 * MAXPATHLEN]; 1157370S<Gerald Jelinek> char query_hook[2 * MAXPATHLEN]; 1167370S<Gerald Jelinek> 1172611Svp157776 zlog_t logsys; 1180Sstevel@tonic-gate 1190Sstevel@tonic-gate mutex_t lock = DEFAULTMUTEX; /* to serialize stuff */ 1200Sstevel@tonic-gate mutex_t msglock = DEFAULTMUTEX; /* for calling setlocale() */ 1210Sstevel@tonic-gate 122766Scarlsonj static sema_t scratch_sem; /* for scratch zones */ 123766Scarlsonj 1240Sstevel@tonic-gate static char zone_door_path[MAXPATHLEN]; 1250Sstevel@tonic-gate static int zone_door = -1; 1260Sstevel@tonic-gate 1270Sstevel@tonic-gate boolean_t in_death_throes = B_FALSE; /* daemon is dying */ 1280Sstevel@tonic-gate boolean_t bringup_failure_recovery = B_FALSE; /* ignore certain failures */ 1290Sstevel@tonic-gate 1300Sstevel@tonic-gate #if !defined(TEXT_DOMAIN) /* should be defined by cc -D */ 1310Sstevel@tonic-gate #define TEXT_DOMAIN "SYS_TEST" /* Use this only if it wasn't */ 1320Sstevel@tonic-gate #endif 1330Sstevel@tonic-gate 1340Sstevel@tonic-gate #define DEFAULT_LOCALE "C" 1350Sstevel@tonic-gate 136766Scarlsonj static const char * 137766Scarlsonj z_cmd_name(zone_cmd_t zcmd) 138766Scarlsonj { 139766Scarlsonj /* This list needs to match the enum in sys/zone.h */ 140766Scarlsonj static const char *zcmdstr[] = { 1412712Snn35248 "ready", "boot", "forceboot", "reboot", "halt", 1422712Snn35248 "note_uninstalling", "mount", "forcemount", "unmount" 143766Scarlsonj }; 144766Scarlsonj 145766Scarlsonj if (zcmd >= sizeof (zcmdstr) / sizeof (*zcmdstr)) 146766Scarlsonj return ("unknown"); 147766Scarlsonj else 148766Scarlsonj return (zcmdstr[(int)zcmd]); 149766Scarlsonj } 150766Scarlsonj 1510Sstevel@tonic-gate static char * 1520Sstevel@tonic-gate get_execbasename(char *execfullname) 1530Sstevel@tonic-gate { 1540Sstevel@tonic-gate char *last_slash, *execbasename; 1550Sstevel@tonic-gate 1560Sstevel@tonic-gate /* guard against '/' at end of command invocation */ 1570Sstevel@tonic-gate for (;;) { 1580Sstevel@tonic-gate last_slash = strrchr(execfullname, '/'); 1590Sstevel@tonic-gate if (last_slash == NULL) { 1600Sstevel@tonic-gate execbasename = execfullname; 1610Sstevel@tonic-gate break; 1620Sstevel@tonic-gate } else { 1630Sstevel@tonic-gate execbasename = last_slash + 1; 1640Sstevel@tonic-gate if (*execbasename == '\0') { 1650Sstevel@tonic-gate *last_slash = '\0'; 1660Sstevel@tonic-gate continue; 1670Sstevel@tonic-gate } 1680Sstevel@tonic-gate break; 1690Sstevel@tonic-gate } 1700Sstevel@tonic-gate } 1710Sstevel@tonic-gate return (execbasename); 1720Sstevel@tonic-gate } 1730Sstevel@tonic-gate 1740Sstevel@tonic-gate static void 1750Sstevel@tonic-gate usage(void) 1760Sstevel@tonic-gate { 1770Sstevel@tonic-gate (void) fprintf(stderr, gettext("Usage: %s -z zonename\n"), progname); 1780Sstevel@tonic-gate (void) fprintf(stderr, 1790Sstevel@tonic-gate gettext("\tNote: %s should not be run directly.\n"), progname); 1800Sstevel@tonic-gate exit(2); 1810Sstevel@tonic-gate } 1820Sstevel@tonic-gate 1830Sstevel@tonic-gate /* ARGSUSED */ 1840Sstevel@tonic-gate static void 1850Sstevel@tonic-gate sigchld(int sig) 1860Sstevel@tonic-gate { 1870Sstevel@tonic-gate } 1880Sstevel@tonic-gate 1890Sstevel@tonic-gate char * 1900Sstevel@tonic-gate localize_msg(char *locale, const char *msg) 1910Sstevel@tonic-gate { 1920Sstevel@tonic-gate char *out; 1930Sstevel@tonic-gate 1940Sstevel@tonic-gate (void) mutex_lock(&msglock); 1950Sstevel@tonic-gate (void) setlocale(LC_MESSAGES, locale); 1960Sstevel@tonic-gate out = gettext(msg); 1970Sstevel@tonic-gate (void) setlocale(LC_MESSAGES, DEFAULT_LOCALE); 1980Sstevel@tonic-gate (void) mutex_unlock(&msglock); 1990Sstevel@tonic-gate return (out); 2000Sstevel@tonic-gate } 2010Sstevel@tonic-gate 2020Sstevel@tonic-gate /* PRINTFLIKE3 */ 2030Sstevel@tonic-gate void 2040Sstevel@tonic-gate zerror(zlog_t *zlogp, boolean_t use_strerror, const char *fmt, ...) 2050Sstevel@tonic-gate { 2060Sstevel@tonic-gate va_list alist; 2070Sstevel@tonic-gate char buf[MAXPATHLEN * 2]; /* enough space for err msg with a path */ 2080Sstevel@tonic-gate char *bp; 2090Sstevel@tonic-gate int saved_errno = errno; 2100Sstevel@tonic-gate 2110Sstevel@tonic-gate if (zlogp == NULL) 2120Sstevel@tonic-gate return; 2130Sstevel@tonic-gate if (zlogp == &logsys) 2140Sstevel@tonic-gate (void) snprintf(buf, sizeof (buf), "[zone '%s'] ", 2150Sstevel@tonic-gate zone_name); 2160Sstevel@tonic-gate else 2170Sstevel@tonic-gate buf[0] = '\0'; 2180Sstevel@tonic-gate bp = &(buf[strlen(buf)]); 2190Sstevel@tonic-gate 2200Sstevel@tonic-gate /* 2210Sstevel@tonic-gate * In theory, the locale pointer should be set to either "C" or a 2220Sstevel@tonic-gate * char array, so it should never be NULL 2230Sstevel@tonic-gate */ 2240Sstevel@tonic-gate assert(zlogp->locale != NULL); 2250Sstevel@tonic-gate /* Locale is per process, but we are multi-threaded... */ 2260Sstevel@tonic-gate fmt = localize_msg(zlogp->locale, fmt); 2270Sstevel@tonic-gate 2280Sstevel@tonic-gate va_start(alist, fmt); 2290Sstevel@tonic-gate (void) vsnprintf(bp, sizeof (buf) - (bp - buf), fmt, alist); 2300Sstevel@tonic-gate va_end(alist); 2310Sstevel@tonic-gate bp = &(buf[strlen(buf)]); 2320Sstevel@tonic-gate if (use_strerror) 2330Sstevel@tonic-gate (void) snprintf(bp, sizeof (buf) - (bp - buf), ": %s", 2340Sstevel@tonic-gate strerror(saved_errno)); 2350Sstevel@tonic-gate if (zlogp == &logsys) { 2360Sstevel@tonic-gate (void) syslog(LOG_ERR, "%s", buf); 2370Sstevel@tonic-gate } else if (zlogp->logfile != NULL) { 2380Sstevel@tonic-gate (void) fprintf(zlogp->logfile, "%s\n", buf); 2390Sstevel@tonic-gate } else { 2400Sstevel@tonic-gate size_t buflen; 2410Sstevel@tonic-gate size_t copylen; 2420Sstevel@tonic-gate 2430Sstevel@tonic-gate buflen = snprintf(zlogp->log, zlogp->loglen, "%s\n", buf); 2440Sstevel@tonic-gate copylen = MIN(buflen, zlogp->loglen); 2450Sstevel@tonic-gate zlogp->log += copylen; 2460Sstevel@tonic-gate zlogp->loglen -= copylen; 2470Sstevel@tonic-gate } 2480Sstevel@tonic-gate } 2490Sstevel@tonic-gate 2502267Sdp /* 2512267Sdp * Emit a warning for any boot arguments which are unrecognized. Since 2522267Sdp * Solaris boot arguments are getopt(3c) compatible (see kernel(1m)), we 2532267Sdp * put the arguments into an argv style array, use getopt to process them, 2542267Sdp * and put the resultant argument string back into outargs. 2552267Sdp * 2562267Sdp * During the filtering, we pull out any arguments which are truly "boot" 2572267Sdp * arguments, leaving only those which are to be passed intact to the 2582267Sdp * progenitor process. The one we support at the moment is -i, which 2592267Sdp * indicates to the kernel which program should be launched as 'init'. 2602267Sdp * 2612267Sdp * A return of Z_INVAL indicates specifically that the arguments are 2622267Sdp * not valid; this is a non-fatal error. Except for Z_OK, all other return 2632267Sdp * values are treated as fatal. 2642267Sdp */ 2652267Sdp static int 2662267Sdp filter_bootargs(zlog_t *zlogp, const char *inargs, char *outargs, 2672267Sdp char *init_file, char *badarg) 2682267Sdp { 2692267Sdp int argc = 0, argc_save; 2702267Sdp int i; 2712267Sdp int err; 2722267Sdp char *arg, *lasts, **argv = NULL, **argv_save; 2732267Sdp char zonecfg_args[BOOTARGS_MAX]; 2742267Sdp char scratchargs[BOOTARGS_MAX], *sargs; 2752267Sdp char c; 2762267Sdp 2772267Sdp bzero(outargs, BOOTARGS_MAX); 2782267Sdp bzero(badarg, BOOTARGS_MAX); 2792267Sdp 2802267Sdp /* 2812267Sdp * If the user didn't specify transient boot arguments, check 2822267Sdp * to see if there were any specified in the zone configuration, 2832267Sdp * and use them if applicable. 2842267Sdp */ 2852267Sdp if (inargs == NULL || inargs[0] == '\0') { 2862267Sdp zone_dochandle_t handle; 2872267Sdp if ((handle = zonecfg_init_handle()) == NULL) { 2882267Sdp zerror(zlogp, B_TRUE, 2892267Sdp "getting zone configuration handle"); 2902267Sdp return (Z_BAD_HANDLE); 2912267Sdp } 2922267Sdp err = zonecfg_get_snapshot_handle(zone_name, handle); 2932267Sdp if (err != Z_OK) { 2942267Sdp zerror(zlogp, B_FALSE, 2952267Sdp "invalid configuration snapshot"); 2962267Sdp zonecfg_fini_handle(handle); 2972267Sdp return (Z_BAD_HANDLE); 2982267Sdp } 2992267Sdp 3002267Sdp bzero(zonecfg_args, sizeof (zonecfg_args)); 3012267Sdp (void) zonecfg_get_bootargs(handle, zonecfg_args, 3022267Sdp sizeof (zonecfg_args)); 3032267Sdp inargs = zonecfg_args; 3042267Sdp zonecfg_fini_handle(handle); 3052267Sdp } 3062267Sdp 3072267Sdp if (strlen(inargs) >= BOOTARGS_MAX) { 3082267Sdp zerror(zlogp, B_FALSE, "boot argument string too long"); 3092267Sdp return (Z_INVAL); 3102267Sdp } 3112267Sdp 3122267Sdp (void) strlcpy(scratchargs, inargs, sizeof (scratchargs)); 3132267Sdp sargs = scratchargs; 3142267Sdp while ((arg = strtok_r(sargs, " \t", &lasts)) != NULL) { 3152267Sdp sargs = NULL; 3162267Sdp argc++; 3172267Sdp } 3182267Sdp 3192267Sdp if ((argv = calloc(argc + 1, sizeof (char *))) == NULL) { 3202267Sdp zerror(zlogp, B_FALSE, "memory allocation failed"); 3212267Sdp return (Z_NOMEM); 3222267Sdp } 3232267Sdp 3242267Sdp argv_save = argv; 3252267Sdp argc_save = argc; 3262267Sdp 3272267Sdp (void) strlcpy(scratchargs, inargs, sizeof (scratchargs)); 3282267Sdp sargs = scratchargs; 3292267Sdp i = 0; 3302267Sdp while ((arg = strtok_r(sargs, " \t", &lasts)) != NULL) { 3312267Sdp sargs = NULL; 3322267Sdp if ((argv[i] = strdup(arg)) == NULL) { 3332267Sdp err = Z_NOMEM; 3342267Sdp zerror(zlogp, B_FALSE, "memory allocation failed"); 3352267Sdp goto done; 3362267Sdp } 3372267Sdp i++; 3382267Sdp } 3392267Sdp 3402267Sdp /* 3412267Sdp * We preserve compatibility with the Solaris system boot behavior, 3422267Sdp * which allows: 3432267Sdp * 3442267Sdp * # reboot kernel/unix -s -m verbose 3452267Sdp * 3462267Sdp * In this example, kernel/unix tells the booter what file to 3472267Sdp * boot. We don't want reboot in a zone to be gratuitously different, 3482267Sdp * so we silently ignore the boot file, if necessary. 3492267Sdp */ 3502267Sdp if (argv[0] == NULL) 3512267Sdp goto done; 3522267Sdp 3532267Sdp assert(argv[0][0] != ' '); 3542267Sdp assert(argv[0][0] != '\t'); 3552267Sdp 3562267Sdp if (argv[0][0] != '-' && argv[0][0] != '\0') { 3572267Sdp argv = &argv[1]; 3582267Sdp argc--; 3592267Sdp } 3602267Sdp 3612267Sdp optind = 0; 3622267Sdp opterr = 0; 3632267Sdp err = Z_OK; 3642712Snn35248 while ((c = getopt(argc, argv, "fi:m:s")) != -1) { 3652267Sdp switch (c) { 3662267Sdp case 'i': 3672267Sdp /* 3682267Sdp * -i is handled by the runtime and is not passed 3692267Sdp * along to userland 3702267Sdp */ 3712267Sdp (void) strlcpy(init_file, optarg, MAXPATHLEN); 3722267Sdp break; 3732712Snn35248 case 'f': 3742712Snn35248 /* This has already been processed by zoneadm */ 3752712Snn35248 break; 3762267Sdp case 'm': 3772267Sdp case 's': 3782267Sdp /* These pass through unmolested */ 3792267Sdp (void) snprintf(outargs, BOOTARGS_MAX, 3802267Sdp "%s -%c %s ", outargs, c, optarg ? optarg : ""); 3812267Sdp break; 3822267Sdp case '?': 3832267Sdp /* 3842267Sdp * We warn about unknown arguments but pass them 3852267Sdp * along anyway-- if someone wants to develop their 3862267Sdp * own init replacement, they can pass it whatever 3872267Sdp * args they want. 3882267Sdp */ 3892267Sdp err = Z_INVAL; 3902267Sdp (void) snprintf(outargs, BOOTARGS_MAX, 3912267Sdp "%s -%c", outargs, optopt); 3922267Sdp (void) snprintf(badarg, BOOTARGS_MAX, 3932267Sdp "%s -%c", badarg, optopt); 3942267Sdp break; 3952267Sdp } 3962267Sdp } 3972267Sdp 3982267Sdp /* 3992267Sdp * For Solaris Zones we warn about and discard non-option arguments. 4002267Sdp * Hence 'boot foo bar baz gub' --> 'boot'. However, to be similar 4012267Sdp * to the kernel, we concat up all the other remaining boot args. 4022267Sdp * and warn on them as a group. 4032267Sdp */ 4042267Sdp if (optind < argc) { 4052267Sdp err = Z_INVAL; 4062267Sdp while (optind < argc) { 4072267Sdp (void) snprintf(badarg, BOOTARGS_MAX, "%s%s%s", 4082267Sdp badarg, strlen(badarg) > 0 ? " " : "", 4092267Sdp argv[optind]); 4102267Sdp optind++; 4112267Sdp } 4122267Sdp zerror(zlogp, B_FALSE, "WARNING: Unused or invalid boot " 4132267Sdp "arguments `%s'.", badarg); 4142267Sdp } 4152267Sdp 4162267Sdp done: 4172267Sdp for (i = 0; i < argc_save; i++) { 4182267Sdp if (argv_save[i] != NULL) 4192267Sdp free(argv_save[i]); 4202267Sdp } 4212267Sdp free(argv_save); 4222267Sdp return (err); 4232267Sdp } 4242267Sdp 4252267Sdp 4260Sstevel@tonic-gate static int 4270Sstevel@tonic-gate mkzonedir(zlog_t *zlogp) 4280Sstevel@tonic-gate { 4290Sstevel@tonic-gate struct stat st; 4300Sstevel@tonic-gate /* 4310Sstevel@tonic-gate * We must create and lock everyone but root out of ZONES_TMPDIR 4320Sstevel@tonic-gate * since anyone can open any UNIX domain socket, regardless of 4330Sstevel@tonic-gate * its file system permissions. Sigh... 4340Sstevel@tonic-gate */ 4350Sstevel@tonic-gate if (mkdir(ZONES_TMPDIR, S_IRWXU) < 0 && errno != EEXIST) { 4360Sstevel@tonic-gate zerror(zlogp, B_TRUE, "could not mkdir '%s'", ZONES_TMPDIR); 4370Sstevel@tonic-gate return (-1); 4380Sstevel@tonic-gate } 4390Sstevel@tonic-gate /* paranoia */ 440871Scasper if ((stat(ZONES_TMPDIR, &st) < 0) || !S_ISDIR(st.st_mode)) { 4410Sstevel@tonic-gate zerror(zlogp, B_TRUE, "'%s' is not a directory", ZONES_TMPDIR); 4420Sstevel@tonic-gate return (-1); 4430Sstevel@tonic-gate } 4440Sstevel@tonic-gate (void) chmod(ZONES_TMPDIR, S_IRWXU); 4450Sstevel@tonic-gate return (0); 4460Sstevel@tonic-gate } 4470Sstevel@tonic-gate 448766Scarlsonj /* 4497370S<Gerald Jelinek> * Run the brand's pre-state change callback, if it exists. 4507370S<Gerald Jelinek> */ 4517370S<Gerald Jelinek> static int 4527370S<Gerald Jelinek> brand_prestatechg(zlog_t *zlogp, int state, int cmd) 4537370S<Gerald Jelinek> { 4547370S<Gerald Jelinek> char cmdbuf[2 * MAXPATHLEN]; 4557370S<Gerald Jelinek> 4567370S<Gerald Jelinek> if (pre_statechg_hook[0] == '\0') 4577370S<Gerald Jelinek> return (0); 4587370S<Gerald Jelinek> 4597370S<Gerald Jelinek> if (snprintf(cmdbuf, sizeof (cmdbuf), "%s %d %d", pre_statechg_hook, 4607370S<Gerald Jelinek> state, cmd) > sizeof (cmdbuf)) 4617370S<Gerald Jelinek> return (-1); 4627370S<Gerald Jelinek> 4637370S<Gerald Jelinek> if (do_subproc(zlogp, cmdbuf, NULL) != 0) 4647370S<Gerald Jelinek> return (-1); 4657370S<Gerald Jelinek> 4667370S<Gerald Jelinek> return (0); 4677370S<Gerald Jelinek> } 4687370S<Gerald Jelinek> 4697370S<Gerald Jelinek> /* 4707370S<Gerald Jelinek> * Run the brand's post-state change callback, if it exists. 4717370S<Gerald Jelinek> */ 4727370S<Gerald Jelinek> static int 4737370S<Gerald Jelinek> brand_poststatechg(zlog_t *zlogp, int state, int cmd) 4747370S<Gerald Jelinek> { 4757370S<Gerald Jelinek> char cmdbuf[2 * MAXPATHLEN]; 4767370S<Gerald Jelinek> 4777370S<Gerald Jelinek> if (post_statechg_hook[0] == '\0') 4787370S<Gerald Jelinek> return (0); 4797370S<Gerald Jelinek> 4807370S<Gerald Jelinek> if (snprintf(cmdbuf, sizeof (cmdbuf), "%s %d %d", post_statechg_hook, 4817370S<Gerald Jelinek> state, cmd) > sizeof (cmdbuf)) 4827370S<Gerald Jelinek> return (-1); 4837370S<Gerald Jelinek> 4847370S<Gerald Jelinek> if (do_subproc(zlogp, cmdbuf, NULL) != 0) 4857370S<Gerald Jelinek> return (-1); 4867370S<Gerald Jelinek> 4877370S<Gerald Jelinek> return (0); 4887370S<Gerald Jelinek> } 4897370S<Gerald Jelinek> 4907370S<Gerald Jelinek> /* 491766Scarlsonj * Bring a zone up to the pre-boot "ready" stage. The mount_cmd argument is 492766Scarlsonj * 'true' if this is being invoked as part of the processing for the "mount" 493766Scarlsonj * subcommand. 494766Scarlsonj */ 495766Scarlsonj static int 4967370S<Gerald Jelinek> zone_ready(zlog_t *zlogp, zone_mnt_t mount_cmd, int zstate) 4970Sstevel@tonic-gate { 4980Sstevel@tonic-gate int err; 4990Sstevel@tonic-gate 5007370S<Gerald Jelinek> if (brand_prestatechg(zlogp, zstate, Z_READY) != 0) 5017370S<Gerald Jelinek> return (-1); 5027370S<Gerald Jelinek> 5030Sstevel@tonic-gate if ((err = zonecfg_create_snapshot(zone_name)) != Z_OK) { 5040Sstevel@tonic-gate zerror(zlogp, B_FALSE, "unable to create snapshot: %s", 5050Sstevel@tonic-gate zonecfg_strerror(err)); 506*8958Sdp@eng.sun.com goto bad; 5070Sstevel@tonic-gate } 5080Sstevel@tonic-gate 509766Scarlsonj if ((zone_id = vplat_create(zlogp, mount_cmd)) == -1) { 5100Sstevel@tonic-gate if ((err = zonecfg_destroy_snapshot(zone_name)) != Z_OK) 5110Sstevel@tonic-gate zerror(zlogp, B_FALSE, "destroying snapshot: %s", 5120Sstevel@tonic-gate zonecfg_strerror(err)); 513*8958Sdp@eng.sun.com goto bad; 5140Sstevel@tonic-gate } 5152303Scarlsonj if (vplat_bringup(zlogp, mount_cmd, zone_id) != 0) { 5160Sstevel@tonic-gate bringup_failure_recovery = B_TRUE; 5175829Sgjelinek (void) vplat_teardown(NULL, (mount_cmd != Z_MNT_BOOT), B_FALSE); 5180Sstevel@tonic-gate if ((err = zonecfg_destroy_snapshot(zone_name)) != Z_OK) 5190Sstevel@tonic-gate zerror(zlogp, B_FALSE, "destroying snapshot: %s", 5200Sstevel@tonic-gate zonecfg_strerror(err)); 521*8958Sdp@eng.sun.com goto bad; 5220Sstevel@tonic-gate } 5230Sstevel@tonic-gate 5247370S<Gerald Jelinek> if (brand_poststatechg(zlogp, zstate, Z_READY) != 0) 525*8958Sdp@eng.sun.com goto bad; 5267370S<Gerald Jelinek> 5270Sstevel@tonic-gate return (0); 528*8958Sdp@eng.sun.com 529*8958Sdp@eng.sun.com bad: 530*8958Sdp@eng.sun.com /* 531*8958Sdp@eng.sun.com * If something goes wrong, we up the zones's state to the target 532*8958Sdp@eng.sun.com * state, READY, and then invoke the hook as if we're halting. 533*8958Sdp@eng.sun.com */ 534*8958Sdp@eng.sun.com (void) brand_poststatechg(zlogp, ZONE_STATE_READY, Z_HALT); 535*8958Sdp@eng.sun.com return (-1); 5360Sstevel@tonic-gate } 5370Sstevel@tonic-gate 5382303Scarlsonj int 5392303Scarlsonj init_template(void) 5400Sstevel@tonic-gate { 5410Sstevel@tonic-gate int fd; 5420Sstevel@tonic-gate int err = 0; 5430Sstevel@tonic-gate 5440Sstevel@tonic-gate fd = open64(CTFS_ROOT "/process/template", O_RDWR); 5450Sstevel@tonic-gate if (fd == -1) 5460Sstevel@tonic-gate return (-1); 5470Sstevel@tonic-gate 5480Sstevel@tonic-gate /* 5490Sstevel@tonic-gate * For now, zoneadmd doesn't do anything with the contract. 5500Sstevel@tonic-gate * Deliver no events, don't inherit, and allow it to be orphaned. 5510Sstevel@tonic-gate */ 5520Sstevel@tonic-gate err |= ct_tmpl_set_critical(fd, 0); 5530Sstevel@tonic-gate err |= ct_tmpl_set_informative(fd, 0); 5540Sstevel@tonic-gate err |= ct_pr_tmpl_set_fatal(fd, CT_PR_EV_HWERR); 5550Sstevel@tonic-gate err |= ct_pr_tmpl_set_param(fd, CT_PR_PGRPONLY | CT_PR_REGENT); 5560Sstevel@tonic-gate if (err || ct_tmpl_activate(fd)) { 5570Sstevel@tonic-gate (void) close(fd); 5580Sstevel@tonic-gate return (-1); 5590Sstevel@tonic-gate } 5600Sstevel@tonic-gate 5610Sstevel@tonic-gate return (fd); 5620Sstevel@tonic-gate } 5630Sstevel@tonic-gate 5642712Snn35248 typedef struct fs_callback { 5652712Snn35248 zlog_t *zlogp; 5662712Snn35248 zoneid_t zoneid; 5675576Sedp boolean_t mount_cmd; 5682712Snn35248 } fs_callback_t; 5692712Snn35248 5700Sstevel@tonic-gate static int 5712712Snn35248 mount_early_fs(void *data, const char *spec, const char *dir, 5722712Snn35248 const char *fstype, const char *opt) 5730Sstevel@tonic-gate { 5742712Snn35248 zlog_t *zlogp = ((fs_callback_t *)data)->zlogp; 5752712Snn35248 zoneid_t zoneid = ((fs_callback_t *)data)->zoneid; 5765576Sedp boolean_t mount_cmd = ((fs_callback_t *)data)->mount_cmd; 5775182Sedp char rootpath[MAXPATHLEN]; 5780Sstevel@tonic-gate pid_t child; 5790Sstevel@tonic-gate int child_status; 5800Sstevel@tonic-gate int tmpl_fd; 5815182Sedp int rv; 5820Sstevel@tonic-gate ctid_t ct; 5830Sstevel@tonic-gate 5845576Sedp /* determine the zone rootpath */ 5855576Sedp if (mount_cmd) { 5865576Sedp char zonepath[MAXPATHLEN]; 5875576Sedp char luroot[MAXPATHLEN]; 5885576Sedp 5895576Sedp if (zone_get_zonepath(zone_name, 5905576Sedp zonepath, sizeof (zonepath)) != Z_OK) { 5915576Sedp zerror(zlogp, B_FALSE, "unable to determine zone path"); 5925576Sedp return (-1); 5935576Sedp } 5945576Sedp 5955576Sedp (void) snprintf(luroot, sizeof (luroot), "%s/lu", zonepath); 5965576Sedp resolve_lofs(zlogp, luroot, sizeof (luroot)); 5975576Sedp (void) strlcpy(rootpath, luroot, sizeof (rootpath)); 5985576Sedp } else { 5995576Sedp if (zone_get_rootpath(zone_name, 6005576Sedp rootpath, sizeof (rootpath)) != Z_OK) { 6015576Sedp zerror(zlogp, B_FALSE, "unable to determine zone root"); 6025576Sedp return (-1); 6035576Sedp } 6045182Sedp } 6055182Sedp 6065182Sedp if ((rv = valid_mount_path(zlogp, rootpath, spec, dir, fstype)) < 0) { 6075182Sedp zerror(zlogp, B_FALSE, "%s%s is not a valid mount point", 6085182Sedp rootpath, dir); 6095182Sedp return (-1); 6105182Sedp } else if (rv > 0) { 6115182Sedp /* The mount point path doesn't exist, create it now. */ 6125182Sedp if (make_one_dir(zlogp, rootpath, dir, 6135182Sedp DEFAULT_DIR_MODE, DEFAULT_DIR_USER, 6145182Sedp DEFAULT_DIR_GROUP) != 0) { 6155182Sedp zerror(zlogp, B_FALSE, "failed to create mount point"); 6165182Sedp return (-1); 6175182Sedp } 6185182Sedp 6195182Sedp /* 6205182Sedp * Now this might seem weird, but we need to invoke 6215182Sedp * valid_mount_path() again. Why? Because it checks 6225182Sedp * to make sure that the mount point path is canonical, 6235182Sedp * which it can only do if the path exists, so now that 6245182Sedp * we've created the path we have to verify it again. 6255182Sedp */ 6265182Sedp if ((rv = valid_mount_path(zlogp, rootpath, spec, dir, 6275182Sedp fstype)) < 0) { 6285182Sedp zerror(zlogp, B_FALSE, 6295182Sedp "%s%s is not a valid mount point", rootpath, dir); 6305182Sedp return (-1); 6315182Sedp } 6325182Sedp } 6335182Sedp 6340Sstevel@tonic-gate if ((tmpl_fd = init_template()) == -1) { 6350Sstevel@tonic-gate zerror(zlogp, B_TRUE, "failed to create contract"); 6360Sstevel@tonic-gate return (-1); 6370Sstevel@tonic-gate } 6380Sstevel@tonic-gate 6390Sstevel@tonic-gate if ((child = fork()) == -1) { 6400Sstevel@tonic-gate (void) ct_tmpl_clear(tmpl_fd); 6410Sstevel@tonic-gate (void) close(tmpl_fd); 6420Sstevel@tonic-gate zerror(zlogp, B_TRUE, "failed to fork"); 6430Sstevel@tonic-gate return (-1); 6440Sstevel@tonic-gate 6450Sstevel@tonic-gate } else if (child == 0) { /* child */ 6462712Snn35248 char opt_buf[MAX_MNTOPT_STR]; 6472712Snn35248 int optlen = 0; 6482712Snn35248 int mflag = MS_DATA; 6492712Snn35248 6500Sstevel@tonic-gate (void) ct_tmpl_clear(tmpl_fd); 6510Sstevel@tonic-gate /* 6520Sstevel@tonic-gate * Even though there are no procs running in the zone, we 6530Sstevel@tonic-gate * do this for paranoia's sake. 6540Sstevel@tonic-gate */ 6550Sstevel@tonic-gate (void) closefrom(0); 6560Sstevel@tonic-gate 6570Sstevel@tonic-gate if (zone_enter(zoneid) == -1) { 6580Sstevel@tonic-gate _exit(errno); 6590Sstevel@tonic-gate } 6602712Snn35248 if (opt != NULL) { 6612712Snn35248 /* 6622712Snn35248 * The mount() system call is incredibly annoying. 6632712Snn35248 * If options are specified, we need to copy them 6642712Snn35248 * into a temporary buffer since the mount() system 6652712Snn35248 * call will overwrite the options string. It will 6662712Snn35248 * also fail if the new option string it wants to 6672712Snn35248 * write is bigger than the one we passed in, so 6682712Snn35248 * you must pass in a buffer of the maximum possible 6692712Snn35248 * option string length. sigh. 6702712Snn35248 */ 6712712Snn35248 (void) strlcpy(opt_buf, opt, sizeof (opt_buf)); 6722712Snn35248 opt = opt_buf; 6732712Snn35248 optlen = MAX_MNTOPT_STR; 6742712Snn35248 mflag = MS_OPTIONSTR; 6752712Snn35248 } 6762712Snn35248 if (mount(spec, dir, mflag, fstype, NULL, 0, opt, optlen) != 0) 6770Sstevel@tonic-gate _exit(errno); 6780Sstevel@tonic-gate _exit(0); 6790Sstevel@tonic-gate } 6800Sstevel@tonic-gate 6810Sstevel@tonic-gate /* parent */ 6820Sstevel@tonic-gate if (contract_latest(&ct) == -1) 6830Sstevel@tonic-gate ct = -1; 6840Sstevel@tonic-gate (void) ct_tmpl_clear(tmpl_fd); 6850Sstevel@tonic-gate (void) close(tmpl_fd); 6860Sstevel@tonic-gate if (waitpid(child, &child_status, 0) != child) { 6870Sstevel@tonic-gate /* unexpected: we must have been signalled */ 6880Sstevel@tonic-gate (void) contract_abandon_id(ct); 6890Sstevel@tonic-gate return (-1); 6900Sstevel@tonic-gate } 6910Sstevel@tonic-gate (void) contract_abandon_id(ct); 6920Sstevel@tonic-gate if (WEXITSTATUS(child_status) != 0) { 6930Sstevel@tonic-gate errno = WEXITSTATUS(child_status); 6940Sstevel@tonic-gate zerror(zlogp, B_TRUE, "mount of %s failed", dir); 6950Sstevel@tonic-gate return (-1); 6960Sstevel@tonic-gate } 6970Sstevel@tonic-gate 6980Sstevel@tonic-gate return (0); 6990Sstevel@tonic-gate } 7000Sstevel@tonic-gate 7017370S<Gerald Jelinek> /* 7027370S<Gerald Jelinek> * If retstr is not NULL, the output of the subproc is returned in the str, 7037370S<Gerald Jelinek> * otherwise it is output using zerror(). Any memory allocated for retstr 7047370S<Gerald Jelinek> * should be freed by the caller. 7057370S<Gerald Jelinek> */ 7062712Snn35248 int 7077370S<Gerald Jelinek> do_subproc(zlog_t *zlogp, char *cmdbuf, char **retstr) 708766Scarlsonj { 7097370S<Gerald Jelinek> char buf[1024]; /* arbitrary large amount */ 7107370S<Gerald Jelinek> char *inbuf; 7112712Snn35248 FILE *file; 7122712Snn35248 int status; 7137370S<Gerald Jelinek> int rd_cnt; 7147370S<Gerald Jelinek> 7157370S<Gerald Jelinek> if (retstr != NULL) { 7167370S<Gerald Jelinek> if ((*retstr = malloc(1024)) == NULL) { 7177370S<Gerald Jelinek> zerror(zlogp, B_FALSE, "out of memory"); 7187370S<Gerald Jelinek> return (-1); 7197370S<Gerald Jelinek> } 7207370S<Gerald Jelinek> inbuf = *retstr; 7217370S<Gerald Jelinek> rd_cnt = 0; 7227370S<Gerald Jelinek> } else { 7237370S<Gerald Jelinek> inbuf = buf; 7247370S<Gerald Jelinek> } 725766Scarlsonj 7262712Snn35248 file = popen(cmdbuf, "r"); 7272712Snn35248 if (file == NULL) { 7282712Snn35248 zerror(zlogp, B_TRUE, "could not launch: %s", cmdbuf); 7292525Sdp return (-1); 7302712Snn35248 } 7312525Sdp 7327370S<Gerald Jelinek> while (fgets(inbuf, 1024, file) != NULL) { 7337370S<Gerald Jelinek> if (retstr == NULL && zlogp != &logsys) { 7342712Snn35248 zerror(zlogp, B_FALSE, "%s", inbuf); 7357370S<Gerald Jelinek> } else { 7367370S<Gerald Jelinek> char *p; 7377370S<Gerald Jelinek> 7387370S<Gerald Jelinek> rd_cnt += 1024 - 1; 7397370S<Gerald Jelinek> if ((p = realloc(*retstr, rd_cnt + 1024)) == NULL) { 7407370S<Gerald Jelinek> zerror(zlogp, B_FALSE, "out of memory"); 7417370S<Gerald Jelinek> (void) pclose(file); 7427370S<Gerald Jelinek> return (-1); 7437370S<Gerald Jelinek> } 7447370S<Gerald Jelinek> 7457370S<Gerald Jelinek> *retstr = p; 7467370S<Gerald Jelinek> inbuf = *retstr + rd_cnt; 7477370S<Gerald Jelinek> } 7487370S<Gerald Jelinek> } 7492712Snn35248 status = pclose(file); 750766Scarlsonj 7512712Snn35248 if (WIFSIGNALED(status)) { 7522712Snn35248 zerror(zlogp, B_FALSE, "%s unexpectedly terminated due to " 7532712Snn35248 "signal %d", cmdbuf, WTERMSIG(status)); 754766Scarlsonj return (-1); 7552712Snn35248 } 7562712Snn35248 assert(WIFEXITED(status)); 7572712Snn35248 if (WEXITSTATUS(status) == ZEXIT_EXEC) { 7582712Snn35248 zerror(zlogp, B_FALSE, "failed to exec %s", cmdbuf); 7592712Snn35248 return (-1); 7602712Snn35248 } 7612712Snn35248 return (WEXITSTATUS(status)); 762766Scarlsonj } 763766Scarlsonj 764766Scarlsonj static int 7657370S<Gerald Jelinek> zone_bootup(zlog_t *zlogp, const char *bootargs, int zstate) 7660Sstevel@tonic-gate { 7670Sstevel@tonic-gate zoneid_t zoneid; 7680Sstevel@tonic-gate struct stat st; 7697089Sgjelinek char zpath[MAXPATHLEN], initpath[MAXPATHLEN], init_file[MAXPATHLEN]; 7702267Sdp char nbootargs[BOOTARGS_MAX]; 7712712Snn35248 char cmdbuf[MAXPATHLEN]; 7722712Snn35248 fs_callback_t cb; 7732727Sedp brand_handle_t bh; 7742267Sdp int err; 7750Sstevel@tonic-gate 7767370S<Gerald Jelinek> if (brand_prestatechg(zlogp, zstate, Z_BOOT) != 0) 7777370S<Gerald Jelinek> return (-1); 7787370S<Gerald Jelinek> 7790Sstevel@tonic-gate if ((zoneid = getzoneidbyname(zone_name)) == -1) { 7800Sstevel@tonic-gate zerror(zlogp, B_TRUE, "unable to get zoneid"); 781*8958Sdp@eng.sun.com goto bad; 7820Sstevel@tonic-gate } 7830Sstevel@tonic-gate 7842712Snn35248 cb.zlogp = zlogp; 7852712Snn35248 cb.zoneid = zoneid; 7865576Sedp cb.mount_cmd = B_FALSE; 7872712Snn35248 7882712Snn35248 /* Get a handle to the brand info for this zone */ 7892727Sedp if ((bh = brand_open(brand_name)) == NULL) { 7902712Snn35248 zerror(zlogp, B_FALSE, "unable to determine zone brand"); 791*8958Sdp@eng.sun.com goto bad; 7922712Snn35248 } 7932712Snn35248 7942712Snn35248 /* 7952712Snn35248 * Get the list of filesystems to mount from the brand 7962712Snn35248 * configuration. These mounts are done via a thread that will 7972712Snn35248 * enter the zone, so they are done from within the context of the 7982712Snn35248 * zone. 7992712Snn35248 */ 8002727Sedp if (brand_platform_iter_mounts(bh, mount_early_fs, &cb) != 0) { 8012712Snn35248 zerror(zlogp, B_FALSE, "unable to mount filesystems"); 8022727Sedp brand_close(bh); 803*8958Sdp@eng.sun.com goto bad; 8042712Snn35248 } 8052712Snn35248 8062712Snn35248 /* 8072712Snn35248 * Get the brand's boot callback if it exists. 8082712Snn35248 */ 8097089Sgjelinek if (zone_get_zonepath(zone_name, zpath, sizeof (zpath)) != Z_OK) { 8107089Sgjelinek zerror(zlogp, B_FALSE, "unable to determine zone path"); 8113716Sgjelinek brand_close(bh); 812*8958Sdp@eng.sun.com goto bad; 8132712Snn35248 } 8142712Snn35248 (void) strcpy(cmdbuf, EXEC_PREFIX); 8157089Sgjelinek if (brand_get_boot(bh, zone_name, zpath, cmdbuf + EXEC_LEN, 8167089Sgjelinek sizeof (cmdbuf) - EXEC_LEN) != 0) { 8172712Snn35248 zerror(zlogp, B_FALSE, 8182712Snn35248 "unable to determine branded zone's boot callback"); 8192727Sedp brand_close(bh); 820*8958Sdp@eng.sun.com goto bad; 8212712Snn35248 } 8222712Snn35248 8232712Snn35248 /* Get the path for this zone's init(1M) (or equivalent) process. */ 8242727Sedp if (brand_get_initname(bh, init_file, MAXPATHLEN) != 0) { 8252712Snn35248 zerror(zlogp, B_FALSE, 8262712Snn35248 "unable to determine zone's init(1M) location"); 8272727Sedp brand_close(bh); 828*8958Sdp@eng.sun.com goto bad; 8292712Snn35248 } 8302712Snn35248 8312727Sedp brand_close(bh); 8320Sstevel@tonic-gate 8332267Sdp err = filter_bootargs(zlogp, bootargs, nbootargs, init_file, 8342267Sdp bad_boot_arg); 8352267Sdp if (err == Z_INVAL) 8362267Sdp eventstream_write(Z_EVT_ZONE_BADARGS); 8372267Sdp else if (err != Z_OK) 838*8958Sdp@eng.sun.com goto bad; 8392267Sdp 8402267Sdp assert(init_file[0] != '\0'); 8412267Sdp 8422712Snn35248 /* Try to anticipate possible problems: Make sure init is executable. */ 8437089Sgjelinek if (zone_get_rootpath(zone_name, zpath, sizeof (zpath)) != Z_OK) { 8440Sstevel@tonic-gate zerror(zlogp, B_FALSE, "unable to determine zone root"); 845*8958Sdp@eng.sun.com goto bad; 8460Sstevel@tonic-gate } 8472712Snn35248 8487089Sgjelinek (void) snprintf(initpath, sizeof (initpath), "%s%s", zpath, init_file); 8490Sstevel@tonic-gate 8500Sstevel@tonic-gate if (stat(initpath, &st) == -1) { 8510Sstevel@tonic-gate zerror(zlogp, B_TRUE, "could not stat %s", initpath); 852*8958Sdp@eng.sun.com goto bad; 8530Sstevel@tonic-gate } 8540Sstevel@tonic-gate 8550Sstevel@tonic-gate if ((st.st_mode & S_IXUSR) == 0) { 8560Sstevel@tonic-gate zerror(zlogp, B_FALSE, "%s is not executable", initpath); 857*8958Sdp@eng.sun.com goto bad; 8580Sstevel@tonic-gate } 8590Sstevel@tonic-gate 8602712Snn35248 /* 8612712Snn35248 * If there is a brand 'boot' callback, execute it now to give the 8622712Snn35248 * brand one last chance to do any additional setup before the zone 8632712Snn35248 * is booted. 8642712Snn35248 */ 8652712Snn35248 if ((strlen(cmdbuf) > EXEC_LEN) && 8667370S<Gerald Jelinek> (do_subproc(zlogp, cmdbuf, NULL) != Z_OK)) { 8672712Snn35248 zerror(zlogp, B_FALSE, "%s failed", cmdbuf); 868*8958Sdp@eng.sun.com goto bad; 8692712Snn35248 } 8702712Snn35248 8712267Sdp if (zone_setattr(zoneid, ZONE_ATTR_INITNAME, init_file, 0) == -1) { 8722267Sdp zerror(zlogp, B_TRUE, "could not set zone boot file"); 873*8958Sdp@eng.sun.com goto bad; 8742267Sdp } 8752267Sdp 8762267Sdp if (zone_setattr(zoneid, ZONE_ATTR_BOOTARGS, nbootargs, 0) == -1) { 8772267Sdp zerror(zlogp, B_TRUE, "could not set zone boot arguments"); 878*8958Sdp@eng.sun.com goto bad; 8792267Sdp } 8802267Sdp 8812267Sdp if (zone_boot(zoneid) == -1) { 8820Sstevel@tonic-gate zerror(zlogp, B_TRUE, "unable to boot zone"); 883*8958Sdp@eng.sun.com goto bad; 8840Sstevel@tonic-gate } 8850Sstevel@tonic-gate 8867370S<Gerald Jelinek> if (brand_poststatechg(zlogp, zstate, Z_BOOT) != 0) 887*8958Sdp@eng.sun.com goto bad; 8887370S<Gerald Jelinek> 8890Sstevel@tonic-gate return (0); 890*8958Sdp@eng.sun.com 891*8958Sdp@eng.sun.com bad: 892*8958Sdp@eng.sun.com /* 893*8958Sdp@eng.sun.com * If something goes wrong, we up the zones's state to the target 894*8958Sdp@eng.sun.com * state, RUNNING, and then invoke the hook as if we're halting. 895*8958Sdp@eng.sun.com */ 896*8958Sdp@eng.sun.com (void) brand_poststatechg(zlogp, ZONE_STATE_RUNNING, Z_HALT); 897*8958Sdp@eng.sun.com return (-1); 8980Sstevel@tonic-gate } 8990Sstevel@tonic-gate 9000Sstevel@tonic-gate static int 9017370S<Gerald Jelinek> zone_halt(zlog_t *zlogp, boolean_t unmount_cmd, boolean_t rebooting, int zstate) 9020Sstevel@tonic-gate { 9030Sstevel@tonic-gate int err; 9040Sstevel@tonic-gate 9057370S<Gerald Jelinek> if (brand_prestatechg(zlogp, zstate, Z_HALT) != 0) 9067370S<Gerald Jelinek> return (-1); 9077370S<Gerald Jelinek> 9083247Sgjelinek if (vplat_teardown(zlogp, unmount_cmd, rebooting) != 0) { 9090Sstevel@tonic-gate if (!bringup_failure_recovery) 9100Sstevel@tonic-gate zerror(zlogp, B_FALSE, "unable to destroy zone"); 9110Sstevel@tonic-gate return (-1); 9120Sstevel@tonic-gate } 9130Sstevel@tonic-gate 9140Sstevel@tonic-gate if ((err = zonecfg_destroy_snapshot(zone_name)) != Z_OK) 9150Sstevel@tonic-gate zerror(zlogp, B_FALSE, "destroying snapshot: %s", 9160Sstevel@tonic-gate zonecfg_strerror(err)); 9170Sstevel@tonic-gate 9187370S<Gerald Jelinek> if (brand_poststatechg(zlogp, zstate, Z_HALT) != 0) 9197370S<Gerald Jelinek> return (-1); 9207370S<Gerald Jelinek> 9210Sstevel@tonic-gate return (0); 9220Sstevel@tonic-gate } 9230Sstevel@tonic-gate 9240Sstevel@tonic-gate /* 9250Sstevel@tonic-gate * Generate AUE_zone_state for a command that boots a zone. 9260Sstevel@tonic-gate */ 9270Sstevel@tonic-gate static void 9280Sstevel@tonic-gate audit_put_record(zlog_t *zlogp, ucred_t *uc, int return_val, 9290Sstevel@tonic-gate char *new_state) 9300Sstevel@tonic-gate { 9310Sstevel@tonic-gate adt_session_data_t *ah; 9320Sstevel@tonic-gate adt_event_data_t *event; 9330Sstevel@tonic-gate int pass_fail, fail_reason; 9340Sstevel@tonic-gate 9350Sstevel@tonic-gate if (!adt_audit_enabled()) 9360Sstevel@tonic-gate return; 9370Sstevel@tonic-gate 9380Sstevel@tonic-gate if (return_val == 0) { 9390Sstevel@tonic-gate pass_fail = ADT_SUCCESS; 9400Sstevel@tonic-gate fail_reason = ADT_SUCCESS; 9410Sstevel@tonic-gate } else { 9420Sstevel@tonic-gate pass_fail = ADT_FAILURE; 9430Sstevel@tonic-gate fail_reason = ADT_FAIL_VALUE_PROGRAM; 9440Sstevel@tonic-gate } 9450Sstevel@tonic-gate 9460Sstevel@tonic-gate if (adt_start_session(&ah, NULL, 0)) { 9470Sstevel@tonic-gate zerror(zlogp, B_TRUE, gettext("audit failure.")); 9480Sstevel@tonic-gate return; 9490Sstevel@tonic-gate } 9500Sstevel@tonic-gate if (adt_set_from_ucred(ah, uc, ADT_NEW)) { 9510Sstevel@tonic-gate zerror(zlogp, B_TRUE, gettext("audit failure.")); 9520Sstevel@tonic-gate (void) adt_end_session(ah); 9530Sstevel@tonic-gate return; 9540Sstevel@tonic-gate } 9550Sstevel@tonic-gate 9560Sstevel@tonic-gate event = adt_alloc_event(ah, ADT_zone_state); 9570Sstevel@tonic-gate if (event == NULL) { 9580Sstevel@tonic-gate zerror(zlogp, B_TRUE, gettext("audit failure.")); 9590Sstevel@tonic-gate (void) adt_end_session(ah); 9600Sstevel@tonic-gate return; 9610Sstevel@tonic-gate } 9620Sstevel@tonic-gate event->adt_zone_state.zonename = zone_name; 9630Sstevel@tonic-gate event->adt_zone_state.new_state = new_state; 9640Sstevel@tonic-gate 9650Sstevel@tonic-gate if (adt_put_event(event, pass_fail, fail_reason)) 9660Sstevel@tonic-gate zerror(zlogp, B_TRUE, gettext("audit failure.")); 9670Sstevel@tonic-gate 9680Sstevel@tonic-gate adt_free_event(event); 9690Sstevel@tonic-gate 9700Sstevel@tonic-gate (void) adt_end_session(ah); 9710Sstevel@tonic-gate } 9720Sstevel@tonic-gate 9730Sstevel@tonic-gate /* 9740Sstevel@tonic-gate * The main routine for the door server that deals with zone state transitions. 9750Sstevel@tonic-gate */ 9760Sstevel@tonic-gate /* ARGSUSED */ 9770Sstevel@tonic-gate static void 9780Sstevel@tonic-gate server(void *cookie, char *args, size_t alen, door_desc_t *dp, 9790Sstevel@tonic-gate uint_t n_desc) 9800Sstevel@tonic-gate { 9810Sstevel@tonic-gate ucred_t *uc = NULL; 9820Sstevel@tonic-gate const priv_set_t *eset; 9830Sstevel@tonic-gate 9840Sstevel@tonic-gate zone_state_t zstate; 9850Sstevel@tonic-gate zone_cmd_t cmd; 9860Sstevel@tonic-gate zone_cmd_arg_t *zargp; 9870Sstevel@tonic-gate 9880Sstevel@tonic-gate boolean_t kernelcall; 9890Sstevel@tonic-gate 9900Sstevel@tonic-gate int rval = -1; 9910Sstevel@tonic-gate uint64_t uniqid; 9920Sstevel@tonic-gate zoneid_t zoneid = -1; 9930Sstevel@tonic-gate zlog_t zlog; 9940Sstevel@tonic-gate zlog_t *zlogp; 9950Sstevel@tonic-gate zone_cmd_rval_t *rvalp; 9960Sstevel@tonic-gate size_t rlen = getpagesize(); /* conservative */ 9972712Snn35248 fs_callback_t cb; 9982727Sedp brand_handle_t bh; 9990Sstevel@tonic-gate 10000Sstevel@tonic-gate /* LINTED E_BAD_PTR_CAST_ALIGN */ 10010Sstevel@tonic-gate zargp = (zone_cmd_arg_t *)args; 10020Sstevel@tonic-gate 10030Sstevel@tonic-gate /* 10040Sstevel@tonic-gate * When we get the door unref message, we've fdetach'd the door, and 10050Sstevel@tonic-gate * it is time for us to shut down zoneadmd. 10060Sstevel@tonic-gate */ 10070Sstevel@tonic-gate if (zargp == DOOR_UNREF_DATA) { 10080Sstevel@tonic-gate /* 10090Sstevel@tonic-gate * See comment at end of main() for info on the last rites. 10100Sstevel@tonic-gate */ 10110Sstevel@tonic-gate exit(0); 10120Sstevel@tonic-gate } 10130Sstevel@tonic-gate 10140Sstevel@tonic-gate if (zargp == NULL) { 10150Sstevel@tonic-gate (void) door_return(NULL, 0, 0, 0); 10160Sstevel@tonic-gate } 10170Sstevel@tonic-gate 10180Sstevel@tonic-gate rvalp = alloca(rlen); 10190Sstevel@tonic-gate bzero(rvalp, rlen); 10200Sstevel@tonic-gate zlog.logfile = NULL; 10210Sstevel@tonic-gate zlog.buflen = zlog.loglen = rlen - sizeof (zone_cmd_rval_t) + 1; 10220Sstevel@tonic-gate zlog.buf = rvalp->errbuf; 10230Sstevel@tonic-gate zlog.log = zlog.buf; 10240Sstevel@tonic-gate /* defer initialization of zlog.locale until after credential check */ 10250Sstevel@tonic-gate zlogp = &zlog; 10260Sstevel@tonic-gate 10270Sstevel@tonic-gate if (alen != sizeof (zone_cmd_arg_t)) { 10280Sstevel@tonic-gate /* 10290Sstevel@tonic-gate * This really shouldn't be happening. 10300Sstevel@tonic-gate */ 10312267Sdp zerror(&logsys, B_FALSE, "argument size (%d bytes) " 10322267Sdp "unexpected (expected %d bytes)", alen, 10332267Sdp sizeof (zone_cmd_arg_t)); 10340Sstevel@tonic-gate goto out; 10350Sstevel@tonic-gate } 10360Sstevel@tonic-gate cmd = zargp->cmd; 10370Sstevel@tonic-gate 10380Sstevel@tonic-gate if (door_ucred(&uc) != 0) { 10390Sstevel@tonic-gate zerror(&logsys, B_TRUE, "door_ucred"); 10400Sstevel@tonic-gate goto out; 10410Sstevel@tonic-gate } 10420Sstevel@tonic-gate eset = ucred_getprivset(uc, PRIV_EFFECTIVE); 10430Sstevel@tonic-gate if (ucred_getzoneid(uc) != GLOBAL_ZONEID || 10440Sstevel@tonic-gate (eset != NULL ? !priv_ismember(eset, PRIV_SYS_CONFIG) : 10450Sstevel@tonic-gate ucred_geteuid(uc) != 0)) { 10460Sstevel@tonic-gate zerror(&logsys, B_FALSE, "insufficient privileges"); 10470Sstevel@tonic-gate goto out; 10480Sstevel@tonic-gate } 10490Sstevel@tonic-gate 10500Sstevel@tonic-gate kernelcall = ucred_getpid(uc) == 0; 10510Sstevel@tonic-gate 10520Sstevel@tonic-gate /* 10530Sstevel@tonic-gate * This is safe because we only use a zlog_t throughout the 10540Sstevel@tonic-gate * duration of a door call; i.e., by the time the pointer 10550Sstevel@tonic-gate * might become invalid, the door call would be over. 10560Sstevel@tonic-gate */ 10570Sstevel@tonic-gate zlog.locale = kernelcall ? DEFAULT_LOCALE : zargp->locale; 10580Sstevel@tonic-gate 10590Sstevel@tonic-gate (void) mutex_lock(&lock); 10600Sstevel@tonic-gate 10610Sstevel@tonic-gate /* 10620Sstevel@tonic-gate * Once we start to really die off, we don't want more connections. 10630Sstevel@tonic-gate */ 10640Sstevel@tonic-gate if (in_death_throes) { 10650Sstevel@tonic-gate (void) mutex_unlock(&lock); 10660Sstevel@tonic-gate ucred_free(uc); 10670Sstevel@tonic-gate (void) door_return(NULL, 0, 0, 0); 10680Sstevel@tonic-gate thr_exit(NULL); 10690Sstevel@tonic-gate } 10700Sstevel@tonic-gate 10710Sstevel@tonic-gate /* 10720Sstevel@tonic-gate * Check for validity of command. 10730Sstevel@tonic-gate */ 10742712Snn35248 if (cmd != Z_READY && cmd != Z_BOOT && cmd != Z_FORCEBOOT && 10752712Snn35248 cmd != Z_REBOOT && cmd != Z_HALT && cmd != Z_NOTE_UNINSTALLING && 10762712Snn35248 cmd != Z_MOUNT && cmd != Z_FORCEMOUNT && cmd != Z_UNMOUNT) { 1077766Scarlsonj zerror(&logsys, B_FALSE, "invalid command %d", (int)cmd); 10780Sstevel@tonic-gate goto out; 10790Sstevel@tonic-gate } 10800Sstevel@tonic-gate 10810Sstevel@tonic-gate if (kernelcall && (cmd != Z_HALT && cmd != Z_REBOOT)) { 10820Sstevel@tonic-gate /* 10830Sstevel@tonic-gate * Can't happen 10840Sstevel@tonic-gate */ 10850Sstevel@tonic-gate zerror(&logsys, B_FALSE, "received unexpected kernel upcall %d", 10860Sstevel@tonic-gate cmd); 10870Sstevel@tonic-gate goto out; 10880Sstevel@tonic-gate } 10890Sstevel@tonic-gate /* 10900Sstevel@tonic-gate * We ignore the possibility of someone calling zone_create(2) 10910Sstevel@tonic-gate * explicitly; all requests must come through zoneadmd. 10920Sstevel@tonic-gate */ 10930Sstevel@tonic-gate if (zone_get_state(zone_name, &zstate) != Z_OK) { 10940Sstevel@tonic-gate /* 10950Sstevel@tonic-gate * Something terribly wrong happened 10960Sstevel@tonic-gate */ 10970Sstevel@tonic-gate zerror(&logsys, B_FALSE, "unable to determine state of zone"); 10980Sstevel@tonic-gate goto out; 10990Sstevel@tonic-gate } 11000Sstevel@tonic-gate 11010Sstevel@tonic-gate if (kernelcall) { 11020Sstevel@tonic-gate /* 11030Sstevel@tonic-gate * Kernel-initiated requests may lose their validity if the 11040Sstevel@tonic-gate * zone_t the kernel was referring to has gone away. 11050Sstevel@tonic-gate */ 11060Sstevel@tonic-gate if ((zoneid = getzoneidbyname(zone_name)) == -1 || 11070Sstevel@tonic-gate zone_getattr(zoneid, ZONE_ATTR_UNIQID, &uniqid, 11080Sstevel@tonic-gate sizeof (uniqid)) == -1 || uniqid != zargp->uniqid) { 11090Sstevel@tonic-gate /* 11100Sstevel@tonic-gate * We're not talking about the same zone. The request 11110Sstevel@tonic-gate * must have arrived too late. Return error. 11120Sstevel@tonic-gate */ 11130Sstevel@tonic-gate rval = -1; 11140Sstevel@tonic-gate goto out; 11150Sstevel@tonic-gate } 11160Sstevel@tonic-gate zlogp = &logsys; /* Log errors to syslog */ 11170Sstevel@tonic-gate } 11180Sstevel@tonic-gate 11192712Snn35248 /* 11202712Snn35248 * If we are being asked to forcibly mount or boot a zone, we 11212712Snn35248 * pretend that an INCOMPLETE zone is actually INSTALLED. 11222712Snn35248 */ 11232712Snn35248 if (zstate == ZONE_STATE_INCOMPLETE && 11242712Snn35248 (cmd == Z_FORCEBOOT || cmd == Z_FORCEMOUNT)) 11252712Snn35248 zstate = ZONE_STATE_INSTALLED; 11262712Snn35248 11270Sstevel@tonic-gate switch (zstate) { 11280Sstevel@tonic-gate case ZONE_STATE_CONFIGURED: 11290Sstevel@tonic-gate case ZONE_STATE_INCOMPLETE: 11300Sstevel@tonic-gate /* 11310Sstevel@tonic-gate * Not our area of expertise; we just print a nice message 11320Sstevel@tonic-gate * and die off. 11330Sstevel@tonic-gate */ 11340Sstevel@tonic-gate zerror(zlogp, B_FALSE, 11350Sstevel@tonic-gate "%s operation is invalid for zones in state '%s'", 1136766Scarlsonj z_cmd_name(cmd), zone_state_str(zstate)); 11370Sstevel@tonic-gate break; 11380Sstevel@tonic-gate 11390Sstevel@tonic-gate case ZONE_STATE_INSTALLED: 11400Sstevel@tonic-gate switch (cmd) { 11410Sstevel@tonic-gate case Z_READY: 11427370S<Gerald Jelinek> rval = zone_ready(zlogp, Z_MNT_BOOT, zstate); 11430Sstevel@tonic-gate if (rval == 0) 11440Sstevel@tonic-gate eventstream_write(Z_EVT_ZONE_READIED); 11450Sstevel@tonic-gate break; 11460Sstevel@tonic-gate case Z_BOOT: 11472712Snn35248 case Z_FORCEBOOT: 11480Sstevel@tonic-gate eventstream_write(Z_EVT_ZONE_BOOTING); 11497370S<Gerald Jelinek> if ((rval = zone_ready(zlogp, Z_MNT_BOOT, zstate)) 11507370S<Gerald Jelinek> == 0) { 11517370S<Gerald Jelinek> rval = zone_bootup(zlogp, zargp->bootbuf, 11527370S<Gerald Jelinek> zstate); 11537370S<Gerald Jelinek> } 11540Sstevel@tonic-gate audit_put_record(zlogp, uc, rval, "boot"); 11550Sstevel@tonic-gate if (rval != 0) { 11560Sstevel@tonic-gate bringup_failure_recovery = B_TRUE; 11577370S<Gerald Jelinek> (void) zone_halt(zlogp, B_FALSE, B_FALSE, 11587370S<Gerald Jelinek> zstate); 11591645Scomay eventstream_write(Z_EVT_ZONE_BOOTFAILED); 11600Sstevel@tonic-gate } 11610Sstevel@tonic-gate break; 11620Sstevel@tonic-gate case Z_HALT: 11630Sstevel@tonic-gate if (kernelcall) /* Invalid; can't happen */ 11640Sstevel@tonic-gate abort(); 11650Sstevel@tonic-gate /* 11660Sstevel@tonic-gate * We could have two clients racing to halt this 11670Sstevel@tonic-gate * zone; the second client loses, but his request 11680Sstevel@tonic-gate * doesn't fail, since the zone is now in the desired 11690Sstevel@tonic-gate * state. 11700Sstevel@tonic-gate */ 11710Sstevel@tonic-gate zerror(zlogp, B_FALSE, "zone is already halted"); 11720Sstevel@tonic-gate rval = 0; 11730Sstevel@tonic-gate break; 11740Sstevel@tonic-gate case Z_REBOOT: 11750Sstevel@tonic-gate if (kernelcall) /* Invalid; can't happen */ 11760Sstevel@tonic-gate abort(); 11770Sstevel@tonic-gate zerror(zlogp, B_FALSE, "%s operation is invalid " 1178766Scarlsonj "for zones in state '%s'", z_cmd_name(cmd), 11790Sstevel@tonic-gate zone_state_str(zstate)); 11800Sstevel@tonic-gate rval = -1; 11810Sstevel@tonic-gate break; 11820Sstevel@tonic-gate case Z_NOTE_UNINSTALLING: 11830Sstevel@tonic-gate if (kernelcall) /* Invalid; can't happen */ 11840Sstevel@tonic-gate abort(); 11850Sstevel@tonic-gate /* 11860Sstevel@tonic-gate * Tell the console to print out a message about this. 11870Sstevel@tonic-gate * Once it does, we will be in_death_throes. 11880Sstevel@tonic-gate */ 11890Sstevel@tonic-gate eventstream_write(Z_EVT_ZONE_UNINSTALLING); 11900Sstevel@tonic-gate break; 1191766Scarlsonj case Z_MOUNT: 11922712Snn35248 case Z_FORCEMOUNT: 1193766Scarlsonj if (kernelcall) /* Invalid; can't happen */ 1194766Scarlsonj abort(); 11958905SRic.Aleshire@Sun.COM if (!zone_isnative && !zone_iscluster && 11968905SRic.Aleshire@Sun.COM !zone_islabeled) { 11977655Sgerald.jelinek@sun.com /* 11987655Sgerald.jelinek@sun.com * -U mounts the zone without lofs mounting 11997655Sgerald.jelinek@sun.com * zone file systems back into the scratch 12007655Sgerald.jelinek@sun.com * zone. This is required when mounting 12017655Sgerald.jelinek@sun.com * non-native branded zones. 12027655Sgerald.jelinek@sun.com */ 12037655Sgerald.jelinek@sun.com (void) strlcpy(zargp->bootbuf, "-U", 12047655Sgerald.jelinek@sun.com BOOTARGS_MAX); 12052712Snn35248 } 12062712Snn35248 12075829Sgjelinek rval = zone_ready(zlogp, 12085829Sgjelinek strcmp(zargp->bootbuf, "-U") == 0 ? 12097370S<Gerald Jelinek> Z_MNT_UPDATE : Z_MNT_SCRATCH, zstate); 12102712Snn35248 if (rval != 0) 12112712Snn35248 break; 12122712Snn35248 12132712Snn35248 eventstream_write(Z_EVT_ZONE_READIED); 12142712Snn35248 12157655Sgerald.jelinek@sun.com /* 12167655Sgerald.jelinek@sun.com * Get a handle to the native brand info. 12177655Sgerald.jelinek@sun.com * We must always use the native brand file system 12187655Sgerald.jelinek@sun.com * list when mounting the zone. 12197655Sgerald.jelinek@sun.com */ 12207655Sgerald.jelinek@sun.com if ((bh = brand_open(NATIVE_BRAND_NAME)) == NULL) { 12212712Snn35248 rval = -1; 12222712Snn35248 break; 12231645Scomay } 12241645Scomay 1225766Scarlsonj /* 12262712Snn35248 * Get the list of filesystems to mount from 12272712Snn35248 * the brand configuration. These mounts are done 12282712Snn35248 * via a thread that will enter the zone, so they 12292712Snn35248 * are done from within the context of the zone. 12302712Snn35248 */ 12312712Snn35248 cb.zlogp = zlogp; 12322712Snn35248 cb.zoneid = zone_id; 12335576Sedp cb.mount_cmd = B_TRUE; 12342727Sedp rval = brand_platform_iter_mounts(bh, 12352712Snn35248 mount_early_fs, &cb); 12362712Snn35248 12372727Sedp brand_close(bh); 12382712Snn35248 12392712Snn35248 /* 1240766Scarlsonj * Ordinarily, /dev/fd would be mounted inside the zone 1241766Scarlsonj * by svc:/system/filesystem/usr:default, but since 1242766Scarlsonj * we're not booting the zone, we need to do this 1243766Scarlsonj * manually. 1244766Scarlsonj */ 1245766Scarlsonj if (rval == 0) 12462712Snn35248 rval = mount_early_fs(&cb, 12472712Snn35248 "fd", "/dev/fd", "fd", NULL); 1248766Scarlsonj break; 1249766Scarlsonj case Z_UNMOUNT: 1250766Scarlsonj if (kernelcall) /* Invalid; can't happen */ 1251766Scarlsonj abort(); 1252766Scarlsonj zerror(zlogp, B_FALSE, "zone is already unmounted"); 1253766Scarlsonj rval = 0; 1254766Scarlsonj break; 12550Sstevel@tonic-gate } 12560Sstevel@tonic-gate break; 12570Sstevel@tonic-gate 12580Sstevel@tonic-gate case ZONE_STATE_READY: 12590Sstevel@tonic-gate switch (cmd) { 12600Sstevel@tonic-gate case Z_READY: 12610Sstevel@tonic-gate /* 12620Sstevel@tonic-gate * We could have two clients racing to ready this 12630Sstevel@tonic-gate * zone; the second client loses, but his request 12640Sstevel@tonic-gate * doesn't fail, since the zone is now in the desired 12650Sstevel@tonic-gate * state. 12660Sstevel@tonic-gate */ 12670Sstevel@tonic-gate zerror(zlogp, B_FALSE, "zone is already ready"); 12680Sstevel@tonic-gate rval = 0; 12690Sstevel@tonic-gate break; 12700Sstevel@tonic-gate case Z_BOOT: 12712267Sdp (void) strlcpy(boot_args, zargp->bootbuf, 12722267Sdp sizeof (boot_args)); 12730Sstevel@tonic-gate eventstream_write(Z_EVT_ZONE_BOOTING); 12747370S<Gerald Jelinek> rval = zone_bootup(zlogp, zargp->bootbuf, zstate); 12750Sstevel@tonic-gate audit_put_record(zlogp, uc, rval, "boot"); 12760Sstevel@tonic-gate if (rval != 0) { 12770Sstevel@tonic-gate bringup_failure_recovery = B_TRUE; 12787370S<Gerald Jelinek> (void) zone_halt(zlogp, B_FALSE, B_TRUE, 12797370S<Gerald Jelinek> zstate); 12801645Scomay eventstream_write(Z_EVT_ZONE_BOOTFAILED); 12810Sstevel@tonic-gate } 12822267Sdp boot_args[0] = '\0'; 12830Sstevel@tonic-gate break; 12840Sstevel@tonic-gate case Z_HALT: 12850Sstevel@tonic-gate if (kernelcall) /* Invalid; can't happen */ 12860Sstevel@tonic-gate abort(); 12877370S<Gerald Jelinek> if ((rval = zone_halt(zlogp, B_FALSE, B_FALSE, zstate)) 12887370S<Gerald Jelinek> != 0) 12890Sstevel@tonic-gate break; 12900Sstevel@tonic-gate eventstream_write(Z_EVT_ZONE_HALTED); 12910Sstevel@tonic-gate break; 12920Sstevel@tonic-gate case Z_REBOOT: 1293766Scarlsonj case Z_NOTE_UNINSTALLING: 1294766Scarlsonj case Z_MOUNT: 1295766Scarlsonj case Z_UNMOUNT: 12960Sstevel@tonic-gate if (kernelcall) /* Invalid; can't happen */ 12970Sstevel@tonic-gate abort(); 12980Sstevel@tonic-gate zerror(zlogp, B_FALSE, "%s operation is invalid " 1299766Scarlsonj "for zones in state '%s'", z_cmd_name(cmd), 13000Sstevel@tonic-gate zone_state_str(zstate)); 13010Sstevel@tonic-gate rval = -1; 13020Sstevel@tonic-gate break; 1303766Scarlsonj } 1304766Scarlsonj break; 1305766Scarlsonj 1306766Scarlsonj case ZONE_STATE_MOUNTED: 1307766Scarlsonj switch (cmd) { 1308766Scarlsonj case Z_UNMOUNT: 13090Sstevel@tonic-gate if (kernelcall) /* Invalid; can't happen */ 13100Sstevel@tonic-gate abort(); 13117370S<Gerald Jelinek> rval = zone_halt(zlogp, B_TRUE, B_FALSE, zstate); 13121645Scomay if (rval == 0) { 13131645Scomay eventstream_write(Z_EVT_ZONE_HALTED); 1314766Scarlsonj (void) sema_post(&scratch_sem); 13151645Scomay } 1316766Scarlsonj break; 1317766Scarlsonj default: 1318766Scarlsonj if (kernelcall) /* Invalid; can't happen */ 1319766Scarlsonj abort(); 1320766Scarlsonj zerror(zlogp, B_FALSE, "%s operation is invalid " 1321766Scarlsonj "for zones in state '%s'", z_cmd_name(cmd), 1322766Scarlsonj zone_state_str(zstate)); 13230Sstevel@tonic-gate rval = -1; 13240Sstevel@tonic-gate break; 13250Sstevel@tonic-gate } 13260Sstevel@tonic-gate break; 13270Sstevel@tonic-gate 13280Sstevel@tonic-gate case ZONE_STATE_RUNNING: 13290Sstevel@tonic-gate case ZONE_STATE_SHUTTING_DOWN: 13300Sstevel@tonic-gate case ZONE_STATE_DOWN: 13310Sstevel@tonic-gate switch (cmd) { 13320Sstevel@tonic-gate case Z_READY: 13337370S<Gerald Jelinek> if ((rval = zone_halt(zlogp, B_FALSE, B_TRUE, zstate)) 13347370S<Gerald Jelinek> != 0) 13350Sstevel@tonic-gate break; 13367370S<Gerald Jelinek> if ((rval = zone_ready(zlogp, Z_MNT_BOOT, zstate)) == 0) 13370Sstevel@tonic-gate eventstream_write(Z_EVT_ZONE_READIED); 13381645Scomay else 13391645Scomay eventstream_write(Z_EVT_ZONE_HALTED); 13400Sstevel@tonic-gate break; 13410Sstevel@tonic-gate case Z_BOOT: 13420Sstevel@tonic-gate /* 13430Sstevel@tonic-gate * We could have two clients racing to boot this 13440Sstevel@tonic-gate * zone; the second client loses, but his request 13450Sstevel@tonic-gate * doesn't fail, since the zone is now in the desired 13460Sstevel@tonic-gate * state. 13470Sstevel@tonic-gate */ 13480Sstevel@tonic-gate zerror(zlogp, B_FALSE, "zone is already booted"); 13490Sstevel@tonic-gate rval = 0; 13500Sstevel@tonic-gate break; 13510Sstevel@tonic-gate case Z_HALT: 13527370S<Gerald Jelinek> if ((rval = zone_halt(zlogp, B_FALSE, B_FALSE, zstate)) 13537370S<Gerald Jelinek> != 0) 13540Sstevel@tonic-gate break; 13550Sstevel@tonic-gate eventstream_write(Z_EVT_ZONE_HALTED); 13560Sstevel@tonic-gate break; 13570Sstevel@tonic-gate case Z_REBOOT: 13582267Sdp (void) strlcpy(boot_args, zargp->bootbuf, 13592267Sdp sizeof (boot_args)); 13600Sstevel@tonic-gate eventstream_write(Z_EVT_ZONE_REBOOTING); 13617370S<Gerald Jelinek> if ((rval = zone_halt(zlogp, B_FALSE, B_TRUE, zstate)) 13627370S<Gerald Jelinek> != 0) { 13631645Scomay eventstream_write(Z_EVT_ZONE_BOOTFAILED); 13642267Sdp boot_args[0] = '\0'; 13651645Scomay break; 13661645Scomay } 13677370S<Gerald Jelinek> if ((rval = zone_ready(zlogp, Z_MNT_BOOT, zstate)) 13687370S<Gerald Jelinek> != 0) { 13691645Scomay eventstream_write(Z_EVT_ZONE_BOOTFAILED); 13702267Sdp boot_args[0] = '\0'; 13710Sstevel@tonic-gate break; 13721645Scomay } 13737370S<Gerald Jelinek> rval = zone_bootup(zlogp, zargp->bootbuf, zstate); 13741645Scomay audit_put_record(zlogp, uc, rval, "reboot"); 13751645Scomay if (rval != 0) { 13767370S<Gerald Jelinek> (void) zone_halt(zlogp, B_FALSE, B_TRUE, 13777370S<Gerald Jelinek> zstate); 13781645Scomay eventstream_write(Z_EVT_ZONE_BOOTFAILED); 13790Sstevel@tonic-gate } 13802267Sdp boot_args[0] = '\0'; 13810Sstevel@tonic-gate break; 13820Sstevel@tonic-gate case Z_NOTE_UNINSTALLING: 1383766Scarlsonj case Z_MOUNT: 1384766Scarlsonj case Z_UNMOUNT: 1385766Scarlsonj zerror(zlogp, B_FALSE, "%s operation is invalid " 1386766Scarlsonj "for zones in state '%s'", z_cmd_name(cmd), 1387766Scarlsonj zone_state_str(zstate)); 13880Sstevel@tonic-gate rval = -1; 13890Sstevel@tonic-gate break; 13900Sstevel@tonic-gate } 13910Sstevel@tonic-gate break; 13920Sstevel@tonic-gate default: 13930Sstevel@tonic-gate abort(); 13940Sstevel@tonic-gate } 13950Sstevel@tonic-gate 13960Sstevel@tonic-gate /* 13970Sstevel@tonic-gate * Because the state of the zone may have changed, we make sure 13980Sstevel@tonic-gate * to wake the console poller, which is in charge of initiating 13990Sstevel@tonic-gate * the shutdown procedure as necessary. 14000Sstevel@tonic-gate */ 14010Sstevel@tonic-gate eventstream_write(Z_EVT_NULL); 14020Sstevel@tonic-gate 14030Sstevel@tonic-gate out: 14040Sstevel@tonic-gate (void) mutex_unlock(&lock); 14050Sstevel@tonic-gate if (kernelcall) { 14060Sstevel@tonic-gate rvalp = NULL; 14070Sstevel@tonic-gate rlen = 0; 14080Sstevel@tonic-gate } else { 14090Sstevel@tonic-gate rvalp->rval = rval; 14100Sstevel@tonic-gate } 14110Sstevel@tonic-gate if (uc != NULL) 14120Sstevel@tonic-gate ucred_free(uc); 14130Sstevel@tonic-gate (void) door_return((char *)rvalp, rlen, NULL, 0); 14140Sstevel@tonic-gate thr_exit(NULL); 14150Sstevel@tonic-gate } 14160Sstevel@tonic-gate 14170Sstevel@tonic-gate static int 14180Sstevel@tonic-gate setup_door(zlog_t *zlogp) 14190Sstevel@tonic-gate { 14200Sstevel@tonic-gate if ((zone_door = door_create(server, NULL, 14210Sstevel@tonic-gate DOOR_UNREF | DOOR_REFUSE_DESC | DOOR_NO_CANCEL)) < 0) { 14220Sstevel@tonic-gate zerror(zlogp, B_TRUE, "%s failed", "door_create"); 14230Sstevel@tonic-gate return (-1); 14240Sstevel@tonic-gate } 14250Sstevel@tonic-gate (void) fdetach(zone_door_path); 14260Sstevel@tonic-gate 14270Sstevel@tonic-gate if (fattach(zone_door, zone_door_path) != 0) { 14280Sstevel@tonic-gate zerror(zlogp, B_TRUE, "fattach to %s failed", zone_door_path); 14290Sstevel@tonic-gate (void) door_revoke(zone_door); 14300Sstevel@tonic-gate (void) fdetach(zone_door_path); 14310Sstevel@tonic-gate zone_door = -1; 14320Sstevel@tonic-gate return (-1); 14330Sstevel@tonic-gate } 14340Sstevel@tonic-gate return (0); 14350Sstevel@tonic-gate } 14360Sstevel@tonic-gate 14370Sstevel@tonic-gate /* 14380Sstevel@tonic-gate * zoneadm(1m) will start zoneadmd if it thinks it isn't running; this 14390Sstevel@tonic-gate * is where zoneadmd itself will check to see that another instance of 14400Sstevel@tonic-gate * zoneadmd isn't already controlling this zone. 14410Sstevel@tonic-gate * 14420Sstevel@tonic-gate * The idea here is that we want to open the path to which we will 14430Sstevel@tonic-gate * attach our door, lock it, and then make sure that no-one has beat us 14440Sstevel@tonic-gate * to fattach(3c)ing onto it. 14450Sstevel@tonic-gate * 14460Sstevel@tonic-gate * fattach(3c) is really a mount, so there are actually two possible 14470Sstevel@tonic-gate * vnodes we could be dealing with. Our strategy is as follows: 14480Sstevel@tonic-gate * 14490Sstevel@tonic-gate * - If the file we opened is a regular file (common case): 14500Sstevel@tonic-gate * There is no fattach(3c)ed door, so we have a chance of becoming 14510Sstevel@tonic-gate * the managing zoneadmd. We attempt to lock the file: if it is 14520Sstevel@tonic-gate * already locked, that means someone else raced us here, so we 14530Sstevel@tonic-gate * lose and give up. zoneadm(1m) will try to contact the zoneadmd 14540Sstevel@tonic-gate * that beat us to it. 14550Sstevel@tonic-gate * 14560Sstevel@tonic-gate * - If the file we opened is a namefs file: 14570Sstevel@tonic-gate * This means there is already an established door fattach(3c)'ed 14580Sstevel@tonic-gate * to the rendezvous path. We've lost the race, so we give up. 14590Sstevel@tonic-gate * Note that in this case we also try to grab the file lock, and 14600Sstevel@tonic-gate * will succeed in acquiring it since the vnode locked by the 14610Sstevel@tonic-gate * "winning" zoneadmd was a regular one, and the one we locked was 14620Sstevel@tonic-gate * the fattach(3c)'ed door node. At any rate, no harm is done, and 14630Sstevel@tonic-gate * we just return to zoneadm(1m) which knows to retry. 14640Sstevel@tonic-gate */ 14650Sstevel@tonic-gate static int 14660Sstevel@tonic-gate make_daemon_exclusive(zlog_t *zlogp) 14670Sstevel@tonic-gate { 14680Sstevel@tonic-gate int doorfd = -1; 14690Sstevel@tonic-gate int err, ret = -1; 14700Sstevel@tonic-gate struct stat st; 14710Sstevel@tonic-gate struct flock flock; 14720Sstevel@tonic-gate zone_state_t zstate; 14730Sstevel@tonic-gate 14740Sstevel@tonic-gate top: 14750Sstevel@tonic-gate if ((err = zone_get_state(zone_name, &zstate)) != Z_OK) { 14762267Sdp zerror(zlogp, B_FALSE, "failed to get zone state: %s", 14770Sstevel@tonic-gate zonecfg_strerror(err)); 14780Sstevel@tonic-gate goto out; 14790Sstevel@tonic-gate } 14800Sstevel@tonic-gate if ((doorfd = open(zone_door_path, O_CREAT|O_RDWR, 14810Sstevel@tonic-gate S_IREAD|S_IWRITE)) < 0) { 14820Sstevel@tonic-gate zerror(zlogp, B_TRUE, "failed to open %s", zone_door_path); 14830Sstevel@tonic-gate goto out; 14840Sstevel@tonic-gate } 14850Sstevel@tonic-gate if (fstat(doorfd, &st) < 0) { 14860Sstevel@tonic-gate zerror(zlogp, B_TRUE, "failed to stat %s", zone_door_path); 14870Sstevel@tonic-gate goto out; 14880Sstevel@tonic-gate } 14890Sstevel@tonic-gate /* 14900Sstevel@tonic-gate * Lock the file to synchronize with other zoneadmd 14910Sstevel@tonic-gate */ 14920Sstevel@tonic-gate flock.l_type = F_WRLCK; 14930Sstevel@tonic-gate flock.l_whence = SEEK_SET; 14940Sstevel@tonic-gate flock.l_start = (off_t)0; 14950Sstevel@tonic-gate flock.l_len = (off_t)0; 14960Sstevel@tonic-gate if (fcntl(doorfd, F_SETLK, &flock) < 0) { 14970Sstevel@tonic-gate /* 14980Sstevel@tonic-gate * Someone else raced us here and grabbed the lock file 14990Sstevel@tonic-gate * first. A warning here is inappropriate since nothing 15000Sstevel@tonic-gate * went wrong. 15010Sstevel@tonic-gate */ 15020Sstevel@tonic-gate goto out; 15030Sstevel@tonic-gate } 15040Sstevel@tonic-gate 15050Sstevel@tonic-gate if (strcmp(st.st_fstype, "namefs") == 0) { 15060Sstevel@tonic-gate struct door_info info; 15070Sstevel@tonic-gate 15080Sstevel@tonic-gate /* 15090Sstevel@tonic-gate * There is already something fattach()'ed to this file. 15100Sstevel@tonic-gate * Lets see what the door is up to. 15110Sstevel@tonic-gate */ 15120Sstevel@tonic-gate if (door_info(doorfd, &info) == 0 && info.di_target != -1) { 15130Sstevel@tonic-gate /* 15140Sstevel@tonic-gate * Another zoneadmd process seems to be in 15150Sstevel@tonic-gate * control of the situation and we don't need to 15160Sstevel@tonic-gate * be here. A warning here is inappropriate 15170Sstevel@tonic-gate * since nothing went wrong. 15180Sstevel@tonic-gate * 15190Sstevel@tonic-gate * If the door has been revoked, the zoneadmd 15200Sstevel@tonic-gate * process currently managing the zone is going 15210Sstevel@tonic-gate * away. We'll return control to zoneadm(1m) 15220Sstevel@tonic-gate * which will try again (by which time zoneadmd 15230Sstevel@tonic-gate * will hopefully have exited). 15240Sstevel@tonic-gate */ 15250Sstevel@tonic-gate goto out; 15260Sstevel@tonic-gate } 15270Sstevel@tonic-gate 15280Sstevel@tonic-gate /* 15290Sstevel@tonic-gate * If we got this far, there's a fattach(3c)'ed door 15300Sstevel@tonic-gate * that belongs to a process that has exited, which can 15310Sstevel@tonic-gate * happen if the previous zoneadmd died unexpectedly. 15320Sstevel@tonic-gate * 15330Sstevel@tonic-gate * Let user know that something is amiss, but that we can 15340Sstevel@tonic-gate * recover; if the zone is in the installed state, then don't 15350Sstevel@tonic-gate * message, since having a running zoneadmd isn't really 15360Sstevel@tonic-gate * expected/needed. We want to keep occurences of this message 15370Sstevel@tonic-gate * limited to times when zoneadmd is picking back up from a 15380Sstevel@tonic-gate * zoneadmd that died while the zone was in some non-trivial 15390Sstevel@tonic-gate * state. 15400Sstevel@tonic-gate */ 15410Sstevel@tonic-gate if (zstate > ZONE_STATE_INSTALLED) { 15420Sstevel@tonic-gate zerror(zlogp, B_FALSE, 15430Sstevel@tonic-gate "zone '%s': WARNING: zone is in state '%s', but " 15440Sstevel@tonic-gate "zoneadmd does not appear to be available; " 15450Sstevel@tonic-gate "restarted zoneadmd to recover.", 15460Sstevel@tonic-gate zone_name, zone_state_str(zstate)); 15470Sstevel@tonic-gate } 15480Sstevel@tonic-gate 15490Sstevel@tonic-gate (void) fdetach(zone_door_path); 15500Sstevel@tonic-gate (void) close(doorfd); 15510Sstevel@tonic-gate goto top; 15520Sstevel@tonic-gate } 15530Sstevel@tonic-gate ret = 0; 15540Sstevel@tonic-gate out: 15550Sstevel@tonic-gate (void) close(doorfd); 15560Sstevel@tonic-gate return (ret); 15570Sstevel@tonic-gate } 15580Sstevel@tonic-gate 15597370S<Gerald Jelinek> /* 15607370S<Gerald Jelinek> * Setup the brand's pre and post state change callbacks, as well as the 15617370S<Gerald Jelinek> * query callback, if any of these exist. 15627370S<Gerald Jelinek> */ 15637370S<Gerald Jelinek> static int 15647370S<Gerald Jelinek> brand_callback_init(brand_handle_t bh, char *zone_name) 15657370S<Gerald Jelinek> { 15667370S<Gerald Jelinek> char zpath[MAXPATHLEN]; 15677370S<Gerald Jelinek> 15687370S<Gerald Jelinek> if (zone_get_zonepath(zone_name, zpath, sizeof (zpath)) != Z_OK) 15697370S<Gerald Jelinek> return (-1); 15707370S<Gerald Jelinek> 15717370S<Gerald Jelinek> (void) strlcpy(pre_statechg_hook, EXEC_PREFIX, 15727370S<Gerald Jelinek> sizeof (pre_statechg_hook)); 15737370S<Gerald Jelinek> 15747370S<Gerald Jelinek> if (brand_get_prestatechange(bh, zone_name, zpath, 15757370S<Gerald Jelinek> pre_statechg_hook + EXEC_LEN, 15767370S<Gerald Jelinek> sizeof (pre_statechg_hook) - EXEC_LEN) != 0) 15777370S<Gerald Jelinek> return (-1); 15787370S<Gerald Jelinek> 15797370S<Gerald Jelinek> if (strlen(pre_statechg_hook) <= EXEC_LEN) 15807370S<Gerald Jelinek> pre_statechg_hook[0] = '\0'; 15817370S<Gerald Jelinek> 15827370S<Gerald Jelinek> (void) strlcpy(post_statechg_hook, EXEC_PREFIX, 15837370S<Gerald Jelinek> sizeof (post_statechg_hook)); 15847370S<Gerald Jelinek> 15857370S<Gerald Jelinek> if (brand_get_poststatechange(bh, zone_name, zpath, 15867370S<Gerald Jelinek> post_statechg_hook + EXEC_LEN, 15877370S<Gerald Jelinek> sizeof (post_statechg_hook) - EXEC_LEN) != 0) 15887370S<Gerald Jelinek> return (-1); 15897370S<Gerald Jelinek> 15907370S<Gerald Jelinek> if (strlen(post_statechg_hook) <= EXEC_LEN) 15917370S<Gerald Jelinek> post_statechg_hook[0] = '\0'; 15927370S<Gerald Jelinek> 15937370S<Gerald Jelinek> (void) strlcpy(query_hook, EXEC_PREFIX, 15947370S<Gerald Jelinek> sizeof (query_hook)); 15957370S<Gerald Jelinek> 15967370S<Gerald Jelinek> if (brand_get_query(bh, zone_name, zpath, query_hook + EXEC_LEN, 15977370S<Gerald Jelinek> sizeof (query_hook) - EXEC_LEN) != 0) 15987370S<Gerald Jelinek> return (-1); 15997370S<Gerald Jelinek> 16007370S<Gerald Jelinek> if (strlen(query_hook) <= EXEC_LEN) 16017370S<Gerald Jelinek> query_hook[0] = '\0'; 16027370S<Gerald Jelinek> 16037370S<Gerald Jelinek> return (0); 16047370S<Gerald Jelinek> } 16057370S<Gerald Jelinek> 16060Sstevel@tonic-gate int 16070Sstevel@tonic-gate main(int argc, char *argv[]) 16080Sstevel@tonic-gate { 16090Sstevel@tonic-gate int opt; 16100Sstevel@tonic-gate zoneid_t zid; 16110Sstevel@tonic-gate priv_set_t *privset; 16120Sstevel@tonic-gate zone_state_t zstate; 16130Sstevel@tonic-gate char parents_locale[MAXPATHLEN]; 16142727Sedp brand_handle_t bh; 16150Sstevel@tonic-gate int err; 16160Sstevel@tonic-gate 16170Sstevel@tonic-gate pid_t pid; 16180Sstevel@tonic-gate sigset_t blockset; 16190Sstevel@tonic-gate sigset_t block_cld; 16200Sstevel@tonic-gate 16210Sstevel@tonic-gate struct { 16220Sstevel@tonic-gate sema_t sem; 16230Sstevel@tonic-gate int status; 16240Sstevel@tonic-gate zlog_t log; 16250Sstevel@tonic-gate } *shstate; 16260Sstevel@tonic-gate size_t shstatelen = getpagesize(); 16270Sstevel@tonic-gate 16280Sstevel@tonic-gate zlog_t errlog; 16290Sstevel@tonic-gate zlog_t *zlogp; 16300Sstevel@tonic-gate 16312382Sdp int ctfd; 16322382Sdp 16330Sstevel@tonic-gate progname = get_execbasename(argv[0]); 16340Sstevel@tonic-gate 16350Sstevel@tonic-gate /* 16360Sstevel@tonic-gate * Make sure stderr is unbuffered 16370Sstevel@tonic-gate */ 16380Sstevel@tonic-gate (void) setbuffer(stderr, NULL, 0); 16390Sstevel@tonic-gate 16400Sstevel@tonic-gate /* 16410Sstevel@tonic-gate * Get out of the way of mounted filesystems, since we will daemonize 16420Sstevel@tonic-gate * soon. 16430Sstevel@tonic-gate */ 16440Sstevel@tonic-gate (void) chdir("/"); 16450Sstevel@tonic-gate 16460Sstevel@tonic-gate /* 16470Sstevel@tonic-gate * Use the default system umask per PSARC 1998/110 rather than 16480Sstevel@tonic-gate * anything that may have been set by the caller. 16490Sstevel@tonic-gate */ 16500Sstevel@tonic-gate (void) umask(CMASK); 16510Sstevel@tonic-gate 16520Sstevel@tonic-gate /* 16530Sstevel@tonic-gate * Initially we want to use our parent's locale. 16540Sstevel@tonic-gate */ 16550Sstevel@tonic-gate (void) setlocale(LC_ALL, ""); 16560Sstevel@tonic-gate (void) textdomain(TEXT_DOMAIN); 16570Sstevel@tonic-gate (void) strlcpy(parents_locale, setlocale(LC_MESSAGES, NULL), 16580Sstevel@tonic-gate sizeof (parents_locale)); 16590Sstevel@tonic-gate 16600Sstevel@tonic-gate /* 16610Sstevel@tonic-gate * This zlog_t is used for writing to stderr 16620Sstevel@tonic-gate */ 16630Sstevel@tonic-gate errlog.logfile = stderr; 16640Sstevel@tonic-gate errlog.buflen = errlog.loglen = 0; 16650Sstevel@tonic-gate errlog.buf = errlog.log = NULL; 16660Sstevel@tonic-gate errlog.locale = parents_locale; 16670Sstevel@tonic-gate 16680Sstevel@tonic-gate /* 16690Sstevel@tonic-gate * We start off writing to stderr until we're ready to daemonize. 16700Sstevel@tonic-gate */ 16710Sstevel@tonic-gate zlogp = &errlog; 16720Sstevel@tonic-gate 16730Sstevel@tonic-gate /* 16740Sstevel@tonic-gate * Process options. 16750Sstevel@tonic-gate */ 1676766Scarlsonj while ((opt = getopt(argc, argv, "R:z:")) != EOF) { 16770Sstevel@tonic-gate switch (opt) { 1678766Scarlsonj case 'R': 1679766Scarlsonj zonecfg_set_root(optarg); 1680766Scarlsonj break; 16810Sstevel@tonic-gate case 'z': 16820Sstevel@tonic-gate zone_name = optarg; 16830Sstevel@tonic-gate break; 16840Sstevel@tonic-gate default: 16850Sstevel@tonic-gate usage(); 16860Sstevel@tonic-gate } 16870Sstevel@tonic-gate } 16880Sstevel@tonic-gate 16890Sstevel@tonic-gate if (zone_name == NULL) 16900Sstevel@tonic-gate usage(); 16910Sstevel@tonic-gate 16920Sstevel@tonic-gate /* 16930Sstevel@tonic-gate * Because usage() prints directly to stderr, it has gettext() 16940Sstevel@tonic-gate * wrapping, which depends on the locale. But since zerror() calls 16950Sstevel@tonic-gate * localize() which tweaks the locale, it is not safe to call zerror() 16960Sstevel@tonic-gate * until after the last call to usage(). Fortunately, the last call 16970Sstevel@tonic-gate * to usage() is just above and the first call to zerror() is just 16980Sstevel@tonic-gate * below. Don't mess this up. 16990Sstevel@tonic-gate */ 17000Sstevel@tonic-gate if (strcmp(zone_name, GLOBAL_ZONENAME) == 0) { 17010Sstevel@tonic-gate zerror(zlogp, B_FALSE, "cannot manage the %s zone", 17020Sstevel@tonic-gate GLOBAL_ZONENAME); 17030Sstevel@tonic-gate return (1); 17040Sstevel@tonic-gate } 17050Sstevel@tonic-gate 17060Sstevel@tonic-gate if (zone_get_id(zone_name, &zid) != 0) { 17072267Sdp zerror(zlogp, B_FALSE, "could not manage %s: %s", zone_name, 17080Sstevel@tonic-gate zonecfg_strerror(Z_NO_ZONE)); 17090Sstevel@tonic-gate return (1); 17100Sstevel@tonic-gate } 17110Sstevel@tonic-gate 17120Sstevel@tonic-gate if ((err = zone_get_state(zone_name, &zstate)) != Z_OK) { 17132267Sdp zerror(zlogp, B_FALSE, "failed to get zone state: %s", 17140Sstevel@tonic-gate zonecfg_strerror(err)); 17150Sstevel@tonic-gate return (1); 17160Sstevel@tonic-gate } 17172712Snn35248 if (zstate < ZONE_STATE_INCOMPLETE) { 17180Sstevel@tonic-gate zerror(zlogp, B_FALSE, 17190Sstevel@tonic-gate "cannot manage a zone which is in state '%s'", 17200Sstevel@tonic-gate zone_state_str(zstate)); 17210Sstevel@tonic-gate return (1); 17220Sstevel@tonic-gate } 17230Sstevel@tonic-gate 17242712Snn35248 /* Get a handle to the brand info for this zone */ 17252712Snn35248 if ((zone_get_brand(zone_name, brand_name, sizeof (brand_name)) 17262727Sedp != Z_OK) || (bh = brand_open(brand_name)) == NULL) { 17272712Snn35248 zerror(zlogp, B_FALSE, "unable to determine zone brand"); 17282712Snn35248 return (1); 17292712Snn35248 } 17302727Sedp zone_isnative = brand_is_native(bh); 17314350Std153743 zone_iscluster = (strcmp(brand_name, CLUSTER_BRAND_NAME) == 0); 17328905SRic.Aleshire@Sun.COM zone_islabeled = (strcmp(brand_name, LABELED_BRAND_NAME) == 0); 17337370S<Gerald Jelinek> 17347370S<Gerald Jelinek> /* Get state change brand hooks. */ 17357370S<Gerald Jelinek> if (brand_callback_init(bh, zone_name) == -1) { 17367370S<Gerald Jelinek> zerror(zlogp, B_TRUE, 17377370S<Gerald Jelinek> "failed to initialize brand state change hooks"); 17387370S<Gerald Jelinek> brand_close(bh); 17397370S<Gerald Jelinek> return (1); 17407370S<Gerald Jelinek> } 17417370S<Gerald Jelinek> 17422727Sedp brand_close(bh); 17432712Snn35248 17440Sstevel@tonic-gate /* 17450Sstevel@tonic-gate * Check that we have all privileges. It would be nice to pare 17460Sstevel@tonic-gate * this down, but this is at least a first cut. 17470Sstevel@tonic-gate */ 17480Sstevel@tonic-gate if ((privset = priv_allocset()) == NULL) { 17490Sstevel@tonic-gate zerror(zlogp, B_TRUE, "%s failed", "priv_allocset"); 17500Sstevel@tonic-gate return (1); 17510Sstevel@tonic-gate } 17520Sstevel@tonic-gate 17530Sstevel@tonic-gate if (getppriv(PRIV_EFFECTIVE, privset) != 0) { 17540Sstevel@tonic-gate zerror(zlogp, B_TRUE, "%s failed", "getppriv"); 17550Sstevel@tonic-gate priv_freeset(privset); 17560Sstevel@tonic-gate return (1); 17570Sstevel@tonic-gate } 17580Sstevel@tonic-gate 17590Sstevel@tonic-gate if (priv_isfullset(privset) == B_FALSE) { 17600Sstevel@tonic-gate zerror(zlogp, B_FALSE, "You lack sufficient privilege to " 17612267Sdp "run this command (all privs required)"); 17620Sstevel@tonic-gate priv_freeset(privset); 17630Sstevel@tonic-gate return (1); 17640Sstevel@tonic-gate } 17650Sstevel@tonic-gate priv_freeset(privset); 17660Sstevel@tonic-gate 17670Sstevel@tonic-gate if (mkzonedir(zlogp) != 0) 17680Sstevel@tonic-gate return (1); 17690Sstevel@tonic-gate 17700Sstevel@tonic-gate /* 17710Sstevel@tonic-gate * Pre-fork: setup shared state 17720Sstevel@tonic-gate */ 17730Sstevel@tonic-gate if ((shstate = (void *)mmap(NULL, shstatelen, 17740Sstevel@tonic-gate PROT_READ|PROT_WRITE, MAP_SHARED|MAP_ANON, -1, (off_t)0)) == 17750Sstevel@tonic-gate MAP_FAILED) { 17760Sstevel@tonic-gate zerror(zlogp, B_TRUE, "%s failed", "mmap"); 17770Sstevel@tonic-gate return (1); 17780Sstevel@tonic-gate } 17790Sstevel@tonic-gate if (sema_init(&shstate->sem, 0, USYNC_PROCESS, NULL) != 0) { 17800Sstevel@tonic-gate zerror(zlogp, B_TRUE, "%s failed", "sema_init()"); 17810Sstevel@tonic-gate (void) munmap((char *)shstate, shstatelen); 17820Sstevel@tonic-gate return (1); 17830Sstevel@tonic-gate } 17840Sstevel@tonic-gate shstate->log.logfile = NULL; 17850Sstevel@tonic-gate shstate->log.buflen = shstatelen - sizeof (*shstate); 17860Sstevel@tonic-gate shstate->log.loglen = shstate->log.buflen; 17870Sstevel@tonic-gate shstate->log.buf = (char *)shstate + sizeof (*shstate); 17880Sstevel@tonic-gate shstate->log.log = shstate->log.buf; 17890Sstevel@tonic-gate shstate->log.locale = parents_locale; 17900Sstevel@tonic-gate shstate->status = -1; 17910Sstevel@tonic-gate 17920Sstevel@tonic-gate /* 17930Sstevel@tonic-gate * We need a SIGCHLD handler so the sema_wait() below will wake 17940Sstevel@tonic-gate * up if the child dies without doing a sema_post(). 17950Sstevel@tonic-gate */ 17960Sstevel@tonic-gate (void) sigset(SIGCHLD, sigchld); 17970Sstevel@tonic-gate /* 17980Sstevel@tonic-gate * We must mask SIGCHLD until after we've coped with the fork 17990Sstevel@tonic-gate * sufficiently to deal with it; otherwise we can race and 18000Sstevel@tonic-gate * receive the signal before pid has been initialized 18010Sstevel@tonic-gate * (yes, this really happens). 18020Sstevel@tonic-gate */ 18030Sstevel@tonic-gate (void) sigemptyset(&block_cld); 18040Sstevel@tonic-gate (void) sigaddset(&block_cld, SIGCHLD); 18050Sstevel@tonic-gate (void) sigprocmask(SIG_BLOCK, &block_cld, NULL); 18060Sstevel@tonic-gate 18072382Sdp if ((ctfd = init_template()) == -1) { 18082382Sdp zerror(zlogp, B_TRUE, "failed to create contract"); 18092382Sdp return (1); 18102382Sdp } 18112382Sdp 18120Sstevel@tonic-gate /* 18130Sstevel@tonic-gate * Do not let another thread localize a message while we are forking. 18140Sstevel@tonic-gate */ 18150Sstevel@tonic-gate (void) mutex_lock(&msglock); 18160Sstevel@tonic-gate pid = fork(); 18170Sstevel@tonic-gate (void) mutex_unlock(&msglock); 18182382Sdp 18192382Sdp /* 18202382Sdp * In all cases (parent, child, and in the event of an error) we 18212382Sdp * don't want to cause creation of contracts on subsequent fork()s. 18222382Sdp */ 18232382Sdp (void) ct_tmpl_clear(ctfd); 18242382Sdp (void) close(ctfd); 18252382Sdp 18260Sstevel@tonic-gate if (pid == -1) { 18270Sstevel@tonic-gate zerror(zlogp, B_TRUE, "could not fork"); 18280Sstevel@tonic-gate return (1); 18290Sstevel@tonic-gate 18300Sstevel@tonic-gate } else if (pid > 0) { /* parent */ 18310Sstevel@tonic-gate (void) sigprocmask(SIG_UNBLOCK, &block_cld, NULL); 18320Sstevel@tonic-gate /* 18330Sstevel@tonic-gate * This marks a window of vulnerability in which we receive 18340Sstevel@tonic-gate * the SIGCLD before falling into sema_wait (normally we would 18350Sstevel@tonic-gate * get woken up from sema_wait with EINTR upon receipt of 18360Sstevel@tonic-gate * SIGCLD). So we may need to use some other scheme like 18370Sstevel@tonic-gate * sema_posting in the sigcld handler. 18380Sstevel@tonic-gate * blech 18390Sstevel@tonic-gate */ 18400Sstevel@tonic-gate (void) sema_wait(&shstate->sem); 18410Sstevel@tonic-gate (void) sema_destroy(&shstate->sem); 18420Sstevel@tonic-gate if (shstate->status != 0) 18430Sstevel@tonic-gate (void) waitpid(pid, NULL, WNOHANG); 18440Sstevel@tonic-gate /* 18450Sstevel@tonic-gate * It's ok if we die with SIGPIPE. It's not like we could have 18460Sstevel@tonic-gate * done anything about it. 18470Sstevel@tonic-gate */ 18480Sstevel@tonic-gate (void) fprintf(stderr, "%s", shstate->log.buf); 18490Sstevel@tonic-gate _exit(shstate->status == 0 ? 0 : 1); 18500Sstevel@tonic-gate } 18510Sstevel@tonic-gate 18520Sstevel@tonic-gate /* 18530Sstevel@tonic-gate * The child charges on. 18540Sstevel@tonic-gate */ 18550Sstevel@tonic-gate (void) sigset(SIGCHLD, SIG_DFL); 18560Sstevel@tonic-gate (void) sigprocmask(SIG_UNBLOCK, &block_cld, NULL); 18570Sstevel@tonic-gate 18580Sstevel@tonic-gate /* 18590Sstevel@tonic-gate * SIGPIPE can be delivered if we write to a socket for which the 18600Sstevel@tonic-gate * peer endpoint is gone. That can lead to too-early termination 18610Sstevel@tonic-gate * of zoneadmd, and that's not good eats. 18620Sstevel@tonic-gate */ 18630Sstevel@tonic-gate (void) sigset(SIGPIPE, SIG_IGN); 18640Sstevel@tonic-gate /* 18650Sstevel@tonic-gate * Stop using stderr 18660Sstevel@tonic-gate */ 18670Sstevel@tonic-gate zlogp = &shstate->log; 18680Sstevel@tonic-gate 18690Sstevel@tonic-gate /* 18700Sstevel@tonic-gate * We don't need stdout/stderr from now on. 18710Sstevel@tonic-gate */ 18720Sstevel@tonic-gate closefrom(0); 18730Sstevel@tonic-gate 18740Sstevel@tonic-gate /* 18750Sstevel@tonic-gate * Initialize the syslog zlog_t. This needs to be done after 18760Sstevel@tonic-gate * the call to closefrom(). 18770Sstevel@tonic-gate */ 18780Sstevel@tonic-gate logsys.buf = logsys.log = NULL; 18790Sstevel@tonic-gate logsys.buflen = logsys.loglen = 0; 18800Sstevel@tonic-gate logsys.logfile = NULL; 18810Sstevel@tonic-gate logsys.locale = DEFAULT_LOCALE; 18820Sstevel@tonic-gate 18830Sstevel@tonic-gate openlog("zoneadmd", LOG_PID, LOG_DAEMON); 18840Sstevel@tonic-gate 18850Sstevel@tonic-gate /* 18860Sstevel@tonic-gate * The eventstream is used to publish state changes in the zone 18870Sstevel@tonic-gate * from the door threads to the console I/O poller. 18880Sstevel@tonic-gate */ 18890Sstevel@tonic-gate if (eventstream_init() == -1) { 18900Sstevel@tonic-gate zerror(zlogp, B_TRUE, "unable to create eventstream"); 18910Sstevel@tonic-gate goto child_out; 18920Sstevel@tonic-gate } 18930Sstevel@tonic-gate 18940Sstevel@tonic-gate (void) snprintf(zone_door_path, sizeof (zone_door_path), 1895766Scarlsonj "%s" ZONE_DOOR_PATH, zonecfg_get_root(), zone_name); 18960Sstevel@tonic-gate 18970Sstevel@tonic-gate /* 18980Sstevel@tonic-gate * See if another zoneadmd is running for this zone. If not, then we 18990Sstevel@tonic-gate * can now modify system state. 19000Sstevel@tonic-gate */ 19010Sstevel@tonic-gate if (make_daemon_exclusive(zlogp) == -1) 19020Sstevel@tonic-gate goto child_out; 19030Sstevel@tonic-gate 19040Sstevel@tonic-gate 19050Sstevel@tonic-gate /* 19060Sstevel@tonic-gate * Create/join a new session; we need to be careful of what we do with 19070Sstevel@tonic-gate * the console from now on so we don't end up being the session leader 19080Sstevel@tonic-gate * for the terminal we're going to be handing out. 19090Sstevel@tonic-gate */ 19100Sstevel@tonic-gate (void) setsid(); 19110Sstevel@tonic-gate 19120Sstevel@tonic-gate /* 19130Sstevel@tonic-gate * This thread shouldn't be receiving any signals; in particular, 19140Sstevel@tonic-gate * SIGCHLD should be received by the thread doing the fork(). 19150Sstevel@tonic-gate */ 19160Sstevel@tonic-gate (void) sigfillset(&blockset); 19170Sstevel@tonic-gate (void) thr_sigsetmask(SIG_BLOCK, &blockset, NULL); 19180Sstevel@tonic-gate 19190Sstevel@tonic-gate /* 19200Sstevel@tonic-gate * Setup the console device and get ready to serve the console; 19210Sstevel@tonic-gate * once this has completed, we're ready to let console clients 19220Sstevel@tonic-gate * make an attempt to connect (they will block until 19230Sstevel@tonic-gate * serve_console_sock() below gets called, and any pending 19240Sstevel@tonic-gate * connection is accept()ed). 19250Sstevel@tonic-gate */ 19268770SJordan.Vaughan@Sun.com if (!zonecfg_in_alt_root() && init_console(zlogp) < 0) 19270Sstevel@tonic-gate goto child_out; 19280Sstevel@tonic-gate 19290Sstevel@tonic-gate /* 19300Sstevel@tonic-gate * Take the lock now, so that when the door server gets going, we 19310Sstevel@tonic-gate * are guaranteed that it won't take a request until we are sure 19320Sstevel@tonic-gate * that everything is completely set up. See the child_out: label 19330Sstevel@tonic-gate * below to see why this matters. 19340Sstevel@tonic-gate */ 19350Sstevel@tonic-gate (void) mutex_lock(&lock); 19360Sstevel@tonic-gate 1937766Scarlsonj /* Init semaphore for scratch zones. */ 1938766Scarlsonj if (sema_init(&scratch_sem, 0, USYNC_THREAD, NULL) == -1) { 1939766Scarlsonj zerror(zlogp, B_TRUE, 1940766Scarlsonj "failed to initialize semaphore for scratch zone"); 1941766Scarlsonj goto child_out; 1942766Scarlsonj } 1943766Scarlsonj 19448453SAnurag.Maskey@Sun.COM /* open the dladm handle */ 19458453SAnurag.Maskey@Sun.COM if (dladm_open(&dld_handle) != DLADM_STATUS_OK) { 19468453SAnurag.Maskey@Sun.COM zerror(zlogp, B_FALSE, "failed to open dladm handle"); 19478453SAnurag.Maskey@Sun.COM goto child_out; 19488453SAnurag.Maskey@Sun.COM } 19498453SAnurag.Maskey@Sun.COM 19500Sstevel@tonic-gate /* 19510Sstevel@tonic-gate * Note: door setup must occur *after* the console is setup. 19520Sstevel@tonic-gate * This is so that as zlogin tests the door to see if zoneadmd 19530Sstevel@tonic-gate * is ready yet, we know that the console will get serviced 19540Sstevel@tonic-gate * once door_info() indicates that the door is "up". 19550Sstevel@tonic-gate */ 19560Sstevel@tonic-gate if (setup_door(zlogp) == -1) 19570Sstevel@tonic-gate goto child_out; 19580Sstevel@tonic-gate 19590Sstevel@tonic-gate /* 19600Sstevel@tonic-gate * Things seem OK so far; tell the parent process that we're done 19610Sstevel@tonic-gate * with setup tasks. This will cause the parent to exit, signalling 19620Sstevel@tonic-gate * to zoneadm, zlogin, or whatever forked it that we are ready to 19630Sstevel@tonic-gate * service requests. 19640Sstevel@tonic-gate */ 19650Sstevel@tonic-gate shstate->status = 0; 19660Sstevel@tonic-gate (void) sema_post(&shstate->sem); 19670Sstevel@tonic-gate (void) munmap((char *)shstate, shstatelen); 19680Sstevel@tonic-gate shstate = NULL; 19690Sstevel@tonic-gate 19700Sstevel@tonic-gate (void) mutex_unlock(&lock); 19710Sstevel@tonic-gate 19720Sstevel@tonic-gate /* 19730Sstevel@tonic-gate * zlogp is now invalid, so reset it to the syslog logger. 19740Sstevel@tonic-gate */ 19750Sstevel@tonic-gate zlogp = &logsys; 19760Sstevel@tonic-gate 19770Sstevel@tonic-gate /* 19780Sstevel@tonic-gate * Now that we are free of any parents, switch to the default locale. 19790Sstevel@tonic-gate */ 19800Sstevel@tonic-gate (void) setlocale(LC_ALL, DEFAULT_LOCALE); 19810Sstevel@tonic-gate 19820Sstevel@tonic-gate /* 19830Sstevel@tonic-gate * At this point the setup portion of main() is basically done, so 19840Sstevel@tonic-gate * we reuse this thread to manage the zone console. When 19850Sstevel@tonic-gate * serve_console() has returned, we are past the point of no return 19860Sstevel@tonic-gate * in the life of this zoneadmd. 19870Sstevel@tonic-gate */ 1988766Scarlsonj if (zonecfg_in_alt_root()) { 1989766Scarlsonj /* 1990766Scarlsonj * This is just awful, but mounted scratch zones don't (and 1991766Scarlsonj * can't) have consoles. We just wait for unmount instead. 1992766Scarlsonj */ 1993766Scarlsonj while (sema_wait(&scratch_sem) == EINTR) 1994766Scarlsonj ; 1995766Scarlsonj } else { 1996766Scarlsonj serve_console(zlogp); 1997766Scarlsonj assert(in_death_throes); 1998766Scarlsonj } 19990Sstevel@tonic-gate 20000Sstevel@tonic-gate /* 20010Sstevel@tonic-gate * This is the next-to-last part of the exit interlock. Upon calling 20020Sstevel@tonic-gate * fdetach(), the door will go unreferenced; once any 20030Sstevel@tonic-gate * outstanding requests (like the door thread doing Z_HALT) are 20040Sstevel@tonic-gate * done, the door will get an UNREF notification; when it handles 20050Sstevel@tonic-gate * the UNREF, the door server will cause the exit. 20060Sstevel@tonic-gate */ 20070Sstevel@tonic-gate assert(!MUTEX_HELD(&lock)); 20080Sstevel@tonic-gate (void) fdetach(zone_door_path); 20098453SAnurag.Maskey@Sun.COM 20100Sstevel@tonic-gate for (;;) 20110Sstevel@tonic-gate (void) pause(); 20120Sstevel@tonic-gate 20130Sstevel@tonic-gate child_out: 20140Sstevel@tonic-gate assert(pid == 0); 20150Sstevel@tonic-gate if (shstate != NULL) { 20160Sstevel@tonic-gate shstate->status = -1; 20170Sstevel@tonic-gate (void) sema_post(&shstate->sem); 20180Sstevel@tonic-gate (void) munmap((char *)shstate, shstatelen); 20190Sstevel@tonic-gate } 20200Sstevel@tonic-gate 20210Sstevel@tonic-gate /* 20220Sstevel@tonic-gate * This might trigger an unref notification, but if so, 20230Sstevel@tonic-gate * we are still holding the lock, so our call to exit will 20240Sstevel@tonic-gate * ultimately win the race and will publish the right exit 20250Sstevel@tonic-gate * code. 20260Sstevel@tonic-gate */ 20270Sstevel@tonic-gate if (zone_door != -1) { 20280Sstevel@tonic-gate assert(MUTEX_HELD(&lock)); 20290Sstevel@tonic-gate (void) door_revoke(zone_door); 20300Sstevel@tonic-gate (void) fdetach(zone_door_path); 20310Sstevel@tonic-gate } 20328453SAnurag.Maskey@Sun.COM 20338453SAnurag.Maskey@Sun.COM if (dld_handle != NULL) 20348453SAnurag.Maskey@Sun.COM dladm_close(dld_handle); 20358453SAnurag.Maskey@Sun.COM 20360Sstevel@tonic-gate return (1); /* return from main() forcibly exits an MT process */ 20370Sstevel@tonic-gate } 2038