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> 793686Sgjelinek #include <strings.h> 800Sstevel@tonic-gate 810Sstevel@tonic-gate #include <pool.h> 820Sstevel@tonic-gate #include <sys/pool.h> 833247Sgjelinek #include <sys/priocntl.h> 843247Sgjelinek #include <sys/fsspriocntl.h> 850Sstevel@tonic-gate 861867Sgjelinek #include "zoneadm.h" 871867Sgjelinek 880Sstevel@tonic-gate #define MAXARGS 8 890Sstevel@tonic-gate 900Sstevel@tonic-gate /* Reflects kernel zone entries */ 910Sstevel@tonic-gate typedef struct zone_entry { 920Sstevel@tonic-gate zoneid_t zid; 930Sstevel@tonic-gate char zname[ZONENAME_MAX]; 940Sstevel@tonic-gate char *zstate_str; 950Sstevel@tonic-gate zone_state_t zstate_num; 962712Snn35248 char zbrand[MAXNAMELEN]; 970Sstevel@tonic-gate char zroot[MAXPATHLEN]; 982303Scarlsonj char zuuid[UUID_PRINTABLE_STRING_LENGTH]; 993448Sdh155122 zone_iptype_t ziptype; 1000Sstevel@tonic-gate } zone_entry_t; 1010Sstevel@tonic-gate 1020Sstevel@tonic-gate static zone_entry_t *zents; 1030Sstevel@tonic-gate static size_t nzents; 1042712Snn35248 static boolean_t is_native_zone = B_TRUE; 1050Sstevel@tonic-gate 1061915Sgjelinek #define LOOPBACK_IF "lo0" 1071915Sgjelinek #define SOCKET_AF(af) (((af) == AF_UNSPEC) ? AF_INET : (af)) 1081915Sgjelinek 1091915Sgjelinek struct net_if { 1101915Sgjelinek char *name; 1111915Sgjelinek int af; 1121915Sgjelinek }; 1131915Sgjelinek 1140Sstevel@tonic-gate /* 0755 is the default directory mode. */ 1150Sstevel@tonic-gate #define DEFAULT_DIR_MODE \ 1160Sstevel@tonic-gate (S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) 1170Sstevel@tonic-gate 1180Sstevel@tonic-gate struct cmd { 1190Sstevel@tonic-gate uint_t cmd_num; /* command number */ 1200Sstevel@tonic-gate char *cmd_name; /* command name */ 1210Sstevel@tonic-gate char *short_usage; /* short form help */ 1220Sstevel@tonic-gate int (*handler)(int argc, char *argv[]); /* function to call */ 1230Sstevel@tonic-gate 1240Sstevel@tonic-gate }; 1250Sstevel@tonic-gate 1260Sstevel@tonic-gate #define SHELP_HELP "help" 1272267Sdp #define SHELP_BOOT "boot [-- boot_arguments]" 1280Sstevel@tonic-gate #define SHELP_HALT "halt" 1290Sstevel@tonic-gate #define SHELP_READY "ready" 1302267Sdp #define SHELP_REBOOT "reboot [-- boot_arguments]" 1310Sstevel@tonic-gate #define SHELP_LIST "list [-cipv]" 1320Sstevel@tonic-gate #define SHELP_VERIFY "verify" 1332712Snn35248 #define SHELP_INSTALL "install [-x nodataset] [brand-specific args]" 1340Sstevel@tonic-gate #define SHELP_UNINSTALL "uninstall [-F]" 1351867Sgjelinek #define SHELP_CLONE "clone [-m method] [-s <ZFS snapshot>] zonename" 1361300Sgjelinek #define SHELP_MOVE "move zonepath" 1372078Sgjelinek #define SHELP_DETACH "detach [-n]" 1382078Sgjelinek #define SHELP_ATTACH "attach [-F] [-n <path>]" 1392303Scarlsonj #define SHELP_MARK "mark incomplete" 1400Sstevel@tonic-gate 1412712Snn35248 #define EXEC_PREFIX "exec " 1422712Snn35248 #define EXEC_LEN (strlen(EXEC_PREFIX)) 1432712Snn35248 #define RMCOMMAND "/usr/bin/rm -rf" 1442712Snn35248 1452712Snn35248 static int cleanup_zonepath(char *, boolean_t); 1462712Snn35248 1473448Sdh155122 extern int ifname_open(char *); 1483448Sdh155122 1490Sstevel@tonic-gate static int help_func(int argc, char *argv[]); 1500Sstevel@tonic-gate static int ready_func(int argc, char *argv[]); 1510Sstevel@tonic-gate static int boot_func(int argc, char *argv[]); 1520Sstevel@tonic-gate static int halt_func(int argc, char *argv[]); 1530Sstevel@tonic-gate static int reboot_func(int argc, char *argv[]); 1540Sstevel@tonic-gate static int list_func(int argc, char *argv[]); 1550Sstevel@tonic-gate static int verify_func(int argc, char *argv[]); 1560Sstevel@tonic-gate static int install_func(int argc, char *argv[]); 1570Sstevel@tonic-gate static int uninstall_func(int argc, char *argv[]); 158766Scarlsonj static int mount_func(int argc, char *argv[]); 159766Scarlsonj static int unmount_func(int argc, char *argv[]); 1601300Sgjelinek static int clone_func(int argc, char *argv[]); 1611300Sgjelinek static int move_func(int argc, char *argv[]); 1621507Sgjelinek static int detach_func(int argc, char *argv[]); 1631507Sgjelinek static int attach_func(int argc, char *argv[]); 1642303Scarlsonj static int mark_func(int argc, char *argv[]); 1653247Sgjelinek static int apply_func(int argc, char *argv[]); 1660Sstevel@tonic-gate static int sanity_check(char *zone, int cmd_num, boolean_t running, 1672712Snn35248 boolean_t unsafe_when_running, boolean_t force); 1680Sstevel@tonic-gate static int cmd_match(char *cmd); 1693339Szt129084 static int verify_details(int, char *argv[]); 1703339Szt129084 static int verify_brand(zone_dochandle_t, int, char *argv[]); 1713339Szt129084 static int invoke_brand_handler(int, char *argv[]); 1720Sstevel@tonic-gate 1730Sstevel@tonic-gate static struct cmd cmdtab[] = { 1740Sstevel@tonic-gate { CMD_HELP, "help", SHELP_HELP, help_func }, 1750Sstevel@tonic-gate { CMD_BOOT, "boot", SHELP_BOOT, boot_func }, 1760Sstevel@tonic-gate { CMD_HALT, "halt", SHELP_HALT, halt_func }, 1770Sstevel@tonic-gate { CMD_READY, "ready", SHELP_READY, ready_func }, 1780Sstevel@tonic-gate { CMD_REBOOT, "reboot", SHELP_REBOOT, reboot_func }, 1790Sstevel@tonic-gate { CMD_LIST, "list", SHELP_LIST, list_func }, 1800Sstevel@tonic-gate { CMD_VERIFY, "verify", SHELP_VERIFY, verify_func }, 1810Sstevel@tonic-gate { CMD_INSTALL, "install", SHELP_INSTALL, install_func }, 1820Sstevel@tonic-gate { CMD_UNINSTALL, "uninstall", SHELP_UNINSTALL, 183766Scarlsonj uninstall_func }, 1841300Sgjelinek /* mount and unmount are private commands for admin/install */ 185766Scarlsonj { CMD_MOUNT, "mount", NULL, mount_func }, 1861300Sgjelinek { CMD_UNMOUNT, "unmount", NULL, unmount_func }, 1871300Sgjelinek { CMD_CLONE, "clone", SHELP_CLONE, clone_func }, 1881507Sgjelinek { CMD_MOVE, "move", SHELP_MOVE, move_func }, 1891507Sgjelinek { CMD_DETACH, "detach", SHELP_DETACH, detach_func }, 1902303Scarlsonj { CMD_ATTACH, "attach", SHELP_ATTACH, attach_func }, 1913247Sgjelinek { CMD_MARK, "mark", SHELP_MARK, mark_func }, 1923247Sgjelinek { CMD_APPLY, "apply", NULL, apply_func } 1930Sstevel@tonic-gate }; 1940Sstevel@tonic-gate 1950Sstevel@tonic-gate /* global variables */ 1960Sstevel@tonic-gate 1970Sstevel@tonic-gate /* set early in main(), never modified thereafter, used all over the place */ 1980Sstevel@tonic-gate static char *execname; 1992712Snn35248 static char target_brand[MAXNAMELEN]; 2000Sstevel@tonic-gate static char *locale; 2011867Sgjelinek char *target_zone; 2022303Scarlsonj static char *target_uuid; 2030Sstevel@tonic-gate 2040Sstevel@tonic-gate /* used in do_subproc() and signal handler */ 2050Sstevel@tonic-gate static volatile boolean_t child_killed; 2062712Snn35248 static int do_subproc_cnt = 0; 2072712Snn35248 2082712Snn35248 /* 2092712Snn35248 * Used to indicate whether this zoneadm instance has another zoneadm 2102712Snn35248 * instance in its ancestry. 2112712Snn35248 */ 2122712Snn35248 static boolean_t zoneadm_is_nested = B_FALSE; 2132712Snn35248 2142712Snn35248 /* used to track nested zone-lock operations */ 2152712Snn35248 static int zone_lock_cnt = 0; 2162712Snn35248 2172712Snn35248 /* used to communicate lock status to children */ 2182712Snn35248 #define LOCK_ENV_VAR "_ZONEADM_LOCK_HELD" 2192712Snn35248 static char zoneadm_lock_held[] = LOCK_ENV_VAR"=1"; 2202712Snn35248 static char zoneadm_lock_not_held[] = LOCK_ENV_VAR"=0"; 2210Sstevel@tonic-gate 2221867Sgjelinek char * 2230Sstevel@tonic-gate cmd_to_str(int cmd_num) 2240Sstevel@tonic-gate { 2250Sstevel@tonic-gate assert(cmd_num >= CMD_MIN && cmd_num <= CMD_MAX); 2260Sstevel@tonic-gate return (cmdtab[cmd_num].cmd_name); 2270Sstevel@tonic-gate } 2280Sstevel@tonic-gate 2290Sstevel@tonic-gate /* This is a separate function because of gettext() wrapping. */ 2300Sstevel@tonic-gate static char * 2310Sstevel@tonic-gate long_help(int cmd_num) 2320Sstevel@tonic-gate { 233222Scomay assert(cmd_num >= CMD_MIN && cmd_num <= CMD_MAX); 2340Sstevel@tonic-gate switch (cmd_num) { 2351634Sgjelinek case CMD_HELP: 2361634Sgjelinek return (gettext("Print usage message.")); 2371634Sgjelinek case CMD_BOOT: 2382267Sdp return (gettext("Activates (boots) specified zone. See " 2392267Sdp "zoneadm(1m) for valid boot\n\targuments.")); 2401634Sgjelinek case CMD_HALT: 2411634Sgjelinek return (gettext("Halts specified zone, bypassing shutdown " 2421634Sgjelinek "scripts and removing runtime\n\tresources of the zone.")); 2431634Sgjelinek case CMD_READY: 2441634Sgjelinek return (gettext("Prepares a zone for running applications but " 2451634Sgjelinek "does not start any user\n\tprocesses in the zone.")); 2461634Sgjelinek case CMD_REBOOT: 2471634Sgjelinek return (gettext("Restarts the zone (equivalent to a halt / " 2482267Sdp "boot sequence).\n\tFails if the zone is not active. " 2492267Sdp "See zoneadm(1m) for valid boot\n\targuments.")); 2501634Sgjelinek case CMD_LIST: 2511634Sgjelinek return (gettext("Lists the current zones, or a " 2521634Sgjelinek "specific zone if indicated. By default,\n\tall " 2531634Sgjelinek "running zones are listed, though this can be " 2541634Sgjelinek "expanded to all\n\tinstalled zones with the -i " 2551634Sgjelinek "option or all configured zones with the\n\t-c " 2562303Scarlsonj "option. When used with the general -z <zone> and/or -u " 2572303Scarlsonj "<uuid-match>\n\toptions, lists only the specified " 2582303Scarlsonj "matching zone, but lists it\n\tregardless of its state, " 2592303Scarlsonj "and the -i and -c options are disallowed. The\n\t-v " 2602303Scarlsonj "option can be used to display verbose information: zone " 2612303Scarlsonj "name, id,\n\tcurrent state, root directory and options. " 2622303Scarlsonj "The -p option can be used\n\tto request machine-parsable " 2632303Scarlsonj "output. The -v and -p options are mutually\n\texclusive." 2642303Scarlsonj " If neither -v nor -p is used, just the zone name is " 2652303Scarlsonj "listed.")); 2661634Sgjelinek case CMD_VERIFY: 2671634Sgjelinek return (gettext("Check to make sure the configuration " 2681634Sgjelinek "can safely be instantiated\n\ton the machine: " 2691634Sgjelinek "physical network interfaces exist, etc.")); 2701634Sgjelinek case CMD_INSTALL: 2711867Sgjelinek return (gettext("Install the configuration on to the system. " 2721867Sgjelinek "The -x nodataset option\n\tcan be used to prevent the " 2731867Sgjelinek "creation of a new ZFS file system for the\n\tzone " 2742712Snn35248 "(assuming the zonepath is within a ZFS file system).\n\t" 2752712Snn35248 "All other arguments are passed to the brand installation " 2762712Snn35248 "function;\n\tsee brand(4) for more information.")); 2771634Sgjelinek case CMD_UNINSTALL: 2781634Sgjelinek return (gettext("Uninstall the configuration from the system. " 2791634Sgjelinek "The -F flag can be used\n\tto force the action.")); 2801634Sgjelinek case CMD_CLONE: 2811867Sgjelinek return (gettext("Clone the installation of another zone. " 2821867Sgjelinek "The -m option can be used to\n\tspecify 'copy' which " 2831867Sgjelinek "forces a copy of the source zone. The -s option\n\t" 2841867Sgjelinek "can be used to specify the name of a ZFS snapshot " 2851867Sgjelinek "that was taken from\n\ta previous clone command. The " 2861867Sgjelinek "snapshot will be used as the source\n\tinstead of " 2871867Sgjelinek "creating a new ZFS snapshot.")); 2881634Sgjelinek case CMD_MOVE: 2891634Sgjelinek return (gettext("Move the zone to a new zonepath.")); 2901634Sgjelinek case CMD_DETACH: 2911634Sgjelinek return (gettext("Detach the zone from the system. The zone " 2921634Sgjelinek "state is changed to\n\t'configured' (but the files under " 2931634Sgjelinek "the zonepath are untouched).\n\tThe zone can subsequently " 2941634Sgjelinek "be attached, or can be moved to another\n\tsystem and " 2952078Sgjelinek "attached there. The -n option can be used to specify\n\t" 2962078Sgjelinek "'no-execute' mode. When -n is used, the information " 2972078Sgjelinek "needed to attach\n\tthe zone is sent to standard output " 2982078Sgjelinek "but the zone is not actually\n\tdetached.")); 2991634Sgjelinek case CMD_ATTACH: 3001634Sgjelinek return (gettext("Attach the zone to the system. The zone " 3011634Sgjelinek "state must be 'configured'\n\tprior to attach; upon " 3021634Sgjelinek "successful completion, the zone state will be\n\t" 3031634Sgjelinek "'installed'. The system software on the current " 3041634Sgjelinek "system must be\n\tcompatible with the software on the " 3051634Sgjelinek "zone's original system.\n\tSpecify -F to force the attach " 3062078Sgjelinek "and skip software compatibility tests.\n\tThe -n option " 3072078Sgjelinek "can be used to specify 'no-execute' mode. When -n is\n\t" 3082078Sgjelinek "used, the information needed to attach the zone is read " 3092078Sgjelinek "from the\n\tspecified path and the configuration is only " 3102078Sgjelinek "validated. The path can\n\tbe '-' to specify standard " 3112078Sgjelinek "input.")); 3122303Scarlsonj case CMD_MARK: 3132303Scarlsonj return (gettext("Set the state of the zone. This can be used " 3142303Scarlsonj "to force the zone\n\tstate to 'incomplete' " 3152303Scarlsonj "administratively if some activity has rendered\n\tthe " 3162303Scarlsonj "zone permanently unusable. The only valid state that " 3172303Scarlsonj "may be\n\tspecified is 'incomplete'.")); 3181634Sgjelinek default: 3191634Sgjelinek return (""); 3200Sstevel@tonic-gate } 3210Sstevel@tonic-gate /* NOTREACHED */ 322222Scomay return (NULL); 3230Sstevel@tonic-gate } 3240Sstevel@tonic-gate 3250Sstevel@tonic-gate /* 3260Sstevel@tonic-gate * Called with explicit B_TRUE when help is explicitly requested, B_FALSE for 3270Sstevel@tonic-gate * unexpected errors. 3280Sstevel@tonic-gate */ 3290Sstevel@tonic-gate 3300Sstevel@tonic-gate static int 3310Sstevel@tonic-gate usage(boolean_t explicit) 3320Sstevel@tonic-gate { 3330Sstevel@tonic-gate int i; 3340Sstevel@tonic-gate FILE *fd = explicit ? stdout : stderr; 3350Sstevel@tonic-gate 3360Sstevel@tonic-gate (void) fprintf(fd, "%s:\t%s help\n", gettext("usage"), execname); 3372303Scarlsonj (void) fprintf(fd, "\t%s [-z <zone>] [-u <uuid-match>] list\n", 3382303Scarlsonj execname); 3392303Scarlsonj (void) fprintf(fd, "\t%s {-z <zone>|-u <uuid-match>} <%s>\n", execname, 3400Sstevel@tonic-gate gettext("subcommand")); 3410Sstevel@tonic-gate (void) fprintf(fd, "\n%s:\n\n", gettext("Subcommands")); 3420Sstevel@tonic-gate for (i = CMD_MIN; i <= CMD_MAX; i++) { 343766Scarlsonj if (cmdtab[i].short_usage == NULL) 344766Scarlsonj continue; 3450Sstevel@tonic-gate (void) fprintf(fd, "%s\n", cmdtab[i].short_usage); 3460Sstevel@tonic-gate if (explicit) 3470Sstevel@tonic-gate (void) fprintf(fd, "\t%s\n\n", long_help(i)); 3480Sstevel@tonic-gate } 3490Sstevel@tonic-gate if (!explicit) 3500Sstevel@tonic-gate (void) fputs("\n", fd); 3510Sstevel@tonic-gate return (Z_USAGE); 3520Sstevel@tonic-gate } 3530Sstevel@tonic-gate 3540Sstevel@tonic-gate static void 3550Sstevel@tonic-gate sub_usage(char *short_usage, int cmd_num) 3560Sstevel@tonic-gate { 3570Sstevel@tonic-gate (void) fprintf(stderr, "%s:\t%s\n", gettext("usage"), short_usage); 3580Sstevel@tonic-gate (void) fprintf(stderr, "\t%s\n", long_help(cmd_num)); 3590Sstevel@tonic-gate } 3600Sstevel@tonic-gate 3610Sstevel@tonic-gate /* 3620Sstevel@tonic-gate * zperror() is like perror(3c) except that this also prints the executable 3630Sstevel@tonic-gate * name at the start of the message, and takes a boolean indicating whether 3640Sstevel@tonic-gate * to call libc'c strerror() or that from libzonecfg. 3650Sstevel@tonic-gate */ 3660Sstevel@tonic-gate 3671867Sgjelinek void 3680Sstevel@tonic-gate zperror(const char *str, boolean_t zonecfg_error) 3690Sstevel@tonic-gate { 3700Sstevel@tonic-gate (void) fprintf(stderr, "%s: %s: %s\n", execname, str, 3710Sstevel@tonic-gate zonecfg_error ? zonecfg_strerror(errno) : strerror(errno)); 3720Sstevel@tonic-gate } 3730Sstevel@tonic-gate 3740Sstevel@tonic-gate /* 3750Sstevel@tonic-gate * zperror2() is very similar to zperror() above, except it also prints a 3760Sstevel@tonic-gate * supplied zone name after the executable. 3770Sstevel@tonic-gate * 3780Sstevel@tonic-gate * All current consumers of this function want libzonecfg's strerror() rather 3790Sstevel@tonic-gate * than libc's; if this ever changes, this function can be made more generic 3800Sstevel@tonic-gate * like zperror() above. 3810Sstevel@tonic-gate */ 3820Sstevel@tonic-gate 3831867Sgjelinek void 3840Sstevel@tonic-gate zperror2(const char *zone, const char *str) 3850Sstevel@tonic-gate { 3860Sstevel@tonic-gate (void) fprintf(stderr, "%s: %s: %s: %s\n", execname, zone, str, 3870Sstevel@tonic-gate zonecfg_strerror(errno)); 3880Sstevel@tonic-gate } 3890Sstevel@tonic-gate 3900Sstevel@tonic-gate /* PRINTFLIKE1 */ 3911867Sgjelinek void 3920Sstevel@tonic-gate zerror(const char *fmt, ...) 3930Sstevel@tonic-gate { 3940Sstevel@tonic-gate va_list alist; 3950Sstevel@tonic-gate 3960Sstevel@tonic-gate va_start(alist, fmt); 3970Sstevel@tonic-gate (void) fprintf(stderr, "%s: ", execname); 3980Sstevel@tonic-gate if (target_zone != NULL) 3990Sstevel@tonic-gate (void) fprintf(stderr, "zone '%s': ", target_zone); 4000Sstevel@tonic-gate (void) vfprintf(stderr, fmt, alist); 4010Sstevel@tonic-gate (void) fprintf(stderr, "\n"); 4020Sstevel@tonic-gate va_end(alist); 4030Sstevel@tonic-gate } 4040Sstevel@tonic-gate 4050Sstevel@tonic-gate static void * 4060Sstevel@tonic-gate safe_calloc(size_t nelem, size_t elsize) 4070Sstevel@tonic-gate { 4080Sstevel@tonic-gate void *r = calloc(nelem, elsize); 4090Sstevel@tonic-gate 4100Sstevel@tonic-gate if (r == NULL) { 4110Sstevel@tonic-gate zerror(gettext("failed to allocate %lu bytes: %s"), 4120Sstevel@tonic-gate (ulong_t)nelem * elsize, strerror(errno)); 4130Sstevel@tonic-gate exit(Z_ERR); 4140Sstevel@tonic-gate } 4150Sstevel@tonic-gate return (r); 4160Sstevel@tonic-gate } 4170Sstevel@tonic-gate 4180Sstevel@tonic-gate static void 4190Sstevel@tonic-gate zone_print(zone_entry_t *zent, boolean_t verbose, boolean_t parsable) 4200Sstevel@tonic-gate { 4210Sstevel@tonic-gate static boolean_t firsttime = B_TRUE; 4223448Sdh155122 char *ip_type_str; 4233448Sdh155122 4243448Sdh155122 if (zent->ziptype == ZS_EXCLUSIVE) 4253448Sdh155122 ip_type_str = "excl"; 4263448Sdh155122 else 4273448Sdh155122 ip_type_str = "shared"; 4280Sstevel@tonic-gate 4290Sstevel@tonic-gate assert(!(verbose && parsable)); 4300Sstevel@tonic-gate if (firsttime && verbose) { 4310Sstevel@tonic-gate firsttime = B_FALSE; 4323448Sdh155122 (void) printf("%*s %-16s %-10s %-30s %-8s %-6s\n", 4333448Sdh155122 ZONEID_WIDTH, "ID", "NAME", "STATUS", "PATH", "BRAND", 4343448Sdh155122 "IP"); 4350Sstevel@tonic-gate } 4360Sstevel@tonic-gate if (!verbose) { 4372303Scarlsonj char *cp, *clim; 4382303Scarlsonj 4390Sstevel@tonic-gate if (!parsable) { 4400Sstevel@tonic-gate (void) printf("%s\n", zent->zname); 4410Sstevel@tonic-gate return; 4420Sstevel@tonic-gate } 4430Sstevel@tonic-gate if (zent->zid == ZONE_ID_UNDEFINED) 4440Sstevel@tonic-gate (void) printf("-"); 4450Sstevel@tonic-gate else 4460Sstevel@tonic-gate (void) printf("%lu", zent->zid); 4472303Scarlsonj (void) printf(":%s:%s:", zent->zname, zent->zstate_str); 4482303Scarlsonj cp = zent->zroot; 4492303Scarlsonj while ((clim = strchr(cp, ':')) != NULL) { 4502303Scarlsonj (void) printf("%.*s\\:", clim - cp, cp); 4512303Scarlsonj cp = clim + 1; 4522303Scarlsonj } 4533448Sdh155122 (void) printf("%s:%s:%s:%s\n", cp, zent->zuuid, zent->zbrand, 4543448Sdh155122 ip_type_str); 4550Sstevel@tonic-gate return; 4560Sstevel@tonic-gate } 4570Sstevel@tonic-gate if (zent->zstate_str != NULL) { 4580Sstevel@tonic-gate if (zent->zid == ZONE_ID_UNDEFINED) 4590Sstevel@tonic-gate (void) printf("%*s", ZONEID_WIDTH, "-"); 4600Sstevel@tonic-gate else 4610Sstevel@tonic-gate (void) printf("%*lu", ZONEID_WIDTH, zent->zid); 4623448Sdh155122 (void) printf(" %-16s %-10s %-30s %-8s %-6s\n", zent->zname, 4633448Sdh155122 zent->zstate_str, zent->zroot, zent->zbrand, ip_type_str); 4640Sstevel@tonic-gate } 4650Sstevel@tonic-gate } 4660Sstevel@tonic-gate 4670Sstevel@tonic-gate static int 468766Scarlsonj lookup_zone_info(const char *zone_name, zoneid_t zid, zone_entry_t *zent) 4690Sstevel@tonic-gate { 4701676Sjpk char root[MAXPATHLEN], *cp; 4710Sstevel@tonic-gate int err; 4722303Scarlsonj uuid_t uuid; 4730Sstevel@tonic-gate 4740Sstevel@tonic-gate (void) strlcpy(zent->zname, zone_name, sizeof (zent->zname)); 4750Sstevel@tonic-gate (void) strlcpy(zent->zroot, "???", sizeof (zent->zroot)); 4762712Snn35248 (void) strlcpy(zent->zbrand, "???", sizeof (zent->zbrand)); 4770Sstevel@tonic-gate zent->zstate_str = "???"; 4780Sstevel@tonic-gate 479766Scarlsonj zent->zid = zid; 4800Sstevel@tonic-gate 4812303Scarlsonj if (zonecfg_get_uuid(zone_name, uuid) == Z_OK && 4822303Scarlsonj !uuid_is_null(uuid)) 4832303Scarlsonj uuid_unparse(uuid, zent->zuuid); 4842303Scarlsonj else 4852303Scarlsonj zent->zuuid[0] = '\0'; 4862303Scarlsonj 4871676Sjpk /* 4881676Sjpk * For labeled zones which query the zone path of lower-level 4891676Sjpk * zones, the path needs to be adjusted to drop the final 4901676Sjpk * "/root" component. This adjusted path is then useful 4911676Sjpk * for reading down any exported directories from the 4921676Sjpk * lower-level zone. 4931676Sjpk */ 4941676Sjpk if (is_system_labeled() && zent->zid != ZONE_ID_UNDEFINED) { 4951676Sjpk if (zone_getattr(zent->zid, ZONE_ATTR_ROOT, zent->zroot, 4961676Sjpk sizeof (zent->zroot)) == -1) { 4971676Sjpk zperror2(zent->zname, 4981676Sjpk gettext("could not get zone path.")); 4991676Sjpk return (Z_ERR); 5001676Sjpk } 5011676Sjpk cp = zent->zroot + strlen(zent->zroot) - 5; 5021676Sjpk if (cp > zent->zroot && strcmp(cp, "/root") == 0) 5031676Sjpk *cp = 0; 5041676Sjpk } else { 5051676Sjpk if ((err = zone_get_zonepath(zent->zname, root, 5061676Sjpk sizeof (root))) != Z_OK) { 5071676Sjpk errno = err; 5081676Sjpk zperror2(zent->zname, 5091676Sjpk gettext("could not get zone path.")); 5101676Sjpk return (Z_ERR); 5111676Sjpk } 5121676Sjpk (void) strlcpy(zent->zroot, root, sizeof (zent->zroot)); 5131676Sjpk } 5140Sstevel@tonic-gate 5150Sstevel@tonic-gate if ((err = zone_get_state(zent->zname, &zent->zstate_num)) != Z_OK) { 5160Sstevel@tonic-gate errno = err; 5170Sstevel@tonic-gate zperror2(zent->zname, gettext("could not get state")); 5180Sstevel@tonic-gate return (Z_ERR); 5190Sstevel@tonic-gate } 5200Sstevel@tonic-gate zent->zstate_str = zone_state_str(zent->zstate_num); 5213009Snn35248 5223009Snn35248 /* 5233009Snn35248 * A zone's brand is only available in the .xml file describing it, 5243009Snn35248 * which is only visible to the global zone. This causes 5253009Snn35248 * zone_get_brand() to fail when called from within a non-global 5263009Snn35248 * zone. Fortunately we only do this on labeled systems, where we 5273009Snn35248 * know all zones are native. 5283009Snn35248 */ 5293009Snn35248 if (getzoneid() != GLOBAL_ZONEID) { 5303009Snn35248 assert(is_system_labeled() != 0); 5313009Snn35248 (void) strlcpy(zent->zbrand, NATIVE_BRAND_NAME, 5323009Snn35248 sizeof (zent->zbrand)); 5333009Snn35248 } else if (zone_get_brand(zent->zname, zent->zbrand, 5342712Snn35248 sizeof (zent->zbrand)) != Z_OK) { 5352712Snn35248 zperror2(zent->zname, gettext("could not get brand name")); 5362712Snn35248 return (Z_ERR); 5372712Snn35248 } 5380Sstevel@tonic-gate 5393448Sdh155122 /* 5403448Sdh155122 * Get ip type of the zone. 5413448Sdh155122 * Note for global zone, ZS_SHARED is set always. 5423448Sdh155122 */ 5433448Sdh155122 if (zid == GLOBAL_ZONEID) { 5443448Sdh155122 zent->ziptype = ZS_SHARED; 5453448Sdh155122 } else { 5463448Sdh155122 5473448Sdh155122 if (zent->zstate_num == ZONE_STATE_RUNNING) { 5483448Sdh155122 ushort_t flags; 5493448Sdh155122 5503448Sdh155122 if (zone_getattr(zid, ZONE_ATTR_FLAGS, &flags, 5513448Sdh155122 sizeof (flags)) < 0) { 5523448Sdh155122 zperror2(zent->zname, 5533448Sdh155122 gettext("could not get zone flags")); 5543448Sdh155122 return (Z_ERR); 5553448Sdh155122 } 5563448Sdh155122 if (flags & ZF_NET_EXCL) 5573448Sdh155122 zent->ziptype = ZS_EXCLUSIVE; 5583448Sdh155122 else 5593448Sdh155122 zent->ziptype = ZS_SHARED; 5603448Sdh155122 } else { 5613448Sdh155122 zone_dochandle_t handle; 5623448Sdh155122 5633448Sdh155122 if ((handle = zonecfg_init_handle()) == NULL) { 5643448Sdh155122 zperror2(zent->zname, 5653448Sdh155122 gettext("could not init handle")); 5663448Sdh155122 return (Z_ERR); 5673448Sdh155122 } 5683448Sdh155122 if ((err = zonecfg_get_handle(zent->zname, handle)) 5693448Sdh155122 != Z_OK) { 5703448Sdh155122 zperror2(zent->zname, 5713448Sdh155122 gettext("could not get handle")); 5723448Sdh155122 zonecfg_fini_handle(handle); 5733448Sdh155122 return (Z_ERR); 5743448Sdh155122 } 5753448Sdh155122 5763448Sdh155122 if ((err = zonecfg_get_iptype(handle, &zent->ziptype)) 5773448Sdh155122 != Z_OK) { 5783448Sdh155122 zperror2(zent->zname, 5793448Sdh155122 gettext("could not get ip-type")); 5803448Sdh155122 zonecfg_fini_handle(handle); 5813448Sdh155122 return (Z_ERR); 5823448Sdh155122 } 5833448Sdh155122 zonecfg_fini_handle(handle); 5843448Sdh155122 } 5853448Sdh155122 } 5863448Sdh155122 5870Sstevel@tonic-gate return (Z_OK); 5880Sstevel@tonic-gate } 5890Sstevel@tonic-gate 5900Sstevel@tonic-gate /* 5910Sstevel@tonic-gate * fetch_zents() calls zone_list(2) to find out how many zones are running 5920Sstevel@tonic-gate * (which is stored in the global nzents), then calls zone_list(2) again 5930Sstevel@tonic-gate * to fetch the list of running zones (stored in the global zents). This 5940Sstevel@tonic-gate * function may be called multiple times, so if zents is already set, we 5950Sstevel@tonic-gate * return immediately to save work. 5960Sstevel@tonic-gate */ 5970Sstevel@tonic-gate 5980Sstevel@tonic-gate static int 599766Scarlsonj fetch_zents(void) 6000Sstevel@tonic-gate { 6010Sstevel@tonic-gate zoneid_t *zids = NULL; 6020Sstevel@tonic-gate uint_t nzents_saved; 603766Scarlsonj int i, retv; 604766Scarlsonj FILE *fp; 605766Scarlsonj boolean_t inaltroot; 606766Scarlsonj zone_entry_t *zentp; 6070Sstevel@tonic-gate 6080Sstevel@tonic-gate if (nzents > 0) 6090Sstevel@tonic-gate return (Z_OK); 6100Sstevel@tonic-gate 6110Sstevel@tonic-gate if (zone_list(NULL, &nzents) != 0) { 6120Sstevel@tonic-gate zperror(gettext("failed to get zoneid list"), B_FALSE); 6130Sstevel@tonic-gate return (Z_ERR); 6140Sstevel@tonic-gate } 6150Sstevel@tonic-gate 6160Sstevel@tonic-gate again: 6170Sstevel@tonic-gate if (nzents == 0) 6180Sstevel@tonic-gate return (Z_OK); 6190Sstevel@tonic-gate 6200Sstevel@tonic-gate zids = safe_calloc(nzents, sizeof (zoneid_t)); 6210Sstevel@tonic-gate nzents_saved = nzents; 6220Sstevel@tonic-gate 6230Sstevel@tonic-gate if (zone_list(zids, &nzents) != 0) { 6240Sstevel@tonic-gate zperror(gettext("failed to get zone list"), B_FALSE); 6250Sstevel@tonic-gate free(zids); 6260Sstevel@tonic-gate return (Z_ERR); 6270Sstevel@tonic-gate } 6280Sstevel@tonic-gate if (nzents != nzents_saved) { 6290Sstevel@tonic-gate /* list changed, try again */ 6300Sstevel@tonic-gate free(zids); 6310Sstevel@tonic-gate goto again; 6320Sstevel@tonic-gate } 6330Sstevel@tonic-gate 6340Sstevel@tonic-gate zents = safe_calloc(nzents, sizeof (zone_entry_t)); 6350Sstevel@tonic-gate 636766Scarlsonj inaltroot = zonecfg_in_alt_root(); 637766Scarlsonj if (inaltroot) 638766Scarlsonj fp = zonecfg_open_scratch("", B_FALSE); 639766Scarlsonj else 640766Scarlsonj fp = NULL; 641766Scarlsonj zentp = zents; 642766Scarlsonj retv = Z_OK; 6430Sstevel@tonic-gate for (i = 0; i < nzents; i++) { 6440Sstevel@tonic-gate char name[ZONENAME_MAX]; 645766Scarlsonj char altname[ZONENAME_MAX]; 6460Sstevel@tonic-gate 647766Scarlsonj if (getzonenamebyid(zids[i], name, sizeof (name)) < 0) { 6480Sstevel@tonic-gate zperror(gettext("failed to get zone name"), B_FALSE); 649766Scarlsonj retv = Z_ERR; 650766Scarlsonj continue; 651766Scarlsonj } 652766Scarlsonj if (zonecfg_is_scratch(name)) { 653766Scarlsonj /* Ignore scratch zones by default */ 654766Scarlsonj if (!inaltroot) 655766Scarlsonj continue; 656766Scarlsonj if (fp == NULL || 657766Scarlsonj zonecfg_reverse_scratch(fp, name, altname, 658766Scarlsonj sizeof (altname), NULL, 0) == -1) { 659924Sgjelinek zerror(gettext("could not resolve scratch " 660766Scarlsonj "zone %s"), name); 661766Scarlsonj retv = Z_ERR; 662766Scarlsonj continue; 663766Scarlsonj } 664766Scarlsonj (void) strcpy(name, altname); 665766Scarlsonj } else { 666766Scarlsonj /* Ignore non-scratch when in an alternate root */ 667766Scarlsonj if (inaltroot && strcmp(name, GLOBAL_ZONENAME) != 0) 668766Scarlsonj continue; 669766Scarlsonj } 670766Scarlsonj if (lookup_zone_info(name, zids[i], zentp) != Z_OK) { 671766Scarlsonj zerror(gettext("failed to get zone data")); 672766Scarlsonj retv = Z_ERR; 673766Scarlsonj continue; 674766Scarlsonj } 675766Scarlsonj zentp++; 6760Sstevel@tonic-gate } 677766Scarlsonj nzents = zentp - zents; 678766Scarlsonj if (fp != NULL) 679766Scarlsonj zonecfg_close_scratch(fp); 6800Sstevel@tonic-gate 6810Sstevel@tonic-gate free(zids); 682766Scarlsonj return (retv); 6830Sstevel@tonic-gate } 6840Sstevel@tonic-gate 685766Scarlsonj static int 6860Sstevel@tonic-gate zone_print_list(zone_state_t min_state, boolean_t verbose, boolean_t parsable) 6870Sstevel@tonic-gate { 6880Sstevel@tonic-gate int i; 6890Sstevel@tonic-gate zone_entry_t zent; 6900Sstevel@tonic-gate FILE *cookie; 6910Sstevel@tonic-gate char *name; 6920Sstevel@tonic-gate 6930Sstevel@tonic-gate /* 6940Sstevel@tonic-gate * First get the list of running zones from the kernel and print them. 6950Sstevel@tonic-gate * If that is all we need, then return. 6960Sstevel@tonic-gate */ 697766Scarlsonj if ((i = fetch_zents()) != Z_OK) { 6980Sstevel@tonic-gate /* 6990Sstevel@tonic-gate * No need for error messages; fetch_zents() has already taken 7000Sstevel@tonic-gate * care of this. 7010Sstevel@tonic-gate */ 702766Scarlsonj return (i); 7030Sstevel@tonic-gate } 704766Scarlsonj for (i = 0; i < nzents; i++) 7050Sstevel@tonic-gate zone_print(&zents[i], verbose, parsable); 7060Sstevel@tonic-gate if (min_state >= ZONE_STATE_RUNNING) 707766Scarlsonj return (Z_OK); 7080Sstevel@tonic-gate /* 7090Sstevel@tonic-gate * Next, get the full list of zones from the configuration, skipping 7100Sstevel@tonic-gate * any we have already printed. 7110Sstevel@tonic-gate */ 7120Sstevel@tonic-gate cookie = setzoneent(); 7130Sstevel@tonic-gate while ((name = getzoneent(cookie)) != NULL) { 7140Sstevel@tonic-gate for (i = 0; i < nzents; i++) { 7150Sstevel@tonic-gate if (strcmp(zents[i].zname, name) == 0) 7160Sstevel@tonic-gate break; 7170Sstevel@tonic-gate } 7180Sstevel@tonic-gate if (i < nzents) { 7190Sstevel@tonic-gate free(name); 7200Sstevel@tonic-gate continue; 7210Sstevel@tonic-gate } 722766Scarlsonj if (lookup_zone_info(name, ZONE_ID_UNDEFINED, &zent) != Z_OK) { 7230Sstevel@tonic-gate free(name); 7240Sstevel@tonic-gate continue; 7250Sstevel@tonic-gate } 7260Sstevel@tonic-gate free(name); 7270Sstevel@tonic-gate if (zent.zstate_num >= min_state) 7280Sstevel@tonic-gate zone_print(&zent, verbose, parsable); 7290Sstevel@tonic-gate } 7300Sstevel@tonic-gate endzoneent(cookie); 731766Scarlsonj return (Z_OK); 7320Sstevel@tonic-gate } 7330Sstevel@tonic-gate 7340Sstevel@tonic-gate static zone_entry_t * 7350Sstevel@tonic-gate lookup_running_zone(char *str) 7360Sstevel@tonic-gate { 7370Sstevel@tonic-gate zoneid_t zoneid; 7380Sstevel@tonic-gate char *cp; 7390Sstevel@tonic-gate int i; 7400Sstevel@tonic-gate 7410Sstevel@tonic-gate if (fetch_zents() != Z_OK) 7420Sstevel@tonic-gate return (NULL); 7430Sstevel@tonic-gate 7440Sstevel@tonic-gate for (i = 0; i < nzents; i++) { 7450Sstevel@tonic-gate if (strcmp(str, zents[i].zname) == 0) 7460Sstevel@tonic-gate return (&zents[i]); 7470Sstevel@tonic-gate } 7480Sstevel@tonic-gate errno = 0; 7490Sstevel@tonic-gate zoneid = strtol(str, &cp, 0); 7500Sstevel@tonic-gate if (zoneid < MIN_ZONEID || zoneid > MAX_ZONEID || 7510Sstevel@tonic-gate errno != 0 || *cp != '\0') 7520Sstevel@tonic-gate return (NULL); 7530Sstevel@tonic-gate for (i = 0; i < nzents; i++) { 7540Sstevel@tonic-gate if (zoneid == zents[i].zid) 7550Sstevel@tonic-gate return (&zents[i]); 7560Sstevel@tonic-gate } 7570Sstevel@tonic-gate return (NULL); 7580Sstevel@tonic-gate } 7590Sstevel@tonic-gate 7600Sstevel@tonic-gate /* 7610Sstevel@tonic-gate * Check a bit in a mode_t: if on is B_TRUE, that bit should be on; if 7620Sstevel@tonic-gate * B_FALSE, it should be off. Return B_TRUE if the mode is bad (incorrect). 7630Sstevel@tonic-gate */ 7640Sstevel@tonic-gate static boolean_t 7650Sstevel@tonic-gate bad_mode_bit(mode_t mode, mode_t bit, boolean_t on, char *file) 7660Sstevel@tonic-gate { 7670Sstevel@tonic-gate char *str; 7680Sstevel@tonic-gate 7690Sstevel@tonic-gate assert(bit == S_IRUSR || bit == S_IWUSR || bit == S_IXUSR || 7700Sstevel@tonic-gate bit == S_IRGRP || bit == S_IWGRP || bit == S_IXGRP || 7710Sstevel@tonic-gate bit == S_IROTH || bit == S_IWOTH || bit == S_IXOTH); 7720Sstevel@tonic-gate /* 7730Sstevel@tonic-gate * TRANSLATION_NOTE 7740Sstevel@tonic-gate * The strings below will be used as part of a larger message, 7750Sstevel@tonic-gate * either: 7760Sstevel@tonic-gate * (file name) must be (owner|group|world) (read|writ|execut)able 7770Sstevel@tonic-gate * or 7780Sstevel@tonic-gate * (file name) must not be (owner|group|world) (read|writ|execut)able 7790Sstevel@tonic-gate */ 7800Sstevel@tonic-gate switch (bit) { 7810Sstevel@tonic-gate case S_IRUSR: 7820Sstevel@tonic-gate str = gettext("owner readable"); 7830Sstevel@tonic-gate break; 7840Sstevel@tonic-gate case S_IWUSR: 7850Sstevel@tonic-gate str = gettext("owner writable"); 7860Sstevel@tonic-gate break; 7870Sstevel@tonic-gate case S_IXUSR: 7880Sstevel@tonic-gate str = gettext("owner executable"); 7890Sstevel@tonic-gate break; 7900Sstevel@tonic-gate case S_IRGRP: 7910Sstevel@tonic-gate str = gettext("group readable"); 7920Sstevel@tonic-gate break; 7930Sstevel@tonic-gate case S_IWGRP: 7940Sstevel@tonic-gate str = gettext("group writable"); 7950Sstevel@tonic-gate break; 7960Sstevel@tonic-gate case S_IXGRP: 7970Sstevel@tonic-gate str = gettext("group executable"); 7980Sstevel@tonic-gate break; 7990Sstevel@tonic-gate case S_IROTH: 8000Sstevel@tonic-gate str = gettext("world readable"); 8010Sstevel@tonic-gate break; 8020Sstevel@tonic-gate case S_IWOTH: 8030Sstevel@tonic-gate str = gettext("world writable"); 8040Sstevel@tonic-gate break; 8050Sstevel@tonic-gate case S_IXOTH: 8060Sstevel@tonic-gate str = gettext("world executable"); 8070Sstevel@tonic-gate break; 8080Sstevel@tonic-gate } 8090Sstevel@tonic-gate if ((mode & bit) == (on ? 0 : bit)) { 8100Sstevel@tonic-gate /* 8110Sstevel@tonic-gate * TRANSLATION_NOTE 8120Sstevel@tonic-gate * The first parameter below is a file name; the second 8130Sstevel@tonic-gate * is one of the "(owner|group|world) (read|writ|execut)able" 8140Sstevel@tonic-gate * strings from above. 8150Sstevel@tonic-gate */ 8160Sstevel@tonic-gate /* 8170Sstevel@tonic-gate * The code below could be simplified but not in a way 8180Sstevel@tonic-gate * that would easily translate to non-English locales. 8190Sstevel@tonic-gate */ 8200Sstevel@tonic-gate if (on) { 8210Sstevel@tonic-gate (void) fprintf(stderr, gettext("%s must be %s.\n"), 8220Sstevel@tonic-gate file, str); 8230Sstevel@tonic-gate } else { 8240Sstevel@tonic-gate (void) fprintf(stderr, gettext("%s must not be %s.\n"), 8250Sstevel@tonic-gate file, str); 8260Sstevel@tonic-gate } 8270Sstevel@tonic-gate return (B_TRUE); 8280Sstevel@tonic-gate } 8290Sstevel@tonic-gate return (B_FALSE); 8300Sstevel@tonic-gate } 8310Sstevel@tonic-gate 8320Sstevel@tonic-gate /* 8330Sstevel@tonic-gate * We want to make sure that no zone has its zone path as a child node 8340Sstevel@tonic-gate * (in the directory sense) of any other. We do that by comparing this 8350Sstevel@tonic-gate * zone's path to the path of all other (non-global) zones. The comparison 8360Sstevel@tonic-gate * in each case is simple: add '/' to the end of the path, then do a 8370Sstevel@tonic-gate * strncmp() of the two paths, using the length of the shorter one. 8380Sstevel@tonic-gate */ 8390Sstevel@tonic-gate 8400Sstevel@tonic-gate static int 8410Sstevel@tonic-gate crosscheck_zonepaths(char *path) 8420Sstevel@tonic-gate { 8430Sstevel@tonic-gate char rpath[MAXPATHLEN]; /* resolved path */ 8440Sstevel@tonic-gate char path_copy[MAXPATHLEN]; /* copy of original path */ 8450Sstevel@tonic-gate char rpath_copy[MAXPATHLEN]; /* copy of original rpath */ 8460Sstevel@tonic-gate struct zoneent *ze; 8470Sstevel@tonic-gate int res, err; 8480Sstevel@tonic-gate FILE *cookie; 8490Sstevel@tonic-gate 8500Sstevel@tonic-gate cookie = setzoneent(); 8510Sstevel@tonic-gate while ((ze = getzoneent_private(cookie)) != NULL) { 8520Sstevel@tonic-gate /* Skip zones which are not installed. */ 8530Sstevel@tonic-gate if (ze->zone_state < ZONE_STATE_INSTALLED) { 8540Sstevel@tonic-gate free(ze); 8550Sstevel@tonic-gate continue; 8560Sstevel@tonic-gate } 8570Sstevel@tonic-gate /* Skip the global zone and the current target zone. */ 8580Sstevel@tonic-gate if (strcmp(ze->zone_name, GLOBAL_ZONENAME) == 0 || 8590Sstevel@tonic-gate strcmp(ze->zone_name, target_zone) == 0) { 8600Sstevel@tonic-gate free(ze); 8610Sstevel@tonic-gate continue; 8620Sstevel@tonic-gate } 8630Sstevel@tonic-gate if (strlen(ze->zone_path) == 0) { 8640Sstevel@tonic-gate /* old index file without path, fall back */ 8650Sstevel@tonic-gate if ((err = zone_get_zonepath(ze->zone_name, 8660Sstevel@tonic-gate ze->zone_path, sizeof (ze->zone_path))) != Z_OK) { 8670Sstevel@tonic-gate errno = err; 8680Sstevel@tonic-gate zperror2(ze->zone_name, 8690Sstevel@tonic-gate gettext("could not get zone path")); 8700Sstevel@tonic-gate free(ze); 8710Sstevel@tonic-gate continue; 8720Sstevel@tonic-gate } 8730Sstevel@tonic-gate } 874766Scarlsonj (void) snprintf(path_copy, sizeof (path_copy), "%s%s", 875766Scarlsonj zonecfg_get_root(), ze->zone_path); 876766Scarlsonj res = resolvepath(path_copy, rpath, sizeof (rpath)); 8770Sstevel@tonic-gate if (res == -1) { 8780Sstevel@tonic-gate if (errno != ENOENT) { 879766Scarlsonj zperror(path_copy, B_FALSE); 8800Sstevel@tonic-gate free(ze); 8810Sstevel@tonic-gate return (Z_ERR); 8820Sstevel@tonic-gate } 8830Sstevel@tonic-gate (void) printf(gettext("WARNING: zone %s is installed, " 8840Sstevel@tonic-gate "but its %s %s does not exist.\n"), ze->zone_name, 885766Scarlsonj "zonepath", path_copy); 8860Sstevel@tonic-gate free(ze); 8870Sstevel@tonic-gate continue; 8880Sstevel@tonic-gate } 8890Sstevel@tonic-gate rpath[res] = '\0'; 8900Sstevel@tonic-gate (void) snprintf(path_copy, sizeof (path_copy), "%s/", path); 8910Sstevel@tonic-gate (void) snprintf(rpath_copy, sizeof (rpath_copy), "%s/", rpath); 8920Sstevel@tonic-gate if (strncmp(path_copy, rpath_copy, 8930Sstevel@tonic-gate min(strlen(path_copy), strlen(rpath_copy))) == 0) { 894924Sgjelinek /* 895924Sgjelinek * TRANSLATION_NOTE 896924Sgjelinek * zonepath is a literal that should not be translated. 897924Sgjelinek */ 8980Sstevel@tonic-gate (void) fprintf(stderr, gettext("%s zonepath (%s) and " 8990Sstevel@tonic-gate "%s zonepath (%s) overlap.\n"), 9000Sstevel@tonic-gate target_zone, path, ze->zone_name, rpath); 9010Sstevel@tonic-gate free(ze); 9020Sstevel@tonic-gate return (Z_ERR); 9030Sstevel@tonic-gate } 9040Sstevel@tonic-gate free(ze); 9050Sstevel@tonic-gate } 9060Sstevel@tonic-gate endzoneent(cookie); 9070Sstevel@tonic-gate return (Z_OK); 9080Sstevel@tonic-gate } 9090Sstevel@tonic-gate 9100Sstevel@tonic-gate static int 9110Sstevel@tonic-gate validate_zonepath(char *path, int cmd_num) 9120Sstevel@tonic-gate { 9130Sstevel@tonic-gate int res; /* result of last library/system call */ 9140Sstevel@tonic-gate boolean_t err = B_FALSE; /* have we run into an error? */ 9150Sstevel@tonic-gate struct stat stbuf; 9162267Sdp struct statvfs64 vfsbuf; 9170Sstevel@tonic-gate char rpath[MAXPATHLEN]; /* resolved path */ 9180Sstevel@tonic-gate char ppath[MAXPATHLEN]; /* parent path */ 9190Sstevel@tonic-gate char rppath[MAXPATHLEN]; /* resolved parent path */ 9200Sstevel@tonic-gate char rootpath[MAXPATHLEN]; /* root path */ 9210Sstevel@tonic-gate zone_state_t state; 9220Sstevel@tonic-gate 9230Sstevel@tonic-gate if (path[0] != '/') { 9240Sstevel@tonic-gate (void) fprintf(stderr, 9250Sstevel@tonic-gate gettext("%s is not an absolute path.\n"), path); 9260Sstevel@tonic-gate return (Z_ERR); 9270Sstevel@tonic-gate } 9280Sstevel@tonic-gate if ((res = resolvepath(path, rpath, sizeof (rpath))) == -1) { 9290Sstevel@tonic-gate if ((errno != ENOENT) || 9301300Sgjelinek (cmd_num != CMD_VERIFY && cmd_num != CMD_INSTALL && 9311300Sgjelinek cmd_num != CMD_CLONE && cmd_num != CMD_MOVE)) { 9320Sstevel@tonic-gate zperror(path, B_FALSE); 9330Sstevel@tonic-gate return (Z_ERR); 9340Sstevel@tonic-gate } 9350Sstevel@tonic-gate if (cmd_num == CMD_VERIFY) { 936924Sgjelinek /* 937924Sgjelinek * TRANSLATION_NOTE 938924Sgjelinek * zoneadm is a literal that should not be translated. 939924Sgjelinek */ 9400Sstevel@tonic-gate (void) fprintf(stderr, gettext("WARNING: %s does not " 941924Sgjelinek "exist, so it could not be verified.\nWhen " 942924Sgjelinek "'zoneadm %s' is run, '%s' will try to create\n%s, " 943924Sgjelinek "and '%s' will be tried again,\nbut the '%s' may " 944924Sgjelinek "fail if:\nthe parent directory of %s is group- or " 945924Sgjelinek "other-writable\nor\n%s overlaps with any other " 9460Sstevel@tonic-gate "installed zones.\n"), path, 9470Sstevel@tonic-gate cmd_to_str(CMD_INSTALL), cmd_to_str(CMD_INSTALL), 9480Sstevel@tonic-gate path, cmd_to_str(CMD_VERIFY), 9490Sstevel@tonic-gate cmd_to_str(CMD_VERIFY), path, path); 9500Sstevel@tonic-gate return (Z_OK); 9510Sstevel@tonic-gate } 9520Sstevel@tonic-gate /* 9530Sstevel@tonic-gate * The zonepath is supposed to be mode 700 but its 9540Sstevel@tonic-gate * parent(s) 755. So use 755 on the mkdirp() then 9550Sstevel@tonic-gate * chmod() the zonepath itself to 700. 9560Sstevel@tonic-gate */ 9570Sstevel@tonic-gate if (mkdirp(path, DEFAULT_DIR_MODE) < 0) { 9580Sstevel@tonic-gate zperror(path, B_FALSE); 9590Sstevel@tonic-gate return (Z_ERR); 9600Sstevel@tonic-gate } 9610Sstevel@tonic-gate /* 9620Sstevel@tonic-gate * If the chmod() fails, report the error, but might 9630Sstevel@tonic-gate * as well continue the verify procedure. 9640Sstevel@tonic-gate */ 9650Sstevel@tonic-gate if (chmod(path, S_IRWXU) != 0) 9660Sstevel@tonic-gate zperror(path, B_FALSE); 9670Sstevel@tonic-gate /* 9680Sstevel@tonic-gate * Since the mkdir() succeeded, we should not have to 9690Sstevel@tonic-gate * worry about a subsequent ENOENT, thus this should 9700Sstevel@tonic-gate * only recurse once. 9710Sstevel@tonic-gate */ 9721300Sgjelinek return (validate_zonepath(path, cmd_num)); 9730Sstevel@tonic-gate } 9740Sstevel@tonic-gate rpath[res] = '\0'; 9750Sstevel@tonic-gate if (strcmp(path, rpath) != 0) { 9760Sstevel@tonic-gate errno = Z_RESOLVED_PATH; 9770Sstevel@tonic-gate zperror(path, B_TRUE); 9780Sstevel@tonic-gate return (Z_ERR); 9790Sstevel@tonic-gate } 9800Sstevel@tonic-gate if ((res = stat(rpath, &stbuf)) != 0) { 9810Sstevel@tonic-gate zperror(rpath, B_FALSE); 9820Sstevel@tonic-gate return (Z_ERR); 9830Sstevel@tonic-gate } 9840Sstevel@tonic-gate if (!S_ISDIR(stbuf.st_mode)) { 9850Sstevel@tonic-gate (void) fprintf(stderr, gettext("%s is not a directory.\n"), 9860Sstevel@tonic-gate rpath); 9870Sstevel@tonic-gate return (Z_ERR); 9880Sstevel@tonic-gate } 9893445Sblakej if (strcmp(stbuf.st_fstype, MNTTYPE_TMPFS) == 0) { 9900Sstevel@tonic-gate (void) printf(gettext("WARNING: %s is on a temporary " 9911867Sgjelinek "file system.\n"), rpath); 9920Sstevel@tonic-gate } 9930Sstevel@tonic-gate if (crosscheck_zonepaths(rpath) != Z_OK) 9940Sstevel@tonic-gate return (Z_ERR); 9950Sstevel@tonic-gate /* 9960Sstevel@tonic-gate * Try to collect and report as many minor errors as possible 9970Sstevel@tonic-gate * before returning, so the user can learn everything that needs 9980Sstevel@tonic-gate * to be fixed up front. 9990Sstevel@tonic-gate */ 10000Sstevel@tonic-gate if (stbuf.st_uid != 0) { 10010Sstevel@tonic-gate (void) fprintf(stderr, gettext("%s is not owned by root.\n"), 10020Sstevel@tonic-gate rpath); 10030Sstevel@tonic-gate err = B_TRUE; 10040Sstevel@tonic-gate } 10050Sstevel@tonic-gate err |= bad_mode_bit(stbuf.st_mode, S_IRUSR, B_TRUE, rpath); 10060Sstevel@tonic-gate err |= bad_mode_bit(stbuf.st_mode, S_IWUSR, B_TRUE, rpath); 10070Sstevel@tonic-gate err |= bad_mode_bit(stbuf.st_mode, S_IXUSR, B_TRUE, rpath); 10080Sstevel@tonic-gate err |= bad_mode_bit(stbuf.st_mode, S_IRGRP, B_FALSE, rpath); 10090Sstevel@tonic-gate err |= bad_mode_bit(stbuf.st_mode, S_IWGRP, B_FALSE, rpath); 10100Sstevel@tonic-gate err |= bad_mode_bit(stbuf.st_mode, S_IXGRP, B_FALSE, rpath); 10110Sstevel@tonic-gate err |= bad_mode_bit(stbuf.st_mode, S_IROTH, B_FALSE, rpath); 10120Sstevel@tonic-gate err |= bad_mode_bit(stbuf.st_mode, S_IWOTH, B_FALSE, rpath); 10130Sstevel@tonic-gate err |= bad_mode_bit(stbuf.st_mode, S_IXOTH, B_FALSE, rpath); 10140Sstevel@tonic-gate 10150Sstevel@tonic-gate (void) snprintf(ppath, sizeof (ppath), "%s/..", path); 10160Sstevel@tonic-gate if ((res = resolvepath(ppath, rppath, sizeof (rppath))) == -1) { 10170Sstevel@tonic-gate zperror(ppath, B_FALSE); 10180Sstevel@tonic-gate return (Z_ERR); 10190Sstevel@tonic-gate } 10200Sstevel@tonic-gate rppath[res] = '\0'; 10210Sstevel@tonic-gate if ((res = stat(rppath, &stbuf)) != 0) { 10220Sstevel@tonic-gate zperror(rppath, B_FALSE); 10230Sstevel@tonic-gate return (Z_ERR); 10240Sstevel@tonic-gate } 10250Sstevel@tonic-gate /* theoretically impossible */ 10260Sstevel@tonic-gate if (!S_ISDIR(stbuf.st_mode)) { 10270Sstevel@tonic-gate (void) fprintf(stderr, gettext("%s is not a directory.\n"), 10280Sstevel@tonic-gate rppath); 10290Sstevel@tonic-gate return (Z_ERR); 10300Sstevel@tonic-gate } 10310Sstevel@tonic-gate if (stbuf.st_uid != 0) { 10320Sstevel@tonic-gate (void) fprintf(stderr, gettext("%s is not owned by root.\n"), 10330Sstevel@tonic-gate rppath); 10340Sstevel@tonic-gate err = B_TRUE; 10350Sstevel@tonic-gate } 10360Sstevel@tonic-gate err |= bad_mode_bit(stbuf.st_mode, S_IRUSR, B_TRUE, rppath); 10370Sstevel@tonic-gate err |= bad_mode_bit(stbuf.st_mode, S_IWUSR, B_TRUE, rppath); 10380Sstevel@tonic-gate err |= bad_mode_bit(stbuf.st_mode, S_IXUSR, B_TRUE, rppath); 10390Sstevel@tonic-gate err |= bad_mode_bit(stbuf.st_mode, S_IWGRP, B_FALSE, rppath); 10400Sstevel@tonic-gate err |= bad_mode_bit(stbuf.st_mode, S_IWOTH, B_FALSE, rppath); 10410Sstevel@tonic-gate if (strcmp(rpath, rppath) == 0) { 10420Sstevel@tonic-gate (void) fprintf(stderr, gettext("%s is its own parent.\n"), 10430Sstevel@tonic-gate rppath); 10440Sstevel@tonic-gate err = B_TRUE; 10450Sstevel@tonic-gate } 10460Sstevel@tonic-gate 10472267Sdp if (statvfs64(rpath, &vfsbuf) != 0) { 10480Sstevel@tonic-gate zperror(rpath, B_FALSE); 10490Sstevel@tonic-gate return (Z_ERR); 10500Sstevel@tonic-gate } 1051823Sgjelinek if (strcmp(vfsbuf.f_basetype, MNTTYPE_NFS) == 0) { 1052924Sgjelinek /* 1053924Sgjelinek * TRANSLATION_NOTE 1054924Sgjelinek * Zonepath and NFS are literals that should not be translated. 1055924Sgjelinek */ 1056924Sgjelinek (void) fprintf(stderr, gettext("Zonepath %s is on an NFS " 10571867Sgjelinek "mounted file system.\n" 10581867Sgjelinek "\tA local file system must be used.\n"), rpath); 1059823Sgjelinek return (Z_ERR); 1060823Sgjelinek } 1061823Sgjelinek if (vfsbuf.f_flag & ST_NOSUID) { 1062924Sgjelinek /* 1063924Sgjelinek * TRANSLATION_NOTE 1064924Sgjelinek * Zonepath and nosuid are literals that should not be 1065924Sgjelinek * translated. 1066924Sgjelinek */ 1067823Sgjelinek (void) fprintf(stderr, gettext("Zonepath %s is on a nosuid " 10681867Sgjelinek "file system.\n"), rpath); 10690Sstevel@tonic-gate return (Z_ERR); 10700Sstevel@tonic-gate } 10710Sstevel@tonic-gate 10720Sstevel@tonic-gate if ((res = zone_get_state(target_zone, &state)) != Z_OK) { 10730Sstevel@tonic-gate errno = res; 10740Sstevel@tonic-gate zperror2(target_zone, gettext("could not get state")); 10750Sstevel@tonic-gate return (Z_ERR); 10760Sstevel@tonic-gate } 10770Sstevel@tonic-gate /* 10780Sstevel@tonic-gate * The existence of the root path is only bad in the configured state, 10790Sstevel@tonic-gate * as it is *supposed* to be there at the installed and later states. 10801507Sgjelinek * However, the root path is expected to be there if the zone is 10811507Sgjelinek * detached. 10820Sstevel@tonic-gate * State/command mismatches are caught earlier in verify_details(). 10830Sstevel@tonic-gate */ 10841507Sgjelinek if (state == ZONE_STATE_CONFIGURED && cmd_num != CMD_ATTACH) { 10850Sstevel@tonic-gate if (snprintf(rootpath, sizeof (rootpath), "%s/root", rpath) >= 10860Sstevel@tonic-gate sizeof (rootpath)) { 1087924Sgjelinek /* 1088924Sgjelinek * TRANSLATION_NOTE 1089924Sgjelinek * Zonepath is a literal that should not be translated. 1090924Sgjelinek */ 10910Sstevel@tonic-gate (void) fprintf(stderr, 10920Sstevel@tonic-gate gettext("Zonepath %s is too long.\n"), rpath); 10930Sstevel@tonic-gate return (Z_ERR); 10940Sstevel@tonic-gate } 10950Sstevel@tonic-gate if ((res = stat(rootpath, &stbuf)) == 0) { 10961507Sgjelinek if (zonecfg_detached(rpath)) 10971507Sgjelinek (void) fprintf(stderr, 10981507Sgjelinek gettext("Cannot %s detached " 10991507Sgjelinek "zone.\nUse attach or remove %s " 11001507Sgjelinek "directory.\n"), cmd_to_str(cmd_num), 11011507Sgjelinek rpath); 11021507Sgjelinek else 11031507Sgjelinek (void) fprintf(stderr, 11041507Sgjelinek gettext("Rootpath %s exists; " 11051507Sgjelinek "remove or move aside prior to %s.\n"), 11061507Sgjelinek rootpath, cmd_to_str(cmd_num)); 11070Sstevel@tonic-gate return (Z_ERR); 11080Sstevel@tonic-gate } 11090Sstevel@tonic-gate } 11100Sstevel@tonic-gate 11110Sstevel@tonic-gate return (err ? Z_ERR : Z_OK); 11120Sstevel@tonic-gate } 11130Sstevel@tonic-gate 11142712Snn35248 /* 11152712Snn35248 * The following two routines implement a simple locking mechanism to 11162712Snn35248 * ensure that only one instance of zoneadm at a time is able to manipulate 11172712Snn35248 * a given zone. The lock is built on top of an fcntl(2) lock of 11182712Snn35248 * [<altroot>]/var/run/zones/<zonename>.zoneadm.lock. If a zoneadm instance 11192712Snn35248 * can grab that lock, it is allowed to manipulate the zone. 11202712Snn35248 * 11212712Snn35248 * Since zoneadm may call external applications which in turn invoke 11222712Snn35248 * zoneadm again, we introduce the notion of "lock inheritance". Any 11232712Snn35248 * instance of zoneadm that has another instance in its ancestry is assumed 11242712Snn35248 * to be acting on behalf of the original zoneadm, and is thus allowed to 11252712Snn35248 * manipulate its zone. 11262712Snn35248 * 11272712Snn35248 * This inheritance is implemented via the _ZONEADM_LOCK_HELD environment 11282712Snn35248 * variable. When zoneadm is granted a lock on its zone, this environment 11292712Snn35248 * variable is set to 1. When it releases the lock, the variable is set to 11302712Snn35248 * 0. Since a child process inherits its parent's environment, checking 11312712Snn35248 * the state of this variable indicates whether or not any ancestor owns 11322712Snn35248 * the lock. 11332712Snn35248 */ 11340Sstevel@tonic-gate static void 11350Sstevel@tonic-gate release_lock_file(int lockfd) 11360Sstevel@tonic-gate { 11372712Snn35248 /* 11382712Snn35248 * If we are cleaning up from a failed attempt to lock the zone for 11392712Snn35248 * the first time, we might have a zone_lock_cnt of 0. In that 11402712Snn35248 * error case, we don't want to do anything but close the lock 11412712Snn35248 * file. 11422712Snn35248 */ 11432712Snn35248 assert(zone_lock_cnt >= 0); 11442712Snn35248 if (zone_lock_cnt > 0) { 11452712Snn35248 assert(getenv(LOCK_ENV_VAR) != NULL); 11462712Snn35248 assert(atoi(getenv(LOCK_ENV_VAR)) == 1); 11472712Snn35248 if (--zone_lock_cnt > 0) { 11482712Snn35248 assert(lockfd == -1); 11492712Snn35248 return; 11502712Snn35248 } 11512712Snn35248 if (putenv(zoneadm_lock_not_held) != 0) { 11522712Snn35248 zperror(target_zone, B_TRUE); 11532712Snn35248 exit(Z_ERR); 11542712Snn35248 } 11552712Snn35248 } 11562712Snn35248 assert(lockfd >= 0); 11570Sstevel@tonic-gate (void) close(lockfd); 11580Sstevel@tonic-gate } 11590Sstevel@tonic-gate 11600Sstevel@tonic-gate static int 11610Sstevel@tonic-gate grab_lock_file(const char *zone_name, int *lockfd) 11620Sstevel@tonic-gate { 11630Sstevel@tonic-gate char pathbuf[PATH_MAX]; 11640Sstevel@tonic-gate struct flock flock; 11650Sstevel@tonic-gate 11662712Snn35248 /* 11672712Snn35248 * If we already have the lock, we can skip this expensive song 11682712Snn35248 * and dance. 11692712Snn35248 */ 11702712Snn35248 if (zone_lock_cnt > 0) { 11712712Snn35248 zone_lock_cnt++; 11722712Snn35248 *lockfd = -1; 11732712Snn35248 return (Z_OK); 11742712Snn35248 } 11752712Snn35248 assert(getenv(LOCK_ENV_VAR) != NULL); 11762712Snn35248 assert(atoi(getenv(LOCK_ENV_VAR)) == 0); 11772712Snn35248 1178766Scarlsonj if (snprintf(pathbuf, sizeof (pathbuf), "%s%s", zonecfg_get_root(), 1179766Scarlsonj ZONES_TMPDIR) >= sizeof (pathbuf)) { 1180766Scarlsonj zerror(gettext("alternate root path is too long")); 1181766Scarlsonj return (Z_ERR); 1182766Scarlsonj } 1183766Scarlsonj if (mkdir(pathbuf, S_IRWXU) < 0 && errno != EEXIST) { 1184766Scarlsonj zerror(gettext("could not mkdir %s: %s"), pathbuf, 11850Sstevel@tonic-gate strerror(errno)); 11860Sstevel@tonic-gate return (Z_ERR); 11870Sstevel@tonic-gate } 1188766Scarlsonj (void) chmod(pathbuf, S_IRWXU); 11890Sstevel@tonic-gate 11900Sstevel@tonic-gate /* 11910Sstevel@tonic-gate * One of these lock files is created for each zone (when needed). 11920Sstevel@tonic-gate * The lock files are not cleaned up (except on system reboot), 11930Sstevel@tonic-gate * but since there is only one per zone, there is no resource 11940Sstevel@tonic-gate * starvation issue. 11950Sstevel@tonic-gate */ 1196766Scarlsonj if (snprintf(pathbuf, sizeof (pathbuf), "%s%s/%s.zoneadm.lock", 1197766Scarlsonj zonecfg_get_root(), ZONES_TMPDIR, zone_name) >= sizeof (pathbuf)) { 1198766Scarlsonj zerror(gettext("alternate root path is too long")); 1199766Scarlsonj return (Z_ERR); 1200766Scarlsonj } 12010Sstevel@tonic-gate if ((*lockfd = open(pathbuf, O_RDWR|O_CREAT, S_IRUSR|S_IWUSR)) < 0) { 12020Sstevel@tonic-gate zerror(gettext("could not open %s: %s"), pathbuf, 12030Sstevel@tonic-gate strerror(errno)); 12040Sstevel@tonic-gate return (Z_ERR); 12050Sstevel@tonic-gate } 12060Sstevel@tonic-gate /* 12070Sstevel@tonic-gate * Lock the file to synchronize with other zoneadmds 12080Sstevel@tonic-gate */ 12090Sstevel@tonic-gate flock.l_type = F_WRLCK; 12100Sstevel@tonic-gate flock.l_whence = SEEK_SET; 12110Sstevel@tonic-gate flock.l_start = (off_t)0; 12120Sstevel@tonic-gate flock.l_len = (off_t)0; 12132712Snn35248 if ((fcntl(*lockfd, F_SETLKW, &flock) < 0) || 12142712Snn35248 (putenv(zoneadm_lock_held) != 0)) { 12150Sstevel@tonic-gate zerror(gettext("unable to lock %s: %s"), pathbuf, 12160Sstevel@tonic-gate strerror(errno)); 12170Sstevel@tonic-gate release_lock_file(*lockfd); 12180Sstevel@tonic-gate return (Z_ERR); 12190Sstevel@tonic-gate } 12202712Snn35248 zone_lock_cnt = 1; 12210Sstevel@tonic-gate return (Z_OK); 12220Sstevel@tonic-gate } 12230Sstevel@tonic-gate 1224766Scarlsonj static boolean_t 12250Sstevel@tonic-gate get_doorname(const char *zone_name, char *buffer) 12260Sstevel@tonic-gate { 1227766Scarlsonj return (snprintf(buffer, PATH_MAX, "%s" ZONE_DOOR_PATH, 1228766Scarlsonj zonecfg_get_root(), zone_name) < PATH_MAX); 12290Sstevel@tonic-gate } 12300Sstevel@tonic-gate 12310Sstevel@tonic-gate /* 12320Sstevel@tonic-gate * system daemons are not audited. For the global zone, this occurs 12330Sstevel@tonic-gate * "naturally" since init is started with the default audit 12340Sstevel@tonic-gate * characteristics. Since zoneadmd is a system daemon and it starts 12350Sstevel@tonic-gate * init for a zone, it is necessary to clear out the audit 12360Sstevel@tonic-gate * characteristics inherited from whomever started zoneadmd. This is 12370Sstevel@tonic-gate * indicated by the audit id, which is set from the ruid parameter of 12380Sstevel@tonic-gate * adt_set_user(), below. 12390Sstevel@tonic-gate */ 12400Sstevel@tonic-gate 12410Sstevel@tonic-gate static void 12420Sstevel@tonic-gate prepare_audit_context() 12430Sstevel@tonic-gate { 12440Sstevel@tonic-gate adt_session_data_t *ah; 12450Sstevel@tonic-gate char *failure = gettext("audit failure: %s"); 12460Sstevel@tonic-gate 12470Sstevel@tonic-gate if (adt_start_session(&ah, NULL, 0)) { 12480Sstevel@tonic-gate zerror(failure, strerror(errno)); 12490Sstevel@tonic-gate return; 12500Sstevel@tonic-gate } 12510Sstevel@tonic-gate if (adt_set_user(ah, ADT_NO_AUDIT, ADT_NO_AUDIT, 12520Sstevel@tonic-gate ADT_NO_AUDIT, ADT_NO_AUDIT, NULL, ADT_NEW)) { 12530Sstevel@tonic-gate zerror(failure, strerror(errno)); 12540Sstevel@tonic-gate (void) adt_end_session(ah); 12550Sstevel@tonic-gate return; 12560Sstevel@tonic-gate } 12570Sstevel@tonic-gate if (adt_set_proc(ah)) 12580Sstevel@tonic-gate zerror(failure, strerror(errno)); 12590Sstevel@tonic-gate 12600Sstevel@tonic-gate (void) adt_end_session(ah); 12610Sstevel@tonic-gate } 12620Sstevel@tonic-gate 12630Sstevel@tonic-gate static int 12640Sstevel@tonic-gate start_zoneadmd(const char *zone_name) 12650Sstevel@tonic-gate { 12660Sstevel@tonic-gate char doorpath[PATH_MAX]; 12670Sstevel@tonic-gate pid_t child_pid; 12680Sstevel@tonic-gate int error = Z_ERR; 12690Sstevel@tonic-gate int doorfd, lockfd; 12700Sstevel@tonic-gate struct door_info info; 12710Sstevel@tonic-gate 1272766Scarlsonj if (!get_doorname(zone_name, doorpath)) 1273766Scarlsonj return (Z_ERR); 12740Sstevel@tonic-gate 12750Sstevel@tonic-gate if (grab_lock_file(zone_name, &lockfd) != Z_OK) 12760Sstevel@tonic-gate return (Z_ERR); 12770Sstevel@tonic-gate 12780Sstevel@tonic-gate /* 12790Sstevel@tonic-gate * Now that we have the lock, re-confirm that the daemon is 12800Sstevel@tonic-gate * *not* up and working fine. If it is still down, we have a green 12810Sstevel@tonic-gate * light to start it. 12820Sstevel@tonic-gate */ 12830Sstevel@tonic-gate if ((doorfd = open(doorpath, O_RDONLY)) < 0) { 12840Sstevel@tonic-gate if (errno != ENOENT) { 12850Sstevel@tonic-gate zperror(doorpath, B_FALSE); 12860Sstevel@tonic-gate goto out; 12870Sstevel@tonic-gate } 12880Sstevel@tonic-gate } else { 12890Sstevel@tonic-gate if (door_info(doorfd, &info) == 0 && 12900Sstevel@tonic-gate ((info.di_attributes & DOOR_REVOKED) == 0)) { 12910Sstevel@tonic-gate error = Z_OK; 12920Sstevel@tonic-gate (void) close(doorfd); 12930Sstevel@tonic-gate goto out; 12940Sstevel@tonic-gate } 12950Sstevel@tonic-gate (void) close(doorfd); 12960Sstevel@tonic-gate } 12970Sstevel@tonic-gate 12980Sstevel@tonic-gate if ((child_pid = fork()) == -1) { 12990Sstevel@tonic-gate zperror(gettext("could not fork"), B_FALSE); 13000Sstevel@tonic-gate goto out; 13010Sstevel@tonic-gate } else if (child_pid == 0) { 1302766Scarlsonj const char *argv[6], **ap; 1303766Scarlsonj 13040Sstevel@tonic-gate /* child process */ 13050Sstevel@tonic-gate prepare_audit_context(); 13060Sstevel@tonic-gate 1307766Scarlsonj ap = argv; 1308766Scarlsonj *ap++ = "zoneadmd"; 1309766Scarlsonj *ap++ = "-z"; 1310766Scarlsonj *ap++ = zone_name; 1311766Scarlsonj if (zonecfg_in_alt_root()) { 1312766Scarlsonj *ap++ = "-R"; 1313766Scarlsonj *ap++ = zonecfg_get_root(); 1314766Scarlsonj } 1315766Scarlsonj *ap = NULL; 1316766Scarlsonj 1317766Scarlsonj (void) execv("/usr/lib/zones/zoneadmd", (char * const *)argv); 1318924Sgjelinek /* 1319924Sgjelinek * TRANSLATION_NOTE 1320924Sgjelinek * zoneadmd is a literal that should not be translated. 1321924Sgjelinek */ 13220Sstevel@tonic-gate zperror(gettext("could not exec zoneadmd"), B_FALSE); 13230Sstevel@tonic-gate _exit(Z_ERR); 13240Sstevel@tonic-gate } else { 13250Sstevel@tonic-gate /* parent process */ 13260Sstevel@tonic-gate pid_t retval; 13270Sstevel@tonic-gate int pstatus = 0; 13280Sstevel@tonic-gate 13290Sstevel@tonic-gate do { 13300Sstevel@tonic-gate retval = waitpid(child_pid, &pstatus, 0); 13310Sstevel@tonic-gate } while (retval != child_pid); 13320Sstevel@tonic-gate if (WIFSIGNALED(pstatus) || (WIFEXITED(pstatus) && 13330Sstevel@tonic-gate WEXITSTATUS(pstatus) != 0)) { 13340Sstevel@tonic-gate zerror(gettext("could not start %s"), "zoneadmd"); 13350Sstevel@tonic-gate goto out; 13360Sstevel@tonic-gate } 13370Sstevel@tonic-gate } 13380Sstevel@tonic-gate error = Z_OK; 13390Sstevel@tonic-gate out: 13400Sstevel@tonic-gate release_lock_file(lockfd); 13410Sstevel@tonic-gate return (error); 13420Sstevel@tonic-gate } 13430Sstevel@tonic-gate 13440Sstevel@tonic-gate static int 13450Sstevel@tonic-gate ping_zoneadmd(const char *zone_name) 13460Sstevel@tonic-gate { 13470Sstevel@tonic-gate char doorpath[PATH_MAX]; 13480Sstevel@tonic-gate int doorfd; 13490Sstevel@tonic-gate struct door_info info; 13500Sstevel@tonic-gate 1351766Scarlsonj if (!get_doorname(zone_name, doorpath)) 1352766Scarlsonj return (Z_ERR); 13530Sstevel@tonic-gate 13540Sstevel@tonic-gate if ((doorfd = open(doorpath, O_RDONLY)) < 0) { 13550Sstevel@tonic-gate return (Z_ERR); 13560Sstevel@tonic-gate } 13570Sstevel@tonic-gate if (door_info(doorfd, &info) == 0 && 13580Sstevel@tonic-gate ((info.di_attributes & DOOR_REVOKED) == 0)) { 13590Sstevel@tonic-gate (void) close(doorfd); 13600Sstevel@tonic-gate return (Z_OK); 13610Sstevel@tonic-gate } 13620Sstevel@tonic-gate (void) close(doorfd); 13630Sstevel@tonic-gate return (Z_ERR); 13640Sstevel@tonic-gate } 13650Sstevel@tonic-gate 13660Sstevel@tonic-gate static int 13670Sstevel@tonic-gate call_zoneadmd(const char *zone_name, zone_cmd_arg_t *arg) 13680Sstevel@tonic-gate { 13690Sstevel@tonic-gate char doorpath[PATH_MAX]; 13700Sstevel@tonic-gate int doorfd, result; 13710Sstevel@tonic-gate door_arg_t darg; 13720Sstevel@tonic-gate 13730Sstevel@tonic-gate zoneid_t zoneid; 13740Sstevel@tonic-gate uint64_t uniqid = 0; 13750Sstevel@tonic-gate 13760Sstevel@tonic-gate zone_cmd_rval_t *rvalp; 13770Sstevel@tonic-gate size_t rlen; 13780Sstevel@tonic-gate char *cp, *errbuf; 13790Sstevel@tonic-gate 13800Sstevel@tonic-gate rlen = getpagesize(); 13810Sstevel@tonic-gate if ((rvalp = malloc(rlen)) == NULL) { 13820Sstevel@tonic-gate zerror(gettext("failed to allocate %lu bytes: %s"), rlen, 13830Sstevel@tonic-gate strerror(errno)); 13840Sstevel@tonic-gate return (-1); 13850Sstevel@tonic-gate } 13860Sstevel@tonic-gate 13870Sstevel@tonic-gate if ((zoneid = getzoneidbyname(zone_name)) != ZONE_ID_UNDEFINED) { 13880Sstevel@tonic-gate (void) zone_getattr(zoneid, ZONE_ATTR_UNIQID, &uniqid, 13890Sstevel@tonic-gate sizeof (uniqid)); 13900Sstevel@tonic-gate } 13910Sstevel@tonic-gate arg->uniqid = uniqid; 13920Sstevel@tonic-gate (void) strlcpy(arg->locale, locale, sizeof (arg->locale)); 1393766Scarlsonj if (!get_doorname(zone_name, doorpath)) { 1394766Scarlsonj zerror(gettext("alternate root path is too long")); 1395766Scarlsonj free(rvalp); 1396766Scarlsonj return (-1); 1397766Scarlsonj } 13980Sstevel@tonic-gate 13990Sstevel@tonic-gate /* 14000Sstevel@tonic-gate * Loop trying to start zoneadmd; if something goes seriously 14010Sstevel@tonic-gate * wrong we break out and fail. 14020Sstevel@tonic-gate */ 14030Sstevel@tonic-gate for (;;) { 14040Sstevel@tonic-gate if (start_zoneadmd(zone_name) != Z_OK) 14050Sstevel@tonic-gate break; 14060Sstevel@tonic-gate 14070Sstevel@tonic-gate if ((doorfd = open(doorpath, O_RDONLY)) < 0) { 14080Sstevel@tonic-gate zperror(gettext("failed to open zone door"), B_FALSE); 14090Sstevel@tonic-gate break; 14100Sstevel@tonic-gate } 14110Sstevel@tonic-gate 14120Sstevel@tonic-gate darg.data_ptr = (char *)arg; 14130Sstevel@tonic-gate darg.data_size = sizeof (*arg); 14140Sstevel@tonic-gate darg.desc_ptr = NULL; 14150Sstevel@tonic-gate darg.desc_num = 0; 14160Sstevel@tonic-gate darg.rbuf = (char *)rvalp; 14170Sstevel@tonic-gate darg.rsize = rlen; 14180Sstevel@tonic-gate if (door_call(doorfd, &darg) != 0) { 14190Sstevel@tonic-gate (void) close(doorfd); 14200Sstevel@tonic-gate /* 14210Sstevel@tonic-gate * We'll get EBADF if the door has been revoked. 14220Sstevel@tonic-gate */ 14230Sstevel@tonic-gate if (errno != EBADF) { 14240Sstevel@tonic-gate zperror(gettext("door_call failed"), B_FALSE); 14250Sstevel@tonic-gate break; 14260Sstevel@tonic-gate } 14270Sstevel@tonic-gate continue; /* take another lap */ 14280Sstevel@tonic-gate } 14290Sstevel@tonic-gate (void) close(doorfd); 14300Sstevel@tonic-gate 14310Sstevel@tonic-gate if (darg.data_size == 0) { 14320Sstevel@tonic-gate /* Door server is going away; kick it again. */ 14330Sstevel@tonic-gate continue; 14340Sstevel@tonic-gate } 14350Sstevel@tonic-gate 14360Sstevel@tonic-gate errbuf = rvalp->errbuf; 14370Sstevel@tonic-gate while (*errbuf != '\0') { 14380Sstevel@tonic-gate /* 14390Sstevel@tonic-gate * Remove any newlines since zerror() 14400Sstevel@tonic-gate * will append one automatically. 14410Sstevel@tonic-gate */ 14420Sstevel@tonic-gate cp = strchr(errbuf, '\n'); 14430Sstevel@tonic-gate if (cp != NULL) 14440Sstevel@tonic-gate *cp = '\0'; 14450Sstevel@tonic-gate zerror("%s", errbuf); 14460Sstevel@tonic-gate if (cp == NULL) 14470Sstevel@tonic-gate break; 14480Sstevel@tonic-gate errbuf = cp + 1; 14490Sstevel@tonic-gate } 14500Sstevel@tonic-gate result = rvalp->rval == 0 ? 0 : -1; 14510Sstevel@tonic-gate free(rvalp); 14520Sstevel@tonic-gate return (result); 14530Sstevel@tonic-gate } 14540Sstevel@tonic-gate 14550Sstevel@tonic-gate free(rvalp); 14560Sstevel@tonic-gate return (-1); 14570Sstevel@tonic-gate } 14580Sstevel@tonic-gate 14590Sstevel@tonic-gate static int 14603339Szt129084 invoke_brand_handler(int cmd_num, char *argv[]) 14613339Szt129084 { 14623339Szt129084 zone_dochandle_t handle; 14633339Szt129084 int err; 14643339Szt129084 14653339Szt129084 if ((handle = zonecfg_init_handle()) == NULL) { 14663339Szt129084 zperror(cmd_to_str(cmd_num), B_TRUE); 14673339Szt129084 return (Z_ERR); 14683339Szt129084 } 14693339Szt129084 if ((err = zonecfg_get_handle(target_zone, handle)) != Z_OK) { 14703339Szt129084 errno = err; 14713339Szt129084 zperror(cmd_to_str(cmd_num), B_TRUE); 14723339Szt129084 zonecfg_fini_handle(handle); 14733339Szt129084 return (Z_ERR); 14743339Szt129084 } 14753339Szt129084 if (verify_brand(handle, cmd_num, argv) != Z_OK) { 14763339Szt129084 zonecfg_fini_handle(handle); 14773339Szt129084 return (Z_ERR); 14783339Szt129084 } 14793339Szt129084 zonecfg_fini_handle(handle); 14803339Szt129084 return (Z_OK); 14813339Szt129084 } 14823339Szt129084 14833339Szt129084 static int 14840Sstevel@tonic-gate ready_func(int argc, char *argv[]) 14850Sstevel@tonic-gate { 14860Sstevel@tonic-gate zone_cmd_arg_t zarg; 14870Sstevel@tonic-gate int arg; 14880Sstevel@tonic-gate 1489766Scarlsonj if (zonecfg_in_alt_root()) { 1490766Scarlsonj zerror(gettext("cannot ready zone in alternate root")); 1491766Scarlsonj return (Z_ERR); 1492766Scarlsonj } 1493766Scarlsonj 14940Sstevel@tonic-gate optind = 0; 14950Sstevel@tonic-gate if ((arg = getopt(argc, argv, "?")) != EOF) { 14960Sstevel@tonic-gate switch (arg) { 14970Sstevel@tonic-gate case '?': 14980Sstevel@tonic-gate sub_usage(SHELP_READY, CMD_READY); 14990Sstevel@tonic-gate return (optopt == '?' ? Z_OK : Z_USAGE); 15000Sstevel@tonic-gate default: 15010Sstevel@tonic-gate sub_usage(SHELP_READY, CMD_READY); 15020Sstevel@tonic-gate return (Z_USAGE); 15030Sstevel@tonic-gate } 15040Sstevel@tonic-gate } 15050Sstevel@tonic-gate if (argc > optind) { 15060Sstevel@tonic-gate sub_usage(SHELP_READY, CMD_READY); 15070Sstevel@tonic-gate return (Z_USAGE); 15080Sstevel@tonic-gate } 15092712Snn35248 if (sanity_check(target_zone, CMD_READY, B_FALSE, B_FALSE, B_FALSE) 15102712Snn35248 != Z_OK) 15110Sstevel@tonic-gate return (Z_ERR); 15123339Szt129084 if (verify_details(CMD_READY, argv) != Z_OK) 15130Sstevel@tonic-gate return (Z_ERR); 15140Sstevel@tonic-gate 15150Sstevel@tonic-gate zarg.cmd = Z_READY; 15160Sstevel@tonic-gate if (call_zoneadmd(target_zone, &zarg) != 0) { 15170Sstevel@tonic-gate zerror(gettext("call to %s failed"), "zoneadmd"); 15180Sstevel@tonic-gate return (Z_ERR); 15190Sstevel@tonic-gate } 15200Sstevel@tonic-gate return (Z_OK); 15210Sstevel@tonic-gate } 15220Sstevel@tonic-gate 15230Sstevel@tonic-gate static int 15240Sstevel@tonic-gate boot_func(int argc, char *argv[]) 15250Sstevel@tonic-gate { 15260Sstevel@tonic-gate zone_cmd_arg_t zarg; 15272712Snn35248 boolean_t force = B_FALSE; 15280Sstevel@tonic-gate int arg; 15290Sstevel@tonic-gate 1530766Scarlsonj if (zonecfg_in_alt_root()) { 1531766Scarlsonj zerror(gettext("cannot boot zone in alternate root")); 1532766Scarlsonj return (Z_ERR); 1533766Scarlsonj } 1534766Scarlsonj 15350Sstevel@tonic-gate zarg.bootbuf[0] = '\0'; 15360Sstevel@tonic-gate 15370Sstevel@tonic-gate /* 15382267Sdp * The following getopt processes arguments to zone boot; that 15392267Sdp * is to say, the [here] portion of the argument string: 15402267Sdp * 15412267Sdp * zoneadm -z myzone boot [here] -- -v -m verbose 15422267Sdp * 15432267Sdp * Where [here] can either be nothing, -? (in which case we bail 15442712Snn35248 * and print usage), -f (a private option to indicate that the 15452712Snn35248 * boot operation should be 'forced'), or -s. Support for -s is 15462712Snn35248 * vestigal and obsolete, but is retained because it was a 15472712Snn35248 * documented interface and there are known consumers including 15482712Snn35248 * admin/install; the proper way to specify boot arguments like -s 15492712Snn35248 * is: 15502267Sdp * 15512267Sdp * zoneadm -z myzone boot -- -s -v -m verbose. 15520Sstevel@tonic-gate */ 15530Sstevel@tonic-gate optind = 0; 15542712Snn35248 while ((arg = getopt(argc, argv, "?fs")) != EOF) { 15550Sstevel@tonic-gate switch (arg) { 15560Sstevel@tonic-gate case '?': 15570Sstevel@tonic-gate sub_usage(SHELP_BOOT, CMD_BOOT); 15580Sstevel@tonic-gate return (optopt == '?' ? Z_OK : Z_USAGE); 15590Sstevel@tonic-gate case 's': 15600Sstevel@tonic-gate (void) strlcpy(zarg.bootbuf, "-s", 15610Sstevel@tonic-gate sizeof (zarg.bootbuf)); 15620Sstevel@tonic-gate break; 15632712Snn35248 case 'f': 15642712Snn35248 force = B_TRUE; 15652712Snn35248 break; 15660Sstevel@tonic-gate default: 15670Sstevel@tonic-gate sub_usage(SHELP_BOOT, CMD_BOOT); 15680Sstevel@tonic-gate return (Z_USAGE); 15690Sstevel@tonic-gate } 15700Sstevel@tonic-gate } 15712267Sdp 15722267Sdp for (; optind < argc; optind++) { 15732267Sdp if (strlcat(zarg.bootbuf, argv[optind], 15742267Sdp sizeof (zarg.bootbuf)) >= sizeof (zarg.bootbuf)) { 15752267Sdp zerror(gettext("Boot argument list too long")); 15762267Sdp return (Z_ERR); 15772267Sdp } 15782267Sdp if (optind < argc - 1) 15792267Sdp if (strlcat(zarg.bootbuf, " ", sizeof (zarg.bootbuf)) >= 15802267Sdp sizeof (zarg.bootbuf)) { 15812267Sdp zerror(gettext("Boot argument list too long")); 15822267Sdp return (Z_ERR); 15832267Sdp } 15842267Sdp } 15852712Snn35248 if (sanity_check(target_zone, CMD_BOOT, B_FALSE, B_FALSE, force) 15862712Snn35248 != Z_OK) 15870Sstevel@tonic-gate return (Z_ERR); 15883339Szt129084 if (verify_details(CMD_BOOT, argv) != Z_OK) 15890Sstevel@tonic-gate return (Z_ERR); 15902712Snn35248 zarg.cmd = force ? Z_FORCEBOOT : Z_BOOT; 15910Sstevel@tonic-gate if (call_zoneadmd(target_zone, &zarg) != 0) { 15920Sstevel@tonic-gate zerror(gettext("call to %s failed"), "zoneadmd"); 15930Sstevel@tonic-gate return (Z_ERR); 15940Sstevel@tonic-gate } 15953247Sgjelinek 15960Sstevel@tonic-gate return (Z_OK); 15970Sstevel@tonic-gate } 15980Sstevel@tonic-gate 15990Sstevel@tonic-gate static void 16000Sstevel@tonic-gate fake_up_local_zone(zoneid_t zid, zone_entry_t *zeptr) 16010Sstevel@tonic-gate { 16020Sstevel@tonic-gate ssize_t result; 16032303Scarlsonj uuid_t uuid; 16042303Scarlsonj FILE *fp; 16053448Sdh155122 ushort_t flags; 16062303Scarlsonj 16072303Scarlsonj (void) memset(zeptr, 0, sizeof (*zeptr)); 16080Sstevel@tonic-gate 16090Sstevel@tonic-gate zeptr->zid = zid; 16102303Scarlsonj 16110Sstevel@tonic-gate /* 16120Sstevel@tonic-gate * Since we're looking up our own (non-global) zone name, 16130Sstevel@tonic-gate * we can be assured that it will succeed. 16140Sstevel@tonic-gate */ 16150Sstevel@tonic-gate result = getzonenamebyid(zid, zeptr->zname, sizeof (zeptr->zname)); 16160Sstevel@tonic-gate assert(result >= 0); 16172303Scarlsonj if (zonecfg_is_scratch(zeptr->zname) && 16182303Scarlsonj (fp = zonecfg_open_scratch("", B_FALSE)) != NULL) { 16192303Scarlsonj (void) zonecfg_reverse_scratch(fp, zeptr->zname, zeptr->zname, 16202303Scarlsonj sizeof (zeptr->zname), NULL, 0); 16212303Scarlsonj zonecfg_close_scratch(fp); 16222303Scarlsonj } 16232303Scarlsonj 16242303Scarlsonj if (is_system_labeled()) { 16251676Sjpk (void) zone_getattr(zid, ZONE_ATTR_ROOT, zeptr->zroot, 16261676Sjpk sizeof (zeptr->zroot)); 16272712Snn35248 (void) strlcpy(zeptr->zbrand, NATIVE_BRAND_NAME, 16282712Snn35248 sizeof (zeptr->zbrand)); 16292303Scarlsonj } else { 16302303Scarlsonj (void) strlcpy(zeptr->zroot, "/", sizeof (zeptr->zroot)); 16312712Snn35248 (void) zone_getattr(zid, ZONE_ATTR_BRAND, zeptr->zbrand, 16322712Snn35248 sizeof (zeptr->zbrand)); 16332303Scarlsonj } 16342303Scarlsonj 16350Sstevel@tonic-gate zeptr->zstate_str = "running"; 16362303Scarlsonj if (zonecfg_get_uuid(zeptr->zname, uuid) == Z_OK && 16372303Scarlsonj !uuid_is_null(uuid)) 16382303Scarlsonj uuid_unparse(uuid, zeptr->zuuid); 16393448Sdh155122 16403448Sdh155122 if (zone_getattr(zid, ZONE_ATTR_FLAGS, &flags, sizeof (flags)) < 0) { 16413448Sdh155122 zperror2(zeptr->zname, gettext("could not get zone flags")); 16423448Sdh155122 exit(Z_ERR); 16433448Sdh155122 } 16443448Sdh155122 if (flags & ZF_NET_EXCL) 16453448Sdh155122 zeptr->ziptype = ZS_EXCLUSIVE; 16463448Sdh155122 else 16473448Sdh155122 zeptr->ziptype = ZS_SHARED; 16480Sstevel@tonic-gate } 16490Sstevel@tonic-gate 16500Sstevel@tonic-gate static int 16510Sstevel@tonic-gate list_func(int argc, char *argv[]) 16520Sstevel@tonic-gate { 16530Sstevel@tonic-gate zone_entry_t *zentp, zent; 1654766Scarlsonj int arg, retv; 16550Sstevel@tonic-gate boolean_t output = B_FALSE, verbose = B_FALSE, parsable = B_FALSE; 16560Sstevel@tonic-gate zone_state_t min_state = ZONE_STATE_RUNNING; 16570Sstevel@tonic-gate zoneid_t zone_id = getzoneid(); 16580Sstevel@tonic-gate 16590Sstevel@tonic-gate if (target_zone == NULL) { 16600Sstevel@tonic-gate /* all zones: default view to running but allow override */ 16610Sstevel@tonic-gate optind = 0; 16620Sstevel@tonic-gate while ((arg = getopt(argc, argv, "?cipv")) != EOF) { 16630Sstevel@tonic-gate switch (arg) { 16640Sstevel@tonic-gate case '?': 16650Sstevel@tonic-gate sub_usage(SHELP_LIST, CMD_LIST); 16660Sstevel@tonic-gate return (optopt == '?' ? Z_OK : Z_USAGE); 16671339Sjonb /* 16681339Sjonb * The 'i' and 'c' options are not mutually 16691339Sjonb * exclusive so if 'c' is given, then min_state 16701339Sjonb * is set to 0 (ZONE_STATE_CONFIGURED) which is 16711339Sjonb * the lowest possible state. If 'i' is given, 16721339Sjonb * then min_state is set to be the lowest state 16731339Sjonb * so far. 16741339Sjonb */ 16750Sstevel@tonic-gate case 'c': 16760Sstevel@tonic-gate min_state = ZONE_STATE_CONFIGURED; 16770Sstevel@tonic-gate break; 16780Sstevel@tonic-gate case 'i': 16791339Sjonb min_state = min(ZONE_STATE_INSTALLED, 16801339Sjonb min_state); 16811339Sjonb 16820Sstevel@tonic-gate break; 16830Sstevel@tonic-gate case 'p': 16840Sstevel@tonic-gate parsable = B_TRUE; 16850Sstevel@tonic-gate break; 16860Sstevel@tonic-gate case 'v': 16870Sstevel@tonic-gate verbose = B_TRUE; 16880Sstevel@tonic-gate break; 16890Sstevel@tonic-gate default: 16900Sstevel@tonic-gate sub_usage(SHELP_LIST, CMD_LIST); 16910Sstevel@tonic-gate return (Z_USAGE); 16920Sstevel@tonic-gate } 16930Sstevel@tonic-gate } 16940Sstevel@tonic-gate if (parsable && verbose) { 16950Sstevel@tonic-gate zerror(gettext("%s -p and -v are mutually exclusive."), 16960Sstevel@tonic-gate cmd_to_str(CMD_LIST)); 16970Sstevel@tonic-gate return (Z_ERR); 16980Sstevel@tonic-gate } 16991676Sjpk if (zone_id == GLOBAL_ZONEID || is_system_labeled()) { 1700766Scarlsonj retv = zone_print_list(min_state, verbose, parsable); 17010Sstevel@tonic-gate } else { 17022712Snn35248 fake_up_local_zone(zone_id, &zent); 1703766Scarlsonj retv = Z_OK; 17040Sstevel@tonic-gate zone_print(&zent, verbose, parsable); 17050Sstevel@tonic-gate } 1706766Scarlsonj return (retv); 17070Sstevel@tonic-gate } 17080Sstevel@tonic-gate 17090Sstevel@tonic-gate /* 17100Sstevel@tonic-gate * Specific target zone: disallow -i/-c suboptions. 17110Sstevel@tonic-gate */ 17120Sstevel@tonic-gate optind = 0; 17130Sstevel@tonic-gate while ((arg = getopt(argc, argv, "?pv")) != EOF) { 17140Sstevel@tonic-gate switch (arg) { 17150Sstevel@tonic-gate case '?': 17160Sstevel@tonic-gate sub_usage(SHELP_LIST, CMD_LIST); 17170Sstevel@tonic-gate return (optopt == '?' ? Z_OK : Z_USAGE); 17180Sstevel@tonic-gate case 'p': 17190Sstevel@tonic-gate parsable = B_TRUE; 17200Sstevel@tonic-gate break; 17210Sstevel@tonic-gate case 'v': 17220Sstevel@tonic-gate verbose = B_TRUE; 17230Sstevel@tonic-gate break; 17240Sstevel@tonic-gate default: 17250Sstevel@tonic-gate sub_usage(SHELP_LIST, CMD_LIST); 17260Sstevel@tonic-gate return (Z_USAGE); 17270Sstevel@tonic-gate } 17280Sstevel@tonic-gate } 17290Sstevel@tonic-gate if (parsable && verbose) { 17300Sstevel@tonic-gate zerror(gettext("%s -p and -v are mutually exclusive."), 17310Sstevel@tonic-gate cmd_to_str(CMD_LIST)); 17320Sstevel@tonic-gate return (Z_ERR); 17330Sstevel@tonic-gate } 17340Sstevel@tonic-gate if (argc > optind) { 17350Sstevel@tonic-gate sub_usage(SHELP_LIST, CMD_LIST); 17360Sstevel@tonic-gate return (Z_USAGE); 17370Sstevel@tonic-gate } 17380Sstevel@tonic-gate if (zone_id != GLOBAL_ZONEID) { 17390Sstevel@tonic-gate fake_up_local_zone(zone_id, &zent); 17400Sstevel@tonic-gate /* 17410Sstevel@tonic-gate * main() will issue a Z_NO_ZONE error if it cannot get an 17420Sstevel@tonic-gate * id for target_zone, which in a non-global zone should 17430Sstevel@tonic-gate * happen for any zone name except `zonename`. Thus we 17440Sstevel@tonic-gate * assert() that here but don't otherwise check. 17450Sstevel@tonic-gate */ 17460Sstevel@tonic-gate assert(strcmp(zent.zname, target_zone) == 0); 17470Sstevel@tonic-gate zone_print(&zent, verbose, parsable); 17480Sstevel@tonic-gate output = B_TRUE; 17490Sstevel@tonic-gate } else if ((zentp = lookup_running_zone(target_zone)) != NULL) { 17500Sstevel@tonic-gate zone_print(zentp, verbose, parsable); 17510Sstevel@tonic-gate output = B_TRUE; 1752766Scarlsonj } else if (lookup_zone_info(target_zone, ZONE_ID_UNDEFINED, 1753766Scarlsonj &zent) == Z_OK) { 17540Sstevel@tonic-gate zone_print(&zent, verbose, parsable); 17550Sstevel@tonic-gate output = B_TRUE; 17560Sstevel@tonic-gate } 17573339Szt129084 17583339Szt129084 /* 17593339Szt129084 * Invoke brand-specific handler. Note that we do this 17603542Sgjelinek * only if we're in the global zone, and target_zone is specified 17613542Sgjelinek * and it is not the global zone. 17623339Szt129084 */ 17633542Sgjelinek if (zone_id == GLOBAL_ZONEID && target_zone != NULL && 17643542Sgjelinek strcmp(target_zone, GLOBAL_ZONENAME) != 0) 17653339Szt129084 if (invoke_brand_handler(CMD_LIST, argv) != Z_OK) 17663339Szt129084 return (Z_ERR); 17673339Szt129084 17680Sstevel@tonic-gate return (output ? Z_OK : Z_ERR); 17690Sstevel@tonic-gate } 17700Sstevel@tonic-gate 17710Sstevel@tonic-gate static void 17720Sstevel@tonic-gate sigterm(int sig) 17730Sstevel@tonic-gate { 17740Sstevel@tonic-gate /* 17750Sstevel@tonic-gate * Ignore SIG{INT,TERM}, so we don't end up in an infinite loop, 17760Sstevel@tonic-gate * then propagate the signal to our process group. 17770Sstevel@tonic-gate */ 17782712Snn35248 assert(sig == SIGINT || sig == SIGTERM); 17790Sstevel@tonic-gate (void) sigset(SIGINT, SIG_IGN); 17800Sstevel@tonic-gate (void) sigset(SIGTERM, SIG_IGN); 17810Sstevel@tonic-gate (void) kill(0, sig); 17820Sstevel@tonic-gate child_killed = B_TRUE; 17830Sstevel@tonic-gate } 17840Sstevel@tonic-gate 17850Sstevel@tonic-gate static int 17860Sstevel@tonic-gate do_subproc(char *cmdbuf) 17870Sstevel@tonic-gate { 17880Sstevel@tonic-gate char inbuf[1024]; /* arbitrary large amount */ 17890Sstevel@tonic-gate FILE *file; 17900Sstevel@tonic-gate 17912712Snn35248 do_subproc_cnt++; 17920Sstevel@tonic-gate child_killed = B_FALSE; 17930Sstevel@tonic-gate /* 17940Sstevel@tonic-gate * We use popen(3c) to launch child processes for [un]install; 17950Sstevel@tonic-gate * this library call does not return a PID, so we have to kill 17960Sstevel@tonic-gate * the whole process group. To avoid killing our parent, we 17970Sstevel@tonic-gate * become a process group leader here. But doing so can wreak 17980Sstevel@tonic-gate * havoc with reading from stdin when launched by a non-job-control 17990Sstevel@tonic-gate * shell, so we close stdin and reopen it as /dev/null first. 18000Sstevel@tonic-gate */ 18010Sstevel@tonic-gate (void) close(STDIN_FILENO); 18022712Snn35248 (void) openat(STDIN_FILENO, "/dev/null", O_RDONLY); 18032712Snn35248 if (!zoneadm_is_nested) 18042712Snn35248 (void) setpgid(0, 0); 18050Sstevel@tonic-gate (void) sigset(SIGINT, sigterm); 18060Sstevel@tonic-gate (void) sigset(SIGTERM, sigterm); 18070Sstevel@tonic-gate file = popen(cmdbuf, "r"); 18080Sstevel@tonic-gate for (;;) { 18090Sstevel@tonic-gate if (child_killed || fgets(inbuf, sizeof (inbuf), file) == NULL) 18100Sstevel@tonic-gate break; 18110Sstevel@tonic-gate (void) fputs(inbuf, stdout); 18120Sstevel@tonic-gate } 18130Sstevel@tonic-gate (void) sigset(SIGINT, SIG_DFL); 18140Sstevel@tonic-gate (void) sigset(SIGTERM, SIG_DFL); 18150Sstevel@tonic-gate return (pclose(file)); 18160Sstevel@tonic-gate } 18170Sstevel@tonic-gate 18180Sstevel@tonic-gate static int 18192712Snn35248 do_subproc_interactive(char *cmdbuf) 18202712Snn35248 { 18212712Snn35248 void (*saveint)(int); 18222712Snn35248 void (*saveterm)(int); 18232712Snn35248 void (*savequit)(int); 18242712Snn35248 void (*savehup)(int); 18252712Snn35248 int pid, child, status; 18262712Snn35248 18272712Snn35248 /* 18282712Snn35248 * do_subproc() links stdin to /dev/null, which would break any 18292712Snn35248 * interactive subprocess we try to launch here. Similarly, we 18302712Snn35248 * can't have been launched as a subprocess ourselves. 18312712Snn35248 */ 18322712Snn35248 assert(do_subproc_cnt == 0 && !zoneadm_is_nested); 18332712Snn35248 18342712Snn35248 if ((child = vfork()) == 0) { 18352712Snn35248 (void) execl("/bin/sh", "sh", "-c", cmdbuf, (char *)NULL); 18362712Snn35248 } 18372712Snn35248 18382712Snn35248 if (child == -1) 18392712Snn35248 return (-1); 18402712Snn35248 18412712Snn35248 saveint = sigset(SIGINT, SIG_IGN); 18422712Snn35248 saveterm = sigset(SIGTERM, SIG_IGN); 18432712Snn35248 savequit = sigset(SIGQUIT, SIG_IGN); 18442712Snn35248 savehup = sigset(SIGHUP, SIG_IGN); 18452712Snn35248 18462712Snn35248 while ((pid = waitpid(child, &status, 0)) != child && pid != -1) 18472712Snn35248 ; 18482712Snn35248 18492712Snn35248 (void) sigset(SIGINT, saveint); 18502712Snn35248 (void) sigset(SIGTERM, saveterm); 18512712Snn35248 (void) sigset(SIGQUIT, savequit); 18522712Snn35248 (void) sigset(SIGHUP, savehup); 18532712Snn35248 18542712Snn35248 return (pid == -1 ? -1 : status); 18552712Snn35248 } 18562712Snn35248 18572712Snn35248 static int 18582712Snn35248 subproc_status(const char *cmd, int status, boolean_t verbose_failure) 18590Sstevel@tonic-gate { 18600Sstevel@tonic-gate if (WIFEXITED(status)) { 18610Sstevel@tonic-gate int exit_code = WEXITSTATUS(status); 18620Sstevel@tonic-gate 18632712Snn35248 if ((verbose_failure) && (exit_code != ZONE_SUBPROC_OK)) 18642712Snn35248 zerror(gettext("'%s' failed with exit code %d."), cmd, 18652712Snn35248 exit_code); 18662712Snn35248 18672712Snn35248 return (exit_code); 18680Sstevel@tonic-gate } else if (WIFSIGNALED(status)) { 18690Sstevel@tonic-gate int signal = WTERMSIG(status); 18700Sstevel@tonic-gate char sigstr[SIG2STR_MAX]; 18710Sstevel@tonic-gate 18720Sstevel@tonic-gate if (sig2str(signal, sigstr) == 0) { 18730Sstevel@tonic-gate zerror(gettext("'%s' terminated by signal SIG%s."), cmd, 18740Sstevel@tonic-gate sigstr); 18750Sstevel@tonic-gate } else { 18760Sstevel@tonic-gate zerror(gettext("'%s' terminated by an unknown signal."), 18770Sstevel@tonic-gate cmd); 18780Sstevel@tonic-gate } 18790Sstevel@tonic-gate } else { 18800Sstevel@tonic-gate zerror(gettext("'%s' failed for unknown reasons."), cmd); 18810Sstevel@tonic-gate } 18822712Snn35248 18832712Snn35248 /* 18842712Snn35248 * Assume a subprocess that died due to a signal or an unknown error 18852712Snn35248 * should be considered an exit code of ZONE_SUBPROC_FATAL, as the 18862712Snn35248 * user will likely need to do some manual cleanup. 18872712Snn35248 */ 18882712Snn35248 return (ZONE_SUBPROC_FATAL); 18890Sstevel@tonic-gate } 18900Sstevel@tonic-gate 18910Sstevel@tonic-gate /* 18920Sstevel@tonic-gate * Various sanity checks; make sure: 18930Sstevel@tonic-gate * 1. We're in the global zone. 18940Sstevel@tonic-gate * 2. The calling user has sufficient privilege. 18950Sstevel@tonic-gate * 3. The target zone is neither the global zone nor anything starting with 18960Sstevel@tonic-gate * "SUNW". 18970Sstevel@tonic-gate * 4a. If we're looking for a 'not running' (i.e., configured or installed) 18980Sstevel@tonic-gate * zone, the name service knows about it. 18990Sstevel@tonic-gate * 4b. For some operations which expect a zone not to be running, that it is 19000Sstevel@tonic-gate * not already running (or ready). 19010Sstevel@tonic-gate */ 19020Sstevel@tonic-gate static int 19030Sstevel@tonic-gate sanity_check(char *zone, int cmd_num, boolean_t running, 19042712Snn35248 boolean_t unsafe_when_running, boolean_t force) 19050Sstevel@tonic-gate { 19060Sstevel@tonic-gate zone_entry_t *zent; 19070Sstevel@tonic-gate priv_set_t *privset; 19082712Snn35248 zone_state_t state, min_state; 1909766Scarlsonj char kernzone[ZONENAME_MAX]; 1910766Scarlsonj FILE *fp; 19110Sstevel@tonic-gate 19120Sstevel@tonic-gate if (getzoneid() != GLOBAL_ZONEID) { 19131645Scomay switch (cmd_num) { 19141645Scomay case CMD_HALT: 19151645Scomay zerror(gettext("use %s to %s this zone."), "halt(1M)", 19161645Scomay cmd_to_str(cmd_num)); 19171645Scomay break; 19181645Scomay case CMD_REBOOT: 19191645Scomay zerror(gettext("use %s to %s this zone."), 19201645Scomay "reboot(1M)", cmd_to_str(cmd_num)); 19211645Scomay break; 19221645Scomay default: 19231645Scomay zerror(gettext("must be in the global zone to %s a " 19241645Scomay "zone."), cmd_to_str(cmd_num)); 19251645Scomay break; 19261645Scomay } 19270Sstevel@tonic-gate return (Z_ERR); 19280Sstevel@tonic-gate } 19290Sstevel@tonic-gate 19300Sstevel@tonic-gate if ((privset = priv_allocset()) == NULL) { 19310Sstevel@tonic-gate zerror(gettext("%s failed"), "priv_allocset"); 19320Sstevel@tonic-gate return (Z_ERR); 19330Sstevel@tonic-gate } 19340Sstevel@tonic-gate 19350Sstevel@tonic-gate if (getppriv(PRIV_EFFECTIVE, privset) != 0) { 19360Sstevel@tonic-gate zerror(gettext("%s failed"), "getppriv"); 19370Sstevel@tonic-gate priv_freeset(privset); 19380Sstevel@tonic-gate return (Z_ERR); 19390Sstevel@tonic-gate } 19400Sstevel@tonic-gate 19410Sstevel@tonic-gate if (priv_isfullset(privset) == B_FALSE) { 19420Sstevel@tonic-gate zerror(gettext("only a privileged user may %s a zone."), 19430Sstevel@tonic-gate cmd_to_str(cmd_num)); 19440Sstevel@tonic-gate priv_freeset(privset); 19450Sstevel@tonic-gate return (Z_ERR); 19460Sstevel@tonic-gate } 19470Sstevel@tonic-gate priv_freeset(privset); 19480Sstevel@tonic-gate 19490Sstevel@tonic-gate if (zone == NULL) { 19500Sstevel@tonic-gate zerror(gettext("no zone specified")); 19510Sstevel@tonic-gate return (Z_ERR); 19520Sstevel@tonic-gate } 19530Sstevel@tonic-gate 19540Sstevel@tonic-gate if (strcmp(zone, GLOBAL_ZONENAME) == 0) { 19550Sstevel@tonic-gate zerror(gettext("%s operation is invalid for the global zone."), 19560Sstevel@tonic-gate cmd_to_str(cmd_num)); 19570Sstevel@tonic-gate return (Z_ERR); 19580Sstevel@tonic-gate } 19590Sstevel@tonic-gate 19600Sstevel@tonic-gate if (strncmp(zone, "SUNW", 4) == 0) { 19610Sstevel@tonic-gate zerror(gettext("%s operation is invalid for zones starting " 19620Sstevel@tonic-gate "with SUNW."), cmd_to_str(cmd_num)); 19630Sstevel@tonic-gate return (Z_ERR); 19640Sstevel@tonic-gate } 19650Sstevel@tonic-gate 19662712Snn35248 if (!is_native_zone && cmd_num == CMD_MOUNT) { 19672712Snn35248 zerror(gettext("%s operation is invalid for branded zones."), 19682712Snn35248 cmd_to_str(cmd_num)); 19692712Snn35248 return (Z_ERR); 19702712Snn35248 } 19712712Snn35248 1972766Scarlsonj if (!zonecfg_in_alt_root()) { 1973766Scarlsonj zent = lookup_running_zone(zone); 1974766Scarlsonj } else if ((fp = zonecfg_open_scratch("", B_FALSE)) == NULL) { 1975766Scarlsonj zent = NULL; 1976766Scarlsonj } else { 1977766Scarlsonj if (zonecfg_find_scratch(fp, zone, zonecfg_get_root(), 1978766Scarlsonj kernzone, sizeof (kernzone)) == 0) 1979766Scarlsonj zent = lookup_running_zone(kernzone); 1980766Scarlsonj else 1981766Scarlsonj zent = NULL; 1982766Scarlsonj zonecfg_close_scratch(fp); 1983766Scarlsonj } 1984766Scarlsonj 19850Sstevel@tonic-gate /* 19860Sstevel@tonic-gate * Look up from the kernel for 'running' zones. 19870Sstevel@tonic-gate */ 19882712Snn35248 if (running && !force) { 19890Sstevel@tonic-gate if (zent == NULL) { 19900Sstevel@tonic-gate zerror(gettext("not running")); 19910Sstevel@tonic-gate return (Z_ERR); 19920Sstevel@tonic-gate } 19930Sstevel@tonic-gate } else { 19940Sstevel@tonic-gate int err; 19950Sstevel@tonic-gate 19960Sstevel@tonic-gate if (unsafe_when_running && zent != NULL) { 19970Sstevel@tonic-gate /* check whether the zone is ready or running */ 19980Sstevel@tonic-gate if ((err = zone_get_state(zent->zname, 19990Sstevel@tonic-gate &zent->zstate_num)) != Z_OK) { 20000Sstevel@tonic-gate errno = err; 20010Sstevel@tonic-gate zperror2(zent->zname, 20020Sstevel@tonic-gate gettext("could not get state")); 20030Sstevel@tonic-gate /* can't tell, so hedge */ 20040Sstevel@tonic-gate zent->zstate_str = "ready/running"; 20050Sstevel@tonic-gate } else { 20060Sstevel@tonic-gate zent->zstate_str = 20070Sstevel@tonic-gate zone_state_str(zent->zstate_num); 20080Sstevel@tonic-gate } 20090Sstevel@tonic-gate zerror(gettext("%s operation is invalid for %s zones."), 20100Sstevel@tonic-gate cmd_to_str(cmd_num), zent->zstate_str); 20110Sstevel@tonic-gate return (Z_ERR); 20120Sstevel@tonic-gate } 20130Sstevel@tonic-gate if ((err = zone_get_state(zone, &state)) != Z_OK) { 20140Sstevel@tonic-gate errno = err; 20150Sstevel@tonic-gate zperror2(zone, gettext("could not get state")); 20160Sstevel@tonic-gate return (Z_ERR); 20170Sstevel@tonic-gate } 20180Sstevel@tonic-gate switch (cmd_num) { 20190Sstevel@tonic-gate case CMD_UNINSTALL: 20200Sstevel@tonic-gate if (state == ZONE_STATE_CONFIGURED) { 20210Sstevel@tonic-gate zerror(gettext("is already in state '%s'."), 20220Sstevel@tonic-gate zone_state_str(ZONE_STATE_CONFIGURED)); 20230Sstevel@tonic-gate return (Z_ERR); 20240Sstevel@tonic-gate } 20250Sstevel@tonic-gate break; 20261507Sgjelinek case CMD_ATTACH: 20271300Sgjelinek case CMD_CLONE: 20280Sstevel@tonic-gate case CMD_INSTALL: 20290Sstevel@tonic-gate if (state == ZONE_STATE_INSTALLED) { 20300Sstevel@tonic-gate zerror(gettext("is already %s."), 20310Sstevel@tonic-gate zone_state_str(ZONE_STATE_INSTALLED)); 20320Sstevel@tonic-gate return (Z_ERR); 20330Sstevel@tonic-gate } else if (state == ZONE_STATE_INCOMPLETE) { 20340Sstevel@tonic-gate zerror(gettext("zone is %s; %s required."), 20350Sstevel@tonic-gate zone_state_str(ZONE_STATE_INCOMPLETE), 20360Sstevel@tonic-gate cmd_to_str(CMD_UNINSTALL)); 20370Sstevel@tonic-gate return (Z_ERR); 20380Sstevel@tonic-gate } 20390Sstevel@tonic-gate break; 20401507Sgjelinek case CMD_DETACH: 20411300Sgjelinek case CMD_MOVE: 20420Sstevel@tonic-gate case CMD_READY: 20430Sstevel@tonic-gate case CMD_BOOT: 2044766Scarlsonj case CMD_MOUNT: 20452303Scarlsonj case CMD_MARK: 20462712Snn35248 if ((cmd_num == CMD_BOOT || cmd_num == CMD_MOUNT) && 20472712Snn35248 force) 20482712Snn35248 min_state = ZONE_STATE_INCOMPLETE; 20492712Snn35248 else 20502712Snn35248 min_state = ZONE_STATE_INSTALLED; 20512712Snn35248 20522712Snn35248 if (force && cmd_num == CMD_BOOT && is_native_zone) { 20532712Snn35248 zerror(gettext("Only branded zones may be " 20542712Snn35248 "force-booted.")); 20552712Snn35248 return (Z_ERR); 20562712Snn35248 } 20572712Snn35248 20582712Snn35248 if (state < min_state) { 20590Sstevel@tonic-gate zerror(gettext("must be %s before %s."), 20602712Snn35248 zone_state_str(min_state), 20610Sstevel@tonic-gate cmd_to_str(cmd_num)); 20620Sstevel@tonic-gate return (Z_ERR); 20630Sstevel@tonic-gate } 20640Sstevel@tonic-gate break; 20650Sstevel@tonic-gate case CMD_VERIFY: 20660Sstevel@tonic-gate if (state == ZONE_STATE_INCOMPLETE) { 20670Sstevel@tonic-gate zerror(gettext("zone is %s; %s required."), 20680Sstevel@tonic-gate zone_state_str(ZONE_STATE_INCOMPLETE), 20690Sstevel@tonic-gate cmd_to_str(CMD_UNINSTALL)); 20700Sstevel@tonic-gate return (Z_ERR); 20710Sstevel@tonic-gate } 20720Sstevel@tonic-gate break; 2073766Scarlsonj case CMD_UNMOUNT: 2074766Scarlsonj if (state != ZONE_STATE_MOUNTED) { 2075766Scarlsonj zerror(gettext("must be %s before %s."), 2076766Scarlsonj zone_state_str(ZONE_STATE_MOUNTED), 2077766Scarlsonj cmd_to_str(cmd_num)); 2078766Scarlsonj return (Z_ERR); 2079766Scarlsonj } 2080766Scarlsonj break; 20810Sstevel@tonic-gate } 20820Sstevel@tonic-gate } 20830Sstevel@tonic-gate return (Z_OK); 20840Sstevel@tonic-gate } 20850Sstevel@tonic-gate 20860Sstevel@tonic-gate static int 20870Sstevel@tonic-gate halt_func(int argc, char *argv[]) 20880Sstevel@tonic-gate { 20890Sstevel@tonic-gate zone_cmd_arg_t zarg; 20900Sstevel@tonic-gate int arg; 20910Sstevel@tonic-gate 2092766Scarlsonj if (zonecfg_in_alt_root()) { 2093766Scarlsonj zerror(gettext("cannot halt zone in alternate root")); 2094766Scarlsonj return (Z_ERR); 2095766Scarlsonj } 2096766Scarlsonj 20970Sstevel@tonic-gate optind = 0; 20980Sstevel@tonic-gate if ((arg = getopt(argc, argv, "?")) != EOF) { 20990Sstevel@tonic-gate switch (arg) { 21000Sstevel@tonic-gate case '?': 21010Sstevel@tonic-gate sub_usage(SHELP_HALT, CMD_HALT); 21020Sstevel@tonic-gate return (optopt == '?' ? Z_OK : Z_USAGE); 21030Sstevel@tonic-gate default: 21040Sstevel@tonic-gate sub_usage(SHELP_HALT, CMD_HALT); 21050Sstevel@tonic-gate return (Z_USAGE); 21060Sstevel@tonic-gate } 21070Sstevel@tonic-gate } 21080Sstevel@tonic-gate if (argc > optind) { 21090Sstevel@tonic-gate sub_usage(SHELP_HALT, CMD_HALT); 21100Sstevel@tonic-gate return (Z_USAGE); 21110Sstevel@tonic-gate } 21120Sstevel@tonic-gate /* 21130Sstevel@tonic-gate * zoneadmd should be the one to decide whether or not to proceed, 21140Sstevel@tonic-gate * so even though it seems that the fourth parameter below should 21150Sstevel@tonic-gate * perhaps be B_TRUE, it really shouldn't be. 21160Sstevel@tonic-gate */ 21172712Snn35248 if (sanity_check(target_zone, CMD_HALT, B_FALSE, B_FALSE, B_FALSE) 21182712Snn35248 != Z_OK) 21190Sstevel@tonic-gate return (Z_ERR); 21200Sstevel@tonic-gate 21213339Szt129084 /* 21223339Szt129084 * Invoke brand-specific handler. 21233339Szt129084 */ 21243339Szt129084 if (invoke_brand_handler(CMD_HALT, argv) != Z_OK) 21253339Szt129084 return (Z_ERR); 21263339Szt129084 21270Sstevel@tonic-gate zarg.cmd = Z_HALT; 21280Sstevel@tonic-gate return ((call_zoneadmd(target_zone, &zarg) == 0) ? Z_OK : Z_ERR); 21290Sstevel@tonic-gate } 21300Sstevel@tonic-gate 21310Sstevel@tonic-gate static int 21320Sstevel@tonic-gate reboot_func(int argc, char *argv[]) 21330Sstevel@tonic-gate { 21340Sstevel@tonic-gate zone_cmd_arg_t zarg; 21350Sstevel@tonic-gate int arg; 21360Sstevel@tonic-gate 2137766Scarlsonj if (zonecfg_in_alt_root()) { 2138766Scarlsonj zerror(gettext("cannot reboot zone in alternate root")); 2139766Scarlsonj return (Z_ERR); 2140766Scarlsonj } 2141766Scarlsonj 21420Sstevel@tonic-gate optind = 0; 21430Sstevel@tonic-gate if ((arg = getopt(argc, argv, "?")) != EOF) { 21440Sstevel@tonic-gate switch (arg) { 21450Sstevel@tonic-gate case '?': 21460Sstevel@tonic-gate sub_usage(SHELP_REBOOT, CMD_REBOOT); 21470Sstevel@tonic-gate return (optopt == '?' ? Z_OK : Z_USAGE); 21480Sstevel@tonic-gate default: 21490Sstevel@tonic-gate sub_usage(SHELP_REBOOT, CMD_REBOOT); 21500Sstevel@tonic-gate return (Z_USAGE); 21510Sstevel@tonic-gate } 21520Sstevel@tonic-gate } 21532267Sdp 21542267Sdp zarg.bootbuf[0] = '\0'; 21552267Sdp for (; optind < argc; optind++) { 21562267Sdp if (strlcat(zarg.bootbuf, argv[optind], 21572267Sdp sizeof (zarg.bootbuf)) >= sizeof (zarg.bootbuf)) { 21582267Sdp zerror(gettext("Boot argument list too long")); 21592267Sdp return (Z_ERR); 21602267Sdp } 21612267Sdp if (optind < argc - 1) 21622267Sdp if (strlcat(zarg.bootbuf, " ", sizeof (zarg.bootbuf)) >= 21632267Sdp sizeof (zarg.bootbuf)) { 21642267Sdp zerror(gettext("Boot argument list too long")); 21652267Sdp return (Z_ERR); 21662267Sdp } 21672267Sdp } 21682267Sdp 21692267Sdp 21700Sstevel@tonic-gate /* 21710Sstevel@tonic-gate * zoneadmd should be the one to decide whether or not to proceed, 21720Sstevel@tonic-gate * so even though it seems that the fourth parameter below should 21730Sstevel@tonic-gate * perhaps be B_TRUE, it really shouldn't be. 21740Sstevel@tonic-gate */ 21752712Snn35248 if (sanity_check(target_zone, CMD_REBOOT, B_TRUE, B_FALSE, B_FALSE) 21762712Snn35248 != Z_OK) 21770Sstevel@tonic-gate return (Z_ERR); 21783339Szt129084 if (verify_details(CMD_REBOOT, argv) != Z_OK) 2179823Sgjelinek return (Z_ERR); 21800Sstevel@tonic-gate 21810Sstevel@tonic-gate zarg.cmd = Z_REBOOT; 21820Sstevel@tonic-gate return ((call_zoneadmd(target_zone, &zarg) == 0) ? Z_OK : Z_ERR); 21830Sstevel@tonic-gate } 21840Sstevel@tonic-gate 21850Sstevel@tonic-gate static int 21863339Szt129084 verify_brand(zone_dochandle_t handle, int cmd_num, char *argv[]) 21872712Snn35248 { 21882712Snn35248 char cmdbuf[MAXPATHLEN]; 21892712Snn35248 int err; 21902712Snn35248 char zonepath[MAXPATHLEN]; 21912727Sedp brand_handle_t bh = NULL; 21923339Szt129084 int status, i; 21932712Snn35248 21942712Snn35248 /* 21952712Snn35248 * Fetch the verify command from the brand configuration. 21962712Snn35248 * "exec" the command so that the returned status is that of 21972712Snn35248 * the command and not the shell. 21982712Snn35248 */ 21992712Snn35248 if ((err = zonecfg_get_zonepath(handle, zonepath, sizeof (zonepath))) != 22002712Snn35248 Z_OK) { 22012712Snn35248 errno = err; 22023339Szt129084 zperror(cmd_to_str(cmd_num), B_TRUE); 22032712Snn35248 return (Z_ERR); 22042712Snn35248 } 22052727Sedp if ((bh = brand_open(target_brand)) == NULL) { 22062712Snn35248 zerror(gettext("missing or invalid brand")); 22072712Snn35248 return (Z_ERR); 22082712Snn35248 } 22092712Snn35248 22102712Snn35248 /* 22112712Snn35248 * If the brand has its own verification routine, execute it now. 22123339Szt129084 * The verification routine validates the intended zoneadm 22133339Szt129084 * operation for the specific brand. The zoneadm subcommand and 22143339Szt129084 * all its arguments are passed to the routine. 22152712Snn35248 */ 22162712Snn35248 (void) strcpy(cmdbuf, EXEC_PREFIX); 22172727Sedp err = brand_get_verify_adm(bh, target_zone, zonepath, 22182712Snn35248 cmdbuf + EXEC_LEN, sizeof (cmdbuf) - EXEC_LEN, 0, NULL); 22192727Sedp brand_close(bh); 22203339Szt129084 if (err != 0) 22213339Szt129084 return (Z_BRAND_ERROR); 22223339Szt129084 if (strlen(cmdbuf) <= EXEC_LEN) 22233339Szt129084 return (Z_OK); 22243339Szt129084 22253339Szt129084 if (strlcat(cmdbuf, cmd_to_str(cmd_num), 22263339Szt129084 sizeof (cmdbuf)) >= sizeof (cmdbuf)) 22273339Szt129084 return (Z_ERR); 22283339Szt129084 22293339Szt129084 /* Build the argv string */ 22303339Szt129084 i = 0; 22313339Szt129084 while (argv[i] != NULL) { 22323339Szt129084 if ((strlcat(cmdbuf, " ", 22333339Szt129084 sizeof (cmdbuf)) >= sizeof (cmdbuf)) || 22343339Szt129084 (strlcat(cmdbuf, argv[i++], 22353339Szt129084 sizeof (cmdbuf)) >= sizeof (cmdbuf))) 22363339Szt129084 return (Z_ERR); 22373339Szt129084 } 22383339Szt129084 22393686Sgjelinek if (zoneadm_is_nested) 22403686Sgjelinek status = do_subproc(cmdbuf); 22413686Sgjelinek else 22423686Sgjelinek status = do_subproc_interactive(cmdbuf); 22433339Szt129084 err = subproc_status(gettext("brand-specific verification"), 22443339Szt129084 status, B_FALSE); 22453339Szt129084 22463339Szt129084 return ((err == ZONE_SUBPROC_OK) ? Z_OK : Z_BRAND_ERROR); 22472712Snn35248 } 22482712Snn35248 22492712Snn35248 static int 22500Sstevel@tonic-gate verify_rctls(zone_dochandle_t handle) 22510Sstevel@tonic-gate { 22520Sstevel@tonic-gate struct zone_rctltab rctltab; 22530Sstevel@tonic-gate size_t rbs = rctlblk_size(); 22540Sstevel@tonic-gate rctlblk_t *rctlblk; 22550Sstevel@tonic-gate int error = Z_INVAL; 22560Sstevel@tonic-gate 22570Sstevel@tonic-gate if ((rctlblk = malloc(rbs)) == NULL) { 22580Sstevel@tonic-gate zerror(gettext("failed to allocate %lu bytes: %s"), rbs, 22590Sstevel@tonic-gate strerror(errno)); 22600Sstevel@tonic-gate return (Z_NOMEM); 22610Sstevel@tonic-gate } 22620Sstevel@tonic-gate 22630Sstevel@tonic-gate if (zonecfg_setrctlent(handle) != Z_OK) { 22640Sstevel@tonic-gate zerror(gettext("zonecfg_setrctlent failed")); 22650Sstevel@tonic-gate free(rctlblk); 22660Sstevel@tonic-gate return (error); 22670Sstevel@tonic-gate } 22680Sstevel@tonic-gate 22690Sstevel@tonic-gate rctltab.zone_rctl_valptr = NULL; 22700Sstevel@tonic-gate while (zonecfg_getrctlent(handle, &rctltab) == Z_OK) { 22710Sstevel@tonic-gate struct zone_rctlvaltab *rctlval; 22720Sstevel@tonic-gate const char *name = rctltab.zone_rctl_name; 22730Sstevel@tonic-gate 22740Sstevel@tonic-gate if (!zonecfg_is_rctl(name)) { 22750Sstevel@tonic-gate zerror(gettext("WARNING: Ignoring unrecognized rctl " 22760Sstevel@tonic-gate "'%s'."), name); 22770Sstevel@tonic-gate zonecfg_free_rctl_value_list(rctltab.zone_rctl_valptr); 22780Sstevel@tonic-gate rctltab.zone_rctl_valptr = NULL; 22790Sstevel@tonic-gate continue; 22800Sstevel@tonic-gate } 22810Sstevel@tonic-gate 22820Sstevel@tonic-gate for (rctlval = rctltab.zone_rctl_valptr; rctlval != NULL; 22830Sstevel@tonic-gate rctlval = rctlval->zone_rctlval_next) { 22840Sstevel@tonic-gate if (zonecfg_construct_rctlblk(rctlval, rctlblk) 22850Sstevel@tonic-gate != Z_OK) { 22860Sstevel@tonic-gate zerror(gettext("invalid rctl value: " 22870Sstevel@tonic-gate "(priv=%s,limit=%s,action%s)"), 22880Sstevel@tonic-gate rctlval->zone_rctlval_priv, 22890Sstevel@tonic-gate rctlval->zone_rctlval_limit, 22900Sstevel@tonic-gate rctlval->zone_rctlval_action); 22910Sstevel@tonic-gate goto out; 22920Sstevel@tonic-gate } 22930Sstevel@tonic-gate if (!zonecfg_valid_rctl(name, rctlblk)) { 22940Sstevel@tonic-gate zerror(gettext("(priv=%s,limit=%s,action=%s) " 22950Sstevel@tonic-gate "is not a valid value for rctl '%s'"), 22960Sstevel@tonic-gate rctlval->zone_rctlval_priv, 22970Sstevel@tonic-gate rctlval->zone_rctlval_limit, 22980Sstevel@tonic-gate rctlval->zone_rctlval_action, 22990Sstevel@tonic-gate name); 23000Sstevel@tonic-gate goto out; 23010Sstevel@tonic-gate } 23020Sstevel@tonic-gate } 23030Sstevel@tonic-gate zonecfg_free_rctl_value_list(rctltab.zone_rctl_valptr); 23040Sstevel@tonic-gate } 23050Sstevel@tonic-gate rctltab.zone_rctl_valptr = NULL; 23060Sstevel@tonic-gate error = Z_OK; 23070Sstevel@tonic-gate out: 23080Sstevel@tonic-gate zonecfg_free_rctl_value_list(rctltab.zone_rctl_valptr); 23090Sstevel@tonic-gate (void) zonecfg_endrctlent(handle); 23100Sstevel@tonic-gate free(rctlblk); 23110Sstevel@tonic-gate return (error); 23120Sstevel@tonic-gate } 23130Sstevel@tonic-gate 23140Sstevel@tonic-gate static int 23150Sstevel@tonic-gate verify_pool(zone_dochandle_t handle) 23160Sstevel@tonic-gate { 23170Sstevel@tonic-gate char poolname[MAXPATHLEN]; 23180Sstevel@tonic-gate pool_conf_t *poolconf; 23190Sstevel@tonic-gate pool_t *pool; 23200Sstevel@tonic-gate int status; 23210Sstevel@tonic-gate int error; 23220Sstevel@tonic-gate 23230Sstevel@tonic-gate /* 23240Sstevel@tonic-gate * This ends up being very similar to the check done in zoneadmd. 23250Sstevel@tonic-gate */ 23260Sstevel@tonic-gate error = zonecfg_get_pool(handle, poolname, sizeof (poolname)); 23270Sstevel@tonic-gate if (error == Z_NO_ENTRY || (error == Z_OK && strlen(poolname) == 0)) { 23280Sstevel@tonic-gate /* 23290Sstevel@tonic-gate * No pool specified. 23300Sstevel@tonic-gate */ 23310Sstevel@tonic-gate return (0); 23320Sstevel@tonic-gate } 23330Sstevel@tonic-gate if (error != Z_OK) { 23340Sstevel@tonic-gate zperror(gettext("Unable to retrieve pool name from " 23350Sstevel@tonic-gate "configuration"), B_TRUE); 23360Sstevel@tonic-gate return (error); 23370Sstevel@tonic-gate } 23380Sstevel@tonic-gate /* 23390Sstevel@tonic-gate * Don't do anything if pools aren't enabled. 23400Sstevel@tonic-gate */ 23410Sstevel@tonic-gate if (pool_get_status(&status) != PO_SUCCESS || status != POOL_ENABLED) { 23420Sstevel@tonic-gate zerror(gettext("WARNING: pools facility not active; " 23430Sstevel@tonic-gate "zone will not be bound to pool '%s'."), poolname); 23440Sstevel@tonic-gate return (Z_OK); 23450Sstevel@tonic-gate } 23460Sstevel@tonic-gate /* 23470Sstevel@tonic-gate * Try to provide a sane error message if the requested pool doesn't 23480Sstevel@tonic-gate * exist. It isn't clear that pools-related failures should 23490Sstevel@tonic-gate * necessarily translate to a failure to verify the zone configuration, 23500Sstevel@tonic-gate * hence they are not considered errors. 23510Sstevel@tonic-gate */ 23520Sstevel@tonic-gate if ((poolconf = pool_conf_alloc()) == NULL) { 23530Sstevel@tonic-gate zerror(gettext("WARNING: pool_conf_alloc failed; " 23540Sstevel@tonic-gate "using default pool")); 23550Sstevel@tonic-gate return (Z_OK); 23560Sstevel@tonic-gate } 23570Sstevel@tonic-gate if (pool_conf_open(poolconf, pool_dynamic_location(), PO_RDONLY) != 23580Sstevel@tonic-gate PO_SUCCESS) { 23590Sstevel@tonic-gate zerror(gettext("WARNING: pool_conf_open failed; " 23600Sstevel@tonic-gate "using default pool")); 23610Sstevel@tonic-gate pool_conf_free(poolconf); 23620Sstevel@tonic-gate return (Z_OK); 23630Sstevel@tonic-gate } 23640Sstevel@tonic-gate pool = pool_get_pool(poolconf, poolname); 23650Sstevel@tonic-gate (void) pool_conf_close(poolconf); 23660Sstevel@tonic-gate pool_conf_free(poolconf); 23670Sstevel@tonic-gate if (pool == NULL) { 23680Sstevel@tonic-gate zerror(gettext("WARNING: pool '%s' not found. " 23690Sstevel@tonic-gate "using default pool"), poolname); 23700Sstevel@tonic-gate } 23710Sstevel@tonic-gate 23720Sstevel@tonic-gate return (Z_OK); 23730Sstevel@tonic-gate } 23740Sstevel@tonic-gate 23750Sstevel@tonic-gate static int 2376823Sgjelinek verify_ipd(zone_dochandle_t handle) 2377823Sgjelinek { 2378823Sgjelinek int return_code = Z_OK; 2379823Sgjelinek struct zone_fstab fstab; 2380823Sgjelinek struct stat st; 2381823Sgjelinek char specdir[MAXPATHLEN]; 2382823Sgjelinek 2383823Sgjelinek if (zonecfg_setipdent(handle) != Z_OK) { 2384924Sgjelinek /* 2385924Sgjelinek * TRANSLATION_NOTE 2386924Sgjelinek * inherit-pkg-dirs is a literal that should not be translated. 2387924Sgjelinek */ 2388924Sgjelinek (void) fprintf(stderr, gettext("could not verify " 2389823Sgjelinek "inherit-pkg-dirs: unable to enumerate mounts\n")); 2390823Sgjelinek return (Z_ERR); 2391823Sgjelinek } 2392823Sgjelinek while (zonecfg_getipdent(handle, &fstab) == Z_OK) { 2393823Sgjelinek /* 2394823Sgjelinek * Verify fs_dir exists. 2395823Sgjelinek */ 2396823Sgjelinek (void) snprintf(specdir, sizeof (specdir), "%s%s", 2397823Sgjelinek zonecfg_get_root(), fstab.zone_fs_dir); 2398823Sgjelinek if (stat(specdir, &st) != 0) { 2399924Sgjelinek /* 2400924Sgjelinek * TRANSLATION_NOTE 2401924Sgjelinek * inherit-pkg-dir is a literal that should not be 2402924Sgjelinek * translated. 2403924Sgjelinek */ 2404924Sgjelinek (void) fprintf(stderr, gettext("could not verify " 2405823Sgjelinek "inherit-pkg-dir %s: %s\n"), 2406823Sgjelinek fstab.zone_fs_dir, strerror(errno)); 2407823Sgjelinek return_code = Z_ERR; 2408823Sgjelinek } 2409823Sgjelinek if (strcmp(st.st_fstype, MNTTYPE_NFS) == 0) { 2410924Sgjelinek /* 2411924Sgjelinek * TRANSLATION_NOTE 2412924Sgjelinek * inherit-pkg-dir and NFS are literals that should 2413924Sgjelinek * not be translated. 2414924Sgjelinek */ 2415823Sgjelinek (void) fprintf(stderr, gettext("cannot verify " 24161867Sgjelinek "inherit-pkg-dir %s: NFS mounted file system.\n" 24171867Sgjelinek "\tA local file system must be used.\n"), 2418823Sgjelinek fstab.zone_fs_dir); 2419823Sgjelinek return_code = Z_ERR; 2420823Sgjelinek } 2421823Sgjelinek } 2422823Sgjelinek (void) zonecfg_endipdent(handle); 2423823Sgjelinek 2424823Sgjelinek return (return_code); 2425823Sgjelinek } 2426823Sgjelinek 24271393Slling /* 24281867Sgjelinek * Verify that the special device/file system exists and is valid. 24291393Slling */ 24301393Slling static int 24311393Slling verify_fs_special(struct zone_fstab *fstab) 24321393Slling { 24331393Slling struct stat st; 24341393Slling 24352971Sgjelinek /* 24362971Sgjelinek * This validation is really intended for standard zone administration. 24372971Sgjelinek * If we are in a mini-root or some other upgrade situation where 24382971Sgjelinek * we are using the scratch zone, just by-pass this. 24392971Sgjelinek */ 24402971Sgjelinek if (zonecfg_in_alt_root()) 24412971Sgjelinek return (Z_OK); 24422971Sgjelinek 24431393Slling if (strcmp(fstab->zone_fs_type, MNTTYPE_ZFS) == 0) 24441393Slling return (verify_fs_zfs(fstab)); 24451393Slling 24461393Slling if (stat(fstab->zone_fs_special, &st) != 0) { 24471397Slling (void) fprintf(stderr, gettext("could not verify fs " 24481393Slling "%s: could not access %s: %s\n"), fstab->zone_fs_dir, 24491393Slling fstab->zone_fs_special, strerror(errno)); 24501393Slling return (Z_ERR); 24511393Slling } 24521393Slling 24531393Slling if (strcmp(st.st_fstype, MNTTYPE_NFS) == 0) { 24541393Slling /* 24551393Slling * TRANSLATION_NOTE 24561393Slling * fs and NFS are literals that should 24571393Slling * not be translated. 24581393Slling */ 24591393Slling (void) fprintf(stderr, gettext("cannot verify " 24601867Sgjelinek "fs %s: NFS mounted file system.\n" 24611867Sgjelinek "\tA local file system must be used.\n"), 24621393Slling fstab->zone_fs_special); 24631393Slling return (Z_ERR); 24641393Slling } 24651393Slling 24661393Slling return (Z_OK); 24671393Slling } 24681393Slling 2469823Sgjelinek static int 24700Sstevel@tonic-gate verify_filesystems(zone_dochandle_t handle) 24710Sstevel@tonic-gate { 24720Sstevel@tonic-gate int return_code = Z_OK; 24730Sstevel@tonic-gate struct zone_fstab fstab; 24740Sstevel@tonic-gate char cmdbuf[MAXPATHLEN]; 24750Sstevel@tonic-gate struct stat st; 24760Sstevel@tonic-gate 24770Sstevel@tonic-gate /* 24780Sstevel@tonic-gate * No need to verify inherit-pkg-dir fs types, as their type is 24790Sstevel@tonic-gate * implicitly lofs, which is known. Therefore, the types are only 24801867Sgjelinek * verified for regular file systems below. 24810Sstevel@tonic-gate * 24820Sstevel@tonic-gate * Since the actual mount point is not known until the dependent mounts 24830Sstevel@tonic-gate * are performed, we don't attempt any path validation here: that will 24840Sstevel@tonic-gate * happen later when zoneadmd actually does the mounts. 24850Sstevel@tonic-gate */ 24860Sstevel@tonic-gate if (zonecfg_setfsent(handle) != Z_OK) { 24871867Sgjelinek (void) fprintf(stderr, gettext("could not verify file systems: " 24880Sstevel@tonic-gate "unable to enumerate mounts\n")); 24890Sstevel@tonic-gate return (Z_ERR); 24900Sstevel@tonic-gate } 24910Sstevel@tonic-gate while (zonecfg_getfsent(handle, &fstab) == Z_OK) { 24920Sstevel@tonic-gate if (!zonecfg_valid_fs_type(fstab.zone_fs_type)) { 24930Sstevel@tonic-gate (void) fprintf(stderr, gettext("cannot verify fs %s: " 24940Sstevel@tonic-gate "type %s is not allowed.\n"), fstab.zone_fs_dir, 24950Sstevel@tonic-gate fstab.zone_fs_type); 24960Sstevel@tonic-gate return_code = Z_ERR; 24970Sstevel@tonic-gate goto next_fs; 24980Sstevel@tonic-gate } 24990Sstevel@tonic-gate /* 25000Sstevel@tonic-gate * Verify /usr/lib/fs/<fstype>/mount exists. 25010Sstevel@tonic-gate */ 25020Sstevel@tonic-gate if (snprintf(cmdbuf, sizeof (cmdbuf), "/usr/lib/fs/%s/mount", 25030Sstevel@tonic-gate fstab.zone_fs_type) > sizeof (cmdbuf)) { 25040Sstevel@tonic-gate (void) fprintf(stderr, gettext("cannot verify fs %s: " 25050Sstevel@tonic-gate "type %s is too long.\n"), fstab.zone_fs_dir, 25060Sstevel@tonic-gate fstab.zone_fs_type); 25070Sstevel@tonic-gate return_code = Z_ERR; 25080Sstevel@tonic-gate goto next_fs; 25090Sstevel@tonic-gate } 25100Sstevel@tonic-gate if (stat(cmdbuf, &st) != 0) { 2511924Sgjelinek (void) fprintf(stderr, gettext("could not verify fs " 2512924Sgjelinek "%s: could not access %s: %s\n"), fstab.zone_fs_dir, 25130Sstevel@tonic-gate cmdbuf, strerror(errno)); 25140Sstevel@tonic-gate return_code = Z_ERR; 25150Sstevel@tonic-gate goto next_fs; 25160Sstevel@tonic-gate } 25170Sstevel@tonic-gate if (!S_ISREG(st.st_mode)) { 2518924Sgjelinek (void) fprintf(stderr, gettext("could not verify fs " 2519924Sgjelinek "%s: %s is not a regular file\n"), 2520924Sgjelinek fstab.zone_fs_dir, cmdbuf); 25210Sstevel@tonic-gate return_code = Z_ERR; 25220Sstevel@tonic-gate goto next_fs; 25230Sstevel@tonic-gate } 25240Sstevel@tonic-gate /* 25250Sstevel@tonic-gate * Verify /usr/lib/fs/<fstype>/fsck exists iff zone_fs_raw is 25260Sstevel@tonic-gate * set. 25270Sstevel@tonic-gate */ 25280Sstevel@tonic-gate if (snprintf(cmdbuf, sizeof (cmdbuf), "/usr/lib/fs/%s/fsck", 25290Sstevel@tonic-gate fstab.zone_fs_type) > sizeof (cmdbuf)) { 25300Sstevel@tonic-gate (void) fprintf(stderr, gettext("cannot verify fs %s: " 25310Sstevel@tonic-gate "type %s is too long.\n"), fstab.zone_fs_dir, 25320Sstevel@tonic-gate fstab.zone_fs_type); 25330Sstevel@tonic-gate return_code = Z_ERR; 25340Sstevel@tonic-gate goto next_fs; 25350Sstevel@tonic-gate } 25360Sstevel@tonic-gate if (fstab.zone_fs_raw[0] == '\0' && stat(cmdbuf, &st) == 0) { 2537924Sgjelinek (void) fprintf(stderr, gettext("could not verify fs " 2538924Sgjelinek "%s: must specify 'raw' device for %s " 25391867Sgjelinek "file systems\n"), 25400Sstevel@tonic-gate fstab.zone_fs_dir, fstab.zone_fs_type); 25410Sstevel@tonic-gate return_code = Z_ERR; 25420Sstevel@tonic-gate goto next_fs; 25430Sstevel@tonic-gate } 25440Sstevel@tonic-gate if (fstab.zone_fs_raw[0] != '\0' && 25450Sstevel@tonic-gate (stat(cmdbuf, &st) != 0 || !S_ISREG(st.st_mode))) { 25460Sstevel@tonic-gate (void) fprintf(stderr, gettext("cannot verify fs %s: " 25470Sstevel@tonic-gate "'raw' device specified but " 25480Sstevel@tonic-gate "no fsck executable exists for %s\n"), 25490Sstevel@tonic-gate fstab.zone_fs_dir, fstab.zone_fs_type); 25500Sstevel@tonic-gate return_code = Z_ERR; 25510Sstevel@tonic-gate goto next_fs; 25520Sstevel@tonic-gate } 25531393Slling 25541393Slling /* Verify fs_special. */ 25551393Slling if ((return_code = verify_fs_special(&fstab)) != Z_OK) 2556823Sgjelinek goto next_fs; 25571393Slling 25581393Slling /* Verify fs_raw. */ 2559823Sgjelinek if (fstab.zone_fs_raw[0] != '\0' && 2560823Sgjelinek stat(fstab.zone_fs_raw, &st) != 0) { 2561924Sgjelinek /* 2562924Sgjelinek * TRANSLATION_NOTE 2563924Sgjelinek * fs is a literal that should not be translated. 2564924Sgjelinek */ 2565924Sgjelinek (void) fprintf(stderr, gettext("could not verify fs " 2566924Sgjelinek "%s: could not access %s: %s\n"), fstab.zone_fs_dir, 2567823Sgjelinek fstab.zone_fs_raw, strerror(errno)); 2568823Sgjelinek return_code = Z_ERR; 2569823Sgjelinek goto next_fs; 2570823Sgjelinek } 25710Sstevel@tonic-gate next_fs: 25720Sstevel@tonic-gate zonecfg_free_fs_option_list(fstab.zone_fs_options); 25730Sstevel@tonic-gate } 25740Sstevel@tonic-gate (void) zonecfg_endfsent(handle); 25750Sstevel@tonic-gate 25760Sstevel@tonic-gate return (return_code); 25770Sstevel@tonic-gate } 25780Sstevel@tonic-gate 25790Sstevel@tonic-gate static int 25801645Scomay verify_limitpriv(zone_dochandle_t handle) 25811645Scomay { 25821645Scomay char *privname = NULL; 25831645Scomay int err; 25841645Scomay priv_set_t *privs; 25851645Scomay 25861645Scomay if ((privs = priv_allocset()) == NULL) { 25871645Scomay zperror(gettext("failed to allocate privilege set"), B_FALSE); 25881645Scomay return (Z_NOMEM); 25891645Scomay } 25901645Scomay err = zonecfg_get_privset(handle, privs, &privname); 25911645Scomay switch (err) { 25921645Scomay case Z_OK: 25931645Scomay break; 25941645Scomay case Z_PRIV_PROHIBITED: 25951645Scomay (void) fprintf(stderr, gettext("privilege \"%s\" is not " 25961645Scomay "permitted within the zone's privilege set\n"), privname); 25971645Scomay break; 25981645Scomay case Z_PRIV_REQUIRED: 25991645Scomay (void) fprintf(stderr, gettext("required privilege \"%s\" is " 26001645Scomay "missing from the zone's privilege set\n"), privname); 26011645Scomay break; 26021645Scomay case Z_PRIV_UNKNOWN: 26031645Scomay (void) fprintf(stderr, gettext("unknown privilege \"%s\" " 26041645Scomay "specified in the zone's privilege set\n"), privname); 26051645Scomay break; 26061645Scomay default: 26071645Scomay zperror( 26081645Scomay gettext("failed to determine the zone's privilege set"), 26091645Scomay B_TRUE); 26101645Scomay break; 26111645Scomay } 26121645Scomay free(privname); 26131645Scomay priv_freeset(privs); 26141645Scomay return (err); 26151645Scomay } 26161645Scomay 26171915Sgjelinek static void 26181915Sgjelinek free_local_netifs(int if_cnt, struct net_if **if_list) 26191915Sgjelinek { 26201915Sgjelinek int i; 26211915Sgjelinek 26221915Sgjelinek for (i = 0; i < if_cnt; i++) { 26231915Sgjelinek free(if_list[i]->name); 26241915Sgjelinek free(if_list[i]); 26251915Sgjelinek } 26261915Sgjelinek free(if_list); 26271915Sgjelinek } 26281915Sgjelinek 26291915Sgjelinek /* 26301915Sgjelinek * Get a list of the network interfaces, along with their address families, 26311915Sgjelinek * that are plumbed in the global zone. See if_tcp(7p) for a description 26321915Sgjelinek * of the ioctls used here. 26331915Sgjelinek */ 26341915Sgjelinek static int 26351915Sgjelinek get_local_netifs(int *if_cnt, struct net_if ***if_list) 26361915Sgjelinek { 26371915Sgjelinek int s; 26381915Sgjelinek int i; 26391915Sgjelinek int res = Z_OK; 26401915Sgjelinek int space_needed; 26411915Sgjelinek int cnt = 0; 26421915Sgjelinek struct lifnum if_num; 26431915Sgjelinek struct lifconf if_conf; 26441915Sgjelinek struct lifreq *if_reqp; 26451915Sgjelinek char *if_buf; 26461915Sgjelinek struct net_if **local_ifs = NULL; 26471915Sgjelinek 26481915Sgjelinek *if_cnt = 0; 26491915Sgjelinek *if_list = NULL; 26501915Sgjelinek 26511915Sgjelinek if ((s = socket(SOCKET_AF(AF_INET), SOCK_DGRAM, 0)) < 0) 26521915Sgjelinek return (Z_ERR); 26531915Sgjelinek 26541915Sgjelinek /* 26551915Sgjelinek * Come back here in the unlikely event that the number of interfaces 26561915Sgjelinek * increases between the time we get the count and the time we do the 26571915Sgjelinek * SIOCGLIFCONF ioctl. 26581915Sgjelinek */ 26591915Sgjelinek retry: 26601915Sgjelinek /* Get the number of interfaces. */ 26611915Sgjelinek if_num.lifn_family = AF_UNSPEC; 26621915Sgjelinek if_num.lifn_flags = LIFC_NOXMIT; 26631915Sgjelinek if (ioctl(s, SIOCGLIFNUM, &if_num) < 0) { 26641915Sgjelinek (void) close(s); 26651915Sgjelinek return (Z_ERR); 26661915Sgjelinek } 26671915Sgjelinek 26681915Sgjelinek /* Get the interface configuration list. */ 26691915Sgjelinek space_needed = if_num.lifn_count * sizeof (struct lifreq); 26701915Sgjelinek if ((if_buf = malloc(space_needed)) == NULL) { 26711915Sgjelinek (void) close(s); 26721915Sgjelinek return (Z_ERR); 26731915Sgjelinek } 26741915Sgjelinek if_conf.lifc_family = AF_UNSPEC; 26751915Sgjelinek if_conf.lifc_flags = LIFC_NOXMIT; 26761915Sgjelinek if_conf.lifc_len = space_needed; 26771915Sgjelinek if_conf.lifc_buf = if_buf; 26781915Sgjelinek if (ioctl(s, SIOCGLIFCONF, &if_conf) < 0) { 26791915Sgjelinek free(if_buf); 26801915Sgjelinek /* 26811915Sgjelinek * SIOCGLIFCONF returns EINVAL if the buffer we passed in is 26821915Sgjelinek * too small. In this case go back and get the new if cnt. 26831915Sgjelinek */ 26841915Sgjelinek if (errno == EINVAL) 26851915Sgjelinek goto retry; 26861915Sgjelinek 26871915Sgjelinek (void) close(s); 26881915Sgjelinek return (Z_ERR); 26891915Sgjelinek } 26901915Sgjelinek (void) close(s); 26911915Sgjelinek 26921915Sgjelinek /* Get the name and address family for each interface. */ 26931915Sgjelinek if_reqp = if_conf.lifc_req; 26941915Sgjelinek for (i = 0; i < (if_conf.lifc_len / sizeof (struct lifreq)); i++) { 26951915Sgjelinek struct net_if **p; 26961915Sgjelinek struct lifreq req; 26971915Sgjelinek 26981915Sgjelinek if (strcmp(LOOPBACK_IF, if_reqp->lifr_name) == 0) { 26991915Sgjelinek if_reqp++; 27001915Sgjelinek continue; 27011915Sgjelinek } 27021915Sgjelinek 27031915Sgjelinek if ((s = socket(SOCKET_AF(if_reqp->lifr_addr.ss_family), 27041915Sgjelinek SOCK_DGRAM, 0)) == -1) { 27051915Sgjelinek res = Z_ERR; 27061915Sgjelinek break; 27071915Sgjelinek } 27081915Sgjelinek 27091915Sgjelinek (void) strncpy(req.lifr_name, if_reqp->lifr_name, 27101915Sgjelinek sizeof (req.lifr_name)); 27111915Sgjelinek if (ioctl(s, SIOCGLIFADDR, &req) < 0) { 27121915Sgjelinek (void) close(s); 27131915Sgjelinek if_reqp++; 27141915Sgjelinek continue; 27151915Sgjelinek } 27161915Sgjelinek 27171915Sgjelinek if ((p = (struct net_if **)realloc(local_ifs, 27181915Sgjelinek sizeof (struct net_if *) * (cnt + 1))) == NULL) { 27191915Sgjelinek res = Z_ERR; 27201915Sgjelinek break; 27211915Sgjelinek } 27221915Sgjelinek local_ifs = p; 27231915Sgjelinek 27241915Sgjelinek if ((local_ifs[cnt] = malloc(sizeof (struct net_if))) == NULL) { 27251915Sgjelinek res = Z_ERR; 27261915Sgjelinek break; 27271915Sgjelinek } 27281915Sgjelinek 27291915Sgjelinek if ((local_ifs[cnt]->name = strdup(if_reqp->lifr_name)) 27301915Sgjelinek == NULL) { 27311915Sgjelinek free(local_ifs[cnt]); 27321915Sgjelinek res = Z_ERR; 27331915Sgjelinek break; 27341915Sgjelinek } 27351915Sgjelinek local_ifs[cnt]->af = req.lifr_addr.ss_family; 27361915Sgjelinek cnt++; 27371915Sgjelinek 27381915Sgjelinek (void) close(s); 27391915Sgjelinek if_reqp++; 27401915Sgjelinek } 27411915Sgjelinek 27421915Sgjelinek free(if_buf); 27431915Sgjelinek 27441915Sgjelinek if (res != Z_OK) { 27451915Sgjelinek free_local_netifs(cnt, local_ifs); 27461915Sgjelinek } else { 27471915Sgjelinek *if_cnt = cnt; 27481915Sgjelinek *if_list = local_ifs; 27491915Sgjelinek } 27501915Sgjelinek 27511915Sgjelinek return (res); 27521915Sgjelinek } 27531915Sgjelinek 27541915Sgjelinek static char * 27551915Sgjelinek af2str(int af) 27561915Sgjelinek { 27571915Sgjelinek switch (af) { 27581915Sgjelinek case AF_INET: 27591915Sgjelinek return ("IPv4"); 27601915Sgjelinek case AF_INET6: 27611915Sgjelinek return ("IPv6"); 27621915Sgjelinek default: 27631915Sgjelinek return ("Unknown"); 27641915Sgjelinek } 27651915Sgjelinek } 27661915Sgjelinek 27671915Sgjelinek /* 27681915Sgjelinek * Cross check the network interface name and address family with the 27691915Sgjelinek * interfaces that are set up in the global zone so that we can print the 27701915Sgjelinek * appropriate error message. 27711915Sgjelinek */ 27721915Sgjelinek static void 27731915Sgjelinek print_net_err(char *phys, char *addr, int af, char *msg) 27741915Sgjelinek { 27751915Sgjelinek int i; 27761915Sgjelinek int local_if_cnt = 0; 27771915Sgjelinek struct net_if **local_ifs = NULL; 27781915Sgjelinek boolean_t found_if = B_FALSE; 27791915Sgjelinek boolean_t found_af = B_FALSE; 27801915Sgjelinek 27811915Sgjelinek if (get_local_netifs(&local_if_cnt, &local_ifs) != Z_OK) { 27821915Sgjelinek (void) fprintf(stderr, 27831915Sgjelinek gettext("could not verify %s %s=%s %s=%s\n\t%s\n"), 27841915Sgjelinek "net", "address", addr, "physical", phys, msg); 27851915Sgjelinek return; 27861915Sgjelinek } 27871915Sgjelinek 27881915Sgjelinek for (i = 0; i < local_if_cnt; i++) { 27891915Sgjelinek if (strcmp(phys, local_ifs[i]->name) == 0) { 27901915Sgjelinek found_if = B_TRUE; 27911915Sgjelinek if (af == local_ifs[i]->af) { 27921915Sgjelinek found_af = B_TRUE; 27931915Sgjelinek break; 27941915Sgjelinek } 27951915Sgjelinek } 27961915Sgjelinek } 27971915Sgjelinek 27981915Sgjelinek free_local_netifs(local_if_cnt, local_ifs); 27991915Sgjelinek 28001915Sgjelinek if (!found_if) { 28011915Sgjelinek (void) fprintf(stderr, 28021915Sgjelinek gettext("could not verify %s %s=%s\n\t" 28031915Sgjelinek "network interface %s is not plumbed in the global zone\n"), 28041915Sgjelinek "net", "physical", phys, phys); 28051915Sgjelinek return; 28061915Sgjelinek } 28071915Sgjelinek 28081915Sgjelinek /* 28091915Sgjelinek * Print this error if we were unable to find the address family 28101915Sgjelinek * for this interface. If the af variable is not initialized to 28111915Sgjelinek * to something meaningful by the caller (not AF_UNSPEC) then we 28121915Sgjelinek * also skip this message since it wouldn't be informative. 28131915Sgjelinek */ 28141915Sgjelinek if (!found_af && af != AF_UNSPEC) { 28151915Sgjelinek (void) fprintf(stderr, 28161915Sgjelinek gettext("could not verify %s %s=%s %s=%s\n\tthe %s address " 28173448Sdh155122 "family is not configured on this network interface in " 28183448Sdh155122 "the\n\tglobal zone\n"), 28191915Sgjelinek "net", "address", addr, "physical", phys, af2str(af)); 28201915Sgjelinek return; 28211915Sgjelinek } 28221915Sgjelinek 28231915Sgjelinek (void) fprintf(stderr, 28241915Sgjelinek gettext("could not verify %s %s=%s %s=%s\n\t%s\n"), 28251915Sgjelinek "net", "address", addr, "physical", phys, msg); 28261915Sgjelinek } 28271915Sgjelinek 28281645Scomay static int 28293339Szt129084 verify_handle(int cmd_num, zone_dochandle_t handle, char *argv[]) 28300Sstevel@tonic-gate { 28310Sstevel@tonic-gate struct zone_nwiftab nwiftab; 28320Sstevel@tonic-gate int return_code = Z_OK; 28330Sstevel@tonic-gate int err; 2834766Scarlsonj boolean_t in_alt_root; 28353448Sdh155122 zone_iptype_t iptype; 28363448Sdh155122 int fd; 28370Sstevel@tonic-gate 2838766Scarlsonj in_alt_root = zonecfg_in_alt_root(); 2839766Scarlsonj if (in_alt_root) 2840766Scarlsonj goto no_net; 2841766Scarlsonj 28423448Sdh155122 if ((err = zonecfg_get_iptype(handle, &iptype)) != Z_OK) { 28433448Sdh155122 errno = err; 28443448Sdh155122 zperror(cmd_to_str(cmd_num), B_TRUE); 28453448Sdh155122 zonecfg_fini_handle(handle); 28463448Sdh155122 return (Z_ERR); 28473448Sdh155122 } 28480Sstevel@tonic-gate if ((err = zonecfg_setnwifent(handle)) != Z_OK) { 28490Sstevel@tonic-gate errno = err; 28500Sstevel@tonic-gate zperror(cmd_to_str(cmd_num), B_TRUE); 28510Sstevel@tonic-gate zonecfg_fini_handle(handle); 28520Sstevel@tonic-gate return (Z_ERR); 28530Sstevel@tonic-gate } 28540Sstevel@tonic-gate while (zonecfg_getnwifent(handle, &nwiftab) == Z_OK) { 28550Sstevel@tonic-gate struct lifreq lifr; 28561915Sgjelinek sa_family_t af = AF_UNSPEC; 28573448Sdh155122 char dl_owner_zname[ZONENAME_MAX]; 28583448Sdh155122 zoneid_t dl_owner_zid; 28593448Sdh155122 zoneid_t target_zid; 28603448Sdh155122 int res; 28610Sstevel@tonic-gate 28620Sstevel@tonic-gate /* skip any loopback interfaces */ 28630Sstevel@tonic-gate if (strcmp(nwiftab.zone_nwif_physical, "lo0") == 0) 28640Sstevel@tonic-gate continue; 28653448Sdh155122 switch (iptype) { 28663448Sdh155122 case ZS_SHARED: 28673448Sdh155122 if ((res = zonecfg_valid_net_address( 28683448Sdh155122 nwiftab.zone_nwif_address, &lifr)) != Z_OK) { 28693448Sdh155122 print_net_err(nwiftab.zone_nwif_physical, 28703448Sdh155122 nwiftab.zone_nwif_address, af, 28713448Sdh155122 zonecfg_strerror(res)); 28723448Sdh155122 return_code = Z_ERR; 28733448Sdh155122 continue; 28743448Sdh155122 } 28753448Sdh155122 af = lifr.lifr_addr.ss_family; 28763448Sdh155122 if (!zonecfg_ifname_exists(af, 28773448Sdh155122 nwiftab.zone_nwif_physical)) { 28783448Sdh155122 /* 28793448Sdh155122 * The interface failed to come up. We continue 28803448Sdh155122 * on anyway for the sake of consistency: a 28813448Sdh155122 * zone is not shut down if the interface fails 28823448Sdh155122 * any time after boot, nor does the global zone 28833448Sdh155122 * fail to boot if an interface fails. 28843448Sdh155122 */ 28853448Sdh155122 (void) fprintf(stderr, 28863448Sdh155122 gettext("WARNING: skipping network " 28873448Sdh155122 "interface '%s' which may not be " 28883448Sdh155122 "present/plumbed in the global " 28893448Sdh155122 "zone.\n"), 28903448Sdh155122 nwiftab.zone_nwif_physical); 28913448Sdh155122 } 28923448Sdh155122 break; 28933448Sdh155122 case ZS_EXCLUSIVE: 28943448Sdh155122 /* Warning if it exists for either IPv4 or IPv6 */ 28953448Sdh155122 28963448Sdh155122 if (zonecfg_ifname_exists(AF_INET, 28973448Sdh155122 nwiftab.zone_nwif_physical) || 28983448Sdh155122 zonecfg_ifname_exists(AF_INET6, 28993448Sdh155122 nwiftab.zone_nwif_physical)) { 29003448Sdh155122 (void) fprintf(stderr, 29013448Sdh155122 gettext("WARNING: skipping network " 29023448Sdh155122 "interface '%s' which is used in the " 29033448Sdh155122 "global zone.\n"), 29043448Sdh155122 nwiftab.zone_nwif_physical); 29053448Sdh155122 break; 29063448Sdh155122 } 29072611Svp157776 /* 29083448Sdh155122 * Verify that the physical interface can 29093448Sdh155122 * be opened 29103448Sdh155122 */ 29113448Sdh155122 fd = ifname_open(nwiftab.zone_nwif_physical); 29123448Sdh155122 if (fd == -1) { 29133448Sdh155122 (void) fprintf(stderr, 29143448Sdh155122 gettext("WARNING: skipping network " 29153448Sdh155122 "interface '%s' which cannot be opened.\n"), 29163448Sdh155122 nwiftab.zone_nwif_physical); 29173448Sdh155122 break; 29183448Sdh155122 } else { 29193448Sdh155122 (void) close(fd); 29203448Sdh155122 } 29213448Sdh155122 /* 29223448Sdh155122 * Verify whether the physical interface is already 29233448Sdh155122 * used by a zone. 29243448Sdh155122 */ 29253448Sdh155122 dl_owner_zid = ALL_ZONES; 29263448Sdh155122 if (zone_check_datalink(&dl_owner_zid, 29273448Sdh155122 nwiftab.zone_nwif_physical) != 0) 29283448Sdh155122 break; 29293448Sdh155122 29303448Sdh155122 /* 29313448Sdh155122 * If the zone being verified is 29323448Sdh155122 * running and owns the interface 29333448Sdh155122 */ 29343448Sdh155122 target_zid = getzoneidbyname(target_zone); 29353448Sdh155122 if (target_zid == dl_owner_zid) 29363448Sdh155122 break; 29373448Sdh155122 29383448Sdh155122 /* Zone id match failed, use name to check */ 29393448Sdh155122 if (getzonenamebyid(dl_owner_zid, dl_owner_zname, 29403448Sdh155122 ZONENAME_MAX) < 0) { 29413448Sdh155122 /* No name, show ID instead */ 29423448Sdh155122 (void) snprintf(dl_owner_zname, ZONENAME_MAX, 29433448Sdh155122 "<%d>", dl_owner_zid); 29443448Sdh155122 } else if (strcmp(dl_owner_zname, target_zone) == 0) 29453448Sdh155122 break; 29463448Sdh155122 29473448Sdh155122 /* 29483448Sdh155122 * Note here we only report a warning that 29493448Sdh155122 * the interface is already in use by another 29503448Sdh155122 * running zone, and the verify process just 29513448Sdh155122 * goes on, if the interface is still in use 29523448Sdh155122 * when this zone really boots up, zoneadmd 29533448Sdh155122 * will find it. If the name of the zone which 29543448Sdh155122 * owns this interface cannot be determined, 29553448Sdh155122 * then it is not possible to determine if there 29563448Sdh155122 * is a conflict so just report it as a warning. 29572611Svp157776 */ 29582611Svp157776 (void) fprintf(stderr, 29593448Sdh155122 gettext("WARNING: skipping network interface " 29603448Sdh155122 "'%s' which is used by the non-global zone " 29613448Sdh155122 "'%s'.\n"), nwiftab.zone_nwif_physical, 29623448Sdh155122 dl_owner_zname); 29633448Sdh155122 break; 29640Sstevel@tonic-gate } 29650Sstevel@tonic-gate } 29660Sstevel@tonic-gate (void) zonecfg_endnwifent(handle); 2967766Scarlsonj no_net: 29680Sstevel@tonic-gate 29691931Sgjelinek /* verify that lofs has not been excluded from the kernel */ 29702078Sgjelinek if (!(cmd_num == CMD_DETACH || cmd_num == CMD_ATTACH || 29712078Sgjelinek cmd_num == CMD_MOVE || cmd_num == CMD_CLONE) && 29722078Sgjelinek modctl(MODLOAD, 1, "fs/lofs", NULL) != 0) { 29731931Sgjelinek if (errno == ENXIO) 29741931Sgjelinek (void) fprintf(stderr, gettext("could not verify " 29751931Sgjelinek "lofs(7FS): possibly excluded in /etc/system\n")); 29761931Sgjelinek else 29771931Sgjelinek (void) fprintf(stderr, gettext("could not verify " 29781931Sgjelinek "lofs(7FS): %s\n"), strerror(errno)); 29791931Sgjelinek return_code = Z_ERR; 29801931Sgjelinek } 29811931Sgjelinek 29820Sstevel@tonic-gate if (verify_filesystems(handle) != Z_OK) 29830Sstevel@tonic-gate return_code = Z_ERR; 2984823Sgjelinek if (verify_ipd(handle) != Z_OK) 2985823Sgjelinek return_code = Z_ERR; 2986766Scarlsonj if (!in_alt_root && verify_rctls(handle) != Z_OK) 29870Sstevel@tonic-gate return_code = Z_ERR; 2988766Scarlsonj if (!in_alt_root && verify_pool(handle) != Z_OK) 29890Sstevel@tonic-gate return_code = Z_ERR; 29903339Szt129084 if (!in_alt_root && verify_brand(handle, cmd_num, argv) != Z_OK) 29912712Snn35248 return_code = Z_ERR; 2992789Sahrens if (!in_alt_root && verify_datasets(handle) != Z_OK) 2993789Sahrens return_code = Z_ERR; 29941645Scomay 29951645Scomay /* 29961645Scomay * As the "mount" command is used for patching/upgrading of zones 29971645Scomay * or other maintenance processes, the zone's privilege set is not 29981645Scomay * checked in this case. Instead, the default, safe set of 29991645Scomay * privileges will be used when this zone is created in the 30001645Scomay * kernel. 30011645Scomay */ 30021645Scomay if (!in_alt_root && cmd_num != CMD_MOUNT && 30031645Scomay verify_limitpriv(handle) != Z_OK) 30041645Scomay return_code = Z_ERR; 30052078Sgjelinek 30062078Sgjelinek return (return_code); 30072078Sgjelinek } 30082078Sgjelinek 30092078Sgjelinek static int 30103339Szt129084 verify_details(int cmd_num, char *argv[]) 30112078Sgjelinek { 30122078Sgjelinek zone_dochandle_t handle; 30132078Sgjelinek char zonepath[MAXPATHLEN], checkpath[MAXPATHLEN]; 30142078Sgjelinek int return_code = Z_OK; 30152078Sgjelinek int err; 30162078Sgjelinek 30172078Sgjelinek if ((handle = zonecfg_init_handle()) == NULL) { 30182078Sgjelinek zperror(cmd_to_str(cmd_num), B_TRUE); 30192078Sgjelinek return (Z_ERR); 30202078Sgjelinek } 30212078Sgjelinek if ((err = zonecfg_get_handle(target_zone, handle)) != Z_OK) { 30222078Sgjelinek errno = err; 30232078Sgjelinek zperror(cmd_to_str(cmd_num), B_TRUE); 30242078Sgjelinek zonecfg_fini_handle(handle); 30252078Sgjelinek return (Z_ERR); 30262078Sgjelinek } 30272078Sgjelinek if ((err = zonecfg_get_zonepath(handle, zonepath, sizeof (zonepath))) != 30282078Sgjelinek Z_OK) { 30292078Sgjelinek errno = err; 30302078Sgjelinek zperror(cmd_to_str(cmd_num), B_TRUE); 30312078Sgjelinek zonecfg_fini_handle(handle); 30322078Sgjelinek return (Z_ERR); 30332078Sgjelinek } 30342078Sgjelinek /* 30352078Sgjelinek * zonecfg_get_zonepath() gets its data from the XML repository. 30362078Sgjelinek * Verify this against the index file, which is checked first by 30372078Sgjelinek * zone_get_zonepath(). If they don't match, bail out. 30382078Sgjelinek */ 30392078Sgjelinek if ((err = zone_get_zonepath(target_zone, checkpath, 30402078Sgjelinek sizeof (checkpath))) != Z_OK) { 30412078Sgjelinek errno = err; 30422078Sgjelinek zperror2(target_zone, gettext("could not get zone path")); 30433716Sgjelinek zonecfg_fini_handle(handle); 30442078Sgjelinek return (Z_ERR); 30452078Sgjelinek } 30462078Sgjelinek if (strcmp(zonepath, checkpath) != 0) { 30472078Sgjelinek /* 30482078Sgjelinek * TRANSLATION_NOTE 30492078Sgjelinek * XML and zonepath are literals that should not be translated. 30502078Sgjelinek */ 30512078Sgjelinek (void) fprintf(stderr, gettext("The XML repository has " 30522078Sgjelinek "zonepath '%s',\nbut the index file has zonepath '%s'.\n" 30532078Sgjelinek "These must match, so fix the incorrect entry.\n"), 30542078Sgjelinek zonepath, checkpath); 30553716Sgjelinek zonecfg_fini_handle(handle); 30562078Sgjelinek return (Z_ERR); 30572078Sgjelinek } 30582078Sgjelinek if (validate_zonepath(zonepath, cmd_num) != Z_OK) { 30592078Sgjelinek (void) fprintf(stderr, gettext("could not verify zonepath %s " 30602078Sgjelinek "because of the above errors.\n"), zonepath); 30612078Sgjelinek return_code = Z_ERR; 30622078Sgjelinek } 30632078Sgjelinek 30643339Szt129084 if (verify_handle(cmd_num, handle, argv) != Z_OK) 30652078Sgjelinek return_code = Z_ERR; 30662078Sgjelinek 30670Sstevel@tonic-gate zonecfg_fini_handle(handle); 30680Sstevel@tonic-gate if (return_code == Z_ERR) 30690Sstevel@tonic-gate (void) fprintf(stderr, 30700Sstevel@tonic-gate gettext("%s: zone %s failed to verify\n"), 30710Sstevel@tonic-gate execname, target_zone); 30720Sstevel@tonic-gate return (return_code); 30730Sstevel@tonic-gate } 30740Sstevel@tonic-gate 30750Sstevel@tonic-gate static int 30760Sstevel@tonic-gate verify_func(int argc, char *argv[]) 30770Sstevel@tonic-gate { 30780Sstevel@tonic-gate int arg; 30790Sstevel@tonic-gate 30800Sstevel@tonic-gate optind = 0; 30810Sstevel@tonic-gate if ((arg = getopt(argc, argv, "?")) != EOF) { 30820Sstevel@tonic-gate switch (arg) { 30830Sstevel@tonic-gate case '?': 30840Sstevel@tonic-gate sub_usage(SHELP_VERIFY, CMD_VERIFY); 30850Sstevel@tonic-gate return (optopt == '?' ? Z_OK : Z_USAGE); 30860Sstevel@tonic-gate default: 30870Sstevel@tonic-gate sub_usage(SHELP_VERIFY, CMD_VERIFY); 30880Sstevel@tonic-gate return (Z_USAGE); 30890Sstevel@tonic-gate } 30900Sstevel@tonic-gate } 30910Sstevel@tonic-gate if (argc > optind) { 30920Sstevel@tonic-gate sub_usage(SHELP_VERIFY, CMD_VERIFY); 30930Sstevel@tonic-gate return (Z_USAGE); 30940Sstevel@tonic-gate } 30952712Snn35248 if (sanity_check(target_zone, CMD_VERIFY, B_FALSE, B_FALSE, B_FALSE) 30962712Snn35248 != Z_OK) 30970Sstevel@tonic-gate return (Z_ERR); 30983339Szt129084 return (verify_details(CMD_VERIFY, argv)); 30990Sstevel@tonic-gate } 31000Sstevel@tonic-gate 31012712Snn35248 static int 31022712Snn35248 addopt(char *buf, int opt, char *optarg, size_t bufsize) 31032712Snn35248 { 31042712Snn35248 char optstring[4]; 31052712Snn35248 31062712Snn35248 if (opt > 0) 31072712Snn35248 (void) sprintf(optstring, " -%c", opt); 31082712Snn35248 else 31092712Snn35248 (void) strcpy(optstring, " "); 31102712Snn35248 31112712Snn35248 if ((strlcat(buf, optstring, bufsize) > bufsize)) 31122712Snn35248 return (Z_ERR); 31132712Snn35248 if ((optarg != NULL) && (strlcat(buf, optarg, bufsize) > bufsize)) 31142712Snn35248 return (Z_ERR); 31152712Snn35248 return (Z_OK); 31162712Snn35248 } 31170Sstevel@tonic-gate 31180Sstevel@tonic-gate static int 31190Sstevel@tonic-gate install_func(int argc, char *argv[]) 31200Sstevel@tonic-gate { 31212712Snn35248 char cmdbuf[MAXPATHLEN]; 31220Sstevel@tonic-gate int lockfd; 31232712Snn35248 int arg, err, subproc_err; 31240Sstevel@tonic-gate char zonepath[MAXPATHLEN]; 31252727Sedp brand_handle_t bh = NULL; 31260Sstevel@tonic-gate int status; 31271867Sgjelinek boolean_t nodataset = B_FALSE; 31282712Snn35248 char opts[128]; 31292712Snn35248 31302712Snn35248 if (target_zone == NULL) { 31312712Snn35248 sub_usage(SHELP_INSTALL, CMD_INSTALL); 31322712Snn35248 return (Z_USAGE); 31332712Snn35248 } 31340Sstevel@tonic-gate 3135766Scarlsonj if (zonecfg_in_alt_root()) { 3136766Scarlsonj zerror(gettext("cannot install zone in alternate root")); 3137766Scarlsonj return (Z_ERR); 3138766Scarlsonj } 3139766Scarlsonj 31402712Snn35248 if ((err = zone_get_zonepath(target_zone, zonepath, 31412712Snn35248 sizeof (zonepath))) != Z_OK) { 31422712Snn35248 errno = err; 31432712Snn35248 zperror2(target_zone, gettext("could not get zone path")); 31442712Snn35248 return (Z_ERR); 31452712Snn35248 } 31462712Snn35248 31472712Snn35248 /* Fetch the install command from the brand configuration. */ 31482727Sedp if ((bh = brand_open(target_brand)) == NULL) { 31492712Snn35248 zerror(gettext("missing or invalid brand")); 31502712Snn35248 return (Z_ERR); 31512712Snn35248 } 31522712Snn35248 31532712Snn35248 (void) strcpy(cmdbuf, EXEC_PREFIX); 31542727Sedp if (brand_get_install(bh, target_zone, zonepath, cmdbuf + EXEC_LEN, 31552712Snn35248 sizeof (cmdbuf) - EXEC_LEN, 0, NULL) != 0) { 31562712Snn35248 zerror("invalid brand configuration: missing install resource"); 31572727Sedp brand_close(bh); 31582712Snn35248 return (Z_ERR); 31592712Snn35248 } 31602712Snn35248 31612712Snn35248 (void) strcpy(opts, "?x:"); 31622712Snn35248 if (!is_native_zone) { 31632712Snn35248 /* 31642712Snn35248 * Fetch the list of recognized command-line options from 31652712Snn35248 * the brand configuration file. 31662712Snn35248 */ 31672727Sedp if (brand_get_installopts(bh, opts + strlen(opts), 31682712Snn35248 sizeof (opts) - strlen(opts)) != 0) { 31692712Snn35248 zerror("invalid brand configuration: missing " 31702712Snn35248 "install options resource"); 31712727Sedp brand_close(bh); 31722712Snn35248 return (Z_ERR); 31732712Snn35248 } 31742712Snn35248 } 31752727Sedp brand_close(bh); 31762712Snn35248 31770Sstevel@tonic-gate optind = 0; 31782712Snn35248 while ((arg = getopt(argc, argv, opts)) != EOF) { 31790Sstevel@tonic-gate switch (arg) { 31800Sstevel@tonic-gate case '?': 31810Sstevel@tonic-gate sub_usage(SHELP_INSTALL, CMD_INSTALL); 31820Sstevel@tonic-gate return (optopt == '?' ? Z_OK : Z_USAGE); 31831867Sgjelinek case 'x': 31841867Sgjelinek if (strcmp(optarg, "nodataset") != 0) { 31851867Sgjelinek sub_usage(SHELP_INSTALL, CMD_INSTALL); 31861867Sgjelinek return (Z_USAGE); 31871867Sgjelinek } 31881867Sgjelinek nodataset = B_TRUE; 31891867Sgjelinek break; 31900Sstevel@tonic-gate default: 31912712Snn35248 if (is_native_zone) { 31922712Snn35248 sub_usage(SHELP_INSTALL, CMD_INSTALL); 31932712Snn35248 return (Z_USAGE); 31942712Snn35248 } 31952712Snn35248 31962712Snn35248 /* 31972712Snn35248 * This option isn't for zoneadm, so append it to 31982712Snn35248 * the command line passed to the brand-specific 31992712Snn35248 * install routine. 32002712Snn35248 */ 32012712Snn35248 if (addopt(cmdbuf, optopt, optarg, 32022712Snn35248 sizeof (cmdbuf)) != Z_OK) { 32032712Snn35248 zerror("Install command line too long"); 32042712Snn35248 return (Z_ERR); 32052712Snn35248 } 32062712Snn35248 break; 32070Sstevel@tonic-gate } 32080Sstevel@tonic-gate } 32092712Snn35248 32102712Snn35248 if (!is_native_zone) { 32112712Snn35248 for (; optind < argc; optind++) { 32122712Snn35248 if (addopt(cmdbuf, 0, argv[optind], 32132712Snn35248 sizeof (cmdbuf)) != Z_OK) { 32142712Snn35248 zerror("Install command line too long"); 32152712Snn35248 return (Z_ERR); 32162712Snn35248 } 32172712Snn35248 } 32182712Snn35248 } 32192712Snn35248 32202712Snn35248 if (sanity_check(target_zone, CMD_INSTALL, B_FALSE, B_TRUE, B_FALSE) 32212712Snn35248 != Z_OK) 32220Sstevel@tonic-gate return (Z_ERR); 32233339Szt129084 if (verify_details(CMD_INSTALL, argv) != Z_OK) 32240Sstevel@tonic-gate return (Z_ERR); 32250Sstevel@tonic-gate 32260Sstevel@tonic-gate if (grab_lock_file(target_zone, &lockfd) != Z_OK) { 32270Sstevel@tonic-gate zerror(gettext("another %s may have an operation in progress."), 32281300Sgjelinek "zoneadm"); 32290Sstevel@tonic-gate return (Z_ERR); 32300Sstevel@tonic-gate } 32310Sstevel@tonic-gate err = zone_set_state(target_zone, ZONE_STATE_INCOMPLETE); 32320Sstevel@tonic-gate if (err != Z_OK) { 32330Sstevel@tonic-gate errno = err; 32340Sstevel@tonic-gate zperror2(target_zone, gettext("could not set state")); 32350Sstevel@tonic-gate goto done; 32360Sstevel@tonic-gate } 32370Sstevel@tonic-gate 32382712Snn35248 if (!nodataset) 32392712Snn35248 create_zfs_zonepath(zonepath); 32402712Snn35248 32410Sstevel@tonic-gate /* 32420Sstevel@tonic-gate * According to the Application Packaging Developer's Guide, a 32430Sstevel@tonic-gate * "checkinstall" script when included in a package is executed as 32440Sstevel@tonic-gate * the user "install", if such a user exists, or by the user 32450Sstevel@tonic-gate * "nobody". In order to support this dubious behavior, the path 32460Sstevel@tonic-gate * to the zone being constructed is opened up during the life of 32470Sstevel@tonic-gate * the command laying down the zone's root file system. Once this 32480Sstevel@tonic-gate * has completed, regardless of whether it was successful, the 32490Sstevel@tonic-gate * path to the zone is again restricted. 32500Sstevel@tonic-gate */ 32510Sstevel@tonic-gate if (chmod(zonepath, DEFAULT_DIR_MODE) != 0) { 32520Sstevel@tonic-gate zperror(zonepath, B_FALSE); 32530Sstevel@tonic-gate err = Z_ERR; 32540Sstevel@tonic-gate goto done; 32550Sstevel@tonic-gate } 32560Sstevel@tonic-gate 32572712Snn35248 if (is_native_zone) 32582712Snn35248 status = do_subproc(cmdbuf); 32592712Snn35248 else 32602712Snn35248 status = do_subproc_interactive(cmdbuf); 32612712Snn35248 32620Sstevel@tonic-gate if (chmod(zonepath, S_IRWXU) != 0) { 32630Sstevel@tonic-gate zperror(zonepath, B_FALSE); 32640Sstevel@tonic-gate err = Z_ERR; 32650Sstevel@tonic-gate goto done; 32660Sstevel@tonic-gate } 32672712Snn35248 if ((subproc_err = 32682712Snn35248 subproc_status(gettext("brand-specific installation"), status, 32692712Snn35248 B_FALSE)) != ZONE_SUBPROC_OK) { 32702712Snn35248 err = Z_ERR; 32710Sstevel@tonic-gate goto done; 32722712Snn35248 } 32730Sstevel@tonic-gate 32740Sstevel@tonic-gate if ((err = zone_set_state(target_zone, ZONE_STATE_INSTALLED)) != Z_OK) { 32750Sstevel@tonic-gate errno = err; 32760Sstevel@tonic-gate zperror2(target_zone, gettext("could not set state")); 32770Sstevel@tonic-gate goto done; 32780Sstevel@tonic-gate } 32790Sstevel@tonic-gate 32800Sstevel@tonic-gate done: 32812712Snn35248 /* 32822712Snn35248 * If the install script exited with ZONE_SUBPROC_USAGE or 32832712Snn35248 * ZONE_SUBPROC_NOTCOMPLETE, try to clean up the zone and leave the 32842712Snn35248 * zone in the CONFIGURED state so that another install can be 32852712Snn35248 * attempted without requiring an uninstall first. 32862712Snn35248 */ 32872712Snn35248 if ((subproc_err == ZONE_SUBPROC_USAGE) || 32882712Snn35248 (subproc_err == ZONE_SUBPROC_NOTCOMPLETE)) { 32892712Snn35248 if ((err = cleanup_zonepath(zonepath, B_FALSE)) != Z_OK) { 32902712Snn35248 errno = err; 32912712Snn35248 zperror2(target_zone, 32922712Snn35248 gettext("cleaning up zonepath failed")); 32932712Snn35248 } else if ((err = zone_set_state(target_zone, 32942712Snn35248 ZONE_STATE_CONFIGURED)) != Z_OK) { 32952712Snn35248 errno = err; 32962712Snn35248 zperror2(target_zone, gettext("could not set state")); 32972712Snn35248 } 32982712Snn35248 } 32992712Snn35248 33000Sstevel@tonic-gate release_lock_file(lockfd); 33010Sstevel@tonic-gate return ((err == Z_OK) ? Z_OK : Z_ERR); 33020Sstevel@tonic-gate } 33030Sstevel@tonic-gate 33040Sstevel@tonic-gate /* 33051300Sgjelinek * Check that the inherited pkg dirs are the same for the clone and its source. 33061300Sgjelinek * The easiest way to do that is check that the list of ipds is the same 33071300Sgjelinek * by matching each one against the other. This algorithm should be fine since 33081300Sgjelinek * the list of ipds should not be that long. 33091300Sgjelinek */ 33101300Sgjelinek static int 33111300Sgjelinek valid_ipd_clone(zone_dochandle_t s_handle, char *source_zone, 33121300Sgjelinek zone_dochandle_t t_handle, char *target_zone) 33131300Sgjelinek { 33141300Sgjelinek int err; 33151300Sgjelinek int res = Z_OK; 33161300Sgjelinek int s_cnt = 0; 33171300Sgjelinek int t_cnt = 0; 33181300Sgjelinek struct zone_fstab s_fstab; 33191300Sgjelinek struct zone_fstab t_fstab; 33201300Sgjelinek 33211300Sgjelinek /* 33221300Sgjelinek * First check the source of the clone against the target. 33231300Sgjelinek */ 33241300Sgjelinek if ((err = zonecfg_setipdent(s_handle)) != Z_OK) { 33251300Sgjelinek errno = err; 33261300Sgjelinek zperror2(source_zone, gettext("could not enumerate " 33271300Sgjelinek "inherit-pkg-dirs")); 33281300Sgjelinek return (Z_ERR); 33291300Sgjelinek } 33301300Sgjelinek 33311300Sgjelinek while (zonecfg_getipdent(s_handle, &s_fstab) == Z_OK) { 33321300Sgjelinek boolean_t match = B_FALSE; 33331300Sgjelinek 33341300Sgjelinek s_cnt++; 33351300Sgjelinek 33361300Sgjelinek if ((err = zonecfg_setipdent(t_handle)) != Z_OK) { 33371300Sgjelinek errno = err; 33381300Sgjelinek zperror2(target_zone, gettext("could not enumerate " 33391300Sgjelinek "inherit-pkg-dirs")); 33401300Sgjelinek (void) zonecfg_endipdent(s_handle); 33411300Sgjelinek return (Z_ERR); 33421300Sgjelinek } 33431300Sgjelinek 33441300Sgjelinek while (zonecfg_getipdent(t_handle, &t_fstab) == Z_OK) { 33451300Sgjelinek if (strcmp(s_fstab.zone_fs_dir, t_fstab.zone_fs_dir) 33461300Sgjelinek == 0) { 33471300Sgjelinek match = B_TRUE; 33481300Sgjelinek break; 33491300Sgjelinek } 33501300Sgjelinek } 33511300Sgjelinek (void) zonecfg_endipdent(t_handle); 33521300Sgjelinek 33531300Sgjelinek if (!match) { 33541300Sgjelinek (void) fprintf(stderr, gettext("inherit-pkg-dir " 33551300Sgjelinek "'%s' is not configured in zone %s.\n"), 33561300Sgjelinek s_fstab.zone_fs_dir, target_zone); 33571300Sgjelinek res = Z_ERR; 33581300Sgjelinek } 33591300Sgjelinek } 33601300Sgjelinek 33611300Sgjelinek (void) zonecfg_endipdent(s_handle); 33621300Sgjelinek 33631300Sgjelinek /* skip the next check if we already have errors */ 33641300Sgjelinek if (res == Z_ERR) 33651300Sgjelinek return (res); 33661300Sgjelinek 33671300Sgjelinek /* 33681300Sgjelinek * Now check the number of ipds in the target so we can verify 33691300Sgjelinek * that the source is not a subset of the target. 33701300Sgjelinek */ 33711300Sgjelinek if ((err = zonecfg_setipdent(t_handle)) != Z_OK) { 33721300Sgjelinek errno = err; 33731300Sgjelinek zperror2(target_zone, gettext("could not enumerate " 33741300Sgjelinek "inherit-pkg-dirs")); 33751300Sgjelinek return (Z_ERR); 33761300Sgjelinek } 33771300Sgjelinek 33781300Sgjelinek while (zonecfg_getipdent(t_handle, &t_fstab) == Z_OK) 33791300Sgjelinek t_cnt++; 33801300Sgjelinek 33811300Sgjelinek (void) zonecfg_endipdent(t_handle); 33821300Sgjelinek 33831300Sgjelinek if (t_cnt != s_cnt) { 33841300Sgjelinek (void) fprintf(stderr, gettext("Zone %s is configured " 33851300Sgjelinek "with inherit-pkg-dirs that are not configured in zone " 33861300Sgjelinek "%s.\n"), target_zone, source_zone); 33871300Sgjelinek res = Z_ERR; 33881300Sgjelinek } 33891300Sgjelinek 33901300Sgjelinek return (res); 33911300Sgjelinek } 33921300Sgjelinek 33931300Sgjelinek static void 33941300Sgjelinek warn_dev_match(zone_dochandle_t s_handle, char *source_zone, 33951300Sgjelinek zone_dochandle_t t_handle, char *target_zone) 33961300Sgjelinek { 33971300Sgjelinek int err; 33981300Sgjelinek struct zone_devtab s_devtab; 33991300Sgjelinek struct zone_devtab t_devtab; 34001300Sgjelinek 34011300Sgjelinek if ((err = zonecfg_setdevent(t_handle)) != Z_OK) { 34021300Sgjelinek errno = err; 34031300Sgjelinek zperror2(target_zone, gettext("could not enumerate devices")); 34041300Sgjelinek return; 34051300Sgjelinek } 34061300Sgjelinek 34071300Sgjelinek while (zonecfg_getdevent(t_handle, &t_devtab) == Z_OK) { 34081300Sgjelinek if ((err = zonecfg_setdevent(s_handle)) != Z_OK) { 34091300Sgjelinek errno = err; 34101300Sgjelinek zperror2(source_zone, 34111300Sgjelinek gettext("could not enumerate devices")); 34121300Sgjelinek (void) zonecfg_enddevent(t_handle); 34131300Sgjelinek return; 34141300Sgjelinek } 34151300Sgjelinek 34161300Sgjelinek while (zonecfg_getdevent(s_handle, &s_devtab) == Z_OK) { 34171300Sgjelinek /* 34181300Sgjelinek * Use fnmatch to catch the case where wildcards 34191300Sgjelinek * were used in one zone and the other has an 34201300Sgjelinek * explicit entry (e.g. /dev/dsk/c0t0d0s6 vs. 34211300Sgjelinek * /dev/\*dsk/c0t0d0s6). 34221300Sgjelinek */ 34231300Sgjelinek if (fnmatch(t_devtab.zone_dev_match, 34241300Sgjelinek s_devtab.zone_dev_match, FNM_PATHNAME) == 0 || 34251300Sgjelinek fnmatch(s_devtab.zone_dev_match, 34261300Sgjelinek t_devtab.zone_dev_match, FNM_PATHNAME) == 0) { 34271300Sgjelinek (void) fprintf(stderr, 34281300Sgjelinek gettext("WARNING: device '%s' " 34291300Sgjelinek "is configured in both zones.\n"), 34301300Sgjelinek t_devtab.zone_dev_match); 34311300Sgjelinek break; 34321300Sgjelinek } 34331300Sgjelinek } 34341300Sgjelinek (void) zonecfg_enddevent(s_handle); 34351300Sgjelinek } 34361300Sgjelinek 34371300Sgjelinek (void) zonecfg_enddevent(t_handle); 34381300Sgjelinek } 34391300Sgjelinek 34401300Sgjelinek /* 34411300Sgjelinek * Check if the specified mount option (opt) is contained within the 34421300Sgjelinek * options string. 34431300Sgjelinek */ 34441300Sgjelinek static boolean_t 34451300Sgjelinek opt_match(char *opt, char *options) 34461300Sgjelinek { 34471300Sgjelinek char *p; 34481300Sgjelinek char *lastp; 34491300Sgjelinek 34501300Sgjelinek if ((p = strtok_r(options, ",", &lastp)) != NULL) { 34511300Sgjelinek if (strcmp(p, opt) == 0) 34521300Sgjelinek return (B_TRUE); 34531300Sgjelinek while ((p = strtok_r(NULL, ",", &lastp)) != NULL) { 34541300Sgjelinek if (strcmp(p, opt) == 0) 34551300Sgjelinek return (B_TRUE); 34561300Sgjelinek } 34571300Sgjelinek } 34581300Sgjelinek 34591300Sgjelinek return (B_FALSE); 34601300Sgjelinek } 34611300Sgjelinek 34621867Sgjelinek #define RW_LOFS "WARNING: read-write lofs file system on '%s' is configured " \ 34631300Sgjelinek "in both zones.\n" 34641300Sgjelinek 34651300Sgjelinek static void 34661300Sgjelinek print_fs_warnings(struct zone_fstab *s_fstab, struct zone_fstab *t_fstab) 34671300Sgjelinek { 34681300Sgjelinek /* 34691300Sgjelinek * It is ok to have shared lofs mounted fs but we want to warn if 34701300Sgjelinek * either is rw since this will effect the other zone. 34711300Sgjelinek */ 34721300Sgjelinek if (strcmp(t_fstab->zone_fs_type, "lofs") == 0) { 34731300Sgjelinek zone_fsopt_t *optp; 34741300Sgjelinek 34751300Sgjelinek /* The default is rw so no options means rw */ 34761300Sgjelinek if (t_fstab->zone_fs_options == NULL || 34771300Sgjelinek s_fstab->zone_fs_options == NULL) { 34781300Sgjelinek (void) fprintf(stderr, gettext(RW_LOFS), 34791300Sgjelinek t_fstab->zone_fs_special); 34801300Sgjelinek return; 34811300Sgjelinek } 34821300Sgjelinek 34831300Sgjelinek for (optp = s_fstab->zone_fs_options; optp != NULL; 34841300Sgjelinek optp = optp->zone_fsopt_next) { 34851300Sgjelinek if (opt_match("rw", optp->zone_fsopt_opt)) { 34861300Sgjelinek (void) fprintf(stderr, gettext(RW_LOFS), 34871300Sgjelinek s_fstab->zone_fs_special); 34881300Sgjelinek return; 34891300Sgjelinek } 34901300Sgjelinek } 34911300Sgjelinek 34921300Sgjelinek for (optp = t_fstab->zone_fs_options; optp != NULL; 34931300Sgjelinek optp = optp->zone_fsopt_next) { 34941300Sgjelinek if (opt_match("rw", optp->zone_fsopt_opt)) { 34951300Sgjelinek (void) fprintf(stderr, gettext(RW_LOFS), 34961300Sgjelinek t_fstab->zone_fs_special); 34971300Sgjelinek return; 34981300Sgjelinek } 34991300Sgjelinek } 35001300Sgjelinek 35011300Sgjelinek return; 35021300Sgjelinek } 35031300Sgjelinek 35041300Sgjelinek /* 35051300Sgjelinek * TRANSLATION_NOTE 35061867Sgjelinek * The first variable is the file system type and the second is 35071867Sgjelinek * the file system special device. For example, 35081867Sgjelinek * WARNING: ufs file system on '/dev/dsk/c0t0d0s0' ... 35091300Sgjelinek */ 35101867Sgjelinek (void) fprintf(stderr, gettext("WARNING: %s file system on '%s' " 35111300Sgjelinek "is configured in both zones.\n"), t_fstab->zone_fs_type, 35121300Sgjelinek t_fstab->zone_fs_special); 35131300Sgjelinek } 35141300Sgjelinek 35151300Sgjelinek static void 35161300Sgjelinek warn_fs_match(zone_dochandle_t s_handle, char *source_zone, 35171300Sgjelinek zone_dochandle_t t_handle, char *target_zone) 35181300Sgjelinek { 35191300Sgjelinek int err; 35201300Sgjelinek struct zone_fstab s_fstab; 35211300Sgjelinek struct zone_fstab t_fstab; 35221300Sgjelinek 35231300Sgjelinek if ((err = zonecfg_setfsent(t_handle)) != Z_OK) { 35241300Sgjelinek errno = err; 35251300Sgjelinek zperror2(target_zone, 35261867Sgjelinek gettext("could not enumerate file systems")); 35271300Sgjelinek return; 35281300Sgjelinek } 35291300Sgjelinek 35301300Sgjelinek while (zonecfg_getfsent(t_handle, &t_fstab) == Z_OK) { 35311300Sgjelinek if ((err = zonecfg_setfsent(s_handle)) != Z_OK) { 35321300Sgjelinek errno = err; 35331300Sgjelinek zperror2(source_zone, 35341867Sgjelinek gettext("could not enumerate file systems")); 35351300Sgjelinek (void) zonecfg_endfsent(t_handle); 35361300Sgjelinek return; 35371300Sgjelinek } 35381300Sgjelinek 35391300Sgjelinek while (zonecfg_getfsent(s_handle, &s_fstab) == Z_OK) { 35401300Sgjelinek if (strcmp(t_fstab.zone_fs_special, 35411300Sgjelinek s_fstab.zone_fs_special) == 0) { 35421300Sgjelinek print_fs_warnings(&s_fstab, &t_fstab); 35431300Sgjelinek break; 35441300Sgjelinek } 35451300Sgjelinek } 35461300Sgjelinek (void) zonecfg_endfsent(s_handle); 35471300Sgjelinek } 35481300Sgjelinek 35491300Sgjelinek (void) zonecfg_endfsent(t_handle); 35501300Sgjelinek } 35511300Sgjelinek 35521300Sgjelinek /* 35531300Sgjelinek * We don't catch the case where you used the same IP address but 35541300Sgjelinek * it is not an exact string match. For example, 192.9.0.128 vs. 192.09.0.128. 35551300Sgjelinek * However, we're not going to worry about that but we will check for 35561300Sgjelinek * a possible netmask on one of the addresses (e.g. 10.0.0.1 and 10.0.0.1/24) 35571300Sgjelinek * and handle that case as a match. 35581300Sgjelinek */ 35591300Sgjelinek static void 35601300Sgjelinek warn_ip_match(zone_dochandle_t s_handle, char *source_zone, 35611300Sgjelinek zone_dochandle_t t_handle, char *target_zone) 35621300Sgjelinek { 35631300Sgjelinek int err; 35641300Sgjelinek struct zone_nwiftab s_nwiftab; 35651300Sgjelinek struct zone_nwiftab t_nwiftab; 35661300Sgjelinek 35671300Sgjelinek if ((err = zonecfg_setnwifent(t_handle)) != Z_OK) { 35681300Sgjelinek errno = err; 35691300Sgjelinek zperror2(target_zone, 35701300Sgjelinek gettext("could not enumerate network interfaces")); 35711300Sgjelinek return; 35721300Sgjelinek } 35731300Sgjelinek 35741300Sgjelinek while (zonecfg_getnwifent(t_handle, &t_nwiftab) == Z_OK) { 35751300Sgjelinek char *p; 35761300Sgjelinek 35771300Sgjelinek /* remove an (optional) netmask from the address */ 35781300Sgjelinek if ((p = strchr(t_nwiftab.zone_nwif_address, '/')) != NULL) 35791300Sgjelinek *p = '\0'; 35801300Sgjelinek 35811300Sgjelinek if ((err = zonecfg_setnwifent(s_handle)) != Z_OK) { 35821300Sgjelinek errno = err; 35831300Sgjelinek zperror2(source_zone, 35841300Sgjelinek gettext("could not enumerate network interfaces")); 35851300Sgjelinek (void) zonecfg_endnwifent(t_handle); 35861300Sgjelinek return; 35871300Sgjelinek } 35881300Sgjelinek 35891300Sgjelinek while (zonecfg_getnwifent(s_handle, &s_nwiftab) == Z_OK) { 35901300Sgjelinek /* remove an (optional) netmask from the address */ 35911300Sgjelinek if ((p = strchr(s_nwiftab.zone_nwif_address, '/')) 35921300Sgjelinek != NULL) 35931300Sgjelinek *p = '\0'; 35941300Sgjelinek 35953448Sdh155122 /* For exclusive-IP zones, address is not specified. */ 35963448Sdh155122 if (strlen(s_nwiftab.zone_nwif_address) == 0) 35973448Sdh155122 continue; 35983448Sdh155122 35991300Sgjelinek if (strcmp(t_nwiftab.zone_nwif_address, 36001300Sgjelinek s_nwiftab.zone_nwif_address) == 0) { 36011300Sgjelinek (void) fprintf(stderr, 36021300Sgjelinek gettext("WARNING: network address '%s' " 36031300Sgjelinek "is configured in both zones.\n"), 36041300Sgjelinek t_nwiftab.zone_nwif_address); 36051300Sgjelinek break; 36061300Sgjelinek } 36071300Sgjelinek } 36081300Sgjelinek (void) zonecfg_endnwifent(s_handle); 36091300Sgjelinek } 36101300Sgjelinek 36111300Sgjelinek (void) zonecfg_endnwifent(t_handle); 36121300Sgjelinek } 36131300Sgjelinek 36141300Sgjelinek static void 36152712Snn35248 warn_dataset_match(zone_dochandle_t s_handle, char *source, 36162712Snn35248 zone_dochandle_t t_handle, char *target) 36171300Sgjelinek { 36181300Sgjelinek int err; 36191300Sgjelinek struct zone_dstab s_dstab; 36201300Sgjelinek struct zone_dstab t_dstab; 36211300Sgjelinek 36221300Sgjelinek if ((err = zonecfg_setdsent(t_handle)) != Z_OK) { 36231300Sgjelinek errno = err; 36242712Snn35248 zperror2(target, gettext("could not enumerate datasets")); 36251300Sgjelinek return; 36261300Sgjelinek } 36271300Sgjelinek 36281300Sgjelinek while (zonecfg_getdsent(t_handle, &t_dstab) == Z_OK) { 36291300Sgjelinek if ((err = zonecfg_setdsent(s_handle)) != Z_OK) { 36301300Sgjelinek errno = err; 36312712Snn35248 zperror2(source, 36321300Sgjelinek gettext("could not enumerate datasets")); 36331300Sgjelinek (void) zonecfg_enddsent(t_handle); 36341300Sgjelinek return; 36351300Sgjelinek } 36361300Sgjelinek 36371300Sgjelinek while (zonecfg_getdsent(s_handle, &s_dstab) == Z_OK) { 36381300Sgjelinek if (strcmp(t_dstab.zone_dataset_name, 36391300Sgjelinek s_dstab.zone_dataset_name) == 0) { 36402712Snn35248 target_zone = source; 36412712Snn35248 zerror(gettext("WARNING: dataset '%s' " 36421300Sgjelinek "is configured in both zones.\n"), 36431300Sgjelinek t_dstab.zone_dataset_name); 36441300Sgjelinek break; 36451300Sgjelinek } 36461300Sgjelinek } 36471300Sgjelinek (void) zonecfg_enddsent(s_handle); 36481300Sgjelinek } 36491300Sgjelinek 36501300Sgjelinek (void) zonecfg_enddsent(t_handle); 36511300Sgjelinek } 36521300Sgjelinek 36532712Snn35248 /* 36542712Snn35248 * Check that the clone and its source have the same brand type. 36552712Snn35248 */ 36562712Snn35248 static int 36572712Snn35248 valid_brand_clone(char *source_zone, char *target_zone) 36582712Snn35248 { 36592727Sedp brand_handle_t bh; 36602712Snn35248 char source_brand[MAXNAMELEN]; 36612712Snn35248 36622712Snn35248 if ((zone_get_brand(source_zone, source_brand, 36632712Snn35248 sizeof (source_brand))) != Z_OK) { 36642712Snn35248 (void) fprintf(stderr, "%s: zone '%s': %s\n", 36652712Snn35248 execname, source_zone, gettext("missing or invalid brand")); 36662712Snn35248 return (Z_ERR); 36672712Snn35248 } 36682712Snn35248 36692712Snn35248 if (strcmp(source_brand, target_brand) != NULL) { 36702712Snn35248 (void) fprintf(stderr, 36712712Snn35248 gettext("%s: Zones '%s' and '%s' have different brand " 36722712Snn35248 "types.\n"), execname, source_zone, target_zone); 36732712Snn35248 return (Z_ERR); 36742712Snn35248 } 36752712Snn35248 36762727Sedp if ((bh = brand_open(target_brand)) == NULL) { 36772712Snn35248 zerror(gettext("missing or invalid brand")); 36782712Snn35248 return (Z_ERR); 36792712Snn35248 } 36802727Sedp brand_close(bh); 36812712Snn35248 return (Z_OK); 36822712Snn35248 } 36832712Snn35248 36841300Sgjelinek static int 36851300Sgjelinek validate_clone(char *source_zone, char *target_zone) 36861300Sgjelinek { 36871300Sgjelinek int err = Z_OK; 36881300Sgjelinek zone_dochandle_t s_handle; 36891300Sgjelinek zone_dochandle_t t_handle; 36901300Sgjelinek 36911300Sgjelinek if ((t_handle = zonecfg_init_handle()) == NULL) { 36921300Sgjelinek zperror(cmd_to_str(CMD_CLONE), B_TRUE); 36931300Sgjelinek return (Z_ERR); 36941300Sgjelinek } 36951300Sgjelinek if ((err = zonecfg_get_handle(target_zone, t_handle)) != Z_OK) { 36961300Sgjelinek errno = err; 36971300Sgjelinek zperror(cmd_to_str(CMD_CLONE), B_TRUE); 36981300Sgjelinek zonecfg_fini_handle(t_handle); 36991300Sgjelinek return (Z_ERR); 37001300Sgjelinek } 37011300Sgjelinek 37021300Sgjelinek if ((s_handle = zonecfg_init_handle()) == NULL) { 37031300Sgjelinek zperror(cmd_to_str(CMD_CLONE), B_TRUE); 37041300Sgjelinek zonecfg_fini_handle(t_handle); 37051300Sgjelinek return (Z_ERR); 37061300Sgjelinek } 37071300Sgjelinek if ((err = zonecfg_get_handle(source_zone, s_handle)) != Z_OK) { 37081300Sgjelinek errno = err; 37091300Sgjelinek zperror(cmd_to_str(CMD_CLONE), B_TRUE); 37101300Sgjelinek goto done; 37111300Sgjelinek } 37121300Sgjelinek 37132712Snn35248 /* verify new zone has same brand type */ 37142712Snn35248 err = valid_brand_clone(source_zone, target_zone); 37152712Snn35248 if (err != Z_OK) 37162712Snn35248 goto done; 37172712Snn35248 37181300Sgjelinek /* verify new zone has same inherit-pkg-dirs */ 37191300Sgjelinek err = valid_ipd_clone(s_handle, source_zone, t_handle, target_zone); 37201300Sgjelinek 37211300Sgjelinek /* warn about imported fs's which are the same */ 37221300Sgjelinek warn_fs_match(s_handle, source_zone, t_handle, target_zone); 37231300Sgjelinek 37241300Sgjelinek /* warn about imported IP addresses which are the same */ 37251300Sgjelinek warn_ip_match(s_handle, source_zone, t_handle, target_zone); 37261300Sgjelinek 37271300Sgjelinek /* warn about imported devices which are the same */ 37281300Sgjelinek warn_dev_match(s_handle, source_zone, t_handle, target_zone); 37291300Sgjelinek 37301300Sgjelinek /* warn about imported datasets which are the same */ 37311300Sgjelinek warn_dataset_match(s_handle, source_zone, t_handle, target_zone); 37321300Sgjelinek 37331300Sgjelinek done: 37341300Sgjelinek zonecfg_fini_handle(t_handle); 37351300Sgjelinek zonecfg_fini_handle(s_handle); 37361300Sgjelinek 37371300Sgjelinek return ((err == Z_OK) ? Z_OK : Z_ERR); 37381300Sgjelinek } 37391300Sgjelinek 37401300Sgjelinek static int 37411300Sgjelinek copy_zone(char *src, char *dst) 37421300Sgjelinek { 37431300Sgjelinek boolean_t out_null = B_FALSE; 37441300Sgjelinek int status; 37451300Sgjelinek char *outfile; 37461300Sgjelinek char cmdbuf[MAXPATHLEN * 2 + 128]; 37471300Sgjelinek 37481300Sgjelinek if ((outfile = tempnam("/var/log", "zone")) == NULL) { 37491300Sgjelinek outfile = "/dev/null"; 37501300Sgjelinek out_null = B_TRUE; 37511300Sgjelinek } 37521300Sgjelinek 37531867Sgjelinek /* 37541867Sgjelinek * Use find to get the list of files to copy. We need to skip 37551867Sgjelinek * files of type "socket" since cpio can't handle those but that 37561867Sgjelinek * should be ok since the app will recreate the socket when it runs. 37571867Sgjelinek * We also need to filter out anything under the .zfs subdir. Since 37581867Sgjelinek * find is running depth-first, we need the extra egrep to filter .zfs. 37591867Sgjelinek */ 37601300Sgjelinek (void) snprintf(cmdbuf, sizeof (cmdbuf), 37611867Sgjelinek "cd %s && /usr/bin/find . -type s -prune -o -depth -print | " 37621607Sgjelinek "/usr/bin/egrep -v '^\\./\\.zfs$|^\\./\\.zfs/' | " 37631300Sgjelinek "/usr/bin/cpio -pdmuP@ %s > %s 2>&1", 37641300Sgjelinek src, dst, outfile); 37651300Sgjelinek 37661300Sgjelinek status = do_subproc(cmdbuf); 37671300Sgjelinek 37682712Snn35248 if (subproc_status("copy", status, B_TRUE) != ZONE_SUBPROC_OK) { 37691300Sgjelinek if (!out_null) 37701300Sgjelinek (void) fprintf(stderr, gettext("\nThe copy failed.\n" 37711300Sgjelinek "More information can be found in %s\n"), outfile); 37722712Snn35248 return (Z_ERR); 37731300Sgjelinek } 37741300Sgjelinek 37751300Sgjelinek if (!out_null) 37761300Sgjelinek (void) unlink(outfile); 37771300Sgjelinek 37781300Sgjelinek return (Z_OK); 37791300Sgjelinek } 37801300Sgjelinek 37811300Sgjelinek static int 37822712Snn35248 zone_postclone(char *zonepath) 37831300Sgjelinek { 37842712Snn35248 char cmdbuf[MAXPATHLEN]; 37852712Snn35248 int status; 37862727Sedp brand_handle_t bh; 37872712Snn35248 int err = Z_OK; 37881676Sjpk 37891676Sjpk /* 37902712Snn35248 * Fetch the post-clone command, if any, from the brand 37912712Snn35248 * configuration. 37921568Sgjelinek */ 37932727Sedp if ((bh = brand_open(target_brand)) == NULL) { 37942712Snn35248 zerror(gettext("missing or invalid brand")); 37951568Sgjelinek return (Z_ERR); 37961568Sgjelinek } 37972712Snn35248 (void) strcpy(cmdbuf, EXEC_PREFIX); 37982727Sedp err = brand_get_postclone(bh, target_zone, zonepath, cmdbuf + EXEC_LEN, 37992712Snn35248 sizeof (cmdbuf) - EXEC_LEN, 0, NULL); 38002727Sedp brand_close(bh); 38012712Snn35248 38022712Snn35248 if (err == 0 && strlen(cmdbuf) > EXEC_LEN) { 38032712Snn35248 status = do_subproc(cmdbuf); 38042712Snn35248 if ((err = subproc_status("postclone", status, B_FALSE)) 38052712Snn35248 != ZONE_SUBPROC_OK) { 38062712Snn35248 zerror(gettext("post-clone configuration failed.")); 38072712Snn35248 err = Z_ERR; 38082712Snn35248 } 38092712Snn35248 } 38102712Snn35248 38112712Snn35248 return (err); 38121300Sgjelinek } 38131300Sgjelinek 38141300Sgjelinek /* ARGSUSED */ 38151867Sgjelinek static int 38161300Sgjelinek zfm_print(const char *p, void *r) { 38171300Sgjelinek zerror(" %s\n", p); 38181300Sgjelinek return (0); 38191300Sgjelinek } 38201300Sgjelinek 38211867Sgjelinek int 38221867Sgjelinek clone_copy(char *source_zonepath, char *zonepath) 38231867Sgjelinek { 38241867Sgjelinek int err; 38251867Sgjelinek 38261867Sgjelinek /* Don't clone the zone if anything is still mounted there */ 38271867Sgjelinek if (zonecfg_find_mounts(source_zonepath, NULL, NULL)) { 38281867Sgjelinek zerror(gettext("These file systems are mounted on " 38291867Sgjelinek "subdirectories of %s.\n"), source_zonepath); 38301867Sgjelinek (void) zonecfg_find_mounts(source_zonepath, zfm_print, NULL); 38311867Sgjelinek return (Z_ERR); 38321867Sgjelinek } 38331867Sgjelinek 38341867Sgjelinek /* 38351867Sgjelinek * Attempt to create a ZFS fs for the zonepath. As usual, we don't 38361867Sgjelinek * care if this works or not since we always have the default behavior 38371867Sgjelinek * of a simple directory for the zonepath. 38381867Sgjelinek */ 38391867Sgjelinek create_zfs_zonepath(zonepath); 38401867Sgjelinek 38411867Sgjelinek (void) printf(gettext("Copying %s..."), source_zonepath); 38421867Sgjelinek (void) fflush(stdout); 38431867Sgjelinek 38441867Sgjelinek err = copy_zone(source_zonepath, zonepath); 38451867Sgjelinek 38461867Sgjelinek (void) printf("\n"); 38471867Sgjelinek 38481867Sgjelinek return (err); 38491867Sgjelinek } 38501867Sgjelinek 38511300Sgjelinek static int 38521300Sgjelinek clone_func(int argc, char *argv[]) 38531300Sgjelinek { 38541300Sgjelinek char *source_zone = NULL; 38551300Sgjelinek int lockfd; 38561300Sgjelinek int err, arg; 38571300Sgjelinek char zonepath[MAXPATHLEN]; 38581300Sgjelinek char source_zonepath[MAXPATHLEN]; 38591300Sgjelinek zone_state_t state; 38601300Sgjelinek zone_entry_t *zent; 38611867Sgjelinek char *method = NULL; 38621867Sgjelinek char *snapshot = NULL; 38631300Sgjelinek 38641300Sgjelinek if (zonecfg_in_alt_root()) { 38651300Sgjelinek zerror(gettext("cannot clone zone in alternate root")); 38661300Sgjelinek return (Z_ERR); 38671300Sgjelinek } 38681300Sgjelinek 38691300Sgjelinek optind = 0; 38701867Sgjelinek if ((arg = getopt(argc, argv, "?m:s:")) != EOF) { 38711300Sgjelinek switch (arg) { 38721300Sgjelinek case '?': 38731300Sgjelinek sub_usage(SHELP_CLONE, CMD_CLONE); 38741300Sgjelinek return (optopt == '?' ? Z_OK : Z_USAGE); 38751300Sgjelinek case 'm': 38761300Sgjelinek method = optarg; 38771300Sgjelinek break; 38781867Sgjelinek case 's': 38791867Sgjelinek snapshot = optarg; 38801867Sgjelinek break; 38811300Sgjelinek default: 38821300Sgjelinek sub_usage(SHELP_CLONE, CMD_CLONE); 38831300Sgjelinek return (Z_USAGE); 38841300Sgjelinek } 38851300Sgjelinek } 38861867Sgjelinek if (argc != (optind + 1) || 38871867Sgjelinek (method != NULL && strcmp(method, "copy") != 0)) { 38881300Sgjelinek sub_usage(SHELP_CLONE, CMD_CLONE); 38891300Sgjelinek return (Z_USAGE); 38901300Sgjelinek } 38911300Sgjelinek source_zone = argv[optind]; 38922712Snn35248 if (sanity_check(target_zone, CMD_CLONE, B_FALSE, B_TRUE, B_FALSE) 38932712Snn35248 != Z_OK) 38941300Sgjelinek return (Z_ERR); 38953339Szt129084 if (verify_details(CMD_CLONE, argv) != Z_OK) 38961300Sgjelinek return (Z_ERR); 38971300Sgjelinek 38981300Sgjelinek /* 38991300Sgjelinek * We also need to do some extra validation on the source zone. 39001300Sgjelinek */ 39011300Sgjelinek if (strcmp(source_zone, GLOBAL_ZONENAME) == 0) { 39021300Sgjelinek zerror(gettext("%s operation is invalid for the global zone."), 39031300Sgjelinek cmd_to_str(CMD_CLONE)); 39041300Sgjelinek return (Z_ERR); 39051300Sgjelinek } 39061300Sgjelinek 39071300Sgjelinek if (strncmp(source_zone, "SUNW", 4) == 0) { 39081300Sgjelinek zerror(gettext("%s operation is invalid for zones starting " 39091300Sgjelinek "with SUNW."), cmd_to_str(CMD_CLONE)); 39101300Sgjelinek return (Z_ERR); 39111300Sgjelinek } 39121300Sgjelinek 39131300Sgjelinek zent = lookup_running_zone(source_zone); 39141300Sgjelinek if (zent != NULL) { 39151300Sgjelinek /* check whether the zone is ready or running */ 39161300Sgjelinek if ((err = zone_get_state(zent->zname, &zent->zstate_num)) 39171300Sgjelinek != Z_OK) { 39181300Sgjelinek errno = err; 39191300Sgjelinek zperror2(zent->zname, gettext("could not get state")); 39201300Sgjelinek /* can't tell, so hedge */ 39211300Sgjelinek zent->zstate_str = "ready/running"; 39221300Sgjelinek } else { 39231300Sgjelinek zent->zstate_str = zone_state_str(zent->zstate_num); 39241300Sgjelinek } 39251300Sgjelinek zerror(gettext("%s operation is invalid for %s zones."), 39261300Sgjelinek cmd_to_str(CMD_CLONE), zent->zstate_str); 39271300Sgjelinek return (Z_ERR); 39281300Sgjelinek } 39291300Sgjelinek 39301300Sgjelinek if ((err = zone_get_state(source_zone, &state)) != Z_OK) { 39311300Sgjelinek errno = err; 39321300Sgjelinek zperror2(source_zone, gettext("could not get state")); 39331300Sgjelinek return (Z_ERR); 39341300Sgjelinek } 39351300Sgjelinek if (state != ZONE_STATE_INSTALLED) { 39361300Sgjelinek (void) fprintf(stderr, 39371300Sgjelinek gettext("%s: zone %s is %s; %s is required.\n"), 39381300Sgjelinek execname, source_zone, zone_state_str(state), 39391300Sgjelinek zone_state_str(ZONE_STATE_INSTALLED)); 39401300Sgjelinek return (Z_ERR); 39411300Sgjelinek } 39421300Sgjelinek 39431300Sgjelinek /* 39441300Sgjelinek * The source zone checks out ok, continue with the clone. 39451300Sgjelinek */ 39461300Sgjelinek 39471300Sgjelinek if (validate_clone(source_zone, target_zone) != Z_OK) 39481300Sgjelinek return (Z_ERR); 39491300Sgjelinek 39501300Sgjelinek if (grab_lock_file(target_zone, &lockfd) != Z_OK) { 39511300Sgjelinek zerror(gettext("another %s may have an operation in progress."), 39521300Sgjelinek "zoneadm"); 39531300Sgjelinek return (Z_ERR); 39541300Sgjelinek } 39551300Sgjelinek 39561300Sgjelinek if ((err = zone_get_zonepath(source_zone, source_zonepath, 39571300Sgjelinek sizeof (source_zonepath))) != Z_OK) { 39581300Sgjelinek errno = err; 39591300Sgjelinek zperror2(source_zone, gettext("could not get zone path")); 39601300Sgjelinek goto done; 39611300Sgjelinek } 39621300Sgjelinek 39631300Sgjelinek if ((err = zone_get_zonepath(target_zone, zonepath, sizeof (zonepath))) 39641300Sgjelinek != Z_OK) { 39651300Sgjelinek errno = err; 39661300Sgjelinek zperror2(target_zone, gettext("could not get zone path")); 39671300Sgjelinek goto done; 39681300Sgjelinek } 39691300Sgjelinek 39701300Sgjelinek if ((err = zone_set_state(target_zone, ZONE_STATE_INCOMPLETE)) 39711300Sgjelinek != Z_OK) { 39721300Sgjelinek errno = err; 39731300Sgjelinek zperror2(target_zone, gettext("could not set state")); 39741300Sgjelinek goto done; 39751300Sgjelinek } 39761300Sgjelinek 39771867Sgjelinek if (snapshot != NULL) { 39781867Sgjelinek err = clone_snapshot_zfs(snapshot, zonepath); 39791867Sgjelinek } else { 39801867Sgjelinek /* 39811867Sgjelinek * We always copy the clone unless the source is ZFS and a 39821867Sgjelinek * ZFS clone worked. We fallback to copying if the ZFS clone 39831867Sgjelinek * fails for some reason. 39841867Sgjelinek */ 39851867Sgjelinek err = Z_ERR; 39861867Sgjelinek if (method == NULL && is_zonepath_zfs(source_zonepath)) 39871867Sgjelinek err = clone_zfs(source_zone, source_zonepath, zonepath); 39881867Sgjelinek 39891867Sgjelinek if (err != Z_OK) 39901867Sgjelinek err = clone_copy(source_zonepath, zonepath); 39911867Sgjelinek } 39921867Sgjelinek 39932712Snn35248 /* 39942712Snn35248 * Trusted Extensions requires that cloned zones use the same sysid 39952712Snn35248 * configuration, so it is not appropriate to perform any 39962712Snn35248 * post-clone reconfiguration. 39972712Snn35248 */ 39982712Snn35248 if ((err == Z_OK) && !is_system_labeled()) 39992712Snn35248 err = zone_postclone(zonepath); 40001300Sgjelinek 40011300Sgjelinek done: 40022712Snn35248 /* 40032712Snn35248 * If everything went well, we mark the zone as installed. 40042712Snn35248 */ 40052712Snn35248 if (err == Z_OK) { 40062712Snn35248 err = zone_set_state(target_zone, ZONE_STATE_INSTALLED); 40072712Snn35248 if (err != Z_OK) { 40082712Snn35248 errno = err; 40092712Snn35248 zperror2(target_zone, gettext("could not set state")); 40102712Snn35248 } 40112712Snn35248 } 40121300Sgjelinek release_lock_file(lockfd); 40131300Sgjelinek return ((err == Z_OK) ? Z_OK : Z_ERR); 40141300Sgjelinek } 40151300Sgjelinek 40161607Sgjelinek /* 40171867Sgjelinek * Used when removing a zonepath after uninstalling or cleaning up after 40181867Sgjelinek * the move subcommand. This handles a zonepath that has non-standard 40191867Sgjelinek * contents so that we will only cleanup the stuff we know about and leave 40201867Sgjelinek * any user data alone. 40211607Sgjelinek * 40221867Sgjelinek * If the "all" parameter is true then we should remove the whole zonepath 40231867Sgjelinek * even if it has non-standard files/directories in it. This can be used when 40241867Sgjelinek * we need to cleanup after moving the zonepath across file systems. 40251867Sgjelinek * 40261867Sgjelinek * We "exec" the RMCOMMAND so that the returned status is that of RMCOMMAND 40271867Sgjelinek * and not the shell. 40281607Sgjelinek */ 40291607Sgjelinek static int 40301867Sgjelinek cleanup_zonepath(char *zonepath, boolean_t all) 40311607Sgjelinek { 40321867Sgjelinek int status; 40331867Sgjelinek int i; 40341867Sgjelinek boolean_t non_std = B_FALSE; 40351867Sgjelinek struct dirent *dp; 40361867Sgjelinek DIR *dirp; 40373686Sgjelinek /* 40383686Sgjelinek * The SUNWattached.xml file is expected since it might 40393686Sgjelinek * exist if the zone was force-attached after a 40403686Sgjelinek * migration. 40413686Sgjelinek */ 40423686Sgjelinek char *std_entries[] = {"dev", "lu", "root", 40433686Sgjelinek "SUNWattached.xml", NULL}; 40441867Sgjelinek /* (MAXPATHLEN * 3) is for the 3 std_entries dirs */ 40451867Sgjelinek char cmdbuf[sizeof (RMCOMMAND) + (MAXPATHLEN * 3) + 64]; 40461867Sgjelinek 40471867Sgjelinek /* 40481867Sgjelinek * We shouldn't need these checks but lets be paranoid since we 40491867Sgjelinek * could blow away the whole system here if we got the wrong zonepath. 40501867Sgjelinek */ 40511867Sgjelinek if (*zonepath == NULL || strcmp(zonepath, "/") == 0) { 40521867Sgjelinek (void) fprintf(stderr, "invalid zonepath '%s'\n", zonepath); 40531867Sgjelinek return (Z_INVAL); 40541867Sgjelinek } 40551867Sgjelinek 40561867Sgjelinek /* 40571867Sgjelinek * If the dirpath is already gone (maybe it was manually removed) then 40581867Sgjelinek * we just return Z_OK so that the cleanup is successful. 40591867Sgjelinek */ 40601867Sgjelinek if ((dirp = opendir(zonepath)) == NULL) 40611867Sgjelinek return (Z_OK); 40621867Sgjelinek 40631867Sgjelinek /* 40641867Sgjelinek * Look through the zonepath directory to see if there are any 40651867Sgjelinek * non-standard files/dirs. Also skip .zfs since that might be 40661867Sgjelinek * there but we'll handle ZFS file systems as a special case. 40671867Sgjelinek */ 40681867Sgjelinek while ((dp = readdir(dirp)) != NULL) { 40691867Sgjelinek if (strcmp(dp->d_name, ".") == 0 || 40701867Sgjelinek strcmp(dp->d_name, "..") == 0 || 40711867Sgjelinek strcmp(dp->d_name, ".zfs") == 0) 40721867Sgjelinek continue; 40731867Sgjelinek 40741867Sgjelinek for (i = 0; std_entries[i] != NULL; i++) 40751867Sgjelinek if (strcmp(dp->d_name, std_entries[i]) == 0) 40761867Sgjelinek break; 40771867Sgjelinek 40781867Sgjelinek if (std_entries[i] == NULL) 40791867Sgjelinek non_std = B_TRUE; 40801867Sgjelinek } 40811867Sgjelinek (void) closedir(dirp); 40821867Sgjelinek 40831867Sgjelinek if (!all && non_std) { 40841607Sgjelinek /* 40851867Sgjelinek * There are extra, non-standard directories/files in the 40861867Sgjelinek * zonepath so we don't want to remove the zonepath. We 40871867Sgjelinek * just want to remove the standard directories and leave 40881867Sgjelinek * the user data alone. 40891607Sgjelinek */ 40901867Sgjelinek (void) snprintf(cmdbuf, sizeof (cmdbuf), "exec " RMCOMMAND); 40911867Sgjelinek 40921867Sgjelinek for (i = 0; std_entries[i] != NULL; i++) { 40931867Sgjelinek char tmpbuf[MAXPATHLEN]; 40941867Sgjelinek 40951867Sgjelinek if (snprintf(tmpbuf, sizeof (tmpbuf), " %s/%s", 40961867Sgjelinek zonepath, std_entries[i]) >= sizeof (tmpbuf) || 40971867Sgjelinek strlcat(cmdbuf, tmpbuf, sizeof (cmdbuf)) >= 40981867Sgjelinek sizeof (cmdbuf)) { 40991867Sgjelinek (void) fprintf(stderr, 41001867Sgjelinek gettext("path is too long\n")); 41011867Sgjelinek return (Z_INVAL); 41021867Sgjelinek } 41031867Sgjelinek } 41041867Sgjelinek 41051867Sgjelinek status = do_subproc(cmdbuf); 41061867Sgjelinek 41071867Sgjelinek (void) fprintf(stderr, gettext("WARNING: Unable to completely " 41081867Sgjelinek "remove %s\nbecause it contains additional user data. " 41091867Sgjelinek "Only the standard directory\nentries have been " 41101867Sgjelinek "removed.\n"), 41111867Sgjelinek zonepath); 41121867Sgjelinek 41132712Snn35248 return ((subproc_status(RMCOMMAND, status, B_TRUE) == 41142712Snn35248 ZONE_SUBPROC_OK) ? Z_OK : Z_ERR); 41151607Sgjelinek } 41161607Sgjelinek 41171867Sgjelinek /* 41181867Sgjelinek * There is nothing unexpected in the zonepath, try to get rid of the 41191867Sgjelinek * whole zonepath directory. 41201867Sgjelinek * 41211867Sgjelinek * If the zonepath is its own zfs file system, try to destroy the 41221867Sgjelinek * file system. If that fails for some reason (e.g. it has clones) 41231867Sgjelinek * then we'll just remove the contents of the zonepath. 41241867Sgjelinek */ 41251867Sgjelinek if (is_zonepath_zfs(zonepath)) { 41261867Sgjelinek if (destroy_zfs(zonepath) == Z_OK) 41271867Sgjelinek return (Z_OK); 41281867Sgjelinek (void) snprintf(cmdbuf, sizeof (cmdbuf), "exec " RMCOMMAND 41291867Sgjelinek " %s/*", zonepath); 41301867Sgjelinek status = do_subproc(cmdbuf); 41312712Snn35248 return ((subproc_status(RMCOMMAND, status, B_TRUE) == 41322712Snn35248 ZONE_SUBPROC_OK) ? Z_OK : Z_ERR); 41331867Sgjelinek } 41341867Sgjelinek 41351867Sgjelinek (void) snprintf(cmdbuf, sizeof (cmdbuf), "exec " RMCOMMAND " %s", 41361867Sgjelinek zonepath); 41371607Sgjelinek status = do_subproc(cmdbuf); 41382712Snn35248 41392712Snn35248 return ((subproc_status(RMCOMMAND, status, B_TRUE) == ZONE_SUBPROC_OK) 41402712Snn35248 ? Z_OK : Z_ERR); 41411607Sgjelinek } 41421607Sgjelinek 41431300Sgjelinek static int 41441300Sgjelinek move_func(int argc, char *argv[]) 41451300Sgjelinek { 41461300Sgjelinek char *new_zonepath = NULL; 41471300Sgjelinek int lockfd; 41481300Sgjelinek int err, arg; 41491300Sgjelinek char zonepath[MAXPATHLEN]; 41501300Sgjelinek zone_dochandle_t handle; 41511300Sgjelinek boolean_t fast; 41521867Sgjelinek boolean_t is_zfs = B_FALSE; 41531867Sgjelinek struct dirent *dp; 41541867Sgjelinek DIR *dirp; 41551867Sgjelinek boolean_t empty = B_TRUE; 41561300Sgjelinek boolean_t revert; 41571300Sgjelinek struct stat zonepath_buf; 41581300Sgjelinek struct stat new_zonepath_buf; 41591300Sgjelinek 41601300Sgjelinek if (zonecfg_in_alt_root()) { 41611300Sgjelinek zerror(gettext("cannot move zone in alternate root")); 41621300Sgjelinek return (Z_ERR); 41631300Sgjelinek } 41641300Sgjelinek 41651300Sgjelinek optind = 0; 41661300Sgjelinek if ((arg = getopt(argc, argv, "?")) != EOF) { 41671300Sgjelinek switch (arg) { 41681300Sgjelinek case '?': 41691300Sgjelinek sub_usage(SHELP_MOVE, CMD_MOVE); 41701300Sgjelinek return (optopt == '?' ? Z_OK : Z_USAGE); 41711300Sgjelinek default: 41721300Sgjelinek sub_usage(SHELP_MOVE, CMD_MOVE); 41731300Sgjelinek return (Z_USAGE); 41741300Sgjelinek } 41751300Sgjelinek } 41761300Sgjelinek if (argc != (optind + 1)) { 41771300Sgjelinek sub_usage(SHELP_MOVE, CMD_MOVE); 41781300Sgjelinek return (Z_USAGE); 41791300Sgjelinek } 41801300Sgjelinek new_zonepath = argv[optind]; 41812712Snn35248 if (sanity_check(target_zone, CMD_MOVE, B_FALSE, B_TRUE, B_FALSE) 41822712Snn35248 != Z_OK) 41831300Sgjelinek return (Z_ERR); 41843339Szt129084 if (verify_details(CMD_MOVE, argv) != Z_OK) 41851300Sgjelinek return (Z_ERR); 41861300Sgjelinek 41871300Sgjelinek /* 41881300Sgjelinek * Check out the new zonepath. This has the side effect of creating 41891300Sgjelinek * a directory for the new zonepath. We depend on this later when we 41901867Sgjelinek * stat to see if we are doing a cross file system move or not. 41911300Sgjelinek */ 41921300Sgjelinek if (validate_zonepath(new_zonepath, CMD_MOVE) != Z_OK) 41931300Sgjelinek return (Z_ERR); 41941300Sgjelinek 41951300Sgjelinek if ((err = zone_get_zonepath(target_zone, zonepath, sizeof (zonepath))) 41961300Sgjelinek != Z_OK) { 41971300Sgjelinek errno = err; 41981300Sgjelinek zperror2(target_zone, gettext("could not get zone path")); 41991300Sgjelinek return (Z_ERR); 42001300Sgjelinek } 42011300Sgjelinek 42021300Sgjelinek if (stat(zonepath, &zonepath_buf) == -1) { 42031300Sgjelinek zperror(gettext("could not stat zone path"), B_FALSE); 42041300Sgjelinek return (Z_ERR); 42051300Sgjelinek } 42061300Sgjelinek 42071300Sgjelinek if (stat(new_zonepath, &new_zonepath_buf) == -1) { 42081300Sgjelinek zperror(gettext("could not stat new zone path"), B_FALSE); 42091300Sgjelinek return (Z_ERR); 42101300Sgjelinek } 42111300Sgjelinek 42121867Sgjelinek /* 42131867Sgjelinek * Check if the destination directory is empty. 42141867Sgjelinek */ 42151867Sgjelinek if ((dirp = opendir(new_zonepath)) == NULL) { 42161867Sgjelinek zperror(gettext("could not open new zone path"), B_FALSE); 42171867Sgjelinek return (Z_ERR); 42181867Sgjelinek } 42191867Sgjelinek while ((dp = readdir(dirp)) != (struct dirent *)0) { 42201867Sgjelinek if (strcmp(dp->d_name, ".") == 0 || 42211867Sgjelinek strcmp(dp->d_name, "..") == 0) 42221867Sgjelinek continue; 42231867Sgjelinek empty = B_FALSE; 42241867Sgjelinek break; 42251867Sgjelinek } 42261867Sgjelinek (void) closedir(dirp); 42271867Sgjelinek 42281867Sgjelinek /* Error if there is anything in the destination directory. */ 42291867Sgjelinek if (!empty) { 42301867Sgjelinek (void) fprintf(stderr, gettext("could not move zone to %s: " 42311867Sgjelinek "directory not empty\n"), new_zonepath); 42321867Sgjelinek return (Z_ERR); 42331867Sgjelinek } 42341867Sgjelinek 42351300Sgjelinek /* Don't move the zone if anything is still mounted there */ 42361300Sgjelinek if (zonecfg_find_mounts(zonepath, NULL, NULL)) { 42371867Sgjelinek zerror(gettext("These file systems are mounted on " 42381300Sgjelinek "subdirectories of %s.\n"), zonepath); 42391300Sgjelinek (void) zonecfg_find_mounts(zonepath, zfm_print, NULL); 42401300Sgjelinek return (Z_ERR); 42411300Sgjelinek } 42421300Sgjelinek 42431300Sgjelinek /* 42441867Sgjelinek * Check if we are moving in the same file system and can do a fast 42451867Sgjelinek * move or if we are crossing file systems and have to copy the data. 42461300Sgjelinek */ 42471300Sgjelinek fast = (zonepath_buf.st_dev == new_zonepath_buf.st_dev); 42481300Sgjelinek 42491300Sgjelinek if ((handle = zonecfg_init_handle()) == NULL) { 42501300Sgjelinek zperror(cmd_to_str(CMD_MOVE), B_TRUE); 42511300Sgjelinek return (Z_ERR); 42521300Sgjelinek } 42531300Sgjelinek 42541300Sgjelinek if ((err = zonecfg_get_handle(target_zone, handle)) != Z_OK) { 42551300Sgjelinek errno = err; 42561300Sgjelinek zperror(cmd_to_str(CMD_MOVE), B_TRUE); 42571300Sgjelinek zonecfg_fini_handle(handle); 42581300Sgjelinek return (Z_ERR); 42591300Sgjelinek } 42601300Sgjelinek 42611300Sgjelinek if (grab_lock_file(target_zone, &lockfd) != Z_OK) { 42621300Sgjelinek zerror(gettext("another %s may have an operation in progress."), 42631300Sgjelinek "zoneadm"); 42641300Sgjelinek zonecfg_fini_handle(handle); 42651300Sgjelinek return (Z_ERR); 42661300Sgjelinek } 42671300Sgjelinek 42681300Sgjelinek /* 42691867Sgjelinek * We're making some file system changes now so we have to clean up 42701867Sgjelinek * the file system before we are done. This will either clean up the 42711300Sgjelinek * new zonepath if the zonecfg update failed or it will clean up the 42721300Sgjelinek * old zonepath if everything is ok. 42731300Sgjelinek */ 42741300Sgjelinek revert = B_TRUE; 42751300Sgjelinek 42761867Sgjelinek if (is_zonepath_zfs(zonepath) && 42771867Sgjelinek move_zfs(zonepath, new_zonepath) != Z_ERR) { 42781867Sgjelinek is_zfs = B_TRUE; 42791867Sgjelinek 42801867Sgjelinek } else if (fast) { 42811867Sgjelinek /* same file system, use rename for a quick move */ 42821300Sgjelinek 42831300Sgjelinek /* 42841300Sgjelinek * Remove the new_zonepath directory that got created above 42851300Sgjelinek * during the validation. It gets in the way of the rename. 42861300Sgjelinek */ 42871300Sgjelinek if (rmdir(new_zonepath) != 0) { 42881300Sgjelinek zperror(gettext("could not rmdir new zone path"), 42891300Sgjelinek B_FALSE); 42901300Sgjelinek zonecfg_fini_handle(handle); 42911300Sgjelinek release_lock_file(lockfd); 42921300Sgjelinek return (Z_ERR); 42931300Sgjelinek } 42941300Sgjelinek 42951300Sgjelinek if (rename(zonepath, new_zonepath) != 0) { 42961300Sgjelinek /* 42971300Sgjelinek * If this fails we don't need to do all of the 42981300Sgjelinek * cleanup that happens for the rest of the code 42991300Sgjelinek * so just return from this error. 43001300Sgjelinek */ 43011300Sgjelinek zperror(gettext("could not move zone"), B_FALSE); 43021300Sgjelinek zonecfg_fini_handle(handle); 43031300Sgjelinek release_lock_file(lockfd); 43041300Sgjelinek return (Z_ERR); 43051300Sgjelinek } 43061300Sgjelinek 43071300Sgjelinek } else { 43081867Sgjelinek /* 43091867Sgjelinek * Attempt to create a ZFS fs for the new zonepath. As usual, 43101867Sgjelinek * we don't care if this works or not since we always have the 43111867Sgjelinek * default behavior of a simple directory for the zonepath. 43121867Sgjelinek */ 43131867Sgjelinek create_zfs_zonepath(new_zonepath); 43141867Sgjelinek 43151300Sgjelinek (void) printf(gettext( 43161867Sgjelinek "Moving across file systems; copying zonepath %s..."), 43171300Sgjelinek zonepath); 43181300Sgjelinek (void) fflush(stdout); 43191300Sgjelinek 43201300Sgjelinek err = copy_zone(zonepath, new_zonepath); 43211300Sgjelinek 43221300Sgjelinek (void) printf("\n"); 43231300Sgjelinek if (err != Z_OK) 43241300Sgjelinek goto done; 43251300Sgjelinek } 43261300Sgjelinek 43271300Sgjelinek if ((err = zonecfg_set_zonepath(handle, new_zonepath)) != Z_OK) { 43281300Sgjelinek errno = err; 43291300Sgjelinek zperror(gettext("could not set new zonepath"), B_TRUE); 43301300Sgjelinek goto done; 43311300Sgjelinek } 43321300Sgjelinek 43331300Sgjelinek if ((err = zonecfg_save(handle)) != Z_OK) { 43341300Sgjelinek errno = err; 43351300Sgjelinek zperror(gettext("zonecfg save failed"), B_TRUE); 43361300Sgjelinek goto done; 43371300Sgjelinek } 43381300Sgjelinek 43391300Sgjelinek revert = B_FALSE; 43401300Sgjelinek 43411300Sgjelinek done: 43421300Sgjelinek zonecfg_fini_handle(handle); 43431300Sgjelinek release_lock_file(lockfd); 43441300Sgjelinek 43451300Sgjelinek /* 43461867Sgjelinek * Clean up the file system based on how things went. We either 43471300Sgjelinek * clean up the new zonepath if the operation failed for some reason 43481300Sgjelinek * or we clean up the old zonepath if everything is ok. 43491300Sgjelinek */ 43501300Sgjelinek if (revert) { 43511300Sgjelinek /* The zonecfg update failed, cleanup the new zonepath. */ 43521867Sgjelinek if (is_zfs) { 43531867Sgjelinek if (move_zfs(new_zonepath, zonepath) == Z_ERR) { 43541867Sgjelinek (void) fprintf(stderr, gettext("could not " 43551867Sgjelinek "restore zonepath, the zfs mountpoint is " 43561867Sgjelinek "set as:\n%s\n"), new_zonepath); 43571867Sgjelinek /* 43581867Sgjelinek * err is already != Z_OK since we're reverting 43591867Sgjelinek */ 43601867Sgjelinek } 43611867Sgjelinek 43621867Sgjelinek } else if (fast) { 43631300Sgjelinek if (rename(new_zonepath, zonepath) != 0) { 43641300Sgjelinek zperror(gettext("could not restore zonepath"), 43651300Sgjelinek B_FALSE); 43661300Sgjelinek /* 43671300Sgjelinek * err is already != Z_OK since we're reverting 43681300Sgjelinek */ 43691300Sgjelinek } 43701300Sgjelinek } else { 43711300Sgjelinek (void) printf(gettext("Cleaning up zonepath %s..."), 43721300Sgjelinek new_zonepath); 43731300Sgjelinek (void) fflush(stdout); 43741867Sgjelinek err = cleanup_zonepath(new_zonepath, B_TRUE); 43751300Sgjelinek (void) printf("\n"); 43761300Sgjelinek 43771607Sgjelinek if (err != Z_OK) { 43781300Sgjelinek errno = err; 43791300Sgjelinek zperror(gettext("could not remove new " 43801300Sgjelinek "zonepath"), B_TRUE); 43811300Sgjelinek } else { 43821300Sgjelinek /* 43831300Sgjelinek * Because we're reverting we know the mainline 43841300Sgjelinek * code failed but we just reused the err 43851300Sgjelinek * variable so we reset it back to Z_ERR. 43861300Sgjelinek */ 43871300Sgjelinek err = Z_ERR; 43881300Sgjelinek } 43891300Sgjelinek } 43901300Sgjelinek 43911300Sgjelinek } else { 43921300Sgjelinek /* The move was successful, cleanup the old zonepath. */ 43931867Sgjelinek if (!is_zfs && !fast) { 43941300Sgjelinek (void) printf( 43951300Sgjelinek gettext("Cleaning up zonepath %s..."), zonepath); 43961300Sgjelinek (void) fflush(stdout); 43971867Sgjelinek err = cleanup_zonepath(zonepath, B_TRUE); 43981300Sgjelinek (void) printf("\n"); 43991300Sgjelinek 44001607Sgjelinek if (err != Z_OK) { 44011300Sgjelinek errno = err; 44021300Sgjelinek zperror(gettext("could not remove zonepath"), 44031300Sgjelinek B_TRUE); 44041300Sgjelinek } 44051300Sgjelinek } 44061300Sgjelinek } 44071300Sgjelinek 44081300Sgjelinek return ((err == Z_OK) ? Z_OK : Z_ERR); 44091300Sgjelinek } 44101300Sgjelinek 44111507Sgjelinek static int 44121507Sgjelinek detach_func(int argc, char *argv[]) 44131507Sgjelinek { 44141507Sgjelinek int lockfd; 44151507Sgjelinek int err, arg; 44161507Sgjelinek char zonepath[MAXPATHLEN]; 44171507Sgjelinek zone_dochandle_t handle; 44182078Sgjelinek boolean_t execute = B_TRUE; 44191507Sgjelinek 44201507Sgjelinek if (zonecfg_in_alt_root()) { 44211507Sgjelinek zerror(gettext("cannot detach zone in alternate root")); 44221507Sgjelinek return (Z_ERR); 44231507Sgjelinek } 44241507Sgjelinek 44251507Sgjelinek optind = 0; 44262078Sgjelinek if ((arg = getopt(argc, argv, "?n")) != EOF) { 44271507Sgjelinek switch (arg) { 44281507Sgjelinek case '?': 44291507Sgjelinek sub_usage(SHELP_DETACH, CMD_DETACH); 44301507Sgjelinek return (optopt == '?' ? Z_OK : Z_USAGE); 44312078Sgjelinek case 'n': 44322078Sgjelinek execute = B_FALSE; 44332078Sgjelinek break; 44341507Sgjelinek default: 44351507Sgjelinek sub_usage(SHELP_DETACH, CMD_DETACH); 44361507Sgjelinek return (Z_USAGE); 44371507Sgjelinek } 44381507Sgjelinek } 44392712Snn35248 44402078Sgjelinek if (execute) { 44412712Snn35248 if (sanity_check(target_zone, CMD_DETACH, B_FALSE, B_TRUE, 44422712Snn35248 B_FALSE) != Z_OK) 44432078Sgjelinek return (Z_ERR); 44443339Szt129084 if (verify_details(CMD_DETACH, argv) != Z_OK) 44452078Sgjelinek return (Z_ERR); 44462078Sgjelinek } else { 44472078Sgjelinek /* 44482078Sgjelinek * We want a dry-run to work for a non-privileged user so we 44492078Sgjelinek * only do minimal validation. 44502078Sgjelinek */ 44512078Sgjelinek if (target_zone == NULL) { 44522078Sgjelinek zerror(gettext("no zone specified")); 44532078Sgjelinek return (Z_ERR); 44542078Sgjelinek } 44552078Sgjelinek 44562078Sgjelinek if (strcmp(target_zone, GLOBAL_ZONENAME) == 0) { 44572078Sgjelinek zerror(gettext("%s operation is invalid for the " 44582078Sgjelinek "global zone."), cmd_to_str(CMD_DETACH)); 44592078Sgjelinek return (Z_ERR); 44602078Sgjelinek } 44612078Sgjelinek } 44621507Sgjelinek 44631507Sgjelinek if ((err = zone_get_zonepath(target_zone, zonepath, sizeof (zonepath))) 44641507Sgjelinek != Z_OK) { 44651507Sgjelinek errno = err; 44661507Sgjelinek zperror2(target_zone, gettext("could not get zone path")); 44671507Sgjelinek return (Z_ERR); 44681507Sgjelinek } 44691507Sgjelinek 44701507Sgjelinek /* Don't detach the zone if anything is still mounted there */ 44712078Sgjelinek if (execute && zonecfg_find_mounts(zonepath, NULL, NULL)) { 44721867Sgjelinek zerror(gettext("These file systems are mounted on " 44731507Sgjelinek "subdirectories of %s.\n"), zonepath); 44741507Sgjelinek (void) zonecfg_find_mounts(zonepath, zfm_print, NULL); 44751507Sgjelinek return (Z_ERR); 44761507Sgjelinek } 44771507Sgjelinek 44781507Sgjelinek if ((handle = zonecfg_init_handle()) == NULL) { 44791507Sgjelinek zperror(cmd_to_str(CMD_DETACH), B_TRUE); 44801507Sgjelinek return (Z_ERR); 44811507Sgjelinek } 44821507Sgjelinek 44831507Sgjelinek if ((err = zonecfg_get_handle(target_zone, handle)) != Z_OK) { 44841507Sgjelinek errno = err; 44851507Sgjelinek zperror(cmd_to_str(CMD_DETACH), B_TRUE); 44861507Sgjelinek zonecfg_fini_handle(handle); 44871507Sgjelinek return (Z_ERR); 44881507Sgjelinek } 44891507Sgjelinek 44902078Sgjelinek if (execute && grab_lock_file(target_zone, &lockfd) != Z_OK) { 44911507Sgjelinek zerror(gettext("another %s may have an operation in progress."), 44921507Sgjelinek "zoneadm"); 44931507Sgjelinek zonecfg_fini_handle(handle); 44941507Sgjelinek return (Z_ERR); 44951507Sgjelinek } 44961507Sgjelinek 44971507Sgjelinek if ((err = zonecfg_get_detach_info(handle, B_TRUE)) != Z_OK) { 44981507Sgjelinek errno = err; 44991507Sgjelinek zperror(gettext("getting the detach information failed"), 45001507Sgjelinek B_TRUE); 45011507Sgjelinek goto done; 45021507Sgjelinek } 45031507Sgjelinek 45042078Sgjelinek if ((err = zonecfg_detach_save(handle, (execute ? 0 : ZONE_DRY_RUN))) 45052078Sgjelinek != Z_OK) { 45061507Sgjelinek errno = err; 45071507Sgjelinek zperror(gettext("saving the detach manifest failed"), B_TRUE); 45081507Sgjelinek goto done; 45091507Sgjelinek } 45101507Sgjelinek 45112078Sgjelinek /* 45122078Sgjelinek * Set the zone state back to configured unless we are running with the 45132078Sgjelinek * no-execute option. 45142078Sgjelinek */ 45152078Sgjelinek if (execute && (err = zone_set_state(target_zone, 45162078Sgjelinek ZONE_STATE_CONFIGURED)) != Z_OK) { 45171507Sgjelinek errno = err; 45181507Sgjelinek zperror(gettext("could not reset state"), B_TRUE); 45191507Sgjelinek } 45201507Sgjelinek 45211507Sgjelinek done: 45221507Sgjelinek zonecfg_fini_handle(handle); 45232078Sgjelinek if (execute) 45242078Sgjelinek release_lock_file(lockfd); 45251507Sgjelinek 45261507Sgjelinek return ((err == Z_OK) ? Z_OK : Z_ERR); 45271507Sgjelinek } 45281507Sgjelinek 45291507Sgjelinek /* 45301507Sgjelinek * During attach we go through and fix up the /dev entries for the zone 45311507Sgjelinek * we are attaching. In order to regenerate /dev with the correct devices, 45321507Sgjelinek * the old /dev will be removed, the zone readied (which generates a new 45331507Sgjelinek * /dev) then halted, then we use the info from the manifest to update 45341507Sgjelinek * the modes, owners, etc. on the new /dev. 45351507Sgjelinek */ 45361507Sgjelinek static int 45371507Sgjelinek dev_fix(zone_dochandle_t handle) 45381507Sgjelinek { 45391507Sgjelinek int res; 45401507Sgjelinek int err; 45411507Sgjelinek int status; 45421507Sgjelinek struct zone_devpermtab devtab; 45431507Sgjelinek zone_cmd_arg_t zarg; 45441507Sgjelinek char devpath[MAXPATHLEN]; 45451507Sgjelinek /* 6: "exec " and " " */ 45461507Sgjelinek char cmdbuf[sizeof (RMCOMMAND) + MAXPATHLEN + 6]; 45471507Sgjelinek 45481507Sgjelinek if ((res = zonecfg_get_zonepath(handle, devpath, sizeof (devpath))) 45491507Sgjelinek != Z_OK) 45501507Sgjelinek return (res); 45511507Sgjelinek 45521507Sgjelinek if (strlcat(devpath, "/dev", sizeof (devpath)) >= sizeof (devpath)) 45531507Sgjelinek return (Z_TOO_BIG); 45541507Sgjelinek 45551507Sgjelinek /* 45561507Sgjelinek * "exec" the command so that the returned status is that of 45571507Sgjelinek * RMCOMMAND and not the shell. 45581507Sgjelinek */ 45592712Snn35248 (void) snprintf(cmdbuf, sizeof (cmdbuf), EXEC_PREFIX RMCOMMAND " %s", 45601507Sgjelinek devpath); 45611507Sgjelinek status = do_subproc(cmdbuf); 45622712Snn35248 if ((err = subproc_status(RMCOMMAND, status, B_TRUE)) != 45632712Snn35248 ZONE_SUBPROC_OK) { 45641507Sgjelinek (void) fprintf(stderr, 45651507Sgjelinek gettext("could not remove existing /dev\n")); 45661507Sgjelinek return (Z_ERR); 45671507Sgjelinek } 45681507Sgjelinek 45691507Sgjelinek /* In order to ready the zone, it must be in the installed state */ 45701507Sgjelinek if ((err = zone_set_state(target_zone, ZONE_STATE_INSTALLED)) != Z_OK) { 45711507Sgjelinek errno = err; 45721507Sgjelinek zperror(gettext("could not reset state"), B_TRUE); 45731507Sgjelinek return (Z_ERR); 45741507Sgjelinek } 45751507Sgjelinek 45761507Sgjelinek /* We have to ready the zone to regen the dev tree */ 45771507Sgjelinek zarg.cmd = Z_READY; 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 45851507Sgjelinek zarg.cmd = Z_HALT; 45861507Sgjelinek if (call_zoneadmd(target_zone, &zarg) != 0) { 45871507Sgjelinek zerror(gettext("call to %s failed"), "zoneadmd"); 45883247Sgjelinek /* attempt to restore zone to configured state */ 45893247Sgjelinek (void) zone_set_state(target_zone, ZONE_STATE_CONFIGURED); 45901507Sgjelinek return (Z_ERR); 45911507Sgjelinek } 45921507Sgjelinek 45933247Sgjelinek /* attempt to restore zone to configured state */ 45943247Sgjelinek (void) zone_set_state(target_zone, ZONE_STATE_CONFIGURED); 45953247Sgjelinek 45961507Sgjelinek if (zonecfg_setdevperment(handle) != Z_OK) { 45971507Sgjelinek (void) fprintf(stderr, 45981507Sgjelinek gettext("unable to enumerate device entries\n")); 45991507Sgjelinek return (Z_ERR); 46001507Sgjelinek } 46011507Sgjelinek 46021507Sgjelinek while (zonecfg_getdevperment(handle, &devtab) == Z_OK) { 46031507Sgjelinek int err; 46041507Sgjelinek 46051507Sgjelinek if ((err = zonecfg_devperms_apply(handle, 46061507Sgjelinek devtab.zone_devperm_name, devtab.zone_devperm_uid, 46071507Sgjelinek devtab.zone_devperm_gid, devtab.zone_devperm_mode, 46081507Sgjelinek devtab.zone_devperm_acl)) != Z_OK && err != Z_INVAL) 46091507Sgjelinek (void) fprintf(stderr, gettext("error updating device " 46101507Sgjelinek "%s: %s\n"), devtab.zone_devperm_name, 46111507Sgjelinek zonecfg_strerror(err)); 46121507Sgjelinek 46131507Sgjelinek free(devtab.zone_devperm_acl); 46141507Sgjelinek } 46151507Sgjelinek 46161507Sgjelinek (void) zonecfg_enddevperment(handle); 46171507Sgjelinek 46181507Sgjelinek return (Z_OK); 46191507Sgjelinek } 46201507Sgjelinek 46212078Sgjelinek /* 46222078Sgjelinek * Validate attaching a zone but don't actually do the work. The zone 46232078Sgjelinek * does not have to exist, so there is some complexity getting a new zone 46242078Sgjelinek * configuration set up so that we can perform the validation. This is 46252078Sgjelinek * handled within zonecfg_attach_manifest() which returns two handles; one 46262078Sgjelinek * for the the full configuration to validate (rem_handle) and the other 46272078Sgjelinek * (local_handle) containing only the zone configuration derived from the 46282078Sgjelinek * manifest. 46292078Sgjelinek */ 46302078Sgjelinek static int 46313339Szt129084 dryrun_attach(char *manifest_path, char *argv[]) 46322078Sgjelinek { 46332078Sgjelinek int fd; 46342078Sgjelinek int err; 46352078Sgjelinek int res; 46362078Sgjelinek zone_dochandle_t local_handle; 46372078Sgjelinek zone_dochandle_t rem_handle = NULL; 46382078Sgjelinek 46392078Sgjelinek if (strcmp(manifest_path, "-") == 0) { 46402078Sgjelinek fd = 0; 46412078Sgjelinek } else if ((fd = open(manifest_path, O_RDONLY)) < 0) { 46422078Sgjelinek zperror(gettext("could not open manifest path"), B_FALSE); 46432078Sgjelinek return (Z_ERR); 46442078Sgjelinek } 46452078Sgjelinek 46462078Sgjelinek if ((local_handle = zonecfg_init_handle()) == NULL) { 46472078Sgjelinek zperror(cmd_to_str(CMD_ATTACH), B_TRUE); 46482078Sgjelinek res = Z_ERR; 46492078Sgjelinek goto done; 46502078Sgjelinek } 46512078Sgjelinek 46522078Sgjelinek if ((rem_handle = zonecfg_init_handle()) == NULL) { 46532078Sgjelinek zperror(cmd_to_str(CMD_ATTACH), B_TRUE); 46542078Sgjelinek res = Z_ERR; 46552078Sgjelinek goto done; 46562078Sgjelinek } 46572078Sgjelinek 46582078Sgjelinek if ((err = zonecfg_attach_manifest(fd, local_handle, rem_handle)) 46592078Sgjelinek != Z_OK) { 46603686Sgjelinek res = Z_ERR; 46613686Sgjelinek 46623686Sgjelinek if (err == Z_INVALID_DOCUMENT) { 46633686Sgjelinek struct stat st; 46643686Sgjelinek char buf[6]; 46653686Sgjelinek 46663686Sgjelinek if (strcmp(manifest_path, "-") == 0) { 46673686Sgjelinek zerror(gettext("Input is not a valid XML " 46683686Sgjelinek "file")); 46693686Sgjelinek goto done; 46703686Sgjelinek } 46713686Sgjelinek 46723686Sgjelinek if (fstat(fd, &st) == -1 || !S_ISREG(st.st_mode)) { 46733686Sgjelinek zerror(gettext("%s is not an XML file"), 46743686Sgjelinek manifest_path); 46753686Sgjelinek goto done; 46763686Sgjelinek } 46773686Sgjelinek 46783686Sgjelinek bzero(buf, sizeof (buf)); 46793686Sgjelinek (void) lseek(fd, 0L, SEEK_SET); 46803686Sgjelinek if (read(fd, buf, sizeof (buf) - 1) < 0 || 46813686Sgjelinek strncmp(buf, "<?xml", 5) != 0) 46823686Sgjelinek zerror(gettext("%s is not an XML file"), 46833686Sgjelinek manifest_path); 46843686Sgjelinek else 46853686Sgjelinek zerror(gettext("Cannot attach to an earlier " 46863686Sgjelinek "release of the operating system")); 46873686Sgjelinek } else { 46882078Sgjelinek zperror(cmd_to_str(CMD_ATTACH), B_TRUE); 46893686Sgjelinek } 46902078Sgjelinek goto done; 46912078Sgjelinek } 46922078Sgjelinek 46933172Sgjelinek /* 46943172Sgjelinek * Retrieve remote handle brand type and determine whether it is 46953172Sgjelinek * native or not. 46963172Sgjelinek */ 46973172Sgjelinek if (zonecfg_get_brand(rem_handle, target_brand, sizeof (target_brand)) 46983172Sgjelinek != Z_OK) { 46993172Sgjelinek zerror(gettext("missing or invalid brand")); 47003172Sgjelinek exit(Z_ERR); 47013172Sgjelinek } 47023172Sgjelinek is_native_zone = (strcmp(target_brand, NATIVE_BRAND_NAME) == 0); 47033172Sgjelinek 47043339Szt129084 res = verify_handle(CMD_ATTACH, local_handle, argv); 47052078Sgjelinek 47062078Sgjelinek /* Get the detach information for the locally defined zone. */ 47072078Sgjelinek if ((err = zonecfg_get_detach_info(local_handle, B_FALSE)) != Z_OK) { 47082078Sgjelinek errno = err; 47092078Sgjelinek zperror(gettext("getting the attach information failed"), 47102078Sgjelinek B_TRUE); 47112078Sgjelinek res = Z_ERR; 47122078Sgjelinek } else { 47132078Sgjelinek /* sw_cmp prints error msgs as necessary */ 47142078Sgjelinek if (sw_cmp(local_handle, rem_handle, SW_CMP_NONE) != Z_OK) 47152078Sgjelinek res = Z_ERR; 47162078Sgjelinek } 47172078Sgjelinek 47182078Sgjelinek done: 47192078Sgjelinek if (strcmp(manifest_path, "-") != 0) 47202078Sgjelinek (void) close(fd); 47212078Sgjelinek 47222078Sgjelinek zonecfg_fini_handle(local_handle); 47232078Sgjelinek zonecfg_fini_handle(rem_handle); 47242078Sgjelinek 47252078Sgjelinek return ((res == Z_OK) ? Z_OK : Z_ERR); 47262078Sgjelinek } 47272078Sgjelinek 4728*3777Sgjelinek /* 4729*3777Sgjelinek * Attempt to generate the information we need to make the zone look like 4730*3777Sgjelinek * it was properly detached by using the pkg information contained within 4731*3777Sgjelinek * the zone itself. 4732*3777Sgjelinek * 4733*3777Sgjelinek * We will perform a dry-run detach within the zone to generate the xml file. 4734*3777Sgjelinek * To do this we need to be able to get a handle on the zone so we can see 4735*3777Sgjelinek * how it is configured. In order to get a handle, we need a copy of the 4736*3777Sgjelinek * zone configuration within the zone. Since the zone's configuration is 4737*3777Sgjelinek * not available within the zone itself, we need to temporarily copy it into 4738*3777Sgjelinek * the zone. 4739*3777Sgjelinek * 4740*3777Sgjelinek * The sequence of actions we are doing is this: 4741*3777Sgjelinek * [set zone state to installed] 4742*3777Sgjelinek * [mount zone] 4743*3777Sgjelinek * zlogin {zone} </etc/zones/{zone}.xml 'cat >/etc/zones/{zone}.xml' 4744*3777Sgjelinek * zlogin {zone} 'zoneadm -z {zone} detach -n' >{zonepath}/SUNWdetached.xml 4745*3777Sgjelinek * zlogin {zone} 'rm -f /etc/zones/{zone}.xml' 4746*3777Sgjelinek * [unmount zone] 4747*3777Sgjelinek * [set zone state to configured] 4748*3777Sgjelinek * 4749*3777Sgjelinek * The successful result of this function is that we will have a 4750*3777Sgjelinek * SUNWdetached.xml file in the zonepath and we can use that to attach the zone. 4751*3777Sgjelinek */ 4752*3777Sgjelinek static boolean_t 4753*3777Sgjelinek gen_detach_info(char *zonepath) 4754*3777Sgjelinek { 4755*3777Sgjelinek int status; 4756*3777Sgjelinek boolean_t mounted = B_FALSE; 4757*3777Sgjelinek boolean_t res = B_FALSE; 4758*3777Sgjelinek char cmdbuf[2 * MAXPATHLEN]; 4759*3777Sgjelinek 4760*3777Sgjelinek /* 4761*3777Sgjelinek * The zone has to be installed to mount and zlogin. Temporarily set 4762*3777Sgjelinek * the state to 'installed'. 4763*3777Sgjelinek */ 4764*3777Sgjelinek if (zone_set_state(target_zone, ZONE_STATE_INSTALLED) != Z_OK) 4765*3777Sgjelinek return (B_FALSE); 4766*3777Sgjelinek 4767*3777Sgjelinek /* Mount the zone so we can zlogin. */ 4768*3777Sgjelinek if (mount_func(0, NULL) != Z_OK) 4769*3777Sgjelinek goto cleanup; 4770*3777Sgjelinek mounted = B_TRUE; 4771*3777Sgjelinek 4772*3777Sgjelinek /* 4773*3777Sgjelinek * We need to copy the zones xml configuration file into the 4774*3777Sgjelinek * zone so we can get a handle for the zone while running inside 4775*3777Sgjelinek * the zone. 4776*3777Sgjelinek */ 4777*3777Sgjelinek if (snprintf(cmdbuf, sizeof (cmdbuf), "/usr/sbin/zlogin -S %s " 4778*3777Sgjelinek "</etc/zones/%s.xml '/usr/bin/cat >/etc/zones/%s.xml'", 4779*3777Sgjelinek target_zone, target_zone, target_zone) >= sizeof (cmdbuf)) 4780*3777Sgjelinek goto cleanup; 4781*3777Sgjelinek 4782*3777Sgjelinek status = do_subproc_interactive(cmdbuf); 4783*3777Sgjelinek if (subproc_status("copy", status, B_TRUE) != ZONE_SUBPROC_OK) 4784*3777Sgjelinek goto cleanup; 4785*3777Sgjelinek 4786*3777Sgjelinek /* Now run the detach command within the mounted zone. */ 4787*3777Sgjelinek if (snprintf(cmdbuf, sizeof (cmdbuf), "/usr/sbin/zlogin -S %s " 4788*3777Sgjelinek "'/usr/sbin/zoneadm -z %s detach -n' >%s/SUNWdetached.xml", 4789*3777Sgjelinek target_zone, target_zone, zonepath) >= sizeof (cmdbuf)) 4790*3777Sgjelinek goto cleanup; 4791*3777Sgjelinek 4792*3777Sgjelinek status = do_subproc_interactive(cmdbuf); 4793*3777Sgjelinek if (subproc_status("detach", status, B_TRUE) != ZONE_SUBPROC_OK) 4794*3777Sgjelinek goto cleanup; 4795*3777Sgjelinek 4796*3777Sgjelinek res = B_TRUE; 4797*3777Sgjelinek 4798*3777Sgjelinek cleanup: 4799*3777Sgjelinek /* Cleanup from the previous actions. */ 4800*3777Sgjelinek if (mounted) { 4801*3777Sgjelinek if (snprintf(cmdbuf, sizeof (cmdbuf), 4802*3777Sgjelinek "/usr/sbin/zlogin -S %s '/usr/bin/rm -f /etc/zones/%s.xml'", 4803*3777Sgjelinek target_zone, target_zone) >= sizeof (cmdbuf)) { 4804*3777Sgjelinek res = B_FALSE; 4805*3777Sgjelinek } else { 4806*3777Sgjelinek status = do_subproc(cmdbuf); 4807*3777Sgjelinek if (subproc_status("rm", status, B_TRUE) 4808*3777Sgjelinek != ZONE_SUBPROC_OK) 4809*3777Sgjelinek res = B_FALSE; 4810*3777Sgjelinek } 4811*3777Sgjelinek 4812*3777Sgjelinek if (unmount_func(0, NULL) != Z_OK) 4813*3777Sgjelinek res = B_FALSE; 4814*3777Sgjelinek } 4815*3777Sgjelinek 4816*3777Sgjelinek if (zone_set_state(target_zone, ZONE_STATE_CONFIGURED) != Z_OK) 4817*3777Sgjelinek res = B_FALSE; 4818*3777Sgjelinek 4819*3777Sgjelinek return (res); 4820*3777Sgjelinek } 4821*3777Sgjelinek 48221507Sgjelinek static int 48231507Sgjelinek attach_func(int argc, char *argv[]) 48241507Sgjelinek { 48251507Sgjelinek int lockfd; 48261507Sgjelinek int err, arg; 48271507Sgjelinek boolean_t force = B_FALSE; 48281507Sgjelinek zone_dochandle_t handle; 48291507Sgjelinek zone_dochandle_t athandle = NULL; 48301507Sgjelinek char zonepath[MAXPATHLEN]; 48312712Snn35248 char brand[MAXNAMELEN], atbrand[MAXNAMELEN]; 48322078Sgjelinek boolean_t execute = B_TRUE; 4833*3777Sgjelinek boolean_t retried = B_FALSE; 48342078Sgjelinek char *manifest_path; 48351507Sgjelinek 48361507Sgjelinek if (zonecfg_in_alt_root()) { 48371507Sgjelinek zerror(gettext("cannot attach zone in alternate root")); 48381507Sgjelinek return (Z_ERR); 48391507Sgjelinek } 48401507Sgjelinek 48411507Sgjelinek optind = 0; 48422078Sgjelinek if ((arg = getopt(argc, argv, "?Fn:")) != EOF) { 48431507Sgjelinek switch (arg) { 48441507Sgjelinek case '?': 48451507Sgjelinek sub_usage(SHELP_ATTACH, CMD_ATTACH); 48461507Sgjelinek return (optopt == '?' ? Z_OK : Z_USAGE); 48471507Sgjelinek case 'F': 48481507Sgjelinek force = B_TRUE; 48491507Sgjelinek break; 48502078Sgjelinek case 'n': 48512078Sgjelinek execute = B_FALSE; 48522078Sgjelinek manifest_path = optarg; 48532078Sgjelinek break; 48541507Sgjelinek default: 48551507Sgjelinek sub_usage(SHELP_ATTACH, CMD_ATTACH); 48561507Sgjelinek return (Z_USAGE); 48571507Sgjelinek } 48581507Sgjelinek } 48592078Sgjelinek 48602078Sgjelinek /* 48612078Sgjelinek * If the no-execute option was specified, we need to branch down 48622078Sgjelinek * a completely different path since there is no zone required to be 48632078Sgjelinek * configured for this option. 48642078Sgjelinek */ 48652078Sgjelinek if (!execute) 48663339Szt129084 return (dryrun_attach(manifest_path, argv)); 48672078Sgjelinek 48682712Snn35248 if (sanity_check(target_zone, CMD_ATTACH, B_FALSE, B_TRUE, B_FALSE) 48692712Snn35248 != Z_OK) 48701507Sgjelinek return (Z_ERR); 48713339Szt129084 if (verify_details(CMD_ATTACH, argv) != Z_OK) 48721507Sgjelinek return (Z_ERR); 48731507Sgjelinek 48741507Sgjelinek if ((err = zone_get_zonepath(target_zone, zonepath, sizeof (zonepath))) 48751507Sgjelinek != Z_OK) { 48761507Sgjelinek errno = err; 48771507Sgjelinek zperror2(target_zone, gettext("could not get zone path")); 48781507Sgjelinek return (Z_ERR); 48791507Sgjelinek } 48801507Sgjelinek 48811507Sgjelinek if ((handle = zonecfg_init_handle()) == NULL) { 48821507Sgjelinek zperror(cmd_to_str(CMD_ATTACH), B_TRUE); 48831507Sgjelinek return (Z_ERR); 48841507Sgjelinek } 48851507Sgjelinek 48861507Sgjelinek if ((err = zonecfg_get_handle(target_zone, handle)) != Z_OK) { 48871507Sgjelinek errno = err; 48881507Sgjelinek zperror(cmd_to_str(CMD_ATTACH), B_TRUE); 48891507Sgjelinek zonecfg_fini_handle(handle); 48901507Sgjelinek return (Z_ERR); 48911507Sgjelinek } 48921507Sgjelinek 48931507Sgjelinek if (grab_lock_file(target_zone, &lockfd) != Z_OK) { 48941507Sgjelinek zerror(gettext("another %s may have an operation in progress."), 48951507Sgjelinek "zoneadm"); 48961507Sgjelinek zonecfg_fini_handle(handle); 48971507Sgjelinek return (Z_ERR); 48981507Sgjelinek } 48991507Sgjelinek 49001507Sgjelinek if (force) 49011507Sgjelinek goto forced; 49021507Sgjelinek 49031507Sgjelinek if ((athandle = zonecfg_init_handle()) == NULL) { 49041507Sgjelinek zperror(cmd_to_str(CMD_ATTACH), B_TRUE); 49051507Sgjelinek goto done; 49061507Sgjelinek } 49071507Sgjelinek 4908*3777Sgjelinek retry: 49091507Sgjelinek if ((err = zonecfg_get_attach_handle(zonepath, target_zone, B_TRUE, 49101507Sgjelinek athandle)) != Z_OK) { 4911*3777Sgjelinek if (err == Z_NO_ZONE) { 4912*3777Sgjelinek /* 4913*3777Sgjelinek * Zone was not detached. Try to fall back to getting 4914*3777Sgjelinek * the needed information from within the zone. 4915*3777Sgjelinek * However, we can only try to generate the attach 4916*3777Sgjelinek * information for native branded zones, so fail if the 4917*3777Sgjelinek * zone is not native. 4918*3777Sgjelinek */ 4919*3777Sgjelinek if (!is_native_zone) { 4920*3777Sgjelinek zerror(gettext("Not a detached zone.")); 4921*3777Sgjelinek goto done; 4922*3777Sgjelinek } 4923*3777Sgjelinek 4924*3777Sgjelinek if (!retried) { 4925*3777Sgjelinek zerror(gettext("The zone was not properly " 4926*3777Sgjelinek "detached.\n\tAttempting to attach " 4927*3777Sgjelinek "anyway.")); 4928*3777Sgjelinek if (gen_detach_info(zonepath)) { 4929*3777Sgjelinek retried = B_TRUE; 4930*3777Sgjelinek goto retry; 4931*3777Sgjelinek } 4932*3777Sgjelinek } 4933*3777Sgjelinek zerror(gettext("Cannot generate the information " 4934*3777Sgjelinek "needed to attach this zone.")); 4935*3777Sgjelinek } else if (err == Z_INVALID_DOCUMENT) { 49361507Sgjelinek zerror(gettext("Cannot attach to an earlier release " 49371507Sgjelinek "of the operating system")); 4938*3777Sgjelinek } else { 49391507Sgjelinek zperror(cmd_to_str(CMD_ATTACH), B_TRUE); 4940*3777Sgjelinek } 49411507Sgjelinek goto done; 49421507Sgjelinek } 49431507Sgjelinek 49441507Sgjelinek /* Get the detach information for the locally defined zone. */ 49451507Sgjelinek if ((err = zonecfg_get_detach_info(handle, B_FALSE)) != Z_OK) { 49461507Sgjelinek errno = err; 49471507Sgjelinek zperror(gettext("getting the attach information failed"), 49481507Sgjelinek B_TRUE); 49491507Sgjelinek goto done; 49501507Sgjelinek } 49511507Sgjelinek 49522712Snn35248 /* 49532712Snn35248 * Ensure that the detached and locally defined zones are both of 49542712Snn35248 * the same brand. 49552712Snn35248 */ 49562712Snn35248 if ((zonecfg_get_brand(handle, brand, sizeof (brand)) != 0) || 49572712Snn35248 (zonecfg_get_brand(athandle, atbrand, sizeof (atbrand)) != 0)) { 49582712Snn35248 err = Z_ERR; 49592712Snn35248 zerror(gettext("missing or invalid brand")); 49602712Snn35248 goto done; 49612712Snn35248 } 49622712Snn35248 49632712Snn35248 if (strcmp(atbrand, brand) != NULL) { 49642712Snn35248 err = Z_ERR; 49652712Snn35248 zerror(gettext("Trying to attach a '%s' zone to a '%s' " 49662712Snn35248 "configuration."), atbrand, brand); 49672712Snn35248 goto done; 49682712Snn35248 } 49692712Snn35248 49701507Sgjelinek /* sw_cmp prints error msgs as necessary */ 49711867Sgjelinek if ((err = sw_cmp(handle, athandle, SW_CMP_NONE)) != Z_OK) 49721507Sgjelinek goto done; 49731507Sgjelinek 49741507Sgjelinek if ((err = dev_fix(athandle)) != Z_OK) 49751507Sgjelinek goto done; 49761507Sgjelinek 49771507Sgjelinek forced: 49781507Sgjelinek 49791507Sgjelinek zonecfg_rm_detached(handle, force); 49801507Sgjelinek 49811507Sgjelinek if ((err = zone_set_state(target_zone, ZONE_STATE_INSTALLED)) != Z_OK) { 49821507Sgjelinek errno = err; 49831507Sgjelinek zperror(gettext("could not reset state"), B_TRUE); 49841507Sgjelinek } 49851507Sgjelinek 49861507Sgjelinek done: 49871507Sgjelinek zonecfg_fini_handle(handle); 49881507Sgjelinek release_lock_file(lockfd); 49891507Sgjelinek if (athandle != NULL) 49901507Sgjelinek zonecfg_fini_handle(athandle); 49911507Sgjelinek 49921507Sgjelinek return ((err == Z_OK) ? Z_OK : Z_ERR); 49931507Sgjelinek } 49941507Sgjelinek 49951300Sgjelinek /* 49960Sstevel@tonic-gate * On input, TRUE => yes, FALSE => no. 49970Sstevel@tonic-gate * On return, TRUE => 1, FALSE => 0, could not ask => -1. 49980Sstevel@tonic-gate */ 49990Sstevel@tonic-gate 50000Sstevel@tonic-gate static int 50010Sstevel@tonic-gate ask_yesno(boolean_t default_answer, const char *question) 50020Sstevel@tonic-gate { 50030Sstevel@tonic-gate char line[64]; /* should be large enough to answer yes or no */ 50040Sstevel@tonic-gate 50050Sstevel@tonic-gate if (!isatty(STDIN_FILENO)) 50060Sstevel@tonic-gate return (-1); 50070Sstevel@tonic-gate for (;;) { 50080Sstevel@tonic-gate (void) printf("%s (%s)? ", question, 50090Sstevel@tonic-gate default_answer ? "[y]/n" : "y/[n]"); 50100Sstevel@tonic-gate if (fgets(line, sizeof (line), stdin) == NULL || 50110Sstevel@tonic-gate line[0] == '\n') 50120Sstevel@tonic-gate return (default_answer ? 1 : 0); 50130Sstevel@tonic-gate if (tolower(line[0]) == 'y') 50140Sstevel@tonic-gate return (1); 50150Sstevel@tonic-gate if (tolower(line[0]) == 'n') 50160Sstevel@tonic-gate return (0); 50170Sstevel@tonic-gate } 50180Sstevel@tonic-gate } 50190Sstevel@tonic-gate 50200Sstevel@tonic-gate static int 50210Sstevel@tonic-gate uninstall_func(int argc, char *argv[]) 50220Sstevel@tonic-gate { 50230Sstevel@tonic-gate char line[ZONENAME_MAX + 128]; /* Enough for "Are you sure ..." */ 50241867Sgjelinek char rootpath[MAXPATHLEN], zonepath[MAXPATHLEN]; 50250Sstevel@tonic-gate boolean_t force = B_FALSE; 50260Sstevel@tonic-gate int lockfd, answer; 50270Sstevel@tonic-gate int err, arg; 50280Sstevel@tonic-gate 5029766Scarlsonj if (zonecfg_in_alt_root()) { 5030766Scarlsonj zerror(gettext("cannot uninstall zone in alternate root")); 5031766Scarlsonj return (Z_ERR); 5032766Scarlsonj } 5033766Scarlsonj 50340Sstevel@tonic-gate optind = 0; 50350Sstevel@tonic-gate while ((arg = getopt(argc, argv, "?F")) != EOF) { 50360Sstevel@tonic-gate switch (arg) { 50370Sstevel@tonic-gate case '?': 50380Sstevel@tonic-gate sub_usage(SHELP_UNINSTALL, CMD_UNINSTALL); 50390Sstevel@tonic-gate return (optopt == '?' ? Z_OK : Z_USAGE); 50400Sstevel@tonic-gate case 'F': 50410Sstevel@tonic-gate force = B_TRUE; 50420Sstevel@tonic-gate break; 50430Sstevel@tonic-gate default: 50440Sstevel@tonic-gate sub_usage(SHELP_UNINSTALL, CMD_UNINSTALL); 50450Sstevel@tonic-gate return (Z_USAGE); 50460Sstevel@tonic-gate } 50470Sstevel@tonic-gate } 50480Sstevel@tonic-gate if (argc > optind) { 50490Sstevel@tonic-gate sub_usage(SHELP_UNINSTALL, CMD_UNINSTALL); 50500Sstevel@tonic-gate return (Z_USAGE); 50510Sstevel@tonic-gate } 50520Sstevel@tonic-gate 50532712Snn35248 if (sanity_check(target_zone, CMD_UNINSTALL, B_FALSE, B_TRUE, B_FALSE) 50542712Snn35248 != Z_OK) 50550Sstevel@tonic-gate return (Z_ERR); 50560Sstevel@tonic-gate 50573339Szt129084 /* 50583339Szt129084 * Invoke brand-specific handler. 50593339Szt129084 */ 50603339Szt129084 if (invoke_brand_handler(CMD_UNINSTALL, argv) != Z_OK) 50613339Szt129084 return (Z_ERR); 50623339Szt129084 50630Sstevel@tonic-gate if (!force) { 50640Sstevel@tonic-gate (void) snprintf(line, sizeof (line), 50650Sstevel@tonic-gate gettext("Are you sure you want to %s zone %s"), 50660Sstevel@tonic-gate cmd_to_str(CMD_UNINSTALL), target_zone); 50670Sstevel@tonic-gate if ((answer = ask_yesno(B_FALSE, line)) == 0) { 50680Sstevel@tonic-gate return (Z_OK); 50690Sstevel@tonic-gate } else if (answer == -1) { 50700Sstevel@tonic-gate zerror(gettext("Input not from terminal and -F " 50710Sstevel@tonic-gate "not specified: %s not done."), 50720Sstevel@tonic-gate cmd_to_str(CMD_UNINSTALL)); 50730Sstevel@tonic-gate return (Z_ERR); 50740Sstevel@tonic-gate } 50750Sstevel@tonic-gate } 50760Sstevel@tonic-gate 50771867Sgjelinek if ((err = zone_get_zonepath(target_zone, zonepath, 50781867Sgjelinek sizeof (zonepath))) != Z_OK) { 50790Sstevel@tonic-gate errno = err; 50800Sstevel@tonic-gate zperror2(target_zone, gettext("could not get zone path")); 50810Sstevel@tonic-gate return (Z_ERR); 50820Sstevel@tonic-gate } 50830Sstevel@tonic-gate if ((err = zone_get_rootpath(target_zone, rootpath, 50840Sstevel@tonic-gate sizeof (rootpath))) != Z_OK) { 50850Sstevel@tonic-gate errno = err; 50860Sstevel@tonic-gate zperror2(target_zone, gettext("could not get root path")); 50870Sstevel@tonic-gate return (Z_ERR); 50880Sstevel@tonic-gate } 50890Sstevel@tonic-gate 50900Sstevel@tonic-gate /* 50910Sstevel@tonic-gate * If there seems to be a zoneadmd running for this zone, call it 50920Sstevel@tonic-gate * to tell it that an uninstall is happening; if all goes well it 50930Sstevel@tonic-gate * will then shut itself down. 50940Sstevel@tonic-gate */ 50950Sstevel@tonic-gate if (ping_zoneadmd(target_zone) == Z_OK) { 50960Sstevel@tonic-gate zone_cmd_arg_t zarg; 50970Sstevel@tonic-gate zarg.cmd = Z_NOTE_UNINSTALLING; 50980Sstevel@tonic-gate /* we don't care too much if this fails... just plow on */ 50990Sstevel@tonic-gate (void) call_zoneadmd(target_zone, &zarg); 51000Sstevel@tonic-gate } 51010Sstevel@tonic-gate 51020Sstevel@tonic-gate if (grab_lock_file(target_zone, &lockfd) != Z_OK) { 51030Sstevel@tonic-gate zerror(gettext("another %s may have an operation in progress."), 51041300Sgjelinek "zoneadm"); 51050Sstevel@tonic-gate return (Z_ERR); 51060Sstevel@tonic-gate } 51070Sstevel@tonic-gate 51080Sstevel@tonic-gate /* Don't uninstall the zone if anything is mounted there */ 51090Sstevel@tonic-gate err = zonecfg_find_mounts(rootpath, NULL, NULL); 51100Sstevel@tonic-gate if (err) { 51111867Sgjelinek zerror(gettext("These file systems are mounted on " 51121645Scomay "subdirectories of %s.\n"), rootpath); 51130Sstevel@tonic-gate (void) zonecfg_find_mounts(rootpath, zfm_print, NULL); 51140Sstevel@tonic-gate return (Z_ERR); 51150Sstevel@tonic-gate } 51160Sstevel@tonic-gate 51170Sstevel@tonic-gate err = zone_set_state(target_zone, ZONE_STATE_INCOMPLETE); 51180Sstevel@tonic-gate if (err != Z_OK) { 51190Sstevel@tonic-gate errno = err; 51200Sstevel@tonic-gate zperror2(target_zone, gettext("could not set state")); 51210Sstevel@tonic-gate goto bad; 51220Sstevel@tonic-gate } 51230Sstevel@tonic-gate 51241867Sgjelinek if ((err = cleanup_zonepath(zonepath, B_FALSE)) != Z_OK) { 51251867Sgjelinek errno = err; 51261867Sgjelinek zperror2(target_zone, gettext("cleaning up zonepath failed")); 51270Sstevel@tonic-gate goto bad; 51281867Sgjelinek } 51291867Sgjelinek 51300Sstevel@tonic-gate err = zone_set_state(target_zone, ZONE_STATE_CONFIGURED); 51310Sstevel@tonic-gate if (err != Z_OK) { 51320Sstevel@tonic-gate errno = err; 51330Sstevel@tonic-gate zperror2(target_zone, gettext("could not reset state")); 51340Sstevel@tonic-gate } 51350Sstevel@tonic-gate bad: 51360Sstevel@tonic-gate release_lock_file(lockfd); 51370Sstevel@tonic-gate return (err); 51380Sstevel@tonic-gate } 51390Sstevel@tonic-gate 5140766Scarlsonj /* ARGSUSED */ 5141766Scarlsonj static int 5142766Scarlsonj mount_func(int argc, char *argv[]) 5143766Scarlsonj { 5144766Scarlsonj zone_cmd_arg_t zarg; 51452712Snn35248 boolean_t force = B_FALSE; 51462712Snn35248 int arg; 51472712Snn35248 51482712Snn35248 /* 51492712Snn35248 * The only supported subargument to the "mount" subcommand is 51502712Snn35248 * "-f", which forces us to mount a zone in the INCOMPLETE state. 51512712Snn35248 */ 51522712Snn35248 optind = 0; 51532712Snn35248 if ((arg = getopt(argc, argv, "f")) != EOF) { 51542712Snn35248 switch (arg) { 51552712Snn35248 case 'f': 51562712Snn35248 force = B_TRUE; 51572712Snn35248 break; 51582712Snn35248 default: 51592712Snn35248 return (Z_USAGE); 51602712Snn35248 } 51612712Snn35248 } 51622712Snn35248 if (argc > optind) 5163766Scarlsonj return (Z_USAGE); 51642712Snn35248 51652712Snn35248 if (sanity_check(target_zone, CMD_MOUNT, B_FALSE, B_FALSE, force) 51662712Snn35248 != Z_OK) 5167766Scarlsonj return (Z_ERR); 51683339Szt129084 if (verify_details(CMD_MOUNT, argv) != Z_OK) 5169766Scarlsonj return (Z_ERR); 5170766Scarlsonj 51712712Snn35248 zarg.cmd = force ? Z_FORCEMOUNT : Z_MOUNT; 5172766Scarlsonj if (call_zoneadmd(target_zone, &zarg) != 0) { 5173766Scarlsonj zerror(gettext("call to %s failed"), "zoneadmd"); 5174766Scarlsonj return (Z_ERR); 5175766Scarlsonj } 5176766Scarlsonj return (Z_OK); 5177766Scarlsonj } 5178766Scarlsonj 5179766Scarlsonj /* ARGSUSED */ 5180766Scarlsonj static int 5181766Scarlsonj unmount_func(int argc, char *argv[]) 5182766Scarlsonj { 5183766Scarlsonj zone_cmd_arg_t zarg; 5184766Scarlsonj 5185766Scarlsonj if (argc > 0) 5186766Scarlsonj return (Z_USAGE); 51872712Snn35248 if (sanity_check(target_zone, CMD_UNMOUNT, B_FALSE, B_FALSE, B_FALSE) 51882712Snn35248 != Z_OK) 5189766Scarlsonj return (Z_ERR); 5190766Scarlsonj 5191766Scarlsonj zarg.cmd = Z_UNMOUNT; 5192766Scarlsonj if (call_zoneadmd(target_zone, &zarg) != 0) { 5193766Scarlsonj zerror(gettext("call to %s failed"), "zoneadmd"); 5194766Scarlsonj return (Z_ERR); 5195766Scarlsonj } 5196766Scarlsonj return (Z_OK); 5197766Scarlsonj } 5198766Scarlsonj 51990Sstevel@tonic-gate static int 52002303Scarlsonj mark_func(int argc, char *argv[]) 52012303Scarlsonj { 52022303Scarlsonj int err, lockfd; 52032303Scarlsonj 52042303Scarlsonj if (argc != 1 || strcmp(argv[0], "incomplete") != 0) 52052303Scarlsonj return (Z_USAGE); 52062712Snn35248 if (sanity_check(target_zone, CMD_MARK, B_FALSE, B_FALSE, B_FALSE) 52072712Snn35248 != Z_OK) 52082303Scarlsonj return (Z_ERR); 52092303Scarlsonj 52103339Szt129084 /* 52113339Szt129084 * Invoke brand-specific handler. 52123339Szt129084 */ 52133339Szt129084 if (invoke_brand_handler(CMD_MARK, argv) != Z_OK) 52143339Szt129084 return (Z_ERR); 52153339Szt129084 52162303Scarlsonj if (grab_lock_file(target_zone, &lockfd) != Z_OK) { 52172303Scarlsonj zerror(gettext("another %s may have an operation in progress."), 52182303Scarlsonj "zoneadm"); 52192303Scarlsonj return (Z_ERR); 52202303Scarlsonj } 52212303Scarlsonj 52222303Scarlsonj err = zone_set_state(target_zone, ZONE_STATE_INCOMPLETE); 52232303Scarlsonj if (err != Z_OK) { 52242303Scarlsonj errno = err; 52252303Scarlsonj zperror2(target_zone, gettext("could not set state")); 52262303Scarlsonj } 52272303Scarlsonj release_lock_file(lockfd); 52282303Scarlsonj 52292303Scarlsonj return (err); 52302303Scarlsonj } 52312303Scarlsonj 52323247Sgjelinek /* 52333247Sgjelinek * Check what scheduling class we're running under and print a warning if 52343247Sgjelinek * we're not using FSS. 52353247Sgjelinek */ 52363247Sgjelinek static int 52373247Sgjelinek check_sched_fss(zone_dochandle_t handle) 52383247Sgjelinek { 52393247Sgjelinek char class_name[PC_CLNMSZ]; 52403247Sgjelinek 52413247Sgjelinek if (zonecfg_get_dflt_sched_class(handle, class_name, 52423247Sgjelinek sizeof (class_name)) != Z_OK) { 52433247Sgjelinek zerror(gettext("WARNING: unable to determine the zone's " 52443247Sgjelinek "scheduling class")); 52453247Sgjelinek } else if (strcmp("FSS", class_name) != 0) { 52463247Sgjelinek zerror(gettext("WARNING: The zone.cpu-shares rctl is set but\n" 52473247Sgjelinek "FSS is not the default scheduling class for this zone. " 52483247Sgjelinek "FSS will be\nused for processes in the zone but to get " 52493247Sgjelinek "the full benefit of FSS,\nit should be the default " 52503247Sgjelinek "scheduling class. See dispadmin(1M) for\nmore details.")); 52513247Sgjelinek return (Z_SYSTEM); 52523247Sgjelinek } 52533247Sgjelinek 52543247Sgjelinek return (Z_OK); 52553247Sgjelinek } 52563247Sgjelinek 52573247Sgjelinek static int 52583247Sgjelinek check_cpu_shares_sched(zone_dochandle_t handle) 52593247Sgjelinek { 52603247Sgjelinek int err; 52613247Sgjelinek int res = Z_OK; 52623247Sgjelinek struct zone_rctltab rctl; 52633247Sgjelinek 52643247Sgjelinek if ((err = zonecfg_setrctlent(handle)) != Z_OK) { 52653247Sgjelinek errno = err; 52663247Sgjelinek zperror(cmd_to_str(CMD_APPLY), B_TRUE); 52673247Sgjelinek return (err); 52683247Sgjelinek } 52693247Sgjelinek 52703247Sgjelinek while (zonecfg_getrctlent(handle, &rctl) == Z_OK) { 52713247Sgjelinek if (strcmp(rctl.zone_rctl_name, "zone.cpu-shares") == 0) { 52723247Sgjelinek if (check_sched_fss(handle) != Z_OK) 52733247Sgjelinek res = Z_SYSTEM; 52743247Sgjelinek break; 52753247Sgjelinek } 52763247Sgjelinek } 52773247Sgjelinek 52783247Sgjelinek (void) zonecfg_endrctlent(handle); 52793247Sgjelinek 52803247Sgjelinek return (res); 52813247Sgjelinek } 52823247Sgjelinek 52833247Sgjelinek /* 52843352Sgjelinek * Check if there is a mix of processes running in different pools within the 52853352Sgjelinek * zone. This is currently only going to be called for the global zone from 52863352Sgjelinek * apply_func but that could be generalized in the future. 52873352Sgjelinek */ 52883352Sgjelinek static boolean_t 52893352Sgjelinek mixed_pools(zoneid_t zoneid) 52903352Sgjelinek { 52913352Sgjelinek DIR *dirp; 52923352Sgjelinek dirent_t *dent; 52933352Sgjelinek boolean_t mixed = B_FALSE; 52943352Sgjelinek boolean_t poolid_set = B_FALSE; 52953352Sgjelinek poolid_t last_poolid = 0; 52963352Sgjelinek 52973352Sgjelinek if ((dirp = opendir("/proc")) == NULL) { 52983352Sgjelinek zerror(gettext("could not open /proc")); 52993352Sgjelinek return (B_FALSE); 53003352Sgjelinek } 53013352Sgjelinek 53023352Sgjelinek while ((dent = readdir(dirp)) != NULL) { 53033352Sgjelinek int procfd; 53043352Sgjelinek psinfo_t ps; 53053352Sgjelinek char procpath[MAXPATHLEN]; 53063352Sgjelinek 53073352Sgjelinek if (dent->d_name[0] == '.') 53083352Sgjelinek continue; 53093352Sgjelinek 53103352Sgjelinek (void) snprintf(procpath, sizeof (procpath), "/proc/%s/psinfo", 53113352Sgjelinek dent->d_name); 53123352Sgjelinek 53133352Sgjelinek if ((procfd = open(procpath, O_RDONLY)) == -1) 53143352Sgjelinek continue; 53153352Sgjelinek 53163352Sgjelinek if (read(procfd, &ps, sizeof (ps)) == sizeof (psinfo_t)) { 53173352Sgjelinek /* skip processes in other zones and system processes */ 53183352Sgjelinek if (zoneid != ps.pr_zoneid || ps.pr_flag & SSYS) { 53193352Sgjelinek (void) close(procfd); 53203352Sgjelinek continue; 53213352Sgjelinek } 53223352Sgjelinek 53233352Sgjelinek if (poolid_set) { 53243352Sgjelinek if (ps.pr_poolid != last_poolid) 53253352Sgjelinek mixed = B_TRUE; 53263352Sgjelinek } else { 53273352Sgjelinek last_poolid = ps.pr_poolid; 53283352Sgjelinek poolid_set = B_TRUE; 53293352Sgjelinek } 53303352Sgjelinek } 53313352Sgjelinek 53323352Sgjelinek (void) close(procfd); 53333352Sgjelinek 53343352Sgjelinek if (mixed) 53353352Sgjelinek break; 53363352Sgjelinek } 53373352Sgjelinek 53383352Sgjelinek (void) closedir(dirp); 53393352Sgjelinek 53403352Sgjelinek return (mixed); 53413352Sgjelinek } 53423352Sgjelinek 53433352Sgjelinek /* 53443352Sgjelinek * Check if a persistent or temporary pool is configured for the zone. 53453352Sgjelinek * This is currently only going to be called for the global zone from 53463352Sgjelinek * apply_func but that could be generalized in the future. 53473352Sgjelinek */ 53483352Sgjelinek static boolean_t 53493352Sgjelinek pool_configured(zone_dochandle_t handle) 53503352Sgjelinek { 53513352Sgjelinek int err1, err2; 53523352Sgjelinek struct zone_psettab pset_tab; 53533352Sgjelinek char poolname[MAXPATHLEN]; 53543352Sgjelinek 53553352Sgjelinek err1 = zonecfg_lookup_pset(handle, &pset_tab); 53563352Sgjelinek err2 = zonecfg_get_pool(handle, poolname, sizeof (poolname)); 53573352Sgjelinek 53583352Sgjelinek if (err1 == Z_NO_ENTRY && 53593352Sgjelinek (err2 == Z_NO_ENTRY || (err2 == Z_OK && strlen(poolname) == 0))) 53603352Sgjelinek return (B_FALSE); 53613352Sgjelinek 53623352Sgjelinek return (B_TRUE); 53633352Sgjelinek } 53643352Sgjelinek 53653352Sgjelinek /* 53663247Sgjelinek * This is an undocumented interface which is currently only used to apply 53673247Sgjelinek * the global zone resource management settings when the system boots. 53683247Sgjelinek * This function does not yet properly handle updating a running system so 53693247Sgjelinek * any projects running in the zone would be trashed if this function 53703247Sgjelinek * were to run after the zone had booted. It also does not reset any 53713247Sgjelinek * rctl settings that were removed from zonecfg. There is still work to be 53723247Sgjelinek * done before we can properly support dynamically updating the resource 53733247Sgjelinek * management settings for a running zone (global or non-global). Thus, this 53743247Sgjelinek * functionality is undocumented for now. 53753247Sgjelinek */ 53763247Sgjelinek /* ARGSUSED */ 53773247Sgjelinek static int 53783247Sgjelinek apply_func(int argc, char *argv[]) 53793247Sgjelinek { 53803247Sgjelinek int err; 53813247Sgjelinek int res = Z_OK; 53823247Sgjelinek priv_set_t *privset; 53833247Sgjelinek zoneid_t zoneid; 53843247Sgjelinek zone_dochandle_t handle; 53853247Sgjelinek struct zone_mcaptab mcap; 53863247Sgjelinek char pool_err[128]; 53873247Sgjelinek 53883247Sgjelinek zoneid = getzoneid(); 53893247Sgjelinek 53903247Sgjelinek if (zonecfg_in_alt_root() || zoneid != GLOBAL_ZONEID || 53913247Sgjelinek target_zone == NULL || strcmp(target_zone, GLOBAL_ZONENAME) != 0) 53923247Sgjelinek return (usage(B_FALSE)); 53933247Sgjelinek 53943247Sgjelinek if ((privset = priv_allocset()) == NULL) { 53953247Sgjelinek zerror(gettext("%s failed"), "priv_allocset"); 53963247Sgjelinek return (Z_ERR); 53973247Sgjelinek } 53983247Sgjelinek 53993247Sgjelinek if (getppriv(PRIV_EFFECTIVE, privset) != 0) { 54003247Sgjelinek zerror(gettext("%s failed"), "getppriv"); 54013247Sgjelinek priv_freeset(privset); 54023247Sgjelinek return (Z_ERR); 54033247Sgjelinek } 54043247Sgjelinek 54053247Sgjelinek if (priv_isfullset(privset) == B_FALSE) { 54063247Sgjelinek (void) usage(B_FALSE); 54073247Sgjelinek priv_freeset(privset); 54083247Sgjelinek return (Z_ERR); 54093247Sgjelinek } 54103247Sgjelinek priv_freeset(privset); 54113247Sgjelinek 54123247Sgjelinek if ((handle = zonecfg_init_handle()) == NULL) { 54133247Sgjelinek zperror(cmd_to_str(CMD_APPLY), B_TRUE); 54143247Sgjelinek return (Z_ERR); 54153247Sgjelinek } 54163247Sgjelinek 54173247Sgjelinek if ((err = zonecfg_get_handle(target_zone, handle)) != Z_OK) { 54183247Sgjelinek errno = err; 54193247Sgjelinek zperror(cmd_to_str(CMD_APPLY), B_TRUE); 54203247Sgjelinek zonecfg_fini_handle(handle); 54213247Sgjelinek return (Z_ERR); 54223247Sgjelinek } 54233247Sgjelinek 54243247Sgjelinek /* specific error msgs are printed within apply_rctls */ 54253247Sgjelinek if ((err = zonecfg_apply_rctls(target_zone, handle)) != Z_OK) { 54263247Sgjelinek errno = err; 54273247Sgjelinek zperror(cmd_to_str(CMD_APPLY), B_TRUE); 54283247Sgjelinek res = Z_ERR; 54293247Sgjelinek } 54303247Sgjelinek 54313247Sgjelinek if ((err = check_cpu_shares_sched(handle)) != Z_OK) 54323247Sgjelinek res = Z_ERR; 54333247Sgjelinek 54343352Sgjelinek if (pool_configured(handle)) { 54353352Sgjelinek if (mixed_pools(zoneid)) { 54363352Sgjelinek zerror(gettext("Zone is using multiple resource " 54373352Sgjelinek "pools. The pool\nconfiguration cannot be " 54383352Sgjelinek "applied without rebooting.")); 54393352Sgjelinek res = Z_ERR; 54403352Sgjelinek } else { 54413352Sgjelinek 54423352Sgjelinek /* 54433352Sgjelinek * The next two blocks of code attempt to set up 54443352Sgjelinek * temporary pools as well as persistent pools. In 54453352Sgjelinek * both cases we call the functions unconditionally. 54463352Sgjelinek * Within each funtion the code will check if the zone 54473352Sgjelinek * is actually configured for a temporary pool or 54483352Sgjelinek * persistent pool and just return if there is nothing 54493352Sgjelinek * to do. 54503352Sgjelinek */ 54513352Sgjelinek if ((err = zonecfg_bind_tmp_pool(handle, zoneid, 54523352Sgjelinek pool_err, sizeof (pool_err))) != Z_OK) { 54533352Sgjelinek if (err == Z_POOL || err == Z_POOL_CREATE || 54543352Sgjelinek err == Z_POOL_BIND) 54553352Sgjelinek zerror("%s: %s", zonecfg_strerror(err), 54563352Sgjelinek pool_err); 54573352Sgjelinek else 54583352Sgjelinek zerror(gettext("could not bind zone to " 54593352Sgjelinek "temporary pool: %s"), 54603352Sgjelinek zonecfg_strerror(err)); 54613352Sgjelinek res = Z_ERR; 54623352Sgjelinek } 54633352Sgjelinek 54643352Sgjelinek if ((err = zonecfg_bind_pool(handle, zoneid, pool_err, 54653352Sgjelinek sizeof (pool_err))) != Z_OK) { 54663352Sgjelinek if (err == Z_POOL || err == Z_POOL_BIND) 54673352Sgjelinek zerror("%s: %s", zonecfg_strerror(err), 54683352Sgjelinek pool_err); 54693352Sgjelinek else 54703352Sgjelinek zerror("%s", zonecfg_strerror(err)); 54713352Sgjelinek } 54723352Sgjelinek } 54733247Sgjelinek } 54743247Sgjelinek 54753247Sgjelinek /* 54763247Sgjelinek * If a memory cap is configured, set the cap in the kernel using 54773247Sgjelinek * zone_setattr() and make sure the rcapd SMF service is enabled. 54783247Sgjelinek */ 54793247Sgjelinek if (zonecfg_getmcapent(handle, &mcap) == Z_OK) { 54803247Sgjelinek uint64_t num; 54813247Sgjelinek char smf_err[128]; 54823247Sgjelinek 54833247Sgjelinek num = (uint64_t)strtoll(mcap.zone_physmem_cap, NULL, 10); 54843247Sgjelinek if (zone_setattr(zoneid, ZONE_ATTR_PHYS_MCAP, &num, 0) == -1) { 54853247Sgjelinek zerror(gettext("could not set zone memory cap")); 54863247Sgjelinek res = Z_ERR; 54873247Sgjelinek } 54883247Sgjelinek 54893247Sgjelinek if (zonecfg_enable_rcapd(smf_err, sizeof (smf_err)) != Z_OK) { 54903247Sgjelinek zerror(gettext("enabling system/rcap service failed: " 54913247Sgjelinek "%s"), smf_err); 54923247Sgjelinek res = Z_ERR; 54933247Sgjelinek } 54943247Sgjelinek } 54953247Sgjelinek 54963247Sgjelinek zonecfg_fini_handle(handle); 54973247Sgjelinek 54983247Sgjelinek return (res); 54993247Sgjelinek } 55003247Sgjelinek 55012303Scarlsonj static int 55020Sstevel@tonic-gate help_func(int argc, char *argv[]) 55030Sstevel@tonic-gate { 55040Sstevel@tonic-gate int arg, cmd_num; 55050Sstevel@tonic-gate 55060Sstevel@tonic-gate if (argc == 0) { 55070Sstevel@tonic-gate (void) usage(B_TRUE); 55080Sstevel@tonic-gate return (Z_OK); 55090Sstevel@tonic-gate } 55100Sstevel@tonic-gate optind = 0; 55110Sstevel@tonic-gate if ((arg = getopt(argc, argv, "?")) != EOF) { 55120Sstevel@tonic-gate switch (arg) { 55130Sstevel@tonic-gate case '?': 55140Sstevel@tonic-gate sub_usage(SHELP_HELP, CMD_HELP); 55150Sstevel@tonic-gate return (optopt == '?' ? Z_OK : Z_USAGE); 55160Sstevel@tonic-gate default: 55170Sstevel@tonic-gate sub_usage(SHELP_HELP, CMD_HELP); 55180Sstevel@tonic-gate return (Z_USAGE); 55190Sstevel@tonic-gate } 55200Sstevel@tonic-gate } 55210Sstevel@tonic-gate while (optind < argc) { 5522988Scarlsonj /* Private commands have NULL short_usage; omit them */ 5523988Scarlsonj if ((cmd_num = cmd_match(argv[optind])) < 0 || 5524988Scarlsonj cmdtab[cmd_num].short_usage == NULL) { 55250Sstevel@tonic-gate sub_usage(SHELP_HELP, CMD_HELP); 55260Sstevel@tonic-gate return (Z_USAGE); 55270Sstevel@tonic-gate } 55280Sstevel@tonic-gate sub_usage(cmdtab[cmd_num].short_usage, cmd_num); 55290Sstevel@tonic-gate optind++; 55300Sstevel@tonic-gate } 55310Sstevel@tonic-gate return (Z_OK); 55320Sstevel@tonic-gate } 55330Sstevel@tonic-gate 55340Sstevel@tonic-gate /* 55350Sstevel@tonic-gate * Returns: CMD_MIN thru CMD_MAX on success, -1 on error 55360Sstevel@tonic-gate */ 55370Sstevel@tonic-gate 55380Sstevel@tonic-gate static int 55390Sstevel@tonic-gate cmd_match(char *cmd) 55400Sstevel@tonic-gate { 55410Sstevel@tonic-gate int i; 55420Sstevel@tonic-gate 55430Sstevel@tonic-gate for (i = CMD_MIN; i <= CMD_MAX; i++) { 55440Sstevel@tonic-gate /* return only if there is an exact match */ 55450Sstevel@tonic-gate if (strcmp(cmd, cmdtab[i].cmd_name) == 0) 55460Sstevel@tonic-gate return (cmdtab[i].cmd_num); 55470Sstevel@tonic-gate } 55480Sstevel@tonic-gate return (-1); 55490Sstevel@tonic-gate } 55500Sstevel@tonic-gate 55510Sstevel@tonic-gate static int 55520Sstevel@tonic-gate parse_and_run(int argc, char *argv[]) 55530Sstevel@tonic-gate { 55540Sstevel@tonic-gate int i = cmd_match(argv[0]); 55550Sstevel@tonic-gate 55560Sstevel@tonic-gate if (i < 0) 55570Sstevel@tonic-gate return (usage(B_FALSE)); 55580Sstevel@tonic-gate return (cmdtab[i].handler(argc - 1, &(argv[1]))); 55590Sstevel@tonic-gate } 55600Sstevel@tonic-gate 55610Sstevel@tonic-gate static char * 55620Sstevel@tonic-gate get_execbasename(char *execfullname) 55630Sstevel@tonic-gate { 55640Sstevel@tonic-gate char *last_slash, *execbasename; 55650Sstevel@tonic-gate 55660Sstevel@tonic-gate /* guard against '/' at end of command invocation */ 55670Sstevel@tonic-gate for (;;) { 55680Sstevel@tonic-gate last_slash = strrchr(execfullname, '/'); 55690Sstevel@tonic-gate if (last_slash == NULL) { 55700Sstevel@tonic-gate execbasename = execfullname; 55710Sstevel@tonic-gate break; 55720Sstevel@tonic-gate } else { 55730Sstevel@tonic-gate execbasename = last_slash + 1; 55740Sstevel@tonic-gate if (*execbasename == '\0') { 55750Sstevel@tonic-gate *last_slash = '\0'; 55760Sstevel@tonic-gate continue; 55770Sstevel@tonic-gate } 55780Sstevel@tonic-gate break; 55790Sstevel@tonic-gate } 55800Sstevel@tonic-gate } 55810Sstevel@tonic-gate return (execbasename); 55820Sstevel@tonic-gate } 55830Sstevel@tonic-gate 55840Sstevel@tonic-gate int 55850Sstevel@tonic-gate main(int argc, char **argv) 55860Sstevel@tonic-gate { 55870Sstevel@tonic-gate int arg; 55880Sstevel@tonic-gate zoneid_t zid; 5589766Scarlsonj struct stat st; 55902712Snn35248 char *zone_lock_env; 55912712Snn35248 int err; 55920Sstevel@tonic-gate 55930Sstevel@tonic-gate if ((locale = setlocale(LC_ALL, "")) == NULL) 55940Sstevel@tonic-gate locale = "C"; 55950Sstevel@tonic-gate (void) textdomain(TEXT_DOMAIN); 55960Sstevel@tonic-gate setbuf(stdout, NULL); 55970Sstevel@tonic-gate (void) sigset(SIGHUP, SIG_IGN); 55980Sstevel@tonic-gate execname = get_execbasename(argv[0]); 55990Sstevel@tonic-gate target_zone = NULL; 56000Sstevel@tonic-gate if (chdir("/") != 0) { 56010Sstevel@tonic-gate zerror(gettext("could not change directory to /.")); 56020Sstevel@tonic-gate exit(Z_ERR); 56030Sstevel@tonic-gate } 56040Sstevel@tonic-gate 56052082Seschrock if (init_zfs() != Z_OK) 56062082Seschrock exit(Z_ERR); 56072082Seschrock 56082303Scarlsonj while ((arg = getopt(argc, argv, "?u:z:R:")) != EOF) { 56090Sstevel@tonic-gate switch (arg) { 56100Sstevel@tonic-gate case '?': 56110Sstevel@tonic-gate return (usage(B_TRUE)); 56122303Scarlsonj case 'u': 56132303Scarlsonj target_uuid = optarg; 56142303Scarlsonj break; 56150Sstevel@tonic-gate case 'z': 56160Sstevel@tonic-gate target_zone = optarg; 56170Sstevel@tonic-gate break; 5618766Scarlsonj case 'R': /* private option for admin/install use */ 5619766Scarlsonj if (*optarg != '/') { 5620766Scarlsonj zerror(gettext("root path must be absolute.")); 5621766Scarlsonj exit(Z_ERR); 5622766Scarlsonj } 5623766Scarlsonj if (stat(optarg, &st) == -1 || !S_ISDIR(st.st_mode)) { 5624766Scarlsonj zerror( 5625766Scarlsonj gettext("root path must be a directory.")); 5626766Scarlsonj exit(Z_ERR); 5627766Scarlsonj } 5628766Scarlsonj zonecfg_set_root(optarg); 5629766Scarlsonj break; 56300Sstevel@tonic-gate default: 56310Sstevel@tonic-gate return (usage(B_FALSE)); 56320Sstevel@tonic-gate } 56330Sstevel@tonic-gate } 56340Sstevel@tonic-gate 56350Sstevel@tonic-gate if (optind >= argc) 56360Sstevel@tonic-gate return (usage(B_FALSE)); 56372303Scarlsonj 56382303Scarlsonj if (target_uuid != NULL && *target_uuid != '\0') { 56392303Scarlsonj uuid_t uuid; 56402303Scarlsonj static char newtarget[ZONENAME_MAX]; 56412303Scarlsonj 56422303Scarlsonj if (uuid_parse(target_uuid, uuid) == -1) { 56432303Scarlsonj zerror(gettext("illegal UUID value specified")); 56442303Scarlsonj exit(Z_ERR); 56452303Scarlsonj } 56462303Scarlsonj if (zonecfg_get_name_by_uuid(uuid, newtarget, 56472303Scarlsonj sizeof (newtarget)) == Z_OK) 56482303Scarlsonj target_zone = newtarget; 56492303Scarlsonj } 56502303Scarlsonj 56510Sstevel@tonic-gate if (target_zone != NULL && zone_get_id(target_zone, &zid) != 0) { 56520Sstevel@tonic-gate errno = Z_NO_ZONE; 56530Sstevel@tonic-gate zperror(target_zone, B_TRUE); 56540Sstevel@tonic-gate exit(Z_ERR); 56550Sstevel@tonic-gate } 56562712Snn35248 56572712Snn35248 /* 56582712Snn35248 * See if we have inherited the right to manipulate this zone from 56592712Snn35248 * a zoneadm instance in our ancestry. If so, set zone_lock_cnt to 56602712Snn35248 * indicate it. If not, make that explicit in our environment. 56612712Snn35248 */ 56622712Snn35248 zone_lock_env = getenv(LOCK_ENV_VAR); 56632712Snn35248 if (zone_lock_env == NULL) { 56642712Snn35248 if (putenv(zoneadm_lock_not_held) != 0) { 56652712Snn35248 zperror(target_zone, B_TRUE); 56662712Snn35248 exit(Z_ERR); 56672712Snn35248 } 56682712Snn35248 } else { 56692712Snn35248 zoneadm_is_nested = B_TRUE; 56702712Snn35248 if (atoi(zone_lock_env) == 1) 56712712Snn35248 zone_lock_cnt = 1; 56722712Snn35248 } 56732712Snn35248 56742712Snn35248 /* 56752712Snn35248 * If we are going to be operating on a single zone, retrieve its 56762712Snn35248 * brand type and determine whether it is native or not. 56772712Snn35248 */ 56782712Snn35248 if ((target_zone != NULL) && 56792712Snn35248 (strcmp(target_zone, GLOBAL_ZONENAME) != NULL)) { 56802712Snn35248 if (zone_get_brand(target_zone, target_brand, 56812712Snn35248 sizeof (target_brand)) != Z_OK) { 56822712Snn35248 zerror(gettext("missing or invalid brand")); 56832712Snn35248 exit(Z_ERR); 56842712Snn35248 } 56852712Snn35248 is_native_zone = (strcmp(target_brand, NATIVE_BRAND_NAME) == 0); 56862712Snn35248 } 56872712Snn35248 56882712Snn35248 err = parse_and_run(argc - optind, &argv[optind]); 56892712Snn35248 56902712Snn35248 return (err); 56910Sstevel@tonic-gate } 5692