10Sstevel@tonic-gate /* 20Sstevel@tonic-gate * CDDL HEADER START 30Sstevel@tonic-gate * 40Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*1507Sgjelinek * Common Development and Distribution License (the "License"). 6*1507Sgjelinek * You may not use this file except in compliance with the License. 70Sstevel@tonic-gate * 80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 100Sstevel@tonic-gate * See the License for the specific language governing permissions 110Sstevel@tonic-gate * and limitations under the License. 120Sstevel@tonic-gate * 130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 180Sstevel@tonic-gate * 190Sstevel@tonic-gate * CDDL HEADER END 200Sstevel@tonic-gate */ 21222Scomay 220Sstevel@tonic-gate /* 231300Sgjelinek * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 240Sstevel@tonic-gate * Use is subject to license terms. 250Sstevel@tonic-gate */ 260Sstevel@tonic-gate 270Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 280Sstevel@tonic-gate 290Sstevel@tonic-gate /* 300Sstevel@tonic-gate * zoneadm is a command interpreter for zone administration. It is all in 310Sstevel@tonic-gate * C (i.e., no lex/yacc), and all the argument passing is argc/argv based. 320Sstevel@tonic-gate * main() calls parse_and_run() which calls cmd_match(), then invokes the 330Sstevel@tonic-gate * appropriate command's handler function. The rest of the program is the 340Sstevel@tonic-gate * handler functions and their helper functions. 350Sstevel@tonic-gate * 360Sstevel@tonic-gate * Some of the helper functions are used largely to simplify I18N: reducing 370Sstevel@tonic-gate * the need for translation notes. This is particularly true of many of 380Sstevel@tonic-gate * the zerror() calls: doing e.g. zerror(gettext("%s failed"), "foo") rather 390Sstevel@tonic-gate * than zerror(gettext("foo failed")) with a translation note indicating 400Sstevel@tonic-gate * that "foo" need not be translated. 410Sstevel@tonic-gate */ 420Sstevel@tonic-gate 430Sstevel@tonic-gate #include <stdio.h> 440Sstevel@tonic-gate #include <errno.h> 450Sstevel@tonic-gate #include <unistd.h> 460Sstevel@tonic-gate #include <signal.h> 470Sstevel@tonic-gate #include <stdarg.h> 480Sstevel@tonic-gate #include <ctype.h> 490Sstevel@tonic-gate #include <stdlib.h> 500Sstevel@tonic-gate #include <string.h> 510Sstevel@tonic-gate #include <wait.h> 520Sstevel@tonic-gate #include <zone.h> 530Sstevel@tonic-gate #include <priv.h> 540Sstevel@tonic-gate #include <locale.h> 550Sstevel@tonic-gate #include <libintl.h> 560Sstevel@tonic-gate #include <libzonecfg.h> 570Sstevel@tonic-gate #include <bsm/adt.h> 580Sstevel@tonic-gate #include <sys/utsname.h> 590Sstevel@tonic-gate #include <sys/param.h> 600Sstevel@tonic-gate #include <sys/types.h> 610Sstevel@tonic-gate #include <sys/stat.h> 620Sstevel@tonic-gate #include <sys/statvfs.h> 630Sstevel@tonic-gate #include <assert.h> 640Sstevel@tonic-gate #include <sys/sockio.h> 650Sstevel@tonic-gate #include <sys/mntent.h> 660Sstevel@tonic-gate #include <limits.h> 67789Sahrens #include <libzfs.h> 680Sstevel@tonic-gate 690Sstevel@tonic-gate #include <fcntl.h> 700Sstevel@tonic-gate #include <door.h> 710Sstevel@tonic-gate #include <macros.h> 720Sstevel@tonic-gate #include <libgen.h> 731300Sgjelinek #include <fnmatch.h> 740Sstevel@tonic-gate 750Sstevel@tonic-gate #include <pool.h> 760Sstevel@tonic-gate #include <sys/pool.h> 770Sstevel@tonic-gate 780Sstevel@tonic-gate #define MAXARGS 8 790Sstevel@tonic-gate 800Sstevel@tonic-gate /* Reflects kernel zone entries */ 810Sstevel@tonic-gate typedef struct zone_entry { 820Sstevel@tonic-gate zoneid_t zid; 830Sstevel@tonic-gate char zname[ZONENAME_MAX]; 840Sstevel@tonic-gate char *zstate_str; 850Sstevel@tonic-gate zone_state_t zstate_num; 860Sstevel@tonic-gate char zroot[MAXPATHLEN]; 870Sstevel@tonic-gate } zone_entry_t; 880Sstevel@tonic-gate 890Sstevel@tonic-gate static zone_entry_t *zents; 900Sstevel@tonic-gate static size_t nzents; 910Sstevel@tonic-gate 920Sstevel@tonic-gate #if !defined(TEXT_DOMAIN) /* should be defined by cc -D */ 930Sstevel@tonic-gate #define TEXT_DOMAIN "SYS_TEST" /* Use this only if it wasn't */ 940Sstevel@tonic-gate #endif 950Sstevel@tonic-gate 960Sstevel@tonic-gate #define Z_ERR 1 970Sstevel@tonic-gate #define Z_USAGE 2 980Sstevel@tonic-gate 990Sstevel@tonic-gate /* 0755 is the default directory mode. */ 1000Sstevel@tonic-gate #define DEFAULT_DIR_MODE \ 1010Sstevel@tonic-gate (S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) 1020Sstevel@tonic-gate 1030Sstevel@tonic-gate #define CMD_HELP 0 1040Sstevel@tonic-gate #define CMD_BOOT 1 1050Sstevel@tonic-gate #define CMD_HALT 2 1060Sstevel@tonic-gate #define CMD_READY 3 1070Sstevel@tonic-gate #define CMD_REBOOT 4 1080Sstevel@tonic-gate #define CMD_LIST 5 1090Sstevel@tonic-gate #define CMD_VERIFY 6 1100Sstevel@tonic-gate #define CMD_INSTALL 7 1110Sstevel@tonic-gate #define CMD_UNINSTALL 8 112766Scarlsonj #define CMD_MOUNT 9 113766Scarlsonj #define CMD_UNMOUNT 10 1141300Sgjelinek #define CMD_CLONE 11 1151300Sgjelinek #define CMD_MOVE 12 116*1507Sgjelinek #define CMD_DETACH 13 117*1507Sgjelinek #define CMD_ATTACH 14 1180Sstevel@tonic-gate 1190Sstevel@tonic-gate #define CMD_MIN CMD_HELP 120*1507Sgjelinek #define CMD_MAX CMD_ATTACH 1211300Sgjelinek 1221300Sgjelinek #define SINGLE_USER_RETRY 30 1230Sstevel@tonic-gate 1240Sstevel@tonic-gate struct cmd { 1250Sstevel@tonic-gate uint_t cmd_num; /* command number */ 1260Sstevel@tonic-gate char *cmd_name; /* command name */ 1270Sstevel@tonic-gate char *short_usage; /* short form help */ 1280Sstevel@tonic-gate int (*handler)(int argc, char *argv[]); /* function to call */ 1290Sstevel@tonic-gate 1300Sstevel@tonic-gate }; 1310Sstevel@tonic-gate 1320Sstevel@tonic-gate #define SHELP_HELP "help" 1330Sstevel@tonic-gate #define SHELP_BOOT "boot [-s]" 1340Sstevel@tonic-gate #define SHELP_HALT "halt" 1350Sstevel@tonic-gate #define SHELP_READY "ready" 1360Sstevel@tonic-gate #define SHELP_REBOOT "reboot" 1370Sstevel@tonic-gate #define SHELP_LIST "list [-cipv]" 1380Sstevel@tonic-gate #define SHELP_VERIFY "verify" 1390Sstevel@tonic-gate #define SHELP_INSTALL "install" 1400Sstevel@tonic-gate #define SHELP_UNINSTALL "uninstall [-F]" 1411300Sgjelinek #define SHELP_CLONE "clone [-m method] zonename" 1421300Sgjelinek #define SHELP_MOVE "move zonepath" 143*1507Sgjelinek #define SHELP_DETACH "detach" 144*1507Sgjelinek #define SHELP_ATTACH "attach [-F]" 1450Sstevel@tonic-gate 1460Sstevel@tonic-gate static int help_func(int argc, char *argv[]); 1470Sstevel@tonic-gate static int ready_func(int argc, char *argv[]); 1480Sstevel@tonic-gate static int boot_func(int argc, char *argv[]); 1490Sstevel@tonic-gate static int halt_func(int argc, char *argv[]); 1500Sstevel@tonic-gate static int reboot_func(int argc, char *argv[]); 1510Sstevel@tonic-gate static int list_func(int argc, char *argv[]); 1520Sstevel@tonic-gate static int verify_func(int argc, char *argv[]); 1530Sstevel@tonic-gate static int install_func(int argc, char *argv[]); 1540Sstevel@tonic-gate static int uninstall_func(int argc, char *argv[]); 155766Scarlsonj static int mount_func(int argc, char *argv[]); 156766Scarlsonj static int unmount_func(int argc, char *argv[]); 1571300Sgjelinek static int clone_func(int argc, char *argv[]); 1581300Sgjelinek static int move_func(int argc, char *argv[]); 159*1507Sgjelinek static int detach_func(int argc, char *argv[]); 160*1507Sgjelinek static int attach_func(int argc, char *argv[]); 1610Sstevel@tonic-gate static int sanity_check(char *zone, int cmd_num, boolean_t running, 1620Sstevel@tonic-gate boolean_t unsafe_when_running); 1630Sstevel@tonic-gate static int cmd_match(char *cmd); 1640Sstevel@tonic-gate static int verify_details(int); 1650Sstevel@tonic-gate 1660Sstevel@tonic-gate static struct cmd cmdtab[] = { 1670Sstevel@tonic-gate { CMD_HELP, "help", SHELP_HELP, help_func }, 1680Sstevel@tonic-gate { CMD_BOOT, "boot", SHELP_BOOT, boot_func }, 1690Sstevel@tonic-gate { CMD_HALT, "halt", SHELP_HALT, halt_func }, 1700Sstevel@tonic-gate { CMD_READY, "ready", SHELP_READY, ready_func }, 1710Sstevel@tonic-gate { CMD_REBOOT, "reboot", SHELP_REBOOT, reboot_func }, 1720Sstevel@tonic-gate { CMD_LIST, "list", SHELP_LIST, list_func }, 1730Sstevel@tonic-gate { CMD_VERIFY, "verify", SHELP_VERIFY, verify_func }, 1740Sstevel@tonic-gate { CMD_INSTALL, "install", SHELP_INSTALL, install_func }, 1750Sstevel@tonic-gate { CMD_UNINSTALL, "uninstall", SHELP_UNINSTALL, 176766Scarlsonj uninstall_func }, 1771300Sgjelinek /* mount and unmount are private commands for admin/install */ 178766Scarlsonj { CMD_MOUNT, "mount", NULL, mount_func }, 1791300Sgjelinek { CMD_UNMOUNT, "unmount", NULL, unmount_func }, 1801300Sgjelinek { CMD_CLONE, "clone", SHELP_CLONE, clone_func }, 181*1507Sgjelinek { CMD_MOVE, "move", SHELP_MOVE, move_func }, 182*1507Sgjelinek { CMD_DETACH, "detach", SHELP_DETACH, detach_func }, 183*1507Sgjelinek { CMD_ATTACH, "attach", SHELP_ATTACH, attach_func } 1840Sstevel@tonic-gate }; 1850Sstevel@tonic-gate 1860Sstevel@tonic-gate /* global variables */ 1870Sstevel@tonic-gate 1880Sstevel@tonic-gate /* set early in main(), never modified thereafter, used all over the place */ 1890Sstevel@tonic-gate static char *execname; 1900Sstevel@tonic-gate static char *target_zone; 1910Sstevel@tonic-gate static char *locale; 1920Sstevel@tonic-gate 1930Sstevel@tonic-gate /* used in do_subproc() and signal handler */ 1940Sstevel@tonic-gate static volatile boolean_t child_killed; 1950Sstevel@tonic-gate 1960Sstevel@tonic-gate static char * 1970Sstevel@tonic-gate cmd_to_str(int cmd_num) 1980Sstevel@tonic-gate { 1990Sstevel@tonic-gate assert(cmd_num >= CMD_MIN && cmd_num <= CMD_MAX); 2000Sstevel@tonic-gate return (cmdtab[cmd_num].cmd_name); 2010Sstevel@tonic-gate } 2020Sstevel@tonic-gate 2030Sstevel@tonic-gate /* This is a separate function because of gettext() wrapping. */ 2040Sstevel@tonic-gate static char * 2050Sstevel@tonic-gate long_help(int cmd_num) 2060Sstevel@tonic-gate { 207222Scomay assert(cmd_num >= CMD_MIN && cmd_num <= CMD_MAX); 2080Sstevel@tonic-gate switch (cmd_num) { 2090Sstevel@tonic-gate case CMD_HELP: 2100Sstevel@tonic-gate return (gettext("Print usage message.")); 2110Sstevel@tonic-gate case CMD_BOOT: 2120Sstevel@tonic-gate return (gettext("Activates (boots) specified zone. " 2130Sstevel@tonic-gate "The -s flag can be used\n\tto boot the zone in " 2140Sstevel@tonic-gate "the single-user state.")); 2150Sstevel@tonic-gate case CMD_HALT: 2160Sstevel@tonic-gate return (gettext("Halts specified zone, bypassing " 2170Sstevel@tonic-gate "shutdown scripts and removing runtime\n\t" 2180Sstevel@tonic-gate "resources of the zone.")); 2190Sstevel@tonic-gate case CMD_READY: 2200Sstevel@tonic-gate return (gettext("Prepares a zone for running " 2210Sstevel@tonic-gate "applications but does not start any user\n\t" 2220Sstevel@tonic-gate "processes in the zone.")); 2230Sstevel@tonic-gate case CMD_REBOOT: 2240Sstevel@tonic-gate return (gettext("Restarts the zone (equivalent to a " 2250Sstevel@tonic-gate "halt / boot sequence).\n\tFails if the zone is " 2260Sstevel@tonic-gate "not active.")); 2270Sstevel@tonic-gate case CMD_LIST: 2280Sstevel@tonic-gate return (gettext("Lists the current zones, or a " 2290Sstevel@tonic-gate "specific zone if indicated. By default,\n\tall " 2300Sstevel@tonic-gate "running zones are listed, though this can be " 2310Sstevel@tonic-gate "expanded to all\n\tinstalled zones with the -i " 2320Sstevel@tonic-gate "option or all configured zones with the\n\t-c " 2330Sstevel@tonic-gate "option. When used with the general -z <zone> " 2340Sstevel@tonic-gate "option, lists only the\n\tspecified zone, but " 2350Sstevel@tonic-gate "lists it regardless of its state, and the -i " 2360Sstevel@tonic-gate "and -c\n\toptions are disallowed. The -v option " 2370Sstevel@tonic-gate "can be used to display verbose\n\tinformation: " 2380Sstevel@tonic-gate "zone name, id, current state, root directory and " 2390Sstevel@tonic-gate "options.\n\tThe -p option can be used to request " 2400Sstevel@tonic-gate "machine-parsable output. The -v\n\tand -p " 2410Sstevel@tonic-gate "options are mutually exclusive. If neither -v " 2420Sstevel@tonic-gate "nor -p is used,\n\tjust the zone name is " 2430Sstevel@tonic-gate "listed.")); 2440Sstevel@tonic-gate case CMD_VERIFY: 2450Sstevel@tonic-gate return (gettext("Check to make sure the configuration " 2460Sstevel@tonic-gate "can safely be instantiated\n\ton the machine: " 2470Sstevel@tonic-gate "physical network interfaces exist, etc.")); 2480Sstevel@tonic-gate case CMD_INSTALL: 2490Sstevel@tonic-gate return (gettext("Install the configuration on to the " 2500Sstevel@tonic-gate "system.")); 2510Sstevel@tonic-gate case CMD_UNINSTALL: 2520Sstevel@tonic-gate return (gettext("Uninstall the configuration from the " 2530Sstevel@tonic-gate "system. The -F flag can be used\n\tto force the " 2540Sstevel@tonic-gate "action.")); 2551300Sgjelinek case CMD_CLONE: 2561300Sgjelinek return (gettext("Clone the installation of another " 2571300Sgjelinek "zone.")); 2581300Sgjelinek case CMD_MOVE: 2591300Sgjelinek return (gettext("Move the zone to a new zonepath.")); 260766Scarlsonj default: 261766Scarlsonj return (""); 2620Sstevel@tonic-gate } 2630Sstevel@tonic-gate /* NOTREACHED */ 264222Scomay return (NULL); 2650Sstevel@tonic-gate } 2660Sstevel@tonic-gate 2670Sstevel@tonic-gate /* 2680Sstevel@tonic-gate * Called with explicit B_TRUE when help is explicitly requested, B_FALSE for 2690Sstevel@tonic-gate * unexpected errors. 2700Sstevel@tonic-gate */ 2710Sstevel@tonic-gate 2720Sstevel@tonic-gate static int 2730Sstevel@tonic-gate usage(boolean_t explicit) 2740Sstevel@tonic-gate { 2750Sstevel@tonic-gate int i; 2760Sstevel@tonic-gate FILE *fd = explicit ? stdout : stderr; 2770Sstevel@tonic-gate 2780Sstevel@tonic-gate (void) fprintf(fd, "%s:\t%s help\n", gettext("usage"), execname); 2790Sstevel@tonic-gate (void) fprintf(fd, "\t%s [-z <zone>] list\n", execname); 2800Sstevel@tonic-gate (void) fprintf(fd, "\t%s -z <zone> <%s>\n", execname, 2810Sstevel@tonic-gate gettext("subcommand")); 2820Sstevel@tonic-gate (void) fprintf(fd, "\n%s:\n\n", gettext("Subcommands")); 2830Sstevel@tonic-gate for (i = CMD_MIN; i <= CMD_MAX; i++) { 284766Scarlsonj if (cmdtab[i].short_usage == NULL) 285766Scarlsonj continue; 2860Sstevel@tonic-gate (void) fprintf(fd, "%s\n", cmdtab[i].short_usage); 2870Sstevel@tonic-gate if (explicit) 2880Sstevel@tonic-gate (void) fprintf(fd, "\t%s\n\n", long_help(i)); 2890Sstevel@tonic-gate } 2900Sstevel@tonic-gate if (!explicit) 2910Sstevel@tonic-gate (void) fputs("\n", fd); 2920Sstevel@tonic-gate return (Z_USAGE); 2930Sstevel@tonic-gate } 2940Sstevel@tonic-gate 2950Sstevel@tonic-gate static void 2960Sstevel@tonic-gate sub_usage(char *short_usage, int cmd_num) 2970Sstevel@tonic-gate { 2980Sstevel@tonic-gate (void) fprintf(stderr, "%s:\t%s\n", gettext("usage"), short_usage); 2990Sstevel@tonic-gate (void) fprintf(stderr, "\t%s\n", long_help(cmd_num)); 3000Sstevel@tonic-gate } 3010Sstevel@tonic-gate 3020Sstevel@tonic-gate /* 3030Sstevel@tonic-gate * zperror() is like perror(3c) except that this also prints the executable 3040Sstevel@tonic-gate * name at the start of the message, and takes a boolean indicating whether 3050Sstevel@tonic-gate * to call libc'c strerror() or that from libzonecfg. 3060Sstevel@tonic-gate */ 3070Sstevel@tonic-gate 3080Sstevel@tonic-gate static void 3090Sstevel@tonic-gate zperror(const char *str, boolean_t zonecfg_error) 3100Sstevel@tonic-gate { 3110Sstevel@tonic-gate (void) fprintf(stderr, "%s: %s: %s\n", execname, str, 3120Sstevel@tonic-gate zonecfg_error ? zonecfg_strerror(errno) : strerror(errno)); 3130Sstevel@tonic-gate } 3140Sstevel@tonic-gate 3150Sstevel@tonic-gate /* 3160Sstevel@tonic-gate * zperror2() is very similar to zperror() above, except it also prints a 3170Sstevel@tonic-gate * supplied zone name after the executable. 3180Sstevel@tonic-gate * 3190Sstevel@tonic-gate * All current consumers of this function want libzonecfg's strerror() rather 3200Sstevel@tonic-gate * than libc's; if this ever changes, this function can be made more generic 3210Sstevel@tonic-gate * like zperror() above. 3220Sstevel@tonic-gate */ 3230Sstevel@tonic-gate 3240Sstevel@tonic-gate static void 3250Sstevel@tonic-gate zperror2(const char *zone, const char *str) 3260Sstevel@tonic-gate { 3270Sstevel@tonic-gate (void) fprintf(stderr, "%s: %s: %s: %s\n", execname, zone, str, 3280Sstevel@tonic-gate zonecfg_strerror(errno)); 3290Sstevel@tonic-gate } 3300Sstevel@tonic-gate 3310Sstevel@tonic-gate /* PRINTFLIKE1 */ 3320Sstevel@tonic-gate static void 3330Sstevel@tonic-gate zerror(const char *fmt, ...) 3340Sstevel@tonic-gate { 3350Sstevel@tonic-gate va_list alist; 3360Sstevel@tonic-gate 3370Sstevel@tonic-gate va_start(alist, fmt); 3380Sstevel@tonic-gate (void) fprintf(stderr, "%s: ", execname); 3390Sstevel@tonic-gate if (target_zone != NULL) 3400Sstevel@tonic-gate (void) fprintf(stderr, "zone '%s': ", target_zone); 3410Sstevel@tonic-gate (void) vfprintf(stderr, fmt, alist); 3420Sstevel@tonic-gate (void) fprintf(stderr, "\n"); 3430Sstevel@tonic-gate va_end(alist); 3440Sstevel@tonic-gate } 3450Sstevel@tonic-gate 3460Sstevel@tonic-gate static void * 3470Sstevel@tonic-gate safe_calloc(size_t nelem, size_t elsize) 3480Sstevel@tonic-gate { 3490Sstevel@tonic-gate void *r = calloc(nelem, elsize); 3500Sstevel@tonic-gate 3510Sstevel@tonic-gate if (r == NULL) { 3520Sstevel@tonic-gate zerror(gettext("failed to allocate %lu bytes: %s"), 3530Sstevel@tonic-gate (ulong_t)nelem * elsize, strerror(errno)); 3540Sstevel@tonic-gate exit(Z_ERR); 3550Sstevel@tonic-gate } 3560Sstevel@tonic-gate return (r); 3570Sstevel@tonic-gate } 3580Sstevel@tonic-gate 3590Sstevel@tonic-gate static void 3600Sstevel@tonic-gate zone_print(zone_entry_t *zent, boolean_t verbose, boolean_t parsable) 3610Sstevel@tonic-gate { 3620Sstevel@tonic-gate static boolean_t firsttime = B_TRUE; 3630Sstevel@tonic-gate 3640Sstevel@tonic-gate assert(!(verbose && parsable)); 3650Sstevel@tonic-gate if (firsttime && verbose) { 3660Sstevel@tonic-gate firsttime = B_FALSE; 3670Sstevel@tonic-gate (void) printf("%*s %-16s %-14s %-30s\n", ZONEID_WIDTH, "ID", 3680Sstevel@tonic-gate "NAME", "STATUS", "PATH"); 3690Sstevel@tonic-gate } 3700Sstevel@tonic-gate if (!verbose) { 3710Sstevel@tonic-gate if (!parsable) { 3720Sstevel@tonic-gate (void) printf("%s\n", zent->zname); 3730Sstevel@tonic-gate return; 3740Sstevel@tonic-gate } 3750Sstevel@tonic-gate if (zent->zid == ZONE_ID_UNDEFINED) 3760Sstevel@tonic-gate (void) printf("-"); 3770Sstevel@tonic-gate else 3780Sstevel@tonic-gate (void) printf("%lu", zent->zid); 3790Sstevel@tonic-gate (void) printf(":%s:%s:%s\n", zent->zname, zent->zstate_str, 3800Sstevel@tonic-gate zent->zroot); 3810Sstevel@tonic-gate return; 3820Sstevel@tonic-gate } 3830Sstevel@tonic-gate if (zent->zstate_str != NULL) { 3840Sstevel@tonic-gate if (zent->zid == ZONE_ID_UNDEFINED) 3850Sstevel@tonic-gate (void) printf("%*s", ZONEID_WIDTH, "-"); 3860Sstevel@tonic-gate else 3870Sstevel@tonic-gate (void) printf("%*lu", ZONEID_WIDTH, zent->zid); 3880Sstevel@tonic-gate (void) printf(" %-16s %-14s %-30s\n", zent->zname, 3890Sstevel@tonic-gate zent->zstate_str, zent->zroot); 3900Sstevel@tonic-gate } 3910Sstevel@tonic-gate } 3920Sstevel@tonic-gate 3930Sstevel@tonic-gate static int 394766Scarlsonj lookup_zone_info(const char *zone_name, zoneid_t zid, zone_entry_t *zent) 3950Sstevel@tonic-gate { 3960Sstevel@tonic-gate char root[MAXPATHLEN]; 3970Sstevel@tonic-gate int err; 3980Sstevel@tonic-gate 3990Sstevel@tonic-gate (void) strlcpy(zent->zname, zone_name, sizeof (zent->zname)); 4000Sstevel@tonic-gate (void) strlcpy(zent->zroot, "???", sizeof (zent->zroot)); 4010Sstevel@tonic-gate zent->zstate_str = "???"; 4020Sstevel@tonic-gate 403766Scarlsonj zent->zid = zid; 4040Sstevel@tonic-gate 4050Sstevel@tonic-gate if ((err = zone_get_zonepath(zent->zname, root, sizeof (root))) != 4060Sstevel@tonic-gate Z_OK) { 4070Sstevel@tonic-gate errno = err; 4080Sstevel@tonic-gate zperror2(zent->zname, gettext("could not get zone path")); 4090Sstevel@tonic-gate return (Z_ERR); 4100Sstevel@tonic-gate } 4110Sstevel@tonic-gate (void) strlcpy(zent->zroot, root, sizeof (zent->zroot)); 4120Sstevel@tonic-gate 4130Sstevel@tonic-gate if ((err = zone_get_state(zent->zname, &zent->zstate_num)) != Z_OK) { 4140Sstevel@tonic-gate errno = err; 4150Sstevel@tonic-gate zperror2(zent->zname, gettext("could not get state")); 4160Sstevel@tonic-gate return (Z_ERR); 4170Sstevel@tonic-gate } 4180Sstevel@tonic-gate zent->zstate_str = zone_state_str(zent->zstate_num); 4190Sstevel@tonic-gate 4200Sstevel@tonic-gate return (Z_OK); 4210Sstevel@tonic-gate } 4220Sstevel@tonic-gate 4230Sstevel@tonic-gate /* 4240Sstevel@tonic-gate * fetch_zents() calls zone_list(2) to find out how many zones are running 4250Sstevel@tonic-gate * (which is stored in the global nzents), then calls zone_list(2) again 4260Sstevel@tonic-gate * to fetch the list of running zones (stored in the global zents). This 4270Sstevel@tonic-gate * function may be called multiple times, so if zents is already set, we 4280Sstevel@tonic-gate * return immediately to save work. 4290Sstevel@tonic-gate */ 4300Sstevel@tonic-gate 4310Sstevel@tonic-gate static int 432766Scarlsonj fetch_zents(void) 4330Sstevel@tonic-gate { 4340Sstevel@tonic-gate zoneid_t *zids = NULL; 4350Sstevel@tonic-gate uint_t nzents_saved; 436766Scarlsonj int i, retv; 437766Scarlsonj FILE *fp; 438766Scarlsonj boolean_t inaltroot; 439766Scarlsonj zone_entry_t *zentp; 4400Sstevel@tonic-gate 4410Sstevel@tonic-gate if (nzents > 0) 4420Sstevel@tonic-gate return (Z_OK); 4430Sstevel@tonic-gate 4440Sstevel@tonic-gate if (zone_list(NULL, &nzents) != 0) { 4450Sstevel@tonic-gate zperror(gettext("failed to get zoneid list"), B_FALSE); 4460Sstevel@tonic-gate return (Z_ERR); 4470Sstevel@tonic-gate } 4480Sstevel@tonic-gate 4490Sstevel@tonic-gate again: 4500Sstevel@tonic-gate if (nzents == 0) 4510Sstevel@tonic-gate return (Z_OK); 4520Sstevel@tonic-gate 4530Sstevel@tonic-gate zids = safe_calloc(nzents, sizeof (zoneid_t)); 4540Sstevel@tonic-gate nzents_saved = nzents; 4550Sstevel@tonic-gate 4560Sstevel@tonic-gate if (zone_list(zids, &nzents) != 0) { 4570Sstevel@tonic-gate zperror(gettext("failed to get zone list"), B_FALSE); 4580Sstevel@tonic-gate free(zids); 4590Sstevel@tonic-gate return (Z_ERR); 4600Sstevel@tonic-gate } 4610Sstevel@tonic-gate if (nzents != nzents_saved) { 4620Sstevel@tonic-gate /* list changed, try again */ 4630Sstevel@tonic-gate free(zids); 4640Sstevel@tonic-gate goto again; 4650Sstevel@tonic-gate } 4660Sstevel@tonic-gate 4670Sstevel@tonic-gate zents = safe_calloc(nzents, sizeof (zone_entry_t)); 4680Sstevel@tonic-gate 469766Scarlsonj inaltroot = zonecfg_in_alt_root(); 470766Scarlsonj if (inaltroot) 471766Scarlsonj fp = zonecfg_open_scratch("", B_FALSE); 472766Scarlsonj else 473766Scarlsonj fp = NULL; 474766Scarlsonj zentp = zents; 475766Scarlsonj retv = Z_OK; 4760Sstevel@tonic-gate for (i = 0; i < nzents; i++) { 4770Sstevel@tonic-gate char name[ZONENAME_MAX]; 478766Scarlsonj char altname[ZONENAME_MAX]; 4790Sstevel@tonic-gate 480766Scarlsonj if (getzonenamebyid(zids[i], name, sizeof (name)) < 0) { 4810Sstevel@tonic-gate zperror(gettext("failed to get zone name"), B_FALSE); 482766Scarlsonj retv = Z_ERR; 483766Scarlsonj continue; 484766Scarlsonj } 485766Scarlsonj if (zonecfg_is_scratch(name)) { 486766Scarlsonj /* Ignore scratch zones by default */ 487766Scarlsonj if (!inaltroot) 488766Scarlsonj continue; 489766Scarlsonj if (fp == NULL || 490766Scarlsonj zonecfg_reverse_scratch(fp, name, altname, 491766Scarlsonj sizeof (altname), NULL, 0) == -1) { 492924Sgjelinek zerror(gettext("could not resolve scratch " 493766Scarlsonj "zone %s"), name); 494766Scarlsonj retv = Z_ERR; 495766Scarlsonj continue; 496766Scarlsonj } 497766Scarlsonj (void) strcpy(name, altname); 498766Scarlsonj } else { 499766Scarlsonj /* Ignore non-scratch when in an alternate root */ 500766Scarlsonj if (inaltroot && strcmp(name, GLOBAL_ZONENAME) != 0) 501766Scarlsonj continue; 502766Scarlsonj } 503766Scarlsonj if (lookup_zone_info(name, zids[i], zentp) != Z_OK) { 504766Scarlsonj zerror(gettext("failed to get zone data")); 505766Scarlsonj retv = Z_ERR; 506766Scarlsonj continue; 507766Scarlsonj } 508766Scarlsonj zentp++; 5090Sstevel@tonic-gate } 510766Scarlsonj nzents = zentp - zents; 511766Scarlsonj if (fp != NULL) 512766Scarlsonj zonecfg_close_scratch(fp); 5130Sstevel@tonic-gate 5140Sstevel@tonic-gate free(zids); 515766Scarlsonj return (retv); 5160Sstevel@tonic-gate } 5170Sstevel@tonic-gate 518766Scarlsonj static int 5190Sstevel@tonic-gate zone_print_list(zone_state_t min_state, boolean_t verbose, boolean_t parsable) 5200Sstevel@tonic-gate { 5210Sstevel@tonic-gate int i; 5220Sstevel@tonic-gate zone_entry_t zent; 5230Sstevel@tonic-gate FILE *cookie; 5240Sstevel@tonic-gate char *name; 5250Sstevel@tonic-gate 5260Sstevel@tonic-gate /* 5270Sstevel@tonic-gate * First get the list of running zones from the kernel and print them. 5280Sstevel@tonic-gate * If that is all we need, then return. 5290Sstevel@tonic-gate */ 530766Scarlsonj if ((i = fetch_zents()) != Z_OK) { 5310Sstevel@tonic-gate /* 5320Sstevel@tonic-gate * No need for error messages; fetch_zents() has already taken 5330Sstevel@tonic-gate * care of this. 5340Sstevel@tonic-gate */ 535766Scarlsonj return (i); 5360Sstevel@tonic-gate } 537766Scarlsonj for (i = 0; i < nzents; i++) 5380Sstevel@tonic-gate zone_print(&zents[i], verbose, parsable); 5390Sstevel@tonic-gate if (min_state >= ZONE_STATE_RUNNING) 540766Scarlsonj return (Z_OK); 5410Sstevel@tonic-gate /* 5420Sstevel@tonic-gate * Next, get the full list of zones from the configuration, skipping 5430Sstevel@tonic-gate * any we have already printed. 5440Sstevel@tonic-gate */ 5450Sstevel@tonic-gate cookie = setzoneent(); 5460Sstevel@tonic-gate while ((name = getzoneent(cookie)) != NULL) { 5470Sstevel@tonic-gate for (i = 0; i < nzents; i++) { 5480Sstevel@tonic-gate if (strcmp(zents[i].zname, name) == 0) 5490Sstevel@tonic-gate break; 5500Sstevel@tonic-gate } 5510Sstevel@tonic-gate if (i < nzents) { 5520Sstevel@tonic-gate free(name); 5530Sstevel@tonic-gate continue; 5540Sstevel@tonic-gate } 555766Scarlsonj if (lookup_zone_info(name, ZONE_ID_UNDEFINED, &zent) != Z_OK) { 5560Sstevel@tonic-gate free(name); 5570Sstevel@tonic-gate continue; 5580Sstevel@tonic-gate } 5590Sstevel@tonic-gate free(name); 5600Sstevel@tonic-gate if (zent.zstate_num >= min_state) 5610Sstevel@tonic-gate zone_print(&zent, verbose, parsable); 5620Sstevel@tonic-gate } 5630Sstevel@tonic-gate endzoneent(cookie); 564766Scarlsonj return (Z_OK); 5650Sstevel@tonic-gate } 5660Sstevel@tonic-gate 5670Sstevel@tonic-gate static zone_entry_t * 5680Sstevel@tonic-gate lookup_running_zone(char *str) 5690Sstevel@tonic-gate { 5700Sstevel@tonic-gate zoneid_t zoneid; 5710Sstevel@tonic-gate char *cp; 5720Sstevel@tonic-gate int i; 5730Sstevel@tonic-gate 5740Sstevel@tonic-gate if (fetch_zents() != Z_OK) 5750Sstevel@tonic-gate return (NULL); 5760Sstevel@tonic-gate 5770Sstevel@tonic-gate for (i = 0; i < nzents; i++) { 5780Sstevel@tonic-gate if (strcmp(str, zents[i].zname) == 0) 5790Sstevel@tonic-gate return (&zents[i]); 5800Sstevel@tonic-gate } 5810Sstevel@tonic-gate errno = 0; 5820Sstevel@tonic-gate zoneid = strtol(str, &cp, 0); 5830Sstevel@tonic-gate if (zoneid < MIN_ZONEID || zoneid > MAX_ZONEID || 5840Sstevel@tonic-gate errno != 0 || *cp != '\0') 5850Sstevel@tonic-gate return (NULL); 5860Sstevel@tonic-gate for (i = 0; i < nzents; i++) { 5870Sstevel@tonic-gate if (zoneid == zents[i].zid) 5880Sstevel@tonic-gate return (&zents[i]); 5890Sstevel@tonic-gate } 5900Sstevel@tonic-gate return (NULL); 5910Sstevel@tonic-gate } 5920Sstevel@tonic-gate 5930Sstevel@tonic-gate /* 5940Sstevel@tonic-gate * Check a bit in a mode_t: if on is B_TRUE, that bit should be on; if 5950Sstevel@tonic-gate * B_FALSE, it should be off. Return B_TRUE if the mode is bad (incorrect). 5960Sstevel@tonic-gate */ 5970Sstevel@tonic-gate static boolean_t 5980Sstevel@tonic-gate bad_mode_bit(mode_t mode, mode_t bit, boolean_t on, char *file) 5990Sstevel@tonic-gate { 6000Sstevel@tonic-gate char *str; 6010Sstevel@tonic-gate 6020Sstevel@tonic-gate assert(bit == S_IRUSR || bit == S_IWUSR || bit == S_IXUSR || 6030Sstevel@tonic-gate bit == S_IRGRP || bit == S_IWGRP || bit == S_IXGRP || 6040Sstevel@tonic-gate bit == S_IROTH || bit == S_IWOTH || bit == S_IXOTH); 6050Sstevel@tonic-gate /* 6060Sstevel@tonic-gate * TRANSLATION_NOTE 6070Sstevel@tonic-gate * The strings below will be used as part of a larger message, 6080Sstevel@tonic-gate * either: 6090Sstevel@tonic-gate * (file name) must be (owner|group|world) (read|writ|execut)able 6100Sstevel@tonic-gate * or 6110Sstevel@tonic-gate * (file name) must not be (owner|group|world) (read|writ|execut)able 6120Sstevel@tonic-gate */ 6130Sstevel@tonic-gate switch (bit) { 6140Sstevel@tonic-gate case S_IRUSR: 6150Sstevel@tonic-gate str = gettext("owner readable"); 6160Sstevel@tonic-gate break; 6170Sstevel@tonic-gate case S_IWUSR: 6180Sstevel@tonic-gate str = gettext("owner writable"); 6190Sstevel@tonic-gate break; 6200Sstevel@tonic-gate case S_IXUSR: 6210Sstevel@tonic-gate str = gettext("owner executable"); 6220Sstevel@tonic-gate break; 6230Sstevel@tonic-gate case S_IRGRP: 6240Sstevel@tonic-gate str = gettext("group readable"); 6250Sstevel@tonic-gate break; 6260Sstevel@tonic-gate case S_IWGRP: 6270Sstevel@tonic-gate str = gettext("group writable"); 6280Sstevel@tonic-gate break; 6290Sstevel@tonic-gate case S_IXGRP: 6300Sstevel@tonic-gate str = gettext("group executable"); 6310Sstevel@tonic-gate break; 6320Sstevel@tonic-gate case S_IROTH: 6330Sstevel@tonic-gate str = gettext("world readable"); 6340Sstevel@tonic-gate break; 6350Sstevel@tonic-gate case S_IWOTH: 6360Sstevel@tonic-gate str = gettext("world writable"); 6370Sstevel@tonic-gate break; 6380Sstevel@tonic-gate case S_IXOTH: 6390Sstevel@tonic-gate str = gettext("world executable"); 6400Sstevel@tonic-gate break; 6410Sstevel@tonic-gate } 6420Sstevel@tonic-gate if ((mode & bit) == (on ? 0 : bit)) { 6430Sstevel@tonic-gate /* 6440Sstevel@tonic-gate * TRANSLATION_NOTE 6450Sstevel@tonic-gate * The first parameter below is a file name; the second 6460Sstevel@tonic-gate * is one of the "(owner|group|world) (read|writ|execut)able" 6470Sstevel@tonic-gate * strings from above. 6480Sstevel@tonic-gate */ 6490Sstevel@tonic-gate /* 6500Sstevel@tonic-gate * The code below could be simplified but not in a way 6510Sstevel@tonic-gate * that would easily translate to non-English locales. 6520Sstevel@tonic-gate */ 6530Sstevel@tonic-gate if (on) { 6540Sstevel@tonic-gate (void) fprintf(stderr, gettext("%s must be %s.\n"), 6550Sstevel@tonic-gate file, str); 6560Sstevel@tonic-gate } else { 6570Sstevel@tonic-gate (void) fprintf(stderr, gettext("%s must not be %s.\n"), 6580Sstevel@tonic-gate file, str); 6590Sstevel@tonic-gate } 6600Sstevel@tonic-gate return (B_TRUE); 6610Sstevel@tonic-gate } 6620Sstevel@tonic-gate return (B_FALSE); 6630Sstevel@tonic-gate } 6640Sstevel@tonic-gate 6650Sstevel@tonic-gate /* 6660Sstevel@tonic-gate * We want to make sure that no zone has its zone path as a child node 6670Sstevel@tonic-gate * (in the directory sense) of any other. We do that by comparing this 6680Sstevel@tonic-gate * zone's path to the path of all other (non-global) zones. The comparison 6690Sstevel@tonic-gate * in each case is simple: add '/' to the end of the path, then do a 6700Sstevel@tonic-gate * strncmp() of the two paths, using the length of the shorter one. 6710Sstevel@tonic-gate */ 6720Sstevel@tonic-gate 6730Sstevel@tonic-gate static int 6740Sstevel@tonic-gate crosscheck_zonepaths(char *path) 6750Sstevel@tonic-gate { 6760Sstevel@tonic-gate char rpath[MAXPATHLEN]; /* resolved path */ 6770Sstevel@tonic-gate char path_copy[MAXPATHLEN]; /* copy of original path */ 6780Sstevel@tonic-gate char rpath_copy[MAXPATHLEN]; /* copy of original rpath */ 6790Sstevel@tonic-gate struct zoneent *ze; 6800Sstevel@tonic-gate int res, err; 6810Sstevel@tonic-gate FILE *cookie; 6820Sstevel@tonic-gate 6830Sstevel@tonic-gate cookie = setzoneent(); 6840Sstevel@tonic-gate while ((ze = getzoneent_private(cookie)) != NULL) { 6850Sstevel@tonic-gate /* Skip zones which are not installed. */ 6860Sstevel@tonic-gate if (ze->zone_state < ZONE_STATE_INSTALLED) { 6870Sstevel@tonic-gate free(ze); 6880Sstevel@tonic-gate continue; 6890Sstevel@tonic-gate } 6900Sstevel@tonic-gate /* Skip the global zone and the current target zone. */ 6910Sstevel@tonic-gate if (strcmp(ze->zone_name, GLOBAL_ZONENAME) == 0 || 6920Sstevel@tonic-gate strcmp(ze->zone_name, target_zone) == 0) { 6930Sstevel@tonic-gate free(ze); 6940Sstevel@tonic-gate continue; 6950Sstevel@tonic-gate } 6960Sstevel@tonic-gate if (strlen(ze->zone_path) == 0) { 6970Sstevel@tonic-gate /* old index file without path, fall back */ 6980Sstevel@tonic-gate if ((err = zone_get_zonepath(ze->zone_name, 6990Sstevel@tonic-gate ze->zone_path, sizeof (ze->zone_path))) != Z_OK) { 7000Sstevel@tonic-gate errno = err; 7010Sstevel@tonic-gate zperror2(ze->zone_name, 7020Sstevel@tonic-gate gettext("could not get zone path")); 7030Sstevel@tonic-gate free(ze); 7040Sstevel@tonic-gate continue; 7050Sstevel@tonic-gate } 7060Sstevel@tonic-gate } 707766Scarlsonj (void) snprintf(path_copy, sizeof (path_copy), "%s%s", 708766Scarlsonj zonecfg_get_root(), ze->zone_path); 709766Scarlsonj res = resolvepath(path_copy, rpath, sizeof (rpath)); 7100Sstevel@tonic-gate if (res == -1) { 7110Sstevel@tonic-gate if (errno != ENOENT) { 712766Scarlsonj zperror(path_copy, B_FALSE); 7130Sstevel@tonic-gate free(ze); 7140Sstevel@tonic-gate return (Z_ERR); 7150Sstevel@tonic-gate } 7160Sstevel@tonic-gate (void) printf(gettext("WARNING: zone %s is installed, " 7170Sstevel@tonic-gate "but its %s %s does not exist.\n"), ze->zone_name, 718766Scarlsonj "zonepath", path_copy); 7190Sstevel@tonic-gate free(ze); 7200Sstevel@tonic-gate continue; 7210Sstevel@tonic-gate } 7220Sstevel@tonic-gate rpath[res] = '\0'; 7230Sstevel@tonic-gate (void) snprintf(path_copy, sizeof (path_copy), "%s/", path); 7240Sstevel@tonic-gate (void) snprintf(rpath_copy, sizeof (rpath_copy), "%s/", rpath); 7250Sstevel@tonic-gate if (strncmp(path_copy, rpath_copy, 7260Sstevel@tonic-gate min(strlen(path_copy), strlen(rpath_copy))) == 0) { 727924Sgjelinek /* 728924Sgjelinek * TRANSLATION_NOTE 729924Sgjelinek * zonepath is a literal that should not be translated. 730924Sgjelinek */ 7310Sstevel@tonic-gate (void) fprintf(stderr, gettext("%s zonepath (%s) and " 7320Sstevel@tonic-gate "%s zonepath (%s) overlap.\n"), 7330Sstevel@tonic-gate target_zone, path, ze->zone_name, rpath); 7340Sstevel@tonic-gate free(ze); 7350Sstevel@tonic-gate return (Z_ERR); 7360Sstevel@tonic-gate } 7370Sstevel@tonic-gate free(ze); 7380Sstevel@tonic-gate } 7390Sstevel@tonic-gate endzoneent(cookie); 7400Sstevel@tonic-gate return (Z_OK); 7410Sstevel@tonic-gate } 7420Sstevel@tonic-gate 7430Sstevel@tonic-gate static int 7440Sstevel@tonic-gate validate_zonepath(char *path, int cmd_num) 7450Sstevel@tonic-gate { 7460Sstevel@tonic-gate int res; /* result of last library/system call */ 7470Sstevel@tonic-gate boolean_t err = B_FALSE; /* have we run into an error? */ 7480Sstevel@tonic-gate struct stat stbuf; 7490Sstevel@tonic-gate struct statvfs vfsbuf; 7500Sstevel@tonic-gate char rpath[MAXPATHLEN]; /* resolved path */ 7510Sstevel@tonic-gate char ppath[MAXPATHLEN]; /* parent path */ 7520Sstevel@tonic-gate char rppath[MAXPATHLEN]; /* resolved parent path */ 7530Sstevel@tonic-gate char rootpath[MAXPATHLEN]; /* root path */ 7540Sstevel@tonic-gate zone_state_t state; 7550Sstevel@tonic-gate 7560Sstevel@tonic-gate if (path[0] != '/') { 7570Sstevel@tonic-gate (void) fprintf(stderr, 7580Sstevel@tonic-gate gettext("%s is not an absolute path.\n"), path); 7590Sstevel@tonic-gate return (Z_ERR); 7600Sstevel@tonic-gate } 7610Sstevel@tonic-gate if ((res = resolvepath(path, rpath, sizeof (rpath))) == -1) { 7620Sstevel@tonic-gate if ((errno != ENOENT) || 7631300Sgjelinek (cmd_num != CMD_VERIFY && cmd_num != CMD_INSTALL && 7641300Sgjelinek cmd_num != CMD_CLONE && cmd_num != CMD_MOVE)) { 7650Sstevel@tonic-gate zperror(path, B_FALSE); 7660Sstevel@tonic-gate return (Z_ERR); 7670Sstevel@tonic-gate } 7680Sstevel@tonic-gate if (cmd_num == CMD_VERIFY) { 769924Sgjelinek /* 770924Sgjelinek * TRANSLATION_NOTE 771924Sgjelinek * zoneadm is a literal that should not be translated. 772924Sgjelinek */ 7730Sstevel@tonic-gate (void) fprintf(stderr, gettext("WARNING: %s does not " 774924Sgjelinek "exist, so it could not be verified.\nWhen " 775924Sgjelinek "'zoneadm %s' is run, '%s' will try to create\n%s, " 776924Sgjelinek "and '%s' will be tried again,\nbut the '%s' may " 777924Sgjelinek "fail if:\nthe parent directory of %s is group- or " 778924Sgjelinek "other-writable\nor\n%s overlaps with any other " 7790Sstevel@tonic-gate "installed zones.\n"), path, 7800Sstevel@tonic-gate cmd_to_str(CMD_INSTALL), cmd_to_str(CMD_INSTALL), 7810Sstevel@tonic-gate path, cmd_to_str(CMD_VERIFY), 7820Sstevel@tonic-gate cmd_to_str(CMD_VERIFY), path, path); 7830Sstevel@tonic-gate return (Z_OK); 7840Sstevel@tonic-gate } 7850Sstevel@tonic-gate /* 7860Sstevel@tonic-gate * The zonepath is supposed to be mode 700 but its 7870Sstevel@tonic-gate * parent(s) 755. So use 755 on the mkdirp() then 7880Sstevel@tonic-gate * chmod() the zonepath itself to 700. 7890Sstevel@tonic-gate */ 7900Sstevel@tonic-gate if (mkdirp(path, DEFAULT_DIR_MODE) < 0) { 7910Sstevel@tonic-gate zperror(path, B_FALSE); 7920Sstevel@tonic-gate return (Z_ERR); 7930Sstevel@tonic-gate } 7940Sstevel@tonic-gate /* 7950Sstevel@tonic-gate * If the chmod() fails, report the error, but might 7960Sstevel@tonic-gate * as well continue the verify procedure. 7970Sstevel@tonic-gate */ 7980Sstevel@tonic-gate if (chmod(path, S_IRWXU) != 0) 7990Sstevel@tonic-gate zperror(path, B_FALSE); 8000Sstevel@tonic-gate /* 8010Sstevel@tonic-gate * Since the mkdir() succeeded, we should not have to 8020Sstevel@tonic-gate * worry about a subsequent ENOENT, thus this should 8030Sstevel@tonic-gate * only recurse once. 8040Sstevel@tonic-gate */ 8051300Sgjelinek return (validate_zonepath(path, cmd_num)); 8060Sstevel@tonic-gate } 8070Sstevel@tonic-gate rpath[res] = '\0'; 8080Sstevel@tonic-gate if (strcmp(path, rpath) != 0) { 8090Sstevel@tonic-gate errno = Z_RESOLVED_PATH; 8100Sstevel@tonic-gate zperror(path, B_TRUE); 8110Sstevel@tonic-gate return (Z_ERR); 8120Sstevel@tonic-gate } 8130Sstevel@tonic-gate if ((res = stat(rpath, &stbuf)) != 0) { 8140Sstevel@tonic-gate zperror(rpath, B_FALSE); 8150Sstevel@tonic-gate return (Z_ERR); 8160Sstevel@tonic-gate } 8170Sstevel@tonic-gate if (!S_ISDIR(stbuf.st_mode)) { 8180Sstevel@tonic-gate (void) fprintf(stderr, gettext("%s is not a directory.\n"), 8190Sstevel@tonic-gate rpath); 8200Sstevel@tonic-gate return (Z_ERR); 8210Sstevel@tonic-gate } 8220Sstevel@tonic-gate if ((strcmp(stbuf.st_fstype, MNTTYPE_TMPFS) == 0) || 8230Sstevel@tonic-gate (strcmp(stbuf.st_fstype, MNTTYPE_XMEMFS) == 0)) { 8240Sstevel@tonic-gate (void) printf(gettext("WARNING: %s is on a temporary " 8250Sstevel@tonic-gate "file-system.\n"), rpath); 8260Sstevel@tonic-gate } 8270Sstevel@tonic-gate if (crosscheck_zonepaths(rpath) != Z_OK) 8280Sstevel@tonic-gate return (Z_ERR); 8290Sstevel@tonic-gate /* 8300Sstevel@tonic-gate * Try to collect and report as many minor errors as possible 8310Sstevel@tonic-gate * before returning, so the user can learn everything that needs 8320Sstevel@tonic-gate * to be fixed up front. 8330Sstevel@tonic-gate */ 8340Sstevel@tonic-gate if (stbuf.st_uid != 0) { 8350Sstevel@tonic-gate (void) fprintf(stderr, gettext("%s is not owned by root.\n"), 8360Sstevel@tonic-gate rpath); 8370Sstevel@tonic-gate err = B_TRUE; 8380Sstevel@tonic-gate } 8390Sstevel@tonic-gate err |= bad_mode_bit(stbuf.st_mode, S_IRUSR, B_TRUE, rpath); 8400Sstevel@tonic-gate err |= bad_mode_bit(stbuf.st_mode, S_IWUSR, B_TRUE, rpath); 8410Sstevel@tonic-gate err |= bad_mode_bit(stbuf.st_mode, S_IXUSR, B_TRUE, rpath); 8420Sstevel@tonic-gate err |= bad_mode_bit(stbuf.st_mode, S_IRGRP, B_FALSE, rpath); 8430Sstevel@tonic-gate err |= bad_mode_bit(stbuf.st_mode, S_IWGRP, B_FALSE, rpath); 8440Sstevel@tonic-gate err |= bad_mode_bit(stbuf.st_mode, S_IXGRP, B_FALSE, rpath); 8450Sstevel@tonic-gate err |= bad_mode_bit(stbuf.st_mode, S_IROTH, B_FALSE, rpath); 8460Sstevel@tonic-gate err |= bad_mode_bit(stbuf.st_mode, S_IWOTH, B_FALSE, rpath); 8470Sstevel@tonic-gate err |= bad_mode_bit(stbuf.st_mode, S_IXOTH, B_FALSE, rpath); 8480Sstevel@tonic-gate 8490Sstevel@tonic-gate (void) snprintf(ppath, sizeof (ppath), "%s/..", path); 8500Sstevel@tonic-gate if ((res = resolvepath(ppath, rppath, sizeof (rppath))) == -1) { 8510Sstevel@tonic-gate zperror(ppath, B_FALSE); 8520Sstevel@tonic-gate return (Z_ERR); 8530Sstevel@tonic-gate } 8540Sstevel@tonic-gate rppath[res] = '\0'; 8550Sstevel@tonic-gate if ((res = stat(rppath, &stbuf)) != 0) { 8560Sstevel@tonic-gate zperror(rppath, B_FALSE); 8570Sstevel@tonic-gate return (Z_ERR); 8580Sstevel@tonic-gate } 8590Sstevel@tonic-gate /* theoretically impossible */ 8600Sstevel@tonic-gate if (!S_ISDIR(stbuf.st_mode)) { 8610Sstevel@tonic-gate (void) fprintf(stderr, gettext("%s is not a directory.\n"), 8620Sstevel@tonic-gate rppath); 8630Sstevel@tonic-gate return (Z_ERR); 8640Sstevel@tonic-gate } 8650Sstevel@tonic-gate if (stbuf.st_uid != 0) { 8660Sstevel@tonic-gate (void) fprintf(stderr, gettext("%s is not owned by root.\n"), 8670Sstevel@tonic-gate rppath); 8680Sstevel@tonic-gate err = B_TRUE; 8690Sstevel@tonic-gate } 8700Sstevel@tonic-gate err |= bad_mode_bit(stbuf.st_mode, S_IRUSR, B_TRUE, rppath); 8710Sstevel@tonic-gate err |= bad_mode_bit(stbuf.st_mode, S_IWUSR, B_TRUE, rppath); 8720Sstevel@tonic-gate err |= bad_mode_bit(stbuf.st_mode, S_IXUSR, B_TRUE, rppath); 8730Sstevel@tonic-gate err |= bad_mode_bit(stbuf.st_mode, S_IWGRP, B_FALSE, rppath); 8740Sstevel@tonic-gate err |= bad_mode_bit(stbuf.st_mode, S_IWOTH, B_FALSE, rppath); 8750Sstevel@tonic-gate if (strcmp(rpath, rppath) == 0) { 8760Sstevel@tonic-gate (void) fprintf(stderr, gettext("%s is its own parent.\n"), 8770Sstevel@tonic-gate rppath); 8780Sstevel@tonic-gate err = B_TRUE; 8790Sstevel@tonic-gate } 8800Sstevel@tonic-gate 8810Sstevel@tonic-gate if (statvfs(rpath, &vfsbuf) != 0) { 8820Sstevel@tonic-gate zperror(rpath, B_FALSE); 8830Sstevel@tonic-gate return (Z_ERR); 8840Sstevel@tonic-gate } 885823Sgjelinek if (strcmp(vfsbuf.f_basetype, MNTTYPE_NFS) == 0) { 886924Sgjelinek /* 887924Sgjelinek * TRANSLATION_NOTE 888924Sgjelinek * Zonepath and NFS are literals that should not be translated. 889924Sgjelinek */ 890924Sgjelinek (void) fprintf(stderr, gettext("Zonepath %s is on an NFS " 891924Sgjelinek "mounted file-system.\n" 892924Sgjelinek "\tA local file-system must be used.\n"), rpath); 893823Sgjelinek return (Z_ERR); 894823Sgjelinek } 895823Sgjelinek if (vfsbuf.f_flag & ST_NOSUID) { 896924Sgjelinek /* 897924Sgjelinek * TRANSLATION_NOTE 898924Sgjelinek * Zonepath and nosuid are literals that should not be 899924Sgjelinek * translated. 900924Sgjelinek */ 901823Sgjelinek (void) fprintf(stderr, gettext("Zonepath %s is on a nosuid " 902924Sgjelinek "file-system.\n"), rpath); 9030Sstevel@tonic-gate return (Z_ERR); 9040Sstevel@tonic-gate } 9050Sstevel@tonic-gate 9060Sstevel@tonic-gate if ((res = zone_get_state(target_zone, &state)) != Z_OK) { 9070Sstevel@tonic-gate errno = res; 9080Sstevel@tonic-gate zperror2(target_zone, gettext("could not get state")); 9090Sstevel@tonic-gate return (Z_ERR); 9100Sstevel@tonic-gate } 9110Sstevel@tonic-gate /* 9120Sstevel@tonic-gate * The existence of the root path is only bad in the configured state, 9130Sstevel@tonic-gate * as it is *supposed* to be there at the installed and later states. 914*1507Sgjelinek * However, the root path is expected to be there if the zone is 915*1507Sgjelinek * detached. 9160Sstevel@tonic-gate * State/command mismatches are caught earlier in verify_details(). 9170Sstevel@tonic-gate */ 918*1507Sgjelinek if (state == ZONE_STATE_CONFIGURED && cmd_num != CMD_ATTACH) { 9190Sstevel@tonic-gate if (snprintf(rootpath, sizeof (rootpath), "%s/root", rpath) >= 9200Sstevel@tonic-gate sizeof (rootpath)) { 921924Sgjelinek /* 922924Sgjelinek * TRANSLATION_NOTE 923924Sgjelinek * Zonepath is a literal that should not be translated. 924924Sgjelinek */ 9250Sstevel@tonic-gate (void) fprintf(stderr, 9260Sstevel@tonic-gate gettext("Zonepath %s is too long.\n"), rpath); 9270Sstevel@tonic-gate return (Z_ERR); 9280Sstevel@tonic-gate } 9290Sstevel@tonic-gate if ((res = stat(rootpath, &stbuf)) == 0) { 930*1507Sgjelinek if (zonecfg_detached(rpath)) 931*1507Sgjelinek (void) fprintf(stderr, 932*1507Sgjelinek gettext("Cannot %s detached " 933*1507Sgjelinek "zone.\nUse attach or remove %s " 934*1507Sgjelinek "directory.\n"), cmd_to_str(cmd_num), 935*1507Sgjelinek rpath); 936*1507Sgjelinek else 937*1507Sgjelinek (void) fprintf(stderr, 938*1507Sgjelinek gettext("Rootpath %s exists; " 939*1507Sgjelinek "remove or move aside prior to %s.\n"), 940*1507Sgjelinek rootpath, cmd_to_str(cmd_num)); 9410Sstevel@tonic-gate return (Z_ERR); 9420Sstevel@tonic-gate } 9430Sstevel@tonic-gate } 9440Sstevel@tonic-gate 9450Sstevel@tonic-gate return (err ? Z_ERR : Z_OK); 9460Sstevel@tonic-gate } 9470Sstevel@tonic-gate 9480Sstevel@tonic-gate static void 9490Sstevel@tonic-gate release_lock_file(int lockfd) 9500Sstevel@tonic-gate { 9510Sstevel@tonic-gate (void) close(lockfd); 9520Sstevel@tonic-gate } 9530Sstevel@tonic-gate 9540Sstevel@tonic-gate static int 9550Sstevel@tonic-gate grab_lock_file(const char *zone_name, int *lockfd) 9560Sstevel@tonic-gate { 9570Sstevel@tonic-gate char pathbuf[PATH_MAX]; 9580Sstevel@tonic-gate struct flock flock; 9590Sstevel@tonic-gate 960766Scarlsonj if (snprintf(pathbuf, sizeof (pathbuf), "%s%s", zonecfg_get_root(), 961766Scarlsonj ZONES_TMPDIR) >= sizeof (pathbuf)) { 962766Scarlsonj zerror(gettext("alternate root path is too long")); 963766Scarlsonj return (Z_ERR); 964766Scarlsonj } 965766Scarlsonj if (mkdir(pathbuf, S_IRWXU) < 0 && errno != EEXIST) { 966766Scarlsonj zerror(gettext("could not mkdir %s: %s"), pathbuf, 9670Sstevel@tonic-gate strerror(errno)); 9680Sstevel@tonic-gate return (Z_ERR); 9690Sstevel@tonic-gate } 970766Scarlsonj (void) chmod(pathbuf, S_IRWXU); 9710Sstevel@tonic-gate 9720Sstevel@tonic-gate /* 9730Sstevel@tonic-gate * One of these lock files is created for each zone (when needed). 9740Sstevel@tonic-gate * The lock files are not cleaned up (except on system reboot), 9750Sstevel@tonic-gate * but since there is only one per zone, there is no resource 9760Sstevel@tonic-gate * starvation issue. 9770Sstevel@tonic-gate */ 978766Scarlsonj if (snprintf(pathbuf, sizeof (pathbuf), "%s%s/%s.zoneadm.lock", 979766Scarlsonj zonecfg_get_root(), ZONES_TMPDIR, zone_name) >= sizeof (pathbuf)) { 980766Scarlsonj zerror(gettext("alternate root path is too long")); 981766Scarlsonj return (Z_ERR); 982766Scarlsonj } 9830Sstevel@tonic-gate if ((*lockfd = open(pathbuf, O_RDWR|O_CREAT, S_IRUSR|S_IWUSR)) < 0) { 9840Sstevel@tonic-gate zerror(gettext("could not open %s: %s"), pathbuf, 9850Sstevel@tonic-gate strerror(errno)); 9860Sstevel@tonic-gate return (Z_ERR); 9870Sstevel@tonic-gate } 9880Sstevel@tonic-gate /* 9890Sstevel@tonic-gate * Lock the file to synchronize with other zoneadmds 9900Sstevel@tonic-gate */ 9910Sstevel@tonic-gate flock.l_type = F_WRLCK; 9920Sstevel@tonic-gate flock.l_whence = SEEK_SET; 9930Sstevel@tonic-gate flock.l_start = (off_t)0; 9940Sstevel@tonic-gate flock.l_len = (off_t)0; 9950Sstevel@tonic-gate if (fcntl(*lockfd, F_SETLKW, &flock) < 0) { 9960Sstevel@tonic-gate zerror(gettext("unable to lock %s: %s"), pathbuf, 9970Sstevel@tonic-gate strerror(errno)); 9980Sstevel@tonic-gate release_lock_file(*lockfd); 9990Sstevel@tonic-gate return (Z_ERR); 10000Sstevel@tonic-gate } 10010Sstevel@tonic-gate return (Z_OK); 10020Sstevel@tonic-gate } 10030Sstevel@tonic-gate 1004766Scarlsonj static boolean_t 10050Sstevel@tonic-gate get_doorname(const char *zone_name, char *buffer) 10060Sstevel@tonic-gate { 1007766Scarlsonj return (snprintf(buffer, PATH_MAX, "%s" ZONE_DOOR_PATH, 1008766Scarlsonj zonecfg_get_root(), zone_name) < PATH_MAX); 10090Sstevel@tonic-gate } 10100Sstevel@tonic-gate 10110Sstevel@tonic-gate /* 10120Sstevel@tonic-gate * system daemons are not audited. For the global zone, this occurs 10130Sstevel@tonic-gate * "naturally" since init is started with the default audit 10140Sstevel@tonic-gate * characteristics. Since zoneadmd is a system daemon and it starts 10150Sstevel@tonic-gate * init for a zone, it is necessary to clear out the audit 10160Sstevel@tonic-gate * characteristics inherited from whomever started zoneadmd. This is 10170Sstevel@tonic-gate * indicated by the audit id, which is set from the ruid parameter of 10180Sstevel@tonic-gate * adt_set_user(), below. 10190Sstevel@tonic-gate */ 10200Sstevel@tonic-gate 10210Sstevel@tonic-gate static void 10220Sstevel@tonic-gate prepare_audit_context() 10230Sstevel@tonic-gate { 10240Sstevel@tonic-gate adt_session_data_t *ah; 10250Sstevel@tonic-gate char *failure = gettext("audit failure: %s"); 10260Sstevel@tonic-gate 10270Sstevel@tonic-gate if (adt_start_session(&ah, NULL, 0)) { 10280Sstevel@tonic-gate zerror(failure, strerror(errno)); 10290Sstevel@tonic-gate return; 10300Sstevel@tonic-gate } 10310Sstevel@tonic-gate if (adt_set_user(ah, ADT_NO_AUDIT, ADT_NO_AUDIT, 10320Sstevel@tonic-gate ADT_NO_AUDIT, ADT_NO_AUDIT, NULL, ADT_NEW)) { 10330Sstevel@tonic-gate zerror(failure, strerror(errno)); 10340Sstevel@tonic-gate (void) adt_end_session(ah); 10350Sstevel@tonic-gate return; 10360Sstevel@tonic-gate } 10370Sstevel@tonic-gate if (adt_set_proc(ah)) 10380Sstevel@tonic-gate zerror(failure, strerror(errno)); 10390Sstevel@tonic-gate 10400Sstevel@tonic-gate (void) adt_end_session(ah); 10410Sstevel@tonic-gate } 10420Sstevel@tonic-gate 10430Sstevel@tonic-gate static int 10440Sstevel@tonic-gate start_zoneadmd(const char *zone_name) 10450Sstevel@tonic-gate { 10460Sstevel@tonic-gate char doorpath[PATH_MAX]; 10470Sstevel@tonic-gate pid_t child_pid; 10480Sstevel@tonic-gate int error = Z_ERR; 10490Sstevel@tonic-gate int doorfd, lockfd; 10500Sstevel@tonic-gate struct door_info info; 10510Sstevel@tonic-gate 1052766Scarlsonj if (!get_doorname(zone_name, doorpath)) 1053766Scarlsonj return (Z_ERR); 10540Sstevel@tonic-gate 10550Sstevel@tonic-gate if (grab_lock_file(zone_name, &lockfd) != Z_OK) 10560Sstevel@tonic-gate return (Z_ERR); 10570Sstevel@tonic-gate 10580Sstevel@tonic-gate /* 10590Sstevel@tonic-gate * Now that we have the lock, re-confirm that the daemon is 10600Sstevel@tonic-gate * *not* up and working fine. If it is still down, we have a green 10610Sstevel@tonic-gate * light to start it. 10620Sstevel@tonic-gate */ 10630Sstevel@tonic-gate if ((doorfd = open(doorpath, O_RDONLY)) < 0) { 10640Sstevel@tonic-gate if (errno != ENOENT) { 10650Sstevel@tonic-gate zperror(doorpath, B_FALSE); 10660Sstevel@tonic-gate goto out; 10670Sstevel@tonic-gate } 10680Sstevel@tonic-gate } else { 10690Sstevel@tonic-gate if (door_info(doorfd, &info) == 0 && 10700Sstevel@tonic-gate ((info.di_attributes & DOOR_REVOKED) == 0)) { 10710Sstevel@tonic-gate error = Z_OK; 10720Sstevel@tonic-gate (void) close(doorfd); 10730Sstevel@tonic-gate goto out; 10740Sstevel@tonic-gate } 10750Sstevel@tonic-gate (void) close(doorfd); 10760Sstevel@tonic-gate } 10770Sstevel@tonic-gate 10780Sstevel@tonic-gate if ((child_pid = fork()) == -1) { 10790Sstevel@tonic-gate zperror(gettext("could not fork"), B_FALSE); 10800Sstevel@tonic-gate goto out; 10810Sstevel@tonic-gate } else if (child_pid == 0) { 1082766Scarlsonj const char *argv[6], **ap; 1083766Scarlsonj 10840Sstevel@tonic-gate /* child process */ 10850Sstevel@tonic-gate prepare_audit_context(); 10860Sstevel@tonic-gate 1087766Scarlsonj ap = argv; 1088766Scarlsonj *ap++ = "zoneadmd"; 1089766Scarlsonj *ap++ = "-z"; 1090766Scarlsonj *ap++ = zone_name; 1091766Scarlsonj if (zonecfg_in_alt_root()) { 1092766Scarlsonj *ap++ = "-R"; 1093766Scarlsonj *ap++ = zonecfg_get_root(); 1094766Scarlsonj } 1095766Scarlsonj *ap = NULL; 1096766Scarlsonj 1097766Scarlsonj (void) execv("/usr/lib/zones/zoneadmd", (char * const *)argv); 1098924Sgjelinek /* 1099924Sgjelinek * TRANSLATION_NOTE 1100924Sgjelinek * zoneadmd is a literal that should not be translated. 1101924Sgjelinek */ 11020Sstevel@tonic-gate zperror(gettext("could not exec zoneadmd"), B_FALSE); 11030Sstevel@tonic-gate _exit(Z_ERR); 11040Sstevel@tonic-gate } else { 11050Sstevel@tonic-gate /* parent process */ 11060Sstevel@tonic-gate pid_t retval; 11070Sstevel@tonic-gate int pstatus = 0; 11080Sstevel@tonic-gate 11090Sstevel@tonic-gate do { 11100Sstevel@tonic-gate retval = waitpid(child_pid, &pstatus, 0); 11110Sstevel@tonic-gate } while (retval != child_pid); 11120Sstevel@tonic-gate if (WIFSIGNALED(pstatus) || (WIFEXITED(pstatus) && 11130Sstevel@tonic-gate WEXITSTATUS(pstatus) != 0)) { 11140Sstevel@tonic-gate zerror(gettext("could not start %s"), "zoneadmd"); 11150Sstevel@tonic-gate goto out; 11160Sstevel@tonic-gate } 11170Sstevel@tonic-gate } 11180Sstevel@tonic-gate error = Z_OK; 11190Sstevel@tonic-gate out: 11200Sstevel@tonic-gate release_lock_file(lockfd); 11210Sstevel@tonic-gate return (error); 11220Sstevel@tonic-gate } 11230Sstevel@tonic-gate 11240Sstevel@tonic-gate static int 11250Sstevel@tonic-gate ping_zoneadmd(const char *zone_name) 11260Sstevel@tonic-gate { 11270Sstevel@tonic-gate char doorpath[PATH_MAX]; 11280Sstevel@tonic-gate int doorfd; 11290Sstevel@tonic-gate struct door_info info; 11300Sstevel@tonic-gate 1131766Scarlsonj if (!get_doorname(zone_name, doorpath)) 1132766Scarlsonj return (Z_ERR); 11330Sstevel@tonic-gate 11340Sstevel@tonic-gate if ((doorfd = open(doorpath, O_RDONLY)) < 0) { 11350Sstevel@tonic-gate return (Z_ERR); 11360Sstevel@tonic-gate } 11370Sstevel@tonic-gate if (door_info(doorfd, &info) == 0 && 11380Sstevel@tonic-gate ((info.di_attributes & DOOR_REVOKED) == 0)) { 11390Sstevel@tonic-gate (void) close(doorfd); 11400Sstevel@tonic-gate return (Z_OK); 11410Sstevel@tonic-gate } 11420Sstevel@tonic-gate (void) close(doorfd); 11430Sstevel@tonic-gate return (Z_ERR); 11440Sstevel@tonic-gate } 11450Sstevel@tonic-gate 11460Sstevel@tonic-gate static int 11470Sstevel@tonic-gate call_zoneadmd(const char *zone_name, zone_cmd_arg_t *arg) 11480Sstevel@tonic-gate { 11490Sstevel@tonic-gate char doorpath[PATH_MAX]; 11500Sstevel@tonic-gate int doorfd, result; 11510Sstevel@tonic-gate door_arg_t darg; 11520Sstevel@tonic-gate 11530Sstevel@tonic-gate zoneid_t zoneid; 11540Sstevel@tonic-gate uint64_t uniqid = 0; 11550Sstevel@tonic-gate 11560Sstevel@tonic-gate zone_cmd_rval_t *rvalp; 11570Sstevel@tonic-gate size_t rlen; 11580Sstevel@tonic-gate char *cp, *errbuf; 11590Sstevel@tonic-gate 11600Sstevel@tonic-gate rlen = getpagesize(); 11610Sstevel@tonic-gate if ((rvalp = malloc(rlen)) == NULL) { 11620Sstevel@tonic-gate zerror(gettext("failed to allocate %lu bytes: %s"), rlen, 11630Sstevel@tonic-gate strerror(errno)); 11640Sstevel@tonic-gate return (-1); 11650Sstevel@tonic-gate } 11660Sstevel@tonic-gate 11670Sstevel@tonic-gate if ((zoneid = getzoneidbyname(zone_name)) != ZONE_ID_UNDEFINED) { 11680Sstevel@tonic-gate (void) zone_getattr(zoneid, ZONE_ATTR_UNIQID, &uniqid, 11690Sstevel@tonic-gate sizeof (uniqid)); 11700Sstevel@tonic-gate } 11710Sstevel@tonic-gate arg->uniqid = uniqid; 11720Sstevel@tonic-gate (void) strlcpy(arg->locale, locale, sizeof (arg->locale)); 1173766Scarlsonj if (!get_doorname(zone_name, doorpath)) { 1174766Scarlsonj zerror(gettext("alternate root path is too long")); 1175766Scarlsonj free(rvalp); 1176766Scarlsonj return (-1); 1177766Scarlsonj } 11780Sstevel@tonic-gate 11790Sstevel@tonic-gate /* 11800Sstevel@tonic-gate * Loop trying to start zoneadmd; if something goes seriously 11810Sstevel@tonic-gate * wrong we break out and fail. 11820Sstevel@tonic-gate */ 11830Sstevel@tonic-gate for (;;) { 11840Sstevel@tonic-gate if (start_zoneadmd(zone_name) != Z_OK) 11850Sstevel@tonic-gate break; 11860Sstevel@tonic-gate 11870Sstevel@tonic-gate if ((doorfd = open(doorpath, O_RDONLY)) < 0) { 11880Sstevel@tonic-gate zperror(gettext("failed to open zone door"), B_FALSE); 11890Sstevel@tonic-gate break; 11900Sstevel@tonic-gate } 11910Sstevel@tonic-gate 11920Sstevel@tonic-gate darg.data_ptr = (char *)arg; 11930Sstevel@tonic-gate darg.data_size = sizeof (*arg); 11940Sstevel@tonic-gate darg.desc_ptr = NULL; 11950Sstevel@tonic-gate darg.desc_num = 0; 11960Sstevel@tonic-gate darg.rbuf = (char *)rvalp; 11970Sstevel@tonic-gate darg.rsize = rlen; 11980Sstevel@tonic-gate if (door_call(doorfd, &darg) != 0) { 11990Sstevel@tonic-gate (void) close(doorfd); 12000Sstevel@tonic-gate /* 12010Sstevel@tonic-gate * We'll get EBADF if the door has been revoked. 12020Sstevel@tonic-gate */ 12030Sstevel@tonic-gate if (errno != EBADF) { 12040Sstevel@tonic-gate zperror(gettext("door_call failed"), B_FALSE); 12050Sstevel@tonic-gate break; 12060Sstevel@tonic-gate } 12070Sstevel@tonic-gate continue; /* take another lap */ 12080Sstevel@tonic-gate } 12090Sstevel@tonic-gate (void) close(doorfd); 12100Sstevel@tonic-gate 12110Sstevel@tonic-gate if (darg.data_size == 0) { 12120Sstevel@tonic-gate /* Door server is going away; kick it again. */ 12130Sstevel@tonic-gate continue; 12140Sstevel@tonic-gate } 12150Sstevel@tonic-gate 12160Sstevel@tonic-gate errbuf = rvalp->errbuf; 12170Sstevel@tonic-gate while (*errbuf != '\0') { 12180Sstevel@tonic-gate /* 12190Sstevel@tonic-gate * Remove any newlines since zerror() 12200Sstevel@tonic-gate * will append one automatically. 12210Sstevel@tonic-gate */ 12220Sstevel@tonic-gate cp = strchr(errbuf, '\n'); 12230Sstevel@tonic-gate if (cp != NULL) 12240Sstevel@tonic-gate *cp = '\0'; 12250Sstevel@tonic-gate zerror("%s", errbuf); 12260Sstevel@tonic-gate if (cp == NULL) 12270Sstevel@tonic-gate break; 12280Sstevel@tonic-gate errbuf = cp + 1; 12290Sstevel@tonic-gate } 12300Sstevel@tonic-gate result = rvalp->rval == 0 ? 0 : -1; 12310Sstevel@tonic-gate free(rvalp); 12320Sstevel@tonic-gate return (result); 12330Sstevel@tonic-gate } 12340Sstevel@tonic-gate 12350Sstevel@tonic-gate free(rvalp); 12360Sstevel@tonic-gate return (-1); 12370Sstevel@tonic-gate } 12380Sstevel@tonic-gate 12390Sstevel@tonic-gate static int 12400Sstevel@tonic-gate ready_func(int argc, char *argv[]) 12410Sstevel@tonic-gate { 12420Sstevel@tonic-gate zone_cmd_arg_t zarg; 12430Sstevel@tonic-gate int arg; 12440Sstevel@tonic-gate 1245766Scarlsonj if (zonecfg_in_alt_root()) { 1246766Scarlsonj zerror(gettext("cannot ready zone in alternate root")); 1247766Scarlsonj return (Z_ERR); 1248766Scarlsonj } 1249766Scarlsonj 12500Sstevel@tonic-gate optind = 0; 12510Sstevel@tonic-gate if ((arg = getopt(argc, argv, "?")) != EOF) { 12520Sstevel@tonic-gate switch (arg) { 12530Sstevel@tonic-gate case '?': 12540Sstevel@tonic-gate sub_usage(SHELP_READY, CMD_READY); 12550Sstevel@tonic-gate return (optopt == '?' ? Z_OK : Z_USAGE); 12560Sstevel@tonic-gate default: 12570Sstevel@tonic-gate sub_usage(SHELP_READY, CMD_READY); 12580Sstevel@tonic-gate return (Z_USAGE); 12590Sstevel@tonic-gate } 12600Sstevel@tonic-gate } 12610Sstevel@tonic-gate if (argc > optind) { 12620Sstevel@tonic-gate sub_usage(SHELP_READY, CMD_READY); 12630Sstevel@tonic-gate return (Z_USAGE); 12640Sstevel@tonic-gate } 12650Sstevel@tonic-gate if (sanity_check(target_zone, CMD_READY, B_FALSE, B_FALSE) != Z_OK) 12660Sstevel@tonic-gate return (Z_ERR); 12670Sstevel@tonic-gate if (verify_details(CMD_READY) != Z_OK) 12680Sstevel@tonic-gate return (Z_ERR); 12690Sstevel@tonic-gate 12700Sstevel@tonic-gate zarg.cmd = Z_READY; 12710Sstevel@tonic-gate if (call_zoneadmd(target_zone, &zarg) != 0) { 12720Sstevel@tonic-gate zerror(gettext("call to %s failed"), "zoneadmd"); 12730Sstevel@tonic-gate return (Z_ERR); 12740Sstevel@tonic-gate } 12750Sstevel@tonic-gate return (Z_OK); 12760Sstevel@tonic-gate } 12770Sstevel@tonic-gate 12780Sstevel@tonic-gate static int 12790Sstevel@tonic-gate boot_func(int argc, char *argv[]) 12800Sstevel@tonic-gate { 12810Sstevel@tonic-gate zone_cmd_arg_t zarg; 12820Sstevel@tonic-gate int arg; 12830Sstevel@tonic-gate 1284766Scarlsonj if (zonecfg_in_alt_root()) { 1285766Scarlsonj zerror(gettext("cannot boot zone in alternate root")); 1286766Scarlsonj return (Z_ERR); 1287766Scarlsonj } 1288766Scarlsonj 12890Sstevel@tonic-gate zarg.bootbuf[0] = '\0'; 12900Sstevel@tonic-gate 12910Sstevel@tonic-gate /* 12920Sstevel@tonic-gate * At the current time, the only supported subargument to the 12930Sstevel@tonic-gate * "boot" subcommand is "-s" which specifies a single-user boot. 12940Sstevel@tonic-gate * In the future, other boot arguments should be supported 12950Sstevel@tonic-gate * including "-m" for specifying alternate smf(5) milestones. 12960Sstevel@tonic-gate */ 12970Sstevel@tonic-gate optind = 0; 12980Sstevel@tonic-gate if ((arg = getopt(argc, argv, "?s")) != EOF) { 12990Sstevel@tonic-gate switch (arg) { 13000Sstevel@tonic-gate case '?': 13010Sstevel@tonic-gate sub_usage(SHELP_BOOT, CMD_BOOT); 13020Sstevel@tonic-gate return (optopt == '?' ? Z_OK : Z_USAGE); 13030Sstevel@tonic-gate case 's': 13040Sstevel@tonic-gate (void) strlcpy(zarg.bootbuf, "-s", 13050Sstevel@tonic-gate sizeof (zarg.bootbuf)); 13060Sstevel@tonic-gate break; 13070Sstevel@tonic-gate default: 13080Sstevel@tonic-gate sub_usage(SHELP_BOOT, CMD_BOOT); 13090Sstevel@tonic-gate return (Z_USAGE); 13100Sstevel@tonic-gate } 13110Sstevel@tonic-gate } 13120Sstevel@tonic-gate if (argc > optind) { 13130Sstevel@tonic-gate sub_usage(SHELP_BOOT, CMD_BOOT); 13140Sstevel@tonic-gate return (Z_USAGE); 13150Sstevel@tonic-gate } 13160Sstevel@tonic-gate if (sanity_check(target_zone, CMD_BOOT, B_FALSE, B_FALSE) != Z_OK) 13170Sstevel@tonic-gate return (Z_ERR); 13180Sstevel@tonic-gate if (verify_details(CMD_BOOT) != Z_OK) 13190Sstevel@tonic-gate return (Z_ERR); 13200Sstevel@tonic-gate zarg.cmd = Z_BOOT; 13210Sstevel@tonic-gate if (call_zoneadmd(target_zone, &zarg) != 0) { 13220Sstevel@tonic-gate zerror(gettext("call to %s failed"), "zoneadmd"); 13230Sstevel@tonic-gate return (Z_ERR); 13240Sstevel@tonic-gate } 13250Sstevel@tonic-gate return (Z_OK); 13260Sstevel@tonic-gate } 13270Sstevel@tonic-gate 13280Sstevel@tonic-gate static void 13290Sstevel@tonic-gate fake_up_local_zone(zoneid_t zid, zone_entry_t *zeptr) 13300Sstevel@tonic-gate { 13310Sstevel@tonic-gate ssize_t result; 13320Sstevel@tonic-gate 13330Sstevel@tonic-gate zeptr->zid = zid; 13340Sstevel@tonic-gate /* 13350Sstevel@tonic-gate * Since we're looking up our own (non-global) zone name, 13360Sstevel@tonic-gate * we can be assured that it will succeed. 13370Sstevel@tonic-gate */ 13380Sstevel@tonic-gate result = getzonenamebyid(zid, zeptr->zname, sizeof (zeptr->zname)); 13390Sstevel@tonic-gate assert(result >= 0); 13400Sstevel@tonic-gate (void) strlcpy(zeptr->zroot, "/", sizeof (zeptr->zroot)); 13410Sstevel@tonic-gate zeptr->zstate_str = "running"; 13420Sstevel@tonic-gate } 13430Sstevel@tonic-gate 13440Sstevel@tonic-gate static int 13450Sstevel@tonic-gate list_func(int argc, char *argv[]) 13460Sstevel@tonic-gate { 13470Sstevel@tonic-gate zone_entry_t *zentp, zent; 1348766Scarlsonj int arg, retv; 13490Sstevel@tonic-gate boolean_t output = B_FALSE, verbose = B_FALSE, parsable = B_FALSE; 13500Sstevel@tonic-gate zone_state_t min_state = ZONE_STATE_RUNNING; 13510Sstevel@tonic-gate zoneid_t zone_id = getzoneid(); 13520Sstevel@tonic-gate 13530Sstevel@tonic-gate if (target_zone == NULL) { 13540Sstevel@tonic-gate /* all zones: default view to running but allow override */ 13550Sstevel@tonic-gate optind = 0; 13560Sstevel@tonic-gate while ((arg = getopt(argc, argv, "?cipv")) != EOF) { 13570Sstevel@tonic-gate switch (arg) { 13580Sstevel@tonic-gate case '?': 13590Sstevel@tonic-gate sub_usage(SHELP_LIST, CMD_LIST); 13600Sstevel@tonic-gate return (optopt == '?' ? Z_OK : Z_USAGE); 13611339Sjonb /* 13621339Sjonb * The 'i' and 'c' options are not mutually 13631339Sjonb * exclusive so if 'c' is given, then min_state 13641339Sjonb * is set to 0 (ZONE_STATE_CONFIGURED) which is 13651339Sjonb * the lowest possible state. If 'i' is given, 13661339Sjonb * then min_state is set to be the lowest state 13671339Sjonb * so far. 13681339Sjonb */ 13690Sstevel@tonic-gate case 'c': 13700Sstevel@tonic-gate min_state = ZONE_STATE_CONFIGURED; 13710Sstevel@tonic-gate break; 13720Sstevel@tonic-gate case 'i': 13731339Sjonb min_state = min(ZONE_STATE_INSTALLED, 13741339Sjonb min_state); 13751339Sjonb 13760Sstevel@tonic-gate break; 13770Sstevel@tonic-gate case 'p': 13780Sstevel@tonic-gate parsable = B_TRUE; 13790Sstevel@tonic-gate break; 13800Sstevel@tonic-gate case 'v': 13810Sstevel@tonic-gate verbose = B_TRUE; 13820Sstevel@tonic-gate break; 13830Sstevel@tonic-gate default: 13840Sstevel@tonic-gate sub_usage(SHELP_LIST, CMD_LIST); 13850Sstevel@tonic-gate return (Z_USAGE); 13860Sstevel@tonic-gate } 13870Sstevel@tonic-gate } 13880Sstevel@tonic-gate if (parsable && verbose) { 13890Sstevel@tonic-gate zerror(gettext("%s -p and -v are mutually exclusive."), 13900Sstevel@tonic-gate cmd_to_str(CMD_LIST)); 13910Sstevel@tonic-gate return (Z_ERR); 13920Sstevel@tonic-gate } 13930Sstevel@tonic-gate if (zone_id == GLOBAL_ZONEID) { 1394766Scarlsonj retv = zone_print_list(min_state, verbose, parsable); 13950Sstevel@tonic-gate } else { 1396766Scarlsonj retv = Z_OK; 13970Sstevel@tonic-gate fake_up_local_zone(zone_id, &zent); 13980Sstevel@tonic-gate zone_print(&zent, verbose, parsable); 13990Sstevel@tonic-gate } 1400766Scarlsonj return (retv); 14010Sstevel@tonic-gate } 14020Sstevel@tonic-gate 14030Sstevel@tonic-gate /* 14040Sstevel@tonic-gate * Specific target zone: disallow -i/-c suboptions. 14050Sstevel@tonic-gate */ 14060Sstevel@tonic-gate optind = 0; 14070Sstevel@tonic-gate while ((arg = getopt(argc, argv, "?pv")) != EOF) { 14080Sstevel@tonic-gate switch (arg) { 14090Sstevel@tonic-gate case '?': 14100Sstevel@tonic-gate sub_usage(SHELP_LIST, CMD_LIST); 14110Sstevel@tonic-gate return (optopt == '?' ? Z_OK : Z_USAGE); 14120Sstevel@tonic-gate case 'p': 14130Sstevel@tonic-gate parsable = B_TRUE; 14140Sstevel@tonic-gate break; 14150Sstevel@tonic-gate case 'v': 14160Sstevel@tonic-gate verbose = B_TRUE; 14170Sstevel@tonic-gate break; 14180Sstevel@tonic-gate default: 14190Sstevel@tonic-gate sub_usage(SHELP_LIST, CMD_LIST); 14200Sstevel@tonic-gate return (Z_USAGE); 14210Sstevel@tonic-gate } 14220Sstevel@tonic-gate } 14230Sstevel@tonic-gate if (parsable && verbose) { 14240Sstevel@tonic-gate zerror(gettext("%s -p and -v are mutually exclusive."), 14250Sstevel@tonic-gate cmd_to_str(CMD_LIST)); 14260Sstevel@tonic-gate return (Z_ERR); 14270Sstevel@tonic-gate } 14280Sstevel@tonic-gate if (argc > optind) { 14290Sstevel@tonic-gate sub_usage(SHELP_LIST, CMD_LIST); 14300Sstevel@tonic-gate return (Z_USAGE); 14310Sstevel@tonic-gate } 14320Sstevel@tonic-gate if (zone_id != GLOBAL_ZONEID) { 14330Sstevel@tonic-gate fake_up_local_zone(zone_id, &zent); 14340Sstevel@tonic-gate /* 14350Sstevel@tonic-gate * main() will issue a Z_NO_ZONE error if it cannot get an 14360Sstevel@tonic-gate * id for target_zone, which in a non-global zone should 14370Sstevel@tonic-gate * happen for any zone name except `zonename`. Thus we 14380Sstevel@tonic-gate * assert() that here but don't otherwise check. 14390Sstevel@tonic-gate */ 14400Sstevel@tonic-gate assert(strcmp(zent.zname, target_zone) == 0); 14410Sstevel@tonic-gate zone_print(&zent, verbose, parsable); 14420Sstevel@tonic-gate output = B_TRUE; 14430Sstevel@tonic-gate } else if ((zentp = lookup_running_zone(target_zone)) != NULL) { 14440Sstevel@tonic-gate zone_print(zentp, verbose, parsable); 14450Sstevel@tonic-gate output = B_TRUE; 1446766Scarlsonj } else if (lookup_zone_info(target_zone, ZONE_ID_UNDEFINED, 1447766Scarlsonj &zent) == Z_OK) { 14480Sstevel@tonic-gate zone_print(&zent, verbose, parsable); 14490Sstevel@tonic-gate output = B_TRUE; 14500Sstevel@tonic-gate } 14510Sstevel@tonic-gate return (output ? Z_OK : Z_ERR); 14520Sstevel@tonic-gate } 14530Sstevel@tonic-gate 14540Sstevel@tonic-gate static void 14550Sstevel@tonic-gate sigterm(int sig) 14560Sstevel@tonic-gate { 14570Sstevel@tonic-gate /* 14580Sstevel@tonic-gate * Ignore SIG{INT,TERM}, so we don't end up in an infinite loop, 14590Sstevel@tonic-gate * then propagate the signal to our process group. 14600Sstevel@tonic-gate */ 14610Sstevel@tonic-gate (void) sigset(SIGINT, SIG_IGN); 14620Sstevel@tonic-gate (void) sigset(SIGTERM, SIG_IGN); 14630Sstevel@tonic-gate (void) kill(0, sig); 14640Sstevel@tonic-gate child_killed = B_TRUE; 14650Sstevel@tonic-gate } 14660Sstevel@tonic-gate 14670Sstevel@tonic-gate static int 14680Sstevel@tonic-gate do_subproc(char *cmdbuf) 14690Sstevel@tonic-gate { 14700Sstevel@tonic-gate char inbuf[1024]; /* arbitrary large amount */ 14710Sstevel@tonic-gate FILE *file; 14720Sstevel@tonic-gate 14730Sstevel@tonic-gate child_killed = B_FALSE; 14740Sstevel@tonic-gate /* 14750Sstevel@tonic-gate * We use popen(3c) to launch child processes for [un]install; 14760Sstevel@tonic-gate * this library call does not return a PID, so we have to kill 14770Sstevel@tonic-gate * the whole process group. To avoid killing our parent, we 14780Sstevel@tonic-gate * become a process group leader here. But doing so can wreak 14790Sstevel@tonic-gate * havoc with reading from stdin when launched by a non-job-control 14800Sstevel@tonic-gate * shell, so we close stdin and reopen it as /dev/null first. 14810Sstevel@tonic-gate */ 14820Sstevel@tonic-gate (void) close(STDIN_FILENO); 14830Sstevel@tonic-gate (void) open("/dev/null", O_RDONLY); 14840Sstevel@tonic-gate (void) setpgid(0, 0); 14850Sstevel@tonic-gate (void) sigset(SIGINT, sigterm); 14860Sstevel@tonic-gate (void) sigset(SIGTERM, sigterm); 14870Sstevel@tonic-gate file = popen(cmdbuf, "r"); 14880Sstevel@tonic-gate for (;;) { 14890Sstevel@tonic-gate if (child_killed || fgets(inbuf, sizeof (inbuf), file) == NULL) 14900Sstevel@tonic-gate break; 14910Sstevel@tonic-gate (void) fputs(inbuf, stdout); 14920Sstevel@tonic-gate } 14930Sstevel@tonic-gate (void) sigset(SIGINT, SIG_DFL); 14940Sstevel@tonic-gate (void) sigset(SIGTERM, SIG_DFL); 14950Sstevel@tonic-gate return (pclose(file)); 14960Sstevel@tonic-gate } 14970Sstevel@tonic-gate 14980Sstevel@tonic-gate static int 14990Sstevel@tonic-gate subproc_status(const char *cmd, int status) 15000Sstevel@tonic-gate { 15010Sstevel@tonic-gate if (WIFEXITED(status)) { 15020Sstevel@tonic-gate int exit_code = WEXITSTATUS(status); 15030Sstevel@tonic-gate 15040Sstevel@tonic-gate if (exit_code == 0) 15050Sstevel@tonic-gate return (Z_OK); 15060Sstevel@tonic-gate zerror(gettext("'%s' failed with exit code %d."), cmd, 15070Sstevel@tonic-gate exit_code); 15080Sstevel@tonic-gate } else if (WIFSIGNALED(status)) { 15090Sstevel@tonic-gate int signal = WTERMSIG(status); 15100Sstevel@tonic-gate char sigstr[SIG2STR_MAX]; 15110Sstevel@tonic-gate 15120Sstevel@tonic-gate if (sig2str(signal, sigstr) == 0) { 15130Sstevel@tonic-gate zerror(gettext("'%s' terminated by signal SIG%s."), cmd, 15140Sstevel@tonic-gate sigstr); 15150Sstevel@tonic-gate } else { 15160Sstevel@tonic-gate zerror(gettext("'%s' terminated by an unknown signal."), 15170Sstevel@tonic-gate cmd); 15180Sstevel@tonic-gate } 15190Sstevel@tonic-gate } else { 15200Sstevel@tonic-gate zerror(gettext("'%s' failed for unknown reasons."), cmd); 15210Sstevel@tonic-gate } 15220Sstevel@tonic-gate return (Z_ERR); 15230Sstevel@tonic-gate } 15240Sstevel@tonic-gate 15250Sstevel@tonic-gate /* 15260Sstevel@tonic-gate * Various sanity checks; make sure: 15270Sstevel@tonic-gate * 1. We're in the global zone. 15280Sstevel@tonic-gate * 2. The calling user has sufficient privilege. 15290Sstevel@tonic-gate * 3. The target zone is neither the global zone nor anything starting with 15300Sstevel@tonic-gate * "SUNW". 15310Sstevel@tonic-gate * 4a. If we're looking for a 'not running' (i.e., configured or installed) 15320Sstevel@tonic-gate * zone, the name service knows about it. 15330Sstevel@tonic-gate * 4b. For some operations which expect a zone not to be running, that it is 15340Sstevel@tonic-gate * not already running (or ready). 15350Sstevel@tonic-gate */ 15360Sstevel@tonic-gate static int 15370Sstevel@tonic-gate sanity_check(char *zone, int cmd_num, boolean_t running, 15380Sstevel@tonic-gate boolean_t unsafe_when_running) 15390Sstevel@tonic-gate { 15400Sstevel@tonic-gate zone_entry_t *zent; 15410Sstevel@tonic-gate priv_set_t *privset; 15420Sstevel@tonic-gate zone_state_t state; 1543766Scarlsonj char kernzone[ZONENAME_MAX]; 1544766Scarlsonj FILE *fp; 15450Sstevel@tonic-gate 15460Sstevel@tonic-gate if (getzoneid() != GLOBAL_ZONEID) { 15470Sstevel@tonic-gate zerror(gettext("must be in the global zone to %s a zone."), 15480Sstevel@tonic-gate cmd_to_str(cmd_num)); 15490Sstevel@tonic-gate return (Z_ERR); 15500Sstevel@tonic-gate } 15510Sstevel@tonic-gate 15520Sstevel@tonic-gate if ((privset = priv_allocset()) == NULL) { 15530Sstevel@tonic-gate zerror(gettext("%s failed"), "priv_allocset"); 15540Sstevel@tonic-gate return (Z_ERR); 15550Sstevel@tonic-gate } 15560Sstevel@tonic-gate 15570Sstevel@tonic-gate if (getppriv(PRIV_EFFECTIVE, privset) != 0) { 15580Sstevel@tonic-gate zerror(gettext("%s failed"), "getppriv"); 15590Sstevel@tonic-gate priv_freeset(privset); 15600Sstevel@tonic-gate return (Z_ERR); 15610Sstevel@tonic-gate } 15620Sstevel@tonic-gate 15630Sstevel@tonic-gate if (priv_isfullset(privset) == B_FALSE) { 15640Sstevel@tonic-gate zerror(gettext("only a privileged user may %s a zone."), 15650Sstevel@tonic-gate cmd_to_str(cmd_num)); 15660Sstevel@tonic-gate priv_freeset(privset); 15670Sstevel@tonic-gate return (Z_ERR); 15680Sstevel@tonic-gate } 15690Sstevel@tonic-gate priv_freeset(privset); 15700Sstevel@tonic-gate 15710Sstevel@tonic-gate if (zone == NULL) { 15720Sstevel@tonic-gate zerror(gettext("no zone specified")); 15730Sstevel@tonic-gate return (Z_ERR); 15740Sstevel@tonic-gate } 15750Sstevel@tonic-gate 15760Sstevel@tonic-gate if (strcmp(zone, GLOBAL_ZONENAME) == 0) { 15770Sstevel@tonic-gate zerror(gettext("%s operation is invalid for the global zone."), 15780Sstevel@tonic-gate cmd_to_str(cmd_num)); 15790Sstevel@tonic-gate return (Z_ERR); 15800Sstevel@tonic-gate } 15810Sstevel@tonic-gate 15820Sstevel@tonic-gate if (strncmp(zone, "SUNW", 4) == 0) { 15830Sstevel@tonic-gate zerror(gettext("%s operation is invalid for zones starting " 15840Sstevel@tonic-gate "with SUNW."), cmd_to_str(cmd_num)); 15850Sstevel@tonic-gate return (Z_ERR); 15860Sstevel@tonic-gate } 15870Sstevel@tonic-gate 1588766Scarlsonj if (!zonecfg_in_alt_root()) { 1589766Scarlsonj zent = lookup_running_zone(zone); 1590766Scarlsonj } else if ((fp = zonecfg_open_scratch("", B_FALSE)) == NULL) { 1591766Scarlsonj zent = NULL; 1592766Scarlsonj } else { 1593766Scarlsonj if (zonecfg_find_scratch(fp, zone, zonecfg_get_root(), 1594766Scarlsonj kernzone, sizeof (kernzone)) == 0) 1595766Scarlsonj zent = lookup_running_zone(kernzone); 1596766Scarlsonj else 1597766Scarlsonj zent = NULL; 1598766Scarlsonj zonecfg_close_scratch(fp); 1599766Scarlsonj } 1600766Scarlsonj 16010Sstevel@tonic-gate /* 16020Sstevel@tonic-gate * Look up from the kernel for 'running' zones. 16030Sstevel@tonic-gate */ 16040Sstevel@tonic-gate if (running) { 16050Sstevel@tonic-gate if (zent == NULL) { 16060Sstevel@tonic-gate zerror(gettext("not running")); 16070Sstevel@tonic-gate return (Z_ERR); 16080Sstevel@tonic-gate } 16090Sstevel@tonic-gate } else { 16100Sstevel@tonic-gate int err; 16110Sstevel@tonic-gate 16120Sstevel@tonic-gate if (unsafe_when_running && zent != NULL) { 16130Sstevel@tonic-gate /* check whether the zone is ready or running */ 16140Sstevel@tonic-gate if ((err = zone_get_state(zent->zname, 16150Sstevel@tonic-gate &zent->zstate_num)) != Z_OK) { 16160Sstevel@tonic-gate errno = err; 16170Sstevel@tonic-gate zperror2(zent->zname, 16180Sstevel@tonic-gate gettext("could not get state")); 16190Sstevel@tonic-gate /* can't tell, so hedge */ 16200Sstevel@tonic-gate zent->zstate_str = "ready/running"; 16210Sstevel@tonic-gate } else { 16220Sstevel@tonic-gate zent->zstate_str = 16230Sstevel@tonic-gate zone_state_str(zent->zstate_num); 16240Sstevel@tonic-gate } 16250Sstevel@tonic-gate zerror(gettext("%s operation is invalid for %s zones."), 16260Sstevel@tonic-gate cmd_to_str(cmd_num), zent->zstate_str); 16270Sstevel@tonic-gate return (Z_ERR); 16280Sstevel@tonic-gate } 16290Sstevel@tonic-gate if ((err = zone_get_state(zone, &state)) != Z_OK) { 16300Sstevel@tonic-gate errno = err; 16310Sstevel@tonic-gate zperror2(zone, gettext("could not get state")); 16320Sstevel@tonic-gate return (Z_ERR); 16330Sstevel@tonic-gate } 16340Sstevel@tonic-gate switch (cmd_num) { 16350Sstevel@tonic-gate case CMD_UNINSTALL: 16360Sstevel@tonic-gate if (state == ZONE_STATE_CONFIGURED) { 16370Sstevel@tonic-gate zerror(gettext("is already in state '%s'."), 16380Sstevel@tonic-gate zone_state_str(ZONE_STATE_CONFIGURED)); 16390Sstevel@tonic-gate return (Z_ERR); 16400Sstevel@tonic-gate } 16410Sstevel@tonic-gate break; 1642*1507Sgjelinek case CMD_ATTACH: 16431300Sgjelinek case CMD_CLONE: 16440Sstevel@tonic-gate case CMD_INSTALL: 16450Sstevel@tonic-gate if (state == ZONE_STATE_INSTALLED) { 16460Sstevel@tonic-gate zerror(gettext("is already %s."), 16470Sstevel@tonic-gate zone_state_str(ZONE_STATE_INSTALLED)); 16480Sstevel@tonic-gate return (Z_ERR); 16490Sstevel@tonic-gate } else if (state == ZONE_STATE_INCOMPLETE) { 16500Sstevel@tonic-gate zerror(gettext("zone is %s; %s required."), 16510Sstevel@tonic-gate zone_state_str(ZONE_STATE_INCOMPLETE), 16520Sstevel@tonic-gate cmd_to_str(CMD_UNINSTALL)); 16530Sstevel@tonic-gate return (Z_ERR); 16540Sstevel@tonic-gate } 16550Sstevel@tonic-gate break; 1656*1507Sgjelinek case CMD_DETACH: 16571300Sgjelinek case CMD_MOVE: 16580Sstevel@tonic-gate case CMD_READY: 16590Sstevel@tonic-gate case CMD_BOOT: 1660766Scarlsonj case CMD_MOUNT: 16610Sstevel@tonic-gate if (state < ZONE_STATE_INSTALLED) { 16620Sstevel@tonic-gate zerror(gettext("must be %s before %s."), 16630Sstevel@tonic-gate zone_state_str(ZONE_STATE_INSTALLED), 16640Sstevel@tonic-gate cmd_to_str(cmd_num)); 16650Sstevel@tonic-gate return (Z_ERR); 16660Sstevel@tonic-gate } 16670Sstevel@tonic-gate break; 16680Sstevel@tonic-gate case CMD_VERIFY: 16690Sstevel@tonic-gate if (state == ZONE_STATE_INCOMPLETE) { 16700Sstevel@tonic-gate zerror(gettext("zone is %s; %s required."), 16710Sstevel@tonic-gate zone_state_str(ZONE_STATE_INCOMPLETE), 16720Sstevel@tonic-gate cmd_to_str(CMD_UNINSTALL)); 16730Sstevel@tonic-gate return (Z_ERR); 16740Sstevel@tonic-gate } 16750Sstevel@tonic-gate break; 1676766Scarlsonj case CMD_UNMOUNT: 1677766Scarlsonj if (state != ZONE_STATE_MOUNTED) { 1678766Scarlsonj zerror(gettext("must be %s before %s."), 1679766Scarlsonj zone_state_str(ZONE_STATE_MOUNTED), 1680766Scarlsonj cmd_to_str(cmd_num)); 1681766Scarlsonj return (Z_ERR); 1682766Scarlsonj } 1683766Scarlsonj break; 16840Sstevel@tonic-gate } 16850Sstevel@tonic-gate } 16860Sstevel@tonic-gate return (Z_OK); 16870Sstevel@tonic-gate } 16880Sstevel@tonic-gate 16890Sstevel@tonic-gate static int 16900Sstevel@tonic-gate halt_func(int argc, char *argv[]) 16910Sstevel@tonic-gate { 16920Sstevel@tonic-gate zone_cmd_arg_t zarg; 16930Sstevel@tonic-gate int arg; 16940Sstevel@tonic-gate 1695766Scarlsonj if (zonecfg_in_alt_root()) { 1696766Scarlsonj zerror(gettext("cannot halt zone in alternate root")); 1697766Scarlsonj return (Z_ERR); 1698766Scarlsonj } 1699766Scarlsonj 17000Sstevel@tonic-gate optind = 0; 17010Sstevel@tonic-gate if ((arg = getopt(argc, argv, "?")) != EOF) { 17020Sstevel@tonic-gate switch (arg) { 17030Sstevel@tonic-gate case '?': 17040Sstevel@tonic-gate sub_usage(SHELP_HALT, CMD_HALT); 17050Sstevel@tonic-gate return (optopt == '?' ? Z_OK : Z_USAGE); 17060Sstevel@tonic-gate default: 17070Sstevel@tonic-gate sub_usage(SHELP_HALT, CMD_HALT); 17080Sstevel@tonic-gate return (Z_USAGE); 17090Sstevel@tonic-gate } 17100Sstevel@tonic-gate } 17110Sstevel@tonic-gate if (argc > optind) { 17120Sstevel@tonic-gate sub_usage(SHELP_HALT, CMD_HALT); 17130Sstevel@tonic-gate return (Z_USAGE); 17140Sstevel@tonic-gate } 17150Sstevel@tonic-gate /* 17160Sstevel@tonic-gate * zoneadmd should be the one to decide whether or not to proceed, 17170Sstevel@tonic-gate * so even though it seems that the fourth parameter below should 17180Sstevel@tonic-gate * perhaps be B_TRUE, it really shouldn't be. 17190Sstevel@tonic-gate */ 17200Sstevel@tonic-gate if (sanity_check(target_zone, CMD_HALT, B_FALSE, B_FALSE) != Z_OK) 17210Sstevel@tonic-gate return (Z_ERR); 17220Sstevel@tonic-gate 17230Sstevel@tonic-gate zarg.cmd = Z_HALT; 17240Sstevel@tonic-gate return ((call_zoneadmd(target_zone, &zarg) == 0) ? Z_OK : Z_ERR); 17250Sstevel@tonic-gate } 17260Sstevel@tonic-gate 17270Sstevel@tonic-gate static int 17280Sstevel@tonic-gate reboot_func(int argc, char *argv[]) 17290Sstevel@tonic-gate { 17300Sstevel@tonic-gate zone_cmd_arg_t zarg; 17310Sstevel@tonic-gate int arg; 17320Sstevel@tonic-gate 1733766Scarlsonj if (zonecfg_in_alt_root()) { 1734766Scarlsonj zerror(gettext("cannot reboot zone in alternate root")); 1735766Scarlsonj return (Z_ERR); 1736766Scarlsonj } 1737766Scarlsonj 17380Sstevel@tonic-gate optind = 0; 17390Sstevel@tonic-gate if ((arg = getopt(argc, argv, "?")) != EOF) { 17400Sstevel@tonic-gate switch (arg) { 17410Sstevel@tonic-gate case '?': 17420Sstevel@tonic-gate sub_usage(SHELP_REBOOT, CMD_REBOOT); 17430Sstevel@tonic-gate return (optopt == '?' ? Z_OK : Z_USAGE); 17440Sstevel@tonic-gate default: 17450Sstevel@tonic-gate sub_usage(SHELP_REBOOT, CMD_REBOOT); 17460Sstevel@tonic-gate return (Z_USAGE); 17470Sstevel@tonic-gate } 17480Sstevel@tonic-gate } 17490Sstevel@tonic-gate if (argc > 0) { 17500Sstevel@tonic-gate sub_usage(SHELP_REBOOT, CMD_REBOOT); 17510Sstevel@tonic-gate return (Z_USAGE); 17520Sstevel@tonic-gate } 17530Sstevel@tonic-gate /* 17540Sstevel@tonic-gate * zoneadmd should be the one to decide whether or not to proceed, 17550Sstevel@tonic-gate * so even though it seems that the fourth parameter below should 17560Sstevel@tonic-gate * perhaps be B_TRUE, it really shouldn't be. 17570Sstevel@tonic-gate */ 17580Sstevel@tonic-gate if (sanity_check(target_zone, CMD_REBOOT, B_TRUE, B_FALSE) != Z_OK) 17590Sstevel@tonic-gate return (Z_ERR); 1760823Sgjelinek if (verify_details(CMD_REBOOT) != Z_OK) 1761823Sgjelinek return (Z_ERR); 17620Sstevel@tonic-gate 17630Sstevel@tonic-gate zarg.cmd = Z_REBOOT; 17640Sstevel@tonic-gate return ((call_zoneadmd(target_zone, &zarg) == 0) ? Z_OK : Z_ERR); 17650Sstevel@tonic-gate } 17660Sstevel@tonic-gate 17670Sstevel@tonic-gate static int 17680Sstevel@tonic-gate verify_rctls(zone_dochandle_t handle) 17690Sstevel@tonic-gate { 17700Sstevel@tonic-gate struct zone_rctltab rctltab; 17710Sstevel@tonic-gate size_t rbs = rctlblk_size(); 17720Sstevel@tonic-gate rctlblk_t *rctlblk; 17730Sstevel@tonic-gate int error = Z_INVAL; 17740Sstevel@tonic-gate 17750Sstevel@tonic-gate if ((rctlblk = malloc(rbs)) == NULL) { 17760Sstevel@tonic-gate zerror(gettext("failed to allocate %lu bytes: %s"), rbs, 17770Sstevel@tonic-gate strerror(errno)); 17780Sstevel@tonic-gate return (Z_NOMEM); 17790Sstevel@tonic-gate } 17800Sstevel@tonic-gate 17810Sstevel@tonic-gate if (zonecfg_setrctlent(handle) != Z_OK) { 17820Sstevel@tonic-gate zerror(gettext("zonecfg_setrctlent failed")); 17830Sstevel@tonic-gate free(rctlblk); 17840Sstevel@tonic-gate return (error); 17850Sstevel@tonic-gate } 17860Sstevel@tonic-gate 17870Sstevel@tonic-gate rctltab.zone_rctl_valptr = NULL; 17880Sstevel@tonic-gate while (zonecfg_getrctlent(handle, &rctltab) == Z_OK) { 17890Sstevel@tonic-gate struct zone_rctlvaltab *rctlval; 17900Sstevel@tonic-gate const char *name = rctltab.zone_rctl_name; 17910Sstevel@tonic-gate 17920Sstevel@tonic-gate if (!zonecfg_is_rctl(name)) { 17930Sstevel@tonic-gate zerror(gettext("WARNING: Ignoring unrecognized rctl " 17940Sstevel@tonic-gate "'%s'."), name); 17950Sstevel@tonic-gate zonecfg_free_rctl_value_list(rctltab.zone_rctl_valptr); 17960Sstevel@tonic-gate rctltab.zone_rctl_valptr = NULL; 17970Sstevel@tonic-gate continue; 17980Sstevel@tonic-gate } 17990Sstevel@tonic-gate 18000Sstevel@tonic-gate for (rctlval = rctltab.zone_rctl_valptr; rctlval != NULL; 18010Sstevel@tonic-gate rctlval = rctlval->zone_rctlval_next) { 18020Sstevel@tonic-gate if (zonecfg_construct_rctlblk(rctlval, rctlblk) 18030Sstevel@tonic-gate != Z_OK) { 18040Sstevel@tonic-gate zerror(gettext("invalid rctl value: " 18050Sstevel@tonic-gate "(priv=%s,limit=%s,action%s)"), 18060Sstevel@tonic-gate rctlval->zone_rctlval_priv, 18070Sstevel@tonic-gate rctlval->zone_rctlval_limit, 18080Sstevel@tonic-gate rctlval->zone_rctlval_action); 18090Sstevel@tonic-gate goto out; 18100Sstevel@tonic-gate } 18110Sstevel@tonic-gate if (!zonecfg_valid_rctl(name, rctlblk)) { 18120Sstevel@tonic-gate zerror(gettext("(priv=%s,limit=%s,action=%s) " 18130Sstevel@tonic-gate "is not a valid value for rctl '%s'"), 18140Sstevel@tonic-gate rctlval->zone_rctlval_priv, 18150Sstevel@tonic-gate rctlval->zone_rctlval_limit, 18160Sstevel@tonic-gate rctlval->zone_rctlval_action, 18170Sstevel@tonic-gate name); 18180Sstevel@tonic-gate goto out; 18190Sstevel@tonic-gate } 18200Sstevel@tonic-gate } 18210Sstevel@tonic-gate zonecfg_free_rctl_value_list(rctltab.zone_rctl_valptr); 18220Sstevel@tonic-gate } 18230Sstevel@tonic-gate rctltab.zone_rctl_valptr = NULL; 18240Sstevel@tonic-gate error = Z_OK; 18250Sstevel@tonic-gate out: 18260Sstevel@tonic-gate zonecfg_free_rctl_value_list(rctltab.zone_rctl_valptr); 18270Sstevel@tonic-gate (void) zonecfg_endrctlent(handle); 18280Sstevel@tonic-gate free(rctlblk); 18290Sstevel@tonic-gate return (error); 18300Sstevel@tonic-gate } 18310Sstevel@tonic-gate 18320Sstevel@tonic-gate static int 18330Sstevel@tonic-gate verify_pool(zone_dochandle_t handle) 18340Sstevel@tonic-gate { 18350Sstevel@tonic-gate char poolname[MAXPATHLEN]; 18360Sstevel@tonic-gate pool_conf_t *poolconf; 18370Sstevel@tonic-gate pool_t *pool; 18380Sstevel@tonic-gate int status; 18390Sstevel@tonic-gate int error; 18400Sstevel@tonic-gate 18410Sstevel@tonic-gate /* 18420Sstevel@tonic-gate * This ends up being very similar to the check done in zoneadmd. 18430Sstevel@tonic-gate */ 18440Sstevel@tonic-gate error = zonecfg_get_pool(handle, poolname, sizeof (poolname)); 18450Sstevel@tonic-gate if (error == Z_NO_ENTRY || (error == Z_OK && strlen(poolname) == 0)) { 18460Sstevel@tonic-gate /* 18470Sstevel@tonic-gate * No pool specified. 18480Sstevel@tonic-gate */ 18490Sstevel@tonic-gate return (0); 18500Sstevel@tonic-gate } 18510Sstevel@tonic-gate if (error != Z_OK) { 18520Sstevel@tonic-gate zperror(gettext("Unable to retrieve pool name from " 18530Sstevel@tonic-gate "configuration"), B_TRUE); 18540Sstevel@tonic-gate return (error); 18550Sstevel@tonic-gate } 18560Sstevel@tonic-gate /* 18570Sstevel@tonic-gate * Don't do anything if pools aren't enabled. 18580Sstevel@tonic-gate */ 18590Sstevel@tonic-gate if (pool_get_status(&status) != PO_SUCCESS || status != POOL_ENABLED) { 18600Sstevel@tonic-gate zerror(gettext("WARNING: pools facility not active; " 18610Sstevel@tonic-gate "zone will not be bound to pool '%s'."), poolname); 18620Sstevel@tonic-gate return (Z_OK); 18630Sstevel@tonic-gate } 18640Sstevel@tonic-gate /* 18650Sstevel@tonic-gate * Try to provide a sane error message if the requested pool doesn't 18660Sstevel@tonic-gate * exist. It isn't clear that pools-related failures should 18670Sstevel@tonic-gate * necessarily translate to a failure to verify the zone configuration, 18680Sstevel@tonic-gate * hence they are not considered errors. 18690Sstevel@tonic-gate */ 18700Sstevel@tonic-gate if ((poolconf = pool_conf_alloc()) == NULL) { 18710Sstevel@tonic-gate zerror(gettext("WARNING: pool_conf_alloc failed; " 18720Sstevel@tonic-gate "using default pool")); 18730Sstevel@tonic-gate return (Z_OK); 18740Sstevel@tonic-gate } 18750Sstevel@tonic-gate if (pool_conf_open(poolconf, pool_dynamic_location(), PO_RDONLY) != 18760Sstevel@tonic-gate PO_SUCCESS) { 18770Sstevel@tonic-gate zerror(gettext("WARNING: pool_conf_open failed; " 18780Sstevel@tonic-gate "using default pool")); 18790Sstevel@tonic-gate pool_conf_free(poolconf); 18800Sstevel@tonic-gate return (Z_OK); 18810Sstevel@tonic-gate } 18820Sstevel@tonic-gate pool = pool_get_pool(poolconf, poolname); 18830Sstevel@tonic-gate (void) pool_conf_close(poolconf); 18840Sstevel@tonic-gate pool_conf_free(poolconf); 18850Sstevel@tonic-gate if (pool == NULL) { 18860Sstevel@tonic-gate zerror(gettext("WARNING: pool '%s' not found. " 18870Sstevel@tonic-gate "using default pool"), poolname); 18880Sstevel@tonic-gate } 18890Sstevel@tonic-gate 18900Sstevel@tonic-gate return (Z_OK); 18910Sstevel@tonic-gate } 18920Sstevel@tonic-gate 18930Sstevel@tonic-gate static int 1894823Sgjelinek verify_ipd(zone_dochandle_t handle) 1895823Sgjelinek { 1896823Sgjelinek int return_code = Z_OK; 1897823Sgjelinek struct zone_fstab fstab; 1898823Sgjelinek struct stat st; 1899823Sgjelinek char specdir[MAXPATHLEN]; 1900823Sgjelinek 1901823Sgjelinek if (zonecfg_setipdent(handle) != Z_OK) { 1902924Sgjelinek /* 1903924Sgjelinek * TRANSLATION_NOTE 1904924Sgjelinek * inherit-pkg-dirs is a literal that should not be translated. 1905924Sgjelinek */ 1906924Sgjelinek (void) fprintf(stderr, gettext("could not verify " 1907823Sgjelinek "inherit-pkg-dirs: unable to enumerate mounts\n")); 1908823Sgjelinek return (Z_ERR); 1909823Sgjelinek } 1910823Sgjelinek while (zonecfg_getipdent(handle, &fstab) == Z_OK) { 1911823Sgjelinek /* 1912823Sgjelinek * Verify fs_dir exists. 1913823Sgjelinek */ 1914823Sgjelinek (void) snprintf(specdir, sizeof (specdir), "%s%s", 1915823Sgjelinek zonecfg_get_root(), fstab.zone_fs_dir); 1916823Sgjelinek if (stat(specdir, &st) != 0) { 1917924Sgjelinek /* 1918924Sgjelinek * TRANSLATION_NOTE 1919924Sgjelinek * inherit-pkg-dir is a literal that should not be 1920924Sgjelinek * translated. 1921924Sgjelinek */ 1922924Sgjelinek (void) fprintf(stderr, gettext("could not verify " 1923823Sgjelinek "inherit-pkg-dir %s: %s\n"), 1924823Sgjelinek fstab.zone_fs_dir, strerror(errno)); 1925823Sgjelinek return_code = Z_ERR; 1926823Sgjelinek } 1927823Sgjelinek if (strcmp(st.st_fstype, MNTTYPE_NFS) == 0) { 1928924Sgjelinek /* 1929924Sgjelinek * TRANSLATION_NOTE 1930924Sgjelinek * inherit-pkg-dir and NFS are literals that should 1931924Sgjelinek * not be translated. 1932924Sgjelinek */ 1933823Sgjelinek (void) fprintf(stderr, gettext("cannot verify " 1934924Sgjelinek "inherit-pkg-dir %s: NFS mounted file-system.\n" 1935924Sgjelinek "\tA local file-system must be used.\n"), 1936823Sgjelinek fstab.zone_fs_dir); 1937823Sgjelinek return_code = Z_ERR; 1938823Sgjelinek } 1939823Sgjelinek } 1940823Sgjelinek (void) zonecfg_endipdent(handle); 1941823Sgjelinek 1942823Sgjelinek return (return_code); 1943823Sgjelinek } 1944823Sgjelinek 19451393Slling /* ARGSUSED */ 19461393Slling static void 19471393Slling zfs_fs_err_handler(const char *fmt, va_list ap) 19481393Slling { 19491393Slling /* 19501393Slling * Do nothing - do not print the libzfs error messages. 19511393Slling */ 19521393Slling } 19531393Slling 19541393Slling /* 19551393Slling * Verify that the ZFS dataset exists, and its mountpoint 19561393Slling * property is set to "legacy". 19571393Slling */ 19581393Slling static int 19591393Slling verify_fs_zfs(struct zone_fstab *fstab) 19601393Slling { 19611393Slling zfs_handle_t *zhp; 19621393Slling char propbuf[ZFS_MAXPROPLEN]; 19631393Slling 19641393Slling zfs_set_error_handler(zfs_fs_err_handler); 19651393Slling 19661393Slling if ((zhp = zfs_open(fstab->zone_fs_special, ZFS_TYPE_ANY)) == NULL) { 19671397Slling (void) fprintf(stderr, gettext("could not verify fs %s: " 19681393Slling "could not access zfs dataset '%s'\n"), 19691393Slling fstab->zone_fs_dir, fstab->zone_fs_special); 19701393Slling return (Z_ERR); 19711393Slling } 19721393Slling 19731393Slling if (zfs_get_type(zhp) != ZFS_TYPE_FILESYSTEM) { 19741393Slling (void) fprintf(stderr, gettext("cannot verify fs %s: " 19751393Slling "'%s' is not a filesystem\n"), 19761393Slling fstab->zone_fs_dir, fstab->zone_fs_special); 19771393Slling zfs_close(zhp); 19781393Slling return (Z_ERR); 19791393Slling } 19801393Slling 19811393Slling if (zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, propbuf, sizeof (propbuf), 19821393Slling NULL, NULL, 0, 0) != 0 || strcmp(propbuf, "legacy") != 0) { 19831397Slling (void) fprintf(stderr, gettext("could not verify fs %s: " 19841397Slling "zfs '%s' mountpoint is not \"legacy\"\n"), 19851393Slling fstab->zone_fs_dir, fstab->zone_fs_special); 19861393Slling zfs_close(zhp); 19871393Slling return (Z_ERR); 19881393Slling } 19891393Slling 19901393Slling zfs_close(zhp); 19911393Slling return (Z_OK); 19921393Slling } 19931393Slling 19941393Slling /* 19951393Slling * Verify that the special device/filesystem exists and is valid. 19961393Slling */ 19971393Slling static int 19981393Slling verify_fs_special(struct zone_fstab *fstab) 19991393Slling { 20001393Slling struct stat st; 20011393Slling 20021393Slling if (strcmp(fstab->zone_fs_type, MNTTYPE_ZFS) == 0) 20031393Slling return (verify_fs_zfs(fstab)); 20041393Slling 20051393Slling if (stat(fstab->zone_fs_special, &st) != 0) { 20061397Slling (void) fprintf(stderr, gettext("could not verify fs " 20071393Slling "%s: could not access %s: %s\n"), fstab->zone_fs_dir, 20081393Slling fstab->zone_fs_special, strerror(errno)); 20091393Slling return (Z_ERR); 20101393Slling } 20111393Slling 20121393Slling if (strcmp(st.st_fstype, MNTTYPE_NFS) == 0) { 20131393Slling /* 20141393Slling * TRANSLATION_NOTE 20151393Slling * fs and NFS are literals that should 20161393Slling * not be translated. 20171393Slling */ 20181393Slling (void) fprintf(stderr, gettext("cannot verify " 20191393Slling "fs %s: NFS mounted file-system.\n" 20201393Slling "\tA local file-system must be used.\n"), 20211393Slling fstab->zone_fs_special); 20221393Slling return (Z_ERR); 20231393Slling } 20241393Slling 20251393Slling return (Z_OK); 20261393Slling } 20271393Slling 2028823Sgjelinek static int 20290Sstevel@tonic-gate verify_filesystems(zone_dochandle_t handle) 20300Sstevel@tonic-gate { 20310Sstevel@tonic-gate int return_code = Z_OK; 20320Sstevel@tonic-gate struct zone_fstab fstab; 20330Sstevel@tonic-gate char cmdbuf[MAXPATHLEN]; 20340Sstevel@tonic-gate struct stat st; 20350Sstevel@tonic-gate 20360Sstevel@tonic-gate /* 20370Sstevel@tonic-gate * No need to verify inherit-pkg-dir fs types, as their type is 20380Sstevel@tonic-gate * implicitly lofs, which is known. Therefore, the types are only 20390Sstevel@tonic-gate * verified for regular filesystems below. 20400Sstevel@tonic-gate * 20410Sstevel@tonic-gate * Since the actual mount point is not known until the dependent mounts 20420Sstevel@tonic-gate * are performed, we don't attempt any path validation here: that will 20430Sstevel@tonic-gate * happen later when zoneadmd actually does the mounts. 20440Sstevel@tonic-gate */ 20450Sstevel@tonic-gate if (zonecfg_setfsent(handle) != Z_OK) { 2046924Sgjelinek (void) fprintf(stderr, gettext("could not verify file-systems: " 20470Sstevel@tonic-gate "unable to enumerate mounts\n")); 20480Sstevel@tonic-gate return (Z_ERR); 20490Sstevel@tonic-gate } 20500Sstevel@tonic-gate while (zonecfg_getfsent(handle, &fstab) == Z_OK) { 20510Sstevel@tonic-gate if (!zonecfg_valid_fs_type(fstab.zone_fs_type)) { 20520Sstevel@tonic-gate (void) fprintf(stderr, gettext("cannot verify fs %s: " 20530Sstevel@tonic-gate "type %s is not allowed.\n"), fstab.zone_fs_dir, 20540Sstevel@tonic-gate fstab.zone_fs_type); 20550Sstevel@tonic-gate return_code = Z_ERR; 20560Sstevel@tonic-gate goto next_fs; 20570Sstevel@tonic-gate } 20580Sstevel@tonic-gate /* 20590Sstevel@tonic-gate * Verify /usr/lib/fs/<fstype>/mount exists. 20600Sstevel@tonic-gate */ 20610Sstevel@tonic-gate if (snprintf(cmdbuf, sizeof (cmdbuf), "/usr/lib/fs/%s/mount", 20620Sstevel@tonic-gate fstab.zone_fs_type) > sizeof (cmdbuf)) { 20630Sstevel@tonic-gate (void) fprintf(stderr, gettext("cannot verify fs %s: " 20640Sstevel@tonic-gate "type %s is too long.\n"), fstab.zone_fs_dir, 20650Sstevel@tonic-gate fstab.zone_fs_type); 20660Sstevel@tonic-gate return_code = Z_ERR; 20670Sstevel@tonic-gate goto next_fs; 20680Sstevel@tonic-gate } 20690Sstevel@tonic-gate if (stat(cmdbuf, &st) != 0) { 2070924Sgjelinek (void) fprintf(stderr, gettext("could not verify fs " 2071924Sgjelinek "%s: could not access %s: %s\n"), fstab.zone_fs_dir, 20720Sstevel@tonic-gate cmdbuf, strerror(errno)); 20730Sstevel@tonic-gate return_code = Z_ERR; 20740Sstevel@tonic-gate goto next_fs; 20750Sstevel@tonic-gate } 20760Sstevel@tonic-gate if (!S_ISREG(st.st_mode)) { 2077924Sgjelinek (void) fprintf(stderr, gettext("could not verify fs " 2078924Sgjelinek "%s: %s is not a regular file\n"), 2079924Sgjelinek fstab.zone_fs_dir, cmdbuf); 20800Sstevel@tonic-gate return_code = Z_ERR; 20810Sstevel@tonic-gate goto next_fs; 20820Sstevel@tonic-gate } 20830Sstevel@tonic-gate /* 20840Sstevel@tonic-gate * Verify /usr/lib/fs/<fstype>/fsck exists iff zone_fs_raw is 20850Sstevel@tonic-gate * set. 20860Sstevel@tonic-gate */ 20870Sstevel@tonic-gate if (snprintf(cmdbuf, sizeof (cmdbuf), "/usr/lib/fs/%s/fsck", 20880Sstevel@tonic-gate fstab.zone_fs_type) > sizeof (cmdbuf)) { 20890Sstevel@tonic-gate (void) fprintf(stderr, gettext("cannot verify fs %s: " 20900Sstevel@tonic-gate "type %s is too long.\n"), fstab.zone_fs_dir, 20910Sstevel@tonic-gate fstab.zone_fs_type); 20920Sstevel@tonic-gate return_code = Z_ERR; 20930Sstevel@tonic-gate goto next_fs; 20940Sstevel@tonic-gate } 20950Sstevel@tonic-gate if (fstab.zone_fs_raw[0] == '\0' && stat(cmdbuf, &st) == 0) { 2096924Sgjelinek (void) fprintf(stderr, gettext("could not verify fs " 2097924Sgjelinek "%s: must specify 'raw' device for %s " 2098924Sgjelinek "file-systems\n"), 20990Sstevel@tonic-gate fstab.zone_fs_dir, fstab.zone_fs_type); 21000Sstevel@tonic-gate return_code = Z_ERR; 21010Sstevel@tonic-gate goto next_fs; 21020Sstevel@tonic-gate } 21030Sstevel@tonic-gate if (fstab.zone_fs_raw[0] != '\0' && 21040Sstevel@tonic-gate (stat(cmdbuf, &st) != 0 || !S_ISREG(st.st_mode))) { 21050Sstevel@tonic-gate (void) fprintf(stderr, gettext("cannot verify fs %s: " 21060Sstevel@tonic-gate "'raw' device specified but " 21070Sstevel@tonic-gate "no fsck executable exists for %s\n"), 21080Sstevel@tonic-gate fstab.zone_fs_dir, fstab.zone_fs_type); 21090Sstevel@tonic-gate return_code = Z_ERR; 21100Sstevel@tonic-gate goto next_fs; 21110Sstevel@tonic-gate } 21121393Slling 21131393Slling /* Verify fs_special. */ 21141393Slling if ((return_code = verify_fs_special(&fstab)) != Z_OK) 2115823Sgjelinek goto next_fs; 21161393Slling 21171393Slling /* Verify fs_raw. */ 2118823Sgjelinek if (fstab.zone_fs_raw[0] != '\0' && 2119823Sgjelinek stat(fstab.zone_fs_raw, &st) != 0) { 2120924Sgjelinek /* 2121924Sgjelinek * TRANSLATION_NOTE 2122924Sgjelinek * fs is a literal that should not be translated. 2123924Sgjelinek */ 2124924Sgjelinek (void) fprintf(stderr, gettext("could not verify fs " 2125924Sgjelinek "%s: could not access %s: %s\n"), fstab.zone_fs_dir, 2126823Sgjelinek fstab.zone_fs_raw, strerror(errno)); 2127823Sgjelinek return_code = Z_ERR; 2128823Sgjelinek goto next_fs; 2129823Sgjelinek } 21300Sstevel@tonic-gate next_fs: 21310Sstevel@tonic-gate zonecfg_free_fs_option_list(fstab.zone_fs_options); 21320Sstevel@tonic-gate } 21330Sstevel@tonic-gate (void) zonecfg_endfsent(handle); 21340Sstevel@tonic-gate 21350Sstevel@tonic-gate return (return_code); 21360Sstevel@tonic-gate } 21370Sstevel@tonic-gate 2138789Sahrens const char *current_dataset; 2139789Sahrens 2140789Sahrens /* 2141789Sahrens * Custom error handler for errors incurred as part of the checks below. We 2142789Sahrens * want to trim off the leading 'cannot open ...' to create a better error 2143789Sahrens * message. The only other way this can fail is if we fail to set the 'zoned' 2144789Sahrens * property. In this case we just pass the error on verbatim. 2145789Sahrens */ 2146789Sahrens static void 2147789Sahrens zfs_error_handler(const char *fmt, va_list ap) 2148789Sahrens { 2149789Sahrens char buf[1024]; 2150789Sahrens 2151789Sahrens (void) vsnprintf(buf, sizeof (buf), fmt, ap); 2152789Sahrens 2153789Sahrens if (strncmp(gettext("cannot open "), buf, 2154789Sahrens strlen(gettext("cannot open "))) == 0) 2155924Sgjelinek /* 2156924Sgjelinek * TRANSLATION_NOTE 2157924Sgjelinek * zfs and dataset are literals that should not be translated. 2158924Sgjelinek */ 2159924Sgjelinek (void) fprintf(stderr, gettext("could not verify zfs " 2160789Sahrens "dataset %s%s\n"), current_dataset, strchr(buf, ':')); 2161789Sahrens else 2162924Sgjelinek (void) fprintf(stderr, gettext("could not verify zfs dataset " 2163789Sahrens "%s: %s\n"), current_dataset, buf); 2164789Sahrens } 2165789Sahrens 2166789Sahrens /* ARGSUSED */ 2167789Sahrens static int 2168789Sahrens check_zvol(zfs_handle_t *zhp, void *unused) 2169789Sahrens { 2170789Sahrens int ret; 2171789Sahrens 2172789Sahrens if (zfs_get_type(zhp) == ZFS_TYPE_VOLUME) { 2173924Sgjelinek /* 2174924Sgjelinek * TRANSLATION_NOTE 2175924Sgjelinek * zfs and dataset are literals that should not be translated. 2176924Sgjelinek */ 2177789Sahrens (void) fprintf(stderr, gettext("cannot verify zfs dataset %s: " 2178789Sahrens "volumes cannot be specified as a zone dataset resource\n"), 2179789Sahrens zfs_get_name(zhp)); 2180789Sahrens ret = -1; 2181789Sahrens } else { 2182789Sahrens ret = zfs_iter_children(zhp, check_zvol, NULL); 2183789Sahrens } 2184789Sahrens 2185789Sahrens zfs_close(zhp); 2186789Sahrens 2187789Sahrens return (ret); 2188789Sahrens } 2189789Sahrens 2190789Sahrens /* 2191789Sahrens * Validate that the given dataset exists on the system, and that neither it nor 2192789Sahrens * its children are zvols. 2193789Sahrens * 2194789Sahrens * Note that we don't do anything with the 'zoned' property here. All 2195789Sahrens * management is done in zoneadmd when the zone is actually rebooted. This 2196789Sahrens * allows us to automatically set the zoned property even when a zone is 2197789Sahrens * rebooted by the administrator. 2198789Sahrens */ 2199789Sahrens static int 2200789Sahrens verify_datasets(zone_dochandle_t handle) 2201789Sahrens { 2202789Sahrens int return_code = Z_OK; 2203789Sahrens struct zone_dstab dstab; 2204789Sahrens zfs_handle_t *zhp; 2205789Sahrens char propbuf[ZFS_MAXPROPLEN]; 2206789Sahrens char source[ZFS_MAXNAMELEN]; 2207789Sahrens zfs_source_t srctype; 2208789Sahrens 2209789Sahrens if (zonecfg_setdsent(handle) != Z_OK) { 2210924Sgjelinek /* 2211924Sgjelinek * TRANSLATION_NOTE 2212924Sgjelinek * zfs and dataset are literals that should not be translated. 2213924Sgjelinek */ 2214924Sgjelinek (void) fprintf(stderr, gettext("could not verify zfs datasets: " 2215789Sahrens "unable to enumerate datasets\n")); 2216789Sahrens return (Z_ERR); 2217789Sahrens } 2218789Sahrens 2219789Sahrens zfs_set_error_handler(zfs_error_handler); 2220789Sahrens 2221789Sahrens while (zonecfg_getdsent(handle, &dstab) == Z_OK) { 2222789Sahrens 2223789Sahrens current_dataset = dstab.zone_dataset_name; 2224789Sahrens 2225789Sahrens if ((zhp = zfs_open(dstab.zone_dataset_name, 2226789Sahrens ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME)) == NULL) { 2227789Sahrens return_code = Z_ERR; 2228789Sahrens continue; 2229789Sahrens } 2230789Sahrens 2231789Sahrens if (zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, propbuf, 2232789Sahrens sizeof (propbuf), &srctype, source, 2233789Sahrens sizeof (source), 0) == 0 && 2234789Sahrens (srctype == ZFS_SRC_INHERITED)) { 2235924Sgjelinek (void) fprintf(stderr, gettext("could not verify zfs " 2236789Sahrens "dataset %s: mountpoint cannot be inherited\n"), 2237789Sahrens dstab.zone_dataset_name); 2238789Sahrens return_code = Z_ERR; 2239789Sahrens zfs_close(zhp); 2240789Sahrens continue; 2241789Sahrens } 2242789Sahrens 2243789Sahrens if (zfs_get_type(zhp) == ZFS_TYPE_VOLUME) { 2244789Sahrens (void) fprintf(stderr, gettext("cannot verify zfs " 2245789Sahrens "dataset %s: volumes cannot be specified as a " 2246789Sahrens "zone dataset resource\n"), 2247789Sahrens dstab.zone_dataset_name); 2248789Sahrens return_code = Z_ERR; 2249789Sahrens } 2250789Sahrens 2251789Sahrens if (zfs_iter_children(zhp, check_zvol, NULL) != 0) 2252789Sahrens return_code = Z_ERR; 2253789Sahrens 2254789Sahrens zfs_close(zhp); 2255789Sahrens } 2256789Sahrens (void) zonecfg_enddsent(handle); 2257789Sahrens 2258789Sahrens return (return_code); 2259789Sahrens } 2260789Sahrens 22610Sstevel@tonic-gate static int 22620Sstevel@tonic-gate verify_details(int cmd_num) 22630Sstevel@tonic-gate { 22640Sstevel@tonic-gate zone_dochandle_t handle; 22650Sstevel@tonic-gate struct zone_nwiftab nwiftab; 22660Sstevel@tonic-gate char zonepath[MAXPATHLEN], checkpath[MAXPATHLEN]; 22670Sstevel@tonic-gate int return_code = Z_OK; 22680Sstevel@tonic-gate int err; 2269766Scarlsonj boolean_t in_alt_root; 22700Sstevel@tonic-gate 22710Sstevel@tonic-gate if ((handle = zonecfg_init_handle()) == NULL) { 22720Sstevel@tonic-gate zperror(cmd_to_str(cmd_num), B_TRUE); 22730Sstevel@tonic-gate return (Z_ERR); 22740Sstevel@tonic-gate } 22750Sstevel@tonic-gate if ((err = zonecfg_get_handle(target_zone, handle)) != Z_OK) { 22760Sstevel@tonic-gate errno = err; 22770Sstevel@tonic-gate zperror(cmd_to_str(cmd_num), B_TRUE); 22780Sstevel@tonic-gate zonecfg_fini_handle(handle); 22790Sstevel@tonic-gate return (Z_ERR); 22800Sstevel@tonic-gate } 22810Sstevel@tonic-gate if ((err = zonecfg_get_zonepath(handle, zonepath, sizeof (zonepath))) != 22820Sstevel@tonic-gate Z_OK) { 22830Sstevel@tonic-gate errno = err; 22840Sstevel@tonic-gate zperror(cmd_to_str(cmd_num), B_TRUE); 22850Sstevel@tonic-gate zonecfg_fini_handle(handle); 22860Sstevel@tonic-gate return (Z_ERR); 22870Sstevel@tonic-gate } 22880Sstevel@tonic-gate /* 22890Sstevel@tonic-gate * zonecfg_get_zonepath() gets its data from the XML repository. 22900Sstevel@tonic-gate * Verify this against the index file, which is checked first by 22910Sstevel@tonic-gate * zone_get_zonepath(). If they don't match, bail out. 22920Sstevel@tonic-gate */ 22930Sstevel@tonic-gate if ((err = zone_get_zonepath(target_zone, checkpath, 22940Sstevel@tonic-gate sizeof (checkpath))) != Z_OK) { 22950Sstevel@tonic-gate errno = err; 22960Sstevel@tonic-gate zperror2(target_zone, gettext("could not get zone path")); 22970Sstevel@tonic-gate return (Z_ERR); 22980Sstevel@tonic-gate } 22990Sstevel@tonic-gate if (strcmp(zonepath, checkpath) != 0) { 2300924Sgjelinek /* 2301924Sgjelinek * TRANSLATION_NOTE 2302924Sgjelinek * XML and zonepath are literals that should not be translated. 2303924Sgjelinek */ 23040Sstevel@tonic-gate (void) fprintf(stderr, gettext("The XML repository has " 23050Sstevel@tonic-gate "zonepath '%s',\nbut the index file has zonepath '%s'.\n" 23060Sstevel@tonic-gate "These must match, so fix the incorrect entry.\n"), 23070Sstevel@tonic-gate zonepath, checkpath); 23080Sstevel@tonic-gate return (Z_ERR); 23090Sstevel@tonic-gate } 23100Sstevel@tonic-gate if (validate_zonepath(zonepath, cmd_num) != Z_OK) { 23110Sstevel@tonic-gate (void) fprintf(stderr, gettext("could not verify zonepath %s " 23120Sstevel@tonic-gate "because of the above errors.\n"), zonepath); 23130Sstevel@tonic-gate return_code = Z_ERR; 23140Sstevel@tonic-gate } 23150Sstevel@tonic-gate 2316766Scarlsonj in_alt_root = zonecfg_in_alt_root(); 2317766Scarlsonj if (in_alt_root) 2318766Scarlsonj goto no_net; 2319766Scarlsonj 23200Sstevel@tonic-gate if ((err = zonecfg_setnwifent(handle)) != Z_OK) { 23210Sstevel@tonic-gate errno = err; 23220Sstevel@tonic-gate zperror(cmd_to_str(cmd_num), B_TRUE); 23230Sstevel@tonic-gate zonecfg_fini_handle(handle); 23240Sstevel@tonic-gate return (Z_ERR); 23250Sstevel@tonic-gate } 23260Sstevel@tonic-gate while (zonecfg_getnwifent(handle, &nwiftab) == Z_OK) { 23270Sstevel@tonic-gate struct lifreq lifr; 23280Sstevel@tonic-gate sa_family_t af; 23290Sstevel@tonic-gate int so, res; 23300Sstevel@tonic-gate 23310Sstevel@tonic-gate /* skip any loopback interfaces */ 23320Sstevel@tonic-gate if (strcmp(nwiftab.zone_nwif_physical, "lo0") == 0) 23330Sstevel@tonic-gate continue; 23340Sstevel@tonic-gate if ((res = zonecfg_valid_net_address(nwiftab.zone_nwif_address, 23350Sstevel@tonic-gate &lifr)) != Z_OK) { 23360Sstevel@tonic-gate (void) fprintf(stderr, gettext("could not verify %s " 23370Sstevel@tonic-gate "%s=%s %s=%s: %s\n"), "net", "address", 23380Sstevel@tonic-gate nwiftab.zone_nwif_address, "physical", 23390Sstevel@tonic-gate nwiftab.zone_nwif_physical, zonecfg_strerror(res)); 23400Sstevel@tonic-gate return_code = Z_ERR; 23410Sstevel@tonic-gate continue; 23420Sstevel@tonic-gate } 23430Sstevel@tonic-gate af = lifr.lifr_addr.ss_family; 23440Sstevel@tonic-gate (void) memset(&lifr, 0, sizeof (lifr)); 23450Sstevel@tonic-gate (void) strlcpy(lifr.lifr_name, nwiftab.zone_nwif_physical, 23460Sstevel@tonic-gate sizeof (lifr.lifr_name)); 23470Sstevel@tonic-gate lifr.lifr_addr.ss_family = af; 23480Sstevel@tonic-gate if ((so = socket(af, SOCK_DGRAM, 0)) < 0) { 23490Sstevel@tonic-gate (void) fprintf(stderr, gettext("could not verify %s " 23500Sstevel@tonic-gate "%s=%s %s=%s: could not get socket: %s\n"), "net", 23510Sstevel@tonic-gate "address", nwiftab.zone_nwif_address, "physical", 23520Sstevel@tonic-gate nwiftab.zone_nwif_physical, strerror(errno)); 23530Sstevel@tonic-gate return_code = Z_ERR; 23540Sstevel@tonic-gate continue; 23550Sstevel@tonic-gate } 23560Sstevel@tonic-gate if (ioctl(so, SIOCGLIFFLAGS, (caddr_t)&lifr) < 0) { 23570Sstevel@tonic-gate (void) fprintf(stderr, 23580Sstevel@tonic-gate gettext("could not verify %s %s=%s %s=%s: %s\n"), 23590Sstevel@tonic-gate "net", "address", nwiftab.zone_nwif_address, 23600Sstevel@tonic-gate "physical", nwiftab.zone_nwif_physical, 23610Sstevel@tonic-gate strerror(errno)); 23620Sstevel@tonic-gate return_code = Z_ERR; 23630Sstevel@tonic-gate } 23640Sstevel@tonic-gate (void) close(so); 23650Sstevel@tonic-gate } 23660Sstevel@tonic-gate (void) zonecfg_endnwifent(handle); 2367766Scarlsonj no_net: 23680Sstevel@tonic-gate 23690Sstevel@tonic-gate if (verify_filesystems(handle) != Z_OK) 23700Sstevel@tonic-gate return_code = Z_ERR; 2371823Sgjelinek if (verify_ipd(handle) != Z_OK) 2372823Sgjelinek return_code = Z_ERR; 2373766Scarlsonj if (!in_alt_root && verify_rctls(handle) != Z_OK) 23740Sstevel@tonic-gate return_code = Z_ERR; 2375766Scarlsonj if (!in_alt_root && verify_pool(handle) != Z_OK) 23760Sstevel@tonic-gate return_code = Z_ERR; 2377789Sahrens if (!in_alt_root && verify_datasets(handle) != Z_OK) 2378789Sahrens return_code = Z_ERR; 23790Sstevel@tonic-gate zonecfg_fini_handle(handle); 23800Sstevel@tonic-gate if (return_code == Z_ERR) 23810Sstevel@tonic-gate (void) fprintf(stderr, 23820Sstevel@tonic-gate gettext("%s: zone %s failed to verify\n"), 23830Sstevel@tonic-gate execname, target_zone); 23840Sstevel@tonic-gate return (return_code); 23850Sstevel@tonic-gate } 23860Sstevel@tonic-gate 23870Sstevel@tonic-gate static int 23880Sstevel@tonic-gate verify_func(int argc, char *argv[]) 23890Sstevel@tonic-gate { 23900Sstevel@tonic-gate int arg; 23910Sstevel@tonic-gate 23920Sstevel@tonic-gate optind = 0; 23930Sstevel@tonic-gate if ((arg = getopt(argc, argv, "?")) != EOF) { 23940Sstevel@tonic-gate switch (arg) { 23950Sstevel@tonic-gate case '?': 23960Sstevel@tonic-gate sub_usage(SHELP_VERIFY, CMD_VERIFY); 23970Sstevel@tonic-gate return (optopt == '?' ? Z_OK : Z_USAGE); 23980Sstevel@tonic-gate default: 23990Sstevel@tonic-gate sub_usage(SHELP_VERIFY, CMD_VERIFY); 24000Sstevel@tonic-gate return (Z_USAGE); 24010Sstevel@tonic-gate } 24020Sstevel@tonic-gate } 24030Sstevel@tonic-gate if (argc > optind) { 24040Sstevel@tonic-gate sub_usage(SHELP_VERIFY, CMD_VERIFY); 24050Sstevel@tonic-gate return (Z_USAGE); 24060Sstevel@tonic-gate } 24070Sstevel@tonic-gate if (sanity_check(target_zone, CMD_VERIFY, B_FALSE, B_FALSE) != Z_OK) 24080Sstevel@tonic-gate return (Z_ERR); 24090Sstevel@tonic-gate return (verify_details(CMD_VERIFY)); 24100Sstevel@tonic-gate } 24110Sstevel@tonic-gate 24120Sstevel@tonic-gate #define LUCREATEZONE "/usr/lib/lu/lucreatezone" 24130Sstevel@tonic-gate 24140Sstevel@tonic-gate static int 24150Sstevel@tonic-gate install_func(int argc, char *argv[]) 24160Sstevel@tonic-gate { 24170Sstevel@tonic-gate /* 9: "exec " and " -z " */ 24180Sstevel@tonic-gate char cmdbuf[sizeof (LUCREATEZONE) + ZONENAME_MAX + 9]; 24190Sstevel@tonic-gate int lockfd; 24200Sstevel@tonic-gate int err, arg; 24210Sstevel@tonic-gate char zonepath[MAXPATHLEN]; 24220Sstevel@tonic-gate int status; 24230Sstevel@tonic-gate 2424766Scarlsonj if (zonecfg_in_alt_root()) { 2425766Scarlsonj zerror(gettext("cannot install zone in alternate root")); 2426766Scarlsonj return (Z_ERR); 2427766Scarlsonj } 2428766Scarlsonj 24290Sstevel@tonic-gate optind = 0; 24300Sstevel@tonic-gate if ((arg = getopt(argc, argv, "?")) != EOF) { 24310Sstevel@tonic-gate switch (arg) { 24320Sstevel@tonic-gate case '?': 24330Sstevel@tonic-gate sub_usage(SHELP_INSTALL, CMD_INSTALL); 24340Sstevel@tonic-gate return (optopt == '?' ? Z_OK : Z_USAGE); 24350Sstevel@tonic-gate default: 24360Sstevel@tonic-gate sub_usage(SHELP_INSTALL, CMD_INSTALL); 24370Sstevel@tonic-gate return (Z_USAGE); 24380Sstevel@tonic-gate } 24390Sstevel@tonic-gate } 24400Sstevel@tonic-gate if (argc > optind) { 24410Sstevel@tonic-gate sub_usage(SHELP_INSTALL, CMD_INSTALL); 24420Sstevel@tonic-gate return (Z_USAGE); 24430Sstevel@tonic-gate } 24440Sstevel@tonic-gate if (sanity_check(target_zone, CMD_INSTALL, B_FALSE, B_TRUE) != Z_OK) 24450Sstevel@tonic-gate return (Z_ERR); 24460Sstevel@tonic-gate if (verify_details(CMD_INSTALL) != Z_OK) 24470Sstevel@tonic-gate return (Z_ERR); 24480Sstevel@tonic-gate 24490Sstevel@tonic-gate if (grab_lock_file(target_zone, &lockfd) != Z_OK) { 24500Sstevel@tonic-gate zerror(gettext("another %s may have an operation in progress."), 24511300Sgjelinek "zoneadm"); 24520Sstevel@tonic-gate return (Z_ERR); 24530Sstevel@tonic-gate } 24540Sstevel@tonic-gate err = zone_set_state(target_zone, ZONE_STATE_INCOMPLETE); 24550Sstevel@tonic-gate if (err != Z_OK) { 24560Sstevel@tonic-gate errno = err; 24570Sstevel@tonic-gate zperror2(target_zone, gettext("could not set state")); 24580Sstevel@tonic-gate goto done; 24590Sstevel@tonic-gate } 24600Sstevel@tonic-gate 24610Sstevel@tonic-gate /* 24620Sstevel@tonic-gate * According to the Application Packaging Developer's Guide, a 24630Sstevel@tonic-gate * "checkinstall" script when included in a package is executed as 24640Sstevel@tonic-gate * the user "install", if such a user exists, or by the user 24650Sstevel@tonic-gate * "nobody". In order to support this dubious behavior, the path 24660Sstevel@tonic-gate * to the zone being constructed is opened up during the life of 24670Sstevel@tonic-gate * the command laying down the zone's root file system. Once this 24680Sstevel@tonic-gate * has completed, regardless of whether it was successful, the 24690Sstevel@tonic-gate * path to the zone is again restricted. 24700Sstevel@tonic-gate */ 24710Sstevel@tonic-gate if ((err = zone_get_zonepath(target_zone, zonepath, 24720Sstevel@tonic-gate sizeof (zonepath))) != Z_OK) { 24730Sstevel@tonic-gate errno = err; 24740Sstevel@tonic-gate zperror2(target_zone, gettext("could not get zone path")); 24750Sstevel@tonic-gate goto done; 24760Sstevel@tonic-gate } 24770Sstevel@tonic-gate if (chmod(zonepath, DEFAULT_DIR_MODE) != 0) { 24780Sstevel@tonic-gate zperror(zonepath, B_FALSE); 24790Sstevel@tonic-gate err = Z_ERR; 24800Sstevel@tonic-gate goto done; 24810Sstevel@tonic-gate } 24820Sstevel@tonic-gate 24830Sstevel@tonic-gate /* 24840Sstevel@tonic-gate * "exec" the command so that the returned status is that of 24850Sstevel@tonic-gate * LUCREATEZONE and not the shell. 24860Sstevel@tonic-gate */ 24870Sstevel@tonic-gate (void) snprintf(cmdbuf, sizeof (cmdbuf), "exec " LUCREATEZONE " -z %s", 24880Sstevel@tonic-gate target_zone); 24890Sstevel@tonic-gate status = do_subproc(cmdbuf); 24900Sstevel@tonic-gate if (chmod(zonepath, S_IRWXU) != 0) { 24910Sstevel@tonic-gate zperror(zonepath, B_FALSE); 24920Sstevel@tonic-gate err = Z_ERR; 24930Sstevel@tonic-gate goto done; 24940Sstevel@tonic-gate } 24950Sstevel@tonic-gate if ((err = subproc_status(LUCREATEZONE, status)) != Z_OK) 24960Sstevel@tonic-gate goto done; 24970Sstevel@tonic-gate 24980Sstevel@tonic-gate if ((err = zone_set_state(target_zone, ZONE_STATE_INSTALLED)) != Z_OK) { 24990Sstevel@tonic-gate errno = err; 25000Sstevel@tonic-gate zperror2(target_zone, gettext("could not set state")); 25010Sstevel@tonic-gate goto done; 25020Sstevel@tonic-gate } 25030Sstevel@tonic-gate 25040Sstevel@tonic-gate done: 25050Sstevel@tonic-gate release_lock_file(lockfd); 25060Sstevel@tonic-gate return ((err == Z_OK) ? Z_OK : Z_ERR); 25070Sstevel@tonic-gate } 25080Sstevel@tonic-gate 25090Sstevel@tonic-gate /* 25101300Sgjelinek * Check that the inherited pkg dirs are the same for the clone and its source. 25111300Sgjelinek * The easiest way to do that is check that the list of ipds is the same 25121300Sgjelinek * by matching each one against the other. This algorithm should be fine since 25131300Sgjelinek * the list of ipds should not be that long. 25141300Sgjelinek */ 25151300Sgjelinek static int 25161300Sgjelinek valid_ipd_clone(zone_dochandle_t s_handle, char *source_zone, 25171300Sgjelinek zone_dochandle_t t_handle, char *target_zone) 25181300Sgjelinek { 25191300Sgjelinek int err; 25201300Sgjelinek int res = Z_OK; 25211300Sgjelinek int s_cnt = 0; 25221300Sgjelinek int t_cnt = 0; 25231300Sgjelinek struct zone_fstab s_fstab; 25241300Sgjelinek struct zone_fstab t_fstab; 25251300Sgjelinek 25261300Sgjelinek /* 25271300Sgjelinek * First check the source of the clone against the target. 25281300Sgjelinek */ 25291300Sgjelinek if ((err = zonecfg_setipdent(s_handle)) != Z_OK) { 25301300Sgjelinek errno = err; 25311300Sgjelinek zperror2(source_zone, gettext("could not enumerate " 25321300Sgjelinek "inherit-pkg-dirs")); 25331300Sgjelinek return (Z_ERR); 25341300Sgjelinek } 25351300Sgjelinek 25361300Sgjelinek while (zonecfg_getipdent(s_handle, &s_fstab) == Z_OK) { 25371300Sgjelinek boolean_t match = B_FALSE; 25381300Sgjelinek 25391300Sgjelinek s_cnt++; 25401300Sgjelinek 25411300Sgjelinek if ((err = zonecfg_setipdent(t_handle)) != Z_OK) { 25421300Sgjelinek errno = err; 25431300Sgjelinek zperror2(target_zone, gettext("could not enumerate " 25441300Sgjelinek "inherit-pkg-dirs")); 25451300Sgjelinek (void) zonecfg_endipdent(s_handle); 25461300Sgjelinek return (Z_ERR); 25471300Sgjelinek } 25481300Sgjelinek 25491300Sgjelinek while (zonecfg_getipdent(t_handle, &t_fstab) == Z_OK) { 25501300Sgjelinek if (strcmp(s_fstab.zone_fs_dir, t_fstab.zone_fs_dir) 25511300Sgjelinek == 0) { 25521300Sgjelinek match = B_TRUE; 25531300Sgjelinek break; 25541300Sgjelinek } 25551300Sgjelinek } 25561300Sgjelinek (void) zonecfg_endipdent(t_handle); 25571300Sgjelinek 25581300Sgjelinek if (!match) { 25591300Sgjelinek (void) fprintf(stderr, gettext("inherit-pkg-dir " 25601300Sgjelinek "'%s' is not configured in zone %s.\n"), 25611300Sgjelinek s_fstab.zone_fs_dir, target_zone); 25621300Sgjelinek res = Z_ERR; 25631300Sgjelinek } 25641300Sgjelinek } 25651300Sgjelinek 25661300Sgjelinek (void) zonecfg_endipdent(s_handle); 25671300Sgjelinek 25681300Sgjelinek /* skip the next check if we already have errors */ 25691300Sgjelinek if (res == Z_ERR) 25701300Sgjelinek return (res); 25711300Sgjelinek 25721300Sgjelinek /* 25731300Sgjelinek * Now check the number of ipds in the target so we can verify 25741300Sgjelinek * that the source is not a subset of the target. 25751300Sgjelinek */ 25761300Sgjelinek if ((err = zonecfg_setipdent(t_handle)) != Z_OK) { 25771300Sgjelinek errno = err; 25781300Sgjelinek zperror2(target_zone, gettext("could not enumerate " 25791300Sgjelinek "inherit-pkg-dirs")); 25801300Sgjelinek return (Z_ERR); 25811300Sgjelinek } 25821300Sgjelinek 25831300Sgjelinek while (zonecfg_getipdent(t_handle, &t_fstab) == Z_OK) 25841300Sgjelinek t_cnt++; 25851300Sgjelinek 25861300Sgjelinek (void) zonecfg_endipdent(t_handle); 25871300Sgjelinek 25881300Sgjelinek if (t_cnt != s_cnt) { 25891300Sgjelinek (void) fprintf(stderr, gettext("Zone %s is configured " 25901300Sgjelinek "with inherit-pkg-dirs that are not configured in zone " 25911300Sgjelinek "%s.\n"), target_zone, source_zone); 25921300Sgjelinek res = Z_ERR; 25931300Sgjelinek } 25941300Sgjelinek 25951300Sgjelinek return (res); 25961300Sgjelinek } 25971300Sgjelinek 25981300Sgjelinek static void 25991300Sgjelinek warn_dev_match(zone_dochandle_t s_handle, char *source_zone, 26001300Sgjelinek zone_dochandle_t t_handle, char *target_zone) 26011300Sgjelinek { 26021300Sgjelinek int err; 26031300Sgjelinek struct zone_devtab s_devtab; 26041300Sgjelinek struct zone_devtab t_devtab; 26051300Sgjelinek 26061300Sgjelinek if ((err = zonecfg_setdevent(t_handle)) != Z_OK) { 26071300Sgjelinek errno = err; 26081300Sgjelinek zperror2(target_zone, gettext("could not enumerate devices")); 26091300Sgjelinek return; 26101300Sgjelinek } 26111300Sgjelinek 26121300Sgjelinek while (zonecfg_getdevent(t_handle, &t_devtab) == Z_OK) { 26131300Sgjelinek if ((err = zonecfg_setdevent(s_handle)) != Z_OK) { 26141300Sgjelinek errno = err; 26151300Sgjelinek zperror2(source_zone, 26161300Sgjelinek gettext("could not enumerate devices")); 26171300Sgjelinek (void) zonecfg_enddevent(t_handle); 26181300Sgjelinek return; 26191300Sgjelinek } 26201300Sgjelinek 26211300Sgjelinek while (zonecfg_getdevent(s_handle, &s_devtab) == Z_OK) { 26221300Sgjelinek /* 26231300Sgjelinek * Use fnmatch to catch the case where wildcards 26241300Sgjelinek * were used in one zone and the other has an 26251300Sgjelinek * explicit entry (e.g. /dev/dsk/c0t0d0s6 vs. 26261300Sgjelinek * /dev/\*dsk/c0t0d0s6). 26271300Sgjelinek */ 26281300Sgjelinek if (fnmatch(t_devtab.zone_dev_match, 26291300Sgjelinek s_devtab.zone_dev_match, FNM_PATHNAME) == 0 || 26301300Sgjelinek fnmatch(s_devtab.zone_dev_match, 26311300Sgjelinek t_devtab.zone_dev_match, FNM_PATHNAME) == 0) { 26321300Sgjelinek (void) fprintf(stderr, 26331300Sgjelinek gettext("WARNING: device '%s' " 26341300Sgjelinek "is configured in both zones.\n"), 26351300Sgjelinek t_devtab.zone_dev_match); 26361300Sgjelinek break; 26371300Sgjelinek } 26381300Sgjelinek } 26391300Sgjelinek (void) zonecfg_enddevent(s_handle); 26401300Sgjelinek } 26411300Sgjelinek 26421300Sgjelinek (void) zonecfg_enddevent(t_handle); 26431300Sgjelinek } 26441300Sgjelinek 26451300Sgjelinek /* 26461300Sgjelinek * Check if the specified mount option (opt) is contained within the 26471300Sgjelinek * options string. 26481300Sgjelinek */ 26491300Sgjelinek static boolean_t 26501300Sgjelinek opt_match(char *opt, char *options) 26511300Sgjelinek { 26521300Sgjelinek char *p; 26531300Sgjelinek char *lastp; 26541300Sgjelinek 26551300Sgjelinek if ((p = strtok_r(options, ",", &lastp)) != NULL) { 26561300Sgjelinek if (strcmp(p, opt) == 0) 26571300Sgjelinek return (B_TRUE); 26581300Sgjelinek while ((p = strtok_r(NULL, ",", &lastp)) != NULL) { 26591300Sgjelinek if (strcmp(p, opt) == 0) 26601300Sgjelinek return (B_TRUE); 26611300Sgjelinek } 26621300Sgjelinek } 26631300Sgjelinek 26641300Sgjelinek return (B_FALSE); 26651300Sgjelinek } 26661300Sgjelinek 26671300Sgjelinek #define RW_LOFS "WARNING: read-write lofs file-system on '%s' is configured " \ 26681300Sgjelinek "in both zones.\n" 26691300Sgjelinek 26701300Sgjelinek static void 26711300Sgjelinek print_fs_warnings(struct zone_fstab *s_fstab, struct zone_fstab *t_fstab) 26721300Sgjelinek { 26731300Sgjelinek /* 26741300Sgjelinek * It is ok to have shared lofs mounted fs but we want to warn if 26751300Sgjelinek * either is rw since this will effect the other zone. 26761300Sgjelinek */ 26771300Sgjelinek if (strcmp(t_fstab->zone_fs_type, "lofs") == 0) { 26781300Sgjelinek zone_fsopt_t *optp; 26791300Sgjelinek 26801300Sgjelinek /* The default is rw so no options means rw */ 26811300Sgjelinek if (t_fstab->zone_fs_options == NULL || 26821300Sgjelinek s_fstab->zone_fs_options == NULL) { 26831300Sgjelinek (void) fprintf(stderr, gettext(RW_LOFS), 26841300Sgjelinek t_fstab->zone_fs_special); 26851300Sgjelinek return; 26861300Sgjelinek } 26871300Sgjelinek 26881300Sgjelinek for (optp = s_fstab->zone_fs_options; optp != NULL; 26891300Sgjelinek optp = optp->zone_fsopt_next) { 26901300Sgjelinek if (opt_match("rw", optp->zone_fsopt_opt)) { 26911300Sgjelinek (void) fprintf(stderr, gettext(RW_LOFS), 26921300Sgjelinek s_fstab->zone_fs_special); 26931300Sgjelinek return; 26941300Sgjelinek } 26951300Sgjelinek } 26961300Sgjelinek 26971300Sgjelinek for (optp = t_fstab->zone_fs_options; optp != NULL; 26981300Sgjelinek optp = optp->zone_fsopt_next) { 26991300Sgjelinek if (opt_match("rw", optp->zone_fsopt_opt)) { 27001300Sgjelinek (void) fprintf(stderr, gettext(RW_LOFS), 27011300Sgjelinek t_fstab->zone_fs_special); 27021300Sgjelinek return; 27031300Sgjelinek } 27041300Sgjelinek } 27051300Sgjelinek 27061300Sgjelinek return; 27071300Sgjelinek } 27081300Sgjelinek 27091300Sgjelinek /* 27101300Sgjelinek * TRANSLATION_NOTE 27111300Sgjelinek * The first variable is the file-system type and the second is 27121300Sgjelinek * the file-system special device. For example, 27131300Sgjelinek * WARNING: ufs file-system on '/dev/dsk/c0t0d0s0' ... 27141300Sgjelinek */ 27151300Sgjelinek (void) fprintf(stderr, gettext("WARNING: %s file-system on '%s' " 27161300Sgjelinek "is configured in both zones.\n"), t_fstab->zone_fs_type, 27171300Sgjelinek t_fstab->zone_fs_special); 27181300Sgjelinek } 27191300Sgjelinek 27201300Sgjelinek static void 27211300Sgjelinek warn_fs_match(zone_dochandle_t s_handle, char *source_zone, 27221300Sgjelinek zone_dochandle_t t_handle, char *target_zone) 27231300Sgjelinek { 27241300Sgjelinek int err; 27251300Sgjelinek struct zone_fstab s_fstab; 27261300Sgjelinek struct zone_fstab t_fstab; 27271300Sgjelinek 27281300Sgjelinek if ((err = zonecfg_setfsent(t_handle)) != Z_OK) { 27291300Sgjelinek errno = err; 27301300Sgjelinek zperror2(target_zone, 27311300Sgjelinek gettext("could not enumerate file-systems")); 27321300Sgjelinek return; 27331300Sgjelinek } 27341300Sgjelinek 27351300Sgjelinek while (zonecfg_getfsent(t_handle, &t_fstab) == Z_OK) { 27361300Sgjelinek if ((err = zonecfg_setfsent(s_handle)) != Z_OK) { 27371300Sgjelinek errno = err; 27381300Sgjelinek zperror2(source_zone, 27391300Sgjelinek gettext("could not enumerate file-systems")); 27401300Sgjelinek (void) zonecfg_endfsent(t_handle); 27411300Sgjelinek return; 27421300Sgjelinek } 27431300Sgjelinek 27441300Sgjelinek while (zonecfg_getfsent(s_handle, &s_fstab) == Z_OK) { 27451300Sgjelinek if (strcmp(t_fstab.zone_fs_special, 27461300Sgjelinek s_fstab.zone_fs_special) == 0) { 27471300Sgjelinek print_fs_warnings(&s_fstab, &t_fstab); 27481300Sgjelinek break; 27491300Sgjelinek } 27501300Sgjelinek } 27511300Sgjelinek (void) zonecfg_endfsent(s_handle); 27521300Sgjelinek } 27531300Sgjelinek 27541300Sgjelinek (void) zonecfg_endfsent(t_handle); 27551300Sgjelinek } 27561300Sgjelinek 27571300Sgjelinek /* 27581300Sgjelinek * We don't catch the case where you used the same IP address but 27591300Sgjelinek * it is not an exact string match. For example, 192.9.0.128 vs. 192.09.0.128. 27601300Sgjelinek * However, we're not going to worry about that but we will check for 27611300Sgjelinek * a possible netmask on one of the addresses (e.g. 10.0.0.1 and 10.0.0.1/24) 27621300Sgjelinek * and handle that case as a match. 27631300Sgjelinek */ 27641300Sgjelinek static void 27651300Sgjelinek warn_ip_match(zone_dochandle_t s_handle, char *source_zone, 27661300Sgjelinek zone_dochandle_t t_handle, char *target_zone) 27671300Sgjelinek { 27681300Sgjelinek int err; 27691300Sgjelinek struct zone_nwiftab s_nwiftab; 27701300Sgjelinek struct zone_nwiftab t_nwiftab; 27711300Sgjelinek 27721300Sgjelinek if ((err = zonecfg_setnwifent(t_handle)) != Z_OK) { 27731300Sgjelinek errno = err; 27741300Sgjelinek zperror2(target_zone, 27751300Sgjelinek gettext("could not enumerate network interfaces")); 27761300Sgjelinek return; 27771300Sgjelinek } 27781300Sgjelinek 27791300Sgjelinek while (zonecfg_getnwifent(t_handle, &t_nwiftab) == Z_OK) { 27801300Sgjelinek char *p; 27811300Sgjelinek 27821300Sgjelinek /* remove an (optional) netmask from the address */ 27831300Sgjelinek if ((p = strchr(t_nwiftab.zone_nwif_address, '/')) != NULL) 27841300Sgjelinek *p = '\0'; 27851300Sgjelinek 27861300Sgjelinek if ((err = zonecfg_setnwifent(s_handle)) != Z_OK) { 27871300Sgjelinek errno = err; 27881300Sgjelinek zperror2(source_zone, 27891300Sgjelinek gettext("could not enumerate network interfaces")); 27901300Sgjelinek (void) zonecfg_endnwifent(t_handle); 27911300Sgjelinek return; 27921300Sgjelinek } 27931300Sgjelinek 27941300Sgjelinek while (zonecfg_getnwifent(s_handle, &s_nwiftab) == Z_OK) { 27951300Sgjelinek /* remove an (optional) netmask from the address */ 27961300Sgjelinek if ((p = strchr(s_nwiftab.zone_nwif_address, '/')) 27971300Sgjelinek != NULL) 27981300Sgjelinek *p = '\0'; 27991300Sgjelinek 28001300Sgjelinek if (strcmp(t_nwiftab.zone_nwif_address, 28011300Sgjelinek s_nwiftab.zone_nwif_address) == 0) { 28021300Sgjelinek (void) fprintf(stderr, 28031300Sgjelinek gettext("WARNING: network address '%s' " 28041300Sgjelinek "is configured in both zones.\n"), 28051300Sgjelinek t_nwiftab.zone_nwif_address); 28061300Sgjelinek break; 28071300Sgjelinek } 28081300Sgjelinek } 28091300Sgjelinek (void) zonecfg_endnwifent(s_handle); 28101300Sgjelinek } 28111300Sgjelinek 28121300Sgjelinek (void) zonecfg_endnwifent(t_handle); 28131300Sgjelinek } 28141300Sgjelinek 28151300Sgjelinek static void 28161300Sgjelinek warn_dataset_match(zone_dochandle_t s_handle, char *source_zone, 28171300Sgjelinek zone_dochandle_t t_handle, char *target_zone) 28181300Sgjelinek { 28191300Sgjelinek int err; 28201300Sgjelinek struct zone_dstab s_dstab; 28211300Sgjelinek struct zone_dstab t_dstab; 28221300Sgjelinek 28231300Sgjelinek if ((err = zonecfg_setdsent(t_handle)) != Z_OK) { 28241300Sgjelinek errno = err; 28251300Sgjelinek zperror2(target_zone, gettext("could not enumerate datasets")); 28261300Sgjelinek return; 28271300Sgjelinek } 28281300Sgjelinek 28291300Sgjelinek while (zonecfg_getdsent(t_handle, &t_dstab) == Z_OK) { 28301300Sgjelinek if ((err = zonecfg_setdsent(s_handle)) != Z_OK) { 28311300Sgjelinek errno = err; 28321300Sgjelinek zperror2(source_zone, 28331300Sgjelinek gettext("could not enumerate datasets")); 28341300Sgjelinek (void) zonecfg_enddsent(t_handle); 28351300Sgjelinek return; 28361300Sgjelinek } 28371300Sgjelinek 28381300Sgjelinek while (zonecfg_getdsent(s_handle, &s_dstab) == Z_OK) { 28391300Sgjelinek if (strcmp(t_dstab.zone_dataset_name, 28401300Sgjelinek s_dstab.zone_dataset_name) == 0) { 28411300Sgjelinek (void) fprintf(stderr, 28421300Sgjelinek gettext("WARNING: dataset '%s' " 28431300Sgjelinek "is configured in both zones.\n"), 28441300Sgjelinek t_dstab.zone_dataset_name); 28451300Sgjelinek break; 28461300Sgjelinek } 28471300Sgjelinek } 28481300Sgjelinek (void) zonecfg_enddsent(s_handle); 28491300Sgjelinek } 28501300Sgjelinek 28511300Sgjelinek (void) zonecfg_enddsent(t_handle); 28521300Sgjelinek } 28531300Sgjelinek 28541300Sgjelinek static int 28551300Sgjelinek validate_clone(char *source_zone, char *target_zone) 28561300Sgjelinek { 28571300Sgjelinek int err = Z_OK; 28581300Sgjelinek zone_dochandle_t s_handle; 28591300Sgjelinek zone_dochandle_t t_handle; 28601300Sgjelinek 28611300Sgjelinek if ((t_handle = zonecfg_init_handle()) == NULL) { 28621300Sgjelinek zperror(cmd_to_str(CMD_CLONE), B_TRUE); 28631300Sgjelinek return (Z_ERR); 28641300Sgjelinek } 28651300Sgjelinek if ((err = zonecfg_get_handle(target_zone, t_handle)) != Z_OK) { 28661300Sgjelinek errno = err; 28671300Sgjelinek zperror(cmd_to_str(CMD_CLONE), B_TRUE); 28681300Sgjelinek zonecfg_fini_handle(t_handle); 28691300Sgjelinek return (Z_ERR); 28701300Sgjelinek } 28711300Sgjelinek 28721300Sgjelinek if ((s_handle = zonecfg_init_handle()) == NULL) { 28731300Sgjelinek zperror(cmd_to_str(CMD_CLONE), B_TRUE); 28741300Sgjelinek zonecfg_fini_handle(t_handle); 28751300Sgjelinek return (Z_ERR); 28761300Sgjelinek } 28771300Sgjelinek if ((err = zonecfg_get_handle(source_zone, s_handle)) != Z_OK) { 28781300Sgjelinek errno = err; 28791300Sgjelinek zperror(cmd_to_str(CMD_CLONE), B_TRUE); 28801300Sgjelinek goto done; 28811300Sgjelinek } 28821300Sgjelinek 28831300Sgjelinek /* verify new zone has same inherit-pkg-dirs */ 28841300Sgjelinek err = valid_ipd_clone(s_handle, source_zone, t_handle, target_zone); 28851300Sgjelinek 28861300Sgjelinek /* warn about imported fs's which are the same */ 28871300Sgjelinek warn_fs_match(s_handle, source_zone, t_handle, target_zone); 28881300Sgjelinek 28891300Sgjelinek /* warn about imported IP addresses which are the same */ 28901300Sgjelinek warn_ip_match(s_handle, source_zone, t_handle, target_zone); 28911300Sgjelinek 28921300Sgjelinek /* warn about imported devices which are the same */ 28931300Sgjelinek warn_dev_match(s_handle, source_zone, t_handle, target_zone); 28941300Sgjelinek 28951300Sgjelinek /* warn about imported datasets which are the same */ 28961300Sgjelinek warn_dataset_match(s_handle, source_zone, t_handle, target_zone); 28971300Sgjelinek 28981300Sgjelinek done: 28991300Sgjelinek zonecfg_fini_handle(t_handle); 29001300Sgjelinek zonecfg_fini_handle(s_handle); 29011300Sgjelinek 29021300Sgjelinek return ((err == Z_OK) ? Z_OK : Z_ERR); 29031300Sgjelinek } 29041300Sgjelinek 29051300Sgjelinek static int 29061300Sgjelinek copy_zone(char *src, char *dst) 29071300Sgjelinek { 29081300Sgjelinek boolean_t out_null = B_FALSE; 29091300Sgjelinek int status; 29101300Sgjelinek int err; 29111300Sgjelinek char *outfile; 29121300Sgjelinek char cmdbuf[MAXPATHLEN * 2 + 128]; 29131300Sgjelinek 29141300Sgjelinek if ((outfile = tempnam("/var/log", "zone")) == NULL) { 29151300Sgjelinek outfile = "/dev/null"; 29161300Sgjelinek out_null = B_TRUE; 29171300Sgjelinek } 29181300Sgjelinek 29191300Sgjelinek (void) snprintf(cmdbuf, sizeof (cmdbuf), 29201300Sgjelinek "cd %s && /usr/bin/find . -depth -print | " 29211300Sgjelinek "/usr/bin/cpio -pdmuP@ %s > %s 2>&1", 29221300Sgjelinek src, dst, outfile); 29231300Sgjelinek 29241300Sgjelinek status = do_subproc(cmdbuf); 29251300Sgjelinek 29261300Sgjelinek if ((err = subproc_status("copy", status)) != Z_OK) { 29271300Sgjelinek if (!out_null) 29281300Sgjelinek (void) fprintf(stderr, gettext("\nThe copy failed.\n" 29291300Sgjelinek "More information can be found in %s\n"), outfile); 29301300Sgjelinek return (err); 29311300Sgjelinek } 29321300Sgjelinek 29331300Sgjelinek if (!out_null) 29341300Sgjelinek (void) unlink(outfile); 29351300Sgjelinek 29361300Sgjelinek return (Z_OK); 29371300Sgjelinek } 29381300Sgjelinek 29391300Sgjelinek /* 29401300Sgjelinek * Wait until the target_zone has booted to single-user. Return Z_OK once 29411300Sgjelinek * the zone has booted to that level or return Z_BAD_ZONE_STATE if the zone 29421300Sgjelinek * has not booted to single-user after the timeout. 29431300Sgjelinek */ 29441300Sgjelinek static int 29451300Sgjelinek zone_wait_single_user() 29461300Sgjelinek { 29471300Sgjelinek char cmdbuf[ZONENAME_MAX + 256]; 29481300Sgjelinek int retry; 29491300Sgjelinek 29501300Sgjelinek (void) snprintf(cmdbuf, sizeof (cmdbuf), 29511300Sgjelinek "test \"`/usr/sbin/zlogin -S %s /usr/bin/svcprop -p " 29521300Sgjelinek "restarter/state svc:/milestone/single-user:default 2>/dev/null`\" " 29531300Sgjelinek "= \"online\"", 29541300Sgjelinek target_zone); 29551300Sgjelinek 29561300Sgjelinek for (retry = 0; retry < SINGLE_USER_RETRY; retry++) { 29571300Sgjelinek int status; 29581300Sgjelinek 29591300Sgjelinek status = do_subproc(cmdbuf); 29601300Sgjelinek if (WIFEXITED(status)) { 29611300Sgjelinek if (WEXITSTATUS(status) == 0) 29621300Sgjelinek return (Z_OK); 29631300Sgjelinek 29641300Sgjelinek (void) sleep(2); 29651300Sgjelinek } else { 29661300Sgjelinek return (Z_BAD_ZONE_STATE); 29671300Sgjelinek } 29681300Sgjelinek } 29691300Sgjelinek 29701300Sgjelinek return (Z_BAD_ZONE_STATE); 29711300Sgjelinek } 29721300Sgjelinek 29731300Sgjelinek 29741300Sgjelinek /* ARGSUSED */ 29751300Sgjelinek int 29761300Sgjelinek zfm_print(const char *p, void *r) { 29771300Sgjelinek zerror(" %s\n", p); 29781300Sgjelinek return (0); 29791300Sgjelinek } 29801300Sgjelinek 29811300Sgjelinek static int 29821300Sgjelinek clone_func(int argc, char *argv[]) 29831300Sgjelinek { 29841300Sgjelinek char cmdbuf[MAXPATHLEN]; 29851300Sgjelinek char *source_zone = NULL; 29861300Sgjelinek int lockfd; 29871300Sgjelinek int err, arg; 29881300Sgjelinek char zonepath[MAXPATHLEN]; 29891300Sgjelinek char source_zonepath[MAXPATHLEN]; 29901300Sgjelinek int status; 29911300Sgjelinek zone_state_t state; 29921300Sgjelinek zone_entry_t *zent; 29931300Sgjelinek char *method = "copy"; 29941300Sgjelinek char *boot_args[] = { "-s", NULL }; 29951300Sgjelinek char *halt_args[] = { NULL }; 29961300Sgjelinek struct stat unconfig_buf; 29971300Sgjelinek boolean_t revert; 29981300Sgjelinek 29991300Sgjelinek if (zonecfg_in_alt_root()) { 30001300Sgjelinek zerror(gettext("cannot clone zone in alternate root")); 30011300Sgjelinek return (Z_ERR); 30021300Sgjelinek } 30031300Sgjelinek 30041300Sgjelinek optind = 0; 30051300Sgjelinek if ((arg = getopt(argc, argv, "?m:")) != EOF) { 30061300Sgjelinek switch (arg) { 30071300Sgjelinek case '?': 30081300Sgjelinek sub_usage(SHELP_CLONE, CMD_CLONE); 30091300Sgjelinek return (optopt == '?' ? Z_OK : Z_USAGE); 30101300Sgjelinek case 'm': 30111300Sgjelinek method = optarg; 30121300Sgjelinek break; 30131300Sgjelinek default: 30141300Sgjelinek sub_usage(SHELP_CLONE, CMD_CLONE); 30151300Sgjelinek return (Z_USAGE); 30161300Sgjelinek } 30171300Sgjelinek } 30181300Sgjelinek if (argc != (optind + 1) || strcmp(method, "copy") != 0) { 30191300Sgjelinek sub_usage(SHELP_CLONE, CMD_CLONE); 30201300Sgjelinek return (Z_USAGE); 30211300Sgjelinek } 30221300Sgjelinek source_zone = argv[optind]; 30231300Sgjelinek if (sanity_check(target_zone, CMD_CLONE, B_FALSE, B_TRUE) != Z_OK) 30241300Sgjelinek return (Z_ERR); 30251300Sgjelinek if (verify_details(CMD_CLONE) != Z_OK) 30261300Sgjelinek return (Z_ERR); 30271300Sgjelinek 30281300Sgjelinek /* 30291300Sgjelinek * We also need to do some extra validation on the source zone. 30301300Sgjelinek */ 30311300Sgjelinek 30321300Sgjelinek if (strcmp(source_zone, GLOBAL_ZONENAME) == 0) { 30331300Sgjelinek zerror(gettext("%s operation is invalid for the global zone."), 30341300Sgjelinek cmd_to_str(CMD_CLONE)); 30351300Sgjelinek return (Z_ERR); 30361300Sgjelinek } 30371300Sgjelinek 30381300Sgjelinek if (strncmp(source_zone, "SUNW", 4) == 0) { 30391300Sgjelinek zerror(gettext("%s operation is invalid for zones starting " 30401300Sgjelinek "with SUNW."), cmd_to_str(CMD_CLONE)); 30411300Sgjelinek return (Z_ERR); 30421300Sgjelinek } 30431300Sgjelinek 30441300Sgjelinek zent = lookup_running_zone(source_zone); 30451300Sgjelinek if (zent != NULL) { 30461300Sgjelinek /* check whether the zone is ready or running */ 30471300Sgjelinek if ((err = zone_get_state(zent->zname, &zent->zstate_num)) 30481300Sgjelinek != Z_OK) { 30491300Sgjelinek errno = err; 30501300Sgjelinek zperror2(zent->zname, gettext("could not get state")); 30511300Sgjelinek /* can't tell, so hedge */ 30521300Sgjelinek zent->zstate_str = "ready/running"; 30531300Sgjelinek } else { 30541300Sgjelinek zent->zstate_str = zone_state_str(zent->zstate_num); 30551300Sgjelinek } 30561300Sgjelinek zerror(gettext("%s operation is invalid for %s zones."), 30571300Sgjelinek cmd_to_str(CMD_CLONE), zent->zstate_str); 30581300Sgjelinek return (Z_ERR); 30591300Sgjelinek } 30601300Sgjelinek 30611300Sgjelinek if ((err = zone_get_state(source_zone, &state)) != Z_OK) { 30621300Sgjelinek errno = err; 30631300Sgjelinek zperror2(source_zone, gettext("could not get state")); 30641300Sgjelinek return (Z_ERR); 30651300Sgjelinek } 30661300Sgjelinek if (state != ZONE_STATE_INSTALLED) { 30671300Sgjelinek (void) fprintf(stderr, 30681300Sgjelinek gettext("%s: zone %s is %s; %s is required.\n"), 30691300Sgjelinek execname, source_zone, zone_state_str(state), 30701300Sgjelinek zone_state_str(ZONE_STATE_INSTALLED)); 30711300Sgjelinek return (Z_ERR); 30721300Sgjelinek } 30731300Sgjelinek 30741300Sgjelinek /* 30751300Sgjelinek * The source zone checks out ok, continue with the clone. 30761300Sgjelinek */ 30771300Sgjelinek 30781300Sgjelinek if (validate_clone(source_zone, target_zone) != Z_OK) 30791300Sgjelinek return (Z_ERR); 30801300Sgjelinek 30811300Sgjelinek if (grab_lock_file(target_zone, &lockfd) != Z_OK) { 30821300Sgjelinek zerror(gettext("another %s may have an operation in progress."), 30831300Sgjelinek "zoneadm"); 30841300Sgjelinek return (Z_ERR); 30851300Sgjelinek } 30861300Sgjelinek 30871300Sgjelinek if ((err = zone_get_zonepath(source_zone, source_zonepath, 30881300Sgjelinek sizeof (source_zonepath))) != Z_OK) { 30891300Sgjelinek errno = err; 30901300Sgjelinek zperror2(source_zone, gettext("could not get zone path")); 30911300Sgjelinek goto done; 30921300Sgjelinek } 30931300Sgjelinek 30941300Sgjelinek if ((err = zone_get_zonepath(target_zone, zonepath, sizeof (zonepath))) 30951300Sgjelinek != Z_OK) { 30961300Sgjelinek errno = err; 30971300Sgjelinek zperror2(target_zone, gettext("could not get zone path")); 30981300Sgjelinek goto done; 30991300Sgjelinek } 31001300Sgjelinek 31011300Sgjelinek /* Don't clone the zone if anything is still mounted there */ 31021300Sgjelinek if (zonecfg_find_mounts(source_zonepath, NULL, NULL)) { 31031300Sgjelinek zerror(gettext("These file-systems are mounted on " 31041300Sgjelinek "subdirectories of %s.\n"), source_zonepath); 31051300Sgjelinek (void) zonecfg_find_mounts(source_zonepath, zfm_print, NULL); 31061300Sgjelinek err = Z_ERR; 31071300Sgjelinek goto done; 31081300Sgjelinek } 31091300Sgjelinek 31101300Sgjelinek if ((err = zone_set_state(target_zone, ZONE_STATE_INCOMPLETE)) 31111300Sgjelinek != Z_OK) { 31121300Sgjelinek errno = err; 31131300Sgjelinek zperror2(target_zone, gettext("could not set state")); 31141300Sgjelinek goto done; 31151300Sgjelinek } 31161300Sgjelinek 31171300Sgjelinek (void) printf(gettext("Cloning zonepath %s..."), source_zonepath); 31181300Sgjelinek (void) fflush(stdout); 31191300Sgjelinek 31201300Sgjelinek if ((err = copy_zone(source_zonepath, zonepath)) != Z_OK) 31211300Sgjelinek goto done; 31221300Sgjelinek 31231300Sgjelinek /* 31241300Sgjelinek * We have to set the state of the zone to installed so that we 31251300Sgjelinek * can boot it and sys-unconfig it from within the zone. However, 31261300Sgjelinek * if something fails during the boot/sys-unconfig, we want to set 31271300Sgjelinek * the state back to incomplete. We use the revert flag to keep 31281300Sgjelinek * track of this. 31291300Sgjelinek */ 31301300Sgjelinek revert = B_TRUE; 31311300Sgjelinek 31321300Sgjelinek if ((err = zone_set_state(target_zone, ZONE_STATE_INSTALLED)) != Z_OK) { 31331300Sgjelinek errno = err; 31341300Sgjelinek zperror2(target_zone, gettext("\ncould not set state")); 31351300Sgjelinek goto done; 31361300Sgjelinek } 31371300Sgjelinek 31381300Sgjelinek /* 31391300Sgjelinek * Check if the zone is already sys-unconfiged. This saves us 31401300Sgjelinek * the work of booting the zone so we can unconfigure it. 31411300Sgjelinek */ 31421300Sgjelinek (void) snprintf(cmdbuf, sizeof (cmdbuf), "%s/root/etc/.UNCONFIGURED", 31431300Sgjelinek zonepath); 31441300Sgjelinek if (stat(cmdbuf, &unconfig_buf) == -1) { 31451300Sgjelinek if ((err = boot_func(1, boot_args)) != Z_OK) { 31461300Sgjelinek errno = err; 31471300Sgjelinek zperror2(target_zone, gettext("\nCould not boot zone " 31481300Sgjelinek "for sys-unconfig\n")); 31491300Sgjelinek goto done; 31501300Sgjelinek } 31511300Sgjelinek 31521300Sgjelinek if ((err = zone_wait_single_user()) != Z_OK) { 31531300Sgjelinek errno = err; 31541300Sgjelinek zperror2(target_zone, gettext("\nCould not boot zone " 31551300Sgjelinek "for sys-unconfig\n")); 31561300Sgjelinek (void) halt_func(0, halt_args); 31571300Sgjelinek goto done; 31581300Sgjelinek } 31591300Sgjelinek 31601300Sgjelinek (void) snprintf(cmdbuf, sizeof (cmdbuf), 31611300Sgjelinek "echo y | /usr/sbin/zlogin -S %s /usr/sbin/sys-unconfig", 31621300Sgjelinek target_zone); 31631300Sgjelinek 31641300Sgjelinek status = do_subproc(cmdbuf); 31651300Sgjelinek if ((err = subproc_status("sys-unconfig", status)) != Z_OK) { 31661300Sgjelinek errno = err; 31671300Sgjelinek zperror2(target_zone, 31681300Sgjelinek gettext("\nsys-unconfig failed\n")); 31691300Sgjelinek /* 31701300Sgjelinek * The sys-unconfig halts the zone but if it failed, 31711300Sgjelinek * for some reason, we'll try to halt it now. 31721300Sgjelinek */ 31731300Sgjelinek (void) halt_func(0, halt_args); 31741300Sgjelinek goto done; 31751300Sgjelinek } 31761300Sgjelinek } 31771300Sgjelinek 31781300Sgjelinek revert = B_FALSE; 31791300Sgjelinek 31801300Sgjelinek done: 31811300Sgjelinek (void) printf("\n"); 31821300Sgjelinek 31831300Sgjelinek if (revert) 31841300Sgjelinek (void) zone_set_state(target_zone, ZONE_STATE_INCOMPLETE); 31851300Sgjelinek 31861300Sgjelinek release_lock_file(lockfd); 31871300Sgjelinek return ((err == Z_OK) ? Z_OK : Z_ERR); 31881300Sgjelinek } 31891300Sgjelinek 31901300Sgjelinek #define RMCOMMAND "/usr/bin/rm -rf" 31911300Sgjelinek 31921300Sgjelinek static int 31931300Sgjelinek move_func(int argc, char *argv[]) 31941300Sgjelinek { 31951300Sgjelinek /* 6: "exec " and " " */ 31961300Sgjelinek char cmdbuf[sizeof (RMCOMMAND) + MAXPATHLEN + 6]; 31971300Sgjelinek char *new_zonepath = NULL; 31981300Sgjelinek int lockfd; 31991300Sgjelinek int err, arg; 32001300Sgjelinek char zonepath[MAXPATHLEN]; 32011300Sgjelinek zone_dochandle_t handle; 32021300Sgjelinek boolean_t fast; 32031300Sgjelinek boolean_t revert; 32041300Sgjelinek struct stat zonepath_buf; 32051300Sgjelinek struct stat new_zonepath_buf; 32061300Sgjelinek 32071300Sgjelinek if (zonecfg_in_alt_root()) { 32081300Sgjelinek zerror(gettext("cannot move zone in alternate root")); 32091300Sgjelinek return (Z_ERR); 32101300Sgjelinek } 32111300Sgjelinek 32121300Sgjelinek optind = 0; 32131300Sgjelinek if ((arg = getopt(argc, argv, "?")) != EOF) { 32141300Sgjelinek switch (arg) { 32151300Sgjelinek case '?': 32161300Sgjelinek sub_usage(SHELP_MOVE, CMD_MOVE); 32171300Sgjelinek return (optopt == '?' ? Z_OK : Z_USAGE); 32181300Sgjelinek default: 32191300Sgjelinek sub_usage(SHELP_MOVE, CMD_MOVE); 32201300Sgjelinek return (Z_USAGE); 32211300Sgjelinek } 32221300Sgjelinek } 32231300Sgjelinek if (argc != (optind + 1)) { 32241300Sgjelinek sub_usage(SHELP_MOVE, CMD_MOVE); 32251300Sgjelinek return (Z_USAGE); 32261300Sgjelinek } 32271300Sgjelinek new_zonepath = argv[optind]; 32281300Sgjelinek if (sanity_check(target_zone, CMD_MOVE, B_FALSE, B_TRUE) != Z_OK) 32291300Sgjelinek return (Z_ERR); 32301300Sgjelinek if (verify_details(CMD_MOVE) != Z_OK) 32311300Sgjelinek return (Z_ERR); 32321300Sgjelinek 32331300Sgjelinek /* 32341300Sgjelinek * Check out the new zonepath. This has the side effect of creating 32351300Sgjelinek * a directory for the new zonepath. We depend on this later when we 32361300Sgjelinek * stat to see if we are doing a cross file-system move or not. 32371300Sgjelinek */ 32381300Sgjelinek if (validate_zonepath(new_zonepath, CMD_MOVE) != Z_OK) 32391300Sgjelinek return (Z_ERR); 32401300Sgjelinek 32411300Sgjelinek if ((err = zone_get_zonepath(target_zone, zonepath, sizeof (zonepath))) 32421300Sgjelinek != Z_OK) { 32431300Sgjelinek errno = err; 32441300Sgjelinek zperror2(target_zone, gettext("could not get zone path")); 32451300Sgjelinek return (Z_ERR); 32461300Sgjelinek } 32471300Sgjelinek 32481300Sgjelinek if (stat(zonepath, &zonepath_buf) == -1) { 32491300Sgjelinek zperror(gettext("could not stat zone path"), B_FALSE); 32501300Sgjelinek return (Z_ERR); 32511300Sgjelinek } 32521300Sgjelinek 32531300Sgjelinek if (stat(new_zonepath, &new_zonepath_buf) == -1) { 32541300Sgjelinek zperror(gettext("could not stat new zone path"), B_FALSE); 32551300Sgjelinek return (Z_ERR); 32561300Sgjelinek } 32571300Sgjelinek 32581300Sgjelinek /* Don't move the zone if anything is still mounted there */ 32591300Sgjelinek if (zonecfg_find_mounts(zonepath, NULL, NULL)) { 32601300Sgjelinek zerror(gettext("These file-systems are mounted on " 32611300Sgjelinek "subdirectories of %s.\n"), zonepath); 32621300Sgjelinek (void) zonecfg_find_mounts(zonepath, zfm_print, NULL); 32631300Sgjelinek return (Z_ERR); 32641300Sgjelinek } 32651300Sgjelinek 32661300Sgjelinek /* 32671300Sgjelinek * Check if we are moving in the same filesystem and can do a fast 32681300Sgjelinek * move or if we are crossing filesystems and have to copy the data. 32691300Sgjelinek */ 32701300Sgjelinek fast = (zonepath_buf.st_dev == new_zonepath_buf.st_dev); 32711300Sgjelinek 32721300Sgjelinek if ((handle = zonecfg_init_handle()) == NULL) { 32731300Sgjelinek zperror(cmd_to_str(CMD_MOVE), B_TRUE); 32741300Sgjelinek return (Z_ERR); 32751300Sgjelinek } 32761300Sgjelinek 32771300Sgjelinek if ((err = zonecfg_get_handle(target_zone, handle)) != Z_OK) { 32781300Sgjelinek errno = err; 32791300Sgjelinek zperror(cmd_to_str(CMD_MOVE), B_TRUE); 32801300Sgjelinek zonecfg_fini_handle(handle); 32811300Sgjelinek return (Z_ERR); 32821300Sgjelinek } 32831300Sgjelinek 32841300Sgjelinek if (grab_lock_file(target_zone, &lockfd) != Z_OK) { 32851300Sgjelinek zerror(gettext("another %s may have an operation in progress."), 32861300Sgjelinek "zoneadm"); 32871300Sgjelinek zonecfg_fini_handle(handle); 32881300Sgjelinek return (Z_ERR); 32891300Sgjelinek } 32901300Sgjelinek 32911300Sgjelinek /* 32921300Sgjelinek * We're making some file-system changes now so we have to clean up 32931300Sgjelinek * the file-system before we are done. This will either clean up the 32941300Sgjelinek * new zonepath if the zonecfg update failed or it will clean up the 32951300Sgjelinek * old zonepath if everything is ok. 32961300Sgjelinek */ 32971300Sgjelinek revert = B_TRUE; 32981300Sgjelinek 32991300Sgjelinek if (fast) { 33001300Sgjelinek /* same filesystem, use rename for a quick move */ 33011300Sgjelinek 33021300Sgjelinek /* 33031300Sgjelinek * Remove the new_zonepath directory that got created above 33041300Sgjelinek * during the validation. It gets in the way of the rename. 33051300Sgjelinek */ 33061300Sgjelinek if (rmdir(new_zonepath) != 0) { 33071300Sgjelinek zperror(gettext("could not rmdir new zone path"), 33081300Sgjelinek B_FALSE); 33091300Sgjelinek zonecfg_fini_handle(handle); 33101300Sgjelinek release_lock_file(lockfd); 33111300Sgjelinek return (Z_ERR); 33121300Sgjelinek } 33131300Sgjelinek 33141300Sgjelinek if (rename(zonepath, new_zonepath) != 0) { 33151300Sgjelinek /* 33161300Sgjelinek * If this fails we don't need to do all of the 33171300Sgjelinek * cleanup that happens for the rest of the code 33181300Sgjelinek * so just return from this error. 33191300Sgjelinek */ 33201300Sgjelinek zperror(gettext("could not move zone"), B_FALSE); 33211300Sgjelinek zonecfg_fini_handle(handle); 33221300Sgjelinek release_lock_file(lockfd); 33231300Sgjelinek return (Z_ERR); 33241300Sgjelinek } 33251300Sgjelinek 33261300Sgjelinek } else { 33271300Sgjelinek (void) printf(gettext( 33281300Sgjelinek "Moving across file-systems; copying zonepath %s..."), 33291300Sgjelinek zonepath); 33301300Sgjelinek (void) fflush(stdout); 33311300Sgjelinek 33321300Sgjelinek err = copy_zone(zonepath, new_zonepath); 33331300Sgjelinek 33341300Sgjelinek (void) printf("\n"); 33351300Sgjelinek if (err != Z_OK) 33361300Sgjelinek goto done; 33371300Sgjelinek } 33381300Sgjelinek 33391300Sgjelinek if ((err = zonecfg_set_zonepath(handle, new_zonepath)) != Z_OK) { 33401300Sgjelinek errno = err; 33411300Sgjelinek zperror(gettext("could not set new zonepath"), B_TRUE); 33421300Sgjelinek goto done; 33431300Sgjelinek } 33441300Sgjelinek 33451300Sgjelinek if ((err = zonecfg_save(handle)) != Z_OK) { 33461300Sgjelinek errno = err; 33471300Sgjelinek zperror(gettext("zonecfg save failed"), B_TRUE); 33481300Sgjelinek goto done; 33491300Sgjelinek } 33501300Sgjelinek 33511300Sgjelinek revert = B_FALSE; 33521300Sgjelinek 33531300Sgjelinek done: 33541300Sgjelinek zonecfg_fini_handle(handle); 33551300Sgjelinek release_lock_file(lockfd); 33561300Sgjelinek 33571300Sgjelinek /* 33581300Sgjelinek * Clean up the file-system based on how things went. We either 33591300Sgjelinek * clean up the new zonepath if the operation failed for some reason 33601300Sgjelinek * or we clean up the old zonepath if everything is ok. 33611300Sgjelinek */ 33621300Sgjelinek if (revert) { 33631300Sgjelinek /* The zonecfg update failed, cleanup the new zonepath. */ 33641300Sgjelinek if (fast) { 33651300Sgjelinek if (rename(new_zonepath, zonepath) != 0) { 33661300Sgjelinek zperror(gettext("could not restore zonepath"), 33671300Sgjelinek B_FALSE); 33681300Sgjelinek /* 33691300Sgjelinek * err is already != Z_OK since we're reverting 33701300Sgjelinek */ 33711300Sgjelinek } 33721300Sgjelinek } else { 33731300Sgjelinek int status; 33741300Sgjelinek 33751300Sgjelinek (void) printf(gettext("Cleaning up zonepath %s..."), 33761300Sgjelinek new_zonepath); 33771300Sgjelinek (void) fflush(stdout); 33781300Sgjelinek 33791300Sgjelinek /* 33801300Sgjelinek * "exec" the command so that the returned status is 33811300Sgjelinek * that of rm and not the shell. 33821300Sgjelinek */ 33831300Sgjelinek (void) snprintf(cmdbuf, sizeof (cmdbuf), 33841300Sgjelinek "exec " RMCOMMAND " %s", new_zonepath); 33851300Sgjelinek 33861300Sgjelinek status = do_subproc(cmdbuf); 33871300Sgjelinek 33881300Sgjelinek (void) printf("\n"); 33891300Sgjelinek 33901300Sgjelinek if ((err = subproc_status("rm", status)) != Z_OK) { 33911300Sgjelinek errno = err; 33921300Sgjelinek zperror(gettext("could not remove new " 33931300Sgjelinek "zonepath"), B_TRUE); 33941300Sgjelinek } else { 33951300Sgjelinek /* 33961300Sgjelinek * Because we're reverting we know the mainline 33971300Sgjelinek * code failed but we just reused the err 33981300Sgjelinek * variable so we reset it back to Z_ERR. 33991300Sgjelinek */ 34001300Sgjelinek err = Z_ERR; 34011300Sgjelinek } 34021300Sgjelinek } 34031300Sgjelinek 34041300Sgjelinek } else { 34051300Sgjelinek /* The move was successful, cleanup the old zonepath. */ 34061300Sgjelinek if (!fast) { 34071300Sgjelinek int status; 34081300Sgjelinek 34091300Sgjelinek (void) printf( 34101300Sgjelinek gettext("Cleaning up zonepath %s..."), zonepath); 34111300Sgjelinek (void) fflush(stdout); 34121300Sgjelinek 34131300Sgjelinek /* 34141300Sgjelinek * "exec" the command so that the returned status is 34151300Sgjelinek * that of rm and not the shell. 34161300Sgjelinek */ 34171300Sgjelinek (void) snprintf(cmdbuf, sizeof (cmdbuf), 34181300Sgjelinek "exec " RMCOMMAND " %s", zonepath); 34191300Sgjelinek 34201300Sgjelinek status = do_subproc(cmdbuf); 34211300Sgjelinek 34221300Sgjelinek (void) printf("\n"); 34231300Sgjelinek 34241300Sgjelinek if ((err = subproc_status("rm", status)) != Z_OK) { 34251300Sgjelinek errno = err; 34261300Sgjelinek zperror(gettext("could not remove zonepath"), 34271300Sgjelinek B_TRUE); 34281300Sgjelinek } 34291300Sgjelinek } 34301300Sgjelinek } 34311300Sgjelinek 34321300Sgjelinek return ((err == Z_OK) ? Z_OK : Z_ERR); 34331300Sgjelinek } 34341300Sgjelinek 3435*1507Sgjelinek static int 3436*1507Sgjelinek detach_func(int argc, char *argv[]) 3437*1507Sgjelinek { 3438*1507Sgjelinek int lockfd; 3439*1507Sgjelinek int err, arg; 3440*1507Sgjelinek char zonepath[MAXPATHLEN]; 3441*1507Sgjelinek zone_dochandle_t handle; 3442*1507Sgjelinek 3443*1507Sgjelinek if (zonecfg_in_alt_root()) { 3444*1507Sgjelinek zerror(gettext("cannot detach zone in alternate root")); 3445*1507Sgjelinek return (Z_ERR); 3446*1507Sgjelinek } 3447*1507Sgjelinek 3448*1507Sgjelinek optind = 0; 3449*1507Sgjelinek if ((arg = getopt(argc, argv, "?")) != EOF) { 3450*1507Sgjelinek switch (arg) { 3451*1507Sgjelinek case '?': 3452*1507Sgjelinek sub_usage(SHELP_DETACH, CMD_DETACH); 3453*1507Sgjelinek return (optopt == '?' ? Z_OK : Z_USAGE); 3454*1507Sgjelinek default: 3455*1507Sgjelinek sub_usage(SHELP_DETACH, CMD_DETACH); 3456*1507Sgjelinek return (Z_USAGE); 3457*1507Sgjelinek } 3458*1507Sgjelinek } 3459*1507Sgjelinek if (sanity_check(target_zone, CMD_DETACH, B_FALSE, B_TRUE) != Z_OK) 3460*1507Sgjelinek return (Z_ERR); 3461*1507Sgjelinek if (verify_details(CMD_DETACH) != Z_OK) 3462*1507Sgjelinek return (Z_ERR); 3463*1507Sgjelinek 3464*1507Sgjelinek if ((err = zone_get_zonepath(target_zone, zonepath, sizeof (zonepath))) 3465*1507Sgjelinek != Z_OK) { 3466*1507Sgjelinek errno = err; 3467*1507Sgjelinek zperror2(target_zone, gettext("could not get zone path")); 3468*1507Sgjelinek return (Z_ERR); 3469*1507Sgjelinek } 3470*1507Sgjelinek 3471*1507Sgjelinek /* Don't detach the zone if anything is still mounted there */ 3472*1507Sgjelinek if (zonecfg_find_mounts(zonepath, NULL, NULL)) { 3473*1507Sgjelinek zerror(gettext("These file-systems are mounted on " 3474*1507Sgjelinek "subdirectories of %s.\n"), zonepath); 3475*1507Sgjelinek (void) zonecfg_find_mounts(zonepath, zfm_print, NULL); 3476*1507Sgjelinek return (Z_ERR); 3477*1507Sgjelinek } 3478*1507Sgjelinek 3479*1507Sgjelinek if ((handle = zonecfg_init_handle()) == NULL) { 3480*1507Sgjelinek zperror(cmd_to_str(CMD_DETACH), B_TRUE); 3481*1507Sgjelinek return (Z_ERR); 3482*1507Sgjelinek } 3483*1507Sgjelinek 3484*1507Sgjelinek if ((err = zonecfg_get_handle(target_zone, handle)) != Z_OK) { 3485*1507Sgjelinek errno = err; 3486*1507Sgjelinek zperror(cmd_to_str(CMD_DETACH), B_TRUE); 3487*1507Sgjelinek zonecfg_fini_handle(handle); 3488*1507Sgjelinek return (Z_ERR); 3489*1507Sgjelinek } 3490*1507Sgjelinek 3491*1507Sgjelinek if (grab_lock_file(target_zone, &lockfd) != Z_OK) { 3492*1507Sgjelinek zerror(gettext("another %s may have an operation in progress."), 3493*1507Sgjelinek "zoneadm"); 3494*1507Sgjelinek zonecfg_fini_handle(handle); 3495*1507Sgjelinek return (Z_ERR); 3496*1507Sgjelinek } 3497*1507Sgjelinek 3498*1507Sgjelinek if ((err = zonecfg_get_detach_info(handle, B_TRUE)) != Z_OK) { 3499*1507Sgjelinek errno = err; 3500*1507Sgjelinek zperror(gettext("getting the detach information failed"), 3501*1507Sgjelinek B_TRUE); 3502*1507Sgjelinek goto done; 3503*1507Sgjelinek } 3504*1507Sgjelinek 3505*1507Sgjelinek if ((err = zonecfg_detach_save(handle)) != Z_OK) { 3506*1507Sgjelinek errno = err; 3507*1507Sgjelinek zperror(gettext("saving the detach manifest failed"), B_TRUE); 3508*1507Sgjelinek goto done; 3509*1507Sgjelinek } 3510*1507Sgjelinek 3511*1507Sgjelinek if ((err = zone_set_state(target_zone, ZONE_STATE_CONFIGURED)) 3512*1507Sgjelinek != Z_OK) { 3513*1507Sgjelinek errno = err; 3514*1507Sgjelinek zperror(gettext("could not reset state"), B_TRUE); 3515*1507Sgjelinek } 3516*1507Sgjelinek 3517*1507Sgjelinek done: 3518*1507Sgjelinek zonecfg_fini_handle(handle); 3519*1507Sgjelinek release_lock_file(lockfd); 3520*1507Sgjelinek 3521*1507Sgjelinek return ((err == Z_OK) ? Z_OK : Z_ERR); 3522*1507Sgjelinek } 3523*1507Sgjelinek 3524*1507Sgjelinek /* 3525*1507Sgjelinek * Find the specified package in the sw inventory on the handle and check 3526*1507Sgjelinek * if the version matches what is passed in. 3527*1507Sgjelinek * Return 0 if the packages match 3528*1507Sgjelinek * 1 if the package is found but we have a version mismatch 3529*1507Sgjelinek * -1 if the package is not found 3530*1507Sgjelinek */ 3531*1507Sgjelinek static int 3532*1507Sgjelinek pkg_cmp(zone_dochandle_t handle, char *pkg_name, char *pkg_vers, 3533*1507Sgjelinek char *return_vers, int vers_size) 3534*1507Sgjelinek { 3535*1507Sgjelinek int res = -1; 3536*1507Sgjelinek struct zone_pkgtab pkgtab; 3537*1507Sgjelinek 3538*1507Sgjelinek if (zonecfg_setpkgent(handle) != Z_OK) { 3539*1507Sgjelinek (void) fprintf(stderr, 3540*1507Sgjelinek gettext("unable to enumerate packages\n")); 3541*1507Sgjelinek return (Z_ERR); 3542*1507Sgjelinek } 3543*1507Sgjelinek 3544*1507Sgjelinek while (zonecfg_getpkgent(handle, &pkgtab) == Z_OK) { 3545*1507Sgjelinek if (strcmp(pkg_name, pkgtab.zone_pkg_name) != 0) 3546*1507Sgjelinek continue; 3547*1507Sgjelinek 3548*1507Sgjelinek if (strcmp(pkg_vers, pkgtab.zone_pkg_version) == 0) { 3549*1507Sgjelinek res = 0; 3550*1507Sgjelinek break; 3551*1507Sgjelinek } 3552*1507Sgjelinek 3553*1507Sgjelinek (void) strlcpy(return_vers, pkgtab.zone_pkg_version, vers_size); 3554*1507Sgjelinek res = 1; 3555*1507Sgjelinek break; 3556*1507Sgjelinek } 3557*1507Sgjelinek 3558*1507Sgjelinek (void) zonecfg_endpkgent(handle); 3559*1507Sgjelinek return (res); 3560*1507Sgjelinek } 3561*1507Sgjelinek 3562*1507Sgjelinek /* 3563*1507Sgjelinek * Used in software comparisons to check the packages between the two zone 3564*1507Sgjelinek * handles. The packages have to match or we print a message telling the 3565*1507Sgjelinek * user what is out of sync. The src_cmp flag tells us if the first handle 3566*1507Sgjelinek * is the source machine global zone or not. This is used to enable the 3567*1507Sgjelinek * right messages to be printed and also to enable extra version checking 3568*1507Sgjelinek * that is not needed for the opposite comparison. 3569*1507Sgjelinek */ 3570*1507Sgjelinek static int 3571*1507Sgjelinek pkg_check(char *header, zone_dochandle_t handle1, zone_dochandle_t handle2, 3572*1507Sgjelinek boolean_t src_cmp) 3573*1507Sgjelinek { 3574*1507Sgjelinek int err; 3575*1507Sgjelinek int res = Z_OK; 3576*1507Sgjelinek boolean_t do_header = B_TRUE; 3577*1507Sgjelinek char other_vers[ZONE_PKG_VERSMAX]; 3578*1507Sgjelinek struct zone_pkgtab pkgtab; 3579*1507Sgjelinek 3580*1507Sgjelinek if (zonecfg_setpkgent(handle1) != Z_OK) { 3581*1507Sgjelinek (void) fprintf(stderr, 3582*1507Sgjelinek gettext("unable to enumerate packages\n")); 3583*1507Sgjelinek return (Z_ERR); 3584*1507Sgjelinek } 3585*1507Sgjelinek 3586*1507Sgjelinek while (zonecfg_getpkgent(handle1, &pkgtab) == Z_OK) { 3587*1507Sgjelinek if ((err = pkg_cmp(handle2, pkgtab.zone_pkg_name, 3588*1507Sgjelinek pkgtab.zone_pkg_version, other_vers, sizeof (other_vers))) 3589*1507Sgjelinek != 0) { 3590*1507Sgjelinek if (do_header && (err < 0 || src_cmp)) { 3591*1507Sgjelinek /* LINTED E_SEC_PRINTF_VAR_FMT */ 3592*1507Sgjelinek (void) fprintf(stderr, header); 3593*1507Sgjelinek do_header = B_FALSE; 3594*1507Sgjelinek } 3595*1507Sgjelinek if (err < 0) { 3596*1507Sgjelinek (void) fprintf(stderr, 3597*1507Sgjelinek (src_cmp == B_TRUE) ? 3598*1507Sgjelinek gettext("\t%s: not installed\n\t\t(%s)\n") : 3599*1507Sgjelinek gettext("\t%s (%s)\n"), 3600*1507Sgjelinek pkgtab.zone_pkg_name, 3601*1507Sgjelinek pkgtab.zone_pkg_version); 3602*1507Sgjelinek res = Z_ERR; 3603*1507Sgjelinek } else if (src_cmp) { 3604*1507Sgjelinek (void) fprintf(stderr, gettext( 3605*1507Sgjelinek "\t%s: version mismatch\n\t\t(%s)" 3606*1507Sgjelinek "\n\t\t(%s)\n"), 3607*1507Sgjelinek pkgtab.zone_pkg_name, 3608*1507Sgjelinek pkgtab.zone_pkg_version, other_vers); 3609*1507Sgjelinek res = Z_ERR; 3610*1507Sgjelinek } 3611*1507Sgjelinek } 3612*1507Sgjelinek } 3613*1507Sgjelinek 3614*1507Sgjelinek (void) zonecfg_endpkgent(handle1); 3615*1507Sgjelinek 3616*1507Sgjelinek return (res); 3617*1507Sgjelinek } 3618*1507Sgjelinek 3619*1507Sgjelinek /* 3620*1507Sgjelinek * Find the specified patch in the sw inventory on the handle and check 3621*1507Sgjelinek * if the version matches what is passed in. 3622*1507Sgjelinek * Return 0 if the patches match 3623*1507Sgjelinek * 1 if the patches is found but we have a version mismatch 3624*1507Sgjelinek * -1 if the patches is not found 3625*1507Sgjelinek */ 3626*1507Sgjelinek static int 3627*1507Sgjelinek patch_cmp(zone_dochandle_t handle, char *patch_id, char *patch_vers, 3628*1507Sgjelinek char *return_vers, int vers_size) 3629*1507Sgjelinek { 3630*1507Sgjelinek int res = -1; 3631*1507Sgjelinek struct zone_patchtab patchtab; 3632*1507Sgjelinek 3633*1507Sgjelinek if (zonecfg_setpatchent(handle) != Z_OK) { 3634*1507Sgjelinek (void) fprintf(stderr, 3635*1507Sgjelinek gettext("unable to enumerate patches\n")); 3636*1507Sgjelinek return (Z_ERR); 3637*1507Sgjelinek } 3638*1507Sgjelinek 3639*1507Sgjelinek while (zonecfg_getpatchent(handle, &patchtab) == Z_OK) { 3640*1507Sgjelinek char *p; 3641*1507Sgjelinek 3642*1507Sgjelinek if ((p = strchr(patchtab.zone_patch_id, '-')) != NULL) 3643*1507Sgjelinek *p++ = '\0'; 3644*1507Sgjelinek else 3645*1507Sgjelinek p = ""; 3646*1507Sgjelinek 3647*1507Sgjelinek if (strcmp(patch_id, patchtab.zone_patch_id) != 0) 3648*1507Sgjelinek continue; 3649*1507Sgjelinek 3650*1507Sgjelinek if (strcmp(patch_vers, p) == 0) { 3651*1507Sgjelinek res = 0; 3652*1507Sgjelinek break; 3653*1507Sgjelinek } 3654*1507Sgjelinek 3655*1507Sgjelinek (void) strlcpy(return_vers, p, vers_size); 3656*1507Sgjelinek /* 3657*1507Sgjelinek * Keep checking. This handles the case where multiple 3658*1507Sgjelinek * versions of the same patch is installed. 3659*1507Sgjelinek */ 3660*1507Sgjelinek res = 1; 3661*1507Sgjelinek } 3662*1507Sgjelinek 3663*1507Sgjelinek (void) zonecfg_endpatchent(handle); 3664*1507Sgjelinek return (res); 3665*1507Sgjelinek } 3666*1507Sgjelinek 3667*1507Sgjelinek /* 3668*1507Sgjelinek * Used in software comparisons to check the patches between the two zone 3669*1507Sgjelinek * handles. The patches have to match or we print a message telling the 3670*1507Sgjelinek * user what is out of sync. The src_cmp flag tells us if the first handle 3671*1507Sgjelinek * is the source machine global zone or not. This is used to enable the 3672*1507Sgjelinek * right messages to be printed and also to enable extra version checking 3673*1507Sgjelinek * that is not needed for the opposite comparison. 3674*1507Sgjelinek */ 3675*1507Sgjelinek static int 3676*1507Sgjelinek patch_check(char *header, zone_dochandle_t handle1, zone_dochandle_t handle2, 3677*1507Sgjelinek boolean_t src_cmp) 3678*1507Sgjelinek { 3679*1507Sgjelinek int err; 3680*1507Sgjelinek int res = Z_OK; 3681*1507Sgjelinek boolean_t do_header = B_TRUE; 3682*1507Sgjelinek char other_vers[MAXNAMELEN]; 3683*1507Sgjelinek struct zone_patchtab patchtab; 3684*1507Sgjelinek 3685*1507Sgjelinek if (zonecfg_setpatchent(handle1) != Z_OK) { 3686*1507Sgjelinek (void) fprintf(stderr, 3687*1507Sgjelinek gettext("unable to enumerate patches\n")); 3688*1507Sgjelinek return (Z_ERR); 3689*1507Sgjelinek } 3690*1507Sgjelinek 3691*1507Sgjelinek while (zonecfg_getpatchent(handle1, &patchtab) == Z_OK) { 3692*1507Sgjelinek char *patch_vers; 3693*1507Sgjelinek 3694*1507Sgjelinek if ((patch_vers = strchr(patchtab.zone_patch_id, '-')) != NULL) 3695*1507Sgjelinek *patch_vers++ = '\0'; 3696*1507Sgjelinek else 3697*1507Sgjelinek patch_vers = ""; 3698*1507Sgjelinek 3699*1507Sgjelinek if ((err = patch_cmp(handle2, patchtab.zone_patch_id, 3700*1507Sgjelinek patch_vers, other_vers, sizeof (other_vers))) != 0) { 3701*1507Sgjelinek if (do_header && (err < 0 || src_cmp)) { 3702*1507Sgjelinek /* LINTED E_SEC_PRINTF_VAR_FMT */ 3703*1507Sgjelinek (void) fprintf(stderr, header); 3704*1507Sgjelinek do_header = B_FALSE; 3705*1507Sgjelinek } 3706*1507Sgjelinek if (err < 0) { 3707*1507Sgjelinek (void) fprintf(stderr, 3708*1507Sgjelinek (src_cmp == B_TRUE) ? 3709*1507Sgjelinek gettext("\t%s: not installed\n") : 3710*1507Sgjelinek gettext("\t%s\n"), 3711*1507Sgjelinek patchtab.zone_patch_id); 3712*1507Sgjelinek res = Z_ERR; 3713*1507Sgjelinek } else if (src_cmp) { 3714*1507Sgjelinek (void) fprintf(stderr, 3715*1507Sgjelinek gettext("\t%s: version mismatch\n\t\t(%s) " 3716*1507Sgjelinek "(%s)\n"), patchtab.zone_patch_id, 3717*1507Sgjelinek patch_vers, other_vers); 3718*1507Sgjelinek res = Z_ERR; 3719*1507Sgjelinek } 3720*1507Sgjelinek } 3721*1507Sgjelinek } 3722*1507Sgjelinek 3723*1507Sgjelinek (void) zonecfg_endpatchent(handle1); 3724*1507Sgjelinek 3725*1507Sgjelinek return (res); 3726*1507Sgjelinek } 3727*1507Sgjelinek 3728*1507Sgjelinek /* 3729*1507Sgjelinek * Compare the software on the local global zone and source system global 3730*1507Sgjelinek * zone. Used when we are trying to attach a zone during migration. 3731*1507Sgjelinek * l_handle is for the local system and s_handle is for the source system. 3732*1507Sgjelinek * These have a snapshot of the appropriate packages and patches in the global 3733*1507Sgjelinek * zone for the two machines. 3734*1507Sgjelinek * The functions called here will print any messages that are needed to 3735*1507Sgjelinek * inform the user about package or patch problems. 3736*1507Sgjelinek */ 3737*1507Sgjelinek static int 3738*1507Sgjelinek sw_cmp(zone_dochandle_t l_handle, zone_dochandle_t s_handle) 3739*1507Sgjelinek { 3740*1507Sgjelinek char *hdr; 3741*1507Sgjelinek int res = Z_OK; 3742*1507Sgjelinek 3743*1507Sgjelinek /* 3744*1507Sgjelinek * Check the source host for pkgs (and versions) that are not on the 3745*1507Sgjelinek * local host. 3746*1507Sgjelinek */ 3747*1507Sgjelinek hdr = gettext("These packages installed on the source system are " 3748*1507Sgjelinek "inconsistent with this system:\n"); 3749*1507Sgjelinek if (pkg_check(hdr, s_handle, l_handle, B_TRUE) != Z_OK) 3750*1507Sgjelinek res = Z_ERR; 3751*1507Sgjelinek 3752*1507Sgjelinek /* 3753*1507Sgjelinek * Now check the local host for pkgs that were not on the source host. 3754*1507Sgjelinek * We already handled version mismatches in the loop above. 3755*1507Sgjelinek */ 3756*1507Sgjelinek hdr = gettext("These packages installed on this system were " 3757*1507Sgjelinek "not installed on the source system:\n"); 3758*1507Sgjelinek if (pkg_check(hdr, l_handle, s_handle, B_FALSE) != Z_OK) 3759*1507Sgjelinek res = Z_ERR; 3760*1507Sgjelinek 3761*1507Sgjelinek /* 3762*1507Sgjelinek * Check the source host for patches that are not on the local host. 3763*1507Sgjelinek */ 3764*1507Sgjelinek hdr = gettext("These patches installed on the source system are " 3765*1507Sgjelinek "inconsistent with this system:\n"); 3766*1507Sgjelinek if (patch_check(hdr, s_handle, l_handle, B_TRUE) != Z_OK) 3767*1507Sgjelinek res = Z_ERR; 3768*1507Sgjelinek 3769*1507Sgjelinek /* 3770*1507Sgjelinek * Check the local host for patches that were not on the source host. 3771*1507Sgjelinek * We already handled version mismatches in the loop above. 3772*1507Sgjelinek */ 3773*1507Sgjelinek hdr = gettext("These patches installed on this system were " 3774*1507Sgjelinek "not installed on the source system:\n"); 3775*1507Sgjelinek if (patch_check(hdr, l_handle, s_handle, B_FALSE) != Z_OK) 3776*1507Sgjelinek res = Z_ERR; 3777*1507Sgjelinek 3778*1507Sgjelinek return (res); 3779*1507Sgjelinek } 3780*1507Sgjelinek 3781*1507Sgjelinek /* 3782*1507Sgjelinek * During attach we go through and fix up the /dev entries for the zone 3783*1507Sgjelinek * we are attaching. In order to regenerate /dev with the correct devices, 3784*1507Sgjelinek * the old /dev will be removed, the zone readied (which generates a new 3785*1507Sgjelinek * /dev) then halted, then we use the info from the manifest to update 3786*1507Sgjelinek * the modes, owners, etc. on the new /dev. 3787*1507Sgjelinek */ 3788*1507Sgjelinek static int 3789*1507Sgjelinek dev_fix(zone_dochandle_t handle) 3790*1507Sgjelinek { 3791*1507Sgjelinek int res; 3792*1507Sgjelinek int err; 3793*1507Sgjelinek int status; 3794*1507Sgjelinek struct zone_devpermtab devtab; 3795*1507Sgjelinek zone_cmd_arg_t zarg; 3796*1507Sgjelinek char devpath[MAXPATHLEN]; 3797*1507Sgjelinek /* 6: "exec " and " " */ 3798*1507Sgjelinek char cmdbuf[sizeof (RMCOMMAND) + MAXPATHLEN + 6]; 3799*1507Sgjelinek 3800*1507Sgjelinek if ((res = zonecfg_get_zonepath(handle, devpath, sizeof (devpath))) 3801*1507Sgjelinek != Z_OK) 3802*1507Sgjelinek return (res); 3803*1507Sgjelinek 3804*1507Sgjelinek if (strlcat(devpath, "/dev", sizeof (devpath)) >= sizeof (devpath)) 3805*1507Sgjelinek return (Z_TOO_BIG); 3806*1507Sgjelinek 3807*1507Sgjelinek /* 3808*1507Sgjelinek * "exec" the command so that the returned status is that of 3809*1507Sgjelinek * RMCOMMAND and not the shell. 3810*1507Sgjelinek */ 3811*1507Sgjelinek (void) snprintf(cmdbuf, sizeof (cmdbuf), "exec " RMCOMMAND " %s", 3812*1507Sgjelinek devpath); 3813*1507Sgjelinek status = do_subproc(cmdbuf); 3814*1507Sgjelinek if ((err = subproc_status(RMCOMMAND, status)) != Z_OK) { 3815*1507Sgjelinek (void) fprintf(stderr, 3816*1507Sgjelinek gettext("could not remove existing /dev\n")); 3817*1507Sgjelinek return (Z_ERR); 3818*1507Sgjelinek } 3819*1507Sgjelinek 3820*1507Sgjelinek /* In order to ready the zone, it must be in the installed state */ 3821*1507Sgjelinek if ((err = zone_set_state(target_zone, ZONE_STATE_INSTALLED)) != Z_OK) { 3822*1507Sgjelinek errno = err; 3823*1507Sgjelinek zperror(gettext("could not reset state"), B_TRUE); 3824*1507Sgjelinek return (Z_ERR); 3825*1507Sgjelinek } 3826*1507Sgjelinek 3827*1507Sgjelinek /* We have to ready the zone to regen the dev tree */ 3828*1507Sgjelinek zarg.cmd = Z_READY; 3829*1507Sgjelinek if (call_zoneadmd(target_zone, &zarg) != 0) { 3830*1507Sgjelinek zerror(gettext("call to %s failed"), "zoneadmd"); 3831*1507Sgjelinek return (Z_ERR); 3832*1507Sgjelinek } 3833*1507Sgjelinek 3834*1507Sgjelinek zarg.cmd = Z_HALT; 3835*1507Sgjelinek if (call_zoneadmd(target_zone, &zarg) != 0) { 3836*1507Sgjelinek zerror(gettext("call to %s failed"), "zoneadmd"); 3837*1507Sgjelinek return (Z_ERR); 3838*1507Sgjelinek } 3839*1507Sgjelinek 3840*1507Sgjelinek if (zonecfg_setdevperment(handle) != Z_OK) { 3841*1507Sgjelinek (void) fprintf(stderr, 3842*1507Sgjelinek gettext("unable to enumerate device entries\n")); 3843*1507Sgjelinek return (Z_ERR); 3844*1507Sgjelinek } 3845*1507Sgjelinek 3846*1507Sgjelinek while (zonecfg_getdevperment(handle, &devtab) == Z_OK) { 3847*1507Sgjelinek int err; 3848*1507Sgjelinek 3849*1507Sgjelinek if ((err = zonecfg_devperms_apply(handle, 3850*1507Sgjelinek devtab.zone_devperm_name, devtab.zone_devperm_uid, 3851*1507Sgjelinek devtab.zone_devperm_gid, devtab.zone_devperm_mode, 3852*1507Sgjelinek devtab.zone_devperm_acl)) != Z_OK && err != Z_INVAL) 3853*1507Sgjelinek (void) fprintf(stderr, gettext("error updating device " 3854*1507Sgjelinek "%s: %s\n"), devtab.zone_devperm_name, 3855*1507Sgjelinek zonecfg_strerror(err)); 3856*1507Sgjelinek 3857*1507Sgjelinek free(devtab.zone_devperm_acl); 3858*1507Sgjelinek } 3859*1507Sgjelinek 3860*1507Sgjelinek (void) zonecfg_enddevperment(handle); 3861*1507Sgjelinek 3862*1507Sgjelinek return (Z_OK); 3863*1507Sgjelinek } 3864*1507Sgjelinek 3865*1507Sgjelinek static int 3866*1507Sgjelinek attach_func(int argc, char *argv[]) 3867*1507Sgjelinek { 3868*1507Sgjelinek int lockfd; 3869*1507Sgjelinek int err, arg; 3870*1507Sgjelinek boolean_t force = B_FALSE; 3871*1507Sgjelinek zone_dochandle_t handle; 3872*1507Sgjelinek zone_dochandle_t athandle = NULL; 3873*1507Sgjelinek char zonepath[MAXPATHLEN]; 3874*1507Sgjelinek 3875*1507Sgjelinek if (zonecfg_in_alt_root()) { 3876*1507Sgjelinek zerror(gettext("cannot attach zone in alternate root")); 3877*1507Sgjelinek return (Z_ERR); 3878*1507Sgjelinek } 3879*1507Sgjelinek 3880*1507Sgjelinek optind = 0; 3881*1507Sgjelinek if ((arg = getopt(argc, argv, "?F")) != EOF) { 3882*1507Sgjelinek switch (arg) { 3883*1507Sgjelinek case '?': 3884*1507Sgjelinek sub_usage(SHELP_ATTACH, CMD_ATTACH); 3885*1507Sgjelinek return (optopt == '?' ? Z_OK : Z_USAGE); 3886*1507Sgjelinek case 'F': 3887*1507Sgjelinek force = B_TRUE; 3888*1507Sgjelinek break; 3889*1507Sgjelinek default: 3890*1507Sgjelinek sub_usage(SHELP_ATTACH, CMD_ATTACH); 3891*1507Sgjelinek return (Z_USAGE); 3892*1507Sgjelinek } 3893*1507Sgjelinek } 3894*1507Sgjelinek if (sanity_check(target_zone, CMD_ATTACH, B_FALSE, B_TRUE) != Z_OK) 3895*1507Sgjelinek return (Z_ERR); 3896*1507Sgjelinek if (verify_details(CMD_ATTACH) != Z_OK) 3897*1507Sgjelinek return (Z_ERR); 3898*1507Sgjelinek 3899*1507Sgjelinek if ((err = zone_get_zonepath(target_zone, zonepath, sizeof (zonepath))) 3900*1507Sgjelinek != Z_OK) { 3901*1507Sgjelinek errno = err; 3902*1507Sgjelinek zperror2(target_zone, gettext("could not get zone path")); 3903*1507Sgjelinek return (Z_ERR); 3904*1507Sgjelinek } 3905*1507Sgjelinek 3906*1507Sgjelinek if ((handle = zonecfg_init_handle()) == NULL) { 3907*1507Sgjelinek zperror(cmd_to_str(CMD_ATTACH), B_TRUE); 3908*1507Sgjelinek return (Z_ERR); 3909*1507Sgjelinek } 3910*1507Sgjelinek 3911*1507Sgjelinek if ((err = zonecfg_get_handle(target_zone, handle)) != Z_OK) { 3912*1507Sgjelinek errno = err; 3913*1507Sgjelinek zperror(cmd_to_str(CMD_ATTACH), B_TRUE); 3914*1507Sgjelinek zonecfg_fini_handle(handle); 3915*1507Sgjelinek return (Z_ERR); 3916*1507Sgjelinek } 3917*1507Sgjelinek 3918*1507Sgjelinek if (grab_lock_file(target_zone, &lockfd) != Z_OK) { 3919*1507Sgjelinek zerror(gettext("another %s may have an operation in progress."), 3920*1507Sgjelinek "zoneadm"); 3921*1507Sgjelinek zonecfg_fini_handle(handle); 3922*1507Sgjelinek return (Z_ERR); 3923*1507Sgjelinek } 3924*1507Sgjelinek 3925*1507Sgjelinek if (force) 3926*1507Sgjelinek goto forced; 3927*1507Sgjelinek 3928*1507Sgjelinek if ((athandle = zonecfg_init_handle()) == NULL) { 3929*1507Sgjelinek zperror(cmd_to_str(CMD_ATTACH), B_TRUE); 3930*1507Sgjelinek goto done; 3931*1507Sgjelinek } 3932*1507Sgjelinek 3933*1507Sgjelinek if ((err = zonecfg_get_attach_handle(zonepath, target_zone, B_TRUE, 3934*1507Sgjelinek athandle)) != Z_OK) { 3935*1507Sgjelinek if (err == Z_NO_ZONE) 3936*1507Sgjelinek zerror(gettext("Not a detached zone")); 3937*1507Sgjelinek else if (err == Z_INVALID_DOCUMENT) 3938*1507Sgjelinek zerror(gettext("Cannot attach to an earlier release " 3939*1507Sgjelinek "of the operating system")); 3940*1507Sgjelinek else 3941*1507Sgjelinek zperror(cmd_to_str(CMD_ATTACH), B_TRUE); 3942*1507Sgjelinek goto done; 3943*1507Sgjelinek } 3944*1507Sgjelinek 3945*1507Sgjelinek /* Get the detach information for the locally defined zone. */ 3946*1507Sgjelinek if ((err = zonecfg_get_detach_info(handle, B_FALSE)) != Z_OK) { 3947*1507Sgjelinek errno = err; 3948*1507Sgjelinek zperror(gettext("getting the attach information failed"), 3949*1507Sgjelinek B_TRUE); 3950*1507Sgjelinek goto done; 3951*1507Sgjelinek } 3952*1507Sgjelinek 3953*1507Sgjelinek /* sw_cmp prints error msgs as necessary */ 3954*1507Sgjelinek if ((err = sw_cmp(handle, athandle)) != Z_OK) 3955*1507Sgjelinek goto done; 3956*1507Sgjelinek 3957*1507Sgjelinek if ((err = dev_fix(athandle)) != Z_OK) 3958*1507Sgjelinek goto done; 3959*1507Sgjelinek 3960*1507Sgjelinek forced: 3961*1507Sgjelinek 3962*1507Sgjelinek zonecfg_rm_detached(handle, force); 3963*1507Sgjelinek 3964*1507Sgjelinek if ((err = zone_set_state(target_zone, ZONE_STATE_INSTALLED)) != Z_OK) { 3965*1507Sgjelinek errno = err; 3966*1507Sgjelinek zperror(gettext("could not reset state"), B_TRUE); 3967*1507Sgjelinek } 3968*1507Sgjelinek 3969*1507Sgjelinek done: 3970*1507Sgjelinek zonecfg_fini_handle(handle); 3971*1507Sgjelinek release_lock_file(lockfd); 3972*1507Sgjelinek if (athandle != NULL) 3973*1507Sgjelinek zonecfg_fini_handle(athandle); 3974*1507Sgjelinek 3975*1507Sgjelinek return ((err == Z_OK) ? Z_OK : Z_ERR); 3976*1507Sgjelinek } 3977*1507Sgjelinek 39781300Sgjelinek /* 39790Sstevel@tonic-gate * On input, TRUE => yes, FALSE => no. 39800Sstevel@tonic-gate * On return, TRUE => 1, FALSE => 0, could not ask => -1. 39810Sstevel@tonic-gate */ 39820Sstevel@tonic-gate 39830Sstevel@tonic-gate static int 39840Sstevel@tonic-gate ask_yesno(boolean_t default_answer, const char *question) 39850Sstevel@tonic-gate { 39860Sstevel@tonic-gate char line[64]; /* should be large enough to answer yes or no */ 39870Sstevel@tonic-gate 39880Sstevel@tonic-gate if (!isatty(STDIN_FILENO)) 39890Sstevel@tonic-gate return (-1); 39900Sstevel@tonic-gate for (;;) { 39910Sstevel@tonic-gate (void) printf("%s (%s)? ", question, 39920Sstevel@tonic-gate default_answer ? "[y]/n" : "y/[n]"); 39930Sstevel@tonic-gate if (fgets(line, sizeof (line), stdin) == NULL || 39940Sstevel@tonic-gate line[0] == '\n') 39950Sstevel@tonic-gate return (default_answer ? 1 : 0); 39960Sstevel@tonic-gate if (tolower(line[0]) == 'y') 39970Sstevel@tonic-gate return (1); 39980Sstevel@tonic-gate if (tolower(line[0]) == 'n') 39990Sstevel@tonic-gate return (0); 40000Sstevel@tonic-gate } 40010Sstevel@tonic-gate } 40020Sstevel@tonic-gate 40030Sstevel@tonic-gate static int 40040Sstevel@tonic-gate uninstall_func(int argc, char *argv[]) 40050Sstevel@tonic-gate { 40060Sstevel@tonic-gate /* 6: "exec " and " " */ 40070Sstevel@tonic-gate char cmdbuf[sizeof (RMCOMMAND) + MAXPATHLEN + 6]; 40080Sstevel@tonic-gate char line[ZONENAME_MAX + 128]; /* Enough for "Are you sure ..." */ 40090Sstevel@tonic-gate char rootpath[MAXPATHLEN], devpath[MAXPATHLEN]; 40100Sstevel@tonic-gate boolean_t force = B_FALSE; 40110Sstevel@tonic-gate int lockfd, answer; 40120Sstevel@tonic-gate int err, arg; 40130Sstevel@tonic-gate int status; 40140Sstevel@tonic-gate 4015766Scarlsonj if (zonecfg_in_alt_root()) { 4016766Scarlsonj zerror(gettext("cannot uninstall zone in alternate root")); 4017766Scarlsonj return (Z_ERR); 4018766Scarlsonj } 4019766Scarlsonj 40200Sstevel@tonic-gate optind = 0; 40210Sstevel@tonic-gate while ((arg = getopt(argc, argv, "?F")) != EOF) { 40220Sstevel@tonic-gate switch (arg) { 40230Sstevel@tonic-gate case '?': 40240Sstevel@tonic-gate sub_usage(SHELP_UNINSTALL, CMD_UNINSTALL); 40250Sstevel@tonic-gate return (optopt == '?' ? Z_OK : Z_USAGE); 40260Sstevel@tonic-gate case 'F': 40270Sstevel@tonic-gate force = B_TRUE; 40280Sstevel@tonic-gate break; 40290Sstevel@tonic-gate default: 40300Sstevel@tonic-gate sub_usage(SHELP_UNINSTALL, CMD_UNINSTALL); 40310Sstevel@tonic-gate return (Z_USAGE); 40320Sstevel@tonic-gate } 40330Sstevel@tonic-gate } 40340Sstevel@tonic-gate if (argc > optind) { 40350Sstevel@tonic-gate sub_usage(SHELP_UNINSTALL, CMD_UNINSTALL); 40360Sstevel@tonic-gate return (Z_USAGE); 40370Sstevel@tonic-gate } 40380Sstevel@tonic-gate 40390Sstevel@tonic-gate if (sanity_check(target_zone, CMD_UNINSTALL, B_FALSE, B_TRUE) != Z_OK) 40400Sstevel@tonic-gate return (Z_ERR); 40410Sstevel@tonic-gate 40420Sstevel@tonic-gate if (!force) { 40430Sstevel@tonic-gate (void) snprintf(line, sizeof (line), 40440Sstevel@tonic-gate gettext("Are you sure you want to %s zone %s"), 40450Sstevel@tonic-gate cmd_to_str(CMD_UNINSTALL), target_zone); 40460Sstevel@tonic-gate if ((answer = ask_yesno(B_FALSE, line)) == 0) { 40470Sstevel@tonic-gate return (Z_OK); 40480Sstevel@tonic-gate } else if (answer == -1) { 40490Sstevel@tonic-gate zerror(gettext("Input not from terminal and -F " 40500Sstevel@tonic-gate "not specified: %s not done."), 40510Sstevel@tonic-gate cmd_to_str(CMD_UNINSTALL)); 40520Sstevel@tonic-gate return (Z_ERR); 40530Sstevel@tonic-gate } 40540Sstevel@tonic-gate } 40550Sstevel@tonic-gate 40560Sstevel@tonic-gate if ((err = zone_get_zonepath(target_zone, devpath, 40570Sstevel@tonic-gate sizeof (devpath))) != Z_OK) { 40580Sstevel@tonic-gate errno = err; 40590Sstevel@tonic-gate zperror2(target_zone, gettext("could not get zone path")); 40600Sstevel@tonic-gate return (Z_ERR); 40610Sstevel@tonic-gate } 40620Sstevel@tonic-gate (void) strlcat(devpath, "/dev", sizeof (devpath)); 40630Sstevel@tonic-gate if ((err = zone_get_rootpath(target_zone, rootpath, 40640Sstevel@tonic-gate sizeof (rootpath))) != Z_OK) { 40650Sstevel@tonic-gate errno = err; 40660Sstevel@tonic-gate zperror2(target_zone, gettext("could not get root path")); 40670Sstevel@tonic-gate return (Z_ERR); 40680Sstevel@tonic-gate } 40690Sstevel@tonic-gate 40700Sstevel@tonic-gate /* 40710Sstevel@tonic-gate * If there seems to be a zoneadmd running for this zone, call it 40720Sstevel@tonic-gate * to tell it that an uninstall is happening; if all goes well it 40730Sstevel@tonic-gate * will then shut itself down. 40740Sstevel@tonic-gate */ 40750Sstevel@tonic-gate if (ping_zoneadmd(target_zone) == Z_OK) { 40760Sstevel@tonic-gate zone_cmd_arg_t zarg; 40770Sstevel@tonic-gate zarg.cmd = Z_NOTE_UNINSTALLING; 40780Sstevel@tonic-gate /* we don't care too much if this fails... just plow on */ 40790Sstevel@tonic-gate (void) call_zoneadmd(target_zone, &zarg); 40800Sstevel@tonic-gate } 40810Sstevel@tonic-gate 40820Sstevel@tonic-gate if (grab_lock_file(target_zone, &lockfd) != Z_OK) { 40830Sstevel@tonic-gate zerror(gettext("another %s may have an operation in progress."), 40841300Sgjelinek "zoneadm"); 40850Sstevel@tonic-gate return (Z_ERR); 40860Sstevel@tonic-gate } 40870Sstevel@tonic-gate 40880Sstevel@tonic-gate /* Don't uninstall the zone if anything is mounted there */ 40890Sstevel@tonic-gate err = zonecfg_find_mounts(rootpath, NULL, NULL); 40900Sstevel@tonic-gate if (err) { 40910Sstevel@tonic-gate zerror(gettext("These file-systems are mounted on " 40920Sstevel@tonic-gate "subdirectories of %s.\n"), rootpath); 40930Sstevel@tonic-gate (void) zonecfg_find_mounts(rootpath, zfm_print, NULL); 40940Sstevel@tonic-gate return (Z_ERR); 40950Sstevel@tonic-gate } 40960Sstevel@tonic-gate 40970Sstevel@tonic-gate err = zone_set_state(target_zone, ZONE_STATE_INCOMPLETE); 40980Sstevel@tonic-gate if (err != Z_OK) { 40990Sstevel@tonic-gate errno = err; 41000Sstevel@tonic-gate zperror2(target_zone, gettext("could not set state")); 41010Sstevel@tonic-gate goto bad; 41020Sstevel@tonic-gate } 41030Sstevel@tonic-gate 41040Sstevel@tonic-gate /* 41050Sstevel@tonic-gate * "exec" the command so that the returned status is that of 41060Sstevel@tonic-gate * RMCOMMAND and not the shell. 41070Sstevel@tonic-gate */ 41080Sstevel@tonic-gate (void) snprintf(cmdbuf, sizeof (cmdbuf), "exec " RMCOMMAND " %s", 41090Sstevel@tonic-gate devpath); 41100Sstevel@tonic-gate status = do_subproc(cmdbuf); 41110Sstevel@tonic-gate if ((err = subproc_status(RMCOMMAND, status)) != Z_OK) 41120Sstevel@tonic-gate goto bad; 41130Sstevel@tonic-gate (void) snprintf(cmdbuf, sizeof (cmdbuf), "exec " RMCOMMAND " %s", 41140Sstevel@tonic-gate rootpath); 41150Sstevel@tonic-gate status = do_subproc(cmdbuf); 41160Sstevel@tonic-gate if ((err = subproc_status(RMCOMMAND, status)) != Z_OK) 41170Sstevel@tonic-gate goto bad; 41180Sstevel@tonic-gate err = zone_set_state(target_zone, ZONE_STATE_CONFIGURED); 41190Sstevel@tonic-gate if (err != Z_OK) { 41200Sstevel@tonic-gate errno = err; 41210Sstevel@tonic-gate zperror2(target_zone, gettext("could not reset state")); 41220Sstevel@tonic-gate } 41230Sstevel@tonic-gate bad: 41240Sstevel@tonic-gate release_lock_file(lockfd); 41250Sstevel@tonic-gate return (err); 41260Sstevel@tonic-gate } 41270Sstevel@tonic-gate 4128766Scarlsonj /* ARGSUSED */ 4129766Scarlsonj static int 4130766Scarlsonj mount_func(int argc, char *argv[]) 4131766Scarlsonj { 4132766Scarlsonj zone_cmd_arg_t zarg; 4133766Scarlsonj 4134766Scarlsonj if (argc > 0) 4135766Scarlsonj return (Z_USAGE); 4136766Scarlsonj if (sanity_check(target_zone, CMD_MOUNT, B_FALSE, B_FALSE) != Z_OK) 4137766Scarlsonj return (Z_ERR); 4138766Scarlsonj if (verify_details(CMD_MOUNT) != Z_OK) 4139766Scarlsonj return (Z_ERR); 4140766Scarlsonj 4141766Scarlsonj zarg.cmd = Z_MOUNT; 4142766Scarlsonj if (call_zoneadmd(target_zone, &zarg) != 0) { 4143766Scarlsonj zerror(gettext("call to %s failed"), "zoneadmd"); 4144766Scarlsonj return (Z_ERR); 4145766Scarlsonj } 4146766Scarlsonj return (Z_OK); 4147766Scarlsonj } 4148766Scarlsonj 4149766Scarlsonj /* ARGSUSED */ 4150766Scarlsonj static int 4151766Scarlsonj unmount_func(int argc, char *argv[]) 4152766Scarlsonj { 4153766Scarlsonj zone_cmd_arg_t zarg; 4154766Scarlsonj 4155766Scarlsonj if (argc > 0) 4156766Scarlsonj return (Z_USAGE); 4157766Scarlsonj if (sanity_check(target_zone, CMD_UNMOUNT, B_FALSE, B_FALSE) != Z_OK) 4158766Scarlsonj return (Z_ERR); 4159766Scarlsonj 4160766Scarlsonj zarg.cmd = Z_UNMOUNT; 4161766Scarlsonj if (call_zoneadmd(target_zone, &zarg) != 0) { 4162766Scarlsonj zerror(gettext("call to %s failed"), "zoneadmd"); 4163766Scarlsonj return (Z_ERR); 4164766Scarlsonj } 4165766Scarlsonj return (Z_OK); 4166766Scarlsonj } 4167766Scarlsonj 41680Sstevel@tonic-gate static int 41690Sstevel@tonic-gate help_func(int argc, char *argv[]) 41700Sstevel@tonic-gate { 41710Sstevel@tonic-gate int arg, cmd_num; 41720Sstevel@tonic-gate 41730Sstevel@tonic-gate if (argc == 0) { 41740Sstevel@tonic-gate (void) usage(B_TRUE); 41750Sstevel@tonic-gate return (Z_OK); 41760Sstevel@tonic-gate } 41770Sstevel@tonic-gate optind = 0; 41780Sstevel@tonic-gate if ((arg = getopt(argc, argv, "?")) != EOF) { 41790Sstevel@tonic-gate switch (arg) { 41800Sstevel@tonic-gate case '?': 41810Sstevel@tonic-gate sub_usage(SHELP_HELP, CMD_HELP); 41820Sstevel@tonic-gate return (optopt == '?' ? Z_OK : Z_USAGE); 41830Sstevel@tonic-gate default: 41840Sstevel@tonic-gate sub_usage(SHELP_HELP, CMD_HELP); 41850Sstevel@tonic-gate return (Z_USAGE); 41860Sstevel@tonic-gate } 41870Sstevel@tonic-gate } 41880Sstevel@tonic-gate while (optind < argc) { 4189988Scarlsonj /* Private commands have NULL short_usage; omit them */ 4190988Scarlsonj if ((cmd_num = cmd_match(argv[optind])) < 0 || 4191988Scarlsonj cmdtab[cmd_num].short_usage == NULL) { 41920Sstevel@tonic-gate sub_usage(SHELP_HELP, CMD_HELP); 41930Sstevel@tonic-gate return (Z_USAGE); 41940Sstevel@tonic-gate } 41950Sstevel@tonic-gate sub_usage(cmdtab[cmd_num].short_usage, cmd_num); 41960Sstevel@tonic-gate optind++; 41970Sstevel@tonic-gate } 41980Sstevel@tonic-gate return (Z_OK); 41990Sstevel@tonic-gate } 42000Sstevel@tonic-gate 42010Sstevel@tonic-gate /* 42020Sstevel@tonic-gate * Returns: CMD_MIN thru CMD_MAX on success, -1 on error 42030Sstevel@tonic-gate */ 42040Sstevel@tonic-gate 42050Sstevel@tonic-gate static int 42060Sstevel@tonic-gate cmd_match(char *cmd) 42070Sstevel@tonic-gate { 42080Sstevel@tonic-gate int i; 42090Sstevel@tonic-gate 42100Sstevel@tonic-gate for (i = CMD_MIN; i <= CMD_MAX; i++) { 42110Sstevel@tonic-gate /* return only if there is an exact match */ 42120Sstevel@tonic-gate if (strcmp(cmd, cmdtab[i].cmd_name) == 0) 42130Sstevel@tonic-gate return (cmdtab[i].cmd_num); 42140Sstevel@tonic-gate } 42150Sstevel@tonic-gate return (-1); 42160Sstevel@tonic-gate } 42170Sstevel@tonic-gate 42180Sstevel@tonic-gate static int 42190Sstevel@tonic-gate parse_and_run(int argc, char *argv[]) 42200Sstevel@tonic-gate { 42210Sstevel@tonic-gate int i = cmd_match(argv[0]); 42220Sstevel@tonic-gate 42230Sstevel@tonic-gate if (i < 0) 42240Sstevel@tonic-gate return (usage(B_FALSE)); 42250Sstevel@tonic-gate return (cmdtab[i].handler(argc - 1, &(argv[1]))); 42260Sstevel@tonic-gate } 42270Sstevel@tonic-gate 42280Sstevel@tonic-gate static char * 42290Sstevel@tonic-gate get_execbasename(char *execfullname) 42300Sstevel@tonic-gate { 42310Sstevel@tonic-gate char *last_slash, *execbasename; 42320Sstevel@tonic-gate 42330Sstevel@tonic-gate /* guard against '/' at end of command invocation */ 42340Sstevel@tonic-gate for (;;) { 42350Sstevel@tonic-gate last_slash = strrchr(execfullname, '/'); 42360Sstevel@tonic-gate if (last_slash == NULL) { 42370Sstevel@tonic-gate execbasename = execfullname; 42380Sstevel@tonic-gate break; 42390Sstevel@tonic-gate } else { 42400Sstevel@tonic-gate execbasename = last_slash + 1; 42410Sstevel@tonic-gate if (*execbasename == '\0') { 42420Sstevel@tonic-gate *last_slash = '\0'; 42430Sstevel@tonic-gate continue; 42440Sstevel@tonic-gate } 42450Sstevel@tonic-gate break; 42460Sstevel@tonic-gate } 42470Sstevel@tonic-gate } 42480Sstevel@tonic-gate return (execbasename); 42490Sstevel@tonic-gate } 42500Sstevel@tonic-gate 42510Sstevel@tonic-gate int 42520Sstevel@tonic-gate main(int argc, char **argv) 42530Sstevel@tonic-gate { 42540Sstevel@tonic-gate int arg; 42550Sstevel@tonic-gate zoneid_t zid; 4256766Scarlsonj struct stat st; 42570Sstevel@tonic-gate 42580Sstevel@tonic-gate if ((locale = setlocale(LC_ALL, "")) == NULL) 42590Sstevel@tonic-gate locale = "C"; 42600Sstevel@tonic-gate (void) textdomain(TEXT_DOMAIN); 42610Sstevel@tonic-gate setbuf(stdout, NULL); 42620Sstevel@tonic-gate (void) sigset(SIGHUP, SIG_IGN); 42630Sstevel@tonic-gate execname = get_execbasename(argv[0]); 42640Sstevel@tonic-gate target_zone = NULL; 42650Sstevel@tonic-gate if (chdir("/") != 0) { 42660Sstevel@tonic-gate zerror(gettext("could not change directory to /.")); 42670Sstevel@tonic-gate exit(Z_ERR); 42680Sstevel@tonic-gate } 42690Sstevel@tonic-gate 4270766Scarlsonj while ((arg = getopt(argc, argv, "?z:R:")) != EOF) { 42710Sstevel@tonic-gate switch (arg) { 42720Sstevel@tonic-gate case '?': 42730Sstevel@tonic-gate return (usage(B_TRUE)); 42740Sstevel@tonic-gate case 'z': 42750Sstevel@tonic-gate target_zone = optarg; 42760Sstevel@tonic-gate break; 4277766Scarlsonj case 'R': /* private option for admin/install use */ 4278766Scarlsonj if (*optarg != '/') { 4279766Scarlsonj zerror(gettext("root path must be absolute.")); 4280766Scarlsonj exit(Z_ERR); 4281766Scarlsonj } 4282766Scarlsonj if (stat(optarg, &st) == -1 || !S_ISDIR(st.st_mode)) { 4283766Scarlsonj zerror( 4284766Scarlsonj gettext("root path must be a directory.")); 4285766Scarlsonj exit(Z_ERR); 4286766Scarlsonj } 4287766Scarlsonj zonecfg_set_root(optarg); 4288766Scarlsonj break; 42890Sstevel@tonic-gate default: 42900Sstevel@tonic-gate return (usage(B_FALSE)); 42910Sstevel@tonic-gate } 42920Sstevel@tonic-gate } 42930Sstevel@tonic-gate 42940Sstevel@tonic-gate if (optind >= argc) 42950Sstevel@tonic-gate return (usage(B_FALSE)); 42960Sstevel@tonic-gate if (target_zone != NULL && zone_get_id(target_zone, &zid) != 0) { 42970Sstevel@tonic-gate errno = Z_NO_ZONE; 42980Sstevel@tonic-gate zperror(target_zone, B_TRUE); 42990Sstevel@tonic-gate exit(Z_ERR); 43000Sstevel@tonic-gate } 43010Sstevel@tonic-gate return (parse_and_run(argc - optind, &argv[optind])); 43020Sstevel@tonic-gate } 4303