17c478bd9Sstevel@tonic-gate /* 27c478bd9Sstevel@tonic-gate * CDDL HEADER START 37c478bd9Sstevel@tonic-gate * 47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5ee519a1fSgjelinek * Common Development and Distribution License (the "License"). 6ee519a1fSgjelinek * You may not use this file except in compliance with the License. 77c478bd9Sstevel@tonic-gate * 87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 117c478bd9Sstevel@tonic-gate * and limitations under the License. 127c478bd9Sstevel@tonic-gate * 137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 187c478bd9Sstevel@tonic-gate * 197c478bd9Sstevel@tonic-gate * CDDL HEADER END 207c478bd9Sstevel@tonic-gate */ 217e362f58Scomay 227c478bd9Sstevel@tonic-gate /* 23865e09a4Sgjelinek * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 247c478bd9Sstevel@tonic-gate * Use is subject to license terms. 257c478bd9Sstevel@tonic-gate */ 267c478bd9Sstevel@tonic-gate 277c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 287c478bd9Sstevel@tonic-gate 297c478bd9Sstevel@tonic-gate /* 307c478bd9Sstevel@tonic-gate * zoneadm is a command interpreter for zone administration. It is all in 317c478bd9Sstevel@tonic-gate * C (i.e., no lex/yacc), and all the argument passing is argc/argv based. 327c478bd9Sstevel@tonic-gate * main() calls parse_and_run() which calls cmd_match(), then invokes the 337c478bd9Sstevel@tonic-gate * appropriate command's handler function. The rest of the program is the 347c478bd9Sstevel@tonic-gate * handler functions and their helper functions. 357c478bd9Sstevel@tonic-gate * 367c478bd9Sstevel@tonic-gate * Some of the helper functions are used largely to simplify I18N: reducing 377c478bd9Sstevel@tonic-gate * the need for translation notes. This is particularly true of many of 387c478bd9Sstevel@tonic-gate * the zerror() calls: doing e.g. zerror(gettext("%s failed"), "foo") rather 397c478bd9Sstevel@tonic-gate * than zerror(gettext("foo failed")) with a translation note indicating 407c478bd9Sstevel@tonic-gate * that "foo" need not be translated. 417c478bd9Sstevel@tonic-gate */ 427c478bd9Sstevel@tonic-gate 437c478bd9Sstevel@tonic-gate #include <stdio.h> 447c478bd9Sstevel@tonic-gate #include <errno.h> 457c478bd9Sstevel@tonic-gate #include <unistd.h> 467c478bd9Sstevel@tonic-gate #include <signal.h> 477c478bd9Sstevel@tonic-gate #include <stdarg.h> 487c478bd9Sstevel@tonic-gate #include <ctype.h> 497c478bd9Sstevel@tonic-gate #include <stdlib.h> 507c478bd9Sstevel@tonic-gate #include <string.h> 517c478bd9Sstevel@tonic-gate #include <wait.h> 527c478bd9Sstevel@tonic-gate #include <zone.h> 537c478bd9Sstevel@tonic-gate #include <priv.h> 547c478bd9Sstevel@tonic-gate #include <locale.h> 557c478bd9Sstevel@tonic-gate #include <libintl.h> 567c478bd9Sstevel@tonic-gate #include <libzonecfg.h> 577c478bd9Sstevel@tonic-gate #include <bsm/adt.h> 587c478bd9Sstevel@tonic-gate #include <sys/param.h> 597c478bd9Sstevel@tonic-gate #include <sys/types.h> 607c478bd9Sstevel@tonic-gate #include <sys/stat.h> 617c478bd9Sstevel@tonic-gate #include <sys/statvfs.h> 627c478bd9Sstevel@tonic-gate #include <assert.h> 637c478bd9Sstevel@tonic-gate #include <sys/sockio.h> 647c478bd9Sstevel@tonic-gate #include <sys/mntent.h> 657c478bd9Sstevel@tonic-gate #include <limits.h> 660b5de56dSgjelinek #include <dirent.h> 677c478bd9Sstevel@tonic-gate 687c478bd9Sstevel@tonic-gate #include <fcntl.h> 697c478bd9Sstevel@tonic-gate #include <door.h> 707c478bd9Sstevel@tonic-gate #include <macros.h> 717c478bd9Sstevel@tonic-gate #include <libgen.h> 72865e09a4Sgjelinek #include <fnmatch.h> 73e7f3c547Sgjelinek #include <sys/modctl.h> 747c478bd9Sstevel@tonic-gate 757c478bd9Sstevel@tonic-gate #include <pool.h> 767c478bd9Sstevel@tonic-gate #include <sys/pool.h> 777c478bd9Sstevel@tonic-gate 780b5de56dSgjelinek #include "zoneadm.h" 790b5de56dSgjelinek 807c478bd9Sstevel@tonic-gate #define MAXARGS 8 817c478bd9Sstevel@tonic-gate 827c478bd9Sstevel@tonic-gate /* Reflects kernel zone entries */ 837c478bd9Sstevel@tonic-gate typedef struct zone_entry { 847c478bd9Sstevel@tonic-gate zoneid_t zid; 857c478bd9Sstevel@tonic-gate char zname[ZONENAME_MAX]; 867c478bd9Sstevel@tonic-gate char *zstate_str; 877c478bd9Sstevel@tonic-gate zone_state_t zstate_num; 887c478bd9Sstevel@tonic-gate char zroot[MAXPATHLEN]; 897c478bd9Sstevel@tonic-gate } zone_entry_t; 907c478bd9Sstevel@tonic-gate 917c478bd9Sstevel@tonic-gate static zone_entry_t *zents; 927c478bd9Sstevel@tonic-gate static size_t nzents; 937c478bd9Sstevel@tonic-gate 941390a385Sgjelinek #define LOOPBACK_IF "lo0" 951390a385Sgjelinek #define SOCKET_AF(af) (((af) == AF_UNSPEC) ? AF_INET : (af)) 961390a385Sgjelinek 971390a385Sgjelinek struct net_if { 981390a385Sgjelinek char *name; 991390a385Sgjelinek int af; 1001390a385Sgjelinek }; 1011390a385Sgjelinek 1027c478bd9Sstevel@tonic-gate /* 0755 is the default directory mode. */ 1037c478bd9Sstevel@tonic-gate #define DEFAULT_DIR_MODE \ 1047c478bd9Sstevel@tonic-gate (S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) 1057c478bd9Sstevel@tonic-gate 1067c478bd9Sstevel@tonic-gate struct cmd { 1077c478bd9Sstevel@tonic-gate uint_t cmd_num; /* command number */ 1087c478bd9Sstevel@tonic-gate char *cmd_name; /* command name */ 1097c478bd9Sstevel@tonic-gate char *short_usage; /* short form help */ 1107c478bd9Sstevel@tonic-gate int (*handler)(int argc, char *argv[]); /* function to call */ 1117c478bd9Sstevel@tonic-gate 1127c478bd9Sstevel@tonic-gate }; 1137c478bd9Sstevel@tonic-gate 1147c478bd9Sstevel@tonic-gate #define SHELP_HELP "help" 115*3f2f09c1Sdp #define SHELP_BOOT "boot [-- boot_arguments]" 1167c478bd9Sstevel@tonic-gate #define SHELP_HALT "halt" 1177c478bd9Sstevel@tonic-gate #define SHELP_READY "ready" 118*3f2f09c1Sdp #define SHELP_REBOOT "reboot [-- boot_arguments]" 1197c478bd9Sstevel@tonic-gate #define SHELP_LIST "list [-cipv]" 1207c478bd9Sstevel@tonic-gate #define SHELP_VERIFY "verify" 1210b5de56dSgjelinek #define SHELP_INSTALL "install [-x nodataset]" 1227c478bd9Sstevel@tonic-gate #define SHELP_UNINSTALL "uninstall [-F]" 1230b5de56dSgjelinek #define SHELP_CLONE "clone [-m method] [-s <ZFS snapshot>] zonename" 124865e09a4Sgjelinek #define SHELP_MOVE "move zonepath" 1258cd327d5Sgjelinek #define SHELP_DETACH "detach [-n]" 1268cd327d5Sgjelinek #define SHELP_ATTACH "attach [-F] [-n <path>]" 1277c478bd9Sstevel@tonic-gate 1287c478bd9Sstevel@tonic-gate static int help_func(int argc, char *argv[]); 1297c478bd9Sstevel@tonic-gate static int ready_func(int argc, char *argv[]); 1307c478bd9Sstevel@tonic-gate static int boot_func(int argc, char *argv[]); 1317c478bd9Sstevel@tonic-gate static int halt_func(int argc, char *argv[]); 1327c478bd9Sstevel@tonic-gate static int reboot_func(int argc, char *argv[]); 1337c478bd9Sstevel@tonic-gate static int list_func(int argc, char *argv[]); 1347c478bd9Sstevel@tonic-gate static int verify_func(int argc, char *argv[]); 1357c478bd9Sstevel@tonic-gate static int install_func(int argc, char *argv[]); 1367c478bd9Sstevel@tonic-gate static int uninstall_func(int argc, char *argv[]); 137108322fbScarlsonj static int mount_func(int argc, char *argv[]); 138108322fbScarlsonj static int unmount_func(int argc, char *argv[]); 139865e09a4Sgjelinek static int clone_func(int argc, char *argv[]); 140865e09a4Sgjelinek static int move_func(int argc, char *argv[]); 141ee519a1fSgjelinek static int detach_func(int argc, char *argv[]); 142ee519a1fSgjelinek static int attach_func(int argc, char *argv[]); 1437c478bd9Sstevel@tonic-gate static int sanity_check(char *zone, int cmd_num, boolean_t running, 1447c478bd9Sstevel@tonic-gate boolean_t unsafe_when_running); 1457c478bd9Sstevel@tonic-gate static int cmd_match(char *cmd); 1467c478bd9Sstevel@tonic-gate static int verify_details(int); 1477c478bd9Sstevel@tonic-gate 1487c478bd9Sstevel@tonic-gate static struct cmd cmdtab[] = { 1497c478bd9Sstevel@tonic-gate { CMD_HELP, "help", SHELP_HELP, help_func }, 1507c478bd9Sstevel@tonic-gate { CMD_BOOT, "boot", SHELP_BOOT, boot_func }, 1517c478bd9Sstevel@tonic-gate { CMD_HALT, "halt", SHELP_HALT, halt_func }, 1527c478bd9Sstevel@tonic-gate { CMD_READY, "ready", SHELP_READY, ready_func }, 1537c478bd9Sstevel@tonic-gate { CMD_REBOOT, "reboot", SHELP_REBOOT, reboot_func }, 1547c478bd9Sstevel@tonic-gate { CMD_LIST, "list", SHELP_LIST, list_func }, 1557c478bd9Sstevel@tonic-gate { CMD_VERIFY, "verify", SHELP_VERIFY, verify_func }, 1567c478bd9Sstevel@tonic-gate { CMD_INSTALL, "install", SHELP_INSTALL, install_func }, 1577c478bd9Sstevel@tonic-gate { CMD_UNINSTALL, "uninstall", SHELP_UNINSTALL, 158108322fbScarlsonj uninstall_func }, 159865e09a4Sgjelinek /* mount and unmount are private commands for admin/install */ 160108322fbScarlsonj { CMD_MOUNT, "mount", NULL, mount_func }, 161865e09a4Sgjelinek { CMD_UNMOUNT, "unmount", NULL, unmount_func }, 162865e09a4Sgjelinek { CMD_CLONE, "clone", SHELP_CLONE, clone_func }, 163ee519a1fSgjelinek { CMD_MOVE, "move", SHELP_MOVE, move_func }, 164ee519a1fSgjelinek { CMD_DETACH, "detach", SHELP_DETACH, detach_func }, 165ee519a1fSgjelinek { CMD_ATTACH, "attach", SHELP_ATTACH, attach_func } 1667c478bd9Sstevel@tonic-gate }; 1677c478bd9Sstevel@tonic-gate 1687c478bd9Sstevel@tonic-gate /* global variables */ 1697c478bd9Sstevel@tonic-gate 1707c478bd9Sstevel@tonic-gate /* set early in main(), never modified thereafter, used all over the place */ 1717c478bd9Sstevel@tonic-gate static char *execname; 1727c478bd9Sstevel@tonic-gate static char *locale; 1730b5de56dSgjelinek char *target_zone; 1747c478bd9Sstevel@tonic-gate 1757c478bd9Sstevel@tonic-gate /* used in do_subproc() and signal handler */ 1767c478bd9Sstevel@tonic-gate static volatile boolean_t child_killed; 1777c478bd9Sstevel@tonic-gate 1780b5de56dSgjelinek char * 1797c478bd9Sstevel@tonic-gate cmd_to_str(int cmd_num) 1807c478bd9Sstevel@tonic-gate { 1817c478bd9Sstevel@tonic-gate assert(cmd_num >= CMD_MIN && cmd_num <= CMD_MAX); 1827c478bd9Sstevel@tonic-gate return (cmdtab[cmd_num].cmd_name); 1837c478bd9Sstevel@tonic-gate } 1847c478bd9Sstevel@tonic-gate 1857c478bd9Sstevel@tonic-gate /* This is a separate function because of gettext() wrapping. */ 1867c478bd9Sstevel@tonic-gate static char * 1877c478bd9Sstevel@tonic-gate long_help(int cmd_num) 1887c478bd9Sstevel@tonic-gate { 1897e362f58Scomay assert(cmd_num >= CMD_MIN && cmd_num <= CMD_MAX); 1907c478bd9Sstevel@tonic-gate switch (cmd_num) { 1917c478bd9Sstevel@tonic-gate case CMD_HELP: 1927c478bd9Sstevel@tonic-gate return (gettext("Print usage message.")); 1937c478bd9Sstevel@tonic-gate case CMD_BOOT: 194*3f2f09c1Sdp return (gettext("Activates (boots) specified zone. See " 195*3f2f09c1Sdp "zoneadm(1m) for valid boot\n\targuments.")); 1967c478bd9Sstevel@tonic-gate case CMD_HALT: 1979e518655Sgjelinek return (gettext("Halts specified zone, bypassing shutdown " 1989e518655Sgjelinek "scripts and removing runtime\n\tresources of the zone.")); 1997c478bd9Sstevel@tonic-gate case CMD_READY: 2009e518655Sgjelinek return (gettext("Prepares a zone for running applications but " 2019e518655Sgjelinek "does not start any user\n\tprocesses in the zone.")); 2027c478bd9Sstevel@tonic-gate case CMD_REBOOT: 2039e518655Sgjelinek return (gettext("Restarts the zone (equivalent to a halt / " 204*3f2f09c1Sdp "boot sequence).\n\tFails if the zone is not active. " 205*3f2f09c1Sdp "See zoneadm(1m) for valid boot\n\targuments.")); 2067c478bd9Sstevel@tonic-gate case CMD_LIST: 2077c478bd9Sstevel@tonic-gate return (gettext("Lists the current zones, or a " 2087c478bd9Sstevel@tonic-gate "specific zone if indicated. By default,\n\tall " 2097c478bd9Sstevel@tonic-gate "running zones are listed, though this can be " 2107c478bd9Sstevel@tonic-gate "expanded to all\n\tinstalled zones with the -i " 2117c478bd9Sstevel@tonic-gate "option or all configured zones with the\n\t-c " 2127c478bd9Sstevel@tonic-gate "option. When used with the general -z <zone> " 2137c478bd9Sstevel@tonic-gate "option, lists only the\n\tspecified zone, but " 2147c478bd9Sstevel@tonic-gate "lists it regardless of its state, and the -i " 2157c478bd9Sstevel@tonic-gate "and -c\n\toptions are disallowed. The -v option " 2167c478bd9Sstevel@tonic-gate "can be used to display verbose\n\tinformation: " 2177c478bd9Sstevel@tonic-gate "zone name, id, current state, root directory and " 2187c478bd9Sstevel@tonic-gate "options.\n\tThe -p option can be used to request " 2197c478bd9Sstevel@tonic-gate "machine-parsable output. The -v\n\tand -p " 2207c478bd9Sstevel@tonic-gate "options are mutually exclusive. If neither -v " 2219e518655Sgjelinek "nor -p is used,\n\tjust the zone name is listed.")); 2227c478bd9Sstevel@tonic-gate case CMD_VERIFY: 2237c478bd9Sstevel@tonic-gate return (gettext("Check to make sure the configuration " 2247c478bd9Sstevel@tonic-gate "can safely be instantiated\n\ton the machine: " 2257c478bd9Sstevel@tonic-gate "physical network interfaces exist, etc.")); 2267c478bd9Sstevel@tonic-gate case CMD_INSTALL: 2270b5de56dSgjelinek return (gettext("Install the configuration on to the system. " 2280b5de56dSgjelinek "The -x nodataset option\n\tcan be used to prevent the " 2290b5de56dSgjelinek "creation of a new ZFS file system for the\n\tzone " 2300b5de56dSgjelinek "(assuming the zonepath is within a ZFS file system).")); 2317c478bd9Sstevel@tonic-gate case CMD_UNINSTALL: 2329e518655Sgjelinek return (gettext("Uninstall the configuration from the system. " 2339e518655Sgjelinek "The -F flag can be used\n\tto force the action.")); 234865e09a4Sgjelinek case CMD_CLONE: 2350b5de56dSgjelinek return (gettext("Clone the installation of another zone. " 2360b5de56dSgjelinek "The -m option can be used to\n\tspecify 'copy' which " 2370b5de56dSgjelinek "forces a copy of the source zone. The -s option\n\t" 2380b5de56dSgjelinek "can be used to specify the name of a ZFS snapshot " 2390b5de56dSgjelinek "that was taken from\n\ta previous clone command. The " 2400b5de56dSgjelinek "snapshot will be used as the source\n\tinstead of " 2410b5de56dSgjelinek "creating a new ZFS snapshot.")); 242865e09a4Sgjelinek case CMD_MOVE: 243865e09a4Sgjelinek return (gettext("Move the zone to a new zonepath.")); 2449e518655Sgjelinek case CMD_DETACH: 2459e518655Sgjelinek return (gettext("Detach the zone from the system. The zone " 2469e518655Sgjelinek "state is changed to\n\t'configured' (but the files under " 2479e518655Sgjelinek "the zonepath are untouched).\n\tThe zone can subsequently " 2489e518655Sgjelinek "be attached, or can be moved to another\n\tsystem and " 2498cd327d5Sgjelinek "attached there. The -n option can be used to specify\n\t" 2508cd327d5Sgjelinek "'no-execute' mode. When -n is used, the information " 2518cd327d5Sgjelinek "needed to attach\n\tthe zone is sent to standard output " 2528cd327d5Sgjelinek "but the zone is not actually\n\tdetached.")); 2539e518655Sgjelinek case CMD_ATTACH: 2549e518655Sgjelinek return (gettext("Attach the zone to the system. The zone " 2559e518655Sgjelinek "state must be 'configured'\n\tprior to attach; upon " 2569e518655Sgjelinek "successful completion, the zone state will be\n\t" 2579e518655Sgjelinek "'installed'. The system software on the current " 2589e518655Sgjelinek "system must be\n\tcompatible with the software on the " 2599e518655Sgjelinek "zone's original system.\n\tSpecify -F to force the attach " 2608cd327d5Sgjelinek "and skip software compatibility tests.\n\tThe -n option " 2618cd327d5Sgjelinek "can be used to specify 'no-execute' mode. When -n is\n\t" 2628cd327d5Sgjelinek "used, the information needed to attach the zone is read " 2638cd327d5Sgjelinek "from the\n\tspecified path and the configuration is only " 2648cd327d5Sgjelinek "validated. The path can\n\tbe '-' to specify standard " 2658cd327d5Sgjelinek "input.")); 266108322fbScarlsonj default: 267108322fbScarlsonj return (""); 2687c478bd9Sstevel@tonic-gate } 2697c478bd9Sstevel@tonic-gate /* NOTREACHED */ 2707e362f58Scomay return (NULL); 2717c478bd9Sstevel@tonic-gate } 2727c478bd9Sstevel@tonic-gate 2737c478bd9Sstevel@tonic-gate /* 2747c478bd9Sstevel@tonic-gate * Called with explicit B_TRUE when help is explicitly requested, B_FALSE for 2757c478bd9Sstevel@tonic-gate * unexpected errors. 2767c478bd9Sstevel@tonic-gate */ 2777c478bd9Sstevel@tonic-gate 2787c478bd9Sstevel@tonic-gate static int 2797c478bd9Sstevel@tonic-gate usage(boolean_t explicit) 2807c478bd9Sstevel@tonic-gate { 2817c478bd9Sstevel@tonic-gate int i; 2827c478bd9Sstevel@tonic-gate FILE *fd = explicit ? stdout : stderr; 2837c478bd9Sstevel@tonic-gate 2847c478bd9Sstevel@tonic-gate (void) fprintf(fd, "%s:\t%s help\n", gettext("usage"), execname); 2857c478bd9Sstevel@tonic-gate (void) fprintf(fd, "\t%s [-z <zone>] list\n", execname); 2867c478bd9Sstevel@tonic-gate (void) fprintf(fd, "\t%s -z <zone> <%s>\n", execname, 2877c478bd9Sstevel@tonic-gate gettext("subcommand")); 2887c478bd9Sstevel@tonic-gate (void) fprintf(fd, "\n%s:\n\n", gettext("Subcommands")); 2897c478bd9Sstevel@tonic-gate for (i = CMD_MIN; i <= CMD_MAX; i++) { 290108322fbScarlsonj if (cmdtab[i].short_usage == NULL) 291108322fbScarlsonj continue; 2927c478bd9Sstevel@tonic-gate (void) fprintf(fd, "%s\n", cmdtab[i].short_usage); 2937c478bd9Sstevel@tonic-gate if (explicit) 2947c478bd9Sstevel@tonic-gate (void) fprintf(fd, "\t%s\n\n", long_help(i)); 2957c478bd9Sstevel@tonic-gate } 2967c478bd9Sstevel@tonic-gate if (!explicit) 2977c478bd9Sstevel@tonic-gate (void) fputs("\n", fd); 2987c478bd9Sstevel@tonic-gate return (Z_USAGE); 2997c478bd9Sstevel@tonic-gate } 3007c478bd9Sstevel@tonic-gate 3017c478bd9Sstevel@tonic-gate static void 3027c478bd9Sstevel@tonic-gate sub_usage(char *short_usage, int cmd_num) 3037c478bd9Sstevel@tonic-gate { 3047c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "%s:\t%s\n", gettext("usage"), short_usage); 3057c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "\t%s\n", long_help(cmd_num)); 3067c478bd9Sstevel@tonic-gate } 3077c478bd9Sstevel@tonic-gate 3087c478bd9Sstevel@tonic-gate /* 3097c478bd9Sstevel@tonic-gate * zperror() is like perror(3c) except that this also prints the executable 3107c478bd9Sstevel@tonic-gate * name at the start of the message, and takes a boolean indicating whether 3117c478bd9Sstevel@tonic-gate * to call libc'c strerror() or that from libzonecfg. 3127c478bd9Sstevel@tonic-gate */ 3137c478bd9Sstevel@tonic-gate 3140b5de56dSgjelinek void 3157c478bd9Sstevel@tonic-gate zperror(const char *str, boolean_t zonecfg_error) 3167c478bd9Sstevel@tonic-gate { 3177c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "%s: %s: %s\n", execname, str, 3187c478bd9Sstevel@tonic-gate zonecfg_error ? zonecfg_strerror(errno) : strerror(errno)); 3197c478bd9Sstevel@tonic-gate } 3207c478bd9Sstevel@tonic-gate 3217c478bd9Sstevel@tonic-gate /* 3227c478bd9Sstevel@tonic-gate * zperror2() is very similar to zperror() above, except it also prints a 3237c478bd9Sstevel@tonic-gate * supplied zone name after the executable. 3247c478bd9Sstevel@tonic-gate * 3257c478bd9Sstevel@tonic-gate * All current consumers of this function want libzonecfg's strerror() rather 3267c478bd9Sstevel@tonic-gate * than libc's; if this ever changes, this function can be made more generic 3277c478bd9Sstevel@tonic-gate * like zperror() above. 3287c478bd9Sstevel@tonic-gate */ 3297c478bd9Sstevel@tonic-gate 3300b5de56dSgjelinek void 3317c478bd9Sstevel@tonic-gate zperror2(const char *zone, const char *str) 3327c478bd9Sstevel@tonic-gate { 3337c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "%s: %s: %s: %s\n", execname, zone, str, 3347c478bd9Sstevel@tonic-gate zonecfg_strerror(errno)); 3357c478bd9Sstevel@tonic-gate } 3367c478bd9Sstevel@tonic-gate 3377c478bd9Sstevel@tonic-gate /* PRINTFLIKE1 */ 3380b5de56dSgjelinek void 3397c478bd9Sstevel@tonic-gate zerror(const char *fmt, ...) 3407c478bd9Sstevel@tonic-gate { 3417c478bd9Sstevel@tonic-gate va_list alist; 3427c478bd9Sstevel@tonic-gate 3437c478bd9Sstevel@tonic-gate va_start(alist, fmt); 3447c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "%s: ", execname); 3457c478bd9Sstevel@tonic-gate if (target_zone != NULL) 3467c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "zone '%s': ", target_zone); 3477c478bd9Sstevel@tonic-gate (void) vfprintf(stderr, fmt, alist); 3487c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "\n"); 3497c478bd9Sstevel@tonic-gate va_end(alist); 3507c478bd9Sstevel@tonic-gate } 3517c478bd9Sstevel@tonic-gate 3527c478bd9Sstevel@tonic-gate static void * 3537c478bd9Sstevel@tonic-gate safe_calloc(size_t nelem, size_t elsize) 3547c478bd9Sstevel@tonic-gate { 3557c478bd9Sstevel@tonic-gate void *r = calloc(nelem, elsize); 3567c478bd9Sstevel@tonic-gate 3577c478bd9Sstevel@tonic-gate if (r == NULL) { 3587c478bd9Sstevel@tonic-gate zerror(gettext("failed to allocate %lu bytes: %s"), 3597c478bd9Sstevel@tonic-gate (ulong_t)nelem * elsize, strerror(errno)); 3607c478bd9Sstevel@tonic-gate exit(Z_ERR); 3617c478bd9Sstevel@tonic-gate } 3627c478bd9Sstevel@tonic-gate return (r); 3637c478bd9Sstevel@tonic-gate } 3647c478bd9Sstevel@tonic-gate 3657c478bd9Sstevel@tonic-gate static void 3667c478bd9Sstevel@tonic-gate zone_print(zone_entry_t *zent, boolean_t verbose, boolean_t parsable) 3677c478bd9Sstevel@tonic-gate { 3687c478bd9Sstevel@tonic-gate static boolean_t firsttime = B_TRUE; 3697c478bd9Sstevel@tonic-gate 3707c478bd9Sstevel@tonic-gate assert(!(verbose && parsable)); 3717c478bd9Sstevel@tonic-gate if (firsttime && verbose) { 3727c478bd9Sstevel@tonic-gate firsttime = B_FALSE; 3737c478bd9Sstevel@tonic-gate (void) printf("%*s %-16s %-14s %-30s\n", ZONEID_WIDTH, "ID", 3747c478bd9Sstevel@tonic-gate "NAME", "STATUS", "PATH"); 3757c478bd9Sstevel@tonic-gate } 3767c478bd9Sstevel@tonic-gate if (!verbose) { 3777c478bd9Sstevel@tonic-gate if (!parsable) { 3787c478bd9Sstevel@tonic-gate (void) printf("%s\n", zent->zname); 3797c478bd9Sstevel@tonic-gate return; 3807c478bd9Sstevel@tonic-gate } 3817c478bd9Sstevel@tonic-gate if (zent->zid == ZONE_ID_UNDEFINED) 3827c478bd9Sstevel@tonic-gate (void) printf("-"); 3837c478bd9Sstevel@tonic-gate else 3847c478bd9Sstevel@tonic-gate (void) printf("%lu", zent->zid); 3857c478bd9Sstevel@tonic-gate (void) printf(":%s:%s:%s\n", zent->zname, zent->zstate_str, 3867c478bd9Sstevel@tonic-gate zent->zroot); 3877c478bd9Sstevel@tonic-gate return; 3887c478bd9Sstevel@tonic-gate } 3897c478bd9Sstevel@tonic-gate if (zent->zstate_str != NULL) { 3907c478bd9Sstevel@tonic-gate if (zent->zid == ZONE_ID_UNDEFINED) 3917c478bd9Sstevel@tonic-gate (void) printf("%*s", ZONEID_WIDTH, "-"); 3927c478bd9Sstevel@tonic-gate else 3937c478bd9Sstevel@tonic-gate (void) printf("%*lu", ZONEID_WIDTH, zent->zid); 3947c478bd9Sstevel@tonic-gate (void) printf(" %-16s %-14s %-30s\n", zent->zname, 3957c478bd9Sstevel@tonic-gate zent->zstate_str, zent->zroot); 3967c478bd9Sstevel@tonic-gate } 3977c478bd9Sstevel@tonic-gate } 3987c478bd9Sstevel@tonic-gate 3997c478bd9Sstevel@tonic-gate static int 400108322fbScarlsonj lookup_zone_info(const char *zone_name, zoneid_t zid, zone_entry_t *zent) 4017c478bd9Sstevel@tonic-gate { 40245916cd2Sjpk char root[MAXPATHLEN], *cp; 4037c478bd9Sstevel@tonic-gate int err; 4047c478bd9Sstevel@tonic-gate 4057c478bd9Sstevel@tonic-gate (void) strlcpy(zent->zname, zone_name, sizeof (zent->zname)); 4067c478bd9Sstevel@tonic-gate (void) strlcpy(zent->zroot, "???", sizeof (zent->zroot)); 4077c478bd9Sstevel@tonic-gate zent->zstate_str = "???"; 4087c478bd9Sstevel@tonic-gate 409108322fbScarlsonj zent->zid = zid; 4107c478bd9Sstevel@tonic-gate 41145916cd2Sjpk /* 41245916cd2Sjpk * For labeled zones which query the zone path of lower-level 41345916cd2Sjpk * zones, the path needs to be adjusted to drop the final 41445916cd2Sjpk * "/root" component. This adjusted path is then useful 41545916cd2Sjpk * for reading down any exported directories from the 41645916cd2Sjpk * lower-level zone. 41745916cd2Sjpk */ 41845916cd2Sjpk if (is_system_labeled() && zent->zid != ZONE_ID_UNDEFINED) { 41945916cd2Sjpk if (zone_getattr(zent->zid, ZONE_ATTR_ROOT, zent->zroot, 42045916cd2Sjpk sizeof (zent->zroot)) == -1) { 42145916cd2Sjpk zperror2(zent->zname, 42245916cd2Sjpk gettext("could not get zone path.")); 42345916cd2Sjpk return (Z_ERR); 42445916cd2Sjpk } 42545916cd2Sjpk cp = zent->zroot + strlen(zent->zroot) - 5; 42645916cd2Sjpk if (cp > zent->zroot && strcmp(cp, "/root") == 0) 42745916cd2Sjpk *cp = 0; 42845916cd2Sjpk } else { 42945916cd2Sjpk if ((err = zone_get_zonepath(zent->zname, root, 43045916cd2Sjpk sizeof (root))) != Z_OK) { 4317c478bd9Sstevel@tonic-gate errno = err; 43245916cd2Sjpk zperror2(zent->zname, 43345916cd2Sjpk gettext("could not get zone path.")); 4347c478bd9Sstevel@tonic-gate return (Z_ERR); 4357c478bd9Sstevel@tonic-gate } 4367c478bd9Sstevel@tonic-gate (void) strlcpy(zent->zroot, root, sizeof (zent->zroot)); 43745916cd2Sjpk } 4387c478bd9Sstevel@tonic-gate 4397c478bd9Sstevel@tonic-gate if ((err = zone_get_state(zent->zname, &zent->zstate_num)) != Z_OK) { 4407c478bd9Sstevel@tonic-gate errno = err; 4417c478bd9Sstevel@tonic-gate zperror2(zent->zname, gettext("could not get state")); 4427c478bd9Sstevel@tonic-gate return (Z_ERR); 4437c478bd9Sstevel@tonic-gate } 4447c478bd9Sstevel@tonic-gate zent->zstate_str = zone_state_str(zent->zstate_num); 4457c478bd9Sstevel@tonic-gate 4467c478bd9Sstevel@tonic-gate return (Z_OK); 4477c478bd9Sstevel@tonic-gate } 4487c478bd9Sstevel@tonic-gate 4497c478bd9Sstevel@tonic-gate /* 4507c478bd9Sstevel@tonic-gate * fetch_zents() calls zone_list(2) to find out how many zones are running 4517c478bd9Sstevel@tonic-gate * (which is stored in the global nzents), then calls zone_list(2) again 4527c478bd9Sstevel@tonic-gate * to fetch the list of running zones (stored in the global zents). This 4537c478bd9Sstevel@tonic-gate * function may be called multiple times, so if zents is already set, we 4547c478bd9Sstevel@tonic-gate * return immediately to save work. 4557c478bd9Sstevel@tonic-gate */ 4567c478bd9Sstevel@tonic-gate 4577c478bd9Sstevel@tonic-gate static int 458108322fbScarlsonj fetch_zents(void) 4597c478bd9Sstevel@tonic-gate { 4607c478bd9Sstevel@tonic-gate zoneid_t *zids = NULL; 4617c478bd9Sstevel@tonic-gate uint_t nzents_saved; 462108322fbScarlsonj int i, retv; 463108322fbScarlsonj FILE *fp; 464108322fbScarlsonj boolean_t inaltroot; 465108322fbScarlsonj zone_entry_t *zentp; 4667c478bd9Sstevel@tonic-gate 4677c478bd9Sstevel@tonic-gate if (nzents > 0) 4687c478bd9Sstevel@tonic-gate return (Z_OK); 4697c478bd9Sstevel@tonic-gate 4707c478bd9Sstevel@tonic-gate if (zone_list(NULL, &nzents) != 0) { 4717c478bd9Sstevel@tonic-gate zperror(gettext("failed to get zoneid list"), B_FALSE); 4727c478bd9Sstevel@tonic-gate return (Z_ERR); 4737c478bd9Sstevel@tonic-gate } 4747c478bd9Sstevel@tonic-gate 4757c478bd9Sstevel@tonic-gate again: 4767c478bd9Sstevel@tonic-gate if (nzents == 0) 4777c478bd9Sstevel@tonic-gate return (Z_OK); 4787c478bd9Sstevel@tonic-gate 4797c478bd9Sstevel@tonic-gate zids = safe_calloc(nzents, sizeof (zoneid_t)); 4807c478bd9Sstevel@tonic-gate nzents_saved = nzents; 4817c478bd9Sstevel@tonic-gate 4827c478bd9Sstevel@tonic-gate if (zone_list(zids, &nzents) != 0) { 4837c478bd9Sstevel@tonic-gate zperror(gettext("failed to get zone list"), B_FALSE); 4847c478bd9Sstevel@tonic-gate free(zids); 4857c478bd9Sstevel@tonic-gate return (Z_ERR); 4867c478bd9Sstevel@tonic-gate } 4877c478bd9Sstevel@tonic-gate if (nzents != nzents_saved) { 4887c478bd9Sstevel@tonic-gate /* list changed, try again */ 4897c478bd9Sstevel@tonic-gate free(zids); 4907c478bd9Sstevel@tonic-gate goto again; 4917c478bd9Sstevel@tonic-gate } 4927c478bd9Sstevel@tonic-gate 4937c478bd9Sstevel@tonic-gate zents = safe_calloc(nzents, sizeof (zone_entry_t)); 4947c478bd9Sstevel@tonic-gate 495108322fbScarlsonj inaltroot = zonecfg_in_alt_root(); 496108322fbScarlsonj if (inaltroot) 497108322fbScarlsonj fp = zonecfg_open_scratch("", B_FALSE); 498108322fbScarlsonj else 499108322fbScarlsonj fp = NULL; 500108322fbScarlsonj zentp = zents; 501108322fbScarlsonj retv = Z_OK; 5027c478bd9Sstevel@tonic-gate for (i = 0; i < nzents; i++) { 5037c478bd9Sstevel@tonic-gate char name[ZONENAME_MAX]; 504108322fbScarlsonj char altname[ZONENAME_MAX]; 5057c478bd9Sstevel@tonic-gate 506108322fbScarlsonj if (getzonenamebyid(zids[i], name, sizeof (name)) < 0) { 5077c478bd9Sstevel@tonic-gate zperror(gettext("failed to get zone name"), B_FALSE); 508108322fbScarlsonj retv = Z_ERR; 509108322fbScarlsonj continue; 5107c478bd9Sstevel@tonic-gate } 511108322fbScarlsonj if (zonecfg_is_scratch(name)) { 512108322fbScarlsonj /* Ignore scratch zones by default */ 513108322fbScarlsonj if (!inaltroot) 514108322fbScarlsonj continue; 515108322fbScarlsonj if (fp == NULL || 516108322fbScarlsonj zonecfg_reverse_scratch(fp, name, altname, 517108322fbScarlsonj sizeof (altname), NULL, 0) == -1) { 5185ee84fbdSgjelinek zerror(gettext("could not resolve scratch " 519108322fbScarlsonj "zone %s"), name); 520108322fbScarlsonj retv = Z_ERR; 521108322fbScarlsonj continue; 522108322fbScarlsonj } 523108322fbScarlsonj (void) strcpy(name, altname); 524108322fbScarlsonj } else { 525108322fbScarlsonj /* Ignore non-scratch when in an alternate root */ 526108322fbScarlsonj if (inaltroot && strcmp(name, GLOBAL_ZONENAME) != 0) 527108322fbScarlsonj continue; 528108322fbScarlsonj } 529108322fbScarlsonj if (lookup_zone_info(name, zids[i], zentp) != Z_OK) { 530108322fbScarlsonj zerror(gettext("failed to get zone data")); 531108322fbScarlsonj retv = Z_ERR; 532108322fbScarlsonj continue; 533108322fbScarlsonj } 534108322fbScarlsonj zentp++; 535108322fbScarlsonj } 536108322fbScarlsonj nzents = zentp - zents; 537108322fbScarlsonj if (fp != NULL) 538108322fbScarlsonj zonecfg_close_scratch(fp); 5397c478bd9Sstevel@tonic-gate 5407c478bd9Sstevel@tonic-gate free(zids); 541108322fbScarlsonj return (retv); 5427c478bd9Sstevel@tonic-gate } 5437c478bd9Sstevel@tonic-gate 544108322fbScarlsonj static int 5457c478bd9Sstevel@tonic-gate zone_print_list(zone_state_t min_state, boolean_t verbose, boolean_t parsable) 5467c478bd9Sstevel@tonic-gate { 5477c478bd9Sstevel@tonic-gate int i; 5487c478bd9Sstevel@tonic-gate zone_entry_t zent; 5497c478bd9Sstevel@tonic-gate FILE *cookie; 5507c478bd9Sstevel@tonic-gate char *name; 5517c478bd9Sstevel@tonic-gate 5527c478bd9Sstevel@tonic-gate /* 5537c478bd9Sstevel@tonic-gate * First get the list of running zones from the kernel and print them. 5547c478bd9Sstevel@tonic-gate * If that is all we need, then return. 5557c478bd9Sstevel@tonic-gate */ 556108322fbScarlsonj if ((i = fetch_zents()) != Z_OK) { 5577c478bd9Sstevel@tonic-gate /* 5587c478bd9Sstevel@tonic-gate * No need for error messages; fetch_zents() has already taken 5597c478bd9Sstevel@tonic-gate * care of this. 5607c478bd9Sstevel@tonic-gate */ 561108322fbScarlsonj return (i); 5627c478bd9Sstevel@tonic-gate } 563108322fbScarlsonj for (i = 0; i < nzents; i++) 5647c478bd9Sstevel@tonic-gate zone_print(&zents[i], verbose, parsable); 5657c478bd9Sstevel@tonic-gate if (min_state >= ZONE_STATE_RUNNING) 566108322fbScarlsonj return (Z_OK); 5677c478bd9Sstevel@tonic-gate /* 5687c478bd9Sstevel@tonic-gate * Next, get the full list of zones from the configuration, skipping 5697c478bd9Sstevel@tonic-gate * any we have already printed. 5707c478bd9Sstevel@tonic-gate */ 5717c478bd9Sstevel@tonic-gate cookie = setzoneent(); 5727c478bd9Sstevel@tonic-gate while ((name = getzoneent(cookie)) != NULL) { 5737c478bd9Sstevel@tonic-gate for (i = 0; i < nzents; i++) { 5747c478bd9Sstevel@tonic-gate if (strcmp(zents[i].zname, name) == 0) 5757c478bd9Sstevel@tonic-gate break; 5767c478bd9Sstevel@tonic-gate } 5777c478bd9Sstevel@tonic-gate if (i < nzents) { 5787c478bd9Sstevel@tonic-gate free(name); 5797c478bd9Sstevel@tonic-gate continue; 5807c478bd9Sstevel@tonic-gate } 581108322fbScarlsonj if (lookup_zone_info(name, ZONE_ID_UNDEFINED, &zent) != Z_OK) { 5827c478bd9Sstevel@tonic-gate free(name); 5837c478bd9Sstevel@tonic-gate continue; 5847c478bd9Sstevel@tonic-gate } 5857c478bd9Sstevel@tonic-gate free(name); 5867c478bd9Sstevel@tonic-gate if (zent.zstate_num >= min_state) 5877c478bd9Sstevel@tonic-gate zone_print(&zent, verbose, parsable); 5887c478bd9Sstevel@tonic-gate } 5897c478bd9Sstevel@tonic-gate endzoneent(cookie); 590108322fbScarlsonj return (Z_OK); 5917c478bd9Sstevel@tonic-gate } 5927c478bd9Sstevel@tonic-gate 5937c478bd9Sstevel@tonic-gate static zone_entry_t * 5947c478bd9Sstevel@tonic-gate lookup_running_zone(char *str) 5957c478bd9Sstevel@tonic-gate { 5967c478bd9Sstevel@tonic-gate zoneid_t zoneid; 5977c478bd9Sstevel@tonic-gate char *cp; 5987c478bd9Sstevel@tonic-gate int i; 5997c478bd9Sstevel@tonic-gate 6007c478bd9Sstevel@tonic-gate if (fetch_zents() != Z_OK) 6017c478bd9Sstevel@tonic-gate return (NULL); 6027c478bd9Sstevel@tonic-gate 6037c478bd9Sstevel@tonic-gate for (i = 0; i < nzents; i++) { 6047c478bd9Sstevel@tonic-gate if (strcmp(str, zents[i].zname) == 0) 6057c478bd9Sstevel@tonic-gate return (&zents[i]); 6067c478bd9Sstevel@tonic-gate } 6077c478bd9Sstevel@tonic-gate errno = 0; 6087c478bd9Sstevel@tonic-gate zoneid = strtol(str, &cp, 0); 6097c478bd9Sstevel@tonic-gate if (zoneid < MIN_ZONEID || zoneid > MAX_ZONEID || 6107c478bd9Sstevel@tonic-gate errno != 0 || *cp != '\0') 6117c478bd9Sstevel@tonic-gate return (NULL); 6127c478bd9Sstevel@tonic-gate for (i = 0; i < nzents; i++) { 6137c478bd9Sstevel@tonic-gate if (zoneid == zents[i].zid) 6147c478bd9Sstevel@tonic-gate return (&zents[i]); 6157c478bd9Sstevel@tonic-gate } 6167c478bd9Sstevel@tonic-gate return (NULL); 6177c478bd9Sstevel@tonic-gate } 6187c478bd9Sstevel@tonic-gate 6197c478bd9Sstevel@tonic-gate /* 6207c478bd9Sstevel@tonic-gate * Check a bit in a mode_t: if on is B_TRUE, that bit should be on; if 6217c478bd9Sstevel@tonic-gate * B_FALSE, it should be off. Return B_TRUE if the mode is bad (incorrect). 6227c478bd9Sstevel@tonic-gate */ 6237c478bd9Sstevel@tonic-gate static boolean_t 6247c478bd9Sstevel@tonic-gate bad_mode_bit(mode_t mode, mode_t bit, boolean_t on, char *file) 6257c478bd9Sstevel@tonic-gate { 6267c478bd9Sstevel@tonic-gate char *str; 6277c478bd9Sstevel@tonic-gate 6287c478bd9Sstevel@tonic-gate assert(bit == S_IRUSR || bit == S_IWUSR || bit == S_IXUSR || 6297c478bd9Sstevel@tonic-gate bit == S_IRGRP || bit == S_IWGRP || bit == S_IXGRP || 6307c478bd9Sstevel@tonic-gate bit == S_IROTH || bit == S_IWOTH || bit == S_IXOTH); 6317c478bd9Sstevel@tonic-gate /* 6327c478bd9Sstevel@tonic-gate * TRANSLATION_NOTE 6337c478bd9Sstevel@tonic-gate * The strings below will be used as part of a larger message, 6347c478bd9Sstevel@tonic-gate * either: 6357c478bd9Sstevel@tonic-gate * (file name) must be (owner|group|world) (read|writ|execut)able 6367c478bd9Sstevel@tonic-gate * or 6377c478bd9Sstevel@tonic-gate * (file name) must not be (owner|group|world) (read|writ|execut)able 6387c478bd9Sstevel@tonic-gate */ 6397c478bd9Sstevel@tonic-gate switch (bit) { 6407c478bd9Sstevel@tonic-gate case S_IRUSR: 6417c478bd9Sstevel@tonic-gate str = gettext("owner readable"); 6427c478bd9Sstevel@tonic-gate break; 6437c478bd9Sstevel@tonic-gate case S_IWUSR: 6447c478bd9Sstevel@tonic-gate str = gettext("owner writable"); 6457c478bd9Sstevel@tonic-gate break; 6467c478bd9Sstevel@tonic-gate case S_IXUSR: 6477c478bd9Sstevel@tonic-gate str = gettext("owner executable"); 6487c478bd9Sstevel@tonic-gate break; 6497c478bd9Sstevel@tonic-gate case S_IRGRP: 6507c478bd9Sstevel@tonic-gate str = gettext("group readable"); 6517c478bd9Sstevel@tonic-gate break; 6527c478bd9Sstevel@tonic-gate case S_IWGRP: 6537c478bd9Sstevel@tonic-gate str = gettext("group writable"); 6547c478bd9Sstevel@tonic-gate break; 6557c478bd9Sstevel@tonic-gate case S_IXGRP: 6567c478bd9Sstevel@tonic-gate str = gettext("group executable"); 6577c478bd9Sstevel@tonic-gate break; 6587c478bd9Sstevel@tonic-gate case S_IROTH: 6597c478bd9Sstevel@tonic-gate str = gettext("world readable"); 6607c478bd9Sstevel@tonic-gate break; 6617c478bd9Sstevel@tonic-gate case S_IWOTH: 6627c478bd9Sstevel@tonic-gate str = gettext("world writable"); 6637c478bd9Sstevel@tonic-gate break; 6647c478bd9Sstevel@tonic-gate case S_IXOTH: 6657c478bd9Sstevel@tonic-gate str = gettext("world executable"); 6667c478bd9Sstevel@tonic-gate break; 6677c478bd9Sstevel@tonic-gate } 6687c478bd9Sstevel@tonic-gate if ((mode & bit) == (on ? 0 : bit)) { 6697c478bd9Sstevel@tonic-gate /* 6707c478bd9Sstevel@tonic-gate * TRANSLATION_NOTE 6717c478bd9Sstevel@tonic-gate * The first parameter below is a file name; the second 6727c478bd9Sstevel@tonic-gate * is one of the "(owner|group|world) (read|writ|execut)able" 6737c478bd9Sstevel@tonic-gate * strings from above. 6747c478bd9Sstevel@tonic-gate */ 6757c478bd9Sstevel@tonic-gate /* 6767c478bd9Sstevel@tonic-gate * The code below could be simplified but not in a way 6777c478bd9Sstevel@tonic-gate * that would easily translate to non-English locales. 6787c478bd9Sstevel@tonic-gate */ 6797c478bd9Sstevel@tonic-gate if (on) { 6807c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("%s must be %s.\n"), 6817c478bd9Sstevel@tonic-gate file, str); 6827c478bd9Sstevel@tonic-gate } else { 6837c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("%s must not be %s.\n"), 6847c478bd9Sstevel@tonic-gate file, str); 6857c478bd9Sstevel@tonic-gate } 6867c478bd9Sstevel@tonic-gate return (B_TRUE); 6877c478bd9Sstevel@tonic-gate } 6887c478bd9Sstevel@tonic-gate return (B_FALSE); 6897c478bd9Sstevel@tonic-gate } 6907c478bd9Sstevel@tonic-gate 6917c478bd9Sstevel@tonic-gate /* 6927c478bd9Sstevel@tonic-gate * We want to make sure that no zone has its zone path as a child node 6937c478bd9Sstevel@tonic-gate * (in the directory sense) of any other. We do that by comparing this 6947c478bd9Sstevel@tonic-gate * zone's path to the path of all other (non-global) zones. The comparison 6957c478bd9Sstevel@tonic-gate * in each case is simple: add '/' to the end of the path, then do a 6967c478bd9Sstevel@tonic-gate * strncmp() of the two paths, using the length of the shorter one. 6977c478bd9Sstevel@tonic-gate */ 6987c478bd9Sstevel@tonic-gate 6997c478bd9Sstevel@tonic-gate static int 7007c478bd9Sstevel@tonic-gate crosscheck_zonepaths(char *path) 7017c478bd9Sstevel@tonic-gate { 7027c478bd9Sstevel@tonic-gate char rpath[MAXPATHLEN]; /* resolved path */ 7037c478bd9Sstevel@tonic-gate char path_copy[MAXPATHLEN]; /* copy of original path */ 7047c478bd9Sstevel@tonic-gate char rpath_copy[MAXPATHLEN]; /* copy of original rpath */ 7057c478bd9Sstevel@tonic-gate struct zoneent *ze; 7067c478bd9Sstevel@tonic-gate int res, err; 7077c478bd9Sstevel@tonic-gate FILE *cookie; 7087c478bd9Sstevel@tonic-gate 7097c478bd9Sstevel@tonic-gate cookie = setzoneent(); 7107c478bd9Sstevel@tonic-gate while ((ze = getzoneent_private(cookie)) != NULL) { 7117c478bd9Sstevel@tonic-gate /* Skip zones which are not installed. */ 7127c478bd9Sstevel@tonic-gate if (ze->zone_state < ZONE_STATE_INSTALLED) { 7137c478bd9Sstevel@tonic-gate free(ze); 7147c478bd9Sstevel@tonic-gate continue; 7157c478bd9Sstevel@tonic-gate } 7167c478bd9Sstevel@tonic-gate /* Skip the global zone and the current target zone. */ 7177c478bd9Sstevel@tonic-gate if (strcmp(ze->zone_name, GLOBAL_ZONENAME) == 0 || 7187c478bd9Sstevel@tonic-gate strcmp(ze->zone_name, target_zone) == 0) { 7197c478bd9Sstevel@tonic-gate free(ze); 7207c478bd9Sstevel@tonic-gate continue; 7217c478bd9Sstevel@tonic-gate } 7227c478bd9Sstevel@tonic-gate if (strlen(ze->zone_path) == 0) { 7237c478bd9Sstevel@tonic-gate /* old index file without path, fall back */ 7247c478bd9Sstevel@tonic-gate if ((err = zone_get_zonepath(ze->zone_name, 7257c478bd9Sstevel@tonic-gate ze->zone_path, sizeof (ze->zone_path))) != Z_OK) { 7267c478bd9Sstevel@tonic-gate errno = err; 7277c478bd9Sstevel@tonic-gate zperror2(ze->zone_name, 7287c478bd9Sstevel@tonic-gate gettext("could not get zone path")); 7297c478bd9Sstevel@tonic-gate free(ze); 7307c478bd9Sstevel@tonic-gate continue; 7317c478bd9Sstevel@tonic-gate } 7327c478bd9Sstevel@tonic-gate } 733108322fbScarlsonj (void) snprintf(path_copy, sizeof (path_copy), "%s%s", 734108322fbScarlsonj zonecfg_get_root(), ze->zone_path); 735108322fbScarlsonj res = resolvepath(path_copy, rpath, sizeof (rpath)); 7367c478bd9Sstevel@tonic-gate if (res == -1) { 7377c478bd9Sstevel@tonic-gate if (errno != ENOENT) { 738108322fbScarlsonj zperror(path_copy, B_FALSE); 7397c478bd9Sstevel@tonic-gate free(ze); 7407c478bd9Sstevel@tonic-gate return (Z_ERR); 7417c478bd9Sstevel@tonic-gate } 7427c478bd9Sstevel@tonic-gate (void) printf(gettext("WARNING: zone %s is installed, " 7437c478bd9Sstevel@tonic-gate "but its %s %s does not exist.\n"), ze->zone_name, 744108322fbScarlsonj "zonepath", path_copy); 7457c478bd9Sstevel@tonic-gate free(ze); 7467c478bd9Sstevel@tonic-gate continue; 7477c478bd9Sstevel@tonic-gate } 7487c478bd9Sstevel@tonic-gate rpath[res] = '\0'; 7497c478bd9Sstevel@tonic-gate (void) snprintf(path_copy, sizeof (path_copy), "%s/", path); 7507c478bd9Sstevel@tonic-gate (void) snprintf(rpath_copy, sizeof (rpath_copy), "%s/", rpath); 7517c478bd9Sstevel@tonic-gate if (strncmp(path_copy, rpath_copy, 7527c478bd9Sstevel@tonic-gate min(strlen(path_copy), strlen(rpath_copy))) == 0) { 7535ee84fbdSgjelinek /* 7545ee84fbdSgjelinek * TRANSLATION_NOTE 7555ee84fbdSgjelinek * zonepath is a literal that should not be translated. 7565ee84fbdSgjelinek */ 7577c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("%s zonepath (%s) and " 7587c478bd9Sstevel@tonic-gate "%s zonepath (%s) overlap.\n"), 7597c478bd9Sstevel@tonic-gate target_zone, path, ze->zone_name, rpath); 7607c478bd9Sstevel@tonic-gate free(ze); 7617c478bd9Sstevel@tonic-gate return (Z_ERR); 7627c478bd9Sstevel@tonic-gate } 7637c478bd9Sstevel@tonic-gate free(ze); 7647c478bd9Sstevel@tonic-gate } 7657c478bd9Sstevel@tonic-gate endzoneent(cookie); 7667c478bd9Sstevel@tonic-gate return (Z_OK); 7677c478bd9Sstevel@tonic-gate } 7687c478bd9Sstevel@tonic-gate 7697c478bd9Sstevel@tonic-gate static int 7707c478bd9Sstevel@tonic-gate validate_zonepath(char *path, int cmd_num) 7717c478bd9Sstevel@tonic-gate { 7727c478bd9Sstevel@tonic-gate int res; /* result of last library/system call */ 7737c478bd9Sstevel@tonic-gate boolean_t err = B_FALSE; /* have we run into an error? */ 7747c478bd9Sstevel@tonic-gate struct stat stbuf; 775*3f2f09c1Sdp struct statvfs64 vfsbuf; 7767c478bd9Sstevel@tonic-gate char rpath[MAXPATHLEN]; /* resolved path */ 7777c478bd9Sstevel@tonic-gate char ppath[MAXPATHLEN]; /* parent path */ 7787c478bd9Sstevel@tonic-gate char rppath[MAXPATHLEN]; /* resolved parent path */ 7797c478bd9Sstevel@tonic-gate char rootpath[MAXPATHLEN]; /* root path */ 7807c478bd9Sstevel@tonic-gate zone_state_t state; 7817c478bd9Sstevel@tonic-gate 7827c478bd9Sstevel@tonic-gate if (path[0] != '/') { 7837c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 7847c478bd9Sstevel@tonic-gate gettext("%s is not an absolute path.\n"), path); 7857c478bd9Sstevel@tonic-gate return (Z_ERR); 7867c478bd9Sstevel@tonic-gate } 7877c478bd9Sstevel@tonic-gate if ((res = resolvepath(path, rpath, sizeof (rpath))) == -1) { 7887c478bd9Sstevel@tonic-gate if ((errno != ENOENT) || 789865e09a4Sgjelinek (cmd_num != CMD_VERIFY && cmd_num != CMD_INSTALL && 790865e09a4Sgjelinek cmd_num != CMD_CLONE && cmd_num != CMD_MOVE)) { 7917c478bd9Sstevel@tonic-gate zperror(path, B_FALSE); 7927c478bd9Sstevel@tonic-gate return (Z_ERR); 7937c478bd9Sstevel@tonic-gate } 7947c478bd9Sstevel@tonic-gate if (cmd_num == CMD_VERIFY) { 7955ee84fbdSgjelinek /* 7965ee84fbdSgjelinek * TRANSLATION_NOTE 7975ee84fbdSgjelinek * zoneadm is a literal that should not be translated. 7985ee84fbdSgjelinek */ 7997c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("WARNING: %s does not " 8005ee84fbdSgjelinek "exist, so it could not be verified.\nWhen " 8015ee84fbdSgjelinek "'zoneadm %s' is run, '%s' will try to create\n%s, " 8025ee84fbdSgjelinek "and '%s' will be tried again,\nbut the '%s' may " 8035ee84fbdSgjelinek "fail if:\nthe parent directory of %s is group- or " 8045ee84fbdSgjelinek "other-writable\nor\n%s overlaps with any other " 8057c478bd9Sstevel@tonic-gate "installed zones.\n"), path, 8067c478bd9Sstevel@tonic-gate cmd_to_str(CMD_INSTALL), cmd_to_str(CMD_INSTALL), 8077c478bd9Sstevel@tonic-gate path, cmd_to_str(CMD_VERIFY), 8087c478bd9Sstevel@tonic-gate cmd_to_str(CMD_VERIFY), path, path); 8097c478bd9Sstevel@tonic-gate return (Z_OK); 8107c478bd9Sstevel@tonic-gate } 8117c478bd9Sstevel@tonic-gate /* 8127c478bd9Sstevel@tonic-gate * The zonepath is supposed to be mode 700 but its 8137c478bd9Sstevel@tonic-gate * parent(s) 755. So use 755 on the mkdirp() then 8147c478bd9Sstevel@tonic-gate * chmod() the zonepath itself to 700. 8157c478bd9Sstevel@tonic-gate */ 8167c478bd9Sstevel@tonic-gate if (mkdirp(path, DEFAULT_DIR_MODE) < 0) { 8177c478bd9Sstevel@tonic-gate zperror(path, B_FALSE); 8187c478bd9Sstevel@tonic-gate return (Z_ERR); 8197c478bd9Sstevel@tonic-gate } 8207c478bd9Sstevel@tonic-gate /* 8217c478bd9Sstevel@tonic-gate * If the chmod() fails, report the error, but might 8227c478bd9Sstevel@tonic-gate * as well continue the verify procedure. 8237c478bd9Sstevel@tonic-gate */ 8247c478bd9Sstevel@tonic-gate if (chmod(path, S_IRWXU) != 0) 8257c478bd9Sstevel@tonic-gate zperror(path, B_FALSE); 8267c478bd9Sstevel@tonic-gate /* 8277c478bd9Sstevel@tonic-gate * Since the mkdir() succeeded, we should not have to 8287c478bd9Sstevel@tonic-gate * worry about a subsequent ENOENT, thus this should 8297c478bd9Sstevel@tonic-gate * only recurse once. 8307c478bd9Sstevel@tonic-gate */ 831865e09a4Sgjelinek return (validate_zonepath(path, cmd_num)); 8327c478bd9Sstevel@tonic-gate } 8337c478bd9Sstevel@tonic-gate rpath[res] = '\0'; 8347c478bd9Sstevel@tonic-gate if (strcmp(path, rpath) != 0) { 8357c478bd9Sstevel@tonic-gate errno = Z_RESOLVED_PATH; 8367c478bd9Sstevel@tonic-gate zperror(path, B_TRUE); 8377c478bd9Sstevel@tonic-gate return (Z_ERR); 8387c478bd9Sstevel@tonic-gate } 8397c478bd9Sstevel@tonic-gate if ((res = stat(rpath, &stbuf)) != 0) { 8407c478bd9Sstevel@tonic-gate zperror(rpath, B_FALSE); 8417c478bd9Sstevel@tonic-gate return (Z_ERR); 8427c478bd9Sstevel@tonic-gate } 8437c478bd9Sstevel@tonic-gate if (!S_ISDIR(stbuf.st_mode)) { 8447c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("%s is not a directory.\n"), 8457c478bd9Sstevel@tonic-gate rpath); 8467c478bd9Sstevel@tonic-gate return (Z_ERR); 8477c478bd9Sstevel@tonic-gate } 8487c478bd9Sstevel@tonic-gate if ((strcmp(stbuf.st_fstype, MNTTYPE_TMPFS) == 0) || 8497c478bd9Sstevel@tonic-gate (strcmp(stbuf.st_fstype, MNTTYPE_XMEMFS) == 0)) { 8507c478bd9Sstevel@tonic-gate (void) printf(gettext("WARNING: %s is on a temporary " 8510b5de56dSgjelinek "file system.\n"), rpath); 8527c478bd9Sstevel@tonic-gate } 8537c478bd9Sstevel@tonic-gate if (crosscheck_zonepaths(rpath) != Z_OK) 8547c478bd9Sstevel@tonic-gate return (Z_ERR); 8557c478bd9Sstevel@tonic-gate /* 8567c478bd9Sstevel@tonic-gate * Try to collect and report as many minor errors as possible 8577c478bd9Sstevel@tonic-gate * before returning, so the user can learn everything that needs 8587c478bd9Sstevel@tonic-gate * to be fixed up front. 8597c478bd9Sstevel@tonic-gate */ 8607c478bd9Sstevel@tonic-gate if (stbuf.st_uid != 0) { 8617c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("%s is not owned by root.\n"), 8627c478bd9Sstevel@tonic-gate rpath); 8637c478bd9Sstevel@tonic-gate err = B_TRUE; 8647c478bd9Sstevel@tonic-gate } 8657c478bd9Sstevel@tonic-gate err |= bad_mode_bit(stbuf.st_mode, S_IRUSR, B_TRUE, rpath); 8667c478bd9Sstevel@tonic-gate err |= bad_mode_bit(stbuf.st_mode, S_IWUSR, B_TRUE, rpath); 8677c478bd9Sstevel@tonic-gate err |= bad_mode_bit(stbuf.st_mode, S_IXUSR, B_TRUE, rpath); 8687c478bd9Sstevel@tonic-gate err |= bad_mode_bit(stbuf.st_mode, S_IRGRP, B_FALSE, rpath); 8697c478bd9Sstevel@tonic-gate err |= bad_mode_bit(stbuf.st_mode, S_IWGRP, B_FALSE, rpath); 8707c478bd9Sstevel@tonic-gate err |= bad_mode_bit(stbuf.st_mode, S_IXGRP, B_FALSE, rpath); 8717c478bd9Sstevel@tonic-gate err |= bad_mode_bit(stbuf.st_mode, S_IROTH, B_FALSE, rpath); 8727c478bd9Sstevel@tonic-gate err |= bad_mode_bit(stbuf.st_mode, S_IWOTH, B_FALSE, rpath); 8737c478bd9Sstevel@tonic-gate err |= bad_mode_bit(stbuf.st_mode, S_IXOTH, B_FALSE, rpath); 8747c478bd9Sstevel@tonic-gate 8757c478bd9Sstevel@tonic-gate (void) snprintf(ppath, sizeof (ppath), "%s/..", path); 8767c478bd9Sstevel@tonic-gate if ((res = resolvepath(ppath, rppath, sizeof (rppath))) == -1) { 8777c478bd9Sstevel@tonic-gate zperror(ppath, B_FALSE); 8787c478bd9Sstevel@tonic-gate return (Z_ERR); 8797c478bd9Sstevel@tonic-gate } 8807c478bd9Sstevel@tonic-gate rppath[res] = '\0'; 8817c478bd9Sstevel@tonic-gate if ((res = stat(rppath, &stbuf)) != 0) { 8827c478bd9Sstevel@tonic-gate zperror(rppath, B_FALSE); 8837c478bd9Sstevel@tonic-gate return (Z_ERR); 8847c478bd9Sstevel@tonic-gate } 8857c478bd9Sstevel@tonic-gate /* theoretically impossible */ 8867c478bd9Sstevel@tonic-gate if (!S_ISDIR(stbuf.st_mode)) { 8877c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("%s is not a directory.\n"), 8887c478bd9Sstevel@tonic-gate rppath); 8897c478bd9Sstevel@tonic-gate return (Z_ERR); 8907c478bd9Sstevel@tonic-gate } 8917c478bd9Sstevel@tonic-gate if (stbuf.st_uid != 0) { 8927c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("%s is not owned by root.\n"), 8937c478bd9Sstevel@tonic-gate rppath); 8947c478bd9Sstevel@tonic-gate err = B_TRUE; 8957c478bd9Sstevel@tonic-gate } 8967c478bd9Sstevel@tonic-gate err |= bad_mode_bit(stbuf.st_mode, S_IRUSR, B_TRUE, rppath); 8977c478bd9Sstevel@tonic-gate err |= bad_mode_bit(stbuf.st_mode, S_IWUSR, B_TRUE, rppath); 8987c478bd9Sstevel@tonic-gate err |= bad_mode_bit(stbuf.st_mode, S_IXUSR, B_TRUE, rppath); 8997c478bd9Sstevel@tonic-gate err |= bad_mode_bit(stbuf.st_mode, S_IWGRP, B_FALSE, rppath); 9007c478bd9Sstevel@tonic-gate err |= bad_mode_bit(stbuf.st_mode, S_IWOTH, B_FALSE, rppath); 9017c478bd9Sstevel@tonic-gate if (strcmp(rpath, rppath) == 0) { 9027c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("%s is its own parent.\n"), 9037c478bd9Sstevel@tonic-gate rppath); 9047c478bd9Sstevel@tonic-gate err = B_TRUE; 9057c478bd9Sstevel@tonic-gate } 9067c478bd9Sstevel@tonic-gate 907*3f2f09c1Sdp if (statvfs64(rpath, &vfsbuf) != 0) { 9087c478bd9Sstevel@tonic-gate zperror(rpath, B_FALSE); 9097c478bd9Sstevel@tonic-gate return (Z_ERR); 9107c478bd9Sstevel@tonic-gate } 911b5abaf04Sgjelinek if (strcmp(vfsbuf.f_basetype, MNTTYPE_NFS) == 0) { 9125ee84fbdSgjelinek /* 9135ee84fbdSgjelinek * TRANSLATION_NOTE 9145ee84fbdSgjelinek * Zonepath and NFS are literals that should not be translated. 9155ee84fbdSgjelinek */ 9165ee84fbdSgjelinek (void) fprintf(stderr, gettext("Zonepath %s is on an NFS " 9170b5de56dSgjelinek "mounted file system.\n" 9180b5de56dSgjelinek "\tA local file system must be used.\n"), rpath); 919b5abaf04Sgjelinek return (Z_ERR); 920b5abaf04Sgjelinek } 921b5abaf04Sgjelinek if (vfsbuf.f_flag & ST_NOSUID) { 9225ee84fbdSgjelinek /* 9235ee84fbdSgjelinek * TRANSLATION_NOTE 9245ee84fbdSgjelinek * Zonepath and nosuid are literals that should not be 9255ee84fbdSgjelinek * translated. 9265ee84fbdSgjelinek */ 927b5abaf04Sgjelinek (void) fprintf(stderr, gettext("Zonepath %s is on a nosuid " 9280b5de56dSgjelinek "file system.\n"), rpath); 9297c478bd9Sstevel@tonic-gate return (Z_ERR); 9307c478bd9Sstevel@tonic-gate } 9317c478bd9Sstevel@tonic-gate 9327c478bd9Sstevel@tonic-gate if ((res = zone_get_state(target_zone, &state)) != Z_OK) { 9337c478bd9Sstevel@tonic-gate errno = res; 9347c478bd9Sstevel@tonic-gate zperror2(target_zone, gettext("could not get state")); 9357c478bd9Sstevel@tonic-gate return (Z_ERR); 9367c478bd9Sstevel@tonic-gate } 9377c478bd9Sstevel@tonic-gate /* 9387c478bd9Sstevel@tonic-gate * The existence of the root path is only bad in the configured state, 9397c478bd9Sstevel@tonic-gate * as it is *supposed* to be there at the installed and later states. 940ee519a1fSgjelinek * However, the root path is expected to be there if the zone is 941ee519a1fSgjelinek * detached. 9427c478bd9Sstevel@tonic-gate * State/command mismatches are caught earlier in verify_details(). 9437c478bd9Sstevel@tonic-gate */ 944ee519a1fSgjelinek if (state == ZONE_STATE_CONFIGURED && cmd_num != CMD_ATTACH) { 9457c478bd9Sstevel@tonic-gate if (snprintf(rootpath, sizeof (rootpath), "%s/root", rpath) >= 9467c478bd9Sstevel@tonic-gate sizeof (rootpath)) { 9475ee84fbdSgjelinek /* 9485ee84fbdSgjelinek * TRANSLATION_NOTE 9495ee84fbdSgjelinek * Zonepath is a literal that should not be translated. 9505ee84fbdSgjelinek */ 9517c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 9527c478bd9Sstevel@tonic-gate gettext("Zonepath %s is too long.\n"), rpath); 9537c478bd9Sstevel@tonic-gate return (Z_ERR); 9547c478bd9Sstevel@tonic-gate } 9557c478bd9Sstevel@tonic-gate if ((res = stat(rootpath, &stbuf)) == 0) { 956ee519a1fSgjelinek if (zonecfg_detached(rpath)) 957ee519a1fSgjelinek (void) fprintf(stderr, 958ee519a1fSgjelinek gettext("Cannot %s detached " 959ee519a1fSgjelinek "zone.\nUse attach or remove %s " 960ee519a1fSgjelinek "directory.\n"), cmd_to_str(cmd_num), 961ee519a1fSgjelinek rpath); 962ee519a1fSgjelinek else 963ee519a1fSgjelinek (void) fprintf(stderr, 964ee519a1fSgjelinek gettext("Rootpath %s exists; " 965ee519a1fSgjelinek "remove or move aside prior to %s.\n"), 966ee519a1fSgjelinek rootpath, cmd_to_str(cmd_num)); 9677c478bd9Sstevel@tonic-gate return (Z_ERR); 9687c478bd9Sstevel@tonic-gate } 9697c478bd9Sstevel@tonic-gate } 9707c478bd9Sstevel@tonic-gate 9717c478bd9Sstevel@tonic-gate return (err ? Z_ERR : Z_OK); 9727c478bd9Sstevel@tonic-gate } 9737c478bd9Sstevel@tonic-gate 9747c478bd9Sstevel@tonic-gate static void 9757c478bd9Sstevel@tonic-gate release_lock_file(int lockfd) 9767c478bd9Sstevel@tonic-gate { 9777c478bd9Sstevel@tonic-gate (void) close(lockfd); 9787c478bd9Sstevel@tonic-gate } 9797c478bd9Sstevel@tonic-gate 9807c478bd9Sstevel@tonic-gate static int 9817c478bd9Sstevel@tonic-gate grab_lock_file(const char *zone_name, int *lockfd) 9827c478bd9Sstevel@tonic-gate { 9837c478bd9Sstevel@tonic-gate char pathbuf[PATH_MAX]; 9847c478bd9Sstevel@tonic-gate struct flock flock; 9857c478bd9Sstevel@tonic-gate 986108322fbScarlsonj if (snprintf(pathbuf, sizeof (pathbuf), "%s%s", zonecfg_get_root(), 987108322fbScarlsonj ZONES_TMPDIR) >= sizeof (pathbuf)) { 988108322fbScarlsonj zerror(gettext("alternate root path is too long")); 989108322fbScarlsonj return (Z_ERR); 990108322fbScarlsonj } 991108322fbScarlsonj if (mkdir(pathbuf, S_IRWXU) < 0 && errno != EEXIST) { 992108322fbScarlsonj zerror(gettext("could not mkdir %s: %s"), pathbuf, 9937c478bd9Sstevel@tonic-gate strerror(errno)); 9947c478bd9Sstevel@tonic-gate return (Z_ERR); 9957c478bd9Sstevel@tonic-gate } 996108322fbScarlsonj (void) chmod(pathbuf, S_IRWXU); 9977c478bd9Sstevel@tonic-gate 9987c478bd9Sstevel@tonic-gate /* 9997c478bd9Sstevel@tonic-gate * One of these lock files is created for each zone (when needed). 10007c478bd9Sstevel@tonic-gate * The lock files are not cleaned up (except on system reboot), 10017c478bd9Sstevel@tonic-gate * but since there is only one per zone, there is no resource 10027c478bd9Sstevel@tonic-gate * starvation issue. 10037c478bd9Sstevel@tonic-gate */ 1004108322fbScarlsonj if (snprintf(pathbuf, sizeof (pathbuf), "%s%s/%s.zoneadm.lock", 1005108322fbScarlsonj zonecfg_get_root(), ZONES_TMPDIR, zone_name) >= sizeof (pathbuf)) { 1006108322fbScarlsonj zerror(gettext("alternate root path is too long")); 1007108322fbScarlsonj return (Z_ERR); 1008108322fbScarlsonj } 10097c478bd9Sstevel@tonic-gate if ((*lockfd = open(pathbuf, O_RDWR|O_CREAT, S_IRUSR|S_IWUSR)) < 0) { 10107c478bd9Sstevel@tonic-gate zerror(gettext("could not open %s: %s"), pathbuf, 10117c478bd9Sstevel@tonic-gate strerror(errno)); 10127c478bd9Sstevel@tonic-gate return (Z_ERR); 10137c478bd9Sstevel@tonic-gate } 10147c478bd9Sstevel@tonic-gate /* 10157c478bd9Sstevel@tonic-gate * Lock the file to synchronize with other zoneadmds 10167c478bd9Sstevel@tonic-gate */ 10177c478bd9Sstevel@tonic-gate flock.l_type = F_WRLCK; 10187c478bd9Sstevel@tonic-gate flock.l_whence = SEEK_SET; 10197c478bd9Sstevel@tonic-gate flock.l_start = (off_t)0; 10207c478bd9Sstevel@tonic-gate flock.l_len = (off_t)0; 10217c478bd9Sstevel@tonic-gate if (fcntl(*lockfd, F_SETLKW, &flock) < 0) { 10227c478bd9Sstevel@tonic-gate zerror(gettext("unable to lock %s: %s"), pathbuf, 10237c478bd9Sstevel@tonic-gate strerror(errno)); 10247c478bd9Sstevel@tonic-gate release_lock_file(*lockfd); 10257c478bd9Sstevel@tonic-gate return (Z_ERR); 10267c478bd9Sstevel@tonic-gate } 10277c478bd9Sstevel@tonic-gate return (Z_OK); 10287c478bd9Sstevel@tonic-gate } 10297c478bd9Sstevel@tonic-gate 1030108322fbScarlsonj static boolean_t 10317c478bd9Sstevel@tonic-gate get_doorname(const char *zone_name, char *buffer) 10327c478bd9Sstevel@tonic-gate { 1033108322fbScarlsonj return (snprintf(buffer, PATH_MAX, "%s" ZONE_DOOR_PATH, 1034108322fbScarlsonj zonecfg_get_root(), zone_name) < PATH_MAX); 10357c478bd9Sstevel@tonic-gate } 10367c478bd9Sstevel@tonic-gate 10377c478bd9Sstevel@tonic-gate /* 10387c478bd9Sstevel@tonic-gate * system daemons are not audited. For the global zone, this occurs 10397c478bd9Sstevel@tonic-gate * "naturally" since init is started with the default audit 10407c478bd9Sstevel@tonic-gate * characteristics. Since zoneadmd is a system daemon and it starts 10417c478bd9Sstevel@tonic-gate * init for a zone, it is necessary to clear out the audit 10427c478bd9Sstevel@tonic-gate * characteristics inherited from whomever started zoneadmd. This is 10437c478bd9Sstevel@tonic-gate * indicated by the audit id, which is set from the ruid parameter of 10447c478bd9Sstevel@tonic-gate * adt_set_user(), below. 10457c478bd9Sstevel@tonic-gate */ 10467c478bd9Sstevel@tonic-gate 10477c478bd9Sstevel@tonic-gate static void 10487c478bd9Sstevel@tonic-gate prepare_audit_context() 10497c478bd9Sstevel@tonic-gate { 10507c478bd9Sstevel@tonic-gate adt_session_data_t *ah; 10517c478bd9Sstevel@tonic-gate char *failure = gettext("audit failure: %s"); 10527c478bd9Sstevel@tonic-gate 10537c478bd9Sstevel@tonic-gate if (adt_start_session(&ah, NULL, 0)) { 10547c478bd9Sstevel@tonic-gate zerror(failure, strerror(errno)); 10557c478bd9Sstevel@tonic-gate return; 10567c478bd9Sstevel@tonic-gate } 10577c478bd9Sstevel@tonic-gate if (adt_set_user(ah, ADT_NO_AUDIT, ADT_NO_AUDIT, 10587c478bd9Sstevel@tonic-gate ADT_NO_AUDIT, ADT_NO_AUDIT, NULL, ADT_NEW)) { 10597c478bd9Sstevel@tonic-gate zerror(failure, strerror(errno)); 10607c478bd9Sstevel@tonic-gate (void) adt_end_session(ah); 10617c478bd9Sstevel@tonic-gate return; 10627c478bd9Sstevel@tonic-gate } 10637c478bd9Sstevel@tonic-gate if (adt_set_proc(ah)) 10647c478bd9Sstevel@tonic-gate zerror(failure, strerror(errno)); 10657c478bd9Sstevel@tonic-gate 10667c478bd9Sstevel@tonic-gate (void) adt_end_session(ah); 10677c478bd9Sstevel@tonic-gate } 10687c478bd9Sstevel@tonic-gate 10697c478bd9Sstevel@tonic-gate static int 10707c478bd9Sstevel@tonic-gate start_zoneadmd(const char *zone_name) 10717c478bd9Sstevel@tonic-gate { 10727c478bd9Sstevel@tonic-gate char doorpath[PATH_MAX]; 10737c478bd9Sstevel@tonic-gate pid_t child_pid; 10747c478bd9Sstevel@tonic-gate int error = Z_ERR; 10757c478bd9Sstevel@tonic-gate int doorfd, lockfd; 10767c478bd9Sstevel@tonic-gate struct door_info info; 10777c478bd9Sstevel@tonic-gate 1078108322fbScarlsonj if (!get_doorname(zone_name, doorpath)) 1079108322fbScarlsonj return (Z_ERR); 10807c478bd9Sstevel@tonic-gate 10817c478bd9Sstevel@tonic-gate if (grab_lock_file(zone_name, &lockfd) != Z_OK) 10827c478bd9Sstevel@tonic-gate return (Z_ERR); 10837c478bd9Sstevel@tonic-gate 10847c478bd9Sstevel@tonic-gate /* 10857c478bd9Sstevel@tonic-gate * Now that we have the lock, re-confirm that the daemon is 10867c478bd9Sstevel@tonic-gate * *not* up and working fine. If it is still down, we have a green 10877c478bd9Sstevel@tonic-gate * light to start it. 10887c478bd9Sstevel@tonic-gate */ 10897c478bd9Sstevel@tonic-gate if ((doorfd = open(doorpath, O_RDONLY)) < 0) { 10907c478bd9Sstevel@tonic-gate if (errno != ENOENT) { 10917c478bd9Sstevel@tonic-gate zperror(doorpath, B_FALSE); 10927c478bd9Sstevel@tonic-gate goto out; 10937c478bd9Sstevel@tonic-gate } 10947c478bd9Sstevel@tonic-gate } else { 10957c478bd9Sstevel@tonic-gate if (door_info(doorfd, &info) == 0 && 10967c478bd9Sstevel@tonic-gate ((info.di_attributes & DOOR_REVOKED) == 0)) { 10977c478bd9Sstevel@tonic-gate error = Z_OK; 10987c478bd9Sstevel@tonic-gate (void) close(doorfd); 10997c478bd9Sstevel@tonic-gate goto out; 11007c478bd9Sstevel@tonic-gate } 11017c478bd9Sstevel@tonic-gate (void) close(doorfd); 11027c478bd9Sstevel@tonic-gate } 11037c478bd9Sstevel@tonic-gate 11047c478bd9Sstevel@tonic-gate if ((child_pid = fork()) == -1) { 11057c478bd9Sstevel@tonic-gate zperror(gettext("could not fork"), B_FALSE); 11067c478bd9Sstevel@tonic-gate goto out; 11077c478bd9Sstevel@tonic-gate } else if (child_pid == 0) { 1108108322fbScarlsonj const char *argv[6], **ap; 11097c478bd9Sstevel@tonic-gate 1110108322fbScarlsonj /* child process */ 11117c478bd9Sstevel@tonic-gate prepare_audit_context(); 11127c478bd9Sstevel@tonic-gate 1113108322fbScarlsonj ap = argv; 1114108322fbScarlsonj *ap++ = "zoneadmd"; 1115108322fbScarlsonj *ap++ = "-z"; 1116108322fbScarlsonj *ap++ = zone_name; 1117108322fbScarlsonj if (zonecfg_in_alt_root()) { 1118108322fbScarlsonj *ap++ = "-R"; 1119108322fbScarlsonj *ap++ = zonecfg_get_root(); 1120108322fbScarlsonj } 1121108322fbScarlsonj *ap = NULL; 1122108322fbScarlsonj 1123108322fbScarlsonj (void) execv("/usr/lib/zones/zoneadmd", (char * const *)argv); 11245ee84fbdSgjelinek /* 11255ee84fbdSgjelinek * TRANSLATION_NOTE 11265ee84fbdSgjelinek * zoneadmd is a literal that should not be translated. 11275ee84fbdSgjelinek */ 11287c478bd9Sstevel@tonic-gate zperror(gettext("could not exec zoneadmd"), B_FALSE); 11297c478bd9Sstevel@tonic-gate _exit(Z_ERR); 11307c478bd9Sstevel@tonic-gate } else { 11317c478bd9Sstevel@tonic-gate /* parent process */ 11327c478bd9Sstevel@tonic-gate pid_t retval; 11337c478bd9Sstevel@tonic-gate int pstatus = 0; 11347c478bd9Sstevel@tonic-gate 11357c478bd9Sstevel@tonic-gate do { 11367c478bd9Sstevel@tonic-gate retval = waitpid(child_pid, &pstatus, 0); 11377c478bd9Sstevel@tonic-gate } while (retval != child_pid); 11387c478bd9Sstevel@tonic-gate if (WIFSIGNALED(pstatus) || (WIFEXITED(pstatus) && 11397c478bd9Sstevel@tonic-gate WEXITSTATUS(pstatus) != 0)) { 11407c478bd9Sstevel@tonic-gate zerror(gettext("could not start %s"), "zoneadmd"); 11417c478bd9Sstevel@tonic-gate goto out; 11427c478bd9Sstevel@tonic-gate } 11437c478bd9Sstevel@tonic-gate } 11447c478bd9Sstevel@tonic-gate error = Z_OK; 11457c478bd9Sstevel@tonic-gate out: 11467c478bd9Sstevel@tonic-gate release_lock_file(lockfd); 11477c478bd9Sstevel@tonic-gate return (error); 11487c478bd9Sstevel@tonic-gate } 11497c478bd9Sstevel@tonic-gate 11507c478bd9Sstevel@tonic-gate static int 11517c478bd9Sstevel@tonic-gate ping_zoneadmd(const char *zone_name) 11527c478bd9Sstevel@tonic-gate { 11537c478bd9Sstevel@tonic-gate char doorpath[PATH_MAX]; 11547c478bd9Sstevel@tonic-gate int doorfd; 11557c478bd9Sstevel@tonic-gate struct door_info info; 11567c478bd9Sstevel@tonic-gate 1157108322fbScarlsonj if (!get_doorname(zone_name, doorpath)) 1158108322fbScarlsonj return (Z_ERR); 11597c478bd9Sstevel@tonic-gate 11607c478bd9Sstevel@tonic-gate if ((doorfd = open(doorpath, O_RDONLY)) < 0) { 11617c478bd9Sstevel@tonic-gate return (Z_ERR); 11627c478bd9Sstevel@tonic-gate } 11637c478bd9Sstevel@tonic-gate if (door_info(doorfd, &info) == 0 && 11647c478bd9Sstevel@tonic-gate ((info.di_attributes & DOOR_REVOKED) == 0)) { 11657c478bd9Sstevel@tonic-gate (void) close(doorfd); 11667c478bd9Sstevel@tonic-gate return (Z_OK); 11677c478bd9Sstevel@tonic-gate } 11687c478bd9Sstevel@tonic-gate (void) close(doorfd); 11697c478bd9Sstevel@tonic-gate return (Z_ERR); 11707c478bd9Sstevel@tonic-gate } 11717c478bd9Sstevel@tonic-gate 11727c478bd9Sstevel@tonic-gate static int 11737c478bd9Sstevel@tonic-gate call_zoneadmd(const char *zone_name, zone_cmd_arg_t *arg) 11747c478bd9Sstevel@tonic-gate { 11757c478bd9Sstevel@tonic-gate char doorpath[PATH_MAX]; 11767c478bd9Sstevel@tonic-gate int doorfd, result; 11777c478bd9Sstevel@tonic-gate door_arg_t darg; 11787c478bd9Sstevel@tonic-gate 11797c478bd9Sstevel@tonic-gate zoneid_t zoneid; 11807c478bd9Sstevel@tonic-gate uint64_t uniqid = 0; 11817c478bd9Sstevel@tonic-gate 11827c478bd9Sstevel@tonic-gate zone_cmd_rval_t *rvalp; 11837c478bd9Sstevel@tonic-gate size_t rlen; 11847c478bd9Sstevel@tonic-gate char *cp, *errbuf; 11857c478bd9Sstevel@tonic-gate 11867c478bd9Sstevel@tonic-gate rlen = getpagesize(); 11877c478bd9Sstevel@tonic-gate if ((rvalp = malloc(rlen)) == NULL) { 11887c478bd9Sstevel@tonic-gate zerror(gettext("failed to allocate %lu bytes: %s"), rlen, 11897c478bd9Sstevel@tonic-gate strerror(errno)); 11907c478bd9Sstevel@tonic-gate return (-1); 11917c478bd9Sstevel@tonic-gate } 11927c478bd9Sstevel@tonic-gate 11937c478bd9Sstevel@tonic-gate if ((zoneid = getzoneidbyname(zone_name)) != ZONE_ID_UNDEFINED) { 11947c478bd9Sstevel@tonic-gate (void) zone_getattr(zoneid, ZONE_ATTR_UNIQID, &uniqid, 11957c478bd9Sstevel@tonic-gate sizeof (uniqid)); 11967c478bd9Sstevel@tonic-gate } 11977c478bd9Sstevel@tonic-gate arg->uniqid = uniqid; 11987c478bd9Sstevel@tonic-gate (void) strlcpy(arg->locale, locale, sizeof (arg->locale)); 1199108322fbScarlsonj if (!get_doorname(zone_name, doorpath)) { 1200108322fbScarlsonj zerror(gettext("alternate root path is too long")); 1201108322fbScarlsonj free(rvalp); 1202108322fbScarlsonj return (-1); 1203108322fbScarlsonj } 12047c478bd9Sstevel@tonic-gate 12057c478bd9Sstevel@tonic-gate /* 12067c478bd9Sstevel@tonic-gate * Loop trying to start zoneadmd; if something goes seriously 12077c478bd9Sstevel@tonic-gate * wrong we break out and fail. 12087c478bd9Sstevel@tonic-gate */ 12097c478bd9Sstevel@tonic-gate for (;;) { 12107c478bd9Sstevel@tonic-gate if (start_zoneadmd(zone_name) != Z_OK) 12117c478bd9Sstevel@tonic-gate break; 12127c478bd9Sstevel@tonic-gate 12137c478bd9Sstevel@tonic-gate if ((doorfd = open(doorpath, O_RDONLY)) < 0) { 12147c478bd9Sstevel@tonic-gate zperror(gettext("failed to open zone door"), B_FALSE); 12157c478bd9Sstevel@tonic-gate break; 12167c478bd9Sstevel@tonic-gate } 12177c478bd9Sstevel@tonic-gate 12187c478bd9Sstevel@tonic-gate darg.data_ptr = (char *)arg; 12197c478bd9Sstevel@tonic-gate darg.data_size = sizeof (*arg); 12207c478bd9Sstevel@tonic-gate darg.desc_ptr = NULL; 12217c478bd9Sstevel@tonic-gate darg.desc_num = 0; 12227c478bd9Sstevel@tonic-gate darg.rbuf = (char *)rvalp; 12237c478bd9Sstevel@tonic-gate darg.rsize = rlen; 12247c478bd9Sstevel@tonic-gate if (door_call(doorfd, &darg) != 0) { 12257c478bd9Sstevel@tonic-gate (void) close(doorfd); 12267c478bd9Sstevel@tonic-gate /* 12277c478bd9Sstevel@tonic-gate * We'll get EBADF if the door has been revoked. 12287c478bd9Sstevel@tonic-gate */ 12297c478bd9Sstevel@tonic-gate if (errno != EBADF) { 12307c478bd9Sstevel@tonic-gate zperror(gettext("door_call failed"), B_FALSE); 12317c478bd9Sstevel@tonic-gate break; 12327c478bd9Sstevel@tonic-gate } 12337c478bd9Sstevel@tonic-gate continue; /* take another lap */ 12347c478bd9Sstevel@tonic-gate } 12357c478bd9Sstevel@tonic-gate (void) close(doorfd); 12367c478bd9Sstevel@tonic-gate 12377c478bd9Sstevel@tonic-gate if (darg.data_size == 0) { 12387c478bd9Sstevel@tonic-gate /* Door server is going away; kick it again. */ 12397c478bd9Sstevel@tonic-gate continue; 12407c478bd9Sstevel@tonic-gate } 12417c478bd9Sstevel@tonic-gate 12427c478bd9Sstevel@tonic-gate errbuf = rvalp->errbuf; 12437c478bd9Sstevel@tonic-gate while (*errbuf != '\0') { 12447c478bd9Sstevel@tonic-gate /* 12457c478bd9Sstevel@tonic-gate * Remove any newlines since zerror() 12467c478bd9Sstevel@tonic-gate * will append one automatically. 12477c478bd9Sstevel@tonic-gate */ 12487c478bd9Sstevel@tonic-gate cp = strchr(errbuf, '\n'); 12497c478bd9Sstevel@tonic-gate if (cp != NULL) 12507c478bd9Sstevel@tonic-gate *cp = '\0'; 12517c478bd9Sstevel@tonic-gate zerror("%s", errbuf); 12527c478bd9Sstevel@tonic-gate if (cp == NULL) 12537c478bd9Sstevel@tonic-gate break; 12547c478bd9Sstevel@tonic-gate errbuf = cp + 1; 12557c478bd9Sstevel@tonic-gate } 12567c478bd9Sstevel@tonic-gate result = rvalp->rval == 0 ? 0 : -1; 12577c478bd9Sstevel@tonic-gate free(rvalp); 12587c478bd9Sstevel@tonic-gate return (result); 12597c478bd9Sstevel@tonic-gate } 12607c478bd9Sstevel@tonic-gate 12617c478bd9Sstevel@tonic-gate free(rvalp); 12627c478bd9Sstevel@tonic-gate return (-1); 12637c478bd9Sstevel@tonic-gate } 12647c478bd9Sstevel@tonic-gate 12657c478bd9Sstevel@tonic-gate static int 12667c478bd9Sstevel@tonic-gate ready_func(int argc, char *argv[]) 12677c478bd9Sstevel@tonic-gate { 12687c478bd9Sstevel@tonic-gate zone_cmd_arg_t zarg; 12697c478bd9Sstevel@tonic-gate int arg; 12707c478bd9Sstevel@tonic-gate 1271108322fbScarlsonj if (zonecfg_in_alt_root()) { 1272108322fbScarlsonj zerror(gettext("cannot ready zone in alternate root")); 1273108322fbScarlsonj return (Z_ERR); 1274108322fbScarlsonj } 1275108322fbScarlsonj 12767c478bd9Sstevel@tonic-gate optind = 0; 12777c478bd9Sstevel@tonic-gate if ((arg = getopt(argc, argv, "?")) != EOF) { 12787c478bd9Sstevel@tonic-gate switch (arg) { 12797c478bd9Sstevel@tonic-gate case '?': 12807c478bd9Sstevel@tonic-gate sub_usage(SHELP_READY, CMD_READY); 12817c478bd9Sstevel@tonic-gate return (optopt == '?' ? Z_OK : Z_USAGE); 12827c478bd9Sstevel@tonic-gate default: 12837c478bd9Sstevel@tonic-gate sub_usage(SHELP_READY, CMD_READY); 12847c478bd9Sstevel@tonic-gate return (Z_USAGE); 12857c478bd9Sstevel@tonic-gate } 12867c478bd9Sstevel@tonic-gate } 12877c478bd9Sstevel@tonic-gate if (argc > optind) { 12887c478bd9Sstevel@tonic-gate sub_usage(SHELP_READY, CMD_READY); 12897c478bd9Sstevel@tonic-gate return (Z_USAGE); 12907c478bd9Sstevel@tonic-gate } 12917c478bd9Sstevel@tonic-gate if (sanity_check(target_zone, CMD_READY, B_FALSE, B_FALSE) != Z_OK) 12927c478bd9Sstevel@tonic-gate return (Z_ERR); 12937c478bd9Sstevel@tonic-gate if (verify_details(CMD_READY) != Z_OK) 12947c478bd9Sstevel@tonic-gate return (Z_ERR); 12957c478bd9Sstevel@tonic-gate 12967c478bd9Sstevel@tonic-gate zarg.cmd = Z_READY; 12977c478bd9Sstevel@tonic-gate if (call_zoneadmd(target_zone, &zarg) != 0) { 12987c478bd9Sstevel@tonic-gate zerror(gettext("call to %s failed"), "zoneadmd"); 12997c478bd9Sstevel@tonic-gate return (Z_ERR); 13007c478bd9Sstevel@tonic-gate } 13017c478bd9Sstevel@tonic-gate return (Z_OK); 13027c478bd9Sstevel@tonic-gate } 13037c478bd9Sstevel@tonic-gate 13047c478bd9Sstevel@tonic-gate static int 13057c478bd9Sstevel@tonic-gate boot_func(int argc, char *argv[]) 13067c478bd9Sstevel@tonic-gate { 13077c478bd9Sstevel@tonic-gate zone_cmd_arg_t zarg; 13087c478bd9Sstevel@tonic-gate int arg; 13097c478bd9Sstevel@tonic-gate 1310108322fbScarlsonj if (zonecfg_in_alt_root()) { 1311108322fbScarlsonj zerror(gettext("cannot boot zone in alternate root")); 1312108322fbScarlsonj return (Z_ERR); 1313108322fbScarlsonj } 1314108322fbScarlsonj 13157c478bd9Sstevel@tonic-gate zarg.bootbuf[0] = '\0'; 13167c478bd9Sstevel@tonic-gate 13177c478bd9Sstevel@tonic-gate /* 1318*3f2f09c1Sdp * The following getopt processes arguments to zone boot; that 1319*3f2f09c1Sdp * is to say, the [here] portion of the argument string: 1320*3f2f09c1Sdp * 1321*3f2f09c1Sdp * zoneadm -z myzone boot [here] -- -v -m verbose 1322*3f2f09c1Sdp * 1323*3f2f09c1Sdp * Where [here] can either be nothing, -? (in which case we bail 1324*3f2f09c1Sdp * and print usage), or -s. Support for -s is vestigal and 1325*3f2f09c1Sdp * obsolete, but is retained because it was a documented interface 1326*3f2f09c1Sdp * and there are known consumers including admin/install; the 1327*3f2f09c1Sdp * proper way to specify boot arguments like -s is: 1328*3f2f09c1Sdp * 1329*3f2f09c1Sdp * zoneadm -z myzone boot -- -s -v -m verbose. 13307c478bd9Sstevel@tonic-gate */ 13317c478bd9Sstevel@tonic-gate optind = 0; 13327c478bd9Sstevel@tonic-gate if ((arg = getopt(argc, argv, "?s")) != EOF) { 13337c478bd9Sstevel@tonic-gate switch (arg) { 13347c478bd9Sstevel@tonic-gate case '?': 13357c478bd9Sstevel@tonic-gate sub_usage(SHELP_BOOT, CMD_BOOT); 13367c478bd9Sstevel@tonic-gate return (optopt == '?' ? Z_OK : Z_USAGE); 13377c478bd9Sstevel@tonic-gate case 's': 13387c478bd9Sstevel@tonic-gate (void) strlcpy(zarg.bootbuf, "-s", 13397c478bd9Sstevel@tonic-gate sizeof (zarg.bootbuf)); 13407c478bd9Sstevel@tonic-gate break; 13417c478bd9Sstevel@tonic-gate default: 13427c478bd9Sstevel@tonic-gate sub_usage(SHELP_BOOT, CMD_BOOT); 13437c478bd9Sstevel@tonic-gate return (Z_USAGE); 13447c478bd9Sstevel@tonic-gate } 13457c478bd9Sstevel@tonic-gate } 1346*3f2f09c1Sdp 1347*3f2f09c1Sdp for (; optind < argc; optind++) { 1348*3f2f09c1Sdp if (strlcat(zarg.bootbuf, argv[optind], 1349*3f2f09c1Sdp sizeof (zarg.bootbuf)) >= sizeof (zarg.bootbuf)) { 1350*3f2f09c1Sdp zerror(gettext("Boot argument list too long")); 1351*3f2f09c1Sdp return (Z_ERR); 13527c478bd9Sstevel@tonic-gate } 1353*3f2f09c1Sdp if (optind < argc - 1) 1354*3f2f09c1Sdp if (strlcat(zarg.bootbuf, " ", sizeof (zarg.bootbuf)) >= 1355*3f2f09c1Sdp sizeof (zarg.bootbuf)) { 1356*3f2f09c1Sdp zerror(gettext("Boot argument list too long")); 1357*3f2f09c1Sdp return (Z_ERR); 1358*3f2f09c1Sdp } 1359*3f2f09c1Sdp } 1360*3f2f09c1Sdp 13617c478bd9Sstevel@tonic-gate if (sanity_check(target_zone, CMD_BOOT, B_FALSE, B_FALSE) != Z_OK) 13627c478bd9Sstevel@tonic-gate return (Z_ERR); 13637c478bd9Sstevel@tonic-gate if (verify_details(CMD_BOOT) != Z_OK) 13647c478bd9Sstevel@tonic-gate return (Z_ERR); 13657c478bd9Sstevel@tonic-gate zarg.cmd = Z_BOOT; 13667c478bd9Sstevel@tonic-gate if (call_zoneadmd(target_zone, &zarg) != 0) { 13677c478bd9Sstevel@tonic-gate zerror(gettext("call to %s failed"), "zoneadmd"); 13687c478bd9Sstevel@tonic-gate return (Z_ERR); 13697c478bd9Sstevel@tonic-gate } 13707c478bd9Sstevel@tonic-gate return (Z_OK); 13717c478bd9Sstevel@tonic-gate } 13727c478bd9Sstevel@tonic-gate 13737c478bd9Sstevel@tonic-gate static void 13747c478bd9Sstevel@tonic-gate fake_up_local_zone(zoneid_t zid, zone_entry_t *zeptr) 13757c478bd9Sstevel@tonic-gate { 13767c478bd9Sstevel@tonic-gate ssize_t result; 13777c478bd9Sstevel@tonic-gate 13787c478bd9Sstevel@tonic-gate zeptr->zid = zid; 13797c478bd9Sstevel@tonic-gate /* 13807c478bd9Sstevel@tonic-gate * Since we're looking up our own (non-global) zone name, 13817c478bd9Sstevel@tonic-gate * we can be assured that it will succeed. 13827c478bd9Sstevel@tonic-gate */ 13837c478bd9Sstevel@tonic-gate result = getzonenamebyid(zid, zeptr->zname, sizeof (zeptr->zname)); 13847c478bd9Sstevel@tonic-gate assert(result >= 0); 138545916cd2Sjpk if (!is_system_labeled()) { 13867c478bd9Sstevel@tonic-gate (void) strlcpy(zeptr->zroot, "/", sizeof (zeptr->zroot)); 138745916cd2Sjpk } else { 138845916cd2Sjpk (void) zone_getattr(zid, ZONE_ATTR_ROOT, zeptr->zroot, 138945916cd2Sjpk sizeof (zeptr->zroot)); 139045916cd2Sjpk } 13917c478bd9Sstevel@tonic-gate zeptr->zstate_str = "running"; 13927c478bd9Sstevel@tonic-gate } 13937c478bd9Sstevel@tonic-gate 13947c478bd9Sstevel@tonic-gate static int 13957c478bd9Sstevel@tonic-gate list_func(int argc, char *argv[]) 13967c478bd9Sstevel@tonic-gate { 13977c478bd9Sstevel@tonic-gate zone_entry_t *zentp, zent; 1398108322fbScarlsonj int arg, retv; 13997c478bd9Sstevel@tonic-gate boolean_t output = B_FALSE, verbose = B_FALSE, parsable = B_FALSE; 14007c478bd9Sstevel@tonic-gate zone_state_t min_state = ZONE_STATE_RUNNING; 14017c478bd9Sstevel@tonic-gate zoneid_t zone_id = getzoneid(); 14027c478bd9Sstevel@tonic-gate 14037c478bd9Sstevel@tonic-gate if (target_zone == NULL) { 14047c478bd9Sstevel@tonic-gate /* all zones: default view to running but allow override */ 14057c478bd9Sstevel@tonic-gate optind = 0; 14067c478bd9Sstevel@tonic-gate while ((arg = getopt(argc, argv, "?cipv")) != EOF) { 14077c478bd9Sstevel@tonic-gate switch (arg) { 14087c478bd9Sstevel@tonic-gate case '?': 14097c478bd9Sstevel@tonic-gate sub_usage(SHELP_LIST, CMD_LIST); 14107c478bd9Sstevel@tonic-gate return (optopt == '?' ? Z_OK : Z_USAGE); 1411475d78a4Sjonb /* 1412475d78a4Sjonb * The 'i' and 'c' options are not mutually 1413475d78a4Sjonb * exclusive so if 'c' is given, then min_state 1414475d78a4Sjonb * is set to 0 (ZONE_STATE_CONFIGURED) which is 1415475d78a4Sjonb * the lowest possible state. If 'i' is given, 1416475d78a4Sjonb * then min_state is set to be the lowest state 1417475d78a4Sjonb * so far. 1418475d78a4Sjonb */ 14197c478bd9Sstevel@tonic-gate case 'c': 14207c478bd9Sstevel@tonic-gate min_state = ZONE_STATE_CONFIGURED; 14217c478bd9Sstevel@tonic-gate break; 14227c478bd9Sstevel@tonic-gate case 'i': 1423475d78a4Sjonb min_state = min(ZONE_STATE_INSTALLED, 1424475d78a4Sjonb min_state); 1425475d78a4Sjonb 14267c478bd9Sstevel@tonic-gate break; 14277c478bd9Sstevel@tonic-gate case 'p': 14287c478bd9Sstevel@tonic-gate parsable = B_TRUE; 14297c478bd9Sstevel@tonic-gate break; 14307c478bd9Sstevel@tonic-gate case 'v': 14317c478bd9Sstevel@tonic-gate verbose = B_TRUE; 14327c478bd9Sstevel@tonic-gate break; 14337c478bd9Sstevel@tonic-gate default: 14347c478bd9Sstevel@tonic-gate sub_usage(SHELP_LIST, CMD_LIST); 14357c478bd9Sstevel@tonic-gate return (Z_USAGE); 14367c478bd9Sstevel@tonic-gate } 14377c478bd9Sstevel@tonic-gate } 14387c478bd9Sstevel@tonic-gate if (parsable && verbose) { 14397c478bd9Sstevel@tonic-gate zerror(gettext("%s -p and -v are mutually exclusive."), 14407c478bd9Sstevel@tonic-gate cmd_to_str(CMD_LIST)); 14417c478bd9Sstevel@tonic-gate return (Z_ERR); 14427c478bd9Sstevel@tonic-gate } 144345916cd2Sjpk if (zone_id == GLOBAL_ZONEID || is_system_labeled()) { 1444108322fbScarlsonj retv = zone_print_list(min_state, verbose, parsable); 14457c478bd9Sstevel@tonic-gate } else { 1446108322fbScarlsonj retv = Z_OK; 14477c478bd9Sstevel@tonic-gate fake_up_local_zone(zone_id, &zent); 14487c478bd9Sstevel@tonic-gate zone_print(&zent, verbose, parsable); 14497c478bd9Sstevel@tonic-gate } 1450108322fbScarlsonj return (retv); 14517c478bd9Sstevel@tonic-gate } 14527c478bd9Sstevel@tonic-gate 14537c478bd9Sstevel@tonic-gate /* 14547c478bd9Sstevel@tonic-gate * Specific target zone: disallow -i/-c suboptions. 14557c478bd9Sstevel@tonic-gate */ 14567c478bd9Sstevel@tonic-gate optind = 0; 14577c478bd9Sstevel@tonic-gate while ((arg = getopt(argc, argv, "?pv")) != EOF) { 14587c478bd9Sstevel@tonic-gate switch (arg) { 14597c478bd9Sstevel@tonic-gate case '?': 14607c478bd9Sstevel@tonic-gate sub_usage(SHELP_LIST, CMD_LIST); 14617c478bd9Sstevel@tonic-gate return (optopt == '?' ? Z_OK : Z_USAGE); 14627c478bd9Sstevel@tonic-gate case 'p': 14637c478bd9Sstevel@tonic-gate parsable = B_TRUE; 14647c478bd9Sstevel@tonic-gate break; 14657c478bd9Sstevel@tonic-gate case 'v': 14667c478bd9Sstevel@tonic-gate verbose = B_TRUE; 14677c478bd9Sstevel@tonic-gate break; 14687c478bd9Sstevel@tonic-gate default: 14697c478bd9Sstevel@tonic-gate sub_usage(SHELP_LIST, CMD_LIST); 14707c478bd9Sstevel@tonic-gate return (Z_USAGE); 14717c478bd9Sstevel@tonic-gate } 14727c478bd9Sstevel@tonic-gate } 14737c478bd9Sstevel@tonic-gate if (parsable && verbose) { 14747c478bd9Sstevel@tonic-gate zerror(gettext("%s -p and -v are mutually exclusive."), 14757c478bd9Sstevel@tonic-gate cmd_to_str(CMD_LIST)); 14767c478bd9Sstevel@tonic-gate return (Z_ERR); 14777c478bd9Sstevel@tonic-gate } 14787c478bd9Sstevel@tonic-gate if (argc > optind) { 14797c478bd9Sstevel@tonic-gate sub_usage(SHELP_LIST, CMD_LIST); 14807c478bd9Sstevel@tonic-gate return (Z_USAGE); 14817c478bd9Sstevel@tonic-gate } 14827c478bd9Sstevel@tonic-gate if (zone_id != GLOBAL_ZONEID) { 14837c478bd9Sstevel@tonic-gate fake_up_local_zone(zone_id, &zent); 14847c478bd9Sstevel@tonic-gate /* 14857c478bd9Sstevel@tonic-gate * main() will issue a Z_NO_ZONE error if it cannot get an 14867c478bd9Sstevel@tonic-gate * id for target_zone, which in a non-global zone should 14877c478bd9Sstevel@tonic-gate * happen for any zone name except `zonename`. Thus we 14887c478bd9Sstevel@tonic-gate * assert() that here but don't otherwise check. 14897c478bd9Sstevel@tonic-gate */ 14907c478bd9Sstevel@tonic-gate assert(strcmp(zent.zname, target_zone) == 0); 14917c478bd9Sstevel@tonic-gate zone_print(&zent, verbose, parsable); 14927c478bd9Sstevel@tonic-gate output = B_TRUE; 14937c478bd9Sstevel@tonic-gate } else if ((zentp = lookup_running_zone(target_zone)) != NULL) { 14947c478bd9Sstevel@tonic-gate zone_print(zentp, verbose, parsable); 14957c478bd9Sstevel@tonic-gate output = B_TRUE; 1496108322fbScarlsonj } else if (lookup_zone_info(target_zone, ZONE_ID_UNDEFINED, 1497108322fbScarlsonj &zent) == Z_OK) { 14987c478bd9Sstevel@tonic-gate zone_print(&zent, verbose, parsable); 14997c478bd9Sstevel@tonic-gate output = B_TRUE; 15007c478bd9Sstevel@tonic-gate } 15017c478bd9Sstevel@tonic-gate return (output ? Z_OK : Z_ERR); 15027c478bd9Sstevel@tonic-gate } 15037c478bd9Sstevel@tonic-gate 15047c478bd9Sstevel@tonic-gate static void 15057c478bd9Sstevel@tonic-gate sigterm(int sig) 15067c478bd9Sstevel@tonic-gate { 15077c478bd9Sstevel@tonic-gate /* 15087c478bd9Sstevel@tonic-gate * Ignore SIG{INT,TERM}, so we don't end up in an infinite loop, 15097c478bd9Sstevel@tonic-gate * then propagate the signal to our process group. 15107c478bd9Sstevel@tonic-gate */ 15117c478bd9Sstevel@tonic-gate (void) sigset(SIGINT, SIG_IGN); 15127c478bd9Sstevel@tonic-gate (void) sigset(SIGTERM, SIG_IGN); 15137c478bd9Sstevel@tonic-gate (void) kill(0, sig); 15147c478bd9Sstevel@tonic-gate child_killed = B_TRUE; 15157c478bd9Sstevel@tonic-gate } 15167c478bd9Sstevel@tonic-gate 15177c478bd9Sstevel@tonic-gate static int 15187c478bd9Sstevel@tonic-gate do_subproc(char *cmdbuf) 15197c478bd9Sstevel@tonic-gate { 15207c478bd9Sstevel@tonic-gate char inbuf[1024]; /* arbitrary large amount */ 15217c478bd9Sstevel@tonic-gate FILE *file; 15227c478bd9Sstevel@tonic-gate 15237c478bd9Sstevel@tonic-gate child_killed = B_FALSE; 15247c478bd9Sstevel@tonic-gate /* 15257c478bd9Sstevel@tonic-gate * We use popen(3c) to launch child processes for [un]install; 15267c478bd9Sstevel@tonic-gate * this library call does not return a PID, so we have to kill 15277c478bd9Sstevel@tonic-gate * the whole process group. To avoid killing our parent, we 15287c478bd9Sstevel@tonic-gate * become a process group leader here. But doing so can wreak 15297c478bd9Sstevel@tonic-gate * havoc with reading from stdin when launched by a non-job-control 15307c478bd9Sstevel@tonic-gate * shell, so we close stdin and reopen it as /dev/null first. 15317c478bd9Sstevel@tonic-gate */ 15327c478bd9Sstevel@tonic-gate (void) close(STDIN_FILENO); 15337c478bd9Sstevel@tonic-gate (void) open("/dev/null", O_RDONLY); 15347c478bd9Sstevel@tonic-gate (void) setpgid(0, 0); 15357c478bd9Sstevel@tonic-gate (void) sigset(SIGINT, sigterm); 15367c478bd9Sstevel@tonic-gate (void) sigset(SIGTERM, sigterm); 15377c478bd9Sstevel@tonic-gate file = popen(cmdbuf, "r"); 15387c478bd9Sstevel@tonic-gate for (;;) { 15397c478bd9Sstevel@tonic-gate if (child_killed || fgets(inbuf, sizeof (inbuf), file) == NULL) 15407c478bd9Sstevel@tonic-gate break; 15417c478bd9Sstevel@tonic-gate (void) fputs(inbuf, stdout); 15427c478bd9Sstevel@tonic-gate } 15437c478bd9Sstevel@tonic-gate (void) sigset(SIGINT, SIG_DFL); 15447c478bd9Sstevel@tonic-gate (void) sigset(SIGTERM, SIG_DFL); 15457c478bd9Sstevel@tonic-gate return (pclose(file)); 15467c478bd9Sstevel@tonic-gate } 15477c478bd9Sstevel@tonic-gate 15487c478bd9Sstevel@tonic-gate static int 15497c478bd9Sstevel@tonic-gate subproc_status(const char *cmd, int status) 15507c478bd9Sstevel@tonic-gate { 15517c478bd9Sstevel@tonic-gate if (WIFEXITED(status)) { 15527c478bd9Sstevel@tonic-gate int exit_code = WEXITSTATUS(status); 15537c478bd9Sstevel@tonic-gate 15547c478bd9Sstevel@tonic-gate if (exit_code == 0) 15557c478bd9Sstevel@tonic-gate return (Z_OK); 15567c478bd9Sstevel@tonic-gate zerror(gettext("'%s' failed with exit code %d."), cmd, 15577c478bd9Sstevel@tonic-gate exit_code); 15587c478bd9Sstevel@tonic-gate } else if (WIFSIGNALED(status)) { 15597c478bd9Sstevel@tonic-gate int signal = WTERMSIG(status); 15607c478bd9Sstevel@tonic-gate char sigstr[SIG2STR_MAX]; 15617c478bd9Sstevel@tonic-gate 15627c478bd9Sstevel@tonic-gate if (sig2str(signal, sigstr) == 0) { 15637c478bd9Sstevel@tonic-gate zerror(gettext("'%s' terminated by signal SIG%s."), cmd, 15647c478bd9Sstevel@tonic-gate sigstr); 15657c478bd9Sstevel@tonic-gate } else { 15667c478bd9Sstevel@tonic-gate zerror(gettext("'%s' terminated by an unknown signal."), 15677c478bd9Sstevel@tonic-gate cmd); 15687c478bd9Sstevel@tonic-gate } 15697c478bd9Sstevel@tonic-gate } else { 15707c478bd9Sstevel@tonic-gate zerror(gettext("'%s' failed for unknown reasons."), cmd); 15717c478bd9Sstevel@tonic-gate } 15727c478bd9Sstevel@tonic-gate return (Z_ERR); 15737c478bd9Sstevel@tonic-gate } 15747c478bd9Sstevel@tonic-gate 15757c478bd9Sstevel@tonic-gate /* 15767c478bd9Sstevel@tonic-gate * Various sanity checks; make sure: 15777c478bd9Sstevel@tonic-gate * 1. We're in the global zone. 15787c478bd9Sstevel@tonic-gate * 2. The calling user has sufficient privilege. 15797c478bd9Sstevel@tonic-gate * 3. The target zone is neither the global zone nor anything starting with 15807c478bd9Sstevel@tonic-gate * "SUNW". 15817c478bd9Sstevel@tonic-gate * 4a. If we're looking for a 'not running' (i.e., configured or installed) 15827c478bd9Sstevel@tonic-gate * zone, the name service knows about it. 15837c478bd9Sstevel@tonic-gate * 4b. For some operations which expect a zone not to be running, that it is 15847c478bd9Sstevel@tonic-gate * not already running (or ready). 15857c478bd9Sstevel@tonic-gate */ 15867c478bd9Sstevel@tonic-gate static int 15877c478bd9Sstevel@tonic-gate sanity_check(char *zone, int cmd_num, boolean_t running, 15887c478bd9Sstevel@tonic-gate boolean_t unsafe_when_running) 15897c478bd9Sstevel@tonic-gate { 15907c478bd9Sstevel@tonic-gate zone_entry_t *zent; 15917c478bd9Sstevel@tonic-gate priv_set_t *privset; 15927c478bd9Sstevel@tonic-gate zone_state_t state; 1593108322fbScarlsonj char kernzone[ZONENAME_MAX]; 1594108322fbScarlsonj FILE *fp; 15957c478bd9Sstevel@tonic-gate 15967c478bd9Sstevel@tonic-gate if (getzoneid() != GLOBAL_ZONEID) { 1597ffbafc53Scomay switch (cmd_num) { 1598ffbafc53Scomay case CMD_HALT: 1599ffbafc53Scomay zerror(gettext("use %s to %s this zone."), "halt(1M)", 16007c478bd9Sstevel@tonic-gate cmd_to_str(cmd_num)); 1601ffbafc53Scomay break; 1602ffbafc53Scomay case CMD_REBOOT: 1603ffbafc53Scomay zerror(gettext("use %s to %s this zone."), 1604ffbafc53Scomay "reboot(1M)", cmd_to_str(cmd_num)); 1605ffbafc53Scomay break; 1606ffbafc53Scomay default: 1607ffbafc53Scomay zerror(gettext("must be in the global zone to %s a " 1608ffbafc53Scomay "zone."), cmd_to_str(cmd_num)); 1609ffbafc53Scomay break; 1610ffbafc53Scomay } 16117c478bd9Sstevel@tonic-gate return (Z_ERR); 16127c478bd9Sstevel@tonic-gate } 16137c478bd9Sstevel@tonic-gate 16147c478bd9Sstevel@tonic-gate if ((privset = priv_allocset()) == NULL) { 16157c478bd9Sstevel@tonic-gate zerror(gettext("%s failed"), "priv_allocset"); 16167c478bd9Sstevel@tonic-gate return (Z_ERR); 16177c478bd9Sstevel@tonic-gate } 16187c478bd9Sstevel@tonic-gate 16197c478bd9Sstevel@tonic-gate if (getppriv(PRIV_EFFECTIVE, privset) != 0) { 16207c478bd9Sstevel@tonic-gate zerror(gettext("%s failed"), "getppriv"); 16217c478bd9Sstevel@tonic-gate priv_freeset(privset); 16227c478bd9Sstevel@tonic-gate return (Z_ERR); 16237c478bd9Sstevel@tonic-gate } 16247c478bd9Sstevel@tonic-gate 16257c478bd9Sstevel@tonic-gate if (priv_isfullset(privset) == B_FALSE) { 16267c478bd9Sstevel@tonic-gate zerror(gettext("only a privileged user may %s a zone."), 16277c478bd9Sstevel@tonic-gate cmd_to_str(cmd_num)); 16287c478bd9Sstevel@tonic-gate priv_freeset(privset); 16297c478bd9Sstevel@tonic-gate return (Z_ERR); 16307c478bd9Sstevel@tonic-gate } 16317c478bd9Sstevel@tonic-gate priv_freeset(privset); 16327c478bd9Sstevel@tonic-gate 16337c478bd9Sstevel@tonic-gate if (zone == NULL) { 16347c478bd9Sstevel@tonic-gate zerror(gettext("no zone specified")); 16357c478bd9Sstevel@tonic-gate return (Z_ERR); 16367c478bd9Sstevel@tonic-gate } 16377c478bd9Sstevel@tonic-gate 16387c478bd9Sstevel@tonic-gate if (strcmp(zone, GLOBAL_ZONENAME) == 0) { 16397c478bd9Sstevel@tonic-gate zerror(gettext("%s operation is invalid for the global zone."), 16407c478bd9Sstevel@tonic-gate cmd_to_str(cmd_num)); 16417c478bd9Sstevel@tonic-gate return (Z_ERR); 16427c478bd9Sstevel@tonic-gate } 16437c478bd9Sstevel@tonic-gate 16447c478bd9Sstevel@tonic-gate if (strncmp(zone, "SUNW", 4) == 0) { 16457c478bd9Sstevel@tonic-gate zerror(gettext("%s operation is invalid for zones starting " 16467c478bd9Sstevel@tonic-gate "with SUNW."), cmd_to_str(cmd_num)); 16477c478bd9Sstevel@tonic-gate return (Z_ERR); 16487c478bd9Sstevel@tonic-gate } 16497c478bd9Sstevel@tonic-gate 1650108322fbScarlsonj if (!zonecfg_in_alt_root()) { 1651108322fbScarlsonj zent = lookup_running_zone(zone); 1652108322fbScarlsonj } else if ((fp = zonecfg_open_scratch("", B_FALSE)) == NULL) { 1653108322fbScarlsonj zent = NULL; 1654108322fbScarlsonj } else { 1655108322fbScarlsonj if (zonecfg_find_scratch(fp, zone, zonecfg_get_root(), 1656108322fbScarlsonj kernzone, sizeof (kernzone)) == 0) 1657108322fbScarlsonj zent = lookup_running_zone(kernzone); 1658108322fbScarlsonj else 1659108322fbScarlsonj zent = NULL; 1660108322fbScarlsonj zonecfg_close_scratch(fp); 1661108322fbScarlsonj } 1662108322fbScarlsonj 16637c478bd9Sstevel@tonic-gate /* 16647c478bd9Sstevel@tonic-gate * Look up from the kernel for 'running' zones. 16657c478bd9Sstevel@tonic-gate */ 16667c478bd9Sstevel@tonic-gate if (running) { 16677c478bd9Sstevel@tonic-gate if (zent == NULL) { 16687c478bd9Sstevel@tonic-gate zerror(gettext("not running")); 16697c478bd9Sstevel@tonic-gate return (Z_ERR); 16707c478bd9Sstevel@tonic-gate } 16717c478bd9Sstevel@tonic-gate } else { 16727c478bd9Sstevel@tonic-gate int err; 16737c478bd9Sstevel@tonic-gate 16747c478bd9Sstevel@tonic-gate if (unsafe_when_running && zent != NULL) { 16757c478bd9Sstevel@tonic-gate /* check whether the zone is ready or running */ 16767c478bd9Sstevel@tonic-gate if ((err = zone_get_state(zent->zname, 16777c478bd9Sstevel@tonic-gate &zent->zstate_num)) != Z_OK) { 16787c478bd9Sstevel@tonic-gate errno = err; 16797c478bd9Sstevel@tonic-gate zperror2(zent->zname, 16807c478bd9Sstevel@tonic-gate gettext("could not get state")); 16817c478bd9Sstevel@tonic-gate /* can't tell, so hedge */ 16827c478bd9Sstevel@tonic-gate zent->zstate_str = "ready/running"; 16837c478bd9Sstevel@tonic-gate } else { 16847c478bd9Sstevel@tonic-gate zent->zstate_str = 16857c478bd9Sstevel@tonic-gate zone_state_str(zent->zstate_num); 16867c478bd9Sstevel@tonic-gate } 16877c478bd9Sstevel@tonic-gate zerror(gettext("%s operation is invalid for %s zones."), 16887c478bd9Sstevel@tonic-gate cmd_to_str(cmd_num), zent->zstate_str); 16897c478bd9Sstevel@tonic-gate return (Z_ERR); 16907c478bd9Sstevel@tonic-gate } 16917c478bd9Sstevel@tonic-gate if ((err = zone_get_state(zone, &state)) != Z_OK) { 16927c478bd9Sstevel@tonic-gate errno = err; 16937c478bd9Sstevel@tonic-gate zperror2(zone, gettext("could not get state")); 16947c478bd9Sstevel@tonic-gate return (Z_ERR); 16957c478bd9Sstevel@tonic-gate } 16967c478bd9Sstevel@tonic-gate switch (cmd_num) { 16977c478bd9Sstevel@tonic-gate case CMD_UNINSTALL: 16987c478bd9Sstevel@tonic-gate if (state == ZONE_STATE_CONFIGURED) { 16997c478bd9Sstevel@tonic-gate zerror(gettext("is already in state '%s'."), 17007c478bd9Sstevel@tonic-gate zone_state_str(ZONE_STATE_CONFIGURED)); 17017c478bd9Sstevel@tonic-gate return (Z_ERR); 17027c478bd9Sstevel@tonic-gate } 17037c478bd9Sstevel@tonic-gate break; 1704ee519a1fSgjelinek case CMD_ATTACH: 1705865e09a4Sgjelinek case CMD_CLONE: 17067c478bd9Sstevel@tonic-gate case CMD_INSTALL: 17077c478bd9Sstevel@tonic-gate if (state == ZONE_STATE_INSTALLED) { 17087c478bd9Sstevel@tonic-gate zerror(gettext("is already %s."), 17097c478bd9Sstevel@tonic-gate zone_state_str(ZONE_STATE_INSTALLED)); 17107c478bd9Sstevel@tonic-gate return (Z_ERR); 17117c478bd9Sstevel@tonic-gate } else if (state == ZONE_STATE_INCOMPLETE) { 17127c478bd9Sstevel@tonic-gate zerror(gettext("zone is %s; %s required."), 17137c478bd9Sstevel@tonic-gate zone_state_str(ZONE_STATE_INCOMPLETE), 17147c478bd9Sstevel@tonic-gate cmd_to_str(CMD_UNINSTALL)); 17157c478bd9Sstevel@tonic-gate return (Z_ERR); 17167c478bd9Sstevel@tonic-gate } 17177c478bd9Sstevel@tonic-gate break; 1718ee519a1fSgjelinek case CMD_DETACH: 1719865e09a4Sgjelinek case CMD_MOVE: 17207c478bd9Sstevel@tonic-gate case CMD_READY: 17217c478bd9Sstevel@tonic-gate case CMD_BOOT: 1722108322fbScarlsonj case CMD_MOUNT: 17237c478bd9Sstevel@tonic-gate if (state < ZONE_STATE_INSTALLED) { 17247c478bd9Sstevel@tonic-gate zerror(gettext("must be %s before %s."), 17257c478bd9Sstevel@tonic-gate zone_state_str(ZONE_STATE_INSTALLED), 17267c478bd9Sstevel@tonic-gate cmd_to_str(cmd_num)); 17277c478bd9Sstevel@tonic-gate return (Z_ERR); 17287c478bd9Sstevel@tonic-gate } 17297c478bd9Sstevel@tonic-gate break; 17307c478bd9Sstevel@tonic-gate case CMD_VERIFY: 17317c478bd9Sstevel@tonic-gate if (state == ZONE_STATE_INCOMPLETE) { 17327c478bd9Sstevel@tonic-gate zerror(gettext("zone is %s; %s required."), 17337c478bd9Sstevel@tonic-gate zone_state_str(ZONE_STATE_INCOMPLETE), 17347c478bd9Sstevel@tonic-gate cmd_to_str(CMD_UNINSTALL)); 17357c478bd9Sstevel@tonic-gate return (Z_ERR); 17367c478bd9Sstevel@tonic-gate } 17377c478bd9Sstevel@tonic-gate break; 1738108322fbScarlsonj case CMD_UNMOUNT: 1739108322fbScarlsonj if (state != ZONE_STATE_MOUNTED) { 1740108322fbScarlsonj zerror(gettext("must be %s before %s."), 1741108322fbScarlsonj zone_state_str(ZONE_STATE_MOUNTED), 1742108322fbScarlsonj cmd_to_str(cmd_num)); 1743108322fbScarlsonj return (Z_ERR); 1744108322fbScarlsonj } 1745108322fbScarlsonj break; 17467c478bd9Sstevel@tonic-gate } 17477c478bd9Sstevel@tonic-gate } 17487c478bd9Sstevel@tonic-gate return (Z_OK); 17497c478bd9Sstevel@tonic-gate } 17507c478bd9Sstevel@tonic-gate 17517c478bd9Sstevel@tonic-gate static int 17527c478bd9Sstevel@tonic-gate halt_func(int argc, char *argv[]) 17537c478bd9Sstevel@tonic-gate { 17547c478bd9Sstevel@tonic-gate zone_cmd_arg_t zarg; 17557c478bd9Sstevel@tonic-gate int arg; 17567c478bd9Sstevel@tonic-gate 1757108322fbScarlsonj if (zonecfg_in_alt_root()) { 1758108322fbScarlsonj zerror(gettext("cannot halt zone in alternate root")); 1759108322fbScarlsonj return (Z_ERR); 1760108322fbScarlsonj } 1761108322fbScarlsonj 17627c478bd9Sstevel@tonic-gate optind = 0; 17637c478bd9Sstevel@tonic-gate if ((arg = getopt(argc, argv, "?")) != EOF) { 17647c478bd9Sstevel@tonic-gate switch (arg) { 17657c478bd9Sstevel@tonic-gate case '?': 17667c478bd9Sstevel@tonic-gate sub_usage(SHELP_HALT, CMD_HALT); 17677c478bd9Sstevel@tonic-gate return (optopt == '?' ? Z_OK : Z_USAGE); 17687c478bd9Sstevel@tonic-gate default: 17697c478bd9Sstevel@tonic-gate sub_usage(SHELP_HALT, CMD_HALT); 17707c478bd9Sstevel@tonic-gate return (Z_USAGE); 17717c478bd9Sstevel@tonic-gate } 17727c478bd9Sstevel@tonic-gate } 17737c478bd9Sstevel@tonic-gate if (argc > optind) { 17747c478bd9Sstevel@tonic-gate sub_usage(SHELP_HALT, CMD_HALT); 17757c478bd9Sstevel@tonic-gate return (Z_USAGE); 17767c478bd9Sstevel@tonic-gate } 17777c478bd9Sstevel@tonic-gate /* 17787c478bd9Sstevel@tonic-gate * zoneadmd should be the one to decide whether or not to proceed, 17797c478bd9Sstevel@tonic-gate * so even though it seems that the fourth parameter below should 17807c478bd9Sstevel@tonic-gate * perhaps be B_TRUE, it really shouldn't be. 17817c478bd9Sstevel@tonic-gate */ 17827c478bd9Sstevel@tonic-gate if (sanity_check(target_zone, CMD_HALT, B_FALSE, B_FALSE) != Z_OK) 17837c478bd9Sstevel@tonic-gate return (Z_ERR); 17847c478bd9Sstevel@tonic-gate 17857c478bd9Sstevel@tonic-gate zarg.cmd = Z_HALT; 17867c478bd9Sstevel@tonic-gate return ((call_zoneadmd(target_zone, &zarg) == 0) ? Z_OK : Z_ERR); 17877c478bd9Sstevel@tonic-gate } 17887c478bd9Sstevel@tonic-gate 17897c478bd9Sstevel@tonic-gate static int 17907c478bd9Sstevel@tonic-gate reboot_func(int argc, char *argv[]) 17917c478bd9Sstevel@tonic-gate { 17927c478bd9Sstevel@tonic-gate zone_cmd_arg_t zarg; 17937c478bd9Sstevel@tonic-gate int arg; 17947c478bd9Sstevel@tonic-gate 1795108322fbScarlsonj if (zonecfg_in_alt_root()) { 1796108322fbScarlsonj zerror(gettext("cannot reboot zone in alternate root")); 1797108322fbScarlsonj return (Z_ERR); 1798108322fbScarlsonj } 1799108322fbScarlsonj 18007c478bd9Sstevel@tonic-gate optind = 0; 18017c478bd9Sstevel@tonic-gate if ((arg = getopt(argc, argv, "?")) != EOF) { 18027c478bd9Sstevel@tonic-gate switch (arg) { 18037c478bd9Sstevel@tonic-gate case '?': 18047c478bd9Sstevel@tonic-gate sub_usage(SHELP_REBOOT, CMD_REBOOT); 18057c478bd9Sstevel@tonic-gate return (optopt == '?' ? Z_OK : Z_USAGE); 18067c478bd9Sstevel@tonic-gate default: 18077c478bd9Sstevel@tonic-gate sub_usage(SHELP_REBOOT, CMD_REBOOT); 18087c478bd9Sstevel@tonic-gate return (Z_USAGE); 18097c478bd9Sstevel@tonic-gate } 18107c478bd9Sstevel@tonic-gate } 1811*3f2f09c1Sdp 1812*3f2f09c1Sdp zarg.bootbuf[0] = '\0'; 1813*3f2f09c1Sdp for (; optind < argc; optind++) { 1814*3f2f09c1Sdp if (strlcat(zarg.bootbuf, argv[optind], 1815*3f2f09c1Sdp sizeof (zarg.bootbuf)) >= sizeof (zarg.bootbuf)) { 1816*3f2f09c1Sdp zerror(gettext("Boot argument list too long")); 1817*3f2f09c1Sdp return (Z_ERR); 18187c478bd9Sstevel@tonic-gate } 1819*3f2f09c1Sdp if (optind < argc - 1) 1820*3f2f09c1Sdp if (strlcat(zarg.bootbuf, " ", sizeof (zarg.bootbuf)) >= 1821*3f2f09c1Sdp sizeof (zarg.bootbuf)) { 1822*3f2f09c1Sdp zerror(gettext("Boot argument list too long")); 1823*3f2f09c1Sdp return (Z_ERR); 1824*3f2f09c1Sdp } 1825*3f2f09c1Sdp } 1826*3f2f09c1Sdp 1827*3f2f09c1Sdp 18287c478bd9Sstevel@tonic-gate /* 18297c478bd9Sstevel@tonic-gate * zoneadmd should be the one to decide whether or not to proceed, 18307c478bd9Sstevel@tonic-gate * so even though it seems that the fourth parameter below should 18317c478bd9Sstevel@tonic-gate * perhaps be B_TRUE, it really shouldn't be. 18327c478bd9Sstevel@tonic-gate */ 18337c478bd9Sstevel@tonic-gate if (sanity_check(target_zone, CMD_REBOOT, B_TRUE, B_FALSE) != Z_OK) 18347c478bd9Sstevel@tonic-gate return (Z_ERR); 1835b5abaf04Sgjelinek if (verify_details(CMD_REBOOT) != Z_OK) 1836b5abaf04Sgjelinek return (Z_ERR); 18377c478bd9Sstevel@tonic-gate 18387c478bd9Sstevel@tonic-gate zarg.cmd = Z_REBOOT; 18397c478bd9Sstevel@tonic-gate return ((call_zoneadmd(target_zone, &zarg) == 0) ? Z_OK : Z_ERR); 18407c478bd9Sstevel@tonic-gate } 18417c478bd9Sstevel@tonic-gate 18427c478bd9Sstevel@tonic-gate static int 18437c478bd9Sstevel@tonic-gate verify_rctls(zone_dochandle_t handle) 18447c478bd9Sstevel@tonic-gate { 18457c478bd9Sstevel@tonic-gate struct zone_rctltab rctltab; 18467c478bd9Sstevel@tonic-gate size_t rbs = rctlblk_size(); 18477c478bd9Sstevel@tonic-gate rctlblk_t *rctlblk; 18487c478bd9Sstevel@tonic-gate int error = Z_INVAL; 18497c478bd9Sstevel@tonic-gate 18507c478bd9Sstevel@tonic-gate if ((rctlblk = malloc(rbs)) == NULL) { 18517c478bd9Sstevel@tonic-gate zerror(gettext("failed to allocate %lu bytes: %s"), rbs, 18527c478bd9Sstevel@tonic-gate strerror(errno)); 18537c478bd9Sstevel@tonic-gate return (Z_NOMEM); 18547c478bd9Sstevel@tonic-gate } 18557c478bd9Sstevel@tonic-gate 18567c478bd9Sstevel@tonic-gate if (zonecfg_setrctlent(handle) != Z_OK) { 18577c478bd9Sstevel@tonic-gate zerror(gettext("zonecfg_setrctlent failed")); 18587c478bd9Sstevel@tonic-gate free(rctlblk); 18597c478bd9Sstevel@tonic-gate return (error); 18607c478bd9Sstevel@tonic-gate } 18617c478bd9Sstevel@tonic-gate 18627c478bd9Sstevel@tonic-gate rctltab.zone_rctl_valptr = NULL; 18637c478bd9Sstevel@tonic-gate while (zonecfg_getrctlent(handle, &rctltab) == Z_OK) { 18647c478bd9Sstevel@tonic-gate struct zone_rctlvaltab *rctlval; 18657c478bd9Sstevel@tonic-gate const char *name = rctltab.zone_rctl_name; 18667c478bd9Sstevel@tonic-gate 18677c478bd9Sstevel@tonic-gate if (!zonecfg_is_rctl(name)) { 18687c478bd9Sstevel@tonic-gate zerror(gettext("WARNING: Ignoring unrecognized rctl " 18697c478bd9Sstevel@tonic-gate "'%s'."), name); 18707c478bd9Sstevel@tonic-gate zonecfg_free_rctl_value_list(rctltab.zone_rctl_valptr); 18717c478bd9Sstevel@tonic-gate rctltab.zone_rctl_valptr = NULL; 18727c478bd9Sstevel@tonic-gate continue; 18737c478bd9Sstevel@tonic-gate } 18747c478bd9Sstevel@tonic-gate 18757c478bd9Sstevel@tonic-gate for (rctlval = rctltab.zone_rctl_valptr; rctlval != NULL; 18767c478bd9Sstevel@tonic-gate rctlval = rctlval->zone_rctlval_next) { 18777c478bd9Sstevel@tonic-gate if (zonecfg_construct_rctlblk(rctlval, rctlblk) 18787c478bd9Sstevel@tonic-gate != Z_OK) { 18797c478bd9Sstevel@tonic-gate zerror(gettext("invalid rctl value: " 18807c478bd9Sstevel@tonic-gate "(priv=%s,limit=%s,action%s)"), 18817c478bd9Sstevel@tonic-gate rctlval->zone_rctlval_priv, 18827c478bd9Sstevel@tonic-gate rctlval->zone_rctlval_limit, 18837c478bd9Sstevel@tonic-gate rctlval->zone_rctlval_action); 18847c478bd9Sstevel@tonic-gate goto out; 18857c478bd9Sstevel@tonic-gate } 18867c478bd9Sstevel@tonic-gate if (!zonecfg_valid_rctl(name, rctlblk)) { 18877c478bd9Sstevel@tonic-gate zerror(gettext("(priv=%s,limit=%s,action=%s) " 18887c478bd9Sstevel@tonic-gate "is not a valid value for rctl '%s'"), 18897c478bd9Sstevel@tonic-gate rctlval->zone_rctlval_priv, 18907c478bd9Sstevel@tonic-gate rctlval->zone_rctlval_limit, 18917c478bd9Sstevel@tonic-gate rctlval->zone_rctlval_action, 18927c478bd9Sstevel@tonic-gate name); 18937c478bd9Sstevel@tonic-gate goto out; 18947c478bd9Sstevel@tonic-gate } 18957c478bd9Sstevel@tonic-gate } 18967c478bd9Sstevel@tonic-gate zonecfg_free_rctl_value_list(rctltab.zone_rctl_valptr); 18977c478bd9Sstevel@tonic-gate } 18987c478bd9Sstevel@tonic-gate rctltab.zone_rctl_valptr = NULL; 18997c478bd9Sstevel@tonic-gate error = Z_OK; 19007c478bd9Sstevel@tonic-gate out: 19017c478bd9Sstevel@tonic-gate zonecfg_free_rctl_value_list(rctltab.zone_rctl_valptr); 19027c478bd9Sstevel@tonic-gate (void) zonecfg_endrctlent(handle); 19037c478bd9Sstevel@tonic-gate free(rctlblk); 19047c478bd9Sstevel@tonic-gate return (error); 19057c478bd9Sstevel@tonic-gate } 19067c478bd9Sstevel@tonic-gate 19077c478bd9Sstevel@tonic-gate static int 19087c478bd9Sstevel@tonic-gate verify_pool(zone_dochandle_t handle) 19097c478bd9Sstevel@tonic-gate { 19107c478bd9Sstevel@tonic-gate char poolname[MAXPATHLEN]; 19117c478bd9Sstevel@tonic-gate pool_conf_t *poolconf; 19127c478bd9Sstevel@tonic-gate pool_t *pool; 19137c478bd9Sstevel@tonic-gate int status; 19147c478bd9Sstevel@tonic-gate int error; 19157c478bd9Sstevel@tonic-gate 19167c478bd9Sstevel@tonic-gate /* 19177c478bd9Sstevel@tonic-gate * This ends up being very similar to the check done in zoneadmd. 19187c478bd9Sstevel@tonic-gate */ 19197c478bd9Sstevel@tonic-gate error = zonecfg_get_pool(handle, poolname, sizeof (poolname)); 19207c478bd9Sstevel@tonic-gate if (error == Z_NO_ENTRY || (error == Z_OK && strlen(poolname) == 0)) { 19217c478bd9Sstevel@tonic-gate /* 19227c478bd9Sstevel@tonic-gate * No pool specified. 19237c478bd9Sstevel@tonic-gate */ 19247c478bd9Sstevel@tonic-gate return (0); 19257c478bd9Sstevel@tonic-gate } 19267c478bd9Sstevel@tonic-gate if (error != Z_OK) { 19277c478bd9Sstevel@tonic-gate zperror(gettext("Unable to retrieve pool name from " 19287c478bd9Sstevel@tonic-gate "configuration"), B_TRUE); 19297c478bd9Sstevel@tonic-gate return (error); 19307c478bd9Sstevel@tonic-gate } 19317c478bd9Sstevel@tonic-gate /* 19327c478bd9Sstevel@tonic-gate * Don't do anything if pools aren't enabled. 19337c478bd9Sstevel@tonic-gate */ 19347c478bd9Sstevel@tonic-gate if (pool_get_status(&status) != PO_SUCCESS || status != POOL_ENABLED) { 19357c478bd9Sstevel@tonic-gate zerror(gettext("WARNING: pools facility not active; " 19367c478bd9Sstevel@tonic-gate "zone will not be bound to pool '%s'."), poolname); 19377c478bd9Sstevel@tonic-gate return (Z_OK); 19387c478bd9Sstevel@tonic-gate } 19397c478bd9Sstevel@tonic-gate /* 19407c478bd9Sstevel@tonic-gate * Try to provide a sane error message if the requested pool doesn't 19417c478bd9Sstevel@tonic-gate * exist. It isn't clear that pools-related failures should 19427c478bd9Sstevel@tonic-gate * necessarily translate to a failure to verify the zone configuration, 19437c478bd9Sstevel@tonic-gate * hence they are not considered errors. 19447c478bd9Sstevel@tonic-gate */ 19457c478bd9Sstevel@tonic-gate if ((poolconf = pool_conf_alloc()) == NULL) { 19467c478bd9Sstevel@tonic-gate zerror(gettext("WARNING: pool_conf_alloc failed; " 19477c478bd9Sstevel@tonic-gate "using default pool")); 19487c478bd9Sstevel@tonic-gate return (Z_OK); 19497c478bd9Sstevel@tonic-gate } 19507c478bd9Sstevel@tonic-gate if (pool_conf_open(poolconf, pool_dynamic_location(), PO_RDONLY) != 19517c478bd9Sstevel@tonic-gate PO_SUCCESS) { 19527c478bd9Sstevel@tonic-gate zerror(gettext("WARNING: pool_conf_open failed; " 19537c478bd9Sstevel@tonic-gate "using default pool")); 19547c478bd9Sstevel@tonic-gate pool_conf_free(poolconf); 19557c478bd9Sstevel@tonic-gate return (Z_OK); 19567c478bd9Sstevel@tonic-gate } 19577c478bd9Sstevel@tonic-gate pool = pool_get_pool(poolconf, poolname); 19587c478bd9Sstevel@tonic-gate (void) pool_conf_close(poolconf); 19597c478bd9Sstevel@tonic-gate pool_conf_free(poolconf); 19607c478bd9Sstevel@tonic-gate if (pool == NULL) { 19617c478bd9Sstevel@tonic-gate zerror(gettext("WARNING: pool '%s' not found. " 19627c478bd9Sstevel@tonic-gate "using default pool"), poolname); 19637c478bd9Sstevel@tonic-gate } 19647c478bd9Sstevel@tonic-gate 19657c478bd9Sstevel@tonic-gate return (Z_OK); 19667c478bd9Sstevel@tonic-gate } 19677c478bd9Sstevel@tonic-gate 19687c478bd9Sstevel@tonic-gate static int 1969b5abaf04Sgjelinek verify_ipd(zone_dochandle_t handle) 1970b5abaf04Sgjelinek { 1971b5abaf04Sgjelinek int return_code = Z_OK; 1972b5abaf04Sgjelinek struct zone_fstab fstab; 1973b5abaf04Sgjelinek struct stat st; 1974b5abaf04Sgjelinek char specdir[MAXPATHLEN]; 1975b5abaf04Sgjelinek 1976b5abaf04Sgjelinek if (zonecfg_setipdent(handle) != Z_OK) { 19775ee84fbdSgjelinek /* 19785ee84fbdSgjelinek * TRANSLATION_NOTE 19795ee84fbdSgjelinek * inherit-pkg-dirs is a literal that should not be translated. 19805ee84fbdSgjelinek */ 19815ee84fbdSgjelinek (void) fprintf(stderr, gettext("could not verify " 1982b5abaf04Sgjelinek "inherit-pkg-dirs: unable to enumerate mounts\n")); 1983b5abaf04Sgjelinek return (Z_ERR); 1984b5abaf04Sgjelinek } 1985b5abaf04Sgjelinek while (zonecfg_getipdent(handle, &fstab) == Z_OK) { 1986b5abaf04Sgjelinek /* 1987b5abaf04Sgjelinek * Verify fs_dir exists. 1988b5abaf04Sgjelinek */ 1989b5abaf04Sgjelinek (void) snprintf(specdir, sizeof (specdir), "%s%s", 1990b5abaf04Sgjelinek zonecfg_get_root(), fstab.zone_fs_dir); 1991b5abaf04Sgjelinek if (stat(specdir, &st) != 0) { 19925ee84fbdSgjelinek /* 19935ee84fbdSgjelinek * TRANSLATION_NOTE 19945ee84fbdSgjelinek * inherit-pkg-dir is a literal that should not be 19955ee84fbdSgjelinek * translated. 19965ee84fbdSgjelinek */ 19975ee84fbdSgjelinek (void) fprintf(stderr, gettext("could not verify " 1998b5abaf04Sgjelinek "inherit-pkg-dir %s: %s\n"), 1999b5abaf04Sgjelinek fstab.zone_fs_dir, strerror(errno)); 2000b5abaf04Sgjelinek return_code = Z_ERR; 2001b5abaf04Sgjelinek } 2002b5abaf04Sgjelinek if (strcmp(st.st_fstype, MNTTYPE_NFS) == 0) { 20035ee84fbdSgjelinek /* 20045ee84fbdSgjelinek * TRANSLATION_NOTE 20055ee84fbdSgjelinek * inherit-pkg-dir and NFS are literals that should 20065ee84fbdSgjelinek * not be translated. 20075ee84fbdSgjelinek */ 2008b5abaf04Sgjelinek (void) fprintf(stderr, gettext("cannot verify " 20090b5de56dSgjelinek "inherit-pkg-dir %s: NFS mounted file system.\n" 20100b5de56dSgjelinek "\tA local file system must be used.\n"), 2011b5abaf04Sgjelinek fstab.zone_fs_dir); 2012b5abaf04Sgjelinek return_code = Z_ERR; 2013b5abaf04Sgjelinek } 2014b5abaf04Sgjelinek } 2015b5abaf04Sgjelinek (void) zonecfg_endipdent(handle); 2016b5abaf04Sgjelinek 2017b5abaf04Sgjelinek return (return_code); 2018b5abaf04Sgjelinek } 2019b5abaf04Sgjelinek 202020c8013fSlling /* 202120c8013fSlling * Verify that the special device/file system exists and is valid. 202220c8013fSlling */ 202320c8013fSlling static int 202420c8013fSlling verify_fs_special(struct zone_fstab *fstab) 202520c8013fSlling { 202620c8013fSlling struct stat st; 202720c8013fSlling 202820c8013fSlling if (strcmp(fstab->zone_fs_type, MNTTYPE_ZFS) == 0) 202920c8013fSlling return (verify_fs_zfs(fstab)); 203020c8013fSlling 203120c8013fSlling if (stat(fstab->zone_fs_special, &st) != 0) { 20325c358068Slling (void) fprintf(stderr, gettext("could not verify fs " 203320c8013fSlling "%s: could not access %s: %s\n"), fstab->zone_fs_dir, 203420c8013fSlling fstab->zone_fs_special, strerror(errno)); 203520c8013fSlling return (Z_ERR); 203620c8013fSlling } 203720c8013fSlling 203820c8013fSlling if (strcmp(st.st_fstype, MNTTYPE_NFS) == 0) { 203920c8013fSlling /* 204020c8013fSlling * TRANSLATION_NOTE 204120c8013fSlling * fs and NFS are literals that should 204220c8013fSlling * not be translated. 204320c8013fSlling */ 204420c8013fSlling (void) fprintf(stderr, gettext("cannot verify " 20450b5de56dSgjelinek "fs %s: NFS mounted file system.\n" 20460b5de56dSgjelinek "\tA local file system must be used.\n"), 204720c8013fSlling fstab->zone_fs_special); 204820c8013fSlling return (Z_ERR); 204920c8013fSlling } 205020c8013fSlling 205120c8013fSlling return (Z_OK); 205220c8013fSlling } 205320c8013fSlling 2054b5abaf04Sgjelinek static int 20557c478bd9Sstevel@tonic-gate verify_filesystems(zone_dochandle_t handle) 20567c478bd9Sstevel@tonic-gate { 20577c478bd9Sstevel@tonic-gate int return_code = Z_OK; 20587c478bd9Sstevel@tonic-gate struct zone_fstab fstab; 20597c478bd9Sstevel@tonic-gate char cmdbuf[MAXPATHLEN]; 20607c478bd9Sstevel@tonic-gate struct stat st; 20617c478bd9Sstevel@tonic-gate 20627c478bd9Sstevel@tonic-gate /* 20637c478bd9Sstevel@tonic-gate * No need to verify inherit-pkg-dir fs types, as their type is 20647c478bd9Sstevel@tonic-gate * implicitly lofs, which is known. Therefore, the types are only 20657c478bd9Sstevel@tonic-gate * verified for regular file systems below. 20667c478bd9Sstevel@tonic-gate * 20677c478bd9Sstevel@tonic-gate * Since the actual mount point is not known until the dependent mounts 20687c478bd9Sstevel@tonic-gate * are performed, we don't attempt any path validation here: that will 20697c478bd9Sstevel@tonic-gate * happen later when zoneadmd actually does the mounts. 20707c478bd9Sstevel@tonic-gate */ 20717c478bd9Sstevel@tonic-gate if (zonecfg_setfsent(handle) != Z_OK) { 20720b5de56dSgjelinek (void) fprintf(stderr, gettext("could not verify file systems: " 20737c478bd9Sstevel@tonic-gate "unable to enumerate mounts\n")); 20747c478bd9Sstevel@tonic-gate return (Z_ERR); 20757c478bd9Sstevel@tonic-gate } 20767c478bd9Sstevel@tonic-gate while (zonecfg_getfsent(handle, &fstab) == Z_OK) { 20777c478bd9Sstevel@tonic-gate if (!zonecfg_valid_fs_type(fstab.zone_fs_type)) { 20787c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("cannot verify fs %s: " 20797c478bd9Sstevel@tonic-gate "type %s is not allowed.\n"), fstab.zone_fs_dir, 20807c478bd9Sstevel@tonic-gate fstab.zone_fs_type); 20817c478bd9Sstevel@tonic-gate return_code = Z_ERR; 20827c478bd9Sstevel@tonic-gate goto next_fs; 20837c478bd9Sstevel@tonic-gate } 20847c478bd9Sstevel@tonic-gate /* 20857c478bd9Sstevel@tonic-gate * Verify /usr/lib/fs/<fstype>/mount exists. 20867c478bd9Sstevel@tonic-gate */ 20877c478bd9Sstevel@tonic-gate if (snprintf(cmdbuf, sizeof (cmdbuf), "/usr/lib/fs/%s/mount", 20887c478bd9Sstevel@tonic-gate fstab.zone_fs_type) > sizeof (cmdbuf)) { 20897c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("cannot verify fs %s: " 20907c478bd9Sstevel@tonic-gate "type %s is too long.\n"), fstab.zone_fs_dir, 20917c478bd9Sstevel@tonic-gate fstab.zone_fs_type); 20927c478bd9Sstevel@tonic-gate return_code = Z_ERR; 20937c478bd9Sstevel@tonic-gate goto next_fs; 20947c478bd9Sstevel@tonic-gate } 20957c478bd9Sstevel@tonic-gate if (stat(cmdbuf, &st) != 0) { 20965ee84fbdSgjelinek (void) fprintf(stderr, gettext("could not verify fs " 20975ee84fbdSgjelinek "%s: could not access %s: %s\n"), fstab.zone_fs_dir, 20987c478bd9Sstevel@tonic-gate cmdbuf, strerror(errno)); 20997c478bd9Sstevel@tonic-gate return_code = Z_ERR; 21007c478bd9Sstevel@tonic-gate goto next_fs; 21017c478bd9Sstevel@tonic-gate } 21027c478bd9Sstevel@tonic-gate if (!S_ISREG(st.st_mode)) { 21035ee84fbdSgjelinek (void) fprintf(stderr, gettext("could not verify fs " 21045ee84fbdSgjelinek "%s: %s is not a regular file\n"), 21055ee84fbdSgjelinek fstab.zone_fs_dir, cmdbuf); 21067c478bd9Sstevel@tonic-gate return_code = Z_ERR; 21077c478bd9Sstevel@tonic-gate goto next_fs; 21087c478bd9Sstevel@tonic-gate } 21097c478bd9Sstevel@tonic-gate /* 21107c478bd9Sstevel@tonic-gate * Verify /usr/lib/fs/<fstype>/fsck exists iff zone_fs_raw is 21117c478bd9Sstevel@tonic-gate * set. 21127c478bd9Sstevel@tonic-gate */ 21137c478bd9Sstevel@tonic-gate if (snprintf(cmdbuf, sizeof (cmdbuf), "/usr/lib/fs/%s/fsck", 21147c478bd9Sstevel@tonic-gate fstab.zone_fs_type) > sizeof (cmdbuf)) { 21157c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("cannot verify fs %s: " 21167c478bd9Sstevel@tonic-gate "type %s is too long.\n"), fstab.zone_fs_dir, 21177c478bd9Sstevel@tonic-gate fstab.zone_fs_type); 21187c478bd9Sstevel@tonic-gate return_code = Z_ERR; 21197c478bd9Sstevel@tonic-gate goto next_fs; 21207c478bd9Sstevel@tonic-gate } 21217c478bd9Sstevel@tonic-gate if (fstab.zone_fs_raw[0] == '\0' && stat(cmdbuf, &st) == 0) { 21225ee84fbdSgjelinek (void) fprintf(stderr, gettext("could not verify fs " 21235ee84fbdSgjelinek "%s: must specify 'raw' device for %s " 21240b5de56dSgjelinek "file systems\n"), 21257c478bd9Sstevel@tonic-gate fstab.zone_fs_dir, fstab.zone_fs_type); 21267c478bd9Sstevel@tonic-gate return_code = Z_ERR; 21277c478bd9Sstevel@tonic-gate goto next_fs; 21287c478bd9Sstevel@tonic-gate } 21297c478bd9Sstevel@tonic-gate if (fstab.zone_fs_raw[0] != '\0' && 21307c478bd9Sstevel@tonic-gate (stat(cmdbuf, &st) != 0 || !S_ISREG(st.st_mode))) { 21317c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("cannot verify fs %s: " 21327c478bd9Sstevel@tonic-gate "'raw' device specified but " 21337c478bd9Sstevel@tonic-gate "no fsck executable exists for %s\n"), 21347c478bd9Sstevel@tonic-gate fstab.zone_fs_dir, fstab.zone_fs_type); 21357c478bd9Sstevel@tonic-gate return_code = Z_ERR; 21367c478bd9Sstevel@tonic-gate goto next_fs; 21377c478bd9Sstevel@tonic-gate } 213820c8013fSlling 213920c8013fSlling /* Verify fs_special. */ 214020c8013fSlling if ((return_code = verify_fs_special(&fstab)) != Z_OK) 2141b5abaf04Sgjelinek goto next_fs; 214220c8013fSlling 214320c8013fSlling /* Verify fs_raw. */ 2144b5abaf04Sgjelinek if (fstab.zone_fs_raw[0] != '\0' && 2145b5abaf04Sgjelinek stat(fstab.zone_fs_raw, &st) != 0) { 21465ee84fbdSgjelinek /* 21475ee84fbdSgjelinek * TRANSLATION_NOTE 21485ee84fbdSgjelinek * fs is a literal that should not be translated. 21495ee84fbdSgjelinek */ 21505ee84fbdSgjelinek (void) fprintf(stderr, gettext("could not verify fs " 21515ee84fbdSgjelinek "%s: could not access %s: %s\n"), fstab.zone_fs_dir, 2152b5abaf04Sgjelinek fstab.zone_fs_raw, strerror(errno)); 2153b5abaf04Sgjelinek return_code = Z_ERR; 2154b5abaf04Sgjelinek goto next_fs; 2155b5abaf04Sgjelinek } 21567c478bd9Sstevel@tonic-gate next_fs: 21577c478bd9Sstevel@tonic-gate zonecfg_free_fs_option_list(fstab.zone_fs_options); 21587c478bd9Sstevel@tonic-gate } 21597c478bd9Sstevel@tonic-gate (void) zonecfg_endfsent(handle); 21607c478bd9Sstevel@tonic-gate 21617c478bd9Sstevel@tonic-gate return (return_code); 21627c478bd9Sstevel@tonic-gate } 21637c478bd9Sstevel@tonic-gate 21647c478bd9Sstevel@tonic-gate static int 2165ffbafc53Scomay verify_limitpriv(zone_dochandle_t handle) 2166ffbafc53Scomay { 2167ffbafc53Scomay char *privname = NULL; 2168ffbafc53Scomay int err; 2169ffbafc53Scomay priv_set_t *privs; 2170ffbafc53Scomay 2171ffbafc53Scomay if ((privs = priv_allocset()) == NULL) { 2172ffbafc53Scomay zperror(gettext("failed to allocate privilege set"), B_FALSE); 2173ffbafc53Scomay return (Z_NOMEM); 2174ffbafc53Scomay } 2175ffbafc53Scomay err = zonecfg_get_privset(handle, privs, &privname); 2176ffbafc53Scomay switch (err) { 2177ffbafc53Scomay case Z_OK: 2178ffbafc53Scomay break; 2179ffbafc53Scomay case Z_PRIV_PROHIBITED: 2180ffbafc53Scomay (void) fprintf(stderr, gettext("privilege \"%s\" is not " 2181ffbafc53Scomay "permitted within the zone's privilege set\n"), privname); 2182ffbafc53Scomay break; 2183ffbafc53Scomay case Z_PRIV_REQUIRED: 2184ffbafc53Scomay (void) fprintf(stderr, gettext("required privilege \"%s\" is " 2185ffbafc53Scomay "missing from the zone's privilege set\n"), privname); 2186ffbafc53Scomay break; 2187ffbafc53Scomay case Z_PRIV_UNKNOWN: 2188ffbafc53Scomay (void) fprintf(stderr, gettext("unknown privilege \"%s\" " 2189ffbafc53Scomay "specified in the zone's privilege set\n"), privname); 2190ffbafc53Scomay break; 2191ffbafc53Scomay default: 2192ffbafc53Scomay zperror( 2193ffbafc53Scomay gettext("failed to determine the zone's privilege set"), 2194ffbafc53Scomay B_TRUE); 2195ffbafc53Scomay break; 2196ffbafc53Scomay } 2197ffbafc53Scomay free(privname); 2198ffbafc53Scomay priv_freeset(privs); 2199ffbafc53Scomay return (err); 2200ffbafc53Scomay } 2201ffbafc53Scomay 22021390a385Sgjelinek static void 22031390a385Sgjelinek free_local_netifs(int if_cnt, struct net_if **if_list) 22041390a385Sgjelinek { 22051390a385Sgjelinek int i; 22061390a385Sgjelinek 22071390a385Sgjelinek for (i = 0; i < if_cnt; i++) { 22081390a385Sgjelinek free(if_list[i]->name); 22091390a385Sgjelinek free(if_list[i]); 22101390a385Sgjelinek } 22111390a385Sgjelinek free(if_list); 22121390a385Sgjelinek } 22131390a385Sgjelinek 22141390a385Sgjelinek /* 22151390a385Sgjelinek * Get a list of the network interfaces, along with their address families, 22161390a385Sgjelinek * that are plumbed in the global zone. See if_tcp(7p) for a description 22171390a385Sgjelinek * of the ioctls used here. 22181390a385Sgjelinek */ 22191390a385Sgjelinek static int 22201390a385Sgjelinek get_local_netifs(int *if_cnt, struct net_if ***if_list) 22211390a385Sgjelinek { 22221390a385Sgjelinek int s; 22231390a385Sgjelinek int i; 22241390a385Sgjelinek int res = Z_OK; 22251390a385Sgjelinek int space_needed; 22261390a385Sgjelinek int cnt = 0; 22271390a385Sgjelinek struct lifnum if_num; 22281390a385Sgjelinek struct lifconf if_conf; 22291390a385Sgjelinek struct lifreq *if_reqp; 22301390a385Sgjelinek char *if_buf; 22311390a385Sgjelinek struct net_if **local_ifs = NULL; 22321390a385Sgjelinek 22331390a385Sgjelinek *if_cnt = 0; 22341390a385Sgjelinek *if_list = NULL; 22351390a385Sgjelinek 22361390a385Sgjelinek if ((s = socket(SOCKET_AF(AF_INET), SOCK_DGRAM, 0)) < 0) 22371390a385Sgjelinek return (Z_ERR); 22381390a385Sgjelinek 22391390a385Sgjelinek /* 22401390a385Sgjelinek * Come back here in the unlikely event that the number of interfaces 22411390a385Sgjelinek * increases between the time we get the count and the time we do the 22421390a385Sgjelinek * SIOCGLIFCONF ioctl. 22431390a385Sgjelinek */ 22441390a385Sgjelinek retry: 22451390a385Sgjelinek /* Get the number of interfaces. */ 22461390a385Sgjelinek if_num.lifn_family = AF_UNSPEC; 22471390a385Sgjelinek if_num.lifn_flags = LIFC_NOXMIT; 22481390a385Sgjelinek if (ioctl(s, SIOCGLIFNUM, &if_num) < 0) { 22491390a385Sgjelinek (void) close(s); 22501390a385Sgjelinek return (Z_ERR); 22511390a385Sgjelinek } 22521390a385Sgjelinek 22531390a385Sgjelinek /* Get the interface configuration list. */ 22541390a385Sgjelinek space_needed = if_num.lifn_count * sizeof (struct lifreq); 22551390a385Sgjelinek if ((if_buf = malloc(space_needed)) == NULL) { 22561390a385Sgjelinek (void) close(s); 22571390a385Sgjelinek return (Z_ERR); 22581390a385Sgjelinek } 22591390a385Sgjelinek if_conf.lifc_family = AF_UNSPEC; 22601390a385Sgjelinek if_conf.lifc_flags = LIFC_NOXMIT; 22611390a385Sgjelinek if_conf.lifc_len = space_needed; 22621390a385Sgjelinek if_conf.lifc_buf = if_buf; 22631390a385Sgjelinek if (ioctl(s, SIOCGLIFCONF, &if_conf) < 0) { 22641390a385Sgjelinek free(if_buf); 22651390a385Sgjelinek /* 22661390a385Sgjelinek * SIOCGLIFCONF returns EINVAL if the buffer we passed in is 22671390a385Sgjelinek * too small. In this case go back and get the new if cnt. 22681390a385Sgjelinek */ 22691390a385Sgjelinek if (errno == EINVAL) 22701390a385Sgjelinek goto retry; 22711390a385Sgjelinek 22721390a385Sgjelinek (void) close(s); 22731390a385Sgjelinek return (Z_ERR); 22741390a385Sgjelinek } 22751390a385Sgjelinek (void) close(s); 22761390a385Sgjelinek 22771390a385Sgjelinek /* Get the name and address family for each interface. */ 22781390a385Sgjelinek if_reqp = if_conf.lifc_req; 22791390a385Sgjelinek for (i = 0; i < (if_conf.lifc_len / sizeof (struct lifreq)); i++) { 22801390a385Sgjelinek struct net_if **p; 22811390a385Sgjelinek struct lifreq req; 22821390a385Sgjelinek 22831390a385Sgjelinek if (strcmp(LOOPBACK_IF, if_reqp->lifr_name) == 0) { 22841390a385Sgjelinek if_reqp++; 22851390a385Sgjelinek continue; 22861390a385Sgjelinek } 22871390a385Sgjelinek 22881390a385Sgjelinek if ((s = socket(SOCKET_AF(if_reqp->lifr_addr.ss_family), 22891390a385Sgjelinek SOCK_DGRAM, 0)) == -1) { 22901390a385Sgjelinek res = Z_ERR; 22911390a385Sgjelinek break; 22921390a385Sgjelinek } 22931390a385Sgjelinek 22941390a385Sgjelinek (void) strncpy(req.lifr_name, if_reqp->lifr_name, 22951390a385Sgjelinek sizeof (req.lifr_name)); 22961390a385Sgjelinek if (ioctl(s, SIOCGLIFADDR, &req) < 0) { 22971390a385Sgjelinek (void) close(s); 22981390a385Sgjelinek if_reqp++; 22991390a385Sgjelinek continue; 23001390a385Sgjelinek } 23011390a385Sgjelinek 23021390a385Sgjelinek if ((p = (struct net_if **)realloc(local_ifs, 23031390a385Sgjelinek sizeof (struct net_if *) * (cnt + 1))) == NULL) { 23041390a385Sgjelinek res = Z_ERR; 23051390a385Sgjelinek break; 23061390a385Sgjelinek } 23071390a385Sgjelinek local_ifs = p; 23081390a385Sgjelinek 23091390a385Sgjelinek if ((local_ifs[cnt] = malloc(sizeof (struct net_if))) == NULL) { 23101390a385Sgjelinek res = Z_ERR; 23111390a385Sgjelinek break; 23121390a385Sgjelinek } 23131390a385Sgjelinek 23141390a385Sgjelinek if ((local_ifs[cnt]->name = strdup(if_reqp->lifr_name)) 23151390a385Sgjelinek == NULL) { 23161390a385Sgjelinek free(local_ifs[cnt]); 23171390a385Sgjelinek res = Z_ERR; 23181390a385Sgjelinek break; 23191390a385Sgjelinek } 23201390a385Sgjelinek local_ifs[cnt]->af = req.lifr_addr.ss_family; 23211390a385Sgjelinek cnt++; 23221390a385Sgjelinek 23231390a385Sgjelinek (void) close(s); 23241390a385Sgjelinek if_reqp++; 23251390a385Sgjelinek } 23261390a385Sgjelinek 23271390a385Sgjelinek free(if_buf); 23281390a385Sgjelinek 23291390a385Sgjelinek if (res != Z_OK) { 23301390a385Sgjelinek free_local_netifs(cnt, local_ifs); 23311390a385Sgjelinek } else { 23321390a385Sgjelinek *if_cnt = cnt; 23331390a385Sgjelinek *if_list = local_ifs; 23341390a385Sgjelinek } 23351390a385Sgjelinek 23361390a385Sgjelinek return (res); 23371390a385Sgjelinek } 23381390a385Sgjelinek 23391390a385Sgjelinek static char * 23401390a385Sgjelinek af2str(int af) 23411390a385Sgjelinek { 23421390a385Sgjelinek switch (af) { 23431390a385Sgjelinek case AF_INET: 23441390a385Sgjelinek return ("IPv4"); 23451390a385Sgjelinek case AF_INET6: 23461390a385Sgjelinek return ("IPv6"); 23471390a385Sgjelinek default: 23481390a385Sgjelinek return ("Unknown"); 23491390a385Sgjelinek } 23501390a385Sgjelinek } 23511390a385Sgjelinek 23521390a385Sgjelinek /* 23531390a385Sgjelinek * Cross check the network interface name and address family with the 23541390a385Sgjelinek * interfaces that are set up in the global zone so that we can print the 23551390a385Sgjelinek * appropriate error message. 23561390a385Sgjelinek */ 23571390a385Sgjelinek static void 23581390a385Sgjelinek print_net_err(char *phys, char *addr, int af, char *msg) 23591390a385Sgjelinek { 23601390a385Sgjelinek int i; 23611390a385Sgjelinek int local_if_cnt = 0; 23621390a385Sgjelinek struct net_if **local_ifs = NULL; 23631390a385Sgjelinek boolean_t found_if = B_FALSE; 23641390a385Sgjelinek boolean_t found_af = B_FALSE; 23651390a385Sgjelinek 23661390a385Sgjelinek if (get_local_netifs(&local_if_cnt, &local_ifs) != Z_OK) { 23671390a385Sgjelinek (void) fprintf(stderr, 23681390a385Sgjelinek gettext("could not verify %s %s=%s %s=%s\n\t%s\n"), 23691390a385Sgjelinek "net", "address", addr, "physical", phys, msg); 23701390a385Sgjelinek return; 23711390a385Sgjelinek } 23721390a385Sgjelinek 23731390a385Sgjelinek for (i = 0; i < local_if_cnt; i++) { 23741390a385Sgjelinek if (strcmp(phys, local_ifs[i]->name) == 0) { 23751390a385Sgjelinek found_if = B_TRUE; 23761390a385Sgjelinek if (af == local_ifs[i]->af) { 23771390a385Sgjelinek found_af = B_TRUE; 23781390a385Sgjelinek break; 23791390a385Sgjelinek } 23801390a385Sgjelinek } 23811390a385Sgjelinek } 23821390a385Sgjelinek 23831390a385Sgjelinek free_local_netifs(local_if_cnt, local_ifs); 23841390a385Sgjelinek 23851390a385Sgjelinek if (!found_if) { 23861390a385Sgjelinek (void) fprintf(stderr, 23871390a385Sgjelinek gettext("could not verify %s %s=%s\n\t" 23881390a385Sgjelinek "network interface %s is not plumbed in the global zone\n"), 23891390a385Sgjelinek "net", "physical", phys, phys); 23901390a385Sgjelinek return; 23911390a385Sgjelinek } 23921390a385Sgjelinek 23931390a385Sgjelinek /* 23941390a385Sgjelinek * Print this error if we were unable to find the address family 23951390a385Sgjelinek * for this interface. If the af variable is not initialized to 23961390a385Sgjelinek * to something meaningful by the caller (not AF_UNSPEC) then we 23971390a385Sgjelinek * also skip this message since it wouldn't be informative. 23981390a385Sgjelinek */ 23991390a385Sgjelinek if (!found_af && af != AF_UNSPEC) { 24001390a385Sgjelinek (void) fprintf(stderr, 24011390a385Sgjelinek gettext("could not verify %s %s=%s %s=%s\n\tthe %s address " 24021390a385Sgjelinek "family is not configured on this interface in the\n\t" 24031390a385Sgjelinek "global zone\n"), 24041390a385Sgjelinek "net", "address", addr, "physical", phys, af2str(af)); 24051390a385Sgjelinek return; 24061390a385Sgjelinek } 24071390a385Sgjelinek 24081390a385Sgjelinek (void) fprintf(stderr, 24091390a385Sgjelinek gettext("could not verify %s %s=%s %s=%s\n\t%s\n"), 24101390a385Sgjelinek "net", "address", addr, "physical", phys, msg); 24111390a385Sgjelinek } 24121390a385Sgjelinek 2413ffbafc53Scomay static int 24148cd327d5Sgjelinek verify_handle(int cmd_num, zone_dochandle_t handle) 24157c478bd9Sstevel@tonic-gate { 24167c478bd9Sstevel@tonic-gate struct zone_nwiftab nwiftab; 24177c478bd9Sstevel@tonic-gate int return_code = Z_OK; 24187c478bd9Sstevel@tonic-gate int err; 2419108322fbScarlsonj boolean_t in_alt_root; 24207c478bd9Sstevel@tonic-gate 2421108322fbScarlsonj in_alt_root = zonecfg_in_alt_root(); 2422108322fbScarlsonj if (in_alt_root) 2423108322fbScarlsonj goto no_net; 2424108322fbScarlsonj 24257c478bd9Sstevel@tonic-gate if ((err = zonecfg_setnwifent(handle)) != Z_OK) { 24267c478bd9Sstevel@tonic-gate errno = err; 24277c478bd9Sstevel@tonic-gate zperror(cmd_to_str(cmd_num), B_TRUE); 24287c478bd9Sstevel@tonic-gate zonecfg_fini_handle(handle); 24297c478bd9Sstevel@tonic-gate return (Z_ERR); 24307c478bd9Sstevel@tonic-gate } 24317c478bd9Sstevel@tonic-gate while (zonecfg_getnwifent(handle, &nwiftab) == Z_OK) { 24327c478bd9Sstevel@tonic-gate struct lifreq lifr; 24331390a385Sgjelinek sa_family_t af = AF_UNSPEC; 24347c478bd9Sstevel@tonic-gate int so, res; 24357c478bd9Sstevel@tonic-gate 24367c478bd9Sstevel@tonic-gate /* skip any loopback interfaces */ 24377c478bd9Sstevel@tonic-gate if (strcmp(nwiftab.zone_nwif_physical, "lo0") == 0) 24387c478bd9Sstevel@tonic-gate continue; 24397c478bd9Sstevel@tonic-gate if ((res = zonecfg_valid_net_address(nwiftab.zone_nwif_address, 24407c478bd9Sstevel@tonic-gate &lifr)) != Z_OK) { 24411390a385Sgjelinek print_net_err(nwiftab.zone_nwif_physical, 24421390a385Sgjelinek nwiftab.zone_nwif_address, af, 24431390a385Sgjelinek zonecfg_strerror(res)); 24447c478bd9Sstevel@tonic-gate return_code = Z_ERR; 24457c478bd9Sstevel@tonic-gate continue; 24467c478bd9Sstevel@tonic-gate } 24477c478bd9Sstevel@tonic-gate af = lifr.lifr_addr.ss_family; 24487c478bd9Sstevel@tonic-gate (void) memset(&lifr, 0, sizeof (lifr)); 24497c478bd9Sstevel@tonic-gate (void) strlcpy(lifr.lifr_name, nwiftab.zone_nwif_physical, 24507c478bd9Sstevel@tonic-gate sizeof (lifr.lifr_name)); 24517c478bd9Sstevel@tonic-gate lifr.lifr_addr.ss_family = af; 24527c478bd9Sstevel@tonic-gate if ((so = socket(af, SOCK_DGRAM, 0)) < 0) { 24537c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("could not verify %s " 24547c478bd9Sstevel@tonic-gate "%s=%s %s=%s: could not get socket: %s\n"), "net", 24557c478bd9Sstevel@tonic-gate "address", nwiftab.zone_nwif_address, "physical", 24567c478bd9Sstevel@tonic-gate nwiftab.zone_nwif_physical, strerror(errno)); 24577c478bd9Sstevel@tonic-gate return_code = Z_ERR; 24587c478bd9Sstevel@tonic-gate continue; 24597c478bd9Sstevel@tonic-gate } 24601390a385Sgjelinek if (ioctl(so, SIOCGLIFFLAGS, &lifr) < 0) { 24611390a385Sgjelinek print_net_err(nwiftab.zone_nwif_physical, 24621390a385Sgjelinek nwiftab.zone_nwif_address, af, 24637c478bd9Sstevel@tonic-gate strerror(errno)); 24647c478bd9Sstevel@tonic-gate return_code = Z_ERR; 24657c478bd9Sstevel@tonic-gate } 24667c478bd9Sstevel@tonic-gate (void) close(so); 24677c478bd9Sstevel@tonic-gate } 24687c478bd9Sstevel@tonic-gate (void) zonecfg_endnwifent(handle); 2469108322fbScarlsonj no_net: 24707c478bd9Sstevel@tonic-gate 2471e7f3c547Sgjelinek /* verify that lofs has not been excluded from the kernel */ 24728cd327d5Sgjelinek if (!(cmd_num == CMD_DETACH || cmd_num == CMD_ATTACH || 24738cd327d5Sgjelinek cmd_num == CMD_MOVE || cmd_num == CMD_CLONE) && 24748cd327d5Sgjelinek modctl(MODLOAD, 1, "fs/lofs", NULL) != 0) { 2475e7f3c547Sgjelinek if (errno == ENXIO) 2476e7f3c547Sgjelinek (void) fprintf(stderr, gettext("could not verify " 2477e7f3c547Sgjelinek "lofs(7FS): possibly excluded in /etc/system\n")); 2478e7f3c547Sgjelinek else 2479e7f3c547Sgjelinek (void) fprintf(stderr, gettext("could not verify " 2480e7f3c547Sgjelinek "lofs(7FS): %s\n"), strerror(errno)); 2481e7f3c547Sgjelinek return_code = Z_ERR; 2482e7f3c547Sgjelinek } 2483e7f3c547Sgjelinek 24847c478bd9Sstevel@tonic-gate if (verify_filesystems(handle) != Z_OK) 24857c478bd9Sstevel@tonic-gate return_code = Z_ERR; 2486b5abaf04Sgjelinek if (verify_ipd(handle) != Z_OK) 2487b5abaf04Sgjelinek return_code = Z_ERR; 2488108322fbScarlsonj if (!in_alt_root && verify_rctls(handle) != Z_OK) 24897c478bd9Sstevel@tonic-gate return_code = Z_ERR; 2490108322fbScarlsonj if (!in_alt_root && verify_pool(handle) != Z_OK) 24917c478bd9Sstevel@tonic-gate return_code = Z_ERR; 2492fa9e4066Sahrens if (!in_alt_root && verify_datasets(handle) != Z_OK) 2493fa9e4066Sahrens return_code = Z_ERR; 2494ffbafc53Scomay 2495ffbafc53Scomay /* 2496ffbafc53Scomay * As the "mount" command is used for patching/upgrading of zones 2497ffbafc53Scomay * or other maintenance processes, the zone's privilege set is not 2498ffbafc53Scomay * checked in this case. Instead, the default, safe set of 2499ffbafc53Scomay * privileges will be used when this zone is created in the 2500ffbafc53Scomay * kernel. 2501ffbafc53Scomay */ 2502ffbafc53Scomay if (!in_alt_root && cmd_num != CMD_MOUNT && 2503ffbafc53Scomay verify_limitpriv(handle) != Z_OK) 2504ffbafc53Scomay return_code = Z_ERR; 25058cd327d5Sgjelinek 25068cd327d5Sgjelinek return (return_code); 25078cd327d5Sgjelinek } 25088cd327d5Sgjelinek 25098cd327d5Sgjelinek static int 25108cd327d5Sgjelinek verify_details(int cmd_num) 25118cd327d5Sgjelinek { 25128cd327d5Sgjelinek zone_dochandle_t handle; 25138cd327d5Sgjelinek char zonepath[MAXPATHLEN], checkpath[MAXPATHLEN]; 25148cd327d5Sgjelinek int return_code = Z_OK; 25158cd327d5Sgjelinek int err; 25168cd327d5Sgjelinek 25178cd327d5Sgjelinek if ((handle = zonecfg_init_handle()) == NULL) { 25188cd327d5Sgjelinek zperror(cmd_to_str(cmd_num), B_TRUE); 25198cd327d5Sgjelinek return (Z_ERR); 25208cd327d5Sgjelinek } 25218cd327d5Sgjelinek if ((err = zonecfg_get_handle(target_zone, handle)) != Z_OK) { 25228cd327d5Sgjelinek errno = err; 25238cd327d5Sgjelinek zperror(cmd_to_str(cmd_num), B_TRUE); 25248cd327d5Sgjelinek zonecfg_fini_handle(handle); 25258cd327d5Sgjelinek return (Z_ERR); 25268cd327d5Sgjelinek } 25278cd327d5Sgjelinek if ((err = zonecfg_get_zonepath(handle, zonepath, sizeof (zonepath))) != 25288cd327d5Sgjelinek Z_OK) { 25298cd327d5Sgjelinek errno = err; 25308cd327d5Sgjelinek zperror(cmd_to_str(cmd_num), B_TRUE); 25318cd327d5Sgjelinek zonecfg_fini_handle(handle); 25328cd327d5Sgjelinek return (Z_ERR); 25338cd327d5Sgjelinek } 25348cd327d5Sgjelinek /* 25358cd327d5Sgjelinek * zonecfg_get_zonepath() gets its data from the XML repository. 25368cd327d5Sgjelinek * Verify this against the index file, which is checked first by 25378cd327d5Sgjelinek * zone_get_zonepath(). If they don't match, bail out. 25388cd327d5Sgjelinek */ 25398cd327d5Sgjelinek if ((err = zone_get_zonepath(target_zone, checkpath, 25408cd327d5Sgjelinek sizeof (checkpath))) != Z_OK) { 25418cd327d5Sgjelinek errno = err; 25428cd327d5Sgjelinek zperror2(target_zone, gettext("could not get zone path")); 25438cd327d5Sgjelinek return (Z_ERR); 25448cd327d5Sgjelinek } 25458cd327d5Sgjelinek if (strcmp(zonepath, checkpath) != 0) { 25468cd327d5Sgjelinek /* 25478cd327d5Sgjelinek * TRANSLATION_NOTE 25488cd327d5Sgjelinek * XML and zonepath are literals that should not be translated. 25498cd327d5Sgjelinek */ 25508cd327d5Sgjelinek (void) fprintf(stderr, gettext("The XML repository has " 25518cd327d5Sgjelinek "zonepath '%s',\nbut the index file has zonepath '%s'.\n" 25528cd327d5Sgjelinek "These must match, so fix the incorrect entry.\n"), 25538cd327d5Sgjelinek zonepath, checkpath); 25548cd327d5Sgjelinek return (Z_ERR); 25558cd327d5Sgjelinek } 25568cd327d5Sgjelinek if (validate_zonepath(zonepath, cmd_num) != Z_OK) { 25578cd327d5Sgjelinek (void) fprintf(stderr, gettext("could not verify zonepath %s " 25588cd327d5Sgjelinek "because of the above errors.\n"), zonepath); 25598cd327d5Sgjelinek return_code = Z_ERR; 25608cd327d5Sgjelinek } 25618cd327d5Sgjelinek 25628cd327d5Sgjelinek if (verify_handle(cmd_num, handle) != Z_OK) 25638cd327d5Sgjelinek return_code = Z_ERR; 25648cd327d5Sgjelinek 25657c478bd9Sstevel@tonic-gate zonecfg_fini_handle(handle); 25667c478bd9Sstevel@tonic-gate if (return_code == Z_ERR) 25677c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 25687c478bd9Sstevel@tonic-gate gettext("%s: zone %s failed to verify\n"), 25697c478bd9Sstevel@tonic-gate execname, target_zone); 25707c478bd9Sstevel@tonic-gate return (return_code); 25717c478bd9Sstevel@tonic-gate } 25727c478bd9Sstevel@tonic-gate 25737c478bd9Sstevel@tonic-gate static int 25747c478bd9Sstevel@tonic-gate verify_func(int argc, char *argv[]) 25757c478bd9Sstevel@tonic-gate { 25767c478bd9Sstevel@tonic-gate int arg; 25777c478bd9Sstevel@tonic-gate 25787c478bd9Sstevel@tonic-gate optind = 0; 25797c478bd9Sstevel@tonic-gate if ((arg = getopt(argc, argv, "?")) != EOF) { 25807c478bd9Sstevel@tonic-gate switch (arg) { 25817c478bd9Sstevel@tonic-gate case '?': 25827c478bd9Sstevel@tonic-gate sub_usage(SHELP_VERIFY, CMD_VERIFY); 25837c478bd9Sstevel@tonic-gate return (optopt == '?' ? Z_OK : Z_USAGE); 25847c478bd9Sstevel@tonic-gate default: 25857c478bd9Sstevel@tonic-gate sub_usage(SHELP_VERIFY, CMD_VERIFY); 25867c478bd9Sstevel@tonic-gate return (Z_USAGE); 25877c478bd9Sstevel@tonic-gate } 25887c478bd9Sstevel@tonic-gate } 25897c478bd9Sstevel@tonic-gate if (argc > optind) { 25907c478bd9Sstevel@tonic-gate sub_usage(SHELP_VERIFY, CMD_VERIFY); 25917c478bd9Sstevel@tonic-gate return (Z_USAGE); 25927c478bd9Sstevel@tonic-gate } 25937c478bd9Sstevel@tonic-gate if (sanity_check(target_zone, CMD_VERIFY, B_FALSE, B_FALSE) != Z_OK) 25947c478bd9Sstevel@tonic-gate return (Z_ERR); 25957c478bd9Sstevel@tonic-gate return (verify_details(CMD_VERIFY)); 25967c478bd9Sstevel@tonic-gate } 25977c478bd9Sstevel@tonic-gate 25987c478bd9Sstevel@tonic-gate #define LUCREATEZONE "/usr/lib/lu/lucreatezone" 25997c478bd9Sstevel@tonic-gate 26007c478bd9Sstevel@tonic-gate static int 26017c478bd9Sstevel@tonic-gate install_func(int argc, char *argv[]) 26027c478bd9Sstevel@tonic-gate { 26037c478bd9Sstevel@tonic-gate /* 9: "exec " and " -z " */ 26047c478bd9Sstevel@tonic-gate char cmdbuf[sizeof (LUCREATEZONE) + ZONENAME_MAX + 9]; 26057c478bd9Sstevel@tonic-gate int lockfd; 26067c478bd9Sstevel@tonic-gate int err, arg; 26077c478bd9Sstevel@tonic-gate char zonepath[MAXPATHLEN]; 26087c478bd9Sstevel@tonic-gate int status; 26090b5de56dSgjelinek boolean_t nodataset = B_FALSE; 26107c478bd9Sstevel@tonic-gate 2611108322fbScarlsonj if (zonecfg_in_alt_root()) { 2612108322fbScarlsonj zerror(gettext("cannot install zone in alternate root")); 2613108322fbScarlsonj return (Z_ERR); 2614108322fbScarlsonj } 2615108322fbScarlsonj 26167c478bd9Sstevel@tonic-gate optind = 0; 26170b5de56dSgjelinek if ((arg = getopt(argc, argv, "?x:")) != EOF) { 26187c478bd9Sstevel@tonic-gate switch (arg) { 26197c478bd9Sstevel@tonic-gate case '?': 26207c478bd9Sstevel@tonic-gate sub_usage(SHELP_INSTALL, CMD_INSTALL); 26217c478bd9Sstevel@tonic-gate return (optopt == '?' ? Z_OK : Z_USAGE); 26220b5de56dSgjelinek case 'x': 26230b5de56dSgjelinek if (strcmp(optarg, "nodataset") != 0) { 26240b5de56dSgjelinek sub_usage(SHELP_INSTALL, CMD_INSTALL); 26250b5de56dSgjelinek return (Z_USAGE); 26260b5de56dSgjelinek } 26270b5de56dSgjelinek nodataset = B_TRUE; 26280b5de56dSgjelinek break; 26297c478bd9Sstevel@tonic-gate default: 26307c478bd9Sstevel@tonic-gate sub_usage(SHELP_INSTALL, CMD_INSTALL); 26317c478bd9Sstevel@tonic-gate return (Z_USAGE); 26327c478bd9Sstevel@tonic-gate } 26337c478bd9Sstevel@tonic-gate } 26347c478bd9Sstevel@tonic-gate if (argc > optind) { 26357c478bd9Sstevel@tonic-gate sub_usage(SHELP_INSTALL, CMD_INSTALL); 26367c478bd9Sstevel@tonic-gate return (Z_USAGE); 26377c478bd9Sstevel@tonic-gate } 26387c478bd9Sstevel@tonic-gate if (sanity_check(target_zone, CMD_INSTALL, B_FALSE, B_TRUE) != Z_OK) 26397c478bd9Sstevel@tonic-gate return (Z_ERR); 26407c478bd9Sstevel@tonic-gate if (verify_details(CMD_INSTALL) != Z_OK) 26417c478bd9Sstevel@tonic-gate return (Z_ERR); 26427c478bd9Sstevel@tonic-gate 26437c478bd9Sstevel@tonic-gate if (grab_lock_file(target_zone, &lockfd) != Z_OK) { 26447c478bd9Sstevel@tonic-gate zerror(gettext("another %s may have an operation in progress."), 2645865e09a4Sgjelinek "zoneadm"); 26467c478bd9Sstevel@tonic-gate return (Z_ERR); 26477c478bd9Sstevel@tonic-gate } 26487c478bd9Sstevel@tonic-gate err = zone_set_state(target_zone, ZONE_STATE_INCOMPLETE); 26497c478bd9Sstevel@tonic-gate if (err != Z_OK) { 26507c478bd9Sstevel@tonic-gate errno = err; 26517c478bd9Sstevel@tonic-gate zperror2(target_zone, gettext("could not set state")); 26527c478bd9Sstevel@tonic-gate goto done; 26537c478bd9Sstevel@tonic-gate } 26547c478bd9Sstevel@tonic-gate 26557c478bd9Sstevel@tonic-gate /* 26567c478bd9Sstevel@tonic-gate * According to the Application Packaging Developer's Guide, a 26577c478bd9Sstevel@tonic-gate * "checkinstall" script when included in a package is executed as 26587c478bd9Sstevel@tonic-gate * the user "install", if such a user exists, or by the user 26597c478bd9Sstevel@tonic-gate * "nobody". In order to support this dubious behavior, the path 26607c478bd9Sstevel@tonic-gate * to the zone being constructed is opened up during the life of 26617c478bd9Sstevel@tonic-gate * the command laying down the zone's root file system. Once this 26627c478bd9Sstevel@tonic-gate * has completed, regardless of whether it was successful, the 26637c478bd9Sstevel@tonic-gate * path to the zone is again restricted. 26647c478bd9Sstevel@tonic-gate */ 26657c478bd9Sstevel@tonic-gate if ((err = zone_get_zonepath(target_zone, zonepath, 26667c478bd9Sstevel@tonic-gate sizeof (zonepath))) != Z_OK) { 26677c478bd9Sstevel@tonic-gate errno = err; 26687c478bd9Sstevel@tonic-gate zperror2(target_zone, gettext("could not get zone path")); 26697c478bd9Sstevel@tonic-gate goto done; 26707c478bd9Sstevel@tonic-gate } 26710b5de56dSgjelinek 26720b5de56dSgjelinek if (!nodataset) 26730b5de56dSgjelinek create_zfs_zonepath(zonepath); 26740b5de56dSgjelinek 26757c478bd9Sstevel@tonic-gate if (chmod(zonepath, DEFAULT_DIR_MODE) != 0) { 26767c478bd9Sstevel@tonic-gate zperror(zonepath, B_FALSE); 26777c478bd9Sstevel@tonic-gate err = Z_ERR; 26787c478bd9Sstevel@tonic-gate goto done; 26797c478bd9Sstevel@tonic-gate } 26807c478bd9Sstevel@tonic-gate 26817c478bd9Sstevel@tonic-gate /* 26827c478bd9Sstevel@tonic-gate * "exec" the command so that the returned status is that of 26837c478bd9Sstevel@tonic-gate * LUCREATEZONE and not the shell. 26847c478bd9Sstevel@tonic-gate */ 26857c478bd9Sstevel@tonic-gate (void) snprintf(cmdbuf, sizeof (cmdbuf), "exec " LUCREATEZONE " -z %s", 26867c478bd9Sstevel@tonic-gate target_zone); 26877c478bd9Sstevel@tonic-gate status = do_subproc(cmdbuf); 26887c478bd9Sstevel@tonic-gate if (chmod(zonepath, S_IRWXU) != 0) { 26897c478bd9Sstevel@tonic-gate zperror(zonepath, B_FALSE); 26907c478bd9Sstevel@tonic-gate err = Z_ERR; 26917c478bd9Sstevel@tonic-gate goto done; 26927c478bd9Sstevel@tonic-gate } 26937c478bd9Sstevel@tonic-gate if ((err = subproc_status(LUCREATEZONE, status)) != Z_OK) 26947c478bd9Sstevel@tonic-gate goto done; 26957c478bd9Sstevel@tonic-gate 26967c478bd9Sstevel@tonic-gate if ((err = zone_set_state(target_zone, ZONE_STATE_INSTALLED)) != Z_OK) { 26977c478bd9Sstevel@tonic-gate errno = err; 26987c478bd9Sstevel@tonic-gate zperror2(target_zone, gettext("could not set state")); 26997c478bd9Sstevel@tonic-gate goto done; 27007c478bd9Sstevel@tonic-gate } 27017c478bd9Sstevel@tonic-gate 27027c478bd9Sstevel@tonic-gate done: 27037c478bd9Sstevel@tonic-gate release_lock_file(lockfd); 27047c478bd9Sstevel@tonic-gate return ((err == Z_OK) ? Z_OK : Z_ERR); 27057c478bd9Sstevel@tonic-gate } 27067c478bd9Sstevel@tonic-gate 27077c478bd9Sstevel@tonic-gate /* 2708865e09a4Sgjelinek * Check that the inherited pkg dirs are the same for the clone and its source. 2709865e09a4Sgjelinek * The easiest way to do that is check that the list of ipds is the same 2710865e09a4Sgjelinek * by matching each one against the other. This algorithm should be fine since 2711865e09a4Sgjelinek * the list of ipds should not be that long. 2712865e09a4Sgjelinek */ 2713865e09a4Sgjelinek static int 2714865e09a4Sgjelinek valid_ipd_clone(zone_dochandle_t s_handle, char *source_zone, 2715865e09a4Sgjelinek zone_dochandle_t t_handle, char *target_zone) 2716865e09a4Sgjelinek { 2717865e09a4Sgjelinek int err; 2718865e09a4Sgjelinek int res = Z_OK; 2719865e09a4Sgjelinek int s_cnt = 0; 2720865e09a4Sgjelinek int t_cnt = 0; 2721865e09a4Sgjelinek struct zone_fstab s_fstab; 2722865e09a4Sgjelinek struct zone_fstab t_fstab; 2723865e09a4Sgjelinek 2724865e09a4Sgjelinek /* 2725865e09a4Sgjelinek * First check the source of the clone against the target. 2726865e09a4Sgjelinek */ 2727865e09a4Sgjelinek if ((err = zonecfg_setipdent(s_handle)) != Z_OK) { 2728865e09a4Sgjelinek errno = err; 2729865e09a4Sgjelinek zperror2(source_zone, gettext("could not enumerate " 2730865e09a4Sgjelinek "inherit-pkg-dirs")); 2731865e09a4Sgjelinek return (Z_ERR); 2732865e09a4Sgjelinek } 2733865e09a4Sgjelinek 2734865e09a4Sgjelinek while (zonecfg_getipdent(s_handle, &s_fstab) == Z_OK) { 2735865e09a4Sgjelinek boolean_t match = B_FALSE; 2736865e09a4Sgjelinek 2737865e09a4Sgjelinek s_cnt++; 2738865e09a4Sgjelinek 2739865e09a4Sgjelinek if ((err = zonecfg_setipdent(t_handle)) != Z_OK) { 2740865e09a4Sgjelinek errno = err; 2741865e09a4Sgjelinek zperror2(target_zone, gettext("could not enumerate " 2742865e09a4Sgjelinek "inherit-pkg-dirs")); 2743865e09a4Sgjelinek (void) zonecfg_endipdent(s_handle); 2744865e09a4Sgjelinek return (Z_ERR); 2745865e09a4Sgjelinek } 2746865e09a4Sgjelinek 2747865e09a4Sgjelinek while (zonecfg_getipdent(t_handle, &t_fstab) == Z_OK) { 2748865e09a4Sgjelinek if (strcmp(s_fstab.zone_fs_dir, t_fstab.zone_fs_dir) 2749865e09a4Sgjelinek == 0) { 2750865e09a4Sgjelinek match = B_TRUE; 2751865e09a4Sgjelinek break; 2752865e09a4Sgjelinek } 2753865e09a4Sgjelinek } 2754865e09a4Sgjelinek (void) zonecfg_endipdent(t_handle); 2755865e09a4Sgjelinek 2756865e09a4Sgjelinek if (!match) { 2757865e09a4Sgjelinek (void) fprintf(stderr, gettext("inherit-pkg-dir " 2758865e09a4Sgjelinek "'%s' is not configured in zone %s.\n"), 2759865e09a4Sgjelinek s_fstab.zone_fs_dir, target_zone); 2760865e09a4Sgjelinek res = Z_ERR; 2761865e09a4Sgjelinek } 2762865e09a4Sgjelinek } 2763865e09a4Sgjelinek 2764865e09a4Sgjelinek (void) zonecfg_endipdent(s_handle); 2765865e09a4Sgjelinek 2766865e09a4Sgjelinek /* skip the next check if we already have errors */ 2767865e09a4Sgjelinek if (res == Z_ERR) 2768865e09a4Sgjelinek return (res); 2769865e09a4Sgjelinek 2770865e09a4Sgjelinek /* 2771865e09a4Sgjelinek * Now check the number of ipds in the target so we can verify 2772865e09a4Sgjelinek * that the source is not a subset of the target. 2773865e09a4Sgjelinek */ 2774865e09a4Sgjelinek if ((err = zonecfg_setipdent(t_handle)) != Z_OK) { 2775865e09a4Sgjelinek errno = err; 2776865e09a4Sgjelinek zperror2(target_zone, gettext("could not enumerate " 2777865e09a4Sgjelinek "inherit-pkg-dirs")); 2778865e09a4Sgjelinek return (Z_ERR); 2779865e09a4Sgjelinek } 2780865e09a4Sgjelinek 2781865e09a4Sgjelinek while (zonecfg_getipdent(t_handle, &t_fstab) == Z_OK) 2782865e09a4Sgjelinek t_cnt++; 2783865e09a4Sgjelinek 2784865e09a4Sgjelinek (void) zonecfg_endipdent(t_handle); 2785865e09a4Sgjelinek 2786865e09a4Sgjelinek if (t_cnt != s_cnt) { 2787865e09a4Sgjelinek (void) fprintf(stderr, gettext("Zone %s is configured " 2788865e09a4Sgjelinek "with inherit-pkg-dirs that are not configured in zone " 2789865e09a4Sgjelinek "%s.\n"), target_zone, source_zone); 2790865e09a4Sgjelinek res = Z_ERR; 2791865e09a4Sgjelinek } 2792865e09a4Sgjelinek 2793865e09a4Sgjelinek return (res); 2794865e09a4Sgjelinek } 2795865e09a4Sgjelinek 2796865e09a4Sgjelinek static void 2797865e09a4Sgjelinek warn_dev_match(zone_dochandle_t s_handle, char *source_zone, 2798865e09a4Sgjelinek zone_dochandle_t t_handle, char *target_zone) 2799865e09a4Sgjelinek { 2800865e09a4Sgjelinek int err; 2801865e09a4Sgjelinek struct zone_devtab s_devtab; 2802865e09a4Sgjelinek struct zone_devtab t_devtab; 2803865e09a4Sgjelinek 2804865e09a4Sgjelinek if ((err = zonecfg_setdevent(t_handle)) != Z_OK) { 2805865e09a4Sgjelinek errno = err; 2806865e09a4Sgjelinek zperror2(target_zone, gettext("could not enumerate devices")); 2807865e09a4Sgjelinek return; 2808865e09a4Sgjelinek } 2809865e09a4Sgjelinek 2810865e09a4Sgjelinek while (zonecfg_getdevent(t_handle, &t_devtab) == Z_OK) { 2811865e09a4Sgjelinek if ((err = zonecfg_setdevent(s_handle)) != Z_OK) { 2812865e09a4Sgjelinek errno = err; 2813865e09a4Sgjelinek zperror2(source_zone, 2814865e09a4Sgjelinek gettext("could not enumerate devices")); 2815865e09a4Sgjelinek (void) zonecfg_enddevent(t_handle); 2816865e09a4Sgjelinek return; 2817865e09a4Sgjelinek } 2818865e09a4Sgjelinek 2819865e09a4Sgjelinek while (zonecfg_getdevent(s_handle, &s_devtab) == Z_OK) { 2820865e09a4Sgjelinek /* 2821865e09a4Sgjelinek * Use fnmatch to catch the case where wildcards 2822865e09a4Sgjelinek * were used in one zone and the other has an 2823865e09a4Sgjelinek * explicit entry (e.g. /dev/dsk/c0t0d0s6 vs. 2824865e09a4Sgjelinek * /dev/\*dsk/c0t0d0s6). 2825865e09a4Sgjelinek */ 2826865e09a4Sgjelinek if (fnmatch(t_devtab.zone_dev_match, 2827865e09a4Sgjelinek s_devtab.zone_dev_match, FNM_PATHNAME) == 0 || 2828865e09a4Sgjelinek fnmatch(s_devtab.zone_dev_match, 2829865e09a4Sgjelinek t_devtab.zone_dev_match, FNM_PATHNAME) == 0) { 2830865e09a4Sgjelinek (void) fprintf(stderr, 2831865e09a4Sgjelinek gettext("WARNING: device '%s' " 2832865e09a4Sgjelinek "is configured in both zones.\n"), 2833865e09a4Sgjelinek t_devtab.zone_dev_match); 2834865e09a4Sgjelinek break; 2835865e09a4Sgjelinek } 2836865e09a4Sgjelinek } 2837865e09a4Sgjelinek (void) zonecfg_enddevent(s_handle); 2838865e09a4Sgjelinek } 2839865e09a4Sgjelinek 2840865e09a4Sgjelinek (void) zonecfg_enddevent(t_handle); 2841865e09a4Sgjelinek } 2842865e09a4Sgjelinek 2843865e09a4Sgjelinek /* 2844865e09a4Sgjelinek * Check if the specified mount option (opt) is contained within the 2845865e09a4Sgjelinek * options string. 2846865e09a4Sgjelinek */ 2847865e09a4Sgjelinek static boolean_t 2848865e09a4Sgjelinek opt_match(char *opt, char *options) 2849865e09a4Sgjelinek { 2850865e09a4Sgjelinek char *p; 2851865e09a4Sgjelinek char *lastp; 2852865e09a4Sgjelinek 2853865e09a4Sgjelinek if ((p = strtok_r(options, ",", &lastp)) != NULL) { 2854865e09a4Sgjelinek if (strcmp(p, opt) == 0) 2855865e09a4Sgjelinek return (B_TRUE); 2856865e09a4Sgjelinek while ((p = strtok_r(NULL, ",", &lastp)) != NULL) { 2857865e09a4Sgjelinek if (strcmp(p, opt) == 0) 2858865e09a4Sgjelinek return (B_TRUE); 2859865e09a4Sgjelinek } 2860865e09a4Sgjelinek } 2861865e09a4Sgjelinek 2862865e09a4Sgjelinek return (B_FALSE); 2863865e09a4Sgjelinek } 2864865e09a4Sgjelinek 28650b5de56dSgjelinek #define RW_LOFS "WARNING: read-write lofs file system on '%s' is configured " \ 2866865e09a4Sgjelinek "in both zones.\n" 2867865e09a4Sgjelinek 2868865e09a4Sgjelinek static void 2869865e09a4Sgjelinek print_fs_warnings(struct zone_fstab *s_fstab, struct zone_fstab *t_fstab) 2870865e09a4Sgjelinek { 2871865e09a4Sgjelinek /* 2872865e09a4Sgjelinek * It is ok to have shared lofs mounted fs but we want to warn if 2873865e09a4Sgjelinek * either is rw since this will effect the other zone. 2874865e09a4Sgjelinek */ 2875865e09a4Sgjelinek if (strcmp(t_fstab->zone_fs_type, "lofs") == 0) { 2876865e09a4Sgjelinek zone_fsopt_t *optp; 2877865e09a4Sgjelinek 2878865e09a4Sgjelinek /* The default is rw so no options means rw */ 2879865e09a4Sgjelinek if (t_fstab->zone_fs_options == NULL || 2880865e09a4Sgjelinek s_fstab->zone_fs_options == NULL) { 2881865e09a4Sgjelinek (void) fprintf(stderr, gettext(RW_LOFS), 2882865e09a4Sgjelinek t_fstab->zone_fs_special); 2883865e09a4Sgjelinek return; 2884865e09a4Sgjelinek } 2885865e09a4Sgjelinek 2886865e09a4Sgjelinek for (optp = s_fstab->zone_fs_options; optp != NULL; 2887865e09a4Sgjelinek optp = optp->zone_fsopt_next) { 2888865e09a4Sgjelinek if (opt_match("rw", optp->zone_fsopt_opt)) { 2889865e09a4Sgjelinek (void) fprintf(stderr, gettext(RW_LOFS), 2890865e09a4Sgjelinek s_fstab->zone_fs_special); 2891865e09a4Sgjelinek return; 2892865e09a4Sgjelinek } 2893865e09a4Sgjelinek } 2894865e09a4Sgjelinek 2895865e09a4Sgjelinek for (optp = t_fstab->zone_fs_options; optp != NULL; 2896865e09a4Sgjelinek optp = optp->zone_fsopt_next) { 2897865e09a4Sgjelinek if (opt_match("rw", optp->zone_fsopt_opt)) { 2898865e09a4Sgjelinek (void) fprintf(stderr, gettext(RW_LOFS), 2899865e09a4Sgjelinek t_fstab->zone_fs_special); 2900865e09a4Sgjelinek return; 2901865e09a4Sgjelinek } 2902865e09a4Sgjelinek } 2903865e09a4Sgjelinek 2904865e09a4Sgjelinek return; 2905865e09a4Sgjelinek } 2906865e09a4Sgjelinek 2907865e09a4Sgjelinek /* 2908865e09a4Sgjelinek * TRANSLATION_NOTE 29090b5de56dSgjelinek * The first variable is the file system type and the second is 29100b5de56dSgjelinek * the file system special device. For example, 29110b5de56dSgjelinek * WARNING: ufs file system on '/dev/dsk/c0t0d0s0' ... 2912865e09a4Sgjelinek */ 29130b5de56dSgjelinek (void) fprintf(stderr, gettext("WARNING: %s file system on '%s' " 2914865e09a4Sgjelinek "is configured in both zones.\n"), t_fstab->zone_fs_type, 2915865e09a4Sgjelinek t_fstab->zone_fs_special); 2916865e09a4Sgjelinek } 2917865e09a4Sgjelinek 2918865e09a4Sgjelinek static void 2919865e09a4Sgjelinek warn_fs_match(zone_dochandle_t s_handle, char *source_zone, 2920865e09a4Sgjelinek zone_dochandle_t t_handle, char *target_zone) 2921865e09a4Sgjelinek { 2922865e09a4Sgjelinek int err; 2923865e09a4Sgjelinek struct zone_fstab s_fstab; 2924865e09a4Sgjelinek struct zone_fstab t_fstab; 2925865e09a4Sgjelinek 2926865e09a4Sgjelinek if ((err = zonecfg_setfsent(t_handle)) != Z_OK) { 2927865e09a4Sgjelinek errno = err; 2928865e09a4Sgjelinek zperror2(target_zone, 29290b5de56dSgjelinek gettext("could not enumerate file systems")); 2930865e09a4Sgjelinek return; 2931865e09a4Sgjelinek } 2932865e09a4Sgjelinek 2933865e09a4Sgjelinek while (zonecfg_getfsent(t_handle, &t_fstab) == Z_OK) { 2934865e09a4Sgjelinek if ((err = zonecfg_setfsent(s_handle)) != Z_OK) { 2935865e09a4Sgjelinek errno = err; 2936865e09a4Sgjelinek zperror2(source_zone, 29370b5de56dSgjelinek gettext("could not enumerate file systems")); 2938865e09a4Sgjelinek (void) zonecfg_endfsent(t_handle); 2939865e09a4Sgjelinek return; 2940865e09a4Sgjelinek } 2941865e09a4Sgjelinek 2942865e09a4Sgjelinek while (zonecfg_getfsent(s_handle, &s_fstab) == Z_OK) { 2943865e09a4Sgjelinek if (strcmp(t_fstab.zone_fs_special, 2944865e09a4Sgjelinek s_fstab.zone_fs_special) == 0) { 2945865e09a4Sgjelinek print_fs_warnings(&s_fstab, &t_fstab); 2946865e09a4Sgjelinek break; 2947865e09a4Sgjelinek } 2948865e09a4Sgjelinek } 2949865e09a4Sgjelinek (void) zonecfg_endfsent(s_handle); 2950865e09a4Sgjelinek } 2951865e09a4Sgjelinek 2952865e09a4Sgjelinek (void) zonecfg_endfsent(t_handle); 2953865e09a4Sgjelinek } 2954865e09a4Sgjelinek 2955865e09a4Sgjelinek /* 2956865e09a4Sgjelinek * We don't catch the case where you used the same IP address but 2957865e09a4Sgjelinek * it is not an exact string match. For example, 192.9.0.128 vs. 192.09.0.128. 2958865e09a4Sgjelinek * However, we're not going to worry about that but we will check for 2959865e09a4Sgjelinek * a possible netmask on one of the addresses (e.g. 10.0.0.1 and 10.0.0.1/24) 2960865e09a4Sgjelinek * and handle that case as a match. 2961865e09a4Sgjelinek */ 2962865e09a4Sgjelinek static void 2963865e09a4Sgjelinek warn_ip_match(zone_dochandle_t s_handle, char *source_zone, 2964865e09a4Sgjelinek zone_dochandle_t t_handle, char *target_zone) 2965865e09a4Sgjelinek { 2966865e09a4Sgjelinek int err; 2967865e09a4Sgjelinek struct zone_nwiftab s_nwiftab; 2968865e09a4Sgjelinek struct zone_nwiftab t_nwiftab; 2969865e09a4Sgjelinek 2970865e09a4Sgjelinek if ((err = zonecfg_setnwifent(t_handle)) != Z_OK) { 2971865e09a4Sgjelinek errno = err; 2972865e09a4Sgjelinek zperror2(target_zone, 2973865e09a4Sgjelinek gettext("could not enumerate network interfaces")); 2974865e09a4Sgjelinek return; 2975865e09a4Sgjelinek } 2976865e09a4Sgjelinek 2977865e09a4Sgjelinek while (zonecfg_getnwifent(t_handle, &t_nwiftab) == Z_OK) { 2978865e09a4Sgjelinek char *p; 2979865e09a4Sgjelinek 2980865e09a4Sgjelinek /* remove an (optional) netmask from the address */ 2981865e09a4Sgjelinek if ((p = strchr(t_nwiftab.zone_nwif_address, '/')) != NULL) 2982865e09a4Sgjelinek *p = '\0'; 2983865e09a4Sgjelinek 2984865e09a4Sgjelinek if ((err = zonecfg_setnwifent(s_handle)) != Z_OK) { 2985865e09a4Sgjelinek errno = err; 2986865e09a4Sgjelinek zperror2(source_zone, 2987865e09a4Sgjelinek gettext("could not enumerate network interfaces")); 2988865e09a4Sgjelinek (void) zonecfg_endnwifent(t_handle); 2989865e09a4Sgjelinek return; 2990865e09a4Sgjelinek } 2991865e09a4Sgjelinek 2992865e09a4Sgjelinek while (zonecfg_getnwifent(s_handle, &s_nwiftab) == Z_OK) { 2993865e09a4Sgjelinek /* remove an (optional) netmask from the address */ 2994865e09a4Sgjelinek if ((p = strchr(s_nwiftab.zone_nwif_address, '/')) 2995865e09a4Sgjelinek != NULL) 2996865e09a4Sgjelinek *p = '\0'; 2997865e09a4Sgjelinek 2998865e09a4Sgjelinek if (strcmp(t_nwiftab.zone_nwif_address, 2999865e09a4Sgjelinek s_nwiftab.zone_nwif_address) == 0) { 3000865e09a4Sgjelinek (void) fprintf(stderr, 3001865e09a4Sgjelinek gettext("WARNING: network address '%s' " 3002865e09a4Sgjelinek "is configured in both zones.\n"), 3003865e09a4Sgjelinek t_nwiftab.zone_nwif_address); 3004865e09a4Sgjelinek break; 3005865e09a4Sgjelinek } 3006865e09a4Sgjelinek } 3007865e09a4Sgjelinek (void) zonecfg_endnwifent(s_handle); 3008865e09a4Sgjelinek } 3009865e09a4Sgjelinek 3010865e09a4Sgjelinek (void) zonecfg_endnwifent(t_handle); 3011865e09a4Sgjelinek } 3012865e09a4Sgjelinek 3013865e09a4Sgjelinek static void 3014865e09a4Sgjelinek warn_dataset_match(zone_dochandle_t s_handle, char *source_zone, 3015865e09a4Sgjelinek zone_dochandle_t t_handle, char *target_zone) 3016865e09a4Sgjelinek { 3017865e09a4Sgjelinek int err; 3018865e09a4Sgjelinek struct zone_dstab s_dstab; 3019865e09a4Sgjelinek struct zone_dstab t_dstab; 3020865e09a4Sgjelinek 3021865e09a4Sgjelinek if ((err = zonecfg_setdsent(t_handle)) != Z_OK) { 3022865e09a4Sgjelinek errno = err; 3023865e09a4Sgjelinek zperror2(target_zone, gettext("could not enumerate datasets")); 3024865e09a4Sgjelinek return; 3025865e09a4Sgjelinek } 3026865e09a4Sgjelinek 3027865e09a4Sgjelinek while (zonecfg_getdsent(t_handle, &t_dstab) == Z_OK) { 3028865e09a4Sgjelinek if ((err = zonecfg_setdsent(s_handle)) != Z_OK) { 3029865e09a4Sgjelinek errno = err; 3030865e09a4Sgjelinek zperror2(source_zone, 3031865e09a4Sgjelinek gettext("could not enumerate datasets")); 3032865e09a4Sgjelinek (void) zonecfg_enddsent(t_handle); 3033865e09a4Sgjelinek return; 3034865e09a4Sgjelinek } 3035865e09a4Sgjelinek 3036865e09a4Sgjelinek while (zonecfg_getdsent(s_handle, &s_dstab) == Z_OK) { 3037865e09a4Sgjelinek if (strcmp(t_dstab.zone_dataset_name, 3038865e09a4Sgjelinek s_dstab.zone_dataset_name) == 0) { 3039865e09a4Sgjelinek (void) fprintf(stderr, 3040865e09a4Sgjelinek gettext("WARNING: dataset '%s' " 3041865e09a4Sgjelinek "is configured in both zones.\n"), 3042865e09a4Sgjelinek t_dstab.zone_dataset_name); 3043865e09a4Sgjelinek break; 3044865e09a4Sgjelinek } 3045865e09a4Sgjelinek } 3046865e09a4Sgjelinek (void) zonecfg_enddsent(s_handle); 3047865e09a4Sgjelinek } 3048865e09a4Sgjelinek 3049865e09a4Sgjelinek (void) zonecfg_enddsent(t_handle); 3050865e09a4Sgjelinek } 3051865e09a4Sgjelinek 3052865e09a4Sgjelinek static int 3053865e09a4Sgjelinek validate_clone(char *source_zone, char *target_zone) 3054865e09a4Sgjelinek { 3055865e09a4Sgjelinek int err = Z_OK; 3056865e09a4Sgjelinek zone_dochandle_t s_handle; 3057865e09a4Sgjelinek zone_dochandle_t t_handle; 3058865e09a4Sgjelinek 3059865e09a4Sgjelinek if ((t_handle = zonecfg_init_handle()) == NULL) { 3060865e09a4Sgjelinek zperror(cmd_to_str(CMD_CLONE), B_TRUE); 3061865e09a4Sgjelinek return (Z_ERR); 3062865e09a4Sgjelinek } 3063865e09a4Sgjelinek if ((err = zonecfg_get_handle(target_zone, t_handle)) != Z_OK) { 3064865e09a4Sgjelinek errno = err; 3065865e09a4Sgjelinek zperror(cmd_to_str(CMD_CLONE), B_TRUE); 3066865e09a4Sgjelinek zonecfg_fini_handle(t_handle); 3067865e09a4Sgjelinek return (Z_ERR); 3068865e09a4Sgjelinek } 3069865e09a4Sgjelinek 3070865e09a4Sgjelinek if ((s_handle = zonecfg_init_handle()) == NULL) { 3071865e09a4Sgjelinek zperror(cmd_to_str(CMD_CLONE), B_TRUE); 3072865e09a4Sgjelinek zonecfg_fini_handle(t_handle); 3073865e09a4Sgjelinek return (Z_ERR); 3074865e09a4Sgjelinek } 3075865e09a4Sgjelinek if ((err = zonecfg_get_handle(source_zone, s_handle)) != Z_OK) { 3076865e09a4Sgjelinek errno = err; 3077865e09a4Sgjelinek zperror(cmd_to_str(CMD_CLONE), B_TRUE); 3078865e09a4Sgjelinek goto done; 3079865e09a4Sgjelinek } 3080865e09a4Sgjelinek 3081865e09a4Sgjelinek /* verify new zone has same inherit-pkg-dirs */ 3082865e09a4Sgjelinek err = valid_ipd_clone(s_handle, source_zone, t_handle, target_zone); 3083865e09a4Sgjelinek 3084865e09a4Sgjelinek /* warn about imported fs's which are the same */ 3085865e09a4Sgjelinek warn_fs_match(s_handle, source_zone, t_handle, target_zone); 3086865e09a4Sgjelinek 3087865e09a4Sgjelinek /* warn about imported IP addresses which are the same */ 3088865e09a4Sgjelinek warn_ip_match(s_handle, source_zone, t_handle, target_zone); 3089865e09a4Sgjelinek 3090865e09a4Sgjelinek /* warn about imported devices which are the same */ 3091865e09a4Sgjelinek warn_dev_match(s_handle, source_zone, t_handle, target_zone); 3092865e09a4Sgjelinek 3093865e09a4Sgjelinek /* warn about imported datasets which are the same */ 3094865e09a4Sgjelinek warn_dataset_match(s_handle, source_zone, t_handle, target_zone); 3095865e09a4Sgjelinek 3096865e09a4Sgjelinek done: 3097865e09a4Sgjelinek zonecfg_fini_handle(t_handle); 3098865e09a4Sgjelinek zonecfg_fini_handle(s_handle); 3099865e09a4Sgjelinek 3100865e09a4Sgjelinek return ((err == Z_OK) ? Z_OK : Z_ERR); 3101865e09a4Sgjelinek } 3102865e09a4Sgjelinek 3103865e09a4Sgjelinek static int 3104865e09a4Sgjelinek copy_zone(char *src, char *dst) 3105865e09a4Sgjelinek { 3106865e09a4Sgjelinek boolean_t out_null = B_FALSE; 3107865e09a4Sgjelinek int status; 3108865e09a4Sgjelinek int err; 3109865e09a4Sgjelinek char *outfile; 3110865e09a4Sgjelinek char cmdbuf[MAXPATHLEN * 2 + 128]; 3111865e09a4Sgjelinek 3112865e09a4Sgjelinek if ((outfile = tempnam("/var/log", "zone")) == NULL) { 3113865e09a4Sgjelinek outfile = "/dev/null"; 3114865e09a4Sgjelinek out_null = B_TRUE; 3115865e09a4Sgjelinek } 3116865e09a4Sgjelinek 31170b5de56dSgjelinek /* 31180b5de56dSgjelinek * Use find to get the list of files to copy. We need to skip 31190b5de56dSgjelinek * files of type "socket" since cpio can't handle those but that 31200b5de56dSgjelinek * should be ok since the app will recreate the socket when it runs. 31210b5de56dSgjelinek * We also need to filter out anything under the .zfs subdir. Since 31220b5de56dSgjelinek * find is running depth-first, we need the extra egrep to filter .zfs. 31230b5de56dSgjelinek */ 3124865e09a4Sgjelinek (void) snprintf(cmdbuf, sizeof (cmdbuf), 31250b5de56dSgjelinek "cd %s && /usr/bin/find . -type s -prune -o -depth -print | " 312607b574eeSgjelinek "/usr/bin/egrep -v '^\\./\\.zfs$|^\\./\\.zfs/' | " 3127865e09a4Sgjelinek "/usr/bin/cpio -pdmuP@ %s > %s 2>&1", 3128865e09a4Sgjelinek src, dst, outfile); 3129865e09a4Sgjelinek 3130865e09a4Sgjelinek status = do_subproc(cmdbuf); 3131865e09a4Sgjelinek 3132865e09a4Sgjelinek if ((err = subproc_status("copy", status)) != Z_OK) { 3133865e09a4Sgjelinek if (!out_null) 3134865e09a4Sgjelinek (void) fprintf(stderr, gettext("\nThe copy failed.\n" 3135865e09a4Sgjelinek "More information can be found in %s\n"), outfile); 3136865e09a4Sgjelinek return (err); 3137865e09a4Sgjelinek } 3138865e09a4Sgjelinek 3139865e09a4Sgjelinek if (!out_null) 3140865e09a4Sgjelinek (void) unlink(outfile); 3141865e09a4Sgjelinek 3142865e09a4Sgjelinek return (Z_OK); 3143865e09a4Sgjelinek } 3144865e09a4Sgjelinek 3145865e09a4Sgjelinek /* 31465cd08338Sgjelinek * Run sys-unconfig on a zone. This will leave the zone in the installed 31475cd08338Sgjelinek * state as long as there were no errors during the sys-unconfig. 3148865e09a4Sgjelinek */ 3149865e09a4Sgjelinek static int 31505cd08338Sgjelinek unconfigure_zone(char *zonepath) 3151865e09a4Sgjelinek { 31525cd08338Sgjelinek int err; 3153865e09a4Sgjelinek int status; 31545cd08338Sgjelinek struct stat unconfig_buf; 31555cd08338Sgjelinek zone_cmd_arg_t zarg; 31565cd08338Sgjelinek char cmdbuf[MAXPATHLEN + 51]; 3157865e09a4Sgjelinek 31585cd08338Sgjelinek /* The zone has to be installed in order to mount the scratch zone. */ 31595cd08338Sgjelinek if ((err = zone_set_state(target_zone, ZONE_STATE_INSTALLED)) != Z_OK) { 31605cd08338Sgjelinek errno = err; 31615cd08338Sgjelinek zperror2(target_zone, gettext("could not set state")); 31625cd08338Sgjelinek return (Z_ERR); 31635cd08338Sgjelinek } 31645cd08338Sgjelinek 31655cd08338Sgjelinek /* 316645916cd2Sjpk * Trusted Extensions requires that cloned zones use the 316745916cd2Sjpk * same sysid configuration, so it is not appropriate to 316845916cd2Sjpk * unconfigure the zone. 316945916cd2Sjpk */ 317045916cd2Sjpk if (is_system_labeled()) 317145916cd2Sjpk return (Z_OK); 317245916cd2Sjpk 317345916cd2Sjpk /* 31745cd08338Sgjelinek * Check if the zone is already sys-unconfiged. This saves us 31755cd08338Sgjelinek * the work of bringing up the scratch zone so we can unconfigure it. 31765cd08338Sgjelinek */ 31775cd08338Sgjelinek (void) snprintf(cmdbuf, sizeof (cmdbuf), "%s/root/etc/.UNCONFIGURED", 31785cd08338Sgjelinek zonepath); 31795cd08338Sgjelinek if (stat(cmdbuf, &unconfig_buf) == 0) 3180865e09a4Sgjelinek return (Z_OK); 3181865e09a4Sgjelinek 31825cd08338Sgjelinek zarg.cmd = Z_MOUNT; 31835cd08338Sgjelinek if (call_zoneadmd(target_zone, &zarg) != 0) { 31845cd08338Sgjelinek zerror(gettext("call to %s failed"), "zoneadmd"); 31855cd08338Sgjelinek (void) zone_set_state(target_zone, ZONE_STATE_INCOMPLETE); 31865cd08338Sgjelinek return (Z_ERR); 3187865e09a4Sgjelinek } 3188865e09a4Sgjelinek 31895cd08338Sgjelinek (void) snprintf(cmdbuf, sizeof (cmdbuf), 31905cd08338Sgjelinek "/usr/sbin/zlogin -S %s /usr/sbin/sys-unconfig -R /a", target_zone); 31915cd08338Sgjelinek 31925cd08338Sgjelinek status = do_subproc(cmdbuf); 31935cd08338Sgjelinek if ((err = subproc_status("sys-unconfig", status)) != Z_OK) { 31945cd08338Sgjelinek errno = err; 31955cd08338Sgjelinek zperror2(target_zone, gettext("sys-unconfig failed\n")); 31965cd08338Sgjelinek (void) zone_set_state(target_zone, ZONE_STATE_INCOMPLETE); 3197865e09a4Sgjelinek } 3198865e09a4Sgjelinek 31995cd08338Sgjelinek zarg.cmd = Z_UNMOUNT; 32005cd08338Sgjelinek if (call_zoneadmd(target_zone, &zarg) != 0) { 32015cd08338Sgjelinek zerror(gettext("call to %s failed"), "zoneadmd"); 32025cd08338Sgjelinek (void) fprintf(stderr, gettext("could not unmount zone\n")); 32035cd08338Sgjelinek return (Z_ERR); 32045cd08338Sgjelinek } 32055cd08338Sgjelinek 32065cd08338Sgjelinek return ((err == Z_OK) ? Z_OK : Z_ERR); 32075cd08338Sgjelinek } 3208865e09a4Sgjelinek 3209865e09a4Sgjelinek /* ARGSUSED */ 32100b5de56dSgjelinek static int 3211865e09a4Sgjelinek zfm_print(const char *p, void *r) { 3212865e09a4Sgjelinek zerror(" %s\n", p); 3213865e09a4Sgjelinek return (0); 3214865e09a4Sgjelinek } 3215865e09a4Sgjelinek 32160b5de56dSgjelinek int 32170b5de56dSgjelinek clone_copy(char *source_zonepath, char *zonepath) 32180b5de56dSgjelinek { 32190b5de56dSgjelinek int err; 32200b5de56dSgjelinek 32210b5de56dSgjelinek /* Don't clone the zone if anything is still mounted there */ 32220b5de56dSgjelinek if (zonecfg_find_mounts(source_zonepath, NULL, NULL)) { 32230b5de56dSgjelinek zerror(gettext("These file systems are mounted on " 32240b5de56dSgjelinek "subdirectories of %s.\n"), source_zonepath); 32250b5de56dSgjelinek (void) zonecfg_find_mounts(source_zonepath, zfm_print, NULL); 32260b5de56dSgjelinek return (Z_ERR); 32270b5de56dSgjelinek } 32280b5de56dSgjelinek 32290b5de56dSgjelinek /* 32300b5de56dSgjelinek * Attempt to create a ZFS fs for the zonepath. As usual, we don't 32310b5de56dSgjelinek * care if this works or not since we always have the default behavior 32320b5de56dSgjelinek * of a simple directory for the zonepath. 32330b5de56dSgjelinek */ 32340b5de56dSgjelinek create_zfs_zonepath(zonepath); 32350b5de56dSgjelinek 32360b5de56dSgjelinek (void) printf(gettext("Copying %s..."), source_zonepath); 32370b5de56dSgjelinek (void) fflush(stdout); 32380b5de56dSgjelinek 32390b5de56dSgjelinek err = copy_zone(source_zonepath, zonepath); 32400b5de56dSgjelinek 32410b5de56dSgjelinek (void) printf("\n"); 32420b5de56dSgjelinek 32430b5de56dSgjelinek return (err); 32440b5de56dSgjelinek } 32450b5de56dSgjelinek 3246865e09a4Sgjelinek static int 3247865e09a4Sgjelinek clone_func(int argc, char *argv[]) 3248865e09a4Sgjelinek { 3249865e09a4Sgjelinek char *source_zone = NULL; 3250865e09a4Sgjelinek int lockfd; 3251865e09a4Sgjelinek int err, arg; 3252865e09a4Sgjelinek char zonepath[MAXPATHLEN]; 3253865e09a4Sgjelinek char source_zonepath[MAXPATHLEN]; 3254865e09a4Sgjelinek zone_state_t state; 3255865e09a4Sgjelinek zone_entry_t *zent; 32560b5de56dSgjelinek char *method = NULL; 32570b5de56dSgjelinek char *snapshot = NULL; 3258865e09a4Sgjelinek 3259865e09a4Sgjelinek if (zonecfg_in_alt_root()) { 3260865e09a4Sgjelinek zerror(gettext("cannot clone zone in alternate root")); 3261865e09a4Sgjelinek return (Z_ERR); 3262865e09a4Sgjelinek } 3263865e09a4Sgjelinek 3264865e09a4Sgjelinek optind = 0; 32650b5de56dSgjelinek if ((arg = getopt(argc, argv, "?m:s:")) != EOF) { 3266865e09a4Sgjelinek switch (arg) { 3267865e09a4Sgjelinek case '?': 3268865e09a4Sgjelinek sub_usage(SHELP_CLONE, CMD_CLONE); 3269865e09a4Sgjelinek return (optopt == '?' ? Z_OK : Z_USAGE); 3270865e09a4Sgjelinek case 'm': 3271865e09a4Sgjelinek method = optarg; 3272865e09a4Sgjelinek break; 32730b5de56dSgjelinek case 's': 32740b5de56dSgjelinek snapshot = optarg; 32750b5de56dSgjelinek break; 3276865e09a4Sgjelinek default: 3277865e09a4Sgjelinek sub_usage(SHELP_CLONE, CMD_CLONE); 3278865e09a4Sgjelinek return (Z_USAGE); 3279865e09a4Sgjelinek } 3280865e09a4Sgjelinek } 32810b5de56dSgjelinek if (argc != (optind + 1) || 32820b5de56dSgjelinek (method != NULL && strcmp(method, "copy") != 0)) { 3283865e09a4Sgjelinek sub_usage(SHELP_CLONE, CMD_CLONE); 3284865e09a4Sgjelinek return (Z_USAGE); 3285865e09a4Sgjelinek } 3286865e09a4Sgjelinek source_zone = argv[optind]; 3287865e09a4Sgjelinek if (sanity_check(target_zone, CMD_CLONE, B_FALSE, B_TRUE) != Z_OK) 3288865e09a4Sgjelinek return (Z_ERR); 3289865e09a4Sgjelinek if (verify_details(CMD_CLONE) != Z_OK) 3290865e09a4Sgjelinek return (Z_ERR); 3291865e09a4Sgjelinek 3292865e09a4Sgjelinek /* 3293865e09a4Sgjelinek * We also need to do some extra validation on the source zone. 3294865e09a4Sgjelinek */ 3295865e09a4Sgjelinek if (strcmp(source_zone, GLOBAL_ZONENAME) == 0) { 3296865e09a4Sgjelinek zerror(gettext("%s operation is invalid for the global zone."), 3297865e09a4Sgjelinek cmd_to_str(CMD_CLONE)); 3298865e09a4Sgjelinek return (Z_ERR); 3299865e09a4Sgjelinek } 3300865e09a4Sgjelinek 3301865e09a4Sgjelinek if (strncmp(source_zone, "SUNW", 4) == 0) { 3302865e09a4Sgjelinek zerror(gettext("%s operation is invalid for zones starting " 3303865e09a4Sgjelinek "with SUNW."), cmd_to_str(CMD_CLONE)); 3304865e09a4Sgjelinek return (Z_ERR); 3305865e09a4Sgjelinek } 3306865e09a4Sgjelinek 3307865e09a4Sgjelinek zent = lookup_running_zone(source_zone); 3308865e09a4Sgjelinek if (zent != NULL) { 3309865e09a4Sgjelinek /* check whether the zone is ready or running */ 3310865e09a4Sgjelinek if ((err = zone_get_state(zent->zname, &zent->zstate_num)) 3311865e09a4Sgjelinek != Z_OK) { 3312865e09a4Sgjelinek errno = err; 3313865e09a4Sgjelinek zperror2(zent->zname, gettext("could not get state")); 3314865e09a4Sgjelinek /* can't tell, so hedge */ 3315865e09a4Sgjelinek zent->zstate_str = "ready/running"; 3316865e09a4Sgjelinek } else { 3317865e09a4Sgjelinek zent->zstate_str = zone_state_str(zent->zstate_num); 3318865e09a4Sgjelinek } 3319865e09a4Sgjelinek zerror(gettext("%s operation is invalid for %s zones."), 3320865e09a4Sgjelinek cmd_to_str(CMD_CLONE), zent->zstate_str); 3321865e09a4Sgjelinek return (Z_ERR); 3322865e09a4Sgjelinek } 3323865e09a4Sgjelinek 3324865e09a4Sgjelinek if ((err = zone_get_state(source_zone, &state)) != Z_OK) { 3325865e09a4Sgjelinek errno = err; 3326865e09a4Sgjelinek zperror2(source_zone, gettext("could not get state")); 3327865e09a4Sgjelinek return (Z_ERR); 3328865e09a4Sgjelinek } 3329865e09a4Sgjelinek if (state != ZONE_STATE_INSTALLED) { 3330865e09a4Sgjelinek (void) fprintf(stderr, 3331865e09a4Sgjelinek gettext("%s: zone %s is %s; %s is required.\n"), 3332865e09a4Sgjelinek execname, source_zone, zone_state_str(state), 3333865e09a4Sgjelinek zone_state_str(ZONE_STATE_INSTALLED)); 3334865e09a4Sgjelinek return (Z_ERR); 3335865e09a4Sgjelinek } 3336865e09a4Sgjelinek 3337865e09a4Sgjelinek /* 3338865e09a4Sgjelinek * The source zone checks out ok, continue with the clone. 3339865e09a4Sgjelinek */ 3340865e09a4Sgjelinek 3341865e09a4Sgjelinek if (validate_clone(source_zone, target_zone) != Z_OK) 3342865e09a4Sgjelinek return (Z_ERR); 3343865e09a4Sgjelinek 3344865e09a4Sgjelinek if (grab_lock_file(target_zone, &lockfd) != Z_OK) { 3345865e09a4Sgjelinek zerror(gettext("another %s may have an operation in progress."), 3346865e09a4Sgjelinek "zoneadm"); 3347865e09a4Sgjelinek return (Z_ERR); 3348865e09a4Sgjelinek } 3349865e09a4Sgjelinek 3350865e09a4Sgjelinek if ((err = zone_get_zonepath(source_zone, source_zonepath, 3351865e09a4Sgjelinek sizeof (source_zonepath))) != Z_OK) { 3352865e09a4Sgjelinek errno = err; 3353865e09a4Sgjelinek zperror2(source_zone, gettext("could not get zone path")); 3354865e09a4Sgjelinek goto done; 3355865e09a4Sgjelinek } 3356865e09a4Sgjelinek 3357865e09a4Sgjelinek if ((err = zone_get_zonepath(target_zone, zonepath, sizeof (zonepath))) 3358865e09a4Sgjelinek != Z_OK) { 3359865e09a4Sgjelinek errno = err; 3360865e09a4Sgjelinek zperror2(target_zone, gettext("could not get zone path")); 3361865e09a4Sgjelinek goto done; 3362865e09a4Sgjelinek } 3363865e09a4Sgjelinek 3364865e09a4Sgjelinek if ((err = zone_set_state(target_zone, ZONE_STATE_INCOMPLETE)) 3365865e09a4Sgjelinek != Z_OK) { 3366865e09a4Sgjelinek errno = err; 3367865e09a4Sgjelinek zperror2(target_zone, gettext("could not set state")); 3368865e09a4Sgjelinek goto done; 3369865e09a4Sgjelinek } 3370865e09a4Sgjelinek 33710b5de56dSgjelinek if (snapshot != NULL) { 33720b5de56dSgjelinek err = clone_snapshot_zfs(snapshot, zonepath); 33730b5de56dSgjelinek } else { 33740b5de56dSgjelinek /* 33750b5de56dSgjelinek * We always copy the clone unless the source is ZFS and a 33760b5de56dSgjelinek * ZFS clone worked. We fallback to copying if the ZFS clone 33770b5de56dSgjelinek * fails for some reason. 33780b5de56dSgjelinek */ 33790b5de56dSgjelinek err = Z_ERR; 33800b5de56dSgjelinek if (method == NULL && is_zonepath_zfs(source_zonepath)) 33810b5de56dSgjelinek err = clone_zfs(source_zone, source_zonepath, zonepath); 3382865e09a4Sgjelinek 33835cd08338Sgjelinek if (err != Z_OK) 33840b5de56dSgjelinek err = clone_copy(source_zonepath, zonepath); 33850b5de56dSgjelinek } 3386865e09a4Sgjelinek 33870b5de56dSgjelinek if (err == Z_OK) 33885cd08338Sgjelinek err = unconfigure_zone(zonepath); 3389865e09a4Sgjelinek 3390865e09a4Sgjelinek done: 3391865e09a4Sgjelinek release_lock_file(lockfd); 3392865e09a4Sgjelinek return ((err == Z_OK) ? Z_OK : Z_ERR); 3393865e09a4Sgjelinek } 3394865e09a4Sgjelinek 3395865e09a4Sgjelinek #define RMCOMMAND "/usr/bin/rm -rf" 3396865e09a4Sgjelinek 339707b574eeSgjelinek /* 33980b5de56dSgjelinek * Used when removing a zonepath after uninstalling or cleaning up after 33990b5de56dSgjelinek * the move subcommand. This handles a zonepath that has non-standard 34000b5de56dSgjelinek * contents so that we will only cleanup the stuff we know about and leave 34010b5de56dSgjelinek * any user data alone. 340207b574eeSgjelinek * 34030b5de56dSgjelinek * If the "all" parameter is true then we should remove the whole zonepath 34040b5de56dSgjelinek * even if it has non-standard files/directories in it. This can be used when 34050b5de56dSgjelinek * we need to cleanup after moving the zonepath across file systems. 34060b5de56dSgjelinek * 34070b5de56dSgjelinek * We "exec" the RMCOMMAND so that the returned status is that of RMCOMMAND 34080b5de56dSgjelinek * and not the shell. 340907b574eeSgjelinek */ 341007b574eeSgjelinek static int 34110b5de56dSgjelinek cleanup_zonepath(char *zonepath, boolean_t all) 341207b574eeSgjelinek { 341307b574eeSgjelinek int status; 34140b5de56dSgjelinek int i; 34150b5de56dSgjelinek boolean_t non_std = B_FALSE; 34160b5de56dSgjelinek struct dirent *dp; 34170b5de56dSgjelinek DIR *dirp; 34180b5de56dSgjelinek char *std_entries[] = {"dev", "lu", "root", NULL}; 34190b5de56dSgjelinek /* (MAXPATHLEN * 3) is for the 3 std_entries dirs */ 34200b5de56dSgjelinek char cmdbuf[sizeof (RMCOMMAND) + (MAXPATHLEN * 3) + 64]; 342107b574eeSgjelinek 342207b574eeSgjelinek /* 34230b5de56dSgjelinek * We shouldn't need these checks but lets be paranoid since we 34240b5de56dSgjelinek * could blow away the whole system here if we got the wrong zonepath. 342507b574eeSgjelinek */ 34260b5de56dSgjelinek if (*zonepath == NULL || strcmp(zonepath, "/") == 0) { 34270b5de56dSgjelinek (void) fprintf(stderr, "invalid zonepath '%s'\n", zonepath); 34280b5de56dSgjelinek return (Z_INVAL); 34290b5de56dSgjelinek } 34300b5de56dSgjelinek 343107b574eeSgjelinek /* 34320b5de56dSgjelinek * If the dirpath is already gone (maybe it was manually removed) then 34330b5de56dSgjelinek * we just return Z_OK so that the cleanup is successful. 343407b574eeSgjelinek */ 34350b5de56dSgjelinek if ((dirp = opendir(zonepath)) == NULL) 34360b5de56dSgjelinek return (Z_OK); 34370b5de56dSgjelinek 34380b5de56dSgjelinek /* 34390b5de56dSgjelinek * Look through the zonepath directory to see if there are any 34400b5de56dSgjelinek * non-standard files/dirs. Also skip .zfs since that might be 34410b5de56dSgjelinek * there but we'll handle ZFS file systems as a special case. 34420b5de56dSgjelinek */ 34430b5de56dSgjelinek while ((dp = readdir(dirp)) != NULL) { 34440b5de56dSgjelinek if (strcmp(dp->d_name, ".") == 0 || 34450b5de56dSgjelinek strcmp(dp->d_name, "..") == 0 || 34460b5de56dSgjelinek strcmp(dp->d_name, ".zfs") == 0) 34470b5de56dSgjelinek continue; 34480b5de56dSgjelinek 34490b5de56dSgjelinek for (i = 0; std_entries[i] != NULL; i++) 34500b5de56dSgjelinek if (strcmp(dp->d_name, std_entries[i]) == 0) 34510b5de56dSgjelinek break; 34520b5de56dSgjelinek 34530b5de56dSgjelinek if (std_entries[i] == NULL) 34540b5de56dSgjelinek non_std = B_TRUE; 34550b5de56dSgjelinek } 34560b5de56dSgjelinek (void) closedir(dirp); 34570b5de56dSgjelinek 34580b5de56dSgjelinek if (!all && non_std) { 34590b5de56dSgjelinek /* 34600b5de56dSgjelinek * There are extra, non-standard directories/files in the 34610b5de56dSgjelinek * zonepath so we don't want to remove the zonepath. We 34620b5de56dSgjelinek * just want to remove the standard directories and leave 34630b5de56dSgjelinek * the user data alone. 34640b5de56dSgjelinek */ 34650b5de56dSgjelinek (void) snprintf(cmdbuf, sizeof (cmdbuf), "exec " RMCOMMAND); 34660b5de56dSgjelinek 34670b5de56dSgjelinek for (i = 0; std_entries[i] != NULL; i++) { 34680b5de56dSgjelinek char tmpbuf[MAXPATHLEN]; 34690b5de56dSgjelinek 34700b5de56dSgjelinek if (snprintf(tmpbuf, sizeof (tmpbuf), " %s/%s", 34710b5de56dSgjelinek zonepath, std_entries[i]) >= sizeof (tmpbuf) || 34720b5de56dSgjelinek strlcat(cmdbuf, tmpbuf, sizeof (cmdbuf)) >= 34730b5de56dSgjelinek sizeof (cmdbuf)) { 34740b5de56dSgjelinek (void) fprintf(stderr, 34750b5de56dSgjelinek gettext("path is too long\n")); 34760b5de56dSgjelinek return (Z_INVAL); 34770b5de56dSgjelinek } 347807b574eeSgjelinek } 347907b574eeSgjelinek 348007b574eeSgjelinek status = do_subproc(cmdbuf); 348107b574eeSgjelinek 34820b5de56dSgjelinek (void) fprintf(stderr, gettext("WARNING: Unable to completely " 34830b5de56dSgjelinek "remove %s\nbecause it contains additional user data. " 34840b5de56dSgjelinek "Only the standard directory\nentries have been " 34850b5de56dSgjelinek "removed.\n"), 34860b5de56dSgjelinek zonepath); 348707b574eeSgjelinek 34880b5de56dSgjelinek return (subproc_status(RMCOMMAND, status)); 34890b5de56dSgjelinek } 34900b5de56dSgjelinek 34910b5de56dSgjelinek /* 34920b5de56dSgjelinek * There is nothing unexpected in the zonepath, try to get rid of the 34930b5de56dSgjelinek * whole zonepath directory. 34940b5de56dSgjelinek * 34950b5de56dSgjelinek * If the zonepath is its own zfs file system, try to destroy the 34960b5de56dSgjelinek * file system. If that fails for some reason (e.g. it has clones) 34970b5de56dSgjelinek * then we'll just remove the contents of the zonepath. 34980b5de56dSgjelinek */ 34990b5de56dSgjelinek if (is_zonepath_zfs(zonepath)) { 35000b5de56dSgjelinek if (destroy_zfs(zonepath) == Z_OK) 35010b5de56dSgjelinek return (Z_OK); 35020b5de56dSgjelinek (void) snprintf(cmdbuf, sizeof (cmdbuf), "exec " RMCOMMAND 35030b5de56dSgjelinek " %s/*", zonepath); 35040b5de56dSgjelinek status = do_subproc(cmdbuf); 35050b5de56dSgjelinek return (subproc_status(RMCOMMAND, status)); 35060b5de56dSgjelinek } 35070b5de56dSgjelinek 35080b5de56dSgjelinek (void) snprintf(cmdbuf, sizeof (cmdbuf), "exec " RMCOMMAND " %s", 35090b5de56dSgjelinek zonepath); 35100b5de56dSgjelinek status = do_subproc(cmdbuf); 35110b5de56dSgjelinek return (subproc_status(RMCOMMAND, status)); 351207b574eeSgjelinek } 351307b574eeSgjelinek 3514865e09a4Sgjelinek static int 3515865e09a4Sgjelinek move_func(int argc, char *argv[]) 3516865e09a4Sgjelinek { 3517865e09a4Sgjelinek char *new_zonepath = NULL; 3518865e09a4Sgjelinek int lockfd; 3519865e09a4Sgjelinek int err, arg; 3520865e09a4Sgjelinek char zonepath[MAXPATHLEN]; 3521865e09a4Sgjelinek zone_dochandle_t handle; 3522865e09a4Sgjelinek boolean_t fast; 35230b5de56dSgjelinek boolean_t is_zfs = B_FALSE; 35240b5de56dSgjelinek struct dirent *dp; 35250b5de56dSgjelinek DIR *dirp; 35260b5de56dSgjelinek boolean_t empty = B_TRUE; 3527865e09a4Sgjelinek boolean_t revert; 3528865e09a4Sgjelinek struct stat zonepath_buf; 3529865e09a4Sgjelinek struct stat new_zonepath_buf; 3530865e09a4Sgjelinek 3531865e09a4Sgjelinek if (zonecfg_in_alt_root()) { 3532865e09a4Sgjelinek zerror(gettext("cannot move zone in alternate root")); 3533865e09a4Sgjelinek return (Z_ERR); 3534865e09a4Sgjelinek } 3535865e09a4Sgjelinek 3536865e09a4Sgjelinek optind = 0; 3537865e09a4Sgjelinek if ((arg = getopt(argc, argv, "?")) != EOF) { 3538865e09a4Sgjelinek switch (arg) { 3539865e09a4Sgjelinek case '?': 3540865e09a4Sgjelinek sub_usage(SHELP_MOVE, CMD_MOVE); 3541865e09a4Sgjelinek return (optopt == '?' ? Z_OK : Z_USAGE); 3542865e09a4Sgjelinek default: 3543865e09a4Sgjelinek sub_usage(SHELP_MOVE, CMD_MOVE); 3544865e09a4Sgjelinek return (Z_USAGE); 3545865e09a4Sgjelinek } 3546865e09a4Sgjelinek } 3547865e09a4Sgjelinek if (argc != (optind + 1)) { 3548865e09a4Sgjelinek sub_usage(SHELP_MOVE, CMD_MOVE); 3549865e09a4Sgjelinek return (Z_USAGE); 3550865e09a4Sgjelinek } 3551865e09a4Sgjelinek new_zonepath = argv[optind]; 3552865e09a4Sgjelinek if (sanity_check(target_zone, CMD_MOVE, B_FALSE, B_TRUE) != Z_OK) 3553865e09a4Sgjelinek return (Z_ERR); 3554865e09a4Sgjelinek if (verify_details(CMD_MOVE) != Z_OK) 3555865e09a4Sgjelinek return (Z_ERR); 3556865e09a4Sgjelinek 3557865e09a4Sgjelinek /* 3558865e09a4Sgjelinek * Check out the new zonepath. This has the side effect of creating 3559865e09a4Sgjelinek * a directory for the new zonepath. We depend on this later when we 35600b5de56dSgjelinek * stat to see if we are doing a cross file system move or not. 3561865e09a4Sgjelinek */ 3562865e09a4Sgjelinek if (validate_zonepath(new_zonepath, CMD_MOVE) != Z_OK) 3563865e09a4Sgjelinek return (Z_ERR); 3564865e09a4Sgjelinek 3565865e09a4Sgjelinek if ((err = zone_get_zonepath(target_zone, zonepath, sizeof (zonepath))) 3566865e09a4Sgjelinek != Z_OK) { 3567865e09a4Sgjelinek errno = err; 3568865e09a4Sgjelinek zperror2(target_zone, gettext("could not get zone path")); 3569865e09a4Sgjelinek return (Z_ERR); 3570865e09a4Sgjelinek } 3571865e09a4Sgjelinek 3572865e09a4Sgjelinek if (stat(zonepath, &zonepath_buf) == -1) { 3573865e09a4Sgjelinek zperror(gettext("could not stat zone path"), B_FALSE); 3574865e09a4Sgjelinek return (Z_ERR); 3575865e09a4Sgjelinek } 3576865e09a4Sgjelinek 3577865e09a4Sgjelinek if (stat(new_zonepath, &new_zonepath_buf) == -1) { 3578865e09a4Sgjelinek zperror(gettext("could not stat new zone path"), B_FALSE); 3579865e09a4Sgjelinek return (Z_ERR); 3580865e09a4Sgjelinek } 3581865e09a4Sgjelinek 35820b5de56dSgjelinek /* 35830b5de56dSgjelinek * Check if the destination directory is empty. 35840b5de56dSgjelinek */ 35850b5de56dSgjelinek if ((dirp = opendir(new_zonepath)) == NULL) { 35860b5de56dSgjelinek zperror(gettext("could not open new zone path"), B_FALSE); 35870b5de56dSgjelinek return (Z_ERR); 35880b5de56dSgjelinek } 35890b5de56dSgjelinek while ((dp = readdir(dirp)) != (struct dirent *)0) { 35900b5de56dSgjelinek if (strcmp(dp->d_name, ".") == 0 || 35910b5de56dSgjelinek strcmp(dp->d_name, "..") == 0) 35920b5de56dSgjelinek continue; 35930b5de56dSgjelinek empty = B_FALSE; 35940b5de56dSgjelinek break; 35950b5de56dSgjelinek } 35960b5de56dSgjelinek (void) closedir(dirp); 35970b5de56dSgjelinek 35980b5de56dSgjelinek /* Error if there is anything in the destination directory. */ 35990b5de56dSgjelinek if (!empty) { 36000b5de56dSgjelinek (void) fprintf(stderr, gettext("could not move zone to %s: " 36010b5de56dSgjelinek "directory not empty\n"), new_zonepath); 36020b5de56dSgjelinek return (Z_ERR); 36030b5de56dSgjelinek } 36040b5de56dSgjelinek 3605865e09a4Sgjelinek /* Don't move the zone if anything is still mounted there */ 3606865e09a4Sgjelinek if (zonecfg_find_mounts(zonepath, NULL, NULL)) { 36070b5de56dSgjelinek zerror(gettext("These file systems are mounted on " 3608865e09a4Sgjelinek "subdirectories of %s.\n"), zonepath); 3609865e09a4Sgjelinek (void) zonecfg_find_mounts(zonepath, zfm_print, NULL); 3610865e09a4Sgjelinek return (Z_ERR); 3611865e09a4Sgjelinek } 3612865e09a4Sgjelinek 3613865e09a4Sgjelinek /* 3614865e09a4Sgjelinek * Check if we are moving in the same file system and can do a fast 3615865e09a4Sgjelinek * move or if we are crossing file systems and have to copy the data. 3616865e09a4Sgjelinek */ 3617865e09a4Sgjelinek fast = (zonepath_buf.st_dev == new_zonepath_buf.st_dev); 3618865e09a4Sgjelinek 3619865e09a4Sgjelinek if ((handle = zonecfg_init_handle()) == NULL) { 3620865e09a4Sgjelinek zperror(cmd_to_str(CMD_MOVE), B_TRUE); 3621865e09a4Sgjelinek return (Z_ERR); 3622865e09a4Sgjelinek } 3623865e09a4Sgjelinek 3624865e09a4Sgjelinek if ((err = zonecfg_get_handle(target_zone, handle)) != Z_OK) { 3625865e09a4Sgjelinek errno = err; 3626865e09a4Sgjelinek zperror(cmd_to_str(CMD_MOVE), B_TRUE); 3627865e09a4Sgjelinek zonecfg_fini_handle(handle); 3628865e09a4Sgjelinek return (Z_ERR); 3629865e09a4Sgjelinek } 3630865e09a4Sgjelinek 3631865e09a4Sgjelinek if (grab_lock_file(target_zone, &lockfd) != Z_OK) { 3632865e09a4Sgjelinek zerror(gettext("another %s may have an operation in progress."), 3633865e09a4Sgjelinek "zoneadm"); 3634865e09a4Sgjelinek zonecfg_fini_handle(handle); 3635865e09a4Sgjelinek return (Z_ERR); 3636865e09a4Sgjelinek } 3637865e09a4Sgjelinek 3638865e09a4Sgjelinek /* 36390b5de56dSgjelinek * We're making some file system changes now so we have to clean up 36400b5de56dSgjelinek * the file system before we are done. This will either clean up the 3641865e09a4Sgjelinek * new zonepath if the zonecfg update failed or it will clean up the 3642865e09a4Sgjelinek * old zonepath if everything is ok. 3643865e09a4Sgjelinek */ 3644865e09a4Sgjelinek revert = B_TRUE; 3645865e09a4Sgjelinek 36460b5de56dSgjelinek if (is_zonepath_zfs(zonepath) && 36470b5de56dSgjelinek move_zfs(zonepath, new_zonepath) != Z_ERR) { 36480b5de56dSgjelinek is_zfs = B_TRUE; 36490b5de56dSgjelinek 36500b5de56dSgjelinek } else if (fast) { 3651865e09a4Sgjelinek /* same file system, use rename for a quick move */ 3652865e09a4Sgjelinek 3653865e09a4Sgjelinek /* 3654865e09a4Sgjelinek * Remove the new_zonepath directory that got created above 3655865e09a4Sgjelinek * during the validation. It gets in the way of the rename. 3656865e09a4Sgjelinek */ 3657865e09a4Sgjelinek if (rmdir(new_zonepath) != 0) { 3658865e09a4Sgjelinek zperror(gettext("could not rmdir new zone path"), 3659865e09a4Sgjelinek B_FALSE); 3660865e09a4Sgjelinek zonecfg_fini_handle(handle); 3661865e09a4Sgjelinek release_lock_file(lockfd); 3662865e09a4Sgjelinek return (Z_ERR); 3663865e09a4Sgjelinek } 3664865e09a4Sgjelinek 3665865e09a4Sgjelinek if (rename(zonepath, new_zonepath) != 0) { 3666865e09a4Sgjelinek /* 3667865e09a4Sgjelinek * If this fails we don't need to do all of the 3668865e09a4Sgjelinek * cleanup that happens for the rest of the code 3669865e09a4Sgjelinek * so just return from this error. 3670865e09a4Sgjelinek */ 3671865e09a4Sgjelinek zperror(gettext("could not move zone"), B_FALSE); 3672865e09a4Sgjelinek zonecfg_fini_handle(handle); 3673865e09a4Sgjelinek release_lock_file(lockfd); 3674865e09a4Sgjelinek return (Z_ERR); 3675865e09a4Sgjelinek } 3676865e09a4Sgjelinek 3677865e09a4Sgjelinek } else { 36780b5de56dSgjelinek /* 36790b5de56dSgjelinek * Attempt to create a ZFS fs for the new zonepath. As usual, 36800b5de56dSgjelinek * we don't care if this works or not since we always have the 36810b5de56dSgjelinek * default behavior of a simple directory for the zonepath. 36820b5de56dSgjelinek */ 36830b5de56dSgjelinek create_zfs_zonepath(new_zonepath); 36840b5de56dSgjelinek 3685865e09a4Sgjelinek (void) printf(gettext( 36860b5de56dSgjelinek "Moving across file systems; copying zonepath %s..."), 3687865e09a4Sgjelinek zonepath); 3688865e09a4Sgjelinek (void) fflush(stdout); 3689865e09a4Sgjelinek 3690865e09a4Sgjelinek err = copy_zone(zonepath, new_zonepath); 3691865e09a4Sgjelinek 3692865e09a4Sgjelinek (void) printf("\n"); 3693865e09a4Sgjelinek if (err != Z_OK) 3694865e09a4Sgjelinek goto done; 3695865e09a4Sgjelinek } 3696865e09a4Sgjelinek 3697865e09a4Sgjelinek if ((err = zonecfg_set_zonepath(handle, new_zonepath)) != Z_OK) { 3698865e09a4Sgjelinek errno = err; 3699865e09a4Sgjelinek zperror(gettext("could not set new zonepath"), B_TRUE); 3700865e09a4Sgjelinek goto done; 3701865e09a4Sgjelinek } 3702865e09a4Sgjelinek 3703865e09a4Sgjelinek if ((err = zonecfg_save(handle)) != Z_OK) { 3704865e09a4Sgjelinek errno = err; 3705865e09a4Sgjelinek zperror(gettext("zonecfg save failed"), B_TRUE); 3706865e09a4Sgjelinek goto done; 3707865e09a4Sgjelinek } 3708865e09a4Sgjelinek 3709865e09a4Sgjelinek revert = B_FALSE; 3710865e09a4Sgjelinek 3711865e09a4Sgjelinek done: 3712865e09a4Sgjelinek zonecfg_fini_handle(handle); 3713865e09a4Sgjelinek release_lock_file(lockfd); 3714865e09a4Sgjelinek 3715865e09a4Sgjelinek /* 37160b5de56dSgjelinek * Clean up the file system based on how things went. We either 3717865e09a4Sgjelinek * clean up the new zonepath if the operation failed for some reason 3718865e09a4Sgjelinek * or we clean up the old zonepath if everything is ok. 3719865e09a4Sgjelinek */ 3720865e09a4Sgjelinek if (revert) { 3721865e09a4Sgjelinek /* The zonecfg update failed, cleanup the new zonepath. */ 37220b5de56dSgjelinek if (is_zfs) { 37230b5de56dSgjelinek if (move_zfs(new_zonepath, zonepath) == Z_ERR) { 37240b5de56dSgjelinek (void) fprintf(stderr, gettext("could not " 37250b5de56dSgjelinek "restore zonepath, the zfs mountpoint is " 37260b5de56dSgjelinek "set as:\n%s\n"), new_zonepath); 37270b5de56dSgjelinek /* 37280b5de56dSgjelinek * err is already != Z_OK since we're reverting 37290b5de56dSgjelinek */ 37300b5de56dSgjelinek } 37310b5de56dSgjelinek 37320b5de56dSgjelinek } else if (fast) { 3733865e09a4Sgjelinek if (rename(new_zonepath, zonepath) != 0) { 3734865e09a4Sgjelinek zperror(gettext("could not restore zonepath"), 3735865e09a4Sgjelinek B_FALSE); 3736865e09a4Sgjelinek /* 3737865e09a4Sgjelinek * err is already != Z_OK since we're reverting 3738865e09a4Sgjelinek */ 3739865e09a4Sgjelinek } 3740865e09a4Sgjelinek } else { 3741865e09a4Sgjelinek (void) printf(gettext("Cleaning up zonepath %s..."), 3742865e09a4Sgjelinek new_zonepath); 3743865e09a4Sgjelinek (void) fflush(stdout); 37440b5de56dSgjelinek err = cleanup_zonepath(new_zonepath, B_TRUE); 3745865e09a4Sgjelinek (void) printf("\n"); 3746865e09a4Sgjelinek 374707b574eeSgjelinek if (err != Z_OK) { 3748865e09a4Sgjelinek errno = err; 3749865e09a4Sgjelinek zperror(gettext("could not remove new " 3750865e09a4Sgjelinek "zonepath"), B_TRUE); 3751865e09a4Sgjelinek } else { 3752865e09a4Sgjelinek /* 3753865e09a4Sgjelinek * Because we're reverting we know the mainline 3754865e09a4Sgjelinek * code failed but we just reused the err 3755865e09a4Sgjelinek * variable so we reset it back to Z_ERR. 3756865e09a4Sgjelinek */ 3757865e09a4Sgjelinek err = Z_ERR; 3758865e09a4Sgjelinek } 3759865e09a4Sgjelinek } 3760865e09a4Sgjelinek 3761865e09a4Sgjelinek } else { 3762865e09a4Sgjelinek /* The move was successful, cleanup the old zonepath. */ 37630b5de56dSgjelinek if (!is_zfs && !fast) { 3764865e09a4Sgjelinek (void) printf( 3765865e09a4Sgjelinek gettext("Cleaning up zonepath %s..."), zonepath); 3766865e09a4Sgjelinek (void) fflush(stdout); 37670b5de56dSgjelinek err = cleanup_zonepath(zonepath, B_TRUE); 3768865e09a4Sgjelinek (void) printf("\n"); 3769865e09a4Sgjelinek 377007b574eeSgjelinek if (err != Z_OK) { 3771865e09a4Sgjelinek errno = err; 3772865e09a4Sgjelinek zperror(gettext("could not remove zonepath"), 3773865e09a4Sgjelinek B_TRUE); 3774865e09a4Sgjelinek } 3775865e09a4Sgjelinek } 3776865e09a4Sgjelinek } 3777865e09a4Sgjelinek 3778865e09a4Sgjelinek return ((err == Z_OK) ? Z_OK : Z_ERR); 3779865e09a4Sgjelinek } 3780865e09a4Sgjelinek 3781ee519a1fSgjelinek static int 3782ee519a1fSgjelinek detach_func(int argc, char *argv[]) 3783ee519a1fSgjelinek { 3784ee519a1fSgjelinek int lockfd; 3785ee519a1fSgjelinek int err, arg; 3786ee519a1fSgjelinek char zonepath[MAXPATHLEN]; 3787ee519a1fSgjelinek zone_dochandle_t handle; 37888cd327d5Sgjelinek boolean_t execute = B_TRUE; 3789ee519a1fSgjelinek 3790ee519a1fSgjelinek if (zonecfg_in_alt_root()) { 3791ee519a1fSgjelinek zerror(gettext("cannot detach zone in alternate root")); 3792ee519a1fSgjelinek return (Z_ERR); 3793ee519a1fSgjelinek } 3794ee519a1fSgjelinek 3795ee519a1fSgjelinek optind = 0; 37968cd327d5Sgjelinek if ((arg = getopt(argc, argv, "?n")) != EOF) { 3797ee519a1fSgjelinek switch (arg) { 3798ee519a1fSgjelinek case '?': 3799ee519a1fSgjelinek sub_usage(SHELP_DETACH, CMD_DETACH); 3800ee519a1fSgjelinek return (optopt == '?' ? Z_OK : Z_USAGE); 38018cd327d5Sgjelinek case 'n': 38028cd327d5Sgjelinek execute = B_FALSE; 38038cd327d5Sgjelinek break; 3804ee519a1fSgjelinek default: 3805ee519a1fSgjelinek sub_usage(SHELP_DETACH, CMD_DETACH); 3806ee519a1fSgjelinek return (Z_USAGE); 3807ee519a1fSgjelinek } 3808ee519a1fSgjelinek } 38098cd327d5Sgjelinek if (execute) { 38108cd327d5Sgjelinek if (sanity_check(target_zone, CMD_DETACH, B_FALSE, B_TRUE) 38118cd327d5Sgjelinek != Z_OK) 3812ee519a1fSgjelinek return (Z_ERR); 3813ee519a1fSgjelinek if (verify_details(CMD_DETACH) != Z_OK) 3814ee519a1fSgjelinek return (Z_ERR); 38158cd327d5Sgjelinek } else { 38168cd327d5Sgjelinek /* 38178cd327d5Sgjelinek * We want a dry-run to work for a non-privileged user so we 38188cd327d5Sgjelinek * only do minimal validation. 38198cd327d5Sgjelinek */ 38208cd327d5Sgjelinek if (getzoneid() != GLOBAL_ZONEID) { 38218cd327d5Sgjelinek zerror(gettext("must be in the global zone to %s a " 38228cd327d5Sgjelinek "zone."), cmd_to_str(CMD_DETACH)); 38238cd327d5Sgjelinek return (Z_ERR); 38248cd327d5Sgjelinek } 38258cd327d5Sgjelinek 38268cd327d5Sgjelinek if (target_zone == NULL) { 38278cd327d5Sgjelinek zerror(gettext("no zone specified")); 38288cd327d5Sgjelinek return (Z_ERR); 38298cd327d5Sgjelinek } 38308cd327d5Sgjelinek 38318cd327d5Sgjelinek if (strcmp(target_zone, GLOBAL_ZONENAME) == 0) { 38328cd327d5Sgjelinek zerror(gettext("%s operation is invalid for the " 38338cd327d5Sgjelinek "global zone."), cmd_to_str(CMD_DETACH)); 38348cd327d5Sgjelinek return (Z_ERR); 38358cd327d5Sgjelinek } 38368cd327d5Sgjelinek } 3837ee519a1fSgjelinek 3838ee519a1fSgjelinek if ((err = zone_get_zonepath(target_zone, zonepath, sizeof (zonepath))) 3839ee519a1fSgjelinek != Z_OK) { 3840ee519a1fSgjelinek errno = err; 3841ee519a1fSgjelinek zperror2(target_zone, gettext("could not get zone path")); 3842ee519a1fSgjelinek return (Z_ERR); 3843ee519a1fSgjelinek } 3844ee519a1fSgjelinek 3845ee519a1fSgjelinek /* Don't detach the zone if anything is still mounted there */ 38468cd327d5Sgjelinek if (execute && zonecfg_find_mounts(zonepath, NULL, NULL)) { 38470b5de56dSgjelinek zerror(gettext("These file systems are mounted on " 3848ee519a1fSgjelinek "subdirectories of %s.\n"), zonepath); 3849ee519a1fSgjelinek (void) zonecfg_find_mounts(zonepath, zfm_print, NULL); 3850ee519a1fSgjelinek return (Z_ERR); 3851ee519a1fSgjelinek } 3852ee519a1fSgjelinek 3853ee519a1fSgjelinek if ((handle = zonecfg_init_handle()) == NULL) { 3854ee519a1fSgjelinek zperror(cmd_to_str(CMD_DETACH), B_TRUE); 3855ee519a1fSgjelinek return (Z_ERR); 3856ee519a1fSgjelinek } 3857ee519a1fSgjelinek 3858ee519a1fSgjelinek if ((err = zonecfg_get_handle(target_zone, handle)) != Z_OK) { 3859ee519a1fSgjelinek errno = err; 3860ee519a1fSgjelinek zperror(cmd_to_str(CMD_DETACH), B_TRUE); 3861ee519a1fSgjelinek zonecfg_fini_handle(handle); 3862ee519a1fSgjelinek return (Z_ERR); 3863ee519a1fSgjelinek } 3864ee519a1fSgjelinek 38658cd327d5Sgjelinek if (execute && grab_lock_file(target_zone, &lockfd) != Z_OK) { 3866ee519a1fSgjelinek zerror(gettext("another %s may have an operation in progress."), 3867ee519a1fSgjelinek "zoneadm"); 3868ee519a1fSgjelinek zonecfg_fini_handle(handle); 3869ee519a1fSgjelinek return (Z_ERR); 3870ee519a1fSgjelinek } 3871ee519a1fSgjelinek 3872ee519a1fSgjelinek if ((err = zonecfg_get_detach_info(handle, B_TRUE)) != Z_OK) { 3873ee519a1fSgjelinek errno = err; 3874ee519a1fSgjelinek zperror(gettext("getting the detach information failed"), 3875ee519a1fSgjelinek B_TRUE); 3876ee519a1fSgjelinek goto done; 3877ee519a1fSgjelinek } 3878ee519a1fSgjelinek 38798cd327d5Sgjelinek if ((err = zonecfg_detach_save(handle, (execute ? 0 : ZONE_DRY_RUN))) 38808cd327d5Sgjelinek != Z_OK) { 3881ee519a1fSgjelinek errno = err; 3882ee519a1fSgjelinek zperror(gettext("saving the detach manifest failed"), B_TRUE); 3883ee519a1fSgjelinek goto done; 3884ee519a1fSgjelinek } 3885ee519a1fSgjelinek 38868cd327d5Sgjelinek /* 38878cd327d5Sgjelinek * Set the zone state back to configured unless we are running with the 38888cd327d5Sgjelinek * no-execute option. 38898cd327d5Sgjelinek */ 38908cd327d5Sgjelinek if (execute && (err = zone_set_state(target_zone, 38918cd327d5Sgjelinek ZONE_STATE_CONFIGURED)) != Z_OK) { 3892ee519a1fSgjelinek errno = err; 3893ee519a1fSgjelinek zperror(gettext("could not reset state"), B_TRUE); 3894ee519a1fSgjelinek } 3895ee519a1fSgjelinek 3896ee519a1fSgjelinek done: 3897ee519a1fSgjelinek zonecfg_fini_handle(handle); 38988cd327d5Sgjelinek if (execute) 3899ee519a1fSgjelinek release_lock_file(lockfd); 3900ee519a1fSgjelinek 3901ee519a1fSgjelinek return ((err == Z_OK) ? Z_OK : Z_ERR); 3902ee519a1fSgjelinek } 3903ee519a1fSgjelinek 3904ee519a1fSgjelinek /* 3905ee519a1fSgjelinek * During attach we go through and fix up the /dev entries for the zone 3906ee519a1fSgjelinek * we are attaching. In order to regenerate /dev with the correct devices, 3907ee519a1fSgjelinek * the old /dev will be removed, the zone readied (which generates a new 3908ee519a1fSgjelinek * /dev) then halted, then we use the info from the manifest to update 3909ee519a1fSgjelinek * the modes, owners, etc. on the new /dev. 3910ee519a1fSgjelinek */ 3911ee519a1fSgjelinek static int 3912ee519a1fSgjelinek dev_fix(zone_dochandle_t handle) 3913ee519a1fSgjelinek { 3914ee519a1fSgjelinek int res; 3915ee519a1fSgjelinek int err; 3916ee519a1fSgjelinek int status; 3917ee519a1fSgjelinek struct zone_devpermtab devtab; 3918ee519a1fSgjelinek zone_cmd_arg_t zarg; 3919ee519a1fSgjelinek char devpath[MAXPATHLEN]; 3920ee519a1fSgjelinek /* 6: "exec " and " " */ 3921ee519a1fSgjelinek char cmdbuf[sizeof (RMCOMMAND) + MAXPATHLEN + 6]; 3922ee519a1fSgjelinek 3923ee519a1fSgjelinek if ((res = zonecfg_get_zonepath(handle, devpath, sizeof (devpath))) 3924ee519a1fSgjelinek != Z_OK) 3925ee519a1fSgjelinek return (res); 3926ee519a1fSgjelinek 3927ee519a1fSgjelinek if (strlcat(devpath, "/dev", sizeof (devpath)) >= sizeof (devpath)) 3928ee519a1fSgjelinek return (Z_TOO_BIG); 3929ee519a1fSgjelinek 3930ee519a1fSgjelinek /* 3931ee519a1fSgjelinek * "exec" the command so that the returned status is that of 3932ee519a1fSgjelinek * RMCOMMAND and not the shell. 3933ee519a1fSgjelinek */ 3934ee519a1fSgjelinek (void) snprintf(cmdbuf, sizeof (cmdbuf), "exec " RMCOMMAND " %s", 3935ee519a1fSgjelinek devpath); 3936ee519a1fSgjelinek status = do_subproc(cmdbuf); 3937ee519a1fSgjelinek if ((err = subproc_status(RMCOMMAND, status)) != Z_OK) { 3938ee519a1fSgjelinek (void) fprintf(stderr, 3939ee519a1fSgjelinek gettext("could not remove existing /dev\n")); 3940ee519a1fSgjelinek return (Z_ERR); 3941ee519a1fSgjelinek } 3942ee519a1fSgjelinek 3943ee519a1fSgjelinek /* In order to ready the zone, it must be in the installed state */ 3944ee519a1fSgjelinek if ((err = zone_set_state(target_zone, ZONE_STATE_INSTALLED)) != Z_OK) { 3945ee519a1fSgjelinek errno = err; 3946ee519a1fSgjelinek zperror(gettext("could not reset state"), B_TRUE); 3947ee519a1fSgjelinek return (Z_ERR); 3948ee519a1fSgjelinek } 3949ee519a1fSgjelinek 3950ee519a1fSgjelinek /* We have to ready the zone to regen the dev tree */ 3951ee519a1fSgjelinek zarg.cmd = Z_READY; 3952ee519a1fSgjelinek if (call_zoneadmd(target_zone, &zarg) != 0) { 3953ee519a1fSgjelinek zerror(gettext("call to %s failed"), "zoneadmd"); 3954ee519a1fSgjelinek return (Z_ERR); 3955ee519a1fSgjelinek } 3956ee519a1fSgjelinek 3957ee519a1fSgjelinek zarg.cmd = Z_HALT; 3958ee519a1fSgjelinek if (call_zoneadmd(target_zone, &zarg) != 0) { 3959ee519a1fSgjelinek zerror(gettext("call to %s failed"), "zoneadmd"); 3960ee519a1fSgjelinek return (Z_ERR); 3961ee519a1fSgjelinek } 3962ee519a1fSgjelinek 3963ee519a1fSgjelinek if (zonecfg_setdevperment(handle) != Z_OK) { 3964ee519a1fSgjelinek (void) fprintf(stderr, 3965ee519a1fSgjelinek gettext("unable to enumerate device entries\n")); 3966ee519a1fSgjelinek return (Z_ERR); 3967ee519a1fSgjelinek } 3968ee519a1fSgjelinek 3969ee519a1fSgjelinek while (zonecfg_getdevperment(handle, &devtab) == Z_OK) { 3970ee519a1fSgjelinek int err; 3971ee519a1fSgjelinek 3972ee519a1fSgjelinek if ((err = zonecfg_devperms_apply(handle, 3973ee519a1fSgjelinek devtab.zone_devperm_name, devtab.zone_devperm_uid, 3974ee519a1fSgjelinek devtab.zone_devperm_gid, devtab.zone_devperm_mode, 3975ee519a1fSgjelinek devtab.zone_devperm_acl)) != Z_OK && err != Z_INVAL) 3976ee519a1fSgjelinek (void) fprintf(stderr, gettext("error updating device " 3977ee519a1fSgjelinek "%s: %s\n"), devtab.zone_devperm_name, 3978ee519a1fSgjelinek zonecfg_strerror(err)); 3979ee519a1fSgjelinek 3980ee519a1fSgjelinek free(devtab.zone_devperm_acl); 3981ee519a1fSgjelinek } 3982ee519a1fSgjelinek 3983ee519a1fSgjelinek (void) zonecfg_enddevperment(handle); 3984ee519a1fSgjelinek 3985ee519a1fSgjelinek return (Z_OK); 3986ee519a1fSgjelinek } 3987ee519a1fSgjelinek 39888cd327d5Sgjelinek /* 39898cd327d5Sgjelinek * Validate attaching a zone but don't actually do the work. The zone 39908cd327d5Sgjelinek * does not have to exist, so there is some complexity getting a new zone 39918cd327d5Sgjelinek * configuration set up so that we can perform the validation. This is 39928cd327d5Sgjelinek * handled within zonecfg_attach_manifest() which returns two handles; one 39938cd327d5Sgjelinek * for the the full configuration to validate (rem_handle) and the other 39948cd327d5Sgjelinek * (local_handle) containing only the zone configuration derived from the 39958cd327d5Sgjelinek * manifest. 39968cd327d5Sgjelinek */ 39978cd327d5Sgjelinek static int 39988cd327d5Sgjelinek dryrun_attach(char *manifest_path) 39998cd327d5Sgjelinek { 40008cd327d5Sgjelinek int fd; 40018cd327d5Sgjelinek int err; 40028cd327d5Sgjelinek int res; 40038cd327d5Sgjelinek zone_dochandle_t local_handle; 40048cd327d5Sgjelinek zone_dochandle_t rem_handle = NULL; 40058cd327d5Sgjelinek 40068cd327d5Sgjelinek if (strcmp(manifest_path, "-") == 0) { 40078cd327d5Sgjelinek fd = 0; 40088cd327d5Sgjelinek } else if ((fd = open(manifest_path, O_RDONLY)) < 0) { 40098cd327d5Sgjelinek zperror(gettext("could not open manifest path"), B_FALSE); 40108cd327d5Sgjelinek return (Z_ERR); 40118cd327d5Sgjelinek } 40128cd327d5Sgjelinek 40138cd327d5Sgjelinek if ((local_handle = zonecfg_init_handle()) == NULL) { 40148cd327d5Sgjelinek zperror(cmd_to_str(CMD_ATTACH), B_TRUE); 40158cd327d5Sgjelinek res = Z_ERR; 40168cd327d5Sgjelinek goto done; 40178cd327d5Sgjelinek } 40188cd327d5Sgjelinek 40198cd327d5Sgjelinek if ((rem_handle = zonecfg_init_handle()) == NULL) { 40208cd327d5Sgjelinek zperror(cmd_to_str(CMD_ATTACH), B_TRUE); 40218cd327d5Sgjelinek res = Z_ERR; 40228cd327d5Sgjelinek goto done; 40238cd327d5Sgjelinek } 40248cd327d5Sgjelinek 40258cd327d5Sgjelinek if ((err = zonecfg_attach_manifest(fd, local_handle, rem_handle)) 40268cd327d5Sgjelinek != Z_OK) { 40278cd327d5Sgjelinek if (err == Z_INVALID_DOCUMENT) 40288cd327d5Sgjelinek zerror(gettext("Cannot attach to an earlier release " 40298cd327d5Sgjelinek "of the operating system")); 40308cd327d5Sgjelinek else 40318cd327d5Sgjelinek zperror(cmd_to_str(CMD_ATTACH), B_TRUE); 40328cd327d5Sgjelinek res = Z_ERR; 40338cd327d5Sgjelinek goto done; 40348cd327d5Sgjelinek } 40358cd327d5Sgjelinek 40368cd327d5Sgjelinek res = verify_handle(CMD_ATTACH, local_handle); 40378cd327d5Sgjelinek 40388cd327d5Sgjelinek /* Get the detach information for the locally defined zone. */ 40398cd327d5Sgjelinek if ((err = zonecfg_get_detach_info(local_handle, B_FALSE)) != Z_OK) { 40408cd327d5Sgjelinek errno = err; 40418cd327d5Sgjelinek zperror(gettext("getting the attach information failed"), 40428cd327d5Sgjelinek B_TRUE); 40438cd327d5Sgjelinek res = Z_ERR; 40448cd327d5Sgjelinek } else { 40458cd327d5Sgjelinek /* sw_cmp prints error msgs as necessary */ 40468cd327d5Sgjelinek if (sw_cmp(local_handle, rem_handle, SW_CMP_NONE) != Z_OK) 40478cd327d5Sgjelinek res = Z_ERR; 40488cd327d5Sgjelinek } 40498cd327d5Sgjelinek 40508cd327d5Sgjelinek done: 40518cd327d5Sgjelinek if (strcmp(manifest_path, "-") != 0) 40528cd327d5Sgjelinek (void) close(fd); 40538cd327d5Sgjelinek 40548cd327d5Sgjelinek zonecfg_fini_handle(local_handle); 40558cd327d5Sgjelinek zonecfg_fini_handle(rem_handle); 40568cd327d5Sgjelinek 40578cd327d5Sgjelinek return ((res == Z_OK) ? Z_OK : Z_ERR); 40588cd327d5Sgjelinek } 40598cd327d5Sgjelinek 4060ee519a1fSgjelinek static int 4061ee519a1fSgjelinek attach_func(int argc, char *argv[]) 4062ee519a1fSgjelinek { 4063ee519a1fSgjelinek int lockfd; 4064ee519a1fSgjelinek int err, arg; 4065ee519a1fSgjelinek boolean_t force = B_FALSE; 4066ee519a1fSgjelinek zone_dochandle_t handle; 4067ee519a1fSgjelinek zone_dochandle_t athandle = NULL; 4068ee519a1fSgjelinek char zonepath[MAXPATHLEN]; 40698cd327d5Sgjelinek boolean_t execute = B_TRUE; 40708cd327d5Sgjelinek char *manifest_path; 4071ee519a1fSgjelinek 4072ee519a1fSgjelinek if (zonecfg_in_alt_root()) { 4073ee519a1fSgjelinek zerror(gettext("cannot attach zone in alternate root")); 4074ee519a1fSgjelinek return (Z_ERR); 4075ee519a1fSgjelinek } 4076ee519a1fSgjelinek 4077ee519a1fSgjelinek optind = 0; 40788cd327d5Sgjelinek if ((arg = getopt(argc, argv, "?Fn:")) != EOF) { 4079ee519a1fSgjelinek switch (arg) { 4080ee519a1fSgjelinek case '?': 4081ee519a1fSgjelinek sub_usage(SHELP_ATTACH, CMD_ATTACH); 4082ee519a1fSgjelinek return (optopt == '?' ? Z_OK : Z_USAGE); 4083ee519a1fSgjelinek case 'F': 4084ee519a1fSgjelinek force = B_TRUE; 4085ee519a1fSgjelinek break; 40868cd327d5Sgjelinek case 'n': 40878cd327d5Sgjelinek execute = B_FALSE; 40888cd327d5Sgjelinek manifest_path = optarg; 40898cd327d5Sgjelinek break; 4090ee519a1fSgjelinek default: 4091ee519a1fSgjelinek sub_usage(SHELP_ATTACH, CMD_ATTACH); 4092ee519a1fSgjelinek return (Z_USAGE); 4093ee519a1fSgjelinek } 4094ee519a1fSgjelinek } 40958cd327d5Sgjelinek 40968cd327d5Sgjelinek /* 40978cd327d5Sgjelinek * If the no-execute option was specified, we need to branch down 40988cd327d5Sgjelinek * a completely different path since there is no zone required to be 40998cd327d5Sgjelinek * configured for this option. 41008cd327d5Sgjelinek */ 41018cd327d5Sgjelinek if (!execute) 41028cd327d5Sgjelinek return (dryrun_attach(manifest_path)); 41038cd327d5Sgjelinek 4104ee519a1fSgjelinek if (sanity_check(target_zone, CMD_ATTACH, B_FALSE, B_TRUE) != Z_OK) 4105ee519a1fSgjelinek return (Z_ERR); 4106ee519a1fSgjelinek if (verify_details(CMD_ATTACH) != Z_OK) 4107ee519a1fSgjelinek return (Z_ERR); 4108ee519a1fSgjelinek 4109ee519a1fSgjelinek if ((err = zone_get_zonepath(target_zone, zonepath, sizeof (zonepath))) 4110ee519a1fSgjelinek != Z_OK) { 4111ee519a1fSgjelinek errno = err; 4112ee519a1fSgjelinek zperror2(target_zone, gettext("could not get zone path")); 4113ee519a1fSgjelinek return (Z_ERR); 4114ee519a1fSgjelinek } 4115ee519a1fSgjelinek 4116ee519a1fSgjelinek if ((handle = zonecfg_init_handle()) == NULL) { 4117ee519a1fSgjelinek zperror(cmd_to_str(CMD_ATTACH), B_TRUE); 4118ee519a1fSgjelinek return (Z_ERR); 4119ee519a1fSgjelinek } 4120ee519a1fSgjelinek 4121ee519a1fSgjelinek if ((err = zonecfg_get_handle(target_zone, handle)) != Z_OK) { 4122ee519a1fSgjelinek errno = err; 4123ee519a1fSgjelinek zperror(cmd_to_str(CMD_ATTACH), B_TRUE); 4124ee519a1fSgjelinek zonecfg_fini_handle(handle); 4125ee519a1fSgjelinek return (Z_ERR); 4126ee519a1fSgjelinek } 4127ee519a1fSgjelinek 4128ee519a1fSgjelinek if (grab_lock_file(target_zone, &lockfd) != Z_OK) { 4129ee519a1fSgjelinek zerror(gettext("another %s may have an operation in progress."), 4130ee519a1fSgjelinek "zoneadm"); 4131ee519a1fSgjelinek zonecfg_fini_handle(handle); 4132ee519a1fSgjelinek return (Z_ERR); 4133ee519a1fSgjelinek } 4134ee519a1fSgjelinek 4135ee519a1fSgjelinek if (force) 4136ee519a1fSgjelinek goto forced; 4137ee519a1fSgjelinek 4138ee519a1fSgjelinek if ((athandle = zonecfg_init_handle()) == NULL) { 4139ee519a1fSgjelinek zperror(cmd_to_str(CMD_ATTACH), B_TRUE); 4140ee519a1fSgjelinek goto done; 4141ee519a1fSgjelinek } 4142ee519a1fSgjelinek 4143ee519a1fSgjelinek if ((err = zonecfg_get_attach_handle(zonepath, target_zone, B_TRUE, 4144ee519a1fSgjelinek athandle)) != Z_OK) { 4145ee519a1fSgjelinek if (err == Z_NO_ZONE) 4146ee519a1fSgjelinek zerror(gettext("Not a detached zone")); 4147ee519a1fSgjelinek else if (err == Z_INVALID_DOCUMENT) 4148ee519a1fSgjelinek zerror(gettext("Cannot attach to an earlier release " 4149ee519a1fSgjelinek "of the operating system")); 4150ee519a1fSgjelinek else 4151ee519a1fSgjelinek zperror(cmd_to_str(CMD_ATTACH), B_TRUE); 4152ee519a1fSgjelinek goto done; 4153ee519a1fSgjelinek } 4154ee519a1fSgjelinek 4155ee519a1fSgjelinek /* Get the detach information for the locally defined zone. */ 4156ee519a1fSgjelinek if ((err = zonecfg_get_detach_info(handle, B_FALSE)) != Z_OK) { 4157ee519a1fSgjelinek errno = err; 4158ee519a1fSgjelinek zperror(gettext("getting the attach information failed"), 4159ee519a1fSgjelinek B_TRUE); 4160ee519a1fSgjelinek goto done; 4161ee519a1fSgjelinek } 4162ee519a1fSgjelinek 4163ee519a1fSgjelinek /* sw_cmp prints error msgs as necessary */ 41640b5de56dSgjelinek if ((err = sw_cmp(handle, athandle, SW_CMP_NONE)) != Z_OK) 4165ee519a1fSgjelinek goto done; 4166ee519a1fSgjelinek 4167ee519a1fSgjelinek if ((err = dev_fix(athandle)) != Z_OK) 4168ee519a1fSgjelinek goto done; 4169ee519a1fSgjelinek 4170ee519a1fSgjelinek forced: 4171ee519a1fSgjelinek 4172ee519a1fSgjelinek zonecfg_rm_detached(handle, force); 4173ee519a1fSgjelinek 4174ee519a1fSgjelinek if ((err = zone_set_state(target_zone, ZONE_STATE_INSTALLED)) != Z_OK) { 4175ee519a1fSgjelinek errno = err; 4176ee519a1fSgjelinek zperror(gettext("could not reset state"), B_TRUE); 4177ee519a1fSgjelinek } 4178ee519a1fSgjelinek 4179ee519a1fSgjelinek done: 4180ee519a1fSgjelinek zonecfg_fini_handle(handle); 4181ee519a1fSgjelinek release_lock_file(lockfd); 4182ee519a1fSgjelinek if (athandle != NULL) 4183ee519a1fSgjelinek zonecfg_fini_handle(athandle); 4184ee519a1fSgjelinek 4185ee519a1fSgjelinek return ((err == Z_OK) ? Z_OK : Z_ERR); 4186ee519a1fSgjelinek } 4187ee519a1fSgjelinek 4188865e09a4Sgjelinek /* 41897c478bd9Sstevel@tonic-gate * On input, TRUE => yes, FALSE => no. 41907c478bd9Sstevel@tonic-gate * On return, TRUE => 1, FALSE => 0, could not ask => -1. 41917c478bd9Sstevel@tonic-gate */ 41927c478bd9Sstevel@tonic-gate 41937c478bd9Sstevel@tonic-gate static int 41947c478bd9Sstevel@tonic-gate ask_yesno(boolean_t default_answer, const char *question) 41957c478bd9Sstevel@tonic-gate { 41967c478bd9Sstevel@tonic-gate char line[64]; /* should be large enough to answer yes or no */ 41977c478bd9Sstevel@tonic-gate 41987c478bd9Sstevel@tonic-gate if (!isatty(STDIN_FILENO)) 41997c478bd9Sstevel@tonic-gate return (-1); 42007c478bd9Sstevel@tonic-gate for (;;) { 42017c478bd9Sstevel@tonic-gate (void) printf("%s (%s)? ", question, 42027c478bd9Sstevel@tonic-gate default_answer ? "[y]/n" : "y/[n]"); 42037c478bd9Sstevel@tonic-gate if (fgets(line, sizeof (line), stdin) == NULL || 42047c478bd9Sstevel@tonic-gate line[0] == '\n') 42057c478bd9Sstevel@tonic-gate return (default_answer ? 1 : 0); 42067c478bd9Sstevel@tonic-gate if (tolower(line[0]) == 'y') 42077c478bd9Sstevel@tonic-gate return (1); 42087c478bd9Sstevel@tonic-gate if (tolower(line[0]) == 'n') 42097c478bd9Sstevel@tonic-gate return (0); 42107c478bd9Sstevel@tonic-gate } 42117c478bd9Sstevel@tonic-gate } 42127c478bd9Sstevel@tonic-gate 42137c478bd9Sstevel@tonic-gate static int 42147c478bd9Sstevel@tonic-gate uninstall_func(int argc, char *argv[]) 42157c478bd9Sstevel@tonic-gate { 42167c478bd9Sstevel@tonic-gate char line[ZONENAME_MAX + 128]; /* Enough for "Are you sure ..." */ 42170b5de56dSgjelinek char rootpath[MAXPATHLEN], zonepath[MAXPATHLEN]; 42187c478bd9Sstevel@tonic-gate boolean_t force = B_FALSE; 42197c478bd9Sstevel@tonic-gate int lockfd, answer; 42207c478bd9Sstevel@tonic-gate int err, arg; 42217c478bd9Sstevel@tonic-gate 4222108322fbScarlsonj if (zonecfg_in_alt_root()) { 4223108322fbScarlsonj zerror(gettext("cannot uninstall zone in alternate root")); 4224108322fbScarlsonj return (Z_ERR); 4225108322fbScarlsonj } 4226108322fbScarlsonj 42277c478bd9Sstevel@tonic-gate optind = 0; 42287c478bd9Sstevel@tonic-gate while ((arg = getopt(argc, argv, "?F")) != EOF) { 42297c478bd9Sstevel@tonic-gate switch (arg) { 42307c478bd9Sstevel@tonic-gate case '?': 42317c478bd9Sstevel@tonic-gate sub_usage(SHELP_UNINSTALL, CMD_UNINSTALL); 42327c478bd9Sstevel@tonic-gate return (optopt == '?' ? Z_OK : Z_USAGE); 42337c478bd9Sstevel@tonic-gate case 'F': 42347c478bd9Sstevel@tonic-gate force = B_TRUE; 42357c478bd9Sstevel@tonic-gate break; 42367c478bd9Sstevel@tonic-gate default: 42377c478bd9Sstevel@tonic-gate sub_usage(SHELP_UNINSTALL, CMD_UNINSTALL); 42387c478bd9Sstevel@tonic-gate return (Z_USAGE); 42397c478bd9Sstevel@tonic-gate } 42407c478bd9Sstevel@tonic-gate } 42417c478bd9Sstevel@tonic-gate if (argc > optind) { 42427c478bd9Sstevel@tonic-gate sub_usage(SHELP_UNINSTALL, CMD_UNINSTALL); 42437c478bd9Sstevel@tonic-gate return (Z_USAGE); 42447c478bd9Sstevel@tonic-gate } 42457c478bd9Sstevel@tonic-gate 42467c478bd9Sstevel@tonic-gate if (sanity_check(target_zone, CMD_UNINSTALL, B_FALSE, B_TRUE) != Z_OK) 42477c478bd9Sstevel@tonic-gate return (Z_ERR); 42487c478bd9Sstevel@tonic-gate 42497c478bd9Sstevel@tonic-gate if (!force) { 42507c478bd9Sstevel@tonic-gate (void) snprintf(line, sizeof (line), 42517c478bd9Sstevel@tonic-gate gettext("Are you sure you want to %s zone %s"), 42527c478bd9Sstevel@tonic-gate cmd_to_str(CMD_UNINSTALL), target_zone); 42537c478bd9Sstevel@tonic-gate if ((answer = ask_yesno(B_FALSE, line)) == 0) { 42547c478bd9Sstevel@tonic-gate return (Z_OK); 42557c478bd9Sstevel@tonic-gate } else if (answer == -1) { 42567c478bd9Sstevel@tonic-gate zerror(gettext("Input not from terminal and -F " 42577c478bd9Sstevel@tonic-gate "not specified: %s not done."), 42587c478bd9Sstevel@tonic-gate cmd_to_str(CMD_UNINSTALL)); 42597c478bd9Sstevel@tonic-gate return (Z_ERR); 42607c478bd9Sstevel@tonic-gate } 42617c478bd9Sstevel@tonic-gate } 42627c478bd9Sstevel@tonic-gate 42630b5de56dSgjelinek if ((err = zone_get_zonepath(target_zone, zonepath, 42640b5de56dSgjelinek sizeof (zonepath))) != Z_OK) { 42657c478bd9Sstevel@tonic-gate errno = err; 42667c478bd9Sstevel@tonic-gate zperror2(target_zone, gettext("could not get zone path")); 42677c478bd9Sstevel@tonic-gate return (Z_ERR); 42687c478bd9Sstevel@tonic-gate } 42697c478bd9Sstevel@tonic-gate if ((err = zone_get_rootpath(target_zone, rootpath, 42707c478bd9Sstevel@tonic-gate sizeof (rootpath))) != Z_OK) { 42717c478bd9Sstevel@tonic-gate errno = err; 42727c478bd9Sstevel@tonic-gate zperror2(target_zone, gettext("could not get root path")); 42737c478bd9Sstevel@tonic-gate return (Z_ERR); 42747c478bd9Sstevel@tonic-gate } 42757c478bd9Sstevel@tonic-gate 42767c478bd9Sstevel@tonic-gate /* 42777c478bd9Sstevel@tonic-gate * If there seems to be a zoneadmd running for this zone, call it 42787c478bd9Sstevel@tonic-gate * to tell it that an uninstall is happening; if all goes well it 42797c478bd9Sstevel@tonic-gate * will then shut itself down. 42807c478bd9Sstevel@tonic-gate */ 42817c478bd9Sstevel@tonic-gate if (ping_zoneadmd(target_zone) == Z_OK) { 42827c478bd9Sstevel@tonic-gate zone_cmd_arg_t zarg; 42837c478bd9Sstevel@tonic-gate zarg.cmd = Z_NOTE_UNINSTALLING; 42847c478bd9Sstevel@tonic-gate /* we don't care too much if this fails... just plow on */ 42857c478bd9Sstevel@tonic-gate (void) call_zoneadmd(target_zone, &zarg); 42867c478bd9Sstevel@tonic-gate } 42877c478bd9Sstevel@tonic-gate 42887c478bd9Sstevel@tonic-gate if (grab_lock_file(target_zone, &lockfd) != Z_OK) { 42897c478bd9Sstevel@tonic-gate zerror(gettext("another %s may have an operation in progress."), 4290865e09a4Sgjelinek "zoneadm"); 42917c478bd9Sstevel@tonic-gate return (Z_ERR); 42927c478bd9Sstevel@tonic-gate } 42937c478bd9Sstevel@tonic-gate 42947c478bd9Sstevel@tonic-gate /* Don't uninstall the zone if anything is mounted there */ 42957c478bd9Sstevel@tonic-gate err = zonecfg_find_mounts(rootpath, NULL, NULL); 42967c478bd9Sstevel@tonic-gate if (err) { 42970b5de56dSgjelinek zerror(gettext("These file systems are mounted on " 42987c478bd9Sstevel@tonic-gate "subdirectories of %s.\n"), rootpath); 42997c478bd9Sstevel@tonic-gate (void) zonecfg_find_mounts(rootpath, zfm_print, NULL); 43007c478bd9Sstevel@tonic-gate return (Z_ERR); 43017c478bd9Sstevel@tonic-gate } 43027c478bd9Sstevel@tonic-gate 43037c478bd9Sstevel@tonic-gate err = zone_set_state(target_zone, ZONE_STATE_INCOMPLETE); 43047c478bd9Sstevel@tonic-gate if (err != Z_OK) { 43057c478bd9Sstevel@tonic-gate errno = err; 43067c478bd9Sstevel@tonic-gate zperror2(target_zone, gettext("could not set state")); 43077c478bd9Sstevel@tonic-gate goto bad; 43087c478bd9Sstevel@tonic-gate } 43097c478bd9Sstevel@tonic-gate 43100b5de56dSgjelinek if ((err = cleanup_zonepath(zonepath, B_FALSE)) != Z_OK) { 43110b5de56dSgjelinek errno = err; 43120b5de56dSgjelinek zperror2(target_zone, gettext("cleaning up zonepath failed")); 43137c478bd9Sstevel@tonic-gate goto bad; 43140b5de56dSgjelinek } 43150b5de56dSgjelinek 43167c478bd9Sstevel@tonic-gate err = zone_set_state(target_zone, ZONE_STATE_CONFIGURED); 43177c478bd9Sstevel@tonic-gate if (err != Z_OK) { 43187c478bd9Sstevel@tonic-gate errno = err; 43197c478bd9Sstevel@tonic-gate zperror2(target_zone, gettext("could not reset state")); 43207c478bd9Sstevel@tonic-gate } 43217c478bd9Sstevel@tonic-gate bad: 43227c478bd9Sstevel@tonic-gate release_lock_file(lockfd); 43237c478bd9Sstevel@tonic-gate return (err); 43247c478bd9Sstevel@tonic-gate } 43257c478bd9Sstevel@tonic-gate 4326108322fbScarlsonj /* ARGSUSED */ 4327108322fbScarlsonj static int 4328108322fbScarlsonj mount_func(int argc, char *argv[]) 4329108322fbScarlsonj { 4330108322fbScarlsonj zone_cmd_arg_t zarg; 4331108322fbScarlsonj 4332108322fbScarlsonj if (argc > 0) 4333108322fbScarlsonj return (Z_USAGE); 4334108322fbScarlsonj if (sanity_check(target_zone, CMD_MOUNT, B_FALSE, B_FALSE) != Z_OK) 4335108322fbScarlsonj return (Z_ERR); 4336108322fbScarlsonj if (verify_details(CMD_MOUNT) != Z_OK) 4337108322fbScarlsonj return (Z_ERR); 4338108322fbScarlsonj 4339108322fbScarlsonj zarg.cmd = Z_MOUNT; 4340108322fbScarlsonj if (call_zoneadmd(target_zone, &zarg) != 0) { 4341108322fbScarlsonj zerror(gettext("call to %s failed"), "zoneadmd"); 4342108322fbScarlsonj return (Z_ERR); 4343108322fbScarlsonj } 4344108322fbScarlsonj return (Z_OK); 4345108322fbScarlsonj } 4346108322fbScarlsonj 4347108322fbScarlsonj /* ARGSUSED */ 4348108322fbScarlsonj static int 4349108322fbScarlsonj unmount_func(int argc, char *argv[]) 4350108322fbScarlsonj { 4351108322fbScarlsonj zone_cmd_arg_t zarg; 4352108322fbScarlsonj 4353108322fbScarlsonj if (argc > 0) 4354108322fbScarlsonj return (Z_USAGE); 4355108322fbScarlsonj if (sanity_check(target_zone, CMD_UNMOUNT, B_FALSE, B_FALSE) != Z_OK) 4356108322fbScarlsonj return (Z_ERR); 4357108322fbScarlsonj 4358108322fbScarlsonj zarg.cmd = Z_UNMOUNT; 4359108322fbScarlsonj if (call_zoneadmd(target_zone, &zarg) != 0) { 4360108322fbScarlsonj zerror(gettext("call to %s failed"), "zoneadmd"); 4361108322fbScarlsonj return (Z_ERR); 4362108322fbScarlsonj } 4363108322fbScarlsonj return (Z_OK); 4364108322fbScarlsonj } 4365108322fbScarlsonj 43667c478bd9Sstevel@tonic-gate static int 43677c478bd9Sstevel@tonic-gate help_func(int argc, char *argv[]) 43687c478bd9Sstevel@tonic-gate { 43697c478bd9Sstevel@tonic-gate int arg, cmd_num; 43707c478bd9Sstevel@tonic-gate 43717c478bd9Sstevel@tonic-gate if (argc == 0) { 43727c478bd9Sstevel@tonic-gate (void) usage(B_TRUE); 43737c478bd9Sstevel@tonic-gate return (Z_OK); 43747c478bd9Sstevel@tonic-gate } 43757c478bd9Sstevel@tonic-gate optind = 0; 43767c478bd9Sstevel@tonic-gate if ((arg = getopt(argc, argv, "?")) != EOF) { 43777c478bd9Sstevel@tonic-gate switch (arg) { 43787c478bd9Sstevel@tonic-gate case '?': 43797c478bd9Sstevel@tonic-gate sub_usage(SHELP_HELP, CMD_HELP); 43807c478bd9Sstevel@tonic-gate return (optopt == '?' ? Z_OK : Z_USAGE); 43817c478bd9Sstevel@tonic-gate default: 43827c478bd9Sstevel@tonic-gate sub_usage(SHELP_HELP, CMD_HELP); 43837c478bd9Sstevel@tonic-gate return (Z_USAGE); 43847c478bd9Sstevel@tonic-gate } 43857c478bd9Sstevel@tonic-gate } 43867c478bd9Sstevel@tonic-gate while (optind < argc) { 4387394a25e2Scarlsonj /* Private commands have NULL short_usage; omit them */ 4388394a25e2Scarlsonj if ((cmd_num = cmd_match(argv[optind])) < 0 || 4389394a25e2Scarlsonj cmdtab[cmd_num].short_usage == NULL) { 43907c478bd9Sstevel@tonic-gate sub_usage(SHELP_HELP, CMD_HELP); 43917c478bd9Sstevel@tonic-gate return (Z_USAGE); 43927c478bd9Sstevel@tonic-gate } 43937c478bd9Sstevel@tonic-gate sub_usage(cmdtab[cmd_num].short_usage, cmd_num); 43947c478bd9Sstevel@tonic-gate optind++; 43957c478bd9Sstevel@tonic-gate } 43967c478bd9Sstevel@tonic-gate return (Z_OK); 43977c478bd9Sstevel@tonic-gate } 43987c478bd9Sstevel@tonic-gate 43997c478bd9Sstevel@tonic-gate /* 44007c478bd9Sstevel@tonic-gate * Returns: CMD_MIN thru CMD_MAX on success, -1 on error 44017c478bd9Sstevel@tonic-gate */ 44027c478bd9Sstevel@tonic-gate 44037c478bd9Sstevel@tonic-gate static int 44047c478bd9Sstevel@tonic-gate cmd_match(char *cmd) 44057c478bd9Sstevel@tonic-gate { 44067c478bd9Sstevel@tonic-gate int i; 44077c478bd9Sstevel@tonic-gate 44087c478bd9Sstevel@tonic-gate for (i = CMD_MIN; i <= CMD_MAX; i++) { 44097c478bd9Sstevel@tonic-gate /* return only if there is an exact match */ 44107c478bd9Sstevel@tonic-gate if (strcmp(cmd, cmdtab[i].cmd_name) == 0) 44117c478bd9Sstevel@tonic-gate return (cmdtab[i].cmd_num); 44127c478bd9Sstevel@tonic-gate } 44137c478bd9Sstevel@tonic-gate return (-1); 44147c478bd9Sstevel@tonic-gate } 44157c478bd9Sstevel@tonic-gate 44167c478bd9Sstevel@tonic-gate static int 44177c478bd9Sstevel@tonic-gate parse_and_run(int argc, char *argv[]) 44187c478bd9Sstevel@tonic-gate { 44197c478bd9Sstevel@tonic-gate int i = cmd_match(argv[0]); 44207c478bd9Sstevel@tonic-gate 44217c478bd9Sstevel@tonic-gate if (i < 0) 44227c478bd9Sstevel@tonic-gate return (usage(B_FALSE)); 44237c478bd9Sstevel@tonic-gate return (cmdtab[i].handler(argc - 1, &(argv[1]))); 44247c478bd9Sstevel@tonic-gate } 44257c478bd9Sstevel@tonic-gate 44267c478bd9Sstevel@tonic-gate static char * 44277c478bd9Sstevel@tonic-gate get_execbasename(char *execfullname) 44287c478bd9Sstevel@tonic-gate { 44297c478bd9Sstevel@tonic-gate char *last_slash, *execbasename; 44307c478bd9Sstevel@tonic-gate 44317c478bd9Sstevel@tonic-gate /* guard against '/' at end of command invocation */ 44327c478bd9Sstevel@tonic-gate for (;;) { 44337c478bd9Sstevel@tonic-gate last_slash = strrchr(execfullname, '/'); 44347c478bd9Sstevel@tonic-gate if (last_slash == NULL) { 44357c478bd9Sstevel@tonic-gate execbasename = execfullname; 44367c478bd9Sstevel@tonic-gate break; 44377c478bd9Sstevel@tonic-gate } else { 44387c478bd9Sstevel@tonic-gate execbasename = last_slash + 1; 44397c478bd9Sstevel@tonic-gate if (*execbasename == '\0') { 44407c478bd9Sstevel@tonic-gate *last_slash = '\0'; 44417c478bd9Sstevel@tonic-gate continue; 44427c478bd9Sstevel@tonic-gate } 44437c478bd9Sstevel@tonic-gate break; 44447c478bd9Sstevel@tonic-gate } 44457c478bd9Sstevel@tonic-gate } 44467c478bd9Sstevel@tonic-gate return (execbasename); 44477c478bd9Sstevel@tonic-gate } 44487c478bd9Sstevel@tonic-gate 44497c478bd9Sstevel@tonic-gate int 44507c478bd9Sstevel@tonic-gate main(int argc, char **argv) 44517c478bd9Sstevel@tonic-gate { 44527c478bd9Sstevel@tonic-gate int arg; 44537c478bd9Sstevel@tonic-gate zoneid_t zid; 4454108322fbScarlsonj struct stat st; 44557c478bd9Sstevel@tonic-gate 44567c478bd9Sstevel@tonic-gate if ((locale = setlocale(LC_ALL, "")) == NULL) 44577c478bd9Sstevel@tonic-gate locale = "C"; 44587c478bd9Sstevel@tonic-gate (void) textdomain(TEXT_DOMAIN); 44597c478bd9Sstevel@tonic-gate setbuf(stdout, NULL); 44607c478bd9Sstevel@tonic-gate (void) sigset(SIGHUP, SIG_IGN); 44617c478bd9Sstevel@tonic-gate execname = get_execbasename(argv[0]); 44627c478bd9Sstevel@tonic-gate target_zone = NULL; 44637c478bd9Sstevel@tonic-gate if (chdir("/") != 0) { 44647c478bd9Sstevel@tonic-gate zerror(gettext("could not change directory to /.")); 44657c478bd9Sstevel@tonic-gate exit(Z_ERR); 44667c478bd9Sstevel@tonic-gate } 44677c478bd9Sstevel@tonic-gate 446899653d4eSeschrock if (init_zfs() != Z_OK) 446999653d4eSeschrock exit(Z_ERR); 447099653d4eSeschrock 4471108322fbScarlsonj while ((arg = getopt(argc, argv, "?z:R:")) != EOF) { 44727c478bd9Sstevel@tonic-gate switch (arg) { 44737c478bd9Sstevel@tonic-gate case '?': 44747c478bd9Sstevel@tonic-gate return (usage(B_TRUE)); 44757c478bd9Sstevel@tonic-gate case 'z': 44767c478bd9Sstevel@tonic-gate target_zone = optarg; 44777c478bd9Sstevel@tonic-gate break; 4478108322fbScarlsonj case 'R': /* private option for admin/install use */ 4479108322fbScarlsonj if (*optarg != '/') { 4480108322fbScarlsonj zerror(gettext("root path must be absolute.")); 4481108322fbScarlsonj exit(Z_ERR); 4482108322fbScarlsonj } 4483108322fbScarlsonj if (stat(optarg, &st) == -1 || !S_ISDIR(st.st_mode)) { 4484108322fbScarlsonj zerror( 4485108322fbScarlsonj gettext("root path must be a directory.")); 4486108322fbScarlsonj exit(Z_ERR); 4487108322fbScarlsonj } 4488108322fbScarlsonj zonecfg_set_root(optarg); 4489108322fbScarlsonj break; 44907c478bd9Sstevel@tonic-gate default: 44917c478bd9Sstevel@tonic-gate return (usage(B_FALSE)); 44927c478bd9Sstevel@tonic-gate } 44937c478bd9Sstevel@tonic-gate } 44947c478bd9Sstevel@tonic-gate 44957c478bd9Sstevel@tonic-gate if (optind >= argc) 44967c478bd9Sstevel@tonic-gate return (usage(B_FALSE)); 44977c478bd9Sstevel@tonic-gate if (target_zone != NULL && zone_get_id(target_zone, &zid) != 0) { 44987c478bd9Sstevel@tonic-gate errno = Z_NO_ZONE; 44997c478bd9Sstevel@tonic-gate zperror(target_zone, B_TRUE); 45007c478bd9Sstevel@tonic-gate exit(Z_ERR); 45017c478bd9Sstevel@tonic-gate } 45027c478bd9Sstevel@tonic-gate return (parse_and_run(argc - optind, &argv[optind])); 45037c478bd9Sstevel@tonic-gate } 4504