17c478bd9Sstevel@tonic-gate /* 27c478bd9Sstevel@tonic-gate * CDDL HEADER START 37c478bd9Sstevel@tonic-gate * 47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5ee519a1fSgjelinek * Common Development and Distribution License (the "License"). 6ee519a1fSgjelinek * You may not use this file except in compliance with the License. 77c478bd9Sstevel@tonic-gate * 87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 117c478bd9Sstevel@tonic-gate * and limitations under the License. 127c478bd9Sstevel@tonic-gate * 137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 187c478bd9Sstevel@tonic-gate * 197c478bd9Sstevel@tonic-gate * CDDL HEADER END 207c478bd9Sstevel@tonic-gate */ 21ffbafc53Scomay 227c478bd9Sstevel@tonic-gate /* 23*34eb7836Sjv227347 * Copyright 2010 Sun Microsystems, Inc. All rights reserved. 247c478bd9Sstevel@tonic-gate * Use is subject to license terms. 257c478bd9Sstevel@tonic-gate */ 267c478bd9Sstevel@tonic-gate 27cf8f45c7Sdstaff #include <libsysevent.h> 28cf8f45c7Sdstaff #include <pthread.h> 29cf8f45c7Sdstaff #include <stdlib.h> 307c478bd9Sstevel@tonic-gate #include <errno.h> 317c478bd9Sstevel@tonic-gate #include <fnmatch.h> 327c478bd9Sstevel@tonic-gate #include <strings.h> 337c478bd9Sstevel@tonic-gate #include <unistd.h> 347c478bd9Sstevel@tonic-gate #include <assert.h> 357c478bd9Sstevel@tonic-gate #include <libgen.h> 367c478bd9Sstevel@tonic-gate #include <libintl.h> 377c478bd9Sstevel@tonic-gate #include <alloca.h> 387c478bd9Sstevel@tonic-gate #include <ctype.h> 399acbbeafSnn35248 #include <sys/acl.h> 409acbbeafSnn35248 #include <sys/stat.h> 419acbbeafSnn35248 #include <sys/brand.h> 427c478bd9Sstevel@tonic-gate #include <sys/mntio.h> 437c478bd9Sstevel@tonic-gate #include <sys/mnttab.h> 44cf8f45c7Sdstaff #include <sys/nvpair.h> 459acbbeafSnn35248 #include <sys/types.h> 46f4b3ec61Sdh155122 #include <sys/sockio.h> 475679c89fSjv227347 #include <sys/systeminfo.h> 48ee519a1fSgjelinek #include <ftw.h> 490209230bSgjelinek #include <pool.h> 500209230bSgjelinek #include <libscf.h> 510209230bSgjelinek #include <libproc.h> 520209230bSgjelinek #include <sys/priocntl.h> 5339935be5Sgjelinek #include <libuutil.h> 54ff17c8bfSgjelinek #include <wait.h> 55ff17c8bfSgjelinek #include <bsm/adt.h> 567c478bd9Sstevel@tonic-gate 577c478bd9Sstevel@tonic-gate #include <arpa/inet.h> 587c478bd9Sstevel@tonic-gate #include <netdb.h> 597c478bd9Sstevel@tonic-gate 607c478bd9Sstevel@tonic-gate #include <libxml/xmlmemory.h> 617c478bd9Sstevel@tonic-gate #include <libxml/parser.h> 627c478bd9Sstevel@tonic-gate 637c478bd9Sstevel@tonic-gate #include <libdevinfo.h> 64108322fbScarlsonj #include <uuid/uuid.h> 65ee519a1fSgjelinek #include <dirent.h> 669acbbeafSnn35248 #include <libbrand.h> 67ee519a1fSgjelinek 687c478bd9Sstevel@tonic-gate #include <libzonecfg.h> 697c478bd9Sstevel@tonic-gate #include "zonecfg_impl.h" 707c478bd9Sstevel@tonic-gate 717c478bd9Sstevel@tonic-gate #define _PATH_TMPFILE "/zonecfg.XXXXXX" 72cf8f45c7Sdstaff #define ZONE_CB_RETRY_COUNT 10 73cf8f45c7Sdstaff #define ZONE_EVENT_PING_SUBCLASS "ping" 74cf8f45c7Sdstaff #define ZONE_EVENT_PING_PUBLISHER "solaris" 757c478bd9Sstevel@tonic-gate 767c478bd9Sstevel@tonic-gate /* Hard-code the DTD element/attribute/entity names just once, here. */ 777c478bd9Sstevel@tonic-gate #define DTD_ELEM_ATTR (const xmlChar *) "attr" 787c478bd9Sstevel@tonic-gate #define DTD_ELEM_COMMENT (const xmlChar *) "comment" 797c478bd9Sstevel@tonic-gate #define DTD_ELEM_DEVICE (const xmlChar *) "device" 807c478bd9Sstevel@tonic-gate #define DTD_ELEM_FS (const xmlChar *) "filesystem" 817c478bd9Sstevel@tonic-gate #define DTD_ELEM_FSOPTION (const xmlChar *) "fsoption" 827c478bd9Sstevel@tonic-gate #define DTD_ELEM_IPD (const xmlChar *) "inherited-pkg-dir" 837c478bd9Sstevel@tonic-gate #define DTD_ELEM_NET (const xmlChar *) "network" 847c478bd9Sstevel@tonic-gate #define DTD_ELEM_RCTL (const xmlChar *) "rctl" 857c478bd9Sstevel@tonic-gate #define DTD_ELEM_RCTLVALUE (const xmlChar *) "rctl-value" 867c478bd9Sstevel@tonic-gate #define DTD_ELEM_ZONE (const xmlChar *) "zone" 87fa9e4066Sahrens #define DTD_ELEM_DATASET (const xmlChar *) "dataset" 880209230bSgjelinek #define DTD_ELEM_TMPPOOL (const xmlChar *) "tmp_pool" 890209230bSgjelinek #define DTD_ELEM_PSET (const xmlChar *) "pset" 900209230bSgjelinek #define DTD_ELEM_MCAP (const xmlChar *) "mcap" 91ee519a1fSgjelinek #define DTD_ELEM_PACKAGE (const xmlChar *) "package" 92ee519a1fSgjelinek #define DTD_ELEM_PATCH (const xmlChar *) "patch" 936cfd72c6Sgjelinek #define DTD_ELEM_OBSOLETES (const xmlChar *) "obsoletes" 94ee519a1fSgjelinek #define DTD_ELEM_DEV_PERM (const xmlChar *) "dev-perm" 957c478bd9Sstevel@tonic-gate 967c478bd9Sstevel@tonic-gate #define DTD_ATTR_ACTION (const xmlChar *) "action" 977c478bd9Sstevel@tonic-gate #define DTD_ATTR_ADDRESS (const xmlChar *) "address" 987c478bd9Sstevel@tonic-gate #define DTD_ATTR_AUTOBOOT (const xmlChar *) "autoboot" 99f4b3ec61Sdh155122 #define DTD_ATTR_IPTYPE (const xmlChar *) "ip-type" 100de860bd9Sgfaden #define DTD_ATTR_DEFROUTER (const xmlChar *) "defrouter" 1017c478bd9Sstevel@tonic-gate #define DTD_ATTR_DIR (const xmlChar *) "directory" 1027c478bd9Sstevel@tonic-gate #define DTD_ATTR_LIMIT (const xmlChar *) "limit" 103ffbafc53Scomay #define DTD_ATTR_LIMITPRIV (const xmlChar *) "limitpriv" 1043f2f09c1Sdp #define DTD_ATTR_BOOTARGS (const xmlChar *) "bootargs" 1050209230bSgjelinek #define DTD_ATTR_SCHED (const xmlChar *) "scheduling-class" 1067c478bd9Sstevel@tonic-gate #define DTD_ATTR_MATCH (const xmlChar *) "match" 1077c478bd9Sstevel@tonic-gate #define DTD_ATTR_NAME (const xmlChar *) "name" 1087c478bd9Sstevel@tonic-gate #define DTD_ATTR_PHYSICAL (const xmlChar *) "physical" 1097c478bd9Sstevel@tonic-gate #define DTD_ATTR_POOL (const xmlChar *) "pool" 1107c478bd9Sstevel@tonic-gate #define DTD_ATTR_PRIV (const xmlChar *) "priv" 1117c478bd9Sstevel@tonic-gate #define DTD_ATTR_RAW (const xmlChar *) "raw" 1127c478bd9Sstevel@tonic-gate #define DTD_ATTR_SPECIAL (const xmlChar *) "special" 1137c478bd9Sstevel@tonic-gate #define DTD_ATTR_TYPE (const xmlChar *) "type" 1147c478bd9Sstevel@tonic-gate #define DTD_ATTR_VALUE (const xmlChar *) "value" 1157c478bd9Sstevel@tonic-gate #define DTD_ATTR_ZONEPATH (const xmlChar *) "zonepath" 1160209230bSgjelinek #define DTD_ATTR_NCPU_MIN (const xmlChar *) "ncpu_min" 1170209230bSgjelinek #define DTD_ATTR_NCPU_MAX (const xmlChar *) "ncpu_max" 1180209230bSgjelinek #define DTD_ATTR_IMPORTANCE (const xmlChar *) "importance" 1190209230bSgjelinek #define DTD_ATTR_PHYSCAP (const xmlChar *) "physcap" 120ee519a1fSgjelinek #define DTD_ATTR_VERSION (const xmlChar *) "version" 121ee519a1fSgjelinek #define DTD_ATTR_ID (const xmlChar *) "id" 122ee519a1fSgjelinek #define DTD_ATTR_UID (const xmlChar *) "uid" 123ee519a1fSgjelinek #define DTD_ATTR_GID (const xmlChar *) "gid" 124ee519a1fSgjelinek #define DTD_ATTR_MODE (const xmlChar *) "mode" 125ee519a1fSgjelinek #define DTD_ATTR_ACL (const xmlChar *) "acl" 1269acbbeafSnn35248 #define DTD_ATTR_BRAND (const xmlChar *) "brand" 1275679c89fSjv227347 #define DTD_ATTR_HOSTID (const xmlChar *) "hostid" 1287c478bd9Sstevel@tonic-gate 1297c478bd9Sstevel@tonic-gate #define DTD_ENTITY_BOOLEAN "boolean" 1307c478bd9Sstevel@tonic-gate #define DTD_ENTITY_DEVPATH "devpath" 1317c478bd9Sstevel@tonic-gate #define DTD_ENTITY_DRIVER "driver" 1327c478bd9Sstevel@tonic-gate #define DTD_ENTITY_DRVMIN "drv_min" 1337c478bd9Sstevel@tonic-gate #define DTD_ENTITY_FALSE "false" 1347c478bd9Sstevel@tonic-gate #define DTD_ENTITY_INT "int" 1357c478bd9Sstevel@tonic-gate #define DTD_ENTITY_STRING "string" 1367c478bd9Sstevel@tonic-gate #define DTD_ENTITY_TRUE "true" 1377c478bd9Sstevel@tonic-gate #define DTD_ENTITY_UINT "uint" 1387c478bd9Sstevel@tonic-gate 139a1be23daSdp #define DTD_ENTITY_BOOL_LEN 6 /* "false" */ 140a1be23daSdp 141ee519a1fSgjelinek #define ATTACH_FORCED "SUNWattached.xml" 142ee519a1fSgjelinek 1430209230bSgjelinek #define TMP_POOL_NAME "SUNWtmp_%s" 1440209230bSgjelinek #define MAX_TMP_POOL_NAME (ZONENAME_MAX + 9) 1450209230bSgjelinek #define RCAP_SERVICE "system/rcap:default" 1460209230bSgjelinek #define POOLD_SERVICE "system/pools/dynamic:default" 1470209230bSgjelinek 1480209230bSgjelinek /* 1490209230bSgjelinek * rctl alias definitions 1500209230bSgjelinek * 1510209230bSgjelinek * This holds the alias, the full rctl name, the default priv value, action 1520209230bSgjelinek * and lower limit. The functions that handle rctl aliases step through 1530209230bSgjelinek * this table, matching on the alias, and using the full values for setting 1540209230bSgjelinek * the rctl entry as well the limit for validation. 1550209230bSgjelinek */ 1560209230bSgjelinek static struct alias { 1570209230bSgjelinek char *shortname; 1580209230bSgjelinek char *realname; 1590209230bSgjelinek char *priv; 1600209230bSgjelinek char *action; 1610209230bSgjelinek uint64_t low_limit; 1620209230bSgjelinek } aliases[] = { 1630209230bSgjelinek {ALIAS_MAXLWPS, "zone.max-lwps", "privileged", "deny", 100}, 1640209230bSgjelinek {ALIAS_MAXSHMMEM, "zone.max-shm-memory", "privileged", "deny", 0}, 1650209230bSgjelinek {ALIAS_MAXSHMIDS, "zone.max-shm-ids", "privileged", "deny", 0}, 1660209230bSgjelinek {ALIAS_MAXMSGIDS, "zone.max-msg-ids", "privileged", "deny", 0}, 1670209230bSgjelinek {ALIAS_MAXSEMIDS, "zone.max-sem-ids", "privileged", "deny", 0}, 1680209230bSgjelinek {ALIAS_MAXLOCKEDMEM, "zone.max-locked-memory", "privileged", "deny", 0}, 1690209230bSgjelinek {ALIAS_MAXSWAP, "zone.max-swap", "privileged", "deny", 0}, 1700209230bSgjelinek {ALIAS_SHARES, "zone.cpu-shares", "privileged", "none", 0}, 171c97ad5cdSakolb {ALIAS_CPUCAP, "zone.cpu-cap", "privileged", "deny", 0}, 1720209230bSgjelinek {NULL, NULL, NULL, NULL, 0} 1730209230bSgjelinek }; 1740209230bSgjelinek 1750209230bSgjelinek /* 1760209230bSgjelinek * Structure for applying rctls to a running zone. It allows important 1770209230bSgjelinek * process values to be passed together easily. 1780209230bSgjelinek */ 1790209230bSgjelinek typedef struct pr_info_handle { 1800209230bSgjelinek struct ps_prochandle *pr; 1810209230bSgjelinek pid_t pid; 1820209230bSgjelinek } pr_info_handle_t; 1830209230bSgjelinek 1847c478bd9Sstevel@tonic-gate struct zone_dochandle { 1857c478bd9Sstevel@tonic-gate char *zone_dh_rootdir; 1867c478bd9Sstevel@tonic-gate xmlDocPtr zone_dh_doc; 1877c478bd9Sstevel@tonic-gate xmlNodePtr zone_dh_cur; 1887c478bd9Sstevel@tonic-gate xmlNodePtr zone_dh_top; 189087719fdSdp boolean_t zone_dh_newzone; 190087719fdSdp boolean_t zone_dh_snapshot; 191ee519a1fSgjelinek boolean_t zone_dh_sw_inv; 192087719fdSdp char zone_dh_delete_name[ZONENAME_MAX]; 1937c478bd9Sstevel@tonic-gate }; 1947c478bd9Sstevel@tonic-gate 195cf8f45c7Sdstaff struct znotify { 196cf8f45c7Sdstaff void * zn_private; 197cf8f45c7Sdstaff evchan_t *zn_eventchan; 198cf8f45c7Sdstaff int (*zn_callback)(const char *zonename, zoneid_t zid, 199cf8f45c7Sdstaff const char *newstate, const char *oldstate, hrtime_t when, void *p); 200cf8f45c7Sdstaff pthread_mutex_t zn_mutex; 201cf8f45c7Sdstaff pthread_cond_t zn_cond; 202cf8f45c7Sdstaff pthread_mutex_t zn_bigmutex; 203cf8f45c7Sdstaff volatile enum {ZN_UNLOCKED, ZN_LOCKED, ZN_PING_INFLIGHT, 204cf8f45c7Sdstaff ZN_PING_RECEIVED} zn_state; 205cf8f45c7Sdstaff char zn_subscriber_id[MAX_SUBID_LEN]; 206cf8f45c7Sdstaff volatile boolean_t zn_failed; 207cf8f45c7Sdstaff int zn_failure_count; 208cf8f45c7Sdstaff }; 209cf8f45c7Sdstaff 210ff17c8bfSgjelinek /* used to track nested zone-lock operations */ 211ff17c8bfSgjelinek static int zone_lock_cnt = 0; 212ee519a1fSgjelinek 213ff17c8bfSgjelinek /* used to communicate lock status to children */ 214ff17c8bfSgjelinek #define LOCK_ENV_VAR "_ZONEADM_LOCK_HELD" 215ff17c8bfSgjelinek static char zoneadm_lock_held[] = LOCK_ENV_VAR"=1"; 216ff17c8bfSgjelinek static char zoneadm_lock_not_held[] = LOCK_ENV_VAR"=0"; 21739935be5Sgjelinek 218108322fbScarlsonj char *zonecfg_root = ""; 219108322fbScarlsonj 2207c478bd9Sstevel@tonic-gate /* 2217c478bd9Sstevel@tonic-gate * For functions which return int, which is most of the functions herein, 2227c478bd9Sstevel@tonic-gate * the return values should be from the Z_foo set defined in <libzonecfg.h>. 2237c478bd9Sstevel@tonic-gate * In some instances, we take pains mapping some libc errno values to Z_foo 2247c478bd9Sstevel@tonic-gate * values from this set. 2257c478bd9Sstevel@tonic-gate */ 2267c478bd9Sstevel@tonic-gate 2277c478bd9Sstevel@tonic-gate /* 228108322fbScarlsonj * Set the root (/) path for all zonecfg configuration files. This is a 229108322fbScarlsonj * private interface used by Live Upgrade extensions to access zone 230108322fbScarlsonj * configuration inside mounted alternate boot environments. 231108322fbScarlsonj */ 232108322fbScarlsonj void 233108322fbScarlsonj zonecfg_set_root(const char *rootpath) 234108322fbScarlsonj { 235108322fbScarlsonj if (*zonecfg_root != '\0') 236108322fbScarlsonj free(zonecfg_root); 237108322fbScarlsonj if (rootpath == NULL || rootpath[0] == '\0' || rootpath[1] == '\0' || 238108322fbScarlsonj (zonecfg_root = strdup(rootpath)) == NULL) 239108322fbScarlsonj zonecfg_root = ""; 240108322fbScarlsonj } 241108322fbScarlsonj 242108322fbScarlsonj const char * 243108322fbScarlsonj zonecfg_get_root(void) 244108322fbScarlsonj { 245108322fbScarlsonj return (zonecfg_root); 246108322fbScarlsonj } 247108322fbScarlsonj 248108322fbScarlsonj boolean_t 249108322fbScarlsonj zonecfg_in_alt_root(void) 250108322fbScarlsonj { 251108322fbScarlsonj return (*zonecfg_root != '\0'); 252108322fbScarlsonj } 253108322fbScarlsonj 254108322fbScarlsonj /* 2557c478bd9Sstevel@tonic-gate * Callers of the _file_path() functions are expected to have the second 2567c478bd9Sstevel@tonic-gate * parameter be a (char foo[MAXPATHLEN]). 2577c478bd9Sstevel@tonic-gate */ 2587c478bd9Sstevel@tonic-gate 259108322fbScarlsonj static boolean_t 2607c478bd9Sstevel@tonic-gate config_file_path(const char *zonename, char *answer) 2617c478bd9Sstevel@tonic-gate { 262108322fbScarlsonj return (snprintf(answer, MAXPATHLEN, "%s%s/%s.xml", zonecfg_root, 263108322fbScarlsonj ZONE_CONFIG_ROOT, zonename) < MAXPATHLEN); 2647c478bd9Sstevel@tonic-gate } 2657c478bd9Sstevel@tonic-gate 266108322fbScarlsonj static boolean_t 267108322fbScarlsonj snap_file_path(const char *zonename, char *answer) 2687c478bd9Sstevel@tonic-gate { 269108322fbScarlsonj return (snprintf(answer, MAXPATHLEN, "%s%s/%s.snapshot.xml", 270108322fbScarlsonj zonecfg_root, ZONE_SNAPSHOT_ROOT, zonename) < MAXPATHLEN); 2717c478bd9Sstevel@tonic-gate } 2727c478bd9Sstevel@tonic-gate 2737c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 2747c478bd9Sstevel@tonic-gate static void 2757c478bd9Sstevel@tonic-gate zonecfg_error_func(void *ctx, const char *msg, ...) 2767c478bd9Sstevel@tonic-gate { 2777c478bd9Sstevel@tonic-gate /* 2787c478bd9Sstevel@tonic-gate * This function does nothing by design. Its purpose is to prevent 2797c478bd9Sstevel@tonic-gate * libxml from dumping unwanted messages to stdout/stderr. 2807c478bd9Sstevel@tonic-gate */ 2817c478bd9Sstevel@tonic-gate } 2827c478bd9Sstevel@tonic-gate 2837c478bd9Sstevel@tonic-gate zone_dochandle_t 2847c478bd9Sstevel@tonic-gate zonecfg_init_handle(void) 2857c478bd9Sstevel@tonic-gate { 286087719fdSdp zone_dochandle_t handle = calloc(1, sizeof (struct zone_dochandle)); 2877c478bd9Sstevel@tonic-gate if (handle == NULL) { 2887c478bd9Sstevel@tonic-gate errno = Z_NOMEM; 2897c478bd9Sstevel@tonic-gate return (NULL); 2907c478bd9Sstevel@tonic-gate } 2917c478bd9Sstevel@tonic-gate 2927c478bd9Sstevel@tonic-gate /* generic libxml initialization */ 293ced33dd6Sjv227347 (void) xmlLineNumbersDefault(1); 2947c478bd9Sstevel@tonic-gate xmlLoadExtDtdDefaultValue |= XML_DETECT_IDS; 2957c478bd9Sstevel@tonic-gate xmlDoValidityCheckingDefaultValue = 1; 2967c478bd9Sstevel@tonic-gate (void) xmlKeepBlanksDefault(0); 2977c478bd9Sstevel@tonic-gate xmlGetWarningsDefaultValue = 0; 2987c478bd9Sstevel@tonic-gate xmlSetGenericErrorFunc(NULL, zonecfg_error_func); 2997c478bd9Sstevel@tonic-gate 3007c478bd9Sstevel@tonic-gate return (handle); 3017c478bd9Sstevel@tonic-gate } 3027c478bd9Sstevel@tonic-gate 3037c478bd9Sstevel@tonic-gate int 3047c478bd9Sstevel@tonic-gate zonecfg_check_handle(zone_dochandle_t handle) 3057c478bd9Sstevel@tonic-gate { 3067c478bd9Sstevel@tonic-gate if (handle == NULL || handle->zone_dh_doc == NULL) 3077c478bd9Sstevel@tonic-gate return (Z_BAD_HANDLE); 3087c478bd9Sstevel@tonic-gate return (Z_OK); 3097c478bd9Sstevel@tonic-gate } 3107c478bd9Sstevel@tonic-gate 3117c478bd9Sstevel@tonic-gate void 3127c478bd9Sstevel@tonic-gate zonecfg_fini_handle(zone_dochandle_t handle) 3137c478bd9Sstevel@tonic-gate { 3147c478bd9Sstevel@tonic-gate if (zonecfg_check_handle(handle) == Z_OK) 3157c478bd9Sstevel@tonic-gate xmlFreeDoc(handle->zone_dh_doc); 3167c478bd9Sstevel@tonic-gate if (handle != NULL) 3177c478bd9Sstevel@tonic-gate free(handle); 3187c478bd9Sstevel@tonic-gate } 3197c478bd9Sstevel@tonic-gate 3207c478bd9Sstevel@tonic-gate static int 3217c478bd9Sstevel@tonic-gate zonecfg_destroy_impl(char *filename) 3227c478bd9Sstevel@tonic-gate { 3237c478bd9Sstevel@tonic-gate if (unlink(filename) == -1) { 3247c478bd9Sstevel@tonic-gate if (errno == EACCES) 3257c478bd9Sstevel@tonic-gate return (Z_ACCES); 3267c478bd9Sstevel@tonic-gate if (errno == ENOENT) 3277c478bd9Sstevel@tonic-gate return (Z_NO_ZONE); 3287c478bd9Sstevel@tonic-gate return (Z_MISC_FS); 3297c478bd9Sstevel@tonic-gate } 3307c478bd9Sstevel@tonic-gate return (Z_OK); 3317c478bd9Sstevel@tonic-gate } 3327c478bd9Sstevel@tonic-gate 3337c478bd9Sstevel@tonic-gate int 334087719fdSdp zonecfg_destroy(const char *zonename, boolean_t force) 3357c478bd9Sstevel@tonic-gate { 3367c478bd9Sstevel@tonic-gate char path[MAXPATHLEN]; 337087719fdSdp struct zoneent ze; 338087719fdSdp int err, state_err; 339087719fdSdp zone_state_t state; 3407c478bd9Sstevel@tonic-gate 341108322fbScarlsonj if (!config_file_path(zonename, path)) 342108322fbScarlsonj return (Z_MISC_FS); 343087719fdSdp 344087719fdSdp state_err = zone_get_state((char *)zonename, &state); 345087719fdSdp err = access(path, W_OK); 346087719fdSdp 347087719fdSdp /* 348087719fdSdp * If there is no file, and no index entry, reliably indicate that no 349087719fdSdp * such zone exists. 350087719fdSdp */ 351087719fdSdp if ((state_err == Z_NO_ZONE) && (err == -1) && (errno == ENOENT)) 352087719fdSdp return (Z_NO_ZONE); 353087719fdSdp 354087719fdSdp /* 355087719fdSdp * Handle any other filesystem related errors (except if the XML 356087719fdSdp * file is missing, which we treat silently), unless we're forcing, 357087719fdSdp * in which case we plow on. 358087719fdSdp */ 359087719fdSdp if (err == -1 && errno != ENOENT) { 360087719fdSdp if (errno == EACCES) 361087719fdSdp return (Z_ACCES); 362087719fdSdp else if (!force) 363087719fdSdp return (Z_MISC_FS); 364087719fdSdp } 365087719fdSdp 366087719fdSdp if (state > ZONE_STATE_INSTALLED) 367087719fdSdp return (Z_BAD_ZONE_STATE); 368087719fdSdp 369087719fdSdp if (!force && state > ZONE_STATE_CONFIGURED) 370087719fdSdp return (Z_BAD_ZONE_STATE); 371087719fdSdp 372087719fdSdp /* 373087719fdSdp * Index deletion succeeds even if the entry doesn't exist. So this 374087719fdSdp * will fail only if we've had some more severe problem. 375087719fdSdp */ 376087719fdSdp bzero(&ze, sizeof (ze)); 377087719fdSdp (void) strlcpy(ze.zone_name, zonename, sizeof (ze.zone_name)); 378087719fdSdp if ((err = putzoneent(&ze, PZE_REMOVE)) != Z_OK) 379087719fdSdp if (!force) 380087719fdSdp return (err); 381087719fdSdp 382087719fdSdp err = zonecfg_destroy_impl(path); 383087719fdSdp 384087719fdSdp /* 385087719fdSdp * Treat failure to find the XML file silently, since, well, it's 386087719fdSdp * gone, and with the index file cleaned up, we're done. 387087719fdSdp */ 388087719fdSdp if (err == Z_OK || err == Z_NO_ZONE) 389087719fdSdp return (Z_OK); 390087719fdSdp return (err); 3917c478bd9Sstevel@tonic-gate } 3927c478bd9Sstevel@tonic-gate 3937c478bd9Sstevel@tonic-gate int 394108322fbScarlsonj zonecfg_destroy_snapshot(const char *zonename) 3957c478bd9Sstevel@tonic-gate { 3967c478bd9Sstevel@tonic-gate char path[MAXPATHLEN]; 3977c478bd9Sstevel@tonic-gate 398108322fbScarlsonj if (!snap_file_path(zonename, path)) 399108322fbScarlsonj return (Z_MISC_FS); 4007c478bd9Sstevel@tonic-gate return (zonecfg_destroy_impl(path)); 4017c478bd9Sstevel@tonic-gate } 4027c478bd9Sstevel@tonic-gate 4037c478bd9Sstevel@tonic-gate static int 404a1be23daSdp getroot(zone_dochandle_t handle, xmlNodePtr *root) 4057c478bd9Sstevel@tonic-gate { 4067c478bd9Sstevel@tonic-gate if (zonecfg_check_handle(handle) == Z_BAD_HANDLE) 4077c478bd9Sstevel@tonic-gate return (Z_BAD_HANDLE); 4087c478bd9Sstevel@tonic-gate 409a1be23daSdp *root = xmlDocGetRootElement(handle->zone_dh_doc); 410a1be23daSdp 411a1be23daSdp if (*root == NULL) 4127c478bd9Sstevel@tonic-gate return (Z_EMPTY_DOCUMENT); 413a1be23daSdp 414a1be23daSdp if (xmlStrcmp((*root)->name, DTD_ELEM_ZONE)) 415a1be23daSdp return (Z_WRONG_DOC_TYPE); 416a1be23daSdp 417a1be23daSdp return (Z_OK); 4187c478bd9Sstevel@tonic-gate } 4197c478bd9Sstevel@tonic-gate 420a1be23daSdp static int 421a1be23daSdp operation_prep(zone_dochandle_t handle) 422a1be23daSdp { 423a1be23daSdp xmlNodePtr root; 424a1be23daSdp int err; 425a1be23daSdp 426a1be23daSdp if ((err = getroot(handle, &root)) != 0) 427a1be23daSdp return (err); 428a1be23daSdp 429a1be23daSdp handle->zone_dh_cur = root; 430a1be23daSdp handle->zone_dh_top = root; 431a1be23daSdp return (Z_OK); 4327c478bd9Sstevel@tonic-gate } 433a1be23daSdp 434a1be23daSdp static int 435ffbafc53Scomay fetchprop(xmlNodePtr cur, const xmlChar *propname, char *dst, size_t dstsize) 436ffbafc53Scomay { 437ffbafc53Scomay xmlChar *property; 438ffbafc53Scomay size_t srcsize; 439ffbafc53Scomay 440ffbafc53Scomay if ((property = xmlGetProp(cur, propname)) == NULL) 441ffbafc53Scomay return (Z_BAD_PROPERTY); 442ffbafc53Scomay srcsize = strlcpy(dst, (char *)property, dstsize); 443ffbafc53Scomay xmlFree(property); 444ffbafc53Scomay if (srcsize >= dstsize) 445ffbafc53Scomay return (Z_TOO_BIG); 446ffbafc53Scomay return (Z_OK); 447ffbafc53Scomay } 448ffbafc53Scomay 449ffbafc53Scomay static int 450ffbafc53Scomay fetch_alloc_prop(xmlNodePtr cur, const xmlChar *propname, char **dst) 451ffbafc53Scomay { 452ffbafc53Scomay xmlChar *property; 453ffbafc53Scomay 454ffbafc53Scomay if ((property = xmlGetProp(cur, propname)) == NULL) 455ffbafc53Scomay return (Z_BAD_PROPERTY); 456ffbafc53Scomay if ((*dst = strdup((char *)property)) == NULL) { 457ffbafc53Scomay xmlFree(property); 458ffbafc53Scomay return (Z_NOMEM); 459ffbafc53Scomay } 460ffbafc53Scomay xmlFree(property); 461ffbafc53Scomay return (Z_OK); 462ffbafc53Scomay } 463ffbafc53Scomay 464ffbafc53Scomay static int 465a1be23daSdp getrootattr(zone_dochandle_t handle, const xmlChar *propname, 466a1be23daSdp char *propval, size_t propsize) 467a1be23daSdp { 468a1be23daSdp xmlNodePtr root; 469a1be23daSdp int err; 470a1be23daSdp 471a1be23daSdp if ((err = getroot(handle, &root)) != 0) 472a1be23daSdp return (err); 473a1be23daSdp 474ffbafc53Scomay return (fetchprop(root, propname, propval, propsize)); 475ffbafc53Scomay } 476ffbafc53Scomay 477ffbafc53Scomay static int 478ffbafc53Scomay get_alloc_rootattr(zone_dochandle_t handle, const xmlChar *propname, 479ffbafc53Scomay char **propval) 480ffbafc53Scomay { 481ffbafc53Scomay xmlNodePtr root; 482ffbafc53Scomay int err; 483ffbafc53Scomay 484ffbafc53Scomay if ((err = getroot(handle, &root)) != 0) 485ffbafc53Scomay return (err); 486ffbafc53Scomay 487ffbafc53Scomay return (fetch_alloc_prop(root, propname, propval)); 488a1be23daSdp } 489a1be23daSdp 490a1be23daSdp static int 491108322fbScarlsonj setrootattr(zone_dochandle_t handle, const xmlChar *propname, 492108322fbScarlsonj const char *propval) 493a1be23daSdp { 494a1be23daSdp int err; 495a1be23daSdp xmlNodePtr root; 496a1be23daSdp 497a1be23daSdp if ((err = getroot(handle, &root)) != Z_OK) 498a1be23daSdp return (err); 499a1be23daSdp 5000209230bSgjelinek /* 5010209230bSgjelinek * If we get a null propval remove the property (ignore return since it 5020209230bSgjelinek * may not be set to begin with). 5030209230bSgjelinek */ 5040209230bSgjelinek if (propval == NULL) { 5050209230bSgjelinek (void) xmlUnsetProp(root, propname); 5060209230bSgjelinek } else { 5070209230bSgjelinek if (xmlSetProp(root, propname, (const xmlChar *) propval) 5080209230bSgjelinek == NULL) 509a1be23daSdp return (Z_INVAL); 5100209230bSgjelinek } 5117c478bd9Sstevel@tonic-gate return (Z_OK); 5127c478bd9Sstevel@tonic-gate } 5137c478bd9Sstevel@tonic-gate 514087719fdSdp static void 515087719fdSdp addcomment(zone_dochandle_t handle, const char *comment) 516087719fdSdp { 517087719fdSdp xmlNodePtr node; 518087719fdSdp node = xmlNewComment((xmlChar *) comment); 519087719fdSdp 520087719fdSdp if (node != NULL) 521087719fdSdp (void) xmlAddPrevSibling(handle->zone_dh_top, node); 522087719fdSdp } 523087719fdSdp 524087719fdSdp static void 525087719fdSdp stripcomments(zone_dochandle_t handle) 526087719fdSdp { 527087719fdSdp xmlDocPtr top; 528087719fdSdp xmlNodePtr child, next; 529087719fdSdp 530087719fdSdp top = handle->zone_dh_doc; 531087719fdSdp for (child = top->xmlChildrenNode; child != NULL; child = next) { 532087719fdSdp next = child->next; 533087719fdSdp if (child->name == NULL) 534087719fdSdp continue; 535087719fdSdp if (xmlStrcmp(child->name, DTD_ELEM_COMMENT) == 0) { 536087719fdSdp next = child->next; 537087719fdSdp xmlUnlinkNode(child); 538087719fdSdp xmlFreeNode(child); 539087719fdSdp } 540087719fdSdp } 541087719fdSdp } 542087719fdSdp 543ee519a1fSgjelinek static void 544ee519a1fSgjelinek strip_sw_inv(zone_dochandle_t handle) 545ee519a1fSgjelinek { 546ee519a1fSgjelinek xmlNodePtr root, child, next; 547ee519a1fSgjelinek 548ee519a1fSgjelinek root = xmlDocGetRootElement(handle->zone_dh_doc); 549ee519a1fSgjelinek for (child = root->xmlChildrenNode; child != NULL; child = next) { 550ee519a1fSgjelinek next = child->next; 551ee519a1fSgjelinek if (child->name == NULL) 552ee519a1fSgjelinek continue; 553ee519a1fSgjelinek if (xmlStrcmp(child->name, DTD_ELEM_PACKAGE) == 0 || 554ee519a1fSgjelinek xmlStrcmp(child->name, DTD_ELEM_PATCH) == 0) { 555ee519a1fSgjelinek next = child->next; 556ee519a1fSgjelinek xmlUnlinkNode(child); 557ee519a1fSgjelinek xmlFreeNode(child); 558ee519a1fSgjelinek } 559ee519a1fSgjelinek } 560ee519a1fSgjelinek } 561ee519a1fSgjelinek 5627c478bd9Sstevel@tonic-gate static int 563108322fbScarlsonj zonecfg_get_handle_impl(const char *zonename, const char *filename, 564108322fbScarlsonj zone_dochandle_t handle) 5657c478bd9Sstevel@tonic-gate { 5667c478bd9Sstevel@tonic-gate xmlValidCtxtPtr cvp; 5677c478bd9Sstevel@tonic-gate struct stat statbuf; 5687c478bd9Sstevel@tonic-gate int valid; 5697c478bd9Sstevel@tonic-gate 5707c478bd9Sstevel@tonic-gate if (zonename == NULL) 5717c478bd9Sstevel@tonic-gate return (Z_NO_ZONE); 5729acbbeafSnn35248 5737c478bd9Sstevel@tonic-gate if ((handle->zone_dh_doc = xmlParseFile(filename)) == NULL) { 5747c478bd9Sstevel@tonic-gate /* distinguish file not found vs. found but not parsed */ 5757c478bd9Sstevel@tonic-gate if (stat(filename, &statbuf) == 0) 5767c478bd9Sstevel@tonic-gate return (Z_INVALID_DOCUMENT); 5777c478bd9Sstevel@tonic-gate return (Z_NO_ZONE); 5787c478bd9Sstevel@tonic-gate } 5797c478bd9Sstevel@tonic-gate if ((cvp = xmlNewValidCtxt()) == NULL) 5807c478bd9Sstevel@tonic-gate return (Z_NOMEM); 5817c478bd9Sstevel@tonic-gate cvp->error = zonecfg_error_func; 5827c478bd9Sstevel@tonic-gate cvp->warning = zonecfg_error_func; 5837c478bd9Sstevel@tonic-gate valid = xmlValidateDocument(cvp, handle->zone_dh_doc); 5847c478bd9Sstevel@tonic-gate xmlFreeValidCtxt(cvp); 5857c478bd9Sstevel@tonic-gate if (valid == 0) 5867c478bd9Sstevel@tonic-gate return (Z_INVALID_DOCUMENT); 587087719fdSdp 5887c478bd9Sstevel@tonic-gate /* delete any comments such as inherited Sun copyright / ident str */ 589087719fdSdp stripcomments(handle); 5907c478bd9Sstevel@tonic-gate return (Z_OK); 5917c478bd9Sstevel@tonic-gate } 5927c478bd9Sstevel@tonic-gate 5937c478bd9Sstevel@tonic-gate int 594108322fbScarlsonj zonecfg_get_handle(const char *zonename, zone_dochandle_t handle) 5957c478bd9Sstevel@tonic-gate { 5967c478bd9Sstevel@tonic-gate char path[MAXPATHLEN]; 5977c478bd9Sstevel@tonic-gate 598108322fbScarlsonj if (!config_file_path(zonename, path)) 599108322fbScarlsonj return (Z_MISC_FS); 600087719fdSdp handle->zone_dh_newzone = B_FALSE; 601087719fdSdp 6027c478bd9Sstevel@tonic-gate return (zonecfg_get_handle_impl(zonename, path, handle)); 6037c478bd9Sstevel@tonic-gate } 6047c478bd9Sstevel@tonic-gate 6057c478bd9Sstevel@tonic-gate int 60616ab8c7bSgjelinek zonecfg_get_attach_handle(const char *path, const char *fname, 60716ab8c7bSgjelinek const char *zonename, boolean_t preserve_sw, zone_dochandle_t handle) 608ee519a1fSgjelinek { 609ee519a1fSgjelinek char migpath[MAXPATHLEN]; 610ee519a1fSgjelinek int err; 611ee519a1fSgjelinek struct stat buf; 612ee519a1fSgjelinek 613ee519a1fSgjelinek if (snprintf(migpath, sizeof (migpath), "%s/root", path) >= 614ee519a1fSgjelinek sizeof (migpath)) 615ee519a1fSgjelinek return (Z_NOMEM); 616ee519a1fSgjelinek 617ee519a1fSgjelinek if (stat(migpath, &buf) == -1 || !S_ISDIR(buf.st_mode)) 618ee519a1fSgjelinek return (Z_NO_ZONE); 619ee519a1fSgjelinek 62016ab8c7bSgjelinek if (snprintf(migpath, sizeof (migpath), "%s/%s", path, fname) >= 621ee519a1fSgjelinek sizeof (migpath)) 622ee519a1fSgjelinek return (Z_NOMEM); 623ee519a1fSgjelinek 624ee519a1fSgjelinek if ((err = zonecfg_get_handle_impl(zonename, migpath, handle)) != Z_OK) 625ee519a1fSgjelinek return (err); 626ee519a1fSgjelinek 627ee519a1fSgjelinek if (!preserve_sw) 628ee519a1fSgjelinek strip_sw_inv(handle); 629ee519a1fSgjelinek 630ee519a1fSgjelinek handle->zone_dh_newzone = B_TRUE; 631ee519a1fSgjelinek if ((err = setrootattr(handle, DTD_ATTR_ZONEPATH, path)) != Z_OK) 632ee519a1fSgjelinek return (err); 633ee519a1fSgjelinek 634ee519a1fSgjelinek return (setrootattr(handle, DTD_ATTR_NAME, zonename)); 635ee519a1fSgjelinek } 636ee519a1fSgjelinek 637ee519a1fSgjelinek int 638108322fbScarlsonj zonecfg_get_snapshot_handle(const char *zonename, zone_dochandle_t handle) 6397c478bd9Sstevel@tonic-gate { 6407c478bd9Sstevel@tonic-gate char path[MAXPATHLEN]; 6417c478bd9Sstevel@tonic-gate 642108322fbScarlsonj if (!snap_file_path(zonename, path)) 643108322fbScarlsonj return (Z_MISC_FS); 644087719fdSdp handle->zone_dh_newzone = B_FALSE; 6457c478bd9Sstevel@tonic-gate return (zonecfg_get_handle_impl(zonename, path, handle)); 6467c478bd9Sstevel@tonic-gate } 6477c478bd9Sstevel@tonic-gate 6487c478bd9Sstevel@tonic-gate int 649108322fbScarlsonj zonecfg_get_template_handle(const char *template, const char *zonename, 650087719fdSdp zone_dochandle_t handle) 651087719fdSdp { 652087719fdSdp char path[MAXPATHLEN]; 653087719fdSdp int err; 654087719fdSdp 655108322fbScarlsonj if (!config_file_path(template, path)) 656108322fbScarlsonj return (Z_MISC_FS); 657087719fdSdp 658087719fdSdp if ((err = zonecfg_get_handle_impl(template, path, handle)) != Z_OK) 659087719fdSdp return (err); 660087719fdSdp handle->zone_dh_newzone = B_TRUE; 661087719fdSdp return (setrootattr(handle, DTD_ATTR_NAME, zonename)); 662087719fdSdp } 663087719fdSdp 6649acbbeafSnn35248 int 6659acbbeafSnn35248 zonecfg_get_xml_handle(const char *path, zone_dochandle_t handle) 6669acbbeafSnn35248 { 6679acbbeafSnn35248 struct stat buf; 6689acbbeafSnn35248 int err; 6699acbbeafSnn35248 6709acbbeafSnn35248 if (stat(path, &buf) == -1) 6719acbbeafSnn35248 return (Z_MISC_FS); 6729acbbeafSnn35248 6739acbbeafSnn35248 if ((err = zonecfg_get_handle_impl("xml", path, handle)) != Z_OK) 6749acbbeafSnn35248 return (err); 6759acbbeafSnn35248 handle->zone_dh_newzone = B_TRUE; 6769acbbeafSnn35248 return (Z_OK); 6779acbbeafSnn35248 } 6789acbbeafSnn35248 6798cd327d5Sgjelinek /* 6808cd327d5Sgjelinek * Initialize two handles from the manifest read on fd. The rem_handle 6818cd327d5Sgjelinek * is initialized from the input file, including the sw inventory. The 6828cd327d5Sgjelinek * local_handle is initialized with the same zone configuration but with 6838cd327d5Sgjelinek * no sw inventory. 6848cd327d5Sgjelinek */ 6858cd327d5Sgjelinek int 6868cd327d5Sgjelinek zonecfg_attach_manifest(int fd, zone_dochandle_t local_handle, 6878cd327d5Sgjelinek zone_dochandle_t rem_handle) 6888cd327d5Sgjelinek { 6898cd327d5Sgjelinek xmlValidCtxtPtr cvp; 6908cd327d5Sgjelinek int valid; 6918cd327d5Sgjelinek 6928cd327d5Sgjelinek /* load the manifest into the handle for the remote system */ 6938cd327d5Sgjelinek if ((rem_handle->zone_dh_doc = xmlReadFd(fd, NULL, NULL, 0)) == NULL) { 6948cd327d5Sgjelinek return (Z_INVALID_DOCUMENT); 6958cd327d5Sgjelinek } 6968cd327d5Sgjelinek if ((cvp = xmlNewValidCtxt()) == NULL) 6978cd327d5Sgjelinek return (Z_NOMEM); 6988cd327d5Sgjelinek cvp->error = zonecfg_error_func; 6998cd327d5Sgjelinek cvp->warning = zonecfg_error_func; 7008cd327d5Sgjelinek valid = xmlValidateDocument(cvp, rem_handle->zone_dh_doc); 7018cd327d5Sgjelinek xmlFreeValidCtxt(cvp); 7028cd327d5Sgjelinek if (valid == 0) 7038cd327d5Sgjelinek return (Z_INVALID_DOCUMENT); 7048cd327d5Sgjelinek 7058cd327d5Sgjelinek /* delete any comments such as inherited Sun copyright / ident str */ 7068cd327d5Sgjelinek stripcomments(rem_handle); 7078cd327d5Sgjelinek 7088cd327d5Sgjelinek rem_handle->zone_dh_newzone = B_TRUE; 7098cd327d5Sgjelinek rem_handle->zone_dh_sw_inv = B_TRUE; 7108cd327d5Sgjelinek 7118cd327d5Sgjelinek /* 7128cd327d5Sgjelinek * Now use the remote system handle to generate a local system handle 7138cd327d5Sgjelinek * with an identical zones configuration but no sw inventory. 7148cd327d5Sgjelinek */ 7158cd327d5Sgjelinek if ((local_handle->zone_dh_doc = xmlCopyDoc(rem_handle->zone_dh_doc, 7168cd327d5Sgjelinek 1)) == NULL) { 7178cd327d5Sgjelinek return (Z_INVALID_DOCUMENT); 7188cd327d5Sgjelinek } 7198cd327d5Sgjelinek 7208cd327d5Sgjelinek /* 7218cd327d5Sgjelinek * We need to re-run xmlValidateDocument on local_handle to properly 7228cd327d5Sgjelinek * update the in-core representation of the configuration. 7238cd327d5Sgjelinek */ 7248cd327d5Sgjelinek if ((cvp = xmlNewValidCtxt()) == NULL) 7258cd327d5Sgjelinek return (Z_NOMEM); 7268cd327d5Sgjelinek cvp->error = zonecfg_error_func; 7278cd327d5Sgjelinek cvp->warning = zonecfg_error_func; 7288cd327d5Sgjelinek valid = xmlValidateDocument(cvp, local_handle->zone_dh_doc); 7298cd327d5Sgjelinek xmlFreeValidCtxt(cvp); 7308cd327d5Sgjelinek if (valid == 0) 7318cd327d5Sgjelinek return (Z_INVALID_DOCUMENT); 7328cd327d5Sgjelinek 7338cd327d5Sgjelinek strip_sw_inv(local_handle); 7348cd327d5Sgjelinek 7358cd327d5Sgjelinek local_handle->zone_dh_newzone = B_TRUE; 7368cd327d5Sgjelinek local_handle->zone_dh_sw_inv = B_FALSE; 7378cd327d5Sgjelinek 7388cd327d5Sgjelinek return (Z_OK); 7398cd327d5Sgjelinek } 7408cd327d5Sgjelinek 741087719fdSdp static boolean_t 742087719fdSdp is_renaming(zone_dochandle_t handle) 743087719fdSdp { 744087719fdSdp if (handle->zone_dh_newzone) 745087719fdSdp return (B_FALSE); 746087719fdSdp if (strlen(handle->zone_dh_delete_name) > 0) 747087719fdSdp return (B_TRUE); 748087719fdSdp return (B_FALSE); 749087719fdSdp } 750087719fdSdp 751087719fdSdp static boolean_t 752087719fdSdp is_new(zone_dochandle_t handle) 753087719fdSdp { 754087719fdSdp return (handle->zone_dh_newzone || handle->zone_dh_snapshot); 755087719fdSdp } 756087719fdSdp 757087719fdSdp static boolean_t 758087719fdSdp is_snapshot(zone_dochandle_t handle) 759087719fdSdp { 760087719fdSdp return (handle->zone_dh_snapshot); 761087719fdSdp } 762087719fdSdp 763087719fdSdp /* 764087719fdSdp * It would be great to be able to use libc's ctype(3c) macros, but we 765087719fdSdp * can't, as they are locale sensitive, and it would break our limited thread 766087719fdSdp * safety if this routine had to change the app locale on the fly. 767087719fdSdp */ 768087719fdSdp int 769108322fbScarlsonj zonecfg_validate_zonename(const char *zone) 770087719fdSdp { 771087719fdSdp int i; 772087719fdSdp 773087719fdSdp if (strcmp(zone, GLOBAL_ZONENAME) == 0) 774087719fdSdp return (Z_BOGUS_ZONE_NAME); 775087719fdSdp 776087719fdSdp if (strlen(zone) >= ZONENAME_MAX) 777087719fdSdp return (Z_BOGUS_ZONE_NAME); 778087719fdSdp 779087719fdSdp if (!((zone[0] >= 'a' && zone[0] <= 'z') || 780087719fdSdp (zone[0] >= 'A' && zone[0] <= 'Z') || 781087719fdSdp (zone[0] >= '0' && zone[0] <= '9'))) 782087719fdSdp return (Z_BOGUS_ZONE_NAME); 783087719fdSdp 784087719fdSdp for (i = 1; zone[i] != '\0'; i++) { 785087719fdSdp if (!((zone[i] >= 'a' && zone[i] <= 'z') || 786087719fdSdp (zone[i] >= 'A' && zone[i] <= 'Z') || 787087719fdSdp (zone[i] >= '0' && zone[i] <= '9') || 788087719fdSdp (zone[i] == '-') || (zone[i] == '_') || (zone[i] == '.'))) 789087719fdSdp return (Z_BOGUS_ZONE_NAME); 790087719fdSdp } 791087719fdSdp 792087719fdSdp return (Z_OK); 793087719fdSdp } 794087719fdSdp 795087719fdSdp /* 796087719fdSdp * Changing the zone name requires us to track both the old and new 797087719fdSdp * name of the zone until commit time. 798087719fdSdp */ 799087719fdSdp int 8007c478bd9Sstevel@tonic-gate zonecfg_get_name(zone_dochandle_t handle, char *name, size_t namesize) 8017c478bd9Sstevel@tonic-gate { 802a1be23daSdp return (getrootattr(handle, DTD_ATTR_NAME, name, namesize)); 8037c478bd9Sstevel@tonic-gate } 8047c478bd9Sstevel@tonic-gate 805a1be23daSdp int 806a1be23daSdp zonecfg_set_name(zone_dochandle_t handle, char *name) 807a1be23daSdp { 808087719fdSdp zone_state_t state; 809087719fdSdp char curname[ZONENAME_MAX], old_delname[ZONENAME_MAX]; 810087719fdSdp int err; 811087719fdSdp 812087719fdSdp if ((err = getrootattr(handle, DTD_ATTR_NAME, curname, 813087719fdSdp sizeof (curname))) != Z_OK) 814087719fdSdp return (err); 815087719fdSdp 816087719fdSdp if (strcmp(name, curname) == 0) 817087719fdSdp return (Z_OK); 818087719fdSdp 819087719fdSdp /* 820087719fdSdp * Switching zone names to one beginning with SUNW is not permitted. 821087719fdSdp */ 822087719fdSdp if (strncmp(name, "SUNW", 4) == 0) 823087719fdSdp return (Z_BOGUS_ZONE_NAME); 824087719fdSdp 825087719fdSdp if ((err = zonecfg_validate_zonename(name)) != Z_OK) 826087719fdSdp return (err); 827087719fdSdp 828087719fdSdp /* 829087719fdSdp * Setting the name back to the original name (effectively a revert of 830087719fdSdp * the name) is fine. But if we carry on, we'll falsely identify the 831087719fdSdp * name as "in use," so special case here. 832087719fdSdp */ 833087719fdSdp if (strcmp(name, handle->zone_dh_delete_name) == 0) { 834087719fdSdp err = setrootattr(handle, DTD_ATTR_NAME, name); 835087719fdSdp handle->zone_dh_delete_name[0] = '\0'; 836087719fdSdp return (err); 837087719fdSdp } 838087719fdSdp 839087719fdSdp /* Check to see if new name chosen is already in use */ 840087719fdSdp if (zone_get_state(name, &state) != Z_NO_ZONE) 841087719fdSdp return (Z_NAME_IN_USE); 842087719fdSdp 843087719fdSdp /* 844087719fdSdp * If this isn't already "new" or in a renaming transition, then 845087719fdSdp * we're initiating a rename here; so stash the "delete name" 846087719fdSdp * (i.e. the name of the zone we'll be removing) for the rename. 847087719fdSdp */ 848087719fdSdp (void) strlcpy(old_delname, handle->zone_dh_delete_name, 849087719fdSdp sizeof (old_delname)); 850087719fdSdp if (!is_new(handle) && !is_renaming(handle)) { 851087719fdSdp /* 852087719fdSdp * Name change is allowed only when the zone we're altering 853087719fdSdp * is not ready or running. 854087719fdSdp */ 855087719fdSdp err = zone_get_state(curname, &state); 856087719fdSdp if (err == Z_OK) { 857087719fdSdp if (state > ZONE_STATE_INSTALLED) 858087719fdSdp return (Z_BAD_ZONE_STATE); 859087719fdSdp } else if (err != Z_NO_ZONE) { 860087719fdSdp return (err); 861087719fdSdp } 862087719fdSdp 863087719fdSdp (void) strlcpy(handle->zone_dh_delete_name, curname, 864087719fdSdp sizeof (handle->zone_dh_delete_name)); 865087719fdSdp assert(is_renaming(handle)); 866087719fdSdp } else if (is_renaming(handle)) { 867087719fdSdp err = zone_get_state(handle->zone_dh_delete_name, &state); 868087719fdSdp if (err == Z_OK) { 869087719fdSdp if (state > ZONE_STATE_INSTALLED) 870087719fdSdp return (Z_BAD_ZONE_STATE); 871087719fdSdp } else if (err != Z_NO_ZONE) { 872087719fdSdp return (err); 873087719fdSdp } 874087719fdSdp } 875087719fdSdp 876087719fdSdp if ((err = setrootattr(handle, DTD_ATTR_NAME, name)) != Z_OK) { 877087719fdSdp /* 878087719fdSdp * Restore the deletename to whatever it was at the 879087719fdSdp * top of the routine, since we've had a failure. 880087719fdSdp */ 881087719fdSdp (void) strlcpy(handle->zone_dh_delete_name, old_delname, 882087719fdSdp sizeof (handle->zone_dh_delete_name)); 883087719fdSdp return (err); 884087719fdSdp } 885087719fdSdp 886087719fdSdp return (Z_OK); 8877c478bd9Sstevel@tonic-gate } 888a1be23daSdp 889a1be23daSdp int 890a1be23daSdp zonecfg_get_zonepath(zone_dochandle_t handle, char *path, size_t pathsize) 891a1be23daSdp { 892108322fbScarlsonj size_t len; 893108322fbScarlsonj 894108322fbScarlsonj if ((len = strlcpy(path, zonecfg_root, pathsize)) >= pathsize) 895108322fbScarlsonj return (Z_TOO_BIG); 896108322fbScarlsonj return (getrootattr(handle, DTD_ATTR_ZONEPATH, path + len, 897108322fbScarlsonj pathsize - len)); 898a1be23daSdp } 899a1be23daSdp 900a1be23daSdp int 901a1be23daSdp zonecfg_set_zonepath(zone_dochandle_t handle, char *zonepath) 902a1be23daSdp { 903555afedfScarlsonj size_t len; 904555afedfScarlsonj 905555afedfScarlsonj /* 906555afedfScarlsonj * The user deals in absolute paths in the running global zone, but the 907555afedfScarlsonj * internal configuration files deal with boot environment relative 908555afedfScarlsonj * paths. Strip out the alternate root when specified. 909555afedfScarlsonj */ 910555afedfScarlsonj len = strlen(zonecfg_root); 911555afedfScarlsonj if (strncmp(zonepath, zonecfg_root, len) != 0 || zonepath[len] != '/') 912555afedfScarlsonj return (Z_BAD_PROPERTY); 913555afedfScarlsonj zonepath += len; 914a1be23daSdp return (setrootattr(handle, DTD_ATTR_ZONEPATH, zonepath)); 915a1be23daSdp } 916a1be23daSdp 917e5816e35SEdward Pilatowicz static int 918e5816e35SEdward Pilatowicz i_zonecfg_get_brand(zone_dochandle_t handle, char *brand, size_t brandsize, 919e5816e35SEdward Pilatowicz boolean_t default_query) 9209acbbeafSnn35248 { 9219acbbeafSnn35248 int ret, sz; 9229acbbeafSnn35248 9239acbbeafSnn35248 ret = getrootattr(handle, DTD_ATTR_BRAND, brand, brandsize); 9249acbbeafSnn35248 925e5816e35SEdward Pilatowicz /* 926e5816e35SEdward Pilatowicz * If the lookup failed, or succeeded in finding a non-null brand 927e5816e35SEdward Pilatowicz * string then return. 928e5816e35SEdward Pilatowicz */ 929e5816e35SEdward Pilatowicz if (ret != Z_OK || brand[0] != '\0') 930e5816e35SEdward Pilatowicz return (ret); 931e5816e35SEdward Pilatowicz 932e5816e35SEdward Pilatowicz if (!default_query) { 933e5816e35SEdward Pilatowicz /* If the zone has no brand, it is the default brand. */ 934b08d9f75SEdward Pilatowicz return (zonecfg_default_brand(brand, brandsize)); 9359acbbeafSnn35248 } 9369acbbeafSnn35248 937e5816e35SEdward Pilatowicz /* if SUNWdefault didn't specify a brand, fallback to "native" */ 938e5816e35SEdward Pilatowicz sz = strlcpy(brand, NATIVE_BRAND_NAME, brandsize); 939e5816e35SEdward Pilatowicz if (sz >= brandsize) 940e5816e35SEdward Pilatowicz return (Z_TOO_BIG); 941e5816e35SEdward Pilatowicz return (Z_OK); 942e5816e35SEdward Pilatowicz } 943e5816e35SEdward Pilatowicz 944e5816e35SEdward Pilatowicz int 945e5816e35SEdward Pilatowicz zonecfg_get_brand(zone_dochandle_t handle, char *brand, size_t brandsize) 946e5816e35SEdward Pilatowicz { 947e5816e35SEdward Pilatowicz return (i_zonecfg_get_brand(handle, brand, brandsize, B_FALSE)); 9489acbbeafSnn35248 } 9499acbbeafSnn35248 9509acbbeafSnn35248 int 9519acbbeafSnn35248 zonecfg_set_brand(zone_dochandle_t handle, char *brand) 9529acbbeafSnn35248 { 9539acbbeafSnn35248 return (setrootattr(handle, DTD_ATTR_BRAND, brand)); 9549acbbeafSnn35248 } 9559acbbeafSnn35248 9569acbbeafSnn35248 int 957a1be23daSdp zonecfg_get_autoboot(zone_dochandle_t handle, boolean_t *autoboot) 958a1be23daSdp { 959a1be23daSdp char autobootstr[DTD_ENTITY_BOOL_LEN]; 960a1be23daSdp int ret; 961a1be23daSdp 962a1be23daSdp if ((ret = getrootattr(handle, DTD_ATTR_AUTOBOOT, autobootstr, 963a1be23daSdp sizeof (autobootstr))) != Z_OK) 964a1be23daSdp return (ret); 965a1be23daSdp 966a1be23daSdp if (strcmp(autobootstr, DTD_ENTITY_TRUE) == 0) 967a1be23daSdp *autoboot = B_TRUE; 968a1be23daSdp else if (strcmp(autobootstr, DTD_ENTITY_FALSE) == 0) 969a1be23daSdp *autoboot = B_FALSE; 970a1be23daSdp else 971a1be23daSdp ret = Z_BAD_PROPERTY; 972a1be23daSdp return (ret); 973a1be23daSdp } 974a1be23daSdp 975a1be23daSdp int 976a1be23daSdp zonecfg_set_autoboot(zone_dochandle_t handle, boolean_t autoboot) 977a1be23daSdp { 978a1be23daSdp return (setrootattr(handle, DTD_ATTR_AUTOBOOT, 979a1be23daSdp autoboot ? DTD_ENTITY_TRUE : DTD_ENTITY_FALSE)); 980a1be23daSdp } 981a1be23daSdp 982a1be23daSdp int 983a1be23daSdp zonecfg_get_pool(zone_dochandle_t handle, char *pool, size_t poolsize) 984a1be23daSdp { 985a1be23daSdp return (getrootattr(handle, DTD_ATTR_POOL, pool, poolsize)); 986a1be23daSdp } 987a1be23daSdp 988a1be23daSdp int 989a1be23daSdp zonecfg_set_pool(zone_dochandle_t handle, char *pool) 990a1be23daSdp { 991a1be23daSdp return (setrootattr(handle, DTD_ATTR_POOL, pool)); 992a1be23daSdp } 993a1be23daSdp 994ffbafc53Scomay int 995ffbafc53Scomay zonecfg_get_limitpriv(zone_dochandle_t handle, char **limitpriv) 996ffbafc53Scomay { 997ffbafc53Scomay return (get_alloc_rootattr(handle, DTD_ATTR_LIMITPRIV, limitpriv)); 998ffbafc53Scomay } 999ffbafc53Scomay 1000ffbafc53Scomay int 10013f2f09c1Sdp zonecfg_set_limitpriv(zone_dochandle_t handle, char *limitpriv) 1002ffbafc53Scomay { 10033f2f09c1Sdp return (setrootattr(handle, DTD_ATTR_LIMITPRIV, limitpriv)); 10043f2f09c1Sdp } 10053f2f09c1Sdp 10063f2f09c1Sdp int 10073f2f09c1Sdp zonecfg_get_bootargs(zone_dochandle_t handle, char *bargs, size_t bargssize) 10083f2f09c1Sdp { 10093f2f09c1Sdp return (getrootattr(handle, DTD_ATTR_BOOTARGS, bargs, bargssize)); 10103f2f09c1Sdp } 10113f2f09c1Sdp 10123f2f09c1Sdp int 10133f2f09c1Sdp zonecfg_set_bootargs(zone_dochandle_t handle, char *bargs) 10143f2f09c1Sdp { 10153f2f09c1Sdp return (setrootattr(handle, DTD_ATTR_BOOTARGS, bargs)); 1016ffbafc53Scomay } 1017ffbafc53Scomay 10180209230bSgjelinek int 10190209230bSgjelinek zonecfg_get_sched_class(zone_dochandle_t handle, char *sched, size_t schedsize) 10200209230bSgjelinek { 10210209230bSgjelinek return (getrootattr(handle, DTD_ATTR_SCHED, sched, schedsize)); 10220209230bSgjelinek } 10230209230bSgjelinek 10240209230bSgjelinek int 10250209230bSgjelinek zonecfg_set_sched(zone_dochandle_t handle, char *sched) 10260209230bSgjelinek { 10270209230bSgjelinek return (setrootattr(handle, DTD_ATTR_SCHED, sched)); 10280209230bSgjelinek } 10290209230bSgjelinek 1030a1be23daSdp /* 1031a1be23daSdp * /etc/zones/index caches a vital piece of information which is also 1032a1be23daSdp * in the <zonename>.xml file: the path to the zone. This is for performance, 1033a1be23daSdp * since we need to walk all zonepath's in order to be able to detect conflicts 1034a1be23daSdp * (see crosscheck_zonepaths() in the zoneadm command). 1035087719fdSdp * 1036087719fdSdp * An additional complexity is that when doing a rename, we'd like the entire 1037087719fdSdp * index update operation (rename, and potential state changes) to be atomic. 1038087719fdSdp * In general, the operation of this function should succeed or fail as 1039087719fdSdp * a unit. 1040a1be23daSdp */ 1041a1be23daSdp int 1042a1be23daSdp zonecfg_refresh_index_file(zone_dochandle_t handle) 1043a1be23daSdp { 1044a1be23daSdp char name[ZONENAME_MAX], zonepath[MAXPATHLEN]; 1045a1be23daSdp struct zoneent ze; 1046a1be23daSdp int err; 1047087719fdSdp int opcode; 1048087719fdSdp char *zn; 1049087719fdSdp 1050087719fdSdp bzero(&ze, sizeof (ze)); 1051087719fdSdp ze.zone_state = -1; /* Preserve existing state in index */ 1052a1be23daSdp 1053a1be23daSdp if ((err = zonecfg_get_name(handle, name, sizeof (name))) != Z_OK) 1054a1be23daSdp return (err); 1055087719fdSdp (void) strlcpy(ze.zone_name, name, sizeof (ze.zone_name)); 1056087719fdSdp 1057a1be23daSdp if ((err = zonecfg_get_zonepath(handle, zonepath, 1058a1be23daSdp sizeof (zonepath))) != Z_OK) 1059a1be23daSdp return (err); 1060555afedfScarlsonj (void) strlcpy(ze.zone_path, zonepath + strlen(zonecfg_root), 1061555afedfScarlsonj sizeof (ze.zone_path)); 1062087719fdSdp 1063087719fdSdp if (is_renaming(handle)) { 1064087719fdSdp opcode = PZE_MODIFY; 1065087719fdSdp (void) strlcpy(ze.zone_name, handle->zone_dh_delete_name, 1066087719fdSdp sizeof (ze.zone_name)); 1067087719fdSdp (void) strlcpy(ze.zone_newname, name, sizeof (ze.zone_newname)); 1068087719fdSdp } else if (is_new(handle)) { 1069087719fdSdp FILE *cookie; 1070087719fdSdp /* 1071087719fdSdp * Be tolerant of the zone already existing in the index file, 1072087719fdSdp * since we might be forcibly overwriting an existing 1073087719fdSdp * configuration with a new one (for example 'create -F' 1074087719fdSdp * in zonecfg). 1075087719fdSdp */ 1076087719fdSdp opcode = PZE_ADD; 1077087719fdSdp cookie = setzoneent(); 1078087719fdSdp while ((zn = getzoneent(cookie)) != NULL) { 1079087719fdSdp if (strcmp(zn, name) == 0) { 1080087719fdSdp opcode = PZE_MODIFY; 1081087719fdSdp free(zn); 1082087719fdSdp break; 1083087719fdSdp } 1084087719fdSdp free(zn); 1085087719fdSdp } 1086087719fdSdp endzoneent(cookie); 1087087719fdSdp ze.zone_state = ZONE_STATE_CONFIGURED; 1088087719fdSdp } else { 1089087719fdSdp opcode = PZE_MODIFY; 10907c478bd9Sstevel@tonic-gate } 10917c478bd9Sstevel@tonic-gate 1092087719fdSdp if ((err = putzoneent(&ze, opcode)) != Z_OK) 1093087719fdSdp return (err); 1094087719fdSdp 1095087719fdSdp return (Z_OK); 1096087719fdSdp } 1097087719fdSdp 1098087719fdSdp /* 1099087719fdSdp * The goal of this routine is to cause the index file update and the 1100087719fdSdp * document save to happen as an atomic operation. We do the document 1101087719fdSdp * first, saving a backup copy using a hard link; if that succeeds, we go 1102087719fdSdp * on to the index. If that fails, we roll the document back into place. 1103087719fdSdp * 1104087719fdSdp * Strategy: 1105087719fdSdp * 1106087719fdSdp * New zone 'foo' configuration: 1107087719fdSdp * Create tmpfile (zonecfg.xxxxxx) 1108087719fdSdp * Write XML to tmpfile 1109087719fdSdp * Rename tmpfile to xmlfile (zonecfg.xxxxxx -> foo.xml) 1110087719fdSdp * Add entry to index file 1111087719fdSdp * If it fails, delete foo.xml, leaving nothing behind. 1112087719fdSdp * 1113087719fdSdp * Save existing zone 'foo': 1114087719fdSdp * Make backup of foo.xml -> .backup 1115087719fdSdp * Create tmpfile (zonecfg.xxxxxx) 1116087719fdSdp * Write XML to tmpfile 1117087719fdSdp * Rename tmpfile to xmlfile (zonecfg.xxxxxx -> foo.xml) 1118087719fdSdp * Modify index file as needed 1119087719fdSdp * If it fails, recover from .backup -> foo.xml 1120087719fdSdp * 1121087719fdSdp * Rename 'foo' to 'bar': 1122087719fdSdp * Create tmpfile (zonecfg.xxxxxx) 1123087719fdSdp * Write XML to tmpfile 1124087719fdSdp * Rename tmpfile to xmlfile (zonecfg.xxxxxx -> bar.xml) 1125087719fdSdp * Add entry for 'bar' to index file, Remove entry for 'foo' (refresh) 1126087719fdSdp * If it fails, delete bar.xml; foo.xml is left behind. 1127087719fdSdp */ 11287c478bd9Sstevel@tonic-gate static int 11297c478bd9Sstevel@tonic-gate zonecfg_save_impl(zone_dochandle_t handle, char *filename) 11307c478bd9Sstevel@tonic-gate { 11317c478bd9Sstevel@tonic-gate char tmpfile[MAXPATHLEN]; 1132087719fdSdp char bakdir[MAXPATHLEN], bakbase[MAXPATHLEN], bakfile[MAXPATHLEN]; 11339acbbeafSnn35248 int tmpfd, err, valid; 11347c478bd9Sstevel@tonic-gate xmlValidCtxt cvp = { NULL }; 1135087719fdSdp boolean_t backup; 11367c478bd9Sstevel@tonic-gate 11377c478bd9Sstevel@tonic-gate (void) strlcpy(tmpfile, filename, sizeof (tmpfile)); 11387c478bd9Sstevel@tonic-gate (void) dirname(tmpfile); 11397c478bd9Sstevel@tonic-gate (void) strlcat(tmpfile, _PATH_TMPFILE, sizeof (tmpfile)); 11407c478bd9Sstevel@tonic-gate 11417c478bd9Sstevel@tonic-gate tmpfd = mkstemp(tmpfile); 11427c478bd9Sstevel@tonic-gate if (tmpfd == -1) { 11437c478bd9Sstevel@tonic-gate (void) unlink(tmpfile); 11447c478bd9Sstevel@tonic-gate return (Z_TEMP_FILE); 11457c478bd9Sstevel@tonic-gate } 11467c478bd9Sstevel@tonic-gate (void) close(tmpfd); 11477c478bd9Sstevel@tonic-gate 11487c478bd9Sstevel@tonic-gate cvp.error = zonecfg_error_func; 11497c478bd9Sstevel@tonic-gate cvp.warning = zonecfg_error_func; 11507c478bd9Sstevel@tonic-gate 1151087719fdSdp /* 11529acbbeafSnn35248 * We do a final validation of the document. Since the library has 11539acbbeafSnn35248 * malfunctioned if it fails to validate, we follow-up with an 11549acbbeafSnn35248 * assert() that the doc is valid. 1155087719fdSdp */ 11569acbbeafSnn35248 valid = xmlValidateDocument(&cvp, handle->zone_dh_doc); 11579acbbeafSnn35248 assert(valid != 0); 11587c478bd9Sstevel@tonic-gate 11597c478bd9Sstevel@tonic-gate if (xmlSaveFormatFile(tmpfile, handle->zone_dh_doc, 1) <= 0) 11607c478bd9Sstevel@tonic-gate goto err; 1161087719fdSdp 11627c478bd9Sstevel@tonic-gate (void) chmod(tmpfile, 0644); 11637c478bd9Sstevel@tonic-gate 1164087719fdSdp /* 1165087719fdSdp * In the event we are doing a standard save, hard link a copy of the 1166087719fdSdp * original file in .backup.<pid>.filename so we can restore it if 1167087719fdSdp * something goes wrong. 1168087719fdSdp */ 1169087719fdSdp if (!is_new(handle) && !is_renaming(handle)) { 1170087719fdSdp backup = B_TRUE; 1171087719fdSdp 1172087719fdSdp (void) strlcpy(bakdir, filename, sizeof (bakdir)); 1173087719fdSdp (void) strlcpy(bakbase, filename, sizeof (bakbase)); 1174087719fdSdp (void) snprintf(bakfile, sizeof (bakfile), "%s/.backup.%d.%s", 1175087719fdSdp dirname(bakdir), getpid(), basename(bakbase)); 1176087719fdSdp 1177087719fdSdp if (link(filename, bakfile) == -1) { 1178087719fdSdp err = errno; 11797c478bd9Sstevel@tonic-gate (void) unlink(tmpfile); 11807c478bd9Sstevel@tonic-gate if (errno == EACCES) 11817c478bd9Sstevel@tonic-gate return (Z_ACCES); 11827c478bd9Sstevel@tonic-gate return (Z_MISC_FS); 11837c478bd9Sstevel@tonic-gate } 1184087719fdSdp } 1185a1be23daSdp 1186087719fdSdp /* 1187087719fdSdp * Move the new document over top of the old. 1188087719fdSdp * i.e.: zonecfg.XXXXXX -> myzone.xml 1189087719fdSdp */ 1190087719fdSdp if (rename(tmpfile, filename) == -1) { 1191087719fdSdp err = errno; 1192087719fdSdp (void) unlink(tmpfile); 1193087719fdSdp if (backup) 1194087719fdSdp (void) unlink(bakfile); 1195087719fdSdp if (err == EACCES) 1196087719fdSdp return (Z_ACCES); 1197087719fdSdp return (Z_MISC_FS); 1198087719fdSdp } 1199087719fdSdp 1200087719fdSdp /* 1201087719fdSdp * If this is a snapshot, we're done-- don't add an index entry. 1202087719fdSdp */ 1203087719fdSdp if (is_snapshot(handle)) 1204087719fdSdp return (Z_OK); 1205087719fdSdp 1206087719fdSdp /* now update the index file to reflect whatever we just did */ 1207087719fdSdp if ((err = zonecfg_refresh_index_file(handle)) != Z_OK) { 1208087719fdSdp if (backup) { 1209087719fdSdp /* 1210087719fdSdp * Try to restore from our backup. 1211087719fdSdp */ 1212087719fdSdp (void) unlink(filename); 1213087719fdSdp (void) rename(bakfile, filename); 1214087719fdSdp } else { 1215087719fdSdp /* 1216087719fdSdp * Either the zone is new, in which case we can delete 1217087719fdSdp * new.xml, or we're doing a rename, so ditto. 1218087719fdSdp */ 1219087719fdSdp assert(is_new(handle) || is_renaming(handle)); 1220087719fdSdp (void) unlink(filename); 1221087719fdSdp } 1222087719fdSdp return (Z_UPDATING_INDEX); 1223087719fdSdp } 1224087719fdSdp 1225087719fdSdp if (backup) 1226087719fdSdp (void) unlink(bakfile); 1227087719fdSdp 1228087719fdSdp return (Z_OK); 12297c478bd9Sstevel@tonic-gate 12307c478bd9Sstevel@tonic-gate err: 12317c478bd9Sstevel@tonic-gate (void) unlink(tmpfile); 12327c478bd9Sstevel@tonic-gate return (Z_SAVING_FILE); 12337c478bd9Sstevel@tonic-gate } 12347c478bd9Sstevel@tonic-gate 12357c478bd9Sstevel@tonic-gate int 12367c478bd9Sstevel@tonic-gate zonecfg_save(zone_dochandle_t handle) 12377c478bd9Sstevel@tonic-gate { 1238087719fdSdp char zname[ZONENAME_MAX], path[MAXPATHLEN]; 1239087719fdSdp char delpath[MAXPATHLEN]; 1240087719fdSdp int err = Z_SAVING_FILE; 12417c478bd9Sstevel@tonic-gate 1242087719fdSdp if (zonecfg_check_handle(handle) != Z_OK) 1243087719fdSdp return (Z_BAD_HANDLE); 1244087719fdSdp 1245087719fdSdp /* 1246ee519a1fSgjelinek * We don't support saving snapshots or a tree containing a sw 1247ee519a1fSgjelinek * inventory at this time. 1248087719fdSdp */ 1249ee519a1fSgjelinek if (handle->zone_dh_snapshot || handle->zone_dh_sw_inv) 1250087719fdSdp return (Z_INVAL); 1251087719fdSdp 1252087719fdSdp if ((err = zonecfg_get_name(handle, zname, sizeof (zname))) != Z_OK) 12537c478bd9Sstevel@tonic-gate return (err); 1254087719fdSdp 1255108322fbScarlsonj if (!config_file_path(zname, path)) 1256108322fbScarlsonj return (Z_MISC_FS); 1257087719fdSdp 1258087719fdSdp addcomment(handle, "\n DO NOT EDIT THIS " 1259087719fdSdp "FILE. Use zonecfg(1M) instead.\n"); 1260087719fdSdp 1261087719fdSdp err = zonecfg_save_impl(handle, path); 1262087719fdSdp 1263087719fdSdp stripcomments(handle); 1264087719fdSdp 1265087719fdSdp if (err != Z_OK) 1266087719fdSdp return (err); 1267087719fdSdp 1268087719fdSdp handle->zone_dh_newzone = B_FALSE; 1269087719fdSdp 1270087719fdSdp if (is_renaming(handle)) { 1271108322fbScarlsonj if (config_file_path(handle->zone_dh_delete_name, delpath)) 1272087719fdSdp (void) unlink(delpath); 1273087719fdSdp handle->zone_dh_delete_name[0] = '\0'; 1274087719fdSdp } 1275087719fdSdp 1276087719fdSdp return (Z_OK); 12777c478bd9Sstevel@tonic-gate } 12787c478bd9Sstevel@tonic-gate 1279ee519a1fSgjelinek int 12809acbbeafSnn35248 zonecfg_verify_save(zone_dochandle_t handle, char *filename) 12819acbbeafSnn35248 { 12829acbbeafSnn35248 int valid; 12839acbbeafSnn35248 12849acbbeafSnn35248 xmlValidCtxt cvp = { NULL }; 12859acbbeafSnn35248 12869acbbeafSnn35248 if (zonecfg_check_handle(handle) != Z_OK) 12879acbbeafSnn35248 return (Z_BAD_HANDLE); 12889acbbeafSnn35248 12899acbbeafSnn35248 cvp.error = zonecfg_error_func; 12909acbbeafSnn35248 cvp.warning = zonecfg_error_func; 12919acbbeafSnn35248 12929acbbeafSnn35248 /* 12939acbbeafSnn35248 * We do a final validation of the document. Since the library has 12949acbbeafSnn35248 * malfunctioned if it fails to validate, we follow-up with an 12959acbbeafSnn35248 * assert() that the doc is valid. 12969acbbeafSnn35248 */ 12979acbbeafSnn35248 valid = xmlValidateDocument(&cvp, handle->zone_dh_doc); 12989acbbeafSnn35248 assert(valid != 0); 12999acbbeafSnn35248 13009acbbeafSnn35248 if (xmlSaveFormatFile(filename, handle->zone_dh_doc, 1) <= 0) 13019acbbeafSnn35248 return (Z_SAVING_FILE); 13029acbbeafSnn35248 13039acbbeafSnn35248 return (Z_OK); 13049acbbeafSnn35248 } 13059acbbeafSnn35248 13069acbbeafSnn35248 int 13078cd327d5Sgjelinek zonecfg_detach_save(zone_dochandle_t handle, uint_t flags) 1308ee519a1fSgjelinek { 1309ee519a1fSgjelinek char zname[ZONENAME_MAX]; 1310ee519a1fSgjelinek char path[MAXPATHLEN]; 1311ee519a1fSgjelinek char migpath[MAXPATHLEN]; 1312ee519a1fSgjelinek xmlValidCtxt cvp = { NULL }; 1313ee519a1fSgjelinek int err = Z_SAVING_FILE; 13149acbbeafSnn35248 int valid; 1315ee519a1fSgjelinek 1316ee519a1fSgjelinek if (zonecfg_check_handle(handle) != Z_OK) 1317ee519a1fSgjelinek return (Z_BAD_HANDLE); 1318ee519a1fSgjelinek 13198cd327d5Sgjelinek if (flags & ZONE_DRY_RUN) { 13208cd327d5Sgjelinek (void) strlcpy(migpath, "-", sizeof (migpath)); 13218cd327d5Sgjelinek } else { 13228cd327d5Sgjelinek if ((err = zonecfg_get_name(handle, zname, sizeof (zname))) 13238cd327d5Sgjelinek != Z_OK) 1324ee519a1fSgjelinek return (err); 1325ee519a1fSgjelinek 13268cd327d5Sgjelinek if ((err = zone_get_zonepath(zname, path, sizeof (path))) 13278cd327d5Sgjelinek != Z_OK) 1328ee519a1fSgjelinek return (err); 1329ee519a1fSgjelinek 133016ab8c7bSgjelinek if (snprintf(migpath, sizeof (migpath), "%s/%s", path, 133116ab8c7bSgjelinek ZONE_DETACHED) >= sizeof (migpath)) 1332ee519a1fSgjelinek return (Z_NOMEM); 13338cd327d5Sgjelinek } 1334ee519a1fSgjelinek 1335ee519a1fSgjelinek if ((err = operation_prep(handle)) != Z_OK) 1336ee519a1fSgjelinek return (err); 1337ee519a1fSgjelinek 1338ee519a1fSgjelinek addcomment(handle, "\n DO NOT EDIT THIS FILE. " 1339ee519a1fSgjelinek "Use zonecfg(1M) and zoneadm(1M) attach.\n"); 1340ee519a1fSgjelinek 1341ee519a1fSgjelinek cvp.error = zonecfg_error_func; 1342ee519a1fSgjelinek cvp.warning = zonecfg_error_func; 1343ee519a1fSgjelinek 1344ee519a1fSgjelinek /* 13459acbbeafSnn35248 * We do a final validation of the document. Since the library has 13469acbbeafSnn35248 * malfunctioned if it fails to validate, we follow-up with an 13479acbbeafSnn35248 * assert() that the doc is valid. 1348ee519a1fSgjelinek */ 13499acbbeafSnn35248 valid = xmlValidateDocument(&cvp, handle->zone_dh_doc); 13509acbbeafSnn35248 assert(valid != 0); 1351ee519a1fSgjelinek 1352ee519a1fSgjelinek if (xmlSaveFormatFile(migpath, handle->zone_dh_doc, 1) <= 0) 1353ee519a1fSgjelinek return (Z_SAVING_FILE); 1354ee519a1fSgjelinek 13558cd327d5Sgjelinek if (!(flags & ZONE_DRY_RUN)) 1356ee519a1fSgjelinek (void) chmod(migpath, 0644); 1357ee519a1fSgjelinek 1358ee519a1fSgjelinek stripcomments(handle); 1359ee519a1fSgjelinek 1360ee519a1fSgjelinek handle->zone_dh_newzone = B_FALSE; 1361ee519a1fSgjelinek 1362ee519a1fSgjelinek return (Z_OK); 1363ee519a1fSgjelinek } 1364ee519a1fSgjelinek 1365ee519a1fSgjelinek boolean_t 1366ee519a1fSgjelinek zonecfg_detached(const char *path) 1367ee519a1fSgjelinek { 1368ee519a1fSgjelinek char migpath[MAXPATHLEN]; 1369ee519a1fSgjelinek struct stat buf; 1370ee519a1fSgjelinek 137116ab8c7bSgjelinek if (snprintf(migpath, sizeof (migpath), "%s/%s", path, ZONE_DETACHED) >= 1372ee519a1fSgjelinek sizeof (migpath)) 1373ee519a1fSgjelinek return (B_FALSE); 1374ee519a1fSgjelinek 1375ee519a1fSgjelinek if (stat(migpath, &buf) != -1) 1376ee519a1fSgjelinek return (B_TRUE); 1377ee519a1fSgjelinek 1378ee519a1fSgjelinek return (B_FALSE); 1379ee519a1fSgjelinek } 1380ee519a1fSgjelinek 1381ee519a1fSgjelinek void 1382ee519a1fSgjelinek zonecfg_rm_detached(zone_dochandle_t handle, boolean_t forced) 1383ee519a1fSgjelinek { 1384ee519a1fSgjelinek char zname[ZONENAME_MAX]; 1385ee519a1fSgjelinek char path[MAXPATHLEN]; 1386ee519a1fSgjelinek char detached[MAXPATHLEN]; 1387ee519a1fSgjelinek char attached[MAXPATHLEN]; 1388ee519a1fSgjelinek 1389ee519a1fSgjelinek if (zonecfg_check_handle(handle) != Z_OK) 1390ee519a1fSgjelinek return; 1391ee519a1fSgjelinek 1392ee519a1fSgjelinek if (zonecfg_get_name(handle, zname, sizeof (zname)) != Z_OK) 1393ee519a1fSgjelinek return; 1394ee519a1fSgjelinek 1395ee519a1fSgjelinek if (zone_get_zonepath(zname, path, sizeof (path)) != Z_OK) 1396ee519a1fSgjelinek return; 1397ee519a1fSgjelinek 139816ab8c7bSgjelinek (void) snprintf(detached, sizeof (detached), "%s/%s", path, 139916ab8c7bSgjelinek ZONE_DETACHED); 1400ee519a1fSgjelinek (void) snprintf(attached, sizeof (attached), "%s/%s", path, 1401ee519a1fSgjelinek ATTACH_FORCED); 1402ee519a1fSgjelinek 1403ee519a1fSgjelinek if (forced) { 1404ee519a1fSgjelinek (void) rename(detached, attached); 1405ee519a1fSgjelinek } else { 1406ee519a1fSgjelinek (void) unlink(attached); 1407ee519a1fSgjelinek (void) unlink(detached); 1408ee519a1fSgjelinek } 1409ee519a1fSgjelinek } 1410ee519a1fSgjelinek 14117c478bd9Sstevel@tonic-gate /* 14127c478bd9Sstevel@tonic-gate * Special case: if access(2) fails with ENOENT, then try again using 14137c478bd9Sstevel@tonic-gate * ZONE_CONFIG_ROOT instead of config_file_path(zonename). This is how we 14147c478bd9Sstevel@tonic-gate * work around the case of a config file which has not been created yet: 14157c478bd9Sstevel@tonic-gate * the user will need access to the directory so use that as a heuristic. 14167c478bd9Sstevel@tonic-gate */ 14177c478bd9Sstevel@tonic-gate 14187c478bd9Sstevel@tonic-gate int 14197c478bd9Sstevel@tonic-gate zonecfg_access(const char *zonename, int amode) 14207c478bd9Sstevel@tonic-gate { 14217c478bd9Sstevel@tonic-gate char path[MAXPATHLEN]; 14227c478bd9Sstevel@tonic-gate 1423108322fbScarlsonj if (!config_file_path(zonename, path)) 1424108322fbScarlsonj return (Z_INVAL); 14257c478bd9Sstevel@tonic-gate if (access(path, amode) == 0) 14267c478bd9Sstevel@tonic-gate return (Z_OK); 1427108322fbScarlsonj if (errno == ENOENT) { 1428108322fbScarlsonj if (snprintf(path, sizeof (path), "%s%s", zonecfg_root, 1429108322fbScarlsonj ZONE_CONFIG_ROOT) >= sizeof (path)) 1430108322fbScarlsonj return (Z_INVAL); 1431108322fbScarlsonj if (access(path, amode) == 0) 14327c478bd9Sstevel@tonic-gate return (Z_OK); 1433108322fbScarlsonj } 14347c478bd9Sstevel@tonic-gate if (errno == EACCES) 14357c478bd9Sstevel@tonic-gate return (Z_ACCES); 14367c478bd9Sstevel@tonic-gate if (errno == EINVAL) 14377c478bd9Sstevel@tonic-gate return (Z_INVAL); 14387c478bd9Sstevel@tonic-gate return (Z_MISC_FS); 14397c478bd9Sstevel@tonic-gate } 14407c478bd9Sstevel@tonic-gate 14417c478bd9Sstevel@tonic-gate int 1442108322fbScarlsonj zonecfg_create_snapshot(const char *zonename) 14437c478bd9Sstevel@tonic-gate { 14447c478bd9Sstevel@tonic-gate zone_dochandle_t handle; 14457c478bd9Sstevel@tonic-gate char path[MAXPATHLEN], zonepath[MAXPATHLEN], rpath[MAXPATHLEN]; 14467c478bd9Sstevel@tonic-gate int error = Z_OK, res; 14477c478bd9Sstevel@tonic-gate 14487c478bd9Sstevel@tonic-gate if ((handle = zonecfg_init_handle()) == NULL) { 14497c478bd9Sstevel@tonic-gate return (Z_NOMEM); 14507c478bd9Sstevel@tonic-gate } 14517c478bd9Sstevel@tonic-gate 1452087719fdSdp handle->zone_dh_newzone = B_TRUE; 1453087719fdSdp handle->zone_dh_snapshot = B_TRUE; 1454087719fdSdp 14557c478bd9Sstevel@tonic-gate if ((error = zonecfg_get_handle(zonename, handle)) != Z_OK) 14567c478bd9Sstevel@tonic-gate goto out; 14577c478bd9Sstevel@tonic-gate if ((error = operation_prep(handle)) != Z_OK) 14587c478bd9Sstevel@tonic-gate goto out; 14597c478bd9Sstevel@tonic-gate error = zonecfg_get_zonepath(handle, zonepath, sizeof (zonepath)); 14607c478bd9Sstevel@tonic-gate if (error != Z_OK) 14617c478bd9Sstevel@tonic-gate goto out; 14627c478bd9Sstevel@tonic-gate if ((res = resolvepath(zonepath, rpath, sizeof (rpath))) == -1) { 14637c478bd9Sstevel@tonic-gate error = Z_RESOLVED_PATH; 14647c478bd9Sstevel@tonic-gate goto out; 14657c478bd9Sstevel@tonic-gate } 14667c478bd9Sstevel@tonic-gate /* 14677c478bd9Sstevel@tonic-gate * If the resolved path is not the same as the original path, then 14687c478bd9Sstevel@tonic-gate * save the resolved path in the snapshot, thus preventing any 14697c478bd9Sstevel@tonic-gate * potential problems down the line when zoneadmd goes to unmount 14707c478bd9Sstevel@tonic-gate * file systems and depends on initial string matches with resolved 14717c478bd9Sstevel@tonic-gate * paths. 14727c478bd9Sstevel@tonic-gate */ 14737c478bd9Sstevel@tonic-gate rpath[res] = '\0'; 14747c478bd9Sstevel@tonic-gate if (strcmp(zonepath, rpath) != 0) { 14757c478bd9Sstevel@tonic-gate if ((error = zonecfg_set_zonepath(handle, rpath)) != Z_OK) 14767c478bd9Sstevel@tonic-gate goto out; 14777c478bd9Sstevel@tonic-gate } 1478108322fbScarlsonj if (snprintf(path, sizeof (path), "%s%s", zonecfg_root, 1479108322fbScarlsonj ZONE_SNAPSHOT_ROOT) >= sizeof (path)) { 1480108322fbScarlsonj error = Z_MISC_FS; 1481108322fbScarlsonj goto out; 1482108322fbScarlsonj } 1483108322fbScarlsonj if ((mkdir(path, S_IRWXU) == -1) && (errno != EEXIST)) { 14847c478bd9Sstevel@tonic-gate error = Z_MISC_FS; 14857c478bd9Sstevel@tonic-gate goto out; 14867c478bd9Sstevel@tonic-gate } 14877c478bd9Sstevel@tonic-gate 1488108322fbScarlsonj if (!snap_file_path(zonename, path)) { 1489108322fbScarlsonj error = Z_MISC_FS; 1490108322fbScarlsonj goto out; 1491108322fbScarlsonj } 1492087719fdSdp 1493087719fdSdp addcomment(handle, "\n DO NOT EDIT THIS FILE. " 1494087719fdSdp "It is a snapshot of running zone state.\n"); 1495087719fdSdp 14967c478bd9Sstevel@tonic-gate error = zonecfg_save_impl(handle, path); 14977c478bd9Sstevel@tonic-gate 1498087719fdSdp stripcomments(handle); 1499087719fdSdp 15007c478bd9Sstevel@tonic-gate out: 15017c478bd9Sstevel@tonic-gate zonecfg_fini_handle(handle); 15027c478bd9Sstevel@tonic-gate return (error); 15037c478bd9Sstevel@tonic-gate } 15047c478bd9Sstevel@tonic-gate 1505f4b3ec61Sdh155122 int 1506f4b3ec61Sdh155122 zonecfg_get_iptype(zone_dochandle_t handle, zone_iptype_t *iptypep) 1507f4b3ec61Sdh155122 { 1508f4b3ec61Sdh155122 char property[10]; /* 10 is big enough for "shared"/"exclusive" */ 1509f4b3ec61Sdh155122 int err; 1510f4b3ec61Sdh155122 1511f4b3ec61Sdh155122 err = getrootattr(handle, DTD_ATTR_IPTYPE, property, sizeof (property)); 1512f4b3ec61Sdh155122 if (err == Z_BAD_PROPERTY) { 1513f4b3ec61Sdh155122 /* Return default value */ 1514f4b3ec61Sdh155122 *iptypep = ZS_SHARED; 1515f4b3ec61Sdh155122 return (Z_OK); 1516f4b3ec61Sdh155122 } else if (err != Z_OK) { 1517f4b3ec61Sdh155122 return (err); 1518f4b3ec61Sdh155122 } 1519f4b3ec61Sdh155122 1520f4b3ec61Sdh155122 if (strlen(property) == 0 || 1521f4b3ec61Sdh155122 strcmp(property, "shared") == 0) 1522f4b3ec61Sdh155122 *iptypep = ZS_SHARED; 1523f4b3ec61Sdh155122 else if (strcmp(property, "exclusive") == 0) 1524f4b3ec61Sdh155122 *iptypep = ZS_EXCLUSIVE; 1525f4b3ec61Sdh155122 else 1526f4b3ec61Sdh155122 return (Z_INVAL); 1527f4b3ec61Sdh155122 1528f4b3ec61Sdh155122 return (Z_OK); 1529f4b3ec61Sdh155122 } 1530f4b3ec61Sdh155122 1531f4b3ec61Sdh155122 int 1532f4b3ec61Sdh155122 zonecfg_set_iptype(zone_dochandle_t handle, zone_iptype_t iptype) 1533f4b3ec61Sdh155122 { 1534f4b3ec61Sdh155122 xmlNodePtr cur; 1535f4b3ec61Sdh155122 1536f4b3ec61Sdh155122 if (handle == NULL) 1537f4b3ec61Sdh155122 return (Z_INVAL); 1538f4b3ec61Sdh155122 1539f4b3ec61Sdh155122 cur = xmlDocGetRootElement(handle->zone_dh_doc); 1540f4b3ec61Sdh155122 if (cur == NULL) { 1541f4b3ec61Sdh155122 return (Z_EMPTY_DOCUMENT); 1542f4b3ec61Sdh155122 } 1543f4b3ec61Sdh155122 1544f4b3ec61Sdh155122 if (xmlStrcmp(cur->name, DTD_ELEM_ZONE) != 0) { 1545f4b3ec61Sdh155122 return (Z_WRONG_DOC_TYPE); 1546f4b3ec61Sdh155122 } 1547f4b3ec61Sdh155122 switch (iptype) { 1548f4b3ec61Sdh155122 case ZS_SHARED: 1549f4b3ec61Sdh155122 /* 1550f4b3ec61Sdh155122 * Since "shared" is the default, we don't write it to the 1551f4b3ec61Sdh155122 * configuration file, so that it's easier to migrate those 1552f4b3ec61Sdh155122 * zones elsewhere, eg., to systems which are not IP-Instances 1553f4b3ec61Sdh155122 * aware. 1554f4b3ec61Sdh155122 * xmlUnsetProp only fails when the attribute doesn't exist, 1555f4b3ec61Sdh155122 * which we don't care. 1556f4b3ec61Sdh155122 */ 1557f4b3ec61Sdh155122 (void) xmlUnsetProp(cur, DTD_ATTR_IPTYPE); 1558f4b3ec61Sdh155122 break; 1559f4b3ec61Sdh155122 case ZS_EXCLUSIVE: 1560f4b3ec61Sdh155122 if (xmlSetProp(cur, DTD_ATTR_IPTYPE, 1561f4b3ec61Sdh155122 (const xmlChar *) "exclusive") == NULL) 1562f4b3ec61Sdh155122 return (Z_INVAL); 1563f4b3ec61Sdh155122 break; 1564f4b3ec61Sdh155122 } 1565f4b3ec61Sdh155122 return (Z_OK); 1566f4b3ec61Sdh155122 } 1567f4b3ec61Sdh155122 15687c478bd9Sstevel@tonic-gate static int 1569a1be23daSdp newprop(xmlNodePtr node, const xmlChar *attrname, char *src) 15707c478bd9Sstevel@tonic-gate { 15717c478bd9Sstevel@tonic-gate xmlAttrPtr newattr; 15727c478bd9Sstevel@tonic-gate 15737c478bd9Sstevel@tonic-gate newattr = xmlNewProp(node, attrname, (xmlChar *)src); 15747c478bd9Sstevel@tonic-gate if (newattr == NULL) { 15757c478bd9Sstevel@tonic-gate xmlUnlinkNode(node); 15767c478bd9Sstevel@tonic-gate xmlFreeNode(node); 15777c478bd9Sstevel@tonic-gate return (Z_BAD_PROPERTY); 15787c478bd9Sstevel@tonic-gate } 15797c478bd9Sstevel@tonic-gate return (Z_OK); 15807c478bd9Sstevel@tonic-gate } 15817c478bd9Sstevel@tonic-gate 15827c478bd9Sstevel@tonic-gate static int 15837c478bd9Sstevel@tonic-gate zonecfg_add_filesystem_core(zone_dochandle_t handle, struct zone_fstab *tabptr) 15847c478bd9Sstevel@tonic-gate { 15857c478bd9Sstevel@tonic-gate xmlNodePtr newnode, cur = handle->zone_dh_cur, options_node; 15867c478bd9Sstevel@tonic-gate zone_fsopt_t *ptr; 15877c478bd9Sstevel@tonic-gate int err; 15887c478bd9Sstevel@tonic-gate 15897c478bd9Sstevel@tonic-gate newnode = xmlNewTextChild(cur, NULL, DTD_ELEM_FS, NULL); 1590a1be23daSdp if ((err = newprop(newnode, DTD_ATTR_SPECIAL, 15917c478bd9Sstevel@tonic-gate tabptr->zone_fs_special)) != Z_OK) 15927c478bd9Sstevel@tonic-gate return (err); 15937c478bd9Sstevel@tonic-gate if (tabptr->zone_fs_raw[0] != '\0' && 1594a1be23daSdp (err = newprop(newnode, DTD_ATTR_RAW, tabptr->zone_fs_raw)) != Z_OK) 15957c478bd9Sstevel@tonic-gate return (err); 1596a1be23daSdp if ((err = newprop(newnode, DTD_ATTR_DIR, tabptr->zone_fs_dir)) != Z_OK) 15977c478bd9Sstevel@tonic-gate return (err); 1598a1be23daSdp if ((err = newprop(newnode, DTD_ATTR_TYPE, 15997c478bd9Sstevel@tonic-gate tabptr->zone_fs_type)) != Z_OK) 16007c478bd9Sstevel@tonic-gate return (err); 16017c478bd9Sstevel@tonic-gate if (tabptr->zone_fs_options != NULL) { 16027c478bd9Sstevel@tonic-gate for (ptr = tabptr->zone_fs_options; ptr != NULL; 16037c478bd9Sstevel@tonic-gate ptr = ptr->zone_fsopt_next) { 16047c478bd9Sstevel@tonic-gate options_node = xmlNewTextChild(newnode, NULL, 16057c478bd9Sstevel@tonic-gate DTD_ELEM_FSOPTION, NULL); 1606a1be23daSdp if ((err = newprop(options_node, DTD_ATTR_NAME, 16077c478bd9Sstevel@tonic-gate ptr->zone_fsopt_opt)) != Z_OK) 16087c478bd9Sstevel@tonic-gate return (err); 16097c478bd9Sstevel@tonic-gate } 16107c478bd9Sstevel@tonic-gate } 16117c478bd9Sstevel@tonic-gate return (Z_OK); 16127c478bd9Sstevel@tonic-gate } 16137c478bd9Sstevel@tonic-gate 16147c478bd9Sstevel@tonic-gate int 16157c478bd9Sstevel@tonic-gate zonecfg_add_filesystem(zone_dochandle_t handle, struct zone_fstab *tabptr) 16167c478bd9Sstevel@tonic-gate { 16177c478bd9Sstevel@tonic-gate int err; 16187c478bd9Sstevel@tonic-gate 16197c478bd9Sstevel@tonic-gate if (tabptr == NULL) 16207c478bd9Sstevel@tonic-gate return (Z_INVAL); 16217c478bd9Sstevel@tonic-gate 16227c478bd9Sstevel@tonic-gate if ((err = operation_prep(handle)) != Z_OK) 16237c478bd9Sstevel@tonic-gate return (err); 16247c478bd9Sstevel@tonic-gate 16257c478bd9Sstevel@tonic-gate if ((err = zonecfg_add_filesystem_core(handle, tabptr)) != Z_OK) 16267c478bd9Sstevel@tonic-gate return (err); 16277c478bd9Sstevel@tonic-gate 16287c478bd9Sstevel@tonic-gate return (Z_OK); 16297c478bd9Sstevel@tonic-gate } 16307c478bd9Sstevel@tonic-gate 16317c478bd9Sstevel@tonic-gate static int 16327c478bd9Sstevel@tonic-gate zonecfg_add_ipd_core(zone_dochandle_t handle, struct zone_fstab *tabptr) 16337c478bd9Sstevel@tonic-gate { 16347c478bd9Sstevel@tonic-gate xmlNodePtr newnode, cur = handle->zone_dh_cur; 16357c478bd9Sstevel@tonic-gate int err; 16367c478bd9Sstevel@tonic-gate 16377c478bd9Sstevel@tonic-gate newnode = xmlNewTextChild(cur, NULL, DTD_ELEM_IPD, NULL); 1638a1be23daSdp if ((err = newprop(newnode, DTD_ATTR_DIR, tabptr->zone_fs_dir)) != Z_OK) 16397c478bd9Sstevel@tonic-gate return (err); 16407c478bd9Sstevel@tonic-gate return (Z_OK); 16417c478bd9Sstevel@tonic-gate } 16427c478bd9Sstevel@tonic-gate 16437c478bd9Sstevel@tonic-gate int 16447c478bd9Sstevel@tonic-gate zonecfg_add_ipd(zone_dochandle_t handle, struct zone_fstab *tabptr) 16457c478bd9Sstevel@tonic-gate { 16467c478bd9Sstevel@tonic-gate int err; 16477c478bd9Sstevel@tonic-gate 16487c478bd9Sstevel@tonic-gate if (tabptr == NULL) 16497c478bd9Sstevel@tonic-gate return (Z_INVAL); 16507c478bd9Sstevel@tonic-gate 16517c478bd9Sstevel@tonic-gate if ((err = operation_prep(handle)) != Z_OK) 16527c478bd9Sstevel@tonic-gate return (err); 16537c478bd9Sstevel@tonic-gate 16547c478bd9Sstevel@tonic-gate if ((err = zonecfg_add_ipd_core(handle, tabptr)) != Z_OK) 16557c478bd9Sstevel@tonic-gate return (err); 16567c478bd9Sstevel@tonic-gate 16577c478bd9Sstevel@tonic-gate return (Z_OK); 16587c478bd9Sstevel@tonic-gate } 16597c478bd9Sstevel@tonic-gate 16607c478bd9Sstevel@tonic-gate int 16617c478bd9Sstevel@tonic-gate zonecfg_add_fs_option(struct zone_fstab *tabptr, char *option) 16627c478bd9Sstevel@tonic-gate { 16637c478bd9Sstevel@tonic-gate zone_fsopt_t *last, *old, *new; 16647c478bd9Sstevel@tonic-gate 16657c478bd9Sstevel@tonic-gate last = tabptr->zone_fs_options; 16667c478bd9Sstevel@tonic-gate for (old = last; old != NULL; old = old->zone_fsopt_next) 16677c478bd9Sstevel@tonic-gate last = old; /* walk to the end of the list */ 16687c478bd9Sstevel@tonic-gate new = (zone_fsopt_t *)malloc(sizeof (zone_fsopt_t)); 16697c478bd9Sstevel@tonic-gate if (new == NULL) 16707c478bd9Sstevel@tonic-gate return (Z_NOMEM); 16717c478bd9Sstevel@tonic-gate (void) strlcpy(new->zone_fsopt_opt, option, 16727c478bd9Sstevel@tonic-gate sizeof (new->zone_fsopt_opt)); 16737c478bd9Sstevel@tonic-gate new->zone_fsopt_next = NULL; 16747c478bd9Sstevel@tonic-gate if (last == NULL) 16757c478bd9Sstevel@tonic-gate tabptr->zone_fs_options = new; 16767c478bd9Sstevel@tonic-gate else 16777c478bd9Sstevel@tonic-gate last->zone_fsopt_next = new; 16787c478bd9Sstevel@tonic-gate return (Z_OK); 16797c478bd9Sstevel@tonic-gate } 16807c478bd9Sstevel@tonic-gate 16817c478bd9Sstevel@tonic-gate int 16827c478bd9Sstevel@tonic-gate zonecfg_remove_fs_option(struct zone_fstab *tabptr, char *option) 16837c478bd9Sstevel@tonic-gate { 16847c478bd9Sstevel@tonic-gate zone_fsopt_t *last, *this, *next; 16857c478bd9Sstevel@tonic-gate 16867c478bd9Sstevel@tonic-gate last = tabptr->zone_fs_options; 16877c478bd9Sstevel@tonic-gate for (this = last; this != NULL; this = this->zone_fsopt_next) { 16887c478bd9Sstevel@tonic-gate if (strcmp(this->zone_fsopt_opt, option) == 0) { 16897c478bd9Sstevel@tonic-gate next = this->zone_fsopt_next; 16907c478bd9Sstevel@tonic-gate if (this == tabptr->zone_fs_options) 16917c478bd9Sstevel@tonic-gate tabptr->zone_fs_options = next; 16927c478bd9Sstevel@tonic-gate else 16937c478bd9Sstevel@tonic-gate last->zone_fsopt_next = next; 16947c478bd9Sstevel@tonic-gate free(this); 16957c478bd9Sstevel@tonic-gate return (Z_OK); 16967c478bd9Sstevel@tonic-gate } else 16977c478bd9Sstevel@tonic-gate last = this; 16987c478bd9Sstevel@tonic-gate } 16997c478bd9Sstevel@tonic-gate return (Z_NO_PROPERTY_ID); 17007c478bd9Sstevel@tonic-gate } 17017c478bd9Sstevel@tonic-gate 17027c478bd9Sstevel@tonic-gate void 17037c478bd9Sstevel@tonic-gate zonecfg_free_fs_option_list(zone_fsopt_t *list) 17047c478bd9Sstevel@tonic-gate { 17057c478bd9Sstevel@tonic-gate zone_fsopt_t *this, *next; 17067c478bd9Sstevel@tonic-gate 17077c478bd9Sstevel@tonic-gate for (this = list; this != NULL; this = next) { 17087c478bd9Sstevel@tonic-gate next = this->zone_fsopt_next; 17097c478bd9Sstevel@tonic-gate free(this); 17107c478bd9Sstevel@tonic-gate } 17117c478bd9Sstevel@tonic-gate } 17127c478bd9Sstevel@tonic-gate 17137c478bd9Sstevel@tonic-gate void 17147c478bd9Sstevel@tonic-gate zonecfg_free_rctl_value_list(struct zone_rctlvaltab *valtab) 17157c478bd9Sstevel@tonic-gate { 17167c478bd9Sstevel@tonic-gate if (valtab == NULL) 17177c478bd9Sstevel@tonic-gate return; 17187c478bd9Sstevel@tonic-gate zonecfg_free_rctl_value_list(valtab->zone_rctlval_next); 17197c478bd9Sstevel@tonic-gate free(valtab); 17207c478bd9Sstevel@tonic-gate } 17217c478bd9Sstevel@tonic-gate 17227c478bd9Sstevel@tonic-gate static boolean_t 17237c478bd9Sstevel@tonic-gate match_prop(xmlNodePtr cur, const xmlChar *attr, char *user_prop) 17247c478bd9Sstevel@tonic-gate { 17257c478bd9Sstevel@tonic-gate xmlChar *gotten_prop; 17267c478bd9Sstevel@tonic-gate int prop_result; 17277c478bd9Sstevel@tonic-gate 17287c478bd9Sstevel@tonic-gate gotten_prop = xmlGetProp(cur, attr); 17297c478bd9Sstevel@tonic-gate if (gotten_prop == NULL) /* shouldn't happen */ 17307c478bd9Sstevel@tonic-gate return (B_FALSE); 17317c478bd9Sstevel@tonic-gate prop_result = xmlStrcmp(gotten_prop, (const xmlChar *) user_prop); 17327c478bd9Sstevel@tonic-gate xmlFree(gotten_prop); 17337c478bd9Sstevel@tonic-gate return ((prop_result == 0)); 17347c478bd9Sstevel@tonic-gate } 17357c478bd9Sstevel@tonic-gate 17367c478bd9Sstevel@tonic-gate static int 17377c478bd9Sstevel@tonic-gate zonecfg_delete_filesystem_core(zone_dochandle_t handle, 17387c478bd9Sstevel@tonic-gate struct zone_fstab *tabptr) 17397c478bd9Sstevel@tonic-gate { 17407c478bd9Sstevel@tonic-gate xmlNodePtr cur = handle->zone_dh_cur; 17417c478bd9Sstevel@tonic-gate boolean_t dir_match, spec_match, raw_match, type_match; 17427c478bd9Sstevel@tonic-gate 17437c478bd9Sstevel@tonic-gate for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) { 17447c478bd9Sstevel@tonic-gate if (xmlStrcmp(cur->name, DTD_ELEM_FS)) 17457c478bd9Sstevel@tonic-gate continue; 17467c478bd9Sstevel@tonic-gate dir_match = match_prop(cur, DTD_ATTR_DIR, tabptr->zone_fs_dir); 17477c478bd9Sstevel@tonic-gate spec_match = match_prop(cur, DTD_ATTR_SPECIAL, 17487c478bd9Sstevel@tonic-gate tabptr->zone_fs_special); 17497c478bd9Sstevel@tonic-gate raw_match = match_prop(cur, DTD_ATTR_RAW, 17507c478bd9Sstevel@tonic-gate tabptr->zone_fs_raw); 17517c478bd9Sstevel@tonic-gate type_match = match_prop(cur, DTD_ATTR_TYPE, 17527c478bd9Sstevel@tonic-gate tabptr->zone_fs_type); 17537c478bd9Sstevel@tonic-gate if (dir_match && spec_match && raw_match && type_match) { 17547c478bd9Sstevel@tonic-gate xmlUnlinkNode(cur); 17557c478bd9Sstevel@tonic-gate xmlFreeNode(cur); 17567c478bd9Sstevel@tonic-gate return (Z_OK); 17577c478bd9Sstevel@tonic-gate } 17587c478bd9Sstevel@tonic-gate } 17597c478bd9Sstevel@tonic-gate return (Z_NO_RESOURCE_ID); 17607c478bd9Sstevel@tonic-gate } 17617c478bd9Sstevel@tonic-gate 17627c478bd9Sstevel@tonic-gate int 17637c478bd9Sstevel@tonic-gate zonecfg_delete_filesystem(zone_dochandle_t handle, struct zone_fstab *tabptr) 17647c478bd9Sstevel@tonic-gate { 17657c478bd9Sstevel@tonic-gate int err; 17667c478bd9Sstevel@tonic-gate 17677c478bd9Sstevel@tonic-gate if (tabptr == NULL) 17687c478bd9Sstevel@tonic-gate return (Z_INVAL); 17697c478bd9Sstevel@tonic-gate 17707c478bd9Sstevel@tonic-gate if ((err = operation_prep(handle)) != Z_OK) 17717c478bd9Sstevel@tonic-gate return (err); 17727c478bd9Sstevel@tonic-gate 17737c478bd9Sstevel@tonic-gate if ((err = zonecfg_delete_filesystem_core(handle, tabptr)) != Z_OK) 17747c478bd9Sstevel@tonic-gate return (err); 17757c478bd9Sstevel@tonic-gate 17767c478bd9Sstevel@tonic-gate return (Z_OK); 17777c478bd9Sstevel@tonic-gate } 17787c478bd9Sstevel@tonic-gate 17797c478bd9Sstevel@tonic-gate int 17807c478bd9Sstevel@tonic-gate zonecfg_modify_filesystem( 17817c478bd9Sstevel@tonic-gate zone_dochandle_t handle, 17827c478bd9Sstevel@tonic-gate struct zone_fstab *oldtabptr, 17837c478bd9Sstevel@tonic-gate struct zone_fstab *newtabptr) 17847c478bd9Sstevel@tonic-gate { 17857c478bd9Sstevel@tonic-gate int err; 17867c478bd9Sstevel@tonic-gate 17877c478bd9Sstevel@tonic-gate if (oldtabptr == NULL || newtabptr == NULL) 17887c478bd9Sstevel@tonic-gate return (Z_INVAL); 17897c478bd9Sstevel@tonic-gate 17907c478bd9Sstevel@tonic-gate if ((err = operation_prep(handle)) != Z_OK) 17917c478bd9Sstevel@tonic-gate return (err); 17927c478bd9Sstevel@tonic-gate 17937c478bd9Sstevel@tonic-gate if ((err = zonecfg_delete_filesystem_core(handle, oldtabptr)) != Z_OK) 17947c478bd9Sstevel@tonic-gate return (err); 17957c478bd9Sstevel@tonic-gate 17967c478bd9Sstevel@tonic-gate if ((err = zonecfg_add_filesystem_core(handle, newtabptr)) != Z_OK) 17977c478bd9Sstevel@tonic-gate return (err); 17987c478bd9Sstevel@tonic-gate 17997c478bd9Sstevel@tonic-gate return (Z_OK); 18007c478bd9Sstevel@tonic-gate } 18017c478bd9Sstevel@tonic-gate 18027c478bd9Sstevel@tonic-gate static int 18037c478bd9Sstevel@tonic-gate zonecfg_delete_ipd_core(zone_dochandle_t handle, struct zone_fstab *tabptr) 18047c478bd9Sstevel@tonic-gate { 18057c478bd9Sstevel@tonic-gate xmlNodePtr cur = handle->zone_dh_cur; 18067c478bd9Sstevel@tonic-gate 18077c478bd9Sstevel@tonic-gate for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) { 18087c478bd9Sstevel@tonic-gate if (xmlStrcmp(cur->name, DTD_ELEM_IPD)) 18097c478bd9Sstevel@tonic-gate continue; 18107c478bd9Sstevel@tonic-gate if (match_prop(cur, DTD_ATTR_DIR, tabptr->zone_fs_dir)) { 18117c478bd9Sstevel@tonic-gate xmlUnlinkNode(cur); 18127c478bd9Sstevel@tonic-gate xmlFreeNode(cur); 18137c478bd9Sstevel@tonic-gate return (Z_OK); 18147c478bd9Sstevel@tonic-gate } 18157c478bd9Sstevel@tonic-gate } 18167c478bd9Sstevel@tonic-gate return (Z_NO_RESOURCE_ID); 18177c478bd9Sstevel@tonic-gate } 18187c478bd9Sstevel@tonic-gate 18197c478bd9Sstevel@tonic-gate int 18207c478bd9Sstevel@tonic-gate zonecfg_delete_ipd(zone_dochandle_t handle, struct zone_fstab *tabptr) 18217c478bd9Sstevel@tonic-gate { 18227c478bd9Sstevel@tonic-gate int err; 18237c478bd9Sstevel@tonic-gate 18247c478bd9Sstevel@tonic-gate if (tabptr == NULL) 18257c478bd9Sstevel@tonic-gate return (Z_INVAL); 18267c478bd9Sstevel@tonic-gate 18277c478bd9Sstevel@tonic-gate if ((err = operation_prep(handle)) != Z_OK) 18287c478bd9Sstevel@tonic-gate return (err); 18297c478bd9Sstevel@tonic-gate 18307c478bd9Sstevel@tonic-gate if ((err = zonecfg_delete_ipd_core(handle, tabptr)) != Z_OK) 18317c478bd9Sstevel@tonic-gate return (err); 18327c478bd9Sstevel@tonic-gate 18337c478bd9Sstevel@tonic-gate return (Z_OK); 18347c478bd9Sstevel@tonic-gate } 18357c478bd9Sstevel@tonic-gate 18367c478bd9Sstevel@tonic-gate int 18377c478bd9Sstevel@tonic-gate zonecfg_modify_ipd(zone_dochandle_t handle, struct zone_fstab *oldtabptr, 18387c478bd9Sstevel@tonic-gate struct zone_fstab *newtabptr) 18397c478bd9Sstevel@tonic-gate { 18407c478bd9Sstevel@tonic-gate int err; 18417c478bd9Sstevel@tonic-gate 18427c478bd9Sstevel@tonic-gate if (oldtabptr == NULL || newtabptr == NULL) 18437c478bd9Sstevel@tonic-gate return (Z_INVAL); 18447c478bd9Sstevel@tonic-gate 18457c478bd9Sstevel@tonic-gate if ((err = operation_prep(handle)) != Z_OK) 18467c478bd9Sstevel@tonic-gate return (err); 18477c478bd9Sstevel@tonic-gate 18487c478bd9Sstevel@tonic-gate if ((err = zonecfg_delete_ipd_core(handle, oldtabptr)) != Z_OK) 18497c478bd9Sstevel@tonic-gate return (err); 18507c478bd9Sstevel@tonic-gate 18517c478bd9Sstevel@tonic-gate if ((err = zonecfg_add_ipd_core(handle, newtabptr)) != Z_OK) 18527c478bd9Sstevel@tonic-gate return (err); 18537c478bd9Sstevel@tonic-gate 18547c478bd9Sstevel@tonic-gate return (Z_OK); 18557c478bd9Sstevel@tonic-gate } 18567c478bd9Sstevel@tonic-gate 18577c478bd9Sstevel@tonic-gate int 18587c478bd9Sstevel@tonic-gate zonecfg_lookup_filesystem( 18597c478bd9Sstevel@tonic-gate zone_dochandle_t handle, 18607c478bd9Sstevel@tonic-gate struct zone_fstab *tabptr) 18617c478bd9Sstevel@tonic-gate { 18627c478bd9Sstevel@tonic-gate xmlNodePtr cur, options, firstmatch; 18637c478bd9Sstevel@tonic-gate int err; 18647c478bd9Sstevel@tonic-gate char dirname[MAXPATHLEN], special[MAXPATHLEN], raw[MAXPATHLEN]; 18657c478bd9Sstevel@tonic-gate char type[FSTYPSZ]; 18667c478bd9Sstevel@tonic-gate char options_str[MAX_MNTOPT_STR]; 18677c478bd9Sstevel@tonic-gate 18687c478bd9Sstevel@tonic-gate if (tabptr == NULL) 18697c478bd9Sstevel@tonic-gate return (Z_INVAL); 18707c478bd9Sstevel@tonic-gate 18717c478bd9Sstevel@tonic-gate if ((err = operation_prep(handle)) != Z_OK) 18727c478bd9Sstevel@tonic-gate return (err); 18737c478bd9Sstevel@tonic-gate 18747c478bd9Sstevel@tonic-gate /* 18757c478bd9Sstevel@tonic-gate * Walk the list of children looking for matches on any properties 18767c478bd9Sstevel@tonic-gate * specified in the fstab parameter. If more than one resource 18777c478bd9Sstevel@tonic-gate * matches, we return Z_INSUFFICIENT_SPEC; if none match, we return 18787c478bd9Sstevel@tonic-gate * Z_NO_RESOURCE_ID. 18797c478bd9Sstevel@tonic-gate */ 18807c478bd9Sstevel@tonic-gate cur = handle->zone_dh_cur; 18817c478bd9Sstevel@tonic-gate firstmatch = NULL; 18827c478bd9Sstevel@tonic-gate for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) { 18837c478bd9Sstevel@tonic-gate if (xmlStrcmp(cur->name, DTD_ELEM_FS)) 18847c478bd9Sstevel@tonic-gate continue; 18857c478bd9Sstevel@tonic-gate if (strlen(tabptr->zone_fs_dir) > 0) { 18867c478bd9Sstevel@tonic-gate if ((fetchprop(cur, DTD_ATTR_DIR, dirname, 18877c478bd9Sstevel@tonic-gate sizeof (dirname)) == Z_OK) && 18887c478bd9Sstevel@tonic-gate (strcmp(tabptr->zone_fs_dir, dirname) == 0)) { 18897c478bd9Sstevel@tonic-gate if (firstmatch == NULL) 18907c478bd9Sstevel@tonic-gate firstmatch = cur; 18917c478bd9Sstevel@tonic-gate else 18927c478bd9Sstevel@tonic-gate return (Z_INSUFFICIENT_SPEC); 18937c478bd9Sstevel@tonic-gate } 18947c478bd9Sstevel@tonic-gate } 18957c478bd9Sstevel@tonic-gate if (strlen(tabptr->zone_fs_special) > 0) { 18967c478bd9Sstevel@tonic-gate if ((fetchprop(cur, DTD_ATTR_SPECIAL, special, 18977c478bd9Sstevel@tonic-gate sizeof (special)) == Z_OK)) { 18987c478bd9Sstevel@tonic-gate if (strcmp(tabptr->zone_fs_special, 18997c478bd9Sstevel@tonic-gate special) == 0) { 19007c478bd9Sstevel@tonic-gate if (firstmatch == NULL) 19017c478bd9Sstevel@tonic-gate firstmatch = cur; 19027c478bd9Sstevel@tonic-gate else if (firstmatch != cur) 19037c478bd9Sstevel@tonic-gate return (Z_INSUFFICIENT_SPEC); 19047c478bd9Sstevel@tonic-gate } else { 19057c478bd9Sstevel@tonic-gate /* 19067c478bd9Sstevel@tonic-gate * If another property matched but this 19077c478bd9Sstevel@tonic-gate * one doesn't then reset firstmatch. 19087c478bd9Sstevel@tonic-gate */ 19097c478bd9Sstevel@tonic-gate if (firstmatch == cur) 19107c478bd9Sstevel@tonic-gate firstmatch = NULL; 19117c478bd9Sstevel@tonic-gate } 19127c478bd9Sstevel@tonic-gate } 19137c478bd9Sstevel@tonic-gate } 19147c478bd9Sstevel@tonic-gate if (strlen(tabptr->zone_fs_raw) > 0) { 19157c478bd9Sstevel@tonic-gate if ((fetchprop(cur, DTD_ATTR_RAW, raw, 19167c478bd9Sstevel@tonic-gate sizeof (raw)) == Z_OK)) { 19177c478bd9Sstevel@tonic-gate if (strcmp(tabptr->zone_fs_raw, raw) == 0) { 19187c478bd9Sstevel@tonic-gate if (firstmatch == NULL) 19197c478bd9Sstevel@tonic-gate firstmatch = cur; 19207c478bd9Sstevel@tonic-gate else if (firstmatch != cur) 19217c478bd9Sstevel@tonic-gate return (Z_INSUFFICIENT_SPEC); 19227c478bd9Sstevel@tonic-gate } else { 19237c478bd9Sstevel@tonic-gate /* 19247c478bd9Sstevel@tonic-gate * If another property matched but this 19257c478bd9Sstevel@tonic-gate * one doesn't then reset firstmatch. 19267c478bd9Sstevel@tonic-gate */ 19277c478bd9Sstevel@tonic-gate if (firstmatch == cur) 19287c478bd9Sstevel@tonic-gate firstmatch = NULL; 19297c478bd9Sstevel@tonic-gate } 19307c478bd9Sstevel@tonic-gate } 19317c478bd9Sstevel@tonic-gate } 19327c478bd9Sstevel@tonic-gate if (strlen(tabptr->zone_fs_type) > 0) { 19337c478bd9Sstevel@tonic-gate if ((fetchprop(cur, DTD_ATTR_TYPE, type, 19347c478bd9Sstevel@tonic-gate sizeof (type)) == Z_OK)) { 19357c478bd9Sstevel@tonic-gate if (strcmp(tabptr->zone_fs_type, type) == 0) { 19367c478bd9Sstevel@tonic-gate if (firstmatch == NULL) 19377c478bd9Sstevel@tonic-gate firstmatch = cur; 19387c478bd9Sstevel@tonic-gate else if (firstmatch != cur) 19397c478bd9Sstevel@tonic-gate return (Z_INSUFFICIENT_SPEC); 19407c478bd9Sstevel@tonic-gate } else { 19417c478bd9Sstevel@tonic-gate /* 19427c478bd9Sstevel@tonic-gate * If another property matched but this 19437c478bd9Sstevel@tonic-gate * one doesn't then reset firstmatch. 19447c478bd9Sstevel@tonic-gate */ 19457c478bd9Sstevel@tonic-gate if (firstmatch == cur) 19467c478bd9Sstevel@tonic-gate firstmatch = NULL; 19477c478bd9Sstevel@tonic-gate } 19487c478bd9Sstevel@tonic-gate } 19497c478bd9Sstevel@tonic-gate } 19507c478bd9Sstevel@tonic-gate } 19517c478bd9Sstevel@tonic-gate 19527c478bd9Sstevel@tonic-gate if (firstmatch == NULL) 19537c478bd9Sstevel@tonic-gate return (Z_NO_RESOURCE_ID); 19547c478bd9Sstevel@tonic-gate 19557c478bd9Sstevel@tonic-gate cur = firstmatch; 19567c478bd9Sstevel@tonic-gate 19577c478bd9Sstevel@tonic-gate if ((err = fetchprop(cur, DTD_ATTR_DIR, tabptr->zone_fs_dir, 19587c478bd9Sstevel@tonic-gate sizeof (tabptr->zone_fs_dir))) != Z_OK) 19597c478bd9Sstevel@tonic-gate return (err); 19607c478bd9Sstevel@tonic-gate 19617c478bd9Sstevel@tonic-gate if ((err = fetchprop(cur, DTD_ATTR_SPECIAL, tabptr->zone_fs_special, 19627c478bd9Sstevel@tonic-gate sizeof (tabptr->zone_fs_special))) != Z_OK) 19637c478bd9Sstevel@tonic-gate return (err); 19647c478bd9Sstevel@tonic-gate 19657c478bd9Sstevel@tonic-gate if ((err = fetchprop(cur, DTD_ATTR_RAW, tabptr->zone_fs_raw, 19667c478bd9Sstevel@tonic-gate sizeof (tabptr->zone_fs_raw))) != Z_OK) 19677c478bd9Sstevel@tonic-gate return (err); 19687c478bd9Sstevel@tonic-gate 19697c478bd9Sstevel@tonic-gate if ((err = fetchprop(cur, DTD_ATTR_TYPE, tabptr->zone_fs_type, 19707c478bd9Sstevel@tonic-gate sizeof (tabptr->zone_fs_type))) != Z_OK) 19717c478bd9Sstevel@tonic-gate return (err); 19727c478bd9Sstevel@tonic-gate 19737c478bd9Sstevel@tonic-gate /* options are optional */ 19747c478bd9Sstevel@tonic-gate tabptr->zone_fs_options = NULL; 19757c478bd9Sstevel@tonic-gate for (options = cur->xmlChildrenNode; options != NULL; 19767c478bd9Sstevel@tonic-gate options = options->next) { 19777c478bd9Sstevel@tonic-gate if ((fetchprop(options, DTD_ATTR_NAME, options_str, 19787c478bd9Sstevel@tonic-gate sizeof (options_str)) != Z_OK)) 19797c478bd9Sstevel@tonic-gate break; 19807c478bd9Sstevel@tonic-gate if (zonecfg_add_fs_option(tabptr, options_str) != Z_OK) 19817c478bd9Sstevel@tonic-gate break; 19827c478bd9Sstevel@tonic-gate } 19837c478bd9Sstevel@tonic-gate return (Z_OK); 19847c478bd9Sstevel@tonic-gate } 19857c478bd9Sstevel@tonic-gate 19867c478bd9Sstevel@tonic-gate int 19877c478bd9Sstevel@tonic-gate zonecfg_lookup_ipd(zone_dochandle_t handle, struct zone_fstab *tabptr) 19887c478bd9Sstevel@tonic-gate { 19897c478bd9Sstevel@tonic-gate xmlNodePtr cur, match; 19907c478bd9Sstevel@tonic-gate int err; 19917c478bd9Sstevel@tonic-gate char dirname[MAXPATHLEN]; 19927c478bd9Sstevel@tonic-gate 19937c478bd9Sstevel@tonic-gate if (tabptr == NULL) 19947c478bd9Sstevel@tonic-gate return (Z_INVAL); 19957c478bd9Sstevel@tonic-gate 19967c478bd9Sstevel@tonic-gate if ((err = operation_prep(handle)) != Z_OK) 19977c478bd9Sstevel@tonic-gate return (err); 19987c478bd9Sstevel@tonic-gate 19997c478bd9Sstevel@tonic-gate /* 20007c478bd9Sstevel@tonic-gate * General algorithm: 20017c478bd9Sstevel@tonic-gate * Walk the list of children looking for matches on any properties 20027c478bd9Sstevel@tonic-gate * specified in the fstab parameter. If more than one resource 20037c478bd9Sstevel@tonic-gate * matches, we return Z_INSUFFICIENT_SPEC; if none match, we return 20047c478bd9Sstevel@tonic-gate * Z_NO_RESOURCE_ID. 20057c478bd9Sstevel@tonic-gate */ 20067c478bd9Sstevel@tonic-gate cur = handle->zone_dh_cur; 20077c478bd9Sstevel@tonic-gate match = NULL; 20087c478bd9Sstevel@tonic-gate for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) { 20097c478bd9Sstevel@tonic-gate if (xmlStrcmp(cur->name, DTD_ELEM_IPD)) 20107c478bd9Sstevel@tonic-gate continue; 20117c478bd9Sstevel@tonic-gate if (strlen(tabptr->zone_fs_dir) > 0) { 20127c478bd9Sstevel@tonic-gate if ((fetchprop(cur, DTD_ATTR_DIR, dirname, 20137c478bd9Sstevel@tonic-gate sizeof (dirname)) == Z_OK) && 20147c478bd9Sstevel@tonic-gate (strcmp(tabptr->zone_fs_dir, dirname) == 0)) { 20157c478bd9Sstevel@tonic-gate if (match == NULL) 20167c478bd9Sstevel@tonic-gate match = cur; 20177c478bd9Sstevel@tonic-gate else 20187c478bd9Sstevel@tonic-gate return (Z_INSUFFICIENT_SPEC); 20197c478bd9Sstevel@tonic-gate } 20207c478bd9Sstevel@tonic-gate } 20217c478bd9Sstevel@tonic-gate } 20227c478bd9Sstevel@tonic-gate 20237c478bd9Sstevel@tonic-gate if (match == NULL) 20247c478bd9Sstevel@tonic-gate return (Z_NO_RESOURCE_ID); 20257c478bd9Sstevel@tonic-gate 20267c478bd9Sstevel@tonic-gate cur = match; 20277c478bd9Sstevel@tonic-gate 20287c478bd9Sstevel@tonic-gate if ((err = fetchprop(cur, DTD_ATTR_DIR, tabptr->zone_fs_dir, 20297c478bd9Sstevel@tonic-gate sizeof (tabptr->zone_fs_dir))) != Z_OK) 20307c478bd9Sstevel@tonic-gate return (err); 20317c478bd9Sstevel@tonic-gate 20327c478bd9Sstevel@tonic-gate return (Z_OK); 20337c478bd9Sstevel@tonic-gate } 20347c478bd9Sstevel@tonic-gate 20357c478bd9Sstevel@tonic-gate /* 20367c478bd9Sstevel@tonic-gate * Compare two IP addresses in string form. Allow for the possibility that 20377c478bd9Sstevel@tonic-gate * one might have "/<prefix-length>" at the end: allow a match on just the 20387c478bd9Sstevel@tonic-gate * IP address (or host name) part. 20397c478bd9Sstevel@tonic-gate */ 20407c478bd9Sstevel@tonic-gate 20417c478bd9Sstevel@tonic-gate boolean_t 20427c478bd9Sstevel@tonic-gate zonecfg_same_net_address(char *a1, char *a2) 20437c478bd9Sstevel@tonic-gate { 20447c478bd9Sstevel@tonic-gate char *slashp, *slashp1, *slashp2; 20457c478bd9Sstevel@tonic-gate int result; 20467c478bd9Sstevel@tonic-gate 20477c478bd9Sstevel@tonic-gate if (strcmp(a1, a2) == 0) 20487c478bd9Sstevel@tonic-gate return (B_TRUE); 20497c478bd9Sstevel@tonic-gate 20507c478bd9Sstevel@tonic-gate /* 20517c478bd9Sstevel@tonic-gate * If neither has a slash or both do, they need to match to be 20527c478bd9Sstevel@tonic-gate * considered the same, but they did not match above, so fail. 20537c478bd9Sstevel@tonic-gate */ 20547c478bd9Sstevel@tonic-gate slashp1 = strchr(a1, '/'); 20557c478bd9Sstevel@tonic-gate slashp2 = strchr(a2, '/'); 20567c478bd9Sstevel@tonic-gate if ((slashp1 == NULL && slashp2 == NULL) || 20577c478bd9Sstevel@tonic-gate (slashp1 != NULL && slashp2 != NULL)) 20587c478bd9Sstevel@tonic-gate return (B_FALSE); 20597c478bd9Sstevel@tonic-gate 20607c478bd9Sstevel@tonic-gate /* 20617c478bd9Sstevel@tonic-gate * Only one had a slash: pick that one, zero out the slash, compare 20627c478bd9Sstevel@tonic-gate * the "address only" strings, restore the slash, and return the 20637c478bd9Sstevel@tonic-gate * result of the comparison. 20647c478bd9Sstevel@tonic-gate */ 20657c478bd9Sstevel@tonic-gate slashp = (slashp1 == NULL) ? slashp2 : slashp1; 20667c478bd9Sstevel@tonic-gate *slashp = '\0'; 20677c478bd9Sstevel@tonic-gate result = strcmp(a1, a2); 20687c478bd9Sstevel@tonic-gate *slashp = '/'; 20697c478bd9Sstevel@tonic-gate return ((result == 0)); 20707c478bd9Sstevel@tonic-gate } 20717c478bd9Sstevel@tonic-gate 20727c478bd9Sstevel@tonic-gate int 20737c478bd9Sstevel@tonic-gate zonecfg_valid_net_address(char *address, struct lifreq *lifr) 20747c478bd9Sstevel@tonic-gate { 20757c478bd9Sstevel@tonic-gate struct sockaddr_in *sin4; 20767c478bd9Sstevel@tonic-gate struct sockaddr_in6 *sin6; 20777c478bd9Sstevel@tonic-gate struct addrinfo hints, *result; 20787c478bd9Sstevel@tonic-gate char *slashp = strchr(address, '/'); 20797c478bd9Sstevel@tonic-gate 20807c478bd9Sstevel@tonic-gate bzero(lifr, sizeof (struct lifreq)); 20817c478bd9Sstevel@tonic-gate sin4 = (struct sockaddr_in *)&lifr->lifr_addr; 20827c478bd9Sstevel@tonic-gate sin6 = (struct sockaddr_in6 *)&lifr->lifr_addr; 20837c478bd9Sstevel@tonic-gate if (slashp != NULL) 20847c478bd9Sstevel@tonic-gate *slashp = '\0'; 20857c478bd9Sstevel@tonic-gate if (inet_pton(AF_INET, address, &sin4->sin_addr) == 1) { 20867c478bd9Sstevel@tonic-gate sin4->sin_family = AF_INET; 20877c478bd9Sstevel@tonic-gate } else if (inet_pton(AF_INET6, address, &sin6->sin6_addr) == 1) { 20887c478bd9Sstevel@tonic-gate if (slashp == NULL) 20897c478bd9Sstevel@tonic-gate return (Z_IPV6_ADDR_PREFIX_LEN); 20907c478bd9Sstevel@tonic-gate sin6->sin6_family = AF_INET6; 20917c478bd9Sstevel@tonic-gate } else { 20927c478bd9Sstevel@tonic-gate /* "address" may be a host name */ 20937c478bd9Sstevel@tonic-gate (void) memset(&hints, 0, sizeof (hints)); 20947c478bd9Sstevel@tonic-gate hints.ai_family = PF_INET; 20957c478bd9Sstevel@tonic-gate if (getaddrinfo(address, NULL, &hints, &result) != 0) 20967c478bd9Sstevel@tonic-gate return (Z_BOGUS_ADDRESS); 20977c478bd9Sstevel@tonic-gate sin4->sin_family = result->ai_family; 2098a1be23daSdp 20997c478bd9Sstevel@tonic-gate (void) memcpy(&sin4->sin_addr, 21007c478bd9Sstevel@tonic-gate /* LINTED E_BAD_PTR_CAST_ALIGN */ 21017c478bd9Sstevel@tonic-gate &((struct sockaddr_in *)result->ai_addr)->sin_addr, 21027c478bd9Sstevel@tonic-gate sizeof (struct in_addr)); 2103a1be23daSdp 21047c478bd9Sstevel@tonic-gate freeaddrinfo(result); 21057c478bd9Sstevel@tonic-gate } 21067c478bd9Sstevel@tonic-gate return (Z_OK); 21077c478bd9Sstevel@tonic-gate } 21087c478bd9Sstevel@tonic-gate 2109f4b3ec61Sdh155122 boolean_t 2110f4b3ec61Sdh155122 zonecfg_ifname_exists(sa_family_t af, char *ifname) 2111f4b3ec61Sdh155122 { 2112f4b3ec61Sdh155122 struct lifreq lifr; 2113f4b3ec61Sdh155122 int so; 2114f4b3ec61Sdh155122 int save_errno; 2115f4b3ec61Sdh155122 2116f4b3ec61Sdh155122 (void) memset(&lifr, 0, sizeof (lifr)); 2117f4b3ec61Sdh155122 (void) strlcpy(lifr.lifr_name, ifname, sizeof (lifr.lifr_name)); 2118f4b3ec61Sdh155122 lifr.lifr_addr.ss_family = af; 2119f4b3ec61Sdh155122 if ((so = socket(af, SOCK_DGRAM, 0)) < 0) { 2120f4b3ec61Sdh155122 /* Odd - can't tell if the ifname exists */ 2121f4b3ec61Sdh155122 return (B_FALSE); 2122f4b3ec61Sdh155122 } 2123f4b3ec61Sdh155122 if (ioctl(so, SIOCGLIFFLAGS, (caddr_t)&lifr) < 0) { 2124f4b3ec61Sdh155122 save_errno = errno; 2125f4b3ec61Sdh155122 (void) close(so); 2126f4b3ec61Sdh155122 errno = save_errno; 2127f4b3ec61Sdh155122 return (B_FALSE); 2128f4b3ec61Sdh155122 } 2129f4b3ec61Sdh155122 (void) close(so); 2130f4b3ec61Sdh155122 return (B_TRUE); 2131f4b3ec61Sdh155122 } 2132f4b3ec61Sdh155122 2133a8053275Sjv227347 /* 2134*34eb7836Sjv227347 * Determines whether there is a net resource with the physical interface, IP 2135*34eb7836Sjv227347 * address, and default router specified by 'tabptr' in the zone configuration 2136*34eb7836Sjv227347 * to which 'handle' refers. 'tabptr' must have an interface, an address, a 2137*34eb7836Sjv227347 * default router, or a combination of the three. This function returns Z_OK 2138*34eb7836Sjv227347 * iff there is exactly one net resource matching the query specified by 2139*34eb7836Sjv227347 * 'tabptr'. The function returns Z_INSUFFICIENT_SPEC if there are multiple 2140*34eb7836Sjv227347 * matches or 'tabptr' does not specify a physical interface, address, or 2141*34eb7836Sjv227347 * default router. The function returns Z_NO_RESOURCE_ID if are no matches. 2142a8053275Sjv227347 * 2143a8053275Sjv227347 * Errors might also be returned if the entry that exactly matches the 2144a8053275Sjv227347 * query lacks critical network resource information. 2145a8053275Sjv227347 * 2146*34eb7836Sjv227347 * If there is a single match, then the matching entry's physical interface, IP 2147*34eb7836Sjv227347 * address, and default router information are stored in 'tabptr'. 2148a8053275Sjv227347 */ 21497c478bd9Sstevel@tonic-gate int 21507c478bd9Sstevel@tonic-gate zonecfg_lookup_nwif(zone_dochandle_t handle, struct zone_nwiftab *tabptr) 21517c478bd9Sstevel@tonic-gate { 2152a8053275Sjv227347 xmlNodePtr cur; 2153a8053275Sjv227347 xmlNodePtr firstmatch; 21547c478bd9Sstevel@tonic-gate int err; 2155a8053275Sjv227347 char address[INET6_ADDRSTRLEN]; 2156a8053275Sjv227347 char physical[LIFNAMSIZ]; 2157a8053275Sjv227347 size_t addrspec; /* nonzero if tabptr has IP addr */ 2158a8053275Sjv227347 size_t physspec; /* nonzero if tabptr has interface */ 2159*34eb7836Sjv227347 size_t defrouterspec; /* nonzero if tabptr has def. router */ 21607c478bd9Sstevel@tonic-gate 21617c478bd9Sstevel@tonic-gate if (tabptr == NULL) 21627c478bd9Sstevel@tonic-gate return (Z_INVAL); 21637c478bd9Sstevel@tonic-gate 2164a8053275Sjv227347 /* 2165*34eb7836Sjv227347 * Determine the fields that will be searched. There must be at least 2166*34eb7836Sjv227347 * one. 2167*34eb7836Sjv227347 * 2168*34eb7836Sjv227347 * zone_nwif_address, zone_nwif_physical, and zone_nwif_defrouter are 2169*34eb7836Sjv227347 * arrays, so no NULL checks are necessary. 2170a8053275Sjv227347 */ 2171a8053275Sjv227347 addrspec = strlen(tabptr->zone_nwif_address); 2172a8053275Sjv227347 physspec = strlen(tabptr->zone_nwif_physical); 2173*34eb7836Sjv227347 defrouterspec = strlen(tabptr->zone_nwif_defrouter); 2174*34eb7836Sjv227347 if (addrspec == 0 && physspec == 0 && defrouterspec == 0) 2175*34eb7836Sjv227347 return (Z_INSUFFICIENT_SPEC); 2176a8053275Sjv227347 21777c478bd9Sstevel@tonic-gate if ((err = operation_prep(handle)) != Z_OK) 21787c478bd9Sstevel@tonic-gate return (err); 21797c478bd9Sstevel@tonic-gate 2180*34eb7836Sjv227347 /* 2181*34eb7836Sjv227347 * Iterate over the configuration's elements and look for net elements 2182*34eb7836Sjv227347 * that match the query. 2183*34eb7836Sjv227347 */ 21847c478bd9Sstevel@tonic-gate firstmatch = NULL; 2185a8053275Sjv227347 cur = handle->zone_dh_cur; 21867c478bd9Sstevel@tonic-gate for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) { 2187a8053275Sjv227347 /* Skip non-net elements */ 21887c478bd9Sstevel@tonic-gate if (xmlStrcmp(cur->name, DTD_ELEM_NET)) 21897c478bd9Sstevel@tonic-gate continue; 2190a8053275Sjv227347 2191a8053275Sjv227347 /* 2192*34eb7836Sjv227347 * If any relevant fields don't match the query, then skip 2193*34eb7836Sjv227347 * the current net element. 2194a8053275Sjv227347 */ 2195*34eb7836Sjv227347 if (physspec != 0 && (fetchprop(cur, DTD_ATTR_PHYSICAL, 2196*34eb7836Sjv227347 physical, sizeof (physical)) != Z_OK || 2197*34eb7836Sjv227347 strcmp(tabptr->zone_nwif_physical, physical) != 0)) 2198*34eb7836Sjv227347 continue; 2199*34eb7836Sjv227347 if (addrspec != 0 && (fetchprop(cur, DTD_ATTR_ADDRESS, address, 2200*34eb7836Sjv227347 sizeof (address)) != Z_OK || 2201*34eb7836Sjv227347 !zonecfg_same_net_address(tabptr->zone_nwif_address, 2202*34eb7836Sjv227347 address))) 2203*34eb7836Sjv227347 continue; 2204*34eb7836Sjv227347 if (defrouterspec != 0 && (fetchprop(cur, DTD_ATTR_DEFROUTER, 2205*34eb7836Sjv227347 address, sizeof (address)) != Z_OK || 2206*34eb7836Sjv227347 !zonecfg_same_net_address(tabptr->zone_nwif_defrouter, 2207*34eb7836Sjv227347 address))) 2208*34eb7836Sjv227347 continue; 2209*34eb7836Sjv227347 22107c478bd9Sstevel@tonic-gate /* 2211*34eb7836Sjv227347 * The current net element matches the query. Select it if 2212*34eb7836Sjv227347 * it's the first match; otherwise, abort the search. 2213a8053275Sjv227347 */ 2214a8053275Sjv227347 if (firstmatch == NULL) 2215a8053275Sjv227347 firstmatch = cur; 2216a8053275Sjv227347 else 2217a8053275Sjv227347 return (Z_INSUFFICIENT_SPEC); 22187c478bd9Sstevel@tonic-gate } 22197c478bd9Sstevel@tonic-gate if (firstmatch == NULL) 22207c478bd9Sstevel@tonic-gate return (Z_NO_RESOURCE_ID); 22217c478bd9Sstevel@tonic-gate 22227c478bd9Sstevel@tonic-gate cur = firstmatch; 22237c478bd9Sstevel@tonic-gate 22247c478bd9Sstevel@tonic-gate if ((err = fetchprop(cur, DTD_ATTR_PHYSICAL, tabptr->zone_nwif_physical, 22257c478bd9Sstevel@tonic-gate sizeof (tabptr->zone_nwif_physical))) != Z_OK) 22267c478bd9Sstevel@tonic-gate return (err); 22277c478bd9Sstevel@tonic-gate 22287c478bd9Sstevel@tonic-gate if ((err = fetchprop(cur, DTD_ATTR_ADDRESS, tabptr->zone_nwif_address, 22297c478bd9Sstevel@tonic-gate sizeof (tabptr->zone_nwif_address))) != Z_OK) 22307c478bd9Sstevel@tonic-gate return (err); 22317c478bd9Sstevel@tonic-gate 2232de860bd9Sgfaden if ((err = fetchprop(cur, DTD_ATTR_DEFROUTER, 2233de860bd9Sgfaden tabptr->zone_nwif_defrouter, 2234de860bd9Sgfaden sizeof (tabptr->zone_nwif_defrouter))) != Z_OK) 2235de860bd9Sgfaden return (err); 2236de860bd9Sgfaden 22377c478bd9Sstevel@tonic-gate return (Z_OK); 22387c478bd9Sstevel@tonic-gate } 22397c478bd9Sstevel@tonic-gate 22407c478bd9Sstevel@tonic-gate static int 22417c478bd9Sstevel@tonic-gate zonecfg_add_nwif_core(zone_dochandle_t handle, struct zone_nwiftab *tabptr) 22427c478bd9Sstevel@tonic-gate { 22437c478bd9Sstevel@tonic-gate xmlNodePtr newnode, cur = handle->zone_dh_cur; 22447c478bd9Sstevel@tonic-gate int err; 22457c478bd9Sstevel@tonic-gate 22467c478bd9Sstevel@tonic-gate newnode = xmlNewTextChild(cur, NULL, DTD_ELEM_NET, NULL); 2247a1be23daSdp if ((err = newprop(newnode, DTD_ATTR_ADDRESS, 22487c478bd9Sstevel@tonic-gate tabptr->zone_nwif_address)) != Z_OK) 22497c478bd9Sstevel@tonic-gate return (err); 2250a1be23daSdp if ((err = newprop(newnode, DTD_ATTR_PHYSICAL, 22517c478bd9Sstevel@tonic-gate tabptr->zone_nwif_physical)) != Z_OK) 22527c478bd9Sstevel@tonic-gate return (err); 225344ce818bSjparcel /* 225444ce818bSjparcel * Do not add this property when it is not set, for backwards 225544ce818bSjparcel * compatibility and because it is optional. 225644ce818bSjparcel */ 225744ce818bSjparcel if ((strlen(tabptr->zone_nwif_defrouter) > 0) && 225844ce818bSjparcel ((err = newprop(newnode, DTD_ATTR_DEFROUTER, 225944ce818bSjparcel tabptr->zone_nwif_defrouter)) != Z_OK)) 2260de860bd9Sgfaden return (err); 22617c478bd9Sstevel@tonic-gate return (Z_OK); 22627c478bd9Sstevel@tonic-gate } 22637c478bd9Sstevel@tonic-gate 22647c478bd9Sstevel@tonic-gate int 22657c478bd9Sstevel@tonic-gate zonecfg_add_nwif(zone_dochandle_t handle, struct zone_nwiftab *tabptr) 22667c478bd9Sstevel@tonic-gate { 22677c478bd9Sstevel@tonic-gate int err; 22687c478bd9Sstevel@tonic-gate 22697c478bd9Sstevel@tonic-gate if (tabptr == NULL) 22707c478bd9Sstevel@tonic-gate return (Z_INVAL); 22717c478bd9Sstevel@tonic-gate 22727c478bd9Sstevel@tonic-gate if ((err = operation_prep(handle)) != Z_OK) 22737c478bd9Sstevel@tonic-gate return (err); 22747c478bd9Sstevel@tonic-gate 22757c478bd9Sstevel@tonic-gate if ((err = zonecfg_add_nwif_core(handle, tabptr)) != Z_OK) 22767c478bd9Sstevel@tonic-gate return (err); 22777c478bd9Sstevel@tonic-gate 22787c478bd9Sstevel@tonic-gate return (Z_OK); 22797c478bd9Sstevel@tonic-gate } 22807c478bd9Sstevel@tonic-gate 22817c478bd9Sstevel@tonic-gate static int 22827c478bd9Sstevel@tonic-gate zonecfg_delete_nwif_core(zone_dochandle_t handle, struct zone_nwiftab *tabptr) 22837c478bd9Sstevel@tonic-gate { 22847c478bd9Sstevel@tonic-gate xmlNodePtr cur = handle->zone_dh_cur; 22857c478bd9Sstevel@tonic-gate boolean_t addr_match, phys_match; 22867c478bd9Sstevel@tonic-gate 22877c478bd9Sstevel@tonic-gate for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) { 22887c478bd9Sstevel@tonic-gate if (xmlStrcmp(cur->name, DTD_ELEM_NET)) 22897c478bd9Sstevel@tonic-gate continue; 22907c478bd9Sstevel@tonic-gate 22917c478bd9Sstevel@tonic-gate addr_match = match_prop(cur, DTD_ATTR_ADDRESS, 22927c478bd9Sstevel@tonic-gate tabptr->zone_nwif_address); 22937c478bd9Sstevel@tonic-gate phys_match = match_prop(cur, DTD_ATTR_PHYSICAL, 22947c478bd9Sstevel@tonic-gate tabptr->zone_nwif_physical); 22957c478bd9Sstevel@tonic-gate 22967c478bd9Sstevel@tonic-gate if (addr_match && phys_match) { 22977c478bd9Sstevel@tonic-gate xmlUnlinkNode(cur); 22987c478bd9Sstevel@tonic-gate xmlFreeNode(cur); 22997c478bd9Sstevel@tonic-gate return (Z_OK); 23007c478bd9Sstevel@tonic-gate } 23017c478bd9Sstevel@tonic-gate } 23027c478bd9Sstevel@tonic-gate return (Z_NO_RESOURCE_ID); 23037c478bd9Sstevel@tonic-gate } 23047c478bd9Sstevel@tonic-gate 23057c478bd9Sstevel@tonic-gate int 23067c478bd9Sstevel@tonic-gate zonecfg_delete_nwif(zone_dochandle_t handle, struct zone_nwiftab *tabptr) 23077c478bd9Sstevel@tonic-gate { 23087c478bd9Sstevel@tonic-gate int err; 23097c478bd9Sstevel@tonic-gate 23107c478bd9Sstevel@tonic-gate if (tabptr == NULL) 23117c478bd9Sstevel@tonic-gate return (Z_INVAL); 23127c478bd9Sstevel@tonic-gate 23137c478bd9Sstevel@tonic-gate if ((err = operation_prep(handle)) != Z_OK) 23147c478bd9Sstevel@tonic-gate return (err); 23157c478bd9Sstevel@tonic-gate 23167c478bd9Sstevel@tonic-gate if ((err = zonecfg_delete_nwif_core(handle, tabptr)) != Z_OK) 23177c478bd9Sstevel@tonic-gate return (err); 23187c478bd9Sstevel@tonic-gate 23197c478bd9Sstevel@tonic-gate return (Z_OK); 23207c478bd9Sstevel@tonic-gate } 23217c478bd9Sstevel@tonic-gate 23227c478bd9Sstevel@tonic-gate int 23237c478bd9Sstevel@tonic-gate zonecfg_modify_nwif( 23247c478bd9Sstevel@tonic-gate zone_dochandle_t handle, 23257c478bd9Sstevel@tonic-gate struct zone_nwiftab *oldtabptr, 23267c478bd9Sstevel@tonic-gate struct zone_nwiftab *newtabptr) 23277c478bd9Sstevel@tonic-gate { 23287c478bd9Sstevel@tonic-gate int err; 23297c478bd9Sstevel@tonic-gate 23307c478bd9Sstevel@tonic-gate if (oldtabptr == NULL || newtabptr == NULL) 23317c478bd9Sstevel@tonic-gate return (Z_INVAL); 23327c478bd9Sstevel@tonic-gate 23337c478bd9Sstevel@tonic-gate if ((err = operation_prep(handle)) != Z_OK) 23347c478bd9Sstevel@tonic-gate return (err); 23357c478bd9Sstevel@tonic-gate 23367c478bd9Sstevel@tonic-gate if ((err = zonecfg_delete_nwif_core(handle, oldtabptr)) != Z_OK) 23377c478bd9Sstevel@tonic-gate return (err); 23387c478bd9Sstevel@tonic-gate 23397c478bd9Sstevel@tonic-gate if ((err = zonecfg_add_nwif_core(handle, newtabptr)) != Z_OK) 23407c478bd9Sstevel@tonic-gate return (err); 23417c478bd9Sstevel@tonic-gate 23427c478bd9Sstevel@tonic-gate return (Z_OK); 23437c478bd9Sstevel@tonic-gate } 23447c478bd9Sstevel@tonic-gate 23455679c89fSjv227347 /* 23465679c89fSjv227347 * Gets the zone hostid string stored in the specified zone configuration 23475679c89fSjv227347 * document. This function returns Z_OK on success. Z_BAD_PROPERTY is returned 23485679c89fSjv227347 * if the config file doesn't specify a hostid or if the hostid is blank. 23495679c89fSjv227347 * 23505679c89fSjv227347 * Note that buflen should be at least HW_HOSTID_LEN. 23515679c89fSjv227347 */ 23525679c89fSjv227347 int 23535679c89fSjv227347 zonecfg_get_hostid(zone_dochandle_t handle, char *bufp, size_t buflen) 23545679c89fSjv227347 { 23555679c89fSjv227347 int err; 23565679c89fSjv227347 23575679c89fSjv227347 if ((err = getrootattr(handle, DTD_ATTR_HOSTID, bufp, buflen)) != Z_OK) 23585679c89fSjv227347 return (err); 23595679c89fSjv227347 if (bufp[0] == '\0') 23605679c89fSjv227347 return (Z_BAD_PROPERTY); 23615679c89fSjv227347 return (Z_OK); 23625679c89fSjv227347 } 23635679c89fSjv227347 23645679c89fSjv227347 /* 23655679c89fSjv227347 * Sets the hostid string in the specified zone config document to the given 23665679c89fSjv227347 * string value. If 'hostidp' is NULL, then the config document's hostid 23675679c89fSjv227347 * attribute is cleared. Non-NULL hostids are validated. This function returns 23685679c89fSjv227347 * Z_OK on success. Any other return value indicates failure. 23695679c89fSjv227347 */ 23705679c89fSjv227347 int 23715679c89fSjv227347 zonecfg_set_hostid(zone_dochandle_t handle, const char *hostidp) 23725679c89fSjv227347 { 23735679c89fSjv227347 int err; 23745679c89fSjv227347 23755679c89fSjv227347 /* 23765679c89fSjv227347 * A NULL hostid string is interpreted as a request to clear the 23775679c89fSjv227347 * hostid. 23785679c89fSjv227347 */ 23795679c89fSjv227347 if (hostidp == NULL || (err = zonecfg_valid_hostid(hostidp)) == Z_OK) 23805679c89fSjv227347 return (setrootattr(handle, DTD_ATTR_HOSTID, hostidp)); 23815679c89fSjv227347 return (err); 23825679c89fSjv227347 } 23835679c89fSjv227347 23845679c89fSjv227347 /* 23855679c89fSjv227347 * Determines if the specified string is a valid hostid string. This function 23865679c89fSjv227347 * returns Z_OK if the string is a valid hostid string. It returns Z_INVAL if 23875679c89fSjv227347 * 'hostidp' is NULL, Z_TOO_BIG if 'hostidp' refers to a string buffer 23885679c89fSjv227347 * containing a hex string with more than 8 digits, and Z_HOSTID_FUBAR if the 23895679c89fSjv227347 * string has an invalid format. 23905679c89fSjv227347 */ 23915679c89fSjv227347 int 23925679c89fSjv227347 zonecfg_valid_hostid(const char *hostidp) 23935679c89fSjv227347 { 23945679c89fSjv227347 char *currentp; 23955679c89fSjv227347 u_longlong_t hostidval; 23965679c89fSjv227347 size_t len; 23975679c89fSjv227347 23985679c89fSjv227347 if (hostidp == NULL) 23995679c89fSjv227347 return (Z_INVAL); 24005679c89fSjv227347 24015679c89fSjv227347 /* Empty strings and strings with whitespace are invalid. */ 24025679c89fSjv227347 if (*hostidp == '\0') 24035679c89fSjv227347 return (Z_HOSTID_FUBAR); 24045679c89fSjv227347 for (currentp = (char *)hostidp; *currentp != '\0'; ++currentp) { 24055679c89fSjv227347 if (isspace(*currentp)) 24065679c89fSjv227347 return (Z_HOSTID_FUBAR); 24075679c89fSjv227347 } 24085679c89fSjv227347 len = (size_t)(currentp - hostidp); 24095679c89fSjv227347 24105679c89fSjv227347 /* 24115679c89fSjv227347 * The caller might pass a hostid that is larger than the maximum 24125679c89fSjv227347 * unsigned 32-bit integral value. Check for this! Also, make sure 24135679c89fSjv227347 * that the whole string is converted (this helps us find illegal 24145679c89fSjv227347 * characters) and that the whole string fits within a buffer of size 24155679c89fSjv227347 * HW_HOSTID_LEN. 24165679c89fSjv227347 */ 24175679c89fSjv227347 currentp = (char *)hostidp; 24185679c89fSjv227347 if (strncmp(hostidp, "0x", 2) == 0 || strncmp(hostidp, "0X", 2) == 0) 24195679c89fSjv227347 currentp += 2; 24205679c89fSjv227347 hostidval = strtoull(currentp, ¤tp, 16); 24215679c89fSjv227347 if ((size_t)(currentp - hostidp) >= HW_HOSTID_LEN) 24225679c89fSjv227347 return (Z_TOO_BIG); 24235679c89fSjv227347 if (hostidval > UINT_MAX || hostidval == HW_INVALID_HOSTID || 24245679c89fSjv227347 currentp != hostidp + len) 24255679c89fSjv227347 return (Z_HOSTID_FUBAR); 24265679c89fSjv227347 return (Z_OK); 24275679c89fSjv227347 } 24285679c89fSjv227347 24297c478bd9Sstevel@tonic-gate int 24307c478bd9Sstevel@tonic-gate zonecfg_lookup_dev(zone_dochandle_t handle, struct zone_devtab *tabptr) 24317c478bd9Sstevel@tonic-gate { 24327c478bd9Sstevel@tonic-gate xmlNodePtr cur, firstmatch; 24337c478bd9Sstevel@tonic-gate int err; 24347c478bd9Sstevel@tonic-gate char match[MAXPATHLEN]; 24357c478bd9Sstevel@tonic-gate 24367c478bd9Sstevel@tonic-gate if (tabptr == NULL) 24377c478bd9Sstevel@tonic-gate return (Z_INVAL); 24387c478bd9Sstevel@tonic-gate 24397c478bd9Sstevel@tonic-gate if ((err = operation_prep(handle)) != Z_OK) 24407c478bd9Sstevel@tonic-gate return (err); 24417c478bd9Sstevel@tonic-gate 24427c478bd9Sstevel@tonic-gate cur = handle->zone_dh_cur; 24437c478bd9Sstevel@tonic-gate firstmatch = NULL; 24447c478bd9Sstevel@tonic-gate for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) { 24457c478bd9Sstevel@tonic-gate if (xmlStrcmp(cur->name, DTD_ELEM_DEVICE)) 24467c478bd9Sstevel@tonic-gate continue; 24477c478bd9Sstevel@tonic-gate if (strlen(tabptr->zone_dev_match) == 0) 24487c478bd9Sstevel@tonic-gate continue; 24497c478bd9Sstevel@tonic-gate 24507c478bd9Sstevel@tonic-gate if ((fetchprop(cur, DTD_ATTR_MATCH, match, 24517c478bd9Sstevel@tonic-gate sizeof (match)) == Z_OK)) { 24527c478bd9Sstevel@tonic-gate if (strcmp(tabptr->zone_dev_match, 24537c478bd9Sstevel@tonic-gate match) == 0) { 24547c478bd9Sstevel@tonic-gate if (firstmatch == NULL) 24557c478bd9Sstevel@tonic-gate firstmatch = cur; 24567c478bd9Sstevel@tonic-gate else if (firstmatch != cur) 24577c478bd9Sstevel@tonic-gate return (Z_INSUFFICIENT_SPEC); 24587c478bd9Sstevel@tonic-gate } else { 24597c478bd9Sstevel@tonic-gate /* 24607c478bd9Sstevel@tonic-gate * If another property matched but this 24617c478bd9Sstevel@tonic-gate * one doesn't then reset firstmatch. 24627c478bd9Sstevel@tonic-gate */ 24637c478bd9Sstevel@tonic-gate if (firstmatch == cur) 24647c478bd9Sstevel@tonic-gate firstmatch = NULL; 24657c478bd9Sstevel@tonic-gate } 24667c478bd9Sstevel@tonic-gate } 24677c478bd9Sstevel@tonic-gate } 24687c478bd9Sstevel@tonic-gate if (firstmatch == NULL) 24697c478bd9Sstevel@tonic-gate return (Z_NO_RESOURCE_ID); 24707c478bd9Sstevel@tonic-gate 24717c478bd9Sstevel@tonic-gate cur = firstmatch; 24727c478bd9Sstevel@tonic-gate 24737c478bd9Sstevel@tonic-gate if ((err = fetchprop(cur, DTD_ATTR_MATCH, tabptr->zone_dev_match, 24747c478bd9Sstevel@tonic-gate sizeof (tabptr->zone_dev_match))) != Z_OK) 24757c478bd9Sstevel@tonic-gate return (err); 24767c478bd9Sstevel@tonic-gate 24777c478bd9Sstevel@tonic-gate return (Z_OK); 24787c478bd9Sstevel@tonic-gate } 24797c478bd9Sstevel@tonic-gate 24807c478bd9Sstevel@tonic-gate static int 24817c478bd9Sstevel@tonic-gate zonecfg_add_dev_core(zone_dochandle_t handle, struct zone_devtab *tabptr) 24827c478bd9Sstevel@tonic-gate { 24837c478bd9Sstevel@tonic-gate xmlNodePtr newnode, cur = handle->zone_dh_cur; 24847c478bd9Sstevel@tonic-gate int err; 24857c478bd9Sstevel@tonic-gate 24867c478bd9Sstevel@tonic-gate newnode = xmlNewTextChild(cur, NULL, DTD_ELEM_DEVICE, NULL); 24877c478bd9Sstevel@tonic-gate 2488a1be23daSdp if ((err = newprop(newnode, DTD_ATTR_MATCH, 24897c478bd9Sstevel@tonic-gate tabptr->zone_dev_match)) != Z_OK) 24907c478bd9Sstevel@tonic-gate return (err); 24917c478bd9Sstevel@tonic-gate 24927c478bd9Sstevel@tonic-gate return (Z_OK); 24937c478bd9Sstevel@tonic-gate } 24947c478bd9Sstevel@tonic-gate 24957c478bd9Sstevel@tonic-gate int 24967c478bd9Sstevel@tonic-gate zonecfg_add_dev(zone_dochandle_t handle, struct zone_devtab *tabptr) 24977c478bd9Sstevel@tonic-gate { 24987c478bd9Sstevel@tonic-gate int err; 24997c478bd9Sstevel@tonic-gate 25007c478bd9Sstevel@tonic-gate if (tabptr == NULL) 25017c478bd9Sstevel@tonic-gate return (Z_INVAL); 25027c478bd9Sstevel@tonic-gate 25037c478bd9Sstevel@tonic-gate if ((err = operation_prep(handle)) != Z_OK) 25047c478bd9Sstevel@tonic-gate return (err); 25057c478bd9Sstevel@tonic-gate 25067c478bd9Sstevel@tonic-gate if ((err = zonecfg_add_dev_core(handle, tabptr)) != Z_OK) 25077c478bd9Sstevel@tonic-gate return (err); 25087c478bd9Sstevel@tonic-gate 25097c478bd9Sstevel@tonic-gate return (Z_OK); 25107c478bd9Sstevel@tonic-gate } 25117c478bd9Sstevel@tonic-gate 25127c478bd9Sstevel@tonic-gate static int 25137c478bd9Sstevel@tonic-gate zonecfg_delete_dev_core(zone_dochandle_t handle, struct zone_devtab *tabptr) 25147c478bd9Sstevel@tonic-gate { 2515facf4a8dSllai1 xmlNodePtr cur = handle->zone_dh_cur; 25167c478bd9Sstevel@tonic-gate int match_match; 25177c478bd9Sstevel@tonic-gate 25187c478bd9Sstevel@tonic-gate for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) { 25197c478bd9Sstevel@tonic-gate if (xmlStrcmp(cur->name, DTD_ELEM_DEVICE)) 25207c478bd9Sstevel@tonic-gate continue; 25217c478bd9Sstevel@tonic-gate 25227c478bd9Sstevel@tonic-gate match_match = match_prop(cur, DTD_ATTR_MATCH, 25237c478bd9Sstevel@tonic-gate tabptr->zone_dev_match); 25247c478bd9Sstevel@tonic-gate 25257c478bd9Sstevel@tonic-gate if (match_match) { 25267c478bd9Sstevel@tonic-gate xmlUnlinkNode(cur); 25277c478bd9Sstevel@tonic-gate xmlFreeNode(cur); 25287c478bd9Sstevel@tonic-gate return (Z_OK); 25297c478bd9Sstevel@tonic-gate } 25307c478bd9Sstevel@tonic-gate } 25317c478bd9Sstevel@tonic-gate return (Z_NO_RESOURCE_ID); 25327c478bd9Sstevel@tonic-gate } 25337c478bd9Sstevel@tonic-gate 25347c478bd9Sstevel@tonic-gate int 25357c478bd9Sstevel@tonic-gate zonecfg_delete_dev(zone_dochandle_t handle, struct zone_devtab *tabptr) 25367c478bd9Sstevel@tonic-gate { 25377c478bd9Sstevel@tonic-gate int err; 25387c478bd9Sstevel@tonic-gate 25397c478bd9Sstevel@tonic-gate if (tabptr == NULL) 25407c478bd9Sstevel@tonic-gate return (Z_INVAL); 25417c478bd9Sstevel@tonic-gate 25427c478bd9Sstevel@tonic-gate if ((err = operation_prep(handle)) != Z_OK) 25437c478bd9Sstevel@tonic-gate return (err); 25447c478bd9Sstevel@tonic-gate 25457c478bd9Sstevel@tonic-gate if ((err = zonecfg_delete_dev_core(handle, tabptr)) != Z_OK) 25467c478bd9Sstevel@tonic-gate return (err); 25477c478bd9Sstevel@tonic-gate 25487c478bd9Sstevel@tonic-gate return (Z_OK); 25497c478bd9Sstevel@tonic-gate } 25507c478bd9Sstevel@tonic-gate 25517c478bd9Sstevel@tonic-gate int 25527c478bd9Sstevel@tonic-gate zonecfg_modify_dev( 25537c478bd9Sstevel@tonic-gate zone_dochandle_t handle, 25547c478bd9Sstevel@tonic-gate struct zone_devtab *oldtabptr, 25557c478bd9Sstevel@tonic-gate struct zone_devtab *newtabptr) 25567c478bd9Sstevel@tonic-gate { 25577c478bd9Sstevel@tonic-gate int err; 25587c478bd9Sstevel@tonic-gate 25597c478bd9Sstevel@tonic-gate if (oldtabptr == NULL || newtabptr == NULL) 25607c478bd9Sstevel@tonic-gate return (Z_INVAL); 25617c478bd9Sstevel@tonic-gate 25627c478bd9Sstevel@tonic-gate if ((err = operation_prep(handle)) != Z_OK) 25637c478bd9Sstevel@tonic-gate return (err); 25647c478bd9Sstevel@tonic-gate 25657c478bd9Sstevel@tonic-gate if ((err = zonecfg_delete_dev_core(handle, oldtabptr)) != Z_OK) 25667c478bd9Sstevel@tonic-gate return (err); 25677c478bd9Sstevel@tonic-gate 25687c478bd9Sstevel@tonic-gate if ((err = zonecfg_add_dev_core(handle, newtabptr)) != Z_OK) 25697c478bd9Sstevel@tonic-gate return (err); 25707c478bd9Sstevel@tonic-gate 25717c478bd9Sstevel@tonic-gate return (Z_OK); 25727c478bd9Sstevel@tonic-gate } 25737c478bd9Sstevel@tonic-gate 2574ff17c8bfSgjelinek /* Lock to serialize all devwalks */ 2575ee519a1fSgjelinek static pthread_mutex_t zonecfg_devwalk_lock = PTHREAD_MUTEX_INITIALIZER; 2576ee519a1fSgjelinek /* 2577ff17c8bfSgjelinek * Global variables used to pass data from zonecfg_dev_manifest to the nftw 2578ee519a1fSgjelinek * call-back (zonecfg_devwalk_cb). g_devwalk_data is really the void* 2579ff17c8bfSgjelinek * parameter and g_devwalk_cb is really the *cb parameter from 2580ff17c8bfSgjelinek * zonecfg_dev_manifest. 2581ee519a1fSgjelinek */ 2582ff17c8bfSgjelinek typedef struct __g_devwalk_data *g_devwalk_data_t; 2583ff17c8bfSgjelinek static g_devwalk_data_t g_devwalk_data; 2584ee519a1fSgjelinek static int (*g_devwalk_cb)(const char *, uid_t, gid_t, mode_t, const char *, 2585ee519a1fSgjelinek void *); 2586ee519a1fSgjelinek static size_t g_devwalk_skip_prefix; 2587ee519a1fSgjelinek 2588ee519a1fSgjelinek /* 2589ff17c8bfSgjelinek * zonecfg_dev_manifest call-back function used during detach to generate the 2590ff17c8bfSgjelinek * dev info in the manifest. 2591ff17c8bfSgjelinek */ 2592ff17c8bfSgjelinek static int 2593ff17c8bfSgjelinek get_detach_dev_entry(const char *name, uid_t uid, gid_t gid, mode_t mode, 2594ff17c8bfSgjelinek const char *acl, void *hdl) 2595ff17c8bfSgjelinek { 2596ff17c8bfSgjelinek zone_dochandle_t handle = (zone_dochandle_t)hdl; 2597ff17c8bfSgjelinek xmlNodePtr newnode; 2598ff17c8bfSgjelinek xmlNodePtr cur; 2599ff17c8bfSgjelinek int err; 2600ff17c8bfSgjelinek char buf[128]; 2601ff17c8bfSgjelinek 2602ff17c8bfSgjelinek if ((err = operation_prep(handle)) != Z_OK) 2603ff17c8bfSgjelinek return (err); 2604ff17c8bfSgjelinek 2605ff17c8bfSgjelinek cur = handle->zone_dh_cur; 2606ff17c8bfSgjelinek newnode = xmlNewTextChild(cur, NULL, DTD_ELEM_DEV_PERM, NULL); 2607ff17c8bfSgjelinek if ((err = newprop(newnode, DTD_ATTR_NAME, (char *)name)) != Z_OK) 2608ff17c8bfSgjelinek return (err); 2609ff17c8bfSgjelinek (void) snprintf(buf, sizeof (buf), "%lu", uid); 2610ff17c8bfSgjelinek if ((err = newprop(newnode, DTD_ATTR_UID, buf)) != Z_OK) 2611ff17c8bfSgjelinek return (err); 2612ff17c8bfSgjelinek (void) snprintf(buf, sizeof (buf), "%lu", gid); 2613ff17c8bfSgjelinek if ((err = newprop(newnode, DTD_ATTR_GID, buf)) != Z_OK) 2614ff17c8bfSgjelinek return (err); 2615ff17c8bfSgjelinek (void) snprintf(buf, sizeof (buf), "%o", mode); 2616ff17c8bfSgjelinek if ((err = newprop(newnode, DTD_ATTR_MODE, buf)) != Z_OK) 2617ff17c8bfSgjelinek return (err); 2618ff17c8bfSgjelinek if ((err = newprop(newnode, DTD_ATTR_ACL, (char *)acl)) != Z_OK) 2619ff17c8bfSgjelinek return (err); 2620ff17c8bfSgjelinek return (Z_OK); 2621ff17c8bfSgjelinek } 2622ff17c8bfSgjelinek 2623ff17c8bfSgjelinek /* 2624ff17c8bfSgjelinek * This is the nftw call-back function used by zonecfg_dev_manifest. It is 2625ff17c8bfSgjelinek * responsible for calling the actual call-back. 2626ee519a1fSgjelinek */ 2627ee519a1fSgjelinek /* ARGSUSED2 */ 2628ee519a1fSgjelinek static int 2629ee519a1fSgjelinek zonecfg_devwalk_cb(const char *path, const struct stat *st, int f, 2630ee519a1fSgjelinek struct FTW *ftw) 2631ee519a1fSgjelinek { 2632ee519a1fSgjelinek acl_t *acl; 2633ee519a1fSgjelinek char *acl_txt = NULL; 2634ee519a1fSgjelinek 2635ee519a1fSgjelinek /* skip all but character and block devices */ 2636ee519a1fSgjelinek if (!S_ISBLK(st->st_mode) && !S_ISCHR(st->st_mode)) 2637ee519a1fSgjelinek return (0); 2638ee519a1fSgjelinek 2639ee519a1fSgjelinek if ((acl_get(path, ACL_NO_TRIVIAL, &acl) == 0) && 2640ee519a1fSgjelinek acl != NULL) { 2641ee519a1fSgjelinek acl_txt = acl_totext(acl, ACL_NORESOLVE); 2642ee519a1fSgjelinek acl_free(acl); 2643ee519a1fSgjelinek } 2644ee519a1fSgjelinek 2645ee519a1fSgjelinek if (strlen(path) <= g_devwalk_skip_prefix) 2646ee519a1fSgjelinek return (0); 2647ee519a1fSgjelinek 2648ee519a1fSgjelinek g_devwalk_cb(path + g_devwalk_skip_prefix, st->st_uid, st->st_gid, 2649ee519a1fSgjelinek st->st_mode & S_IAMB, acl_txt != NULL ? acl_txt : "", 2650ee519a1fSgjelinek g_devwalk_data); 2651ee519a1fSgjelinek free(acl_txt); 2652ee519a1fSgjelinek return (0); 2653ee519a1fSgjelinek } 2654ee519a1fSgjelinek 2655ee519a1fSgjelinek /* 2656ff17c8bfSgjelinek * Walk the dev tree for the zone specified by hdl and call the 2657ff17c8bfSgjelinek * get_detach_dev_entry call-back function for each entry in the tree. The 2658ff17c8bfSgjelinek * call-back will be passed the name, uid, gid, mode, acl string and the 2659ff17c8bfSgjelinek * handle input parameter for each dev entry. 2660ee519a1fSgjelinek * 2661ff17c8bfSgjelinek * Data is passed to get_detach_dev_entry through the global variables 2662ee519a1fSgjelinek * g_devwalk_data, *g_devwalk_cb, and g_devwalk_skip_prefix. The 2663ff17c8bfSgjelinek * zonecfg_devwalk_cb function will actually call get_detach_dev_entry. 2664ee519a1fSgjelinek */ 2665ee519a1fSgjelinek int 2666ff17c8bfSgjelinek zonecfg_dev_manifest(zone_dochandle_t hdl) 2667ee519a1fSgjelinek { 2668ee519a1fSgjelinek char path[MAXPATHLEN]; 2669ee519a1fSgjelinek int ret; 2670ee519a1fSgjelinek 2671ee519a1fSgjelinek if ((ret = zonecfg_get_zonepath(hdl, path, sizeof (path))) != Z_OK) 2672ee519a1fSgjelinek return (ret); 2673ee519a1fSgjelinek 2674ee519a1fSgjelinek if (strlcat(path, "/dev", sizeof (path)) >= sizeof (path)) 2675ee519a1fSgjelinek return (Z_TOO_BIG); 2676ee519a1fSgjelinek 2677ee519a1fSgjelinek /* 2678ff17c8bfSgjelinek * We have to serialize all devwalks in the same process 2679ee519a1fSgjelinek * (which should be fine), since nftw() is so badly designed. 2680ee519a1fSgjelinek */ 2681ee519a1fSgjelinek (void) pthread_mutex_lock(&zonecfg_devwalk_lock); 2682ee519a1fSgjelinek 2683ff17c8bfSgjelinek g_devwalk_skip_prefix = strlen(path) + 1; 2684ff17c8bfSgjelinek g_devwalk_data = (g_devwalk_data_t)hdl; 2685ff17c8bfSgjelinek g_devwalk_cb = get_detach_dev_entry; 2686ee519a1fSgjelinek (void) nftw(path, zonecfg_devwalk_cb, 0, FTW_PHYS); 2687ee519a1fSgjelinek 2688ee519a1fSgjelinek (void) pthread_mutex_unlock(&zonecfg_devwalk_lock); 2689ee519a1fSgjelinek return (Z_OK); 2690ee519a1fSgjelinek } 2691ee519a1fSgjelinek 2692ee519a1fSgjelinek /* 2693ee519a1fSgjelinek * Update the owner, group, mode and acl on the specified dev (inpath) for 2694ee519a1fSgjelinek * the zone (hdl). This function can be used to fix up the dev tree after 2695ee519a1fSgjelinek * attaching a migrated zone. 2696ee519a1fSgjelinek */ 2697ee519a1fSgjelinek int 2698ee519a1fSgjelinek zonecfg_devperms_apply(zone_dochandle_t hdl, const char *inpath, uid_t owner, 2699ee519a1fSgjelinek gid_t group, mode_t mode, const char *acltxt) 2700ee519a1fSgjelinek { 2701ee519a1fSgjelinek int ret; 2702ee519a1fSgjelinek char path[MAXPATHLEN]; 2703ee519a1fSgjelinek struct stat st; 2704ee519a1fSgjelinek acl_t *aclp; 2705ee519a1fSgjelinek 2706ee519a1fSgjelinek if ((ret = zonecfg_get_zonepath(hdl, path, sizeof (path))) != Z_OK) 2707ee519a1fSgjelinek return (ret); 2708ee519a1fSgjelinek 2709ee519a1fSgjelinek if (strlcat(path, "/dev/", sizeof (path)) >= sizeof (path)) 2710ee519a1fSgjelinek return (Z_TOO_BIG); 2711ee519a1fSgjelinek if (strlcat(path, inpath, sizeof (path)) >= sizeof (path)) 2712ee519a1fSgjelinek return (Z_TOO_BIG); 2713ee519a1fSgjelinek 2714ee519a1fSgjelinek if (stat(path, &st) == -1) 2715ee519a1fSgjelinek return (Z_INVAL); 2716ee519a1fSgjelinek 2717ee519a1fSgjelinek /* make sure we're only touching device nodes */ 2718ee519a1fSgjelinek if (!S_ISCHR(st.st_mode) && !S_ISBLK(st.st_mode)) 2719ee519a1fSgjelinek return (Z_INVAL); 2720ee519a1fSgjelinek 2721ee519a1fSgjelinek if (chown(path, owner, group) == -1) 2722ee519a1fSgjelinek return (Z_SYSTEM); 2723ee519a1fSgjelinek 2724ee519a1fSgjelinek if (chmod(path, mode) == -1) 2725ee519a1fSgjelinek return (Z_SYSTEM); 2726ee519a1fSgjelinek 2727ee519a1fSgjelinek if ((acltxt == NULL) || (strcmp(acltxt, "") == 0)) 2728ee519a1fSgjelinek return (Z_OK); 2729ee519a1fSgjelinek 27302ad45a84Sjv227347 if (acl_fromtext(acltxt, &aclp) != 0) { 27312ad45a84Sjv227347 errno = EINVAL; 2732ee519a1fSgjelinek return (Z_SYSTEM); 27332ad45a84Sjv227347 } 2734ee519a1fSgjelinek 2735ee519a1fSgjelinek errno = 0; 2736ee519a1fSgjelinek if (acl_set(path, aclp) == -1) { 2737ee519a1fSgjelinek free(aclp); 2738ee519a1fSgjelinek return (Z_SYSTEM); 2739ee519a1fSgjelinek } 2740ee519a1fSgjelinek 2741ee519a1fSgjelinek free(aclp); 2742ee519a1fSgjelinek return (Z_OK); 2743ee519a1fSgjelinek } 2744ee519a1fSgjelinek 27457c478bd9Sstevel@tonic-gate /* 27467c478bd9Sstevel@tonic-gate * This function finds everything mounted under a zone's rootpath. 27477c478bd9Sstevel@tonic-gate * This returns the number of mounts under rootpath, or -1 on error. 27487c478bd9Sstevel@tonic-gate * callback is called once per mount found with the first argument 27490094b373Sjv227347 * pointing to a mnttab structure containing the mount's information. 27507c478bd9Sstevel@tonic-gate * 27517c478bd9Sstevel@tonic-gate * If the callback function returns non-zero zonecfg_find_mounts 27527c478bd9Sstevel@tonic-gate * aborts with an error. 27537c478bd9Sstevel@tonic-gate */ 27547c478bd9Sstevel@tonic-gate int 27550094b373Sjv227347 zonecfg_find_mounts(char *rootpath, int (*callback)(const struct mnttab *, 27560094b373Sjv227347 void *), void *priv) { 27577c478bd9Sstevel@tonic-gate FILE *mnttab; 27587c478bd9Sstevel@tonic-gate struct mnttab m; 27597c478bd9Sstevel@tonic-gate size_t l; 276007b574eeSgjelinek int zfsl; 27617c478bd9Sstevel@tonic-gate int rv = 0; 276207b574eeSgjelinek char zfs_path[MAXPATHLEN]; 27637c478bd9Sstevel@tonic-gate 27647c478bd9Sstevel@tonic-gate assert(rootpath != NULL); 27657c478bd9Sstevel@tonic-gate 276607b574eeSgjelinek if ((zfsl = snprintf(zfs_path, sizeof (zfs_path), "%s/.zfs/", rootpath)) 276707b574eeSgjelinek >= sizeof (zfs_path)) 276807b574eeSgjelinek return (-1); 276907b574eeSgjelinek 27707c478bd9Sstevel@tonic-gate l = strlen(rootpath); 27717c478bd9Sstevel@tonic-gate 27727c478bd9Sstevel@tonic-gate mnttab = fopen("/etc/mnttab", "r"); 27737c478bd9Sstevel@tonic-gate 27747c478bd9Sstevel@tonic-gate if (mnttab == NULL) 27757c478bd9Sstevel@tonic-gate return (-1); 27767c478bd9Sstevel@tonic-gate 27777c478bd9Sstevel@tonic-gate if (ioctl(fileno(mnttab), MNTIOC_SHOWHIDDEN, NULL) < 0) { 27787c478bd9Sstevel@tonic-gate rv = -1; 27797c478bd9Sstevel@tonic-gate goto out; 27807c478bd9Sstevel@tonic-gate } 27817c478bd9Sstevel@tonic-gate 27827c478bd9Sstevel@tonic-gate while (!getmntent(mnttab, &m)) { 27837c478bd9Sstevel@tonic-gate if ((strncmp(rootpath, m.mnt_mountp, l) == 0) && 278407b574eeSgjelinek (m.mnt_mountp[l] == '/') && 278507b574eeSgjelinek (strncmp(zfs_path, m.mnt_mountp, zfsl) != 0)) { 27867c478bd9Sstevel@tonic-gate rv++; 27877c478bd9Sstevel@tonic-gate if (callback == NULL) 27887c478bd9Sstevel@tonic-gate continue; 27890094b373Sjv227347 if (callback(&m, priv)) { 27907c478bd9Sstevel@tonic-gate rv = -1; 27917c478bd9Sstevel@tonic-gate goto out; 27927c478bd9Sstevel@tonic-gate 27937c478bd9Sstevel@tonic-gate } 27947c478bd9Sstevel@tonic-gate } 27957c478bd9Sstevel@tonic-gate } 27967c478bd9Sstevel@tonic-gate 27977c478bd9Sstevel@tonic-gate out: 27987c478bd9Sstevel@tonic-gate (void) fclose(mnttab); 27997c478bd9Sstevel@tonic-gate return (rv); 28007c478bd9Sstevel@tonic-gate } 28017c478bd9Sstevel@tonic-gate 28027c478bd9Sstevel@tonic-gate int 28037c478bd9Sstevel@tonic-gate zonecfg_lookup_attr(zone_dochandle_t handle, struct zone_attrtab *tabptr) 28047c478bd9Sstevel@tonic-gate { 28057c478bd9Sstevel@tonic-gate xmlNodePtr cur, firstmatch; 28067c478bd9Sstevel@tonic-gate int err; 28077c478bd9Sstevel@tonic-gate char name[MAXNAMELEN], type[MAXNAMELEN], value[MAXNAMELEN]; 28087c478bd9Sstevel@tonic-gate 28097c478bd9Sstevel@tonic-gate if (tabptr == NULL) 28107c478bd9Sstevel@tonic-gate return (Z_INVAL); 28117c478bd9Sstevel@tonic-gate 28127c478bd9Sstevel@tonic-gate if ((err = operation_prep(handle)) != Z_OK) 28137c478bd9Sstevel@tonic-gate return (err); 28147c478bd9Sstevel@tonic-gate 28157c478bd9Sstevel@tonic-gate cur = handle->zone_dh_cur; 28167c478bd9Sstevel@tonic-gate firstmatch = NULL; 28177c478bd9Sstevel@tonic-gate for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) { 28187c478bd9Sstevel@tonic-gate if (xmlStrcmp(cur->name, DTD_ELEM_ATTR)) 28197c478bd9Sstevel@tonic-gate continue; 28207c478bd9Sstevel@tonic-gate if (strlen(tabptr->zone_attr_name) > 0) { 28217c478bd9Sstevel@tonic-gate if ((fetchprop(cur, DTD_ATTR_NAME, name, 28227c478bd9Sstevel@tonic-gate sizeof (name)) == Z_OK) && 28237c478bd9Sstevel@tonic-gate (strcmp(tabptr->zone_attr_name, name) == 0)) { 28247c478bd9Sstevel@tonic-gate if (firstmatch == NULL) 28257c478bd9Sstevel@tonic-gate firstmatch = cur; 28267c478bd9Sstevel@tonic-gate else 28277c478bd9Sstevel@tonic-gate return (Z_INSUFFICIENT_SPEC); 28287c478bd9Sstevel@tonic-gate } 28297c478bd9Sstevel@tonic-gate } 28307c478bd9Sstevel@tonic-gate if (strlen(tabptr->zone_attr_type) > 0) { 28317c478bd9Sstevel@tonic-gate if ((fetchprop(cur, DTD_ATTR_TYPE, type, 28327c478bd9Sstevel@tonic-gate sizeof (type)) == Z_OK)) { 28337c478bd9Sstevel@tonic-gate if (strcmp(tabptr->zone_attr_type, type) == 0) { 28347c478bd9Sstevel@tonic-gate if (firstmatch == NULL) 28357c478bd9Sstevel@tonic-gate firstmatch = cur; 28367c478bd9Sstevel@tonic-gate else if (firstmatch != cur) 28377c478bd9Sstevel@tonic-gate return (Z_INSUFFICIENT_SPEC); 28387c478bd9Sstevel@tonic-gate } else { 28397c478bd9Sstevel@tonic-gate /* 28407c478bd9Sstevel@tonic-gate * If another property matched but this 28417c478bd9Sstevel@tonic-gate * one doesn't then reset firstmatch. 28427c478bd9Sstevel@tonic-gate */ 28437c478bd9Sstevel@tonic-gate if (firstmatch == cur) 28447c478bd9Sstevel@tonic-gate firstmatch = NULL; 28457c478bd9Sstevel@tonic-gate } 28467c478bd9Sstevel@tonic-gate } 28477c478bd9Sstevel@tonic-gate } 28487c478bd9Sstevel@tonic-gate if (strlen(tabptr->zone_attr_value) > 0) { 28497c478bd9Sstevel@tonic-gate if ((fetchprop(cur, DTD_ATTR_VALUE, value, 28507c478bd9Sstevel@tonic-gate sizeof (value)) == Z_OK)) { 28517c478bd9Sstevel@tonic-gate if (strcmp(tabptr->zone_attr_value, value) == 28527c478bd9Sstevel@tonic-gate 0) { 28537c478bd9Sstevel@tonic-gate if (firstmatch == NULL) 28547c478bd9Sstevel@tonic-gate firstmatch = cur; 28557c478bd9Sstevel@tonic-gate else if (firstmatch != cur) 28567c478bd9Sstevel@tonic-gate return (Z_INSUFFICIENT_SPEC); 28577c478bd9Sstevel@tonic-gate } else { 28587c478bd9Sstevel@tonic-gate /* 28597c478bd9Sstevel@tonic-gate * If another property matched but this 28607c478bd9Sstevel@tonic-gate * one doesn't then reset firstmatch. 28617c478bd9Sstevel@tonic-gate */ 28627c478bd9Sstevel@tonic-gate if (firstmatch == cur) 28637c478bd9Sstevel@tonic-gate firstmatch = NULL; 28647c478bd9Sstevel@tonic-gate } 28657c478bd9Sstevel@tonic-gate } 28667c478bd9Sstevel@tonic-gate } 28677c478bd9Sstevel@tonic-gate } 28687c478bd9Sstevel@tonic-gate if (firstmatch == NULL) 28697c478bd9Sstevel@tonic-gate return (Z_NO_RESOURCE_ID); 28707c478bd9Sstevel@tonic-gate 28717c478bd9Sstevel@tonic-gate cur = firstmatch; 28727c478bd9Sstevel@tonic-gate 28737c478bd9Sstevel@tonic-gate if ((err = fetchprop(cur, DTD_ATTR_NAME, tabptr->zone_attr_name, 28747c478bd9Sstevel@tonic-gate sizeof (tabptr->zone_attr_name))) != Z_OK) 28757c478bd9Sstevel@tonic-gate return (err); 28767c478bd9Sstevel@tonic-gate 28777c478bd9Sstevel@tonic-gate if ((err = fetchprop(cur, DTD_ATTR_TYPE, tabptr->zone_attr_type, 28787c478bd9Sstevel@tonic-gate sizeof (tabptr->zone_attr_type))) != Z_OK) 28797c478bd9Sstevel@tonic-gate return (err); 28807c478bd9Sstevel@tonic-gate 28817c478bd9Sstevel@tonic-gate if ((err = fetchprop(cur, DTD_ATTR_VALUE, tabptr->zone_attr_value, 28827c478bd9Sstevel@tonic-gate sizeof (tabptr->zone_attr_value))) != Z_OK) 28837c478bd9Sstevel@tonic-gate return (err); 28847c478bd9Sstevel@tonic-gate 28857c478bd9Sstevel@tonic-gate return (Z_OK); 28867c478bd9Sstevel@tonic-gate } 28877c478bd9Sstevel@tonic-gate 28887c478bd9Sstevel@tonic-gate static int 28897c478bd9Sstevel@tonic-gate zonecfg_add_attr_core(zone_dochandle_t handle, struct zone_attrtab *tabptr) 28907c478bd9Sstevel@tonic-gate { 28917c478bd9Sstevel@tonic-gate xmlNodePtr newnode, cur = handle->zone_dh_cur; 28927c478bd9Sstevel@tonic-gate int err; 28937c478bd9Sstevel@tonic-gate 28947c478bd9Sstevel@tonic-gate newnode = xmlNewTextChild(cur, NULL, DTD_ELEM_ATTR, NULL); 2895a1be23daSdp err = newprop(newnode, DTD_ATTR_NAME, tabptr->zone_attr_name); 28967c478bd9Sstevel@tonic-gate if (err != Z_OK) 28977c478bd9Sstevel@tonic-gate return (err); 2898a1be23daSdp err = newprop(newnode, DTD_ATTR_TYPE, tabptr->zone_attr_type); 28997c478bd9Sstevel@tonic-gate if (err != Z_OK) 29007c478bd9Sstevel@tonic-gate return (err); 2901a1be23daSdp err = newprop(newnode, DTD_ATTR_VALUE, tabptr->zone_attr_value); 29027c478bd9Sstevel@tonic-gate if (err != Z_OK) 29037c478bd9Sstevel@tonic-gate return (err); 29047c478bd9Sstevel@tonic-gate return (Z_OK); 29057c478bd9Sstevel@tonic-gate } 29067c478bd9Sstevel@tonic-gate 29077c478bd9Sstevel@tonic-gate int 29087c478bd9Sstevel@tonic-gate zonecfg_add_attr(zone_dochandle_t handle, struct zone_attrtab *tabptr) 29097c478bd9Sstevel@tonic-gate { 29107c478bd9Sstevel@tonic-gate int err; 29117c478bd9Sstevel@tonic-gate 29127c478bd9Sstevel@tonic-gate if (tabptr == NULL) 29137c478bd9Sstevel@tonic-gate return (Z_INVAL); 29147c478bd9Sstevel@tonic-gate 29157c478bd9Sstevel@tonic-gate if ((err = operation_prep(handle)) != Z_OK) 29167c478bd9Sstevel@tonic-gate return (err); 29177c478bd9Sstevel@tonic-gate 29187c478bd9Sstevel@tonic-gate if ((err = zonecfg_add_attr_core(handle, tabptr)) != Z_OK) 29197c478bd9Sstevel@tonic-gate return (err); 29207c478bd9Sstevel@tonic-gate 29217c478bd9Sstevel@tonic-gate return (Z_OK); 29227c478bd9Sstevel@tonic-gate } 29237c478bd9Sstevel@tonic-gate 29247c478bd9Sstevel@tonic-gate static int 29257c478bd9Sstevel@tonic-gate zonecfg_delete_attr_core(zone_dochandle_t handle, struct zone_attrtab *tabptr) 29267c478bd9Sstevel@tonic-gate { 29277c478bd9Sstevel@tonic-gate xmlNodePtr cur = handle->zone_dh_cur; 29287c478bd9Sstevel@tonic-gate int name_match, type_match, value_match; 29297c478bd9Sstevel@tonic-gate 29307c478bd9Sstevel@tonic-gate for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) { 29317c478bd9Sstevel@tonic-gate if (xmlStrcmp(cur->name, DTD_ELEM_ATTR)) 29327c478bd9Sstevel@tonic-gate continue; 29337c478bd9Sstevel@tonic-gate 29347c478bd9Sstevel@tonic-gate name_match = match_prop(cur, DTD_ATTR_NAME, 29357c478bd9Sstevel@tonic-gate tabptr->zone_attr_name); 29367c478bd9Sstevel@tonic-gate type_match = match_prop(cur, DTD_ATTR_TYPE, 29377c478bd9Sstevel@tonic-gate tabptr->zone_attr_type); 29387c478bd9Sstevel@tonic-gate value_match = match_prop(cur, DTD_ATTR_VALUE, 29397c478bd9Sstevel@tonic-gate tabptr->zone_attr_value); 29407c478bd9Sstevel@tonic-gate 29417c478bd9Sstevel@tonic-gate if (name_match && type_match && value_match) { 29427c478bd9Sstevel@tonic-gate xmlUnlinkNode(cur); 29437c478bd9Sstevel@tonic-gate xmlFreeNode(cur); 29447c478bd9Sstevel@tonic-gate return (Z_OK); 29457c478bd9Sstevel@tonic-gate } 29467c478bd9Sstevel@tonic-gate } 29477c478bd9Sstevel@tonic-gate return (Z_NO_RESOURCE_ID); 29487c478bd9Sstevel@tonic-gate } 29497c478bd9Sstevel@tonic-gate 29507c478bd9Sstevel@tonic-gate int 29517c478bd9Sstevel@tonic-gate zonecfg_delete_attr(zone_dochandle_t handle, struct zone_attrtab *tabptr) 29527c478bd9Sstevel@tonic-gate { 29537c478bd9Sstevel@tonic-gate int err; 29547c478bd9Sstevel@tonic-gate 29557c478bd9Sstevel@tonic-gate if (tabptr == NULL) 29567c478bd9Sstevel@tonic-gate return (Z_INVAL); 29577c478bd9Sstevel@tonic-gate 29587c478bd9Sstevel@tonic-gate if ((err = operation_prep(handle)) != Z_OK) 29597c478bd9Sstevel@tonic-gate return (err); 29607c478bd9Sstevel@tonic-gate 29617c478bd9Sstevel@tonic-gate if ((err = zonecfg_delete_attr_core(handle, tabptr)) != Z_OK) 29627c478bd9Sstevel@tonic-gate return (err); 29637c478bd9Sstevel@tonic-gate 29647c478bd9Sstevel@tonic-gate return (Z_OK); 29657c478bd9Sstevel@tonic-gate } 29667c478bd9Sstevel@tonic-gate 29677c478bd9Sstevel@tonic-gate int 29687c478bd9Sstevel@tonic-gate zonecfg_modify_attr( 29697c478bd9Sstevel@tonic-gate zone_dochandle_t handle, 29707c478bd9Sstevel@tonic-gate struct zone_attrtab *oldtabptr, 29717c478bd9Sstevel@tonic-gate struct zone_attrtab *newtabptr) 29727c478bd9Sstevel@tonic-gate { 29737c478bd9Sstevel@tonic-gate int err; 29747c478bd9Sstevel@tonic-gate 29757c478bd9Sstevel@tonic-gate if (oldtabptr == NULL || newtabptr == NULL) 29767c478bd9Sstevel@tonic-gate return (Z_INVAL); 29777c478bd9Sstevel@tonic-gate 29787c478bd9Sstevel@tonic-gate if ((err = operation_prep(handle)) != Z_OK) 29797c478bd9Sstevel@tonic-gate return (err); 29807c478bd9Sstevel@tonic-gate 29817c478bd9Sstevel@tonic-gate if ((err = zonecfg_delete_attr_core(handle, oldtabptr)) != Z_OK) 29827c478bd9Sstevel@tonic-gate return (err); 29837c478bd9Sstevel@tonic-gate 29847c478bd9Sstevel@tonic-gate if ((err = zonecfg_add_attr_core(handle, newtabptr)) != Z_OK) 29857c478bd9Sstevel@tonic-gate return (err); 29867c478bd9Sstevel@tonic-gate 29877c478bd9Sstevel@tonic-gate return (Z_OK); 29887c478bd9Sstevel@tonic-gate } 29897c478bd9Sstevel@tonic-gate 29907c478bd9Sstevel@tonic-gate int 29917c478bd9Sstevel@tonic-gate zonecfg_get_attr_boolean(const struct zone_attrtab *attr, boolean_t *value) 29927c478bd9Sstevel@tonic-gate { 29937c478bd9Sstevel@tonic-gate if (attr == NULL) 29947c478bd9Sstevel@tonic-gate return (Z_INVAL); 29957c478bd9Sstevel@tonic-gate 29967c478bd9Sstevel@tonic-gate if (strcmp(attr->zone_attr_type, DTD_ENTITY_BOOLEAN) != 0) 29977c478bd9Sstevel@tonic-gate return (Z_INVAL); 29987c478bd9Sstevel@tonic-gate 29997c478bd9Sstevel@tonic-gate if (strcmp(attr->zone_attr_value, DTD_ENTITY_TRUE) == 0) { 30007c478bd9Sstevel@tonic-gate *value = B_TRUE; 30017c478bd9Sstevel@tonic-gate return (Z_OK); 30027c478bd9Sstevel@tonic-gate } 30037c478bd9Sstevel@tonic-gate if (strcmp(attr->zone_attr_value, DTD_ENTITY_FALSE) == 0) { 30047c478bd9Sstevel@tonic-gate *value = B_FALSE; 30057c478bd9Sstevel@tonic-gate return (Z_OK); 30067c478bd9Sstevel@tonic-gate } 30077c478bd9Sstevel@tonic-gate return (Z_INVAL); 30087c478bd9Sstevel@tonic-gate } 30097c478bd9Sstevel@tonic-gate 30107c478bd9Sstevel@tonic-gate int 30117c478bd9Sstevel@tonic-gate zonecfg_get_attr_int(const struct zone_attrtab *attr, int64_t *value) 30127c478bd9Sstevel@tonic-gate { 30137c478bd9Sstevel@tonic-gate long long result; 30147c478bd9Sstevel@tonic-gate char *endptr; 30157c478bd9Sstevel@tonic-gate 30167c478bd9Sstevel@tonic-gate if (attr == NULL) 30177c478bd9Sstevel@tonic-gate return (Z_INVAL); 30187c478bd9Sstevel@tonic-gate 30197c478bd9Sstevel@tonic-gate if (strcmp(attr->zone_attr_type, DTD_ENTITY_INT) != 0) 30207c478bd9Sstevel@tonic-gate return (Z_INVAL); 30217c478bd9Sstevel@tonic-gate 30227c478bd9Sstevel@tonic-gate errno = 0; 30237c478bd9Sstevel@tonic-gate result = strtoll(attr->zone_attr_value, &endptr, 10); 30247c478bd9Sstevel@tonic-gate if (errno != 0 || *endptr != '\0') 30257c478bd9Sstevel@tonic-gate return (Z_INVAL); 30267c478bd9Sstevel@tonic-gate *value = result; 30277c478bd9Sstevel@tonic-gate return (Z_OK); 30287c478bd9Sstevel@tonic-gate } 30297c478bd9Sstevel@tonic-gate 30307c478bd9Sstevel@tonic-gate int 30317c478bd9Sstevel@tonic-gate zonecfg_get_attr_string(const struct zone_attrtab *attr, char *value, 30327c478bd9Sstevel@tonic-gate size_t val_sz) 30337c478bd9Sstevel@tonic-gate { 30347c478bd9Sstevel@tonic-gate if (attr == NULL) 30357c478bd9Sstevel@tonic-gate return (Z_INVAL); 30367c478bd9Sstevel@tonic-gate 30377c478bd9Sstevel@tonic-gate if (strcmp(attr->zone_attr_type, DTD_ENTITY_STRING) != 0) 30387c478bd9Sstevel@tonic-gate return (Z_INVAL); 30397c478bd9Sstevel@tonic-gate 30407c478bd9Sstevel@tonic-gate if (strlcpy(value, attr->zone_attr_value, val_sz) >= val_sz) 30417c478bd9Sstevel@tonic-gate return (Z_TOO_BIG); 30427c478bd9Sstevel@tonic-gate return (Z_OK); 30437c478bd9Sstevel@tonic-gate } 30447c478bd9Sstevel@tonic-gate 30457c478bd9Sstevel@tonic-gate int 30467c478bd9Sstevel@tonic-gate zonecfg_get_attr_uint(const struct zone_attrtab *attr, uint64_t *value) 30477c478bd9Sstevel@tonic-gate { 30487c478bd9Sstevel@tonic-gate unsigned long long result; 30497c478bd9Sstevel@tonic-gate long long neg_result; 30507c478bd9Sstevel@tonic-gate char *endptr; 30517c478bd9Sstevel@tonic-gate 30527c478bd9Sstevel@tonic-gate if (attr == NULL) 30537c478bd9Sstevel@tonic-gate return (Z_INVAL); 30547c478bd9Sstevel@tonic-gate 30557c478bd9Sstevel@tonic-gate if (strcmp(attr->zone_attr_type, DTD_ENTITY_UINT) != 0) 30567c478bd9Sstevel@tonic-gate return (Z_INVAL); 30577c478bd9Sstevel@tonic-gate 30587c478bd9Sstevel@tonic-gate errno = 0; 30597c478bd9Sstevel@tonic-gate result = strtoull(attr->zone_attr_value, &endptr, 10); 30607c478bd9Sstevel@tonic-gate if (errno != 0 || *endptr != '\0') 30617c478bd9Sstevel@tonic-gate return (Z_INVAL); 30627c478bd9Sstevel@tonic-gate errno = 0; 30637c478bd9Sstevel@tonic-gate neg_result = strtoll(attr->zone_attr_value, &endptr, 10); 30647c478bd9Sstevel@tonic-gate /* 30657c478bd9Sstevel@tonic-gate * Incredibly, strtoull("<negative number>", ...) will not fail but 30667c478bd9Sstevel@tonic-gate * return whatever (negative) number cast as a u_longlong_t, so we 30677c478bd9Sstevel@tonic-gate * need to look for this here. 30687c478bd9Sstevel@tonic-gate */ 30697c478bd9Sstevel@tonic-gate if (errno == 0 && neg_result < 0) 30707c478bd9Sstevel@tonic-gate return (Z_INVAL); 30717c478bd9Sstevel@tonic-gate *value = result; 30727c478bd9Sstevel@tonic-gate return (Z_OK); 30737c478bd9Sstevel@tonic-gate } 30747c478bd9Sstevel@tonic-gate 30757c478bd9Sstevel@tonic-gate int 30767c478bd9Sstevel@tonic-gate zonecfg_lookup_rctl(zone_dochandle_t handle, struct zone_rctltab *tabptr) 30777c478bd9Sstevel@tonic-gate { 30787c478bd9Sstevel@tonic-gate xmlNodePtr cur, val; 30797c478bd9Sstevel@tonic-gate char savedname[MAXNAMELEN]; 30807c478bd9Sstevel@tonic-gate struct zone_rctlvaltab *valptr; 30817c478bd9Sstevel@tonic-gate int err; 30827c478bd9Sstevel@tonic-gate 30832ad45a84Sjv227347 if (strlen(tabptr->zone_rctl_name) == 0) 30847c478bd9Sstevel@tonic-gate return (Z_INVAL); 30857c478bd9Sstevel@tonic-gate 30867c478bd9Sstevel@tonic-gate if ((err = operation_prep(handle)) != Z_OK) 30877c478bd9Sstevel@tonic-gate return (err); 30887c478bd9Sstevel@tonic-gate 30897c478bd9Sstevel@tonic-gate cur = handle->zone_dh_cur; 30907c478bd9Sstevel@tonic-gate for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) { 30917c478bd9Sstevel@tonic-gate if (xmlStrcmp(cur->name, DTD_ELEM_RCTL)) 30927c478bd9Sstevel@tonic-gate continue; 30937c478bd9Sstevel@tonic-gate if ((fetchprop(cur, DTD_ATTR_NAME, savedname, 30947c478bd9Sstevel@tonic-gate sizeof (savedname)) == Z_OK) && 30957c478bd9Sstevel@tonic-gate (strcmp(savedname, tabptr->zone_rctl_name) == 0)) { 30967c478bd9Sstevel@tonic-gate tabptr->zone_rctl_valptr = NULL; 30977c478bd9Sstevel@tonic-gate for (val = cur->xmlChildrenNode; val != NULL; 30987c478bd9Sstevel@tonic-gate val = val->next) { 30997c478bd9Sstevel@tonic-gate valptr = (struct zone_rctlvaltab *)malloc( 31007c478bd9Sstevel@tonic-gate sizeof (struct zone_rctlvaltab)); 31017c478bd9Sstevel@tonic-gate if (valptr == NULL) 31027c478bd9Sstevel@tonic-gate return (Z_NOMEM); 31037c478bd9Sstevel@tonic-gate if ((fetchprop(val, DTD_ATTR_PRIV, 31047c478bd9Sstevel@tonic-gate valptr->zone_rctlval_priv, 31057c478bd9Sstevel@tonic-gate sizeof (valptr->zone_rctlval_priv)) != 31067c478bd9Sstevel@tonic-gate Z_OK)) 31077c478bd9Sstevel@tonic-gate break; 31087c478bd9Sstevel@tonic-gate if ((fetchprop(val, DTD_ATTR_LIMIT, 31097c478bd9Sstevel@tonic-gate valptr->zone_rctlval_limit, 31107c478bd9Sstevel@tonic-gate sizeof (valptr->zone_rctlval_limit)) != 31117c478bd9Sstevel@tonic-gate Z_OK)) 31127c478bd9Sstevel@tonic-gate break; 31137c478bd9Sstevel@tonic-gate if ((fetchprop(val, DTD_ATTR_ACTION, 31147c478bd9Sstevel@tonic-gate valptr->zone_rctlval_action, 31157c478bd9Sstevel@tonic-gate sizeof (valptr->zone_rctlval_action)) != 31167c478bd9Sstevel@tonic-gate Z_OK)) 31177c478bd9Sstevel@tonic-gate break; 31187c478bd9Sstevel@tonic-gate if (zonecfg_add_rctl_value(tabptr, valptr) != 31197c478bd9Sstevel@tonic-gate Z_OK) 31207c478bd9Sstevel@tonic-gate break; 31217c478bd9Sstevel@tonic-gate } 31227c478bd9Sstevel@tonic-gate return (Z_OK); 31237c478bd9Sstevel@tonic-gate } 31247c478bd9Sstevel@tonic-gate } 31257c478bd9Sstevel@tonic-gate return (Z_NO_RESOURCE_ID); 31267c478bd9Sstevel@tonic-gate } 31277c478bd9Sstevel@tonic-gate 31287c478bd9Sstevel@tonic-gate static int 31297c478bd9Sstevel@tonic-gate zonecfg_add_rctl_core(zone_dochandle_t handle, struct zone_rctltab *tabptr) 31307c478bd9Sstevel@tonic-gate { 31317c478bd9Sstevel@tonic-gate xmlNodePtr newnode, cur = handle->zone_dh_cur, valnode; 31327c478bd9Sstevel@tonic-gate struct zone_rctlvaltab *valptr; 31337c478bd9Sstevel@tonic-gate int err; 31347c478bd9Sstevel@tonic-gate 31357c478bd9Sstevel@tonic-gate newnode = xmlNewTextChild(cur, NULL, DTD_ELEM_RCTL, NULL); 3136a1be23daSdp err = newprop(newnode, DTD_ATTR_NAME, tabptr->zone_rctl_name); 31377c478bd9Sstevel@tonic-gate if (err != Z_OK) 31387c478bd9Sstevel@tonic-gate return (err); 31397c478bd9Sstevel@tonic-gate for (valptr = tabptr->zone_rctl_valptr; valptr != NULL; 31407c478bd9Sstevel@tonic-gate valptr = valptr->zone_rctlval_next) { 31417c478bd9Sstevel@tonic-gate valnode = xmlNewTextChild(newnode, NULL, 31427c478bd9Sstevel@tonic-gate DTD_ELEM_RCTLVALUE, NULL); 3143a1be23daSdp err = newprop(valnode, DTD_ATTR_PRIV, 31447c478bd9Sstevel@tonic-gate valptr->zone_rctlval_priv); 31457c478bd9Sstevel@tonic-gate if (err != Z_OK) 31467c478bd9Sstevel@tonic-gate return (err); 3147a1be23daSdp err = newprop(valnode, DTD_ATTR_LIMIT, 31487c478bd9Sstevel@tonic-gate valptr->zone_rctlval_limit); 31497c478bd9Sstevel@tonic-gate if (err != Z_OK) 31507c478bd9Sstevel@tonic-gate return (err); 3151a1be23daSdp err = newprop(valnode, DTD_ATTR_ACTION, 31527c478bd9Sstevel@tonic-gate valptr->zone_rctlval_action); 31537c478bd9Sstevel@tonic-gate if (err != Z_OK) 31547c478bd9Sstevel@tonic-gate return (err); 31557c478bd9Sstevel@tonic-gate } 31567c478bd9Sstevel@tonic-gate return (Z_OK); 31577c478bd9Sstevel@tonic-gate } 31587c478bd9Sstevel@tonic-gate 31597c478bd9Sstevel@tonic-gate int 31607c478bd9Sstevel@tonic-gate zonecfg_add_rctl(zone_dochandle_t handle, struct zone_rctltab *tabptr) 31617c478bd9Sstevel@tonic-gate { 31627c478bd9Sstevel@tonic-gate int err; 31637c478bd9Sstevel@tonic-gate 31642ad45a84Sjv227347 if (tabptr == NULL) 31657c478bd9Sstevel@tonic-gate return (Z_INVAL); 31667c478bd9Sstevel@tonic-gate 31677c478bd9Sstevel@tonic-gate if ((err = operation_prep(handle)) != Z_OK) 31687c478bd9Sstevel@tonic-gate return (err); 31697c478bd9Sstevel@tonic-gate 31707c478bd9Sstevel@tonic-gate if ((err = zonecfg_add_rctl_core(handle, tabptr)) != Z_OK) 31717c478bd9Sstevel@tonic-gate return (err); 31727c478bd9Sstevel@tonic-gate 31737c478bd9Sstevel@tonic-gate return (Z_OK); 31747c478bd9Sstevel@tonic-gate } 31757c478bd9Sstevel@tonic-gate 31767c478bd9Sstevel@tonic-gate static int 31777c478bd9Sstevel@tonic-gate zonecfg_delete_rctl_core(zone_dochandle_t handle, struct zone_rctltab *tabptr) 31787c478bd9Sstevel@tonic-gate { 31797c478bd9Sstevel@tonic-gate xmlNodePtr cur = handle->zone_dh_cur; 31807c478bd9Sstevel@tonic-gate xmlChar *savedname; 31817c478bd9Sstevel@tonic-gate int name_result; 31827c478bd9Sstevel@tonic-gate 31837c478bd9Sstevel@tonic-gate for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) { 31847c478bd9Sstevel@tonic-gate if (xmlStrcmp(cur->name, DTD_ELEM_RCTL)) 31857c478bd9Sstevel@tonic-gate continue; 31867c478bd9Sstevel@tonic-gate 31877c478bd9Sstevel@tonic-gate savedname = xmlGetProp(cur, DTD_ATTR_NAME); 31887c478bd9Sstevel@tonic-gate if (savedname == NULL) /* shouldn't happen */ 31897c478bd9Sstevel@tonic-gate continue; 31907c478bd9Sstevel@tonic-gate name_result = xmlStrcmp(savedname, 31917c478bd9Sstevel@tonic-gate (const xmlChar *) tabptr->zone_rctl_name); 31927c478bd9Sstevel@tonic-gate xmlFree(savedname); 31937c478bd9Sstevel@tonic-gate 31947c478bd9Sstevel@tonic-gate if (name_result == 0) { 31957c478bd9Sstevel@tonic-gate xmlUnlinkNode(cur); 31967c478bd9Sstevel@tonic-gate xmlFreeNode(cur); 31977c478bd9Sstevel@tonic-gate return (Z_OK); 31987c478bd9Sstevel@tonic-gate } 31997c478bd9Sstevel@tonic-gate } 32007c478bd9Sstevel@tonic-gate return (Z_NO_RESOURCE_ID); 32017c478bd9Sstevel@tonic-gate } 32027c478bd9Sstevel@tonic-gate 32037c478bd9Sstevel@tonic-gate int 32047c478bd9Sstevel@tonic-gate zonecfg_delete_rctl(zone_dochandle_t handle, struct zone_rctltab *tabptr) 32057c478bd9Sstevel@tonic-gate { 32067c478bd9Sstevel@tonic-gate int err; 32077c478bd9Sstevel@tonic-gate 32082ad45a84Sjv227347 if (tabptr == NULL) 32097c478bd9Sstevel@tonic-gate return (Z_INVAL); 32107c478bd9Sstevel@tonic-gate 32117c478bd9Sstevel@tonic-gate if ((err = operation_prep(handle)) != Z_OK) 32127c478bd9Sstevel@tonic-gate return (err); 32137c478bd9Sstevel@tonic-gate 32147c478bd9Sstevel@tonic-gate if ((err = zonecfg_delete_rctl_core(handle, tabptr)) != Z_OK) 32157c478bd9Sstevel@tonic-gate return (err); 32167c478bd9Sstevel@tonic-gate 32177c478bd9Sstevel@tonic-gate return (Z_OK); 32187c478bd9Sstevel@tonic-gate } 32197c478bd9Sstevel@tonic-gate 32207c478bd9Sstevel@tonic-gate int 32217c478bd9Sstevel@tonic-gate zonecfg_modify_rctl( 32227c478bd9Sstevel@tonic-gate zone_dochandle_t handle, 32237c478bd9Sstevel@tonic-gate struct zone_rctltab *oldtabptr, 32247c478bd9Sstevel@tonic-gate struct zone_rctltab *newtabptr) 32257c478bd9Sstevel@tonic-gate { 32267c478bd9Sstevel@tonic-gate int err; 32277c478bd9Sstevel@tonic-gate 32282ad45a84Sjv227347 if (oldtabptr == NULL || newtabptr == NULL) 32297c478bd9Sstevel@tonic-gate return (Z_INVAL); 32307c478bd9Sstevel@tonic-gate 32317c478bd9Sstevel@tonic-gate if ((err = operation_prep(handle)) != Z_OK) 32327c478bd9Sstevel@tonic-gate return (err); 32337c478bd9Sstevel@tonic-gate 32347c478bd9Sstevel@tonic-gate if ((err = zonecfg_delete_rctl_core(handle, oldtabptr)) != Z_OK) 32357c478bd9Sstevel@tonic-gate return (err); 32367c478bd9Sstevel@tonic-gate 32377c478bd9Sstevel@tonic-gate if ((err = zonecfg_add_rctl_core(handle, newtabptr)) != Z_OK) 32387c478bd9Sstevel@tonic-gate return (err); 32397c478bd9Sstevel@tonic-gate 32407c478bd9Sstevel@tonic-gate return (Z_OK); 32417c478bd9Sstevel@tonic-gate } 32427c478bd9Sstevel@tonic-gate 32437c478bd9Sstevel@tonic-gate int 32447c478bd9Sstevel@tonic-gate zonecfg_add_rctl_value( 32457c478bd9Sstevel@tonic-gate struct zone_rctltab *tabptr, 32467c478bd9Sstevel@tonic-gate struct zone_rctlvaltab *valtabptr) 32477c478bd9Sstevel@tonic-gate { 32487c478bd9Sstevel@tonic-gate struct zone_rctlvaltab *last, *old, *new; 32497c478bd9Sstevel@tonic-gate rctlblk_t *rctlblk = alloca(rctlblk_size()); 32507c478bd9Sstevel@tonic-gate 32517c478bd9Sstevel@tonic-gate last = tabptr->zone_rctl_valptr; 32527c478bd9Sstevel@tonic-gate for (old = last; old != NULL; old = old->zone_rctlval_next) 32537c478bd9Sstevel@tonic-gate last = old; /* walk to the end of the list */ 32547c478bd9Sstevel@tonic-gate new = valtabptr; /* alloc'd by caller */ 32557c478bd9Sstevel@tonic-gate new->zone_rctlval_next = NULL; 32567c478bd9Sstevel@tonic-gate if (zonecfg_construct_rctlblk(valtabptr, rctlblk) != Z_OK) 32577c478bd9Sstevel@tonic-gate return (Z_INVAL); 32587c478bd9Sstevel@tonic-gate if (!zonecfg_valid_rctlblk(rctlblk)) 32597c478bd9Sstevel@tonic-gate return (Z_INVAL); 32607c478bd9Sstevel@tonic-gate if (last == NULL) 32617c478bd9Sstevel@tonic-gate tabptr->zone_rctl_valptr = new; 32627c478bd9Sstevel@tonic-gate else 32637c478bd9Sstevel@tonic-gate last->zone_rctlval_next = new; 32647c478bd9Sstevel@tonic-gate return (Z_OK); 32657c478bd9Sstevel@tonic-gate } 32667c478bd9Sstevel@tonic-gate 32677c478bd9Sstevel@tonic-gate int 32687c478bd9Sstevel@tonic-gate zonecfg_remove_rctl_value( 32697c478bd9Sstevel@tonic-gate struct zone_rctltab *tabptr, 32707c478bd9Sstevel@tonic-gate struct zone_rctlvaltab *valtabptr) 32717c478bd9Sstevel@tonic-gate { 32727c478bd9Sstevel@tonic-gate struct zone_rctlvaltab *last, *this, *next; 32737c478bd9Sstevel@tonic-gate 32747c478bd9Sstevel@tonic-gate last = tabptr->zone_rctl_valptr; 32757c478bd9Sstevel@tonic-gate for (this = last; this != NULL; this = this->zone_rctlval_next) { 32767c478bd9Sstevel@tonic-gate if (strcmp(this->zone_rctlval_priv, 32777c478bd9Sstevel@tonic-gate valtabptr->zone_rctlval_priv) == 0 && 32787c478bd9Sstevel@tonic-gate strcmp(this->zone_rctlval_limit, 32797c478bd9Sstevel@tonic-gate valtabptr->zone_rctlval_limit) == 0 && 32807c478bd9Sstevel@tonic-gate strcmp(this->zone_rctlval_action, 32817c478bd9Sstevel@tonic-gate valtabptr->zone_rctlval_action) == 0) { 32827c478bd9Sstevel@tonic-gate next = this->zone_rctlval_next; 32837c478bd9Sstevel@tonic-gate if (this == tabptr->zone_rctl_valptr) 32847c478bd9Sstevel@tonic-gate tabptr->zone_rctl_valptr = next; 32857c478bd9Sstevel@tonic-gate else 32867c478bd9Sstevel@tonic-gate last->zone_rctlval_next = next; 32877c478bd9Sstevel@tonic-gate free(this); 32887c478bd9Sstevel@tonic-gate return (Z_OK); 32897c478bd9Sstevel@tonic-gate } else 32907c478bd9Sstevel@tonic-gate last = this; 32917c478bd9Sstevel@tonic-gate } 32927c478bd9Sstevel@tonic-gate return (Z_NO_PROPERTY_ID); 32937c478bd9Sstevel@tonic-gate } 32947c478bd9Sstevel@tonic-gate 3295ff17c8bfSgjelinek void 3296ff17c8bfSgjelinek zonecfg_set_swinv(zone_dochandle_t handle) 3297ff17c8bfSgjelinek { 3298ff17c8bfSgjelinek handle->zone_dh_sw_inv = B_TRUE; 3299ff17c8bfSgjelinek } 3300ff17c8bfSgjelinek 3301ff17c8bfSgjelinek /* 3302ff17c8bfSgjelinek * Add the pkg to the sw inventory on the handle. 3303ff17c8bfSgjelinek */ 3304ff17c8bfSgjelinek int 3305ff17c8bfSgjelinek zonecfg_add_pkg(zone_dochandle_t handle, char *name, char *version) 3306ff17c8bfSgjelinek { 3307ff17c8bfSgjelinek xmlNodePtr newnode; 3308ff17c8bfSgjelinek xmlNodePtr cur; 3309ff17c8bfSgjelinek int err; 3310ff17c8bfSgjelinek 3311ff17c8bfSgjelinek if ((err = operation_prep(handle)) != Z_OK) 3312ff17c8bfSgjelinek return (err); 3313ff17c8bfSgjelinek 3314ff17c8bfSgjelinek cur = handle->zone_dh_cur; 3315ff17c8bfSgjelinek newnode = xmlNewTextChild(cur, NULL, DTD_ELEM_PACKAGE, NULL); 3316ff17c8bfSgjelinek if ((err = newprop(newnode, DTD_ATTR_NAME, name)) != Z_OK) 3317ff17c8bfSgjelinek return (err); 3318ff17c8bfSgjelinek if ((err = newprop(newnode, DTD_ATTR_VERSION, version)) != Z_OK) 3319ff17c8bfSgjelinek return (err); 3320ff17c8bfSgjelinek return (Z_OK); 3321ff17c8bfSgjelinek } 3322ff17c8bfSgjelinek 3323ff17c8bfSgjelinek int 3324ff17c8bfSgjelinek zonecfg_add_patch(zone_dochandle_t handle, char *id, void **pnode) 3325ff17c8bfSgjelinek { 3326ff17c8bfSgjelinek xmlNodePtr node = (xmlNodePtr)*pnode; 3327ff17c8bfSgjelinek xmlNodePtr cur; 3328ff17c8bfSgjelinek int err; 3329ff17c8bfSgjelinek 3330ff17c8bfSgjelinek if ((err = operation_prep(handle)) != Z_OK) 3331ff17c8bfSgjelinek return (err); 3332ff17c8bfSgjelinek 3333ff17c8bfSgjelinek cur = handle->zone_dh_cur; 3334ff17c8bfSgjelinek node = xmlNewTextChild(cur, NULL, DTD_ELEM_PATCH, NULL); 3335ff17c8bfSgjelinek if ((err = newprop(node, DTD_ATTR_ID, id)) != Z_OK) 3336ff17c8bfSgjelinek return (err); 3337ff17c8bfSgjelinek *pnode = (void *)node; 3338ff17c8bfSgjelinek return (Z_OK); 3339ff17c8bfSgjelinek } 3340ff17c8bfSgjelinek 3341ff17c8bfSgjelinek int 3342ff17c8bfSgjelinek zonecfg_add_patch_obs(char *id, void *cur) 3343ff17c8bfSgjelinek { 3344ff17c8bfSgjelinek xmlNodePtr node; 3345ff17c8bfSgjelinek int err; 3346ff17c8bfSgjelinek 3347ff17c8bfSgjelinek node = xmlNewTextChild((xmlNodePtr)cur, NULL, DTD_ELEM_OBSOLETES, NULL); 3348ff17c8bfSgjelinek if ((err = newprop(node, DTD_ATTR_ID, id)) != Z_OK) 3349ff17c8bfSgjelinek return (err); 3350ff17c8bfSgjelinek return (Z_OK); 3351ff17c8bfSgjelinek } 3352ff17c8bfSgjelinek 33537c478bd9Sstevel@tonic-gate char * 33547c478bd9Sstevel@tonic-gate zonecfg_strerror(int errnum) 33557c478bd9Sstevel@tonic-gate { 33567c478bd9Sstevel@tonic-gate switch (errnum) { 33577c478bd9Sstevel@tonic-gate case Z_OK: 33587c478bd9Sstevel@tonic-gate return (dgettext(TEXT_DOMAIN, "OK")); 33597c478bd9Sstevel@tonic-gate case Z_EMPTY_DOCUMENT: 33607c478bd9Sstevel@tonic-gate return (dgettext(TEXT_DOMAIN, "Empty document")); 33617c478bd9Sstevel@tonic-gate case Z_WRONG_DOC_TYPE: 33627c478bd9Sstevel@tonic-gate return (dgettext(TEXT_DOMAIN, "Wrong document type")); 33637c478bd9Sstevel@tonic-gate case Z_BAD_PROPERTY: 33647c478bd9Sstevel@tonic-gate return (dgettext(TEXT_DOMAIN, "Bad document property")); 33657c478bd9Sstevel@tonic-gate case Z_TEMP_FILE: 33667c478bd9Sstevel@tonic-gate return (dgettext(TEXT_DOMAIN, 33677c478bd9Sstevel@tonic-gate "Problem creating temporary file")); 33687c478bd9Sstevel@tonic-gate case Z_SAVING_FILE: 33697c478bd9Sstevel@tonic-gate return (dgettext(TEXT_DOMAIN, "Problem saving file")); 33707c478bd9Sstevel@tonic-gate case Z_NO_ENTRY: 33717c478bd9Sstevel@tonic-gate return (dgettext(TEXT_DOMAIN, "No such entry")); 33727c478bd9Sstevel@tonic-gate case Z_BOGUS_ZONE_NAME: 33737c478bd9Sstevel@tonic-gate return (dgettext(TEXT_DOMAIN, "Bogus zone name")); 33747c478bd9Sstevel@tonic-gate case Z_REQD_RESOURCE_MISSING: 33757c478bd9Sstevel@tonic-gate return (dgettext(TEXT_DOMAIN, "Required resource missing")); 33767c478bd9Sstevel@tonic-gate case Z_REQD_PROPERTY_MISSING: 33777c478bd9Sstevel@tonic-gate return (dgettext(TEXT_DOMAIN, "Required property missing")); 33787c478bd9Sstevel@tonic-gate case Z_BAD_HANDLE: 33797c478bd9Sstevel@tonic-gate return (dgettext(TEXT_DOMAIN, "Bad handle")); 33807c478bd9Sstevel@tonic-gate case Z_NOMEM: 33817c478bd9Sstevel@tonic-gate return (dgettext(TEXT_DOMAIN, "Out of memory")); 33827c478bd9Sstevel@tonic-gate case Z_INVAL: 33837c478bd9Sstevel@tonic-gate return (dgettext(TEXT_DOMAIN, "Invalid argument")); 33847c478bd9Sstevel@tonic-gate case Z_ACCES: 33857c478bd9Sstevel@tonic-gate return (dgettext(TEXT_DOMAIN, "Permission denied")); 33867c478bd9Sstevel@tonic-gate case Z_TOO_BIG: 33877c478bd9Sstevel@tonic-gate return (dgettext(TEXT_DOMAIN, "Argument list too long")); 33887c478bd9Sstevel@tonic-gate case Z_MISC_FS: 33897c478bd9Sstevel@tonic-gate return (dgettext(TEXT_DOMAIN, 33907c478bd9Sstevel@tonic-gate "Miscellaneous file system error")); 33917c478bd9Sstevel@tonic-gate case Z_NO_ZONE: 33927c478bd9Sstevel@tonic-gate return (dgettext(TEXT_DOMAIN, "No such zone configured")); 33937c478bd9Sstevel@tonic-gate case Z_NO_RESOURCE_TYPE: 33947c478bd9Sstevel@tonic-gate return (dgettext(TEXT_DOMAIN, "No such resource type")); 33957c478bd9Sstevel@tonic-gate case Z_NO_RESOURCE_ID: 33967c478bd9Sstevel@tonic-gate return (dgettext(TEXT_DOMAIN, "No such resource with that id")); 33977c478bd9Sstevel@tonic-gate case Z_NO_PROPERTY_TYPE: 33987c478bd9Sstevel@tonic-gate return (dgettext(TEXT_DOMAIN, "No such property type")); 33997c478bd9Sstevel@tonic-gate case Z_NO_PROPERTY_ID: 34007c478bd9Sstevel@tonic-gate return (dgettext(TEXT_DOMAIN, "No such property with that id")); 3401087719fdSdp case Z_BAD_ZONE_STATE: 34027c478bd9Sstevel@tonic-gate return (dgettext(TEXT_DOMAIN, 3403087719fdSdp "Zone state is invalid for the requested operation")); 34047c478bd9Sstevel@tonic-gate case Z_INVALID_DOCUMENT: 34057c478bd9Sstevel@tonic-gate return (dgettext(TEXT_DOMAIN, "Invalid document")); 3406087719fdSdp case Z_NAME_IN_USE: 3407087719fdSdp return (dgettext(TEXT_DOMAIN, "Zone name already in use")); 34087c478bd9Sstevel@tonic-gate case Z_NO_SUCH_ID: 34097c478bd9Sstevel@tonic-gate return (dgettext(TEXT_DOMAIN, "No such zone ID")); 34107c478bd9Sstevel@tonic-gate case Z_UPDATING_INDEX: 34117c478bd9Sstevel@tonic-gate return (dgettext(TEXT_DOMAIN, "Problem updating index file")); 34127c478bd9Sstevel@tonic-gate case Z_LOCKING_FILE: 34137c478bd9Sstevel@tonic-gate return (dgettext(TEXT_DOMAIN, "Locking index file")); 34147c478bd9Sstevel@tonic-gate case Z_UNLOCKING_FILE: 34157c478bd9Sstevel@tonic-gate return (dgettext(TEXT_DOMAIN, "Unlocking index file")); 34167c478bd9Sstevel@tonic-gate case Z_INSUFFICIENT_SPEC: 34177c478bd9Sstevel@tonic-gate return (dgettext(TEXT_DOMAIN, "Insufficient specification")); 34187c478bd9Sstevel@tonic-gate case Z_RESOLVED_PATH: 34197c478bd9Sstevel@tonic-gate return (dgettext(TEXT_DOMAIN, "Resolved path mismatch")); 34207c478bd9Sstevel@tonic-gate case Z_IPV6_ADDR_PREFIX_LEN: 34217c478bd9Sstevel@tonic-gate return (dgettext(TEXT_DOMAIN, 34227c478bd9Sstevel@tonic-gate "IPv6 address missing required prefix length")); 34237c478bd9Sstevel@tonic-gate case Z_BOGUS_ADDRESS: 34247c478bd9Sstevel@tonic-gate return (dgettext(TEXT_DOMAIN, 34257c478bd9Sstevel@tonic-gate "Neither an IPv4 nor an IPv6 address nor a host name")); 3426ffbafc53Scomay case Z_PRIV_PROHIBITED: 3427ffbafc53Scomay return (dgettext(TEXT_DOMAIN, 3428ffbafc53Scomay "Specified privilege is prohibited")); 3429ffbafc53Scomay case Z_PRIV_REQUIRED: 3430ffbafc53Scomay return (dgettext(TEXT_DOMAIN, 3431ffbafc53Scomay "Required privilege is missing")); 3432ffbafc53Scomay case Z_PRIV_UNKNOWN: 3433ffbafc53Scomay return (dgettext(TEXT_DOMAIN, 3434ffbafc53Scomay "Specified privilege is unknown")); 34359acbbeafSnn35248 case Z_BRAND_ERROR: 34369acbbeafSnn35248 return (dgettext(TEXT_DOMAIN, 34379acbbeafSnn35248 "Brand-specific error")); 34380209230bSgjelinek case Z_INCOMPATIBLE: 34390209230bSgjelinek return (dgettext(TEXT_DOMAIN, "Incompatible settings")); 34400209230bSgjelinek case Z_ALIAS_DISALLOW: 34410209230bSgjelinek return (dgettext(TEXT_DOMAIN, 34420209230bSgjelinek "An incompatible rctl already exists for this property")); 34430209230bSgjelinek case Z_CLEAR_DISALLOW: 34440209230bSgjelinek return (dgettext(TEXT_DOMAIN, 34450209230bSgjelinek "Clearing this property is not allowed")); 34460209230bSgjelinek case Z_POOL: 34470209230bSgjelinek return (dgettext(TEXT_DOMAIN, "libpool(3LIB) error")); 34480209230bSgjelinek case Z_POOLS_NOT_ACTIVE: 34490209230bSgjelinek return (dgettext(TEXT_DOMAIN, "Pools facility not active; " 34500209230bSgjelinek "zone will not be bound to pool")); 34510209230bSgjelinek case Z_POOL_ENABLE: 34520209230bSgjelinek return (dgettext(TEXT_DOMAIN, 34530209230bSgjelinek "Could not enable pools facility")); 34540209230bSgjelinek case Z_NO_POOL: 34550209230bSgjelinek return (dgettext(TEXT_DOMAIN, 34560209230bSgjelinek "Pool not found; using default pool")); 34570209230bSgjelinek case Z_POOL_CREATE: 34580209230bSgjelinek return (dgettext(TEXT_DOMAIN, 34590209230bSgjelinek "Could not create a temporary pool")); 34600209230bSgjelinek case Z_POOL_BIND: 34610209230bSgjelinek return (dgettext(TEXT_DOMAIN, "Could not bind zone to pool")); 34625679c89fSjv227347 case Z_HOSTID_FUBAR: 34635679c89fSjv227347 return (dgettext(TEXT_DOMAIN, "Specified hostid is invalid")); 34642ad45a84Sjv227347 case Z_SYSTEM: 34652ad45a84Sjv227347 return (strerror(errno)); 34667c478bd9Sstevel@tonic-gate default: 34677c478bd9Sstevel@tonic-gate return (dgettext(TEXT_DOMAIN, "Unknown error")); 34687c478bd9Sstevel@tonic-gate } 34697c478bd9Sstevel@tonic-gate } 34707c478bd9Sstevel@tonic-gate 34717c478bd9Sstevel@tonic-gate /* 34727c478bd9Sstevel@tonic-gate * Note that the zonecfg_setXent() and zonecfg_endXent() calls are all the 34737c478bd9Sstevel@tonic-gate * same, as they just turn around and call zonecfg_setent() / zonecfg_endent(). 34747c478bd9Sstevel@tonic-gate */ 34757c478bd9Sstevel@tonic-gate 34767c478bd9Sstevel@tonic-gate static int 34777c478bd9Sstevel@tonic-gate zonecfg_setent(zone_dochandle_t handle) 34787c478bd9Sstevel@tonic-gate { 34797c478bd9Sstevel@tonic-gate xmlNodePtr cur; 34807c478bd9Sstevel@tonic-gate int err; 34817c478bd9Sstevel@tonic-gate 34827c478bd9Sstevel@tonic-gate if (handle == NULL) 34837c478bd9Sstevel@tonic-gate return (Z_INVAL); 34847c478bd9Sstevel@tonic-gate 34857c478bd9Sstevel@tonic-gate if ((err = operation_prep(handle)) != Z_OK) { 34867c478bd9Sstevel@tonic-gate handle->zone_dh_cur = NULL; 34877c478bd9Sstevel@tonic-gate return (err); 34887c478bd9Sstevel@tonic-gate } 34897c478bd9Sstevel@tonic-gate cur = handle->zone_dh_cur; 34907c478bd9Sstevel@tonic-gate cur = cur->xmlChildrenNode; 34917c478bd9Sstevel@tonic-gate handle->zone_dh_cur = cur; 34927c478bd9Sstevel@tonic-gate return (Z_OK); 34937c478bd9Sstevel@tonic-gate } 34947c478bd9Sstevel@tonic-gate 34957c478bd9Sstevel@tonic-gate static int 34967c478bd9Sstevel@tonic-gate zonecfg_endent(zone_dochandle_t handle) 34977c478bd9Sstevel@tonic-gate { 34987c478bd9Sstevel@tonic-gate if (handle == NULL) 34997c478bd9Sstevel@tonic-gate return (Z_INVAL); 35007c478bd9Sstevel@tonic-gate 35017c478bd9Sstevel@tonic-gate handle->zone_dh_cur = handle->zone_dh_top; 35027c478bd9Sstevel@tonic-gate return (Z_OK); 35037c478bd9Sstevel@tonic-gate } 35047c478bd9Sstevel@tonic-gate 35050209230bSgjelinek /* 35060209230bSgjelinek * Do the work required to manipulate a process through libproc. 35070209230bSgjelinek * If grab_process() returns no errors (0), then release_process() 35080209230bSgjelinek * must eventually be called. 35090209230bSgjelinek * 35100209230bSgjelinek * Return values: 35110209230bSgjelinek * 0 Successful creation of agent thread 35120209230bSgjelinek * 1 Error grabbing 35130209230bSgjelinek * 2 Error creating agent 35140209230bSgjelinek */ 35150209230bSgjelinek static int 35160209230bSgjelinek grab_process(pr_info_handle_t *p) 35170209230bSgjelinek { 35180209230bSgjelinek int ret; 35190209230bSgjelinek 35200209230bSgjelinek if ((p->pr = Pgrab(p->pid, 0, &ret)) != NULL) { 35210209230bSgjelinek 35220209230bSgjelinek if (Psetflags(p->pr, PR_RLC) != 0) { 35230209230bSgjelinek Prelease(p->pr, 0); 35240209230bSgjelinek return (1); 35250209230bSgjelinek } 35260209230bSgjelinek if (Pcreate_agent(p->pr) == 0) { 35270209230bSgjelinek return (0); 35280209230bSgjelinek 35290209230bSgjelinek } else { 35300209230bSgjelinek Prelease(p->pr, 0); 35310209230bSgjelinek return (2); 35320209230bSgjelinek } 35330209230bSgjelinek } else { 35340209230bSgjelinek return (1); 35350209230bSgjelinek } 35360209230bSgjelinek } 35370209230bSgjelinek 35380209230bSgjelinek /* 35390209230bSgjelinek * Release the specified process. This destroys the agent 35400209230bSgjelinek * and releases the process. If the process is NULL, nothing 35410209230bSgjelinek * is done. This function should only be called if grab_process() 35420209230bSgjelinek * has previously been called and returned success. 35430209230bSgjelinek * 35440209230bSgjelinek * This function is Pgrab-safe. 35450209230bSgjelinek */ 35460209230bSgjelinek static void 35470209230bSgjelinek release_process(struct ps_prochandle *Pr) 35480209230bSgjelinek { 35490209230bSgjelinek if (Pr == NULL) 35500209230bSgjelinek return; 35510209230bSgjelinek 35520209230bSgjelinek Pdestroy_agent(Pr); 35530209230bSgjelinek Prelease(Pr, 0); 35540209230bSgjelinek } 35550209230bSgjelinek 35560209230bSgjelinek static boolean_t 35570209230bSgjelinek grab_zone_proc(char *zonename, pr_info_handle_t *p) 35580209230bSgjelinek { 35590209230bSgjelinek DIR *dirp; 35600209230bSgjelinek struct dirent *dentp; 35610209230bSgjelinek zoneid_t zoneid; 35620209230bSgjelinek int pid_self; 35630209230bSgjelinek psinfo_t psinfo; 35640209230bSgjelinek 35650209230bSgjelinek if (zone_get_id(zonename, &zoneid) != 0) 35660209230bSgjelinek return (B_FALSE); 35670209230bSgjelinek 35680209230bSgjelinek pid_self = getpid(); 35690209230bSgjelinek 35700209230bSgjelinek if ((dirp = opendir("/proc")) == NULL) 35710209230bSgjelinek return (B_FALSE); 35720209230bSgjelinek 35730209230bSgjelinek while (dentp = readdir(dirp)) { 35740209230bSgjelinek p->pid = atoi(dentp->d_name); 35750209230bSgjelinek 35760209230bSgjelinek /* Skip self */ 35770209230bSgjelinek if (p->pid == pid_self) 35780209230bSgjelinek continue; 35790209230bSgjelinek 35800209230bSgjelinek if (proc_get_psinfo(p->pid, &psinfo) != 0) 35810209230bSgjelinek continue; 35820209230bSgjelinek 35830209230bSgjelinek if (psinfo.pr_zoneid != zoneid) 35840209230bSgjelinek continue; 35850209230bSgjelinek 35860209230bSgjelinek /* attempt to grab process */ 35870209230bSgjelinek if (grab_process(p) != 0) 35880209230bSgjelinek continue; 35890209230bSgjelinek 35900209230bSgjelinek if (pr_getzoneid(p->pr) != zoneid) { 35910209230bSgjelinek release_process(p->pr); 35920209230bSgjelinek continue; 35930209230bSgjelinek } 35940209230bSgjelinek 35950209230bSgjelinek (void) closedir(dirp); 35960209230bSgjelinek return (B_TRUE); 35970209230bSgjelinek } 35980209230bSgjelinek 35990209230bSgjelinek (void) closedir(dirp); 36000209230bSgjelinek return (B_FALSE); 36010209230bSgjelinek } 36020209230bSgjelinek 36030209230bSgjelinek static boolean_t 36040209230bSgjelinek get_priv_rctl(struct ps_prochandle *pr, char *name, rctlblk_t *rblk) 36050209230bSgjelinek { 36060209230bSgjelinek if (pr_getrctl(pr, name, NULL, rblk, RCTL_FIRST)) 36070209230bSgjelinek return (B_FALSE); 36080209230bSgjelinek 36090209230bSgjelinek if (rctlblk_get_privilege(rblk) == RCPRIV_PRIVILEGED) 36100209230bSgjelinek return (B_TRUE); 36110209230bSgjelinek 36120209230bSgjelinek while (pr_getrctl(pr, name, rblk, rblk, RCTL_NEXT) == 0) { 36130209230bSgjelinek if (rctlblk_get_privilege(rblk) == RCPRIV_PRIVILEGED) 36140209230bSgjelinek return (B_TRUE); 36150209230bSgjelinek } 36160209230bSgjelinek 36170209230bSgjelinek return (B_FALSE); 36180209230bSgjelinek } 36190209230bSgjelinek 36200209230bSgjelinek /* 36210209230bSgjelinek * Apply the current rctl settings to the specified, running zone. 36220209230bSgjelinek */ 36230209230bSgjelinek int 36240209230bSgjelinek zonecfg_apply_rctls(char *zone_name, zone_dochandle_t handle) 36250209230bSgjelinek { 36260209230bSgjelinek int err; 36270209230bSgjelinek int res = Z_OK; 36280209230bSgjelinek rctlblk_t *rblk; 36290209230bSgjelinek pr_info_handle_t p; 36300209230bSgjelinek struct zone_rctltab rctl; 36310209230bSgjelinek 36320209230bSgjelinek if ((err = zonecfg_setrctlent(handle)) != Z_OK) 36330209230bSgjelinek return (err); 36340209230bSgjelinek 36350209230bSgjelinek if ((rblk = (rctlblk_t *)malloc(rctlblk_size())) == NULL) { 36360209230bSgjelinek (void) zonecfg_endrctlent(handle); 36370209230bSgjelinek return (Z_NOMEM); 36380209230bSgjelinek } 36390209230bSgjelinek 36400209230bSgjelinek if (!grab_zone_proc(zone_name, &p)) { 36410209230bSgjelinek (void) zonecfg_endrctlent(handle); 36420209230bSgjelinek free(rblk); 36430209230bSgjelinek return (Z_SYSTEM); 36440209230bSgjelinek } 36450209230bSgjelinek 36460209230bSgjelinek while (zonecfg_getrctlent(handle, &rctl) == Z_OK) { 36470209230bSgjelinek char *rname; 36480209230bSgjelinek struct zone_rctlvaltab *valptr; 36490209230bSgjelinek 36500209230bSgjelinek rname = rctl.zone_rctl_name; 36510209230bSgjelinek 36520209230bSgjelinek /* first delete all current privileged settings for this rctl */ 36530209230bSgjelinek while (get_priv_rctl(p.pr, rname, rblk)) { 36540209230bSgjelinek if (pr_setrctl(p.pr, rname, NULL, rblk, RCTL_DELETE) != 36550209230bSgjelinek 0) { 36560209230bSgjelinek res = Z_SYSTEM; 36570209230bSgjelinek goto done; 36580209230bSgjelinek } 36590209230bSgjelinek } 36600209230bSgjelinek 36610209230bSgjelinek /* now set each new value for the rctl */ 36620209230bSgjelinek for (valptr = rctl.zone_rctl_valptr; valptr != NULL; 36630209230bSgjelinek valptr = valptr->zone_rctlval_next) { 36640209230bSgjelinek if ((err = zonecfg_construct_rctlblk(valptr, rblk)) 36650209230bSgjelinek != Z_OK) { 36660209230bSgjelinek res = errno = err; 36670209230bSgjelinek goto done; 36680209230bSgjelinek } 36690209230bSgjelinek 36700209230bSgjelinek if (pr_setrctl(p.pr, rname, NULL, rblk, RCTL_INSERT)) { 36710209230bSgjelinek res = Z_SYSTEM; 36720209230bSgjelinek goto done; 36730209230bSgjelinek } 36740209230bSgjelinek } 36750209230bSgjelinek } 36760209230bSgjelinek 36770209230bSgjelinek done: 36780209230bSgjelinek release_process(p.pr); 36790209230bSgjelinek free(rblk); 36800209230bSgjelinek (void) zonecfg_endrctlent(handle); 36810209230bSgjelinek 36820209230bSgjelinek return (res); 36830209230bSgjelinek } 36840209230bSgjelinek 36850209230bSgjelinek static const xmlChar * 36860209230bSgjelinek nm_to_dtd(char *nm) 36870209230bSgjelinek { 36880209230bSgjelinek if (strcmp(nm, "device") == 0) 36890209230bSgjelinek return (DTD_ELEM_DEVICE); 36900209230bSgjelinek if (strcmp(nm, "fs") == 0) 36910209230bSgjelinek return (DTD_ELEM_FS); 36920209230bSgjelinek if (strcmp(nm, "inherit-pkg-dir") == 0) 36930209230bSgjelinek return (DTD_ELEM_IPD); 36940209230bSgjelinek if (strcmp(nm, "net") == 0) 36950209230bSgjelinek return (DTD_ELEM_NET); 36960209230bSgjelinek if (strcmp(nm, "attr") == 0) 36970209230bSgjelinek return (DTD_ELEM_ATTR); 36980209230bSgjelinek if (strcmp(nm, "rctl") == 0) 36990209230bSgjelinek return (DTD_ELEM_RCTL); 37000209230bSgjelinek if (strcmp(nm, "dataset") == 0) 37010209230bSgjelinek return (DTD_ELEM_DATASET); 37020209230bSgjelinek 37030209230bSgjelinek return (NULL); 37040209230bSgjelinek } 37050209230bSgjelinek 37060209230bSgjelinek int 37070209230bSgjelinek zonecfg_num_resources(zone_dochandle_t handle, char *rsrc) 37080209230bSgjelinek { 37090209230bSgjelinek int num = 0; 37100209230bSgjelinek const xmlChar *dtd; 37110209230bSgjelinek xmlNodePtr cur; 37120209230bSgjelinek 37130209230bSgjelinek if ((dtd = nm_to_dtd(rsrc)) == NULL) 37140209230bSgjelinek return (num); 37150209230bSgjelinek 37160209230bSgjelinek if (zonecfg_setent(handle) != Z_OK) 37170209230bSgjelinek return (num); 37180209230bSgjelinek 37190209230bSgjelinek for (cur = handle->zone_dh_cur; cur != NULL; cur = cur->next) 37200209230bSgjelinek if (xmlStrcmp(cur->name, dtd) == 0) 37210209230bSgjelinek num++; 37220209230bSgjelinek 37230209230bSgjelinek (void) zonecfg_endent(handle); 37240209230bSgjelinek 37250209230bSgjelinek return (num); 37260209230bSgjelinek } 37270209230bSgjelinek 37280209230bSgjelinek int 37290209230bSgjelinek zonecfg_del_all_resources(zone_dochandle_t handle, char *rsrc) 37300209230bSgjelinek { 37310209230bSgjelinek int err; 37320209230bSgjelinek const xmlChar *dtd; 37330209230bSgjelinek xmlNodePtr cur; 37340209230bSgjelinek 37350209230bSgjelinek if ((dtd = nm_to_dtd(rsrc)) == NULL) 37360209230bSgjelinek return (Z_NO_RESOURCE_TYPE); 37370209230bSgjelinek 37380209230bSgjelinek if ((err = zonecfg_setent(handle)) != Z_OK) 37390209230bSgjelinek return (err); 37400209230bSgjelinek 37410209230bSgjelinek cur = handle->zone_dh_cur; 37420209230bSgjelinek while (cur != NULL) { 37430209230bSgjelinek xmlNodePtr tmp; 37440209230bSgjelinek 37450209230bSgjelinek if (xmlStrcmp(cur->name, dtd)) { 37460209230bSgjelinek cur = cur->next; 37470209230bSgjelinek continue; 37480209230bSgjelinek } 37490209230bSgjelinek 37500209230bSgjelinek tmp = cur->next; 37510209230bSgjelinek xmlUnlinkNode(cur); 37520209230bSgjelinek xmlFreeNode(cur); 37530209230bSgjelinek cur = tmp; 37540209230bSgjelinek } 37550209230bSgjelinek 37560209230bSgjelinek (void) zonecfg_endent(handle); 37570209230bSgjelinek return (Z_OK); 37580209230bSgjelinek } 37590209230bSgjelinek 37600209230bSgjelinek static boolean_t 37610209230bSgjelinek valid_uint(char *s, uint64_t *n) 37620209230bSgjelinek { 37630209230bSgjelinek char *endp; 37640209230bSgjelinek 37650209230bSgjelinek /* strtoull accepts '-'?! so we want to flag that as an error */ 37660209230bSgjelinek if (strchr(s, '-') != NULL) 37670209230bSgjelinek return (B_FALSE); 37680209230bSgjelinek 37690209230bSgjelinek errno = 0; 37700209230bSgjelinek *n = strtoull(s, &endp, 10); 37710209230bSgjelinek 37720209230bSgjelinek if (errno != 0 || *endp != '\0') 37730209230bSgjelinek return (B_FALSE); 37740209230bSgjelinek return (B_TRUE); 37750209230bSgjelinek } 37760209230bSgjelinek 37770209230bSgjelinek /* 37780209230bSgjelinek * Convert a string representing a number (possibly a fraction) into an integer. 37790209230bSgjelinek * The string can have a modifier (K, M, G or T). The modifiers are treated 37800209230bSgjelinek * as powers of two (not 10). 37810209230bSgjelinek */ 37820209230bSgjelinek int 37830209230bSgjelinek zonecfg_str_to_bytes(char *str, uint64_t *bytes) 37840209230bSgjelinek { 37850209230bSgjelinek long double val; 37860209230bSgjelinek char *unitp; 37870209230bSgjelinek uint64_t scale; 37880209230bSgjelinek 37890209230bSgjelinek if ((val = strtold(str, &unitp)) < 0) 37900209230bSgjelinek return (-1); 37910209230bSgjelinek 37920209230bSgjelinek /* remove any leading white space from units string */ 37930209230bSgjelinek while (isspace(*unitp) != 0) 37940209230bSgjelinek ++unitp; 37950209230bSgjelinek 37960209230bSgjelinek /* if no units explicitly set, error */ 37970209230bSgjelinek if (unitp == NULL || *unitp == '\0') { 37980209230bSgjelinek scale = 1; 37990209230bSgjelinek } else { 38000209230bSgjelinek int i; 38010209230bSgjelinek char *units[] = {"K", "M", "G", "T", NULL}; 38020209230bSgjelinek 38030209230bSgjelinek scale = 1024; 38040209230bSgjelinek 38050209230bSgjelinek /* update scale based on units */ 38060209230bSgjelinek for (i = 0; units[i] != NULL; i++) { 38070209230bSgjelinek if (strcasecmp(unitp, units[i]) == 0) 38080209230bSgjelinek break; 38090209230bSgjelinek scale <<= 10; 38100209230bSgjelinek } 38110209230bSgjelinek 38120209230bSgjelinek if (units[i] == NULL) 38130209230bSgjelinek return (-1); 38140209230bSgjelinek } 38150209230bSgjelinek 38160209230bSgjelinek *bytes = (uint64_t)(val * scale); 38170209230bSgjelinek return (0); 38180209230bSgjelinek } 38190209230bSgjelinek 38200209230bSgjelinek boolean_t 38210209230bSgjelinek zonecfg_valid_ncpus(char *lowstr, char *highstr) 38220209230bSgjelinek { 38230209230bSgjelinek uint64_t low, high; 38240209230bSgjelinek 38250209230bSgjelinek if (!valid_uint(lowstr, &low) || !valid_uint(highstr, &high) || 38260209230bSgjelinek low < 1 || low > high) 38270209230bSgjelinek return (B_FALSE); 38280209230bSgjelinek 38290209230bSgjelinek return (B_TRUE); 38300209230bSgjelinek } 38310209230bSgjelinek 38320209230bSgjelinek boolean_t 38330209230bSgjelinek zonecfg_valid_importance(char *impstr) 38340209230bSgjelinek { 38350209230bSgjelinek uint64_t num; 38360209230bSgjelinek 38370209230bSgjelinek if (!valid_uint(impstr, &num)) 38380209230bSgjelinek return (B_FALSE); 38390209230bSgjelinek 38400209230bSgjelinek return (B_TRUE); 38410209230bSgjelinek } 38420209230bSgjelinek 38430209230bSgjelinek boolean_t 38440209230bSgjelinek zonecfg_valid_alias_limit(char *name, char *limitstr, uint64_t *limit) 38450209230bSgjelinek { 38460209230bSgjelinek int i; 38470209230bSgjelinek 38480209230bSgjelinek for (i = 0; aliases[i].shortname != NULL; i++) 38490209230bSgjelinek if (strcmp(name, aliases[i].shortname) == 0) 38500209230bSgjelinek break; 38510209230bSgjelinek 38520209230bSgjelinek if (aliases[i].shortname == NULL) 38530209230bSgjelinek return (B_FALSE); 38540209230bSgjelinek 38550209230bSgjelinek if (!valid_uint(limitstr, limit) || *limit < aliases[i].low_limit) 38560209230bSgjelinek return (B_FALSE); 38570209230bSgjelinek 38580209230bSgjelinek return (B_TRUE); 38590209230bSgjelinek } 38600209230bSgjelinek 38610209230bSgjelinek boolean_t 38620209230bSgjelinek zonecfg_valid_memlimit(char *memstr, uint64_t *mem_val) 38630209230bSgjelinek { 38640209230bSgjelinek if (zonecfg_str_to_bytes(memstr, mem_val) != 0) 38650209230bSgjelinek return (B_FALSE); 38660209230bSgjelinek 38670209230bSgjelinek return (B_TRUE); 38680209230bSgjelinek } 38690209230bSgjelinek 38700209230bSgjelinek static int 38710209230bSgjelinek zerr_pool(char *pool_err, int err_size, int res) 38720209230bSgjelinek { 38730209230bSgjelinek (void) strlcpy(pool_err, pool_strerror(pool_error()), err_size); 38740209230bSgjelinek return (res); 38750209230bSgjelinek } 38760209230bSgjelinek 38770209230bSgjelinek static int 38780209230bSgjelinek create_tmp_pset(char *pool_err, int err_size, pool_conf_t *pconf, pool_t *pool, 38790209230bSgjelinek char *name, int min, int max) 38800209230bSgjelinek { 38810209230bSgjelinek pool_resource_t *res; 38820209230bSgjelinek pool_elem_t *elem; 38830209230bSgjelinek pool_value_t *val; 38840209230bSgjelinek 38850209230bSgjelinek if ((res = pool_resource_create(pconf, "pset", name)) == NULL) 38860209230bSgjelinek return (zerr_pool(pool_err, err_size, Z_POOL)); 38870209230bSgjelinek 38880209230bSgjelinek if (pool_associate(pconf, pool, res) != PO_SUCCESS) 38890209230bSgjelinek return (zerr_pool(pool_err, err_size, Z_POOL)); 38900209230bSgjelinek 38910209230bSgjelinek if ((elem = pool_resource_to_elem(pconf, res)) == NULL) 38920209230bSgjelinek return (zerr_pool(pool_err, err_size, Z_POOL)); 38930209230bSgjelinek 38940209230bSgjelinek if ((val = pool_value_alloc()) == NULL) 38950209230bSgjelinek return (zerr_pool(pool_err, err_size, Z_POOL)); 38960209230bSgjelinek 38970209230bSgjelinek /* set the maximum number of cpus for the pset */ 38980209230bSgjelinek pool_value_set_uint64(val, (uint64_t)max); 38990209230bSgjelinek 39000209230bSgjelinek if (pool_put_property(pconf, elem, "pset.max", val) != PO_SUCCESS) { 39010209230bSgjelinek pool_value_free(val); 39020209230bSgjelinek return (zerr_pool(pool_err, err_size, Z_POOL)); 39030209230bSgjelinek } 39040209230bSgjelinek 39050209230bSgjelinek /* set the minimum number of cpus for the pset */ 39060209230bSgjelinek pool_value_set_uint64(val, (uint64_t)min); 39070209230bSgjelinek 39080209230bSgjelinek if (pool_put_property(pconf, elem, "pset.min", val) != PO_SUCCESS) { 39090209230bSgjelinek pool_value_free(val); 39100209230bSgjelinek return (zerr_pool(pool_err, err_size, Z_POOL)); 39110209230bSgjelinek } 39120209230bSgjelinek 39130209230bSgjelinek pool_value_free(val); 39140209230bSgjelinek 39150209230bSgjelinek return (Z_OK); 39160209230bSgjelinek } 39170209230bSgjelinek 39180209230bSgjelinek static int 39190209230bSgjelinek create_tmp_pool(char *pool_err, int err_size, pool_conf_t *pconf, char *name, 39200209230bSgjelinek struct zone_psettab *pset_tab) 39210209230bSgjelinek { 39220209230bSgjelinek pool_t *pool; 39230209230bSgjelinek int res = Z_OK; 39240209230bSgjelinek 39250209230bSgjelinek /* create a temporary pool configuration */ 39260209230bSgjelinek if (pool_conf_open(pconf, NULL, PO_TEMP) != PO_SUCCESS) { 39270209230bSgjelinek res = zerr_pool(pool_err, err_size, Z_POOL); 39280209230bSgjelinek return (res); 39290209230bSgjelinek } 39300209230bSgjelinek 39310209230bSgjelinek if ((pool = pool_create(pconf, name)) == NULL) { 39320209230bSgjelinek res = zerr_pool(pool_err, err_size, Z_POOL_CREATE); 39330209230bSgjelinek goto done; 39340209230bSgjelinek } 39350209230bSgjelinek 39360209230bSgjelinek /* set pool importance */ 39370209230bSgjelinek if (pset_tab->zone_importance[0] != '\0') { 39380209230bSgjelinek pool_elem_t *elem; 39390209230bSgjelinek pool_value_t *val; 39400209230bSgjelinek 39410209230bSgjelinek if ((elem = pool_to_elem(pconf, pool)) == NULL) { 39420209230bSgjelinek res = zerr_pool(pool_err, err_size, Z_POOL); 39430209230bSgjelinek goto done; 39440209230bSgjelinek } 39450209230bSgjelinek 39460209230bSgjelinek if ((val = pool_value_alloc()) == NULL) { 39470209230bSgjelinek res = zerr_pool(pool_err, err_size, Z_POOL); 39480209230bSgjelinek goto done; 39490209230bSgjelinek } 39500209230bSgjelinek 39510209230bSgjelinek pool_value_set_int64(val, 39520209230bSgjelinek (int64_t)atoi(pset_tab->zone_importance)); 39530209230bSgjelinek 39540209230bSgjelinek if (pool_put_property(pconf, elem, "pool.importance", val) 39550209230bSgjelinek != PO_SUCCESS) { 39560209230bSgjelinek res = zerr_pool(pool_err, err_size, Z_POOL); 39570209230bSgjelinek pool_value_free(val); 39580209230bSgjelinek goto done; 39590209230bSgjelinek } 39600209230bSgjelinek 39610209230bSgjelinek pool_value_free(val); 39620209230bSgjelinek } 39630209230bSgjelinek 39640209230bSgjelinek if ((res = create_tmp_pset(pool_err, err_size, pconf, pool, name, 39650209230bSgjelinek atoi(pset_tab->zone_ncpu_min), 39660209230bSgjelinek atoi(pset_tab->zone_ncpu_max))) != Z_OK) 39670209230bSgjelinek goto done; 39680209230bSgjelinek 39690209230bSgjelinek /* validation */ 39700209230bSgjelinek if (pool_conf_status(pconf) == POF_INVALID) { 39710209230bSgjelinek res = zerr_pool(pool_err, err_size, Z_POOL); 39720209230bSgjelinek goto done; 39730209230bSgjelinek } 39740209230bSgjelinek 39750209230bSgjelinek /* 39760209230bSgjelinek * This validation is the one we expect to fail if the user specified 39770209230bSgjelinek * an invalid configuration (too many cpus) for this system. 39780209230bSgjelinek */ 39790209230bSgjelinek if (pool_conf_validate(pconf, POV_RUNTIME) != PO_SUCCESS) { 39800209230bSgjelinek res = zerr_pool(pool_err, err_size, Z_POOL_CREATE); 39810209230bSgjelinek goto done; 39820209230bSgjelinek } 39830209230bSgjelinek 39840209230bSgjelinek /* 39850209230bSgjelinek * Commit the dynamic configuration but not the pool configuration 39860209230bSgjelinek * file. 39870209230bSgjelinek */ 39880209230bSgjelinek if (pool_conf_commit(pconf, 1) != PO_SUCCESS) 39890209230bSgjelinek res = zerr_pool(pool_err, err_size, Z_POOL); 39900209230bSgjelinek 39910209230bSgjelinek done: 39920209230bSgjelinek (void) pool_conf_close(pconf); 39930209230bSgjelinek return (res); 39940209230bSgjelinek } 39950209230bSgjelinek 39960209230bSgjelinek static int 39970209230bSgjelinek get_running_tmp_pset(pool_conf_t *pconf, pool_t *pool, pool_resource_t *pset, 39980209230bSgjelinek struct zone_psettab *pset_tab) 39990209230bSgjelinek { 40000209230bSgjelinek int nfound = 0; 40010209230bSgjelinek pool_elem_t *pe; 40020209230bSgjelinek pool_value_t *pv = pool_value_alloc(); 40030209230bSgjelinek uint64_t val_uint; 40040209230bSgjelinek 40050209230bSgjelinek if (pool != NULL) { 40060209230bSgjelinek pe = pool_to_elem(pconf, pool); 40070209230bSgjelinek if (pool_get_property(pconf, pe, "pool.importance", pv) 40080209230bSgjelinek != POC_INVAL) { 40090209230bSgjelinek int64_t val_int; 40100209230bSgjelinek 40110209230bSgjelinek (void) pool_value_get_int64(pv, &val_int); 40120209230bSgjelinek (void) snprintf(pset_tab->zone_importance, 40130209230bSgjelinek sizeof (pset_tab->zone_importance), "%d", val_int); 40140209230bSgjelinek nfound++; 40150209230bSgjelinek } 40160209230bSgjelinek } 40170209230bSgjelinek 40180209230bSgjelinek if (pset != NULL) { 40190209230bSgjelinek pe = pool_resource_to_elem(pconf, pset); 40200209230bSgjelinek if (pool_get_property(pconf, pe, "pset.min", pv) != POC_INVAL) { 40210209230bSgjelinek (void) pool_value_get_uint64(pv, &val_uint); 40220209230bSgjelinek (void) snprintf(pset_tab->zone_ncpu_min, 40230209230bSgjelinek sizeof (pset_tab->zone_ncpu_min), "%u", val_uint); 40240209230bSgjelinek nfound++; 40250209230bSgjelinek } 40260209230bSgjelinek 40270209230bSgjelinek if (pool_get_property(pconf, pe, "pset.max", pv) != POC_INVAL) { 40280209230bSgjelinek (void) pool_value_get_uint64(pv, &val_uint); 40290209230bSgjelinek (void) snprintf(pset_tab->zone_ncpu_max, 40300209230bSgjelinek sizeof (pset_tab->zone_ncpu_max), "%u", val_uint); 40310209230bSgjelinek nfound++; 40320209230bSgjelinek } 40330209230bSgjelinek } 40340209230bSgjelinek 40350209230bSgjelinek pool_value_free(pv); 40360209230bSgjelinek 40370209230bSgjelinek if (nfound == 3) 40380209230bSgjelinek return (PO_SUCCESS); 40390209230bSgjelinek 40400209230bSgjelinek return (PO_FAIL); 40410209230bSgjelinek } 40420209230bSgjelinek 40430209230bSgjelinek /* 40440209230bSgjelinek * Determine if a tmp pool is configured and if so, if the configuration is 40450209230bSgjelinek * still valid or if it has been changed since the tmp pool was created. 40460209230bSgjelinek * If the tmp pool configuration is no longer valid, delete the tmp pool. 40470209230bSgjelinek * 40480209230bSgjelinek * Set *valid=B_TRUE if there is an existing, valid tmp pool configuration. 40490209230bSgjelinek */ 40500209230bSgjelinek static int 40510209230bSgjelinek verify_del_tmp_pool(pool_conf_t *pconf, char *tmp_name, char *pool_err, 40520209230bSgjelinek int err_size, struct zone_psettab *pset_tab, boolean_t *exists) 40530209230bSgjelinek { 40540209230bSgjelinek int res = Z_OK; 40550209230bSgjelinek pool_t *pool; 40560209230bSgjelinek pool_resource_t *pset; 40570209230bSgjelinek struct zone_psettab pset_current; 40580209230bSgjelinek 40590209230bSgjelinek *exists = B_FALSE; 40600209230bSgjelinek 40610209230bSgjelinek if (pool_conf_open(pconf, pool_dynamic_location(), PO_RDWR) 40620209230bSgjelinek != PO_SUCCESS) { 40630209230bSgjelinek res = zerr_pool(pool_err, err_size, Z_POOL); 40640209230bSgjelinek return (res); 40650209230bSgjelinek } 40660209230bSgjelinek 40670209230bSgjelinek pool = pool_get_pool(pconf, tmp_name); 40680209230bSgjelinek pset = pool_get_resource(pconf, "pset", tmp_name); 40690209230bSgjelinek 40700209230bSgjelinek if (pool == NULL && pset == NULL) { 40710209230bSgjelinek /* no tmp pool configured */ 40720209230bSgjelinek goto done; 40730209230bSgjelinek } 40740209230bSgjelinek 40750209230bSgjelinek /* 40760209230bSgjelinek * If an existing tmp pool for this zone is configured with the proper 40770209230bSgjelinek * settings, then the tmp pool is valid. 40780209230bSgjelinek */ 40790209230bSgjelinek if (get_running_tmp_pset(pconf, pool, pset, &pset_current) 40800209230bSgjelinek == PO_SUCCESS && 40810209230bSgjelinek strcmp(pset_tab->zone_ncpu_min, 40820209230bSgjelinek pset_current.zone_ncpu_min) == 0 && 40830209230bSgjelinek strcmp(pset_tab->zone_ncpu_max, 40840209230bSgjelinek pset_current.zone_ncpu_max) == 0 && 40850209230bSgjelinek strcmp(pset_tab->zone_importance, 40860209230bSgjelinek pset_current.zone_importance) == 0) { 40870209230bSgjelinek *exists = B_TRUE; 40880209230bSgjelinek 40890209230bSgjelinek } else { 40900209230bSgjelinek /* 40910209230bSgjelinek * An out-of-date tmp pool configuration exists. Delete it 40920209230bSgjelinek * so that we can create the correct tmp pool config. 40930209230bSgjelinek */ 40940209230bSgjelinek if (pset != NULL && 40950209230bSgjelinek pool_resource_destroy(pconf, pset) != PO_SUCCESS) { 40960209230bSgjelinek res = zerr_pool(pool_err, err_size, Z_POOL); 40970209230bSgjelinek goto done; 40980209230bSgjelinek } 40990209230bSgjelinek 41000209230bSgjelinek if (pool != NULL && 41010209230bSgjelinek pool_destroy(pconf, pool) != PO_SUCCESS) { 41020209230bSgjelinek res = zerr_pool(pool_err, err_size, Z_POOL); 41030209230bSgjelinek goto done; 41040209230bSgjelinek } 41050209230bSgjelinek 41060209230bSgjelinek /* commit dynamic config */ 41070209230bSgjelinek if (pool_conf_commit(pconf, 0) != PO_SUCCESS) 41080209230bSgjelinek res = zerr_pool(pool_err, err_size, Z_POOL); 41090209230bSgjelinek } 41100209230bSgjelinek 41110209230bSgjelinek done: 41120209230bSgjelinek (void) pool_conf_close(pconf); 41130209230bSgjelinek 41140209230bSgjelinek return (res); 41150209230bSgjelinek } 41160209230bSgjelinek 41170209230bSgjelinek /* 41180209230bSgjelinek * Destroy any existing tmp pool. 41190209230bSgjelinek */ 41200209230bSgjelinek int 41210209230bSgjelinek zonecfg_destroy_tmp_pool(char *zone_name, char *pool_err, int err_size) 41220209230bSgjelinek { 41230209230bSgjelinek int status; 41240209230bSgjelinek int res = Z_OK; 41250209230bSgjelinek pool_conf_t *pconf; 41260209230bSgjelinek pool_t *pool; 41270209230bSgjelinek pool_resource_t *pset; 41280209230bSgjelinek char tmp_name[MAX_TMP_POOL_NAME]; 41290209230bSgjelinek 41300209230bSgjelinek /* if pools not enabled then nothing to do */ 41310209230bSgjelinek if (pool_get_status(&status) != PO_SUCCESS || status != POOL_ENABLED) 41320209230bSgjelinek return (Z_OK); 41330209230bSgjelinek 41340209230bSgjelinek if ((pconf = pool_conf_alloc()) == NULL) 41350209230bSgjelinek return (zerr_pool(pool_err, err_size, Z_POOL)); 41360209230bSgjelinek 41370209230bSgjelinek (void) snprintf(tmp_name, sizeof (tmp_name), TMP_POOL_NAME, zone_name); 41380209230bSgjelinek 41390209230bSgjelinek if (pool_conf_open(pconf, pool_dynamic_location(), PO_RDWR) 41400209230bSgjelinek != PO_SUCCESS) { 41410209230bSgjelinek res = zerr_pool(pool_err, err_size, Z_POOL); 41420209230bSgjelinek pool_conf_free(pconf); 41430209230bSgjelinek return (res); 41440209230bSgjelinek } 41450209230bSgjelinek 41460209230bSgjelinek pool = pool_get_pool(pconf, tmp_name); 41470209230bSgjelinek pset = pool_get_resource(pconf, "pset", tmp_name); 41480209230bSgjelinek 41490209230bSgjelinek if (pool == NULL && pset == NULL) { 41500209230bSgjelinek /* nothing to destroy, we're done */ 41510209230bSgjelinek goto done; 41520209230bSgjelinek } 41530209230bSgjelinek 41540209230bSgjelinek if (pset != NULL && pool_resource_destroy(pconf, pset) != PO_SUCCESS) { 41550209230bSgjelinek res = zerr_pool(pool_err, err_size, Z_POOL); 41560209230bSgjelinek goto done; 41570209230bSgjelinek } 41580209230bSgjelinek 41590209230bSgjelinek if (pool != NULL && pool_destroy(pconf, pool) != PO_SUCCESS) { 41600209230bSgjelinek res = zerr_pool(pool_err, err_size, Z_POOL); 41610209230bSgjelinek goto done; 41620209230bSgjelinek } 41630209230bSgjelinek 41640209230bSgjelinek /* commit dynamic config */ 41650209230bSgjelinek if (pool_conf_commit(pconf, 0) != PO_SUCCESS) 41660209230bSgjelinek res = zerr_pool(pool_err, err_size, Z_POOL); 41670209230bSgjelinek 41680209230bSgjelinek done: 41690209230bSgjelinek (void) pool_conf_close(pconf); 41700209230bSgjelinek pool_conf_free(pconf); 41710209230bSgjelinek 41720209230bSgjelinek return (res); 41730209230bSgjelinek } 41740209230bSgjelinek 41750209230bSgjelinek /* 41760209230bSgjelinek * Attempt to bind to a tmp pool for this zone. If there is no tmp pool 41770209230bSgjelinek * configured, we just return Z_OK. 41780209230bSgjelinek * 41790209230bSgjelinek * We either attempt to create the tmp pool for this zone or rebind to an 41800209230bSgjelinek * existing tmp pool for this zone. 41810209230bSgjelinek * 41820209230bSgjelinek * Rebinding is used when a zone with a tmp pool reboots so that we don't have 41830209230bSgjelinek * to recreate the tmp pool. To do this we need to be sure we work correctly 41840209230bSgjelinek * for the following cases: 41850209230bSgjelinek * 41860209230bSgjelinek * - there is an existing, properly configured tmp pool. 41870209230bSgjelinek * - zonecfg added tmp pool after zone was booted, must now create. 41880209230bSgjelinek * - zonecfg updated tmp pool config after zone was booted, in this case 41890209230bSgjelinek * we destroy the old tmp pool and create a new one. 41900209230bSgjelinek */ 41910209230bSgjelinek int 41920209230bSgjelinek zonecfg_bind_tmp_pool(zone_dochandle_t handle, zoneid_t zoneid, char *pool_err, 41930209230bSgjelinek int err_size) 41940209230bSgjelinek { 41950209230bSgjelinek struct zone_psettab pset_tab; 41960209230bSgjelinek int err; 41970209230bSgjelinek int status; 41980209230bSgjelinek pool_conf_t *pconf; 41990209230bSgjelinek boolean_t exists; 42000209230bSgjelinek char zone_name[ZONENAME_MAX]; 42010209230bSgjelinek char tmp_name[MAX_TMP_POOL_NAME]; 42020209230bSgjelinek 42030209230bSgjelinek (void) getzonenamebyid(zoneid, zone_name, sizeof (zone_name)); 42040209230bSgjelinek 42050209230bSgjelinek err = zonecfg_lookup_pset(handle, &pset_tab); 42060209230bSgjelinek 42070209230bSgjelinek /* if no temporary pool configured, we're done */ 42080209230bSgjelinek if (err == Z_NO_ENTRY) 42090209230bSgjelinek return (Z_OK); 42100209230bSgjelinek 42110209230bSgjelinek /* 42120209230bSgjelinek * importance might not have a value but we need to validate it here, 42130209230bSgjelinek * so set the default. 42140209230bSgjelinek */ 42150209230bSgjelinek if (pset_tab.zone_importance[0] == '\0') 42160209230bSgjelinek (void) strlcpy(pset_tab.zone_importance, "1", 42170209230bSgjelinek sizeof (pset_tab.zone_importance)); 42180209230bSgjelinek 42190209230bSgjelinek /* if pools not enabled, enable them now */ 42200209230bSgjelinek if (pool_get_status(&status) != PO_SUCCESS || status != POOL_ENABLED) { 42210209230bSgjelinek if (pool_set_status(POOL_ENABLED) != PO_SUCCESS) 42220209230bSgjelinek return (Z_POOL_ENABLE); 42230209230bSgjelinek } 42240209230bSgjelinek 42250209230bSgjelinek if ((pconf = pool_conf_alloc()) == NULL) 42260209230bSgjelinek return (zerr_pool(pool_err, err_size, Z_POOL)); 42270209230bSgjelinek 42280209230bSgjelinek (void) snprintf(tmp_name, sizeof (tmp_name), TMP_POOL_NAME, zone_name); 42290209230bSgjelinek 42300209230bSgjelinek /* 42310209230bSgjelinek * Check if a valid tmp pool/pset already exists. If so, we just 42320209230bSgjelinek * reuse it. 42330209230bSgjelinek */ 42340209230bSgjelinek if ((err = verify_del_tmp_pool(pconf, tmp_name, pool_err, err_size, 42350209230bSgjelinek &pset_tab, &exists)) != Z_OK) { 42360209230bSgjelinek pool_conf_free(pconf); 42370209230bSgjelinek return (err); 42380209230bSgjelinek } 42390209230bSgjelinek 42400209230bSgjelinek if (!exists) 42410209230bSgjelinek err = create_tmp_pool(pool_err, err_size, pconf, tmp_name, 42420209230bSgjelinek &pset_tab); 42430209230bSgjelinek 42440209230bSgjelinek pool_conf_free(pconf); 42450209230bSgjelinek 42460209230bSgjelinek if (err != Z_OK) 42470209230bSgjelinek return (err); 42480209230bSgjelinek 42490209230bSgjelinek /* Bind the zone to the pool. */ 42500209230bSgjelinek if (pool_set_binding(tmp_name, P_ZONEID, zoneid) != PO_SUCCESS) 42510209230bSgjelinek return (zerr_pool(pool_err, err_size, Z_POOL_BIND)); 42520209230bSgjelinek 42530209230bSgjelinek return (Z_OK); 42540209230bSgjelinek } 42550209230bSgjelinek 42560209230bSgjelinek /* 42570209230bSgjelinek * Attempt to bind to a permanent pool for this zone. If there is no 42580209230bSgjelinek * permanent pool configured, we just return Z_OK. 42590209230bSgjelinek */ 42600209230bSgjelinek int 42610209230bSgjelinek zonecfg_bind_pool(zone_dochandle_t handle, zoneid_t zoneid, char *pool_err, 42620209230bSgjelinek int err_size) 42630209230bSgjelinek { 42640209230bSgjelinek pool_conf_t *poolconf; 42650209230bSgjelinek pool_t *pool; 42660209230bSgjelinek char poolname[MAXPATHLEN]; 42670209230bSgjelinek int status; 42680209230bSgjelinek int error; 42690209230bSgjelinek 42700209230bSgjelinek /* 42710209230bSgjelinek * Find the pool mentioned in the zone configuration, and bind to it. 42720209230bSgjelinek */ 42730209230bSgjelinek error = zonecfg_get_pool(handle, poolname, sizeof (poolname)); 42740209230bSgjelinek if (error == Z_NO_ENTRY || (error == Z_OK && strlen(poolname) == 0)) { 42750209230bSgjelinek /* 42760209230bSgjelinek * The property is not set on the zone, so the pool 42770209230bSgjelinek * should be bound to the default pool. But that's 42780209230bSgjelinek * already done by the kernel, so we can just return. 42790209230bSgjelinek */ 42800209230bSgjelinek return (Z_OK); 42810209230bSgjelinek } 42820209230bSgjelinek if (error != Z_OK) { 42830209230bSgjelinek /* 42840209230bSgjelinek * Not an error, even though it shouldn't be happening. 42850209230bSgjelinek */ 42860209230bSgjelinek return (Z_OK); 42870209230bSgjelinek } 42880209230bSgjelinek /* 42890209230bSgjelinek * Don't do anything if pools aren't enabled. 42900209230bSgjelinek */ 42910209230bSgjelinek if (pool_get_status(&status) != PO_SUCCESS || status != POOL_ENABLED) 42920209230bSgjelinek return (Z_POOLS_NOT_ACTIVE); 42930209230bSgjelinek 42940209230bSgjelinek /* 42950209230bSgjelinek * Try to provide a sane error message if the requested pool doesn't 42960209230bSgjelinek * exist. 42970209230bSgjelinek */ 42980209230bSgjelinek if ((poolconf = pool_conf_alloc()) == NULL) 42990209230bSgjelinek return (zerr_pool(pool_err, err_size, Z_POOL)); 43000209230bSgjelinek 43010209230bSgjelinek if (pool_conf_open(poolconf, pool_dynamic_location(), PO_RDONLY) != 43020209230bSgjelinek PO_SUCCESS) { 43030209230bSgjelinek pool_conf_free(poolconf); 43040209230bSgjelinek return (zerr_pool(pool_err, err_size, Z_POOL)); 43050209230bSgjelinek } 43060209230bSgjelinek pool = pool_get_pool(poolconf, poolname); 43070209230bSgjelinek (void) pool_conf_close(poolconf); 43080209230bSgjelinek pool_conf_free(poolconf); 43090209230bSgjelinek if (pool == NULL) 43100209230bSgjelinek return (Z_NO_POOL); 43110209230bSgjelinek 43120209230bSgjelinek /* 43130209230bSgjelinek * Bind the zone to the pool. 43140209230bSgjelinek */ 43150209230bSgjelinek if (pool_set_binding(poolname, P_ZONEID, zoneid) != PO_SUCCESS) { 43160209230bSgjelinek /* if bind fails, return poolname for the error msg */ 43170209230bSgjelinek (void) strlcpy(pool_err, poolname, err_size); 43180209230bSgjelinek return (Z_POOL_BIND); 43190209230bSgjelinek } 43200209230bSgjelinek 43210209230bSgjelinek return (Z_OK); 43220209230bSgjelinek } 43230209230bSgjelinek 43240209230bSgjelinek 43250209230bSgjelinek static boolean_t 43260209230bSgjelinek svc_enabled(char *svc_name) 43270209230bSgjelinek { 43280209230bSgjelinek scf_simple_prop_t *prop; 43290209230bSgjelinek boolean_t found = B_FALSE; 43300209230bSgjelinek 43310209230bSgjelinek prop = scf_simple_prop_get(NULL, svc_name, SCF_PG_GENERAL, 43320209230bSgjelinek SCF_PROPERTY_ENABLED); 43330209230bSgjelinek 43340209230bSgjelinek if (scf_simple_prop_numvalues(prop) == 1 && 43350209230bSgjelinek *scf_simple_prop_next_boolean(prop) != 0) 43360209230bSgjelinek found = B_TRUE; 43370209230bSgjelinek 43380209230bSgjelinek scf_simple_prop_free(prop); 43390209230bSgjelinek 43400209230bSgjelinek return (found); 43410209230bSgjelinek } 43420209230bSgjelinek 43430209230bSgjelinek /* 43440209230bSgjelinek * If the zone has capped-memory, make sure the rcap service is enabled. 43450209230bSgjelinek */ 43460209230bSgjelinek int 43470209230bSgjelinek zonecfg_enable_rcapd(char *err, int size) 43480209230bSgjelinek { 43490209230bSgjelinek if (!svc_enabled(RCAP_SERVICE) && 43500209230bSgjelinek smf_enable_instance(RCAP_SERVICE, 0) == -1) { 43510209230bSgjelinek (void) strlcpy(err, scf_strerror(scf_error()), size); 43520209230bSgjelinek return (Z_SYSTEM); 43530209230bSgjelinek } 43540209230bSgjelinek 43550209230bSgjelinek return (Z_OK); 43560209230bSgjelinek } 43570209230bSgjelinek 43580209230bSgjelinek /* 43590209230bSgjelinek * Return true if pset has cpu range specified and poold is not enabled. 43600209230bSgjelinek */ 43610209230bSgjelinek boolean_t 43620209230bSgjelinek zonecfg_warn_poold(zone_dochandle_t handle) 43630209230bSgjelinek { 43640209230bSgjelinek struct zone_psettab pset_tab; 43650209230bSgjelinek int min, max; 43660209230bSgjelinek int err; 43670209230bSgjelinek 43680209230bSgjelinek err = zonecfg_lookup_pset(handle, &pset_tab); 43690209230bSgjelinek 43700209230bSgjelinek /* if no temporary pool configured, we're done */ 43710209230bSgjelinek if (err == Z_NO_ENTRY) 43720209230bSgjelinek return (B_FALSE); 43730209230bSgjelinek 43740209230bSgjelinek min = atoi(pset_tab.zone_ncpu_min); 43750209230bSgjelinek max = atoi(pset_tab.zone_ncpu_max); 43760209230bSgjelinek 43770209230bSgjelinek /* range not specified, no need for poold */ 43780209230bSgjelinek if (min == max) 43790209230bSgjelinek return (B_FALSE); 43800209230bSgjelinek 43810209230bSgjelinek /* we have a range, check if poold service is enabled */ 43820209230bSgjelinek if (svc_enabled(POOLD_SERVICE)) 43830209230bSgjelinek return (B_FALSE); 43840209230bSgjelinek 43850209230bSgjelinek return (B_TRUE); 43860209230bSgjelinek } 43870209230bSgjelinek 4388*34eb7836Sjv227347 /* 4389*34eb7836Sjv227347 * Retrieve the specified pool's thread scheduling class. 'poolname' must 4390*34eb7836Sjv227347 * refer to the name of a configured resource pool. The thread scheduling 4391*34eb7836Sjv227347 * class specified by the pool will be stored in the buffer to which 'class' 4392*34eb7836Sjv227347 * points. 'clsize' is the byte size of the buffer to which 'class' points. 4393*34eb7836Sjv227347 * 4394*34eb7836Sjv227347 * This function returns Z_OK if it successfully stored the specified pool's 4395*34eb7836Sjv227347 * thread scheduling class into the buffer to which 'class' points. It returns 4396*34eb7836Sjv227347 * Z_NO_POOL if resource pools are not enabled, the function is unable to 4397*34eb7836Sjv227347 * access the system's resource pools configuration, or the specified pool 4398*34eb7836Sjv227347 * does not exist. The function returns Z_TOO_BIG if the buffer to which 4399*34eb7836Sjv227347 * 'class' points is not large enough to contain the thread scheduling class' 4400*34eb7836Sjv227347 * name. The function returns Z_NO_ENTRY if the pool does not specify a thread 4401*34eb7836Sjv227347 * scheduling class. 4402*34eb7836Sjv227347 */ 44030209230bSgjelinek static int 44040209230bSgjelinek get_pool_sched_class(char *poolname, char *class, int clsize) 44050209230bSgjelinek { 44060209230bSgjelinek int status; 44070209230bSgjelinek pool_conf_t *poolconf; 44080209230bSgjelinek pool_t *pool; 44090209230bSgjelinek pool_elem_t *pe; 44100209230bSgjelinek pool_value_t *pv = pool_value_alloc(); 44110209230bSgjelinek const char *sched_str; 44120209230bSgjelinek 44130209230bSgjelinek if (pool_get_status(&status) != PO_SUCCESS || status != POOL_ENABLED) 44140209230bSgjelinek return (Z_NO_POOL); 44150209230bSgjelinek 44160209230bSgjelinek if ((poolconf = pool_conf_alloc()) == NULL) 44170209230bSgjelinek return (Z_NO_POOL); 44180209230bSgjelinek 44190209230bSgjelinek if (pool_conf_open(poolconf, pool_dynamic_location(), PO_RDONLY) != 44200209230bSgjelinek PO_SUCCESS) { 44210209230bSgjelinek pool_conf_free(poolconf); 44220209230bSgjelinek return (Z_NO_POOL); 44230209230bSgjelinek } 44240209230bSgjelinek 44250209230bSgjelinek if ((pool = pool_get_pool(poolconf, poolname)) == NULL) { 44260209230bSgjelinek (void) pool_conf_close(poolconf); 44270209230bSgjelinek pool_conf_free(poolconf); 44280209230bSgjelinek return (Z_NO_POOL); 44290209230bSgjelinek } 44300209230bSgjelinek 44310209230bSgjelinek pe = pool_to_elem(poolconf, pool); 4432*34eb7836Sjv227347 if (pool_get_property(poolconf, pe, "pool.scheduler", pv) != 4433*34eb7836Sjv227347 POC_STRING) { 44340209230bSgjelinek (void) pool_conf_close(poolconf); 44350209230bSgjelinek pool_conf_free(poolconf); 4436*34eb7836Sjv227347 return (Z_NO_ENTRY); 4437*34eb7836Sjv227347 } 4438*34eb7836Sjv227347 (void) pool_value_get_string(pv, &sched_str); 4439*34eb7836Sjv227347 (void) pool_conf_close(poolconf); 4440*34eb7836Sjv227347 pool_conf_free(poolconf); 4441*34eb7836Sjv227347 if (strlcpy(class, sched_str, clsize) >= clsize) 4442*34eb7836Sjv227347 return (Z_TOO_BIG); 44430209230bSgjelinek return (Z_OK); 44440209230bSgjelinek } 44450209230bSgjelinek 44460209230bSgjelinek /* 44470209230bSgjelinek * Get the default scheduling class for the zone. This will either be the 44480209230bSgjelinek * class set on the zone's pool or the system default scheduling class. 44490209230bSgjelinek */ 44500209230bSgjelinek int 44510209230bSgjelinek zonecfg_get_dflt_sched_class(zone_dochandle_t handle, char *class, int clsize) 44520209230bSgjelinek { 44530209230bSgjelinek char poolname[MAXPATHLEN]; 44540209230bSgjelinek 44550209230bSgjelinek if (zonecfg_get_pool(handle, poolname, sizeof (poolname)) == Z_OK) { 44560209230bSgjelinek /* check if the zone's pool specified a sched class */ 44570209230bSgjelinek if (get_pool_sched_class(poolname, class, clsize) == Z_OK) 44580209230bSgjelinek return (Z_OK); 44590209230bSgjelinek } 44600209230bSgjelinek 44610209230bSgjelinek if (priocntl(0, 0, PC_GETDFLCL, class, (uint64_t)clsize) == -1) 44620209230bSgjelinek return (Z_TOO_BIG); 44630209230bSgjelinek 44640209230bSgjelinek return (Z_OK); 44650209230bSgjelinek } 44660209230bSgjelinek 44677c478bd9Sstevel@tonic-gate int 44687c478bd9Sstevel@tonic-gate zonecfg_setfsent(zone_dochandle_t handle) 44697c478bd9Sstevel@tonic-gate { 44707c478bd9Sstevel@tonic-gate return (zonecfg_setent(handle)); 44717c478bd9Sstevel@tonic-gate } 44727c478bd9Sstevel@tonic-gate 44737c478bd9Sstevel@tonic-gate int 44747c478bd9Sstevel@tonic-gate zonecfg_getfsent(zone_dochandle_t handle, struct zone_fstab *tabptr) 44757c478bd9Sstevel@tonic-gate { 44767c478bd9Sstevel@tonic-gate xmlNodePtr cur, options; 44777c478bd9Sstevel@tonic-gate char options_str[MAX_MNTOPT_STR]; 44787c478bd9Sstevel@tonic-gate int err; 44797c478bd9Sstevel@tonic-gate 44807c478bd9Sstevel@tonic-gate if (handle == NULL) 44817c478bd9Sstevel@tonic-gate return (Z_INVAL); 44827c478bd9Sstevel@tonic-gate 44837c478bd9Sstevel@tonic-gate if ((cur = handle->zone_dh_cur) == NULL) 44847c478bd9Sstevel@tonic-gate return (Z_NO_ENTRY); 44857c478bd9Sstevel@tonic-gate 44867c478bd9Sstevel@tonic-gate for (; cur != NULL; cur = cur->next) 44877c478bd9Sstevel@tonic-gate if (!xmlStrcmp(cur->name, DTD_ELEM_FS)) 44887c478bd9Sstevel@tonic-gate break; 44897c478bd9Sstevel@tonic-gate if (cur == NULL) { 44907c478bd9Sstevel@tonic-gate handle->zone_dh_cur = handle->zone_dh_top; 44917c478bd9Sstevel@tonic-gate return (Z_NO_ENTRY); 44927c478bd9Sstevel@tonic-gate } 44937c478bd9Sstevel@tonic-gate 44947c478bd9Sstevel@tonic-gate if ((err = fetchprop(cur, DTD_ATTR_SPECIAL, tabptr->zone_fs_special, 44957c478bd9Sstevel@tonic-gate sizeof (tabptr->zone_fs_special))) != Z_OK) { 44967c478bd9Sstevel@tonic-gate handle->zone_dh_cur = handle->zone_dh_top; 44977c478bd9Sstevel@tonic-gate return (err); 44987c478bd9Sstevel@tonic-gate } 44997c478bd9Sstevel@tonic-gate 45007c478bd9Sstevel@tonic-gate if ((err = fetchprop(cur, DTD_ATTR_RAW, tabptr->zone_fs_raw, 45017c478bd9Sstevel@tonic-gate sizeof (tabptr->zone_fs_raw))) != Z_OK) { 45027c478bd9Sstevel@tonic-gate handle->zone_dh_cur = handle->zone_dh_top; 45037c478bd9Sstevel@tonic-gate return (err); 45047c478bd9Sstevel@tonic-gate } 45057c478bd9Sstevel@tonic-gate 45067c478bd9Sstevel@tonic-gate if ((err = fetchprop(cur, DTD_ATTR_DIR, tabptr->zone_fs_dir, 45077c478bd9Sstevel@tonic-gate sizeof (tabptr->zone_fs_dir))) != Z_OK) { 45087c478bd9Sstevel@tonic-gate handle->zone_dh_cur = handle->zone_dh_top; 45097c478bd9Sstevel@tonic-gate return (err); 45107c478bd9Sstevel@tonic-gate } 45117c478bd9Sstevel@tonic-gate 45127c478bd9Sstevel@tonic-gate if ((err = fetchprop(cur, DTD_ATTR_TYPE, tabptr->zone_fs_type, 45137c478bd9Sstevel@tonic-gate sizeof (tabptr->zone_fs_type))) != Z_OK) { 45147c478bd9Sstevel@tonic-gate handle->zone_dh_cur = handle->zone_dh_top; 45157c478bd9Sstevel@tonic-gate return (err); 45167c478bd9Sstevel@tonic-gate } 45177c478bd9Sstevel@tonic-gate 45187c478bd9Sstevel@tonic-gate /* OK for options to be NULL */ 45197c478bd9Sstevel@tonic-gate tabptr->zone_fs_options = NULL; 45207c478bd9Sstevel@tonic-gate for (options = cur->xmlChildrenNode; options != NULL; 45217c478bd9Sstevel@tonic-gate options = options->next) { 45227c478bd9Sstevel@tonic-gate if (fetchprop(options, DTD_ATTR_NAME, options_str, 45237c478bd9Sstevel@tonic-gate sizeof (options_str)) != Z_OK) 45247c478bd9Sstevel@tonic-gate break; 45257c478bd9Sstevel@tonic-gate if (zonecfg_add_fs_option(tabptr, options_str) != Z_OK) 45267c478bd9Sstevel@tonic-gate break; 45277c478bd9Sstevel@tonic-gate } 45287c478bd9Sstevel@tonic-gate 45297c478bd9Sstevel@tonic-gate handle->zone_dh_cur = cur->next; 45307c478bd9Sstevel@tonic-gate return (Z_OK); 45317c478bd9Sstevel@tonic-gate } 45327c478bd9Sstevel@tonic-gate 45337c478bd9Sstevel@tonic-gate int 45347c478bd9Sstevel@tonic-gate zonecfg_endfsent(zone_dochandle_t handle) 45357c478bd9Sstevel@tonic-gate { 45367c478bd9Sstevel@tonic-gate return (zonecfg_endent(handle)); 45377c478bd9Sstevel@tonic-gate } 45387c478bd9Sstevel@tonic-gate 45397c478bd9Sstevel@tonic-gate int 45407c478bd9Sstevel@tonic-gate zonecfg_setipdent(zone_dochandle_t handle) 45417c478bd9Sstevel@tonic-gate { 45427c478bd9Sstevel@tonic-gate return (zonecfg_setent(handle)); 45437c478bd9Sstevel@tonic-gate } 45447c478bd9Sstevel@tonic-gate 45457c478bd9Sstevel@tonic-gate int 45467c478bd9Sstevel@tonic-gate zonecfg_getipdent(zone_dochandle_t handle, struct zone_fstab *tabptr) 45477c478bd9Sstevel@tonic-gate { 45487c478bd9Sstevel@tonic-gate xmlNodePtr cur; 45497c478bd9Sstevel@tonic-gate int err; 45507c478bd9Sstevel@tonic-gate 45517c478bd9Sstevel@tonic-gate if (handle == NULL) 45527c478bd9Sstevel@tonic-gate return (Z_INVAL); 45537c478bd9Sstevel@tonic-gate 45547c478bd9Sstevel@tonic-gate if ((cur = handle->zone_dh_cur) == NULL) 45557c478bd9Sstevel@tonic-gate return (Z_NO_ENTRY); 45567c478bd9Sstevel@tonic-gate 45577c478bd9Sstevel@tonic-gate for (; cur != NULL; cur = cur->next) 45587c478bd9Sstevel@tonic-gate if (!xmlStrcmp(cur->name, DTD_ELEM_IPD)) 45597c478bd9Sstevel@tonic-gate break; 45607c478bd9Sstevel@tonic-gate if (cur == NULL) { 45617c478bd9Sstevel@tonic-gate handle->zone_dh_cur = handle->zone_dh_top; 45627c478bd9Sstevel@tonic-gate return (Z_NO_ENTRY); 45637c478bd9Sstevel@tonic-gate } 45647c478bd9Sstevel@tonic-gate 45657c478bd9Sstevel@tonic-gate if ((err = fetchprop(cur, DTD_ATTR_DIR, tabptr->zone_fs_dir, 45667c478bd9Sstevel@tonic-gate sizeof (tabptr->zone_fs_dir))) != Z_OK) { 45677c478bd9Sstevel@tonic-gate handle->zone_dh_cur = handle->zone_dh_top; 45687c478bd9Sstevel@tonic-gate return (err); 45697c478bd9Sstevel@tonic-gate } 45707c478bd9Sstevel@tonic-gate 45717c478bd9Sstevel@tonic-gate handle->zone_dh_cur = cur->next; 45727c478bd9Sstevel@tonic-gate return (Z_OK); 45737c478bd9Sstevel@tonic-gate } 45747c478bd9Sstevel@tonic-gate 45757c478bd9Sstevel@tonic-gate int 45767c478bd9Sstevel@tonic-gate zonecfg_endipdent(zone_dochandle_t handle) 45777c478bd9Sstevel@tonic-gate { 45787c478bd9Sstevel@tonic-gate return (zonecfg_endent(handle)); 45797c478bd9Sstevel@tonic-gate } 45807c478bd9Sstevel@tonic-gate 45817c478bd9Sstevel@tonic-gate int 45827c478bd9Sstevel@tonic-gate zonecfg_setnwifent(zone_dochandle_t handle) 45837c478bd9Sstevel@tonic-gate { 45847c478bd9Sstevel@tonic-gate return (zonecfg_setent(handle)); 45857c478bd9Sstevel@tonic-gate } 45867c478bd9Sstevel@tonic-gate 45877c478bd9Sstevel@tonic-gate int 45887c478bd9Sstevel@tonic-gate zonecfg_getnwifent(zone_dochandle_t handle, struct zone_nwiftab *tabptr) 45897c478bd9Sstevel@tonic-gate { 45907c478bd9Sstevel@tonic-gate xmlNodePtr cur; 45917c478bd9Sstevel@tonic-gate int err; 45927c478bd9Sstevel@tonic-gate 45937c478bd9Sstevel@tonic-gate if (handle == NULL) 45947c478bd9Sstevel@tonic-gate return (Z_INVAL); 45957c478bd9Sstevel@tonic-gate 45967c478bd9Sstevel@tonic-gate if ((cur = handle->zone_dh_cur) == NULL) 45977c478bd9Sstevel@tonic-gate return (Z_NO_ENTRY); 45987c478bd9Sstevel@tonic-gate 45997c478bd9Sstevel@tonic-gate for (; cur != NULL; cur = cur->next) 46007c478bd9Sstevel@tonic-gate if (!xmlStrcmp(cur->name, DTD_ELEM_NET)) 46017c478bd9Sstevel@tonic-gate break; 46027c478bd9Sstevel@tonic-gate if (cur == NULL) { 46037c478bd9Sstevel@tonic-gate handle->zone_dh_cur = handle->zone_dh_top; 46047c478bd9Sstevel@tonic-gate return (Z_NO_ENTRY); 46057c478bd9Sstevel@tonic-gate } 46067c478bd9Sstevel@tonic-gate 46077c478bd9Sstevel@tonic-gate if ((err = fetchprop(cur, DTD_ATTR_ADDRESS, tabptr->zone_nwif_address, 46087c478bd9Sstevel@tonic-gate sizeof (tabptr->zone_nwif_address))) != Z_OK) { 46097c478bd9Sstevel@tonic-gate handle->zone_dh_cur = handle->zone_dh_top; 46107c478bd9Sstevel@tonic-gate return (err); 46117c478bd9Sstevel@tonic-gate } 46127c478bd9Sstevel@tonic-gate 46137c478bd9Sstevel@tonic-gate if ((err = fetchprop(cur, DTD_ATTR_PHYSICAL, tabptr->zone_nwif_physical, 46147c478bd9Sstevel@tonic-gate sizeof (tabptr->zone_nwif_physical))) != Z_OK) { 46157c478bd9Sstevel@tonic-gate handle->zone_dh_cur = handle->zone_dh_top; 46167c478bd9Sstevel@tonic-gate return (err); 46177c478bd9Sstevel@tonic-gate } 46187c478bd9Sstevel@tonic-gate 4619de860bd9Sgfaden if ((err = fetchprop(cur, DTD_ATTR_DEFROUTER, 4620de860bd9Sgfaden tabptr->zone_nwif_defrouter, 4621de860bd9Sgfaden sizeof (tabptr->zone_nwif_defrouter))) != Z_OK) { 4622de860bd9Sgfaden handle->zone_dh_cur = handle->zone_dh_top; 4623de860bd9Sgfaden return (err); 4624de860bd9Sgfaden } 4625de860bd9Sgfaden 46267c478bd9Sstevel@tonic-gate handle->zone_dh_cur = cur->next; 46277c478bd9Sstevel@tonic-gate return (Z_OK); 46287c478bd9Sstevel@tonic-gate } 46297c478bd9Sstevel@tonic-gate 46307c478bd9Sstevel@tonic-gate int 46317c478bd9Sstevel@tonic-gate zonecfg_endnwifent(zone_dochandle_t handle) 46327c478bd9Sstevel@tonic-gate { 46337c478bd9Sstevel@tonic-gate return (zonecfg_endent(handle)); 46347c478bd9Sstevel@tonic-gate } 46357c478bd9Sstevel@tonic-gate 46367c478bd9Sstevel@tonic-gate int 46377c478bd9Sstevel@tonic-gate zonecfg_setdevent(zone_dochandle_t handle) 46387c478bd9Sstevel@tonic-gate { 46397c478bd9Sstevel@tonic-gate return (zonecfg_setent(handle)); 46407c478bd9Sstevel@tonic-gate } 46417c478bd9Sstevel@tonic-gate 46427c478bd9Sstevel@tonic-gate int 46437c478bd9Sstevel@tonic-gate zonecfg_getdevent(zone_dochandle_t handle, struct zone_devtab *tabptr) 46447c478bd9Sstevel@tonic-gate { 46457c478bd9Sstevel@tonic-gate xmlNodePtr cur; 46467c478bd9Sstevel@tonic-gate int err; 46477c478bd9Sstevel@tonic-gate 46487c478bd9Sstevel@tonic-gate if (handle == NULL) 46497c478bd9Sstevel@tonic-gate return (Z_INVAL); 46507c478bd9Sstevel@tonic-gate 46517c478bd9Sstevel@tonic-gate if ((cur = handle->zone_dh_cur) == NULL) 46527c478bd9Sstevel@tonic-gate return (Z_NO_ENTRY); 46537c478bd9Sstevel@tonic-gate 46547c478bd9Sstevel@tonic-gate for (; cur != NULL; cur = cur->next) 46557c478bd9Sstevel@tonic-gate if (!xmlStrcmp(cur->name, DTD_ELEM_DEVICE)) 46567c478bd9Sstevel@tonic-gate break; 46577c478bd9Sstevel@tonic-gate if (cur == NULL) { 46587c478bd9Sstevel@tonic-gate handle->zone_dh_cur = handle->zone_dh_top; 46597c478bd9Sstevel@tonic-gate return (Z_NO_ENTRY); 46607c478bd9Sstevel@tonic-gate } 46617c478bd9Sstevel@tonic-gate 46627c478bd9Sstevel@tonic-gate if ((err = fetchprop(cur, DTD_ATTR_MATCH, tabptr->zone_dev_match, 46637c478bd9Sstevel@tonic-gate sizeof (tabptr->zone_dev_match))) != Z_OK) { 46647c478bd9Sstevel@tonic-gate handle->zone_dh_cur = handle->zone_dh_top; 46657c478bd9Sstevel@tonic-gate return (err); 46667c478bd9Sstevel@tonic-gate } 46677c478bd9Sstevel@tonic-gate 46687c478bd9Sstevel@tonic-gate handle->zone_dh_cur = cur->next; 46697c478bd9Sstevel@tonic-gate return (Z_OK); 46707c478bd9Sstevel@tonic-gate } 46717c478bd9Sstevel@tonic-gate 46727c478bd9Sstevel@tonic-gate int 46737c478bd9Sstevel@tonic-gate zonecfg_enddevent(zone_dochandle_t handle) 46747c478bd9Sstevel@tonic-gate { 46757c478bd9Sstevel@tonic-gate return (zonecfg_endent(handle)); 46767c478bd9Sstevel@tonic-gate } 46777c478bd9Sstevel@tonic-gate 46787c478bd9Sstevel@tonic-gate int 46797c478bd9Sstevel@tonic-gate zonecfg_setrctlent(zone_dochandle_t handle) 46807c478bd9Sstevel@tonic-gate { 46817c478bd9Sstevel@tonic-gate return (zonecfg_setent(handle)); 46827c478bd9Sstevel@tonic-gate } 46837c478bd9Sstevel@tonic-gate 46847c478bd9Sstevel@tonic-gate int 46857c478bd9Sstevel@tonic-gate zonecfg_getrctlent(zone_dochandle_t handle, struct zone_rctltab *tabptr) 46867c478bd9Sstevel@tonic-gate { 46877c478bd9Sstevel@tonic-gate xmlNodePtr cur, val; 46887c478bd9Sstevel@tonic-gate struct zone_rctlvaltab *valptr; 46897c478bd9Sstevel@tonic-gate int err; 46907c478bd9Sstevel@tonic-gate 46917c478bd9Sstevel@tonic-gate if (handle == NULL) 46927c478bd9Sstevel@tonic-gate return (Z_INVAL); 46937c478bd9Sstevel@tonic-gate 46947c478bd9Sstevel@tonic-gate if ((cur = handle->zone_dh_cur) == NULL) 46957c478bd9Sstevel@tonic-gate return (Z_NO_ENTRY); 46967c478bd9Sstevel@tonic-gate 46977c478bd9Sstevel@tonic-gate for (; cur != NULL; cur = cur->next) 46987c478bd9Sstevel@tonic-gate if (!xmlStrcmp(cur->name, DTD_ELEM_RCTL)) 46997c478bd9Sstevel@tonic-gate break; 47007c478bd9Sstevel@tonic-gate if (cur == NULL) { 47017c478bd9Sstevel@tonic-gate handle->zone_dh_cur = handle->zone_dh_top; 47027c478bd9Sstevel@tonic-gate return (Z_NO_ENTRY); 47037c478bd9Sstevel@tonic-gate } 47047c478bd9Sstevel@tonic-gate 47057c478bd9Sstevel@tonic-gate if ((err = fetchprop(cur, DTD_ATTR_NAME, tabptr->zone_rctl_name, 47067c478bd9Sstevel@tonic-gate sizeof (tabptr->zone_rctl_name))) != Z_OK) { 47077c478bd9Sstevel@tonic-gate handle->zone_dh_cur = handle->zone_dh_top; 47087c478bd9Sstevel@tonic-gate return (err); 47097c478bd9Sstevel@tonic-gate } 47107c478bd9Sstevel@tonic-gate 47117c478bd9Sstevel@tonic-gate tabptr->zone_rctl_valptr = NULL; 47127c478bd9Sstevel@tonic-gate for (val = cur->xmlChildrenNode; val != NULL; val = val->next) { 47137c478bd9Sstevel@tonic-gate valptr = (struct zone_rctlvaltab *)malloc( 47147c478bd9Sstevel@tonic-gate sizeof (struct zone_rctlvaltab)); 47157c478bd9Sstevel@tonic-gate if (valptr == NULL) 47167c478bd9Sstevel@tonic-gate return (Z_NOMEM); 47177c478bd9Sstevel@tonic-gate if (fetchprop(val, DTD_ATTR_PRIV, valptr->zone_rctlval_priv, 47187c478bd9Sstevel@tonic-gate sizeof (valptr->zone_rctlval_priv)) != Z_OK) 47197c478bd9Sstevel@tonic-gate break; 47207c478bd9Sstevel@tonic-gate if (fetchprop(val, DTD_ATTR_LIMIT, valptr->zone_rctlval_limit, 47217c478bd9Sstevel@tonic-gate sizeof (valptr->zone_rctlval_limit)) != Z_OK) 47227c478bd9Sstevel@tonic-gate break; 47237c478bd9Sstevel@tonic-gate if (fetchprop(val, DTD_ATTR_ACTION, valptr->zone_rctlval_action, 47247c478bd9Sstevel@tonic-gate sizeof (valptr->zone_rctlval_action)) != Z_OK) 47257c478bd9Sstevel@tonic-gate break; 47267c478bd9Sstevel@tonic-gate if (zonecfg_add_rctl_value(tabptr, valptr) != Z_OK) 47277c478bd9Sstevel@tonic-gate break; 47287c478bd9Sstevel@tonic-gate } 47297c478bd9Sstevel@tonic-gate 47307c478bd9Sstevel@tonic-gate handle->zone_dh_cur = cur->next; 47317c478bd9Sstevel@tonic-gate return (Z_OK); 47327c478bd9Sstevel@tonic-gate } 47337c478bd9Sstevel@tonic-gate 47347c478bd9Sstevel@tonic-gate int 47357c478bd9Sstevel@tonic-gate zonecfg_endrctlent(zone_dochandle_t handle) 47367c478bd9Sstevel@tonic-gate { 47377c478bd9Sstevel@tonic-gate return (zonecfg_endent(handle)); 47387c478bd9Sstevel@tonic-gate } 47397c478bd9Sstevel@tonic-gate 47407c478bd9Sstevel@tonic-gate int 47417c478bd9Sstevel@tonic-gate zonecfg_setattrent(zone_dochandle_t handle) 47427c478bd9Sstevel@tonic-gate { 47437c478bd9Sstevel@tonic-gate return (zonecfg_setent(handle)); 47447c478bd9Sstevel@tonic-gate } 47457c478bd9Sstevel@tonic-gate 47467c478bd9Sstevel@tonic-gate int 47477c478bd9Sstevel@tonic-gate zonecfg_getattrent(zone_dochandle_t handle, struct zone_attrtab *tabptr) 47487c478bd9Sstevel@tonic-gate { 47497c478bd9Sstevel@tonic-gate xmlNodePtr cur; 47507c478bd9Sstevel@tonic-gate int err; 47517c478bd9Sstevel@tonic-gate 47527c478bd9Sstevel@tonic-gate if (handle == NULL) 47537c478bd9Sstevel@tonic-gate return (Z_INVAL); 47547c478bd9Sstevel@tonic-gate 47557c478bd9Sstevel@tonic-gate if ((cur = handle->zone_dh_cur) == NULL) 47567c478bd9Sstevel@tonic-gate return (Z_NO_ENTRY); 47577c478bd9Sstevel@tonic-gate 47587c478bd9Sstevel@tonic-gate for (; cur != NULL; cur = cur->next) 47597c478bd9Sstevel@tonic-gate if (!xmlStrcmp(cur->name, DTD_ELEM_ATTR)) 47607c478bd9Sstevel@tonic-gate break; 47617c478bd9Sstevel@tonic-gate if (cur == NULL) { 47627c478bd9Sstevel@tonic-gate handle->zone_dh_cur = handle->zone_dh_top; 47637c478bd9Sstevel@tonic-gate return (Z_NO_ENTRY); 47647c478bd9Sstevel@tonic-gate } 47657c478bd9Sstevel@tonic-gate 47667c478bd9Sstevel@tonic-gate if ((err = fetchprop(cur, DTD_ATTR_NAME, tabptr->zone_attr_name, 47677c478bd9Sstevel@tonic-gate sizeof (tabptr->zone_attr_name))) != Z_OK) { 47687c478bd9Sstevel@tonic-gate handle->zone_dh_cur = handle->zone_dh_top; 47697c478bd9Sstevel@tonic-gate return (err); 47707c478bd9Sstevel@tonic-gate } 47717c478bd9Sstevel@tonic-gate 47727c478bd9Sstevel@tonic-gate if ((err = fetchprop(cur, DTD_ATTR_TYPE, tabptr->zone_attr_type, 47737c478bd9Sstevel@tonic-gate sizeof (tabptr->zone_attr_type))) != Z_OK) { 47747c478bd9Sstevel@tonic-gate handle->zone_dh_cur = handle->zone_dh_top; 47757c478bd9Sstevel@tonic-gate return (err); 47767c478bd9Sstevel@tonic-gate } 47777c478bd9Sstevel@tonic-gate 47787c478bd9Sstevel@tonic-gate if ((err = fetchprop(cur, DTD_ATTR_VALUE, tabptr->zone_attr_value, 47797c478bd9Sstevel@tonic-gate sizeof (tabptr->zone_attr_value))) != Z_OK) { 47807c478bd9Sstevel@tonic-gate handle->zone_dh_cur = handle->zone_dh_top; 47817c478bd9Sstevel@tonic-gate return (err); 47827c478bd9Sstevel@tonic-gate } 47837c478bd9Sstevel@tonic-gate 47847c478bd9Sstevel@tonic-gate handle->zone_dh_cur = cur->next; 47857c478bd9Sstevel@tonic-gate return (Z_OK); 47867c478bd9Sstevel@tonic-gate } 47877c478bd9Sstevel@tonic-gate 47887c478bd9Sstevel@tonic-gate int 47897c478bd9Sstevel@tonic-gate zonecfg_endattrent(zone_dochandle_t handle) 47907c478bd9Sstevel@tonic-gate { 47917c478bd9Sstevel@tonic-gate return (zonecfg_endent(handle)); 47927c478bd9Sstevel@tonic-gate } 47937c478bd9Sstevel@tonic-gate 4794ffbafc53Scomay /* 4795ffbafc53Scomay * The privileges available on the system and described in privileges(5) 47969acbbeafSnn35248 * fall into four categories with respect to non-global zones: 47979acbbeafSnn35248 * 47989acbbeafSnn35248 * Default set of privileges considered safe for all non-global 47999acbbeafSnn35248 * zones. These privileges are "safe" in the sense that a 48009acbbeafSnn35248 * privileged process in the zone cannot affect processes in any 48019acbbeafSnn35248 * other zone on the system. 48029acbbeafSnn35248 * 48039acbbeafSnn35248 * Set of privileges not currently permitted within a non-global 48049acbbeafSnn35248 * zone. These privileges are considered by default, "unsafe," 48059acbbeafSnn35248 * and include ones which affect global resources (such as the 48069acbbeafSnn35248 * system clock or physical memory) or are overly broad and cover 48079acbbeafSnn35248 * more than one mechanism in the system. In other cases, there 48089acbbeafSnn35248 * has not been sufficient virtualization in the parts of the 48099acbbeafSnn35248 * system the privilege covers to allow its use within a 48109acbbeafSnn35248 * non-global zone. 48119acbbeafSnn35248 * 48129acbbeafSnn35248 * Set of privileges required in order to get a zone booted and 48139acbbeafSnn35248 * init(1M) started. These cannot be removed from the zone's 48149acbbeafSnn35248 * privilege set. 48159acbbeafSnn35248 * 48169acbbeafSnn35248 * All other privileges are optional and are potentially useful for 4817ffbafc53Scomay * processes executing inside a non-global zone. 4818ffbafc53Scomay * 4819ffbafc53Scomay * When privileges are added to the system, a determination needs to be 4820ffbafc53Scomay * made as to which category the privilege belongs to. Ideally, 4821ffbafc53Scomay * privileges should be fine-grained enough and the mechanisms they cover 4822ffbafc53Scomay * virtualized enough so that they can be made available to non-global 4823ffbafc53Scomay * zones. 4824ffbafc53Scomay */ 4825ffbafc53Scomay 4826ffbafc53Scomay /* 4827ffbafc53Scomay * Define some of the tokens that priv_str_to_set(3C) recognizes. Since 4828ffbafc53Scomay * the privilege string separator can be any character, although it is 4829ffbafc53Scomay * usually a comma character, define these here as well in the event that 4830ffbafc53Scomay * they change or are augmented in the future. 4831ffbafc53Scomay */ 4832ffbafc53Scomay #define BASIC_TOKEN "basic" 4833ffbafc53Scomay #define DEFAULT_TOKEN "default" 4834ffbafc53Scomay #define ZONE_TOKEN "zone" 4835ffbafc53Scomay #define TOKEN_PRIV_CHAR ',' 4836ffbafc53Scomay #define TOKEN_PRIV_STR "," 4837ffbafc53Scomay 48389acbbeafSnn35248 typedef struct priv_node { 48399acbbeafSnn35248 struct priv_node *pn_next; /* Next privilege */ 48409acbbeafSnn35248 char *pn_priv; /* Privileges name */ 48419acbbeafSnn35248 } priv_node_t; 48429acbbeafSnn35248 48439acbbeafSnn35248 /* Privileges lists can differ across brands */ 48449acbbeafSnn35248 typedef struct priv_lists { 48459acbbeafSnn35248 /* Privileges considered safe for all non-global zones of a brand */ 48469acbbeafSnn35248 struct priv_node *pl_default; 48479acbbeafSnn35248 48489acbbeafSnn35248 /* Privileges not permitted for all non-global zones of a brand */ 48499acbbeafSnn35248 struct priv_node *pl_prohibited; 48509acbbeafSnn35248 48519acbbeafSnn35248 /* Privileges required for all non-global zones of a brand */ 48529acbbeafSnn35248 struct priv_node *pl_required; 4853bf1d7e28Sdh155122 4854bf1d7e28Sdh155122 /* 4855bf1d7e28Sdh155122 * ip-type of the zone these privileges lists apply to. 4856bf1d7e28Sdh155122 * It is used to pass ip-type to the callback function, 4857bf1d7e28Sdh155122 * priv_lists_cb, which has no way of getting the ip-type. 4858bf1d7e28Sdh155122 */ 4859bf1d7e28Sdh155122 const char *pl_iptype; 48609acbbeafSnn35248 } priv_lists_t; 48619acbbeafSnn35248 48629acbbeafSnn35248 static int 4863bf1d7e28Sdh155122 priv_lists_cb(void *data, priv_iter_t *priv_iter) 48647c478bd9Sstevel@tonic-gate { 48659acbbeafSnn35248 priv_lists_t *plp = (priv_lists_t *)data; 48669acbbeafSnn35248 priv_node_t *pnp; 48679acbbeafSnn35248 4868bf1d7e28Sdh155122 /* Skip this privilege if ip-type does not match */ 4869bf1d7e28Sdh155122 if ((strcmp(priv_iter->pi_iptype, "all") != 0) && 4870bf1d7e28Sdh155122 (strcmp(priv_iter->pi_iptype, plp->pl_iptype) != 0)) 4871bf1d7e28Sdh155122 return (0); 4872bf1d7e28Sdh155122 48739acbbeafSnn35248 /* Allocate a new priv list node. */ 48749acbbeafSnn35248 if ((pnp = malloc(sizeof (*pnp))) == NULL) 48759acbbeafSnn35248 return (-1); 4876bf1d7e28Sdh155122 if ((pnp->pn_priv = strdup(priv_iter->pi_name)) == NULL) { 48779acbbeafSnn35248 free(pnp); 48789acbbeafSnn35248 return (-1); 48799acbbeafSnn35248 } 48809acbbeafSnn35248 48819acbbeafSnn35248 /* Insert the new priv list node into the right list */ 4882bf1d7e28Sdh155122 if (strcmp(priv_iter->pi_set, "default") == 0) { 48839acbbeafSnn35248 pnp->pn_next = plp->pl_default; 48849acbbeafSnn35248 plp->pl_default = pnp; 4885bf1d7e28Sdh155122 } else if (strcmp(priv_iter->pi_set, "prohibited") == 0) { 48869acbbeafSnn35248 pnp->pn_next = plp->pl_prohibited; 48879acbbeafSnn35248 plp->pl_prohibited = pnp; 4888bf1d7e28Sdh155122 } else if (strcmp(priv_iter->pi_set, "required") == 0) { 48899acbbeafSnn35248 pnp->pn_next = plp->pl_required; 48909acbbeafSnn35248 plp->pl_required = pnp; 48919acbbeafSnn35248 } else { 48929acbbeafSnn35248 free(pnp->pn_priv); 48939acbbeafSnn35248 free(pnp); 48949acbbeafSnn35248 return (-1); 48959acbbeafSnn35248 } 48969acbbeafSnn35248 return (0); 48979acbbeafSnn35248 } 48989acbbeafSnn35248 48999acbbeafSnn35248 static void 49009acbbeafSnn35248 priv_lists_destroy(priv_lists_t *plp) 49019acbbeafSnn35248 { 49029acbbeafSnn35248 priv_node_t *pnp; 49039acbbeafSnn35248 49049acbbeafSnn35248 assert(plp != NULL); 49059acbbeafSnn35248 49069acbbeafSnn35248 while ((pnp = plp->pl_default) != NULL) { 49079acbbeafSnn35248 plp->pl_default = pnp->pn_next; 49089acbbeafSnn35248 free(pnp->pn_priv); 49099acbbeafSnn35248 free(pnp); 49109acbbeafSnn35248 } 49119acbbeafSnn35248 while ((pnp = plp->pl_prohibited) != NULL) { 49129acbbeafSnn35248 plp->pl_prohibited = pnp->pn_next; 49139acbbeafSnn35248 free(pnp->pn_priv); 49149acbbeafSnn35248 free(pnp); 49159acbbeafSnn35248 } 49169acbbeafSnn35248 while ((pnp = plp->pl_required) != NULL) { 49179acbbeafSnn35248 plp->pl_required = pnp->pn_next; 49189acbbeafSnn35248 free(pnp->pn_priv); 49199acbbeafSnn35248 free(pnp); 49209acbbeafSnn35248 } 49219acbbeafSnn35248 free(plp); 49229acbbeafSnn35248 } 49239acbbeafSnn35248 49249acbbeafSnn35248 static int 4925e5816e35SEdward Pilatowicz priv_lists_create(zone_dochandle_t handle, char *brand, priv_lists_t **plpp, 4926bf1d7e28Sdh155122 const char *curr_iptype) 49279acbbeafSnn35248 { 49289acbbeafSnn35248 priv_lists_t *plp; 4929123807fbSedp brand_handle_t bh; 4930e5816e35SEdward Pilatowicz char brand_str[MAXNAMELEN]; 4931e5816e35SEdward Pilatowicz 4932e5816e35SEdward Pilatowicz /* handle or brand must be set, but never both */ 4933e5816e35SEdward Pilatowicz assert((handle != NULL) || (brand != NULL)); 4934e5816e35SEdward Pilatowicz assert((handle == NULL) || (brand == NULL)); 49359acbbeafSnn35248 49369acbbeafSnn35248 if (handle != NULL) { 4937e5816e35SEdward Pilatowicz brand = brand_str; 4938e5816e35SEdward Pilatowicz if (zonecfg_get_brand(handle, brand, sizeof (brand_str)) != 0) 49399acbbeafSnn35248 return (Z_BRAND_ERROR); 49409acbbeafSnn35248 } 49419acbbeafSnn35248 4942123807fbSedp if ((bh = brand_open(brand)) == NULL) 49439acbbeafSnn35248 return (Z_BRAND_ERROR); 49449acbbeafSnn35248 49459acbbeafSnn35248 if ((plp = calloc(1, sizeof (priv_lists_t))) == NULL) { 4946123807fbSedp brand_close(bh); 49479acbbeafSnn35248 return (Z_NOMEM); 49489acbbeafSnn35248 } 49499acbbeafSnn35248 4950bf1d7e28Sdh155122 plp->pl_iptype = curr_iptype; 4951bf1d7e28Sdh155122 49529acbbeafSnn35248 /* construct the privilege lists */ 4953123807fbSedp if (brand_config_iter_privilege(bh, priv_lists_cb, plp) != 0) { 49549acbbeafSnn35248 priv_lists_destroy(plp); 4955123807fbSedp brand_close(bh); 49569acbbeafSnn35248 return (Z_BRAND_ERROR); 49579acbbeafSnn35248 } 49589acbbeafSnn35248 4959123807fbSedp brand_close(bh); 49609acbbeafSnn35248 *plpp = plp; 49619acbbeafSnn35248 return (Z_OK); 49629acbbeafSnn35248 } 49639acbbeafSnn35248 49649acbbeafSnn35248 static int 49659acbbeafSnn35248 get_default_privset(priv_set_t *privs, priv_lists_t *plp) 49669acbbeafSnn35248 { 49679acbbeafSnn35248 priv_node_t *pnp; 4968ffbafc53Scomay priv_set_t *basic; 49697c478bd9Sstevel@tonic-gate 4970ffbafc53Scomay basic = priv_str_to_set(BASIC_TOKEN, TOKEN_PRIV_STR, NULL); 49717c478bd9Sstevel@tonic-gate if (basic == NULL) 4972ffbafc53Scomay return (errno == ENOMEM ? Z_NOMEM : Z_INVAL); 49737c478bd9Sstevel@tonic-gate 49747c478bd9Sstevel@tonic-gate priv_union(basic, privs); 49757c478bd9Sstevel@tonic-gate priv_freeset(basic); 49767c478bd9Sstevel@tonic-gate 49779acbbeafSnn35248 for (pnp = plp->pl_default; pnp != NULL; pnp = pnp->pn_next) { 49789acbbeafSnn35248 if (priv_addset(privs, pnp->pn_priv) != 0) 49797c478bd9Sstevel@tonic-gate return (Z_INVAL); 49807c478bd9Sstevel@tonic-gate } 49819acbbeafSnn35248 49827c478bd9Sstevel@tonic-gate return (Z_OK); 49837c478bd9Sstevel@tonic-gate } 49847c478bd9Sstevel@tonic-gate 49859acbbeafSnn35248 int 4986e5816e35SEdward Pilatowicz zonecfg_default_brand(char *brand, size_t brandsize) 4987e5816e35SEdward Pilatowicz { 4988e5816e35SEdward Pilatowicz zone_dochandle_t handle; 49897522f2fcSEdward Pilatowicz int myzoneid = getzoneid(); 4990e5816e35SEdward Pilatowicz int ret; 4991e5816e35SEdward Pilatowicz 49927522f2fcSEdward Pilatowicz /* 49937522f2fcSEdward Pilatowicz * If we're running within a zone, then the default brand is the 49947522f2fcSEdward Pilatowicz * current zone's brand. 49957522f2fcSEdward Pilatowicz */ 49967522f2fcSEdward Pilatowicz if (myzoneid != GLOBAL_ZONEID) { 49977522f2fcSEdward Pilatowicz ret = zone_getattr(myzoneid, ZONE_ATTR_BRAND, brand, brandsize); 49987522f2fcSEdward Pilatowicz if (ret < 0) 49997522f2fcSEdward Pilatowicz return ((errno == EFAULT) ? Z_TOO_BIG : Z_INVAL); 50007522f2fcSEdward Pilatowicz return (Z_OK); 50017522f2fcSEdward Pilatowicz } 50027522f2fcSEdward Pilatowicz 5003e5816e35SEdward Pilatowicz if ((handle = zonecfg_init_handle()) == NULL) 5004e5816e35SEdward Pilatowicz return (Z_NOMEM); 5005e5816e35SEdward Pilatowicz if ((ret = zonecfg_get_handle("SUNWdefault", handle)) == Z_OK) { 5006e5816e35SEdward Pilatowicz ret = i_zonecfg_get_brand(handle, brand, brandsize, B_TRUE); 5007e5816e35SEdward Pilatowicz zonecfg_fini_handle(handle); 5008e5816e35SEdward Pilatowicz return (ret); 5009e5816e35SEdward Pilatowicz } 5010e5816e35SEdward Pilatowicz return (ret); 5011e5816e35SEdward Pilatowicz } 5012e5816e35SEdward Pilatowicz 5013e5816e35SEdward Pilatowicz int 5014bf1d7e28Sdh155122 zonecfg_default_privset(priv_set_t *privs, const char *curr_iptype) 50159acbbeafSnn35248 { 50169acbbeafSnn35248 priv_lists_t *plp; 5017e5816e35SEdward Pilatowicz char buf[MAXNAMELEN]; 50189acbbeafSnn35248 int ret; 50199acbbeafSnn35248 5020e5816e35SEdward Pilatowicz if ((ret = zonecfg_default_brand(buf, sizeof (buf))) != Z_OK) 5021e5816e35SEdward Pilatowicz return (ret); 5022e5816e35SEdward Pilatowicz if ((ret = priv_lists_create(NULL, buf, &plp, curr_iptype)) != Z_OK) 50239acbbeafSnn35248 return (ret); 50249acbbeafSnn35248 ret = get_default_privset(privs, plp); 50259acbbeafSnn35248 priv_lists_destroy(plp); 50269acbbeafSnn35248 return (ret); 50279acbbeafSnn35248 } 50289acbbeafSnn35248 5029ffbafc53Scomay void 5030ffbafc53Scomay append_priv_token(char *priv, char *str, size_t strlen) 5031ffbafc53Scomay { 5032ffbafc53Scomay if (*str != '\0') 5033ffbafc53Scomay (void) strlcat(str, TOKEN_PRIV_STR, strlen); 5034ffbafc53Scomay (void) strlcat(str, priv, strlen); 5035ffbafc53Scomay } 5036ffbafc53Scomay 5037ffbafc53Scomay /* 5038ffbafc53Scomay * Verify that the supplied string is a valid privilege limit set for a 5039ffbafc53Scomay * non-global zone. This string must not only be acceptable to 5040ffbafc53Scomay * priv_str_to_set(3C) which parses it, but it also must resolve to a 5041ffbafc53Scomay * privilege set that includes certain required privileges and lacks 5042ffbafc53Scomay * certain prohibited privileges. 5043ffbafc53Scomay */ 5044ffbafc53Scomay static int 5045ffbafc53Scomay verify_privset(char *privbuf, priv_set_t *privs, char **privname, 50469acbbeafSnn35248 boolean_t add_default, priv_lists_t *plp) 5047ffbafc53Scomay { 50489acbbeafSnn35248 priv_node_t *pnp; 50499acbbeafSnn35248 char *tmp, *cp, *lasts; 5050ffbafc53Scomay size_t len; 5051ffbafc53Scomay priv_set_t *mergeset; 5052ffbafc53Scomay const char *token; 5053ffbafc53Scomay 5054ffbafc53Scomay /* 5055ffbafc53Scomay * The verification of the privilege string occurs in several 5056ffbafc53Scomay * phases. In the first phase, the supplied string is scanned for 5057ffbafc53Scomay * the ZONE_TOKEN token which is not support as part of the 5058ffbafc53Scomay * "limitpriv" property. 5059ffbafc53Scomay * 5060ffbafc53Scomay * Duplicate the supplied privilege string since strtok_r(3C) 5061ffbafc53Scomay * tokenizes its input by null-terminating the tokens. 5062ffbafc53Scomay */ 5063ffbafc53Scomay if ((tmp = strdup(privbuf)) == NULL) 5064ffbafc53Scomay return (Z_NOMEM); 5065ffbafc53Scomay for (cp = strtok_r(tmp, TOKEN_PRIV_STR, &lasts); cp != NULL; 5066ffbafc53Scomay cp = strtok_r(NULL, TOKEN_PRIV_STR, &lasts)) { 5067ffbafc53Scomay if (strcmp(cp, ZONE_TOKEN) == 0) { 5068ffbafc53Scomay free(tmp); 5069ffbafc53Scomay if ((*privname = strdup(ZONE_TOKEN)) == NULL) 5070ffbafc53Scomay return (Z_NOMEM); 5071ffbafc53Scomay else 5072ffbafc53Scomay return (Z_PRIV_UNKNOWN); 5073ffbafc53Scomay } 5074ffbafc53Scomay } 5075ffbafc53Scomay free(tmp); 5076ffbafc53Scomay 5077ffbafc53Scomay if (add_default) { 5078ffbafc53Scomay /* 5079ffbafc53Scomay * If DEFAULT_TOKEN was specified, a string needs to be 5080ffbafc53Scomay * built containing the privileges from the default, safe 5081ffbafc53Scomay * set along with those of the "limitpriv" property. 5082ffbafc53Scomay */ 5083ffbafc53Scomay len = strlen(privbuf) + sizeof (BASIC_TOKEN) + 2; 50849acbbeafSnn35248 50859acbbeafSnn35248 for (pnp = plp->pl_default; pnp != NULL; pnp = pnp->pn_next) 50869acbbeafSnn35248 len += strlen(pnp->pn_priv) + 1; 5087ffbafc53Scomay tmp = alloca(len); 5088ffbafc53Scomay *tmp = '\0'; 5089ffbafc53Scomay 5090ffbafc53Scomay append_priv_token(BASIC_TOKEN, tmp, len); 50919acbbeafSnn35248 for (pnp = plp->pl_default; pnp != NULL; pnp = pnp->pn_next) 50929acbbeafSnn35248 append_priv_token(pnp->pn_priv, tmp, len); 5093ffbafc53Scomay (void) strlcat(tmp, TOKEN_PRIV_STR, len); 5094ffbafc53Scomay (void) strlcat(tmp, privbuf, len); 5095ffbafc53Scomay } else { 5096ffbafc53Scomay tmp = privbuf; 5097ffbafc53Scomay } 5098ffbafc53Scomay 5099ffbafc53Scomay 5100ffbafc53Scomay /* 5101ffbafc53Scomay * In the next phase, attempt to convert the merged privilege 5102ffbafc53Scomay * string into a privilege set. In the case of an error, either 5103ffbafc53Scomay * there was a memory allocation failure or there was an invalid 5104ffbafc53Scomay * privilege token in the string. In either case, return an 5105ffbafc53Scomay * appropriate error code but in the event of an invalid token, 5106ffbafc53Scomay * allocate a string containing its name and return that back to 5107ffbafc53Scomay * the caller. 5108ffbafc53Scomay */ 5109ffbafc53Scomay mergeset = priv_str_to_set(tmp, TOKEN_PRIV_STR, &token); 5110ffbafc53Scomay if (mergeset == NULL) { 5111ffbafc53Scomay if (token == NULL) 5112ffbafc53Scomay return (Z_NOMEM); 5113ffbafc53Scomay if ((cp = strchr(token, TOKEN_PRIV_CHAR)) != NULL) 5114ffbafc53Scomay *cp = '\0'; 5115ffbafc53Scomay if ((*privname = strdup(token)) == NULL) 5116ffbafc53Scomay return (Z_NOMEM); 5117ffbafc53Scomay else 5118ffbafc53Scomay return (Z_PRIV_UNKNOWN); 5119ffbafc53Scomay } 5120ffbafc53Scomay 5121ffbafc53Scomay /* 5122ffbafc53Scomay * Next, verify that none of the prohibited zone privileges are 5123ffbafc53Scomay * present in the merged privilege set. 5124ffbafc53Scomay */ 51259acbbeafSnn35248 for (pnp = plp->pl_prohibited; pnp != NULL; pnp = pnp->pn_next) { 51269acbbeafSnn35248 if (priv_ismember(mergeset, pnp->pn_priv)) { 5127ffbafc53Scomay priv_freeset(mergeset); 51289acbbeafSnn35248 if ((*privname = strdup(pnp->pn_priv)) == NULL) 5129ffbafc53Scomay return (Z_NOMEM); 5130ffbafc53Scomay else 5131ffbafc53Scomay return (Z_PRIV_PROHIBITED); 5132ffbafc53Scomay } 5133ffbafc53Scomay } 5134ffbafc53Scomay 5135ffbafc53Scomay /* 5136ffbafc53Scomay * Finally, verify that all of the required zone privileges are 5137ffbafc53Scomay * present in the merged privilege set. 5138ffbafc53Scomay */ 51399acbbeafSnn35248 for (pnp = plp->pl_required; pnp != NULL; pnp = pnp->pn_next) { 51409acbbeafSnn35248 if (!priv_ismember(mergeset, pnp->pn_priv)) { 5141ffbafc53Scomay priv_freeset(mergeset); 51429acbbeafSnn35248 if ((*privname = strdup(pnp->pn_priv)) == NULL) 5143ffbafc53Scomay return (Z_NOMEM); 5144ffbafc53Scomay else 5145ffbafc53Scomay return (Z_PRIV_REQUIRED); 5146ffbafc53Scomay } 5147ffbafc53Scomay } 5148ffbafc53Scomay 5149ffbafc53Scomay priv_copyset(mergeset, privs); 5150ffbafc53Scomay priv_freeset(mergeset); 5151ffbafc53Scomay return (Z_OK); 5152ffbafc53Scomay } 5153ffbafc53Scomay 5154ffbafc53Scomay /* 5155ffbafc53Scomay * Fill in the supplied privilege set with either the default, safe set of 5156ffbafc53Scomay * privileges suitable for a non-global zone, or one based on the 5157ffbafc53Scomay * "limitpriv" property in the zone's configuration. 5158ffbafc53Scomay * 5159ffbafc53Scomay * In the event of an invalid privilege specification in the 5160ffbafc53Scomay * configuration, a string is allocated and returned containing the 5161ffbafc53Scomay * "privilege" causing the issue. It is the caller's responsibility to 5162ffbafc53Scomay * free this memory when it is done with it. 5163ffbafc53Scomay */ 5164ffbafc53Scomay int 5165ffbafc53Scomay zonecfg_get_privset(zone_dochandle_t handle, priv_set_t *privs, 5166ffbafc53Scomay char **privname) 5167ffbafc53Scomay { 51689acbbeafSnn35248 priv_lists_t *plp; 51699acbbeafSnn35248 char *cp, *limitpriv = NULL; 51709acbbeafSnn35248 int err, limitlen; 5171bf1d7e28Sdh155122 zone_iptype_t iptype; 5172bf1d7e28Sdh155122 const char *curr_iptype; 5173ffbafc53Scomay 5174ffbafc53Scomay /* 5175ffbafc53Scomay * Attempt to lookup the "limitpriv" property. If it does not 5176ffbafc53Scomay * exist or matches the string DEFAULT_TOKEN exactly, then the 5177ffbafc53Scomay * default, safe privilege set is returned. 5178ffbafc53Scomay */ 51799acbbeafSnn35248 if ((err = zonecfg_get_limitpriv(handle, &limitpriv)) != Z_OK) 5180ffbafc53Scomay return (err); 51819acbbeafSnn35248 5182bf1d7e28Sdh155122 if ((err = zonecfg_get_iptype(handle, &iptype)) != Z_OK) 5183bf1d7e28Sdh155122 return (err); 5184bf1d7e28Sdh155122 5185bf1d7e28Sdh155122 switch (iptype) { 5186bf1d7e28Sdh155122 case ZS_SHARED: 5187bf1d7e28Sdh155122 curr_iptype = "shared"; 5188bf1d7e28Sdh155122 break; 5189bf1d7e28Sdh155122 case ZS_EXCLUSIVE: 5190bf1d7e28Sdh155122 curr_iptype = "exclusive"; 5191bf1d7e28Sdh155122 break; 5192bf1d7e28Sdh155122 } 5193bf1d7e28Sdh155122 5194e5816e35SEdward Pilatowicz if ((err = priv_lists_create(handle, NULL, &plp, curr_iptype)) != Z_OK) 51959acbbeafSnn35248 return (err); 51969acbbeafSnn35248 5197ffbafc53Scomay limitlen = strlen(limitpriv); 5198ffbafc53Scomay if (limitlen == 0 || strcmp(limitpriv, DEFAULT_TOKEN) == 0) { 5199ffbafc53Scomay free(limitpriv); 52009acbbeafSnn35248 err = get_default_privset(privs, plp); 52019acbbeafSnn35248 priv_lists_destroy(plp); 52029acbbeafSnn35248 return (err); 5203ffbafc53Scomay } 5204ffbafc53Scomay 5205ffbafc53Scomay /* 5206ffbafc53Scomay * Check if the string DEFAULT_TOKEN is the first token in a list 5207ffbafc53Scomay * of privileges. 5208ffbafc53Scomay */ 5209ffbafc53Scomay cp = strchr(limitpriv, TOKEN_PRIV_CHAR); 5210ffbafc53Scomay if (cp != NULL && 5211ffbafc53Scomay strncmp(limitpriv, DEFAULT_TOKEN, cp - limitpriv) == 0) 52129acbbeafSnn35248 err = verify_privset(cp + 1, privs, privname, B_TRUE, plp); 5213ffbafc53Scomay else 52149acbbeafSnn35248 err = verify_privset(limitpriv, privs, privname, B_FALSE, plp); 5215ffbafc53Scomay 5216ffbafc53Scomay free(limitpriv); 52179acbbeafSnn35248 priv_lists_destroy(plp); 5218ffbafc53Scomay return (err); 5219ffbafc53Scomay } 5220ffbafc53Scomay 52217c478bd9Sstevel@tonic-gate int 52227c478bd9Sstevel@tonic-gate zone_get_zonepath(char *zone_name, char *zonepath, size_t rp_sz) 52237c478bd9Sstevel@tonic-gate { 52247c478bd9Sstevel@tonic-gate zone_dochandle_t handle; 52257c478bd9Sstevel@tonic-gate boolean_t found = B_FALSE; 52267c478bd9Sstevel@tonic-gate struct zoneent *ze; 52277c478bd9Sstevel@tonic-gate FILE *cookie; 52287c478bd9Sstevel@tonic-gate int err; 5229108322fbScarlsonj char *cp; 52307c478bd9Sstevel@tonic-gate 52317c478bd9Sstevel@tonic-gate if (zone_name == NULL) 52327c478bd9Sstevel@tonic-gate return (Z_INVAL); 52337c478bd9Sstevel@tonic-gate 5234108322fbScarlsonj (void) strlcpy(zonepath, zonecfg_root, rp_sz); 5235108322fbScarlsonj cp = zonepath + strlen(zonepath); 5236108322fbScarlsonj while (cp > zonepath && cp[-1] == '/') 5237108322fbScarlsonj *--cp = '\0'; 5238108322fbScarlsonj 52397c478bd9Sstevel@tonic-gate if (strcmp(zone_name, GLOBAL_ZONENAME) == 0) { 5240108322fbScarlsonj if (zonepath[0] == '\0') 52417c478bd9Sstevel@tonic-gate (void) strlcpy(zonepath, "/", rp_sz); 52427c478bd9Sstevel@tonic-gate return (Z_OK); 52437c478bd9Sstevel@tonic-gate } 52447c478bd9Sstevel@tonic-gate 52457c478bd9Sstevel@tonic-gate /* 52467c478bd9Sstevel@tonic-gate * First check the index file. Because older versions did not have 52477c478bd9Sstevel@tonic-gate * a copy of the zone path, allow for it to be zero length, in which 52487c478bd9Sstevel@tonic-gate * case we ignore this result and fall back to the XML files. 52497c478bd9Sstevel@tonic-gate */ 52507c478bd9Sstevel@tonic-gate cookie = setzoneent(); 52517c478bd9Sstevel@tonic-gate while ((ze = getzoneent_private(cookie)) != NULL) { 52527c478bd9Sstevel@tonic-gate if (strcmp(ze->zone_name, zone_name) == 0) { 52537c478bd9Sstevel@tonic-gate found = B_TRUE; 5254108322fbScarlsonj if (ze->zone_path[0] != '\0') 5255108322fbScarlsonj (void) strlcpy(cp, ze->zone_path, 5256108322fbScarlsonj rp_sz - (cp - zonepath)); 52577c478bd9Sstevel@tonic-gate } 52587c478bd9Sstevel@tonic-gate free(ze); 52597c478bd9Sstevel@tonic-gate if (found) 52607c478bd9Sstevel@tonic-gate break; 52617c478bd9Sstevel@tonic-gate } 52627c478bd9Sstevel@tonic-gate endzoneent(cookie); 5263108322fbScarlsonj if (found && *cp != '\0') 52647c478bd9Sstevel@tonic-gate return (Z_OK); 52657c478bd9Sstevel@tonic-gate 52667c478bd9Sstevel@tonic-gate /* Fall back to the XML files. */ 52677c478bd9Sstevel@tonic-gate if ((handle = zonecfg_init_handle()) == NULL) 52687c478bd9Sstevel@tonic-gate return (Z_NOMEM); 52697c478bd9Sstevel@tonic-gate 52707c478bd9Sstevel@tonic-gate /* 52717c478bd9Sstevel@tonic-gate * Check the snapshot first: if a zone is running, its zonepath 52727c478bd9Sstevel@tonic-gate * may have changed. 52737c478bd9Sstevel@tonic-gate */ 52747c478bd9Sstevel@tonic-gate if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK) { 5275e767a340Sgjelinek if ((err = zonecfg_get_handle(zone_name, handle)) != Z_OK) { 5276e767a340Sgjelinek zonecfg_fini_handle(handle); 52777c478bd9Sstevel@tonic-gate return (err); 52787c478bd9Sstevel@tonic-gate } 5279e767a340Sgjelinek } 52807c478bd9Sstevel@tonic-gate err = zonecfg_get_zonepath(handle, zonepath, rp_sz); 52817c478bd9Sstevel@tonic-gate zonecfg_fini_handle(handle); 52827c478bd9Sstevel@tonic-gate return (err); 52837c478bd9Sstevel@tonic-gate } 52847c478bd9Sstevel@tonic-gate 52857c478bd9Sstevel@tonic-gate int 52867c478bd9Sstevel@tonic-gate zone_get_rootpath(char *zone_name, char *rootpath, size_t rp_sz) 52877c478bd9Sstevel@tonic-gate { 52887c478bd9Sstevel@tonic-gate int err; 52897c478bd9Sstevel@tonic-gate 52907c478bd9Sstevel@tonic-gate /* This function makes sense for non-global zones only. */ 52917c478bd9Sstevel@tonic-gate if (strcmp(zone_name, GLOBAL_ZONENAME) == 0) 52927c478bd9Sstevel@tonic-gate return (Z_BOGUS_ZONE_NAME); 52937c478bd9Sstevel@tonic-gate if ((err = zone_get_zonepath(zone_name, rootpath, rp_sz)) != Z_OK) 52947c478bd9Sstevel@tonic-gate return (err); 52957c478bd9Sstevel@tonic-gate if (strlcat(rootpath, "/root", rp_sz) >= rp_sz) 52967c478bd9Sstevel@tonic-gate return (Z_TOO_BIG); 52977c478bd9Sstevel@tonic-gate return (Z_OK); 52987c478bd9Sstevel@tonic-gate } 52997c478bd9Sstevel@tonic-gate 53009acbbeafSnn35248 int 53019acbbeafSnn35248 zone_get_brand(char *zone_name, char *brandname, size_t rp_sz) 53029acbbeafSnn35248 { 53039acbbeafSnn35248 int err; 53049acbbeafSnn35248 zone_dochandle_t handle; 53059acbbeafSnn35248 char myzone[MAXNAMELEN]; 53069acbbeafSnn35248 int myzoneid = getzoneid(); 53079acbbeafSnn35248 53089acbbeafSnn35248 /* 53099acbbeafSnn35248 * If we are not in the global zone, then we don't have the zone 53109acbbeafSnn35248 * .xml files with the brand name available. Thus, we are going to 53119acbbeafSnn35248 * have to ask the kernel for the information. 53129acbbeafSnn35248 */ 53139acbbeafSnn35248 if (myzoneid != GLOBAL_ZONEID) { 53142ec67e04Sgjelinek if (is_system_labeled()) { 53152ec67e04Sgjelinek (void) strlcpy(brandname, NATIVE_BRAND_NAME, rp_sz); 53162ec67e04Sgjelinek return (Z_OK); 53172ec67e04Sgjelinek } 53189acbbeafSnn35248 if (zone_getattr(myzoneid, ZONE_ATTR_NAME, myzone, 53199acbbeafSnn35248 sizeof (myzone)) < 0) 53209acbbeafSnn35248 return (Z_NO_ZONE); 53211d6979b7Ssaurabh vyas - Sun Microsystems - Bangalore India if (!zonecfg_is_scratch(myzone)) { 53221d6979b7Ssaurabh vyas - Sun Microsystems - Bangalore India if (strncmp(zone_name, myzone, MAXNAMELEN) != 0) 53239acbbeafSnn35248 return (Z_NO_ZONE); 53241d6979b7Ssaurabh vyas - Sun Microsystems - Bangalore India } 53259acbbeafSnn35248 err = zone_getattr(myzoneid, ZONE_ATTR_BRAND, brandname, rp_sz); 53269acbbeafSnn35248 if (err < 0) 53279acbbeafSnn35248 return ((errno == EFAULT) ? Z_TOO_BIG : Z_INVAL); 53281d6979b7Ssaurabh vyas - Sun Microsystems - Bangalore India 53299acbbeafSnn35248 return (Z_OK); 53309acbbeafSnn35248 } 53319acbbeafSnn35248 5332e5816e35SEdward Pilatowicz if (strcmp(zone_name, "global") == 0) 5333e5816e35SEdward Pilatowicz return (zonecfg_default_brand(brandname, rp_sz)); 5334e5816e35SEdward Pilatowicz 53359acbbeafSnn35248 if ((handle = zonecfg_init_handle()) == NULL) 53369acbbeafSnn35248 return (Z_NOMEM); 53379acbbeafSnn35248 53389acbbeafSnn35248 err = zonecfg_get_handle((char *)zone_name, handle); 53399acbbeafSnn35248 if (err == Z_OK) 53409acbbeafSnn35248 err = zonecfg_get_brand(handle, brandname, rp_sz); 53419acbbeafSnn35248 53429acbbeafSnn35248 zonecfg_fini_handle(handle); 53439acbbeafSnn35248 return (err); 53449acbbeafSnn35248 } 53459acbbeafSnn35248 5346facf4a8dSllai1 /* 5347facf4a8dSllai1 * Return the appropriate root for the active /dev. 5348facf4a8dSllai1 * For normal zone, the path is $ZONEPATH/root; 5349facf4a8dSllai1 * for scratch zone, the dev path is $ZONEPATH/lu. 5350facf4a8dSllai1 */ 5351facf4a8dSllai1 int 5352facf4a8dSllai1 zone_get_devroot(char *zone_name, char *devroot, size_t rp_sz) 5353facf4a8dSllai1 { 5354facf4a8dSllai1 int err; 5355facf4a8dSllai1 char *suffix; 5356facf4a8dSllai1 zone_state_t state; 5357facf4a8dSllai1 5358facf4a8dSllai1 /* This function makes sense for non-global zones only. */ 5359facf4a8dSllai1 if (strcmp(zone_name, GLOBAL_ZONENAME) == 0) 5360facf4a8dSllai1 return (Z_BOGUS_ZONE_NAME); 5361facf4a8dSllai1 if ((err = zone_get_zonepath(zone_name, devroot, rp_sz)) != Z_OK) 5362facf4a8dSllai1 return (err); 5363facf4a8dSllai1 5364facf4a8dSllai1 if (zone_get_state(zone_name, &state) == Z_OK && 5365facf4a8dSllai1 state == ZONE_STATE_MOUNTED) 5366facf4a8dSllai1 suffix = "/lu"; 5367facf4a8dSllai1 else 5368facf4a8dSllai1 suffix = "/root"; 5369facf4a8dSllai1 if (strlcat(devroot, suffix, rp_sz) >= rp_sz) 5370facf4a8dSllai1 return (Z_TOO_BIG); 5371facf4a8dSllai1 return (Z_OK); 5372facf4a8dSllai1 } 5373facf4a8dSllai1 53747c478bd9Sstevel@tonic-gate static zone_state_t 5375108322fbScarlsonj kernel_state_to_user_state(zoneid_t zoneid, zone_status_t kernel_state) 53767c478bd9Sstevel@tonic-gate { 5377108322fbScarlsonj char zoneroot[MAXPATHLEN]; 5378108322fbScarlsonj size_t zlen; 5379108322fbScarlsonj 53807c478bd9Sstevel@tonic-gate assert(kernel_state <= ZONE_MAX_STATE); 53817c478bd9Sstevel@tonic-gate switch (kernel_state) { 53827c478bd9Sstevel@tonic-gate case ZONE_IS_UNINITIALIZED: 5383bd41d0a8Snordmark case ZONE_IS_INITIALIZED: 5384bd41d0a8Snordmark /* The kernel will not return these two states */ 5385108322fbScarlsonj return (ZONE_STATE_READY); 53867c478bd9Sstevel@tonic-gate case ZONE_IS_READY: 5387108322fbScarlsonj /* 5388108322fbScarlsonj * If the zone's root is mounted on $ZONEPATH/lu, then 5389108322fbScarlsonj * it's a mounted scratch zone. 5390108322fbScarlsonj */ 5391108322fbScarlsonj if (zone_getattr(zoneid, ZONE_ATTR_ROOT, zoneroot, 5392108322fbScarlsonj sizeof (zoneroot)) >= 0) { 5393108322fbScarlsonj zlen = strlen(zoneroot); 5394108322fbScarlsonj if (zlen > 3 && 5395108322fbScarlsonj strcmp(zoneroot + zlen - 3, "/lu") == 0) 5396108322fbScarlsonj return (ZONE_STATE_MOUNTED); 5397108322fbScarlsonj } 53987c478bd9Sstevel@tonic-gate return (ZONE_STATE_READY); 53997c478bd9Sstevel@tonic-gate case ZONE_IS_BOOTING: 54007c478bd9Sstevel@tonic-gate case ZONE_IS_RUNNING: 54017c478bd9Sstevel@tonic-gate return (ZONE_STATE_RUNNING); 54027c478bd9Sstevel@tonic-gate case ZONE_IS_SHUTTING_DOWN: 54037c478bd9Sstevel@tonic-gate case ZONE_IS_EMPTY: 54047c478bd9Sstevel@tonic-gate return (ZONE_STATE_SHUTTING_DOWN); 54057c478bd9Sstevel@tonic-gate case ZONE_IS_DOWN: 54067c478bd9Sstevel@tonic-gate case ZONE_IS_DYING: 54077c478bd9Sstevel@tonic-gate case ZONE_IS_DEAD: 54087c478bd9Sstevel@tonic-gate default: 54097c478bd9Sstevel@tonic-gate return (ZONE_STATE_DOWN); 54107c478bd9Sstevel@tonic-gate } 54117c478bd9Sstevel@tonic-gate /* NOTREACHED */ 54127c478bd9Sstevel@tonic-gate } 54137c478bd9Sstevel@tonic-gate 54147c478bd9Sstevel@tonic-gate int 54157c478bd9Sstevel@tonic-gate zone_get_state(char *zone_name, zone_state_t *state_num) 54167c478bd9Sstevel@tonic-gate { 54177c478bd9Sstevel@tonic-gate zone_status_t status; 54187c478bd9Sstevel@tonic-gate zoneid_t zone_id; 54197c478bd9Sstevel@tonic-gate struct zoneent *ze; 54207c478bd9Sstevel@tonic-gate boolean_t found = B_FALSE; 54217c478bd9Sstevel@tonic-gate FILE *cookie; 5422108322fbScarlsonj char kernzone[ZONENAME_MAX]; 5423108322fbScarlsonj FILE *fp; 54247c478bd9Sstevel@tonic-gate 54257c478bd9Sstevel@tonic-gate if (zone_name == NULL) 54267c478bd9Sstevel@tonic-gate return (Z_INVAL); 54277c478bd9Sstevel@tonic-gate 5428108322fbScarlsonj /* 5429108322fbScarlsonj * If we're looking at an alternate root, then we need to query the 5430108322fbScarlsonj * kernel using the scratch zone name. 5431108322fbScarlsonj */ 5432108322fbScarlsonj zone_id = -1; 5433108322fbScarlsonj if (*zonecfg_root != '\0' && !zonecfg_is_scratch(zone_name)) { 5434108322fbScarlsonj if ((fp = zonecfg_open_scratch("", B_FALSE)) != NULL) { 5435108322fbScarlsonj if (zonecfg_find_scratch(fp, zone_name, zonecfg_root, 5436108322fbScarlsonj kernzone, sizeof (kernzone)) == 0) 5437108322fbScarlsonj zone_id = getzoneidbyname(kernzone); 5438108322fbScarlsonj zonecfg_close_scratch(fp); 5439108322fbScarlsonj } 5440108322fbScarlsonj } else { 5441108322fbScarlsonj zone_id = getzoneidbyname(zone_name); 5442108322fbScarlsonj } 5443108322fbScarlsonj 54447c478bd9Sstevel@tonic-gate /* check to see if zone is running */ 5445108322fbScarlsonj if (zone_id != -1 && 54467c478bd9Sstevel@tonic-gate zone_getattr(zone_id, ZONE_ATTR_STATUS, &status, 54477c478bd9Sstevel@tonic-gate sizeof (status)) >= 0) { 5448108322fbScarlsonj *state_num = kernel_state_to_user_state(zone_id, status); 54497c478bd9Sstevel@tonic-gate return (Z_OK); 54507c478bd9Sstevel@tonic-gate } 54517c478bd9Sstevel@tonic-gate 54527c478bd9Sstevel@tonic-gate cookie = setzoneent(); 54537c478bd9Sstevel@tonic-gate while ((ze = getzoneent_private(cookie)) != NULL) { 54547c478bd9Sstevel@tonic-gate if (strcmp(ze->zone_name, zone_name) == 0) { 54557c478bd9Sstevel@tonic-gate found = B_TRUE; 54567c478bd9Sstevel@tonic-gate *state_num = ze->zone_state; 54577c478bd9Sstevel@tonic-gate } 54587c478bd9Sstevel@tonic-gate free(ze); 54597c478bd9Sstevel@tonic-gate if (found) 54607c478bd9Sstevel@tonic-gate break; 54617c478bd9Sstevel@tonic-gate } 54627c478bd9Sstevel@tonic-gate endzoneent(cookie); 54637c478bd9Sstevel@tonic-gate return ((found) ? Z_OK : Z_NO_ZONE); 54647c478bd9Sstevel@tonic-gate } 54657c478bd9Sstevel@tonic-gate 54667c478bd9Sstevel@tonic-gate int 54677c478bd9Sstevel@tonic-gate zone_set_state(char *zone, zone_state_t state) 54687c478bd9Sstevel@tonic-gate { 54697c478bd9Sstevel@tonic-gate struct zoneent ze; 54707c478bd9Sstevel@tonic-gate 54717c478bd9Sstevel@tonic-gate if (state != ZONE_STATE_CONFIGURED && state != ZONE_STATE_INSTALLED && 54727c478bd9Sstevel@tonic-gate state != ZONE_STATE_INCOMPLETE) 54737c478bd9Sstevel@tonic-gate return (Z_INVAL); 54747c478bd9Sstevel@tonic-gate 5475087719fdSdp bzero(&ze, sizeof (ze)); 54767c478bd9Sstevel@tonic-gate (void) strlcpy(ze.zone_name, zone, sizeof (ze.zone_name)); 54777c478bd9Sstevel@tonic-gate ze.zone_state = state; 54787c478bd9Sstevel@tonic-gate (void) strlcpy(ze.zone_path, "", sizeof (ze.zone_path)); 54797c478bd9Sstevel@tonic-gate return (putzoneent(&ze, PZE_MODIFY)); 54807c478bd9Sstevel@tonic-gate } 54817c478bd9Sstevel@tonic-gate 54827c478bd9Sstevel@tonic-gate /* 54837c478bd9Sstevel@tonic-gate * Get id (if any) for specified zone. There are four possible outcomes: 54847c478bd9Sstevel@tonic-gate * - If the string corresponds to the numeric id of an active (booted) 54857c478bd9Sstevel@tonic-gate * zone, sets *zip to the zone id and returns 0. 54867c478bd9Sstevel@tonic-gate * - If the string corresponds to the name of an active (booted) zone, 54877c478bd9Sstevel@tonic-gate * sets *zip to the zone id and returns 0. 54887c478bd9Sstevel@tonic-gate * - If the string is a name in the configuration but is not booted, 54897c478bd9Sstevel@tonic-gate * sets *zip to ZONE_ID_UNDEFINED and returns 0. 54907c478bd9Sstevel@tonic-gate * - Otherwise, leaves *zip unchanged and returns -1. 54917c478bd9Sstevel@tonic-gate * 54927c478bd9Sstevel@tonic-gate * This function acts as an auxiliary filter on the function of the same 54937c478bd9Sstevel@tonic-gate * name in libc; the linker binds to this version if libzonecfg exists, 54947c478bd9Sstevel@tonic-gate * and the libc version if it doesn't. Any changes to this version of 54957c478bd9Sstevel@tonic-gate * the function should probably be reflected in the libc version as well. 54967c478bd9Sstevel@tonic-gate */ 54977c478bd9Sstevel@tonic-gate int 54987c478bd9Sstevel@tonic-gate zone_get_id(const char *str, zoneid_t *zip) 54997c478bd9Sstevel@tonic-gate { 55007c478bd9Sstevel@tonic-gate zone_dochandle_t hdl; 55017c478bd9Sstevel@tonic-gate zoneid_t zoneid; 55027c478bd9Sstevel@tonic-gate char *cp; 55037c478bd9Sstevel@tonic-gate int err; 55047c478bd9Sstevel@tonic-gate 55057c478bd9Sstevel@tonic-gate /* first try looking for active zone by id */ 55067c478bd9Sstevel@tonic-gate errno = 0; 55077c478bd9Sstevel@tonic-gate zoneid = (zoneid_t)strtol(str, &cp, 0); 55087c478bd9Sstevel@tonic-gate if (errno == 0 && cp != str && *cp == '\0' && 55097c478bd9Sstevel@tonic-gate getzonenamebyid(zoneid, NULL, 0) != -1) { 55107c478bd9Sstevel@tonic-gate *zip = zoneid; 55117c478bd9Sstevel@tonic-gate return (0); 55127c478bd9Sstevel@tonic-gate } 55137c478bd9Sstevel@tonic-gate 55147c478bd9Sstevel@tonic-gate /* then look for active zone by name */ 55157c478bd9Sstevel@tonic-gate if ((zoneid = getzoneidbyname(str)) != -1) { 55167c478bd9Sstevel@tonic-gate *zip = zoneid; 55177c478bd9Sstevel@tonic-gate return (0); 55187c478bd9Sstevel@tonic-gate } 55197c478bd9Sstevel@tonic-gate 55207c478bd9Sstevel@tonic-gate /* if in global zone, try looking up name in configuration database */ 55217c478bd9Sstevel@tonic-gate if (getzoneid() != GLOBAL_ZONEID || 55227c478bd9Sstevel@tonic-gate (hdl = zonecfg_init_handle()) == NULL) 55237c478bd9Sstevel@tonic-gate return (-1); 55247c478bd9Sstevel@tonic-gate 5525108322fbScarlsonj if (zonecfg_get_handle(str, hdl) == Z_OK) { 55267c478bd9Sstevel@tonic-gate /* zone exists but isn't active */ 55277c478bd9Sstevel@tonic-gate *zip = ZONE_ID_UNDEFINED; 55287c478bd9Sstevel@tonic-gate err = 0; 55297c478bd9Sstevel@tonic-gate } else { 55307c478bd9Sstevel@tonic-gate err = -1; 55317c478bd9Sstevel@tonic-gate } 55327c478bd9Sstevel@tonic-gate 55337c478bd9Sstevel@tonic-gate zonecfg_fini_handle(hdl); 55347c478bd9Sstevel@tonic-gate return (err); 55357c478bd9Sstevel@tonic-gate } 55367c478bd9Sstevel@tonic-gate 55377c478bd9Sstevel@tonic-gate char * 55387c478bd9Sstevel@tonic-gate zone_state_str(zone_state_t state_num) 55397c478bd9Sstevel@tonic-gate { 55407c478bd9Sstevel@tonic-gate switch (state_num) { 55417c478bd9Sstevel@tonic-gate case ZONE_STATE_CONFIGURED: 55427c478bd9Sstevel@tonic-gate return (ZONE_STATE_STR_CONFIGURED); 55437c478bd9Sstevel@tonic-gate case ZONE_STATE_INCOMPLETE: 55447c478bd9Sstevel@tonic-gate return (ZONE_STATE_STR_INCOMPLETE); 55457c478bd9Sstevel@tonic-gate case ZONE_STATE_INSTALLED: 55467c478bd9Sstevel@tonic-gate return (ZONE_STATE_STR_INSTALLED); 55477c478bd9Sstevel@tonic-gate case ZONE_STATE_READY: 55487c478bd9Sstevel@tonic-gate return (ZONE_STATE_STR_READY); 5549108322fbScarlsonj case ZONE_STATE_MOUNTED: 5550108322fbScarlsonj return (ZONE_STATE_STR_MOUNTED); 55517c478bd9Sstevel@tonic-gate case ZONE_STATE_RUNNING: 55527c478bd9Sstevel@tonic-gate return (ZONE_STATE_STR_RUNNING); 55537c478bd9Sstevel@tonic-gate case ZONE_STATE_SHUTTING_DOWN: 55547c478bd9Sstevel@tonic-gate return (ZONE_STATE_STR_SHUTTING_DOWN); 55557c478bd9Sstevel@tonic-gate case ZONE_STATE_DOWN: 55567c478bd9Sstevel@tonic-gate return (ZONE_STATE_STR_DOWN); 55577c478bd9Sstevel@tonic-gate default: 55587c478bd9Sstevel@tonic-gate return ("unknown"); 55597c478bd9Sstevel@tonic-gate } 55607c478bd9Sstevel@tonic-gate } 55617c478bd9Sstevel@tonic-gate 55627c478bd9Sstevel@tonic-gate /* 5563108322fbScarlsonj * Given a UUID value, find an associated zone name. This is intended to be 5564108322fbScarlsonj * used by callers who set up some 'default' name (corresponding to the 5565108322fbScarlsonj * expected name for the zone) in the zonename buffer, and thus the function 5566108322fbScarlsonj * doesn't touch this buffer on failure. 5567108322fbScarlsonj */ 5568108322fbScarlsonj int 5569555afedfScarlsonj zonecfg_get_name_by_uuid(const uuid_t uuidin, char *zonename, size_t namelen) 5570108322fbScarlsonj { 5571108322fbScarlsonj FILE *fp; 5572108322fbScarlsonj struct zoneent *ze; 5573555afedfScarlsonj uchar_t *uuid; 5574108322fbScarlsonj 5575108322fbScarlsonj /* 5576108322fbScarlsonj * A small amount of subterfuge via casts is necessary here because 5577108322fbScarlsonj * libuuid doesn't use const correctly, but we don't want to export 5578108322fbScarlsonj * this brokenness to our clients. 5579108322fbScarlsonj */ 5580555afedfScarlsonj uuid = (uchar_t *)uuidin; 5581555afedfScarlsonj if (uuid_is_null(uuid)) 5582108322fbScarlsonj return (Z_NO_ZONE); 5583108322fbScarlsonj if ((fp = setzoneent()) == NULL) 5584108322fbScarlsonj return (Z_NO_ZONE); 5585108322fbScarlsonj while ((ze = getzoneent_private(fp)) != NULL) { 5586555afedfScarlsonj if (uuid_compare(uuid, ze->zone_uuid) == 0) 5587108322fbScarlsonj break; 5588108322fbScarlsonj free(ze); 5589108322fbScarlsonj } 5590108322fbScarlsonj endzoneent(fp); 5591108322fbScarlsonj if (ze != NULL) { 5592108322fbScarlsonj (void) strlcpy(zonename, ze->zone_name, namelen); 5593108322fbScarlsonj free(ze); 5594108322fbScarlsonj return (Z_OK); 5595108322fbScarlsonj } else { 5596108322fbScarlsonj return (Z_NO_ZONE); 5597108322fbScarlsonj } 5598108322fbScarlsonj } 5599108322fbScarlsonj 5600108322fbScarlsonj /* 5601108322fbScarlsonj * Given a zone name, get its UUID. Returns a "NULL" UUID value if the zone 5602108322fbScarlsonj * exists but the file doesn't have a value set yet. Returns an error if the 5603108322fbScarlsonj * zone cannot be located. 5604108322fbScarlsonj */ 5605108322fbScarlsonj int 5606108322fbScarlsonj zonecfg_get_uuid(const char *zonename, uuid_t uuid) 5607108322fbScarlsonj { 5608108322fbScarlsonj FILE *fp; 5609108322fbScarlsonj struct zoneent *ze; 5610108322fbScarlsonj 5611108322fbScarlsonj if ((fp = setzoneent()) == NULL) 5612108322fbScarlsonj return (Z_NO_ZONE); 5613108322fbScarlsonj while ((ze = getzoneent_private(fp)) != NULL) { 5614108322fbScarlsonj if (strcmp(ze->zone_name, zonename) == 0) 5615108322fbScarlsonj break; 5616108322fbScarlsonj free(ze); 5617108322fbScarlsonj } 5618108322fbScarlsonj endzoneent(fp); 5619108322fbScarlsonj if (ze != NULL) { 5620108322fbScarlsonj uuid_copy(uuid, ze->zone_uuid); 5621108322fbScarlsonj free(ze); 5622108322fbScarlsonj return (Z_OK); 5623108322fbScarlsonj } else { 5624108322fbScarlsonj return (Z_NO_ZONE); 5625108322fbScarlsonj } 5626108322fbScarlsonj } 5627108322fbScarlsonj 5628108322fbScarlsonj /* 56297c478bd9Sstevel@tonic-gate * File-system convenience functions. 56307c478bd9Sstevel@tonic-gate */ 56317c478bd9Sstevel@tonic-gate boolean_t 56327c478bd9Sstevel@tonic-gate zonecfg_valid_fs_type(const char *type) 56337c478bd9Sstevel@tonic-gate { 56347c478bd9Sstevel@tonic-gate /* 56357c478bd9Sstevel@tonic-gate * We already know which FS types don't work. 56367c478bd9Sstevel@tonic-gate */ 56377c478bd9Sstevel@tonic-gate if (strcmp(type, "proc") == 0 || 56387c478bd9Sstevel@tonic-gate strcmp(type, "mntfs") == 0 || 56397c478bd9Sstevel@tonic-gate strcmp(type, "autofs") == 0 || 56407c478bd9Sstevel@tonic-gate strncmp(type, "nfs", sizeof ("nfs") - 1) == 0 || 56417c478bd9Sstevel@tonic-gate strcmp(type, "cachefs") == 0) 56427c478bd9Sstevel@tonic-gate return (B_FALSE); 56437c478bd9Sstevel@tonic-gate /* 56447c478bd9Sstevel@tonic-gate * The caller may do more detailed verification to make sure other 56457c478bd9Sstevel@tonic-gate * aspects of this filesystem type make sense. 56467c478bd9Sstevel@tonic-gate */ 56477c478bd9Sstevel@tonic-gate return (B_TRUE); 56487c478bd9Sstevel@tonic-gate } 56497c478bd9Sstevel@tonic-gate 56507c478bd9Sstevel@tonic-gate /* 56517c478bd9Sstevel@tonic-gate * Generally uninteresting rctl convenience functions. 56527c478bd9Sstevel@tonic-gate */ 56537c478bd9Sstevel@tonic-gate 56547c478bd9Sstevel@tonic-gate int 56557c478bd9Sstevel@tonic-gate zonecfg_construct_rctlblk(const struct zone_rctlvaltab *rctlval, 56567c478bd9Sstevel@tonic-gate rctlblk_t *rctlblk) 56577c478bd9Sstevel@tonic-gate { 56587c478bd9Sstevel@tonic-gate unsigned long long ull; 56597c478bd9Sstevel@tonic-gate char *endp; 56607c478bd9Sstevel@tonic-gate rctl_priv_t priv; 56617c478bd9Sstevel@tonic-gate rctl_qty_t limit; 56627c478bd9Sstevel@tonic-gate uint_t action; 56637c478bd9Sstevel@tonic-gate 56647c478bd9Sstevel@tonic-gate /* Get the privilege */ 56657c478bd9Sstevel@tonic-gate if (strcmp(rctlval->zone_rctlval_priv, "basic") == 0) { 56667c478bd9Sstevel@tonic-gate priv = RCPRIV_BASIC; 56677c478bd9Sstevel@tonic-gate } else if (strcmp(rctlval->zone_rctlval_priv, "privileged") == 0) { 56687c478bd9Sstevel@tonic-gate priv = RCPRIV_PRIVILEGED; 56697c478bd9Sstevel@tonic-gate } else { 56707c478bd9Sstevel@tonic-gate /* Invalid privilege */ 56717c478bd9Sstevel@tonic-gate return (Z_INVAL); 56727c478bd9Sstevel@tonic-gate } 56737c478bd9Sstevel@tonic-gate 56747c478bd9Sstevel@tonic-gate /* deal with negative input; strtoull(3c) doesn't do what we want */ 56757c478bd9Sstevel@tonic-gate if (rctlval->zone_rctlval_limit[0] == '-') 56767c478bd9Sstevel@tonic-gate return (Z_INVAL); 56777c478bd9Sstevel@tonic-gate /* Get the limit */ 56787c478bd9Sstevel@tonic-gate errno = 0; 56797c478bd9Sstevel@tonic-gate ull = strtoull(rctlval->zone_rctlval_limit, &endp, 0); 56807c478bd9Sstevel@tonic-gate if (errno != 0 || *endp != '\0') { 56817c478bd9Sstevel@tonic-gate /* parse failed */ 56827c478bd9Sstevel@tonic-gate return (Z_INVAL); 56837c478bd9Sstevel@tonic-gate } 56847c478bd9Sstevel@tonic-gate limit = (rctl_qty_t)ull; 56857c478bd9Sstevel@tonic-gate 56867c478bd9Sstevel@tonic-gate /* Get the action */ 56877c478bd9Sstevel@tonic-gate if (strcmp(rctlval->zone_rctlval_action, "none") == 0) { 56887c478bd9Sstevel@tonic-gate action = RCTL_LOCAL_NOACTION; 56897c478bd9Sstevel@tonic-gate } else if (strcmp(rctlval->zone_rctlval_action, "signal") == 0) { 56907c478bd9Sstevel@tonic-gate action = RCTL_LOCAL_SIGNAL; 56917c478bd9Sstevel@tonic-gate } else if (strcmp(rctlval->zone_rctlval_action, "deny") == 0) { 56927c478bd9Sstevel@tonic-gate action = RCTL_LOCAL_DENY; 56937c478bd9Sstevel@tonic-gate } else { 56947c478bd9Sstevel@tonic-gate /* Invalid Action */ 56957c478bd9Sstevel@tonic-gate return (Z_INVAL); 56967c478bd9Sstevel@tonic-gate } 56977c478bd9Sstevel@tonic-gate rctlblk_set_local_action(rctlblk, action, 0); 56987c478bd9Sstevel@tonic-gate rctlblk_set_privilege(rctlblk, priv); 56997c478bd9Sstevel@tonic-gate rctlblk_set_value(rctlblk, limit); 57007c478bd9Sstevel@tonic-gate return (Z_OK); 57017c478bd9Sstevel@tonic-gate } 57027c478bd9Sstevel@tonic-gate 57037c478bd9Sstevel@tonic-gate static int 57047c478bd9Sstevel@tonic-gate rctl_check(const char *rctlname, void *arg) 57057c478bd9Sstevel@tonic-gate { 57067c478bd9Sstevel@tonic-gate const char *attrname = arg; 57077c478bd9Sstevel@tonic-gate 57087c478bd9Sstevel@tonic-gate /* 57097c478bd9Sstevel@tonic-gate * Returning 1 here is our signal to zonecfg_is_rctl() that it is 57107c478bd9Sstevel@tonic-gate * indeed an rctl name recognized by the system. 57117c478bd9Sstevel@tonic-gate */ 57127c478bd9Sstevel@tonic-gate return (strcmp(rctlname, attrname) == 0 ? 1 : 0); 57137c478bd9Sstevel@tonic-gate } 57147c478bd9Sstevel@tonic-gate 57157c478bd9Sstevel@tonic-gate boolean_t 57167c478bd9Sstevel@tonic-gate zonecfg_is_rctl(const char *name) 57177c478bd9Sstevel@tonic-gate { 57187c478bd9Sstevel@tonic-gate return (rctl_walk(rctl_check, (void *)name) == 1); 57197c478bd9Sstevel@tonic-gate } 57207c478bd9Sstevel@tonic-gate 57217c478bd9Sstevel@tonic-gate boolean_t 57227c478bd9Sstevel@tonic-gate zonecfg_valid_rctlname(const char *name) 57237c478bd9Sstevel@tonic-gate { 57247c478bd9Sstevel@tonic-gate const char *c; 57257c478bd9Sstevel@tonic-gate 57267c478bd9Sstevel@tonic-gate if (strncmp(name, "zone.", sizeof ("zone.") - 1) != 0) 57277c478bd9Sstevel@tonic-gate return (B_FALSE); 57287c478bd9Sstevel@tonic-gate if (strlen(name) == sizeof ("zone.") - 1) 57297c478bd9Sstevel@tonic-gate return (B_FALSE); 57307c478bd9Sstevel@tonic-gate for (c = name + sizeof ("zone.") - 1; *c != '\0'; c++) { 57317c478bd9Sstevel@tonic-gate if (!isalpha(*c) && *c != '-') 57327c478bd9Sstevel@tonic-gate return (B_FALSE); 57337c478bd9Sstevel@tonic-gate } 57347c478bd9Sstevel@tonic-gate return (B_TRUE); 57357c478bd9Sstevel@tonic-gate } 57367c478bd9Sstevel@tonic-gate 57377c478bd9Sstevel@tonic-gate boolean_t 57387c478bd9Sstevel@tonic-gate zonecfg_valid_rctlblk(const rctlblk_t *rctlblk) 57397c478bd9Sstevel@tonic-gate { 57407c478bd9Sstevel@tonic-gate rctl_priv_t priv = rctlblk_get_privilege((rctlblk_t *)rctlblk); 57417c478bd9Sstevel@tonic-gate uint_t action = rctlblk_get_local_action((rctlblk_t *)rctlblk, NULL); 57427c478bd9Sstevel@tonic-gate 57437c478bd9Sstevel@tonic-gate if (priv != RCPRIV_PRIVILEGED) 57447c478bd9Sstevel@tonic-gate return (B_FALSE); 57457c478bd9Sstevel@tonic-gate if (action != RCTL_LOCAL_NOACTION && action != RCTL_LOCAL_DENY) 57467c478bd9Sstevel@tonic-gate return (B_FALSE); 57477c478bd9Sstevel@tonic-gate return (B_TRUE); 57487c478bd9Sstevel@tonic-gate } 57497c478bd9Sstevel@tonic-gate 57507c478bd9Sstevel@tonic-gate boolean_t 57517c478bd9Sstevel@tonic-gate zonecfg_valid_rctl(const char *name, const rctlblk_t *rctlblk) 57527c478bd9Sstevel@tonic-gate { 57537c478bd9Sstevel@tonic-gate rctlblk_t *current, *next; 57547c478bd9Sstevel@tonic-gate rctl_qty_t limit = rctlblk_get_value((rctlblk_t *)rctlblk); 57557c478bd9Sstevel@tonic-gate uint_t action = rctlblk_get_local_action((rctlblk_t *)rctlblk, NULL); 57567c478bd9Sstevel@tonic-gate uint_t global_flags; 57577c478bd9Sstevel@tonic-gate 57587c478bd9Sstevel@tonic-gate if (!zonecfg_valid_rctlblk(rctlblk)) 57597c478bd9Sstevel@tonic-gate return (B_FALSE); 57607c478bd9Sstevel@tonic-gate if (!zonecfg_valid_rctlname(name)) 57617c478bd9Sstevel@tonic-gate return (B_FALSE); 57627c478bd9Sstevel@tonic-gate 57637c478bd9Sstevel@tonic-gate current = alloca(rctlblk_size()); 57647c478bd9Sstevel@tonic-gate if (getrctl(name, NULL, current, RCTL_FIRST) != 0) 57657c478bd9Sstevel@tonic-gate return (B_TRUE); /* not an rctl on this system */ 57667c478bd9Sstevel@tonic-gate /* 57677c478bd9Sstevel@tonic-gate * Make sure the proposed value isn't greater than the current system 57687c478bd9Sstevel@tonic-gate * value. 57697c478bd9Sstevel@tonic-gate */ 57707c478bd9Sstevel@tonic-gate next = alloca(rctlblk_size()); 57717c478bd9Sstevel@tonic-gate while (rctlblk_get_privilege(current) != RCPRIV_SYSTEM) { 57727c478bd9Sstevel@tonic-gate rctlblk_t *tmp; 57737c478bd9Sstevel@tonic-gate 57747c478bd9Sstevel@tonic-gate if (getrctl(name, current, next, RCTL_NEXT) != 0) 57757c478bd9Sstevel@tonic-gate return (B_FALSE); /* shouldn't happen */ 57767c478bd9Sstevel@tonic-gate tmp = current; 57777c478bd9Sstevel@tonic-gate current = next; 57787c478bd9Sstevel@tonic-gate next = tmp; 57797c478bd9Sstevel@tonic-gate } 57807c478bd9Sstevel@tonic-gate if (limit > rctlblk_get_value(current)) 57817c478bd9Sstevel@tonic-gate return (B_FALSE); 57827c478bd9Sstevel@tonic-gate 57837c478bd9Sstevel@tonic-gate /* 57847c478bd9Sstevel@tonic-gate * Make sure the proposed action is allowed. 57857c478bd9Sstevel@tonic-gate */ 57867c478bd9Sstevel@tonic-gate global_flags = rctlblk_get_global_flags(current); 57877c478bd9Sstevel@tonic-gate if ((global_flags & RCTL_GLOBAL_DENY_NEVER) && 57887c478bd9Sstevel@tonic-gate action == RCTL_LOCAL_DENY) 57897c478bd9Sstevel@tonic-gate return (B_FALSE); 57907c478bd9Sstevel@tonic-gate if ((global_flags & RCTL_GLOBAL_DENY_ALWAYS) && 57917c478bd9Sstevel@tonic-gate action == RCTL_LOCAL_NOACTION) 57927c478bd9Sstevel@tonic-gate return (B_FALSE); 57937c478bd9Sstevel@tonic-gate 57947c478bd9Sstevel@tonic-gate return (B_TRUE); 57957c478bd9Sstevel@tonic-gate } 5796fa9e4066Sahrens 5797cf8f45c7Sdstaff /* 5798cf8f45c7Sdstaff * There is always a race condition between reading the initial copy of 5799cf8f45c7Sdstaff * a zones state and its state changing. We address this by providing 5800cf8f45c7Sdstaff * zonecfg_notify_critical_enter and zonecfg_noticy_critical_exit functions. 5801cf8f45c7Sdstaff * When zonecfg_critical_enter is called, sets the state field to LOCKED 5802cf8f45c7Sdstaff * and aquires biglock. Biglock protects against other threads executing 5803cf8f45c7Sdstaff * critical_enter and the state field protects against state changes during 5804cf8f45c7Sdstaff * the critical period. 5805cf8f45c7Sdstaff * 5806cf8f45c7Sdstaff * If any state changes occur, zn_cb will set the failed field of the znotify 5807cf8f45c7Sdstaff * structure. This will cause the critical_exit function to re-lock the 5808cf8f45c7Sdstaff * channel and return an error. Since evsnts may be delayed, the critical_exit 5809cf8f45c7Sdstaff * function "flushes" the queue by putting an event on the queue and waiting for 5810cf8f45c7Sdstaff * zn_cb to notify critical_exit that it received the ping event. 5811cf8f45c7Sdstaff */ 5812cf8f45c7Sdstaff static const char * 5813cf8f45c7Sdstaff string_get_tok(const char *in, char delim, int num) 5814cf8f45c7Sdstaff { 5815cf8f45c7Sdstaff int i = 0; 5816cf8f45c7Sdstaff 5817cf8f45c7Sdstaff for (; i < num; in++) { 5818cf8f45c7Sdstaff if (*in == delim) 5819cf8f45c7Sdstaff i++; 5820cf8f45c7Sdstaff if (*in == 0) 5821cf8f45c7Sdstaff return (NULL); 5822cf8f45c7Sdstaff } 5823cf8f45c7Sdstaff return (in); 5824cf8f45c7Sdstaff } 5825cf8f45c7Sdstaff 5826cf8f45c7Sdstaff static boolean_t 5827cf8f45c7Sdstaff is_ping(sysevent_t *ev) 5828cf8f45c7Sdstaff { 5829cf8f45c7Sdstaff if (strcmp(sysevent_get_subclass_name(ev), 5830cf8f45c7Sdstaff ZONE_EVENT_PING_SUBCLASS) == 0) { 5831cf8f45c7Sdstaff return (B_TRUE); 5832cf8f45c7Sdstaff } else { 5833cf8f45c7Sdstaff return (B_FALSE); 5834cf8f45c7Sdstaff } 5835cf8f45c7Sdstaff } 5836cf8f45c7Sdstaff 5837cf8f45c7Sdstaff static boolean_t 5838cf8f45c7Sdstaff is_my_ping(sysevent_t *ev) 5839cf8f45c7Sdstaff { 5840cf8f45c7Sdstaff const char *sender; 5841cf8f45c7Sdstaff char mypid[sizeof (pid_t) * 3 + 1]; 5842cf8f45c7Sdstaff 5843cf8f45c7Sdstaff (void) snprintf(mypid, sizeof (mypid), "%i", getpid()); 5844cf8f45c7Sdstaff sender = string_get_tok(sysevent_get_pub(ev), ':', 3); 5845cf8f45c7Sdstaff if (sender == NULL) 5846cf8f45c7Sdstaff return (B_FALSE); 5847cf8f45c7Sdstaff if (strcmp(sender, mypid) != 0) 5848cf8f45c7Sdstaff return (B_FALSE); 5849cf8f45c7Sdstaff return (B_TRUE); 5850cf8f45c7Sdstaff } 5851cf8f45c7Sdstaff 5852cf8f45c7Sdstaff static int 5853cf8f45c7Sdstaff do_callback(struct znotify *zevtchan, sysevent_t *ev) 5854cf8f45c7Sdstaff { 5855cf8f45c7Sdstaff nvlist_t *l; 5856cf8f45c7Sdstaff int zid; 5857cf8f45c7Sdstaff char *zonename; 5858cf8f45c7Sdstaff char *newstate; 5859cf8f45c7Sdstaff char *oldstate; 5860cf8f45c7Sdstaff int ret; 5861cf8f45c7Sdstaff hrtime_t when; 5862cf8f45c7Sdstaff 5863cf8f45c7Sdstaff if (strcmp(sysevent_get_subclass_name(ev), 5864cf8f45c7Sdstaff ZONE_EVENT_STATUS_SUBCLASS) == 0) { 5865cf8f45c7Sdstaff 5866cf8f45c7Sdstaff if (sysevent_get_attr_list(ev, &l) != 0) { 5867cf8f45c7Sdstaff if (errno == ENOMEM) { 5868cf8f45c7Sdstaff zevtchan->zn_failure_count++; 5869cf8f45c7Sdstaff return (EAGAIN); 5870cf8f45c7Sdstaff } 5871cf8f45c7Sdstaff return (0); 5872cf8f45c7Sdstaff } 5873cf8f45c7Sdstaff ret = 0; 5874cf8f45c7Sdstaff 5875cf8f45c7Sdstaff if ((nvlist_lookup_string(l, ZONE_CB_NAME, &zonename) == 0) && 5876cf8f45c7Sdstaff (nvlist_lookup_string(l, ZONE_CB_NEWSTATE, &newstate) 5877cf8f45c7Sdstaff == 0) && 5878cf8f45c7Sdstaff (nvlist_lookup_string(l, ZONE_CB_OLDSTATE, &oldstate) 5879cf8f45c7Sdstaff == 0) && 5880cf8f45c7Sdstaff (nvlist_lookup_uint64(l, ZONE_CB_TIMESTAMP, 5881cf8f45c7Sdstaff (uint64_t *)&when) == 0) && 5882cf8f45c7Sdstaff (nvlist_lookup_int32(l, ZONE_CB_ZONEID, &zid) == 0)) { 5883cf8f45c7Sdstaff ret = zevtchan->zn_callback(zonename, zid, newstate, 5884cf8f45c7Sdstaff oldstate, when, zevtchan->zn_private); 5885cf8f45c7Sdstaff } 5886cf8f45c7Sdstaff 5887cf8f45c7Sdstaff zevtchan->zn_failure_count = 0; 5888cf8f45c7Sdstaff nvlist_free(l); 5889cf8f45c7Sdstaff return (ret); 5890cf8f45c7Sdstaff } else { 5891cf8f45c7Sdstaff /* 5892cf8f45c7Sdstaff * We have received an event in an unknown subclass. Ignore. 5893cf8f45c7Sdstaff */ 5894cf8f45c7Sdstaff zevtchan->zn_failure_count = 0; 5895cf8f45c7Sdstaff return (0); 5896cf8f45c7Sdstaff } 5897cf8f45c7Sdstaff } 5898cf8f45c7Sdstaff 5899cf8f45c7Sdstaff static int 5900cf8f45c7Sdstaff zn_cb(sysevent_t *ev, void *p) 5901cf8f45c7Sdstaff { 5902cf8f45c7Sdstaff struct znotify *zevtchan = p; 5903cf8f45c7Sdstaff int error; 5904cf8f45c7Sdstaff 5905cf8f45c7Sdstaff (void) pthread_mutex_lock(&(zevtchan->zn_mutex)); 5906cf8f45c7Sdstaff 5907cf8f45c7Sdstaff if (is_ping(ev) && !is_my_ping(ev)) { 5908cf8f45c7Sdstaff (void) pthread_mutex_unlock((&zevtchan->zn_mutex)); 5909cf8f45c7Sdstaff return (0); 5910cf8f45c7Sdstaff } 5911cf8f45c7Sdstaff 5912cf8f45c7Sdstaff if (zevtchan->zn_state == ZN_LOCKED) { 5913cf8f45c7Sdstaff assert(!is_ping(ev)); 5914cf8f45c7Sdstaff zevtchan->zn_failed = B_TRUE; 5915cf8f45c7Sdstaff (void) pthread_mutex_unlock(&(zevtchan->zn_mutex)); 5916cf8f45c7Sdstaff return (0); 5917cf8f45c7Sdstaff } 5918cf8f45c7Sdstaff 5919cf8f45c7Sdstaff if (zevtchan->zn_state == ZN_PING_INFLIGHT) { 5920cf8f45c7Sdstaff if (is_ping(ev)) { 5921cf8f45c7Sdstaff zevtchan->zn_state = ZN_PING_RECEIVED; 5922cf8f45c7Sdstaff (void) pthread_cond_signal(&(zevtchan->zn_cond)); 5923cf8f45c7Sdstaff (void) pthread_mutex_unlock(&(zevtchan->zn_mutex)); 5924cf8f45c7Sdstaff return (0); 5925cf8f45c7Sdstaff } else { 5926cf8f45c7Sdstaff zevtchan->zn_failed = B_TRUE; 5927cf8f45c7Sdstaff (void) pthread_mutex_unlock(&(zevtchan->zn_mutex)); 5928cf8f45c7Sdstaff return (0); 5929cf8f45c7Sdstaff } 5930cf8f45c7Sdstaff } 5931cf8f45c7Sdstaff 5932cf8f45c7Sdstaff if (zevtchan->zn_state == ZN_UNLOCKED) { 5933cf8f45c7Sdstaff 5934cf8f45c7Sdstaff error = do_callback(zevtchan, ev); 5935cf8f45c7Sdstaff (void) pthread_mutex_unlock(&(zevtchan->zn_mutex)); 5936cf8f45c7Sdstaff /* 5937cf8f45c7Sdstaff * Every ENOMEM failure causes do_callback to increment 5938cf8f45c7Sdstaff * zn_failure_count and every success causes it to 5939cf8f45c7Sdstaff * set zn_failure_count to zero. If we got EAGAIN, 5940cf8f45c7Sdstaff * we will sleep for zn_failure_count seconds and return 5941cf8f45c7Sdstaff * EAGAIN to gpec to try again. 5942cf8f45c7Sdstaff * 5943cf8f45c7Sdstaff * After 55 seconds, or 10 try's we give up and drop the 5944cf8f45c7Sdstaff * event. 5945cf8f45c7Sdstaff */ 5946cf8f45c7Sdstaff if (error == EAGAIN) { 5947cf8f45c7Sdstaff if (zevtchan->zn_failure_count > ZONE_CB_RETRY_COUNT) { 5948cf8f45c7Sdstaff return (0); 5949cf8f45c7Sdstaff } 5950cf8f45c7Sdstaff (void) sleep(zevtchan->zn_failure_count); 5951cf8f45c7Sdstaff } 5952cf8f45c7Sdstaff return (error); 5953cf8f45c7Sdstaff } 5954cf8f45c7Sdstaff 5955cf8f45c7Sdstaff if (zevtchan->zn_state == ZN_PING_RECEIVED) { 5956cf8f45c7Sdstaff (void) pthread_mutex_unlock(&(zevtchan->zn_mutex)); 5957cf8f45c7Sdstaff return (0); 5958cf8f45c7Sdstaff } 5959cf8f45c7Sdstaff 5960cf8f45c7Sdstaff abort(); 5961cf8f45c7Sdstaff return (0); 5962cf8f45c7Sdstaff } 5963cf8f45c7Sdstaff 5964cf8f45c7Sdstaff void 5965cf8f45c7Sdstaff zonecfg_notify_critical_enter(void *h) 5966cf8f45c7Sdstaff { 5967cf8f45c7Sdstaff struct znotify *zevtchan = h; 5968cf8f45c7Sdstaff 5969cf8f45c7Sdstaff (void) pthread_mutex_lock(&(zevtchan->zn_bigmutex)); 5970cf8f45c7Sdstaff zevtchan->zn_state = ZN_LOCKED; 5971cf8f45c7Sdstaff } 5972cf8f45c7Sdstaff 5973cf8f45c7Sdstaff int 5974cf8f45c7Sdstaff zonecfg_notify_critical_exit(void * h) 5975cf8f45c7Sdstaff { 5976cf8f45c7Sdstaff 5977cf8f45c7Sdstaff struct znotify *zevtchan = h; 5978cf8f45c7Sdstaff 5979cf8f45c7Sdstaff if (zevtchan->zn_state == ZN_UNLOCKED) 5980cf8f45c7Sdstaff return (0); 5981cf8f45c7Sdstaff 5982cf8f45c7Sdstaff (void) pthread_mutex_lock(&(zevtchan->zn_mutex)); 5983cf8f45c7Sdstaff zevtchan->zn_state = ZN_PING_INFLIGHT; 5984cf8f45c7Sdstaff 5985ee519a1fSgjelinek (void) sysevent_evc_publish(zevtchan->zn_eventchan, 5986ee519a1fSgjelinek ZONE_EVENT_STATUS_CLASS, 5987cf8f45c7Sdstaff ZONE_EVENT_PING_SUBCLASS, ZONE_EVENT_PING_PUBLISHER, 5988cf8f45c7Sdstaff zevtchan->zn_subscriber_id, NULL, EVCH_SLEEP); 5989cf8f45c7Sdstaff 5990cf8f45c7Sdstaff while (zevtchan->zn_state != ZN_PING_RECEIVED) { 5991cf8f45c7Sdstaff (void) pthread_cond_wait(&(zevtchan->zn_cond), 5992cf8f45c7Sdstaff &(zevtchan->zn_mutex)); 5993cf8f45c7Sdstaff } 5994cf8f45c7Sdstaff 5995cf8f45c7Sdstaff if (zevtchan->zn_failed == B_TRUE) { 5996cf8f45c7Sdstaff zevtchan->zn_state = ZN_LOCKED; 5997cf8f45c7Sdstaff zevtchan->zn_failed = B_FALSE; 5998cf8f45c7Sdstaff (void) pthread_mutex_unlock(&(zevtchan->zn_mutex)); 5999cf8f45c7Sdstaff return (1); 6000cf8f45c7Sdstaff } 6001cf8f45c7Sdstaff 6002cf8f45c7Sdstaff zevtchan->zn_state = ZN_UNLOCKED; 6003cf8f45c7Sdstaff (void) pthread_mutex_unlock(&(zevtchan->zn_mutex)); 6004cf8f45c7Sdstaff (void) pthread_mutex_unlock(&(zevtchan->zn_bigmutex)); 6005cf8f45c7Sdstaff return (0); 6006cf8f45c7Sdstaff } 6007cf8f45c7Sdstaff 6008cf8f45c7Sdstaff void 6009cf8f45c7Sdstaff zonecfg_notify_critical_abort(void *h) 6010cf8f45c7Sdstaff { 6011cf8f45c7Sdstaff struct znotify *zevtchan = h; 6012cf8f45c7Sdstaff 6013cf8f45c7Sdstaff zevtchan->zn_state = ZN_UNLOCKED; 6014cf8f45c7Sdstaff zevtchan->zn_failed = B_FALSE; 6015cf8f45c7Sdstaff /* 6016cf8f45c7Sdstaff * Don't do anything about zn_lock. If it is held, it could only be 6017cf8f45c7Sdstaff * held by zn_cb and it will be unlocked soon. 6018cf8f45c7Sdstaff */ 6019cf8f45c7Sdstaff (void) pthread_mutex_unlock(&(zevtchan->zn_bigmutex)); 6020cf8f45c7Sdstaff } 6021cf8f45c7Sdstaff 6022cf8f45c7Sdstaff void * 6023cf8f45c7Sdstaff zonecfg_notify_bind(int(*func)(const char *zonename, zoneid_t zid, 6024cf8f45c7Sdstaff const char *newstate, const char *oldstate, hrtime_t when, void *p), 6025cf8f45c7Sdstaff void *p) 6026cf8f45c7Sdstaff { 6027cf8f45c7Sdstaff struct znotify *zevtchan; 6028cf8f45c7Sdstaff int i = 1; 6029cf8f45c7Sdstaff int r; 6030cf8f45c7Sdstaff 6031cf8f45c7Sdstaff zevtchan = malloc(sizeof (struct znotify)); 6032cf8f45c7Sdstaff 6033cf8f45c7Sdstaff if (zevtchan == NULL) 6034cf8f45c7Sdstaff return (NULL); 6035cf8f45c7Sdstaff 6036cf8f45c7Sdstaff zevtchan->zn_private = p; 6037cf8f45c7Sdstaff zevtchan->zn_callback = func; 6038cf8f45c7Sdstaff zevtchan->zn_state = ZN_UNLOCKED; 6039cf8f45c7Sdstaff zevtchan->zn_failed = B_FALSE; 6040cf8f45c7Sdstaff 6041cf8f45c7Sdstaff if (pthread_mutex_init(&(zevtchan->zn_mutex), NULL)) 60429d4be64eSdstaff goto out3; 6043cf8f45c7Sdstaff if (pthread_cond_init(&(zevtchan->zn_cond), NULL)) { 6044cf8f45c7Sdstaff (void) pthread_mutex_destroy(&(zevtchan->zn_mutex)); 60459d4be64eSdstaff goto out3; 6046cf8f45c7Sdstaff } 6047cf8f45c7Sdstaff if (pthread_mutex_init(&(zevtchan->zn_bigmutex), NULL)) { 6048cf8f45c7Sdstaff (void) pthread_mutex_destroy(&(zevtchan->zn_mutex)); 6049cf8f45c7Sdstaff (void) pthread_cond_destroy(&(zevtchan->zn_cond)); 60509d4be64eSdstaff goto out3; 6051cf8f45c7Sdstaff } 6052cf8f45c7Sdstaff 6053cf8f45c7Sdstaff if (sysevent_evc_bind(ZONE_EVENT_CHANNEL, &(zevtchan->zn_eventchan), 6054cf8f45c7Sdstaff 0) != 0) 6055cf8f45c7Sdstaff goto out2; 6056cf8f45c7Sdstaff 6057cf8f45c7Sdstaff do { 6058cf8f45c7Sdstaff /* 6059cf8f45c7Sdstaff * At 4 digits the subscriber ID gets too long and we have 6060cf8f45c7Sdstaff * no chance of successfully registering. 6061cf8f45c7Sdstaff */ 6062cf8f45c7Sdstaff if (i > 999) 60639d4be64eSdstaff goto out1; 6064cf8f45c7Sdstaff 6065cf8f45c7Sdstaff (void) sprintf(zevtchan->zn_subscriber_id, "zone_%li_%i", 6066cf8f45c7Sdstaff getpid() % 999999l, i); 6067cf8f45c7Sdstaff 6068cf8f45c7Sdstaff r = sysevent_evc_subscribe(zevtchan->zn_eventchan, 6069cf8f45c7Sdstaff zevtchan->zn_subscriber_id, ZONE_EVENT_STATUS_CLASS, zn_cb, 6070cf8f45c7Sdstaff zevtchan, 0); 6071cf8f45c7Sdstaff 6072cf8f45c7Sdstaff i++; 6073cf8f45c7Sdstaff 6074cf8f45c7Sdstaff } while (r); 6075cf8f45c7Sdstaff 6076cf8f45c7Sdstaff return (zevtchan); 60779d4be64eSdstaff out1: 607849b225e1SGavin Maltby (void) sysevent_evc_unbind(zevtchan->zn_eventchan); 60799d4be64eSdstaff out2: 6080cf8f45c7Sdstaff (void) pthread_mutex_destroy(&zevtchan->zn_mutex); 6081cf8f45c7Sdstaff (void) pthread_cond_destroy(&zevtchan->zn_cond); 6082cf8f45c7Sdstaff (void) pthread_mutex_destroy(&(zevtchan->zn_bigmutex)); 60839d4be64eSdstaff out3: 6084cf8f45c7Sdstaff free(zevtchan); 6085cf8f45c7Sdstaff 6086cf8f45c7Sdstaff return (NULL); 6087cf8f45c7Sdstaff } 6088cf8f45c7Sdstaff 6089cf8f45c7Sdstaff void 6090cf8f45c7Sdstaff zonecfg_notify_unbind(void *handle) 6091cf8f45c7Sdstaff { 6092cf8f45c7Sdstaff 6093cf8f45c7Sdstaff int ret; 6094cf8f45c7Sdstaff 609549b225e1SGavin Maltby (void) sysevent_evc_unbind(((struct znotify *)handle)->zn_eventchan); 6096cf8f45c7Sdstaff /* 6097cf8f45c7Sdstaff * Check that all evc threads have gone away. This should be 6098cf8f45c7Sdstaff * enforced by sysevent_evc_unbind. 6099cf8f45c7Sdstaff */ 6100cf8f45c7Sdstaff ret = pthread_mutex_trylock(&((struct znotify *)handle)->zn_mutex); 6101cf8f45c7Sdstaff 6102cf8f45c7Sdstaff if (ret) 6103cf8f45c7Sdstaff abort(); 6104cf8f45c7Sdstaff 6105cf8f45c7Sdstaff (void) pthread_mutex_unlock(&((struct znotify *)handle)->zn_mutex); 6106cf8f45c7Sdstaff (void) pthread_mutex_destroy(&((struct znotify *)handle)->zn_mutex); 6107cf8f45c7Sdstaff (void) pthread_cond_destroy(&((struct znotify *)handle)->zn_cond); 6108cf8f45c7Sdstaff (void) pthread_mutex_destroy(&((struct znotify *)handle)->zn_bigmutex); 6109cf8f45c7Sdstaff 6110cf8f45c7Sdstaff free(handle); 6111cf8f45c7Sdstaff } 6112cf8f45c7Sdstaff 6113fa9e4066Sahrens static int 6114fa9e4066Sahrens zonecfg_add_ds_core(zone_dochandle_t handle, struct zone_dstab *tabptr) 6115fa9e4066Sahrens { 6116fa9e4066Sahrens xmlNodePtr newnode, cur = handle->zone_dh_cur; 6117fa9e4066Sahrens int err; 6118fa9e4066Sahrens 6119fa9e4066Sahrens newnode = xmlNewTextChild(cur, NULL, DTD_ELEM_DATASET, NULL); 6120fa9e4066Sahrens if ((err = newprop(newnode, DTD_ATTR_NAME, 6121fa9e4066Sahrens tabptr->zone_dataset_name)) != Z_OK) 6122fa9e4066Sahrens return (err); 6123fa9e4066Sahrens return (Z_OK); 6124fa9e4066Sahrens } 6125fa9e4066Sahrens 6126fa9e4066Sahrens int 6127fa9e4066Sahrens zonecfg_add_ds(zone_dochandle_t handle, struct zone_dstab *tabptr) 6128fa9e4066Sahrens { 6129fa9e4066Sahrens int err; 6130fa9e4066Sahrens 6131fa9e4066Sahrens if (tabptr == NULL) 6132fa9e4066Sahrens return (Z_INVAL); 6133fa9e4066Sahrens 6134fa9e4066Sahrens if ((err = operation_prep(handle)) != Z_OK) 6135fa9e4066Sahrens return (err); 6136fa9e4066Sahrens 6137fa9e4066Sahrens if ((err = zonecfg_add_ds_core(handle, tabptr)) != Z_OK) 6138fa9e4066Sahrens return (err); 6139fa9e4066Sahrens 6140fa9e4066Sahrens return (Z_OK); 6141fa9e4066Sahrens } 6142fa9e4066Sahrens 6143fa9e4066Sahrens static int 6144fa9e4066Sahrens zonecfg_delete_ds_core(zone_dochandle_t handle, struct zone_dstab *tabptr) 6145fa9e4066Sahrens { 6146fa9e4066Sahrens xmlNodePtr cur = handle->zone_dh_cur; 6147fa9e4066Sahrens 6148fa9e4066Sahrens for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) { 6149fa9e4066Sahrens if (xmlStrcmp(cur->name, DTD_ELEM_DATASET)) 6150fa9e4066Sahrens continue; 6151fa9e4066Sahrens 6152fa9e4066Sahrens if (match_prop(cur, DTD_ATTR_NAME, 6153fa9e4066Sahrens tabptr->zone_dataset_name)) { 6154fa9e4066Sahrens xmlUnlinkNode(cur); 6155fa9e4066Sahrens xmlFreeNode(cur); 6156fa9e4066Sahrens return (Z_OK); 6157fa9e4066Sahrens } 6158fa9e4066Sahrens } 6159fa9e4066Sahrens return (Z_NO_RESOURCE_ID); 6160fa9e4066Sahrens } 6161fa9e4066Sahrens 6162fa9e4066Sahrens int 6163fa9e4066Sahrens zonecfg_delete_ds(zone_dochandle_t handle, struct zone_dstab *tabptr) 6164fa9e4066Sahrens { 6165fa9e4066Sahrens int err; 6166fa9e4066Sahrens 6167fa9e4066Sahrens if (tabptr == NULL) 6168fa9e4066Sahrens return (Z_INVAL); 6169fa9e4066Sahrens 6170fa9e4066Sahrens if ((err = operation_prep(handle)) != Z_OK) 6171fa9e4066Sahrens return (err); 6172fa9e4066Sahrens 6173fa9e4066Sahrens if ((err = zonecfg_delete_ds_core(handle, tabptr)) != Z_OK) 6174fa9e4066Sahrens return (err); 6175fa9e4066Sahrens 6176fa9e4066Sahrens return (Z_OK); 6177fa9e4066Sahrens } 6178fa9e4066Sahrens 6179fa9e4066Sahrens int 6180fa9e4066Sahrens zonecfg_modify_ds( 6181fa9e4066Sahrens zone_dochandle_t handle, 6182fa9e4066Sahrens struct zone_dstab *oldtabptr, 6183fa9e4066Sahrens struct zone_dstab *newtabptr) 6184fa9e4066Sahrens { 6185fa9e4066Sahrens int err; 6186fa9e4066Sahrens 6187fa9e4066Sahrens if (oldtabptr == NULL || newtabptr == NULL) 6188fa9e4066Sahrens return (Z_INVAL); 6189fa9e4066Sahrens 6190fa9e4066Sahrens if ((err = operation_prep(handle)) != Z_OK) 6191fa9e4066Sahrens return (err); 6192fa9e4066Sahrens 6193fa9e4066Sahrens if ((err = zonecfg_delete_ds_core(handle, oldtabptr)) != Z_OK) 6194fa9e4066Sahrens return (err); 6195fa9e4066Sahrens 6196fa9e4066Sahrens if ((err = zonecfg_add_ds_core(handle, newtabptr)) != Z_OK) 6197fa9e4066Sahrens return (err); 6198fa9e4066Sahrens 6199fa9e4066Sahrens return (Z_OK); 6200fa9e4066Sahrens } 6201fa9e4066Sahrens 6202fa9e4066Sahrens int 6203fa9e4066Sahrens zonecfg_lookup_ds(zone_dochandle_t handle, struct zone_dstab *tabptr) 6204fa9e4066Sahrens { 6205fa9e4066Sahrens xmlNodePtr cur, firstmatch; 6206fa9e4066Sahrens int err; 6207fa9e4066Sahrens char dataset[MAXNAMELEN]; 6208fa9e4066Sahrens 6209fa9e4066Sahrens if (tabptr == NULL) 6210fa9e4066Sahrens return (Z_INVAL); 6211fa9e4066Sahrens 6212fa9e4066Sahrens if ((err = operation_prep(handle)) != Z_OK) 6213fa9e4066Sahrens return (err); 6214fa9e4066Sahrens 6215fa9e4066Sahrens cur = handle->zone_dh_cur; 6216fa9e4066Sahrens firstmatch = NULL; 6217fa9e4066Sahrens for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) { 6218fa9e4066Sahrens if (xmlStrcmp(cur->name, DTD_ELEM_DATASET)) 6219fa9e4066Sahrens continue; 6220fa9e4066Sahrens if (strlen(tabptr->zone_dataset_name) > 0) { 6221fa9e4066Sahrens if ((fetchprop(cur, DTD_ATTR_NAME, dataset, 6222fa9e4066Sahrens sizeof (dataset)) == Z_OK) && 6223fa9e4066Sahrens (strcmp(tabptr->zone_dataset_name, 6224fa9e4066Sahrens dataset) == 0)) { 6225fa9e4066Sahrens if (firstmatch == NULL) 6226fa9e4066Sahrens firstmatch = cur; 6227fa9e4066Sahrens else 6228fa9e4066Sahrens return (Z_INSUFFICIENT_SPEC); 6229fa9e4066Sahrens } 6230fa9e4066Sahrens } 6231fa9e4066Sahrens } 6232fa9e4066Sahrens if (firstmatch == NULL) 6233fa9e4066Sahrens return (Z_NO_RESOURCE_ID); 6234fa9e4066Sahrens 6235fa9e4066Sahrens cur = firstmatch; 6236fa9e4066Sahrens 6237fa9e4066Sahrens if ((err = fetchprop(cur, DTD_ATTR_NAME, tabptr->zone_dataset_name, 6238fa9e4066Sahrens sizeof (tabptr->zone_dataset_name))) != Z_OK) 6239fa9e4066Sahrens return (err); 6240fa9e4066Sahrens 6241fa9e4066Sahrens return (Z_OK); 6242fa9e4066Sahrens } 6243fa9e4066Sahrens 6244fa9e4066Sahrens int 6245fa9e4066Sahrens zonecfg_setdsent(zone_dochandle_t handle) 6246fa9e4066Sahrens { 6247fa9e4066Sahrens return (zonecfg_setent(handle)); 6248fa9e4066Sahrens } 6249fa9e4066Sahrens 6250fa9e4066Sahrens int 6251fa9e4066Sahrens zonecfg_getdsent(zone_dochandle_t handle, struct zone_dstab *tabptr) 6252fa9e4066Sahrens { 6253fa9e4066Sahrens xmlNodePtr cur; 6254fa9e4066Sahrens int err; 6255fa9e4066Sahrens 6256fa9e4066Sahrens if (handle == NULL) 6257fa9e4066Sahrens return (Z_INVAL); 6258fa9e4066Sahrens 6259fa9e4066Sahrens if ((cur = handle->zone_dh_cur) == NULL) 6260fa9e4066Sahrens return (Z_NO_ENTRY); 6261fa9e4066Sahrens 6262fa9e4066Sahrens for (; cur != NULL; cur = cur->next) 6263fa9e4066Sahrens if (!xmlStrcmp(cur->name, DTD_ELEM_DATASET)) 6264fa9e4066Sahrens break; 6265fa9e4066Sahrens if (cur == NULL) { 6266fa9e4066Sahrens handle->zone_dh_cur = handle->zone_dh_top; 6267fa9e4066Sahrens return (Z_NO_ENTRY); 6268fa9e4066Sahrens } 6269fa9e4066Sahrens 6270fa9e4066Sahrens if ((err = fetchprop(cur, DTD_ATTR_NAME, tabptr->zone_dataset_name, 6271fa9e4066Sahrens sizeof (tabptr->zone_dataset_name))) != Z_OK) { 6272fa9e4066Sahrens handle->zone_dh_cur = handle->zone_dh_top; 6273fa9e4066Sahrens return (err); 6274fa9e4066Sahrens } 6275fa9e4066Sahrens 6276fa9e4066Sahrens handle->zone_dh_cur = cur->next; 6277fa9e4066Sahrens return (Z_OK); 6278fa9e4066Sahrens } 6279fa9e4066Sahrens 6280fa9e4066Sahrens int 6281fa9e4066Sahrens zonecfg_enddsent(zone_dochandle_t handle) 6282fa9e4066Sahrens { 6283fa9e4066Sahrens return (zonecfg_endent(handle)); 6284fa9e4066Sahrens } 6285ee519a1fSgjelinek 62860209230bSgjelinek /* 62870209230bSgjelinek * Support for aliased rctls; that is, rctls that have simplified names in 62880209230bSgjelinek * zonecfg. For example, max-lwps is an alias for a well defined zone.max-lwps 62890209230bSgjelinek * rctl. If there are multiple existing values for one of these rctls or if 62900209230bSgjelinek * there is a single value that does not match the well defined template (i.e. 62910209230bSgjelinek * it has a different action) then we cannot treat the rctl as having an alias 62920209230bSgjelinek * so we return Z_ALIAS_DISALLOW. That means that the rctl cannot be 62930209230bSgjelinek * managed in zonecfg via an alias and that the standard rctl syntax must be 62940209230bSgjelinek * used. 62950209230bSgjelinek * 62960209230bSgjelinek * The possible return values are: 62970209230bSgjelinek * Z_NO_PROPERTY_ID - invalid alias name 62980209230bSgjelinek * Z_ALIAS_DISALLOW - pre-existing, incompatible rctl definition 62990209230bSgjelinek * Z_NO_ENTRY - no rctl is configured for this alias 63000209230bSgjelinek * Z_OK - we got a valid rctl for the specified alias 63010209230bSgjelinek */ 63020209230bSgjelinek int 63030209230bSgjelinek zonecfg_get_aliased_rctl(zone_dochandle_t handle, char *name, uint64_t *rval) 63040209230bSgjelinek { 63050209230bSgjelinek boolean_t found = B_FALSE; 63060209230bSgjelinek boolean_t found_val = B_FALSE; 63070209230bSgjelinek xmlNodePtr cur, val; 63080209230bSgjelinek char savedname[MAXNAMELEN]; 63090209230bSgjelinek struct zone_rctlvaltab rctl; 63100209230bSgjelinek int i; 63110209230bSgjelinek int err; 63120209230bSgjelinek 63130209230bSgjelinek for (i = 0; aliases[i].shortname != NULL; i++) 63140209230bSgjelinek if (strcmp(name, aliases[i].shortname) == 0) 63150209230bSgjelinek break; 63160209230bSgjelinek 63170209230bSgjelinek if (aliases[i].shortname == NULL) 63180209230bSgjelinek return (Z_NO_PROPERTY_ID); 63190209230bSgjelinek 63200209230bSgjelinek if ((err = operation_prep(handle)) != Z_OK) 63210209230bSgjelinek return (err); 63220209230bSgjelinek 63230209230bSgjelinek cur = handle->zone_dh_cur; 63240209230bSgjelinek for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) { 63250209230bSgjelinek if (xmlStrcmp(cur->name, DTD_ELEM_RCTL) != 0) 63260209230bSgjelinek continue; 63270209230bSgjelinek if ((fetchprop(cur, DTD_ATTR_NAME, savedname, 63280209230bSgjelinek sizeof (savedname)) == Z_OK) && 63290209230bSgjelinek (strcmp(savedname, aliases[i].realname) == 0)) { 63300209230bSgjelinek 63310209230bSgjelinek /* 63320209230bSgjelinek * If we already saw one of these, we can't have an 63330209230bSgjelinek * alias since we just found another. 63340209230bSgjelinek */ 63350209230bSgjelinek if (found) 63360209230bSgjelinek return (Z_ALIAS_DISALLOW); 63370209230bSgjelinek found = B_TRUE; 63380209230bSgjelinek 63390209230bSgjelinek for (val = cur->xmlChildrenNode; val != NULL; 63400209230bSgjelinek val = val->next) { 63410209230bSgjelinek /* 63420209230bSgjelinek * If we already have one value, we can't have 63430209230bSgjelinek * an alias since we just found another. 63440209230bSgjelinek */ 63450209230bSgjelinek if (found_val) 63460209230bSgjelinek return (Z_ALIAS_DISALLOW); 63470209230bSgjelinek found_val = B_TRUE; 63480209230bSgjelinek 63490209230bSgjelinek if ((fetchprop(val, DTD_ATTR_PRIV, 63500209230bSgjelinek rctl.zone_rctlval_priv, 63510209230bSgjelinek sizeof (rctl.zone_rctlval_priv)) != Z_OK)) 63520209230bSgjelinek break; 63530209230bSgjelinek if ((fetchprop(val, DTD_ATTR_LIMIT, 63540209230bSgjelinek rctl.zone_rctlval_limit, 63550209230bSgjelinek sizeof (rctl.zone_rctlval_limit)) != Z_OK)) 63560209230bSgjelinek break; 63570209230bSgjelinek if ((fetchprop(val, DTD_ATTR_ACTION, 63580209230bSgjelinek rctl.zone_rctlval_action, 63590209230bSgjelinek sizeof (rctl.zone_rctlval_action)) != Z_OK)) 63600209230bSgjelinek break; 63610209230bSgjelinek } 63620209230bSgjelinek 63630209230bSgjelinek /* check priv and action match the expected vals */ 63640209230bSgjelinek if (strcmp(rctl.zone_rctlval_priv, 63650209230bSgjelinek aliases[i].priv) != 0 || 63660209230bSgjelinek strcmp(rctl.zone_rctlval_action, 63670209230bSgjelinek aliases[i].action) != 0) 63680209230bSgjelinek return (Z_ALIAS_DISALLOW); 63690209230bSgjelinek } 63700209230bSgjelinek } 63710209230bSgjelinek 63720209230bSgjelinek if (found) { 63730209230bSgjelinek *rval = strtoull(rctl.zone_rctlval_limit, NULL, 10); 63740209230bSgjelinek return (Z_OK); 63750209230bSgjelinek } 63760209230bSgjelinek 63770209230bSgjelinek return (Z_NO_ENTRY); 63780209230bSgjelinek } 63790209230bSgjelinek 63800209230bSgjelinek int 63810209230bSgjelinek zonecfg_rm_aliased_rctl(zone_dochandle_t handle, char *name) 63820209230bSgjelinek { 63830209230bSgjelinek int i; 63840209230bSgjelinek uint64_t val; 63850209230bSgjelinek struct zone_rctltab rctltab; 63860209230bSgjelinek 63870209230bSgjelinek /* 63880209230bSgjelinek * First check that we have a valid aliased rctl to remove. 63890209230bSgjelinek * This will catch an rctl entry with non-standard values or 63900209230bSgjelinek * multiple rctl values for this name. We need to ignore those 63910209230bSgjelinek * rctl entries. 63920209230bSgjelinek */ 63930209230bSgjelinek if (zonecfg_get_aliased_rctl(handle, name, &val) != Z_OK) 63940209230bSgjelinek return (Z_OK); 63950209230bSgjelinek 63960209230bSgjelinek for (i = 0; aliases[i].shortname != NULL; i++) 63970209230bSgjelinek if (strcmp(name, aliases[i].shortname) == 0) 63980209230bSgjelinek break; 63990209230bSgjelinek 64000209230bSgjelinek if (aliases[i].shortname == NULL) 64010209230bSgjelinek return (Z_NO_RESOURCE_ID); 64020209230bSgjelinek 64030209230bSgjelinek (void) strlcpy(rctltab.zone_rctl_name, aliases[i].realname, 64040209230bSgjelinek sizeof (rctltab.zone_rctl_name)); 64050209230bSgjelinek 64060209230bSgjelinek return (zonecfg_delete_rctl(handle, &rctltab)); 64070209230bSgjelinek } 64080209230bSgjelinek 64090209230bSgjelinek boolean_t 64100209230bSgjelinek zonecfg_aliased_rctl_ok(zone_dochandle_t handle, char *name) 64110209230bSgjelinek { 64120209230bSgjelinek uint64_t tmp_val; 64130209230bSgjelinek 64140209230bSgjelinek switch (zonecfg_get_aliased_rctl(handle, name, &tmp_val)) { 64150209230bSgjelinek case Z_OK: 64160209230bSgjelinek /*FALLTHRU*/ 64170209230bSgjelinek case Z_NO_ENTRY: 64180209230bSgjelinek return (B_TRUE); 64190209230bSgjelinek default: 64200209230bSgjelinek return (B_FALSE); 64210209230bSgjelinek } 64220209230bSgjelinek } 64230209230bSgjelinek 64240209230bSgjelinek int 64250209230bSgjelinek zonecfg_set_aliased_rctl(zone_dochandle_t handle, char *name, uint64_t val) 64260209230bSgjelinek { 64270209230bSgjelinek int i; 64280209230bSgjelinek int err; 64290209230bSgjelinek struct zone_rctltab rctltab; 64300209230bSgjelinek struct zone_rctlvaltab *rctlvaltab; 64310209230bSgjelinek char buf[128]; 64320209230bSgjelinek 64330209230bSgjelinek if (!zonecfg_aliased_rctl_ok(handle, name)) 64340209230bSgjelinek return (Z_ALIAS_DISALLOW); 64350209230bSgjelinek 64360209230bSgjelinek for (i = 0; aliases[i].shortname != NULL; i++) 64370209230bSgjelinek if (strcmp(name, aliases[i].shortname) == 0) 64380209230bSgjelinek break; 64390209230bSgjelinek 64400209230bSgjelinek if (aliases[i].shortname == NULL) 64410209230bSgjelinek return (Z_NO_RESOURCE_ID); 64420209230bSgjelinek 64430209230bSgjelinek /* remove any pre-existing definition for this rctl */ 64440209230bSgjelinek (void) zonecfg_rm_aliased_rctl(handle, name); 64450209230bSgjelinek 64460209230bSgjelinek (void) strlcpy(rctltab.zone_rctl_name, aliases[i].realname, 64470209230bSgjelinek sizeof (rctltab.zone_rctl_name)); 64480209230bSgjelinek 64490209230bSgjelinek rctltab.zone_rctl_valptr = NULL; 64500209230bSgjelinek 64510209230bSgjelinek if ((rctlvaltab = calloc(1, sizeof (struct zone_rctlvaltab))) == NULL) 64520209230bSgjelinek return (Z_NOMEM); 64530209230bSgjelinek 64540209230bSgjelinek (void) snprintf(buf, sizeof (buf), "%llu", (long long)val); 64550209230bSgjelinek 64560209230bSgjelinek (void) strlcpy(rctlvaltab->zone_rctlval_priv, aliases[i].priv, 64570209230bSgjelinek sizeof (rctlvaltab->zone_rctlval_priv)); 64580209230bSgjelinek (void) strlcpy(rctlvaltab->zone_rctlval_limit, buf, 64590209230bSgjelinek sizeof (rctlvaltab->zone_rctlval_limit)); 64600209230bSgjelinek (void) strlcpy(rctlvaltab->zone_rctlval_action, aliases[i].action, 64610209230bSgjelinek sizeof (rctlvaltab->zone_rctlval_action)); 64620209230bSgjelinek 64630209230bSgjelinek rctlvaltab->zone_rctlval_next = NULL; 64640209230bSgjelinek 64650209230bSgjelinek if ((err = zonecfg_add_rctl_value(&rctltab, rctlvaltab)) != Z_OK) 64660209230bSgjelinek return (err); 64670209230bSgjelinek 64680209230bSgjelinek return (zonecfg_add_rctl(handle, &rctltab)); 64690209230bSgjelinek } 64700209230bSgjelinek 64710209230bSgjelinek static int 64720209230bSgjelinek delete_tmp_pool(zone_dochandle_t handle) 64730209230bSgjelinek { 64740209230bSgjelinek int err; 64750209230bSgjelinek xmlNodePtr cur = handle->zone_dh_cur; 64760209230bSgjelinek 64770209230bSgjelinek if ((err = operation_prep(handle)) != Z_OK) 64780209230bSgjelinek return (err); 64790209230bSgjelinek 64800209230bSgjelinek for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) { 64810209230bSgjelinek if (xmlStrcmp(cur->name, DTD_ELEM_TMPPOOL) == 0) { 64820209230bSgjelinek xmlUnlinkNode(cur); 64830209230bSgjelinek xmlFreeNode(cur); 64840209230bSgjelinek return (Z_OK); 64850209230bSgjelinek } 64860209230bSgjelinek } 64870209230bSgjelinek 64880209230bSgjelinek return (Z_NO_RESOURCE_ID); 64890209230bSgjelinek } 64900209230bSgjelinek 64910209230bSgjelinek static int 64920209230bSgjelinek modify_tmp_pool(zone_dochandle_t handle, char *pool_importance) 64930209230bSgjelinek { 64940209230bSgjelinek int err; 64950209230bSgjelinek xmlNodePtr cur = handle->zone_dh_cur; 64960209230bSgjelinek xmlNodePtr newnode; 64970209230bSgjelinek 64980209230bSgjelinek err = delete_tmp_pool(handle); 64990209230bSgjelinek if (err != Z_OK && err != Z_NO_RESOURCE_ID) 65000209230bSgjelinek return (err); 65010209230bSgjelinek 65020209230bSgjelinek if (*pool_importance != '\0') { 65030209230bSgjelinek if ((err = operation_prep(handle)) != Z_OK) 65040209230bSgjelinek return (err); 65050209230bSgjelinek 65060209230bSgjelinek newnode = xmlNewTextChild(cur, NULL, DTD_ELEM_TMPPOOL, NULL); 65070209230bSgjelinek if ((err = newprop(newnode, DTD_ATTR_IMPORTANCE, 65080209230bSgjelinek pool_importance)) != Z_OK) 65090209230bSgjelinek return (err); 65100209230bSgjelinek } 65110209230bSgjelinek 65120209230bSgjelinek return (Z_OK); 65130209230bSgjelinek } 65140209230bSgjelinek 65150209230bSgjelinek static int 65160209230bSgjelinek add_pset_core(zone_dochandle_t handle, struct zone_psettab *tabptr) 65170209230bSgjelinek { 65180209230bSgjelinek xmlNodePtr newnode, cur = handle->zone_dh_cur; 65190209230bSgjelinek int err; 65200209230bSgjelinek 65210209230bSgjelinek newnode = xmlNewTextChild(cur, NULL, DTD_ELEM_PSET, NULL); 65220209230bSgjelinek if ((err = newprop(newnode, DTD_ATTR_NCPU_MIN, 65230209230bSgjelinek tabptr->zone_ncpu_min)) != Z_OK) 65240209230bSgjelinek return (err); 65250209230bSgjelinek if ((err = newprop(newnode, DTD_ATTR_NCPU_MAX, 65260209230bSgjelinek tabptr->zone_ncpu_max)) != Z_OK) 65270209230bSgjelinek return (err); 65280209230bSgjelinek 65290209230bSgjelinek if ((err = modify_tmp_pool(handle, tabptr->zone_importance)) != Z_OK) 65300209230bSgjelinek return (err); 65310209230bSgjelinek 65320209230bSgjelinek return (Z_OK); 65330209230bSgjelinek } 65340209230bSgjelinek 65350209230bSgjelinek int 65360209230bSgjelinek zonecfg_add_pset(zone_dochandle_t handle, struct zone_psettab *tabptr) 65370209230bSgjelinek { 65380209230bSgjelinek int err; 65390209230bSgjelinek 65400209230bSgjelinek if (tabptr == NULL) 65410209230bSgjelinek return (Z_INVAL); 65420209230bSgjelinek 65430209230bSgjelinek if ((err = operation_prep(handle)) != Z_OK) 65440209230bSgjelinek return (err); 65450209230bSgjelinek 65460209230bSgjelinek if ((err = add_pset_core(handle, tabptr)) != Z_OK) 65470209230bSgjelinek return (err); 65480209230bSgjelinek 65490209230bSgjelinek return (Z_OK); 65500209230bSgjelinek } 65510209230bSgjelinek 65520209230bSgjelinek int 65530209230bSgjelinek zonecfg_delete_pset(zone_dochandle_t handle) 65540209230bSgjelinek { 65550209230bSgjelinek int err; 65560209230bSgjelinek int res = Z_NO_RESOURCE_ID; 65570209230bSgjelinek xmlNodePtr cur = handle->zone_dh_cur; 65580209230bSgjelinek 65590209230bSgjelinek if ((err = operation_prep(handle)) != Z_OK) 65600209230bSgjelinek return (err); 65610209230bSgjelinek 65620209230bSgjelinek for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) { 65630209230bSgjelinek if (xmlStrcmp(cur->name, DTD_ELEM_PSET) == 0) { 65640209230bSgjelinek xmlUnlinkNode(cur); 65650209230bSgjelinek xmlFreeNode(cur); 65660209230bSgjelinek res = Z_OK; 65670209230bSgjelinek break; 65680209230bSgjelinek } 65690209230bSgjelinek } 65700209230bSgjelinek 65710209230bSgjelinek /* 65720209230bSgjelinek * Once we have msets, we should check that a mset 65730209230bSgjelinek * do not exist before we delete the tmp_pool data. 65740209230bSgjelinek */ 65750209230bSgjelinek err = delete_tmp_pool(handle); 65760209230bSgjelinek if (err != Z_OK && err != Z_NO_RESOURCE_ID) 65770209230bSgjelinek return (err); 65780209230bSgjelinek 65790209230bSgjelinek return (res); 65800209230bSgjelinek } 65810209230bSgjelinek 65820209230bSgjelinek int 65830209230bSgjelinek zonecfg_modify_pset(zone_dochandle_t handle, struct zone_psettab *tabptr) 65840209230bSgjelinek { 65850209230bSgjelinek int err; 65860209230bSgjelinek 65870209230bSgjelinek if (tabptr == NULL) 65880209230bSgjelinek return (Z_INVAL); 65890209230bSgjelinek 65900209230bSgjelinek if ((err = zonecfg_delete_pset(handle)) != Z_OK) 65910209230bSgjelinek return (err); 65920209230bSgjelinek 65930209230bSgjelinek if ((err = add_pset_core(handle, tabptr)) != Z_OK) 65940209230bSgjelinek return (err); 65950209230bSgjelinek 65960209230bSgjelinek return (Z_OK); 65970209230bSgjelinek } 65980209230bSgjelinek 65990209230bSgjelinek int 66000209230bSgjelinek zonecfg_lookup_pset(zone_dochandle_t handle, struct zone_psettab *tabptr) 66010209230bSgjelinek { 66020209230bSgjelinek xmlNodePtr cur; 66030209230bSgjelinek int err; 66040209230bSgjelinek int res = Z_NO_ENTRY; 66050209230bSgjelinek 66060209230bSgjelinek if (tabptr == NULL) 66070209230bSgjelinek return (Z_INVAL); 66080209230bSgjelinek 66090209230bSgjelinek if ((err = operation_prep(handle)) != Z_OK) 66100209230bSgjelinek return (err); 66110209230bSgjelinek 66120209230bSgjelinek /* this is an optional component */ 66130209230bSgjelinek tabptr->zone_importance[0] = '\0'; 66140209230bSgjelinek 66150209230bSgjelinek cur = handle->zone_dh_cur; 66160209230bSgjelinek for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) { 66170209230bSgjelinek if (xmlStrcmp(cur->name, DTD_ELEM_PSET) == 0) { 66180209230bSgjelinek if ((err = fetchprop(cur, DTD_ATTR_NCPU_MIN, 66190209230bSgjelinek tabptr->zone_ncpu_min, 66200209230bSgjelinek sizeof (tabptr->zone_ncpu_min))) != Z_OK) { 66210209230bSgjelinek handle->zone_dh_cur = handle->zone_dh_top; 66220209230bSgjelinek return (err); 66230209230bSgjelinek } 66240209230bSgjelinek 66250209230bSgjelinek if ((err = fetchprop(cur, DTD_ATTR_NCPU_MAX, 66260209230bSgjelinek tabptr->zone_ncpu_max, 66270209230bSgjelinek sizeof (tabptr->zone_ncpu_max))) != Z_OK) { 66280209230bSgjelinek handle->zone_dh_cur = handle->zone_dh_top; 66290209230bSgjelinek return (err); 66300209230bSgjelinek } 66310209230bSgjelinek 66320209230bSgjelinek res = Z_OK; 66330209230bSgjelinek 66340209230bSgjelinek } else if (xmlStrcmp(cur->name, DTD_ELEM_TMPPOOL) == 0) { 66350209230bSgjelinek if ((err = fetchprop(cur, DTD_ATTR_IMPORTANCE, 66360209230bSgjelinek tabptr->zone_importance, 66370209230bSgjelinek sizeof (tabptr->zone_importance))) != Z_OK) { 66380209230bSgjelinek handle->zone_dh_cur = handle->zone_dh_top; 66390209230bSgjelinek return (err); 66400209230bSgjelinek } 66410209230bSgjelinek } 66420209230bSgjelinek } 66430209230bSgjelinek 66440209230bSgjelinek return (res); 66450209230bSgjelinek } 66460209230bSgjelinek 66470209230bSgjelinek int 66480209230bSgjelinek zonecfg_getpsetent(zone_dochandle_t handle, struct zone_psettab *tabptr) 66490209230bSgjelinek { 66500209230bSgjelinek int err; 66510209230bSgjelinek 66520209230bSgjelinek if ((err = zonecfg_setent(handle)) != Z_OK) 66530209230bSgjelinek return (err); 66540209230bSgjelinek 66550209230bSgjelinek err = zonecfg_lookup_pset(handle, tabptr); 66560209230bSgjelinek 66570209230bSgjelinek (void) zonecfg_endent(handle); 66580209230bSgjelinek 66590209230bSgjelinek return (err); 66600209230bSgjelinek } 66610209230bSgjelinek 66620209230bSgjelinek static int 66630209230bSgjelinek add_mcap(zone_dochandle_t handle, struct zone_mcaptab *tabptr) 66640209230bSgjelinek { 66650209230bSgjelinek xmlNodePtr newnode, cur = handle->zone_dh_cur; 66660209230bSgjelinek int err; 66670209230bSgjelinek 66680209230bSgjelinek newnode = xmlNewTextChild(cur, NULL, DTD_ELEM_MCAP, NULL); 66690209230bSgjelinek if ((err = newprop(newnode, DTD_ATTR_PHYSCAP, tabptr->zone_physmem_cap)) 66700209230bSgjelinek != Z_OK) 66710209230bSgjelinek return (err); 66720209230bSgjelinek 66730209230bSgjelinek return (Z_OK); 66740209230bSgjelinek } 66750209230bSgjelinek 66760209230bSgjelinek int 66770209230bSgjelinek zonecfg_delete_mcap(zone_dochandle_t handle) 66780209230bSgjelinek { 66790209230bSgjelinek int err; 66800209230bSgjelinek xmlNodePtr cur = handle->zone_dh_cur; 66810209230bSgjelinek 66820209230bSgjelinek if ((err = operation_prep(handle)) != Z_OK) 66830209230bSgjelinek return (err); 66840209230bSgjelinek 66850209230bSgjelinek for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) { 66860209230bSgjelinek if (xmlStrcmp(cur->name, DTD_ELEM_MCAP) != 0) 66870209230bSgjelinek continue; 66880209230bSgjelinek 66890209230bSgjelinek xmlUnlinkNode(cur); 66900209230bSgjelinek xmlFreeNode(cur); 66910209230bSgjelinek return (Z_OK); 66920209230bSgjelinek } 66930209230bSgjelinek return (Z_NO_RESOURCE_ID); 66940209230bSgjelinek } 66950209230bSgjelinek 66960209230bSgjelinek int 66970209230bSgjelinek zonecfg_modify_mcap(zone_dochandle_t handle, struct zone_mcaptab *tabptr) 66980209230bSgjelinek { 66990209230bSgjelinek int err; 67000209230bSgjelinek 67010209230bSgjelinek if (tabptr == NULL) 67020209230bSgjelinek return (Z_INVAL); 67030209230bSgjelinek 67040209230bSgjelinek err = zonecfg_delete_mcap(handle); 67050209230bSgjelinek /* it is ok if there is no mcap entry */ 67060209230bSgjelinek if (err != Z_OK && err != Z_NO_RESOURCE_ID) 67070209230bSgjelinek return (err); 67080209230bSgjelinek 67090209230bSgjelinek if ((err = add_mcap(handle, tabptr)) != Z_OK) 67100209230bSgjelinek return (err); 67110209230bSgjelinek 67120209230bSgjelinek return (Z_OK); 67130209230bSgjelinek } 67140209230bSgjelinek 67150209230bSgjelinek int 67160209230bSgjelinek zonecfg_lookup_mcap(zone_dochandle_t handle, struct zone_mcaptab *tabptr) 67170209230bSgjelinek { 67180209230bSgjelinek xmlNodePtr cur; 67190209230bSgjelinek int err; 67200209230bSgjelinek 67210209230bSgjelinek if (tabptr == NULL) 67220209230bSgjelinek return (Z_INVAL); 67230209230bSgjelinek 67240209230bSgjelinek if ((err = operation_prep(handle)) != Z_OK) 67250209230bSgjelinek return (err); 67260209230bSgjelinek 67270209230bSgjelinek cur = handle->zone_dh_cur; 67280209230bSgjelinek for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) { 67290209230bSgjelinek if (xmlStrcmp(cur->name, DTD_ELEM_MCAP) != 0) 67300209230bSgjelinek continue; 67310209230bSgjelinek if ((err = fetchprop(cur, DTD_ATTR_PHYSCAP, 67320209230bSgjelinek tabptr->zone_physmem_cap, 67330209230bSgjelinek sizeof (tabptr->zone_physmem_cap))) != Z_OK) { 67340209230bSgjelinek handle->zone_dh_cur = handle->zone_dh_top; 67350209230bSgjelinek return (err); 67360209230bSgjelinek } 67370209230bSgjelinek 67380209230bSgjelinek return (Z_OK); 67390209230bSgjelinek } 67400209230bSgjelinek 67410209230bSgjelinek return (Z_NO_ENTRY); 67420209230bSgjelinek } 67430209230bSgjelinek 67440209230bSgjelinek static int 67450209230bSgjelinek getmcapent_core(zone_dochandle_t handle, struct zone_mcaptab *tabptr) 67460209230bSgjelinek { 67470209230bSgjelinek xmlNodePtr cur; 67480209230bSgjelinek int err; 67490209230bSgjelinek 67500209230bSgjelinek if (handle == NULL) 67510209230bSgjelinek return (Z_INVAL); 67520209230bSgjelinek 67530209230bSgjelinek if ((cur = handle->zone_dh_cur) == NULL) 67540209230bSgjelinek return (Z_NO_ENTRY); 67550209230bSgjelinek 67560209230bSgjelinek for (; cur != NULL; cur = cur->next) 67570209230bSgjelinek if (xmlStrcmp(cur->name, DTD_ELEM_MCAP) == 0) 67580209230bSgjelinek break; 67590209230bSgjelinek if (cur == NULL) { 67600209230bSgjelinek handle->zone_dh_cur = handle->zone_dh_top; 67610209230bSgjelinek return (Z_NO_ENTRY); 67620209230bSgjelinek } 67630209230bSgjelinek 67640209230bSgjelinek if ((err = fetchprop(cur, DTD_ATTR_PHYSCAP, tabptr->zone_physmem_cap, 67650209230bSgjelinek sizeof (tabptr->zone_physmem_cap))) != Z_OK) { 67660209230bSgjelinek handle->zone_dh_cur = handle->zone_dh_top; 67670209230bSgjelinek return (err); 67680209230bSgjelinek } 67690209230bSgjelinek 67700209230bSgjelinek handle->zone_dh_cur = cur->next; 67710209230bSgjelinek return (Z_OK); 67720209230bSgjelinek } 67730209230bSgjelinek 67740209230bSgjelinek int 67750209230bSgjelinek zonecfg_getmcapent(zone_dochandle_t handle, struct zone_mcaptab *tabptr) 67760209230bSgjelinek { 67770209230bSgjelinek int err; 67780209230bSgjelinek 67790209230bSgjelinek if ((err = zonecfg_setent(handle)) != Z_OK) 67800209230bSgjelinek return (err); 67810209230bSgjelinek 67820209230bSgjelinek err = getmcapent_core(handle, tabptr); 67830209230bSgjelinek 67840209230bSgjelinek (void) zonecfg_endent(handle); 67850209230bSgjelinek 67860209230bSgjelinek return (err); 67870209230bSgjelinek } 67880209230bSgjelinek 67896cfd72c6Sgjelinek /* 67906cfd72c6Sgjelinek * Get the full tree of pkg/patch metadata in a set of nested AVL trees. 67916cfd72c6Sgjelinek * pkgs_avl is an AVL tree of pkgs. Each pkg element contains a 67926cfd72c6Sgjelinek * zpe_patches_avl member which holds an AVL tree of patches for that pkg. 67936cfd72c6Sgjelinek * The patch elements have the same zpe_patches_avl member, each of which can 67946cfd72c6Sgjelinek * hold an AVL tree of patches that are obsoleted by the patch. 67956cfd72c6Sgjelinek * 67966cfd72c6Sgjelinek * The zone xml data contains DTD_ELEM_PACKAGE elements, followed by 67976cfd72c6Sgjelinek * DTD_ELEM_PATCH elements. The DTD_ELEM_PATCH patch element applies to the 67986cfd72c6Sgjelinek * DTD_ELEM_PACKAGE that precedes it. The DTD_ELEM_PATCH element may have 67996cfd72c6Sgjelinek * child DTD_ELEM_OBSOLETES nodes associated with it. The DTD_ELEM_PACKAGE 68006cfd72c6Sgjelinek * really should have had the DTD_ELEM_PATCH elements as children but it 68016cfd72c6Sgjelinek * was not defined that way initially so we are stuck with the DTD definition 68026cfd72c6Sgjelinek * now. However, we can safely assume the ordering for compatibility. 68036cfd72c6Sgjelinek */ 6804ee519a1fSgjelinek int 68056cfd72c6Sgjelinek zonecfg_getpkgdata(zone_dochandle_t handle, uu_avl_pool_t *pkg_pool, 68066cfd72c6Sgjelinek uu_avl_t *pkgs_avl) 6807ee519a1fSgjelinek { 6808ee519a1fSgjelinek xmlNodePtr cur; 68096cfd72c6Sgjelinek int res; 68106cfd72c6Sgjelinek zone_pkg_entry_t *pkg; 68116cfd72c6Sgjelinek char name[MAXNAMELEN]; 68126cfd72c6Sgjelinek char version[ZONE_PKG_VERSMAX]; 6813ee519a1fSgjelinek 6814ee519a1fSgjelinek if (handle == NULL) 6815ee519a1fSgjelinek return (Z_INVAL); 6816ee519a1fSgjelinek 68176cfd72c6Sgjelinek if ((res = zonecfg_setent(handle)) != Z_OK) 68186cfd72c6Sgjelinek return (res); 6819ee519a1fSgjelinek 68206cfd72c6Sgjelinek if ((cur = handle->zone_dh_cur) == NULL) { 68216cfd72c6Sgjelinek res = Z_NO_ENTRY; 68226cfd72c6Sgjelinek goto done; 6823ee519a1fSgjelinek } 6824ee519a1fSgjelinek 68256cfd72c6Sgjelinek for (; cur != NULL; cur = cur->next) { 68266cfd72c6Sgjelinek if (xmlStrcmp(cur->name, DTD_ELEM_PACKAGE) == 0) { 68276cfd72c6Sgjelinek uu_avl_index_t where; 68286cfd72c6Sgjelinek 68296cfd72c6Sgjelinek if ((res = fetchprop(cur, DTD_ATTR_NAME, name, 68306cfd72c6Sgjelinek sizeof (name))) != Z_OK) 68316cfd72c6Sgjelinek goto done; 68326cfd72c6Sgjelinek 68336cfd72c6Sgjelinek if ((res = fetchprop(cur, DTD_ATTR_VERSION, version, 68346cfd72c6Sgjelinek sizeof (version))) != Z_OK) 68356cfd72c6Sgjelinek goto done; 68366cfd72c6Sgjelinek 68376cfd72c6Sgjelinek if ((pkg = (zone_pkg_entry_t *) 68386cfd72c6Sgjelinek malloc(sizeof (zone_pkg_entry_t))) == NULL) { 68396cfd72c6Sgjelinek res = Z_NOMEM; 68406cfd72c6Sgjelinek goto done; 6841ee519a1fSgjelinek } 6842ee519a1fSgjelinek 68436cfd72c6Sgjelinek if ((pkg->zpe_name = strdup(name)) == NULL) { 68446cfd72c6Sgjelinek free(pkg); 68456cfd72c6Sgjelinek res = Z_NOMEM; 68466cfd72c6Sgjelinek goto done; 6847ee519a1fSgjelinek } 6848ee519a1fSgjelinek 68496cfd72c6Sgjelinek if ((pkg->zpe_vers = strdup(version)) == NULL) { 68506cfd72c6Sgjelinek free(pkg->zpe_name); 68516cfd72c6Sgjelinek free(pkg); 68526cfd72c6Sgjelinek res = Z_NOMEM; 68536cfd72c6Sgjelinek goto done; 6854ee519a1fSgjelinek } 6855ee519a1fSgjelinek 68566cfd72c6Sgjelinek pkg->zpe_patches_avl = NULL; 68576cfd72c6Sgjelinek 68586cfd72c6Sgjelinek uu_avl_node_init(pkg, &pkg->zpe_entry, pkg_pool); 68596cfd72c6Sgjelinek if (uu_avl_find(pkgs_avl, pkg, NULL, &where) != NULL) { 68606cfd72c6Sgjelinek free(pkg->zpe_name); 68616cfd72c6Sgjelinek free(pkg->zpe_vers); 68626cfd72c6Sgjelinek free(pkg); 68636cfd72c6Sgjelinek } else { 68646cfd72c6Sgjelinek uu_avl_insert(pkgs_avl, pkg, where); 6865ee519a1fSgjelinek } 6866ee519a1fSgjelinek 68676cfd72c6Sgjelinek } else if (xmlStrcmp(cur->name, DTD_ELEM_PATCH) == 0) { 68686cfd72c6Sgjelinek zone_pkg_entry_t *patch; 68696cfd72c6Sgjelinek uu_avl_index_t where; 68706cfd72c6Sgjelinek char *p; 68716cfd72c6Sgjelinek char *dashp = NULL; 68726cfd72c6Sgjelinek xmlNodePtr child; 68736cfd72c6Sgjelinek 68746cfd72c6Sgjelinek if ((res = fetchprop(cur, DTD_ATTR_ID, name, 68756cfd72c6Sgjelinek sizeof (name))) != Z_OK) 68766cfd72c6Sgjelinek goto done; 68776cfd72c6Sgjelinek 68786cfd72c6Sgjelinek if ((patch = (zone_pkg_entry_t *) 68796cfd72c6Sgjelinek malloc(sizeof (zone_pkg_entry_t))) == NULL) { 68806cfd72c6Sgjelinek res = Z_NOMEM; 68816cfd72c6Sgjelinek goto done; 6882ee519a1fSgjelinek } 6883ee519a1fSgjelinek 68846cfd72c6Sgjelinek if ((p = strchr(name, '-')) != NULL) { 68856cfd72c6Sgjelinek dashp = p; 68866cfd72c6Sgjelinek *p++ = '\0'; 68876cfd72c6Sgjelinek } else { 68886cfd72c6Sgjelinek p = ""; 6889ee519a1fSgjelinek } 6890ee519a1fSgjelinek 68916cfd72c6Sgjelinek if ((patch->zpe_name = strdup(name)) == NULL) { 68926cfd72c6Sgjelinek free(patch); 68936cfd72c6Sgjelinek res = Z_NOMEM; 68946cfd72c6Sgjelinek goto done; 6895ee519a1fSgjelinek } 6896ee519a1fSgjelinek 68976cfd72c6Sgjelinek if ((patch->zpe_vers = strdup(p)) == NULL) { 68986cfd72c6Sgjelinek free(patch->zpe_name); 68996cfd72c6Sgjelinek free(patch); 69006cfd72c6Sgjelinek res = Z_NOMEM; 69016cfd72c6Sgjelinek goto done; 6902ee519a1fSgjelinek } 6903ee519a1fSgjelinek 69046cfd72c6Sgjelinek if (dashp != NULL) 69056cfd72c6Sgjelinek *dashp = '-'; 69066cfd72c6Sgjelinek 69076cfd72c6Sgjelinek patch->zpe_patches_avl = NULL; 69086cfd72c6Sgjelinek 69096cfd72c6Sgjelinek if (pkg->zpe_patches_avl == NULL) { 69106cfd72c6Sgjelinek pkg->zpe_patches_avl = uu_avl_create(pkg_pool, 69116cfd72c6Sgjelinek NULL, UU_DEFAULT); 69126cfd72c6Sgjelinek if (pkg->zpe_patches_avl == NULL) { 69136cfd72c6Sgjelinek free(patch->zpe_name); 69146cfd72c6Sgjelinek free(patch->zpe_vers); 69156cfd72c6Sgjelinek free(patch); 69166cfd72c6Sgjelinek res = Z_NOMEM; 69176cfd72c6Sgjelinek goto done; 69186cfd72c6Sgjelinek } 69196cfd72c6Sgjelinek } 69206cfd72c6Sgjelinek 69216cfd72c6Sgjelinek uu_avl_node_init(patch, &patch->zpe_entry, pkg_pool); 69226cfd72c6Sgjelinek if (uu_avl_find(pkg->zpe_patches_avl, patch, NULL, 69236cfd72c6Sgjelinek &where) != NULL) { 69246cfd72c6Sgjelinek free(patch->zpe_name); 69256cfd72c6Sgjelinek free(patch->zpe_vers); 69266cfd72c6Sgjelinek free(patch); 69276cfd72c6Sgjelinek } else { 69286cfd72c6Sgjelinek uu_avl_insert(pkg->zpe_patches_avl, patch, 69296cfd72c6Sgjelinek where); 69306cfd72c6Sgjelinek } 69316cfd72c6Sgjelinek 69326cfd72c6Sgjelinek /* Add any patches this patch obsoletes. */ 69336cfd72c6Sgjelinek for (child = cur->xmlChildrenNode; child != NULL; 69346cfd72c6Sgjelinek child = child->next) { 69356cfd72c6Sgjelinek zone_pkg_entry_t *obs; 69366cfd72c6Sgjelinek 69376cfd72c6Sgjelinek if (xmlStrcmp(child->name, DTD_ELEM_OBSOLETES) 69386cfd72c6Sgjelinek != 0) 69396cfd72c6Sgjelinek continue; 69406cfd72c6Sgjelinek 69416cfd72c6Sgjelinek if ((res = fetchprop(child, DTD_ATTR_ID, 69426cfd72c6Sgjelinek name, sizeof (name))) != Z_OK) 69436cfd72c6Sgjelinek goto done; 69446cfd72c6Sgjelinek 69456cfd72c6Sgjelinek if ((obs = (zone_pkg_entry_t *)malloc( 69466cfd72c6Sgjelinek sizeof (zone_pkg_entry_t))) == NULL) { 69476cfd72c6Sgjelinek res = Z_NOMEM; 69486cfd72c6Sgjelinek goto done; 69496cfd72c6Sgjelinek } 69506cfd72c6Sgjelinek 69516cfd72c6Sgjelinek if ((obs->zpe_name = strdup(name)) == NULL) { 69526cfd72c6Sgjelinek free(obs); 69536cfd72c6Sgjelinek res = Z_NOMEM; 69546cfd72c6Sgjelinek goto done; 69556cfd72c6Sgjelinek } 69566cfd72c6Sgjelinek /* 69576cfd72c6Sgjelinek * The version doesn't matter for obsoleted 69586cfd72c6Sgjelinek * patches. 69596cfd72c6Sgjelinek */ 69606cfd72c6Sgjelinek obs->zpe_vers = NULL; 69616cfd72c6Sgjelinek obs->zpe_patches_avl = NULL; 69626cfd72c6Sgjelinek 69636cfd72c6Sgjelinek /* 69646cfd72c6Sgjelinek * If this is the first obsolete patch, add an 69656cfd72c6Sgjelinek * AVL tree to the parent patch element. 69666cfd72c6Sgjelinek */ 69676cfd72c6Sgjelinek if (patch->zpe_patches_avl == NULL) { 69686cfd72c6Sgjelinek patch->zpe_patches_avl = 69696cfd72c6Sgjelinek uu_avl_create(pkg_pool, NULL, 69706cfd72c6Sgjelinek UU_DEFAULT); 69716cfd72c6Sgjelinek if (patch->zpe_patches_avl == NULL) { 69726cfd72c6Sgjelinek free(obs->zpe_name); 69736cfd72c6Sgjelinek free(obs); 69746cfd72c6Sgjelinek res = Z_NOMEM; 69756cfd72c6Sgjelinek goto done; 69766cfd72c6Sgjelinek } 69776cfd72c6Sgjelinek } 69786cfd72c6Sgjelinek 69796cfd72c6Sgjelinek /* Insert obsolete patch into the AVL tree. */ 69806cfd72c6Sgjelinek uu_avl_node_init(obs, &obs->zpe_entry, 69816cfd72c6Sgjelinek pkg_pool); 69826cfd72c6Sgjelinek if (uu_avl_find(patch->zpe_patches_avl, obs, 69836cfd72c6Sgjelinek NULL, &where) != NULL) { 69846cfd72c6Sgjelinek free(obs->zpe_name); 69856cfd72c6Sgjelinek free(obs); 69866cfd72c6Sgjelinek } else { 69876cfd72c6Sgjelinek uu_avl_insert(patch->zpe_patches_avl, 69886cfd72c6Sgjelinek obs, where); 69896cfd72c6Sgjelinek } 69906cfd72c6Sgjelinek } 69916cfd72c6Sgjelinek } 69926cfd72c6Sgjelinek } 69936cfd72c6Sgjelinek 69946cfd72c6Sgjelinek done: 69956cfd72c6Sgjelinek (void) zonecfg_endent(handle); 69966cfd72c6Sgjelinek return (res); 6997ee519a1fSgjelinek } 6998ee519a1fSgjelinek 6999ee519a1fSgjelinek int 7000ee519a1fSgjelinek zonecfg_setdevperment(zone_dochandle_t handle) 7001ee519a1fSgjelinek { 7002ee519a1fSgjelinek return (zonecfg_setent(handle)); 7003ee519a1fSgjelinek } 7004ee519a1fSgjelinek 7005ee519a1fSgjelinek int 7006ee519a1fSgjelinek zonecfg_getdevperment(zone_dochandle_t handle, struct zone_devpermtab *tabptr) 7007ee519a1fSgjelinek { 7008ee519a1fSgjelinek xmlNodePtr cur; 7009ee519a1fSgjelinek int err; 7010ee519a1fSgjelinek char buf[128]; 7011ee519a1fSgjelinek 7012ee519a1fSgjelinek tabptr->zone_devperm_acl = NULL; 7013ee519a1fSgjelinek 7014ee519a1fSgjelinek if (handle == NULL) 7015ee519a1fSgjelinek return (Z_INVAL); 7016ee519a1fSgjelinek 7017ee519a1fSgjelinek if ((cur = handle->zone_dh_cur) == NULL) 7018ee519a1fSgjelinek return (Z_NO_ENTRY); 7019ee519a1fSgjelinek 7020ee519a1fSgjelinek for (; cur != NULL; cur = cur->next) 7021ee519a1fSgjelinek if (!xmlStrcmp(cur->name, DTD_ELEM_DEV_PERM)) 7022ee519a1fSgjelinek break; 7023ee519a1fSgjelinek if (cur == NULL) { 7024ee519a1fSgjelinek handle->zone_dh_cur = handle->zone_dh_top; 7025ee519a1fSgjelinek return (Z_NO_ENTRY); 7026ee519a1fSgjelinek } 7027ee519a1fSgjelinek 7028ee519a1fSgjelinek if ((err = fetchprop(cur, DTD_ATTR_NAME, tabptr->zone_devperm_name, 7029ee519a1fSgjelinek sizeof (tabptr->zone_devperm_name))) != Z_OK) { 7030ee519a1fSgjelinek handle->zone_dh_cur = handle->zone_dh_top; 7031ee519a1fSgjelinek return (err); 7032ee519a1fSgjelinek } 7033ee519a1fSgjelinek 7034ee519a1fSgjelinek if ((err = fetchprop(cur, DTD_ATTR_UID, buf, sizeof (buf))) != Z_OK) { 7035ee519a1fSgjelinek handle->zone_dh_cur = handle->zone_dh_top; 7036ee519a1fSgjelinek return (err); 7037ee519a1fSgjelinek } 7038ee519a1fSgjelinek tabptr->zone_devperm_uid = (uid_t)atol(buf); 7039ee519a1fSgjelinek 7040ee519a1fSgjelinek if ((err = fetchprop(cur, DTD_ATTR_GID, buf, sizeof (buf))) != Z_OK) { 7041ee519a1fSgjelinek handle->zone_dh_cur = handle->zone_dh_top; 7042ee519a1fSgjelinek return (err); 7043ee519a1fSgjelinek } 7044ee519a1fSgjelinek tabptr->zone_devperm_gid = (gid_t)atol(buf); 7045ee519a1fSgjelinek 7046ee519a1fSgjelinek if ((err = fetchprop(cur, DTD_ATTR_MODE, buf, sizeof (buf))) != Z_OK) { 7047ee519a1fSgjelinek handle->zone_dh_cur = handle->zone_dh_top; 7048ee519a1fSgjelinek return (err); 7049ee519a1fSgjelinek } 7050ee519a1fSgjelinek tabptr->zone_devperm_mode = (mode_t)strtol(buf, (char **)NULL, 8); 7051ee519a1fSgjelinek 7052ee519a1fSgjelinek if ((err = fetch_alloc_prop(cur, DTD_ATTR_ACL, 7053ee519a1fSgjelinek &(tabptr->zone_devperm_acl))) != Z_OK) { 7054ee519a1fSgjelinek handle->zone_dh_cur = handle->zone_dh_top; 7055ee519a1fSgjelinek return (err); 7056ee519a1fSgjelinek } 7057ee519a1fSgjelinek 7058ee519a1fSgjelinek handle->zone_dh_cur = cur->next; 7059ee519a1fSgjelinek return (Z_OK); 7060ee519a1fSgjelinek } 7061ee519a1fSgjelinek 7062ee519a1fSgjelinek int 7063ee519a1fSgjelinek zonecfg_enddevperment(zone_dochandle_t handle) 7064ee519a1fSgjelinek { 7065ee519a1fSgjelinek return (zonecfg_endent(handle)); 7066ee519a1fSgjelinek } 7067ee519a1fSgjelinek 7068ff17c8bfSgjelinek /* PRINTFLIKE1 */ 7069ff17c8bfSgjelinek static void 7070ff17c8bfSgjelinek zerror(const char *zone_name, const char *fmt, ...) 707139935be5Sgjelinek { 7072ff17c8bfSgjelinek va_list alist; 707339935be5Sgjelinek 7074ff17c8bfSgjelinek va_start(alist, fmt); 7075ff17c8bfSgjelinek (void) fprintf(stderr, "zone '%s': ", zone_name); 7076ff17c8bfSgjelinek (void) vfprintf(stderr, fmt, alist); 7077ff17c8bfSgjelinek (void) fprintf(stderr, "\n"); 7078ff17c8bfSgjelinek va_end(alist); 7079ee519a1fSgjelinek } 7080ee519a1fSgjelinek 7081ee519a1fSgjelinek static void 7082ff17c8bfSgjelinek zperror(const char *str) 7083ee519a1fSgjelinek { 7084ff17c8bfSgjelinek (void) fprintf(stderr, "%s: %s\n", str, strerror(errno)); 7085ee519a1fSgjelinek } 7086ee519a1fSgjelinek 7087ee519a1fSgjelinek /* 7088ff17c8bfSgjelinek * The following three routines implement a simple locking mechanism to 7089ff17c8bfSgjelinek * ensure that only one instance of zoneadm at a time is able to manipulate 7090ff17c8bfSgjelinek * a given zone. The lock is built on top of an fcntl(2) lock of 7091ff17c8bfSgjelinek * [<altroot>]/var/run/zones/<zonename>.zoneadm.lock. If a zoneadm instance 7092ff17c8bfSgjelinek * can grab that lock, it is allowed to manipulate the zone. 7093ff17c8bfSgjelinek * 7094ff17c8bfSgjelinek * Since zoneadm may call external applications which in turn invoke 7095ff17c8bfSgjelinek * zoneadm again, we introduce the notion of "lock inheritance". Any 7096ff17c8bfSgjelinek * instance of zoneadm that has another instance in its ancestry is assumed 7097ff17c8bfSgjelinek * to be acting on behalf of the original zoneadm, and is thus allowed to 7098ff17c8bfSgjelinek * manipulate its zone. 7099ff17c8bfSgjelinek * 7100ff17c8bfSgjelinek * This inheritance is implemented via the _ZONEADM_LOCK_HELD environment 7101ff17c8bfSgjelinek * variable. When zoneadm is granted a lock on its zone, this environment 7102ff17c8bfSgjelinek * variable is set to 1. When it releases the lock, the variable is set to 7103ff17c8bfSgjelinek * 0. Since a child process inherits its parent's environment, checking 7104ff17c8bfSgjelinek * the state of this variable indicates whether or not any ancestor owns 7105ff17c8bfSgjelinek * the lock. 710639935be5Sgjelinek */ 7107ff17c8bfSgjelinek void 7108ff17c8bfSgjelinek zonecfg_init_lock_file(const char *zone_name, char **lock_env) 710939935be5Sgjelinek { 7110ff17c8bfSgjelinek *lock_env = getenv(LOCK_ENV_VAR); 7111ff17c8bfSgjelinek if (*lock_env == NULL) { 7112ff17c8bfSgjelinek if (putenv(zoneadm_lock_not_held) != 0) { 7113ff17c8bfSgjelinek zerror(zone_name, gettext("could not set env: %s"), 7114ff17c8bfSgjelinek strerror(errno)); 7115ff17c8bfSgjelinek exit(1); 7116ff17c8bfSgjelinek } 711739935be5Sgjelinek } else { 7118ff17c8bfSgjelinek if (atoi(*lock_env) == 1) 7119ff17c8bfSgjelinek zone_lock_cnt = 1; 712039935be5Sgjelinek } 712139935be5Sgjelinek } 712239935be5Sgjelinek 7123ff17c8bfSgjelinek void 7124ff17c8bfSgjelinek zonecfg_release_lock_file(const char *zone_name, int lockfd) 7125ff17c8bfSgjelinek { 7126ff17c8bfSgjelinek /* 7127ff17c8bfSgjelinek * If we are cleaning up from a failed attempt to lock the zone for 7128ff17c8bfSgjelinek * the first time, we might have a zone_lock_cnt of 0. In that 7129ff17c8bfSgjelinek * error case, we don't want to do anything but close the lock 7130ff17c8bfSgjelinek * file. 7131ff17c8bfSgjelinek */ 7132ff17c8bfSgjelinek assert(zone_lock_cnt >= 0); 7133ff17c8bfSgjelinek if (zone_lock_cnt > 0) { 7134ff17c8bfSgjelinek assert(getenv(LOCK_ENV_VAR) != NULL); 7135ff17c8bfSgjelinek assert(atoi(getenv(LOCK_ENV_VAR)) == 1); 7136ff17c8bfSgjelinek if (--zone_lock_cnt > 0) { 7137ff17c8bfSgjelinek assert(lockfd == -1); 7138ff17c8bfSgjelinek return; 713939935be5Sgjelinek } 7140ff17c8bfSgjelinek if (putenv(zoneadm_lock_not_held) != 0) { 7141ff17c8bfSgjelinek zerror(zone_name, gettext("could not set env: %s"), 7142ff17c8bfSgjelinek strerror(errno)); 7143ff17c8bfSgjelinek exit(1); 7144ff17c8bfSgjelinek } 7145ff17c8bfSgjelinek } 7146ff17c8bfSgjelinek assert(lockfd >= 0); 7147ff17c8bfSgjelinek (void) close(lockfd); 714839935be5Sgjelinek } 714939935be5Sgjelinek 7150ff17c8bfSgjelinek int 7151ff17c8bfSgjelinek zonecfg_grab_lock_file(const char *zone_name, int *lockfd) 7152ff17c8bfSgjelinek { 7153ff17c8bfSgjelinek char pathbuf[PATH_MAX]; 7154ff17c8bfSgjelinek struct flock flock; 7155ff17c8bfSgjelinek 7156ff17c8bfSgjelinek /* 7157ff17c8bfSgjelinek * If we already have the lock, we can skip this expensive song 7158ff17c8bfSgjelinek * and dance. 7159ff17c8bfSgjelinek */ 7160ff17c8bfSgjelinek assert(zone_lock_cnt >= 0); 7161ff17c8bfSgjelinek assert(getenv(LOCK_ENV_VAR) != NULL); 7162ff17c8bfSgjelinek if (zone_lock_cnt > 0) { 7163ff17c8bfSgjelinek assert(atoi(getenv(LOCK_ENV_VAR)) == 1); 7164ff17c8bfSgjelinek zone_lock_cnt++; 7165ff17c8bfSgjelinek *lockfd = -1; 716639935be5Sgjelinek return (Z_OK); 716739935be5Sgjelinek } 7168ff17c8bfSgjelinek assert(getenv(LOCK_ENV_VAR) != NULL); 7169ff17c8bfSgjelinek assert(atoi(getenv(LOCK_ENV_VAR)) == 0); 717039935be5Sgjelinek 7171ff17c8bfSgjelinek if (snprintf(pathbuf, sizeof (pathbuf), "%s%s", zonecfg_get_root(), 7172ff17c8bfSgjelinek ZONES_TMPDIR) >= sizeof (pathbuf)) { 7173ff17c8bfSgjelinek zerror(zone_name, gettext("alternate root path is too long")); 717439935be5Sgjelinek return (-1); 717539935be5Sgjelinek } 7176ff17c8bfSgjelinek if (mkdir(pathbuf, S_IRWXU) < 0 && errno != EEXIST) { 7177ff17c8bfSgjelinek zerror(zone_name, gettext("could not mkdir %s: %s"), pathbuf, 7178ff17c8bfSgjelinek strerror(errno)); 7179ff17c8bfSgjelinek return (-1); 7180ff17c8bfSgjelinek } 7181ff17c8bfSgjelinek (void) chmod(pathbuf, S_IRWXU); 718239935be5Sgjelinek 718339935be5Sgjelinek /* 7184ff17c8bfSgjelinek * One of these lock files is created for each zone (when needed). 7185ff17c8bfSgjelinek * The lock files are not cleaned up (except on system reboot), 7186ff17c8bfSgjelinek * but since there is only one per zone, there is no resource 7187ff17c8bfSgjelinek * starvation issue. 718839935be5Sgjelinek */ 7189ff17c8bfSgjelinek if (snprintf(pathbuf, sizeof (pathbuf), "%s%s/%s.zoneadm.lock", 7190ff17c8bfSgjelinek zonecfg_get_root(), ZONES_TMPDIR, zone_name) >= sizeof (pathbuf)) { 7191ff17c8bfSgjelinek zerror(zone_name, gettext("alternate root path is too long")); 7192ff17c8bfSgjelinek return (-1); 7193ff17c8bfSgjelinek } 7194ff17c8bfSgjelinek if ((*lockfd = open(pathbuf, O_RDWR|O_CREAT, S_IRUSR|S_IWUSR)) < 0) { 7195ff17c8bfSgjelinek zerror(zone_name, gettext("could not open %s: %s"), pathbuf, 7196ff17c8bfSgjelinek strerror(errno)); 7197ff17c8bfSgjelinek return (-1); 7198ff17c8bfSgjelinek } 7199ff17c8bfSgjelinek /* 7200ff17c8bfSgjelinek * Lock the file to synchronize with other zoneadmds 7201ff17c8bfSgjelinek */ 7202ff17c8bfSgjelinek flock.l_type = F_WRLCK; 7203ff17c8bfSgjelinek flock.l_whence = SEEK_SET; 7204ff17c8bfSgjelinek flock.l_start = (off_t)0; 7205ff17c8bfSgjelinek flock.l_len = (off_t)0; 7206ff17c8bfSgjelinek if ((fcntl(*lockfd, F_SETLKW, &flock) < 0) || 7207ff17c8bfSgjelinek (putenv(zoneadm_lock_held) != 0)) { 7208ff17c8bfSgjelinek zerror(zone_name, gettext("unable to lock %s: %s"), pathbuf, 7209ff17c8bfSgjelinek strerror(errno)); 7210ff17c8bfSgjelinek zonecfg_release_lock_file(zone_name, *lockfd); 7211ff17c8bfSgjelinek return (-1); 7212ff17c8bfSgjelinek } 7213ff17c8bfSgjelinek zone_lock_cnt = 1; 7214ff17c8bfSgjelinek return (Z_OK); 7215ff17c8bfSgjelinek } 7216ff17c8bfSgjelinek 7217edfa49ffS boolean_t 7218edfa49ffS zonecfg_lock_file_held(int *lockfd) 7219edfa49ffS { 7220edfa49ffS if (*lockfd >= 0 || zone_lock_cnt > 0) 7221edfa49ffS return (B_TRUE); 7222edfa49ffS return (B_FALSE); 7223edfa49ffS } 7224edfa49ffS 7225ff17c8bfSgjelinek static boolean_t 7226ff17c8bfSgjelinek get_doorname(const char *zone_name, char *buffer) 722739935be5Sgjelinek { 7228ff17c8bfSgjelinek return (snprintf(buffer, PATH_MAX, "%s" ZONE_DOOR_PATH, 7229ff17c8bfSgjelinek zonecfg_get_root(), zone_name) < PATH_MAX); 7230e66285fdSgjelinek } 7231ee519a1fSgjelinek 7232ee519a1fSgjelinek /* 7233ff17c8bfSgjelinek * system daemons are not audited. For the global zone, this occurs 7234ff17c8bfSgjelinek * "naturally" since init is started with the default audit 7235ff17c8bfSgjelinek * characteristics. Since zoneadmd is a system daemon and it starts 7236ff17c8bfSgjelinek * init for a zone, it is necessary to clear out the audit 7237ff17c8bfSgjelinek * characteristics inherited from whomever started zoneadmd. This is 7238ff17c8bfSgjelinek * indicated by the audit id, which is set from the ruid parameter of 7239ff17c8bfSgjelinek * adt_set_user(), below. 7240ee519a1fSgjelinek */ 7241ff17c8bfSgjelinek 7242ff17c8bfSgjelinek static void 7243ff17c8bfSgjelinek prepare_audit_context(const char *zone_name) 7244ff17c8bfSgjelinek { 7245ff17c8bfSgjelinek adt_session_data_t *ah; 7246ff17c8bfSgjelinek char *failure = gettext("audit failure: %s"); 7247ff17c8bfSgjelinek 7248ff17c8bfSgjelinek if (adt_start_session(&ah, NULL, 0)) { 7249ff17c8bfSgjelinek zerror(zone_name, failure, strerror(errno)); 7250ff17c8bfSgjelinek return; 7251ff17c8bfSgjelinek } 7252ff17c8bfSgjelinek if (adt_set_user(ah, ADT_NO_AUDIT, ADT_NO_AUDIT, 7253ff17c8bfSgjelinek ADT_NO_AUDIT, ADT_NO_AUDIT, NULL, ADT_NEW)) { 7254ff17c8bfSgjelinek zerror(zone_name, failure, strerror(errno)); 7255ff17c8bfSgjelinek (void) adt_end_session(ah); 7256ff17c8bfSgjelinek return; 7257ff17c8bfSgjelinek } 7258ff17c8bfSgjelinek if (adt_set_proc(ah)) 7259ff17c8bfSgjelinek zerror(zone_name, failure, strerror(errno)); 7260ff17c8bfSgjelinek 7261ff17c8bfSgjelinek (void) adt_end_session(ah); 7262ff17c8bfSgjelinek } 7263ff17c8bfSgjelinek 7264ff17c8bfSgjelinek static int 7265ff17c8bfSgjelinek start_zoneadmd(const char *zone_name, boolean_t lock) 7266ff17c8bfSgjelinek { 7267ff17c8bfSgjelinek char doorpath[PATH_MAX]; 7268ff17c8bfSgjelinek pid_t child_pid; 7269ff17c8bfSgjelinek int error = -1; 7270ff17c8bfSgjelinek int doorfd, lockfd; 7271ff17c8bfSgjelinek struct door_info info; 7272ff17c8bfSgjelinek 7273ff17c8bfSgjelinek if (!get_doorname(zone_name, doorpath)) 7274ff17c8bfSgjelinek return (-1); 7275ff17c8bfSgjelinek 7276ff17c8bfSgjelinek if (lock) 7277ff17c8bfSgjelinek if (zonecfg_grab_lock_file(zone_name, &lockfd) != Z_OK) 7278ff17c8bfSgjelinek return (-1); 7279ff17c8bfSgjelinek 7280ff17c8bfSgjelinek /* 7281ff17c8bfSgjelinek * Now that we have the lock, re-confirm that the daemon is 7282ff17c8bfSgjelinek * *not* up and working fine. If it is still down, we have a green 7283ff17c8bfSgjelinek * light to start it. 7284ff17c8bfSgjelinek */ 7285ff17c8bfSgjelinek if ((doorfd = open(doorpath, O_RDONLY)) < 0) { 7286ff17c8bfSgjelinek if (errno != ENOENT) { 7287ff17c8bfSgjelinek zperror(doorpath); 7288ff17c8bfSgjelinek goto out; 7289ff17c8bfSgjelinek } 7290ff17c8bfSgjelinek } else { 7291ff17c8bfSgjelinek if (door_info(doorfd, &info) == 0 && 7292ff17c8bfSgjelinek ((info.di_attributes & DOOR_REVOKED) == 0)) { 7293ff17c8bfSgjelinek error = Z_OK; 7294ff17c8bfSgjelinek (void) close(doorfd); 7295ff17c8bfSgjelinek goto out; 7296ff17c8bfSgjelinek } 7297ff17c8bfSgjelinek (void) close(doorfd); 7298ff17c8bfSgjelinek } 7299ff17c8bfSgjelinek 7300ff17c8bfSgjelinek if ((child_pid = fork()) == -1) { 7301ff17c8bfSgjelinek zperror(gettext("could not fork")); 7302ff17c8bfSgjelinek goto out; 7303ff17c8bfSgjelinek } 7304ff17c8bfSgjelinek 7305ff17c8bfSgjelinek if (child_pid == 0) { 7306ff17c8bfSgjelinek const char *argv[6], **ap; 7307ff17c8bfSgjelinek 7308ff17c8bfSgjelinek /* child process */ 7309ff17c8bfSgjelinek prepare_audit_context(zone_name); 7310ff17c8bfSgjelinek 7311ff17c8bfSgjelinek ap = argv; 7312ff17c8bfSgjelinek *ap++ = "zoneadmd"; 7313ff17c8bfSgjelinek *ap++ = "-z"; 7314ff17c8bfSgjelinek *ap++ = zone_name; 7315ff17c8bfSgjelinek if (zonecfg_in_alt_root()) { 7316ff17c8bfSgjelinek *ap++ = "-R"; 7317ff17c8bfSgjelinek *ap++ = zonecfg_get_root(); 7318ff17c8bfSgjelinek } 7319ff17c8bfSgjelinek *ap = NULL; 7320ff17c8bfSgjelinek 7321ff17c8bfSgjelinek (void) execv("/usr/lib/zones/zoneadmd", (char * const *)argv); 7322ff17c8bfSgjelinek /* 7323ff17c8bfSgjelinek * TRANSLATION_NOTE 7324ff17c8bfSgjelinek * zoneadmd is a literal that should not be translated. 7325ff17c8bfSgjelinek */ 7326ff17c8bfSgjelinek zperror(gettext("could not exec zoneadmd")); 7327ff17c8bfSgjelinek _exit(1); 7328ff17c8bfSgjelinek } else { 7329ff17c8bfSgjelinek /* parent process */ 7330ff17c8bfSgjelinek pid_t retval; 7331ff17c8bfSgjelinek int pstatus = 0; 7332ee519a1fSgjelinek 7333ee519a1fSgjelinek do { 7334ff17c8bfSgjelinek retval = waitpid(child_pid, &pstatus, 0); 7335ff17c8bfSgjelinek } while (retval != child_pid); 7336ff17c8bfSgjelinek if (WIFSIGNALED(pstatus) || (WIFEXITED(pstatus) && 7337ff17c8bfSgjelinek WEXITSTATUS(pstatus) != 0)) { 7338ff17c8bfSgjelinek zerror(zone_name, gettext("could not start %s"), 7339ff17c8bfSgjelinek "zoneadmd"); 7340ff17c8bfSgjelinek goto out; 73416cfd72c6Sgjelinek } 73426cfd72c6Sgjelinek } 7343ff17c8bfSgjelinek error = Z_OK; 7344ff17c8bfSgjelinek out: 7345ff17c8bfSgjelinek if (lock) 7346ff17c8bfSgjelinek zonecfg_release_lock_file(zone_name, lockfd); 7347ff17c8bfSgjelinek return (error); 7348ee519a1fSgjelinek } 7349ee519a1fSgjelinek 7350ee519a1fSgjelinek int 7351ff17c8bfSgjelinek zonecfg_ping_zoneadmd(const char *zone_name) 7352ee519a1fSgjelinek { 7353ff17c8bfSgjelinek char doorpath[PATH_MAX]; 7354ff17c8bfSgjelinek int doorfd; 7355ff17c8bfSgjelinek struct door_info info; 7356ee519a1fSgjelinek 7357ff17c8bfSgjelinek if (!get_doorname(zone_name, doorpath)) 7358ff17c8bfSgjelinek return (-1); 7359ee519a1fSgjelinek 7360ff17c8bfSgjelinek if ((doorfd = open(doorpath, O_RDONLY)) < 0) { 7361ff17c8bfSgjelinek return (-1); 7362ff17c8bfSgjelinek } 7363ff17c8bfSgjelinek if (door_info(doorfd, &info) == 0 && 7364ff17c8bfSgjelinek ((info.di_attributes & DOOR_REVOKED) == 0)) { 7365ff17c8bfSgjelinek (void) close(doorfd); 7366ff17c8bfSgjelinek return (Z_OK); 7367ff17c8bfSgjelinek } 7368ff17c8bfSgjelinek (void) close(doorfd); 7369ff17c8bfSgjelinek return (-1); 7370ff17c8bfSgjelinek } 7371ee519a1fSgjelinek 7372ff17c8bfSgjelinek int 7373ff17c8bfSgjelinek zonecfg_call_zoneadmd(const char *zone_name, zone_cmd_arg_t *arg, char *locale, 7374ff17c8bfSgjelinek boolean_t lock) 7375ff17c8bfSgjelinek { 7376ff17c8bfSgjelinek char doorpath[PATH_MAX]; 7377ff17c8bfSgjelinek int doorfd, result; 7378ff17c8bfSgjelinek door_arg_t darg; 7379ff17c8bfSgjelinek 7380ff17c8bfSgjelinek zoneid_t zoneid; 7381ff17c8bfSgjelinek uint64_t uniqid = 0; 7382ff17c8bfSgjelinek 7383ff17c8bfSgjelinek zone_cmd_rval_t *rvalp; 7384ff17c8bfSgjelinek size_t rlen; 7385ff17c8bfSgjelinek char *cp, *errbuf; 7386ff17c8bfSgjelinek 7387ff17c8bfSgjelinek rlen = getpagesize(); 7388ff17c8bfSgjelinek if ((rvalp = malloc(rlen)) == NULL) { 7389ff17c8bfSgjelinek zerror(zone_name, gettext("failed to allocate %lu bytes: %s"), 7390ff17c8bfSgjelinek rlen, strerror(errno)); 7391ff17c8bfSgjelinek return (-1); 7392ff17c8bfSgjelinek } 7393ff17c8bfSgjelinek 7394ff17c8bfSgjelinek if ((zoneid = getzoneidbyname(zone_name)) != ZONE_ID_UNDEFINED) { 7395ff17c8bfSgjelinek (void) zone_getattr(zoneid, ZONE_ATTR_UNIQID, &uniqid, 7396ff17c8bfSgjelinek sizeof (uniqid)); 7397ff17c8bfSgjelinek } 7398ff17c8bfSgjelinek arg->uniqid = uniqid; 7399ff17c8bfSgjelinek (void) strlcpy(arg->locale, locale, sizeof (arg->locale)); 7400ff17c8bfSgjelinek if (!get_doorname(zone_name, doorpath)) { 7401ff17c8bfSgjelinek zerror(zone_name, gettext("alternate root path is too long")); 7402ff17c8bfSgjelinek free(rvalp); 7403ff17c8bfSgjelinek return (-1); 7404ff17c8bfSgjelinek } 7405ff17c8bfSgjelinek 7406ff17c8bfSgjelinek /* 7407ff17c8bfSgjelinek * Loop trying to start zoneadmd; if something goes seriously 7408ff17c8bfSgjelinek * wrong we break out and fail. 7409ff17c8bfSgjelinek */ 7410ff17c8bfSgjelinek for (;;) { 7411ff17c8bfSgjelinek if (start_zoneadmd(zone_name, lock) != Z_OK) 7412ff17c8bfSgjelinek break; 7413ff17c8bfSgjelinek 7414ff17c8bfSgjelinek if ((doorfd = open(doorpath, O_RDONLY)) < 0) { 7415ff17c8bfSgjelinek zperror(gettext("failed to open zone door")); 7416ff17c8bfSgjelinek break; 7417ff17c8bfSgjelinek } 7418ff17c8bfSgjelinek 7419ff17c8bfSgjelinek darg.data_ptr = (char *)arg; 7420ff17c8bfSgjelinek darg.data_size = sizeof (*arg); 7421ff17c8bfSgjelinek darg.desc_ptr = NULL; 7422ff17c8bfSgjelinek darg.desc_num = 0; 7423ff17c8bfSgjelinek darg.rbuf = (char *)rvalp; 7424ff17c8bfSgjelinek darg.rsize = rlen; 7425ff17c8bfSgjelinek if (door_call(doorfd, &darg) != 0) { 7426ff17c8bfSgjelinek (void) close(doorfd); 7427ff17c8bfSgjelinek /* 7428ff17c8bfSgjelinek * We'll get EBADF if the door has been revoked. 7429ff17c8bfSgjelinek */ 7430ff17c8bfSgjelinek if (errno != EBADF) { 7431ff17c8bfSgjelinek zperror(gettext("door_call failed")); 7432ff17c8bfSgjelinek break; 7433ff17c8bfSgjelinek } 7434ff17c8bfSgjelinek continue; /* take another lap */ 7435ff17c8bfSgjelinek } 7436ff17c8bfSgjelinek (void) close(doorfd); 7437ff17c8bfSgjelinek 7438ff17c8bfSgjelinek if (darg.data_size == 0) { 7439ff17c8bfSgjelinek /* Door server is going away; kick it again. */ 7440ff17c8bfSgjelinek continue; 7441ff17c8bfSgjelinek } 7442ff17c8bfSgjelinek 7443ff17c8bfSgjelinek errbuf = rvalp->errbuf; 7444ff17c8bfSgjelinek while (*errbuf != '\0') { 7445ff17c8bfSgjelinek /* 7446ff17c8bfSgjelinek * Remove any newlines since zerror() 7447ff17c8bfSgjelinek * will append one automatically. 7448ff17c8bfSgjelinek */ 7449ff17c8bfSgjelinek cp = strchr(errbuf, '\n'); 7450ff17c8bfSgjelinek if (cp != NULL) 7451ff17c8bfSgjelinek *cp = '\0'; 7452ff17c8bfSgjelinek zerror(zone_name, "%s", errbuf); 7453ff17c8bfSgjelinek if (cp == NULL) 7454ff17c8bfSgjelinek break; 7455ff17c8bfSgjelinek errbuf = cp + 1; 7456ff17c8bfSgjelinek } 7457ff17c8bfSgjelinek result = rvalp->rval == 0 ? 0 : -1; 7458ff17c8bfSgjelinek free(rvalp); 7459ff17c8bfSgjelinek return (result); 7460ff17c8bfSgjelinek } 7461ff17c8bfSgjelinek 7462ff17c8bfSgjelinek free(rvalp); 7463ff17c8bfSgjelinek return (-1); 7464ee519a1fSgjelinek } 7465