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 /* 23*9d5056eaSjv227347 * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 247c478bd9Sstevel@tonic-gate * Use is subject to license terms. 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> 987c478bd9Sstevel@tonic-gate #include <sys/contract/process.h> 997c478bd9Sstevel@tonic-gate #include <sys/ctfs.h> 1007c478bd9Sstevel@tonic-gate 1017c478bd9Sstevel@tonic-gate #include <libzonecfg.h> 1027c478bd9Sstevel@tonic-gate #include "zoneadmd.h" 1037c478bd9Sstevel@tonic-gate 1047c478bd9Sstevel@tonic-gate static char *progname; 1057c478bd9Sstevel@tonic-gate char *zone_name; /* zone which we are managing */ 1069acbbeafSnn35248 char brand_name[MAXNAMELEN]; 1079acbbeafSnn35248 boolean_t zone_isnative; 10884561e8cStd153743 boolean_t zone_iscluster; 109108322fbScarlsonj static zoneid_t zone_id; 1104ac67f02SAnurag S. Maskey dladm_handle_t dld_handle = NULL; 1117c478bd9Sstevel@tonic-gate 112c5cd6260S static char pre_statechg_hook[2 * MAXPATHLEN]; 113c5cd6260S static char post_statechg_hook[2 * MAXPATHLEN]; 114c5cd6260S char query_hook[2 * MAXPATHLEN]; 115c5cd6260S 11622321485Svp157776 zlog_t logsys; 1177c478bd9Sstevel@tonic-gate 1187c478bd9Sstevel@tonic-gate mutex_t lock = DEFAULTMUTEX; /* to serialize stuff */ 1197c478bd9Sstevel@tonic-gate mutex_t msglock = DEFAULTMUTEX; /* for calling setlocale() */ 1207c478bd9Sstevel@tonic-gate 121108322fbScarlsonj static sema_t scratch_sem; /* for scratch zones */ 122108322fbScarlsonj 1237c478bd9Sstevel@tonic-gate static char zone_door_path[MAXPATHLEN]; 1247c478bd9Sstevel@tonic-gate static int zone_door = -1; 1257c478bd9Sstevel@tonic-gate 1267c478bd9Sstevel@tonic-gate boolean_t in_death_throes = B_FALSE; /* daemon is dying */ 1277c478bd9Sstevel@tonic-gate boolean_t bringup_failure_recovery = B_FALSE; /* ignore certain failures */ 1287c478bd9Sstevel@tonic-gate 1297c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN) /* should be defined by cc -D */ 1307c478bd9Sstevel@tonic-gate #define TEXT_DOMAIN "SYS_TEST" /* Use this only if it wasn't */ 1317c478bd9Sstevel@tonic-gate #endif 1327c478bd9Sstevel@tonic-gate 1337c478bd9Sstevel@tonic-gate #define DEFAULT_LOCALE "C" 1347c478bd9Sstevel@tonic-gate 135108322fbScarlsonj static const char * 136108322fbScarlsonj z_cmd_name(zone_cmd_t zcmd) 137108322fbScarlsonj { 138108322fbScarlsonj /* This list needs to match the enum in sys/zone.h */ 139108322fbScarlsonj static const char *zcmdstr[] = { 1409acbbeafSnn35248 "ready", "boot", "forceboot", "reboot", "halt", 1419acbbeafSnn35248 "note_uninstalling", "mount", "forcemount", "unmount" 142108322fbScarlsonj }; 143108322fbScarlsonj 144108322fbScarlsonj if (zcmd >= sizeof (zcmdstr) / sizeof (*zcmdstr)) 145108322fbScarlsonj return ("unknown"); 146108322fbScarlsonj else 147108322fbScarlsonj return (zcmdstr[(int)zcmd]); 148108322fbScarlsonj } 149108322fbScarlsonj 1507c478bd9Sstevel@tonic-gate static char * 1517c478bd9Sstevel@tonic-gate get_execbasename(char *execfullname) 1527c478bd9Sstevel@tonic-gate { 1537c478bd9Sstevel@tonic-gate char *last_slash, *execbasename; 1547c478bd9Sstevel@tonic-gate 1557c478bd9Sstevel@tonic-gate /* guard against '/' at end of command invocation */ 1567c478bd9Sstevel@tonic-gate for (;;) { 1577c478bd9Sstevel@tonic-gate last_slash = strrchr(execfullname, '/'); 1587c478bd9Sstevel@tonic-gate if (last_slash == NULL) { 1597c478bd9Sstevel@tonic-gate execbasename = execfullname; 1607c478bd9Sstevel@tonic-gate break; 1617c478bd9Sstevel@tonic-gate } else { 1627c478bd9Sstevel@tonic-gate execbasename = last_slash + 1; 1637c478bd9Sstevel@tonic-gate if (*execbasename == '\0') { 1647c478bd9Sstevel@tonic-gate *last_slash = '\0'; 1657c478bd9Sstevel@tonic-gate continue; 1667c478bd9Sstevel@tonic-gate } 1677c478bd9Sstevel@tonic-gate break; 1687c478bd9Sstevel@tonic-gate } 1697c478bd9Sstevel@tonic-gate } 1707c478bd9Sstevel@tonic-gate return (execbasename); 1717c478bd9Sstevel@tonic-gate } 1727c478bd9Sstevel@tonic-gate 1737c478bd9Sstevel@tonic-gate static void 1747c478bd9Sstevel@tonic-gate usage(void) 1757c478bd9Sstevel@tonic-gate { 1767c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("Usage: %s -z zonename\n"), progname); 1777c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 1787c478bd9Sstevel@tonic-gate gettext("\tNote: %s should not be run directly.\n"), progname); 1797c478bd9Sstevel@tonic-gate exit(2); 1807c478bd9Sstevel@tonic-gate } 1817c478bd9Sstevel@tonic-gate 1827c478bd9Sstevel@tonic-gate /* ARGSUSED */ 1837c478bd9Sstevel@tonic-gate static void 1847c478bd9Sstevel@tonic-gate sigchld(int sig) 1857c478bd9Sstevel@tonic-gate { 1867c478bd9Sstevel@tonic-gate } 1877c478bd9Sstevel@tonic-gate 1887c478bd9Sstevel@tonic-gate char * 1897c478bd9Sstevel@tonic-gate localize_msg(char *locale, const char *msg) 1907c478bd9Sstevel@tonic-gate { 1917c478bd9Sstevel@tonic-gate char *out; 1927c478bd9Sstevel@tonic-gate 1937c478bd9Sstevel@tonic-gate (void) mutex_lock(&msglock); 1947c478bd9Sstevel@tonic-gate (void) setlocale(LC_MESSAGES, locale); 1957c478bd9Sstevel@tonic-gate out = gettext(msg); 1967c478bd9Sstevel@tonic-gate (void) setlocale(LC_MESSAGES, DEFAULT_LOCALE); 1977c478bd9Sstevel@tonic-gate (void) mutex_unlock(&msglock); 1987c478bd9Sstevel@tonic-gate return (out); 1997c478bd9Sstevel@tonic-gate } 2007c478bd9Sstevel@tonic-gate 2017c478bd9Sstevel@tonic-gate /* PRINTFLIKE3 */ 2027c478bd9Sstevel@tonic-gate void 2037c478bd9Sstevel@tonic-gate zerror(zlog_t *zlogp, boolean_t use_strerror, const char *fmt, ...) 2047c478bd9Sstevel@tonic-gate { 2057c478bd9Sstevel@tonic-gate va_list alist; 2067c478bd9Sstevel@tonic-gate char buf[MAXPATHLEN * 2]; /* enough space for err msg with a path */ 2077c478bd9Sstevel@tonic-gate char *bp; 2087c478bd9Sstevel@tonic-gate int saved_errno = errno; 2097c478bd9Sstevel@tonic-gate 2107c478bd9Sstevel@tonic-gate if (zlogp == NULL) 2117c478bd9Sstevel@tonic-gate return; 2127c478bd9Sstevel@tonic-gate if (zlogp == &logsys) 2137c478bd9Sstevel@tonic-gate (void) snprintf(buf, sizeof (buf), "[zone '%s'] ", 2147c478bd9Sstevel@tonic-gate zone_name); 2157c478bd9Sstevel@tonic-gate else 2167c478bd9Sstevel@tonic-gate buf[0] = '\0'; 2177c478bd9Sstevel@tonic-gate bp = &(buf[strlen(buf)]); 2187c478bd9Sstevel@tonic-gate 2197c478bd9Sstevel@tonic-gate /* 2207c478bd9Sstevel@tonic-gate * In theory, the locale pointer should be set to either "C" or a 2217c478bd9Sstevel@tonic-gate * char array, so it should never be NULL 2227c478bd9Sstevel@tonic-gate */ 2237c478bd9Sstevel@tonic-gate assert(zlogp->locale != NULL); 2247c478bd9Sstevel@tonic-gate /* Locale is per process, but we are multi-threaded... */ 2257c478bd9Sstevel@tonic-gate fmt = localize_msg(zlogp->locale, fmt); 2267c478bd9Sstevel@tonic-gate 2277c478bd9Sstevel@tonic-gate va_start(alist, fmt); 2287c478bd9Sstevel@tonic-gate (void) vsnprintf(bp, sizeof (buf) - (bp - buf), fmt, alist); 2297c478bd9Sstevel@tonic-gate va_end(alist); 2307c478bd9Sstevel@tonic-gate bp = &(buf[strlen(buf)]); 2317c478bd9Sstevel@tonic-gate if (use_strerror) 2327c478bd9Sstevel@tonic-gate (void) snprintf(bp, sizeof (buf) - (bp - buf), ": %s", 2337c478bd9Sstevel@tonic-gate strerror(saved_errno)); 2347c478bd9Sstevel@tonic-gate if (zlogp == &logsys) { 2357c478bd9Sstevel@tonic-gate (void) syslog(LOG_ERR, "%s", buf); 2367c478bd9Sstevel@tonic-gate } else if (zlogp->logfile != NULL) { 2377c478bd9Sstevel@tonic-gate (void) fprintf(zlogp->logfile, "%s\n", buf); 2387c478bd9Sstevel@tonic-gate } else { 2397c478bd9Sstevel@tonic-gate size_t buflen; 2407c478bd9Sstevel@tonic-gate size_t copylen; 2417c478bd9Sstevel@tonic-gate 2427c478bd9Sstevel@tonic-gate buflen = snprintf(zlogp->log, zlogp->loglen, "%s\n", buf); 2437c478bd9Sstevel@tonic-gate copylen = MIN(buflen, zlogp->loglen); 2447c478bd9Sstevel@tonic-gate zlogp->log += copylen; 2457c478bd9Sstevel@tonic-gate zlogp->loglen -= copylen; 2467c478bd9Sstevel@tonic-gate } 2477c478bd9Sstevel@tonic-gate } 2487c478bd9Sstevel@tonic-gate 2493f2f09c1Sdp /* 2503f2f09c1Sdp * Emit a warning for any boot arguments which are unrecognized. Since 2513f2f09c1Sdp * Solaris boot arguments are getopt(3c) compatible (see kernel(1m)), we 2523f2f09c1Sdp * put the arguments into an argv style array, use getopt to process them, 2533f2f09c1Sdp * and put the resultant argument string back into outargs. 2543f2f09c1Sdp * 2553f2f09c1Sdp * During the filtering, we pull out any arguments which are truly "boot" 2563f2f09c1Sdp * arguments, leaving only those which are to be passed intact to the 2573f2f09c1Sdp * progenitor process. The one we support at the moment is -i, which 2583f2f09c1Sdp * indicates to the kernel which program should be launched as 'init'. 2593f2f09c1Sdp * 2603f2f09c1Sdp * A return of Z_INVAL indicates specifically that the arguments are 2613f2f09c1Sdp * not valid; this is a non-fatal error. Except for Z_OK, all other return 2623f2f09c1Sdp * values are treated as fatal. 2633f2f09c1Sdp */ 2643f2f09c1Sdp static int 2653f2f09c1Sdp filter_bootargs(zlog_t *zlogp, const char *inargs, char *outargs, 2663f2f09c1Sdp char *init_file, char *badarg) 2673f2f09c1Sdp { 2683f2f09c1Sdp int argc = 0, argc_save; 2693f2f09c1Sdp int i; 2703f2f09c1Sdp int err; 2713f2f09c1Sdp char *arg, *lasts, **argv = NULL, **argv_save; 2723f2f09c1Sdp char zonecfg_args[BOOTARGS_MAX]; 2733f2f09c1Sdp char scratchargs[BOOTARGS_MAX], *sargs; 2743f2f09c1Sdp char c; 2753f2f09c1Sdp 2763f2f09c1Sdp bzero(outargs, BOOTARGS_MAX); 2773f2f09c1Sdp bzero(badarg, BOOTARGS_MAX); 2783f2f09c1Sdp 2793f2f09c1Sdp /* 2803f2f09c1Sdp * If the user didn't specify transient boot arguments, check 2813f2f09c1Sdp * to see if there were any specified in the zone configuration, 2823f2f09c1Sdp * and use them if applicable. 2833f2f09c1Sdp */ 2843f2f09c1Sdp if (inargs == NULL || inargs[0] == '\0') { 2853f2f09c1Sdp zone_dochandle_t handle; 2863f2f09c1Sdp if ((handle = zonecfg_init_handle()) == NULL) { 2873f2f09c1Sdp zerror(zlogp, B_TRUE, 2883f2f09c1Sdp "getting zone configuration handle"); 2893f2f09c1Sdp return (Z_BAD_HANDLE); 2903f2f09c1Sdp } 2913f2f09c1Sdp err = zonecfg_get_snapshot_handle(zone_name, handle); 2923f2f09c1Sdp if (err != Z_OK) { 2933f2f09c1Sdp zerror(zlogp, B_FALSE, 2943f2f09c1Sdp "invalid configuration snapshot"); 2953f2f09c1Sdp zonecfg_fini_handle(handle); 2963f2f09c1Sdp return (Z_BAD_HANDLE); 2973f2f09c1Sdp } 2983f2f09c1Sdp 2993f2f09c1Sdp bzero(zonecfg_args, sizeof (zonecfg_args)); 3003f2f09c1Sdp (void) zonecfg_get_bootargs(handle, zonecfg_args, 3013f2f09c1Sdp sizeof (zonecfg_args)); 3023f2f09c1Sdp inargs = zonecfg_args; 3033f2f09c1Sdp zonecfg_fini_handle(handle); 3043f2f09c1Sdp } 3053f2f09c1Sdp 3063f2f09c1Sdp if (strlen(inargs) >= BOOTARGS_MAX) { 3073f2f09c1Sdp zerror(zlogp, B_FALSE, "boot argument string too long"); 3083f2f09c1Sdp return (Z_INVAL); 3093f2f09c1Sdp } 3103f2f09c1Sdp 3113f2f09c1Sdp (void) strlcpy(scratchargs, inargs, sizeof (scratchargs)); 3123f2f09c1Sdp sargs = scratchargs; 3133f2f09c1Sdp while ((arg = strtok_r(sargs, " \t", &lasts)) != NULL) { 3143f2f09c1Sdp sargs = NULL; 3153f2f09c1Sdp argc++; 3163f2f09c1Sdp } 3173f2f09c1Sdp 3183f2f09c1Sdp if ((argv = calloc(argc + 1, sizeof (char *))) == NULL) { 3193f2f09c1Sdp zerror(zlogp, B_FALSE, "memory allocation failed"); 3203f2f09c1Sdp return (Z_NOMEM); 3213f2f09c1Sdp } 3223f2f09c1Sdp 3233f2f09c1Sdp argv_save = argv; 3243f2f09c1Sdp argc_save = argc; 3253f2f09c1Sdp 3263f2f09c1Sdp (void) strlcpy(scratchargs, inargs, sizeof (scratchargs)); 3273f2f09c1Sdp sargs = scratchargs; 3283f2f09c1Sdp i = 0; 3293f2f09c1Sdp while ((arg = strtok_r(sargs, " \t", &lasts)) != NULL) { 3303f2f09c1Sdp sargs = NULL; 3313f2f09c1Sdp if ((argv[i] = strdup(arg)) == NULL) { 3323f2f09c1Sdp err = Z_NOMEM; 3333f2f09c1Sdp zerror(zlogp, B_FALSE, "memory allocation failed"); 3343f2f09c1Sdp goto done; 3353f2f09c1Sdp } 3363f2f09c1Sdp i++; 3373f2f09c1Sdp } 3383f2f09c1Sdp 3393f2f09c1Sdp /* 3403f2f09c1Sdp * We preserve compatibility with the Solaris system boot behavior, 3413f2f09c1Sdp * which allows: 3423f2f09c1Sdp * 3433f2f09c1Sdp * # reboot kernel/unix -s -m verbose 3443f2f09c1Sdp * 3453f2f09c1Sdp * In this example, kernel/unix tells the booter what file to 3463f2f09c1Sdp * boot. We don't want reboot in a zone to be gratuitously different, 3473f2f09c1Sdp * so we silently ignore the boot file, if necessary. 3483f2f09c1Sdp */ 3493f2f09c1Sdp if (argv[0] == NULL) 3503f2f09c1Sdp goto done; 3513f2f09c1Sdp 3523f2f09c1Sdp assert(argv[0][0] != ' '); 3533f2f09c1Sdp assert(argv[0][0] != '\t'); 3543f2f09c1Sdp 3553f2f09c1Sdp if (argv[0][0] != '-' && argv[0][0] != '\0') { 3563f2f09c1Sdp argv = &argv[1]; 3573f2f09c1Sdp argc--; 3583f2f09c1Sdp } 3593f2f09c1Sdp 3603f2f09c1Sdp optind = 0; 3613f2f09c1Sdp opterr = 0; 3623f2f09c1Sdp err = Z_OK; 3639acbbeafSnn35248 while ((c = getopt(argc, argv, "fi:m:s")) != -1) { 3643f2f09c1Sdp switch (c) { 3653f2f09c1Sdp case 'i': 3663f2f09c1Sdp /* 3673f2f09c1Sdp * -i is handled by the runtime and is not passed 3683f2f09c1Sdp * along to userland 3693f2f09c1Sdp */ 3703f2f09c1Sdp (void) strlcpy(init_file, optarg, MAXPATHLEN); 3713f2f09c1Sdp break; 3729acbbeafSnn35248 case 'f': 3739acbbeafSnn35248 /* This has already been processed by zoneadm */ 3749acbbeafSnn35248 break; 3753f2f09c1Sdp case 'm': 3763f2f09c1Sdp case 's': 3773f2f09c1Sdp /* These pass through unmolested */ 3783f2f09c1Sdp (void) snprintf(outargs, BOOTARGS_MAX, 3793f2f09c1Sdp "%s -%c %s ", outargs, c, optarg ? optarg : ""); 3803f2f09c1Sdp break; 3813f2f09c1Sdp case '?': 3823f2f09c1Sdp /* 3833f2f09c1Sdp * We warn about unknown arguments but pass them 3843f2f09c1Sdp * along anyway-- if someone wants to develop their 3853f2f09c1Sdp * own init replacement, they can pass it whatever 3863f2f09c1Sdp * args they want. 3873f2f09c1Sdp */ 3883f2f09c1Sdp err = Z_INVAL; 3893f2f09c1Sdp (void) snprintf(outargs, BOOTARGS_MAX, 3903f2f09c1Sdp "%s -%c", outargs, optopt); 3913f2f09c1Sdp (void) snprintf(badarg, BOOTARGS_MAX, 3923f2f09c1Sdp "%s -%c", badarg, optopt); 3933f2f09c1Sdp break; 3943f2f09c1Sdp } 3953f2f09c1Sdp } 3963f2f09c1Sdp 3973f2f09c1Sdp /* 3983f2f09c1Sdp * For Solaris Zones we warn about and discard non-option arguments. 3993f2f09c1Sdp * Hence 'boot foo bar baz gub' --> 'boot'. However, to be similar 4003f2f09c1Sdp * to the kernel, we concat up all the other remaining boot args. 4013f2f09c1Sdp * and warn on them as a group. 4023f2f09c1Sdp */ 4033f2f09c1Sdp if (optind < argc) { 4043f2f09c1Sdp err = Z_INVAL; 4053f2f09c1Sdp while (optind < argc) { 4063f2f09c1Sdp (void) snprintf(badarg, BOOTARGS_MAX, "%s%s%s", 4073f2f09c1Sdp badarg, strlen(badarg) > 0 ? " " : "", 4083f2f09c1Sdp argv[optind]); 4093f2f09c1Sdp optind++; 4103f2f09c1Sdp } 4113f2f09c1Sdp zerror(zlogp, B_FALSE, "WARNING: Unused or invalid boot " 4123f2f09c1Sdp "arguments `%s'.", badarg); 4133f2f09c1Sdp } 4143f2f09c1Sdp 4153f2f09c1Sdp done: 4163f2f09c1Sdp for (i = 0; i < argc_save; i++) { 4173f2f09c1Sdp if (argv_save[i] != NULL) 4183f2f09c1Sdp free(argv_save[i]); 4193f2f09c1Sdp } 4203f2f09c1Sdp free(argv_save); 4213f2f09c1Sdp return (err); 4223f2f09c1Sdp } 4233f2f09c1Sdp 4243f2f09c1Sdp 4257c478bd9Sstevel@tonic-gate static int 4267c478bd9Sstevel@tonic-gate mkzonedir(zlog_t *zlogp) 4277c478bd9Sstevel@tonic-gate { 4287c478bd9Sstevel@tonic-gate struct stat st; 4297c478bd9Sstevel@tonic-gate /* 4307c478bd9Sstevel@tonic-gate * We must create and lock everyone but root out of ZONES_TMPDIR 4317c478bd9Sstevel@tonic-gate * since anyone can open any UNIX domain socket, regardless of 4327c478bd9Sstevel@tonic-gate * its file system permissions. Sigh... 4337c478bd9Sstevel@tonic-gate */ 4347c478bd9Sstevel@tonic-gate if (mkdir(ZONES_TMPDIR, S_IRWXU) < 0 && errno != EEXIST) { 4357c478bd9Sstevel@tonic-gate zerror(zlogp, B_TRUE, "could not mkdir '%s'", ZONES_TMPDIR); 4367c478bd9Sstevel@tonic-gate return (-1); 4377c478bd9Sstevel@tonic-gate } 4387c478bd9Sstevel@tonic-gate /* paranoia */ 4394bc0a2efScasper if ((stat(ZONES_TMPDIR, &st) < 0) || !S_ISDIR(st.st_mode)) { 4407c478bd9Sstevel@tonic-gate zerror(zlogp, B_TRUE, "'%s' is not a directory", ZONES_TMPDIR); 4417c478bd9Sstevel@tonic-gate return (-1); 4427c478bd9Sstevel@tonic-gate } 4437c478bd9Sstevel@tonic-gate (void) chmod(ZONES_TMPDIR, S_IRWXU); 4447c478bd9Sstevel@tonic-gate return (0); 4457c478bd9Sstevel@tonic-gate } 4467c478bd9Sstevel@tonic-gate 447108322fbScarlsonj /* 448c5cd6260S * Run the brand's pre-state change callback, if it exists. 449c5cd6260S */ 450c5cd6260S static int 451c5cd6260S brand_prestatechg(zlog_t *zlogp, int state, int cmd) 452c5cd6260S { 453c5cd6260S char cmdbuf[2 * MAXPATHLEN]; 454c5cd6260S 455c5cd6260S if (pre_statechg_hook[0] == '\0') 456c5cd6260S return (0); 457c5cd6260S 458c5cd6260S if (snprintf(cmdbuf, sizeof (cmdbuf), "%s %d %d", pre_statechg_hook, 459c5cd6260S state, cmd) > sizeof (cmdbuf)) 460c5cd6260S return (-1); 461c5cd6260S 462c5cd6260S if (do_subproc(zlogp, cmdbuf, NULL) != 0) 463c5cd6260S return (-1); 464c5cd6260S 465c5cd6260S return (0); 466c5cd6260S } 467c5cd6260S 468c5cd6260S /* 469c5cd6260S * Run the brand's post-state change callback, if it exists. 470c5cd6260S */ 471c5cd6260S static int 472c5cd6260S brand_poststatechg(zlog_t *zlogp, int state, int cmd) 473c5cd6260S { 474c5cd6260S char cmdbuf[2 * MAXPATHLEN]; 475c5cd6260S 476c5cd6260S if (post_statechg_hook[0] == '\0') 477c5cd6260S return (0); 478c5cd6260S 479c5cd6260S if (snprintf(cmdbuf, sizeof (cmdbuf), "%s %d %d", post_statechg_hook, 480c5cd6260S state, cmd) > sizeof (cmdbuf)) 481c5cd6260S return (-1); 482c5cd6260S 483c5cd6260S if (do_subproc(zlogp, cmdbuf, NULL) != 0) 484c5cd6260S return (-1); 485c5cd6260S 486c5cd6260S return (0); 487c5cd6260S } 488c5cd6260S 489c5cd6260S /* 490108322fbScarlsonj * Bring a zone up to the pre-boot "ready" stage. The mount_cmd argument is 491108322fbScarlsonj * 'true' if this is being invoked as part of the processing for the "mount" 492108322fbScarlsonj * subcommand. 493108322fbScarlsonj */ 494108322fbScarlsonj static int 495c5cd6260S zone_ready(zlog_t *zlogp, zone_mnt_t mount_cmd, int zstate) 4967c478bd9Sstevel@tonic-gate { 4977c478bd9Sstevel@tonic-gate int err; 4987c478bd9Sstevel@tonic-gate 499c5cd6260S if (brand_prestatechg(zlogp, zstate, Z_READY) != 0) 500c5cd6260S return (-1); 501c5cd6260S 5027c478bd9Sstevel@tonic-gate if ((err = zonecfg_create_snapshot(zone_name)) != Z_OK) { 5037c478bd9Sstevel@tonic-gate zerror(zlogp, B_FALSE, "unable to create snapshot: %s", 5047c478bd9Sstevel@tonic-gate zonecfg_strerror(err)); 5057c478bd9Sstevel@tonic-gate return (-1); 5067c478bd9Sstevel@tonic-gate } 5077c478bd9Sstevel@tonic-gate 508108322fbScarlsonj if ((zone_id = vplat_create(zlogp, mount_cmd)) == -1) { 5097c478bd9Sstevel@tonic-gate if ((err = zonecfg_destroy_snapshot(zone_name)) != Z_OK) 5107c478bd9Sstevel@tonic-gate zerror(zlogp, B_FALSE, "destroying snapshot: %s", 5117c478bd9Sstevel@tonic-gate zonecfg_strerror(err)); 5127c478bd9Sstevel@tonic-gate return (-1); 5137c478bd9Sstevel@tonic-gate } 514555afedfScarlsonj if (vplat_bringup(zlogp, mount_cmd, zone_id) != 0) { 5157c478bd9Sstevel@tonic-gate bringup_failure_recovery = B_TRUE; 5166cfd72c6Sgjelinek (void) vplat_teardown(NULL, (mount_cmd != Z_MNT_BOOT), B_FALSE); 5177c478bd9Sstevel@tonic-gate if ((err = zonecfg_destroy_snapshot(zone_name)) != Z_OK) 5187c478bd9Sstevel@tonic-gate zerror(zlogp, B_FALSE, "destroying snapshot: %s", 5197c478bd9Sstevel@tonic-gate zonecfg_strerror(err)); 5207c478bd9Sstevel@tonic-gate return (-1); 5217c478bd9Sstevel@tonic-gate } 5227c478bd9Sstevel@tonic-gate 523c5cd6260S if (brand_poststatechg(zlogp, zstate, Z_READY) != 0) 524c5cd6260S return (-1); 525c5cd6260S 5267c478bd9Sstevel@tonic-gate return (0); 5277c478bd9Sstevel@tonic-gate } 5287c478bd9Sstevel@tonic-gate 529555afedfScarlsonj int 530555afedfScarlsonj init_template(void) 5317c478bd9Sstevel@tonic-gate { 5327c478bd9Sstevel@tonic-gate int fd; 5337c478bd9Sstevel@tonic-gate int err = 0; 5347c478bd9Sstevel@tonic-gate 5357c478bd9Sstevel@tonic-gate fd = open64(CTFS_ROOT "/process/template", O_RDWR); 5367c478bd9Sstevel@tonic-gate if (fd == -1) 5377c478bd9Sstevel@tonic-gate return (-1); 5387c478bd9Sstevel@tonic-gate 5397c478bd9Sstevel@tonic-gate /* 5407c478bd9Sstevel@tonic-gate * For now, zoneadmd doesn't do anything with the contract. 5417c478bd9Sstevel@tonic-gate * Deliver no events, don't inherit, and allow it to be orphaned. 5427c478bd9Sstevel@tonic-gate */ 5437c478bd9Sstevel@tonic-gate err |= ct_tmpl_set_critical(fd, 0); 5447c478bd9Sstevel@tonic-gate err |= ct_tmpl_set_informative(fd, 0); 5457c478bd9Sstevel@tonic-gate err |= ct_pr_tmpl_set_fatal(fd, CT_PR_EV_HWERR); 5467c478bd9Sstevel@tonic-gate err |= ct_pr_tmpl_set_param(fd, CT_PR_PGRPONLY | CT_PR_REGENT); 5477c478bd9Sstevel@tonic-gate if (err || ct_tmpl_activate(fd)) { 5487c478bd9Sstevel@tonic-gate (void) close(fd); 5497c478bd9Sstevel@tonic-gate return (-1); 5507c478bd9Sstevel@tonic-gate } 5517c478bd9Sstevel@tonic-gate 5527c478bd9Sstevel@tonic-gate return (fd); 5537c478bd9Sstevel@tonic-gate } 5547c478bd9Sstevel@tonic-gate 5559acbbeafSnn35248 typedef struct fs_callback { 5569acbbeafSnn35248 zlog_t *zlogp; 5579acbbeafSnn35248 zoneid_t zoneid; 558910f48daSedp boolean_t mount_cmd; 5599acbbeafSnn35248 } fs_callback_t; 5609acbbeafSnn35248 5617c478bd9Sstevel@tonic-gate static int 5629acbbeafSnn35248 mount_early_fs(void *data, const char *spec, const char *dir, 5639acbbeafSnn35248 const char *fstype, const char *opt) 5647c478bd9Sstevel@tonic-gate { 5659acbbeafSnn35248 zlog_t *zlogp = ((fs_callback_t *)data)->zlogp; 5669acbbeafSnn35248 zoneid_t zoneid = ((fs_callback_t *)data)->zoneid; 567910f48daSedp boolean_t mount_cmd = ((fs_callback_t *)data)->mount_cmd; 568d314f035Sedp char rootpath[MAXPATHLEN]; 5697c478bd9Sstevel@tonic-gate pid_t child; 5707c478bd9Sstevel@tonic-gate int child_status; 5717c478bd9Sstevel@tonic-gate int tmpl_fd; 572d314f035Sedp int rv; 5737c478bd9Sstevel@tonic-gate ctid_t ct; 5747c478bd9Sstevel@tonic-gate 575910f48daSedp /* determine the zone rootpath */ 576910f48daSedp if (mount_cmd) { 577910f48daSedp char zonepath[MAXPATHLEN]; 578910f48daSedp char luroot[MAXPATHLEN]; 579910f48daSedp 580910f48daSedp if (zone_get_zonepath(zone_name, 581910f48daSedp zonepath, sizeof (zonepath)) != Z_OK) { 582910f48daSedp zerror(zlogp, B_FALSE, "unable to determine zone path"); 583910f48daSedp return (-1); 584910f48daSedp } 585910f48daSedp 586910f48daSedp (void) snprintf(luroot, sizeof (luroot), "%s/lu", zonepath); 587910f48daSedp resolve_lofs(zlogp, luroot, sizeof (luroot)); 588910f48daSedp (void) strlcpy(rootpath, luroot, sizeof (rootpath)); 589910f48daSedp } else { 590910f48daSedp if (zone_get_rootpath(zone_name, 591910f48daSedp rootpath, sizeof (rootpath)) != Z_OK) { 592d314f035Sedp zerror(zlogp, B_FALSE, "unable to determine zone root"); 593d314f035Sedp return (-1); 594d314f035Sedp } 595910f48daSedp } 596d314f035Sedp 597d314f035Sedp if ((rv = valid_mount_path(zlogp, rootpath, spec, dir, fstype)) < 0) { 598d314f035Sedp zerror(zlogp, B_FALSE, "%s%s is not a valid mount point", 599d314f035Sedp rootpath, dir); 600d314f035Sedp return (-1); 601d314f035Sedp } else if (rv > 0) { 602d314f035Sedp /* The mount point path doesn't exist, create it now. */ 603d314f035Sedp if (make_one_dir(zlogp, rootpath, dir, 604d314f035Sedp DEFAULT_DIR_MODE, DEFAULT_DIR_USER, 605d314f035Sedp DEFAULT_DIR_GROUP) != 0) { 606d314f035Sedp zerror(zlogp, B_FALSE, "failed to create mount point"); 607d314f035Sedp return (-1); 608d314f035Sedp } 609d314f035Sedp 610d314f035Sedp /* 611d314f035Sedp * Now this might seem weird, but we need to invoke 612d314f035Sedp * valid_mount_path() again. Why? Because it checks 613d314f035Sedp * to make sure that the mount point path is canonical, 614d314f035Sedp * which it can only do if the path exists, so now that 615d314f035Sedp * we've created the path we have to verify it again. 616d314f035Sedp */ 617d314f035Sedp if ((rv = valid_mount_path(zlogp, rootpath, spec, dir, 618d314f035Sedp fstype)) < 0) { 619d314f035Sedp zerror(zlogp, B_FALSE, 620d314f035Sedp "%s%s is not a valid mount point", rootpath, dir); 621d314f035Sedp return (-1); 622d314f035Sedp } 623d314f035Sedp } 624d314f035Sedp 6257c478bd9Sstevel@tonic-gate if ((tmpl_fd = init_template()) == -1) { 6267c478bd9Sstevel@tonic-gate zerror(zlogp, B_TRUE, "failed to create contract"); 6277c478bd9Sstevel@tonic-gate return (-1); 6287c478bd9Sstevel@tonic-gate } 6297c478bd9Sstevel@tonic-gate 6307c478bd9Sstevel@tonic-gate if ((child = fork()) == -1) { 6317c478bd9Sstevel@tonic-gate (void) ct_tmpl_clear(tmpl_fd); 6327c478bd9Sstevel@tonic-gate (void) close(tmpl_fd); 6337c478bd9Sstevel@tonic-gate zerror(zlogp, B_TRUE, "failed to fork"); 6347c478bd9Sstevel@tonic-gate return (-1); 6357c478bd9Sstevel@tonic-gate 6367c478bd9Sstevel@tonic-gate } else if (child == 0) { /* child */ 6379acbbeafSnn35248 char opt_buf[MAX_MNTOPT_STR]; 6389acbbeafSnn35248 int optlen = 0; 6399acbbeafSnn35248 int mflag = MS_DATA; 6409acbbeafSnn35248 6417c478bd9Sstevel@tonic-gate (void) ct_tmpl_clear(tmpl_fd); 6427c478bd9Sstevel@tonic-gate /* 6437c478bd9Sstevel@tonic-gate * Even though there are no procs running in the zone, we 6447c478bd9Sstevel@tonic-gate * do this for paranoia's sake. 6457c478bd9Sstevel@tonic-gate */ 6467c478bd9Sstevel@tonic-gate (void) closefrom(0); 6477c478bd9Sstevel@tonic-gate 6487c478bd9Sstevel@tonic-gate if (zone_enter(zoneid) == -1) { 6497c478bd9Sstevel@tonic-gate _exit(errno); 6507c478bd9Sstevel@tonic-gate } 6519acbbeafSnn35248 if (opt != NULL) { 6529acbbeafSnn35248 /* 6539acbbeafSnn35248 * The mount() system call is incredibly annoying. 6549acbbeafSnn35248 * If options are specified, we need to copy them 6559acbbeafSnn35248 * into a temporary buffer since the mount() system 6569acbbeafSnn35248 * call will overwrite the options string. It will 6579acbbeafSnn35248 * also fail if the new option string it wants to 6589acbbeafSnn35248 * write is bigger than the one we passed in, so 6599acbbeafSnn35248 * you must pass in a buffer of the maximum possible 6609acbbeafSnn35248 * option string length. sigh. 6619acbbeafSnn35248 */ 6629acbbeafSnn35248 (void) strlcpy(opt_buf, opt, sizeof (opt_buf)); 6639acbbeafSnn35248 opt = opt_buf; 6649acbbeafSnn35248 optlen = MAX_MNTOPT_STR; 6659acbbeafSnn35248 mflag = MS_OPTIONSTR; 6669acbbeafSnn35248 } 6679acbbeafSnn35248 if (mount(spec, dir, mflag, fstype, NULL, 0, opt, optlen) != 0) 6687c478bd9Sstevel@tonic-gate _exit(errno); 6697c478bd9Sstevel@tonic-gate _exit(0); 6707c478bd9Sstevel@tonic-gate } 6717c478bd9Sstevel@tonic-gate 6727c478bd9Sstevel@tonic-gate /* parent */ 6737c478bd9Sstevel@tonic-gate if (contract_latest(&ct) == -1) 6747c478bd9Sstevel@tonic-gate ct = -1; 6757c478bd9Sstevel@tonic-gate (void) ct_tmpl_clear(tmpl_fd); 6767c478bd9Sstevel@tonic-gate (void) close(tmpl_fd); 6777c478bd9Sstevel@tonic-gate if (waitpid(child, &child_status, 0) != child) { 6787c478bd9Sstevel@tonic-gate /* unexpected: we must have been signalled */ 6797c478bd9Sstevel@tonic-gate (void) contract_abandon_id(ct); 6807c478bd9Sstevel@tonic-gate return (-1); 6817c478bd9Sstevel@tonic-gate } 6827c478bd9Sstevel@tonic-gate (void) contract_abandon_id(ct); 6837c478bd9Sstevel@tonic-gate if (WEXITSTATUS(child_status) != 0) { 6847c478bd9Sstevel@tonic-gate errno = WEXITSTATUS(child_status); 6857c478bd9Sstevel@tonic-gate zerror(zlogp, B_TRUE, "mount of %s failed", dir); 6867c478bd9Sstevel@tonic-gate return (-1); 6877c478bd9Sstevel@tonic-gate } 6887c478bd9Sstevel@tonic-gate 6897c478bd9Sstevel@tonic-gate return (0); 6907c478bd9Sstevel@tonic-gate } 6917c478bd9Sstevel@tonic-gate 692c5cd6260S /* 693c5cd6260S * If retstr is not NULL, the output of the subproc is returned in the str, 694c5cd6260S * otherwise it is output using zerror(). Any memory allocated for retstr 695c5cd6260S * should be freed by the caller. 696c5cd6260S */ 6979acbbeafSnn35248 int 698c5cd6260S do_subproc(zlog_t *zlogp, char *cmdbuf, char **retstr) 699108322fbScarlsonj { 700c5cd6260S char buf[1024]; /* arbitrary large amount */ 701c5cd6260S char *inbuf; 7029acbbeafSnn35248 FILE *file; 7039acbbeafSnn35248 int status; 704c5cd6260S int rd_cnt; 705c5cd6260S 706c5cd6260S if (retstr != NULL) { 707c5cd6260S if ((*retstr = malloc(1024)) == NULL) { 708c5cd6260S zerror(zlogp, B_FALSE, "out of memory"); 709c5cd6260S return (-1); 710c5cd6260S } 711c5cd6260S inbuf = *retstr; 712c5cd6260S rd_cnt = 0; 713c5cd6260S } else { 714c5cd6260S inbuf = buf; 715c5cd6260S } 716108322fbScarlsonj 7179acbbeafSnn35248 file = popen(cmdbuf, "r"); 7189acbbeafSnn35248 if (file == NULL) { 7199acbbeafSnn35248 zerror(zlogp, B_TRUE, "could not launch: %s", cmdbuf); 720108322fbScarlsonj return (-1); 7219acbbeafSnn35248 } 722108322fbScarlsonj 723c5cd6260S while (fgets(inbuf, 1024, file) != NULL) { 724c5cd6260S if (retstr == NULL && zlogp != &logsys) { 7259acbbeafSnn35248 zerror(zlogp, B_FALSE, "%s", inbuf); 726c5cd6260S } else { 727c5cd6260S char *p; 728c5cd6260S 729c5cd6260S rd_cnt += 1024 - 1; 730c5cd6260S if ((p = realloc(*retstr, rd_cnt + 1024)) == NULL) { 731c5cd6260S zerror(zlogp, B_FALSE, "out of memory"); 732c5cd6260S (void) pclose(file); 733c5cd6260S return (-1); 734c5cd6260S } 735c5cd6260S 736c5cd6260S *retstr = p; 737c5cd6260S inbuf = *retstr + rd_cnt; 738c5cd6260S } 739c5cd6260S } 7409acbbeafSnn35248 status = pclose(file); 7419acbbeafSnn35248 7429acbbeafSnn35248 if (WIFSIGNALED(status)) { 7439acbbeafSnn35248 zerror(zlogp, B_FALSE, "%s unexpectedly terminated due to " 7449acbbeafSnn35248 "signal %d", cmdbuf, WTERMSIG(status)); 7455518d15bSdp return (-1); 7469acbbeafSnn35248 } 7479acbbeafSnn35248 assert(WIFEXITED(status)); 7489acbbeafSnn35248 if (WEXITSTATUS(status) == ZEXIT_EXEC) { 7499acbbeafSnn35248 zerror(zlogp, B_FALSE, "failed to exec %s", cmdbuf); 750108322fbScarlsonj return (-1); 7519acbbeafSnn35248 } 7529acbbeafSnn35248 return (WEXITSTATUS(status)); 753108322fbScarlsonj } 754108322fbScarlsonj 755108322fbScarlsonj static int 756c5cd6260S zone_bootup(zlog_t *zlogp, const char *bootargs, int zstate) 7577c478bd9Sstevel@tonic-gate { 7587c478bd9Sstevel@tonic-gate zoneid_t zoneid; 7597c478bd9Sstevel@tonic-gate struct stat st; 760ff17c8bfSgjelinek char zpath[MAXPATHLEN], initpath[MAXPATHLEN], init_file[MAXPATHLEN]; 7613f2f09c1Sdp char nbootargs[BOOTARGS_MAX]; 7629acbbeafSnn35248 char cmdbuf[MAXPATHLEN]; 7639acbbeafSnn35248 fs_callback_t cb; 764123807fbSedp brand_handle_t bh; 7653f2f09c1Sdp int err; 7667c478bd9Sstevel@tonic-gate 767c5cd6260S if (brand_prestatechg(zlogp, zstate, Z_BOOT) != 0) 768c5cd6260S return (-1); 769c5cd6260S 7707c478bd9Sstevel@tonic-gate if ((zoneid = getzoneidbyname(zone_name)) == -1) { 7717c478bd9Sstevel@tonic-gate zerror(zlogp, B_TRUE, "unable to get zoneid"); 7727c478bd9Sstevel@tonic-gate return (-1); 7737c478bd9Sstevel@tonic-gate } 7747c478bd9Sstevel@tonic-gate 7759acbbeafSnn35248 cb.zlogp = zlogp; 7769acbbeafSnn35248 cb.zoneid = zoneid; 777910f48daSedp cb.mount_cmd = B_FALSE; 7789acbbeafSnn35248 7799acbbeafSnn35248 /* Get a handle to the brand info for this zone */ 780123807fbSedp if ((bh = brand_open(brand_name)) == NULL) { 7819acbbeafSnn35248 zerror(zlogp, B_FALSE, "unable to determine zone brand"); 7827c478bd9Sstevel@tonic-gate return (-1); 7839acbbeafSnn35248 } 7849acbbeafSnn35248 7859acbbeafSnn35248 /* 7869acbbeafSnn35248 * Get the list of filesystems to mount from the brand 7879acbbeafSnn35248 * configuration. These mounts are done via a thread that will 7889acbbeafSnn35248 * enter the zone, so they are done from within the context of the 7899acbbeafSnn35248 * zone. 7909acbbeafSnn35248 */ 791123807fbSedp if (brand_platform_iter_mounts(bh, mount_early_fs, &cb) != 0) { 7929acbbeafSnn35248 zerror(zlogp, B_FALSE, "unable to mount filesystems"); 793123807fbSedp brand_close(bh); 7949acbbeafSnn35248 return (-1); 7959acbbeafSnn35248 } 7969acbbeafSnn35248 7979acbbeafSnn35248 /* 7989acbbeafSnn35248 * Get the brand's boot callback if it exists. 7999acbbeafSnn35248 */ 800ff17c8bfSgjelinek if (zone_get_zonepath(zone_name, zpath, sizeof (zpath)) != Z_OK) { 801ff17c8bfSgjelinek zerror(zlogp, B_FALSE, "unable to determine zone path"); 802e767a340Sgjelinek brand_close(bh); 8039acbbeafSnn35248 return (-1); 8049acbbeafSnn35248 } 8059acbbeafSnn35248 (void) strcpy(cmdbuf, EXEC_PREFIX); 806ff17c8bfSgjelinek if (brand_get_boot(bh, zone_name, zpath, cmdbuf + EXEC_LEN, 807ff17c8bfSgjelinek sizeof (cmdbuf) - EXEC_LEN) != 0) { 8089acbbeafSnn35248 zerror(zlogp, B_FALSE, 8099acbbeafSnn35248 "unable to determine branded zone's boot callback"); 810123807fbSedp brand_close(bh); 8119acbbeafSnn35248 return (-1); 8129acbbeafSnn35248 } 8139acbbeafSnn35248 8149acbbeafSnn35248 /* Get the path for this zone's init(1M) (or equivalent) process. */ 815123807fbSedp if (brand_get_initname(bh, init_file, MAXPATHLEN) != 0) { 8169acbbeafSnn35248 zerror(zlogp, B_FALSE, 8179acbbeafSnn35248 "unable to determine zone's init(1M) location"); 818123807fbSedp brand_close(bh); 8199acbbeafSnn35248 return (-1); 8209acbbeafSnn35248 } 8219acbbeafSnn35248 822123807fbSedp brand_close(bh); 8237c478bd9Sstevel@tonic-gate 8243f2f09c1Sdp err = filter_bootargs(zlogp, bootargs, nbootargs, init_file, 8253f2f09c1Sdp bad_boot_arg); 8263f2f09c1Sdp if (err == Z_INVAL) 8273f2f09c1Sdp eventstream_write(Z_EVT_ZONE_BADARGS); 8283f2f09c1Sdp else if (err != Z_OK) 8293f2f09c1Sdp return (-1); 8303f2f09c1Sdp 8313f2f09c1Sdp assert(init_file[0] != '\0'); 8323f2f09c1Sdp 8339acbbeafSnn35248 /* Try to anticipate possible problems: Make sure init is executable. */ 834ff17c8bfSgjelinek if (zone_get_rootpath(zone_name, zpath, sizeof (zpath)) != Z_OK) { 8357c478bd9Sstevel@tonic-gate zerror(zlogp, B_FALSE, "unable to determine zone root"); 8367c478bd9Sstevel@tonic-gate return (-1); 8377c478bd9Sstevel@tonic-gate } 8389acbbeafSnn35248 839ff17c8bfSgjelinek (void) snprintf(initpath, sizeof (initpath), "%s%s", zpath, init_file); 8407c478bd9Sstevel@tonic-gate 8417c478bd9Sstevel@tonic-gate if (stat(initpath, &st) == -1) { 8427c478bd9Sstevel@tonic-gate zerror(zlogp, B_TRUE, "could not stat %s", initpath); 8437c478bd9Sstevel@tonic-gate return (-1); 8447c478bd9Sstevel@tonic-gate } 8457c478bd9Sstevel@tonic-gate 8467c478bd9Sstevel@tonic-gate if ((st.st_mode & S_IXUSR) == 0) { 8477c478bd9Sstevel@tonic-gate zerror(zlogp, B_FALSE, "%s is not executable", initpath); 8487c478bd9Sstevel@tonic-gate return (-1); 8497c478bd9Sstevel@tonic-gate } 8507c478bd9Sstevel@tonic-gate 8519acbbeafSnn35248 /* 8529acbbeafSnn35248 * If there is a brand 'boot' callback, execute it now to give the 8539acbbeafSnn35248 * brand one last chance to do any additional setup before the zone 8549acbbeafSnn35248 * is booted. 8559acbbeafSnn35248 */ 8569acbbeafSnn35248 if ((strlen(cmdbuf) > EXEC_LEN) && 857c5cd6260S (do_subproc(zlogp, cmdbuf, NULL) != Z_OK)) { 8589acbbeafSnn35248 zerror(zlogp, B_FALSE, "%s failed", cmdbuf); 8599acbbeafSnn35248 return (-1); 8609acbbeafSnn35248 } 8619acbbeafSnn35248 8623f2f09c1Sdp if (zone_setattr(zoneid, ZONE_ATTR_INITNAME, init_file, 0) == -1) { 8633f2f09c1Sdp zerror(zlogp, B_TRUE, "could not set zone boot file"); 8643f2f09c1Sdp return (-1); 8653f2f09c1Sdp } 8663f2f09c1Sdp 8673f2f09c1Sdp if (zone_setattr(zoneid, ZONE_ATTR_BOOTARGS, nbootargs, 0) == -1) { 8683f2f09c1Sdp zerror(zlogp, B_TRUE, "could not set zone boot arguments"); 8693f2f09c1Sdp return (-1); 8703f2f09c1Sdp } 8713f2f09c1Sdp 8723f2f09c1Sdp if (zone_boot(zoneid) == -1) { 8737c478bd9Sstevel@tonic-gate zerror(zlogp, B_TRUE, "unable to boot zone"); 8747c478bd9Sstevel@tonic-gate return (-1); 8757c478bd9Sstevel@tonic-gate } 8767c478bd9Sstevel@tonic-gate 877c5cd6260S if (brand_poststatechg(zlogp, zstate, Z_BOOT) != 0) 878c5cd6260S return (-1); 879c5cd6260S 8807c478bd9Sstevel@tonic-gate return (0); 8817c478bd9Sstevel@tonic-gate } 8827c478bd9Sstevel@tonic-gate 8837c478bd9Sstevel@tonic-gate static int 884c5cd6260S zone_halt(zlog_t *zlogp, boolean_t unmount_cmd, boolean_t rebooting, int zstate) 8857c478bd9Sstevel@tonic-gate { 8867c478bd9Sstevel@tonic-gate int err; 8877c478bd9Sstevel@tonic-gate 888c5cd6260S if (brand_prestatechg(zlogp, zstate, Z_HALT) != 0) 889c5cd6260S return (-1); 890c5cd6260S 8910209230bSgjelinek if (vplat_teardown(zlogp, unmount_cmd, rebooting) != 0) { 8927c478bd9Sstevel@tonic-gate if (!bringup_failure_recovery) 8937c478bd9Sstevel@tonic-gate zerror(zlogp, B_FALSE, "unable to destroy zone"); 8947c478bd9Sstevel@tonic-gate return (-1); 8957c478bd9Sstevel@tonic-gate } 8967c478bd9Sstevel@tonic-gate 8977c478bd9Sstevel@tonic-gate if ((err = zonecfg_destroy_snapshot(zone_name)) != Z_OK) 8987c478bd9Sstevel@tonic-gate zerror(zlogp, B_FALSE, "destroying snapshot: %s", 8997c478bd9Sstevel@tonic-gate zonecfg_strerror(err)); 9007c478bd9Sstevel@tonic-gate 901c5cd6260S if (brand_poststatechg(zlogp, zstate, Z_HALT) != 0) 902c5cd6260S return (-1); 903c5cd6260S 9047c478bd9Sstevel@tonic-gate return (0); 9057c478bd9Sstevel@tonic-gate } 9067c478bd9Sstevel@tonic-gate 9077c478bd9Sstevel@tonic-gate /* 9087c478bd9Sstevel@tonic-gate * Generate AUE_zone_state for a command that boots a zone. 9097c478bd9Sstevel@tonic-gate */ 9107c478bd9Sstevel@tonic-gate static void 9117c478bd9Sstevel@tonic-gate audit_put_record(zlog_t *zlogp, ucred_t *uc, int return_val, 9127c478bd9Sstevel@tonic-gate char *new_state) 9137c478bd9Sstevel@tonic-gate { 9147c478bd9Sstevel@tonic-gate adt_session_data_t *ah; 9157c478bd9Sstevel@tonic-gate adt_event_data_t *event; 9167c478bd9Sstevel@tonic-gate int pass_fail, fail_reason; 9177c478bd9Sstevel@tonic-gate 9187c478bd9Sstevel@tonic-gate if (!adt_audit_enabled()) 9197c478bd9Sstevel@tonic-gate return; 9207c478bd9Sstevel@tonic-gate 9217c478bd9Sstevel@tonic-gate if (return_val == 0) { 9227c478bd9Sstevel@tonic-gate pass_fail = ADT_SUCCESS; 9237c478bd9Sstevel@tonic-gate fail_reason = ADT_SUCCESS; 9247c478bd9Sstevel@tonic-gate } else { 9257c478bd9Sstevel@tonic-gate pass_fail = ADT_FAILURE; 9267c478bd9Sstevel@tonic-gate fail_reason = ADT_FAIL_VALUE_PROGRAM; 9277c478bd9Sstevel@tonic-gate } 9287c478bd9Sstevel@tonic-gate 9297c478bd9Sstevel@tonic-gate if (adt_start_session(&ah, NULL, 0)) { 9307c478bd9Sstevel@tonic-gate zerror(zlogp, B_TRUE, gettext("audit failure.")); 9317c478bd9Sstevel@tonic-gate return; 9327c478bd9Sstevel@tonic-gate } 9337c478bd9Sstevel@tonic-gate if (adt_set_from_ucred(ah, uc, ADT_NEW)) { 9347c478bd9Sstevel@tonic-gate zerror(zlogp, B_TRUE, gettext("audit failure.")); 9357c478bd9Sstevel@tonic-gate (void) adt_end_session(ah); 9367c478bd9Sstevel@tonic-gate return; 9377c478bd9Sstevel@tonic-gate } 9387c478bd9Sstevel@tonic-gate 9397c478bd9Sstevel@tonic-gate event = adt_alloc_event(ah, ADT_zone_state); 9407c478bd9Sstevel@tonic-gate if (event == NULL) { 9417c478bd9Sstevel@tonic-gate zerror(zlogp, B_TRUE, gettext("audit failure.")); 9427c478bd9Sstevel@tonic-gate (void) adt_end_session(ah); 9437c478bd9Sstevel@tonic-gate return; 9447c478bd9Sstevel@tonic-gate } 9457c478bd9Sstevel@tonic-gate event->adt_zone_state.zonename = zone_name; 9467c478bd9Sstevel@tonic-gate event->adt_zone_state.new_state = new_state; 9477c478bd9Sstevel@tonic-gate 9487c478bd9Sstevel@tonic-gate if (adt_put_event(event, pass_fail, fail_reason)) 9497c478bd9Sstevel@tonic-gate zerror(zlogp, B_TRUE, gettext("audit failure.")); 9507c478bd9Sstevel@tonic-gate 9517c478bd9Sstevel@tonic-gate adt_free_event(event); 9527c478bd9Sstevel@tonic-gate 9537c478bd9Sstevel@tonic-gate (void) adt_end_session(ah); 9547c478bd9Sstevel@tonic-gate } 9557c478bd9Sstevel@tonic-gate 9567c478bd9Sstevel@tonic-gate /* 9577c478bd9Sstevel@tonic-gate * The main routine for the door server that deals with zone state transitions. 9587c478bd9Sstevel@tonic-gate */ 9597c478bd9Sstevel@tonic-gate /* ARGSUSED */ 9607c478bd9Sstevel@tonic-gate static void 9617c478bd9Sstevel@tonic-gate server(void *cookie, char *args, size_t alen, door_desc_t *dp, 9627c478bd9Sstevel@tonic-gate uint_t n_desc) 9637c478bd9Sstevel@tonic-gate { 9647c478bd9Sstevel@tonic-gate ucred_t *uc = NULL; 9657c478bd9Sstevel@tonic-gate const priv_set_t *eset; 9667c478bd9Sstevel@tonic-gate 9677c478bd9Sstevel@tonic-gate zone_state_t zstate; 9687c478bd9Sstevel@tonic-gate zone_cmd_t cmd; 9697c478bd9Sstevel@tonic-gate zone_cmd_arg_t *zargp; 9707c478bd9Sstevel@tonic-gate 9717c478bd9Sstevel@tonic-gate boolean_t kernelcall; 9727c478bd9Sstevel@tonic-gate 9737c478bd9Sstevel@tonic-gate int rval = -1; 9747c478bd9Sstevel@tonic-gate uint64_t uniqid; 9757c478bd9Sstevel@tonic-gate zoneid_t zoneid = -1; 9767c478bd9Sstevel@tonic-gate zlog_t zlog; 9777c478bd9Sstevel@tonic-gate zlog_t *zlogp; 9787c478bd9Sstevel@tonic-gate zone_cmd_rval_t *rvalp; 9797c478bd9Sstevel@tonic-gate size_t rlen = getpagesize(); /* conservative */ 9809acbbeafSnn35248 fs_callback_t cb; 981123807fbSedp brand_handle_t bh; 9827c478bd9Sstevel@tonic-gate 9837c478bd9Sstevel@tonic-gate /* LINTED E_BAD_PTR_CAST_ALIGN */ 9847c478bd9Sstevel@tonic-gate zargp = (zone_cmd_arg_t *)args; 9857c478bd9Sstevel@tonic-gate 9867c478bd9Sstevel@tonic-gate /* 9877c478bd9Sstevel@tonic-gate * When we get the door unref message, we've fdetach'd the door, and 9887c478bd9Sstevel@tonic-gate * it is time for us to shut down zoneadmd. 9897c478bd9Sstevel@tonic-gate */ 9907c478bd9Sstevel@tonic-gate if (zargp == DOOR_UNREF_DATA) { 9917c478bd9Sstevel@tonic-gate /* 9927c478bd9Sstevel@tonic-gate * See comment at end of main() for info on the last rites. 9937c478bd9Sstevel@tonic-gate */ 9947c478bd9Sstevel@tonic-gate exit(0); 9957c478bd9Sstevel@tonic-gate } 9967c478bd9Sstevel@tonic-gate 9977c478bd9Sstevel@tonic-gate if (zargp == NULL) { 9987c478bd9Sstevel@tonic-gate (void) door_return(NULL, 0, 0, 0); 9997c478bd9Sstevel@tonic-gate } 10007c478bd9Sstevel@tonic-gate 10017c478bd9Sstevel@tonic-gate rvalp = alloca(rlen); 10027c478bd9Sstevel@tonic-gate bzero(rvalp, rlen); 10037c478bd9Sstevel@tonic-gate zlog.logfile = NULL; 10047c478bd9Sstevel@tonic-gate zlog.buflen = zlog.loglen = rlen - sizeof (zone_cmd_rval_t) + 1; 10057c478bd9Sstevel@tonic-gate zlog.buf = rvalp->errbuf; 10067c478bd9Sstevel@tonic-gate zlog.log = zlog.buf; 10077c478bd9Sstevel@tonic-gate /* defer initialization of zlog.locale until after credential check */ 10087c478bd9Sstevel@tonic-gate zlogp = &zlog; 10097c478bd9Sstevel@tonic-gate 10107c478bd9Sstevel@tonic-gate if (alen != sizeof (zone_cmd_arg_t)) { 10117c478bd9Sstevel@tonic-gate /* 10127c478bd9Sstevel@tonic-gate * This really shouldn't be happening. 10137c478bd9Sstevel@tonic-gate */ 10143f2f09c1Sdp zerror(&logsys, B_FALSE, "argument size (%d bytes) " 10153f2f09c1Sdp "unexpected (expected %d bytes)", alen, 10163f2f09c1Sdp sizeof (zone_cmd_arg_t)); 10177c478bd9Sstevel@tonic-gate goto out; 10187c478bd9Sstevel@tonic-gate } 10197c478bd9Sstevel@tonic-gate cmd = zargp->cmd; 10207c478bd9Sstevel@tonic-gate 10217c478bd9Sstevel@tonic-gate if (door_ucred(&uc) != 0) { 10227c478bd9Sstevel@tonic-gate zerror(&logsys, B_TRUE, "door_ucred"); 10237c478bd9Sstevel@tonic-gate goto out; 10247c478bd9Sstevel@tonic-gate } 10257c478bd9Sstevel@tonic-gate eset = ucred_getprivset(uc, PRIV_EFFECTIVE); 10267c478bd9Sstevel@tonic-gate if (ucred_getzoneid(uc) != GLOBAL_ZONEID || 10277c478bd9Sstevel@tonic-gate (eset != NULL ? !priv_ismember(eset, PRIV_SYS_CONFIG) : 10287c478bd9Sstevel@tonic-gate ucred_geteuid(uc) != 0)) { 10297c478bd9Sstevel@tonic-gate zerror(&logsys, B_FALSE, "insufficient privileges"); 10307c478bd9Sstevel@tonic-gate goto out; 10317c478bd9Sstevel@tonic-gate } 10327c478bd9Sstevel@tonic-gate 10337c478bd9Sstevel@tonic-gate kernelcall = ucred_getpid(uc) == 0; 10347c478bd9Sstevel@tonic-gate 10357c478bd9Sstevel@tonic-gate /* 10367c478bd9Sstevel@tonic-gate * This is safe because we only use a zlog_t throughout the 10377c478bd9Sstevel@tonic-gate * duration of a door call; i.e., by the time the pointer 10387c478bd9Sstevel@tonic-gate * might become invalid, the door call would be over. 10397c478bd9Sstevel@tonic-gate */ 10407c478bd9Sstevel@tonic-gate zlog.locale = kernelcall ? DEFAULT_LOCALE : zargp->locale; 10417c478bd9Sstevel@tonic-gate 10427c478bd9Sstevel@tonic-gate (void) mutex_lock(&lock); 10437c478bd9Sstevel@tonic-gate 10447c478bd9Sstevel@tonic-gate /* 10457c478bd9Sstevel@tonic-gate * Once we start to really die off, we don't want more connections. 10467c478bd9Sstevel@tonic-gate */ 10477c478bd9Sstevel@tonic-gate if (in_death_throes) { 10487c478bd9Sstevel@tonic-gate (void) mutex_unlock(&lock); 10497c478bd9Sstevel@tonic-gate ucred_free(uc); 10507c478bd9Sstevel@tonic-gate (void) door_return(NULL, 0, 0, 0); 10517c478bd9Sstevel@tonic-gate thr_exit(NULL); 10527c478bd9Sstevel@tonic-gate } 10537c478bd9Sstevel@tonic-gate 10547c478bd9Sstevel@tonic-gate /* 10557c478bd9Sstevel@tonic-gate * Check for validity of command. 10567c478bd9Sstevel@tonic-gate */ 10579acbbeafSnn35248 if (cmd != Z_READY && cmd != Z_BOOT && cmd != Z_FORCEBOOT && 10589acbbeafSnn35248 cmd != Z_REBOOT && cmd != Z_HALT && cmd != Z_NOTE_UNINSTALLING && 10599acbbeafSnn35248 cmd != Z_MOUNT && cmd != Z_FORCEMOUNT && cmd != Z_UNMOUNT) { 1060108322fbScarlsonj zerror(&logsys, B_FALSE, "invalid command %d", (int)cmd); 10617c478bd9Sstevel@tonic-gate goto out; 10627c478bd9Sstevel@tonic-gate } 10637c478bd9Sstevel@tonic-gate 10647c478bd9Sstevel@tonic-gate if (kernelcall && (cmd != Z_HALT && cmd != Z_REBOOT)) { 10657c478bd9Sstevel@tonic-gate /* 10667c478bd9Sstevel@tonic-gate * Can't happen 10677c478bd9Sstevel@tonic-gate */ 10687c478bd9Sstevel@tonic-gate zerror(&logsys, B_FALSE, "received unexpected kernel upcall %d", 10697c478bd9Sstevel@tonic-gate cmd); 10707c478bd9Sstevel@tonic-gate goto out; 10717c478bd9Sstevel@tonic-gate } 10727c478bd9Sstevel@tonic-gate /* 10737c478bd9Sstevel@tonic-gate * We ignore the possibility of someone calling zone_create(2) 10747c478bd9Sstevel@tonic-gate * explicitly; all requests must come through zoneadmd. 10757c478bd9Sstevel@tonic-gate */ 10767c478bd9Sstevel@tonic-gate if (zone_get_state(zone_name, &zstate) != Z_OK) { 10777c478bd9Sstevel@tonic-gate /* 10787c478bd9Sstevel@tonic-gate * Something terribly wrong happened 10797c478bd9Sstevel@tonic-gate */ 10807c478bd9Sstevel@tonic-gate zerror(&logsys, B_FALSE, "unable to determine state of zone"); 10817c478bd9Sstevel@tonic-gate goto out; 10827c478bd9Sstevel@tonic-gate } 10837c478bd9Sstevel@tonic-gate 10847c478bd9Sstevel@tonic-gate if (kernelcall) { 10857c478bd9Sstevel@tonic-gate /* 10867c478bd9Sstevel@tonic-gate * Kernel-initiated requests may lose their validity if the 10877c478bd9Sstevel@tonic-gate * zone_t the kernel was referring to has gone away. 10887c478bd9Sstevel@tonic-gate */ 10897c478bd9Sstevel@tonic-gate if ((zoneid = getzoneidbyname(zone_name)) == -1 || 10907c478bd9Sstevel@tonic-gate zone_getattr(zoneid, ZONE_ATTR_UNIQID, &uniqid, 10917c478bd9Sstevel@tonic-gate sizeof (uniqid)) == -1 || uniqid != zargp->uniqid) { 10927c478bd9Sstevel@tonic-gate /* 10937c478bd9Sstevel@tonic-gate * We're not talking about the same zone. The request 10947c478bd9Sstevel@tonic-gate * must have arrived too late. Return error. 10957c478bd9Sstevel@tonic-gate */ 10967c478bd9Sstevel@tonic-gate rval = -1; 10977c478bd9Sstevel@tonic-gate goto out; 10987c478bd9Sstevel@tonic-gate } 10997c478bd9Sstevel@tonic-gate zlogp = &logsys; /* Log errors to syslog */ 11007c478bd9Sstevel@tonic-gate } 11017c478bd9Sstevel@tonic-gate 11029acbbeafSnn35248 /* 11039acbbeafSnn35248 * If we are being asked to forcibly mount or boot a zone, we 11049acbbeafSnn35248 * pretend that an INCOMPLETE zone is actually INSTALLED. 11059acbbeafSnn35248 */ 11069acbbeafSnn35248 if (zstate == ZONE_STATE_INCOMPLETE && 11079acbbeafSnn35248 (cmd == Z_FORCEBOOT || cmd == Z_FORCEMOUNT)) 11089acbbeafSnn35248 zstate = ZONE_STATE_INSTALLED; 11099acbbeafSnn35248 11107c478bd9Sstevel@tonic-gate switch (zstate) { 11117c478bd9Sstevel@tonic-gate case ZONE_STATE_CONFIGURED: 11127c478bd9Sstevel@tonic-gate case ZONE_STATE_INCOMPLETE: 11137c478bd9Sstevel@tonic-gate /* 11147c478bd9Sstevel@tonic-gate * Not our area of expertise; we just print a nice message 11157c478bd9Sstevel@tonic-gate * and die off. 11167c478bd9Sstevel@tonic-gate */ 11177c478bd9Sstevel@tonic-gate zerror(zlogp, B_FALSE, 11187c478bd9Sstevel@tonic-gate "%s operation is invalid for zones in state '%s'", 1119108322fbScarlsonj z_cmd_name(cmd), zone_state_str(zstate)); 11207c478bd9Sstevel@tonic-gate break; 11217c478bd9Sstevel@tonic-gate 11227c478bd9Sstevel@tonic-gate case ZONE_STATE_INSTALLED: 11237c478bd9Sstevel@tonic-gate switch (cmd) { 11247c478bd9Sstevel@tonic-gate case Z_READY: 1125c5cd6260S rval = zone_ready(zlogp, Z_MNT_BOOT, zstate); 11267c478bd9Sstevel@tonic-gate if (rval == 0) 11277c478bd9Sstevel@tonic-gate eventstream_write(Z_EVT_ZONE_READIED); 11287c478bd9Sstevel@tonic-gate break; 11297c478bd9Sstevel@tonic-gate case Z_BOOT: 11309acbbeafSnn35248 case Z_FORCEBOOT: 11317c478bd9Sstevel@tonic-gate eventstream_write(Z_EVT_ZONE_BOOTING); 1132c5cd6260S if ((rval = zone_ready(zlogp, Z_MNT_BOOT, zstate)) 1133c5cd6260S == 0) { 1134c5cd6260S rval = zone_bootup(zlogp, zargp->bootbuf, 1135c5cd6260S zstate); 1136c5cd6260S } 11377c478bd9Sstevel@tonic-gate audit_put_record(zlogp, uc, rval, "boot"); 11387c478bd9Sstevel@tonic-gate if (rval != 0) { 11397c478bd9Sstevel@tonic-gate bringup_failure_recovery = B_TRUE; 1140c5cd6260S (void) zone_halt(zlogp, B_FALSE, B_FALSE, 1141c5cd6260S zstate); 1142ffbafc53Scomay eventstream_write(Z_EVT_ZONE_BOOTFAILED); 11437c478bd9Sstevel@tonic-gate } 11447c478bd9Sstevel@tonic-gate break; 11457c478bd9Sstevel@tonic-gate case Z_HALT: 11467c478bd9Sstevel@tonic-gate if (kernelcall) /* Invalid; can't happen */ 11477c478bd9Sstevel@tonic-gate abort(); 11487c478bd9Sstevel@tonic-gate /* 11497c478bd9Sstevel@tonic-gate * We could have two clients racing to halt this 11507c478bd9Sstevel@tonic-gate * zone; the second client loses, but his request 11517c478bd9Sstevel@tonic-gate * doesn't fail, since the zone is now in the desired 11527c478bd9Sstevel@tonic-gate * state. 11537c478bd9Sstevel@tonic-gate */ 11547c478bd9Sstevel@tonic-gate zerror(zlogp, B_FALSE, "zone is already halted"); 11557c478bd9Sstevel@tonic-gate rval = 0; 11567c478bd9Sstevel@tonic-gate break; 11577c478bd9Sstevel@tonic-gate case Z_REBOOT: 11587c478bd9Sstevel@tonic-gate if (kernelcall) /* Invalid; can't happen */ 11597c478bd9Sstevel@tonic-gate abort(); 11607c478bd9Sstevel@tonic-gate zerror(zlogp, B_FALSE, "%s operation is invalid " 1161108322fbScarlsonj "for zones in state '%s'", z_cmd_name(cmd), 11627c478bd9Sstevel@tonic-gate zone_state_str(zstate)); 11637c478bd9Sstevel@tonic-gate rval = -1; 11647c478bd9Sstevel@tonic-gate break; 11657c478bd9Sstevel@tonic-gate case Z_NOTE_UNINSTALLING: 11667c478bd9Sstevel@tonic-gate if (kernelcall) /* Invalid; can't happen */ 11677c478bd9Sstevel@tonic-gate abort(); 11687c478bd9Sstevel@tonic-gate /* 11697c478bd9Sstevel@tonic-gate * Tell the console to print out a message about this. 11707c478bd9Sstevel@tonic-gate * Once it does, we will be in_death_throes. 11717c478bd9Sstevel@tonic-gate */ 11727c478bd9Sstevel@tonic-gate eventstream_write(Z_EVT_ZONE_UNINSTALLING); 11737c478bd9Sstevel@tonic-gate break; 1174108322fbScarlsonj case Z_MOUNT: 11759acbbeafSnn35248 case Z_FORCEMOUNT: 1176108322fbScarlsonj if (kernelcall) /* Invalid; can't happen */ 1177108322fbScarlsonj abort(); 117884561e8cStd153743 if (!zone_isnative && !zone_iscluster) { 11790679f794S /* 11800679f794S * -U mounts the zone without lofs mounting 11810679f794S * zone file systems back into the scratch 11820679f794S * zone. This is required when mounting 11830679f794S * non-native branded zones. 11840679f794S */ 11850679f794S (void) strlcpy(zargp->bootbuf, "-U", 11860679f794S BOOTARGS_MAX); 1187ffbafc53Scomay } 1188ffbafc53Scomay 11896cfd72c6Sgjelinek rval = zone_ready(zlogp, 11906cfd72c6Sgjelinek strcmp(zargp->bootbuf, "-U") == 0 ? 1191c5cd6260S Z_MNT_UPDATE : Z_MNT_SCRATCH, zstate); 11929acbbeafSnn35248 if (rval != 0) 11939acbbeafSnn35248 break; 11949acbbeafSnn35248 11959acbbeafSnn35248 eventstream_write(Z_EVT_ZONE_READIED); 11969acbbeafSnn35248 11970679f794S /* 11980679f794S * Get a handle to the native brand info. 11990679f794S * We must always use the native brand file system 12000679f794S * list when mounting the zone. 12010679f794S */ 12020679f794S if ((bh = brand_open(NATIVE_BRAND_NAME)) == NULL) { 12039acbbeafSnn35248 rval = -1; 12049acbbeafSnn35248 break; 12059acbbeafSnn35248 } 12069acbbeafSnn35248 12079acbbeafSnn35248 /* 12089acbbeafSnn35248 * Get the list of filesystems to mount from 12099acbbeafSnn35248 * the brand configuration. These mounts are done 12109acbbeafSnn35248 * via a thread that will enter the zone, so they 12119acbbeafSnn35248 * are done from within the context of the zone. 12129acbbeafSnn35248 */ 12139acbbeafSnn35248 cb.zlogp = zlogp; 12149acbbeafSnn35248 cb.zoneid = zone_id; 1215910f48daSedp cb.mount_cmd = B_TRUE; 1216123807fbSedp rval = brand_platform_iter_mounts(bh, 12179acbbeafSnn35248 mount_early_fs, &cb); 12189acbbeafSnn35248 1219123807fbSedp brand_close(bh); 12209acbbeafSnn35248 1221108322fbScarlsonj /* 1222108322fbScarlsonj * Ordinarily, /dev/fd would be mounted inside the zone 1223108322fbScarlsonj * by svc:/system/filesystem/usr:default, but since 1224108322fbScarlsonj * we're not booting the zone, we need to do this 1225108322fbScarlsonj * manually. 1226108322fbScarlsonj */ 1227108322fbScarlsonj if (rval == 0) 12289acbbeafSnn35248 rval = mount_early_fs(&cb, 12299acbbeafSnn35248 "fd", "/dev/fd", "fd", NULL); 1230108322fbScarlsonj break; 1231108322fbScarlsonj case Z_UNMOUNT: 1232108322fbScarlsonj if (kernelcall) /* Invalid; can't happen */ 1233108322fbScarlsonj abort(); 1234108322fbScarlsonj zerror(zlogp, B_FALSE, "zone is already unmounted"); 1235108322fbScarlsonj rval = 0; 1236108322fbScarlsonj break; 12377c478bd9Sstevel@tonic-gate } 12387c478bd9Sstevel@tonic-gate break; 12397c478bd9Sstevel@tonic-gate 12407c478bd9Sstevel@tonic-gate case ZONE_STATE_READY: 12417c478bd9Sstevel@tonic-gate switch (cmd) { 12427c478bd9Sstevel@tonic-gate case Z_READY: 12437c478bd9Sstevel@tonic-gate /* 12447c478bd9Sstevel@tonic-gate * We could have two clients racing to ready this 12457c478bd9Sstevel@tonic-gate * zone; the second client loses, but his request 12467c478bd9Sstevel@tonic-gate * doesn't fail, since the zone is now in the desired 12477c478bd9Sstevel@tonic-gate * state. 12487c478bd9Sstevel@tonic-gate */ 12497c478bd9Sstevel@tonic-gate zerror(zlogp, B_FALSE, "zone is already ready"); 12507c478bd9Sstevel@tonic-gate rval = 0; 12517c478bd9Sstevel@tonic-gate break; 12527c478bd9Sstevel@tonic-gate case Z_BOOT: 12533f2f09c1Sdp (void) strlcpy(boot_args, zargp->bootbuf, 12543f2f09c1Sdp sizeof (boot_args)); 12557c478bd9Sstevel@tonic-gate eventstream_write(Z_EVT_ZONE_BOOTING); 1256c5cd6260S rval = zone_bootup(zlogp, zargp->bootbuf, zstate); 12577c478bd9Sstevel@tonic-gate audit_put_record(zlogp, uc, rval, "boot"); 12587c478bd9Sstevel@tonic-gate if (rval != 0) { 12597c478bd9Sstevel@tonic-gate bringup_failure_recovery = B_TRUE; 1260c5cd6260S (void) zone_halt(zlogp, B_FALSE, B_TRUE, 1261c5cd6260S zstate); 1262ffbafc53Scomay eventstream_write(Z_EVT_ZONE_BOOTFAILED); 12637c478bd9Sstevel@tonic-gate } 12643f2f09c1Sdp boot_args[0] = '\0'; 12657c478bd9Sstevel@tonic-gate break; 12667c478bd9Sstevel@tonic-gate case Z_HALT: 12677c478bd9Sstevel@tonic-gate if (kernelcall) /* Invalid; can't happen */ 12687c478bd9Sstevel@tonic-gate abort(); 1269c5cd6260S if ((rval = zone_halt(zlogp, B_FALSE, B_FALSE, zstate)) 1270c5cd6260S != 0) 12717c478bd9Sstevel@tonic-gate break; 12727c478bd9Sstevel@tonic-gate eventstream_write(Z_EVT_ZONE_HALTED); 12737c478bd9Sstevel@tonic-gate break; 12747c478bd9Sstevel@tonic-gate case Z_REBOOT: 1275108322fbScarlsonj case Z_NOTE_UNINSTALLING: 1276108322fbScarlsonj case Z_MOUNT: 1277108322fbScarlsonj case Z_UNMOUNT: 12787c478bd9Sstevel@tonic-gate if (kernelcall) /* Invalid; can't happen */ 12797c478bd9Sstevel@tonic-gate abort(); 12807c478bd9Sstevel@tonic-gate zerror(zlogp, B_FALSE, "%s operation is invalid " 1281108322fbScarlsonj "for zones in state '%s'", z_cmd_name(cmd), 12827c478bd9Sstevel@tonic-gate zone_state_str(zstate)); 12837c478bd9Sstevel@tonic-gate rval = -1; 12847c478bd9Sstevel@tonic-gate break; 1285108322fbScarlsonj } 1286108322fbScarlsonj break; 1287108322fbScarlsonj 1288108322fbScarlsonj case ZONE_STATE_MOUNTED: 1289108322fbScarlsonj switch (cmd) { 1290108322fbScarlsonj case Z_UNMOUNT: 12917c478bd9Sstevel@tonic-gate if (kernelcall) /* Invalid; can't happen */ 12927c478bd9Sstevel@tonic-gate abort(); 1293c5cd6260S rval = zone_halt(zlogp, B_TRUE, B_FALSE, zstate); 1294ffbafc53Scomay if (rval == 0) { 1295ffbafc53Scomay eventstream_write(Z_EVT_ZONE_HALTED); 1296108322fbScarlsonj (void) sema_post(&scratch_sem); 1297ffbafc53Scomay } 1298108322fbScarlsonj break; 1299108322fbScarlsonj default: 1300108322fbScarlsonj if (kernelcall) /* Invalid; can't happen */ 1301108322fbScarlsonj abort(); 1302108322fbScarlsonj zerror(zlogp, B_FALSE, "%s operation is invalid " 1303108322fbScarlsonj "for zones in state '%s'", z_cmd_name(cmd), 1304108322fbScarlsonj zone_state_str(zstate)); 13057c478bd9Sstevel@tonic-gate rval = -1; 13067c478bd9Sstevel@tonic-gate break; 13077c478bd9Sstevel@tonic-gate } 13087c478bd9Sstevel@tonic-gate break; 13097c478bd9Sstevel@tonic-gate 13107c478bd9Sstevel@tonic-gate case ZONE_STATE_RUNNING: 13117c478bd9Sstevel@tonic-gate case ZONE_STATE_SHUTTING_DOWN: 13127c478bd9Sstevel@tonic-gate case ZONE_STATE_DOWN: 13137c478bd9Sstevel@tonic-gate switch (cmd) { 13147c478bd9Sstevel@tonic-gate case Z_READY: 1315c5cd6260S if ((rval = zone_halt(zlogp, B_FALSE, B_TRUE, zstate)) 1316c5cd6260S != 0) 13177c478bd9Sstevel@tonic-gate break; 1318c5cd6260S if ((rval = zone_ready(zlogp, Z_MNT_BOOT, zstate)) == 0) 13197c478bd9Sstevel@tonic-gate eventstream_write(Z_EVT_ZONE_READIED); 1320ffbafc53Scomay else 1321ffbafc53Scomay eventstream_write(Z_EVT_ZONE_HALTED); 13227c478bd9Sstevel@tonic-gate break; 13237c478bd9Sstevel@tonic-gate case Z_BOOT: 13247c478bd9Sstevel@tonic-gate /* 13257c478bd9Sstevel@tonic-gate * We could have two clients racing to boot this 13267c478bd9Sstevel@tonic-gate * zone; the second client loses, but his request 13277c478bd9Sstevel@tonic-gate * doesn't fail, since the zone is now in the desired 13287c478bd9Sstevel@tonic-gate * state. 13297c478bd9Sstevel@tonic-gate */ 13307c478bd9Sstevel@tonic-gate zerror(zlogp, B_FALSE, "zone is already booted"); 13317c478bd9Sstevel@tonic-gate rval = 0; 13327c478bd9Sstevel@tonic-gate break; 13337c478bd9Sstevel@tonic-gate case Z_HALT: 1334c5cd6260S if ((rval = zone_halt(zlogp, B_FALSE, B_FALSE, zstate)) 1335c5cd6260S != 0) 13367c478bd9Sstevel@tonic-gate break; 13377c478bd9Sstevel@tonic-gate eventstream_write(Z_EVT_ZONE_HALTED); 13387c478bd9Sstevel@tonic-gate break; 13397c478bd9Sstevel@tonic-gate case Z_REBOOT: 13403f2f09c1Sdp (void) strlcpy(boot_args, zargp->bootbuf, 13413f2f09c1Sdp sizeof (boot_args)); 13427c478bd9Sstevel@tonic-gate eventstream_write(Z_EVT_ZONE_REBOOTING); 1343c5cd6260S if ((rval = zone_halt(zlogp, B_FALSE, B_TRUE, zstate)) 1344c5cd6260S != 0) { 1345ffbafc53Scomay eventstream_write(Z_EVT_ZONE_BOOTFAILED); 13463f2f09c1Sdp boot_args[0] = '\0'; 13477c478bd9Sstevel@tonic-gate break; 1348ffbafc53Scomay } 1349c5cd6260S if ((rval = zone_ready(zlogp, Z_MNT_BOOT, zstate)) 1350c5cd6260S != 0) { 1351ffbafc53Scomay eventstream_write(Z_EVT_ZONE_BOOTFAILED); 13523f2f09c1Sdp boot_args[0] = '\0'; 1353ffbafc53Scomay break; 1354ffbafc53Scomay } 1355c5cd6260S rval = zone_bootup(zlogp, zargp->bootbuf, zstate); 13567c478bd9Sstevel@tonic-gate audit_put_record(zlogp, uc, rval, "reboot"); 1357ffbafc53Scomay if (rval != 0) { 1358c5cd6260S (void) zone_halt(zlogp, B_FALSE, B_TRUE, 1359c5cd6260S zstate); 1360ffbafc53Scomay eventstream_write(Z_EVT_ZONE_BOOTFAILED); 13617c478bd9Sstevel@tonic-gate } 13623f2f09c1Sdp boot_args[0] = '\0'; 13637c478bd9Sstevel@tonic-gate break; 13647c478bd9Sstevel@tonic-gate case Z_NOTE_UNINSTALLING: 1365108322fbScarlsonj case Z_MOUNT: 1366108322fbScarlsonj case Z_UNMOUNT: 1367108322fbScarlsonj zerror(zlogp, B_FALSE, "%s operation is invalid " 1368108322fbScarlsonj "for zones in state '%s'", z_cmd_name(cmd), 1369108322fbScarlsonj zone_state_str(zstate)); 13707c478bd9Sstevel@tonic-gate rval = -1; 13717c478bd9Sstevel@tonic-gate break; 13727c478bd9Sstevel@tonic-gate } 13737c478bd9Sstevel@tonic-gate break; 13747c478bd9Sstevel@tonic-gate default: 13757c478bd9Sstevel@tonic-gate abort(); 13767c478bd9Sstevel@tonic-gate } 13777c478bd9Sstevel@tonic-gate 13787c478bd9Sstevel@tonic-gate /* 13797c478bd9Sstevel@tonic-gate * Because the state of the zone may have changed, we make sure 13807c478bd9Sstevel@tonic-gate * to wake the console poller, which is in charge of initiating 13817c478bd9Sstevel@tonic-gate * the shutdown procedure as necessary. 13827c478bd9Sstevel@tonic-gate */ 13837c478bd9Sstevel@tonic-gate eventstream_write(Z_EVT_NULL); 13847c478bd9Sstevel@tonic-gate 13857c478bd9Sstevel@tonic-gate out: 13867c478bd9Sstevel@tonic-gate (void) mutex_unlock(&lock); 13877c478bd9Sstevel@tonic-gate if (kernelcall) { 13887c478bd9Sstevel@tonic-gate rvalp = NULL; 13897c478bd9Sstevel@tonic-gate rlen = 0; 13907c478bd9Sstevel@tonic-gate } else { 13917c478bd9Sstevel@tonic-gate rvalp->rval = rval; 13927c478bd9Sstevel@tonic-gate } 13937c478bd9Sstevel@tonic-gate if (uc != NULL) 13947c478bd9Sstevel@tonic-gate ucred_free(uc); 13957c478bd9Sstevel@tonic-gate (void) door_return((char *)rvalp, rlen, NULL, 0); 13967c478bd9Sstevel@tonic-gate thr_exit(NULL); 13977c478bd9Sstevel@tonic-gate } 13987c478bd9Sstevel@tonic-gate 13997c478bd9Sstevel@tonic-gate static int 14007c478bd9Sstevel@tonic-gate setup_door(zlog_t *zlogp) 14017c478bd9Sstevel@tonic-gate { 14027c478bd9Sstevel@tonic-gate if ((zone_door = door_create(server, NULL, 14037c478bd9Sstevel@tonic-gate DOOR_UNREF | DOOR_REFUSE_DESC | DOOR_NO_CANCEL)) < 0) { 14047c478bd9Sstevel@tonic-gate zerror(zlogp, B_TRUE, "%s failed", "door_create"); 14057c478bd9Sstevel@tonic-gate return (-1); 14067c478bd9Sstevel@tonic-gate } 14077c478bd9Sstevel@tonic-gate (void) fdetach(zone_door_path); 14087c478bd9Sstevel@tonic-gate 14097c478bd9Sstevel@tonic-gate if (fattach(zone_door, zone_door_path) != 0) { 14107c478bd9Sstevel@tonic-gate zerror(zlogp, B_TRUE, "fattach to %s failed", zone_door_path); 14117c478bd9Sstevel@tonic-gate (void) door_revoke(zone_door); 14127c478bd9Sstevel@tonic-gate (void) fdetach(zone_door_path); 14137c478bd9Sstevel@tonic-gate zone_door = -1; 14147c478bd9Sstevel@tonic-gate return (-1); 14157c478bd9Sstevel@tonic-gate } 14167c478bd9Sstevel@tonic-gate return (0); 14177c478bd9Sstevel@tonic-gate } 14187c478bd9Sstevel@tonic-gate 14197c478bd9Sstevel@tonic-gate /* 14207c478bd9Sstevel@tonic-gate * zoneadm(1m) will start zoneadmd if it thinks it isn't running; this 14217c478bd9Sstevel@tonic-gate * is where zoneadmd itself will check to see that another instance of 14227c478bd9Sstevel@tonic-gate * zoneadmd isn't already controlling this zone. 14237c478bd9Sstevel@tonic-gate * 14247c478bd9Sstevel@tonic-gate * The idea here is that we want to open the path to which we will 14257c478bd9Sstevel@tonic-gate * attach our door, lock it, and then make sure that no-one has beat us 14267c478bd9Sstevel@tonic-gate * to fattach(3c)ing onto it. 14277c478bd9Sstevel@tonic-gate * 14287c478bd9Sstevel@tonic-gate * fattach(3c) is really a mount, so there are actually two possible 14297c478bd9Sstevel@tonic-gate * vnodes we could be dealing with. Our strategy is as follows: 14307c478bd9Sstevel@tonic-gate * 14317c478bd9Sstevel@tonic-gate * - If the file we opened is a regular file (common case): 14327c478bd9Sstevel@tonic-gate * There is no fattach(3c)ed door, so we have a chance of becoming 14337c478bd9Sstevel@tonic-gate * the managing zoneadmd. We attempt to lock the file: if it is 14347c478bd9Sstevel@tonic-gate * already locked, that means someone else raced us here, so we 14357c478bd9Sstevel@tonic-gate * lose and give up. zoneadm(1m) will try to contact the zoneadmd 14367c478bd9Sstevel@tonic-gate * that beat us to it. 14377c478bd9Sstevel@tonic-gate * 14387c478bd9Sstevel@tonic-gate * - If the file we opened is a namefs file: 14397c478bd9Sstevel@tonic-gate * This means there is already an established door fattach(3c)'ed 14407c478bd9Sstevel@tonic-gate * to the rendezvous path. We've lost the race, so we give up. 14417c478bd9Sstevel@tonic-gate * Note that in this case we also try to grab the file lock, and 14427c478bd9Sstevel@tonic-gate * will succeed in acquiring it since the vnode locked by the 14437c478bd9Sstevel@tonic-gate * "winning" zoneadmd was a regular one, and the one we locked was 14447c478bd9Sstevel@tonic-gate * the fattach(3c)'ed door node. At any rate, no harm is done, and 14457c478bd9Sstevel@tonic-gate * we just return to zoneadm(1m) which knows to retry. 14467c478bd9Sstevel@tonic-gate */ 14477c478bd9Sstevel@tonic-gate static int 14487c478bd9Sstevel@tonic-gate make_daemon_exclusive(zlog_t *zlogp) 14497c478bd9Sstevel@tonic-gate { 14507c478bd9Sstevel@tonic-gate int doorfd = -1; 14517c478bd9Sstevel@tonic-gate int err, ret = -1; 14527c478bd9Sstevel@tonic-gate struct stat st; 14537c478bd9Sstevel@tonic-gate struct flock flock; 14547c478bd9Sstevel@tonic-gate zone_state_t zstate; 14557c478bd9Sstevel@tonic-gate 14567c478bd9Sstevel@tonic-gate top: 14577c478bd9Sstevel@tonic-gate if ((err = zone_get_state(zone_name, &zstate)) != Z_OK) { 14583f2f09c1Sdp zerror(zlogp, B_FALSE, "failed to get zone state: %s", 14597c478bd9Sstevel@tonic-gate zonecfg_strerror(err)); 14607c478bd9Sstevel@tonic-gate goto out; 14617c478bd9Sstevel@tonic-gate } 14627c478bd9Sstevel@tonic-gate if ((doorfd = open(zone_door_path, O_CREAT|O_RDWR, 14637c478bd9Sstevel@tonic-gate S_IREAD|S_IWRITE)) < 0) { 14647c478bd9Sstevel@tonic-gate zerror(zlogp, B_TRUE, "failed to open %s", zone_door_path); 14657c478bd9Sstevel@tonic-gate goto out; 14667c478bd9Sstevel@tonic-gate } 14677c478bd9Sstevel@tonic-gate if (fstat(doorfd, &st) < 0) { 14687c478bd9Sstevel@tonic-gate zerror(zlogp, B_TRUE, "failed to stat %s", zone_door_path); 14697c478bd9Sstevel@tonic-gate goto out; 14707c478bd9Sstevel@tonic-gate } 14717c478bd9Sstevel@tonic-gate /* 14727c478bd9Sstevel@tonic-gate * Lock the file to synchronize with other zoneadmd 14737c478bd9Sstevel@tonic-gate */ 14747c478bd9Sstevel@tonic-gate flock.l_type = F_WRLCK; 14757c478bd9Sstevel@tonic-gate flock.l_whence = SEEK_SET; 14767c478bd9Sstevel@tonic-gate flock.l_start = (off_t)0; 14777c478bd9Sstevel@tonic-gate flock.l_len = (off_t)0; 14787c478bd9Sstevel@tonic-gate if (fcntl(doorfd, F_SETLK, &flock) < 0) { 14797c478bd9Sstevel@tonic-gate /* 14807c478bd9Sstevel@tonic-gate * Someone else raced us here and grabbed the lock file 14817c478bd9Sstevel@tonic-gate * first. A warning here is inappropriate since nothing 14827c478bd9Sstevel@tonic-gate * went wrong. 14837c478bd9Sstevel@tonic-gate */ 14847c478bd9Sstevel@tonic-gate goto out; 14857c478bd9Sstevel@tonic-gate } 14867c478bd9Sstevel@tonic-gate 14877c478bd9Sstevel@tonic-gate if (strcmp(st.st_fstype, "namefs") == 0) { 14887c478bd9Sstevel@tonic-gate struct door_info info; 14897c478bd9Sstevel@tonic-gate 14907c478bd9Sstevel@tonic-gate /* 14917c478bd9Sstevel@tonic-gate * There is already something fattach()'ed to this file. 14927c478bd9Sstevel@tonic-gate * Lets see what the door is up to. 14937c478bd9Sstevel@tonic-gate */ 14947c478bd9Sstevel@tonic-gate if (door_info(doorfd, &info) == 0 && info.di_target != -1) { 14957c478bd9Sstevel@tonic-gate /* 14967c478bd9Sstevel@tonic-gate * Another zoneadmd process seems to be in 14977c478bd9Sstevel@tonic-gate * control of the situation and we don't need to 14987c478bd9Sstevel@tonic-gate * be here. A warning here is inappropriate 14997c478bd9Sstevel@tonic-gate * since nothing went wrong. 15007c478bd9Sstevel@tonic-gate * 15017c478bd9Sstevel@tonic-gate * If the door has been revoked, the zoneadmd 15027c478bd9Sstevel@tonic-gate * process currently managing the zone is going 15037c478bd9Sstevel@tonic-gate * away. We'll return control to zoneadm(1m) 15047c478bd9Sstevel@tonic-gate * which will try again (by which time zoneadmd 15057c478bd9Sstevel@tonic-gate * will hopefully have exited). 15067c478bd9Sstevel@tonic-gate */ 15077c478bd9Sstevel@tonic-gate goto out; 15087c478bd9Sstevel@tonic-gate } 15097c478bd9Sstevel@tonic-gate 15107c478bd9Sstevel@tonic-gate /* 15117c478bd9Sstevel@tonic-gate * If we got this far, there's a fattach(3c)'ed door 15127c478bd9Sstevel@tonic-gate * that belongs to a process that has exited, which can 15137c478bd9Sstevel@tonic-gate * happen if the previous zoneadmd died unexpectedly. 15147c478bd9Sstevel@tonic-gate * 15157c478bd9Sstevel@tonic-gate * Let user know that something is amiss, but that we can 15167c478bd9Sstevel@tonic-gate * recover; if the zone is in the installed state, then don't 15177c478bd9Sstevel@tonic-gate * message, since having a running zoneadmd isn't really 15187c478bd9Sstevel@tonic-gate * expected/needed. We want to keep occurences of this message 15197c478bd9Sstevel@tonic-gate * limited to times when zoneadmd is picking back up from a 15207c478bd9Sstevel@tonic-gate * zoneadmd that died while the zone was in some non-trivial 15217c478bd9Sstevel@tonic-gate * state. 15227c478bd9Sstevel@tonic-gate */ 15237c478bd9Sstevel@tonic-gate if (zstate > ZONE_STATE_INSTALLED) { 15247c478bd9Sstevel@tonic-gate zerror(zlogp, B_FALSE, 15257c478bd9Sstevel@tonic-gate "zone '%s': WARNING: zone is in state '%s', but " 15267c478bd9Sstevel@tonic-gate "zoneadmd does not appear to be available; " 15277c478bd9Sstevel@tonic-gate "restarted zoneadmd to recover.", 15287c478bd9Sstevel@tonic-gate zone_name, zone_state_str(zstate)); 15297c478bd9Sstevel@tonic-gate } 15307c478bd9Sstevel@tonic-gate 15317c478bd9Sstevel@tonic-gate (void) fdetach(zone_door_path); 15327c478bd9Sstevel@tonic-gate (void) close(doorfd); 15337c478bd9Sstevel@tonic-gate goto top; 15347c478bd9Sstevel@tonic-gate } 15357c478bd9Sstevel@tonic-gate ret = 0; 15367c478bd9Sstevel@tonic-gate out: 15377c478bd9Sstevel@tonic-gate (void) close(doorfd); 15387c478bd9Sstevel@tonic-gate return (ret); 15397c478bd9Sstevel@tonic-gate } 15407c478bd9Sstevel@tonic-gate 1541c5cd6260S /* 1542c5cd6260S * Setup the brand's pre and post state change callbacks, as well as the 1543c5cd6260S * query callback, if any of these exist. 1544c5cd6260S */ 1545c5cd6260S static int 1546c5cd6260S brand_callback_init(brand_handle_t bh, char *zone_name) 1547c5cd6260S { 1548c5cd6260S char zpath[MAXPATHLEN]; 1549c5cd6260S 1550c5cd6260S if (zone_get_zonepath(zone_name, zpath, sizeof (zpath)) != Z_OK) 1551c5cd6260S return (-1); 1552c5cd6260S 1553c5cd6260S (void) strlcpy(pre_statechg_hook, EXEC_PREFIX, 1554c5cd6260S sizeof (pre_statechg_hook)); 1555c5cd6260S 1556c5cd6260S if (brand_get_prestatechange(bh, zone_name, zpath, 1557c5cd6260S pre_statechg_hook + EXEC_LEN, 1558c5cd6260S sizeof (pre_statechg_hook) - EXEC_LEN) != 0) 1559c5cd6260S return (-1); 1560c5cd6260S 1561c5cd6260S if (strlen(pre_statechg_hook) <= EXEC_LEN) 1562c5cd6260S pre_statechg_hook[0] = '\0'; 1563c5cd6260S 1564c5cd6260S (void) strlcpy(post_statechg_hook, EXEC_PREFIX, 1565c5cd6260S sizeof (post_statechg_hook)); 1566c5cd6260S 1567c5cd6260S if (brand_get_poststatechange(bh, zone_name, zpath, 1568c5cd6260S post_statechg_hook + EXEC_LEN, 1569c5cd6260S sizeof (post_statechg_hook) - EXEC_LEN) != 0) 1570c5cd6260S return (-1); 1571c5cd6260S 1572c5cd6260S if (strlen(post_statechg_hook) <= EXEC_LEN) 1573c5cd6260S post_statechg_hook[0] = '\0'; 1574c5cd6260S 1575c5cd6260S (void) strlcpy(query_hook, EXEC_PREFIX, 1576c5cd6260S sizeof (query_hook)); 1577c5cd6260S 1578c5cd6260S if (brand_get_query(bh, zone_name, zpath, query_hook + EXEC_LEN, 1579c5cd6260S sizeof (query_hook) - EXEC_LEN) != 0) 1580c5cd6260S return (-1); 1581c5cd6260S 1582c5cd6260S if (strlen(query_hook) <= EXEC_LEN) 1583c5cd6260S query_hook[0] = '\0'; 1584c5cd6260S 1585c5cd6260S return (0); 1586c5cd6260S } 1587c5cd6260S 15887c478bd9Sstevel@tonic-gate int 15897c478bd9Sstevel@tonic-gate main(int argc, char *argv[]) 15907c478bd9Sstevel@tonic-gate { 15917c478bd9Sstevel@tonic-gate int opt; 15927c478bd9Sstevel@tonic-gate zoneid_t zid; 15937c478bd9Sstevel@tonic-gate priv_set_t *privset; 15947c478bd9Sstevel@tonic-gate zone_state_t zstate; 15957c478bd9Sstevel@tonic-gate char parents_locale[MAXPATHLEN]; 1596123807fbSedp brand_handle_t bh; 15977c478bd9Sstevel@tonic-gate int err; 15987c478bd9Sstevel@tonic-gate 15997c478bd9Sstevel@tonic-gate pid_t pid; 16007c478bd9Sstevel@tonic-gate sigset_t blockset; 16017c478bd9Sstevel@tonic-gate sigset_t block_cld; 16027c478bd9Sstevel@tonic-gate 16037c478bd9Sstevel@tonic-gate struct { 16047c478bd9Sstevel@tonic-gate sema_t sem; 16057c478bd9Sstevel@tonic-gate int status; 16067c478bd9Sstevel@tonic-gate zlog_t log; 16077c478bd9Sstevel@tonic-gate } *shstate; 16087c478bd9Sstevel@tonic-gate size_t shstatelen = getpagesize(); 16097c478bd9Sstevel@tonic-gate 16107c478bd9Sstevel@tonic-gate zlog_t errlog; 16117c478bd9Sstevel@tonic-gate zlog_t *zlogp; 16127c478bd9Sstevel@tonic-gate 16135cb4571dSdp int ctfd; 16145cb4571dSdp 16157c478bd9Sstevel@tonic-gate progname = get_execbasename(argv[0]); 16167c478bd9Sstevel@tonic-gate 16177c478bd9Sstevel@tonic-gate /* 16187c478bd9Sstevel@tonic-gate * Make sure stderr is unbuffered 16197c478bd9Sstevel@tonic-gate */ 16207c478bd9Sstevel@tonic-gate (void) setbuffer(stderr, NULL, 0); 16217c478bd9Sstevel@tonic-gate 16227c478bd9Sstevel@tonic-gate /* 16237c478bd9Sstevel@tonic-gate * Get out of the way of mounted filesystems, since we will daemonize 16247c478bd9Sstevel@tonic-gate * soon. 16257c478bd9Sstevel@tonic-gate */ 16267c478bd9Sstevel@tonic-gate (void) chdir("/"); 16277c478bd9Sstevel@tonic-gate 16287c478bd9Sstevel@tonic-gate /* 16297c478bd9Sstevel@tonic-gate * Use the default system umask per PSARC 1998/110 rather than 16307c478bd9Sstevel@tonic-gate * anything that may have been set by the caller. 16317c478bd9Sstevel@tonic-gate */ 16327c478bd9Sstevel@tonic-gate (void) umask(CMASK); 16337c478bd9Sstevel@tonic-gate 16347c478bd9Sstevel@tonic-gate /* 16357c478bd9Sstevel@tonic-gate * Initially we want to use our parent's locale. 16367c478bd9Sstevel@tonic-gate */ 16377c478bd9Sstevel@tonic-gate (void) setlocale(LC_ALL, ""); 16387c478bd9Sstevel@tonic-gate (void) textdomain(TEXT_DOMAIN); 16397c478bd9Sstevel@tonic-gate (void) strlcpy(parents_locale, setlocale(LC_MESSAGES, NULL), 16407c478bd9Sstevel@tonic-gate sizeof (parents_locale)); 16417c478bd9Sstevel@tonic-gate 16427c478bd9Sstevel@tonic-gate /* 16437c478bd9Sstevel@tonic-gate * This zlog_t is used for writing to stderr 16447c478bd9Sstevel@tonic-gate */ 16457c478bd9Sstevel@tonic-gate errlog.logfile = stderr; 16467c478bd9Sstevel@tonic-gate errlog.buflen = errlog.loglen = 0; 16477c478bd9Sstevel@tonic-gate errlog.buf = errlog.log = NULL; 16487c478bd9Sstevel@tonic-gate errlog.locale = parents_locale; 16497c478bd9Sstevel@tonic-gate 16507c478bd9Sstevel@tonic-gate /* 16517c478bd9Sstevel@tonic-gate * We start off writing to stderr until we're ready to daemonize. 16527c478bd9Sstevel@tonic-gate */ 16537c478bd9Sstevel@tonic-gate zlogp = &errlog; 16547c478bd9Sstevel@tonic-gate 16557c478bd9Sstevel@tonic-gate /* 16567c478bd9Sstevel@tonic-gate * Process options. 16577c478bd9Sstevel@tonic-gate */ 1658108322fbScarlsonj while ((opt = getopt(argc, argv, "R:z:")) != EOF) { 16597c478bd9Sstevel@tonic-gate switch (opt) { 1660108322fbScarlsonj case 'R': 1661108322fbScarlsonj zonecfg_set_root(optarg); 1662108322fbScarlsonj break; 16637c478bd9Sstevel@tonic-gate case 'z': 16647c478bd9Sstevel@tonic-gate zone_name = optarg; 16657c478bd9Sstevel@tonic-gate break; 16667c478bd9Sstevel@tonic-gate default: 16677c478bd9Sstevel@tonic-gate usage(); 16687c478bd9Sstevel@tonic-gate } 16697c478bd9Sstevel@tonic-gate } 16707c478bd9Sstevel@tonic-gate 16717c478bd9Sstevel@tonic-gate if (zone_name == NULL) 16727c478bd9Sstevel@tonic-gate usage(); 16737c478bd9Sstevel@tonic-gate 16747c478bd9Sstevel@tonic-gate /* 16757c478bd9Sstevel@tonic-gate * Because usage() prints directly to stderr, it has gettext() 16767c478bd9Sstevel@tonic-gate * wrapping, which depends on the locale. But since zerror() calls 16777c478bd9Sstevel@tonic-gate * localize() which tweaks the locale, it is not safe to call zerror() 16787c478bd9Sstevel@tonic-gate * until after the last call to usage(). Fortunately, the last call 16797c478bd9Sstevel@tonic-gate * to usage() is just above and the first call to zerror() is just 16807c478bd9Sstevel@tonic-gate * below. Don't mess this up. 16817c478bd9Sstevel@tonic-gate */ 16827c478bd9Sstevel@tonic-gate if (strcmp(zone_name, GLOBAL_ZONENAME) == 0) { 16837c478bd9Sstevel@tonic-gate zerror(zlogp, B_FALSE, "cannot manage the %s zone", 16847c478bd9Sstevel@tonic-gate GLOBAL_ZONENAME); 16857c478bd9Sstevel@tonic-gate return (1); 16867c478bd9Sstevel@tonic-gate } 16877c478bd9Sstevel@tonic-gate 16887c478bd9Sstevel@tonic-gate if (zone_get_id(zone_name, &zid) != 0) { 16893f2f09c1Sdp zerror(zlogp, B_FALSE, "could not manage %s: %s", zone_name, 16907c478bd9Sstevel@tonic-gate zonecfg_strerror(Z_NO_ZONE)); 16917c478bd9Sstevel@tonic-gate return (1); 16927c478bd9Sstevel@tonic-gate } 16937c478bd9Sstevel@tonic-gate 16947c478bd9Sstevel@tonic-gate if ((err = zone_get_state(zone_name, &zstate)) != Z_OK) { 16953f2f09c1Sdp zerror(zlogp, B_FALSE, "failed to get zone state: %s", 16967c478bd9Sstevel@tonic-gate zonecfg_strerror(err)); 16977c478bd9Sstevel@tonic-gate return (1); 16987c478bd9Sstevel@tonic-gate } 16999acbbeafSnn35248 if (zstate < ZONE_STATE_INCOMPLETE) { 17007c478bd9Sstevel@tonic-gate zerror(zlogp, B_FALSE, 17017c478bd9Sstevel@tonic-gate "cannot manage a zone which is in state '%s'", 17027c478bd9Sstevel@tonic-gate zone_state_str(zstate)); 17037c478bd9Sstevel@tonic-gate return (1); 17047c478bd9Sstevel@tonic-gate } 17057c478bd9Sstevel@tonic-gate 17069acbbeafSnn35248 /* Get a handle to the brand info for this zone */ 17079acbbeafSnn35248 if ((zone_get_brand(zone_name, brand_name, sizeof (brand_name)) 1708123807fbSedp != Z_OK) || (bh = brand_open(brand_name)) == NULL) { 17099acbbeafSnn35248 zerror(zlogp, B_FALSE, "unable to determine zone brand"); 17109acbbeafSnn35248 return (1); 17119acbbeafSnn35248 } 1712123807fbSedp zone_isnative = brand_is_native(bh); 171384561e8cStd153743 zone_iscluster = (strcmp(brand_name, CLUSTER_BRAND_NAME) == 0); 1714c5cd6260S 1715c5cd6260S /* Get state change brand hooks. */ 1716c5cd6260S if (brand_callback_init(bh, zone_name) == -1) { 1717c5cd6260S zerror(zlogp, B_TRUE, 1718c5cd6260S "failed to initialize brand state change hooks"); 1719c5cd6260S brand_close(bh); 1720c5cd6260S return (1); 1721c5cd6260S } 1722c5cd6260S 1723123807fbSedp brand_close(bh); 17249acbbeafSnn35248 17257c478bd9Sstevel@tonic-gate /* 17267c478bd9Sstevel@tonic-gate * Check that we have all privileges. It would be nice to pare 17277c478bd9Sstevel@tonic-gate * this down, but this is at least a first cut. 17287c478bd9Sstevel@tonic-gate */ 17297c478bd9Sstevel@tonic-gate if ((privset = priv_allocset()) == NULL) { 17307c478bd9Sstevel@tonic-gate zerror(zlogp, B_TRUE, "%s failed", "priv_allocset"); 17317c478bd9Sstevel@tonic-gate return (1); 17327c478bd9Sstevel@tonic-gate } 17337c478bd9Sstevel@tonic-gate 17347c478bd9Sstevel@tonic-gate if (getppriv(PRIV_EFFECTIVE, privset) != 0) { 17357c478bd9Sstevel@tonic-gate zerror(zlogp, B_TRUE, "%s failed", "getppriv"); 17367c478bd9Sstevel@tonic-gate priv_freeset(privset); 17377c478bd9Sstevel@tonic-gate return (1); 17387c478bd9Sstevel@tonic-gate } 17397c478bd9Sstevel@tonic-gate 17407c478bd9Sstevel@tonic-gate if (priv_isfullset(privset) == B_FALSE) { 17417c478bd9Sstevel@tonic-gate zerror(zlogp, B_FALSE, "You lack sufficient privilege to " 17423f2f09c1Sdp "run this command (all privs required)"); 17437c478bd9Sstevel@tonic-gate priv_freeset(privset); 17447c478bd9Sstevel@tonic-gate return (1); 17457c478bd9Sstevel@tonic-gate } 17467c478bd9Sstevel@tonic-gate priv_freeset(privset); 17477c478bd9Sstevel@tonic-gate 17487c478bd9Sstevel@tonic-gate if (mkzonedir(zlogp) != 0) 17497c478bd9Sstevel@tonic-gate return (1); 17507c478bd9Sstevel@tonic-gate 17517c478bd9Sstevel@tonic-gate /* 17527c478bd9Sstevel@tonic-gate * Pre-fork: setup shared state 17537c478bd9Sstevel@tonic-gate */ 17547c478bd9Sstevel@tonic-gate if ((shstate = (void *)mmap(NULL, shstatelen, 17557c478bd9Sstevel@tonic-gate PROT_READ|PROT_WRITE, MAP_SHARED|MAP_ANON, -1, (off_t)0)) == 17567c478bd9Sstevel@tonic-gate MAP_FAILED) { 17577c478bd9Sstevel@tonic-gate zerror(zlogp, B_TRUE, "%s failed", "mmap"); 17587c478bd9Sstevel@tonic-gate return (1); 17597c478bd9Sstevel@tonic-gate } 17607c478bd9Sstevel@tonic-gate if (sema_init(&shstate->sem, 0, USYNC_PROCESS, NULL) != 0) { 17617c478bd9Sstevel@tonic-gate zerror(zlogp, B_TRUE, "%s failed", "sema_init()"); 17627c478bd9Sstevel@tonic-gate (void) munmap((char *)shstate, shstatelen); 17637c478bd9Sstevel@tonic-gate return (1); 17647c478bd9Sstevel@tonic-gate } 17657c478bd9Sstevel@tonic-gate shstate->log.logfile = NULL; 17667c478bd9Sstevel@tonic-gate shstate->log.buflen = shstatelen - sizeof (*shstate); 17677c478bd9Sstevel@tonic-gate shstate->log.loglen = shstate->log.buflen; 17687c478bd9Sstevel@tonic-gate shstate->log.buf = (char *)shstate + sizeof (*shstate); 17697c478bd9Sstevel@tonic-gate shstate->log.log = shstate->log.buf; 17707c478bd9Sstevel@tonic-gate shstate->log.locale = parents_locale; 17717c478bd9Sstevel@tonic-gate shstate->status = -1; 17727c478bd9Sstevel@tonic-gate 17737c478bd9Sstevel@tonic-gate /* 17747c478bd9Sstevel@tonic-gate * We need a SIGCHLD handler so the sema_wait() below will wake 17757c478bd9Sstevel@tonic-gate * up if the child dies without doing a sema_post(). 17767c478bd9Sstevel@tonic-gate */ 17777c478bd9Sstevel@tonic-gate (void) sigset(SIGCHLD, sigchld); 17787c478bd9Sstevel@tonic-gate /* 17797c478bd9Sstevel@tonic-gate * We must mask SIGCHLD until after we've coped with the fork 17807c478bd9Sstevel@tonic-gate * sufficiently to deal with it; otherwise we can race and 17817c478bd9Sstevel@tonic-gate * receive the signal before pid has been initialized 17827c478bd9Sstevel@tonic-gate * (yes, this really happens). 17837c478bd9Sstevel@tonic-gate */ 17847c478bd9Sstevel@tonic-gate (void) sigemptyset(&block_cld); 17857c478bd9Sstevel@tonic-gate (void) sigaddset(&block_cld, SIGCHLD); 17867c478bd9Sstevel@tonic-gate (void) sigprocmask(SIG_BLOCK, &block_cld, NULL); 17877c478bd9Sstevel@tonic-gate 17885cb4571dSdp if ((ctfd = init_template()) == -1) { 17895cb4571dSdp zerror(zlogp, B_TRUE, "failed to create contract"); 17905cb4571dSdp return (1); 17915cb4571dSdp } 17925cb4571dSdp 17937c478bd9Sstevel@tonic-gate /* 17947c478bd9Sstevel@tonic-gate * Do not let another thread localize a message while we are forking. 17957c478bd9Sstevel@tonic-gate */ 17967c478bd9Sstevel@tonic-gate (void) mutex_lock(&msglock); 17977c478bd9Sstevel@tonic-gate pid = fork(); 17987c478bd9Sstevel@tonic-gate (void) mutex_unlock(&msglock); 17995cb4571dSdp 18005cb4571dSdp /* 18015cb4571dSdp * In all cases (parent, child, and in the event of an error) we 18025cb4571dSdp * don't want to cause creation of contracts on subsequent fork()s. 18035cb4571dSdp */ 18045cb4571dSdp (void) ct_tmpl_clear(ctfd); 18055cb4571dSdp (void) close(ctfd); 18065cb4571dSdp 18077c478bd9Sstevel@tonic-gate if (pid == -1) { 18087c478bd9Sstevel@tonic-gate zerror(zlogp, B_TRUE, "could not fork"); 18097c478bd9Sstevel@tonic-gate return (1); 18107c478bd9Sstevel@tonic-gate 18117c478bd9Sstevel@tonic-gate } else if (pid > 0) { /* parent */ 18127c478bd9Sstevel@tonic-gate (void) sigprocmask(SIG_UNBLOCK, &block_cld, NULL); 18137c478bd9Sstevel@tonic-gate /* 18147c478bd9Sstevel@tonic-gate * This marks a window of vulnerability in which we receive 18157c478bd9Sstevel@tonic-gate * the SIGCLD before falling into sema_wait (normally we would 18167c478bd9Sstevel@tonic-gate * get woken up from sema_wait with EINTR upon receipt of 18177c478bd9Sstevel@tonic-gate * SIGCLD). So we may need to use some other scheme like 18187c478bd9Sstevel@tonic-gate * sema_posting in the sigcld handler. 18197c478bd9Sstevel@tonic-gate * blech 18207c478bd9Sstevel@tonic-gate */ 18217c478bd9Sstevel@tonic-gate (void) sema_wait(&shstate->sem); 18227c478bd9Sstevel@tonic-gate (void) sema_destroy(&shstate->sem); 18237c478bd9Sstevel@tonic-gate if (shstate->status != 0) 18247c478bd9Sstevel@tonic-gate (void) waitpid(pid, NULL, WNOHANG); 18257c478bd9Sstevel@tonic-gate /* 18267c478bd9Sstevel@tonic-gate * It's ok if we die with SIGPIPE. It's not like we could have 18277c478bd9Sstevel@tonic-gate * done anything about it. 18287c478bd9Sstevel@tonic-gate */ 18297c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "%s", shstate->log.buf); 18307c478bd9Sstevel@tonic-gate _exit(shstate->status == 0 ? 0 : 1); 18317c478bd9Sstevel@tonic-gate } 18327c478bd9Sstevel@tonic-gate 18337c478bd9Sstevel@tonic-gate /* 18347c478bd9Sstevel@tonic-gate * The child charges on. 18357c478bd9Sstevel@tonic-gate */ 18367c478bd9Sstevel@tonic-gate (void) sigset(SIGCHLD, SIG_DFL); 18377c478bd9Sstevel@tonic-gate (void) sigprocmask(SIG_UNBLOCK, &block_cld, NULL); 18387c478bd9Sstevel@tonic-gate 18397c478bd9Sstevel@tonic-gate /* 18407c478bd9Sstevel@tonic-gate * SIGPIPE can be delivered if we write to a socket for which the 18417c478bd9Sstevel@tonic-gate * peer endpoint is gone. That can lead to too-early termination 18427c478bd9Sstevel@tonic-gate * of zoneadmd, and that's not good eats. 18437c478bd9Sstevel@tonic-gate */ 18447c478bd9Sstevel@tonic-gate (void) sigset(SIGPIPE, SIG_IGN); 18457c478bd9Sstevel@tonic-gate /* 18467c478bd9Sstevel@tonic-gate * Stop using stderr 18477c478bd9Sstevel@tonic-gate */ 18487c478bd9Sstevel@tonic-gate zlogp = &shstate->log; 18497c478bd9Sstevel@tonic-gate 18507c478bd9Sstevel@tonic-gate /* 18517c478bd9Sstevel@tonic-gate * We don't need stdout/stderr from now on. 18527c478bd9Sstevel@tonic-gate */ 18537c478bd9Sstevel@tonic-gate closefrom(0); 18547c478bd9Sstevel@tonic-gate 18557c478bd9Sstevel@tonic-gate /* 18567c478bd9Sstevel@tonic-gate * Initialize the syslog zlog_t. This needs to be done after 18577c478bd9Sstevel@tonic-gate * the call to closefrom(). 18587c478bd9Sstevel@tonic-gate */ 18597c478bd9Sstevel@tonic-gate logsys.buf = logsys.log = NULL; 18607c478bd9Sstevel@tonic-gate logsys.buflen = logsys.loglen = 0; 18617c478bd9Sstevel@tonic-gate logsys.logfile = NULL; 18627c478bd9Sstevel@tonic-gate logsys.locale = DEFAULT_LOCALE; 18637c478bd9Sstevel@tonic-gate 18647c478bd9Sstevel@tonic-gate openlog("zoneadmd", LOG_PID, LOG_DAEMON); 18657c478bd9Sstevel@tonic-gate 18667c478bd9Sstevel@tonic-gate /* 18677c478bd9Sstevel@tonic-gate * The eventstream is used to publish state changes in the zone 18687c478bd9Sstevel@tonic-gate * from the door threads to the console I/O poller. 18697c478bd9Sstevel@tonic-gate */ 18707c478bd9Sstevel@tonic-gate if (eventstream_init() == -1) { 18717c478bd9Sstevel@tonic-gate zerror(zlogp, B_TRUE, "unable to create eventstream"); 18727c478bd9Sstevel@tonic-gate goto child_out; 18737c478bd9Sstevel@tonic-gate } 18747c478bd9Sstevel@tonic-gate 18757c478bd9Sstevel@tonic-gate (void) snprintf(zone_door_path, sizeof (zone_door_path), 1876108322fbScarlsonj "%s" ZONE_DOOR_PATH, zonecfg_get_root(), zone_name); 18777c478bd9Sstevel@tonic-gate 18787c478bd9Sstevel@tonic-gate /* 18797c478bd9Sstevel@tonic-gate * See if another zoneadmd is running for this zone. If not, then we 18807c478bd9Sstevel@tonic-gate * can now modify system state. 18817c478bd9Sstevel@tonic-gate */ 18827c478bd9Sstevel@tonic-gate if (make_daemon_exclusive(zlogp) == -1) 18837c478bd9Sstevel@tonic-gate goto child_out; 18847c478bd9Sstevel@tonic-gate 18857c478bd9Sstevel@tonic-gate 18867c478bd9Sstevel@tonic-gate /* 18877c478bd9Sstevel@tonic-gate * Create/join a new session; we need to be careful of what we do with 18887c478bd9Sstevel@tonic-gate * the console from now on so we don't end up being the session leader 18897c478bd9Sstevel@tonic-gate * for the terminal we're going to be handing out. 18907c478bd9Sstevel@tonic-gate */ 18917c478bd9Sstevel@tonic-gate (void) setsid(); 18927c478bd9Sstevel@tonic-gate 18937c478bd9Sstevel@tonic-gate /* 18947c478bd9Sstevel@tonic-gate * This thread shouldn't be receiving any signals; in particular, 18957c478bd9Sstevel@tonic-gate * SIGCHLD should be received by the thread doing the fork(). 18967c478bd9Sstevel@tonic-gate */ 18977c478bd9Sstevel@tonic-gate (void) sigfillset(&blockset); 18987c478bd9Sstevel@tonic-gate (void) thr_sigsetmask(SIG_BLOCK, &blockset, NULL); 18997c478bd9Sstevel@tonic-gate 19007c478bd9Sstevel@tonic-gate /* 19017c478bd9Sstevel@tonic-gate * Setup the console device and get ready to serve the console; 19027c478bd9Sstevel@tonic-gate * once this has completed, we're ready to let console clients 19037c478bd9Sstevel@tonic-gate * make an attempt to connect (they will block until 19047c478bd9Sstevel@tonic-gate * serve_console_sock() below gets called, and any pending 19057c478bd9Sstevel@tonic-gate * connection is accept()ed). 19067c478bd9Sstevel@tonic-gate */ 1907*9d5056eaSjv227347 if (!zonecfg_in_alt_root() && init_console(zlogp) < 0) 19087c478bd9Sstevel@tonic-gate goto child_out; 19097c478bd9Sstevel@tonic-gate 19107c478bd9Sstevel@tonic-gate /* 19117c478bd9Sstevel@tonic-gate * Take the lock now, so that when the door server gets going, we 19127c478bd9Sstevel@tonic-gate * are guaranteed that it won't take a request until we are sure 19137c478bd9Sstevel@tonic-gate * that everything is completely set up. See the child_out: label 19147c478bd9Sstevel@tonic-gate * below to see why this matters. 19157c478bd9Sstevel@tonic-gate */ 19167c478bd9Sstevel@tonic-gate (void) mutex_lock(&lock); 19177c478bd9Sstevel@tonic-gate 1918108322fbScarlsonj /* Init semaphore for scratch zones. */ 1919108322fbScarlsonj if (sema_init(&scratch_sem, 0, USYNC_THREAD, NULL) == -1) { 1920108322fbScarlsonj zerror(zlogp, B_TRUE, 1921108322fbScarlsonj "failed to initialize semaphore for scratch zone"); 1922108322fbScarlsonj goto child_out; 1923108322fbScarlsonj } 1924108322fbScarlsonj 19254ac67f02SAnurag S. Maskey /* open the dladm handle */ 19264ac67f02SAnurag S. Maskey if (dladm_open(&dld_handle) != DLADM_STATUS_OK) { 19274ac67f02SAnurag S. Maskey zerror(zlogp, B_FALSE, "failed to open dladm handle"); 19284ac67f02SAnurag S. Maskey goto child_out; 19294ac67f02SAnurag S. Maskey } 19304ac67f02SAnurag S. Maskey 19317c478bd9Sstevel@tonic-gate /* 19327c478bd9Sstevel@tonic-gate * Note: door setup must occur *after* the console is setup. 19337c478bd9Sstevel@tonic-gate * This is so that as zlogin tests the door to see if zoneadmd 19347c478bd9Sstevel@tonic-gate * is ready yet, we know that the console will get serviced 19357c478bd9Sstevel@tonic-gate * once door_info() indicates that the door is "up". 19367c478bd9Sstevel@tonic-gate */ 19377c478bd9Sstevel@tonic-gate if (setup_door(zlogp) == -1) 19387c478bd9Sstevel@tonic-gate goto child_out; 19397c478bd9Sstevel@tonic-gate 19407c478bd9Sstevel@tonic-gate /* 19417c478bd9Sstevel@tonic-gate * Things seem OK so far; tell the parent process that we're done 19427c478bd9Sstevel@tonic-gate * with setup tasks. This will cause the parent to exit, signalling 19437c478bd9Sstevel@tonic-gate * to zoneadm, zlogin, or whatever forked it that we are ready to 19447c478bd9Sstevel@tonic-gate * service requests. 19457c478bd9Sstevel@tonic-gate */ 19467c478bd9Sstevel@tonic-gate shstate->status = 0; 19477c478bd9Sstevel@tonic-gate (void) sema_post(&shstate->sem); 19487c478bd9Sstevel@tonic-gate (void) munmap((char *)shstate, shstatelen); 19497c478bd9Sstevel@tonic-gate shstate = NULL; 19507c478bd9Sstevel@tonic-gate 19517c478bd9Sstevel@tonic-gate (void) mutex_unlock(&lock); 19527c478bd9Sstevel@tonic-gate 19537c478bd9Sstevel@tonic-gate /* 19547c478bd9Sstevel@tonic-gate * zlogp is now invalid, so reset it to the syslog logger. 19557c478bd9Sstevel@tonic-gate */ 19567c478bd9Sstevel@tonic-gate zlogp = &logsys; 19577c478bd9Sstevel@tonic-gate 19587c478bd9Sstevel@tonic-gate /* 19597c478bd9Sstevel@tonic-gate * Now that we are free of any parents, switch to the default locale. 19607c478bd9Sstevel@tonic-gate */ 19617c478bd9Sstevel@tonic-gate (void) setlocale(LC_ALL, DEFAULT_LOCALE); 19627c478bd9Sstevel@tonic-gate 19637c478bd9Sstevel@tonic-gate /* 19647c478bd9Sstevel@tonic-gate * At this point the setup portion of main() is basically done, so 19657c478bd9Sstevel@tonic-gate * we reuse this thread to manage the zone console. When 19667c478bd9Sstevel@tonic-gate * serve_console() has returned, we are past the point of no return 19677c478bd9Sstevel@tonic-gate * in the life of this zoneadmd. 19687c478bd9Sstevel@tonic-gate */ 1969108322fbScarlsonj if (zonecfg_in_alt_root()) { 1970108322fbScarlsonj /* 1971108322fbScarlsonj * This is just awful, but mounted scratch zones don't (and 1972108322fbScarlsonj * can't) have consoles. We just wait for unmount instead. 1973108322fbScarlsonj */ 1974108322fbScarlsonj while (sema_wait(&scratch_sem) == EINTR) 1975108322fbScarlsonj ; 1976108322fbScarlsonj } else { 19777c478bd9Sstevel@tonic-gate serve_console(zlogp); 19787c478bd9Sstevel@tonic-gate assert(in_death_throes); 1979108322fbScarlsonj } 19807c478bd9Sstevel@tonic-gate 19817c478bd9Sstevel@tonic-gate /* 19827c478bd9Sstevel@tonic-gate * This is the next-to-last part of the exit interlock. Upon calling 19837c478bd9Sstevel@tonic-gate * fdetach(), the door will go unreferenced; once any 19847c478bd9Sstevel@tonic-gate * outstanding requests (like the door thread doing Z_HALT) are 19857c478bd9Sstevel@tonic-gate * done, the door will get an UNREF notification; when it handles 19867c478bd9Sstevel@tonic-gate * the UNREF, the door server will cause the exit. 19877c478bd9Sstevel@tonic-gate */ 19887c478bd9Sstevel@tonic-gate assert(!MUTEX_HELD(&lock)); 19897c478bd9Sstevel@tonic-gate (void) fdetach(zone_door_path); 19904ac67f02SAnurag S. Maskey 19917c478bd9Sstevel@tonic-gate for (;;) 19927c478bd9Sstevel@tonic-gate (void) pause(); 19937c478bd9Sstevel@tonic-gate 19947c478bd9Sstevel@tonic-gate child_out: 19957c478bd9Sstevel@tonic-gate assert(pid == 0); 19967c478bd9Sstevel@tonic-gate if (shstate != NULL) { 19977c478bd9Sstevel@tonic-gate shstate->status = -1; 19987c478bd9Sstevel@tonic-gate (void) sema_post(&shstate->sem); 19997c478bd9Sstevel@tonic-gate (void) munmap((char *)shstate, shstatelen); 20007c478bd9Sstevel@tonic-gate } 20017c478bd9Sstevel@tonic-gate 20027c478bd9Sstevel@tonic-gate /* 20037c478bd9Sstevel@tonic-gate * This might trigger an unref notification, but if so, 20047c478bd9Sstevel@tonic-gate * we are still holding the lock, so our call to exit will 20057c478bd9Sstevel@tonic-gate * ultimately win the race and will publish the right exit 20067c478bd9Sstevel@tonic-gate * code. 20077c478bd9Sstevel@tonic-gate */ 20087c478bd9Sstevel@tonic-gate if (zone_door != -1) { 20097c478bd9Sstevel@tonic-gate assert(MUTEX_HELD(&lock)); 20107c478bd9Sstevel@tonic-gate (void) door_revoke(zone_door); 20117c478bd9Sstevel@tonic-gate (void) fdetach(zone_door_path); 20127c478bd9Sstevel@tonic-gate } 20134ac67f02SAnurag S. Maskey 20144ac67f02SAnurag S. Maskey if (dld_handle != NULL) 20154ac67f02SAnurag S. Maskey dladm_close(dld_handle); 20164ac67f02SAnurag S. Maskey 20177c478bd9Sstevel@tonic-gate return (1); /* return from main() forcibly exits an MT process */ 20187c478bd9Sstevel@tonic-gate } 2019