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 51507Sgjelinek * Common Development and Distribution License (the "License"). 61507Sgjelinek * You may not use this file except in compliance with the License. 70Sstevel@tonic-gate * 80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 100Sstevel@tonic-gate * See the License for the specific language governing permissions 110Sstevel@tonic-gate * and limitations under the License. 120Sstevel@tonic-gate * 130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 180Sstevel@tonic-gate * 190Sstevel@tonic-gate * CDDL HEADER END 200Sstevel@tonic-gate */ 21222Scomay 220Sstevel@tonic-gate /* 233352Sgjelinek * Copyright 2007 Sun Microsystems, Inc. All rights reserved. 240Sstevel@tonic-gate * Use is subject to license terms. 250Sstevel@tonic-gate */ 260Sstevel@tonic-gate 270Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 280Sstevel@tonic-gate 290Sstevel@tonic-gate /* 300Sstevel@tonic-gate * zoneadm is a command interpreter for zone administration. It is all in 310Sstevel@tonic-gate * C (i.e., no lex/yacc), and all the argument passing is argc/argv based. 320Sstevel@tonic-gate * main() calls parse_and_run() which calls cmd_match(), then invokes the 330Sstevel@tonic-gate * appropriate command's handler function. The rest of the program is the 340Sstevel@tonic-gate * handler functions and their helper functions. 350Sstevel@tonic-gate * 360Sstevel@tonic-gate * Some of the helper functions are used largely to simplify I18N: reducing 370Sstevel@tonic-gate * the need for translation notes. This is particularly true of many of 380Sstevel@tonic-gate * the zerror() calls: doing e.g. zerror(gettext("%s failed"), "foo") rather 390Sstevel@tonic-gate * than zerror(gettext("foo failed")) with a translation note indicating 400Sstevel@tonic-gate * that "foo" need not be translated. 410Sstevel@tonic-gate */ 420Sstevel@tonic-gate 430Sstevel@tonic-gate #include <stdio.h> 440Sstevel@tonic-gate #include <errno.h> 450Sstevel@tonic-gate #include <unistd.h> 460Sstevel@tonic-gate #include <signal.h> 470Sstevel@tonic-gate #include <stdarg.h> 480Sstevel@tonic-gate #include <ctype.h> 490Sstevel@tonic-gate #include <stdlib.h> 500Sstevel@tonic-gate #include <string.h> 510Sstevel@tonic-gate #include <wait.h> 520Sstevel@tonic-gate #include <zone.h> 530Sstevel@tonic-gate #include <priv.h> 540Sstevel@tonic-gate #include <locale.h> 550Sstevel@tonic-gate #include <libintl.h> 560Sstevel@tonic-gate #include <libzonecfg.h> 570Sstevel@tonic-gate #include <bsm/adt.h> 582712Snn35248 #include <sys/brand.h> 590Sstevel@tonic-gate #include <sys/param.h> 600Sstevel@tonic-gate #include <sys/types.h> 610Sstevel@tonic-gate #include <sys/stat.h> 620Sstevel@tonic-gate #include <sys/statvfs.h> 630Sstevel@tonic-gate #include <assert.h> 640Sstevel@tonic-gate #include <sys/sockio.h> 650Sstevel@tonic-gate #include <sys/mntent.h> 660Sstevel@tonic-gate #include <limits.h> 671867Sgjelinek #include <dirent.h> 682303Scarlsonj #include <uuid/uuid.h> 694456Sss150715 #include <libdlpi.h> 700Sstevel@tonic-gate 710Sstevel@tonic-gate #include <fcntl.h> 720Sstevel@tonic-gate #include <door.h> 730Sstevel@tonic-gate #include <macros.h> 740Sstevel@tonic-gate #include <libgen.h> 751300Sgjelinek #include <fnmatch.h> 761931Sgjelinek #include <sys/modctl.h> 772712Snn35248 #include <libbrand.h> 783247Sgjelinek #include <libscf.h> 793352Sgjelinek #include <procfs.h> 803686Sgjelinek #include <strings.h> 810Sstevel@tonic-gate 820Sstevel@tonic-gate #include <pool.h> 830Sstevel@tonic-gate #include <sys/pool.h> 843247Sgjelinek #include <sys/priocntl.h> 853247Sgjelinek #include <sys/fsspriocntl.h> 860Sstevel@tonic-gate 871867Sgjelinek #include "zoneadm.h" 881867Sgjelinek 890Sstevel@tonic-gate #define MAXARGS 8 900Sstevel@tonic-gate 910Sstevel@tonic-gate /* Reflects kernel zone entries */ 920Sstevel@tonic-gate typedef struct zone_entry { 930Sstevel@tonic-gate zoneid_t zid; 940Sstevel@tonic-gate char zname[ZONENAME_MAX]; 950Sstevel@tonic-gate char *zstate_str; 960Sstevel@tonic-gate zone_state_t zstate_num; 972712Snn35248 char zbrand[MAXNAMELEN]; 980Sstevel@tonic-gate char zroot[MAXPATHLEN]; 992303Scarlsonj char zuuid[UUID_PRINTABLE_STRING_LENGTH]; 1003448Sdh155122 zone_iptype_t ziptype; 1010Sstevel@tonic-gate } zone_entry_t; 1020Sstevel@tonic-gate 1034350Std153743 #define CLUSTER_BRAND_NAME "cluster" 1044350Std153743 1050Sstevel@tonic-gate static zone_entry_t *zents; 1060Sstevel@tonic-gate static size_t nzents; 1072712Snn35248 static boolean_t is_native_zone = B_TRUE; 1084350Std153743 static boolean_t is_cluster_zone = B_FALSE; 1090Sstevel@tonic-gate 1101915Sgjelinek #define LOOPBACK_IF "lo0" 1111915Sgjelinek #define SOCKET_AF(af) (((af) == AF_UNSPEC) ? AF_INET : (af)) 1121915Sgjelinek 1131915Sgjelinek struct net_if { 1141915Sgjelinek char *name; 1151915Sgjelinek int af; 1161915Sgjelinek }; 1171915Sgjelinek 1180Sstevel@tonic-gate /* 0755 is the default directory mode. */ 1190Sstevel@tonic-gate #define DEFAULT_DIR_MODE \ 1200Sstevel@tonic-gate (S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) 1210Sstevel@tonic-gate 1220Sstevel@tonic-gate struct cmd { 1230Sstevel@tonic-gate uint_t cmd_num; /* command number */ 1240Sstevel@tonic-gate char *cmd_name; /* command name */ 1250Sstevel@tonic-gate char *short_usage; /* short form help */ 1260Sstevel@tonic-gate int (*handler)(int argc, char *argv[]); /* function to call */ 1270Sstevel@tonic-gate 1280Sstevel@tonic-gate }; 1290Sstevel@tonic-gate 1300Sstevel@tonic-gate #define SHELP_HELP "help" 1312267Sdp #define SHELP_BOOT "boot [-- boot_arguments]" 1320Sstevel@tonic-gate #define SHELP_HALT "halt" 1330Sstevel@tonic-gate #define SHELP_READY "ready" 1342267Sdp #define SHELP_REBOOT "reboot [-- boot_arguments]" 1350Sstevel@tonic-gate #define SHELP_LIST "list [-cipv]" 1360Sstevel@tonic-gate #define SHELP_VERIFY "verify" 1372712Snn35248 #define SHELP_INSTALL "install [-x nodataset] [brand-specific args]" 1380Sstevel@tonic-gate #define SHELP_UNINSTALL "uninstall [-F]" 1391867Sgjelinek #define SHELP_CLONE "clone [-m method] [-s <ZFS snapshot>] zonename" 1401300Sgjelinek #define SHELP_MOVE "move zonepath" 1412078Sgjelinek #define SHELP_DETACH "detach [-n]" 1422078Sgjelinek #define SHELP_ATTACH "attach [-F] [-n <path>]" 1432303Scarlsonj #define SHELP_MARK "mark incomplete" 1440Sstevel@tonic-gate 1452712Snn35248 #define EXEC_PREFIX "exec " 1462712Snn35248 #define EXEC_LEN (strlen(EXEC_PREFIX)) 1472712Snn35248 #define RMCOMMAND "/usr/bin/rm -rf" 1482712Snn35248 1492712Snn35248 static int cleanup_zonepath(char *, boolean_t); 1502712Snn35248 1513448Sdh155122 1520Sstevel@tonic-gate static int help_func(int argc, char *argv[]); 1530Sstevel@tonic-gate static int ready_func(int argc, char *argv[]); 1540Sstevel@tonic-gate static int boot_func(int argc, char *argv[]); 1550Sstevel@tonic-gate static int halt_func(int argc, char *argv[]); 1560Sstevel@tonic-gate static int reboot_func(int argc, char *argv[]); 1570Sstevel@tonic-gate static int list_func(int argc, char *argv[]); 1580Sstevel@tonic-gate static int verify_func(int argc, char *argv[]); 1590Sstevel@tonic-gate static int install_func(int argc, char *argv[]); 1600Sstevel@tonic-gate static int uninstall_func(int argc, char *argv[]); 161766Scarlsonj static int mount_func(int argc, char *argv[]); 162766Scarlsonj static int unmount_func(int argc, char *argv[]); 1631300Sgjelinek static int clone_func(int argc, char *argv[]); 1641300Sgjelinek static int move_func(int argc, char *argv[]); 1651507Sgjelinek static int detach_func(int argc, char *argv[]); 1661507Sgjelinek static int attach_func(int argc, char *argv[]); 1672303Scarlsonj static int mark_func(int argc, char *argv[]); 1683247Sgjelinek static int apply_func(int argc, char *argv[]); 1690Sstevel@tonic-gate static int sanity_check(char *zone, int cmd_num, boolean_t running, 1702712Snn35248 boolean_t unsafe_when_running, boolean_t force); 1710Sstevel@tonic-gate static int cmd_match(char *cmd); 1723339Szt129084 static int verify_details(int, char *argv[]); 1733339Szt129084 static int verify_brand(zone_dochandle_t, int, char *argv[]); 1743339Szt129084 static int invoke_brand_handler(int, char *argv[]); 1750Sstevel@tonic-gate 1760Sstevel@tonic-gate static struct cmd cmdtab[] = { 1770Sstevel@tonic-gate { CMD_HELP, "help", SHELP_HELP, help_func }, 1780Sstevel@tonic-gate { CMD_BOOT, "boot", SHELP_BOOT, boot_func }, 1790Sstevel@tonic-gate { CMD_HALT, "halt", SHELP_HALT, halt_func }, 1800Sstevel@tonic-gate { CMD_READY, "ready", SHELP_READY, ready_func }, 1810Sstevel@tonic-gate { CMD_REBOOT, "reboot", SHELP_REBOOT, reboot_func }, 1820Sstevel@tonic-gate { CMD_LIST, "list", SHELP_LIST, list_func }, 1830Sstevel@tonic-gate { CMD_VERIFY, "verify", SHELP_VERIFY, verify_func }, 1840Sstevel@tonic-gate { CMD_INSTALL, "install", SHELP_INSTALL, install_func }, 1850Sstevel@tonic-gate { CMD_UNINSTALL, "uninstall", SHELP_UNINSTALL, 186766Scarlsonj uninstall_func }, 1871300Sgjelinek /* mount and unmount are private commands for admin/install */ 188766Scarlsonj { CMD_MOUNT, "mount", NULL, mount_func }, 1891300Sgjelinek { CMD_UNMOUNT, "unmount", NULL, unmount_func }, 1901300Sgjelinek { CMD_CLONE, "clone", SHELP_CLONE, clone_func }, 1911507Sgjelinek { CMD_MOVE, "move", SHELP_MOVE, move_func }, 1921507Sgjelinek { CMD_DETACH, "detach", SHELP_DETACH, detach_func }, 1932303Scarlsonj { CMD_ATTACH, "attach", SHELP_ATTACH, attach_func }, 1943247Sgjelinek { CMD_MARK, "mark", SHELP_MARK, mark_func }, 1953247Sgjelinek { CMD_APPLY, "apply", NULL, apply_func } 1960Sstevel@tonic-gate }; 1970Sstevel@tonic-gate 1980Sstevel@tonic-gate /* global variables */ 1990Sstevel@tonic-gate 2000Sstevel@tonic-gate /* set early in main(), never modified thereafter, used all over the place */ 2010Sstevel@tonic-gate static char *execname; 2022712Snn35248 static char target_brand[MAXNAMELEN]; 2030Sstevel@tonic-gate static char *locale; 2041867Sgjelinek char *target_zone; 2052303Scarlsonj static char *target_uuid; 2060Sstevel@tonic-gate 2070Sstevel@tonic-gate /* used in do_subproc() and signal handler */ 2080Sstevel@tonic-gate static volatile boolean_t child_killed; 2092712Snn35248 static int do_subproc_cnt = 0; 2102712Snn35248 2112712Snn35248 /* 2122712Snn35248 * Used to indicate whether this zoneadm instance has another zoneadm 2132712Snn35248 * instance in its ancestry. 2142712Snn35248 */ 2152712Snn35248 static boolean_t zoneadm_is_nested = B_FALSE; 2162712Snn35248 2172712Snn35248 /* used to track nested zone-lock operations */ 2182712Snn35248 static int zone_lock_cnt = 0; 2192712Snn35248 2202712Snn35248 /* used to communicate lock status to children */ 2212712Snn35248 #define LOCK_ENV_VAR "_ZONEADM_LOCK_HELD" 2222712Snn35248 static char zoneadm_lock_held[] = LOCK_ENV_VAR"=1"; 2232712Snn35248 static char zoneadm_lock_not_held[] = LOCK_ENV_VAR"=0"; 2240Sstevel@tonic-gate 2251867Sgjelinek char * 2260Sstevel@tonic-gate cmd_to_str(int cmd_num) 2270Sstevel@tonic-gate { 2280Sstevel@tonic-gate assert(cmd_num >= CMD_MIN && cmd_num <= CMD_MAX); 2290Sstevel@tonic-gate return (cmdtab[cmd_num].cmd_name); 2300Sstevel@tonic-gate } 2310Sstevel@tonic-gate 2320Sstevel@tonic-gate /* This is a separate function because of gettext() wrapping. */ 2330Sstevel@tonic-gate static char * 2340Sstevel@tonic-gate long_help(int cmd_num) 2350Sstevel@tonic-gate { 236222Scomay assert(cmd_num >= CMD_MIN && cmd_num <= CMD_MAX); 2370Sstevel@tonic-gate switch (cmd_num) { 2381634Sgjelinek case CMD_HELP: 2391634Sgjelinek return (gettext("Print usage message.")); 2401634Sgjelinek case CMD_BOOT: 2412267Sdp return (gettext("Activates (boots) specified zone. See " 2422267Sdp "zoneadm(1m) for valid boot\n\targuments.")); 2431634Sgjelinek case CMD_HALT: 2441634Sgjelinek return (gettext("Halts specified zone, bypassing shutdown " 2451634Sgjelinek "scripts and removing runtime\n\tresources of the zone.")); 2461634Sgjelinek case CMD_READY: 2471634Sgjelinek return (gettext("Prepares a zone for running applications but " 2481634Sgjelinek "does not start any user\n\tprocesses in the zone.")); 2491634Sgjelinek case CMD_REBOOT: 2501634Sgjelinek return (gettext("Restarts the zone (equivalent to a halt / " 2512267Sdp "boot sequence).\n\tFails if the zone is not active. " 2522267Sdp "See zoneadm(1m) for valid boot\n\targuments.")); 2531634Sgjelinek case CMD_LIST: 2541634Sgjelinek return (gettext("Lists the current zones, or a " 2551634Sgjelinek "specific zone if indicated. By default,\n\tall " 2561634Sgjelinek "running zones are listed, though this can be " 2571634Sgjelinek "expanded to all\n\tinstalled zones with the -i " 2581634Sgjelinek "option or all configured zones with the\n\t-c " 2592303Scarlsonj "option. When used with the general -z <zone> and/or -u " 2602303Scarlsonj "<uuid-match>\n\toptions, lists only the specified " 2612303Scarlsonj "matching zone, but lists it\n\tregardless of its state, " 2622303Scarlsonj "and the -i and -c options are disallowed. The\n\t-v " 2632303Scarlsonj "option can be used to display verbose information: zone " 2642303Scarlsonj "name, id,\n\tcurrent state, root directory and options. " 2652303Scarlsonj "The -p option can be used\n\tto request machine-parsable " 2662303Scarlsonj "output. The -v and -p options are mutually\n\texclusive." 2672303Scarlsonj " If neither -v nor -p is used, just the zone name is " 2682303Scarlsonj "listed.")); 2691634Sgjelinek case CMD_VERIFY: 2701634Sgjelinek return (gettext("Check to make sure the configuration " 2711634Sgjelinek "can safely be instantiated\n\ton the machine: " 2721634Sgjelinek "physical network interfaces exist, etc.")); 2731634Sgjelinek case CMD_INSTALL: 2741867Sgjelinek return (gettext("Install the configuration on to the system. " 2751867Sgjelinek "The -x nodataset option\n\tcan be used to prevent the " 2761867Sgjelinek "creation of a new ZFS file system for the\n\tzone " 2772712Snn35248 "(assuming the zonepath is within a ZFS file system).\n\t" 2782712Snn35248 "All other arguments are passed to the brand installation " 2792712Snn35248 "function;\n\tsee brand(4) for more information.")); 2801634Sgjelinek case CMD_UNINSTALL: 2811634Sgjelinek return (gettext("Uninstall the configuration from the system. " 2821634Sgjelinek "The -F flag can be used\n\tto force the action.")); 2831634Sgjelinek case CMD_CLONE: 2841867Sgjelinek return (gettext("Clone the installation of another zone. " 2851867Sgjelinek "The -m option can be used to\n\tspecify 'copy' which " 2861867Sgjelinek "forces a copy of the source zone. The -s option\n\t" 2871867Sgjelinek "can be used to specify the name of a ZFS snapshot " 2881867Sgjelinek "that was taken from\n\ta previous clone command. The " 2891867Sgjelinek "snapshot will be used as the source\n\tinstead of " 2901867Sgjelinek "creating a new ZFS snapshot.")); 2911634Sgjelinek case CMD_MOVE: 2921634Sgjelinek return (gettext("Move the zone to a new zonepath.")); 2931634Sgjelinek case CMD_DETACH: 2941634Sgjelinek return (gettext("Detach the zone from the system. The zone " 2951634Sgjelinek "state is changed to\n\t'configured' (but the files under " 2961634Sgjelinek "the zonepath are untouched).\n\tThe zone can subsequently " 2971634Sgjelinek "be attached, or can be moved to another\n\tsystem and " 2982078Sgjelinek "attached there. The -n option can be used to specify\n\t" 2992078Sgjelinek "'no-execute' mode. When -n is used, the information " 3002078Sgjelinek "needed to attach\n\tthe zone is sent to standard output " 3012078Sgjelinek "but the zone is not actually\n\tdetached.")); 3021634Sgjelinek case CMD_ATTACH: 3031634Sgjelinek return (gettext("Attach the zone to the system. The zone " 3041634Sgjelinek "state must be 'configured'\n\tprior to attach; upon " 3051634Sgjelinek "successful completion, the zone state will be\n\t" 3061634Sgjelinek "'installed'. The system software on the current " 3071634Sgjelinek "system must be\n\tcompatible with the software on the " 3081634Sgjelinek "zone's original system.\n\tSpecify -F to force the attach " 3092078Sgjelinek "and skip software compatibility tests.\n\tThe -n option " 3102078Sgjelinek "can be used to specify 'no-execute' mode. When -n is\n\t" 3112078Sgjelinek "used, the information needed to attach the zone is read " 3122078Sgjelinek "from the\n\tspecified path and the configuration is only " 3132078Sgjelinek "validated. The path can\n\tbe '-' to specify standard " 3142078Sgjelinek "input.")); 3152303Scarlsonj case CMD_MARK: 3162303Scarlsonj return (gettext("Set the state of the zone. This can be used " 3172303Scarlsonj "to force the zone\n\tstate to 'incomplete' " 3182303Scarlsonj "administratively if some activity has rendered\n\tthe " 3192303Scarlsonj "zone permanently unusable. The only valid state that " 3202303Scarlsonj "may be\n\tspecified is 'incomplete'.")); 3211634Sgjelinek default: 3221634Sgjelinek return (""); 3230Sstevel@tonic-gate } 3240Sstevel@tonic-gate /* NOTREACHED */ 325222Scomay return (NULL); 3260Sstevel@tonic-gate } 3270Sstevel@tonic-gate 3280Sstevel@tonic-gate /* 3290Sstevel@tonic-gate * Called with explicit B_TRUE when help is explicitly requested, B_FALSE for 3300Sstevel@tonic-gate * unexpected errors. 3310Sstevel@tonic-gate */ 3320Sstevel@tonic-gate 3330Sstevel@tonic-gate static int 3340Sstevel@tonic-gate usage(boolean_t explicit) 3350Sstevel@tonic-gate { 3360Sstevel@tonic-gate int i; 3370Sstevel@tonic-gate FILE *fd = explicit ? stdout : stderr; 3380Sstevel@tonic-gate 3390Sstevel@tonic-gate (void) fprintf(fd, "%s:\t%s help\n", gettext("usage"), execname); 3402303Scarlsonj (void) fprintf(fd, "\t%s [-z <zone>] [-u <uuid-match>] list\n", 3412303Scarlsonj execname); 3422303Scarlsonj (void) fprintf(fd, "\t%s {-z <zone>|-u <uuid-match>} <%s>\n", execname, 3430Sstevel@tonic-gate gettext("subcommand")); 3440Sstevel@tonic-gate (void) fprintf(fd, "\n%s:\n\n", gettext("Subcommands")); 3450Sstevel@tonic-gate for (i = CMD_MIN; i <= CMD_MAX; i++) { 346766Scarlsonj if (cmdtab[i].short_usage == NULL) 347766Scarlsonj continue; 3480Sstevel@tonic-gate (void) fprintf(fd, "%s\n", cmdtab[i].short_usage); 3490Sstevel@tonic-gate if (explicit) 3500Sstevel@tonic-gate (void) fprintf(fd, "\t%s\n\n", long_help(i)); 3510Sstevel@tonic-gate } 3520Sstevel@tonic-gate if (!explicit) 3530Sstevel@tonic-gate (void) fputs("\n", fd); 3540Sstevel@tonic-gate return (Z_USAGE); 3550Sstevel@tonic-gate } 3560Sstevel@tonic-gate 3570Sstevel@tonic-gate static void 3580Sstevel@tonic-gate sub_usage(char *short_usage, int cmd_num) 3590Sstevel@tonic-gate { 3600Sstevel@tonic-gate (void) fprintf(stderr, "%s:\t%s\n", gettext("usage"), short_usage); 3610Sstevel@tonic-gate (void) fprintf(stderr, "\t%s\n", long_help(cmd_num)); 3620Sstevel@tonic-gate } 3630Sstevel@tonic-gate 3640Sstevel@tonic-gate /* 3650Sstevel@tonic-gate * zperror() is like perror(3c) except that this also prints the executable 3660Sstevel@tonic-gate * name at the start of the message, and takes a boolean indicating whether 3670Sstevel@tonic-gate * to call libc'c strerror() or that from libzonecfg. 3680Sstevel@tonic-gate */ 3690Sstevel@tonic-gate 3701867Sgjelinek void 3710Sstevel@tonic-gate zperror(const char *str, boolean_t zonecfg_error) 3720Sstevel@tonic-gate { 3730Sstevel@tonic-gate (void) fprintf(stderr, "%s: %s: %s\n", execname, str, 3740Sstevel@tonic-gate zonecfg_error ? zonecfg_strerror(errno) : strerror(errno)); 3750Sstevel@tonic-gate } 3760Sstevel@tonic-gate 3770Sstevel@tonic-gate /* 3780Sstevel@tonic-gate * zperror2() is very similar to zperror() above, except it also prints a 3790Sstevel@tonic-gate * supplied zone name after the executable. 3800Sstevel@tonic-gate * 3810Sstevel@tonic-gate * All current consumers of this function want libzonecfg's strerror() rather 3820Sstevel@tonic-gate * than libc's; if this ever changes, this function can be made more generic 3830Sstevel@tonic-gate * like zperror() above. 3840Sstevel@tonic-gate */ 3850Sstevel@tonic-gate 3861867Sgjelinek void 3870Sstevel@tonic-gate zperror2(const char *zone, const char *str) 3880Sstevel@tonic-gate { 3890Sstevel@tonic-gate (void) fprintf(stderr, "%s: %s: %s: %s\n", execname, zone, str, 3900Sstevel@tonic-gate zonecfg_strerror(errno)); 3910Sstevel@tonic-gate } 3920Sstevel@tonic-gate 3930Sstevel@tonic-gate /* PRINTFLIKE1 */ 3941867Sgjelinek void 3950Sstevel@tonic-gate zerror(const char *fmt, ...) 3960Sstevel@tonic-gate { 3970Sstevel@tonic-gate va_list alist; 3980Sstevel@tonic-gate 3990Sstevel@tonic-gate va_start(alist, fmt); 4000Sstevel@tonic-gate (void) fprintf(stderr, "%s: ", execname); 4010Sstevel@tonic-gate if (target_zone != NULL) 4020Sstevel@tonic-gate (void) fprintf(stderr, "zone '%s': ", target_zone); 4030Sstevel@tonic-gate (void) vfprintf(stderr, fmt, alist); 4040Sstevel@tonic-gate (void) fprintf(stderr, "\n"); 4050Sstevel@tonic-gate va_end(alist); 4060Sstevel@tonic-gate } 4070Sstevel@tonic-gate 4080Sstevel@tonic-gate static void * 4090Sstevel@tonic-gate safe_calloc(size_t nelem, size_t elsize) 4100Sstevel@tonic-gate { 4110Sstevel@tonic-gate void *r = calloc(nelem, elsize); 4120Sstevel@tonic-gate 4130Sstevel@tonic-gate if (r == NULL) { 4140Sstevel@tonic-gate zerror(gettext("failed to allocate %lu bytes: %s"), 4150Sstevel@tonic-gate (ulong_t)nelem * elsize, strerror(errno)); 4160Sstevel@tonic-gate exit(Z_ERR); 4170Sstevel@tonic-gate } 4180Sstevel@tonic-gate return (r); 4190Sstevel@tonic-gate } 4200Sstevel@tonic-gate 4210Sstevel@tonic-gate static void 4220Sstevel@tonic-gate zone_print(zone_entry_t *zent, boolean_t verbose, boolean_t parsable) 4230Sstevel@tonic-gate { 4240Sstevel@tonic-gate static boolean_t firsttime = B_TRUE; 4253448Sdh155122 char *ip_type_str; 4263448Sdh155122 4273448Sdh155122 if (zent->ziptype == ZS_EXCLUSIVE) 4283448Sdh155122 ip_type_str = "excl"; 4293448Sdh155122 else 4303448Sdh155122 ip_type_str = "shared"; 4310Sstevel@tonic-gate 4320Sstevel@tonic-gate assert(!(verbose && parsable)); 4330Sstevel@tonic-gate if (firsttime && verbose) { 4340Sstevel@tonic-gate firsttime = B_FALSE; 4353448Sdh155122 (void) printf("%*s %-16s %-10s %-30s %-8s %-6s\n", 4363448Sdh155122 ZONEID_WIDTH, "ID", "NAME", "STATUS", "PATH", "BRAND", 4373448Sdh155122 "IP"); 4380Sstevel@tonic-gate } 4390Sstevel@tonic-gate if (!verbose) { 4402303Scarlsonj char *cp, *clim; 4412303Scarlsonj 4420Sstevel@tonic-gate if (!parsable) { 4430Sstevel@tonic-gate (void) printf("%s\n", zent->zname); 4440Sstevel@tonic-gate return; 4450Sstevel@tonic-gate } 4460Sstevel@tonic-gate if (zent->zid == ZONE_ID_UNDEFINED) 4470Sstevel@tonic-gate (void) printf("-"); 4480Sstevel@tonic-gate else 4490Sstevel@tonic-gate (void) printf("%lu", zent->zid); 4502303Scarlsonj (void) printf(":%s:%s:", zent->zname, zent->zstate_str); 4512303Scarlsonj cp = zent->zroot; 4522303Scarlsonj while ((clim = strchr(cp, ':')) != NULL) { 4532303Scarlsonj (void) printf("%.*s\\:", clim - cp, cp); 4542303Scarlsonj cp = clim + 1; 4552303Scarlsonj } 4563448Sdh155122 (void) printf("%s:%s:%s:%s\n", cp, zent->zuuid, zent->zbrand, 4573448Sdh155122 ip_type_str); 4580Sstevel@tonic-gate return; 4590Sstevel@tonic-gate } 4600Sstevel@tonic-gate if (zent->zstate_str != NULL) { 4610Sstevel@tonic-gate if (zent->zid == ZONE_ID_UNDEFINED) 4620Sstevel@tonic-gate (void) printf("%*s", ZONEID_WIDTH, "-"); 4630Sstevel@tonic-gate else 4640Sstevel@tonic-gate (void) printf("%*lu", ZONEID_WIDTH, zent->zid); 4653448Sdh155122 (void) printf(" %-16s %-10s %-30s %-8s %-6s\n", zent->zname, 4663448Sdh155122 zent->zstate_str, zent->zroot, zent->zbrand, ip_type_str); 4670Sstevel@tonic-gate } 4680Sstevel@tonic-gate } 4690Sstevel@tonic-gate 4700Sstevel@tonic-gate static int 471766Scarlsonj lookup_zone_info(const char *zone_name, zoneid_t zid, zone_entry_t *zent) 4720Sstevel@tonic-gate { 4731676Sjpk char root[MAXPATHLEN], *cp; 4740Sstevel@tonic-gate int err; 4752303Scarlsonj uuid_t uuid; 4760Sstevel@tonic-gate 4770Sstevel@tonic-gate (void) strlcpy(zent->zname, zone_name, sizeof (zent->zname)); 4780Sstevel@tonic-gate (void) strlcpy(zent->zroot, "???", sizeof (zent->zroot)); 4792712Snn35248 (void) strlcpy(zent->zbrand, "???", sizeof (zent->zbrand)); 4800Sstevel@tonic-gate zent->zstate_str = "???"; 4810Sstevel@tonic-gate 482766Scarlsonj zent->zid = zid; 4830Sstevel@tonic-gate 4842303Scarlsonj if (zonecfg_get_uuid(zone_name, uuid) == Z_OK && 4852303Scarlsonj !uuid_is_null(uuid)) 4862303Scarlsonj uuid_unparse(uuid, zent->zuuid); 4872303Scarlsonj else 4882303Scarlsonj zent->zuuid[0] = '\0'; 4892303Scarlsonj 4901676Sjpk /* 4911676Sjpk * For labeled zones which query the zone path of lower-level 4921676Sjpk * zones, the path needs to be adjusted to drop the final 4931676Sjpk * "/root" component. This adjusted path is then useful 4941676Sjpk * for reading down any exported directories from the 4951676Sjpk * lower-level zone. 4961676Sjpk */ 4971676Sjpk if (is_system_labeled() && zent->zid != ZONE_ID_UNDEFINED) { 4981676Sjpk if (zone_getattr(zent->zid, ZONE_ATTR_ROOT, zent->zroot, 4991676Sjpk sizeof (zent->zroot)) == -1) { 5001676Sjpk zperror2(zent->zname, 5011676Sjpk gettext("could not get zone path.")); 5021676Sjpk return (Z_ERR); 5031676Sjpk } 5041676Sjpk cp = zent->zroot + strlen(zent->zroot) - 5; 5051676Sjpk if (cp > zent->zroot && strcmp(cp, "/root") == 0) 5061676Sjpk *cp = 0; 5071676Sjpk } else { 5081676Sjpk if ((err = zone_get_zonepath(zent->zname, root, 5091676Sjpk sizeof (root))) != Z_OK) { 5101676Sjpk errno = err; 5111676Sjpk zperror2(zent->zname, 5121676Sjpk gettext("could not get zone path.")); 5131676Sjpk return (Z_ERR); 5141676Sjpk } 5151676Sjpk (void) strlcpy(zent->zroot, root, sizeof (zent->zroot)); 5161676Sjpk } 5170Sstevel@tonic-gate 5180Sstevel@tonic-gate if ((err = zone_get_state(zent->zname, &zent->zstate_num)) != Z_OK) { 5190Sstevel@tonic-gate errno = err; 5200Sstevel@tonic-gate zperror2(zent->zname, gettext("could not get state")); 5210Sstevel@tonic-gate return (Z_ERR); 5220Sstevel@tonic-gate } 5230Sstevel@tonic-gate zent->zstate_str = zone_state_str(zent->zstate_num); 5243009Snn35248 5253009Snn35248 /* 5263009Snn35248 * A zone's brand is only available in the .xml file describing it, 5273009Snn35248 * which is only visible to the global zone. This causes 5283009Snn35248 * zone_get_brand() to fail when called from within a non-global 5293009Snn35248 * zone. Fortunately we only do this on labeled systems, where we 5303009Snn35248 * know all zones are native. 5313009Snn35248 */ 5323009Snn35248 if (getzoneid() != GLOBAL_ZONEID) { 5333009Snn35248 assert(is_system_labeled() != 0); 5343009Snn35248 (void) strlcpy(zent->zbrand, NATIVE_BRAND_NAME, 5353009Snn35248 sizeof (zent->zbrand)); 5363009Snn35248 } else if (zone_get_brand(zent->zname, zent->zbrand, 5372712Snn35248 sizeof (zent->zbrand)) != Z_OK) { 5382712Snn35248 zperror2(zent->zname, gettext("could not get brand name")); 5392712Snn35248 return (Z_ERR); 5402712Snn35248 } 5410Sstevel@tonic-gate 5423448Sdh155122 /* 5433448Sdh155122 * Get ip type of the zone. 5443448Sdh155122 * Note for global zone, ZS_SHARED is set always. 5453448Sdh155122 */ 5463448Sdh155122 if (zid == GLOBAL_ZONEID) { 5473448Sdh155122 zent->ziptype = ZS_SHARED; 5483448Sdh155122 } else { 5493448Sdh155122 5503448Sdh155122 if (zent->zstate_num == ZONE_STATE_RUNNING) { 5513448Sdh155122 ushort_t flags; 5523448Sdh155122 5533448Sdh155122 if (zone_getattr(zid, ZONE_ATTR_FLAGS, &flags, 5543448Sdh155122 sizeof (flags)) < 0) { 5553448Sdh155122 zperror2(zent->zname, 5563448Sdh155122 gettext("could not get zone flags")); 5573448Sdh155122 return (Z_ERR); 5583448Sdh155122 } 5593448Sdh155122 if (flags & ZF_NET_EXCL) 5603448Sdh155122 zent->ziptype = ZS_EXCLUSIVE; 5613448Sdh155122 else 5623448Sdh155122 zent->ziptype = ZS_SHARED; 5633448Sdh155122 } else { 5643448Sdh155122 zone_dochandle_t handle; 5653448Sdh155122 5663448Sdh155122 if ((handle = zonecfg_init_handle()) == NULL) { 5673448Sdh155122 zperror2(zent->zname, 5683448Sdh155122 gettext("could not init handle")); 5693448Sdh155122 return (Z_ERR); 5703448Sdh155122 } 5713448Sdh155122 if ((err = zonecfg_get_handle(zent->zname, handle)) 5723448Sdh155122 != Z_OK) { 5733448Sdh155122 zperror2(zent->zname, 5743448Sdh155122 gettext("could not get handle")); 5753448Sdh155122 zonecfg_fini_handle(handle); 5763448Sdh155122 return (Z_ERR); 5773448Sdh155122 } 5783448Sdh155122 5793448Sdh155122 if ((err = zonecfg_get_iptype(handle, &zent->ziptype)) 5803448Sdh155122 != Z_OK) { 5813448Sdh155122 zperror2(zent->zname, 5823448Sdh155122 gettext("could not get ip-type")); 5833448Sdh155122 zonecfg_fini_handle(handle); 5843448Sdh155122 return (Z_ERR); 5853448Sdh155122 } 5863448Sdh155122 zonecfg_fini_handle(handle); 5873448Sdh155122 } 5883448Sdh155122 } 5893448Sdh155122 5900Sstevel@tonic-gate return (Z_OK); 5910Sstevel@tonic-gate } 5920Sstevel@tonic-gate 5930Sstevel@tonic-gate /* 5940Sstevel@tonic-gate * fetch_zents() calls zone_list(2) to find out how many zones are running 5950Sstevel@tonic-gate * (which is stored in the global nzents), then calls zone_list(2) again 5960Sstevel@tonic-gate * to fetch the list of running zones (stored in the global zents). This 5970Sstevel@tonic-gate * function may be called multiple times, so if zents is already set, we 5980Sstevel@tonic-gate * return immediately to save work. 5990Sstevel@tonic-gate */ 6000Sstevel@tonic-gate 6010Sstevel@tonic-gate static int 602766Scarlsonj fetch_zents(void) 6030Sstevel@tonic-gate { 6040Sstevel@tonic-gate zoneid_t *zids = NULL; 6050Sstevel@tonic-gate uint_t nzents_saved; 606766Scarlsonj int i, retv; 607766Scarlsonj FILE *fp; 608766Scarlsonj boolean_t inaltroot; 609766Scarlsonj zone_entry_t *zentp; 6100Sstevel@tonic-gate 6110Sstevel@tonic-gate if (nzents > 0) 6120Sstevel@tonic-gate return (Z_OK); 6130Sstevel@tonic-gate 6140Sstevel@tonic-gate if (zone_list(NULL, &nzents) != 0) { 6150Sstevel@tonic-gate zperror(gettext("failed to get zoneid list"), B_FALSE); 6160Sstevel@tonic-gate return (Z_ERR); 6170Sstevel@tonic-gate } 6180Sstevel@tonic-gate 6190Sstevel@tonic-gate again: 6200Sstevel@tonic-gate if (nzents == 0) 6210Sstevel@tonic-gate return (Z_OK); 6220Sstevel@tonic-gate 6230Sstevel@tonic-gate zids = safe_calloc(nzents, sizeof (zoneid_t)); 6240Sstevel@tonic-gate nzents_saved = nzents; 6250Sstevel@tonic-gate 6260Sstevel@tonic-gate if (zone_list(zids, &nzents) != 0) { 6270Sstevel@tonic-gate zperror(gettext("failed to get zone list"), B_FALSE); 6280Sstevel@tonic-gate free(zids); 6290Sstevel@tonic-gate return (Z_ERR); 6300Sstevel@tonic-gate } 6310Sstevel@tonic-gate if (nzents != nzents_saved) { 6320Sstevel@tonic-gate /* list changed, try again */ 6330Sstevel@tonic-gate free(zids); 6340Sstevel@tonic-gate goto again; 6350Sstevel@tonic-gate } 6360Sstevel@tonic-gate 6370Sstevel@tonic-gate zents = safe_calloc(nzents, sizeof (zone_entry_t)); 6380Sstevel@tonic-gate 639766Scarlsonj inaltroot = zonecfg_in_alt_root(); 640766Scarlsonj if (inaltroot) 641766Scarlsonj fp = zonecfg_open_scratch("", B_FALSE); 642766Scarlsonj else 643766Scarlsonj fp = NULL; 644766Scarlsonj zentp = zents; 645766Scarlsonj retv = Z_OK; 6460Sstevel@tonic-gate for (i = 0; i < nzents; i++) { 6470Sstevel@tonic-gate char name[ZONENAME_MAX]; 648766Scarlsonj char altname[ZONENAME_MAX]; 6490Sstevel@tonic-gate 650766Scarlsonj if (getzonenamebyid(zids[i], name, sizeof (name)) < 0) { 6510Sstevel@tonic-gate zperror(gettext("failed to get zone name"), B_FALSE); 652766Scarlsonj retv = Z_ERR; 653766Scarlsonj continue; 654766Scarlsonj } 655766Scarlsonj if (zonecfg_is_scratch(name)) { 656766Scarlsonj /* Ignore scratch zones by default */ 657766Scarlsonj if (!inaltroot) 658766Scarlsonj continue; 659766Scarlsonj if (fp == NULL || 660766Scarlsonj zonecfg_reverse_scratch(fp, name, altname, 661766Scarlsonj sizeof (altname), NULL, 0) == -1) { 662924Sgjelinek zerror(gettext("could not resolve scratch " 663766Scarlsonj "zone %s"), name); 664766Scarlsonj retv = Z_ERR; 665766Scarlsonj continue; 666766Scarlsonj } 667766Scarlsonj (void) strcpy(name, altname); 668766Scarlsonj } else { 669766Scarlsonj /* Ignore non-scratch when in an alternate root */ 670766Scarlsonj if (inaltroot && strcmp(name, GLOBAL_ZONENAME) != 0) 671766Scarlsonj continue; 672766Scarlsonj } 673766Scarlsonj if (lookup_zone_info(name, zids[i], zentp) != Z_OK) { 674766Scarlsonj zerror(gettext("failed to get zone data")); 675766Scarlsonj retv = Z_ERR; 676766Scarlsonj continue; 677766Scarlsonj } 678766Scarlsonj zentp++; 6790Sstevel@tonic-gate } 680766Scarlsonj nzents = zentp - zents; 681766Scarlsonj if (fp != NULL) 682766Scarlsonj zonecfg_close_scratch(fp); 6830Sstevel@tonic-gate 6840Sstevel@tonic-gate free(zids); 685766Scarlsonj return (retv); 6860Sstevel@tonic-gate } 6870Sstevel@tonic-gate 688766Scarlsonj static int 6890Sstevel@tonic-gate zone_print_list(zone_state_t min_state, boolean_t verbose, boolean_t parsable) 6900Sstevel@tonic-gate { 6910Sstevel@tonic-gate int i; 6920Sstevel@tonic-gate zone_entry_t zent; 6930Sstevel@tonic-gate FILE *cookie; 6940Sstevel@tonic-gate char *name; 6950Sstevel@tonic-gate 6960Sstevel@tonic-gate /* 6970Sstevel@tonic-gate * First get the list of running zones from the kernel and print them. 6980Sstevel@tonic-gate * If that is all we need, then return. 6990Sstevel@tonic-gate */ 700766Scarlsonj if ((i = fetch_zents()) != Z_OK) { 7010Sstevel@tonic-gate /* 7020Sstevel@tonic-gate * No need for error messages; fetch_zents() has already taken 7030Sstevel@tonic-gate * care of this. 7040Sstevel@tonic-gate */ 705766Scarlsonj return (i); 7060Sstevel@tonic-gate } 707766Scarlsonj for (i = 0; i < nzents; i++) 7080Sstevel@tonic-gate zone_print(&zents[i], verbose, parsable); 7090Sstevel@tonic-gate if (min_state >= ZONE_STATE_RUNNING) 710766Scarlsonj return (Z_OK); 7110Sstevel@tonic-gate /* 7120Sstevel@tonic-gate * Next, get the full list of zones from the configuration, skipping 7130Sstevel@tonic-gate * any we have already printed. 7140Sstevel@tonic-gate */ 7150Sstevel@tonic-gate cookie = setzoneent(); 7160Sstevel@tonic-gate while ((name = getzoneent(cookie)) != NULL) { 7170Sstevel@tonic-gate for (i = 0; i < nzents; i++) { 7180Sstevel@tonic-gate if (strcmp(zents[i].zname, name) == 0) 7190Sstevel@tonic-gate break; 7200Sstevel@tonic-gate } 7210Sstevel@tonic-gate if (i < nzents) { 7220Sstevel@tonic-gate free(name); 7230Sstevel@tonic-gate continue; 7240Sstevel@tonic-gate } 725766Scarlsonj if (lookup_zone_info(name, ZONE_ID_UNDEFINED, &zent) != Z_OK) { 7260Sstevel@tonic-gate free(name); 7270Sstevel@tonic-gate continue; 7280Sstevel@tonic-gate } 7290Sstevel@tonic-gate free(name); 7300Sstevel@tonic-gate if (zent.zstate_num >= min_state) 7310Sstevel@tonic-gate zone_print(&zent, verbose, parsable); 7320Sstevel@tonic-gate } 7330Sstevel@tonic-gate endzoneent(cookie); 734766Scarlsonj return (Z_OK); 7350Sstevel@tonic-gate } 7360Sstevel@tonic-gate 7370Sstevel@tonic-gate static zone_entry_t * 7380Sstevel@tonic-gate lookup_running_zone(char *str) 7390Sstevel@tonic-gate { 7400Sstevel@tonic-gate zoneid_t zoneid; 7410Sstevel@tonic-gate char *cp; 7420Sstevel@tonic-gate int i; 7430Sstevel@tonic-gate 7440Sstevel@tonic-gate if (fetch_zents() != Z_OK) 7450Sstevel@tonic-gate return (NULL); 7460Sstevel@tonic-gate 7470Sstevel@tonic-gate for (i = 0; i < nzents; i++) { 7480Sstevel@tonic-gate if (strcmp(str, zents[i].zname) == 0) 7490Sstevel@tonic-gate return (&zents[i]); 7500Sstevel@tonic-gate } 7510Sstevel@tonic-gate errno = 0; 7520Sstevel@tonic-gate zoneid = strtol(str, &cp, 0); 7530Sstevel@tonic-gate if (zoneid < MIN_ZONEID || zoneid > MAX_ZONEID || 7540Sstevel@tonic-gate errno != 0 || *cp != '\0') 7550Sstevel@tonic-gate return (NULL); 7560Sstevel@tonic-gate for (i = 0; i < nzents; i++) { 7570Sstevel@tonic-gate if (zoneid == zents[i].zid) 7580Sstevel@tonic-gate return (&zents[i]); 7590Sstevel@tonic-gate } 7600Sstevel@tonic-gate return (NULL); 7610Sstevel@tonic-gate } 7620Sstevel@tonic-gate 7630Sstevel@tonic-gate /* 7640Sstevel@tonic-gate * Check a bit in a mode_t: if on is B_TRUE, that bit should be on; if 7650Sstevel@tonic-gate * B_FALSE, it should be off. Return B_TRUE if the mode is bad (incorrect). 7660Sstevel@tonic-gate */ 7670Sstevel@tonic-gate static boolean_t 7680Sstevel@tonic-gate bad_mode_bit(mode_t mode, mode_t bit, boolean_t on, char *file) 7690Sstevel@tonic-gate { 7700Sstevel@tonic-gate char *str; 7710Sstevel@tonic-gate 7720Sstevel@tonic-gate assert(bit == S_IRUSR || bit == S_IWUSR || bit == S_IXUSR || 7730Sstevel@tonic-gate bit == S_IRGRP || bit == S_IWGRP || bit == S_IXGRP || 7740Sstevel@tonic-gate bit == S_IROTH || bit == S_IWOTH || bit == S_IXOTH); 7750Sstevel@tonic-gate /* 7760Sstevel@tonic-gate * TRANSLATION_NOTE 7770Sstevel@tonic-gate * The strings below will be used as part of a larger message, 7780Sstevel@tonic-gate * either: 7790Sstevel@tonic-gate * (file name) must be (owner|group|world) (read|writ|execut)able 7800Sstevel@tonic-gate * or 7810Sstevel@tonic-gate * (file name) must not be (owner|group|world) (read|writ|execut)able 7820Sstevel@tonic-gate */ 7830Sstevel@tonic-gate switch (bit) { 7840Sstevel@tonic-gate case S_IRUSR: 7850Sstevel@tonic-gate str = gettext("owner readable"); 7860Sstevel@tonic-gate break; 7870Sstevel@tonic-gate case S_IWUSR: 7880Sstevel@tonic-gate str = gettext("owner writable"); 7890Sstevel@tonic-gate break; 7900Sstevel@tonic-gate case S_IXUSR: 7910Sstevel@tonic-gate str = gettext("owner executable"); 7920Sstevel@tonic-gate break; 7930Sstevel@tonic-gate case S_IRGRP: 7940Sstevel@tonic-gate str = gettext("group readable"); 7950Sstevel@tonic-gate break; 7960Sstevel@tonic-gate case S_IWGRP: 7970Sstevel@tonic-gate str = gettext("group writable"); 7980Sstevel@tonic-gate break; 7990Sstevel@tonic-gate case S_IXGRP: 8000Sstevel@tonic-gate str = gettext("group executable"); 8010Sstevel@tonic-gate break; 8020Sstevel@tonic-gate case S_IROTH: 8030Sstevel@tonic-gate str = gettext("world readable"); 8040Sstevel@tonic-gate break; 8050Sstevel@tonic-gate case S_IWOTH: 8060Sstevel@tonic-gate str = gettext("world writable"); 8070Sstevel@tonic-gate break; 8080Sstevel@tonic-gate case S_IXOTH: 8090Sstevel@tonic-gate str = gettext("world executable"); 8100Sstevel@tonic-gate break; 8110Sstevel@tonic-gate } 8120Sstevel@tonic-gate if ((mode & bit) == (on ? 0 : bit)) { 8130Sstevel@tonic-gate /* 8140Sstevel@tonic-gate * TRANSLATION_NOTE 8150Sstevel@tonic-gate * The first parameter below is a file name; the second 8160Sstevel@tonic-gate * is one of the "(owner|group|world) (read|writ|execut)able" 8170Sstevel@tonic-gate * strings from above. 8180Sstevel@tonic-gate */ 8190Sstevel@tonic-gate /* 8200Sstevel@tonic-gate * The code below could be simplified but not in a way 8210Sstevel@tonic-gate * that would easily translate to non-English locales. 8220Sstevel@tonic-gate */ 8230Sstevel@tonic-gate if (on) { 8240Sstevel@tonic-gate (void) fprintf(stderr, gettext("%s must be %s.\n"), 8250Sstevel@tonic-gate file, str); 8260Sstevel@tonic-gate } else { 8270Sstevel@tonic-gate (void) fprintf(stderr, gettext("%s must not be %s.\n"), 8280Sstevel@tonic-gate file, str); 8290Sstevel@tonic-gate } 8300Sstevel@tonic-gate return (B_TRUE); 8310Sstevel@tonic-gate } 8320Sstevel@tonic-gate return (B_FALSE); 8330Sstevel@tonic-gate } 8340Sstevel@tonic-gate 8350Sstevel@tonic-gate /* 8360Sstevel@tonic-gate * We want to make sure that no zone has its zone path as a child node 8370Sstevel@tonic-gate * (in the directory sense) of any other. We do that by comparing this 8380Sstevel@tonic-gate * zone's path to the path of all other (non-global) zones. The comparison 8390Sstevel@tonic-gate * in each case is simple: add '/' to the end of the path, then do a 8400Sstevel@tonic-gate * strncmp() of the two paths, using the length of the shorter one. 8410Sstevel@tonic-gate */ 8420Sstevel@tonic-gate 8430Sstevel@tonic-gate static int 8440Sstevel@tonic-gate crosscheck_zonepaths(char *path) 8450Sstevel@tonic-gate { 8460Sstevel@tonic-gate char rpath[MAXPATHLEN]; /* resolved path */ 8470Sstevel@tonic-gate char path_copy[MAXPATHLEN]; /* copy of original path */ 8480Sstevel@tonic-gate char rpath_copy[MAXPATHLEN]; /* copy of original rpath */ 8490Sstevel@tonic-gate struct zoneent *ze; 8500Sstevel@tonic-gate int res, err; 8510Sstevel@tonic-gate FILE *cookie; 8520Sstevel@tonic-gate 8530Sstevel@tonic-gate cookie = setzoneent(); 8540Sstevel@tonic-gate while ((ze = getzoneent_private(cookie)) != NULL) { 8550Sstevel@tonic-gate /* Skip zones which are not installed. */ 8560Sstevel@tonic-gate if (ze->zone_state < ZONE_STATE_INSTALLED) { 8570Sstevel@tonic-gate free(ze); 8580Sstevel@tonic-gate continue; 8590Sstevel@tonic-gate } 8600Sstevel@tonic-gate /* Skip the global zone and the current target zone. */ 8610Sstevel@tonic-gate if (strcmp(ze->zone_name, GLOBAL_ZONENAME) == 0 || 8620Sstevel@tonic-gate strcmp(ze->zone_name, target_zone) == 0) { 8630Sstevel@tonic-gate free(ze); 8640Sstevel@tonic-gate continue; 8650Sstevel@tonic-gate } 8660Sstevel@tonic-gate if (strlen(ze->zone_path) == 0) { 8670Sstevel@tonic-gate /* old index file without path, fall back */ 8680Sstevel@tonic-gate if ((err = zone_get_zonepath(ze->zone_name, 8690Sstevel@tonic-gate ze->zone_path, sizeof (ze->zone_path))) != Z_OK) { 8700Sstevel@tonic-gate errno = err; 8710Sstevel@tonic-gate zperror2(ze->zone_name, 8720Sstevel@tonic-gate gettext("could not get zone path")); 8730Sstevel@tonic-gate free(ze); 8740Sstevel@tonic-gate continue; 8750Sstevel@tonic-gate } 8760Sstevel@tonic-gate } 877766Scarlsonj (void) snprintf(path_copy, sizeof (path_copy), "%s%s", 878766Scarlsonj zonecfg_get_root(), ze->zone_path); 879766Scarlsonj res = resolvepath(path_copy, rpath, sizeof (rpath)); 8800Sstevel@tonic-gate if (res == -1) { 8810Sstevel@tonic-gate if (errno != ENOENT) { 882766Scarlsonj zperror(path_copy, B_FALSE); 8830Sstevel@tonic-gate free(ze); 8840Sstevel@tonic-gate return (Z_ERR); 8850Sstevel@tonic-gate } 8860Sstevel@tonic-gate (void) printf(gettext("WARNING: zone %s is installed, " 8870Sstevel@tonic-gate "but its %s %s does not exist.\n"), ze->zone_name, 888766Scarlsonj "zonepath", path_copy); 8890Sstevel@tonic-gate free(ze); 8900Sstevel@tonic-gate continue; 8910Sstevel@tonic-gate } 8920Sstevel@tonic-gate rpath[res] = '\0'; 8930Sstevel@tonic-gate (void) snprintf(path_copy, sizeof (path_copy), "%s/", path); 8940Sstevel@tonic-gate (void) snprintf(rpath_copy, sizeof (rpath_copy), "%s/", rpath); 8950Sstevel@tonic-gate if (strncmp(path_copy, rpath_copy, 8960Sstevel@tonic-gate min(strlen(path_copy), strlen(rpath_copy))) == 0) { 897924Sgjelinek /* 898924Sgjelinek * TRANSLATION_NOTE 899924Sgjelinek * zonepath is a literal that should not be translated. 900924Sgjelinek */ 9010Sstevel@tonic-gate (void) fprintf(stderr, gettext("%s zonepath (%s) and " 9020Sstevel@tonic-gate "%s zonepath (%s) overlap.\n"), 9030Sstevel@tonic-gate target_zone, path, ze->zone_name, rpath); 9040Sstevel@tonic-gate free(ze); 9050Sstevel@tonic-gate return (Z_ERR); 9060Sstevel@tonic-gate } 9070Sstevel@tonic-gate free(ze); 9080Sstevel@tonic-gate } 9090Sstevel@tonic-gate endzoneent(cookie); 9100Sstevel@tonic-gate return (Z_OK); 9110Sstevel@tonic-gate } 9120Sstevel@tonic-gate 9130Sstevel@tonic-gate static int 9140Sstevel@tonic-gate validate_zonepath(char *path, int cmd_num) 9150Sstevel@tonic-gate { 9160Sstevel@tonic-gate int res; /* result of last library/system call */ 9170Sstevel@tonic-gate boolean_t err = B_FALSE; /* have we run into an error? */ 9180Sstevel@tonic-gate struct stat stbuf; 9192267Sdp struct statvfs64 vfsbuf; 9200Sstevel@tonic-gate char rpath[MAXPATHLEN]; /* resolved path */ 9210Sstevel@tonic-gate char ppath[MAXPATHLEN]; /* parent path */ 9220Sstevel@tonic-gate char rppath[MAXPATHLEN]; /* resolved parent path */ 9230Sstevel@tonic-gate char rootpath[MAXPATHLEN]; /* root path */ 9240Sstevel@tonic-gate zone_state_t state; 9250Sstevel@tonic-gate 9260Sstevel@tonic-gate if (path[0] != '/') { 9270Sstevel@tonic-gate (void) fprintf(stderr, 9280Sstevel@tonic-gate gettext("%s is not an absolute path.\n"), path); 9290Sstevel@tonic-gate return (Z_ERR); 9300Sstevel@tonic-gate } 9310Sstevel@tonic-gate if ((res = resolvepath(path, rpath, sizeof (rpath))) == -1) { 9320Sstevel@tonic-gate if ((errno != ENOENT) || 9331300Sgjelinek (cmd_num != CMD_VERIFY && cmd_num != CMD_INSTALL && 9341300Sgjelinek cmd_num != CMD_CLONE && cmd_num != CMD_MOVE)) { 9350Sstevel@tonic-gate zperror(path, B_FALSE); 9360Sstevel@tonic-gate return (Z_ERR); 9370Sstevel@tonic-gate } 9380Sstevel@tonic-gate if (cmd_num == CMD_VERIFY) { 939924Sgjelinek /* 940924Sgjelinek * TRANSLATION_NOTE 941924Sgjelinek * zoneadm is a literal that should not be translated. 942924Sgjelinek */ 9430Sstevel@tonic-gate (void) fprintf(stderr, gettext("WARNING: %s does not " 944924Sgjelinek "exist, so it could not be verified.\nWhen " 945924Sgjelinek "'zoneadm %s' is run, '%s' will try to create\n%s, " 946924Sgjelinek "and '%s' will be tried again,\nbut the '%s' may " 947924Sgjelinek "fail if:\nthe parent directory of %s is group- or " 948924Sgjelinek "other-writable\nor\n%s overlaps with any other " 9490Sstevel@tonic-gate "installed zones.\n"), path, 9500Sstevel@tonic-gate cmd_to_str(CMD_INSTALL), cmd_to_str(CMD_INSTALL), 9510Sstevel@tonic-gate path, cmd_to_str(CMD_VERIFY), 9520Sstevel@tonic-gate cmd_to_str(CMD_VERIFY), path, path); 9530Sstevel@tonic-gate return (Z_OK); 9540Sstevel@tonic-gate } 9550Sstevel@tonic-gate /* 9560Sstevel@tonic-gate * The zonepath is supposed to be mode 700 but its 9570Sstevel@tonic-gate * parent(s) 755. So use 755 on the mkdirp() then 9580Sstevel@tonic-gate * chmod() the zonepath itself to 700. 9590Sstevel@tonic-gate */ 9600Sstevel@tonic-gate if (mkdirp(path, DEFAULT_DIR_MODE) < 0) { 9610Sstevel@tonic-gate zperror(path, B_FALSE); 9620Sstevel@tonic-gate return (Z_ERR); 9630Sstevel@tonic-gate } 9640Sstevel@tonic-gate /* 9650Sstevel@tonic-gate * If the chmod() fails, report the error, but might 9660Sstevel@tonic-gate * as well continue the verify procedure. 9670Sstevel@tonic-gate */ 9680Sstevel@tonic-gate if (chmod(path, S_IRWXU) != 0) 9690Sstevel@tonic-gate zperror(path, B_FALSE); 9700Sstevel@tonic-gate /* 9710Sstevel@tonic-gate * Since the mkdir() succeeded, we should not have to 9720Sstevel@tonic-gate * worry about a subsequent ENOENT, thus this should 9730Sstevel@tonic-gate * only recurse once. 9740Sstevel@tonic-gate */ 9751300Sgjelinek return (validate_zonepath(path, cmd_num)); 9760Sstevel@tonic-gate } 9770Sstevel@tonic-gate rpath[res] = '\0'; 9780Sstevel@tonic-gate if (strcmp(path, rpath) != 0) { 9790Sstevel@tonic-gate errno = Z_RESOLVED_PATH; 9800Sstevel@tonic-gate zperror(path, B_TRUE); 9810Sstevel@tonic-gate return (Z_ERR); 9820Sstevel@tonic-gate } 9830Sstevel@tonic-gate if ((res = stat(rpath, &stbuf)) != 0) { 9840Sstevel@tonic-gate zperror(rpath, B_FALSE); 9850Sstevel@tonic-gate return (Z_ERR); 9860Sstevel@tonic-gate } 9870Sstevel@tonic-gate if (!S_ISDIR(stbuf.st_mode)) { 9880Sstevel@tonic-gate (void) fprintf(stderr, gettext("%s is not a directory.\n"), 9890Sstevel@tonic-gate rpath); 9900Sstevel@tonic-gate return (Z_ERR); 9910Sstevel@tonic-gate } 9923445Sblakej if (strcmp(stbuf.st_fstype, MNTTYPE_TMPFS) == 0) { 9930Sstevel@tonic-gate (void) printf(gettext("WARNING: %s is on a temporary " 9941867Sgjelinek "file system.\n"), rpath); 9950Sstevel@tonic-gate } 9960Sstevel@tonic-gate if (crosscheck_zonepaths(rpath) != Z_OK) 9970Sstevel@tonic-gate return (Z_ERR); 9980Sstevel@tonic-gate /* 9990Sstevel@tonic-gate * Try to collect and report as many minor errors as possible 10000Sstevel@tonic-gate * before returning, so the user can learn everything that needs 10010Sstevel@tonic-gate * to be fixed up front. 10020Sstevel@tonic-gate */ 10030Sstevel@tonic-gate if (stbuf.st_uid != 0) { 10040Sstevel@tonic-gate (void) fprintf(stderr, gettext("%s is not owned by root.\n"), 10050Sstevel@tonic-gate rpath); 10060Sstevel@tonic-gate err = B_TRUE; 10070Sstevel@tonic-gate } 10080Sstevel@tonic-gate err |= bad_mode_bit(stbuf.st_mode, S_IRUSR, B_TRUE, rpath); 10090Sstevel@tonic-gate err |= bad_mode_bit(stbuf.st_mode, S_IWUSR, B_TRUE, rpath); 10100Sstevel@tonic-gate err |= bad_mode_bit(stbuf.st_mode, S_IXUSR, B_TRUE, rpath); 10110Sstevel@tonic-gate err |= bad_mode_bit(stbuf.st_mode, S_IRGRP, B_FALSE, rpath); 10120Sstevel@tonic-gate err |= bad_mode_bit(stbuf.st_mode, S_IWGRP, B_FALSE, rpath); 10130Sstevel@tonic-gate err |= bad_mode_bit(stbuf.st_mode, S_IXGRP, B_FALSE, rpath); 10140Sstevel@tonic-gate err |= bad_mode_bit(stbuf.st_mode, S_IROTH, B_FALSE, rpath); 10150Sstevel@tonic-gate err |= bad_mode_bit(stbuf.st_mode, S_IWOTH, B_FALSE, rpath); 10160Sstevel@tonic-gate err |= bad_mode_bit(stbuf.st_mode, S_IXOTH, B_FALSE, rpath); 10170Sstevel@tonic-gate 10180Sstevel@tonic-gate (void) snprintf(ppath, sizeof (ppath), "%s/..", path); 10190Sstevel@tonic-gate if ((res = resolvepath(ppath, rppath, sizeof (rppath))) == -1) { 10200Sstevel@tonic-gate zperror(ppath, B_FALSE); 10210Sstevel@tonic-gate return (Z_ERR); 10220Sstevel@tonic-gate } 10230Sstevel@tonic-gate rppath[res] = '\0'; 10240Sstevel@tonic-gate if ((res = stat(rppath, &stbuf)) != 0) { 10250Sstevel@tonic-gate zperror(rppath, B_FALSE); 10260Sstevel@tonic-gate return (Z_ERR); 10270Sstevel@tonic-gate } 10280Sstevel@tonic-gate /* theoretically impossible */ 10290Sstevel@tonic-gate if (!S_ISDIR(stbuf.st_mode)) { 10300Sstevel@tonic-gate (void) fprintf(stderr, gettext("%s is not a directory.\n"), 10310Sstevel@tonic-gate rppath); 10320Sstevel@tonic-gate return (Z_ERR); 10330Sstevel@tonic-gate } 10340Sstevel@tonic-gate if (stbuf.st_uid != 0) { 10350Sstevel@tonic-gate (void) fprintf(stderr, gettext("%s is not owned by root.\n"), 10360Sstevel@tonic-gate rppath); 10370Sstevel@tonic-gate err = B_TRUE; 10380Sstevel@tonic-gate } 10390Sstevel@tonic-gate err |= bad_mode_bit(stbuf.st_mode, S_IRUSR, B_TRUE, rppath); 10400Sstevel@tonic-gate err |= bad_mode_bit(stbuf.st_mode, S_IWUSR, B_TRUE, rppath); 10410Sstevel@tonic-gate err |= bad_mode_bit(stbuf.st_mode, S_IXUSR, B_TRUE, rppath); 10420Sstevel@tonic-gate err |= bad_mode_bit(stbuf.st_mode, S_IWGRP, B_FALSE, rppath); 10430Sstevel@tonic-gate err |= bad_mode_bit(stbuf.st_mode, S_IWOTH, B_FALSE, rppath); 10440Sstevel@tonic-gate if (strcmp(rpath, rppath) == 0) { 10450Sstevel@tonic-gate (void) fprintf(stderr, gettext("%s is its own parent.\n"), 10460Sstevel@tonic-gate rppath); 10470Sstevel@tonic-gate err = B_TRUE; 10480Sstevel@tonic-gate } 10490Sstevel@tonic-gate 10502267Sdp if (statvfs64(rpath, &vfsbuf) != 0) { 10510Sstevel@tonic-gate zperror(rpath, B_FALSE); 10520Sstevel@tonic-gate return (Z_ERR); 10530Sstevel@tonic-gate } 1054823Sgjelinek if (strcmp(vfsbuf.f_basetype, MNTTYPE_NFS) == 0) { 1055924Sgjelinek /* 1056924Sgjelinek * TRANSLATION_NOTE 1057924Sgjelinek * Zonepath and NFS are literals that should not be translated. 1058924Sgjelinek */ 1059924Sgjelinek (void) fprintf(stderr, gettext("Zonepath %s is on an NFS " 10601867Sgjelinek "mounted file system.\n" 10611867Sgjelinek "\tA local file system must be used.\n"), rpath); 1062823Sgjelinek return (Z_ERR); 1063823Sgjelinek } 1064823Sgjelinek if (vfsbuf.f_flag & ST_NOSUID) { 1065924Sgjelinek /* 1066924Sgjelinek * TRANSLATION_NOTE 1067924Sgjelinek * Zonepath and nosuid are literals that should not be 1068924Sgjelinek * translated. 1069924Sgjelinek */ 1070823Sgjelinek (void) fprintf(stderr, gettext("Zonepath %s is on a nosuid " 10711867Sgjelinek "file system.\n"), rpath); 10720Sstevel@tonic-gate return (Z_ERR); 10730Sstevel@tonic-gate } 10740Sstevel@tonic-gate 10750Sstevel@tonic-gate if ((res = zone_get_state(target_zone, &state)) != Z_OK) { 10760Sstevel@tonic-gate errno = res; 10770Sstevel@tonic-gate zperror2(target_zone, gettext("could not get state")); 10780Sstevel@tonic-gate return (Z_ERR); 10790Sstevel@tonic-gate } 10800Sstevel@tonic-gate /* 10810Sstevel@tonic-gate * The existence of the root path is only bad in the configured state, 10820Sstevel@tonic-gate * as it is *supposed* to be there at the installed and later states. 10831507Sgjelinek * However, the root path is expected to be there if the zone is 10841507Sgjelinek * detached. 10850Sstevel@tonic-gate * State/command mismatches are caught earlier in verify_details(). 10860Sstevel@tonic-gate */ 10871507Sgjelinek if (state == ZONE_STATE_CONFIGURED && cmd_num != CMD_ATTACH) { 10880Sstevel@tonic-gate if (snprintf(rootpath, sizeof (rootpath), "%s/root", rpath) >= 10890Sstevel@tonic-gate sizeof (rootpath)) { 1090924Sgjelinek /* 1091924Sgjelinek * TRANSLATION_NOTE 1092924Sgjelinek * Zonepath is a literal that should not be translated. 1093924Sgjelinek */ 10940Sstevel@tonic-gate (void) fprintf(stderr, 10950Sstevel@tonic-gate gettext("Zonepath %s is too long.\n"), rpath); 10960Sstevel@tonic-gate return (Z_ERR); 10970Sstevel@tonic-gate } 10980Sstevel@tonic-gate if ((res = stat(rootpath, &stbuf)) == 0) { 10991507Sgjelinek if (zonecfg_detached(rpath)) 11001507Sgjelinek (void) fprintf(stderr, 11011507Sgjelinek gettext("Cannot %s detached " 11021507Sgjelinek "zone.\nUse attach or remove %s " 11031507Sgjelinek "directory.\n"), cmd_to_str(cmd_num), 11041507Sgjelinek rpath); 11051507Sgjelinek else 11061507Sgjelinek (void) fprintf(stderr, 11071507Sgjelinek gettext("Rootpath %s exists; " 11081507Sgjelinek "remove or move aside prior to %s.\n"), 11091507Sgjelinek rootpath, cmd_to_str(cmd_num)); 11100Sstevel@tonic-gate return (Z_ERR); 11110Sstevel@tonic-gate } 11120Sstevel@tonic-gate } 11130Sstevel@tonic-gate 11140Sstevel@tonic-gate return (err ? Z_ERR : Z_OK); 11150Sstevel@tonic-gate } 11160Sstevel@tonic-gate 11172712Snn35248 /* 11182712Snn35248 * The following two routines implement a simple locking mechanism to 11192712Snn35248 * ensure that only one instance of zoneadm at a time is able to manipulate 11202712Snn35248 * a given zone. The lock is built on top of an fcntl(2) lock of 11212712Snn35248 * [<altroot>]/var/run/zones/<zonename>.zoneadm.lock. If a zoneadm instance 11222712Snn35248 * can grab that lock, it is allowed to manipulate the zone. 11232712Snn35248 * 11242712Snn35248 * Since zoneadm may call external applications which in turn invoke 11252712Snn35248 * zoneadm again, we introduce the notion of "lock inheritance". Any 11262712Snn35248 * instance of zoneadm that has another instance in its ancestry is assumed 11272712Snn35248 * to be acting on behalf of the original zoneadm, and is thus allowed to 11282712Snn35248 * manipulate its zone. 11292712Snn35248 * 11302712Snn35248 * This inheritance is implemented via the _ZONEADM_LOCK_HELD environment 11312712Snn35248 * variable. When zoneadm is granted a lock on its zone, this environment 11322712Snn35248 * variable is set to 1. When it releases the lock, the variable is set to 11332712Snn35248 * 0. Since a child process inherits its parent's environment, checking 11342712Snn35248 * the state of this variable indicates whether or not any ancestor owns 11352712Snn35248 * the lock. 11362712Snn35248 */ 11370Sstevel@tonic-gate static void 11380Sstevel@tonic-gate release_lock_file(int lockfd) 11390Sstevel@tonic-gate { 11402712Snn35248 /* 11412712Snn35248 * If we are cleaning up from a failed attempt to lock the zone for 11422712Snn35248 * the first time, we might have a zone_lock_cnt of 0. In that 11432712Snn35248 * error case, we don't want to do anything but close the lock 11442712Snn35248 * file. 11452712Snn35248 */ 11462712Snn35248 assert(zone_lock_cnt >= 0); 11472712Snn35248 if (zone_lock_cnt > 0) { 11482712Snn35248 assert(getenv(LOCK_ENV_VAR) != NULL); 11492712Snn35248 assert(atoi(getenv(LOCK_ENV_VAR)) == 1); 11502712Snn35248 if (--zone_lock_cnt > 0) { 11512712Snn35248 assert(lockfd == -1); 11522712Snn35248 return; 11532712Snn35248 } 11542712Snn35248 if (putenv(zoneadm_lock_not_held) != 0) { 11552712Snn35248 zperror(target_zone, B_TRUE); 11562712Snn35248 exit(Z_ERR); 11572712Snn35248 } 11582712Snn35248 } 11592712Snn35248 assert(lockfd >= 0); 11600Sstevel@tonic-gate (void) close(lockfd); 11610Sstevel@tonic-gate } 11620Sstevel@tonic-gate 11630Sstevel@tonic-gate static int 11640Sstevel@tonic-gate grab_lock_file(const char *zone_name, int *lockfd) 11650Sstevel@tonic-gate { 11660Sstevel@tonic-gate char pathbuf[PATH_MAX]; 11670Sstevel@tonic-gate struct flock flock; 11680Sstevel@tonic-gate 11692712Snn35248 /* 11702712Snn35248 * If we already have the lock, we can skip this expensive song 11712712Snn35248 * and dance. 11722712Snn35248 */ 11732712Snn35248 if (zone_lock_cnt > 0) { 11742712Snn35248 zone_lock_cnt++; 11752712Snn35248 *lockfd = -1; 11762712Snn35248 return (Z_OK); 11772712Snn35248 } 11782712Snn35248 assert(getenv(LOCK_ENV_VAR) != NULL); 11792712Snn35248 assert(atoi(getenv(LOCK_ENV_VAR)) == 0); 11802712Snn35248 1181766Scarlsonj if (snprintf(pathbuf, sizeof (pathbuf), "%s%s", zonecfg_get_root(), 1182766Scarlsonj ZONES_TMPDIR) >= sizeof (pathbuf)) { 1183766Scarlsonj zerror(gettext("alternate root path is too long")); 1184766Scarlsonj return (Z_ERR); 1185766Scarlsonj } 1186766Scarlsonj if (mkdir(pathbuf, S_IRWXU) < 0 && errno != EEXIST) { 1187766Scarlsonj zerror(gettext("could not mkdir %s: %s"), pathbuf, 11880Sstevel@tonic-gate strerror(errno)); 11890Sstevel@tonic-gate return (Z_ERR); 11900Sstevel@tonic-gate } 1191766Scarlsonj (void) chmod(pathbuf, S_IRWXU); 11920Sstevel@tonic-gate 11930Sstevel@tonic-gate /* 11940Sstevel@tonic-gate * One of these lock files is created for each zone (when needed). 11950Sstevel@tonic-gate * The lock files are not cleaned up (except on system reboot), 11960Sstevel@tonic-gate * but since there is only one per zone, there is no resource 11970Sstevel@tonic-gate * starvation issue. 11980Sstevel@tonic-gate */ 1199766Scarlsonj if (snprintf(pathbuf, sizeof (pathbuf), "%s%s/%s.zoneadm.lock", 1200766Scarlsonj zonecfg_get_root(), ZONES_TMPDIR, zone_name) >= sizeof (pathbuf)) { 1201766Scarlsonj zerror(gettext("alternate root path is too long")); 1202766Scarlsonj return (Z_ERR); 1203766Scarlsonj } 12040Sstevel@tonic-gate if ((*lockfd = open(pathbuf, O_RDWR|O_CREAT, S_IRUSR|S_IWUSR)) < 0) { 12050Sstevel@tonic-gate zerror(gettext("could not open %s: %s"), pathbuf, 12060Sstevel@tonic-gate strerror(errno)); 12070Sstevel@tonic-gate return (Z_ERR); 12080Sstevel@tonic-gate } 12090Sstevel@tonic-gate /* 12100Sstevel@tonic-gate * Lock the file to synchronize with other zoneadmds 12110Sstevel@tonic-gate */ 12120Sstevel@tonic-gate flock.l_type = F_WRLCK; 12130Sstevel@tonic-gate flock.l_whence = SEEK_SET; 12140Sstevel@tonic-gate flock.l_start = (off_t)0; 12150Sstevel@tonic-gate flock.l_len = (off_t)0; 12162712Snn35248 if ((fcntl(*lockfd, F_SETLKW, &flock) < 0) || 12172712Snn35248 (putenv(zoneadm_lock_held) != 0)) { 12180Sstevel@tonic-gate zerror(gettext("unable to lock %s: %s"), pathbuf, 12190Sstevel@tonic-gate strerror(errno)); 12200Sstevel@tonic-gate release_lock_file(*lockfd); 12210Sstevel@tonic-gate return (Z_ERR); 12220Sstevel@tonic-gate } 12232712Snn35248 zone_lock_cnt = 1; 12240Sstevel@tonic-gate return (Z_OK); 12250Sstevel@tonic-gate } 12260Sstevel@tonic-gate 1227766Scarlsonj static boolean_t 12280Sstevel@tonic-gate get_doorname(const char *zone_name, char *buffer) 12290Sstevel@tonic-gate { 1230766Scarlsonj return (snprintf(buffer, PATH_MAX, "%s" ZONE_DOOR_PATH, 1231766Scarlsonj zonecfg_get_root(), zone_name) < PATH_MAX); 12320Sstevel@tonic-gate } 12330Sstevel@tonic-gate 12340Sstevel@tonic-gate /* 12350Sstevel@tonic-gate * system daemons are not audited. For the global zone, this occurs 12360Sstevel@tonic-gate * "naturally" since init is started with the default audit 12370Sstevel@tonic-gate * characteristics. Since zoneadmd is a system daemon and it starts 12380Sstevel@tonic-gate * init for a zone, it is necessary to clear out the audit 12390Sstevel@tonic-gate * characteristics inherited from whomever started zoneadmd. This is 12400Sstevel@tonic-gate * indicated by the audit id, which is set from the ruid parameter of 12410Sstevel@tonic-gate * adt_set_user(), below. 12420Sstevel@tonic-gate */ 12430Sstevel@tonic-gate 12440Sstevel@tonic-gate static void 12450Sstevel@tonic-gate prepare_audit_context() 12460Sstevel@tonic-gate { 12470Sstevel@tonic-gate adt_session_data_t *ah; 12480Sstevel@tonic-gate char *failure = gettext("audit failure: %s"); 12490Sstevel@tonic-gate 12500Sstevel@tonic-gate if (adt_start_session(&ah, NULL, 0)) { 12510Sstevel@tonic-gate zerror(failure, strerror(errno)); 12520Sstevel@tonic-gate return; 12530Sstevel@tonic-gate } 12540Sstevel@tonic-gate if (adt_set_user(ah, ADT_NO_AUDIT, ADT_NO_AUDIT, 12550Sstevel@tonic-gate ADT_NO_AUDIT, ADT_NO_AUDIT, NULL, ADT_NEW)) { 12560Sstevel@tonic-gate zerror(failure, strerror(errno)); 12570Sstevel@tonic-gate (void) adt_end_session(ah); 12580Sstevel@tonic-gate return; 12590Sstevel@tonic-gate } 12600Sstevel@tonic-gate if (adt_set_proc(ah)) 12610Sstevel@tonic-gate zerror(failure, strerror(errno)); 12620Sstevel@tonic-gate 12630Sstevel@tonic-gate (void) adt_end_session(ah); 12640Sstevel@tonic-gate } 12650Sstevel@tonic-gate 12660Sstevel@tonic-gate static int 12670Sstevel@tonic-gate start_zoneadmd(const char *zone_name) 12680Sstevel@tonic-gate { 12690Sstevel@tonic-gate char doorpath[PATH_MAX]; 12700Sstevel@tonic-gate pid_t child_pid; 12710Sstevel@tonic-gate int error = Z_ERR; 12720Sstevel@tonic-gate int doorfd, lockfd; 12730Sstevel@tonic-gate struct door_info info; 12740Sstevel@tonic-gate 1275766Scarlsonj if (!get_doorname(zone_name, doorpath)) 1276766Scarlsonj return (Z_ERR); 12770Sstevel@tonic-gate 12780Sstevel@tonic-gate if (grab_lock_file(zone_name, &lockfd) != Z_OK) 12790Sstevel@tonic-gate return (Z_ERR); 12800Sstevel@tonic-gate 12810Sstevel@tonic-gate /* 12820Sstevel@tonic-gate * Now that we have the lock, re-confirm that the daemon is 12830Sstevel@tonic-gate * *not* up and working fine. If it is still down, we have a green 12840Sstevel@tonic-gate * light to start it. 12850Sstevel@tonic-gate */ 12860Sstevel@tonic-gate if ((doorfd = open(doorpath, O_RDONLY)) < 0) { 12870Sstevel@tonic-gate if (errno != ENOENT) { 12880Sstevel@tonic-gate zperror(doorpath, B_FALSE); 12890Sstevel@tonic-gate goto out; 12900Sstevel@tonic-gate } 12910Sstevel@tonic-gate } else { 12920Sstevel@tonic-gate if (door_info(doorfd, &info) == 0 && 12930Sstevel@tonic-gate ((info.di_attributes & DOOR_REVOKED) == 0)) { 12940Sstevel@tonic-gate error = Z_OK; 12950Sstevel@tonic-gate (void) close(doorfd); 12960Sstevel@tonic-gate goto out; 12970Sstevel@tonic-gate } 12980Sstevel@tonic-gate (void) close(doorfd); 12990Sstevel@tonic-gate } 13000Sstevel@tonic-gate 13010Sstevel@tonic-gate if ((child_pid = fork()) == -1) { 13020Sstevel@tonic-gate zperror(gettext("could not fork"), B_FALSE); 13030Sstevel@tonic-gate goto out; 13040Sstevel@tonic-gate } else if (child_pid == 0) { 1305766Scarlsonj const char *argv[6], **ap; 1306766Scarlsonj 13070Sstevel@tonic-gate /* child process */ 13080Sstevel@tonic-gate prepare_audit_context(); 13090Sstevel@tonic-gate 1310766Scarlsonj ap = argv; 1311766Scarlsonj *ap++ = "zoneadmd"; 1312766Scarlsonj *ap++ = "-z"; 1313766Scarlsonj *ap++ = zone_name; 1314766Scarlsonj if (zonecfg_in_alt_root()) { 1315766Scarlsonj *ap++ = "-R"; 1316766Scarlsonj *ap++ = zonecfg_get_root(); 1317766Scarlsonj } 1318766Scarlsonj *ap = NULL; 1319766Scarlsonj 1320766Scarlsonj (void) execv("/usr/lib/zones/zoneadmd", (char * const *)argv); 1321924Sgjelinek /* 1322924Sgjelinek * TRANSLATION_NOTE 1323924Sgjelinek * zoneadmd is a literal that should not be translated. 1324924Sgjelinek */ 13250Sstevel@tonic-gate zperror(gettext("could not exec zoneadmd"), B_FALSE); 13260Sstevel@tonic-gate _exit(Z_ERR); 13270Sstevel@tonic-gate } else { 13280Sstevel@tonic-gate /* parent process */ 13290Sstevel@tonic-gate pid_t retval; 13300Sstevel@tonic-gate int pstatus = 0; 13310Sstevel@tonic-gate 13320Sstevel@tonic-gate do { 13330Sstevel@tonic-gate retval = waitpid(child_pid, &pstatus, 0); 13340Sstevel@tonic-gate } while (retval != child_pid); 13350Sstevel@tonic-gate if (WIFSIGNALED(pstatus) || (WIFEXITED(pstatus) && 13360Sstevel@tonic-gate WEXITSTATUS(pstatus) != 0)) { 13370Sstevel@tonic-gate zerror(gettext("could not start %s"), "zoneadmd"); 13380Sstevel@tonic-gate goto out; 13390Sstevel@tonic-gate } 13400Sstevel@tonic-gate } 13410Sstevel@tonic-gate error = Z_OK; 13420Sstevel@tonic-gate out: 13430Sstevel@tonic-gate release_lock_file(lockfd); 13440Sstevel@tonic-gate return (error); 13450Sstevel@tonic-gate } 13460Sstevel@tonic-gate 13470Sstevel@tonic-gate static int 13480Sstevel@tonic-gate ping_zoneadmd(const char *zone_name) 13490Sstevel@tonic-gate { 13500Sstevel@tonic-gate char doorpath[PATH_MAX]; 13510Sstevel@tonic-gate int doorfd; 13520Sstevel@tonic-gate struct door_info info; 13530Sstevel@tonic-gate 1354766Scarlsonj if (!get_doorname(zone_name, doorpath)) 1355766Scarlsonj return (Z_ERR); 13560Sstevel@tonic-gate 13570Sstevel@tonic-gate if ((doorfd = open(doorpath, O_RDONLY)) < 0) { 13580Sstevel@tonic-gate return (Z_ERR); 13590Sstevel@tonic-gate } 13600Sstevel@tonic-gate if (door_info(doorfd, &info) == 0 && 13610Sstevel@tonic-gate ((info.di_attributes & DOOR_REVOKED) == 0)) { 13620Sstevel@tonic-gate (void) close(doorfd); 13630Sstevel@tonic-gate return (Z_OK); 13640Sstevel@tonic-gate } 13650Sstevel@tonic-gate (void) close(doorfd); 13660Sstevel@tonic-gate return (Z_ERR); 13670Sstevel@tonic-gate } 13680Sstevel@tonic-gate 13690Sstevel@tonic-gate static int 13700Sstevel@tonic-gate call_zoneadmd(const char *zone_name, zone_cmd_arg_t *arg) 13710Sstevel@tonic-gate { 13720Sstevel@tonic-gate char doorpath[PATH_MAX]; 13730Sstevel@tonic-gate int doorfd, result; 13740Sstevel@tonic-gate door_arg_t darg; 13750Sstevel@tonic-gate 13760Sstevel@tonic-gate zoneid_t zoneid; 13770Sstevel@tonic-gate uint64_t uniqid = 0; 13780Sstevel@tonic-gate 13790Sstevel@tonic-gate zone_cmd_rval_t *rvalp; 13800Sstevel@tonic-gate size_t rlen; 13810Sstevel@tonic-gate char *cp, *errbuf; 13820Sstevel@tonic-gate 13830Sstevel@tonic-gate rlen = getpagesize(); 13840Sstevel@tonic-gate if ((rvalp = malloc(rlen)) == NULL) { 13850Sstevel@tonic-gate zerror(gettext("failed to allocate %lu bytes: %s"), rlen, 13860Sstevel@tonic-gate strerror(errno)); 13870Sstevel@tonic-gate return (-1); 13880Sstevel@tonic-gate } 13890Sstevel@tonic-gate 13900Sstevel@tonic-gate if ((zoneid = getzoneidbyname(zone_name)) != ZONE_ID_UNDEFINED) { 13910Sstevel@tonic-gate (void) zone_getattr(zoneid, ZONE_ATTR_UNIQID, &uniqid, 13920Sstevel@tonic-gate sizeof (uniqid)); 13930Sstevel@tonic-gate } 13940Sstevel@tonic-gate arg->uniqid = uniqid; 13950Sstevel@tonic-gate (void) strlcpy(arg->locale, locale, sizeof (arg->locale)); 1396766Scarlsonj if (!get_doorname(zone_name, doorpath)) { 1397766Scarlsonj zerror(gettext("alternate root path is too long")); 1398766Scarlsonj free(rvalp); 1399766Scarlsonj return (-1); 1400766Scarlsonj } 14010Sstevel@tonic-gate 14020Sstevel@tonic-gate /* 14030Sstevel@tonic-gate * Loop trying to start zoneadmd; if something goes seriously 14040Sstevel@tonic-gate * wrong we break out and fail. 14050Sstevel@tonic-gate */ 14060Sstevel@tonic-gate for (;;) { 14070Sstevel@tonic-gate if (start_zoneadmd(zone_name) != Z_OK) 14080Sstevel@tonic-gate break; 14090Sstevel@tonic-gate 14100Sstevel@tonic-gate if ((doorfd = open(doorpath, O_RDONLY)) < 0) { 14110Sstevel@tonic-gate zperror(gettext("failed to open zone door"), B_FALSE); 14120Sstevel@tonic-gate break; 14130Sstevel@tonic-gate } 14140Sstevel@tonic-gate 14150Sstevel@tonic-gate darg.data_ptr = (char *)arg; 14160Sstevel@tonic-gate darg.data_size = sizeof (*arg); 14170Sstevel@tonic-gate darg.desc_ptr = NULL; 14180Sstevel@tonic-gate darg.desc_num = 0; 14190Sstevel@tonic-gate darg.rbuf = (char *)rvalp; 14200Sstevel@tonic-gate darg.rsize = rlen; 14210Sstevel@tonic-gate if (door_call(doorfd, &darg) != 0) { 14220Sstevel@tonic-gate (void) close(doorfd); 14230Sstevel@tonic-gate /* 14240Sstevel@tonic-gate * We'll get EBADF if the door has been revoked. 14250Sstevel@tonic-gate */ 14260Sstevel@tonic-gate if (errno != EBADF) { 14270Sstevel@tonic-gate zperror(gettext("door_call failed"), B_FALSE); 14280Sstevel@tonic-gate break; 14290Sstevel@tonic-gate } 14300Sstevel@tonic-gate continue; /* take another lap */ 14310Sstevel@tonic-gate } 14320Sstevel@tonic-gate (void) close(doorfd); 14330Sstevel@tonic-gate 14340Sstevel@tonic-gate if (darg.data_size == 0) { 14350Sstevel@tonic-gate /* Door server is going away; kick it again. */ 14360Sstevel@tonic-gate continue; 14370Sstevel@tonic-gate } 14380Sstevel@tonic-gate 14390Sstevel@tonic-gate errbuf = rvalp->errbuf; 14400Sstevel@tonic-gate while (*errbuf != '\0') { 14410Sstevel@tonic-gate /* 14420Sstevel@tonic-gate * Remove any newlines since zerror() 14430Sstevel@tonic-gate * will append one automatically. 14440Sstevel@tonic-gate */ 14450Sstevel@tonic-gate cp = strchr(errbuf, '\n'); 14460Sstevel@tonic-gate if (cp != NULL) 14470Sstevel@tonic-gate *cp = '\0'; 14480Sstevel@tonic-gate zerror("%s", errbuf); 14490Sstevel@tonic-gate if (cp == NULL) 14500Sstevel@tonic-gate break; 14510Sstevel@tonic-gate errbuf = cp + 1; 14520Sstevel@tonic-gate } 14530Sstevel@tonic-gate result = rvalp->rval == 0 ? 0 : -1; 14540Sstevel@tonic-gate free(rvalp); 14550Sstevel@tonic-gate return (result); 14560Sstevel@tonic-gate } 14570Sstevel@tonic-gate 14580Sstevel@tonic-gate free(rvalp); 14590Sstevel@tonic-gate return (-1); 14600Sstevel@tonic-gate } 14610Sstevel@tonic-gate 14620Sstevel@tonic-gate static int 14633339Szt129084 invoke_brand_handler(int cmd_num, char *argv[]) 14643339Szt129084 { 14653339Szt129084 zone_dochandle_t handle; 14663339Szt129084 int err; 14673339Szt129084 14683339Szt129084 if ((handle = zonecfg_init_handle()) == NULL) { 14693339Szt129084 zperror(cmd_to_str(cmd_num), B_TRUE); 14703339Szt129084 return (Z_ERR); 14713339Szt129084 } 14723339Szt129084 if ((err = zonecfg_get_handle(target_zone, handle)) != Z_OK) { 14733339Szt129084 errno = err; 14743339Szt129084 zperror(cmd_to_str(cmd_num), B_TRUE); 14753339Szt129084 zonecfg_fini_handle(handle); 14763339Szt129084 return (Z_ERR); 14773339Szt129084 } 14783339Szt129084 if (verify_brand(handle, cmd_num, argv) != Z_OK) { 14793339Szt129084 zonecfg_fini_handle(handle); 14803339Szt129084 return (Z_ERR); 14813339Szt129084 } 14823339Szt129084 zonecfg_fini_handle(handle); 14833339Szt129084 return (Z_OK); 14843339Szt129084 } 14853339Szt129084 14863339Szt129084 static int 14870Sstevel@tonic-gate ready_func(int argc, char *argv[]) 14880Sstevel@tonic-gate { 14890Sstevel@tonic-gate zone_cmd_arg_t zarg; 14900Sstevel@tonic-gate int arg; 14910Sstevel@tonic-gate 1492766Scarlsonj if (zonecfg_in_alt_root()) { 1493766Scarlsonj zerror(gettext("cannot ready zone in alternate root")); 1494766Scarlsonj return (Z_ERR); 1495766Scarlsonj } 1496766Scarlsonj 14970Sstevel@tonic-gate optind = 0; 14980Sstevel@tonic-gate if ((arg = getopt(argc, argv, "?")) != EOF) { 14990Sstevel@tonic-gate switch (arg) { 15000Sstevel@tonic-gate case '?': 15010Sstevel@tonic-gate sub_usage(SHELP_READY, CMD_READY); 15020Sstevel@tonic-gate return (optopt == '?' ? Z_OK : Z_USAGE); 15030Sstevel@tonic-gate default: 15040Sstevel@tonic-gate sub_usage(SHELP_READY, CMD_READY); 15050Sstevel@tonic-gate return (Z_USAGE); 15060Sstevel@tonic-gate } 15070Sstevel@tonic-gate } 15080Sstevel@tonic-gate if (argc > optind) { 15090Sstevel@tonic-gate sub_usage(SHELP_READY, CMD_READY); 15100Sstevel@tonic-gate return (Z_USAGE); 15110Sstevel@tonic-gate } 15122712Snn35248 if (sanity_check(target_zone, CMD_READY, B_FALSE, B_FALSE, B_FALSE) 15132712Snn35248 != Z_OK) 15140Sstevel@tonic-gate return (Z_ERR); 15153339Szt129084 if (verify_details(CMD_READY, argv) != Z_OK) 15160Sstevel@tonic-gate return (Z_ERR); 15170Sstevel@tonic-gate 15180Sstevel@tonic-gate zarg.cmd = Z_READY; 15190Sstevel@tonic-gate if (call_zoneadmd(target_zone, &zarg) != 0) { 15200Sstevel@tonic-gate zerror(gettext("call to %s failed"), "zoneadmd"); 15210Sstevel@tonic-gate return (Z_ERR); 15220Sstevel@tonic-gate } 15230Sstevel@tonic-gate return (Z_OK); 15240Sstevel@tonic-gate } 15250Sstevel@tonic-gate 15260Sstevel@tonic-gate static int 15270Sstevel@tonic-gate boot_func(int argc, char *argv[]) 15280Sstevel@tonic-gate { 15290Sstevel@tonic-gate zone_cmd_arg_t zarg; 15302712Snn35248 boolean_t force = B_FALSE; 15310Sstevel@tonic-gate int arg; 15320Sstevel@tonic-gate 1533766Scarlsonj if (zonecfg_in_alt_root()) { 1534766Scarlsonj zerror(gettext("cannot boot zone in alternate root")); 1535766Scarlsonj return (Z_ERR); 1536766Scarlsonj } 1537766Scarlsonj 15380Sstevel@tonic-gate zarg.bootbuf[0] = '\0'; 15390Sstevel@tonic-gate 15400Sstevel@tonic-gate /* 15412267Sdp * The following getopt processes arguments to zone boot; that 15422267Sdp * is to say, the [here] portion of the argument string: 15432267Sdp * 15442267Sdp * zoneadm -z myzone boot [here] -- -v -m verbose 15452267Sdp * 15462267Sdp * Where [here] can either be nothing, -? (in which case we bail 15472712Snn35248 * and print usage), -f (a private option to indicate that the 15482712Snn35248 * boot operation should be 'forced'), or -s. Support for -s is 15492712Snn35248 * vestigal and obsolete, but is retained because it was a 15502712Snn35248 * documented interface and there are known consumers including 15512712Snn35248 * admin/install; the proper way to specify boot arguments like -s 15522712Snn35248 * is: 15532267Sdp * 15542267Sdp * zoneadm -z myzone boot -- -s -v -m verbose. 15550Sstevel@tonic-gate */ 15560Sstevel@tonic-gate optind = 0; 15572712Snn35248 while ((arg = getopt(argc, argv, "?fs")) != EOF) { 15580Sstevel@tonic-gate switch (arg) { 15590Sstevel@tonic-gate case '?': 15600Sstevel@tonic-gate sub_usage(SHELP_BOOT, CMD_BOOT); 15610Sstevel@tonic-gate return (optopt == '?' ? Z_OK : Z_USAGE); 15620Sstevel@tonic-gate case 's': 15630Sstevel@tonic-gate (void) strlcpy(zarg.bootbuf, "-s", 15640Sstevel@tonic-gate sizeof (zarg.bootbuf)); 15650Sstevel@tonic-gate break; 15662712Snn35248 case 'f': 15672712Snn35248 force = B_TRUE; 15682712Snn35248 break; 15690Sstevel@tonic-gate default: 15700Sstevel@tonic-gate sub_usage(SHELP_BOOT, CMD_BOOT); 15710Sstevel@tonic-gate return (Z_USAGE); 15720Sstevel@tonic-gate } 15730Sstevel@tonic-gate } 15742267Sdp 15752267Sdp for (; optind < argc; optind++) { 15762267Sdp if (strlcat(zarg.bootbuf, argv[optind], 15772267Sdp sizeof (zarg.bootbuf)) >= sizeof (zarg.bootbuf)) { 15782267Sdp zerror(gettext("Boot argument list too long")); 15792267Sdp return (Z_ERR); 15802267Sdp } 15812267Sdp if (optind < argc - 1) 15822267Sdp if (strlcat(zarg.bootbuf, " ", sizeof (zarg.bootbuf)) >= 15832267Sdp sizeof (zarg.bootbuf)) { 15842267Sdp zerror(gettext("Boot argument list too long")); 15852267Sdp return (Z_ERR); 15862267Sdp } 15872267Sdp } 15882712Snn35248 if (sanity_check(target_zone, CMD_BOOT, B_FALSE, B_FALSE, force) 15892712Snn35248 != Z_OK) 15900Sstevel@tonic-gate return (Z_ERR); 15913339Szt129084 if (verify_details(CMD_BOOT, argv) != Z_OK) 15920Sstevel@tonic-gate return (Z_ERR); 15932712Snn35248 zarg.cmd = force ? Z_FORCEBOOT : Z_BOOT; 15940Sstevel@tonic-gate if (call_zoneadmd(target_zone, &zarg) != 0) { 15950Sstevel@tonic-gate zerror(gettext("call to %s failed"), "zoneadmd"); 15960Sstevel@tonic-gate return (Z_ERR); 15970Sstevel@tonic-gate } 15983247Sgjelinek 15990Sstevel@tonic-gate return (Z_OK); 16000Sstevel@tonic-gate } 16010Sstevel@tonic-gate 16020Sstevel@tonic-gate static void 16030Sstevel@tonic-gate fake_up_local_zone(zoneid_t zid, zone_entry_t *zeptr) 16040Sstevel@tonic-gate { 16050Sstevel@tonic-gate ssize_t result; 16062303Scarlsonj uuid_t uuid; 16072303Scarlsonj FILE *fp; 16083448Sdh155122 ushort_t flags; 16092303Scarlsonj 16102303Scarlsonj (void) memset(zeptr, 0, sizeof (*zeptr)); 16110Sstevel@tonic-gate 16120Sstevel@tonic-gate zeptr->zid = zid; 16132303Scarlsonj 16140Sstevel@tonic-gate /* 16150Sstevel@tonic-gate * Since we're looking up our own (non-global) zone name, 16160Sstevel@tonic-gate * we can be assured that it will succeed. 16170Sstevel@tonic-gate */ 16180Sstevel@tonic-gate result = getzonenamebyid(zid, zeptr->zname, sizeof (zeptr->zname)); 16190Sstevel@tonic-gate assert(result >= 0); 16202303Scarlsonj if (zonecfg_is_scratch(zeptr->zname) && 16212303Scarlsonj (fp = zonecfg_open_scratch("", B_FALSE)) != NULL) { 16222303Scarlsonj (void) zonecfg_reverse_scratch(fp, zeptr->zname, zeptr->zname, 16232303Scarlsonj sizeof (zeptr->zname), NULL, 0); 16242303Scarlsonj zonecfg_close_scratch(fp); 16252303Scarlsonj } 16262303Scarlsonj 16272303Scarlsonj if (is_system_labeled()) { 16281676Sjpk (void) zone_getattr(zid, ZONE_ATTR_ROOT, zeptr->zroot, 16291676Sjpk sizeof (zeptr->zroot)); 16302712Snn35248 (void) strlcpy(zeptr->zbrand, NATIVE_BRAND_NAME, 16314350Std153743 sizeof (zeptr->zbrand)); 16322303Scarlsonj } else { 16332303Scarlsonj (void) strlcpy(zeptr->zroot, "/", sizeof (zeptr->zroot)); 16342712Snn35248 (void) zone_getattr(zid, ZONE_ATTR_BRAND, zeptr->zbrand, 16352712Snn35248 sizeof (zeptr->zbrand)); 16362303Scarlsonj } 16372303Scarlsonj 16380Sstevel@tonic-gate zeptr->zstate_str = "running"; 16392303Scarlsonj if (zonecfg_get_uuid(zeptr->zname, uuid) == Z_OK && 16402303Scarlsonj !uuid_is_null(uuid)) 16412303Scarlsonj uuid_unparse(uuid, zeptr->zuuid); 16423448Sdh155122 16433448Sdh155122 if (zone_getattr(zid, ZONE_ATTR_FLAGS, &flags, sizeof (flags)) < 0) { 16443448Sdh155122 zperror2(zeptr->zname, gettext("could not get zone flags")); 16453448Sdh155122 exit(Z_ERR); 16463448Sdh155122 } 16473448Sdh155122 if (flags & ZF_NET_EXCL) 16483448Sdh155122 zeptr->ziptype = ZS_EXCLUSIVE; 16493448Sdh155122 else 16503448Sdh155122 zeptr->ziptype = ZS_SHARED; 16510Sstevel@tonic-gate } 16520Sstevel@tonic-gate 16530Sstevel@tonic-gate static int 16540Sstevel@tonic-gate list_func(int argc, char *argv[]) 16550Sstevel@tonic-gate { 16560Sstevel@tonic-gate zone_entry_t *zentp, zent; 1657766Scarlsonj int arg, retv; 16580Sstevel@tonic-gate boolean_t output = B_FALSE, verbose = B_FALSE, parsable = B_FALSE; 16590Sstevel@tonic-gate zone_state_t min_state = ZONE_STATE_RUNNING; 16600Sstevel@tonic-gate zoneid_t zone_id = getzoneid(); 16610Sstevel@tonic-gate 16620Sstevel@tonic-gate if (target_zone == NULL) { 16630Sstevel@tonic-gate /* all zones: default view to running but allow override */ 16640Sstevel@tonic-gate optind = 0; 16650Sstevel@tonic-gate while ((arg = getopt(argc, argv, "?cipv")) != EOF) { 16660Sstevel@tonic-gate switch (arg) { 16670Sstevel@tonic-gate case '?': 16680Sstevel@tonic-gate sub_usage(SHELP_LIST, CMD_LIST); 16690Sstevel@tonic-gate return (optopt == '?' ? Z_OK : Z_USAGE); 16701339Sjonb /* 16711339Sjonb * The 'i' and 'c' options are not mutually 16721339Sjonb * exclusive so if 'c' is given, then min_state 16731339Sjonb * is set to 0 (ZONE_STATE_CONFIGURED) which is 16741339Sjonb * the lowest possible state. If 'i' is given, 16751339Sjonb * then min_state is set to be the lowest state 16761339Sjonb * so far. 16771339Sjonb */ 16780Sstevel@tonic-gate case 'c': 16790Sstevel@tonic-gate min_state = ZONE_STATE_CONFIGURED; 16800Sstevel@tonic-gate break; 16810Sstevel@tonic-gate case 'i': 16821339Sjonb min_state = min(ZONE_STATE_INSTALLED, 16831339Sjonb min_state); 16841339Sjonb 16850Sstevel@tonic-gate break; 16860Sstevel@tonic-gate case 'p': 16870Sstevel@tonic-gate parsable = B_TRUE; 16880Sstevel@tonic-gate break; 16890Sstevel@tonic-gate case 'v': 16900Sstevel@tonic-gate verbose = B_TRUE; 16910Sstevel@tonic-gate break; 16920Sstevel@tonic-gate default: 16930Sstevel@tonic-gate sub_usage(SHELP_LIST, CMD_LIST); 16940Sstevel@tonic-gate return (Z_USAGE); 16950Sstevel@tonic-gate } 16960Sstevel@tonic-gate } 16970Sstevel@tonic-gate if (parsable && verbose) { 16980Sstevel@tonic-gate zerror(gettext("%s -p and -v are mutually exclusive."), 16990Sstevel@tonic-gate cmd_to_str(CMD_LIST)); 17000Sstevel@tonic-gate return (Z_ERR); 17010Sstevel@tonic-gate } 17021676Sjpk if (zone_id == GLOBAL_ZONEID || is_system_labeled()) { 1703766Scarlsonj retv = zone_print_list(min_state, verbose, parsable); 17040Sstevel@tonic-gate } else { 17052712Snn35248 fake_up_local_zone(zone_id, &zent); 1706766Scarlsonj retv = Z_OK; 17070Sstevel@tonic-gate zone_print(&zent, verbose, parsable); 17080Sstevel@tonic-gate } 1709766Scarlsonj return (retv); 17100Sstevel@tonic-gate } 17110Sstevel@tonic-gate 17120Sstevel@tonic-gate /* 17130Sstevel@tonic-gate * Specific target zone: disallow -i/-c suboptions. 17140Sstevel@tonic-gate */ 17150Sstevel@tonic-gate optind = 0; 17160Sstevel@tonic-gate while ((arg = getopt(argc, argv, "?pv")) != EOF) { 17170Sstevel@tonic-gate switch (arg) { 17180Sstevel@tonic-gate case '?': 17190Sstevel@tonic-gate sub_usage(SHELP_LIST, CMD_LIST); 17200Sstevel@tonic-gate return (optopt == '?' ? Z_OK : Z_USAGE); 17210Sstevel@tonic-gate case 'p': 17220Sstevel@tonic-gate parsable = B_TRUE; 17230Sstevel@tonic-gate break; 17240Sstevel@tonic-gate case 'v': 17250Sstevel@tonic-gate verbose = B_TRUE; 17260Sstevel@tonic-gate break; 17270Sstevel@tonic-gate default: 17280Sstevel@tonic-gate sub_usage(SHELP_LIST, CMD_LIST); 17290Sstevel@tonic-gate return (Z_USAGE); 17300Sstevel@tonic-gate } 17310Sstevel@tonic-gate } 17320Sstevel@tonic-gate if (parsable && verbose) { 17330Sstevel@tonic-gate zerror(gettext("%s -p and -v are mutually exclusive."), 17340Sstevel@tonic-gate cmd_to_str(CMD_LIST)); 17350Sstevel@tonic-gate return (Z_ERR); 17360Sstevel@tonic-gate } 17370Sstevel@tonic-gate if (argc > optind) { 17380Sstevel@tonic-gate sub_usage(SHELP_LIST, CMD_LIST); 17390Sstevel@tonic-gate return (Z_USAGE); 17400Sstevel@tonic-gate } 17413979Sgjelinek if (zone_id != GLOBAL_ZONEID && !is_system_labeled()) { 17420Sstevel@tonic-gate fake_up_local_zone(zone_id, &zent); 17430Sstevel@tonic-gate /* 17440Sstevel@tonic-gate * main() will issue a Z_NO_ZONE error if it cannot get an 17450Sstevel@tonic-gate * id for target_zone, which in a non-global zone should 17460Sstevel@tonic-gate * happen for any zone name except `zonename`. Thus we 17470Sstevel@tonic-gate * assert() that here but don't otherwise check. 17480Sstevel@tonic-gate */ 17490Sstevel@tonic-gate assert(strcmp(zent.zname, target_zone) == 0); 17500Sstevel@tonic-gate zone_print(&zent, verbose, parsable); 17510Sstevel@tonic-gate output = B_TRUE; 17520Sstevel@tonic-gate } else if ((zentp = lookup_running_zone(target_zone)) != NULL) { 17530Sstevel@tonic-gate zone_print(zentp, verbose, parsable); 17540Sstevel@tonic-gate output = B_TRUE; 1755766Scarlsonj } else if (lookup_zone_info(target_zone, ZONE_ID_UNDEFINED, 1756766Scarlsonj &zent) == Z_OK) { 17570Sstevel@tonic-gate zone_print(&zent, verbose, parsable); 17580Sstevel@tonic-gate output = B_TRUE; 17590Sstevel@tonic-gate } 17603339Szt129084 17613339Szt129084 /* 17623339Szt129084 * Invoke brand-specific handler. Note that we do this 17633542Sgjelinek * only if we're in the global zone, and target_zone is specified 17643542Sgjelinek * and it is not the global zone. 17653339Szt129084 */ 17663542Sgjelinek if (zone_id == GLOBAL_ZONEID && target_zone != NULL && 17673542Sgjelinek strcmp(target_zone, GLOBAL_ZONENAME) != 0) 17683339Szt129084 if (invoke_brand_handler(CMD_LIST, argv) != Z_OK) 17693339Szt129084 return (Z_ERR); 17703339Szt129084 17710Sstevel@tonic-gate return (output ? Z_OK : Z_ERR); 17720Sstevel@tonic-gate } 17730Sstevel@tonic-gate 17740Sstevel@tonic-gate static void 17750Sstevel@tonic-gate sigterm(int sig) 17760Sstevel@tonic-gate { 17770Sstevel@tonic-gate /* 17780Sstevel@tonic-gate * Ignore SIG{INT,TERM}, so we don't end up in an infinite loop, 17790Sstevel@tonic-gate * then propagate the signal to our process group. 17800Sstevel@tonic-gate */ 17812712Snn35248 assert(sig == SIGINT || sig == SIGTERM); 17820Sstevel@tonic-gate (void) sigset(SIGINT, SIG_IGN); 17830Sstevel@tonic-gate (void) sigset(SIGTERM, SIG_IGN); 17840Sstevel@tonic-gate (void) kill(0, sig); 17850Sstevel@tonic-gate child_killed = B_TRUE; 17860Sstevel@tonic-gate } 17870Sstevel@tonic-gate 17880Sstevel@tonic-gate static int 17890Sstevel@tonic-gate do_subproc(char *cmdbuf) 17900Sstevel@tonic-gate { 17910Sstevel@tonic-gate char inbuf[1024]; /* arbitrary large amount */ 17920Sstevel@tonic-gate FILE *file; 17930Sstevel@tonic-gate 17942712Snn35248 do_subproc_cnt++; 17950Sstevel@tonic-gate child_killed = B_FALSE; 17960Sstevel@tonic-gate /* 17970Sstevel@tonic-gate * We use popen(3c) to launch child processes for [un]install; 17980Sstevel@tonic-gate * this library call does not return a PID, so we have to kill 17990Sstevel@tonic-gate * the whole process group. To avoid killing our parent, we 18000Sstevel@tonic-gate * become a process group leader here. But doing so can wreak 18010Sstevel@tonic-gate * havoc with reading from stdin when launched by a non-job-control 18020Sstevel@tonic-gate * shell, so we close stdin and reopen it as /dev/null first. 18030Sstevel@tonic-gate */ 18040Sstevel@tonic-gate (void) close(STDIN_FILENO); 18052712Snn35248 (void) openat(STDIN_FILENO, "/dev/null", O_RDONLY); 18062712Snn35248 if (!zoneadm_is_nested) 18072712Snn35248 (void) setpgid(0, 0); 18080Sstevel@tonic-gate (void) sigset(SIGINT, sigterm); 18090Sstevel@tonic-gate (void) sigset(SIGTERM, sigterm); 18100Sstevel@tonic-gate file = popen(cmdbuf, "r"); 18110Sstevel@tonic-gate for (;;) { 18120Sstevel@tonic-gate if (child_killed || fgets(inbuf, sizeof (inbuf), file) == NULL) 18130Sstevel@tonic-gate break; 18140Sstevel@tonic-gate (void) fputs(inbuf, stdout); 18150Sstevel@tonic-gate } 18160Sstevel@tonic-gate (void) sigset(SIGINT, SIG_DFL); 18170Sstevel@tonic-gate (void) sigset(SIGTERM, SIG_DFL); 18180Sstevel@tonic-gate return (pclose(file)); 18190Sstevel@tonic-gate } 18200Sstevel@tonic-gate 18210Sstevel@tonic-gate static int 18222712Snn35248 do_subproc_interactive(char *cmdbuf) 18232712Snn35248 { 18242712Snn35248 void (*saveint)(int); 18252712Snn35248 void (*saveterm)(int); 18262712Snn35248 void (*savequit)(int); 18272712Snn35248 void (*savehup)(int); 18282712Snn35248 int pid, child, status; 18292712Snn35248 18302712Snn35248 /* 18312712Snn35248 * do_subproc() links stdin to /dev/null, which would break any 18322712Snn35248 * interactive subprocess we try to launch here. Similarly, we 18332712Snn35248 * can't have been launched as a subprocess ourselves. 18342712Snn35248 */ 18352712Snn35248 assert(do_subproc_cnt == 0 && !zoneadm_is_nested); 18362712Snn35248 18372712Snn35248 if ((child = vfork()) == 0) { 18382712Snn35248 (void) execl("/bin/sh", "sh", "-c", cmdbuf, (char *)NULL); 18392712Snn35248 } 18402712Snn35248 18412712Snn35248 if (child == -1) 18422712Snn35248 return (-1); 18432712Snn35248 18442712Snn35248 saveint = sigset(SIGINT, SIG_IGN); 18452712Snn35248 saveterm = sigset(SIGTERM, SIG_IGN); 18462712Snn35248 savequit = sigset(SIGQUIT, SIG_IGN); 18472712Snn35248 savehup = sigset(SIGHUP, SIG_IGN); 18482712Snn35248 18492712Snn35248 while ((pid = waitpid(child, &status, 0)) != child && pid != -1) 18502712Snn35248 ; 18512712Snn35248 18522712Snn35248 (void) sigset(SIGINT, saveint); 18532712Snn35248 (void) sigset(SIGTERM, saveterm); 18542712Snn35248 (void) sigset(SIGQUIT, savequit); 18552712Snn35248 (void) sigset(SIGHUP, savehup); 18562712Snn35248 18572712Snn35248 return (pid == -1 ? -1 : status); 18582712Snn35248 } 18592712Snn35248 18602712Snn35248 static int 18612712Snn35248 subproc_status(const char *cmd, int status, boolean_t verbose_failure) 18620Sstevel@tonic-gate { 18630Sstevel@tonic-gate if (WIFEXITED(status)) { 18640Sstevel@tonic-gate int exit_code = WEXITSTATUS(status); 18650Sstevel@tonic-gate 18662712Snn35248 if ((verbose_failure) && (exit_code != ZONE_SUBPROC_OK)) 18672712Snn35248 zerror(gettext("'%s' failed with exit code %d."), cmd, 18682712Snn35248 exit_code); 18692712Snn35248 18702712Snn35248 return (exit_code); 18710Sstevel@tonic-gate } else if (WIFSIGNALED(status)) { 18720Sstevel@tonic-gate int signal = WTERMSIG(status); 18730Sstevel@tonic-gate char sigstr[SIG2STR_MAX]; 18740Sstevel@tonic-gate 18750Sstevel@tonic-gate if (sig2str(signal, sigstr) == 0) { 18760Sstevel@tonic-gate zerror(gettext("'%s' terminated by signal SIG%s."), cmd, 18770Sstevel@tonic-gate sigstr); 18780Sstevel@tonic-gate } else { 18790Sstevel@tonic-gate zerror(gettext("'%s' terminated by an unknown signal."), 18800Sstevel@tonic-gate cmd); 18810Sstevel@tonic-gate } 18820Sstevel@tonic-gate } else { 18830Sstevel@tonic-gate zerror(gettext("'%s' failed for unknown reasons."), cmd); 18840Sstevel@tonic-gate } 18852712Snn35248 18862712Snn35248 /* 18872712Snn35248 * Assume a subprocess that died due to a signal or an unknown error 18882712Snn35248 * should be considered an exit code of ZONE_SUBPROC_FATAL, as the 18892712Snn35248 * user will likely need to do some manual cleanup. 18902712Snn35248 */ 18912712Snn35248 return (ZONE_SUBPROC_FATAL); 18920Sstevel@tonic-gate } 18930Sstevel@tonic-gate 18940Sstevel@tonic-gate /* 18950Sstevel@tonic-gate * Various sanity checks; make sure: 18960Sstevel@tonic-gate * 1. We're in the global zone. 18970Sstevel@tonic-gate * 2. The calling user has sufficient privilege. 18980Sstevel@tonic-gate * 3. The target zone is neither the global zone nor anything starting with 18990Sstevel@tonic-gate * "SUNW". 19000Sstevel@tonic-gate * 4a. If we're looking for a 'not running' (i.e., configured or installed) 19010Sstevel@tonic-gate * zone, the name service knows about it. 19020Sstevel@tonic-gate * 4b. For some operations which expect a zone not to be running, that it is 19030Sstevel@tonic-gate * not already running (or ready). 19040Sstevel@tonic-gate */ 19050Sstevel@tonic-gate static int 19060Sstevel@tonic-gate sanity_check(char *zone, int cmd_num, boolean_t running, 19072712Snn35248 boolean_t unsafe_when_running, boolean_t force) 19080Sstevel@tonic-gate { 19090Sstevel@tonic-gate zone_entry_t *zent; 19100Sstevel@tonic-gate priv_set_t *privset; 19112712Snn35248 zone_state_t state, min_state; 1912766Scarlsonj char kernzone[ZONENAME_MAX]; 1913766Scarlsonj FILE *fp; 19140Sstevel@tonic-gate 19150Sstevel@tonic-gate if (getzoneid() != GLOBAL_ZONEID) { 19161645Scomay switch (cmd_num) { 19171645Scomay case CMD_HALT: 19181645Scomay zerror(gettext("use %s to %s this zone."), "halt(1M)", 19191645Scomay cmd_to_str(cmd_num)); 19201645Scomay break; 19211645Scomay case CMD_REBOOT: 19221645Scomay zerror(gettext("use %s to %s this zone."), 19231645Scomay "reboot(1M)", cmd_to_str(cmd_num)); 19241645Scomay break; 19251645Scomay default: 19261645Scomay zerror(gettext("must be in the global zone to %s a " 19271645Scomay "zone."), cmd_to_str(cmd_num)); 19281645Scomay break; 19291645Scomay } 19300Sstevel@tonic-gate return (Z_ERR); 19310Sstevel@tonic-gate } 19320Sstevel@tonic-gate 19330Sstevel@tonic-gate if ((privset = priv_allocset()) == NULL) { 19340Sstevel@tonic-gate zerror(gettext("%s failed"), "priv_allocset"); 19350Sstevel@tonic-gate return (Z_ERR); 19360Sstevel@tonic-gate } 19370Sstevel@tonic-gate 19380Sstevel@tonic-gate if (getppriv(PRIV_EFFECTIVE, privset) != 0) { 19390Sstevel@tonic-gate zerror(gettext("%s failed"), "getppriv"); 19400Sstevel@tonic-gate priv_freeset(privset); 19410Sstevel@tonic-gate return (Z_ERR); 19420Sstevel@tonic-gate } 19430Sstevel@tonic-gate 19440Sstevel@tonic-gate if (priv_isfullset(privset) == B_FALSE) { 19450Sstevel@tonic-gate zerror(gettext("only a privileged user may %s a zone."), 19460Sstevel@tonic-gate cmd_to_str(cmd_num)); 19470Sstevel@tonic-gate priv_freeset(privset); 19480Sstevel@tonic-gate return (Z_ERR); 19490Sstevel@tonic-gate } 19500Sstevel@tonic-gate priv_freeset(privset); 19510Sstevel@tonic-gate 19520Sstevel@tonic-gate if (zone == NULL) { 19530Sstevel@tonic-gate zerror(gettext("no zone specified")); 19540Sstevel@tonic-gate return (Z_ERR); 19550Sstevel@tonic-gate } 19560Sstevel@tonic-gate 19570Sstevel@tonic-gate if (strcmp(zone, GLOBAL_ZONENAME) == 0) { 19580Sstevel@tonic-gate zerror(gettext("%s operation is invalid for the global zone."), 19590Sstevel@tonic-gate cmd_to_str(cmd_num)); 19600Sstevel@tonic-gate return (Z_ERR); 19610Sstevel@tonic-gate } 19620Sstevel@tonic-gate 19630Sstevel@tonic-gate if (strncmp(zone, "SUNW", 4) == 0) { 19640Sstevel@tonic-gate zerror(gettext("%s operation is invalid for zones starting " 19650Sstevel@tonic-gate "with SUNW."), cmd_to_str(cmd_num)); 19660Sstevel@tonic-gate return (Z_ERR); 19670Sstevel@tonic-gate } 19680Sstevel@tonic-gate 19694350Std153743 if (!is_native_zone && !is_cluster_zone && cmd_num == CMD_MOUNT) { 19702712Snn35248 zerror(gettext("%s operation is invalid for branded zones."), 19712712Snn35248 cmd_to_str(cmd_num)); 19722712Snn35248 return (Z_ERR); 19732712Snn35248 } 19742712Snn35248 1975766Scarlsonj if (!zonecfg_in_alt_root()) { 1976766Scarlsonj zent = lookup_running_zone(zone); 1977766Scarlsonj } else if ((fp = zonecfg_open_scratch("", B_FALSE)) == NULL) { 1978766Scarlsonj zent = NULL; 1979766Scarlsonj } else { 1980766Scarlsonj if (zonecfg_find_scratch(fp, zone, zonecfg_get_root(), 1981766Scarlsonj kernzone, sizeof (kernzone)) == 0) 1982766Scarlsonj zent = lookup_running_zone(kernzone); 1983766Scarlsonj else 1984766Scarlsonj zent = NULL; 1985766Scarlsonj zonecfg_close_scratch(fp); 1986766Scarlsonj } 1987766Scarlsonj 19880Sstevel@tonic-gate /* 19890Sstevel@tonic-gate * Look up from the kernel for 'running' zones. 19900Sstevel@tonic-gate */ 19912712Snn35248 if (running && !force) { 19920Sstevel@tonic-gate if (zent == NULL) { 19930Sstevel@tonic-gate zerror(gettext("not running")); 19940Sstevel@tonic-gate return (Z_ERR); 19950Sstevel@tonic-gate } 19960Sstevel@tonic-gate } else { 19970Sstevel@tonic-gate int err; 19980Sstevel@tonic-gate 19990Sstevel@tonic-gate if (unsafe_when_running && zent != NULL) { 20000Sstevel@tonic-gate /* check whether the zone is ready or running */ 20010Sstevel@tonic-gate if ((err = zone_get_state(zent->zname, 20020Sstevel@tonic-gate &zent->zstate_num)) != Z_OK) { 20030Sstevel@tonic-gate errno = err; 20040Sstevel@tonic-gate zperror2(zent->zname, 20050Sstevel@tonic-gate gettext("could not get state")); 20060Sstevel@tonic-gate /* can't tell, so hedge */ 20070Sstevel@tonic-gate zent->zstate_str = "ready/running"; 20080Sstevel@tonic-gate } else { 20090Sstevel@tonic-gate zent->zstate_str = 20100Sstevel@tonic-gate zone_state_str(zent->zstate_num); 20110Sstevel@tonic-gate } 20120Sstevel@tonic-gate zerror(gettext("%s operation is invalid for %s zones."), 20130Sstevel@tonic-gate cmd_to_str(cmd_num), zent->zstate_str); 20140Sstevel@tonic-gate return (Z_ERR); 20150Sstevel@tonic-gate } 20160Sstevel@tonic-gate if ((err = zone_get_state(zone, &state)) != Z_OK) { 20170Sstevel@tonic-gate errno = err; 20180Sstevel@tonic-gate zperror2(zone, gettext("could not get state")); 20190Sstevel@tonic-gate return (Z_ERR); 20200Sstevel@tonic-gate } 20210Sstevel@tonic-gate switch (cmd_num) { 20220Sstevel@tonic-gate case CMD_UNINSTALL: 20230Sstevel@tonic-gate if (state == ZONE_STATE_CONFIGURED) { 20240Sstevel@tonic-gate zerror(gettext("is already in state '%s'."), 20250Sstevel@tonic-gate zone_state_str(ZONE_STATE_CONFIGURED)); 20260Sstevel@tonic-gate return (Z_ERR); 20270Sstevel@tonic-gate } 20280Sstevel@tonic-gate break; 20291507Sgjelinek case CMD_ATTACH: 20301300Sgjelinek case CMD_CLONE: 20310Sstevel@tonic-gate case CMD_INSTALL: 20320Sstevel@tonic-gate if (state == ZONE_STATE_INSTALLED) { 20330Sstevel@tonic-gate zerror(gettext("is already %s."), 20340Sstevel@tonic-gate zone_state_str(ZONE_STATE_INSTALLED)); 20350Sstevel@tonic-gate return (Z_ERR); 20360Sstevel@tonic-gate } else if (state == ZONE_STATE_INCOMPLETE) { 20370Sstevel@tonic-gate zerror(gettext("zone is %s; %s required."), 20380Sstevel@tonic-gate zone_state_str(ZONE_STATE_INCOMPLETE), 20390Sstevel@tonic-gate cmd_to_str(CMD_UNINSTALL)); 20400Sstevel@tonic-gate return (Z_ERR); 20410Sstevel@tonic-gate } 20420Sstevel@tonic-gate break; 20431507Sgjelinek case CMD_DETACH: 20441300Sgjelinek case CMD_MOVE: 20450Sstevel@tonic-gate case CMD_READY: 20460Sstevel@tonic-gate case CMD_BOOT: 2047766Scarlsonj case CMD_MOUNT: 20482303Scarlsonj case CMD_MARK: 20492712Snn35248 if ((cmd_num == CMD_BOOT || cmd_num == CMD_MOUNT) && 20502712Snn35248 force) 20512712Snn35248 min_state = ZONE_STATE_INCOMPLETE; 20522712Snn35248 else 20532712Snn35248 min_state = ZONE_STATE_INSTALLED; 20542712Snn35248 20552712Snn35248 if (force && cmd_num == CMD_BOOT && is_native_zone) { 20562712Snn35248 zerror(gettext("Only branded zones may be " 20572712Snn35248 "force-booted.")); 20582712Snn35248 return (Z_ERR); 20592712Snn35248 } 20602712Snn35248 20612712Snn35248 if (state < min_state) { 20620Sstevel@tonic-gate zerror(gettext("must be %s before %s."), 20632712Snn35248 zone_state_str(min_state), 20640Sstevel@tonic-gate cmd_to_str(cmd_num)); 20650Sstevel@tonic-gate return (Z_ERR); 20660Sstevel@tonic-gate } 20670Sstevel@tonic-gate break; 20680Sstevel@tonic-gate case CMD_VERIFY: 20690Sstevel@tonic-gate if (state == ZONE_STATE_INCOMPLETE) { 20700Sstevel@tonic-gate zerror(gettext("zone is %s; %s required."), 20710Sstevel@tonic-gate zone_state_str(ZONE_STATE_INCOMPLETE), 20720Sstevel@tonic-gate cmd_to_str(CMD_UNINSTALL)); 20730Sstevel@tonic-gate return (Z_ERR); 20740Sstevel@tonic-gate } 20750Sstevel@tonic-gate break; 2076766Scarlsonj case CMD_UNMOUNT: 2077766Scarlsonj if (state != ZONE_STATE_MOUNTED) { 2078766Scarlsonj zerror(gettext("must be %s before %s."), 2079766Scarlsonj zone_state_str(ZONE_STATE_MOUNTED), 2080766Scarlsonj cmd_to_str(cmd_num)); 2081766Scarlsonj return (Z_ERR); 2082766Scarlsonj } 2083766Scarlsonj break; 20840Sstevel@tonic-gate } 20850Sstevel@tonic-gate } 20860Sstevel@tonic-gate return (Z_OK); 20870Sstevel@tonic-gate } 20880Sstevel@tonic-gate 20890Sstevel@tonic-gate static int 20900Sstevel@tonic-gate halt_func(int argc, char *argv[]) 20910Sstevel@tonic-gate { 20920Sstevel@tonic-gate zone_cmd_arg_t zarg; 20930Sstevel@tonic-gate int arg; 20940Sstevel@tonic-gate 2095766Scarlsonj if (zonecfg_in_alt_root()) { 2096766Scarlsonj zerror(gettext("cannot halt zone in alternate root")); 2097766Scarlsonj return (Z_ERR); 2098766Scarlsonj } 2099766Scarlsonj 21000Sstevel@tonic-gate optind = 0; 21010Sstevel@tonic-gate if ((arg = getopt(argc, argv, "?")) != EOF) { 21020Sstevel@tonic-gate switch (arg) { 21030Sstevel@tonic-gate case '?': 21040Sstevel@tonic-gate sub_usage(SHELP_HALT, CMD_HALT); 21050Sstevel@tonic-gate return (optopt == '?' ? Z_OK : Z_USAGE); 21060Sstevel@tonic-gate default: 21070Sstevel@tonic-gate sub_usage(SHELP_HALT, CMD_HALT); 21080Sstevel@tonic-gate return (Z_USAGE); 21090Sstevel@tonic-gate } 21100Sstevel@tonic-gate } 21110Sstevel@tonic-gate if (argc > optind) { 21120Sstevel@tonic-gate sub_usage(SHELP_HALT, CMD_HALT); 21130Sstevel@tonic-gate return (Z_USAGE); 21140Sstevel@tonic-gate } 21150Sstevel@tonic-gate /* 21160Sstevel@tonic-gate * zoneadmd should be the one to decide whether or not to proceed, 21170Sstevel@tonic-gate * so even though it seems that the fourth parameter below should 21180Sstevel@tonic-gate * perhaps be B_TRUE, it really shouldn't be. 21190Sstevel@tonic-gate */ 21202712Snn35248 if (sanity_check(target_zone, CMD_HALT, B_FALSE, B_FALSE, B_FALSE) 21212712Snn35248 != Z_OK) 21220Sstevel@tonic-gate return (Z_ERR); 21230Sstevel@tonic-gate 21243339Szt129084 /* 21253339Szt129084 * Invoke brand-specific handler. 21263339Szt129084 */ 21273339Szt129084 if (invoke_brand_handler(CMD_HALT, argv) != Z_OK) 21283339Szt129084 return (Z_ERR); 21293339Szt129084 21300Sstevel@tonic-gate zarg.cmd = Z_HALT; 21310Sstevel@tonic-gate return ((call_zoneadmd(target_zone, &zarg) == 0) ? Z_OK : Z_ERR); 21320Sstevel@tonic-gate } 21330Sstevel@tonic-gate 21340Sstevel@tonic-gate static int 21350Sstevel@tonic-gate reboot_func(int argc, char *argv[]) 21360Sstevel@tonic-gate { 21370Sstevel@tonic-gate zone_cmd_arg_t zarg; 21380Sstevel@tonic-gate int arg; 21390Sstevel@tonic-gate 2140766Scarlsonj if (zonecfg_in_alt_root()) { 2141766Scarlsonj zerror(gettext("cannot reboot zone in alternate root")); 2142766Scarlsonj return (Z_ERR); 2143766Scarlsonj } 2144766Scarlsonj 21450Sstevel@tonic-gate optind = 0; 21460Sstevel@tonic-gate if ((arg = getopt(argc, argv, "?")) != EOF) { 21470Sstevel@tonic-gate switch (arg) { 21480Sstevel@tonic-gate case '?': 21490Sstevel@tonic-gate sub_usage(SHELP_REBOOT, CMD_REBOOT); 21500Sstevel@tonic-gate return (optopt == '?' ? Z_OK : Z_USAGE); 21510Sstevel@tonic-gate default: 21520Sstevel@tonic-gate sub_usage(SHELP_REBOOT, CMD_REBOOT); 21530Sstevel@tonic-gate return (Z_USAGE); 21540Sstevel@tonic-gate } 21550Sstevel@tonic-gate } 21562267Sdp 21572267Sdp zarg.bootbuf[0] = '\0'; 21582267Sdp for (; optind < argc; optind++) { 21592267Sdp if (strlcat(zarg.bootbuf, argv[optind], 21602267Sdp sizeof (zarg.bootbuf)) >= sizeof (zarg.bootbuf)) { 21612267Sdp zerror(gettext("Boot argument list too long")); 21622267Sdp return (Z_ERR); 21632267Sdp } 21642267Sdp if (optind < argc - 1) 21652267Sdp if (strlcat(zarg.bootbuf, " ", sizeof (zarg.bootbuf)) >= 21662267Sdp sizeof (zarg.bootbuf)) { 21672267Sdp zerror(gettext("Boot argument list too long")); 21682267Sdp return (Z_ERR); 21692267Sdp } 21702267Sdp } 21712267Sdp 21722267Sdp 21730Sstevel@tonic-gate /* 21740Sstevel@tonic-gate * zoneadmd should be the one to decide whether or not to proceed, 21750Sstevel@tonic-gate * so even though it seems that the fourth parameter below should 21760Sstevel@tonic-gate * perhaps be B_TRUE, it really shouldn't be. 21770Sstevel@tonic-gate */ 21782712Snn35248 if (sanity_check(target_zone, CMD_REBOOT, B_TRUE, B_FALSE, B_FALSE) 21792712Snn35248 != Z_OK) 21800Sstevel@tonic-gate return (Z_ERR); 21813339Szt129084 if (verify_details(CMD_REBOOT, argv) != Z_OK) 2182823Sgjelinek return (Z_ERR); 21830Sstevel@tonic-gate 21840Sstevel@tonic-gate zarg.cmd = Z_REBOOT; 21850Sstevel@tonic-gate return ((call_zoneadmd(target_zone, &zarg) == 0) ? Z_OK : Z_ERR); 21860Sstevel@tonic-gate } 21870Sstevel@tonic-gate 21880Sstevel@tonic-gate static int 21893339Szt129084 verify_brand(zone_dochandle_t handle, int cmd_num, char *argv[]) 21902712Snn35248 { 21912712Snn35248 char cmdbuf[MAXPATHLEN]; 21922712Snn35248 int err; 21932712Snn35248 char zonepath[MAXPATHLEN]; 21942727Sedp brand_handle_t bh = NULL; 21953339Szt129084 int status, i; 21962712Snn35248 21972712Snn35248 /* 21982712Snn35248 * Fetch the verify command from the brand configuration. 21992712Snn35248 * "exec" the command so that the returned status is that of 22002712Snn35248 * the command and not the shell. 22012712Snn35248 */ 22022712Snn35248 if ((err = zonecfg_get_zonepath(handle, zonepath, sizeof (zonepath))) != 22032712Snn35248 Z_OK) { 22042712Snn35248 errno = err; 22053339Szt129084 zperror(cmd_to_str(cmd_num), B_TRUE); 22062712Snn35248 return (Z_ERR); 22072712Snn35248 } 22082727Sedp if ((bh = brand_open(target_brand)) == NULL) { 22092712Snn35248 zerror(gettext("missing or invalid brand")); 22102712Snn35248 return (Z_ERR); 22112712Snn35248 } 22122712Snn35248 22132712Snn35248 /* 22142712Snn35248 * If the brand has its own verification routine, execute it now. 22153339Szt129084 * The verification routine validates the intended zoneadm 22163339Szt129084 * operation for the specific brand. The zoneadm subcommand and 22173339Szt129084 * all its arguments are passed to the routine. 22182712Snn35248 */ 22192712Snn35248 (void) strcpy(cmdbuf, EXEC_PREFIX); 22202727Sedp err = brand_get_verify_adm(bh, target_zone, zonepath, 22212712Snn35248 cmdbuf + EXEC_LEN, sizeof (cmdbuf) - EXEC_LEN, 0, NULL); 22222727Sedp brand_close(bh); 22233339Szt129084 if (err != 0) 22243339Szt129084 return (Z_BRAND_ERROR); 22253339Szt129084 if (strlen(cmdbuf) <= EXEC_LEN) 22263339Szt129084 return (Z_OK); 22273339Szt129084 22283339Szt129084 if (strlcat(cmdbuf, cmd_to_str(cmd_num), 22293339Szt129084 sizeof (cmdbuf)) >= sizeof (cmdbuf)) 22303339Szt129084 return (Z_ERR); 22313339Szt129084 22323339Szt129084 /* Build the argv string */ 22333339Szt129084 i = 0; 22343339Szt129084 while (argv[i] != NULL) { 22353339Szt129084 if ((strlcat(cmdbuf, " ", 22363339Szt129084 sizeof (cmdbuf)) >= sizeof (cmdbuf)) || 22373339Szt129084 (strlcat(cmdbuf, argv[i++], 22383339Szt129084 sizeof (cmdbuf)) >= sizeof (cmdbuf))) 22393339Szt129084 return (Z_ERR); 22403339Szt129084 } 22413339Szt129084 22423686Sgjelinek if (zoneadm_is_nested) 22433686Sgjelinek status = do_subproc(cmdbuf); 22443686Sgjelinek else 22453686Sgjelinek status = do_subproc_interactive(cmdbuf); 22463339Szt129084 err = subproc_status(gettext("brand-specific verification"), 22473339Szt129084 status, B_FALSE); 22483339Szt129084 22493339Szt129084 return ((err == ZONE_SUBPROC_OK) ? Z_OK : Z_BRAND_ERROR); 22502712Snn35248 } 22512712Snn35248 22522712Snn35248 static int 22530Sstevel@tonic-gate verify_rctls(zone_dochandle_t handle) 22540Sstevel@tonic-gate { 22550Sstevel@tonic-gate struct zone_rctltab rctltab; 22560Sstevel@tonic-gate size_t rbs = rctlblk_size(); 22570Sstevel@tonic-gate rctlblk_t *rctlblk; 22580Sstevel@tonic-gate int error = Z_INVAL; 22590Sstevel@tonic-gate 22600Sstevel@tonic-gate if ((rctlblk = malloc(rbs)) == NULL) { 22610Sstevel@tonic-gate zerror(gettext("failed to allocate %lu bytes: %s"), rbs, 22620Sstevel@tonic-gate strerror(errno)); 22630Sstevel@tonic-gate return (Z_NOMEM); 22640Sstevel@tonic-gate } 22650Sstevel@tonic-gate 22660Sstevel@tonic-gate if (zonecfg_setrctlent(handle) != Z_OK) { 22670Sstevel@tonic-gate zerror(gettext("zonecfg_setrctlent failed")); 22680Sstevel@tonic-gate free(rctlblk); 22690Sstevel@tonic-gate return (error); 22700Sstevel@tonic-gate } 22710Sstevel@tonic-gate 22720Sstevel@tonic-gate rctltab.zone_rctl_valptr = NULL; 22730Sstevel@tonic-gate while (zonecfg_getrctlent(handle, &rctltab) == Z_OK) { 22740Sstevel@tonic-gate struct zone_rctlvaltab *rctlval; 22750Sstevel@tonic-gate const char *name = rctltab.zone_rctl_name; 22760Sstevel@tonic-gate 22770Sstevel@tonic-gate if (!zonecfg_is_rctl(name)) { 22780Sstevel@tonic-gate zerror(gettext("WARNING: Ignoring unrecognized rctl " 22790Sstevel@tonic-gate "'%s'."), name); 22800Sstevel@tonic-gate zonecfg_free_rctl_value_list(rctltab.zone_rctl_valptr); 22810Sstevel@tonic-gate rctltab.zone_rctl_valptr = NULL; 22820Sstevel@tonic-gate continue; 22830Sstevel@tonic-gate } 22840Sstevel@tonic-gate 22850Sstevel@tonic-gate for (rctlval = rctltab.zone_rctl_valptr; rctlval != NULL; 22860Sstevel@tonic-gate rctlval = rctlval->zone_rctlval_next) { 22870Sstevel@tonic-gate if (zonecfg_construct_rctlblk(rctlval, rctlblk) 22880Sstevel@tonic-gate != Z_OK) { 22890Sstevel@tonic-gate zerror(gettext("invalid rctl value: " 22900Sstevel@tonic-gate "(priv=%s,limit=%s,action%s)"), 22910Sstevel@tonic-gate rctlval->zone_rctlval_priv, 22920Sstevel@tonic-gate rctlval->zone_rctlval_limit, 22930Sstevel@tonic-gate rctlval->zone_rctlval_action); 22940Sstevel@tonic-gate goto out; 22950Sstevel@tonic-gate } 22960Sstevel@tonic-gate if (!zonecfg_valid_rctl(name, rctlblk)) { 22970Sstevel@tonic-gate zerror(gettext("(priv=%s,limit=%s,action=%s) " 22980Sstevel@tonic-gate "is not a valid value for rctl '%s'"), 22990Sstevel@tonic-gate rctlval->zone_rctlval_priv, 23000Sstevel@tonic-gate rctlval->zone_rctlval_limit, 23010Sstevel@tonic-gate rctlval->zone_rctlval_action, 23020Sstevel@tonic-gate name); 23030Sstevel@tonic-gate goto out; 23040Sstevel@tonic-gate } 23050Sstevel@tonic-gate } 23060Sstevel@tonic-gate zonecfg_free_rctl_value_list(rctltab.zone_rctl_valptr); 23070Sstevel@tonic-gate } 23080Sstevel@tonic-gate rctltab.zone_rctl_valptr = NULL; 23090Sstevel@tonic-gate error = Z_OK; 23100Sstevel@tonic-gate out: 23110Sstevel@tonic-gate zonecfg_free_rctl_value_list(rctltab.zone_rctl_valptr); 23120Sstevel@tonic-gate (void) zonecfg_endrctlent(handle); 23130Sstevel@tonic-gate free(rctlblk); 23140Sstevel@tonic-gate return (error); 23150Sstevel@tonic-gate } 23160Sstevel@tonic-gate 23170Sstevel@tonic-gate static int 23180Sstevel@tonic-gate verify_pool(zone_dochandle_t handle) 23190Sstevel@tonic-gate { 23200Sstevel@tonic-gate char poolname[MAXPATHLEN]; 23210Sstevel@tonic-gate pool_conf_t *poolconf; 23220Sstevel@tonic-gate pool_t *pool; 23230Sstevel@tonic-gate int status; 23240Sstevel@tonic-gate int error; 23250Sstevel@tonic-gate 23260Sstevel@tonic-gate /* 23270Sstevel@tonic-gate * This ends up being very similar to the check done in zoneadmd. 23280Sstevel@tonic-gate */ 23290Sstevel@tonic-gate error = zonecfg_get_pool(handle, poolname, sizeof (poolname)); 23300Sstevel@tonic-gate if (error == Z_NO_ENTRY || (error == Z_OK && strlen(poolname) == 0)) { 23310Sstevel@tonic-gate /* 23320Sstevel@tonic-gate * No pool specified. 23330Sstevel@tonic-gate */ 23340Sstevel@tonic-gate return (0); 23350Sstevel@tonic-gate } 23360Sstevel@tonic-gate if (error != Z_OK) { 23370Sstevel@tonic-gate zperror(gettext("Unable to retrieve pool name from " 23380Sstevel@tonic-gate "configuration"), B_TRUE); 23390Sstevel@tonic-gate return (error); 23400Sstevel@tonic-gate } 23410Sstevel@tonic-gate /* 23420Sstevel@tonic-gate * Don't do anything if pools aren't enabled. 23430Sstevel@tonic-gate */ 23440Sstevel@tonic-gate if (pool_get_status(&status) != PO_SUCCESS || status != POOL_ENABLED) { 23450Sstevel@tonic-gate zerror(gettext("WARNING: pools facility not active; " 23460Sstevel@tonic-gate "zone will not be bound to pool '%s'."), poolname); 23470Sstevel@tonic-gate return (Z_OK); 23480Sstevel@tonic-gate } 23490Sstevel@tonic-gate /* 23500Sstevel@tonic-gate * Try to provide a sane error message if the requested pool doesn't 23510Sstevel@tonic-gate * exist. It isn't clear that pools-related failures should 23520Sstevel@tonic-gate * necessarily translate to a failure to verify the zone configuration, 23530Sstevel@tonic-gate * hence they are not considered errors. 23540Sstevel@tonic-gate */ 23550Sstevel@tonic-gate if ((poolconf = pool_conf_alloc()) == NULL) { 23560Sstevel@tonic-gate zerror(gettext("WARNING: pool_conf_alloc failed; " 23570Sstevel@tonic-gate "using default pool")); 23580Sstevel@tonic-gate return (Z_OK); 23590Sstevel@tonic-gate } 23600Sstevel@tonic-gate if (pool_conf_open(poolconf, pool_dynamic_location(), PO_RDONLY) != 23610Sstevel@tonic-gate PO_SUCCESS) { 23620Sstevel@tonic-gate zerror(gettext("WARNING: pool_conf_open failed; " 23630Sstevel@tonic-gate "using default pool")); 23640Sstevel@tonic-gate pool_conf_free(poolconf); 23650Sstevel@tonic-gate return (Z_OK); 23660Sstevel@tonic-gate } 23670Sstevel@tonic-gate pool = pool_get_pool(poolconf, poolname); 23680Sstevel@tonic-gate (void) pool_conf_close(poolconf); 23690Sstevel@tonic-gate pool_conf_free(poolconf); 23700Sstevel@tonic-gate if (pool == NULL) { 23710Sstevel@tonic-gate zerror(gettext("WARNING: pool '%s' not found. " 23720Sstevel@tonic-gate "using default pool"), poolname); 23730Sstevel@tonic-gate } 23740Sstevel@tonic-gate 23750Sstevel@tonic-gate return (Z_OK); 23760Sstevel@tonic-gate } 23770Sstevel@tonic-gate 23780Sstevel@tonic-gate static int 2379823Sgjelinek verify_ipd(zone_dochandle_t handle) 2380823Sgjelinek { 2381823Sgjelinek int return_code = Z_OK; 2382823Sgjelinek struct zone_fstab fstab; 2383823Sgjelinek struct stat st; 2384823Sgjelinek char specdir[MAXPATHLEN]; 2385823Sgjelinek 2386823Sgjelinek if (zonecfg_setipdent(handle) != Z_OK) { 2387924Sgjelinek /* 2388924Sgjelinek * TRANSLATION_NOTE 2389924Sgjelinek * inherit-pkg-dirs is a literal that should not be translated. 2390924Sgjelinek */ 2391924Sgjelinek (void) fprintf(stderr, gettext("could not verify " 2392823Sgjelinek "inherit-pkg-dirs: unable to enumerate mounts\n")); 2393823Sgjelinek return (Z_ERR); 2394823Sgjelinek } 2395823Sgjelinek while (zonecfg_getipdent(handle, &fstab) == Z_OK) { 2396823Sgjelinek /* 2397823Sgjelinek * Verify fs_dir exists. 2398823Sgjelinek */ 2399823Sgjelinek (void) snprintf(specdir, sizeof (specdir), "%s%s", 2400823Sgjelinek zonecfg_get_root(), fstab.zone_fs_dir); 2401823Sgjelinek if (stat(specdir, &st) != 0) { 2402924Sgjelinek /* 2403924Sgjelinek * TRANSLATION_NOTE 2404924Sgjelinek * inherit-pkg-dir is a literal that should not be 2405924Sgjelinek * translated. 2406924Sgjelinek */ 2407924Sgjelinek (void) fprintf(stderr, gettext("could not verify " 2408823Sgjelinek "inherit-pkg-dir %s: %s\n"), 2409823Sgjelinek fstab.zone_fs_dir, strerror(errno)); 2410823Sgjelinek return_code = Z_ERR; 2411823Sgjelinek } 2412823Sgjelinek if (strcmp(st.st_fstype, MNTTYPE_NFS) == 0) { 2413924Sgjelinek /* 2414924Sgjelinek * TRANSLATION_NOTE 2415924Sgjelinek * inherit-pkg-dir and NFS are literals that should 2416924Sgjelinek * not be translated. 2417924Sgjelinek */ 2418823Sgjelinek (void) fprintf(stderr, gettext("cannot verify " 24191867Sgjelinek "inherit-pkg-dir %s: NFS mounted file system.\n" 24201867Sgjelinek "\tA local file system must be used.\n"), 2421823Sgjelinek fstab.zone_fs_dir); 2422823Sgjelinek return_code = Z_ERR; 2423823Sgjelinek } 2424823Sgjelinek } 2425823Sgjelinek (void) zonecfg_endipdent(handle); 2426823Sgjelinek 2427823Sgjelinek return (return_code); 2428823Sgjelinek } 2429823Sgjelinek 24301393Slling /* 24311867Sgjelinek * Verify that the special device/file system exists and is valid. 24321393Slling */ 24331393Slling static int 24341393Slling verify_fs_special(struct zone_fstab *fstab) 24351393Slling { 24361393Slling struct stat st; 24371393Slling 24382971Sgjelinek /* 24392971Sgjelinek * This validation is really intended for standard zone administration. 24402971Sgjelinek * If we are in a mini-root or some other upgrade situation where 24412971Sgjelinek * we are using the scratch zone, just by-pass this. 24422971Sgjelinek */ 24432971Sgjelinek if (zonecfg_in_alt_root()) 24442971Sgjelinek return (Z_OK); 24452971Sgjelinek 24461393Slling if (strcmp(fstab->zone_fs_type, MNTTYPE_ZFS) == 0) 24471393Slling return (verify_fs_zfs(fstab)); 24481393Slling 24491393Slling if (stat(fstab->zone_fs_special, &st) != 0) { 24501397Slling (void) fprintf(stderr, gettext("could not verify fs " 24511393Slling "%s: could not access %s: %s\n"), fstab->zone_fs_dir, 24521393Slling fstab->zone_fs_special, strerror(errno)); 24531393Slling return (Z_ERR); 24541393Slling } 24551393Slling 24561393Slling if (strcmp(st.st_fstype, MNTTYPE_NFS) == 0) { 24571393Slling /* 24581393Slling * TRANSLATION_NOTE 24591393Slling * fs and NFS are literals that should 24601393Slling * not be translated. 24611393Slling */ 24621393Slling (void) fprintf(stderr, gettext("cannot verify " 24631867Sgjelinek "fs %s: NFS mounted file system.\n" 24641867Sgjelinek "\tA local file system must be used.\n"), 24651393Slling fstab->zone_fs_special); 24661393Slling return (Z_ERR); 24671393Slling } 24681393Slling 24691393Slling return (Z_OK); 24701393Slling } 24711393Slling 2472823Sgjelinek static int 24730Sstevel@tonic-gate verify_filesystems(zone_dochandle_t handle) 24740Sstevel@tonic-gate { 24750Sstevel@tonic-gate int return_code = Z_OK; 24760Sstevel@tonic-gate struct zone_fstab fstab; 24770Sstevel@tonic-gate char cmdbuf[MAXPATHLEN]; 24780Sstevel@tonic-gate struct stat st; 24790Sstevel@tonic-gate 24800Sstevel@tonic-gate /* 24810Sstevel@tonic-gate * No need to verify inherit-pkg-dir fs types, as their type is 24820Sstevel@tonic-gate * implicitly lofs, which is known. Therefore, the types are only 24831867Sgjelinek * verified for regular file systems below. 24840Sstevel@tonic-gate * 24850Sstevel@tonic-gate * Since the actual mount point is not known until the dependent mounts 24860Sstevel@tonic-gate * are performed, we don't attempt any path validation here: that will 24870Sstevel@tonic-gate * happen later when zoneadmd actually does the mounts. 24880Sstevel@tonic-gate */ 24890Sstevel@tonic-gate if (zonecfg_setfsent(handle) != Z_OK) { 24901867Sgjelinek (void) fprintf(stderr, gettext("could not verify file systems: " 24910Sstevel@tonic-gate "unable to enumerate mounts\n")); 24920Sstevel@tonic-gate return (Z_ERR); 24930Sstevel@tonic-gate } 24940Sstevel@tonic-gate while (zonecfg_getfsent(handle, &fstab) == Z_OK) { 24950Sstevel@tonic-gate if (!zonecfg_valid_fs_type(fstab.zone_fs_type)) { 24960Sstevel@tonic-gate (void) fprintf(stderr, gettext("cannot verify fs %s: " 24970Sstevel@tonic-gate "type %s is not allowed.\n"), fstab.zone_fs_dir, 24980Sstevel@tonic-gate fstab.zone_fs_type); 24990Sstevel@tonic-gate return_code = Z_ERR; 25000Sstevel@tonic-gate goto next_fs; 25010Sstevel@tonic-gate } 25020Sstevel@tonic-gate /* 25030Sstevel@tonic-gate * Verify /usr/lib/fs/<fstype>/mount exists. 25040Sstevel@tonic-gate */ 25050Sstevel@tonic-gate if (snprintf(cmdbuf, sizeof (cmdbuf), "/usr/lib/fs/%s/mount", 25060Sstevel@tonic-gate fstab.zone_fs_type) > sizeof (cmdbuf)) { 25070Sstevel@tonic-gate (void) fprintf(stderr, gettext("cannot verify fs %s: " 25080Sstevel@tonic-gate "type %s is too long.\n"), fstab.zone_fs_dir, 25090Sstevel@tonic-gate fstab.zone_fs_type); 25100Sstevel@tonic-gate return_code = Z_ERR; 25110Sstevel@tonic-gate goto next_fs; 25120Sstevel@tonic-gate } 25130Sstevel@tonic-gate if (stat(cmdbuf, &st) != 0) { 2514924Sgjelinek (void) fprintf(stderr, gettext("could not verify fs " 2515924Sgjelinek "%s: could not access %s: %s\n"), fstab.zone_fs_dir, 25160Sstevel@tonic-gate cmdbuf, strerror(errno)); 25170Sstevel@tonic-gate return_code = Z_ERR; 25180Sstevel@tonic-gate goto next_fs; 25190Sstevel@tonic-gate } 25200Sstevel@tonic-gate if (!S_ISREG(st.st_mode)) { 2521924Sgjelinek (void) fprintf(stderr, gettext("could not verify fs " 2522924Sgjelinek "%s: %s is not a regular file\n"), 2523924Sgjelinek fstab.zone_fs_dir, cmdbuf); 25240Sstevel@tonic-gate return_code = Z_ERR; 25250Sstevel@tonic-gate goto next_fs; 25260Sstevel@tonic-gate } 25270Sstevel@tonic-gate /* 25280Sstevel@tonic-gate * Verify /usr/lib/fs/<fstype>/fsck exists iff zone_fs_raw is 25290Sstevel@tonic-gate * set. 25300Sstevel@tonic-gate */ 25310Sstevel@tonic-gate if (snprintf(cmdbuf, sizeof (cmdbuf), "/usr/lib/fs/%s/fsck", 25320Sstevel@tonic-gate fstab.zone_fs_type) > sizeof (cmdbuf)) { 25330Sstevel@tonic-gate (void) fprintf(stderr, gettext("cannot verify fs %s: " 25340Sstevel@tonic-gate "type %s is too long.\n"), fstab.zone_fs_dir, 25350Sstevel@tonic-gate fstab.zone_fs_type); 25360Sstevel@tonic-gate return_code = Z_ERR; 25370Sstevel@tonic-gate goto next_fs; 25380Sstevel@tonic-gate } 25390Sstevel@tonic-gate if (fstab.zone_fs_raw[0] == '\0' && stat(cmdbuf, &st) == 0) { 2540924Sgjelinek (void) fprintf(stderr, gettext("could not verify fs " 2541924Sgjelinek "%s: must specify 'raw' device for %s " 25421867Sgjelinek "file systems\n"), 25430Sstevel@tonic-gate fstab.zone_fs_dir, fstab.zone_fs_type); 25440Sstevel@tonic-gate return_code = Z_ERR; 25450Sstevel@tonic-gate goto next_fs; 25460Sstevel@tonic-gate } 25470Sstevel@tonic-gate if (fstab.zone_fs_raw[0] != '\0' && 25480Sstevel@tonic-gate (stat(cmdbuf, &st) != 0 || !S_ISREG(st.st_mode))) { 25490Sstevel@tonic-gate (void) fprintf(stderr, gettext("cannot verify fs %s: " 25500Sstevel@tonic-gate "'raw' device specified but " 25510Sstevel@tonic-gate "no fsck executable exists for %s\n"), 25520Sstevel@tonic-gate fstab.zone_fs_dir, fstab.zone_fs_type); 25530Sstevel@tonic-gate return_code = Z_ERR; 25540Sstevel@tonic-gate goto next_fs; 25550Sstevel@tonic-gate } 25561393Slling 25571393Slling /* Verify fs_special. */ 25581393Slling if ((return_code = verify_fs_special(&fstab)) != Z_OK) 2559823Sgjelinek goto next_fs; 25601393Slling 25611393Slling /* Verify fs_raw. */ 2562823Sgjelinek if (fstab.zone_fs_raw[0] != '\0' && 2563823Sgjelinek stat(fstab.zone_fs_raw, &st) != 0) { 2564924Sgjelinek /* 2565924Sgjelinek * TRANSLATION_NOTE 2566924Sgjelinek * fs is a literal that should not be translated. 2567924Sgjelinek */ 2568924Sgjelinek (void) fprintf(stderr, gettext("could not verify fs " 2569924Sgjelinek "%s: could not access %s: %s\n"), fstab.zone_fs_dir, 2570823Sgjelinek fstab.zone_fs_raw, strerror(errno)); 2571823Sgjelinek return_code = Z_ERR; 2572823Sgjelinek goto next_fs; 2573823Sgjelinek } 25740Sstevel@tonic-gate next_fs: 25750Sstevel@tonic-gate zonecfg_free_fs_option_list(fstab.zone_fs_options); 25760Sstevel@tonic-gate } 25770Sstevel@tonic-gate (void) zonecfg_endfsent(handle); 25780Sstevel@tonic-gate 25790Sstevel@tonic-gate return (return_code); 25800Sstevel@tonic-gate } 25810Sstevel@tonic-gate 25820Sstevel@tonic-gate static int 25831645Scomay verify_limitpriv(zone_dochandle_t handle) 25841645Scomay { 25851645Scomay char *privname = NULL; 25861645Scomay int err; 25871645Scomay priv_set_t *privs; 25881645Scomay 25891645Scomay if ((privs = priv_allocset()) == NULL) { 25901645Scomay zperror(gettext("failed to allocate privilege set"), B_FALSE); 25911645Scomay return (Z_NOMEM); 25921645Scomay } 25931645Scomay err = zonecfg_get_privset(handle, privs, &privname); 25941645Scomay switch (err) { 25951645Scomay case Z_OK: 25961645Scomay break; 25971645Scomay case Z_PRIV_PROHIBITED: 25981645Scomay (void) fprintf(stderr, gettext("privilege \"%s\" is not " 25991645Scomay "permitted within the zone's privilege set\n"), privname); 26001645Scomay break; 26011645Scomay case Z_PRIV_REQUIRED: 26021645Scomay (void) fprintf(stderr, gettext("required privilege \"%s\" is " 26031645Scomay "missing from the zone's privilege set\n"), privname); 26041645Scomay break; 26051645Scomay case Z_PRIV_UNKNOWN: 26061645Scomay (void) fprintf(stderr, gettext("unknown privilege \"%s\" " 26071645Scomay "specified in the zone's privilege set\n"), privname); 26081645Scomay break; 26091645Scomay default: 26101645Scomay zperror( 26111645Scomay gettext("failed to determine the zone's privilege set"), 26121645Scomay B_TRUE); 26131645Scomay break; 26141645Scomay } 26151645Scomay free(privname); 26161645Scomay priv_freeset(privs); 26171645Scomay return (err); 26181645Scomay } 26191645Scomay 26201915Sgjelinek static void 26211915Sgjelinek free_local_netifs(int if_cnt, struct net_if **if_list) 26221915Sgjelinek { 26231915Sgjelinek int i; 26241915Sgjelinek 26251915Sgjelinek for (i = 0; i < if_cnt; i++) { 26261915Sgjelinek free(if_list[i]->name); 26271915Sgjelinek free(if_list[i]); 26281915Sgjelinek } 26291915Sgjelinek free(if_list); 26301915Sgjelinek } 26311915Sgjelinek 26321915Sgjelinek /* 26331915Sgjelinek * Get a list of the network interfaces, along with their address families, 26341915Sgjelinek * that are plumbed in the global zone. See if_tcp(7p) for a description 26351915Sgjelinek * of the ioctls used here. 26361915Sgjelinek */ 26371915Sgjelinek static int 26381915Sgjelinek get_local_netifs(int *if_cnt, struct net_if ***if_list) 26391915Sgjelinek { 26401915Sgjelinek int s; 26411915Sgjelinek int i; 26421915Sgjelinek int res = Z_OK; 26431915Sgjelinek int space_needed; 26441915Sgjelinek int cnt = 0; 26451915Sgjelinek struct lifnum if_num; 26461915Sgjelinek struct lifconf if_conf; 26471915Sgjelinek struct lifreq *if_reqp; 26481915Sgjelinek char *if_buf; 26491915Sgjelinek struct net_if **local_ifs = NULL; 26501915Sgjelinek 26511915Sgjelinek *if_cnt = 0; 26521915Sgjelinek *if_list = NULL; 26531915Sgjelinek 26541915Sgjelinek if ((s = socket(SOCKET_AF(AF_INET), SOCK_DGRAM, 0)) < 0) 26551915Sgjelinek return (Z_ERR); 26561915Sgjelinek 26571915Sgjelinek /* 26581915Sgjelinek * Come back here in the unlikely event that the number of interfaces 26591915Sgjelinek * increases between the time we get the count and the time we do the 26601915Sgjelinek * SIOCGLIFCONF ioctl. 26611915Sgjelinek */ 26621915Sgjelinek retry: 26631915Sgjelinek /* Get the number of interfaces. */ 26641915Sgjelinek if_num.lifn_family = AF_UNSPEC; 26651915Sgjelinek if_num.lifn_flags = LIFC_NOXMIT; 26661915Sgjelinek if (ioctl(s, SIOCGLIFNUM, &if_num) < 0) { 26671915Sgjelinek (void) close(s); 26681915Sgjelinek return (Z_ERR); 26691915Sgjelinek } 26701915Sgjelinek 26711915Sgjelinek /* Get the interface configuration list. */ 26721915Sgjelinek space_needed = if_num.lifn_count * sizeof (struct lifreq); 26731915Sgjelinek if ((if_buf = malloc(space_needed)) == NULL) { 26741915Sgjelinek (void) close(s); 26751915Sgjelinek return (Z_ERR); 26761915Sgjelinek } 26771915Sgjelinek if_conf.lifc_family = AF_UNSPEC; 26781915Sgjelinek if_conf.lifc_flags = LIFC_NOXMIT; 26791915Sgjelinek if_conf.lifc_len = space_needed; 26801915Sgjelinek if_conf.lifc_buf = if_buf; 26811915Sgjelinek if (ioctl(s, SIOCGLIFCONF, &if_conf) < 0) { 26821915Sgjelinek free(if_buf); 26831915Sgjelinek /* 26841915Sgjelinek * SIOCGLIFCONF returns EINVAL if the buffer we passed in is 26851915Sgjelinek * too small. In this case go back and get the new if cnt. 26861915Sgjelinek */ 26871915Sgjelinek if (errno == EINVAL) 26881915Sgjelinek goto retry; 26891915Sgjelinek 26901915Sgjelinek (void) close(s); 26911915Sgjelinek return (Z_ERR); 26921915Sgjelinek } 26931915Sgjelinek (void) close(s); 26941915Sgjelinek 26951915Sgjelinek /* Get the name and address family for each interface. */ 26961915Sgjelinek if_reqp = if_conf.lifc_req; 26971915Sgjelinek for (i = 0; i < (if_conf.lifc_len / sizeof (struct lifreq)); i++) { 26981915Sgjelinek struct net_if **p; 26991915Sgjelinek struct lifreq req; 27001915Sgjelinek 27011915Sgjelinek if (strcmp(LOOPBACK_IF, if_reqp->lifr_name) == 0) { 27021915Sgjelinek if_reqp++; 27031915Sgjelinek continue; 27041915Sgjelinek } 27051915Sgjelinek 27061915Sgjelinek if ((s = socket(SOCKET_AF(if_reqp->lifr_addr.ss_family), 27071915Sgjelinek SOCK_DGRAM, 0)) == -1) { 27081915Sgjelinek res = Z_ERR; 27091915Sgjelinek break; 27101915Sgjelinek } 27111915Sgjelinek 27121915Sgjelinek (void) strncpy(req.lifr_name, if_reqp->lifr_name, 27131915Sgjelinek sizeof (req.lifr_name)); 27141915Sgjelinek if (ioctl(s, SIOCGLIFADDR, &req) < 0) { 27151915Sgjelinek (void) close(s); 27161915Sgjelinek if_reqp++; 27171915Sgjelinek continue; 27181915Sgjelinek } 27191915Sgjelinek 27201915Sgjelinek if ((p = (struct net_if **)realloc(local_ifs, 27211915Sgjelinek sizeof (struct net_if *) * (cnt + 1))) == NULL) { 27221915Sgjelinek res = Z_ERR; 27231915Sgjelinek break; 27241915Sgjelinek } 27251915Sgjelinek local_ifs = p; 27261915Sgjelinek 27271915Sgjelinek if ((local_ifs[cnt] = malloc(sizeof (struct net_if))) == NULL) { 27281915Sgjelinek res = Z_ERR; 27291915Sgjelinek break; 27301915Sgjelinek } 27311915Sgjelinek 27321915Sgjelinek if ((local_ifs[cnt]->name = strdup(if_reqp->lifr_name)) 27331915Sgjelinek == NULL) { 27341915Sgjelinek free(local_ifs[cnt]); 27351915Sgjelinek res = Z_ERR; 27361915Sgjelinek break; 27371915Sgjelinek } 27381915Sgjelinek local_ifs[cnt]->af = req.lifr_addr.ss_family; 27391915Sgjelinek cnt++; 27401915Sgjelinek 27411915Sgjelinek (void) close(s); 27421915Sgjelinek if_reqp++; 27431915Sgjelinek } 27441915Sgjelinek 27451915Sgjelinek free(if_buf); 27461915Sgjelinek 27471915Sgjelinek if (res != Z_OK) { 27481915Sgjelinek free_local_netifs(cnt, local_ifs); 27491915Sgjelinek } else { 27501915Sgjelinek *if_cnt = cnt; 27511915Sgjelinek *if_list = local_ifs; 27521915Sgjelinek } 27531915Sgjelinek 27541915Sgjelinek return (res); 27551915Sgjelinek } 27561915Sgjelinek 27571915Sgjelinek static char * 27581915Sgjelinek af2str(int af) 27591915Sgjelinek { 27601915Sgjelinek switch (af) { 27611915Sgjelinek case AF_INET: 27621915Sgjelinek return ("IPv4"); 27631915Sgjelinek case AF_INET6: 27641915Sgjelinek return ("IPv6"); 27651915Sgjelinek default: 27661915Sgjelinek return ("Unknown"); 27671915Sgjelinek } 27681915Sgjelinek } 27691915Sgjelinek 27701915Sgjelinek /* 27711915Sgjelinek * Cross check the network interface name and address family with the 27721915Sgjelinek * interfaces that are set up in the global zone so that we can print the 27731915Sgjelinek * appropriate error message. 27741915Sgjelinek */ 27751915Sgjelinek static void 27761915Sgjelinek print_net_err(char *phys, char *addr, int af, char *msg) 27771915Sgjelinek { 27781915Sgjelinek int i; 27791915Sgjelinek int local_if_cnt = 0; 27801915Sgjelinek struct net_if **local_ifs = NULL; 27811915Sgjelinek boolean_t found_if = B_FALSE; 27821915Sgjelinek boolean_t found_af = B_FALSE; 27831915Sgjelinek 27841915Sgjelinek if (get_local_netifs(&local_if_cnt, &local_ifs) != Z_OK) { 27851915Sgjelinek (void) fprintf(stderr, 27861915Sgjelinek gettext("could not verify %s %s=%s %s=%s\n\t%s\n"), 27871915Sgjelinek "net", "address", addr, "physical", phys, msg); 27881915Sgjelinek return; 27891915Sgjelinek } 27901915Sgjelinek 27911915Sgjelinek for (i = 0; i < local_if_cnt; i++) { 27921915Sgjelinek if (strcmp(phys, local_ifs[i]->name) == 0) { 27931915Sgjelinek found_if = B_TRUE; 27941915Sgjelinek if (af == local_ifs[i]->af) { 27951915Sgjelinek found_af = B_TRUE; 27961915Sgjelinek break; 27971915Sgjelinek } 27981915Sgjelinek } 27991915Sgjelinek } 28001915Sgjelinek 28011915Sgjelinek free_local_netifs(local_if_cnt, local_ifs); 28021915Sgjelinek 28031915Sgjelinek if (!found_if) { 28041915Sgjelinek (void) fprintf(stderr, 28051915Sgjelinek gettext("could not verify %s %s=%s\n\t" 28061915Sgjelinek "network interface %s is not plumbed in the global zone\n"), 28071915Sgjelinek "net", "physical", phys, phys); 28081915Sgjelinek return; 28091915Sgjelinek } 28101915Sgjelinek 28111915Sgjelinek /* 28121915Sgjelinek * Print this error if we were unable to find the address family 28131915Sgjelinek * for this interface. If the af variable is not initialized to 28141915Sgjelinek * to something meaningful by the caller (not AF_UNSPEC) then we 28151915Sgjelinek * also skip this message since it wouldn't be informative. 28161915Sgjelinek */ 28171915Sgjelinek if (!found_af && af != AF_UNSPEC) { 28181915Sgjelinek (void) fprintf(stderr, 28191915Sgjelinek gettext("could not verify %s %s=%s %s=%s\n\tthe %s address " 28203448Sdh155122 "family is not configured on this network interface in " 28213448Sdh155122 "the\n\tglobal zone\n"), 28221915Sgjelinek "net", "address", addr, "physical", phys, af2str(af)); 28231915Sgjelinek return; 28241915Sgjelinek } 28251915Sgjelinek 28261915Sgjelinek (void) fprintf(stderr, 28271915Sgjelinek gettext("could not verify %s %s=%s %s=%s\n\t%s\n"), 28281915Sgjelinek "net", "address", addr, "physical", phys, msg); 28291915Sgjelinek } 28301915Sgjelinek 28311645Scomay static int 28323339Szt129084 verify_handle(int cmd_num, zone_dochandle_t handle, char *argv[]) 28330Sstevel@tonic-gate { 28340Sstevel@tonic-gate struct zone_nwiftab nwiftab; 28350Sstevel@tonic-gate int return_code = Z_OK; 28360Sstevel@tonic-gate int err; 2837766Scarlsonj boolean_t in_alt_root; 28383448Sdh155122 zone_iptype_t iptype; 28394456Sss150715 dlpi_handle_t dh; 28400Sstevel@tonic-gate 2841766Scarlsonj in_alt_root = zonecfg_in_alt_root(); 2842766Scarlsonj if (in_alt_root) 2843766Scarlsonj goto no_net; 2844766Scarlsonj 28453448Sdh155122 if ((err = zonecfg_get_iptype(handle, &iptype)) != Z_OK) { 28463448Sdh155122 errno = err; 28473448Sdh155122 zperror(cmd_to_str(cmd_num), B_TRUE); 28483448Sdh155122 zonecfg_fini_handle(handle); 28493448Sdh155122 return (Z_ERR); 28503448Sdh155122 } 28510Sstevel@tonic-gate if ((err = zonecfg_setnwifent(handle)) != Z_OK) { 28520Sstevel@tonic-gate errno = err; 28530Sstevel@tonic-gate zperror(cmd_to_str(cmd_num), B_TRUE); 28540Sstevel@tonic-gate zonecfg_fini_handle(handle); 28550Sstevel@tonic-gate return (Z_ERR); 28560Sstevel@tonic-gate } 28570Sstevel@tonic-gate while (zonecfg_getnwifent(handle, &nwiftab) == Z_OK) { 28580Sstevel@tonic-gate struct lifreq lifr; 28591915Sgjelinek sa_family_t af = AF_UNSPEC; 28603448Sdh155122 char dl_owner_zname[ZONENAME_MAX]; 28613448Sdh155122 zoneid_t dl_owner_zid; 28623448Sdh155122 zoneid_t target_zid; 28633448Sdh155122 int res; 28640Sstevel@tonic-gate 28650Sstevel@tonic-gate /* skip any loopback interfaces */ 28660Sstevel@tonic-gate if (strcmp(nwiftab.zone_nwif_physical, "lo0") == 0) 28670Sstevel@tonic-gate continue; 28683448Sdh155122 switch (iptype) { 28693448Sdh155122 case ZS_SHARED: 28703448Sdh155122 if ((res = zonecfg_valid_net_address( 28713448Sdh155122 nwiftab.zone_nwif_address, &lifr)) != Z_OK) { 28723448Sdh155122 print_net_err(nwiftab.zone_nwif_physical, 28733448Sdh155122 nwiftab.zone_nwif_address, af, 28743448Sdh155122 zonecfg_strerror(res)); 28754350Std153743 return_code = Z_ERR; 28764350Std153743 continue; 28773448Sdh155122 } 28783448Sdh155122 af = lifr.lifr_addr.ss_family; 28793448Sdh155122 if (!zonecfg_ifname_exists(af, 28803448Sdh155122 nwiftab.zone_nwif_physical)) { 28813448Sdh155122 /* 28823448Sdh155122 * The interface failed to come up. We continue 28833448Sdh155122 * on anyway for the sake of consistency: a 28843448Sdh155122 * zone is not shut down if the interface fails 28853448Sdh155122 * any time after boot, nor does the global zone 28863448Sdh155122 * fail to boot if an interface fails. 28873448Sdh155122 */ 28883448Sdh155122 (void) fprintf(stderr, 28893448Sdh155122 gettext("WARNING: skipping network " 28904350Std153743 "interface '%s' which may not be " 28914350Std153743 "present/plumbed in the global " 28924350Std153743 "zone.\n"), 28933448Sdh155122 nwiftab.zone_nwif_physical); 28943448Sdh155122 } 28953448Sdh155122 break; 28963448Sdh155122 case ZS_EXCLUSIVE: 28973448Sdh155122 /* Warning if it exists for either IPv4 or IPv6 */ 28983448Sdh155122 28993448Sdh155122 if (zonecfg_ifname_exists(AF_INET, 29003448Sdh155122 nwiftab.zone_nwif_physical) || 29013448Sdh155122 zonecfg_ifname_exists(AF_INET6, 29023448Sdh155122 nwiftab.zone_nwif_physical)) { 29033448Sdh155122 (void) fprintf(stderr, 29043448Sdh155122 gettext("WARNING: skipping network " 29053448Sdh155122 "interface '%s' which is used in the " 29063448Sdh155122 "global zone.\n"), 29073448Sdh155122 nwiftab.zone_nwif_physical); 29083448Sdh155122 break; 29093448Sdh155122 } 29104456Sss150715 29112611Svp157776 /* 29124456Sss150715 * Verify that the physical interface can be opened. 29133448Sdh155122 */ 29144456Sss150715 err = dlpi_open(nwiftab.zone_nwif_physical, &dh, 0); 29154456Sss150715 if (err != DLPI_SUCCESS) { 29163448Sdh155122 (void) fprintf(stderr, 29173448Sdh155122 gettext("WARNING: skipping network " 29184456Sss150715 "interface '%s' which cannot be opened: " 29194456Sss150715 "dlpi error (%s).\n"), 29204456Sss150715 nwiftab.zone_nwif_physical, 29214456Sss150715 dlpi_strerror(err)); 29223448Sdh155122 break; 29233448Sdh155122 } else { 29244456Sss150715 dlpi_close(dh); 29253448Sdh155122 } 29263448Sdh155122 /* 29273448Sdh155122 * Verify whether the physical interface is already 29283448Sdh155122 * used by a zone. 29293448Sdh155122 */ 29303448Sdh155122 dl_owner_zid = ALL_ZONES; 29313448Sdh155122 if (zone_check_datalink(&dl_owner_zid, 29323448Sdh155122 nwiftab.zone_nwif_physical) != 0) 29333448Sdh155122 break; 29343448Sdh155122 29353448Sdh155122 /* 29363448Sdh155122 * If the zone being verified is 29373448Sdh155122 * running and owns the interface 29383448Sdh155122 */ 29393448Sdh155122 target_zid = getzoneidbyname(target_zone); 29403448Sdh155122 if (target_zid == dl_owner_zid) 29413448Sdh155122 break; 29423448Sdh155122 29433448Sdh155122 /* Zone id match failed, use name to check */ 29443448Sdh155122 if (getzonenamebyid(dl_owner_zid, dl_owner_zname, 29453448Sdh155122 ZONENAME_MAX) < 0) { 29463448Sdh155122 /* No name, show ID instead */ 29473448Sdh155122 (void) snprintf(dl_owner_zname, ZONENAME_MAX, 29483448Sdh155122 "<%d>", dl_owner_zid); 29493448Sdh155122 } else if (strcmp(dl_owner_zname, target_zone) == 0) 29503448Sdh155122 break; 29513448Sdh155122 29523448Sdh155122 /* 29533448Sdh155122 * Note here we only report a warning that 29543448Sdh155122 * the interface is already in use by another 29553448Sdh155122 * running zone, and the verify process just 29563448Sdh155122 * goes on, if the interface is still in use 29573448Sdh155122 * when this zone really boots up, zoneadmd 29583448Sdh155122 * will find it. If the name of the zone which 29593448Sdh155122 * owns this interface cannot be determined, 29603448Sdh155122 * then it is not possible to determine if there 29613448Sdh155122 * is a conflict so just report it as a warning. 29622611Svp157776 */ 29632611Svp157776 (void) fprintf(stderr, 29643448Sdh155122 gettext("WARNING: skipping network interface " 29653448Sdh155122 "'%s' which is used by the non-global zone " 29663448Sdh155122 "'%s'.\n"), nwiftab.zone_nwif_physical, 29673448Sdh155122 dl_owner_zname); 29683448Sdh155122 break; 29690Sstevel@tonic-gate } 29700Sstevel@tonic-gate } 29710Sstevel@tonic-gate (void) zonecfg_endnwifent(handle); 2972766Scarlsonj no_net: 29730Sstevel@tonic-gate 29741931Sgjelinek /* verify that lofs has not been excluded from the kernel */ 29752078Sgjelinek if (!(cmd_num == CMD_DETACH || cmd_num == CMD_ATTACH || 29762078Sgjelinek cmd_num == CMD_MOVE || cmd_num == CMD_CLONE) && 29772078Sgjelinek modctl(MODLOAD, 1, "fs/lofs", NULL) != 0) { 29781931Sgjelinek if (errno == ENXIO) 29791931Sgjelinek (void) fprintf(stderr, gettext("could not verify " 29801931Sgjelinek "lofs(7FS): possibly excluded in /etc/system\n")); 29811931Sgjelinek else 29821931Sgjelinek (void) fprintf(stderr, gettext("could not verify " 29831931Sgjelinek "lofs(7FS): %s\n"), strerror(errno)); 29841931Sgjelinek return_code = Z_ERR; 29851931Sgjelinek } 29861931Sgjelinek 29870Sstevel@tonic-gate if (verify_filesystems(handle) != Z_OK) 29880Sstevel@tonic-gate return_code = Z_ERR; 2989823Sgjelinek if (verify_ipd(handle) != Z_OK) 2990823Sgjelinek return_code = Z_ERR; 2991766Scarlsonj if (!in_alt_root && verify_rctls(handle) != Z_OK) 29920Sstevel@tonic-gate return_code = Z_ERR; 2993766Scarlsonj if (!in_alt_root && verify_pool(handle) != Z_OK) 29940Sstevel@tonic-gate return_code = Z_ERR; 29953339Szt129084 if (!in_alt_root && verify_brand(handle, cmd_num, argv) != Z_OK) 29962712Snn35248 return_code = Z_ERR; 2997789Sahrens if (!in_alt_root && verify_datasets(handle) != Z_OK) 2998789Sahrens return_code = Z_ERR; 29991645Scomay 30001645Scomay /* 30011645Scomay * As the "mount" command is used for patching/upgrading of zones 30021645Scomay * or other maintenance processes, the zone's privilege set is not 30031645Scomay * checked in this case. Instead, the default, safe set of 30041645Scomay * privileges will be used when this zone is created in the 30051645Scomay * kernel. 30061645Scomay */ 30071645Scomay if (!in_alt_root && cmd_num != CMD_MOUNT && 30081645Scomay verify_limitpriv(handle) != Z_OK) 30091645Scomay return_code = Z_ERR; 30102078Sgjelinek 30112078Sgjelinek return (return_code); 30122078Sgjelinek } 30132078Sgjelinek 30142078Sgjelinek static int 30153339Szt129084 verify_details(int cmd_num, char *argv[]) 30162078Sgjelinek { 30172078Sgjelinek zone_dochandle_t handle; 30182078Sgjelinek char zonepath[MAXPATHLEN], checkpath[MAXPATHLEN]; 30192078Sgjelinek int return_code = Z_OK; 30202078Sgjelinek int err; 30212078Sgjelinek 30222078Sgjelinek if ((handle = zonecfg_init_handle()) == NULL) { 30232078Sgjelinek zperror(cmd_to_str(cmd_num), B_TRUE); 30242078Sgjelinek return (Z_ERR); 30252078Sgjelinek } 30262078Sgjelinek if ((err = zonecfg_get_handle(target_zone, handle)) != Z_OK) { 30272078Sgjelinek errno = err; 30282078Sgjelinek zperror(cmd_to_str(cmd_num), B_TRUE); 30292078Sgjelinek zonecfg_fini_handle(handle); 30302078Sgjelinek return (Z_ERR); 30312078Sgjelinek } 30322078Sgjelinek if ((err = zonecfg_get_zonepath(handle, zonepath, sizeof (zonepath))) != 30332078Sgjelinek Z_OK) { 30342078Sgjelinek errno = err; 30352078Sgjelinek zperror(cmd_to_str(cmd_num), B_TRUE); 30362078Sgjelinek zonecfg_fini_handle(handle); 30372078Sgjelinek return (Z_ERR); 30382078Sgjelinek } 30392078Sgjelinek /* 30402078Sgjelinek * zonecfg_get_zonepath() gets its data from the XML repository. 30412078Sgjelinek * Verify this against the index file, which is checked first by 30422078Sgjelinek * zone_get_zonepath(). If they don't match, bail out. 30432078Sgjelinek */ 30442078Sgjelinek if ((err = zone_get_zonepath(target_zone, checkpath, 30452078Sgjelinek sizeof (checkpath))) != Z_OK) { 30462078Sgjelinek errno = err; 30472078Sgjelinek zperror2(target_zone, gettext("could not get zone path")); 30483716Sgjelinek zonecfg_fini_handle(handle); 30492078Sgjelinek return (Z_ERR); 30502078Sgjelinek } 30512078Sgjelinek if (strcmp(zonepath, checkpath) != 0) { 30522078Sgjelinek /* 30532078Sgjelinek * TRANSLATION_NOTE 30542078Sgjelinek * XML and zonepath are literals that should not be translated. 30552078Sgjelinek */ 30562078Sgjelinek (void) fprintf(stderr, gettext("The XML repository has " 30572078Sgjelinek "zonepath '%s',\nbut the index file has zonepath '%s'.\n" 30582078Sgjelinek "These must match, so fix the incorrect entry.\n"), 30592078Sgjelinek zonepath, checkpath); 30603716Sgjelinek zonecfg_fini_handle(handle); 30612078Sgjelinek return (Z_ERR); 30622078Sgjelinek } 30632078Sgjelinek if (validate_zonepath(zonepath, cmd_num) != Z_OK) { 30642078Sgjelinek (void) fprintf(stderr, gettext("could not verify zonepath %s " 30652078Sgjelinek "because of the above errors.\n"), zonepath); 30662078Sgjelinek return_code = Z_ERR; 30672078Sgjelinek } 30682078Sgjelinek 30693339Szt129084 if (verify_handle(cmd_num, handle, argv) != Z_OK) 30702078Sgjelinek return_code = Z_ERR; 30712078Sgjelinek 30720Sstevel@tonic-gate zonecfg_fini_handle(handle); 30730Sstevel@tonic-gate if (return_code == Z_ERR) 30740Sstevel@tonic-gate (void) fprintf(stderr, 30750Sstevel@tonic-gate gettext("%s: zone %s failed to verify\n"), 30760Sstevel@tonic-gate execname, target_zone); 30770Sstevel@tonic-gate return (return_code); 30780Sstevel@tonic-gate } 30790Sstevel@tonic-gate 30800Sstevel@tonic-gate static int 30810Sstevel@tonic-gate verify_func(int argc, char *argv[]) 30820Sstevel@tonic-gate { 30830Sstevel@tonic-gate int arg; 30840Sstevel@tonic-gate 30850Sstevel@tonic-gate optind = 0; 30860Sstevel@tonic-gate if ((arg = getopt(argc, argv, "?")) != EOF) { 30870Sstevel@tonic-gate switch (arg) { 30880Sstevel@tonic-gate case '?': 30890Sstevel@tonic-gate sub_usage(SHELP_VERIFY, CMD_VERIFY); 30900Sstevel@tonic-gate return (optopt == '?' ? Z_OK : Z_USAGE); 30910Sstevel@tonic-gate default: 30920Sstevel@tonic-gate sub_usage(SHELP_VERIFY, CMD_VERIFY); 30930Sstevel@tonic-gate return (Z_USAGE); 30940Sstevel@tonic-gate } 30950Sstevel@tonic-gate } 30960Sstevel@tonic-gate if (argc > optind) { 30970Sstevel@tonic-gate sub_usage(SHELP_VERIFY, CMD_VERIFY); 30980Sstevel@tonic-gate return (Z_USAGE); 30990Sstevel@tonic-gate } 31002712Snn35248 if (sanity_check(target_zone, CMD_VERIFY, B_FALSE, B_FALSE, B_FALSE) 31012712Snn35248 != Z_OK) 31020Sstevel@tonic-gate return (Z_ERR); 31033339Szt129084 return (verify_details(CMD_VERIFY, argv)); 31040Sstevel@tonic-gate } 31050Sstevel@tonic-gate 31062712Snn35248 static int 31072712Snn35248 addopt(char *buf, int opt, char *optarg, size_t bufsize) 31082712Snn35248 { 31092712Snn35248 char optstring[4]; 31102712Snn35248 31112712Snn35248 if (opt > 0) 31122712Snn35248 (void) sprintf(optstring, " -%c", opt); 31132712Snn35248 else 31142712Snn35248 (void) strcpy(optstring, " "); 31152712Snn35248 31162712Snn35248 if ((strlcat(buf, optstring, bufsize) > bufsize)) 31172712Snn35248 return (Z_ERR); 31182712Snn35248 if ((optarg != NULL) && (strlcat(buf, optarg, bufsize) > bufsize)) 31192712Snn35248 return (Z_ERR); 31202712Snn35248 return (Z_OK); 31212712Snn35248 } 31220Sstevel@tonic-gate 31230Sstevel@tonic-gate static int 31240Sstevel@tonic-gate install_func(int argc, char *argv[]) 31250Sstevel@tonic-gate { 31262712Snn35248 char cmdbuf[MAXPATHLEN]; 3127*4586Sgjelinek char postcmdbuf[MAXPATHLEN]; 31280Sstevel@tonic-gate int lockfd; 31292712Snn35248 int arg, err, subproc_err; 31300Sstevel@tonic-gate char zonepath[MAXPATHLEN]; 31312727Sedp brand_handle_t bh = NULL; 31320Sstevel@tonic-gate int status; 31331867Sgjelinek boolean_t nodataset = B_FALSE; 3134*4586Sgjelinek boolean_t do_postinstall = B_FALSE; 31352712Snn35248 char opts[128]; 31362712Snn35248 31372712Snn35248 if (target_zone == NULL) { 31382712Snn35248 sub_usage(SHELP_INSTALL, CMD_INSTALL); 31392712Snn35248 return (Z_USAGE); 31402712Snn35248 } 31410Sstevel@tonic-gate 3142766Scarlsonj if (zonecfg_in_alt_root()) { 3143766Scarlsonj zerror(gettext("cannot install zone in alternate root")); 3144766Scarlsonj return (Z_ERR); 3145766Scarlsonj } 3146766Scarlsonj 31472712Snn35248 if ((err = zone_get_zonepath(target_zone, zonepath, 31482712Snn35248 sizeof (zonepath))) != Z_OK) { 31492712Snn35248 errno = err; 31502712Snn35248 zperror2(target_zone, gettext("could not get zone path")); 31512712Snn35248 return (Z_ERR); 31522712Snn35248 } 31532712Snn35248 31542712Snn35248 /* Fetch the install command from the brand configuration. */ 31552727Sedp if ((bh = brand_open(target_brand)) == NULL) { 31562712Snn35248 zerror(gettext("missing or invalid brand")); 31572712Snn35248 return (Z_ERR); 31582712Snn35248 } 31592712Snn35248 31602712Snn35248 (void) strcpy(cmdbuf, EXEC_PREFIX); 31612727Sedp if (brand_get_install(bh, target_zone, zonepath, cmdbuf + EXEC_LEN, 31622712Snn35248 sizeof (cmdbuf) - EXEC_LEN, 0, NULL) != 0) { 31632712Snn35248 zerror("invalid brand configuration: missing install resource"); 31642727Sedp brand_close(bh); 31652712Snn35248 return (Z_ERR); 31662712Snn35248 } 31672712Snn35248 3168*4586Sgjelinek (void) strcpy(postcmdbuf, EXEC_PREFIX); 3169*4586Sgjelinek if (brand_get_postinstall(bh, target_zone, zonepath, 3170*4586Sgjelinek postcmdbuf + EXEC_LEN, sizeof (postcmdbuf) - EXEC_LEN, 0, NULL) 3171*4586Sgjelinek != 0) { 3172*4586Sgjelinek zerror("invalid brand configuration: missing postinstall " 3173*4586Sgjelinek "resource"); 3174*4586Sgjelinek brand_close(bh); 3175*4586Sgjelinek return (Z_ERR); 3176*4586Sgjelinek } else if (strlen(postcmdbuf) > EXEC_LEN) { 3177*4586Sgjelinek do_postinstall = B_TRUE; 3178*4586Sgjelinek } 3179*4586Sgjelinek 31802712Snn35248 (void) strcpy(opts, "?x:"); 31812712Snn35248 if (!is_native_zone) { 31822712Snn35248 /* 31832712Snn35248 * Fetch the list of recognized command-line options from 31842712Snn35248 * the brand configuration file. 31852712Snn35248 */ 31862727Sedp if (brand_get_installopts(bh, opts + strlen(opts), 31872712Snn35248 sizeof (opts) - strlen(opts)) != 0) { 31882712Snn35248 zerror("invalid brand configuration: missing " 31892712Snn35248 "install options resource"); 31902727Sedp brand_close(bh); 31912712Snn35248 return (Z_ERR); 31922712Snn35248 } 31932712Snn35248 } 31942727Sedp brand_close(bh); 31952712Snn35248 31960Sstevel@tonic-gate optind = 0; 31972712Snn35248 while ((arg = getopt(argc, argv, opts)) != EOF) { 31980Sstevel@tonic-gate switch (arg) { 31990Sstevel@tonic-gate case '?': 32000Sstevel@tonic-gate sub_usage(SHELP_INSTALL, CMD_INSTALL); 32010Sstevel@tonic-gate return (optopt == '?' ? Z_OK : Z_USAGE); 32021867Sgjelinek case 'x': 32031867Sgjelinek if (strcmp(optarg, "nodataset") != 0) { 32041867Sgjelinek sub_usage(SHELP_INSTALL, CMD_INSTALL); 32051867Sgjelinek return (Z_USAGE); 32061867Sgjelinek } 32071867Sgjelinek nodataset = B_TRUE; 32081867Sgjelinek break; 32090Sstevel@tonic-gate default: 32102712Snn35248 if (is_native_zone) { 32112712Snn35248 sub_usage(SHELP_INSTALL, CMD_INSTALL); 32122712Snn35248 return (Z_USAGE); 32132712Snn35248 } 32142712Snn35248 32152712Snn35248 /* 32162712Snn35248 * This option isn't for zoneadm, so append it to 32172712Snn35248 * the command line passed to the brand-specific 3218*4586Sgjelinek * install and postinstall routines. 32192712Snn35248 */ 32202712Snn35248 if (addopt(cmdbuf, optopt, optarg, 32212712Snn35248 sizeof (cmdbuf)) != Z_OK) { 32222712Snn35248 zerror("Install command line too long"); 32232712Snn35248 return (Z_ERR); 32242712Snn35248 } 3225*4586Sgjelinek if (addopt(postcmdbuf, optopt, optarg, 3226*4586Sgjelinek sizeof (postcmdbuf)) != Z_OK) { 3227*4586Sgjelinek zerror("Post-Install command line too long"); 3228*4586Sgjelinek return (Z_ERR); 3229*4586Sgjelinek } 32302712Snn35248 break; 32310Sstevel@tonic-gate } 32320Sstevel@tonic-gate } 32332712Snn35248 32342712Snn35248 if (!is_native_zone) { 32352712Snn35248 for (; optind < argc; optind++) { 32362712Snn35248 if (addopt(cmdbuf, 0, argv[optind], 32372712Snn35248 sizeof (cmdbuf)) != Z_OK) { 32382712Snn35248 zerror("Install command line too long"); 32392712Snn35248 return (Z_ERR); 32402712Snn35248 } 3241*4586Sgjelinek if (addopt(postcmdbuf, 0, argv[optind], 3242*4586Sgjelinek sizeof (postcmdbuf)) != Z_OK) { 3243*4586Sgjelinek zerror("Post-Install command line too long"); 3244*4586Sgjelinek return (Z_ERR); 3245*4586Sgjelinek } 32462712Snn35248 } 32472712Snn35248 } 32482712Snn35248 32492712Snn35248 if (sanity_check(target_zone, CMD_INSTALL, B_FALSE, B_TRUE, B_FALSE) 32502712Snn35248 != Z_OK) 32510Sstevel@tonic-gate return (Z_ERR); 32523339Szt129084 if (verify_details(CMD_INSTALL, argv) != Z_OK) 32530Sstevel@tonic-gate return (Z_ERR); 32540Sstevel@tonic-gate 32550Sstevel@tonic-gate if (grab_lock_file(target_zone, &lockfd) != Z_OK) { 32560Sstevel@tonic-gate zerror(gettext("another %s may have an operation in progress."), 32571300Sgjelinek "zoneadm"); 32580Sstevel@tonic-gate return (Z_ERR); 32590Sstevel@tonic-gate } 32600Sstevel@tonic-gate err = zone_set_state(target_zone, ZONE_STATE_INCOMPLETE); 32610Sstevel@tonic-gate if (err != Z_OK) { 32620Sstevel@tonic-gate errno = err; 32630Sstevel@tonic-gate zperror2(target_zone, gettext("could not set state")); 32640Sstevel@tonic-gate goto done; 32650Sstevel@tonic-gate } 32660Sstevel@tonic-gate 32672712Snn35248 if (!nodataset) 32682712Snn35248 create_zfs_zonepath(zonepath); 32692712Snn35248 32700Sstevel@tonic-gate /* 32710Sstevel@tonic-gate * According to the Application Packaging Developer's Guide, a 32720Sstevel@tonic-gate * "checkinstall" script when included in a package is executed as 32730Sstevel@tonic-gate * the user "install", if such a user exists, or by the user 32740Sstevel@tonic-gate * "nobody". In order to support this dubious behavior, the path 32750Sstevel@tonic-gate * to the zone being constructed is opened up during the life of 32760Sstevel@tonic-gate * the command laying down the zone's root file system. Once this 32770Sstevel@tonic-gate * has completed, regardless of whether it was successful, the 32780Sstevel@tonic-gate * path to the zone is again restricted. 32790Sstevel@tonic-gate */ 32800Sstevel@tonic-gate if (chmod(zonepath, DEFAULT_DIR_MODE) != 0) { 32810Sstevel@tonic-gate zperror(zonepath, B_FALSE); 32820Sstevel@tonic-gate err = Z_ERR; 32830Sstevel@tonic-gate goto done; 32840Sstevel@tonic-gate } 32850Sstevel@tonic-gate 32862712Snn35248 if (is_native_zone) 32872712Snn35248 status = do_subproc(cmdbuf); 32882712Snn35248 else 32892712Snn35248 status = do_subproc_interactive(cmdbuf); 32902712Snn35248 32910Sstevel@tonic-gate if (chmod(zonepath, S_IRWXU) != 0) { 32920Sstevel@tonic-gate zperror(zonepath, B_FALSE); 32930Sstevel@tonic-gate err = Z_ERR; 32940Sstevel@tonic-gate goto done; 32950Sstevel@tonic-gate } 32962712Snn35248 if ((subproc_err = 32972712Snn35248 subproc_status(gettext("brand-specific installation"), status, 32982712Snn35248 B_FALSE)) != ZONE_SUBPROC_OK) { 32992712Snn35248 err = Z_ERR; 33000Sstevel@tonic-gate goto done; 33012712Snn35248 } 33020Sstevel@tonic-gate 33030Sstevel@tonic-gate if ((err = zone_set_state(target_zone, ZONE_STATE_INSTALLED)) != Z_OK) { 33040Sstevel@tonic-gate errno = err; 33050Sstevel@tonic-gate zperror2(target_zone, gettext("could not set state")); 33060Sstevel@tonic-gate goto done; 33070Sstevel@tonic-gate } 33080Sstevel@tonic-gate 3309*4586Sgjelinek if (do_postinstall) { 3310*4586Sgjelinek status = do_subproc(postcmdbuf); 3311*4586Sgjelinek 3312*4586Sgjelinek if ((subproc_err = 3313*4586Sgjelinek subproc_status(gettext("brand-specific post-install"), 3314*4586Sgjelinek status, B_FALSE)) != ZONE_SUBPROC_OK) { 3315*4586Sgjelinek err = Z_ERR; 3316*4586Sgjelinek (void) zone_set_state(target_zone, 3317*4586Sgjelinek ZONE_STATE_INCOMPLETE); 3318*4586Sgjelinek } 3319*4586Sgjelinek } 3320*4586Sgjelinek 33210Sstevel@tonic-gate done: 33222712Snn35248 /* 33232712Snn35248 * If the install script exited with ZONE_SUBPROC_USAGE or 33242712Snn35248 * ZONE_SUBPROC_NOTCOMPLETE, try to clean up the zone and leave the 33252712Snn35248 * zone in the CONFIGURED state so that another install can be 33262712Snn35248 * attempted without requiring an uninstall first. 33272712Snn35248 */ 33282712Snn35248 if ((subproc_err == ZONE_SUBPROC_USAGE) || 33292712Snn35248 (subproc_err == ZONE_SUBPROC_NOTCOMPLETE)) { 33302712Snn35248 if ((err = cleanup_zonepath(zonepath, B_FALSE)) != Z_OK) { 33312712Snn35248 errno = err; 33322712Snn35248 zperror2(target_zone, 33332712Snn35248 gettext("cleaning up zonepath failed")); 33342712Snn35248 } else if ((err = zone_set_state(target_zone, 33352712Snn35248 ZONE_STATE_CONFIGURED)) != Z_OK) { 33362712Snn35248 errno = err; 33372712Snn35248 zperror2(target_zone, gettext("could not set state")); 33382712Snn35248 } 33392712Snn35248 } 33402712Snn35248 33410Sstevel@tonic-gate release_lock_file(lockfd); 33420Sstevel@tonic-gate return ((err == Z_OK) ? Z_OK : Z_ERR); 33430Sstevel@tonic-gate } 33440Sstevel@tonic-gate 33450Sstevel@tonic-gate /* 33461300Sgjelinek * Check that the inherited pkg dirs are the same for the clone and its source. 33471300Sgjelinek * The easiest way to do that is check that the list of ipds is the same 33481300Sgjelinek * by matching each one against the other. This algorithm should be fine since 33491300Sgjelinek * the list of ipds should not be that long. 33501300Sgjelinek */ 33511300Sgjelinek static int 33521300Sgjelinek valid_ipd_clone(zone_dochandle_t s_handle, char *source_zone, 33531300Sgjelinek zone_dochandle_t t_handle, char *target_zone) 33541300Sgjelinek { 33551300Sgjelinek int err; 33561300Sgjelinek int res = Z_OK; 33571300Sgjelinek int s_cnt = 0; 33581300Sgjelinek int t_cnt = 0; 33591300Sgjelinek struct zone_fstab s_fstab; 33601300Sgjelinek struct zone_fstab t_fstab; 33611300Sgjelinek 33621300Sgjelinek /* 33631300Sgjelinek * First check the source of the clone against the target. 33641300Sgjelinek */ 33651300Sgjelinek if ((err = zonecfg_setipdent(s_handle)) != Z_OK) { 33661300Sgjelinek errno = err; 33671300Sgjelinek zperror2(source_zone, gettext("could not enumerate " 33681300Sgjelinek "inherit-pkg-dirs")); 33691300Sgjelinek return (Z_ERR); 33701300Sgjelinek } 33711300Sgjelinek 33721300Sgjelinek while (zonecfg_getipdent(s_handle, &s_fstab) == Z_OK) { 33731300Sgjelinek boolean_t match = B_FALSE; 33741300Sgjelinek 33751300Sgjelinek s_cnt++; 33761300Sgjelinek 33771300Sgjelinek if ((err = zonecfg_setipdent(t_handle)) != Z_OK) { 33781300Sgjelinek errno = err; 33791300Sgjelinek zperror2(target_zone, gettext("could not enumerate " 33801300Sgjelinek "inherit-pkg-dirs")); 33811300Sgjelinek (void) zonecfg_endipdent(s_handle); 33821300Sgjelinek return (Z_ERR); 33831300Sgjelinek } 33841300Sgjelinek 33851300Sgjelinek while (zonecfg_getipdent(t_handle, &t_fstab) == Z_OK) { 33861300Sgjelinek if (strcmp(s_fstab.zone_fs_dir, t_fstab.zone_fs_dir) 33871300Sgjelinek == 0) { 33881300Sgjelinek match = B_TRUE; 33891300Sgjelinek break; 33901300Sgjelinek } 33911300Sgjelinek } 33921300Sgjelinek (void) zonecfg_endipdent(t_handle); 33931300Sgjelinek 33941300Sgjelinek if (!match) { 33951300Sgjelinek (void) fprintf(stderr, gettext("inherit-pkg-dir " 33961300Sgjelinek "'%s' is not configured in zone %s.\n"), 33971300Sgjelinek s_fstab.zone_fs_dir, target_zone); 33981300Sgjelinek res = Z_ERR; 33991300Sgjelinek } 34001300Sgjelinek } 34011300Sgjelinek 34021300Sgjelinek (void) zonecfg_endipdent(s_handle); 34031300Sgjelinek 34041300Sgjelinek /* skip the next check if we already have errors */ 34051300Sgjelinek if (res == Z_ERR) 34061300Sgjelinek return (res); 34071300Sgjelinek 34081300Sgjelinek /* 34091300Sgjelinek * Now check the number of ipds in the target so we can verify 34101300Sgjelinek * that the source is not a subset of the target. 34111300Sgjelinek */ 34121300Sgjelinek if ((err = zonecfg_setipdent(t_handle)) != Z_OK) { 34131300Sgjelinek errno = err; 34141300Sgjelinek zperror2(target_zone, gettext("could not enumerate " 34151300Sgjelinek "inherit-pkg-dirs")); 34161300Sgjelinek return (Z_ERR); 34171300Sgjelinek } 34181300Sgjelinek 34191300Sgjelinek while (zonecfg_getipdent(t_handle, &t_fstab) == Z_OK) 34201300Sgjelinek t_cnt++; 34211300Sgjelinek 34221300Sgjelinek (void) zonecfg_endipdent(t_handle); 34231300Sgjelinek 34241300Sgjelinek if (t_cnt != s_cnt) { 34251300Sgjelinek (void) fprintf(stderr, gettext("Zone %s is configured " 34261300Sgjelinek "with inherit-pkg-dirs that are not configured in zone " 34271300Sgjelinek "%s.\n"), target_zone, source_zone); 34281300Sgjelinek res = Z_ERR; 34291300Sgjelinek } 34301300Sgjelinek 34311300Sgjelinek return (res); 34321300Sgjelinek } 34331300Sgjelinek 34341300Sgjelinek static void 34351300Sgjelinek warn_dev_match(zone_dochandle_t s_handle, char *source_zone, 34361300Sgjelinek zone_dochandle_t t_handle, char *target_zone) 34371300Sgjelinek { 34381300Sgjelinek int err; 34391300Sgjelinek struct zone_devtab s_devtab; 34401300Sgjelinek struct zone_devtab t_devtab; 34411300Sgjelinek 34421300Sgjelinek if ((err = zonecfg_setdevent(t_handle)) != Z_OK) { 34431300Sgjelinek errno = err; 34441300Sgjelinek zperror2(target_zone, gettext("could not enumerate devices")); 34451300Sgjelinek return; 34461300Sgjelinek } 34471300Sgjelinek 34481300Sgjelinek while (zonecfg_getdevent(t_handle, &t_devtab) == Z_OK) { 34491300Sgjelinek if ((err = zonecfg_setdevent(s_handle)) != Z_OK) { 34501300Sgjelinek errno = err; 34511300Sgjelinek zperror2(source_zone, 34521300Sgjelinek gettext("could not enumerate devices")); 34531300Sgjelinek (void) zonecfg_enddevent(t_handle); 34541300Sgjelinek return; 34551300Sgjelinek } 34561300Sgjelinek 34571300Sgjelinek while (zonecfg_getdevent(s_handle, &s_devtab) == Z_OK) { 34581300Sgjelinek /* 34591300Sgjelinek * Use fnmatch to catch the case where wildcards 34601300Sgjelinek * were used in one zone and the other has an 34611300Sgjelinek * explicit entry (e.g. /dev/dsk/c0t0d0s6 vs. 34621300Sgjelinek * /dev/\*dsk/c0t0d0s6). 34631300Sgjelinek */ 34641300Sgjelinek if (fnmatch(t_devtab.zone_dev_match, 34651300Sgjelinek s_devtab.zone_dev_match, FNM_PATHNAME) == 0 || 34661300Sgjelinek fnmatch(s_devtab.zone_dev_match, 34671300Sgjelinek t_devtab.zone_dev_match, FNM_PATHNAME) == 0) { 34681300Sgjelinek (void) fprintf(stderr, 34691300Sgjelinek gettext("WARNING: device '%s' " 34701300Sgjelinek "is configured in both zones.\n"), 34711300Sgjelinek t_devtab.zone_dev_match); 34721300Sgjelinek break; 34731300Sgjelinek } 34741300Sgjelinek } 34751300Sgjelinek (void) zonecfg_enddevent(s_handle); 34761300Sgjelinek } 34771300Sgjelinek 34781300Sgjelinek (void) zonecfg_enddevent(t_handle); 34791300Sgjelinek } 34801300Sgjelinek 34811300Sgjelinek /* 34821300Sgjelinek * Check if the specified mount option (opt) is contained within the 34831300Sgjelinek * options string. 34841300Sgjelinek */ 34851300Sgjelinek static boolean_t 34861300Sgjelinek opt_match(char *opt, char *options) 34871300Sgjelinek { 34881300Sgjelinek char *p; 34891300Sgjelinek char *lastp; 34901300Sgjelinek 34911300Sgjelinek if ((p = strtok_r(options, ",", &lastp)) != NULL) { 34921300Sgjelinek if (strcmp(p, opt) == 0) 34931300Sgjelinek return (B_TRUE); 34941300Sgjelinek while ((p = strtok_r(NULL, ",", &lastp)) != NULL) { 34951300Sgjelinek if (strcmp(p, opt) == 0) 34961300Sgjelinek return (B_TRUE); 34971300Sgjelinek } 34981300Sgjelinek } 34991300Sgjelinek 35001300Sgjelinek return (B_FALSE); 35011300Sgjelinek } 35021300Sgjelinek 35031867Sgjelinek #define RW_LOFS "WARNING: read-write lofs file system on '%s' is configured " \ 35041300Sgjelinek "in both zones.\n" 35051300Sgjelinek 35061300Sgjelinek static void 35071300Sgjelinek print_fs_warnings(struct zone_fstab *s_fstab, struct zone_fstab *t_fstab) 35081300Sgjelinek { 35091300Sgjelinek /* 35101300Sgjelinek * It is ok to have shared lofs mounted fs but we want to warn if 35111300Sgjelinek * either is rw since this will effect the other zone. 35121300Sgjelinek */ 35131300Sgjelinek if (strcmp(t_fstab->zone_fs_type, "lofs") == 0) { 35141300Sgjelinek zone_fsopt_t *optp; 35151300Sgjelinek 35161300Sgjelinek /* The default is rw so no options means rw */ 35171300Sgjelinek if (t_fstab->zone_fs_options == NULL || 35181300Sgjelinek s_fstab->zone_fs_options == NULL) { 35191300Sgjelinek (void) fprintf(stderr, gettext(RW_LOFS), 35201300Sgjelinek t_fstab->zone_fs_special); 35211300Sgjelinek return; 35221300Sgjelinek } 35231300Sgjelinek 35241300Sgjelinek for (optp = s_fstab->zone_fs_options; optp != NULL; 35251300Sgjelinek optp = optp->zone_fsopt_next) { 35261300Sgjelinek if (opt_match("rw", optp->zone_fsopt_opt)) { 35271300Sgjelinek (void) fprintf(stderr, gettext(RW_LOFS), 35281300Sgjelinek s_fstab->zone_fs_special); 35291300Sgjelinek return; 35301300Sgjelinek } 35311300Sgjelinek } 35321300Sgjelinek 35331300Sgjelinek for (optp = t_fstab->zone_fs_options; optp != NULL; 35341300Sgjelinek optp = optp->zone_fsopt_next) { 35351300Sgjelinek if (opt_match("rw", optp->zone_fsopt_opt)) { 35361300Sgjelinek (void) fprintf(stderr, gettext(RW_LOFS), 35371300Sgjelinek t_fstab->zone_fs_special); 35381300Sgjelinek return; 35391300Sgjelinek } 35401300Sgjelinek } 35411300Sgjelinek 35421300Sgjelinek return; 35431300Sgjelinek } 35441300Sgjelinek 35451300Sgjelinek /* 35461300Sgjelinek * TRANSLATION_NOTE 35471867Sgjelinek * The first variable is the file system type and the second is 35481867Sgjelinek * the file system special device. For example, 35491867Sgjelinek * WARNING: ufs file system on '/dev/dsk/c0t0d0s0' ... 35501300Sgjelinek */ 35511867Sgjelinek (void) fprintf(stderr, gettext("WARNING: %s file system on '%s' " 35521300Sgjelinek "is configured in both zones.\n"), t_fstab->zone_fs_type, 35531300Sgjelinek t_fstab->zone_fs_special); 35541300Sgjelinek } 35551300Sgjelinek 35561300Sgjelinek static void 35571300Sgjelinek warn_fs_match(zone_dochandle_t s_handle, char *source_zone, 35581300Sgjelinek zone_dochandle_t t_handle, char *target_zone) 35591300Sgjelinek { 35601300Sgjelinek int err; 35611300Sgjelinek struct zone_fstab s_fstab; 35621300Sgjelinek struct zone_fstab t_fstab; 35631300Sgjelinek 35641300Sgjelinek if ((err = zonecfg_setfsent(t_handle)) != Z_OK) { 35651300Sgjelinek errno = err; 35661300Sgjelinek zperror2(target_zone, 35671867Sgjelinek gettext("could not enumerate file systems")); 35681300Sgjelinek return; 35691300Sgjelinek } 35701300Sgjelinek 35711300Sgjelinek while (zonecfg_getfsent(t_handle, &t_fstab) == Z_OK) { 35721300Sgjelinek if ((err = zonecfg_setfsent(s_handle)) != Z_OK) { 35731300Sgjelinek errno = err; 35741300Sgjelinek zperror2(source_zone, 35751867Sgjelinek gettext("could not enumerate file systems")); 35761300Sgjelinek (void) zonecfg_endfsent(t_handle); 35771300Sgjelinek return; 35781300Sgjelinek } 35791300Sgjelinek 35801300Sgjelinek while (zonecfg_getfsent(s_handle, &s_fstab) == Z_OK) { 35811300Sgjelinek if (strcmp(t_fstab.zone_fs_special, 35821300Sgjelinek s_fstab.zone_fs_special) == 0) { 35831300Sgjelinek print_fs_warnings(&s_fstab, &t_fstab); 35841300Sgjelinek break; 35851300Sgjelinek } 35861300Sgjelinek } 35871300Sgjelinek (void) zonecfg_endfsent(s_handle); 35881300Sgjelinek } 35891300Sgjelinek 35901300Sgjelinek (void) zonecfg_endfsent(t_handle); 35911300Sgjelinek } 35921300Sgjelinek 35931300Sgjelinek /* 35941300Sgjelinek * We don't catch the case where you used the same IP address but 35951300Sgjelinek * it is not an exact string match. For example, 192.9.0.128 vs. 192.09.0.128. 35961300Sgjelinek * However, we're not going to worry about that but we will check for 35971300Sgjelinek * a possible netmask on one of the addresses (e.g. 10.0.0.1 and 10.0.0.1/24) 35981300Sgjelinek * and handle that case as a match. 35991300Sgjelinek */ 36001300Sgjelinek static void 36011300Sgjelinek warn_ip_match(zone_dochandle_t s_handle, char *source_zone, 36021300Sgjelinek zone_dochandle_t t_handle, char *target_zone) 36031300Sgjelinek { 36041300Sgjelinek int err; 36051300Sgjelinek struct zone_nwiftab s_nwiftab; 36061300Sgjelinek struct zone_nwiftab t_nwiftab; 36071300Sgjelinek 36081300Sgjelinek if ((err = zonecfg_setnwifent(t_handle)) != Z_OK) { 36091300Sgjelinek errno = err; 36101300Sgjelinek zperror2(target_zone, 36111300Sgjelinek gettext("could not enumerate network interfaces")); 36121300Sgjelinek return; 36131300Sgjelinek } 36141300Sgjelinek 36151300Sgjelinek while (zonecfg_getnwifent(t_handle, &t_nwiftab) == Z_OK) { 36161300Sgjelinek char *p; 36171300Sgjelinek 36181300Sgjelinek /* remove an (optional) netmask from the address */ 36191300Sgjelinek if ((p = strchr(t_nwiftab.zone_nwif_address, '/')) != NULL) 36201300Sgjelinek *p = '\0'; 36211300Sgjelinek 36221300Sgjelinek if ((err = zonecfg_setnwifent(s_handle)) != Z_OK) { 36231300Sgjelinek errno = err; 36241300Sgjelinek zperror2(source_zone, 36251300Sgjelinek gettext("could not enumerate network interfaces")); 36261300Sgjelinek (void) zonecfg_endnwifent(t_handle); 36271300Sgjelinek return; 36281300Sgjelinek } 36291300Sgjelinek 36301300Sgjelinek while (zonecfg_getnwifent(s_handle, &s_nwiftab) == Z_OK) { 36311300Sgjelinek /* remove an (optional) netmask from the address */ 36321300Sgjelinek if ((p = strchr(s_nwiftab.zone_nwif_address, '/')) 36331300Sgjelinek != NULL) 36341300Sgjelinek *p = '\0'; 36351300Sgjelinek 36363448Sdh155122 /* For exclusive-IP zones, address is not specified. */ 36373448Sdh155122 if (strlen(s_nwiftab.zone_nwif_address) == 0) 36383448Sdh155122 continue; 36393448Sdh155122 36401300Sgjelinek if (strcmp(t_nwiftab.zone_nwif_address, 36411300Sgjelinek s_nwiftab.zone_nwif_address) == 0) { 36421300Sgjelinek (void) fprintf(stderr, 36431300Sgjelinek gettext("WARNING: network address '%s' " 36441300Sgjelinek "is configured in both zones.\n"), 36451300Sgjelinek t_nwiftab.zone_nwif_address); 36461300Sgjelinek break; 36471300Sgjelinek } 36481300Sgjelinek } 36491300Sgjelinek (void) zonecfg_endnwifent(s_handle); 36501300Sgjelinek } 36511300Sgjelinek 36521300Sgjelinek (void) zonecfg_endnwifent(t_handle); 36531300Sgjelinek } 36541300Sgjelinek 36551300Sgjelinek static void 36562712Snn35248 warn_dataset_match(zone_dochandle_t s_handle, char *source, 36572712Snn35248 zone_dochandle_t t_handle, char *target) 36581300Sgjelinek { 36591300Sgjelinek int err; 36601300Sgjelinek struct zone_dstab s_dstab; 36611300Sgjelinek struct zone_dstab t_dstab; 36621300Sgjelinek 36631300Sgjelinek if ((err = zonecfg_setdsent(t_handle)) != Z_OK) { 36641300Sgjelinek errno = err; 36652712Snn35248 zperror2(target, gettext("could not enumerate datasets")); 36661300Sgjelinek return; 36671300Sgjelinek } 36681300Sgjelinek 36691300Sgjelinek while (zonecfg_getdsent(t_handle, &t_dstab) == Z_OK) { 36701300Sgjelinek if ((err = zonecfg_setdsent(s_handle)) != Z_OK) { 36711300Sgjelinek errno = err; 36722712Snn35248 zperror2(source, 36731300Sgjelinek gettext("could not enumerate datasets")); 36741300Sgjelinek (void) zonecfg_enddsent(t_handle); 36751300Sgjelinek return; 36761300Sgjelinek } 36771300Sgjelinek 36781300Sgjelinek while (zonecfg_getdsent(s_handle, &s_dstab) == Z_OK) { 36791300Sgjelinek if (strcmp(t_dstab.zone_dataset_name, 36801300Sgjelinek s_dstab.zone_dataset_name) == 0) { 36812712Snn35248 target_zone = source; 36822712Snn35248 zerror(gettext("WARNING: dataset '%s' " 36831300Sgjelinek "is configured in both zones.\n"), 36841300Sgjelinek t_dstab.zone_dataset_name); 36851300Sgjelinek break; 36861300Sgjelinek } 36871300Sgjelinek } 36881300Sgjelinek (void) zonecfg_enddsent(s_handle); 36891300Sgjelinek } 36901300Sgjelinek 36911300Sgjelinek (void) zonecfg_enddsent(t_handle); 36921300Sgjelinek } 36931300Sgjelinek 36942712Snn35248 /* 36952712Snn35248 * Check that the clone and its source have the same brand type. 36962712Snn35248 */ 36972712Snn35248 static int 36982712Snn35248 valid_brand_clone(char *source_zone, char *target_zone) 36992712Snn35248 { 37002727Sedp brand_handle_t bh; 37012712Snn35248 char source_brand[MAXNAMELEN]; 37022712Snn35248 37032712Snn35248 if ((zone_get_brand(source_zone, source_brand, 37042712Snn35248 sizeof (source_brand))) != Z_OK) { 37052712Snn35248 (void) fprintf(stderr, "%s: zone '%s': %s\n", 37062712Snn35248 execname, source_zone, gettext("missing or invalid brand")); 37072712Snn35248 return (Z_ERR); 37082712Snn35248 } 37092712Snn35248 37102712Snn35248 if (strcmp(source_brand, target_brand) != NULL) { 37112712Snn35248 (void) fprintf(stderr, 37122712Snn35248 gettext("%s: Zones '%s' and '%s' have different brand " 37132712Snn35248 "types.\n"), execname, source_zone, target_zone); 37142712Snn35248 return (Z_ERR); 37152712Snn35248 } 37162712Snn35248 37172727Sedp if ((bh = brand_open(target_brand)) == NULL) { 37182712Snn35248 zerror(gettext("missing or invalid brand")); 37192712Snn35248 return (Z_ERR); 37202712Snn35248 } 37212727Sedp brand_close(bh); 37222712Snn35248 return (Z_OK); 37232712Snn35248 } 37242712Snn35248 37251300Sgjelinek static int 37261300Sgjelinek validate_clone(char *source_zone, char *target_zone) 37271300Sgjelinek { 37281300Sgjelinek int err = Z_OK; 37291300Sgjelinek zone_dochandle_t s_handle; 37301300Sgjelinek zone_dochandle_t t_handle; 37311300Sgjelinek 37321300Sgjelinek if ((t_handle = zonecfg_init_handle()) == NULL) { 37331300Sgjelinek zperror(cmd_to_str(CMD_CLONE), B_TRUE); 37341300Sgjelinek return (Z_ERR); 37351300Sgjelinek } 37361300Sgjelinek if ((err = zonecfg_get_handle(target_zone, t_handle)) != Z_OK) { 37371300Sgjelinek errno = err; 37381300Sgjelinek zperror(cmd_to_str(CMD_CLONE), B_TRUE); 37391300Sgjelinek zonecfg_fini_handle(t_handle); 37401300Sgjelinek return (Z_ERR); 37411300Sgjelinek } 37421300Sgjelinek 37431300Sgjelinek if ((s_handle = zonecfg_init_handle()) == NULL) { 37441300Sgjelinek zperror(cmd_to_str(CMD_CLONE), B_TRUE); 37451300Sgjelinek zonecfg_fini_handle(t_handle); 37461300Sgjelinek return (Z_ERR); 37471300Sgjelinek } 37481300Sgjelinek if ((err = zonecfg_get_handle(source_zone, s_handle)) != Z_OK) { 37491300Sgjelinek errno = err; 37501300Sgjelinek zperror(cmd_to_str(CMD_CLONE), B_TRUE); 37511300Sgjelinek goto done; 37521300Sgjelinek } 37531300Sgjelinek 37542712Snn35248 /* verify new zone has same brand type */ 37552712Snn35248 err = valid_brand_clone(source_zone, target_zone); 37562712Snn35248 if (err != Z_OK) 37572712Snn35248 goto done; 37582712Snn35248 37591300Sgjelinek /* verify new zone has same inherit-pkg-dirs */ 37601300Sgjelinek err = valid_ipd_clone(s_handle, source_zone, t_handle, target_zone); 37611300Sgjelinek 37621300Sgjelinek /* warn about imported fs's which are the same */ 37631300Sgjelinek warn_fs_match(s_handle, source_zone, t_handle, target_zone); 37641300Sgjelinek 37651300Sgjelinek /* warn about imported IP addresses which are the same */ 37661300Sgjelinek warn_ip_match(s_handle, source_zone, t_handle, target_zone); 37671300Sgjelinek 37681300Sgjelinek /* warn about imported devices which are the same */ 37691300Sgjelinek warn_dev_match(s_handle, source_zone, t_handle, target_zone); 37701300Sgjelinek 37711300Sgjelinek /* warn about imported datasets which are the same */ 37721300Sgjelinek warn_dataset_match(s_handle, source_zone, t_handle, target_zone); 37731300Sgjelinek 37741300Sgjelinek done: 37751300Sgjelinek zonecfg_fini_handle(t_handle); 37761300Sgjelinek zonecfg_fini_handle(s_handle); 37771300Sgjelinek 37781300Sgjelinek return ((err == Z_OK) ? Z_OK : Z_ERR); 37791300Sgjelinek } 37801300Sgjelinek 37811300Sgjelinek static int 37821300Sgjelinek copy_zone(char *src, char *dst) 37831300Sgjelinek { 37841300Sgjelinek boolean_t out_null = B_FALSE; 37851300Sgjelinek int status; 37861300Sgjelinek char *outfile; 37871300Sgjelinek char cmdbuf[MAXPATHLEN * 2 + 128]; 37881300Sgjelinek 37891300Sgjelinek if ((outfile = tempnam("/var/log", "zone")) == NULL) { 37901300Sgjelinek outfile = "/dev/null"; 37911300Sgjelinek out_null = B_TRUE; 37921300Sgjelinek } 37931300Sgjelinek 37941867Sgjelinek /* 37951867Sgjelinek * Use find to get the list of files to copy. We need to skip 37961867Sgjelinek * files of type "socket" since cpio can't handle those but that 37971867Sgjelinek * should be ok since the app will recreate the socket when it runs. 37981867Sgjelinek * We also need to filter out anything under the .zfs subdir. Since 37991867Sgjelinek * find is running depth-first, we need the extra egrep to filter .zfs. 38001867Sgjelinek */ 38011300Sgjelinek (void) snprintf(cmdbuf, sizeof (cmdbuf), 38021867Sgjelinek "cd %s && /usr/bin/find . -type s -prune -o -depth -print | " 38031607Sgjelinek "/usr/bin/egrep -v '^\\./\\.zfs$|^\\./\\.zfs/' | " 38041300Sgjelinek "/usr/bin/cpio -pdmuP@ %s > %s 2>&1", 38051300Sgjelinek src, dst, outfile); 38061300Sgjelinek 38071300Sgjelinek status = do_subproc(cmdbuf); 38081300Sgjelinek 38092712Snn35248 if (subproc_status("copy", status, B_TRUE) != ZONE_SUBPROC_OK) { 38101300Sgjelinek if (!out_null) 38111300Sgjelinek (void) fprintf(stderr, gettext("\nThe copy failed.\n" 38121300Sgjelinek "More information can be found in %s\n"), outfile); 38132712Snn35248 return (Z_ERR); 38141300Sgjelinek } 38151300Sgjelinek 38161300Sgjelinek if (!out_null) 38171300Sgjelinek (void) unlink(outfile); 38181300Sgjelinek 38191300Sgjelinek return (Z_OK); 38201300Sgjelinek } 38211300Sgjelinek 38221300Sgjelinek static int 38232712Snn35248 zone_postclone(char *zonepath) 38241300Sgjelinek { 38252712Snn35248 char cmdbuf[MAXPATHLEN]; 38262712Snn35248 int status; 38272727Sedp brand_handle_t bh; 38282712Snn35248 int err = Z_OK; 38291676Sjpk 38301676Sjpk /* 38312712Snn35248 * Fetch the post-clone command, if any, from the brand 38322712Snn35248 * configuration. 38331568Sgjelinek */ 38342727Sedp if ((bh = brand_open(target_brand)) == NULL) { 38352712Snn35248 zerror(gettext("missing or invalid brand")); 38361568Sgjelinek return (Z_ERR); 38371568Sgjelinek } 38382712Snn35248 (void) strcpy(cmdbuf, EXEC_PREFIX); 38392727Sedp err = brand_get_postclone(bh, target_zone, zonepath, cmdbuf + EXEC_LEN, 38402712Snn35248 sizeof (cmdbuf) - EXEC_LEN, 0, NULL); 38412727Sedp brand_close(bh); 38422712Snn35248 38432712Snn35248 if (err == 0 && strlen(cmdbuf) > EXEC_LEN) { 38442712Snn35248 status = do_subproc(cmdbuf); 38452712Snn35248 if ((err = subproc_status("postclone", status, B_FALSE)) 38462712Snn35248 != ZONE_SUBPROC_OK) { 38472712Snn35248 zerror(gettext("post-clone configuration failed.")); 38482712Snn35248 err = Z_ERR; 38492712Snn35248 } 38502712Snn35248 } 38512712Snn35248 38522712Snn35248 return (err); 38531300Sgjelinek } 38541300Sgjelinek 38551300Sgjelinek /* ARGSUSED */ 38561867Sgjelinek static int 38571300Sgjelinek zfm_print(const char *p, void *r) { 38581300Sgjelinek zerror(" %s\n", p); 38591300Sgjelinek return (0); 38601300Sgjelinek } 38611300Sgjelinek 38621867Sgjelinek int 38631867Sgjelinek clone_copy(char *source_zonepath, char *zonepath) 38641867Sgjelinek { 38651867Sgjelinek int err; 38661867Sgjelinek 38671867Sgjelinek /* Don't clone the zone if anything is still mounted there */ 38681867Sgjelinek if (zonecfg_find_mounts(source_zonepath, NULL, NULL)) { 38691867Sgjelinek zerror(gettext("These file systems are mounted on " 38701867Sgjelinek "subdirectories of %s.\n"), source_zonepath); 38711867Sgjelinek (void) zonecfg_find_mounts(source_zonepath, zfm_print, NULL); 38721867Sgjelinek return (Z_ERR); 38731867Sgjelinek } 38741867Sgjelinek 38751867Sgjelinek /* 38761867Sgjelinek * Attempt to create a ZFS fs for the zonepath. As usual, we don't 38771867Sgjelinek * care if this works or not since we always have the default behavior 38781867Sgjelinek * of a simple directory for the zonepath. 38791867Sgjelinek */ 38801867Sgjelinek create_zfs_zonepath(zonepath); 38811867Sgjelinek 38821867Sgjelinek (void) printf(gettext("Copying %s..."), source_zonepath); 38831867Sgjelinek (void) fflush(stdout); 38841867Sgjelinek 38851867Sgjelinek err = copy_zone(source_zonepath, zonepath); 38861867Sgjelinek 38871867Sgjelinek (void) printf("\n"); 38881867Sgjelinek 38891867Sgjelinek return (err); 38901867Sgjelinek } 38911867Sgjelinek 38921300Sgjelinek static int 38931300Sgjelinek clone_func(int argc, char *argv[]) 38941300Sgjelinek { 38951300Sgjelinek char *source_zone = NULL; 38961300Sgjelinek int lockfd; 38971300Sgjelinek int err, arg; 38981300Sgjelinek char zonepath[MAXPATHLEN]; 38991300Sgjelinek char source_zonepath[MAXPATHLEN]; 39001300Sgjelinek zone_state_t state; 39011300Sgjelinek zone_entry_t *zent; 39021867Sgjelinek char *method = NULL; 39031867Sgjelinek char *snapshot = NULL; 39041300Sgjelinek 39051300Sgjelinek if (zonecfg_in_alt_root()) { 39061300Sgjelinek zerror(gettext("cannot clone zone in alternate root")); 39071300Sgjelinek return (Z_ERR); 39081300Sgjelinek } 39091300Sgjelinek 39101300Sgjelinek optind = 0; 39111867Sgjelinek if ((arg = getopt(argc, argv, "?m:s:")) != EOF) { 39121300Sgjelinek switch (arg) { 39131300Sgjelinek case '?': 39141300Sgjelinek sub_usage(SHELP_CLONE, CMD_CLONE); 39151300Sgjelinek return (optopt == '?' ? Z_OK : Z_USAGE); 39161300Sgjelinek case 'm': 39171300Sgjelinek method = optarg; 39181300Sgjelinek break; 39191867Sgjelinek case 's': 39201867Sgjelinek snapshot = optarg; 39211867Sgjelinek break; 39221300Sgjelinek default: 39231300Sgjelinek sub_usage(SHELP_CLONE, CMD_CLONE); 39241300Sgjelinek return (Z_USAGE); 39251300Sgjelinek } 39261300Sgjelinek } 39271867Sgjelinek if (argc != (optind + 1) || 39281867Sgjelinek (method != NULL && strcmp(method, "copy") != 0)) { 39291300Sgjelinek sub_usage(SHELP_CLONE, CMD_CLONE); 39301300Sgjelinek return (Z_USAGE); 39311300Sgjelinek } 39321300Sgjelinek source_zone = argv[optind]; 39332712Snn35248 if (sanity_check(target_zone, CMD_CLONE, B_FALSE, B_TRUE, B_FALSE) 39342712Snn35248 != Z_OK) 39351300Sgjelinek return (Z_ERR); 39363339Szt129084 if (verify_details(CMD_CLONE, argv) != Z_OK) 39371300Sgjelinek return (Z_ERR); 39381300Sgjelinek 39391300Sgjelinek /* 39401300Sgjelinek * We also need to do some extra validation on the source zone. 39411300Sgjelinek */ 39421300Sgjelinek if (strcmp(source_zone, GLOBAL_ZONENAME) == 0) { 39431300Sgjelinek zerror(gettext("%s operation is invalid for the global zone."), 39441300Sgjelinek cmd_to_str(CMD_CLONE)); 39451300Sgjelinek return (Z_ERR); 39461300Sgjelinek } 39471300Sgjelinek 39481300Sgjelinek if (strncmp(source_zone, "SUNW", 4) == 0) { 39491300Sgjelinek zerror(gettext("%s operation is invalid for zones starting " 39501300Sgjelinek "with SUNW."), cmd_to_str(CMD_CLONE)); 39511300Sgjelinek return (Z_ERR); 39521300Sgjelinek } 39531300Sgjelinek 39541300Sgjelinek zent = lookup_running_zone(source_zone); 39551300Sgjelinek if (zent != NULL) { 39561300Sgjelinek /* check whether the zone is ready or running */ 39571300Sgjelinek if ((err = zone_get_state(zent->zname, &zent->zstate_num)) 39581300Sgjelinek != Z_OK) { 39591300Sgjelinek errno = err; 39601300Sgjelinek zperror2(zent->zname, gettext("could not get state")); 39611300Sgjelinek /* can't tell, so hedge */ 39621300Sgjelinek zent->zstate_str = "ready/running"; 39631300Sgjelinek } else { 39641300Sgjelinek zent->zstate_str = zone_state_str(zent->zstate_num); 39651300Sgjelinek } 39661300Sgjelinek zerror(gettext("%s operation is invalid for %s zones."), 39671300Sgjelinek cmd_to_str(CMD_CLONE), zent->zstate_str); 39681300Sgjelinek return (Z_ERR); 39691300Sgjelinek } 39701300Sgjelinek 39711300Sgjelinek if ((err = zone_get_state(source_zone, &state)) != Z_OK) { 39721300Sgjelinek errno = err; 39731300Sgjelinek zperror2(source_zone, gettext("could not get state")); 39741300Sgjelinek return (Z_ERR); 39751300Sgjelinek } 39761300Sgjelinek if (state != ZONE_STATE_INSTALLED) { 39771300Sgjelinek (void) fprintf(stderr, 39781300Sgjelinek gettext("%s: zone %s is %s; %s is required.\n"), 39791300Sgjelinek execname, source_zone, zone_state_str(state), 39801300Sgjelinek zone_state_str(ZONE_STATE_INSTALLED)); 39811300Sgjelinek return (Z_ERR); 39821300Sgjelinek } 39831300Sgjelinek 39841300Sgjelinek /* 39851300Sgjelinek * The source zone checks out ok, continue with the clone. 39861300Sgjelinek */ 39871300Sgjelinek 39881300Sgjelinek if (validate_clone(source_zone, target_zone) != Z_OK) 39891300Sgjelinek return (Z_ERR); 39901300Sgjelinek 39911300Sgjelinek if (grab_lock_file(target_zone, &lockfd) != Z_OK) { 39921300Sgjelinek zerror(gettext("another %s may have an operation in progress."), 39931300Sgjelinek "zoneadm"); 39941300Sgjelinek return (Z_ERR); 39951300Sgjelinek } 39961300Sgjelinek 39971300Sgjelinek if ((err = zone_get_zonepath(source_zone, source_zonepath, 39981300Sgjelinek sizeof (source_zonepath))) != Z_OK) { 39991300Sgjelinek errno = err; 40001300Sgjelinek zperror2(source_zone, gettext("could not get zone path")); 40011300Sgjelinek goto done; 40021300Sgjelinek } 40031300Sgjelinek 40041300Sgjelinek if ((err = zone_get_zonepath(target_zone, zonepath, sizeof (zonepath))) 40051300Sgjelinek != Z_OK) { 40061300Sgjelinek errno = err; 40071300Sgjelinek zperror2(target_zone, gettext("could not get zone path")); 40081300Sgjelinek goto done; 40091300Sgjelinek } 40101300Sgjelinek 40111300Sgjelinek if ((err = zone_set_state(target_zone, ZONE_STATE_INCOMPLETE)) 40121300Sgjelinek != Z_OK) { 40131300Sgjelinek errno = err; 40141300Sgjelinek zperror2(target_zone, gettext("could not set state")); 40151300Sgjelinek goto done; 40161300Sgjelinek } 40171300Sgjelinek 40181867Sgjelinek if (snapshot != NULL) { 40191867Sgjelinek err = clone_snapshot_zfs(snapshot, zonepath); 40201867Sgjelinek } else { 40211867Sgjelinek /* 40221867Sgjelinek * We always copy the clone unless the source is ZFS and a 40231867Sgjelinek * ZFS clone worked. We fallback to copying if the ZFS clone 40241867Sgjelinek * fails for some reason. 40251867Sgjelinek */ 40261867Sgjelinek err = Z_ERR; 40271867Sgjelinek if (method == NULL && is_zonepath_zfs(source_zonepath)) 40281867Sgjelinek err = clone_zfs(source_zone, source_zonepath, zonepath); 40291867Sgjelinek 40301867Sgjelinek if (err != Z_OK) 40311867Sgjelinek err = clone_copy(source_zonepath, zonepath); 40321867Sgjelinek } 40331867Sgjelinek 40342712Snn35248 /* 40352712Snn35248 * Trusted Extensions requires that cloned zones use the same sysid 40362712Snn35248 * configuration, so it is not appropriate to perform any 40372712Snn35248 * post-clone reconfiguration. 40382712Snn35248 */ 40392712Snn35248 if ((err == Z_OK) && !is_system_labeled()) 40402712Snn35248 err = zone_postclone(zonepath); 40411300Sgjelinek 40421300Sgjelinek done: 40432712Snn35248 /* 40442712Snn35248 * If everything went well, we mark the zone as installed. 40452712Snn35248 */ 40462712Snn35248 if (err == Z_OK) { 40472712Snn35248 err = zone_set_state(target_zone, ZONE_STATE_INSTALLED); 40482712Snn35248 if (err != Z_OK) { 40492712Snn35248 errno = err; 40502712Snn35248 zperror2(target_zone, gettext("could not set state")); 40512712Snn35248 } 40522712Snn35248 } 40531300Sgjelinek release_lock_file(lockfd); 40541300Sgjelinek return ((err == Z_OK) ? Z_OK : Z_ERR); 40551300Sgjelinek } 40561300Sgjelinek 40571607Sgjelinek /* 40581867Sgjelinek * Used when removing a zonepath after uninstalling or cleaning up after 40591867Sgjelinek * the move subcommand. This handles a zonepath that has non-standard 40601867Sgjelinek * contents so that we will only cleanup the stuff we know about and leave 40611867Sgjelinek * any user data alone. 40621607Sgjelinek * 40631867Sgjelinek * If the "all" parameter is true then we should remove the whole zonepath 40641867Sgjelinek * even if it has non-standard files/directories in it. This can be used when 40651867Sgjelinek * we need to cleanup after moving the zonepath across file systems. 40661867Sgjelinek * 40671867Sgjelinek * We "exec" the RMCOMMAND so that the returned status is that of RMCOMMAND 40681867Sgjelinek * and not the shell. 40691607Sgjelinek */ 40701607Sgjelinek static int 40711867Sgjelinek cleanup_zonepath(char *zonepath, boolean_t all) 40721607Sgjelinek { 40731867Sgjelinek int status; 40741867Sgjelinek int i; 40751867Sgjelinek boolean_t non_std = B_FALSE; 40761867Sgjelinek struct dirent *dp; 40771867Sgjelinek DIR *dirp; 40783686Sgjelinek /* 40793686Sgjelinek * The SUNWattached.xml file is expected since it might 40803686Sgjelinek * exist if the zone was force-attached after a 40813686Sgjelinek * migration. 40823686Sgjelinek */ 40833686Sgjelinek char *std_entries[] = {"dev", "lu", "root", 40843686Sgjelinek "SUNWattached.xml", NULL}; 40851867Sgjelinek /* (MAXPATHLEN * 3) is for the 3 std_entries dirs */ 40861867Sgjelinek char cmdbuf[sizeof (RMCOMMAND) + (MAXPATHLEN * 3) + 64]; 40871867Sgjelinek 40881867Sgjelinek /* 40891867Sgjelinek * We shouldn't need these checks but lets be paranoid since we 40901867Sgjelinek * could blow away the whole system here if we got the wrong zonepath. 40911867Sgjelinek */ 40921867Sgjelinek if (*zonepath == NULL || strcmp(zonepath, "/") == 0) { 40931867Sgjelinek (void) fprintf(stderr, "invalid zonepath '%s'\n", zonepath); 40941867Sgjelinek return (Z_INVAL); 40951867Sgjelinek } 40961867Sgjelinek 40971867Sgjelinek /* 40981867Sgjelinek * If the dirpath is already gone (maybe it was manually removed) then 40991867Sgjelinek * we just return Z_OK so that the cleanup is successful. 41001867Sgjelinek */ 41011867Sgjelinek if ((dirp = opendir(zonepath)) == NULL) 41021867Sgjelinek return (Z_OK); 41031867Sgjelinek 41041867Sgjelinek /* 41051867Sgjelinek * Look through the zonepath directory to see if there are any 41061867Sgjelinek * non-standard files/dirs. Also skip .zfs since that might be 41071867Sgjelinek * there but we'll handle ZFS file systems as a special case. 41081867Sgjelinek */ 41091867Sgjelinek while ((dp = readdir(dirp)) != NULL) { 41101867Sgjelinek if (strcmp(dp->d_name, ".") == 0 || 41111867Sgjelinek strcmp(dp->d_name, "..") == 0 || 41121867Sgjelinek strcmp(dp->d_name, ".zfs") == 0) 41131867Sgjelinek continue; 41141867Sgjelinek 41151867Sgjelinek for (i = 0; std_entries[i] != NULL; i++) 41161867Sgjelinek if (strcmp(dp->d_name, std_entries[i]) == 0) 41171867Sgjelinek break; 41181867Sgjelinek 41191867Sgjelinek if (std_entries[i] == NULL) 41201867Sgjelinek non_std = B_TRUE; 41211867Sgjelinek } 41221867Sgjelinek (void) closedir(dirp); 41231867Sgjelinek 41241867Sgjelinek if (!all && non_std) { 41251607Sgjelinek /* 41261867Sgjelinek * There are extra, non-standard directories/files in the 41271867Sgjelinek * zonepath so we don't want to remove the zonepath. We 41281867Sgjelinek * just want to remove the standard directories and leave 41291867Sgjelinek * the user data alone. 41301607Sgjelinek */ 41311867Sgjelinek (void) snprintf(cmdbuf, sizeof (cmdbuf), "exec " RMCOMMAND); 41321867Sgjelinek 41331867Sgjelinek for (i = 0; std_entries[i] != NULL; i++) { 41341867Sgjelinek char tmpbuf[MAXPATHLEN]; 41351867Sgjelinek 41361867Sgjelinek if (snprintf(tmpbuf, sizeof (tmpbuf), " %s/%s", 41371867Sgjelinek zonepath, std_entries[i]) >= sizeof (tmpbuf) || 41381867Sgjelinek strlcat(cmdbuf, tmpbuf, sizeof (cmdbuf)) >= 41391867Sgjelinek sizeof (cmdbuf)) { 41401867Sgjelinek (void) fprintf(stderr, 41411867Sgjelinek gettext("path is too long\n")); 41421867Sgjelinek return (Z_INVAL); 41431867Sgjelinek } 41441867Sgjelinek } 41451867Sgjelinek 41461867Sgjelinek status = do_subproc(cmdbuf); 41471867Sgjelinek 41481867Sgjelinek (void) fprintf(stderr, gettext("WARNING: Unable to completely " 41491867Sgjelinek "remove %s\nbecause it contains additional user data. " 41501867Sgjelinek "Only the standard directory\nentries have been " 41511867Sgjelinek "removed.\n"), 41521867Sgjelinek zonepath); 41531867Sgjelinek 41542712Snn35248 return ((subproc_status(RMCOMMAND, status, B_TRUE) == 41552712Snn35248 ZONE_SUBPROC_OK) ? Z_OK : Z_ERR); 41561607Sgjelinek } 41571607Sgjelinek 41581867Sgjelinek /* 41591867Sgjelinek * There is nothing unexpected in the zonepath, try to get rid of the 41601867Sgjelinek * whole zonepath directory. 41611867Sgjelinek * 41621867Sgjelinek * If the zonepath is its own zfs file system, try to destroy the 41631867Sgjelinek * file system. If that fails for some reason (e.g. it has clones) 41641867Sgjelinek * then we'll just remove the contents of the zonepath. 41651867Sgjelinek */ 41661867Sgjelinek if (is_zonepath_zfs(zonepath)) { 41671867Sgjelinek if (destroy_zfs(zonepath) == Z_OK) 41681867Sgjelinek return (Z_OK); 41691867Sgjelinek (void) snprintf(cmdbuf, sizeof (cmdbuf), "exec " RMCOMMAND 41701867Sgjelinek " %s/*", zonepath); 41711867Sgjelinek status = do_subproc(cmdbuf); 41722712Snn35248 return ((subproc_status(RMCOMMAND, status, B_TRUE) == 41732712Snn35248 ZONE_SUBPROC_OK) ? Z_OK : Z_ERR); 41741867Sgjelinek } 41751867Sgjelinek 41761867Sgjelinek (void) snprintf(cmdbuf, sizeof (cmdbuf), "exec " RMCOMMAND " %s", 41771867Sgjelinek zonepath); 41781607Sgjelinek status = do_subproc(cmdbuf); 41792712Snn35248 41802712Snn35248 return ((subproc_status(RMCOMMAND, status, B_TRUE) == ZONE_SUBPROC_OK) 41812712Snn35248 ? Z_OK : Z_ERR); 41821607Sgjelinek } 41831607Sgjelinek 41841300Sgjelinek static int 41851300Sgjelinek move_func(int argc, char *argv[]) 41861300Sgjelinek { 41871300Sgjelinek char *new_zonepath = NULL; 41881300Sgjelinek int lockfd; 41891300Sgjelinek int err, arg; 41901300Sgjelinek char zonepath[MAXPATHLEN]; 41911300Sgjelinek zone_dochandle_t handle; 41921300Sgjelinek boolean_t fast; 41931867Sgjelinek boolean_t is_zfs = B_FALSE; 41941867Sgjelinek struct dirent *dp; 41951867Sgjelinek DIR *dirp; 41961867Sgjelinek boolean_t empty = B_TRUE; 41971300Sgjelinek boolean_t revert; 41981300Sgjelinek struct stat zonepath_buf; 41991300Sgjelinek struct stat new_zonepath_buf; 42001300Sgjelinek 42011300Sgjelinek if (zonecfg_in_alt_root()) { 42021300Sgjelinek zerror(gettext("cannot move zone in alternate root")); 42031300Sgjelinek return (Z_ERR); 42041300Sgjelinek } 42051300Sgjelinek 42061300Sgjelinek optind = 0; 42071300Sgjelinek if ((arg = getopt(argc, argv, "?")) != EOF) { 42081300Sgjelinek switch (arg) { 42091300Sgjelinek case '?': 42101300Sgjelinek sub_usage(SHELP_MOVE, CMD_MOVE); 42111300Sgjelinek return (optopt == '?' ? Z_OK : Z_USAGE); 42121300Sgjelinek default: 42131300Sgjelinek sub_usage(SHELP_MOVE, CMD_MOVE); 42141300Sgjelinek return (Z_USAGE); 42151300Sgjelinek } 42161300Sgjelinek } 42171300Sgjelinek if (argc != (optind + 1)) { 42181300Sgjelinek sub_usage(SHELP_MOVE, CMD_MOVE); 42191300Sgjelinek return (Z_USAGE); 42201300Sgjelinek } 42211300Sgjelinek new_zonepath = argv[optind]; 42222712Snn35248 if (sanity_check(target_zone, CMD_MOVE, B_FALSE, B_TRUE, B_FALSE) 42232712Snn35248 != Z_OK) 42241300Sgjelinek return (Z_ERR); 42253339Szt129084 if (verify_details(CMD_MOVE, argv) != Z_OK) 42261300Sgjelinek return (Z_ERR); 42271300Sgjelinek 42281300Sgjelinek /* 42291300Sgjelinek * Check out the new zonepath. This has the side effect of creating 42301300Sgjelinek * a directory for the new zonepath. We depend on this later when we 42311867Sgjelinek * stat to see if we are doing a cross file system move or not. 42321300Sgjelinek */ 42331300Sgjelinek if (validate_zonepath(new_zonepath, CMD_MOVE) != Z_OK) 42341300Sgjelinek return (Z_ERR); 42351300Sgjelinek 42361300Sgjelinek if ((err = zone_get_zonepath(target_zone, zonepath, sizeof (zonepath))) 42371300Sgjelinek != Z_OK) { 42381300Sgjelinek errno = err; 42391300Sgjelinek zperror2(target_zone, gettext("could not get zone path")); 42401300Sgjelinek return (Z_ERR); 42411300Sgjelinek } 42421300Sgjelinek 42431300Sgjelinek if (stat(zonepath, &zonepath_buf) == -1) { 42441300Sgjelinek zperror(gettext("could not stat zone path"), B_FALSE); 42451300Sgjelinek return (Z_ERR); 42461300Sgjelinek } 42471300Sgjelinek 42481300Sgjelinek if (stat(new_zonepath, &new_zonepath_buf) == -1) { 42491300Sgjelinek zperror(gettext("could not stat new zone path"), B_FALSE); 42501300Sgjelinek return (Z_ERR); 42511300Sgjelinek } 42521300Sgjelinek 42531867Sgjelinek /* 42541867Sgjelinek * Check if the destination directory is empty. 42551867Sgjelinek */ 42561867Sgjelinek if ((dirp = opendir(new_zonepath)) == NULL) { 42571867Sgjelinek zperror(gettext("could not open new zone path"), B_FALSE); 42581867Sgjelinek return (Z_ERR); 42591867Sgjelinek } 42601867Sgjelinek while ((dp = readdir(dirp)) != (struct dirent *)0) { 42611867Sgjelinek if (strcmp(dp->d_name, ".") == 0 || 42621867Sgjelinek strcmp(dp->d_name, "..") == 0) 42631867Sgjelinek continue; 42641867Sgjelinek empty = B_FALSE; 42651867Sgjelinek break; 42661867Sgjelinek } 42671867Sgjelinek (void) closedir(dirp); 42681867Sgjelinek 42691867Sgjelinek /* Error if there is anything in the destination directory. */ 42701867Sgjelinek if (!empty) { 42711867Sgjelinek (void) fprintf(stderr, gettext("could not move zone to %s: " 42721867Sgjelinek "directory not empty\n"), new_zonepath); 42731867Sgjelinek return (Z_ERR); 42741867Sgjelinek } 42751867Sgjelinek 42761300Sgjelinek /* Don't move the zone if anything is still mounted there */ 42771300Sgjelinek if (zonecfg_find_mounts(zonepath, NULL, NULL)) { 42781867Sgjelinek zerror(gettext("These file systems are mounted on " 42791300Sgjelinek "subdirectories of %s.\n"), zonepath); 42801300Sgjelinek (void) zonecfg_find_mounts(zonepath, zfm_print, NULL); 42811300Sgjelinek return (Z_ERR); 42821300Sgjelinek } 42831300Sgjelinek 42841300Sgjelinek /* 42851867Sgjelinek * Check if we are moving in the same file system and can do a fast 42861867Sgjelinek * move or if we are crossing file systems and have to copy the data. 42871300Sgjelinek */ 42881300Sgjelinek fast = (zonepath_buf.st_dev == new_zonepath_buf.st_dev); 42891300Sgjelinek 42901300Sgjelinek if ((handle = zonecfg_init_handle()) == NULL) { 42911300Sgjelinek zperror(cmd_to_str(CMD_MOVE), B_TRUE); 42921300Sgjelinek return (Z_ERR); 42931300Sgjelinek } 42941300Sgjelinek 42951300Sgjelinek if ((err = zonecfg_get_handle(target_zone, handle)) != Z_OK) { 42961300Sgjelinek errno = err; 42971300Sgjelinek zperror(cmd_to_str(CMD_MOVE), B_TRUE); 42981300Sgjelinek zonecfg_fini_handle(handle); 42991300Sgjelinek return (Z_ERR); 43001300Sgjelinek } 43011300Sgjelinek 43021300Sgjelinek if (grab_lock_file(target_zone, &lockfd) != Z_OK) { 43031300Sgjelinek zerror(gettext("another %s may have an operation in progress."), 43041300Sgjelinek "zoneadm"); 43051300Sgjelinek zonecfg_fini_handle(handle); 43061300Sgjelinek return (Z_ERR); 43071300Sgjelinek } 43081300Sgjelinek 43091300Sgjelinek /* 43101867Sgjelinek * We're making some file system changes now so we have to clean up 43111867Sgjelinek * the file system before we are done. This will either clean up the 43121300Sgjelinek * new zonepath if the zonecfg update failed or it will clean up the 43131300Sgjelinek * old zonepath if everything is ok. 43141300Sgjelinek */ 43151300Sgjelinek revert = B_TRUE; 43161300Sgjelinek 43171867Sgjelinek if (is_zonepath_zfs(zonepath) && 43181867Sgjelinek move_zfs(zonepath, new_zonepath) != Z_ERR) { 43191867Sgjelinek is_zfs = B_TRUE; 43201867Sgjelinek 43211867Sgjelinek } else if (fast) { 43221867Sgjelinek /* same file system, use rename for a quick move */ 43231300Sgjelinek 43241300Sgjelinek /* 43251300Sgjelinek * Remove the new_zonepath directory that got created above 43261300Sgjelinek * during the validation. It gets in the way of the rename. 43271300Sgjelinek */ 43281300Sgjelinek if (rmdir(new_zonepath) != 0) { 43291300Sgjelinek zperror(gettext("could not rmdir new zone path"), 43301300Sgjelinek B_FALSE); 43311300Sgjelinek zonecfg_fini_handle(handle); 43321300Sgjelinek release_lock_file(lockfd); 43331300Sgjelinek return (Z_ERR); 43341300Sgjelinek } 43351300Sgjelinek 43361300Sgjelinek if (rename(zonepath, new_zonepath) != 0) { 43371300Sgjelinek /* 43381300Sgjelinek * If this fails we don't need to do all of the 43391300Sgjelinek * cleanup that happens for the rest of the code 43401300Sgjelinek * so just return from this error. 43411300Sgjelinek */ 43421300Sgjelinek zperror(gettext("could not move zone"), B_FALSE); 43431300Sgjelinek zonecfg_fini_handle(handle); 43441300Sgjelinek release_lock_file(lockfd); 43451300Sgjelinek return (Z_ERR); 43461300Sgjelinek } 43471300Sgjelinek 43481300Sgjelinek } else { 43491867Sgjelinek /* 43501867Sgjelinek * Attempt to create a ZFS fs for the new zonepath. As usual, 43511867Sgjelinek * we don't care if this works or not since we always have the 43521867Sgjelinek * default behavior of a simple directory for the zonepath. 43531867Sgjelinek */ 43541867Sgjelinek create_zfs_zonepath(new_zonepath); 43551867Sgjelinek 43561300Sgjelinek (void) printf(gettext( 43571867Sgjelinek "Moving across file systems; copying zonepath %s..."), 43581300Sgjelinek zonepath); 43591300Sgjelinek (void) fflush(stdout); 43601300Sgjelinek 43611300Sgjelinek err = copy_zone(zonepath, new_zonepath); 43621300Sgjelinek 43631300Sgjelinek (void) printf("\n"); 43641300Sgjelinek if (err != Z_OK) 43651300Sgjelinek goto done; 43661300Sgjelinek } 43671300Sgjelinek 43681300Sgjelinek if ((err = zonecfg_set_zonepath(handle, new_zonepath)) != Z_OK) { 43691300Sgjelinek errno = err; 43701300Sgjelinek zperror(gettext("could not set new zonepath"), B_TRUE); 43711300Sgjelinek goto done; 43721300Sgjelinek } 43731300Sgjelinek 43741300Sgjelinek if ((err = zonecfg_save(handle)) != Z_OK) { 43751300Sgjelinek errno = err; 43761300Sgjelinek zperror(gettext("zonecfg save failed"), B_TRUE); 43771300Sgjelinek goto done; 43781300Sgjelinek } 43791300Sgjelinek 43801300Sgjelinek revert = B_FALSE; 43811300Sgjelinek 43821300Sgjelinek done: 43831300Sgjelinek zonecfg_fini_handle(handle); 43841300Sgjelinek release_lock_file(lockfd); 43851300Sgjelinek 43861300Sgjelinek /* 43871867Sgjelinek * Clean up the file system based on how things went. We either 43881300Sgjelinek * clean up the new zonepath if the operation failed for some reason 43891300Sgjelinek * or we clean up the old zonepath if everything is ok. 43901300Sgjelinek */ 43911300Sgjelinek if (revert) { 43921300Sgjelinek /* The zonecfg update failed, cleanup the new zonepath. */ 43931867Sgjelinek if (is_zfs) { 43941867Sgjelinek if (move_zfs(new_zonepath, zonepath) == Z_ERR) { 43951867Sgjelinek (void) fprintf(stderr, gettext("could not " 43961867Sgjelinek "restore zonepath, the zfs mountpoint is " 43971867Sgjelinek "set as:\n%s\n"), new_zonepath); 43981867Sgjelinek /* 43991867Sgjelinek * err is already != Z_OK since we're reverting 44001867Sgjelinek */ 44011867Sgjelinek } 44021867Sgjelinek 44031867Sgjelinek } else if (fast) { 44041300Sgjelinek if (rename(new_zonepath, zonepath) != 0) { 44051300Sgjelinek zperror(gettext("could not restore zonepath"), 44061300Sgjelinek B_FALSE); 44071300Sgjelinek /* 44081300Sgjelinek * err is already != Z_OK since we're reverting 44091300Sgjelinek */ 44101300Sgjelinek } 44111300Sgjelinek } else { 44121300Sgjelinek (void) printf(gettext("Cleaning up zonepath %s..."), 44131300Sgjelinek new_zonepath); 44141300Sgjelinek (void) fflush(stdout); 44151867Sgjelinek err = cleanup_zonepath(new_zonepath, B_TRUE); 44161300Sgjelinek (void) printf("\n"); 44171300Sgjelinek 44181607Sgjelinek if (err != Z_OK) { 44191300Sgjelinek errno = err; 44201300Sgjelinek zperror(gettext("could not remove new " 44211300Sgjelinek "zonepath"), B_TRUE); 44221300Sgjelinek } else { 44231300Sgjelinek /* 44241300Sgjelinek * Because we're reverting we know the mainline 44251300Sgjelinek * code failed but we just reused the err 44261300Sgjelinek * variable so we reset it back to Z_ERR. 44271300Sgjelinek */ 44281300Sgjelinek err = Z_ERR; 44291300Sgjelinek } 44301300Sgjelinek } 44311300Sgjelinek 44321300Sgjelinek } else { 44331300Sgjelinek /* The move was successful, cleanup the old zonepath. */ 44341867Sgjelinek if (!is_zfs && !fast) { 44351300Sgjelinek (void) printf( 44361300Sgjelinek gettext("Cleaning up zonepath %s..."), zonepath); 44371300Sgjelinek (void) fflush(stdout); 44381867Sgjelinek err = cleanup_zonepath(zonepath, B_TRUE); 44391300Sgjelinek (void) printf("\n"); 44401300Sgjelinek 44411607Sgjelinek if (err != Z_OK) { 44421300Sgjelinek errno = err; 44431300Sgjelinek zperror(gettext("could not remove zonepath"), 44441300Sgjelinek B_TRUE); 44451300Sgjelinek } 44461300Sgjelinek } 44471300Sgjelinek } 44481300Sgjelinek 44491300Sgjelinek return ((err == Z_OK) ? Z_OK : Z_ERR); 44501300Sgjelinek } 44511300Sgjelinek 44521507Sgjelinek static int 44531507Sgjelinek detach_func(int argc, char *argv[]) 44541507Sgjelinek { 44551507Sgjelinek int lockfd; 44561507Sgjelinek int err, arg; 44571507Sgjelinek char zonepath[MAXPATHLEN]; 44581507Sgjelinek zone_dochandle_t handle; 44592078Sgjelinek boolean_t execute = B_TRUE; 44601507Sgjelinek 44611507Sgjelinek if (zonecfg_in_alt_root()) { 44621507Sgjelinek zerror(gettext("cannot detach zone in alternate root")); 44631507Sgjelinek return (Z_ERR); 44641507Sgjelinek } 44651507Sgjelinek 44661507Sgjelinek optind = 0; 44672078Sgjelinek if ((arg = getopt(argc, argv, "?n")) != EOF) { 44681507Sgjelinek switch (arg) { 44691507Sgjelinek case '?': 44701507Sgjelinek sub_usage(SHELP_DETACH, CMD_DETACH); 44711507Sgjelinek return (optopt == '?' ? Z_OK : Z_USAGE); 44722078Sgjelinek case 'n': 44732078Sgjelinek execute = B_FALSE; 44742078Sgjelinek break; 44751507Sgjelinek default: 44761507Sgjelinek sub_usage(SHELP_DETACH, CMD_DETACH); 44771507Sgjelinek return (Z_USAGE); 44781507Sgjelinek } 44791507Sgjelinek } 44802712Snn35248 44812078Sgjelinek if (execute) { 44822712Snn35248 if (sanity_check(target_zone, CMD_DETACH, B_FALSE, B_TRUE, 44832712Snn35248 B_FALSE) != Z_OK) 44842078Sgjelinek return (Z_ERR); 44853339Szt129084 if (verify_details(CMD_DETACH, argv) != Z_OK) 44862078Sgjelinek return (Z_ERR); 44872078Sgjelinek } else { 44882078Sgjelinek /* 44892078Sgjelinek * We want a dry-run to work for a non-privileged user so we 44902078Sgjelinek * only do minimal validation. 44912078Sgjelinek */ 44922078Sgjelinek if (target_zone == NULL) { 44932078Sgjelinek zerror(gettext("no zone specified")); 44942078Sgjelinek return (Z_ERR); 44952078Sgjelinek } 44962078Sgjelinek 44972078Sgjelinek if (strcmp(target_zone, GLOBAL_ZONENAME) == 0) { 44982078Sgjelinek zerror(gettext("%s operation is invalid for the " 44992078Sgjelinek "global zone."), cmd_to_str(CMD_DETACH)); 45002078Sgjelinek return (Z_ERR); 45012078Sgjelinek } 45022078Sgjelinek } 45031507Sgjelinek 45041507Sgjelinek if ((err = zone_get_zonepath(target_zone, zonepath, sizeof (zonepath))) 45051507Sgjelinek != Z_OK) { 45061507Sgjelinek errno = err; 45071507Sgjelinek zperror2(target_zone, gettext("could not get zone path")); 45081507Sgjelinek return (Z_ERR); 45091507Sgjelinek } 45101507Sgjelinek 45111507Sgjelinek /* Don't detach the zone if anything is still mounted there */ 45122078Sgjelinek if (execute && zonecfg_find_mounts(zonepath, NULL, NULL)) { 45131867Sgjelinek zerror(gettext("These file systems are mounted on " 45141507Sgjelinek "subdirectories of %s.\n"), zonepath); 45151507Sgjelinek (void) zonecfg_find_mounts(zonepath, zfm_print, NULL); 45161507Sgjelinek return (Z_ERR); 45171507Sgjelinek } 45181507Sgjelinek 45191507Sgjelinek if ((handle = zonecfg_init_handle()) == NULL) { 45201507Sgjelinek zperror(cmd_to_str(CMD_DETACH), B_TRUE); 45211507Sgjelinek return (Z_ERR); 45221507Sgjelinek } 45231507Sgjelinek 45241507Sgjelinek if ((err = zonecfg_get_handle(target_zone, handle)) != Z_OK) { 45251507Sgjelinek errno = err; 45261507Sgjelinek zperror(cmd_to_str(CMD_DETACH), B_TRUE); 45271507Sgjelinek zonecfg_fini_handle(handle); 45281507Sgjelinek return (Z_ERR); 45291507Sgjelinek } 45301507Sgjelinek 45312078Sgjelinek if (execute && grab_lock_file(target_zone, &lockfd) != Z_OK) { 45321507Sgjelinek zerror(gettext("another %s may have an operation in progress."), 45331507Sgjelinek "zoneadm"); 45341507Sgjelinek zonecfg_fini_handle(handle); 45351507Sgjelinek return (Z_ERR); 45361507Sgjelinek } 45371507Sgjelinek 45381507Sgjelinek if ((err = zonecfg_get_detach_info(handle, B_TRUE)) != Z_OK) { 45391507Sgjelinek errno = err; 45401507Sgjelinek zperror(gettext("getting the detach information failed"), 45411507Sgjelinek B_TRUE); 45421507Sgjelinek goto done; 45431507Sgjelinek } 45441507Sgjelinek 45452078Sgjelinek if ((err = zonecfg_detach_save(handle, (execute ? 0 : ZONE_DRY_RUN))) 45462078Sgjelinek != Z_OK) { 45471507Sgjelinek errno = err; 45481507Sgjelinek zperror(gettext("saving the detach manifest failed"), B_TRUE); 45491507Sgjelinek goto done; 45501507Sgjelinek } 45511507Sgjelinek 45522078Sgjelinek /* 45532078Sgjelinek * Set the zone state back to configured unless we are running with the 45542078Sgjelinek * no-execute option. 45552078Sgjelinek */ 45562078Sgjelinek if (execute && (err = zone_set_state(target_zone, 45572078Sgjelinek ZONE_STATE_CONFIGURED)) != Z_OK) { 45581507Sgjelinek errno = err; 45591507Sgjelinek zperror(gettext("could not reset state"), B_TRUE); 45601507Sgjelinek } 45611507Sgjelinek 45621507Sgjelinek done: 45631507Sgjelinek zonecfg_fini_handle(handle); 45642078Sgjelinek if (execute) 45652078Sgjelinek release_lock_file(lockfd); 45661507Sgjelinek 45671507Sgjelinek return ((err == Z_OK) ? Z_OK : Z_ERR); 45681507Sgjelinek } 45691507Sgjelinek 45701507Sgjelinek /* 45711507Sgjelinek * During attach we go through and fix up the /dev entries for the zone 45721507Sgjelinek * we are attaching. In order to regenerate /dev with the correct devices, 45731507Sgjelinek * the old /dev will be removed, the zone readied (which generates a new 45741507Sgjelinek * /dev) then halted, then we use the info from the manifest to update 45751507Sgjelinek * the modes, owners, etc. on the new /dev. 45761507Sgjelinek */ 45771507Sgjelinek static int 45781507Sgjelinek dev_fix(zone_dochandle_t handle) 45791507Sgjelinek { 45801507Sgjelinek int res; 45811507Sgjelinek int err; 45821507Sgjelinek int status; 45831507Sgjelinek struct zone_devpermtab devtab; 45841507Sgjelinek zone_cmd_arg_t zarg; 45851507Sgjelinek char devpath[MAXPATHLEN]; 45861507Sgjelinek /* 6: "exec " and " " */ 45871507Sgjelinek char cmdbuf[sizeof (RMCOMMAND) + MAXPATHLEN + 6]; 45881507Sgjelinek 45891507Sgjelinek if ((res = zonecfg_get_zonepath(handle, devpath, sizeof (devpath))) 45901507Sgjelinek != Z_OK) 45911507Sgjelinek return (res); 45921507Sgjelinek 45931507Sgjelinek if (strlcat(devpath, "/dev", sizeof (devpath)) >= sizeof (devpath)) 45941507Sgjelinek return (Z_TOO_BIG); 45951507Sgjelinek 45961507Sgjelinek /* 45971507Sgjelinek * "exec" the command so that the returned status is that of 45981507Sgjelinek * RMCOMMAND and not the shell. 45991507Sgjelinek */ 46002712Snn35248 (void) snprintf(cmdbuf, sizeof (cmdbuf), EXEC_PREFIX RMCOMMAND " %s", 46011507Sgjelinek devpath); 46021507Sgjelinek status = do_subproc(cmdbuf); 46032712Snn35248 if ((err = subproc_status(RMCOMMAND, status, B_TRUE)) != 46042712Snn35248 ZONE_SUBPROC_OK) { 46051507Sgjelinek (void) fprintf(stderr, 46061507Sgjelinek gettext("could not remove existing /dev\n")); 46071507Sgjelinek return (Z_ERR); 46081507Sgjelinek } 46091507Sgjelinek 46101507Sgjelinek /* In order to ready the zone, it must be in the installed state */ 46111507Sgjelinek if ((err = zone_set_state(target_zone, ZONE_STATE_INSTALLED)) != Z_OK) { 46121507Sgjelinek errno = err; 46131507Sgjelinek zperror(gettext("could not reset state"), B_TRUE); 46141507Sgjelinek return (Z_ERR); 46151507Sgjelinek } 46161507Sgjelinek 46171507Sgjelinek /* We have to ready the zone to regen the dev tree */ 46181507Sgjelinek zarg.cmd = Z_READY; 46191507Sgjelinek if (call_zoneadmd(target_zone, &zarg) != 0) { 46201507Sgjelinek zerror(gettext("call to %s failed"), "zoneadmd"); 46213247Sgjelinek /* attempt to restore zone to configured state */ 46223247Sgjelinek (void) zone_set_state(target_zone, ZONE_STATE_CONFIGURED); 46231507Sgjelinek return (Z_ERR); 46241507Sgjelinek } 46251507Sgjelinek 46261507Sgjelinek zarg.cmd = Z_HALT; 46271507Sgjelinek if (call_zoneadmd(target_zone, &zarg) != 0) { 46281507Sgjelinek zerror(gettext("call to %s failed"), "zoneadmd"); 46293247Sgjelinek /* attempt to restore zone to configured state */ 46303247Sgjelinek (void) zone_set_state(target_zone, ZONE_STATE_CONFIGURED); 46311507Sgjelinek return (Z_ERR); 46321507Sgjelinek } 46331507Sgjelinek 46343247Sgjelinek /* attempt to restore zone to configured state */ 46353247Sgjelinek (void) zone_set_state(target_zone, ZONE_STATE_CONFIGURED); 46363247Sgjelinek 46371507Sgjelinek if (zonecfg_setdevperment(handle) != Z_OK) { 46381507Sgjelinek (void) fprintf(stderr, 46391507Sgjelinek gettext("unable to enumerate device entries\n")); 46401507Sgjelinek return (Z_ERR); 46411507Sgjelinek } 46421507Sgjelinek 46431507Sgjelinek while (zonecfg_getdevperment(handle, &devtab) == Z_OK) { 46441507Sgjelinek int err; 46451507Sgjelinek 46461507Sgjelinek if ((err = zonecfg_devperms_apply(handle, 46471507Sgjelinek devtab.zone_devperm_name, devtab.zone_devperm_uid, 46481507Sgjelinek devtab.zone_devperm_gid, devtab.zone_devperm_mode, 46491507Sgjelinek devtab.zone_devperm_acl)) != Z_OK && err != Z_INVAL) 46501507Sgjelinek (void) fprintf(stderr, gettext("error updating device " 46511507Sgjelinek "%s: %s\n"), devtab.zone_devperm_name, 46521507Sgjelinek zonecfg_strerror(err)); 46531507Sgjelinek 46541507Sgjelinek free(devtab.zone_devperm_acl); 46551507Sgjelinek } 46561507Sgjelinek 46571507Sgjelinek (void) zonecfg_enddevperment(handle); 46581507Sgjelinek 46591507Sgjelinek return (Z_OK); 46601507Sgjelinek } 46611507Sgjelinek 46622078Sgjelinek /* 46632078Sgjelinek * Validate attaching a zone but don't actually do the work. The zone 46642078Sgjelinek * does not have to exist, so there is some complexity getting a new zone 46652078Sgjelinek * configuration set up so that we can perform the validation. This is 46662078Sgjelinek * handled within zonecfg_attach_manifest() which returns two handles; one 46672078Sgjelinek * for the the full configuration to validate (rem_handle) and the other 46682078Sgjelinek * (local_handle) containing only the zone configuration derived from the 46692078Sgjelinek * manifest. 46702078Sgjelinek */ 46712078Sgjelinek static int 46723339Szt129084 dryrun_attach(char *manifest_path, char *argv[]) 46732078Sgjelinek { 46742078Sgjelinek int fd; 46752078Sgjelinek int err; 46762078Sgjelinek int res; 46772078Sgjelinek zone_dochandle_t local_handle; 46782078Sgjelinek zone_dochandle_t rem_handle = NULL; 46792078Sgjelinek 46802078Sgjelinek if (strcmp(manifest_path, "-") == 0) { 46812078Sgjelinek fd = 0; 46822078Sgjelinek } else if ((fd = open(manifest_path, O_RDONLY)) < 0) { 46832078Sgjelinek zperror(gettext("could not open manifest path"), B_FALSE); 46842078Sgjelinek return (Z_ERR); 46852078Sgjelinek } 46862078Sgjelinek 46872078Sgjelinek if ((local_handle = zonecfg_init_handle()) == NULL) { 46882078Sgjelinek zperror(cmd_to_str(CMD_ATTACH), B_TRUE); 46892078Sgjelinek res = Z_ERR; 46902078Sgjelinek goto done; 46912078Sgjelinek } 46922078Sgjelinek 46932078Sgjelinek if ((rem_handle = zonecfg_init_handle()) == NULL) { 46942078Sgjelinek zperror(cmd_to_str(CMD_ATTACH), B_TRUE); 46952078Sgjelinek res = Z_ERR; 46962078Sgjelinek goto done; 46972078Sgjelinek } 46982078Sgjelinek 46992078Sgjelinek if ((err = zonecfg_attach_manifest(fd, local_handle, rem_handle)) 47002078Sgjelinek != Z_OK) { 47013686Sgjelinek res = Z_ERR; 47023686Sgjelinek 47033686Sgjelinek if (err == Z_INVALID_DOCUMENT) { 47043686Sgjelinek struct stat st; 47053686Sgjelinek char buf[6]; 47063686Sgjelinek 47073686Sgjelinek if (strcmp(manifest_path, "-") == 0) { 47083686Sgjelinek zerror(gettext("Input is not a valid XML " 47093686Sgjelinek "file")); 47103686Sgjelinek goto done; 47113686Sgjelinek } 47123686Sgjelinek 47133686Sgjelinek if (fstat(fd, &st) == -1 || !S_ISREG(st.st_mode)) { 47143686Sgjelinek zerror(gettext("%s is not an XML file"), 47153686Sgjelinek manifest_path); 47163686Sgjelinek goto done; 47173686Sgjelinek } 47183686Sgjelinek 47193686Sgjelinek bzero(buf, sizeof (buf)); 47203686Sgjelinek (void) lseek(fd, 0L, SEEK_SET); 47213686Sgjelinek if (read(fd, buf, sizeof (buf) - 1) < 0 || 47223686Sgjelinek strncmp(buf, "<?xml", 5) != 0) 47233686Sgjelinek zerror(gettext("%s is not an XML file"), 47243686Sgjelinek manifest_path); 47253686Sgjelinek else 47263686Sgjelinek zerror(gettext("Cannot attach to an earlier " 47273686Sgjelinek "release of the operating system")); 47283686Sgjelinek } else { 47292078Sgjelinek zperror(cmd_to_str(CMD_ATTACH), B_TRUE); 47303686Sgjelinek } 47312078Sgjelinek goto done; 47322078Sgjelinek } 47332078Sgjelinek 47343172Sgjelinek /* 47353172Sgjelinek * Retrieve remote handle brand type and determine whether it is 47363172Sgjelinek * native or not. 47373172Sgjelinek */ 47383172Sgjelinek if (zonecfg_get_brand(rem_handle, target_brand, sizeof (target_brand)) 47393172Sgjelinek != Z_OK) { 47403172Sgjelinek zerror(gettext("missing or invalid brand")); 47413172Sgjelinek exit(Z_ERR); 47423172Sgjelinek } 47433172Sgjelinek is_native_zone = (strcmp(target_brand, NATIVE_BRAND_NAME) == 0); 47444350Std153743 is_cluster_zone = 47454350Std153743 (strcmp(target_brand, CLUSTER_BRAND_NAME) == 0); 47463172Sgjelinek 47473339Szt129084 res = verify_handle(CMD_ATTACH, local_handle, argv); 47482078Sgjelinek 47492078Sgjelinek /* Get the detach information for the locally defined zone. */ 47502078Sgjelinek if ((err = zonecfg_get_detach_info(local_handle, B_FALSE)) != Z_OK) { 47512078Sgjelinek errno = err; 47522078Sgjelinek zperror(gettext("getting the attach information failed"), 47532078Sgjelinek B_TRUE); 47542078Sgjelinek res = Z_ERR; 47552078Sgjelinek } else { 47562078Sgjelinek /* sw_cmp prints error msgs as necessary */ 47572078Sgjelinek if (sw_cmp(local_handle, rem_handle, SW_CMP_NONE) != Z_OK) 47582078Sgjelinek res = Z_ERR; 47592078Sgjelinek } 47602078Sgjelinek 47612078Sgjelinek done: 47622078Sgjelinek if (strcmp(manifest_path, "-") != 0) 47632078Sgjelinek (void) close(fd); 47642078Sgjelinek 47652078Sgjelinek zonecfg_fini_handle(local_handle); 47662078Sgjelinek zonecfg_fini_handle(rem_handle); 47672078Sgjelinek 47682078Sgjelinek return ((res == Z_OK) ? Z_OK : Z_ERR); 47692078Sgjelinek } 47702078Sgjelinek 47713777Sgjelinek /* 47723777Sgjelinek * Attempt to generate the information we need to make the zone look like 47733777Sgjelinek * it was properly detached by using the pkg information contained within 47743777Sgjelinek * the zone itself. 47753777Sgjelinek * 47763777Sgjelinek * We will perform a dry-run detach within the zone to generate the xml file. 47773777Sgjelinek * To do this we need to be able to get a handle on the zone so we can see 47783777Sgjelinek * how it is configured. In order to get a handle, we need a copy of the 47793777Sgjelinek * zone configuration within the zone. Since the zone's configuration is 47803777Sgjelinek * not available within the zone itself, we need to temporarily copy it into 47813777Sgjelinek * the zone. 47823777Sgjelinek * 47833777Sgjelinek * The sequence of actions we are doing is this: 47843777Sgjelinek * [set zone state to installed] 47853777Sgjelinek * [mount zone] 47863777Sgjelinek * zlogin {zone} </etc/zones/{zone}.xml 'cat >/etc/zones/{zone}.xml' 47873777Sgjelinek * zlogin {zone} 'zoneadm -z {zone} detach -n' >{zonepath}/SUNWdetached.xml 47883777Sgjelinek * zlogin {zone} 'rm -f /etc/zones/{zone}.xml' 47893777Sgjelinek * [unmount zone] 47903777Sgjelinek * [set zone state to configured] 47913777Sgjelinek * 47923777Sgjelinek * The successful result of this function is that we will have a 47933777Sgjelinek * SUNWdetached.xml file in the zonepath and we can use that to attach the zone. 47943777Sgjelinek */ 47953777Sgjelinek static boolean_t 47963777Sgjelinek gen_detach_info(char *zonepath) 47973777Sgjelinek { 47983777Sgjelinek int status; 47993777Sgjelinek boolean_t mounted = B_FALSE; 48003777Sgjelinek boolean_t res = B_FALSE; 48013777Sgjelinek char cmdbuf[2 * MAXPATHLEN]; 48023777Sgjelinek 48033777Sgjelinek /* 48043777Sgjelinek * The zone has to be installed to mount and zlogin. Temporarily set 48053777Sgjelinek * the state to 'installed'. 48063777Sgjelinek */ 48073777Sgjelinek if (zone_set_state(target_zone, ZONE_STATE_INSTALLED) != Z_OK) 48083777Sgjelinek return (B_FALSE); 48093777Sgjelinek 48103777Sgjelinek /* Mount the zone so we can zlogin. */ 48113777Sgjelinek if (mount_func(0, NULL) != Z_OK) 48123777Sgjelinek goto cleanup; 48133777Sgjelinek mounted = B_TRUE; 48143777Sgjelinek 48153777Sgjelinek /* 48163777Sgjelinek * We need to copy the zones xml configuration file into the 48173777Sgjelinek * zone so we can get a handle for the zone while running inside 48183777Sgjelinek * the zone. 48193777Sgjelinek */ 48203777Sgjelinek if (snprintf(cmdbuf, sizeof (cmdbuf), "/usr/sbin/zlogin -S %s " 48213777Sgjelinek "</etc/zones/%s.xml '/usr/bin/cat >/etc/zones/%s.xml'", 48223777Sgjelinek target_zone, target_zone, target_zone) >= sizeof (cmdbuf)) 48233777Sgjelinek goto cleanup; 48243777Sgjelinek 48253777Sgjelinek status = do_subproc_interactive(cmdbuf); 48263777Sgjelinek if (subproc_status("copy", status, B_TRUE) != ZONE_SUBPROC_OK) 48273777Sgjelinek goto cleanup; 48283777Sgjelinek 48293777Sgjelinek /* Now run the detach command within the mounted zone. */ 48303777Sgjelinek if (snprintf(cmdbuf, sizeof (cmdbuf), "/usr/sbin/zlogin -S %s " 48313777Sgjelinek "'/usr/sbin/zoneadm -z %s detach -n' >%s/SUNWdetached.xml", 48323777Sgjelinek target_zone, target_zone, zonepath) >= sizeof (cmdbuf)) 48333777Sgjelinek goto cleanup; 48343777Sgjelinek 48353777Sgjelinek status = do_subproc_interactive(cmdbuf); 48363777Sgjelinek if (subproc_status("detach", status, B_TRUE) != ZONE_SUBPROC_OK) 48373777Sgjelinek goto cleanup; 48383777Sgjelinek 48393777Sgjelinek res = B_TRUE; 48403777Sgjelinek 48413777Sgjelinek cleanup: 48423777Sgjelinek /* Cleanup from the previous actions. */ 48433777Sgjelinek if (mounted) { 48443777Sgjelinek if (snprintf(cmdbuf, sizeof (cmdbuf), 48453777Sgjelinek "/usr/sbin/zlogin -S %s '/usr/bin/rm -f /etc/zones/%s.xml'", 48463777Sgjelinek target_zone, target_zone) >= sizeof (cmdbuf)) { 48473777Sgjelinek res = B_FALSE; 48483777Sgjelinek } else { 48493777Sgjelinek status = do_subproc(cmdbuf); 48503777Sgjelinek if (subproc_status("rm", status, B_TRUE) 48513777Sgjelinek != ZONE_SUBPROC_OK) 48523777Sgjelinek res = B_FALSE; 48533777Sgjelinek } 48543777Sgjelinek 48553777Sgjelinek if (unmount_func(0, NULL) != Z_OK) 48563777Sgjelinek res = B_FALSE; 48573777Sgjelinek } 48583777Sgjelinek 48593777Sgjelinek if (zone_set_state(target_zone, ZONE_STATE_CONFIGURED) != Z_OK) 48603777Sgjelinek res = B_FALSE; 48613777Sgjelinek 48623777Sgjelinek return (res); 48633777Sgjelinek } 48643777Sgjelinek 48651507Sgjelinek static int 48661507Sgjelinek attach_func(int argc, char *argv[]) 48671507Sgjelinek { 48681507Sgjelinek int lockfd; 48691507Sgjelinek int err, arg; 48701507Sgjelinek boolean_t force = B_FALSE; 48711507Sgjelinek zone_dochandle_t handle; 48721507Sgjelinek zone_dochandle_t athandle = NULL; 48731507Sgjelinek char zonepath[MAXPATHLEN]; 48742712Snn35248 char brand[MAXNAMELEN], atbrand[MAXNAMELEN]; 48752078Sgjelinek boolean_t execute = B_TRUE; 48763777Sgjelinek boolean_t retried = B_FALSE; 48772078Sgjelinek char *manifest_path; 48781507Sgjelinek 48791507Sgjelinek if (zonecfg_in_alt_root()) { 48801507Sgjelinek zerror(gettext("cannot attach zone in alternate root")); 48811507Sgjelinek return (Z_ERR); 48821507Sgjelinek } 48831507Sgjelinek 48841507Sgjelinek optind = 0; 48852078Sgjelinek if ((arg = getopt(argc, argv, "?Fn:")) != EOF) { 48861507Sgjelinek switch (arg) { 48871507Sgjelinek case '?': 48881507Sgjelinek sub_usage(SHELP_ATTACH, CMD_ATTACH); 48891507Sgjelinek return (optopt == '?' ? Z_OK : Z_USAGE); 48901507Sgjelinek case 'F': 48911507Sgjelinek force = B_TRUE; 48921507Sgjelinek break; 48932078Sgjelinek case 'n': 48942078Sgjelinek execute = B_FALSE; 48952078Sgjelinek manifest_path = optarg; 48962078Sgjelinek break; 48971507Sgjelinek default: 48981507Sgjelinek sub_usage(SHELP_ATTACH, CMD_ATTACH); 48991507Sgjelinek return (Z_USAGE); 49001507Sgjelinek } 49011507Sgjelinek } 49022078Sgjelinek 49032078Sgjelinek /* 49042078Sgjelinek * If the no-execute option was specified, we need to branch down 49052078Sgjelinek * a completely different path since there is no zone required to be 49062078Sgjelinek * configured for this option. 49072078Sgjelinek */ 49082078Sgjelinek if (!execute) 49093339Szt129084 return (dryrun_attach(manifest_path, argv)); 49102078Sgjelinek 49112712Snn35248 if (sanity_check(target_zone, CMD_ATTACH, B_FALSE, B_TRUE, B_FALSE) 49122712Snn35248 != Z_OK) 49131507Sgjelinek return (Z_ERR); 49143339Szt129084 if (verify_details(CMD_ATTACH, argv) != Z_OK) 49151507Sgjelinek return (Z_ERR); 49161507Sgjelinek 49171507Sgjelinek if ((err = zone_get_zonepath(target_zone, zonepath, sizeof (zonepath))) 49181507Sgjelinek != Z_OK) { 49191507Sgjelinek errno = err; 49201507Sgjelinek zperror2(target_zone, gettext("could not get zone path")); 49211507Sgjelinek return (Z_ERR); 49221507Sgjelinek } 49231507Sgjelinek 49241507Sgjelinek if ((handle = zonecfg_init_handle()) == NULL) { 49251507Sgjelinek zperror(cmd_to_str(CMD_ATTACH), B_TRUE); 49261507Sgjelinek return (Z_ERR); 49271507Sgjelinek } 49281507Sgjelinek 49291507Sgjelinek if ((err = zonecfg_get_handle(target_zone, handle)) != Z_OK) { 49301507Sgjelinek errno = err; 49311507Sgjelinek zperror(cmd_to_str(CMD_ATTACH), B_TRUE); 49321507Sgjelinek zonecfg_fini_handle(handle); 49331507Sgjelinek return (Z_ERR); 49341507Sgjelinek } 49351507Sgjelinek 49361507Sgjelinek if (grab_lock_file(target_zone, &lockfd) != Z_OK) { 49371507Sgjelinek zerror(gettext("another %s may have an operation in progress."), 49381507Sgjelinek "zoneadm"); 49391507Sgjelinek zonecfg_fini_handle(handle); 49401507Sgjelinek return (Z_ERR); 49411507Sgjelinek } 49421507Sgjelinek 49431507Sgjelinek if (force) 49441507Sgjelinek goto forced; 49451507Sgjelinek 49461507Sgjelinek if ((athandle = zonecfg_init_handle()) == NULL) { 49471507Sgjelinek zperror(cmd_to_str(CMD_ATTACH), B_TRUE); 49481507Sgjelinek goto done; 49491507Sgjelinek } 49501507Sgjelinek 49513777Sgjelinek retry: 49521507Sgjelinek if ((err = zonecfg_get_attach_handle(zonepath, target_zone, B_TRUE, 49531507Sgjelinek athandle)) != Z_OK) { 49543777Sgjelinek if (err == Z_NO_ZONE) { 49553777Sgjelinek /* 49563777Sgjelinek * Zone was not detached. Try to fall back to getting 49573777Sgjelinek * the needed information from within the zone. 49583777Sgjelinek * However, we can only try to generate the attach 49593777Sgjelinek * information for native branded zones, so fail if the 49603777Sgjelinek * zone is not native. 49613777Sgjelinek */ 49623777Sgjelinek if (!is_native_zone) { 49633777Sgjelinek zerror(gettext("Not a detached zone.")); 49643777Sgjelinek goto done; 49653777Sgjelinek } 49663777Sgjelinek 49673777Sgjelinek if (!retried) { 49683777Sgjelinek zerror(gettext("The zone was not properly " 49693777Sgjelinek "detached.\n\tAttempting to attach " 49703777Sgjelinek "anyway.")); 49713777Sgjelinek if (gen_detach_info(zonepath)) { 49723777Sgjelinek retried = B_TRUE; 49733777Sgjelinek goto retry; 49743777Sgjelinek } 49753777Sgjelinek } 49763777Sgjelinek zerror(gettext("Cannot generate the information " 49773777Sgjelinek "needed to attach this zone.")); 49783777Sgjelinek } else if (err == Z_INVALID_DOCUMENT) { 49791507Sgjelinek zerror(gettext("Cannot attach to an earlier release " 49801507Sgjelinek "of the operating system")); 49813777Sgjelinek } else { 49821507Sgjelinek zperror(cmd_to_str(CMD_ATTACH), B_TRUE); 49833777Sgjelinek } 49841507Sgjelinek goto done; 49851507Sgjelinek } 49861507Sgjelinek 49871507Sgjelinek /* Get the detach information for the locally defined zone. */ 49881507Sgjelinek if ((err = zonecfg_get_detach_info(handle, B_FALSE)) != Z_OK) { 49891507Sgjelinek errno = err; 49901507Sgjelinek zperror(gettext("getting the attach information failed"), 49911507Sgjelinek B_TRUE); 49921507Sgjelinek goto done; 49931507Sgjelinek } 49941507Sgjelinek 49952712Snn35248 /* 49962712Snn35248 * Ensure that the detached and locally defined zones are both of 49972712Snn35248 * the same brand. 49982712Snn35248 */ 49992712Snn35248 if ((zonecfg_get_brand(handle, brand, sizeof (brand)) != 0) || 50002712Snn35248 (zonecfg_get_brand(athandle, atbrand, sizeof (atbrand)) != 0)) { 50012712Snn35248 err = Z_ERR; 50022712Snn35248 zerror(gettext("missing or invalid brand")); 50032712Snn35248 goto done; 50042712Snn35248 } 50052712Snn35248 50062712Snn35248 if (strcmp(atbrand, brand) != NULL) { 50072712Snn35248 err = Z_ERR; 50082712Snn35248 zerror(gettext("Trying to attach a '%s' zone to a '%s' " 50092712Snn35248 "configuration."), atbrand, brand); 50102712Snn35248 goto done; 50112712Snn35248 } 50122712Snn35248 50131507Sgjelinek /* sw_cmp prints error msgs as necessary */ 50141867Sgjelinek if ((err = sw_cmp(handle, athandle, SW_CMP_NONE)) != Z_OK) 50151507Sgjelinek goto done; 50161507Sgjelinek 50171507Sgjelinek if ((err = dev_fix(athandle)) != Z_OK) 50181507Sgjelinek goto done; 50191507Sgjelinek 50201507Sgjelinek forced: 50211507Sgjelinek 50221507Sgjelinek zonecfg_rm_detached(handle, force); 50231507Sgjelinek 50241507Sgjelinek if ((err = zone_set_state(target_zone, ZONE_STATE_INSTALLED)) != Z_OK) { 50251507Sgjelinek errno = err; 50261507Sgjelinek zperror(gettext("could not reset state"), B_TRUE); 50271507Sgjelinek } 50281507Sgjelinek 50291507Sgjelinek done: 50301507Sgjelinek zonecfg_fini_handle(handle); 50311507Sgjelinek release_lock_file(lockfd); 50321507Sgjelinek if (athandle != NULL) 50331507Sgjelinek zonecfg_fini_handle(athandle); 50341507Sgjelinek 50351507Sgjelinek return ((err == Z_OK) ? Z_OK : Z_ERR); 50361507Sgjelinek } 50371507Sgjelinek 50381300Sgjelinek /* 50390Sstevel@tonic-gate * On input, TRUE => yes, FALSE => no. 50400Sstevel@tonic-gate * On return, TRUE => 1, FALSE => 0, could not ask => -1. 50410Sstevel@tonic-gate */ 50420Sstevel@tonic-gate 50430Sstevel@tonic-gate static int 50440Sstevel@tonic-gate ask_yesno(boolean_t default_answer, const char *question) 50450Sstevel@tonic-gate { 50460Sstevel@tonic-gate char line[64]; /* should be large enough to answer yes or no */ 50470Sstevel@tonic-gate 50480Sstevel@tonic-gate if (!isatty(STDIN_FILENO)) 50490Sstevel@tonic-gate return (-1); 50500Sstevel@tonic-gate for (;;) { 50510Sstevel@tonic-gate (void) printf("%s (%s)? ", question, 50520Sstevel@tonic-gate default_answer ? "[y]/n" : "y/[n]"); 50530Sstevel@tonic-gate if (fgets(line, sizeof (line), stdin) == NULL || 50540Sstevel@tonic-gate line[0] == '\n') 50550Sstevel@tonic-gate return (default_answer ? 1 : 0); 50560Sstevel@tonic-gate if (tolower(line[0]) == 'y') 50570Sstevel@tonic-gate return (1); 50580Sstevel@tonic-gate if (tolower(line[0]) == 'n') 50590Sstevel@tonic-gate return (0); 50600Sstevel@tonic-gate } 50610Sstevel@tonic-gate } 50620Sstevel@tonic-gate 50630Sstevel@tonic-gate static int 50640Sstevel@tonic-gate uninstall_func(int argc, char *argv[]) 50650Sstevel@tonic-gate { 50660Sstevel@tonic-gate char line[ZONENAME_MAX + 128]; /* Enough for "Are you sure ..." */ 50671867Sgjelinek char rootpath[MAXPATHLEN], zonepath[MAXPATHLEN]; 50680Sstevel@tonic-gate boolean_t force = B_FALSE; 50690Sstevel@tonic-gate int lockfd, answer; 50700Sstevel@tonic-gate int err, arg; 50710Sstevel@tonic-gate 5072766Scarlsonj if (zonecfg_in_alt_root()) { 5073766Scarlsonj zerror(gettext("cannot uninstall zone in alternate root")); 5074766Scarlsonj return (Z_ERR); 5075766Scarlsonj } 5076766Scarlsonj 50770Sstevel@tonic-gate optind = 0; 50780Sstevel@tonic-gate while ((arg = getopt(argc, argv, "?F")) != EOF) { 50790Sstevel@tonic-gate switch (arg) { 50800Sstevel@tonic-gate case '?': 50810Sstevel@tonic-gate sub_usage(SHELP_UNINSTALL, CMD_UNINSTALL); 50820Sstevel@tonic-gate return (optopt == '?' ? Z_OK : Z_USAGE); 50830Sstevel@tonic-gate case 'F': 50840Sstevel@tonic-gate force = B_TRUE; 50850Sstevel@tonic-gate break; 50860Sstevel@tonic-gate default: 50870Sstevel@tonic-gate sub_usage(SHELP_UNINSTALL, CMD_UNINSTALL); 50880Sstevel@tonic-gate return (Z_USAGE); 50890Sstevel@tonic-gate } 50900Sstevel@tonic-gate } 50910Sstevel@tonic-gate if (argc > optind) { 50920Sstevel@tonic-gate sub_usage(SHELP_UNINSTALL, CMD_UNINSTALL); 50930Sstevel@tonic-gate return (Z_USAGE); 50940Sstevel@tonic-gate } 50950Sstevel@tonic-gate 50962712Snn35248 if (sanity_check(target_zone, CMD_UNINSTALL, B_FALSE, B_TRUE, B_FALSE) 50972712Snn35248 != Z_OK) 50980Sstevel@tonic-gate return (Z_ERR); 50990Sstevel@tonic-gate 51003339Szt129084 /* 51013339Szt129084 * Invoke brand-specific handler. 51023339Szt129084 */ 51033339Szt129084 if (invoke_brand_handler(CMD_UNINSTALL, argv) != Z_OK) 51043339Szt129084 return (Z_ERR); 51053339Szt129084 51060Sstevel@tonic-gate if (!force) { 51070Sstevel@tonic-gate (void) snprintf(line, sizeof (line), 51080Sstevel@tonic-gate gettext("Are you sure you want to %s zone %s"), 51090Sstevel@tonic-gate cmd_to_str(CMD_UNINSTALL), target_zone); 51100Sstevel@tonic-gate if ((answer = ask_yesno(B_FALSE, line)) == 0) { 51110Sstevel@tonic-gate return (Z_OK); 51120Sstevel@tonic-gate } else if (answer == -1) { 51130Sstevel@tonic-gate zerror(gettext("Input not from terminal and -F " 51140Sstevel@tonic-gate "not specified: %s not done."), 51150Sstevel@tonic-gate cmd_to_str(CMD_UNINSTALL)); 51160Sstevel@tonic-gate return (Z_ERR); 51170Sstevel@tonic-gate } 51180Sstevel@tonic-gate } 51190Sstevel@tonic-gate 51201867Sgjelinek if ((err = zone_get_zonepath(target_zone, zonepath, 51211867Sgjelinek sizeof (zonepath))) != Z_OK) { 51220Sstevel@tonic-gate errno = err; 51230Sstevel@tonic-gate zperror2(target_zone, gettext("could not get zone path")); 51240Sstevel@tonic-gate return (Z_ERR); 51250Sstevel@tonic-gate } 51260Sstevel@tonic-gate if ((err = zone_get_rootpath(target_zone, rootpath, 51270Sstevel@tonic-gate sizeof (rootpath))) != Z_OK) { 51280Sstevel@tonic-gate errno = err; 51290Sstevel@tonic-gate zperror2(target_zone, gettext("could not get root path")); 51300Sstevel@tonic-gate return (Z_ERR); 51310Sstevel@tonic-gate } 51320Sstevel@tonic-gate 51330Sstevel@tonic-gate /* 51340Sstevel@tonic-gate * If there seems to be a zoneadmd running for this zone, call it 51350Sstevel@tonic-gate * to tell it that an uninstall is happening; if all goes well it 51360Sstevel@tonic-gate * will then shut itself down. 51370Sstevel@tonic-gate */ 51380Sstevel@tonic-gate if (ping_zoneadmd(target_zone) == Z_OK) { 51390Sstevel@tonic-gate zone_cmd_arg_t zarg; 51400Sstevel@tonic-gate zarg.cmd = Z_NOTE_UNINSTALLING; 51410Sstevel@tonic-gate /* we don't care too much if this fails... just plow on */ 51420Sstevel@tonic-gate (void) call_zoneadmd(target_zone, &zarg); 51430Sstevel@tonic-gate } 51440Sstevel@tonic-gate 51450Sstevel@tonic-gate if (grab_lock_file(target_zone, &lockfd) != Z_OK) { 51460Sstevel@tonic-gate zerror(gettext("another %s may have an operation in progress."), 51471300Sgjelinek "zoneadm"); 51480Sstevel@tonic-gate return (Z_ERR); 51490Sstevel@tonic-gate } 51500Sstevel@tonic-gate 51510Sstevel@tonic-gate /* Don't uninstall the zone if anything is mounted there */ 51520Sstevel@tonic-gate err = zonecfg_find_mounts(rootpath, NULL, NULL); 51530Sstevel@tonic-gate if (err) { 51541867Sgjelinek zerror(gettext("These file systems are mounted on " 51551645Scomay "subdirectories of %s.\n"), rootpath); 51560Sstevel@tonic-gate (void) zonecfg_find_mounts(rootpath, zfm_print, NULL); 51570Sstevel@tonic-gate return (Z_ERR); 51580Sstevel@tonic-gate } 51590Sstevel@tonic-gate 51600Sstevel@tonic-gate err = zone_set_state(target_zone, ZONE_STATE_INCOMPLETE); 51610Sstevel@tonic-gate if (err != Z_OK) { 51620Sstevel@tonic-gate errno = err; 51630Sstevel@tonic-gate zperror2(target_zone, gettext("could not set state")); 51640Sstevel@tonic-gate goto bad; 51650Sstevel@tonic-gate } 51660Sstevel@tonic-gate 51671867Sgjelinek if ((err = cleanup_zonepath(zonepath, B_FALSE)) != Z_OK) { 51681867Sgjelinek errno = err; 51691867Sgjelinek zperror2(target_zone, gettext("cleaning up zonepath failed")); 51700Sstevel@tonic-gate goto bad; 51711867Sgjelinek } 51721867Sgjelinek 51730Sstevel@tonic-gate err = zone_set_state(target_zone, ZONE_STATE_CONFIGURED); 51740Sstevel@tonic-gate if (err != Z_OK) { 51750Sstevel@tonic-gate errno = err; 51760Sstevel@tonic-gate zperror2(target_zone, gettext("could not reset state")); 51770Sstevel@tonic-gate } 51780Sstevel@tonic-gate bad: 51790Sstevel@tonic-gate release_lock_file(lockfd); 51800Sstevel@tonic-gate return (err); 51810Sstevel@tonic-gate } 51820Sstevel@tonic-gate 5183766Scarlsonj /* ARGSUSED */ 5184766Scarlsonj static int 5185766Scarlsonj mount_func(int argc, char *argv[]) 5186766Scarlsonj { 5187766Scarlsonj zone_cmd_arg_t zarg; 51882712Snn35248 boolean_t force = B_FALSE; 51892712Snn35248 int arg; 51902712Snn35248 51912712Snn35248 /* 51922712Snn35248 * The only supported subargument to the "mount" subcommand is 51932712Snn35248 * "-f", which forces us to mount a zone in the INCOMPLETE state. 51942712Snn35248 */ 51952712Snn35248 optind = 0; 51962712Snn35248 if ((arg = getopt(argc, argv, "f")) != EOF) { 51972712Snn35248 switch (arg) { 51982712Snn35248 case 'f': 51992712Snn35248 force = B_TRUE; 52002712Snn35248 break; 52012712Snn35248 default: 52022712Snn35248 return (Z_USAGE); 52032712Snn35248 } 52042712Snn35248 } 52052712Snn35248 if (argc > optind) 5206766Scarlsonj return (Z_USAGE); 52072712Snn35248 52082712Snn35248 if (sanity_check(target_zone, CMD_MOUNT, B_FALSE, B_FALSE, force) 52092712Snn35248 != Z_OK) 5210766Scarlsonj return (Z_ERR); 52113339Szt129084 if (verify_details(CMD_MOUNT, argv) != Z_OK) 5212766Scarlsonj return (Z_ERR); 5213766Scarlsonj 52142712Snn35248 zarg.cmd = force ? Z_FORCEMOUNT : Z_MOUNT; 5215766Scarlsonj if (call_zoneadmd(target_zone, &zarg) != 0) { 5216766Scarlsonj zerror(gettext("call to %s failed"), "zoneadmd"); 5217766Scarlsonj return (Z_ERR); 5218766Scarlsonj } 5219766Scarlsonj return (Z_OK); 5220766Scarlsonj } 5221766Scarlsonj 5222766Scarlsonj /* ARGSUSED */ 5223766Scarlsonj static int 5224766Scarlsonj unmount_func(int argc, char *argv[]) 5225766Scarlsonj { 5226766Scarlsonj zone_cmd_arg_t zarg; 5227766Scarlsonj 5228766Scarlsonj if (argc > 0) 5229766Scarlsonj return (Z_USAGE); 52302712Snn35248 if (sanity_check(target_zone, CMD_UNMOUNT, B_FALSE, B_FALSE, B_FALSE) 52312712Snn35248 != Z_OK) 5232766Scarlsonj return (Z_ERR); 5233766Scarlsonj 5234766Scarlsonj zarg.cmd = Z_UNMOUNT; 5235766Scarlsonj if (call_zoneadmd(target_zone, &zarg) != 0) { 5236766Scarlsonj zerror(gettext("call to %s failed"), "zoneadmd"); 5237766Scarlsonj return (Z_ERR); 5238766Scarlsonj } 5239766Scarlsonj return (Z_OK); 5240766Scarlsonj } 5241766Scarlsonj 52420Sstevel@tonic-gate static int 52432303Scarlsonj mark_func(int argc, char *argv[]) 52442303Scarlsonj { 52452303Scarlsonj int err, lockfd; 52462303Scarlsonj 52472303Scarlsonj if (argc != 1 || strcmp(argv[0], "incomplete") != 0) 52482303Scarlsonj return (Z_USAGE); 52492712Snn35248 if (sanity_check(target_zone, CMD_MARK, B_FALSE, B_FALSE, B_FALSE) 52502712Snn35248 != Z_OK) 52512303Scarlsonj return (Z_ERR); 52522303Scarlsonj 52533339Szt129084 /* 52543339Szt129084 * Invoke brand-specific handler. 52553339Szt129084 */ 52563339Szt129084 if (invoke_brand_handler(CMD_MARK, argv) != Z_OK) 52573339Szt129084 return (Z_ERR); 52583339Szt129084 52592303Scarlsonj if (grab_lock_file(target_zone, &lockfd) != Z_OK) { 52602303Scarlsonj zerror(gettext("another %s may have an operation in progress."), 52612303Scarlsonj "zoneadm"); 52622303Scarlsonj return (Z_ERR); 52632303Scarlsonj } 52642303Scarlsonj 52652303Scarlsonj err = zone_set_state(target_zone, ZONE_STATE_INCOMPLETE); 52662303Scarlsonj if (err != Z_OK) { 52672303Scarlsonj errno = err; 52682303Scarlsonj zperror2(target_zone, gettext("could not set state")); 52692303Scarlsonj } 52702303Scarlsonj release_lock_file(lockfd); 52712303Scarlsonj 52722303Scarlsonj return (err); 52732303Scarlsonj } 52742303Scarlsonj 52753247Sgjelinek /* 52763247Sgjelinek * Check what scheduling class we're running under and print a warning if 52773247Sgjelinek * we're not using FSS. 52783247Sgjelinek */ 52793247Sgjelinek static int 52803247Sgjelinek check_sched_fss(zone_dochandle_t handle) 52813247Sgjelinek { 52823247Sgjelinek char class_name[PC_CLNMSZ]; 52833247Sgjelinek 52843247Sgjelinek if (zonecfg_get_dflt_sched_class(handle, class_name, 52853247Sgjelinek sizeof (class_name)) != Z_OK) { 52863247Sgjelinek zerror(gettext("WARNING: unable to determine the zone's " 52873247Sgjelinek "scheduling class")); 52883247Sgjelinek } else if (strcmp("FSS", class_name) != 0) { 52893247Sgjelinek zerror(gettext("WARNING: The zone.cpu-shares rctl is set but\n" 52903247Sgjelinek "FSS is not the default scheduling class for this zone. " 52913247Sgjelinek "FSS will be\nused for processes in the zone but to get " 52923247Sgjelinek "the full benefit of FSS,\nit should be the default " 52933247Sgjelinek "scheduling class. See dispadmin(1M) for\nmore details.")); 52943247Sgjelinek return (Z_SYSTEM); 52953247Sgjelinek } 52963247Sgjelinek 52973247Sgjelinek return (Z_OK); 52983247Sgjelinek } 52993247Sgjelinek 53003247Sgjelinek static int 53013247Sgjelinek check_cpu_shares_sched(zone_dochandle_t handle) 53023247Sgjelinek { 53033247Sgjelinek int err; 53043247Sgjelinek int res = Z_OK; 53053247Sgjelinek struct zone_rctltab rctl; 53063247Sgjelinek 53073247Sgjelinek if ((err = zonecfg_setrctlent(handle)) != Z_OK) { 53083247Sgjelinek errno = err; 53093247Sgjelinek zperror(cmd_to_str(CMD_APPLY), B_TRUE); 53103247Sgjelinek return (err); 53113247Sgjelinek } 53123247Sgjelinek 53133247Sgjelinek while (zonecfg_getrctlent(handle, &rctl) == Z_OK) { 53143247Sgjelinek if (strcmp(rctl.zone_rctl_name, "zone.cpu-shares") == 0) { 53153247Sgjelinek if (check_sched_fss(handle) != Z_OK) 53163247Sgjelinek res = Z_SYSTEM; 53173247Sgjelinek break; 53183247Sgjelinek } 53193247Sgjelinek } 53203247Sgjelinek 53213247Sgjelinek (void) zonecfg_endrctlent(handle); 53223247Sgjelinek 53233247Sgjelinek return (res); 53243247Sgjelinek } 53253247Sgjelinek 53263247Sgjelinek /* 53273352Sgjelinek * Check if there is a mix of processes running in different pools within the 53283352Sgjelinek * zone. This is currently only going to be called for the global zone from 53293352Sgjelinek * apply_func but that could be generalized in the future. 53303352Sgjelinek */ 53313352Sgjelinek static boolean_t 53323352Sgjelinek mixed_pools(zoneid_t zoneid) 53333352Sgjelinek { 53343352Sgjelinek DIR *dirp; 53353352Sgjelinek dirent_t *dent; 53363352Sgjelinek boolean_t mixed = B_FALSE; 53373352Sgjelinek boolean_t poolid_set = B_FALSE; 53383352Sgjelinek poolid_t last_poolid = 0; 53393352Sgjelinek 53403352Sgjelinek if ((dirp = opendir("/proc")) == NULL) { 53413352Sgjelinek zerror(gettext("could not open /proc")); 53423352Sgjelinek return (B_FALSE); 53433352Sgjelinek } 53443352Sgjelinek 53453352Sgjelinek while ((dent = readdir(dirp)) != NULL) { 53463352Sgjelinek int procfd; 53473352Sgjelinek psinfo_t ps; 53483352Sgjelinek char procpath[MAXPATHLEN]; 53493352Sgjelinek 53503352Sgjelinek if (dent->d_name[0] == '.') 53513352Sgjelinek continue; 53523352Sgjelinek 53533352Sgjelinek (void) snprintf(procpath, sizeof (procpath), "/proc/%s/psinfo", 53543352Sgjelinek dent->d_name); 53553352Sgjelinek 53563352Sgjelinek if ((procfd = open(procpath, O_RDONLY)) == -1) 53573352Sgjelinek continue; 53583352Sgjelinek 53593352Sgjelinek if (read(procfd, &ps, sizeof (ps)) == sizeof (psinfo_t)) { 53603352Sgjelinek /* skip processes in other zones and system processes */ 53613352Sgjelinek if (zoneid != ps.pr_zoneid || ps.pr_flag & SSYS) { 53623352Sgjelinek (void) close(procfd); 53633352Sgjelinek continue; 53643352Sgjelinek } 53653352Sgjelinek 53663352Sgjelinek if (poolid_set) { 53673352Sgjelinek if (ps.pr_poolid != last_poolid) 53683352Sgjelinek mixed = B_TRUE; 53693352Sgjelinek } else { 53703352Sgjelinek last_poolid = ps.pr_poolid; 53713352Sgjelinek poolid_set = B_TRUE; 53723352Sgjelinek } 53733352Sgjelinek } 53743352Sgjelinek 53753352Sgjelinek (void) close(procfd); 53763352Sgjelinek 53773352Sgjelinek if (mixed) 53783352Sgjelinek break; 53793352Sgjelinek } 53803352Sgjelinek 53813352Sgjelinek (void) closedir(dirp); 53823352Sgjelinek 53833352Sgjelinek return (mixed); 53843352Sgjelinek } 53853352Sgjelinek 53863352Sgjelinek /* 53873352Sgjelinek * Check if a persistent or temporary pool is configured for the zone. 53883352Sgjelinek * This is currently only going to be called for the global zone from 53893352Sgjelinek * apply_func but that could be generalized in the future. 53903352Sgjelinek */ 53913352Sgjelinek static boolean_t 53923352Sgjelinek pool_configured(zone_dochandle_t handle) 53933352Sgjelinek { 53943352Sgjelinek int err1, err2; 53953352Sgjelinek struct zone_psettab pset_tab; 53963352Sgjelinek char poolname[MAXPATHLEN]; 53973352Sgjelinek 53983352Sgjelinek err1 = zonecfg_lookup_pset(handle, &pset_tab); 53993352Sgjelinek err2 = zonecfg_get_pool(handle, poolname, sizeof (poolname)); 54003352Sgjelinek 54013352Sgjelinek if (err1 == Z_NO_ENTRY && 54023352Sgjelinek (err2 == Z_NO_ENTRY || (err2 == Z_OK && strlen(poolname) == 0))) 54033352Sgjelinek return (B_FALSE); 54043352Sgjelinek 54053352Sgjelinek return (B_TRUE); 54063352Sgjelinek } 54073352Sgjelinek 54083352Sgjelinek /* 54093247Sgjelinek * This is an undocumented interface which is currently only used to apply 54103247Sgjelinek * the global zone resource management settings when the system boots. 54113247Sgjelinek * This function does not yet properly handle updating a running system so 54123247Sgjelinek * any projects running in the zone would be trashed if this function 54133247Sgjelinek * were to run after the zone had booted. It also does not reset any 54143247Sgjelinek * rctl settings that were removed from zonecfg. There is still work to be 54153247Sgjelinek * done before we can properly support dynamically updating the resource 54163247Sgjelinek * management settings for a running zone (global or non-global). Thus, this 54173247Sgjelinek * functionality is undocumented for now. 54183247Sgjelinek */ 54193247Sgjelinek /* ARGSUSED */ 54203247Sgjelinek static int 54213247Sgjelinek apply_func(int argc, char *argv[]) 54223247Sgjelinek { 54233247Sgjelinek int err; 54243247Sgjelinek int res = Z_OK; 54253247Sgjelinek priv_set_t *privset; 54263247Sgjelinek zoneid_t zoneid; 54273247Sgjelinek zone_dochandle_t handle; 54283247Sgjelinek struct zone_mcaptab mcap; 54293247Sgjelinek char pool_err[128]; 54303247Sgjelinek 54313247Sgjelinek zoneid = getzoneid(); 54323247Sgjelinek 54333247Sgjelinek if (zonecfg_in_alt_root() || zoneid != GLOBAL_ZONEID || 54343247Sgjelinek target_zone == NULL || strcmp(target_zone, GLOBAL_ZONENAME) != 0) 54353247Sgjelinek return (usage(B_FALSE)); 54363247Sgjelinek 54373247Sgjelinek if ((privset = priv_allocset()) == NULL) { 54383247Sgjelinek zerror(gettext("%s failed"), "priv_allocset"); 54393247Sgjelinek return (Z_ERR); 54403247Sgjelinek } 54413247Sgjelinek 54423247Sgjelinek if (getppriv(PRIV_EFFECTIVE, privset) != 0) { 54433247Sgjelinek zerror(gettext("%s failed"), "getppriv"); 54443247Sgjelinek priv_freeset(privset); 54453247Sgjelinek return (Z_ERR); 54463247Sgjelinek } 54473247Sgjelinek 54483247Sgjelinek if (priv_isfullset(privset) == B_FALSE) { 54493247Sgjelinek (void) usage(B_FALSE); 54503247Sgjelinek priv_freeset(privset); 54513247Sgjelinek return (Z_ERR); 54523247Sgjelinek } 54533247Sgjelinek priv_freeset(privset); 54543247Sgjelinek 54553247Sgjelinek if ((handle = zonecfg_init_handle()) == NULL) { 54563247Sgjelinek zperror(cmd_to_str(CMD_APPLY), B_TRUE); 54573247Sgjelinek return (Z_ERR); 54583247Sgjelinek } 54593247Sgjelinek 54603247Sgjelinek if ((err = zonecfg_get_handle(target_zone, handle)) != Z_OK) { 54613247Sgjelinek errno = err; 54623247Sgjelinek zperror(cmd_to_str(CMD_APPLY), B_TRUE); 54633247Sgjelinek zonecfg_fini_handle(handle); 54643247Sgjelinek return (Z_ERR); 54653247Sgjelinek } 54663247Sgjelinek 54673247Sgjelinek /* specific error msgs are printed within apply_rctls */ 54683247Sgjelinek if ((err = zonecfg_apply_rctls(target_zone, handle)) != Z_OK) { 54693247Sgjelinek errno = err; 54703247Sgjelinek zperror(cmd_to_str(CMD_APPLY), B_TRUE); 54713247Sgjelinek res = Z_ERR; 54723247Sgjelinek } 54733247Sgjelinek 54743247Sgjelinek if ((err = check_cpu_shares_sched(handle)) != Z_OK) 54753247Sgjelinek res = Z_ERR; 54763247Sgjelinek 54773352Sgjelinek if (pool_configured(handle)) { 54783352Sgjelinek if (mixed_pools(zoneid)) { 54793352Sgjelinek zerror(gettext("Zone is using multiple resource " 54803352Sgjelinek "pools. The pool\nconfiguration cannot be " 54813352Sgjelinek "applied without rebooting.")); 54823352Sgjelinek res = Z_ERR; 54833352Sgjelinek } else { 54843352Sgjelinek 54853352Sgjelinek /* 54863352Sgjelinek * The next two blocks of code attempt to set up 54873352Sgjelinek * temporary pools as well as persistent pools. In 54883352Sgjelinek * both cases we call the functions unconditionally. 54893352Sgjelinek * Within each funtion the code will check if the zone 54903352Sgjelinek * is actually configured for a temporary pool or 54913352Sgjelinek * persistent pool and just return if there is nothing 54923352Sgjelinek * to do. 54933352Sgjelinek */ 54943352Sgjelinek if ((err = zonecfg_bind_tmp_pool(handle, zoneid, 54953352Sgjelinek pool_err, sizeof (pool_err))) != Z_OK) { 54963352Sgjelinek if (err == Z_POOL || err == Z_POOL_CREATE || 54973352Sgjelinek err == Z_POOL_BIND) 54983352Sgjelinek zerror("%s: %s", zonecfg_strerror(err), 54993352Sgjelinek pool_err); 55003352Sgjelinek else 55013352Sgjelinek zerror(gettext("could not bind zone to " 55023352Sgjelinek "temporary pool: %s"), 55033352Sgjelinek zonecfg_strerror(err)); 55043352Sgjelinek res = Z_ERR; 55053352Sgjelinek } 55063352Sgjelinek 55073352Sgjelinek if ((err = zonecfg_bind_pool(handle, zoneid, pool_err, 55083352Sgjelinek sizeof (pool_err))) != Z_OK) { 55093352Sgjelinek if (err == Z_POOL || err == Z_POOL_BIND) 55103352Sgjelinek zerror("%s: %s", zonecfg_strerror(err), 55113352Sgjelinek pool_err); 55123352Sgjelinek else 55133352Sgjelinek zerror("%s", zonecfg_strerror(err)); 55143352Sgjelinek } 55153352Sgjelinek } 55163247Sgjelinek } 55173247Sgjelinek 55183247Sgjelinek /* 55193247Sgjelinek * If a memory cap is configured, set the cap in the kernel using 55203247Sgjelinek * zone_setattr() and make sure the rcapd SMF service is enabled. 55213247Sgjelinek */ 55223247Sgjelinek if (zonecfg_getmcapent(handle, &mcap) == Z_OK) { 55233247Sgjelinek uint64_t num; 55243247Sgjelinek char smf_err[128]; 55253247Sgjelinek 55263247Sgjelinek num = (uint64_t)strtoll(mcap.zone_physmem_cap, NULL, 10); 55273247Sgjelinek if (zone_setattr(zoneid, ZONE_ATTR_PHYS_MCAP, &num, 0) == -1) { 55283247Sgjelinek zerror(gettext("could not set zone memory cap")); 55293247Sgjelinek res = Z_ERR; 55303247Sgjelinek } 55313247Sgjelinek 55323247Sgjelinek if (zonecfg_enable_rcapd(smf_err, sizeof (smf_err)) != Z_OK) { 55333247Sgjelinek zerror(gettext("enabling system/rcap service failed: " 55343247Sgjelinek "%s"), smf_err); 55353247Sgjelinek res = Z_ERR; 55363247Sgjelinek } 55373247Sgjelinek } 55383247Sgjelinek 55393247Sgjelinek zonecfg_fini_handle(handle); 55403247Sgjelinek 55413247Sgjelinek return (res); 55423247Sgjelinek } 55433247Sgjelinek 55442303Scarlsonj static int 55450Sstevel@tonic-gate help_func(int argc, char *argv[]) 55460Sstevel@tonic-gate { 55470Sstevel@tonic-gate int arg, cmd_num; 55480Sstevel@tonic-gate 55490Sstevel@tonic-gate if (argc == 0) { 55500Sstevel@tonic-gate (void) usage(B_TRUE); 55510Sstevel@tonic-gate return (Z_OK); 55520Sstevel@tonic-gate } 55530Sstevel@tonic-gate optind = 0; 55540Sstevel@tonic-gate if ((arg = getopt(argc, argv, "?")) != EOF) { 55550Sstevel@tonic-gate switch (arg) { 55560Sstevel@tonic-gate case '?': 55570Sstevel@tonic-gate sub_usage(SHELP_HELP, CMD_HELP); 55580Sstevel@tonic-gate return (optopt == '?' ? Z_OK : Z_USAGE); 55590Sstevel@tonic-gate default: 55600Sstevel@tonic-gate sub_usage(SHELP_HELP, CMD_HELP); 55610Sstevel@tonic-gate return (Z_USAGE); 55620Sstevel@tonic-gate } 55630Sstevel@tonic-gate } 55640Sstevel@tonic-gate while (optind < argc) { 5565988Scarlsonj /* Private commands have NULL short_usage; omit them */ 5566988Scarlsonj if ((cmd_num = cmd_match(argv[optind])) < 0 || 5567988Scarlsonj cmdtab[cmd_num].short_usage == NULL) { 55680Sstevel@tonic-gate sub_usage(SHELP_HELP, CMD_HELP); 55690Sstevel@tonic-gate return (Z_USAGE); 55700Sstevel@tonic-gate } 55710Sstevel@tonic-gate sub_usage(cmdtab[cmd_num].short_usage, cmd_num); 55720Sstevel@tonic-gate optind++; 55730Sstevel@tonic-gate } 55740Sstevel@tonic-gate return (Z_OK); 55750Sstevel@tonic-gate } 55760Sstevel@tonic-gate 55770Sstevel@tonic-gate /* 55780Sstevel@tonic-gate * Returns: CMD_MIN thru CMD_MAX on success, -1 on error 55790Sstevel@tonic-gate */ 55800Sstevel@tonic-gate 55810Sstevel@tonic-gate static int 55820Sstevel@tonic-gate cmd_match(char *cmd) 55830Sstevel@tonic-gate { 55840Sstevel@tonic-gate int i; 55850Sstevel@tonic-gate 55860Sstevel@tonic-gate for (i = CMD_MIN; i <= CMD_MAX; i++) { 55870Sstevel@tonic-gate /* return only if there is an exact match */ 55880Sstevel@tonic-gate if (strcmp(cmd, cmdtab[i].cmd_name) == 0) 55890Sstevel@tonic-gate return (cmdtab[i].cmd_num); 55900Sstevel@tonic-gate } 55910Sstevel@tonic-gate return (-1); 55920Sstevel@tonic-gate } 55930Sstevel@tonic-gate 55940Sstevel@tonic-gate static int 55950Sstevel@tonic-gate parse_and_run(int argc, char *argv[]) 55960Sstevel@tonic-gate { 55970Sstevel@tonic-gate int i = cmd_match(argv[0]); 55980Sstevel@tonic-gate 55990Sstevel@tonic-gate if (i < 0) 56000Sstevel@tonic-gate return (usage(B_FALSE)); 56010Sstevel@tonic-gate return (cmdtab[i].handler(argc - 1, &(argv[1]))); 56020Sstevel@tonic-gate } 56030Sstevel@tonic-gate 56040Sstevel@tonic-gate static char * 56050Sstevel@tonic-gate get_execbasename(char *execfullname) 56060Sstevel@tonic-gate { 56070Sstevel@tonic-gate char *last_slash, *execbasename; 56080Sstevel@tonic-gate 56090Sstevel@tonic-gate /* guard against '/' at end of command invocation */ 56100Sstevel@tonic-gate for (;;) { 56110Sstevel@tonic-gate last_slash = strrchr(execfullname, '/'); 56120Sstevel@tonic-gate if (last_slash == NULL) { 56130Sstevel@tonic-gate execbasename = execfullname; 56140Sstevel@tonic-gate break; 56150Sstevel@tonic-gate } else { 56160Sstevel@tonic-gate execbasename = last_slash + 1; 56170Sstevel@tonic-gate if (*execbasename == '\0') { 56180Sstevel@tonic-gate *last_slash = '\0'; 56190Sstevel@tonic-gate continue; 56200Sstevel@tonic-gate } 56210Sstevel@tonic-gate break; 56220Sstevel@tonic-gate } 56230Sstevel@tonic-gate } 56240Sstevel@tonic-gate return (execbasename); 56250Sstevel@tonic-gate } 56260Sstevel@tonic-gate 56270Sstevel@tonic-gate int 56280Sstevel@tonic-gate main(int argc, char **argv) 56290Sstevel@tonic-gate { 56300Sstevel@tonic-gate int arg; 56310Sstevel@tonic-gate zoneid_t zid; 5632766Scarlsonj struct stat st; 56332712Snn35248 char *zone_lock_env; 56342712Snn35248 int err; 56350Sstevel@tonic-gate 56360Sstevel@tonic-gate if ((locale = setlocale(LC_ALL, "")) == NULL) 56370Sstevel@tonic-gate locale = "C"; 56380Sstevel@tonic-gate (void) textdomain(TEXT_DOMAIN); 56390Sstevel@tonic-gate setbuf(stdout, NULL); 56400Sstevel@tonic-gate (void) sigset(SIGHUP, SIG_IGN); 56410Sstevel@tonic-gate execname = get_execbasename(argv[0]); 56420Sstevel@tonic-gate target_zone = NULL; 56430Sstevel@tonic-gate if (chdir("/") != 0) { 56440Sstevel@tonic-gate zerror(gettext("could not change directory to /.")); 56450Sstevel@tonic-gate exit(Z_ERR); 56460Sstevel@tonic-gate } 56470Sstevel@tonic-gate 56482082Seschrock if (init_zfs() != Z_OK) 56492082Seschrock exit(Z_ERR); 56502082Seschrock 56512303Scarlsonj while ((arg = getopt(argc, argv, "?u:z:R:")) != EOF) { 56520Sstevel@tonic-gate switch (arg) { 56530Sstevel@tonic-gate case '?': 56540Sstevel@tonic-gate return (usage(B_TRUE)); 56552303Scarlsonj case 'u': 56562303Scarlsonj target_uuid = optarg; 56572303Scarlsonj break; 56580Sstevel@tonic-gate case 'z': 56590Sstevel@tonic-gate target_zone = optarg; 56600Sstevel@tonic-gate break; 5661766Scarlsonj case 'R': /* private option for admin/install use */ 5662766Scarlsonj if (*optarg != '/') { 5663766Scarlsonj zerror(gettext("root path must be absolute.")); 5664766Scarlsonj exit(Z_ERR); 5665766Scarlsonj } 5666766Scarlsonj if (stat(optarg, &st) == -1 || !S_ISDIR(st.st_mode)) { 5667766Scarlsonj zerror( 5668766Scarlsonj gettext("root path must be a directory.")); 5669766Scarlsonj exit(Z_ERR); 5670766Scarlsonj } 5671766Scarlsonj zonecfg_set_root(optarg); 5672766Scarlsonj break; 56730Sstevel@tonic-gate default: 56740Sstevel@tonic-gate return (usage(B_FALSE)); 56750Sstevel@tonic-gate } 56760Sstevel@tonic-gate } 56770Sstevel@tonic-gate 56780Sstevel@tonic-gate if (optind >= argc) 56790Sstevel@tonic-gate return (usage(B_FALSE)); 56802303Scarlsonj 56812303Scarlsonj if (target_uuid != NULL && *target_uuid != '\0') { 56822303Scarlsonj uuid_t uuid; 56832303Scarlsonj static char newtarget[ZONENAME_MAX]; 56842303Scarlsonj 56852303Scarlsonj if (uuid_parse(target_uuid, uuid) == -1) { 56862303Scarlsonj zerror(gettext("illegal UUID value specified")); 56872303Scarlsonj exit(Z_ERR); 56882303Scarlsonj } 56892303Scarlsonj if (zonecfg_get_name_by_uuid(uuid, newtarget, 56902303Scarlsonj sizeof (newtarget)) == Z_OK) 56912303Scarlsonj target_zone = newtarget; 56922303Scarlsonj } 56932303Scarlsonj 56940Sstevel@tonic-gate if (target_zone != NULL && zone_get_id(target_zone, &zid) != 0) { 56950Sstevel@tonic-gate errno = Z_NO_ZONE; 56960Sstevel@tonic-gate zperror(target_zone, B_TRUE); 56970Sstevel@tonic-gate exit(Z_ERR); 56980Sstevel@tonic-gate } 56992712Snn35248 57002712Snn35248 /* 57012712Snn35248 * See if we have inherited the right to manipulate this zone from 57022712Snn35248 * a zoneadm instance in our ancestry. If so, set zone_lock_cnt to 57032712Snn35248 * indicate it. If not, make that explicit in our environment. 57042712Snn35248 */ 57052712Snn35248 zone_lock_env = getenv(LOCK_ENV_VAR); 57062712Snn35248 if (zone_lock_env == NULL) { 57072712Snn35248 if (putenv(zoneadm_lock_not_held) != 0) { 57082712Snn35248 zperror(target_zone, B_TRUE); 57092712Snn35248 exit(Z_ERR); 57102712Snn35248 } 57112712Snn35248 } else { 57122712Snn35248 zoneadm_is_nested = B_TRUE; 57132712Snn35248 if (atoi(zone_lock_env) == 1) 57142712Snn35248 zone_lock_cnt = 1; 57152712Snn35248 } 57162712Snn35248 57172712Snn35248 /* 57182712Snn35248 * If we are going to be operating on a single zone, retrieve its 57192712Snn35248 * brand type and determine whether it is native or not. 57202712Snn35248 */ 57212712Snn35248 if ((target_zone != NULL) && 57222712Snn35248 (strcmp(target_zone, GLOBAL_ZONENAME) != NULL)) { 57232712Snn35248 if (zone_get_brand(target_zone, target_brand, 57242712Snn35248 sizeof (target_brand)) != Z_OK) { 57252712Snn35248 zerror(gettext("missing or invalid brand")); 57262712Snn35248 exit(Z_ERR); 57272712Snn35248 } 57282712Snn35248 is_native_zone = (strcmp(target_brand, NATIVE_BRAND_NAME) == 0); 57294350Std153743 is_cluster_zone = 57304350Std153743 (strcmp(target_brand, CLUSTER_BRAND_NAME) == 0); 57312712Snn35248 } 57322712Snn35248 57332712Snn35248 err = parse_and_run(argc - optind, &argv[optind]); 57342712Snn35248 57352712Snn35248 return (err); 57360Sstevel@tonic-gate } 5737