10Sstevel@tonic-gate /* 20Sstevel@tonic-gate * CDDL HEADER START 30Sstevel@tonic-gate * 40Sstevel@tonic-gate * The contents of this file are subject to the terms of the 50Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 60Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 70Sstevel@tonic-gate * with the License. 80Sstevel@tonic-gate * 90Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 100Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 110Sstevel@tonic-gate * See the License for the specific language governing permissions 120Sstevel@tonic-gate * and limitations under the License. 130Sstevel@tonic-gate * 140Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 150Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 160Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 170Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 180Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 190Sstevel@tonic-gate * 200Sstevel@tonic-gate * CDDL HEADER END 210Sstevel@tonic-gate */ 22222Scomay 230Sstevel@tonic-gate /* 240Sstevel@tonic-gate * Copyright 2005 Sun Microsystems, Inc. All rights reserved. 250Sstevel@tonic-gate * Use is subject to license terms. 260Sstevel@tonic-gate */ 270Sstevel@tonic-gate 280Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 290Sstevel@tonic-gate 300Sstevel@tonic-gate /* 310Sstevel@tonic-gate * zoneadm is a command interpreter for zone administration. It is all in 320Sstevel@tonic-gate * C (i.e., no lex/yacc), and all the argument passing is argc/argv based. 330Sstevel@tonic-gate * main() calls parse_and_run() which calls cmd_match(), then invokes the 340Sstevel@tonic-gate * appropriate command's handler function. The rest of the program is the 350Sstevel@tonic-gate * handler functions and their helper functions. 360Sstevel@tonic-gate * 370Sstevel@tonic-gate * Some of the helper functions are used largely to simplify I18N: reducing 380Sstevel@tonic-gate * the need for translation notes. This is particularly true of many of 390Sstevel@tonic-gate * the zerror() calls: doing e.g. zerror(gettext("%s failed"), "foo") rather 400Sstevel@tonic-gate * than zerror(gettext("foo failed")) with a translation note indicating 410Sstevel@tonic-gate * that "foo" need not be translated. 420Sstevel@tonic-gate */ 430Sstevel@tonic-gate 440Sstevel@tonic-gate #include <stdio.h> 450Sstevel@tonic-gate #include <errno.h> 460Sstevel@tonic-gate #include <unistd.h> 470Sstevel@tonic-gate #include <signal.h> 480Sstevel@tonic-gate #include <stdarg.h> 490Sstevel@tonic-gate #include <ctype.h> 500Sstevel@tonic-gate #include <stdlib.h> 510Sstevel@tonic-gate #include <string.h> 520Sstevel@tonic-gate #include <wait.h> 530Sstevel@tonic-gate #include <zone.h> 540Sstevel@tonic-gate #include <priv.h> 550Sstevel@tonic-gate #include <locale.h> 560Sstevel@tonic-gate #include <libintl.h> 570Sstevel@tonic-gate #include <libzonecfg.h> 580Sstevel@tonic-gate #include <bsm/adt.h> 590Sstevel@tonic-gate #include <sys/utsname.h> 600Sstevel@tonic-gate #include <sys/param.h> 610Sstevel@tonic-gate #include <sys/types.h> 620Sstevel@tonic-gate #include <sys/stat.h> 630Sstevel@tonic-gate #include <sys/statvfs.h> 640Sstevel@tonic-gate #include <assert.h> 650Sstevel@tonic-gate #include <sys/sockio.h> 660Sstevel@tonic-gate #include <sys/mntent.h> 670Sstevel@tonic-gate #include <limits.h> 68*789Sahrens #include <libzfs.h> 690Sstevel@tonic-gate 700Sstevel@tonic-gate #include <fcntl.h> 710Sstevel@tonic-gate #include <door.h> 720Sstevel@tonic-gate #include <macros.h> 730Sstevel@tonic-gate #include <libgen.h> 740Sstevel@tonic-gate 750Sstevel@tonic-gate #include <pool.h> 760Sstevel@tonic-gate #include <sys/pool.h> 770Sstevel@tonic-gate 780Sstevel@tonic-gate #define MAXARGS 8 790Sstevel@tonic-gate 800Sstevel@tonic-gate /* Reflects kernel zone entries */ 810Sstevel@tonic-gate typedef struct zone_entry { 820Sstevel@tonic-gate zoneid_t zid; 830Sstevel@tonic-gate char zname[ZONENAME_MAX]; 840Sstevel@tonic-gate char *zstate_str; 850Sstevel@tonic-gate zone_state_t zstate_num; 860Sstevel@tonic-gate char zroot[MAXPATHLEN]; 870Sstevel@tonic-gate } zone_entry_t; 880Sstevel@tonic-gate 890Sstevel@tonic-gate static zone_entry_t *zents; 900Sstevel@tonic-gate static size_t nzents; 910Sstevel@tonic-gate 920Sstevel@tonic-gate #if !defined(TEXT_DOMAIN) /* should be defined by cc -D */ 930Sstevel@tonic-gate #define TEXT_DOMAIN "SYS_TEST" /* Use this only if it wasn't */ 940Sstevel@tonic-gate #endif 950Sstevel@tonic-gate 960Sstevel@tonic-gate #define Z_ERR 1 970Sstevel@tonic-gate #define Z_USAGE 2 980Sstevel@tonic-gate 990Sstevel@tonic-gate /* 0755 is the default directory mode. */ 1000Sstevel@tonic-gate #define DEFAULT_DIR_MODE \ 1010Sstevel@tonic-gate (S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) 1020Sstevel@tonic-gate 1030Sstevel@tonic-gate #define CMD_HELP 0 1040Sstevel@tonic-gate #define CMD_BOOT 1 1050Sstevel@tonic-gate #define CMD_HALT 2 1060Sstevel@tonic-gate #define CMD_READY 3 1070Sstevel@tonic-gate #define CMD_REBOOT 4 1080Sstevel@tonic-gate #define CMD_LIST 5 1090Sstevel@tonic-gate #define CMD_VERIFY 6 1100Sstevel@tonic-gate #define CMD_INSTALL 7 1110Sstevel@tonic-gate #define CMD_UNINSTALL 8 112766Scarlsonj #define CMD_MOUNT 9 113766Scarlsonj #define CMD_UNMOUNT 10 1140Sstevel@tonic-gate 1150Sstevel@tonic-gate #define CMD_MIN CMD_HELP 116766Scarlsonj #define CMD_MAX CMD_UNMOUNT 1170Sstevel@tonic-gate 1180Sstevel@tonic-gate struct cmd { 1190Sstevel@tonic-gate uint_t cmd_num; /* command number */ 1200Sstevel@tonic-gate char *cmd_name; /* command name */ 1210Sstevel@tonic-gate char *short_usage; /* short form help */ 1220Sstevel@tonic-gate int (*handler)(int argc, char *argv[]); /* function to call */ 1230Sstevel@tonic-gate 1240Sstevel@tonic-gate }; 1250Sstevel@tonic-gate 1260Sstevel@tonic-gate #define SHELP_HELP "help" 1270Sstevel@tonic-gate #define SHELP_BOOT "boot [-s]" 1280Sstevel@tonic-gate #define SHELP_HALT "halt" 1290Sstevel@tonic-gate #define SHELP_READY "ready" 1300Sstevel@tonic-gate #define SHELP_REBOOT "reboot" 1310Sstevel@tonic-gate #define SHELP_LIST "list [-cipv]" 1320Sstevel@tonic-gate #define SHELP_VERIFY "verify" 1330Sstevel@tonic-gate #define SHELP_INSTALL "install" 1340Sstevel@tonic-gate #define SHELP_UNINSTALL "uninstall [-F]" 1350Sstevel@tonic-gate 1360Sstevel@tonic-gate static int help_func(int argc, char *argv[]); 1370Sstevel@tonic-gate static int ready_func(int argc, char *argv[]); 1380Sstevel@tonic-gate static int boot_func(int argc, char *argv[]); 1390Sstevel@tonic-gate static int halt_func(int argc, char *argv[]); 1400Sstevel@tonic-gate static int reboot_func(int argc, char *argv[]); 1410Sstevel@tonic-gate static int list_func(int argc, char *argv[]); 1420Sstevel@tonic-gate static int verify_func(int argc, char *argv[]); 1430Sstevel@tonic-gate static int install_func(int argc, char *argv[]); 1440Sstevel@tonic-gate static int uninstall_func(int argc, char *argv[]); 145766Scarlsonj static int mount_func(int argc, char *argv[]); 146766Scarlsonj static int unmount_func(int argc, char *argv[]); 1470Sstevel@tonic-gate static int sanity_check(char *zone, int cmd_num, boolean_t running, 1480Sstevel@tonic-gate boolean_t unsafe_when_running); 1490Sstevel@tonic-gate static int cmd_match(char *cmd); 1500Sstevel@tonic-gate static int verify_details(int); 1510Sstevel@tonic-gate 1520Sstevel@tonic-gate static struct cmd cmdtab[] = { 1530Sstevel@tonic-gate { CMD_HELP, "help", SHELP_HELP, help_func }, 1540Sstevel@tonic-gate { CMD_BOOT, "boot", SHELP_BOOT, boot_func }, 1550Sstevel@tonic-gate { CMD_HALT, "halt", SHELP_HALT, halt_func }, 1560Sstevel@tonic-gate { CMD_READY, "ready", SHELP_READY, ready_func }, 1570Sstevel@tonic-gate { CMD_REBOOT, "reboot", SHELP_REBOOT, reboot_func }, 1580Sstevel@tonic-gate { CMD_LIST, "list", SHELP_LIST, list_func }, 1590Sstevel@tonic-gate { CMD_VERIFY, "verify", SHELP_VERIFY, verify_func }, 1600Sstevel@tonic-gate { CMD_INSTALL, "install", SHELP_INSTALL, install_func }, 1610Sstevel@tonic-gate { CMD_UNINSTALL, "uninstall", SHELP_UNINSTALL, 162766Scarlsonj uninstall_func }, 163766Scarlsonj /* Private commands for admin/install */ 164766Scarlsonj { CMD_MOUNT, "mount", NULL, mount_func }, 165766Scarlsonj { CMD_UNMOUNT, "unmount", NULL, unmount_func } 1660Sstevel@tonic-gate }; 1670Sstevel@tonic-gate 1680Sstevel@tonic-gate /* global variables */ 1690Sstevel@tonic-gate 1700Sstevel@tonic-gate /* set early in main(), never modified thereafter, used all over the place */ 1710Sstevel@tonic-gate static char *execname; 1720Sstevel@tonic-gate static char *target_zone; 1730Sstevel@tonic-gate static char *locale; 1740Sstevel@tonic-gate 1750Sstevel@tonic-gate /* used in do_subproc() and signal handler */ 1760Sstevel@tonic-gate static volatile boolean_t child_killed; 1770Sstevel@tonic-gate 1780Sstevel@tonic-gate static char * 1790Sstevel@tonic-gate cmd_to_str(int cmd_num) 1800Sstevel@tonic-gate { 1810Sstevel@tonic-gate assert(cmd_num >= CMD_MIN && cmd_num <= CMD_MAX); 1820Sstevel@tonic-gate return (cmdtab[cmd_num].cmd_name); 1830Sstevel@tonic-gate } 1840Sstevel@tonic-gate 1850Sstevel@tonic-gate /* This is a separate function because of gettext() wrapping. */ 1860Sstevel@tonic-gate static char * 1870Sstevel@tonic-gate long_help(int cmd_num) 1880Sstevel@tonic-gate { 189222Scomay assert(cmd_num >= CMD_MIN && cmd_num <= CMD_MAX); 1900Sstevel@tonic-gate switch (cmd_num) { 1910Sstevel@tonic-gate case CMD_HELP: 1920Sstevel@tonic-gate return (gettext("Print usage message.")); 1930Sstevel@tonic-gate case CMD_BOOT: 1940Sstevel@tonic-gate return (gettext("Activates (boots) specified zone. " 1950Sstevel@tonic-gate "The -s flag can be used\n\tto boot the zone in " 1960Sstevel@tonic-gate "the single-user state.")); 1970Sstevel@tonic-gate case CMD_HALT: 1980Sstevel@tonic-gate return (gettext("Halts specified zone, bypassing " 1990Sstevel@tonic-gate "shutdown scripts and removing runtime\n\t" 2000Sstevel@tonic-gate "resources of the zone.")); 2010Sstevel@tonic-gate case CMD_READY: 2020Sstevel@tonic-gate return (gettext("Prepares a zone for running " 2030Sstevel@tonic-gate "applications but does not start any user\n\t" 2040Sstevel@tonic-gate "processes in the zone.")); 2050Sstevel@tonic-gate case CMD_REBOOT: 2060Sstevel@tonic-gate return (gettext("Restarts the zone (equivalent to a " 2070Sstevel@tonic-gate "halt / boot sequence).\n\tFails if the zone is " 2080Sstevel@tonic-gate "not active.")); 2090Sstevel@tonic-gate case CMD_LIST: 2100Sstevel@tonic-gate return (gettext("Lists the current zones, or a " 2110Sstevel@tonic-gate "specific zone if indicated. By default,\n\tall " 2120Sstevel@tonic-gate "running zones are listed, though this can be " 2130Sstevel@tonic-gate "expanded to all\n\tinstalled zones with the -i " 2140Sstevel@tonic-gate "option or all configured zones with the\n\t-c " 2150Sstevel@tonic-gate "option. When used with the general -z <zone> " 2160Sstevel@tonic-gate "option, lists only the\n\tspecified zone, but " 2170Sstevel@tonic-gate "lists it regardless of its state, and the -i " 2180Sstevel@tonic-gate "and -c\n\toptions are disallowed. The -v option " 2190Sstevel@tonic-gate "can be used to display verbose\n\tinformation: " 2200Sstevel@tonic-gate "zone name, id, current state, root directory and " 2210Sstevel@tonic-gate "options.\n\tThe -p option can be used to request " 2220Sstevel@tonic-gate "machine-parsable output. The -v\n\tand -p " 2230Sstevel@tonic-gate "options are mutually exclusive. If neither -v " 2240Sstevel@tonic-gate "nor -p is used,\n\tjust the zone name is " 2250Sstevel@tonic-gate "listed.")); 2260Sstevel@tonic-gate case CMD_VERIFY: 2270Sstevel@tonic-gate return (gettext("Check to make sure the configuration " 2280Sstevel@tonic-gate "can safely be instantiated\n\ton the machine: " 2290Sstevel@tonic-gate "physical network interfaces exist, etc.")); 2300Sstevel@tonic-gate case CMD_INSTALL: 2310Sstevel@tonic-gate return (gettext("Install the configuration on to the " 2320Sstevel@tonic-gate "system.")); 2330Sstevel@tonic-gate case CMD_UNINSTALL: 2340Sstevel@tonic-gate return (gettext("Uninstall the configuration from the " 2350Sstevel@tonic-gate "system. The -F flag can be used\n\tto force the " 2360Sstevel@tonic-gate "action.")); 237766Scarlsonj default: 238766Scarlsonj return (""); 2390Sstevel@tonic-gate } 2400Sstevel@tonic-gate /* NOTREACHED */ 241222Scomay return (NULL); 2420Sstevel@tonic-gate } 2430Sstevel@tonic-gate 2440Sstevel@tonic-gate /* 2450Sstevel@tonic-gate * Called with explicit B_TRUE when help is explicitly requested, B_FALSE for 2460Sstevel@tonic-gate * unexpected errors. 2470Sstevel@tonic-gate */ 2480Sstevel@tonic-gate 2490Sstevel@tonic-gate static int 2500Sstevel@tonic-gate usage(boolean_t explicit) 2510Sstevel@tonic-gate { 2520Sstevel@tonic-gate int i; 2530Sstevel@tonic-gate FILE *fd = explicit ? stdout : stderr; 2540Sstevel@tonic-gate 2550Sstevel@tonic-gate (void) fprintf(fd, "%s:\t%s help\n", gettext("usage"), execname); 2560Sstevel@tonic-gate (void) fprintf(fd, "\t%s [-z <zone>] list\n", execname); 2570Sstevel@tonic-gate (void) fprintf(fd, "\t%s -z <zone> <%s>\n", execname, 2580Sstevel@tonic-gate gettext("subcommand")); 2590Sstevel@tonic-gate (void) fprintf(fd, "\n%s:\n\n", gettext("Subcommands")); 2600Sstevel@tonic-gate for (i = CMD_MIN; i <= CMD_MAX; i++) { 261766Scarlsonj if (cmdtab[i].short_usage == NULL) 262766Scarlsonj continue; 2630Sstevel@tonic-gate (void) fprintf(fd, "%s\n", cmdtab[i].short_usage); 2640Sstevel@tonic-gate if (explicit) 2650Sstevel@tonic-gate (void) fprintf(fd, "\t%s\n\n", long_help(i)); 2660Sstevel@tonic-gate } 2670Sstevel@tonic-gate if (!explicit) 2680Sstevel@tonic-gate (void) fputs("\n", fd); 2690Sstevel@tonic-gate return (Z_USAGE); 2700Sstevel@tonic-gate } 2710Sstevel@tonic-gate 2720Sstevel@tonic-gate static void 2730Sstevel@tonic-gate sub_usage(char *short_usage, int cmd_num) 2740Sstevel@tonic-gate { 2750Sstevel@tonic-gate (void) fprintf(stderr, "%s:\t%s\n", gettext("usage"), short_usage); 2760Sstevel@tonic-gate (void) fprintf(stderr, "\t%s\n", long_help(cmd_num)); 2770Sstevel@tonic-gate } 2780Sstevel@tonic-gate 2790Sstevel@tonic-gate /* 2800Sstevel@tonic-gate * zperror() is like perror(3c) except that this also prints the executable 2810Sstevel@tonic-gate * name at the start of the message, and takes a boolean indicating whether 2820Sstevel@tonic-gate * to call libc'c strerror() or that from libzonecfg. 2830Sstevel@tonic-gate */ 2840Sstevel@tonic-gate 2850Sstevel@tonic-gate static void 2860Sstevel@tonic-gate zperror(const char *str, boolean_t zonecfg_error) 2870Sstevel@tonic-gate { 2880Sstevel@tonic-gate (void) fprintf(stderr, "%s: %s: %s\n", execname, str, 2890Sstevel@tonic-gate zonecfg_error ? zonecfg_strerror(errno) : strerror(errno)); 2900Sstevel@tonic-gate } 2910Sstevel@tonic-gate 2920Sstevel@tonic-gate /* 2930Sstevel@tonic-gate * zperror2() is very similar to zperror() above, except it also prints a 2940Sstevel@tonic-gate * supplied zone name after the executable. 2950Sstevel@tonic-gate * 2960Sstevel@tonic-gate * All current consumers of this function want libzonecfg's strerror() rather 2970Sstevel@tonic-gate * than libc's; if this ever changes, this function can be made more generic 2980Sstevel@tonic-gate * like zperror() above. 2990Sstevel@tonic-gate */ 3000Sstevel@tonic-gate 3010Sstevel@tonic-gate static void 3020Sstevel@tonic-gate zperror2(const char *zone, const char *str) 3030Sstevel@tonic-gate { 3040Sstevel@tonic-gate (void) fprintf(stderr, "%s: %s: %s: %s\n", execname, zone, str, 3050Sstevel@tonic-gate zonecfg_strerror(errno)); 3060Sstevel@tonic-gate } 3070Sstevel@tonic-gate 3080Sstevel@tonic-gate /* PRINTFLIKE1 */ 3090Sstevel@tonic-gate static void 3100Sstevel@tonic-gate zerror(const char *fmt, ...) 3110Sstevel@tonic-gate { 3120Sstevel@tonic-gate va_list alist; 3130Sstevel@tonic-gate 3140Sstevel@tonic-gate va_start(alist, fmt); 3150Sstevel@tonic-gate (void) fprintf(stderr, "%s: ", execname); 3160Sstevel@tonic-gate if (target_zone != NULL) 3170Sstevel@tonic-gate (void) fprintf(stderr, "zone '%s': ", target_zone); 3180Sstevel@tonic-gate (void) vfprintf(stderr, fmt, alist); 3190Sstevel@tonic-gate (void) fprintf(stderr, "\n"); 3200Sstevel@tonic-gate va_end(alist); 3210Sstevel@tonic-gate } 3220Sstevel@tonic-gate 3230Sstevel@tonic-gate static void * 3240Sstevel@tonic-gate safe_calloc(size_t nelem, size_t elsize) 3250Sstevel@tonic-gate { 3260Sstevel@tonic-gate void *r = calloc(nelem, elsize); 3270Sstevel@tonic-gate 3280Sstevel@tonic-gate if (r == NULL) { 3290Sstevel@tonic-gate zerror(gettext("failed to allocate %lu bytes: %s"), 3300Sstevel@tonic-gate (ulong_t)nelem * elsize, strerror(errno)); 3310Sstevel@tonic-gate exit(Z_ERR); 3320Sstevel@tonic-gate } 3330Sstevel@tonic-gate return (r); 3340Sstevel@tonic-gate } 3350Sstevel@tonic-gate 3360Sstevel@tonic-gate static void 3370Sstevel@tonic-gate zone_print(zone_entry_t *zent, boolean_t verbose, boolean_t parsable) 3380Sstevel@tonic-gate { 3390Sstevel@tonic-gate static boolean_t firsttime = B_TRUE; 3400Sstevel@tonic-gate 3410Sstevel@tonic-gate assert(!(verbose && parsable)); 3420Sstevel@tonic-gate if (firsttime && verbose) { 3430Sstevel@tonic-gate firsttime = B_FALSE; 3440Sstevel@tonic-gate (void) printf("%*s %-16s %-14s %-30s\n", ZONEID_WIDTH, "ID", 3450Sstevel@tonic-gate "NAME", "STATUS", "PATH"); 3460Sstevel@tonic-gate } 3470Sstevel@tonic-gate if (!verbose) { 3480Sstevel@tonic-gate if (!parsable) { 3490Sstevel@tonic-gate (void) printf("%s\n", zent->zname); 3500Sstevel@tonic-gate return; 3510Sstevel@tonic-gate } 3520Sstevel@tonic-gate if (zent->zid == ZONE_ID_UNDEFINED) 3530Sstevel@tonic-gate (void) printf("-"); 3540Sstevel@tonic-gate else 3550Sstevel@tonic-gate (void) printf("%lu", zent->zid); 3560Sstevel@tonic-gate (void) printf(":%s:%s:%s\n", zent->zname, zent->zstate_str, 3570Sstevel@tonic-gate zent->zroot); 3580Sstevel@tonic-gate return; 3590Sstevel@tonic-gate } 3600Sstevel@tonic-gate if (zent->zstate_str != NULL) { 3610Sstevel@tonic-gate if (zent->zid == ZONE_ID_UNDEFINED) 3620Sstevel@tonic-gate (void) printf("%*s", ZONEID_WIDTH, "-"); 3630Sstevel@tonic-gate else 3640Sstevel@tonic-gate (void) printf("%*lu", ZONEID_WIDTH, zent->zid); 3650Sstevel@tonic-gate (void) printf(" %-16s %-14s %-30s\n", zent->zname, 3660Sstevel@tonic-gate zent->zstate_str, zent->zroot); 3670Sstevel@tonic-gate } 3680Sstevel@tonic-gate } 3690Sstevel@tonic-gate 3700Sstevel@tonic-gate static int 371766Scarlsonj lookup_zone_info(const char *zone_name, zoneid_t zid, zone_entry_t *zent) 3720Sstevel@tonic-gate { 3730Sstevel@tonic-gate char root[MAXPATHLEN]; 3740Sstevel@tonic-gate int err; 3750Sstevel@tonic-gate 3760Sstevel@tonic-gate (void) strlcpy(zent->zname, zone_name, sizeof (zent->zname)); 3770Sstevel@tonic-gate (void) strlcpy(zent->zroot, "???", sizeof (zent->zroot)); 3780Sstevel@tonic-gate zent->zstate_str = "???"; 3790Sstevel@tonic-gate 380766Scarlsonj zent->zid = zid; 3810Sstevel@tonic-gate 3820Sstevel@tonic-gate if ((err = zone_get_zonepath(zent->zname, root, sizeof (root))) != 3830Sstevel@tonic-gate Z_OK) { 3840Sstevel@tonic-gate errno = err; 3850Sstevel@tonic-gate zperror2(zent->zname, gettext("could not get zone path")); 3860Sstevel@tonic-gate return (Z_ERR); 3870Sstevel@tonic-gate } 3880Sstevel@tonic-gate (void) strlcpy(zent->zroot, root, sizeof (zent->zroot)); 3890Sstevel@tonic-gate 3900Sstevel@tonic-gate if ((err = zone_get_state(zent->zname, &zent->zstate_num)) != Z_OK) { 3910Sstevel@tonic-gate errno = err; 3920Sstevel@tonic-gate zperror2(zent->zname, gettext("could not get state")); 3930Sstevel@tonic-gate return (Z_ERR); 3940Sstevel@tonic-gate } 3950Sstevel@tonic-gate zent->zstate_str = zone_state_str(zent->zstate_num); 3960Sstevel@tonic-gate 3970Sstevel@tonic-gate return (Z_OK); 3980Sstevel@tonic-gate } 3990Sstevel@tonic-gate 4000Sstevel@tonic-gate /* 4010Sstevel@tonic-gate * fetch_zents() calls zone_list(2) to find out how many zones are running 4020Sstevel@tonic-gate * (which is stored in the global nzents), then calls zone_list(2) again 4030Sstevel@tonic-gate * to fetch the list of running zones (stored in the global zents). This 4040Sstevel@tonic-gate * function may be called multiple times, so if zents is already set, we 4050Sstevel@tonic-gate * return immediately to save work. 4060Sstevel@tonic-gate */ 4070Sstevel@tonic-gate 4080Sstevel@tonic-gate static int 409766Scarlsonj fetch_zents(void) 4100Sstevel@tonic-gate { 4110Sstevel@tonic-gate zoneid_t *zids = NULL; 4120Sstevel@tonic-gate uint_t nzents_saved; 413766Scarlsonj int i, retv; 414766Scarlsonj FILE *fp; 415766Scarlsonj boolean_t inaltroot; 416766Scarlsonj zone_entry_t *zentp; 4170Sstevel@tonic-gate 4180Sstevel@tonic-gate if (nzents > 0) 4190Sstevel@tonic-gate return (Z_OK); 4200Sstevel@tonic-gate 4210Sstevel@tonic-gate if (zone_list(NULL, &nzents) != 0) { 4220Sstevel@tonic-gate zperror(gettext("failed to get zoneid list"), B_FALSE); 4230Sstevel@tonic-gate return (Z_ERR); 4240Sstevel@tonic-gate } 4250Sstevel@tonic-gate 4260Sstevel@tonic-gate again: 4270Sstevel@tonic-gate if (nzents == 0) 4280Sstevel@tonic-gate return (Z_OK); 4290Sstevel@tonic-gate 4300Sstevel@tonic-gate zids = safe_calloc(nzents, sizeof (zoneid_t)); 4310Sstevel@tonic-gate nzents_saved = nzents; 4320Sstevel@tonic-gate 4330Sstevel@tonic-gate if (zone_list(zids, &nzents) != 0) { 4340Sstevel@tonic-gate zperror(gettext("failed to get zone list"), B_FALSE); 4350Sstevel@tonic-gate free(zids); 4360Sstevel@tonic-gate return (Z_ERR); 4370Sstevel@tonic-gate } 4380Sstevel@tonic-gate if (nzents != nzents_saved) { 4390Sstevel@tonic-gate /* list changed, try again */ 4400Sstevel@tonic-gate free(zids); 4410Sstevel@tonic-gate goto again; 4420Sstevel@tonic-gate } 4430Sstevel@tonic-gate 4440Sstevel@tonic-gate zents = safe_calloc(nzents, sizeof (zone_entry_t)); 4450Sstevel@tonic-gate 446766Scarlsonj inaltroot = zonecfg_in_alt_root(); 447766Scarlsonj if (inaltroot) 448766Scarlsonj fp = zonecfg_open_scratch("", B_FALSE); 449766Scarlsonj else 450766Scarlsonj fp = NULL; 451766Scarlsonj zentp = zents; 452766Scarlsonj retv = Z_OK; 4530Sstevel@tonic-gate for (i = 0; i < nzents; i++) { 4540Sstevel@tonic-gate char name[ZONENAME_MAX]; 455766Scarlsonj char altname[ZONENAME_MAX]; 4560Sstevel@tonic-gate 457766Scarlsonj if (getzonenamebyid(zids[i], name, sizeof (name)) < 0) { 4580Sstevel@tonic-gate zperror(gettext("failed to get zone name"), B_FALSE); 459766Scarlsonj retv = Z_ERR; 460766Scarlsonj continue; 461766Scarlsonj } 462766Scarlsonj if (zonecfg_is_scratch(name)) { 463766Scarlsonj /* Ignore scratch zones by default */ 464766Scarlsonj if (!inaltroot) 465766Scarlsonj continue; 466766Scarlsonj if (fp == NULL || 467766Scarlsonj zonecfg_reverse_scratch(fp, name, altname, 468766Scarlsonj sizeof (altname), NULL, 0) == -1) { 469766Scarlsonj zerror(gettext("cannot resolve scratch " 470766Scarlsonj "zone %s"), name); 471766Scarlsonj retv = Z_ERR; 472766Scarlsonj continue; 473766Scarlsonj } 474766Scarlsonj (void) strcpy(name, altname); 475766Scarlsonj } else { 476766Scarlsonj /* Ignore non-scratch when in an alternate root */ 477766Scarlsonj if (inaltroot && strcmp(name, GLOBAL_ZONENAME) != 0) 478766Scarlsonj continue; 479766Scarlsonj } 480766Scarlsonj if (lookup_zone_info(name, zids[i], zentp) != Z_OK) { 481766Scarlsonj zerror(gettext("failed to get zone data")); 482766Scarlsonj retv = Z_ERR; 483766Scarlsonj continue; 484766Scarlsonj } 485766Scarlsonj zentp++; 4860Sstevel@tonic-gate } 487766Scarlsonj nzents = zentp - zents; 488766Scarlsonj if (fp != NULL) 489766Scarlsonj zonecfg_close_scratch(fp); 4900Sstevel@tonic-gate 4910Sstevel@tonic-gate free(zids); 492766Scarlsonj return (retv); 4930Sstevel@tonic-gate } 4940Sstevel@tonic-gate 495766Scarlsonj static int 4960Sstevel@tonic-gate zone_print_list(zone_state_t min_state, boolean_t verbose, boolean_t parsable) 4970Sstevel@tonic-gate { 4980Sstevel@tonic-gate int i; 4990Sstevel@tonic-gate zone_entry_t zent; 5000Sstevel@tonic-gate FILE *cookie; 5010Sstevel@tonic-gate char *name; 5020Sstevel@tonic-gate 5030Sstevel@tonic-gate /* 5040Sstevel@tonic-gate * First get the list of running zones from the kernel and print them. 5050Sstevel@tonic-gate * If that is all we need, then return. 5060Sstevel@tonic-gate */ 507766Scarlsonj if ((i = fetch_zents()) != Z_OK) { 5080Sstevel@tonic-gate /* 5090Sstevel@tonic-gate * No need for error messages; fetch_zents() has already taken 5100Sstevel@tonic-gate * care of this. 5110Sstevel@tonic-gate */ 512766Scarlsonj return (i); 5130Sstevel@tonic-gate } 514766Scarlsonj for (i = 0; i < nzents; i++) 5150Sstevel@tonic-gate zone_print(&zents[i], verbose, parsable); 5160Sstevel@tonic-gate if (min_state >= ZONE_STATE_RUNNING) 517766Scarlsonj return (Z_OK); 5180Sstevel@tonic-gate /* 5190Sstevel@tonic-gate * Next, get the full list of zones from the configuration, skipping 5200Sstevel@tonic-gate * any we have already printed. 5210Sstevel@tonic-gate */ 5220Sstevel@tonic-gate cookie = setzoneent(); 5230Sstevel@tonic-gate while ((name = getzoneent(cookie)) != NULL) { 5240Sstevel@tonic-gate for (i = 0; i < nzents; i++) { 5250Sstevel@tonic-gate if (strcmp(zents[i].zname, name) == 0) 5260Sstevel@tonic-gate break; 5270Sstevel@tonic-gate } 5280Sstevel@tonic-gate if (i < nzents) { 5290Sstevel@tonic-gate free(name); 5300Sstevel@tonic-gate continue; 5310Sstevel@tonic-gate } 532766Scarlsonj if (lookup_zone_info(name, ZONE_ID_UNDEFINED, &zent) != Z_OK) { 5330Sstevel@tonic-gate free(name); 5340Sstevel@tonic-gate continue; 5350Sstevel@tonic-gate } 5360Sstevel@tonic-gate free(name); 5370Sstevel@tonic-gate if (zent.zstate_num >= min_state) 5380Sstevel@tonic-gate zone_print(&zent, verbose, parsable); 5390Sstevel@tonic-gate } 5400Sstevel@tonic-gate endzoneent(cookie); 541766Scarlsonj return (Z_OK); 5420Sstevel@tonic-gate } 5430Sstevel@tonic-gate 5440Sstevel@tonic-gate static zone_entry_t * 5450Sstevel@tonic-gate lookup_running_zone(char *str) 5460Sstevel@tonic-gate { 5470Sstevel@tonic-gate zoneid_t zoneid; 5480Sstevel@tonic-gate char *cp; 5490Sstevel@tonic-gate int i; 5500Sstevel@tonic-gate 5510Sstevel@tonic-gate if (fetch_zents() != Z_OK) 5520Sstevel@tonic-gate return (NULL); 5530Sstevel@tonic-gate 5540Sstevel@tonic-gate for (i = 0; i < nzents; i++) { 5550Sstevel@tonic-gate if (strcmp(str, zents[i].zname) == 0) 5560Sstevel@tonic-gate return (&zents[i]); 5570Sstevel@tonic-gate } 5580Sstevel@tonic-gate errno = 0; 5590Sstevel@tonic-gate zoneid = strtol(str, &cp, 0); 5600Sstevel@tonic-gate if (zoneid < MIN_ZONEID || zoneid > MAX_ZONEID || 5610Sstevel@tonic-gate errno != 0 || *cp != '\0') 5620Sstevel@tonic-gate return (NULL); 5630Sstevel@tonic-gate for (i = 0; i < nzents; i++) { 5640Sstevel@tonic-gate if (zoneid == zents[i].zid) 5650Sstevel@tonic-gate return (&zents[i]); 5660Sstevel@tonic-gate } 5670Sstevel@tonic-gate return (NULL); 5680Sstevel@tonic-gate } 5690Sstevel@tonic-gate 5700Sstevel@tonic-gate /* 5710Sstevel@tonic-gate * Check a bit in a mode_t: if on is B_TRUE, that bit should be on; if 5720Sstevel@tonic-gate * B_FALSE, it should be off. Return B_TRUE if the mode is bad (incorrect). 5730Sstevel@tonic-gate */ 5740Sstevel@tonic-gate static boolean_t 5750Sstevel@tonic-gate bad_mode_bit(mode_t mode, mode_t bit, boolean_t on, char *file) 5760Sstevel@tonic-gate { 5770Sstevel@tonic-gate char *str; 5780Sstevel@tonic-gate 5790Sstevel@tonic-gate assert(bit == S_IRUSR || bit == S_IWUSR || bit == S_IXUSR || 5800Sstevel@tonic-gate bit == S_IRGRP || bit == S_IWGRP || bit == S_IXGRP || 5810Sstevel@tonic-gate bit == S_IROTH || bit == S_IWOTH || bit == S_IXOTH); 5820Sstevel@tonic-gate /* 5830Sstevel@tonic-gate * TRANSLATION_NOTE 5840Sstevel@tonic-gate * The strings below will be used as part of a larger message, 5850Sstevel@tonic-gate * either: 5860Sstevel@tonic-gate * (file name) must be (owner|group|world) (read|writ|execut)able 5870Sstevel@tonic-gate * or 5880Sstevel@tonic-gate * (file name) must not be (owner|group|world) (read|writ|execut)able 5890Sstevel@tonic-gate */ 5900Sstevel@tonic-gate switch (bit) { 5910Sstevel@tonic-gate case S_IRUSR: 5920Sstevel@tonic-gate str = gettext("owner readable"); 5930Sstevel@tonic-gate break; 5940Sstevel@tonic-gate case S_IWUSR: 5950Sstevel@tonic-gate str = gettext("owner writable"); 5960Sstevel@tonic-gate break; 5970Sstevel@tonic-gate case S_IXUSR: 5980Sstevel@tonic-gate str = gettext("owner executable"); 5990Sstevel@tonic-gate break; 6000Sstevel@tonic-gate case S_IRGRP: 6010Sstevel@tonic-gate str = gettext("group readable"); 6020Sstevel@tonic-gate break; 6030Sstevel@tonic-gate case S_IWGRP: 6040Sstevel@tonic-gate str = gettext("group writable"); 6050Sstevel@tonic-gate break; 6060Sstevel@tonic-gate case S_IXGRP: 6070Sstevel@tonic-gate str = gettext("group executable"); 6080Sstevel@tonic-gate break; 6090Sstevel@tonic-gate case S_IROTH: 6100Sstevel@tonic-gate str = gettext("world readable"); 6110Sstevel@tonic-gate break; 6120Sstevel@tonic-gate case S_IWOTH: 6130Sstevel@tonic-gate str = gettext("world writable"); 6140Sstevel@tonic-gate break; 6150Sstevel@tonic-gate case S_IXOTH: 6160Sstevel@tonic-gate str = gettext("world executable"); 6170Sstevel@tonic-gate break; 6180Sstevel@tonic-gate } 6190Sstevel@tonic-gate if ((mode & bit) == (on ? 0 : bit)) { 6200Sstevel@tonic-gate /* 6210Sstevel@tonic-gate * TRANSLATION_NOTE 6220Sstevel@tonic-gate * The first parameter below is a file name; the second 6230Sstevel@tonic-gate * is one of the "(owner|group|world) (read|writ|execut)able" 6240Sstevel@tonic-gate * strings from above. 6250Sstevel@tonic-gate */ 6260Sstevel@tonic-gate /* 6270Sstevel@tonic-gate * The code below could be simplified but not in a way 6280Sstevel@tonic-gate * that would easily translate to non-English locales. 6290Sstevel@tonic-gate */ 6300Sstevel@tonic-gate if (on) { 6310Sstevel@tonic-gate (void) fprintf(stderr, gettext("%s must be %s.\n"), 6320Sstevel@tonic-gate file, str); 6330Sstevel@tonic-gate } else { 6340Sstevel@tonic-gate (void) fprintf(stderr, gettext("%s must not be %s.\n"), 6350Sstevel@tonic-gate file, str); 6360Sstevel@tonic-gate } 6370Sstevel@tonic-gate return (B_TRUE); 6380Sstevel@tonic-gate } 6390Sstevel@tonic-gate return (B_FALSE); 6400Sstevel@tonic-gate } 6410Sstevel@tonic-gate 6420Sstevel@tonic-gate /* 6430Sstevel@tonic-gate * We want to make sure that no zone has its zone path as a child node 6440Sstevel@tonic-gate * (in the directory sense) of any other. We do that by comparing this 6450Sstevel@tonic-gate * zone's path to the path of all other (non-global) zones. The comparison 6460Sstevel@tonic-gate * in each case is simple: add '/' to the end of the path, then do a 6470Sstevel@tonic-gate * strncmp() of the two paths, using the length of the shorter one. 6480Sstevel@tonic-gate */ 6490Sstevel@tonic-gate 6500Sstevel@tonic-gate static int 6510Sstevel@tonic-gate crosscheck_zonepaths(char *path) 6520Sstevel@tonic-gate { 6530Sstevel@tonic-gate char rpath[MAXPATHLEN]; /* resolved path */ 6540Sstevel@tonic-gate char path_copy[MAXPATHLEN]; /* copy of original path */ 6550Sstevel@tonic-gate char rpath_copy[MAXPATHLEN]; /* copy of original rpath */ 6560Sstevel@tonic-gate struct zoneent *ze; 6570Sstevel@tonic-gate int res, err; 6580Sstevel@tonic-gate FILE *cookie; 6590Sstevel@tonic-gate 6600Sstevel@tonic-gate cookie = setzoneent(); 6610Sstevel@tonic-gate while ((ze = getzoneent_private(cookie)) != NULL) { 6620Sstevel@tonic-gate /* Skip zones which are not installed. */ 6630Sstevel@tonic-gate if (ze->zone_state < ZONE_STATE_INSTALLED) { 6640Sstevel@tonic-gate free(ze); 6650Sstevel@tonic-gate continue; 6660Sstevel@tonic-gate } 6670Sstevel@tonic-gate /* Skip the global zone and the current target zone. */ 6680Sstevel@tonic-gate if (strcmp(ze->zone_name, GLOBAL_ZONENAME) == 0 || 6690Sstevel@tonic-gate strcmp(ze->zone_name, target_zone) == 0) { 6700Sstevel@tonic-gate free(ze); 6710Sstevel@tonic-gate continue; 6720Sstevel@tonic-gate } 6730Sstevel@tonic-gate if (strlen(ze->zone_path) == 0) { 6740Sstevel@tonic-gate /* old index file without path, fall back */ 6750Sstevel@tonic-gate if ((err = zone_get_zonepath(ze->zone_name, 6760Sstevel@tonic-gate ze->zone_path, sizeof (ze->zone_path))) != Z_OK) { 6770Sstevel@tonic-gate errno = err; 6780Sstevel@tonic-gate zperror2(ze->zone_name, 6790Sstevel@tonic-gate gettext("could not get zone path")); 6800Sstevel@tonic-gate free(ze); 6810Sstevel@tonic-gate continue; 6820Sstevel@tonic-gate } 6830Sstevel@tonic-gate } 684766Scarlsonj (void) snprintf(path_copy, sizeof (path_copy), "%s%s", 685766Scarlsonj zonecfg_get_root(), ze->zone_path); 686766Scarlsonj res = resolvepath(path_copy, rpath, sizeof (rpath)); 6870Sstevel@tonic-gate if (res == -1) { 6880Sstevel@tonic-gate if (errno != ENOENT) { 689766Scarlsonj zperror(path_copy, B_FALSE); 6900Sstevel@tonic-gate free(ze); 6910Sstevel@tonic-gate return (Z_ERR); 6920Sstevel@tonic-gate } 6930Sstevel@tonic-gate (void) printf(gettext("WARNING: zone %s is installed, " 6940Sstevel@tonic-gate "but its %s %s does not exist.\n"), ze->zone_name, 695766Scarlsonj "zonepath", path_copy); 6960Sstevel@tonic-gate free(ze); 6970Sstevel@tonic-gate continue; 6980Sstevel@tonic-gate } 6990Sstevel@tonic-gate rpath[res] = '\0'; 7000Sstevel@tonic-gate (void) snprintf(path_copy, sizeof (path_copy), "%s/", path); 7010Sstevel@tonic-gate (void) snprintf(rpath_copy, sizeof (rpath_copy), "%s/", rpath); 7020Sstevel@tonic-gate if (strncmp(path_copy, rpath_copy, 7030Sstevel@tonic-gate min(strlen(path_copy), strlen(rpath_copy))) == 0) { 7040Sstevel@tonic-gate (void) fprintf(stderr, gettext("%s zonepath (%s) and " 7050Sstevel@tonic-gate "%s zonepath (%s) overlap.\n"), 7060Sstevel@tonic-gate target_zone, path, ze->zone_name, rpath); 7070Sstevel@tonic-gate free(ze); 7080Sstevel@tonic-gate return (Z_ERR); 7090Sstevel@tonic-gate } 7100Sstevel@tonic-gate free(ze); 7110Sstevel@tonic-gate } 7120Sstevel@tonic-gate endzoneent(cookie); 7130Sstevel@tonic-gate return (Z_OK); 7140Sstevel@tonic-gate } 7150Sstevel@tonic-gate 7160Sstevel@tonic-gate static int 7170Sstevel@tonic-gate validate_zonepath(char *path, int cmd_num) 7180Sstevel@tonic-gate { 7190Sstevel@tonic-gate int res; /* result of last library/system call */ 7200Sstevel@tonic-gate boolean_t err = B_FALSE; /* have we run into an error? */ 7210Sstevel@tonic-gate struct stat stbuf; 7220Sstevel@tonic-gate struct statvfs vfsbuf; 7230Sstevel@tonic-gate char rpath[MAXPATHLEN]; /* resolved path */ 7240Sstevel@tonic-gate char ppath[MAXPATHLEN]; /* parent path */ 7250Sstevel@tonic-gate char rppath[MAXPATHLEN]; /* resolved parent path */ 7260Sstevel@tonic-gate char rootpath[MAXPATHLEN]; /* root path */ 7270Sstevel@tonic-gate zone_state_t state; 7280Sstevel@tonic-gate 7290Sstevel@tonic-gate if (path[0] != '/') { 7300Sstevel@tonic-gate (void) fprintf(stderr, 7310Sstevel@tonic-gate gettext("%s is not an absolute path.\n"), path); 7320Sstevel@tonic-gate return (Z_ERR); 7330Sstevel@tonic-gate } 7340Sstevel@tonic-gate if ((res = resolvepath(path, rpath, sizeof (rpath))) == -1) { 7350Sstevel@tonic-gate if ((errno != ENOENT) || 7360Sstevel@tonic-gate (cmd_num != CMD_VERIFY && cmd_num != CMD_INSTALL)) { 7370Sstevel@tonic-gate zperror(path, B_FALSE); 7380Sstevel@tonic-gate return (Z_ERR); 7390Sstevel@tonic-gate } 7400Sstevel@tonic-gate if (cmd_num == CMD_VERIFY) { 7410Sstevel@tonic-gate (void) fprintf(stderr, gettext("WARNING: %s does not " 7420Sstevel@tonic-gate "exist, so it cannot be verified.\nWhen 'zoneadm " 7430Sstevel@tonic-gate "%s' is run, '%s' will try to create\n%s, and '%s' " 7440Sstevel@tonic-gate "will be tried again,\nbut the '%s' may fail if:\n" 7450Sstevel@tonic-gate "the parent directory of %s is group- or other-" 7460Sstevel@tonic-gate "writable\nor\n%s overlaps with any other " 7470Sstevel@tonic-gate "installed zones.\n"), path, 7480Sstevel@tonic-gate cmd_to_str(CMD_INSTALL), cmd_to_str(CMD_INSTALL), 7490Sstevel@tonic-gate path, cmd_to_str(CMD_VERIFY), 7500Sstevel@tonic-gate cmd_to_str(CMD_VERIFY), path, path); 7510Sstevel@tonic-gate return (Z_OK); 7520Sstevel@tonic-gate } 7530Sstevel@tonic-gate /* 7540Sstevel@tonic-gate * The zonepath is supposed to be mode 700 but its 7550Sstevel@tonic-gate * parent(s) 755. So use 755 on the mkdirp() then 7560Sstevel@tonic-gate * chmod() the zonepath itself to 700. 7570Sstevel@tonic-gate */ 7580Sstevel@tonic-gate if (mkdirp(path, DEFAULT_DIR_MODE) < 0) { 7590Sstevel@tonic-gate zperror(path, B_FALSE); 7600Sstevel@tonic-gate return (Z_ERR); 7610Sstevel@tonic-gate } 7620Sstevel@tonic-gate /* 7630Sstevel@tonic-gate * If the chmod() fails, report the error, but might 7640Sstevel@tonic-gate * as well continue the verify procedure. 7650Sstevel@tonic-gate */ 7660Sstevel@tonic-gate if (chmod(path, S_IRWXU) != 0) 7670Sstevel@tonic-gate zperror(path, B_FALSE); 7680Sstevel@tonic-gate /* 7690Sstevel@tonic-gate * Since the mkdir() succeeded, we should not have to 7700Sstevel@tonic-gate * worry about a subsequent ENOENT, thus this should 7710Sstevel@tonic-gate * only recurse once. 7720Sstevel@tonic-gate */ 7730Sstevel@tonic-gate return (validate_zonepath(path, CMD_INSTALL)); 7740Sstevel@tonic-gate } 7750Sstevel@tonic-gate rpath[res] = '\0'; 7760Sstevel@tonic-gate if (strcmp(path, rpath) != 0) { 7770Sstevel@tonic-gate errno = Z_RESOLVED_PATH; 7780Sstevel@tonic-gate zperror(path, B_TRUE); 7790Sstevel@tonic-gate return (Z_ERR); 7800Sstevel@tonic-gate } 7810Sstevel@tonic-gate if ((res = stat(rpath, &stbuf)) != 0) { 7820Sstevel@tonic-gate zperror(rpath, B_FALSE); 7830Sstevel@tonic-gate return (Z_ERR); 7840Sstevel@tonic-gate } 7850Sstevel@tonic-gate if (!S_ISDIR(stbuf.st_mode)) { 7860Sstevel@tonic-gate (void) fprintf(stderr, gettext("%s is not a directory.\n"), 7870Sstevel@tonic-gate rpath); 7880Sstevel@tonic-gate return (Z_ERR); 7890Sstevel@tonic-gate } 7900Sstevel@tonic-gate if ((strcmp(stbuf.st_fstype, MNTTYPE_TMPFS) == 0) || 7910Sstevel@tonic-gate (strcmp(stbuf.st_fstype, MNTTYPE_XMEMFS) == 0)) { 7920Sstevel@tonic-gate (void) printf(gettext("WARNING: %s is on a temporary " 7930Sstevel@tonic-gate "file-system.\n"), rpath); 7940Sstevel@tonic-gate } 7950Sstevel@tonic-gate if (crosscheck_zonepaths(rpath) != Z_OK) 7960Sstevel@tonic-gate return (Z_ERR); 7970Sstevel@tonic-gate /* 7980Sstevel@tonic-gate * Try to collect and report as many minor errors as possible 7990Sstevel@tonic-gate * before returning, so the user can learn everything that needs 8000Sstevel@tonic-gate * to be fixed up front. 8010Sstevel@tonic-gate */ 8020Sstevel@tonic-gate if (stbuf.st_uid != 0) { 8030Sstevel@tonic-gate (void) fprintf(stderr, gettext("%s is not owned by root.\n"), 8040Sstevel@tonic-gate rpath); 8050Sstevel@tonic-gate err = B_TRUE; 8060Sstevel@tonic-gate } 8070Sstevel@tonic-gate err |= bad_mode_bit(stbuf.st_mode, S_IRUSR, B_TRUE, rpath); 8080Sstevel@tonic-gate err |= bad_mode_bit(stbuf.st_mode, S_IWUSR, B_TRUE, rpath); 8090Sstevel@tonic-gate err |= bad_mode_bit(stbuf.st_mode, S_IXUSR, B_TRUE, rpath); 8100Sstevel@tonic-gate err |= bad_mode_bit(stbuf.st_mode, S_IRGRP, B_FALSE, rpath); 8110Sstevel@tonic-gate err |= bad_mode_bit(stbuf.st_mode, S_IWGRP, B_FALSE, rpath); 8120Sstevel@tonic-gate err |= bad_mode_bit(stbuf.st_mode, S_IXGRP, B_FALSE, rpath); 8130Sstevel@tonic-gate err |= bad_mode_bit(stbuf.st_mode, S_IROTH, B_FALSE, rpath); 8140Sstevel@tonic-gate err |= bad_mode_bit(stbuf.st_mode, S_IWOTH, B_FALSE, rpath); 8150Sstevel@tonic-gate err |= bad_mode_bit(stbuf.st_mode, S_IXOTH, B_FALSE, rpath); 8160Sstevel@tonic-gate 8170Sstevel@tonic-gate (void) snprintf(ppath, sizeof (ppath), "%s/..", path); 8180Sstevel@tonic-gate if ((res = resolvepath(ppath, rppath, sizeof (rppath))) == -1) { 8190Sstevel@tonic-gate zperror(ppath, B_FALSE); 8200Sstevel@tonic-gate return (Z_ERR); 8210Sstevel@tonic-gate } 8220Sstevel@tonic-gate rppath[res] = '\0'; 8230Sstevel@tonic-gate if ((res = stat(rppath, &stbuf)) != 0) { 8240Sstevel@tonic-gate zperror(rppath, B_FALSE); 8250Sstevel@tonic-gate return (Z_ERR); 8260Sstevel@tonic-gate } 8270Sstevel@tonic-gate /* theoretically impossible */ 8280Sstevel@tonic-gate if (!S_ISDIR(stbuf.st_mode)) { 8290Sstevel@tonic-gate (void) fprintf(stderr, gettext("%s is not a directory.\n"), 8300Sstevel@tonic-gate rppath); 8310Sstevel@tonic-gate return (Z_ERR); 8320Sstevel@tonic-gate } 8330Sstevel@tonic-gate if (stbuf.st_uid != 0) { 8340Sstevel@tonic-gate (void) fprintf(stderr, gettext("%s is not owned by root.\n"), 8350Sstevel@tonic-gate rppath); 8360Sstevel@tonic-gate err = B_TRUE; 8370Sstevel@tonic-gate } 8380Sstevel@tonic-gate err |= bad_mode_bit(stbuf.st_mode, S_IRUSR, B_TRUE, rppath); 8390Sstevel@tonic-gate err |= bad_mode_bit(stbuf.st_mode, S_IWUSR, B_TRUE, rppath); 8400Sstevel@tonic-gate err |= bad_mode_bit(stbuf.st_mode, S_IXUSR, B_TRUE, rppath); 8410Sstevel@tonic-gate err |= bad_mode_bit(stbuf.st_mode, S_IWGRP, B_FALSE, rppath); 8420Sstevel@tonic-gate err |= bad_mode_bit(stbuf.st_mode, S_IWOTH, B_FALSE, rppath); 8430Sstevel@tonic-gate if (strcmp(rpath, rppath) == 0) { 8440Sstevel@tonic-gate (void) fprintf(stderr, gettext("%s is its own parent.\n"), 8450Sstevel@tonic-gate rppath); 8460Sstevel@tonic-gate err = B_TRUE; 8470Sstevel@tonic-gate } 8480Sstevel@tonic-gate 8490Sstevel@tonic-gate if (statvfs(rpath, &vfsbuf) != 0) { 8500Sstevel@tonic-gate zperror(rpath, B_FALSE); 8510Sstevel@tonic-gate return (Z_ERR); 8520Sstevel@tonic-gate } 8530Sstevel@tonic-gate if (strncmp(vfsbuf.f_basetype, "nfs", 3) == 0) { 8540Sstevel@tonic-gate (void) fprintf(stderr, gettext("Zonepath %s is over NFS, " 8550Sstevel@tonic-gate "which is not currently supported.\n"), rpath); 8560Sstevel@tonic-gate return (Z_ERR); 8570Sstevel@tonic-gate } 8580Sstevel@tonic-gate 8590Sstevel@tonic-gate if ((res = zone_get_state(target_zone, &state)) != Z_OK) { 8600Sstevel@tonic-gate errno = res; 8610Sstevel@tonic-gate zperror2(target_zone, gettext("could not get state")); 8620Sstevel@tonic-gate return (Z_ERR); 8630Sstevel@tonic-gate } 8640Sstevel@tonic-gate /* 8650Sstevel@tonic-gate * The existence of the root path is only bad in the configured state, 8660Sstevel@tonic-gate * as it is *supposed* to be there at the installed and later states. 8670Sstevel@tonic-gate * State/command mismatches are caught earlier in verify_details(). 8680Sstevel@tonic-gate */ 8690Sstevel@tonic-gate if (state == ZONE_STATE_CONFIGURED) { 8700Sstevel@tonic-gate if (snprintf(rootpath, sizeof (rootpath), "%s/root", rpath) >= 8710Sstevel@tonic-gate sizeof (rootpath)) { 8720Sstevel@tonic-gate (void) fprintf(stderr, 8730Sstevel@tonic-gate gettext("Zonepath %s is too long.\n"), rpath); 8740Sstevel@tonic-gate return (Z_ERR); 8750Sstevel@tonic-gate } 8760Sstevel@tonic-gate if ((res = stat(rootpath, &stbuf)) == 0) { 8770Sstevel@tonic-gate (void) fprintf(stderr, gettext("Rootpath %s exists; " 8780Sstevel@tonic-gate "remove or move aside prior to %s.\n"), rootpath, 8790Sstevel@tonic-gate cmd_to_str(CMD_INSTALL)); 8800Sstevel@tonic-gate return (Z_ERR); 8810Sstevel@tonic-gate } 8820Sstevel@tonic-gate } 8830Sstevel@tonic-gate 8840Sstevel@tonic-gate return (err ? Z_ERR : Z_OK); 8850Sstevel@tonic-gate } 8860Sstevel@tonic-gate 8870Sstevel@tonic-gate static void 8880Sstevel@tonic-gate release_lock_file(int lockfd) 8890Sstevel@tonic-gate { 8900Sstevel@tonic-gate (void) close(lockfd); 8910Sstevel@tonic-gate } 8920Sstevel@tonic-gate 8930Sstevel@tonic-gate static int 8940Sstevel@tonic-gate grab_lock_file(const char *zone_name, int *lockfd) 8950Sstevel@tonic-gate { 8960Sstevel@tonic-gate char pathbuf[PATH_MAX]; 8970Sstevel@tonic-gate struct flock flock; 8980Sstevel@tonic-gate 899766Scarlsonj if (snprintf(pathbuf, sizeof (pathbuf), "%s%s", zonecfg_get_root(), 900766Scarlsonj ZONES_TMPDIR) >= sizeof (pathbuf)) { 901766Scarlsonj zerror(gettext("alternate root path is too long")); 902766Scarlsonj return (Z_ERR); 903766Scarlsonj } 904766Scarlsonj if (mkdir(pathbuf, S_IRWXU) < 0 && errno != EEXIST) { 905766Scarlsonj zerror(gettext("could not mkdir %s: %s"), pathbuf, 9060Sstevel@tonic-gate strerror(errno)); 9070Sstevel@tonic-gate return (Z_ERR); 9080Sstevel@tonic-gate } 909766Scarlsonj (void) chmod(pathbuf, S_IRWXU); 9100Sstevel@tonic-gate 9110Sstevel@tonic-gate /* 9120Sstevel@tonic-gate * One of these lock files is created for each zone (when needed). 9130Sstevel@tonic-gate * The lock files are not cleaned up (except on system reboot), 9140Sstevel@tonic-gate * but since there is only one per zone, there is no resource 9150Sstevel@tonic-gate * starvation issue. 9160Sstevel@tonic-gate */ 917766Scarlsonj if (snprintf(pathbuf, sizeof (pathbuf), "%s%s/%s.zoneadm.lock", 918766Scarlsonj zonecfg_get_root(), ZONES_TMPDIR, zone_name) >= sizeof (pathbuf)) { 919766Scarlsonj zerror(gettext("alternate root path is too long")); 920766Scarlsonj return (Z_ERR); 921766Scarlsonj } 9220Sstevel@tonic-gate if ((*lockfd = open(pathbuf, O_RDWR|O_CREAT, S_IRUSR|S_IWUSR)) < 0) { 9230Sstevel@tonic-gate zerror(gettext("could not open %s: %s"), pathbuf, 9240Sstevel@tonic-gate strerror(errno)); 9250Sstevel@tonic-gate return (Z_ERR); 9260Sstevel@tonic-gate } 9270Sstevel@tonic-gate /* 9280Sstevel@tonic-gate * Lock the file to synchronize with other zoneadmds 9290Sstevel@tonic-gate */ 9300Sstevel@tonic-gate flock.l_type = F_WRLCK; 9310Sstevel@tonic-gate flock.l_whence = SEEK_SET; 9320Sstevel@tonic-gate flock.l_start = (off_t)0; 9330Sstevel@tonic-gate flock.l_len = (off_t)0; 9340Sstevel@tonic-gate if (fcntl(*lockfd, F_SETLKW, &flock) < 0) { 9350Sstevel@tonic-gate zerror(gettext("unable to lock %s: %s"), pathbuf, 9360Sstevel@tonic-gate strerror(errno)); 9370Sstevel@tonic-gate release_lock_file(*lockfd); 9380Sstevel@tonic-gate return (Z_ERR); 9390Sstevel@tonic-gate } 9400Sstevel@tonic-gate return (Z_OK); 9410Sstevel@tonic-gate } 9420Sstevel@tonic-gate 943766Scarlsonj static boolean_t 9440Sstevel@tonic-gate get_doorname(const char *zone_name, char *buffer) 9450Sstevel@tonic-gate { 946766Scarlsonj return (snprintf(buffer, PATH_MAX, "%s" ZONE_DOOR_PATH, 947766Scarlsonj zonecfg_get_root(), zone_name) < PATH_MAX); 9480Sstevel@tonic-gate } 9490Sstevel@tonic-gate 9500Sstevel@tonic-gate /* 9510Sstevel@tonic-gate * system daemons are not audited. For the global zone, this occurs 9520Sstevel@tonic-gate * "naturally" since init is started with the default audit 9530Sstevel@tonic-gate * characteristics. Since zoneadmd is a system daemon and it starts 9540Sstevel@tonic-gate * init for a zone, it is necessary to clear out the audit 9550Sstevel@tonic-gate * characteristics inherited from whomever started zoneadmd. This is 9560Sstevel@tonic-gate * indicated by the audit id, which is set from the ruid parameter of 9570Sstevel@tonic-gate * adt_set_user(), below. 9580Sstevel@tonic-gate */ 9590Sstevel@tonic-gate 9600Sstevel@tonic-gate static void 9610Sstevel@tonic-gate prepare_audit_context() 9620Sstevel@tonic-gate { 9630Sstevel@tonic-gate adt_session_data_t *ah; 9640Sstevel@tonic-gate char *failure = gettext("audit failure: %s"); 9650Sstevel@tonic-gate 9660Sstevel@tonic-gate if (adt_start_session(&ah, NULL, 0)) { 9670Sstevel@tonic-gate zerror(failure, strerror(errno)); 9680Sstevel@tonic-gate return; 9690Sstevel@tonic-gate } 9700Sstevel@tonic-gate if (adt_set_user(ah, ADT_NO_AUDIT, ADT_NO_AUDIT, 9710Sstevel@tonic-gate ADT_NO_AUDIT, ADT_NO_AUDIT, NULL, ADT_NEW)) { 9720Sstevel@tonic-gate zerror(failure, strerror(errno)); 9730Sstevel@tonic-gate (void) adt_end_session(ah); 9740Sstevel@tonic-gate return; 9750Sstevel@tonic-gate } 9760Sstevel@tonic-gate if (adt_set_proc(ah)) 9770Sstevel@tonic-gate zerror(failure, strerror(errno)); 9780Sstevel@tonic-gate 9790Sstevel@tonic-gate (void) adt_end_session(ah); 9800Sstevel@tonic-gate } 9810Sstevel@tonic-gate 9820Sstevel@tonic-gate static int 9830Sstevel@tonic-gate start_zoneadmd(const char *zone_name) 9840Sstevel@tonic-gate { 9850Sstevel@tonic-gate char doorpath[PATH_MAX]; 9860Sstevel@tonic-gate pid_t child_pid; 9870Sstevel@tonic-gate int error = Z_ERR; 9880Sstevel@tonic-gate int doorfd, lockfd; 9890Sstevel@tonic-gate struct door_info info; 9900Sstevel@tonic-gate 991766Scarlsonj if (!get_doorname(zone_name, doorpath)) 992766Scarlsonj return (Z_ERR); 9930Sstevel@tonic-gate 9940Sstevel@tonic-gate if (grab_lock_file(zone_name, &lockfd) != Z_OK) 9950Sstevel@tonic-gate return (Z_ERR); 9960Sstevel@tonic-gate 9970Sstevel@tonic-gate /* 9980Sstevel@tonic-gate * Now that we have the lock, re-confirm that the daemon is 9990Sstevel@tonic-gate * *not* up and working fine. If it is still down, we have a green 10000Sstevel@tonic-gate * light to start it. 10010Sstevel@tonic-gate */ 10020Sstevel@tonic-gate if ((doorfd = open(doorpath, O_RDONLY)) < 0) { 10030Sstevel@tonic-gate if (errno != ENOENT) { 10040Sstevel@tonic-gate zperror(doorpath, B_FALSE); 10050Sstevel@tonic-gate goto out; 10060Sstevel@tonic-gate } 10070Sstevel@tonic-gate } else { 10080Sstevel@tonic-gate if (door_info(doorfd, &info) == 0 && 10090Sstevel@tonic-gate ((info.di_attributes & DOOR_REVOKED) == 0)) { 10100Sstevel@tonic-gate error = Z_OK; 10110Sstevel@tonic-gate (void) close(doorfd); 10120Sstevel@tonic-gate goto out; 10130Sstevel@tonic-gate } 10140Sstevel@tonic-gate (void) close(doorfd); 10150Sstevel@tonic-gate } 10160Sstevel@tonic-gate 10170Sstevel@tonic-gate if ((child_pid = fork()) == -1) { 10180Sstevel@tonic-gate zperror(gettext("could not fork"), B_FALSE); 10190Sstevel@tonic-gate goto out; 10200Sstevel@tonic-gate } else if (child_pid == 0) { 1021766Scarlsonj const char *argv[6], **ap; 1022766Scarlsonj 10230Sstevel@tonic-gate /* child process */ 10240Sstevel@tonic-gate prepare_audit_context(); 10250Sstevel@tonic-gate 1026766Scarlsonj ap = argv; 1027766Scarlsonj *ap++ = "zoneadmd"; 1028766Scarlsonj *ap++ = "-z"; 1029766Scarlsonj *ap++ = zone_name; 1030766Scarlsonj if (zonecfg_in_alt_root()) { 1031766Scarlsonj *ap++ = "-R"; 1032766Scarlsonj *ap++ = zonecfg_get_root(); 1033766Scarlsonj } 1034766Scarlsonj *ap = NULL; 1035766Scarlsonj 1036766Scarlsonj (void) execv("/usr/lib/zones/zoneadmd", (char * const *)argv); 10370Sstevel@tonic-gate zperror(gettext("could not exec zoneadmd"), B_FALSE); 10380Sstevel@tonic-gate _exit(Z_ERR); 10390Sstevel@tonic-gate } else { 10400Sstevel@tonic-gate /* parent process */ 10410Sstevel@tonic-gate pid_t retval; 10420Sstevel@tonic-gate int pstatus = 0; 10430Sstevel@tonic-gate 10440Sstevel@tonic-gate do { 10450Sstevel@tonic-gate retval = waitpid(child_pid, &pstatus, 0); 10460Sstevel@tonic-gate } while (retval != child_pid); 10470Sstevel@tonic-gate if (WIFSIGNALED(pstatus) || (WIFEXITED(pstatus) && 10480Sstevel@tonic-gate WEXITSTATUS(pstatus) != 0)) { 10490Sstevel@tonic-gate zerror(gettext("could not start %s"), "zoneadmd"); 10500Sstevel@tonic-gate goto out; 10510Sstevel@tonic-gate } 10520Sstevel@tonic-gate } 10530Sstevel@tonic-gate error = Z_OK; 10540Sstevel@tonic-gate out: 10550Sstevel@tonic-gate release_lock_file(lockfd); 10560Sstevel@tonic-gate return (error); 10570Sstevel@tonic-gate } 10580Sstevel@tonic-gate 10590Sstevel@tonic-gate static int 10600Sstevel@tonic-gate ping_zoneadmd(const char *zone_name) 10610Sstevel@tonic-gate { 10620Sstevel@tonic-gate char doorpath[PATH_MAX]; 10630Sstevel@tonic-gate int doorfd; 10640Sstevel@tonic-gate struct door_info info; 10650Sstevel@tonic-gate 1066766Scarlsonj if (!get_doorname(zone_name, doorpath)) 1067766Scarlsonj return (Z_ERR); 10680Sstevel@tonic-gate 10690Sstevel@tonic-gate if ((doorfd = open(doorpath, O_RDONLY)) < 0) { 10700Sstevel@tonic-gate return (Z_ERR); 10710Sstevel@tonic-gate } 10720Sstevel@tonic-gate if (door_info(doorfd, &info) == 0 && 10730Sstevel@tonic-gate ((info.di_attributes & DOOR_REVOKED) == 0)) { 10740Sstevel@tonic-gate (void) close(doorfd); 10750Sstevel@tonic-gate return (Z_OK); 10760Sstevel@tonic-gate } 10770Sstevel@tonic-gate (void) close(doorfd); 10780Sstevel@tonic-gate return (Z_ERR); 10790Sstevel@tonic-gate } 10800Sstevel@tonic-gate 10810Sstevel@tonic-gate static int 10820Sstevel@tonic-gate call_zoneadmd(const char *zone_name, zone_cmd_arg_t *arg) 10830Sstevel@tonic-gate { 10840Sstevel@tonic-gate char doorpath[PATH_MAX]; 10850Sstevel@tonic-gate int doorfd, result; 10860Sstevel@tonic-gate door_arg_t darg; 10870Sstevel@tonic-gate 10880Sstevel@tonic-gate zoneid_t zoneid; 10890Sstevel@tonic-gate uint64_t uniqid = 0; 10900Sstevel@tonic-gate 10910Sstevel@tonic-gate zone_cmd_rval_t *rvalp; 10920Sstevel@tonic-gate size_t rlen; 10930Sstevel@tonic-gate char *cp, *errbuf; 10940Sstevel@tonic-gate 10950Sstevel@tonic-gate rlen = getpagesize(); 10960Sstevel@tonic-gate if ((rvalp = malloc(rlen)) == NULL) { 10970Sstevel@tonic-gate zerror(gettext("failed to allocate %lu bytes: %s"), rlen, 10980Sstevel@tonic-gate strerror(errno)); 10990Sstevel@tonic-gate return (-1); 11000Sstevel@tonic-gate } 11010Sstevel@tonic-gate 11020Sstevel@tonic-gate if ((zoneid = getzoneidbyname(zone_name)) != ZONE_ID_UNDEFINED) { 11030Sstevel@tonic-gate (void) zone_getattr(zoneid, ZONE_ATTR_UNIQID, &uniqid, 11040Sstevel@tonic-gate sizeof (uniqid)); 11050Sstevel@tonic-gate } 11060Sstevel@tonic-gate arg->uniqid = uniqid; 11070Sstevel@tonic-gate (void) strlcpy(arg->locale, locale, sizeof (arg->locale)); 1108766Scarlsonj if (!get_doorname(zone_name, doorpath)) { 1109766Scarlsonj zerror(gettext("alternate root path is too long")); 1110766Scarlsonj free(rvalp); 1111766Scarlsonj return (-1); 1112766Scarlsonj } 11130Sstevel@tonic-gate 11140Sstevel@tonic-gate /* 11150Sstevel@tonic-gate * Loop trying to start zoneadmd; if something goes seriously 11160Sstevel@tonic-gate * wrong we break out and fail. 11170Sstevel@tonic-gate */ 11180Sstevel@tonic-gate for (;;) { 11190Sstevel@tonic-gate if (start_zoneadmd(zone_name) != Z_OK) 11200Sstevel@tonic-gate break; 11210Sstevel@tonic-gate 11220Sstevel@tonic-gate if ((doorfd = open(doorpath, O_RDONLY)) < 0) { 11230Sstevel@tonic-gate zperror(gettext("failed to open zone door"), B_FALSE); 11240Sstevel@tonic-gate break; 11250Sstevel@tonic-gate } 11260Sstevel@tonic-gate 11270Sstevel@tonic-gate darg.data_ptr = (char *)arg; 11280Sstevel@tonic-gate darg.data_size = sizeof (*arg); 11290Sstevel@tonic-gate darg.desc_ptr = NULL; 11300Sstevel@tonic-gate darg.desc_num = 0; 11310Sstevel@tonic-gate darg.rbuf = (char *)rvalp; 11320Sstevel@tonic-gate darg.rsize = rlen; 11330Sstevel@tonic-gate if (door_call(doorfd, &darg) != 0) { 11340Sstevel@tonic-gate (void) close(doorfd); 11350Sstevel@tonic-gate /* 11360Sstevel@tonic-gate * We'll get EBADF if the door has been revoked. 11370Sstevel@tonic-gate */ 11380Sstevel@tonic-gate if (errno != EBADF) { 11390Sstevel@tonic-gate zperror(gettext("door_call failed"), B_FALSE); 11400Sstevel@tonic-gate break; 11410Sstevel@tonic-gate } 11420Sstevel@tonic-gate continue; /* take another lap */ 11430Sstevel@tonic-gate } 11440Sstevel@tonic-gate (void) close(doorfd); 11450Sstevel@tonic-gate 11460Sstevel@tonic-gate if (darg.data_size == 0) { 11470Sstevel@tonic-gate /* Door server is going away; kick it again. */ 11480Sstevel@tonic-gate continue; 11490Sstevel@tonic-gate } 11500Sstevel@tonic-gate 11510Sstevel@tonic-gate errbuf = rvalp->errbuf; 11520Sstevel@tonic-gate while (*errbuf != '\0') { 11530Sstevel@tonic-gate /* 11540Sstevel@tonic-gate * Remove any newlines since zerror() 11550Sstevel@tonic-gate * will append one automatically. 11560Sstevel@tonic-gate */ 11570Sstevel@tonic-gate cp = strchr(errbuf, '\n'); 11580Sstevel@tonic-gate if (cp != NULL) 11590Sstevel@tonic-gate *cp = '\0'; 11600Sstevel@tonic-gate zerror("%s", errbuf); 11610Sstevel@tonic-gate if (cp == NULL) 11620Sstevel@tonic-gate break; 11630Sstevel@tonic-gate errbuf = cp + 1; 11640Sstevel@tonic-gate } 11650Sstevel@tonic-gate result = rvalp->rval == 0 ? 0 : -1; 11660Sstevel@tonic-gate free(rvalp); 11670Sstevel@tonic-gate return (result); 11680Sstevel@tonic-gate } 11690Sstevel@tonic-gate 11700Sstevel@tonic-gate free(rvalp); 11710Sstevel@tonic-gate return (-1); 11720Sstevel@tonic-gate } 11730Sstevel@tonic-gate 11740Sstevel@tonic-gate static int 11750Sstevel@tonic-gate ready_func(int argc, char *argv[]) 11760Sstevel@tonic-gate { 11770Sstevel@tonic-gate zone_cmd_arg_t zarg; 11780Sstevel@tonic-gate int arg; 11790Sstevel@tonic-gate 1180766Scarlsonj if (zonecfg_in_alt_root()) { 1181766Scarlsonj zerror(gettext("cannot ready zone in alternate root")); 1182766Scarlsonj return (Z_ERR); 1183766Scarlsonj } 1184766Scarlsonj 11850Sstevel@tonic-gate optind = 0; 11860Sstevel@tonic-gate if ((arg = getopt(argc, argv, "?")) != EOF) { 11870Sstevel@tonic-gate switch (arg) { 11880Sstevel@tonic-gate case '?': 11890Sstevel@tonic-gate sub_usage(SHELP_READY, CMD_READY); 11900Sstevel@tonic-gate return (optopt == '?' ? Z_OK : Z_USAGE); 11910Sstevel@tonic-gate default: 11920Sstevel@tonic-gate sub_usage(SHELP_READY, CMD_READY); 11930Sstevel@tonic-gate return (Z_USAGE); 11940Sstevel@tonic-gate } 11950Sstevel@tonic-gate } 11960Sstevel@tonic-gate if (argc > optind) { 11970Sstevel@tonic-gate sub_usage(SHELP_READY, CMD_READY); 11980Sstevel@tonic-gate return (Z_USAGE); 11990Sstevel@tonic-gate } 12000Sstevel@tonic-gate if (sanity_check(target_zone, CMD_READY, B_FALSE, B_FALSE) != Z_OK) 12010Sstevel@tonic-gate return (Z_ERR); 12020Sstevel@tonic-gate if (verify_details(CMD_READY) != Z_OK) 12030Sstevel@tonic-gate return (Z_ERR); 12040Sstevel@tonic-gate 12050Sstevel@tonic-gate zarg.cmd = Z_READY; 12060Sstevel@tonic-gate if (call_zoneadmd(target_zone, &zarg) != 0) { 12070Sstevel@tonic-gate zerror(gettext("call to %s failed"), "zoneadmd"); 12080Sstevel@tonic-gate return (Z_ERR); 12090Sstevel@tonic-gate } 12100Sstevel@tonic-gate return (Z_OK); 12110Sstevel@tonic-gate } 12120Sstevel@tonic-gate 12130Sstevel@tonic-gate static int 12140Sstevel@tonic-gate boot_func(int argc, char *argv[]) 12150Sstevel@tonic-gate { 12160Sstevel@tonic-gate zone_cmd_arg_t zarg; 12170Sstevel@tonic-gate int arg; 12180Sstevel@tonic-gate 1219766Scarlsonj if (zonecfg_in_alt_root()) { 1220766Scarlsonj zerror(gettext("cannot boot zone in alternate root")); 1221766Scarlsonj return (Z_ERR); 1222766Scarlsonj } 1223766Scarlsonj 12240Sstevel@tonic-gate zarg.bootbuf[0] = '\0'; 12250Sstevel@tonic-gate 12260Sstevel@tonic-gate /* 12270Sstevel@tonic-gate * At the current time, the only supported subargument to the 12280Sstevel@tonic-gate * "boot" subcommand is "-s" which specifies a single-user boot. 12290Sstevel@tonic-gate * In the future, other boot arguments should be supported 12300Sstevel@tonic-gate * including "-m" for specifying alternate smf(5) milestones. 12310Sstevel@tonic-gate */ 12320Sstevel@tonic-gate optind = 0; 12330Sstevel@tonic-gate if ((arg = getopt(argc, argv, "?s")) != EOF) { 12340Sstevel@tonic-gate switch (arg) { 12350Sstevel@tonic-gate case '?': 12360Sstevel@tonic-gate sub_usage(SHELP_BOOT, CMD_BOOT); 12370Sstevel@tonic-gate return (optopt == '?' ? Z_OK : Z_USAGE); 12380Sstevel@tonic-gate case 's': 12390Sstevel@tonic-gate (void) strlcpy(zarg.bootbuf, "-s", 12400Sstevel@tonic-gate sizeof (zarg.bootbuf)); 12410Sstevel@tonic-gate break; 12420Sstevel@tonic-gate default: 12430Sstevel@tonic-gate sub_usage(SHELP_BOOT, CMD_BOOT); 12440Sstevel@tonic-gate return (Z_USAGE); 12450Sstevel@tonic-gate } 12460Sstevel@tonic-gate } 12470Sstevel@tonic-gate if (argc > optind) { 12480Sstevel@tonic-gate sub_usage(SHELP_BOOT, CMD_BOOT); 12490Sstevel@tonic-gate return (Z_USAGE); 12500Sstevel@tonic-gate } 12510Sstevel@tonic-gate if (sanity_check(target_zone, CMD_BOOT, B_FALSE, B_FALSE) != Z_OK) 12520Sstevel@tonic-gate return (Z_ERR); 12530Sstevel@tonic-gate if (verify_details(CMD_BOOT) != Z_OK) 12540Sstevel@tonic-gate return (Z_ERR); 12550Sstevel@tonic-gate zarg.cmd = Z_BOOT; 12560Sstevel@tonic-gate if (call_zoneadmd(target_zone, &zarg) != 0) { 12570Sstevel@tonic-gate zerror(gettext("call to %s failed"), "zoneadmd"); 12580Sstevel@tonic-gate return (Z_ERR); 12590Sstevel@tonic-gate } 12600Sstevel@tonic-gate return (Z_OK); 12610Sstevel@tonic-gate } 12620Sstevel@tonic-gate 12630Sstevel@tonic-gate static void 12640Sstevel@tonic-gate fake_up_local_zone(zoneid_t zid, zone_entry_t *zeptr) 12650Sstevel@tonic-gate { 12660Sstevel@tonic-gate ssize_t result; 12670Sstevel@tonic-gate 12680Sstevel@tonic-gate zeptr->zid = zid; 12690Sstevel@tonic-gate /* 12700Sstevel@tonic-gate * Since we're looking up our own (non-global) zone name, 12710Sstevel@tonic-gate * we can be assured that it will succeed. 12720Sstevel@tonic-gate */ 12730Sstevel@tonic-gate result = getzonenamebyid(zid, zeptr->zname, sizeof (zeptr->zname)); 12740Sstevel@tonic-gate assert(result >= 0); 12750Sstevel@tonic-gate (void) strlcpy(zeptr->zroot, "/", sizeof (zeptr->zroot)); 12760Sstevel@tonic-gate zeptr->zstate_str = "running"; 12770Sstevel@tonic-gate } 12780Sstevel@tonic-gate 12790Sstevel@tonic-gate static int 12800Sstevel@tonic-gate list_func(int argc, char *argv[]) 12810Sstevel@tonic-gate { 12820Sstevel@tonic-gate zone_entry_t *zentp, zent; 1283766Scarlsonj int arg, retv; 12840Sstevel@tonic-gate boolean_t output = B_FALSE, verbose = B_FALSE, parsable = B_FALSE; 12850Sstevel@tonic-gate zone_state_t min_state = ZONE_STATE_RUNNING; 12860Sstevel@tonic-gate zoneid_t zone_id = getzoneid(); 12870Sstevel@tonic-gate 12880Sstevel@tonic-gate if (target_zone == NULL) { 12890Sstevel@tonic-gate /* all zones: default view to running but allow override */ 12900Sstevel@tonic-gate optind = 0; 12910Sstevel@tonic-gate while ((arg = getopt(argc, argv, "?cipv")) != EOF) { 12920Sstevel@tonic-gate switch (arg) { 12930Sstevel@tonic-gate case '?': 12940Sstevel@tonic-gate sub_usage(SHELP_LIST, CMD_LIST); 12950Sstevel@tonic-gate return (optopt == '?' ? Z_OK : Z_USAGE); 12960Sstevel@tonic-gate case 'c': 12970Sstevel@tonic-gate min_state = ZONE_STATE_CONFIGURED; 12980Sstevel@tonic-gate break; 12990Sstevel@tonic-gate case 'i': 13000Sstevel@tonic-gate min_state = ZONE_STATE_INSTALLED; 13010Sstevel@tonic-gate break; 13020Sstevel@tonic-gate case 'p': 13030Sstevel@tonic-gate parsable = B_TRUE; 13040Sstevel@tonic-gate break; 13050Sstevel@tonic-gate case 'v': 13060Sstevel@tonic-gate verbose = B_TRUE; 13070Sstevel@tonic-gate break; 13080Sstevel@tonic-gate default: 13090Sstevel@tonic-gate sub_usage(SHELP_LIST, CMD_LIST); 13100Sstevel@tonic-gate return (Z_USAGE); 13110Sstevel@tonic-gate } 13120Sstevel@tonic-gate } 13130Sstevel@tonic-gate if (parsable && verbose) { 13140Sstevel@tonic-gate zerror(gettext("%s -p and -v are mutually exclusive."), 13150Sstevel@tonic-gate cmd_to_str(CMD_LIST)); 13160Sstevel@tonic-gate return (Z_ERR); 13170Sstevel@tonic-gate } 13180Sstevel@tonic-gate if (zone_id == GLOBAL_ZONEID) { 1319766Scarlsonj retv = zone_print_list(min_state, verbose, parsable); 13200Sstevel@tonic-gate } else { 1321766Scarlsonj retv = Z_OK; 13220Sstevel@tonic-gate fake_up_local_zone(zone_id, &zent); 13230Sstevel@tonic-gate zone_print(&zent, verbose, parsable); 13240Sstevel@tonic-gate } 1325766Scarlsonj return (retv); 13260Sstevel@tonic-gate } 13270Sstevel@tonic-gate 13280Sstevel@tonic-gate /* 13290Sstevel@tonic-gate * Specific target zone: disallow -i/-c suboptions. 13300Sstevel@tonic-gate */ 13310Sstevel@tonic-gate optind = 0; 13320Sstevel@tonic-gate while ((arg = getopt(argc, argv, "?pv")) != EOF) { 13330Sstevel@tonic-gate switch (arg) { 13340Sstevel@tonic-gate case '?': 13350Sstevel@tonic-gate sub_usage(SHELP_LIST, CMD_LIST); 13360Sstevel@tonic-gate return (optopt == '?' ? Z_OK : Z_USAGE); 13370Sstevel@tonic-gate case 'p': 13380Sstevel@tonic-gate parsable = B_TRUE; 13390Sstevel@tonic-gate break; 13400Sstevel@tonic-gate case 'v': 13410Sstevel@tonic-gate verbose = B_TRUE; 13420Sstevel@tonic-gate break; 13430Sstevel@tonic-gate default: 13440Sstevel@tonic-gate sub_usage(SHELP_LIST, CMD_LIST); 13450Sstevel@tonic-gate return (Z_USAGE); 13460Sstevel@tonic-gate } 13470Sstevel@tonic-gate } 13480Sstevel@tonic-gate if (parsable && verbose) { 13490Sstevel@tonic-gate zerror(gettext("%s -p and -v are mutually exclusive."), 13500Sstevel@tonic-gate cmd_to_str(CMD_LIST)); 13510Sstevel@tonic-gate return (Z_ERR); 13520Sstevel@tonic-gate } 13530Sstevel@tonic-gate if (argc > optind) { 13540Sstevel@tonic-gate sub_usage(SHELP_LIST, CMD_LIST); 13550Sstevel@tonic-gate return (Z_USAGE); 13560Sstevel@tonic-gate } 13570Sstevel@tonic-gate if (zone_id != GLOBAL_ZONEID) { 13580Sstevel@tonic-gate fake_up_local_zone(zone_id, &zent); 13590Sstevel@tonic-gate /* 13600Sstevel@tonic-gate * main() will issue a Z_NO_ZONE error if it cannot get an 13610Sstevel@tonic-gate * id for target_zone, which in a non-global zone should 13620Sstevel@tonic-gate * happen for any zone name except `zonename`. Thus we 13630Sstevel@tonic-gate * assert() that here but don't otherwise check. 13640Sstevel@tonic-gate */ 13650Sstevel@tonic-gate assert(strcmp(zent.zname, target_zone) == 0); 13660Sstevel@tonic-gate zone_print(&zent, verbose, parsable); 13670Sstevel@tonic-gate output = B_TRUE; 13680Sstevel@tonic-gate } else if ((zentp = lookup_running_zone(target_zone)) != NULL) { 13690Sstevel@tonic-gate zone_print(zentp, verbose, parsable); 13700Sstevel@tonic-gate output = B_TRUE; 1371766Scarlsonj } else if (lookup_zone_info(target_zone, ZONE_ID_UNDEFINED, 1372766Scarlsonj &zent) == Z_OK) { 13730Sstevel@tonic-gate zone_print(&zent, verbose, parsable); 13740Sstevel@tonic-gate output = B_TRUE; 13750Sstevel@tonic-gate } 13760Sstevel@tonic-gate return (output ? Z_OK : Z_ERR); 13770Sstevel@tonic-gate } 13780Sstevel@tonic-gate 13790Sstevel@tonic-gate static void 13800Sstevel@tonic-gate sigterm(int sig) 13810Sstevel@tonic-gate { 13820Sstevel@tonic-gate /* 13830Sstevel@tonic-gate * Ignore SIG{INT,TERM}, so we don't end up in an infinite loop, 13840Sstevel@tonic-gate * then propagate the signal to our process group. 13850Sstevel@tonic-gate */ 13860Sstevel@tonic-gate (void) sigset(SIGINT, SIG_IGN); 13870Sstevel@tonic-gate (void) sigset(SIGTERM, SIG_IGN); 13880Sstevel@tonic-gate (void) kill(0, sig); 13890Sstevel@tonic-gate child_killed = B_TRUE; 13900Sstevel@tonic-gate } 13910Sstevel@tonic-gate 13920Sstevel@tonic-gate static int 13930Sstevel@tonic-gate do_subproc(char *cmdbuf) 13940Sstevel@tonic-gate { 13950Sstevel@tonic-gate char inbuf[1024]; /* arbitrary large amount */ 13960Sstevel@tonic-gate FILE *file; 13970Sstevel@tonic-gate 13980Sstevel@tonic-gate child_killed = B_FALSE; 13990Sstevel@tonic-gate /* 14000Sstevel@tonic-gate * We use popen(3c) to launch child processes for [un]install; 14010Sstevel@tonic-gate * this library call does not return a PID, so we have to kill 14020Sstevel@tonic-gate * the whole process group. To avoid killing our parent, we 14030Sstevel@tonic-gate * become a process group leader here. But doing so can wreak 14040Sstevel@tonic-gate * havoc with reading from stdin when launched by a non-job-control 14050Sstevel@tonic-gate * shell, so we close stdin and reopen it as /dev/null first. 14060Sstevel@tonic-gate */ 14070Sstevel@tonic-gate (void) close(STDIN_FILENO); 14080Sstevel@tonic-gate (void) open("/dev/null", O_RDONLY); 14090Sstevel@tonic-gate (void) setpgid(0, 0); 14100Sstevel@tonic-gate (void) sigset(SIGINT, sigterm); 14110Sstevel@tonic-gate (void) sigset(SIGTERM, sigterm); 14120Sstevel@tonic-gate file = popen(cmdbuf, "r"); 14130Sstevel@tonic-gate for (;;) { 14140Sstevel@tonic-gate if (child_killed || fgets(inbuf, sizeof (inbuf), file) == NULL) 14150Sstevel@tonic-gate break; 14160Sstevel@tonic-gate (void) fputs(inbuf, stdout); 14170Sstevel@tonic-gate } 14180Sstevel@tonic-gate (void) sigset(SIGINT, SIG_DFL); 14190Sstevel@tonic-gate (void) sigset(SIGTERM, SIG_DFL); 14200Sstevel@tonic-gate return (pclose(file)); 14210Sstevel@tonic-gate } 14220Sstevel@tonic-gate 14230Sstevel@tonic-gate static int 14240Sstevel@tonic-gate subproc_status(const char *cmd, int status) 14250Sstevel@tonic-gate { 14260Sstevel@tonic-gate if (WIFEXITED(status)) { 14270Sstevel@tonic-gate int exit_code = WEXITSTATUS(status); 14280Sstevel@tonic-gate 14290Sstevel@tonic-gate if (exit_code == 0) 14300Sstevel@tonic-gate return (Z_OK); 14310Sstevel@tonic-gate zerror(gettext("'%s' failed with exit code %d."), cmd, 14320Sstevel@tonic-gate exit_code); 14330Sstevel@tonic-gate } else if (WIFSIGNALED(status)) { 14340Sstevel@tonic-gate int signal = WTERMSIG(status); 14350Sstevel@tonic-gate char sigstr[SIG2STR_MAX]; 14360Sstevel@tonic-gate 14370Sstevel@tonic-gate if (sig2str(signal, sigstr) == 0) { 14380Sstevel@tonic-gate zerror(gettext("'%s' terminated by signal SIG%s."), cmd, 14390Sstevel@tonic-gate sigstr); 14400Sstevel@tonic-gate } else { 14410Sstevel@tonic-gate zerror(gettext("'%s' terminated by an unknown signal."), 14420Sstevel@tonic-gate cmd); 14430Sstevel@tonic-gate } 14440Sstevel@tonic-gate } else { 14450Sstevel@tonic-gate zerror(gettext("'%s' failed for unknown reasons."), cmd); 14460Sstevel@tonic-gate } 14470Sstevel@tonic-gate return (Z_ERR); 14480Sstevel@tonic-gate } 14490Sstevel@tonic-gate 14500Sstevel@tonic-gate /* 14510Sstevel@tonic-gate * Various sanity checks; make sure: 14520Sstevel@tonic-gate * 1. We're in the global zone. 14530Sstevel@tonic-gate * 2. The calling user has sufficient privilege. 14540Sstevel@tonic-gate * 3. The target zone is neither the global zone nor anything starting with 14550Sstevel@tonic-gate * "SUNW". 14560Sstevel@tonic-gate * 4a. If we're looking for a 'not running' (i.e., configured or installed) 14570Sstevel@tonic-gate * zone, the name service knows about it. 14580Sstevel@tonic-gate * 4b. For some operations which expect a zone not to be running, that it is 14590Sstevel@tonic-gate * not already running (or ready). 14600Sstevel@tonic-gate */ 14610Sstevel@tonic-gate static int 14620Sstevel@tonic-gate sanity_check(char *zone, int cmd_num, boolean_t running, 14630Sstevel@tonic-gate boolean_t unsafe_when_running) 14640Sstevel@tonic-gate { 14650Sstevel@tonic-gate zone_entry_t *zent; 14660Sstevel@tonic-gate priv_set_t *privset; 14670Sstevel@tonic-gate zone_state_t state; 1468766Scarlsonj char kernzone[ZONENAME_MAX]; 1469766Scarlsonj FILE *fp; 14700Sstevel@tonic-gate 14710Sstevel@tonic-gate if (getzoneid() != GLOBAL_ZONEID) { 14720Sstevel@tonic-gate zerror(gettext("must be in the global zone to %s a zone."), 14730Sstevel@tonic-gate cmd_to_str(cmd_num)); 14740Sstevel@tonic-gate return (Z_ERR); 14750Sstevel@tonic-gate } 14760Sstevel@tonic-gate 14770Sstevel@tonic-gate if ((privset = priv_allocset()) == NULL) { 14780Sstevel@tonic-gate zerror(gettext("%s failed"), "priv_allocset"); 14790Sstevel@tonic-gate return (Z_ERR); 14800Sstevel@tonic-gate } 14810Sstevel@tonic-gate 14820Sstevel@tonic-gate if (getppriv(PRIV_EFFECTIVE, privset) != 0) { 14830Sstevel@tonic-gate zerror(gettext("%s failed"), "getppriv"); 14840Sstevel@tonic-gate priv_freeset(privset); 14850Sstevel@tonic-gate return (Z_ERR); 14860Sstevel@tonic-gate } 14870Sstevel@tonic-gate 14880Sstevel@tonic-gate if (priv_isfullset(privset) == B_FALSE) { 14890Sstevel@tonic-gate zerror(gettext("only a privileged user may %s a zone."), 14900Sstevel@tonic-gate cmd_to_str(cmd_num)); 14910Sstevel@tonic-gate priv_freeset(privset); 14920Sstevel@tonic-gate return (Z_ERR); 14930Sstevel@tonic-gate } 14940Sstevel@tonic-gate priv_freeset(privset); 14950Sstevel@tonic-gate 14960Sstevel@tonic-gate if (zone == NULL) { 14970Sstevel@tonic-gate zerror(gettext("no zone specified")); 14980Sstevel@tonic-gate return (Z_ERR); 14990Sstevel@tonic-gate } 15000Sstevel@tonic-gate 15010Sstevel@tonic-gate if (strcmp(zone, GLOBAL_ZONENAME) == 0) { 15020Sstevel@tonic-gate zerror(gettext("%s operation is invalid for the global zone."), 15030Sstevel@tonic-gate cmd_to_str(cmd_num)); 15040Sstevel@tonic-gate return (Z_ERR); 15050Sstevel@tonic-gate } 15060Sstevel@tonic-gate 15070Sstevel@tonic-gate if (strncmp(zone, "SUNW", 4) == 0) { 15080Sstevel@tonic-gate zerror(gettext("%s operation is invalid for zones starting " 15090Sstevel@tonic-gate "with SUNW."), cmd_to_str(cmd_num)); 15100Sstevel@tonic-gate return (Z_ERR); 15110Sstevel@tonic-gate } 15120Sstevel@tonic-gate 1513766Scarlsonj if (!zonecfg_in_alt_root()) { 1514766Scarlsonj zent = lookup_running_zone(zone); 1515766Scarlsonj } else if ((fp = zonecfg_open_scratch("", B_FALSE)) == NULL) { 1516766Scarlsonj zent = NULL; 1517766Scarlsonj } else { 1518766Scarlsonj if (zonecfg_find_scratch(fp, zone, zonecfg_get_root(), 1519766Scarlsonj kernzone, sizeof (kernzone)) == 0) 1520766Scarlsonj zent = lookup_running_zone(kernzone); 1521766Scarlsonj else 1522766Scarlsonj zent = NULL; 1523766Scarlsonj zonecfg_close_scratch(fp); 1524766Scarlsonj } 1525766Scarlsonj 15260Sstevel@tonic-gate /* 15270Sstevel@tonic-gate * Look up from the kernel for 'running' zones. 15280Sstevel@tonic-gate */ 15290Sstevel@tonic-gate if (running) { 15300Sstevel@tonic-gate if (zent == NULL) { 15310Sstevel@tonic-gate zerror(gettext("not running")); 15320Sstevel@tonic-gate return (Z_ERR); 15330Sstevel@tonic-gate } 15340Sstevel@tonic-gate } else { 15350Sstevel@tonic-gate int err; 15360Sstevel@tonic-gate 15370Sstevel@tonic-gate if (unsafe_when_running && zent != NULL) { 15380Sstevel@tonic-gate /* check whether the zone is ready or running */ 15390Sstevel@tonic-gate if ((err = zone_get_state(zent->zname, 15400Sstevel@tonic-gate &zent->zstate_num)) != Z_OK) { 15410Sstevel@tonic-gate errno = err; 15420Sstevel@tonic-gate zperror2(zent->zname, 15430Sstevel@tonic-gate gettext("could not get state")); 15440Sstevel@tonic-gate /* can't tell, so hedge */ 15450Sstevel@tonic-gate zent->zstate_str = "ready/running"; 15460Sstevel@tonic-gate } else { 15470Sstevel@tonic-gate zent->zstate_str = 15480Sstevel@tonic-gate zone_state_str(zent->zstate_num); 15490Sstevel@tonic-gate } 15500Sstevel@tonic-gate zerror(gettext("%s operation is invalid for %s zones."), 15510Sstevel@tonic-gate cmd_to_str(cmd_num), zent->zstate_str); 15520Sstevel@tonic-gate return (Z_ERR); 15530Sstevel@tonic-gate } 15540Sstevel@tonic-gate if ((err = zone_get_state(zone, &state)) != Z_OK) { 15550Sstevel@tonic-gate errno = err; 15560Sstevel@tonic-gate zperror2(zone, gettext("could not get state")); 15570Sstevel@tonic-gate return (Z_ERR); 15580Sstevel@tonic-gate } 15590Sstevel@tonic-gate switch (cmd_num) { 15600Sstevel@tonic-gate case CMD_UNINSTALL: 15610Sstevel@tonic-gate if (state == ZONE_STATE_CONFIGURED) { 15620Sstevel@tonic-gate zerror(gettext("is already in state '%s'."), 15630Sstevel@tonic-gate zone_state_str(ZONE_STATE_CONFIGURED)); 15640Sstevel@tonic-gate return (Z_ERR); 15650Sstevel@tonic-gate } 15660Sstevel@tonic-gate break; 15670Sstevel@tonic-gate case CMD_INSTALL: 15680Sstevel@tonic-gate if (state == ZONE_STATE_INSTALLED) { 15690Sstevel@tonic-gate zerror(gettext("is already %s."), 15700Sstevel@tonic-gate zone_state_str(ZONE_STATE_INSTALLED)); 15710Sstevel@tonic-gate return (Z_ERR); 15720Sstevel@tonic-gate } else if (state == ZONE_STATE_INCOMPLETE) { 15730Sstevel@tonic-gate zerror(gettext("zone is %s; %s required."), 15740Sstevel@tonic-gate zone_state_str(ZONE_STATE_INCOMPLETE), 15750Sstevel@tonic-gate cmd_to_str(CMD_UNINSTALL)); 15760Sstevel@tonic-gate return (Z_ERR); 15770Sstevel@tonic-gate } 15780Sstevel@tonic-gate break; 15790Sstevel@tonic-gate case CMD_READY: 15800Sstevel@tonic-gate case CMD_BOOT: 1581766Scarlsonj case CMD_MOUNT: 15820Sstevel@tonic-gate if (state < ZONE_STATE_INSTALLED) { 15830Sstevel@tonic-gate zerror(gettext("must be %s before %s."), 15840Sstevel@tonic-gate zone_state_str(ZONE_STATE_INSTALLED), 15850Sstevel@tonic-gate cmd_to_str(cmd_num)); 15860Sstevel@tonic-gate return (Z_ERR); 15870Sstevel@tonic-gate } 15880Sstevel@tonic-gate break; 15890Sstevel@tonic-gate case CMD_VERIFY: 15900Sstevel@tonic-gate if (state == ZONE_STATE_INCOMPLETE) { 15910Sstevel@tonic-gate zerror(gettext("zone is %s; %s required."), 15920Sstevel@tonic-gate zone_state_str(ZONE_STATE_INCOMPLETE), 15930Sstevel@tonic-gate cmd_to_str(CMD_UNINSTALL)); 15940Sstevel@tonic-gate return (Z_ERR); 15950Sstevel@tonic-gate } 15960Sstevel@tonic-gate break; 1597766Scarlsonj case CMD_UNMOUNT: 1598766Scarlsonj if (state != ZONE_STATE_MOUNTED) { 1599766Scarlsonj zerror(gettext("must be %s before %s."), 1600766Scarlsonj zone_state_str(ZONE_STATE_MOUNTED), 1601766Scarlsonj cmd_to_str(cmd_num)); 1602766Scarlsonj return (Z_ERR); 1603766Scarlsonj } 1604766Scarlsonj break; 16050Sstevel@tonic-gate } 16060Sstevel@tonic-gate } 16070Sstevel@tonic-gate return (Z_OK); 16080Sstevel@tonic-gate } 16090Sstevel@tonic-gate 16100Sstevel@tonic-gate static int 16110Sstevel@tonic-gate halt_func(int argc, char *argv[]) 16120Sstevel@tonic-gate { 16130Sstevel@tonic-gate zone_cmd_arg_t zarg; 16140Sstevel@tonic-gate int arg; 16150Sstevel@tonic-gate 1616766Scarlsonj if (zonecfg_in_alt_root()) { 1617766Scarlsonj zerror(gettext("cannot halt zone in alternate root")); 1618766Scarlsonj return (Z_ERR); 1619766Scarlsonj } 1620766Scarlsonj 16210Sstevel@tonic-gate optind = 0; 16220Sstevel@tonic-gate if ((arg = getopt(argc, argv, "?")) != EOF) { 16230Sstevel@tonic-gate switch (arg) { 16240Sstevel@tonic-gate case '?': 16250Sstevel@tonic-gate sub_usage(SHELP_HALT, CMD_HALT); 16260Sstevel@tonic-gate return (optopt == '?' ? Z_OK : Z_USAGE); 16270Sstevel@tonic-gate default: 16280Sstevel@tonic-gate sub_usage(SHELP_HALT, CMD_HALT); 16290Sstevel@tonic-gate return (Z_USAGE); 16300Sstevel@tonic-gate } 16310Sstevel@tonic-gate } 16320Sstevel@tonic-gate if (argc > optind) { 16330Sstevel@tonic-gate sub_usage(SHELP_HALT, CMD_HALT); 16340Sstevel@tonic-gate return (Z_USAGE); 16350Sstevel@tonic-gate } 16360Sstevel@tonic-gate /* 16370Sstevel@tonic-gate * zoneadmd should be the one to decide whether or not to proceed, 16380Sstevel@tonic-gate * so even though it seems that the fourth parameter below should 16390Sstevel@tonic-gate * perhaps be B_TRUE, it really shouldn't be. 16400Sstevel@tonic-gate */ 16410Sstevel@tonic-gate if (sanity_check(target_zone, CMD_HALT, B_FALSE, B_FALSE) != Z_OK) 16420Sstevel@tonic-gate return (Z_ERR); 16430Sstevel@tonic-gate 16440Sstevel@tonic-gate zarg.cmd = Z_HALT; 16450Sstevel@tonic-gate return ((call_zoneadmd(target_zone, &zarg) == 0) ? Z_OK : Z_ERR); 16460Sstevel@tonic-gate } 16470Sstevel@tonic-gate 16480Sstevel@tonic-gate static int 16490Sstevel@tonic-gate reboot_func(int argc, char *argv[]) 16500Sstevel@tonic-gate { 16510Sstevel@tonic-gate zone_cmd_arg_t zarg; 16520Sstevel@tonic-gate int arg; 16530Sstevel@tonic-gate 1654766Scarlsonj if (zonecfg_in_alt_root()) { 1655766Scarlsonj zerror(gettext("cannot reboot zone in alternate root")); 1656766Scarlsonj return (Z_ERR); 1657766Scarlsonj } 1658766Scarlsonj 16590Sstevel@tonic-gate optind = 0; 16600Sstevel@tonic-gate if ((arg = getopt(argc, argv, "?")) != EOF) { 16610Sstevel@tonic-gate switch (arg) { 16620Sstevel@tonic-gate case '?': 16630Sstevel@tonic-gate sub_usage(SHELP_REBOOT, CMD_REBOOT); 16640Sstevel@tonic-gate return (optopt == '?' ? Z_OK : Z_USAGE); 16650Sstevel@tonic-gate default: 16660Sstevel@tonic-gate sub_usage(SHELP_REBOOT, CMD_REBOOT); 16670Sstevel@tonic-gate return (Z_USAGE); 16680Sstevel@tonic-gate } 16690Sstevel@tonic-gate } 16700Sstevel@tonic-gate if (argc > 0) { 16710Sstevel@tonic-gate sub_usage(SHELP_REBOOT, CMD_REBOOT); 16720Sstevel@tonic-gate return (Z_USAGE); 16730Sstevel@tonic-gate } 16740Sstevel@tonic-gate /* 16750Sstevel@tonic-gate * zoneadmd should be the one to decide whether or not to proceed, 16760Sstevel@tonic-gate * so even though it seems that the fourth parameter below should 16770Sstevel@tonic-gate * perhaps be B_TRUE, it really shouldn't be. 16780Sstevel@tonic-gate */ 16790Sstevel@tonic-gate if (sanity_check(target_zone, CMD_REBOOT, B_TRUE, B_FALSE) != Z_OK) 16800Sstevel@tonic-gate return (Z_ERR); 16810Sstevel@tonic-gate 16820Sstevel@tonic-gate zarg.cmd = Z_REBOOT; 16830Sstevel@tonic-gate return ((call_zoneadmd(target_zone, &zarg) == 0) ? Z_OK : Z_ERR); 16840Sstevel@tonic-gate } 16850Sstevel@tonic-gate 16860Sstevel@tonic-gate static int 16870Sstevel@tonic-gate verify_rctls(zone_dochandle_t handle) 16880Sstevel@tonic-gate { 16890Sstevel@tonic-gate struct zone_rctltab rctltab; 16900Sstevel@tonic-gate size_t rbs = rctlblk_size(); 16910Sstevel@tonic-gate rctlblk_t *rctlblk; 16920Sstevel@tonic-gate int error = Z_INVAL; 16930Sstevel@tonic-gate 16940Sstevel@tonic-gate if ((rctlblk = malloc(rbs)) == NULL) { 16950Sstevel@tonic-gate zerror(gettext("failed to allocate %lu bytes: %s"), rbs, 16960Sstevel@tonic-gate strerror(errno)); 16970Sstevel@tonic-gate return (Z_NOMEM); 16980Sstevel@tonic-gate } 16990Sstevel@tonic-gate 17000Sstevel@tonic-gate if (zonecfg_setrctlent(handle) != Z_OK) { 17010Sstevel@tonic-gate zerror(gettext("zonecfg_setrctlent failed")); 17020Sstevel@tonic-gate free(rctlblk); 17030Sstevel@tonic-gate return (error); 17040Sstevel@tonic-gate } 17050Sstevel@tonic-gate 17060Sstevel@tonic-gate rctltab.zone_rctl_valptr = NULL; 17070Sstevel@tonic-gate while (zonecfg_getrctlent(handle, &rctltab) == Z_OK) { 17080Sstevel@tonic-gate struct zone_rctlvaltab *rctlval; 17090Sstevel@tonic-gate const char *name = rctltab.zone_rctl_name; 17100Sstevel@tonic-gate 17110Sstevel@tonic-gate if (!zonecfg_is_rctl(name)) { 17120Sstevel@tonic-gate zerror(gettext("WARNING: Ignoring unrecognized rctl " 17130Sstevel@tonic-gate "'%s'."), name); 17140Sstevel@tonic-gate zonecfg_free_rctl_value_list(rctltab.zone_rctl_valptr); 17150Sstevel@tonic-gate rctltab.zone_rctl_valptr = NULL; 17160Sstevel@tonic-gate continue; 17170Sstevel@tonic-gate } 17180Sstevel@tonic-gate 17190Sstevel@tonic-gate for (rctlval = rctltab.zone_rctl_valptr; rctlval != NULL; 17200Sstevel@tonic-gate rctlval = rctlval->zone_rctlval_next) { 17210Sstevel@tonic-gate if (zonecfg_construct_rctlblk(rctlval, rctlblk) 17220Sstevel@tonic-gate != Z_OK) { 17230Sstevel@tonic-gate zerror(gettext("invalid rctl value: " 17240Sstevel@tonic-gate "(priv=%s,limit=%s,action%s)"), 17250Sstevel@tonic-gate rctlval->zone_rctlval_priv, 17260Sstevel@tonic-gate rctlval->zone_rctlval_limit, 17270Sstevel@tonic-gate rctlval->zone_rctlval_action); 17280Sstevel@tonic-gate goto out; 17290Sstevel@tonic-gate } 17300Sstevel@tonic-gate if (!zonecfg_valid_rctl(name, rctlblk)) { 17310Sstevel@tonic-gate zerror(gettext("(priv=%s,limit=%s,action=%s) " 17320Sstevel@tonic-gate "is not a valid value for rctl '%s'"), 17330Sstevel@tonic-gate rctlval->zone_rctlval_priv, 17340Sstevel@tonic-gate rctlval->zone_rctlval_limit, 17350Sstevel@tonic-gate rctlval->zone_rctlval_action, 17360Sstevel@tonic-gate name); 17370Sstevel@tonic-gate goto out; 17380Sstevel@tonic-gate } 17390Sstevel@tonic-gate } 17400Sstevel@tonic-gate zonecfg_free_rctl_value_list(rctltab.zone_rctl_valptr); 17410Sstevel@tonic-gate } 17420Sstevel@tonic-gate rctltab.zone_rctl_valptr = NULL; 17430Sstevel@tonic-gate error = Z_OK; 17440Sstevel@tonic-gate out: 17450Sstevel@tonic-gate zonecfg_free_rctl_value_list(rctltab.zone_rctl_valptr); 17460Sstevel@tonic-gate (void) zonecfg_endrctlent(handle); 17470Sstevel@tonic-gate free(rctlblk); 17480Sstevel@tonic-gate return (error); 17490Sstevel@tonic-gate } 17500Sstevel@tonic-gate 17510Sstevel@tonic-gate static int 17520Sstevel@tonic-gate verify_pool(zone_dochandle_t handle) 17530Sstevel@tonic-gate { 17540Sstevel@tonic-gate char poolname[MAXPATHLEN]; 17550Sstevel@tonic-gate pool_conf_t *poolconf; 17560Sstevel@tonic-gate pool_t *pool; 17570Sstevel@tonic-gate int status; 17580Sstevel@tonic-gate int error; 17590Sstevel@tonic-gate 17600Sstevel@tonic-gate /* 17610Sstevel@tonic-gate * This ends up being very similar to the check done in zoneadmd. 17620Sstevel@tonic-gate */ 17630Sstevel@tonic-gate error = zonecfg_get_pool(handle, poolname, sizeof (poolname)); 17640Sstevel@tonic-gate if (error == Z_NO_ENTRY || (error == Z_OK && strlen(poolname) == 0)) { 17650Sstevel@tonic-gate /* 17660Sstevel@tonic-gate * No pool specified. 17670Sstevel@tonic-gate */ 17680Sstevel@tonic-gate return (0); 17690Sstevel@tonic-gate } 17700Sstevel@tonic-gate if (error != Z_OK) { 17710Sstevel@tonic-gate zperror(gettext("Unable to retrieve pool name from " 17720Sstevel@tonic-gate "configuration"), B_TRUE); 17730Sstevel@tonic-gate return (error); 17740Sstevel@tonic-gate } 17750Sstevel@tonic-gate /* 17760Sstevel@tonic-gate * Don't do anything if pools aren't enabled. 17770Sstevel@tonic-gate */ 17780Sstevel@tonic-gate if (pool_get_status(&status) != PO_SUCCESS || status != POOL_ENABLED) { 17790Sstevel@tonic-gate zerror(gettext("WARNING: pools facility not active; " 17800Sstevel@tonic-gate "zone will not be bound to pool '%s'."), poolname); 17810Sstevel@tonic-gate return (Z_OK); 17820Sstevel@tonic-gate } 17830Sstevel@tonic-gate /* 17840Sstevel@tonic-gate * Try to provide a sane error message if the requested pool doesn't 17850Sstevel@tonic-gate * exist. It isn't clear that pools-related failures should 17860Sstevel@tonic-gate * necessarily translate to a failure to verify the zone configuration, 17870Sstevel@tonic-gate * hence they are not considered errors. 17880Sstevel@tonic-gate */ 17890Sstevel@tonic-gate if ((poolconf = pool_conf_alloc()) == NULL) { 17900Sstevel@tonic-gate zerror(gettext("WARNING: pool_conf_alloc failed; " 17910Sstevel@tonic-gate "using default pool")); 17920Sstevel@tonic-gate return (Z_OK); 17930Sstevel@tonic-gate } 17940Sstevel@tonic-gate if (pool_conf_open(poolconf, pool_dynamic_location(), PO_RDONLY) != 17950Sstevel@tonic-gate PO_SUCCESS) { 17960Sstevel@tonic-gate zerror(gettext("WARNING: pool_conf_open failed; " 17970Sstevel@tonic-gate "using default pool")); 17980Sstevel@tonic-gate pool_conf_free(poolconf); 17990Sstevel@tonic-gate return (Z_OK); 18000Sstevel@tonic-gate } 18010Sstevel@tonic-gate pool = pool_get_pool(poolconf, poolname); 18020Sstevel@tonic-gate (void) pool_conf_close(poolconf); 18030Sstevel@tonic-gate pool_conf_free(poolconf); 18040Sstevel@tonic-gate if (pool == NULL) { 18050Sstevel@tonic-gate zerror(gettext("WARNING: pool '%s' not found. " 18060Sstevel@tonic-gate "using default pool"), poolname); 18070Sstevel@tonic-gate } 18080Sstevel@tonic-gate 18090Sstevel@tonic-gate return (Z_OK); 18100Sstevel@tonic-gate } 18110Sstevel@tonic-gate 18120Sstevel@tonic-gate static int 18130Sstevel@tonic-gate verify_filesystems(zone_dochandle_t handle) 18140Sstevel@tonic-gate { 18150Sstevel@tonic-gate int return_code = Z_OK; 18160Sstevel@tonic-gate struct zone_fstab fstab; 18170Sstevel@tonic-gate char cmdbuf[MAXPATHLEN]; 18180Sstevel@tonic-gate struct stat st; 18190Sstevel@tonic-gate 18200Sstevel@tonic-gate /* 18210Sstevel@tonic-gate * No need to verify inherit-pkg-dir fs types, as their type is 18220Sstevel@tonic-gate * implicitly lofs, which is known. Therefore, the types are only 18230Sstevel@tonic-gate * verified for regular filesystems below. 18240Sstevel@tonic-gate * 18250Sstevel@tonic-gate * Since the actual mount point is not known until the dependent mounts 18260Sstevel@tonic-gate * are performed, we don't attempt any path validation here: that will 18270Sstevel@tonic-gate * happen later when zoneadmd actually does the mounts. 18280Sstevel@tonic-gate */ 18290Sstevel@tonic-gate if (zonecfg_setfsent(handle) != Z_OK) { 18300Sstevel@tonic-gate (void) fprintf(stderr, gettext("cannot verify file-systems: " 18310Sstevel@tonic-gate "unable to enumerate mounts\n")); 18320Sstevel@tonic-gate return (Z_ERR); 18330Sstevel@tonic-gate } 18340Sstevel@tonic-gate while (zonecfg_getfsent(handle, &fstab) == Z_OK) { 18350Sstevel@tonic-gate if (!zonecfg_valid_fs_type(fstab.zone_fs_type)) { 18360Sstevel@tonic-gate (void) fprintf(stderr, gettext("cannot verify fs %s: " 18370Sstevel@tonic-gate "type %s is not allowed.\n"), fstab.zone_fs_dir, 18380Sstevel@tonic-gate fstab.zone_fs_type); 18390Sstevel@tonic-gate return_code = Z_ERR; 18400Sstevel@tonic-gate goto next_fs; 18410Sstevel@tonic-gate } 18420Sstevel@tonic-gate /* 18430Sstevel@tonic-gate * Verify /usr/lib/fs/<fstype>/mount exists. 18440Sstevel@tonic-gate */ 18450Sstevel@tonic-gate if (snprintf(cmdbuf, sizeof (cmdbuf), "/usr/lib/fs/%s/mount", 18460Sstevel@tonic-gate fstab.zone_fs_type) > sizeof (cmdbuf)) { 18470Sstevel@tonic-gate (void) fprintf(stderr, gettext("cannot verify fs %s: " 18480Sstevel@tonic-gate "type %s is too long.\n"), fstab.zone_fs_dir, 18490Sstevel@tonic-gate fstab.zone_fs_type); 18500Sstevel@tonic-gate return_code = Z_ERR; 18510Sstevel@tonic-gate goto next_fs; 18520Sstevel@tonic-gate } 18530Sstevel@tonic-gate if (stat(cmdbuf, &st) != 0) { 18540Sstevel@tonic-gate (void) fprintf(stderr, gettext("cannot verify fs %s: " 18550Sstevel@tonic-gate "can't access %s: %s\n"), fstab.zone_fs_dir, 18560Sstevel@tonic-gate cmdbuf, strerror(errno)); 18570Sstevel@tonic-gate return_code = Z_ERR; 18580Sstevel@tonic-gate goto next_fs; 18590Sstevel@tonic-gate } 18600Sstevel@tonic-gate if (!S_ISREG(st.st_mode)) { 18610Sstevel@tonic-gate (void) fprintf(stderr, gettext("cannot verify fs %s: " 18620Sstevel@tonic-gate "%s is not a regular file\n"), fstab.zone_fs_dir, 18630Sstevel@tonic-gate cmdbuf); 18640Sstevel@tonic-gate return_code = Z_ERR; 18650Sstevel@tonic-gate goto next_fs; 18660Sstevel@tonic-gate } 18670Sstevel@tonic-gate /* 18680Sstevel@tonic-gate * Verify /usr/lib/fs/<fstype>/fsck exists iff zone_fs_raw is 18690Sstevel@tonic-gate * set. 18700Sstevel@tonic-gate */ 18710Sstevel@tonic-gate if (snprintf(cmdbuf, sizeof (cmdbuf), "/usr/lib/fs/%s/fsck", 18720Sstevel@tonic-gate fstab.zone_fs_type) > sizeof (cmdbuf)) { 18730Sstevel@tonic-gate (void) fprintf(stderr, gettext("cannot verify fs %s: " 18740Sstevel@tonic-gate "type %s is too long.\n"), fstab.zone_fs_dir, 18750Sstevel@tonic-gate fstab.zone_fs_type); 18760Sstevel@tonic-gate return_code = Z_ERR; 18770Sstevel@tonic-gate goto next_fs; 18780Sstevel@tonic-gate } 18790Sstevel@tonic-gate if (fstab.zone_fs_raw[0] == '\0' && stat(cmdbuf, &st) == 0) { 18800Sstevel@tonic-gate (void) fprintf(stderr, gettext("cannot verify fs %s: " 18810Sstevel@tonic-gate "must specify 'raw' device for %s file-systems\n"), 18820Sstevel@tonic-gate fstab.zone_fs_dir, fstab.zone_fs_type); 18830Sstevel@tonic-gate return_code = Z_ERR; 18840Sstevel@tonic-gate goto next_fs; 18850Sstevel@tonic-gate } 18860Sstevel@tonic-gate if (fstab.zone_fs_raw[0] != '\0' && 18870Sstevel@tonic-gate (stat(cmdbuf, &st) != 0 || !S_ISREG(st.st_mode))) { 18880Sstevel@tonic-gate (void) fprintf(stderr, gettext("cannot verify fs %s: " 18890Sstevel@tonic-gate "'raw' device specified but " 18900Sstevel@tonic-gate "no fsck executable exists for %s\n"), 18910Sstevel@tonic-gate fstab.zone_fs_dir, fstab.zone_fs_type); 18920Sstevel@tonic-gate return_code = Z_ERR; 18930Sstevel@tonic-gate goto next_fs; 18940Sstevel@tonic-gate } 18950Sstevel@tonic-gate next_fs: 18960Sstevel@tonic-gate zonecfg_free_fs_option_list(fstab.zone_fs_options); 18970Sstevel@tonic-gate } 18980Sstevel@tonic-gate (void) zonecfg_endfsent(handle); 18990Sstevel@tonic-gate 19000Sstevel@tonic-gate return (return_code); 19010Sstevel@tonic-gate } 19020Sstevel@tonic-gate 1903*789Sahrens const char *current_dataset; 1904*789Sahrens 1905*789Sahrens /* 1906*789Sahrens * Custom error handler for errors incurred as part of the checks below. We 1907*789Sahrens * want to trim off the leading 'cannot open ...' to create a better error 1908*789Sahrens * message. The only other way this can fail is if we fail to set the 'zoned' 1909*789Sahrens * property. In this case we just pass the error on verbatim. 1910*789Sahrens */ 1911*789Sahrens static void 1912*789Sahrens zfs_error_handler(const char *fmt, va_list ap) 1913*789Sahrens { 1914*789Sahrens char buf[1024]; 1915*789Sahrens 1916*789Sahrens (void) vsnprintf(buf, sizeof (buf), fmt, ap); 1917*789Sahrens 1918*789Sahrens if (strncmp(gettext("cannot open "), buf, 1919*789Sahrens strlen(gettext("cannot open "))) == 0) 1920*789Sahrens (void) fprintf(stderr, gettext("cannot verify zfs " 1921*789Sahrens "dataset %s%s\n"), current_dataset, strchr(buf, ':')); 1922*789Sahrens else 1923*789Sahrens (void) fprintf(stderr, gettext("cannot verify zfs dataset " 1924*789Sahrens "%s: %s\n"), current_dataset, buf); 1925*789Sahrens } 1926*789Sahrens 1927*789Sahrens /* ARGSUSED */ 1928*789Sahrens static int 1929*789Sahrens check_zvol(zfs_handle_t *zhp, void *unused) 1930*789Sahrens { 1931*789Sahrens int ret; 1932*789Sahrens 1933*789Sahrens if (zfs_get_type(zhp) == ZFS_TYPE_VOLUME) { 1934*789Sahrens (void) fprintf(stderr, gettext("cannot verify zfs dataset %s: " 1935*789Sahrens "volumes cannot be specified as a zone dataset resource\n"), 1936*789Sahrens zfs_get_name(zhp)); 1937*789Sahrens ret = -1; 1938*789Sahrens } else { 1939*789Sahrens ret = zfs_iter_children(zhp, check_zvol, NULL); 1940*789Sahrens } 1941*789Sahrens 1942*789Sahrens zfs_close(zhp); 1943*789Sahrens 1944*789Sahrens return (ret); 1945*789Sahrens } 1946*789Sahrens 1947*789Sahrens /* 1948*789Sahrens * Validate that the given dataset exists on the system, and that neither it nor 1949*789Sahrens * its children are zvols. 1950*789Sahrens * 1951*789Sahrens * Note that we don't do anything with the 'zoned' property here. All 1952*789Sahrens * management is done in zoneadmd when the zone is actually rebooted. This 1953*789Sahrens * allows us to automatically set the zoned property even when a zone is 1954*789Sahrens * rebooted by the administrator. 1955*789Sahrens */ 1956*789Sahrens static int 1957*789Sahrens verify_datasets(zone_dochandle_t handle) 1958*789Sahrens { 1959*789Sahrens int return_code = Z_OK; 1960*789Sahrens struct zone_dstab dstab; 1961*789Sahrens zfs_handle_t *zhp; 1962*789Sahrens char propbuf[ZFS_MAXPROPLEN]; 1963*789Sahrens char source[ZFS_MAXNAMELEN]; 1964*789Sahrens zfs_source_t srctype; 1965*789Sahrens 1966*789Sahrens if (zonecfg_setdsent(handle) != Z_OK) { 1967*789Sahrens (void) fprintf(stderr, gettext("cannot verify zfs datasets: " 1968*789Sahrens "unable to enumerate datasets\n")); 1969*789Sahrens return (Z_ERR); 1970*789Sahrens } 1971*789Sahrens 1972*789Sahrens zfs_set_error_handler(zfs_error_handler); 1973*789Sahrens 1974*789Sahrens while (zonecfg_getdsent(handle, &dstab) == Z_OK) { 1975*789Sahrens 1976*789Sahrens current_dataset = dstab.zone_dataset_name; 1977*789Sahrens 1978*789Sahrens if ((zhp = zfs_open(dstab.zone_dataset_name, 1979*789Sahrens ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME)) == NULL) { 1980*789Sahrens return_code = Z_ERR; 1981*789Sahrens continue; 1982*789Sahrens } 1983*789Sahrens 1984*789Sahrens if (zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, propbuf, 1985*789Sahrens sizeof (propbuf), &srctype, source, 1986*789Sahrens sizeof (source), 0) == 0 && 1987*789Sahrens (srctype == ZFS_SRC_INHERITED)) { 1988*789Sahrens (void) fprintf(stderr, gettext("cannot verify zfs " 1989*789Sahrens "dataset %s: mountpoint cannot be inherited\n"), 1990*789Sahrens dstab.zone_dataset_name); 1991*789Sahrens return_code = Z_ERR; 1992*789Sahrens zfs_close(zhp); 1993*789Sahrens continue; 1994*789Sahrens } 1995*789Sahrens 1996*789Sahrens if (zfs_get_type(zhp) == ZFS_TYPE_VOLUME) { 1997*789Sahrens (void) fprintf(stderr, gettext("cannot verify zfs " 1998*789Sahrens "dataset %s: volumes cannot be specified as a " 1999*789Sahrens "zone dataset resource\n"), 2000*789Sahrens dstab.zone_dataset_name); 2001*789Sahrens return_code = Z_ERR; 2002*789Sahrens } 2003*789Sahrens 2004*789Sahrens if (zfs_iter_children(zhp, check_zvol, NULL) != 0) 2005*789Sahrens return_code = Z_ERR; 2006*789Sahrens 2007*789Sahrens zfs_close(zhp); 2008*789Sahrens } 2009*789Sahrens (void) zonecfg_enddsent(handle); 2010*789Sahrens 2011*789Sahrens return (return_code); 2012*789Sahrens } 2013*789Sahrens 20140Sstevel@tonic-gate static int 20150Sstevel@tonic-gate verify_details(int cmd_num) 20160Sstevel@tonic-gate { 20170Sstevel@tonic-gate zone_dochandle_t handle; 20180Sstevel@tonic-gate struct zone_nwiftab nwiftab; 20190Sstevel@tonic-gate char zonepath[MAXPATHLEN], checkpath[MAXPATHLEN]; 20200Sstevel@tonic-gate int return_code = Z_OK; 20210Sstevel@tonic-gate int err; 2022766Scarlsonj boolean_t in_alt_root; 20230Sstevel@tonic-gate 20240Sstevel@tonic-gate if ((handle = zonecfg_init_handle()) == NULL) { 20250Sstevel@tonic-gate zperror(cmd_to_str(cmd_num), B_TRUE); 20260Sstevel@tonic-gate return (Z_ERR); 20270Sstevel@tonic-gate } 20280Sstevel@tonic-gate if ((err = zonecfg_get_handle(target_zone, handle)) != Z_OK) { 20290Sstevel@tonic-gate errno = err; 20300Sstevel@tonic-gate zperror(cmd_to_str(cmd_num), B_TRUE); 20310Sstevel@tonic-gate zonecfg_fini_handle(handle); 20320Sstevel@tonic-gate return (Z_ERR); 20330Sstevel@tonic-gate } 20340Sstevel@tonic-gate if ((err = zonecfg_get_zonepath(handle, zonepath, sizeof (zonepath))) != 20350Sstevel@tonic-gate Z_OK) { 20360Sstevel@tonic-gate errno = err; 20370Sstevel@tonic-gate zperror(cmd_to_str(cmd_num), B_TRUE); 20380Sstevel@tonic-gate zonecfg_fini_handle(handle); 20390Sstevel@tonic-gate return (Z_ERR); 20400Sstevel@tonic-gate } 20410Sstevel@tonic-gate /* 20420Sstevel@tonic-gate * zonecfg_get_zonepath() gets its data from the XML repository. 20430Sstevel@tonic-gate * Verify this against the index file, which is checked first by 20440Sstevel@tonic-gate * zone_get_zonepath(). If they don't match, bail out. 20450Sstevel@tonic-gate */ 20460Sstevel@tonic-gate if ((err = zone_get_zonepath(target_zone, checkpath, 20470Sstevel@tonic-gate sizeof (checkpath))) != Z_OK) { 20480Sstevel@tonic-gate errno = err; 20490Sstevel@tonic-gate zperror2(target_zone, gettext("could not get zone path")); 20500Sstevel@tonic-gate return (Z_ERR); 20510Sstevel@tonic-gate } 20520Sstevel@tonic-gate if (strcmp(zonepath, checkpath) != 0) { 20530Sstevel@tonic-gate (void) fprintf(stderr, gettext("The XML repository has " 20540Sstevel@tonic-gate "zonepath '%s',\nbut the index file has zonepath '%s'.\n" 20550Sstevel@tonic-gate "These must match, so fix the incorrect entry.\n"), 20560Sstevel@tonic-gate zonepath, checkpath); 20570Sstevel@tonic-gate return (Z_ERR); 20580Sstevel@tonic-gate } 20590Sstevel@tonic-gate if (validate_zonepath(zonepath, cmd_num) != Z_OK) { 20600Sstevel@tonic-gate (void) fprintf(stderr, gettext("could not verify zonepath %s " 20610Sstevel@tonic-gate "because of the above errors.\n"), zonepath); 20620Sstevel@tonic-gate return_code = Z_ERR; 20630Sstevel@tonic-gate } 20640Sstevel@tonic-gate 2065766Scarlsonj in_alt_root = zonecfg_in_alt_root(); 2066766Scarlsonj if (in_alt_root) 2067766Scarlsonj goto no_net; 2068766Scarlsonj 20690Sstevel@tonic-gate if ((err = zonecfg_setnwifent(handle)) != Z_OK) { 20700Sstevel@tonic-gate errno = err; 20710Sstevel@tonic-gate zperror(cmd_to_str(cmd_num), B_TRUE); 20720Sstevel@tonic-gate zonecfg_fini_handle(handle); 20730Sstevel@tonic-gate return (Z_ERR); 20740Sstevel@tonic-gate } 20750Sstevel@tonic-gate while (zonecfg_getnwifent(handle, &nwiftab) == Z_OK) { 20760Sstevel@tonic-gate struct lifreq lifr; 20770Sstevel@tonic-gate sa_family_t af; 20780Sstevel@tonic-gate int so, res; 20790Sstevel@tonic-gate 20800Sstevel@tonic-gate /* skip any loopback interfaces */ 20810Sstevel@tonic-gate if (strcmp(nwiftab.zone_nwif_physical, "lo0") == 0) 20820Sstevel@tonic-gate continue; 20830Sstevel@tonic-gate if ((res = zonecfg_valid_net_address(nwiftab.zone_nwif_address, 20840Sstevel@tonic-gate &lifr)) != Z_OK) { 20850Sstevel@tonic-gate (void) fprintf(stderr, gettext("could not verify %s " 20860Sstevel@tonic-gate "%s=%s %s=%s: %s\n"), "net", "address", 20870Sstevel@tonic-gate nwiftab.zone_nwif_address, "physical", 20880Sstevel@tonic-gate nwiftab.zone_nwif_physical, zonecfg_strerror(res)); 20890Sstevel@tonic-gate return_code = Z_ERR; 20900Sstevel@tonic-gate continue; 20910Sstevel@tonic-gate } 20920Sstevel@tonic-gate af = lifr.lifr_addr.ss_family; 20930Sstevel@tonic-gate (void) memset(&lifr, 0, sizeof (lifr)); 20940Sstevel@tonic-gate (void) strlcpy(lifr.lifr_name, nwiftab.zone_nwif_physical, 20950Sstevel@tonic-gate sizeof (lifr.lifr_name)); 20960Sstevel@tonic-gate lifr.lifr_addr.ss_family = af; 20970Sstevel@tonic-gate if ((so = socket(af, SOCK_DGRAM, 0)) < 0) { 20980Sstevel@tonic-gate (void) fprintf(stderr, gettext("could not verify %s " 20990Sstevel@tonic-gate "%s=%s %s=%s: could not get socket: %s\n"), "net", 21000Sstevel@tonic-gate "address", nwiftab.zone_nwif_address, "physical", 21010Sstevel@tonic-gate nwiftab.zone_nwif_physical, strerror(errno)); 21020Sstevel@tonic-gate return_code = Z_ERR; 21030Sstevel@tonic-gate continue; 21040Sstevel@tonic-gate } 21050Sstevel@tonic-gate if (ioctl(so, SIOCGLIFFLAGS, (caddr_t)&lifr) < 0) { 21060Sstevel@tonic-gate (void) fprintf(stderr, 21070Sstevel@tonic-gate gettext("could not verify %s %s=%s %s=%s: %s\n"), 21080Sstevel@tonic-gate "net", "address", nwiftab.zone_nwif_address, 21090Sstevel@tonic-gate "physical", nwiftab.zone_nwif_physical, 21100Sstevel@tonic-gate strerror(errno)); 21110Sstevel@tonic-gate return_code = Z_ERR; 21120Sstevel@tonic-gate } 21130Sstevel@tonic-gate (void) close(so); 21140Sstevel@tonic-gate } 21150Sstevel@tonic-gate (void) zonecfg_endnwifent(handle); 2116766Scarlsonj no_net: 21170Sstevel@tonic-gate 21180Sstevel@tonic-gate if (verify_filesystems(handle) != Z_OK) 21190Sstevel@tonic-gate return_code = Z_ERR; 2120766Scarlsonj if (!in_alt_root && verify_rctls(handle) != Z_OK) 21210Sstevel@tonic-gate return_code = Z_ERR; 2122766Scarlsonj if (!in_alt_root && verify_pool(handle) != Z_OK) 21230Sstevel@tonic-gate return_code = Z_ERR; 2124*789Sahrens if (!in_alt_root && verify_datasets(handle) != Z_OK) 2125*789Sahrens return_code = Z_ERR; 21260Sstevel@tonic-gate zonecfg_fini_handle(handle); 21270Sstevel@tonic-gate if (return_code == Z_ERR) 21280Sstevel@tonic-gate (void) fprintf(stderr, 21290Sstevel@tonic-gate gettext("%s: zone %s failed to verify\n"), 21300Sstevel@tonic-gate execname, target_zone); 21310Sstevel@tonic-gate return (return_code); 21320Sstevel@tonic-gate } 21330Sstevel@tonic-gate 21340Sstevel@tonic-gate static int 21350Sstevel@tonic-gate verify_func(int argc, char *argv[]) 21360Sstevel@tonic-gate { 21370Sstevel@tonic-gate int arg; 21380Sstevel@tonic-gate 21390Sstevel@tonic-gate optind = 0; 21400Sstevel@tonic-gate if ((arg = getopt(argc, argv, "?")) != EOF) { 21410Sstevel@tonic-gate switch (arg) { 21420Sstevel@tonic-gate case '?': 21430Sstevel@tonic-gate sub_usage(SHELP_VERIFY, CMD_VERIFY); 21440Sstevel@tonic-gate return (optopt == '?' ? Z_OK : Z_USAGE); 21450Sstevel@tonic-gate default: 21460Sstevel@tonic-gate sub_usage(SHELP_VERIFY, CMD_VERIFY); 21470Sstevel@tonic-gate return (Z_USAGE); 21480Sstevel@tonic-gate } 21490Sstevel@tonic-gate } 21500Sstevel@tonic-gate if (argc > optind) { 21510Sstevel@tonic-gate sub_usage(SHELP_VERIFY, CMD_VERIFY); 21520Sstevel@tonic-gate return (Z_USAGE); 21530Sstevel@tonic-gate } 21540Sstevel@tonic-gate if (sanity_check(target_zone, CMD_VERIFY, B_FALSE, B_FALSE) != Z_OK) 21550Sstevel@tonic-gate return (Z_ERR); 21560Sstevel@tonic-gate return (verify_details(CMD_VERIFY)); 21570Sstevel@tonic-gate } 21580Sstevel@tonic-gate 21590Sstevel@tonic-gate #define LUCREATEZONE "/usr/lib/lu/lucreatezone" 21600Sstevel@tonic-gate 21610Sstevel@tonic-gate static int 21620Sstevel@tonic-gate install_func(int argc, char *argv[]) 21630Sstevel@tonic-gate { 21640Sstevel@tonic-gate /* 9: "exec " and " -z " */ 21650Sstevel@tonic-gate char cmdbuf[sizeof (LUCREATEZONE) + ZONENAME_MAX + 9]; 21660Sstevel@tonic-gate int lockfd; 21670Sstevel@tonic-gate int err, arg; 21680Sstevel@tonic-gate char zonepath[MAXPATHLEN]; 21690Sstevel@tonic-gate int status; 21700Sstevel@tonic-gate 2171766Scarlsonj if (zonecfg_in_alt_root()) { 2172766Scarlsonj zerror(gettext("cannot install zone in alternate root")); 2173766Scarlsonj return (Z_ERR); 2174766Scarlsonj } 2175766Scarlsonj 21760Sstevel@tonic-gate optind = 0; 21770Sstevel@tonic-gate if ((arg = getopt(argc, argv, "?")) != EOF) { 21780Sstevel@tonic-gate switch (arg) { 21790Sstevel@tonic-gate case '?': 21800Sstevel@tonic-gate sub_usage(SHELP_INSTALL, CMD_INSTALL); 21810Sstevel@tonic-gate return (optopt == '?' ? Z_OK : Z_USAGE); 21820Sstevel@tonic-gate default: 21830Sstevel@tonic-gate sub_usage(SHELP_INSTALL, CMD_INSTALL); 21840Sstevel@tonic-gate return (Z_USAGE); 21850Sstevel@tonic-gate } 21860Sstevel@tonic-gate } 21870Sstevel@tonic-gate if (argc > optind) { 21880Sstevel@tonic-gate sub_usage(SHELP_INSTALL, CMD_INSTALL); 21890Sstevel@tonic-gate return (Z_USAGE); 21900Sstevel@tonic-gate } 21910Sstevel@tonic-gate if (sanity_check(target_zone, CMD_INSTALL, B_FALSE, B_TRUE) != Z_OK) 21920Sstevel@tonic-gate return (Z_ERR); 21930Sstevel@tonic-gate if (verify_details(CMD_INSTALL) != Z_OK) 21940Sstevel@tonic-gate return (Z_ERR); 21950Sstevel@tonic-gate 21960Sstevel@tonic-gate if (grab_lock_file(target_zone, &lockfd) != Z_OK) { 21970Sstevel@tonic-gate zerror(gettext("another %s may have an operation in progress."), 21980Sstevel@tonic-gate "zoneadmd"); 21990Sstevel@tonic-gate return (Z_ERR); 22000Sstevel@tonic-gate } 22010Sstevel@tonic-gate err = zone_set_state(target_zone, ZONE_STATE_INCOMPLETE); 22020Sstevel@tonic-gate if (err != Z_OK) { 22030Sstevel@tonic-gate errno = err; 22040Sstevel@tonic-gate zperror2(target_zone, gettext("could not set state")); 22050Sstevel@tonic-gate goto done; 22060Sstevel@tonic-gate } 22070Sstevel@tonic-gate 22080Sstevel@tonic-gate /* 22090Sstevel@tonic-gate * According to the Application Packaging Developer's Guide, a 22100Sstevel@tonic-gate * "checkinstall" script when included in a package is executed as 22110Sstevel@tonic-gate * the user "install", if such a user exists, or by the user 22120Sstevel@tonic-gate * "nobody". In order to support this dubious behavior, the path 22130Sstevel@tonic-gate * to the zone being constructed is opened up during the life of 22140Sstevel@tonic-gate * the command laying down the zone's root file system. Once this 22150Sstevel@tonic-gate * has completed, regardless of whether it was successful, the 22160Sstevel@tonic-gate * path to the zone is again restricted. 22170Sstevel@tonic-gate */ 22180Sstevel@tonic-gate if ((err = zone_get_zonepath(target_zone, zonepath, 22190Sstevel@tonic-gate sizeof (zonepath))) != Z_OK) { 22200Sstevel@tonic-gate errno = err; 22210Sstevel@tonic-gate zperror2(target_zone, gettext("could not get zone path")); 22220Sstevel@tonic-gate goto done; 22230Sstevel@tonic-gate } 22240Sstevel@tonic-gate if (chmod(zonepath, DEFAULT_DIR_MODE) != 0) { 22250Sstevel@tonic-gate zperror(zonepath, B_FALSE); 22260Sstevel@tonic-gate err = Z_ERR; 22270Sstevel@tonic-gate goto done; 22280Sstevel@tonic-gate } 22290Sstevel@tonic-gate 22300Sstevel@tonic-gate /* 22310Sstevel@tonic-gate * "exec" the command so that the returned status is that of 22320Sstevel@tonic-gate * LUCREATEZONE and not the shell. 22330Sstevel@tonic-gate */ 22340Sstevel@tonic-gate (void) snprintf(cmdbuf, sizeof (cmdbuf), "exec " LUCREATEZONE " -z %s", 22350Sstevel@tonic-gate target_zone); 22360Sstevel@tonic-gate status = do_subproc(cmdbuf); 22370Sstevel@tonic-gate if (chmod(zonepath, S_IRWXU) != 0) { 22380Sstevel@tonic-gate zperror(zonepath, B_FALSE); 22390Sstevel@tonic-gate err = Z_ERR; 22400Sstevel@tonic-gate goto done; 22410Sstevel@tonic-gate } 22420Sstevel@tonic-gate if ((err = subproc_status(LUCREATEZONE, status)) != Z_OK) 22430Sstevel@tonic-gate goto done; 22440Sstevel@tonic-gate 22450Sstevel@tonic-gate if ((err = zone_set_state(target_zone, ZONE_STATE_INSTALLED)) != Z_OK) { 22460Sstevel@tonic-gate errno = err; 22470Sstevel@tonic-gate zperror2(target_zone, gettext("could not set state")); 22480Sstevel@tonic-gate goto done; 22490Sstevel@tonic-gate } 22500Sstevel@tonic-gate 22510Sstevel@tonic-gate done: 22520Sstevel@tonic-gate release_lock_file(lockfd); 22530Sstevel@tonic-gate return ((err == Z_OK) ? Z_OK : Z_ERR); 22540Sstevel@tonic-gate } 22550Sstevel@tonic-gate 22560Sstevel@tonic-gate /* 22570Sstevel@tonic-gate * On input, TRUE => yes, FALSE => no. 22580Sstevel@tonic-gate * On return, TRUE => 1, FALSE => 0, could not ask => -1. 22590Sstevel@tonic-gate */ 22600Sstevel@tonic-gate 22610Sstevel@tonic-gate static int 22620Sstevel@tonic-gate ask_yesno(boolean_t default_answer, const char *question) 22630Sstevel@tonic-gate { 22640Sstevel@tonic-gate char line[64]; /* should be large enough to answer yes or no */ 22650Sstevel@tonic-gate 22660Sstevel@tonic-gate if (!isatty(STDIN_FILENO)) 22670Sstevel@tonic-gate return (-1); 22680Sstevel@tonic-gate for (;;) { 22690Sstevel@tonic-gate (void) printf("%s (%s)? ", question, 22700Sstevel@tonic-gate default_answer ? "[y]/n" : "y/[n]"); 22710Sstevel@tonic-gate if (fgets(line, sizeof (line), stdin) == NULL || 22720Sstevel@tonic-gate line[0] == '\n') 22730Sstevel@tonic-gate return (default_answer ? 1 : 0); 22740Sstevel@tonic-gate if (tolower(line[0]) == 'y') 22750Sstevel@tonic-gate return (1); 22760Sstevel@tonic-gate if (tolower(line[0]) == 'n') 22770Sstevel@tonic-gate return (0); 22780Sstevel@tonic-gate } 22790Sstevel@tonic-gate } 22800Sstevel@tonic-gate 22810Sstevel@tonic-gate #define RMCOMMAND "/usr/bin/rm -rf" 22820Sstevel@tonic-gate 22830Sstevel@tonic-gate /* ARGSUSED */ 22840Sstevel@tonic-gate int 22850Sstevel@tonic-gate zfm_print(const char *p, void *r) { 22860Sstevel@tonic-gate zerror(" %s\n", p); 22870Sstevel@tonic-gate return (0); 22880Sstevel@tonic-gate } 22890Sstevel@tonic-gate 22900Sstevel@tonic-gate static int 22910Sstevel@tonic-gate uninstall_func(int argc, char *argv[]) 22920Sstevel@tonic-gate { 22930Sstevel@tonic-gate /* 6: "exec " and " " */ 22940Sstevel@tonic-gate char cmdbuf[sizeof (RMCOMMAND) + MAXPATHLEN + 6]; 22950Sstevel@tonic-gate char line[ZONENAME_MAX + 128]; /* Enough for "Are you sure ..." */ 22960Sstevel@tonic-gate char rootpath[MAXPATHLEN], devpath[MAXPATHLEN]; 22970Sstevel@tonic-gate boolean_t force = B_FALSE; 22980Sstevel@tonic-gate int lockfd, answer; 22990Sstevel@tonic-gate int err, arg; 23000Sstevel@tonic-gate int status; 23010Sstevel@tonic-gate 2302766Scarlsonj if (zonecfg_in_alt_root()) { 2303766Scarlsonj zerror(gettext("cannot uninstall zone in alternate root")); 2304766Scarlsonj return (Z_ERR); 2305766Scarlsonj } 2306766Scarlsonj 23070Sstevel@tonic-gate optind = 0; 23080Sstevel@tonic-gate while ((arg = getopt(argc, argv, "?F")) != EOF) { 23090Sstevel@tonic-gate switch (arg) { 23100Sstevel@tonic-gate case '?': 23110Sstevel@tonic-gate sub_usage(SHELP_UNINSTALL, CMD_UNINSTALL); 23120Sstevel@tonic-gate return (optopt == '?' ? Z_OK : Z_USAGE); 23130Sstevel@tonic-gate case 'F': 23140Sstevel@tonic-gate force = B_TRUE; 23150Sstevel@tonic-gate break; 23160Sstevel@tonic-gate default: 23170Sstevel@tonic-gate sub_usage(SHELP_UNINSTALL, CMD_UNINSTALL); 23180Sstevel@tonic-gate return (Z_USAGE); 23190Sstevel@tonic-gate } 23200Sstevel@tonic-gate } 23210Sstevel@tonic-gate if (argc > optind) { 23220Sstevel@tonic-gate sub_usage(SHELP_UNINSTALL, CMD_UNINSTALL); 23230Sstevel@tonic-gate return (Z_USAGE); 23240Sstevel@tonic-gate } 23250Sstevel@tonic-gate 23260Sstevel@tonic-gate if (sanity_check(target_zone, CMD_UNINSTALL, B_FALSE, B_TRUE) != Z_OK) 23270Sstevel@tonic-gate return (Z_ERR); 23280Sstevel@tonic-gate 23290Sstevel@tonic-gate if (!force) { 23300Sstevel@tonic-gate (void) snprintf(line, sizeof (line), 23310Sstevel@tonic-gate gettext("Are you sure you want to %s zone %s"), 23320Sstevel@tonic-gate cmd_to_str(CMD_UNINSTALL), target_zone); 23330Sstevel@tonic-gate if ((answer = ask_yesno(B_FALSE, line)) == 0) { 23340Sstevel@tonic-gate return (Z_OK); 23350Sstevel@tonic-gate } else if (answer == -1) { 23360Sstevel@tonic-gate zerror(gettext("Input not from terminal and -F " 23370Sstevel@tonic-gate "not specified: %s not done."), 23380Sstevel@tonic-gate cmd_to_str(CMD_UNINSTALL)); 23390Sstevel@tonic-gate return (Z_ERR); 23400Sstevel@tonic-gate } 23410Sstevel@tonic-gate } 23420Sstevel@tonic-gate 23430Sstevel@tonic-gate if ((err = zone_get_zonepath(target_zone, devpath, 23440Sstevel@tonic-gate sizeof (devpath))) != Z_OK) { 23450Sstevel@tonic-gate errno = err; 23460Sstevel@tonic-gate zperror2(target_zone, gettext("could not get zone path")); 23470Sstevel@tonic-gate return (Z_ERR); 23480Sstevel@tonic-gate } 23490Sstevel@tonic-gate (void) strlcat(devpath, "/dev", sizeof (devpath)); 23500Sstevel@tonic-gate if ((err = zone_get_rootpath(target_zone, rootpath, 23510Sstevel@tonic-gate sizeof (rootpath))) != Z_OK) { 23520Sstevel@tonic-gate errno = err; 23530Sstevel@tonic-gate zperror2(target_zone, gettext("could not get root path")); 23540Sstevel@tonic-gate return (Z_ERR); 23550Sstevel@tonic-gate } 23560Sstevel@tonic-gate 23570Sstevel@tonic-gate /* 23580Sstevel@tonic-gate * If there seems to be a zoneadmd running for this zone, call it 23590Sstevel@tonic-gate * to tell it that an uninstall is happening; if all goes well it 23600Sstevel@tonic-gate * will then shut itself down. 23610Sstevel@tonic-gate */ 23620Sstevel@tonic-gate if (ping_zoneadmd(target_zone) == Z_OK) { 23630Sstevel@tonic-gate zone_cmd_arg_t zarg; 23640Sstevel@tonic-gate zarg.cmd = Z_NOTE_UNINSTALLING; 23650Sstevel@tonic-gate /* we don't care too much if this fails... just plow on */ 23660Sstevel@tonic-gate (void) call_zoneadmd(target_zone, &zarg); 23670Sstevel@tonic-gate } 23680Sstevel@tonic-gate 23690Sstevel@tonic-gate if (grab_lock_file(target_zone, &lockfd) != Z_OK) { 23700Sstevel@tonic-gate zerror(gettext("another %s may have an operation in progress."), 23710Sstevel@tonic-gate "zoneadmd"); 23720Sstevel@tonic-gate return (Z_ERR); 23730Sstevel@tonic-gate } 23740Sstevel@tonic-gate 23750Sstevel@tonic-gate /* Don't uninstall the zone if anything is mounted there */ 23760Sstevel@tonic-gate err = zonecfg_find_mounts(rootpath, NULL, NULL); 23770Sstevel@tonic-gate if (err) { 23780Sstevel@tonic-gate zerror(gettext("These file-systems are mounted on " 23790Sstevel@tonic-gate "subdirectories of %s.\n"), rootpath); 23800Sstevel@tonic-gate (void) zonecfg_find_mounts(rootpath, zfm_print, NULL); 23810Sstevel@tonic-gate return (Z_ERR); 23820Sstevel@tonic-gate } 23830Sstevel@tonic-gate 23840Sstevel@tonic-gate err = zone_set_state(target_zone, ZONE_STATE_INCOMPLETE); 23850Sstevel@tonic-gate if (err != Z_OK) { 23860Sstevel@tonic-gate errno = err; 23870Sstevel@tonic-gate zperror2(target_zone, gettext("could not set state")); 23880Sstevel@tonic-gate goto bad; 23890Sstevel@tonic-gate } 23900Sstevel@tonic-gate 23910Sstevel@tonic-gate /* 23920Sstevel@tonic-gate * "exec" the command so that the returned status is that of 23930Sstevel@tonic-gate * RMCOMMAND and not the shell. 23940Sstevel@tonic-gate */ 23950Sstevel@tonic-gate (void) snprintf(cmdbuf, sizeof (cmdbuf), "exec " RMCOMMAND " %s", 23960Sstevel@tonic-gate devpath); 23970Sstevel@tonic-gate status = do_subproc(cmdbuf); 23980Sstevel@tonic-gate if ((err = subproc_status(RMCOMMAND, status)) != Z_OK) 23990Sstevel@tonic-gate goto bad; 24000Sstevel@tonic-gate (void) snprintf(cmdbuf, sizeof (cmdbuf), "exec " RMCOMMAND " %s", 24010Sstevel@tonic-gate rootpath); 24020Sstevel@tonic-gate status = do_subproc(cmdbuf); 24030Sstevel@tonic-gate if ((err = subproc_status(RMCOMMAND, status)) != Z_OK) 24040Sstevel@tonic-gate goto bad; 24050Sstevel@tonic-gate err = zone_set_state(target_zone, ZONE_STATE_CONFIGURED); 24060Sstevel@tonic-gate if (err != Z_OK) { 24070Sstevel@tonic-gate errno = err; 24080Sstevel@tonic-gate zperror2(target_zone, gettext("could not reset state")); 24090Sstevel@tonic-gate } 24100Sstevel@tonic-gate bad: 24110Sstevel@tonic-gate release_lock_file(lockfd); 24120Sstevel@tonic-gate return (err); 24130Sstevel@tonic-gate } 24140Sstevel@tonic-gate 2415766Scarlsonj /* ARGSUSED */ 2416766Scarlsonj static int 2417766Scarlsonj mount_func(int argc, char *argv[]) 2418766Scarlsonj { 2419766Scarlsonj zone_cmd_arg_t zarg; 2420766Scarlsonj 2421766Scarlsonj if (argc > 0) 2422766Scarlsonj return (Z_USAGE); 2423766Scarlsonj if (sanity_check(target_zone, CMD_MOUNT, B_FALSE, B_FALSE) != Z_OK) 2424766Scarlsonj return (Z_ERR); 2425766Scarlsonj if (verify_details(CMD_MOUNT) != Z_OK) 2426766Scarlsonj return (Z_ERR); 2427766Scarlsonj 2428766Scarlsonj zarg.cmd = Z_MOUNT; 2429766Scarlsonj if (call_zoneadmd(target_zone, &zarg) != 0) { 2430766Scarlsonj zerror(gettext("call to %s failed"), "zoneadmd"); 2431766Scarlsonj return (Z_ERR); 2432766Scarlsonj } 2433766Scarlsonj return (Z_OK); 2434766Scarlsonj } 2435766Scarlsonj 2436766Scarlsonj /* ARGSUSED */ 2437766Scarlsonj static int 2438766Scarlsonj unmount_func(int argc, char *argv[]) 2439766Scarlsonj { 2440766Scarlsonj zone_cmd_arg_t zarg; 2441766Scarlsonj 2442766Scarlsonj if (argc > 0) 2443766Scarlsonj return (Z_USAGE); 2444766Scarlsonj if (sanity_check(target_zone, CMD_UNMOUNT, B_FALSE, B_FALSE) != Z_OK) 2445766Scarlsonj return (Z_ERR); 2446766Scarlsonj 2447766Scarlsonj zarg.cmd = Z_UNMOUNT; 2448766Scarlsonj if (call_zoneadmd(target_zone, &zarg) != 0) { 2449766Scarlsonj zerror(gettext("call to %s failed"), "zoneadmd"); 2450766Scarlsonj return (Z_ERR); 2451766Scarlsonj } 2452766Scarlsonj return (Z_OK); 2453766Scarlsonj } 2454766Scarlsonj 24550Sstevel@tonic-gate static int 24560Sstevel@tonic-gate help_func(int argc, char *argv[]) 24570Sstevel@tonic-gate { 24580Sstevel@tonic-gate int arg, cmd_num; 24590Sstevel@tonic-gate 24600Sstevel@tonic-gate if (argc == 0) { 24610Sstevel@tonic-gate (void) usage(B_TRUE); 24620Sstevel@tonic-gate return (Z_OK); 24630Sstevel@tonic-gate } 24640Sstevel@tonic-gate optind = 0; 24650Sstevel@tonic-gate if ((arg = getopt(argc, argv, "?")) != EOF) { 24660Sstevel@tonic-gate switch (arg) { 24670Sstevel@tonic-gate case '?': 24680Sstevel@tonic-gate sub_usage(SHELP_HELP, CMD_HELP); 24690Sstevel@tonic-gate return (optopt == '?' ? Z_OK : Z_USAGE); 24700Sstevel@tonic-gate default: 24710Sstevel@tonic-gate sub_usage(SHELP_HELP, CMD_HELP); 24720Sstevel@tonic-gate return (Z_USAGE); 24730Sstevel@tonic-gate } 24740Sstevel@tonic-gate } 24750Sstevel@tonic-gate while (optind < argc) { 24760Sstevel@tonic-gate if ((cmd_num = cmd_match(argv[optind])) < 0) { 24770Sstevel@tonic-gate sub_usage(SHELP_HELP, CMD_HELP); 24780Sstevel@tonic-gate return (Z_USAGE); 24790Sstevel@tonic-gate } 24800Sstevel@tonic-gate sub_usage(cmdtab[cmd_num].short_usage, cmd_num); 24810Sstevel@tonic-gate optind++; 24820Sstevel@tonic-gate } 24830Sstevel@tonic-gate return (Z_OK); 24840Sstevel@tonic-gate } 24850Sstevel@tonic-gate 24860Sstevel@tonic-gate /* 24870Sstevel@tonic-gate * Returns: CMD_MIN thru CMD_MAX on success, -1 on error 24880Sstevel@tonic-gate */ 24890Sstevel@tonic-gate 24900Sstevel@tonic-gate static int 24910Sstevel@tonic-gate cmd_match(char *cmd) 24920Sstevel@tonic-gate { 24930Sstevel@tonic-gate int i; 24940Sstevel@tonic-gate 24950Sstevel@tonic-gate for (i = CMD_MIN; i <= CMD_MAX; i++) { 24960Sstevel@tonic-gate /* return only if there is an exact match */ 24970Sstevel@tonic-gate if (strcmp(cmd, cmdtab[i].cmd_name) == 0) 24980Sstevel@tonic-gate return (cmdtab[i].cmd_num); 24990Sstevel@tonic-gate } 25000Sstevel@tonic-gate return (-1); 25010Sstevel@tonic-gate } 25020Sstevel@tonic-gate 25030Sstevel@tonic-gate static int 25040Sstevel@tonic-gate parse_and_run(int argc, char *argv[]) 25050Sstevel@tonic-gate { 25060Sstevel@tonic-gate int i = cmd_match(argv[0]); 25070Sstevel@tonic-gate 25080Sstevel@tonic-gate if (i < 0) 25090Sstevel@tonic-gate return (usage(B_FALSE)); 25100Sstevel@tonic-gate return (cmdtab[i].handler(argc - 1, &(argv[1]))); 25110Sstevel@tonic-gate } 25120Sstevel@tonic-gate 25130Sstevel@tonic-gate static char * 25140Sstevel@tonic-gate get_execbasename(char *execfullname) 25150Sstevel@tonic-gate { 25160Sstevel@tonic-gate char *last_slash, *execbasename; 25170Sstevel@tonic-gate 25180Sstevel@tonic-gate /* guard against '/' at end of command invocation */ 25190Sstevel@tonic-gate for (;;) { 25200Sstevel@tonic-gate last_slash = strrchr(execfullname, '/'); 25210Sstevel@tonic-gate if (last_slash == NULL) { 25220Sstevel@tonic-gate execbasename = execfullname; 25230Sstevel@tonic-gate break; 25240Sstevel@tonic-gate } else { 25250Sstevel@tonic-gate execbasename = last_slash + 1; 25260Sstevel@tonic-gate if (*execbasename == '\0') { 25270Sstevel@tonic-gate *last_slash = '\0'; 25280Sstevel@tonic-gate continue; 25290Sstevel@tonic-gate } 25300Sstevel@tonic-gate break; 25310Sstevel@tonic-gate } 25320Sstevel@tonic-gate } 25330Sstevel@tonic-gate return (execbasename); 25340Sstevel@tonic-gate } 25350Sstevel@tonic-gate 25360Sstevel@tonic-gate int 25370Sstevel@tonic-gate main(int argc, char **argv) 25380Sstevel@tonic-gate { 25390Sstevel@tonic-gate int arg; 25400Sstevel@tonic-gate zoneid_t zid; 2541766Scarlsonj struct stat st; 25420Sstevel@tonic-gate 25430Sstevel@tonic-gate if ((locale = setlocale(LC_ALL, "")) == NULL) 25440Sstevel@tonic-gate locale = "C"; 25450Sstevel@tonic-gate (void) textdomain(TEXT_DOMAIN); 25460Sstevel@tonic-gate setbuf(stdout, NULL); 25470Sstevel@tonic-gate (void) sigset(SIGHUP, SIG_IGN); 25480Sstevel@tonic-gate execname = get_execbasename(argv[0]); 25490Sstevel@tonic-gate target_zone = NULL; 25500Sstevel@tonic-gate if (chdir("/") != 0) { 25510Sstevel@tonic-gate zerror(gettext("could not change directory to /.")); 25520Sstevel@tonic-gate exit(Z_ERR); 25530Sstevel@tonic-gate } 25540Sstevel@tonic-gate 2555766Scarlsonj while ((arg = getopt(argc, argv, "?z:R:")) != EOF) { 25560Sstevel@tonic-gate switch (arg) { 25570Sstevel@tonic-gate case '?': 25580Sstevel@tonic-gate return (usage(B_TRUE)); 25590Sstevel@tonic-gate case 'z': 25600Sstevel@tonic-gate target_zone = optarg; 25610Sstevel@tonic-gate break; 2562766Scarlsonj case 'R': /* private option for admin/install use */ 2563766Scarlsonj if (*optarg != '/') { 2564766Scarlsonj zerror(gettext("root path must be absolute.")); 2565766Scarlsonj exit(Z_ERR); 2566766Scarlsonj } 2567766Scarlsonj if (stat(optarg, &st) == -1 || !S_ISDIR(st.st_mode)) { 2568766Scarlsonj zerror( 2569766Scarlsonj gettext("root path must be a directory.")); 2570766Scarlsonj exit(Z_ERR); 2571766Scarlsonj } 2572766Scarlsonj zonecfg_set_root(optarg); 2573766Scarlsonj break; 25740Sstevel@tonic-gate default: 25750Sstevel@tonic-gate return (usage(B_FALSE)); 25760Sstevel@tonic-gate } 25770Sstevel@tonic-gate } 25780Sstevel@tonic-gate 25790Sstevel@tonic-gate if (optind >= argc) 25800Sstevel@tonic-gate return (usage(B_FALSE)); 25810Sstevel@tonic-gate if (target_zone != NULL && zone_get_id(target_zone, &zid) != 0) { 25820Sstevel@tonic-gate errno = Z_NO_ZONE; 25830Sstevel@tonic-gate zperror(target_zone, B_TRUE); 25840Sstevel@tonic-gate exit(Z_ERR); 25850Sstevel@tonic-gate } 25860Sstevel@tonic-gate return (parse_and_run(argc - optind, &argv[optind])); 25870Sstevel@tonic-gate } 2588