17c478bd9Sstevel@tonic-gate /* 27c478bd9Sstevel@tonic-gate * CDDL HEADER START 37c478bd9Sstevel@tonic-gate * 47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5ffbafc53Scomay * Common Development and Distribution License (the "License"). 6ffbafc53Scomay * You may not use this file except in compliance with the License. 77c478bd9Sstevel@tonic-gate * 87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 117c478bd9Sstevel@tonic-gate * and limitations under the License. 127c478bd9Sstevel@tonic-gate * 137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 187c478bd9Sstevel@tonic-gate * 197c478bd9Sstevel@tonic-gate * CDDL HEADER END 207c478bd9Sstevel@tonic-gate */ 21ffbafc53Scomay 227c478bd9Sstevel@tonic-gate /* 23efd4c9b6SSteve Lawrence * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved. 243c7284bdSAlexander Eremin * Copyright 2014 Nexenta Systems, Inc. All rights reserved. 257c478bd9Sstevel@tonic-gate */ 267c478bd9Sstevel@tonic-gate 277c478bd9Sstevel@tonic-gate /* 287c478bd9Sstevel@tonic-gate * zoneadmd manages zones; one zoneadmd process is launched for each 297c478bd9Sstevel@tonic-gate * non-global zone on the system. This daemon juggles four jobs: 307c478bd9Sstevel@tonic-gate * 317c478bd9Sstevel@tonic-gate * - Implement setup and teardown of the zone "virtual platform": mount and 327c478bd9Sstevel@tonic-gate * unmount filesystems; create and destroy network interfaces; communicate 337c478bd9Sstevel@tonic-gate * with devfsadmd to lay out devices for the zone; instantiate the zone 347c478bd9Sstevel@tonic-gate * console device; configure process runtime attributes such as resource 357c478bd9Sstevel@tonic-gate * controls, pool bindings, fine-grained privileges. 367c478bd9Sstevel@tonic-gate * 377c478bd9Sstevel@tonic-gate * - Launch the zone's init(1M) process. 387c478bd9Sstevel@tonic-gate * 397c478bd9Sstevel@tonic-gate * - Implement a door server; clients (like zoneadm) connect to the door 407c478bd9Sstevel@tonic-gate * server and request zone state changes. The kernel is also a client of 417c478bd9Sstevel@tonic-gate * this door server. A request to halt or reboot the zone which originates 427c478bd9Sstevel@tonic-gate * *inside* the zone results in a door upcall from the kernel into zoneadmd. 437c478bd9Sstevel@tonic-gate * 447c478bd9Sstevel@tonic-gate * One minor problem is that messages emitted by zoneadmd need to be passed 457c478bd9Sstevel@tonic-gate * back to the zoneadm process making the request. These messages need to 467c478bd9Sstevel@tonic-gate * be rendered in the client's locale; so, this is passed in as part of the 477c478bd9Sstevel@tonic-gate * request. The exception is the kernel upcall to zoneadmd, in which case 487c478bd9Sstevel@tonic-gate * messages are syslog'd. 497c478bd9Sstevel@tonic-gate * 507c478bd9Sstevel@tonic-gate * To make all of this work, the Makefile adds -a to xgettext to extract *all* 517c478bd9Sstevel@tonic-gate * strings, and an exclusion file (zoneadmd.xcl) is used to exclude those 527c478bd9Sstevel@tonic-gate * strings which do not need to be translated. 537c478bd9Sstevel@tonic-gate * 547c478bd9Sstevel@tonic-gate * - Act as a console server for zlogin -C processes; see comments in zcons.c 557c478bd9Sstevel@tonic-gate * for more information about the zone console architecture. 567c478bd9Sstevel@tonic-gate * 577c478bd9Sstevel@tonic-gate * DESIGN NOTES 587c478bd9Sstevel@tonic-gate * 597c478bd9Sstevel@tonic-gate * Restart: 607c478bd9Sstevel@tonic-gate * A chief design constraint of zoneadmd is that it should be restartable in 617c478bd9Sstevel@tonic-gate * the case that the administrator kills it off, or it suffers a fatal error, 627c478bd9Sstevel@tonic-gate * without the running zone being impacted; this is akin to being able to 637c478bd9Sstevel@tonic-gate * reboot the service processor of a server without affecting the OS instance. 647c478bd9Sstevel@tonic-gate */ 657c478bd9Sstevel@tonic-gate 667c478bd9Sstevel@tonic-gate #include <sys/param.h> 677c478bd9Sstevel@tonic-gate #include <sys/mman.h> 687c478bd9Sstevel@tonic-gate #include <sys/types.h> 697c478bd9Sstevel@tonic-gate #include <sys/stat.h> 707c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h> 717c478bd9Sstevel@tonic-gate 727c478bd9Sstevel@tonic-gate #include <bsm/adt.h> 737c478bd9Sstevel@tonic-gate #include <bsm/adt_event.h> 747c478bd9Sstevel@tonic-gate 757c478bd9Sstevel@tonic-gate #include <alloca.h> 767c478bd9Sstevel@tonic-gate #include <assert.h> 777c478bd9Sstevel@tonic-gate #include <errno.h> 787c478bd9Sstevel@tonic-gate #include <door.h> 797c478bd9Sstevel@tonic-gate #include <fcntl.h> 807c478bd9Sstevel@tonic-gate #include <locale.h> 817c478bd9Sstevel@tonic-gate #include <signal.h> 827c478bd9Sstevel@tonic-gate #include <stdarg.h> 837c478bd9Sstevel@tonic-gate #include <stdio.h> 847c478bd9Sstevel@tonic-gate #include <stdlib.h> 857c478bd9Sstevel@tonic-gate #include <string.h> 867c478bd9Sstevel@tonic-gate #include <strings.h> 877c478bd9Sstevel@tonic-gate #include <synch.h> 887c478bd9Sstevel@tonic-gate #include <syslog.h> 897c478bd9Sstevel@tonic-gate #include <thread.h> 907c478bd9Sstevel@tonic-gate #include <unistd.h> 917c478bd9Sstevel@tonic-gate #include <wait.h> 927c478bd9Sstevel@tonic-gate #include <limits.h> 937c478bd9Sstevel@tonic-gate #include <zone.h> 949acbbeafSnn35248 #include <libbrand.h> 950679f794S #include <sys/brand.h> 967c478bd9Sstevel@tonic-gate #include <libcontract.h> 977c478bd9Sstevel@tonic-gate #include <libcontract_priv.h> 9862868012SSteve Lawrence #include <sys/brand.h> 997c478bd9Sstevel@tonic-gate #include <sys/contract/process.h> 1007c478bd9Sstevel@tonic-gate #include <sys/ctfs.h> 1012b24ab6bSSebastien Roy #include <libdladm.h> 1022b24ab6bSSebastien Roy #include <sys/dls_mgmt.h> 1033c7284bdSAlexander Eremin #include <libscf.h> 1047c478bd9Sstevel@tonic-gate 1057c478bd9Sstevel@tonic-gate #include <libzonecfg.h> 106efd4c9b6SSteve Lawrence #include <zonestat_impl.h> 1077c478bd9Sstevel@tonic-gate #include "zoneadmd.h" 1087c478bd9Sstevel@tonic-gate 1097c478bd9Sstevel@tonic-gate static char *progname; 1107c478bd9Sstevel@tonic-gate char *zone_name; /* zone which we are managing */ 111efd4c9b6SSteve Lawrence char pool_name[MAXNAMELEN]; 112e5816e35SEdward Pilatowicz char default_brand[MAXNAMELEN]; 1139acbbeafSnn35248 char brand_name[MAXNAMELEN]; 1149acbbeafSnn35248 boolean_t zone_isnative; 11584561e8cStd153743 boolean_t zone_iscluster; 1169a5d73e0SRic Aleshire boolean_t zone_islabeled; 1173c7284bdSAlexander Eremin boolean_t shutdown_in_progress; 118108322fbScarlsonj static zoneid_t zone_id; 1194ac67f02SAnurag S. Maskey dladm_handle_t dld_handle = NULL; 1207c478bd9Sstevel@tonic-gate 121c5cd6260S static char pre_statechg_hook[2 * MAXPATHLEN]; 122c5cd6260S static char post_statechg_hook[2 * MAXPATHLEN]; 123c5cd6260S char query_hook[2 * MAXPATHLEN]; 124c5cd6260S 12522321485Svp157776 zlog_t logsys; 1267c478bd9Sstevel@tonic-gate 1277c478bd9Sstevel@tonic-gate mutex_t lock = DEFAULTMUTEX; /* to serialize stuff */ 1287c478bd9Sstevel@tonic-gate mutex_t msglock = DEFAULTMUTEX; /* for calling setlocale() */ 1297c478bd9Sstevel@tonic-gate 130108322fbScarlsonj static sema_t scratch_sem; /* for scratch zones */ 131108322fbScarlsonj 1327c478bd9Sstevel@tonic-gate static char zone_door_path[MAXPATHLEN]; 1337c478bd9Sstevel@tonic-gate static int zone_door = -1; 1347c478bd9Sstevel@tonic-gate 1357c478bd9Sstevel@tonic-gate boolean_t in_death_throes = B_FALSE; /* daemon is dying */ 1367c478bd9Sstevel@tonic-gate boolean_t bringup_failure_recovery = B_FALSE; /* ignore certain failures */ 1377c478bd9Sstevel@tonic-gate 1387c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN) /* should be defined by cc -D */ 1397c478bd9Sstevel@tonic-gate #define TEXT_DOMAIN "SYS_TEST" /* Use this only if it wasn't */ 1407c478bd9Sstevel@tonic-gate #endif 1417c478bd9Sstevel@tonic-gate 1427c478bd9Sstevel@tonic-gate #define DEFAULT_LOCALE "C" 1437c478bd9Sstevel@tonic-gate 144108322fbScarlsonj static const char * 145108322fbScarlsonj z_cmd_name(zone_cmd_t zcmd) 146108322fbScarlsonj { 147108322fbScarlsonj /* This list needs to match the enum in sys/zone.h */ 148108322fbScarlsonj static const char *zcmdstr[] = { 1499acbbeafSnn35248 "ready", "boot", "forceboot", "reboot", "halt", 1503c7284bdSAlexander Eremin "note_uninstalling", "mount", "forcemount", "unmount", 1513c7284bdSAlexander Eremin "shutdown" 152108322fbScarlsonj }; 153108322fbScarlsonj 154108322fbScarlsonj if (zcmd >= sizeof (zcmdstr) / sizeof (*zcmdstr)) 155108322fbScarlsonj return ("unknown"); 156108322fbScarlsonj else 157108322fbScarlsonj return (zcmdstr[(int)zcmd]); 158108322fbScarlsonj } 159108322fbScarlsonj 1607c478bd9Sstevel@tonic-gate static char * 1617c478bd9Sstevel@tonic-gate get_execbasename(char *execfullname) 1627c478bd9Sstevel@tonic-gate { 1637c478bd9Sstevel@tonic-gate char *last_slash, *execbasename; 1647c478bd9Sstevel@tonic-gate 1657c478bd9Sstevel@tonic-gate /* guard against '/' at end of command invocation */ 1667c478bd9Sstevel@tonic-gate for (;;) { 1677c478bd9Sstevel@tonic-gate last_slash = strrchr(execfullname, '/'); 1687c478bd9Sstevel@tonic-gate if (last_slash == NULL) { 1697c478bd9Sstevel@tonic-gate execbasename = execfullname; 1707c478bd9Sstevel@tonic-gate break; 1717c478bd9Sstevel@tonic-gate } else { 1727c478bd9Sstevel@tonic-gate execbasename = last_slash + 1; 1737c478bd9Sstevel@tonic-gate if (*execbasename == '\0') { 1747c478bd9Sstevel@tonic-gate *last_slash = '\0'; 1757c478bd9Sstevel@tonic-gate continue; 1767c478bd9Sstevel@tonic-gate } 1777c478bd9Sstevel@tonic-gate break; 1787c478bd9Sstevel@tonic-gate } 1797c478bd9Sstevel@tonic-gate } 1807c478bd9Sstevel@tonic-gate return (execbasename); 1817c478bd9Sstevel@tonic-gate } 1827c478bd9Sstevel@tonic-gate 1837c478bd9Sstevel@tonic-gate static void 1847c478bd9Sstevel@tonic-gate usage(void) 1857c478bd9Sstevel@tonic-gate { 1867c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("Usage: %s -z zonename\n"), progname); 1877c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 1887c478bd9Sstevel@tonic-gate gettext("\tNote: %s should not be run directly.\n"), progname); 1897c478bd9Sstevel@tonic-gate exit(2); 1907c478bd9Sstevel@tonic-gate } 1917c478bd9Sstevel@tonic-gate 1927c478bd9Sstevel@tonic-gate /* ARGSUSED */ 1937c478bd9Sstevel@tonic-gate static void 1947c478bd9Sstevel@tonic-gate sigchld(int sig) 1957c478bd9Sstevel@tonic-gate { 1967c478bd9Sstevel@tonic-gate } 1977c478bd9Sstevel@tonic-gate 1987c478bd9Sstevel@tonic-gate char * 1997c478bd9Sstevel@tonic-gate localize_msg(char *locale, const char *msg) 2007c478bd9Sstevel@tonic-gate { 2017c478bd9Sstevel@tonic-gate char *out; 2027c478bd9Sstevel@tonic-gate 2037c478bd9Sstevel@tonic-gate (void) mutex_lock(&msglock); 2047c478bd9Sstevel@tonic-gate (void) setlocale(LC_MESSAGES, locale); 2057c478bd9Sstevel@tonic-gate out = gettext(msg); 2067c478bd9Sstevel@tonic-gate (void) setlocale(LC_MESSAGES, DEFAULT_LOCALE); 2077c478bd9Sstevel@tonic-gate (void) mutex_unlock(&msglock); 2087c478bd9Sstevel@tonic-gate return (out); 2097c478bd9Sstevel@tonic-gate } 2107c478bd9Sstevel@tonic-gate 2117c478bd9Sstevel@tonic-gate /* PRINTFLIKE3 */ 2127c478bd9Sstevel@tonic-gate void 2137c478bd9Sstevel@tonic-gate zerror(zlog_t *zlogp, boolean_t use_strerror, const char *fmt, ...) 2147c478bd9Sstevel@tonic-gate { 2157c478bd9Sstevel@tonic-gate va_list alist; 2167c478bd9Sstevel@tonic-gate char buf[MAXPATHLEN * 2]; /* enough space for err msg with a path */ 2177c478bd9Sstevel@tonic-gate char *bp; 2187c478bd9Sstevel@tonic-gate int saved_errno = errno; 2197c478bd9Sstevel@tonic-gate 2207c478bd9Sstevel@tonic-gate if (zlogp == NULL) 2217c478bd9Sstevel@tonic-gate return; 2227c478bd9Sstevel@tonic-gate if (zlogp == &logsys) 2237c478bd9Sstevel@tonic-gate (void) snprintf(buf, sizeof (buf), "[zone '%s'] ", 2247c478bd9Sstevel@tonic-gate zone_name); 2257c478bd9Sstevel@tonic-gate else 2267c478bd9Sstevel@tonic-gate buf[0] = '\0'; 2277c478bd9Sstevel@tonic-gate bp = &(buf[strlen(buf)]); 2287c478bd9Sstevel@tonic-gate 2297c478bd9Sstevel@tonic-gate /* 2307c478bd9Sstevel@tonic-gate * In theory, the locale pointer should be set to either "C" or a 2317c478bd9Sstevel@tonic-gate * char array, so it should never be NULL 2327c478bd9Sstevel@tonic-gate */ 2337c478bd9Sstevel@tonic-gate assert(zlogp->locale != NULL); 2347c478bd9Sstevel@tonic-gate /* Locale is per process, but we are multi-threaded... */ 2357c478bd9Sstevel@tonic-gate fmt = localize_msg(zlogp->locale, fmt); 2367c478bd9Sstevel@tonic-gate 2377c478bd9Sstevel@tonic-gate va_start(alist, fmt); 2387c478bd9Sstevel@tonic-gate (void) vsnprintf(bp, sizeof (buf) - (bp - buf), fmt, alist); 2397c478bd9Sstevel@tonic-gate va_end(alist); 2407c478bd9Sstevel@tonic-gate bp = &(buf[strlen(buf)]); 2417c478bd9Sstevel@tonic-gate if (use_strerror) 2427c478bd9Sstevel@tonic-gate (void) snprintf(bp, sizeof (buf) - (bp - buf), ": %s", 2437c478bd9Sstevel@tonic-gate strerror(saved_errno)); 2447c478bd9Sstevel@tonic-gate if (zlogp == &logsys) { 2457c478bd9Sstevel@tonic-gate (void) syslog(LOG_ERR, "%s", buf); 2467c478bd9Sstevel@tonic-gate } else if (zlogp->logfile != NULL) { 2477c478bd9Sstevel@tonic-gate (void) fprintf(zlogp->logfile, "%s\n", buf); 2487c478bd9Sstevel@tonic-gate } else { 2497c478bd9Sstevel@tonic-gate size_t buflen; 2507c478bd9Sstevel@tonic-gate size_t copylen; 2517c478bd9Sstevel@tonic-gate 2527c478bd9Sstevel@tonic-gate buflen = snprintf(zlogp->log, zlogp->loglen, "%s\n", buf); 2537c478bd9Sstevel@tonic-gate copylen = MIN(buflen, zlogp->loglen); 2547c478bd9Sstevel@tonic-gate zlogp->log += copylen; 2557c478bd9Sstevel@tonic-gate zlogp->loglen -= copylen; 2567c478bd9Sstevel@tonic-gate } 2577c478bd9Sstevel@tonic-gate } 2587c478bd9Sstevel@tonic-gate 2593f2f09c1Sdp /* 2603f2f09c1Sdp * Emit a warning for any boot arguments which are unrecognized. Since 2613f2f09c1Sdp * Solaris boot arguments are getopt(3c) compatible (see kernel(1m)), we 2623f2f09c1Sdp * put the arguments into an argv style array, use getopt to process them, 2633f2f09c1Sdp * and put the resultant argument string back into outargs. 2643f2f09c1Sdp * 2653f2f09c1Sdp * During the filtering, we pull out any arguments which are truly "boot" 2663f2f09c1Sdp * arguments, leaving only those which are to be passed intact to the 2673f2f09c1Sdp * progenitor process. The one we support at the moment is -i, which 2683f2f09c1Sdp * indicates to the kernel which program should be launched as 'init'. 2693f2f09c1Sdp * 2703f2f09c1Sdp * A return of Z_INVAL indicates specifically that the arguments are 2713f2f09c1Sdp * not valid; this is a non-fatal error. Except for Z_OK, all other return 2723f2f09c1Sdp * values are treated as fatal. 2733f2f09c1Sdp */ 2743f2f09c1Sdp static int 2753f2f09c1Sdp filter_bootargs(zlog_t *zlogp, const char *inargs, char *outargs, 2763f2f09c1Sdp char *init_file, char *badarg) 2773f2f09c1Sdp { 2783f2f09c1Sdp int argc = 0, argc_save; 2793f2f09c1Sdp int i; 2803f2f09c1Sdp int err; 2813f2f09c1Sdp char *arg, *lasts, **argv = NULL, **argv_save; 2823f2f09c1Sdp char zonecfg_args[BOOTARGS_MAX]; 2833f2f09c1Sdp char scratchargs[BOOTARGS_MAX], *sargs; 2843f2f09c1Sdp char c; 2853f2f09c1Sdp 2863f2f09c1Sdp bzero(outargs, BOOTARGS_MAX); 2873f2f09c1Sdp bzero(badarg, BOOTARGS_MAX); 2883f2f09c1Sdp 2893f2f09c1Sdp /* 2903f2f09c1Sdp * If the user didn't specify transient boot arguments, check 2913f2f09c1Sdp * to see if there were any specified in the zone configuration, 2923f2f09c1Sdp * and use them if applicable. 2933f2f09c1Sdp */ 2943f2f09c1Sdp if (inargs == NULL || inargs[0] == '\0') { 2953f2f09c1Sdp zone_dochandle_t handle; 2963f2f09c1Sdp if ((handle = zonecfg_init_handle()) == NULL) { 2973f2f09c1Sdp zerror(zlogp, B_TRUE, 2983f2f09c1Sdp "getting zone configuration handle"); 2993f2f09c1Sdp return (Z_BAD_HANDLE); 3003f2f09c1Sdp } 3013f2f09c1Sdp err = zonecfg_get_snapshot_handle(zone_name, handle); 3023f2f09c1Sdp if (err != Z_OK) { 3033f2f09c1Sdp zerror(zlogp, B_FALSE, 3043f2f09c1Sdp "invalid configuration snapshot"); 3053f2f09c1Sdp zonecfg_fini_handle(handle); 3063f2f09c1Sdp return (Z_BAD_HANDLE); 3073f2f09c1Sdp } 3083f2f09c1Sdp 3093f2f09c1Sdp bzero(zonecfg_args, sizeof (zonecfg_args)); 3103f2f09c1Sdp (void) zonecfg_get_bootargs(handle, zonecfg_args, 3113f2f09c1Sdp sizeof (zonecfg_args)); 3123f2f09c1Sdp inargs = zonecfg_args; 3133f2f09c1Sdp zonecfg_fini_handle(handle); 3143f2f09c1Sdp } 3153f2f09c1Sdp 3163f2f09c1Sdp if (strlen(inargs) >= BOOTARGS_MAX) { 3173f2f09c1Sdp zerror(zlogp, B_FALSE, "boot argument string too long"); 3183f2f09c1Sdp return (Z_INVAL); 3193f2f09c1Sdp } 3203f2f09c1Sdp 3213f2f09c1Sdp (void) strlcpy(scratchargs, inargs, sizeof (scratchargs)); 3223f2f09c1Sdp sargs = scratchargs; 3233f2f09c1Sdp while ((arg = strtok_r(sargs, " \t", &lasts)) != NULL) { 3243f2f09c1Sdp sargs = NULL; 3253f2f09c1Sdp argc++; 3263f2f09c1Sdp } 3273f2f09c1Sdp 3283f2f09c1Sdp if ((argv = calloc(argc + 1, sizeof (char *))) == NULL) { 3293f2f09c1Sdp zerror(zlogp, B_FALSE, "memory allocation failed"); 3303f2f09c1Sdp return (Z_NOMEM); 3313f2f09c1Sdp } 3323f2f09c1Sdp 3333f2f09c1Sdp argv_save = argv; 3343f2f09c1Sdp argc_save = argc; 3353f2f09c1Sdp 3363f2f09c1Sdp (void) strlcpy(scratchargs, inargs, sizeof (scratchargs)); 3373f2f09c1Sdp sargs = scratchargs; 3383f2f09c1Sdp i = 0; 3393f2f09c1Sdp while ((arg = strtok_r(sargs, " \t", &lasts)) != NULL) { 3403f2f09c1Sdp sargs = NULL; 3413f2f09c1Sdp if ((argv[i] = strdup(arg)) == NULL) { 3423f2f09c1Sdp err = Z_NOMEM; 3433f2f09c1Sdp zerror(zlogp, B_FALSE, "memory allocation failed"); 3443f2f09c1Sdp goto done; 3453f2f09c1Sdp } 3463f2f09c1Sdp i++; 3473f2f09c1Sdp } 3483f2f09c1Sdp 3493f2f09c1Sdp /* 3503f2f09c1Sdp * We preserve compatibility with the Solaris system boot behavior, 3513f2f09c1Sdp * which allows: 3523f2f09c1Sdp * 3533f2f09c1Sdp * # reboot kernel/unix -s -m verbose 3543f2f09c1Sdp * 3553f2f09c1Sdp * In this example, kernel/unix tells the booter what file to 3563f2f09c1Sdp * boot. We don't want reboot in a zone to be gratuitously different, 3573f2f09c1Sdp * so we silently ignore the boot file, if necessary. 3583f2f09c1Sdp */ 3593f2f09c1Sdp if (argv[0] == NULL) 3603f2f09c1Sdp goto done; 3613f2f09c1Sdp 3623f2f09c1Sdp assert(argv[0][0] != ' '); 3633f2f09c1Sdp assert(argv[0][0] != '\t'); 3643f2f09c1Sdp 3653f2f09c1Sdp if (argv[0][0] != '-' && argv[0][0] != '\0') { 3663f2f09c1Sdp argv = &argv[1]; 3673f2f09c1Sdp argc--; 3683f2f09c1Sdp } 3693f2f09c1Sdp 3703f2f09c1Sdp optind = 0; 3713f2f09c1Sdp opterr = 0; 3723f2f09c1Sdp err = Z_OK; 3739acbbeafSnn35248 while ((c = getopt(argc, argv, "fi:m:s")) != -1) { 3743f2f09c1Sdp switch (c) { 3753f2f09c1Sdp case 'i': 3763f2f09c1Sdp /* 3773f2f09c1Sdp * -i is handled by the runtime and is not passed 3783f2f09c1Sdp * along to userland 3793f2f09c1Sdp */ 3803f2f09c1Sdp (void) strlcpy(init_file, optarg, MAXPATHLEN); 3813f2f09c1Sdp break; 3829acbbeafSnn35248 case 'f': 3839acbbeafSnn35248 /* This has already been processed by zoneadm */ 3849acbbeafSnn35248 break; 3853f2f09c1Sdp case 'm': 3863f2f09c1Sdp case 's': 3873f2f09c1Sdp /* These pass through unmolested */ 3883f2f09c1Sdp (void) snprintf(outargs, BOOTARGS_MAX, 3893f2f09c1Sdp "%s -%c %s ", outargs, c, optarg ? optarg : ""); 3903f2f09c1Sdp break; 3913f2f09c1Sdp case '?': 3923f2f09c1Sdp /* 3933f2f09c1Sdp * We warn about unknown arguments but pass them 3943f2f09c1Sdp * along anyway-- if someone wants to develop their 3953f2f09c1Sdp * own init replacement, they can pass it whatever 3963f2f09c1Sdp * args they want. 3973f2f09c1Sdp */ 3983f2f09c1Sdp err = Z_INVAL; 3993f2f09c1Sdp (void) snprintf(outargs, BOOTARGS_MAX, 4003f2f09c1Sdp "%s -%c", outargs, optopt); 4013f2f09c1Sdp (void) snprintf(badarg, BOOTARGS_MAX, 4023f2f09c1Sdp "%s -%c", badarg, optopt); 4033f2f09c1Sdp break; 4043f2f09c1Sdp } 4053f2f09c1Sdp } 4063f2f09c1Sdp 4073f2f09c1Sdp /* 4083f2f09c1Sdp * For Solaris Zones we warn about and discard non-option arguments. 4093f2f09c1Sdp * Hence 'boot foo bar baz gub' --> 'boot'. However, to be similar 4103f2f09c1Sdp * to the kernel, we concat up all the other remaining boot args. 4113f2f09c1Sdp * and warn on them as a group. 4123f2f09c1Sdp */ 4133f2f09c1Sdp if (optind < argc) { 4143f2f09c1Sdp err = Z_INVAL; 4153f2f09c1Sdp while (optind < argc) { 4163f2f09c1Sdp (void) snprintf(badarg, BOOTARGS_MAX, "%s%s%s", 4173f2f09c1Sdp badarg, strlen(badarg) > 0 ? " " : "", 4183f2f09c1Sdp argv[optind]); 4193f2f09c1Sdp optind++; 4203f2f09c1Sdp } 4213f2f09c1Sdp zerror(zlogp, B_FALSE, "WARNING: Unused or invalid boot " 4223f2f09c1Sdp "arguments `%s'.", badarg); 4233f2f09c1Sdp } 4243f2f09c1Sdp 4253f2f09c1Sdp done: 4263f2f09c1Sdp for (i = 0; i < argc_save; i++) { 4273f2f09c1Sdp if (argv_save[i] != NULL) 4283f2f09c1Sdp free(argv_save[i]); 4293f2f09c1Sdp } 4303f2f09c1Sdp free(argv_save); 4313f2f09c1Sdp return (err); 4323f2f09c1Sdp } 4333f2f09c1Sdp 4343f2f09c1Sdp 4357c478bd9Sstevel@tonic-gate static int 4367c478bd9Sstevel@tonic-gate mkzonedir(zlog_t *zlogp) 4377c478bd9Sstevel@tonic-gate { 4387c478bd9Sstevel@tonic-gate struct stat st; 4397c478bd9Sstevel@tonic-gate /* 4407c478bd9Sstevel@tonic-gate * We must create and lock everyone but root out of ZONES_TMPDIR 4417c478bd9Sstevel@tonic-gate * since anyone can open any UNIX domain socket, regardless of 4427c478bd9Sstevel@tonic-gate * its file system permissions. Sigh... 4437c478bd9Sstevel@tonic-gate */ 4447c478bd9Sstevel@tonic-gate if (mkdir(ZONES_TMPDIR, S_IRWXU) < 0 && errno != EEXIST) { 4457c478bd9Sstevel@tonic-gate zerror(zlogp, B_TRUE, "could not mkdir '%s'", ZONES_TMPDIR); 4467c478bd9Sstevel@tonic-gate return (-1); 4477c478bd9Sstevel@tonic-gate } 4487c478bd9Sstevel@tonic-gate /* paranoia */ 4494bc0a2efScasper if ((stat(ZONES_TMPDIR, &st) < 0) || !S_ISDIR(st.st_mode)) { 4507c478bd9Sstevel@tonic-gate zerror(zlogp, B_TRUE, "'%s' is not a directory", ZONES_TMPDIR); 4517c478bd9Sstevel@tonic-gate return (-1); 4527c478bd9Sstevel@tonic-gate } 4537c478bd9Sstevel@tonic-gate (void) chmod(ZONES_TMPDIR, S_IRWXU); 4547c478bd9Sstevel@tonic-gate return (0); 4557c478bd9Sstevel@tonic-gate } 4567c478bd9Sstevel@tonic-gate 457108322fbScarlsonj /* 458c5cd6260S * Run the brand's pre-state change callback, if it exists. 459c5cd6260S */ 460c5cd6260S static int 461c5cd6260S brand_prestatechg(zlog_t *zlogp, int state, int cmd) 462c5cd6260S { 463c5cd6260S char cmdbuf[2 * MAXPATHLEN]; 4642a8b76b2SSusan Kamm-Worrell const char *altroot; 465c5cd6260S 466c5cd6260S if (pre_statechg_hook[0] == '\0') 467c5cd6260S return (0); 468c5cd6260S 4692a8b76b2SSusan Kamm-Worrell altroot = zonecfg_get_root(); 4702a8b76b2SSusan Kamm-Worrell if (snprintf(cmdbuf, sizeof (cmdbuf), "%s %d %d %s", pre_statechg_hook, 4712a8b76b2SSusan Kamm-Worrell state, cmd, altroot) > sizeof (cmdbuf)) 472c5cd6260S return (-1); 473c5cd6260S 474c5cd6260S if (do_subproc(zlogp, cmdbuf, NULL) != 0) 475c5cd6260S return (-1); 476c5cd6260S 477c5cd6260S return (0); 478c5cd6260S } 479c5cd6260S 480c5cd6260S /* 481c5cd6260S * Run the brand's post-state change callback, if it exists. 482c5cd6260S */ 483c5cd6260S static int 484c5cd6260S brand_poststatechg(zlog_t *zlogp, int state, int cmd) 485c5cd6260S { 486c5cd6260S char cmdbuf[2 * MAXPATHLEN]; 4872a8b76b2SSusan Kamm-Worrell const char *altroot; 488c5cd6260S 489c5cd6260S if (post_statechg_hook[0] == '\0') 490c5cd6260S return (0); 491c5cd6260S 4922a8b76b2SSusan Kamm-Worrell altroot = zonecfg_get_root(); 4932a8b76b2SSusan Kamm-Worrell if (snprintf(cmdbuf, sizeof (cmdbuf), "%s %d %d %s", post_statechg_hook, 4942a8b76b2SSusan Kamm-Worrell state, cmd, altroot) > sizeof (cmdbuf)) 495c5cd6260S return (-1); 496c5cd6260S 497c5cd6260S if (do_subproc(zlogp, cmdbuf, NULL) != 0) 498c5cd6260S return (-1); 499c5cd6260S 500c5cd6260S return (0); 501c5cd6260S } 502c5cd6260S 503c5cd6260S /* 504efd4c9b6SSteve Lawrence * Notify zonestatd of the new zone. If zonestatd is not running, this 505efd4c9b6SSteve Lawrence * will do nothing. 506efd4c9b6SSteve Lawrence */ 507efd4c9b6SSteve Lawrence static void 508efd4c9b6SSteve Lawrence notify_zonestatd(zoneid_t zoneid) 509efd4c9b6SSteve Lawrence { 510efd4c9b6SSteve Lawrence int cmd[2]; 511efd4c9b6SSteve Lawrence int fd; 512efd4c9b6SSteve Lawrence door_arg_t params; 513efd4c9b6SSteve Lawrence 514efd4c9b6SSteve Lawrence fd = open(ZS_DOOR_PATH, O_RDONLY); 515efd4c9b6SSteve Lawrence if (fd < 0) 516efd4c9b6SSteve Lawrence return; 517efd4c9b6SSteve Lawrence 518efd4c9b6SSteve Lawrence cmd[0] = ZSD_CMD_NEW_ZONE; 519efd4c9b6SSteve Lawrence cmd[1] = zoneid; 520efd4c9b6SSteve Lawrence params.data_ptr = (char *)&cmd; 521efd4c9b6SSteve Lawrence params.data_size = sizeof (cmd); 522efd4c9b6SSteve Lawrence params.desc_ptr = NULL; 523efd4c9b6SSteve Lawrence params.desc_num = 0; 524efd4c9b6SSteve Lawrence params.rbuf = NULL; 525efd4c9b6SSteve Lawrence params.rsize = NULL; 526efd4c9b6SSteve Lawrence (void) door_call(fd, ¶ms); 527efd4c9b6SSteve Lawrence (void) close(fd); 528efd4c9b6SSteve Lawrence } 529efd4c9b6SSteve Lawrence 530efd4c9b6SSteve Lawrence /* 531108322fbScarlsonj * Bring a zone up to the pre-boot "ready" stage. The mount_cmd argument is 532108322fbScarlsonj * 'true' if this is being invoked as part of the processing for the "mount" 533108322fbScarlsonj * subcommand. 534108322fbScarlsonj */ 535108322fbScarlsonj static int 536c5cd6260S zone_ready(zlog_t *zlogp, zone_mnt_t mount_cmd, int zstate) 5377c478bd9Sstevel@tonic-gate { 5387c478bd9Sstevel@tonic-gate int err; 5397c478bd9Sstevel@tonic-gate 540c5cd6260S if (brand_prestatechg(zlogp, zstate, Z_READY) != 0) 541c5cd6260S return (-1); 542c5cd6260S 5437c478bd9Sstevel@tonic-gate if ((err = zonecfg_create_snapshot(zone_name)) != Z_OK) { 5447c478bd9Sstevel@tonic-gate zerror(zlogp, B_FALSE, "unable to create snapshot: %s", 5457c478bd9Sstevel@tonic-gate zonecfg_strerror(err)); 5468fe9fdf2SDan Price goto bad; 5477c478bd9Sstevel@tonic-gate } 5487c478bd9Sstevel@tonic-gate 549108322fbScarlsonj if ((zone_id = vplat_create(zlogp, mount_cmd)) == -1) { 5507c478bd9Sstevel@tonic-gate if ((err = zonecfg_destroy_snapshot(zone_name)) != Z_OK) 5517c478bd9Sstevel@tonic-gate zerror(zlogp, B_FALSE, "destroying snapshot: %s", 5527c478bd9Sstevel@tonic-gate zonecfg_strerror(err)); 5538fe9fdf2SDan Price goto bad; 5547c478bd9Sstevel@tonic-gate } 555555afedfScarlsonj if (vplat_bringup(zlogp, mount_cmd, zone_id) != 0) { 5567c478bd9Sstevel@tonic-gate bringup_failure_recovery = B_TRUE; 5576cfd72c6Sgjelinek (void) vplat_teardown(NULL, (mount_cmd != Z_MNT_BOOT), B_FALSE); 5587c478bd9Sstevel@tonic-gate if ((err = zonecfg_destroy_snapshot(zone_name)) != Z_OK) 5597c478bd9Sstevel@tonic-gate zerror(zlogp, B_FALSE, "destroying snapshot: %s", 5607c478bd9Sstevel@tonic-gate zonecfg_strerror(err)); 5618fe9fdf2SDan Price goto bad; 5627c478bd9Sstevel@tonic-gate } 5637c478bd9Sstevel@tonic-gate 564c5cd6260S if (brand_poststatechg(zlogp, zstate, Z_READY) != 0) 5658fe9fdf2SDan Price goto bad; 566c5cd6260S 5677c478bd9Sstevel@tonic-gate return (0); 5688fe9fdf2SDan Price 5698fe9fdf2SDan Price bad: 5708fe9fdf2SDan Price /* 5718fe9fdf2SDan Price * If something goes wrong, we up the zones's state to the target 5728fe9fdf2SDan Price * state, READY, and then invoke the hook as if we're halting. 5738fe9fdf2SDan Price */ 5748fe9fdf2SDan Price (void) brand_poststatechg(zlogp, ZONE_STATE_READY, Z_HALT); 5758fe9fdf2SDan Price return (-1); 5767c478bd9Sstevel@tonic-gate } 5777c478bd9Sstevel@tonic-gate 578555afedfScarlsonj int 579555afedfScarlsonj init_template(void) 5807c478bd9Sstevel@tonic-gate { 5817c478bd9Sstevel@tonic-gate int fd; 5827c478bd9Sstevel@tonic-gate int err = 0; 5837c478bd9Sstevel@tonic-gate 5847c478bd9Sstevel@tonic-gate fd = open64(CTFS_ROOT "/process/template", O_RDWR); 5857c478bd9Sstevel@tonic-gate if (fd == -1) 5867c478bd9Sstevel@tonic-gate return (-1); 5877c478bd9Sstevel@tonic-gate 5887c478bd9Sstevel@tonic-gate /* 5897c478bd9Sstevel@tonic-gate * For now, zoneadmd doesn't do anything with the contract. 5907c478bd9Sstevel@tonic-gate * Deliver no events, don't inherit, and allow it to be orphaned. 5917c478bd9Sstevel@tonic-gate */ 5927c478bd9Sstevel@tonic-gate err |= ct_tmpl_set_critical(fd, 0); 5937c478bd9Sstevel@tonic-gate err |= ct_tmpl_set_informative(fd, 0); 5947c478bd9Sstevel@tonic-gate err |= ct_pr_tmpl_set_fatal(fd, CT_PR_EV_HWERR); 5957c478bd9Sstevel@tonic-gate err |= ct_pr_tmpl_set_param(fd, CT_PR_PGRPONLY | CT_PR_REGENT); 5967c478bd9Sstevel@tonic-gate if (err || ct_tmpl_activate(fd)) { 5977c478bd9Sstevel@tonic-gate (void) close(fd); 5987c478bd9Sstevel@tonic-gate return (-1); 5997c478bd9Sstevel@tonic-gate } 6007c478bd9Sstevel@tonic-gate 6017c478bd9Sstevel@tonic-gate return (fd); 6027c478bd9Sstevel@tonic-gate } 6037c478bd9Sstevel@tonic-gate 6049acbbeafSnn35248 typedef struct fs_callback { 6059acbbeafSnn35248 zlog_t *zlogp; 6069acbbeafSnn35248 zoneid_t zoneid; 607910f48daSedp boolean_t mount_cmd; 6089acbbeafSnn35248 } fs_callback_t; 6099acbbeafSnn35248 6107c478bd9Sstevel@tonic-gate static int 6119acbbeafSnn35248 mount_early_fs(void *data, const char *spec, const char *dir, 6129acbbeafSnn35248 const char *fstype, const char *opt) 6137c478bd9Sstevel@tonic-gate { 6149acbbeafSnn35248 zlog_t *zlogp = ((fs_callback_t *)data)->zlogp; 6159acbbeafSnn35248 zoneid_t zoneid = ((fs_callback_t *)data)->zoneid; 616910f48daSedp boolean_t mount_cmd = ((fs_callback_t *)data)->mount_cmd; 617d314f035Sedp char rootpath[MAXPATHLEN]; 6187c478bd9Sstevel@tonic-gate pid_t child; 6197c478bd9Sstevel@tonic-gate int child_status; 6207c478bd9Sstevel@tonic-gate int tmpl_fd; 621d314f035Sedp int rv; 6227c478bd9Sstevel@tonic-gate ctid_t ct; 6237c478bd9Sstevel@tonic-gate 624910f48daSedp /* determine the zone rootpath */ 625910f48daSedp if (mount_cmd) { 626910f48daSedp char zonepath[MAXPATHLEN]; 627910f48daSedp char luroot[MAXPATHLEN]; 628910f48daSedp 629910f48daSedp if (zone_get_zonepath(zone_name, 630910f48daSedp zonepath, sizeof (zonepath)) != Z_OK) { 631910f48daSedp zerror(zlogp, B_FALSE, "unable to determine zone path"); 632910f48daSedp return (-1); 633910f48daSedp } 634910f48daSedp 635910f48daSedp (void) snprintf(luroot, sizeof (luroot), "%s/lu", zonepath); 636910f48daSedp resolve_lofs(zlogp, luroot, sizeof (luroot)); 637910f48daSedp (void) strlcpy(rootpath, luroot, sizeof (rootpath)); 638910f48daSedp } else { 639910f48daSedp if (zone_get_rootpath(zone_name, 640910f48daSedp rootpath, sizeof (rootpath)) != Z_OK) { 641d314f035Sedp zerror(zlogp, B_FALSE, "unable to determine zone root"); 642d314f035Sedp return (-1); 643d314f035Sedp } 644910f48daSedp } 645d314f035Sedp 646d314f035Sedp if ((rv = valid_mount_path(zlogp, rootpath, spec, dir, fstype)) < 0) { 647d314f035Sedp zerror(zlogp, B_FALSE, "%s%s is not a valid mount point", 648d314f035Sedp rootpath, dir); 649d314f035Sedp return (-1); 650d314f035Sedp } else if (rv > 0) { 651d314f035Sedp /* The mount point path doesn't exist, create it now. */ 652d314f035Sedp if (make_one_dir(zlogp, rootpath, dir, 653d314f035Sedp DEFAULT_DIR_MODE, DEFAULT_DIR_USER, 654d314f035Sedp DEFAULT_DIR_GROUP) != 0) { 655d314f035Sedp zerror(zlogp, B_FALSE, "failed to create mount point"); 656d314f035Sedp return (-1); 657d314f035Sedp } 658d314f035Sedp 659d314f035Sedp /* 660d314f035Sedp * Now this might seem weird, but we need to invoke 661d314f035Sedp * valid_mount_path() again. Why? Because it checks 662d314f035Sedp * to make sure that the mount point path is canonical, 663d314f035Sedp * which it can only do if the path exists, so now that 664d314f035Sedp * we've created the path we have to verify it again. 665d314f035Sedp */ 666d314f035Sedp if ((rv = valid_mount_path(zlogp, rootpath, spec, dir, 667d314f035Sedp fstype)) < 0) { 668d314f035Sedp zerror(zlogp, B_FALSE, 669d314f035Sedp "%s%s is not a valid mount point", rootpath, dir); 670d314f035Sedp return (-1); 671d314f035Sedp } 672d314f035Sedp } 673d314f035Sedp 6747c478bd9Sstevel@tonic-gate if ((tmpl_fd = init_template()) == -1) { 6757c478bd9Sstevel@tonic-gate zerror(zlogp, B_TRUE, "failed to create contract"); 6767c478bd9Sstevel@tonic-gate return (-1); 6777c478bd9Sstevel@tonic-gate } 6787c478bd9Sstevel@tonic-gate 6797c478bd9Sstevel@tonic-gate if ((child = fork()) == -1) { 6807c478bd9Sstevel@tonic-gate (void) ct_tmpl_clear(tmpl_fd); 6817c478bd9Sstevel@tonic-gate (void) close(tmpl_fd); 6827c478bd9Sstevel@tonic-gate zerror(zlogp, B_TRUE, "failed to fork"); 6837c478bd9Sstevel@tonic-gate return (-1); 6847c478bd9Sstevel@tonic-gate 6857c478bd9Sstevel@tonic-gate } else if (child == 0) { /* child */ 6869acbbeafSnn35248 char opt_buf[MAX_MNTOPT_STR]; 6879acbbeafSnn35248 int optlen = 0; 6889acbbeafSnn35248 int mflag = MS_DATA; 6899acbbeafSnn35248 6907c478bd9Sstevel@tonic-gate (void) ct_tmpl_clear(tmpl_fd); 6917c478bd9Sstevel@tonic-gate /* 6927c478bd9Sstevel@tonic-gate * Even though there are no procs running in the zone, we 6937c478bd9Sstevel@tonic-gate * do this for paranoia's sake. 6947c478bd9Sstevel@tonic-gate */ 6957c478bd9Sstevel@tonic-gate (void) closefrom(0); 6967c478bd9Sstevel@tonic-gate 6977c478bd9Sstevel@tonic-gate if (zone_enter(zoneid) == -1) { 6987c478bd9Sstevel@tonic-gate _exit(errno); 6997c478bd9Sstevel@tonic-gate } 7009acbbeafSnn35248 if (opt != NULL) { 7019acbbeafSnn35248 /* 7029acbbeafSnn35248 * The mount() system call is incredibly annoying. 7039acbbeafSnn35248 * If options are specified, we need to copy them 7049acbbeafSnn35248 * into a temporary buffer since the mount() system 7059acbbeafSnn35248 * call will overwrite the options string. It will 7069acbbeafSnn35248 * also fail if the new option string it wants to 7079acbbeafSnn35248 * write is bigger than the one we passed in, so 7089acbbeafSnn35248 * you must pass in a buffer of the maximum possible 7099acbbeafSnn35248 * option string length. sigh. 7109acbbeafSnn35248 */ 7119acbbeafSnn35248 (void) strlcpy(opt_buf, opt, sizeof (opt_buf)); 7129acbbeafSnn35248 opt = opt_buf; 7139acbbeafSnn35248 optlen = MAX_MNTOPT_STR; 7149acbbeafSnn35248 mflag = MS_OPTIONSTR; 7159acbbeafSnn35248 } 7169acbbeafSnn35248 if (mount(spec, dir, mflag, fstype, NULL, 0, opt, optlen) != 0) 7177c478bd9Sstevel@tonic-gate _exit(errno); 7187c478bd9Sstevel@tonic-gate _exit(0); 7197c478bd9Sstevel@tonic-gate } 7207c478bd9Sstevel@tonic-gate 7217c478bd9Sstevel@tonic-gate /* parent */ 7227c478bd9Sstevel@tonic-gate if (contract_latest(&ct) == -1) 7237c478bd9Sstevel@tonic-gate ct = -1; 7247c478bd9Sstevel@tonic-gate (void) ct_tmpl_clear(tmpl_fd); 7257c478bd9Sstevel@tonic-gate (void) close(tmpl_fd); 7267c478bd9Sstevel@tonic-gate if (waitpid(child, &child_status, 0) != child) { 7277c478bd9Sstevel@tonic-gate /* unexpected: we must have been signalled */ 7287c478bd9Sstevel@tonic-gate (void) contract_abandon_id(ct); 7297c478bd9Sstevel@tonic-gate return (-1); 7307c478bd9Sstevel@tonic-gate } 7317c478bd9Sstevel@tonic-gate (void) contract_abandon_id(ct); 7327c478bd9Sstevel@tonic-gate if (WEXITSTATUS(child_status) != 0) { 7337c478bd9Sstevel@tonic-gate errno = WEXITSTATUS(child_status); 7347c478bd9Sstevel@tonic-gate zerror(zlogp, B_TRUE, "mount of %s failed", dir); 7357c478bd9Sstevel@tonic-gate return (-1); 7367c478bd9Sstevel@tonic-gate } 7377c478bd9Sstevel@tonic-gate 7387c478bd9Sstevel@tonic-gate return (0); 7397c478bd9Sstevel@tonic-gate } 7407c478bd9Sstevel@tonic-gate 741c5cd6260S /* 742c5cd6260S * If retstr is not NULL, the output of the subproc is returned in the str, 743c5cd6260S * otherwise it is output using zerror(). Any memory allocated for retstr 744c5cd6260S * should be freed by the caller. 745c5cd6260S */ 7469acbbeafSnn35248 int 747c5cd6260S do_subproc(zlog_t *zlogp, char *cmdbuf, char **retstr) 748108322fbScarlsonj { 749c5cd6260S char buf[1024]; /* arbitrary large amount */ 750c5cd6260S char *inbuf; 7519acbbeafSnn35248 FILE *file; 7529acbbeafSnn35248 int status; 753c5cd6260S int rd_cnt; 754c5cd6260S 755c5cd6260S if (retstr != NULL) { 756c5cd6260S if ((*retstr = malloc(1024)) == NULL) { 757c5cd6260S zerror(zlogp, B_FALSE, "out of memory"); 758c5cd6260S return (-1); 759c5cd6260S } 760c5cd6260S inbuf = *retstr; 761c5cd6260S rd_cnt = 0; 762c5cd6260S } else { 763c5cd6260S inbuf = buf; 764c5cd6260S } 765108322fbScarlsonj 7669acbbeafSnn35248 file = popen(cmdbuf, "r"); 7679acbbeafSnn35248 if (file == NULL) { 7689acbbeafSnn35248 zerror(zlogp, B_TRUE, "could not launch: %s", cmdbuf); 769108322fbScarlsonj return (-1); 7709acbbeafSnn35248 } 771108322fbScarlsonj 772c5cd6260S while (fgets(inbuf, 1024, file) != NULL) { 7731f314719S if (retstr == NULL) { 7741f314719S if (zlogp != &logsys) 7759acbbeafSnn35248 zerror(zlogp, B_FALSE, "%s", inbuf); 776c5cd6260S } else { 777c5cd6260S char *p; 778c5cd6260S 779c5cd6260S rd_cnt += 1024 - 1; 780c5cd6260S if ((p = realloc(*retstr, rd_cnt + 1024)) == NULL) { 781c5cd6260S zerror(zlogp, B_FALSE, "out of memory"); 782c5cd6260S (void) pclose(file); 783c5cd6260S return (-1); 784c5cd6260S } 785c5cd6260S 786c5cd6260S *retstr = p; 787c5cd6260S inbuf = *retstr + rd_cnt; 788c5cd6260S } 789c5cd6260S } 7909acbbeafSnn35248 status = pclose(file); 7919acbbeafSnn35248 7929acbbeafSnn35248 if (WIFSIGNALED(status)) { 7939acbbeafSnn35248 zerror(zlogp, B_FALSE, "%s unexpectedly terminated due to " 7949acbbeafSnn35248 "signal %d", cmdbuf, WTERMSIG(status)); 7955518d15bSdp return (-1); 7969acbbeafSnn35248 } 7979acbbeafSnn35248 assert(WIFEXITED(status)); 7989acbbeafSnn35248 if (WEXITSTATUS(status) == ZEXIT_EXEC) { 7999acbbeafSnn35248 zerror(zlogp, B_FALSE, "failed to exec %s", cmdbuf); 800108322fbScarlsonj return (-1); 8019acbbeafSnn35248 } 8029acbbeafSnn35248 return (WEXITSTATUS(status)); 803108322fbScarlsonj } 804108322fbScarlsonj 805108322fbScarlsonj static int 806c5cd6260S zone_bootup(zlog_t *zlogp, const char *bootargs, int zstate) 8077c478bd9Sstevel@tonic-gate { 8087c478bd9Sstevel@tonic-gate zoneid_t zoneid; 8097c478bd9Sstevel@tonic-gate struct stat st; 810ff17c8bfSgjelinek char zpath[MAXPATHLEN], initpath[MAXPATHLEN], init_file[MAXPATHLEN]; 8113f2f09c1Sdp char nbootargs[BOOTARGS_MAX]; 8129acbbeafSnn35248 char cmdbuf[MAXPATHLEN]; 8139acbbeafSnn35248 fs_callback_t cb; 814123807fbSedp brand_handle_t bh; 8152b24ab6bSSebastien Roy zone_iptype_t iptype; 8162b24ab6bSSebastien Roy boolean_t links_loaded = B_FALSE; 8172b24ab6bSSebastien Roy dladm_status_t status; 8182b24ab6bSSebastien Roy char errmsg[DLADM_STRSIZE]; 8193f2f09c1Sdp int err; 820bafd1f14SJerry Jelinek boolean_t restart_init; 8217c478bd9Sstevel@tonic-gate 822c5cd6260S if (brand_prestatechg(zlogp, zstate, Z_BOOT) != 0) 823c5cd6260S return (-1); 824c5cd6260S 8257c478bd9Sstevel@tonic-gate if ((zoneid = getzoneidbyname(zone_name)) == -1) { 8267c478bd9Sstevel@tonic-gate zerror(zlogp, B_TRUE, "unable to get zoneid"); 8278fe9fdf2SDan Price goto bad; 8287c478bd9Sstevel@tonic-gate } 8297c478bd9Sstevel@tonic-gate 8309acbbeafSnn35248 cb.zlogp = zlogp; 8319acbbeafSnn35248 cb.zoneid = zoneid; 832910f48daSedp cb.mount_cmd = B_FALSE; 8339acbbeafSnn35248 8349acbbeafSnn35248 /* Get a handle to the brand info for this zone */ 835123807fbSedp if ((bh = brand_open(brand_name)) == NULL) { 8369acbbeafSnn35248 zerror(zlogp, B_FALSE, "unable to determine zone brand"); 8378fe9fdf2SDan Price goto bad; 8389acbbeafSnn35248 } 8399acbbeafSnn35248 8409acbbeafSnn35248 /* 8419acbbeafSnn35248 * Get the list of filesystems to mount from the brand 8429acbbeafSnn35248 * configuration. These mounts are done via a thread that will 8439acbbeafSnn35248 * enter the zone, so they are done from within the context of the 8449acbbeafSnn35248 * zone. 8459acbbeafSnn35248 */ 846123807fbSedp if (brand_platform_iter_mounts(bh, mount_early_fs, &cb) != 0) { 8479acbbeafSnn35248 zerror(zlogp, B_FALSE, "unable to mount filesystems"); 848123807fbSedp brand_close(bh); 8498fe9fdf2SDan Price goto bad; 8509acbbeafSnn35248 } 8519acbbeafSnn35248 8529acbbeafSnn35248 /* 8539acbbeafSnn35248 * Get the brand's boot callback if it exists. 8549acbbeafSnn35248 */ 855ff17c8bfSgjelinek if (zone_get_zonepath(zone_name, zpath, sizeof (zpath)) != Z_OK) { 856ff17c8bfSgjelinek zerror(zlogp, B_FALSE, "unable to determine zone path"); 857e767a340Sgjelinek brand_close(bh); 8588fe9fdf2SDan Price goto bad; 8599acbbeafSnn35248 } 8609acbbeafSnn35248 (void) strcpy(cmdbuf, EXEC_PREFIX); 861ff17c8bfSgjelinek if (brand_get_boot(bh, zone_name, zpath, cmdbuf + EXEC_LEN, 862ff17c8bfSgjelinek sizeof (cmdbuf) - EXEC_LEN) != 0) { 8639acbbeafSnn35248 zerror(zlogp, B_FALSE, 8649acbbeafSnn35248 "unable to determine branded zone's boot callback"); 865123807fbSedp brand_close(bh); 8668fe9fdf2SDan Price goto bad; 8679acbbeafSnn35248 } 8689acbbeafSnn35248 8699acbbeafSnn35248 /* Get the path for this zone's init(1M) (or equivalent) process. */ 870123807fbSedp if (brand_get_initname(bh, init_file, MAXPATHLEN) != 0) { 8719acbbeafSnn35248 zerror(zlogp, B_FALSE, 8729acbbeafSnn35248 "unable to determine zone's init(1M) location"); 873123807fbSedp brand_close(bh); 8748fe9fdf2SDan Price goto bad; 8759acbbeafSnn35248 } 8769acbbeafSnn35248 877bafd1f14SJerry Jelinek /* See if this zone's brand should restart init if it dies. */ 878bafd1f14SJerry Jelinek restart_init = brand_restartinit(bh); 879bafd1f14SJerry Jelinek 880123807fbSedp brand_close(bh); 8817c478bd9Sstevel@tonic-gate 8823f2f09c1Sdp err = filter_bootargs(zlogp, bootargs, nbootargs, init_file, 8833f2f09c1Sdp bad_boot_arg); 8843f2f09c1Sdp if (err == Z_INVAL) 8853f2f09c1Sdp eventstream_write(Z_EVT_ZONE_BADARGS); 8863f2f09c1Sdp else if (err != Z_OK) 8878fe9fdf2SDan Price goto bad; 8883f2f09c1Sdp 8893f2f09c1Sdp assert(init_file[0] != '\0'); 8903f2f09c1Sdp 8919acbbeafSnn35248 /* Try to anticipate possible problems: Make sure init is executable. */ 892ff17c8bfSgjelinek if (zone_get_rootpath(zone_name, zpath, sizeof (zpath)) != Z_OK) { 8937c478bd9Sstevel@tonic-gate zerror(zlogp, B_FALSE, "unable to determine zone root"); 8948fe9fdf2SDan Price goto bad; 8957c478bd9Sstevel@tonic-gate } 8969acbbeafSnn35248 897ff17c8bfSgjelinek (void) snprintf(initpath, sizeof (initpath), "%s%s", zpath, init_file); 8987c478bd9Sstevel@tonic-gate 8997c478bd9Sstevel@tonic-gate if (stat(initpath, &st) == -1) { 9007c478bd9Sstevel@tonic-gate zerror(zlogp, B_TRUE, "could not stat %s", initpath); 9018fe9fdf2SDan Price goto bad; 9027c478bd9Sstevel@tonic-gate } 9037c478bd9Sstevel@tonic-gate 9047c478bd9Sstevel@tonic-gate if ((st.st_mode & S_IXUSR) == 0) { 9057c478bd9Sstevel@tonic-gate zerror(zlogp, B_FALSE, "%s is not executable", initpath); 9068fe9fdf2SDan Price goto bad; 9077c478bd9Sstevel@tonic-gate } 9087c478bd9Sstevel@tonic-gate 9099acbbeafSnn35248 /* 9102b24ab6bSSebastien Roy * Exclusive stack zones interact with the dlmgmtd running in the 9112b24ab6bSSebastien Roy * global zone. dladm_zone_boot() tells dlmgmtd that this zone is 9122b24ab6bSSebastien Roy * booting, and loads its datalinks from the zone's datalink 9132b24ab6bSSebastien Roy * configuration file. 9142b24ab6bSSebastien Roy */ 9152b24ab6bSSebastien Roy if (vplat_get_iptype(zlogp, &iptype) == 0 && iptype == ZS_EXCLUSIVE) { 9162b24ab6bSSebastien Roy status = dladm_zone_boot(dld_handle, zoneid); 9172b24ab6bSSebastien Roy if (status != DLADM_STATUS_OK) { 9182b24ab6bSSebastien Roy zerror(zlogp, B_FALSE, "unable to load zone datalinks: " 9192b24ab6bSSebastien Roy " %s", dladm_status2str(status, errmsg)); 9202b24ab6bSSebastien Roy goto bad; 9212b24ab6bSSebastien Roy } 9222b24ab6bSSebastien Roy links_loaded = B_TRUE; 9232b24ab6bSSebastien Roy } 9242b24ab6bSSebastien Roy 9252b24ab6bSSebastien Roy /* 9269acbbeafSnn35248 * If there is a brand 'boot' callback, execute it now to give the 9279acbbeafSnn35248 * brand one last chance to do any additional setup before the zone 9289acbbeafSnn35248 * is booted. 9299acbbeafSnn35248 */ 9309acbbeafSnn35248 if ((strlen(cmdbuf) > EXEC_LEN) && 931c5cd6260S (do_subproc(zlogp, cmdbuf, NULL) != Z_OK)) { 9329acbbeafSnn35248 zerror(zlogp, B_FALSE, "%s failed", cmdbuf); 9338fe9fdf2SDan Price goto bad; 9349acbbeafSnn35248 } 9359acbbeafSnn35248 9363f2f09c1Sdp if (zone_setattr(zoneid, ZONE_ATTR_INITNAME, init_file, 0) == -1) { 9373f2f09c1Sdp zerror(zlogp, B_TRUE, "could not set zone boot file"); 9388fe9fdf2SDan Price goto bad; 9393f2f09c1Sdp } 9403f2f09c1Sdp 9413f2f09c1Sdp if (zone_setattr(zoneid, ZONE_ATTR_BOOTARGS, nbootargs, 0) == -1) { 9423f2f09c1Sdp zerror(zlogp, B_TRUE, "could not set zone boot arguments"); 9438fe9fdf2SDan Price goto bad; 9443f2f09c1Sdp } 9453f2f09c1Sdp 946bafd1f14SJerry Jelinek if (!restart_init && zone_setattr(zoneid, ZONE_ATTR_INITNORESTART, 947bafd1f14SJerry Jelinek NULL, 0) == -1) { 948bafd1f14SJerry Jelinek zerror(zlogp, B_TRUE, "could not set zone init-no-restart"); 949bafd1f14SJerry Jelinek goto bad; 950bafd1f14SJerry Jelinek } 951bafd1f14SJerry Jelinek 952efd4c9b6SSteve Lawrence /* 953efd4c9b6SSteve Lawrence * Inform zonestatd of a new zone so that it can install a door for 954efd4c9b6SSteve Lawrence * the zone to contact it. 955efd4c9b6SSteve Lawrence */ 956efd4c9b6SSteve Lawrence notify_zonestatd(zone_id); 957efd4c9b6SSteve Lawrence 9583f2f09c1Sdp if (zone_boot(zoneid) == -1) { 9597c478bd9Sstevel@tonic-gate zerror(zlogp, B_TRUE, "unable to boot zone"); 9608fe9fdf2SDan Price goto bad; 9617c478bd9Sstevel@tonic-gate } 9627c478bd9Sstevel@tonic-gate 963c5cd6260S if (brand_poststatechg(zlogp, zstate, Z_BOOT) != 0) 9648fe9fdf2SDan Price goto bad; 965c5cd6260S 9667c478bd9Sstevel@tonic-gate return (0); 9678fe9fdf2SDan Price 9688fe9fdf2SDan Price bad: 9698fe9fdf2SDan Price /* 9708fe9fdf2SDan Price * If something goes wrong, we up the zones's state to the target 9718fe9fdf2SDan Price * state, RUNNING, and then invoke the hook as if we're halting. 9728fe9fdf2SDan Price */ 9738fe9fdf2SDan Price (void) brand_poststatechg(zlogp, ZONE_STATE_RUNNING, Z_HALT); 9742b24ab6bSSebastien Roy if (links_loaded) 9752b24ab6bSSebastien Roy (void) dladm_zone_halt(dld_handle, zoneid); 9768fe9fdf2SDan Price return (-1); 9777c478bd9Sstevel@tonic-gate } 9787c478bd9Sstevel@tonic-gate 9797c478bd9Sstevel@tonic-gate static int 980c5cd6260S zone_halt(zlog_t *zlogp, boolean_t unmount_cmd, boolean_t rebooting, int zstate) 9817c478bd9Sstevel@tonic-gate { 9827c478bd9Sstevel@tonic-gate int err; 9837c478bd9Sstevel@tonic-gate 984c5cd6260S if (brand_prestatechg(zlogp, zstate, Z_HALT) != 0) 985c5cd6260S return (-1); 986c5cd6260S 9870209230bSgjelinek if (vplat_teardown(zlogp, unmount_cmd, rebooting) != 0) { 9887c478bd9Sstevel@tonic-gate if (!bringup_failure_recovery) 9897c478bd9Sstevel@tonic-gate zerror(zlogp, B_FALSE, "unable to destroy zone"); 9907c478bd9Sstevel@tonic-gate return (-1); 9917c478bd9Sstevel@tonic-gate } 9927c478bd9Sstevel@tonic-gate 9937c478bd9Sstevel@tonic-gate if ((err = zonecfg_destroy_snapshot(zone_name)) != Z_OK) 9947c478bd9Sstevel@tonic-gate zerror(zlogp, B_FALSE, "destroying snapshot: %s", 9957c478bd9Sstevel@tonic-gate zonecfg_strerror(err)); 9967c478bd9Sstevel@tonic-gate 997c5cd6260S if (brand_poststatechg(zlogp, zstate, Z_HALT) != 0) 998c5cd6260S return (-1); 999c5cd6260S 10007c478bd9Sstevel@tonic-gate return (0); 10017c478bd9Sstevel@tonic-gate } 10027c478bd9Sstevel@tonic-gate 10033c7284bdSAlexander Eremin static int 10043c7284bdSAlexander Eremin zone_graceful_shutdown(zlog_t *zlogp) 10053c7284bdSAlexander Eremin { 10063c7284bdSAlexander Eremin zoneid_t zoneid; 10073c7284bdSAlexander Eremin pid_t child; 10083c7284bdSAlexander Eremin char cmdbuf[MAXPATHLEN]; 10093c7284bdSAlexander Eremin brand_handle_t bh = NULL; 10103c7284bdSAlexander Eremin char zpath[MAXPATHLEN]; 10113c7284bdSAlexander Eremin ctid_t ct; 10123c7284bdSAlexander Eremin int tmpl_fd; 10133c7284bdSAlexander Eremin int child_status; 10143c7284bdSAlexander Eremin 10153c7284bdSAlexander Eremin if (shutdown_in_progress) { 10163c7284bdSAlexander Eremin zerror(zlogp, B_FALSE, "shutdown already in progress"); 10173c7284bdSAlexander Eremin return (-1); 10183c7284bdSAlexander Eremin } 10193c7284bdSAlexander Eremin 10203c7284bdSAlexander Eremin if ((zoneid = getzoneidbyname(zone_name)) == -1) { 10213c7284bdSAlexander Eremin zerror(zlogp, B_TRUE, "unable to get zoneid"); 10223c7284bdSAlexander Eremin return (-1); 10233c7284bdSAlexander Eremin } 10243c7284bdSAlexander Eremin 10253c7284bdSAlexander Eremin /* Get a handle to the brand info for this zone */ 10263c7284bdSAlexander Eremin if ((bh = brand_open(brand_name)) == NULL) { 10273c7284bdSAlexander Eremin zerror(zlogp, B_FALSE, "unable to determine zone brand"); 10283c7284bdSAlexander Eremin return (-1); 10293c7284bdSAlexander Eremin } 10303c7284bdSAlexander Eremin 10313c7284bdSAlexander Eremin if (zone_get_zonepath(zone_name, zpath, sizeof (zpath)) != Z_OK) { 10323c7284bdSAlexander Eremin zerror(zlogp, B_FALSE, "unable to determine zone path"); 10333c7284bdSAlexander Eremin brand_close(bh); 10343c7284bdSAlexander Eremin return (-1); 10353c7284bdSAlexander Eremin } 10363c7284bdSAlexander Eremin 10373c7284bdSAlexander Eremin /* 10383c7284bdSAlexander Eremin * If there is a brand 'shutdown' callback, execute it now to give the 10393c7284bdSAlexander Eremin * brand a chance to cleanup any custom configuration. 10403c7284bdSAlexander Eremin */ 10413c7284bdSAlexander Eremin (void) strcpy(cmdbuf, EXEC_PREFIX); 10423c7284bdSAlexander Eremin if (brand_get_shutdown(bh, zone_name, zpath, cmdbuf + EXEC_LEN, 10433c7284bdSAlexander Eremin sizeof (cmdbuf) - EXEC_LEN) != 0 || strlen(cmdbuf) <= EXEC_LEN) { 10443c7284bdSAlexander Eremin (void) strcat(cmdbuf, SHUTDOWN_DEFAULT); 10453c7284bdSAlexander Eremin } 10463c7284bdSAlexander Eremin brand_close(bh); 10473c7284bdSAlexander Eremin 10483c7284bdSAlexander Eremin if ((tmpl_fd = init_template()) == -1) { 10493c7284bdSAlexander Eremin zerror(zlogp, B_TRUE, "failed to create contract"); 10503c7284bdSAlexander Eremin return (-1); 10513c7284bdSAlexander Eremin } 10523c7284bdSAlexander Eremin 10533c7284bdSAlexander Eremin if ((child = fork()) == -1) { 10543c7284bdSAlexander Eremin (void) ct_tmpl_clear(tmpl_fd); 10553c7284bdSAlexander Eremin (void) close(tmpl_fd); 10563c7284bdSAlexander Eremin zerror(zlogp, B_TRUE, "failed to fork"); 10573c7284bdSAlexander Eremin return (-1); 10583c7284bdSAlexander Eremin } else if (child == 0) { 10593c7284bdSAlexander Eremin (void) ct_tmpl_clear(tmpl_fd); 10603c7284bdSAlexander Eremin if (zone_enter(zoneid) == -1) { 10613c7284bdSAlexander Eremin _exit(errno); 10623c7284bdSAlexander Eremin } 10633c7284bdSAlexander Eremin _exit(execl("/bin/sh", "sh", "-c", cmdbuf, (char *)NULL)); 10643c7284bdSAlexander Eremin } 10653c7284bdSAlexander Eremin 10663c7284bdSAlexander Eremin if (contract_latest(&ct) == -1) 10673c7284bdSAlexander Eremin ct = -1; 10683c7284bdSAlexander Eremin (void) ct_tmpl_clear(tmpl_fd); 10693c7284bdSAlexander Eremin (void) close(tmpl_fd); 10703c7284bdSAlexander Eremin 10713c7284bdSAlexander Eremin if (waitpid(child, &child_status, 0) != child) { 10723c7284bdSAlexander Eremin /* unexpected: we must have been signalled */ 10733c7284bdSAlexander Eremin (void) contract_abandon_id(ct); 10743c7284bdSAlexander Eremin return (-1); 10753c7284bdSAlexander Eremin } 10763c7284bdSAlexander Eremin 10773c7284bdSAlexander Eremin (void) contract_abandon_id(ct); 10783c7284bdSAlexander Eremin if (WEXITSTATUS(child_status) != 0) { 10793c7284bdSAlexander Eremin errno = WEXITSTATUS(child_status); 10803c7284bdSAlexander Eremin zerror(zlogp, B_FALSE, "unable to shutdown zone"); 10813c7284bdSAlexander Eremin return (-1); 10823c7284bdSAlexander Eremin } 10833c7284bdSAlexander Eremin 10843c7284bdSAlexander Eremin shutdown_in_progress = B_TRUE; 10853c7284bdSAlexander Eremin 10863c7284bdSAlexander Eremin return (0); 10873c7284bdSAlexander Eremin } 10883c7284bdSAlexander Eremin 10893c7284bdSAlexander Eremin static int 10903c7284bdSAlexander Eremin zone_wait_shutdown(zlog_t *zlogp) 10913c7284bdSAlexander Eremin { 10923c7284bdSAlexander Eremin zone_state_t zstate; 10933c7284bdSAlexander Eremin uint64_t *tm = NULL; 10943c7284bdSAlexander Eremin scf_simple_prop_t *prop = NULL; 10953c7284bdSAlexander Eremin int timeout; 10963c7284bdSAlexander Eremin int tries; 10973c7284bdSAlexander Eremin int rc = -1; 10983c7284bdSAlexander Eremin 10993c7284bdSAlexander Eremin /* Get default stop timeout from SMF framework */ 11003c7284bdSAlexander Eremin timeout = SHUTDOWN_WAIT; 11013c7284bdSAlexander Eremin if ((prop = scf_simple_prop_get(NULL, SHUTDOWN_FMRI, "stop", 11023c7284bdSAlexander Eremin SCF_PROPERTY_TIMEOUT)) != NULL) { 11033c7284bdSAlexander Eremin if ((tm = scf_simple_prop_next_count(prop)) != NULL) { 11043c7284bdSAlexander Eremin if (tm != 0) 11053c7284bdSAlexander Eremin timeout = *tm; 11063c7284bdSAlexander Eremin } 11073c7284bdSAlexander Eremin scf_simple_prop_free(prop); 11083c7284bdSAlexander Eremin } 11093c7284bdSAlexander Eremin 11103c7284bdSAlexander Eremin /* allow time for zone to shutdown cleanly */ 11113c7284bdSAlexander Eremin for (tries = 0; tries < timeout; tries ++) { 11123c7284bdSAlexander Eremin (void) sleep(1); 11133c7284bdSAlexander Eremin if (zone_get_state(zone_name, &zstate) == Z_OK && 11143c7284bdSAlexander Eremin zstate == ZONE_STATE_INSTALLED) { 11153c7284bdSAlexander Eremin rc = 0; 11163c7284bdSAlexander Eremin break; 11173c7284bdSAlexander Eremin } 11183c7284bdSAlexander Eremin } 11193c7284bdSAlexander Eremin 11203c7284bdSAlexander Eremin if (rc != 0) 11213c7284bdSAlexander Eremin zerror(zlogp, B_FALSE, "unable to shutdown zone"); 11223c7284bdSAlexander Eremin 11233c7284bdSAlexander Eremin shutdown_in_progress = B_FALSE; 11243c7284bdSAlexander Eremin 11253c7284bdSAlexander Eremin return (rc); 11263c7284bdSAlexander Eremin } 11273c7284bdSAlexander Eremin 11283c7284bdSAlexander Eremin 11293c7284bdSAlexander Eremin 11307c478bd9Sstevel@tonic-gate /* 11317c478bd9Sstevel@tonic-gate * Generate AUE_zone_state for a command that boots a zone. 11327c478bd9Sstevel@tonic-gate */ 11337c478bd9Sstevel@tonic-gate static void 11347c478bd9Sstevel@tonic-gate audit_put_record(zlog_t *zlogp, ucred_t *uc, int return_val, 11357c478bd9Sstevel@tonic-gate char *new_state) 11367c478bd9Sstevel@tonic-gate { 11377c478bd9Sstevel@tonic-gate adt_session_data_t *ah; 11387c478bd9Sstevel@tonic-gate adt_event_data_t *event; 11397c478bd9Sstevel@tonic-gate int pass_fail, fail_reason; 11407c478bd9Sstevel@tonic-gate 11417c478bd9Sstevel@tonic-gate if (!adt_audit_enabled()) 11427c478bd9Sstevel@tonic-gate return; 11437c478bd9Sstevel@tonic-gate 11447c478bd9Sstevel@tonic-gate if (return_val == 0) { 11457c478bd9Sstevel@tonic-gate pass_fail = ADT_SUCCESS; 11467c478bd9Sstevel@tonic-gate fail_reason = ADT_SUCCESS; 11477c478bd9Sstevel@tonic-gate } else { 11487c478bd9Sstevel@tonic-gate pass_fail = ADT_FAILURE; 11497c478bd9Sstevel@tonic-gate fail_reason = ADT_FAIL_VALUE_PROGRAM; 11507c478bd9Sstevel@tonic-gate } 11517c478bd9Sstevel@tonic-gate 11527c478bd9Sstevel@tonic-gate if (adt_start_session(&ah, NULL, 0)) { 11537c478bd9Sstevel@tonic-gate zerror(zlogp, B_TRUE, gettext("audit failure.")); 11547c478bd9Sstevel@tonic-gate return; 11557c478bd9Sstevel@tonic-gate } 11567c478bd9Sstevel@tonic-gate if (adt_set_from_ucred(ah, uc, ADT_NEW)) { 11577c478bd9Sstevel@tonic-gate zerror(zlogp, B_TRUE, gettext("audit failure.")); 11587c478bd9Sstevel@tonic-gate (void) adt_end_session(ah); 11597c478bd9Sstevel@tonic-gate return; 11607c478bd9Sstevel@tonic-gate } 11617c478bd9Sstevel@tonic-gate 11627c478bd9Sstevel@tonic-gate event = adt_alloc_event(ah, ADT_zone_state); 11637c478bd9Sstevel@tonic-gate if (event == NULL) { 11647c478bd9Sstevel@tonic-gate zerror(zlogp, B_TRUE, gettext("audit failure.")); 11657c478bd9Sstevel@tonic-gate (void) adt_end_session(ah); 11667c478bd9Sstevel@tonic-gate return; 11677c478bd9Sstevel@tonic-gate } 11687c478bd9Sstevel@tonic-gate event->adt_zone_state.zonename = zone_name; 11697c478bd9Sstevel@tonic-gate event->adt_zone_state.new_state = new_state; 11707c478bd9Sstevel@tonic-gate 11717c478bd9Sstevel@tonic-gate if (adt_put_event(event, pass_fail, fail_reason)) 11727c478bd9Sstevel@tonic-gate zerror(zlogp, B_TRUE, gettext("audit failure.")); 11737c478bd9Sstevel@tonic-gate 11747c478bd9Sstevel@tonic-gate adt_free_event(event); 11757c478bd9Sstevel@tonic-gate 11767c478bd9Sstevel@tonic-gate (void) adt_end_session(ah); 11777c478bd9Sstevel@tonic-gate } 11787c478bd9Sstevel@tonic-gate 11797c478bd9Sstevel@tonic-gate /* 11807c478bd9Sstevel@tonic-gate * The main routine for the door server that deals with zone state transitions. 11817c478bd9Sstevel@tonic-gate */ 11827c478bd9Sstevel@tonic-gate /* ARGSUSED */ 11837c478bd9Sstevel@tonic-gate static void 11847c478bd9Sstevel@tonic-gate server(void *cookie, char *args, size_t alen, door_desc_t *dp, 11857c478bd9Sstevel@tonic-gate uint_t n_desc) 11867c478bd9Sstevel@tonic-gate { 11877c478bd9Sstevel@tonic-gate ucred_t *uc = NULL; 11887c478bd9Sstevel@tonic-gate const priv_set_t *eset; 11897c478bd9Sstevel@tonic-gate 11907c478bd9Sstevel@tonic-gate zone_state_t zstate; 11917c478bd9Sstevel@tonic-gate zone_cmd_t cmd; 11927c478bd9Sstevel@tonic-gate zone_cmd_arg_t *zargp; 11937c478bd9Sstevel@tonic-gate 11947c478bd9Sstevel@tonic-gate boolean_t kernelcall; 11957c478bd9Sstevel@tonic-gate 11967c478bd9Sstevel@tonic-gate int rval = -1; 11977c478bd9Sstevel@tonic-gate uint64_t uniqid; 11987c478bd9Sstevel@tonic-gate zoneid_t zoneid = -1; 11997c478bd9Sstevel@tonic-gate zlog_t zlog; 12007c478bd9Sstevel@tonic-gate zlog_t *zlogp; 12017c478bd9Sstevel@tonic-gate zone_cmd_rval_t *rvalp; 12027c478bd9Sstevel@tonic-gate size_t rlen = getpagesize(); /* conservative */ 12039acbbeafSnn35248 fs_callback_t cb; 1204123807fbSedp brand_handle_t bh; 12053c7284bdSAlexander Eremin boolean_t wait_shut = B_FALSE; 12067c478bd9Sstevel@tonic-gate 12077c478bd9Sstevel@tonic-gate /* LINTED E_BAD_PTR_CAST_ALIGN */ 12087c478bd9Sstevel@tonic-gate zargp = (zone_cmd_arg_t *)args; 12097c478bd9Sstevel@tonic-gate 12107c478bd9Sstevel@tonic-gate /* 12117c478bd9Sstevel@tonic-gate * When we get the door unref message, we've fdetach'd the door, and 12127c478bd9Sstevel@tonic-gate * it is time for us to shut down zoneadmd. 12137c478bd9Sstevel@tonic-gate */ 12147c478bd9Sstevel@tonic-gate if (zargp == DOOR_UNREF_DATA) { 12157c478bd9Sstevel@tonic-gate /* 12167c478bd9Sstevel@tonic-gate * See comment at end of main() for info on the last rites. 12177c478bd9Sstevel@tonic-gate */ 12187c478bd9Sstevel@tonic-gate exit(0); 12197c478bd9Sstevel@tonic-gate } 12207c478bd9Sstevel@tonic-gate 12217c478bd9Sstevel@tonic-gate if (zargp == NULL) { 12227c478bd9Sstevel@tonic-gate (void) door_return(NULL, 0, 0, 0); 12237c478bd9Sstevel@tonic-gate } 12247c478bd9Sstevel@tonic-gate 12257c478bd9Sstevel@tonic-gate rvalp = alloca(rlen); 12267c478bd9Sstevel@tonic-gate bzero(rvalp, rlen); 12277c478bd9Sstevel@tonic-gate zlog.logfile = NULL; 12287c478bd9Sstevel@tonic-gate zlog.buflen = zlog.loglen = rlen - sizeof (zone_cmd_rval_t) + 1; 12297c478bd9Sstevel@tonic-gate zlog.buf = rvalp->errbuf; 12307c478bd9Sstevel@tonic-gate zlog.log = zlog.buf; 12317c478bd9Sstevel@tonic-gate /* defer initialization of zlog.locale until after credential check */ 12327c478bd9Sstevel@tonic-gate zlogp = &zlog; 12337c478bd9Sstevel@tonic-gate 12347c478bd9Sstevel@tonic-gate if (alen != sizeof (zone_cmd_arg_t)) { 12357c478bd9Sstevel@tonic-gate /* 12367c478bd9Sstevel@tonic-gate * This really shouldn't be happening. 12377c478bd9Sstevel@tonic-gate */ 12383f2f09c1Sdp zerror(&logsys, B_FALSE, "argument size (%d bytes) " 12393f2f09c1Sdp "unexpected (expected %d bytes)", alen, 12403f2f09c1Sdp sizeof (zone_cmd_arg_t)); 12417c478bd9Sstevel@tonic-gate goto out; 12427c478bd9Sstevel@tonic-gate } 12437c478bd9Sstevel@tonic-gate cmd = zargp->cmd; 12447c478bd9Sstevel@tonic-gate 12457c478bd9Sstevel@tonic-gate if (door_ucred(&uc) != 0) { 12467c478bd9Sstevel@tonic-gate zerror(&logsys, B_TRUE, "door_ucred"); 12477c478bd9Sstevel@tonic-gate goto out; 12487c478bd9Sstevel@tonic-gate } 12497c478bd9Sstevel@tonic-gate eset = ucred_getprivset(uc, PRIV_EFFECTIVE); 12507c478bd9Sstevel@tonic-gate if (ucred_getzoneid(uc) != GLOBAL_ZONEID || 12517c478bd9Sstevel@tonic-gate (eset != NULL ? !priv_ismember(eset, PRIV_SYS_CONFIG) : 12527c478bd9Sstevel@tonic-gate ucred_geteuid(uc) != 0)) { 12537c478bd9Sstevel@tonic-gate zerror(&logsys, B_FALSE, "insufficient privileges"); 12547c478bd9Sstevel@tonic-gate goto out; 12557c478bd9Sstevel@tonic-gate } 12567c478bd9Sstevel@tonic-gate 12577c478bd9Sstevel@tonic-gate kernelcall = ucred_getpid(uc) == 0; 12587c478bd9Sstevel@tonic-gate 12597c478bd9Sstevel@tonic-gate /* 12607c478bd9Sstevel@tonic-gate * This is safe because we only use a zlog_t throughout the 12617c478bd9Sstevel@tonic-gate * duration of a door call; i.e., by the time the pointer 12627c478bd9Sstevel@tonic-gate * might become invalid, the door call would be over. 12637c478bd9Sstevel@tonic-gate */ 12647c478bd9Sstevel@tonic-gate zlog.locale = kernelcall ? DEFAULT_LOCALE : zargp->locale; 12657c478bd9Sstevel@tonic-gate 12667c478bd9Sstevel@tonic-gate (void) mutex_lock(&lock); 12677c478bd9Sstevel@tonic-gate 12687c478bd9Sstevel@tonic-gate /* 12697c478bd9Sstevel@tonic-gate * Once we start to really die off, we don't want more connections. 12707c478bd9Sstevel@tonic-gate */ 12717c478bd9Sstevel@tonic-gate if (in_death_throes) { 12727c478bd9Sstevel@tonic-gate (void) mutex_unlock(&lock); 12737c478bd9Sstevel@tonic-gate ucred_free(uc); 12747c478bd9Sstevel@tonic-gate (void) door_return(NULL, 0, 0, 0); 12757c478bd9Sstevel@tonic-gate thr_exit(NULL); 12767c478bd9Sstevel@tonic-gate } 12777c478bd9Sstevel@tonic-gate 12787c478bd9Sstevel@tonic-gate /* 12797c478bd9Sstevel@tonic-gate * Check for validity of command. 12807c478bd9Sstevel@tonic-gate */ 12819acbbeafSnn35248 if (cmd != Z_READY && cmd != Z_BOOT && cmd != Z_FORCEBOOT && 12823c7284bdSAlexander Eremin cmd != Z_REBOOT && cmd != Z_SHUTDOWN && cmd != Z_HALT && 12833c7284bdSAlexander Eremin cmd != Z_NOTE_UNINSTALLING && cmd != Z_MOUNT && 12843c7284bdSAlexander Eremin cmd != Z_FORCEMOUNT && cmd != Z_UNMOUNT) { 1285108322fbScarlsonj zerror(&logsys, B_FALSE, "invalid command %d", (int)cmd); 12867c478bd9Sstevel@tonic-gate goto out; 12877c478bd9Sstevel@tonic-gate } 12887c478bd9Sstevel@tonic-gate 12897c478bd9Sstevel@tonic-gate if (kernelcall && (cmd != Z_HALT && cmd != Z_REBOOT)) { 12907c478bd9Sstevel@tonic-gate /* 12917c478bd9Sstevel@tonic-gate * Can't happen 12927c478bd9Sstevel@tonic-gate */ 12937c478bd9Sstevel@tonic-gate zerror(&logsys, B_FALSE, "received unexpected kernel upcall %d", 12947c478bd9Sstevel@tonic-gate cmd); 12957c478bd9Sstevel@tonic-gate goto out; 12967c478bd9Sstevel@tonic-gate } 12977c478bd9Sstevel@tonic-gate /* 12987c478bd9Sstevel@tonic-gate * We ignore the possibility of someone calling zone_create(2) 12997c478bd9Sstevel@tonic-gate * explicitly; all requests must come through zoneadmd. 13007c478bd9Sstevel@tonic-gate */ 13017c478bd9Sstevel@tonic-gate if (zone_get_state(zone_name, &zstate) != Z_OK) { 13027c478bd9Sstevel@tonic-gate /* 13037c478bd9Sstevel@tonic-gate * Something terribly wrong happened 13047c478bd9Sstevel@tonic-gate */ 13057c478bd9Sstevel@tonic-gate zerror(&logsys, B_FALSE, "unable to determine state of zone"); 13067c478bd9Sstevel@tonic-gate goto out; 13077c478bd9Sstevel@tonic-gate } 13087c478bd9Sstevel@tonic-gate 13097c478bd9Sstevel@tonic-gate if (kernelcall) { 13107c478bd9Sstevel@tonic-gate /* 13117c478bd9Sstevel@tonic-gate * Kernel-initiated requests may lose their validity if the 13127c478bd9Sstevel@tonic-gate * zone_t the kernel was referring to has gone away. 13137c478bd9Sstevel@tonic-gate */ 13147c478bd9Sstevel@tonic-gate if ((zoneid = getzoneidbyname(zone_name)) == -1 || 13157c478bd9Sstevel@tonic-gate zone_getattr(zoneid, ZONE_ATTR_UNIQID, &uniqid, 13167c478bd9Sstevel@tonic-gate sizeof (uniqid)) == -1 || uniqid != zargp->uniqid) { 13177c478bd9Sstevel@tonic-gate /* 13187c478bd9Sstevel@tonic-gate * We're not talking about the same zone. The request 13197c478bd9Sstevel@tonic-gate * must have arrived too late. Return error. 13207c478bd9Sstevel@tonic-gate */ 13217c478bd9Sstevel@tonic-gate rval = -1; 13227c478bd9Sstevel@tonic-gate goto out; 13237c478bd9Sstevel@tonic-gate } 13247c478bd9Sstevel@tonic-gate zlogp = &logsys; /* Log errors to syslog */ 13257c478bd9Sstevel@tonic-gate } 13267c478bd9Sstevel@tonic-gate 13279acbbeafSnn35248 /* 13289acbbeafSnn35248 * If we are being asked to forcibly mount or boot a zone, we 13299acbbeafSnn35248 * pretend that an INCOMPLETE zone is actually INSTALLED. 13309acbbeafSnn35248 */ 13319acbbeafSnn35248 if (zstate == ZONE_STATE_INCOMPLETE && 13329acbbeafSnn35248 (cmd == Z_FORCEBOOT || cmd == Z_FORCEMOUNT)) 13339acbbeafSnn35248 zstate = ZONE_STATE_INSTALLED; 13349acbbeafSnn35248 13357c478bd9Sstevel@tonic-gate switch (zstate) { 13367c478bd9Sstevel@tonic-gate case ZONE_STATE_CONFIGURED: 13377c478bd9Sstevel@tonic-gate case ZONE_STATE_INCOMPLETE: 13387c478bd9Sstevel@tonic-gate /* 13397c478bd9Sstevel@tonic-gate * Not our area of expertise; we just print a nice message 13407c478bd9Sstevel@tonic-gate * and die off. 13417c478bd9Sstevel@tonic-gate */ 13427c478bd9Sstevel@tonic-gate zerror(zlogp, B_FALSE, 13437c478bd9Sstevel@tonic-gate "%s operation is invalid for zones in state '%s'", 1344108322fbScarlsonj z_cmd_name(cmd), zone_state_str(zstate)); 13457c478bd9Sstevel@tonic-gate break; 13467c478bd9Sstevel@tonic-gate 13477c478bd9Sstevel@tonic-gate case ZONE_STATE_INSTALLED: 13487c478bd9Sstevel@tonic-gate switch (cmd) { 13497c478bd9Sstevel@tonic-gate case Z_READY: 1350c5cd6260S rval = zone_ready(zlogp, Z_MNT_BOOT, zstate); 13517c478bd9Sstevel@tonic-gate if (rval == 0) 13527c478bd9Sstevel@tonic-gate eventstream_write(Z_EVT_ZONE_READIED); 13537c478bd9Sstevel@tonic-gate break; 13547c478bd9Sstevel@tonic-gate case Z_BOOT: 13559acbbeafSnn35248 case Z_FORCEBOOT: 13567c478bd9Sstevel@tonic-gate eventstream_write(Z_EVT_ZONE_BOOTING); 1357c5cd6260S if ((rval = zone_ready(zlogp, Z_MNT_BOOT, zstate)) 1358c5cd6260S == 0) { 1359c5cd6260S rval = zone_bootup(zlogp, zargp->bootbuf, 1360c5cd6260S zstate); 1361c5cd6260S } 13627c478bd9Sstevel@tonic-gate audit_put_record(zlogp, uc, rval, "boot"); 13637c478bd9Sstevel@tonic-gate if (rval != 0) { 13647c478bd9Sstevel@tonic-gate bringup_failure_recovery = B_TRUE; 1365c5cd6260S (void) zone_halt(zlogp, B_FALSE, B_FALSE, 1366c5cd6260S zstate); 1367ffbafc53Scomay eventstream_write(Z_EVT_ZONE_BOOTFAILED); 13687c478bd9Sstevel@tonic-gate } 13697c478bd9Sstevel@tonic-gate break; 13703c7284bdSAlexander Eremin case Z_SHUTDOWN: 13717c478bd9Sstevel@tonic-gate case Z_HALT: 13727c478bd9Sstevel@tonic-gate if (kernelcall) /* Invalid; can't happen */ 13737c478bd9Sstevel@tonic-gate abort(); 13747c478bd9Sstevel@tonic-gate /* 13757c478bd9Sstevel@tonic-gate * We could have two clients racing to halt this 13767c478bd9Sstevel@tonic-gate * zone; the second client loses, but his request 13777c478bd9Sstevel@tonic-gate * doesn't fail, since the zone is now in the desired 13787c478bd9Sstevel@tonic-gate * state. 13797c478bd9Sstevel@tonic-gate */ 13807c478bd9Sstevel@tonic-gate zerror(zlogp, B_FALSE, "zone is already halted"); 13817c478bd9Sstevel@tonic-gate rval = 0; 13827c478bd9Sstevel@tonic-gate break; 13837c478bd9Sstevel@tonic-gate case Z_REBOOT: 13847c478bd9Sstevel@tonic-gate if (kernelcall) /* Invalid; can't happen */ 13857c478bd9Sstevel@tonic-gate abort(); 13867c478bd9Sstevel@tonic-gate zerror(zlogp, B_FALSE, "%s operation is invalid " 1387108322fbScarlsonj "for zones in state '%s'", z_cmd_name(cmd), 13887c478bd9Sstevel@tonic-gate zone_state_str(zstate)); 13897c478bd9Sstevel@tonic-gate rval = -1; 13907c478bd9Sstevel@tonic-gate break; 13917c478bd9Sstevel@tonic-gate case Z_NOTE_UNINSTALLING: 13927c478bd9Sstevel@tonic-gate if (kernelcall) /* Invalid; can't happen */ 13937c478bd9Sstevel@tonic-gate abort(); 13947c478bd9Sstevel@tonic-gate /* 13957c478bd9Sstevel@tonic-gate * Tell the console to print out a message about this. 13967c478bd9Sstevel@tonic-gate * Once it does, we will be in_death_throes. 13977c478bd9Sstevel@tonic-gate */ 13987c478bd9Sstevel@tonic-gate eventstream_write(Z_EVT_ZONE_UNINSTALLING); 13997c478bd9Sstevel@tonic-gate break; 1400108322fbScarlsonj case Z_MOUNT: 14019acbbeafSnn35248 case Z_FORCEMOUNT: 1402108322fbScarlsonj if (kernelcall) /* Invalid; can't happen */ 1403108322fbScarlsonj abort(); 14049a5d73e0SRic Aleshire if (!zone_isnative && !zone_iscluster && 14059a5d73e0SRic Aleshire !zone_islabeled) { 14060679f794S /* 14070679f794S * -U mounts the zone without lofs mounting 14080679f794S * zone file systems back into the scratch 14090679f794S * zone. This is required when mounting 14100679f794S * non-native branded zones. 14110679f794S */ 14120679f794S (void) strlcpy(zargp->bootbuf, "-U", 14130679f794S BOOTARGS_MAX); 1414ffbafc53Scomay } 1415ffbafc53Scomay 14166cfd72c6Sgjelinek rval = zone_ready(zlogp, 14176cfd72c6Sgjelinek strcmp(zargp->bootbuf, "-U") == 0 ? 1418c5cd6260S Z_MNT_UPDATE : Z_MNT_SCRATCH, zstate); 14199acbbeafSnn35248 if (rval != 0) 14209acbbeafSnn35248 break; 14219acbbeafSnn35248 14229acbbeafSnn35248 eventstream_write(Z_EVT_ZONE_READIED); 14239acbbeafSnn35248 14240679f794S /* 1425e5816e35SEdward Pilatowicz * Get a handle to the default brand info. 1426e5816e35SEdward Pilatowicz * We must always use the default brand file system 14270679f794S * list when mounting the zone. 14280679f794S */ 1429e5816e35SEdward Pilatowicz if ((bh = brand_open(default_brand)) == NULL) { 14309acbbeafSnn35248 rval = -1; 14319acbbeafSnn35248 break; 14329acbbeafSnn35248 } 14339acbbeafSnn35248 14349acbbeafSnn35248 /* 14359acbbeafSnn35248 * Get the list of filesystems to mount from 14369acbbeafSnn35248 * the brand configuration. These mounts are done 14379acbbeafSnn35248 * via a thread that will enter the zone, so they 14389acbbeafSnn35248 * are done from within the context of the zone. 14399acbbeafSnn35248 */ 14409acbbeafSnn35248 cb.zlogp = zlogp; 14419acbbeafSnn35248 cb.zoneid = zone_id; 1442910f48daSedp cb.mount_cmd = B_TRUE; 1443123807fbSedp rval = brand_platform_iter_mounts(bh, 14449acbbeafSnn35248 mount_early_fs, &cb); 14459acbbeafSnn35248 1446123807fbSedp brand_close(bh); 14479acbbeafSnn35248 1448108322fbScarlsonj /* 1449108322fbScarlsonj * Ordinarily, /dev/fd would be mounted inside the zone 1450108322fbScarlsonj * by svc:/system/filesystem/usr:default, but since 1451108322fbScarlsonj * we're not booting the zone, we need to do this 1452108322fbScarlsonj * manually. 1453108322fbScarlsonj */ 1454108322fbScarlsonj if (rval == 0) 14559acbbeafSnn35248 rval = mount_early_fs(&cb, 14569acbbeafSnn35248 "fd", "/dev/fd", "fd", NULL); 1457108322fbScarlsonj break; 1458108322fbScarlsonj case Z_UNMOUNT: 1459108322fbScarlsonj if (kernelcall) /* Invalid; can't happen */ 1460108322fbScarlsonj abort(); 1461108322fbScarlsonj zerror(zlogp, B_FALSE, "zone is already unmounted"); 1462108322fbScarlsonj rval = 0; 1463108322fbScarlsonj break; 14647c478bd9Sstevel@tonic-gate } 14657c478bd9Sstevel@tonic-gate break; 14667c478bd9Sstevel@tonic-gate 14677c478bd9Sstevel@tonic-gate case ZONE_STATE_READY: 14687c478bd9Sstevel@tonic-gate switch (cmd) { 14697c478bd9Sstevel@tonic-gate case Z_READY: 14707c478bd9Sstevel@tonic-gate /* 14717c478bd9Sstevel@tonic-gate * We could have two clients racing to ready this 14727c478bd9Sstevel@tonic-gate * zone; the second client loses, but his request 14737c478bd9Sstevel@tonic-gate * doesn't fail, since the zone is now in the desired 14747c478bd9Sstevel@tonic-gate * state. 14757c478bd9Sstevel@tonic-gate */ 14767c478bd9Sstevel@tonic-gate zerror(zlogp, B_FALSE, "zone is already ready"); 14777c478bd9Sstevel@tonic-gate rval = 0; 14787c478bd9Sstevel@tonic-gate break; 14797c478bd9Sstevel@tonic-gate case Z_BOOT: 14803f2f09c1Sdp (void) strlcpy(boot_args, zargp->bootbuf, 14813f2f09c1Sdp sizeof (boot_args)); 14827c478bd9Sstevel@tonic-gate eventstream_write(Z_EVT_ZONE_BOOTING); 1483c5cd6260S rval = zone_bootup(zlogp, zargp->bootbuf, zstate); 14847c478bd9Sstevel@tonic-gate audit_put_record(zlogp, uc, rval, "boot"); 14857c478bd9Sstevel@tonic-gate if (rval != 0) { 14867c478bd9Sstevel@tonic-gate bringup_failure_recovery = B_TRUE; 1487c5cd6260S (void) zone_halt(zlogp, B_FALSE, B_TRUE, 1488c5cd6260S zstate); 1489ffbafc53Scomay eventstream_write(Z_EVT_ZONE_BOOTFAILED); 14907c478bd9Sstevel@tonic-gate } 14913f2f09c1Sdp boot_args[0] = '\0'; 14927c478bd9Sstevel@tonic-gate break; 14937c478bd9Sstevel@tonic-gate case Z_HALT: 14947c478bd9Sstevel@tonic-gate if (kernelcall) /* Invalid; can't happen */ 14957c478bd9Sstevel@tonic-gate abort(); 1496c5cd6260S if ((rval = zone_halt(zlogp, B_FALSE, B_FALSE, zstate)) 1497c5cd6260S != 0) 14987c478bd9Sstevel@tonic-gate break; 14997c478bd9Sstevel@tonic-gate eventstream_write(Z_EVT_ZONE_HALTED); 15007c478bd9Sstevel@tonic-gate break; 15013c7284bdSAlexander Eremin case Z_SHUTDOWN: 15027c478bd9Sstevel@tonic-gate case Z_REBOOT: 1503108322fbScarlsonj case Z_NOTE_UNINSTALLING: 1504108322fbScarlsonj case Z_MOUNT: 1505108322fbScarlsonj case Z_UNMOUNT: 15067c478bd9Sstevel@tonic-gate if (kernelcall) /* Invalid; can't happen */ 15077c478bd9Sstevel@tonic-gate abort(); 15087c478bd9Sstevel@tonic-gate zerror(zlogp, B_FALSE, "%s operation is invalid " 1509108322fbScarlsonj "for zones in state '%s'", z_cmd_name(cmd), 15107c478bd9Sstevel@tonic-gate zone_state_str(zstate)); 15117c478bd9Sstevel@tonic-gate rval = -1; 15127c478bd9Sstevel@tonic-gate break; 1513108322fbScarlsonj } 1514108322fbScarlsonj break; 1515108322fbScarlsonj 1516108322fbScarlsonj case ZONE_STATE_MOUNTED: 1517108322fbScarlsonj switch (cmd) { 1518108322fbScarlsonj case Z_UNMOUNT: 15197c478bd9Sstevel@tonic-gate if (kernelcall) /* Invalid; can't happen */ 15207c478bd9Sstevel@tonic-gate abort(); 1521c5cd6260S rval = zone_halt(zlogp, B_TRUE, B_FALSE, zstate); 1522ffbafc53Scomay if (rval == 0) { 1523ffbafc53Scomay eventstream_write(Z_EVT_ZONE_HALTED); 1524108322fbScarlsonj (void) sema_post(&scratch_sem); 1525ffbafc53Scomay } 1526108322fbScarlsonj break; 1527108322fbScarlsonj default: 1528108322fbScarlsonj if (kernelcall) /* Invalid; can't happen */ 1529108322fbScarlsonj abort(); 1530108322fbScarlsonj zerror(zlogp, B_FALSE, "%s operation is invalid " 1531108322fbScarlsonj "for zones in state '%s'", z_cmd_name(cmd), 1532108322fbScarlsonj zone_state_str(zstate)); 15337c478bd9Sstevel@tonic-gate rval = -1; 15347c478bd9Sstevel@tonic-gate break; 15357c478bd9Sstevel@tonic-gate } 15367c478bd9Sstevel@tonic-gate break; 15377c478bd9Sstevel@tonic-gate 15387c478bd9Sstevel@tonic-gate case ZONE_STATE_RUNNING: 15397c478bd9Sstevel@tonic-gate case ZONE_STATE_SHUTTING_DOWN: 15407c478bd9Sstevel@tonic-gate case ZONE_STATE_DOWN: 15417c478bd9Sstevel@tonic-gate switch (cmd) { 15427c478bd9Sstevel@tonic-gate case Z_READY: 1543c5cd6260S if ((rval = zone_halt(zlogp, B_FALSE, B_TRUE, zstate)) 1544c5cd6260S != 0) 15457c478bd9Sstevel@tonic-gate break; 1546c5cd6260S if ((rval = zone_ready(zlogp, Z_MNT_BOOT, zstate)) == 0) 15477c478bd9Sstevel@tonic-gate eventstream_write(Z_EVT_ZONE_READIED); 1548ffbafc53Scomay else 1549ffbafc53Scomay eventstream_write(Z_EVT_ZONE_HALTED); 15507c478bd9Sstevel@tonic-gate break; 15517c478bd9Sstevel@tonic-gate case Z_BOOT: 15527c478bd9Sstevel@tonic-gate /* 15537c478bd9Sstevel@tonic-gate * We could have two clients racing to boot this 15547c478bd9Sstevel@tonic-gate * zone; the second client loses, but his request 15557c478bd9Sstevel@tonic-gate * doesn't fail, since the zone is now in the desired 15567c478bd9Sstevel@tonic-gate * state. 15577c478bd9Sstevel@tonic-gate */ 15587c478bd9Sstevel@tonic-gate zerror(zlogp, B_FALSE, "zone is already booted"); 15597c478bd9Sstevel@tonic-gate rval = 0; 15607c478bd9Sstevel@tonic-gate break; 15617c478bd9Sstevel@tonic-gate case Z_HALT: 1562c5cd6260S if ((rval = zone_halt(zlogp, B_FALSE, B_FALSE, zstate)) 1563c5cd6260S != 0) 15647c478bd9Sstevel@tonic-gate break; 15657c478bd9Sstevel@tonic-gate eventstream_write(Z_EVT_ZONE_HALTED); 15667c478bd9Sstevel@tonic-gate break; 15677c478bd9Sstevel@tonic-gate case Z_REBOOT: 15683f2f09c1Sdp (void) strlcpy(boot_args, zargp->bootbuf, 15693f2f09c1Sdp sizeof (boot_args)); 15707c478bd9Sstevel@tonic-gate eventstream_write(Z_EVT_ZONE_REBOOTING); 1571c5cd6260S if ((rval = zone_halt(zlogp, B_FALSE, B_TRUE, zstate)) 1572c5cd6260S != 0) { 1573ffbafc53Scomay eventstream_write(Z_EVT_ZONE_BOOTFAILED); 15743f2f09c1Sdp boot_args[0] = '\0'; 15757c478bd9Sstevel@tonic-gate break; 1576ffbafc53Scomay } 1577c5cd6260S if ((rval = zone_ready(zlogp, Z_MNT_BOOT, zstate)) 1578c5cd6260S != 0) { 1579ffbafc53Scomay eventstream_write(Z_EVT_ZONE_BOOTFAILED); 15803f2f09c1Sdp boot_args[0] = '\0'; 1581ffbafc53Scomay break; 1582ffbafc53Scomay } 1583c5cd6260S rval = zone_bootup(zlogp, zargp->bootbuf, zstate); 15847c478bd9Sstevel@tonic-gate audit_put_record(zlogp, uc, rval, "reboot"); 1585ffbafc53Scomay if (rval != 0) { 1586c5cd6260S (void) zone_halt(zlogp, B_FALSE, B_TRUE, 1587c5cd6260S zstate); 1588ffbafc53Scomay eventstream_write(Z_EVT_ZONE_BOOTFAILED); 15897c478bd9Sstevel@tonic-gate } 15903f2f09c1Sdp boot_args[0] = '\0'; 15917c478bd9Sstevel@tonic-gate break; 15923c7284bdSAlexander Eremin case Z_SHUTDOWN: 15933c7284bdSAlexander Eremin if ((rval = zone_graceful_shutdown(zlogp)) == 0) { 15943c7284bdSAlexander Eremin wait_shut = B_TRUE; 15953c7284bdSAlexander Eremin } 15963c7284bdSAlexander Eremin break; 15977c478bd9Sstevel@tonic-gate case Z_NOTE_UNINSTALLING: 1598108322fbScarlsonj case Z_MOUNT: 1599108322fbScarlsonj case Z_UNMOUNT: 1600108322fbScarlsonj zerror(zlogp, B_FALSE, "%s operation is invalid " 1601108322fbScarlsonj "for zones in state '%s'", z_cmd_name(cmd), 1602108322fbScarlsonj zone_state_str(zstate)); 16037c478bd9Sstevel@tonic-gate rval = -1; 16047c478bd9Sstevel@tonic-gate break; 16057c478bd9Sstevel@tonic-gate } 16067c478bd9Sstevel@tonic-gate break; 16077c478bd9Sstevel@tonic-gate default: 16087c478bd9Sstevel@tonic-gate abort(); 16097c478bd9Sstevel@tonic-gate } 16107c478bd9Sstevel@tonic-gate 16117c478bd9Sstevel@tonic-gate /* 16127c478bd9Sstevel@tonic-gate * Because the state of the zone may have changed, we make sure 16137c478bd9Sstevel@tonic-gate * to wake the console poller, which is in charge of initiating 16147c478bd9Sstevel@tonic-gate * the shutdown procedure as necessary. 16157c478bd9Sstevel@tonic-gate */ 16167c478bd9Sstevel@tonic-gate eventstream_write(Z_EVT_NULL); 16177c478bd9Sstevel@tonic-gate 16187c478bd9Sstevel@tonic-gate out: 16197c478bd9Sstevel@tonic-gate (void) mutex_unlock(&lock); 16203c7284bdSAlexander Eremin 16213c7284bdSAlexander Eremin /* Wait for the Z_SHUTDOWN commands to complete */ 16223c7284bdSAlexander Eremin if (wait_shut) 16233c7284bdSAlexander Eremin rval = zone_wait_shutdown(zlogp); 16243c7284bdSAlexander Eremin 16257c478bd9Sstevel@tonic-gate if (kernelcall) { 16267c478bd9Sstevel@tonic-gate rvalp = NULL; 16277c478bd9Sstevel@tonic-gate rlen = 0; 16287c478bd9Sstevel@tonic-gate } else { 16297c478bd9Sstevel@tonic-gate rvalp->rval = rval; 16307c478bd9Sstevel@tonic-gate } 16317c478bd9Sstevel@tonic-gate if (uc != NULL) 16327c478bd9Sstevel@tonic-gate ucred_free(uc); 16337c478bd9Sstevel@tonic-gate (void) door_return((char *)rvalp, rlen, NULL, 0); 16347c478bd9Sstevel@tonic-gate thr_exit(NULL); 16357c478bd9Sstevel@tonic-gate } 16367c478bd9Sstevel@tonic-gate 16377c478bd9Sstevel@tonic-gate static int 16387c478bd9Sstevel@tonic-gate setup_door(zlog_t *zlogp) 16397c478bd9Sstevel@tonic-gate { 16407c478bd9Sstevel@tonic-gate if ((zone_door = door_create(server, NULL, 16417c478bd9Sstevel@tonic-gate DOOR_UNREF | DOOR_REFUSE_DESC | DOOR_NO_CANCEL)) < 0) { 16427c478bd9Sstevel@tonic-gate zerror(zlogp, B_TRUE, "%s failed", "door_create"); 16437c478bd9Sstevel@tonic-gate return (-1); 16447c478bd9Sstevel@tonic-gate } 16457c478bd9Sstevel@tonic-gate (void) fdetach(zone_door_path); 16467c478bd9Sstevel@tonic-gate 16477c478bd9Sstevel@tonic-gate if (fattach(zone_door, zone_door_path) != 0) { 16487c478bd9Sstevel@tonic-gate zerror(zlogp, B_TRUE, "fattach to %s failed", zone_door_path); 16497c478bd9Sstevel@tonic-gate (void) door_revoke(zone_door); 16507c478bd9Sstevel@tonic-gate (void) fdetach(zone_door_path); 16517c478bd9Sstevel@tonic-gate zone_door = -1; 16527c478bd9Sstevel@tonic-gate return (-1); 16537c478bd9Sstevel@tonic-gate } 16547c478bd9Sstevel@tonic-gate return (0); 16557c478bd9Sstevel@tonic-gate } 16567c478bd9Sstevel@tonic-gate 16577c478bd9Sstevel@tonic-gate /* 16587c478bd9Sstevel@tonic-gate * zoneadm(1m) will start zoneadmd if it thinks it isn't running; this 16597c478bd9Sstevel@tonic-gate * is where zoneadmd itself will check to see that another instance of 16607c478bd9Sstevel@tonic-gate * zoneadmd isn't already controlling this zone. 16617c478bd9Sstevel@tonic-gate * 16627c478bd9Sstevel@tonic-gate * The idea here is that we want to open the path to which we will 16637c478bd9Sstevel@tonic-gate * attach our door, lock it, and then make sure that no-one has beat us 16647c478bd9Sstevel@tonic-gate * to fattach(3c)ing onto it. 16657c478bd9Sstevel@tonic-gate * 16667c478bd9Sstevel@tonic-gate * fattach(3c) is really a mount, so there are actually two possible 16677c478bd9Sstevel@tonic-gate * vnodes we could be dealing with. Our strategy is as follows: 16687c478bd9Sstevel@tonic-gate * 16697c478bd9Sstevel@tonic-gate * - If the file we opened is a regular file (common case): 16707c478bd9Sstevel@tonic-gate * There is no fattach(3c)ed door, so we have a chance of becoming 16717c478bd9Sstevel@tonic-gate * the managing zoneadmd. We attempt to lock the file: if it is 16727c478bd9Sstevel@tonic-gate * already locked, that means someone else raced us here, so we 16737c478bd9Sstevel@tonic-gate * lose and give up. zoneadm(1m) will try to contact the zoneadmd 16747c478bd9Sstevel@tonic-gate * that beat us to it. 16757c478bd9Sstevel@tonic-gate * 16767c478bd9Sstevel@tonic-gate * - If the file we opened is a namefs file: 16777c478bd9Sstevel@tonic-gate * This means there is already an established door fattach(3c)'ed 16787c478bd9Sstevel@tonic-gate * to the rendezvous path. We've lost the race, so we give up. 16797c478bd9Sstevel@tonic-gate * Note that in this case we also try to grab the file lock, and 16807c478bd9Sstevel@tonic-gate * will succeed in acquiring it since the vnode locked by the 16817c478bd9Sstevel@tonic-gate * "winning" zoneadmd was a regular one, and the one we locked was 16827c478bd9Sstevel@tonic-gate * the fattach(3c)'ed door node. At any rate, no harm is done, and 16837c478bd9Sstevel@tonic-gate * we just return to zoneadm(1m) which knows to retry. 16847c478bd9Sstevel@tonic-gate */ 16857c478bd9Sstevel@tonic-gate static int 16867c478bd9Sstevel@tonic-gate make_daemon_exclusive(zlog_t *zlogp) 16877c478bd9Sstevel@tonic-gate { 16887c478bd9Sstevel@tonic-gate int doorfd = -1; 16897c478bd9Sstevel@tonic-gate int err, ret = -1; 16907c478bd9Sstevel@tonic-gate struct stat st; 16917c478bd9Sstevel@tonic-gate struct flock flock; 16927c478bd9Sstevel@tonic-gate zone_state_t zstate; 16937c478bd9Sstevel@tonic-gate 16947c478bd9Sstevel@tonic-gate top: 16957c478bd9Sstevel@tonic-gate if ((err = zone_get_state(zone_name, &zstate)) != Z_OK) { 16963f2f09c1Sdp zerror(zlogp, B_FALSE, "failed to get zone state: %s", 16977c478bd9Sstevel@tonic-gate zonecfg_strerror(err)); 16987c478bd9Sstevel@tonic-gate goto out; 16997c478bd9Sstevel@tonic-gate } 17007c478bd9Sstevel@tonic-gate if ((doorfd = open(zone_door_path, O_CREAT|O_RDWR, 17017c478bd9Sstevel@tonic-gate S_IREAD|S_IWRITE)) < 0) { 17027c478bd9Sstevel@tonic-gate zerror(zlogp, B_TRUE, "failed to open %s", zone_door_path); 17037c478bd9Sstevel@tonic-gate goto out; 17047c478bd9Sstevel@tonic-gate } 17057c478bd9Sstevel@tonic-gate if (fstat(doorfd, &st) < 0) { 17067c478bd9Sstevel@tonic-gate zerror(zlogp, B_TRUE, "failed to stat %s", zone_door_path); 17077c478bd9Sstevel@tonic-gate goto out; 17087c478bd9Sstevel@tonic-gate } 17097c478bd9Sstevel@tonic-gate /* 17107c478bd9Sstevel@tonic-gate * Lock the file to synchronize with other zoneadmd 17117c478bd9Sstevel@tonic-gate */ 17127c478bd9Sstevel@tonic-gate flock.l_type = F_WRLCK; 17137c478bd9Sstevel@tonic-gate flock.l_whence = SEEK_SET; 17147c478bd9Sstevel@tonic-gate flock.l_start = (off_t)0; 17157c478bd9Sstevel@tonic-gate flock.l_len = (off_t)0; 17167c478bd9Sstevel@tonic-gate if (fcntl(doorfd, F_SETLK, &flock) < 0) { 17177c478bd9Sstevel@tonic-gate /* 17187c478bd9Sstevel@tonic-gate * Someone else raced us here and grabbed the lock file 17197c478bd9Sstevel@tonic-gate * first. A warning here is inappropriate since nothing 17207c478bd9Sstevel@tonic-gate * went wrong. 17217c478bd9Sstevel@tonic-gate */ 17227c478bd9Sstevel@tonic-gate goto out; 17237c478bd9Sstevel@tonic-gate } 17247c478bd9Sstevel@tonic-gate 17257c478bd9Sstevel@tonic-gate if (strcmp(st.st_fstype, "namefs") == 0) { 17267c478bd9Sstevel@tonic-gate struct door_info info; 17277c478bd9Sstevel@tonic-gate 17287c478bd9Sstevel@tonic-gate /* 17297c478bd9Sstevel@tonic-gate * There is already something fattach()'ed to this file. 17307c478bd9Sstevel@tonic-gate * Lets see what the door is up to. 17317c478bd9Sstevel@tonic-gate */ 17327c478bd9Sstevel@tonic-gate if (door_info(doorfd, &info) == 0 && info.di_target != -1) { 17337c478bd9Sstevel@tonic-gate /* 17347c478bd9Sstevel@tonic-gate * Another zoneadmd process seems to be in 17357c478bd9Sstevel@tonic-gate * control of the situation and we don't need to 17367c478bd9Sstevel@tonic-gate * be here. A warning here is inappropriate 17377c478bd9Sstevel@tonic-gate * since nothing went wrong. 17387c478bd9Sstevel@tonic-gate * 17397c478bd9Sstevel@tonic-gate * If the door has been revoked, the zoneadmd 17407c478bd9Sstevel@tonic-gate * process currently managing the zone is going 17417c478bd9Sstevel@tonic-gate * away. We'll return control to zoneadm(1m) 17427c478bd9Sstevel@tonic-gate * which will try again (by which time zoneadmd 17437c478bd9Sstevel@tonic-gate * will hopefully have exited). 17447c478bd9Sstevel@tonic-gate */ 17457c478bd9Sstevel@tonic-gate goto out; 17467c478bd9Sstevel@tonic-gate } 17477c478bd9Sstevel@tonic-gate 17487c478bd9Sstevel@tonic-gate /* 17497c478bd9Sstevel@tonic-gate * If we got this far, there's a fattach(3c)'ed door 17507c478bd9Sstevel@tonic-gate * that belongs to a process that has exited, which can 17517c478bd9Sstevel@tonic-gate * happen if the previous zoneadmd died unexpectedly. 17527c478bd9Sstevel@tonic-gate * 17537c478bd9Sstevel@tonic-gate * Let user know that something is amiss, but that we can 17547c478bd9Sstevel@tonic-gate * recover; if the zone is in the installed state, then don't 17557c478bd9Sstevel@tonic-gate * message, since having a running zoneadmd isn't really 17567c478bd9Sstevel@tonic-gate * expected/needed. We want to keep occurences of this message 17577c478bd9Sstevel@tonic-gate * limited to times when zoneadmd is picking back up from a 17587c478bd9Sstevel@tonic-gate * zoneadmd that died while the zone was in some non-trivial 17597c478bd9Sstevel@tonic-gate * state. 17607c478bd9Sstevel@tonic-gate */ 17617c478bd9Sstevel@tonic-gate if (zstate > ZONE_STATE_INSTALLED) { 17627c478bd9Sstevel@tonic-gate zerror(zlogp, B_FALSE, 17637c478bd9Sstevel@tonic-gate "zone '%s': WARNING: zone is in state '%s', but " 17647c478bd9Sstevel@tonic-gate "zoneadmd does not appear to be available; " 17657c478bd9Sstevel@tonic-gate "restarted zoneadmd to recover.", 17667c478bd9Sstevel@tonic-gate zone_name, zone_state_str(zstate)); 17677c478bd9Sstevel@tonic-gate } 17687c478bd9Sstevel@tonic-gate 17697c478bd9Sstevel@tonic-gate (void) fdetach(zone_door_path); 17707c478bd9Sstevel@tonic-gate (void) close(doorfd); 17717c478bd9Sstevel@tonic-gate goto top; 17727c478bd9Sstevel@tonic-gate } 17737c478bd9Sstevel@tonic-gate ret = 0; 17747c478bd9Sstevel@tonic-gate out: 17757c478bd9Sstevel@tonic-gate (void) close(doorfd); 17767c478bd9Sstevel@tonic-gate return (ret); 17777c478bd9Sstevel@tonic-gate } 17787c478bd9Sstevel@tonic-gate 1779c5cd6260S /* 1780c5cd6260S * Setup the brand's pre and post state change callbacks, as well as the 1781c5cd6260S * query callback, if any of these exist. 1782c5cd6260S */ 1783c5cd6260S static int 1784c5cd6260S brand_callback_init(brand_handle_t bh, char *zone_name) 1785c5cd6260S { 1786c5cd6260S char zpath[MAXPATHLEN]; 1787c5cd6260S 1788c5cd6260S if (zone_get_zonepath(zone_name, zpath, sizeof (zpath)) != Z_OK) 1789c5cd6260S return (-1); 1790c5cd6260S 1791c5cd6260S (void) strlcpy(pre_statechg_hook, EXEC_PREFIX, 1792c5cd6260S sizeof (pre_statechg_hook)); 1793c5cd6260S 1794c5cd6260S if (brand_get_prestatechange(bh, zone_name, zpath, 1795c5cd6260S pre_statechg_hook + EXEC_LEN, 1796c5cd6260S sizeof (pre_statechg_hook) - EXEC_LEN) != 0) 1797c5cd6260S return (-1); 1798c5cd6260S 1799c5cd6260S if (strlen(pre_statechg_hook) <= EXEC_LEN) 1800c5cd6260S pre_statechg_hook[0] = '\0'; 1801c5cd6260S 1802c5cd6260S (void) strlcpy(post_statechg_hook, EXEC_PREFIX, 1803c5cd6260S sizeof (post_statechg_hook)); 1804c5cd6260S 1805c5cd6260S if (brand_get_poststatechange(bh, zone_name, zpath, 1806c5cd6260S post_statechg_hook + EXEC_LEN, 1807c5cd6260S sizeof (post_statechg_hook) - EXEC_LEN) != 0) 1808c5cd6260S return (-1); 1809c5cd6260S 1810c5cd6260S if (strlen(post_statechg_hook) <= EXEC_LEN) 1811c5cd6260S post_statechg_hook[0] = '\0'; 1812c5cd6260S 1813c5cd6260S (void) strlcpy(query_hook, EXEC_PREFIX, 1814c5cd6260S sizeof (query_hook)); 1815c5cd6260S 1816c5cd6260S if (brand_get_query(bh, zone_name, zpath, query_hook + EXEC_LEN, 1817c5cd6260S sizeof (query_hook) - EXEC_LEN) != 0) 1818c5cd6260S return (-1); 1819c5cd6260S 1820c5cd6260S if (strlen(query_hook) <= EXEC_LEN) 1821c5cd6260S query_hook[0] = '\0'; 1822c5cd6260S 1823c5cd6260S return (0); 1824c5cd6260S } 1825c5cd6260S 18267c478bd9Sstevel@tonic-gate int 18277c478bd9Sstevel@tonic-gate main(int argc, char *argv[]) 18287c478bd9Sstevel@tonic-gate { 18297c478bd9Sstevel@tonic-gate int opt; 18307c478bd9Sstevel@tonic-gate zoneid_t zid; 18317c478bd9Sstevel@tonic-gate priv_set_t *privset; 18327c478bd9Sstevel@tonic-gate zone_state_t zstate; 18337c478bd9Sstevel@tonic-gate char parents_locale[MAXPATHLEN]; 1834123807fbSedp brand_handle_t bh; 18357c478bd9Sstevel@tonic-gate int err; 18367c478bd9Sstevel@tonic-gate 18377c478bd9Sstevel@tonic-gate pid_t pid; 18387c478bd9Sstevel@tonic-gate sigset_t blockset; 18397c478bd9Sstevel@tonic-gate sigset_t block_cld; 18407c478bd9Sstevel@tonic-gate 18417c478bd9Sstevel@tonic-gate struct { 18427c478bd9Sstevel@tonic-gate sema_t sem; 18437c478bd9Sstevel@tonic-gate int status; 18447c478bd9Sstevel@tonic-gate zlog_t log; 18457c478bd9Sstevel@tonic-gate } *shstate; 18467c478bd9Sstevel@tonic-gate size_t shstatelen = getpagesize(); 18477c478bd9Sstevel@tonic-gate 18487c478bd9Sstevel@tonic-gate zlog_t errlog; 18497c478bd9Sstevel@tonic-gate zlog_t *zlogp; 18507c478bd9Sstevel@tonic-gate 18515cb4571dSdp int ctfd; 18525cb4571dSdp 18537c478bd9Sstevel@tonic-gate progname = get_execbasename(argv[0]); 18547c478bd9Sstevel@tonic-gate 18557c478bd9Sstevel@tonic-gate /* 18567c478bd9Sstevel@tonic-gate * Make sure stderr is unbuffered 18577c478bd9Sstevel@tonic-gate */ 18587c478bd9Sstevel@tonic-gate (void) setbuffer(stderr, NULL, 0); 18597c478bd9Sstevel@tonic-gate 18607c478bd9Sstevel@tonic-gate /* 18617c478bd9Sstevel@tonic-gate * Get out of the way of mounted filesystems, since we will daemonize 18627c478bd9Sstevel@tonic-gate * soon. 18637c478bd9Sstevel@tonic-gate */ 18647c478bd9Sstevel@tonic-gate (void) chdir("/"); 18657c478bd9Sstevel@tonic-gate 18667c478bd9Sstevel@tonic-gate /* 18677c478bd9Sstevel@tonic-gate * Use the default system umask per PSARC 1998/110 rather than 18687c478bd9Sstevel@tonic-gate * anything that may have been set by the caller. 18697c478bd9Sstevel@tonic-gate */ 18707c478bd9Sstevel@tonic-gate (void) umask(CMASK); 18717c478bd9Sstevel@tonic-gate 18727c478bd9Sstevel@tonic-gate /* 18737c478bd9Sstevel@tonic-gate * Initially we want to use our parent's locale. 18747c478bd9Sstevel@tonic-gate */ 18757c478bd9Sstevel@tonic-gate (void) setlocale(LC_ALL, ""); 18767c478bd9Sstevel@tonic-gate (void) textdomain(TEXT_DOMAIN); 18777c478bd9Sstevel@tonic-gate (void) strlcpy(parents_locale, setlocale(LC_MESSAGES, NULL), 18787c478bd9Sstevel@tonic-gate sizeof (parents_locale)); 18797c478bd9Sstevel@tonic-gate 18807c478bd9Sstevel@tonic-gate /* 18817c478bd9Sstevel@tonic-gate * This zlog_t is used for writing to stderr 18827c478bd9Sstevel@tonic-gate */ 18837c478bd9Sstevel@tonic-gate errlog.logfile = stderr; 18847c478bd9Sstevel@tonic-gate errlog.buflen = errlog.loglen = 0; 18857c478bd9Sstevel@tonic-gate errlog.buf = errlog.log = NULL; 18867c478bd9Sstevel@tonic-gate errlog.locale = parents_locale; 18877c478bd9Sstevel@tonic-gate 18887c478bd9Sstevel@tonic-gate /* 18897c478bd9Sstevel@tonic-gate * We start off writing to stderr until we're ready to daemonize. 18907c478bd9Sstevel@tonic-gate */ 18917c478bd9Sstevel@tonic-gate zlogp = &errlog; 18927c478bd9Sstevel@tonic-gate 18937c478bd9Sstevel@tonic-gate /* 18947c478bd9Sstevel@tonic-gate * Process options. 18957c478bd9Sstevel@tonic-gate */ 1896108322fbScarlsonj while ((opt = getopt(argc, argv, "R:z:")) != EOF) { 18977c478bd9Sstevel@tonic-gate switch (opt) { 1898108322fbScarlsonj case 'R': 1899108322fbScarlsonj zonecfg_set_root(optarg); 1900108322fbScarlsonj break; 19017c478bd9Sstevel@tonic-gate case 'z': 19027c478bd9Sstevel@tonic-gate zone_name = optarg; 19037c478bd9Sstevel@tonic-gate break; 19047c478bd9Sstevel@tonic-gate default: 19057c478bd9Sstevel@tonic-gate usage(); 19067c478bd9Sstevel@tonic-gate } 19077c478bd9Sstevel@tonic-gate } 19087c478bd9Sstevel@tonic-gate 19097c478bd9Sstevel@tonic-gate if (zone_name == NULL) 19107c478bd9Sstevel@tonic-gate usage(); 19117c478bd9Sstevel@tonic-gate 19127c478bd9Sstevel@tonic-gate /* 19137c478bd9Sstevel@tonic-gate * Because usage() prints directly to stderr, it has gettext() 19147c478bd9Sstevel@tonic-gate * wrapping, which depends on the locale. But since zerror() calls 19157c478bd9Sstevel@tonic-gate * localize() which tweaks the locale, it is not safe to call zerror() 19167c478bd9Sstevel@tonic-gate * until after the last call to usage(). Fortunately, the last call 19177c478bd9Sstevel@tonic-gate * to usage() is just above and the first call to zerror() is just 19187c478bd9Sstevel@tonic-gate * below. Don't mess this up. 19197c478bd9Sstevel@tonic-gate */ 19207c478bd9Sstevel@tonic-gate if (strcmp(zone_name, GLOBAL_ZONENAME) == 0) { 19217c478bd9Sstevel@tonic-gate zerror(zlogp, B_FALSE, "cannot manage the %s zone", 19227c478bd9Sstevel@tonic-gate GLOBAL_ZONENAME); 19237c478bd9Sstevel@tonic-gate return (1); 19247c478bd9Sstevel@tonic-gate } 19257c478bd9Sstevel@tonic-gate 19267c478bd9Sstevel@tonic-gate if (zone_get_id(zone_name, &zid) != 0) { 19273f2f09c1Sdp zerror(zlogp, B_FALSE, "could not manage %s: %s", zone_name, 19287c478bd9Sstevel@tonic-gate zonecfg_strerror(Z_NO_ZONE)); 19297c478bd9Sstevel@tonic-gate return (1); 19307c478bd9Sstevel@tonic-gate } 19317c478bd9Sstevel@tonic-gate 19327c478bd9Sstevel@tonic-gate if ((err = zone_get_state(zone_name, &zstate)) != Z_OK) { 19333f2f09c1Sdp zerror(zlogp, B_FALSE, "failed to get zone state: %s", 19347c478bd9Sstevel@tonic-gate zonecfg_strerror(err)); 19357c478bd9Sstevel@tonic-gate return (1); 19367c478bd9Sstevel@tonic-gate } 19379acbbeafSnn35248 if (zstate < ZONE_STATE_INCOMPLETE) { 19387c478bd9Sstevel@tonic-gate zerror(zlogp, B_FALSE, 19397c478bd9Sstevel@tonic-gate "cannot manage a zone which is in state '%s'", 19407c478bd9Sstevel@tonic-gate zone_state_str(zstate)); 19417c478bd9Sstevel@tonic-gate return (1); 19427c478bd9Sstevel@tonic-gate } 19437c478bd9Sstevel@tonic-gate 1944e5816e35SEdward Pilatowicz if (zonecfg_default_brand(default_brand, 1945e5816e35SEdward Pilatowicz sizeof (default_brand)) != Z_OK) { 1946e5816e35SEdward Pilatowicz zerror(zlogp, B_FALSE, "unable to determine default brand"); 1947e5816e35SEdward Pilatowicz return (1); 1948e5816e35SEdward Pilatowicz } 1949e5816e35SEdward Pilatowicz 19509acbbeafSnn35248 /* Get a handle to the brand info for this zone */ 195162868012SSteve Lawrence if (zone_get_brand(zone_name, brand_name, sizeof (brand_name)) 195262868012SSteve Lawrence != Z_OK) { 19539acbbeafSnn35248 zerror(zlogp, B_FALSE, "unable to determine zone brand"); 19549acbbeafSnn35248 return (1); 19559acbbeafSnn35248 } 195662868012SSteve Lawrence zone_isnative = (strcmp(brand_name, NATIVE_BRAND_NAME) == 0); 19579a5d73e0SRic Aleshire zone_islabeled = (strcmp(brand_name, LABELED_BRAND_NAME) == 0); 1958c5cd6260S 195962868012SSteve Lawrence /* 196062868012SSteve Lawrence * In the alternate root environment, the only supported 196162868012SSteve Lawrence * operations are mount and unmount. In this case, just treat 196262868012SSteve Lawrence * the zone as native if it is cluster. Cluster zones can be 196362868012SSteve Lawrence * native for the purpose of LU or upgrade, and the cluster 196462868012SSteve Lawrence * brand may not exist in the miniroot (such as in net install 196562868012SSteve Lawrence * upgrade). 196662868012SSteve Lawrence */ 196762868012SSteve Lawrence if (strcmp(brand_name, CLUSTER_BRAND_NAME) == 0) { 196862868012SSteve Lawrence zone_iscluster = B_TRUE; 196962868012SSteve Lawrence if (zonecfg_in_alt_root()) { 1970e5816e35SEdward Pilatowicz (void) strlcpy(brand_name, default_brand, 197162868012SSteve Lawrence sizeof (brand_name)); 197262868012SSteve Lawrence } 197362868012SSteve Lawrence } else { 197462868012SSteve Lawrence zone_iscluster = B_FALSE; 197562868012SSteve Lawrence } 197662868012SSteve Lawrence 197762868012SSteve Lawrence if ((bh = brand_open(brand_name)) == NULL) { 197862868012SSteve Lawrence zerror(zlogp, B_FALSE, "unable to open zone brand"); 197962868012SSteve Lawrence return (1); 198062868012SSteve Lawrence } 198162868012SSteve Lawrence 1982c5cd6260S /* Get state change brand hooks. */ 1983c5cd6260S if (brand_callback_init(bh, zone_name) == -1) { 1984c5cd6260S zerror(zlogp, B_TRUE, 1985c5cd6260S "failed to initialize brand state change hooks"); 1986c5cd6260S brand_close(bh); 1987c5cd6260S return (1); 1988c5cd6260S } 1989c5cd6260S 1990123807fbSedp brand_close(bh); 19919acbbeafSnn35248 19927c478bd9Sstevel@tonic-gate /* 19937c478bd9Sstevel@tonic-gate * Check that we have all privileges. It would be nice to pare 19947c478bd9Sstevel@tonic-gate * this down, but this is at least a first cut. 19957c478bd9Sstevel@tonic-gate */ 19967c478bd9Sstevel@tonic-gate if ((privset = priv_allocset()) == NULL) { 19977c478bd9Sstevel@tonic-gate zerror(zlogp, B_TRUE, "%s failed", "priv_allocset"); 19987c478bd9Sstevel@tonic-gate return (1); 19997c478bd9Sstevel@tonic-gate } 20007c478bd9Sstevel@tonic-gate 20017c478bd9Sstevel@tonic-gate if (getppriv(PRIV_EFFECTIVE, privset) != 0) { 20027c478bd9Sstevel@tonic-gate zerror(zlogp, B_TRUE, "%s failed", "getppriv"); 20037c478bd9Sstevel@tonic-gate priv_freeset(privset); 20047c478bd9Sstevel@tonic-gate return (1); 20057c478bd9Sstevel@tonic-gate } 20067c478bd9Sstevel@tonic-gate 20077c478bd9Sstevel@tonic-gate if (priv_isfullset(privset) == B_FALSE) { 20087c478bd9Sstevel@tonic-gate zerror(zlogp, B_FALSE, "You lack sufficient privilege to " 20093f2f09c1Sdp "run this command (all privs required)"); 20107c478bd9Sstevel@tonic-gate priv_freeset(privset); 20117c478bd9Sstevel@tonic-gate return (1); 20127c478bd9Sstevel@tonic-gate } 20137c478bd9Sstevel@tonic-gate priv_freeset(privset); 20147c478bd9Sstevel@tonic-gate 20157c478bd9Sstevel@tonic-gate if (mkzonedir(zlogp) != 0) 20167c478bd9Sstevel@tonic-gate return (1); 20177c478bd9Sstevel@tonic-gate 20187c478bd9Sstevel@tonic-gate /* 20197c478bd9Sstevel@tonic-gate * Pre-fork: setup shared state 20207c478bd9Sstevel@tonic-gate */ 20217c478bd9Sstevel@tonic-gate if ((shstate = (void *)mmap(NULL, shstatelen, 20227c478bd9Sstevel@tonic-gate PROT_READ|PROT_WRITE, MAP_SHARED|MAP_ANON, -1, (off_t)0)) == 20237c478bd9Sstevel@tonic-gate MAP_FAILED) { 20247c478bd9Sstevel@tonic-gate zerror(zlogp, B_TRUE, "%s failed", "mmap"); 20257c478bd9Sstevel@tonic-gate return (1); 20267c478bd9Sstevel@tonic-gate } 20277c478bd9Sstevel@tonic-gate if (sema_init(&shstate->sem, 0, USYNC_PROCESS, NULL) != 0) { 20287c478bd9Sstevel@tonic-gate zerror(zlogp, B_TRUE, "%s failed", "sema_init()"); 20297c478bd9Sstevel@tonic-gate (void) munmap((char *)shstate, shstatelen); 20307c478bd9Sstevel@tonic-gate return (1); 20317c478bd9Sstevel@tonic-gate } 20327c478bd9Sstevel@tonic-gate shstate->log.logfile = NULL; 20337c478bd9Sstevel@tonic-gate shstate->log.buflen = shstatelen - sizeof (*shstate); 20347c478bd9Sstevel@tonic-gate shstate->log.loglen = shstate->log.buflen; 20357c478bd9Sstevel@tonic-gate shstate->log.buf = (char *)shstate + sizeof (*shstate); 20367c478bd9Sstevel@tonic-gate shstate->log.log = shstate->log.buf; 20377c478bd9Sstevel@tonic-gate shstate->log.locale = parents_locale; 20387c478bd9Sstevel@tonic-gate shstate->status = -1; 20397c478bd9Sstevel@tonic-gate 20407c478bd9Sstevel@tonic-gate /* 20417c478bd9Sstevel@tonic-gate * We need a SIGCHLD handler so the sema_wait() below will wake 20427c478bd9Sstevel@tonic-gate * up if the child dies without doing a sema_post(). 20437c478bd9Sstevel@tonic-gate */ 20447c478bd9Sstevel@tonic-gate (void) sigset(SIGCHLD, sigchld); 20457c478bd9Sstevel@tonic-gate /* 20467c478bd9Sstevel@tonic-gate * We must mask SIGCHLD until after we've coped with the fork 20477c478bd9Sstevel@tonic-gate * sufficiently to deal with it; otherwise we can race and 20487c478bd9Sstevel@tonic-gate * receive the signal before pid has been initialized 20497c478bd9Sstevel@tonic-gate * (yes, this really happens). 20507c478bd9Sstevel@tonic-gate */ 20517c478bd9Sstevel@tonic-gate (void) sigemptyset(&block_cld); 20527c478bd9Sstevel@tonic-gate (void) sigaddset(&block_cld, SIGCHLD); 20537c478bd9Sstevel@tonic-gate (void) sigprocmask(SIG_BLOCK, &block_cld, NULL); 20547c478bd9Sstevel@tonic-gate 2055*056d3a7dSJerry Jelinek /* 2056*056d3a7dSJerry Jelinek * The parent only needs stderr after the fork, so close other fd's 2057*056d3a7dSJerry Jelinek * that we inherited from zoneadm so that the parent doesn't have those 2058*056d3a7dSJerry Jelinek * open while waiting. The child will close the rest after the fork. 2059*056d3a7dSJerry Jelinek */ 2060*056d3a7dSJerry Jelinek closefrom(3); 2061*056d3a7dSJerry Jelinek 20625cb4571dSdp if ((ctfd = init_template()) == -1) { 20635cb4571dSdp zerror(zlogp, B_TRUE, "failed to create contract"); 20645cb4571dSdp return (1); 20655cb4571dSdp } 20665cb4571dSdp 20677c478bd9Sstevel@tonic-gate /* 20687c478bd9Sstevel@tonic-gate * Do not let another thread localize a message while we are forking. 20697c478bd9Sstevel@tonic-gate */ 20707c478bd9Sstevel@tonic-gate (void) mutex_lock(&msglock); 20717c478bd9Sstevel@tonic-gate pid = fork(); 20727c478bd9Sstevel@tonic-gate (void) mutex_unlock(&msglock); 20735cb4571dSdp 20745cb4571dSdp /* 20755cb4571dSdp * In all cases (parent, child, and in the event of an error) we 20765cb4571dSdp * don't want to cause creation of contracts on subsequent fork()s. 20775cb4571dSdp */ 20785cb4571dSdp (void) ct_tmpl_clear(ctfd); 20795cb4571dSdp (void) close(ctfd); 20805cb4571dSdp 20817c478bd9Sstevel@tonic-gate if (pid == -1) { 20827c478bd9Sstevel@tonic-gate zerror(zlogp, B_TRUE, "could not fork"); 20837c478bd9Sstevel@tonic-gate return (1); 20847c478bd9Sstevel@tonic-gate 20857c478bd9Sstevel@tonic-gate } else if (pid > 0) { /* parent */ 20867c478bd9Sstevel@tonic-gate (void) sigprocmask(SIG_UNBLOCK, &block_cld, NULL); 20877c478bd9Sstevel@tonic-gate /* 20887c478bd9Sstevel@tonic-gate * This marks a window of vulnerability in which we receive 20897c478bd9Sstevel@tonic-gate * the SIGCLD before falling into sema_wait (normally we would 20907c478bd9Sstevel@tonic-gate * get woken up from sema_wait with EINTR upon receipt of 20917c478bd9Sstevel@tonic-gate * SIGCLD). So we may need to use some other scheme like 20927c478bd9Sstevel@tonic-gate * sema_posting in the sigcld handler. 20937c478bd9Sstevel@tonic-gate * blech 20947c478bd9Sstevel@tonic-gate */ 20957c478bd9Sstevel@tonic-gate (void) sema_wait(&shstate->sem); 20967c478bd9Sstevel@tonic-gate (void) sema_destroy(&shstate->sem); 20977c478bd9Sstevel@tonic-gate if (shstate->status != 0) 20987c478bd9Sstevel@tonic-gate (void) waitpid(pid, NULL, WNOHANG); 20997c478bd9Sstevel@tonic-gate /* 21007c478bd9Sstevel@tonic-gate * It's ok if we die with SIGPIPE. It's not like we could have 21017c478bd9Sstevel@tonic-gate * done anything about it. 21027c478bd9Sstevel@tonic-gate */ 21037c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "%s", shstate->log.buf); 21047c478bd9Sstevel@tonic-gate _exit(shstate->status == 0 ? 0 : 1); 21057c478bd9Sstevel@tonic-gate } 21067c478bd9Sstevel@tonic-gate 21077c478bd9Sstevel@tonic-gate /* 21087c478bd9Sstevel@tonic-gate * The child charges on. 21097c478bd9Sstevel@tonic-gate */ 21107c478bd9Sstevel@tonic-gate (void) sigset(SIGCHLD, SIG_DFL); 21117c478bd9Sstevel@tonic-gate (void) sigprocmask(SIG_UNBLOCK, &block_cld, NULL); 21127c478bd9Sstevel@tonic-gate 21137c478bd9Sstevel@tonic-gate /* 21147c478bd9Sstevel@tonic-gate * SIGPIPE can be delivered if we write to a socket for which the 21157c478bd9Sstevel@tonic-gate * peer endpoint is gone. That can lead to too-early termination 21167c478bd9Sstevel@tonic-gate * of zoneadmd, and that's not good eats. 21177c478bd9Sstevel@tonic-gate */ 21187c478bd9Sstevel@tonic-gate (void) sigset(SIGPIPE, SIG_IGN); 21197c478bd9Sstevel@tonic-gate /* 21207c478bd9Sstevel@tonic-gate * Stop using stderr 21217c478bd9Sstevel@tonic-gate */ 21227c478bd9Sstevel@tonic-gate zlogp = &shstate->log; 21237c478bd9Sstevel@tonic-gate 21247c478bd9Sstevel@tonic-gate /* 21257c478bd9Sstevel@tonic-gate * We don't need stdout/stderr from now on. 21267c478bd9Sstevel@tonic-gate */ 21277c478bd9Sstevel@tonic-gate closefrom(0); 21287c478bd9Sstevel@tonic-gate 21297c478bd9Sstevel@tonic-gate /* 21307c478bd9Sstevel@tonic-gate * Initialize the syslog zlog_t. This needs to be done after 21317c478bd9Sstevel@tonic-gate * the call to closefrom(). 21327c478bd9Sstevel@tonic-gate */ 21337c478bd9Sstevel@tonic-gate logsys.buf = logsys.log = NULL; 21347c478bd9Sstevel@tonic-gate logsys.buflen = logsys.loglen = 0; 21357c478bd9Sstevel@tonic-gate logsys.logfile = NULL; 21367c478bd9Sstevel@tonic-gate logsys.locale = DEFAULT_LOCALE; 21377c478bd9Sstevel@tonic-gate 21387c478bd9Sstevel@tonic-gate openlog("zoneadmd", LOG_PID, LOG_DAEMON); 21397c478bd9Sstevel@tonic-gate 21407c478bd9Sstevel@tonic-gate /* 21417c478bd9Sstevel@tonic-gate * The eventstream is used to publish state changes in the zone 21427c478bd9Sstevel@tonic-gate * from the door threads to the console I/O poller. 21437c478bd9Sstevel@tonic-gate */ 21447c478bd9Sstevel@tonic-gate if (eventstream_init() == -1) { 21457c478bd9Sstevel@tonic-gate zerror(zlogp, B_TRUE, "unable to create eventstream"); 21467c478bd9Sstevel@tonic-gate goto child_out; 21477c478bd9Sstevel@tonic-gate } 21487c478bd9Sstevel@tonic-gate 21497c478bd9Sstevel@tonic-gate (void) snprintf(zone_door_path, sizeof (zone_door_path), 2150108322fbScarlsonj "%s" ZONE_DOOR_PATH, zonecfg_get_root(), zone_name); 21517c478bd9Sstevel@tonic-gate 21527c478bd9Sstevel@tonic-gate /* 21537c478bd9Sstevel@tonic-gate * See if another zoneadmd is running for this zone. If not, then we 21547c478bd9Sstevel@tonic-gate * can now modify system state. 21557c478bd9Sstevel@tonic-gate */ 21567c478bd9Sstevel@tonic-gate if (make_daemon_exclusive(zlogp) == -1) 21577c478bd9Sstevel@tonic-gate goto child_out; 21587c478bd9Sstevel@tonic-gate 21597c478bd9Sstevel@tonic-gate 21607c478bd9Sstevel@tonic-gate /* 21617c478bd9Sstevel@tonic-gate * Create/join a new session; we need to be careful of what we do with 21627c478bd9Sstevel@tonic-gate * the console from now on so we don't end up being the session leader 21637c478bd9Sstevel@tonic-gate * for the terminal we're going to be handing out. 21647c478bd9Sstevel@tonic-gate */ 21657c478bd9Sstevel@tonic-gate (void) setsid(); 21667c478bd9Sstevel@tonic-gate 21677c478bd9Sstevel@tonic-gate /* 21687c478bd9Sstevel@tonic-gate * This thread shouldn't be receiving any signals; in particular, 21697c478bd9Sstevel@tonic-gate * SIGCHLD should be received by the thread doing the fork(). 21707c478bd9Sstevel@tonic-gate */ 21717c478bd9Sstevel@tonic-gate (void) sigfillset(&blockset); 21727c478bd9Sstevel@tonic-gate (void) thr_sigsetmask(SIG_BLOCK, &blockset, NULL); 21737c478bd9Sstevel@tonic-gate 21747c478bd9Sstevel@tonic-gate /* 21757c478bd9Sstevel@tonic-gate * Setup the console device and get ready to serve the console; 21767c478bd9Sstevel@tonic-gate * once this has completed, we're ready to let console clients 21777c478bd9Sstevel@tonic-gate * make an attempt to connect (they will block until 21787c478bd9Sstevel@tonic-gate * serve_console_sock() below gets called, and any pending 21797c478bd9Sstevel@tonic-gate * connection is accept()ed). 21807c478bd9Sstevel@tonic-gate */ 21819d5056eaSjv227347 if (!zonecfg_in_alt_root() && init_console(zlogp) < 0) 21827c478bd9Sstevel@tonic-gate goto child_out; 21837c478bd9Sstevel@tonic-gate 21847c478bd9Sstevel@tonic-gate /* 21857c478bd9Sstevel@tonic-gate * Take the lock now, so that when the door server gets going, we 21867c478bd9Sstevel@tonic-gate * are guaranteed that it won't take a request until we are sure 21877c478bd9Sstevel@tonic-gate * that everything is completely set up. See the child_out: label 21887c478bd9Sstevel@tonic-gate * below to see why this matters. 21897c478bd9Sstevel@tonic-gate */ 21907c478bd9Sstevel@tonic-gate (void) mutex_lock(&lock); 21917c478bd9Sstevel@tonic-gate 2192108322fbScarlsonj /* Init semaphore for scratch zones. */ 2193108322fbScarlsonj if (sema_init(&scratch_sem, 0, USYNC_THREAD, NULL) == -1) { 2194108322fbScarlsonj zerror(zlogp, B_TRUE, 2195108322fbScarlsonj "failed to initialize semaphore for scratch zone"); 2196108322fbScarlsonj goto child_out; 2197108322fbScarlsonj } 2198108322fbScarlsonj 21994ac67f02SAnurag S. Maskey /* open the dladm handle */ 22004ac67f02SAnurag S. Maskey if (dladm_open(&dld_handle) != DLADM_STATUS_OK) { 22014ac67f02SAnurag S. Maskey zerror(zlogp, B_FALSE, "failed to open dladm handle"); 22024ac67f02SAnurag S. Maskey goto child_out; 22034ac67f02SAnurag S. Maskey } 22044ac67f02SAnurag S. Maskey 22057c478bd9Sstevel@tonic-gate /* 22067c478bd9Sstevel@tonic-gate * Note: door setup must occur *after* the console is setup. 22077c478bd9Sstevel@tonic-gate * This is so that as zlogin tests the door to see if zoneadmd 22087c478bd9Sstevel@tonic-gate * is ready yet, we know that the console will get serviced 22097c478bd9Sstevel@tonic-gate * once door_info() indicates that the door is "up". 22107c478bd9Sstevel@tonic-gate */ 22117c478bd9Sstevel@tonic-gate if (setup_door(zlogp) == -1) 22127c478bd9Sstevel@tonic-gate goto child_out; 22137c478bd9Sstevel@tonic-gate 22147c478bd9Sstevel@tonic-gate /* 22157c478bd9Sstevel@tonic-gate * Things seem OK so far; tell the parent process that we're done 22167c478bd9Sstevel@tonic-gate * with setup tasks. This will cause the parent to exit, signalling 22177c478bd9Sstevel@tonic-gate * to zoneadm, zlogin, or whatever forked it that we are ready to 22187c478bd9Sstevel@tonic-gate * service requests. 22197c478bd9Sstevel@tonic-gate */ 22207c478bd9Sstevel@tonic-gate shstate->status = 0; 22217c478bd9Sstevel@tonic-gate (void) sema_post(&shstate->sem); 22227c478bd9Sstevel@tonic-gate (void) munmap((char *)shstate, shstatelen); 22237c478bd9Sstevel@tonic-gate shstate = NULL; 22247c478bd9Sstevel@tonic-gate 22257c478bd9Sstevel@tonic-gate (void) mutex_unlock(&lock); 22267c478bd9Sstevel@tonic-gate 22277c478bd9Sstevel@tonic-gate /* 22287c478bd9Sstevel@tonic-gate * zlogp is now invalid, so reset it to the syslog logger. 22297c478bd9Sstevel@tonic-gate */ 22307c478bd9Sstevel@tonic-gate zlogp = &logsys; 22317c478bd9Sstevel@tonic-gate 22327c478bd9Sstevel@tonic-gate /* 22337c478bd9Sstevel@tonic-gate * Now that we are free of any parents, switch to the default locale. 22347c478bd9Sstevel@tonic-gate */ 22357c478bd9Sstevel@tonic-gate (void) setlocale(LC_ALL, DEFAULT_LOCALE); 22367c478bd9Sstevel@tonic-gate 22377c478bd9Sstevel@tonic-gate /* 22387c478bd9Sstevel@tonic-gate * At this point the setup portion of main() is basically done, so 22397c478bd9Sstevel@tonic-gate * we reuse this thread to manage the zone console. When 22407c478bd9Sstevel@tonic-gate * serve_console() has returned, we are past the point of no return 22417c478bd9Sstevel@tonic-gate * in the life of this zoneadmd. 22427c478bd9Sstevel@tonic-gate */ 2243108322fbScarlsonj if (zonecfg_in_alt_root()) { 2244108322fbScarlsonj /* 2245108322fbScarlsonj * This is just awful, but mounted scratch zones don't (and 2246108322fbScarlsonj * can't) have consoles. We just wait for unmount instead. 2247108322fbScarlsonj */ 2248108322fbScarlsonj while (sema_wait(&scratch_sem) == EINTR) 2249108322fbScarlsonj ; 2250108322fbScarlsonj } else { 22517c478bd9Sstevel@tonic-gate serve_console(zlogp); 22527c478bd9Sstevel@tonic-gate assert(in_death_throes); 2253108322fbScarlsonj } 22547c478bd9Sstevel@tonic-gate 22557c478bd9Sstevel@tonic-gate /* 22567c478bd9Sstevel@tonic-gate * This is the next-to-last part of the exit interlock. Upon calling 22577c478bd9Sstevel@tonic-gate * fdetach(), the door will go unreferenced; once any 22587c478bd9Sstevel@tonic-gate * outstanding requests (like the door thread doing Z_HALT) are 22597c478bd9Sstevel@tonic-gate * done, the door will get an UNREF notification; when it handles 2260f594b34cSEdward Pilatowicz * the UNREF, the door server will cause the exit. It's possible 2261f594b34cSEdward Pilatowicz * that fdetach() can fail because the file is in use, in which 2262f594b34cSEdward Pilatowicz * case we'll retry the operation. 22637c478bd9Sstevel@tonic-gate */ 22647c478bd9Sstevel@tonic-gate assert(!MUTEX_HELD(&lock)); 2265f594b34cSEdward Pilatowicz for (;;) { 2266f594b34cSEdward Pilatowicz if ((fdetach(zone_door_path) == 0) || (errno != EBUSY)) 2267f594b34cSEdward Pilatowicz break; 2268f594b34cSEdward Pilatowicz yield(); 2269f594b34cSEdward Pilatowicz } 22704ac67f02SAnurag S. Maskey 22717c478bd9Sstevel@tonic-gate for (;;) 22727c478bd9Sstevel@tonic-gate (void) pause(); 22737c478bd9Sstevel@tonic-gate 22747c478bd9Sstevel@tonic-gate child_out: 22757c478bd9Sstevel@tonic-gate assert(pid == 0); 22767c478bd9Sstevel@tonic-gate if (shstate != NULL) { 22777c478bd9Sstevel@tonic-gate shstate->status = -1; 22787c478bd9Sstevel@tonic-gate (void) sema_post(&shstate->sem); 22797c478bd9Sstevel@tonic-gate (void) munmap((char *)shstate, shstatelen); 22807c478bd9Sstevel@tonic-gate } 22817c478bd9Sstevel@tonic-gate 22827c478bd9Sstevel@tonic-gate /* 22837c478bd9Sstevel@tonic-gate * This might trigger an unref notification, but if so, 22847c478bd9Sstevel@tonic-gate * we are still holding the lock, so our call to exit will 22857c478bd9Sstevel@tonic-gate * ultimately win the race and will publish the right exit 22867c478bd9Sstevel@tonic-gate * code. 22877c478bd9Sstevel@tonic-gate */ 22887c478bd9Sstevel@tonic-gate if (zone_door != -1) { 22897c478bd9Sstevel@tonic-gate assert(MUTEX_HELD(&lock)); 22907c478bd9Sstevel@tonic-gate (void) door_revoke(zone_door); 22917c478bd9Sstevel@tonic-gate (void) fdetach(zone_door_path); 22927c478bd9Sstevel@tonic-gate } 22934ac67f02SAnurag S. Maskey 22944ac67f02SAnurag S. Maskey if (dld_handle != NULL) 22954ac67f02SAnurag S. Maskey dladm_close(dld_handle); 22964ac67f02SAnurag S. Maskey 22977c478bd9Sstevel@tonic-gate return (1); /* return from main() forcibly exits an MT process */ 22987c478bd9Sstevel@tonic-gate } 2299