1*0Sstevel@tonic-gate /* 2*0Sstevel@tonic-gate * CDDL HEADER START 3*0Sstevel@tonic-gate * 4*0Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*0Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 6*0Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 7*0Sstevel@tonic-gate * with the License. 8*0Sstevel@tonic-gate * 9*0Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10*0Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 11*0Sstevel@tonic-gate * See the License for the specific language governing permissions 12*0Sstevel@tonic-gate * and limitations under the License. 13*0Sstevel@tonic-gate * 14*0Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 15*0Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16*0Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 17*0Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 18*0Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 19*0Sstevel@tonic-gate * 20*0Sstevel@tonic-gate * CDDL HEADER END 21*0Sstevel@tonic-gate */ 22*0Sstevel@tonic-gate /* 23*0Sstevel@tonic-gate * Copyright 2005 Sun Microsystems, Inc. All rights reserved. 24*0Sstevel@tonic-gate * Use is subject to license terms. 25*0Sstevel@tonic-gate */ 26*0Sstevel@tonic-gate 27*0Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 28*0Sstevel@tonic-gate 29*0Sstevel@tonic-gate /* 30*0Sstevel@tonic-gate * zoneadm is a command interpreter for zone administration. It is all in 31*0Sstevel@tonic-gate * C (i.e., no lex/yacc), and all the argument passing is argc/argv based. 32*0Sstevel@tonic-gate * main() calls parse_and_run() which calls cmd_match(), then invokes the 33*0Sstevel@tonic-gate * appropriate command's handler function. The rest of the program is the 34*0Sstevel@tonic-gate * handler functions and their helper functions. 35*0Sstevel@tonic-gate * 36*0Sstevel@tonic-gate * Some of the helper functions are used largely to simplify I18N: reducing 37*0Sstevel@tonic-gate * the need for translation notes. This is particularly true of many of 38*0Sstevel@tonic-gate * the zerror() calls: doing e.g. zerror(gettext("%s failed"), "foo") rather 39*0Sstevel@tonic-gate * than zerror(gettext("foo failed")) with a translation note indicating 40*0Sstevel@tonic-gate * that "foo" need not be translated. 41*0Sstevel@tonic-gate */ 42*0Sstevel@tonic-gate 43*0Sstevel@tonic-gate #include <stdio.h> 44*0Sstevel@tonic-gate #include <errno.h> 45*0Sstevel@tonic-gate #include <unistd.h> 46*0Sstevel@tonic-gate #include <signal.h> 47*0Sstevel@tonic-gate #include <stdarg.h> 48*0Sstevel@tonic-gate #include <ctype.h> 49*0Sstevel@tonic-gate #include <stdlib.h> 50*0Sstevel@tonic-gate #include <string.h> 51*0Sstevel@tonic-gate #include <wait.h> 52*0Sstevel@tonic-gate #include <zone.h> 53*0Sstevel@tonic-gate #include <priv.h> 54*0Sstevel@tonic-gate #include <locale.h> 55*0Sstevel@tonic-gate #include <libintl.h> 56*0Sstevel@tonic-gate #include <libzonecfg.h> 57*0Sstevel@tonic-gate #include <bsm/adt.h> 58*0Sstevel@tonic-gate #include <sys/utsname.h> 59*0Sstevel@tonic-gate #include <sys/param.h> 60*0Sstevel@tonic-gate #include <sys/types.h> 61*0Sstevel@tonic-gate #include <sys/stat.h> 62*0Sstevel@tonic-gate #include <sys/statvfs.h> 63*0Sstevel@tonic-gate #include <assert.h> 64*0Sstevel@tonic-gate #include <sys/sockio.h> 65*0Sstevel@tonic-gate #include <sys/mntent.h> 66*0Sstevel@tonic-gate #include <limits.h> 67*0Sstevel@tonic-gate 68*0Sstevel@tonic-gate #include <fcntl.h> 69*0Sstevel@tonic-gate #include <door.h> 70*0Sstevel@tonic-gate #include <macros.h> 71*0Sstevel@tonic-gate #include <libgen.h> 72*0Sstevel@tonic-gate 73*0Sstevel@tonic-gate #include <pool.h> 74*0Sstevel@tonic-gate #include <sys/pool.h> 75*0Sstevel@tonic-gate 76*0Sstevel@tonic-gate #define MAXARGS 8 77*0Sstevel@tonic-gate 78*0Sstevel@tonic-gate /* Reflects kernel zone entries */ 79*0Sstevel@tonic-gate typedef struct zone_entry { 80*0Sstevel@tonic-gate zoneid_t zid; 81*0Sstevel@tonic-gate char zname[ZONENAME_MAX]; 82*0Sstevel@tonic-gate char *zstate_str; 83*0Sstevel@tonic-gate zone_state_t zstate_num; 84*0Sstevel@tonic-gate char zroot[MAXPATHLEN]; 85*0Sstevel@tonic-gate } zone_entry_t; 86*0Sstevel@tonic-gate 87*0Sstevel@tonic-gate static zone_entry_t *zents; 88*0Sstevel@tonic-gate static size_t nzents; 89*0Sstevel@tonic-gate 90*0Sstevel@tonic-gate #if !defined(TEXT_DOMAIN) /* should be defined by cc -D */ 91*0Sstevel@tonic-gate #define TEXT_DOMAIN "SYS_TEST" /* Use this only if it wasn't */ 92*0Sstevel@tonic-gate #endif 93*0Sstevel@tonic-gate 94*0Sstevel@tonic-gate #define Z_ERR 1 95*0Sstevel@tonic-gate #define Z_USAGE 2 96*0Sstevel@tonic-gate 97*0Sstevel@tonic-gate /* 0755 is the default directory mode. */ 98*0Sstevel@tonic-gate #define DEFAULT_DIR_MODE \ 99*0Sstevel@tonic-gate (S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) 100*0Sstevel@tonic-gate 101*0Sstevel@tonic-gate #define CMD_HELP 0 102*0Sstevel@tonic-gate #define CMD_BOOT 1 103*0Sstevel@tonic-gate #define CMD_HALT 2 104*0Sstevel@tonic-gate #define CMD_READY 3 105*0Sstevel@tonic-gate #define CMD_REBOOT 4 106*0Sstevel@tonic-gate #define CMD_LIST 5 107*0Sstevel@tonic-gate #define CMD_VERIFY 6 108*0Sstevel@tonic-gate #define CMD_INSTALL 7 109*0Sstevel@tonic-gate #define CMD_UNINSTALL 8 110*0Sstevel@tonic-gate 111*0Sstevel@tonic-gate #define CMD_MIN CMD_HELP 112*0Sstevel@tonic-gate #define CMD_MAX CMD_UNINSTALL 113*0Sstevel@tonic-gate 114*0Sstevel@tonic-gate struct cmd { 115*0Sstevel@tonic-gate uint_t cmd_num; /* command number */ 116*0Sstevel@tonic-gate char *cmd_name; /* command name */ 117*0Sstevel@tonic-gate char *short_usage; /* short form help */ 118*0Sstevel@tonic-gate int (*handler)(int argc, char *argv[]); /* function to call */ 119*0Sstevel@tonic-gate 120*0Sstevel@tonic-gate }; 121*0Sstevel@tonic-gate 122*0Sstevel@tonic-gate #define SHELP_HELP "help" 123*0Sstevel@tonic-gate #define SHELP_BOOT "boot [-s]" 124*0Sstevel@tonic-gate #define SHELP_HALT "halt" 125*0Sstevel@tonic-gate #define SHELP_READY "ready" 126*0Sstevel@tonic-gate #define SHELP_REBOOT "reboot" 127*0Sstevel@tonic-gate #define SHELP_LIST "list [-cipv]" 128*0Sstevel@tonic-gate #define SHELP_VERIFY "verify" 129*0Sstevel@tonic-gate #define SHELP_INSTALL "install" 130*0Sstevel@tonic-gate #define SHELP_UNINSTALL "uninstall [-F]" 131*0Sstevel@tonic-gate 132*0Sstevel@tonic-gate static int help_func(int argc, char *argv[]); 133*0Sstevel@tonic-gate static int ready_func(int argc, char *argv[]); 134*0Sstevel@tonic-gate static int boot_func(int argc, char *argv[]); 135*0Sstevel@tonic-gate static int halt_func(int argc, char *argv[]); 136*0Sstevel@tonic-gate static int reboot_func(int argc, char *argv[]); 137*0Sstevel@tonic-gate static int list_func(int argc, char *argv[]); 138*0Sstevel@tonic-gate static int verify_func(int argc, char *argv[]); 139*0Sstevel@tonic-gate static int install_func(int argc, char *argv[]); 140*0Sstevel@tonic-gate static int uninstall_func(int argc, char *argv[]); 141*0Sstevel@tonic-gate static int sanity_check(char *zone, int cmd_num, boolean_t running, 142*0Sstevel@tonic-gate boolean_t unsafe_when_running); 143*0Sstevel@tonic-gate static int cmd_match(char *cmd); 144*0Sstevel@tonic-gate static int verify_details(int); 145*0Sstevel@tonic-gate 146*0Sstevel@tonic-gate static struct cmd cmdtab[] = { 147*0Sstevel@tonic-gate { CMD_HELP, "help", SHELP_HELP, help_func }, 148*0Sstevel@tonic-gate { CMD_BOOT, "boot", SHELP_BOOT, boot_func }, 149*0Sstevel@tonic-gate { CMD_HALT, "halt", SHELP_HALT, halt_func }, 150*0Sstevel@tonic-gate { CMD_READY, "ready", SHELP_READY, ready_func }, 151*0Sstevel@tonic-gate { CMD_REBOOT, "reboot", SHELP_REBOOT, reboot_func }, 152*0Sstevel@tonic-gate { CMD_LIST, "list", SHELP_LIST, list_func }, 153*0Sstevel@tonic-gate { CMD_VERIFY, "verify", SHELP_VERIFY, verify_func }, 154*0Sstevel@tonic-gate { CMD_INSTALL, "install", SHELP_INSTALL, install_func }, 155*0Sstevel@tonic-gate { CMD_UNINSTALL, "uninstall", SHELP_UNINSTALL, 156*0Sstevel@tonic-gate uninstall_func } 157*0Sstevel@tonic-gate }; 158*0Sstevel@tonic-gate 159*0Sstevel@tonic-gate /* global variables */ 160*0Sstevel@tonic-gate 161*0Sstevel@tonic-gate /* set early in main(), never modified thereafter, used all over the place */ 162*0Sstevel@tonic-gate static char *execname; 163*0Sstevel@tonic-gate static char *target_zone; 164*0Sstevel@tonic-gate static char *locale; 165*0Sstevel@tonic-gate 166*0Sstevel@tonic-gate /* used in do_subproc() and signal handler */ 167*0Sstevel@tonic-gate static volatile boolean_t child_killed; 168*0Sstevel@tonic-gate 169*0Sstevel@tonic-gate static char * 170*0Sstevel@tonic-gate cmd_to_str(int cmd_num) 171*0Sstevel@tonic-gate { 172*0Sstevel@tonic-gate assert(cmd_num >= CMD_MIN && cmd_num <= CMD_MAX); 173*0Sstevel@tonic-gate return (cmdtab[cmd_num].cmd_name); 174*0Sstevel@tonic-gate } 175*0Sstevel@tonic-gate 176*0Sstevel@tonic-gate /* This is a separate function because of gettext() wrapping. */ 177*0Sstevel@tonic-gate static char * 178*0Sstevel@tonic-gate long_help(int cmd_num) 179*0Sstevel@tonic-gate { 180*0Sstevel@tonic-gate if (cmd_num < CMD_MIN || cmd_num > CMD_MAX) 181*0Sstevel@tonic-gate return (""); 182*0Sstevel@tonic-gate switch (cmd_num) { 183*0Sstevel@tonic-gate case CMD_HELP: 184*0Sstevel@tonic-gate return (gettext("Print usage message.")); 185*0Sstevel@tonic-gate case CMD_BOOT: 186*0Sstevel@tonic-gate return (gettext("Activates (boots) specified zone. " 187*0Sstevel@tonic-gate "The -s flag can be used\n\tto boot the zone in " 188*0Sstevel@tonic-gate "the single-user state.")); 189*0Sstevel@tonic-gate case CMD_HALT: 190*0Sstevel@tonic-gate return (gettext("Halts specified zone, bypassing " 191*0Sstevel@tonic-gate "shutdown scripts and removing runtime\n\t" 192*0Sstevel@tonic-gate "resources of the zone.")); 193*0Sstevel@tonic-gate case CMD_READY: 194*0Sstevel@tonic-gate return (gettext("Prepares a zone for running " 195*0Sstevel@tonic-gate "applications but does not start any user\n\t" 196*0Sstevel@tonic-gate "processes in the zone.")); 197*0Sstevel@tonic-gate case CMD_REBOOT: 198*0Sstevel@tonic-gate return (gettext("Restarts the zone (equivalent to a " 199*0Sstevel@tonic-gate "halt / boot sequence).\n\tFails if the zone is " 200*0Sstevel@tonic-gate "not active.")); 201*0Sstevel@tonic-gate case CMD_LIST: 202*0Sstevel@tonic-gate return (gettext("Lists the current zones, or a " 203*0Sstevel@tonic-gate "specific zone if indicated. By default,\n\tall " 204*0Sstevel@tonic-gate "running zones are listed, though this can be " 205*0Sstevel@tonic-gate "expanded to all\n\tinstalled zones with the -i " 206*0Sstevel@tonic-gate "option or all configured zones with the\n\t-c " 207*0Sstevel@tonic-gate "option. When used with the general -z <zone> " 208*0Sstevel@tonic-gate "option, lists only the\n\tspecified zone, but " 209*0Sstevel@tonic-gate "lists it regardless of its state, and the -i " 210*0Sstevel@tonic-gate "and -c\n\toptions are disallowed. The -v option " 211*0Sstevel@tonic-gate "can be used to display verbose\n\tinformation: " 212*0Sstevel@tonic-gate "zone name, id, current state, root directory and " 213*0Sstevel@tonic-gate "options.\n\tThe -p option can be used to request " 214*0Sstevel@tonic-gate "machine-parsable output. The -v\n\tand -p " 215*0Sstevel@tonic-gate "options are mutually exclusive. If neither -v " 216*0Sstevel@tonic-gate "nor -p is used,\n\tjust the zone name is " 217*0Sstevel@tonic-gate "listed.")); 218*0Sstevel@tonic-gate case CMD_VERIFY: 219*0Sstevel@tonic-gate return (gettext("Check to make sure the configuration " 220*0Sstevel@tonic-gate "can safely be instantiated\n\ton the machine: " 221*0Sstevel@tonic-gate "physical network interfaces exist, etc.")); 222*0Sstevel@tonic-gate case CMD_INSTALL: 223*0Sstevel@tonic-gate return (gettext("Install the configuration on to the " 224*0Sstevel@tonic-gate "system.")); 225*0Sstevel@tonic-gate case CMD_UNINSTALL: 226*0Sstevel@tonic-gate return (gettext("Uninstall the configuration from the " 227*0Sstevel@tonic-gate "system. The -F flag can be used\n\tto force the " 228*0Sstevel@tonic-gate "action.")); 229*0Sstevel@tonic-gate } 230*0Sstevel@tonic-gate /* NOTREACHED */ 231*0Sstevel@tonic-gate } 232*0Sstevel@tonic-gate 233*0Sstevel@tonic-gate /* 234*0Sstevel@tonic-gate * Called with explicit B_TRUE when help is explicitly requested, B_FALSE for 235*0Sstevel@tonic-gate * unexpected errors. 236*0Sstevel@tonic-gate */ 237*0Sstevel@tonic-gate 238*0Sstevel@tonic-gate static int 239*0Sstevel@tonic-gate usage(boolean_t explicit) 240*0Sstevel@tonic-gate { 241*0Sstevel@tonic-gate int i; 242*0Sstevel@tonic-gate FILE *fd = explicit ? stdout : stderr; 243*0Sstevel@tonic-gate 244*0Sstevel@tonic-gate (void) fprintf(fd, "%s:\t%s help\n", gettext("usage"), execname); 245*0Sstevel@tonic-gate (void) fprintf(fd, "\t%s [-z <zone>] list\n", execname); 246*0Sstevel@tonic-gate (void) fprintf(fd, "\t%s -z <zone> <%s>\n", execname, 247*0Sstevel@tonic-gate gettext("subcommand")); 248*0Sstevel@tonic-gate (void) fprintf(fd, "\n%s:\n\n", gettext("Subcommands")); 249*0Sstevel@tonic-gate for (i = CMD_MIN; i <= CMD_MAX; i++) { 250*0Sstevel@tonic-gate (void) fprintf(fd, "%s\n", cmdtab[i].short_usage); 251*0Sstevel@tonic-gate if (explicit) 252*0Sstevel@tonic-gate (void) fprintf(fd, "\t%s\n\n", long_help(i)); 253*0Sstevel@tonic-gate } 254*0Sstevel@tonic-gate if (!explicit) 255*0Sstevel@tonic-gate (void) fputs("\n", fd); 256*0Sstevel@tonic-gate return (Z_USAGE); 257*0Sstevel@tonic-gate } 258*0Sstevel@tonic-gate 259*0Sstevel@tonic-gate static void 260*0Sstevel@tonic-gate sub_usage(char *short_usage, int cmd_num) 261*0Sstevel@tonic-gate { 262*0Sstevel@tonic-gate (void) fprintf(stderr, "%s:\t%s\n", gettext("usage"), short_usage); 263*0Sstevel@tonic-gate (void) fprintf(stderr, "\t%s\n", long_help(cmd_num)); 264*0Sstevel@tonic-gate } 265*0Sstevel@tonic-gate 266*0Sstevel@tonic-gate /* 267*0Sstevel@tonic-gate * zperror() is like perror(3c) except that this also prints the executable 268*0Sstevel@tonic-gate * name at the start of the message, and takes a boolean indicating whether 269*0Sstevel@tonic-gate * to call libc'c strerror() or that from libzonecfg. 270*0Sstevel@tonic-gate */ 271*0Sstevel@tonic-gate 272*0Sstevel@tonic-gate static void 273*0Sstevel@tonic-gate zperror(const char *str, boolean_t zonecfg_error) 274*0Sstevel@tonic-gate { 275*0Sstevel@tonic-gate (void) fprintf(stderr, "%s: %s: %s\n", execname, str, 276*0Sstevel@tonic-gate zonecfg_error ? zonecfg_strerror(errno) : strerror(errno)); 277*0Sstevel@tonic-gate } 278*0Sstevel@tonic-gate 279*0Sstevel@tonic-gate /* 280*0Sstevel@tonic-gate * zperror2() is very similar to zperror() above, except it also prints a 281*0Sstevel@tonic-gate * supplied zone name after the executable. 282*0Sstevel@tonic-gate * 283*0Sstevel@tonic-gate * All current consumers of this function want libzonecfg's strerror() rather 284*0Sstevel@tonic-gate * than libc's; if this ever changes, this function can be made more generic 285*0Sstevel@tonic-gate * like zperror() above. 286*0Sstevel@tonic-gate */ 287*0Sstevel@tonic-gate 288*0Sstevel@tonic-gate static void 289*0Sstevel@tonic-gate zperror2(const char *zone, const char *str) 290*0Sstevel@tonic-gate { 291*0Sstevel@tonic-gate (void) fprintf(stderr, "%s: %s: %s: %s\n", execname, zone, str, 292*0Sstevel@tonic-gate zonecfg_strerror(errno)); 293*0Sstevel@tonic-gate } 294*0Sstevel@tonic-gate 295*0Sstevel@tonic-gate /* PRINTFLIKE1 */ 296*0Sstevel@tonic-gate static void 297*0Sstevel@tonic-gate zerror(const char *fmt, ...) 298*0Sstevel@tonic-gate { 299*0Sstevel@tonic-gate va_list alist; 300*0Sstevel@tonic-gate 301*0Sstevel@tonic-gate va_start(alist, fmt); 302*0Sstevel@tonic-gate (void) fprintf(stderr, "%s: ", execname); 303*0Sstevel@tonic-gate if (target_zone != NULL) 304*0Sstevel@tonic-gate (void) fprintf(stderr, "zone '%s': ", target_zone); 305*0Sstevel@tonic-gate (void) vfprintf(stderr, fmt, alist); 306*0Sstevel@tonic-gate (void) fprintf(stderr, "\n"); 307*0Sstevel@tonic-gate va_end(alist); 308*0Sstevel@tonic-gate } 309*0Sstevel@tonic-gate 310*0Sstevel@tonic-gate static void * 311*0Sstevel@tonic-gate safe_calloc(size_t nelem, size_t elsize) 312*0Sstevel@tonic-gate { 313*0Sstevel@tonic-gate void *r = calloc(nelem, elsize); 314*0Sstevel@tonic-gate 315*0Sstevel@tonic-gate if (r == NULL) { 316*0Sstevel@tonic-gate zerror(gettext("failed to allocate %lu bytes: %s"), 317*0Sstevel@tonic-gate (ulong_t)nelem * elsize, strerror(errno)); 318*0Sstevel@tonic-gate exit(Z_ERR); 319*0Sstevel@tonic-gate } 320*0Sstevel@tonic-gate return (r); 321*0Sstevel@tonic-gate } 322*0Sstevel@tonic-gate 323*0Sstevel@tonic-gate static void 324*0Sstevel@tonic-gate zone_print(zone_entry_t *zent, boolean_t verbose, boolean_t parsable) 325*0Sstevel@tonic-gate { 326*0Sstevel@tonic-gate static boolean_t firsttime = B_TRUE; 327*0Sstevel@tonic-gate 328*0Sstevel@tonic-gate assert(!(verbose && parsable)); 329*0Sstevel@tonic-gate if (firsttime && verbose) { 330*0Sstevel@tonic-gate firsttime = B_FALSE; 331*0Sstevel@tonic-gate (void) printf("%*s %-16s %-14s %-30s\n", ZONEID_WIDTH, "ID", 332*0Sstevel@tonic-gate "NAME", "STATUS", "PATH"); 333*0Sstevel@tonic-gate } 334*0Sstevel@tonic-gate if (!verbose) { 335*0Sstevel@tonic-gate if (!parsable) { 336*0Sstevel@tonic-gate (void) printf("%s\n", zent->zname); 337*0Sstevel@tonic-gate return; 338*0Sstevel@tonic-gate } 339*0Sstevel@tonic-gate if (zent->zid == ZONE_ID_UNDEFINED) 340*0Sstevel@tonic-gate (void) printf("-"); 341*0Sstevel@tonic-gate else 342*0Sstevel@tonic-gate (void) printf("%lu", zent->zid); 343*0Sstevel@tonic-gate (void) printf(":%s:%s:%s\n", zent->zname, zent->zstate_str, 344*0Sstevel@tonic-gate zent->zroot); 345*0Sstevel@tonic-gate return; 346*0Sstevel@tonic-gate } 347*0Sstevel@tonic-gate if (zent->zstate_str != NULL) { 348*0Sstevel@tonic-gate if (zent->zid == ZONE_ID_UNDEFINED) 349*0Sstevel@tonic-gate (void) printf("%*s", ZONEID_WIDTH, "-"); 350*0Sstevel@tonic-gate else 351*0Sstevel@tonic-gate (void) printf("%*lu", ZONEID_WIDTH, zent->zid); 352*0Sstevel@tonic-gate (void) printf(" %-16s %-14s %-30s\n", zent->zname, 353*0Sstevel@tonic-gate zent->zstate_str, zent->zroot); 354*0Sstevel@tonic-gate } 355*0Sstevel@tonic-gate } 356*0Sstevel@tonic-gate 357*0Sstevel@tonic-gate static int 358*0Sstevel@tonic-gate lookup_zone_info(char *zone_name, zone_entry_t *zent) 359*0Sstevel@tonic-gate { 360*0Sstevel@tonic-gate char root[MAXPATHLEN]; 361*0Sstevel@tonic-gate int err; 362*0Sstevel@tonic-gate 363*0Sstevel@tonic-gate (void) strlcpy(zent->zname, zone_name, sizeof (zent->zname)); 364*0Sstevel@tonic-gate (void) strlcpy(zent->zroot, "???", sizeof (zent->zroot)); 365*0Sstevel@tonic-gate zent->zstate_str = "???"; 366*0Sstevel@tonic-gate 367*0Sstevel@tonic-gate if ((zent->zid = getzoneidbyname(zone_name)) == -1) 368*0Sstevel@tonic-gate zent->zid = ZONE_ID_UNDEFINED; 369*0Sstevel@tonic-gate 370*0Sstevel@tonic-gate if ((err = zone_get_zonepath(zent->zname, root, sizeof (root))) != 371*0Sstevel@tonic-gate Z_OK) { 372*0Sstevel@tonic-gate errno = err; 373*0Sstevel@tonic-gate zperror2(zent->zname, gettext("could not get zone path")); 374*0Sstevel@tonic-gate return (Z_ERR); 375*0Sstevel@tonic-gate } 376*0Sstevel@tonic-gate (void) strlcpy(zent->zroot, root, sizeof (zent->zroot)); 377*0Sstevel@tonic-gate 378*0Sstevel@tonic-gate if ((err = zone_get_state(zent->zname, &zent->zstate_num)) != Z_OK) { 379*0Sstevel@tonic-gate errno = err; 380*0Sstevel@tonic-gate zperror2(zent->zname, gettext("could not get state")); 381*0Sstevel@tonic-gate return (Z_ERR); 382*0Sstevel@tonic-gate } 383*0Sstevel@tonic-gate zent->zstate_str = zone_state_str(zent->zstate_num); 384*0Sstevel@tonic-gate 385*0Sstevel@tonic-gate return (Z_OK); 386*0Sstevel@tonic-gate } 387*0Sstevel@tonic-gate 388*0Sstevel@tonic-gate /* 389*0Sstevel@tonic-gate * fetch_zents() calls zone_list(2) to find out how many zones are running 390*0Sstevel@tonic-gate * (which is stored in the global nzents), then calls zone_list(2) again 391*0Sstevel@tonic-gate * to fetch the list of running zones (stored in the global zents). This 392*0Sstevel@tonic-gate * function may be called multiple times, so if zents is already set, we 393*0Sstevel@tonic-gate * return immediately to save work. 394*0Sstevel@tonic-gate */ 395*0Sstevel@tonic-gate 396*0Sstevel@tonic-gate static int 397*0Sstevel@tonic-gate fetch_zents() 398*0Sstevel@tonic-gate { 399*0Sstevel@tonic-gate zoneid_t *zids = NULL; 400*0Sstevel@tonic-gate uint_t nzents_saved; 401*0Sstevel@tonic-gate int i; 402*0Sstevel@tonic-gate 403*0Sstevel@tonic-gate if (nzents > 0) 404*0Sstevel@tonic-gate return (Z_OK); 405*0Sstevel@tonic-gate 406*0Sstevel@tonic-gate if (zone_list(NULL, &nzents) != 0) { 407*0Sstevel@tonic-gate zperror(gettext("failed to get zoneid list"), B_FALSE); 408*0Sstevel@tonic-gate return (Z_ERR); 409*0Sstevel@tonic-gate } 410*0Sstevel@tonic-gate 411*0Sstevel@tonic-gate again: 412*0Sstevel@tonic-gate if (nzents == 0) 413*0Sstevel@tonic-gate return (Z_OK); 414*0Sstevel@tonic-gate 415*0Sstevel@tonic-gate zids = safe_calloc(nzents, sizeof (zoneid_t)); 416*0Sstevel@tonic-gate nzents_saved = nzents; 417*0Sstevel@tonic-gate 418*0Sstevel@tonic-gate if (zone_list(zids, &nzents) != 0) { 419*0Sstevel@tonic-gate zperror(gettext("failed to get zone list"), B_FALSE); 420*0Sstevel@tonic-gate free(zids); 421*0Sstevel@tonic-gate return (Z_ERR); 422*0Sstevel@tonic-gate } 423*0Sstevel@tonic-gate if (nzents != nzents_saved) { 424*0Sstevel@tonic-gate /* list changed, try again */ 425*0Sstevel@tonic-gate free(zids); 426*0Sstevel@tonic-gate goto again; 427*0Sstevel@tonic-gate } 428*0Sstevel@tonic-gate 429*0Sstevel@tonic-gate zents = safe_calloc(nzents, sizeof (zone_entry_t)); 430*0Sstevel@tonic-gate 431*0Sstevel@tonic-gate for (i = 0; i < nzents; i++) { 432*0Sstevel@tonic-gate char name[ZONENAME_MAX]; 433*0Sstevel@tonic-gate 434*0Sstevel@tonic-gate if (getzonenamebyid(zids[i], name, sizeof (name)) < 0) 435*0Sstevel@tonic-gate zperror(gettext("failed to get zone name"), B_FALSE); 436*0Sstevel@tonic-gate else if (lookup_zone_info(name, &zents[i]) != Z_OK) 437*0Sstevel@tonic-gate zerror(gettext("failed to get zone list")); 438*0Sstevel@tonic-gate } 439*0Sstevel@tonic-gate 440*0Sstevel@tonic-gate free(zids); 441*0Sstevel@tonic-gate return (Z_OK); 442*0Sstevel@tonic-gate } 443*0Sstevel@tonic-gate 444*0Sstevel@tonic-gate static void 445*0Sstevel@tonic-gate zone_print_list(zone_state_t min_state, boolean_t verbose, boolean_t parsable) 446*0Sstevel@tonic-gate { 447*0Sstevel@tonic-gate int i; 448*0Sstevel@tonic-gate zone_entry_t zent; 449*0Sstevel@tonic-gate FILE *cookie; 450*0Sstevel@tonic-gate char *name; 451*0Sstevel@tonic-gate 452*0Sstevel@tonic-gate /* 453*0Sstevel@tonic-gate * First get the list of running zones from the kernel and print them. 454*0Sstevel@tonic-gate * If that is all we need, then return. 455*0Sstevel@tonic-gate */ 456*0Sstevel@tonic-gate if (fetch_zents() != Z_OK) { 457*0Sstevel@tonic-gate /* 458*0Sstevel@tonic-gate * No need for error messages; fetch_zents() has already taken 459*0Sstevel@tonic-gate * care of this. 460*0Sstevel@tonic-gate */ 461*0Sstevel@tonic-gate return; 462*0Sstevel@tonic-gate } 463*0Sstevel@tonic-gate for (i = 0; i < nzents; i++) { 464*0Sstevel@tonic-gate if (!verbose && !parsable) { 465*0Sstevel@tonic-gate zone_print(&zents[i], verbose, parsable); 466*0Sstevel@tonic-gate continue; 467*0Sstevel@tonic-gate } 468*0Sstevel@tonic-gate zone_print(&zents[i], verbose, parsable); 469*0Sstevel@tonic-gate } 470*0Sstevel@tonic-gate if (min_state >= ZONE_STATE_RUNNING) 471*0Sstevel@tonic-gate return; 472*0Sstevel@tonic-gate /* 473*0Sstevel@tonic-gate * Next, get the full list of zones from the configuration, skipping 474*0Sstevel@tonic-gate * any we have already printed. 475*0Sstevel@tonic-gate */ 476*0Sstevel@tonic-gate cookie = setzoneent(); 477*0Sstevel@tonic-gate while ((name = getzoneent(cookie)) != NULL) { 478*0Sstevel@tonic-gate for (i = 0; i < nzents; i++) { 479*0Sstevel@tonic-gate if (strcmp(zents[i].zname, name) == 0) 480*0Sstevel@tonic-gate break; 481*0Sstevel@tonic-gate } 482*0Sstevel@tonic-gate if (i < nzents) { 483*0Sstevel@tonic-gate free(name); 484*0Sstevel@tonic-gate continue; 485*0Sstevel@tonic-gate } 486*0Sstevel@tonic-gate if (lookup_zone_info(name, &zent) != Z_OK) { 487*0Sstevel@tonic-gate free(name); 488*0Sstevel@tonic-gate continue; 489*0Sstevel@tonic-gate } 490*0Sstevel@tonic-gate free(name); 491*0Sstevel@tonic-gate if (zent.zstate_num >= min_state) 492*0Sstevel@tonic-gate zone_print(&zent, verbose, parsable); 493*0Sstevel@tonic-gate } 494*0Sstevel@tonic-gate endzoneent(cookie); 495*0Sstevel@tonic-gate } 496*0Sstevel@tonic-gate 497*0Sstevel@tonic-gate static zone_entry_t * 498*0Sstevel@tonic-gate lookup_running_zone(char *str) 499*0Sstevel@tonic-gate { 500*0Sstevel@tonic-gate zoneid_t zoneid; 501*0Sstevel@tonic-gate char *cp; 502*0Sstevel@tonic-gate int i; 503*0Sstevel@tonic-gate 504*0Sstevel@tonic-gate if (fetch_zents() != Z_OK) 505*0Sstevel@tonic-gate return (NULL); 506*0Sstevel@tonic-gate 507*0Sstevel@tonic-gate for (i = 0; i < nzents; i++) { 508*0Sstevel@tonic-gate if (strcmp(str, zents[i].zname) == 0) 509*0Sstevel@tonic-gate return (&zents[i]); 510*0Sstevel@tonic-gate } 511*0Sstevel@tonic-gate errno = 0; 512*0Sstevel@tonic-gate zoneid = strtol(str, &cp, 0); 513*0Sstevel@tonic-gate if (zoneid < MIN_ZONEID || zoneid > MAX_ZONEID || 514*0Sstevel@tonic-gate errno != 0 || *cp != '\0') 515*0Sstevel@tonic-gate return (NULL); 516*0Sstevel@tonic-gate for (i = 0; i < nzents; i++) { 517*0Sstevel@tonic-gate if (zoneid == zents[i].zid) 518*0Sstevel@tonic-gate return (&zents[i]); 519*0Sstevel@tonic-gate } 520*0Sstevel@tonic-gate return (NULL); 521*0Sstevel@tonic-gate } 522*0Sstevel@tonic-gate 523*0Sstevel@tonic-gate /* 524*0Sstevel@tonic-gate * Check a bit in a mode_t: if on is B_TRUE, that bit should be on; if 525*0Sstevel@tonic-gate * B_FALSE, it should be off. Return B_TRUE if the mode is bad (incorrect). 526*0Sstevel@tonic-gate */ 527*0Sstevel@tonic-gate static boolean_t 528*0Sstevel@tonic-gate bad_mode_bit(mode_t mode, mode_t bit, boolean_t on, char *file) 529*0Sstevel@tonic-gate { 530*0Sstevel@tonic-gate char *str; 531*0Sstevel@tonic-gate 532*0Sstevel@tonic-gate assert(bit == S_IRUSR || bit == S_IWUSR || bit == S_IXUSR || 533*0Sstevel@tonic-gate bit == S_IRGRP || bit == S_IWGRP || bit == S_IXGRP || 534*0Sstevel@tonic-gate bit == S_IROTH || bit == S_IWOTH || bit == S_IXOTH); 535*0Sstevel@tonic-gate /* 536*0Sstevel@tonic-gate * TRANSLATION_NOTE 537*0Sstevel@tonic-gate * The strings below will be used as part of a larger message, 538*0Sstevel@tonic-gate * either: 539*0Sstevel@tonic-gate * (file name) must be (owner|group|world) (read|writ|execut)able 540*0Sstevel@tonic-gate * or 541*0Sstevel@tonic-gate * (file name) must not be (owner|group|world) (read|writ|execut)able 542*0Sstevel@tonic-gate */ 543*0Sstevel@tonic-gate switch (bit) { 544*0Sstevel@tonic-gate case S_IRUSR: 545*0Sstevel@tonic-gate str = gettext("owner readable"); 546*0Sstevel@tonic-gate break; 547*0Sstevel@tonic-gate case S_IWUSR: 548*0Sstevel@tonic-gate str = gettext("owner writable"); 549*0Sstevel@tonic-gate break; 550*0Sstevel@tonic-gate case S_IXUSR: 551*0Sstevel@tonic-gate str = gettext("owner executable"); 552*0Sstevel@tonic-gate break; 553*0Sstevel@tonic-gate case S_IRGRP: 554*0Sstevel@tonic-gate str = gettext("group readable"); 555*0Sstevel@tonic-gate break; 556*0Sstevel@tonic-gate case S_IWGRP: 557*0Sstevel@tonic-gate str = gettext("group writable"); 558*0Sstevel@tonic-gate break; 559*0Sstevel@tonic-gate case S_IXGRP: 560*0Sstevel@tonic-gate str = gettext("group executable"); 561*0Sstevel@tonic-gate break; 562*0Sstevel@tonic-gate case S_IROTH: 563*0Sstevel@tonic-gate str = gettext("world readable"); 564*0Sstevel@tonic-gate break; 565*0Sstevel@tonic-gate case S_IWOTH: 566*0Sstevel@tonic-gate str = gettext("world writable"); 567*0Sstevel@tonic-gate break; 568*0Sstevel@tonic-gate case S_IXOTH: 569*0Sstevel@tonic-gate str = gettext("world executable"); 570*0Sstevel@tonic-gate break; 571*0Sstevel@tonic-gate } 572*0Sstevel@tonic-gate if ((mode & bit) == (on ? 0 : bit)) { 573*0Sstevel@tonic-gate /* 574*0Sstevel@tonic-gate * TRANSLATION_NOTE 575*0Sstevel@tonic-gate * The first parameter below is a file name; the second 576*0Sstevel@tonic-gate * is one of the "(owner|group|world) (read|writ|execut)able" 577*0Sstevel@tonic-gate * strings from above. 578*0Sstevel@tonic-gate */ 579*0Sstevel@tonic-gate /* 580*0Sstevel@tonic-gate * The code below could be simplified but not in a way 581*0Sstevel@tonic-gate * that would easily translate to non-English locales. 582*0Sstevel@tonic-gate */ 583*0Sstevel@tonic-gate if (on) { 584*0Sstevel@tonic-gate (void) fprintf(stderr, gettext("%s must be %s.\n"), 585*0Sstevel@tonic-gate file, str); 586*0Sstevel@tonic-gate } else { 587*0Sstevel@tonic-gate (void) fprintf(stderr, gettext("%s must not be %s.\n"), 588*0Sstevel@tonic-gate file, str); 589*0Sstevel@tonic-gate } 590*0Sstevel@tonic-gate return (B_TRUE); 591*0Sstevel@tonic-gate } 592*0Sstevel@tonic-gate return (B_FALSE); 593*0Sstevel@tonic-gate } 594*0Sstevel@tonic-gate 595*0Sstevel@tonic-gate /* 596*0Sstevel@tonic-gate * We want to make sure that no zone has its zone path as a child node 597*0Sstevel@tonic-gate * (in the directory sense) of any other. We do that by comparing this 598*0Sstevel@tonic-gate * zone's path to the path of all other (non-global) zones. The comparison 599*0Sstevel@tonic-gate * in each case is simple: add '/' to the end of the path, then do a 600*0Sstevel@tonic-gate * strncmp() of the two paths, using the length of the shorter one. 601*0Sstevel@tonic-gate */ 602*0Sstevel@tonic-gate 603*0Sstevel@tonic-gate static int 604*0Sstevel@tonic-gate crosscheck_zonepaths(char *path) 605*0Sstevel@tonic-gate { 606*0Sstevel@tonic-gate char rpath[MAXPATHLEN]; /* resolved path */ 607*0Sstevel@tonic-gate char path_copy[MAXPATHLEN]; /* copy of original path */ 608*0Sstevel@tonic-gate char rpath_copy[MAXPATHLEN]; /* copy of original rpath */ 609*0Sstevel@tonic-gate struct zoneent *ze; 610*0Sstevel@tonic-gate int res, err; 611*0Sstevel@tonic-gate FILE *cookie; 612*0Sstevel@tonic-gate 613*0Sstevel@tonic-gate cookie = setzoneent(); 614*0Sstevel@tonic-gate while ((ze = getzoneent_private(cookie)) != NULL) { 615*0Sstevel@tonic-gate /* Skip zones which are not installed. */ 616*0Sstevel@tonic-gate if (ze->zone_state < ZONE_STATE_INSTALLED) { 617*0Sstevel@tonic-gate free(ze); 618*0Sstevel@tonic-gate continue; 619*0Sstevel@tonic-gate } 620*0Sstevel@tonic-gate /* Skip the global zone and the current target zone. */ 621*0Sstevel@tonic-gate if (strcmp(ze->zone_name, GLOBAL_ZONENAME) == 0 || 622*0Sstevel@tonic-gate strcmp(ze->zone_name, target_zone) == 0) { 623*0Sstevel@tonic-gate free(ze); 624*0Sstevel@tonic-gate continue; 625*0Sstevel@tonic-gate } 626*0Sstevel@tonic-gate if (strlen(ze->zone_path) == 0) { 627*0Sstevel@tonic-gate /* old index file without path, fall back */ 628*0Sstevel@tonic-gate if ((err = zone_get_zonepath(ze->zone_name, 629*0Sstevel@tonic-gate ze->zone_path, sizeof (ze->zone_path))) != Z_OK) { 630*0Sstevel@tonic-gate errno = err; 631*0Sstevel@tonic-gate zperror2(ze->zone_name, 632*0Sstevel@tonic-gate gettext("could not get zone path")); 633*0Sstevel@tonic-gate free(ze); 634*0Sstevel@tonic-gate continue; 635*0Sstevel@tonic-gate } 636*0Sstevel@tonic-gate } 637*0Sstevel@tonic-gate res = resolvepath(ze->zone_path, rpath, sizeof (rpath)); 638*0Sstevel@tonic-gate if (res == -1) { 639*0Sstevel@tonic-gate if (errno != ENOENT) { 640*0Sstevel@tonic-gate zperror(ze->zone_path, B_FALSE); 641*0Sstevel@tonic-gate free(ze); 642*0Sstevel@tonic-gate return (Z_ERR); 643*0Sstevel@tonic-gate } 644*0Sstevel@tonic-gate (void) printf(gettext("WARNING: zone %s is installed, " 645*0Sstevel@tonic-gate "but its %s %s does not exist.\n"), ze->zone_name, 646*0Sstevel@tonic-gate "zonepath", ze->zone_path); 647*0Sstevel@tonic-gate free(ze); 648*0Sstevel@tonic-gate continue; 649*0Sstevel@tonic-gate } 650*0Sstevel@tonic-gate rpath[res] = '\0'; 651*0Sstevel@tonic-gate (void) snprintf(path_copy, sizeof (path_copy), "%s/", path); 652*0Sstevel@tonic-gate (void) snprintf(rpath_copy, sizeof (rpath_copy), "%s/", rpath); 653*0Sstevel@tonic-gate if (strncmp(path_copy, rpath_copy, 654*0Sstevel@tonic-gate min(strlen(path_copy), strlen(rpath_copy))) == 0) { 655*0Sstevel@tonic-gate (void) fprintf(stderr, gettext("%s zonepath (%s) and " 656*0Sstevel@tonic-gate "%s zonepath (%s) overlap.\n"), 657*0Sstevel@tonic-gate target_zone, path, ze->zone_name, rpath); 658*0Sstevel@tonic-gate free(ze); 659*0Sstevel@tonic-gate return (Z_ERR); 660*0Sstevel@tonic-gate } 661*0Sstevel@tonic-gate free(ze); 662*0Sstevel@tonic-gate } 663*0Sstevel@tonic-gate endzoneent(cookie); 664*0Sstevel@tonic-gate return (Z_OK); 665*0Sstevel@tonic-gate } 666*0Sstevel@tonic-gate 667*0Sstevel@tonic-gate static int 668*0Sstevel@tonic-gate validate_zonepath(char *path, int cmd_num) 669*0Sstevel@tonic-gate { 670*0Sstevel@tonic-gate int res; /* result of last library/system call */ 671*0Sstevel@tonic-gate boolean_t err = B_FALSE; /* have we run into an error? */ 672*0Sstevel@tonic-gate struct stat stbuf; 673*0Sstevel@tonic-gate struct statvfs vfsbuf; 674*0Sstevel@tonic-gate char rpath[MAXPATHLEN]; /* resolved path */ 675*0Sstevel@tonic-gate char ppath[MAXPATHLEN]; /* parent path */ 676*0Sstevel@tonic-gate char rppath[MAXPATHLEN]; /* resolved parent path */ 677*0Sstevel@tonic-gate char rootpath[MAXPATHLEN]; /* root path */ 678*0Sstevel@tonic-gate zone_state_t state; 679*0Sstevel@tonic-gate 680*0Sstevel@tonic-gate if (path[0] != '/') { 681*0Sstevel@tonic-gate (void) fprintf(stderr, 682*0Sstevel@tonic-gate gettext("%s is not an absolute path.\n"), path); 683*0Sstevel@tonic-gate return (Z_ERR); 684*0Sstevel@tonic-gate } 685*0Sstevel@tonic-gate if ((res = resolvepath(path, rpath, sizeof (rpath))) == -1) { 686*0Sstevel@tonic-gate if ((errno != ENOENT) || 687*0Sstevel@tonic-gate (cmd_num != CMD_VERIFY && cmd_num != CMD_INSTALL)) { 688*0Sstevel@tonic-gate zperror(path, B_FALSE); 689*0Sstevel@tonic-gate return (Z_ERR); 690*0Sstevel@tonic-gate } 691*0Sstevel@tonic-gate if (cmd_num == CMD_VERIFY) { 692*0Sstevel@tonic-gate (void) fprintf(stderr, gettext("WARNING: %s does not " 693*0Sstevel@tonic-gate "exist, so it cannot be verified.\nWhen 'zoneadm " 694*0Sstevel@tonic-gate "%s' is run, '%s' will try to create\n%s, and '%s' " 695*0Sstevel@tonic-gate "will be tried again,\nbut the '%s' may fail if:\n" 696*0Sstevel@tonic-gate "the parent directory of %s is group- or other-" 697*0Sstevel@tonic-gate "writable\nor\n%s overlaps with any other " 698*0Sstevel@tonic-gate "installed zones.\n"), path, 699*0Sstevel@tonic-gate cmd_to_str(CMD_INSTALL), cmd_to_str(CMD_INSTALL), 700*0Sstevel@tonic-gate path, cmd_to_str(CMD_VERIFY), 701*0Sstevel@tonic-gate cmd_to_str(CMD_VERIFY), path, path); 702*0Sstevel@tonic-gate return (Z_OK); 703*0Sstevel@tonic-gate } 704*0Sstevel@tonic-gate /* 705*0Sstevel@tonic-gate * The zonepath is supposed to be mode 700 but its 706*0Sstevel@tonic-gate * parent(s) 755. So use 755 on the mkdirp() then 707*0Sstevel@tonic-gate * chmod() the zonepath itself to 700. 708*0Sstevel@tonic-gate */ 709*0Sstevel@tonic-gate if (mkdirp(path, DEFAULT_DIR_MODE) < 0) { 710*0Sstevel@tonic-gate zperror(path, B_FALSE); 711*0Sstevel@tonic-gate return (Z_ERR); 712*0Sstevel@tonic-gate } 713*0Sstevel@tonic-gate /* 714*0Sstevel@tonic-gate * If the chmod() fails, report the error, but might 715*0Sstevel@tonic-gate * as well continue the verify procedure. 716*0Sstevel@tonic-gate */ 717*0Sstevel@tonic-gate if (chmod(path, S_IRWXU) != 0) 718*0Sstevel@tonic-gate zperror(path, B_FALSE); 719*0Sstevel@tonic-gate /* 720*0Sstevel@tonic-gate * Since the mkdir() succeeded, we should not have to 721*0Sstevel@tonic-gate * worry about a subsequent ENOENT, thus this should 722*0Sstevel@tonic-gate * only recurse once. 723*0Sstevel@tonic-gate */ 724*0Sstevel@tonic-gate return (validate_zonepath(path, CMD_INSTALL)); 725*0Sstevel@tonic-gate } 726*0Sstevel@tonic-gate rpath[res] = '\0'; 727*0Sstevel@tonic-gate if (strcmp(path, rpath) != 0) { 728*0Sstevel@tonic-gate errno = Z_RESOLVED_PATH; 729*0Sstevel@tonic-gate zperror(path, B_TRUE); 730*0Sstevel@tonic-gate return (Z_ERR); 731*0Sstevel@tonic-gate } 732*0Sstevel@tonic-gate if ((res = stat(rpath, &stbuf)) != 0) { 733*0Sstevel@tonic-gate zperror(rpath, B_FALSE); 734*0Sstevel@tonic-gate return (Z_ERR); 735*0Sstevel@tonic-gate } 736*0Sstevel@tonic-gate if (!S_ISDIR(stbuf.st_mode)) { 737*0Sstevel@tonic-gate (void) fprintf(stderr, gettext("%s is not a directory.\n"), 738*0Sstevel@tonic-gate rpath); 739*0Sstevel@tonic-gate return (Z_ERR); 740*0Sstevel@tonic-gate } 741*0Sstevel@tonic-gate if ((strcmp(stbuf.st_fstype, MNTTYPE_TMPFS) == 0) || 742*0Sstevel@tonic-gate (strcmp(stbuf.st_fstype, MNTTYPE_XMEMFS) == 0)) { 743*0Sstevel@tonic-gate (void) printf(gettext("WARNING: %s is on a temporary " 744*0Sstevel@tonic-gate "file-system.\n"), rpath); 745*0Sstevel@tonic-gate } 746*0Sstevel@tonic-gate if (crosscheck_zonepaths(rpath) != Z_OK) 747*0Sstevel@tonic-gate return (Z_ERR); 748*0Sstevel@tonic-gate /* 749*0Sstevel@tonic-gate * Try to collect and report as many minor errors as possible 750*0Sstevel@tonic-gate * before returning, so the user can learn everything that needs 751*0Sstevel@tonic-gate * to be fixed up front. 752*0Sstevel@tonic-gate */ 753*0Sstevel@tonic-gate if (stbuf.st_uid != 0) { 754*0Sstevel@tonic-gate (void) fprintf(stderr, gettext("%s is not owned by root.\n"), 755*0Sstevel@tonic-gate rpath); 756*0Sstevel@tonic-gate err = B_TRUE; 757*0Sstevel@tonic-gate } 758*0Sstevel@tonic-gate err |= bad_mode_bit(stbuf.st_mode, S_IRUSR, B_TRUE, rpath); 759*0Sstevel@tonic-gate err |= bad_mode_bit(stbuf.st_mode, S_IWUSR, B_TRUE, rpath); 760*0Sstevel@tonic-gate err |= bad_mode_bit(stbuf.st_mode, S_IXUSR, B_TRUE, rpath); 761*0Sstevel@tonic-gate err |= bad_mode_bit(stbuf.st_mode, S_IRGRP, B_FALSE, rpath); 762*0Sstevel@tonic-gate err |= bad_mode_bit(stbuf.st_mode, S_IWGRP, B_FALSE, rpath); 763*0Sstevel@tonic-gate err |= bad_mode_bit(stbuf.st_mode, S_IXGRP, B_FALSE, rpath); 764*0Sstevel@tonic-gate err |= bad_mode_bit(stbuf.st_mode, S_IROTH, B_FALSE, rpath); 765*0Sstevel@tonic-gate err |= bad_mode_bit(stbuf.st_mode, S_IWOTH, B_FALSE, rpath); 766*0Sstevel@tonic-gate err |= bad_mode_bit(stbuf.st_mode, S_IXOTH, B_FALSE, rpath); 767*0Sstevel@tonic-gate 768*0Sstevel@tonic-gate (void) snprintf(ppath, sizeof (ppath), "%s/..", path); 769*0Sstevel@tonic-gate if ((res = resolvepath(ppath, rppath, sizeof (rppath))) == -1) { 770*0Sstevel@tonic-gate zperror(ppath, B_FALSE); 771*0Sstevel@tonic-gate return (Z_ERR); 772*0Sstevel@tonic-gate } 773*0Sstevel@tonic-gate rppath[res] = '\0'; 774*0Sstevel@tonic-gate if ((res = stat(rppath, &stbuf)) != 0) { 775*0Sstevel@tonic-gate zperror(rppath, B_FALSE); 776*0Sstevel@tonic-gate return (Z_ERR); 777*0Sstevel@tonic-gate } 778*0Sstevel@tonic-gate /* theoretically impossible */ 779*0Sstevel@tonic-gate if (!S_ISDIR(stbuf.st_mode)) { 780*0Sstevel@tonic-gate (void) fprintf(stderr, gettext("%s is not a directory.\n"), 781*0Sstevel@tonic-gate rppath); 782*0Sstevel@tonic-gate return (Z_ERR); 783*0Sstevel@tonic-gate } 784*0Sstevel@tonic-gate if (stbuf.st_uid != 0) { 785*0Sstevel@tonic-gate (void) fprintf(stderr, gettext("%s is not owned by root.\n"), 786*0Sstevel@tonic-gate rppath); 787*0Sstevel@tonic-gate err = B_TRUE; 788*0Sstevel@tonic-gate } 789*0Sstevel@tonic-gate err |= bad_mode_bit(stbuf.st_mode, S_IRUSR, B_TRUE, rppath); 790*0Sstevel@tonic-gate err |= bad_mode_bit(stbuf.st_mode, S_IWUSR, B_TRUE, rppath); 791*0Sstevel@tonic-gate err |= bad_mode_bit(stbuf.st_mode, S_IXUSR, B_TRUE, rppath); 792*0Sstevel@tonic-gate err |= bad_mode_bit(stbuf.st_mode, S_IWGRP, B_FALSE, rppath); 793*0Sstevel@tonic-gate err |= bad_mode_bit(stbuf.st_mode, S_IWOTH, B_FALSE, rppath); 794*0Sstevel@tonic-gate if (strcmp(rpath, rppath) == 0) { 795*0Sstevel@tonic-gate (void) fprintf(stderr, gettext("%s is its own parent.\n"), 796*0Sstevel@tonic-gate rppath); 797*0Sstevel@tonic-gate err = B_TRUE; 798*0Sstevel@tonic-gate } 799*0Sstevel@tonic-gate 800*0Sstevel@tonic-gate if (statvfs(rpath, &vfsbuf) != 0) { 801*0Sstevel@tonic-gate zperror(rpath, B_FALSE); 802*0Sstevel@tonic-gate return (Z_ERR); 803*0Sstevel@tonic-gate } 804*0Sstevel@tonic-gate if (strncmp(vfsbuf.f_basetype, "nfs", 3) == 0) { 805*0Sstevel@tonic-gate (void) fprintf(stderr, gettext("Zonepath %s is over NFS, " 806*0Sstevel@tonic-gate "which is not currently supported.\n"), rpath); 807*0Sstevel@tonic-gate return (Z_ERR); 808*0Sstevel@tonic-gate } 809*0Sstevel@tonic-gate 810*0Sstevel@tonic-gate if ((res = zone_get_state(target_zone, &state)) != Z_OK) { 811*0Sstevel@tonic-gate errno = res; 812*0Sstevel@tonic-gate zperror2(target_zone, gettext("could not get state")); 813*0Sstevel@tonic-gate return (Z_ERR); 814*0Sstevel@tonic-gate } 815*0Sstevel@tonic-gate /* 816*0Sstevel@tonic-gate * The existence of the root path is only bad in the configured state, 817*0Sstevel@tonic-gate * as it is *supposed* to be there at the installed and later states. 818*0Sstevel@tonic-gate * State/command mismatches are caught earlier in verify_details(). 819*0Sstevel@tonic-gate */ 820*0Sstevel@tonic-gate if (state == ZONE_STATE_CONFIGURED) { 821*0Sstevel@tonic-gate if (snprintf(rootpath, sizeof (rootpath), "%s/root", rpath) >= 822*0Sstevel@tonic-gate sizeof (rootpath)) { 823*0Sstevel@tonic-gate (void) fprintf(stderr, 824*0Sstevel@tonic-gate gettext("Zonepath %s is too long.\n"), rpath); 825*0Sstevel@tonic-gate return (Z_ERR); 826*0Sstevel@tonic-gate } 827*0Sstevel@tonic-gate if ((res = stat(rootpath, &stbuf)) == 0) { 828*0Sstevel@tonic-gate (void) fprintf(stderr, gettext("Rootpath %s exists; " 829*0Sstevel@tonic-gate "remove or move aside prior to %s.\n"), rootpath, 830*0Sstevel@tonic-gate cmd_to_str(CMD_INSTALL)); 831*0Sstevel@tonic-gate return (Z_ERR); 832*0Sstevel@tonic-gate } 833*0Sstevel@tonic-gate } 834*0Sstevel@tonic-gate 835*0Sstevel@tonic-gate return (err ? Z_ERR : Z_OK); 836*0Sstevel@tonic-gate } 837*0Sstevel@tonic-gate 838*0Sstevel@tonic-gate static void 839*0Sstevel@tonic-gate release_lock_file(int lockfd) 840*0Sstevel@tonic-gate { 841*0Sstevel@tonic-gate (void) close(lockfd); 842*0Sstevel@tonic-gate } 843*0Sstevel@tonic-gate 844*0Sstevel@tonic-gate static int 845*0Sstevel@tonic-gate grab_lock_file(const char *zone_name, int *lockfd) 846*0Sstevel@tonic-gate { 847*0Sstevel@tonic-gate char pathbuf[PATH_MAX]; 848*0Sstevel@tonic-gate struct flock flock; 849*0Sstevel@tonic-gate 850*0Sstevel@tonic-gate if (mkdir(ZONES_TMPDIR, S_IRWXU) < 0 && errno != EEXIST) { 851*0Sstevel@tonic-gate zerror(gettext("could not mkdir %s: %s"), ZONES_TMPDIR, 852*0Sstevel@tonic-gate strerror(errno)); 853*0Sstevel@tonic-gate return (Z_ERR); 854*0Sstevel@tonic-gate } 855*0Sstevel@tonic-gate (void) chmod(ZONES_TMPDIR, S_IRWXU); 856*0Sstevel@tonic-gate 857*0Sstevel@tonic-gate /* 858*0Sstevel@tonic-gate * One of these lock files is created for each zone (when needed). 859*0Sstevel@tonic-gate * The lock files are not cleaned up (except on system reboot), 860*0Sstevel@tonic-gate * but since there is only one per zone, there is no resource 861*0Sstevel@tonic-gate * starvation issue. 862*0Sstevel@tonic-gate */ 863*0Sstevel@tonic-gate (void) snprintf(pathbuf, sizeof (pathbuf), "%s/%s.zoneadm.lock", 864*0Sstevel@tonic-gate ZONES_TMPDIR, zone_name); 865*0Sstevel@tonic-gate if ((*lockfd = open(pathbuf, O_RDWR|O_CREAT, S_IRUSR|S_IWUSR)) < 0) { 866*0Sstevel@tonic-gate zerror(gettext("could not open %s: %s"), pathbuf, 867*0Sstevel@tonic-gate strerror(errno)); 868*0Sstevel@tonic-gate return (Z_ERR); 869*0Sstevel@tonic-gate } 870*0Sstevel@tonic-gate /* 871*0Sstevel@tonic-gate * Lock the file to synchronize with other zoneadmds 872*0Sstevel@tonic-gate */ 873*0Sstevel@tonic-gate flock.l_type = F_WRLCK; 874*0Sstevel@tonic-gate flock.l_whence = SEEK_SET; 875*0Sstevel@tonic-gate flock.l_start = (off_t)0; 876*0Sstevel@tonic-gate flock.l_len = (off_t)0; 877*0Sstevel@tonic-gate if (fcntl(*lockfd, F_SETLKW, &flock) < 0) { 878*0Sstevel@tonic-gate zerror(gettext("unable to lock %s: %s"), pathbuf, 879*0Sstevel@tonic-gate strerror(errno)); 880*0Sstevel@tonic-gate release_lock_file(*lockfd); 881*0Sstevel@tonic-gate return (Z_ERR); 882*0Sstevel@tonic-gate } 883*0Sstevel@tonic-gate return (Z_OK); 884*0Sstevel@tonic-gate } 885*0Sstevel@tonic-gate 886*0Sstevel@tonic-gate static void 887*0Sstevel@tonic-gate get_doorname(const char *zone_name, char *buffer) 888*0Sstevel@tonic-gate { 889*0Sstevel@tonic-gate (void) snprintf(buffer, PATH_MAX, ZONE_DOOR_PATH, zone_name); 890*0Sstevel@tonic-gate } 891*0Sstevel@tonic-gate 892*0Sstevel@tonic-gate /* 893*0Sstevel@tonic-gate * system daemons are not audited. For the global zone, this occurs 894*0Sstevel@tonic-gate * "naturally" since init is started with the default audit 895*0Sstevel@tonic-gate * characteristics. Since zoneadmd is a system daemon and it starts 896*0Sstevel@tonic-gate * init for a zone, it is necessary to clear out the audit 897*0Sstevel@tonic-gate * characteristics inherited from whomever started zoneadmd. This is 898*0Sstevel@tonic-gate * indicated by the audit id, which is set from the ruid parameter of 899*0Sstevel@tonic-gate * adt_set_user(), below. 900*0Sstevel@tonic-gate */ 901*0Sstevel@tonic-gate 902*0Sstevel@tonic-gate static void 903*0Sstevel@tonic-gate prepare_audit_context() 904*0Sstevel@tonic-gate { 905*0Sstevel@tonic-gate adt_session_data_t *ah; 906*0Sstevel@tonic-gate char *failure = gettext("audit failure: %s"); 907*0Sstevel@tonic-gate 908*0Sstevel@tonic-gate if (adt_start_session(&ah, NULL, 0)) { 909*0Sstevel@tonic-gate zerror(failure, strerror(errno)); 910*0Sstevel@tonic-gate return; 911*0Sstevel@tonic-gate } 912*0Sstevel@tonic-gate if (adt_set_user(ah, ADT_NO_AUDIT, ADT_NO_AUDIT, 913*0Sstevel@tonic-gate ADT_NO_AUDIT, ADT_NO_AUDIT, NULL, ADT_NEW)) { 914*0Sstevel@tonic-gate zerror(failure, strerror(errno)); 915*0Sstevel@tonic-gate (void) adt_end_session(ah); 916*0Sstevel@tonic-gate return; 917*0Sstevel@tonic-gate } 918*0Sstevel@tonic-gate if (adt_set_proc(ah)) 919*0Sstevel@tonic-gate zerror(failure, strerror(errno)); 920*0Sstevel@tonic-gate 921*0Sstevel@tonic-gate (void) adt_end_session(ah); 922*0Sstevel@tonic-gate } 923*0Sstevel@tonic-gate 924*0Sstevel@tonic-gate static int 925*0Sstevel@tonic-gate start_zoneadmd(const char *zone_name) 926*0Sstevel@tonic-gate { 927*0Sstevel@tonic-gate char doorpath[PATH_MAX]; 928*0Sstevel@tonic-gate pid_t child_pid; 929*0Sstevel@tonic-gate int error = Z_ERR; 930*0Sstevel@tonic-gate int doorfd, lockfd; 931*0Sstevel@tonic-gate struct door_info info; 932*0Sstevel@tonic-gate 933*0Sstevel@tonic-gate get_doorname(zone_name, doorpath); 934*0Sstevel@tonic-gate 935*0Sstevel@tonic-gate if (grab_lock_file(zone_name, &lockfd) != Z_OK) 936*0Sstevel@tonic-gate return (Z_ERR); 937*0Sstevel@tonic-gate 938*0Sstevel@tonic-gate /* 939*0Sstevel@tonic-gate * Now that we have the lock, re-confirm that the daemon is 940*0Sstevel@tonic-gate * *not* up and working fine. If it is still down, we have a green 941*0Sstevel@tonic-gate * light to start it. 942*0Sstevel@tonic-gate */ 943*0Sstevel@tonic-gate if ((doorfd = open(doorpath, O_RDONLY)) < 0) { 944*0Sstevel@tonic-gate if (errno != ENOENT) { 945*0Sstevel@tonic-gate zperror(doorpath, B_FALSE); 946*0Sstevel@tonic-gate goto out; 947*0Sstevel@tonic-gate } 948*0Sstevel@tonic-gate } else { 949*0Sstevel@tonic-gate if (door_info(doorfd, &info) == 0 && 950*0Sstevel@tonic-gate ((info.di_attributes & DOOR_REVOKED) == 0)) { 951*0Sstevel@tonic-gate error = Z_OK; 952*0Sstevel@tonic-gate (void) close(doorfd); 953*0Sstevel@tonic-gate goto out; 954*0Sstevel@tonic-gate } 955*0Sstevel@tonic-gate (void) close(doorfd); 956*0Sstevel@tonic-gate } 957*0Sstevel@tonic-gate 958*0Sstevel@tonic-gate if ((child_pid = fork()) == -1) { 959*0Sstevel@tonic-gate zperror(gettext("could not fork"), B_FALSE); 960*0Sstevel@tonic-gate goto out; 961*0Sstevel@tonic-gate } else if (child_pid == 0) { 962*0Sstevel@tonic-gate /* child process */ 963*0Sstevel@tonic-gate 964*0Sstevel@tonic-gate prepare_audit_context(); 965*0Sstevel@tonic-gate 966*0Sstevel@tonic-gate (void) execl("/usr/lib/zones/zoneadmd", "zoneadmd", "-z", 967*0Sstevel@tonic-gate zone_name, NULL); 968*0Sstevel@tonic-gate zperror(gettext("could not exec zoneadmd"), B_FALSE); 969*0Sstevel@tonic-gate _exit(Z_ERR); 970*0Sstevel@tonic-gate } else { 971*0Sstevel@tonic-gate /* parent process */ 972*0Sstevel@tonic-gate pid_t retval; 973*0Sstevel@tonic-gate int pstatus = 0; 974*0Sstevel@tonic-gate 975*0Sstevel@tonic-gate do { 976*0Sstevel@tonic-gate retval = waitpid(child_pid, &pstatus, 0); 977*0Sstevel@tonic-gate } while (retval != child_pid); 978*0Sstevel@tonic-gate if (WIFSIGNALED(pstatus) || (WIFEXITED(pstatus) && 979*0Sstevel@tonic-gate WEXITSTATUS(pstatus) != 0)) { 980*0Sstevel@tonic-gate zerror(gettext("could not start %s"), "zoneadmd"); 981*0Sstevel@tonic-gate goto out; 982*0Sstevel@tonic-gate } 983*0Sstevel@tonic-gate } 984*0Sstevel@tonic-gate error = Z_OK; 985*0Sstevel@tonic-gate out: 986*0Sstevel@tonic-gate release_lock_file(lockfd); 987*0Sstevel@tonic-gate return (error); 988*0Sstevel@tonic-gate } 989*0Sstevel@tonic-gate 990*0Sstevel@tonic-gate static int 991*0Sstevel@tonic-gate ping_zoneadmd(const char *zone_name) 992*0Sstevel@tonic-gate { 993*0Sstevel@tonic-gate char doorpath[PATH_MAX]; 994*0Sstevel@tonic-gate int doorfd; 995*0Sstevel@tonic-gate struct door_info info; 996*0Sstevel@tonic-gate 997*0Sstevel@tonic-gate get_doorname(zone_name, doorpath); 998*0Sstevel@tonic-gate 999*0Sstevel@tonic-gate if ((doorfd = open(doorpath, O_RDONLY)) < 0) { 1000*0Sstevel@tonic-gate return (Z_ERR); 1001*0Sstevel@tonic-gate } 1002*0Sstevel@tonic-gate if (door_info(doorfd, &info) == 0 && 1003*0Sstevel@tonic-gate ((info.di_attributes & DOOR_REVOKED) == 0)) { 1004*0Sstevel@tonic-gate (void) close(doorfd); 1005*0Sstevel@tonic-gate return (Z_OK); 1006*0Sstevel@tonic-gate } 1007*0Sstevel@tonic-gate (void) close(doorfd); 1008*0Sstevel@tonic-gate return (Z_ERR); 1009*0Sstevel@tonic-gate } 1010*0Sstevel@tonic-gate 1011*0Sstevel@tonic-gate static int 1012*0Sstevel@tonic-gate call_zoneadmd(const char *zone_name, zone_cmd_arg_t *arg) 1013*0Sstevel@tonic-gate { 1014*0Sstevel@tonic-gate char doorpath[PATH_MAX]; 1015*0Sstevel@tonic-gate int doorfd, result; 1016*0Sstevel@tonic-gate door_arg_t darg; 1017*0Sstevel@tonic-gate 1018*0Sstevel@tonic-gate zoneid_t zoneid; 1019*0Sstevel@tonic-gate uint64_t uniqid = 0; 1020*0Sstevel@tonic-gate 1021*0Sstevel@tonic-gate zone_cmd_rval_t *rvalp; 1022*0Sstevel@tonic-gate size_t rlen; 1023*0Sstevel@tonic-gate char *cp, *errbuf; 1024*0Sstevel@tonic-gate 1025*0Sstevel@tonic-gate rlen = getpagesize(); 1026*0Sstevel@tonic-gate if ((rvalp = malloc(rlen)) == NULL) { 1027*0Sstevel@tonic-gate zerror(gettext("failed to allocate %lu bytes: %s"), rlen, 1028*0Sstevel@tonic-gate strerror(errno)); 1029*0Sstevel@tonic-gate return (-1); 1030*0Sstevel@tonic-gate } 1031*0Sstevel@tonic-gate 1032*0Sstevel@tonic-gate if ((zoneid = getzoneidbyname(zone_name)) != ZONE_ID_UNDEFINED) { 1033*0Sstevel@tonic-gate (void) zone_getattr(zoneid, ZONE_ATTR_UNIQID, &uniqid, 1034*0Sstevel@tonic-gate sizeof (uniqid)); 1035*0Sstevel@tonic-gate } 1036*0Sstevel@tonic-gate arg->uniqid = uniqid; 1037*0Sstevel@tonic-gate (void) strlcpy(arg->locale, locale, sizeof (arg->locale)); 1038*0Sstevel@tonic-gate get_doorname(zone_name, doorpath); 1039*0Sstevel@tonic-gate 1040*0Sstevel@tonic-gate /* 1041*0Sstevel@tonic-gate * Loop trying to start zoneadmd; if something goes seriously 1042*0Sstevel@tonic-gate * wrong we break out and fail. 1043*0Sstevel@tonic-gate */ 1044*0Sstevel@tonic-gate for (;;) { 1045*0Sstevel@tonic-gate if (start_zoneadmd(zone_name) != Z_OK) 1046*0Sstevel@tonic-gate break; 1047*0Sstevel@tonic-gate 1048*0Sstevel@tonic-gate if ((doorfd = open(doorpath, O_RDONLY)) < 0) { 1049*0Sstevel@tonic-gate zperror(gettext("failed to open zone door"), B_FALSE); 1050*0Sstevel@tonic-gate break; 1051*0Sstevel@tonic-gate } 1052*0Sstevel@tonic-gate 1053*0Sstevel@tonic-gate darg.data_ptr = (char *)arg; 1054*0Sstevel@tonic-gate darg.data_size = sizeof (*arg); 1055*0Sstevel@tonic-gate darg.desc_ptr = NULL; 1056*0Sstevel@tonic-gate darg.desc_num = 0; 1057*0Sstevel@tonic-gate darg.rbuf = (char *)rvalp; 1058*0Sstevel@tonic-gate darg.rsize = rlen; 1059*0Sstevel@tonic-gate if (door_call(doorfd, &darg) != 0) { 1060*0Sstevel@tonic-gate (void) close(doorfd); 1061*0Sstevel@tonic-gate /* 1062*0Sstevel@tonic-gate * We'll get EBADF if the door has been revoked. 1063*0Sstevel@tonic-gate */ 1064*0Sstevel@tonic-gate if (errno != EBADF) { 1065*0Sstevel@tonic-gate zperror(gettext("door_call failed"), B_FALSE); 1066*0Sstevel@tonic-gate break; 1067*0Sstevel@tonic-gate } 1068*0Sstevel@tonic-gate continue; /* take another lap */ 1069*0Sstevel@tonic-gate } 1070*0Sstevel@tonic-gate (void) close(doorfd); 1071*0Sstevel@tonic-gate 1072*0Sstevel@tonic-gate if (darg.data_size == 0) { 1073*0Sstevel@tonic-gate /* Door server is going away; kick it again. */ 1074*0Sstevel@tonic-gate continue; 1075*0Sstevel@tonic-gate } 1076*0Sstevel@tonic-gate 1077*0Sstevel@tonic-gate errbuf = rvalp->errbuf; 1078*0Sstevel@tonic-gate while (*errbuf != '\0') { 1079*0Sstevel@tonic-gate /* 1080*0Sstevel@tonic-gate * Remove any newlines since zerror() 1081*0Sstevel@tonic-gate * will append one automatically. 1082*0Sstevel@tonic-gate */ 1083*0Sstevel@tonic-gate cp = strchr(errbuf, '\n'); 1084*0Sstevel@tonic-gate if (cp != NULL) 1085*0Sstevel@tonic-gate *cp = '\0'; 1086*0Sstevel@tonic-gate zerror("%s", errbuf); 1087*0Sstevel@tonic-gate if (cp == NULL) 1088*0Sstevel@tonic-gate break; 1089*0Sstevel@tonic-gate errbuf = cp + 1; 1090*0Sstevel@tonic-gate } 1091*0Sstevel@tonic-gate result = rvalp->rval == 0 ? 0 : -1; 1092*0Sstevel@tonic-gate free(rvalp); 1093*0Sstevel@tonic-gate return (result); 1094*0Sstevel@tonic-gate } 1095*0Sstevel@tonic-gate 1096*0Sstevel@tonic-gate free(rvalp); 1097*0Sstevel@tonic-gate return (-1); 1098*0Sstevel@tonic-gate } 1099*0Sstevel@tonic-gate 1100*0Sstevel@tonic-gate static int 1101*0Sstevel@tonic-gate ready_func(int argc, char *argv[]) 1102*0Sstevel@tonic-gate { 1103*0Sstevel@tonic-gate zone_cmd_arg_t zarg; 1104*0Sstevel@tonic-gate int arg; 1105*0Sstevel@tonic-gate 1106*0Sstevel@tonic-gate optind = 0; 1107*0Sstevel@tonic-gate if ((arg = getopt(argc, argv, "?")) != EOF) { 1108*0Sstevel@tonic-gate switch (arg) { 1109*0Sstevel@tonic-gate case '?': 1110*0Sstevel@tonic-gate sub_usage(SHELP_READY, CMD_READY); 1111*0Sstevel@tonic-gate return (optopt == '?' ? Z_OK : Z_USAGE); 1112*0Sstevel@tonic-gate default: 1113*0Sstevel@tonic-gate sub_usage(SHELP_READY, CMD_READY); 1114*0Sstevel@tonic-gate return (Z_USAGE); 1115*0Sstevel@tonic-gate } 1116*0Sstevel@tonic-gate } 1117*0Sstevel@tonic-gate if (argc > optind) { 1118*0Sstevel@tonic-gate sub_usage(SHELP_READY, CMD_READY); 1119*0Sstevel@tonic-gate return (Z_USAGE); 1120*0Sstevel@tonic-gate } 1121*0Sstevel@tonic-gate if (sanity_check(target_zone, CMD_READY, B_FALSE, B_FALSE) != Z_OK) 1122*0Sstevel@tonic-gate return (Z_ERR); 1123*0Sstevel@tonic-gate if (verify_details(CMD_READY) != Z_OK) 1124*0Sstevel@tonic-gate return (Z_ERR); 1125*0Sstevel@tonic-gate 1126*0Sstevel@tonic-gate zarg.cmd = Z_READY; 1127*0Sstevel@tonic-gate if (call_zoneadmd(target_zone, &zarg) != 0) { 1128*0Sstevel@tonic-gate zerror(gettext("call to %s failed"), "zoneadmd"); 1129*0Sstevel@tonic-gate return (Z_ERR); 1130*0Sstevel@tonic-gate } 1131*0Sstevel@tonic-gate return (Z_OK); 1132*0Sstevel@tonic-gate } 1133*0Sstevel@tonic-gate 1134*0Sstevel@tonic-gate static int 1135*0Sstevel@tonic-gate boot_func(int argc, char *argv[]) 1136*0Sstevel@tonic-gate { 1137*0Sstevel@tonic-gate zone_cmd_arg_t zarg; 1138*0Sstevel@tonic-gate int arg; 1139*0Sstevel@tonic-gate 1140*0Sstevel@tonic-gate zarg.bootbuf[0] = '\0'; 1141*0Sstevel@tonic-gate 1142*0Sstevel@tonic-gate /* 1143*0Sstevel@tonic-gate * At the current time, the only supported subargument to the 1144*0Sstevel@tonic-gate * "boot" subcommand is "-s" which specifies a single-user boot. 1145*0Sstevel@tonic-gate * In the future, other boot arguments should be supported 1146*0Sstevel@tonic-gate * including "-m" for specifying alternate smf(5) milestones. 1147*0Sstevel@tonic-gate */ 1148*0Sstevel@tonic-gate optind = 0; 1149*0Sstevel@tonic-gate if ((arg = getopt(argc, argv, "?s")) != EOF) { 1150*0Sstevel@tonic-gate switch (arg) { 1151*0Sstevel@tonic-gate case '?': 1152*0Sstevel@tonic-gate sub_usage(SHELP_BOOT, CMD_BOOT); 1153*0Sstevel@tonic-gate return (optopt == '?' ? Z_OK : Z_USAGE); 1154*0Sstevel@tonic-gate case 's': 1155*0Sstevel@tonic-gate (void) strlcpy(zarg.bootbuf, "-s", 1156*0Sstevel@tonic-gate sizeof (zarg.bootbuf)); 1157*0Sstevel@tonic-gate break; 1158*0Sstevel@tonic-gate default: 1159*0Sstevel@tonic-gate sub_usage(SHELP_BOOT, CMD_BOOT); 1160*0Sstevel@tonic-gate return (Z_USAGE); 1161*0Sstevel@tonic-gate } 1162*0Sstevel@tonic-gate } 1163*0Sstevel@tonic-gate if (argc > optind) { 1164*0Sstevel@tonic-gate sub_usage(SHELP_BOOT, CMD_BOOT); 1165*0Sstevel@tonic-gate return (Z_USAGE); 1166*0Sstevel@tonic-gate } 1167*0Sstevel@tonic-gate if (sanity_check(target_zone, CMD_BOOT, B_FALSE, B_FALSE) != Z_OK) 1168*0Sstevel@tonic-gate return (Z_ERR); 1169*0Sstevel@tonic-gate if (verify_details(CMD_BOOT) != Z_OK) 1170*0Sstevel@tonic-gate return (Z_ERR); 1171*0Sstevel@tonic-gate zarg.cmd = Z_BOOT; 1172*0Sstevel@tonic-gate if (call_zoneadmd(target_zone, &zarg) != 0) { 1173*0Sstevel@tonic-gate zerror(gettext("call to %s failed"), "zoneadmd"); 1174*0Sstevel@tonic-gate return (Z_ERR); 1175*0Sstevel@tonic-gate } 1176*0Sstevel@tonic-gate return (Z_OK); 1177*0Sstevel@tonic-gate } 1178*0Sstevel@tonic-gate 1179*0Sstevel@tonic-gate static void 1180*0Sstevel@tonic-gate fake_up_local_zone(zoneid_t zid, zone_entry_t *zeptr) 1181*0Sstevel@tonic-gate { 1182*0Sstevel@tonic-gate ssize_t result; 1183*0Sstevel@tonic-gate 1184*0Sstevel@tonic-gate zeptr->zid = zid; 1185*0Sstevel@tonic-gate /* 1186*0Sstevel@tonic-gate * Since we're looking up our own (non-global) zone name, 1187*0Sstevel@tonic-gate * we can be assured that it will succeed. 1188*0Sstevel@tonic-gate */ 1189*0Sstevel@tonic-gate result = getzonenamebyid(zid, zeptr->zname, sizeof (zeptr->zname)); 1190*0Sstevel@tonic-gate assert(result >= 0); 1191*0Sstevel@tonic-gate (void) strlcpy(zeptr->zroot, "/", sizeof (zeptr->zroot)); 1192*0Sstevel@tonic-gate zeptr->zstate_str = "running"; 1193*0Sstevel@tonic-gate } 1194*0Sstevel@tonic-gate 1195*0Sstevel@tonic-gate static int 1196*0Sstevel@tonic-gate list_func(int argc, char *argv[]) 1197*0Sstevel@tonic-gate { 1198*0Sstevel@tonic-gate zone_entry_t *zentp, zent; 1199*0Sstevel@tonic-gate int arg; 1200*0Sstevel@tonic-gate boolean_t output = B_FALSE, verbose = B_FALSE, parsable = B_FALSE; 1201*0Sstevel@tonic-gate zone_state_t min_state = ZONE_STATE_RUNNING; 1202*0Sstevel@tonic-gate zoneid_t zone_id = getzoneid(); 1203*0Sstevel@tonic-gate 1204*0Sstevel@tonic-gate if (target_zone == NULL) { 1205*0Sstevel@tonic-gate /* all zones: default view to running but allow override */ 1206*0Sstevel@tonic-gate optind = 0; 1207*0Sstevel@tonic-gate while ((arg = getopt(argc, argv, "?cipv")) != EOF) { 1208*0Sstevel@tonic-gate switch (arg) { 1209*0Sstevel@tonic-gate case '?': 1210*0Sstevel@tonic-gate sub_usage(SHELP_LIST, CMD_LIST); 1211*0Sstevel@tonic-gate return (optopt == '?' ? Z_OK : Z_USAGE); 1212*0Sstevel@tonic-gate case 'c': 1213*0Sstevel@tonic-gate min_state = ZONE_STATE_CONFIGURED; 1214*0Sstevel@tonic-gate break; 1215*0Sstevel@tonic-gate case 'i': 1216*0Sstevel@tonic-gate min_state = ZONE_STATE_INSTALLED; 1217*0Sstevel@tonic-gate break; 1218*0Sstevel@tonic-gate case 'p': 1219*0Sstevel@tonic-gate parsable = B_TRUE; 1220*0Sstevel@tonic-gate break; 1221*0Sstevel@tonic-gate case 'v': 1222*0Sstevel@tonic-gate verbose = B_TRUE; 1223*0Sstevel@tonic-gate break; 1224*0Sstevel@tonic-gate default: 1225*0Sstevel@tonic-gate sub_usage(SHELP_LIST, CMD_LIST); 1226*0Sstevel@tonic-gate return (Z_USAGE); 1227*0Sstevel@tonic-gate } 1228*0Sstevel@tonic-gate } 1229*0Sstevel@tonic-gate if (parsable && verbose) { 1230*0Sstevel@tonic-gate zerror(gettext("%s -p and -v are mutually exclusive."), 1231*0Sstevel@tonic-gate cmd_to_str(CMD_LIST)); 1232*0Sstevel@tonic-gate return (Z_ERR); 1233*0Sstevel@tonic-gate } 1234*0Sstevel@tonic-gate if (zone_id == GLOBAL_ZONEID) { 1235*0Sstevel@tonic-gate zone_print_list(min_state, verbose, parsable); 1236*0Sstevel@tonic-gate } else { 1237*0Sstevel@tonic-gate fake_up_local_zone(zone_id, &zent); 1238*0Sstevel@tonic-gate zone_print(&zent, verbose, parsable); 1239*0Sstevel@tonic-gate } 1240*0Sstevel@tonic-gate return (Z_OK); 1241*0Sstevel@tonic-gate } 1242*0Sstevel@tonic-gate 1243*0Sstevel@tonic-gate /* 1244*0Sstevel@tonic-gate * Specific target zone: disallow -i/-c suboptions. 1245*0Sstevel@tonic-gate */ 1246*0Sstevel@tonic-gate optind = 0; 1247*0Sstevel@tonic-gate while ((arg = getopt(argc, argv, "?pv")) != EOF) { 1248*0Sstevel@tonic-gate switch (arg) { 1249*0Sstevel@tonic-gate case '?': 1250*0Sstevel@tonic-gate sub_usage(SHELP_LIST, CMD_LIST); 1251*0Sstevel@tonic-gate return (optopt == '?' ? Z_OK : Z_USAGE); 1252*0Sstevel@tonic-gate case 'p': 1253*0Sstevel@tonic-gate parsable = B_TRUE; 1254*0Sstevel@tonic-gate break; 1255*0Sstevel@tonic-gate case 'v': 1256*0Sstevel@tonic-gate verbose = B_TRUE; 1257*0Sstevel@tonic-gate break; 1258*0Sstevel@tonic-gate default: 1259*0Sstevel@tonic-gate sub_usage(SHELP_LIST, CMD_LIST); 1260*0Sstevel@tonic-gate return (Z_USAGE); 1261*0Sstevel@tonic-gate } 1262*0Sstevel@tonic-gate } 1263*0Sstevel@tonic-gate if (parsable && verbose) { 1264*0Sstevel@tonic-gate zerror(gettext("%s -p and -v are mutually exclusive."), 1265*0Sstevel@tonic-gate cmd_to_str(CMD_LIST)); 1266*0Sstevel@tonic-gate return (Z_ERR); 1267*0Sstevel@tonic-gate } 1268*0Sstevel@tonic-gate if (argc > optind) { 1269*0Sstevel@tonic-gate sub_usage(SHELP_LIST, CMD_LIST); 1270*0Sstevel@tonic-gate return (Z_USAGE); 1271*0Sstevel@tonic-gate } 1272*0Sstevel@tonic-gate if (zone_id != GLOBAL_ZONEID) { 1273*0Sstevel@tonic-gate fake_up_local_zone(zone_id, &zent); 1274*0Sstevel@tonic-gate /* 1275*0Sstevel@tonic-gate * main() will issue a Z_NO_ZONE error if it cannot get an 1276*0Sstevel@tonic-gate * id for target_zone, which in a non-global zone should 1277*0Sstevel@tonic-gate * happen for any zone name except `zonename`. Thus we 1278*0Sstevel@tonic-gate * assert() that here but don't otherwise check. 1279*0Sstevel@tonic-gate */ 1280*0Sstevel@tonic-gate assert(strcmp(zent.zname, target_zone) == 0); 1281*0Sstevel@tonic-gate zone_print(&zent, verbose, parsable); 1282*0Sstevel@tonic-gate output = B_TRUE; 1283*0Sstevel@tonic-gate } else if ((zentp = lookup_running_zone(target_zone)) != NULL) { 1284*0Sstevel@tonic-gate zone_print(zentp, verbose, parsable); 1285*0Sstevel@tonic-gate output = B_TRUE; 1286*0Sstevel@tonic-gate } else if (lookup_zone_info(target_zone, &zent) == Z_OK) { 1287*0Sstevel@tonic-gate zone_print(&zent, verbose, parsable); 1288*0Sstevel@tonic-gate output = B_TRUE; 1289*0Sstevel@tonic-gate } 1290*0Sstevel@tonic-gate return (output ? Z_OK : Z_ERR); 1291*0Sstevel@tonic-gate } 1292*0Sstevel@tonic-gate 1293*0Sstevel@tonic-gate static void 1294*0Sstevel@tonic-gate sigterm(int sig) 1295*0Sstevel@tonic-gate { 1296*0Sstevel@tonic-gate /* 1297*0Sstevel@tonic-gate * Ignore SIG{INT,TERM}, so we don't end up in an infinite loop, 1298*0Sstevel@tonic-gate * then propagate the signal to our process group. 1299*0Sstevel@tonic-gate */ 1300*0Sstevel@tonic-gate (void) sigset(SIGINT, SIG_IGN); 1301*0Sstevel@tonic-gate (void) sigset(SIGTERM, SIG_IGN); 1302*0Sstevel@tonic-gate (void) kill(0, sig); 1303*0Sstevel@tonic-gate child_killed = B_TRUE; 1304*0Sstevel@tonic-gate } 1305*0Sstevel@tonic-gate 1306*0Sstevel@tonic-gate static int 1307*0Sstevel@tonic-gate do_subproc(char *cmdbuf) 1308*0Sstevel@tonic-gate { 1309*0Sstevel@tonic-gate char inbuf[1024]; /* arbitrary large amount */ 1310*0Sstevel@tonic-gate FILE *file; 1311*0Sstevel@tonic-gate 1312*0Sstevel@tonic-gate child_killed = B_FALSE; 1313*0Sstevel@tonic-gate /* 1314*0Sstevel@tonic-gate * We use popen(3c) to launch child processes for [un]install; 1315*0Sstevel@tonic-gate * this library call does not return a PID, so we have to kill 1316*0Sstevel@tonic-gate * the whole process group. To avoid killing our parent, we 1317*0Sstevel@tonic-gate * become a process group leader here. But doing so can wreak 1318*0Sstevel@tonic-gate * havoc with reading from stdin when launched by a non-job-control 1319*0Sstevel@tonic-gate * shell, so we close stdin and reopen it as /dev/null first. 1320*0Sstevel@tonic-gate */ 1321*0Sstevel@tonic-gate (void) close(STDIN_FILENO); 1322*0Sstevel@tonic-gate (void) open("/dev/null", O_RDONLY); 1323*0Sstevel@tonic-gate (void) setpgid(0, 0); 1324*0Sstevel@tonic-gate (void) sigset(SIGINT, sigterm); 1325*0Sstevel@tonic-gate (void) sigset(SIGTERM, sigterm); 1326*0Sstevel@tonic-gate file = popen(cmdbuf, "r"); 1327*0Sstevel@tonic-gate for (;;) { 1328*0Sstevel@tonic-gate if (child_killed || fgets(inbuf, sizeof (inbuf), file) == NULL) 1329*0Sstevel@tonic-gate break; 1330*0Sstevel@tonic-gate (void) fputs(inbuf, stdout); 1331*0Sstevel@tonic-gate } 1332*0Sstevel@tonic-gate (void) sigset(SIGINT, SIG_DFL); 1333*0Sstevel@tonic-gate (void) sigset(SIGTERM, SIG_DFL); 1334*0Sstevel@tonic-gate return (pclose(file)); 1335*0Sstevel@tonic-gate } 1336*0Sstevel@tonic-gate 1337*0Sstevel@tonic-gate static int 1338*0Sstevel@tonic-gate subproc_status(const char *cmd, int status) 1339*0Sstevel@tonic-gate { 1340*0Sstevel@tonic-gate if (WIFEXITED(status)) { 1341*0Sstevel@tonic-gate int exit_code = WEXITSTATUS(status); 1342*0Sstevel@tonic-gate 1343*0Sstevel@tonic-gate if (exit_code == 0) 1344*0Sstevel@tonic-gate return (Z_OK); 1345*0Sstevel@tonic-gate zerror(gettext("'%s' failed with exit code %d."), cmd, 1346*0Sstevel@tonic-gate exit_code); 1347*0Sstevel@tonic-gate } else if (WIFSIGNALED(status)) { 1348*0Sstevel@tonic-gate int signal = WTERMSIG(status); 1349*0Sstevel@tonic-gate char sigstr[SIG2STR_MAX]; 1350*0Sstevel@tonic-gate 1351*0Sstevel@tonic-gate if (sig2str(signal, sigstr) == 0) { 1352*0Sstevel@tonic-gate zerror(gettext("'%s' terminated by signal SIG%s."), cmd, 1353*0Sstevel@tonic-gate sigstr); 1354*0Sstevel@tonic-gate } else { 1355*0Sstevel@tonic-gate zerror(gettext("'%s' terminated by an unknown signal."), 1356*0Sstevel@tonic-gate cmd); 1357*0Sstevel@tonic-gate } 1358*0Sstevel@tonic-gate } else { 1359*0Sstevel@tonic-gate zerror(gettext("'%s' failed for unknown reasons."), cmd); 1360*0Sstevel@tonic-gate } 1361*0Sstevel@tonic-gate return (Z_ERR); 1362*0Sstevel@tonic-gate } 1363*0Sstevel@tonic-gate 1364*0Sstevel@tonic-gate /* 1365*0Sstevel@tonic-gate * Various sanity checks; make sure: 1366*0Sstevel@tonic-gate * 1. We're in the global zone. 1367*0Sstevel@tonic-gate * 2. The calling user has sufficient privilege. 1368*0Sstevel@tonic-gate * 3. The target zone is neither the global zone nor anything starting with 1369*0Sstevel@tonic-gate * "SUNW". 1370*0Sstevel@tonic-gate * 4a. If we're looking for a 'not running' (i.e., configured or installed) 1371*0Sstevel@tonic-gate * zone, the name service knows about it. 1372*0Sstevel@tonic-gate * 4b. For some operations which expect a zone not to be running, that it is 1373*0Sstevel@tonic-gate * not already running (or ready). 1374*0Sstevel@tonic-gate */ 1375*0Sstevel@tonic-gate static int 1376*0Sstevel@tonic-gate sanity_check(char *zone, int cmd_num, boolean_t running, 1377*0Sstevel@tonic-gate boolean_t unsafe_when_running) 1378*0Sstevel@tonic-gate { 1379*0Sstevel@tonic-gate zone_entry_t *zent; 1380*0Sstevel@tonic-gate priv_set_t *privset; 1381*0Sstevel@tonic-gate zone_state_t state; 1382*0Sstevel@tonic-gate 1383*0Sstevel@tonic-gate if (getzoneid() != GLOBAL_ZONEID) { 1384*0Sstevel@tonic-gate zerror(gettext("must be in the global zone to %s a zone."), 1385*0Sstevel@tonic-gate cmd_to_str(cmd_num)); 1386*0Sstevel@tonic-gate return (Z_ERR); 1387*0Sstevel@tonic-gate } 1388*0Sstevel@tonic-gate 1389*0Sstevel@tonic-gate if ((privset = priv_allocset()) == NULL) { 1390*0Sstevel@tonic-gate zerror(gettext("%s failed"), "priv_allocset"); 1391*0Sstevel@tonic-gate return (Z_ERR); 1392*0Sstevel@tonic-gate } 1393*0Sstevel@tonic-gate 1394*0Sstevel@tonic-gate if (getppriv(PRIV_EFFECTIVE, privset) != 0) { 1395*0Sstevel@tonic-gate zerror(gettext("%s failed"), "getppriv"); 1396*0Sstevel@tonic-gate priv_freeset(privset); 1397*0Sstevel@tonic-gate return (Z_ERR); 1398*0Sstevel@tonic-gate } 1399*0Sstevel@tonic-gate 1400*0Sstevel@tonic-gate if (priv_isfullset(privset) == B_FALSE) { 1401*0Sstevel@tonic-gate zerror(gettext("only a privileged user may %s a zone."), 1402*0Sstevel@tonic-gate cmd_to_str(cmd_num)); 1403*0Sstevel@tonic-gate priv_freeset(privset); 1404*0Sstevel@tonic-gate return (Z_ERR); 1405*0Sstevel@tonic-gate } 1406*0Sstevel@tonic-gate priv_freeset(privset); 1407*0Sstevel@tonic-gate 1408*0Sstevel@tonic-gate if (zone == NULL) { 1409*0Sstevel@tonic-gate zerror(gettext("no zone specified")); 1410*0Sstevel@tonic-gate return (Z_ERR); 1411*0Sstevel@tonic-gate } 1412*0Sstevel@tonic-gate 1413*0Sstevel@tonic-gate zent = lookup_running_zone(zone); 1414*0Sstevel@tonic-gate 1415*0Sstevel@tonic-gate if (strcmp(zone, GLOBAL_ZONENAME) == 0) { 1416*0Sstevel@tonic-gate assert((zent != NULL) && (zent->zid == GLOBAL_ZONEID)); 1417*0Sstevel@tonic-gate zerror(gettext("%s operation is invalid for the global zone."), 1418*0Sstevel@tonic-gate cmd_to_str(cmd_num)); 1419*0Sstevel@tonic-gate return (Z_ERR); 1420*0Sstevel@tonic-gate } 1421*0Sstevel@tonic-gate 1422*0Sstevel@tonic-gate if (strncmp(zone, "SUNW", 4) == 0) { 1423*0Sstevel@tonic-gate zerror(gettext("%s operation is invalid for zones starting " 1424*0Sstevel@tonic-gate "with SUNW."), cmd_to_str(cmd_num)); 1425*0Sstevel@tonic-gate return (Z_ERR); 1426*0Sstevel@tonic-gate } 1427*0Sstevel@tonic-gate 1428*0Sstevel@tonic-gate /* 1429*0Sstevel@tonic-gate * Look up from the kernel for 'running' zones. 1430*0Sstevel@tonic-gate */ 1431*0Sstevel@tonic-gate if (running) { 1432*0Sstevel@tonic-gate if (zent == NULL) { 1433*0Sstevel@tonic-gate zerror(gettext("not running")); 1434*0Sstevel@tonic-gate return (Z_ERR); 1435*0Sstevel@tonic-gate } 1436*0Sstevel@tonic-gate } else { 1437*0Sstevel@tonic-gate int err; 1438*0Sstevel@tonic-gate 1439*0Sstevel@tonic-gate if (unsafe_when_running && zent != NULL) { 1440*0Sstevel@tonic-gate /* check whether the zone is ready or running */ 1441*0Sstevel@tonic-gate if ((err = zone_get_state(zent->zname, 1442*0Sstevel@tonic-gate &zent->zstate_num)) != Z_OK) { 1443*0Sstevel@tonic-gate errno = err; 1444*0Sstevel@tonic-gate zperror2(zent->zname, 1445*0Sstevel@tonic-gate gettext("could not get state")); 1446*0Sstevel@tonic-gate /* can't tell, so hedge */ 1447*0Sstevel@tonic-gate zent->zstate_str = "ready/running"; 1448*0Sstevel@tonic-gate } else { 1449*0Sstevel@tonic-gate zent->zstate_str = 1450*0Sstevel@tonic-gate zone_state_str(zent->zstate_num); 1451*0Sstevel@tonic-gate } 1452*0Sstevel@tonic-gate zerror(gettext("%s operation is invalid for %s zones."), 1453*0Sstevel@tonic-gate cmd_to_str(cmd_num), zent->zstate_str); 1454*0Sstevel@tonic-gate return (Z_ERR); 1455*0Sstevel@tonic-gate } 1456*0Sstevel@tonic-gate if ((err = zone_get_state(zone, &state)) != Z_OK) { 1457*0Sstevel@tonic-gate errno = err; 1458*0Sstevel@tonic-gate zperror2(zone, gettext("could not get state")); 1459*0Sstevel@tonic-gate return (Z_ERR); 1460*0Sstevel@tonic-gate } 1461*0Sstevel@tonic-gate switch (cmd_num) { 1462*0Sstevel@tonic-gate case CMD_UNINSTALL: 1463*0Sstevel@tonic-gate if (state == ZONE_STATE_CONFIGURED) { 1464*0Sstevel@tonic-gate zerror(gettext("is already in state '%s'."), 1465*0Sstevel@tonic-gate zone_state_str(ZONE_STATE_CONFIGURED)); 1466*0Sstevel@tonic-gate return (Z_ERR); 1467*0Sstevel@tonic-gate } 1468*0Sstevel@tonic-gate break; 1469*0Sstevel@tonic-gate case CMD_INSTALL: 1470*0Sstevel@tonic-gate if (state == ZONE_STATE_INSTALLED) { 1471*0Sstevel@tonic-gate zerror(gettext("is already %s."), 1472*0Sstevel@tonic-gate zone_state_str(ZONE_STATE_INSTALLED)); 1473*0Sstevel@tonic-gate return (Z_ERR); 1474*0Sstevel@tonic-gate } else if (state == ZONE_STATE_INCOMPLETE) { 1475*0Sstevel@tonic-gate zerror(gettext("zone is %s; %s required."), 1476*0Sstevel@tonic-gate zone_state_str(ZONE_STATE_INCOMPLETE), 1477*0Sstevel@tonic-gate cmd_to_str(CMD_UNINSTALL)); 1478*0Sstevel@tonic-gate return (Z_ERR); 1479*0Sstevel@tonic-gate } 1480*0Sstevel@tonic-gate break; 1481*0Sstevel@tonic-gate case CMD_READY: 1482*0Sstevel@tonic-gate case CMD_BOOT: 1483*0Sstevel@tonic-gate if (state < ZONE_STATE_INSTALLED) { 1484*0Sstevel@tonic-gate zerror(gettext("must be %s before %s."), 1485*0Sstevel@tonic-gate zone_state_str(ZONE_STATE_INSTALLED), 1486*0Sstevel@tonic-gate cmd_to_str(cmd_num)); 1487*0Sstevel@tonic-gate return (Z_ERR); 1488*0Sstevel@tonic-gate } 1489*0Sstevel@tonic-gate break; 1490*0Sstevel@tonic-gate case CMD_VERIFY: 1491*0Sstevel@tonic-gate if (state == ZONE_STATE_INCOMPLETE) { 1492*0Sstevel@tonic-gate zerror(gettext("zone is %s; %s required."), 1493*0Sstevel@tonic-gate zone_state_str(ZONE_STATE_INCOMPLETE), 1494*0Sstevel@tonic-gate cmd_to_str(CMD_UNINSTALL)); 1495*0Sstevel@tonic-gate return (Z_ERR); 1496*0Sstevel@tonic-gate } 1497*0Sstevel@tonic-gate break; 1498*0Sstevel@tonic-gate } 1499*0Sstevel@tonic-gate } 1500*0Sstevel@tonic-gate return (Z_OK); 1501*0Sstevel@tonic-gate } 1502*0Sstevel@tonic-gate 1503*0Sstevel@tonic-gate static int 1504*0Sstevel@tonic-gate halt_func(int argc, char *argv[]) 1505*0Sstevel@tonic-gate { 1506*0Sstevel@tonic-gate zone_cmd_arg_t zarg; 1507*0Sstevel@tonic-gate int arg; 1508*0Sstevel@tonic-gate 1509*0Sstevel@tonic-gate optind = 0; 1510*0Sstevel@tonic-gate if ((arg = getopt(argc, argv, "?")) != EOF) { 1511*0Sstevel@tonic-gate switch (arg) { 1512*0Sstevel@tonic-gate case '?': 1513*0Sstevel@tonic-gate sub_usage(SHELP_HALT, CMD_HALT); 1514*0Sstevel@tonic-gate return (optopt == '?' ? Z_OK : Z_USAGE); 1515*0Sstevel@tonic-gate default: 1516*0Sstevel@tonic-gate sub_usage(SHELP_HALT, CMD_HALT); 1517*0Sstevel@tonic-gate return (Z_USAGE); 1518*0Sstevel@tonic-gate } 1519*0Sstevel@tonic-gate } 1520*0Sstevel@tonic-gate if (argc > optind) { 1521*0Sstevel@tonic-gate sub_usage(SHELP_HALT, CMD_HALT); 1522*0Sstevel@tonic-gate return (Z_USAGE); 1523*0Sstevel@tonic-gate } 1524*0Sstevel@tonic-gate /* 1525*0Sstevel@tonic-gate * zoneadmd should be the one to decide whether or not to proceed, 1526*0Sstevel@tonic-gate * so even though it seems that the fourth parameter below should 1527*0Sstevel@tonic-gate * perhaps be B_TRUE, it really shouldn't be. 1528*0Sstevel@tonic-gate */ 1529*0Sstevel@tonic-gate if (sanity_check(target_zone, CMD_HALT, B_FALSE, B_FALSE) != Z_OK) 1530*0Sstevel@tonic-gate return (Z_ERR); 1531*0Sstevel@tonic-gate 1532*0Sstevel@tonic-gate zarg.cmd = Z_HALT; 1533*0Sstevel@tonic-gate return ((call_zoneadmd(target_zone, &zarg) == 0) ? Z_OK : Z_ERR); 1534*0Sstevel@tonic-gate } 1535*0Sstevel@tonic-gate 1536*0Sstevel@tonic-gate static int 1537*0Sstevel@tonic-gate reboot_func(int argc, char *argv[]) 1538*0Sstevel@tonic-gate { 1539*0Sstevel@tonic-gate zone_cmd_arg_t zarg; 1540*0Sstevel@tonic-gate int arg; 1541*0Sstevel@tonic-gate 1542*0Sstevel@tonic-gate optind = 0; 1543*0Sstevel@tonic-gate if ((arg = getopt(argc, argv, "?")) != EOF) { 1544*0Sstevel@tonic-gate switch (arg) { 1545*0Sstevel@tonic-gate case '?': 1546*0Sstevel@tonic-gate sub_usage(SHELP_REBOOT, CMD_REBOOT); 1547*0Sstevel@tonic-gate return (optopt == '?' ? Z_OK : Z_USAGE); 1548*0Sstevel@tonic-gate default: 1549*0Sstevel@tonic-gate sub_usage(SHELP_REBOOT, CMD_REBOOT); 1550*0Sstevel@tonic-gate return (Z_USAGE); 1551*0Sstevel@tonic-gate } 1552*0Sstevel@tonic-gate } 1553*0Sstevel@tonic-gate if (argc > 0) { 1554*0Sstevel@tonic-gate sub_usage(SHELP_REBOOT, CMD_REBOOT); 1555*0Sstevel@tonic-gate return (Z_USAGE); 1556*0Sstevel@tonic-gate } 1557*0Sstevel@tonic-gate /* 1558*0Sstevel@tonic-gate * zoneadmd should be the one to decide whether or not to proceed, 1559*0Sstevel@tonic-gate * so even though it seems that the fourth parameter below should 1560*0Sstevel@tonic-gate * perhaps be B_TRUE, it really shouldn't be. 1561*0Sstevel@tonic-gate */ 1562*0Sstevel@tonic-gate if (sanity_check(target_zone, CMD_REBOOT, B_TRUE, B_FALSE) != Z_OK) 1563*0Sstevel@tonic-gate return (Z_ERR); 1564*0Sstevel@tonic-gate 1565*0Sstevel@tonic-gate zarg.cmd = Z_REBOOT; 1566*0Sstevel@tonic-gate return ((call_zoneadmd(target_zone, &zarg) == 0) ? Z_OK : Z_ERR); 1567*0Sstevel@tonic-gate } 1568*0Sstevel@tonic-gate 1569*0Sstevel@tonic-gate static int 1570*0Sstevel@tonic-gate verify_rctls(zone_dochandle_t handle) 1571*0Sstevel@tonic-gate { 1572*0Sstevel@tonic-gate struct zone_rctltab rctltab; 1573*0Sstevel@tonic-gate size_t rbs = rctlblk_size(); 1574*0Sstevel@tonic-gate rctlblk_t *rctlblk; 1575*0Sstevel@tonic-gate int error = Z_INVAL; 1576*0Sstevel@tonic-gate 1577*0Sstevel@tonic-gate if ((rctlblk = malloc(rbs)) == NULL) { 1578*0Sstevel@tonic-gate zerror(gettext("failed to allocate %lu bytes: %s"), rbs, 1579*0Sstevel@tonic-gate strerror(errno)); 1580*0Sstevel@tonic-gate return (Z_NOMEM); 1581*0Sstevel@tonic-gate } 1582*0Sstevel@tonic-gate 1583*0Sstevel@tonic-gate if (zonecfg_setrctlent(handle) != Z_OK) { 1584*0Sstevel@tonic-gate zerror(gettext("zonecfg_setrctlent failed")); 1585*0Sstevel@tonic-gate free(rctlblk); 1586*0Sstevel@tonic-gate return (error); 1587*0Sstevel@tonic-gate } 1588*0Sstevel@tonic-gate 1589*0Sstevel@tonic-gate rctltab.zone_rctl_valptr = NULL; 1590*0Sstevel@tonic-gate while (zonecfg_getrctlent(handle, &rctltab) == Z_OK) { 1591*0Sstevel@tonic-gate struct zone_rctlvaltab *rctlval; 1592*0Sstevel@tonic-gate const char *name = rctltab.zone_rctl_name; 1593*0Sstevel@tonic-gate 1594*0Sstevel@tonic-gate if (!zonecfg_is_rctl(name)) { 1595*0Sstevel@tonic-gate zerror(gettext("WARNING: Ignoring unrecognized rctl " 1596*0Sstevel@tonic-gate "'%s'."), name); 1597*0Sstevel@tonic-gate zonecfg_free_rctl_value_list(rctltab.zone_rctl_valptr); 1598*0Sstevel@tonic-gate rctltab.zone_rctl_valptr = NULL; 1599*0Sstevel@tonic-gate continue; 1600*0Sstevel@tonic-gate } 1601*0Sstevel@tonic-gate 1602*0Sstevel@tonic-gate for (rctlval = rctltab.zone_rctl_valptr; rctlval != NULL; 1603*0Sstevel@tonic-gate rctlval = rctlval->zone_rctlval_next) { 1604*0Sstevel@tonic-gate if (zonecfg_construct_rctlblk(rctlval, rctlblk) 1605*0Sstevel@tonic-gate != Z_OK) { 1606*0Sstevel@tonic-gate zerror(gettext("invalid rctl value: " 1607*0Sstevel@tonic-gate "(priv=%s,limit=%s,action%s)"), 1608*0Sstevel@tonic-gate rctlval->zone_rctlval_priv, 1609*0Sstevel@tonic-gate rctlval->zone_rctlval_limit, 1610*0Sstevel@tonic-gate rctlval->zone_rctlval_action); 1611*0Sstevel@tonic-gate goto out; 1612*0Sstevel@tonic-gate } 1613*0Sstevel@tonic-gate if (!zonecfg_valid_rctl(name, rctlblk)) { 1614*0Sstevel@tonic-gate zerror(gettext("(priv=%s,limit=%s,action=%s) " 1615*0Sstevel@tonic-gate "is not a valid value for rctl '%s'"), 1616*0Sstevel@tonic-gate rctlval->zone_rctlval_priv, 1617*0Sstevel@tonic-gate rctlval->zone_rctlval_limit, 1618*0Sstevel@tonic-gate rctlval->zone_rctlval_action, 1619*0Sstevel@tonic-gate name); 1620*0Sstevel@tonic-gate goto out; 1621*0Sstevel@tonic-gate } 1622*0Sstevel@tonic-gate } 1623*0Sstevel@tonic-gate zonecfg_free_rctl_value_list(rctltab.zone_rctl_valptr); 1624*0Sstevel@tonic-gate } 1625*0Sstevel@tonic-gate rctltab.zone_rctl_valptr = NULL; 1626*0Sstevel@tonic-gate error = Z_OK; 1627*0Sstevel@tonic-gate out: 1628*0Sstevel@tonic-gate zonecfg_free_rctl_value_list(rctltab.zone_rctl_valptr); 1629*0Sstevel@tonic-gate (void) zonecfg_endrctlent(handle); 1630*0Sstevel@tonic-gate free(rctlblk); 1631*0Sstevel@tonic-gate return (error); 1632*0Sstevel@tonic-gate } 1633*0Sstevel@tonic-gate 1634*0Sstevel@tonic-gate static int 1635*0Sstevel@tonic-gate verify_pool(zone_dochandle_t handle) 1636*0Sstevel@tonic-gate { 1637*0Sstevel@tonic-gate char poolname[MAXPATHLEN]; 1638*0Sstevel@tonic-gate pool_conf_t *poolconf; 1639*0Sstevel@tonic-gate pool_t *pool; 1640*0Sstevel@tonic-gate int status; 1641*0Sstevel@tonic-gate int error; 1642*0Sstevel@tonic-gate 1643*0Sstevel@tonic-gate /* 1644*0Sstevel@tonic-gate * This ends up being very similar to the check done in zoneadmd. 1645*0Sstevel@tonic-gate */ 1646*0Sstevel@tonic-gate error = zonecfg_get_pool(handle, poolname, sizeof (poolname)); 1647*0Sstevel@tonic-gate if (error == Z_NO_ENTRY || (error == Z_OK && strlen(poolname) == 0)) { 1648*0Sstevel@tonic-gate /* 1649*0Sstevel@tonic-gate * No pool specified. 1650*0Sstevel@tonic-gate */ 1651*0Sstevel@tonic-gate return (0); 1652*0Sstevel@tonic-gate } 1653*0Sstevel@tonic-gate if (error != Z_OK) { 1654*0Sstevel@tonic-gate zperror(gettext("Unable to retrieve pool name from " 1655*0Sstevel@tonic-gate "configuration"), B_TRUE); 1656*0Sstevel@tonic-gate return (error); 1657*0Sstevel@tonic-gate } 1658*0Sstevel@tonic-gate /* 1659*0Sstevel@tonic-gate * Don't do anything if pools aren't enabled. 1660*0Sstevel@tonic-gate */ 1661*0Sstevel@tonic-gate if (pool_get_status(&status) != PO_SUCCESS || status != POOL_ENABLED) { 1662*0Sstevel@tonic-gate zerror(gettext("WARNING: pools facility not active; " 1663*0Sstevel@tonic-gate "zone will not be bound to pool '%s'."), poolname); 1664*0Sstevel@tonic-gate return (Z_OK); 1665*0Sstevel@tonic-gate } 1666*0Sstevel@tonic-gate /* 1667*0Sstevel@tonic-gate * Try to provide a sane error message if the requested pool doesn't 1668*0Sstevel@tonic-gate * exist. It isn't clear that pools-related failures should 1669*0Sstevel@tonic-gate * necessarily translate to a failure to verify the zone configuration, 1670*0Sstevel@tonic-gate * hence they are not considered errors. 1671*0Sstevel@tonic-gate */ 1672*0Sstevel@tonic-gate if ((poolconf = pool_conf_alloc()) == NULL) { 1673*0Sstevel@tonic-gate zerror(gettext("WARNING: pool_conf_alloc failed; " 1674*0Sstevel@tonic-gate "using default pool")); 1675*0Sstevel@tonic-gate return (Z_OK); 1676*0Sstevel@tonic-gate } 1677*0Sstevel@tonic-gate if (pool_conf_open(poolconf, pool_dynamic_location(), PO_RDONLY) != 1678*0Sstevel@tonic-gate PO_SUCCESS) { 1679*0Sstevel@tonic-gate zerror(gettext("WARNING: pool_conf_open failed; " 1680*0Sstevel@tonic-gate "using default pool")); 1681*0Sstevel@tonic-gate pool_conf_free(poolconf); 1682*0Sstevel@tonic-gate return (Z_OK); 1683*0Sstevel@tonic-gate } 1684*0Sstevel@tonic-gate pool = pool_get_pool(poolconf, poolname); 1685*0Sstevel@tonic-gate (void) pool_conf_close(poolconf); 1686*0Sstevel@tonic-gate pool_conf_free(poolconf); 1687*0Sstevel@tonic-gate if (pool == NULL) { 1688*0Sstevel@tonic-gate zerror(gettext("WARNING: pool '%s' not found. " 1689*0Sstevel@tonic-gate "using default pool"), poolname); 1690*0Sstevel@tonic-gate } 1691*0Sstevel@tonic-gate 1692*0Sstevel@tonic-gate return (Z_OK); 1693*0Sstevel@tonic-gate } 1694*0Sstevel@tonic-gate 1695*0Sstevel@tonic-gate static int 1696*0Sstevel@tonic-gate verify_filesystems(zone_dochandle_t handle) 1697*0Sstevel@tonic-gate { 1698*0Sstevel@tonic-gate int return_code = Z_OK; 1699*0Sstevel@tonic-gate struct zone_fstab fstab; 1700*0Sstevel@tonic-gate char cmdbuf[MAXPATHLEN]; 1701*0Sstevel@tonic-gate struct stat st; 1702*0Sstevel@tonic-gate 1703*0Sstevel@tonic-gate /* 1704*0Sstevel@tonic-gate * No need to verify inherit-pkg-dir fs types, as their type is 1705*0Sstevel@tonic-gate * implicitly lofs, which is known. Therefore, the types are only 1706*0Sstevel@tonic-gate * verified for regular filesystems below. 1707*0Sstevel@tonic-gate * 1708*0Sstevel@tonic-gate * Since the actual mount point is not known until the dependent mounts 1709*0Sstevel@tonic-gate * are performed, we don't attempt any path validation here: that will 1710*0Sstevel@tonic-gate * happen later when zoneadmd actually does the mounts. 1711*0Sstevel@tonic-gate */ 1712*0Sstevel@tonic-gate if (zonecfg_setfsent(handle) != Z_OK) { 1713*0Sstevel@tonic-gate (void) fprintf(stderr, gettext("cannot verify file-systems: " 1714*0Sstevel@tonic-gate "unable to enumerate mounts\n")); 1715*0Sstevel@tonic-gate return (Z_ERR); 1716*0Sstevel@tonic-gate } 1717*0Sstevel@tonic-gate while (zonecfg_getfsent(handle, &fstab) == Z_OK) { 1718*0Sstevel@tonic-gate if (!zonecfg_valid_fs_type(fstab.zone_fs_type)) { 1719*0Sstevel@tonic-gate (void) fprintf(stderr, gettext("cannot verify fs %s: " 1720*0Sstevel@tonic-gate "type %s is not allowed.\n"), fstab.zone_fs_dir, 1721*0Sstevel@tonic-gate fstab.zone_fs_type); 1722*0Sstevel@tonic-gate return_code = Z_ERR; 1723*0Sstevel@tonic-gate goto next_fs; 1724*0Sstevel@tonic-gate } 1725*0Sstevel@tonic-gate /* 1726*0Sstevel@tonic-gate * Verify /usr/lib/fs/<fstype>/mount exists. 1727*0Sstevel@tonic-gate */ 1728*0Sstevel@tonic-gate if (snprintf(cmdbuf, sizeof (cmdbuf), "/usr/lib/fs/%s/mount", 1729*0Sstevel@tonic-gate fstab.zone_fs_type) > sizeof (cmdbuf)) { 1730*0Sstevel@tonic-gate (void) fprintf(stderr, gettext("cannot verify fs %s: " 1731*0Sstevel@tonic-gate "type %s is too long.\n"), fstab.zone_fs_dir, 1732*0Sstevel@tonic-gate fstab.zone_fs_type); 1733*0Sstevel@tonic-gate return_code = Z_ERR; 1734*0Sstevel@tonic-gate goto next_fs; 1735*0Sstevel@tonic-gate } 1736*0Sstevel@tonic-gate if (stat(cmdbuf, &st) != 0) { 1737*0Sstevel@tonic-gate (void) fprintf(stderr, gettext("cannot verify fs %s: " 1738*0Sstevel@tonic-gate "can't access %s: %s\n"), fstab.zone_fs_dir, 1739*0Sstevel@tonic-gate cmdbuf, strerror(errno)); 1740*0Sstevel@tonic-gate return_code = Z_ERR; 1741*0Sstevel@tonic-gate goto next_fs; 1742*0Sstevel@tonic-gate } 1743*0Sstevel@tonic-gate if (!S_ISREG(st.st_mode)) { 1744*0Sstevel@tonic-gate (void) fprintf(stderr, gettext("cannot verify fs %s: " 1745*0Sstevel@tonic-gate "%s is not a regular file\n"), fstab.zone_fs_dir, 1746*0Sstevel@tonic-gate cmdbuf); 1747*0Sstevel@tonic-gate return_code = Z_ERR; 1748*0Sstevel@tonic-gate goto next_fs; 1749*0Sstevel@tonic-gate } 1750*0Sstevel@tonic-gate /* 1751*0Sstevel@tonic-gate * Verify /usr/lib/fs/<fstype>/fsck exists iff zone_fs_raw is 1752*0Sstevel@tonic-gate * set. 1753*0Sstevel@tonic-gate */ 1754*0Sstevel@tonic-gate if (snprintf(cmdbuf, sizeof (cmdbuf), "/usr/lib/fs/%s/fsck", 1755*0Sstevel@tonic-gate fstab.zone_fs_type) > sizeof (cmdbuf)) { 1756*0Sstevel@tonic-gate (void) fprintf(stderr, gettext("cannot verify fs %s: " 1757*0Sstevel@tonic-gate "type %s is too long.\n"), fstab.zone_fs_dir, 1758*0Sstevel@tonic-gate fstab.zone_fs_type); 1759*0Sstevel@tonic-gate return_code = Z_ERR; 1760*0Sstevel@tonic-gate goto next_fs; 1761*0Sstevel@tonic-gate } 1762*0Sstevel@tonic-gate if (fstab.zone_fs_raw[0] == '\0' && stat(cmdbuf, &st) == 0) { 1763*0Sstevel@tonic-gate (void) fprintf(stderr, gettext("cannot verify fs %s: " 1764*0Sstevel@tonic-gate "must specify 'raw' device for %s file-systems\n"), 1765*0Sstevel@tonic-gate fstab.zone_fs_dir, fstab.zone_fs_type); 1766*0Sstevel@tonic-gate return_code = Z_ERR; 1767*0Sstevel@tonic-gate goto next_fs; 1768*0Sstevel@tonic-gate } 1769*0Sstevel@tonic-gate if (fstab.zone_fs_raw[0] != '\0' && 1770*0Sstevel@tonic-gate (stat(cmdbuf, &st) != 0 || !S_ISREG(st.st_mode))) { 1771*0Sstevel@tonic-gate (void) fprintf(stderr, gettext("cannot verify fs %s: " 1772*0Sstevel@tonic-gate "'raw' device specified but " 1773*0Sstevel@tonic-gate "no fsck executable exists for %s\n"), 1774*0Sstevel@tonic-gate fstab.zone_fs_dir, fstab.zone_fs_type); 1775*0Sstevel@tonic-gate return_code = Z_ERR; 1776*0Sstevel@tonic-gate goto next_fs; 1777*0Sstevel@tonic-gate } 1778*0Sstevel@tonic-gate next_fs: 1779*0Sstevel@tonic-gate zonecfg_free_fs_option_list(fstab.zone_fs_options); 1780*0Sstevel@tonic-gate } 1781*0Sstevel@tonic-gate (void) zonecfg_endfsent(handle); 1782*0Sstevel@tonic-gate 1783*0Sstevel@tonic-gate return (return_code); 1784*0Sstevel@tonic-gate } 1785*0Sstevel@tonic-gate 1786*0Sstevel@tonic-gate static int 1787*0Sstevel@tonic-gate verify_details(int cmd_num) 1788*0Sstevel@tonic-gate { 1789*0Sstevel@tonic-gate zone_dochandle_t handle; 1790*0Sstevel@tonic-gate struct zone_nwiftab nwiftab; 1791*0Sstevel@tonic-gate char zonepath[MAXPATHLEN], checkpath[MAXPATHLEN]; 1792*0Sstevel@tonic-gate int return_code = Z_OK; 1793*0Sstevel@tonic-gate int err; 1794*0Sstevel@tonic-gate 1795*0Sstevel@tonic-gate if ((handle = zonecfg_init_handle()) == NULL) { 1796*0Sstevel@tonic-gate zperror(cmd_to_str(cmd_num), B_TRUE); 1797*0Sstevel@tonic-gate return (Z_ERR); 1798*0Sstevel@tonic-gate } 1799*0Sstevel@tonic-gate if ((err = zonecfg_get_handle(target_zone, handle)) != Z_OK) { 1800*0Sstevel@tonic-gate errno = err; 1801*0Sstevel@tonic-gate zperror(cmd_to_str(cmd_num), B_TRUE); 1802*0Sstevel@tonic-gate zonecfg_fini_handle(handle); 1803*0Sstevel@tonic-gate return (Z_ERR); 1804*0Sstevel@tonic-gate } 1805*0Sstevel@tonic-gate if ((err = zonecfg_get_zonepath(handle, zonepath, sizeof (zonepath))) != 1806*0Sstevel@tonic-gate Z_OK) { 1807*0Sstevel@tonic-gate errno = err; 1808*0Sstevel@tonic-gate zperror(cmd_to_str(cmd_num), B_TRUE); 1809*0Sstevel@tonic-gate zonecfg_fini_handle(handle); 1810*0Sstevel@tonic-gate return (Z_ERR); 1811*0Sstevel@tonic-gate } 1812*0Sstevel@tonic-gate /* 1813*0Sstevel@tonic-gate * zonecfg_get_zonepath() gets its data from the XML repository. 1814*0Sstevel@tonic-gate * Verify this against the index file, which is checked first by 1815*0Sstevel@tonic-gate * zone_get_zonepath(). If they don't match, bail out. 1816*0Sstevel@tonic-gate */ 1817*0Sstevel@tonic-gate if ((err = zone_get_zonepath(target_zone, checkpath, 1818*0Sstevel@tonic-gate sizeof (checkpath))) != Z_OK) { 1819*0Sstevel@tonic-gate errno = err; 1820*0Sstevel@tonic-gate zperror2(target_zone, gettext("could not get zone path")); 1821*0Sstevel@tonic-gate return (Z_ERR); 1822*0Sstevel@tonic-gate } 1823*0Sstevel@tonic-gate if (strcmp(zonepath, checkpath) != 0) { 1824*0Sstevel@tonic-gate (void) fprintf(stderr, gettext("The XML repository has " 1825*0Sstevel@tonic-gate "zonepath '%s',\nbut the index file has zonepath '%s'.\n" 1826*0Sstevel@tonic-gate "These must match, so fix the incorrect entry.\n"), 1827*0Sstevel@tonic-gate zonepath, checkpath); 1828*0Sstevel@tonic-gate return (Z_ERR); 1829*0Sstevel@tonic-gate } 1830*0Sstevel@tonic-gate if (validate_zonepath(zonepath, cmd_num) != Z_OK) { 1831*0Sstevel@tonic-gate (void) fprintf(stderr, gettext("could not verify zonepath %s " 1832*0Sstevel@tonic-gate "because of the above errors.\n"), zonepath); 1833*0Sstevel@tonic-gate return_code = Z_ERR; 1834*0Sstevel@tonic-gate } 1835*0Sstevel@tonic-gate 1836*0Sstevel@tonic-gate if ((err = zonecfg_setnwifent(handle)) != Z_OK) { 1837*0Sstevel@tonic-gate errno = err; 1838*0Sstevel@tonic-gate zperror(cmd_to_str(cmd_num), B_TRUE); 1839*0Sstevel@tonic-gate zonecfg_fini_handle(handle); 1840*0Sstevel@tonic-gate return (Z_ERR); 1841*0Sstevel@tonic-gate } 1842*0Sstevel@tonic-gate while (zonecfg_getnwifent(handle, &nwiftab) == Z_OK) { 1843*0Sstevel@tonic-gate struct lifreq lifr; 1844*0Sstevel@tonic-gate sa_family_t af; 1845*0Sstevel@tonic-gate int so, res; 1846*0Sstevel@tonic-gate 1847*0Sstevel@tonic-gate /* skip any loopback interfaces */ 1848*0Sstevel@tonic-gate if (strcmp(nwiftab.zone_nwif_physical, "lo0") == 0) 1849*0Sstevel@tonic-gate continue; 1850*0Sstevel@tonic-gate if ((res = zonecfg_valid_net_address(nwiftab.zone_nwif_address, 1851*0Sstevel@tonic-gate &lifr)) != Z_OK) { 1852*0Sstevel@tonic-gate (void) fprintf(stderr, gettext("could not verify %s " 1853*0Sstevel@tonic-gate "%s=%s %s=%s: %s\n"), "net", "address", 1854*0Sstevel@tonic-gate nwiftab.zone_nwif_address, "physical", 1855*0Sstevel@tonic-gate nwiftab.zone_nwif_physical, zonecfg_strerror(res)); 1856*0Sstevel@tonic-gate return_code = Z_ERR; 1857*0Sstevel@tonic-gate continue; 1858*0Sstevel@tonic-gate } 1859*0Sstevel@tonic-gate af = lifr.lifr_addr.ss_family; 1860*0Sstevel@tonic-gate (void) memset(&lifr, 0, sizeof (lifr)); 1861*0Sstevel@tonic-gate (void) strlcpy(lifr.lifr_name, nwiftab.zone_nwif_physical, 1862*0Sstevel@tonic-gate sizeof (lifr.lifr_name)); 1863*0Sstevel@tonic-gate lifr.lifr_addr.ss_family = af; 1864*0Sstevel@tonic-gate if ((so = socket(af, SOCK_DGRAM, 0)) < 0) { 1865*0Sstevel@tonic-gate (void) fprintf(stderr, gettext("could not verify %s " 1866*0Sstevel@tonic-gate "%s=%s %s=%s: could not get socket: %s\n"), "net", 1867*0Sstevel@tonic-gate "address", nwiftab.zone_nwif_address, "physical", 1868*0Sstevel@tonic-gate nwiftab.zone_nwif_physical, strerror(errno)); 1869*0Sstevel@tonic-gate return_code = Z_ERR; 1870*0Sstevel@tonic-gate continue; 1871*0Sstevel@tonic-gate } 1872*0Sstevel@tonic-gate if (ioctl(so, SIOCGLIFFLAGS, (caddr_t)&lifr) < 0) { 1873*0Sstevel@tonic-gate (void) fprintf(stderr, 1874*0Sstevel@tonic-gate gettext("could not verify %s %s=%s %s=%s: %s\n"), 1875*0Sstevel@tonic-gate "net", "address", nwiftab.zone_nwif_address, 1876*0Sstevel@tonic-gate "physical", nwiftab.zone_nwif_physical, 1877*0Sstevel@tonic-gate strerror(errno)); 1878*0Sstevel@tonic-gate return_code = Z_ERR; 1879*0Sstevel@tonic-gate } 1880*0Sstevel@tonic-gate (void) close(so); 1881*0Sstevel@tonic-gate } 1882*0Sstevel@tonic-gate (void) zonecfg_endnwifent(handle); 1883*0Sstevel@tonic-gate 1884*0Sstevel@tonic-gate if (verify_filesystems(handle) != Z_OK) 1885*0Sstevel@tonic-gate return_code = Z_ERR; 1886*0Sstevel@tonic-gate if (verify_rctls(handle) != Z_OK) 1887*0Sstevel@tonic-gate return_code = Z_ERR; 1888*0Sstevel@tonic-gate if (verify_pool(handle) != Z_OK) 1889*0Sstevel@tonic-gate return_code = Z_ERR; 1890*0Sstevel@tonic-gate zonecfg_fini_handle(handle); 1891*0Sstevel@tonic-gate if (return_code == Z_ERR) 1892*0Sstevel@tonic-gate (void) fprintf(stderr, 1893*0Sstevel@tonic-gate gettext("%s: zone %s failed to verify\n"), 1894*0Sstevel@tonic-gate execname, target_zone); 1895*0Sstevel@tonic-gate return (return_code); 1896*0Sstevel@tonic-gate } 1897*0Sstevel@tonic-gate 1898*0Sstevel@tonic-gate static int 1899*0Sstevel@tonic-gate verify_func(int argc, char *argv[]) 1900*0Sstevel@tonic-gate { 1901*0Sstevel@tonic-gate int arg; 1902*0Sstevel@tonic-gate 1903*0Sstevel@tonic-gate optind = 0; 1904*0Sstevel@tonic-gate if ((arg = getopt(argc, argv, "?")) != EOF) { 1905*0Sstevel@tonic-gate switch (arg) { 1906*0Sstevel@tonic-gate case '?': 1907*0Sstevel@tonic-gate sub_usage(SHELP_VERIFY, CMD_VERIFY); 1908*0Sstevel@tonic-gate return (optopt == '?' ? Z_OK : Z_USAGE); 1909*0Sstevel@tonic-gate default: 1910*0Sstevel@tonic-gate sub_usage(SHELP_VERIFY, CMD_VERIFY); 1911*0Sstevel@tonic-gate return (Z_USAGE); 1912*0Sstevel@tonic-gate } 1913*0Sstevel@tonic-gate } 1914*0Sstevel@tonic-gate if (argc > optind) { 1915*0Sstevel@tonic-gate sub_usage(SHELP_VERIFY, CMD_VERIFY); 1916*0Sstevel@tonic-gate return (Z_USAGE); 1917*0Sstevel@tonic-gate } 1918*0Sstevel@tonic-gate if (sanity_check(target_zone, CMD_VERIFY, B_FALSE, B_FALSE) != Z_OK) 1919*0Sstevel@tonic-gate return (Z_ERR); 1920*0Sstevel@tonic-gate return (verify_details(CMD_VERIFY)); 1921*0Sstevel@tonic-gate } 1922*0Sstevel@tonic-gate 1923*0Sstevel@tonic-gate #define LUCREATEZONE "/usr/lib/lu/lucreatezone" 1924*0Sstevel@tonic-gate 1925*0Sstevel@tonic-gate static int 1926*0Sstevel@tonic-gate install_func(int argc, char *argv[]) 1927*0Sstevel@tonic-gate { 1928*0Sstevel@tonic-gate /* 9: "exec " and " -z " */ 1929*0Sstevel@tonic-gate char cmdbuf[sizeof (LUCREATEZONE) + ZONENAME_MAX + 9]; 1930*0Sstevel@tonic-gate int lockfd; 1931*0Sstevel@tonic-gate int err, arg; 1932*0Sstevel@tonic-gate char zonepath[MAXPATHLEN]; 1933*0Sstevel@tonic-gate int status; 1934*0Sstevel@tonic-gate 1935*0Sstevel@tonic-gate optind = 0; 1936*0Sstevel@tonic-gate if ((arg = getopt(argc, argv, "?")) != EOF) { 1937*0Sstevel@tonic-gate switch (arg) { 1938*0Sstevel@tonic-gate case '?': 1939*0Sstevel@tonic-gate sub_usage(SHELP_INSTALL, CMD_INSTALL); 1940*0Sstevel@tonic-gate return (optopt == '?' ? Z_OK : Z_USAGE); 1941*0Sstevel@tonic-gate default: 1942*0Sstevel@tonic-gate sub_usage(SHELP_INSTALL, CMD_INSTALL); 1943*0Sstevel@tonic-gate return (Z_USAGE); 1944*0Sstevel@tonic-gate } 1945*0Sstevel@tonic-gate } 1946*0Sstevel@tonic-gate if (argc > optind) { 1947*0Sstevel@tonic-gate sub_usage(SHELP_INSTALL, CMD_INSTALL); 1948*0Sstevel@tonic-gate return (Z_USAGE); 1949*0Sstevel@tonic-gate } 1950*0Sstevel@tonic-gate if (sanity_check(target_zone, CMD_INSTALL, B_FALSE, B_TRUE) != Z_OK) 1951*0Sstevel@tonic-gate return (Z_ERR); 1952*0Sstevel@tonic-gate if (verify_details(CMD_INSTALL) != Z_OK) 1953*0Sstevel@tonic-gate return (Z_ERR); 1954*0Sstevel@tonic-gate 1955*0Sstevel@tonic-gate if (grab_lock_file(target_zone, &lockfd) != Z_OK) { 1956*0Sstevel@tonic-gate zerror(gettext("another %s may have an operation in progress."), 1957*0Sstevel@tonic-gate "zoneadmd"); 1958*0Sstevel@tonic-gate return (Z_ERR); 1959*0Sstevel@tonic-gate } 1960*0Sstevel@tonic-gate err = zone_set_state(target_zone, ZONE_STATE_INCOMPLETE); 1961*0Sstevel@tonic-gate if (err != Z_OK) { 1962*0Sstevel@tonic-gate errno = err; 1963*0Sstevel@tonic-gate zperror2(target_zone, gettext("could not set state")); 1964*0Sstevel@tonic-gate goto done; 1965*0Sstevel@tonic-gate } 1966*0Sstevel@tonic-gate 1967*0Sstevel@tonic-gate /* 1968*0Sstevel@tonic-gate * According to the Application Packaging Developer's Guide, a 1969*0Sstevel@tonic-gate * "checkinstall" script when included in a package is executed as 1970*0Sstevel@tonic-gate * the user "install", if such a user exists, or by the user 1971*0Sstevel@tonic-gate * "nobody". In order to support this dubious behavior, the path 1972*0Sstevel@tonic-gate * to the zone being constructed is opened up during the life of 1973*0Sstevel@tonic-gate * the command laying down the zone's root file system. Once this 1974*0Sstevel@tonic-gate * has completed, regardless of whether it was successful, the 1975*0Sstevel@tonic-gate * path to the zone is again restricted. 1976*0Sstevel@tonic-gate */ 1977*0Sstevel@tonic-gate if ((err = zone_get_zonepath(target_zone, zonepath, 1978*0Sstevel@tonic-gate sizeof (zonepath))) != Z_OK) { 1979*0Sstevel@tonic-gate errno = err; 1980*0Sstevel@tonic-gate zperror2(target_zone, gettext("could not get zone path")); 1981*0Sstevel@tonic-gate goto done; 1982*0Sstevel@tonic-gate } 1983*0Sstevel@tonic-gate if (chmod(zonepath, DEFAULT_DIR_MODE) != 0) { 1984*0Sstevel@tonic-gate zperror(zonepath, B_FALSE); 1985*0Sstevel@tonic-gate err = Z_ERR; 1986*0Sstevel@tonic-gate goto done; 1987*0Sstevel@tonic-gate } 1988*0Sstevel@tonic-gate 1989*0Sstevel@tonic-gate /* 1990*0Sstevel@tonic-gate * "exec" the command so that the returned status is that of 1991*0Sstevel@tonic-gate * LUCREATEZONE and not the shell. 1992*0Sstevel@tonic-gate */ 1993*0Sstevel@tonic-gate (void) snprintf(cmdbuf, sizeof (cmdbuf), "exec " LUCREATEZONE " -z %s", 1994*0Sstevel@tonic-gate target_zone); 1995*0Sstevel@tonic-gate status = do_subproc(cmdbuf); 1996*0Sstevel@tonic-gate if (chmod(zonepath, S_IRWXU) != 0) { 1997*0Sstevel@tonic-gate zperror(zonepath, B_FALSE); 1998*0Sstevel@tonic-gate err = Z_ERR; 1999*0Sstevel@tonic-gate goto done; 2000*0Sstevel@tonic-gate } 2001*0Sstevel@tonic-gate if ((err = subproc_status(LUCREATEZONE, status)) != Z_OK) 2002*0Sstevel@tonic-gate goto done; 2003*0Sstevel@tonic-gate 2004*0Sstevel@tonic-gate if ((err = zone_set_state(target_zone, ZONE_STATE_INSTALLED)) != Z_OK) { 2005*0Sstevel@tonic-gate errno = err; 2006*0Sstevel@tonic-gate zperror2(target_zone, gettext("could not set state")); 2007*0Sstevel@tonic-gate goto done; 2008*0Sstevel@tonic-gate } 2009*0Sstevel@tonic-gate 2010*0Sstevel@tonic-gate done: 2011*0Sstevel@tonic-gate release_lock_file(lockfd); 2012*0Sstevel@tonic-gate return ((err == Z_OK) ? Z_OK : Z_ERR); 2013*0Sstevel@tonic-gate } 2014*0Sstevel@tonic-gate 2015*0Sstevel@tonic-gate /* 2016*0Sstevel@tonic-gate * On input, TRUE => yes, FALSE => no. 2017*0Sstevel@tonic-gate * On return, TRUE => 1, FALSE => 0, could not ask => -1. 2018*0Sstevel@tonic-gate */ 2019*0Sstevel@tonic-gate 2020*0Sstevel@tonic-gate static int 2021*0Sstevel@tonic-gate ask_yesno(boolean_t default_answer, const char *question) 2022*0Sstevel@tonic-gate { 2023*0Sstevel@tonic-gate char line[64]; /* should be large enough to answer yes or no */ 2024*0Sstevel@tonic-gate 2025*0Sstevel@tonic-gate if (!isatty(STDIN_FILENO)) 2026*0Sstevel@tonic-gate return (-1); 2027*0Sstevel@tonic-gate for (;;) { 2028*0Sstevel@tonic-gate (void) printf("%s (%s)? ", question, 2029*0Sstevel@tonic-gate default_answer ? "[y]/n" : "y/[n]"); 2030*0Sstevel@tonic-gate if (fgets(line, sizeof (line), stdin) == NULL || 2031*0Sstevel@tonic-gate line[0] == '\n') 2032*0Sstevel@tonic-gate return (default_answer ? 1 : 0); 2033*0Sstevel@tonic-gate if (tolower(line[0]) == 'y') 2034*0Sstevel@tonic-gate return (1); 2035*0Sstevel@tonic-gate if (tolower(line[0]) == 'n') 2036*0Sstevel@tonic-gate return (0); 2037*0Sstevel@tonic-gate } 2038*0Sstevel@tonic-gate } 2039*0Sstevel@tonic-gate 2040*0Sstevel@tonic-gate #define RMCOMMAND "/usr/bin/rm -rf" 2041*0Sstevel@tonic-gate 2042*0Sstevel@tonic-gate /* ARGSUSED */ 2043*0Sstevel@tonic-gate int 2044*0Sstevel@tonic-gate zfm_print(const char *p, void *r) { 2045*0Sstevel@tonic-gate zerror(" %s\n", p); 2046*0Sstevel@tonic-gate return (0); 2047*0Sstevel@tonic-gate } 2048*0Sstevel@tonic-gate 2049*0Sstevel@tonic-gate static int 2050*0Sstevel@tonic-gate uninstall_func(int argc, char *argv[]) 2051*0Sstevel@tonic-gate { 2052*0Sstevel@tonic-gate /* 6: "exec " and " " */ 2053*0Sstevel@tonic-gate char cmdbuf[sizeof (RMCOMMAND) + MAXPATHLEN + 6]; 2054*0Sstevel@tonic-gate char line[ZONENAME_MAX + 128]; /* Enough for "Are you sure ..." */ 2055*0Sstevel@tonic-gate char rootpath[MAXPATHLEN], devpath[MAXPATHLEN]; 2056*0Sstevel@tonic-gate boolean_t force = B_FALSE; 2057*0Sstevel@tonic-gate int lockfd, answer; 2058*0Sstevel@tonic-gate int err, arg; 2059*0Sstevel@tonic-gate int status; 2060*0Sstevel@tonic-gate 2061*0Sstevel@tonic-gate optind = 0; 2062*0Sstevel@tonic-gate while ((arg = getopt(argc, argv, "?F")) != EOF) { 2063*0Sstevel@tonic-gate switch (arg) { 2064*0Sstevel@tonic-gate case '?': 2065*0Sstevel@tonic-gate sub_usage(SHELP_UNINSTALL, CMD_UNINSTALL); 2066*0Sstevel@tonic-gate return (optopt == '?' ? Z_OK : Z_USAGE); 2067*0Sstevel@tonic-gate case 'F': 2068*0Sstevel@tonic-gate force = B_TRUE; 2069*0Sstevel@tonic-gate break; 2070*0Sstevel@tonic-gate default: 2071*0Sstevel@tonic-gate sub_usage(SHELP_UNINSTALL, CMD_UNINSTALL); 2072*0Sstevel@tonic-gate return (Z_USAGE); 2073*0Sstevel@tonic-gate } 2074*0Sstevel@tonic-gate } 2075*0Sstevel@tonic-gate if (argc > optind) { 2076*0Sstevel@tonic-gate sub_usage(SHELP_UNINSTALL, CMD_UNINSTALL); 2077*0Sstevel@tonic-gate return (Z_USAGE); 2078*0Sstevel@tonic-gate } 2079*0Sstevel@tonic-gate 2080*0Sstevel@tonic-gate if (sanity_check(target_zone, CMD_UNINSTALL, B_FALSE, B_TRUE) != Z_OK) 2081*0Sstevel@tonic-gate return (Z_ERR); 2082*0Sstevel@tonic-gate 2083*0Sstevel@tonic-gate if (!force) { 2084*0Sstevel@tonic-gate (void) snprintf(line, sizeof (line), 2085*0Sstevel@tonic-gate gettext("Are you sure you want to %s zone %s"), 2086*0Sstevel@tonic-gate cmd_to_str(CMD_UNINSTALL), target_zone); 2087*0Sstevel@tonic-gate if ((answer = ask_yesno(B_FALSE, line)) == 0) { 2088*0Sstevel@tonic-gate return (Z_OK); 2089*0Sstevel@tonic-gate } else if (answer == -1) { 2090*0Sstevel@tonic-gate zerror(gettext("Input not from terminal and -F " 2091*0Sstevel@tonic-gate "not specified: %s not done."), 2092*0Sstevel@tonic-gate cmd_to_str(CMD_UNINSTALL)); 2093*0Sstevel@tonic-gate return (Z_ERR); 2094*0Sstevel@tonic-gate } 2095*0Sstevel@tonic-gate } 2096*0Sstevel@tonic-gate 2097*0Sstevel@tonic-gate if ((err = zone_get_zonepath(target_zone, devpath, 2098*0Sstevel@tonic-gate sizeof (devpath))) != Z_OK) { 2099*0Sstevel@tonic-gate errno = err; 2100*0Sstevel@tonic-gate zperror2(target_zone, gettext("could not get zone path")); 2101*0Sstevel@tonic-gate return (Z_ERR); 2102*0Sstevel@tonic-gate } 2103*0Sstevel@tonic-gate (void) strlcat(devpath, "/dev", sizeof (devpath)); 2104*0Sstevel@tonic-gate if ((err = zone_get_rootpath(target_zone, rootpath, 2105*0Sstevel@tonic-gate sizeof (rootpath))) != Z_OK) { 2106*0Sstevel@tonic-gate errno = err; 2107*0Sstevel@tonic-gate zperror2(target_zone, gettext("could not get root path")); 2108*0Sstevel@tonic-gate return (Z_ERR); 2109*0Sstevel@tonic-gate } 2110*0Sstevel@tonic-gate 2111*0Sstevel@tonic-gate /* 2112*0Sstevel@tonic-gate * If there seems to be a zoneadmd running for this zone, call it 2113*0Sstevel@tonic-gate * to tell it that an uninstall is happening; if all goes well it 2114*0Sstevel@tonic-gate * will then shut itself down. 2115*0Sstevel@tonic-gate */ 2116*0Sstevel@tonic-gate if (ping_zoneadmd(target_zone) == Z_OK) { 2117*0Sstevel@tonic-gate zone_cmd_arg_t zarg; 2118*0Sstevel@tonic-gate zarg.cmd = Z_NOTE_UNINSTALLING; 2119*0Sstevel@tonic-gate /* we don't care too much if this fails... just plow on */ 2120*0Sstevel@tonic-gate (void) call_zoneadmd(target_zone, &zarg); 2121*0Sstevel@tonic-gate } 2122*0Sstevel@tonic-gate 2123*0Sstevel@tonic-gate if (grab_lock_file(target_zone, &lockfd) != Z_OK) { 2124*0Sstevel@tonic-gate zerror(gettext("another %s may have an operation in progress."), 2125*0Sstevel@tonic-gate "zoneadmd"); 2126*0Sstevel@tonic-gate return (Z_ERR); 2127*0Sstevel@tonic-gate } 2128*0Sstevel@tonic-gate 2129*0Sstevel@tonic-gate /* Don't uninstall the zone if anything is mounted there */ 2130*0Sstevel@tonic-gate err = zonecfg_find_mounts(rootpath, NULL, NULL); 2131*0Sstevel@tonic-gate if (err) { 2132*0Sstevel@tonic-gate zerror(gettext("These file-systems are mounted on " 2133*0Sstevel@tonic-gate "subdirectories of %s.\n"), rootpath); 2134*0Sstevel@tonic-gate (void) zonecfg_find_mounts(rootpath, zfm_print, NULL); 2135*0Sstevel@tonic-gate return (Z_ERR); 2136*0Sstevel@tonic-gate } 2137*0Sstevel@tonic-gate 2138*0Sstevel@tonic-gate err = zone_set_state(target_zone, ZONE_STATE_INCOMPLETE); 2139*0Sstevel@tonic-gate if (err != Z_OK) { 2140*0Sstevel@tonic-gate errno = err; 2141*0Sstevel@tonic-gate zperror2(target_zone, gettext("could not set state")); 2142*0Sstevel@tonic-gate goto bad; 2143*0Sstevel@tonic-gate } 2144*0Sstevel@tonic-gate 2145*0Sstevel@tonic-gate /* 2146*0Sstevel@tonic-gate * "exec" the command so that the returned status is that of 2147*0Sstevel@tonic-gate * RMCOMMAND and not the shell. 2148*0Sstevel@tonic-gate */ 2149*0Sstevel@tonic-gate (void) snprintf(cmdbuf, sizeof (cmdbuf), "exec " RMCOMMAND " %s", 2150*0Sstevel@tonic-gate devpath); 2151*0Sstevel@tonic-gate status = do_subproc(cmdbuf); 2152*0Sstevel@tonic-gate if ((err = subproc_status(RMCOMMAND, status)) != Z_OK) 2153*0Sstevel@tonic-gate goto bad; 2154*0Sstevel@tonic-gate (void) snprintf(cmdbuf, sizeof (cmdbuf), "exec " RMCOMMAND " %s", 2155*0Sstevel@tonic-gate rootpath); 2156*0Sstevel@tonic-gate status = do_subproc(cmdbuf); 2157*0Sstevel@tonic-gate if ((err = subproc_status(RMCOMMAND, status)) != Z_OK) 2158*0Sstevel@tonic-gate goto bad; 2159*0Sstevel@tonic-gate err = zone_set_state(target_zone, ZONE_STATE_CONFIGURED); 2160*0Sstevel@tonic-gate if (err != Z_OK) { 2161*0Sstevel@tonic-gate errno = err; 2162*0Sstevel@tonic-gate zperror2(target_zone, gettext("could not reset state")); 2163*0Sstevel@tonic-gate } 2164*0Sstevel@tonic-gate bad: 2165*0Sstevel@tonic-gate release_lock_file(lockfd); 2166*0Sstevel@tonic-gate return (err); 2167*0Sstevel@tonic-gate } 2168*0Sstevel@tonic-gate 2169*0Sstevel@tonic-gate static int 2170*0Sstevel@tonic-gate help_func(int argc, char *argv[]) 2171*0Sstevel@tonic-gate { 2172*0Sstevel@tonic-gate int arg, cmd_num; 2173*0Sstevel@tonic-gate 2174*0Sstevel@tonic-gate if (argc == 0) { 2175*0Sstevel@tonic-gate (void) usage(B_TRUE); 2176*0Sstevel@tonic-gate return (Z_OK); 2177*0Sstevel@tonic-gate } 2178*0Sstevel@tonic-gate optind = 0; 2179*0Sstevel@tonic-gate if ((arg = getopt(argc, argv, "?")) != EOF) { 2180*0Sstevel@tonic-gate switch (arg) { 2181*0Sstevel@tonic-gate case '?': 2182*0Sstevel@tonic-gate sub_usage(SHELP_HELP, CMD_HELP); 2183*0Sstevel@tonic-gate return (optopt == '?' ? Z_OK : Z_USAGE); 2184*0Sstevel@tonic-gate default: 2185*0Sstevel@tonic-gate sub_usage(SHELP_HELP, CMD_HELP); 2186*0Sstevel@tonic-gate return (Z_USAGE); 2187*0Sstevel@tonic-gate } 2188*0Sstevel@tonic-gate } 2189*0Sstevel@tonic-gate while (optind < argc) { 2190*0Sstevel@tonic-gate if ((cmd_num = cmd_match(argv[optind])) < 0) { 2191*0Sstevel@tonic-gate sub_usage(SHELP_HELP, CMD_HELP); 2192*0Sstevel@tonic-gate return (Z_USAGE); 2193*0Sstevel@tonic-gate } 2194*0Sstevel@tonic-gate sub_usage(cmdtab[cmd_num].short_usage, cmd_num); 2195*0Sstevel@tonic-gate optind++; 2196*0Sstevel@tonic-gate } 2197*0Sstevel@tonic-gate return (Z_OK); 2198*0Sstevel@tonic-gate } 2199*0Sstevel@tonic-gate 2200*0Sstevel@tonic-gate /* 2201*0Sstevel@tonic-gate * Returns: CMD_MIN thru CMD_MAX on success, -1 on error 2202*0Sstevel@tonic-gate */ 2203*0Sstevel@tonic-gate 2204*0Sstevel@tonic-gate static int 2205*0Sstevel@tonic-gate cmd_match(char *cmd) 2206*0Sstevel@tonic-gate { 2207*0Sstevel@tonic-gate int i; 2208*0Sstevel@tonic-gate 2209*0Sstevel@tonic-gate for (i = CMD_MIN; i <= CMD_MAX; i++) { 2210*0Sstevel@tonic-gate /* return only if there is an exact match */ 2211*0Sstevel@tonic-gate if (strcmp(cmd, cmdtab[i].cmd_name) == 0) 2212*0Sstevel@tonic-gate return (cmdtab[i].cmd_num); 2213*0Sstevel@tonic-gate } 2214*0Sstevel@tonic-gate return (-1); 2215*0Sstevel@tonic-gate } 2216*0Sstevel@tonic-gate 2217*0Sstevel@tonic-gate static int 2218*0Sstevel@tonic-gate parse_and_run(int argc, char *argv[]) 2219*0Sstevel@tonic-gate { 2220*0Sstevel@tonic-gate int i = cmd_match(argv[0]); 2221*0Sstevel@tonic-gate 2222*0Sstevel@tonic-gate if (i < 0) 2223*0Sstevel@tonic-gate return (usage(B_FALSE)); 2224*0Sstevel@tonic-gate return (cmdtab[i].handler(argc - 1, &(argv[1]))); 2225*0Sstevel@tonic-gate } 2226*0Sstevel@tonic-gate 2227*0Sstevel@tonic-gate static char * 2228*0Sstevel@tonic-gate get_execbasename(char *execfullname) 2229*0Sstevel@tonic-gate { 2230*0Sstevel@tonic-gate char *last_slash, *execbasename; 2231*0Sstevel@tonic-gate 2232*0Sstevel@tonic-gate /* guard against '/' at end of command invocation */ 2233*0Sstevel@tonic-gate for (;;) { 2234*0Sstevel@tonic-gate last_slash = strrchr(execfullname, '/'); 2235*0Sstevel@tonic-gate if (last_slash == NULL) { 2236*0Sstevel@tonic-gate execbasename = execfullname; 2237*0Sstevel@tonic-gate break; 2238*0Sstevel@tonic-gate } else { 2239*0Sstevel@tonic-gate execbasename = last_slash + 1; 2240*0Sstevel@tonic-gate if (*execbasename == '\0') { 2241*0Sstevel@tonic-gate *last_slash = '\0'; 2242*0Sstevel@tonic-gate continue; 2243*0Sstevel@tonic-gate } 2244*0Sstevel@tonic-gate break; 2245*0Sstevel@tonic-gate } 2246*0Sstevel@tonic-gate } 2247*0Sstevel@tonic-gate return (execbasename); 2248*0Sstevel@tonic-gate } 2249*0Sstevel@tonic-gate 2250*0Sstevel@tonic-gate int 2251*0Sstevel@tonic-gate main(int argc, char **argv) 2252*0Sstevel@tonic-gate { 2253*0Sstevel@tonic-gate int arg; 2254*0Sstevel@tonic-gate zoneid_t zid; 2255*0Sstevel@tonic-gate 2256*0Sstevel@tonic-gate if ((locale = setlocale(LC_ALL, "")) == NULL) 2257*0Sstevel@tonic-gate locale = "C"; 2258*0Sstevel@tonic-gate (void) textdomain(TEXT_DOMAIN); 2259*0Sstevel@tonic-gate setbuf(stdout, NULL); 2260*0Sstevel@tonic-gate (void) sigset(SIGHUP, SIG_IGN); 2261*0Sstevel@tonic-gate execname = get_execbasename(argv[0]); 2262*0Sstevel@tonic-gate target_zone = NULL; 2263*0Sstevel@tonic-gate if (chdir("/") != 0) { 2264*0Sstevel@tonic-gate zerror(gettext("could not change directory to /.")); 2265*0Sstevel@tonic-gate exit(Z_ERR); 2266*0Sstevel@tonic-gate } 2267*0Sstevel@tonic-gate 2268*0Sstevel@tonic-gate while ((arg = getopt(argc, argv, "?z:")) != EOF) { 2269*0Sstevel@tonic-gate switch (arg) { 2270*0Sstevel@tonic-gate case '?': 2271*0Sstevel@tonic-gate return (usage(B_TRUE)); 2272*0Sstevel@tonic-gate case 'z': 2273*0Sstevel@tonic-gate target_zone = optarg; 2274*0Sstevel@tonic-gate break; 2275*0Sstevel@tonic-gate default: 2276*0Sstevel@tonic-gate return (usage(B_FALSE)); 2277*0Sstevel@tonic-gate } 2278*0Sstevel@tonic-gate } 2279*0Sstevel@tonic-gate 2280*0Sstevel@tonic-gate if (optind >= argc) 2281*0Sstevel@tonic-gate return (usage(B_FALSE)); 2282*0Sstevel@tonic-gate if (target_zone != NULL && zone_get_id(target_zone, &zid) != 0) { 2283*0Sstevel@tonic-gate errno = Z_NO_ZONE; 2284*0Sstevel@tonic-gate zperror(target_zone, B_TRUE); 2285*0Sstevel@tonic-gate exit(Z_ERR); 2286*0Sstevel@tonic-gate } 2287*0Sstevel@tonic-gate return (parse_and_run(argc - optind, &argv[optind])); 2288*0Sstevel@tonic-gate } 2289