10Sstevel@tonic-gate /* 20Sstevel@tonic-gate * CDDL HEADER START 30Sstevel@tonic-gate * 40Sstevel@tonic-gate * The contents of this file are subject to the terms of the 51645Scomay * Common Development and Distribution License (the "License"). 61645Scomay * You may not use this file except in compliance with the License. 70Sstevel@tonic-gate * 80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 100Sstevel@tonic-gate * See the License for the specific language governing permissions 110Sstevel@tonic-gate * and limitations under the License. 120Sstevel@tonic-gate * 130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 180Sstevel@tonic-gate * 190Sstevel@tonic-gate * CDDL HEADER END 200Sstevel@tonic-gate */ 211645Scomay 220Sstevel@tonic-gate /* 235829Sgjelinek * Copyright 2008 Sun Microsystems, Inc. All rights reserved. 240Sstevel@tonic-gate * Use is subject to license terms. 250Sstevel@tonic-gate */ 260Sstevel@tonic-gate 270Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 280Sstevel@tonic-gate 290Sstevel@tonic-gate /* 300Sstevel@tonic-gate * zoneadmd manages zones; one zoneadmd process is launched for each 310Sstevel@tonic-gate * non-global zone on the system. This daemon juggles four jobs: 320Sstevel@tonic-gate * 330Sstevel@tonic-gate * - Implement setup and teardown of the zone "virtual platform": mount and 340Sstevel@tonic-gate * unmount filesystems; create and destroy network interfaces; communicate 350Sstevel@tonic-gate * with devfsadmd to lay out devices for the zone; instantiate the zone 360Sstevel@tonic-gate * console device; configure process runtime attributes such as resource 370Sstevel@tonic-gate * controls, pool bindings, fine-grained privileges. 380Sstevel@tonic-gate * 390Sstevel@tonic-gate * - Launch the zone's init(1M) process. 400Sstevel@tonic-gate * 410Sstevel@tonic-gate * - Implement a door server; clients (like zoneadm) connect to the door 420Sstevel@tonic-gate * server and request zone state changes. The kernel is also a client of 430Sstevel@tonic-gate * this door server. A request to halt or reboot the zone which originates 440Sstevel@tonic-gate * *inside* the zone results in a door upcall from the kernel into zoneadmd. 450Sstevel@tonic-gate * 460Sstevel@tonic-gate * One minor problem is that messages emitted by zoneadmd need to be passed 470Sstevel@tonic-gate * back to the zoneadm process making the request. These messages need to 480Sstevel@tonic-gate * be rendered in the client's locale; so, this is passed in as part of the 490Sstevel@tonic-gate * request. The exception is the kernel upcall to zoneadmd, in which case 500Sstevel@tonic-gate * messages are syslog'd. 510Sstevel@tonic-gate * 520Sstevel@tonic-gate * To make all of this work, the Makefile adds -a to xgettext to extract *all* 530Sstevel@tonic-gate * strings, and an exclusion file (zoneadmd.xcl) is used to exclude those 540Sstevel@tonic-gate * strings which do not need to be translated. 550Sstevel@tonic-gate * 560Sstevel@tonic-gate * - Act as a console server for zlogin -C processes; see comments in zcons.c 570Sstevel@tonic-gate * for more information about the zone console architecture. 580Sstevel@tonic-gate * 590Sstevel@tonic-gate * DESIGN NOTES 600Sstevel@tonic-gate * 610Sstevel@tonic-gate * Restart: 620Sstevel@tonic-gate * A chief design constraint of zoneadmd is that it should be restartable in 630Sstevel@tonic-gate * the case that the administrator kills it off, or it suffers a fatal error, 640Sstevel@tonic-gate * without the running zone being impacted; this is akin to being able to 650Sstevel@tonic-gate * reboot the service processor of a server without affecting the OS instance. 660Sstevel@tonic-gate */ 670Sstevel@tonic-gate 680Sstevel@tonic-gate #include <sys/param.h> 690Sstevel@tonic-gate #include <sys/mman.h> 700Sstevel@tonic-gate #include <sys/types.h> 710Sstevel@tonic-gate #include <sys/stat.h> 720Sstevel@tonic-gate #include <sys/sysmacros.h> 730Sstevel@tonic-gate 740Sstevel@tonic-gate #include <bsm/adt.h> 750Sstevel@tonic-gate #include <bsm/adt_event.h> 760Sstevel@tonic-gate 770Sstevel@tonic-gate #include <alloca.h> 780Sstevel@tonic-gate #include <assert.h> 790Sstevel@tonic-gate #include <errno.h> 800Sstevel@tonic-gate #include <door.h> 810Sstevel@tonic-gate #include <fcntl.h> 820Sstevel@tonic-gate #include <locale.h> 830Sstevel@tonic-gate #include <signal.h> 840Sstevel@tonic-gate #include <stdarg.h> 850Sstevel@tonic-gate #include <stdio.h> 860Sstevel@tonic-gate #include <stdlib.h> 870Sstevel@tonic-gate #include <string.h> 880Sstevel@tonic-gate #include <strings.h> 890Sstevel@tonic-gate #include <synch.h> 900Sstevel@tonic-gate #include <syslog.h> 910Sstevel@tonic-gate #include <thread.h> 920Sstevel@tonic-gate #include <unistd.h> 930Sstevel@tonic-gate #include <wait.h> 940Sstevel@tonic-gate #include <limits.h> 950Sstevel@tonic-gate #include <zone.h> 962712Snn35248 #include <libbrand.h> 970Sstevel@tonic-gate #include <libcontract.h> 980Sstevel@tonic-gate #include <libcontract_priv.h> 990Sstevel@tonic-gate #include <sys/contract/process.h> 1000Sstevel@tonic-gate #include <sys/ctfs.h> 1010Sstevel@tonic-gate 1020Sstevel@tonic-gate #include <libzonecfg.h> 1030Sstevel@tonic-gate #include "zoneadmd.h" 1040Sstevel@tonic-gate 1050Sstevel@tonic-gate static char *progname; 1060Sstevel@tonic-gate char *zone_name; /* zone which we are managing */ 1072712Snn35248 char brand_name[MAXNAMELEN]; 1082712Snn35248 boolean_t zone_isnative; 1094350Std153743 boolean_t zone_iscluster; 110766Scarlsonj static zoneid_t zone_id; 1110Sstevel@tonic-gate 1122611Svp157776 zlog_t logsys; 1130Sstevel@tonic-gate 1140Sstevel@tonic-gate mutex_t lock = DEFAULTMUTEX; /* to serialize stuff */ 1150Sstevel@tonic-gate mutex_t msglock = DEFAULTMUTEX; /* for calling setlocale() */ 1160Sstevel@tonic-gate 117766Scarlsonj static sema_t scratch_sem; /* for scratch zones */ 118766Scarlsonj 1190Sstevel@tonic-gate static char zone_door_path[MAXPATHLEN]; 1200Sstevel@tonic-gate static int zone_door = -1; 1210Sstevel@tonic-gate 1220Sstevel@tonic-gate boolean_t in_death_throes = B_FALSE; /* daemon is dying */ 1230Sstevel@tonic-gate boolean_t bringup_failure_recovery = B_FALSE; /* ignore certain failures */ 1240Sstevel@tonic-gate 1250Sstevel@tonic-gate #if !defined(TEXT_DOMAIN) /* should be defined by cc -D */ 1260Sstevel@tonic-gate #define TEXT_DOMAIN "SYS_TEST" /* Use this only if it wasn't */ 1270Sstevel@tonic-gate #endif 1280Sstevel@tonic-gate 1290Sstevel@tonic-gate #define DEFAULT_LOCALE "C" 1300Sstevel@tonic-gate 131766Scarlsonj static const char * 132766Scarlsonj z_cmd_name(zone_cmd_t zcmd) 133766Scarlsonj { 134766Scarlsonj /* This list needs to match the enum in sys/zone.h */ 135766Scarlsonj static const char *zcmdstr[] = { 1362712Snn35248 "ready", "boot", "forceboot", "reboot", "halt", 1372712Snn35248 "note_uninstalling", "mount", "forcemount", "unmount" 138766Scarlsonj }; 139766Scarlsonj 140766Scarlsonj if (zcmd >= sizeof (zcmdstr) / sizeof (*zcmdstr)) 141766Scarlsonj return ("unknown"); 142766Scarlsonj else 143766Scarlsonj return (zcmdstr[(int)zcmd]); 144766Scarlsonj } 145766Scarlsonj 1460Sstevel@tonic-gate static char * 1470Sstevel@tonic-gate get_execbasename(char *execfullname) 1480Sstevel@tonic-gate { 1490Sstevel@tonic-gate char *last_slash, *execbasename; 1500Sstevel@tonic-gate 1510Sstevel@tonic-gate /* guard against '/' at end of command invocation */ 1520Sstevel@tonic-gate for (;;) { 1530Sstevel@tonic-gate last_slash = strrchr(execfullname, '/'); 1540Sstevel@tonic-gate if (last_slash == NULL) { 1550Sstevel@tonic-gate execbasename = execfullname; 1560Sstevel@tonic-gate break; 1570Sstevel@tonic-gate } else { 1580Sstevel@tonic-gate execbasename = last_slash + 1; 1590Sstevel@tonic-gate if (*execbasename == '\0') { 1600Sstevel@tonic-gate *last_slash = '\0'; 1610Sstevel@tonic-gate continue; 1620Sstevel@tonic-gate } 1630Sstevel@tonic-gate break; 1640Sstevel@tonic-gate } 1650Sstevel@tonic-gate } 1660Sstevel@tonic-gate return (execbasename); 1670Sstevel@tonic-gate } 1680Sstevel@tonic-gate 1690Sstevel@tonic-gate static void 1700Sstevel@tonic-gate usage(void) 1710Sstevel@tonic-gate { 1720Sstevel@tonic-gate (void) fprintf(stderr, gettext("Usage: %s -z zonename\n"), progname); 1730Sstevel@tonic-gate (void) fprintf(stderr, 1740Sstevel@tonic-gate gettext("\tNote: %s should not be run directly.\n"), progname); 1750Sstevel@tonic-gate exit(2); 1760Sstevel@tonic-gate } 1770Sstevel@tonic-gate 1780Sstevel@tonic-gate /* ARGSUSED */ 1790Sstevel@tonic-gate static void 1800Sstevel@tonic-gate sigchld(int sig) 1810Sstevel@tonic-gate { 1820Sstevel@tonic-gate } 1830Sstevel@tonic-gate 1840Sstevel@tonic-gate char * 1850Sstevel@tonic-gate localize_msg(char *locale, const char *msg) 1860Sstevel@tonic-gate { 1870Sstevel@tonic-gate char *out; 1880Sstevel@tonic-gate 1890Sstevel@tonic-gate (void) mutex_lock(&msglock); 1900Sstevel@tonic-gate (void) setlocale(LC_MESSAGES, locale); 1910Sstevel@tonic-gate out = gettext(msg); 1920Sstevel@tonic-gate (void) setlocale(LC_MESSAGES, DEFAULT_LOCALE); 1930Sstevel@tonic-gate (void) mutex_unlock(&msglock); 1940Sstevel@tonic-gate return (out); 1950Sstevel@tonic-gate } 1960Sstevel@tonic-gate 1970Sstevel@tonic-gate /* PRINTFLIKE3 */ 1980Sstevel@tonic-gate void 1990Sstevel@tonic-gate zerror(zlog_t *zlogp, boolean_t use_strerror, const char *fmt, ...) 2000Sstevel@tonic-gate { 2010Sstevel@tonic-gate va_list alist; 2020Sstevel@tonic-gate char buf[MAXPATHLEN * 2]; /* enough space for err msg with a path */ 2030Sstevel@tonic-gate char *bp; 2040Sstevel@tonic-gate int saved_errno = errno; 2050Sstevel@tonic-gate 2060Sstevel@tonic-gate if (zlogp == NULL) 2070Sstevel@tonic-gate return; 2080Sstevel@tonic-gate if (zlogp == &logsys) 2090Sstevel@tonic-gate (void) snprintf(buf, sizeof (buf), "[zone '%s'] ", 2100Sstevel@tonic-gate zone_name); 2110Sstevel@tonic-gate else 2120Sstevel@tonic-gate buf[0] = '\0'; 2130Sstevel@tonic-gate bp = &(buf[strlen(buf)]); 2140Sstevel@tonic-gate 2150Sstevel@tonic-gate /* 2160Sstevel@tonic-gate * In theory, the locale pointer should be set to either "C" or a 2170Sstevel@tonic-gate * char array, so it should never be NULL 2180Sstevel@tonic-gate */ 2190Sstevel@tonic-gate assert(zlogp->locale != NULL); 2200Sstevel@tonic-gate /* Locale is per process, but we are multi-threaded... */ 2210Sstevel@tonic-gate fmt = localize_msg(zlogp->locale, fmt); 2220Sstevel@tonic-gate 2230Sstevel@tonic-gate va_start(alist, fmt); 2240Sstevel@tonic-gate (void) vsnprintf(bp, sizeof (buf) - (bp - buf), fmt, alist); 2250Sstevel@tonic-gate va_end(alist); 2260Sstevel@tonic-gate bp = &(buf[strlen(buf)]); 2270Sstevel@tonic-gate if (use_strerror) 2280Sstevel@tonic-gate (void) snprintf(bp, sizeof (buf) - (bp - buf), ": %s", 2290Sstevel@tonic-gate strerror(saved_errno)); 2300Sstevel@tonic-gate if (zlogp == &logsys) { 2310Sstevel@tonic-gate (void) syslog(LOG_ERR, "%s", buf); 2320Sstevel@tonic-gate } else if (zlogp->logfile != NULL) { 2330Sstevel@tonic-gate (void) fprintf(zlogp->logfile, "%s\n", buf); 2340Sstevel@tonic-gate } else { 2350Sstevel@tonic-gate size_t buflen; 2360Sstevel@tonic-gate size_t copylen; 2370Sstevel@tonic-gate 2380Sstevel@tonic-gate buflen = snprintf(zlogp->log, zlogp->loglen, "%s\n", buf); 2390Sstevel@tonic-gate copylen = MIN(buflen, zlogp->loglen); 2400Sstevel@tonic-gate zlogp->log += copylen; 2410Sstevel@tonic-gate zlogp->loglen -= copylen; 2420Sstevel@tonic-gate } 2430Sstevel@tonic-gate } 2440Sstevel@tonic-gate 2452267Sdp /* 2462267Sdp * Emit a warning for any boot arguments which are unrecognized. Since 2472267Sdp * Solaris boot arguments are getopt(3c) compatible (see kernel(1m)), we 2482267Sdp * put the arguments into an argv style array, use getopt to process them, 2492267Sdp * and put the resultant argument string back into outargs. 2502267Sdp * 2512267Sdp * During the filtering, we pull out any arguments which are truly "boot" 2522267Sdp * arguments, leaving only those which are to be passed intact to the 2532267Sdp * progenitor process. The one we support at the moment is -i, which 2542267Sdp * indicates to the kernel which program should be launched as 'init'. 2552267Sdp * 2562267Sdp * A return of Z_INVAL indicates specifically that the arguments are 2572267Sdp * not valid; this is a non-fatal error. Except for Z_OK, all other return 2582267Sdp * values are treated as fatal. 2592267Sdp */ 2602267Sdp static int 2612267Sdp filter_bootargs(zlog_t *zlogp, const char *inargs, char *outargs, 2622267Sdp char *init_file, char *badarg) 2632267Sdp { 2642267Sdp int argc = 0, argc_save; 2652267Sdp int i; 2662267Sdp int err; 2672267Sdp char *arg, *lasts, **argv = NULL, **argv_save; 2682267Sdp char zonecfg_args[BOOTARGS_MAX]; 2692267Sdp char scratchargs[BOOTARGS_MAX], *sargs; 2702267Sdp char c; 2712267Sdp 2722267Sdp bzero(outargs, BOOTARGS_MAX); 2732267Sdp bzero(badarg, BOOTARGS_MAX); 2742267Sdp 2752267Sdp /* 2762267Sdp * If the user didn't specify transient boot arguments, check 2772267Sdp * to see if there were any specified in the zone configuration, 2782267Sdp * and use them if applicable. 2792267Sdp */ 2802267Sdp if (inargs == NULL || inargs[0] == '\0') { 2812267Sdp zone_dochandle_t handle; 2822267Sdp if ((handle = zonecfg_init_handle()) == NULL) { 2832267Sdp zerror(zlogp, B_TRUE, 2842267Sdp "getting zone configuration handle"); 2852267Sdp return (Z_BAD_HANDLE); 2862267Sdp } 2872267Sdp err = zonecfg_get_snapshot_handle(zone_name, handle); 2882267Sdp if (err != Z_OK) { 2892267Sdp zerror(zlogp, B_FALSE, 2902267Sdp "invalid configuration snapshot"); 2912267Sdp zonecfg_fini_handle(handle); 2922267Sdp return (Z_BAD_HANDLE); 2932267Sdp } 2942267Sdp 2952267Sdp bzero(zonecfg_args, sizeof (zonecfg_args)); 2962267Sdp (void) zonecfg_get_bootargs(handle, zonecfg_args, 2972267Sdp sizeof (zonecfg_args)); 2982267Sdp inargs = zonecfg_args; 2992267Sdp zonecfg_fini_handle(handle); 3002267Sdp } 3012267Sdp 3022267Sdp if (strlen(inargs) >= BOOTARGS_MAX) { 3032267Sdp zerror(zlogp, B_FALSE, "boot argument string too long"); 3042267Sdp return (Z_INVAL); 3052267Sdp } 3062267Sdp 3072267Sdp (void) strlcpy(scratchargs, inargs, sizeof (scratchargs)); 3082267Sdp sargs = scratchargs; 3092267Sdp while ((arg = strtok_r(sargs, " \t", &lasts)) != NULL) { 3102267Sdp sargs = NULL; 3112267Sdp argc++; 3122267Sdp } 3132267Sdp 3142267Sdp if ((argv = calloc(argc + 1, sizeof (char *))) == NULL) { 3152267Sdp zerror(zlogp, B_FALSE, "memory allocation failed"); 3162267Sdp return (Z_NOMEM); 3172267Sdp } 3182267Sdp 3192267Sdp argv_save = argv; 3202267Sdp argc_save = argc; 3212267Sdp 3222267Sdp (void) strlcpy(scratchargs, inargs, sizeof (scratchargs)); 3232267Sdp sargs = scratchargs; 3242267Sdp i = 0; 3252267Sdp while ((arg = strtok_r(sargs, " \t", &lasts)) != NULL) { 3262267Sdp sargs = NULL; 3272267Sdp if ((argv[i] = strdup(arg)) == NULL) { 3282267Sdp err = Z_NOMEM; 3292267Sdp zerror(zlogp, B_FALSE, "memory allocation failed"); 3302267Sdp goto done; 3312267Sdp } 3322267Sdp i++; 3332267Sdp } 3342267Sdp 3352267Sdp /* 3362267Sdp * We preserve compatibility with the Solaris system boot behavior, 3372267Sdp * which allows: 3382267Sdp * 3392267Sdp * # reboot kernel/unix -s -m verbose 3402267Sdp * 3412267Sdp * In this example, kernel/unix tells the booter what file to 3422267Sdp * boot. We don't want reboot in a zone to be gratuitously different, 3432267Sdp * so we silently ignore the boot file, if necessary. 3442267Sdp */ 3452267Sdp if (argv[0] == NULL) 3462267Sdp goto done; 3472267Sdp 3482267Sdp assert(argv[0][0] != ' '); 3492267Sdp assert(argv[0][0] != '\t'); 3502267Sdp 3512267Sdp if (argv[0][0] != '-' && argv[0][0] != '\0') { 3522267Sdp argv = &argv[1]; 3532267Sdp argc--; 3542267Sdp } 3552267Sdp 3562267Sdp optind = 0; 3572267Sdp opterr = 0; 3582267Sdp err = Z_OK; 3592712Snn35248 while ((c = getopt(argc, argv, "fi:m:s")) != -1) { 3602267Sdp switch (c) { 3612267Sdp case 'i': 3622267Sdp /* 3632267Sdp * -i is handled by the runtime and is not passed 3642267Sdp * along to userland 3652267Sdp */ 3662267Sdp (void) strlcpy(init_file, optarg, MAXPATHLEN); 3672267Sdp break; 3682712Snn35248 case 'f': 3692712Snn35248 /* This has already been processed by zoneadm */ 3702712Snn35248 break; 3712267Sdp case 'm': 3722267Sdp case 's': 3732267Sdp /* These pass through unmolested */ 3742267Sdp (void) snprintf(outargs, BOOTARGS_MAX, 3752267Sdp "%s -%c %s ", outargs, c, optarg ? optarg : ""); 3762267Sdp break; 3772267Sdp case '?': 3782267Sdp /* 3792267Sdp * We warn about unknown arguments but pass them 3802267Sdp * along anyway-- if someone wants to develop their 3812267Sdp * own init replacement, they can pass it whatever 3822267Sdp * args they want. 3832267Sdp */ 3842267Sdp err = Z_INVAL; 3852267Sdp (void) snprintf(outargs, BOOTARGS_MAX, 3862267Sdp "%s -%c", outargs, optopt); 3872267Sdp (void) snprintf(badarg, BOOTARGS_MAX, 3882267Sdp "%s -%c", badarg, optopt); 3892267Sdp break; 3902267Sdp } 3912267Sdp } 3922267Sdp 3932267Sdp /* 3942267Sdp * For Solaris Zones we warn about and discard non-option arguments. 3952267Sdp * Hence 'boot foo bar baz gub' --> 'boot'. However, to be similar 3962267Sdp * to the kernel, we concat up all the other remaining boot args. 3972267Sdp * and warn on them as a group. 3982267Sdp */ 3992267Sdp if (optind < argc) { 4002267Sdp err = Z_INVAL; 4012267Sdp while (optind < argc) { 4022267Sdp (void) snprintf(badarg, BOOTARGS_MAX, "%s%s%s", 4032267Sdp badarg, strlen(badarg) > 0 ? " " : "", 4042267Sdp argv[optind]); 4052267Sdp optind++; 4062267Sdp } 4072267Sdp zerror(zlogp, B_FALSE, "WARNING: Unused or invalid boot " 4082267Sdp "arguments `%s'.", badarg); 4092267Sdp } 4102267Sdp 4112267Sdp done: 4122267Sdp for (i = 0; i < argc_save; i++) { 4132267Sdp if (argv_save[i] != NULL) 4142267Sdp free(argv_save[i]); 4152267Sdp } 4162267Sdp free(argv_save); 4172267Sdp return (err); 4182267Sdp } 4192267Sdp 4202267Sdp 4210Sstevel@tonic-gate static int 4220Sstevel@tonic-gate mkzonedir(zlog_t *zlogp) 4230Sstevel@tonic-gate { 4240Sstevel@tonic-gate struct stat st; 4250Sstevel@tonic-gate /* 4260Sstevel@tonic-gate * We must create and lock everyone but root out of ZONES_TMPDIR 4270Sstevel@tonic-gate * since anyone can open any UNIX domain socket, regardless of 4280Sstevel@tonic-gate * its file system permissions. Sigh... 4290Sstevel@tonic-gate */ 4300Sstevel@tonic-gate if (mkdir(ZONES_TMPDIR, S_IRWXU) < 0 && errno != EEXIST) { 4310Sstevel@tonic-gate zerror(zlogp, B_TRUE, "could not mkdir '%s'", ZONES_TMPDIR); 4320Sstevel@tonic-gate return (-1); 4330Sstevel@tonic-gate } 4340Sstevel@tonic-gate /* paranoia */ 435871Scasper if ((stat(ZONES_TMPDIR, &st) < 0) || !S_ISDIR(st.st_mode)) { 4360Sstevel@tonic-gate zerror(zlogp, B_TRUE, "'%s' is not a directory", ZONES_TMPDIR); 4370Sstevel@tonic-gate return (-1); 4380Sstevel@tonic-gate } 4390Sstevel@tonic-gate (void) chmod(ZONES_TMPDIR, S_IRWXU); 4400Sstevel@tonic-gate return (0); 4410Sstevel@tonic-gate } 4420Sstevel@tonic-gate 443766Scarlsonj /* 444766Scarlsonj * Bring a zone up to the pre-boot "ready" stage. The mount_cmd argument is 445766Scarlsonj * 'true' if this is being invoked as part of the processing for the "mount" 446766Scarlsonj * subcommand. 447766Scarlsonj */ 448766Scarlsonj static int 4495829Sgjelinek zone_ready(zlog_t *zlogp, zone_mnt_t mount_cmd) 4500Sstevel@tonic-gate { 4510Sstevel@tonic-gate int err; 4520Sstevel@tonic-gate 4530Sstevel@tonic-gate if ((err = zonecfg_create_snapshot(zone_name)) != Z_OK) { 4540Sstevel@tonic-gate zerror(zlogp, B_FALSE, "unable to create snapshot: %s", 4550Sstevel@tonic-gate zonecfg_strerror(err)); 4560Sstevel@tonic-gate return (-1); 4570Sstevel@tonic-gate } 4580Sstevel@tonic-gate 459766Scarlsonj if ((zone_id = vplat_create(zlogp, mount_cmd)) == -1) { 4600Sstevel@tonic-gate if ((err = zonecfg_destroy_snapshot(zone_name)) != Z_OK) 4610Sstevel@tonic-gate zerror(zlogp, B_FALSE, "destroying snapshot: %s", 4620Sstevel@tonic-gate zonecfg_strerror(err)); 4630Sstevel@tonic-gate return (-1); 4640Sstevel@tonic-gate } 4652303Scarlsonj if (vplat_bringup(zlogp, mount_cmd, zone_id) != 0) { 4660Sstevel@tonic-gate bringup_failure_recovery = B_TRUE; 4675829Sgjelinek (void) vplat_teardown(NULL, (mount_cmd != Z_MNT_BOOT), B_FALSE); 4680Sstevel@tonic-gate if ((err = zonecfg_destroy_snapshot(zone_name)) != Z_OK) 4690Sstevel@tonic-gate zerror(zlogp, B_FALSE, "destroying snapshot: %s", 4700Sstevel@tonic-gate zonecfg_strerror(err)); 4710Sstevel@tonic-gate return (-1); 4720Sstevel@tonic-gate } 4730Sstevel@tonic-gate 4740Sstevel@tonic-gate return (0); 4750Sstevel@tonic-gate } 4760Sstevel@tonic-gate 4772303Scarlsonj int 4782303Scarlsonj init_template(void) 4790Sstevel@tonic-gate { 4800Sstevel@tonic-gate int fd; 4810Sstevel@tonic-gate int err = 0; 4820Sstevel@tonic-gate 4830Sstevel@tonic-gate fd = open64(CTFS_ROOT "/process/template", O_RDWR); 4840Sstevel@tonic-gate if (fd == -1) 4850Sstevel@tonic-gate return (-1); 4860Sstevel@tonic-gate 4870Sstevel@tonic-gate /* 4880Sstevel@tonic-gate * For now, zoneadmd doesn't do anything with the contract. 4890Sstevel@tonic-gate * Deliver no events, don't inherit, and allow it to be orphaned. 4900Sstevel@tonic-gate */ 4910Sstevel@tonic-gate err |= ct_tmpl_set_critical(fd, 0); 4920Sstevel@tonic-gate err |= ct_tmpl_set_informative(fd, 0); 4930Sstevel@tonic-gate err |= ct_pr_tmpl_set_fatal(fd, CT_PR_EV_HWERR); 4940Sstevel@tonic-gate err |= ct_pr_tmpl_set_param(fd, CT_PR_PGRPONLY | CT_PR_REGENT); 4950Sstevel@tonic-gate if (err || ct_tmpl_activate(fd)) { 4960Sstevel@tonic-gate (void) close(fd); 4970Sstevel@tonic-gate return (-1); 4980Sstevel@tonic-gate } 4990Sstevel@tonic-gate 5000Sstevel@tonic-gate return (fd); 5010Sstevel@tonic-gate } 5020Sstevel@tonic-gate 5032712Snn35248 typedef struct fs_callback { 5042712Snn35248 zlog_t *zlogp; 5052712Snn35248 zoneid_t zoneid; 5065576Sedp boolean_t mount_cmd; 5072712Snn35248 } fs_callback_t; 5082712Snn35248 5090Sstevel@tonic-gate static int 5102712Snn35248 mount_early_fs(void *data, const char *spec, const char *dir, 5112712Snn35248 const char *fstype, const char *opt) 5120Sstevel@tonic-gate { 5132712Snn35248 zlog_t *zlogp = ((fs_callback_t *)data)->zlogp; 5142712Snn35248 zoneid_t zoneid = ((fs_callback_t *)data)->zoneid; 5155576Sedp boolean_t mount_cmd = ((fs_callback_t *)data)->mount_cmd; 5165182Sedp char rootpath[MAXPATHLEN]; 5170Sstevel@tonic-gate pid_t child; 5180Sstevel@tonic-gate int child_status; 5190Sstevel@tonic-gate int tmpl_fd; 5205182Sedp int rv; 5210Sstevel@tonic-gate ctid_t ct; 5220Sstevel@tonic-gate 5235576Sedp /* determine the zone rootpath */ 5245576Sedp if (mount_cmd) { 5255576Sedp char zonepath[MAXPATHLEN]; 5265576Sedp char luroot[MAXPATHLEN]; 5275576Sedp 5285576Sedp assert(zone_isnative || zone_iscluster); 5295576Sedp 5305576Sedp if (zone_get_zonepath(zone_name, 5315576Sedp zonepath, sizeof (zonepath)) != Z_OK) { 5325576Sedp zerror(zlogp, B_FALSE, "unable to determine zone path"); 5335576Sedp return (-1); 5345576Sedp } 5355576Sedp 5365576Sedp (void) snprintf(luroot, sizeof (luroot), "%s/lu", zonepath); 5375576Sedp resolve_lofs(zlogp, luroot, sizeof (luroot)); 5385576Sedp (void) strlcpy(rootpath, luroot, sizeof (rootpath)); 5395576Sedp } else { 5405576Sedp if (zone_get_rootpath(zone_name, 5415576Sedp rootpath, sizeof (rootpath)) != Z_OK) { 5425576Sedp zerror(zlogp, B_FALSE, "unable to determine zone root"); 5435576Sedp return (-1); 5445576Sedp } 5455182Sedp } 5465182Sedp 5475182Sedp if ((rv = valid_mount_path(zlogp, rootpath, spec, dir, fstype)) < 0) { 5485182Sedp zerror(zlogp, B_FALSE, "%s%s is not a valid mount point", 5495182Sedp rootpath, dir); 5505182Sedp return (-1); 5515182Sedp } else if (rv > 0) { 5525182Sedp /* The mount point path doesn't exist, create it now. */ 5535182Sedp if (make_one_dir(zlogp, rootpath, dir, 5545182Sedp DEFAULT_DIR_MODE, DEFAULT_DIR_USER, 5555182Sedp DEFAULT_DIR_GROUP) != 0) { 5565182Sedp zerror(zlogp, B_FALSE, "failed to create mount point"); 5575182Sedp return (-1); 5585182Sedp } 5595182Sedp 5605182Sedp /* 5615182Sedp * Now this might seem weird, but we need to invoke 5625182Sedp * valid_mount_path() again. Why? Because it checks 5635182Sedp * to make sure that the mount point path is canonical, 5645182Sedp * which it can only do if the path exists, so now that 5655182Sedp * we've created the path we have to verify it again. 5665182Sedp */ 5675182Sedp if ((rv = valid_mount_path(zlogp, rootpath, spec, dir, 5685182Sedp fstype)) < 0) { 5695182Sedp zerror(zlogp, B_FALSE, 5705182Sedp "%s%s is not a valid mount point", rootpath, dir); 5715182Sedp return (-1); 5725182Sedp } 5735182Sedp } 5745182Sedp 5750Sstevel@tonic-gate if ((tmpl_fd = init_template()) == -1) { 5760Sstevel@tonic-gate zerror(zlogp, B_TRUE, "failed to create contract"); 5770Sstevel@tonic-gate return (-1); 5780Sstevel@tonic-gate } 5790Sstevel@tonic-gate 5800Sstevel@tonic-gate if ((child = fork()) == -1) { 5810Sstevel@tonic-gate (void) ct_tmpl_clear(tmpl_fd); 5820Sstevel@tonic-gate (void) close(tmpl_fd); 5830Sstevel@tonic-gate zerror(zlogp, B_TRUE, "failed to fork"); 5840Sstevel@tonic-gate return (-1); 5850Sstevel@tonic-gate 5860Sstevel@tonic-gate } else if (child == 0) { /* child */ 5872712Snn35248 char opt_buf[MAX_MNTOPT_STR]; 5882712Snn35248 int optlen = 0; 5892712Snn35248 int mflag = MS_DATA; 5902712Snn35248 5910Sstevel@tonic-gate (void) ct_tmpl_clear(tmpl_fd); 5920Sstevel@tonic-gate /* 5930Sstevel@tonic-gate * Even though there are no procs running in the zone, we 5940Sstevel@tonic-gate * do this for paranoia's sake. 5950Sstevel@tonic-gate */ 5960Sstevel@tonic-gate (void) closefrom(0); 5970Sstevel@tonic-gate 5980Sstevel@tonic-gate if (zone_enter(zoneid) == -1) { 5990Sstevel@tonic-gate _exit(errno); 6000Sstevel@tonic-gate } 6012712Snn35248 if (opt != NULL) { 6022712Snn35248 /* 6032712Snn35248 * The mount() system call is incredibly annoying. 6042712Snn35248 * If options are specified, we need to copy them 6052712Snn35248 * into a temporary buffer since the mount() system 6062712Snn35248 * call will overwrite the options string. It will 6072712Snn35248 * also fail if the new option string it wants to 6082712Snn35248 * write is bigger than the one we passed in, so 6092712Snn35248 * you must pass in a buffer of the maximum possible 6102712Snn35248 * option string length. sigh. 6112712Snn35248 */ 6122712Snn35248 (void) strlcpy(opt_buf, opt, sizeof (opt_buf)); 6132712Snn35248 opt = opt_buf; 6142712Snn35248 optlen = MAX_MNTOPT_STR; 6152712Snn35248 mflag = MS_OPTIONSTR; 6162712Snn35248 } 6172712Snn35248 if (mount(spec, dir, mflag, fstype, NULL, 0, opt, optlen) != 0) 6180Sstevel@tonic-gate _exit(errno); 6190Sstevel@tonic-gate _exit(0); 6200Sstevel@tonic-gate } 6210Sstevel@tonic-gate 6220Sstevel@tonic-gate /* parent */ 6230Sstevel@tonic-gate if (contract_latest(&ct) == -1) 6240Sstevel@tonic-gate ct = -1; 6250Sstevel@tonic-gate (void) ct_tmpl_clear(tmpl_fd); 6260Sstevel@tonic-gate (void) close(tmpl_fd); 6270Sstevel@tonic-gate if (waitpid(child, &child_status, 0) != child) { 6280Sstevel@tonic-gate /* unexpected: we must have been signalled */ 6290Sstevel@tonic-gate (void) contract_abandon_id(ct); 6300Sstevel@tonic-gate return (-1); 6310Sstevel@tonic-gate } 6320Sstevel@tonic-gate (void) contract_abandon_id(ct); 6330Sstevel@tonic-gate if (WEXITSTATUS(child_status) != 0) { 6340Sstevel@tonic-gate errno = WEXITSTATUS(child_status); 6350Sstevel@tonic-gate zerror(zlogp, B_TRUE, "mount of %s failed", dir); 6360Sstevel@tonic-gate return (-1); 6370Sstevel@tonic-gate } 6380Sstevel@tonic-gate 6390Sstevel@tonic-gate return (0); 6400Sstevel@tonic-gate } 6410Sstevel@tonic-gate 6422712Snn35248 int 6432712Snn35248 do_subproc(zlog_t *zlogp, char *cmdbuf) 644766Scarlsonj { 6452712Snn35248 char inbuf[1024]; /* arbitrary large amount */ 6462712Snn35248 FILE *file; 6472712Snn35248 int status; 648766Scarlsonj 6492712Snn35248 file = popen(cmdbuf, "r"); 6502712Snn35248 if (file == NULL) { 6512712Snn35248 zerror(zlogp, B_TRUE, "could not launch: %s", cmdbuf); 6522525Sdp return (-1); 6532712Snn35248 } 6542525Sdp 6552712Snn35248 while (fgets(inbuf, sizeof (inbuf), file) != NULL) 6562712Snn35248 if (zlogp != &logsys) 6572712Snn35248 zerror(zlogp, B_FALSE, "%s", inbuf); 6582712Snn35248 status = pclose(file); 659766Scarlsonj 6602712Snn35248 if (WIFSIGNALED(status)) { 6612712Snn35248 zerror(zlogp, B_FALSE, "%s unexpectedly terminated due to " 6622712Snn35248 "signal %d", cmdbuf, WTERMSIG(status)); 663766Scarlsonj return (-1); 6642712Snn35248 } 6652712Snn35248 assert(WIFEXITED(status)); 6662712Snn35248 if (WEXITSTATUS(status) == ZEXIT_EXEC) { 6672712Snn35248 zerror(zlogp, B_FALSE, "failed to exec %s", cmdbuf); 6682712Snn35248 return (-1); 6692712Snn35248 } 6702712Snn35248 return (WEXITSTATUS(status)); 671766Scarlsonj } 672766Scarlsonj 673766Scarlsonj static int 6740Sstevel@tonic-gate zone_bootup(zlog_t *zlogp, const char *bootargs) 6750Sstevel@tonic-gate { 6760Sstevel@tonic-gate zoneid_t zoneid; 6770Sstevel@tonic-gate struct stat st; 678*7089Sgjelinek char zpath[MAXPATHLEN], initpath[MAXPATHLEN], init_file[MAXPATHLEN]; 6792267Sdp char nbootargs[BOOTARGS_MAX]; 6802712Snn35248 char cmdbuf[MAXPATHLEN]; 6812712Snn35248 fs_callback_t cb; 6822727Sedp brand_handle_t bh; 6832267Sdp int err; 6840Sstevel@tonic-gate 6850Sstevel@tonic-gate if (init_console_slave(zlogp) != 0) 6860Sstevel@tonic-gate return (-1); 6870Sstevel@tonic-gate reset_slave_terminal(zlogp); 6880Sstevel@tonic-gate 6890Sstevel@tonic-gate if ((zoneid = getzoneidbyname(zone_name)) == -1) { 6900Sstevel@tonic-gate zerror(zlogp, B_TRUE, "unable to get zoneid"); 6910Sstevel@tonic-gate return (-1); 6920Sstevel@tonic-gate } 6930Sstevel@tonic-gate 6942712Snn35248 cb.zlogp = zlogp; 6952712Snn35248 cb.zoneid = zoneid; 6965576Sedp cb.mount_cmd = B_FALSE; 6972712Snn35248 6982712Snn35248 /* Get a handle to the brand info for this zone */ 6992727Sedp if ((bh = brand_open(brand_name)) == NULL) { 7002712Snn35248 zerror(zlogp, B_FALSE, "unable to determine zone brand"); 7012712Snn35248 return (-1); 7022712Snn35248 } 7032712Snn35248 7042712Snn35248 /* 7052712Snn35248 * Get the list of filesystems to mount from the brand 7062712Snn35248 * configuration. These mounts are done via a thread that will 7072712Snn35248 * enter the zone, so they are done from within the context of the 7082712Snn35248 * zone. 7092712Snn35248 */ 7102727Sedp if (brand_platform_iter_mounts(bh, mount_early_fs, &cb) != 0) { 7112712Snn35248 zerror(zlogp, B_FALSE, "unable to mount filesystems"); 7122727Sedp brand_close(bh); 7130Sstevel@tonic-gate return (-1); 7142712Snn35248 } 7152712Snn35248 7162712Snn35248 /* 7172712Snn35248 * Get the brand's boot callback if it exists. 7182712Snn35248 */ 719*7089Sgjelinek if (zone_get_zonepath(zone_name, zpath, sizeof (zpath)) != Z_OK) { 720*7089Sgjelinek zerror(zlogp, B_FALSE, "unable to determine zone path"); 7213716Sgjelinek brand_close(bh); 7222712Snn35248 return (-1); 7232712Snn35248 } 7242712Snn35248 (void) strcpy(cmdbuf, EXEC_PREFIX); 725*7089Sgjelinek if (brand_get_boot(bh, zone_name, zpath, cmdbuf + EXEC_LEN, 726*7089Sgjelinek sizeof (cmdbuf) - EXEC_LEN) != 0) { 7272712Snn35248 zerror(zlogp, B_FALSE, 7282712Snn35248 "unable to determine branded zone's boot callback"); 7292727Sedp brand_close(bh); 7302712Snn35248 return (-1); 7312712Snn35248 } 7322712Snn35248 7332712Snn35248 /* Get the path for this zone's init(1M) (or equivalent) process. */ 7342727Sedp if (brand_get_initname(bh, init_file, MAXPATHLEN) != 0) { 7352712Snn35248 zerror(zlogp, B_FALSE, 7362712Snn35248 "unable to determine zone's init(1M) location"); 7372727Sedp brand_close(bh); 7382712Snn35248 return (-1); 7392712Snn35248 } 7402712Snn35248 7412727Sedp brand_close(bh); 7420Sstevel@tonic-gate 7432267Sdp err = filter_bootargs(zlogp, bootargs, nbootargs, init_file, 7442267Sdp bad_boot_arg); 7452267Sdp if (err == Z_INVAL) 7462267Sdp eventstream_write(Z_EVT_ZONE_BADARGS); 7472267Sdp else if (err != Z_OK) 7482267Sdp return (-1); 7492267Sdp 7502267Sdp assert(init_file[0] != '\0'); 7512267Sdp 7522712Snn35248 /* Try to anticipate possible problems: Make sure init is executable. */ 753*7089Sgjelinek if (zone_get_rootpath(zone_name, zpath, sizeof (zpath)) != Z_OK) { 7540Sstevel@tonic-gate zerror(zlogp, B_FALSE, "unable to determine zone root"); 7550Sstevel@tonic-gate return (-1); 7560Sstevel@tonic-gate } 7572712Snn35248 758*7089Sgjelinek (void) snprintf(initpath, sizeof (initpath), "%s%s", zpath, init_file); 7590Sstevel@tonic-gate 7600Sstevel@tonic-gate if (stat(initpath, &st) == -1) { 7610Sstevel@tonic-gate zerror(zlogp, B_TRUE, "could not stat %s", initpath); 7620Sstevel@tonic-gate return (-1); 7630Sstevel@tonic-gate } 7640Sstevel@tonic-gate 7650Sstevel@tonic-gate if ((st.st_mode & S_IXUSR) == 0) { 7660Sstevel@tonic-gate zerror(zlogp, B_FALSE, "%s is not executable", initpath); 7670Sstevel@tonic-gate return (-1); 7680Sstevel@tonic-gate } 7690Sstevel@tonic-gate 7702712Snn35248 /* 7712712Snn35248 * If there is a brand 'boot' callback, execute it now to give the 7722712Snn35248 * brand one last chance to do any additional setup before the zone 7732712Snn35248 * is booted. 7742712Snn35248 */ 7752712Snn35248 if ((strlen(cmdbuf) > EXEC_LEN) && 7762712Snn35248 (do_subproc(zlogp, cmdbuf) != Z_OK)) { 7772712Snn35248 zerror(zlogp, B_FALSE, "%s failed", cmdbuf); 7782712Snn35248 return (-1); 7792712Snn35248 } 7802712Snn35248 7812267Sdp if (zone_setattr(zoneid, ZONE_ATTR_INITNAME, init_file, 0) == -1) { 7822267Sdp zerror(zlogp, B_TRUE, "could not set zone boot file"); 7832267Sdp return (-1); 7842267Sdp } 7852267Sdp 7862267Sdp if (zone_setattr(zoneid, ZONE_ATTR_BOOTARGS, nbootargs, 0) == -1) { 7872267Sdp zerror(zlogp, B_TRUE, "could not set zone boot arguments"); 7882267Sdp return (-1); 7892267Sdp } 7902267Sdp 7912267Sdp if (zone_boot(zoneid) == -1) { 7920Sstevel@tonic-gate zerror(zlogp, B_TRUE, "unable to boot zone"); 7930Sstevel@tonic-gate return (-1); 7940Sstevel@tonic-gate } 7950Sstevel@tonic-gate 7960Sstevel@tonic-gate return (0); 7970Sstevel@tonic-gate } 7980Sstevel@tonic-gate 7990Sstevel@tonic-gate static int 8003247Sgjelinek zone_halt(zlog_t *zlogp, boolean_t unmount_cmd, boolean_t rebooting) 8010Sstevel@tonic-gate { 8020Sstevel@tonic-gate int err; 8030Sstevel@tonic-gate 8043247Sgjelinek if (vplat_teardown(zlogp, unmount_cmd, rebooting) != 0) { 8050Sstevel@tonic-gate if (!bringup_failure_recovery) 8060Sstevel@tonic-gate zerror(zlogp, B_FALSE, "unable to destroy zone"); 8070Sstevel@tonic-gate return (-1); 8080Sstevel@tonic-gate } 8090Sstevel@tonic-gate 8100Sstevel@tonic-gate if ((err = zonecfg_destroy_snapshot(zone_name)) != Z_OK) 8110Sstevel@tonic-gate zerror(zlogp, B_FALSE, "destroying snapshot: %s", 8120Sstevel@tonic-gate zonecfg_strerror(err)); 8130Sstevel@tonic-gate 8140Sstevel@tonic-gate return (0); 8150Sstevel@tonic-gate } 8160Sstevel@tonic-gate 8170Sstevel@tonic-gate /* 8180Sstevel@tonic-gate * Generate AUE_zone_state for a command that boots a zone. 8190Sstevel@tonic-gate */ 8200Sstevel@tonic-gate static void 8210Sstevel@tonic-gate audit_put_record(zlog_t *zlogp, ucred_t *uc, int return_val, 8220Sstevel@tonic-gate char *new_state) 8230Sstevel@tonic-gate { 8240Sstevel@tonic-gate adt_session_data_t *ah; 8250Sstevel@tonic-gate adt_event_data_t *event; 8260Sstevel@tonic-gate int pass_fail, fail_reason; 8270Sstevel@tonic-gate 8280Sstevel@tonic-gate if (!adt_audit_enabled()) 8290Sstevel@tonic-gate return; 8300Sstevel@tonic-gate 8310Sstevel@tonic-gate if (return_val == 0) { 8320Sstevel@tonic-gate pass_fail = ADT_SUCCESS; 8330Sstevel@tonic-gate fail_reason = ADT_SUCCESS; 8340Sstevel@tonic-gate } else { 8350Sstevel@tonic-gate pass_fail = ADT_FAILURE; 8360Sstevel@tonic-gate fail_reason = ADT_FAIL_VALUE_PROGRAM; 8370Sstevel@tonic-gate } 8380Sstevel@tonic-gate 8390Sstevel@tonic-gate if (adt_start_session(&ah, NULL, 0)) { 8400Sstevel@tonic-gate zerror(zlogp, B_TRUE, gettext("audit failure.")); 8410Sstevel@tonic-gate return; 8420Sstevel@tonic-gate } 8430Sstevel@tonic-gate if (adt_set_from_ucred(ah, uc, ADT_NEW)) { 8440Sstevel@tonic-gate zerror(zlogp, B_TRUE, gettext("audit failure.")); 8450Sstevel@tonic-gate (void) adt_end_session(ah); 8460Sstevel@tonic-gate return; 8470Sstevel@tonic-gate } 8480Sstevel@tonic-gate 8490Sstevel@tonic-gate event = adt_alloc_event(ah, ADT_zone_state); 8500Sstevel@tonic-gate if (event == NULL) { 8510Sstevel@tonic-gate zerror(zlogp, B_TRUE, gettext("audit failure.")); 8520Sstevel@tonic-gate (void) adt_end_session(ah); 8530Sstevel@tonic-gate return; 8540Sstevel@tonic-gate } 8550Sstevel@tonic-gate event->adt_zone_state.zonename = zone_name; 8560Sstevel@tonic-gate event->adt_zone_state.new_state = new_state; 8570Sstevel@tonic-gate 8580Sstevel@tonic-gate if (adt_put_event(event, pass_fail, fail_reason)) 8590Sstevel@tonic-gate zerror(zlogp, B_TRUE, gettext("audit failure.")); 8600Sstevel@tonic-gate 8610Sstevel@tonic-gate adt_free_event(event); 8620Sstevel@tonic-gate 8630Sstevel@tonic-gate (void) adt_end_session(ah); 8640Sstevel@tonic-gate } 8650Sstevel@tonic-gate 8660Sstevel@tonic-gate /* 8670Sstevel@tonic-gate * The main routine for the door server that deals with zone state transitions. 8680Sstevel@tonic-gate */ 8690Sstevel@tonic-gate /* ARGSUSED */ 8700Sstevel@tonic-gate static void 8710Sstevel@tonic-gate server(void *cookie, char *args, size_t alen, door_desc_t *dp, 8720Sstevel@tonic-gate uint_t n_desc) 8730Sstevel@tonic-gate { 8740Sstevel@tonic-gate ucred_t *uc = NULL; 8750Sstevel@tonic-gate const priv_set_t *eset; 8760Sstevel@tonic-gate 8770Sstevel@tonic-gate zone_state_t zstate; 8780Sstevel@tonic-gate zone_cmd_t cmd; 8790Sstevel@tonic-gate zone_cmd_arg_t *zargp; 8800Sstevel@tonic-gate 8810Sstevel@tonic-gate boolean_t kernelcall; 8820Sstevel@tonic-gate 8830Sstevel@tonic-gate int rval = -1; 8840Sstevel@tonic-gate uint64_t uniqid; 8850Sstevel@tonic-gate zoneid_t zoneid = -1; 8860Sstevel@tonic-gate zlog_t zlog; 8870Sstevel@tonic-gate zlog_t *zlogp; 8880Sstevel@tonic-gate zone_cmd_rval_t *rvalp; 8890Sstevel@tonic-gate size_t rlen = getpagesize(); /* conservative */ 8902712Snn35248 fs_callback_t cb; 8912727Sedp brand_handle_t bh; 8920Sstevel@tonic-gate 8930Sstevel@tonic-gate /* LINTED E_BAD_PTR_CAST_ALIGN */ 8940Sstevel@tonic-gate zargp = (zone_cmd_arg_t *)args; 8950Sstevel@tonic-gate 8960Sstevel@tonic-gate /* 8970Sstevel@tonic-gate * When we get the door unref message, we've fdetach'd the door, and 8980Sstevel@tonic-gate * it is time for us to shut down zoneadmd. 8990Sstevel@tonic-gate */ 9000Sstevel@tonic-gate if (zargp == DOOR_UNREF_DATA) { 9010Sstevel@tonic-gate /* 9020Sstevel@tonic-gate * See comment at end of main() for info on the last rites. 9030Sstevel@tonic-gate */ 9040Sstevel@tonic-gate exit(0); 9050Sstevel@tonic-gate } 9060Sstevel@tonic-gate 9070Sstevel@tonic-gate if (zargp == NULL) { 9080Sstevel@tonic-gate (void) door_return(NULL, 0, 0, 0); 9090Sstevel@tonic-gate } 9100Sstevel@tonic-gate 9110Sstevel@tonic-gate rvalp = alloca(rlen); 9120Sstevel@tonic-gate bzero(rvalp, rlen); 9130Sstevel@tonic-gate zlog.logfile = NULL; 9140Sstevel@tonic-gate zlog.buflen = zlog.loglen = rlen - sizeof (zone_cmd_rval_t) + 1; 9150Sstevel@tonic-gate zlog.buf = rvalp->errbuf; 9160Sstevel@tonic-gate zlog.log = zlog.buf; 9170Sstevel@tonic-gate /* defer initialization of zlog.locale until after credential check */ 9180Sstevel@tonic-gate zlogp = &zlog; 9190Sstevel@tonic-gate 9200Sstevel@tonic-gate if (alen != sizeof (zone_cmd_arg_t)) { 9210Sstevel@tonic-gate /* 9220Sstevel@tonic-gate * This really shouldn't be happening. 9230Sstevel@tonic-gate */ 9242267Sdp zerror(&logsys, B_FALSE, "argument size (%d bytes) " 9252267Sdp "unexpected (expected %d bytes)", alen, 9262267Sdp sizeof (zone_cmd_arg_t)); 9270Sstevel@tonic-gate goto out; 9280Sstevel@tonic-gate } 9290Sstevel@tonic-gate cmd = zargp->cmd; 9300Sstevel@tonic-gate 9310Sstevel@tonic-gate if (door_ucred(&uc) != 0) { 9320Sstevel@tonic-gate zerror(&logsys, B_TRUE, "door_ucred"); 9330Sstevel@tonic-gate goto out; 9340Sstevel@tonic-gate } 9350Sstevel@tonic-gate eset = ucred_getprivset(uc, PRIV_EFFECTIVE); 9360Sstevel@tonic-gate if (ucred_getzoneid(uc) != GLOBAL_ZONEID || 9370Sstevel@tonic-gate (eset != NULL ? !priv_ismember(eset, PRIV_SYS_CONFIG) : 9380Sstevel@tonic-gate ucred_geteuid(uc) != 0)) { 9390Sstevel@tonic-gate zerror(&logsys, B_FALSE, "insufficient privileges"); 9400Sstevel@tonic-gate goto out; 9410Sstevel@tonic-gate } 9420Sstevel@tonic-gate 9430Sstevel@tonic-gate kernelcall = ucred_getpid(uc) == 0; 9440Sstevel@tonic-gate 9450Sstevel@tonic-gate /* 9460Sstevel@tonic-gate * This is safe because we only use a zlog_t throughout the 9470Sstevel@tonic-gate * duration of a door call; i.e., by the time the pointer 9480Sstevel@tonic-gate * might become invalid, the door call would be over. 9490Sstevel@tonic-gate */ 9500Sstevel@tonic-gate zlog.locale = kernelcall ? DEFAULT_LOCALE : zargp->locale; 9510Sstevel@tonic-gate 9520Sstevel@tonic-gate (void) mutex_lock(&lock); 9530Sstevel@tonic-gate 9540Sstevel@tonic-gate /* 9550Sstevel@tonic-gate * Once we start to really die off, we don't want more connections. 9560Sstevel@tonic-gate */ 9570Sstevel@tonic-gate if (in_death_throes) { 9580Sstevel@tonic-gate (void) mutex_unlock(&lock); 9590Sstevel@tonic-gate ucred_free(uc); 9600Sstevel@tonic-gate (void) door_return(NULL, 0, 0, 0); 9610Sstevel@tonic-gate thr_exit(NULL); 9620Sstevel@tonic-gate } 9630Sstevel@tonic-gate 9640Sstevel@tonic-gate /* 9650Sstevel@tonic-gate * Check for validity of command. 9660Sstevel@tonic-gate */ 9672712Snn35248 if (cmd != Z_READY && cmd != Z_BOOT && cmd != Z_FORCEBOOT && 9682712Snn35248 cmd != Z_REBOOT && cmd != Z_HALT && cmd != Z_NOTE_UNINSTALLING && 9692712Snn35248 cmd != Z_MOUNT && cmd != Z_FORCEMOUNT && cmd != Z_UNMOUNT) { 970766Scarlsonj zerror(&logsys, B_FALSE, "invalid command %d", (int)cmd); 9710Sstevel@tonic-gate goto out; 9720Sstevel@tonic-gate } 9730Sstevel@tonic-gate 9740Sstevel@tonic-gate if (kernelcall && (cmd != Z_HALT && cmd != Z_REBOOT)) { 9750Sstevel@tonic-gate /* 9760Sstevel@tonic-gate * Can't happen 9770Sstevel@tonic-gate */ 9780Sstevel@tonic-gate zerror(&logsys, B_FALSE, "received unexpected kernel upcall %d", 9790Sstevel@tonic-gate cmd); 9800Sstevel@tonic-gate goto out; 9810Sstevel@tonic-gate } 9820Sstevel@tonic-gate /* 9830Sstevel@tonic-gate * We ignore the possibility of someone calling zone_create(2) 9840Sstevel@tonic-gate * explicitly; all requests must come through zoneadmd. 9850Sstevel@tonic-gate */ 9860Sstevel@tonic-gate if (zone_get_state(zone_name, &zstate) != Z_OK) { 9870Sstevel@tonic-gate /* 9880Sstevel@tonic-gate * Something terribly wrong happened 9890Sstevel@tonic-gate */ 9900Sstevel@tonic-gate zerror(&logsys, B_FALSE, "unable to determine state of zone"); 9910Sstevel@tonic-gate goto out; 9920Sstevel@tonic-gate } 9930Sstevel@tonic-gate 9940Sstevel@tonic-gate if (kernelcall) { 9950Sstevel@tonic-gate /* 9960Sstevel@tonic-gate * Kernel-initiated requests may lose their validity if the 9970Sstevel@tonic-gate * zone_t the kernel was referring to has gone away. 9980Sstevel@tonic-gate */ 9990Sstevel@tonic-gate if ((zoneid = getzoneidbyname(zone_name)) == -1 || 10000Sstevel@tonic-gate zone_getattr(zoneid, ZONE_ATTR_UNIQID, &uniqid, 10010Sstevel@tonic-gate sizeof (uniqid)) == -1 || uniqid != zargp->uniqid) { 10020Sstevel@tonic-gate /* 10030Sstevel@tonic-gate * We're not talking about the same zone. The request 10040Sstevel@tonic-gate * must have arrived too late. Return error. 10050Sstevel@tonic-gate */ 10060Sstevel@tonic-gate rval = -1; 10070Sstevel@tonic-gate goto out; 10080Sstevel@tonic-gate } 10090Sstevel@tonic-gate zlogp = &logsys; /* Log errors to syslog */ 10100Sstevel@tonic-gate } 10110Sstevel@tonic-gate 10122712Snn35248 /* 10132712Snn35248 * If we are being asked to forcibly mount or boot a zone, we 10142712Snn35248 * pretend that an INCOMPLETE zone is actually INSTALLED. 10152712Snn35248 */ 10162712Snn35248 if (zstate == ZONE_STATE_INCOMPLETE && 10172712Snn35248 (cmd == Z_FORCEBOOT || cmd == Z_FORCEMOUNT)) 10182712Snn35248 zstate = ZONE_STATE_INSTALLED; 10192712Snn35248 10200Sstevel@tonic-gate switch (zstate) { 10210Sstevel@tonic-gate case ZONE_STATE_CONFIGURED: 10220Sstevel@tonic-gate case ZONE_STATE_INCOMPLETE: 10230Sstevel@tonic-gate /* 10240Sstevel@tonic-gate * Not our area of expertise; we just print a nice message 10250Sstevel@tonic-gate * and die off. 10260Sstevel@tonic-gate */ 10270Sstevel@tonic-gate zerror(zlogp, B_FALSE, 10280Sstevel@tonic-gate "%s operation is invalid for zones in state '%s'", 1029766Scarlsonj z_cmd_name(cmd), zone_state_str(zstate)); 10300Sstevel@tonic-gate break; 10310Sstevel@tonic-gate 10320Sstevel@tonic-gate case ZONE_STATE_INSTALLED: 10330Sstevel@tonic-gate switch (cmd) { 10340Sstevel@tonic-gate case Z_READY: 10355829Sgjelinek rval = zone_ready(zlogp, Z_MNT_BOOT); 10360Sstevel@tonic-gate if (rval == 0) 10370Sstevel@tonic-gate eventstream_write(Z_EVT_ZONE_READIED); 10380Sstevel@tonic-gate break; 10390Sstevel@tonic-gate case Z_BOOT: 10402712Snn35248 case Z_FORCEBOOT: 10410Sstevel@tonic-gate eventstream_write(Z_EVT_ZONE_BOOTING); 10425829Sgjelinek if ((rval = zone_ready(zlogp, Z_MNT_BOOT)) == 0) 10430Sstevel@tonic-gate rval = zone_bootup(zlogp, zargp->bootbuf); 10440Sstevel@tonic-gate audit_put_record(zlogp, uc, rval, "boot"); 10450Sstevel@tonic-gate if (rval != 0) { 10460Sstevel@tonic-gate bringup_failure_recovery = B_TRUE; 10473247Sgjelinek (void) zone_halt(zlogp, B_FALSE, B_FALSE); 10481645Scomay eventstream_write(Z_EVT_ZONE_BOOTFAILED); 10490Sstevel@tonic-gate } 10500Sstevel@tonic-gate break; 10510Sstevel@tonic-gate case Z_HALT: 10520Sstevel@tonic-gate if (kernelcall) /* Invalid; can't happen */ 10530Sstevel@tonic-gate abort(); 10540Sstevel@tonic-gate /* 10550Sstevel@tonic-gate * We could have two clients racing to halt this 10560Sstevel@tonic-gate * zone; the second client loses, but his request 10570Sstevel@tonic-gate * doesn't fail, since the zone is now in the desired 10580Sstevel@tonic-gate * state. 10590Sstevel@tonic-gate */ 10600Sstevel@tonic-gate zerror(zlogp, B_FALSE, "zone is already halted"); 10610Sstevel@tonic-gate rval = 0; 10620Sstevel@tonic-gate break; 10630Sstevel@tonic-gate case Z_REBOOT: 10640Sstevel@tonic-gate if (kernelcall) /* Invalid; can't happen */ 10650Sstevel@tonic-gate abort(); 10660Sstevel@tonic-gate zerror(zlogp, B_FALSE, "%s operation is invalid " 1067766Scarlsonj "for zones in state '%s'", z_cmd_name(cmd), 10680Sstevel@tonic-gate zone_state_str(zstate)); 10690Sstevel@tonic-gate rval = -1; 10700Sstevel@tonic-gate break; 10710Sstevel@tonic-gate case Z_NOTE_UNINSTALLING: 10720Sstevel@tonic-gate if (kernelcall) /* Invalid; can't happen */ 10730Sstevel@tonic-gate abort(); 10740Sstevel@tonic-gate /* 10750Sstevel@tonic-gate * Tell the console to print out a message about this. 10760Sstevel@tonic-gate * Once it does, we will be in_death_throes. 10770Sstevel@tonic-gate */ 10780Sstevel@tonic-gate eventstream_write(Z_EVT_ZONE_UNINSTALLING); 10790Sstevel@tonic-gate break; 1080766Scarlsonj case Z_MOUNT: 10812712Snn35248 case Z_FORCEMOUNT: 1082766Scarlsonj if (kernelcall) /* Invalid; can't happen */ 1083766Scarlsonj abort(); 10844350Std153743 if (!zone_isnative && !zone_iscluster) { 10852712Snn35248 zerror(zlogp, B_FALSE, 10862712Snn35248 "%s operation is invalid for branded " 10872712Snn35248 "zones", z_cmd_name(cmd)); 10882712Snn35248 rval = -1; 10892712Snn35248 break; 10902712Snn35248 } 10912712Snn35248 10925829Sgjelinek rval = zone_ready(zlogp, 10935829Sgjelinek strcmp(zargp->bootbuf, "-U") == 0 ? 10945829Sgjelinek Z_MNT_UPDATE : Z_MNT_SCRATCH); 10952712Snn35248 if (rval != 0) 10962712Snn35248 break; 10972712Snn35248 10982712Snn35248 eventstream_write(Z_EVT_ZONE_READIED); 10992712Snn35248 11002712Snn35248 /* Get a handle to the brand info for this zone */ 11012727Sedp if ((bh = brand_open(brand_name)) == NULL) { 11022712Snn35248 rval = -1; 11032712Snn35248 break; 11041645Scomay } 11051645Scomay 1106766Scarlsonj /* 11072712Snn35248 * Get the list of filesystems to mount from 11082712Snn35248 * the brand configuration. These mounts are done 11092712Snn35248 * via a thread that will enter the zone, so they 11102712Snn35248 * are done from within the context of the zone. 11112712Snn35248 */ 11122712Snn35248 cb.zlogp = zlogp; 11132712Snn35248 cb.zoneid = zone_id; 11145576Sedp cb.mount_cmd = B_TRUE; 11152727Sedp rval = brand_platform_iter_mounts(bh, 11162712Snn35248 mount_early_fs, &cb); 11172712Snn35248 11182727Sedp brand_close(bh); 11192712Snn35248 11202712Snn35248 /* 1121766Scarlsonj * Ordinarily, /dev/fd would be mounted inside the zone 1122766Scarlsonj * by svc:/system/filesystem/usr:default, but since 1123766Scarlsonj * we're not booting the zone, we need to do this 1124766Scarlsonj * manually. 1125766Scarlsonj */ 1126766Scarlsonj if (rval == 0) 11272712Snn35248 rval = mount_early_fs(&cb, 11282712Snn35248 "fd", "/dev/fd", "fd", NULL); 1129766Scarlsonj break; 1130766Scarlsonj case Z_UNMOUNT: 1131766Scarlsonj if (kernelcall) /* Invalid; can't happen */ 1132766Scarlsonj abort(); 1133766Scarlsonj zerror(zlogp, B_FALSE, "zone is already unmounted"); 1134766Scarlsonj rval = 0; 1135766Scarlsonj break; 11360Sstevel@tonic-gate } 11370Sstevel@tonic-gate break; 11380Sstevel@tonic-gate 11390Sstevel@tonic-gate case ZONE_STATE_READY: 11400Sstevel@tonic-gate switch (cmd) { 11410Sstevel@tonic-gate case Z_READY: 11420Sstevel@tonic-gate /* 11430Sstevel@tonic-gate * We could have two clients racing to ready this 11440Sstevel@tonic-gate * zone; the second client loses, but his request 11450Sstevel@tonic-gate * doesn't fail, since the zone is now in the desired 11460Sstevel@tonic-gate * state. 11470Sstevel@tonic-gate */ 11480Sstevel@tonic-gate zerror(zlogp, B_FALSE, "zone is already ready"); 11490Sstevel@tonic-gate rval = 0; 11500Sstevel@tonic-gate break; 11510Sstevel@tonic-gate case Z_BOOT: 11522267Sdp (void) strlcpy(boot_args, zargp->bootbuf, 11532267Sdp sizeof (boot_args)); 11540Sstevel@tonic-gate eventstream_write(Z_EVT_ZONE_BOOTING); 11550Sstevel@tonic-gate rval = zone_bootup(zlogp, zargp->bootbuf); 11560Sstevel@tonic-gate audit_put_record(zlogp, uc, rval, "boot"); 11570Sstevel@tonic-gate if (rval != 0) { 11580Sstevel@tonic-gate bringup_failure_recovery = B_TRUE; 11593247Sgjelinek (void) zone_halt(zlogp, B_FALSE, B_TRUE); 11601645Scomay eventstream_write(Z_EVT_ZONE_BOOTFAILED); 11610Sstevel@tonic-gate } 11622267Sdp boot_args[0] = '\0'; 11630Sstevel@tonic-gate break; 11640Sstevel@tonic-gate case Z_HALT: 11650Sstevel@tonic-gate if (kernelcall) /* Invalid; can't happen */ 11660Sstevel@tonic-gate abort(); 11673247Sgjelinek if ((rval = zone_halt(zlogp, B_FALSE, B_FALSE)) != 0) 11680Sstevel@tonic-gate break; 11690Sstevel@tonic-gate eventstream_write(Z_EVT_ZONE_HALTED); 11700Sstevel@tonic-gate break; 11710Sstevel@tonic-gate case Z_REBOOT: 1172766Scarlsonj case Z_NOTE_UNINSTALLING: 1173766Scarlsonj case Z_MOUNT: 1174766Scarlsonj case Z_UNMOUNT: 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; 1182766Scarlsonj } 1183766Scarlsonj break; 1184766Scarlsonj 1185766Scarlsonj case ZONE_STATE_MOUNTED: 1186766Scarlsonj switch (cmd) { 1187766Scarlsonj case Z_UNMOUNT: 11880Sstevel@tonic-gate if (kernelcall) /* Invalid; can't happen */ 11890Sstevel@tonic-gate abort(); 11903247Sgjelinek rval = zone_halt(zlogp, B_TRUE, B_FALSE); 11911645Scomay if (rval == 0) { 11921645Scomay eventstream_write(Z_EVT_ZONE_HALTED); 1193766Scarlsonj (void) sema_post(&scratch_sem); 11941645Scomay } 1195766Scarlsonj break; 1196766Scarlsonj default: 1197766Scarlsonj if (kernelcall) /* Invalid; can't happen */ 1198766Scarlsonj abort(); 1199766Scarlsonj zerror(zlogp, B_FALSE, "%s operation is invalid " 1200766Scarlsonj "for zones in state '%s'", z_cmd_name(cmd), 1201766Scarlsonj zone_state_str(zstate)); 12020Sstevel@tonic-gate rval = -1; 12030Sstevel@tonic-gate break; 12040Sstevel@tonic-gate } 12050Sstevel@tonic-gate break; 12060Sstevel@tonic-gate 12070Sstevel@tonic-gate case ZONE_STATE_RUNNING: 12080Sstevel@tonic-gate case ZONE_STATE_SHUTTING_DOWN: 12090Sstevel@tonic-gate case ZONE_STATE_DOWN: 12100Sstevel@tonic-gate switch (cmd) { 12110Sstevel@tonic-gate case Z_READY: 12123247Sgjelinek if ((rval = zone_halt(zlogp, B_FALSE, B_TRUE)) != 0) 12130Sstevel@tonic-gate break; 12145829Sgjelinek if ((rval = zone_ready(zlogp, Z_MNT_BOOT)) == 0) 12150Sstevel@tonic-gate eventstream_write(Z_EVT_ZONE_READIED); 12161645Scomay else 12171645Scomay eventstream_write(Z_EVT_ZONE_HALTED); 12180Sstevel@tonic-gate break; 12190Sstevel@tonic-gate case Z_BOOT: 12200Sstevel@tonic-gate /* 12210Sstevel@tonic-gate * We could have two clients racing to boot this 12220Sstevel@tonic-gate * zone; the second client loses, but his request 12230Sstevel@tonic-gate * doesn't fail, since the zone is now in the desired 12240Sstevel@tonic-gate * state. 12250Sstevel@tonic-gate */ 12260Sstevel@tonic-gate zerror(zlogp, B_FALSE, "zone is already booted"); 12270Sstevel@tonic-gate rval = 0; 12280Sstevel@tonic-gate break; 12290Sstevel@tonic-gate case Z_HALT: 12303247Sgjelinek if ((rval = zone_halt(zlogp, B_FALSE, B_FALSE)) != 0) 12310Sstevel@tonic-gate break; 12320Sstevel@tonic-gate eventstream_write(Z_EVT_ZONE_HALTED); 12330Sstevel@tonic-gate break; 12340Sstevel@tonic-gate case Z_REBOOT: 12352267Sdp (void) strlcpy(boot_args, zargp->bootbuf, 12362267Sdp sizeof (boot_args)); 12370Sstevel@tonic-gate eventstream_write(Z_EVT_ZONE_REBOOTING); 12383247Sgjelinek if ((rval = zone_halt(zlogp, B_FALSE, B_TRUE)) != 0) { 12391645Scomay eventstream_write(Z_EVT_ZONE_BOOTFAILED); 12402267Sdp boot_args[0] = '\0'; 12411645Scomay break; 12421645Scomay } 12435829Sgjelinek if ((rval = zone_ready(zlogp, Z_MNT_BOOT)) != 0) { 12441645Scomay eventstream_write(Z_EVT_ZONE_BOOTFAILED); 12452267Sdp boot_args[0] = '\0'; 12460Sstevel@tonic-gate break; 12471645Scomay } 12482267Sdp rval = zone_bootup(zlogp, zargp->bootbuf); 12491645Scomay audit_put_record(zlogp, uc, rval, "reboot"); 12501645Scomay if (rval != 0) { 12513247Sgjelinek (void) zone_halt(zlogp, B_FALSE, B_TRUE); 12521645Scomay eventstream_write(Z_EVT_ZONE_BOOTFAILED); 12530Sstevel@tonic-gate } 12542267Sdp boot_args[0] = '\0'; 12550Sstevel@tonic-gate break; 12560Sstevel@tonic-gate case Z_NOTE_UNINSTALLING: 1257766Scarlsonj case Z_MOUNT: 1258766Scarlsonj case Z_UNMOUNT: 1259766Scarlsonj zerror(zlogp, B_FALSE, "%s operation is invalid " 1260766Scarlsonj "for zones in state '%s'", z_cmd_name(cmd), 1261766Scarlsonj zone_state_str(zstate)); 12620Sstevel@tonic-gate rval = -1; 12630Sstevel@tonic-gate break; 12640Sstevel@tonic-gate } 12650Sstevel@tonic-gate break; 12660Sstevel@tonic-gate default: 12670Sstevel@tonic-gate abort(); 12680Sstevel@tonic-gate } 12690Sstevel@tonic-gate 12700Sstevel@tonic-gate /* 12710Sstevel@tonic-gate * Because the state of the zone may have changed, we make sure 12720Sstevel@tonic-gate * to wake the console poller, which is in charge of initiating 12730Sstevel@tonic-gate * the shutdown procedure as necessary. 12740Sstevel@tonic-gate */ 12750Sstevel@tonic-gate eventstream_write(Z_EVT_NULL); 12760Sstevel@tonic-gate 12770Sstevel@tonic-gate out: 12780Sstevel@tonic-gate (void) mutex_unlock(&lock); 12790Sstevel@tonic-gate if (kernelcall) { 12800Sstevel@tonic-gate rvalp = NULL; 12810Sstevel@tonic-gate rlen = 0; 12820Sstevel@tonic-gate } else { 12830Sstevel@tonic-gate rvalp->rval = rval; 12840Sstevel@tonic-gate } 12850Sstevel@tonic-gate if (uc != NULL) 12860Sstevel@tonic-gate ucred_free(uc); 12870Sstevel@tonic-gate (void) door_return((char *)rvalp, rlen, NULL, 0); 12880Sstevel@tonic-gate thr_exit(NULL); 12890Sstevel@tonic-gate } 12900Sstevel@tonic-gate 12910Sstevel@tonic-gate static int 12920Sstevel@tonic-gate setup_door(zlog_t *zlogp) 12930Sstevel@tonic-gate { 12940Sstevel@tonic-gate if ((zone_door = door_create(server, NULL, 12950Sstevel@tonic-gate DOOR_UNREF | DOOR_REFUSE_DESC | DOOR_NO_CANCEL)) < 0) { 12960Sstevel@tonic-gate zerror(zlogp, B_TRUE, "%s failed", "door_create"); 12970Sstevel@tonic-gate return (-1); 12980Sstevel@tonic-gate } 12990Sstevel@tonic-gate (void) fdetach(zone_door_path); 13000Sstevel@tonic-gate 13010Sstevel@tonic-gate if (fattach(zone_door, zone_door_path) != 0) { 13020Sstevel@tonic-gate zerror(zlogp, B_TRUE, "fattach to %s failed", zone_door_path); 13030Sstevel@tonic-gate (void) door_revoke(zone_door); 13040Sstevel@tonic-gate (void) fdetach(zone_door_path); 13050Sstevel@tonic-gate zone_door = -1; 13060Sstevel@tonic-gate return (-1); 13070Sstevel@tonic-gate } 13080Sstevel@tonic-gate return (0); 13090Sstevel@tonic-gate } 13100Sstevel@tonic-gate 13110Sstevel@tonic-gate /* 13120Sstevel@tonic-gate * zoneadm(1m) will start zoneadmd if it thinks it isn't running; this 13130Sstevel@tonic-gate * is where zoneadmd itself will check to see that another instance of 13140Sstevel@tonic-gate * zoneadmd isn't already controlling this zone. 13150Sstevel@tonic-gate * 13160Sstevel@tonic-gate * The idea here is that we want to open the path to which we will 13170Sstevel@tonic-gate * attach our door, lock it, and then make sure that no-one has beat us 13180Sstevel@tonic-gate * to fattach(3c)ing onto it. 13190Sstevel@tonic-gate * 13200Sstevel@tonic-gate * fattach(3c) is really a mount, so there are actually two possible 13210Sstevel@tonic-gate * vnodes we could be dealing with. Our strategy is as follows: 13220Sstevel@tonic-gate * 13230Sstevel@tonic-gate * - If the file we opened is a regular file (common case): 13240Sstevel@tonic-gate * There is no fattach(3c)ed door, so we have a chance of becoming 13250Sstevel@tonic-gate * the managing zoneadmd. We attempt to lock the file: if it is 13260Sstevel@tonic-gate * already locked, that means someone else raced us here, so we 13270Sstevel@tonic-gate * lose and give up. zoneadm(1m) will try to contact the zoneadmd 13280Sstevel@tonic-gate * that beat us to it. 13290Sstevel@tonic-gate * 13300Sstevel@tonic-gate * - If the file we opened is a namefs file: 13310Sstevel@tonic-gate * This means there is already an established door fattach(3c)'ed 13320Sstevel@tonic-gate * to the rendezvous path. We've lost the race, so we give up. 13330Sstevel@tonic-gate * Note that in this case we also try to grab the file lock, and 13340Sstevel@tonic-gate * will succeed in acquiring it since the vnode locked by the 13350Sstevel@tonic-gate * "winning" zoneadmd was a regular one, and the one we locked was 13360Sstevel@tonic-gate * the fattach(3c)'ed door node. At any rate, no harm is done, and 13370Sstevel@tonic-gate * we just return to zoneadm(1m) which knows to retry. 13380Sstevel@tonic-gate */ 13390Sstevel@tonic-gate static int 13400Sstevel@tonic-gate make_daemon_exclusive(zlog_t *zlogp) 13410Sstevel@tonic-gate { 13420Sstevel@tonic-gate int doorfd = -1; 13430Sstevel@tonic-gate int err, ret = -1; 13440Sstevel@tonic-gate struct stat st; 13450Sstevel@tonic-gate struct flock flock; 13460Sstevel@tonic-gate zone_state_t zstate; 13470Sstevel@tonic-gate 13480Sstevel@tonic-gate top: 13490Sstevel@tonic-gate if ((err = zone_get_state(zone_name, &zstate)) != Z_OK) { 13502267Sdp zerror(zlogp, B_FALSE, "failed to get zone state: %s", 13510Sstevel@tonic-gate zonecfg_strerror(err)); 13520Sstevel@tonic-gate goto out; 13530Sstevel@tonic-gate } 13540Sstevel@tonic-gate if ((doorfd = open(zone_door_path, O_CREAT|O_RDWR, 13550Sstevel@tonic-gate S_IREAD|S_IWRITE)) < 0) { 13560Sstevel@tonic-gate zerror(zlogp, B_TRUE, "failed to open %s", zone_door_path); 13570Sstevel@tonic-gate goto out; 13580Sstevel@tonic-gate } 13590Sstevel@tonic-gate if (fstat(doorfd, &st) < 0) { 13600Sstevel@tonic-gate zerror(zlogp, B_TRUE, "failed to stat %s", zone_door_path); 13610Sstevel@tonic-gate goto out; 13620Sstevel@tonic-gate } 13630Sstevel@tonic-gate /* 13640Sstevel@tonic-gate * Lock the file to synchronize with other zoneadmd 13650Sstevel@tonic-gate */ 13660Sstevel@tonic-gate flock.l_type = F_WRLCK; 13670Sstevel@tonic-gate flock.l_whence = SEEK_SET; 13680Sstevel@tonic-gate flock.l_start = (off_t)0; 13690Sstevel@tonic-gate flock.l_len = (off_t)0; 13700Sstevel@tonic-gate if (fcntl(doorfd, F_SETLK, &flock) < 0) { 13710Sstevel@tonic-gate /* 13720Sstevel@tonic-gate * Someone else raced us here and grabbed the lock file 13730Sstevel@tonic-gate * first. A warning here is inappropriate since nothing 13740Sstevel@tonic-gate * went wrong. 13750Sstevel@tonic-gate */ 13760Sstevel@tonic-gate goto out; 13770Sstevel@tonic-gate } 13780Sstevel@tonic-gate 13790Sstevel@tonic-gate if (strcmp(st.st_fstype, "namefs") == 0) { 13800Sstevel@tonic-gate struct door_info info; 13810Sstevel@tonic-gate 13820Sstevel@tonic-gate /* 13830Sstevel@tonic-gate * There is already something fattach()'ed to this file. 13840Sstevel@tonic-gate * Lets see what the door is up to. 13850Sstevel@tonic-gate */ 13860Sstevel@tonic-gate if (door_info(doorfd, &info) == 0 && info.di_target != -1) { 13870Sstevel@tonic-gate /* 13880Sstevel@tonic-gate * Another zoneadmd process seems to be in 13890Sstevel@tonic-gate * control of the situation and we don't need to 13900Sstevel@tonic-gate * be here. A warning here is inappropriate 13910Sstevel@tonic-gate * since nothing went wrong. 13920Sstevel@tonic-gate * 13930Sstevel@tonic-gate * If the door has been revoked, the zoneadmd 13940Sstevel@tonic-gate * process currently managing the zone is going 13950Sstevel@tonic-gate * away. We'll return control to zoneadm(1m) 13960Sstevel@tonic-gate * which will try again (by which time zoneadmd 13970Sstevel@tonic-gate * will hopefully have exited). 13980Sstevel@tonic-gate */ 13990Sstevel@tonic-gate goto out; 14000Sstevel@tonic-gate } 14010Sstevel@tonic-gate 14020Sstevel@tonic-gate /* 14030Sstevel@tonic-gate * If we got this far, there's a fattach(3c)'ed door 14040Sstevel@tonic-gate * that belongs to a process that has exited, which can 14050Sstevel@tonic-gate * happen if the previous zoneadmd died unexpectedly. 14060Sstevel@tonic-gate * 14070Sstevel@tonic-gate * Let user know that something is amiss, but that we can 14080Sstevel@tonic-gate * recover; if the zone is in the installed state, then don't 14090Sstevel@tonic-gate * message, since having a running zoneadmd isn't really 14100Sstevel@tonic-gate * expected/needed. We want to keep occurences of this message 14110Sstevel@tonic-gate * limited to times when zoneadmd is picking back up from a 14120Sstevel@tonic-gate * zoneadmd that died while the zone was in some non-trivial 14130Sstevel@tonic-gate * state. 14140Sstevel@tonic-gate */ 14150Sstevel@tonic-gate if (zstate > ZONE_STATE_INSTALLED) { 14160Sstevel@tonic-gate zerror(zlogp, B_FALSE, 14170Sstevel@tonic-gate "zone '%s': WARNING: zone is in state '%s', but " 14180Sstevel@tonic-gate "zoneadmd does not appear to be available; " 14190Sstevel@tonic-gate "restarted zoneadmd to recover.", 14200Sstevel@tonic-gate zone_name, zone_state_str(zstate)); 14210Sstevel@tonic-gate } 14220Sstevel@tonic-gate 14230Sstevel@tonic-gate (void) fdetach(zone_door_path); 14240Sstevel@tonic-gate (void) close(doorfd); 14250Sstevel@tonic-gate goto top; 14260Sstevel@tonic-gate } 14270Sstevel@tonic-gate ret = 0; 14280Sstevel@tonic-gate out: 14290Sstevel@tonic-gate (void) close(doorfd); 14300Sstevel@tonic-gate return (ret); 14310Sstevel@tonic-gate } 14320Sstevel@tonic-gate 14330Sstevel@tonic-gate int 14340Sstevel@tonic-gate main(int argc, char *argv[]) 14350Sstevel@tonic-gate { 14360Sstevel@tonic-gate int opt; 14370Sstevel@tonic-gate zoneid_t zid; 14380Sstevel@tonic-gate priv_set_t *privset; 14390Sstevel@tonic-gate zone_state_t zstate; 14400Sstevel@tonic-gate char parents_locale[MAXPATHLEN]; 14412727Sedp brand_handle_t bh; 14420Sstevel@tonic-gate int err; 14430Sstevel@tonic-gate 14440Sstevel@tonic-gate pid_t pid; 14450Sstevel@tonic-gate sigset_t blockset; 14460Sstevel@tonic-gate sigset_t block_cld; 14470Sstevel@tonic-gate 14480Sstevel@tonic-gate struct { 14490Sstevel@tonic-gate sema_t sem; 14500Sstevel@tonic-gate int status; 14510Sstevel@tonic-gate zlog_t log; 14520Sstevel@tonic-gate } *shstate; 14530Sstevel@tonic-gate size_t shstatelen = getpagesize(); 14540Sstevel@tonic-gate 14550Sstevel@tonic-gate zlog_t errlog; 14560Sstevel@tonic-gate zlog_t *zlogp; 14570Sstevel@tonic-gate 14582382Sdp int ctfd; 14592382Sdp 14600Sstevel@tonic-gate progname = get_execbasename(argv[0]); 14610Sstevel@tonic-gate 14620Sstevel@tonic-gate /* 14630Sstevel@tonic-gate * Make sure stderr is unbuffered 14640Sstevel@tonic-gate */ 14650Sstevel@tonic-gate (void) setbuffer(stderr, NULL, 0); 14660Sstevel@tonic-gate 14670Sstevel@tonic-gate /* 14680Sstevel@tonic-gate * Get out of the way of mounted filesystems, since we will daemonize 14690Sstevel@tonic-gate * soon. 14700Sstevel@tonic-gate */ 14710Sstevel@tonic-gate (void) chdir("/"); 14720Sstevel@tonic-gate 14730Sstevel@tonic-gate /* 14740Sstevel@tonic-gate * Use the default system umask per PSARC 1998/110 rather than 14750Sstevel@tonic-gate * anything that may have been set by the caller. 14760Sstevel@tonic-gate */ 14770Sstevel@tonic-gate (void) umask(CMASK); 14780Sstevel@tonic-gate 14790Sstevel@tonic-gate /* 14800Sstevel@tonic-gate * Initially we want to use our parent's locale. 14810Sstevel@tonic-gate */ 14820Sstevel@tonic-gate (void) setlocale(LC_ALL, ""); 14830Sstevel@tonic-gate (void) textdomain(TEXT_DOMAIN); 14840Sstevel@tonic-gate (void) strlcpy(parents_locale, setlocale(LC_MESSAGES, NULL), 14850Sstevel@tonic-gate sizeof (parents_locale)); 14860Sstevel@tonic-gate 14870Sstevel@tonic-gate /* 14880Sstevel@tonic-gate * This zlog_t is used for writing to stderr 14890Sstevel@tonic-gate */ 14900Sstevel@tonic-gate errlog.logfile = stderr; 14910Sstevel@tonic-gate errlog.buflen = errlog.loglen = 0; 14920Sstevel@tonic-gate errlog.buf = errlog.log = NULL; 14930Sstevel@tonic-gate errlog.locale = parents_locale; 14940Sstevel@tonic-gate 14950Sstevel@tonic-gate /* 14960Sstevel@tonic-gate * We start off writing to stderr until we're ready to daemonize. 14970Sstevel@tonic-gate */ 14980Sstevel@tonic-gate zlogp = &errlog; 14990Sstevel@tonic-gate 15000Sstevel@tonic-gate /* 15010Sstevel@tonic-gate * Process options. 15020Sstevel@tonic-gate */ 1503766Scarlsonj while ((opt = getopt(argc, argv, "R:z:")) != EOF) { 15040Sstevel@tonic-gate switch (opt) { 1505766Scarlsonj case 'R': 1506766Scarlsonj zonecfg_set_root(optarg); 1507766Scarlsonj break; 15080Sstevel@tonic-gate case 'z': 15090Sstevel@tonic-gate zone_name = optarg; 15100Sstevel@tonic-gate break; 15110Sstevel@tonic-gate default: 15120Sstevel@tonic-gate usage(); 15130Sstevel@tonic-gate } 15140Sstevel@tonic-gate } 15150Sstevel@tonic-gate 15160Sstevel@tonic-gate if (zone_name == NULL) 15170Sstevel@tonic-gate usage(); 15180Sstevel@tonic-gate 15190Sstevel@tonic-gate /* 15200Sstevel@tonic-gate * Because usage() prints directly to stderr, it has gettext() 15210Sstevel@tonic-gate * wrapping, which depends on the locale. But since zerror() calls 15220Sstevel@tonic-gate * localize() which tweaks the locale, it is not safe to call zerror() 15230Sstevel@tonic-gate * until after the last call to usage(). Fortunately, the last call 15240Sstevel@tonic-gate * to usage() is just above and the first call to zerror() is just 15250Sstevel@tonic-gate * below. Don't mess this up. 15260Sstevel@tonic-gate */ 15270Sstevel@tonic-gate if (strcmp(zone_name, GLOBAL_ZONENAME) == 0) { 15280Sstevel@tonic-gate zerror(zlogp, B_FALSE, "cannot manage the %s zone", 15290Sstevel@tonic-gate GLOBAL_ZONENAME); 15300Sstevel@tonic-gate return (1); 15310Sstevel@tonic-gate } 15320Sstevel@tonic-gate 15330Sstevel@tonic-gate if (zone_get_id(zone_name, &zid) != 0) { 15342267Sdp zerror(zlogp, B_FALSE, "could not manage %s: %s", zone_name, 15350Sstevel@tonic-gate zonecfg_strerror(Z_NO_ZONE)); 15360Sstevel@tonic-gate return (1); 15370Sstevel@tonic-gate } 15380Sstevel@tonic-gate 15390Sstevel@tonic-gate if ((err = zone_get_state(zone_name, &zstate)) != Z_OK) { 15402267Sdp zerror(zlogp, B_FALSE, "failed to get zone state: %s", 15410Sstevel@tonic-gate zonecfg_strerror(err)); 15420Sstevel@tonic-gate return (1); 15430Sstevel@tonic-gate } 15442712Snn35248 if (zstate < ZONE_STATE_INCOMPLETE) { 15450Sstevel@tonic-gate zerror(zlogp, B_FALSE, 15460Sstevel@tonic-gate "cannot manage a zone which is in state '%s'", 15470Sstevel@tonic-gate zone_state_str(zstate)); 15480Sstevel@tonic-gate return (1); 15490Sstevel@tonic-gate } 15500Sstevel@tonic-gate 15512712Snn35248 /* Get a handle to the brand info for this zone */ 15522712Snn35248 if ((zone_get_brand(zone_name, brand_name, sizeof (brand_name)) 15532727Sedp != Z_OK) || (bh = brand_open(brand_name)) == NULL) { 15542712Snn35248 zerror(zlogp, B_FALSE, "unable to determine zone brand"); 15552712Snn35248 return (1); 15562712Snn35248 } 15572727Sedp zone_isnative = brand_is_native(bh); 15584350Std153743 zone_iscluster = (strcmp(brand_name, CLUSTER_BRAND_NAME) == 0); 15592727Sedp brand_close(bh); 15602712Snn35248 15610Sstevel@tonic-gate /* 15620Sstevel@tonic-gate * Check that we have all privileges. It would be nice to pare 15630Sstevel@tonic-gate * this down, but this is at least a first cut. 15640Sstevel@tonic-gate */ 15650Sstevel@tonic-gate if ((privset = priv_allocset()) == NULL) { 15660Sstevel@tonic-gate zerror(zlogp, B_TRUE, "%s failed", "priv_allocset"); 15670Sstevel@tonic-gate return (1); 15680Sstevel@tonic-gate } 15690Sstevel@tonic-gate 15700Sstevel@tonic-gate if (getppriv(PRIV_EFFECTIVE, privset) != 0) { 15710Sstevel@tonic-gate zerror(zlogp, B_TRUE, "%s failed", "getppriv"); 15720Sstevel@tonic-gate priv_freeset(privset); 15730Sstevel@tonic-gate return (1); 15740Sstevel@tonic-gate } 15750Sstevel@tonic-gate 15760Sstevel@tonic-gate if (priv_isfullset(privset) == B_FALSE) { 15770Sstevel@tonic-gate zerror(zlogp, B_FALSE, "You lack sufficient privilege to " 15782267Sdp "run this command (all privs required)"); 15790Sstevel@tonic-gate priv_freeset(privset); 15800Sstevel@tonic-gate return (1); 15810Sstevel@tonic-gate } 15820Sstevel@tonic-gate priv_freeset(privset); 15830Sstevel@tonic-gate 15840Sstevel@tonic-gate if (mkzonedir(zlogp) != 0) 15850Sstevel@tonic-gate return (1); 15860Sstevel@tonic-gate 15870Sstevel@tonic-gate /* 15880Sstevel@tonic-gate * Pre-fork: setup shared state 15890Sstevel@tonic-gate */ 15900Sstevel@tonic-gate if ((shstate = (void *)mmap(NULL, shstatelen, 15910Sstevel@tonic-gate PROT_READ|PROT_WRITE, MAP_SHARED|MAP_ANON, -1, (off_t)0)) == 15920Sstevel@tonic-gate MAP_FAILED) { 15930Sstevel@tonic-gate zerror(zlogp, B_TRUE, "%s failed", "mmap"); 15940Sstevel@tonic-gate return (1); 15950Sstevel@tonic-gate } 15960Sstevel@tonic-gate if (sema_init(&shstate->sem, 0, USYNC_PROCESS, NULL) != 0) { 15970Sstevel@tonic-gate zerror(zlogp, B_TRUE, "%s failed", "sema_init()"); 15980Sstevel@tonic-gate (void) munmap((char *)shstate, shstatelen); 15990Sstevel@tonic-gate return (1); 16000Sstevel@tonic-gate } 16010Sstevel@tonic-gate shstate->log.logfile = NULL; 16020Sstevel@tonic-gate shstate->log.buflen = shstatelen - sizeof (*shstate); 16030Sstevel@tonic-gate shstate->log.loglen = shstate->log.buflen; 16040Sstevel@tonic-gate shstate->log.buf = (char *)shstate + sizeof (*shstate); 16050Sstevel@tonic-gate shstate->log.log = shstate->log.buf; 16060Sstevel@tonic-gate shstate->log.locale = parents_locale; 16070Sstevel@tonic-gate shstate->status = -1; 16080Sstevel@tonic-gate 16090Sstevel@tonic-gate /* 16100Sstevel@tonic-gate * We need a SIGCHLD handler so the sema_wait() below will wake 16110Sstevel@tonic-gate * up if the child dies without doing a sema_post(). 16120Sstevel@tonic-gate */ 16130Sstevel@tonic-gate (void) sigset(SIGCHLD, sigchld); 16140Sstevel@tonic-gate /* 16150Sstevel@tonic-gate * We must mask SIGCHLD until after we've coped with the fork 16160Sstevel@tonic-gate * sufficiently to deal with it; otherwise we can race and 16170Sstevel@tonic-gate * receive the signal before pid has been initialized 16180Sstevel@tonic-gate * (yes, this really happens). 16190Sstevel@tonic-gate */ 16200Sstevel@tonic-gate (void) sigemptyset(&block_cld); 16210Sstevel@tonic-gate (void) sigaddset(&block_cld, SIGCHLD); 16220Sstevel@tonic-gate (void) sigprocmask(SIG_BLOCK, &block_cld, NULL); 16230Sstevel@tonic-gate 16242382Sdp if ((ctfd = init_template()) == -1) { 16252382Sdp zerror(zlogp, B_TRUE, "failed to create contract"); 16262382Sdp return (1); 16272382Sdp } 16282382Sdp 16290Sstevel@tonic-gate /* 16300Sstevel@tonic-gate * Do not let another thread localize a message while we are forking. 16310Sstevel@tonic-gate */ 16320Sstevel@tonic-gate (void) mutex_lock(&msglock); 16330Sstevel@tonic-gate pid = fork(); 16340Sstevel@tonic-gate (void) mutex_unlock(&msglock); 16352382Sdp 16362382Sdp /* 16372382Sdp * In all cases (parent, child, and in the event of an error) we 16382382Sdp * don't want to cause creation of contracts on subsequent fork()s. 16392382Sdp */ 16402382Sdp (void) ct_tmpl_clear(ctfd); 16412382Sdp (void) close(ctfd); 16422382Sdp 16430Sstevel@tonic-gate if (pid == -1) { 16440Sstevel@tonic-gate zerror(zlogp, B_TRUE, "could not fork"); 16450Sstevel@tonic-gate return (1); 16460Sstevel@tonic-gate 16470Sstevel@tonic-gate } else if (pid > 0) { /* parent */ 16480Sstevel@tonic-gate (void) sigprocmask(SIG_UNBLOCK, &block_cld, NULL); 16490Sstevel@tonic-gate /* 16500Sstevel@tonic-gate * This marks a window of vulnerability in which we receive 16510Sstevel@tonic-gate * the SIGCLD before falling into sema_wait (normally we would 16520Sstevel@tonic-gate * get woken up from sema_wait with EINTR upon receipt of 16530Sstevel@tonic-gate * SIGCLD). So we may need to use some other scheme like 16540Sstevel@tonic-gate * sema_posting in the sigcld handler. 16550Sstevel@tonic-gate * blech 16560Sstevel@tonic-gate */ 16570Sstevel@tonic-gate (void) sema_wait(&shstate->sem); 16580Sstevel@tonic-gate (void) sema_destroy(&shstate->sem); 16590Sstevel@tonic-gate if (shstate->status != 0) 16600Sstevel@tonic-gate (void) waitpid(pid, NULL, WNOHANG); 16610Sstevel@tonic-gate /* 16620Sstevel@tonic-gate * It's ok if we die with SIGPIPE. It's not like we could have 16630Sstevel@tonic-gate * done anything about it. 16640Sstevel@tonic-gate */ 16650Sstevel@tonic-gate (void) fprintf(stderr, "%s", shstate->log.buf); 16660Sstevel@tonic-gate _exit(shstate->status == 0 ? 0 : 1); 16670Sstevel@tonic-gate } 16680Sstevel@tonic-gate 16690Sstevel@tonic-gate /* 16700Sstevel@tonic-gate * The child charges on. 16710Sstevel@tonic-gate */ 16720Sstevel@tonic-gate (void) sigset(SIGCHLD, SIG_DFL); 16730Sstevel@tonic-gate (void) sigprocmask(SIG_UNBLOCK, &block_cld, NULL); 16740Sstevel@tonic-gate 16750Sstevel@tonic-gate /* 16760Sstevel@tonic-gate * SIGPIPE can be delivered if we write to a socket for which the 16770Sstevel@tonic-gate * peer endpoint is gone. That can lead to too-early termination 16780Sstevel@tonic-gate * of zoneadmd, and that's not good eats. 16790Sstevel@tonic-gate */ 16800Sstevel@tonic-gate (void) sigset(SIGPIPE, SIG_IGN); 16810Sstevel@tonic-gate /* 16820Sstevel@tonic-gate * Stop using stderr 16830Sstevel@tonic-gate */ 16840Sstevel@tonic-gate zlogp = &shstate->log; 16850Sstevel@tonic-gate 16860Sstevel@tonic-gate /* 16870Sstevel@tonic-gate * We don't need stdout/stderr from now on. 16880Sstevel@tonic-gate */ 16890Sstevel@tonic-gate closefrom(0); 16900Sstevel@tonic-gate 16910Sstevel@tonic-gate /* 16920Sstevel@tonic-gate * Initialize the syslog zlog_t. This needs to be done after 16930Sstevel@tonic-gate * the call to closefrom(). 16940Sstevel@tonic-gate */ 16950Sstevel@tonic-gate logsys.buf = logsys.log = NULL; 16960Sstevel@tonic-gate logsys.buflen = logsys.loglen = 0; 16970Sstevel@tonic-gate logsys.logfile = NULL; 16980Sstevel@tonic-gate logsys.locale = DEFAULT_LOCALE; 16990Sstevel@tonic-gate 17000Sstevel@tonic-gate openlog("zoneadmd", LOG_PID, LOG_DAEMON); 17010Sstevel@tonic-gate 17020Sstevel@tonic-gate /* 17030Sstevel@tonic-gate * The eventstream is used to publish state changes in the zone 17040Sstevel@tonic-gate * from the door threads to the console I/O poller. 17050Sstevel@tonic-gate */ 17060Sstevel@tonic-gate if (eventstream_init() == -1) { 17070Sstevel@tonic-gate zerror(zlogp, B_TRUE, "unable to create eventstream"); 17080Sstevel@tonic-gate goto child_out; 17090Sstevel@tonic-gate } 17100Sstevel@tonic-gate 17110Sstevel@tonic-gate (void) snprintf(zone_door_path, sizeof (zone_door_path), 1712766Scarlsonj "%s" ZONE_DOOR_PATH, zonecfg_get_root(), zone_name); 17130Sstevel@tonic-gate 17140Sstevel@tonic-gate /* 17150Sstevel@tonic-gate * See if another zoneadmd is running for this zone. If not, then we 17160Sstevel@tonic-gate * can now modify system state. 17170Sstevel@tonic-gate */ 17180Sstevel@tonic-gate if (make_daemon_exclusive(zlogp) == -1) 17190Sstevel@tonic-gate goto child_out; 17200Sstevel@tonic-gate 17210Sstevel@tonic-gate 17220Sstevel@tonic-gate /* 17230Sstevel@tonic-gate * Create/join a new session; we need to be careful of what we do with 17240Sstevel@tonic-gate * the console from now on so we don't end up being the session leader 17250Sstevel@tonic-gate * for the terminal we're going to be handing out. 17260Sstevel@tonic-gate */ 17270Sstevel@tonic-gate (void) setsid(); 17280Sstevel@tonic-gate 17290Sstevel@tonic-gate /* 17300Sstevel@tonic-gate * This thread shouldn't be receiving any signals; in particular, 17310Sstevel@tonic-gate * SIGCHLD should be received by the thread doing the fork(). 17320Sstevel@tonic-gate */ 17330Sstevel@tonic-gate (void) sigfillset(&blockset); 17340Sstevel@tonic-gate (void) thr_sigsetmask(SIG_BLOCK, &blockset, NULL); 17350Sstevel@tonic-gate 17360Sstevel@tonic-gate /* 17370Sstevel@tonic-gate * Setup the console device and get ready to serve the console; 17380Sstevel@tonic-gate * once this has completed, we're ready to let console clients 17390Sstevel@tonic-gate * make an attempt to connect (they will block until 17400Sstevel@tonic-gate * serve_console_sock() below gets called, and any pending 17410Sstevel@tonic-gate * connection is accept()ed). 17420Sstevel@tonic-gate */ 1743766Scarlsonj if (!zonecfg_in_alt_root() && init_console(zlogp) == -1) 17440Sstevel@tonic-gate goto child_out; 17450Sstevel@tonic-gate 17460Sstevel@tonic-gate /* 17470Sstevel@tonic-gate * Take the lock now, so that when the door server gets going, we 17480Sstevel@tonic-gate * are guaranteed that it won't take a request until we are sure 17490Sstevel@tonic-gate * that everything is completely set up. See the child_out: label 17500Sstevel@tonic-gate * below to see why this matters. 17510Sstevel@tonic-gate */ 17520Sstevel@tonic-gate (void) mutex_lock(&lock); 17530Sstevel@tonic-gate 1754766Scarlsonj /* Init semaphore for scratch zones. */ 1755766Scarlsonj if (sema_init(&scratch_sem, 0, USYNC_THREAD, NULL) == -1) { 1756766Scarlsonj zerror(zlogp, B_TRUE, 1757766Scarlsonj "failed to initialize semaphore for scratch zone"); 1758766Scarlsonj goto child_out; 1759766Scarlsonj } 1760766Scarlsonj 17610Sstevel@tonic-gate /* 17620Sstevel@tonic-gate * Note: door setup must occur *after* the console is setup. 17630Sstevel@tonic-gate * This is so that as zlogin tests the door to see if zoneadmd 17640Sstevel@tonic-gate * is ready yet, we know that the console will get serviced 17650Sstevel@tonic-gate * once door_info() indicates that the door is "up". 17660Sstevel@tonic-gate */ 17670Sstevel@tonic-gate if (setup_door(zlogp) == -1) 17680Sstevel@tonic-gate goto child_out; 17690Sstevel@tonic-gate 17700Sstevel@tonic-gate /* 17710Sstevel@tonic-gate * Things seem OK so far; tell the parent process that we're done 17720Sstevel@tonic-gate * with setup tasks. This will cause the parent to exit, signalling 17730Sstevel@tonic-gate * to zoneadm, zlogin, or whatever forked it that we are ready to 17740Sstevel@tonic-gate * service requests. 17750Sstevel@tonic-gate */ 17760Sstevel@tonic-gate shstate->status = 0; 17770Sstevel@tonic-gate (void) sema_post(&shstate->sem); 17780Sstevel@tonic-gate (void) munmap((char *)shstate, shstatelen); 17790Sstevel@tonic-gate shstate = NULL; 17800Sstevel@tonic-gate 17810Sstevel@tonic-gate (void) mutex_unlock(&lock); 17820Sstevel@tonic-gate 17830Sstevel@tonic-gate /* 17840Sstevel@tonic-gate * zlogp is now invalid, so reset it to the syslog logger. 17850Sstevel@tonic-gate */ 17860Sstevel@tonic-gate zlogp = &logsys; 17870Sstevel@tonic-gate 17880Sstevel@tonic-gate /* 17890Sstevel@tonic-gate * Now that we are free of any parents, switch to the default locale. 17900Sstevel@tonic-gate */ 17910Sstevel@tonic-gate (void) setlocale(LC_ALL, DEFAULT_LOCALE); 17920Sstevel@tonic-gate 17930Sstevel@tonic-gate /* 17940Sstevel@tonic-gate * At this point the setup portion of main() is basically done, so 17950Sstevel@tonic-gate * we reuse this thread to manage the zone console. When 17960Sstevel@tonic-gate * serve_console() has returned, we are past the point of no return 17970Sstevel@tonic-gate * in the life of this zoneadmd. 17980Sstevel@tonic-gate */ 1799766Scarlsonj if (zonecfg_in_alt_root()) { 1800766Scarlsonj /* 1801766Scarlsonj * This is just awful, but mounted scratch zones don't (and 1802766Scarlsonj * can't) have consoles. We just wait for unmount instead. 1803766Scarlsonj */ 1804766Scarlsonj while (sema_wait(&scratch_sem) == EINTR) 1805766Scarlsonj ; 1806766Scarlsonj } else { 1807766Scarlsonj serve_console(zlogp); 1808766Scarlsonj assert(in_death_throes); 1809766Scarlsonj } 18100Sstevel@tonic-gate 18110Sstevel@tonic-gate /* 18120Sstevel@tonic-gate * This is the next-to-last part of the exit interlock. Upon calling 18130Sstevel@tonic-gate * fdetach(), the door will go unreferenced; once any 18140Sstevel@tonic-gate * outstanding requests (like the door thread doing Z_HALT) are 18150Sstevel@tonic-gate * done, the door will get an UNREF notification; when it handles 18160Sstevel@tonic-gate * the UNREF, the door server will cause the exit. 18170Sstevel@tonic-gate */ 18180Sstevel@tonic-gate assert(!MUTEX_HELD(&lock)); 18190Sstevel@tonic-gate (void) fdetach(zone_door_path); 18200Sstevel@tonic-gate for (;;) 18210Sstevel@tonic-gate (void) pause(); 18220Sstevel@tonic-gate 18230Sstevel@tonic-gate child_out: 18240Sstevel@tonic-gate assert(pid == 0); 18250Sstevel@tonic-gate if (shstate != NULL) { 18260Sstevel@tonic-gate shstate->status = -1; 18270Sstevel@tonic-gate (void) sema_post(&shstate->sem); 18280Sstevel@tonic-gate (void) munmap((char *)shstate, shstatelen); 18290Sstevel@tonic-gate } 18300Sstevel@tonic-gate 18310Sstevel@tonic-gate /* 18320Sstevel@tonic-gate * This might trigger an unref notification, but if so, 18330Sstevel@tonic-gate * we are still holding the lock, so our call to exit will 18340Sstevel@tonic-gate * ultimately win the race and will publish the right exit 18350Sstevel@tonic-gate * code. 18360Sstevel@tonic-gate */ 18370Sstevel@tonic-gate if (zone_door != -1) { 18380Sstevel@tonic-gate assert(MUTEX_HELD(&lock)); 18390Sstevel@tonic-gate (void) door_revoke(zone_door); 18400Sstevel@tonic-gate (void) fdetach(zone_door_path); 18410Sstevel@tonic-gate } 18420Sstevel@tonic-gate return (1); /* return from main() forcibly exits an MT process */ 18430Sstevel@tonic-gate } 1844