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> 690Sstevel@tonic-gate 700Sstevel@tonic-gate #include <fcntl.h> 710Sstevel@tonic-gate #include <door.h> 720Sstevel@tonic-gate #include <macros.h> 730Sstevel@tonic-gate #include <libgen.h> 741300Sgjelinek #include <fnmatch.h> 751931Sgjelinek #include <sys/modctl.h> 762712Snn35248 #include <libbrand.h> 773247Sgjelinek #include <libscf.h> 783352Sgjelinek #include <procfs.h> 790Sstevel@tonic-gate 800Sstevel@tonic-gate #include <pool.h> 810Sstevel@tonic-gate #include <sys/pool.h> 823247Sgjelinek #include <sys/priocntl.h> 833247Sgjelinek #include <sys/fsspriocntl.h> 840Sstevel@tonic-gate 851867Sgjelinek #include "zoneadm.h" 861867Sgjelinek 870Sstevel@tonic-gate #define MAXARGS 8 880Sstevel@tonic-gate 890Sstevel@tonic-gate /* Reflects kernel zone entries */ 900Sstevel@tonic-gate typedef struct zone_entry { 910Sstevel@tonic-gate zoneid_t zid; 920Sstevel@tonic-gate char zname[ZONENAME_MAX]; 930Sstevel@tonic-gate char *zstate_str; 940Sstevel@tonic-gate zone_state_t zstate_num; 952712Snn35248 char zbrand[MAXNAMELEN]; 960Sstevel@tonic-gate char zroot[MAXPATHLEN]; 972303Scarlsonj char zuuid[UUID_PRINTABLE_STRING_LENGTH]; 98*3448Sdh155122 zone_iptype_t ziptype; 990Sstevel@tonic-gate } zone_entry_t; 1000Sstevel@tonic-gate 1010Sstevel@tonic-gate static zone_entry_t *zents; 1020Sstevel@tonic-gate static size_t nzents; 1032712Snn35248 static boolean_t is_native_zone = B_TRUE; 1040Sstevel@tonic-gate 1051915Sgjelinek #define LOOPBACK_IF "lo0" 1061915Sgjelinek #define SOCKET_AF(af) (((af) == AF_UNSPEC) ? AF_INET : (af)) 1071915Sgjelinek 1081915Sgjelinek struct net_if { 1091915Sgjelinek char *name; 1101915Sgjelinek int af; 1111915Sgjelinek }; 1121915Sgjelinek 1130Sstevel@tonic-gate /* 0755 is the default directory mode. */ 1140Sstevel@tonic-gate #define DEFAULT_DIR_MODE \ 1150Sstevel@tonic-gate (S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) 1160Sstevel@tonic-gate 1170Sstevel@tonic-gate struct cmd { 1180Sstevel@tonic-gate uint_t cmd_num; /* command number */ 1190Sstevel@tonic-gate char *cmd_name; /* command name */ 1200Sstevel@tonic-gate char *short_usage; /* short form help */ 1210Sstevel@tonic-gate int (*handler)(int argc, char *argv[]); /* function to call */ 1220Sstevel@tonic-gate 1230Sstevel@tonic-gate }; 1240Sstevel@tonic-gate 1250Sstevel@tonic-gate #define SHELP_HELP "help" 1262267Sdp #define SHELP_BOOT "boot [-- boot_arguments]" 1270Sstevel@tonic-gate #define SHELP_HALT "halt" 1280Sstevel@tonic-gate #define SHELP_READY "ready" 1292267Sdp #define SHELP_REBOOT "reboot [-- boot_arguments]" 1300Sstevel@tonic-gate #define SHELP_LIST "list [-cipv]" 1310Sstevel@tonic-gate #define SHELP_VERIFY "verify" 1322712Snn35248 #define SHELP_INSTALL "install [-x nodataset] [brand-specific args]" 1330Sstevel@tonic-gate #define SHELP_UNINSTALL "uninstall [-F]" 1341867Sgjelinek #define SHELP_CLONE "clone [-m method] [-s <ZFS snapshot>] zonename" 1351300Sgjelinek #define SHELP_MOVE "move zonepath" 1362078Sgjelinek #define SHELP_DETACH "detach [-n]" 1372078Sgjelinek #define SHELP_ATTACH "attach [-F] [-n <path>]" 1382303Scarlsonj #define SHELP_MARK "mark incomplete" 1390Sstevel@tonic-gate 1402712Snn35248 #define EXEC_PREFIX "exec " 1412712Snn35248 #define EXEC_LEN (strlen(EXEC_PREFIX)) 1422712Snn35248 #define RMCOMMAND "/usr/bin/rm -rf" 1432712Snn35248 1442712Snn35248 static int cleanup_zonepath(char *, boolean_t); 1452712Snn35248 146*3448Sdh155122 extern int ifname_open(char *); 147*3448Sdh155122 1480Sstevel@tonic-gate static int help_func(int argc, char *argv[]); 1490Sstevel@tonic-gate static int ready_func(int argc, char *argv[]); 1500Sstevel@tonic-gate static int boot_func(int argc, char *argv[]); 1510Sstevel@tonic-gate static int halt_func(int argc, char *argv[]); 1520Sstevel@tonic-gate static int reboot_func(int argc, char *argv[]); 1530Sstevel@tonic-gate static int list_func(int argc, char *argv[]); 1540Sstevel@tonic-gate static int verify_func(int argc, char *argv[]); 1550Sstevel@tonic-gate static int install_func(int argc, char *argv[]); 1560Sstevel@tonic-gate static int uninstall_func(int argc, char *argv[]); 157766Scarlsonj static int mount_func(int argc, char *argv[]); 158766Scarlsonj static int unmount_func(int argc, char *argv[]); 1591300Sgjelinek static int clone_func(int argc, char *argv[]); 1601300Sgjelinek static int move_func(int argc, char *argv[]); 1611507Sgjelinek static int detach_func(int argc, char *argv[]); 1621507Sgjelinek static int attach_func(int argc, char *argv[]); 1632303Scarlsonj static int mark_func(int argc, char *argv[]); 1643247Sgjelinek static int apply_func(int argc, char *argv[]); 1650Sstevel@tonic-gate static int sanity_check(char *zone, int cmd_num, boolean_t running, 1662712Snn35248 boolean_t unsafe_when_running, boolean_t force); 1670Sstevel@tonic-gate static int cmd_match(char *cmd); 1683339Szt129084 static int verify_details(int, char *argv[]); 1693339Szt129084 static int verify_brand(zone_dochandle_t, int, char *argv[]); 1703339Szt129084 static int invoke_brand_handler(int, char *argv[]); 1710Sstevel@tonic-gate 1720Sstevel@tonic-gate static struct cmd cmdtab[] = { 1730Sstevel@tonic-gate { CMD_HELP, "help", SHELP_HELP, help_func }, 1740Sstevel@tonic-gate { CMD_BOOT, "boot", SHELP_BOOT, boot_func }, 1750Sstevel@tonic-gate { CMD_HALT, "halt", SHELP_HALT, halt_func }, 1760Sstevel@tonic-gate { CMD_READY, "ready", SHELP_READY, ready_func }, 1770Sstevel@tonic-gate { CMD_REBOOT, "reboot", SHELP_REBOOT, reboot_func }, 1780Sstevel@tonic-gate { CMD_LIST, "list", SHELP_LIST, list_func }, 1790Sstevel@tonic-gate { CMD_VERIFY, "verify", SHELP_VERIFY, verify_func }, 1800Sstevel@tonic-gate { CMD_INSTALL, "install", SHELP_INSTALL, install_func }, 1810Sstevel@tonic-gate { CMD_UNINSTALL, "uninstall", SHELP_UNINSTALL, 182766Scarlsonj uninstall_func }, 1831300Sgjelinek /* mount and unmount are private commands for admin/install */ 184766Scarlsonj { CMD_MOUNT, "mount", NULL, mount_func }, 1851300Sgjelinek { CMD_UNMOUNT, "unmount", NULL, unmount_func }, 1861300Sgjelinek { CMD_CLONE, "clone", SHELP_CLONE, clone_func }, 1871507Sgjelinek { CMD_MOVE, "move", SHELP_MOVE, move_func }, 1881507Sgjelinek { CMD_DETACH, "detach", SHELP_DETACH, detach_func }, 1892303Scarlsonj { CMD_ATTACH, "attach", SHELP_ATTACH, attach_func }, 1903247Sgjelinek { CMD_MARK, "mark", SHELP_MARK, mark_func }, 1913247Sgjelinek { CMD_APPLY, "apply", NULL, apply_func } 1920Sstevel@tonic-gate }; 1930Sstevel@tonic-gate 1940Sstevel@tonic-gate /* global variables */ 1950Sstevel@tonic-gate 1960Sstevel@tonic-gate /* set early in main(), never modified thereafter, used all over the place */ 1970Sstevel@tonic-gate static char *execname; 1982712Snn35248 static char target_brand[MAXNAMELEN]; 1990Sstevel@tonic-gate static char *locale; 2001867Sgjelinek char *target_zone; 2012303Scarlsonj static char *target_uuid; 2020Sstevel@tonic-gate 2030Sstevel@tonic-gate /* used in do_subproc() and signal handler */ 2040Sstevel@tonic-gate static volatile boolean_t child_killed; 2052712Snn35248 static int do_subproc_cnt = 0; 2062712Snn35248 2072712Snn35248 /* 2082712Snn35248 * Used to indicate whether this zoneadm instance has another zoneadm 2092712Snn35248 * instance in its ancestry. 2102712Snn35248 */ 2112712Snn35248 static boolean_t zoneadm_is_nested = B_FALSE; 2122712Snn35248 2132712Snn35248 /* used to track nested zone-lock operations */ 2142712Snn35248 static int zone_lock_cnt = 0; 2152712Snn35248 2162712Snn35248 /* used to communicate lock status to children */ 2172712Snn35248 #define LOCK_ENV_VAR "_ZONEADM_LOCK_HELD" 2182712Snn35248 static char zoneadm_lock_held[] = LOCK_ENV_VAR"=1"; 2192712Snn35248 static char zoneadm_lock_not_held[] = LOCK_ENV_VAR"=0"; 2200Sstevel@tonic-gate 2211867Sgjelinek char * 2220Sstevel@tonic-gate cmd_to_str(int cmd_num) 2230Sstevel@tonic-gate { 2240Sstevel@tonic-gate assert(cmd_num >= CMD_MIN && cmd_num <= CMD_MAX); 2250Sstevel@tonic-gate return (cmdtab[cmd_num].cmd_name); 2260Sstevel@tonic-gate } 2270Sstevel@tonic-gate 2280Sstevel@tonic-gate /* This is a separate function because of gettext() wrapping. */ 2290Sstevel@tonic-gate static char * 2300Sstevel@tonic-gate long_help(int cmd_num) 2310Sstevel@tonic-gate { 232222Scomay assert(cmd_num >= CMD_MIN && cmd_num <= CMD_MAX); 2330Sstevel@tonic-gate switch (cmd_num) { 2341634Sgjelinek case CMD_HELP: 2351634Sgjelinek return (gettext("Print usage message.")); 2361634Sgjelinek case CMD_BOOT: 2372267Sdp return (gettext("Activates (boots) specified zone. See " 2382267Sdp "zoneadm(1m) for valid boot\n\targuments.")); 2391634Sgjelinek case CMD_HALT: 2401634Sgjelinek return (gettext("Halts specified zone, bypassing shutdown " 2411634Sgjelinek "scripts and removing runtime\n\tresources of the zone.")); 2421634Sgjelinek case CMD_READY: 2431634Sgjelinek return (gettext("Prepares a zone for running applications but " 2441634Sgjelinek "does not start any user\n\tprocesses in the zone.")); 2451634Sgjelinek case CMD_REBOOT: 2461634Sgjelinek return (gettext("Restarts the zone (equivalent to a halt / " 2472267Sdp "boot sequence).\n\tFails if the zone is not active. " 2482267Sdp "See zoneadm(1m) for valid boot\n\targuments.")); 2491634Sgjelinek case CMD_LIST: 2501634Sgjelinek return (gettext("Lists the current zones, or a " 2511634Sgjelinek "specific zone if indicated. By default,\n\tall " 2521634Sgjelinek "running zones are listed, though this can be " 2531634Sgjelinek "expanded to all\n\tinstalled zones with the -i " 2541634Sgjelinek "option or all configured zones with the\n\t-c " 2552303Scarlsonj "option. When used with the general -z <zone> and/or -u " 2562303Scarlsonj "<uuid-match>\n\toptions, lists only the specified " 2572303Scarlsonj "matching zone, but lists it\n\tregardless of its state, " 2582303Scarlsonj "and the -i and -c options are disallowed. The\n\t-v " 2592303Scarlsonj "option can be used to display verbose information: zone " 2602303Scarlsonj "name, id,\n\tcurrent state, root directory and options. " 2612303Scarlsonj "The -p option can be used\n\tto request machine-parsable " 2622303Scarlsonj "output. The -v and -p options are mutually\n\texclusive." 2632303Scarlsonj " If neither -v nor -p is used, just the zone name is " 2642303Scarlsonj "listed.")); 2651634Sgjelinek case CMD_VERIFY: 2661634Sgjelinek return (gettext("Check to make sure the configuration " 2671634Sgjelinek "can safely be instantiated\n\ton the machine: " 2681634Sgjelinek "physical network interfaces exist, etc.")); 2691634Sgjelinek case CMD_INSTALL: 2701867Sgjelinek return (gettext("Install the configuration on to the system. " 2711867Sgjelinek "The -x nodataset option\n\tcan be used to prevent the " 2721867Sgjelinek "creation of a new ZFS file system for the\n\tzone " 2732712Snn35248 "(assuming the zonepath is within a ZFS file system).\n\t" 2742712Snn35248 "All other arguments are passed to the brand installation " 2752712Snn35248 "function;\n\tsee brand(4) for more information.")); 2761634Sgjelinek case CMD_UNINSTALL: 2771634Sgjelinek return (gettext("Uninstall the configuration from the system. " 2781634Sgjelinek "The -F flag can be used\n\tto force the action.")); 2791634Sgjelinek case CMD_CLONE: 2801867Sgjelinek return (gettext("Clone the installation of another zone. " 2811867Sgjelinek "The -m option can be used to\n\tspecify 'copy' which " 2821867Sgjelinek "forces a copy of the source zone. The -s option\n\t" 2831867Sgjelinek "can be used to specify the name of a ZFS snapshot " 2841867Sgjelinek "that was taken from\n\ta previous clone command. The " 2851867Sgjelinek "snapshot will be used as the source\n\tinstead of " 2861867Sgjelinek "creating a new ZFS snapshot.")); 2871634Sgjelinek case CMD_MOVE: 2881634Sgjelinek return (gettext("Move the zone to a new zonepath.")); 2891634Sgjelinek case CMD_DETACH: 2901634Sgjelinek return (gettext("Detach the zone from the system. The zone " 2911634Sgjelinek "state is changed to\n\t'configured' (but the files under " 2921634Sgjelinek "the zonepath are untouched).\n\tThe zone can subsequently " 2931634Sgjelinek "be attached, or can be moved to another\n\tsystem and " 2942078Sgjelinek "attached there. The -n option can be used to specify\n\t" 2952078Sgjelinek "'no-execute' mode. When -n is used, the information " 2962078Sgjelinek "needed to attach\n\tthe zone is sent to standard output " 2972078Sgjelinek "but the zone is not actually\n\tdetached.")); 2981634Sgjelinek case CMD_ATTACH: 2991634Sgjelinek return (gettext("Attach the zone to the system. The zone " 3001634Sgjelinek "state must be 'configured'\n\tprior to attach; upon " 3011634Sgjelinek "successful completion, the zone state will be\n\t" 3021634Sgjelinek "'installed'. The system software on the current " 3031634Sgjelinek "system must be\n\tcompatible with the software on the " 3041634Sgjelinek "zone's original system.\n\tSpecify -F to force the attach " 3052078Sgjelinek "and skip software compatibility tests.\n\tThe -n option " 3062078Sgjelinek "can be used to specify 'no-execute' mode. When -n is\n\t" 3072078Sgjelinek "used, the information needed to attach the zone is read " 3082078Sgjelinek "from the\n\tspecified path and the configuration is only " 3092078Sgjelinek "validated. The path can\n\tbe '-' to specify standard " 3102078Sgjelinek "input.")); 3112303Scarlsonj case CMD_MARK: 3122303Scarlsonj return (gettext("Set the state of the zone. This can be used " 3132303Scarlsonj "to force the zone\n\tstate to 'incomplete' " 3142303Scarlsonj "administratively if some activity has rendered\n\tthe " 3152303Scarlsonj "zone permanently unusable. The only valid state that " 3162303Scarlsonj "may be\n\tspecified is 'incomplete'.")); 3171634Sgjelinek default: 3181634Sgjelinek return (""); 3190Sstevel@tonic-gate } 3200Sstevel@tonic-gate /* NOTREACHED */ 321222Scomay return (NULL); 3220Sstevel@tonic-gate } 3230Sstevel@tonic-gate 3240Sstevel@tonic-gate /* 3250Sstevel@tonic-gate * Called with explicit B_TRUE when help is explicitly requested, B_FALSE for 3260Sstevel@tonic-gate * unexpected errors. 3270Sstevel@tonic-gate */ 3280Sstevel@tonic-gate 3290Sstevel@tonic-gate static int 3300Sstevel@tonic-gate usage(boolean_t explicit) 3310Sstevel@tonic-gate { 3320Sstevel@tonic-gate int i; 3330Sstevel@tonic-gate FILE *fd = explicit ? stdout : stderr; 3340Sstevel@tonic-gate 3350Sstevel@tonic-gate (void) fprintf(fd, "%s:\t%s help\n", gettext("usage"), execname); 3362303Scarlsonj (void) fprintf(fd, "\t%s [-z <zone>] [-u <uuid-match>] list\n", 3372303Scarlsonj execname); 3382303Scarlsonj (void) fprintf(fd, "\t%s {-z <zone>|-u <uuid-match>} <%s>\n", execname, 3390Sstevel@tonic-gate gettext("subcommand")); 3400Sstevel@tonic-gate (void) fprintf(fd, "\n%s:\n\n", gettext("Subcommands")); 3410Sstevel@tonic-gate for (i = CMD_MIN; i <= CMD_MAX; i++) { 342766Scarlsonj if (cmdtab[i].short_usage == NULL) 343766Scarlsonj continue; 3440Sstevel@tonic-gate (void) fprintf(fd, "%s\n", cmdtab[i].short_usage); 3450Sstevel@tonic-gate if (explicit) 3460Sstevel@tonic-gate (void) fprintf(fd, "\t%s\n\n", long_help(i)); 3470Sstevel@tonic-gate } 3480Sstevel@tonic-gate if (!explicit) 3490Sstevel@tonic-gate (void) fputs("\n", fd); 3500Sstevel@tonic-gate return (Z_USAGE); 3510Sstevel@tonic-gate } 3520Sstevel@tonic-gate 3530Sstevel@tonic-gate static void 3540Sstevel@tonic-gate sub_usage(char *short_usage, int cmd_num) 3550Sstevel@tonic-gate { 3560Sstevel@tonic-gate (void) fprintf(stderr, "%s:\t%s\n", gettext("usage"), short_usage); 3570Sstevel@tonic-gate (void) fprintf(stderr, "\t%s\n", long_help(cmd_num)); 3580Sstevel@tonic-gate } 3590Sstevel@tonic-gate 3600Sstevel@tonic-gate /* 3610Sstevel@tonic-gate * zperror() is like perror(3c) except that this also prints the executable 3620Sstevel@tonic-gate * name at the start of the message, and takes a boolean indicating whether 3630Sstevel@tonic-gate * to call libc'c strerror() or that from libzonecfg. 3640Sstevel@tonic-gate */ 3650Sstevel@tonic-gate 3661867Sgjelinek void 3670Sstevel@tonic-gate zperror(const char *str, boolean_t zonecfg_error) 3680Sstevel@tonic-gate { 3690Sstevel@tonic-gate (void) fprintf(stderr, "%s: %s: %s\n", execname, str, 3700Sstevel@tonic-gate zonecfg_error ? zonecfg_strerror(errno) : strerror(errno)); 3710Sstevel@tonic-gate } 3720Sstevel@tonic-gate 3730Sstevel@tonic-gate /* 3740Sstevel@tonic-gate * zperror2() is very similar to zperror() above, except it also prints a 3750Sstevel@tonic-gate * supplied zone name after the executable. 3760Sstevel@tonic-gate * 3770Sstevel@tonic-gate * All current consumers of this function want libzonecfg's strerror() rather 3780Sstevel@tonic-gate * than libc's; if this ever changes, this function can be made more generic 3790Sstevel@tonic-gate * like zperror() above. 3800Sstevel@tonic-gate */ 3810Sstevel@tonic-gate 3821867Sgjelinek void 3830Sstevel@tonic-gate zperror2(const char *zone, const char *str) 3840Sstevel@tonic-gate { 3850Sstevel@tonic-gate (void) fprintf(stderr, "%s: %s: %s: %s\n", execname, zone, str, 3860Sstevel@tonic-gate zonecfg_strerror(errno)); 3870Sstevel@tonic-gate } 3880Sstevel@tonic-gate 3890Sstevel@tonic-gate /* PRINTFLIKE1 */ 3901867Sgjelinek void 3910Sstevel@tonic-gate zerror(const char *fmt, ...) 3920Sstevel@tonic-gate { 3930Sstevel@tonic-gate va_list alist; 3940Sstevel@tonic-gate 3950Sstevel@tonic-gate va_start(alist, fmt); 3960Sstevel@tonic-gate (void) fprintf(stderr, "%s: ", execname); 3970Sstevel@tonic-gate if (target_zone != NULL) 3980Sstevel@tonic-gate (void) fprintf(stderr, "zone '%s': ", target_zone); 3990Sstevel@tonic-gate (void) vfprintf(stderr, fmt, alist); 4000Sstevel@tonic-gate (void) fprintf(stderr, "\n"); 4010Sstevel@tonic-gate va_end(alist); 4020Sstevel@tonic-gate } 4030Sstevel@tonic-gate 4040Sstevel@tonic-gate static void * 4050Sstevel@tonic-gate safe_calloc(size_t nelem, size_t elsize) 4060Sstevel@tonic-gate { 4070Sstevel@tonic-gate void *r = calloc(nelem, elsize); 4080Sstevel@tonic-gate 4090Sstevel@tonic-gate if (r == NULL) { 4100Sstevel@tonic-gate zerror(gettext("failed to allocate %lu bytes: %s"), 4110Sstevel@tonic-gate (ulong_t)nelem * elsize, strerror(errno)); 4120Sstevel@tonic-gate exit(Z_ERR); 4130Sstevel@tonic-gate } 4140Sstevel@tonic-gate return (r); 4150Sstevel@tonic-gate } 4160Sstevel@tonic-gate 4170Sstevel@tonic-gate static void 4180Sstevel@tonic-gate zone_print(zone_entry_t *zent, boolean_t verbose, boolean_t parsable) 4190Sstevel@tonic-gate { 4200Sstevel@tonic-gate static boolean_t firsttime = B_TRUE; 421*3448Sdh155122 char *ip_type_str; 422*3448Sdh155122 423*3448Sdh155122 if (zent->ziptype == ZS_EXCLUSIVE) 424*3448Sdh155122 ip_type_str = "excl"; 425*3448Sdh155122 else 426*3448Sdh155122 ip_type_str = "shared"; 4270Sstevel@tonic-gate 4280Sstevel@tonic-gate assert(!(verbose && parsable)); 4290Sstevel@tonic-gate if (firsttime && verbose) { 4300Sstevel@tonic-gate firsttime = B_FALSE; 431*3448Sdh155122 (void) printf("%*s %-16s %-10s %-30s %-8s %-6s\n", 432*3448Sdh155122 ZONEID_WIDTH, "ID", "NAME", "STATUS", "PATH", "BRAND", 433*3448Sdh155122 "IP"); 4340Sstevel@tonic-gate } 4350Sstevel@tonic-gate if (!verbose) { 4362303Scarlsonj char *cp, *clim; 4372303Scarlsonj 4380Sstevel@tonic-gate if (!parsable) { 4390Sstevel@tonic-gate (void) printf("%s\n", zent->zname); 4400Sstevel@tonic-gate return; 4410Sstevel@tonic-gate } 4420Sstevel@tonic-gate if (zent->zid == ZONE_ID_UNDEFINED) 4430Sstevel@tonic-gate (void) printf("-"); 4440Sstevel@tonic-gate else 4450Sstevel@tonic-gate (void) printf("%lu", zent->zid); 4462303Scarlsonj (void) printf(":%s:%s:", zent->zname, zent->zstate_str); 4472303Scarlsonj cp = zent->zroot; 4482303Scarlsonj while ((clim = strchr(cp, ':')) != NULL) { 4492303Scarlsonj (void) printf("%.*s\\:", clim - cp, cp); 4502303Scarlsonj cp = clim + 1; 4512303Scarlsonj } 452*3448Sdh155122 (void) printf("%s:%s:%s:%s\n", cp, zent->zuuid, zent->zbrand, 453*3448Sdh155122 ip_type_str); 4540Sstevel@tonic-gate return; 4550Sstevel@tonic-gate } 4560Sstevel@tonic-gate if (zent->zstate_str != NULL) { 4570Sstevel@tonic-gate if (zent->zid == ZONE_ID_UNDEFINED) 4580Sstevel@tonic-gate (void) printf("%*s", ZONEID_WIDTH, "-"); 4590Sstevel@tonic-gate else 4600Sstevel@tonic-gate (void) printf("%*lu", ZONEID_WIDTH, zent->zid); 461*3448Sdh155122 (void) printf(" %-16s %-10s %-30s %-8s %-6s\n", zent->zname, 462*3448Sdh155122 zent->zstate_str, zent->zroot, zent->zbrand, ip_type_str); 4630Sstevel@tonic-gate } 4640Sstevel@tonic-gate } 4650Sstevel@tonic-gate 4660Sstevel@tonic-gate static int 467766Scarlsonj lookup_zone_info(const char *zone_name, zoneid_t zid, zone_entry_t *zent) 4680Sstevel@tonic-gate { 4691676Sjpk char root[MAXPATHLEN], *cp; 4700Sstevel@tonic-gate int err; 4712303Scarlsonj uuid_t uuid; 4720Sstevel@tonic-gate 4730Sstevel@tonic-gate (void) strlcpy(zent->zname, zone_name, sizeof (zent->zname)); 4740Sstevel@tonic-gate (void) strlcpy(zent->zroot, "???", sizeof (zent->zroot)); 4752712Snn35248 (void) strlcpy(zent->zbrand, "???", sizeof (zent->zbrand)); 4760Sstevel@tonic-gate zent->zstate_str = "???"; 4770Sstevel@tonic-gate 478766Scarlsonj zent->zid = zid; 4790Sstevel@tonic-gate 4802303Scarlsonj if (zonecfg_get_uuid(zone_name, uuid) == Z_OK && 4812303Scarlsonj !uuid_is_null(uuid)) 4822303Scarlsonj uuid_unparse(uuid, zent->zuuid); 4832303Scarlsonj else 4842303Scarlsonj zent->zuuid[0] = '\0'; 4852303Scarlsonj 4861676Sjpk /* 4871676Sjpk * For labeled zones which query the zone path of lower-level 4881676Sjpk * zones, the path needs to be adjusted to drop the final 4891676Sjpk * "/root" component. This adjusted path is then useful 4901676Sjpk * for reading down any exported directories from the 4911676Sjpk * lower-level zone. 4921676Sjpk */ 4931676Sjpk if (is_system_labeled() && zent->zid != ZONE_ID_UNDEFINED) { 4941676Sjpk if (zone_getattr(zent->zid, ZONE_ATTR_ROOT, zent->zroot, 4951676Sjpk sizeof (zent->zroot)) == -1) { 4961676Sjpk zperror2(zent->zname, 4971676Sjpk gettext("could not get zone path.")); 4981676Sjpk return (Z_ERR); 4991676Sjpk } 5001676Sjpk cp = zent->zroot + strlen(zent->zroot) - 5; 5011676Sjpk if (cp > zent->zroot && strcmp(cp, "/root") == 0) 5021676Sjpk *cp = 0; 5031676Sjpk } else { 5041676Sjpk if ((err = zone_get_zonepath(zent->zname, root, 5051676Sjpk sizeof (root))) != Z_OK) { 5061676Sjpk errno = err; 5071676Sjpk zperror2(zent->zname, 5081676Sjpk gettext("could not get zone path.")); 5091676Sjpk return (Z_ERR); 5101676Sjpk } 5111676Sjpk (void) strlcpy(zent->zroot, root, sizeof (zent->zroot)); 5121676Sjpk } 5130Sstevel@tonic-gate 5140Sstevel@tonic-gate if ((err = zone_get_state(zent->zname, &zent->zstate_num)) != Z_OK) { 5150Sstevel@tonic-gate errno = err; 5160Sstevel@tonic-gate zperror2(zent->zname, gettext("could not get state")); 5170Sstevel@tonic-gate return (Z_ERR); 5180Sstevel@tonic-gate } 5190Sstevel@tonic-gate zent->zstate_str = zone_state_str(zent->zstate_num); 5203009Snn35248 5213009Snn35248 /* 5223009Snn35248 * A zone's brand is only available in the .xml file describing it, 5233009Snn35248 * which is only visible to the global zone. This causes 5243009Snn35248 * zone_get_brand() to fail when called from within a non-global 5253009Snn35248 * zone. Fortunately we only do this on labeled systems, where we 5263009Snn35248 * know all zones are native. 5273009Snn35248 */ 5283009Snn35248 if (getzoneid() != GLOBAL_ZONEID) { 5293009Snn35248 assert(is_system_labeled() != 0); 5303009Snn35248 (void) strlcpy(zent->zbrand, NATIVE_BRAND_NAME, 5313009Snn35248 sizeof (zent->zbrand)); 5323009Snn35248 } else if (zone_get_brand(zent->zname, zent->zbrand, 5332712Snn35248 sizeof (zent->zbrand)) != Z_OK) { 5342712Snn35248 zperror2(zent->zname, gettext("could not get brand name")); 5352712Snn35248 return (Z_ERR); 5362712Snn35248 } 5370Sstevel@tonic-gate 538*3448Sdh155122 /* 539*3448Sdh155122 * Get ip type of the zone. 540*3448Sdh155122 * Note for global zone, ZS_SHARED is set always. 541*3448Sdh155122 */ 542*3448Sdh155122 if (zid == GLOBAL_ZONEID) { 543*3448Sdh155122 zent->ziptype = ZS_SHARED; 544*3448Sdh155122 } else { 545*3448Sdh155122 546*3448Sdh155122 if (zent->zstate_num == ZONE_STATE_RUNNING) { 547*3448Sdh155122 ushort_t flags; 548*3448Sdh155122 549*3448Sdh155122 if (zone_getattr(zid, ZONE_ATTR_FLAGS, &flags, 550*3448Sdh155122 sizeof (flags)) < 0) { 551*3448Sdh155122 zperror2(zent->zname, 552*3448Sdh155122 gettext("could not get zone flags")); 553*3448Sdh155122 return (Z_ERR); 554*3448Sdh155122 } 555*3448Sdh155122 if (flags & ZF_NET_EXCL) 556*3448Sdh155122 zent->ziptype = ZS_EXCLUSIVE; 557*3448Sdh155122 else 558*3448Sdh155122 zent->ziptype = ZS_SHARED; 559*3448Sdh155122 } else { 560*3448Sdh155122 zone_dochandle_t handle; 561*3448Sdh155122 562*3448Sdh155122 if ((handle = zonecfg_init_handle()) == NULL) { 563*3448Sdh155122 zperror2(zent->zname, 564*3448Sdh155122 gettext("could not init handle")); 565*3448Sdh155122 return (Z_ERR); 566*3448Sdh155122 } 567*3448Sdh155122 if ((err = zonecfg_get_handle(zent->zname, handle)) 568*3448Sdh155122 != Z_OK) { 569*3448Sdh155122 zperror2(zent->zname, 570*3448Sdh155122 gettext("could not get handle")); 571*3448Sdh155122 zonecfg_fini_handle(handle); 572*3448Sdh155122 return (Z_ERR); 573*3448Sdh155122 } 574*3448Sdh155122 575*3448Sdh155122 if ((err = zonecfg_get_iptype(handle, &zent->ziptype)) 576*3448Sdh155122 != Z_OK) { 577*3448Sdh155122 zperror2(zent->zname, 578*3448Sdh155122 gettext("could not get ip-type")); 579*3448Sdh155122 zonecfg_fini_handle(handle); 580*3448Sdh155122 return (Z_ERR); 581*3448Sdh155122 } 582*3448Sdh155122 zonecfg_fini_handle(handle); 583*3448Sdh155122 } 584*3448Sdh155122 } 585*3448Sdh155122 5860Sstevel@tonic-gate return (Z_OK); 5870Sstevel@tonic-gate } 5880Sstevel@tonic-gate 5890Sstevel@tonic-gate /* 5900Sstevel@tonic-gate * fetch_zents() calls zone_list(2) to find out how many zones are running 5910Sstevel@tonic-gate * (which is stored in the global nzents), then calls zone_list(2) again 5920Sstevel@tonic-gate * to fetch the list of running zones (stored in the global zents). This 5930Sstevel@tonic-gate * function may be called multiple times, so if zents is already set, we 5940Sstevel@tonic-gate * return immediately to save work. 5950Sstevel@tonic-gate */ 5960Sstevel@tonic-gate 5970Sstevel@tonic-gate static int 598766Scarlsonj fetch_zents(void) 5990Sstevel@tonic-gate { 6000Sstevel@tonic-gate zoneid_t *zids = NULL; 6010Sstevel@tonic-gate uint_t nzents_saved; 602766Scarlsonj int i, retv; 603766Scarlsonj FILE *fp; 604766Scarlsonj boolean_t inaltroot; 605766Scarlsonj zone_entry_t *zentp; 6060Sstevel@tonic-gate 6070Sstevel@tonic-gate if (nzents > 0) 6080Sstevel@tonic-gate return (Z_OK); 6090Sstevel@tonic-gate 6100Sstevel@tonic-gate if (zone_list(NULL, &nzents) != 0) { 6110Sstevel@tonic-gate zperror(gettext("failed to get zoneid list"), B_FALSE); 6120Sstevel@tonic-gate return (Z_ERR); 6130Sstevel@tonic-gate } 6140Sstevel@tonic-gate 6150Sstevel@tonic-gate again: 6160Sstevel@tonic-gate if (nzents == 0) 6170Sstevel@tonic-gate return (Z_OK); 6180Sstevel@tonic-gate 6190Sstevel@tonic-gate zids = safe_calloc(nzents, sizeof (zoneid_t)); 6200Sstevel@tonic-gate nzents_saved = nzents; 6210Sstevel@tonic-gate 6220Sstevel@tonic-gate if (zone_list(zids, &nzents) != 0) { 6230Sstevel@tonic-gate zperror(gettext("failed to get zone list"), B_FALSE); 6240Sstevel@tonic-gate free(zids); 6250Sstevel@tonic-gate return (Z_ERR); 6260Sstevel@tonic-gate } 6270Sstevel@tonic-gate if (nzents != nzents_saved) { 6280Sstevel@tonic-gate /* list changed, try again */ 6290Sstevel@tonic-gate free(zids); 6300Sstevel@tonic-gate goto again; 6310Sstevel@tonic-gate } 6320Sstevel@tonic-gate 6330Sstevel@tonic-gate zents = safe_calloc(nzents, sizeof (zone_entry_t)); 6340Sstevel@tonic-gate 635766Scarlsonj inaltroot = zonecfg_in_alt_root(); 636766Scarlsonj if (inaltroot) 637766Scarlsonj fp = zonecfg_open_scratch("", B_FALSE); 638766Scarlsonj else 639766Scarlsonj fp = NULL; 640766Scarlsonj zentp = zents; 641766Scarlsonj retv = Z_OK; 6420Sstevel@tonic-gate for (i = 0; i < nzents; i++) { 6430Sstevel@tonic-gate char name[ZONENAME_MAX]; 644766Scarlsonj char altname[ZONENAME_MAX]; 6450Sstevel@tonic-gate 646766Scarlsonj if (getzonenamebyid(zids[i], name, sizeof (name)) < 0) { 6470Sstevel@tonic-gate zperror(gettext("failed to get zone name"), B_FALSE); 648766Scarlsonj retv = Z_ERR; 649766Scarlsonj continue; 650766Scarlsonj } 651766Scarlsonj if (zonecfg_is_scratch(name)) { 652766Scarlsonj /* Ignore scratch zones by default */ 653766Scarlsonj if (!inaltroot) 654766Scarlsonj continue; 655766Scarlsonj if (fp == NULL || 656766Scarlsonj zonecfg_reverse_scratch(fp, name, altname, 657766Scarlsonj sizeof (altname), NULL, 0) == -1) { 658924Sgjelinek zerror(gettext("could not resolve scratch " 659766Scarlsonj "zone %s"), name); 660766Scarlsonj retv = Z_ERR; 661766Scarlsonj continue; 662766Scarlsonj } 663766Scarlsonj (void) strcpy(name, altname); 664766Scarlsonj } else { 665766Scarlsonj /* Ignore non-scratch when in an alternate root */ 666766Scarlsonj if (inaltroot && strcmp(name, GLOBAL_ZONENAME) != 0) 667766Scarlsonj continue; 668766Scarlsonj } 669766Scarlsonj if (lookup_zone_info(name, zids[i], zentp) != Z_OK) { 670766Scarlsonj zerror(gettext("failed to get zone data")); 671766Scarlsonj retv = Z_ERR; 672766Scarlsonj continue; 673766Scarlsonj } 674766Scarlsonj zentp++; 6750Sstevel@tonic-gate } 676766Scarlsonj nzents = zentp - zents; 677766Scarlsonj if (fp != NULL) 678766Scarlsonj zonecfg_close_scratch(fp); 6790Sstevel@tonic-gate 6800Sstevel@tonic-gate free(zids); 681766Scarlsonj return (retv); 6820Sstevel@tonic-gate } 6830Sstevel@tonic-gate 684766Scarlsonj static int 6850Sstevel@tonic-gate zone_print_list(zone_state_t min_state, boolean_t verbose, boolean_t parsable) 6860Sstevel@tonic-gate { 6870Sstevel@tonic-gate int i; 6880Sstevel@tonic-gate zone_entry_t zent; 6890Sstevel@tonic-gate FILE *cookie; 6900Sstevel@tonic-gate char *name; 6910Sstevel@tonic-gate 6920Sstevel@tonic-gate /* 6930Sstevel@tonic-gate * First get the list of running zones from the kernel and print them. 6940Sstevel@tonic-gate * If that is all we need, then return. 6950Sstevel@tonic-gate */ 696766Scarlsonj if ((i = fetch_zents()) != Z_OK) { 6970Sstevel@tonic-gate /* 6980Sstevel@tonic-gate * No need for error messages; fetch_zents() has already taken 6990Sstevel@tonic-gate * care of this. 7000Sstevel@tonic-gate */ 701766Scarlsonj return (i); 7020Sstevel@tonic-gate } 703766Scarlsonj for (i = 0; i < nzents; i++) 7040Sstevel@tonic-gate zone_print(&zents[i], verbose, parsable); 7050Sstevel@tonic-gate if (min_state >= ZONE_STATE_RUNNING) 706766Scarlsonj return (Z_OK); 7070Sstevel@tonic-gate /* 7080Sstevel@tonic-gate * Next, get the full list of zones from the configuration, skipping 7090Sstevel@tonic-gate * any we have already printed. 7100Sstevel@tonic-gate */ 7110Sstevel@tonic-gate cookie = setzoneent(); 7120Sstevel@tonic-gate while ((name = getzoneent(cookie)) != NULL) { 7130Sstevel@tonic-gate for (i = 0; i < nzents; i++) { 7140Sstevel@tonic-gate if (strcmp(zents[i].zname, name) == 0) 7150Sstevel@tonic-gate break; 7160Sstevel@tonic-gate } 7170Sstevel@tonic-gate if (i < nzents) { 7180Sstevel@tonic-gate free(name); 7190Sstevel@tonic-gate continue; 7200Sstevel@tonic-gate } 721766Scarlsonj if (lookup_zone_info(name, ZONE_ID_UNDEFINED, &zent) != Z_OK) { 7220Sstevel@tonic-gate free(name); 7230Sstevel@tonic-gate continue; 7240Sstevel@tonic-gate } 7250Sstevel@tonic-gate free(name); 7260Sstevel@tonic-gate if (zent.zstate_num >= min_state) 7270Sstevel@tonic-gate zone_print(&zent, verbose, parsable); 7280Sstevel@tonic-gate } 7290Sstevel@tonic-gate endzoneent(cookie); 730766Scarlsonj return (Z_OK); 7310Sstevel@tonic-gate } 7320Sstevel@tonic-gate 7330Sstevel@tonic-gate static zone_entry_t * 7340Sstevel@tonic-gate lookup_running_zone(char *str) 7350Sstevel@tonic-gate { 7360Sstevel@tonic-gate zoneid_t zoneid; 7370Sstevel@tonic-gate char *cp; 7380Sstevel@tonic-gate int i; 7390Sstevel@tonic-gate 7400Sstevel@tonic-gate if (fetch_zents() != Z_OK) 7410Sstevel@tonic-gate return (NULL); 7420Sstevel@tonic-gate 7430Sstevel@tonic-gate for (i = 0; i < nzents; i++) { 7440Sstevel@tonic-gate if (strcmp(str, zents[i].zname) == 0) 7450Sstevel@tonic-gate return (&zents[i]); 7460Sstevel@tonic-gate } 7470Sstevel@tonic-gate errno = 0; 7480Sstevel@tonic-gate zoneid = strtol(str, &cp, 0); 7490Sstevel@tonic-gate if (zoneid < MIN_ZONEID || zoneid > MAX_ZONEID || 7500Sstevel@tonic-gate errno != 0 || *cp != '\0') 7510Sstevel@tonic-gate return (NULL); 7520Sstevel@tonic-gate for (i = 0; i < nzents; i++) { 7530Sstevel@tonic-gate if (zoneid == zents[i].zid) 7540Sstevel@tonic-gate return (&zents[i]); 7550Sstevel@tonic-gate } 7560Sstevel@tonic-gate return (NULL); 7570Sstevel@tonic-gate } 7580Sstevel@tonic-gate 7590Sstevel@tonic-gate /* 7600Sstevel@tonic-gate * Check a bit in a mode_t: if on is B_TRUE, that bit should be on; if 7610Sstevel@tonic-gate * B_FALSE, it should be off. Return B_TRUE if the mode is bad (incorrect). 7620Sstevel@tonic-gate */ 7630Sstevel@tonic-gate static boolean_t 7640Sstevel@tonic-gate bad_mode_bit(mode_t mode, mode_t bit, boolean_t on, char *file) 7650Sstevel@tonic-gate { 7660Sstevel@tonic-gate char *str; 7670Sstevel@tonic-gate 7680Sstevel@tonic-gate assert(bit == S_IRUSR || bit == S_IWUSR || bit == S_IXUSR || 7690Sstevel@tonic-gate bit == S_IRGRP || bit == S_IWGRP || bit == S_IXGRP || 7700Sstevel@tonic-gate bit == S_IROTH || bit == S_IWOTH || bit == S_IXOTH); 7710Sstevel@tonic-gate /* 7720Sstevel@tonic-gate * TRANSLATION_NOTE 7730Sstevel@tonic-gate * The strings below will be used as part of a larger message, 7740Sstevel@tonic-gate * either: 7750Sstevel@tonic-gate * (file name) must be (owner|group|world) (read|writ|execut)able 7760Sstevel@tonic-gate * or 7770Sstevel@tonic-gate * (file name) must not be (owner|group|world) (read|writ|execut)able 7780Sstevel@tonic-gate */ 7790Sstevel@tonic-gate switch (bit) { 7800Sstevel@tonic-gate case S_IRUSR: 7810Sstevel@tonic-gate str = gettext("owner readable"); 7820Sstevel@tonic-gate break; 7830Sstevel@tonic-gate case S_IWUSR: 7840Sstevel@tonic-gate str = gettext("owner writable"); 7850Sstevel@tonic-gate break; 7860Sstevel@tonic-gate case S_IXUSR: 7870Sstevel@tonic-gate str = gettext("owner executable"); 7880Sstevel@tonic-gate break; 7890Sstevel@tonic-gate case S_IRGRP: 7900Sstevel@tonic-gate str = gettext("group readable"); 7910Sstevel@tonic-gate break; 7920Sstevel@tonic-gate case S_IWGRP: 7930Sstevel@tonic-gate str = gettext("group writable"); 7940Sstevel@tonic-gate break; 7950Sstevel@tonic-gate case S_IXGRP: 7960Sstevel@tonic-gate str = gettext("group executable"); 7970Sstevel@tonic-gate break; 7980Sstevel@tonic-gate case S_IROTH: 7990Sstevel@tonic-gate str = gettext("world readable"); 8000Sstevel@tonic-gate break; 8010Sstevel@tonic-gate case S_IWOTH: 8020Sstevel@tonic-gate str = gettext("world writable"); 8030Sstevel@tonic-gate break; 8040Sstevel@tonic-gate case S_IXOTH: 8050Sstevel@tonic-gate str = gettext("world executable"); 8060Sstevel@tonic-gate break; 8070Sstevel@tonic-gate } 8080Sstevel@tonic-gate if ((mode & bit) == (on ? 0 : bit)) { 8090Sstevel@tonic-gate /* 8100Sstevel@tonic-gate * TRANSLATION_NOTE 8110Sstevel@tonic-gate * The first parameter below is a file name; the second 8120Sstevel@tonic-gate * is one of the "(owner|group|world) (read|writ|execut)able" 8130Sstevel@tonic-gate * strings from above. 8140Sstevel@tonic-gate */ 8150Sstevel@tonic-gate /* 8160Sstevel@tonic-gate * The code below could be simplified but not in a way 8170Sstevel@tonic-gate * that would easily translate to non-English locales. 8180Sstevel@tonic-gate */ 8190Sstevel@tonic-gate if (on) { 8200Sstevel@tonic-gate (void) fprintf(stderr, gettext("%s must be %s.\n"), 8210Sstevel@tonic-gate file, str); 8220Sstevel@tonic-gate } else { 8230Sstevel@tonic-gate (void) fprintf(stderr, gettext("%s must not be %s.\n"), 8240Sstevel@tonic-gate file, str); 8250Sstevel@tonic-gate } 8260Sstevel@tonic-gate return (B_TRUE); 8270Sstevel@tonic-gate } 8280Sstevel@tonic-gate return (B_FALSE); 8290Sstevel@tonic-gate } 8300Sstevel@tonic-gate 8310Sstevel@tonic-gate /* 8320Sstevel@tonic-gate * We want to make sure that no zone has its zone path as a child node 8330Sstevel@tonic-gate * (in the directory sense) of any other. We do that by comparing this 8340Sstevel@tonic-gate * zone's path to the path of all other (non-global) zones. The comparison 8350Sstevel@tonic-gate * in each case is simple: add '/' to the end of the path, then do a 8360Sstevel@tonic-gate * strncmp() of the two paths, using the length of the shorter one. 8370Sstevel@tonic-gate */ 8380Sstevel@tonic-gate 8390Sstevel@tonic-gate static int 8400Sstevel@tonic-gate crosscheck_zonepaths(char *path) 8410Sstevel@tonic-gate { 8420Sstevel@tonic-gate char rpath[MAXPATHLEN]; /* resolved path */ 8430Sstevel@tonic-gate char path_copy[MAXPATHLEN]; /* copy of original path */ 8440Sstevel@tonic-gate char rpath_copy[MAXPATHLEN]; /* copy of original rpath */ 8450Sstevel@tonic-gate struct zoneent *ze; 8460Sstevel@tonic-gate int res, err; 8470Sstevel@tonic-gate FILE *cookie; 8480Sstevel@tonic-gate 8490Sstevel@tonic-gate cookie = setzoneent(); 8500Sstevel@tonic-gate while ((ze = getzoneent_private(cookie)) != NULL) { 8510Sstevel@tonic-gate /* Skip zones which are not installed. */ 8520Sstevel@tonic-gate if (ze->zone_state < ZONE_STATE_INSTALLED) { 8530Sstevel@tonic-gate free(ze); 8540Sstevel@tonic-gate continue; 8550Sstevel@tonic-gate } 8560Sstevel@tonic-gate /* Skip the global zone and the current target zone. */ 8570Sstevel@tonic-gate if (strcmp(ze->zone_name, GLOBAL_ZONENAME) == 0 || 8580Sstevel@tonic-gate strcmp(ze->zone_name, target_zone) == 0) { 8590Sstevel@tonic-gate free(ze); 8600Sstevel@tonic-gate continue; 8610Sstevel@tonic-gate } 8620Sstevel@tonic-gate if (strlen(ze->zone_path) == 0) { 8630Sstevel@tonic-gate /* old index file without path, fall back */ 8640Sstevel@tonic-gate if ((err = zone_get_zonepath(ze->zone_name, 8650Sstevel@tonic-gate ze->zone_path, sizeof (ze->zone_path))) != Z_OK) { 8660Sstevel@tonic-gate errno = err; 8670Sstevel@tonic-gate zperror2(ze->zone_name, 8680Sstevel@tonic-gate gettext("could not get zone path")); 8690Sstevel@tonic-gate free(ze); 8700Sstevel@tonic-gate continue; 8710Sstevel@tonic-gate } 8720Sstevel@tonic-gate } 873766Scarlsonj (void) snprintf(path_copy, sizeof (path_copy), "%s%s", 874766Scarlsonj zonecfg_get_root(), ze->zone_path); 875766Scarlsonj res = resolvepath(path_copy, rpath, sizeof (rpath)); 8760Sstevel@tonic-gate if (res == -1) { 8770Sstevel@tonic-gate if (errno != ENOENT) { 878766Scarlsonj zperror(path_copy, B_FALSE); 8790Sstevel@tonic-gate free(ze); 8800Sstevel@tonic-gate return (Z_ERR); 8810Sstevel@tonic-gate } 8820Sstevel@tonic-gate (void) printf(gettext("WARNING: zone %s is installed, " 8830Sstevel@tonic-gate "but its %s %s does not exist.\n"), ze->zone_name, 884766Scarlsonj "zonepath", path_copy); 8850Sstevel@tonic-gate free(ze); 8860Sstevel@tonic-gate continue; 8870Sstevel@tonic-gate } 8880Sstevel@tonic-gate rpath[res] = '\0'; 8890Sstevel@tonic-gate (void) snprintf(path_copy, sizeof (path_copy), "%s/", path); 8900Sstevel@tonic-gate (void) snprintf(rpath_copy, sizeof (rpath_copy), "%s/", rpath); 8910Sstevel@tonic-gate if (strncmp(path_copy, rpath_copy, 8920Sstevel@tonic-gate min(strlen(path_copy), strlen(rpath_copy))) == 0) { 893924Sgjelinek /* 894924Sgjelinek * TRANSLATION_NOTE 895924Sgjelinek * zonepath is a literal that should not be translated. 896924Sgjelinek */ 8970Sstevel@tonic-gate (void) fprintf(stderr, gettext("%s zonepath (%s) and " 8980Sstevel@tonic-gate "%s zonepath (%s) overlap.\n"), 8990Sstevel@tonic-gate target_zone, path, ze->zone_name, rpath); 9000Sstevel@tonic-gate free(ze); 9010Sstevel@tonic-gate return (Z_ERR); 9020Sstevel@tonic-gate } 9030Sstevel@tonic-gate free(ze); 9040Sstevel@tonic-gate } 9050Sstevel@tonic-gate endzoneent(cookie); 9060Sstevel@tonic-gate return (Z_OK); 9070Sstevel@tonic-gate } 9080Sstevel@tonic-gate 9090Sstevel@tonic-gate static int 9100Sstevel@tonic-gate validate_zonepath(char *path, int cmd_num) 9110Sstevel@tonic-gate { 9120Sstevel@tonic-gate int res; /* result of last library/system call */ 9130Sstevel@tonic-gate boolean_t err = B_FALSE; /* have we run into an error? */ 9140Sstevel@tonic-gate struct stat stbuf; 9152267Sdp struct statvfs64 vfsbuf; 9160Sstevel@tonic-gate char rpath[MAXPATHLEN]; /* resolved path */ 9170Sstevel@tonic-gate char ppath[MAXPATHLEN]; /* parent path */ 9180Sstevel@tonic-gate char rppath[MAXPATHLEN]; /* resolved parent path */ 9190Sstevel@tonic-gate char rootpath[MAXPATHLEN]; /* root path */ 9200Sstevel@tonic-gate zone_state_t state; 9210Sstevel@tonic-gate 9220Sstevel@tonic-gate if (path[0] != '/') { 9230Sstevel@tonic-gate (void) fprintf(stderr, 9240Sstevel@tonic-gate gettext("%s is not an absolute path.\n"), path); 9250Sstevel@tonic-gate return (Z_ERR); 9260Sstevel@tonic-gate } 9270Sstevel@tonic-gate if ((res = resolvepath(path, rpath, sizeof (rpath))) == -1) { 9280Sstevel@tonic-gate if ((errno != ENOENT) || 9291300Sgjelinek (cmd_num != CMD_VERIFY && cmd_num != CMD_INSTALL && 9301300Sgjelinek cmd_num != CMD_CLONE && cmd_num != CMD_MOVE)) { 9310Sstevel@tonic-gate zperror(path, B_FALSE); 9320Sstevel@tonic-gate return (Z_ERR); 9330Sstevel@tonic-gate } 9340Sstevel@tonic-gate if (cmd_num == CMD_VERIFY) { 935924Sgjelinek /* 936924Sgjelinek * TRANSLATION_NOTE 937924Sgjelinek * zoneadm is a literal that should not be translated. 938924Sgjelinek */ 9390Sstevel@tonic-gate (void) fprintf(stderr, gettext("WARNING: %s does not " 940924Sgjelinek "exist, so it could not be verified.\nWhen " 941924Sgjelinek "'zoneadm %s' is run, '%s' will try to create\n%s, " 942924Sgjelinek "and '%s' will be tried again,\nbut the '%s' may " 943924Sgjelinek "fail if:\nthe parent directory of %s is group- or " 944924Sgjelinek "other-writable\nor\n%s overlaps with any other " 9450Sstevel@tonic-gate "installed zones.\n"), path, 9460Sstevel@tonic-gate cmd_to_str(CMD_INSTALL), cmd_to_str(CMD_INSTALL), 9470Sstevel@tonic-gate path, cmd_to_str(CMD_VERIFY), 9480Sstevel@tonic-gate cmd_to_str(CMD_VERIFY), path, path); 9490Sstevel@tonic-gate return (Z_OK); 9500Sstevel@tonic-gate } 9510Sstevel@tonic-gate /* 9520Sstevel@tonic-gate * The zonepath is supposed to be mode 700 but its 9530Sstevel@tonic-gate * parent(s) 755. So use 755 on the mkdirp() then 9540Sstevel@tonic-gate * chmod() the zonepath itself to 700. 9550Sstevel@tonic-gate */ 9560Sstevel@tonic-gate if (mkdirp(path, DEFAULT_DIR_MODE) < 0) { 9570Sstevel@tonic-gate zperror(path, B_FALSE); 9580Sstevel@tonic-gate return (Z_ERR); 9590Sstevel@tonic-gate } 9600Sstevel@tonic-gate /* 9610Sstevel@tonic-gate * If the chmod() fails, report the error, but might 9620Sstevel@tonic-gate * as well continue the verify procedure. 9630Sstevel@tonic-gate */ 9640Sstevel@tonic-gate if (chmod(path, S_IRWXU) != 0) 9650Sstevel@tonic-gate zperror(path, B_FALSE); 9660Sstevel@tonic-gate /* 9670Sstevel@tonic-gate * Since the mkdir() succeeded, we should not have to 9680Sstevel@tonic-gate * worry about a subsequent ENOENT, thus this should 9690Sstevel@tonic-gate * only recurse once. 9700Sstevel@tonic-gate */ 9711300Sgjelinek return (validate_zonepath(path, cmd_num)); 9720Sstevel@tonic-gate } 9730Sstevel@tonic-gate rpath[res] = '\0'; 9740Sstevel@tonic-gate if (strcmp(path, rpath) != 0) { 9750Sstevel@tonic-gate errno = Z_RESOLVED_PATH; 9760Sstevel@tonic-gate zperror(path, B_TRUE); 9770Sstevel@tonic-gate return (Z_ERR); 9780Sstevel@tonic-gate } 9790Sstevel@tonic-gate if ((res = stat(rpath, &stbuf)) != 0) { 9800Sstevel@tonic-gate zperror(rpath, B_FALSE); 9810Sstevel@tonic-gate return (Z_ERR); 9820Sstevel@tonic-gate } 9830Sstevel@tonic-gate if (!S_ISDIR(stbuf.st_mode)) { 9840Sstevel@tonic-gate (void) fprintf(stderr, gettext("%s is not a directory.\n"), 9850Sstevel@tonic-gate rpath); 9860Sstevel@tonic-gate return (Z_ERR); 9870Sstevel@tonic-gate } 9883445Sblakej if (strcmp(stbuf.st_fstype, MNTTYPE_TMPFS) == 0) { 9890Sstevel@tonic-gate (void) printf(gettext("WARNING: %s is on a temporary " 9901867Sgjelinek "file system.\n"), rpath); 9910Sstevel@tonic-gate } 9920Sstevel@tonic-gate if (crosscheck_zonepaths(rpath) != Z_OK) 9930Sstevel@tonic-gate return (Z_ERR); 9940Sstevel@tonic-gate /* 9950Sstevel@tonic-gate * Try to collect and report as many minor errors as possible 9960Sstevel@tonic-gate * before returning, so the user can learn everything that needs 9970Sstevel@tonic-gate * to be fixed up front. 9980Sstevel@tonic-gate */ 9990Sstevel@tonic-gate if (stbuf.st_uid != 0) { 10000Sstevel@tonic-gate (void) fprintf(stderr, gettext("%s is not owned by root.\n"), 10010Sstevel@tonic-gate rpath); 10020Sstevel@tonic-gate err = B_TRUE; 10030Sstevel@tonic-gate } 10040Sstevel@tonic-gate err |= bad_mode_bit(stbuf.st_mode, S_IRUSR, B_TRUE, rpath); 10050Sstevel@tonic-gate err |= bad_mode_bit(stbuf.st_mode, S_IWUSR, B_TRUE, rpath); 10060Sstevel@tonic-gate err |= bad_mode_bit(stbuf.st_mode, S_IXUSR, B_TRUE, rpath); 10070Sstevel@tonic-gate err |= bad_mode_bit(stbuf.st_mode, S_IRGRP, B_FALSE, rpath); 10080Sstevel@tonic-gate err |= bad_mode_bit(stbuf.st_mode, S_IWGRP, B_FALSE, rpath); 10090Sstevel@tonic-gate err |= bad_mode_bit(stbuf.st_mode, S_IXGRP, B_FALSE, rpath); 10100Sstevel@tonic-gate err |= bad_mode_bit(stbuf.st_mode, S_IROTH, B_FALSE, rpath); 10110Sstevel@tonic-gate err |= bad_mode_bit(stbuf.st_mode, S_IWOTH, B_FALSE, rpath); 10120Sstevel@tonic-gate err |= bad_mode_bit(stbuf.st_mode, S_IXOTH, B_FALSE, rpath); 10130Sstevel@tonic-gate 10140Sstevel@tonic-gate (void) snprintf(ppath, sizeof (ppath), "%s/..", path); 10150Sstevel@tonic-gate if ((res = resolvepath(ppath, rppath, sizeof (rppath))) == -1) { 10160Sstevel@tonic-gate zperror(ppath, B_FALSE); 10170Sstevel@tonic-gate return (Z_ERR); 10180Sstevel@tonic-gate } 10190Sstevel@tonic-gate rppath[res] = '\0'; 10200Sstevel@tonic-gate if ((res = stat(rppath, &stbuf)) != 0) { 10210Sstevel@tonic-gate zperror(rppath, B_FALSE); 10220Sstevel@tonic-gate return (Z_ERR); 10230Sstevel@tonic-gate } 10240Sstevel@tonic-gate /* theoretically impossible */ 10250Sstevel@tonic-gate if (!S_ISDIR(stbuf.st_mode)) { 10260Sstevel@tonic-gate (void) fprintf(stderr, gettext("%s is not a directory.\n"), 10270Sstevel@tonic-gate rppath); 10280Sstevel@tonic-gate return (Z_ERR); 10290Sstevel@tonic-gate } 10300Sstevel@tonic-gate if (stbuf.st_uid != 0) { 10310Sstevel@tonic-gate (void) fprintf(stderr, gettext("%s is not owned by root.\n"), 10320Sstevel@tonic-gate rppath); 10330Sstevel@tonic-gate err = B_TRUE; 10340Sstevel@tonic-gate } 10350Sstevel@tonic-gate err |= bad_mode_bit(stbuf.st_mode, S_IRUSR, B_TRUE, rppath); 10360Sstevel@tonic-gate err |= bad_mode_bit(stbuf.st_mode, S_IWUSR, B_TRUE, rppath); 10370Sstevel@tonic-gate err |= bad_mode_bit(stbuf.st_mode, S_IXUSR, B_TRUE, rppath); 10380Sstevel@tonic-gate err |= bad_mode_bit(stbuf.st_mode, S_IWGRP, B_FALSE, rppath); 10390Sstevel@tonic-gate err |= bad_mode_bit(stbuf.st_mode, S_IWOTH, B_FALSE, rppath); 10400Sstevel@tonic-gate if (strcmp(rpath, rppath) == 0) { 10410Sstevel@tonic-gate (void) fprintf(stderr, gettext("%s is its own parent.\n"), 10420Sstevel@tonic-gate rppath); 10430Sstevel@tonic-gate err = B_TRUE; 10440Sstevel@tonic-gate } 10450Sstevel@tonic-gate 10462267Sdp if (statvfs64(rpath, &vfsbuf) != 0) { 10470Sstevel@tonic-gate zperror(rpath, B_FALSE); 10480Sstevel@tonic-gate return (Z_ERR); 10490Sstevel@tonic-gate } 1050823Sgjelinek if (strcmp(vfsbuf.f_basetype, MNTTYPE_NFS) == 0) { 1051924Sgjelinek /* 1052924Sgjelinek * TRANSLATION_NOTE 1053924Sgjelinek * Zonepath and NFS are literals that should not be translated. 1054924Sgjelinek */ 1055924Sgjelinek (void) fprintf(stderr, gettext("Zonepath %s is on an NFS " 10561867Sgjelinek "mounted file system.\n" 10571867Sgjelinek "\tA local file system must be used.\n"), rpath); 1058823Sgjelinek return (Z_ERR); 1059823Sgjelinek } 1060823Sgjelinek if (vfsbuf.f_flag & ST_NOSUID) { 1061924Sgjelinek /* 1062924Sgjelinek * TRANSLATION_NOTE 1063924Sgjelinek * Zonepath and nosuid are literals that should not be 1064924Sgjelinek * translated. 1065924Sgjelinek */ 1066823Sgjelinek (void) fprintf(stderr, gettext("Zonepath %s is on a nosuid " 10671867Sgjelinek "file system.\n"), rpath); 10680Sstevel@tonic-gate return (Z_ERR); 10690Sstevel@tonic-gate } 10700Sstevel@tonic-gate 10710Sstevel@tonic-gate if ((res = zone_get_state(target_zone, &state)) != Z_OK) { 10720Sstevel@tonic-gate errno = res; 10730Sstevel@tonic-gate zperror2(target_zone, gettext("could not get state")); 10740Sstevel@tonic-gate return (Z_ERR); 10750Sstevel@tonic-gate } 10760Sstevel@tonic-gate /* 10770Sstevel@tonic-gate * The existence of the root path is only bad in the configured state, 10780Sstevel@tonic-gate * as it is *supposed* to be there at the installed and later states. 10791507Sgjelinek * However, the root path is expected to be there if the zone is 10801507Sgjelinek * detached. 10810Sstevel@tonic-gate * State/command mismatches are caught earlier in verify_details(). 10820Sstevel@tonic-gate */ 10831507Sgjelinek if (state == ZONE_STATE_CONFIGURED && cmd_num != CMD_ATTACH) { 10840Sstevel@tonic-gate if (snprintf(rootpath, sizeof (rootpath), "%s/root", rpath) >= 10850Sstevel@tonic-gate sizeof (rootpath)) { 1086924Sgjelinek /* 1087924Sgjelinek * TRANSLATION_NOTE 1088924Sgjelinek * Zonepath is a literal that should not be translated. 1089924Sgjelinek */ 10900Sstevel@tonic-gate (void) fprintf(stderr, 10910Sstevel@tonic-gate gettext("Zonepath %s is too long.\n"), rpath); 10920Sstevel@tonic-gate return (Z_ERR); 10930Sstevel@tonic-gate } 10940Sstevel@tonic-gate if ((res = stat(rootpath, &stbuf)) == 0) { 10951507Sgjelinek if (zonecfg_detached(rpath)) 10961507Sgjelinek (void) fprintf(stderr, 10971507Sgjelinek gettext("Cannot %s detached " 10981507Sgjelinek "zone.\nUse attach or remove %s " 10991507Sgjelinek "directory.\n"), cmd_to_str(cmd_num), 11001507Sgjelinek rpath); 11011507Sgjelinek else 11021507Sgjelinek (void) fprintf(stderr, 11031507Sgjelinek gettext("Rootpath %s exists; " 11041507Sgjelinek "remove or move aside prior to %s.\n"), 11051507Sgjelinek rootpath, cmd_to_str(cmd_num)); 11060Sstevel@tonic-gate return (Z_ERR); 11070Sstevel@tonic-gate } 11080Sstevel@tonic-gate } 11090Sstevel@tonic-gate 11100Sstevel@tonic-gate return (err ? Z_ERR : Z_OK); 11110Sstevel@tonic-gate } 11120Sstevel@tonic-gate 11132712Snn35248 /* 11142712Snn35248 * The following two routines implement a simple locking mechanism to 11152712Snn35248 * ensure that only one instance of zoneadm at a time is able to manipulate 11162712Snn35248 * a given zone. The lock is built on top of an fcntl(2) lock of 11172712Snn35248 * [<altroot>]/var/run/zones/<zonename>.zoneadm.lock. If a zoneadm instance 11182712Snn35248 * can grab that lock, it is allowed to manipulate the zone. 11192712Snn35248 * 11202712Snn35248 * Since zoneadm may call external applications which in turn invoke 11212712Snn35248 * zoneadm again, we introduce the notion of "lock inheritance". Any 11222712Snn35248 * instance of zoneadm that has another instance in its ancestry is assumed 11232712Snn35248 * to be acting on behalf of the original zoneadm, and is thus allowed to 11242712Snn35248 * manipulate its zone. 11252712Snn35248 * 11262712Snn35248 * This inheritance is implemented via the _ZONEADM_LOCK_HELD environment 11272712Snn35248 * variable. When zoneadm is granted a lock on its zone, this environment 11282712Snn35248 * variable is set to 1. When it releases the lock, the variable is set to 11292712Snn35248 * 0. Since a child process inherits its parent's environment, checking 11302712Snn35248 * the state of this variable indicates whether or not any ancestor owns 11312712Snn35248 * the lock. 11322712Snn35248 */ 11330Sstevel@tonic-gate static void 11340Sstevel@tonic-gate release_lock_file(int lockfd) 11350Sstevel@tonic-gate { 11362712Snn35248 /* 11372712Snn35248 * If we are cleaning up from a failed attempt to lock the zone for 11382712Snn35248 * the first time, we might have a zone_lock_cnt of 0. In that 11392712Snn35248 * error case, we don't want to do anything but close the lock 11402712Snn35248 * file. 11412712Snn35248 */ 11422712Snn35248 assert(zone_lock_cnt >= 0); 11432712Snn35248 if (zone_lock_cnt > 0) { 11442712Snn35248 assert(getenv(LOCK_ENV_VAR) != NULL); 11452712Snn35248 assert(atoi(getenv(LOCK_ENV_VAR)) == 1); 11462712Snn35248 if (--zone_lock_cnt > 0) { 11472712Snn35248 assert(lockfd == -1); 11482712Snn35248 return; 11492712Snn35248 } 11502712Snn35248 if (putenv(zoneadm_lock_not_held) != 0) { 11512712Snn35248 zperror(target_zone, B_TRUE); 11522712Snn35248 exit(Z_ERR); 11532712Snn35248 } 11542712Snn35248 } 11552712Snn35248 assert(lockfd >= 0); 11560Sstevel@tonic-gate (void) close(lockfd); 11570Sstevel@tonic-gate } 11580Sstevel@tonic-gate 11590Sstevel@tonic-gate static int 11600Sstevel@tonic-gate grab_lock_file(const char *zone_name, int *lockfd) 11610Sstevel@tonic-gate { 11620Sstevel@tonic-gate char pathbuf[PATH_MAX]; 11630Sstevel@tonic-gate struct flock flock; 11640Sstevel@tonic-gate 11652712Snn35248 /* 11662712Snn35248 * If we already have the lock, we can skip this expensive song 11672712Snn35248 * and dance. 11682712Snn35248 */ 11692712Snn35248 if (zone_lock_cnt > 0) { 11702712Snn35248 zone_lock_cnt++; 11712712Snn35248 *lockfd = -1; 11722712Snn35248 return (Z_OK); 11732712Snn35248 } 11742712Snn35248 assert(getenv(LOCK_ENV_VAR) != NULL); 11752712Snn35248 assert(atoi(getenv(LOCK_ENV_VAR)) == 0); 11762712Snn35248 1177766Scarlsonj if (snprintf(pathbuf, sizeof (pathbuf), "%s%s", zonecfg_get_root(), 1178766Scarlsonj ZONES_TMPDIR) >= sizeof (pathbuf)) { 1179766Scarlsonj zerror(gettext("alternate root path is too long")); 1180766Scarlsonj return (Z_ERR); 1181766Scarlsonj } 1182766Scarlsonj if (mkdir(pathbuf, S_IRWXU) < 0 && errno != EEXIST) { 1183766Scarlsonj zerror(gettext("could not mkdir %s: %s"), pathbuf, 11840Sstevel@tonic-gate strerror(errno)); 11850Sstevel@tonic-gate return (Z_ERR); 11860Sstevel@tonic-gate } 1187766Scarlsonj (void) chmod(pathbuf, S_IRWXU); 11880Sstevel@tonic-gate 11890Sstevel@tonic-gate /* 11900Sstevel@tonic-gate * One of these lock files is created for each zone (when needed). 11910Sstevel@tonic-gate * The lock files are not cleaned up (except on system reboot), 11920Sstevel@tonic-gate * but since there is only one per zone, there is no resource 11930Sstevel@tonic-gate * starvation issue. 11940Sstevel@tonic-gate */ 1195766Scarlsonj if (snprintf(pathbuf, sizeof (pathbuf), "%s%s/%s.zoneadm.lock", 1196766Scarlsonj zonecfg_get_root(), ZONES_TMPDIR, zone_name) >= sizeof (pathbuf)) { 1197766Scarlsonj zerror(gettext("alternate root path is too long")); 1198766Scarlsonj return (Z_ERR); 1199766Scarlsonj } 12000Sstevel@tonic-gate if ((*lockfd = open(pathbuf, O_RDWR|O_CREAT, S_IRUSR|S_IWUSR)) < 0) { 12010Sstevel@tonic-gate zerror(gettext("could not open %s: %s"), pathbuf, 12020Sstevel@tonic-gate strerror(errno)); 12030Sstevel@tonic-gate return (Z_ERR); 12040Sstevel@tonic-gate } 12050Sstevel@tonic-gate /* 12060Sstevel@tonic-gate * Lock the file to synchronize with other zoneadmds 12070Sstevel@tonic-gate */ 12080Sstevel@tonic-gate flock.l_type = F_WRLCK; 12090Sstevel@tonic-gate flock.l_whence = SEEK_SET; 12100Sstevel@tonic-gate flock.l_start = (off_t)0; 12110Sstevel@tonic-gate flock.l_len = (off_t)0; 12122712Snn35248 if ((fcntl(*lockfd, F_SETLKW, &flock) < 0) || 12132712Snn35248 (putenv(zoneadm_lock_held) != 0)) { 12140Sstevel@tonic-gate zerror(gettext("unable to lock %s: %s"), pathbuf, 12150Sstevel@tonic-gate strerror(errno)); 12160Sstevel@tonic-gate release_lock_file(*lockfd); 12170Sstevel@tonic-gate return (Z_ERR); 12180Sstevel@tonic-gate } 12192712Snn35248 zone_lock_cnt = 1; 12200Sstevel@tonic-gate return (Z_OK); 12210Sstevel@tonic-gate } 12220Sstevel@tonic-gate 1223766Scarlsonj static boolean_t 12240Sstevel@tonic-gate get_doorname(const char *zone_name, char *buffer) 12250Sstevel@tonic-gate { 1226766Scarlsonj return (snprintf(buffer, PATH_MAX, "%s" ZONE_DOOR_PATH, 1227766Scarlsonj zonecfg_get_root(), zone_name) < PATH_MAX); 12280Sstevel@tonic-gate } 12290Sstevel@tonic-gate 12300Sstevel@tonic-gate /* 12310Sstevel@tonic-gate * system daemons are not audited. For the global zone, this occurs 12320Sstevel@tonic-gate * "naturally" since init is started with the default audit 12330Sstevel@tonic-gate * characteristics. Since zoneadmd is a system daemon and it starts 12340Sstevel@tonic-gate * init for a zone, it is necessary to clear out the audit 12350Sstevel@tonic-gate * characteristics inherited from whomever started zoneadmd. This is 12360Sstevel@tonic-gate * indicated by the audit id, which is set from the ruid parameter of 12370Sstevel@tonic-gate * adt_set_user(), below. 12380Sstevel@tonic-gate */ 12390Sstevel@tonic-gate 12400Sstevel@tonic-gate static void 12410Sstevel@tonic-gate prepare_audit_context() 12420Sstevel@tonic-gate { 12430Sstevel@tonic-gate adt_session_data_t *ah; 12440Sstevel@tonic-gate char *failure = gettext("audit failure: %s"); 12450Sstevel@tonic-gate 12460Sstevel@tonic-gate if (adt_start_session(&ah, NULL, 0)) { 12470Sstevel@tonic-gate zerror(failure, strerror(errno)); 12480Sstevel@tonic-gate return; 12490Sstevel@tonic-gate } 12500Sstevel@tonic-gate if (adt_set_user(ah, ADT_NO_AUDIT, ADT_NO_AUDIT, 12510Sstevel@tonic-gate ADT_NO_AUDIT, ADT_NO_AUDIT, NULL, ADT_NEW)) { 12520Sstevel@tonic-gate zerror(failure, strerror(errno)); 12530Sstevel@tonic-gate (void) adt_end_session(ah); 12540Sstevel@tonic-gate return; 12550Sstevel@tonic-gate } 12560Sstevel@tonic-gate if (adt_set_proc(ah)) 12570Sstevel@tonic-gate zerror(failure, strerror(errno)); 12580Sstevel@tonic-gate 12590Sstevel@tonic-gate (void) adt_end_session(ah); 12600Sstevel@tonic-gate } 12610Sstevel@tonic-gate 12620Sstevel@tonic-gate static int 12630Sstevel@tonic-gate start_zoneadmd(const char *zone_name) 12640Sstevel@tonic-gate { 12650Sstevel@tonic-gate char doorpath[PATH_MAX]; 12660Sstevel@tonic-gate pid_t child_pid; 12670Sstevel@tonic-gate int error = Z_ERR; 12680Sstevel@tonic-gate int doorfd, lockfd; 12690Sstevel@tonic-gate struct door_info info; 12700Sstevel@tonic-gate 1271766Scarlsonj if (!get_doorname(zone_name, doorpath)) 1272766Scarlsonj return (Z_ERR); 12730Sstevel@tonic-gate 12740Sstevel@tonic-gate if (grab_lock_file(zone_name, &lockfd) != Z_OK) 12750Sstevel@tonic-gate return (Z_ERR); 12760Sstevel@tonic-gate 12770Sstevel@tonic-gate /* 12780Sstevel@tonic-gate * Now that we have the lock, re-confirm that the daemon is 12790Sstevel@tonic-gate * *not* up and working fine. If it is still down, we have a green 12800Sstevel@tonic-gate * light to start it. 12810Sstevel@tonic-gate */ 12820Sstevel@tonic-gate if ((doorfd = open(doorpath, O_RDONLY)) < 0) { 12830Sstevel@tonic-gate if (errno != ENOENT) { 12840Sstevel@tonic-gate zperror(doorpath, B_FALSE); 12850Sstevel@tonic-gate goto out; 12860Sstevel@tonic-gate } 12870Sstevel@tonic-gate } else { 12880Sstevel@tonic-gate if (door_info(doorfd, &info) == 0 && 12890Sstevel@tonic-gate ((info.di_attributes & DOOR_REVOKED) == 0)) { 12900Sstevel@tonic-gate error = Z_OK; 12910Sstevel@tonic-gate (void) close(doorfd); 12920Sstevel@tonic-gate goto out; 12930Sstevel@tonic-gate } 12940Sstevel@tonic-gate (void) close(doorfd); 12950Sstevel@tonic-gate } 12960Sstevel@tonic-gate 12970Sstevel@tonic-gate if ((child_pid = fork()) == -1) { 12980Sstevel@tonic-gate zperror(gettext("could not fork"), B_FALSE); 12990Sstevel@tonic-gate goto out; 13000Sstevel@tonic-gate } else if (child_pid == 0) { 1301766Scarlsonj const char *argv[6], **ap; 1302766Scarlsonj 13030Sstevel@tonic-gate /* child process */ 13040Sstevel@tonic-gate prepare_audit_context(); 13050Sstevel@tonic-gate 1306766Scarlsonj ap = argv; 1307766Scarlsonj *ap++ = "zoneadmd"; 1308766Scarlsonj *ap++ = "-z"; 1309766Scarlsonj *ap++ = zone_name; 1310766Scarlsonj if (zonecfg_in_alt_root()) { 1311766Scarlsonj *ap++ = "-R"; 1312766Scarlsonj *ap++ = zonecfg_get_root(); 1313766Scarlsonj } 1314766Scarlsonj *ap = NULL; 1315766Scarlsonj 1316766Scarlsonj (void) execv("/usr/lib/zones/zoneadmd", (char * const *)argv); 1317924Sgjelinek /* 1318924Sgjelinek * TRANSLATION_NOTE 1319924Sgjelinek * zoneadmd is a literal that should not be translated. 1320924Sgjelinek */ 13210Sstevel@tonic-gate zperror(gettext("could not exec zoneadmd"), B_FALSE); 13220Sstevel@tonic-gate _exit(Z_ERR); 13230Sstevel@tonic-gate } else { 13240Sstevel@tonic-gate /* parent process */ 13250Sstevel@tonic-gate pid_t retval; 13260Sstevel@tonic-gate int pstatus = 0; 13270Sstevel@tonic-gate 13280Sstevel@tonic-gate do { 13290Sstevel@tonic-gate retval = waitpid(child_pid, &pstatus, 0); 13300Sstevel@tonic-gate } while (retval != child_pid); 13310Sstevel@tonic-gate if (WIFSIGNALED(pstatus) || (WIFEXITED(pstatus) && 13320Sstevel@tonic-gate WEXITSTATUS(pstatus) != 0)) { 13330Sstevel@tonic-gate zerror(gettext("could not start %s"), "zoneadmd"); 13340Sstevel@tonic-gate goto out; 13350Sstevel@tonic-gate } 13360Sstevel@tonic-gate } 13370Sstevel@tonic-gate error = Z_OK; 13380Sstevel@tonic-gate out: 13390Sstevel@tonic-gate release_lock_file(lockfd); 13400Sstevel@tonic-gate return (error); 13410Sstevel@tonic-gate } 13420Sstevel@tonic-gate 13430Sstevel@tonic-gate static int 13440Sstevel@tonic-gate ping_zoneadmd(const char *zone_name) 13450Sstevel@tonic-gate { 13460Sstevel@tonic-gate char doorpath[PATH_MAX]; 13470Sstevel@tonic-gate int doorfd; 13480Sstevel@tonic-gate struct door_info info; 13490Sstevel@tonic-gate 1350766Scarlsonj if (!get_doorname(zone_name, doorpath)) 1351766Scarlsonj return (Z_ERR); 13520Sstevel@tonic-gate 13530Sstevel@tonic-gate if ((doorfd = open(doorpath, O_RDONLY)) < 0) { 13540Sstevel@tonic-gate return (Z_ERR); 13550Sstevel@tonic-gate } 13560Sstevel@tonic-gate if (door_info(doorfd, &info) == 0 && 13570Sstevel@tonic-gate ((info.di_attributes & DOOR_REVOKED) == 0)) { 13580Sstevel@tonic-gate (void) close(doorfd); 13590Sstevel@tonic-gate return (Z_OK); 13600Sstevel@tonic-gate } 13610Sstevel@tonic-gate (void) close(doorfd); 13620Sstevel@tonic-gate return (Z_ERR); 13630Sstevel@tonic-gate } 13640Sstevel@tonic-gate 13650Sstevel@tonic-gate static int 13660Sstevel@tonic-gate call_zoneadmd(const char *zone_name, zone_cmd_arg_t *arg) 13670Sstevel@tonic-gate { 13680Sstevel@tonic-gate char doorpath[PATH_MAX]; 13690Sstevel@tonic-gate int doorfd, result; 13700Sstevel@tonic-gate door_arg_t darg; 13710Sstevel@tonic-gate 13720Sstevel@tonic-gate zoneid_t zoneid; 13730Sstevel@tonic-gate uint64_t uniqid = 0; 13740Sstevel@tonic-gate 13750Sstevel@tonic-gate zone_cmd_rval_t *rvalp; 13760Sstevel@tonic-gate size_t rlen; 13770Sstevel@tonic-gate char *cp, *errbuf; 13780Sstevel@tonic-gate 13790Sstevel@tonic-gate rlen = getpagesize(); 13800Sstevel@tonic-gate if ((rvalp = malloc(rlen)) == NULL) { 13810Sstevel@tonic-gate zerror(gettext("failed to allocate %lu bytes: %s"), rlen, 13820Sstevel@tonic-gate strerror(errno)); 13830Sstevel@tonic-gate return (-1); 13840Sstevel@tonic-gate } 13850Sstevel@tonic-gate 13860Sstevel@tonic-gate if ((zoneid = getzoneidbyname(zone_name)) != ZONE_ID_UNDEFINED) { 13870Sstevel@tonic-gate (void) zone_getattr(zoneid, ZONE_ATTR_UNIQID, &uniqid, 13880Sstevel@tonic-gate sizeof (uniqid)); 13890Sstevel@tonic-gate } 13900Sstevel@tonic-gate arg->uniqid = uniqid; 13910Sstevel@tonic-gate (void) strlcpy(arg->locale, locale, sizeof (arg->locale)); 1392766Scarlsonj if (!get_doorname(zone_name, doorpath)) { 1393766Scarlsonj zerror(gettext("alternate root path is too long")); 1394766Scarlsonj free(rvalp); 1395766Scarlsonj return (-1); 1396766Scarlsonj } 13970Sstevel@tonic-gate 13980Sstevel@tonic-gate /* 13990Sstevel@tonic-gate * Loop trying to start zoneadmd; if something goes seriously 14000Sstevel@tonic-gate * wrong we break out and fail. 14010Sstevel@tonic-gate */ 14020Sstevel@tonic-gate for (;;) { 14030Sstevel@tonic-gate if (start_zoneadmd(zone_name) != Z_OK) 14040Sstevel@tonic-gate break; 14050Sstevel@tonic-gate 14060Sstevel@tonic-gate if ((doorfd = open(doorpath, O_RDONLY)) < 0) { 14070Sstevel@tonic-gate zperror(gettext("failed to open zone door"), B_FALSE); 14080Sstevel@tonic-gate break; 14090Sstevel@tonic-gate } 14100Sstevel@tonic-gate 14110Sstevel@tonic-gate darg.data_ptr = (char *)arg; 14120Sstevel@tonic-gate darg.data_size = sizeof (*arg); 14130Sstevel@tonic-gate darg.desc_ptr = NULL; 14140Sstevel@tonic-gate darg.desc_num = 0; 14150Sstevel@tonic-gate darg.rbuf = (char *)rvalp; 14160Sstevel@tonic-gate darg.rsize = rlen; 14170Sstevel@tonic-gate if (door_call(doorfd, &darg) != 0) { 14180Sstevel@tonic-gate (void) close(doorfd); 14190Sstevel@tonic-gate /* 14200Sstevel@tonic-gate * We'll get EBADF if the door has been revoked. 14210Sstevel@tonic-gate */ 14220Sstevel@tonic-gate if (errno != EBADF) { 14230Sstevel@tonic-gate zperror(gettext("door_call failed"), B_FALSE); 14240Sstevel@tonic-gate break; 14250Sstevel@tonic-gate } 14260Sstevel@tonic-gate continue; /* take another lap */ 14270Sstevel@tonic-gate } 14280Sstevel@tonic-gate (void) close(doorfd); 14290Sstevel@tonic-gate 14300Sstevel@tonic-gate if (darg.data_size == 0) { 14310Sstevel@tonic-gate /* Door server is going away; kick it again. */ 14320Sstevel@tonic-gate continue; 14330Sstevel@tonic-gate } 14340Sstevel@tonic-gate 14350Sstevel@tonic-gate errbuf = rvalp->errbuf; 14360Sstevel@tonic-gate while (*errbuf != '\0') { 14370Sstevel@tonic-gate /* 14380Sstevel@tonic-gate * Remove any newlines since zerror() 14390Sstevel@tonic-gate * will append one automatically. 14400Sstevel@tonic-gate */ 14410Sstevel@tonic-gate cp = strchr(errbuf, '\n'); 14420Sstevel@tonic-gate if (cp != NULL) 14430Sstevel@tonic-gate *cp = '\0'; 14440Sstevel@tonic-gate zerror("%s", errbuf); 14450Sstevel@tonic-gate if (cp == NULL) 14460Sstevel@tonic-gate break; 14470Sstevel@tonic-gate errbuf = cp + 1; 14480Sstevel@tonic-gate } 14490Sstevel@tonic-gate result = rvalp->rval == 0 ? 0 : -1; 14500Sstevel@tonic-gate free(rvalp); 14510Sstevel@tonic-gate return (result); 14520Sstevel@tonic-gate } 14530Sstevel@tonic-gate 14540Sstevel@tonic-gate free(rvalp); 14550Sstevel@tonic-gate return (-1); 14560Sstevel@tonic-gate } 14570Sstevel@tonic-gate 14580Sstevel@tonic-gate static int 14593339Szt129084 invoke_brand_handler(int cmd_num, char *argv[]) 14603339Szt129084 { 14613339Szt129084 zone_dochandle_t handle; 14623339Szt129084 int err; 14633339Szt129084 14643339Szt129084 if ((handle = zonecfg_init_handle()) == NULL) { 14653339Szt129084 zperror(cmd_to_str(cmd_num), B_TRUE); 14663339Szt129084 return (Z_ERR); 14673339Szt129084 } 14683339Szt129084 if ((err = zonecfg_get_handle(target_zone, handle)) != Z_OK) { 14693339Szt129084 errno = err; 14703339Szt129084 zperror(cmd_to_str(cmd_num), B_TRUE); 14713339Szt129084 zonecfg_fini_handle(handle); 14723339Szt129084 return (Z_ERR); 14733339Szt129084 } 14743339Szt129084 if (verify_brand(handle, cmd_num, argv) != Z_OK) { 14753339Szt129084 zonecfg_fini_handle(handle); 14763339Szt129084 return (Z_ERR); 14773339Szt129084 } 14783339Szt129084 zonecfg_fini_handle(handle); 14793339Szt129084 return (Z_OK); 14803339Szt129084 } 14813339Szt129084 14823339Szt129084 static int 14830Sstevel@tonic-gate ready_func(int argc, char *argv[]) 14840Sstevel@tonic-gate { 14850Sstevel@tonic-gate zone_cmd_arg_t zarg; 14860Sstevel@tonic-gate int arg; 14870Sstevel@tonic-gate 1488766Scarlsonj if (zonecfg_in_alt_root()) { 1489766Scarlsonj zerror(gettext("cannot ready zone in alternate root")); 1490766Scarlsonj return (Z_ERR); 1491766Scarlsonj } 1492766Scarlsonj 14930Sstevel@tonic-gate optind = 0; 14940Sstevel@tonic-gate if ((arg = getopt(argc, argv, "?")) != EOF) { 14950Sstevel@tonic-gate switch (arg) { 14960Sstevel@tonic-gate case '?': 14970Sstevel@tonic-gate sub_usage(SHELP_READY, CMD_READY); 14980Sstevel@tonic-gate return (optopt == '?' ? Z_OK : Z_USAGE); 14990Sstevel@tonic-gate default: 15000Sstevel@tonic-gate sub_usage(SHELP_READY, CMD_READY); 15010Sstevel@tonic-gate return (Z_USAGE); 15020Sstevel@tonic-gate } 15030Sstevel@tonic-gate } 15040Sstevel@tonic-gate if (argc > optind) { 15050Sstevel@tonic-gate sub_usage(SHELP_READY, CMD_READY); 15060Sstevel@tonic-gate return (Z_USAGE); 15070Sstevel@tonic-gate } 15082712Snn35248 if (sanity_check(target_zone, CMD_READY, B_FALSE, B_FALSE, B_FALSE) 15092712Snn35248 != Z_OK) 15100Sstevel@tonic-gate return (Z_ERR); 15113339Szt129084 if (verify_details(CMD_READY, argv) != Z_OK) 15120Sstevel@tonic-gate return (Z_ERR); 15130Sstevel@tonic-gate 15140Sstevel@tonic-gate zarg.cmd = Z_READY; 15150Sstevel@tonic-gate if (call_zoneadmd(target_zone, &zarg) != 0) { 15160Sstevel@tonic-gate zerror(gettext("call to %s failed"), "zoneadmd"); 15170Sstevel@tonic-gate return (Z_ERR); 15180Sstevel@tonic-gate } 15190Sstevel@tonic-gate return (Z_OK); 15200Sstevel@tonic-gate } 15210Sstevel@tonic-gate 15220Sstevel@tonic-gate static int 15230Sstevel@tonic-gate boot_func(int argc, char *argv[]) 15240Sstevel@tonic-gate { 15250Sstevel@tonic-gate zone_cmd_arg_t zarg; 15262712Snn35248 boolean_t force = B_FALSE; 15270Sstevel@tonic-gate int arg; 15280Sstevel@tonic-gate 1529766Scarlsonj if (zonecfg_in_alt_root()) { 1530766Scarlsonj zerror(gettext("cannot boot zone in alternate root")); 1531766Scarlsonj return (Z_ERR); 1532766Scarlsonj } 1533766Scarlsonj 15340Sstevel@tonic-gate zarg.bootbuf[0] = '\0'; 15350Sstevel@tonic-gate 15360Sstevel@tonic-gate /* 15372267Sdp * The following getopt processes arguments to zone boot; that 15382267Sdp * is to say, the [here] portion of the argument string: 15392267Sdp * 15402267Sdp * zoneadm -z myzone boot [here] -- -v -m verbose 15412267Sdp * 15422267Sdp * Where [here] can either be nothing, -? (in which case we bail 15432712Snn35248 * and print usage), -f (a private option to indicate that the 15442712Snn35248 * boot operation should be 'forced'), or -s. Support for -s is 15452712Snn35248 * vestigal and obsolete, but is retained because it was a 15462712Snn35248 * documented interface and there are known consumers including 15472712Snn35248 * admin/install; the proper way to specify boot arguments like -s 15482712Snn35248 * is: 15492267Sdp * 15502267Sdp * zoneadm -z myzone boot -- -s -v -m verbose. 15510Sstevel@tonic-gate */ 15520Sstevel@tonic-gate optind = 0; 15532712Snn35248 while ((arg = getopt(argc, argv, "?fs")) != EOF) { 15540Sstevel@tonic-gate switch (arg) { 15550Sstevel@tonic-gate case '?': 15560Sstevel@tonic-gate sub_usage(SHELP_BOOT, CMD_BOOT); 15570Sstevel@tonic-gate return (optopt == '?' ? Z_OK : Z_USAGE); 15580Sstevel@tonic-gate case 's': 15590Sstevel@tonic-gate (void) strlcpy(zarg.bootbuf, "-s", 15600Sstevel@tonic-gate sizeof (zarg.bootbuf)); 15610Sstevel@tonic-gate break; 15622712Snn35248 case 'f': 15632712Snn35248 force = B_TRUE; 15642712Snn35248 break; 15650Sstevel@tonic-gate default: 15660Sstevel@tonic-gate sub_usage(SHELP_BOOT, CMD_BOOT); 15670Sstevel@tonic-gate return (Z_USAGE); 15680Sstevel@tonic-gate } 15690Sstevel@tonic-gate } 15702267Sdp 15712267Sdp for (; optind < argc; optind++) { 15722267Sdp if (strlcat(zarg.bootbuf, argv[optind], 15732267Sdp sizeof (zarg.bootbuf)) >= sizeof (zarg.bootbuf)) { 15742267Sdp zerror(gettext("Boot argument list too long")); 15752267Sdp return (Z_ERR); 15762267Sdp } 15772267Sdp if (optind < argc - 1) 15782267Sdp if (strlcat(zarg.bootbuf, " ", sizeof (zarg.bootbuf)) >= 15792267Sdp sizeof (zarg.bootbuf)) { 15802267Sdp zerror(gettext("Boot argument list too long")); 15812267Sdp return (Z_ERR); 15822267Sdp } 15832267Sdp } 15842712Snn35248 if (sanity_check(target_zone, CMD_BOOT, B_FALSE, B_FALSE, force) 15852712Snn35248 != Z_OK) 15860Sstevel@tonic-gate return (Z_ERR); 15873339Szt129084 if (verify_details(CMD_BOOT, argv) != Z_OK) 15880Sstevel@tonic-gate return (Z_ERR); 15892712Snn35248 zarg.cmd = force ? Z_FORCEBOOT : Z_BOOT; 15900Sstevel@tonic-gate if (call_zoneadmd(target_zone, &zarg) != 0) { 15910Sstevel@tonic-gate zerror(gettext("call to %s failed"), "zoneadmd"); 15920Sstevel@tonic-gate return (Z_ERR); 15930Sstevel@tonic-gate } 15943247Sgjelinek 15950Sstevel@tonic-gate return (Z_OK); 15960Sstevel@tonic-gate } 15970Sstevel@tonic-gate 15980Sstevel@tonic-gate static void 15990Sstevel@tonic-gate fake_up_local_zone(zoneid_t zid, zone_entry_t *zeptr) 16000Sstevel@tonic-gate { 16010Sstevel@tonic-gate ssize_t result; 16022303Scarlsonj uuid_t uuid; 16032303Scarlsonj FILE *fp; 1604*3448Sdh155122 ushort_t flags; 16052303Scarlsonj 16062303Scarlsonj (void) memset(zeptr, 0, sizeof (*zeptr)); 16070Sstevel@tonic-gate 16080Sstevel@tonic-gate zeptr->zid = zid; 16092303Scarlsonj 16100Sstevel@tonic-gate /* 16110Sstevel@tonic-gate * Since we're looking up our own (non-global) zone name, 16120Sstevel@tonic-gate * we can be assured that it will succeed. 16130Sstevel@tonic-gate */ 16140Sstevel@tonic-gate result = getzonenamebyid(zid, zeptr->zname, sizeof (zeptr->zname)); 16150Sstevel@tonic-gate assert(result >= 0); 16162303Scarlsonj if (zonecfg_is_scratch(zeptr->zname) && 16172303Scarlsonj (fp = zonecfg_open_scratch("", B_FALSE)) != NULL) { 16182303Scarlsonj (void) zonecfg_reverse_scratch(fp, zeptr->zname, zeptr->zname, 16192303Scarlsonj sizeof (zeptr->zname), NULL, 0); 16202303Scarlsonj zonecfg_close_scratch(fp); 16212303Scarlsonj } 16222303Scarlsonj 16232303Scarlsonj if (is_system_labeled()) { 16241676Sjpk (void) zone_getattr(zid, ZONE_ATTR_ROOT, zeptr->zroot, 16251676Sjpk sizeof (zeptr->zroot)); 16262712Snn35248 (void) strlcpy(zeptr->zbrand, NATIVE_BRAND_NAME, 16272712Snn35248 sizeof (zeptr->zbrand)); 16282303Scarlsonj } else { 16292303Scarlsonj (void) strlcpy(zeptr->zroot, "/", sizeof (zeptr->zroot)); 16302712Snn35248 (void) zone_getattr(zid, ZONE_ATTR_BRAND, zeptr->zbrand, 16312712Snn35248 sizeof (zeptr->zbrand)); 16322303Scarlsonj } 16332303Scarlsonj 16340Sstevel@tonic-gate zeptr->zstate_str = "running"; 16352303Scarlsonj if (zonecfg_get_uuid(zeptr->zname, uuid) == Z_OK && 16362303Scarlsonj !uuid_is_null(uuid)) 16372303Scarlsonj uuid_unparse(uuid, zeptr->zuuid); 1638*3448Sdh155122 1639*3448Sdh155122 if (zone_getattr(zid, ZONE_ATTR_FLAGS, &flags, sizeof (flags)) < 0) { 1640*3448Sdh155122 zperror2(zeptr->zname, gettext("could not get zone flags")); 1641*3448Sdh155122 exit(Z_ERR); 1642*3448Sdh155122 } 1643*3448Sdh155122 if (flags & ZF_NET_EXCL) 1644*3448Sdh155122 zeptr->ziptype = ZS_EXCLUSIVE; 1645*3448Sdh155122 else 1646*3448Sdh155122 zeptr->ziptype = ZS_SHARED; 16470Sstevel@tonic-gate } 16480Sstevel@tonic-gate 16490Sstevel@tonic-gate static int 16500Sstevel@tonic-gate list_func(int argc, char *argv[]) 16510Sstevel@tonic-gate { 16520Sstevel@tonic-gate zone_entry_t *zentp, zent; 1653766Scarlsonj int arg, retv; 16540Sstevel@tonic-gate boolean_t output = B_FALSE, verbose = B_FALSE, parsable = B_FALSE; 16550Sstevel@tonic-gate zone_state_t min_state = ZONE_STATE_RUNNING; 16560Sstevel@tonic-gate zoneid_t zone_id = getzoneid(); 16570Sstevel@tonic-gate 16580Sstevel@tonic-gate if (target_zone == NULL) { 16590Sstevel@tonic-gate /* all zones: default view to running but allow override */ 16600Sstevel@tonic-gate optind = 0; 16610Sstevel@tonic-gate while ((arg = getopt(argc, argv, "?cipv")) != EOF) { 16620Sstevel@tonic-gate switch (arg) { 16630Sstevel@tonic-gate case '?': 16640Sstevel@tonic-gate sub_usage(SHELP_LIST, CMD_LIST); 16650Sstevel@tonic-gate return (optopt == '?' ? Z_OK : Z_USAGE); 16661339Sjonb /* 16671339Sjonb * The 'i' and 'c' options are not mutually 16681339Sjonb * exclusive so if 'c' is given, then min_state 16691339Sjonb * is set to 0 (ZONE_STATE_CONFIGURED) which is 16701339Sjonb * the lowest possible state. If 'i' is given, 16711339Sjonb * then min_state is set to be the lowest state 16721339Sjonb * so far. 16731339Sjonb */ 16740Sstevel@tonic-gate case 'c': 16750Sstevel@tonic-gate min_state = ZONE_STATE_CONFIGURED; 16760Sstevel@tonic-gate break; 16770Sstevel@tonic-gate case 'i': 16781339Sjonb min_state = min(ZONE_STATE_INSTALLED, 16791339Sjonb min_state); 16801339Sjonb 16810Sstevel@tonic-gate break; 16820Sstevel@tonic-gate case 'p': 16830Sstevel@tonic-gate parsable = B_TRUE; 16840Sstevel@tonic-gate break; 16850Sstevel@tonic-gate case 'v': 16860Sstevel@tonic-gate verbose = B_TRUE; 16870Sstevel@tonic-gate break; 16880Sstevel@tonic-gate default: 16890Sstevel@tonic-gate sub_usage(SHELP_LIST, CMD_LIST); 16900Sstevel@tonic-gate return (Z_USAGE); 16910Sstevel@tonic-gate } 16920Sstevel@tonic-gate } 16930Sstevel@tonic-gate if (parsable && verbose) { 16940Sstevel@tonic-gate zerror(gettext("%s -p and -v are mutually exclusive."), 16950Sstevel@tonic-gate cmd_to_str(CMD_LIST)); 16960Sstevel@tonic-gate return (Z_ERR); 16970Sstevel@tonic-gate } 16981676Sjpk if (zone_id == GLOBAL_ZONEID || is_system_labeled()) { 1699766Scarlsonj retv = zone_print_list(min_state, verbose, parsable); 17000Sstevel@tonic-gate } else { 17012712Snn35248 fake_up_local_zone(zone_id, &zent); 1702766Scarlsonj retv = Z_OK; 17030Sstevel@tonic-gate zone_print(&zent, verbose, parsable); 17040Sstevel@tonic-gate } 1705766Scarlsonj return (retv); 17060Sstevel@tonic-gate } 17070Sstevel@tonic-gate 17080Sstevel@tonic-gate /* 17090Sstevel@tonic-gate * Specific target zone: disallow -i/-c suboptions. 17100Sstevel@tonic-gate */ 17110Sstevel@tonic-gate optind = 0; 17120Sstevel@tonic-gate while ((arg = getopt(argc, argv, "?pv")) != EOF) { 17130Sstevel@tonic-gate switch (arg) { 17140Sstevel@tonic-gate case '?': 17150Sstevel@tonic-gate sub_usage(SHELP_LIST, CMD_LIST); 17160Sstevel@tonic-gate return (optopt == '?' ? Z_OK : Z_USAGE); 17170Sstevel@tonic-gate case 'p': 17180Sstevel@tonic-gate parsable = B_TRUE; 17190Sstevel@tonic-gate break; 17200Sstevel@tonic-gate case 'v': 17210Sstevel@tonic-gate verbose = B_TRUE; 17220Sstevel@tonic-gate break; 17230Sstevel@tonic-gate default: 17240Sstevel@tonic-gate sub_usage(SHELP_LIST, CMD_LIST); 17250Sstevel@tonic-gate return (Z_USAGE); 17260Sstevel@tonic-gate } 17270Sstevel@tonic-gate } 17280Sstevel@tonic-gate if (parsable && verbose) { 17290Sstevel@tonic-gate zerror(gettext("%s -p and -v are mutually exclusive."), 17300Sstevel@tonic-gate cmd_to_str(CMD_LIST)); 17310Sstevel@tonic-gate return (Z_ERR); 17320Sstevel@tonic-gate } 17330Sstevel@tonic-gate if (argc > optind) { 17340Sstevel@tonic-gate sub_usage(SHELP_LIST, CMD_LIST); 17350Sstevel@tonic-gate return (Z_USAGE); 17360Sstevel@tonic-gate } 17370Sstevel@tonic-gate if (zone_id != GLOBAL_ZONEID) { 17380Sstevel@tonic-gate fake_up_local_zone(zone_id, &zent); 17390Sstevel@tonic-gate /* 17400Sstevel@tonic-gate * main() will issue a Z_NO_ZONE error if it cannot get an 17410Sstevel@tonic-gate * id for target_zone, which in a non-global zone should 17420Sstevel@tonic-gate * happen for any zone name except `zonename`. Thus we 17430Sstevel@tonic-gate * assert() that here but don't otherwise check. 17440Sstevel@tonic-gate */ 17450Sstevel@tonic-gate assert(strcmp(zent.zname, target_zone) == 0); 17460Sstevel@tonic-gate zone_print(&zent, verbose, parsable); 17470Sstevel@tonic-gate output = B_TRUE; 17480Sstevel@tonic-gate } else if ((zentp = lookup_running_zone(target_zone)) != NULL) { 17490Sstevel@tonic-gate zone_print(zentp, verbose, parsable); 17500Sstevel@tonic-gate output = B_TRUE; 1751766Scarlsonj } else if (lookup_zone_info(target_zone, ZONE_ID_UNDEFINED, 1752766Scarlsonj &zent) == Z_OK) { 17530Sstevel@tonic-gate zone_print(&zent, verbose, parsable); 17540Sstevel@tonic-gate output = B_TRUE; 17550Sstevel@tonic-gate } 17563339Szt129084 17573339Szt129084 /* 17583339Szt129084 * Invoke brand-specific handler. Note that we do this 17593339Szt129084 * only if we're in the global zone, and target_zone is specified. 17603339Szt129084 */ 17613339Szt129084 if (zone_id == GLOBAL_ZONEID && target_zone != NULL) 17623339Szt129084 if (invoke_brand_handler(CMD_LIST, argv) != Z_OK) 17633339Szt129084 return (Z_ERR); 17643339Szt129084 17650Sstevel@tonic-gate return (output ? Z_OK : Z_ERR); 17660Sstevel@tonic-gate } 17670Sstevel@tonic-gate 17680Sstevel@tonic-gate static void 17690Sstevel@tonic-gate sigterm(int sig) 17700Sstevel@tonic-gate { 17710Sstevel@tonic-gate /* 17720Sstevel@tonic-gate * Ignore SIG{INT,TERM}, so we don't end up in an infinite loop, 17730Sstevel@tonic-gate * then propagate the signal to our process group. 17740Sstevel@tonic-gate */ 17752712Snn35248 assert(sig == SIGINT || sig == SIGTERM); 17760Sstevel@tonic-gate (void) sigset(SIGINT, SIG_IGN); 17770Sstevel@tonic-gate (void) sigset(SIGTERM, SIG_IGN); 17780Sstevel@tonic-gate (void) kill(0, sig); 17790Sstevel@tonic-gate child_killed = B_TRUE; 17800Sstevel@tonic-gate } 17810Sstevel@tonic-gate 17820Sstevel@tonic-gate static int 17830Sstevel@tonic-gate do_subproc(char *cmdbuf) 17840Sstevel@tonic-gate { 17850Sstevel@tonic-gate char inbuf[1024]; /* arbitrary large amount */ 17860Sstevel@tonic-gate FILE *file; 17870Sstevel@tonic-gate 17882712Snn35248 do_subproc_cnt++; 17890Sstevel@tonic-gate child_killed = B_FALSE; 17900Sstevel@tonic-gate /* 17910Sstevel@tonic-gate * We use popen(3c) to launch child processes for [un]install; 17920Sstevel@tonic-gate * this library call does not return a PID, so we have to kill 17930Sstevel@tonic-gate * the whole process group. To avoid killing our parent, we 17940Sstevel@tonic-gate * become a process group leader here. But doing so can wreak 17950Sstevel@tonic-gate * havoc with reading from stdin when launched by a non-job-control 17960Sstevel@tonic-gate * shell, so we close stdin and reopen it as /dev/null first. 17970Sstevel@tonic-gate */ 17980Sstevel@tonic-gate (void) close(STDIN_FILENO); 17992712Snn35248 (void) openat(STDIN_FILENO, "/dev/null", O_RDONLY); 18002712Snn35248 if (!zoneadm_is_nested) 18012712Snn35248 (void) setpgid(0, 0); 18020Sstevel@tonic-gate (void) sigset(SIGINT, sigterm); 18030Sstevel@tonic-gate (void) sigset(SIGTERM, sigterm); 18040Sstevel@tonic-gate file = popen(cmdbuf, "r"); 18050Sstevel@tonic-gate for (;;) { 18060Sstevel@tonic-gate if (child_killed || fgets(inbuf, sizeof (inbuf), file) == NULL) 18070Sstevel@tonic-gate break; 18080Sstevel@tonic-gate (void) fputs(inbuf, stdout); 18090Sstevel@tonic-gate } 18100Sstevel@tonic-gate (void) sigset(SIGINT, SIG_DFL); 18110Sstevel@tonic-gate (void) sigset(SIGTERM, SIG_DFL); 18120Sstevel@tonic-gate return (pclose(file)); 18130Sstevel@tonic-gate } 18140Sstevel@tonic-gate 18150Sstevel@tonic-gate static int 18162712Snn35248 do_subproc_interactive(char *cmdbuf) 18172712Snn35248 { 18182712Snn35248 void (*saveint)(int); 18192712Snn35248 void (*saveterm)(int); 18202712Snn35248 void (*savequit)(int); 18212712Snn35248 void (*savehup)(int); 18222712Snn35248 int pid, child, status; 18232712Snn35248 18242712Snn35248 /* 18252712Snn35248 * do_subproc() links stdin to /dev/null, which would break any 18262712Snn35248 * interactive subprocess we try to launch here. Similarly, we 18272712Snn35248 * can't have been launched as a subprocess ourselves. 18282712Snn35248 */ 18292712Snn35248 assert(do_subproc_cnt == 0 && !zoneadm_is_nested); 18302712Snn35248 18312712Snn35248 if ((child = vfork()) == 0) { 18322712Snn35248 (void) execl("/bin/sh", "sh", "-c", cmdbuf, (char *)NULL); 18332712Snn35248 } 18342712Snn35248 18352712Snn35248 if (child == -1) 18362712Snn35248 return (-1); 18372712Snn35248 18382712Snn35248 saveint = sigset(SIGINT, SIG_IGN); 18392712Snn35248 saveterm = sigset(SIGTERM, SIG_IGN); 18402712Snn35248 savequit = sigset(SIGQUIT, SIG_IGN); 18412712Snn35248 savehup = sigset(SIGHUP, SIG_IGN); 18422712Snn35248 18432712Snn35248 while ((pid = waitpid(child, &status, 0)) != child && pid != -1) 18442712Snn35248 ; 18452712Snn35248 18462712Snn35248 (void) sigset(SIGINT, saveint); 18472712Snn35248 (void) sigset(SIGTERM, saveterm); 18482712Snn35248 (void) sigset(SIGQUIT, savequit); 18492712Snn35248 (void) sigset(SIGHUP, savehup); 18502712Snn35248 18512712Snn35248 return (pid == -1 ? -1 : status); 18522712Snn35248 } 18532712Snn35248 18542712Snn35248 static int 18552712Snn35248 subproc_status(const char *cmd, int status, boolean_t verbose_failure) 18560Sstevel@tonic-gate { 18570Sstevel@tonic-gate if (WIFEXITED(status)) { 18580Sstevel@tonic-gate int exit_code = WEXITSTATUS(status); 18590Sstevel@tonic-gate 18602712Snn35248 if ((verbose_failure) && (exit_code != ZONE_SUBPROC_OK)) 18612712Snn35248 zerror(gettext("'%s' failed with exit code %d."), cmd, 18622712Snn35248 exit_code); 18632712Snn35248 18642712Snn35248 return (exit_code); 18650Sstevel@tonic-gate } else if (WIFSIGNALED(status)) { 18660Sstevel@tonic-gate int signal = WTERMSIG(status); 18670Sstevel@tonic-gate char sigstr[SIG2STR_MAX]; 18680Sstevel@tonic-gate 18690Sstevel@tonic-gate if (sig2str(signal, sigstr) == 0) { 18700Sstevel@tonic-gate zerror(gettext("'%s' terminated by signal SIG%s."), cmd, 18710Sstevel@tonic-gate sigstr); 18720Sstevel@tonic-gate } else { 18730Sstevel@tonic-gate zerror(gettext("'%s' terminated by an unknown signal."), 18740Sstevel@tonic-gate cmd); 18750Sstevel@tonic-gate } 18760Sstevel@tonic-gate } else { 18770Sstevel@tonic-gate zerror(gettext("'%s' failed for unknown reasons."), cmd); 18780Sstevel@tonic-gate } 18792712Snn35248 18802712Snn35248 /* 18812712Snn35248 * Assume a subprocess that died due to a signal or an unknown error 18822712Snn35248 * should be considered an exit code of ZONE_SUBPROC_FATAL, as the 18832712Snn35248 * user will likely need to do some manual cleanup. 18842712Snn35248 */ 18852712Snn35248 return (ZONE_SUBPROC_FATAL); 18860Sstevel@tonic-gate } 18870Sstevel@tonic-gate 18880Sstevel@tonic-gate /* 18890Sstevel@tonic-gate * Various sanity checks; make sure: 18900Sstevel@tonic-gate * 1. We're in the global zone. 18910Sstevel@tonic-gate * 2. The calling user has sufficient privilege. 18920Sstevel@tonic-gate * 3. The target zone is neither the global zone nor anything starting with 18930Sstevel@tonic-gate * "SUNW". 18940Sstevel@tonic-gate * 4a. If we're looking for a 'not running' (i.e., configured or installed) 18950Sstevel@tonic-gate * zone, the name service knows about it. 18960Sstevel@tonic-gate * 4b. For some operations which expect a zone not to be running, that it is 18970Sstevel@tonic-gate * not already running (or ready). 18980Sstevel@tonic-gate */ 18990Sstevel@tonic-gate static int 19000Sstevel@tonic-gate sanity_check(char *zone, int cmd_num, boolean_t running, 19012712Snn35248 boolean_t unsafe_when_running, boolean_t force) 19020Sstevel@tonic-gate { 19030Sstevel@tonic-gate zone_entry_t *zent; 19040Sstevel@tonic-gate priv_set_t *privset; 19052712Snn35248 zone_state_t state, min_state; 1906766Scarlsonj char kernzone[ZONENAME_MAX]; 1907766Scarlsonj FILE *fp; 19080Sstevel@tonic-gate 19090Sstevel@tonic-gate if (getzoneid() != GLOBAL_ZONEID) { 19101645Scomay switch (cmd_num) { 19111645Scomay case CMD_HALT: 19121645Scomay zerror(gettext("use %s to %s this zone."), "halt(1M)", 19131645Scomay cmd_to_str(cmd_num)); 19141645Scomay break; 19151645Scomay case CMD_REBOOT: 19161645Scomay zerror(gettext("use %s to %s this zone."), 19171645Scomay "reboot(1M)", cmd_to_str(cmd_num)); 19181645Scomay break; 19191645Scomay default: 19201645Scomay zerror(gettext("must be in the global zone to %s a " 19211645Scomay "zone."), cmd_to_str(cmd_num)); 19221645Scomay break; 19231645Scomay } 19240Sstevel@tonic-gate return (Z_ERR); 19250Sstevel@tonic-gate } 19260Sstevel@tonic-gate 19270Sstevel@tonic-gate if ((privset = priv_allocset()) == NULL) { 19280Sstevel@tonic-gate zerror(gettext("%s failed"), "priv_allocset"); 19290Sstevel@tonic-gate return (Z_ERR); 19300Sstevel@tonic-gate } 19310Sstevel@tonic-gate 19320Sstevel@tonic-gate if (getppriv(PRIV_EFFECTIVE, privset) != 0) { 19330Sstevel@tonic-gate zerror(gettext("%s failed"), "getppriv"); 19340Sstevel@tonic-gate priv_freeset(privset); 19350Sstevel@tonic-gate return (Z_ERR); 19360Sstevel@tonic-gate } 19370Sstevel@tonic-gate 19380Sstevel@tonic-gate if (priv_isfullset(privset) == B_FALSE) { 19390Sstevel@tonic-gate zerror(gettext("only a privileged user may %s a zone."), 19400Sstevel@tonic-gate cmd_to_str(cmd_num)); 19410Sstevel@tonic-gate priv_freeset(privset); 19420Sstevel@tonic-gate return (Z_ERR); 19430Sstevel@tonic-gate } 19440Sstevel@tonic-gate priv_freeset(privset); 19450Sstevel@tonic-gate 19460Sstevel@tonic-gate if (zone == NULL) { 19470Sstevel@tonic-gate zerror(gettext("no zone specified")); 19480Sstevel@tonic-gate return (Z_ERR); 19490Sstevel@tonic-gate } 19500Sstevel@tonic-gate 19510Sstevel@tonic-gate if (strcmp(zone, GLOBAL_ZONENAME) == 0) { 19520Sstevel@tonic-gate zerror(gettext("%s operation is invalid for the global zone."), 19530Sstevel@tonic-gate cmd_to_str(cmd_num)); 19540Sstevel@tonic-gate return (Z_ERR); 19550Sstevel@tonic-gate } 19560Sstevel@tonic-gate 19570Sstevel@tonic-gate if (strncmp(zone, "SUNW", 4) == 0) { 19580Sstevel@tonic-gate zerror(gettext("%s operation is invalid for zones starting " 19590Sstevel@tonic-gate "with SUNW."), cmd_to_str(cmd_num)); 19600Sstevel@tonic-gate return (Z_ERR); 19610Sstevel@tonic-gate } 19620Sstevel@tonic-gate 19632712Snn35248 if (!is_native_zone && cmd_num == CMD_MOUNT) { 19642712Snn35248 zerror(gettext("%s operation is invalid for branded zones."), 19652712Snn35248 cmd_to_str(cmd_num)); 19662712Snn35248 return (Z_ERR); 19672712Snn35248 } 19682712Snn35248 1969766Scarlsonj if (!zonecfg_in_alt_root()) { 1970766Scarlsonj zent = lookup_running_zone(zone); 1971766Scarlsonj } else if ((fp = zonecfg_open_scratch("", B_FALSE)) == NULL) { 1972766Scarlsonj zent = NULL; 1973766Scarlsonj } else { 1974766Scarlsonj if (zonecfg_find_scratch(fp, zone, zonecfg_get_root(), 1975766Scarlsonj kernzone, sizeof (kernzone)) == 0) 1976766Scarlsonj zent = lookup_running_zone(kernzone); 1977766Scarlsonj else 1978766Scarlsonj zent = NULL; 1979766Scarlsonj zonecfg_close_scratch(fp); 1980766Scarlsonj } 1981766Scarlsonj 19820Sstevel@tonic-gate /* 19830Sstevel@tonic-gate * Look up from the kernel for 'running' zones. 19840Sstevel@tonic-gate */ 19852712Snn35248 if (running && !force) { 19860Sstevel@tonic-gate if (zent == NULL) { 19870Sstevel@tonic-gate zerror(gettext("not running")); 19880Sstevel@tonic-gate return (Z_ERR); 19890Sstevel@tonic-gate } 19900Sstevel@tonic-gate } else { 19910Sstevel@tonic-gate int err; 19920Sstevel@tonic-gate 19930Sstevel@tonic-gate if (unsafe_when_running && zent != NULL) { 19940Sstevel@tonic-gate /* check whether the zone is ready or running */ 19950Sstevel@tonic-gate if ((err = zone_get_state(zent->zname, 19960Sstevel@tonic-gate &zent->zstate_num)) != Z_OK) { 19970Sstevel@tonic-gate errno = err; 19980Sstevel@tonic-gate zperror2(zent->zname, 19990Sstevel@tonic-gate gettext("could not get state")); 20000Sstevel@tonic-gate /* can't tell, so hedge */ 20010Sstevel@tonic-gate zent->zstate_str = "ready/running"; 20020Sstevel@tonic-gate } else { 20030Sstevel@tonic-gate zent->zstate_str = 20040Sstevel@tonic-gate zone_state_str(zent->zstate_num); 20050Sstevel@tonic-gate } 20060Sstevel@tonic-gate zerror(gettext("%s operation is invalid for %s zones."), 20070Sstevel@tonic-gate cmd_to_str(cmd_num), zent->zstate_str); 20080Sstevel@tonic-gate return (Z_ERR); 20090Sstevel@tonic-gate } 20100Sstevel@tonic-gate if ((err = zone_get_state(zone, &state)) != Z_OK) { 20110Sstevel@tonic-gate errno = err; 20120Sstevel@tonic-gate zperror2(zone, gettext("could not get state")); 20130Sstevel@tonic-gate return (Z_ERR); 20140Sstevel@tonic-gate } 20150Sstevel@tonic-gate switch (cmd_num) { 20160Sstevel@tonic-gate case CMD_UNINSTALL: 20170Sstevel@tonic-gate if (state == ZONE_STATE_CONFIGURED) { 20180Sstevel@tonic-gate zerror(gettext("is already in state '%s'."), 20190Sstevel@tonic-gate zone_state_str(ZONE_STATE_CONFIGURED)); 20200Sstevel@tonic-gate return (Z_ERR); 20210Sstevel@tonic-gate } 20220Sstevel@tonic-gate break; 20231507Sgjelinek case CMD_ATTACH: 20241300Sgjelinek case CMD_CLONE: 20250Sstevel@tonic-gate case CMD_INSTALL: 20260Sstevel@tonic-gate if (state == ZONE_STATE_INSTALLED) { 20270Sstevel@tonic-gate zerror(gettext("is already %s."), 20280Sstevel@tonic-gate zone_state_str(ZONE_STATE_INSTALLED)); 20290Sstevel@tonic-gate return (Z_ERR); 20300Sstevel@tonic-gate } else if (state == ZONE_STATE_INCOMPLETE) { 20310Sstevel@tonic-gate zerror(gettext("zone is %s; %s required."), 20320Sstevel@tonic-gate zone_state_str(ZONE_STATE_INCOMPLETE), 20330Sstevel@tonic-gate cmd_to_str(CMD_UNINSTALL)); 20340Sstevel@tonic-gate return (Z_ERR); 20350Sstevel@tonic-gate } 20360Sstevel@tonic-gate break; 20371507Sgjelinek case CMD_DETACH: 20381300Sgjelinek case CMD_MOVE: 20390Sstevel@tonic-gate case CMD_READY: 20400Sstevel@tonic-gate case CMD_BOOT: 2041766Scarlsonj case CMD_MOUNT: 20422303Scarlsonj case CMD_MARK: 20432712Snn35248 if ((cmd_num == CMD_BOOT || cmd_num == CMD_MOUNT) && 20442712Snn35248 force) 20452712Snn35248 min_state = ZONE_STATE_INCOMPLETE; 20462712Snn35248 else 20472712Snn35248 min_state = ZONE_STATE_INSTALLED; 20482712Snn35248 20492712Snn35248 if (force && cmd_num == CMD_BOOT && is_native_zone) { 20502712Snn35248 zerror(gettext("Only branded zones may be " 20512712Snn35248 "force-booted.")); 20522712Snn35248 return (Z_ERR); 20532712Snn35248 } 20542712Snn35248 20552712Snn35248 if (state < min_state) { 20560Sstevel@tonic-gate zerror(gettext("must be %s before %s."), 20572712Snn35248 zone_state_str(min_state), 20580Sstevel@tonic-gate cmd_to_str(cmd_num)); 20590Sstevel@tonic-gate return (Z_ERR); 20600Sstevel@tonic-gate } 20610Sstevel@tonic-gate break; 20620Sstevel@tonic-gate case CMD_VERIFY: 20630Sstevel@tonic-gate if (state == ZONE_STATE_INCOMPLETE) { 20640Sstevel@tonic-gate zerror(gettext("zone is %s; %s required."), 20650Sstevel@tonic-gate zone_state_str(ZONE_STATE_INCOMPLETE), 20660Sstevel@tonic-gate cmd_to_str(CMD_UNINSTALL)); 20670Sstevel@tonic-gate return (Z_ERR); 20680Sstevel@tonic-gate } 20690Sstevel@tonic-gate break; 2070766Scarlsonj case CMD_UNMOUNT: 2071766Scarlsonj if (state != ZONE_STATE_MOUNTED) { 2072766Scarlsonj zerror(gettext("must be %s before %s."), 2073766Scarlsonj zone_state_str(ZONE_STATE_MOUNTED), 2074766Scarlsonj cmd_to_str(cmd_num)); 2075766Scarlsonj return (Z_ERR); 2076766Scarlsonj } 2077766Scarlsonj break; 20780Sstevel@tonic-gate } 20790Sstevel@tonic-gate } 20800Sstevel@tonic-gate return (Z_OK); 20810Sstevel@tonic-gate } 20820Sstevel@tonic-gate 20830Sstevel@tonic-gate static int 20840Sstevel@tonic-gate halt_func(int argc, char *argv[]) 20850Sstevel@tonic-gate { 20860Sstevel@tonic-gate zone_cmd_arg_t zarg; 20870Sstevel@tonic-gate int arg; 20880Sstevel@tonic-gate 2089766Scarlsonj if (zonecfg_in_alt_root()) { 2090766Scarlsonj zerror(gettext("cannot halt zone in alternate root")); 2091766Scarlsonj return (Z_ERR); 2092766Scarlsonj } 2093766Scarlsonj 20940Sstevel@tonic-gate optind = 0; 20950Sstevel@tonic-gate if ((arg = getopt(argc, argv, "?")) != EOF) { 20960Sstevel@tonic-gate switch (arg) { 20970Sstevel@tonic-gate case '?': 20980Sstevel@tonic-gate sub_usage(SHELP_HALT, CMD_HALT); 20990Sstevel@tonic-gate return (optopt == '?' ? Z_OK : Z_USAGE); 21000Sstevel@tonic-gate default: 21010Sstevel@tonic-gate sub_usage(SHELP_HALT, CMD_HALT); 21020Sstevel@tonic-gate return (Z_USAGE); 21030Sstevel@tonic-gate } 21040Sstevel@tonic-gate } 21050Sstevel@tonic-gate if (argc > optind) { 21060Sstevel@tonic-gate sub_usage(SHELP_HALT, CMD_HALT); 21070Sstevel@tonic-gate return (Z_USAGE); 21080Sstevel@tonic-gate } 21090Sstevel@tonic-gate /* 21100Sstevel@tonic-gate * zoneadmd should be the one to decide whether or not to proceed, 21110Sstevel@tonic-gate * so even though it seems that the fourth parameter below should 21120Sstevel@tonic-gate * perhaps be B_TRUE, it really shouldn't be. 21130Sstevel@tonic-gate */ 21142712Snn35248 if (sanity_check(target_zone, CMD_HALT, B_FALSE, B_FALSE, B_FALSE) 21152712Snn35248 != Z_OK) 21160Sstevel@tonic-gate return (Z_ERR); 21170Sstevel@tonic-gate 21183339Szt129084 /* 21193339Szt129084 * Invoke brand-specific handler. 21203339Szt129084 */ 21213339Szt129084 if (invoke_brand_handler(CMD_HALT, argv) != Z_OK) 21223339Szt129084 return (Z_ERR); 21233339Szt129084 21240Sstevel@tonic-gate zarg.cmd = Z_HALT; 21250Sstevel@tonic-gate return ((call_zoneadmd(target_zone, &zarg) == 0) ? Z_OK : Z_ERR); 21260Sstevel@tonic-gate } 21270Sstevel@tonic-gate 21280Sstevel@tonic-gate static int 21290Sstevel@tonic-gate reboot_func(int argc, char *argv[]) 21300Sstevel@tonic-gate { 21310Sstevel@tonic-gate zone_cmd_arg_t zarg; 21320Sstevel@tonic-gate int arg; 21330Sstevel@tonic-gate 2134766Scarlsonj if (zonecfg_in_alt_root()) { 2135766Scarlsonj zerror(gettext("cannot reboot zone in alternate root")); 2136766Scarlsonj return (Z_ERR); 2137766Scarlsonj } 2138766Scarlsonj 21390Sstevel@tonic-gate optind = 0; 21400Sstevel@tonic-gate if ((arg = getopt(argc, argv, "?")) != EOF) { 21410Sstevel@tonic-gate switch (arg) { 21420Sstevel@tonic-gate case '?': 21430Sstevel@tonic-gate sub_usage(SHELP_REBOOT, CMD_REBOOT); 21440Sstevel@tonic-gate return (optopt == '?' ? Z_OK : Z_USAGE); 21450Sstevel@tonic-gate default: 21460Sstevel@tonic-gate sub_usage(SHELP_REBOOT, CMD_REBOOT); 21470Sstevel@tonic-gate return (Z_USAGE); 21480Sstevel@tonic-gate } 21490Sstevel@tonic-gate } 21502267Sdp 21512267Sdp zarg.bootbuf[0] = '\0'; 21522267Sdp for (; optind < argc; optind++) { 21532267Sdp if (strlcat(zarg.bootbuf, argv[optind], 21542267Sdp sizeof (zarg.bootbuf)) >= sizeof (zarg.bootbuf)) { 21552267Sdp zerror(gettext("Boot argument list too long")); 21562267Sdp return (Z_ERR); 21572267Sdp } 21582267Sdp if (optind < argc - 1) 21592267Sdp if (strlcat(zarg.bootbuf, " ", sizeof (zarg.bootbuf)) >= 21602267Sdp sizeof (zarg.bootbuf)) { 21612267Sdp zerror(gettext("Boot argument list too long")); 21622267Sdp return (Z_ERR); 21632267Sdp } 21642267Sdp } 21652267Sdp 21662267Sdp 21670Sstevel@tonic-gate /* 21680Sstevel@tonic-gate * zoneadmd should be the one to decide whether or not to proceed, 21690Sstevel@tonic-gate * so even though it seems that the fourth parameter below should 21700Sstevel@tonic-gate * perhaps be B_TRUE, it really shouldn't be. 21710Sstevel@tonic-gate */ 21722712Snn35248 if (sanity_check(target_zone, CMD_REBOOT, B_TRUE, B_FALSE, B_FALSE) 21732712Snn35248 != Z_OK) 21740Sstevel@tonic-gate return (Z_ERR); 21753339Szt129084 if (verify_details(CMD_REBOOT, argv) != Z_OK) 2176823Sgjelinek return (Z_ERR); 21770Sstevel@tonic-gate 21780Sstevel@tonic-gate zarg.cmd = Z_REBOOT; 21790Sstevel@tonic-gate return ((call_zoneadmd(target_zone, &zarg) == 0) ? Z_OK : Z_ERR); 21800Sstevel@tonic-gate } 21810Sstevel@tonic-gate 21820Sstevel@tonic-gate static int 21833339Szt129084 verify_brand(zone_dochandle_t handle, int cmd_num, char *argv[]) 21842712Snn35248 { 21852712Snn35248 char cmdbuf[MAXPATHLEN]; 21862712Snn35248 int err; 21872712Snn35248 char zonepath[MAXPATHLEN]; 21882727Sedp brand_handle_t bh = NULL; 21893339Szt129084 int status, i; 21902712Snn35248 21912712Snn35248 /* 21922712Snn35248 * Fetch the verify command from the brand configuration. 21932712Snn35248 * "exec" the command so that the returned status is that of 21942712Snn35248 * the command and not the shell. 21952712Snn35248 */ 21962712Snn35248 if ((err = zonecfg_get_zonepath(handle, zonepath, sizeof (zonepath))) != 21972712Snn35248 Z_OK) { 21982712Snn35248 errno = err; 21993339Szt129084 zperror(cmd_to_str(cmd_num), B_TRUE); 22002712Snn35248 return (Z_ERR); 22012712Snn35248 } 22022727Sedp if ((bh = brand_open(target_brand)) == NULL) { 22032712Snn35248 zerror(gettext("missing or invalid brand")); 22042712Snn35248 return (Z_ERR); 22052712Snn35248 } 22062712Snn35248 22072712Snn35248 /* 22082712Snn35248 * If the brand has its own verification routine, execute it now. 22093339Szt129084 * The verification routine validates the intended zoneadm 22103339Szt129084 * operation for the specific brand. The zoneadm subcommand and 22113339Szt129084 * all its arguments are passed to the routine. 22122712Snn35248 */ 22132712Snn35248 (void) strcpy(cmdbuf, EXEC_PREFIX); 22142727Sedp err = brand_get_verify_adm(bh, target_zone, zonepath, 22152712Snn35248 cmdbuf + EXEC_LEN, sizeof (cmdbuf) - EXEC_LEN, 0, NULL); 22162727Sedp brand_close(bh); 22173339Szt129084 if (err != 0) 22183339Szt129084 return (Z_BRAND_ERROR); 22193339Szt129084 if (strlen(cmdbuf) <= EXEC_LEN) 22203339Szt129084 return (Z_OK); 22213339Szt129084 22223339Szt129084 if (strlcat(cmdbuf, cmd_to_str(cmd_num), 22233339Szt129084 sizeof (cmdbuf)) >= sizeof (cmdbuf)) 22243339Szt129084 return (Z_ERR); 22253339Szt129084 22263339Szt129084 /* Build the argv string */ 22273339Szt129084 i = 0; 22283339Szt129084 while (argv[i] != NULL) { 22293339Szt129084 if ((strlcat(cmdbuf, " ", 22303339Szt129084 sizeof (cmdbuf)) >= sizeof (cmdbuf)) || 22313339Szt129084 (strlcat(cmdbuf, argv[i++], 22323339Szt129084 sizeof (cmdbuf)) >= sizeof (cmdbuf))) 22333339Szt129084 return (Z_ERR); 22343339Szt129084 } 22353339Szt129084 22363339Szt129084 status = do_subproc_interactive(cmdbuf); 22373339Szt129084 err = subproc_status(gettext("brand-specific verification"), 22383339Szt129084 status, B_FALSE); 22393339Szt129084 22403339Szt129084 return ((err == ZONE_SUBPROC_OK) ? Z_OK : Z_BRAND_ERROR); 22412712Snn35248 } 22422712Snn35248 22432712Snn35248 static int 22440Sstevel@tonic-gate verify_rctls(zone_dochandle_t handle) 22450Sstevel@tonic-gate { 22460Sstevel@tonic-gate struct zone_rctltab rctltab; 22470Sstevel@tonic-gate size_t rbs = rctlblk_size(); 22480Sstevel@tonic-gate rctlblk_t *rctlblk; 22490Sstevel@tonic-gate int error = Z_INVAL; 22500Sstevel@tonic-gate 22510Sstevel@tonic-gate if ((rctlblk = malloc(rbs)) == NULL) { 22520Sstevel@tonic-gate zerror(gettext("failed to allocate %lu bytes: %s"), rbs, 22530Sstevel@tonic-gate strerror(errno)); 22540Sstevel@tonic-gate return (Z_NOMEM); 22550Sstevel@tonic-gate } 22560Sstevel@tonic-gate 22570Sstevel@tonic-gate if (zonecfg_setrctlent(handle) != Z_OK) { 22580Sstevel@tonic-gate zerror(gettext("zonecfg_setrctlent failed")); 22590Sstevel@tonic-gate free(rctlblk); 22600Sstevel@tonic-gate return (error); 22610Sstevel@tonic-gate } 22620Sstevel@tonic-gate 22630Sstevel@tonic-gate rctltab.zone_rctl_valptr = NULL; 22640Sstevel@tonic-gate while (zonecfg_getrctlent(handle, &rctltab) == Z_OK) { 22650Sstevel@tonic-gate struct zone_rctlvaltab *rctlval; 22660Sstevel@tonic-gate const char *name = rctltab.zone_rctl_name; 22670Sstevel@tonic-gate 22680Sstevel@tonic-gate if (!zonecfg_is_rctl(name)) { 22690Sstevel@tonic-gate zerror(gettext("WARNING: Ignoring unrecognized rctl " 22700Sstevel@tonic-gate "'%s'."), name); 22710Sstevel@tonic-gate zonecfg_free_rctl_value_list(rctltab.zone_rctl_valptr); 22720Sstevel@tonic-gate rctltab.zone_rctl_valptr = NULL; 22730Sstevel@tonic-gate continue; 22740Sstevel@tonic-gate } 22750Sstevel@tonic-gate 22760Sstevel@tonic-gate for (rctlval = rctltab.zone_rctl_valptr; rctlval != NULL; 22770Sstevel@tonic-gate rctlval = rctlval->zone_rctlval_next) { 22780Sstevel@tonic-gate if (zonecfg_construct_rctlblk(rctlval, rctlblk) 22790Sstevel@tonic-gate != Z_OK) { 22800Sstevel@tonic-gate zerror(gettext("invalid rctl value: " 22810Sstevel@tonic-gate "(priv=%s,limit=%s,action%s)"), 22820Sstevel@tonic-gate rctlval->zone_rctlval_priv, 22830Sstevel@tonic-gate rctlval->zone_rctlval_limit, 22840Sstevel@tonic-gate rctlval->zone_rctlval_action); 22850Sstevel@tonic-gate goto out; 22860Sstevel@tonic-gate } 22870Sstevel@tonic-gate if (!zonecfg_valid_rctl(name, rctlblk)) { 22880Sstevel@tonic-gate zerror(gettext("(priv=%s,limit=%s,action=%s) " 22890Sstevel@tonic-gate "is not a valid value for rctl '%s'"), 22900Sstevel@tonic-gate rctlval->zone_rctlval_priv, 22910Sstevel@tonic-gate rctlval->zone_rctlval_limit, 22920Sstevel@tonic-gate rctlval->zone_rctlval_action, 22930Sstevel@tonic-gate name); 22940Sstevel@tonic-gate goto out; 22950Sstevel@tonic-gate } 22960Sstevel@tonic-gate } 22970Sstevel@tonic-gate zonecfg_free_rctl_value_list(rctltab.zone_rctl_valptr); 22980Sstevel@tonic-gate } 22990Sstevel@tonic-gate rctltab.zone_rctl_valptr = NULL; 23000Sstevel@tonic-gate error = Z_OK; 23010Sstevel@tonic-gate out: 23020Sstevel@tonic-gate zonecfg_free_rctl_value_list(rctltab.zone_rctl_valptr); 23030Sstevel@tonic-gate (void) zonecfg_endrctlent(handle); 23040Sstevel@tonic-gate free(rctlblk); 23050Sstevel@tonic-gate return (error); 23060Sstevel@tonic-gate } 23070Sstevel@tonic-gate 23080Sstevel@tonic-gate static int 23090Sstevel@tonic-gate verify_pool(zone_dochandle_t handle) 23100Sstevel@tonic-gate { 23110Sstevel@tonic-gate char poolname[MAXPATHLEN]; 23120Sstevel@tonic-gate pool_conf_t *poolconf; 23130Sstevel@tonic-gate pool_t *pool; 23140Sstevel@tonic-gate int status; 23150Sstevel@tonic-gate int error; 23160Sstevel@tonic-gate 23170Sstevel@tonic-gate /* 23180Sstevel@tonic-gate * This ends up being very similar to the check done in zoneadmd. 23190Sstevel@tonic-gate */ 23200Sstevel@tonic-gate error = zonecfg_get_pool(handle, poolname, sizeof (poolname)); 23210Sstevel@tonic-gate if (error == Z_NO_ENTRY || (error == Z_OK && strlen(poolname) == 0)) { 23220Sstevel@tonic-gate /* 23230Sstevel@tonic-gate * No pool specified. 23240Sstevel@tonic-gate */ 23250Sstevel@tonic-gate return (0); 23260Sstevel@tonic-gate } 23270Sstevel@tonic-gate if (error != Z_OK) { 23280Sstevel@tonic-gate zperror(gettext("Unable to retrieve pool name from " 23290Sstevel@tonic-gate "configuration"), B_TRUE); 23300Sstevel@tonic-gate return (error); 23310Sstevel@tonic-gate } 23320Sstevel@tonic-gate /* 23330Sstevel@tonic-gate * Don't do anything if pools aren't enabled. 23340Sstevel@tonic-gate */ 23350Sstevel@tonic-gate if (pool_get_status(&status) != PO_SUCCESS || status != POOL_ENABLED) { 23360Sstevel@tonic-gate zerror(gettext("WARNING: pools facility not active; " 23370Sstevel@tonic-gate "zone will not be bound to pool '%s'."), poolname); 23380Sstevel@tonic-gate return (Z_OK); 23390Sstevel@tonic-gate } 23400Sstevel@tonic-gate /* 23410Sstevel@tonic-gate * Try to provide a sane error message if the requested pool doesn't 23420Sstevel@tonic-gate * exist. It isn't clear that pools-related failures should 23430Sstevel@tonic-gate * necessarily translate to a failure to verify the zone configuration, 23440Sstevel@tonic-gate * hence they are not considered errors. 23450Sstevel@tonic-gate */ 23460Sstevel@tonic-gate if ((poolconf = pool_conf_alloc()) == NULL) { 23470Sstevel@tonic-gate zerror(gettext("WARNING: pool_conf_alloc failed; " 23480Sstevel@tonic-gate "using default pool")); 23490Sstevel@tonic-gate return (Z_OK); 23500Sstevel@tonic-gate } 23510Sstevel@tonic-gate if (pool_conf_open(poolconf, pool_dynamic_location(), PO_RDONLY) != 23520Sstevel@tonic-gate PO_SUCCESS) { 23530Sstevel@tonic-gate zerror(gettext("WARNING: pool_conf_open failed; " 23540Sstevel@tonic-gate "using default pool")); 23550Sstevel@tonic-gate pool_conf_free(poolconf); 23560Sstevel@tonic-gate return (Z_OK); 23570Sstevel@tonic-gate } 23580Sstevel@tonic-gate pool = pool_get_pool(poolconf, poolname); 23590Sstevel@tonic-gate (void) pool_conf_close(poolconf); 23600Sstevel@tonic-gate pool_conf_free(poolconf); 23610Sstevel@tonic-gate if (pool == NULL) { 23620Sstevel@tonic-gate zerror(gettext("WARNING: pool '%s' not found. " 23630Sstevel@tonic-gate "using default pool"), poolname); 23640Sstevel@tonic-gate } 23650Sstevel@tonic-gate 23660Sstevel@tonic-gate return (Z_OK); 23670Sstevel@tonic-gate } 23680Sstevel@tonic-gate 23690Sstevel@tonic-gate static int 2370823Sgjelinek verify_ipd(zone_dochandle_t handle) 2371823Sgjelinek { 2372823Sgjelinek int return_code = Z_OK; 2373823Sgjelinek struct zone_fstab fstab; 2374823Sgjelinek struct stat st; 2375823Sgjelinek char specdir[MAXPATHLEN]; 2376823Sgjelinek 2377823Sgjelinek if (zonecfg_setipdent(handle) != Z_OK) { 2378924Sgjelinek /* 2379924Sgjelinek * TRANSLATION_NOTE 2380924Sgjelinek * inherit-pkg-dirs is a literal that should not be translated. 2381924Sgjelinek */ 2382924Sgjelinek (void) fprintf(stderr, gettext("could not verify " 2383823Sgjelinek "inherit-pkg-dirs: unable to enumerate mounts\n")); 2384823Sgjelinek return (Z_ERR); 2385823Sgjelinek } 2386823Sgjelinek while (zonecfg_getipdent(handle, &fstab) == Z_OK) { 2387823Sgjelinek /* 2388823Sgjelinek * Verify fs_dir exists. 2389823Sgjelinek */ 2390823Sgjelinek (void) snprintf(specdir, sizeof (specdir), "%s%s", 2391823Sgjelinek zonecfg_get_root(), fstab.zone_fs_dir); 2392823Sgjelinek if (stat(specdir, &st) != 0) { 2393924Sgjelinek /* 2394924Sgjelinek * TRANSLATION_NOTE 2395924Sgjelinek * inherit-pkg-dir is a literal that should not be 2396924Sgjelinek * translated. 2397924Sgjelinek */ 2398924Sgjelinek (void) fprintf(stderr, gettext("could not verify " 2399823Sgjelinek "inherit-pkg-dir %s: %s\n"), 2400823Sgjelinek fstab.zone_fs_dir, strerror(errno)); 2401823Sgjelinek return_code = Z_ERR; 2402823Sgjelinek } 2403823Sgjelinek if (strcmp(st.st_fstype, MNTTYPE_NFS) == 0) { 2404924Sgjelinek /* 2405924Sgjelinek * TRANSLATION_NOTE 2406924Sgjelinek * inherit-pkg-dir and NFS are literals that should 2407924Sgjelinek * not be translated. 2408924Sgjelinek */ 2409823Sgjelinek (void) fprintf(stderr, gettext("cannot verify " 24101867Sgjelinek "inherit-pkg-dir %s: NFS mounted file system.\n" 24111867Sgjelinek "\tA local file system must be used.\n"), 2412823Sgjelinek fstab.zone_fs_dir); 2413823Sgjelinek return_code = Z_ERR; 2414823Sgjelinek } 2415823Sgjelinek } 2416823Sgjelinek (void) zonecfg_endipdent(handle); 2417823Sgjelinek 2418823Sgjelinek return (return_code); 2419823Sgjelinek } 2420823Sgjelinek 24211393Slling /* 24221867Sgjelinek * Verify that the special device/file system exists and is valid. 24231393Slling */ 24241393Slling static int 24251393Slling verify_fs_special(struct zone_fstab *fstab) 24261393Slling { 24271393Slling struct stat st; 24281393Slling 24292971Sgjelinek /* 24302971Sgjelinek * This validation is really intended for standard zone administration. 24312971Sgjelinek * If we are in a mini-root or some other upgrade situation where 24322971Sgjelinek * we are using the scratch zone, just by-pass this. 24332971Sgjelinek */ 24342971Sgjelinek if (zonecfg_in_alt_root()) 24352971Sgjelinek return (Z_OK); 24362971Sgjelinek 24371393Slling if (strcmp(fstab->zone_fs_type, MNTTYPE_ZFS) == 0) 24381393Slling return (verify_fs_zfs(fstab)); 24391393Slling 24401393Slling if (stat(fstab->zone_fs_special, &st) != 0) { 24411397Slling (void) fprintf(stderr, gettext("could not verify fs " 24421393Slling "%s: could not access %s: %s\n"), fstab->zone_fs_dir, 24431393Slling fstab->zone_fs_special, strerror(errno)); 24441393Slling return (Z_ERR); 24451393Slling } 24461393Slling 24471393Slling if (strcmp(st.st_fstype, MNTTYPE_NFS) == 0) { 24481393Slling /* 24491393Slling * TRANSLATION_NOTE 24501393Slling * fs and NFS are literals that should 24511393Slling * not be translated. 24521393Slling */ 24531393Slling (void) fprintf(stderr, gettext("cannot verify " 24541867Sgjelinek "fs %s: NFS mounted file system.\n" 24551867Sgjelinek "\tA local file system must be used.\n"), 24561393Slling fstab->zone_fs_special); 24571393Slling return (Z_ERR); 24581393Slling } 24591393Slling 24601393Slling return (Z_OK); 24611393Slling } 24621393Slling 2463823Sgjelinek static int 24640Sstevel@tonic-gate verify_filesystems(zone_dochandle_t handle) 24650Sstevel@tonic-gate { 24660Sstevel@tonic-gate int return_code = Z_OK; 24670Sstevel@tonic-gate struct zone_fstab fstab; 24680Sstevel@tonic-gate char cmdbuf[MAXPATHLEN]; 24690Sstevel@tonic-gate struct stat st; 24700Sstevel@tonic-gate 24710Sstevel@tonic-gate /* 24720Sstevel@tonic-gate * No need to verify inherit-pkg-dir fs types, as their type is 24730Sstevel@tonic-gate * implicitly lofs, which is known. Therefore, the types are only 24741867Sgjelinek * verified for regular file systems below. 24750Sstevel@tonic-gate * 24760Sstevel@tonic-gate * Since the actual mount point is not known until the dependent mounts 24770Sstevel@tonic-gate * are performed, we don't attempt any path validation here: that will 24780Sstevel@tonic-gate * happen later when zoneadmd actually does the mounts. 24790Sstevel@tonic-gate */ 24800Sstevel@tonic-gate if (zonecfg_setfsent(handle) != Z_OK) { 24811867Sgjelinek (void) fprintf(stderr, gettext("could not verify file systems: " 24820Sstevel@tonic-gate "unable to enumerate mounts\n")); 24830Sstevel@tonic-gate return (Z_ERR); 24840Sstevel@tonic-gate } 24850Sstevel@tonic-gate while (zonecfg_getfsent(handle, &fstab) == Z_OK) { 24860Sstevel@tonic-gate if (!zonecfg_valid_fs_type(fstab.zone_fs_type)) { 24870Sstevel@tonic-gate (void) fprintf(stderr, gettext("cannot verify fs %s: " 24880Sstevel@tonic-gate "type %s is not allowed.\n"), fstab.zone_fs_dir, 24890Sstevel@tonic-gate fstab.zone_fs_type); 24900Sstevel@tonic-gate return_code = Z_ERR; 24910Sstevel@tonic-gate goto next_fs; 24920Sstevel@tonic-gate } 24930Sstevel@tonic-gate /* 24940Sstevel@tonic-gate * Verify /usr/lib/fs/<fstype>/mount exists. 24950Sstevel@tonic-gate */ 24960Sstevel@tonic-gate if (snprintf(cmdbuf, sizeof (cmdbuf), "/usr/lib/fs/%s/mount", 24970Sstevel@tonic-gate fstab.zone_fs_type) > sizeof (cmdbuf)) { 24980Sstevel@tonic-gate (void) fprintf(stderr, gettext("cannot verify fs %s: " 24990Sstevel@tonic-gate "type %s is too long.\n"), fstab.zone_fs_dir, 25000Sstevel@tonic-gate fstab.zone_fs_type); 25010Sstevel@tonic-gate return_code = Z_ERR; 25020Sstevel@tonic-gate goto next_fs; 25030Sstevel@tonic-gate } 25040Sstevel@tonic-gate if (stat(cmdbuf, &st) != 0) { 2505924Sgjelinek (void) fprintf(stderr, gettext("could not verify fs " 2506924Sgjelinek "%s: could not access %s: %s\n"), fstab.zone_fs_dir, 25070Sstevel@tonic-gate cmdbuf, strerror(errno)); 25080Sstevel@tonic-gate return_code = Z_ERR; 25090Sstevel@tonic-gate goto next_fs; 25100Sstevel@tonic-gate } 25110Sstevel@tonic-gate if (!S_ISREG(st.st_mode)) { 2512924Sgjelinek (void) fprintf(stderr, gettext("could not verify fs " 2513924Sgjelinek "%s: %s is not a regular file\n"), 2514924Sgjelinek fstab.zone_fs_dir, cmdbuf); 25150Sstevel@tonic-gate return_code = Z_ERR; 25160Sstevel@tonic-gate goto next_fs; 25170Sstevel@tonic-gate } 25180Sstevel@tonic-gate /* 25190Sstevel@tonic-gate * Verify /usr/lib/fs/<fstype>/fsck exists iff zone_fs_raw is 25200Sstevel@tonic-gate * set. 25210Sstevel@tonic-gate */ 25220Sstevel@tonic-gate if (snprintf(cmdbuf, sizeof (cmdbuf), "/usr/lib/fs/%s/fsck", 25230Sstevel@tonic-gate fstab.zone_fs_type) > sizeof (cmdbuf)) { 25240Sstevel@tonic-gate (void) fprintf(stderr, gettext("cannot verify fs %s: " 25250Sstevel@tonic-gate "type %s is too long.\n"), fstab.zone_fs_dir, 25260Sstevel@tonic-gate fstab.zone_fs_type); 25270Sstevel@tonic-gate return_code = Z_ERR; 25280Sstevel@tonic-gate goto next_fs; 25290Sstevel@tonic-gate } 25300Sstevel@tonic-gate if (fstab.zone_fs_raw[0] == '\0' && stat(cmdbuf, &st) == 0) { 2531924Sgjelinek (void) fprintf(stderr, gettext("could not verify fs " 2532924Sgjelinek "%s: must specify 'raw' device for %s " 25331867Sgjelinek "file systems\n"), 25340Sstevel@tonic-gate fstab.zone_fs_dir, fstab.zone_fs_type); 25350Sstevel@tonic-gate return_code = Z_ERR; 25360Sstevel@tonic-gate goto next_fs; 25370Sstevel@tonic-gate } 25380Sstevel@tonic-gate if (fstab.zone_fs_raw[0] != '\0' && 25390Sstevel@tonic-gate (stat(cmdbuf, &st) != 0 || !S_ISREG(st.st_mode))) { 25400Sstevel@tonic-gate (void) fprintf(stderr, gettext("cannot verify fs %s: " 25410Sstevel@tonic-gate "'raw' device specified but " 25420Sstevel@tonic-gate "no fsck executable exists for %s\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 } 25471393Slling 25481393Slling /* Verify fs_special. */ 25491393Slling if ((return_code = verify_fs_special(&fstab)) != Z_OK) 2550823Sgjelinek goto next_fs; 25511393Slling 25521393Slling /* Verify fs_raw. */ 2553823Sgjelinek if (fstab.zone_fs_raw[0] != '\0' && 2554823Sgjelinek stat(fstab.zone_fs_raw, &st) != 0) { 2555924Sgjelinek /* 2556924Sgjelinek * TRANSLATION_NOTE 2557924Sgjelinek * fs is a literal that should not be translated. 2558924Sgjelinek */ 2559924Sgjelinek (void) fprintf(stderr, gettext("could not verify fs " 2560924Sgjelinek "%s: could not access %s: %s\n"), fstab.zone_fs_dir, 2561823Sgjelinek fstab.zone_fs_raw, strerror(errno)); 2562823Sgjelinek return_code = Z_ERR; 2563823Sgjelinek goto next_fs; 2564823Sgjelinek } 25650Sstevel@tonic-gate next_fs: 25660Sstevel@tonic-gate zonecfg_free_fs_option_list(fstab.zone_fs_options); 25670Sstevel@tonic-gate } 25680Sstevel@tonic-gate (void) zonecfg_endfsent(handle); 25690Sstevel@tonic-gate 25700Sstevel@tonic-gate return (return_code); 25710Sstevel@tonic-gate } 25720Sstevel@tonic-gate 25730Sstevel@tonic-gate static int 25741645Scomay verify_limitpriv(zone_dochandle_t handle) 25751645Scomay { 25761645Scomay char *privname = NULL; 25771645Scomay int err; 25781645Scomay priv_set_t *privs; 25791645Scomay 25801645Scomay if ((privs = priv_allocset()) == NULL) { 25811645Scomay zperror(gettext("failed to allocate privilege set"), B_FALSE); 25821645Scomay return (Z_NOMEM); 25831645Scomay } 25841645Scomay err = zonecfg_get_privset(handle, privs, &privname); 25851645Scomay switch (err) { 25861645Scomay case Z_OK: 25871645Scomay break; 25881645Scomay case Z_PRIV_PROHIBITED: 25891645Scomay (void) fprintf(stderr, gettext("privilege \"%s\" is not " 25901645Scomay "permitted within the zone's privilege set\n"), privname); 25911645Scomay break; 25921645Scomay case Z_PRIV_REQUIRED: 25931645Scomay (void) fprintf(stderr, gettext("required privilege \"%s\" is " 25941645Scomay "missing from the zone's privilege set\n"), privname); 25951645Scomay break; 25961645Scomay case Z_PRIV_UNKNOWN: 25971645Scomay (void) fprintf(stderr, gettext("unknown privilege \"%s\" " 25981645Scomay "specified in the zone's privilege set\n"), privname); 25991645Scomay break; 26001645Scomay default: 26011645Scomay zperror( 26021645Scomay gettext("failed to determine the zone's privilege set"), 26031645Scomay B_TRUE); 26041645Scomay break; 26051645Scomay } 26061645Scomay free(privname); 26071645Scomay priv_freeset(privs); 26081645Scomay return (err); 26091645Scomay } 26101645Scomay 26111915Sgjelinek static void 26121915Sgjelinek free_local_netifs(int if_cnt, struct net_if **if_list) 26131915Sgjelinek { 26141915Sgjelinek int i; 26151915Sgjelinek 26161915Sgjelinek for (i = 0; i < if_cnt; i++) { 26171915Sgjelinek free(if_list[i]->name); 26181915Sgjelinek free(if_list[i]); 26191915Sgjelinek } 26201915Sgjelinek free(if_list); 26211915Sgjelinek } 26221915Sgjelinek 26231915Sgjelinek /* 26241915Sgjelinek * Get a list of the network interfaces, along with their address families, 26251915Sgjelinek * that are plumbed in the global zone. See if_tcp(7p) for a description 26261915Sgjelinek * of the ioctls used here. 26271915Sgjelinek */ 26281915Sgjelinek static int 26291915Sgjelinek get_local_netifs(int *if_cnt, struct net_if ***if_list) 26301915Sgjelinek { 26311915Sgjelinek int s; 26321915Sgjelinek int i; 26331915Sgjelinek int res = Z_OK; 26341915Sgjelinek int space_needed; 26351915Sgjelinek int cnt = 0; 26361915Sgjelinek struct lifnum if_num; 26371915Sgjelinek struct lifconf if_conf; 26381915Sgjelinek struct lifreq *if_reqp; 26391915Sgjelinek char *if_buf; 26401915Sgjelinek struct net_if **local_ifs = NULL; 26411915Sgjelinek 26421915Sgjelinek *if_cnt = 0; 26431915Sgjelinek *if_list = NULL; 26441915Sgjelinek 26451915Sgjelinek if ((s = socket(SOCKET_AF(AF_INET), SOCK_DGRAM, 0)) < 0) 26461915Sgjelinek return (Z_ERR); 26471915Sgjelinek 26481915Sgjelinek /* 26491915Sgjelinek * Come back here in the unlikely event that the number of interfaces 26501915Sgjelinek * increases between the time we get the count and the time we do the 26511915Sgjelinek * SIOCGLIFCONF ioctl. 26521915Sgjelinek */ 26531915Sgjelinek retry: 26541915Sgjelinek /* Get the number of interfaces. */ 26551915Sgjelinek if_num.lifn_family = AF_UNSPEC; 26561915Sgjelinek if_num.lifn_flags = LIFC_NOXMIT; 26571915Sgjelinek if (ioctl(s, SIOCGLIFNUM, &if_num) < 0) { 26581915Sgjelinek (void) close(s); 26591915Sgjelinek return (Z_ERR); 26601915Sgjelinek } 26611915Sgjelinek 26621915Sgjelinek /* Get the interface configuration list. */ 26631915Sgjelinek space_needed = if_num.lifn_count * sizeof (struct lifreq); 26641915Sgjelinek if ((if_buf = malloc(space_needed)) == NULL) { 26651915Sgjelinek (void) close(s); 26661915Sgjelinek return (Z_ERR); 26671915Sgjelinek } 26681915Sgjelinek if_conf.lifc_family = AF_UNSPEC; 26691915Sgjelinek if_conf.lifc_flags = LIFC_NOXMIT; 26701915Sgjelinek if_conf.lifc_len = space_needed; 26711915Sgjelinek if_conf.lifc_buf = if_buf; 26721915Sgjelinek if (ioctl(s, SIOCGLIFCONF, &if_conf) < 0) { 26731915Sgjelinek free(if_buf); 26741915Sgjelinek /* 26751915Sgjelinek * SIOCGLIFCONF returns EINVAL if the buffer we passed in is 26761915Sgjelinek * too small. In this case go back and get the new if cnt. 26771915Sgjelinek */ 26781915Sgjelinek if (errno == EINVAL) 26791915Sgjelinek goto retry; 26801915Sgjelinek 26811915Sgjelinek (void) close(s); 26821915Sgjelinek return (Z_ERR); 26831915Sgjelinek } 26841915Sgjelinek (void) close(s); 26851915Sgjelinek 26861915Sgjelinek /* Get the name and address family for each interface. */ 26871915Sgjelinek if_reqp = if_conf.lifc_req; 26881915Sgjelinek for (i = 0; i < (if_conf.lifc_len / sizeof (struct lifreq)); i++) { 26891915Sgjelinek struct net_if **p; 26901915Sgjelinek struct lifreq req; 26911915Sgjelinek 26921915Sgjelinek if (strcmp(LOOPBACK_IF, if_reqp->lifr_name) == 0) { 26931915Sgjelinek if_reqp++; 26941915Sgjelinek continue; 26951915Sgjelinek } 26961915Sgjelinek 26971915Sgjelinek if ((s = socket(SOCKET_AF(if_reqp->lifr_addr.ss_family), 26981915Sgjelinek SOCK_DGRAM, 0)) == -1) { 26991915Sgjelinek res = Z_ERR; 27001915Sgjelinek break; 27011915Sgjelinek } 27021915Sgjelinek 27031915Sgjelinek (void) strncpy(req.lifr_name, if_reqp->lifr_name, 27041915Sgjelinek sizeof (req.lifr_name)); 27051915Sgjelinek if (ioctl(s, SIOCGLIFADDR, &req) < 0) { 27061915Sgjelinek (void) close(s); 27071915Sgjelinek if_reqp++; 27081915Sgjelinek continue; 27091915Sgjelinek } 27101915Sgjelinek 27111915Sgjelinek if ((p = (struct net_if **)realloc(local_ifs, 27121915Sgjelinek sizeof (struct net_if *) * (cnt + 1))) == NULL) { 27131915Sgjelinek res = Z_ERR; 27141915Sgjelinek break; 27151915Sgjelinek } 27161915Sgjelinek local_ifs = p; 27171915Sgjelinek 27181915Sgjelinek if ((local_ifs[cnt] = malloc(sizeof (struct net_if))) == NULL) { 27191915Sgjelinek res = Z_ERR; 27201915Sgjelinek break; 27211915Sgjelinek } 27221915Sgjelinek 27231915Sgjelinek if ((local_ifs[cnt]->name = strdup(if_reqp->lifr_name)) 27241915Sgjelinek == NULL) { 27251915Sgjelinek free(local_ifs[cnt]); 27261915Sgjelinek res = Z_ERR; 27271915Sgjelinek break; 27281915Sgjelinek } 27291915Sgjelinek local_ifs[cnt]->af = req.lifr_addr.ss_family; 27301915Sgjelinek cnt++; 27311915Sgjelinek 27321915Sgjelinek (void) close(s); 27331915Sgjelinek if_reqp++; 27341915Sgjelinek } 27351915Sgjelinek 27361915Sgjelinek free(if_buf); 27371915Sgjelinek 27381915Sgjelinek if (res != Z_OK) { 27391915Sgjelinek free_local_netifs(cnt, local_ifs); 27401915Sgjelinek } else { 27411915Sgjelinek *if_cnt = cnt; 27421915Sgjelinek *if_list = local_ifs; 27431915Sgjelinek } 27441915Sgjelinek 27451915Sgjelinek return (res); 27461915Sgjelinek } 27471915Sgjelinek 27481915Sgjelinek static char * 27491915Sgjelinek af2str(int af) 27501915Sgjelinek { 27511915Sgjelinek switch (af) { 27521915Sgjelinek case AF_INET: 27531915Sgjelinek return ("IPv4"); 27541915Sgjelinek case AF_INET6: 27551915Sgjelinek return ("IPv6"); 27561915Sgjelinek default: 27571915Sgjelinek return ("Unknown"); 27581915Sgjelinek } 27591915Sgjelinek } 27601915Sgjelinek 27611915Sgjelinek /* 27621915Sgjelinek * Cross check the network interface name and address family with the 27631915Sgjelinek * interfaces that are set up in the global zone so that we can print the 27641915Sgjelinek * appropriate error message. 27651915Sgjelinek */ 27661915Sgjelinek static void 27671915Sgjelinek print_net_err(char *phys, char *addr, int af, char *msg) 27681915Sgjelinek { 27691915Sgjelinek int i; 27701915Sgjelinek int local_if_cnt = 0; 27711915Sgjelinek struct net_if **local_ifs = NULL; 27721915Sgjelinek boolean_t found_if = B_FALSE; 27731915Sgjelinek boolean_t found_af = B_FALSE; 27741915Sgjelinek 27751915Sgjelinek if (get_local_netifs(&local_if_cnt, &local_ifs) != Z_OK) { 27761915Sgjelinek (void) fprintf(stderr, 27771915Sgjelinek gettext("could not verify %s %s=%s %s=%s\n\t%s\n"), 27781915Sgjelinek "net", "address", addr, "physical", phys, msg); 27791915Sgjelinek return; 27801915Sgjelinek } 27811915Sgjelinek 27821915Sgjelinek for (i = 0; i < local_if_cnt; i++) { 27831915Sgjelinek if (strcmp(phys, local_ifs[i]->name) == 0) { 27841915Sgjelinek found_if = B_TRUE; 27851915Sgjelinek if (af == local_ifs[i]->af) { 27861915Sgjelinek found_af = B_TRUE; 27871915Sgjelinek break; 27881915Sgjelinek } 27891915Sgjelinek } 27901915Sgjelinek } 27911915Sgjelinek 27921915Sgjelinek free_local_netifs(local_if_cnt, local_ifs); 27931915Sgjelinek 27941915Sgjelinek if (!found_if) { 27951915Sgjelinek (void) fprintf(stderr, 27961915Sgjelinek gettext("could not verify %s %s=%s\n\t" 27971915Sgjelinek "network interface %s is not plumbed in the global zone\n"), 27981915Sgjelinek "net", "physical", phys, phys); 27991915Sgjelinek return; 28001915Sgjelinek } 28011915Sgjelinek 28021915Sgjelinek /* 28031915Sgjelinek * Print this error if we were unable to find the address family 28041915Sgjelinek * for this interface. If the af variable is not initialized to 28051915Sgjelinek * to something meaningful by the caller (not AF_UNSPEC) then we 28061915Sgjelinek * also skip this message since it wouldn't be informative. 28071915Sgjelinek */ 28081915Sgjelinek if (!found_af && af != AF_UNSPEC) { 28091915Sgjelinek (void) fprintf(stderr, 28101915Sgjelinek gettext("could not verify %s %s=%s %s=%s\n\tthe %s address " 2811*3448Sdh155122 "family is not configured on this network interface in " 2812*3448Sdh155122 "the\n\tglobal zone\n"), 28131915Sgjelinek "net", "address", addr, "physical", phys, af2str(af)); 28141915Sgjelinek return; 28151915Sgjelinek } 28161915Sgjelinek 28171915Sgjelinek (void) fprintf(stderr, 28181915Sgjelinek gettext("could not verify %s %s=%s %s=%s\n\t%s\n"), 28191915Sgjelinek "net", "address", addr, "physical", phys, msg); 28201915Sgjelinek } 28211915Sgjelinek 28221645Scomay static int 28233339Szt129084 verify_handle(int cmd_num, zone_dochandle_t handle, char *argv[]) 28240Sstevel@tonic-gate { 28250Sstevel@tonic-gate struct zone_nwiftab nwiftab; 28260Sstevel@tonic-gate int return_code = Z_OK; 28270Sstevel@tonic-gate int err; 2828766Scarlsonj boolean_t in_alt_root; 2829*3448Sdh155122 zone_iptype_t iptype; 2830*3448Sdh155122 int fd; 28310Sstevel@tonic-gate 2832766Scarlsonj in_alt_root = zonecfg_in_alt_root(); 2833766Scarlsonj if (in_alt_root) 2834766Scarlsonj goto no_net; 2835766Scarlsonj 2836*3448Sdh155122 if ((err = zonecfg_get_iptype(handle, &iptype)) != Z_OK) { 2837*3448Sdh155122 errno = err; 2838*3448Sdh155122 zperror(cmd_to_str(cmd_num), B_TRUE); 2839*3448Sdh155122 zonecfg_fini_handle(handle); 2840*3448Sdh155122 return (Z_ERR); 2841*3448Sdh155122 } 28420Sstevel@tonic-gate if ((err = zonecfg_setnwifent(handle)) != Z_OK) { 28430Sstevel@tonic-gate errno = err; 28440Sstevel@tonic-gate zperror(cmd_to_str(cmd_num), B_TRUE); 28450Sstevel@tonic-gate zonecfg_fini_handle(handle); 28460Sstevel@tonic-gate return (Z_ERR); 28470Sstevel@tonic-gate } 28480Sstevel@tonic-gate while (zonecfg_getnwifent(handle, &nwiftab) == Z_OK) { 28490Sstevel@tonic-gate struct lifreq lifr; 28501915Sgjelinek sa_family_t af = AF_UNSPEC; 2851*3448Sdh155122 char dl_owner_zname[ZONENAME_MAX]; 2852*3448Sdh155122 zoneid_t dl_owner_zid; 2853*3448Sdh155122 zoneid_t target_zid; 2854*3448Sdh155122 int res; 28550Sstevel@tonic-gate 28560Sstevel@tonic-gate /* skip any loopback interfaces */ 28570Sstevel@tonic-gate if (strcmp(nwiftab.zone_nwif_physical, "lo0") == 0) 28580Sstevel@tonic-gate continue; 2859*3448Sdh155122 switch (iptype) { 2860*3448Sdh155122 case ZS_SHARED: 2861*3448Sdh155122 if ((res = zonecfg_valid_net_address( 2862*3448Sdh155122 nwiftab.zone_nwif_address, &lifr)) != Z_OK) { 2863*3448Sdh155122 print_net_err(nwiftab.zone_nwif_physical, 2864*3448Sdh155122 nwiftab.zone_nwif_address, af, 2865*3448Sdh155122 zonecfg_strerror(res)); 2866*3448Sdh155122 return_code = Z_ERR; 2867*3448Sdh155122 continue; 2868*3448Sdh155122 } 2869*3448Sdh155122 af = lifr.lifr_addr.ss_family; 2870*3448Sdh155122 if (!zonecfg_ifname_exists(af, 2871*3448Sdh155122 nwiftab.zone_nwif_physical)) { 2872*3448Sdh155122 /* 2873*3448Sdh155122 * The interface failed to come up. We continue 2874*3448Sdh155122 * on anyway for the sake of consistency: a 2875*3448Sdh155122 * zone is not shut down if the interface fails 2876*3448Sdh155122 * any time after boot, nor does the global zone 2877*3448Sdh155122 * fail to boot if an interface fails. 2878*3448Sdh155122 */ 2879*3448Sdh155122 (void) fprintf(stderr, 2880*3448Sdh155122 gettext("WARNING: skipping network " 2881*3448Sdh155122 "interface '%s' which may not be " 2882*3448Sdh155122 "present/plumbed in the global " 2883*3448Sdh155122 "zone.\n"), 2884*3448Sdh155122 nwiftab.zone_nwif_physical); 2885*3448Sdh155122 } 2886*3448Sdh155122 break; 2887*3448Sdh155122 case ZS_EXCLUSIVE: 2888*3448Sdh155122 /* Warning if it exists for either IPv4 or IPv6 */ 2889*3448Sdh155122 2890*3448Sdh155122 if (zonecfg_ifname_exists(AF_INET, 2891*3448Sdh155122 nwiftab.zone_nwif_physical) || 2892*3448Sdh155122 zonecfg_ifname_exists(AF_INET6, 2893*3448Sdh155122 nwiftab.zone_nwif_physical)) { 2894*3448Sdh155122 (void) fprintf(stderr, 2895*3448Sdh155122 gettext("WARNING: skipping network " 2896*3448Sdh155122 "interface '%s' which is used in the " 2897*3448Sdh155122 "global zone.\n"), 2898*3448Sdh155122 nwiftab.zone_nwif_physical); 2899*3448Sdh155122 break; 2900*3448Sdh155122 } 29012611Svp157776 /* 2902*3448Sdh155122 * Verify that the physical interface can 2903*3448Sdh155122 * be opened 2904*3448Sdh155122 */ 2905*3448Sdh155122 fd = ifname_open(nwiftab.zone_nwif_physical); 2906*3448Sdh155122 if (fd == -1) { 2907*3448Sdh155122 (void) fprintf(stderr, 2908*3448Sdh155122 gettext("WARNING: skipping network " 2909*3448Sdh155122 "interface '%s' which cannot be opened.\n"), 2910*3448Sdh155122 nwiftab.zone_nwif_physical); 2911*3448Sdh155122 break; 2912*3448Sdh155122 } else { 2913*3448Sdh155122 (void) close(fd); 2914*3448Sdh155122 } 2915*3448Sdh155122 /* 2916*3448Sdh155122 * Verify whether the physical interface is already 2917*3448Sdh155122 * used by a zone. 2918*3448Sdh155122 */ 2919*3448Sdh155122 dl_owner_zid = ALL_ZONES; 2920*3448Sdh155122 if (zone_check_datalink(&dl_owner_zid, 2921*3448Sdh155122 nwiftab.zone_nwif_physical) != 0) 2922*3448Sdh155122 break; 2923*3448Sdh155122 2924*3448Sdh155122 /* 2925*3448Sdh155122 * If the zone being verified is 2926*3448Sdh155122 * running and owns the interface 2927*3448Sdh155122 */ 2928*3448Sdh155122 target_zid = getzoneidbyname(target_zone); 2929*3448Sdh155122 if (target_zid == dl_owner_zid) 2930*3448Sdh155122 break; 2931*3448Sdh155122 2932*3448Sdh155122 /* Zone id match failed, use name to check */ 2933*3448Sdh155122 if (getzonenamebyid(dl_owner_zid, dl_owner_zname, 2934*3448Sdh155122 ZONENAME_MAX) < 0) { 2935*3448Sdh155122 /* No name, show ID instead */ 2936*3448Sdh155122 (void) snprintf(dl_owner_zname, ZONENAME_MAX, 2937*3448Sdh155122 "<%d>", dl_owner_zid); 2938*3448Sdh155122 } else if (strcmp(dl_owner_zname, target_zone) == 0) 2939*3448Sdh155122 break; 2940*3448Sdh155122 2941*3448Sdh155122 /* 2942*3448Sdh155122 * Note here we only report a warning that 2943*3448Sdh155122 * the interface is already in use by another 2944*3448Sdh155122 * running zone, and the verify process just 2945*3448Sdh155122 * goes on, if the interface is still in use 2946*3448Sdh155122 * when this zone really boots up, zoneadmd 2947*3448Sdh155122 * will find it. If the name of the zone which 2948*3448Sdh155122 * owns this interface cannot be determined, 2949*3448Sdh155122 * then it is not possible to determine if there 2950*3448Sdh155122 * is a conflict so just report it as a warning. 29512611Svp157776 */ 29522611Svp157776 (void) fprintf(stderr, 2953*3448Sdh155122 gettext("WARNING: skipping network interface " 2954*3448Sdh155122 "'%s' which is used by the non-global zone " 2955*3448Sdh155122 "'%s'.\n"), nwiftab.zone_nwif_physical, 2956*3448Sdh155122 dl_owner_zname); 2957*3448Sdh155122 break; 29580Sstevel@tonic-gate } 29590Sstevel@tonic-gate } 29600Sstevel@tonic-gate (void) zonecfg_endnwifent(handle); 2961766Scarlsonj no_net: 29620Sstevel@tonic-gate 29631931Sgjelinek /* verify that lofs has not been excluded from the kernel */ 29642078Sgjelinek if (!(cmd_num == CMD_DETACH || cmd_num == CMD_ATTACH || 29652078Sgjelinek cmd_num == CMD_MOVE || cmd_num == CMD_CLONE) && 29662078Sgjelinek modctl(MODLOAD, 1, "fs/lofs", NULL) != 0) { 29671931Sgjelinek if (errno == ENXIO) 29681931Sgjelinek (void) fprintf(stderr, gettext("could not verify " 29691931Sgjelinek "lofs(7FS): possibly excluded in /etc/system\n")); 29701931Sgjelinek else 29711931Sgjelinek (void) fprintf(stderr, gettext("could not verify " 29721931Sgjelinek "lofs(7FS): %s\n"), strerror(errno)); 29731931Sgjelinek return_code = Z_ERR; 29741931Sgjelinek } 29751931Sgjelinek 29760Sstevel@tonic-gate if (verify_filesystems(handle) != Z_OK) 29770Sstevel@tonic-gate return_code = Z_ERR; 2978823Sgjelinek if (verify_ipd(handle) != Z_OK) 2979823Sgjelinek return_code = Z_ERR; 2980766Scarlsonj if (!in_alt_root && verify_rctls(handle) != Z_OK) 29810Sstevel@tonic-gate return_code = Z_ERR; 2982766Scarlsonj if (!in_alt_root && verify_pool(handle) != Z_OK) 29830Sstevel@tonic-gate return_code = Z_ERR; 29843339Szt129084 if (!in_alt_root && verify_brand(handle, cmd_num, argv) != Z_OK) 29852712Snn35248 return_code = Z_ERR; 2986789Sahrens if (!in_alt_root && verify_datasets(handle) != Z_OK) 2987789Sahrens return_code = Z_ERR; 29881645Scomay 29891645Scomay /* 29901645Scomay * As the "mount" command is used for patching/upgrading of zones 29911645Scomay * or other maintenance processes, the zone's privilege set is not 29921645Scomay * checked in this case. Instead, the default, safe set of 29931645Scomay * privileges will be used when this zone is created in the 29941645Scomay * kernel. 29951645Scomay */ 29961645Scomay if (!in_alt_root && cmd_num != CMD_MOUNT && 29971645Scomay verify_limitpriv(handle) != Z_OK) 29981645Scomay return_code = Z_ERR; 29992078Sgjelinek 30002078Sgjelinek return (return_code); 30012078Sgjelinek } 30022078Sgjelinek 30032078Sgjelinek static int 30043339Szt129084 verify_details(int cmd_num, char *argv[]) 30052078Sgjelinek { 30062078Sgjelinek zone_dochandle_t handle; 30072078Sgjelinek char zonepath[MAXPATHLEN], checkpath[MAXPATHLEN]; 30082078Sgjelinek int return_code = Z_OK; 30092078Sgjelinek int err; 30102078Sgjelinek 30112078Sgjelinek if ((handle = zonecfg_init_handle()) == NULL) { 30122078Sgjelinek zperror(cmd_to_str(cmd_num), B_TRUE); 30132078Sgjelinek return (Z_ERR); 30142078Sgjelinek } 30152078Sgjelinek if ((err = zonecfg_get_handle(target_zone, handle)) != Z_OK) { 30162078Sgjelinek errno = err; 30172078Sgjelinek zperror(cmd_to_str(cmd_num), B_TRUE); 30182078Sgjelinek zonecfg_fini_handle(handle); 30192078Sgjelinek return (Z_ERR); 30202078Sgjelinek } 30212078Sgjelinek if ((err = zonecfg_get_zonepath(handle, zonepath, sizeof (zonepath))) != 30222078Sgjelinek Z_OK) { 30232078Sgjelinek errno = err; 30242078Sgjelinek zperror(cmd_to_str(cmd_num), B_TRUE); 30252078Sgjelinek zonecfg_fini_handle(handle); 30262078Sgjelinek return (Z_ERR); 30272078Sgjelinek } 30282078Sgjelinek /* 30292078Sgjelinek * zonecfg_get_zonepath() gets its data from the XML repository. 30302078Sgjelinek * Verify this against the index file, which is checked first by 30312078Sgjelinek * zone_get_zonepath(). If they don't match, bail out. 30322078Sgjelinek */ 30332078Sgjelinek if ((err = zone_get_zonepath(target_zone, checkpath, 30342078Sgjelinek sizeof (checkpath))) != Z_OK) { 30352078Sgjelinek errno = err; 30362078Sgjelinek zperror2(target_zone, gettext("could not get zone path")); 30372078Sgjelinek return (Z_ERR); 30382078Sgjelinek } 30392078Sgjelinek if (strcmp(zonepath, checkpath) != 0) { 30402078Sgjelinek /* 30412078Sgjelinek * TRANSLATION_NOTE 30422078Sgjelinek * XML and zonepath are literals that should not be translated. 30432078Sgjelinek */ 30442078Sgjelinek (void) fprintf(stderr, gettext("The XML repository has " 30452078Sgjelinek "zonepath '%s',\nbut the index file has zonepath '%s'.\n" 30462078Sgjelinek "These must match, so fix the incorrect entry.\n"), 30472078Sgjelinek zonepath, checkpath); 30482078Sgjelinek return (Z_ERR); 30492078Sgjelinek } 30502078Sgjelinek if (validate_zonepath(zonepath, cmd_num) != Z_OK) { 30512078Sgjelinek (void) fprintf(stderr, gettext("could not verify zonepath %s " 30522078Sgjelinek "because of the above errors.\n"), zonepath); 30532078Sgjelinek return_code = Z_ERR; 30542078Sgjelinek } 30552078Sgjelinek 30563339Szt129084 if (verify_handle(cmd_num, handle, argv) != Z_OK) 30572078Sgjelinek return_code = Z_ERR; 30582078Sgjelinek 30590Sstevel@tonic-gate zonecfg_fini_handle(handle); 30600Sstevel@tonic-gate if (return_code == Z_ERR) 30610Sstevel@tonic-gate (void) fprintf(stderr, 30620Sstevel@tonic-gate gettext("%s: zone %s failed to verify\n"), 30630Sstevel@tonic-gate execname, target_zone); 30640Sstevel@tonic-gate return (return_code); 30650Sstevel@tonic-gate } 30660Sstevel@tonic-gate 30670Sstevel@tonic-gate static int 30680Sstevel@tonic-gate verify_func(int argc, char *argv[]) 30690Sstevel@tonic-gate { 30700Sstevel@tonic-gate int arg; 30710Sstevel@tonic-gate 30720Sstevel@tonic-gate optind = 0; 30730Sstevel@tonic-gate if ((arg = getopt(argc, argv, "?")) != EOF) { 30740Sstevel@tonic-gate switch (arg) { 30750Sstevel@tonic-gate case '?': 30760Sstevel@tonic-gate sub_usage(SHELP_VERIFY, CMD_VERIFY); 30770Sstevel@tonic-gate return (optopt == '?' ? Z_OK : Z_USAGE); 30780Sstevel@tonic-gate default: 30790Sstevel@tonic-gate sub_usage(SHELP_VERIFY, CMD_VERIFY); 30800Sstevel@tonic-gate return (Z_USAGE); 30810Sstevel@tonic-gate } 30820Sstevel@tonic-gate } 30830Sstevel@tonic-gate if (argc > optind) { 30840Sstevel@tonic-gate sub_usage(SHELP_VERIFY, CMD_VERIFY); 30850Sstevel@tonic-gate return (Z_USAGE); 30860Sstevel@tonic-gate } 30872712Snn35248 if (sanity_check(target_zone, CMD_VERIFY, B_FALSE, B_FALSE, B_FALSE) 30882712Snn35248 != Z_OK) 30890Sstevel@tonic-gate return (Z_ERR); 30903339Szt129084 return (verify_details(CMD_VERIFY, argv)); 30910Sstevel@tonic-gate } 30920Sstevel@tonic-gate 30932712Snn35248 static int 30942712Snn35248 addopt(char *buf, int opt, char *optarg, size_t bufsize) 30952712Snn35248 { 30962712Snn35248 char optstring[4]; 30972712Snn35248 30982712Snn35248 if (opt > 0) 30992712Snn35248 (void) sprintf(optstring, " -%c", opt); 31002712Snn35248 else 31012712Snn35248 (void) strcpy(optstring, " "); 31022712Snn35248 31032712Snn35248 if ((strlcat(buf, optstring, bufsize) > bufsize)) 31042712Snn35248 return (Z_ERR); 31052712Snn35248 if ((optarg != NULL) && (strlcat(buf, optarg, bufsize) > bufsize)) 31062712Snn35248 return (Z_ERR); 31072712Snn35248 return (Z_OK); 31082712Snn35248 } 31090Sstevel@tonic-gate 31100Sstevel@tonic-gate static int 31110Sstevel@tonic-gate install_func(int argc, char *argv[]) 31120Sstevel@tonic-gate { 31132712Snn35248 char cmdbuf[MAXPATHLEN]; 31140Sstevel@tonic-gate int lockfd; 31152712Snn35248 int arg, err, subproc_err; 31160Sstevel@tonic-gate char zonepath[MAXPATHLEN]; 31172727Sedp brand_handle_t bh = NULL; 31180Sstevel@tonic-gate int status; 31191867Sgjelinek boolean_t nodataset = B_FALSE; 31202712Snn35248 char opts[128]; 31212712Snn35248 31222712Snn35248 if (target_zone == NULL) { 31232712Snn35248 sub_usage(SHELP_INSTALL, CMD_INSTALL); 31242712Snn35248 return (Z_USAGE); 31252712Snn35248 } 31260Sstevel@tonic-gate 3127766Scarlsonj if (zonecfg_in_alt_root()) { 3128766Scarlsonj zerror(gettext("cannot install zone in alternate root")); 3129766Scarlsonj return (Z_ERR); 3130766Scarlsonj } 3131766Scarlsonj 31322712Snn35248 if ((err = zone_get_zonepath(target_zone, zonepath, 31332712Snn35248 sizeof (zonepath))) != Z_OK) { 31342712Snn35248 errno = err; 31352712Snn35248 zperror2(target_zone, gettext("could not get zone path")); 31362712Snn35248 return (Z_ERR); 31372712Snn35248 } 31382712Snn35248 31392712Snn35248 /* Fetch the install command from the brand configuration. */ 31402727Sedp if ((bh = brand_open(target_brand)) == NULL) { 31412712Snn35248 zerror(gettext("missing or invalid brand")); 31422712Snn35248 return (Z_ERR); 31432712Snn35248 } 31442712Snn35248 31452712Snn35248 (void) strcpy(cmdbuf, EXEC_PREFIX); 31462727Sedp if (brand_get_install(bh, target_zone, zonepath, cmdbuf + EXEC_LEN, 31472712Snn35248 sizeof (cmdbuf) - EXEC_LEN, 0, NULL) != 0) { 31482712Snn35248 zerror("invalid brand configuration: missing install resource"); 31492727Sedp brand_close(bh); 31502712Snn35248 return (Z_ERR); 31512712Snn35248 } 31522712Snn35248 31532712Snn35248 (void) strcpy(opts, "?x:"); 31542712Snn35248 if (!is_native_zone) { 31552712Snn35248 /* 31562712Snn35248 * Fetch the list of recognized command-line options from 31572712Snn35248 * the brand configuration file. 31582712Snn35248 */ 31592727Sedp if (brand_get_installopts(bh, opts + strlen(opts), 31602712Snn35248 sizeof (opts) - strlen(opts)) != 0) { 31612712Snn35248 zerror("invalid brand configuration: missing " 31622712Snn35248 "install options resource"); 31632727Sedp brand_close(bh); 31642712Snn35248 return (Z_ERR); 31652712Snn35248 } 31662712Snn35248 } 31672727Sedp brand_close(bh); 31682712Snn35248 31690Sstevel@tonic-gate optind = 0; 31702712Snn35248 while ((arg = getopt(argc, argv, opts)) != EOF) { 31710Sstevel@tonic-gate switch (arg) { 31720Sstevel@tonic-gate case '?': 31730Sstevel@tonic-gate sub_usage(SHELP_INSTALL, CMD_INSTALL); 31740Sstevel@tonic-gate return (optopt == '?' ? Z_OK : Z_USAGE); 31751867Sgjelinek case 'x': 31761867Sgjelinek if (strcmp(optarg, "nodataset") != 0) { 31771867Sgjelinek sub_usage(SHELP_INSTALL, CMD_INSTALL); 31781867Sgjelinek return (Z_USAGE); 31791867Sgjelinek } 31801867Sgjelinek nodataset = B_TRUE; 31811867Sgjelinek break; 31820Sstevel@tonic-gate default: 31832712Snn35248 if (is_native_zone) { 31842712Snn35248 sub_usage(SHELP_INSTALL, CMD_INSTALL); 31852712Snn35248 return (Z_USAGE); 31862712Snn35248 } 31872712Snn35248 31882712Snn35248 /* 31892712Snn35248 * This option isn't for zoneadm, so append it to 31902712Snn35248 * the command line passed to the brand-specific 31912712Snn35248 * install routine. 31922712Snn35248 */ 31932712Snn35248 if (addopt(cmdbuf, optopt, optarg, 31942712Snn35248 sizeof (cmdbuf)) != Z_OK) { 31952712Snn35248 zerror("Install command line too long"); 31962712Snn35248 return (Z_ERR); 31972712Snn35248 } 31982712Snn35248 break; 31990Sstevel@tonic-gate } 32000Sstevel@tonic-gate } 32012712Snn35248 32022712Snn35248 if (!is_native_zone) { 32032712Snn35248 for (; optind < argc; optind++) { 32042712Snn35248 if (addopt(cmdbuf, 0, argv[optind], 32052712Snn35248 sizeof (cmdbuf)) != Z_OK) { 32062712Snn35248 zerror("Install command line too long"); 32072712Snn35248 return (Z_ERR); 32082712Snn35248 } 32092712Snn35248 } 32102712Snn35248 } 32112712Snn35248 32122712Snn35248 if (sanity_check(target_zone, CMD_INSTALL, B_FALSE, B_TRUE, B_FALSE) 32132712Snn35248 != Z_OK) 32140Sstevel@tonic-gate return (Z_ERR); 32153339Szt129084 if (verify_details(CMD_INSTALL, argv) != Z_OK) 32160Sstevel@tonic-gate return (Z_ERR); 32170Sstevel@tonic-gate 32180Sstevel@tonic-gate if (grab_lock_file(target_zone, &lockfd) != Z_OK) { 32190Sstevel@tonic-gate zerror(gettext("another %s may have an operation in progress."), 32201300Sgjelinek "zoneadm"); 32210Sstevel@tonic-gate return (Z_ERR); 32220Sstevel@tonic-gate } 32230Sstevel@tonic-gate err = zone_set_state(target_zone, ZONE_STATE_INCOMPLETE); 32240Sstevel@tonic-gate if (err != Z_OK) { 32250Sstevel@tonic-gate errno = err; 32260Sstevel@tonic-gate zperror2(target_zone, gettext("could not set state")); 32270Sstevel@tonic-gate goto done; 32280Sstevel@tonic-gate } 32290Sstevel@tonic-gate 32302712Snn35248 if (!nodataset) 32312712Snn35248 create_zfs_zonepath(zonepath); 32322712Snn35248 32330Sstevel@tonic-gate /* 32340Sstevel@tonic-gate * According to the Application Packaging Developer's Guide, a 32350Sstevel@tonic-gate * "checkinstall" script when included in a package is executed as 32360Sstevel@tonic-gate * the user "install", if such a user exists, or by the user 32370Sstevel@tonic-gate * "nobody". In order to support this dubious behavior, the path 32380Sstevel@tonic-gate * to the zone being constructed is opened up during the life of 32390Sstevel@tonic-gate * the command laying down the zone's root file system. Once this 32400Sstevel@tonic-gate * has completed, regardless of whether it was successful, the 32410Sstevel@tonic-gate * path to the zone is again restricted. 32420Sstevel@tonic-gate */ 32430Sstevel@tonic-gate if (chmod(zonepath, DEFAULT_DIR_MODE) != 0) { 32440Sstevel@tonic-gate zperror(zonepath, B_FALSE); 32450Sstevel@tonic-gate err = Z_ERR; 32460Sstevel@tonic-gate goto done; 32470Sstevel@tonic-gate } 32480Sstevel@tonic-gate 32492712Snn35248 if (is_native_zone) 32502712Snn35248 status = do_subproc(cmdbuf); 32512712Snn35248 else 32522712Snn35248 status = do_subproc_interactive(cmdbuf); 32532712Snn35248 32540Sstevel@tonic-gate if (chmod(zonepath, S_IRWXU) != 0) { 32550Sstevel@tonic-gate zperror(zonepath, B_FALSE); 32560Sstevel@tonic-gate err = Z_ERR; 32570Sstevel@tonic-gate goto done; 32580Sstevel@tonic-gate } 32592712Snn35248 if ((subproc_err = 32602712Snn35248 subproc_status(gettext("brand-specific installation"), status, 32612712Snn35248 B_FALSE)) != ZONE_SUBPROC_OK) { 32622712Snn35248 err = Z_ERR; 32630Sstevel@tonic-gate goto done; 32642712Snn35248 } 32650Sstevel@tonic-gate 32660Sstevel@tonic-gate if ((err = zone_set_state(target_zone, ZONE_STATE_INSTALLED)) != Z_OK) { 32670Sstevel@tonic-gate errno = err; 32680Sstevel@tonic-gate zperror2(target_zone, gettext("could not set state")); 32690Sstevel@tonic-gate goto done; 32700Sstevel@tonic-gate } 32710Sstevel@tonic-gate 32720Sstevel@tonic-gate done: 32732712Snn35248 /* 32742712Snn35248 * If the install script exited with ZONE_SUBPROC_USAGE or 32752712Snn35248 * ZONE_SUBPROC_NOTCOMPLETE, try to clean up the zone and leave the 32762712Snn35248 * zone in the CONFIGURED state so that another install can be 32772712Snn35248 * attempted without requiring an uninstall first. 32782712Snn35248 */ 32792712Snn35248 if ((subproc_err == ZONE_SUBPROC_USAGE) || 32802712Snn35248 (subproc_err == ZONE_SUBPROC_NOTCOMPLETE)) { 32812712Snn35248 if ((err = cleanup_zonepath(zonepath, B_FALSE)) != Z_OK) { 32822712Snn35248 errno = err; 32832712Snn35248 zperror2(target_zone, 32842712Snn35248 gettext("cleaning up zonepath failed")); 32852712Snn35248 } else if ((err = zone_set_state(target_zone, 32862712Snn35248 ZONE_STATE_CONFIGURED)) != Z_OK) { 32872712Snn35248 errno = err; 32882712Snn35248 zperror2(target_zone, gettext("could not set state")); 32892712Snn35248 } 32902712Snn35248 } 32912712Snn35248 32920Sstevel@tonic-gate release_lock_file(lockfd); 32930Sstevel@tonic-gate return ((err == Z_OK) ? Z_OK : Z_ERR); 32940Sstevel@tonic-gate } 32950Sstevel@tonic-gate 32960Sstevel@tonic-gate /* 32971300Sgjelinek * Check that the inherited pkg dirs are the same for the clone and its source. 32981300Sgjelinek * The easiest way to do that is check that the list of ipds is the same 32991300Sgjelinek * by matching each one against the other. This algorithm should be fine since 33001300Sgjelinek * the list of ipds should not be that long. 33011300Sgjelinek */ 33021300Sgjelinek static int 33031300Sgjelinek valid_ipd_clone(zone_dochandle_t s_handle, char *source_zone, 33041300Sgjelinek zone_dochandle_t t_handle, char *target_zone) 33051300Sgjelinek { 33061300Sgjelinek int err; 33071300Sgjelinek int res = Z_OK; 33081300Sgjelinek int s_cnt = 0; 33091300Sgjelinek int t_cnt = 0; 33101300Sgjelinek struct zone_fstab s_fstab; 33111300Sgjelinek struct zone_fstab t_fstab; 33121300Sgjelinek 33131300Sgjelinek /* 33141300Sgjelinek * First check the source of the clone against the target. 33151300Sgjelinek */ 33161300Sgjelinek if ((err = zonecfg_setipdent(s_handle)) != Z_OK) { 33171300Sgjelinek errno = err; 33181300Sgjelinek zperror2(source_zone, gettext("could not enumerate " 33191300Sgjelinek "inherit-pkg-dirs")); 33201300Sgjelinek return (Z_ERR); 33211300Sgjelinek } 33221300Sgjelinek 33231300Sgjelinek while (zonecfg_getipdent(s_handle, &s_fstab) == Z_OK) { 33241300Sgjelinek boolean_t match = B_FALSE; 33251300Sgjelinek 33261300Sgjelinek s_cnt++; 33271300Sgjelinek 33281300Sgjelinek if ((err = zonecfg_setipdent(t_handle)) != Z_OK) { 33291300Sgjelinek errno = err; 33301300Sgjelinek zperror2(target_zone, gettext("could not enumerate " 33311300Sgjelinek "inherit-pkg-dirs")); 33321300Sgjelinek (void) zonecfg_endipdent(s_handle); 33331300Sgjelinek return (Z_ERR); 33341300Sgjelinek } 33351300Sgjelinek 33361300Sgjelinek while (zonecfg_getipdent(t_handle, &t_fstab) == Z_OK) { 33371300Sgjelinek if (strcmp(s_fstab.zone_fs_dir, t_fstab.zone_fs_dir) 33381300Sgjelinek == 0) { 33391300Sgjelinek match = B_TRUE; 33401300Sgjelinek break; 33411300Sgjelinek } 33421300Sgjelinek } 33431300Sgjelinek (void) zonecfg_endipdent(t_handle); 33441300Sgjelinek 33451300Sgjelinek if (!match) { 33461300Sgjelinek (void) fprintf(stderr, gettext("inherit-pkg-dir " 33471300Sgjelinek "'%s' is not configured in zone %s.\n"), 33481300Sgjelinek s_fstab.zone_fs_dir, target_zone); 33491300Sgjelinek res = Z_ERR; 33501300Sgjelinek } 33511300Sgjelinek } 33521300Sgjelinek 33531300Sgjelinek (void) zonecfg_endipdent(s_handle); 33541300Sgjelinek 33551300Sgjelinek /* skip the next check if we already have errors */ 33561300Sgjelinek if (res == Z_ERR) 33571300Sgjelinek return (res); 33581300Sgjelinek 33591300Sgjelinek /* 33601300Sgjelinek * Now check the number of ipds in the target so we can verify 33611300Sgjelinek * that the source is not a subset of the target. 33621300Sgjelinek */ 33631300Sgjelinek if ((err = zonecfg_setipdent(t_handle)) != Z_OK) { 33641300Sgjelinek errno = err; 33651300Sgjelinek zperror2(target_zone, gettext("could not enumerate " 33661300Sgjelinek "inherit-pkg-dirs")); 33671300Sgjelinek return (Z_ERR); 33681300Sgjelinek } 33691300Sgjelinek 33701300Sgjelinek while (zonecfg_getipdent(t_handle, &t_fstab) == Z_OK) 33711300Sgjelinek t_cnt++; 33721300Sgjelinek 33731300Sgjelinek (void) zonecfg_endipdent(t_handle); 33741300Sgjelinek 33751300Sgjelinek if (t_cnt != s_cnt) { 33761300Sgjelinek (void) fprintf(stderr, gettext("Zone %s is configured " 33771300Sgjelinek "with inherit-pkg-dirs that are not configured in zone " 33781300Sgjelinek "%s.\n"), target_zone, source_zone); 33791300Sgjelinek res = Z_ERR; 33801300Sgjelinek } 33811300Sgjelinek 33821300Sgjelinek return (res); 33831300Sgjelinek } 33841300Sgjelinek 33851300Sgjelinek static void 33861300Sgjelinek warn_dev_match(zone_dochandle_t s_handle, char *source_zone, 33871300Sgjelinek zone_dochandle_t t_handle, char *target_zone) 33881300Sgjelinek { 33891300Sgjelinek int err; 33901300Sgjelinek struct zone_devtab s_devtab; 33911300Sgjelinek struct zone_devtab t_devtab; 33921300Sgjelinek 33931300Sgjelinek if ((err = zonecfg_setdevent(t_handle)) != Z_OK) { 33941300Sgjelinek errno = err; 33951300Sgjelinek zperror2(target_zone, gettext("could not enumerate devices")); 33961300Sgjelinek return; 33971300Sgjelinek } 33981300Sgjelinek 33991300Sgjelinek while (zonecfg_getdevent(t_handle, &t_devtab) == Z_OK) { 34001300Sgjelinek if ((err = zonecfg_setdevent(s_handle)) != Z_OK) { 34011300Sgjelinek errno = err; 34021300Sgjelinek zperror2(source_zone, 34031300Sgjelinek gettext("could not enumerate devices")); 34041300Sgjelinek (void) zonecfg_enddevent(t_handle); 34051300Sgjelinek return; 34061300Sgjelinek } 34071300Sgjelinek 34081300Sgjelinek while (zonecfg_getdevent(s_handle, &s_devtab) == Z_OK) { 34091300Sgjelinek /* 34101300Sgjelinek * Use fnmatch to catch the case where wildcards 34111300Sgjelinek * were used in one zone and the other has an 34121300Sgjelinek * explicit entry (e.g. /dev/dsk/c0t0d0s6 vs. 34131300Sgjelinek * /dev/\*dsk/c0t0d0s6). 34141300Sgjelinek */ 34151300Sgjelinek if (fnmatch(t_devtab.zone_dev_match, 34161300Sgjelinek s_devtab.zone_dev_match, FNM_PATHNAME) == 0 || 34171300Sgjelinek fnmatch(s_devtab.zone_dev_match, 34181300Sgjelinek t_devtab.zone_dev_match, FNM_PATHNAME) == 0) { 34191300Sgjelinek (void) fprintf(stderr, 34201300Sgjelinek gettext("WARNING: device '%s' " 34211300Sgjelinek "is configured in both zones.\n"), 34221300Sgjelinek t_devtab.zone_dev_match); 34231300Sgjelinek break; 34241300Sgjelinek } 34251300Sgjelinek } 34261300Sgjelinek (void) zonecfg_enddevent(s_handle); 34271300Sgjelinek } 34281300Sgjelinek 34291300Sgjelinek (void) zonecfg_enddevent(t_handle); 34301300Sgjelinek } 34311300Sgjelinek 34321300Sgjelinek /* 34331300Sgjelinek * Check if the specified mount option (opt) is contained within the 34341300Sgjelinek * options string. 34351300Sgjelinek */ 34361300Sgjelinek static boolean_t 34371300Sgjelinek opt_match(char *opt, char *options) 34381300Sgjelinek { 34391300Sgjelinek char *p; 34401300Sgjelinek char *lastp; 34411300Sgjelinek 34421300Sgjelinek if ((p = strtok_r(options, ",", &lastp)) != NULL) { 34431300Sgjelinek if (strcmp(p, opt) == 0) 34441300Sgjelinek return (B_TRUE); 34451300Sgjelinek while ((p = strtok_r(NULL, ",", &lastp)) != NULL) { 34461300Sgjelinek if (strcmp(p, opt) == 0) 34471300Sgjelinek return (B_TRUE); 34481300Sgjelinek } 34491300Sgjelinek } 34501300Sgjelinek 34511300Sgjelinek return (B_FALSE); 34521300Sgjelinek } 34531300Sgjelinek 34541867Sgjelinek #define RW_LOFS "WARNING: read-write lofs file system on '%s' is configured " \ 34551300Sgjelinek "in both zones.\n" 34561300Sgjelinek 34571300Sgjelinek static void 34581300Sgjelinek print_fs_warnings(struct zone_fstab *s_fstab, struct zone_fstab *t_fstab) 34591300Sgjelinek { 34601300Sgjelinek /* 34611300Sgjelinek * It is ok to have shared lofs mounted fs but we want to warn if 34621300Sgjelinek * either is rw since this will effect the other zone. 34631300Sgjelinek */ 34641300Sgjelinek if (strcmp(t_fstab->zone_fs_type, "lofs") == 0) { 34651300Sgjelinek zone_fsopt_t *optp; 34661300Sgjelinek 34671300Sgjelinek /* The default is rw so no options means rw */ 34681300Sgjelinek if (t_fstab->zone_fs_options == NULL || 34691300Sgjelinek s_fstab->zone_fs_options == NULL) { 34701300Sgjelinek (void) fprintf(stderr, gettext(RW_LOFS), 34711300Sgjelinek t_fstab->zone_fs_special); 34721300Sgjelinek return; 34731300Sgjelinek } 34741300Sgjelinek 34751300Sgjelinek for (optp = s_fstab->zone_fs_options; optp != NULL; 34761300Sgjelinek optp = optp->zone_fsopt_next) { 34771300Sgjelinek if (opt_match("rw", optp->zone_fsopt_opt)) { 34781300Sgjelinek (void) fprintf(stderr, gettext(RW_LOFS), 34791300Sgjelinek s_fstab->zone_fs_special); 34801300Sgjelinek return; 34811300Sgjelinek } 34821300Sgjelinek } 34831300Sgjelinek 34841300Sgjelinek for (optp = t_fstab->zone_fs_options; optp != NULL; 34851300Sgjelinek optp = optp->zone_fsopt_next) { 34861300Sgjelinek if (opt_match("rw", optp->zone_fsopt_opt)) { 34871300Sgjelinek (void) fprintf(stderr, gettext(RW_LOFS), 34881300Sgjelinek t_fstab->zone_fs_special); 34891300Sgjelinek return; 34901300Sgjelinek } 34911300Sgjelinek } 34921300Sgjelinek 34931300Sgjelinek return; 34941300Sgjelinek } 34951300Sgjelinek 34961300Sgjelinek /* 34971300Sgjelinek * TRANSLATION_NOTE 34981867Sgjelinek * The first variable is the file system type and the second is 34991867Sgjelinek * the file system special device. For example, 35001867Sgjelinek * WARNING: ufs file system on '/dev/dsk/c0t0d0s0' ... 35011300Sgjelinek */ 35021867Sgjelinek (void) fprintf(stderr, gettext("WARNING: %s file system on '%s' " 35031300Sgjelinek "is configured in both zones.\n"), t_fstab->zone_fs_type, 35041300Sgjelinek t_fstab->zone_fs_special); 35051300Sgjelinek } 35061300Sgjelinek 35071300Sgjelinek static void 35081300Sgjelinek warn_fs_match(zone_dochandle_t s_handle, char *source_zone, 35091300Sgjelinek zone_dochandle_t t_handle, char *target_zone) 35101300Sgjelinek { 35111300Sgjelinek int err; 35121300Sgjelinek struct zone_fstab s_fstab; 35131300Sgjelinek struct zone_fstab t_fstab; 35141300Sgjelinek 35151300Sgjelinek if ((err = zonecfg_setfsent(t_handle)) != Z_OK) { 35161300Sgjelinek errno = err; 35171300Sgjelinek zperror2(target_zone, 35181867Sgjelinek gettext("could not enumerate file systems")); 35191300Sgjelinek return; 35201300Sgjelinek } 35211300Sgjelinek 35221300Sgjelinek while (zonecfg_getfsent(t_handle, &t_fstab) == Z_OK) { 35231300Sgjelinek if ((err = zonecfg_setfsent(s_handle)) != Z_OK) { 35241300Sgjelinek errno = err; 35251300Sgjelinek zperror2(source_zone, 35261867Sgjelinek gettext("could not enumerate file systems")); 35271300Sgjelinek (void) zonecfg_endfsent(t_handle); 35281300Sgjelinek return; 35291300Sgjelinek } 35301300Sgjelinek 35311300Sgjelinek while (zonecfg_getfsent(s_handle, &s_fstab) == Z_OK) { 35321300Sgjelinek if (strcmp(t_fstab.zone_fs_special, 35331300Sgjelinek s_fstab.zone_fs_special) == 0) { 35341300Sgjelinek print_fs_warnings(&s_fstab, &t_fstab); 35351300Sgjelinek break; 35361300Sgjelinek } 35371300Sgjelinek } 35381300Sgjelinek (void) zonecfg_endfsent(s_handle); 35391300Sgjelinek } 35401300Sgjelinek 35411300Sgjelinek (void) zonecfg_endfsent(t_handle); 35421300Sgjelinek } 35431300Sgjelinek 35441300Sgjelinek /* 35451300Sgjelinek * We don't catch the case where you used the same IP address but 35461300Sgjelinek * it is not an exact string match. For example, 192.9.0.128 vs. 192.09.0.128. 35471300Sgjelinek * However, we're not going to worry about that but we will check for 35481300Sgjelinek * a possible netmask on one of the addresses (e.g. 10.0.0.1 and 10.0.0.1/24) 35491300Sgjelinek * and handle that case as a match. 35501300Sgjelinek */ 35511300Sgjelinek static void 35521300Sgjelinek warn_ip_match(zone_dochandle_t s_handle, char *source_zone, 35531300Sgjelinek zone_dochandle_t t_handle, char *target_zone) 35541300Sgjelinek { 35551300Sgjelinek int err; 35561300Sgjelinek struct zone_nwiftab s_nwiftab; 35571300Sgjelinek struct zone_nwiftab t_nwiftab; 35581300Sgjelinek 35591300Sgjelinek if ((err = zonecfg_setnwifent(t_handle)) != Z_OK) { 35601300Sgjelinek errno = err; 35611300Sgjelinek zperror2(target_zone, 35621300Sgjelinek gettext("could not enumerate network interfaces")); 35631300Sgjelinek return; 35641300Sgjelinek } 35651300Sgjelinek 35661300Sgjelinek while (zonecfg_getnwifent(t_handle, &t_nwiftab) == Z_OK) { 35671300Sgjelinek char *p; 35681300Sgjelinek 35691300Sgjelinek /* remove an (optional) netmask from the address */ 35701300Sgjelinek if ((p = strchr(t_nwiftab.zone_nwif_address, '/')) != NULL) 35711300Sgjelinek *p = '\0'; 35721300Sgjelinek 35731300Sgjelinek if ((err = zonecfg_setnwifent(s_handle)) != Z_OK) { 35741300Sgjelinek errno = err; 35751300Sgjelinek zperror2(source_zone, 35761300Sgjelinek gettext("could not enumerate network interfaces")); 35771300Sgjelinek (void) zonecfg_endnwifent(t_handle); 35781300Sgjelinek return; 35791300Sgjelinek } 35801300Sgjelinek 35811300Sgjelinek while (zonecfg_getnwifent(s_handle, &s_nwiftab) == Z_OK) { 35821300Sgjelinek /* remove an (optional) netmask from the address */ 35831300Sgjelinek if ((p = strchr(s_nwiftab.zone_nwif_address, '/')) 35841300Sgjelinek != NULL) 35851300Sgjelinek *p = '\0'; 35861300Sgjelinek 3587*3448Sdh155122 /* For exclusive-IP zones, address is not specified. */ 3588*3448Sdh155122 if (strlen(s_nwiftab.zone_nwif_address) == 0) 3589*3448Sdh155122 continue; 3590*3448Sdh155122 35911300Sgjelinek if (strcmp(t_nwiftab.zone_nwif_address, 35921300Sgjelinek s_nwiftab.zone_nwif_address) == 0) { 35931300Sgjelinek (void) fprintf(stderr, 35941300Sgjelinek gettext("WARNING: network address '%s' " 35951300Sgjelinek "is configured in both zones.\n"), 35961300Sgjelinek t_nwiftab.zone_nwif_address); 35971300Sgjelinek break; 35981300Sgjelinek } 35991300Sgjelinek } 36001300Sgjelinek (void) zonecfg_endnwifent(s_handle); 36011300Sgjelinek } 36021300Sgjelinek 36031300Sgjelinek (void) zonecfg_endnwifent(t_handle); 36041300Sgjelinek } 36051300Sgjelinek 36061300Sgjelinek static void 36072712Snn35248 warn_dataset_match(zone_dochandle_t s_handle, char *source, 36082712Snn35248 zone_dochandle_t t_handle, char *target) 36091300Sgjelinek { 36101300Sgjelinek int err; 36111300Sgjelinek struct zone_dstab s_dstab; 36121300Sgjelinek struct zone_dstab t_dstab; 36131300Sgjelinek 36141300Sgjelinek if ((err = zonecfg_setdsent(t_handle)) != Z_OK) { 36151300Sgjelinek errno = err; 36162712Snn35248 zperror2(target, gettext("could not enumerate datasets")); 36171300Sgjelinek return; 36181300Sgjelinek } 36191300Sgjelinek 36201300Sgjelinek while (zonecfg_getdsent(t_handle, &t_dstab) == Z_OK) { 36211300Sgjelinek if ((err = zonecfg_setdsent(s_handle)) != Z_OK) { 36221300Sgjelinek errno = err; 36232712Snn35248 zperror2(source, 36241300Sgjelinek gettext("could not enumerate datasets")); 36251300Sgjelinek (void) zonecfg_enddsent(t_handle); 36261300Sgjelinek return; 36271300Sgjelinek } 36281300Sgjelinek 36291300Sgjelinek while (zonecfg_getdsent(s_handle, &s_dstab) == Z_OK) { 36301300Sgjelinek if (strcmp(t_dstab.zone_dataset_name, 36311300Sgjelinek s_dstab.zone_dataset_name) == 0) { 36322712Snn35248 target_zone = source; 36332712Snn35248 zerror(gettext("WARNING: dataset '%s' " 36341300Sgjelinek "is configured in both zones.\n"), 36351300Sgjelinek t_dstab.zone_dataset_name); 36361300Sgjelinek break; 36371300Sgjelinek } 36381300Sgjelinek } 36391300Sgjelinek (void) zonecfg_enddsent(s_handle); 36401300Sgjelinek } 36411300Sgjelinek 36421300Sgjelinek (void) zonecfg_enddsent(t_handle); 36431300Sgjelinek } 36441300Sgjelinek 36452712Snn35248 /* 36462712Snn35248 * Check that the clone and its source have the same brand type. 36472712Snn35248 */ 36482712Snn35248 static int 36492712Snn35248 valid_brand_clone(char *source_zone, char *target_zone) 36502712Snn35248 { 36512727Sedp brand_handle_t bh; 36522712Snn35248 char source_brand[MAXNAMELEN]; 36532712Snn35248 36542712Snn35248 if ((zone_get_brand(source_zone, source_brand, 36552712Snn35248 sizeof (source_brand))) != Z_OK) { 36562712Snn35248 (void) fprintf(stderr, "%s: zone '%s': %s\n", 36572712Snn35248 execname, source_zone, gettext("missing or invalid brand")); 36582712Snn35248 return (Z_ERR); 36592712Snn35248 } 36602712Snn35248 36612712Snn35248 if (strcmp(source_brand, target_brand) != NULL) { 36622712Snn35248 (void) fprintf(stderr, 36632712Snn35248 gettext("%s: Zones '%s' and '%s' have different brand " 36642712Snn35248 "types.\n"), execname, source_zone, target_zone); 36652712Snn35248 return (Z_ERR); 36662712Snn35248 } 36672712Snn35248 36682727Sedp if ((bh = brand_open(target_brand)) == NULL) { 36692712Snn35248 zerror(gettext("missing or invalid brand")); 36702712Snn35248 return (Z_ERR); 36712712Snn35248 } 36722727Sedp brand_close(bh); 36732712Snn35248 return (Z_OK); 36742712Snn35248 } 36752712Snn35248 36761300Sgjelinek static int 36771300Sgjelinek validate_clone(char *source_zone, char *target_zone) 36781300Sgjelinek { 36791300Sgjelinek int err = Z_OK; 36801300Sgjelinek zone_dochandle_t s_handle; 36811300Sgjelinek zone_dochandle_t t_handle; 36821300Sgjelinek 36831300Sgjelinek if ((t_handle = zonecfg_init_handle()) == NULL) { 36841300Sgjelinek zperror(cmd_to_str(CMD_CLONE), B_TRUE); 36851300Sgjelinek return (Z_ERR); 36861300Sgjelinek } 36871300Sgjelinek if ((err = zonecfg_get_handle(target_zone, t_handle)) != Z_OK) { 36881300Sgjelinek errno = err; 36891300Sgjelinek zperror(cmd_to_str(CMD_CLONE), B_TRUE); 36901300Sgjelinek zonecfg_fini_handle(t_handle); 36911300Sgjelinek return (Z_ERR); 36921300Sgjelinek } 36931300Sgjelinek 36941300Sgjelinek if ((s_handle = zonecfg_init_handle()) == NULL) { 36951300Sgjelinek zperror(cmd_to_str(CMD_CLONE), B_TRUE); 36961300Sgjelinek zonecfg_fini_handle(t_handle); 36971300Sgjelinek return (Z_ERR); 36981300Sgjelinek } 36991300Sgjelinek if ((err = zonecfg_get_handle(source_zone, s_handle)) != Z_OK) { 37001300Sgjelinek errno = err; 37011300Sgjelinek zperror(cmd_to_str(CMD_CLONE), B_TRUE); 37021300Sgjelinek goto done; 37031300Sgjelinek } 37041300Sgjelinek 37052712Snn35248 /* verify new zone has same brand type */ 37062712Snn35248 err = valid_brand_clone(source_zone, target_zone); 37072712Snn35248 if (err != Z_OK) 37082712Snn35248 goto done; 37092712Snn35248 37101300Sgjelinek /* verify new zone has same inherit-pkg-dirs */ 37111300Sgjelinek err = valid_ipd_clone(s_handle, source_zone, t_handle, target_zone); 37121300Sgjelinek 37131300Sgjelinek /* warn about imported fs's which are the same */ 37141300Sgjelinek warn_fs_match(s_handle, source_zone, t_handle, target_zone); 37151300Sgjelinek 37161300Sgjelinek /* warn about imported IP addresses which are the same */ 37171300Sgjelinek warn_ip_match(s_handle, source_zone, t_handle, target_zone); 37181300Sgjelinek 37191300Sgjelinek /* warn about imported devices which are the same */ 37201300Sgjelinek warn_dev_match(s_handle, source_zone, t_handle, target_zone); 37211300Sgjelinek 37221300Sgjelinek /* warn about imported datasets which are the same */ 37231300Sgjelinek warn_dataset_match(s_handle, source_zone, t_handle, target_zone); 37241300Sgjelinek 37251300Sgjelinek done: 37261300Sgjelinek zonecfg_fini_handle(t_handle); 37271300Sgjelinek zonecfg_fini_handle(s_handle); 37281300Sgjelinek 37291300Sgjelinek return ((err == Z_OK) ? Z_OK : Z_ERR); 37301300Sgjelinek } 37311300Sgjelinek 37321300Sgjelinek static int 37331300Sgjelinek copy_zone(char *src, char *dst) 37341300Sgjelinek { 37351300Sgjelinek boolean_t out_null = B_FALSE; 37361300Sgjelinek int status; 37371300Sgjelinek char *outfile; 37381300Sgjelinek char cmdbuf[MAXPATHLEN * 2 + 128]; 37391300Sgjelinek 37401300Sgjelinek if ((outfile = tempnam("/var/log", "zone")) == NULL) { 37411300Sgjelinek outfile = "/dev/null"; 37421300Sgjelinek out_null = B_TRUE; 37431300Sgjelinek } 37441300Sgjelinek 37451867Sgjelinek /* 37461867Sgjelinek * Use find to get the list of files to copy. We need to skip 37471867Sgjelinek * files of type "socket" since cpio can't handle those but that 37481867Sgjelinek * should be ok since the app will recreate the socket when it runs. 37491867Sgjelinek * We also need to filter out anything under the .zfs subdir. Since 37501867Sgjelinek * find is running depth-first, we need the extra egrep to filter .zfs. 37511867Sgjelinek */ 37521300Sgjelinek (void) snprintf(cmdbuf, sizeof (cmdbuf), 37531867Sgjelinek "cd %s && /usr/bin/find . -type s -prune -o -depth -print | " 37541607Sgjelinek "/usr/bin/egrep -v '^\\./\\.zfs$|^\\./\\.zfs/' | " 37551300Sgjelinek "/usr/bin/cpio -pdmuP@ %s > %s 2>&1", 37561300Sgjelinek src, dst, outfile); 37571300Sgjelinek 37581300Sgjelinek status = do_subproc(cmdbuf); 37591300Sgjelinek 37602712Snn35248 if (subproc_status("copy", status, B_TRUE) != ZONE_SUBPROC_OK) { 37611300Sgjelinek if (!out_null) 37621300Sgjelinek (void) fprintf(stderr, gettext("\nThe copy failed.\n" 37631300Sgjelinek "More information can be found in %s\n"), outfile); 37642712Snn35248 return (Z_ERR); 37651300Sgjelinek } 37661300Sgjelinek 37671300Sgjelinek if (!out_null) 37681300Sgjelinek (void) unlink(outfile); 37691300Sgjelinek 37701300Sgjelinek return (Z_OK); 37711300Sgjelinek } 37721300Sgjelinek 37731300Sgjelinek static int 37742712Snn35248 zone_postclone(char *zonepath) 37751300Sgjelinek { 37762712Snn35248 char cmdbuf[MAXPATHLEN]; 37772712Snn35248 int status; 37782727Sedp brand_handle_t bh; 37792712Snn35248 int err = Z_OK; 37801676Sjpk 37811676Sjpk /* 37822712Snn35248 * Fetch the post-clone command, if any, from the brand 37832712Snn35248 * configuration. 37841568Sgjelinek */ 37852727Sedp if ((bh = brand_open(target_brand)) == NULL) { 37862712Snn35248 zerror(gettext("missing or invalid brand")); 37871568Sgjelinek return (Z_ERR); 37881568Sgjelinek } 37892712Snn35248 (void) strcpy(cmdbuf, EXEC_PREFIX); 37902727Sedp err = brand_get_postclone(bh, target_zone, zonepath, cmdbuf + EXEC_LEN, 37912712Snn35248 sizeof (cmdbuf) - EXEC_LEN, 0, NULL); 37922727Sedp brand_close(bh); 37932712Snn35248 37942712Snn35248 if (err == 0 && strlen(cmdbuf) > EXEC_LEN) { 37952712Snn35248 status = do_subproc(cmdbuf); 37962712Snn35248 if ((err = subproc_status("postclone", status, B_FALSE)) 37972712Snn35248 != ZONE_SUBPROC_OK) { 37982712Snn35248 zerror(gettext("post-clone configuration failed.")); 37992712Snn35248 err = Z_ERR; 38002712Snn35248 } 38012712Snn35248 } 38022712Snn35248 38032712Snn35248 return (err); 38041300Sgjelinek } 38051300Sgjelinek 38061300Sgjelinek /* ARGSUSED */ 38071867Sgjelinek static int 38081300Sgjelinek zfm_print(const char *p, void *r) { 38091300Sgjelinek zerror(" %s\n", p); 38101300Sgjelinek return (0); 38111300Sgjelinek } 38121300Sgjelinek 38131867Sgjelinek int 38141867Sgjelinek clone_copy(char *source_zonepath, char *zonepath) 38151867Sgjelinek { 38161867Sgjelinek int err; 38171867Sgjelinek 38181867Sgjelinek /* Don't clone the zone if anything is still mounted there */ 38191867Sgjelinek if (zonecfg_find_mounts(source_zonepath, NULL, NULL)) { 38201867Sgjelinek zerror(gettext("These file systems are mounted on " 38211867Sgjelinek "subdirectories of %s.\n"), source_zonepath); 38221867Sgjelinek (void) zonecfg_find_mounts(source_zonepath, zfm_print, NULL); 38231867Sgjelinek return (Z_ERR); 38241867Sgjelinek } 38251867Sgjelinek 38261867Sgjelinek /* 38271867Sgjelinek * Attempt to create a ZFS fs for the zonepath. As usual, we don't 38281867Sgjelinek * care if this works or not since we always have the default behavior 38291867Sgjelinek * of a simple directory for the zonepath. 38301867Sgjelinek */ 38311867Sgjelinek create_zfs_zonepath(zonepath); 38321867Sgjelinek 38331867Sgjelinek (void) printf(gettext("Copying %s..."), source_zonepath); 38341867Sgjelinek (void) fflush(stdout); 38351867Sgjelinek 38361867Sgjelinek err = copy_zone(source_zonepath, zonepath); 38371867Sgjelinek 38381867Sgjelinek (void) printf("\n"); 38391867Sgjelinek 38401867Sgjelinek return (err); 38411867Sgjelinek } 38421867Sgjelinek 38431300Sgjelinek static int 38441300Sgjelinek clone_func(int argc, char *argv[]) 38451300Sgjelinek { 38461300Sgjelinek char *source_zone = NULL; 38471300Sgjelinek int lockfd; 38481300Sgjelinek int err, arg; 38491300Sgjelinek char zonepath[MAXPATHLEN]; 38501300Sgjelinek char source_zonepath[MAXPATHLEN]; 38511300Sgjelinek zone_state_t state; 38521300Sgjelinek zone_entry_t *zent; 38531867Sgjelinek char *method = NULL; 38541867Sgjelinek char *snapshot = NULL; 38551300Sgjelinek 38561300Sgjelinek if (zonecfg_in_alt_root()) { 38571300Sgjelinek zerror(gettext("cannot clone zone in alternate root")); 38581300Sgjelinek return (Z_ERR); 38591300Sgjelinek } 38601300Sgjelinek 38611300Sgjelinek optind = 0; 38621867Sgjelinek if ((arg = getopt(argc, argv, "?m:s:")) != EOF) { 38631300Sgjelinek switch (arg) { 38641300Sgjelinek case '?': 38651300Sgjelinek sub_usage(SHELP_CLONE, CMD_CLONE); 38661300Sgjelinek return (optopt == '?' ? Z_OK : Z_USAGE); 38671300Sgjelinek case 'm': 38681300Sgjelinek method = optarg; 38691300Sgjelinek break; 38701867Sgjelinek case 's': 38711867Sgjelinek snapshot = optarg; 38721867Sgjelinek break; 38731300Sgjelinek default: 38741300Sgjelinek sub_usage(SHELP_CLONE, CMD_CLONE); 38751300Sgjelinek return (Z_USAGE); 38761300Sgjelinek } 38771300Sgjelinek } 38781867Sgjelinek if (argc != (optind + 1) || 38791867Sgjelinek (method != NULL && strcmp(method, "copy") != 0)) { 38801300Sgjelinek sub_usage(SHELP_CLONE, CMD_CLONE); 38811300Sgjelinek return (Z_USAGE); 38821300Sgjelinek } 38831300Sgjelinek source_zone = argv[optind]; 38842712Snn35248 if (sanity_check(target_zone, CMD_CLONE, B_FALSE, B_TRUE, B_FALSE) 38852712Snn35248 != Z_OK) 38861300Sgjelinek return (Z_ERR); 38873339Szt129084 if (verify_details(CMD_CLONE, argv) != Z_OK) 38881300Sgjelinek return (Z_ERR); 38891300Sgjelinek 38901300Sgjelinek /* 38911300Sgjelinek * We also need to do some extra validation on the source zone. 38921300Sgjelinek */ 38931300Sgjelinek if (strcmp(source_zone, GLOBAL_ZONENAME) == 0) { 38941300Sgjelinek zerror(gettext("%s operation is invalid for the global zone."), 38951300Sgjelinek cmd_to_str(CMD_CLONE)); 38961300Sgjelinek return (Z_ERR); 38971300Sgjelinek } 38981300Sgjelinek 38991300Sgjelinek if (strncmp(source_zone, "SUNW", 4) == 0) { 39001300Sgjelinek zerror(gettext("%s operation is invalid for zones starting " 39011300Sgjelinek "with SUNW."), cmd_to_str(CMD_CLONE)); 39021300Sgjelinek return (Z_ERR); 39031300Sgjelinek } 39041300Sgjelinek 39051300Sgjelinek zent = lookup_running_zone(source_zone); 39061300Sgjelinek if (zent != NULL) { 39071300Sgjelinek /* check whether the zone is ready or running */ 39081300Sgjelinek if ((err = zone_get_state(zent->zname, &zent->zstate_num)) 39091300Sgjelinek != Z_OK) { 39101300Sgjelinek errno = err; 39111300Sgjelinek zperror2(zent->zname, gettext("could not get state")); 39121300Sgjelinek /* can't tell, so hedge */ 39131300Sgjelinek zent->zstate_str = "ready/running"; 39141300Sgjelinek } else { 39151300Sgjelinek zent->zstate_str = zone_state_str(zent->zstate_num); 39161300Sgjelinek } 39171300Sgjelinek zerror(gettext("%s operation is invalid for %s zones."), 39181300Sgjelinek cmd_to_str(CMD_CLONE), zent->zstate_str); 39191300Sgjelinek return (Z_ERR); 39201300Sgjelinek } 39211300Sgjelinek 39221300Sgjelinek if ((err = zone_get_state(source_zone, &state)) != Z_OK) { 39231300Sgjelinek errno = err; 39241300Sgjelinek zperror2(source_zone, gettext("could not get state")); 39251300Sgjelinek return (Z_ERR); 39261300Sgjelinek } 39271300Sgjelinek if (state != ZONE_STATE_INSTALLED) { 39281300Sgjelinek (void) fprintf(stderr, 39291300Sgjelinek gettext("%s: zone %s is %s; %s is required.\n"), 39301300Sgjelinek execname, source_zone, zone_state_str(state), 39311300Sgjelinek zone_state_str(ZONE_STATE_INSTALLED)); 39321300Sgjelinek return (Z_ERR); 39331300Sgjelinek } 39341300Sgjelinek 39351300Sgjelinek /* 39361300Sgjelinek * The source zone checks out ok, continue with the clone. 39371300Sgjelinek */ 39381300Sgjelinek 39391300Sgjelinek if (validate_clone(source_zone, target_zone) != Z_OK) 39401300Sgjelinek return (Z_ERR); 39411300Sgjelinek 39421300Sgjelinek if (grab_lock_file(target_zone, &lockfd) != Z_OK) { 39431300Sgjelinek zerror(gettext("another %s may have an operation in progress."), 39441300Sgjelinek "zoneadm"); 39451300Sgjelinek return (Z_ERR); 39461300Sgjelinek } 39471300Sgjelinek 39481300Sgjelinek if ((err = zone_get_zonepath(source_zone, source_zonepath, 39491300Sgjelinek sizeof (source_zonepath))) != Z_OK) { 39501300Sgjelinek errno = err; 39511300Sgjelinek zperror2(source_zone, gettext("could not get zone path")); 39521300Sgjelinek goto done; 39531300Sgjelinek } 39541300Sgjelinek 39551300Sgjelinek if ((err = zone_get_zonepath(target_zone, zonepath, sizeof (zonepath))) 39561300Sgjelinek != Z_OK) { 39571300Sgjelinek errno = err; 39581300Sgjelinek zperror2(target_zone, gettext("could not get zone path")); 39591300Sgjelinek goto done; 39601300Sgjelinek } 39611300Sgjelinek 39621300Sgjelinek if ((err = zone_set_state(target_zone, ZONE_STATE_INCOMPLETE)) 39631300Sgjelinek != Z_OK) { 39641300Sgjelinek errno = err; 39651300Sgjelinek zperror2(target_zone, gettext("could not set state")); 39661300Sgjelinek goto done; 39671300Sgjelinek } 39681300Sgjelinek 39691867Sgjelinek if (snapshot != NULL) { 39701867Sgjelinek err = clone_snapshot_zfs(snapshot, zonepath); 39711867Sgjelinek } else { 39721867Sgjelinek /* 39731867Sgjelinek * We always copy the clone unless the source is ZFS and a 39741867Sgjelinek * ZFS clone worked. We fallback to copying if the ZFS clone 39751867Sgjelinek * fails for some reason. 39761867Sgjelinek */ 39771867Sgjelinek err = Z_ERR; 39781867Sgjelinek if (method == NULL && is_zonepath_zfs(source_zonepath)) 39791867Sgjelinek err = clone_zfs(source_zone, source_zonepath, zonepath); 39801867Sgjelinek 39811867Sgjelinek if (err != Z_OK) 39821867Sgjelinek err = clone_copy(source_zonepath, zonepath); 39831867Sgjelinek } 39841867Sgjelinek 39852712Snn35248 /* 39862712Snn35248 * Trusted Extensions requires that cloned zones use the same sysid 39872712Snn35248 * configuration, so it is not appropriate to perform any 39882712Snn35248 * post-clone reconfiguration. 39892712Snn35248 */ 39902712Snn35248 if ((err == Z_OK) && !is_system_labeled()) 39912712Snn35248 err = zone_postclone(zonepath); 39921300Sgjelinek 39931300Sgjelinek done: 39942712Snn35248 /* 39952712Snn35248 * If everything went well, we mark the zone as installed. 39962712Snn35248 */ 39972712Snn35248 if (err == Z_OK) { 39982712Snn35248 err = zone_set_state(target_zone, ZONE_STATE_INSTALLED); 39992712Snn35248 if (err != Z_OK) { 40002712Snn35248 errno = err; 40012712Snn35248 zperror2(target_zone, gettext("could not set state")); 40022712Snn35248 } 40032712Snn35248 } 40041300Sgjelinek release_lock_file(lockfd); 40051300Sgjelinek return ((err == Z_OK) ? Z_OK : Z_ERR); 40061300Sgjelinek } 40071300Sgjelinek 40081607Sgjelinek /* 40091867Sgjelinek * Used when removing a zonepath after uninstalling or cleaning up after 40101867Sgjelinek * the move subcommand. This handles a zonepath that has non-standard 40111867Sgjelinek * contents so that we will only cleanup the stuff we know about and leave 40121867Sgjelinek * any user data alone. 40131607Sgjelinek * 40141867Sgjelinek * If the "all" parameter is true then we should remove the whole zonepath 40151867Sgjelinek * even if it has non-standard files/directories in it. This can be used when 40161867Sgjelinek * we need to cleanup after moving the zonepath across file systems. 40171867Sgjelinek * 40181867Sgjelinek * We "exec" the RMCOMMAND so that the returned status is that of RMCOMMAND 40191867Sgjelinek * and not the shell. 40201607Sgjelinek */ 40211607Sgjelinek static int 40221867Sgjelinek cleanup_zonepath(char *zonepath, boolean_t all) 40231607Sgjelinek { 40241867Sgjelinek int status; 40251867Sgjelinek int i; 40261867Sgjelinek boolean_t non_std = B_FALSE; 40271867Sgjelinek struct dirent *dp; 40281867Sgjelinek DIR *dirp; 40291867Sgjelinek char *std_entries[] = {"dev", "lu", "root", NULL}; 40301867Sgjelinek /* (MAXPATHLEN * 3) is for the 3 std_entries dirs */ 40311867Sgjelinek char cmdbuf[sizeof (RMCOMMAND) + (MAXPATHLEN * 3) + 64]; 40321867Sgjelinek 40331867Sgjelinek /* 40341867Sgjelinek * We shouldn't need these checks but lets be paranoid since we 40351867Sgjelinek * could blow away the whole system here if we got the wrong zonepath. 40361867Sgjelinek */ 40371867Sgjelinek if (*zonepath == NULL || strcmp(zonepath, "/") == 0) { 40381867Sgjelinek (void) fprintf(stderr, "invalid zonepath '%s'\n", zonepath); 40391867Sgjelinek return (Z_INVAL); 40401867Sgjelinek } 40411867Sgjelinek 40421867Sgjelinek /* 40431867Sgjelinek * If the dirpath is already gone (maybe it was manually removed) then 40441867Sgjelinek * we just return Z_OK so that the cleanup is successful. 40451867Sgjelinek */ 40461867Sgjelinek if ((dirp = opendir(zonepath)) == NULL) 40471867Sgjelinek return (Z_OK); 40481867Sgjelinek 40491867Sgjelinek /* 40501867Sgjelinek * Look through the zonepath directory to see if there are any 40511867Sgjelinek * non-standard files/dirs. Also skip .zfs since that might be 40521867Sgjelinek * there but we'll handle ZFS file systems as a special case. 40531867Sgjelinek */ 40541867Sgjelinek while ((dp = readdir(dirp)) != NULL) { 40551867Sgjelinek if (strcmp(dp->d_name, ".") == 0 || 40561867Sgjelinek strcmp(dp->d_name, "..") == 0 || 40571867Sgjelinek strcmp(dp->d_name, ".zfs") == 0) 40581867Sgjelinek continue; 40591867Sgjelinek 40601867Sgjelinek for (i = 0; std_entries[i] != NULL; i++) 40611867Sgjelinek if (strcmp(dp->d_name, std_entries[i]) == 0) 40621867Sgjelinek break; 40631867Sgjelinek 40641867Sgjelinek if (std_entries[i] == NULL) 40651867Sgjelinek non_std = B_TRUE; 40661867Sgjelinek } 40671867Sgjelinek (void) closedir(dirp); 40681867Sgjelinek 40691867Sgjelinek if (!all && non_std) { 40701607Sgjelinek /* 40711867Sgjelinek * There are extra, non-standard directories/files in the 40721867Sgjelinek * zonepath so we don't want to remove the zonepath. We 40731867Sgjelinek * just want to remove the standard directories and leave 40741867Sgjelinek * the user data alone. 40751607Sgjelinek */ 40761867Sgjelinek (void) snprintf(cmdbuf, sizeof (cmdbuf), "exec " RMCOMMAND); 40771867Sgjelinek 40781867Sgjelinek for (i = 0; std_entries[i] != NULL; i++) { 40791867Sgjelinek char tmpbuf[MAXPATHLEN]; 40801867Sgjelinek 40811867Sgjelinek if (snprintf(tmpbuf, sizeof (tmpbuf), " %s/%s", 40821867Sgjelinek zonepath, std_entries[i]) >= sizeof (tmpbuf) || 40831867Sgjelinek strlcat(cmdbuf, tmpbuf, sizeof (cmdbuf)) >= 40841867Sgjelinek sizeof (cmdbuf)) { 40851867Sgjelinek (void) fprintf(stderr, 40861867Sgjelinek gettext("path is too long\n")); 40871867Sgjelinek return (Z_INVAL); 40881867Sgjelinek } 40891867Sgjelinek } 40901867Sgjelinek 40911867Sgjelinek status = do_subproc(cmdbuf); 40921867Sgjelinek 40931867Sgjelinek (void) fprintf(stderr, gettext("WARNING: Unable to completely " 40941867Sgjelinek "remove %s\nbecause it contains additional user data. " 40951867Sgjelinek "Only the standard directory\nentries have been " 40961867Sgjelinek "removed.\n"), 40971867Sgjelinek zonepath); 40981867Sgjelinek 40992712Snn35248 return ((subproc_status(RMCOMMAND, status, B_TRUE) == 41002712Snn35248 ZONE_SUBPROC_OK) ? Z_OK : Z_ERR); 41011607Sgjelinek } 41021607Sgjelinek 41031867Sgjelinek /* 41041867Sgjelinek * There is nothing unexpected in the zonepath, try to get rid of the 41051867Sgjelinek * whole zonepath directory. 41061867Sgjelinek * 41071867Sgjelinek * If the zonepath is its own zfs file system, try to destroy the 41081867Sgjelinek * file system. If that fails for some reason (e.g. it has clones) 41091867Sgjelinek * then we'll just remove the contents of the zonepath. 41101867Sgjelinek */ 41111867Sgjelinek if (is_zonepath_zfs(zonepath)) { 41121867Sgjelinek if (destroy_zfs(zonepath) == Z_OK) 41131867Sgjelinek return (Z_OK); 41141867Sgjelinek (void) snprintf(cmdbuf, sizeof (cmdbuf), "exec " RMCOMMAND 41151867Sgjelinek " %s/*", zonepath); 41161867Sgjelinek status = do_subproc(cmdbuf); 41172712Snn35248 return ((subproc_status(RMCOMMAND, status, B_TRUE) == 41182712Snn35248 ZONE_SUBPROC_OK) ? Z_OK : Z_ERR); 41191867Sgjelinek } 41201867Sgjelinek 41211867Sgjelinek (void) snprintf(cmdbuf, sizeof (cmdbuf), "exec " RMCOMMAND " %s", 41221867Sgjelinek zonepath); 41231607Sgjelinek status = do_subproc(cmdbuf); 41242712Snn35248 41252712Snn35248 return ((subproc_status(RMCOMMAND, status, B_TRUE) == ZONE_SUBPROC_OK) 41262712Snn35248 ? Z_OK : Z_ERR); 41271607Sgjelinek } 41281607Sgjelinek 41291300Sgjelinek static int 41301300Sgjelinek move_func(int argc, char *argv[]) 41311300Sgjelinek { 41321300Sgjelinek char *new_zonepath = NULL; 41331300Sgjelinek int lockfd; 41341300Sgjelinek int err, arg; 41351300Sgjelinek char zonepath[MAXPATHLEN]; 41361300Sgjelinek zone_dochandle_t handle; 41371300Sgjelinek boolean_t fast; 41381867Sgjelinek boolean_t is_zfs = B_FALSE; 41391867Sgjelinek struct dirent *dp; 41401867Sgjelinek DIR *dirp; 41411867Sgjelinek boolean_t empty = B_TRUE; 41421300Sgjelinek boolean_t revert; 41431300Sgjelinek struct stat zonepath_buf; 41441300Sgjelinek struct stat new_zonepath_buf; 41451300Sgjelinek 41461300Sgjelinek if (zonecfg_in_alt_root()) { 41471300Sgjelinek zerror(gettext("cannot move zone in alternate root")); 41481300Sgjelinek return (Z_ERR); 41491300Sgjelinek } 41501300Sgjelinek 41511300Sgjelinek optind = 0; 41521300Sgjelinek if ((arg = getopt(argc, argv, "?")) != EOF) { 41531300Sgjelinek switch (arg) { 41541300Sgjelinek case '?': 41551300Sgjelinek sub_usage(SHELP_MOVE, CMD_MOVE); 41561300Sgjelinek return (optopt == '?' ? Z_OK : Z_USAGE); 41571300Sgjelinek default: 41581300Sgjelinek sub_usage(SHELP_MOVE, CMD_MOVE); 41591300Sgjelinek return (Z_USAGE); 41601300Sgjelinek } 41611300Sgjelinek } 41621300Sgjelinek if (argc != (optind + 1)) { 41631300Sgjelinek sub_usage(SHELP_MOVE, CMD_MOVE); 41641300Sgjelinek return (Z_USAGE); 41651300Sgjelinek } 41661300Sgjelinek new_zonepath = argv[optind]; 41672712Snn35248 if (sanity_check(target_zone, CMD_MOVE, B_FALSE, B_TRUE, B_FALSE) 41682712Snn35248 != Z_OK) 41691300Sgjelinek return (Z_ERR); 41703339Szt129084 if (verify_details(CMD_MOVE, argv) != Z_OK) 41711300Sgjelinek return (Z_ERR); 41721300Sgjelinek 41731300Sgjelinek /* 41741300Sgjelinek * Check out the new zonepath. This has the side effect of creating 41751300Sgjelinek * a directory for the new zonepath. We depend on this later when we 41761867Sgjelinek * stat to see if we are doing a cross file system move or not. 41771300Sgjelinek */ 41781300Sgjelinek if (validate_zonepath(new_zonepath, CMD_MOVE) != Z_OK) 41791300Sgjelinek return (Z_ERR); 41801300Sgjelinek 41811300Sgjelinek if ((err = zone_get_zonepath(target_zone, zonepath, sizeof (zonepath))) 41821300Sgjelinek != Z_OK) { 41831300Sgjelinek errno = err; 41841300Sgjelinek zperror2(target_zone, gettext("could not get zone path")); 41851300Sgjelinek return (Z_ERR); 41861300Sgjelinek } 41871300Sgjelinek 41881300Sgjelinek if (stat(zonepath, &zonepath_buf) == -1) { 41891300Sgjelinek zperror(gettext("could not stat zone path"), B_FALSE); 41901300Sgjelinek return (Z_ERR); 41911300Sgjelinek } 41921300Sgjelinek 41931300Sgjelinek if (stat(new_zonepath, &new_zonepath_buf) == -1) { 41941300Sgjelinek zperror(gettext("could not stat new zone path"), B_FALSE); 41951300Sgjelinek return (Z_ERR); 41961300Sgjelinek } 41971300Sgjelinek 41981867Sgjelinek /* 41991867Sgjelinek * Check if the destination directory is empty. 42001867Sgjelinek */ 42011867Sgjelinek if ((dirp = opendir(new_zonepath)) == NULL) { 42021867Sgjelinek zperror(gettext("could not open new zone path"), B_FALSE); 42031867Sgjelinek return (Z_ERR); 42041867Sgjelinek } 42051867Sgjelinek while ((dp = readdir(dirp)) != (struct dirent *)0) { 42061867Sgjelinek if (strcmp(dp->d_name, ".") == 0 || 42071867Sgjelinek strcmp(dp->d_name, "..") == 0) 42081867Sgjelinek continue; 42091867Sgjelinek empty = B_FALSE; 42101867Sgjelinek break; 42111867Sgjelinek } 42121867Sgjelinek (void) closedir(dirp); 42131867Sgjelinek 42141867Sgjelinek /* Error if there is anything in the destination directory. */ 42151867Sgjelinek if (!empty) { 42161867Sgjelinek (void) fprintf(stderr, gettext("could not move zone to %s: " 42171867Sgjelinek "directory not empty\n"), new_zonepath); 42181867Sgjelinek return (Z_ERR); 42191867Sgjelinek } 42201867Sgjelinek 42211300Sgjelinek /* Don't move the zone if anything is still mounted there */ 42221300Sgjelinek if (zonecfg_find_mounts(zonepath, NULL, NULL)) { 42231867Sgjelinek zerror(gettext("These file systems are mounted on " 42241300Sgjelinek "subdirectories of %s.\n"), zonepath); 42251300Sgjelinek (void) zonecfg_find_mounts(zonepath, zfm_print, NULL); 42261300Sgjelinek return (Z_ERR); 42271300Sgjelinek } 42281300Sgjelinek 42291300Sgjelinek /* 42301867Sgjelinek * Check if we are moving in the same file system and can do a fast 42311867Sgjelinek * move or if we are crossing file systems and have to copy the data. 42321300Sgjelinek */ 42331300Sgjelinek fast = (zonepath_buf.st_dev == new_zonepath_buf.st_dev); 42341300Sgjelinek 42351300Sgjelinek if ((handle = zonecfg_init_handle()) == NULL) { 42361300Sgjelinek zperror(cmd_to_str(CMD_MOVE), B_TRUE); 42371300Sgjelinek return (Z_ERR); 42381300Sgjelinek } 42391300Sgjelinek 42401300Sgjelinek if ((err = zonecfg_get_handle(target_zone, handle)) != Z_OK) { 42411300Sgjelinek errno = err; 42421300Sgjelinek zperror(cmd_to_str(CMD_MOVE), B_TRUE); 42431300Sgjelinek zonecfg_fini_handle(handle); 42441300Sgjelinek return (Z_ERR); 42451300Sgjelinek } 42461300Sgjelinek 42471300Sgjelinek if (grab_lock_file(target_zone, &lockfd) != Z_OK) { 42481300Sgjelinek zerror(gettext("another %s may have an operation in progress."), 42491300Sgjelinek "zoneadm"); 42501300Sgjelinek zonecfg_fini_handle(handle); 42511300Sgjelinek return (Z_ERR); 42521300Sgjelinek } 42531300Sgjelinek 42541300Sgjelinek /* 42551867Sgjelinek * We're making some file system changes now so we have to clean up 42561867Sgjelinek * the file system before we are done. This will either clean up the 42571300Sgjelinek * new zonepath if the zonecfg update failed or it will clean up the 42581300Sgjelinek * old zonepath if everything is ok. 42591300Sgjelinek */ 42601300Sgjelinek revert = B_TRUE; 42611300Sgjelinek 42621867Sgjelinek if (is_zonepath_zfs(zonepath) && 42631867Sgjelinek move_zfs(zonepath, new_zonepath) != Z_ERR) { 42641867Sgjelinek is_zfs = B_TRUE; 42651867Sgjelinek 42661867Sgjelinek } else if (fast) { 42671867Sgjelinek /* same file system, use rename for a quick move */ 42681300Sgjelinek 42691300Sgjelinek /* 42701300Sgjelinek * Remove the new_zonepath directory that got created above 42711300Sgjelinek * during the validation. It gets in the way of the rename. 42721300Sgjelinek */ 42731300Sgjelinek if (rmdir(new_zonepath) != 0) { 42741300Sgjelinek zperror(gettext("could not rmdir new zone path"), 42751300Sgjelinek B_FALSE); 42761300Sgjelinek zonecfg_fini_handle(handle); 42771300Sgjelinek release_lock_file(lockfd); 42781300Sgjelinek return (Z_ERR); 42791300Sgjelinek } 42801300Sgjelinek 42811300Sgjelinek if (rename(zonepath, new_zonepath) != 0) { 42821300Sgjelinek /* 42831300Sgjelinek * If this fails we don't need to do all of the 42841300Sgjelinek * cleanup that happens for the rest of the code 42851300Sgjelinek * so just return from this error. 42861300Sgjelinek */ 42871300Sgjelinek zperror(gettext("could not move zone"), B_FALSE); 42881300Sgjelinek zonecfg_fini_handle(handle); 42891300Sgjelinek release_lock_file(lockfd); 42901300Sgjelinek return (Z_ERR); 42911300Sgjelinek } 42921300Sgjelinek 42931300Sgjelinek } else { 42941867Sgjelinek /* 42951867Sgjelinek * Attempt to create a ZFS fs for the new zonepath. As usual, 42961867Sgjelinek * we don't care if this works or not since we always have the 42971867Sgjelinek * default behavior of a simple directory for the zonepath. 42981867Sgjelinek */ 42991867Sgjelinek create_zfs_zonepath(new_zonepath); 43001867Sgjelinek 43011300Sgjelinek (void) printf(gettext( 43021867Sgjelinek "Moving across file systems; copying zonepath %s..."), 43031300Sgjelinek zonepath); 43041300Sgjelinek (void) fflush(stdout); 43051300Sgjelinek 43061300Sgjelinek err = copy_zone(zonepath, new_zonepath); 43071300Sgjelinek 43081300Sgjelinek (void) printf("\n"); 43091300Sgjelinek if (err != Z_OK) 43101300Sgjelinek goto done; 43111300Sgjelinek } 43121300Sgjelinek 43131300Sgjelinek if ((err = zonecfg_set_zonepath(handle, new_zonepath)) != Z_OK) { 43141300Sgjelinek errno = err; 43151300Sgjelinek zperror(gettext("could not set new zonepath"), B_TRUE); 43161300Sgjelinek goto done; 43171300Sgjelinek } 43181300Sgjelinek 43191300Sgjelinek if ((err = zonecfg_save(handle)) != Z_OK) { 43201300Sgjelinek errno = err; 43211300Sgjelinek zperror(gettext("zonecfg save failed"), B_TRUE); 43221300Sgjelinek goto done; 43231300Sgjelinek } 43241300Sgjelinek 43251300Sgjelinek revert = B_FALSE; 43261300Sgjelinek 43271300Sgjelinek done: 43281300Sgjelinek zonecfg_fini_handle(handle); 43291300Sgjelinek release_lock_file(lockfd); 43301300Sgjelinek 43311300Sgjelinek /* 43321867Sgjelinek * Clean up the file system based on how things went. We either 43331300Sgjelinek * clean up the new zonepath if the operation failed for some reason 43341300Sgjelinek * or we clean up the old zonepath if everything is ok. 43351300Sgjelinek */ 43361300Sgjelinek if (revert) { 43371300Sgjelinek /* The zonecfg update failed, cleanup the new zonepath. */ 43381867Sgjelinek if (is_zfs) { 43391867Sgjelinek if (move_zfs(new_zonepath, zonepath) == Z_ERR) { 43401867Sgjelinek (void) fprintf(stderr, gettext("could not " 43411867Sgjelinek "restore zonepath, the zfs mountpoint is " 43421867Sgjelinek "set as:\n%s\n"), new_zonepath); 43431867Sgjelinek /* 43441867Sgjelinek * err is already != Z_OK since we're reverting 43451867Sgjelinek */ 43461867Sgjelinek } 43471867Sgjelinek 43481867Sgjelinek } else if (fast) { 43491300Sgjelinek if (rename(new_zonepath, zonepath) != 0) { 43501300Sgjelinek zperror(gettext("could not restore zonepath"), 43511300Sgjelinek B_FALSE); 43521300Sgjelinek /* 43531300Sgjelinek * err is already != Z_OK since we're reverting 43541300Sgjelinek */ 43551300Sgjelinek } 43561300Sgjelinek } else { 43571300Sgjelinek (void) printf(gettext("Cleaning up zonepath %s..."), 43581300Sgjelinek new_zonepath); 43591300Sgjelinek (void) fflush(stdout); 43601867Sgjelinek err = cleanup_zonepath(new_zonepath, B_TRUE); 43611300Sgjelinek (void) printf("\n"); 43621300Sgjelinek 43631607Sgjelinek if (err != Z_OK) { 43641300Sgjelinek errno = err; 43651300Sgjelinek zperror(gettext("could not remove new " 43661300Sgjelinek "zonepath"), B_TRUE); 43671300Sgjelinek } else { 43681300Sgjelinek /* 43691300Sgjelinek * Because we're reverting we know the mainline 43701300Sgjelinek * code failed but we just reused the err 43711300Sgjelinek * variable so we reset it back to Z_ERR. 43721300Sgjelinek */ 43731300Sgjelinek err = Z_ERR; 43741300Sgjelinek } 43751300Sgjelinek } 43761300Sgjelinek 43771300Sgjelinek } else { 43781300Sgjelinek /* The move was successful, cleanup the old zonepath. */ 43791867Sgjelinek if (!is_zfs && !fast) { 43801300Sgjelinek (void) printf( 43811300Sgjelinek gettext("Cleaning up zonepath %s..."), zonepath); 43821300Sgjelinek (void) fflush(stdout); 43831867Sgjelinek err = cleanup_zonepath(zonepath, B_TRUE); 43841300Sgjelinek (void) printf("\n"); 43851300Sgjelinek 43861607Sgjelinek if (err != Z_OK) { 43871300Sgjelinek errno = err; 43881300Sgjelinek zperror(gettext("could not remove zonepath"), 43891300Sgjelinek B_TRUE); 43901300Sgjelinek } 43911300Sgjelinek } 43921300Sgjelinek } 43931300Sgjelinek 43941300Sgjelinek return ((err == Z_OK) ? Z_OK : Z_ERR); 43951300Sgjelinek } 43961300Sgjelinek 43971507Sgjelinek static int 43981507Sgjelinek detach_func(int argc, char *argv[]) 43991507Sgjelinek { 44001507Sgjelinek int lockfd; 44011507Sgjelinek int err, arg; 44021507Sgjelinek char zonepath[MAXPATHLEN]; 44031507Sgjelinek zone_dochandle_t handle; 44042078Sgjelinek boolean_t execute = B_TRUE; 44051507Sgjelinek 44061507Sgjelinek if (zonecfg_in_alt_root()) { 44071507Sgjelinek zerror(gettext("cannot detach zone in alternate root")); 44081507Sgjelinek return (Z_ERR); 44091507Sgjelinek } 44101507Sgjelinek 44111507Sgjelinek optind = 0; 44122078Sgjelinek if ((arg = getopt(argc, argv, "?n")) != EOF) { 44131507Sgjelinek switch (arg) { 44141507Sgjelinek case '?': 44151507Sgjelinek sub_usage(SHELP_DETACH, CMD_DETACH); 44161507Sgjelinek return (optopt == '?' ? Z_OK : Z_USAGE); 44172078Sgjelinek case 'n': 44182078Sgjelinek execute = B_FALSE; 44192078Sgjelinek break; 44201507Sgjelinek default: 44211507Sgjelinek sub_usage(SHELP_DETACH, CMD_DETACH); 44221507Sgjelinek return (Z_USAGE); 44231507Sgjelinek } 44241507Sgjelinek } 44252712Snn35248 44262078Sgjelinek if (execute) { 44272712Snn35248 if (sanity_check(target_zone, CMD_DETACH, B_FALSE, B_TRUE, 44282712Snn35248 B_FALSE) != Z_OK) 44292078Sgjelinek return (Z_ERR); 44303339Szt129084 if (verify_details(CMD_DETACH, argv) != Z_OK) 44312078Sgjelinek return (Z_ERR); 44322078Sgjelinek } else { 44332078Sgjelinek /* 44342078Sgjelinek * We want a dry-run to work for a non-privileged user so we 44352078Sgjelinek * only do minimal validation. 44362078Sgjelinek */ 44372078Sgjelinek if (getzoneid() != GLOBAL_ZONEID) { 44382078Sgjelinek zerror(gettext("must be in the global zone to %s a " 44392078Sgjelinek "zone."), cmd_to_str(CMD_DETACH)); 44402078Sgjelinek return (Z_ERR); 44412078Sgjelinek } 44422078Sgjelinek 44432078Sgjelinek if (target_zone == NULL) { 44442078Sgjelinek zerror(gettext("no zone specified")); 44452078Sgjelinek return (Z_ERR); 44462078Sgjelinek } 44472078Sgjelinek 44482078Sgjelinek if (strcmp(target_zone, GLOBAL_ZONENAME) == 0) { 44492078Sgjelinek zerror(gettext("%s operation is invalid for the " 44502078Sgjelinek "global zone."), cmd_to_str(CMD_DETACH)); 44512078Sgjelinek return (Z_ERR); 44522078Sgjelinek } 44532078Sgjelinek } 44541507Sgjelinek 44551507Sgjelinek if ((err = zone_get_zonepath(target_zone, zonepath, sizeof (zonepath))) 44561507Sgjelinek != Z_OK) { 44571507Sgjelinek errno = err; 44581507Sgjelinek zperror2(target_zone, gettext("could not get zone path")); 44591507Sgjelinek return (Z_ERR); 44601507Sgjelinek } 44611507Sgjelinek 44621507Sgjelinek /* Don't detach the zone if anything is still mounted there */ 44632078Sgjelinek if (execute && zonecfg_find_mounts(zonepath, NULL, NULL)) { 44641867Sgjelinek zerror(gettext("These file systems are mounted on " 44651507Sgjelinek "subdirectories of %s.\n"), zonepath); 44661507Sgjelinek (void) zonecfg_find_mounts(zonepath, zfm_print, NULL); 44671507Sgjelinek return (Z_ERR); 44681507Sgjelinek } 44691507Sgjelinek 44701507Sgjelinek if ((handle = zonecfg_init_handle()) == NULL) { 44711507Sgjelinek zperror(cmd_to_str(CMD_DETACH), B_TRUE); 44721507Sgjelinek return (Z_ERR); 44731507Sgjelinek } 44741507Sgjelinek 44751507Sgjelinek if ((err = zonecfg_get_handle(target_zone, handle)) != Z_OK) { 44761507Sgjelinek errno = err; 44771507Sgjelinek zperror(cmd_to_str(CMD_DETACH), B_TRUE); 44781507Sgjelinek zonecfg_fini_handle(handle); 44791507Sgjelinek return (Z_ERR); 44801507Sgjelinek } 44811507Sgjelinek 44822078Sgjelinek if (execute && grab_lock_file(target_zone, &lockfd) != Z_OK) { 44831507Sgjelinek zerror(gettext("another %s may have an operation in progress."), 44841507Sgjelinek "zoneadm"); 44851507Sgjelinek zonecfg_fini_handle(handle); 44861507Sgjelinek return (Z_ERR); 44871507Sgjelinek } 44881507Sgjelinek 44891507Sgjelinek if ((err = zonecfg_get_detach_info(handle, B_TRUE)) != Z_OK) { 44901507Sgjelinek errno = err; 44911507Sgjelinek zperror(gettext("getting the detach information failed"), 44921507Sgjelinek B_TRUE); 44931507Sgjelinek goto done; 44941507Sgjelinek } 44951507Sgjelinek 44962078Sgjelinek if ((err = zonecfg_detach_save(handle, (execute ? 0 : ZONE_DRY_RUN))) 44972078Sgjelinek != Z_OK) { 44981507Sgjelinek errno = err; 44991507Sgjelinek zperror(gettext("saving the detach manifest failed"), B_TRUE); 45001507Sgjelinek goto done; 45011507Sgjelinek } 45021507Sgjelinek 45032078Sgjelinek /* 45042078Sgjelinek * Set the zone state back to configured unless we are running with the 45052078Sgjelinek * no-execute option. 45062078Sgjelinek */ 45072078Sgjelinek if (execute && (err = zone_set_state(target_zone, 45082078Sgjelinek ZONE_STATE_CONFIGURED)) != Z_OK) { 45091507Sgjelinek errno = err; 45101507Sgjelinek zperror(gettext("could not reset state"), B_TRUE); 45111507Sgjelinek } 45121507Sgjelinek 45131507Sgjelinek done: 45141507Sgjelinek zonecfg_fini_handle(handle); 45152078Sgjelinek if (execute) 45162078Sgjelinek release_lock_file(lockfd); 45171507Sgjelinek 45181507Sgjelinek return ((err == Z_OK) ? Z_OK : Z_ERR); 45191507Sgjelinek } 45201507Sgjelinek 45211507Sgjelinek /* 45221507Sgjelinek * During attach we go through and fix up the /dev entries for the zone 45231507Sgjelinek * we are attaching. In order to regenerate /dev with the correct devices, 45241507Sgjelinek * the old /dev will be removed, the zone readied (which generates a new 45251507Sgjelinek * /dev) then halted, then we use the info from the manifest to update 45261507Sgjelinek * the modes, owners, etc. on the new /dev. 45271507Sgjelinek */ 45281507Sgjelinek static int 45291507Sgjelinek dev_fix(zone_dochandle_t handle) 45301507Sgjelinek { 45311507Sgjelinek int res; 45321507Sgjelinek int err; 45331507Sgjelinek int status; 45341507Sgjelinek struct zone_devpermtab devtab; 45351507Sgjelinek zone_cmd_arg_t zarg; 45361507Sgjelinek char devpath[MAXPATHLEN]; 45371507Sgjelinek /* 6: "exec " and " " */ 45381507Sgjelinek char cmdbuf[sizeof (RMCOMMAND) + MAXPATHLEN + 6]; 45391507Sgjelinek 45401507Sgjelinek if ((res = zonecfg_get_zonepath(handle, devpath, sizeof (devpath))) 45411507Sgjelinek != Z_OK) 45421507Sgjelinek return (res); 45431507Sgjelinek 45441507Sgjelinek if (strlcat(devpath, "/dev", sizeof (devpath)) >= sizeof (devpath)) 45451507Sgjelinek return (Z_TOO_BIG); 45461507Sgjelinek 45471507Sgjelinek /* 45481507Sgjelinek * "exec" the command so that the returned status is that of 45491507Sgjelinek * RMCOMMAND and not the shell. 45501507Sgjelinek */ 45512712Snn35248 (void) snprintf(cmdbuf, sizeof (cmdbuf), EXEC_PREFIX RMCOMMAND " %s", 45521507Sgjelinek devpath); 45531507Sgjelinek status = do_subproc(cmdbuf); 45542712Snn35248 if ((err = subproc_status(RMCOMMAND, status, B_TRUE)) != 45552712Snn35248 ZONE_SUBPROC_OK) { 45561507Sgjelinek (void) fprintf(stderr, 45571507Sgjelinek gettext("could not remove existing /dev\n")); 45581507Sgjelinek return (Z_ERR); 45591507Sgjelinek } 45601507Sgjelinek 45611507Sgjelinek /* In order to ready the zone, it must be in the installed state */ 45621507Sgjelinek if ((err = zone_set_state(target_zone, ZONE_STATE_INSTALLED)) != Z_OK) { 45631507Sgjelinek errno = err; 45641507Sgjelinek zperror(gettext("could not reset state"), B_TRUE); 45651507Sgjelinek return (Z_ERR); 45661507Sgjelinek } 45671507Sgjelinek 45681507Sgjelinek /* We have to ready the zone to regen the dev tree */ 45691507Sgjelinek zarg.cmd = Z_READY; 45701507Sgjelinek if (call_zoneadmd(target_zone, &zarg) != 0) { 45711507Sgjelinek zerror(gettext("call to %s failed"), "zoneadmd"); 45723247Sgjelinek /* attempt to restore zone to configured state */ 45733247Sgjelinek (void) zone_set_state(target_zone, ZONE_STATE_CONFIGURED); 45741507Sgjelinek return (Z_ERR); 45751507Sgjelinek } 45761507Sgjelinek 45771507Sgjelinek zarg.cmd = Z_HALT; 45781507Sgjelinek if (call_zoneadmd(target_zone, &zarg) != 0) { 45791507Sgjelinek zerror(gettext("call to %s failed"), "zoneadmd"); 45803247Sgjelinek /* attempt to restore zone to configured state */ 45813247Sgjelinek (void) zone_set_state(target_zone, ZONE_STATE_CONFIGURED); 45821507Sgjelinek return (Z_ERR); 45831507Sgjelinek } 45841507Sgjelinek 45853247Sgjelinek /* attempt to restore zone to configured state */ 45863247Sgjelinek (void) zone_set_state(target_zone, ZONE_STATE_CONFIGURED); 45873247Sgjelinek 45881507Sgjelinek if (zonecfg_setdevperment(handle) != Z_OK) { 45891507Sgjelinek (void) fprintf(stderr, 45901507Sgjelinek gettext("unable to enumerate device entries\n")); 45911507Sgjelinek return (Z_ERR); 45921507Sgjelinek } 45931507Sgjelinek 45941507Sgjelinek while (zonecfg_getdevperment(handle, &devtab) == Z_OK) { 45951507Sgjelinek int err; 45961507Sgjelinek 45971507Sgjelinek if ((err = zonecfg_devperms_apply(handle, 45981507Sgjelinek devtab.zone_devperm_name, devtab.zone_devperm_uid, 45991507Sgjelinek devtab.zone_devperm_gid, devtab.zone_devperm_mode, 46001507Sgjelinek devtab.zone_devperm_acl)) != Z_OK && err != Z_INVAL) 46011507Sgjelinek (void) fprintf(stderr, gettext("error updating device " 46021507Sgjelinek "%s: %s\n"), devtab.zone_devperm_name, 46031507Sgjelinek zonecfg_strerror(err)); 46041507Sgjelinek 46051507Sgjelinek free(devtab.zone_devperm_acl); 46061507Sgjelinek } 46071507Sgjelinek 46081507Sgjelinek (void) zonecfg_enddevperment(handle); 46091507Sgjelinek 46101507Sgjelinek return (Z_OK); 46111507Sgjelinek } 46121507Sgjelinek 46132078Sgjelinek /* 46142078Sgjelinek * Validate attaching a zone but don't actually do the work. The zone 46152078Sgjelinek * does not have to exist, so there is some complexity getting a new zone 46162078Sgjelinek * configuration set up so that we can perform the validation. This is 46172078Sgjelinek * handled within zonecfg_attach_manifest() which returns two handles; one 46182078Sgjelinek * for the the full configuration to validate (rem_handle) and the other 46192078Sgjelinek * (local_handle) containing only the zone configuration derived from the 46202078Sgjelinek * manifest. 46212078Sgjelinek */ 46222078Sgjelinek static int 46233339Szt129084 dryrun_attach(char *manifest_path, char *argv[]) 46242078Sgjelinek { 46252078Sgjelinek int fd; 46262078Sgjelinek int err; 46272078Sgjelinek int res; 46282078Sgjelinek zone_dochandle_t local_handle; 46292078Sgjelinek zone_dochandle_t rem_handle = NULL; 46302078Sgjelinek 46312078Sgjelinek if (strcmp(manifest_path, "-") == 0) { 46322078Sgjelinek fd = 0; 46332078Sgjelinek } else if ((fd = open(manifest_path, O_RDONLY)) < 0) { 46342078Sgjelinek zperror(gettext("could not open manifest path"), B_FALSE); 46352078Sgjelinek return (Z_ERR); 46362078Sgjelinek } 46372078Sgjelinek 46382078Sgjelinek if ((local_handle = zonecfg_init_handle()) == NULL) { 46392078Sgjelinek zperror(cmd_to_str(CMD_ATTACH), B_TRUE); 46402078Sgjelinek res = Z_ERR; 46412078Sgjelinek goto done; 46422078Sgjelinek } 46432078Sgjelinek 46442078Sgjelinek if ((rem_handle = zonecfg_init_handle()) == NULL) { 46452078Sgjelinek zperror(cmd_to_str(CMD_ATTACH), B_TRUE); 46462078Sgjelinek res = Z_ERR; 46472078Sgjelinek goto done; 46482078Sgjelinek } 46492078Sgjelinek 46502078Sgjelinek if ((err = zonecfg_attach_manifest(fd, local_handle, rem_handle)) 46512078Sgjelinek != Z_OK) { 46522078Sgjelinek if (err == Z_INVALID_DOCUMENT) 46532078Sgjelinek zerror(gettext("Cannot attach to an earlier release " 46542078Sgjelinek "of the operating system")); 46552078Sgjelinek else 46562078Sgjelinek zperror(cmd_to_str(CMD_ATTACH), B_TRUE); 46572078Sgjelinek res = Z_ERR; 46582078Sgjelinek goto done; 46592078Sgjelinek } 46602078Sgjelinek 46613172Sgjelinek /* 46623172Sgjelinek * Retrieve remote handle brand type and determine whether it is 46633172Sgjelinek * native or not. 46643172Sgjelinek */ 46653172Sgjelinek if (zonecfg_get_brand(rem_handle, target_brand, sizeof (target_brand)) 46663172Sgjelinek != Z_OK) { 46673172Sgjelinek zerror(gettext("missing or invalid brand")); 46683172Sgjelinek exit(Z_ERR); 46693172Sgjelinek } 46703172Sgjelinek is_native_zone = (strcmp(target_brand, NATIVE_BRAND_NAME) == 0); 46713172Sgjelinek 46723339Szt129084 res = verify_handle(CMD_ATTACH, local_handle, argv); 46732078Sgjelinek 46742078Sgjelinek /* Get the detach information for the locally defined zone. */ 46752078Sgjelinek if ((err = zonecfg_get_detach_info(local_handle, B_FALSE)) != Z_OK) { 46762078Sgjelinek errno = err; 46772078Sgjelinek zperror(gettext("getting the attach information failed"), 46782078Sgjelinek B_TRUE); 46792078Sgjelinek res = Z_ERR; 46802078Sgjelinek } else { 46812078Sgjelinek /* sw_cmp prints error msgs as necessary */ 46822078Sgjelinek if (sw_cmp(local_handle, rem_handle, SW_CMP_NONE) != Z_OK) 46832078Sgjelinek res = Z_ERR; 46842078Sgjelinek } 46852078Sgjelinek 46862078Sgjelinek done: 46872078Sgjelinek if (strcmp(manifest_path, "-") != 0) 46882078Sgjelinek (void) close(fd); 46892078Sgjelinek 46902078Sgjelinek zonecfg_fini_handle(local_handle); 46912078Sgjelinek zonecfg_fini_handle(rem_handle); 46922078Sgjelinek 46932078Sgjelinek return ((res == Z_OK) ? Z_OK : Z_ERR); 46942078Sgjelinek } 46952078Sgjelinek 46961507Sgjelinek static int 46971507Sgjelinek attach_func(int argc, char *argv[]) 46981507Sgjelinek { 46991507Sgjelinek int lockfd; 47001507Sgjelinek int err, arg; 47011507Sgjelinek boolean_t force = B_FALSE; 47021507Sgjelinek zone_dochandle_t handle; 47031507Sgjelinek zone_dochandle_t athandle = NULL; 47041507Sgjelinek char zonepath[MAXPATHLEN]; 47052712Snn35248 char brand[MAXNAMELEN], atbrand[MAXNAMELEN]; 47062078Sgjelinek boolean_t execute = B_TRUE; 47072078Sgjelinek char *manifest_path; 47081507Sgjelinek 47091507Sgjelinek if (zonecfg_in_alt_root()) { 47101507Sgjelinek zerror(gettext("cannot attach zone in alternate root")); 47111507Sgjelinek return (Z_ERR); 47121507Sgjelinek } 47131507Sgjelinek 47141507Sgjelinek optind = 0; 47152078Sgjelinek if ((arg = getopt(argc, argv, "?Fn:")) != EOF) { 47161507Sgjelinek switch (arg) { 47171507Sgjelinek case '?': 47181507Sgjelinek sub_usage(SHELP_ATTACH, CMD_ATTACH); 47191507Sgjelinek return (optopt == '?' ? Z_OK : Z_USAGE); 47201507Sgjelinek case 'F': 47211507Sgjelinek force = B_TRUE; 47221507Sgjelinek break; 47232078Sgjelinek case 'n': 47242078Sgjelinek execute = B_FALSE; 47252078Sgjelinek manifest_path = optarg; 47262078Sgjelinek break; 47271507Sgjelinek default: 47281507Sgjelinek sub_usage(SHELP_ATTACH, CMD_ATTACH); 47291507Sgjelinek return (Z_USAGE); 47301507Sgjelinek } 47311507Sgjelinek } 47322078Sgjelinek 47332078Sgjelinek /* 47342078Sgjelinek * If the no-execute option was specified, we need to branch down 47352078Sgjelinek * a completely different path since there is no zone required to be 47362078Sgjelinek * configured for this option. 47372078Sgjelinek */ 47382078Sgjelinek if (!execute) 47393339Szt129084 return (dryrun_attach(manifest_path, argv)); 47402078Sgjelinek 47412712Snn35248 if (sanity_check(target_zone, CMD_ATTACH, B_FALSE, B_TRUE, B_FALSE) 47422712Snn35248 != Z_OK) 47431507Sgjelinek return (Z_ERR); 47443339Szt129084 if (verify_details(CMD_ATTACH, argv) != Z_OK) 47451507Sgjelinek return (Z_ERR); 47461507Sgjelinek 47471507Sgjelinek if ((err = zone_get_zonepath(target_zone, zonepath, sizeof (zonepath))) 47481507Sgjelinek != Z_OK) { 47491507Sgjelinek errno = err; 47501507Sgjelinek zperror2(target_zone, gettext("could not get zone path")); 47511507Sgjelinek return (Z_ERR); 47521507Sgjelinek } 47531507Sgjelinek 47541507Sgjelinek if ((handle = zonecfg_init_handle()) == NULL) { 47551507Sgjelinek zperror(cmd_to_str(CMD_ATTACH), B_TRUE); 47561507Sgjelinek return (Z_ERR); 47571507Sgjelinek } 47581507Sgjelinek 47591507Sgjelinek if ((err = zonecfg_get_handle(target_zone, handle)) != Z_OK) { 47601507Sgjelinek errno = err; 47611507Sgjelinek zperror(cmd_to_str(CMD_ATTACH), B_TRUE); 47621507Sgjelinek zonecfg_fini_handle(handle); 47631507Sgjelinek return (Z_ERR); 47641507Sgjelinek } 47651507Sgjelinek 47661507Sgjelinek if (grab_lock_file(target_zone, &lockfd) != Z_OK) { 47671507Sgjelinek zerror(gettext("another %s may have an operation in progress."), 47681507Sgjelinek "zoneadm"); 47691507Sgjelinek zonecfg_fini_handle(handle); 47701507Sgjelinek return (Z_ERR); 47711507Sgjelinek } 47721507Sgjelinek 47731507Sgjelinek if (force) 47741507Sgjelinek goto forced; 47751507Sgjelinek 47761507Sgjelinek if ((athandle = zonecfg_init_handle()) == NULL) { 47771507Sgjelinek zperror(cmd_to_str(CMD_ATTACH), B_TRUE); 47781507Sgjelinek goto done; 47791507Sgjelinek } 47801507Sgjelinek 47811507Sgjelinek if ((err = zonecfg_get_attach_handle(zonepath, target_zone, B_TRUE, 47821507Sgjelinek athandle)) != Z_OK) { 47831507Sgjelinek if (err == Z_NO_ZONE) 47841507Sgjelinek zerror(gettext("Not a detached zone")); 47851507Sgjelinek else if (err == Z_INVALID_DOCUMENT) 47861507Sgjelinek zerror(gettext("Cannot attach to an earlier release " 47871507Sgjelinek "of the operating system")); 47881507Sgjelinek else 47891507Sgjelinek zperror(cmd_to_str(CMD_ATTACH), B_TRUE); 47901507Sgjelinek goto done; 47911507Sgjelinek } 47921507Sgjelinek 47931507Sgjelinek /* Get the detach information for the locally defined zone. */ 47941507Sgjelinek if ((err = zonecfg_get_detach_info(handle, B_FALSE)) != Z_OK) { 47951507Sgjelinek errno = err; 47961507Sgjelinek zperror(gettext("getting the attach information failed"), 47971507Sgjelinek B_TRUE); 47981507Sgjelinek goto done; 47991507Sgjelinek } 48001507Sgjelinek 48012712Snn35248 /* 48022712Snn35248 * Ensure that the detached and locally defined zones are both of 48032712Snn35248 * the same brand. 48042712Snn35248 */ 48052712Snn35248 if ((zonecfg_get_brand(handle, brand, sizeof (brand)) != 0) || 48062712Snn35248 (zonecfg_get_brand(athandle, atbrand, sizeof (atbrand)) != 0)) { 48072712Snn35248 err = Z_ERR; 48082712Snn35248 zerror(gettext("missing or invalid brand")); 48092712Snn35248 goto done; 48102712Snn35248 } 48112712Snn35248 48122712Snn35248 if (strcmp(atbrand, brand) != NULL) { 48132712Snn35248 err = Z_ERR; 48142712Snn35248 zerror(gettext("Trying to attach a '%s' zone to a '%s' " 48152712Snn35248 "configuration."), atbrand, brand); 48162712Snn35248 goto done; 48172712Snn35248 } 48182712Snn35248 48191507Sgjelinek /* sw_cmp prints error msgs as necessary */ 48201867Sgjelinek if ((err = sw_cmp(handle, athandle, SW_CMP_NONE)) != Z_OK) 48211507Sgjelinek goto done; 48221507Sgjelinek 48231507Sgjelinek if ((err = dev_fix(athandle)) != Z_OK) 48241507Sgjelinek goto done; 48251507Sgjelinek 48261507Sgjelinek forced: 48271507Sgjelinek 48281507Sgjelinek zonecfg_rm_detached(handle, force); 48291507Sgjelinek 48301507Sgjelinek if ((err = zone_set_state(target_zone, ZONE_STATE_INSTALLED)) != Z_OK) { 48311507Sgjelinek errno = err; 48321507Sgjelinek zperror(gettext("could not reset state"), B_TRUE); 48331507Sgjelinek } 48341507Sgjelinek 48351507Sgjelinek done: 48361507Sgjelinek zonecfg_fini_handle(handle); 48371507Sgjelinek release_lock_file(lockfd); 48381507Sgjelinek if (athandle != NULL) 48391507Sgjelinek zonecfg_fini_handle(athandle); 48401507Sgjelinek 48411507Sgjelinek return ((err == Z_OK) ? Z_OK : Z_ERR); 48421507Sgjelinek } 48431507Sgjelinek 48441300Sgjelinek /* 48450Sstevel@tonic-gate * On input, TRUE => yes, FALSE => no. 48460Sstevel@tonic-gate * On return, TRUE => 1, FALSE => 0, could not ask => -1. 48470Sstevel@tonic-gate */ 48480Sstevel@tonic-gate 48490Sstevel@tonic-gate static int 48500Sstevel@tonic-gate ask_yesno(boolean_t default_answer, const char *question) 48510Sstevel@tonic-gate { 48520Sstevel@tonic-gate char line[64]; /* should be large enough to answer yes or no */ 48530Sstevel@tonic-gate 48540Sstevel@tonic-gate if (!isatty(STDIN_FILENO)) 48550Sstevel@tonic-gate return (-1); 48560Sstevel@tonic-gate for (;;) { 48570Sstevel@tonic-gate (void) printf("%s (%s)? ", question, 48580Sstevel@tonic-gate default_answer ? "[y]/n" : "y/[n]"); 48590Sstevel@tonic-gate if (fgets(line, sizeof (line), stdin) == NULL || 48600Sstevel@tonic-gate line[0] == '\n') 48610Sstevel@tonic-gate return (default_answer ? 1 : 0); 48620Sstevel@tonic-gate if (tolower(line[0]) == 'y') 48630Sstevel@tonic-gate return (1); 48640Sstevel@tonic-gate if (tolower(line[0]) == 'n') 48650Sstevel@tonic-gate return (0); 48660Sstevel@tonic-gate } 48670Sstevel@tonic-gate } 48680Sstevel@tonic-gate 48690Sstevel@tonic-gate static int 48700Sstevel@tonic-gate uninstall_func(int argc, char *argv[]) 48710Sstevel@tonic-gate { 48720Sstevel@tonic-gate char line[ZONENAME_MAX + 128]; /* Enough for "Are you sure ..." */ 48731867Sgjelinek char rootpath[MAXPATHLEN], zonepath[MAXPATHLEN]; 48740Sstevel@tonic-gate boolean_t force = B_FALSE; 48750Sstevel@tonic-gate int lockfd, answer; 48760Sstevel@tonic-gate int err, arg; 48770Sstevel@tonic-gate 4878766Scarlsonj if (zonecfg_in_alt_root()) { 4879766Scarlsonj zerror(gettext("cannot uninstall zone in alternate root")); 4880766Scarlsonj return (Z_ERR); 4881766Scarlsonj } 4882766Scarlsonj 48830Sstevel@tonic-gate optind = 0; 48840Sstevel@tonic-gate while ((arg = getopt(argc, argv, "?F")) != EOF) { 48850Sstevel@tonic-gate switch (arg) { 48860Sstevel@tonic-gate case '?': 48870Sstevel@tonic-gate sub_usage(SHELP_UNINSTALL, CMD_UNINSTALL); 48880Sstevel@tonic-gate return (optopt == '?' ? Z_OK : Z_USAGE); 48890Sstevel@tonic-gate case 'F': 48900Sstevel@tonic-gate force = B_TRUE; 48910Sstevel@tonic-gate break; 48920Sstevel@tonic-gate default: 48930Sstevel@tonic-gate sub_usage(SHELP_UNINSTALL, CMD_UNINSTALL); 48940Sstevel@tonic-gate return (Z_USAGE); 48950Sstevel@tonic-gate } 48960Sstevel@tonic-gate } 48970Sstevel@tonic-gate if (argc > optind) { 48980Sstevel@tonic-gate sub_usage(SHELP_UNINSTALL, CMD_UNINSTALL); 48990Sstevel@tonic-gate return (Z_USAGE); 49000Sstevel@tonic-gate } 49010Sstevel@tonic-gate 49022712Snn35248 if (sanity_check(target_zone, CMD_UNINSTALL, B_FALSE, B_TRUE, B_FALSE) 49032712Snn35248 != Z_OK) 49040Sstevel@tonic-gate return (Z_ERR); 49050Sstevel@tonic-gate 49063339Szt129084 /* 49073339Szt129084 * Invoke brand-specific handler. 49083339Szt129084 */ 49093339Szt129084 if (invoke_brand_handler(CMD_UNINSTALL, argv) != Z_OK) 49103339Szt129084 return (Z_ERR); 49113339Szt129084 49120Sstevel@tonic-gate if (!force) { 49130Sstevel@tonic-gate (void) snprintf(line, sizeof (line), 49140Sstevel@tonic-gate gettext("Are you sure you want to %s zone %s"), 49150Sstevel@tonic-gate cmd_to_str(CMD_UNINSTALL), target_zone); 49160Sstevel@tonic-gate if ((answer = ask_yesno(B_FALSE, line)) == 0) { 49170Sstevel@tonic-gate return (Z_OK); 49180Sstevel@tonic-gate } else if (answer == -1) { 49190Sstevel@tonic-gate zerror(gettext("Input not from terminal and -F " 49200Sstevel@tonic-gate "not specified: %s not done."), 49210Sstevel@tonic-gate cmd_to_str(CMD_UNINSTALL)); 49220Sstevel@tonic-gate return (Z_ERR); 49230Sstevel@tonic-gate } 49240Sstevel@tonic-gate } 49250Sstevel@tonic-gate 49261867Sgjelinek if ((err = zone_get_zonepath(target_zone, zonepath, 49271867Sgjelinek sizeof (zonepath))) != Z_OK) { 49280Sstevel@tonic-gate errno = err; 49290Sstevel@tonic-gate zperror2(target_zone, gettext("could not get zone path")); 49300Sstevel@tonic-gate return (Z_ERR); 49310Sstevel@tonic-gate } 49320Sstevel@tonic-gate if ((err = zone_get_rootpath(target_zone, rootpath, 49330Sstevel@tonic-gate sizeof (rootpath))) != Z_OK) { 49340Sstevel@tonic-gate errno = err; 49350Sstevel@tonic-gate zperror2(target_zone, gettext("could not get root path")); 49360Sstevel@tonic-gate return (Z_ERR); 49370Sstevel@tonic-gate } 49380Sstevel@tonic-gate 49390Sstevel@tonic-gate /* 49400Sstevel@tonic-gate * If there seems to be a zoneadmd running for this zone, call it 49410Sstevel@tonic-gate * to tell it that an uninstall is happening; if all goes well it 49420Sstevel@tonic-gate * will then shut itself down. 49430Sstevel@tonic-gate */ 49440Sstevel@tonic-gate if (ping_zoneadmd(target_zone) == Z_OK) { 49450Sstevel@tonic-gate zone_cmd_arg_t zarg; 49460Sstevel@tonic-gate zarg.cmd = Z_NOTE_UNINSTALLING; 49470Sstevel@tonic-gate /* we don't care too much if this fails... just plow on */ 49480Sstevel@tonic-gate (void) call_zoneadmd(target_zone, &zarg); 49490Sstevel@tonic-gate } 49500Sstevel@tonic-gate 49510Sstevel@tonic-gate if (grab_lock_file(target_zone, &lockfd) != Z_OK) { 49520Sstevel@tonic-gate zerror(gettext("another %s may have an operation in progress."), 49531300Sgjelinek "zoneadm"); 49540Sstevel@tonic-gate return (Z_ERR); 49550Sstevel@tonic-gate } 49560Sstevel@tonic-gate 49570Sstevel@tonic-gate /* Don't uninstall the zone if anything is mounted there */ 49580Sstevel@tonic-gate err = zonecfg_find_mounts(rootpath, NULL, NULL); 49590Sstevel@tonic-gate if (err) { 49601867Sgjelinek zerror(gettext("These file systems are mounted on " 49611645Scomay "subdirectories of %s.\n"), rootpath); 49620Sstevel@tonic-gate (void) zonecfg_find_mounts(rootpath, zfm_print, NULL); 49630Sstevel@tonic-gate return (Z_ERR); 49640Sstevel@tonic-gate } 49650Sstevel@tonic-gate 49660Sstevel@tonic-gate err = zone_set_state(target_zone, ZONE_STATE_INCOMPLETE); 49670Sstevel@tonic-gate if (err != Z_OK) { 49680Sstevel@tonic-gate errno = err; 49690Sstevel@tonic-gate zperror2(target_zone, gettext("could not set state")); 49700Sstevel@tonic-gate goto bad; 49710Sstevel@tonic-gate } 49720Sstevel@tonic-gate 49731867Sgjelinek if ((err = cleanup_zonepath(zonepath, B_FALSE)) != Z_OK) { 49741867Sgjelinek errno = err; 49751867Sgjelinek zperror2(target_zone, gettext("cleaning up zonepath failed")); 49760Sstevel@tonic-gate goto bad; 49771867Sgjelinek } 49781867Sgjelinek 49790Sstevel@tonic-gate err = zone_set_state(target_zone, ZONE_STATE_CONFIGURED); 49800Sstevel@tonic-gate if (err != Z_OK) { 49810Sstevel@tonic-gate errno = err; 49820Sstevel@tonic-gate zperror2(target_zone, gettext("could not reset state")); 49830Sstevel@tonic-gate } 49840Sstevel@tonic-gate bad: 49850Sstevel@tonic-gate release_lock_file(lockfd); 49860Sstevel@tonic-gate return (err); 49870Sstevel@tonic-gate } 49880Sstevel@tonic-gate 4989766Scarlsonj /* ARGSUSED */ 4990766Scarlsonj static int 4991766Scarlsonj mount_func(int argc, char *argv[]) 4992766Scarlsonj { 4993766Scarlsonj zone_cmd_arg_t zarg; 49942712Snn35248 boolean_t force = B_FALSE; 49952712Snn35248 int arg; 49962712Snn35248 49972712Snn35248 /* 49982712Snn35248 * The only supported subargument to the "mount" subcommand is 49992712Snn35248 * "-f", which forces us to mount a zone in the INCOMPLETE state. 50002712Snn35248 */ 50012712Snn35248 optind = 0; 50022712Snn35248 if ((arg = getopt(argc, argv, "f")) != EOF) { 50032712Snn35248 switch (arg) { 50042712Snn35248 case 'f': 50052712Snn35248 force = B_TRUE; 50062712Snn35248 break; 50072712Snn35248 default: 50082712Snn35248 return (Z_USAGE); 50092712Snn35248 } 50102712Snn35248 } 50112712Snn35248 if (argc > optind) 5012766Scarlsonj return (Z_USAGE); 50132712Snn35248 50142712Snn35248 if (sanity_check(target_zone, CMD_MOUNT, B_FALSE, B_FALSE, force) 50152712Snn35248 != Z_OK) 5016766Scarlsonj return (Z_ERR); 50173339Szt129084 if (verify_details(CMD_MOUNT, argv) != Z_OK) 5018766Scarlsonj return (Z_ERR); 5019766Scarlsonj 50202712Snn35248 zarg.cmd = force ? Z_FORCEMOUNT : Z_MOUNT; 5021766Scarlsonj if (call_zoneadmd(target_zone, &zarg) != 0) { 5022766Scarlsonj zerror(gettext("call to %s failed"), "zoneadmd"); 5023766Scarlsonj return (Z_ERR); 5024766Scarlsonj } 5025766Scarlsonj return (Z_OK); 5026766Scarlsonj } 5027766Scarlsonj 5028766Scarlsonj /* ARGSUSED */ 5029766Scarlsonj static int 5030766Scarlsonj unmount_func(int argc, char *argv[]) 5031766Scarlsonj { 5032766Scarlsonj zone_cmd_arg_t zarg; 5033766Scarlsonj 5034766Scarlsonj if (argc > 0) 5035766Scarlsonj return (Z_USAGE); 50362712Snn35248 if (sanity_check(target_zone, CMD_UNMOUNT, B_FALSE, B_FALSE, B_FALSE) 50372712Snn35248 != Z_OK) 5038766Scarlsonj return (Z_ERR); 5039766Scarlsonj 5040766Scarlsonj zarg.cmd = Z_UNMOUNT; 5041766Scarlsonj if (call_zoneadmd(target_zone, &zarg) != 0) { 5042766Scarlsonj zerror(gettext("call to %s failed"), "zoneadmd"); 5043766Scarlsonj return (Z_ERR); 5044766Scarlsonj } 5045766Scarlsonj return (Z_OK); 5046766Scarlsonj } 5047766Scarlsonj 50480Sstevel@tonic-gate static int 50492303Scarlsonj mark_func(int argc, char *argv[]) 50502303Scarlsonj { 50512303Scarlsonj int err, lockfd; 50522303Scarlsonj 50532303Scarlsonj if (argc != 1 || strcmp(argv[0], "incomplete") != 0) 50542303Scarlsonj return (Z_USAGE); 50552712Snn35248 if (sanity_check(target_zone, CMD_MARK, B_FALSE, B_FALSE, B_FALSE) 50562712Snn35248 != Z_OK) 50572303Scarlsonj return (Z_ERR); 50582303Scarlsonj 50593339Szt129084 /* 50603339Szt129084 * Invoke brand-specific handler. 50613339Szt129084 */ 50623339Szt129084 if (invoke_brand_handler(CMD_MARK, argv) != Z_OK) 50633339Szt129084 return (Z_ERR); 50643339Szt129084 50652303Scarlsonj if (grab_lock_file(target_zone, &lockfd) != Z_OK) { 50662303Scarlsonj zerror(gettext("another %s may have an operation in progress."), 50672303Scarlsonj "zoneadm"); 50682303Scarlsonj return (Z_ERR); 50692303Scarlsonj } 50702303Scarlsonj 50712303Scarlsonj err = zone_set_state(target_zone, ZONE_STATE_INCOMPLETE); 50722303Scarlsonj if (err != Z_OK) { 50732303Scarlsonj errno = err; 50742303Scarlsonj zperror2(target_zone, gettext("could not set state")); 50752303Scarlsonj } 50762303Scarlsonj release_lock_file(lockfd); 50772303Scarlsonj 50782303Scarlsonj return (err); 50792303Scarlsonj } 50802303Scarlsonj 50813247Sgjelinek /* 50823247Sgjelinek * Check what scheduling class we're running under and print a warning if 50833247Sgjelinek * we're not using FSS. 50843247Sgjelinek */ 50853247Sgjelinek static int 50863247Sgjelinek check_sched_fss(zone_dochandle_t handle) 50873247Sgjelinek { 50883247Sgjelinek char class_name[PC_CLNMSZ]; 50893247Sgjelinek 50903247Sgjelinek if (zonecfg_get_dflt_sched_class(handle, class_name, 50913247Sgjelinek sizeof (class_name)) != Z_OK) { 50923247Sgjelinek zerror(gettext("WARNING: unable to determine the zone's " 50933247Sgjelinek "scheduling class")); 50943247Sgjelinek } else if (strcmp("FSS", class_name) != 0) { 50953247Sgjelinek zerror(gettext("WARNING: The zone.cpu-shares rctl is set but\n" 50963247Sgjelinek "FSS is not the default scheduling class for this zone. " 50973247Sgjelinek "FSS will be\nused for processes in the zone but to get " 50983247Sgjelinek "the full benefit of FSS,\nit should be the default " 50993247Sgjelinek "scheduling class. See dispadmin(1M) for\nmore details.")); 51003247Sgjelinek return (Z_SYSTEM); 51013247Sgjelinek } 51023247Sgjelinek 51033247Sgjelinek return (Z_OK); 51043247Sgjelinek } 51053247Sgjelinek 51063247Sgjelinek static int 51073247Sgjelinek check_cpu_shares_sched(zone_dochandle_t handle) 51083247Sgjelinek { 51093247Sgjelinek int err; 51103247Sgjelinek int res = Z_OK; 51113247Sgjelinek struct zone_rctltab rctl; 51123247Sgjelinek 51133247Sgjelinek if ((err = zonecfg_setrctlent(handle)) != Z_OK) { 51143247Sgjelinek errno = err; 51153247Sgjelinek zperror(cmd_to_str(CMD_APPLY), B_TRUE); 51163247Sgjelinek return (err); 51173247Sgjelinek } 51183247Sgjelinek 51193247Sgjelinek while (zonecfg_getrctlent(handle, &rctl) == Z_OK) { 51203247Sgjelinek if (strcmp(rctl.zone_rctl_name, "zone.cpu-shares") == 0) { 51213247Sgjelinek if (check_sched_fss(handle) != Z_OK) 51223247Sgjelinek res = Z_SYSTEM; 51233247Sgjelinek break; 51243247Sgjelinek } 51253247Sgjelinek } 51263247Sgjelinek 51273247Sgjelinek (void) zonecfg_endrctlent(handle); 51283247Sgjelinek 51293247Sgjelinek return (res); 51303247Sgjelinek } 51313247Sgjelinek 51323247Sgjelinek /* 51333352Sgjelinek * Check if there is a mix of processes running in different pools within the 51343352Sgjelinek * zone. This is currently only going to be called for the global zone from 51353352Sgjelinek * apply_func but that could be generalized in the future. 51363352Sgjelinek */ 51373352Sgjelinek static boolean_t 51383352Sgjelinek mixed_pools(zoneid_t zoneid) 51393352Sgjelinek { 51403352Sgjelinek DIR *dirp; 51413352Sgjelinek dirent_t *dent; 51423352Sgjelinek boolean_t mixed = B_FALSE; 51433352Sgjelinek boolean_t poolid_set = B_FALSE; 51443352Sgjelinek poolid_t last_poolid = 0; 51453352Sgjelinek 51463352Sgjelinek if ((dirp = opendir("/proc")) == NULL) { 51473352Sgjelinek zerror(gettext("could not open /proc")); 51483352Sgjelinek return (B_FALSE); 51493352Sgjelinek } 51503352Sgjelinek 51513352Sgjelinek while ((dent = readdir(dirp)) != NULL) { 51523352Sgjelinek int procfd; 51533352Sgjelinek psinfo_t ps; 51543352Sgjelinek char procpath[MAXPATHLEN]; 51553352Sgjelinek 51563352Sgjelinek if (dent->d_name[0] == '.') 51573352Sgjelinek continue; 51583352Sgjelinek 51593352Sgjelinek (void) snprintf(procpath, sizeof (procpath), "/proc/%s/psinfo", 51603352Sgjelinek dent->d_name); 51613352Sgjelinek 51623352Sgjelinek if ((procfd = open(procpath, O_RDONLY)) == -1) 51633352Sgjelinek continue; 51643352Sgjelinek 51653352Sgjelinek if (read(procfd, &ps, sizeof (ps)) == sizeof (psinfo_t)) { 51663352Sgjelinek /* skip processes in other zones and system processes */ 51673352Sgjelinek if (zoneid != ps.pr_zoneid || ps.pr_flag & SSYS) { 51683352Sgjelinek (void) close(procfd); 51693352Sgjelinek continue; 51703352Sgjelinek } 51713352Sgjelinek 51723352Sgjelinek if (poolid_set) { 51733352Sgjelinek if (ps.pr_poolid != last_poolid) 51743352Sgjelinek mixed = B_TRUE; 51753352Sgjelinek } else { 51763352Sgjelinek last_poolid = ps.pr_poolid; 51773352Sgjelinek poolid_set = B_TRUE; 51783352Sgjelinek } 51793352Sgjelinek } 51803352Sgjelinek 51813352Sgjelinek (void) close(procfd); 51823352Sgjelinek 51833352Sgjelinek if (mixed) 51843352Sgjelinek break; 51853352Sgjelinek } 51863352Sgjelinek 51873352Sgjelinek (void) closedir(dirp); 51883352Sgjelinek 51893352Sgjelinek return (mixed); 51903352Sgjelinek } 51913352Sgjelinek 51923352Sgjelinek /* 51933352Sgjelinek * Check if a persistent or temporary pool is configured for the zone. 51943352Sgjelinek * This is currently only going to be called for the global zone from 51953352Sgjelinek * apply_func but that could be generalized in the future. 51963352Sgjelinek */ 51973352Sgjelinek static boolean_t 51983352Sgjelinek pool_configured(zone_dochandle_t handle) 51993352Sgjelinek { 52003352Sgjelinek int err1, err2; 52013352Sgjelinek struct zone_psettab pset_tab; 52023352Sgjelinek char poolname[MAXPATHLEN]; 52033352Sgjelinek 52043352Sgjelinek err1 = zonecfg_lookup_pset(handle, &pset_tab); 52053352Sgjelinek err2 = zonecfg_get_pool(handle, poolname, sizeof (poolname)); 52063352Sgjelinek 52073352Sgjelinek if (err1 == Z_NO_ENTRY && 52083352Sgjelinek (err2 == Z_NO_ENTRY || (err2 == Z_OK && strlen(poolname) == 0))) 52093352Sgjelinek return (B_FALSE); 52103352Sgjelinek 52113352Sgjelinek return (B_TRUE); 52123352Sgjelinek } 52133352Sgjelinek 52143352Sgjelinek /* 52153247Sgjelinek * This is an undocumented interface which is currently only used to apply 52163247Sgjelinek * the global zone resource management settings when the system boots. 52173247Sgjelinek * This function does not yet properly handle updating a running system so 52183247Sgjelinek * any projects running in the zone would be trashed if this function 52193247Sgjelinek * were to run after the zone had booted. It also does not reset any 52203247Sgjelinek * rctl settings that were removed from zonecfg. There is still work to be 52213247Sgjelinek * done before we can properly support dynamically updating the resource 52223247Sgjelinek * management settings for a running zone (global or non-global). Thus, this 52233247Sgjelinek * functionality is undocumented for now. 52243247Sgjelinek */ 52253247Sgjelinek /* ARGSUSED */ 52263247Sgjelinek static int 52273247Sgjelinek apply_func(int argc, char *argv[]) 52283247Sgjelinek { 52293247Sgjelinek int err; 52303247Sgjelinek int res = Z_OK; 52313247Sgjelinek priv_set_t *privset; 52323247Sgjelinek zoneid_t zoneid; 52333247Sgjelinek zone_dochandle_t handle; 52343247Sgjelinek struct zone_mcaptab mcap; 52353247Sgjelinek char pool_err[128]; 52363247Sgjelinek 52373247Sgjelinek zoneid = getzoneid(); 52383247Sgjelinek 52393247Sgjelinek if (zonecfg_in_alt_root() || zoneid != GLOBAL_ZONEID || 52403247Sgjelinek target_zone == NULL || strcmp(target_zone, GLOBAL_ZONENAME) != 0) 52413247Sgjelinek return (usage(B_FALSE)); 52423247Sgjelinek 52433247Sgjelinek if ((privset = priv_allocset()) == NULL) { 52443247Sgjelinek zerror(gettext("%s failed"), "priv_allocset"); 52453247Sgjelinek return (Z_ERR); 52463247Sgjelinek } 52473247Sgjelinek 52483247Sgjelinek if (getppriv(PRIV_EFFECTIVE, privset) != 0) { 52493247Sgjelinek zerror(gettext("%s failed"), "getppriv"); 52503247Sgjelinek priv_freeset(privset); 52513247Sgjelinek return (Z_ERR); 52523247Sgjelinek } 52533247Sgjelinek 52543247Sgjelinek if (priv_isfullset(privset) == B_FALSE) { 52553247Sgjelinek (void) usage(B_FALSE); 52563247Sgjelinek priv_freeset(privset); 52573247Sgjelinek return (Z_ERR); 52583247Sgjelinek } 52593247Sgjelinek priv_freeset(privset); 52603247Sgjelinek 52613247Sgjelinek if ((handle = zonecfg_init_handle()) == NULL) { 52623247Sgjelinek zperror(cmd_to_str(CMD_APPLY), B_TRUE); 52633247Sgjelinek return (Z_ERR); 52643247Sgjelinek } 52653247Sgjelinek 52663247Sgjelinek if ((err = zonecfg_get_handle(target_zone, handle)) != Z_OK) { 52673247Sgjelinek errno = err; 52683247Sgjelinek zperror(cmd_to_str(CMD_APPLY), B_TRUE); 52693247Sgjelinek zonecfg_fini_handle(handle); 52703247Sgjelinek return (Z_ERR); 52713247Sgjelinek } 52723247Sgjelinek 52733247Sgjelinek /* specific error msgs are printed within apply_rctls */ 52743247Sgjelinek if ((err = zonecfg_apply_rctls(target_zone, handle)) != Z_OK) { 52753247Sgjelinek errno = err; 52763247Sgjelinek zperror(cmd_to_str(CMD_APPLY), B_TRUE); 52773247Sgjelinek res = Z_ERR; 52783247Sgjelinek } 52793247Sgjelinek 52803247Sgjelinek if ((err = check_cpu_shares_sched(handle)) != Z_OK) 52813247Sgjelinek res = Z_ERR; 52823247Sgjelinek 52833352Sgjelinek if (pool_configured(handle)) { 52843352Sgjelinek if (mixed_pools(zoneid)) { 52853352Sgjelinek zerror(gettext("Zone is using multiple resource " 52863352Sgjelinek "pools. The pool\nconfiguration cannot be " 52873352Sgjelinek "applied without rebooting.")); 52883352Sgjelinek res = Z_ERR; 52893352Sgjelinek } else { 52903352Sgjelinek 52913352Sgjelinek /* 52923352Sgjelinek * The next two blocks of code attempt to set up 52933352Sgjelinek * temporary pools as well as persistent pools. In 52943352Sgjelinek * both cases we call the functions unconditionally. 52953352Sgjelinek * Within each funtion the code will check if the zone 52963352Sgjelinek * is actually configured for a temporary pool or 52973352Sgjelinek * persistent pool and just return if there is nothing 52983352Sgjelinek * to do. 52993352Sgjelinek */ 53003352Sgjelinek if ((err = zonecfg_bind_tmp_pool(handle, zoneid, 53013352Sgjelinek pool_err, sizeof (pool_err))) != Z_OK) { 53023352Sgjelinek if (err == Z_POOL || err == Z_POOL_CREATE || 53033352Sgjelinek err == Z_POOL_BIND) 53043352Sgjelinek zerror("%s: %s", zonecfg_strerror(err), 53053352Sgjelinek pool_err); 53063352Sgjelinek else 53073352Sgjelinek zerror(gettext("could not bind zone to " 53083352Sgjelinek "temporary pool: %s"), 53093352Sgjelinek zonecfg_strerror(err)); 53103352Sgjelinek res = Z_ERR; 53113352Sgjelinek } 53123352Sgjelinek 53133352Sgjelinek if ((err = zonecfg_bind_pool(handle, zoneid, pool_err, 53143352Sgjelinek sizeof (pool_err))) != Z_OK) { 53153352Sgjelinek if (err == Z_POOL || err == Z_POOL_BIND) 53163352Sgjelinek zerror("%s: %s", zonecfg_strerror(err), 53173352Sgjelinek pool_err); 53183352Sgjelinek else 53193352Sgjelinek zerror("%s", zonecfg_strerror(err)); 53203352Sgjelinek } 53213352Sgjelinek } 53223247Sgjelinek } 53233247Sgjelinek 53243247Sgjelinek /* 53253247Sgjelinek * If a memory cap is configured, set the cap in the kernel using 53263247Sgjelinek * zone_setattr() and make sure the rcapd SMF service is enabled. 53273247Sgjelinek */ 53283247Sgjelinek if (zonecfg_getmcapent(handle, &mcap) == Z_OK) { 53293247Sgjelinek uint64_t num; 53303247Sgjelinek char smf_err[128]; 53313247Sgjelinek 53323247Sgjelinek num = (uint64_t)strtoll(mcap.zone_physmem_cap, NULL, 10); 53333247Sgjelinek if (zone_setattr(zoneid, ZONE_ATTR_PHYS_MCAP, &num, 0) == -1) { 53343247Sgjelinek zerror(gettext("could not set zone memory cap")); 53353247Sgjelinek res = Z_ERR; 53363247Sgjelinek } 53373247Sgjelinek 53383247Sgjelinek if (zonecfg_enable_rcapd(smf_err, sizeof (smf_err)) != Z_OK) { 53393247Sgjelinek zerror(gettext("enabling system/rcap service failed: " 53403247Sgjelinek "%s"), smf_err); 53413247Sgjelinek res = Z_ERR; 53423247Sgjelinek } 53433247Sgjelinek } 53443247Sgjelinek 53453247Sgjelinek zonecfg_fini_handle(handle); 53463247Sgjelinek 53473247Sgjelinek return (res); 53483247Sgjelinek } 53493247Sgjelinek 53502303Scarlsonj static int 53510Sstevel@tonic-gate help_func(int argc, char *argv[]) 53520Sstevel@tonic-gate { 53530Sstevel@tonic-gate int arg, cmd_num; 53540Sstevel@tonic-gate 53550Sstevel@tonic-gate if (argc == 0) { 53560Sstevel@tonic-gate (void) usage(B_TRUE); 53570Sstevel@tonic-gate return (Z_OK); 53580Sstevel@tonic-gate } 53590Sstevel@tonic-gate optind = 0; 53600Sstevel@tonic-gate if ((arg = getopt(argc, argv, "?")) != EOF) { 53610Sstevel@tonic-gate switch (arg) { 53620Sstevel@tonic-gate case '?': 53630Sstevel@tonic-gate sub_usage(SHELP_HELP, CMD_HELP); 53640Sstevel@tonic-gate return (optopt == '?' ? Z_OK : Z_USAGE); 53650Sstevel@tonic-gate default: 53660Sstevel@tonic-gate sub_usage(SHELP_HELP, CMD_HELP); 53670Sstevel@tonic-gate return (Z_USAGE); 53680Sstevel@tonic-gate } 53690Sstevel@tonic-gate } 53700Sstevel@tonic-gate while (optind < argc) { 5371988Scarlsonj /* Private commands have NULL short_usage; omit them */ 5372988Scarlsonj if ((cmd_num = cmd_match(argv[optind])) < 0 || 5373988Scarlsonj cmdtab[cmd_num].short_usage == NULL) { 53740Sstevel@tonic-gate sub_usage(SHELP_HELP, CMD_HELP); 53750Sstevel@tonic-gate return (Z_USAGE); 53760Sstevel@tonic-gate } 53770Sstevel@tonic-gate sub_usage(cmdtab[cmd_num].short_usage, cmd_num); 53780Sstevel@tonic-gate optind++; 53790Sstevel@tonic-gate } 53800Sstevel@tonic-gate return (Z_OK); 53810Sstevel@tonic-gate } 53820Sstevel@tonic-gate 53830Sstevel@tonic-gate /* 53840Sstevel@tonic-gate * Returns: CMD_MIN thru CMD_MAX on success, -1 on error 53850Sstevel@tonic-gate */ 53860Sstevel@tonic-gate 53870Sstevel@tonic-gate static int 53880Sstevel@tonic-gate cmd_match(char *cmd) 53890Sstevel@tonic-gate { 53900Sstevel@tonic-gate int i; 53910Sstevel@tonic-gate 53920Sstevel@tonic-gate for (i = CMD_MIN; i <= CMD_MAX; i++) { 53930Sstevel@tonic-gate /* return only if there is an exact match */ 53940Sstevel@tonic-gate if (strcmp(cmd, cmdtab[i].cmd_name) == 0) 53950Sstevel@tonic-gate return (cmdtab[i].cmd_num); 53960Sstevel@tonic-gate } 53970Sstevel@tonic-gate return (-1); 53980Sstevel@tonic-gate } 53990Sstevel@tonic-gate 54000Sstevel@tonic-gate static int 54010Sstevel@tonic-gate parse_and_run(int argc, char *argv[]) 54020Sstevel@tonic-gate { 54030Sstevel@tonic-gate int i = cmd_match(argv[0]); 54040Sstevel@tonic-gate 54050Sstevel@tonic-gate if (i < 0) 54060Sstevel@tonic-gate return (usage(B_FALSE)); 54070Sstevel@tonic-gate return (cmdtab[i].handler(argc - 1, &(argv[1]))); 54080Sstevel@tonic-gate } 54090Sstevel@tonic-gate 54100Sstevel@tonic-gate static char * 54110Sstevel@tonic-gate get_execbasename(char *execfullname) 54120Sstevel@tonic-gate { 54130Sstevel@tonic-gate char *last_slash, *execbasename; 54140Sstevel@tonic-gate 54150Sstevel@tonic-gate /* guard against '/' at end of command invocation */ 54160Sstevel@tonic-gate for (;;) { 54170Sstevel@tonic-gate last_slash = strrchr(execfullname, '/'); 54180Sstevel@tonic-gate if (last_slash == NULL) { 54190Sstevel@tonic-gate execbasename = execfullname; 54200Sstevel@tonic-gate break; 54210Sstevel@tonic-gate } else { 54220Sstevel@tonic-gate execbasename = last_slash + 1; 54230Sstevel@tonic-gate if (*execbasename == '\0') { 54240Sstevel@tonic-gate *last_slash = '\0'; 54250Sstevel@tonic-gate continue; 54260Sstevel@tonic-gate } 54270Sstevel@tonic-gate break; 54280Sstevel@tonic-gate } 54290Sstevel@tonic-gate } 54300Sstevel@tonic-gate return (execbasename); 54310Sstevel@tonic-gate } 54320Sstevel@tonic-gate 54330Sstevel@tonic-gate int 54340Sstevel@tonic-gate main(int argc, char **argv) 54350Sstevel@tonic-gate { 54360Sstevel@tonic-gate int arg; 54370Sstevel@tonic-gate zoneid_t zid; 5438766Scarlsonj struct stat st; 54392712Snn35248 char *zone_lock_env; 54402712Snn35248 int err; 54410Sstevel@tonic-gate 54420Sstevel@tonic-gate if ((locale = setlocale(LC_ALL, "")) == NULL) 54430Sstevel@tonic-gate locale = "C"; 54440Sstevel@tonic-gate (void) textdomain(TEXT_DOMAIN); 54450Sstevel@tonic-gate setbuf(stdout, NULL); 54460Sstevel@tonic-gate (void) sigset(SIGHUP, SIG_IGN); 54470Sstevel@tonic-gate execname = get_execbasename(argv[0]); 54480Sstevel@tonic-gate target_zone = NULL; 54490Sstevel@tonic-gate if (chdir("/") != 0) { 54500Sstevel@tonic-gate zerror(gettext("could not change directory to /.")); 54510Sstevel@tonic-gate exit(Z_ERR); 54520Sstevel@tonic-gate } 54530Sstevel@tonic-gate 54542082Seschrock if (init_zfs() != Z_OK) 54552082Seschrock exit(Z_ERR); 54562082Seschrock 54572303Scarlsonj while ((arg = getopt(argc, argv, "?u:z:R:")) != EOF) { 54580Sstevel@tonic-gate switch (arg) { 54590Sstevel@tonic-gate case '?': 54600Sstevel@tonic-gate return (usage(B_TRUE)); 54612303Scarlsonj case 'u': 54622303Scarlsonj target_uuid = optarg; 54632303Scarlsonj break; 54640Sstevel@tonic-gate case 'z': 54650Sstevel@tonic-gate target_zone = optarg; 54660Sstevel@tonic-gate break; 5467766Scarlsonj case 'R': /* private option for admin/install use */ 5468766Scarlsonj if (*optarg != '/') { 5469766Scarlsonj zerror(gettext("root path must be absolute.")); 5470766Scarlsonj exit(Z_ERR); 5471766Scarlsonj } 5472766Scarlsonj if (stat(optarg, &st) == -1 || !S_ISDIR(st.st_mode)) { 5473766Scarlsonj zerror( 5474766Scarlsonj gettext("root path must be a directory.")); 5475766Scarlsonj exit(Z_ERR); 5476766Scarlsonj } 5477766Scarlsonj zonecfg_set_root(optarg); 5478766Scarlsonj break; 54790Sstevel@tonic-gate default: 54800Sstevel@tonic-gate return (usage(B_FALSE)); 54810Sstevel@tonic-gate } 54820Sstevel@tonic-gate } 54830Sstevel@tonic-gate 54840Sstevel@tonic-gate if (optind >= argc) 54850Sstevel@tonic-gate return (usage(B_FALSE)); 54862303Scarlsonj 54872303Scarlsonj if (target_uuid != NULL && *target_uuid != '\0') { 54882303Scarlsonj uuid_t uuid; 54892303Scarlsonj static char newtarget[ZONENAME_MAX]; 54902303Scarlsonj 54912303Scarlsonj if (uuid_parse(target_uuid, uuid) == -1) { 54922303Scarlsonj zerror(gettext("illegal UUID value specified")); 54932303Scarlsonj exit(Z_ERR); 54942303Scarlsonj } 54952303Scarlsonj if (zonecfg_get_name_by_uuid(uuid, newtarget, 54962303Scarlsonj sizeof (newtarget)) == Z_OK) 54972303Scarlsonj target_zone = newtarget; 54982303Scarlsonj } 54992303Scarlsonj 55000Sstevel@tonic-gate if (target_zone != NULL && zone_get_id(target_zone, &zid) != 0) { 55010Sstevel@tonic-gate errno = Z_NO_ZONE; 55020Sstevel@tonic-gate zperror(target_zone, B_TRUE); 55030Sstevel@tonic-gate exit(Z_ERR); 55040Sstevel@tonic-gate } 55052712Snn35248 55062712Snn35248 /* 55072712Snn35248 * See if we have inherited the right to manipulate this zone from 55082712Snn35248 * a zoneadm instance in our ancestry. If so, set zone_lock_cnt to 55092712Snn35248 * indicate it. If not, make that explicit in our environment. 55102712Snn35248 */ 55112712Snn35248 zone_lock_env = getenv(LOCK_ENV_VAR); 55122712Snn35248 if (zone_lock_env == NULL) { 55132712Snn35248 if (putenv(zoneadm_lock_not_held) != 0) { 55142712Snn35248 zperror(target_zone, B_TRUE); 55152712Snn35248 exit(Z_ERR); 55162712Snn35248 } 55172712Snn35248 } else { 55182712Snn35248 zoneadm_is_nested = B_TRUE; 55192712Snn35248 if (atoi(zone_lock_env) == 1) 55202712Snn35248 zone_lock_cnt = 1; 55212712Snn35248 } 55222712Snn35248 55232712Snn35248 /* 55242712Snn35248 * If we are going to be operating on a single zone, retrieve its 55252712Snn35248 * brand type and determine whether it is native or not. 55262712Snn35248 */ 55272712Snn35248 if ((target_zone != NULL) && 55282712Snn35248 (strcmp(target_zone, GLOBAL_ZONENAME) != NULL)) { 55292712Snn35248 if (zone_get_brand(target_zone, target_brand, 55302712Snn35248 sizeof (target_brand)) != Z_OK) { 55312712Snn35248 zerror(gettext("missing or invalid brand")); 55322712Snn35248 exit(Z_ERR); 55332712Snn35248 } 55342712Snn35248 is_native_zone = (strcmp(target_brand, NATIVE_BRAND_NAME) == 0); 55352712Snn35248 } 55362712Snn35248 55372712Snn35248 err = parse_and_run(argc - optind, &argv[optind]); 55382712Snn35248 55392712Snn35248 return (err); 55400Sstevel@tonic-gate } 5541