17c478bd9Sstevel@tonic-gate /* 27c478bd9Sstevel@tonic-gate * CDDL HEADER START 37c478bd9Sstevel@tonic-gate * 47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 57c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 67c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 77c478bd9Sstevel@tonic-gate * with the License. 87c478bd9Sstevel@tonic-gate * 97c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 107c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 117c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 127c478bd9Sstevel@tonic-gate * and limitations under the License. 137c478bd9Sstevel@tonic-gate * 147c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 157c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 167c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 177c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 187c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 197c478bd9Sstevel@tonic-gate * 207c478bd9Sstevel@tonic-gate * CDDL HEADER END 217c478bd9Sstevel@tonic-gate */ 227e362f58Scomay 237c478bd9Sstevel@tonic-gate /* 24865e09a4Sgjelinek * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 257c478bd9Sstevel@tonic-gate * Use is subject to license terms. 267c478bd9Sstevel@tonic-gate */ 277c478bd9Sstevel@tonic-gate 287c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 297c478bd9Sstevel@tonic-gate 307c478bd9Sstevel@tonic-gate /* 317c478bd9Sstevel@tonic-gate * zoneadm is a command interpreter for zone administration. It is all in 327c478bd9Sstevel@tonic-gate * C (i.e., no lex/yacc), and all the argument passing is argc/argv based. 337c478bd9Sstevel@tonic-gate * main() calls parse_and_run() which calls cmd_match(), then invokes the 347c478bd9Sstevel@tonic-gate * appropriate command's handler function. The rest of the program is the 357c478bd9Sstevel@tonic-gate * handler functions and their helper functions. 367c478bd9Sstevel@tonic-gate * 377c478bd9Sstevel@tonic-gate * Some of the helper functions are used largely to simplify I18N: reducing 387c478bd9Sstevel@tonic-gate * the need for translation notes. This is particularly true of many of 397c478bd9Sstevel@tonic-gate * the zerror() calls: doing e.g. zerror(gettext("%s failed"), "foo") rather 407c478bd9Sstevel@tonic-gate * than zerror(gettext("foo failed")) with a translation note indicating 417c478bd9Sstevel@tonic-gate * that "foo" need not be translated. 427c478bd9Sstevel@tonic-gate */ 437c478bd9Sstevel@tonic-gate 447c478bd9Sstevel@tonic-gate #include <stdio.h> 457c478bd9Sstevel@tonic-gate #include <errno.h> 467c478bd9Sstevel@tonic-gate #include <unistd.h> 477c478bd9Sstevel@tonic-gate #include <signal.h> 487c478bd9Sstevel@tonic-gate #include <stdarg.h> 497c478bd9Sstevel@tonic-gate #include <ctype.h> 507c478bd9Sstevel@tonic-gate #include <stdlib.h> 517c478bd9Sstevel@tonic-gate #include <string.h> 527c478bd9Sstevel@tonic-gate #include <wait.h> 537c478bd9Sstevel@tonic-gate #include <zone.h> 547c478bd9Sstevel@tonic-gate #include <priv.h> 557c478bd9Sstevel@tonic-gate #include <locale.h> 567c478bd9Sstevel@tonic-gate #include <libintl.h> 577c478bd9Sstevel@tonic-gate #include <libzonecfg.h> 587c478bd9Sstevel@tonic-gate #include <bsm/adt.h> 597c478bd9Sstevel@tonic-gate #include <sys/utsname.h> 607c478bd9Sstevel@tonic-gate #include <sys/param.h> 617c478bd9Sstevel@tonic-gate #include <sys/types.h> 627c478bd9Sstevel@tonic-gate #include <sys/stat.h> 637c478bd9Sstevel@tonic-gate #include <sys/statvfs.h> 647c478bd9Sstevel@tonic-gate #include <assert.h> 657c478bd9Sstevel@tonic-gate #include <sys/sockio.h> 667c478bd9Sstevel@tonic-gate #include <sys/mntent.h> 677c478bd9Sstevel@tonic-gate #include <limits.h> 68fa9e4066Sahrens #include <libzfs.h> 697c478bd9Sstevel@tonic-gate 707c478bd9Sstevel@tonic-gate #include <fcntl.h> 717c478bd9Sstevel@tonic-gate #include <door.h> 727c478bd9Sstevel@tonic-gate #include <macros.h> 737c478bd9Sstevel@tonic-gate #include <libgen.h> 74865e09a4Sgjelinek #include <fnmatch.h> 757c478bd9Sstevel@tonic-gate 767c478bd9Sstevel@tonic-gate #include <pool.h> 777c478bd9Sstevel@tonic-gate #include <sys/pool.h> 787c478bd9Sstevel@tonic-gate 797c478bd9Sstevel@tonic-gate #define MAXARGS 8 807c478bd9Sstevel@tonic-gate 817c478bd9Sstevel@tonic-gate /* Reflects kernel zone entries */ 827c478bd9Sstevel@tonic-gate typedef struct zone_entry { 837c478bd9Sstevel@tonic-gate zoneid_t zid; 847c478bd9Sstevel@tonic-gate char zname[ZONENAME_MAX]; 857c478bd9Sstevel@tonic-gate char *zstate_str; 867c478bd9Sstevel@tonic-gate zone_state_t zstate_num; 877c478bd9Sstevel@tonic-gate char zroot[MAXPATHLEN]; 887c478bd9Sstevel@tonic-gate } zone_entry_t; 897c478bd9Sstevel@tonic-gate 907c478bd9Sstevel@tonic-gate static zone_entry_t *zents; 917c478bd9Sstevel@tonic-gate static size_t nzents; 927c478bd9Sstevel@tonic-gate 937c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN) /* should be defined by cc -D */ 947c478bd9Sstevel@tonic-gate #define TEXT_DOMAIN "SYS_TEST" /* Use this only if it wasn't */ 957c478bd9Sstevel@tonic-gate #endif 967c478bd9Sstevel@tonic-gate 977c478bd9Sstevel@tonic-gate #define Z_ERR 1 987c478bd9Sstevel@tonic-gate #define Z_USAGE 2 997c478bd9Sstevel@tonic-gate 1007c478bd9Sstevel@tonic-gate /* 0755 is the default directory mode. */ 1017c478bd9Sstevel@tonic-gate #define DEFAULT_DIR_MODE \ 1027c478bd9Sstevel@tonic-gate (S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) 1037c478bd9Sstevel@tonic-gate 1047c478bd9Sstevel@tonic-gate #define CMD_HELP 0 1057c478bd9Sstevel@tonic-gate #define CMD_BOOT 1 1067c478bd9Sstevel@tonic-gate #define CMD_HALT 2 1077c478bd9Sstevel@tonic-gate #define CMD_READY 3 1087c478bd9Sstevel@tonic-gate #define CMD_REBOOT 4 1097c478bd9Sstevel@tonic-gate #define CMD_LIST 5 1107c478bd9Sstevel@tonic-gate #define CMD_VERIFY 6 1117c478bd9Sstevel@tonic-gate #define CMD_INSTALL 7 1127c478bd9Sstevel@tonic-gate #define CMD_UNINSTALL 8 113108322fbScarlsonj #define CMD_MOUNT 9 114108322fbScarlsonj #define CMD_UNMOUNT 10 115865e09a4Sgjelinek #define CMD_CLONE 11 116865e09a4Sgjelinek #define CMD_MOVE 12 1177c478bd9Sstevel@tonic-gate 1187c478bd9Sstevel@tonic-gate #define CMD_MIN CMD_HELP 119865e09a4Sgjelinek #define CMD_MAX CMD_MOVE 120865e09a4Sgjelinek 121865e09a4Sgjelinek #define SINGLE_USER_RETRY 30 1227c478bd9Sstevel@tonic-gate 1237c478bd9Sstevel@tonic-gate struct cmd { 1247c478bd9Sstevel@tonic-gate uint_t cmd_num; /* command number */ 1257c478bd9Sstevel@tonic-gate char *cmd_name; /* command name */ 1267c478bd9Sstevel@tonic-gate char *short_usage; /* short form help */ 1277c478bd9Sstevel@tonic-gate int (*handler)(int argc, char *argv[]); /* function to call */ 1287c478bd9Sstevel@tonic-gate 1297c478bd9Sstevel@tonic-gate }; 1307c478bd9Sstevel@tonic-gate 1317c478bd9Sstevel@tonic-gate #define SHELP_HELP "help" 1327c478bd9Sstevel@tonic-gate #define SHELP_BOOT "boot [-s]" 1337c478bd9Sstevel@tonic-gate #define SHELP_HALT "halt" 1347c478bd9Sstevel@tonic-gate #define SHELP_READY "ready" 1357c478bd9Sstevel@tonic-gate #define SHELP_REBOOT "reboot" 1367c478bd9Sstevel@tonic-gate #define SHELP_LIST "list [-cipv]" 1377c478bd9Sstevel@tonic-gate #define SHELP_VERIFY "verify" 1387c478bd9Sstevel@tonic-gate #define SHELP_INSTALL "install" 1397c478bd9Sstevel@tonic-gate #define SHELP_UNINSTALL "uninstall [-F]" 140865e09a4Sgjelinek #define SHELP_CLONE "clone [-m method] zonename" 141865e09a4Sgjelinek #define SHELP_MOVE "move zonepath" 1427c478bd9Sstevel@tonic-gate 1437c478bd9Sstevel@tonic-gate static int help_func(int argc, char *argv[]); 1447c478bd9Sstevel@tonic-gate static int ready_func(int argc, char *argv[]); 1457c478bd9Sstevel@tonic-gate static int boot_func(int argc, char *argv[]); 1467c478bd9Sstevel@tonic-gate static int halt_func(int argc, char *argv[]); 1477c478bd9Sstevel@tonic-gate static int reboot_func(int argc, char *argv[]); 1487c478bd9Sstevel@tonic-gate static int list_func(int argc, char *argv[]); 1497c478bd9Sstevel@tonic-gate static int verify_func(int argc, char *argv[]); 1507c478bd9Sstevel@tonic-gate static int install_func(int argc, char *argv[]); 1517c478bd9Sstevel@tonic-gate static int uninstall_func(int argc, char *argv[]); 152108322fbScarlsonj static int mount_func(int argc, char *argv[]); 153108322fbScarlsonj static int unmount_func(int argc, char *argv[]); 154865e09a4Sgjelinek static int clone_func(int argc, char *argv[]); 155865e09a4Sgjelinek static int move_func(int argc, char *argv[]); 1567c478bd9Sstevel@tonic-gate static int sanity_check(char *zone, int cmd_num, boolean_t running, 1577c478bd9Sstevel@tonic-gate boolean_t unsafe_when_running); 1587c478bd9Sstevel@tonic-gate static int cmd_match(char *cmd); 1597c478bd9Sstevel@tonic-gate static int verify_details(int); 1607c478bd9Sstevel@tonic-gate 1617c478bd9Sstevel@tonic-gate static struct cmd cmdtab[] = { 1627c478bd9Sstevel@tonic-gate { CMD_HELP, "help", SHELP_HELP, help_func }, 1637c478bd9Sstevel@tonic-gate { CMD_BOOT, "boot", SHELP_BOOT, boot_func }, 1647c478bd9Sstevel@tonic-gate { CMD_HALT, "halt", SHELP_HALT, halt_func }, 1657c478bd9Sstevel@tonic-gate { CMD_READY, "ready", SHELP_READY, ready_func }, 1667c478bd9Sstevel@tonic-gate { CMD_REBOOT, "reboot", SHELP_REBOOT, reboot_func }, 1677c478bd9Sstevel@tonic-gate { CMD_LIST, "list", SHELP_LIST, list_func }, 1687c478bd9Sstevel@tonic-gate { CMD_VERIFY, "verify", SHELP_VERIFY, verify_func }, 1697c478bd9Sstevel@tonic-gate { CMD_INSTALL, "install", SHELP_INSTALL, install_func }, 1707c478bd9Sstevel@tonic-gate { CMD_UNINSTALL, "uninstall", SHELP_UNINSTALL, 171108322fbScarlsonj uninstall_func }, 172865e09a4Sgjelinek /* mount and unmount are private commands for admin/install */ 173108322fbScarlsonj { CMD_MOUNT, "mount", NULL, mount_func }, 174865e09a4Sgjelinek { CMD_UNMOUNT, "unmount", NULL, unmount_func }, 175865e09a4Sgjelinek { CMD_CLONE, "clone", SHELP_CLONE, clone_func }, 176865e09a4Sgjelinek { CMD_MOVE, "move", SHELP_MOVE, move_func } 1777c478bd9Sstevel@tonic-gate }; 1787c478bd9Sstevel@tonic-gate 1797c478bd9Sstevel@tonic-gate /* global variables */ 1807c478bd9Sstevel@tonic-gate 1817c478bd9Sstevel@tonic-gate /* set early in main(), never modified thereafter, used all over the place */ 1827c478bd9Sstevel@tonic-gate static char *execname; 1837c478bd9Sstevel@tonic-gate static char *target_zone; 1847c478bd9Sstevel@tonic-gate static char *locale; 1857c478bd9Sstevel@tonic-gate 1867c478bd9Sstevel@tonic-gate /* used in do_subproc() and signal handler */ 1877c478bd9Sstevel@tonic-gate static volatile boolean_t child_killed; 1887c478bd9Sstevel@tonic-gate 1897c478bd9Sstevel@tonic-gate static char * 1907c478bd9Sstevel@tonic-gate cmd_to_str(int cmd_num) 1917c478bd9Sstevel@tonic-gate { 1927c478bd9Sstevel@tonic-gate assert(cmd_num >= CMD_MIN && cmd_num <= CMD_MAX); 1937c478bd9Sstevel@tonic-gate return (cmdtab[cmd_num].cmd_name); 1947c478bd9Sstevel@tonic-gate } 1957c478bd9Sstevel@tonic-gate 1967c478bd9Sstevel@tonic-gate /* This is a separate function because of gettext() wrapping. */ 1977c478bd9Sstevel@tonic-gate static char * 1987c478bd9Sstevel@tonic-gate long_help(int cmd_num) 1997c478bd9Sstevel@tonic-gate { 2007e362f58Scomay assert(cmd_num >= CMD_MIN && cmd_num <= CMD_MAX); 2017c478bd9Sstevel@tonic-gate switch (cmd_num) { 2027c478bd9Sstevel@tonic-gate case CMD_HELP: 2037c478bd9Sstevel@tonic-gate return (gettext("Print usage message.")); 2047c478bd9Sstevel@tonic-gate case CMD_BOOT: 2057c478bd9Sstevel@tonic-gate return (gettext("Activates (boots) specified zone. " 2067c478bd9Sstevel@tonic-gate "The -s flag can be used\n\tto boot the zone in " 2077c478bd9Sstevel@tonic-gate "the single-user state.")); 2087c478bd9Sstevel@tonic-gate case CMD_HALT: 2097c478bd9Sstevel@tonic-gate return (gettext("Halts specified zone, bypassing " 2107c478bd9Sstevel@tonic-gate "shutdown scripts and removing runtime\n\t" 2117c478bd9Sstevel@tonic-gate "resources of the zone.")); 2127c478bd9Sstevel@tonic-gate case CMD_READY: 2137c478bd9Sstevel@tonic-gate return (gettext("Prepares a zone for running " 2147c478bd9Sstevel@tonic-gate "applications but does not start any user\n\t" 2157c478bd9Sstevel@tonic-gate "processes in the zone.")); 2167c478bd9Sstevel@tonic-gate case CMD_REBOOT: 2177c478bd9Sstevel@tonic-gate return (gettext("Restarts the zone (equivalent to a " 2187c478bd9Sstevel@tonic-gate "halt / boot sequence).\n\tFails if the zone is " 2197c478bd9Sstevel@tonic-gate "not active.")); 2207c478bd9Sstevel@tonic-gate case CMD_LIST: 2217c478bd9Sstevel@tonic-gate return (gettext("Lists the current zones, or a " 2227c478bd9Sstevel@tonic-gate "specific zone if indicated. By default,\n\tall " 2237c478bd9Sstevel@tonic-gate "running zones are listed, though this can be " 2247c478bd9Sstevel@tonic-gate "expanded to all\n\tinstalled zones with the -i " 2257c478bd9Sstevel@tonic-gate "option or all configured zones with the\n\t-c " 2267c478bd9Sstevel@tonic-gate "option. When used with the general -z <zone> " 2277c478bd9Sstevel@tonic-gate "option, lists only the\n\tspecified zone, but " 2287c478bd9Sstevel@tonic-gate "lists it regardless of its state, and the -i " 2297c478bd9Sstevel@tonic-gate "and -c\n\toptions are disallowed. The -v option " 2307c478bd9Sstevel@tonic-gate "can be used to display verbose\n\tinformation: " 2317c478bd9Sstevel@tonic-gate "zone name, id, current state, root directory and " 2327c478bd9Sstevel@tonic-gate "options.\n\tThe -p option can be used to request " 2337c478bd9Sstevel@tonic-gate "machine-parsable output. The -v\n\tand -p " 2347c478bd9Sstevel@tonic-gate "options are mutually exclusive. If neither -v " 2357c478bd9Sstevel@tonic-gate "nor -p is used,\n\tjust the zone name is " 2367c478bd9Sstevel@tonic-gate "listed.")); 2377c478bd9Sstevel@tonic-gate case CMD_VERIFY: 2387c478bd9Sstevel@tonic-gate return (gettext("Check to make sure the configuration " 2397c478bd9Sstevel@tonic-gate "can safely be instantiated\n\ton the machine: " 2407c478bd9Sstevel@tonic-gate "physical network interfaces exist, etc.")); 2417c478bd9Sstevel@tonic-gate case CMD_INSTALL: 2427c478bd9Sstevel@tonic-gate return (gettext("Install the configuration on to the " 2437c478bd9Sstevel@tonic-gate "system.")); 2447c478bd9Sstevel@tonic-gate case CMD_UNINSTALL: 2457c478bd9Sstevel@tonic-gate return (gettext("Uninstall the configuration from the " 2467c478bd9Sstevel@tonic-gate "system. The -F flag can be used\n\tto force the " 2477c478bd9Sstevel@tonic-gate "action.")); 248865e09a4Sgjelinek case CMD_CLONE: 249865e09a4Sgjelinek return (gettext("Clone the installation of another " 250865e09a4Sgjelinek "zone.")); 251865e09a4Sgjelinek case CMD_MOVE: 252865e09a4Sgjelinek return (gettext("Move the zone to a new zonepath.")); 253108322fbScarlsonj default: 254108322fbScarlsonj return (""); 2557c478bd9Sstevel@tonic-gate } 2567c478bd9Sstevel@tonic-gate /* NOTREACHED */ 2577e362f58Scomay return (NULL); 2587c478bd9Sstevel@tonic-gate } 2597c478bd9Sstevel@tonic-gate 2607c478bd9Sstevel@tonic-gate /* 2617c478bd9Sstevel@tonic-gate * Called with explicit B_TRUE when help is explicitly requested, B_FALSE for 2627c478bd9Sstevel@tonic-gate * unexpected errors. 2637c478bd9Sstevel@tonic-gate */ 2647c478bd9Sstevel@tonic-gate 2657c478bd9Sstevel@tonic-gate static int 2667c478bd9Sstevel@tonic-gate usage(boolean_t explicit) 2677c478bd9Sstevel@tonic-gate { 2687c478bd9Sstevel@tonic-gate int i; 2697c478bd9Sstevel@tonic-gate FILE *fd = explicit ? stdout : stderr; 2707c478bd9Sstevel@tonic-gate 2717c478bd9Sstevel@tonic-gate (void) fprintf(fd, "%s:\t%s help\n", gettext("usage"), execname); 2727c478bd9Sstevel@tonic-gate (void) fprintf(fd, "\t%s [-z <zone>] list\n", execname); 2737c478bd9Sstevel@tonic-gate (void) fprintf(fd, "\t%s -z <zone> <%s>\n", execname, 2747c478bd9Sstevel@tonic-gate gettext("subcommand")); 2757c478bd9Sstevel@tonic-gate (void) fprintf(fd, "\n%s:\n\n", gettext("Subcommands")); 2767c478bd9Sstevel@tonic-gate for (i = CMD_MIN; i <= CMD_MAX; i++) { 277108322fbScarlsonj if (cmdtab[i].short_usage == NULL) 278108322fbScarlsonj continue; 2797c478bd9Sstevel@tonic-gate (void) fprintf(fd, "%s\n", cmdtab[i].short_usage); 2807c478bd9Sstevel@tonic-gate if (explicit) 2817c478bd9Sstevel@tonic-gate (void) fprintf(fd, "\t%s\n\n", long_help(i)); 2827c478bd9Sstevel@tonic-gate } 2837c478bd9Sstevel@tonic-gate if (!explicit) 2847c478bd9Sstevel@tonic-gate (void) fputs("\n", fd); 2857c478bd9Sstevel@tonic-gate return (Z_USAGE); 2867c478bd9Sstevel@tonic-gate } 2877c478bd9Sstevel@tonic-gate 2887c478bd9Sstevel@tonic-gate static void 2897c478bd9Sstevel@tonic-gate sub_usage(char *short_usage, int cmd_num) 2907c478bd9Sstevel@tonic-gate { 2917c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "%s:\t%s\n", gettext("usage"), short_usage); 2927c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "\t%s\n", long_help(cmd_num)); 2937c478bd9Sstevel@tonic-gate } 2947c478bd9Sstevel@tonic-gate 2957c478bd9Sstevel@tonic-gate /* 2967c478bd9Sstevel@tonic-gate * zperror() is like perror(3c) except that this also prints the executable 2977c478bd9Sstevel@tonic-gate * name at the start of the message, and takes a boolean indicating whether 2987c478bd9Sstevel@tonic-gate * to call libc'c strerror() or that from libzonecfg. 2997c478bd9Sstevel@tonic-gate */ 3007c478bd9Sstevel@tonic-gate 3017c478bd9Sstevel@tonic-gate static void 3027c478bd9Sstevel@tonic-gate zperror(const char *str, boolean_t zonecfg_error) 3037c478bd9Sstevel@tonic-gate { 3047c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "%s: %s: %s\n", execname, str, 3057c478bd9Sstevel@tonic-gate zonecfg_error ? zonecfg_strerror(errno) : strerror(errno)); 3067c478bd9Sstevel@tonic-gate } 3077c478bd9Sstevel@tonic-gate 3087c478bd9Sstevel@tonic-gate /* 3097c478bd9Sstevel@tonic-gate * zperror2() is very similar to zperror() above, except it also prints a 3107c478bd9Sstevel@tonic-gate * supplied zone name after the executable. 3117c478bd9Sstevel@tonic-gate * 3127c478bd9Sstevel@tonic-gate * All current consumers of this function want libzonecfg's strerror() rather 3137c478bd9Sstevel@tonic-gate * than libc's; if this ever changes, this function can be made more generic 3147c478bd9Sstevel@tonic-gate * like zperror() above. 3157c478bd9Sstevel@tonic-gate */ 3167c478bd9Sstevel@tonic-gate 3177c478bd9Sstevel@tonic-gate static void 3187c478bd9Sstevel@tonic-gate zperror2(const char *zone, const char *str) 3197c478bd9Sstevel@tonic-gate { 3207c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "%s: %s: %s: %s\n", execname, zone, str, 3217c478bd9Sstevel@tonic-gate zonecfg_strerror(errno)); 3227c478bd9Sstevel@tonic-gate } 3237c478bd9Sstevel@tonic-gate 3247c478bd9Sstevel@tonic-gate /* PRINTFLIKE1 */ 3257c478bd9Sstevel@tonic-gate static void 3267c478bd9Sstevel@tonic-gate zerror(const char *fmt, ...) 3277c478bd9Sstevel@tonic-gate { 3287c478bd9Sstevel@tonic-gate va_list alist; 3297c478bd9Sstevel@tonic-gate 3307c478bd9Sstevel@tonic-gate va_start(alist, fmt); 3317c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "%s: ", execname); 3327c478bd9Sstevel@tonic-gate if (target_zone != NULL) 3337c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "zone '%s': ", target_zone); 3347c478bd9Sstevel@tonic-gate (void) vfprintf(stderr, fmt, alist); 3357c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "\n"); 3367c478bd9Sstevel@tonic-gate va_end(alist); 3377c478bd9Sstevel@tonic-gate } 3387c478bd9Sstevel@tonic-gate 3397c478bd9Sstevel@tonic-gate static void * 3407c478bd9Sstevel@tonic-gate safe_calloc(size_t nelem, size_t elsize) 3417c478bd9Sstevel@tonic-gate { 3427c478bd9Sstevel@tonic-gate void *r = calloc(nelem, elsize); 3437c478bd9Sstevel@tonic-gate 3447c478bd9Sstevel@tonic-gate if (r == NULL) { 3457c478bd9Sstevel@tonic-gate zerror(gettext("failed to allocate %lu bytes: %s"), 3467c478bd9Sstevel@tonic-gate (ulong_t)nelem * elsize, strerror(errno)); 3477c478bd9Sstevel@tonic-gate exit(Z_ERR); 3487c478bd9Sstevel@tonic-gate } 3497c478bd9Sstevel@tonic-gate return (r); 3507c478bd9Sstevel@tonic-gate } 3517c478bd9Sstevel@tonic-gate 3527c478bd9Sstevel@tonic-gate static void 3537c478bd9Sstevel@tonic-gate zone_print(zone_entry_t *zent, boolean_t verbose, boolean_t parsable) 3547c478bd9Sstevel@tonic-gate { 3557c478bd9Sstevel@tonic-gate static boolean_t firsttime = B_TRUE; 3567c478bd9Sstevel@tonic-gate 3577c478bd9Sstevel@tonic-gate assert(!(verbose && parsable)); 3587c478bd9Sstevel@tonic-gate if (firsttime && verbose) { 3597c478bd9Sstevel@tonic-gate firsttime = B_FALSE; 3607c478bd9Sstevel@tonic-gate (void) printf("%*s %-16s %-14s %-30s\n", ZONEID_WIDTH, "ID", 3617c478bd9Sstevel@tonic-gate "NAME", "STATUS", "PATH"); 3627c478bd9Sstevel@tonic-gate } 3637c478bd9Sstevel@tonic-gate if (!verbose) { 3647c478bd9Sstevel@tonic-gate if (!parsable) { 3657c478bd9Sstevel@tonic-gate (void) printf("%s\n", zent->zname); 3667c478bd9Sstevel@tonic-gate return; 3677c478bd9Sstevel@tonic-gate } 3687c478bd9Sstevel@tonic-gate if (zent->zid == ZONE_ID_UNDEFINED) 3697c478bd9Sstevel@tonic-gate (void) printf("-"); 3707c478bd9Sstevel@tonic-gate else 3717c478bd9Sstevel@tonic-gate (void) printf("%lu", zent->zid); 3727c478bd9Sstevel@tonic-gate (void) printf(":%s:%s:%s\n", zent->zname, zent->zstate_str, 3737c478bd9Sstevel@tonic-gate zent->zroot); 3747c478bd9Sstevel@tonic-gate return; 3757c478bd9Sstevel@tonic-gate } 3767c478bd9Sstevel@tonic-gate if (zent->zstate_str != NULL) { 3777c478bd9Sstevel@tonic-gate if (zent->zid == ZONE_ID_UNDEFINED) 3787c478bd9Sstevel@tonic-gate (void) printf("%*s", ZONEID_WIDTH, "-"); 3797c478bd9Sstevel@tonic-gate else 3807c478bd9Sstevel@tonic-gate (void) printf("%*lu", ZONEID_WIDTH, zent->zid); 3817c478bd9Sstevel@tonic-gate (void) printf(" %-16s %-14s %-30s\n", zent->zname, 3827c478bd9Sstevel@tonic-gate zent->zstate_str, zent->zroot); 3837c478bd9Sstevel@tonic-gate } 3847c478bd9Sstevel@tonic-gate } 3857c478bd9Sstevel@tonic-gate 3867c478bd9Sstevel@tonic-gate static int 387108322fbScarlsonj lookup_zone_info(const char *zone_name, zoneid_t zid, zone_entry_t *zent) 3887c478bd9Sstevel@tonic-gate { 3897c478bd9Sstevel@tonic-gate char root[MAXPATHLEN]; 3907c478bd9Sstevel@tonic-gate int err; 3917c478bd9Sstevel@tonic-gate 3927c478bd9Sstevel@tonic-gate (void) strlcpy(zent->zname, zone_name, sizeof (zent->zname)); 3937c478bd9Sstevel@tonic-gate (void) strlcpy(zent->zroot, "???", sizeof (zent->zroot)); 3947c478bd9Sstevel@tonic-gate zent->zstate_str = "???"; 3957c478bd9Sstevel@tonic-gate 396108322fbScarlsonj zent->zid = zid; 3977c478bd9Sstevel@tonic-gate 3987c478bd9Sstevel@tonic-gate if ((err = zone_get_zonepath(zent->zname, root, sizeof (root))) != 3997c478bd9Sstevel@tonic-gate Z_OK) { 4007c478bd9Sstevel@tonic-gate errno = err; 4017c478bd9Sstevel@tonic-gate zperror2(zent->zname, gettext("could not get zone path")); 4027c478bd9Sstevel@tonic-gate return (Z_ERR); 4037c478bd9Sstevel@tonic-gate } 4047c478bd9Sstevel@tonic-gate (void) strlcpy(zent->zroot, root, sizeof (zent->zroot)); 4057c478bd9Sstevel@tonic-gate 4067c478bd9Sstevel@tonic-gate if ((err = zone_get_state(zent->zname, &zent->zstate_num)) != Z_OK) { 4077c478bd9Sstevel@tonic-gate errno = err; 4087c478bd9Sstevel@tonic-gate zperror2(zent->zname, gettext("could not get state")); 4097c478bd9Sstevel@tonic-gate return (Z_ERR); 4107c478bd9Sstevel@tonic-gate } 4117c478bd9Sstevel@tonic-gate zent->zstate_str = zone_state_str(zent->zstate_num); 4127c478bd9Sstevel@tonic-gate 4137c478bd9Sstevel@tonic-gate return (Z_OK); 4147c478bd9Sstevel@tonic-gate } 4157c478bd9Sstevel@tonic-gate 4167c478bd9Sstevel@tonic-gate /* 4177c478bd9Sstevel@tonic-gate * fetch_zents() calls zone_list(2) to find out how many zones are running 4187c478bd9Sstevel@tonic-gate * (which is stored in the global nzents), then calls zone_list(2) again 4197c478bd9Sstevel@tonic-gate * to fetch the list of running zones (stored in the global zents). This 4207c478bd9Sstevel@tonic-gate * function may be called multiple times, so if zents is already set, we 4217c478bd9Sstevel@tonic-gate * return immediately to save work. 4227c478bd9Sstevel@tonic-gate */ 4237c478bd9Sstevel@tonic-gate 4247c478bd9Sstevel@tonic-gate static int 425108322fbScarlsonj fetch_zents(void) 4267c478bd9Sstevel@tonic-gate { 4277c478bd9Sstevel@tonic-gate zoneid_t *zids = NULL; 4287c478bd9Sstevel@tonic-gate uint_t nzents_saved; 429108322fbScarlsonj int i, retv; 430108322fbScarlsonj FILE *fp; 431108322fbScarlsonj boolean_t inaltroot; 432108322fbScarlsonj zone_entry_t *zentp; 4337c478bd9Sstevel@tonic-gate 4347c478bd9Sstevel@tonic-gate if (nzents > 0) 4357c478bd9Sstevel@tonic-gate return (Z_OK); 4367c478bd9Sstevel@tonic-gate 4377c478bd9Sstevel@tonic-gate if (zone_list(NULL, &nzents) != 0) { 4387c478bd9Sstevel@tonic-gate zperror(gettext("failed to get zoneid list"), B_FALSE); 4397c478bd9Sstevel@tonic-gate return (Z_ERR); 4407c478bd9Sstevel@tonic-gate } 4417c478bd9Sstevel@tonic-gate 4427c478bd9Sstevel@tonic-gate again: 4437c478bd9Sstevel@tonic-gate if (nzents == 0) 4447c478bd9Sstevel@tonic-gate return (Z_OK); 4457c478bd9Sstevel@tonic-gate 4467c478bd9Sstevel@tonic-gate zids = safe_calloc(nzents, sizeof (zoneid_t)); 4477c478bd9Sstevel@tonic-gate nzents_saved = nzents; 4487c478bd9Sstevel@tonic-gate 4497c478bd9Sstevel@tonic-gate if (zone_list(zids, &nzents) != 0) { 4507c478bd9Sstevel@tonic-gate zperror(gettext("failed to get zone list"), B_FALSE); 4517c478bd9Sstevel@tonic-gate free(zids); 4527c478bd9Sstevel@tonic-gate return (Z_ERR); 4537c478bd9Sstevel@tonic-gate } 4547c478bd9Sstevel@tonic-gate if (nzents != nzents_saved) { 4557c478bd9Sstevel@tonic-gate /* list changed, try again */ 4567c478bd9Sstevel@tonic-gate free(zids); 4577c478bd9Sstevel@tonic-gate goto again; 4587c478bd9Sstevel@tonic-gate } 4597c478bd9Sstevel@tonic-gate 4607c478bd9Sstevel@tonic-gate zents = safe_calloc(nzents, sizeof (zone_entry_t)); 4617c478bd9Sstevel@tonic-gate 462108322fbScarlsonj inaltroot = zonecfg_in_alt_root(); 463108322fbScarlsonj if (inaltroot) 464108322fbScarlsonj fp = zonecfg_open_scratch("", B_FALSE); 465108322fbScarlsonj else 466108322fbScarlsonj fp = NULL; 467108322fbScarlsonj zentp = zents; 468108322fbScarlsonj retv = Z_OK; 4697c478bd9Sstevel@tonic-gate for (i = 0; i < nzents; i++) { 4707c478bd9Sstevel@tonic-gate char name[ZONENAME_MAX]; 471108322fbScarlsonj char altname[ZONENAME_MAX]; 4727c478bd9Sstevel@tonic-gate 473108322fbScarlsonj if (getzonenamebyid(zids[i], name, sizeof (name)) < 0) { 4747c478bd9Sstevel@tonic-gate zperror(gettext("failed to get zone name"), B_FALSE); 475108322fbScarlsonj retv = Z_ERR; 476108322fbScarlsonj continue; 4777c478bd9Sstevel@tonic-gate } 478108322fbScarlsonj if (zonecfg_is_scratch(name)) { 479108322fbScarlsonj /* Ignore scratch zones by default */ 480108322fbScarlsonj if (!inaltroot) 481108322fbScarlsonj continue; 482108322fbScarlsonj if (fp == NULL || 483108322fbScarlsonj zonecfg_reverse_scratch(fp, name, altname, 484108322fbScarlsonj sizeof (altname), NULL, 0) == -1) { 4855ee84fbdSgjelinek zerror(gettext("could not resolve scratch " 486108322fbScarlsonj "zone %s"), name); 487108322fbScarlsonj retv = Z_ERR; 488108322fbScarlsonj continue; 489108322fbScarlsonj } 490108322fbScarlsonj (void) strcpy(name, altname); 491108322fbScarlsonj } else { 492108322fbScarlsonj /* Ignore non-scratch when in an alternate root */ 493108322fbScarlsonj if (inaltroot && strcmp(name, GLOBAL_ZONENAME) != 0) 494108322fbScarlsonj continue; 495108322fbScarlsonj } 496108322fbScarlsonj if (lookup_zone_info(name, zids[i], zentp) != Z_OK) { 497108322fbScarlsonj zerror(gettext("failed to get zone data")); 498108322fbScarlsonj retv = Z_ERR; 499108322fbScarlsonj continue; 500108322fbScarlsonj } 501108322fbScarlsonj zentp++; 502108322fbScarlsonj } 503108322fbScarlsonj nzents = zentp - zents; 504108322fbScarlsonj if (fp != NULL) 505108322fbScarlsonj zonecfg_close_scratch(fp); 5067c478bd9Sstevel@tonic-gate 5077c478bd9Sstevel@tonic-gate free(zids); 508108322fbScarlsonj return (retv); 5097c478bd9Sstevel@tonic-gate } 5107c478bd9Sstevel@tonic-gate 511108322fbScarlsonj static int 5127c478bd9Sstevel@tonic-gate zone_print_list(zone_state_t min_state, boolean_t verbose, boolean_t parsable) 5137c478bd9Sstevel@tonic-gate { 5147c478bd9Sstevel@tonic-gate int i; 5157c478bd9Sstevel@tonic-gate zone_entry_t zent; 5167c478bd9Sstevel@tonic-gate FILE *cookie; 5177c478bd9Sstevel@tonic-gate char *name; 5187c478bd9Sstevel@tonic-gate 5197c478bd9Sstevel@tonic-gate /* 5207c478bd9Sstevel@tonic-gate * First get the list of running zones from the kernel and print them. 5217c478bd9Sstevel@tonic-gate * If that is all we need, then return. 5227c478bd9Sstevel@tonic-gate */ 523108322fbScarlsonj if ((i = fetch_zents()) != Z_OK) { 5247c478bd9Sstevel@tonic-gate /* 5257c478bd9Sstevel@tonic-gate * No need for error messages; fetch_zents() has already taken 5267c478bd9Sstevel@tonic-gate * care of this. 5277c478bd9Sstevel@tonic-gate */ 528108322fbScarlsonj return (i); 5297c478bd9Sstevel@tonic-gate } 530108322fbScarlsonj for (i = 0; i < nzents; i++) 5317c478bd9Sstevel@tonic-gate zone_print(&zents[i], verbose, parsable); 5327c478bd9Sstevel@tonic-gate if (min_state >= ZONE_STATE_RUNNING) 533108322fbScarlsonj return (Z_OK); 5347c478bd9Sstevel@tonic-gate /* 5357c478bd9Sstevel@tonic-gate * Next, get the full list of zones from the configuration, skipping 5367c478bd9Sstevel@tonic-gate * any we have already printed. 5377c478bd9Sstevel@tonic-gate */ 5387c478bd9Sstevel@tonic-gate cookie = setzoneent(); 5397c478bd9Sstevel@tonic-gate while ((name = getzoneent(cookie)) != NULL) { 5407c478bd9Sstevel@tonic-gate for (i = 0; i < nzents; i++) { 5417c478bd9Sstevel@tonic-gate if (strcmp(zents[i].zname, name) == 0) 5427c478bd9Sstevel@tonic-gate break; 5437c478bd9Sstevel@tonic-gate } 5447c478bd9Sstevel@tonic-gate if (i < nzents) { 5457c478bd9Sstevel@tonic-gate free(name); 5467c478bd9Sstevel@tonic-gate continue; 5477c478bd9Sstevel@tonic-gate } 548108322fbScarlsonj if (lookup_zone_info(name, ZONE_ID_UNDEFINED, &zent) != Z_OK) { 5497c478bd9Sstevel@tonic-gate free(name); 5507c478bd9Sstevel@tonic-gate continue; 5517c478bd9Sstevel@tonic-gate } 5527c478bd9Sstevel@tonic-gate free(name); 5537c478bd9Sstevel@tonic-gate if (zent.zstate_num >= min_state) 5547c478bd9Sstevel@tonic-gate zone_print(&zent, verbose, parsable); 5557c478bd9Sstevel@tonic-gate } 5567c478bd9Sstevel@tonic-gate endzoneent(cookie); 557108322fbScarlsonj return (Z_OK); 5587c478bd9Sstevel@tonic-gate } 5597c478bd9Sstevel@tonic-gate 5607c478bd9Sstevel@tonic-gate static zone_entry_t * 5617c478bd9Sstevel@tonic-gate lookup_running_zone(char *str) 5627c478bd9Sstevel@tonic-gate { 5637c478bd9Sstevel@tonic-gate zoneid_t zoneid; 5647c478bd9Sstevel@tonic-gate char *cp; 5657c478bd9Sstevel@tonic-gate int i; 5667c478bd9Sstevel@tonic-gate 5677c478bd9Sstevel@tonic-gate if (fetch_zents() != Z_OK) 5687c478bd9Sstevel@tonic-gate return (NULL); 5697c478bd9Sstevel@tonic-gate 5707c478bd9Sstevel@tonic-gate for (i = 0; i < nzents; i++) { 5717c478bd9Sstevel@tonic-gate if (strcmp(str, zents[i].zname) == 0) 5727c478bd9Sstevel@tonic-gate return (&zents[i]); 5737c478bd9Sstevel@tonic-gate } 5747c478bd9Sstevel@tonic-gate errno = 0; 5757c478bd9Sstevel@tonic-gate zoneid = strtol(str, &cp, 0); 5767c478bd9Sstevel@tonic-gate if (zoneid < MIN_ZONEID || zoneid > MAX_ZONEID || 5777c478bd9Sstevel@tonic-gate errno != 0 || *cp != '\0') 5787c478bd9Sstevel@tonic-gate return (NULL); 5797c478bd9Sstevel@tonic-gate for (i = 0; i < nzents; i++) { 5807c478bd9Sstevel@tonic-gate if (zoneid == zents[i].zid) 5817c478bd9Sstevel@tonic-gate return (&zents[i]); 5827c478bd9Sstevel@tonic-gate } 5837c478bd9Sstevel@tonic-gate return (NULL); 5847c478bd9Sstevel@tonic-gate } 5857c478bd9Sstevel@tonic-gate 5867c478bd9Sstevel@tonic-gate /* 5877c478bd9Sstevel@tonic-gate * Check a bit in a mode_t: if on is B_TRUE, that bit should be on; if 5887c478bd9Sstevel@tonic-gate * B_FALSE, it should be off. Return B_TRUE if the mode is bad (incorrect). 5897c478bd9Sstevel@tonic-gate */ 5907c478bd9Sstevel@tonic-gate static boolean_t 5917c478bd9Sstevel@tonic-gate bad_mode_bit(mode_t mode, mode_t bit, boolean_t on, char *file) 5927c478bd9Sstevel@tonic-gate { 5937c478bd9Sstevel@tonic-gate char *str; 5947c478bd9Sstevel@tonic-gate 5957c478bd9Sstevel@tonic-gate assert(bit == S_IRUSR || bit == S_IWUSR || bit == S_IXUSR || 5967c478bd9Sstevel@tonic-gate bit == S_IRGRP || bit == S_IWGRP || bit == S_IXGRP || 5977c478bd9Sstevel@tonic-gate bit == S_IROTH || bit == S_IWOTH || bit == S_IXOTH); 5987c478bd9Sstevel@tonic-gate /* 5997c478bd9Sstevel@tonic-gate * TRANSLATION_NOTE 6007c478bd9Sstevel@tonic-gate * The strings below will be used as part of a larger message, 6017c478bd9Sstevel@tonic-gate * either: 6027c478bd9Sstevel@tonic-gate * (file name) must be (owner|group|world) (read|writ|execut)able 6037c478bd9Sstevel@tonic-gate * or 6047c478bd9Sstevel@tonic-gate * (file name) must not be (owner|group|world) (read|writ|execut)able 6057c478bd9Sstevel@tonic-gate */ 6067c478bd9Sstevel@tonic-gate switch (bit) { 6077c478bd9Sstevel@tonic-gate case S_IRUSR: 6087c478bd9Sstevel@tonic-gate str = gettext("owner readable"); 6097c478bd9Sstevel@tonic-gate break; 6107c478bd9Sstevel@tonic-gate case S_IWUSR: 6117c478bd9Sstevel@tonic-gate str = gettext("owner writable"); 6127c478bd9Sstevel@tonic-gate break; 6137c478bd9Sstevel@tonic-gate case S_IXUSR: 6147c478bd9Sstevel@tonic-gate str = gettext("owner executable"); 6157c478bd9Sstevel@tonic-gate break; 6167c478bd9Sstevel@tonic-gate case S_IRGRP: 6177c478bd9Sstevel@tonic-gate str = gettext("group readable"); 6187c478bd9Sstevel@tonic-gate break; 6197c478bd9Sstevel@tonic-gate case S_IWGRP: 6207c478bd9Sstevel@tonic-gate str = gettext("group writable"); 6217c478bd9Sstevel@tonic-gate break; 6227c478bd9Sstevel@tonic-gate case S_IXGRP: 6237c478bd9Sstevel@tonic-gate str = gettext("group executable"); 6247c478bd9Sstevel@tonic-gate break; 6257c478bd9Sstevel@tonic-gate case S_IROTH: 6267c478bd9Sstevel@tonic-gate str = gettext("world readable"); 6277c478bd9Sstevel@tonic-gate break; 6287c478bd9Sstevel@tonic-gate case S_IWOTH: 6297c478bd9Sstevel@tonic-gate str = gettext("world writable"); 6307c478bd9Sstevel@tonic-gate break; 6317c478bd9Sstevel@tonic-gate case S_IXOTH: 6327c478bd9Sstevel@tonic-gate str = gettext("world executable"); 6337c478bd9Sstevel@tonic-gate break; 6347c478bd9Sstevel@tonic-gate } 6357c478bd9Sstevel@tonic-gate if ((mode & bit) == (on ? 0 : bit)) { 6367c478bd9Sstevel@tonic-gate /* 6377c478bd9Sstevel@tonic-gate * TRANSLATION_NOTE 6387c478bd9Sstevel@tonic-gate * The first parameter below is a file name; the second 6397c478bd9Sstevel@tonic-gate * is one of the "(owner|group|world) (read|writ|execut)able" 6407c478bd9Sstevel@tonic-gate * strings from above. 6417c478bd9Sstevel@tonic-gate */ 6427c478bd9Sstevel@tonic-gate /* 6437c478bd9Sstevel@tonic-gate * The code below could be simplified but not in a way 6447c478bd9Sstevel@tonic-gate * that would easily translate to non-English locales. 6457c478bd9Sstevel@tonic-gate */ 6467c478bd9Sstevel@tonic-gate if (on) { 6477c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("%s must be %s.\n"), 6487c478bd9Sstevel@tonic-gate file, str); 6497c478bd9Sstevel@tonic-gate } else { 6507c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("%s must not be %s.\n"), 6517c478bd9Sstevel@tonic-gate file, str); 6527c478bd9Sstevel@tonic-gate } 6537c478bd9Sstevel@tonic-gate return (B_TRUE); 6547c478bd9Sstevel@tonic-gate } 6557c478bd9Sstevel@tonic-gate return (B_FALSE); 6567c478bd9Sstevel@tonic-gate } 6577c478bd9Sstevel@tonic-gate 6587c478bd9Sstevel@tonic-gate /* 6597c478bd9Sstevel@tonic-gate * We want to make sure that no zone has its zone path as a child node 6607c478bd9Sstevel@tonic-gate * (in the directory sense) of any other. We do that by comparing this 6617c478bd9Sstevel@tonic-gate * zone's path to the path of all other (non-global) zones. The comparison 6627c478bd9Sstevel@tonic-gate * in each case is simple: add '/' to the end of the path, then do a 6637c478bd9Sstevel@tonic-gate * strncmp() of the two paths, using the length of the shorter one. 6647c478bd9Sstevel@tonic-gate */ 6657c478bd9Sstevel@tonic-gate 6667c478bd9Sstevel@tonic-gate static int 6677c478bd9Sstevel@tonic-gate crosscheck_zonepaths(char *path) 6687c478bd9Sstevel@tonic-gate { 6697c478bd9Sstevel@tonic-gate char rpath[MAXPATHLEN]; /* resolved path */ 6707c478bd9Sstevel@tonic-gate char path_copy[MAXPATHLEN]; /* copy of original path */ 6717c478bd9Sstevel@tonic-gate char rpath_copy[MAXPATHLEN]; /* copy of original rpath */ 6727c478bd9Sstevel@tonic-gate struct zoneent *ze; 6737c478bd9Sstevel@tonic-gate int res, err; 6747c478bd9Sstevel@tonic-gate FILE *cookie; 6757c478bd9Sstevel@tonic-gate 6767c478bd9Sstevel@tonic-gate cookie = setzoneent(); 6777c478bd9Sstevel@tonic-gate while ((ze = getzoneent_private(cookie)) != NULL) { 6787c478bd9Sstevel@tonic-gate /* Skip zones which are not installed. */ 6797c478bd9Sstevel@tonic-gate if (ze->zone_state < ZONE_STATE_INSTALLED) { 6807c478bd9Sstevel@tonic-gate free(ze); 6817c478bd9Sstevel@tonic-gate continue; 6827c478bd9Sstevel@tonic-gate } 6837c478bd9Sstevel@tonic-gate /* Skip the global zone and the current target zone. */ 6847c478bd9Sstevel@tonic-gate if (strcmp(ze->zone_name, GLOBAL_ZONENAME) == 0 || 6857c478bd9Sstevel@tonic-gate strcmp(ze->zone_name, target_zone) == 0) { 6867c478bd9Sstevel@tonic-gate free(ze); 6877c478bd9Sstevel@tonic-gate continue; 6887c478bd9Sstevel@tonic-gate } 6897c478bd9Sstevel@tonic-gate if (strlen(ze->zone_path) == 0) { 6907c478bd9Sstevel@tonic-gate /* old index file without path, fall back */ 6917c478bd9Sstevel@tonic-gate if ((err = zone_get_zonepath(ze->zone_name, 6927c478bd9Sstevel@tonic-gate ze->zone_path, sizeof (ze->zone_path))) != Z_OK) { 6937c478bd9Sstevel@tonic-gate errno = err; 6947c478bd9Sstevel@tonic-gate zperror2(ze->zone_name, 6957c478bd9Sstevel@tonic-gate gettext("could not get zone path")); 6967c478bd9Sstevel@tonic-gate free(ze); 6977c478bd9Sstevel@tonic-gate continue; 6987c478bd9Sstevel@tonic-gate } 6997c478bd9Sstevel@tonic-gate } 700108322fbScarlsonj (void) snprintf(path_copy, sizeof (path_copy), "%s%s", 701108322fbScarlsonj zonecfg_get_root(), ze->zone_path); 702108322fbScarlsonj res = resolvepath(path_copy, rpath, sizeof (rpath)); 7037c478bd9Sstevel@tonic-gate if (res == -1) { 7047c478bd9Sstevel@tonic-gate if (errno != ENOENT) { 705108322fbScarlsonj zperror(path_copy, B_FALSE); 7067c478bd9Sstevel@tonic-gate free(ze); 7077c478bd9Sstevel@tonic-gate return (Z_ERR); 7087c478bd9Sstevel@tonic-gate } 7097c478bd9Sstevel@tonic-gate (void) printf(gettext("WARNING: zone %s is installed, " 7107c478bd9Sstevel@tonic-gate "but its %s %s does not exist.\n"), ze->zone_name, 711108322fbScarlsonj "zonepath", path_copy); 7127c478bd9Sstevel@tonic-gate free(ze); 7137c478bd9Sstevel@tonic-gate continue; 7147c478bd9Sstevel@tonic-gate } 7157c478bd9Sstevel@tonic-gate rpath[res] = '\0'; 7167c478bd9Sstevel@tonic-gate (void) snprintf(path_copy, sizeof (path_copy), "%s/", path); 7177c478bd9Sstevel@tonic-gate (void) snprintf(rpath_copy, sizeof (rpath_copy), "%s/", rpath); 7187c478bd9Sstevel@tonic-gate if (strncmp(path_copy, rpath_copy, 7197c478bd9Sstevel@tonic-gate min(strlen(path_copy), strlen(rpath_copy))) == 0) { 7205ee84fbdSgjelinek /* 7215ee84fbdSgjelinek * TRANSLATION_NOTE 7225ee84fbdSgjelinek * zonepath is a literal that should not be translated. 7235ee84fbdSgjelinek */ 7247c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("%s zonepath (%s) and " 7257c478bd9Sstevel@tonic-gate "%s zonepath (%s) overlap.\n"), 7267c478bd9Sstevel@tonic-gate target_zone, path, ze->zone_name, rpath); 7277c478bd9Sstevel@tonic-gate free(ze); 7287c478bd9Sstevel@tonic-gate return (Z_ERR); 7297c478bd9Sstevel@tonic-gate } 7307c478bd9Sstevel@tonic-gate free(ze); 7317c478bd9Sstevel@tonic-gate } 7327c478bd9Sstevel@tonic-gate endzoneent(cookie); 7337c478bd9Sstevel@tonic-gate return (Z_OK); 7347c478bd9Sstevel@tonic-gate } 7357c478bd9Sstevel@tonic-gate 7367c478bd9Sstevel@tonic-gate static int 7377c478bd9Sstevel@tonic-gate validate_zonepath(char *path, int cmd_num) 7387c478bd9Sstevel@tonic-gate { 7397c478bd9Sstevel@tonic-gate int res; /* result of last library/system call */ 7407c478bd9Sstevel@tonic-gate boolean_t err = B_FALSE; /* have we run into an error? */ 7417c478bd9Sstevel@tonic-gate struct stat stbuf; 7427c478bd9Sstevel@tonic-gate struct statvfs vfsbuf; 7437c478bd9Sstevel@tonic-gate char rpath[MAXPATHLEN]; /* resolved path */ 7447c478bd9Sstevel@tonic-gate char ppath[MAXPATHLEN]; /* parent path */ 7457c478bd9Sstevel@tonic-gate char rppath[MAXPATHLEN]; /* resolved parent path */ 7467c478bd9Sstevel@tonic-gate char rootpath[MAXPATHLEN]; /* root path */ 7477c478bd9Sstevel@tonic-gate zone_state_t state; 7487c478bd9Sstevel@tonic-gate 7497c478bd9Sstevel@tonic-gate if (path[0] != '/') { 7507c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 7517c478bd9Sstevel@tonic-gate gettext("%s is not an absolute path.\n"), path); 7527c478bd9Sstevel@tonic-gate return (Z_ERR); 7537c478bd9Sstevel@tonic-gate } 7547c478bd9Sstevel@tonic-gate if ((res = resolvepath(path, rpath, sizeof (rpath))) == -1) { 7557c478bd9Sstevel@tonic-gate if ((errno != ENOENT) || 756865e09a4Sgjelinek (cmd_num != CMD_VERIFY && cmd_num != CMD_INSTALL && 757865e09a4Sgjelinek cmd_num != CMD_CLONE && cmd_num != CMD_MOVE)) { 7587c478bd9Sstevel@tonic-gate zperror(path, B_FALSE); 7597c478bd9Sstevel@tonic-gate return (Z_ERR); 7607c478bd9Sstevel@tonic-gate } 7617c478bd9Sstevel@tonic-gate if (cmd_num == CMD_VERIFY) { 7625ee84fbdSgjelinek /* 7635ee84fbdSgjelinek * TRANSLATION_NOTE 7645ee84fbdSgjelinek * zoneadm is a literal that should not be translated. 7655ee84fbdSgjelinek */ 7667c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("WARNING: %s does not " 7675ee84fbdSgjelinek "exist, so it could not be verified.\nWhen " 7685ee84fbdSgjelinek "'zoneadm %s' is run, '%s' will try to create\n%s, " 7695ee84fbdSgjelinek "and '%s' will be tried again,\nbut the '%s' may " 7705ee84fbdSgjelinek "fail if:\nthe parent directory of %s is group- or " 7715ee84fbdSgjelinek "other-writable\nor\n%s overlaps with any other " 7727c478bd9Sstevel@tonic-gate "installed zones.\n"), path, 7737c478bd9Sstevel@tonic-gate cmd_to_str(CMD_INSTALL), cmd_to_str(CMD_INSTALL), 7747c478bd9Sstevel@tonic-gate path, cmd_to_str(CMD_VERIFY), 7757c478bd9Sstevel@tonic-gate cmd_to_str(CMD_VERIFY), path, path); 7767c478bd9Sstevel@tonic-gate return (Z_OK); 7777c478bd9Sstevel@tonic-gate } 7787c478bd9Sstevel@tonic-gate /* 7797c478bd9Sstevel@tonic-gate * The zonepath is supposed to be mode 700 but its 7807c478bd9Sstevel@tonic-gate * parent(s) 755. So use 755 on the mkdirp() then 7817c478bd9Sstevel@tonic-gate * chmod() the zonepath itself to 700. 7827c478bd9Sstevel@tonic-gate */ 7837c478bd9Sstevel@tonic-gate if (mkdirp(path, DEFAULT_DIR_MODE) < 0) { 7847c478bd9Sstevel@tonic-gate zperror(path, B_FALSE); 7857c478bd9Sstevel@tonic-gate return (Z_ERR); 7867c478bd9Sstevel@tonic-gate } 7877c478bd9Sstevel@tonic-gate /* 7887c478bd9Sstevel@tonic-gate * If the chmod() fails, report the error, but might 7897c478bd9Sstevel@tonic-gate * as well continue the verify procedure. 7907c478bd9Sstevel@tonic-gate */ 7917c478bd9Sstevel@tonic-gate if (chmod(path, S_IRWXU) != 0) 7927c478bd9Sstevel@tonic-gate zperror(path, B_FALSE); 7937c478bd9Sstevel@tonic-gate /* 7947c478bd9Sstevel@tonic-gate * Since the mkdir() succeeded, we should not have to 7957c478bd9Sstevel@tonic-gate * worry about a subsequent ENOENT, thus this should 7967c478bd9Sstevel@tonic-gate * only recurse once. 7977c478bd9Sstevel@tonic-gate */ 798865e09a4Sgjelinek return (validate_zonepath(path, cmd_num)); 7997c478bd9Sstevel@tonic-gate } 8007c478bd9Sstevel@tonic-gate rpath[res] = '\0'; 8017c478bd9Sstevel@tonic-gate if (strcmp(path, rpath) != 0) { 8027c478bd9Sstevel@tonic-gate errno = Z_RESOLVED_PATH; 8037c478bd9Sstevel@tonic-gate zperror(path, B_TRUE); 8047c478bd9Sstevel@tonic-gate return (Z_ERR); 8057c478bd9Sstevel@tonic-gate } 8067c478bd9Sstevel@tonic-gate if ((res = stat(rpath, &stbuf)) != 0) { 8077c478bd9Sstevel@tonic-gate zperror(rpath, B_FALSE); 8087c478bd9Sstevel@tonic-gate return (Z_ERR); 8097c478bd9Sstevel@tonic-gate } 8107c478bd9Sstevel@tonic-gate if (!S_ISDIR(stbuf.st_mode)) { 8117c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("%s is not a directory.\n"), 8127c478bd9Sstevel@tonic-gate rpath); 8137c478bd9Sstevel@tonic-gate return (Z_ERR); 8147c478bd9Sstevel@tonic-gate } 8157c478bd9Sstevel@tonic-gate if ((strcmp(stbuf.st_fstype, MNTTYPE_TMPFS) == 0) || 8167c478bd9Sstevel@tonic-gate (strcmp(stbuf.st_fstype, MNTTYPE_XMEMFS) == 0)) { 8177c478bd9Sstevel@tonic-gate (void) printf(gettext("WARNING: %s is on a temporary " 8187c478bd9Sstevel@tonic-gate "file-system.\n"), rpath); 8197c478bd9Sstevel@tonic-gate } 8207c478bd9Sstevel@tonic-gate if (crosscheck_zonepaths(rpath) != Z_OK) 8217c478bd9Sstevel@tonic-gate return (Z_ERR); 8227c478bd9Sstevel@tonic-gate /* 8237c478bd9Sstevel@tonic-gate * Try to collect and report as many minor errors as possible 8247c478bd9Sstevel@tonic-gate * before returning, so the user can learn everything that needs 8257c478bd9Sstevel@tonic-gate * to be fixed up front. 8267c478bd9Sstevel@tonic-gate */ 8277c478bd9Sstevel@tonic-gate if (stbuf.st_uid != 0) { 8287c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("%s is not owned by root.\n"), 8297c478bd9Sstevel@tonic-gate rpath); 8307c478bd9Sstevel@tonic-gate err = B_TRUE; 8317c478bd9Sstevel@tonic-gate } 8327c478bd9Sstevel@tonic-gate err |= bad_mode_bit(stbuf.st_mode, S_IRUSR, B_TRUE, rpath); 8337c478bd9Sstevel@tonic-gate err |= bad_mode_bit(stbuf.st_mode, S_IWUSR, B_TRUE, rpath); 8347c478bd9Sstevel@tonic-gate err |= bad_mode_bit(stbuf.st_mode, S_IXUSR, B_TRUE, rpath); 8357c478bd9Sstevel@tonic-gate err |= bad_mode_bit(stbuf.st_mode, S_IRGRP, B_FALSE, rpath); 8367c478bd9Sstevel@tonic-gate err |= bad_mode_bit(stbuf.st_mode, S_IWGRP, B_FALSE, rpath); 8377c478bd9Sstevel@tonic-gate err |= bad_mode_bit(stbuf.st_mode, S_IXGRP, B_FALSE, rpath); 8387c478bd9Sstevel@tonic-gate err |= bad_mode_bit(stbuf.st_mode, S_IROTH, B_FALSE, rpath); 8397c478bd9Sstevel@tonic-gate err |= bad_mode_bit(stbuf.st_mode, S_IWOTH, B_FALSE, rpath); 8407c478bd9Sstevel@tonic-gate err |= bad_mode_bit(stbuf.st_mode, S_IXOTH, B_FALSE, rpath); 8417c478bd9Sstevel@tonic-gate 8427c478bd9Sstevel@tonic-gate (void) snprintf(ppath, sizeof (ppath), "%s/..", path); 8437c478bd9Sstevel@tonic-gate if ((res = resolvepath(ppath, rppath, sizeof (rppath))) == -1) { 8447c478bd9Sstevel@tonic-gate zperror(ppath, B_FALSE); 8457c478bd9Sstevel@tonic-gate return (Z_ERR); 8467c478bd9Sstevel@tonic-gate } 8477c478bd9Sstevel@tonic-gate rppath[res] = '\0'; 8487c478bd9Sstevel@tonic-gate if ((res = stat(rppath, &stbuf)) != 0) { 8497c478bd9Sstevel@tonic-gate zperror(rppath, B_FALSE); 8507c478bd9Sstevel@tonic-gate return (Z_ERR); 8517c478bd9Sstevel@tonic-gate } 8527c478bd9Sstevel@tonic-gate /* theoretically impossible */ 8537c478bd9Sstevel@tonic-gate if (!S_ISDIR(stbuf.st_mode)) { 8547c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("%s is not a directory.\n"), 8557c478bd9Sstevel@tonic-gate rppath); 8567c478bd9Sstevel@tonic-gate return (Z_ERR); 8577c478bd9Sstevel@tonic-gate } 8587c478bd9Sstevel@tonic-gate if (stbuf.st_uid != 0) { 8597c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("%s is not owned by root.\n"), 8607c478bd9Sstevel@tonic-gate rppath); 8617c478bd9Sstevel@tonic-gate err = B_TRUE; 8627c478bd9Sstevel@tonic-gate } 8637c478bd9Sstevel@tonic-gate err |= bad_mode_bit(stbuf.st_mode, S_IRUSR, B_TRUE, rppath); 8647c478bd9Sstevel@tonic-gate err |= bad_mode_bit(stbuf.st_mode, S_IWUSR, B_TRUE, rppath); 8657c478bd9Sstevel@tonic-gate err |= bad_mode_bit(stbuf.st_mode, S_IXUSR, B_TRUE, rppath); 8667c478bd9Sstevel@tonic-gate err |= bad_mode_bit(stbuf.st_mode, S_IWGRP, B_FALSE, rppath); 8677c478bd9Sstevel@tonic-gate err |= bad_mode_bit(stbuf.st_mode, S_IWOTH, B_FALSE, rppath); 8687c478bd9Sstevel@tonic-gate if (strcmp(rpath, rppath) == 0) { 8697c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("%s is its own parent.\n"), 8707c478bd9Sstevel@tonic-gate rppath); 8717c478bd9Sstevel@tonic-gate err = B_TRUE; 8727c478bd9Sstevel@tonic-gate } 8737c478bd9Sstevel@tonic-gate 8747c478bd9Sstevel@tonic-gate if (statvfs(rpath, &vfsbuf) != 0) { 8757c478bd9Sstevel@tonic-gate zperror(rpath, B_FALSE); 8767c478bd9Sstevel@tonic-gate return (Z_ERR); 8777c478bd9Sstevel@tonic-gate } 878b5abaf04Sgjelinek if (strcmp(vfsbuf.f_basetype, MNTTYPE_NFS) == 0) { 8795ee84fbdSgjelinek /* 8805ee84fbdSgjelinek * TRANSLATION_NOTE 8815ee84fbdSgjelinek * Zonepath and NFS are literals that should not be translated. 8825ee84fbdSgjelinek */ 8835ee84fbdSgjelinek (void) fprintf(stderr, gettext("Zonepath %s is on an NFS " 8845ee84fbdSgjelinek "mounted file-system.\n" 8855ee84fbdSgjelinek "\tA local file-system must be used.\n"), rpath); 886b5abaf04Sgjelinek return (Z_ERR); 887b5abaf04Sgjelinek } 888b5abaf04Sgjelinek if (vfsbuf.f_flag & ST_NOSUID) { 8895ee84fbdSgjelinek /* 8905ee84fbdSgjelinek * TRANSLATION_NOTE 8915ee84fbdSgjelinek * Zonepath and nosuid are literals that should not be 8925ee84fbdSgjelinek * translated. 8935ee84fbdSgjelinek */ 894b5abaf04Sgjelinek (void) fprintf(stderr, gettext("Zonepath %s is on a nosuid " 8955ee84fbdSgjelinek "file-system.\n"), rpath); 8967c478bd9Sstevel@tonic-gate return (Z_ERR); 8977c478bd9Sstevel@tonic-gate } 8987c478bd9Sstevel@tonic-gate 8997c478bd9Sstevel@tonic-gate if ((res = zone_get_state(target_zone, &state)) != Z_OK) { 9007c478bd9Sstevel@tonic-gate errno = res; 9017c478bd9Sstevel@tonic-gate zperror2(target_zone, gettext("could not get state")); 9027c478bd9Sstevel@tonic-gate return (Z_ERR); 9037c478bd9Sstevel@tonic-gate } 9047c478bd9Sstevel@tonic-gate /* 9057c478bd9Sstevel@tonic-gate * The existence of the root path is only bad in the configured state, 9067c478bd9Sstevel@tonic-gate * as it is *supposed* to be there at the installed and later states. 9077c478bd9Sstevel@tonic-gate * State/command mismatches are caught earlier in verify_details(). 9087c478bd9Sstevel@tonic-gate */ 9097c478bd9Sstevel@tonic-gate if (state == ZONE_STATE_CONFIGURED) { 9107c478bd9Sstevel@tonic-gate if (snprintf(rootpath, sizeof (rootpath), "%s/root", rpath) >= 9117c478bd9Sstevel@tonic-gate sizeof (rootpath)) { 9125ee84fbdSgjelinek /* 9135ee84fbdSgjelinek * TRANSLATION_NOTE 9145ee84fbdSgjelinek * Zonepath is a literal that should not be translated. 9155ee84fbdSgjelinek */ 9167c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 9177c478bd9Sstevel@tonic-gate gettext("Zonepath %s is too long.\n"), rpath); 9187c478bd9Sstevel@tonic-gate return (Z_ERR); 9197c478bd9Sstevel@tonic-gate } 9207c478bd9Sstevel@tonic-gate if ((res = stat(rootpath, &stbuf)) == 0) { 9217c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("Rootpath %s exists; " 9227c478bd9Sstevel@tonic-gate "remove or move aside prior to %s.\n"), rootpath, 9237c478bd9Sstevel@tonic-gate cmd_to_str(CMD_INSTALL)); 9247c478bd9Sstevel@tonic-gate return (Z_ERR); 9257c478bd9Sstevel@tonic-gate } 9267c478bd9Sstevel@tonic-gate } 9277c478bd9Sstevel@tonic-gate 9287c478bd9Sstevel@tonic-gate return (err ? Z_ERR : Z_OK); 9297c478bd9Sstevel@tonic-gate } 9307c478bd9Sstevel@tonic-gate 9317c478bd9Sstevel@tonic-gate static void 9327c478bd9Sstevel@tonic-gate release_lock_file(int lockfd) 9337c478bd9Sstevel@tonic-gate { 9347c478bd9Sstevel@tonic-gate (void) close(lockfd); 9357c478bd9Sstevel@tonic-gate } 9367c478bd9Sstevel@tonic-gate 9377c478bd9Sstevel@tonic-gate static int 9387c478bd9Sstevel@tonic-gate grab_lock_file(const char *zone_name, int *lockfd) 9397c478bd9Sstevel@tonic-gate { 9407c478bd9Sstevel@tonic-gate char pathbuf[PATH_MAX]; 9417c478bd9Sstevel@tonic-gate struct flock flock; 9427c478bd9Sstevel@tonic-gate 943108322fbScarlsonj if (snprintf(pathbuf, sizeof (pathbuf), "%s%s", zonecfg_get_root(), 944108322fbScarlsonj ZONES_TMPDIR) >= sizeof (pathbuf)) { 945108322fbScarlsonj zerror(gettext("alternate root path is too long")); 946108322fbScarlsonj return (Z_ERR); 947108322fbScarlsonj } 948108322fbScarlsonj if (mkdir(pathbuf, S_IRWXU) < 0 && errno != EEXIST) { 949108322fbScarlsonj zerror(gettext("could not mkdir %s: %s"), pathbuf, 9507c478bd9Sstevel@tonic-gate strerror(errno)); 9517c478bd9Sstevel@tonic-gate return (Z_ERR); 9527c478bd9Sstevel@tonic-gate } 953108322fbScarlsonj (void) chmod(pathbuf, S_IRWXU); 9547c478bd9Sstevel@tonic-gate 9557c478bd9Sstevel@tonic-gate /* 9567c478bd9Sstevel@tonic-gate * One of these lock files is created for each zone (when needed). 9577c478bd9Sstevel@tonic-gate * The lock files are not cleaned up (except on system reboot), 9587c478bd9Sstevel@tonic-gate * but since there is only one per zone, there is no resource 9597c478bd9Sstevel@tonic-gate * starvation issue. 9607c478bd9Sstevel@tonic-gate */ 961108322fbScarlsonj if (snprintf(pathbuf, sizeof (pathbuf), "%s%s/%s.zoneadm.lock", 962108322fbScarlsonj zonecfg_get_root(), ZONES_TMPDIR, zone_name) >= sizeof (pathbuf)) { 963108322fbScarlsonj zerror(gettext("alternate root path is too long")); 964108322fbScarlsonj return (Z_ERR); 965108322fbScarlsonj } 9667c478bd9Sstevel@tonic-gate if ((*lockfd = open(pathbuf, O_RDWR|O_CREAT, S_IRUSR|S_IWUSR)) < 0) { 9677c478bd9Sstevel@tonic-gate zerror(gettext("could not open %s: %s"), pathbuf, 9687c478bd9Sstevel@tonic-gate strerror(errno)); 9697c478bd9Sstevel@tonic-gate return (Z_ERR); 9707c478bd9Sstevel@tonic-gate } 9717c478bd9Sstevel@tonic-gate /* 9727c478bd9Sstevel@tonic-gate * Lock the file to synchronize with other zoneadmds 9737c478bd9Sstevel@tonic-gate */ 9747c478bd9Sstevel@tonic-gate flock.l_type = F_WRLCK; 9757c478bd9Sstevel@tonic-gate flock.l_whence = SEEK_SET; 9767c478bd9Sstevel@tonic-gate flock.l_start = (off_t)0; 9777c478bd9Sstevel@tonic-gate flock.l_len = (off_t)0; 9787c478bd9Sstevel@tonic-gate if (fcntl(*lockfd, F_SETLKW, &flock) < 0) { 9797c478bd9Sstevel@tonic-gate zerror(gettext("unable to lock %s: %s"), pathbuf, 9807c478bd9Sstevel@tonic-gate strerror(errno)); 9817c478bd9Sstevel@tonic-gate release_lock_file(*lockfd); 9827c478bd9Sstevel@tonic-gate return (Z_ERR); 9837c478bd9Sstevel@tonic-gate } 9847c478bd9Sstevel@tonic-gate return (Z_OK); 9857c478bd9Sstevel@tonic-gate } 9867c478bd9Sstevel@tonic-gate 987108322fbScarlsonj static boolean_t 9887c478bd9Sstevel@tonic-gate get_doorname(const char *zone_name, char *buffer) 9897c478bd9Sstevel@tonic-gate { 990108322fbScarlsonj return (snprintf(buffer, PATH_MAX, "%s" ZONE_DOOR_PATH, 991108322fbScarlsonj zonecfg_get_root(), zone_name) < PATH_MAX); 9927c478bd9Sstevel@tonic-gate } 9937c478bd9Sstevel@tonic-gate 9947c478bd9Sstevel@tonic-gate /* 9957c478bd9Sstevel@tonic-gate * system daemons are not audited. For the global zone, this occurs 9967c478bd9Sstevel@tonic-gate * "naturally" since init is started with the default audit 9977c478bd9Sstevel@tonic-gate * characteristics. Since zoneadmd is a system daemon and it starts 9987c478bd9Sstevel@tonic-gate * init for a zone, it is necessary to clear out the audit 9997c478bd9Sstevel@tonic-gate * characteristics inherited from whomever started zoneadmd. This is 10007c478bd9Sstevel@tonic-gate * indicated by the audit id, which is set from the ruid parameter of 10017c478bd9Sstevel@tonic-gate * adt_set_user(), below. 10027c478bd9Sstevel@tonic-gate */ 10037c478bd9Sstevel@tonic-gate 10047c478bd9Sstevel@tonic-gate static void 10057c478bd9Sstevel@tonic-gate prepare_audit_context() 10067c478bd9Sstevel@tonic-gate { 10077c478bd9Sstevel@tonic-gate adt_session_data_t *ah; 10087c478bd9Sstevel@tonic-gate char *failure = gettext("audit failure: %s"); 10097c478bd9Sstevel@tonic-gate 10107c478bd9Sstevel@tonic-gate if (adt_start_session(&ah, NULL, 0)) { 10117c478bd9Sstevel@tonic-gate zerror(failure, strerror(errno)); 10127c478bd9Sstevel@tonic-gate return; 10137c478bd9Sstevel@tonic-gate } 10147c478bd9Sstevel@tonic-gate if (adt_set_user(ah, ADT_NO_AUDIT, ADT_NO_AUDIT, 10157c478bd9Sstevel@tonic-gate ADT_NO_AUDIT, ADT_NO_AUDIT, NULL, ADT_NEW)) { 10167c478bd9Sstevel@tonic-gate zerror(failure, strerror(errno)); 10177c478bd9Sstevel@tonic-gate (void) adt_end_session(ah); 10187c478bd9Sstevel@tonic-gate return; 10197c478bd9Sstevel@tonic-gate } 10207c478bd9Sstevel@tonic-gate if (adt_set_proc(ah)) 10217c478bd9Sstevel@tonic-gate zerror(failure, strerror(errno)); 10227c478bd9Sstevel@tonic-gate 10237c478bd9Sstevel@tonic-gate (void) adt_end_session(ah); 10247c478bd9Sstevel@tonic-gate } 10257c478bd9Sstevel@tonic-gate 10267c478bd9Sstevel@tonic-gate static int 10277c478bd9Sstevel@tonic-gate start_zoneadmd(const char *zone_name) 10287c478bd9Sstevel@tonic-gate { 10297c478bd9Sstevel@tonic-gate char doorpath[PATH_MAX]; 10307c478bd9Sstevel@tonic-gate pid_t child_pid; 10317c478bd9Sstevel@tonic-gate int error = Z_ERR; 10327c478bd9Sstevel@tonic-gate int doorfd, lockfd; 10337c478bd9Sstevel@tonic-gate struct door_info info; 10347c478bd9Sstevel@tonic-gate 1035108322fbScarlsonj if (!get_doorname(zone_name, doorpath)) 1036108322fbScarlsonj return (Z_ERR); 10377c478bd9Sstevel@tonic-gate 10387c478bd9Sstevel@tonic-gate if (grab_lock_file(zone_name, &lockfd) != Z_OK) 10397c478bd9Sstevel@tonic-gate return (Z_ERR); 10407c478bd9Sstevel@tonic-gate 10417c478bd9Sstevel@tonic-gate /* 10427c478bd9Sstevel@tonic-gate * Now that we have the lock, re-confirm that the daemon is 10437c478bd9Sstevel@tonic-gate * *not* up and working fine. If it is still down, we have a green 10447c478bd9Sstevel@tonic-gate * light to start it. 10457c478bd9Sstevel@tonic-gate */ 10467c478bd9Sstevel@tonic-gate if ((doorfd = open(doorpath, O_RDONLY)) < 0) { 10477c478bd9Sstevel@tonic-gate if (errno != ENOENT) { 10487c478bd9Sstevel@tonic-gate zperror(doorpath, B_FALSE); 10497c478bd9Sstevel@tonic-gate goto out; 10507c478bd9Sstevel@tonic-gate } 10517c478bd9Sstevel@tonic-gate } else { 10527c478bd9Sstevel@tonic-gate if (door_info(doorfd, &info) == 0 && 10537c478bd9Sstevel@tonic-gate ((info.di_attributes & DOOR_REVOKED) == 0)) { 10547c478bd9Sstevel@tonic-gate error = Z_OK; 10557c478bd9Sstevel@tonic-gate (void) close(doorfd); 10567c478bd9Sstevel@tonic-gate goto out; 10577c478bd9Sstevel@tonic-gate } 10587c478bd9Sstevel@tonic-gate (void) close(doorfd); 10597c478bd9Sstevel@tonic-gate } 10607c478bd9Sstevel@tonic-gate 10617c478bd9Sstevel@tonic-gate if ((child_pid = fork()) == -1) { 10627c478bd9Sstevel@tonic-gate zperror(gettext("could not fork"), B_FALSE); 10637c478bd9Sstevel@tonic-gate goto out; 10647c478bd9Sstevel@tonic-gate } else if (child_pid == 0) { 1065108322fbScarlsonj const char *argv[6], **ap; 10667c478bd9Sstevel@tonic-gate 1067108322fbScarlsonj /* child process */ 10687c478bd9Sstevel@tonic-gate prepare_audit_context(); 10697c478bd9Sstevel@tonic-gate 1070108322fbScarlsonj ap = argv; 1071108322fbScarlsonj *ap++ = "zoneadmd"; 1072108322fbScarlsonj *ap++ = "-z"; 1073108322fbScarlsonj *ap++ = zone_name; 1074108322fbScarlsonj if (zonecfg_in_alt_root()) { 1075108322fbScarlsonj *ap++ = "-R"; 1076108322fbScarlsonj *ap++ = zonecfg_get_root(); 1077108322fbScarlsonj } 1078108322fbScarlsonj *ap = NULL; 1079108322fbScarlsonj 1080108322fbScarlsonj (void) execv("/usr/lib/zones/zoneadmd", (char * const *)argv); 10815ee84fbdSgjelinek /* 10825ee84fbdSgjelinek * TRANSLATION_NOTE 10835ee84fbdSgjelinek * zoneadmd is a literal that should not be translated. 10845ee84fbdSgjelinek */ 10857c478bd9Sstevel@tonic-gate zperror(gettext("could not exec zoneadmd"), B_FALSE); 10867c478bd9Sstevel@tonic-gate _exit(Z_ERR); 10877c478bd9Sstevel@tonic-gate } else { 10887c478bd9Sstevel@tonic-gate /* parent process */ 10897c478bd9Sstevel@tonic-gate pid_t retval; 10907c478bd9Sstevel@tonic-gate int pstatus = 0; 10917c478bd9Sstevel@tonic-gate 10927c478bd9Sstevel@tonic-gate do { 10937c478bd9Sstevel@tonic-gate retval = waitpid(child_pid, &pstatus, 0); 10947c478bd9Sstevel@tonic-gate } while (retval != child_pid); 10957c478bd9Sstevel@tonic-gate if (WIFSIGNALED(pstatus) || (WIFEXITED(pstatus) && 10967c478bd9Sstevel@tonic-gate WEXITSTATUS(pstatus) != 0)) { 10977c478bd9Sstevel@tonic-gate zerror(gettext("could not start %s"), "zoneadmd"); 10987c478bd9Sstevel@tonic-gate goto out; 10997c478bd9Sstevel@tonic-gate } 11007c478bd9Sstevel@tonic-gate } 11017c478bd9Sstevel@tonic-gate error = Z_OK; 11027c478bd9Sstevel@tonic-gate out: 11037c478bd9Sstevel@tonic-gate release_lock_file(lockfd); 11047c478bd9Sstevel@tonic-gate return (error); 11057c478bd9Sstevel@tonic-gate } 11067c478bd9Sstevel@tonic-gate 11077c478bd9Sstevel@tonic-gate static int 11087c478bd9Sstevel@tonic-gate ping_zoneadmd(const char *zone_name) 11097c478bd9Sstevel@tonic-gate { 11107c478bd9Sstevel@tonic-gate char doorpath[PATH_MAX]; 11117c478bd9Sstevel@tonic-gate int doorfd; 11127c478bd9Sstevel@tonic-gate struct door_info info; 11137c478bd9Sstevel@tonic-gate 1114108322fbScarlsonj if (!get_doorname(zone_name, doorpath)) 1115108322fbScarlsonj return (Z_ERR); 11167c478bd9Sstevel@tonic-gate 11177c478bd9Sstevel@tonic-gate if ((doorfd = open(doorpath, O_RDONLY)) < 0) { 11187c478bd9Sstevel@tonic-gate return (Z_ERR); 11197c478bd9Sstevel@tonic-gate } 11207c478bd9Sstevel@tonic-gate if (door_info(doorfd, &info) == 0 && 11217c478bd9Sstevel@tonic-gate ((info.di_attributes & DOOR_REVOKED) == 0)) { 11227c478bd9Sstevel@tonic-gate (void) close(doorfd); 11237c478bd9Sstevel@tonic-gate return (Z_OK); 11247c478bd9Sstevel@tonic-gate } 11257c478bd9Sstevel@tonic-gate (void) close(doorfd); 11267c478bd9Sstevel@tonic-gate return (Z_ERR); 11277c478bd9Sstevel@tonic-gate } 11287c478bd9Sstevel@tonic-gate 11297c478bd9Sstevel@tonic-gate static int 11307c478bd9Sstevel@tonic-gate call_zoneadmd(const char *zone_name, zone_cmd_arg_t *arg) 11317c478bd9Sstevel@tonic-gate { 11327c478bd9Sstevel@tonic-gate char doorpath[PATH_MAX]; 11337c478bd9Sstevel@tonic-gate int doorfd, result; 11347c478bd9Sstevel@tonic-gate door_arg_t darg; 11357c478bd9Sstevel@tonic-gate 11367c478bd9Sstevel@tonic-gate zoneid_t zoneid; 11377c478bd9Sstevel@tonic-gate uint64_t uniqid = 0; 11387c478bd9Sstevel@tonic-gate 11397c478bd9Sstevel@tonic-gate zone_cmd_rval_t *rvalp; 11407c478bd9Sstevel@tonic-gate size_t rlen; 11417c478bd9Sstevel@tonic-gate char *cp, *errbuf; 11427c478bd9Sstevel@tonic-gate 11437c478bd9Sstevel@tonic-gate rlen = getpagesize(); 11447c478bd9Sstevel@tonic-gate if ((rvalp = malloc(rlen)) == NULL) { 11457c478bd9Sstevel@tonic-gate zerror(gettext("failed to allocate %lu bytes: %s"), rlen, 11467c478bd9Sstevel@tonic-gate strerror(errno)); 11477c478bd9Sstevel@tonic-gate return (-1); 11487c478bd9Sstevel@tonic-gate } 11497c478bd9Sstevel@tonic-gate 11507c478bd9Sstevel@tonic-gate if ((zoneid = getzoneidbyname(zone_name)) != ZONE_ID_UNDEFINED) { 11517c478bd9Sstevel@tonic-gate (void) zone_getattr(zoneid, ZONE_ATTR_UNIQID, &uniqid, 11527c478bd9Sstevel@tonic-gate sizeof (uniqid)); 11537c478bd9Sstevel@tonic-gate } 11547c478bd9Sstevel@tonic-gate arg->uniqid = uniqid; 11557c478bd9Sstevel@tonic-gate (void) strlcpy(arg->locale, locale, sizeof (arg->locale)); 1156108322fbScarlsonj if (!get_doorname(zone_name, doorpath)) { 1157108322fbScarlsonj zerror(gettext("alternate root path is too long")); 1158108322fbScarlsonj free(rvalp); 1159108322fbScarlsonj return (-1); 1160108322fbScarlsonj } 11617c478bd9Sstevel@tonic-gate 11627c478bd9Sstevel@tonic-gate /* 11637c478bd9Sstevel@tonic-gate * Loop trying to start zoneadmd; if something goes seriously 11647c478bd9Sstevel@tonic-gate * wrong we break out and fail. 11657c478bd9Sstevel@tonic-gate */ 11667c478bd9Sstevel@tonic-gate for (;;) { 11677c478bd9Sstevel@tonic-gate if (start_zoneadmd(zone_name) != Z_OK) 11687c478bd9Sstevel@tonic-gate break; 11697c478bd9Sstevel@tonic-gate 11707c478bd9Sstevel@tonic-gate if ((doorfd = open(doorpath, O_RDONLY)) < 0) { 11717c478bd9Sstevel@tonic-gate zperror(gettext("failed to open zone door"), B_FALSE); 11727c478bd9Sstevel@tonic-gate break; 11737c478bd9Sstevel@tonic-gate } 11747c478bd9Sstevel@tonic-gate 11757c478bd9Sstevel@tonic-gate darg.data_ptr = (char *)arg; 11767c478bd9Sstevel@tonic-gate darg.data_size = sizeof (*arg); 11777c478bd9Sstevel@tonic-gate darg.desc_ptr = NULL; 11787c478bd9Sstevel@tonic-gate darg.desc_num = 0; 11797c478bd9Sstevel@tonic-gate darg.rbuf = (char *)rvalp; 11807c478bd9Sstevel@tonic-gate darg.rsize = rlen; 11817c478bd9Sstevel@tonic-gate if (door_call(doorfd, &darg) != 0) { 11827c478bd9Sstevel@tonic-gate (void) close(doorfd); 11837c478bd9Sstevel@tonic-gate /* 11847c478bd9Sstevel@tonic-gate * We'll get EBADF if the door has been revoked. 11857c478bd9Sstevel@tonic-gate */ 11867c478bd9Sstevel@tonic-gate if (errno != EBADF) { 11877c478bd9Sstevel@tonic-gate zperror(gettext("door_call failed"), B_FALSE); 11887c478bd9Sstevel@tonic-gate break; 11897c478bd9Sstevel@tonic-gate } 11907c478bd9Sstevel@tonic-gate continue; /* take another lap */ 11917c478bd9Sstevel@tonic-gate } 11927c478bd9Sstevel@tonic-gate (void) close(doorfd); 11937c478bd9Sstevel@tonic-gate 11947c478bd9Sstevel@tonic-gate if (darg.data_size == 0) { 11957c478bd9Sstevel@tonic-gate /* Door server is going away; kick it again. */ 11967c478bd9Sstevel@tonic-gate continue; 11977c478bd9Sstevel@tonic-gate } 11987c478bd9Sstevel@tonic-gate 11997c478bd9Sstevel@tonic-gate errbuf = rvalp->errbuf; 12007c478bd9Sstevel@tonic-gate while (*errbuf != '\0') { 12017c478bd9Sstevel@tonic-gate /* 12027c478bd9Sstevel@tonic-gate * Remove any newlines since zerror() 12037c478bd9Sstevel@tonic-gate * will append one automatically. 12047c478bd9Sstevel@tonic-gate */ 12057c478bd9Sstevel@tonic-gate cp = strchr(errbuf, '\n'); 12067c478bd9Sstevel@tonic-gate if (cp != NULL) 12077c478bd9Sstevel@tonic-gate *cp = '\0'; 12087c478bd9Sstevel@tonic-gate zerror("%s", errbuf); 12097c478bd9Sstevel@tonic-gate if (cp == NULL) 12107c478bd9Sstevel@tonic-gate break; 12117c478bd9Sstevel@tonic-gate errbuf = cp + 1; 12127c478bd9Sstevel@tonic-gate } 12137c478bd9Sstevel@tonic-gate result = rvalp->rval == 0 ? 0 : -1; 12147c478bd9Sstevel@tonic-gate free(rvalp); 12157c478bd9Sstevel@tonic-gate return (result); 12167c478bd9Sstevel@tonic-gate } 12177c478bd9Sstevel@tonic-gate 12187c478bd9Sstevel@tonic-gate free(rvalp); 12197c478bd9Sstevel@tonic-gate return (-1); 12207c478bd9Sstevel@tonic-gate } 12217c478bd9Sstevel@tonic-gate 12227c478bd9Sstevel@tonic-gate static int 12237c478bd9Sstevel@tonic-gate ready_func(int argc, char *argv[]) 12247c478bd9Sstevel@tonic-gate { 12257c478bd9Sstevel@tonic-gate zone_cmd_arg_t zarg; 12267c478bd9Sstevel@tonic-gate int arg; 12277c478bd9Sstevel@tonic-gate 1228108322fbScarlsonj if (zonecfg_in_alt_root()) { 1229108322fbScarlsonj zerror(gettext("cannot ready zone in alternate root")); 1230108322fbScarlsonj return (Z_ERR); 1231108322fbScarlsonj } 1232108322fbScarlsonj 12337c478bd9Sstevel@tonic-gate optind = 0; 12347c478bd9Sstevel@tonic-gate if ((arg = getopt(argc, argv, "?")) != EOF) { 12357c478bd9Sstevel@tonic-gate switch (arg) { 12367c478bd9Sstevel@tonic-gate case '?': 12377c478bd9Sstevel@tonic-gate sub_usage(SHELP_READY, CMD_READY); 12387c478bd9Sstevel@tonic-gate return (optopt == '?' ? Z_OK : Z_USAGE); 12397c478bd9Sstevel@tonic-gate default: 12407c478bd9Sstevel@tonic-gate sub_usage(SHELP_READY, CMD_READY); 12417c478bd9Sstevel@tonic-gate return (Z_USAGE); 12427c478bd9Sstevel@tonic-gate } 12437c478bd9Sstevel@tonic-gate } 12447c478bd9Sstevel@tonic-gate if (argc > optind) { 12457c478bd9Sstevel@tonic-gate sub_usage(SHELP_READY, CMD_READY); 12467c478bd9Sstevel@tonic-gate return (Z_USAGE); 12477c478bd9Sstevel@tonic-gate } 12487c478bd9Sstevel@tonic-gate if (sanity_check(target_zone, CMD_READY, B_FALSE, B_FALSE) != Z_OK) 12497c478bd9Sstevel@tonic-gate return (Z_ERR); 12507c478bd9Sstevel@tonic-gate if (verify_details(CMD_READY) != Z_OK) 12517c478bd9Sstevel@tonic-gate return (Z_ERR); 12527c478bd9Sstevel@tonic-gate 12537c478bd9Sstevel@tonic-gate zarg.cmd = Z_READY; 12547c478bd9Sstevel@tonic-gate if (call_zoneadmd(target_zone, &zarg) != 0) { 12557c478bd9Sstevel@tonic-gate zerror(gettext("call to %s failed"), "zoneadmd"); 12567c478bd9Sstevel@tonic-gate return (Z_ERR); 12577c478bd9Sstevel@tonic-gate } 12587c478bd9Sstevel@tonic-gate return (Z_OK); 12597c478bd9Sstevel@tonic-gate } 12607c478bd9Sstevel@tonic-gate 12617c478bd9Sstevel@tonic-gate static int 12627c478bd9Sstevel@tonic-gate boot_func(int argc, char *argv[]) 12637c478bd9Sstevel@tonic-gate { 12647c478bd9Sstevel@tonic-gate zone_cmd_arg_t zarg; 12657c478bd9Sstevel@tonic-gate int arg; 12667c478bd9Sstevel@tonic-gate 1267108322fbScarlsonj if (zonecfg_in_alt_root()) { 1268108322fbScarlsonj zerror(gettext("cannot boot zone in alternate root")); 1269108322fbScarlsonj return (Z_ERR); 1270108322fbScarlsonj } 1271108322fbScarlsonj 12727c478bd9Sstevel@tonic-gate zarg.bootbuf[0] = '\0'; 12737c478bd9Sstevel@tonic-gate 12747c478bd9Sstevel@tonic-gate /* 12757c478bd9Sstevel@tonic-gate * At the current time, the only supported subargument to the 12767c478bd9Sstevel@tonic-gate * "boot" subcommand is "-s" which specifies a single-user boot. 12777c478bd9Sstevel@tonic-gate * In the future, other boot arguments should be supported 12787c478bd9Sstevel@tonic-gate * including "-m" for specifying alternate smf(5) milestones. 12797c478bd9Sstevel@tonic-gate */ 12807c478bd9Sstevel@tonic-gate optind = 0; 12817c478bd9Sstevel@tonic-gate if ((arg = getopt(argc, argv, "?s")) != EOF) { 12827c478bd9Sstevel@tonic-gate switch (arg) { 12837c478bd9Sstevel@tonic-gate case '?': 12847c478bd9Sstevel@tonic-gate sub_usage(SHELP_BOOT, CMD_BOOT); 12857c478bd9Sstevel@tonic-gate return (optopt == '?' ? Z_OK : Z_USAGE); 12867c478bd9Sstevel@tonic-gate case 's': 12877c478bd9Sstevel@tonic-gate (void) strlcpy(zarg.bootbuf, "-s", 12887c478bd9Sstevel@tonic-gate sizeof (zarg.bootbuf)); 12897c478bd9Sstevel@tonic-gate break; 12907c478bd9Sstevel@tonic-gate default: 12917c478bd9Sstevel@tonic-gate sub_usage(SHELP_BOOT, CMD_BOOT); 12927c478bd9Sstevel@tonic-gate return (Z_USAGE); 12937c478bd9Sstevel@tonic-gate } 12947c478bd9Sstevel@tonic-gate } 12957c478bd9Sstevel@tonic-gate if (argc > optind) { 12967c478bd9Sstevel@tonic-gate sub_usage(SHELP_BOOT, CMD_BOOT); 12977c478bd9Sstevel@tonic-gate return (Z_USAGE); 12987c478bd9Sstevel@tonic-gate } 12997c478bd9Sstevel@tonic-gate if (sanity_check(target_zone, CMD_BOOT, B_FALSE, B_FALSE) != Z_OK) 13007c478bd9Sstevel@tonic-gate return (Z_ERR); 13017c478bd9Sstevel@tonic-gate if (verify_details(CMD_BOOT) != Z_OK) 13027c478bd9Sstevel@tonic-gate return (Z_ERR); 13037c478bd9Sstevel@tonic-gate zarg.cmd = Z_BOOT; 13047c478bd9Sstevel@tonic-gate if (call_zoneadmd(target_zone, &zarg) != 0) { 13057c478bd9Sstevel@tonic-gate zerror(gettext("call to %s failed"), "zoneadmd"); 13067c478bd9Sstevel@tonic-gate return (Z_ERR); 13077c478bd9Sstevel@tonic-gate } 13087c478bd9Sstevel@tonic-gate return (Z_OK); 13097c478bd9Sstevel@tonic-gate } 13107c478bd9Sstevel@tonic-gate 13117c478bd9Sstevel@tonic-gate static void 13127c478bd9Sstevel@tonic-gate fake_up_local_zone(zoneid_t zid, zone_entry_t *zeptr) 13137c478bd9Sstevel@tonic-gate { 13147c478bd9Sstevel@tonic-gate ssize_t result; 13157c478bd9Sstevel@tonic-gate 13167c478bd9Sstevel@tonic-gate zeptr->zid = zid; 13177c478bd9Sstevel@tonic-gate /* 13187c478bd9Sstevel@tonic-gate * Since we're looking up our own (non-global) zone name, 13197c478bd9Sstevel@tonic-gate * we can be assured that it will succeed. 13207c478bd9Sstevel@tonic-gate */ 13217c478bd9Sstevel@tonic-gate result = getzonenamebyid(zid, zeptr->zname, sizeof (zeptr->zname)); 13227c478bd9Sstevel@tonic-gate assert(result >= 0); 13237c478bd9Sstevel@tonic-gate (void) strlcpy(zeptr->zroot, "/", sizeof (zeptr->zroot)); 13247c478bd9Sstevel@tonic-gate zeptr->zstate_str = "running"; 13257c478bd9Sstevel@tonic-gate } 13267c478bd9Sstevel@tonic-gate 13277c478bd9Sstevel@tonic-gate static int 13287c478bd9Sstevel@tonic-gate list_func(int argc, char *argv[]) 13297c478bd9Sstevel@tonic-gate { 13307c478bd9Sstevel@tonic-gate zone_entry_t *zentp, zent; 1331108322fbScarlsonj int arg, retv; 13327c478bd9Sstevel@tonic-gate boolean_t output = B_FALSE, verbose = B_FALSE, parsable = B_FALSE; 13337c478bd9Sstevel@tonic-gate zone_state_t min_state = ZONE_STATE_RUNNING; 13347c478bd9Sstevel@tonic-gate zoneid_t zone_id = getzoneid(); 13357c478bd9Sstevel@tonic-gate 13367c478bd9Sstevel@tonic-gate if (target_zone == NULL) { 13377c478bd9Sstevel@tonic-gate /* all zones: default view to running but allow override */ 13387c478bd9Sstevel@tonic-gate optind = 0; 13397c478bd9Sstevel@tonic-gate while ((arg = getopt(argc, argv, "?cipv")) != EOF) { 13407c478bd9Sstevel@tonic-gate switch (arg) { 13417c478bd9Sstevel@tonic-gate case '?': 13427c478bd9Sstevel@tonic-gate sub_usage(SHELP_LIST, CMD_LIST); 13437c478bd9Sstevel@tonic-gate return (optopt == '?' ? Z_OK : Z_USAGE); 1344475d78a4Sjonb /* 1345475d78a4Sjonb * The 'i' and 'c' options are not mutually 1346475d78a4Sjonb * exclusive so if 'c' is given, then min_state 1347475d78a4Sjonb * is set to 0 (ZONE_STATE_CONFIGURED) which is 1348475d78a4Sjonb * the lowest possible state. If 'i' is given, 1349475d78a4Sjonb * then min_state is set to be the lowest state 1350475d78a4Sjonb * so far. 1351475d78a4Sjonb */ 13527c478bd9Sstevel@tonic-gate case 'c': 13537c478bd9Sstevel@tonic-gate min_state = ZONE_STATE_CONFIGURED; 13547c478bd9Sstevel@tonic-gate break; 13557c478bd9Sstevel@tonic-gate case 'i': 1356475d78a4Sjonb min_state = min(ZONE_STATE_INSTALLED, 1357475d78a4Sjonb min_state); 1358475d78a4Sjonb 13597c478bd9Sstevel@tonic-gate break; 13607c478bd9Sstevel@tonic-gate case 'p': 13617c478bd9Sstevel@tonic-gate parsable = B_TRUE; 13627c478bd9Sstevel@tonic-gate break; 13637c478bd9Sstevel@tonic-gate case 'v': 13647c478bd9Sstevel@tonic-gate verbose = B_TRUE; 13657c478bd9Sstevel@tonic-gate break; 13667c478bd9Sstevel@tonic-gate default: 13677c478bd9Sstevel@tonic-gate sub_usage(SHELP_LIST, CMD_LIST); 13687c478bd9Sstevel@tonic-gate return (Z_USAGE); 13697c478bd9Sstevel@tonic-gate } 13707c478bd9Sstevel@tonic-gate } 13717c478bd9Sstevel@tonic-gate if (parsable && verbose) { 13727c478bd9Sstevel@tonic-gate zerror(gettext("%s -p and -v are mutually exclusive."), 13737c478bd9Sstevel@tonic-gate cmd_to_str(CMD_LIST)); 13747c478bd9Sstevel@tonic-gate return (Z_ERR); 13757c478bd9Sstevel@tonic-gate } 13767c478bd9Sstevel@tonic-gate if (zone_id == GLOBAL_ZONEID) { 1377108322fbScarlsonj retv = zone_print_list(min_state, verbose, parsable); 13787c478bd9Sstevel@tonic-gate } else { 1379108322fbScarlsonj retv = Z_OK; 13807c478bd9Sstevel@tonic-gate fake_up_local_zone(zone_id, &zent); 13817c478bd9Sstevel@tonic-gate zone_print(&zent, verbose, parsable); 13827c478bd9Sstevel@tonic-gate } 1383108322fbScarlsonj return (retv); 13847c478bd9Sstevel@tonic-gate } 13857c478bd9Sstevel@tonic-gate 13867c478bd9Sstevel@tonic-gate /* 13877c478bd9Sstevel@tonic-gate * Specific target zone: disallow -i/-c suboptions. 13887c478bd9Sstevel@tonic-gate */ 13897c478bd9Sstevel@tonic-gate optind = 0; 13907c478bd9Sstevel@tonic-gate while ((arg = getopt(argc, argv, "?pv")) != EOF) { 13917c478bd9Sstevel@tonic-gate switch (arg) { 13927c478bd9Sstevel@tonic-gate case '?': 13937c478bd9Sstevel@tonic-gate sub_usage(SHELP_LIST, CMD_LIST); 13947c478bd9Sstevel@tonic-gate return (optopt == '?' ? Z_OK : Z_USAGE); 13957c478bd9Sstevel@tonic-gate case 'p': 13967c478bd9Sstevel@tonic-gate parsable = B_TRUE; 13977c478bd9Sstevel@tonic-gate break; 13987c478bd9Sstevel@tonic-gate case 'v': 13997c478bd9Sstevel@tonic-gate verbose = B_TRUE; 14007c478bd9Sstevel@tonic-gate break; 14017c478bd9Sstevel@tonic-gate default: 14027c478bd9Sstevel@tonic-gate sub_usage(SHELP_LIST, CMD_LIST); 14037c478bd9Sstevel@tonic-gate return (Z_USAGE); 14047c478bd9Sstevel@tonic-gate } 14057c478bd9Sstevel@tonic-gate } 14067c478bd9Sstevel@tonic-gate if (parsable && verbose) { 14077c478bd9Sstevel@tonic-gate zerror(gettext("%s -p and -v are mutually exclusive."), 14087c478bd9Sstevel@tonic-gate cmd_to_str(CMD_LIST)); 14097c478bd9Sstevel@tonic-gate return (Z_ERR); 14107c478bd9Sstevel@tonic-gate } 14117c478bd9Sstevel@tonic-gate if (argc > optind) { 14127c478bd9Sstevel@tonic-gate sub_usage(SHELP_LIST, CMD_LIST); 14137c478bd9Sstevel@tonic-gate return (Z_USAGE); 14147c478bd9Sstevel@tonic-gate } 14157c478bd9Sstevel@tonic-gate if (zone_id != GLOBAL_ZONEID) { 14167c478bd9Sstevel@tonic-gate fake_up_local_zone(zone_id, &zent); 14177c478bd9Sstevel@tonic-gate /* 14187c478bd9Sstevel@tonic-gate * main() will issue a Z_NO_ZONE error if it cannot get an 14197c478bd9Sstevel@tonic-gate * id for target_zone, which in a non-global zone should 14207c478bd9Sstevel@tonic-gate * happen for any zone name except `zonename`. Thus we 14217c478bd9Sstevel@tonic-gate * assert() that here but don't otherwise check. 14227c478bd9Sstevel@tonic-gate */ 14237c478bd9Sstevel@tonic-gate assert(strcmp(zent.zname, target_zone) == 0); 14247c478bd9Sstevel@tonic-gate zone_print(&zent, verbose, parsable); 14257c478bd9Sstevel@tonic-gate output = B_TRUE; 14267c478bd9Sstevel@tonic-gate } else if ((zentp = lookup_running_zone(target_zone)) != NULL) { 14277c478bd9Sstevel@tonic-gate zone_print(zentp, verbose, parsable); 14287c478bd9Sstevel@tonic-gate output = B_TRUE; 1429108322fbScarlsonj } else if (lookup_zone_info(target_zone, ZONE_ID_UNDEFINED, 1430108322fbScarlsonj &zent) == Z_OK) { 14317c478bd9Sstevel@tonic-gate zone_print(&zent, verbose, parsable); 14327c478bd9Sstevel@tonic-gate output = B_TRUE; 14337c478bd9Sstevel@tonic-gate } 14347c478bd9Sstevel@tonic-gate return (output ? Z_OK : Z_ERR); 14357c478bd9Sstevel@tonic-gate } 14367c478bd9Sstevel@tonic-gate 14377c478bd9Sstevel@tonic-gate static void 14387c478bd9Sstevel@tonic-gate sigterm(int sig) 14397c478bd9Sstevel@tonic-gate { 14407c478bd9Sstevel@tonic-gate /* 14417c478bd9Sstevel@tonic-gate * Ignore SIG{INT,TERM}, so we don't end up in an infinite loop, 14427c478bd9Sstevel@tonic-gate * then propagate the signal to our process group. 14437c478bd9Sstevel@tonic-gate */ 14447c478bd9Sstevel@tonic-gate (void) sigset(SIGINT, SIG_IGN); 14457c478bd9Sstevel@tonic-gate (void) sigset(SIGTERM, SIG_IGN); 14467c478bd9Sstevel@tonic-gate (void) kill(0, sig); 14477c478bd9Sstevel@tonic-gate child_killed = B_TRUE; 14487c478bd9Sstevel@tonic-gate } 14497c478bd9Sstevel@tonic-gate 14507c478bd9Sstevel@tonic-gate static int 14517c478bd9Sstevel@tonic-gate do_subproc(char *cmdbuf) 14527c478bd9Sstevel@tonic-gate { 14537c478bd9Sstevel@tonic-gate char inbuf[1024]; /* arbitrary large amount */ 14547c478bd9Sstevel@tonic-gate FILE *file; 14557c478bd9Sstevel@tonic-gate 14567c478bd9Sstevel@tonic-gate child_killed = B_FALSE; 14577c478bd9Sstevel@tonic-gate /* 14587c478bd9Sstevel@tonic-gate * We use popen(3c) to launch child processes for [un]install; 14597c478bd9Sstevel@tonic-gate * this library call does not return a PID, so we have to kill 14607c478bd9Sstevel@tonic-gate * the whole process group. To avoid killing our parent, we 14617c478bd9Sstevel@tonic-gate * become a process group leader here. But doing so can wreak 14627c478bd9Sstevel@tonic-gate * havoc with reading from stdin when launched by a non-job-control 14637c478bd9Sstevel@tonic-gate * shell, so we close stdin and reopen it as /dev/null first. 14647c478bd9Sstevel@tonic-gate */ 14657c478bd9Sstevel@tonic-gate (void) close(STDIN_FILENO); 14667c478bd9Sstevel@tonic-gate (void) open("/dev/null", O_RDONLY); 14677c478bd9Sstevel@tonic-gate (void) setpgid(0, 0); 14687c478bd9Sstevel@tonic-gate (void) sigset(SIGINT, sigterm); 14697c478bd9Sstevel@tonic-gate (void) sigset(SIGTERM, sigterm); 14707c478bd9Sstevel@tonic-gate file = popen(cmdbuf, "r"); 14717c478bd9Sstevel@tonic-gate for (;;) { 14727c478bd9Sstevel@tonic-gate if (child_killed || fgets(inbuf, sizeof (inbuf), file) == NULL) 14737c478bd9Sstevel@tonic-gate break; 14747c478bd9Sstevel@tonic-gate (void) fputs(inbuf, stdout); 14757c478bd9Sstevel@tonic-gate } 14767c478bd9Sstevel@tonic-gate (void) sigset(SIGINT, SIG_DFL); 14777c478bd9Sstevel@tonic-gate (void) sigset(SIGTERM, SIG_DFL); 14787c478bd9Sstevel@tonic-gate return (pclose(file)); 14797c478bd9Sstevel@tonic-gate } 14807c478bd9Sstevel@tonic-gate 14817c478bd9Sstevel@tonic-gate static int 14827c478bd9Sstevel@tonic-gate subproc_status(const char *cmd, int status) 14837c478bd9Sstevel@tonic-gate { 14847c478bd9Sstevel@tonic-gate if (WIFEXITED(status)) { 14857c478bd9Sstevel@tonic-gate int exit_code = WEXITSTATUS(status); 14867c478bd9Sstevel@tonic-gate 14877c478bd9Sstevel@tonic-gate if (exit_code == 0) 14887c478bd9Sstevel@tonic-gate return (Z_OK); 14897c478bd9Sstevel@tonic-gate zerror(gettext("'%s' failed with exit code %d."), cmd, 14907c478bd9Sstevel@tonic-gate exit_code); 14917c478bd9Sstevel@tonic-gate } else if (WIFSIGNALED(status)) { 14927c478bd9Sstevel@tonic-gate int signal = WTERMSIG(status); 14937c478bd9Sstevel@tonic-gate char sigstr[SIG2STR_MAX]; 14947c478bd9Sstevel@tonic-gate 14957c478bd9Sstevel@tonic-gate if (sig2str(signal, sigstr) == 0) { 14967c478bd9Sstevel@tonic-gate zerror(gettext("'%s' terminated by signal SIG%s."), cmd, 14977c478bd9Sstevel@tonic-gate sigstr); 14987c478bd9Sstevel@tonic-gate } else { 14997c478bd9Sstevel@tonic-gate zerror(gettext("'%s' terminated by an unknown signal."), 15007c478bd9Sstevel@tonic-gate cmd); 15017c478bd9Sstevel@tonic-gate } 15027c478bd9Sstevel@tonic-gate } else { 15037c478bd9Sstevel@tonic-gate zerror(gettext("'%s' failed for unknown reasons."), cmd); 15047c478bd9Sstevel@tonic-gate } 15057c478bd9Sstevel@tonic-gate return (Z_ERR); 15067c478bd9Sstevel@tonic-gate } 15077c478bd9Sstevel@tonic-gate 15087c478bd9Sstevel@tonic-gate /* 15097c478bd9Sstevel@tonic-gate * Various sanity checks; make sure: 15107c478bd9Sstevel@tonic-gate * 1. We're in the global zone. 15117c478bd9Sstevel@tonic-gate * 2. The calling user has sufficient privilege. 15127c478bd9Sstevel@tonic-gate * 3. The target zone is neither the global zone nor anything starting with 15137c478bd9Sstevel@tonic-gate * "SUNW". 15147c478bd9Sstevel@tonic-gate * 4a. If we're looking for a 'not running' (i.e., configured or installed) 15157c478bd9Sstevel@tonic-gate * zone, the name service knows about it. 15167c478bd9Sstevel@tonic-gate * 4b. For some operations which expect a zone not to be running, that it is 15177c478bd9Sstevel@tonic-gate * not already running (or ready). 15187c478bd9Sstevel@tonic-gate */ 15197c478bd9Sstevel@tonic-gate static int 15207c478bd9Sstevel@tonic-gate sanity_check(char *zone, int cmd_num, boolean_t running, 15217c478bd9Sstevel@tonic-gate boolean_t unsafe_when_running) 15227c478bd9Sstevel@tonic-gate { 15237c478bd9Sstevel@tonic-gate zone_entry_t *zent; 15247c478bd9Sstevel@tonic-gate priv_set_t *privset; 15257c478bd9Sstevel@tonic-gate zone_state_t state; 1526108322fbScarlsonj char kernzone[ZONENAME_MAX]; 1527108322fbScarlsonj FILE *fp; 15287c478bd9Sstevel@tonic-gate 15297c478bd9Sstevel@tonic-gate if (getzoneid() != GLOBAL_ZONEID) { 15307c478bd9Sstevel@tonic-gate zerror(gettext("must be in the global zone to %s a zone."), 15317c478bd9Sstevel@tonic-gate cmd_to_str(cmd_num)); 15327c478bd9Sstevel@tonic-gate return (Z_ERR); 15337c478bd9Sstevel@tonic-gate } 15347c478bd9Sstevel@tonic-gate 15357c478bd9Sstevel@tonic-gate if ((privset = priv_allocset()) == NULL) { 15367c478bd9Sstevel@tonic-gate zerror(gettext("%s failed"), "priv_allocset"); 15377c478bd9Sstevel@tonic-gate return (Z_ERR); 15387c478bd9Sstevel@tonic-gate } 15397c478bd9Sstevel@tonic-gate 15407c478bd9Sstevel@tonic-gate if (getppriv(PRIV_EFFECTIVE, privset) != 0) { 15417c478bd9Sstevel@tonic-gate zerror(gettext("%s failed"), "getppriv"); 15427c478bd9Sstevel@tonic-gate priv_freeset(privset); 15437c478bd9Sstevel@tonic-gate return (Z_ERR); 15447c478bd9Sstevel@tonic-gate } 15457c478bd9Sstevel@tonic-gate 15467c478bd9Sstevel@tonic-gate if (priv_isfullset(privset) == B_FALSE) { 15477c478bd9Sstevel@tonic-gate zerror(gettext("only a privileged user may %s a zone."), 15487c478bd9Sstevel@tonic-gate cmd_to_str(cmd_num)); 15497c478bd9Sstevel@tonic-gate priv_freeset(privset); 15507c478bd9Sstevel@tonic-gate return (Z_ERR); 15517c478bd9Sstevel@tonic-gate } 15527c478bd9Sstevel@tonic-gate priv_freeset(privset); 15537c478bd9Sstevel@tonic-gate 15547c478bd9Sstevel@tonic-gate if (zone == NULL) { 15557c478bd9Sstevel@tonic-gate zerror(gettext("no zone specified")); 15567c478bd9Sstevel@tonic-gate return (Z_ERR); 15577c478bd9Sstevel@tonic-gate } 15587c478bd9Sstevel@tonic-gate 15597c478bd9Sstevel@tonic-gate if (strcmp(zone, GLOBAL_ZONENAME) == 0) { 15607c478bd9Sstevel@tonic-gate zerror(gettext("%s operation is invalid for the global zone."), 15617c478bd9Sstevel@tonic-gate cmd_to_str(cmd_num)); 15627c478bd9Sstevel@tonic-gate return (Z_ERR); 15637c478bd9Sstevel@tonic-gate } 15647c478bd9Sstevel@tonic-gate 15657c478bd9Sstevel@tonic-gate if (strncmp(zone, "SUNW", 4) == 0) { 15667c478bd9Sstevel@tonic-gate zerror(gettext("%s operation is invalid for zones starting " 15677c478bd9Sstevel@tonic-gate "with SUNW."), cmd_to_str(cmd_num)); 15687c478bd9Sstevel@tonic-gate return (Z_ERR); 15697c478bd9Sstevel@tonic-gate } 15707c478bd9Sstevel@tonic-gate 1571108322fbScarlsonj if (!zonecfg_in_alt_root()) { 1572108322fbScarlsonj zent = lookup_running_zone(zone); 1573108322fbScarlsonj } else if ((fp = zonecfg_open_scratch("", B_FALSE)) == NULL) { 1574108322fbScarlsonj zent = NULL; 1575108322fbScarlsonj } else { 1576108322fbScarlsonj if (zonecfg_find_scratch(fp, zone, zonecfg_get_root(), 1577108322fbScarlsonj kernzone, sizeof (kernzone)) == 0) 1578108322fbScarlsonj zent = lookup_running_zone(kernzone); 1579108322fbScarlsonj else 1580108322fbScarlsonj zent = NULL; 1581108322fbScarlsonj zonecfg_close_scratch(fp); 1582108322fbScarlsonj } 1583108322fbScarlsonj 15847c478bd9Sstevel@tonic-gate /* 15857c478bd9Sstevel@tonic-gate * Look up from the kernel for 'running' zones. 15867c478bd9Sstevel@tonic-gate */ 15877c478bd9Sstevel@tonic-gate if (running) { 15887c478bd9Sstevel@tonic-gate if (zent == NULL) { 15897c478bd9Sstevel@tonic-gate zerror(gettext("not running")); 15907c478bd9Sstevel@tonic-gate return (Z_ERR); 15917c478bd9Sstevel@tonic-gate } 15927c478bd9Sstevel@tonic-gate } else { 15937c478bd9Sstevel@tonic-gate int err; 15947c478bd9Sstevel@tonic-gate 15957c478bd9Sstevel@tonic-gate if (unsafe_when_running && zent != NULL) { 15967c478bd9Sstevel@tonic-gate /* check whether the zone is ready or running */ 15977c478bd9Sstevel@tonic-gate if ((err = zone_get_state(zent->zname, 15987c478bd9Sstevel@tonic-gate &zent->zstate_num)) != Z_OK) { 15997c478bd9Sstevel@tonic-gate errno = err; 16007c478bd9Sstevel@tonic-gate zperror2(zent->zname, 16017c478bd9Sstevel@tonic-gate gettext("could not get state")); 16027c478bd9Sstevel@tonic-gate /* can't tell, so hedge */ 16037c478bd9Sstevel@tonic-gate zent->zstate_str = "ready/running"; 16047c478bd9Sstevel@tonic-gate } else { 16057c478bd9Sstevel@tonic-gate zent->zstate_str = 16067c478bd9Sstevel@tonic-gate zone_state_str(zent->zstate_num); 16077c478bd9Sstevel@tonic-gate } 16087c478bd9Sstevel@tonic-gate zerror(gettext("%s operation is invalid for %s zones."), 16097c478bd9Sstevel@tonic-gate cmd_to_str(cmd_num), zent->zstate_str); 16107c478bd9Sstevel@tonic-gate return (Z_ERR); 16117c478bd9Sstevel@tonic-gate } 16127c478bd9Sstevel@tonic-gate if ((err = zone_get_state(zone, &state)) != Z_OK) { 16137c478bd9Sstevel@tonic-gate errno = err; 16147c478bd9Sstevel@tonic-gate zperror2(zone, gettext("could not get state")); 16157c478bd9Sstevel@tonic-gate return (Z_ERR); 16167c478bd9Sstevel@tonic-gate } 16177c478bd9Sstevel@tonic-gate switch (cmd_num) { 16187c478bd9Sstevel@tonic-gate case CMD_UNINSTALL: 16197c478bd9Sstevel@tonic-gate if (state == ZONE_STATE_CONFIGURED) { 16207c478bd9Sstevel@tonic-gate zerror(gettext("is already in state '%s'."), 16217c478bd9Sstevel@tonic-gate zone_state_str(ZONE_STATE_CONFIGURED)); 16227c478bd9Sstevel@tonic-gate return (Z_ERR); 16237c478bd9Sstevel@tonic-gate } 16247c478bd9Sstevel@tonic-gate break; 1625865e09a4Sgjelinek case CMD_CLONE: 16267c478bd9Sstevel@tonic-gate case CMD_INSTALL: 16277c478bd9Sstevel@tonic-gate if (state == ZONE_STATE_INSTALLED) { 16287c478bd9Sstevel@tonic-gate zerror(gettext("is already %s."), 16297c478bd9Sstevel@tonic-gate zone_state_str(ZONE_STATE_INSTALLED)); 16307c478bd9Sstevel@tonic-gate return (Z_ERR); 16317c478bd9Sstevel@tonic-gate } else if (state == ZONE_STATE_INCOMPLETE) { 16327c478bd9Sstevel@tonic-gate zerror(gettext("zone is %s; %s required."), 16337c478bd9Sstevel@tonic-gate zone_state_str(ZONE_STATE_INCOMPLETE), 16347c478bd9Sstevel@tonic-gate cmd_to_str(CMD_UNINSTALL)); 16357c478bd9Sstevel@tonic-gate return (Z_ERR); 16367c478bd9Sstevel@tonic-gate } 16377c478bd9Sstevel@tonic-gate break; 1638865e09a4Sgjelinek case CMD_MOVE: 16397c478bd9Sstevel@tonic-gate case CMD_READY: 16407c478bd9Sstevel@tonic-gate case CMD_BOOT: 1641108322fbScarlsonj case CMD_MOUNT: 16427c478bd9Sstevel@tonic-gate if (state < ZONE_STATE_INSTALLED) { 16437c478bd9Sstevel@tonic-gate zerror(gettext("must be %s before %s."), 16447c478bd9Sstevel@tonic-gate zone_state_str(ZONE_STATE_INSTALLED), 16457c478bd9Sstevel@tonic-gate cmd_to_str(cmd_num)); 16467c478bd9Sstevel@tonic-gate return (Z_ERR); 16477c478bd9Sstevel@tonic-gate } 16487c478bd9Sstevel@tonic-gate break; 16497c478bd9Sstevel@tonic-gate case CMD_VERIFY: 16507c478bd9Sstevel@tonic-gate if (state == ZONE_STATE_INCOMPLETE) { 16517c478bd9Sstevel@tonic-gate zerror(gettext("zone is %s; %s required."), 16527c478bd9Sstevel@tonic-gate zone_state_str(ZONE_STATE_INCOMPLETE), 16537c478bd9Sstevel@tonic-gate cmd_to_str(CMD_UNINSTALL)); 16547c478bd9Sstevel@tonic-gate return (Z_ERR); 16557c478bd9Sstevel@tonic-gate } 16567c478bd9Sstevel@tonic-gate break; 1657108322fbScarlsonj case CMD_UNMOUNT: 1658108322fbScarlsonj if (state != ZONE_STATE_MOUNTED) { 1659108322fbScarlsonj zerror(gettext("must be %s before %s."), 1660108322fbScarlsonj zone_state_str(ZONE_STATE_MOUNTED), 1661108322fbScarlsonj cmd_to_str(cmd_num)); 1662108322fbScarlsonj return (Z_ERR); 1663108322fbScarlsonj } 1664108322fbScarlsonj break; 16657c478bd9Sstevel@tonic-gate } 16667c478bd9Sstevel@tonic-gate } 16677c478bd9Sstevel@tonic-gate return (Z_OK); 16687c478bd9Sstevel@tonic-gate } 16697c478bd9Sstevel@tonic-gate 16707c478bd9Sstevel@tonic-gate static int 16717c478bd9Sstevel@tonic-gate halt_func(int argc, char *argv[]) 16727c478bd9Sstevel@tonic-gate { 16737c478bd9Sstevel@tonic-gate zone_cmd_arg_t zarg; 16747c478bd9Sstevel@tonic-gate int arg; 16757c478bd9Sstevel@tonic-gate 1676108322fbScarlsonj if (zonecfg_in_alt_root()) { 1677108322fbScarlsonj zerror(gettext("cannot halt zone in alternate root")); 1678108322fbScarlsonj return (Z_ERR); 1679108322fbScarlsonj } 1680108322fbScarlsonj 16817c478bd9Sstevel@tonic-gate optind = 0; 16827c478bd9Sstevel@tonic-gate if ((arg = getopt(argc, argv, "?")) != EOF) { 16837c478bd9Sstevel@tonic-gate switch (arg) { 16847c478bd9Sstevel@tonic-gate case '?': 16857c478bd9Sstevel@tonic-gate sub_usage(SHELP_HALT, CMD_HALT); 16867c478bd9Sstevel@tonic-gate return (optopt == '?' ? Z_OK : Z_USAGE); 16877c478bd9Sstevel@tonic-gate default: 16887c478bd9Sstevel@tonic-gate sub_usage(SHELP_HALT, CMD_HALT); 16897c478bd9Sstevel@tonic-gate return (Z_USAGE); 16907c478bd9Sstevel@tonic-gate } 16917c478bd9Sstevel@tonic-gate } 16927c478bd9Sstevel@tonic-gate if (argc > optind) { 16937c478bd9Sstevel@tonic-gate sub_usage(SHELP_HALT, CMD_HALT); 16947c478bd9Sstevel@tonic-gate return (Z_USAGE); 16957c478bd9Sstevel@tonic-gate } 16967c478bd9Sstevel@tonic-gate /* 16977c478bd9Sstevel@tonic-gate * zoneadmd should be the one to decide whether or not to proceed, 16987c478bd9Sstevel@tonic-gate * so even though it seems that the fourth parameter below should 16997c478bd9Sstevel@tonic-gate * perhaps be B_TRUE, it really shouldn't be. 17007c478bd9Sstevel@tonic-gate */ 17017c478bd9Sstevel@tonic-gate if (sanity_check(target_zone, CMD_HALT, B_FALSE, B_FALSE) != Z_OK) 17027c478bd9Sstevel@tonic-gate return (Z_ERR); 17037c478bd9Sstevel@tonic-gate 17047c478bd9Sstevel@tonic-gate zarg.cmd = Z_HALT; 17057c478bd9Sstevel@tonic-gate return ((call_zoneadmd(target_zone, &zarg) == 0) ? Z_OK : Z_ERR); 17067c478bd9Sstevel@tonic-gate } 17077c478bd9Sstevel@tonic-gate 17087c478bd9Sstevel@tonic-gate static int 17097c478bd9Sstevel@tonic-gate reboot_func(int argc, char *argv[]) 17107c478bd9Sstevel@tonic-gate { 17117c478bd9Sstevel@tonic-gate zone_cmd_arg_t zarg; 17127c478bd9Sstevel@tonic-gate int arg; 17137c478bd9Sstevel@tonic-gate 1714108322fbScarlsonj if (zonecfg_in_alt_root()) { 1715108322fbScarlsonj zerror(gettext("cannot reboot zone in alternate root")); 1716108322fbScarlsonj return (Z_ERR); 1717108322fbScarlsonj } 1718108322fbScarlsonj 17197c478bd9Sstevel@tonic-gate optind = 0; 17207c478bd9Sstevel@tonic-gate if ((arg = getopt(argc, argv, "?")) != EOF) { 17217c478bd9Sstevel@tonic-gate switch (arg) { 17227c478bd9Sstevel@tonic-gate case '?': 17237c478bd9Sstevel@tonic-gate sub_usage(SHELP_REBOOT, CMD_REBOOT); 17247c478bd9Sstevel@tonic-gate return (optopt == '?' ? Z_OK : Z_USAGE); 17257c478bd9Sstevel@tonic-gate default: 17267c478bd9Sstevel@tonic-gate sub_usage(SHELP_REBOOT, CMD_REBOOT); 17277c478bd9Sstevel@tonic-gate return (Z_USAGE); 17287c478bd9Sstevel@tonic-gate } 17297c478bd9Sstevel@tonic-gate } 17307c478bd9Sstevel@tonic-gate if (argc > 0) { 17317c478bd9Sstevel@tonic-gate sub_usage(SHELP_REBOOT, CMD_REBOOT); 17327c478bd9Sstevel@tonic-gate return (Z_USAGE); 17337c478bd9Sstevel@tonic-gate } 17347c478bd9Sstevel@tonic-gate /* 17357c478bd9Sstevel@tonic-gate * zoneadmd should be the one to decide whether or not to proceed, 17367c478bd9Sstevel@tonic-gate * so even though it seems that the fourth parameter below should 17377c478bd9Sstevel@tonic-gate * perhaps be B_TRUE, it really shouldn't be. 17387c478bd9Sstevel@tonic-gate */ 17397c478bd9Sstevel@tonic-gate if (sanity_check(target_zone, CMD_REBOOT, B_TRUE, B_FALSE) != Z_OK) 17407c478bd9Sstevel@tonic-gate return (Z_ERR); 1741b5abaf04Sgjelinek if (verify_details(CMD_REBOOT) != Z_OK) 1742b5abaf04Sgjelinek return (Z_ERR); 17437c478bd9Sstevel@tonic-gate 17447c478bd9Sstevel@tonic-gate zarg.cmd = Z_REBOOT; 17457c478bd9Sstevel@tonic-gate return ((call_zoneadmd(target_zone, &zarg) == 0) ? Z_OK : Z_ERR); 17467c478bd9Sstevel@tonic-gate } 17477c478bd9Sstevel@tonic-gate 17487c478bd9Sstevel@tonic-gate static int 17497c478bd9Sstevel@tonic-gate verify_rctls(zone_dochandle_t handle) 17507c478bd9Sstevel@tonic-gate { 17517c478bd9Sstevel@tonic-gate struct zone_rctltab rctltab; 17527c478bd9Sstevel@tonic-gate size_t rbs = rctlblk_size(); 17537c478bd9Sstevel@tonic-gate rctlblk_t *rctlblk; 17547c478bd9Sstevel@tonic-gate int error = Z_INVAL; 17557c478bd9Sstevel@tonic-gate 17567c478bd9Sstevel@tonic-gate if ((rctlblk = malloc(rbs)) == NULL) { 17577c478bd9Sstevel@tonic-gate zerror(gettext("failed to allocate %lu bytes: %s"), rbs, 17587c478bd9Sstevel@tonic-gate strerror(errno)); 17597c478bd9Sstevel@tonic-gate return (Z_NOMEM); 17607c478bd9Sstevel@tonic-gate } 17617c478bd9Sstevel@tonic-gate 17627c478bd9Sstevel@tonic-gate if (zonecfg_setrctlent(handle) != Z_OK) { 17637c478bd9Sstevel@tonic-gate zerror(gettext("zonecfg_setrctlent failed")); 17647c478bd9Sstevel@tonic-gate free(rctlblk); 17657c478bd9Sstevel@tonic-gate return (error); 17667c478bd9Sstevel@tonic-gate } 17677c478bd9Sstevel@tonic-gate 17687c478bd9Sstevel@tonic-gate rctltab.zone_rctl_valptr = NULL; 17697c478bd9Sstevel@tonic-gate while (zonecfg_getrctlent(handle, &rctltab) == Z_OK) { 17707c478bd9Sstevel@tonic-gate struct zone_rctlvaltab *rctlval; 17717c478bd9Sstevel@tonic-gate const char *name = rctltab.zone_rctl_name; 17727c478bd9Sstevel@tonic-gate 17737c478bd9Sstevel@tonic-gate if (!zonecfg_is_rctl(name)) { 17747c478bd9Sstevel@tonic-gate zerror(gettext("WARNING: Ignoring unrecognized rctl " 17757c478bd9Sstevel@tonic-gate "'%s'."), name); 17767c478bd9Sstevel@tonic-gate zonecfg_free_rctl_value_list(rctltab.zone_rctl_valptr); 17777c478bd9Sstevel@tonic-gate rctltab.zone_rctl_valptr = NULL; 17787c478bd9Sstevel@tonic-gate continue; 17797c478bd9Sstevel@tonic-gate } 17807c478bd9Sstevel@tonic-gate 17817c478bd9Sstevel@tonic-gate for (rctlval = rctltab.zone_rctl_valptr; rctlval != NULL; 17827c478bd9Sstevel@tonic-gate rctlval = rctlval->zone_rctlval_next) { 17837c478bd9Sstevel@tonic-gate if (zonecfg_construct_rctlblk(rctlval, rctlblk) 17847c478bd9Sstevel@tonic-gate != Z_OK) { 17857c478bd9Sstevel@tonic-gate zerror(gettext("invalid rctl value: " 17867c478bd9Sstevel@tonic-gate "(priv=%s,limit=%s,action%s)"), 17877c478bd9Sstevel@tonic-gate rctlval->zone_rctlval_priv, 17887c478bd9Sstevel@tonic-gate rctlval->zone_rctlval_limit, 17897c478bd9Sstevel@tonic-gate rctlval->zone_rctlval_action); 17907c478bd9Sstevel@tonic-gate goto out; 17917c478bd9Sstevel@tonic-gate } 17927c478bd9Sstevel@tonic-gate if (!zonecfg_valid_rctl(name, rctlblk)) { 17937c478bd9Sstevel@tonic-gate zerror(gettext("(priv=%s,limit=%s,action=%s) " 17947c478bd9Sstevel@tonic-gate "is not a valid value for rctl '%s'"), 17957c478bd9Sstevel@tonic-gate rctlval->zone_rctlval_priv, 17967c478bd9Sstevel@tonic-gate rctlval->zone_rctlval_limit, 17977c478bd9Sstevel@tonic-gate rctlval->zone_rctlval_action, 17987c478bd9Sstevel@tonic-gate name); 17997c478bd9Sstevel@tonic-gate goto out; 18007c478bd9Sstevel@tonic-gate } 18017c478bd9Sstevel@tonic-gate } 18027c478bd9Sstevel@tonic-gate zonecfg_free_rctl_value_list(rctltab.zone_rctl_valptr); 18037c478bd9Sstevel@tonic-gate } 18047c478bd9Sstevel@tonic-gate rctltab.zone_rctl_valptr = NULL; 18057c478bd9Sstevel@tonic-gate error = Z_OK; 18067c478bd9Sstevel@tonic-gate out: 18077c478bd9Sstevel@tonic-gate zonecfg_free_rctl_value_list(rctltab.zone_rctl_valptr); 18087c478bd9Sstevel@tonic-gate (void) zonecfg_endrctlent(handle); 18097c478bd9Sstevel@tonic-gate free(rctlblk); 18107c478bd9Sstevel@tonic-gate return (error); 18117c478bd9Sstevel@tonic-gate } 18127c478bd9Sstevel@tonic-gate 18137c478bd9Sstevel@tonic-gate static int 18147c478bd9Sstevel@tonic-gate verify_pool(zone_dochandle_t handle) 18157c478bd9Sstevel@tonic-gate { 18167c478bd9Sstevel@tonic-gate char poolname[MAXPATHLEN]; 18177c478bd9Sstevel@tonic-gate pool_conf_t *poolconf; 18187c478bd9Sstevel@tonic-gate pool_t *pool; 18197c478bd9Sstevel@tonic-gate int status; 18207c478bd9Sstevel@tonic-gate int error; 18217c478bd9Sstevel@tonic-gate 18227c478bd9Sstevel@tonic-gate /* 18237c478bd9Sstevel@tonic-gate * This ends up being very similar to the check done in zoneadmd. 18247c478bd9Sstevel@tonic-gate */ 18257c478bd9Sstevel@tonic-gate error = zonecfg_get_pool(handle, poolname, sizeof (poolname)); 18267c478bd9Sstevel@tonic-gate if (error == Z_NO_ENTRY || (error == Z_OK && strlen(poolname) == 0)) { 18277c478bd9Sstevel@tonic-gate /* 18287c478bd9Sstevel@tonic-gate * No pool specified. 18297c478bd9Sstevel@tonic-gate */ 18307c478bd9Sstevel@tonic-gate return (0); 18317c478bd9Sstevel@tonic-gate } 18327c478bd9Sstevel@tonic-gate if (error != Z_OK) { 18337c478bd9Sstevel@tonic-gate zperror(gettext("Unable to retrieve pool name from " 18347c478bd9Sstevel@tonic-gate "configuration"), B_TRUE); 18357c478bd9Sstevel@tonic-gate return (error); 18367c478bd9Sstevel@tonic-gate } 18377c478bd9Sstevel@tonic-gate /* 18387c478bd9Sstevel@tonic-gate * Don't do anything if pools aren't enabled. 18397c478bd9Sstevel@tonic-gate */ 18407c478bd9Sstevel@tonic-gate if (pool_get_status(&status) != PO_SUCCESS || status != POOL_ENABLED) { 18417c478bd9Sstevel@tonic-gate zerror(gettext("WARNING: pools facility not active; " 18427c478bd9Sstevel@tonic-gate "zone will not be bound to pool '%s'."), poolname); 18437c478bd9Sstevel@tonic-gate return (Z_OK); 18447c478bd9Sstevel@tonic-gate } 18457c478bd9Sstevel@tonic-gate /* 18467c478bd9Sstevel@tonic-gate * Try to provide a sane error message if the requested pool doesn't 18477c478bd9Sstevel@tonic-gate * exist. It isn't clear that pools-related failures should 18487c478bd9Sstevel@tonic-gate * necessarily translate to a failure to verify the zone configuration, 18497c478bd9Sstevel@tonic-gate * hence they are not considered errors. 18507c478bd9Sstevel@tonic-gate */ 18517c478bd9Sstevel@tonic-gate if ((poolconf = pool_conf_alloc()) == NULL) { 18527c478bd9Sstevel@tonic-gate zerror(gettext("WARNING: pool_conf_alloc failed; " 18537c478bd9Sstevel@tonic-gate "using default pool")); 18547c478bd9Sstevel@tonic-gate return (Z_OK); 18557c478bd9Sstevel@tonic-gate } 18567c478bd9Sstevel@tonic-gate if (pool_conf_open(poolconf, pool_dynamic_location(), PO_RDONLY) != 18577c478bd9Sstevel@tonic-gate PO_SUCCESS) { 18587c478bd9Sstevel@tonic-gate zerror(gettext("WARNING: pool_conf_open failed; " 18597c478bd9Sstevel@tonic-gate "using default pool")); 18607c478bd9Sstevel@tonic-gate pool_conf_free(poolconf); 18617c478bd9Sstevel@tonic-gate return (Z_OK); 18627c478bd9Sstevel@tonic-gate } 18637c478bd9Sstevel@tonic-gate pool = pool_get_pool(poolconf, poolname); 18647c478bd9Sstevel@tonic-gate (void) pool_conf_close(poolconf); 18657c478bd9Sstevel@tonic-gate pool_conf_free(poolconf); 18667c478bd9Sstevel@tonic-gate if (pool == NULL) { 18677c478bd9Sstevel@tonic-gate zerror(gettext("WARNING: pool '%s' not found. " 18687c478bd9Sstevel@tonic-gate "using default pool"), poolname); 18697c478bd9Sstevel@tonic-gate } 18707c478bd9Sstevel@tonic-gate 18717c478bd9Sstevel@tonic-gate return (Z_OK); 18727c478bd9Sstevel@tonic-gate } 18737c478bd9Sstevel@tonic-gate 18747c478bd9Sstevel@tonic-gate static int 1875b5abaf04Sgjelinek verify_ipd(zone_dochandle_t handle) 1876b5abaf04Sgjelinek { 1877b5abaf04Sgjelinek int return_code = Z_OK; 1878b5abaf04Sgjelinek struct zone_fstab fstab; 1879b5abaf04Sgjelinek struct stat st; 1880b5abaf04Sgjelinek char specdir[MAXPATHLEN]; 1881b5abaf04Sgjelinek 1882b5abaf04Sgjelinek if (zonecfg_setipdent(handle) != Z_OK) { 18835ee84fbdSgjelinek /* 18845ee84fbdSgjelinek * TRANSLATION_NOTE 18855ee84fbdSgjelinek * inherit-pkg-dirs is a literal that should not be translated. 18865ee84fbdSgjelinek */ 18875ee84fbdSgjelinek (void) fprintf(stderr, gettext("could not verify " 1888b5abaf04Sgjelinek "inherit-pkg-dirs: unable to enumerate mounts\n")); 1889b5abaf04Sgjelinek return (Z_ERR); 1890b5abaf04Sgjelinek } 1891b5abaf04Sgjelinek while (zonecfg_getipdent(handle, &fstab) == Z_OK) { 1892b5abaf04Sgjelinek /* 1893b5abaf04Sgjelinek * Verify fs_dir exists. 1894b5abaf04Sgjelinek */ 1895b5abaf04Sgjelinek (void) snprintf(specdir, sizeof (specdir), "%s%s", 1896b5abaf04Sgjelinek zonecfg_get_root(), fstab.zone_fs_dir); 1897b5abaf04Sgjelinek if (stat(specdir, &st) != 0) { 18985ee84fbdSgjelinek /* 18995ee84fbdSgjelinek * TRANSLATION_NOTE 19005ee84fbdSgjelinek * inherit-pkg-dir is a literal that should not be 19015ee84fbdSgjelinek * translated. 19025ee84fbdSgjelinek */ 19035ee84fbdSgjelinek (void) fprintf(stderr, gettext("could not verify " 1904b5abaf04Sgjelinek "inherit-pkg-dir %s: %s\n"), 1905b5abaf04Sgjelinek fstab.zone_fs_dir, strerror(errno)); 1906b5abaf04Sgjelinek return_code = Z_ERR; 1907b5abaf04Sgjelinek } 1908b5abaf04Sgjelinek if (strcmp(st.st_fstype, MNTTYPE_NFS) == 0) { 19095ee84fbdSgjelinek /* 19105ee84fbdSgjelinek * TRANSLATION_NOTE 19115ee84fbdSgjelinek * inherit-pkg-dir and NFS are literals that should 19125ee84fbdSgjelinek * not be translated. 19135ee84fbdSgjelinek */ 1914b5abaf04Sgjelinek (void) fprintf(stderr, gettext("cannot verify " 19155ee84fbdSgjelinek "inherit-pkg-dir %s: NFS mounted file-system.\n" 19165ee84fbdSgjelinek "\tA local file-system must be used.\n"), 1917b5abaf04Sgjelinek fstab.zone_fs_dir); 1918b5abaf04Sgjelinek return_code = Z_ERR; 1919b5abaf04Sgjelinek } 1920b5abaf04Sgjelinek } 1921b5abaf04Sgjelinek (void) zonecfg_endipdent(handle); 1922b5abaf04Sgjelinek 1923b5abaf04Sgjelinek return (return_code); 1924b5abaf04Sgjelinek } 1925b5abaf04Sgjelinek 192620c8013fSlling /* ARGSUSED */ 192720c8013fSlling static void 192820c8013fSlling zfs_fs_err_handler(const char *fmt, va_list ap) 192920c8013fSlling { 193020c8013fSlling /* 193120c8013fSlling * Do nothing - do not print the libzfs error messages. 193220c8013fSlling */ 193320c8013fSlling } 193420c8013fSlling 193520c8013fSlling /* 193620c8013fSlling * Verify that the ZFS dataset exists, and its mountpoint 193720c8013fSlling * property is set to "legacy". 193820c8013fSlling */ 193920c8013fSlling static int 194020c8013fSlling verify_fs_zfs(struct zone_fstab *fstab) 194120c8013fSlling { 194220c8013fSlling zfs_handle_t *zhp; 194320c8013fSlling char propbuf[ZFS_MAXPROPLEN]; 194420c8013fSlling 194520c8013fSlling zfs_set_error_handler(zfs_fs_err_handler); 194620c8013fSlling 194720c8013fSlling if ((zhp = zfs_open(fstab->zone_fs_special, ZFS_TYPE_ANY)) == NULL) { 1948*5c358068Slling (void) fprintf(stderr, gettext("could not verify fs %s: " 194920c8013fSlling "could not access zfs dataset '%s'\n"), 195020c8013fSlling fstab->zone_fs_dir, fstab->zone_fs_special); 195120c8013fSlling return (Z_ERR); 195220c8013fSlling } 195320c8013fSlling 195420c8013fSlling if (zfs_get_type(zhp) != ZFS_TYPE_FILESYSTEM) { 195520c8013fSlling (void) fprintf(stderr, gettext("cannot verify fs %s: " 195620c8013fSlling "'%s' is not a filesystem\n"), 195720c8013fSlling fstab->zone_fs_dir, fstab->zone_fs_special); 195820c8013fSlling zfs_close(zhp); 195920c8013fSlling return (Z_ERR); 196020c8013fSlling } 196120c8013fSlling 196220c8013fSlling if (zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, propbuf, sizeof (propbuf), 196320c8013fSlling NULL, NULL, 0, 0) != 0 || strcmp(propbuf, "legacy") != 0) { 1964*5c358068Slling (void) fprintf(stderr, gettext("could not verify fs %s: " 1965*5c358068Slling "zfs '%s' mountpoint is not \"legacy\"\n"), 196620c8013fSlling fstab->zone_fs_dir, fstab->zone_fs_special); 196720c8013fSlling zfs_close(zhp); 196820c8013fSlling return (Z_ERR); 196920c8013fSlling } 197020c8013fSlling 197120c8013fSlling zfs_close(zhp); 197220c8013fSlling return (Z_OK); 197320c8013fSlling } 197420c8013fSlling 197520c8013fSlling /* 197620c8013fSlling * Verify that the special device/filesystem exists and is valid. 197720c8013fSlling */ 197820c8013fSlling static int 197920c8013fSlling verify_fs_special(struct zone_fstab *fstab) 198020c8013fSlling { 198120c8013fSlling struct stat st; 198220c8013fSlling 198320c8013fSlling if (strcmp(fstab->zone_fs_type, MNTTYPE_ZFS) == 0) 198420c8013fSlling return (verify_fs_zfs(fstab)); 198520c8013fSlling 198620c8013fSlling if (stat(fstab->zone_fs_special, &st) != 0) { 1987*5c358068Slling (void) fprintf(stderr, gettext("could not verify fs " 198820c8013fSlling "%s: could not access %s: %s\n"), fstab->zone_fs_dir, 198920c8013fSlling fstab->zone_fs_special, strerror(errno)); 199020c8013fSlling return (Z_ERR); 199120c8013fSlling } 199220c8013fSlling 199320c8013fSlling if (strcmp(st.st_fstype, MNTTYPE_NFS) == 0) { 199420c8013fSlling /* 199520c8013fSlling * TRANSLATION_NOTE 199620c8013fSlling * fs and NFS are literals that should 199720c8013fSlling * not be translated. 199820c8013fSlling */ 199920c8013fSlling (void) fprintf(stderr, gettext("cannot verify " 200020c8013fSlling "fs %s: NFS mounted file-system.\n" 200120c8013fSlling "\tA local file-system must be used.\n"), 200220c8013fSlling fstab->zone_fs_special); 200320c8013fSlling return (Z_ERR); 200420c8013fSlling } 200520c8013fSlling 200620c8013fSlling return (Z_OK); 200720c8013fSlling } 200820c8013fSlling 2009b5abaf04Sgjelinek static int 20107c478bd9Sstevel@tonic-gate verify_filesystems(zone_dochandle_t handle) 20117c478bd9Sstevel@tonic-gate { 20127c478bd9Sstevel@tonic-gate int return_code = Z_OK; 20137c478bd9Sstevel@tonic-gate struct zone_fstab fstab; 20147c478bd9Sstevel@tonic-gate char cmdbuf[MAXPATHLEN]; 20157c478bd9Sstevel@tonic-gate struct stat st; 20167c478bd9Sstevel@tonic-gate 20177c478bd9Sstevel@tonic-gate /* 20187c478bd9Sstevel@tonic-gate * No need to verify inherit-pkg-dir fs types, as their type is 20197c478bd9Sstevel@tonic-gate * implicitly lofs, which is known. Therefore, the types are only 20207c478bd9Sstevel@tonic-gate * verified for regular filesystems below. 20217c478bd9Sstevel@tonic-gate * 20227c478bd9Sstevel@tonic-gate * Since the actual mount point is not known until the dependent mounts 20237c478bd9Sstevel@tonic-gate * are performed, we don't attempt any path validation here: that will 20247c478bd9Sstevel@tonic-gate * happen later when zoneadmd actually does the mounts. 20257c478bd9Sstevel@tonic-gate */ 20267c478bd9Sstevel@tonic-gate if (zonecfg_setfsent(handle) != Z_OK) { 20275ee84fbdSgjelinek (void) fprintf(stderr, gettext("could not verify file-systems: " 20287c478bd9Sstevel@tonic-gate "unable to enumerate mounts\n")); 20297c478bd9Sstevel@tonic-gate return (Z_ERR); 20307c478bd9Sstevel@tonic-gate } 20317c478bd9Sstevel@tonic-gate while (zonecfg_getfsent(handle, &fstab) == Z_OK) { 20327c478bd9Sstevel@tonic-gate if (!zonecfg_valid_fs_type(fstab.zone_fs_type)) { 20337c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("cannot verify fs %s: " 20347c478bd9Sstevel@tonic-gate "type %s is not allowed.\n"), fstab.zone_fs_dir, 20357c478bd9Sstevel@tonic-gate fstab.zone_fs_type); 20367c478bd9Sstevel@tonic-gate return_code = Z_ERR; 20377c478bd9Sstevel@tonic-gate goto next_fs; 20387c478bd9Sstevel@tonic-gate } 20397c478bd9Sstevel@tonic-gate /* 20407c478bd9Sstevel@tonic-gate * Verify /usr/lib/fs/<fstype>/mount exists. 20417c478bd9Sstevel@tonic-gate */ 20427c478bd9Sstevel@tonic-gate if (snprintf(cmdbuf, sizeof (cmdbuf), "/usr/lib/fs/%s/mount", 20437c478bd9Sstevel@tonic-gate fstab.zone_fs_type) > sizeof (cmdbuf)) { 20447c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("cannot verify fs %s: " 20457c478bd9Sstevel@tonic-gate "type %s is too long.\n"), fstab.zone_fs_dir, 20467c478bd9Sstevel@tonic-gate fstab.zone_fs_type); 20477c478bd9Sstevel@tonic-gate return_code = Z_ERR; 20487c478bd9Sstevel@tonic-gate goto next_fs; 20497c478bd9Sstevel@tonic-gate } 20507c478bd9Sstevel@tonic-gate if (stat(cmdbuf, &st) != 0) { 20515ee84fbdSgjelinek (void) fprintf(stderr, gettext("could not verify fs " 20525ee84fbdSgjelinek "%s: could not access %s: %s\n"), fstab.zone_fs_dir, 20537c478bd9Sstevel@tonic-gate cmdbuf, strerror(errno)); 20547c478bd9Sstevel@tonic-gate return_code = Z_ERR; 20557c478bd9Sstevel@tonic-gate goto next_fs; 20567c478bd9Sstevel@tonic-gate } 20577c478bd9Sstevel@tonic-gate if (!S_ISREG(st.st_mode)) { 20585ee84fbdSgjelinek (void) fprintf(stderr, gettext("could not verify fs " 20595ee84fbdSgjelinek "%s: %s is not a regular file\n"), 20605ee84fbdSgjelinek fstab.zone_fs_dir, cmdbuf); 20617c478bd9Sstevel@tonic-gate return_code = Z_ERR; 20627c478bd9Sstevel@tonic-gate goto next_fs; 20637c478bd9Sstevel@tonic-gate } 20647c478bd9Sstevel@tonic-gate /* 20657c478bd9Sstevel@tonic-gate * Verify /usr/lib/fs/<fstype>/fsck exists iff zone_fs_raw is 20667c478bd9Sstevel@tonic-gate * set. 20677c478bd9Sstevel@tonic-gate */ 20687c478bd9Sstevel@tonic-gate if (snprintf(cmdbuf, sizeof (cmdbuf), "/usr/lib/fs/%s/fsck", 20697c478bd9Sstevel@tonic-gate fstab.zone_fs_type) > sizeof (cmdbuf)) { 20707c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("cannot verify fs %s: " 20717c478bd9Sstevel@tonic-gate "type %s is too long.\n"), fstab.zone_fs_dir, 20727c478bd9Sstevel@tonic-gate fstab.zone_fs_type); 20737c478bd9Sstevel@tonic-gate return_code = Z_ERR; 20747c478bd9Sstevel@tonic-gate goto next_fs; 20757c478bd9Sstevel@tonic-gate } 20767c478bd9Sstevel@tonic-gate if (fstab.zone_fs_raw[0] == '\0' && stat(cmdbuf, &st) == 0) { 20775ee84fbdSgjelinek (void) fprintf(stderr, gettext("could not verify fs " 20785ee84fbdSgjelinek "%s: must specify 'raw' device for %s " 20795ee84fbdSgjelinek "file-systems\n"), 20807c478bd9Sstevel@tonic-gate fstab.zone_fs_dir, fstab.zone_fs_type); 20817c478bd9Sstevel@tonic-gate return_code = Z_ERR; 20827c478bd9Sstevel@tonic-gate goto next_fs; 20837c478bd9Sstevel@tonic-gate } 20847c478bd9Sstevel@tonic-gate if (fstab.zone_fs_raw[0] != '\0' && 20857c478bd9Sstevel@tonic-gate (stat(cmdbuf, &st) != 0 || !S_ISREG(st.st_mode))) { 20867c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("cannot verify fs %s: " 20877c478bd9Sstevel@tonic-gate "'raw' device specified but " 20887c478bd9Sstevel@tonic-gate "no fsck executable exists for %s\n"), 20897c478bd9Sstevel@tonic-gate fstab.zone_fs_dir, fstab.zone_fs_type); 20907c478bd9Sstevel@tonic-gate return_code = Z_ERR; 20917c478bd9Sstevel@tonic-gate goto next_fs; 20927c478bd9Sstevel@tonic-gate } 209320c8013fSlling 209420c8013fSlling /* Verify fs_special. */ 209520c8013fSlling if ((return_code = verify_fs_special(&fstab)) != Z_OK) 2096b5abaf04Sgjelinek goto next_fs; 209720c8013fSlling 209820c8013fSlling /* Verify fs_raw. */ 2099b5abaf04Sgjelinek if (fstab.zone_fs_raw[0] != '\0' && 2100b5abaf04Sgjelinek stat(fstab.zone_fs_raw, &st) != 0) { 21015ee84fbdSgjelinek /* 21025ee84fbdSgjelinek * TRANSLATION_NOTE 21035ee84fbdSgjelinek * fs is a literal that should not be translated. 21045ee84fbdSgjelinek */ 21055ee84fbdSgjelinek (void) fprintf(stderr, gettext("could not verify fs " 21065ee84fbdSgjelinek "%s: could not access %s: %s\n"), fstab.zone_fs_dir, 2107b5abaf04Sgjelinek fstab.zone_fs_raw, strerror(errno)); 2108b5abaf04Sgjelinek return_code = Z_ERR; 2109b5abaf04Sgjelinek goto next_fs; 2110b5abaf04Sgjelinek } 21117c478bd9Sstevel@tonic-gate next_fs: 21127c478bd9Sstevel@tonic-gate zonecfg_free_fs_option_list(fstab.zone_fs_options); 21137c478bd9Sstevel@tonic-gate } 21147c478bd9Sstevel@tonic-gate (void) zonecfg_endfsent(handle); 21157c478bd9Sstevel@tonic-gate 21167c478bd9Sstevel@tonic-gate return (return_code); 21177c478bd9Sstevel@tonic-gate } 21187c478bd9Sstevel@tonic-gate 2119fa9e4066Sahrens const char *current_dataset; 2120fa9e4066Sahrens 2121fa9e4066Sahrens /* 2122fa9e4066Sahrens * Custom error handler for errors incurred as part of the checks below. We 2123fa9e4066Sahrens * want to trim off the leading 'cannot open ...' to create a better error 2124fa9e4066Sahrens * message. The only other way this can fail is if we fail to set the 'zoned' 2125fa9e4066Sahrens * property. In this case we just pass the error on verbatim. 2126fa9e4066Sahrens */ 2127fa9e4066Sahrens static void 2128fa9e4066Sahrens zfs_error_handler(const char *fmt, va_list ap) 2129fa9e4066Sahrens { 2130fa9e4066Sahrens char buf[1024]; 2131fa9e4066Sahrens 2132fa9e4066Sahrens (void) vsnprintf(buf, sizeof (buf), fmt, ap); 2133fa9e4066Sahrens 2134fa9e4066Sahrens if (strncmp(gettext("cannot open "), buf, 2135fa9e4066Sahrens strlen(gettext("cannot open "))) == 0) 21365ee84fbdSgjelinek /* 21375ee84fbdSgjelinek * TRANSLATION_NOTE 21385ee84fbdSgjelinek * zfs and dataset are literals that should not be translated. 21395ee84fbdSgjelinek */ 21405ee84fbdSgjelinek (void) fprintf(stderr, gettext("could not verify zfs " 2141fa9e4066Sahrens "dataset %s%s\n"), current_dataset, strchr(buf, ':')); 2142fa9e4066Sahrens else 21435ee84fbdSgjelinek (void) fprintf(stderr, gettext("could not verify zfs dataset " 2144fa9e4066Sahrens "%s: %s\n"), current_dataset, buf); 2145fa9e4066Sahrens } 2146fa9e4066Sahrens 2147fa9e4066Sahrens /* ARGSUSED */ 2148fa9e4066Sahrens static int 2149fa9e4066Sahrens check_zvol(zfs_handle_t *zhp, void *unused) 2150fa9e4066Sahrens { 2151fa9e4066Sahrens int ret; 2152fa9e4066Sahrens 2153fa9e4066Sahrens if (zfs_get_type(zhp) == ZFS_TYPE_VOLUME) { 21545ee84fbdSgjelinek /* 21555ee84fbdSgjelinek * TRANSLATION_NOTE 21565ee84fbdSgjelinek * zfs and dataset are literals that should not be translated. 21575ee84fbdSgjelinek */ 2158fa9e4066Sahrens (void) fprintf(stderr, gettext("cannot verify zfs dataset %s: " 2159fa9e4066Sahrens "volumes cannot be specified as a zone dataset resource\n"), 2160fa9e4066Sahrens zfs_get_name(zhp)); 2161fa9e4066Sahrens ret = -1; 2162fa9e4066Sahrens } else { 2163fa9e4066Sahrens ret = zfs_iter_children(zhp, check_zvol, NULL); 2164fa9e4066Sahrens } 2165fa9e4066Sahrens 2166fa9e4066Sahrens zfs_close(zhp); 2167fa9e4066Sahrens 2168fa9e4066Sahrens return (ret); 2169fa9e4066Sahrens } 2170fa9e4066Sahrens 2171fa9e4066Sahrens /* 2172fa9e4066Sahrens * Validate that the given dataset exists on the system, and that neither it nor 2173fa9e4066Sahrens * its children are zvols. 2174fa9e4066Sahrens * 2175fa9e4066Sahrens * Note that we don't do anything with the 'zoned' property here. All 2176fa9e4066Sahrens * management is done in zoneadmd when the zone is actually rebooted. This 2177fa9e4066Sahrens * allows us to automatically set the zoned property even when a zone is 2178fa9e4066Sahrens * rebooted by the administrator. 2179fa9e4066Sahrens */ 2180fa9e4066Sahrens static int 2181fa9e4066Sahrens verify_datasets(zone_dochandle_t handle) 2182fa9e4066Sahrens { 2183fa9e4066Sahrens int return_code = Z_OK; 2184fa9e4066Sahrens struct zone_dstab dstab; 2185fa9e4066Sahrens zfs_handle_t *zhp; 2186fa9e4066Sahrens char propbuf[ZFS_MAXPROPLEN]; 2187fa9e4066Sahrens char source[ZFS_MAXNAMELEN]; 2188fa9e4066Sahrens zfs_source_t srctype; 2189fa9e4066Sahrens 2190fa9e4066Sahrens if (zonecfg_setdsent(handle) != Z_OK) { 21915ee84fbdSgjelinek /* 21925ee84fbdSgjelinek * TRANSLATION_NOTE 21935ee84fbdSgjelinek * zfs and dataset are literals that should not be translated. 21945ee84fbdSgjelinek */ 21955ee84fbdSgjelinek (void) fprintf(stderr, gettext("could not verify zfs datasets: " 2196fa9e4066Sahrens "unable to enumerate datasets\n")); 2197fa9e4066Sahrens return (Z_ERR); 2198fa9e4066Sahrens } 2199fa9e4066Sahrens 2200fa9e4066Sahrens zfs_set_error_handler(zfs_error_handler); 2201fa9e4066Sahrens 2202fa9e4066Sahrens while (zonecfg_getdsent(handle, &dstab) == Z_OK) { 2203fa9e4066Sahrens 2204fa9e4066Sahrens current_dataset = dstab.zone_dataset_name; 2205fa9e4066Sahrens 2206fa9e4066Sahrens if ((zhp = zfs_open(dstab.zone_dataset_name, 2207fa9e4066Sahrens ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME)) == NULL) { 2208fa9e4066Sahrens return_code = Z_ERR; 2209fa9e4066Sahrens continue; 2210fa9e4066Sahrens } 2211fa9e4066Sahrens 2212fa9e4066Sahrens if (zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, propbuf, 2213fa9e4066Sahrens sizeof (propbuf), &srctype, source, 2214fa9e4066Sahrens sizeof (source), 0) == 0 && 2215fa9e4066Sahrens (srctype == ZFS_SRC_INHERITED)) { 22165ee84fbdSgjelinek (void) fprintf(stderr, gettext("could not verify zfs " 2217fa9e4066Sahrens "dataset %s: mountpoint cannot be inherited\n"), 2218fa9e4066Sahrens dstab.zone_dataset_name); 2219fa9e4066Sahrens return_code = Z_ERR; 2220fa9e4066Sahrens zfs_close(zhp); 2221fa9e4066Sahrens continue; 2222fa9e4066Sahrens } 2223fa9e4066Sahrens 2224fa9e4066Sahrens if (zfs_get_type(zhp) == ZFS_TYPE_VOLUME) { 2225fa9e4066Sahrens (void) fprintf(stderr, gettext("cannot verify zfs " 2226fa9e4066Sahrens "dataset %s: volumes cannot be specified as a " 2227fa9e4066Sahrens "zone dataset resource\n"), 2228fa9e4066Sahrens dstab.zone_dataset_name); 2229fa9e4066Sahrens return_code = Z_ERR; 2230fa9e4066Sahrens } 2231fa9e4066Sahrens 2232fa9e4066Sahrens if (zfs_iter_children(zhp, check_zvol, NULL) != 0) 2233fa9e4066Sahrens return_code = Z_ERR; 2234fa9e4066Sahrens 2235fa9e4066Sahrens zfs_close(zhp); 2236fa9e4066Sahrens } 2237fa9e4066Sahrens (void) zonecfg_enddsent(handle); 2238fa9e4066Sahrens 2239fa9e4066Sahrens return (return_code); 2240fa9e4066Sahrens } 2241fa9e4066Sahrens 22427c478bd9Sstevel@tonic-gate static int 22437c478bd9Sstevel@tonic-gate verify_details(int cmd_num) 22447c478bd9Sstevel@tonic-gate { 22457c478bd9Sstevel@tonic-gate zone_dochandle_t handle; 22467c478bd9Sstevel@tonic-gate struct zone_nwiftab nwiftab; 22477c478bd9Sstevel@tonic-gate char zonepath[MAXPATHLEN], checkpath[MAXPATHLEN]; 22487c478bd9Sstevel@tonic-gate int return_code = Z_OK; 22497c478bd9Sstevel@tonic-gate int err; 2250108322fbScarlsonj boolean_t in_alt_root; 22517c478bd9Sstevel@tonic-gate 22527c478bd9Sstevel@tonic-gate if ((handle = zonecfg_init_handle()) == NULL) { 22537c478bd9Sstevel@tonic-gate zperror(cmd_to_str(cmd_num), B_TRUE); 22547c478bd9Sstevel@tonic-gate return (Z_ERR); 22557c478bd9Sstevel@tonic-gate } 22567c478bd9Sstevel@tonic-gate if ((err = zonecfg_get_handle(target_zone, handle)) != Z_OK) { 22577c478bd9Sstevel@tonic-gate errno = err; 22587c478bd9Sstevel@tonic-gate zperror(cmd_to_str(cmd_num), B_TRUE); 22597c478bd9Sstevel@tonic-gate zonecfg_fini_handle(handle); 22607c478bd9Sstevel@tonic-gate return (Z_ERR); 22617c478bd9Sstevel@tonic-gate } 22627c478bd9Sstevel@tonic-gate if ((err = zonecfg_get_zonepath(handle, zonepath, sizeof (zonepath))) != 22637c478bd9Sstevel@tonic-gate Z_OK) { 22647c478bd9Sstevel@tonic-gate errno = err; 22657c478bd9Sstevel@tonic-gate zperror(cmd_to_str(cmd_num), B_TRUE); 22667c478bd9Sstevel@tonic-gate zonecfg_fini_handle(handle); 22677c478bd9Sstevel@tonic-gate return (Z_ERR); 22687c478bd9Sstevel@tonic-gate } 22697c478bd9Sstevel@tonic-gate /* 22707c478bd9Sstevel@tonic-gate * zonecfg_get_zonepath() gets its data from the XML repository. 22717c478bd9Sstevel@tonic-gate * Verify this against the index file, which is checked first by 22727c478bd9Sstevel@tonic-gate * zone_get_zonepath(). If they don't match, bail out. 22737c478bd9Sstevel@tonic-gate */ 22747c478bd9Sstevel@tonic-gate if ((err = zone_get_zonepath(target_zone, checkpath, 22757c478bd9Sstevel@tonic-gate sizeof (checkpath))) != Z_OK) { 22767c478bd9Sstevel@tonic-gate errno = err; 22777c478bd9Sstevel@tonic-gate zperror2(target_zone, gettext("could not get zone path")); 22787c478bd9Sstevel@tonic-gate return (Z_ERR); 22797c478bd9Sstevel@tonic-gate } 22807c478bd9Sstevel@tonic-gate if (strcmp(zonepath, checkpath) != 0) { 22815ee84fbdSgjelinek /* 22825ee84fbdSgjelinek * TRANSLATION_NOTE 22835ee84fbdSgjelinek * XML and zonepath are literals that should not be translated. 22845ee84fbdSgjelinek */ 22857c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("The XML repository has " 22867c478bd9Sstevel@tonic-gate "zonepath '%s',\nbut the index file has zonepath '%s'.\n" 22877c478bd9Sstevel@tonic-gate "These must match, so fix the incorrect entry.\n"), 22887c478bd9Sstevel@tonic-gate zonepath, checkpath); 22897c478bd9Sstevel@tonic-gate return (Z_ERR); 22907c478bd9Sstevel@tonic-gate } 22917c478bd9Sstevel@tonic-gate if (validate_zonepath(zonepath, cmd_num) != Z_OK) { 22927c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("could not verify zonepath %s " 22937c478bd9Sstevel@tonic-gate "because of the above errors.\n"), zonepath); 22947c478bd9Sstevel@tonic-gate return_code = Z_ERR; 22957c478bd9Sstevel@tonic-gate } 22967c478bd9Sstevel@tonic-gate 2297108322fbScarlsonj in_alt_root = zonecfg_in_alt_root(); 2298108322fbScarlsonj if (in_alt_root) 2299108322fbScarlsonj goto no_net; 2300108322fbScarlsonj 23017c478bd9Sstevel@tonic-gate if ((err = zonecfg_setnwifent(handle)) != Z_OK) { 23027c478bd9Sstevel@tonic-gate errno = err; 23037c478bd9Sstevel@tonic-gate zperror(cmd_to_str(cmd_num), B_TRUE); 23047c478bd9Sstevel@tonic-gate zonecfg_fini_handle(handle); 23057c478bd9Sstevel@tonic-gate return (Z_ERR); 23067c478bd9Sstevel@tonic-gate } 23077c478bd9Sstevel@tonic-gate while (zonecfg_getnwifent(handle, &nwiftab) == Z_OK) { 23087c478bd9Sstevel@tonic-gate struct lifreq lifr; 23097c478bd9Sstevel@tonic-gate sa_family_t af; 23107c478bd9Sstevel@tonic-gate int so, res; 23117c478bd9Sstevel@tonic-gate 23127c478bd9Sstevel@tonic-gate /* skip any loopback interfaces */ 23137c478bd9Sstevel@tonic-gate if (strcmp(nwiftab.zone_nwif_physical, "lo0") == 0) 23147c478bd9Sstevel@tonic-gate continue; 23157c478bd9Sstevel@tonic-gate if ((res = zonecfg_valid_net_address(nwiftab.zone_nwif_address, 23167c478bd9Sstevel@tonic-gate &lifr)) != Z_OK) { 23177c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("could not verify %s " 23187c478bd9Sstevel@tonic-gate "%s=%s %s=%s: %s\n"), "net", "address", 23197c478bd9Sstevel@tonic-gate nwiftab.zone_nwif_address, "physical", 23207c478bd9Sstevel@tonic-gate nwiftab.zone_nwif_physical, zonecfg_strerror(res)); 23217c478bd9Sstevel@tonic-gate return_code = Z_ERR; 23227c478bd9Sstevel@tonic-gate continue; 23237c478bd9Sstevel@tonic-gate } 23247c478bd9Sstevel@tonic-gate af = lifr.lifr_addr.ss_family; 23257c478bd9Sstevel@tonic-gate (void) memset(&lifr, 0, sizeof (lifr)); 23267c478bd9Sstevel@tonic-gate (void) strlcpy(lifr.lifr_name, nwiftab.zone_nwif_physical, 23277c478bd9Sstevel@tonic-gate sizeof (lifr.lifr_name)); 23287c478bd9Sstevel@tonic-gate lifr.lifr_addr.ss_family = af; 23297c478bd9Sstevel@tonic-gate if ((so = socket(af, SOCK_DGRAM, 0)) < 0) { 23307c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("could not verify %s " 23317c478bd9Sstevel@tonic-gate "%s=%s %s=%s: could not get socket: %s\n"), "net", 23327c478bd9Sstevel@tonic-gate "address", nwiftab.zone_nwif_address, "physical", 23337c478bd9Sstevel@tonic-gate nwiftab.zone_nwif_physical, strerror(errno)); 23347c478bd9Sstevel@tonic-gate return_code = Z_ERR; 23357c478bd9Sstevel@tonic-gate continue; 23367c478bd9Sstevel@tonic-gate } 23377c478bd9Sstevel@tonic-gate if (ioctl(so, SIOCGLIFFLAGS, (caddr_t)&lifr) < 0) { 23387c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 23397c478bd9Sstevel@tonic-gate gettext("could not verify %s %s=%s %s=%s: %s\n"), 23407c478bd9Sstevel@tonic-gate "net", "address", nwiftab.zone_nwif_address, 23417c478bd9Sstevel@tonic-gate "physical", nwiftab.zone_nwif_physical, 23427c478bd9Sstevel@tonic-gate strerror(errno)); 23437c478bd9Sstevel@tonic-gate return_code = Z_ERR; 23447c478bd9Sstevel@tonic-gate } 23457c478bd9Sstevel@tonic-gate (void) close(so); 23467c478bd9Sstevel@tonic-gate } 23477c478bd9Sstevel@tonic-gate (void) zonecfg_endnwifent(handle); 2348108322fbScarlsonj no_net: 23497c478bd9Sstevel@tonic-gate 23507c478bd9Sstevel@tonic-gate if (verify_filesystems(handle) != Z_OK) 23517c478bd9Sstevel@tonic-gate return_code = Z_ERR; 2352b5abaf04Sgjelinek if (verify_ipd(handle) != Z_OK) 2353b5abaf04Sgjelinek return_code = Z_ERR; 2354108322fbScarlsonj if (!in_alt_root && verify_rctls(handle) != Z_OK) 23557c478bd9Sstevel@tonic-gate return_code = Z_ERR; 2356108322fbScarlsonj if (!in_alt_root && verify_pool(handle) != Z_OK) 23577c478bd9Sstevel@tonic-gate return_code = Z_ERR; 2358fa9e4066Sahrens if (!in_alt_root && verify_datasets(handle) != Z_OK) 2359fa9e4066Sahrens return_code = Z_ERR; 23607c478bd9Sstevel@tonic-gate zonecfg_fini_handle(handle); 23617c478bd9Sstevel@tonic-gate if (return_code == Z_ERR) 23627c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 23637c478bd9Sstevel@tonic-gate gettext("%s: zone %s failed to verify\n"), 23647c478bd9Sstevel@tonic-gate execname, target_zone); 23657c478bd9Sstevel@tonic-gate return (return_code); 23667c478bd9Sstevel@tonic-gate } 23677c478bd9Sstevel@tonic-gate 23687c478bd9Sstevel@tonic-gate static int 23697c478bd9Sstevel@tonic-gate verify_func(int argc, char *argv[]) 23707c478bd9Sstevel@tonic-gate { 23717c478bd9Sstevel@tonic-gate int arg; 23727c478bd9Sstevel@tonic-gate 23737c478bd9Sstevel@tonic-gate optind = 0; 23747c478bd9Sstevel@tonic-gate if ((arg = getopt(argc, argv, "?")) != EOF) { 23757c478bd9Sstevel@tonic-gate switch (arg) { 23767c478bd9Sstevel@tonic-gate case '?': 23777c478bd9Sstevel@tonic-gate sub_usage(SHELP_VERIFY, CMD_VERIFY); 23787c478bd9Sstevel@tonic-gate return (optopt == '?' ? Z_OK : Z_USAGE); 23797c478bd9Sstevel@tonic-gate default: 23807c478bd9Sstevel@tonic-gate sub_usage(SHELP_VERIFY, CMD_VERIFY); 23817c478bd9Sstevel@tonic-gate return (Z_USAGE); 23827c478bd9Sstevel@tonic-gate } 23837c478bd9Sstevel@tonic-gate } 23847c478bd9Sstevel@tonic-gate if (argc > optind) { 23857c478bd9Sstevel@tonic-gate sub_usage(SHELP_VERIFY, CMD_VERIFY); 23867c478bd9Sstevel@tonic-gate return (Z_USAGE); 23877c478bd9Sstevel@tonic-gate } 23887c478bd9Sstevel@tonic-gate if (sanity_check(target_zone, CMD_VERIFY, B_FALSE, B_FALSE) != Z_OK) 23897c478bd9Sstevel@tonic-gate return (Z_ERR); 23907c478bd9Sstevel@tonic-gate return (verify_details(CMD_VERIFY)); 23917c478bd9Sstevel@tonic-gate } 23927c478bd9Sstevel@tonic-gate 23937c478bd9Sstevel@tonic-gate #define LUCREATEZONE "/usr/lib/lu/lucreatezone" 23947c478bd9Sstevel@tonic-gate 23957c478bd9Sstevel@tonic-gate static int 23967c478bd9Sstevel@tonic-gate install_func(int argc, char *argv[]) 23977c478bd9Sstevel@tonic-gate { 23987c478bd9Sstevel@tonic-gate /* 9: "exec " and " -z " */ 23997c478bd9Sstevel@tonic-gate char cmdbuf[sizeof (LUCREATEZONE) + ZONENAME_MAX + 9]; 24007c478bd9Sstevel@tonic-gate int lockfd; 24017c478bd9Sstevel@tonic-gate int err, arg; 24027c478bd9Sstevel@tonic-gate char zonepath[MAXPATHLEN]; 24037c478bd9Sstevel@tonic-gate int status; 24047c478bd9Sstevel@tonic-gate 2405108322fbScarlsonj if (zonecfg_in_alt_root()) { 2406108322fbScarlsonj zerror(gettext("cannot install zone in alternate root")); 2407108322fbScarlsonj return (Z_ERR); 2408108322fbScarlsonj } 2409108322fbScarlsonj 24107c478bd9Sstevel@tonic-gate optind = 0; 24117c478bd9Sstevel@tonic-gate if ((arg = getopt(argc, argv, "?")) != EOF) { 24127c478bd9Sstevel@tonic-gate switch (arg) { 24137c478bd9Sstevel@tonic-gate case '?': 24147c478bd9Sstevel@tonic-gate sub_usage(SHELP_INSTALL, CMD_INSTALL); 24157c478bd9Sstevel@tonic-gate return (optopt == '?' ? Z_OK : Z_USAGE); 24167c478bd9Sstevel@tonic-gate default: 24177c478bd9Sstevel@tonic-gate sub_usage(SHELP_INSTALL, CMD_INSTALL); 24187c478bd9Sstevel@tonic-gate return (Z_USAGE); 24197c478bd9Sstevel@tonic-gate } 24207c478bd9Sstevel@tonic-gate } 24217c478bd9Sstevel@tonic-gate if (argc > optind) { 24227c478bd9Sstevel@tonic-gate sub_usage(SHELP_INSTALL, CMD_INSTALL); 24237c478bd9Sstevel@tonic-gate return (Z_USAGE); 24247c478bd9Sstevel@tonic-gate } 24257c478bd9Sstevel@tonic-gate if (sanity_check(target_zone, CMD_INSTALL, B_FALSE, B_TRUE) != Z_OK) 24267c478bd9Sstevel@tonic-gate return (Z_ERR); 24277c478bd9Sstevel@tonic-gate if (verify_details(CMD_INSTALL) != Z_OK) 24287c478bd9Sstevel@tonic-gate return (Z_ERR); 24297c478bd9Sstevel@tonic-gate 24307c478bd9Sstevel@tonic-gate if (grab_lock_file(target_zone, &lockfd) != Z_OK) { 24317c478bd9Sstevel@tonic-gate zerror(gettext("another %s may have an operation in progress."), 2432865e09a4Sgjelinek "zoneadm"); 24337c478bd9Sstevel@tonic-gate return (Z_ERR); 24347c478bd9Sstevel@tonic-gate } 24357c478bd9Sstevel@tonic-gate err = zone_set_state(target_zone, ZONE_STATE_INCOMPLETE); 24367c478bd9Sstevel@tonic-gate if (err != Z_OK) { 24377c478bd9Sstevel@tonic-gate errno = err; 24387c478bd9Sstevel@tonic-gate zperror2(target_zone, gettext("could not set state")); 24397c478bd9Sstevel@tonic-gate goto done; 24407c478bd9Sstevel@tonic-gate } 24417c478bd9Sstevel@tonic-gate 24427c478bd9Sstevel@tonic-gate /* 24437c478bd9Sstevel@tonic-gate * According to the Application Packaging Developer's Guide, a 24447c478bd9Sstevel@tonic-gate * "checkinstall" script when included in a package is executed as 24457c478bd9Sstevel@tonic-gate * the user "install", if such a user exists, or by the user 24467c478bd9Sstevel@tonic-gate * "nobody". In order to support this dubious behavior, the path 24477c478bd9Sstevel@tonic-gate * to the zone being constructed is opened up during the life of 24487c478bd9Sstevel@tonic-gate * the command laying down the zone's root file system. Once this 24497c478bd9Sstevel@tonic-gate * has completed, regardless of whether it was successful, the 24507c478bd9Sstevel@tonic-gate * path to the zone is again restricted. 24517c478bd9Sstevel@tonic-gate */ 24527c478bd9Sstevel@tonic-gate if ((err = zone_get_zonepath(target_zone, zonepath, 24537c478bd9Sstevel@tonic-gate sizeof (zonepath))) != Z_OK) { 24547c478bd9Sstevel@tonic-gate errno = err; 24557c478bd9Sstevel@tonic-gate zperror2(target_zone, gettext("could not get zone path")); 24567c478bd9Sstevel@tonic-gate goto done; 24577c478bd9Sstevel@tonic-gate } 24587c478bd9Sstevel@tonic-gate if (chmod(zonepath, DEFAULT_DIR_MODE) != 0) { 24597c478bd9Sstevel@tonic-gate zperror(zonepath, B_FALSE); 24607c478bd9Sstevel@tonic-gate err = Z_ERR; 24617c478bd9Sstevel@tonic-gate goto done; 24627c478bd9Sstevel@tonic-gate } 24637c478bd9Sstevel@tonic-gate 24647c478bd9Sstevel@tonic-gate /* 24657c478bd9Sstevel@tonic-gate * "exec" the command so that the returned status is that of 24667c478bd9Sstevel@tonic-gate * LUCREATEZONE and not the shell. 24677c478bd9Sstevel@tonic-gate */ 24687c478bd9Sstevel@tonic-gate (void) snprintf(cmdbuf, sizeof (cmdbuf), "exec " LUCREATEZONE " -z %s", 24697c478bd9Sstevel@tonic-gate target_zone); 24707c478bd9Sstevel@tonic-gate status = do_subproc(cmdbuf); 24717c478bd9Sstevel@tonic-gate if (chmod(zonepath, S_IRWXU) != 0) { 24727c478bd9Sstevel@tonic-gate zperror(zonepath, B_FALSE); 24737c478bd9Sstevel@tonic-gate err = Z_ERR; 24747c478bd9Sstevel@tonic-gate goto done; 24757c478bd9Sstevel@tonic-gate } 24767c478bd9Sstevel@tonic-gate if ((err = subproc_status(LUCREATEZONE, status)) != Z_OK) 24777c478bd9Sstevel@tonic-gate goto done; 24787c478bd9Sstevel@tonic-gate 24797c478bd9Sstevel@tonic-gate if ((err = zone_set_state(target_zone, ZONE_STATE_INSTALLED)) != Z_OK) { 24807c478bd9Sstevel@tonic-gate errno = err; 24817c478bd9Sstevel@tonic-gate zperror2(target_zone, gettext("could not set state")); 24827c478bd9Sstevel@tonic-gate goto done; 24837c478bd9Sstevel@tonic-gate } 24847c478bd9Sstevel@tonic-gate 24857c478bd9Sstevel@tonic-gate done: 24867c478bd9Sstevel@tonic-gate release_lock_file(lockfd); 24877c478bd9Sstevel@tonic-gate return ((err == Z_OK) ? Z_OK : Z_ERR); 24887c478bd9Sstevel@tonic-gate } 24897c478bd9Sstevel@tonic-gate 24907c478bd9Sstevel@tonic-gate /* 2491865e09a4Sgjelinek * Check that the inherited pkg dirs are the same for the clone and its source. 2492865e09a4Sgjelinek * The easiest way to do that is check that the list of ipds is the same 2493865e09a4Sgjelinek * by matching each one against the other. This algorithm should be fine since 2494865e09a4Sgjelinek * the list of ipds should not be that long. 2495865e09a4Sgjelinek */ 2496865e09a4Sgjelinek static int 2497865e09a4Sgjelinek valid_ipd_clone(zone_dochandle_t s_handle, char *source_zone, 2498865e09a4Sgjelinek zone_dochandle_t t_handle, char *target_zone) 2499865e09a4Sgjelinek { 2500865e09a4Sgjelinek int err; 2501865e09a4Sgjelinek int res = Z_OK; 2502865e09a4Sgjelinek int s_cnt = 0; 2503865e09a4Sgjelinek int t_cnt = 0; 2504865e09a4Sgjelinek struct zone_fstab s_fstab; 2505865e09a4Sgjelinek struct zone_fstab t_fstab; 2506865e09a4Sgjelinek 2507865e09a4Sgjelinek /* 2508865e09a4Sgjelinek * First check the source of the clone against the target. 2509865e09a4Sgjelinek */ 2510865e09a4Sgjelinek if ((err = zonecfg_setipdent(s_handle)) != Z_OK) { 2511865e09a4Sgjelinek errno = err; 2512865e09a4Sgjelinek zperror2(source_zone, gettext("could not enumerate " 2513865e09a4Sgjelinek "inherit-pkg-dirs")); 2514865e09a4Sgjelinek return (Z_ERR); 2515865e09a4Sgjelinek } 2516865e09a4Sgjelinek 2517865e09a4Sgjelinek while (zonecfg_getipdent(s_handle, &s_fstab) == Z_OK) { 2518865e09a4Sgjelinek boolean_t match = B_FALSE; 2519865e09a4Sgjelinek 2520865e09a4Sgjelinek s_cnt++; 2521865e09a4Sgjelinek 2522865e09a4Sgjelinek if ((err = zonecfg_setipdent(t_handle)) != Z_OK) { 2523865e09a4Sgjelinek errno = err; 2524865e09a4Sgjelinek zperror2(target_zone, gettext("could not enumerate " 2525865e09a4Sgjelinek "inherit-pkg-dirs")); 2526865e09a4Sgjelinek (void) zonecfg_endipdent(s_handle); 2527865e09a4Sgjelinek return (Z_ERR); 2528865e09a4Sgjelinek } 2529865e09a4Sgjelinek 2530865e09a4Sgjelinek while (zonecfg_getipdent(t_handle, &t_fstab) == Z_OK) { 2531865e09a4Sgjelinek if (strcmp(s_fstab.zone_fs_dir, t_fstab.zone_fs_dir) 2532865e09a4Sgjelinek == 0) { 2533865e09a4Sgjelinek match = B_TRUE; 2534865e09a4Sgjelinek break; 2535865e09a4Sgjelinek } 2536865e09a4Sgjelinek } 2537865e09a4Sgjelinek (void) zonecfg_endipdent(t_handle); 2538865e09a4Sgjelinek 2539865e09a4Sgjelinek if (!match) { 2540865e09a4Sgjelinek (void) fprintf(stderr, gettext("inherit-pkg-dir " 2541865e09a4Sgjelinek "'%s' is not configured in zone %s.\n"), 2542865e09a4Sgjelinek s_fstab.zone_fs_dir, target_zone); 2543865e09a4Sgjelinek res = Z_ERR; 2544865e09a4Sgjelinek } 2545865e09a4Sgjelinek } 2546865e09a4Sgjelinek 2547865e09a4Sgjelinek (void) zonecfg_endipdent(s_handle); 2548865e09a4Sgjelinek 2549865e09a4Sgjelinek /* skip the next check if we already have errors */ 2550865e09a4Sgjelinek if (res == Z_ERR) 2551865e09a4Sgjelinek return (res); 2552865e09a4Sgjelinek 2553865e09a4Sgjelinek /* 2554865e09a4Sgjelinek * Now check the number of ipds in the target so we can verify 2555865e09a4Sgjelinek * that the source is not a subset of the target. 2556865e09a4Sgjelinek */ 2557865e09a4Sgjelinek if ((err = zonecfg_setipdent(t_handle)) != Z_OK) { 2558865e09a4Sgjelinek errno = err; 2559865e09a4Sgjelinek zperror2(target_zone, gettext("could not enumerate " 2560865e09a4Sgjelinek "inherit-pkg-dirs")); 2561865e09a4Sgjelinek return (Z_ERR); 2562865e09a4Sgjelinek } 2563865e09a4Sgjelinek 2564865e09a4Sgjelinek while (zonecfg_getipdent(t_handle, &t_fstab) == Z_OK) 2565865e09a4Sgjelinek t_cnt++; 2566865e09a4Sgjelinek 2567865e09a4Sgjelinek (void) zonecfg_endipdent(t_handle); 2568865e09a4Sgjelinek 2569865e09a4Sgjelinek if (t_cnt != s_cnt) { 2570865e09a4Sgjelinek (void) fprintf(stderr, gettext("Zone %s is configured " 2571865e09a4Sgjelinek "with inherit-pkg-dirs that are not configured in zone " 2572865e09a4Sgjelinek "%s.\n"), target_zone, source_zone); 2573865e09a4Sgjelinek res = Z_ERR; 2574865e09a4Sgjelinek } 2575865e09a4Sgjelinek 2576865e09a4Sgjelinek return (res); 2577865e09a4Sgjelinek } 2578865e09a4Sgjelinek 2579865e09a4Sgjelinek static void 2580865e09a4Sgjelinek warn_dev_match(zone_dochandle_t s_handle, char *source_zone, 2581865e09a4Sgjelinek zone_dochandle_t t_handle, char *target_zone) 2582865e09a4Sgjelinek { 2583865e09a4Sgjelinek int err; 2584865e09a4Sgjelinek struct zone_devtab s_devtab; 2585865e09a4Sgjelinek struct zone_devtab t_devtab; 2586865e09a4Sgjelinek 2587865e09a4Sgjelinek if ((err = zonecfg_setdevent(t_handle)) != Z_OK) { 2588865e09a4Sgjelinek errno = err; 2589865e09a4Sgjelinek zperror2(target_zone, gettext("could not enumerate devices")); 2590865e09a4Sgjelinek return; 2591865e09a4Sgjelinek } 2592865e09a4Sgjelinek 2593865e09a4Sgjelinek while (zonecfg_getdevent(t_handle, &t_devtab) == Z_OK) { 2594865e09a4Sgjelinek if ((err = zonecfg_setdevent(s_handle)) != Z_OK) { 2595865e09a4Sgjelinek errno = err; 2596865e09a4Sgjelinek zperror2(source_zone, 2597865e09a4Sgjelinek gettext("could not enumerate devices")); 2598865e09a4Sgjelinek (void) zonecfg_enddevent(t_handle); 2599865e09a4Sgjelinek return; 2600865e09a4Sgjelinek } 2601865e09a4Sgjelinek 2602865e09a4Sgjelinek while (zonecfg_getdevent(s_handle, &s_devtab) == Z_OK) { 2603865e09a4Sgjelinek /* 2604865e09a4Sgjelinek * Use fnmatch to catch the case where wildcards 2605865e09a4Sgjelinek * were used in one zone and the other has an 2606865e09a4Sgjelinek * explicit entry (e.g. /dev/dsk/c0t0d0s6 vs. 2607865e09a4Sgjelinek * /dev/\*dsk/c0t0d0s6). 2608865e09a4Sgjelinek */ 2609865e09a4Sgjelinek if (fnmatch(t_devtab.zone_dev_match, 2610865e09a4Sgjelinek s_devtab.zone_dev_match, FNM_PATHNAME) == 0 || 2611865e09a4Sgjelinek fnmatch(s_devtab.zone_dev_match, 2612865e09a4Sgjelinek t_devtab.zone_dev_match, FNM_PATHNAME) == 0) { 2613865e09a4Sgjelinek (void) fprintf(stderr, 2614865e09a4Sgjelinek gettext("WARNING: device '%s' " 2615865e09a4Sgjelinek "is configured in both zones.\n"), 2616865e09a4Sgjelinek t_devtab.zone_dev_match); 2617865e09a4Sgjelinek break; 2618865e09a4Sgjelinek } 2619865e09a4Sgjelinek } 2620865e09a4Sgjelinek (void) zonecfg_enddevent(s_handle); 2621865e09a4Sgjelinek } 2622865e09a4Sgjelinek 2623865e09a4Sgjelinek (void) zonecfg_enddevent(t_handle); 2624865e09a4Sgjelinek } 2625865e09a4Sgjelinek 2626865e09a4Sgjelinek /* 2627865e09a4Sgjelinek * Check if the specified mount option (opt) is contained within the 2628865e09a4Sgjelinek * options string. 2629865e09a4Sgjelinek */ 2630865e09a4Sgjelinek static boolean_t 2631865e09a4Sgjelinek opt_match(char *opt, char *options) 2632865e09a4Sgjelinek { 2633865e09a4Sgjelinek char *p; 2634865e09a4Sgjelinek char *lastp; 2635865e09a4Sgjelinek 2636865e09a4Sgjelinek if ((p = strtok_r(options, ",", &lastp)) != NULL) { 2637865e09a4Sgjelinek if (strcmp(p, opt) == 0) 2638865e09a4Sgjelinek return (B_TRUE); 2639865e09a4Sgjelinek while ((p = strtok_r(NULL, ",", &lastp)) != NULL) { 2640865e09a4Sgjelinek if (strcmp(p, opt) == 0) 2641865e09a4Sgjelinek return (B_TRUE); 2642865e09a4Sgjelinek } 2643865e09a4Sgjelinek } 2644865e09a4Sgjelinek 2645865e09a4Sgjelinek return (B_FALSE); 2646865e09a4Sgjelinek } 2647865e09a4Sgjelinek 2648865e09a4Sgjelinek #define RW_LOFS "WARNING: read-write lofs file-system on '%s' is configured " \ 2649865e09a4Sgjelinek "in both zones.\n" 2650865e09a4Sgjelinek 2651865e09a4Sgjelinek static void 2652865e09a4Sgjelinek print_fs_warnings(struct zone_fstab *s_fstab, struct zone_fstab *t_fstab) 2653865e09a4Sgjelinek { 2654865e09a4Sgjelinek /* 2655865e09a4Sgjelinek * It is ok to have shared lofs mounted fs but we want to warn if 2656865e09a4Sgjelinek * either is rw since this will effect the other zone. 2657865e09a4Sgjelinek */ 2658865e09a4Sgjelinek if (strcmp(t_fstab->zone_fs_type, "lofs") == 0) { 2659865e09a4Sgjelinek zone_fsopt_t *optp; 2660865e09a4Sgjelinek 2661865e09a4Sgjelinek /* The default is rw so no options means rw */ 2662865e09a4Sgjelinek if (t_fstab->zone_fs_options == NULL || 2663865e09a4Sgjelinek s_fstab->zone_fs_options == NULL) { 2664865e09a4Sgjelinek (void) fprintf(stderr, gettext(RW_LOFS), 2665865e09a4Sgjelinek t_fstab->zone_fs_special); 2666865e09a4Sgjelinek return; 2667865e09a4Sgjelinek } 2668865e09a4Sgjelinek 2669865e09a4Sgjelinek for (optp = s_fstab->zone_fs_options; optp != NULL; 2670865e09a4Sgjelinek optp = optp->zone_fsopt_next) { 2671865e09a4Sgjelinek if (opt_match("rw", optp->zone_fsopt_opt)) { 2672865e09a4Sgjelinek (void) fprintf(stderr, gettext(RW_LOFS), 2673865e09a4Sgjelinek s_fstab->zone_fs_special); 2674865e09a4Sgjelinek return; 2675865e09a4Sgjelinek } 2676865e09a4Sgjelinek } 2677865e09a4Sgjelinek 2678865e09a4Sgjelinek for (optp = t_fstab->zone_fs_options; optp != NULL; 2679865e09a4Sgjelinek optp = optp->zone_fsopt_next) { 2680865e09a4Sgjelinek if (opt_match("rw", optp->zone_fsopt_opt)) { 2681865e09a4Sgjelinek (void) fprintf(stderr, gettext(RW_LOFS), 2682865e09a4Sgjelinek t_fstab->zone_fs_special); 2683865e09a4Sgjelinek return; 2684865e09a4Sgjelinek } 2685865e09a4Sgjelinek } 2686865e09a4Sgjelinek 2687865e09a4Sgjelinek return; 2688865e09a4Sgjelinek } 2689865e09a4Sgjelinek 2690865e09a4Sgjelinek /* 2691865e09a4Sgjelinek * TRANSLATION_NOTE 2692865e09a4Sgjelinek * The first variable is the file-system type and the second is 2693865e09a4Sgjelinek * the file-system special device. For example, 2694865e09a4Sgjelinek * WARNING: ufs file-system on '/dev/dsk/c0t0d0s0' ... 2695865e09a4Sgjelinek */ 2696865e09a4Sgjelinek (void) fprintf(stderr, gettext("WARNING: %s file-system on '%s' " 2697865e09a4Sgjelinek "is configured in both zones.\n"), t_fstab->zone_fs_type, 2698865e09a4Sgjelinek t_fstab->zone_fs_special); 2699865e09a4Sgjelinek } 2700865e09a4Sgjelinek 2701865e09a4Sgjelinek static void 2702865e09a4Sgjelinek warn_fs_match(zone_dochandle_t s_handle, char *source_zone, 2703865e09a4Sgjelinek zone_dochandle_t t_handle, char *target_zone) 2704865e09a4Sgjelinek { 2705865e09a4Sgjelinek int err; 2706865e09a4Sgjelinek struct zone_fstab s_fstab; 2707865e09a4Sgjelinek struct zone_fstab t_fstab; 2708865e09a4Sgjelinek 2709865e09a4Sgjelinek if ((err = zonecfg_setfsent(t_handle)) != Z_OK) { 2710865e09a4Sgjelinek errno = err; 2711865e09a4Sgjelinek zperror2(target_zone, 2712865e09a4Sgjelinek gettext("could not enumerate file-systems")); 2713865e09a4Sgjelinek return; 2714865e09a4Sgjelinek } 2715865e09a4Sgjelinek 2716865e09a4Sgjelinek while (zonecfg_getfsent(t_handle, &t_fstab) == Z_OK) { 2717865e09a4Sgjelinek if ((err = zonecfg_setfsent(s_handle)) != Z_OK) { 2718865e09a4Sgjelinek errno = err; 2719865e09a4Sgjelinek zperror2(source_zone, 2720865e09a4Sgjelinek gettext("could not enumerate file-systems")); 2721865e09a4Sgjelinek (void) zonecfg_endfsent(t_handle); 2722865e09a4Sgjelinek return; 2723865e09a4Sgjelinek } 2724865e09a4Sgjelinek 2725865e09a4Sgjelinek while (zonecfg_getfsent(s_handle, &s_fstab) == Z_OK) { 2726865e09a4Sgjelinek if (strcmp(t_fstab.zone_fs_special, 2727865e09a4Sgjelinek s_fstab.zone_fs_special) == 0) { 2728865e09a4Sgjelinek print_fs_warnings(&s_fstab, &t_fstab); 2729865e09a4Sgjelinek break; 2730865e09a4Sgjelinek } 2731865e09a4Sgjelinek } 2732865e09a4Sgjelinek (void) zonecfg_endfsent(s_handle); 2733865e09a4Sgjelinek } 2734865e09a4Sgjelinek 2735865e09a4Sgjelinek (void) zonecfg_endfsent(t_handle); 2736865e09a4Sgjelinek } 2737865e09a4Sgjelinek 2738865e09a4Sgjelinek /* 2739865e09a4Sgjelinek * We don't catch the case where you used the same IP address but 2740865e09a4Sgjelinek * it is not an exact string match. For example, 192.9.0.128 vs. 192.09.0.128. 2741865e09a4Sgjelinek * However, we're not going to worry about that but we will check for 2742865e09a4Sgjelinek * a possible netmask on one of the addresses (e.g. 10.0.0.1 and 10.0.0.1/24) 2743865e09a4Sgjelinek * and handle that case as a match. 2744865e09a4Sgjelinek */ 2745865e09a4Sgjelinek static void 2746865e09a4Sgjelinek warn_ip_match(zone_dochandle_t s_handle, char *source_zone, 2747865e09a4Sgjelinek zone_dochandle_t t_handle, char *target_zone) 2748865e09a4Sgjelinek { 2749865e09a4Sgjelinek int err; 2750865e09a4Sgjelinek struct zone_nwiftab s_nwiftab; 2751865e09a4Sgjelinek struct zone_nwiftab t_nwiftab; 2752865e09a4Sgjelinek 2753865e09a4Sgjelinek if ((err = zonecfg_setnwifent(t_handle)) != Z_OK) { 2754865e09a4Sgjelinek errno = err; 2755865e09a4Sgjelinek zperror2(target_zone, 2756865e09a4Sgjelinek gettext("could not enumerate network interfaces")); 2757865e09a4Sgjelinek return; 2758865e09a4Sgjelinek } 2759865e09a4Sgjelinek 2760865e09a4Sgjelinek while (zonecfg_getnwifent(t_handle, &t_nwiftab) == Z_OK) { 2761865e09a4Sgjelinek char *p; 2762865e09a4Sgjelinek 2763865e09a4Sgjelinek /* remove an (optional) netmask from the address */ 2764865e09a4Sgjelinek if ((p = strchr(t_nwiftab.zone_nwif_address, '/')) != NULL) 2765865e09a4Sgjelinek *p = '\0'; 2766865e09a4Sgjelinek 2767865e09a4Sgjelinek if ((err = zonecfg_setnwifent(s_handle)) != Z_OK) { 2768865e09a4Sgjelinek errno = err; 2769865e09a4Sgjelinek zperror2(source_zone, 2770865e09a4Sgjelinek gettext("could not enumerate network interfaces")); 2771865e09a4Sgjelinek (void) zonecfg_endnwifent(t_handle); 2772865e09a4Sgjelinek return; 2773865e09a4Sgjelinek } 2774865e09a4Sgjelinek 2775865e09a4Sgjelinek while (zonecfg_getnwifent(s_handle, &s_nwiftab) == Z_OK) { 2776865e09a4Sgjelinek /* remove an (optional) netmask from the address */ 2777865e09a4Sgjelinek if ((p = strchr(s_nwiftab.zone_nwif_address, '/')) 2778865e09a4Sgjelinek != NULL) 2779865e09a4Sgjelinek *p = '\0'; 2780865e09a4Sgjelinek 2781865e09a4Sgjelinek if (strcmp(t_nwiftab.zone_nwif_address, 2782865e09a4Sgjelinek s_nwiftab.zone_nwif_address) == 0) { 2783865e09a4Sgjelinek (void) fprintf(stderr, 2784865e09a4Sgjelinek gettext("WARNING: network address '%s' " 2785865e09a4Sgjelinek "is configured in both zones.\n"), 2786865e09a4Sgjelinek t_nwiftab.zone_nwif_address); 2787865e09a4Sgjelinek break; 2788865e09a4Sgjelinek } 2789865e09a4Sgjelinek } 2790865e09a4Sgjelinek (void) zonecfg_endnwifent(s_handle); 2791865e09a4Sgjelinek } 2792865e09a4Sgjelinek 2793865e09a4Sgjelinek (void) zonecfg_endnwifent(t_handle); 2794865e09a4Sgjelinek } 2795865e09a4Sgjelinek 2796865e09a4Sgjelinek static void 2797865e09a4Sgjelinek warn_dataset_match(zone_dochandle_t s_handle, char *source_zone, 2798865e09a4Sgjelinek zone_dochandle_t t_handle, char *target_zone) 2799865e09a4Sgjelinek { 2800865e09a4Sgjelinek int err; 2801865e09a4Sgjelinek struct zone_dstab s_dstab; 2802865e09a4Sgjelinek struct zone_dstab t_dstab; 2803865e09a4Sgjelinek 2804865e09a4Sgjelinek if ((err = zonecfg_setdsent(t_handle)) != Z_OK) { 2805865e09a4Sgjelinek errno = err; 2806865e09a4Sgjelinek zperror2(target_zone, gettext("could not enumerate datasets")); 2807865e09a4Sgjelinek return; 2808865e09a4Sgjelinek } 2809865e09a4Sgjelinek 2810865e09a4Sgjelinek while (zonecfg_getdsent(t_handle, &t_dstab) == Z_OK) { 2811865e09a4Sgjelinek if ((err = zonecfg_setdsent(s_handle)) != Z_OK) { 2812865e09a4Sgjelinek errno = err; 2813865e09a4Sgjelinek zperror2(source_zone, 2814865e09a4Sgjelinek gettext("could not enumerate datasets")); 2815865e09a4Sgjelinek (void) zonecfg_enddsent(t_handle); 2816865e09a4Sgjelinek return; 2817865e09a4Sgjelinek } 2818865e09a4Sgjelinek 2819865e09a4Sgjelinek while (zonecfg_getdsent(s_handle, &s_dstab) == Z_OK) { 2820865e09a4Sgjelinek if (strcmp(t_dstab.zone_dataset_name, 2821865e09a4Sgjelinek s_dstab.zone_dataset_name) == 0) { 2822865e09a4Sgjelinek (void) fprintf(stderr, 2823865e09a4Sgjelinek gettext("WARNING: dataset '%s' " 2824865e09a4Sgjelinek "is configured in both zones.\n"), 2825865e09a4Sgjelinek t_dstab.zone_dataset_name); 2826865e09a4Sgjelinek break; 2827865e09a4Sgjelinek } 2828865e09a4Sgjelinek } 2829865e09a4Sgjelinek (void) zonecfg_enddsent(s_handle); 2830865e09a4Sgjelinek } 2831865e09a4Sgjelinek 2832865e09a4Sgjelinek (void) zonecfg_enddsent(t_handle); 2833865e09a4Sgjelinek } 2834865e09a4Sgjelinek 2835865e09a4Sgjelinek static int 2836865e09a4Sgjelinek validate_clone(char *source_zone, char *target_zone) 2837865e09a4Sgjelinek { 2838865e09a4Sgjelinek int err = Z_OK; 2839865e09a4Sgjelinek zone_dochandle_t s_handle; 2840865e09a4Sgjelinek zone_dochandle_t t_handle; 2841865e09a4Sgjelinek 2842865e09a4Sgjelinek if ((t_handle = zonecfg_init_handle()) == NULL) { 2843865e09a4Sgjelinek zperror(cmd_to_str(CMD_CLONE), B_TRUE); 2844865e09a4Sgjelinek return (Z_ERR); 2845865e09a4Sgjelinek } 2846865e09a4Sgjelinek if ((err = zonecfg_get_handle(target_zone, t_handle)) != Z_OK) { 2847865e09a4Sgjelinek errno = err; 2848865e09a4Sgjelinek zperror(cmd_to_str(CMD_CLONE), B_TRUE); 2849865e09a4Sgjelinek zonecfg_fini_handle(t_handle); 2850865e09a4Sgjelinek return (Z_ERR); 2851865e09a4Sgjelinek } 2852865e09a4Sgjelinek 2853865e09a4Sgjelinek if ((s_handle = zonecfg_init_handle()) == NULL) { 2854865e09a4Sgjelinek zperror(cmd_to_str(CMD_CLONE), B_TRUE); 2855865e09a4Sgjelinek zonecfg_fini_handle(t_handle); 2856865e09a4Sgjelinek return (Z_ERR); 2857865e09a4Sgjelinek } 2858865e09a4Sgjelinek if ((err = zonecfg_get_handle(source_zone, s_handle)) != Z_OK) { 2859865e09a4Sgjelinek errno = err; 2860865e09a4Sgjelinek zperror(cmd_to_str(CMD_CLONE), B_TRUE); 2861865e09a4Sgjelinek goto done; 2862865e09a4Sgjelinek } 2863865e09a4Sgjelinek 2864865e09a4Sgjelinek /* verify new zone has same inherit-pkg-dirs */ 2865865e09a4Sgjelinek err = valid_ipd_clone(s_handle, source_zone, t_handle, target_zone); 2866865e09a4Sgjelinek 2867865e09a4Sgjelinek /* warn about imported fs's which are the same */ 2868865e09a4Sgjelinek warn_fs_match(s_handle, source_zone, t_handle, target_zone); 2869865e09a4Sgjelinek 2870865e09a4Sgjelinek /* warn about imported IP addresses which are the same */ 2871865e09a4Sgjelinek warn_ip_match(s_handle, source_zone, t_handle, target_zone); 2872865e09a4Sgjelinek 2873865e09a4Sgjelinek /* warn about imported devices which are the same */ 2874865e09a4Sgjelinek warn_dev_match(s_handle, source_zone, t_handle, target_zone); 2875865e09a4Sgjelinek 2876865e09a4Sgjelinek /* warn about imported datasets which are the same */ 2877865e09a4Sgjelinek warn_dataset_match(s_handle, source_zone, t_handle, target_zone); 2878865e09a4Sgjelinek 2879865e09a4Sgjelinek done: 2880865e09a4Sgjelinek zonecfg_fini_handle(t_handle); 2881865e09a4Sgjelinek zonecfg_fini_handle(s_handle); 2882865e09a4Sgjelinek 2883865e09a4Sgjelinek return ((err == Z_OK) ? Z_OK : Z_ERR); 2884865e09a4Sgjelinek } 2885865e09a4Sgjelinek 2886865e09a4Sgjelinek static int 2887865e09a4Sgjelinek copy_zone(char *src, char *dst) 2888865e09a4Sgjelinek { 2889865e09a4Sgjelinek boolean_t out_null = B_FALSE; 2890865e09a4Sgjelinek int status; 2891865e09a4Sgjelinek int err; 2892865e09a4Sgjelinek char *outfile; 2893865e09a4Sgjelinek char cmdbuf[MAXPATHLEN * 2 + 128]; 2894865e09a4Sgjelinek 2895865e09a4Sgjelinek if ((outfile = tempnam("/var/log", "zone")) == NULL) { 2896865e09a4Sgjelinek outfile = "/dev/null"; 2897865e09a4Sgjelinek out_null = B_TRUE; 2898865e09a4Sgjelinek } 2899865e09a4Sgjelinek 2900865e09a4Sgjelinek (void) snprintf(cmdbuf, sizeof (cmdbuf), 2901865e09a4Sgjelinek "cd %s && /usr/bin/find . -depth -print | " 2902865e09a4Sgjelinek "/usr/bin/cpio -pdmuP@ %s > %s 2>&1", 2903865e09a4Sgjelinek src, dst, outfile); 2904865e09a4Sgjelinek 2905865e09a4Sgjelinek status = do_subproc(cmdbuf); 2906865e09a4Sgjelinek 2907865e09a4Sgjelinek if ((err = subproc_status("copy", status)) != Z_OK) { 2908865e09a4Sgjelinek if (!out_null) 2909865e09a4Sgjelinek (void) fprintf(stderr, gettext("\nThe copy failed.\n" 2910865e09a4Sgjelinek "More information can be found in %s\n"), outfile); 2911865e09a4Sgjelinek return (err); 2912865e09a4Sgjelinek } 2913865e09a4Sgjelinek 2914865e09a4Sgjelinek if (!out_null) 2915865e09a4Sgjelinek (void) unlink(outfile); 2916865e09a4Sgjelinek 2917865e09a4Sgjelinek return (Z_OK); 2918865e09a4Sgjelinek } 2919865e09a4Sgjelinek 2920865e09a4Sgjelinek /* 2921865e09a4Sgjelinek * Wait until the target_zone has booted to single-user. Return Z_OK once 2922865e09a4Sgjelinek * the zone has booted to that level or return Z_BAD_ZONE_STATE if the zone 2923865e09a4Sgjelinek * has not booted to single-user after the timeout. 2924865e09a4Sgjelinek */ 2925865e09a4Sgjelinek static int 2926865e09a4Sgjelinek zone_wait_single_user() 2927865e09a4Sgjelinek { 2928865e09a4Sgjelinek char cmdbuf[ZONENAME_MAX + 256]; 2929865e09a4Sgjelinek int retry; 2930865e09a4Sgjelinek 2931865e09a4Sgjelinek (void) snprintf(cmdbuf, sizeof (cmdbuf), 2932865e09a4Sgjelinek "test \"`/usr/sbin/zlogin -S %s /usr/bin/svcprop -p " 2933865e09a4Sgjelinek "restarter/state svc:/milestone/single-user:default 2>/dev/null`\" " 2934865e09a4Sgjelinek "= \"online\"", 2935865e09a4Sgjelinek target_zone); 2936865e09a4Sgjelinek 2937865e09a4Sgjelinek for (retry = 0; retry < SINGLE_USER_RETRY; retry++) { 2938865e09a4Sgjelinek int status; 2939865e09a4Sgjelinek 2940865e09a4Sgjelinek status = do_subproc(cmdbuf); 2941865e09a4Sgjelinek if (WIFEXITED(status)) { 2942865e09a4Sgjelinek if (WEXITSTATUS(status) == 0) 2943865e09a4Sgjelinek return (Z_OK); 2944865e09a4Sgjelinek 2945865e09a4Sgjelinek (void) sleep(2); 2946865e09a4Sgjelinek } else { 2947865e09a4Sgjelinek return (Z_BAD_ZONE_STATE); 2948865e09a4Sgjelinek } 2949865e09a4Sgjelinek } 2950865e09a4Sgjelinek 2951865e09a4Sgjelinek return (Z_BAD_ZONE_STATE); 2952865e09a4Sgjelinek } 2953865e09a4Sgjelinek 2954865e09a4Sgjelinek 2955865e09a4Sgjelinek /* ARGSUSED */ 2956865e09a4Sgjelinek int 2957865e09a4Sgjelinek zfm_print(const char *p, void *r) { 2958865e09a4Sgjelinek zerror(" %s\n", p); 2959865e09a4Sgjelinek return (0); 2960865e09a4Sgjelinek } 2961865e09a4Sgjelinek 2962865e09a4Sgjelinek static int 2963865e09a4Sgjelinek clone_func(int argc, char *argv[]) 2964865e09a4Sgjelinek { 2965865e09a4Sgjelinek char cmdbuf[MAXPATHLEN]; 2966865e09a4Sgjelinek char *source_zone = NULL; 2967865e09a4Sgjelinek int lockfd; 2968865e09a4Sgjelinek int err, arg; 2969865e09a4Sgjelinek char zonepath[MAXPATHLEN]; 2970865e09a4Sgjelinek char source_zonepath[MAXPATHLEN]; 2971865e09a4Sgjelinek int status; 2972865e09a4Sgjelinek zone_state_t state; 2973865e09a4Sgjelinek zone_entry_t *zent; 2974865e09a4Sgjelinek char *method = "copy"; 2975865e09a4Sgjelinek char *boot_args[] = { "-s", NULL }; 2976865e09a4Sgjelinek char *halt_args[] = { NULL }; 2977865e09a4Sgjelinek struct stat unconfig_buf; 2978865e09a4Sgjelinek boolean_t revert; 2979865e09a4Sgjelinek 2980865e09a4Sgjelinek if (zonecfg_in_alt_root()) { 2981865e09a4Sgjelinek zerror(gettext("cannot clone zone in alternate root")); 2982865e09a4Sgjelinek return (Z_ERR); 2983865e09a4Sgjelinek } 2984865e09a4Sgjelinek 2985865e09a4Sgjelinek optind = 0; 2986865e09a4Sgjelinek if ((arg = getopt(argc, argv, "?m:")) != EOF) { 2987865e09a4Sgjelinek switch (arg) { 2988865e09a4Sgjelinek case '?': 2989865e09a4Sgjelinek sub_usage(SHELP_CLONE, CMD_CLONE); 2990865e09a4Sgjelinek return (optopt == '?' ? Z_OK : Z_USAGE); 2991865e09a4Sgjelinek case 'm': 2992865e09a4Sgjelinek method = optarg; 2993865e09a4Sgjelinek break; 2994865e09a4Sgjelinek default: 2995865e09a4Sgjelinek sub_usage(SHELP_CLONE, CMD_CLONE); 2996865e09a4Sgjelinek return (Z_USAGE); 2997865e09a4Sgjelinek } 2998865e09a4Sgjelinek } 2999865e09a4Sgjelinek if (argc != (optind + 1) || strcmp(method, "copy") != 0) { 3000865e09a4Sgjelinek sub_usage(SHELP_CLONE, CMD_CLONE); 3001865e09a4Sgjelinek return (Z_USAGE); 3002865e09a4Sgjelinek } 3003865e09a4Sgjelinek source_zone = argv[optind]; 3004865e09a4Sgjelinek if (sanity_check(target_zone, CMD_CLONE, B_FALSE, B_TRUE) != Z_OK) 3005865e09a4Sgjelinek return (Z_ERR); 3006865e09a4Sgjelinek if (verify_details(CMD_CLONE) != Z_OK) 3007865e09a4Sgjelinek return (Z_ERR); 3008865e09a4Sgjelinek 3009865e09a4Sgjelinek /* 3010865e09a4Sgjelinek * We also need to do some extra validation on the source zone. 3011865e09a4Sgjelinek */ 3012865e09a4Sgjelinek 3013865e09a4Sgjelinek if (strcmp(source_zone, GLOBAL_ZONENAME) == 0) { 3014865e09a4Sgjelinek zerror(gettext("%s operation is invalid for the global zone."), 3015865e09a4Sgjelinek cmd_to_str(CMD_CLONE)); 3016865e09a4Sgjelinek return (Z_ERR); 3017865e09a4Sgjelinek } 3018865e09a4Sgjelinek 3019865e09a4Sgjelinek if (strncmp(source_zone, "SUNW", 4) == 0) { 3020865e09a4Sgjelinek zerror(gettext("%s operation is invalid for zones starting " 3021865e09a4Sgjelinek "with SUNW."), cmd_to_str(CMD_CLONE)); 3022865e09a4Sgjelinek return (Z_ERR); 3023865e09a4Sgjelinek } 3024865e09a4Sgjelinek 3025865e09a4Sgjelinek zent = lookup_running_zone(source_zone); 3026865e09a4Sgjelinek if (zent != NULL) { 3027865e09a4Sgjelinek /* check whether the zone is ready or running */ 3028865e09a4Sgjelinek if ((err = zone_get_state(zent->zname, &zent->zstate_num)) 3029865e09a4Sgjelinek != Z_OK) { 3030865e09a4Sgjelinek errno = err; 3031865e09a4Sgjelinek zperror2(zent->zname, gettext("could not get state")); 3032865e09a4Sgjelinek /* can't tell, so hedge */ 3033865e09a4Sgjelinek zent->zstate_str = "ready/running"; 3034865e09a4Sgjelinek } else { 3035865e09a4Sgjelinek zent->zstate_str = zone_state_str(zent->zstate_num); 3036865e09a4Sgjelinek } 3037865e09a4Sgjelinek zerror(gettext("%s operation is invalid for %s zones."), 3038865e09a4Sgjelinek cmd_to_str(CMD_CLONE), zent->zstate_str); 3039865e09a4Sgjelinek return (Z_ERR); 3040865e09a4Sgjelinek } 3041865e09a4Sgjelinek 3042865e09a4Sgjelinek if ((err = zone_get_state(source_zone, &state)) != Z_OK) { 3043865e09a4Sgjelinek errno = err; 3044865e09a4Sgjelinek zperror2(source_zone, gettext("could not get state")); 3045865e09a4Sgjelinek return (Z_ERR); 3046865e09a4Sgjelinek } 3047865e09a4Sgjelinek if (state != ZONE_STATE_INSTALLED) { 3048865e09a4Sgjelinek (void) fprintf(stderr, 3049865e09a4Sgjelinek gettext("%s: zone %s is %s; %s is required.\n"), 3050865e09a4Sgjelinek execname, source_zone, zone_state_str(state), 3051865e09a4Sgjelinek zone_state_str(ZONE_STATE_INSTALLED)); 3052865e09a4Sgjelinek return (Z_ERR); 3053865e09a4Sgjelinek } 3054865e09a4Sgjelinek 3055865e09a4Sgjelinek /* 3056865e09a4Sgjelinek * The source zone checks out ok, continue with the clone. 3057865e09a4Sgjelinek */ 3058865e09a4Sgjelinek 3059865e09a4Sgjelinek if (validate_clone(source_zone, target_zone) != Z_OK) 3060865e09a4Sgjelinek return (Z_ERR); 3061865e09a4Sgjelinek 3062865e09a4Sgjelinek if (grab_lock_file(target_zone, &lockfd) != Z_OK) { 3063865e09a4Sgjelinek zerror(gettext("another %s may have an operation in progress."), 3064865e09a4Sgjelinek "zoneadm"); 3065865e09a4Sgjelinek return (Z_ERR); 3066865e09a4Sgjelinek } 3067865e09a4Sgjelinek 3068865e09a4Sgjelinek if ((err = zone_get_zonepath(source_zone, source_zonepath, 3069865e09a4Sgjelinek sizeof (source_zonepath))) != Z_OK) { 3070865e09a4Sgjelinek errno = err; 3071865e09a4Sgjelinek zperror2(source_zone, gettext("could not get zone path")); 3072865e09a4Sgjelinek goto done; 3073865e09a4Sgjelinek } 3074865e09a4Sgjelinek 3075865e09a4Sgjelinek if ((err = zone_get_zonepath(target_zone, zonepath, sizeof (zonepath))) 3076865e09a4Sgjelinek != Z_OK) { 3077865e09a4Sgjelinek errno = err; 3078865e09a4Sgjelinek zperror2(target_zone, gettext("could not get zone path")); 3079865e09a4Sgjelinek goto done; 3080865e09a4Sgjelinek } 3081865e09a4Sgjelinek 3082865e09a4Sgjelinek /* Don't clone the zone if anything is still mounted there */ 3083865e09a4Sgjelinek if (zonecfg_find_mounts(source_zonepath, NULL, NULL)) { 3084865e09a4Sgjelinek zerror(gettext("These file-systems are mounted on " 3085865e09a4Sgjelinek "subdirectories of %s.\n"), source_zonepath); 3086865e09a4Sgjelinek (void) zonecfg_find_mounts(source_zonepath, zfm_print, NULL); 3087865e09a4Sgjelinek err = Z_ERR; 3088865e09a4Sgjelinek goto done; 3089865e09a4Sgjelinek } 3090865e09a4Sgjelinek 3091865e09a4Sgjelinek if ((err = zone_set_state(target_zone, ZONE_STATE_INCOMPLETE)) 3092865e09a4Sgjelinek != Z_OK) { 3093865e09a4Sgjelinek errno = err; 3094865e09a4Sgjelinek zperror2(target_zone, gettext("could not set state")); 3095865e09a4Sgjelinek goto done; 3096865e09a4Sgjelinek } 3097865e09a4Sgjelinek 3098865e09a4Sgjelinek (void) printf(gettext("Cloning zonepath %s..."), source_zonepath); 3099865e09a4Sgjelinek (void) fflush(stdout); 3100865e09a4Sgjelinek 3101865e09a4Sgjelinek if ((err = copy_zone(source_zonepath, zonepath)) != Z_OK) 3102865e09a4Sgjelinek goto done; 3103865e09a4Sgjelinek 3104865e09a4Sgjelinek /* 3105865e09a4Sgjelinek * We have to set the state of the zone to installed so that we 3106865e09a4Sgjelinek * can boot it and sys-unconfig it from within the zone. However, 3107865e09a4Sgjelinek * if something fails during the boot/sys-unconfig, we want to set 3108865e09a4Sgjelinek * the state back to incomplete. We use the revert flag to keep 3109865e09a4Sgjelinek * track of this. 3110865e09a4Sgjelinek */ 3111865e09a4Sgjelinek revert = B_TRUE; 3112865e09a4Sgjelinek 3113865e09a4Sgjelinek if ((err = zone_set_state(target_zone, ZONE_STATE_INSTALLED)) != Z_OK) { 3114865e09a4Sgjelinek errno = err; 3115865e09a4Sgjelinek zperror2(target_zone, gettext("\ncould not set state")); 3116865e09a4Sgjelinek goto done; 3117865e09a4Sgjelinek } 3118865e09a4Sgjelinek 3119865e09a4Sgjelinek /* 3120865e09a4Sgjelinek * Check if the zone is already sys-unconfiged. This saves us 3121865e09a4Sgjelinek * the work of booting the zone so we can unconfigure it. 3122865e09a4Sgjelinek */ 3123865e09a4Sgjelinek (void) snprintf(cmdbuf, sizeof (cmdbuf), "%s/root/etc/.UNCONFIGURED", 3124865e09a4Sgjelinek zonepath); 3125865e09a4Sgjelinek if (stat(cmdbuf, &unconfig_buf) == -1) { 3126865e09a4Sgjelinek if ((err = boot_func(1, boot_args)) != Z_OK) { 3127865e09a4Sgjelinek errno = err; 3128865e09a4Sgjelinek zperror2(target_zone, gettext("\nCould not boot zone " 3129865e09a4Sgjelinek "for sys-unconfig\n")); 3130865e09a4Sgjelinek goto done; 3131865e09a4Sgjelinek } 3132865e09a4Sgjelinek 3133865e09a4Sgjelinek if ((err = zone_wait_single_user()) != Z_OK) { 3134865e09a4Sgjelinek errno = err; 3135865e09a4Sgjelinek zperror2(target_zone, gettext("\nCould not boot zone " 3136865e09a4Sgjelinek "for sys-unconfig\n")); 3137865e09a4Sgjelinek (void) halt_func(0, halt_args); 3138865e09a4Sgjelinek goto done; 3139865e09a4Sgjelinek } 3140865e09a4Sgjelinek 3141865e09a4Sgjelinek (void) snprintf(cmdbuf, sizeof (cmdbuf), 3142865e09a4Sgjelinek "echo y | /usr/sbin/zlogin -S %s /usr/sbin/sys-unconfig", 3143865e09a4Sgjelinek target_zone); 3144865e09a4Sgjelinek 3145865e09a4Sgjelinek status = do_subproc(cmdbuf); 3146865e09a4Sgjelinek if ((err = subproc_status("sys-unconfig", status)) != Z_OK) { 3147865e09a4Sgjelinek errno = err; 3148865e09a4Sgjelinek zperror2(target_zone, 3149865e09a4Sgjelinek gettext("\nsys-unconfig failed\n")); 3150865e09a4Sgjelinek /* 3151865e09a4Sgjelinek * The sys-unconfig halts the zone but if it failed, 3152865e09a4Sgjelinek * for some reason, we'll try to halt it now. 3153865e09a4Sgjelinek */ 3154865e09a4Sgjelinek (void) halt_func(0, halt_args); 3155865e09a4Sgjelinek goto done; 3156865e09a4Sgjelinek } 3157865e09a4Sgjelinek } 3158865e09a4Sgjelinek 3159865e09a4Sgjelinek revert = B_FALSE; 3160865e09a4Sgjelinek 3161865e09a4Sgjelinek done: 3162865e09a4Sgjelinek (void) printf("\n"); 3163865e09a4Sgjelinek 3164865e09a4Sgjelinek if (revert) 3165865e09a4Sgjelinek (void) zone_set_state(target_zone, ZONE_STATE_INCOMPLETE); 3166865e09a4Sgjelinek 3167865e09a4Sgjelinek release_lock_file(lockfd); 3168865e09a4Sgjelinek return ((err == Z_OK) ? Z_OK : Z_ERR); 3169865e09a4Sgjelinek } 3170865e09a4Sgjelinek 3171865e09a4Sgjelinek #define RMCOMMAND "/usr/bin/rm -rf" 3172865e09a4Sgjelinek 3173865e09a4Sgjelinek static int 3174865e09a4Sgjelinek move_func(int argc, char *argv[]) 3175865e09a4Sgjelinek { 3176865e09a4Sgjelinek /* 6: "exec " and " " */ 3177865e09a4Sgjelinek char cmdbuf[sizeof (RMCOMMAND) + MAXPATHLEN + 6]; 3178865e09a4Sgjelinek char *new_zonepath = NULL; 3179865e09a4Sgjelinek int lockfd; 3180865e09a4Sgjelinek int err, arg; 3181865e09a4Sgjelinek char zonepath[MAXPATHLEN]; 3182865e09a4Sgjelinek zone_dochandle_t handle; 3183865e09a4Sgjelinek boolean_t fast; 3184865e09a4Sgjelinek boolean_t revert; 3185865e09a4Sgjelinek struct stat zonepath_buf; 3186865e09a4Sgjelinek struct stat new_zonepath_buf; 3187865e09a4Sgjelinek 3188865e09a4Sgjelinek if (zonecfg_in_alt_root()) { 3189865e09a4Sgjelinek zerror(gettext("cannot move zone in alternate root")); 3190865e09a4Sgjelinek return (Z_ERR); 3191865e09a4Sgjelinek } 3192865e09a4Sgjelinek 3193865e09a4Sgjelinek optind = 0; 3194865e09a4Sgjelinek if ((arg = getopt(argc, argv, "?")) != EOF) { 3195865e09a4Sgjelinek switch (arg) { 3196865e09a4Sgjelinek case '?': 3197865e09a4Sgjelinek sub_usage(SHELP_MOVE, CMD_MOVE); 3198865e09a4Sgjelinek return (optopt == '?' ? Z_OK : Z_USAGE); 3199865e09a4Sgjelinek default: 3200865e09a4Sgjelinek sub_usage(SHELP_MOVE, CMD_MOVE); 3201865e09a4Sgjelinek return (Z_USAGE); 3202865e09a4Sgjelinek } 3203865e09a4Sgjelinek } 3204865e09a4Sgjelinek if (argc != (optind + 1)) { 3205865e09a4Sgjelinek sub_usage(SHELP_MOVE, CMD_MOVE); 3206865e09a4Sgjelinek return (Z_USAGE); 3207865e09a4Sgjelinek } 3208865e09a4Sgjelinek new_zonepath = argv[optind]; 3209865e09a4Sgjelinek if (sanity_check(target_zone, CMD_MOVE, B_FALSE, B_TRUE) != Z_OK) 3210865e09a4Sgjelinek return (Z_ERR); 3211865e09a4Sgjelinek if (verify_details(CMD_MOVE) != Z_OK) 3212865e09a4Sgjelinek return (Z_ERR); 3213865e09a4Sgjelinek 3214865e09a4Sgjelinek /* 3215865e09a4Sgjelinek * Check out the new zonepath. This has the side effect of creating 3216865e09a4Sgjelinek * a directory for the new zonepath. We depend on this later when we 3217865e09a4Sgjelinek * stat to see if we are doing a cross file-system move or not. 3218865e09a4Sgjelinek */ 3219865e09a4Sgjelinek if (validate_zonepath(new_zonepath, CMD_MOVE) != Z_OK) 3220865e09a4Sgjelinek return (Z_ERR); 3221865e09a4Sgjelinek 3222865e09a4Sgjelinek if ((err = zone_get_zonepath(target_zone, zonepath, sizeof (zonepath))) 3223865e09a4Sgjelinek != Z_OK) { 3224865e09a4Sgjelinek errno = err; 3225865e09a4Sgjelinek zperror2(target_zone, gettext("could not get zone path")); 3226865e09a4Sgjelinek return (Z_ERR); 3227865e09a4Sgjelinek } 3228865e09a4Sgjelinek 3229865e09a4Sgjelinek if (stat(zonepath, &zonepath_buf) == -1) { 3230865e09a4Sgjelinek zperror(gettext("could not stat zone path"), B_FALSE); 3231865e09a4Sgjelinek return (Z_ERR); 3232865e09a4Sgjelinek } 3233865e09a4Sgjelinek 3234865e09a4Sgjelinek if (stat(new_zonepath, &new_zonepath_buf) == -1) { 3235865e09a4Sgjelinek zperror(gettext("could not stat new zone path"), B_FALSE); 3236865e09a4Sgjelinek return (Z_ERR); 3237865e09a4Sgjelinek } 3238865e09a4Sgjelinek 3239865e09a4Sgjelinek /* Don't move the zone if anything is still mounted there */ 3240865e09a4Sgjelinek if (zonecfg_find_mounts(zonepath, NULL, NULL)) { 3241865e09a4Sgjelinek zerror(gettext("These file-systems are mounted on " 3242865e09a4Sgjelinek "subdirectories of %s.\n"), zonepath); 3243865e09a4Sgjelinek (void) zonecfg_find_mounts(zonepath, zfm_print, NULL); 3244865e09a4Sgjelinek return (Z_ERR); 3245865e09a4Sgjelinek } 3246865e09a4Sgjelinek 3247865e09a4Sgjelinek /* 3248865e09a4Sgjelinek * Check if we are moving in the same filesystem and can do a fast 3249865e09a4Sgjelinek * move or if we are crossing filesystems and have to copy the data. 3250865e09a4Sgjelinek */ 3251865e09a4Sgjelinek fast = (zonepath_buf.st_dev == new_zonepath_buf.st_dev); 3252865e09a4Sgjelinek 3253865e09a4Sgjelinek if ((handle = zonecfg_init_handle()) == NULL) { 3254865e09a4Sgjelinek zperror(cmd_to_str(CMD_MOVE), B_TRUE); 3255865e09a4Sgjelinek return (Z_ERR); 3256865e09a4Sgjelinek } 3257865e09a4Sgjelinek 3258865e09a4Sgjelinek if ((err = zonecfg_get_handle(target_zone, handle)) != Z_OK) { 3259865e09a4Sgjelinek errno = err; 3260865e09a4Sgjelinek zperror(cmd_to_str(CMD_MOVE), B_TRUE); 3261865e09a4Sgjelinek zonecfg_fini_handle(handle); 3262865e09a4Sgjelinek return (Z_ERR); 3263865e09a4Sgjelinek } 3264865e09a4Sgjelinek 3265865e09a4Sgjelinek if (grab_lock_file(target_zone, &lockfd) != Z_OK) { 3266865e09a4Sgjelinek zerror(gettext("another %s may have an operation in progress."), 3267865e09a4Sgjelinek "zoneadm"); 3268865e09a4Sgjelinek zonecfg_fini_handle(handle); 3269865e09a4Sgjelinek return (Z_ERR); 3270865e09a4Sgjelinek } 3271865e09a4Sgjelinek 3272865e09a4Sgjelinek /* 3273865e09a4Sgjelinek * We're making some file-system changes now so we have to clean up 3274865e09a4Sgjelinek * the file-system before we are done. This will either clean up the 3275865e09a4Sgjelinek * new zonepath if the zonecfg update failed or it will clean up the 3276865e09a4Sgjelinek * old zonepath if everything is ok. 3277865e09a4Sgjelinek */ 3278865e09a4Sgjelinek revert = B_TRUE; 3279865e09a4Sgjelinek 3280865e09a4Sgjelinek if (fast) { 3281865e09a4Sgjelinek /* same filesystem, use rename for a quick move */ 3282865e09a4Sgjelinek 3283865e09a4Sgjelinek /* 3284865e09a4Sgjelinek * Remove the new_zonepath directory that got created above 3285865e09a4Sgjelinek * during the validation. It gets in the way of the rename. 3286865e09a4Sgjelinek */ 3287865e09a4Sgjelinek if (rmdir(new_zonepath) != 0) { 3288865e09a4Sgjelinek zperror(gettext("could not rmdir new zone path"), 3289865e09a4Sgjelinek B_FALSE); 3290865e09a4Sgjelinek zonecfg_fini_handle(handle); 3291865e09a4Sgjelinek release_lock_file(lockfd); 3292865e09a4Sgjelinek return (Z_ERR); 3293865e09a4Sgjelinek } 3294865e09a4Sgjelinek 3295865e09a4Sgjelinek if (rename(zonepath, new_zonepath) != 0) { 3296865e09a4Sgjelinek /* 3297865e09a4Sgjelinek * If this fails we don't need to do all of the 3298865e09a4Sgjelinek * cleanup that happens for the rest of the code 3299865e09a4Sgjelinek * so just return from this error. 3300865e09a4Sgjelinek */ 3301865e09a4Sgjelinek zperror(gettext("could not move zone"), B_FALSE); 3302865e09a4Sgjelinek zonecfg_fini_handle(handle); 3303865e09a4Sgjelinek release_lock_file(lockfd); 3304865e09a4Sgjelinek return (Z_ERR); 3305865e09a4Sgjelinek } 3306865e09a4Sgjelinek 3307865e09a4Sgjelinek } else { 3308865e09a4Sgjelinek (void) printf(gettext( 3309865e09a4Sgjelinek "Moving across file-systems; copying zonepath %s..."), 3310865e09a4Sgjelinek zonepath); 3311865e09a4Sgjelinek (void) fflush(stdout); 3312865e09a4Sgjelinek 3313865e09a4Sgjelinek err = copy_zone(zonepath, new_zonepath); 3314865e09a4Sgjelinek 3315865e09a4Sgjelinek (void) printf("\n"); 3316865e09a4Sgjelinek if (err != Z_OK) 3317865e09a4Sgjelinek goto done; 3318865e09a4Sgjelinek } 3319865e09a4Sgjelinek 3320865e09a4Sgjelinek if ((err = zonecfg_set_zonepath(handle, new_zonepath)) != Z_OK) { 3321865e09a4Sgjelinek errno = err; 3322865e09a4Sgjelinek zperror(gettext("could not set new zonepath"), B_TRUE); 3323865e09a4Sgjelinek goto done; 3324865e09a4Sgjelinek } 3325865e09a4Sgjelinek 3326865e09a4Sgjelinek if ((err = zonecfg_save(handle)) != Z_OK) { 3327865e09a4Sgjelinek errno = err; 3328865e09a4Sgjelinek zperror(gettext("zonecfg save failed"), B_TRUE); 3329865e09a4Sgjelinek goto done; 3330865e09a4Sgjelinek } 3331865e09a4Sgjelinek 3332865e09a4Sgjelinek revert = B_FALSE; 3333865e09a4Sgjelinek 3334865e09a4Sgjelinek done: 3335865e09a4Sgjelinek zonecfg_fini_handle(handle); 3336865e09a4Sgjelinek release_lock_file(lockfd); 3337865e09a4Sgjelinek 3338865e09a4Sgjelinek /* 3339865e09a4Sgjelinek * Clean up the file-system based on how things went. We either 3340865e09a4Sgjelinek * clean up the new zonepath if the operation failed for some reason 3341865e09a4Sgjelinek * or we clean up the old zonepath if everything is ok. 3342865e09a4Sgjelinek */ 3343865e09a4Sgjelinek if (revert) { 3344865e09a4Sgjelinek /* The zonecfg update failed, cleanup the new zonepath. */ 3345865e09a4Sgjelinek if (fast) { 3346865e09a4Sgjelinek if (rename(new_zonepath, zonepath) != 0) { 3347865e09a4Sgjelinek zperror(gettext("could not restore zonepath"), 3348865e09a4Sgjelinek B_FALSE); 3349865e09a4Sgjelinek /* 3350865e09a4Sgjelinek * err is already != Z_OK since we're reverting 3351865e09a4Sgjelinek */ 3352865e09a4Sgjelinek } 3353865e09a4Sgjelinek } else { 3354865e09a4Sgjelinek int status; 3355865e09a4Sgjelinek 3356865e09a4Sgjelinek (void) printf(gettext("Cleaning up zonepath %s..."), 3357865e09a4Sgjelinek new_zonepath); 3358865e09a4Sgjelinek (void) fflush(stdout); 3359865e09a4Sgjelinek 3360865e09a4Sgjelinek /* 3361865e09a4Sgjelinek * "exec" the command so that the returned status is 3362865e09a4Sgjelinek * that of rm and not the shell. 3363865e09a4Sgjelinek */ 3364865e09a4Sgjelinek (void) snprintf(cmdbuf, sizeof (cmdbuf), 3365865e09a4Sgjelinek "exec " RMCOMMAND " %s", new_zonepath); 3366865e09a4Sgjelinek 3367865e09a4Sgjelinek status = do_subproc(cmdbuf); 3368865e09a4Sgjelinek 3369865e09a4Sgjelinek (void) printf("\n"); 3370865e09a4Sgjelinek 3371865e09a4Sgjelinek if ((err = subproc_status("rm", status)) != Z_OK) { 3372865e09a4Sgjelinek errno = err; 3373865e09a4Sgjelinek zperror(gettext("could not remove new " 3374865e09a4Sgjelinek "zonepath"), B_TRUE); 3375865e09a4Sgjelinek } else { 3376865e09a4Sgjelinek /* 3377865e09a4Sgjelinek * Because we're reverting we know the mainline 3378865e09a4Sgjelinek * code failed but we just reused the err 3379865e09a4Sgjelinek * variable so we reset it back to Z_ERR. 3380865e09a4Sgjelinek */ 3381865e09a4Sgjelinek err = Z_ERR; 3382865e09a4Sgjelinek } 3383865e09a4Sgjelinek } 3384865e09a4Sgjelinek 3385865e09a4Sgjelinek } else { 3386865e09a4Sgjelinek /* The move was successful, cleanup the old zonepath. */ 3387865e09a4Sgjelinek if (!fast) { 3388865e09a4Sgjelinek int status; 3389865e09a4Sgjelinek 3390865e09a4Sgjelinek (void) printf( 3391865e09a4Sgjelinek gettext("Cleaning up zonepath %s..."), zonepath); 3392865e09a4Sgjelinek (void) fflush(stdout); 3393865e09a4Sgjelinek 3394865e09a4Sgjelinek /* 3395865e09a4Sgjelinek * "exec" the command so that the returned status is 3396865e09a4Sgjelinek * that of rm and not the shell. 3397865e09a4Sgjelinek */ 3398865e09a4Sgjelinek (void) snprintf(cmdbuf, sizeof (cmdbuf), 3399865e09a4Sgjelinek "exec " RMCOMMAND " %s", zonepath); 3400865e09a4Sgjelinek 3401865e09a4Sgjelinek status = do_subproc(cmdbuf); 3402865e09a4Sgjelinek 3403865e09a4Sgjelinek (void) printf("\n"); 3404865e09a4Sgjelinek 3405865e09a4Sgjelinek if ((err = subproc_status("rm", status)) != Z_OK) { 3406865e09a4Sgjelinek errno = err; 3407865e09a4Sgjelinek zperror(gettext("could not remove zonepath"), 3408865e09a4Sgjelinek B_TRUE); 3409865e09a4Sgjelinek } 3410865e09a4Sgjelinek } 3411865e09a4Sgjelinek } 3412865e09a4Sgjelinek 3413865e09a4Sgjelinek return ((err == Z_OK) ? Z_OK : Z_ERR); 3414865e09a4Sgjelinek } 3415865e09a4Sgjelinek 3416865e09a4Sgjelinek /* 34177c478bd9Sstevel@tonic-gate * On input, TRUE => yes, FALSE => no. 34187c478bd9Sstevel@tonic-gate * On return, TRUE => 1, FALSE => 0, could not ask => -1. 34197c478bd9Sstevel@tonic-gate */ 34207c478bd9Sstevel@tonic-gate 34217c478bd9Sstevel@tonic-gate static int 34227c478bd9Sstevel@tonic-gate ask_yesno(boolean_t default_answer, const char *question) 34237c478bd9Sstevel@tonic-gate { 34247c478bd9Sstevel@tonic-gate char line[64]; /* should be large enough to answer yes or no */ 34257c478bd9Sstevel@tonic-gate 34267c478bd9Sstevel@tonic-gate if (!isatty(STDIN_FILENO)) 34277c478bd9Sstevel@tonic-gate return (-1); 34287c478bd9Sstevel@tonic-gate for (;;) { 34297c478bd9Sstevel@tonic-gate (void) printf("%s (%s)? ", question, 34307c478bd9Sstevel@tonic-gate default_answer ? "[y]/n" : "y/[n]"); 34317c478bd9Sstevel@tonic-gate if (fgets(line, sizeof (line), stdin) == NULL || 34327c478bd9Sstevel@tonic-gate line[0] == '\n') 34337c478bd9Sstevel@tonic-gate return (default_answer ? 1 : 0); 34347c478bd9Sstevel@tonic-gate if (tolower(line[0]) == 'y') 34357c478bd9Sstevel@tonic-gate return (1); 34367c478bd9Sstevel@tonic-gate if (tolower(line[0]) == 'n') 34377c478bd9Sstevel@tonic-gate return (0); 34387c478bd9Sstevel@tonic-gate } 34397c478bd9Sstevel@tonic-gate } 34407c478bd9Sstevel@tonic-gate 34417c478bd9Sstevel@tonic-gate static int 34427c478bd9Sstevel@tonic-gate uninstall_func(int argc, char *argv[]) 34437c478bd9Sstevel@tonic-gate { 34447c478bd9Sstevel@tonic-gate /* 6: "exec " and " " */ 34457c478bd9Sstevel@tonic-gate char cmdbuf[sizeof (RMCOMMAND) + MAXPATHLEN + 6]; 34467c478bd9Sstevel@tonic-gate char line[ZONENAME_MAX + 128]; /* Enough for "Are you sure ..." */ 34477c478bd9Sstevel@tonic-gate char rootpath[MAXPATHLEN], devpath[MAXPATHLEN]; 34487c478bd9Sstevel@tonic-gate boolean_t force = B_FALSE; 34497c478bd9Sstevel@tonic-gate int lockfd, answer; 34507c478bd9Sstevel@tonic-gate int err, arg; 34517c478bd9Sstevel@tonic-gate int status; 34527c478bd9Sstevel@tonic-gate 3453108322fbScarlsonj if (zonecfg_in_alt_root()) { 3454108322fbScarlsonj zerror(gettext("cannot uninstall zone in alternate root")); 3455108322fbScarlsonj return (Z_ERR); 3456108322fbScarlsonj } 3457108322fbScarlsonj 34587c478bd9Sstevel@tonic-gate optind = 0; 34597c478bd9Sstevel@tonic-gate while ((arg = getopt(argc, argv, "?F")) != EOF) { 34607c478bd9Sstevel@tonic-gate switch (arg) { 34617c478bd9Sstevel@tonic-gate case '?': 34627c478bd9Sstevel@tonic-gate sub_usage(SHELP_UNINSTALL, CMD_UNINSTALL); 34637c478bd9Sstevel@tonic-gate return (optopt == '?' ? Z_OK : Z_USAGE); 34647c478bd9Sstevel@tonic-gate case 'F': 34657c478bd9Sstevel@tonic-gate force = B_TRUE; 34667c478bd9Sstevel@tonic-gate break; 34677c478bd9Sstevel@tonic-gate default: 34687c478bd9Sstevel@tonic-gate sub_usage(SHELP_UNINSTALL, CMD_UNINSTALL); 34697c478bd9Sstevel@tonic-gate return (Z_USAGE); 34707c478bd9Sstevel@tonic-gate } 34717c478bd9Sstevel@tonic-gate } 34727c478bd9Sstevel@tonic-gate if (argc > optind) { 34737c478bd9Sstevel@tonic-gate sub_usage(SHELP_UNINSTALL, CMD_UNINSTALL); 34747c478bd9Sstevel@tonic-gate return (Z_USAGE); 34757c478bd9Sstevel@tonic-gate } 34767c478bd9Sstevel@tonic-gate 34777c478bd9Sstevel@tonic-gate if (sanity_check(target_zone, CMD_UNINSTALL, B_FALSE, B_TRUE) != Z_OK) 34787c478bd9Sstevel@tonic-gate return (Z_ERR); 34797c478bd9Sstevel@tonic-gate 34807c478bd9Sstevel@tonic-gate if (!force) { 34817c478bd9Sstevel@tonic-gate (void) snprintf(line, sizeof (line), 34827c478bd9Sstevel@tonic-gate gettext("Are you sure you want to %s zone %s"), 34837c478bd9Sstevel@tonic-gate cmd_to_str(CMD_UNINSTALL), target_zone); 34847c478bd9Sstevel@tonic-gate if ((answer = ask_yesno(B_FALSE, line)) == 0) { 34857c478bd9Sstevel@tonic-gate return (Z_OK); 34867c478bd9Sstevel@tonic-gate } else if (answer == -1) { 34877c478bd9Sstevel@tonic-gate zerror(gettext("Input not from terminal and -F " 34887c478bd9Sstevel@tonic-gate "not specified: %s not done."), 34897c478bd9Sstevel@tonic-gate cmd_to_str(CMD_UNINSTALL)); 34907c478bd9Sstevel@tonic-gate return (Z_ERR); 34917c478bd9Sstevel@tonic-gate } 34927c478bd9Sstevel@tonic-gate } 34937c478bd9Sstevel@tonic-gate 34947c478bd9Sstevel@tonic-gate if ((err = zone_get_zonepath(target_zone, devpath, 34957c478bd9Sstevel@tonic-gate sizeof (devpath))) != Z_OK) { 34967c478bd9Sstevel@tonic-gate errno = err; 34977c478bd9Sstevel@tonic-gate zperror2(target_zone, gettext("could not get zone path")); 34987c478bd9Sstevel@tonic-gate return (Z_ERR); 34997c478bd9Sstevel@tonic-gate } 35007c478bd9Sstevel@tonic-gate (void) strlcat(devpath, "/dev", sizeof (devpath)); 35017c478bd9Sstevel@tonic-gate if ((err = zone_get_rootpath(target_zone, rootpath, 35027c478bd9Sstevel@tonic-gate sizeof (rootpath))) != Z_OK) { 35037c478bd9Sstevel@tonic-gate errno = err; 35047c478bd9Sstevel@tonic-gate zperror2(target_zone, gettext("could not get root path")); 35057c478bd9Sstevel@tonic-gate return (Z_ERR); 35067c478bd9Sstevel@tonic-gate } 35077c478bd9Sstevel@tonic-gate 35087c478bd9Sstevel@tonic-gate /* 35097c478bd9Sstevel@tonic-gate * If there seems to be a zoneadmd running for this zone, call it 35107c478bd9Sstevel@tonic-gate * to tell it that an uninstall is happening; if all goes well it 35117c478bd9Sstevel@tonic-gate * will then shut itself down. 35127c478bd9Sstevel@tonic-gate */ 35137c478bd9Sstevel@tonic-gate if (ping_zoneadmd(target_zone) == Z_OK) { 35147c478bd9Sstevel@tonic-gate zone_cmd_arg_t zarg; 35157c478bd9Sstevel@tonic-gate zarg.cmd = Z_NOTE_UNINSTALLING; 35167c478bd9Sstevel@tonic-gate /* we don't care too much if this fails... just plow on */ 35177c478bd9Sstevel@tonic-gate (void) call_zoneadmd(target_zone, &zarg); 35187c478bd9Sstevel@tonic-gate } 35197c478bd9Sstevel@tonic-gate 35207c478bd9Sstevel@tonic-gate if (grab_lock_file(target_zone, &lockfd) != Z_OK) { 35217c478bd9Sstevel@tonic-gate zerror(gettext("another %s may have an operation in progress."), 3522865e09a4Sgjelinek "zoneadm"); 35237c478bd9Sstevel@tonic-gate return (Z_ERR); 35247c478bd9Sstevel@tonic-gate } 35257c478bd9Sstevel@tonic-gate 35267c478bd9Sstevel@tonic-gate /* Don't uninstall the zone if anything is mounted there */ 35277c478bd9Sstevel@tonic-gate err = zonecfg_find_mounts(rootpath, NULL, NULL); 35287c478bd9Sstevel@tonic-gate if (err) { 35297c478bd9Sstevel@tonic-gate zerror(gettext("These file-systems are mounted on " 35307c478bd9Sstevel@tonic-gate "subdirectories of %s.\n"), rootpath); 35317c478bd9Sstevel@tonic-gate (void) zonecfg_find_mounts(rootpath, zfm_print, NULL); 35327c478bd9Sstevel@tonic-gate return (Z_ERR); 35337c478bd9Sstevel@tonic-gate } 35347c478bd9Sstevel@tonic-gate 35357c478bd9Sstevel@tonic-gate err = zone_set_state(target_zone, ZONE_STATE_INCOMPLETE); 35367c478bd9Sstevel@tonic-gate if (err != Z_OK) { 35377c478bd9Sstevel@tonic-gate errno = err; 35387c478bd9Sstevel@tonic-gate zperror2(target_zone, gettext("could not set state")); 35397c478bd9Sstevel@tonic-gate goto bad; 35407c478bd9Sstevel@tonic-gate } 35417c478bd9Sstevel@tonic-gate 35427c478bd9Sstevel@tonic-gate /* 35437c478bd9Sstevel@tonic-gate * "exec" the command so that the returned status is that of 35447c478bd9Sstevel@tonic-gate * RMCOMMAND and not the shell. 35457c478bd9Sstevel@tonic-gate */ 35467c478bd9Sstevel@tonic-gate (void) snprintf(cmdbuf, sizeof (cmdbuf), "exec " RMCOMMAND " %s", 35477c478bd9Sstevel@tonic-gate devpath); 35487c478bd9Sstevel@tonic-gate status = do_subproc(cmdbuf); 35497c478bd9Sstevel@tonic-gate if ((err = subproc_status(RMCOMMAND, status)) != Z_OK) 35507c478bd9Sstevel@tonic-gate goto bad; 35517c478bd9Sstevel@tonic-gate (void) snprintf(cmdbuf, sizeof (cmdbuf), "exec " RMCOMMAND " %s", 35527c478bd9Sstevel@tonic-gate rootpath); 35537c478bd9Sstevel@tonic-gate status = do_subproc(cmdbuf); 35547c478bd9Sstevel@tonic-gate if ((err = subproc_status(RMCOMMAND, status)) != Z_OK) 35557c478bd9Sstevel@tonic-gate goto bad; 35567c478bd9Sstevel@tonic-gate err = zone_set_state(target_zone, ZONE_STATE_CONFIGURED); 35577c478bd9Sstevel@tonic-gate if (err != Z_OK) { 35587c478bd9Sstevel@tonic-gate errno = err; 35597c478bd9Sstevel@tonic-gate zperror2(target_zone, gettext("could not reset state")); 35607c478bd9Sstevel@tonic-gate } 35617c478bd9Sstevel@tonic-gate bad: 35627c478bd9Sstevel@tonic-gate release_lock_file(lockfd); 35637c478bd9Sstevel@tonic-gate return (err); 35647c478bd9Sstevel@tonic-gate } 35657c478bd9Sstevel@tonic-gate 3566108322fbScarlsonj /* ARGSUSED */ 3567108322fbScarlsonj static int 3568108322fbScarlsonj mount_func(int argc, char *argv[]) 3569108322fbScarlsonj { 3570108322fbScarlsonj zone_cmd_arg_t zarg; 3571108322fbScarlsonj 3572108322fbScarlsonj if (argc > 0) 3573108322fbScarlsonj return (Z_USAGE); 3574108322fbScarlsonj if (sanity_check(target_zone, CMD_MOUNT, B_FALSE, B_FALSE) != Z_OK) 3575108322fbScarlsonj return (Z_ERR); 3576108322fbScarlsonj if (verify_details(CMD_MOUNT) != Z_OK) 3577108322fbScarlsonj return (Z_ERR); 3578108322fbScarlsonj 3579108322fbScarlsonj zarg.cmd = Z_MOUNT; 3580108322fbScarlsonj if (call_zoneadmd(target_zone, &zarg) != 0) { 3581108322fbScarlsonj zerror(gettext("call to %s failed"), "zoneadmd"); 3582108322fbScarlsonj return (Z_ERR); 3583108322fbScarlsonj } 3584108322fbScarlsonj return (Z_OK); 3585108322fbScarlsonj } 3586108322fbScarlsonj 3587108322fbScarlsonj /* ARGSUSED */ 3588108322fbScarlsonj static int 3589108322fbScarlsonj unmount_func(int argc, char *argv[]) 3590108322fbScarlsonj { 3591108322fbScarlsonj zone_cmd_arg_t zarg; 3592108322fbScarlsonj 3593108322fbScarlsonj if (argc > 0) 3594108322fbScarlsonj return (Z_USAGE); 3595108322fbScarlsonj if (sanity_check(target_zone, CMD_UNMOUNT, B_FALSE, B_FALSE) != Z_OK) 3596108322fbScarlsonj return (Z_ERR); 3597108322fbScarlsonj 3598108322fbScarlsonj zarg.cmd = Z_UNMOUNT; 3599108322fbScarlsonj if (call_zoneadmd(target_zone, &zarg) != 0) { 3600108322fbScarlsonj zerror(gettext("call to %s failed"), "zoneadmd"); 3601108322fbScarlsonj return (Z_ERR); 3602108322fbScarlsonj } 3603108322fbScarlsonj return (Z_OK); 3604108322fbScarlsonj } 3605108322fbScarlsonj 36067c478bd9Sstevel@tonic-gate static int 36077c478bd9Sstevel@tonic-gate help_func(int argc, char *argv[]) 36087c478bd9Sstevel@tonic-gate { 36097c478bd9Sstevel@tonic-gate int arg, cmd_num; 36107c478bd9Sstevel@tonic-gate 36117c478bd9Sstevel@tonic-gate if (argc == 0) { 36127c478bd9Sstevel@tonic-gate (void) usage(B_TRUE); 36137c478bd9Sstevel@tonic-gate return (Z_OK); 36147c478bd9Sstevel@tonic-gate } 36157c478bd9Sstevel@tonic-gate optind = 0; 36167c478bd9Sstevel@tonic-gate if ((arg = getopt(argc, argv, "?")) != EOF) { 36177c478bd9Sstevel@tonic-gate switch (arg) { 36187c478bd9Sstevel@tonic-gate case '?': 36197c478bd9Sstevel@tonic-gate sub_usage(SHELP_HELP, CMD_HELP); 36207c478bd9Sstevel@tonic-gate return (optopt == '?' ? Z_OK : Z_USAGE); 36217c478bd9Sstevel@tonic-gate default: 36227c478bd9Sstevel@tonic-gate sub_usage(SHELP_HELP, CMD_HELP); 36237c478bd9Sstevel@tonic-gate return (Z_USAGE); 36247c478bd9Sstevel@tonic-gate } 36257c478bd9Sstevel@tonic-gate } 36267c478bd9Sstevel@tonic-gate while (optind < argc) { 3627394a25e2Scarlsonj /* Private commands have NULL short_usage; omit them */ 3628394a25e2Scarlsonj if ((cmd_num = cmd_match(argv[optind])) < 0 || 3629394a25e2Scarlsonj cmdtab[cmd_num].short_usage == NULL) { 36307c478bd9Sstevel@tonic-gate sub_usage(SHELP_HELP, CMD_HELP); 36317c478bd9Sstevel@tonic-gate return (Z_USAGE); 36327c478bd9Sstevel@tonic-gate } 36337c478bd9Sstevel@tonic-gate sub_usage(cmdtab[cmd_num].short_usage, cmd_num); 36347c478bd9Sstevel@tonic-gate optind++; 36357c478bd9Sstevel@tonic-gate } 36367c478bd9Sstevel@tonic-gate return (Z_OK); 36377c478bd9Sstevel@tonic-gate } 36387c478bd9Sstevel@tonic-gate 36397c478bd9Sstevel@tonic-gate /* 36407c478bd9Sstevel@tonic-gate * Returns: CMD_MIN thru CMD_MAX on success, -1 on error 36417c478bd9Sstevel@tonic-gate */ 36427c478bd9Sstevel@tonic-gate 36437c478bd9Sstevel@tonic-gate static int 36447c478bd9Sstevel@tonic-gate cmd_match(char *cmd) 36457c478bd9Sstevel@tonic-gate { 36467c478bd9Sstevel@tonic-gate int i; 36477c478bd9Sstevel@tonic-gate 36487c478bd9Sstevel@tonic-gate for (i = CMD_MIN; i <= CMD_MAX; i++) { 36497c478bd9Sstevel@tonic-gate /* return only if there is an exact match */ 36507c478bd9Sstevel@tonic-gate if (strcmp(cmd, cmdtab[i].cmd_name) == 0) 36517c478bd9Sstevel@tonic-gate return (cmdtab[i].cmd_num); 36527c478bd9Sstevel@tonic-gate } 36537c478bd9Sstevel@tonic-gate return (-1); 36547c478bd9Sstevel@tonic-gate } 36557c478bd9Sstevel@tonic-gate 36567c478bd9Sstevel@tonic-gate static int 36577c478bd9Sstevel@tonic-gate parse_and_run(int argc, char *argv[]) 36587c478bd9Sstevel@tonic-gate { 36597c478bd9Sstevel@tonic-gate int i = cmd_match(argv[0]); 36607c478bd9Sstevel@tonic-gate 36617c478bd9Sstevel@tonic-gate if (i < 0) 36627c478bd9Sstevel@tonic-gate return (usage(B_FALSE)); 36637c478bd9Sstevel@tonic-gate return (cmdtab[i].handler(argc - 1, &(argv[1]))); 36647c478bd9Sstevel@tonic-gate } 36657c478bd9Sstevel@tonic-gate 36667c478bd9Sstevel@tonic-gate static char * 36677c478bd9Sstevel@tonic-gate get_execbasename(char *execfullname) 36687c478bd9Sstevel@tonic-gate { 36697c478bd9Sstevel@tonic-gate char *last_slash, *execbasename; 36707c478bd9Sstevel@tonic-gate 36717c478bd9Sstevel@tonic-gate /* guard against '/' at end of command invocation */ 36727c478bd9Sstevel@tonic-gate for (;;) { 36737c478bd9Sstevel@tonic-gate last_slash = strrchr(execfullname, '/'); 36747c478bd9Sstevel@tonic-gate if (last_slash == NULL) { 36757c478bd9Sstevel@tonic-gate execbasename = execfullname; 36767c478bd9Sstevel@tonic-gate break; 36777c478bd9Sstevel@tonic-gate } else { 36787c478bd9Sstevel@tonic-gate execbasename = last_slash + 1; 36797c478bd9Sstevel@tonic-gate if (*execbasename == '\0') { 36807c478bd9Sstevel@tonic-gate *last_slash = '\0'; 36817c478bd9Sstevel@tonic-gate continue; 36827c478bd9Sstevel@tonic-gate } 36837c478bd9Sstevel@tonic-gate break; 36847c478bd9Sstevel@tonic-gate } 36857c478bd9Sstevel@tonic-gate } 36867c478bd9Sstevel@tonic-gate return (execbasename); 36877c478bd9Sstevel@tonic-gate } 36887c478bd9Sstevel@tonic-gate 36897c478bd9Sstevel@tonic-gate int 36907c478bd9Sstevel@tonic-gate main(int argc, char **argv) 36917c478bd9Sstevel@tonic-gate { 36927c478bd9Sstevel@tonic-gate int arg; 36937c478bd9Sstevel@tonic-gate zoneid_t zid; 3694108322fbScarlsonj struct stat st; 36957c478bd9Sstevel@tonic-gate 36967c478bd9Sstevel@tonic-gate if ((locale = setlocale(LC_ALL, "")) == NULL) 36977c478bd9Sstevel@tonic-gate locale = "C"; 36987c478bd9Sstevel@tonic-gate (void) textdomain(TEXT_DOMAIN); 36997c478bd9Sstevel@tonic-gate setbuf(stdout, NULL); 37007c478bd9Sstevel@tonic-gate (void) sigset(SIGHUP, SIG_IGN); 37017c478bd9Sstevel@tonic-gate execname = get_execbasename(argv[0]); 37027c478bd9Sstevel@tonic-gate target_zone = NULL; 37037c478bd9Sstevel@tonic-gate if (chdir("/") != 0) { 37047c478bd9Sstevel@tonic-gate zerror(gettext("could not change directory to /.")); 37057c478bd9Sstevel@tonic-gate exit(Z_ERR); 37067c478bd9Sstevel@tonic-gate } 37077c478bd9Sstevel@tonic-gate 3708108322fbScarlsonj while ((arg = getopt(argc, argv, "?z:R:")) != EOF) { 37097c478bd9Sstevel@tonic-gate switch (arg) { 37107c478bd9Sstevel@tonic-gate case '?': 37117c478bd9Sstevel@tonic-gate return (usage(B_TRUE)); 37127c478bd9Sstevel@tonic-gate case 'z': 37137c478bd9Sstevel@tonic-gate target_zone = optarg; 37147c478bd9Sstevel@tonic-gate break; 3715108322fbScarlsonj case 'R': /* private option for admin/install use */ 3716108322fbScarlsonj if (*optarg != '/') { 3717108322fbScarlsonj zerror(gettext("root path must be absolute.")); 3718108322fbScarlsonj exit(Z_ERR); 3719108322fbScarlsonj } 3720108322fbScarlsonj if (stat(optarg, &st) == -1 || !S_ISDIR(st.st_mode)) { 3721108322fbScarlsonj zerror( 3722108322fbScarlsonj gettext("root path must be a directory.")); 3723108322fbScarlsonj exit(Z_ERR); 3724108322fbScarlsonj } 3725108322fbScarlsonj zonecfg_set_root(optarg); 3726108322fbScarlsonj break; 37277c478bd9Sstevel@tonic-gate default: 37287c478bd9Sstevel@tonic-gate return (usage(B_FALSE)); 37297c478bd9Sstevel@tonic-gate } 37307c478bd9Sstevel@tonic-gate } 37317c478bd9Sstevel@tonic-gate 37327c478bd9Sstevel@tonic-gate if (optind >= argc) 37337c478bd9Sstevel@tonic-gate return (usage(B_FALSE)); 37347c478bd9Sstevel@tonic-gate if (target_zone != NULL && zone_get_id(target_zone, &zid) != 0) { 37357c478bd9Sstevel@tonic-gate errno = Z_NO_ZONE; 37367c478bd9Sstevel@tonic-gate zperror(target_zone, B_TRUE); 37377c478bd9Sstevel@tonic-gate exit(Z_ERR); 37387c478bd9Sstevel@tonic-gate } 37397c478bd9Sstevel@tonic-gate return (parse_and_run(argc - optind, &argv[optind])); 37407c478bd9Sstevel@tonic-gate } 3741