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 5*1645Scomay * Common Development and Distribution License (the "License"). 6*1645Scomay * 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 */ 21*1645Scomay 220Sstevel@tonic-gate /* 23*1645Scomay * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 240Sstevel@tonic-gate * Use is subject to license terms. 250Sstevel@tonic-gate */ 260Sstevel@tonic-gate 270Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 280Sstevel@tonic-gate 290Sstevel@tonic-gate /* 300Sstevel@tonic-gate * zoneadmd manages zones; one zoneadmd process is launched for each 310Sstevel@tonic-gate * non-global zone on the system. This daemon juggles four jobs: 320Sstevel@tonic-gate * 330Sstevel@tonic-gate * - Implement setup and teardown of the zone "virtual platform": mount and 340Sstevel@tonic-gate * unmount filesystems; create and destroy network interfaces; communicate 350Sstevel@tonic-gate * with devfsadmd to lay out devices for the zone; instantiate the zone 360Sstevel@tonic-gate * console device; configure process runtime attributes such as resource 370Sstevel@tonic-gate * controls, pool bindings, fine-grained privileges. 380Sstevel@tonic-gate * 390Sstevel@tonic-gate * - Launch the zone's init(1M) process. 400Sstevel@tonic-gate * 410Sstevel@tonic-gate * - Implement a door server; clients (like zoneadm) connect to the door 420Sstevel@tonic-gate * server and request zone state changes. The kernel is also a client of 430Sstevel@tonic-gate * this door server. A request to halt or reboot the zone which originates 440Sstevel@tonic-gate * *inside* the zone results in a door upcall from the kernel into zoneadmd. 450Sstevel@tonic-gate * 460Sstevel@tonic-gate * One minor problem is that messages emitted by zoneadmd need to be passed 470Sstevel@tonic-gate * back to the zoneadm process making the request. These messages need to 480Sstevel@tonic-gate * be rendered in the client's locale; so, this is passed in as part of the 490Sstevel@tonic-gate * request. The exception is the kernel upcall to zoneadmd, in which case 500Sstevel@tonic-gate * messages are syslog'd. 510Sstevel@tonic-gate * 520Sstevel@tonic-gate * To make all of this work, the Makefile adds -a to xgettext to extract *all* 530Sstevel@tonic-gate * strings, and an exclusion file (zoneadmd.xcl) is used to exclude those 540Sstevel@tonic-gate * strings which do not need to be translated. 550Sstevel@tonic-gate * 560Sstevel@tonic-gate * - Act as a console server for zlogin -C processes; see comments in zcons.c 570Sstevel@tonic-gate * for more information about the zone console architecture. 580Sstevel@tonic-gate * 590Sstevel@tonic-gate * DESIGN NOTES 600Sstevel@tonic-gate * 610Sstevel@tonic-gate * Restart: 620Sstevel@tonic-gate * A chief design constraint of zoneadmd is that it should be restartable in 630Sstevel@tonic-gate * the case that the administrator kills it off, or it suffers a fatal error, 640Sstevel@tonic-gate * without the running zone being impacted; this is akin to being able to 650Sstevel@tonic-gate * reboot the service processor of a server without affecting the OS instance. 660Sstevel@tonic-gate */ 670Sstevel@tonic-gate 680Sstevel@tonic-gate #include <sys/param.h> 690Sstevel@tonic-gate #include <sys/mman.h> 700Sstevel@tonic-gate #include <sys/types.h> 710Sstevel@tonic-gate #include <sys/stat.h> 720Sstevel@tonic-gate #include <sys/sysmacros.h> 730Sstevel@tonic-gate 740Sstevel@tonic-gate #include <bsm/adt.h> 750Sstevel@tonic-gate #include <bsm/adt_event.h> 760Sstevel@tonic-gate 770Sstevel@tonic-gate #include <alloca.h> 780Sstevel@tonic-gate #include <assert.h> 790Sstevel@tonic-gate #include <errno.h> 800Sstevel@tonic-gate #include <door.h> 810Sstevel@tonic-gate #include <fcntl.h> 820Sstevel@tonic-gate #include <locale.h> 830Sstevel@tonic-gate #include <signal.h> 840Sstevel@tonic-gate #include <stdarg.h> 850Sstevel@tonic-gate #include <stdio.h> 860Sstevel@tonic-gate #include <stdlib.h> 870Sstevel@tonic-gate #include <string.h> 880Sstevel@tonic-gate #include <strings.h> 890Sstevel@tonic-gate #include <synch.h> 900Sstevel@tonic-gate #include <syslog.h> 910Sstevel@tonic-gate #include <thread.h> 920Sstevel@tonic-gate #include <unistd.h> 930Sstevel@tonic-gate #include <wait.h> 940Sstevel@tonic-gate #include <limits.h> 950Sstevel@tonic-gate #include <zone.h> 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 */ 106766Scarlsonj static zoneid_t zone_id; 1070Sstevel@tonic-gate 1080Sstevel@tonic-gate static zlog_t logsys; 1090Sstevel@tonic-gate 1100Sstevel@tonic-gate mutex_t lock = DEFAULTMUTEX; /* to serialize stuff */ 1110Sstevel@tonic-gate mutex_t msglock = DEFAULTMUTEX; /* for calling setlocale() */ 1120Sstevel@tonic-gate 113766Scarlsonj static sema_t scratch_sem; /* for scratch zones */ 114766Scarlsonj 1150Sstevel@tonic-gate static char zone_door_path[MAXPATHLEN]; 1160Sstevel@tonic-gate static int zone_door = -1; 1170Sstevel@tonic-gate 1180Sstevel@tonic-gate boolean_t in_death_throes = B_FALSE; /* daemon is dying */ 1190Sstevel@tonic-gate boolean_t bringup_failure_recovery = B_FALSE; /* ignore certain failures */ 1200Sstevel@tonic-gate 1210Sstevel@tonic-gate #if !defined(TEXT_DOMAIN) /* should be defined by cc -D */ 1220Sstevel@tonic-gate #define TEXT_DOMAIN "SYS_TEST" /* Use this only if it wasn't */ 1230Sstevel@tonic-gate #endif 1240Sstevel@tonic-gate 1250Sstevel@tonic-gate #define PATH_TO_INIT "/sbin/init" 1260Sstevel@tonic-gate 1270Sstevel@tonic-gate #define DEFAULT_LOCALE "C" 1280Sstevel@tonic-gate 129766Scarlsonj static const char * 130766Scarlsonj z_cmd_name(zone_cmd_t zcmd) 131766Scarlsonj { 132766Scarlsonj /* This list needs to match the enum in sys/zone.h */ 133766Scarlsonj static const char *zcmdstr[] = { 134766Scarlsonj "ready", "boot", "reboot", "halt", "note_uninstalling", 135766Scarlsonj "mount", "unmount" 136766Scarlsonj }; 137766Scarlsonj 138766Scarlsonj if (zcmd >= sizeof (zcmdstr) / sizeof (*zcmdstr)) 139766Scarlsonj return ("unknown"); 140766Scarlsonj else 141766Scarlsonj return (zcmdstr[(int)zcmd]); 142766Scarlsonj } 143766Scarlsonj 1440Sstevel@tonic-gate static char * 1450Sstevel@tonic-gate get_execbasename(char *execfullname) 1460Sstevel@tonic-gate { 1470Sstevel@tonic-gate char *last_slash, *execbasename; 1480Sstevel@tonic-gate 1490Sstevel@tonic-gate /* guard against '/' at end of command invocation */ 1500Sstevel@tonic-gate for (;;) { 1510Sstevel@tonic-gate last_slash = strrchr(execfullname, '/'); 1520Sstevel@tonic-gate if (last_slash == NULL) { 1530Sstevel@tonic-gate execbasename = execfullname; 1540Sstevel@tonic-gate break; 1550Sstevel@tonic-gate } else { 1560Sstevel@tonic-gate execbasename = last_slash + 1; 1570Sstevel@tonic-gate if (*execbasename == '\0') { 1580Sstevel@tonic-gate *last_slash = '\0'; 1590Sstevel@tonic-gate continue; 1600Sstevel@tonic-gate } 1610Sstevel@tonic-gate break; 1620Sstevel@tonic-gate } 1630Sstevel@tonic-gate } 1640Sstevel@tonic-gate return (execbasename); 1650Sstevel@tonic-gate } 1660Sstevel@tonic-gate 1670Sstevel@tonic-gate static void 1680Sstevel@tonic-gate usage(void) 1690Sstevel@tonic-gate { 1700Sstevel@tonic-gate (void) fprintf(stderr, gettext("Usage: %s -z zonename\n"), progname); 1710Sstevel@tonic-gate (void) fprintf(stderr, 1720Sstevel@tonic-gate gettext("\tNote: %s should not be run directly.\n"), progname); 1730Sstevel@tonic-gate exit(2); 1740Sstevel@tonic-gate } 1750Sstevel@tonic-gate 1760Sstevel@tonic-gate /* ARGSUSED */ 1770Sstevel@tonic-gate static void 1780Sstevel@tonic-gate sigchld(int sig) 1790Sstevel@tonic-gate { 1800Sstevel@tonic-gate } 1810Sstevel@tonic-gate 1820Sstevel@tonic-gate char * 1830Sstevel@tonic-gate localize_msg(char *locale, const char *msg) 1840Sstevel@tonic-gate { 1850Sstevel@tonic-gate char *out; 1860Sstevel@tonic-gate 1870Sstevel@tonic-gate (void) mutex_lock(&msglock); 1880Sstevel@tonic-gate (void) setlocale(LC_MESSAGES, locale); 1890Sstevel@tonic-gate out = gettext(msg); 1900Sstevel@tonic-gate (void) setlocale(LC_MESSAGES, DEFAULT_LOCALE); 1910Sstevel@tonic-gate (void) mutex_unlock(&msglock); 1920Sstevel@tonic-gate return (out); 1930Sstevel@tonic-gate } 1940Sstevel@tonic-gate 1950Sstevel@tonic-gate /* PRINTFLIKE3 */ 1960Sstevel@tonic-gate void 1970Sstevel@tonic-gate zerror(zlog_t *zlogp, boolean_t use_strerror, const char *fmt, ...) 1980Sstevel@tonic-gate { 1990Sstevel@tonic-gate va_list alist; 2000Sstevel@tonic-gate char buf[MAXPATHLEN * 2]; /* enough space for err msg with a path */ 2010Sstevel@tonic-gate char *bp; 2020Sstevel@tonic-gate int saved_errno = errno; 2030Sstevel@tonic-gate 2040Sstevel@tonic-gate if (zlogp == NULL) 2050Sstevel@tonic-gate return; 2060Sstevel@tonic-gate if (zlogp == &logsys) 2070Sstevel@tonic-gate (void) snprintf(buf, sizeof (buf), "[zone '%s'] ", 2080Sstevel@tonic-gate zone_name); 2090Sstevel@tonic-gate else 2100Sstevel@tonic-gate buf[0] = '\0'; 2110Sstevel@tonic-gate bp = &(buf[strlen(buf)]); 2120Sstevel@tonic-gate 2130Sstevel@tonic-gate /* 2140Sstevel@tonic-gate * In theory, the locale pointer should be set to either "C" or a 2150Sstevel@tonic-gate * char array, so it should never be NULL 2160Sstevel@tonic-gate */ 2170Sstevel@tonic-gate assert(zlogp->locale != NULL); 2180Sstevel@tonic-gate /* Locale is per process, but we are multi-threaded... */ 2190Sstevel@tonic-gate fmt = localize_msg(zlogp->locale, fmt); 2200Sstevel@tonic-gate 2210Sstevel@tonic-gate va_start(alist, fmt); 2220Sstevel@tonic-gate (void) vsnprintf(bp, sizeof (buf) - (bp - buf), fmt, alist); 2230Sstevel@tonic-gate va_end(alist); 2240Sstevel@tonic-gate bp = &(buf[strlen(buf)]); 2250Sstevel@tonic-gate if (use_strerror) 2260Sstevel@tonic-gate (void) snprintf(bp, sizeof (buf) - (bp - buf), ": %s", 2270Sstevel@tonic-gate strerror(saved_errno)); 2280Sstevel@tonic-gate if (zlogp == &logsys) { 2290Sstevel@tonic-gate (void) syslog(LOG_ERR, "%s", buf); 2300Sstevel@tonic-gate } else if (zlogp->logfile != NULL) { 2310Sstevel@tonic-gate (void) fprintf(zlogp->logfile, "%s\n", buf); 2320Sstevel@tonic-gate } else { 2330Sstevel@tonic-gate size_t buflen; 2340Sstevel@tonic-gate size_t copylen; 2350Sstevel@tonic-gate 2360Sstevel@tonic-gate buflen = snprintf(zlogp->log, zlogp->loglen, "%s\n", buf); 2370Sstevel@tonic-gate copylen = MIN(buflen, zlogp->loglen); 2380Sstevel@tonic-gate zlogp->log += copylen; 2390Sstevel@tonic-gate zlogp->loglen -= copylen; 2400Sstevel@tonic-gate } 2410Sstevel@tonic-gate } 2420Sstevel@tonic-gate 2430Sstevel@tonic-gate static int 2440Sstevel@tonic-gate mkzonedir(zlog_t *zlogp) 2450Sstevel@tonic-gate { 2460Sstevel@tonic-gate struct stat st; 2470Sstevel@tonic-gate /* 2480Sstevel@tonic-gate * We must create and lock everyone but root out of ZONES_TMPDIR 2490Sstevel@tonic-gate * since anyone can open any UNIX domain socket, regardless of 2500Sstevel@tonic-gate * its file system permissions. Sigh... 2510Sstevel@tonic-gate */ 2520Sstevel@tonic-gate if (mkdir(ZONES_TMPDIR, S_IRWXU) < 0 && errno != EEXIST) { 2530Sstevel@tonic-gate zerror(zlogp, B_TRUE, "could not mkdir '%s'", ZONES_TMPDIR); 2540Sstevel@tonic-gate return (-1); 2550Sstevel@tonic-gate } 2560Sstevel@tonic-gate /* paranoia */ 257871Scasper if ((stat(ZONES_TMPDIR, &st) < 0) || !S_ISDIR(st.st_mode)) { 2580Sstevel@tonic-gate zerror(zlogp, B_TRUE, "'%s' is not a directory", ZONES_TMPDIR); 2590Sstevel@tonic-gate return (-1); 2600Sstevel@tonic-gate } 2610Sstevel@tonic-gate (void) chmod(ZONES_TMPDIR, S_IRWXU); 2620Sstevel@tonic-gate return (0); 2630Sstevel@tonic-gate } 2640Sstevel@tonic-gate 265766Scarlsonj /* 266766Scarlsonj * Bring a zone up to the pre-boot "ready" stage. The mount_cmd argument is 267766Scarlsonj * 'true' if this is being invoked as part of the processing for the "mount" 268766Scarlsonj * subcommand. 269766Scarlsonj */ 270766Scarlsonj static int 271766Scarlsonj zone_ready(zlog_t *zlogp, boolean_t mount_cmd) 2720Sstevel@tonic-gate { 2730Sstevel@tonic-gate int err; 2740Sstevel@tonic-gate 2750Sstevel@tonic-gate if ((err = zonecfg_create_snapshot(zone_name)) != Z_OK) { 2760Sstevel@tonic-gate zerror(zlogp, B_FALSE, "unable to create snapshot: %s", 2770Sstevel@tonic-gate zonecfg_strerror(err)); 2780Sstevel@tonic-gate return (-1); 2790Sstevel@tonic-gate } 2800Sstevel@tonic-gate 281766Scarlsonj if ((zone_id = vplat_create(zlogp, mount_cmd)) == -1) { 2820Sstevel@tonic-gate if ((err = zonecfg_destroy_snapshot(zone_name)) != Z_OK) 2830Sstevel@tonic-gate zerror(zlogp, B_FALSE, "destroying snapshot: %s", 2840Sstevel@tonic-gate zonecfg_strerror(err)); 2850Sstevel@tonic-gate return (-1); 2860Sstevel@tonic-gate } 287766Scarlsonj if (vplat_bringup(zlogp, mount_cmd) != 0) { 2880Sstevel@tonic-gate bringup_failure_recovery = B_TRUE; 289766Scarlsonj (void) vplat_teardown(NULL, mount_cmd); 2900Sstevel@tonic-gate if ((err = zonecfg_destroy_snapshot(zone_name)) != Z_OK) 2910Sstevel@tonic-gate zerror(zlogp, B_FALSE, "destroying snapshot: %s", 2920Sstevel@tonic-gate zonecfg_strerror(err)); 2930Sstevel@tonic-gate return (-1); 2940Sstevel@tonic-gate } 2950Sstevel@tonic-gate 2960Sstevel@tonic-gate return (0); 2970Sstevel@tonic-gate } 2980Sstevel@tonic-gate 2990Sstevel@tonic-gate static int 3000Sstevel@tonic-gate init_template() 3010Sstevel@tonic-gate { 3020Sstevel@tonic-gate int fd; 3030Sstevel@tonic-gate int err = 0; 3040Sstevel@tonic-gate 3050Sstevel@tonic-gate fd = open64(CTFS_ROOT "/process/template", O_RDWR); 3060Sstevel@tonic-gate if (fd == -1) 3070Sstevel@tonic-gate return (-1); 3080Sstevel@tonic-gate 3090Sstevel@tonic-gate /* 3100Sstevel@tonic-gate * For now, zoneadmd doesn't do anything with the contract. 3110Sstevel@tonic-gate * Deliver no events, don't inherit, and allow it to be orphaned. 3120Sstevel@tonic-gate */ 3130Sstevel@tonic-gate err |= ct_tmpl_set_critical(fd, 0); 3140Sstevel@tonic-gate err |= ct_tmpl_set_informative(fd, 0); 3150Sstevel@tonic-gate err |= ct_pr_tmpl_set_fatal(fd, CT_PR_EV_HWERR); 3160Sstevel@tonic-gate err |= ct_pr_tmpl_set_param(fd, CT_PR_PGRPONLY | CT_PR_REGENT); 3170Sstevel@tonic-gate if (err || ct_tmpl_activate(fd)) { 3180Sstevel@tonic-gate (void) close(fd); 3190Sstevel@tonic-gate return (-1); 3200Sstevel@tonic-gate } 3210Sstevel@tonic-gate 3220Sstevel@tonic-gate return (fd); 3230Sstevel@tonic-gate } 3240Sstevel@tonic-gate 3250Sstevel@tonic-gate static int 3260Sstevel@tonic-gate mount_early_fs(zlog_t *zlogp, zoneid_t zoneid, const char *spec, 3270Sstevel@tonic-gate const char *dir, char *fstype) 3280Sstevel@tonic-gate { 3290Sstevel@tonic-gate pid_t child; 3300Sstevel@tonic-gate int child_status; 3310Sstevel@tonic-gate int tmpl_fd; 3320Sstevel@tonic-gate ctid_t ct; 3330Sstevel@tonic-gate 3340Sstevel@tonic-gate if ((tmpl_fd = init_template()) == -1) { 3350Sstevel@tonic-gate zerror(zlogp, B_TRUE, "failed to create contract"); 3360Sstevel@tonic-gate return (-1); 3370Sstevel@tonic-gate } 3380Sstevel@tonic-gate 3390Sstevel@tonic-gate if ((child = fork()) == -1) { 3400Sstevel@tonic-gate (void) ct_tmpl_clear(tmpl_fd); 3410Sstevel@tonic-gate (void) close(tmpl_fd); 3420Sstevel@tonic-gate zerror(zlogp, B_TRUE, "failed to fork"); 3430Sstevel@tonic-gate return (-1); 3440Sstevel@tonic-gate 3450Sstevel@tonic-gate } else if (child == 0) { /* child */ 3460Sstevel@tonic-gate (void) ct_tmpl_clear(tmpl_fd); 3470Sstevel@tonic-gate /* 3480Sstevel@tonic-gate * Even though there are no procs running in the zone, we 3490Sstevel@tonic-gate * do this for paranoia's sake. 3500Sstevel@tonic-gate */ 3510Sstevel@tonic-gate (void) closefrom(0); 3520Sstevel@tonic-gate 3530Sstevel@tonic-gate if (zone_enter(zoneid) == -1) { 3540Sstevel@tonic-gate _exit(errno); 3550Sstevel@tonic-gate } 3560Sstevel@tonic-gate if (mount(spec, dir, MS_DATA, fstype, NULL, 0, NULL, 0) != 0) 3570Sstevel@tonic-gate _exit(errno); 3580Sstevel@tonic-gate _exit(0); 3590Sstevel@tonic-gate } 3600Sstevel@tonic-gate 3610Sstevel@tonic-gate /* parent */ 3620Sstevel@tonic-gate if (contract_latest(&ct) == -1) 3630Sstevel@tonic-gate ct = -1; 3640Sstevel@tonic-gate (void) ct_tmpl_clear(tmpl_fd); 3650Sstevel@tonic-gate (void) close(tmpl_fd); 3660Sstevel@tonic-gate if (waitpid(child, &child_status, 0) != child) { 3670Sstevel@tonic-gate /* unexpected: we must have been signalled */ 3680Sstevel@tonic-gate (void) contract_abandon_id(ct); 3690Sstevel@tonic-gate return (-1); 3700Sstevel@tonic-gate } 3710Sstevel@tonic-gate (void) contract_abandon_id(ct); 3720Sstevel@tonic-gate if (WEXITSTATUS(child_status) != 0) { 3730Sstevel@tonic-gate errno = WEXITSTATUS(child_status); 3740Sstevel@tonic-gate zerror(zlogp, B_TRUE, "mount of %s failed", dir); 3750Sstevel@tonic-gate return (-1); 3760Sstevel@tonic-gate } 3770Sstevel@tonic-gate 3780Sstevel@tonic-gate return (0); 3790Sstevel@tonic-gate } 3800Sstevel@tonic-gate 3810Sstevel@tonic-gate static int 382766Scarlsonj zone_mount_early(zlog_t *zlogp, zoneid_t zoneid) 383766Scarlsonj { 384766Scarlsonj if (mount_early_fs(zlogp, zoneid, "/proc", "/proc", "proc") != 0) 385766Scarlsonj return (-1); 386766Scarlsonj 387766Scarlsonj if (mount_early_fs(zlogp, zoneid, "ctfs", CTFS_ROOT, "ctfs") != 0) 388766Scarlsonj return (-1); 389766Scarlsonj 390766Scarlsonj if (mount_early_fs(zlogp, zoneid, "swap", "/etc/svc/volatile", 391766Scarlsonj "tmpfs") != 0) 392766Scarlsonj return (-1); 393766Scarlsonj 394766Scarlsonj if (mount_early_fs(zlogp, zoneid, "mnttab", "/etc/mnttab", 395766Scarlsonj "mntfs") != 0) 396766Scarlsonj return (-1); 397766Scarlsonj 398766Scarlsonj return (0); 399766Scarlsonj } 400766Scarlsonj 401766Scarlsonj static int 4020Sstevel@tonic-gate zone_bootup(zlog_t *zlogp, const char *bootargs) 4030Sstevel@tonic-gate { 4040Sstevel@tonic-gate zoneid_t zoneid; 4050Sstevel@tonic-gate struct stat st; 4060Sstevel@tonic-gate char zroot[MAXPATHLEN], initpath[MAXPATHLEN]; 4070Sstevel@tonic-gate 4080Sstevel@tonic-gate if (init_console_slave(zlogp) != 0) 4090Sstevel@tonic-gate return (-1); 4100Sstevel@tonic-gate reset_slave_terminal(zlogp); 4110Sstevel@tonic-gate 4120Sstevel@tonic-gate if ((zoneid = getzoneidbyname(zone_name)) == -1) { 4130Sstevel@tonic-gate zerror(zlogp, B_TRUE, "unable to get zoneid"); 4140Sstevel@tonic-gate return (-1); 4150Sstevel@tonic-gate } 4160Sstevel@tonic-gate 417766Scarlsonj if (zone_mount_early(zlogp, zoneid) != 0) 4180Sstevel@tonic-gate return (-1); 4190Sstevel@tonic-gate 4200Sstevel@tonic-gate /* 4210Sstevel@tonic-gate * Try to anticipate possible problems: Make sure init is executable. 4220Sstevel@tonic-gate */ 4230Sstevel@tonic-gate if (zone_get_rootpath(zone_name, zroot, sizeof (zroot)) != Z_OK) { 4240Sstevel@tonic-gate zerror(zlogp, B_FALSE, "unable to determine zone root"); 4250Sstevel@tonic-gate return (-1); 4260Sstevel@tonic-gate } 4270Sstevel@tonic-gate (void) snprintf(initpath, sizeof (initpath), "%s%s", zroot, 4280Sstevel@tonic-gate PATH_TO_INIT); 4290Sstevel@tonic-gate 4300Sstevel@tonic-gate if (stat(initpath, &st) == -1) { 4310Sstevel@tonic-gate zerror(zlogp, B_TRUE, "could not stat %s", initpath); 4320Sstevel@tonic-gate return (-1); 4330Sstevel@tonic-gate } 4340Sstevel@tonic-gate 4350Sstevel@tonic-gate if ((st.st_mode & S_IXUSR) == 0) { 4360Sstevel@tonic-gate zerror(zlogp, B_FALSE, "%s is not executable", initpath); 4370Sstevel@tonic-gate return (-1); 4380Sstevel@tonic-gate } 4390Sstevel@tonic-gate 4400Sstevel@tonic-gate if (zone_boot(zoneid, bootargs) == -1) { 4410Sstevel@tonic-gate zerror(zlogp, B_TRUE, "unable to boot zone"); 4420Sstevel@tonic-gate return (-1); 4430Sstevel@tonic-gate } 4440Sstevel@tonic-gate 4450Sstevel@tonic-gate return (0); 4460Sstevel@tonic-gate } 4470Sstevel@tonic-gate 4480Sstevel@tonic-gate static int 449766Scarlsonj zone_halt(zlog_t *zlogp, boolean_t unmount_cmd) 4500Sstevel@tonic-gate { 4510Sstevel@tonic-gate int err; 4520Sstevel@tonic-gate 453766Scarlsonj if (vplat_teardown(zlogp, unmount_cmd) != 0) { 4540Sstevel@tonic-gate if (!bringup_failure_recovery) 4550Sstevel@tonic-gate zerror(zlogp, B_FALSE, "unable to destroy zone"); 4560Sstevel@tonic-gate return (-1); 4570Sstevel@tonic-gate } 4580Sstevel@tonic-gate 4590Sstevel@tonic-gate if ((err = zonecfg_destroy_snapshot(zone_name)) != Z_OK) 4600Sstevel@tonic-gate zerror(zlogp, B_FALSE, "destroying snapshot: %s", 4610Sstevel@tonic-gate zonecfg_strerror(err)); 4620Sstevel@tonic-gate 4630Sstevel@tonic-gate return (0); 4640Sstevel@tonic-gate } 4650Sstevel@tonic-gate 4660Sstevel@tonic-gate /* 4670Sstevel@tonic-gate * Generate AUE_zone_state for a command that boots a zone. 4680Sstevel@tonic-gate */ 4690Sstevel@tonic-gate static void 4700Sstevel@tonic-gate audit_put_record(zlog_t *zlogp, ucred_t *uc, int return_val, 4710Sstevel@tonic-gate char *new_state) 4720Sstevel@tonic-gate { 4730Sstevel@tonic-gate adt_session_data_t *ah; 4740Sstevel@tonic-gate adt_event_data_t *event; 4750Sstevel@tonic-gate int pass_fail, fail_reason; 4760Sstevel@tonic-gate 4770Sstevel@tonic-gate if (!adt_audit_enabled()) 4780Sstevel@tonic-gate return; 4790Sstevel@tonic-gate 4800Sstevel@tonic-gate if (return_val == 0) { 4810Sstevel@tonic-gate pass_fail = ADT_SUCCESS; 4820Sstevel@tonic-gate fail_reason = ADT_SUCCESS; 4830Sstevel@tonic-gate } else { 4840Sstevel@tonic-gate pass_fail = ADT_FAILURE; 4850Sstevel@tonic-gate fail_reason = ADT_FAIL_VALUE_PROGRAM; 4860Sstevel@tonic-gate } 4870Sstevel@tonic-gate 4880Sstevel@tonic-gate if (adt_start_session(&ah, NULL, 0)) { 4890Sstevel@tonic-gate zerror(zlogp, B_TRUE, gettext("audit failure.")); 4900Sstevel@tonic-gate return; 4910Sstevel@tonic-gate } 4920Sstevel@tonic-gate if (adt_set_from_ucred(ah, uc, ADT_NEW)) { 4930Sstevel@tonic-gate zerror(zlogp, B_TRUE, gettext("audit failure.")); 4940Sstevel@tonic-gate (void) adt_end_session(ah); 4950Sstevel@tonic-gate return; 4960Sstevel@tonic-gate } 4970Sstevel@tonic-gate 4980Sstevel@tonic-gate event = adt_alloc_event(ah, ADT_zone_state); 4990Sstevel@tonic-gate if (event == NULL) { 5000Sstevel@tonic-gate zerror(zlogp, B_TRUE, gettext("audit failure.")); 5010Sstevel@tonic-gate (void) adt_end_session(ah); 5020Sstevel@tonic-gate return; 5030Sstevel@tonic-gate } 5040Sstevel@tonic-gate event->adt_zone_state.zonename = zone_name; 5050Sstevel@tonic-gate event->adt_zone_state.new_state = new_state; 5060Sstevel@tonic-gate 5070Sstevel@tonic-gate if (adt_put_event(event, pass_fail, fail_reason)) 5080Sstevel@tonic-gate zerror(zlogp, B_TRUE, gettext("audit failure.")); 5090Sstevel@tonic-gate 5100Sstevel@tonic-gate adt_free_event(event); 5110Sstevel@tonic-gate 5120Sstevel@tonic-gate (void) adt_end_session(ah); 5130Sstevel@tonic-gate } 5140Sstevel@tonic-gate 5150Sstevel@tonic-gate /* 5160Sstevel@tonic-gate * The main routine for the door server that deals with zone state transitions. 5170Sstevel@tonic-gate */ 5180Sstevel@tonic-gate /* ARGSUSED */ 5190Sstevel@tonic-gate static void 5200Sstevel@tonic-gate server(void *cookie, char *args, size_t alen, door_desc_t *dp, 5210Sstevel@tonic-gate uint_t n_desc) 5220Sstevel@tonic-gate { 5230Sstevel@tonic-gate ucred_t *uc = NULL; 5240Sstevel@tonic-gate const priv_set_t *eset; 5250Sstevel@tonic-gate 5260Sstevel@tonic-gate zone_state_t zstate; 5270Sstevel@tonic-gate zone_cmd_t cmd; 5280Sstevel@tonic-gate zone_cmd_arg_t *zargp; 5290Sstevel@tonic-gate 5300Sstevel@tonic-gate boolean_t kernelcall; 5310Sstevel@tonic-gate 5320Sstevel@tonic-gate int rval = -1; 5330Sstevel@tonic-gate uint64_t uniqid; 5340Sstevel@tonic-gate zoneid_t zoneid = -1; 5350Sstevel@tonic-gate zlog_t zlog; 5360Sstevel@tonic-gate zlog_t *zlogp; 5370Sstevel@tonic-gate zone_cmd_rval_t *rvalp; 5380Sstevel@tonic-gate size_t rlen = getpagesize(); /* conservative */ 5390Sstevel@tonic-gate 5400Sstevel@tonic-gate /* LINTED E_BAD_PTR_CAST_ALIGN */ 5410Sstevel@tonic-gate zargp = (zone_cmd_arg_t *)args; 5420Sstevel@tonic-gate 5430Sstevel@tonic-gate /* 5440Sstevel@tonic-gate * When we get the door unref message, we've fdetach'd the door, and 5450Sstevel@tonic-gate * it is time for us to shut down zoneadmd. 5460Sstevel@tonic-gate */ 5470Sstevel@tonic-gate if (zargp == DOOR_UNREF_DATA) { 5480Sstevel@tonic-gate /* 5490Sstevel@tonic-gate * See comment at end of main() for info on the last rites. 5500Sstevel@tonic-gate */ 5510Sstevel@tonic-gate exit(0); 5520Sstevel@tonic-gate } 5530Sstevel@tonic-gate 5540Sstevel@tonic-gate if (zargp == NULL) { 5550Sstevel@tonic-gate (void) door_return(NULL, 0, 0, 0); 5560Sstevel@tonic-gate } 5570Sstevel@tonic-gate 5580Sstevel@tonic-gate rvalp = alloca(rlen); 5590Sstevel@tonic-gate bzero(rvalp, rlen); 5600Sstevel@tonic-gate zlog.logfile = NULL; 5610Sstevel@tonic-gate zlog.buflen = zlog.loglen = rlen - sizeof (zone_cmd_rval_t) + 1; 5620Sstevel@tonic-gate zlog.buf = rvalp->errbuf; 5630Sstevel@tonic-gate zlog.log = zlog.buf; 5640Sstevel@tonic-gate /* defer initialization of zlog.locale until after credential check */ 5650Sstevel@tonic-gate zlogp = &zlog; 5660Sstevel@tonic-gate 5670Sstevel@tonic-gate if (alen != sizeof (zone_cmd_arg_t)) { 5680Sstevel@tonic-gate /* 5690Sstevel@tonic-gate * This really shouldn't be happening. 5700Sstevel@tonic-gate */ 5710Sstevel@tonic-gate zerror(&logsys, B_FALSE, "invalid argument"); 5720Sstevel@tonic-gate goto out; 5730Sstevel@tonic-gate } 5740Sstevel@tonic-gate cmd = zargp->cmd; 5750Sstevel@tonic-gate 5760Sstevel@tonic-gate if (door_ucred(&uc) != 0) { 5770Sstevel@tonic-gate zerror(&logsys, B_TRUE, "door_ucred"); 5780Sstevel@tonic-gate goto out; 5790Sstevel@tonic-gate } 5800Sstevel@tonic-gate eset = ucred_getprivset(uc, PRIV_EFFECTIVE); 5810Sstevel@tonic-gate if (ucred_getzoneid(uc) != GLOBAL_ZONEID || 5820Sstevel@tonic-gate (eset != NULL ? !priv_ismember(eset, PRIV_SYS_CONFIG) : 5830Sstevel@tonic-gate ucred_geteuid(uc) != 0)) { 5840Sstevel@tonic-gate zerror(&logsys, B_FALSE, "insufficient privileges"); 5850Sstevel@tonic-gate goto out; 5860Sstevel@tonic-gate } 5870Sstevel@tonic-gate 5880Sstevel@tonic-gate kernelcall = ucred_getpid(uc) == 0; 5890Sstevel@tonic-gate 5900Sstevel@tonic-gate /* 5910Sstevel@tonic-gate * This is safe because we only use a zlog_t throughout the 5920Sstevel@tonic-gate * duration of a door call; i.e., by the time the pointer 5930Sstevel@tonic-gate * might become invalid, the door call would be over. 5940Sstevel@tonic-gate */ 5950Sstevel@tonic-gate zlog.locale = kernelcall ? DEFAULT_LOCALE : zargp->locale; 5960Sstevel@tonic-gate 5970Sstevel@tonic-gate (void) mutex_lock(&lock); 5980Sstevel@tonic-gate 5990Sstevel@tonic-gate /* 6000Sstevel@tonic-gate * Once we start to really die off, we don't want more connections. 6010Sstevel@tonic-gate */ 6020Sstevel@tonic-gate if (in_death_throes) { 6030Sstevel@tonic-gate (void) mutex_unlock(&lock); 6040Sstevel@tonic-gate ucred_free(uc); 6050Sstevel@tonic-gate (void) door_return(NULL, 0, 0, 0); 6060Sstevel@tonic-gate thr_exit(NULL); 6070Sstevel@tonic-gate } 6080Sstevel@tonic-gate 6090Sstevel@tonic-gate /* 6100Sstevel@tonic-gate * Check for validity of command. 6110Sstevel@tonic-gate */ 6120Sstevel@tonic-gate if (cmd != Z_READY && cmd != Z_BOOT && cmd != Z_REBOOT && 613766Scarlsonj cmd != Z_HALT && cmd != Z_NOTE_UNINSTALLING && cmd != Z_MOUNT && 614766Scarlsonj cmd != Z_UNMOUNT) { 615766Scarlsonj zerror(&logsys, B_FALSE, "invalid command %d", (int)cmd); 6160Sstevel@tonic-gate goto out; 6170Sstevel@tonic-gate } 6180Sstevel@tonic-gate 6190Sstevel@tonic-gate if (kernelcall && (cmd != Z_HALT && cmd != Z_REBOOT)) { 6200Sstevel@tonic-gate /* 6210Sstevel@tonic-gate * Can't happen 6220Sstevel@tonic-gate */ 6230Sstevel@tonic-gate zerror(&logsys, B_FALSE, "received unexpected kernel upcall %d", 6240Sstevel@tonic-gate cmd); 6250Sstevel@tonic-gate goto out; 6260Sstevel@tonic-gate } 6270Sstevel@tonic-gate /* 6280Sstevel@tonic-gate * We ignore the possibility of someone calling zone_create(2) 6290Sstevel@tonic-gate * explicitly; all requests must come through zoneadmd. 6300Sstevel@tonic-gate */ 6310Sstevel@tonic-gate if (zone_get_state(zone_name, &zstate) != Z_OK) { 6320Sstevel@tonic-gate /* 6330Sstevel@tonic-gate * Something terribly wrong happened 6340Sstevel@tonic-gate */ 6350Sstevel@tonic-gate zerror(&logsys, B_FALSE, "unable to determine state of zone"); 6360Sstevel@tonic-gate goto out; 6370Sstevel@tonic-gate } 6380Sstevel@tonic-gate 6390Sstevel@tonic-gate if (kernelcall) { 6400Sstevel@tonic-gate /* 6410Sstevel@tonic-gate * Kernel-initiated requests may lose their validity if the 6420Sstevel@tonic-gate * zone_t the kernel was referring to has gone away. 6430Sstevel@tonic-gate */ 6440Sstevel@tonic-gate if ((zoneid = getzoneidbyname(zone_name)) == -1 || 6450Sstevel@tonic-gate zone_getattr(zoneid, ZONE_ATTR_UNIQID, &uniqid, 6460Sstevel@tonic-gate sizeof (uniqid)) == -1 || uniqid != zargp->uniqid) { 6470Sstevel@tonic-gate /* 6480Sstevel@tonic-gate * We're not talking about the same zone. The request 6490Sstevel@tonic-gate * must have arrived too late. Return error. 6500Sstevel@tonic-gate */ 6510Sstevel@tonic-gate rval = -1; 6520Sstevel@tonic-gate goto out; 6530Sstevel@tonic-gate } 6540Sstevel@tonic-gate zlogp = &logsys; /* Log errors to syslog */ 6550Sstevel@tonic-gate } 6560Sstevel@tonic-gate 6570Sstevel@tonic-gate switch (zstate) { 6580Sstevel@tonic-gate case ZONE_STATE_CONFIGURED: 6590Sstevel@tonic-gate case ZONE_STATE_INCOMPLETE: 6600Sstevel@tonic-gate /* 6610Sstevel@tonic-gate * Not our area of expertise; we just print a nice message 6620Sstevel@tonic-gate * and die off. 6630Sstevel@tonic-gate */ 6640Sstevel@tonic-gate zerror(zlogp, B_FALSE, 6650Sstevel@tonic-gate "%s operation is invalid for zones in state '%s'", 666766Scarlsonj z_cmd_name(cmd), zone_state_str(zstate)); 6670Sstevel@tonic-gate break; 6680Sstevel@tonic-gate 6690Sstevel@tonic-gate case ZONE_STATE_INSTALLED: 6700Sstevel@tonic-gate switch (cmd) { 6710Sstevel@tonic-gate case Z_READY: 672766Scarlsonj rval = zone_ready(zlogp, B_FALSE); 6730Sstevel@tonic-gate if (rval == 0) 6740Sstevel@tonic-gate eventstream_write(Z_EVT_ZONE_READIED); 6750Sstevel@tonic-gate break; 6760Sstevel@tonic-gate case Z_BOOT: 6770Sstevel@tonic-gate eventstream_write(Z_EVT_ZONE_BOOTING); 678766Scarlsonj if ((rval = zone_ready(zlogp, B_FALSE)) == 0) 6790Sstevel@tonic-gate rval = zone_bootup(zlogp, zargp->bootbuf); 6800Sstevel@tonic-gate audit_put_record(zlogp, uc, rval, "boot"); 6810Sstevel@tonic-gate if (rval != 0) { 6820Sstevel@tonic-gate bringup_failure_recovery = B_TRUE; 683766Scarlsonj (void) zone_halt(zlogp, B_FALSE); 684*1645Scomay eventstream_write(Z_EVT_ZONE_BOOTFAILED); 6850Sstevel@tonic-gate } 6860Sstevel@tonic-gate break; 6870Sstevel@tonic-gate case Z_HALT: 6880Sstevel@tonic-gate if (kernelcall) /* Invalid; can't happen */ 6890Sstevel@tonic-gate abort(); 6900Sstevel@tonic-gate /* 6910Sstevel@tonic-gate * We could have two clients racing to halt this 6920Sstevel@tonic-gate * zone; the second client loses, but his request 6930Sstevel@tonic-gate * doesn't fail, since the zone is now in the desired 6940Sstevel@tonic-gate * state. 6950Sstevel@tonic-gate */ 6960Sstevel@tonic-gate zerror(zlogp, B_FALSE, "zone is already halted"); 6970Sstevel@tonic-gate rval = 0; 6980Sstevel@tonic-gate break; 6990Sstevel@tonic-gate case Z_REBOOT: 7000Sstevel@tonic-gate if (kernelcall) /* Invalid; can't happen */ 7010Sstevel@tonic-gate abort(); 7020Sstevel@tonic-gate zerror(zlogp, B_FALSE, "%s operation is invalid " 703766Scarlsonj "for zones in state '%s'", z_cmd_name(cmd), 7040Sstevel@tonic-gate zone_state_str(zstate)); 7050Sstevel@tonic-gate rval = -1; 7060Sstevel@tonic-gate break; 7070Sstevel@tonic-gate case Z_NOTE_UNINSTALLING: 7080Sstevel@tonic-gate if (kernelcall) /* Invalid; can't happen */ 7090Sstevel@tonic-gate abort(); 7100Sstevel@tonic-gate /* 7110Sstevel@tonic-gate * Tell the console to print out a message about this. 7120Sstevel@tonic-gate * Once it does, we will be in_death_throes. 7130Sstevel@tonic-gate */ 7140Sstevel@tonic-gate eventstream_write(Z_EVT_ZONE_UNINSTALLING); 7150Sstevel@tonic-gate break; 716766Scarlsonj case Z_MOUNT: 717766Scarlsonj if (kernelcall) /* Invalid; can't happen */ 718766Scarlsonj abort(); 719766Scarlsonj rval = zone_ready(zlogp, B_TRUE); 720*1645Scomay if (rval == 0) { 721*1645Scomay eventstream_write(Z_EVT_ZONE_READIED); 722766Scarlsonj rval = zone_mount_early(zlogp, zone_id); 723*1645Scomay } 724*1645Scomay 725766Scarlsonj /* 726766Scarlsonj * Ordinarily, /dev/fd would be mounted inside the zone 727766Scarlsonj * by svc:/system/filesystem/usr:default, but since 728766Scarlsonj * we're not booting the zone, we need to do this 729766Scarlsonj * manually. 730766Scarlsonj */ 731766Scarlsonj if (rval == 0) 732766Scarlsonj rval = mount_early_fs(zlogp, zone_id, "fd", 733766Scarlsonj "/dev/fd", "fd"); 734766Scarlsonj break; 735766Scarlsonj case Z_UNMOUNT: 736766Scarlsonj if (kernelcall) /* Invalid; can't happen */ 737766Scarlsonj abort(); 738766Scarlsonj zerror(zlogp, B_FALSE, "zone is already unmounted"); 739766Scarlsonj rval = 0; 740766Scarlsonj break; 7410Sstevel@tonic-gate } 7420Sstevel@tonic-gate break; 7430Sstevel@tonic-gate 7440Sstevel@tonic-gate case ZONE_STATE_READY: 7450Sstevel@tonic-gate switch (cmd) { 7460Sstevel@tonic-gate case Z_READY: 7470Sstevel@tonic-gate /* 7480Sstevel@tonic-gate * We could have two clients racing to ready this 7490Sstevel@tonic-gate * zone; the second client loses, but his request 7500Sstevel@tonic-gate * doesn't fail, since the zone is now in the desired 7510Sstevel@tonic-gate * state. 7520Sstevel@tonic-gate */ 7530Sstevel@tonic-gate zerror(zlogp, B_FALSE, "zone is already ready"); 7540Sstevel@tonic-gate rval = 0; 7550Sstevel@tonic-gate break; 7560Sstevel@tonic-gate case Z_BOOT: 7570Sstevel@tonic-gate eventstream_write(Z_EVT_ZONE_BOOTING); 7580Sstevel@tonic-gate rval = zone_bootup(zlogp, zargp->bootbuf); 7590Sstevel@tonic-gate audit_put_record(zlogp, uc, rval, "boot"); 7600Sstevel@tonic-gate if (rval != 0) { 7610Sstevel@tonic-gate bringup_failure_recovery = B_TRUE; 762766Scarlsonj (void) zone_halt(zlogp, B_FALSE); 763*1645Scomay eventstream_write(Z_EVT_ZONE_BOOTFAILED); 7640Sstevel@tonic-gate } 7650Sstevel@tonic-gate break; 7660Sstevel@tonic-gate case Z_HALT: 7670Sstevel@tonic-gate if (kernelcall) /* Invalid; can't happen */ 7680Sstevel@tonic-gate abort(); 769766Scarlsonj if ((rval = zone_halt(zlogp, B_FALSE)) != 0) 7700Sstevel@tonic-gate break; 7710Sstevel@tonic-gate eventstream_write(Z_EVT_ZONE_HALTED); 7720Sstevel@tonic-gate break; 7730Sstevel@tonic-gate case Z_REBOOT: 774766Scarlsonj case Z_NOTE_UNINSTALLING: 775766Scarlsonj case Z_MOUNT: 776766Scarlsonj case Z_UNMOUNT: 7770Sstevel@tonic-gate if (kernelcall) /* Invalid; can't happen */ 7780Sstevel@tonic-gate abort(); 7790Sstevel@tonic-gate zerror(zlogp, B_FALSE, "%s operation is invalid " 780766Scarlsonj "for zones in state '%s'", z_cmd_name(cmd), 7810Sstevel@tonic-gate zone_state_str(zstate)); 7820Sstevel@tonic-gate rval = -1; 7830Sstevel@tonic-gate break; 784766Scarlsonj } 785766Scarlsonj break; 786766Scarlsonj 787766Scarlsonj case ZONE_STATE_MOUNTED: 788766Scarlsonj switch (cmd) { 789766Scarlsonj case Z_UNMOUNT: 7900Sstevel@tonic-gate if (kernelcall) /* Invalid; can't happen */ 7910Sstevel@tonic-gate abort(); 792766Scarlsonj rval = zone_halt(zlogp, B_TRUE); 793*1645Scomay if (rval == 0) { 794*1645Scomay eventstream_write(Z_EVT_ZONE_HALTED); 795766Scarlsonj (void) sema_post(&scratch_sem); 796*1645Scomay } 797766Scarlsonj break; 798766Scarlsonj default: 799766Scarlsonj if (kernelcall) /* Invalid; can't happen */ 800766Scarlsonj abort(); 801766Scarlsonj zerror(zlogp, B_FALSE, "%s operation is invalid " 802766Scarlsonj "for zones in state '%s'", z_cmd_name(cmd), 803766Scarlsonj zone_state_str(zstate)); 8040Sstevel@tonic-gate rval = -1; 8050Sstevel@tonic-gate break; 8060Sstevel@tonic-gate } 8070Sstevel@tonic-gate break; 8080Sstevel@tonic-gate 8090Sstevel@tonic-gate case ZONE_STATE_RUNNING: 8100Sstevel@tonic-gate case ZONE_STATE_SHUTTING_DOWN: 8110Sstevel@tonic-gate case ZONE_STATE_DOWN: 8120Sstevel@tonic-gate switch (cmd) { 8130Sstevel@tonic-gate case Z_READY: 814766Scarlsonj if ((rval = zone_halt(zlogp, B_FALSE)) != 0) 8150Sstevel@tonic-gate break; 816766Scarlsonj if ((rval = zone_ready(zlogp, B_FALSE)) == 0) 8170Sstevel@tonic-gate eventstream_write(Z_EVT_ZONE_READIED); 818*1645Scomay else 819*1645Scomay eventstream_write(Z_EVT_ZONE_HALTED); 8200Sstevel@tonic-gate break; 8210Sstevel@tonic-gate case Z_BOOT: 8220Sstevel@tonic-gate /* 8230Sstevel@tonic-gate * We could have two clients racing to boot this 8240Sstevel@tonic-gate * zone; the second client loses, but his request 8250Sstevel@tonic-gate * doesn't fail, since the zone is now in the desired 8260Sstevel@tonic-gate * state. 8270Sstevel@tonic-gate */ 8280Sstevel@tonic-gate zerror(zlogp, B_FALSE, "zone is already booted"); 8290Sstevel@tonic-gate rval = 0; 8300Sstevel@tonic-gate break; 8310Sstevel@tonic-gate case Z_HALT: 832766Scarlsonj if ((rval = zone_halt(zlogp, B_FALSE)) != 0) 8330Sstevel@tonic-gate break; 8340Sstevel@tonic-gate eventstream_write(Z_EVT_ZONE_HALTED); 8350Sstevel@tonic-gate break; 8360Sstevel@tonic-gate case Z_REBOOT: 8370Sstevel@tonic-gate eventstream_write(Z_EVT_ZONE_REBOOTING); 838*1645Scomay if ((rval = zone_halt(zlogp, B_FALSE)) != 0) { 839*1645Scomay eventstream_write(Z_EVT_ZONE_BOOTFAILED); 840*1645Scomay break; 841*1645Scomay } 842*1645Scomay if ((rval = zone_ready(zlogp, B_FALSE)) != 0) { 843*1645Scomay eventstream_write(Z_EVT_ZONE_BOOTFAILED); 8440Sstevel@tonic-gate break; 845*1645Scomay } 846*1645Scomay rval = zone_bootup(zlogp, ""); 847*1645Scomay audit_put_record(zlogp, uc, rval, "reboot"); 848*1645Scomay if (rval != 0) { 849*1645Scomay (void) zone_halt(zlogp, B_FALSE); 850*1645Scomay eventstream_write(Z_EVT_ZONE_BOOTFAILED); 8510Sstevel@tonic-gate } 8520Sstevel@tonic-gate break; 8530Sstevel@tonic-gate case Z_NOTE_UNINSTALLING: 854766Scarlsonj case Z_MOUNT: 855766Scarlsonj case Z_UNMOUNT: 856766Scarlsonj zerror(zlogp, B_FALSE, "%s operation is invalid " 857766Scarlsonj "for zones in state '%s'", z_cmd_name(cmd), 858766Scarlsonj zone_state_str(zstate)); 8590Sstevel@tonic-gate rval = -1; 8600Sstevel@tonic-gate break; 8610Sstevel@tonic-gate } 8620Sstevel@tonic-gate break; 8630Sstevel@tonic-gate default: 8640Sstevel@tonic-gate abort(); 8650Sstevel@tonic-gate } 8660Sstevel@tonic-gate 8670Sstevel@tonic-gate /* 8680Sstevel@tonic-gate * Because the state of the zone may have changed, we make sure 8690Sstevel@tonic-gate * to wake the console poller, which is in charge of initiating 8700Sstevel@tonic-gate * the shutdown procedure as necessary. 8710Sstevel@tonic-gate */ 8720Sstevel@tonic-gate eventstream_write(Z_EVT_NULL); 8730Sstevel@tonic-gate 8740Sstevel@tonic-gate out: 8750Sstevel@tonic-gate (void) mutex_unlock(&lock); 8760Sstevel@tonic-gate if (kernelcall) { 8770Sstevel@tonic-gate rvalp = NULL; 8780Sstevel@tonic-gate rlen = 0; 8790Sstevel@tonic-gate } else { 8800Sstevel@tonic-gate rvalp->rval = rval; 8810Sstevel@tonic-gate } 8820Sstevel@tonic-gate if (uc != NULL) 8830Sstevel@tonic-gate ucred_free(uc); 8840Sstevel@tonic-gate (void) door_return((char *)rvalp, rlen, NULL, 0); 8850Sstevel@tonic-gate thr_exit(NULL); 8860Sstevel@tonic-gate } 8870Sstevel@tonic-gate 8880Sstevel@tonic-gate static int 8890Sstevel@tonic-gate setup_door(zlog_t *zlogp) 8900Sstevel@tonic-gate { 8910Sstevel@tonic-gate if ((zone_door = door_create(server, NULL, 8920Sstevel@tonic-gate DOOR_UNREF | DOOR_REFUSE_DESC | DOOR_NO_CANCEL)) < 0) { 8930Sstevel@tonic-gate zerror(zlogp, B_TRUE, "%s failed", "door_create"); 8940Sstevel@tonic-gate return (-1); 8950Sstevel@tonic-gate } 8960Sstevel@tonic-gate (void) fdetach(zone_door_path); 8970Sstevel@tonic-gate 8980Sstevel@tonic-gate if (fattach(zone_door, zone_door_path) != 0) { 8990Sstevel@tonic-gate zerror(zlogp, B_TRUE, "fattach to %s failed", zone_door_path); 9000Sstevel@tonic-gate (void) door_revoke(zone_door); 9010Sstevel@tonic-gate (void) fdetach(zone_door_path); 9020Sstevel@tonic-gate zone_door = -1; 9030Sstevel@tonic-gate return (-1); 9040Sstevel@tonic-gate } 9050Sstevel@tonic-gate return (0); 9060Sstevel@tonic-gate } 9070Sstevel@tonic-gate 9080Sstevel@tonic-gate /* 9090Sstevel@tonic-gate * zoneadm(1m) will start zoneadmd if it thinks it isn't running; this 9100Sstevel@tonic-gate * is where zoneadmd itself will check to see that another instance of 9110Sstevel@tonic-gate * zoneadmd isn't already controlling this zone. 9120Sstevel@tonic-gate * 9130Sstevel@tonic-gate * The idea here is that we want to open the path to which we will 9140Sstevel@tonic-gate * attach our door, lock it, and then make sure that no-one has beat us 9150Sstevel@tonic-gate * to fattach(3c)ing onto it. 9160Sstevel@tonic-gate * 9170Sstevel@tonic-gate * fattach(3c) is really a mount, so there are actually two possible 9180Sstevel@tonic-gate * vnodes we could be dealing with. Our strategy is as follows: 9190Sstevel@tonic-gate * 9200Sstevel@tonic-gate * - If the file we opened is a regular file (common case): 9210Sstevel@tonic-gate * There is no fattach(3c)ed door, so we have a chance of becoming 9220Sstevel@tonic-gate * the managing zoneadmd. We attempt to lock the file: if it is 9230Sstevel@tonic-gate * already locked, that means someone else raced us here, so we 9240Sstevel@tonic-gate * lose and give up. zoneadm(1m) will try to contact the zoneadmd 9250Sstevel@tonic-gate * that beat us to it. 9260Sstevel@tonic-gate * 9270Sstevel@tonic-gate * - If the file we opened is a namefs file: 9280Sstevel@tonic-gate * This means there is already an established door fattach(3c)'ed 9290Sstevel@tonic-gate * to the rendezvous path. We've lost the race, so we give up. 9300Sstevel@tonic-gate * Note that in this case we also try to grab the file lock, and 9310Sstevel@tonic-gate * will succeed in acquiring it since the vnode locked by the 9320Sstevel@tonic-gate * "winning" zoneadmd was a regular one, and the one we locked was 9330Sstevel@tonic-gate * the fattach(3c)'ed door node. At any rate, no harm is done, and 9340Sstevel@tonic-gate * we just return to zoneadm(1m) which knows to retry. 9350Sstevel@tonic-gate */ 9360Sstevel@tonic-gate static int 9370Sstevel@tonic-gate make_daemon_exclusive(zlog_t *zlogp) 9380Sstevel@tonic-gate { 9390Sstevel@tonic-gate int doorfd = -1; 9400Sstevel@tonic-gate int err, ret = -1; 9410Sstevel@tonic-gate struct stat st; 9420Sstevel@tonic-gate struct flock flock; 9430Sstevel@tonic-gate zone_state_t zstate; 9440Sstevel@tonic-gate 9450Sstevel@tonic-gate top: 9460Sstevel@tonic-gate if ((err = zone_get_state(zone_name, &zstate)) != Z_OK) { 9470Sstevel@tonic-gate zerror(zlogp, B_FALSE, "failed to get zone state: %s\n", 9480Sstevel@tonic-gate zonecfg_strerror(err)); 9490Sstevel@tonic-gate goto out; 9500Sstevel@tonic-gate } 9510Sstevel@tonic-gate if ((doorfd = open(zone_door_path, O_CREAT|O_RDWR, 9520Sstevel@tonic-gate S_IREAD|S_IWRITE)) < 0) { 9530Sstevel@tonic-gate zerror(zlogp, B_TRUE, "failed to open %s", zone_door_path); 9540Sstevel@tonic-gate goto out; 9550Sstevel@tonic-gate } 9560Sstevel@tonic-gate if (fstat(doorfd, &st) < 0) { 9570Sstevel@tonic-gate zerror(zlogp, B_TRUE, "failed to stat %s", zone_door_path); 9580Sstevel@tonic-gate goto out; 9590Sstevel@tonic-gate } 9600Sstevel@tonic-gate /* 9610Sstevel@tonic-gate * Lock the file to synchronize with other zoneadmd 9620Sstevel@tonic-gate */ 9630Sstevel@tonic-gate flock.l_type = F_WRLCK; 9640Sstevel@tonic-gate flock.l_whence = SEEK_SET; 9650Sstevel@tonic-gate flock.l_start = (off_t)0; 9660Sstevel@tonic-gate flock.l_len = (off_t)0; 9670Sstevel@tonic-gate if (fcntl(doorfd, F_SETLK, &flock) < 0) { 9680Sstevel@tonic-gate /* 9690Sstevel@tonic-gate * Someone else raced us here and grabbed the lock file 9700Sstevel@tonic-gate * first. A warning here is inappropriate since nothing 9710Sstevel@tonic-gate * went wrong. 9720Sstevel@tonic-gate */ 9730Sstevel@tonic-gate goto out; 9740Sstevel@tonic-gate } 9750Sstevel@tonic-gate 9760Sstevel@tonic-gate if (strcmp(st.st_fstype, "namefs") == 0) { 9770Sstevel@tonic-gate struct door_info info; 9780Sstevel@tonic-gate 9790Sstevel@tonic-gate /* 9800Sstevel@tonic-gate * There is already something fattach()'ed to this file. 9810Sstevel@tonic-gate * Lets see what the door is up to. 9820Sstevel@tonic-gate */ 9830Sstevel@tonic-gate if (door_info(doorfd, &info) == 0 && info.di_target != -1) { 9840Sstevel@tonic-gate /* 9850Sstevel@tonic-gate * Another zoneadmd process seems to be in 9860Sstevel@tonic-gate * control of the situation and we don't need to 9870Sstevel@tonic-gate * be here. A warning here is inappropriate 9880Sstevel@tonic-gate * since nothing went wrong. 9890Sstevel@tonic-gate * 9900Sstevel@tonic-gate * If the door has been revoked, the zoneadmd 9910Sstevel@tonic-gate * process currently managing the zone is going 9920Sstevel@tonic-gate * away. We'll return control to zoneadm(1m) 9930Sstevel@tonic-gate * which will try again (by which time zoneadmd 9940Sstevel@tonic-gate * will hopefully have exited). 9950Sstevel@tonic-gate */ 9960Sstevel@tonic-gate goto out; 9970Sstevel@tonic-gate } 9980Sstevel@tonic-gate 9990Sstevel@tonic-gate /* 10000Sstevel@tonic-gate * If we got this far, there's a fattach(3c)'ed door 10010Sstevel@tonic-gate * that belongs to a process that has exited, which can 10020Sstevel@tonic-gate * happen if the previous zoneadmd died unexpectedly. 10030Sstevel@tonic-gate * 10040Sstevel@tonic-gate * Let user know that something is amiss, but that we can 10050Sstevel@tonic-gate * recover; if the zone is in the installed state, then don't 10060Sstevel@tonic-gate * message, since having a running zoneadmd isn't really 10070Sstevel@tonic-gate * expected/needed. We want to keep occurences of this message 10080Sstevel@tonic-gate * limited to times when zoneadmd is picking back up from a 10090Sstevel@tonic-gate * zoneadmd that died while the zone was in some non-trivial 10100Sstevel@tonic-gate * state. 10110Sstevel@tonic-gate */ 10120Sstevel@tonic-gate if (zstate > ZONE_STATE_INSTALLED) { 10130Sstevel@tonic-gate zerror(zlogp, B_FALSE, 10140Sstevel@tonic-gate "zone '%s': WARNING: zone is in state '%s', but " 10150Sstevel@tonic-gate "zoneadmd does not appear to be available; " 10160Sstevel@tonic-gate "restarted zoneadmd to recover.", 10170Sstevel@tonic-gate zone_name, zone_state_str(zstate)); 10180Sstevel@tonic-gate } 10190Sstevel@tonic-gate 10200Sstevel@tonic-gate (void) fdetach(zone_door_path); 10210Sstevel@tonic-gate (void) close(doorfd); 10220Sstevel@tonic-gate goto top; 10230Sstevel@tonic-gate } 10240Sstevel@tonic-gate ret = 0; 10250Sstevel@tonic-gate out: 10260Sstevel@tonic-gate (void) close(doorfd); 10270Sstevel@tonic-gate return (ret); 10280Sstevel@tonic-gate } 10290Sstevel@tonic-gate 10300Sstevel@tonic-gate int 10310Sstevel@tonic-gate main(int argc, char *argv[]) 10320Sstevel@tonic-gate { 10330Sstevel@tonic-gate int opt; 10340Sstevel@tonic-gate zoneid_t zid; 10350Sstevel@tonic-gate priv_set_t *privset; 10360Sstevel@tonic-gate zone_state_t zstate; 10370Sstevel@tonic-gate char parents_locale[MAXPATHLEN]; 10380Sstevel@tonic-gate int err; 10390Sstevel@tonic-gate 10400Sstevel@tonic-gate pid_t pid; 10410Sstevel@tonic-gate sigset_t blockset; 10420Sstevel@tonic-gate sigset_t block_cld; 10430Sstevel@tonic-gate 10440Sstevel@tonic-gate struct { 10450Sstevel@tonic-gate sema_t sem; 10460Sstevel@tonic-gate int status; 10470Sstevel@tonic-gate zlog_t log; 10480Sstevel@tonic-gate } *shstate; 10490Sstevel@tonic-gate size_t shstatelen = getpagesize(); 10500Sstevel@tonic-gate 10510Sstevel@tonic-gate zlog_t errlog; 10520Sstevel@tonic-gate zlog_t *zlogp; 10530Sstevel@tonic-gate 10540Sstevel@tonic-gate progname = get_execbasename(argv[0]); 10550Sstevel@tonic-gate 10560Sstevel@tonic-gate /* 10570Sstevel@tonic-gate * Make sure stderr is unbuffered 10580Sstevel@tonic-gate */ 10590Sstevel@tonic-gate (void) setbuffer(stderr, NULL, 0); 10600Sstevel@tonic-gate 10610Sstevel@tonic-gate /* 10620Sstevel@tonic-gate * Get out of the way of mounted filesystems, since we will daemonize 10630Sstevel@tonic-gate * soon. 10640Sstevel@tonic-gate */ 10650Sstevel@tonic-gate (void) chdir("/"); 10660Sstevel@tonic-gate 10670Sstevel@tonic-gate /* 10680Sstevel@tonic-gate * Use the default system umask per PSARC 1998/110 rather than 10690Sstevel@tonic-gate * anything that may have been set by the caller. 10700Sstevel@tonic-gate */ 10710Sstevel@tonic-gate (void) umask(CMASK); 10720Sstevel@tonic-gate 10730Sstevel@tonic-gate /* 10740Sstevel@tonic-gate * Initially we want to use our parent's locale. 10750Sstevel@tonic-gate */ 10760Sstevel@tonic-gate (void) setlocale(LC_ALL, ""); 10770Sstevel@tonic-gate (void) textdomain(TEXT_DOMAIN); 10780Sstevel@tonic-gate (void) strlcpy(parents_locale, setlocale(LC_MESSAGES, NULL), 10790Sstevel@tonic-gate sizeof (parents_locale)); 10800Sstevel@tonic-gate 10810Sstevel@tonic-gate /* 10820Sstevel@tonic-gate * This zlog_t is used for writing to stderr 10830Sstevel@tonic-gate */ 10840Sstevel@tonic-gate errlog.logfile = stderr; 10850Sstevel@tonic-gate errlog.buflen = errlog.loglen = 0; 10860Sstevel@tonic-gate errlog.buf = errlog.log = NULL; 10870Sstevel@tonic-gate errlog.locale = parents_locale; 10880Sstevel@tonic-gate 10890Sstevel@tonic-gate /* 10900Sstevel@tonic-gate * We start off writing to stderr until we're ready to daemonize. 10910Sstevel@tonic-gate */ 10920Sstevel@tonic-gate zlogp = &errlog; 10930Sstevel@tonic-gate 10940Sstevel@tonic-gate /* 10950Sstevel@tonic-gate * Process options. 10960Sstevel@tonic-gate */ 1097766Scarlsonj while ((opt = getopt(argc, argv, "R:z:")) != EOF) { 10980Sstevel@tonic-gate switch (opt) { 1099766Scarlsonj case 'R': 1100766Scarlsonj zonecfg_set_root(optarg); 1101766Scarlsonj break; 11020Sstevel@tonic-gate case 'z': 11030Sstevel@tonic-gate zone_name = optarg; 11040Sstevel@tonic-gate break; 11050Sstevel@tonic-gate default: 11060Sstevel@tonic-gate usage(); 11070Sstevel@tonic-gate } 11080Sstevel@tonic-gate } 11090Sstevel@tonic-gate 11100Sstevel@tonic-gate if (zone_name == NULL) 11110Sstevel@tonic-gate usage(); 11120Sstevel@tonic-gate 11130Sstevel@tonic-gate /* 11140Sstevel@tonic-gate * Because usage() prints directly to stderr, it has gettext() 11150Sstevel@tonic-gate * wrapping, which depends on the locale. But since zerror() calls 11160Sstevel@tonic-gate * localize() which tweaks the locale, it is not safe to call zerror() 11170Sstevel@tonic-gate * until after the last call to usage(). Fortunately, the last call 11180Sstevel@tonic-gate * to usage() is just above and the first call to zerror() is just 11190Sstevel@tonic-gate * below. Don't mess this up. 11200Sstevel@tonic-gate */ 11210Sstevel@tonic-gate if (strcmp(zone_name, GLOBAL_ZONENAME) == 0) { 11220Sstevel@tonic-gate zerror(zlogp, B_FALSE, "cannot manage the %s zone", 11230Sstevel@tonic-gate GLOBAL_ZONENAME); 11240Sstevel@tonic-gate return (1); 11250Sstevel@tonic-gate } 11260Sstevel@tonic-gate 11270Sstevel@tonic-gate if (zone_get_id(zone_name, &zid) != 0) { 11280Sstevel@tonic-gate zerror(zlogp, B_FALSE, "could not manage %s: %s\n", zone_name, 11290Sstevel@tonic-gate zonecfg_strerror(Z_NO_ZONE)); 11300Sstevel@tonic-gate return (1); 11310Sstevel@tonic-gate } 11320Sstevel@tonic-gate 11330Sstevel@tonic-gate if ((err = zone_get_state(zone_name, &zstate)) != Z_OK) { 11340Sstevel@tonic-gate zerror(zlogp, B_FALSE, "failed to get zone state: %s\n", 11350Sstevel@tonic-gate zonecfg_strerror(err)); 11360Sstevel@tonic-gate return (1); 11370Sstevel@tonic-gate } 11380Sstevel@tonic-gate if (zstate < ZONE_STATE_INSTALLED) { 11390Sstevel@tonic-gate zerror(zlogp, B_FALSE, 11400Sstevel@tonic-gate "cannot manage a zone which is in state '%s'", 11410Sstevel@tonic-gate zone_state_str(zstate)); 11420Sstevel@tonic-gate return (1); 11430Sstevel@tonic-gate } 11440Sstevel@tonic-gate 11450Sstevel@tonic-gate /* 11460Sstevel@tonic-gate * Check that we have all privileges. It would be nice to pare 11470Sstevel@tonic-gate * this down, but this is at least a first cut. 11480Sstevel@tonic-gate */ 11490Sstevel@tonic-gate if ((privset = priv_allocset()) == NULL) { 11500Sstevel@tonic-gate zerror(zlogp, B_TRUE, "%s failed", "priv_allocset"); 11510Sstevel@tonic-gate return (1); 11520Sstevel@tonic-gate } 11530Sstevel@tonic-gate 11540Sstevel@tonic-gate if (getppriv(PRIV_EFFECTIVE, privset) != 0) { 11550Sstevel@tonic-gate zerror(zlogp, B_TRUE, "%s failed", "getppriv"); 11560Sstevel@tonic-gate priv_freeset(privset); 11570Sstevel@tonic-gate return (1); 11580Sstevel@tonic-gate } 11590Sstevel@tonic-gate 11600Sstevel@tonic-gate if (priv_isfullset(privset) == B_FALSE) { 11610Sstevel@tonic-gate zerror(zlogp, B_FALSE, "You lack sufficient privilege to " 11620Sstevel@tonic-gate "run this command (all privs required)\n"); 11630Sstevel@tonic-gate priv_freeset(privset); 11640Sstevel@tonic-gate return (1); 11650Sstevel@tonic-gate } 11660Sstevel@tonic-gate priv_freeset(privset); 11670Sstevel@tonic-gate 11680Sstevel@tonic-gate if (mkzonedir(zlogp) != 0) 11690Sstevel@tonic-gate return (1); 11700Sstevel@tonic-gate 11710Sstevel@tonic-gate /* 11720Sstevel@tonic-gate * Pre-fork: setup shared state 11730Sstevel@tonic-gate */ 11740Sstevel@tonic-gate if ((shstate = (void *)mmap(NULL, shstatelen, 11750Sstevel@tonic-gate PROT_READ|PROT_WRITE, MAP_SHARED|MAP_ANON, -1, (off_t)0)) == 11760Sstevel@tonic-gate MAP_FAILED) { 11770Sstevel@tonic-gate zerror(zlogp, B_TRUE, "%s failed", "mmap"); 11780Sstevel@tonic-gate return (1); 11790Sstevel@tonic-gate } 11800Sstevel@tonic-gate if (sema_init(&shstate->sem, 0, USYNC_PROCESS, NULL) != 0) { 11810Sstevel@tonic-gate zerror(zlogp, B_TRUE, "%s failed", "sema_init()"); 11820Sstevel@tonic-gate (void) munmap((char *)shstate, shstatelen); 11830Sstevel@tonic-gate return (1); 11840Sstevel@tonic-gate } 11850Sstevel@tonic-gate shstate->log.logfile = NULL; 11860Sstevel@tonic-gate shstate->log.buflen = shstatelen - sizeof (*shstate); 11870Sstevel@tonic-gate shstate->log.loglen = shstate->log.buflen; 11880Sstevel@tonic-gate shstate->log.buf = (char *)shstate + sizeof (*shstate); 11890Sstevel@tonic-gate shstate->log.log = shstate->log.buf; 11900Sstevel@tonic-gate shstate->log.locale = parents_locale; 11910Sstevel@tonic-gate shstate->status = -1; 11920Sstevel@tonic-gate 11930Sstevel@tonic-gate /* 11940Sstevel@tonic-gate * We need a SIGCHLD handler so the sema_wait() below will wake 11950Sstevel@tonic-gate * up if the child dies without doing a sema_post(). 11960Sstevel@tonic-gate */ 11970Sstevel@tonic-gate (void) sigset(SIGCHLD, sigchld); 11980Sstevel@tonic-gate /* 11990Sstevel@tonic-gate * We must mask SIGCHLD until after we've coped with the fork 12000Sstevel@tonic-gate * sufficiently to deal with it; otherwise we can race and 12010Sstevel@tonic-gate * receive the signal before pid has been initialized 12020Sstevel@tonic-gate * (yes, this really happens). 12030Sstevel@tonic-gate */ 12040Sstevel@tonic-gate (void) sigemptyset(&block_cld); 12050Sstevel@tonic-gate (void) sigaddset(&block_cld, SIGCHLD); 12060Sstevel@tonic-gate (void) sigprocmask(SIG_BLOCK, &block_cld, NULL); 12070Sstevel@tonic-gate 12080Sstevel@tonic-gate /* 12090Sstevel@tonic-gate * Do not let another thread localize a message while we are forking. 12100Sstevel@tonic-gate */ 12110Sstevel@tonic-gate (void) mutex_lock(&msglock); 12120Sstevel@tonic-gate pid = fork(); 12130Sstevel@tonic-gate (void) mutex_unlock(&msglock); 12140Sstevel@tonic-gate if (pid == -1) { 12150Sstevel@tonic-gate zerror(zlogp, B_TRUE, "could not fork"); 12160Sstevel@tonic-gate return (1); 12170Sstevel@tonic-gate 12180Sstevel@tonic-gate } else if (pid > 0) { /* parent */ 12190Sstevel@tonic-gate (void) sigprocmask(SIG_UNBLOCK, &block_cld, NULL); 12200Sstevel@tonic-gate /* 12210Sstevel@tonic-gate * This marks a window of vulnerability in which we receive 12220Sstevel@tonic-gate * the SIGCLD before falling into sema_wait (normally we would 12230Sstevel@tonic-gate * get woken up from sema_wait with EINTR upon receipt of 12240Sstevel@tonic-gate * SIGCLD). So we may need to use some other scheme like 12250Sstevel@tonic-gate * sema_posting in the sigcld handler. 12260Sstevel@tonic-gate * blech 12270Sstevel@tonic-gate */ 12280Sstevel@tonic-gate (void) sema_wait(&shstate->sem); 12290Sstevel@tonic-gate (void) sema_destroy(&shstate->sem); 12300Sstevel@tonic-gate if (shstate->status != 0) 12310Sstevel@tonic-gate (void) waitpid(pid, NULL, WNOHANG); 12320Sstevel@tonic-gate /* 12330Sstevel@tonic-gate * It's ok if we die with SIGPIPE. It's not like we could have 12340Sstevel@tonic-gate * done anything about it. 12350Sstevel@tonic-gate */ 12360Sstevel@tonic-gate (void) fprintf(stderr, "%s", shstate->log.buf); 12370Sstevel@tonic-gate _exit(shstate->status == 0 ? 0 : 1); 12380Sstevel@tonic-gate } 12390Sstevel@tonic-gate 12400Sstevel@tonic-gate /* 12410Sstevel@tonic-gate * The child charges on. 12420Sstevel@tonic-gate */ 12430Sstevel@tonic-gate (void) sigset(SIGCHLD, SIG_DFL); 12440Sstevel@tonic-gate (void) sigprocmask(SIG_UNBLOCK, &block_cld, NULL); 12450Sstevel@tonic-gate 12460Sstevel@tonic-gate /* 12470Sstevel@tonic-gate * SIGPIPE can be delivered if we write to a socket for which the 12480Sstevel@tonic-gate * peer endpoint is gone. That can lead to too-early termination 12490Sstevel@tonic-gate * of zoneadmd, and that's not good eats. 12500Sstevel@tonic-gate */ 12510Sstevel@tonic-gate (void) sigset(SIGPIPE, SIG_IGN); 12520Sstevel@tonic-gate /* 12530Sstevel@tonic-gate * Stop using stderr 12540Sstevel@tonic-gate */ 12550Sstevel@tonic-gate zlogp = &shstate->log; 12560Sstevel@tonic-gate 12570Sstevel@tonic-gate /* 12580Sstevel@tonic-gate * We don't need stdout/stderr from now on. 12590Sstevel@tonic-gate */ 12600Sstevel@tonic-gate closefrom(0); 12610Sstevel@tonic-gate 12620Sstevel@tonic-gate /* 12630Sstevel@tonic-gate * Initialize the syslog zlog_t. This needs to be done after 12640Sstevel@tonic-gate * the call to closefrom(). 12650Sstevel@tonic-gate */ 12660Sstevel@tonic-gate logsys.buf = logsys.log = NULL; 12670Sstevel@tonic-gate logsys.buflen = logsys.loglen = 0; 12680Sstevel@tonic-gate logsys.logfile = NULL; 12690Sstevel@tonic-gate logsys.locale = DEFAULT_LOCALE; 12700Sstevel@tonic-gate 12710Sstevel@tonic-gate openlog("zoneadmd", LOG_PID, LOG_DAEMON); 12720Sstevel@tonic-gate 12730Sstevel@tonic-gate /* 12740Sstevel@tonic-gate * The eventstream is used to publish state changes in the zone 12750Sstevel@tonic-gate * from the door threads to the console I/O poller. 12760Sstevel@tonic-gate */ 12770Sstevel@tonic-gate if (eventstream_init() == -1) { 12780Sstevel@tonic-gate zerror(zlogp, B_TRUE, "unable to create eventstream"); 12790Sstevel@tonic-gate goto child_out; 12800Sstevel@tonic-gate } 12810Sstevel@tonic-gate 12820Sstevel@tonic-gate (void) snprintf(zone_door_path, sizeof (zone_door_path), 1283766Scarlsonj "%s" ZONE_DOOR_PATH, zonecfg_get_root(), zone_name); 12840Sstevel@tonic-gate 12850Sstevel@tonic-gate /* 12860Sstevel@tonic-gate * See if another zoneadmd is running for this zone. If not, then we 12870Sstevel@tonic-gate * can now modify system state. 12880Sstevel@tonic-gate */ 12890Sstevel@tonic-gate if (make_daemon_exclusive(zlogp) == -1) 12900Sstevel@tonic-gate goto child_out; 12910Sstevel@tonic-gate 12920Sstevel@tonic-gate 12930Sstevel@tonic-gate /* 12940Sstevel@tonic-gate * Create/join a new session; we need to be careful of what we do with 12950Sstevel@tonic-gate * the console from now on so we don't end up being the session leader 12960Sstevel@tonic-gate * for the terminal we're going to be handing out. 12970Sstevel@tonic-gate */ 12980Sstevel@tonic-gate (void) setsid(); 12990Sstevel@tonic-gate 13000Sstevel@tonic-gate /* 13010Sstevel@tonic-gate * This thread shouldn't be receiving any signals; in particular, 13020Sstevel@tonic-gate * SIGCHLD should be received by the thread doing the fork(). 13030Sstevel@tonic-gate */ 13040Sstevel@tonic-gate (void) sigfillset(&blockset); 13050Sstevel@tonic-gate (void) thr_sigsetmask(SIG_BLOCK, &blockset, NULL); 13060Sstevel@tonic-gate 13070Sstevel@tonic-gate /* 13080Sstevel@tonic-gate * Setup the console device and get ready to serve the console; 13090Sstevel@tonic-gate * once this has completed, we're ready to let console clients 13100Sstevel@tonic-gate * make an attempt to connect (they will block until 13110Sstevel@tonic-gate * serve_console_sock() below gets called, and any pending 13120Sstevel@tonic-gate * connection is accept()ed). 13130Sstevel@tonic-gate */ 1314766Scarlsonj if (!zonecfg_in_alt_root() && init_console(zlogp) == -1) 13150Sstevel@tonic-gate goto child_out; 13160Sstevel@tonic-gate 13170Sstevel@tonic-gate /* 13180Sstevel@tonic-gate * Take the lock now, so that when the door server gets going, we 13190Sstevel@tonic-gate * are guaranteed that it won't take a request until we are sure 13200Sstevel@tonic-gate * that everything is completely set up. See the child_out: label 13210Sstevel@tonic-gate * below to see why this matters. 13220Sstevel@tonic-gate */ 13230Sstevel@tonic-gate (void) mutex_lock(&lock); 13240Sstevel@tonic-gate 1325766Scarlsonj /* Init semaphore for scratch zones. */ 1326766Scarlsonj if (sema_init(&scratch_sem, 0, USYNC_THREAD, NULL) == -1) { 1327766Scarlsonj zerror(zlogp, B_TRUE, 1328766Scarlsonj "failed to initialize semaphore for scratch zone"); 1329766Scarlsonj goto child_out; 1330766Scarlsonj } 1331766Scarlsonj 13320Sstevel@tonic-gate /* 13330Sstevel@tonic-gate * Note: door setup must occur *after* the console is setup. 13340Sstevel@tonic-gate * This is so that as zlogin tests the door to see if zoneadmd 13350Sstevel@tonic-gate * is ready yet, we know that the console will get serviced 13360Sstevel@tonic-gate * once door_info() indicates that the door is "up". 13370Sstevel@tonic-gate */ 13380Sstevel@tonic-gate if (setup_door(zlogp) == -1) 13390Sstevel@tonic-gate goto child_out; 13400Sstevel@tonic-gate 13410Sstevel@tonic-gate /* 13420Sstevel@tonic-gate * Things seem OK so far; tell the parent process that we're done 13430Sstevel@tonic-gate * with setup tasks. This will cause the parent to exit, signalling 13440Sstevel@tonic-gate * to zoneadm, zlogin, or whatever forked it that we are ready to 13450Sstevel@tonic-gate * service requests. 13460Sstevel@tonic-gate */ 13470Sstevel@tonic-gate shstate->status = 0; 13480Sstevel@tonic-gate (void) sema_post(&shstate->sem); 13490Sstevel@tonic-gate (void) munmap((char *)shstate, shstatelen); 13500Sstevel@tonic-gate shstate = NULL; 13510Sstevel@tonic-gate 13520Sstevel@tonic-gate (void) mutex_unlock(&lock); 13530Sstevel@tonic-gate 13540Sstevel@tonic-gate /* 13550Sstevel@tonic-gate * zlogp is now invalid, so reset it to the syslog logger. 13560Sstevel@tonic-gate */ 13570Sstevel@tonic-gate zlogp = &logsys; 13580Sstevel@tonic-gate 13590Sstevel@tonic-gate /* 13600Sstevel@tonic-gate * Now that we are free of any parents, switch to the default locale. 13610Sstevel@tonic-gate */ 13620Sstevel@tonic-gate (void) setlocale(LC_ALL, DEFAULT_LOCALE); 13630Sstevel@tonic-gate 13640Sstevel@tonic-gate /* 13650Sstevel@tonic-gate * At this point the setup portion of main() is basically done, so 13660Sstevel@tonic-gate * we reuse this thread to manage the zone console. When 13670Sstevel@tonic-gate * serve_console() has returned, we are past the point of no return 13680Sstevel@tonic-gate * in the life of this zoneadmd. 13690Sstevel@tonic-gate */ 1370766Scarlsonj if (zonecfg_in_alt_root()) { 1371766Scarlsonj /* 1372766Scarlsonj * This is just awful, but mounted scratch zones don't (and 1373766Scarlsonj * can't) have consoles. We just wait for unmount instead. 1374766Scarlsonj */ 1375766Scarlsonj while (sema_wait(&scratch_sem) == EINTR) 1376766Scarlsonj ; 1377766Scarlsonj } else { 1378766Scarlsonj serve_console(zlogp); 1379766Scarlsonj assert(in_death_throes); 1380766Scarlsonj } 13810Sstevel@tonic-gate 13820Sstevel@tonic-gate /* 13830Sstevel@tonic-gate * This is the next-to-last part of the exit interlock. Upon calling 13840Sstevel@tonic-gate * fdetach(), the door will go unreferenced; once any 13850Sstevel@tonic-gate * outstanding requests (like the door thread doing Z_HALT) are 13860Sstevel@tonic-gate * done, the door will get an UNREF notification; when it handles 13870Sstevel@tonic-gate * the UNREF, the door server will cause the exit. 13880Sstevel@tonic-gate */ 13890Sstevel@tonic-gate assert(!MUTEX_HELD(&lock)); 13900Sstevel@tonic-gate (void) fdetach(zone_door_path); 13910Sstevel@tonic-gate for (;;) 13920Sstevel@tonic-gate (void) pause(); 13930Sstevel@tonic-gate 13940Sstevel@tonic-gate child_out: 13950Sstevel@tonic-gate assert(pid == 0); 13960Sstevel@tonic-gate if (shstate != NULL) { 13970Sstevel@tonic-gate shstate->status = -1; 13980Sstevel@tonic-gate (void) sema_post(&shstate->sem); 13990Sstevel@tonic-gate (void) munmap((char *)shstate, shstatelen); 14000Sstevel@tonic-gate } 14010Sstevel@tonic-gate 14020Sstevel@tonic-gate /* 14030Sstevel@tonic-gate * This might trigger an unref notification, but if so, 14040Sstevel@tonic-gate * we are still holding the lock, so our call to exit will 14050Sstevel@tonic-gate * ultimately win the race and will publish the right exit 14060Sstevel@tonic-gate * code. 14070Sstevel@tonic-gate */ 14080Sstevel@tonic-gate if (zone_door != -1) { 14090Sstevel@tonic-gate assert(MUTEX_HELD(&lock)); 14100Sstevel@tonic-gate (void) door_revoke(zone_door); 14110Sstevel@tonic-gate (void) fdetach(zone_door_path); 14120Sstevel@tonic-gate } 14130Sstevel@tonic-gate return (1); /* return from main() forcibly exits an MT process */ 14140Sstevel@tonic-gate } 1415