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 /* 236cfd72c6Sgjelinek * Copyright 2008 Sun Microsystems, Inc. All rights reserved. 247c478bd9Sstevel@tonic-gate * Use is subject to license terms. 257c478bd9Sstevel@tonic-gate */ 267c478bd9Sstevel@tonic-gate 277c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 287c478bd9Sstevel@tonic-gate 29cf8f45c7Sdstaff #include <libsysevent.h> 30cf8f45c7Sdstaff #include <pthread.h> 31cf8f45c7Sdstaff #include <stdlib.h> 327c478bd9Sstevel@tonic-gate #include <errno.h> 337c478bd9Sstevel@tonic-gate #include <fnmatch.h> 347c478bd9Sstevel@tonic-gate #include <strings.h> 357c478bd9Sstevel@tonic-gate #include <unistd.h> 367c478bd9Sstevel@tonic-gate #include <assert.h> 377c478bd9Sstevel@tonic-gate #include <libgen.h> 387c478bd9Sstevel@tonic-gate #include <libintl.h> 397c478bd9Sstevel@tonic-gate #include <alloca.h> 407c478bd9Sstevel@tonic-gate #include <ctype.h> 419acbbeafSnn35248 #include <sys/acl.h> 429acbbeafSnn35248 #include <sys/stat.h> 439acbbeafSnn35248 #include <sys/brand.h> 447c478bd9Sstevel@tonic-gate #include <sys/mntio.h> 457c478bd9Sstevel@tonic-gate #include <sys/mnttab.h> 46cf8f45c7Sdstaff #include <sys/nvpair.h> 479acbbeafSnn35248 #include <sys/types.h> 48f4b3ec61Sdh155122 #include <sys/sockio.h> 49ee519a1fSgjelinek #include <ftw.h> 500209230bSgjelinek #include <pool.h> 510209230bSgjelinek #include <libscf.h> 520209230bSgjelinek #include <libproc.h> 530209230bSgjelinek #include <sys/priocntl.h> 5439935be5Sgjelinek #include <libuutil.h> 557c478bd9Sstevel@tonic-gate 567c478bd9Sstevel@tonic-gate #include <arpa/inet.h> 577c478bd9Sstevel@tonic-gate #include <netdb.h> 587c478bd9Sstevel@tonic-gate 597c478bd9Sstevel@tonic-gate #include <libxml/xmlmemory.h> 607c478bd9Sstevel@tonic-gate #include <libxml/parser.h> 617c478bd9Sstevel@tonic-gate 627c478bd9Sstevel@tonic-gate #include <libdevinfo.h> 63108322fbScarlsonj #include <uuid/uuid.h> 64ee519a1fSgjelinek #include <dirent.h> 659acbbeafSnn35248 #include <libbrand.h> 66ee519a1fSgjelinek 677c478bd9Sstevel@tonic-gate #include <libzonecfg.h> 687c478bd9Sstevel@tonic-gate #include "zonecfg_impl.h" 697c478bd9Sstevel@tonic-gate 707c478bd9Sstevel@tonic-gate #define _PATH_TMPFILE "/zonecfg.XXXXXX" 71cf8f45c7Sdstaff #define ZONE_CB_RETRY_COUNT 10 72cf8f45c7Sdstaff #define ZONE_EVENT_PING_SUBCLASS "ping" 73cf8f45c7Sdstaff #define ZONE_EVENT_PING_PUBLISHER "solaris" 747c478bd9Sstevel@tonic-gate 757c478bd9Sstevel@tonic-gate /* Hard-code the DTD element/attribute/entity names just once, here. */ 767c478bd9Sstevel@tonic-gate #define DTD_ELEM_ATTR (const xmlChar *) "attr" 777c478bd9Sstevel@tonic-gate #define DTD_ELEM_COMMENT (const xmlChar *) "comment" 787c478bd9Sstevel@tonic-gate #define DTD_ELEM_DEVICE (const xmlChar *) "device" 797c478bd9Sstevel@tonic-gate #define DTD_ELEM_FS (const xmlChar *) "filesystem" 807c478bd9Sstevel@tonic-gate #define DTD_ELEM_FSOPTION (const xmlChar *) "fsoption" 817c478bd9Sstevel@tonic-gate #define DTD_ELEM_IPD (const xmlChar *) "inherited-pkg-dir" 827c478bd9Sstevel@tonic-gate #define DTD_ELEM_NET (const xmlChar *) "network" 837c478bd9Sstevel@tonic-gate #define DTD_ELEM_RCTL (const xmlChar *) "rctl" 847c478bd9Sstevel@tonic-gate #define DTD_ELEM_RCTLVALUE (const xmlChar *) "rctl-value" 857c478bd9Sstevel@tonic-gate #define DTD_ELEM_ZONE (const xmlChar *) "zone" 86fa9e4066Sahrens #define DTD_ELEM_DATASET (const xmlChar *) "dataset" 870209230bSgjelinek #define DTD_ELEM_TMPPOOL (const xmlChar *) "tmp_pool" 880209230bSgjelinek #define DTD_ELEM_PSET (const xmlChar *) "pset" 890209230bSgjelinek #define DTD_ELEM_MCAP (const xmlChar *) "mcap" 90ee519a1fSgjelinek #define DTD_ELEM_PACKAGE (const xmlChar *) "package" 91ee519a1fSgjelinek #define DTD_ELEM_PATCH (const xmlChar *) "patch" 926cfd72c6Sgjelinek #define DTD_ELEM_OBSOLETES (const xmlChar *) "obsoletes" 93ee519a1fSgjelinek #define DTD_ELEM_DEV_PERM (const xmlChar *) "dev-perm" 947c478bd9Sstevel@tonic-gate 957c478bd9Sstevel@tonic-gate #define DTD_ATTR_ACTION (const xmlChar *) "action" 967c478bd9Sstevel@tonic-gate #define DTD_ATTR_ADDRESS (const xmlChar *) "address" 977c478bd9Sstevel@tonic-gate #define DTD_ATTR_AUTOBOOT (const xmlChar *) "autoboot" 98f4b3ec61Sdh155122 #define DTD_ATTR_IPTYPE (const xmlChar *) "ip-type" 99de860bd9Sgfaden #define DTD_ATTR_DEFROUTER (const xmlChar *) "defrouter" 1007c478bd9Sstevel@tonic-gate #define DTD_ATTR_DIR (const xmlChar *) "directory" 1017c478bd9Sstevel@tonic-gate #define DTD_ATTR_LIMIT (const xmlChar *) "limit" 102ffbafc53Scomay #define DTD_ATTR_LIMITPRIV (const xmlChar *) "limitpriv" 1033f2f09c1Sdp #define DTD_ATTR_BOOTARGS (const xmlChar *) "bootargs" 1040209230bSgjelinek #define DTD_ATTR_SCHED (const xmlChar *) "scheduling-class" 1057c478bd9Sstevel@tonic-gate #define DTD_ATTR_MATCH (const xmlChar *) "match" 1067c478bd9Sstevel@tonic-gate #define DTD_ATTR_NAME (const xmlChar *) "name" 1077c478bd9Sstevel@tonic-gate #define DTD_ATTR_PHYSICAL (const xmlChar *) "physical" 1087c478bd9Sstevel@tonic-gate #define DTD_ATTR_POOL (const xmlChar *) "pool" 1097c478bd9Sstevel@tonic-gate #define DTD_ATTR_PRIV (const xmlChar *) "priv" 1107c478bd9Sstevel@tonic-gate #define DTD_ATTR_RAW (const xmlChar *) "raw" 1117c478bd9Sstevel@tonic-gate #define DTD_ATTR_SPECIAL (const xmlChar *) "special" 1127c478bd9Sstevel@tonic-gate #define DTD_ATTR_TYPE (const xmlChar *) "type" 1137c478bd9Sstevel@tonic-gate #define DTD_ATTR_VALUE (const xmlChar *) "value" 1147c478bd9Sstevel@tonic-gate #define DTD_ATTR_ZONEPATH (const xmlChar *) "zonepath" 1150209230bSgjelinek #define DTD_ATTR_NCPU_MIN (const xmlChar *) "ncpu_min" 1160209230bSgjelinek #define DTD_ATTR_NCPU_MAX (const xmlChar *) "ncpu_max" 1170209230bSgjelinek #define DTD_ATTR_IMPORTANCE (const xmlChar *) "importance" 1180209230bSgjelinek #define DTD_ATTR_PHYSCAP (const xmlChar *) "physcap" 119ee519a1fSgjelinek #define DTD_ATTR_VERSION (const xmlChar *) "version" 120ee519a1fSgjelinek #define DTD_ATTR_ID (const xmlChar *) "id" 121ee519a1fSgjelinek #define DTD_ATTR_UID (const xmlChar *) "uid" 122ee519a1fSgjelinek #define DTD_ATTR_GID (const xmlChar *) "gid" 123ee519a1fSgjelinek #define DTD_ATTR_MODE (const xmlChar *) "mode" 124ee519a1fSgjelinek #define DTD_ATTR_ACL (const xmlChar *) "acl" 1259acbbeafSnn35248 #define DTD_ATTR_BRAND (const xmlChar *) "brand" 1267c478bd9Sstevel@tonic-gate 1277c478bd9Sstevel@tonic-gate #define DTD_ENTITY_BOOLEAN "boolean" 1287c478bd9Sstevel@tonic-gate #define DTD_ENTITY_DEVPATH "devpath" 1297c478bd9Sstevel@tonic-gate #define DTD_ENTITY_DRIVER "driver" 1307c478bd9Sstevel@tonic-gate #define DTD_ENTITY_DRVMIN "drv_min" 1317c478bd9Sstevel@tonic-gate #define DTD_ENTITY_FALSE "false" 1327c478bd9Sstevel@tonic-gate #define DTD_ENTITY_INT "int" 1337c478bd9Sstevel@tonic-gate #define DTD_ENTITY_STRING "string" 1347c478bd9Sstevel@tonic-gate #define DTD_ENTITY_TRUE "true" 1357c478bd9Sstevel@tonic-gate #define DTD_ENTITY_UINT "uint" 1367c478bd9Sstevel@tonic-gate 137a1be23daSdp #define DTD_ENTITY_BOOL_LEN 6 /* "false" */ 138a1be23daSdp 139ee519a1fSgjelinek #define DETACHED "SUNWdetached.xml" 140ee519a1fSgjelinek #define ATTACH_FORCED "SUNWattached.xml" 141ee519a1fSgjelinek #define PKG_PATH "/var/sadm/pkg" 142ee519a1fSgjelinek #define CONTENTS_FILE "/var/sadm/install/contents" 14345916cd2Sjpk #define SUNW_PKG_ALL_ZONES "SUNW_PKG_ALLZONES=true\n" 14445916cd2Sjpk #define SUNW_PKG_THIS_ZONE "SUNW_PKG_THISZONE=true\n" 145ee519a1fSgjelinek #define VERSION "VERSION=" 146ee519a1fSgjelinek #define PATCHLIST "PATCHLIST=" 147ee519a1fSgjelinek #define PATCHINFO "PATCH_INFO_" 148ee519a1fSgjelinek #define PKGINFO_RD_LEN 128 149ee519a1fSgjelinek 1500209230bSgjelinek #define TMP_POOL_NAME "SUNWtmp_%s" 1510209230bSgjelinek #define MAX_TMP_POOL_NAME (ZONENAME_MAX + 9) 1520209230bSgjelinek #define RCAP_SERVICE "system/rcap:default" 1530209230bSgjelinek #define POOLD_SERVICE "system/pools/dynamic:default" 1540209230bSgjelinek 15539935be5Sgjelinek enum zn_ipd_fs {ZONE_IPD, ZONE_FS}; 15639935be5Sgjelinek 1570209230bSgjelinek /* 1580209230bSgjelinek * rctl alias definitions 1590209230bSgjelinek * 1600209230bSgjelinek * This holds the alias, the full rctl name, the default priv value, action 1610209230bSgjelinek * and lower limit. The functions that handle rctl aliases step through 1620209230bSgjelinek * this table, matching on the alias, and using the full values for setting 1630209230bSgjelinek * the rctl entry as well the limit for validation. 1640209230bSgjelinek */ 1650209230bSgjelinek static struct alias { 1660209230bSgjelinek char *shortname; 1670209230bSgjelinek char *realname; 1680209230bSgjelinek char *priv; 1690209230bSgjelinek char *action; 1700209230bSgjelinek uint64_t low_limit; 1710209230bSgjelinek } aliases[] = { 1720209230bSgjelinek {ALIAS_MAXLWPS, "zone.max-lwps", "privileged", "deny", 100}, 1730209230bSgjelinek {ALIAS_MAXSHMMEM, "zone.max-shm-memory", "privileged", "deny", 0}, 1740209230bSgjelinek {ALIAS_MAXSHMIDS, "zone.max-shm-ids", "privileged", "deny", 0}, 1750209230bSgjelinek {ALIAS_MAXMSGIDS, "zone.max-msg-ids", "privileged", "deny", 0}, 1760209230bSgjelinek {ALIAS_MAXSEMIDS, "zone.max-sem-ids", "privileged", "deny", 0}, 1770209230bSgjelinek {ALIAS_MAXLOCKEDMEM, "zone.max-locked-memory", "privileged", "deny", 0}, 1780209230bSgjelinek {ALIAS_MAXSWAP, "zone.max-swap", "privileged", "deny", 0}, 1790209230bSgjelinek {ALIAS_SHARES, "zone.cpu-shares", "privileged", "none", 0}, 180c97ad5cdSakolb {ALIAS_CPUCAP, "zone.cpu-cap", "privileged", "deny", 0}, 1810209230bSgjelinek {NULL, NULL, NULL, NULL, 0} 1820209230bSgjelinek }; 1830209230bSgjelinek 1840209230bSgjelinek /* 1850209230bSgjelinek * Structure for applying rctls to a running zone. It allows important 1860209230bSgjelinek * process values to be passed together easily. 1870209230bSgjelinek */ 1880209230bSgjelinek typedef struct pr_info_handle { 1890209230bSgjelinek struct ps_prochandle *pr; 1900209230bSgjelinek pid_t pid; 1910209230bSgjelinek } pr_info_handle_t; 1920209230bSgjelinek 1937c478bd9Sstevel@tonic-gate struct zone_dochandle { 1947c478bd9Sstevel@tonic-gate char *zone_dh_rootdir; 1957c478bd9Sstevel@tonic-gate xmlDocPtr zone_dh_doc; 1967c478bd9Sstevel@tonic-gate xmlNodePtr zone_dh_cur; 1977c478bd9Sstevel@tonic-gate xmlNodePtr zone_dh_top; 198087719fdSdp boolean_t zone_dh_newzone; 199087719fdSdp boolean_t zone_dh_snapshot; 200ee519a1fSgjelinek boolean_t zone_dh_sw_inv; 201087719fdSdp char zone_dh_delete_name[ZONENAME_MAX]; 2027c478bd9Sstevel@tonic-gate }; 2037c478bd9Sstevel@tonic-gate 204cf8f45c7Sdstaff struct znotify { 205cf8f45c7Sdstaff void * zn_private; 206cf8f45c7Sdstaff evchan_t *zn_eventchan; 207cf8f45c7Sdstaff int (*zn_callback)(const char *zonename, zoneid_t zid, 208cf8f45c7Sdstaff const char *newstate, const char *oldstate, hrtime_t when, void *p); 209cf8f45c7Sdstaff pthread_mutex_t zn_mutex; 210cf8f45c7Sdstaff pthread_cond_t zn_cond; 211cf8f45c7Sdstaff pthread_mutex_t zn_bigmutex; 212cf8f45c7Sdstaff volatile enum {ZN_UNLOCKED, ZN_LOCKED, ZN_PING_INFLIGHT, 213cf8f45c7Sdstaff ZN_PING_RECEIVED} zn_state; 214cf8f45c7Sdstaff char zn_subscriber_id[MAX_SUBID_LEN]; 215cf8f45c7Sdstaff volatile boolean_t zn_failed; 216cf8f45c7Sdstaff int zn_failure_count; 217cf8f45c7Sdstaff }; 218cf8f45c7Sdstaff 219ee519a1fSgjelinek struct zone_pkginfo { 220ee519a1fSgjelinek boolean_t zpi_all_zones; 221ee519a1fSgjelinek boolean_t zpi_this_zone; 222ee519a1fSgjelinek int zpi_patch_cnt; 223ee519a1fSgjelinek char *zpi_version; 224ee519a1fSgjelinek char **zpi_patchinfo; 225ee519a1fSgjelinek }; 226ee519a1fSgjelinek 22739935be5Sgjelinek typedef struct { 22839935be5Sgjelinek uu_avl_node_t patch_node; 22939935be5Sgjelinek char *patch_num; 23039935be5Sgjelinek char *patch_vers; 2316cfd72c6Sgjelinek uu_list_t *obs_patches; 23239935be5Sgjelinek } patch_node_t; 23339935be5Sgjelinek 23439935be5Sgjelinek typedef struct { 2356cfd72c6Sgjelinek uu_list_node_t link; 2366cfd72c6Sgjelinek char *patch_num; 2376cfd72c6Sgjelinek } obs_patch_node_t; 2386cfd72c6Sgjelinek 2396cfd72c6Sgjelinek typedef struct { 24039935be5Sgjelinek uu_avl_t *obs_patches_avl; 24139935be5Sgjelinek zone_dochandle_t handle; 24239935be5Sgjelinek int res; 24339935be5Sgjelinek } patch_parms_t; 24439935be5Sgjelinek 245108322fbScarlsonj char *zonecfg_root = ""; 246108322fbScarlsonj 2477c478bd9Sstevel@tonic-gate /* 2487c478bd9Sstevel@tonic-gate * For functions which return int, which is most of the functions herein, 2497c478bd9Sstevel@tonic-gate * the return values should be from the Z_foo set defined in <libzonecfg.h>. 2507c478bd9Sstevel@tonic-gate * In some instances, we take pains mapping some libc errno values to Z_foo 2517c478bd9Sstevel@tonic-gate * values from this set. 2527c478bd9Sstevel@tonic-gate */ 2537c478bd9Sstevel@tonic-gate 2547c478bd9Sstevel@tonic-gate /* 255108322fbScarlsonj * Set the root (/) path for all zonecfg configuration files. This is a 256108322fbScarlsonj * private interface used by Live Upgrade extensions to access zone 257108322fbScarlsonj * configuration inside mounted alternate boot environments. 258108322fbScarlsonj */ 259108322fbScarlsonj void 260108322fbScarlsonj zonecfg_set_root(const char *rootpath) 261108322fbScarlsonj { 262108322fbScarlsonj if (*zonecfg_root != '\0') 263108322fbScarlsonj free(zonecfg_root); 264108322fbScarlsonj if (rootpath == NULL || rootpath[0] == '\0' || rootpath[1] == '\0' || 265108322fbScarlsonj (zonecfg_root = strdup(rootpath)) == NULL) 266108322fbScarlsonj zonecfg_root = ""; 267108322fbScarlsonj } 268108322fbScarlsonj 269108322fbScarlsonj const char * 270108322fbScarlsonj zonecfg_get_root(void) 271108322fbScarlsonj { 272108322fbScarlsonj return (zonecfg_root); 273108322fbScarlsonj } 274108322fbScarlsonj 275108322fbScarlsonj boolean_t 276108322fbScarlsonj zonecfg_in_alt_root(void) 277108322fbScarlsonj { 278108322fbScarlsonj return (*zonecfg_root != '\0'); 279108322fbScarlsonj } 280108322fbScarlsonj 281108322fbScarlsonj /* 2827c478bd9Sstevel@tonic-gate * Callers of the _file_path() functions are expected to have the second 2837c478bd9Sstevel@tonic-gate * parameter be a (char foo[MAXPATHLEN]). 2847c478bd9Sstevel@tonic-gate */ 2857c478bd9Sstevel@tonic-gate 286108322fbScarlsonj static boolean_t 2877c478bd9Sstevel@tonic-gate config_file_path(const char *zonename, char *answer) 2887c478bd9Sstevel@tonic-gate { 289108322fbScarlsonj return (snprintf(answer, MAXPATHLEN, "%s%s/%s.xml", zonecfg_root, 290108322fbScarlsonj ZONE_CONFIG_ROOT, zonename) < MAXPATHLEN); 2917c478bd9Sstevel@tonic-gate } 2927c478bd9Sstevel@tonic-gate 293108322fbScarlsonj static boolean_t 294108322fbScarlsonj snap_file_path(const char *zonename, char *answer) 2957c478bd9Sstevel@tonic-gate { 296108322fbScarlsonj return (snprintf(answer, MAXPATHLEN, "%s%s/%s.snapshot.xml", 297108322fbScarlsonj zonecfg_root, ZONE_SNAPSHOT_ROOT, zonename) < MAXPATHLEN); 2987c478bd9Sstevel@tonic-gate } 2997c478bd9Sstevel@tonic-gate 3007c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 3017c478bd9Sstevel@tonic-gate static void 3027c478bd9Sstevel@tonic-gate zonecfg_error_func(void *ctx, const char *msg, ...) 3037c478bd9Sstevel@tonic-gate { 3047c478bd9Sstevel@tonic-gate /* 3057c478bd9Sstevel@tonic-gate * This function does nothing by design. Its purpose is to prevent 3067c478bd9Sstevel@tonic-gate * libxml from dumping unwanted messages to stdout/stderr. 3077c478bd9Sstevel@tonic-gate */ 3087c478bd9Sstevel@tonic-gate } 3097c478bd9Sstevel@tonic-gate 3107c478bd9Sstevel@tonic-gate zone_dochandle_t 3117c478bd9Sstevel@tonic-gate zonecfg_init_handle(void) 3127c478bd9Sstevel@tonic-gate { 313087719fdSdp zone_dochandle_t handle = calloc(1, sizeof (struct zone_dochandle)); 3147c478bd9Sstevel@tonic-gate if (handle == NULL) { 3157c478bd9Sstevel@tonic-gate errno = Z_NOMEM; 3167c478bd9Sstevel@tonic-gate return (NULL); 3177c478bd9Sstevel@tonic-gate } 3187c478bd9Sstevel@tonic-gate 3197c478bd9Sstevel@tonic-gate /* generic libxml initialization */ 3207c478bd9Sstevel@tonic-gate xmlLineNumbersDefault(1); 3217c478bd9Sstevel@tonic-gate xmlLoadExtDtdDefaultValue |= XML_DETECT_IDS; 3227c478bd9Sstevel@tonic-gate xmlDoValidityCheckingDefaultValue = 1; 3237c478bd9Sstevel@tonic-gate (void) xmlKeepBlanksDefault(0); 3247c478bd9Sstevel@tonic-gate xmlGetWarningsDefaultValue = 0; 3257c478bd9Sstevel@tonic-gate xmlSetGenericErrorFunc(NULL, zonecfg_error_func); 3267c478bd9Sstevel@tonic-gate 3277c478bd9Sstevel@tonic-gate return (handle); 3287c478bd9Sstevel@tonic-gate } 3297c478bd9Sstevel@tonic-gate 3307c478bd9Sstevel@tonic-gate int 3317c478bd9Sstevel@tonic-gate zonecfg_check_handle(zone_dochandle_t handle) 3327c478bd9Sstevel@tonic-gate { 3337c478bd9Sstevel@tonic-gate if (handle == NULL || handle->zone_dh_doc == NULL) 3347c478bd9Sstevel@tonic-gate return (Z_BAD_HANDLE); 3357c478bd9Sstevel@tonic-gate return (Z_OK); 3367c478bd9Sstevel@tonic-gate } 3377c478bd9Sstevel@tonic-gate 3387c478bd9Sstevel@tonic-gate void 3397c478bd9Sstevel@tonic-gate zonecfg_fini_handle(zone_dochandle_t handle) 3407c478bd9Sstevel@tonic-gate { 3417c478bd9Sstevel@tonic-gate if (zonecfg_check_handle(handle) == Z_OK) 3427c478bd9Sstevel@tonic-gate xmlFreeDoc(handle->zone_dh_doc); 3437c478bd9Sstevel@tonic-gate if (handle != NULL) 3447c478bd9Sstevel@tonic-gate free(handle); 3457c478bd9Sstevel@tonic-gate } 3467c478bd9Sstevel@tonic-gate 3477c478bd9Sstevel@tonic-gate static int 3487c478bd9Sstevel@tonic-gate zonecfg_destroy_impl(char *filename) 3497c478bd9Sstevel@tonic-gate { 3507c478bd9Sstevel@tonic-gate if (unlink(filename) == -1) { 3517c478bd9Sstevel@tonic-gate if (errno == EACCES) 3527c478bd9Sstevel@tonic-gate return (Z_ACCES); 3537c478bd9Sstevel@tonic-gate if (errno == ENOENT) 3547c478bd9Sstevel@tonic-gate return (Z_NO_ZONE); 3557c478bd9Sstevel@tonic-gate return (Z_MISC_FS); 3567c478bd9Sstevel@tonic-gate } 3577c478bd9Sstevel@tonic-gate return (Z_OK); 3587c478bd9Sstevel@tonic-gate } 3597c478bd9Sstevel@tonic-gate 3607c478bd9Sstevel@tonic-gate int 361087719fdSdp zonecfg_destroy(const char *zonename, boolean_t force) 3627c478bd9Sstevel@tonic-gate { 3637c478bd9Sstevel@tonic-gate char path[MAXPATHLEN]; 364087719fdSdp struct zoneent ze; 365087719fdSdp int err, state_err; 366087719fdSdp zone_state_t state; 3677c478bd9Sstevel@tonic-gate 368108322fbScarlsonj if (!config_file_path(zonename, path)) 369108322fbScarlsonj return (Z_MISC_FS); 370087719fdSdp 371087719fdSdp state_err = zone_get_state((char *)zonename, &state); 372087719fdSdp err = access(path, W_OK); 373087719fdSdp 374087719fdSdp /* 375087719fdSdp * If there is no file, and no index entry, reliably indicate that no 376087719fdSdp * such zone exists. 377087719fdSdp */ 378087719fdSdp if ((state_err == Z_NO_ZONE) && (err == -1) && (errno == ENOENT)) 379087719fdSdp return (Z_NO_ZONE); 380087719fdSdp 381087719fdSdp /* 382087719fdSdp * Handle any other filesystem related errors (except if the XML 383087719fdSdp * file is missing, which we treat silently), unless we're forcing, 384087719fdSdp * in which case we plow on. 385087719fdSdp */ 386087719fdSdp if (err == -1 && errno != ENOENT) { 387087719fdSdp if (errno == EACCES) 388087719fdSdp return (Z_ACCES); 389087719fdSdp else if (!force) 390087719fdSdp return (Z_MISC_FS); 391087719fdSdp } 392087719fdSdp 393087719fdSdp if (state > ZONE_STATE_INSTALLED) 394087719fdSdp return (Z_BAD_ZONE_STATE); 395087719fdSdp 396087719fdSdp if (!force && state > ZONE_STATE_CONFIGURED) 397087719fdSdp return (Z_BAD_ZONE_STATE); 398087719fdSdp 399087719fdSdp /* 400087719fdSdp * Index deletion succeeds even if the entry doesn't exist. So this 401087719fdSdp * will fail only if we've had some more severe problem. 402087719fdSdp */ 403087719fdSdp bzero(&ze, sizeof (ze)); 404087719fdSdp (void) strlcpy(ze.zone_name, zonename, sizeof (ze.zone_name)); 405087719fdSdp if ((err = putzoneent(&ze, PZE_REMOVE)) != Z_OK) 406087719fdSdp if (!force) 407087719fdSdp return (err); 408087719fdSdp 409087719fdSdp err = zonecfg_destroy_impl(path); 410087719fdSdp 411087719fdSdp /* 412087719fdSdp * Treat failure to find the XML file silently, since, well, it's 413087719fdSdp * gone, and with the index file cleaned up, we're done. 414087719fdSdp */ 415087719fdSdp if (err == Z_OK || err == Z_NO_ZONE) 416087719fdSdp return (Z_OK); 417087719fdSdp return (err); 4187c478bd9Sstevel@tonic-gate } 4197c478bd9Sstevel@tonic-gate 4207c478bd9Sstevel@tonic-gate int 421108322fbScarlsonj zonecfg_destroy_snapshot(const char *zonename) 4227c478bd9Sstevel@tonic-gate { 4237c478bd9Sstevel@tonic-gate char path[MAXPATHLEN]; 4247c478bd9Sstevel@tonic-gate 425108322fbScarlsonj if (!snap_file_path(zonename, path)) 426108322fbScarlsonj return (Z_MISC_FS); 4277c478bd9Sstevel@tonic-gate return (zonecfg_destroy_impl(path)); 4287c478bd9Sstevel@tonic-gate } 4297c478bd9Sstevel@tonic-gate 4307c478bd9Sstevel@tonic-gate static int 431a1be23daSdp getroot(zone_dochandle_t handle, xmlNodePtr *root) 4327c478bd9Sstevel@tonic-gate { 4337c478bd9Sstevel@tonic-gate if (zonecfg_check_handle(handle) == Z_BAD_HANDLE) 4347c478bd9Sstevel@tonic-gate return (Z_BAD_HANDLE); 4357c478bd9Sstevel@tonic-gate 436a1be23daSdp *root = xmlDocGetRootElement(handle->zone_dh_doc); 437a1be23daSdp 438a1be23daSdp if (*root == NULL) 4397c478bd9Sstevel@tonic-gate return (Z_EMPTY_DOCUMENT); 440a1be23daSdp 441a1be23daSdp if (xmlStrcmp((*root)->name, DTD_ELEM_ZONE)) 442a1be23daSdp return (Z_WRONG_DOC_TYPE); 443a1be23daSdp 444a1be23daSdp return (Z_OK); 4457c478bd9Sstevel@tonic-gate } 4467c478bd9Sstevel@tonic-gate 447a1be23daSdp static int 448a1be23daSdp operation_prep(zone_dochandle_t handle) 449a1be23daSdp { 450a1be23daSdp xmlNodePtr root; 451a1be23daSdp int err; 452a1be23daSdp 453a1be23daSdp if ((err = getroot(handle, &root)) != 0) 454a1be23daSdp return (err); 455a1be23daSdp 456a1be23daSdp handle->zone_dh_cur = root; 457a1be23daSdp handle->zone_dh_top = root; 458a1be23daSdp return (Z_OK); 4597c478bd9Sstevel@tonic-gate } 460a1be23daSdp 461a1be23daSdp static int 462ffbafc53Scomay fetchprop(xmlNodePtr cur, const xmlChar *propname, char *dst, size_t dstsize) 463ffbafc53Scomay { 464ffbafc53Scomay xmlChar *property; 465ffbafc53Scomay size_t srcsize; 466ffbafc53Scomay 467ffbafc53Scomay if ((property = xmlGetProp(cur, propname)) == NULL) 468ffbafc53Scomay return (Z_BAD_PROPERTY); 469ffbafc53Scomay srcsize = strlcpy(dst, (char *)property, dstsize); 470ffbafc53Scomay xmlFree(property); 471ffbafc53Scomay if (srcsize >= dstsize) 472ffbafc53Scomay return (Z_TOO_BIG); 473ffbafc53Scomay return (Z_OK); 474ffbafc53Scomay } 475ffbafc53Scomay 476ffbafc53Scomay static int 477ffbafc53Scomay fetch_alloc_prop(xmlNodePtr cur, const xmlChar *propname, char **dst) 478ffbafc53Scomay { 479ffbafc53Scomay xmlChar *property; 480ffbafc53Scomay 481ffbafc53Scomay if ((property = xmlGetProp(cur, propname)) == NULL) 482ffbafc53Scomay return (Z_BAD_PROPERTY); 483ffbafc53Scomay if ((*dst = strdup((char *)property)) == NULL) { 484ffbafc53Scomay xmlFree(property); 485ffbafc53Scomay return (Z_NOMEM); 486ffbafc53Scomay } 487ffbafc53Scomay xmlFree(property); 488ffbafc53Scomay return (Z_OK); 489ffbafc53Scomay } 490ffbafc53Scomay 491ffbafc53Scomay static int 492a1be23daSdp getrootattr(zone_dochandle_t handle, const xmlChar *propname, 493a1be23daSdp char *propval, size_t propsize) 494a1be23daSdp { 495a1be23daSdp xmlNodePtr root; 496a1be23daSdp int err; 497a1be23daSdp 498a1be23daSdp if ((err = getroot(handle, &root)) != 0) 499a1be23daSdp return (err); 500a1be23daSdp 501ffbafc53Scomay return (fetchprop(root, propname, propval, propsize)); 502ffbafc53Scomay } 503ffbafc53Scomay 504ffbafc53Scomay static int 505ffbafc53Scomay get_alloc_rootattr(zone_dochandle_t handle, const xmlChar *propname, 506ffbafc53Scomay char **propval) 507ffbafc53Scomay { 508ffbafc53Scomay xmlNodePtr root; 509ffbafc53Scomay int err; 510ffbafc53Scomay 511ffbafc53Scomay if ((err = getroot(handle, &root)) != 0) 512ffbafc53Scomay return (err); 513ffbafc53Scomay 514ffbafc53Scomay return (fetch_alloc_prop(root, propname, propval)); 515a1be23daSdp } 516a1be23daSdp 517a1be23daSdp static int 518108322fbScarlsonj setrootattr(zone_dochandle_t handle, const xmlChar *propname, 519108322fbScarlsonj const char *propval) 520a1be23daSdp { 521a1be23daSdp int err; 522a1be23daSdp xmlNodePtr root; 523a1be23daSdp 524a1be23daSdp if ((err = getroot(handle, &root)) != Z_OK) 525a1be23daSdp return (err); 526a1be23daSdp 5270209230bSgjelinek /* 5280209230bSgjelinek * If we get a null propval remove the property (ignore return since it 5290209230bSgjelinek * may not be set to begin with). 5300209230bSgjelinek */ 5310209230bSgjelinek if (propval == NULL) { 5320209230bSgjelinek (void) xmlUnsetProp(root, propname); 5330209230bSgjelinek } else { 5340209230bSgjelinek if (xmlSetProp(root, propname, (const xmlChar *) propval) 5350209230bSgjelinek == NULL) 536a1be23daSdp return (Z_INVAL); 5370209230bSgjelinek } 5387c478bd9Sstevel@tonic-gate return (Z_OK); 5397c478bd9Sstevel@tonic-gate } 5407c478bd9Sstevel@tonic-gate 541087719fdSdp static void 542087719fdSdp addcomment(zone_dochandle_t handle, const char *comment) 543087719fdSdp { 544087719fdSdp xmlNodePtr node; 545087719fdSdp node = xmlNewComment((xmlChar *) comment); 546087719fdSdp 547087719fdSdp if (node != NULL) 548087719fdSdp (void) xmlAddPrevSibling(handle->zone_dh_top, node); 549087719fdSdp } 550087719fdSdp 551087719fdSdp static void 552087719fdSdp stripcomments(zone_dochandle_t handle) 553087719fdSdp { 554087719fdSdp xmlDocPtr top; 555087719fdSdp xmlNodePtr child, next; 556087719fdSdp 557087719fdSdp top = handle->zone_dh_doc; 558087719fdSdp for (child = top->xmlChildrenNode; child != NULL; child = next) { 559087719fdSdp next = child->next; 560087719fdSdp if (child->name == NULL) 561087719fdSdp continue; 562087719fdSdp if (xmlStrcmp(child->name, DTD_ELEM_COMMENT) == 0) { 563087719fdSdp next = child->next; 564087719fdSdp xmlUnlinkNode(child); 565087719fdSdp xmlFreeNode(child); 566087719fdSdp } 567087719fdSdp } 568087719fdSdp } 569087719fdSdp 570ee519a1fSgjelinek static void 571ee519a1fSgjelinek strip_sw_inv(zone_dochandle_t handle) 572ee519a1fSgjelinek { 573ee519a1fSgjelinek xmlNodePtr root, child, next; 574ee519a1fSgjelinek 575ee519a1fSgjelinek root = xmlDocGetRootElement(handle->zone_dh_doc); 576ee519a1fSgjelinek for (child = root->xmlChildrenNode; child != NULL; child = next) { 577ee519a1fSgjelinek next = child->next; 578ee519a1fSgjelinek if (child->name == NULL) 579ee519a1fSgjelinek continue; 580ee519a1fSgjelinek if (xmlStrcmp(child->name, DTD_ELEM_PACKAGE) == 0 || 581ee519a1fSgjelinek xmlStrcmp(child->name, DTD_ELEM_PATCH) == 0) { 582ee519a1fSgjelinek next = child->next; 583ee519a1fSgjelinek xmlUnlinkNode(child); 584ee519a1fSgjelinek xmlFreeNode(child); 585ee519a1fSgjelinek } 586ee519a1fSgjelinek } 587ee519a1fSgjelinek } 588ee519a1fSgjelinek 5897c478bd9Sstevel@tonic-gate static int 590108322fbScarlsonj zonecfg_get_handle_impl(const char *zonename, const char *filename, 591108322fbScarlsonj zone_dochandle_t handle) 5927c478bd9Sstevel@tonic-gate { 5937c478bd9Sstevel@tonic-gate xmlValidCtxtPtr cvp; 5947c478bd9Sstevel@tonic-gate struct stat statbuf; 5957c478bd9Sstevel@tonic-gate int valid; 5967c478bd9Sstevel@tonic-gate 5977c478bd9Sstevel@tonic-gate if (zonename == NULL) 5987c478bd9Sstevel@tonic-gate return (Z_NO_ZONE); 5999acbbeafSnn35248 6007c478bd9Sstevel@tonic-gate if ((handle->zone_dh_doc = xmlParseFile(filename)) == NULL) { 6017c478bd9Sstevel@tonic-gate /* distinguish file not found vs. found but not parsed */ 6027c478bd9Sstevel@tonic-gate if (stat(filename, &statbuf) == 0) 6037c478bd9Sstevel@tonic-gate return (Z_INVALID_DOCUMENT); 6047c478bd9Sstevel@tonic-gate return (Z_NO_ZONE); 6057c478bd9Sstevel@tonic-gate } 6067c478bd9Sstevel@tonic-gate if ((cvp = xmlNewValidCtxt()) == NULL) 6077c478bd9Sstevel@tonic-gate return (Z_NOMEM); 6087c478bd9Sstevel@tonic-gate cvp->error = zonecfg_error_func; 6097c478bd9Sstevel@tonic-gate cvp->warning = zonecfg_error_func; 6107c478bd9Sstevel@tonic-gate valid = xmlValidateDocument(cvp, handle->zone_dh_doc); 6117c478bd9Sstevel@tonic-gate xmlFreeValidCtxt(cvp); 6127c478bd9Sstevel@tonic-gate if (valid == 0) 6137c478bd9Sstevel@tonic-gate return (Z_INVALID_DOCUMENT); 614087719fdSdp 6157c478bd9Sstevel@tonic-gate /* delete any comments such as inherited Sun copyright / ident str */ 616087719fdSdp stripcomments(handle); 6177c478bd9Sstevel@tonic-gate return (Z_OK); 6187c478bd9Sstevel@tonic-gate } 6197c478bd9Sstevel@tonic-gate 6207c478bd9Sstevel@tonic-gate int 621108322fbScarlsonj zonecfg_get_handle(const char *zonename, zone_dochandle_t handle) 6227c478bd9Sstevel@tonic-gate { 6237c478bd9Sstevel@tonic-gate char path[MAXPATHLEN]; 6247c478bd9Sstevel@tonic-gate 625108322fbScarlsonj if (!config_file_path(zonename, path)) 626108322fbScarlsonj return (Z_MISC_FS); 627087719fdSdp handle->zone_dh_newzone = B_FALSE; 628087719fdSdp 6297c478bd9Sstevel@tonic-gate return (zonecfg_get_handle_impl(zonename, path, handle)); 6307c478bd9Sstevel@tonic-gate } 6317c478bd9Sstevel@tonic-gate 6327c478bd9Sstevel@tonic-gate int 633ee519a1fSgjelinek zonecfg_get_attach_handle(const char *path, const char *zonename, 634ee519a1fSgjelinek boolean_t preserve_sw, zone_dochandle_t handle) 635ee519a1fSgjelinek { 636ee519a1fSgjelinek char migpath[MAXPATHLEN]; 637ee519a1fSgjelinek int err; 638ee519a1fSgjelinek struct stat buf; 639ee519a1fSgjelinek 640ee519a1fSgjelinek if (snprintf(migpath, sizeof (migpath), "%s/root", path) >= 641ee519a1fSgjelinek sizeof (migpath)) 642ee519a1fSgjelinek return (Z_NOMEM); 643ee519a1fSgjelinek 644ee519a1fSgjelinek if (stat(migpath, &buf) == -1 || !S_ISDIR(buf.st_mode)) 645ee519a1fSgjelinek return (Z_NO_ZONE); 646ee519a1fSgjelinek 647ee519a1fSgjelinek if (snprintf(migpath, sizeof (migpath), "%s/%s", path, DETACHED) >= 648ee519a1fSgjelinek sizeof (migpath)) 649ee519a1fSgjelinek return (Z_NOMEM); 650ee519a1fSgjelinek 651ee519a1fSgjelinek if ((err = zonecfg_get_handle_impl(zonename, migpath, handle)) != Z_OK) 652ee519a1fSgjelinek return (err); 653ee519a1fSgjelinek 654ee519a1fSgjelinek if (!preserve_sw) 655ee519a1fSgjelinek strip_sw_inv(handle); 656ee519a1fSgjelinek 657ee519a1fSgjelinek handle->zone_dh_newzone = B_TRUE; 658ee519a1fSgjelinek if ((err = setrootattr(handle, DTD_ATTR_ZONEPATH, path)) != Z_OK) 659ee519a1fSgjelinek return (err); 660ee519a1fSgjelinek 661ee519a1fSgjelinek return (setrootattr(handle, DTD_ATTR_NAME, zonename)); 662ee519a1fSgjelinek } 663ee519a1fSgjelinek 664ee519a1fSgjelinek int 665108322fbScarlsonj zonecfg_get_snapshot_handle(const char *zonename, zone_dochandle_t handle) 6667c478bd9Sstevel@tonic-gate { 6677c478bd9Sstevel@tonic-gate char path[MAXPATHLEN]; 6687c478bd9Sstevel@tonic-gate 669108322fbScarlsonj if (!snap_file_path(zonename, path)) 670108322fbScarlsonj return (Z_MISC_FS); 671087719fdSdp handle->zone_dh_newzone = B_FALSE; 6727c478bd9Sstevel@tonic-gate return (zonecfg_get_handle_impl(zonename, path, handle)); 6737c478bd9Sstevel@tonic-gate } 6747c478bd9Sstevel@tonic-gate 6757c478bd9Sstevel@tonic-gate int 676108322fbScarlsonj zonecfg_get_template_handle(const char *template, const char *zonename, 677087719fdSdp zone_dochandle_t handle) 678087719fdSdp { 679087719fdSdp char path[MAXPATHLEN]; 680087719fdSdp int err; 681087719fdSdp 682108322fbScarlsonj if (!config_file_path(template, path)) 683108322fbScarlsonj return (Z_MISC_FS); 684087719fdSdp 685087719fdSdp if ((err = zonecfg_get_handle_impl(template, path, handle)) != Z_OK) 686087719fdSdp return (err); 687087719fdSdp handle->zone_dh_newzone = B_TRUE; 688087719fdSdp return (setrootattr(handle, DTD_ATTR_NAME, zonename)); 689087719fdSdp } 690087719fdSdp 6919acbbeafSnn35248 int 6929acbbeafSnn35248 zonecfg_get_xml_handle(const char *path, zone_dochandle_t handle) 6939acbbeafSnn35248 { 6949acbbeafSnn35248 struct stat buf; 6959acbbeafSnn35248 int err; 6969acbbeafSnn35248 6979acbbeafSnn35248 if (stat(path, &buf) == -1) 6989acbbeafSnn35248 return (Z_MISC_FS); 6999acbbeafSnn35248 7009acbbeafSnn35248 if ((err = zonecfg_get_handle_impl("xml", path, handle)) != Z_OK) 7019acbbeafSnn35248 return (err); 7029acbbeafSnn35248 handle->zone_dh_newzone = B_TRUE; 7039acbbeafSnn35248 return (Z_OK); 7049acbbeafSnn35248 } 7059acbbeafSnn35248 7068cd327d5Sgjelinek /* 7078cd327d5Sgjelinek * Initialize two handles from the manifest read on fd. The rem_handle 7088cd327d5Sgjelinek * is initialized from the input file, including the sw inventory. The 7098cd327d5Sgjelinek * local_handle is initialized with the same zone configuration but with 7108cd327d5Sgjelinek * no sw inventory. 7118cd327d5Sgjelinek */ 7128cd327d5Sgjelinek int 7138cd327d5Sgjelinek zonecfg_attach_manifest(int fd, zone_dochandle_t local_handle, 7148cd327d5Sgjelinek zone_dochandle_t rem_handle) 7158cd327d5Sgjelinek { 7168cd327d5Sgjelinek xmlValidCtxtPtr cvp; 7178cd327d5Sgjelinek int valid; 7188cd327d5Sgjelinek 7198cd327d5Sgjelinek /* load the manifest into the handle for the remote system */ 7208cd327d5Sgjelinek if ((rem_handle->zone_dh_doc = xmlReadFd(fd, NULL, NULL, 0)) == NULL) { 7218cd327d5Sgjelinek return (Z_INVALID_DOCUMENT); 7228cd327d5Sgjelinek } 7238cd327d5Sgjelinek if ((cvp = xmlNewValidCtxt()) == NULL) 7248cd327d5Sgjelinek return (Z_NOMEM); 7258cd327d5Sgjelinek cvp->error = zonecfg_error_func; 7268cd327d5Sgjelinek cvp->warning = zonecfg_error_func; 7278cd327d5Sgjelinek valid = xmlValidateDocument(cvp, rem_handle->zone_dh_doc); 7288cd327d5Sgjelinek xmlFreeValidCtxt(cvp); 7298cd327d5Sgjelinek if (valid == 0) 7308cd327d5Sgjelinek return (Z_INVALID_DOCUMENT); 7318cd327d5Sgjelinek 7328cd327d5Sgjelinek /* delete any comments such as inherited Sun copyright / ident str */ 7338cd327d5Sgjelinek stripcomments(rem_handle); 7348cd327d5Sgjelinek 7358cd327d5Sgjelinek rem_handle->zone_dh_newzone = B_TRUE; 7368cd327d5Sgjelinek rem_handle->zone_dh_sw_inv = B_TRUE; 7378cd327d5Sgjelinek 7388cd327d5Sgjelinek /* 7398cd327d5Sgjelinek * Now use the remote system handle to generate a local system handle 7408cd327d5Sgjelinek * with an identical zones configuration but no sw inventory. 7418cd327d5Sgjelinek */ 7428cd327d5Sgjelinek if ((local_handle->zone_dh_doc = xmlCopyDoc(rem_handle->zone_dh_doc, 7438cd327d5Sgjelinek 1)) == NULL) { 7448cd327d5Sgjelinek return (Z_INVALID_DOCUMENT); 7458cd327d5Sgjelinek } 7468cd327d5Sgjelinek 7478cd327d5Sgjelinek /* 7488cd327d5Sgjelinek * We need to re-run xmlValidateDocument on local_handle to properly 7498cd327d5Sgjelinek * update the in-core representation of the configuration. 7508cd327d5Sgjelinek */ 7518cd327d5Sgjelinek if ((cvp = xmlNewValidCtxt()) == NULL) 7528cd327d5Sgjelinek return (Z_NOMEM); 7538cd327d5Sgjelinek cvp->error = zonecfg_error_func; 7548cd327d5Sgjelinek cvp->warning = zonecfg_error_func; 7558cd327d5Sgjelinek valid = xmlValidateDocument(cvp, local_handle->zone_dh_doc); 7568cd327d5Sgjelinek xmlFreeValidCtxt(cvp); 7578cd327d5Sgjelinek if (valid == 0) 7588cd327d5Sgjelinek return (Z_INVALID_DOCUMENT); 7598cd327d5Sgjelinek 7608cd327d5Sgjelinek strip_sw_inv(local_handle); 7618cd327d5Sgjelinek 7628cd327d5Sgjelinek local_handle->zone_dh_newzone = B_TRUE; 7638cd327d5Sgjelinek local_handle->zone_dh_sw_inv = B_FALSE; 7648cd327d5Sgjelinek 7658cd327d5Sgjelinek return (Z_OK); 7668cd327d5Sgjelinek } 7678cd327d5Sgjelinek 768087719fdSdp static boolean_t 769087719fdSdp is_renaming(zone_dochandle_t handle) 770087719fdSdp { 771087719fdSdp if (handle->zone_dh_newzone) 772087719fdSdp return (B_FALSE); 773087719fdSdp if (strlen(handle->zone_dh_delete_name) > 0) 774087719fdSdp return (B_TRUE); 775087719fdSdp return (B_FALSE); 776087719fdSdp } 777087719fdSdp 778087719fdSdp static boolean_t 779087719fdSdp is_new(zone_dochandle_t handle) 780087719fdSdp { 781087719fdSdp return (handle->zone_dh_newzone || handle->zone_dh_snapshot); 782087719fdSdp } 783087719fdSdp 784087719fdSdp static boolean_t 785087719fdSdp is_snapshot(zone_dochandle_t handle) 786087719fdSdp { 787087719fdSdp return (handle->zone_dh_snapshot); 788087719fdSdp } 789087719fdSdp 790087719fdSdp /* 791087719fdSdp * It would be great to be able to use libc's ctype(3c) macros, but we 792087719fdSdp * can't, as they are locale sensitive, and it would break our limited thread 793087719fdSdp * safety if this routine had to change the app locale on the fly. 794087719fdSdp */ 795087719fdSdp int 796108322fbScarlsonj zonecfg_validate_zonename(const char *zone) 797087719fdSdp { 798087719fdSdp int i; 799087719fdSdp 800087719fdSdp if (strcmp(zone, GLOBAL_ZONENAME) == 0) 801087719fdSdp return (Z_BOGUS_ZONE_NAME); 802087719fdSdp 803087719fdSdp if (strlen(zone) >= ZONENAME_MAX) 804087719fdSdp return (Z_BOGUS_ZONE_NAME); 805087719fdSdp 806087719fdSdp if (!((zone[0] >= 'a' && zone[0] <= 'z') || 807087719fdSdp (zone[0] >= 'A' && zone[0] <= 'Z') || 808087719fdSdp (zone[0] >= '0' && zone[0] <= '9'))) 809087719fdSdp return (Z_BOGUS_ZONE_NAME); 810087719fdSdp 811087719fdSdp for (i = 1; zone[i] != '\0'; i++) { 812087719fdSdp if (!((zone[i] >= 'a' && zone[i] <= 'z') || 813087719fdSdp (zone[i] >= 'A' && zone[i] <= 'Z') || 814087719fdSdp (zone[i] >= '0' && zone[i] <= '9') || 815087719fdSdp (zone[i] == '-') || (zone[i] == '_') || (zone[i] == '.'))) 816087719fdSdp return (Z_BOGUS_ZONE_NAME); 817087719fdSdp } 818087719fdSdp 819087719fdSdp return (Z_OK); 820087719fdSdp } 821087719fdSdp 822087719fdSdp /* 823087719fdSdp * Changing the zone name requires us to track both the old and new 824087719fdSdp * name of the zone until commit time. 825087719fdSdp */ 826087719fdSdp int 8277c478bd9Sstevel@tonic-gate zonecfg_get_name(zone_dochandle_t handle, char *name, size_t namesize) 8287c478bd9Sstevel@tonic-gate { 829a1be23daSdp return (getrootattr(handle, DTD_ATTR_NAME, name, namesize)); 8307c478bd9Sstevel@tonic-gate } 8317c478bd9Sstevel@tonic-gate 832a1be23daSdp int 833a1be23daSdp zonecfg_set_name(zone_dochandle_t handle, char *name) 834a1be23daSdp { 835087719fdSdp zone_state_t state; 836087719fdSdp char curname[ZONENAME_MAX], old_delname[ZONENAME_MAX]; 837087719fdSdp int err; 838087719fdSdp 839087719fdSdp if ((err = getrootattr(handle, DTD_ATTR_NAME, curname, 840087719fdSdp sizeof (curname))) != Z_OK) 841087719fdSdp return (err); 842087719fdSdp 843087719fdSdp if (strcmp(name, curname) == 0) 844087719fdSdp return (Z_OK); 845087719fdSdp 846087719fdSdp /* 847087719fdSdp * Switching zone names to one beginning with SUNW is not permitted. 848087719fdSdp */ 849087719fdSdp if (strncmp(name, "SUNW", 4) == 0) 850087719fdSdp return (Z_BOGUS_ZONE_NAME); 851087719fdSdp 852087719fdSdp if ((err = zonecfg_validate_zonename(name)) != Z_OK) 853087719fdSdp return (err); 854087719fdSdp 855087719fdSdp /* 856087719fdSdp * Setting the name back to the original name (effectively a revert of 857087719fdSdp * the name) is fine. But if we carry on, we'll falsely identify the 858087719fdSdp * name as "in use," so special case here. 859087719fdSdp */ 860087719fdSdp if (strcmp(name, handle->zone_dh_delete_name) == 0) { 861087719fdSdp err = setrootattr(handle, DTD_ATTR_NAME, name); 862087719fdSdp handle->zone_dh_delete_name[0] = '\0'; 863087719fdSdp return (err); 864087719fdSdp } 865087719fdSdp 866087719fdSdp /* Check to see if new name chosen is already in use */ 867087719fdSdp if (zone_get_state(name, &state) != Z_NO_ZONE) 868087719fdSdp return (Z_NAME_IN_USE); 869087719fdSdp 870087719fdSdp /* 871087719fdSdp * If this isn't already "new" or in a renaming transition, then 872087719fdSdp * we're initiating a rename here; so stash the "delete name" 873087719fdSdp * (i.e. the name of the zone we'll be removing) for the rename. 874087719fdSdp */ 875087719fdSdp (void) strlcpy(old_delname, handle->zone_dh_delete_name, 876087719fdSdp sizeof (old_delname)); 877087719fdSdp if (!is_new(handle) && !is_renaming(handle)) { 878087719fdSdp /* 879087719fdSdp * Name change is allowed only when the zone we're altering 880087719fdSdp * is not ready or running. 881087719fdSdp */ 882087719fdSdp err = zone_get_state(curname, &state); 883087719fdSdp if (err == Z_OK) { 884087719fdSdp if (state > ZONE_STATE_INSTALLED) 885087719fdSdp return (Z_BAD_ZONE_STATE); 886087719fdSdp } else if (err != Z_NO_ZONE) { 887087719fdSdp return (err); 888087719fdSdp } 889087719fdSdp 890087719fdSdp (void) strlcpy(handle->zone_dh_delete_name, curname, 891087719fdSdp sizeof (handle->zone_dh_delete_name)); 892087719fdSdp assert(is_renaming(handle)); 893087719fdSdp } else if (is_renaming(handle)) { 894087719fdSdp err = zone_get_state(handle->zone_dh_delete_name, &state); 895087719fdSdp if (err == Z_OK) { 896087719fdSdp if (state > ZONE_STATE_INSTALLED) 897087719fdSdp return (Z_BAD_ZONE_STATE); 898087719fdSdp } else if (err != Z_NO_ZONE) { 899087719fdSdp return (err); 900087719fdSdp } 901087719fdSdp } 902087719fdSdp 903087719fdSdp if ((err = setrootattr(handle, DTD_ATTR_NAME, name)) != Z_OK) { 904087719fdSdp /* 905087719fdSdp * Restore the deletename to whatever it was at the 906087719fdSdp * top of the routine, since we've had a failure. 907087719fdSdp */ 908087719fdSdp (void) strlcpy(handle->zone_dh_delete_name, old_delname, 909087719fdSdp sizeof (handle->zone_dh_delete_name)); 910087719fdSdp return (err); 911087719fdSdp } 912087719fdSdp 913087719fdSdp return (Z_OK); 9147c478bd9Sstevel@tonic-gate } 915a1be23daSdp 916a1be23daSdp int 917a1be23daSdp zonecfg_get_zonepath(zone_dochandle_t handle, char *path, size_t pathsize) 918a1be23daSdp { 919108322fbScarlsonj size_t len; 920108322fbScarlsonj 921108322fbScarlsonj if ((len = strlcpy(path, zonecfg_root, pathsize)) >= pathsize) 922108322fbScarlsonj return (Z_TOO_BIG); 923108322fbScarlsonj return (getrootattr(handle, DTD_ATTR_ZONEPATH, path + len, 924108322fbScarlsonj pathsize - len)); 925a1be23daSdp } 926a1be23daSdp 927a1be23daSdp int 928a1be23daSdp zonecfg_set_zonepath(zone_dochandle_t handle, char *zonepath) 929a1be23daSdp { 930555afedfScarlsonj size_t len; 931555afedfScarlsonj 932555afedfScarlsonj /* 933555afedfScarlsonj * The user deals in absolute paths in the running global zone, but the 934555afedfScarlsonj * internal configuration files deal with boot environment relative 935555afedfScarlsonj * paths. Strip out the alternate root when specified. 936555afedfScarlsonj */ 937555afedfScarlsonj len = strlen(zonecfg_root); 938555afedfScarlsonj if (strncmp(zonepath, zonecfg_root, len) != 0 || zonepath[len] != '/') 939555afedfScarlsonj return (Z_BAD_PROPERTY); 940555afedfScarlsonj zonepath += len; 941a1be23daSdp return (setrootattr(handle, DTD_ATTR_ZONEPATH, zonepath)); 942a1be23daSdp } 943a1be23daSdp 944a1be23daSdp int 9459acbbeafSnn35248 zonecfg_get_brand(zone_dochandle_t handle, char *brand, size_t brandsize) 9469acbbeafSnn35248 { 9479acbbeafSnn35248 int ret, sz; 9489acbbeafSnn35248 9499acbbeafSnn35248 ret = getrootattr(handle, DTD_ATTR_BRAND, brand, brandsize); 9509acbbeafSnn35248 9519acbbeafSnn35248 /* If the zone has no brand, it is native. */ 9529acbbeafSnn35248 if (ret == Z_OK && brand[0] == '\0') { 9539acbbeafSnn35248 sz = strlcpy(brand, NATIVE_BRAND_NAME, brandsize); 9549acbbeafSnn35248 if (sz >= brandsize) 9559acbbeafSnn35248 ret = Z_TOO_BIG; 9569acbbeafSnn35248 else 9579acbbeafSnn35248 ret = Z_OK; 9589acbbeafSnn35248 } 9599acbbeafSnn35248 9609acbbeafSnn35248 return (ret); 9619acbbeafSnn35248 } 9629acbbeafSnn35248 9639acbbeafSnn35248 int 9649acbbeafSnn35248 zonecfg_set_brand(zone_dochandle_t handle, char *brand) 9659acbbeafSnn35248 { 9669acbbeafSnn35248 return (setrootattr(handle, DTD_ATTR_BRAND, brand)); 9679acbbeafSnn35248 } 9689acbbeafSnn35248 9699acbbeafSnn35248 int 970a1be23daSdp zonecfg_get_autoboot(zone_dochandle_t handle, boolean_t *autoboot) 971a1be23daSdp { 972a1be23daSdp char autobootstr[DTD_ENTITY_BOOL_LEN]; 973a1be23daSdp int ret; 974a1be23daSdp 975a1be23daSdp if ((ret = getrootattr(handle, DTD_ATTR_AUTOBOOT, autobootstr, 976a1be23daSdp sizeof (autobootstr))) != Z_OK) 977a1be23daSdp return (ret); 978a1be23daSdp 979a1be23daSdp if (strcmp(autobootstr, DTD_ENTITY_TRUE) == 0) 980a1be23daSdp *autoboot = B_TRUE; 981a1be23daSdp else if (strcmp(autobootstr, DTD_ENTITY_FALSE) == 0) 982a1be23daSdp *autoboot = B_FALSE; 983a1be23daSdp else 984a1be23daSdp ret = Z_BAD_PROPERTY; 985a1be23daSdp return (ret); 986a1be23daSdp } 987a1be23daSdp 988a1be23daSdp int 989a1be23daSdp zonecfg_set_autoboot(zone_dochandle_t handle, boolean_t autoboot) 990a1be23daSdp { 991a1be23daSdp return (setrootattr(handle, DTD_ATTR_AUTOBOOT, 992a1be23daSdp autoboot ? DTD_ENTITY_TRUE : DTD_ENTITY_FALSE)); 993a1be23daSdp } 994a1be23daSdp 995a1be23daSdp int 996a1be23daSdp zonecfg_get_pool(zone_dochandle_t handle, char *pool, size_t poolsize) 997a1be23daSdp { 998a1be23daSdp return (getrootattr(handle, DTD_ATTR_POOL, pool, poolsize)); 999a1be23daSdp } 1000a1be23daSdp 1001a1be23daSdp int 1002a1be23daSdp zonecfg_set_pool(zone_dochandle_t handle, char *pool) 1003a1be23daSdp { 1004a1be23daSdp return (setrootattr(handle, DTD_ATTR_POOL, pool)); 1005a1be23daSdp } 1006a1be23daSdp 1007ffbafc53Scomay int 1008ffbafc53Scomay zonecfg_get_limitpriv(zone_dochandle_t handle, char **limitpriv) 1009ffbafc53Scomay { 1010ffbafc53Scomay return (get_alloc_rootattr(handle, DTD_ATTR_LIMITPRIV, limitpriv)); 1011ffbafc53Scomay } 1012ffbafc53Scomay 1013ffbafc53Scomay int 10143f2f09c1Sdp zonecfg_set_limitpriv(zone_dochandle_t handle, char *limitpriv) 1015ffbafc53Scomay { 10163f2f09c1Sdp return (setrootattr(handle, DTD_ATTR_LIMITPRIV, limitpriv)); 10173f2f09c1Sdp } 10183f2f09c1Sdp 10193f2f09c1Sdp int 10203f2f09c1Sdp zonecfg_get_bootargs(zone_dochandle_t handle, char *bargs, size_t bargssize) 10213f2f09c1Sdp { 10223f2f09c1Sdp return (getrootattr(handle, DTD_ATTR_BOOTARGS, bargs, bargssize)); 10233f2f09c1Sdp } 10243f2f09c1Sdp 10253f2f09c1Sdp int 10263f2f09c1Sdp zonecfg_set_bootargs(zone_dochandle_t handle, char *bargs) 10273f2f09c1Sdp { 10283f2f09c1Sdp return (setrootattr(handle, DTD_ATTR_BOOTARGS, bargs)); 1029ffbafc53Scomay } 1030ffbafc53Scomay 10310209230bSgjelinek int 10320209230bSgjelinek zonecfg_get_sched_class(zone_dochandle_t handle, char *sched, size_t schedsize) 10330209230bSgjelinek { 10340209230bSgjelinek return (getrootattr(handle, DTD_ATTR_SCHED, sched, schedsize)); 10350209230bSgjelinek } 10360209230bSgjelinek 10370209230bSgjelinek int 10380209230bSgjelinek zonecfg_set_sched(zone_dochandle_t handle, char *sched) 10390209230bSgjelinek { 10400209230bSgjelinek return (setrootattr(handle, DTD_ATTR_SCHED, sched)); 10410209230bSgjelinek } 10420209230bSgjelinek 1043a1be23daSdp /* 1044a1be23daSdp * /etc/zones/index caches a vital piece of information which is also 1045a1be23daSdp * in the <zonename>.xml file: the path to the zone. This is for performance, 1046a1be23daSdp * since we need to walk all zonepath's in order to be able to detect conflicts 1047a1be23daSdp * (see crosscheck_zonepaths() in the zoneadm command). 1048087719fdSdp * 1049087719fdSdp * An additional complexity is that when doing a rename, we'd like the entire 1050087719fdSdp * index update operation (rename, and potential state changes) to be atomic. 1051087719fdSdp * In general, the operation of this function should succeed or fail as 1052087719fdSdp * a unit. 1053a1be23daSdp */ 1054a1be23daSdp int 1055a1be23daSdp zonecfg_refresh_index_file(zone_dochandle_t handle) 1056a1be23daSdp { 1057a1be23daSdp char name[ZONENAME_MAX], zonepath[MAXPATHLEN]; 1058a1be23daSdp struct zoneent ze; 1059a1be23daSdp int err; 1060087719fdSdp int opcode; 1061087719fdSdp char *zn; 1062087719fdSdp 1063087719fdSdp bzero(&ze, sizeof (ze)); 1064087719fdSdp ze.zone_state = -1; /* Preserve existing state in index */ 1065a1be23daSdp 1066a1be23daSdp if ((err = zonecfg_get_name(handle, name, sizeof (name))) != Z_OK) 1067a1be23daSdp return (err); 1068087719fdSdp (void) strlcpy(ze.zone_name, name, sizeof (ze.zone_name)); 1069087719fdSdp 1070a1be23daSdp if ((err = zonecfg_get_zonepath(handle, zonepath, 1071a1be23daSdp sizeof (zonepath))) != Z_OK) 1072a1be23daSdp return (err); 1073555afedfScarlsonj (void) strlcpy(ze.zone_path, zonepath + strlen(zonecfg_root), 1074555afedfScarlsonj sizeof (ze.zone_path)); 1075087719fdSdp 1076087719fdSdp if (is_renaming(handle)) { 1077087719fdSdp opcode = PZE_MODIFY; 1078087719fdSdp (void) strlcpy(ze.zone_name, handle->zone_dh_delete_name, 1079087719fdSdp sizeof (ze.zone_name)); 1080087719fdSdp (void) strlcpy(ze.zone_newname, name, sizeof (ze.zone_newname)); 1081087719fdSdp } else if (is_new(handle)) { 1082087719fdSdp FILE *cookie; 1083087719fdSdp /* 1084087719fdSdp * Be tolerant of the zone already existing in the index file, 1085087719fdSdp * since we might be forcibly overwriting an existing 1086087719fdSdp * configuration with a new one (for example 'create -F' 1087087719fdSdp * in zonecfg). 1088087719fdSdp */ 1089087719fdSdp opcode = PZE_ADD; 1090087719fdSdp cookie = setzoneent(); 1091087719fdSdp while ((zn = getzoneent(cookie)) != NULL) { 1092087719fdSdp if (strcmp(zn, name) == 0) { 1093087719fdSdp opcode = PZE_MODIFY; 1094087719fdSdp free(zn); 1095087719fdSdp break; 1096087719fdSdp } 1097087719fdSdp free(zn); 1098087719fdSdp } 1099087719fdSdp endzoneent(cookie); 1100087719fdSdp ze.zone_state = ZONE_STATE_CONFIGURED; 1101087719fdSdp } else { 1102087719fdSdp opcode = PZE_MODIFY; 11037c478bd9Sstevel@tonic-gate } 11047c478bd9Sstevel@tonic-gate 1105087719fdSdp if ((err = putzoneent(&ze, opcode)) != Z_OK) 1106087719fdSdp return (err); 1107087719fdSdp 1108087719fdSdp return (Z_OK); 1109087719fdSdp } 1110087719fdSdp 1111087719fdSdp /* 1112087719fdSdp * The goal of this routine is to cause the index file update and the 1113087719fdSdp * document save to happen as an atomic operation. We do the document 1114087719fdSdp * first, saving a backup copy using a hard link; if that succeeds, we go 1115087719fdSdp * on to the index. If that fails, we roll the document back into place. 1116087719fdSdp * 1117087719fdSdp * Strategy: 1118087719fdSdp * 1119087719fdSdp * New zone 'foo' configuration: 1120087719fdSdp * Create tmpfile (zonecfg.xxxxxx) 1121087719fdSdp * Write XML to tmpfile 1122087719fdSdp * Rename tmpfile to xmlfile (zonecfg.xxxxxx -> foo.xml) 1123087719fdSdp * Add entry to index file 1124087719fdSdp * If it fails, delete foo.xml, leaving nothing behind. 1125087719fdSdp * 1126087719fdSdp * Save existing zone 'foo': 1127087719fdSdp * Make backup of foo.xml -> .backup 1128087719fdSdp * Create tmpfile (zonecfg.xxxxxx) 1129087719fdSdp * Write XML to tmpfile 1130087719fdSdp * Rename tmpfile to xmlfile (zonecfg.xxxxxx -> foo.xml) 1131087719fdSdp * Modify index file as needed 1132087719fdSdp * If it fails, recover from .backup -> foo.xml 1133087719fdSdp * 1134087719fdSdp * Rename 'foo' to 'bar': 1135087719fdSdp * Create tmpfile (zonecfg.xxxxxx) 1136087719fdSdp * Write XML to tmpfile 1137087719fdSdp * Rename tmpfile to xmlfile (zonecfg.xxxxxx -> bar.xml) 1138087719fdSdp * Add entry for 'bar' to index file, Remove entry for 'foo' (refresh) 1139087719fdSdp * If it fails, delete bar.xml; foo.xml is left behind. 1140087719fdSdp */ 11417c478bd9Sstevel@tonic-gate static int 11427c478bd9Sstevel@tonic-gate zonecfg_save_impl(zone_dochandle_t handle, char *filename) 11437c478bd9Sstevel@tonic-gate { 11447c478bd9Sstevel@tonic-gate char tmpfile[MAXPATHLEN]; 1145087719fdSdp char bakdir[MAXPATHLEN], bakbase[MAXPATHLEN], bakfile[MAXPATHLEN]; 11469acbbeafSnn35248 int tmpfd, err, valid; 11477c478bd9Sstevel@tonic-gate xmlValidCtxt cvp = { NULL }; 1148087719fdSdp boolean_t backup; 11497c478bd9Sstevel@tonic-gate 11507c478bd9Sstevel@tonic-gate (void) strlcpy(tmpfile, filename, sizeof (tmpfile)); 11517c478bd9Sstevel@tonic-gate (void) dirname(tmpfile); 11527c478bd9Sstevel@tonic-gate (void) strlcat(tmpfile, _PATH_TMPFILE, sizeof (tmpfile)); 11537c478bd9Sstevel@tonic-gate 11547c478bd9Sstevel@tonic-gate tmpfd = mkstemp(tmpfile); 11557c478bd9Sstevel@tonic-gate if (tmpfd == -1) { 11567c478bd9Sstevel@tonic-gate (void) unlink(tmpfile); 11577c478bd9Sstevel@tonic-gate return (Z_TEMP_FILE); 11587c478bd9Sstevel@tonic-gate } 11597c478bd9Sstevel@tonic-gate (void) close(tmpfd); 11607c478bd9Sstevel@tonic-gate 11617c478bd9Sstevel@tonic-gate cvp.error = zonecfg_error_func; 11627c478bd9Sstevel@tonic-gate cvp.warning = zonecfg_error_func; 11637c478bd9Sstevel@tonic-gate 1164087719fdSdp /* 11659acbbeafSnn35248 * We do a final validation of the document. Since the library has 11669acbbeafSnn35248 * malfunctioned if it fails to validate, we follow-up with an 11679acbbeafSnn35248 * assert() that the doc is valid. 1168087719fdSdp */ 11699acbbeafSnn35248 valid = xmlValidateDocument(&cvp, handle->zone_dh_doc); 11709acbbeafSnn35248 assert(valid != 0); 11717c478bd9Sstevel@tonic-gate 11727c478bd9Sstevel@tonic-gate if (xmlSaveFormatFile(tmpfile, handle->zone_dh_doc, 1) <= 0) 11737c478bd9Sstevel@tonic-gate goto err; 1174087719fdSdp 11757c478bd9Sstevel@tonic-gate (void) chmod(tmpfile, 0644); 11767c478bd9Sstevel@tonic-gate 1177087719fdSdp /* 1178087719fdSdp * In the event we are doing a standard save, hard link a copy of the 1179087719fdSdp * original file in .backup.<pid>.filename so we can restore it if 1180087719fdSdp * something goes wrong. 1181087719fdSdp */ 1182087719fdSdp if (!is_new(handle) && !is_renaming(handle)) { 1183087719fdSdp backup = B_TRUE; 1184087719fdSdp 1185087719fdSdp (void) strlcpy(bakdir, filename, sizeof (bakdir)); 1186087719fdSdp (void) strlcpy(bakbase, filename, sizeof (bakbase)); 1187087719fdSdp (void) snprintf(bakfile, sizeof (bakfile), "%s/.backup.%d.%s", 1188087719fdSdp dirname(bakdir), getpid(), basename(bakbase)); 1189087719fdSdp 1190087719fdSdp if (link(filename, bakfile) == -1) { 1191087719fdSdp err = errno; 11927c478bd9Sstevel@tonic-gate (void) unlink(tmpfile); 11937c478bd9Sstevel@tonic-gate if (errno == EACCES) 11947c478bd9Sstevel@tonic-gate return (Z_ACCES); 11957c478bd9Sstevel@tonic-gate return (Z_MISC_FS); 11967c478bd9Sstevel@tonic-gate } 1197087719fdSdp } 1198a1be23daSdp 1199087719fdSdp /* 1200087719fdSdp * Move the new document over top of the old. 1201087719fdSdp * i.e.: zonecfg.XXXXXX -> myzone.xml 1202087719fdSdp */ 1203087719fdSdp if (rename(tmpfile, filename) == -1) { 1204087719fdSdp err = errno; 1205087719fdSdp (void) unlink(tmpfile); 1206087719fdSdp if (backup) 1207087719fdSdp (void) unlink(bakfile); 1208087719fdSdp if (err == EACCES) 1209087719fdSdp return (Z_ACCES); 1210087719fdSdp return (Z_MISC_FS); 1211087719fdSdp } 1212087719fdSdp 1213087719fdSdp /* 1214087719fdSdp * If this is a snapshot, we're done-- don't add an index entry. 1215087719fdSdp */ 1216087719fdSdp if (is_snapshot(handle)) 1217087719fdSdp return (Z_OK); 1218087719fdSdp 1219087719fdSdp /* now update the index file to reflect whatever we just did */ 1220087719fdSdp if ((err = zonecfg_refresh_index_file(handle)) != Z_OK) { 1221087719fdSdp if (backup) { 1222087719fdSdp /* 1223087719fdSdp * Try to restore from our backup. 1224087719fdSdp */ 1225087719fdSdp (void) unlink(filename); 1226087719fdSdp (void) rename(bakfile, filename); 1227087719fdSdp } else { 1228087719fdSdp /* 1229087719fdSdp * Either the zone is new, in which case we can delete 1230087719fdSdp * new.xml, or we're doing a rename, so ditto. 1231087719fdSdp */ 1232087719fdSdp assert(is_new(handle) || is_renaming(handle)); 1233087719fdSdp (void) unlink(filename); 1234087719fdSdp } 1235087719fdSdp return (Z_UPDATING_INDEX); 1236087719fdSdp } 1237087719fdSdp 1238087719fdSdp if (backup) 1239087719fdSdp (void) unlink(bakfile); 1240087719fdSdp 1241087719fdSdp return (Z_OK); 12427c478bd9Sstevel@tonic-gate 12437c478bd9Sstevel@tonic-gate err: 12447c478bd9Sstevel@tonic-gate (void) unlink(tmpfile); 12457c478bd9Sstevel@tonic-gate return (Z_SAVING_FILE); 12467c478bd9Sstevel@tonic-gate } 12477c478bd9Sstevel@tonic-gate 12487c478bd9Sstevel@tonic-gate int 12497c478bd9Sstevel@tonic-gate zonecfg_save(zone_dochandle_t handle) 12507c478bd9Sstevel@tonic-gate { 1251087719fdSdp char zname[ZONENAME_MAX], path[MAXPATHLEN]; 1252087719fdSdp char delpath[MAXPATHLEN]; 1253087719fdSdp int err = Z_SAVING_FILE; 12547c478bd9Sstevel@tonic-gate 1255087719fdSdp if (zonecfg_check_handle(handle) != Z_OK) 1256087719fdSdp return (Z_BAD_HANDLE); 1257087719fdSdp 1258087719fdSdp /* 1259ee519a1fSgjelinek * We don't support saving snapshots or a tree containing a sw 1260ee519a1fSgjelinek * inventory at this time. 1261087719fdSdp */ 1262ee519a1fSgjelinek if (handle->zone_dh_snapshot || handle->zone_dh_sw_inv) 1263087719fdSdp return (Z_INVAL); 1264087719fdSdp 1265087719fdSdp if ((err = zonecfg_get_name(handle, zname, sizeof (zname))) != Z_OK) 12667c478bd9Sstevel@tonic-gate return (err); 1267087719fdSdp 1268108322fbScarlsonj if (!config_file_path(zname, path)) 1269108322fbScarlsonj return (Z_MISC_FS); 1270087719fdSdp 1271087719fdSdp addcomment(handle, "\n DO NOT EDIT THIS " 1272087719fdSdp "FILE. Use zonecfg(1M) instead.\n"); 1273087719fdSdp 1274087719fdSdp err = zonecfg_save_impl(handle, path); 1275087719fdSdp 1276087719fdSdp stripcomments(handle); 1277087719fdSdp 1278087719fdSdp if (err != Z_OK) 1279087719fdSdp return (err); 1280087719fdSdp 1281087719fdSdp handle->zone_dh_newzone = B_FALSE; 1282087719fdSdp 1283087719fdSdp if (is_renaming(handle)) { 1284108322fbScarlsonj if (config_file_path(handle->zone_dh_delete_name, delpath)) 1285087719fdSdp (void) unlink(delpath); 1286087719fdSdp handle->zone_dh_delete_name[0] = '\0'; 1287087719fdSdp } 1288087719fdSdp 1289087719fdSdp return (Z_OK); 12907c478bd9Sstevel@tonic-gate } 12917c478bd9Sstevel@tonic-gate 1292ee519a1fSgjelinek int 12939acbbeafSnn35248 zonecfg_verify_save(zone_dochandle_t handle, char *filename) 12949acbbeafSnn35248 { 12959acbbeafSnn35248 int valid; 12969acbbeafSnn35248 12979acbbeafSnn35248 xmlValidCtxt cvp = { NULL }; 12989acbbeafSnn35248 12999acbbeafSnn35248 if (zonecfg_check_handle(handle) != Z_OK) 13009acbbeafSnn35248 return (Z_BAD_HANDLE); 13019acbbeafSnn35248 13029acbbeafSnn35248 cvp.error = zonecfg_error_func; 13039acbbeafSnn35248 cvp.warning = zonecfg_error_func; 13049acbbeafSnn35248 13059acbbeafSnn35248 /* 13069acbbeafSnn35248 * We do a final validation of the document. Since the library has 13079acbbeafSnn35248 * malfunctioned if it fails to validate, we follow-up with an 13089acbbeafSnn35248 * assert() that the doc is valid. 13099acbbeafSnn35248 */ 13109acbbeafSnn35248 valid = xmlValidateDocument(&cvp, handle->zone_dh_doc); 13119acbbeafSnn35248 assert(valid != 0); 13129acbbeafSnn35248 13139acbbeafSnn35248 if (xmlSaveFormatFile(filename, handle->zone_dh_doc, 1) <= 0) 13149acbbeafSnn35248 return (Z_SAVING_FILE); 13159acbbeafSnn35248 13169acbbeafSnn35248 return (Z_OK); 13179acbbeafSnn35248 } 13189acbbeafSnn35248 13199acbbeafSnn35248 int 13208cd327d5Sgjelinek zonecfg_detach_save(zone_dochandle_t handle, uint_t flags) 1321ee519a1fSgjelinek { 1322ee519a1fSgjelinek char zname[ZONENAME_MAX]; 1323ee519a1fSgjelinek char path[MAXPATHLEN]; 1324ee519a1fSgjelinek char migpath[MAXPATHLEN]; 1325ee519a1fSgjelinek xmlValidCtxt cvp = { NULL }; 1326ee519a1fSgjelinek int err = Z_SAVING_FILE; 13279acbbeafSnn35248 int valid; 1328ee519a1fSgjelinek 1329ee519a1fSgjelinek if (zonecfg_check_handle(handle) != Z_OK) 1330ee519a1fSgjelinek return (Z_BAD_HANDLE); 1331ee519a1fSgjelinek 1332ee519a1fSgjelinek /* 1333ee519a1fSgjelinek * We can only detach if we have taken a sw inventory. 1334ee519a1fSgjelinek */ 1335ee519a1fSgjelinek if (!handle->zone_dh_sw_inv) 1336ee519a1fSgjelinek return (Z_INVAL); 1337ee519a1fSgjelinek 13388cd327d5Sgjelinek if (flags & ZONE_DRY_RUN) { 13398cd327d5Sgjelinek (void) strlcpy(migpath, "-", sizeof (migpath)); 13408cd327d5Sgjelinek } else { 13418cd327d5Sgjelinek if ((err = zonecfg_get_name(handle, zname, sizeof (zname))) 13428cd327d5Sgjelinek != Z_OK) 1343ee519a1fSgjelinek return (err); 1344ee519a1fSgjelinek 13458cd327d5Sgjelinek if ((err = zone_get_zonepath(zname, path, sizeof (path))) 13468cd327d5Sgjelinek != Z_OK) 1347ee519a1fSgjelinek return (err); 1348ee519a1fSgjelinek 13498cd327d5Sgjelinek if (snprintf(migpath, sizeof (migpath), "%s/%s", path, DETACHED) 13508cd327d5Sgjelinek >= sizeof (migpath)) 1351ee519a1fSgjelinek return (Z_NOMEM); 13528cd327d5Sgjelinek } 1353ee519a1fSgjelinek 1354ee519a1fSgjelinek if ((err = operation_prep(handle)) != Z_OK) 1355ee519a1fSgjelinek return (err); 1356ee519a1fSgjelinek 1357ee519a1fSgjelinek addcomment(handle, "\n DO NOT EDIT THIS FILE. " 1358ee519a1fSgjelinek "Use zonecfg(1M) and zoneadm(1M) attach.\n"); 1359ee519a1fSgjelinek 1360ee519a1fSgjelinek cvp.error = zonecfg_error_func; 1361ee519a1fSgjelinek cvp.warning = zonecfg_error_func; 1362ee519a1fSgjelinek 1363ee519a1fSgjelinek /* 13649acbbeafSnn35248 * We do a final validation of the document. Since the library has 13659acbbeafSnn35248 * malfunctioned if it fails to validate, we follow-up with an 13669acbbeafSnn35248 * assert() that the doc is valid. 1367ee519a1fSgjelinek */ 13689acbbeafSnn35248 valid = xmlValidateDocument(&cvp, handle->zone_dh_doc); 13699acbbeafSnn35248 assert(valid != 0); 1370ee519a1fSgjelinek 1371ee519a1fSgjelinek if (xmlSaveFormatFile(migpath, handle->zone_dh_doc, 1) <= 0) 1372ee519a1fSgjelinek return (Z_SAVING_FILE); 1373ee519a1fSgjelinek 13748cd327d5Sgjelinek if (!(flags & ZONE_DRY_RUN)) 1375ee519a1fSgjelinek (void) chmod(migpath, 0644); 1376ee519a1fSgjelinek 1377ee519a1fSgjelinek stripcomments(handle); 1378ee519a1fSgjelinek 1379ee519a1fSgjelinek handle->zone_dh_newzone = B_FALSE; 1380ee519a1fSgjelinek 1381ee519a1fSgjelinek return (Z_OK); 1382ee519a1fSgjelinek } 1383ee519a1fSgjelinek 1384ee519a1fSgjelinek boolean_t 1385ee519a1fSgjelinek zonecfg_detached(const char *path) 1386ee519a1fSgjelinek { 1387ee519a1fSgjelinek char migpath[MAXPATHLEN]; 1388ee519a1fSgjelinek struct stat buf; 1389ee519a1fSgjelinek 1390ee519a1fSgjelinek if (snprintf(migpath, sizeof (migpath), "%s/%s", path, DETACHED) >= 1391ee519a1fSgjelinek sizeof (migpath)) 1392ee519a1fSgjelinek return (B_FALSE); 1393ee519a1fSgjelinek 1394ee519a1fSgjelinek if (stat(migpath, &buf) != -1) 1395ee519a1fSgjelinek return (B_TRUE); 1396ee519a1fSgjelinek 1397ee519a1fSgjelinek return (B_FALSE); 1398ee519a1fSgjelinek } 1399ee519a1fSgjelinek 1400ee519a1fSgjelinek void 1401ee519a1fSgjelinek zonecfg_rm_detached(zone_dochandle_t handle, boolean_t forced) 1402ee519a1fSgjelinek { 1403ee519a1fSgjelinek char zname[ZONENAME_MAX]; 1404ee519a1fSgjelinek char path[MAXPATHLEN]; 1405ee519a1fSgjelinek char detached[MAXPATHLEN]; 1406ee519a1fSgjelinek char attached[MAXPATHLEN]; 1407ee519a1fSgjelinek 1408ee519a1fSgjelinek if (zonecfg_check_handle(handle) != Z_OK) 1409ee519a1fSgjelinek return; 1410ee519a1fSgjelinek 1411ee519a1fSgjelinek if (zonecfg_get_name(handle, zname, sizeof (zname)) != Z_OK) 1412ee519a1fSgjelinek return; 1413ee519a1fSgjelinek 1414ee519a1fSgjelinek if (zone_get_zonepath(zname, path, sizeof (path)) != Z_OK) 1415ee519a1fSgjelinek return; 1416ee519a1fSgjelinek 1417ee519a1fSgjelinek (void) snprintf(detached, sizeof (detached), "%s/%s", path, DETACHED); 1418ee519a1fSgjelinek (void) snprintf(attached, sizeof (attached), "%s/%s", path, 1419ee519a1fSgjelinek ATTACH_FORCED); 1420ee519a1fSgjelinek 1421ee519a1fSgjelinek if (forced) { 1422ee519a1fSgjelinek (void) rename(detached, attached); 1423ee519a1fSgjelinek } else { 1424ee519a1fSgjelinek (void) unlink(attached); 1425ee519a1fSgjelinek (void) unlink(detached); 1426ee519a1fSgjelinek } 1427ee519a1fSgjelinek } 1428ee519a1fSgjelinek 14297c478bd9Sstevel@tonic-gate /* 14307c478bd9Sstevel@tonic-gate * Special case: if access(2) fails with ENOENT, then try again using 14317c478bd9Sstevel@tonic-gate * ZONE_CONFIG_ROOT instead of config_file_path(zonename). This is how we 14327c478bd9Sstevel@tonic-gate * work around the case of a config file which has not been created yet: 14337c478bd9Sstevel@tonic-gate * the user will need access to the directory so use that as a heuristic. 14347c478bd9Sstevel@tonic-gate */ 14357c478bd9Sstevel@tonic-gate 14367c478bd9Sstevel@tonic-gate int 14377c478bd9Sstevel@tonic-gate zonecfg_access(const char *zonename, int amode) 14387c478bd9Sstevel@tonic-gate { 14397c478bd9Sstevel@tonic-gate char path[MAXPATHLEN]; 14407c478bd9Sstevel@tonic-gate 1441108322fbScarlsonj if (!config_file_path(zonename, path)) 1442108322fbScarlsonj return (Z_INVAL); 14437c478bd9Sstevel@tonic-gate if (access(path, amode) == 0) 14447c478bd9Sstevel@tonic-gate return (Z_OK); 1445108322fbScarlsonj if (errno == ENOENT) { 1446108322fbScarlsonj if (snprintf(path, sizeof (path), "%s%s", zonecfg_root, 1447108322fbScarlsonj ZONE_CONFIG_ROOT) >= sizeof (path)) 1448108322fbScarlsonj return (Z_INVAL); 1449108322fbScarlsonj if (access(path, amode) == 0) 14507c478bd9Sstevel@tonic-gate return (Z_OK); 1451108322fbScarlsonj } 14527c478bd9Sstevel@tonic-gate if (errno == EACCES) 14537c478bd9Sstevel@tonic-gate return (Z_ACCES); 14547c478bd9Sstevel@tonic-gate if (errno == EINVAL) 14557c478bd9Sstevel@tonic-gate return (Z_INVAL); 14567c478bd9Sstevel@tonic-gate return (Z_MISC_FS); 14577c478bd9Sstevel@tonic-gate } 14587c478bd9Sstevel@tonic-gate 14597c478bd9Sstevel@tonic-gate int 1460108322fbScarlsonj zonecfg_create_snapshot(const char *zonename) 14617c478bd9Sstevel@tonic-gate { 14627c478bd9Sstevel@tonic-gate zone_dochandle_t handle; 14637c478bd9Sstevel@tonic-gate char path[MAXPATHLEN], zonepath[MAXPATHLEN], rpath[MAXPATHLEN]; 14647c478bd9Sstevel@tonic-gate int error = Z_OK, res; 14657c478bd9Sstevel@tonic-gate 14667c478bd9Sstevel@tonic-gate if ((handle = zonecfg_init_handle()) == NULL) { 14677c478bd9Sstevel@tonic-gate return (Z_NOMEM); 14687c478bd9Sstevel@tonic-gate } 14697c478bd9Sstevel@tonic-gate 1470087719fdSdp handle->zone_dh_newzone = B_TRUE; 1471087719fdSdp handle->zone_dh_snapshot = B_TRUE; 1472087719fdSdp 14737c478bd9Sstevel@tonic-gate if ((error = zonecfg_get_handle(zonename, handle)) != Z_OK) 14747c478bd9Sstevel@tonic-gate goto out; 14757c478bd9Sstevel@tonic-gate if ((error = operation_prep(handle)) != Z_OK) 14767c478bd9Sstevel@tonic-gate goto out; 14777c478bd9Sstevel@tonic-gate error = zonecfg_get_zonepath(handle, zonepath, sizeof (zonepath)); 14787c478bd9Sstevel@tonic-gate if (error != Z_OK) 14797c478bd9Sstevel@tonic-gate goto out; 14807c478bd9Sstevel@tonic-gate if ((res = resolvepath(zonepath, rpath, sizeof (rpath))) == -1) { 14817c478bd9Sstevel@tonic-gate error = Z_RESOLVED_PATH; 14827c478bd9Sstevel@tonic-gate goto out; 14837c478bd9Sstevel@tonic-gate } 14847c478bd9Sstevel@tonic-gate /* 14857c478bd9Sstevel@tonic-gate * If the resolved path is not the same as the original path, then 14867c478bd9Sstevel@tonic-gate * save the resolved path in the snapshot, thus preventing any 14877c478bd9Sstevel@tonic-gate * potential problems down the line when zoneadmd goes to unmount 14887c478bd9Sstevel@tonic-gate * file systems and depends on initial string matches with resolved 14897c478bd9Sstevel@tonic-gate * paths. 14907c478bd9Sstevel@tonic-gate */ 14917c478bd9Sstevel@tonic-gate rpath[res] = '\0'; 14927c478bd9Sstevel@tonic-gate if (strcmp(zonepath, rpath) != 0) { 14937c478bd9Sstevel@tonic-gate if ((error = zonecfg_set_zonepath(handle, rpath)) != Z_OK) 14947c478bd9Sstevel@tonic-gate goto out; 14957c478bd9Sstevel@tonic-gate } 1496108322fbScarlsonj if (snprintf(path, sizeof (path), "%s%s", zonecfg_root, 1497108322fbScarlsonj ZONE_SNAPSHOT_ROOT) >= sizeof (path)) { 1498108322fbScarlsonj error = Z_MISC_FS; 1499108322fbScarlsonj goto out; 1500108322fbScarlsonj } 1501108322fbScarlsonj if ((mkdir(path, S_IRWXU) == -1) && (errno != EEXIST)) { 15027c478bd9Sstevel@tonic-gate error = Z_MISC_FS; 15037c478bd9Sstevel@tonic-gate goto out; 15047c478bd9Sstevel@tonic-gate } 15057c478bd9Sstevel@tonic-gate 1506108322fbScarlsonj if (!snap_file_path(zonename, path)) { 1507108322fbScarlsonj error = Z_MISC_FS; 1508108322fbScarlsonj goto out; 1509108322fbScarlsonj } 1510087719fdSdp 1511087719fdSdp addcomment(handle, "\n DO NOT EDIT THIS FILE. " 1512087719fdSdp "It is a snapshot of running zone state.\n"); 1513087719fdSdp 15147c478bd9Sstevel@tonic-gate error = zonecfg_save_impl(handle, path); 15157c478bd9Sstevel@tonic-gate 1516087719fdSdp stripcomments(handle); 1517087719fdSdp 15187c478bd9Sstevel@tonic-gate out: 15197c478bd9Sstevel@tonic-gate zonecfg_fini_handle(handle); 15207c478bd9Sstevel@tonic-gate return (error); 15217c478bd9Sstevel@tonic-gate } 15227c478bd9Sstevel@tonic-gate 1523f4b3ec61Sdh155122 int 1524f4b3ec61Sdh155122 zonecfg_get_iptype(zone_dochandle_t handle, zone_iptype_t *iptypep) 1525f4b3ec61Sdh155122 { 1526f4b3ec61Sdh155122 char property[10]; /* 10 is big enough for "shared"/"exclusive" */ 1527f4b3ec61Sdh155122 int err; 1528f4b3ec61Sdh155122 1529f4b3ec61Sdh155122 err = getrootattr(handle, DTD_ATTR_IPTYPE, property, sizeof (property)); 1530f4b3ec61Sdh155122 if (err == Z_BAD_PROPERTY) { 1531f4b3ec61Sdh155122 /* Return default value */ 1532f4b3ec61Sdh155122 *iptypep = ZS_SHARED; 1533f4b3ec61Sdh155122 return (Z_OK); 1534f4b3ec61Sdh155122 } else if (err != Z_OK) { 1535f4b3ec61Sdh155122 return (err); 1536f4b3ec61Sdh155122 } 1537f4b3ec61Sdh155122 1538f4b3ec61Sdh155122 if (strlen(property) == 0 || 1539f4b3ec61Sdh155122 strcmp(property, "shared") == 0) 1540f4b3ec61Sdh155122 *iptypep = ZS_SHARED; 1541f4b3ec61Sdh155122 else if (strcmp(property, "exclusive") == 0) 1542f4b3ec61Sdh155122 *iptypep = ZS_EXCLUSIVE; 1543f4b3ec61Sdh155122 else 1544f4b3ec61Sdh155122 return (Z_INVAL); 1545f4b3ec61Sdh155122 1546f4b3ec61Sdh155122 return (Z_OK); 1547f4b3ec61Sdh155122 } 1548f4b3ec61Sdh155122 1549f4b3ec61Sdh155122 int 1550f4b3ec61Sdh155122 zonecfg_set_iptype(zone_dochandle_t handle, zone_iptype_t iptype) 1551f4b3ec61Sdh155122 { 1552f4b3ec61Sdh155122 xmlNodePtr cur; 1553f4b3ec61Sdh155122 1554f4b3ec61Sdh155122 if (handle == NULL) 1555f4b3ec61Sdh155122 return (Z_INVAL); 1556f4b3ec61Sdh155122 1557f4b3ec61Sdh155122 cur = xmlDocGetRootElement(handle->zone_dh_doc); 1558f4b3ec61Sdh155122 if (cur == NULL) { 1559f4b3ec61Sdh155122 return (Z_EMPTY_DOCUMENT); 1560f4b3ec61Sdh155122 } 1561f4b3ec61Sdh155122 1562f4b3ec61Sdh155122 if (xmlStrcmp(cur->name, DTD_ELEM_ZONE) != 0) { 1563f4b3ec61Sdh155122 return (Z_WRONG_DOC_TYPE); 1564f4b3ec61Sdh155122 } 1565f4b3ec61Sdh155122 switch (iptype) { 1566f4b3ec61Sdh155122 case ZS_SHARED: 1567f4b3ec61Sdh155122 /* 1568f4b3ec61Sdh155122 * Since "shared" is the default, we don't write it to the 1569f4b3ec61Sdh155122 * configuration file, so that it's easier to migrate those 1570f4b3ec61Sdh155122 * zones elsewhere, eg., to systems which are not IP-Instances 1571f4b3ec61Sdh155122 * aware. 1572f4b3ec61Sdh155122 * xmlUnsetProp only fails when the attribute doesn't exist, 1573f4b3ec61Sdh155122 * which we don't care. 1574f4b3ec61Sdh155122 */ 1575f4b3ec61Sdh155122 (void) xmlUnsetProp(cur, DTD_ATTR_IPTYPE); 1576f4b3ec61Sdh155122 break; 1577f4b3ec61Sdh155122 case ZS_EXCLUSIVE: 1578f4b3ec61Sdh155122 if (xmlSetProp(cur, DTD_ATTR_IPTYPE, 1579f4b3ec61Sdh155122 (const xmlChar *) "exclusive") == NULL) 1580f4b3ec61Sdh155122 return (Z_INVAL); 1581f4b3ec61Sdh155122 break; 1582f4b3ec61Sdh155122 } 1583f4b3ec61Sdh155122 return (Z_OK); 1584f4b3ec61Sdh155122 } 1585f4b3ec61Sdh155122 15867c478bd9Sstevel@tonic-gate static int 1587a1be23daSdp newprop(xmlNodePtr node, const xmlChar *attrname, char *src) 15887c478bd9Sstevel@tonic-gate { 15897c478bd9Sstevel@tonic-gate xmlAttrPtr newattr; 15907c478bd9Sstevel@tonic-gate 15917c478bd9Sstevel@tonic-gate newattr = xmlNewProp(node, attrname, (xmlChar *)src); 15927c478bd9Sstevel@tonic-gate if (newattr == NULL) { 15937c478bd9Sstevel@tonic-gate xmlUnlinkNode(node); 15947c478bd9Sstevel@tonic-gate xmlFreeNode(node); 15957c478bd9Sstevel@tonic-gate return (Z_BAD_PROPERTY); 15967c478bd9Sstevel@tonic-gate } 15977c478bd9Sstevel@tonic-gate return (Z_OK); 15987c478bd9Sstevel@tonic-gate } 15997c478bd9Sstevel@tonic-gate 16007c478bd9Sstevel@tonic-gate static int 16017c478bd9Sstevel@tonic-gate zonecfg_add_filesystem_core(zone_dochandle_t handle, struct zone_fstab *tabptr) 16027c478bd9Sstevel@tonic-gate { 16037c478bd9Sstevel@tonic-gate xmlNodePtr newnode, cur = handle->zone_dh_cur, options_node; 16047c478bd9Sstevel@tonic-gate zone_fsopt_t *ptr; 16057c478bd9Sstevel@tonic-gate int err; 16067c478bd9Sstevel@tonic-gate 16077c478bd9Sstevel@tonic-gate newnode = xmlNewTextChild(cur, NULL, DTD_ELEM_FS, NULL); 1608a1be23daSdp if ((err = newprop(newnode, DTD_ATTR_SPECIAL, 16097c478bd9Sstevel@tonic-gate tabptr->zone_fs_special)) != Z_OK) 16107c478bd9Sstevel@tonic-gate return (err); 16117c478bd9Sstevel@tonic-gate if (tabptr->zone_fs_raw[0] != '\0' && 1612a1be23daSdp (err = newprop(newnode, DTD_ATTR_RAW, tabptr->zone_fs_raw)) != Z_OK) 16137c478bd9Sstevel@tonic-gate return (err); 1614a1be23daSdp if ((err = newprop(newnode, DTD_ATTR_DIR, tabptr->zone_fs_dir)) != Z_OK) 16157c478bd9Sstevel@tonic-gate return (err); 1616a1be23daSdp if ((err = newprop(newnode, DTD_ATTR_TYPE, 16177c478bd9Sstevel@tonic-gate tabptr->zone_fs_type)) != Z_OK) 16187c478bd9Sstevel@tonic-gate return (err); 16197c478bd9Sstevel@tonic-gate if (tabptr->zone_fs_options != NULL) { 16207c478bd9Sstevel@tonic-gate for (ptr = tabptr->zone_fs_options; ptr != NULL; 16217c478bd9Sstevel@tonic-gate ptr = ptr->zone_fsopt_next) { 16227c478bd9Sstevel@tonic-gate options_node = xmlNewTextChild(newnode, NULL, 16237c478bd9Sstevel@tonic-gate DTD_ELEM_FSOPTION, NULL); 1624a1be23daSdp if ((err = newprop(options_node, DTD_ATTR_NAME, 16257c478bd9Sstevel@tonic-gate ptr->zone_fsopt_opt)) != Z_OK) 16267c478bd9Sstevel@tonic-gate return (err); 16277c478bd9Sstevel@tonic-gate } 16287c478bd9Sstevel@tonic-gate } 16297c478bd9Sstevel@tonic-gate return (Z_OK); 16307c478bd9Sstevel@tonic-gate } 16317c478bd9Sstevel@tonic-gate 16327c478bd9Sstevel@tonic-gate int 16337c478bd9Sstevel@tonic-gate zonecfg_add_filesystem(zone_dochandle_t handle, struct zone_fstab *tabptr) 16347c478bd9Sstevel@tonic-gate { 16357c478bd9Sstevel@tonic-gate int err; 16367c478bd9Sstevel@tonic-gate 16377c478bd9Sstevel@tonic-gate if (tabptr == NULL) 16387c478bd9Sstevel@tonic-gate return (Z_INVAL); 16397c478bd9Sstevel@tonic-gate 16407c478bd9Sstevel@tonic-gate if ((err = operation_prep(handle)) != Z_OK) 16417c478bd9Sstevel@tonic-gate return (err); 16427c478bd9Sstevel@tonic-gate 16437c478bd9Sstevel@tonic-gate if ((err = zonecfg_add_filesystem_core(handle, tabptr)) != Z_OK) 16447c478bd9Sstevel@tonic-gate return (err); 16457c478bd9Sstevel@tonic-gate 16467c478bd9Sstevel@tonic-gate return (Z_OK); 16477c478bd9Sstevel@tonic-gate } 16487c478bd9Sstevel@tonic-gate 16497c478bd9Sstevel@tonic-gate static int 16507c478bd9Sstevel@tonic-gate zonecfg_add_ipd_core(zone_dochandle_t handle, struct zone_fstab *tabptr) 16517c478bd9Sstevel@tonic-gate { 16527c478bd9Sstevel@tonic-gate xmlNodePtr newnode, cur = handle->zone_dh_cur; 16537c478bd9Sstevel@tonic-gate int err; 16547c478bd9Sstevel@tonic-gate 16557c478bd9Sstevel@tonic-gate newnode = xmlNewTextChild(cur, NULL, DTD_ELEM_IPD, NULL); 1656a1be23daSdp if ((err = newprop(newnode, DTD_ATTR_DIR, tabptr->zone_fs_dir)) != Z_OK) 16577c478bd9Sstevel@tonic-gate return (err); 16587c478bd9Sstevel@tonic-gate return (Z_OK); 16597c478bd9Sstevel@tonic-gate } 16607c478bd9Sstevel@tonic-gate 16617c478bd9Sstevel@tonic-gate int 16627c478bd9Sstevel@tonic-gate zonecfg_add_ipd(zone_dochandle_t handle, struct zone_fstab *tabptr) 16637c478bd9Sstevel@tonic-gate { 16647c478bd9Sstevel@tonic-gate int err; 16657c478bd9Sstevel@tonic-gate 16667c478bd9Sstevel@tonic-gate if (tabptr == NULL) 16677c478bd9Sstevel@tonic-gate return (Z_INVAL); 16687c478bd9Sstevel@tonic-gate 16697c478bd9Sstevel@tonic-gate if ((err = operation_prep(handle)) != Z_OK) 16707c478bd9Sstevel@tonic-gate return (err); 16717c478bd9Sstevel@tonic-gate 16727c478bd9Sstevel@tonic-gate if ((err = zonecfg_add_ipd_core(handle, tabptr)) != Z_OK) 16737c478bd9Sstevel@tonic-gate return (err); 16747c478bd9Sstevel@tonic-gate 16757c478bd9Sstevel@tonic-gate return (Z_OK); 16767c478bd9Sstevel@tonic-gate } 16777c478bd9Sstevel@tonic-gate 16787c478bd9Sstevel@tonic-gate int 16797c478bd9Sstevel@tonic-gate zonecfg_add_fs_option(struct zone_fstab *tabptr, char *option) 16807c478bd9Sstevel@tonic-gate { 16817c478bd9Sstevel@tonic-gate zone_fsopt_t *last, *old, *new; 16827c478bd9Sstevel@tonic-gate 16837c478bd9Sstevel@tonic-gate last = tabptr->zone_fs_options; 16847c478bd9Sstevel@tonic-gate for (old = last; old != NULL; old = old->zone_fsopt_next) 16857c478bd9Sstevel@tonic-gate last = old; /* walk to the end of the list */ 16867c478bd9Sstevel@tonic-gate new = (zone_fsopt_t *)malloc(sizeof (zone_fsopt_t)); 16877c478bd9Sstevel@tonic-gate if (new == NULL) 16887c478bd9Sstevel@tonic-gate return (Z_NOMEM); 16897c478bd9Sstevel@tonic-gate (void) strlcpy(new->zone_fsopt_opt, option, 16907c478bd9Sstevel@tonic-gate sizeof (new->zone_fsopt_opt)); 16917c478bd9Sstevel@tonic-gate new->zone_fsopt_next = NULL; 16927c478bd9Sstevel@tonic-gate if (last == NULL) 16937c478bd9Sstevel@tonic-gate tabptr->zone_fs_options = new; 16947c478bd9Sstevel@tonic-gate else 16957c478bd9Sstevel@tonic-gate last->zone_fsopt_next = new; 16967c478bd9Sstevel@tonic-gate return (Z_OK); 16977c478bd9Sstevel@tonic-gate } 16987c478bd9Sstevel@tonic-gate 16997c478bd9Sstevel@tonic-gate int 17007c478bd9Sstevel@tonic-gate zonecfg_remove_fs_option(struct zone_fstab *tabptr, char *option) 17017c478bd9Sstevel@tonic-gate { 17027c478bd9Sstevel@tonic-gate zone_fsopt_t *last, *this, *next; 17037c478bd9Sstevel@tonic-gate 17047c478bd9Sstevel@tonic-gate last = tabptr->zone_fs_options; 17057c478bd9Sstevel@tonic-gate for (this = last; this != NULL; this = this->zone_fsopt_next) { 17067c478bd9Sstevel@tonic-gate if (strcmp(this->zone_fsopt_opt, option) == 0) { 17077c478bd9Sstevel@tonic-gate next = this->zone_fsopt_next; 17087c478bd9Sstevel@tonic-gate if (this == tabptr->zone_fs_options) 17097c478bd9Sstevel@tonic-gate tabptr->zone_fs_options = next; 17107c478bd9Sstevel@tonic-gate else 17117c478bd9Sstevel@tonic-gate last->zone_fsopt_next = next; 17127c478bd9Sstevel@tonic-gate free(this); 17137c478bd9Sstevel@tonic-gate return (Z_OK); 17147c478bd9Sstevel@tonic-gate } else 17157c478bd9Sstevel@tonic-gate last = this; 17167c478bd9Sstevel@tonic-gate } 17177c478bd9Sstevel@tonic-gate return (Z_NO_PROPERTY_ID); 17187c478bd9Sstevel@tonic-gate } 17197c478bd9Sstevel@tonic-gate 17207c478bd9Sstevel@tonic-gate void 17217c478bd9Sstevel@tonic-gate zonecfg_free_fs_option_list(zone_fsopt_t *list) 17227c478bd9Sstevel@tonic-gate { 17237c478bd9Sstevel@tonic-gate zone_fsopt_t *this, *next; 17247c478bd9Sstevel@tonic-gate 17257c478bd9Sstevel@tonic-gate for (this = list; this != NULL; this = next) { 17267c478bd9Sstevel@tonic-gate next = this->zone_fsopt_next; 17277c478bd9Sstevel@tonic-gate free(this); 17287c478bd9Sstevel@tonic-gate } 17297c478bd9Sstevel@tonic-gate } 17307c478bd9Sstevel@tonic-gate 17317c478bd9Sstevel@tonic-gate void 17327c478bd9Sstevel@tonic-gate zonecfg_free_rctl_value_list(struct zone_rctlvaltab *valtab) 17337c478bd9Sstevel@tonic-gate { 17347c478bd9Sstevel@tonic-gate if (valtab == NULL) 17357c478bd9Sstevel@tonic-gate return; 17367c478bd9Sstevel@tonic-gate zonecfg_free_rctl_value_list(valtab->zone_rctlval_next); 17377c478bd9Sstevel@tonic-gate free(valtab); 17387c478bd9Sstevel@tonic-gate } 17397c478bd9Sstevel@tonic-gate 17407c478bd9Sstevel@tonic-gate static boolean_t 17417c478bd9Sstevel@tonic-gate match_prop(xmlNodePtr cur, const xmlChar *attr, char *user_prop) 17427c478bd9Sstevel@tonic-gate { 17437c478bd9Sstevel@tonic-gate xmlChar *gotten_prop; 17447c478bd9Sstevel@tonic-gate int prop_result; 17457c478bd9Sstevel@tonic-gate 17467c478bd9Sstevel@tonic-gate gotten_prop = xmlGetProp(cur, attr); 17477c478bd9Sstevel@tonic-gate if (gotten_prop == NULL) /* shouldn't happen */ 17487c478bd9Sstevel@tonic-gate return (B_FALSE); 17497c478bd9Sstevel@tonic-gate prop_result = xmlStrcmp(gotten_prop, (const xmlChar *) user_prop); 17507c478bd9Sstevel@tonic-gate xmlFree(gotten_prop); 17517c478bd9Sstevel@tonic-gate return ((prop_result == 0)); 17527c478bd9Sstevel@tonic-gate } 17537c478bd9Sstevel@tonic-gate 17547c478bd9Sstevel@tonic-gate static int 17557c478bd9Sstevel@tonic-gate zonecfg_delete_filesystem_core(zone_dochandle_t handle, 17567c478bd9Sstevel@tonic-gate struct zone_fstab *tabptr) 17577c478bd9Sstevel@tonic-gate { 17587c478bd9Sstevel@tonic-gate xmlNodePtr cur = handle->zone_dh_cur; 17597c478bd9Sstevel@tonic-gate boolean_t dir_match, spec_match, raw_match, type_match; 17607c478bd9Sstevel@tonic-gate 17617c478bd9Sstevel@tonic-gate for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) { 17627c478bd9Sstevel@tonic-gate if (xmlStrcmp(cur->name, DTD_ELEM_FS)) 17637c478bd9Sstevel@tonic-gate continue; 17647c478bd9Sstevel@tonic-gate dir_match = match_prop(cur, DTD_ATTR_DIR, tabptr->zone_fs_dir); 17657c478bd9Sstevel@tonic-gate spec_match = match_prop(cur, DTD_ATTR_SPECIAL, 17667c478bd9Sstevel@tonic-gate tabptr->zone_fs_special); 17677c478bd9Sstevel@tonic-gate raw_match = match_prop(cur, DTD_ATTR_RAW, 17687c478bd9Sstevel@tonic-gate tabptr->zone_fs_raw); 17697c478bd9Sstevel@tonic-gate type_match = match_prop(cur, DTD_ATTR_TYPE, 17707c478bd9Sstevel@tonic-gate tabptr->zone_fs_type); 17717c478bd9Sstevel@tonic-gate if (dir_match && spec_match && raw_match && type_match) { 17727c478bd9Sstevel@tonic-gate xmlUnlinkNode(cur); 17737c478bd9Sstevel@tonic-gate xmlFreeNode(cur); 17747c478bd9Sstevel@tonic-gate return (Z_OK); 17757c478bd9Sstevel@tonic-gate } 17767c478bd9Sstevel@tonic-gate } 17777c478bd9Sstevel@tonic-gate return (Z_NO_RESOURCE_ID); 17787c478bd9Sstevel@tonic-gate } 17797c478bd9Sstevel@tonic-gate 17807c478bd9Sstevel@tonic-gate int 17817c478bd9Sstevel@tonic-gate zonecfg_delete_filesystem(zone_dochandle_t handle, struct zone_fstab *tabptr) 17827c478bd9Sstevel@tonic-gate { 17837c478bd9Sstevel@tonic-gate int err; 17847c478bd9Sstevel@tonic-gate 17857c478bd9Sstevel@tonic-gate if (tabptr == NULL) 17867c478bd9Sstevel@tonic-gate return (Z_INVAL); 17877c478bd9Sstevel@tonic-gate 17887c478bd9Sstevel@tonic-gate if ((err = operation_prep(handle)) != Z_OK) 17897c478bd9Sstevel@tonic-gate return (err); 17907c478bd9Sstevel@tonic-gate 17917c478bd9Sstevel@tonic-gate if ((err = zonecfg_delete_filesystem_core(handle, tabptr)) != Z_OK) 17927c478bd9Sstevel@tonic-gate return (err); 17937c478bd9Sstevel@tonic-gate 17947c478bd9Sstevel@tonic-gate return (Z_OK); 17957c478bd9Sstevel@tonic-gate } 17967c478bd9Sstevel@tonic-gate 17977c478bd9Sstevel@tonic-gate int 17987c478bd9Sstevel@tonic-gate zonecfg_modify_filesystem( 17997c478bd9Sstevel@tonic-gate zone_dochandle_t handle, 18007c478bd9Sstevel@tonic-gate struct zone_fstab *oldtabptr, 18017c478bd9Sstevel@tonic-gate struct zone_fstab *newtabptr) 18027c478bd9Sstevel@tonic-gate { 18037c478bd9Sstevel@tonic-gate int err; 18047c478bd9Sstevel@tonic-gate 18057c478bd9Sstevel@tonic-gate if (oldtabptr == NULL || newtabptr == NULL) 18067c478bd9Sstevel@tonic-gate return (Z_INVAL); 18077c478bd9Sstevel@tonic-gate 18087c478bd9Sstevel@tonic-gate if ((err = operation_prep(handle)) != Z_OK) 18097c478bd9Sstevel@tonic-gate return (err); 18107c478bd9Sstevel@tonic-gate 18117c478bd9Sstevel@tonic-gate if ((err = zonecfg_delete_filesystem_core(handle, oldtabptr)) != Z_OK) 18127c478bd9Sstevel@tonic-gate return (err); 18137c478bd9Sstevel@tonic-gate 18147c478bd9Sstevel@tonic-gate if ((err = zonecfg_add_filesystem_core(handle, newtabptr)) != Z_OK) 18157c478bd9Sstevel@tonic-gate return (err); 18167c478bd9Sstevel@tonic-gate 18177c478bd9Sstevel@tonic-gate return (Z_OK); 18187c478bd9Sstevel@tonic-gate } 18197c478bd9Sstevel@tonic-gate 18207c478bd9Sstevel@tonic-gate static int 18217c478bd9Sstevel@tonic-gate zonecfg_delete_ipd_core(zone_dochandle_t handle, struct zone_fstab *tabptr) 18227c478bd9Sstevel@tonic-gate { 18237c478bd9Sstevel@tonic-gate xmlNodePtr cur = handle->zone_dh_cur; 18247c478bd9Sstevel@tonic-gate 18257c478bd9Sstevel@tonic-gate for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) { 18267c478bd9Sstevel@tonic-gate if (xmlStrcmp(cur->name, DTD_ELEM_IPD)) 18277c478bd9Sstevel@tonic-gate continue; 18287c478bd9Sstevel@tonic-gate if (match_prop(cur, DTD_ATTR_DIR, tabptr->zone_fs_dir)) { 18297c478bd9Sstevel@tonic-gate xmlUnlinkNode(cur); 18307c478bd9Sstevel@tonic-gate xmlFreeNode(cur); 18317c478bd9Sstevel@tonic-gate return (Z_OK); 18327c478bd9Sstevel@tonic-gate } 18337c478bd9Sstevel@tonic-gate } 18347c478bd9Sstevel@tonic-gate return (Z_NO_RESOURCE_ID); 18357c478bd9Sstevel@tonic-gate } 18367c478bd9Sstevel@tonic-gate 18377c478bd9Sstevel@tonic-gate int 18387c478bd9Sstevel@tonic-gate zonecfg_delete_ipd(zone_dochandle_t handle, struct zone_fstab *tabptr) 18397c478bd9Sstevel@tonic-gate { 18407c478bd9Sstevel@tonic-gate int err; 18417c478bd9Sstevel@tonic-gate 18427c478bd9Sstevel@tonic-gate if (tabptr == 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, tabptr)) != Z_OK) 18497c478bd9Sstevel@tonic-gate return (err); 18507c478bd9Sstevel@tonic-gate 18517c478bd9Sstevel@tonic-gate return (Z_OK); 18527c478bd9Sstevel@tonic-gate } 18537c478bd9Sstevel@tonic-gate 18547c478bd9Sstevel@tonic-gate int 18557c478bd9Sstevel@tonic-gate zonecfg_modify_ipd(zone_dochandle_t handle, struct zone_fstab *oldtabptr, 18567c478bd9Sstevel@tonic-gate struct zone_fstab *newtabptr) 18577c478bd9Sstevel@tonic-gate { 18587c478bd9Sstevel@tonic-gate int err; 18597c478bd9Sstevel@tonic-gate 18607c478bd9Sstevel@tonic-gate if (oldtabptr == NULL || newtabptr == NULL) 18617c478bd9Sstevel@tonic-gate return (Z_INVAL); 18627c478bd9Sstevel@tonic-gate 18637c478bd9Sstevel@tonic-gate if ((err = operation_prep(handle)) != Z_OK) 18647c478bd9Sstevel@tonic-gate return (err); 18657c478bd9Sstevel@tonic-gate 18667c478bd9Sstevel@tonic-gate if ((err = zonecfg_delete_ipd_core(handle, oldtabptr)) != Z_OK) 18677c478bd9Sstevel@tonic-gate return (err); 18687c478bd9Sstevel@tonic-gate 18697c478bd9Sstevel@tonic-gate if ((err = zonecfg_add_ipd_core(handle, newtabptr)) != Z_OK) 18707c478bd9Sstevel@tonic-gate return (err); 18717c478bd9Sstevel@tonic-gate 18727c478bd9Sstevel@tonic-gate return (Z_OK); 18737c478bd9Sstevel@tonic-gate } 18747c478bd9Sstevel@tonic-gate 18757c478bd9Sstevel@tonic-gate int 18767c478bd9Sstevel@tonic-gate zonecfg_lookup_filesystem( 18777c478bd9Sstevel@tonic-gate zone_dochandle_t handle, 18787c478bd9Sstevel@tonic-gate struct zone_fstab *tabptr) 18797c478bd9Sstevel@tonic-gate { 18807c478bd9Sstevel@tonic-gate xmlNodePtr cur, options, firstmatch; 18817c478bd9Sstevel@tonic-gate int err; 18827c478bd9Sstevel@tonic-gate char dirname[MAXPATHLEN], special[MAXPATHLEN], raw[MAXPATHLEN]; 18837c478bd9Sstevel@tonic-gate char type[FSTYPSZ]; 18847c478bd9Sstevel@tonic-gate char options_str[MAX_MNTOPT_STR]; 18857c478bd9Sstevel@tonic-gate 18867c478bd9Sstevel@tonic-gate if (tabptr == NULL) 18877c478bd9Sstevel@tonic-gate return (Z_INVAL); 18887c478bd9Sstevel@tonic-gate 18897c478bd9Sstevel@tonic-gate if ((err = operation_prep(handle)) != Z_OK) 18907c478bd9Sstevel@tonic-gate return (err); 18917c478bd9Sstevel@tonic-gate 18927c478bd9Sstevel@tonic-gate /* 18937c478bd9Sstevel@tonic-gate * Walk the list of children looking for matches on any properties 18947c478bd9Sstevel@tonic-gate * specified in the fstab parameter. If more than one resource 18957c478bd9Sstevel@tonic-gate * matches, we return Z_INSUFFICIENT_SPEC; if none match, we return 18967c478bd9Sstevel@tonic-gate * Z_NO_RESOURCE_ID. 18977c478bd9Sstevel@tonic-gate */ 18987c478bd9Sstevel@tonic-gate cur = handle->zone_dh_cur; 18997c478bd9Sstevel@tonic-gate firstmatch = NULL; 19007c478bd9Sstevel@tonic-gate for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) { 19017c478bd9Sstevel@tonic-gate if (xmlStrcmp(cur->name, DTD_ELEM_FS)) 19027c478bd9Sstevel@tonic-gate continue; 19037c478bd9Sstevel@tonic-gate if (strlen(tabptr->zone_fs_dir) > 0) { 19047c478bd9Sstevel@tonic-gate if ((fetchprop(cur, DTD_ATTR_DIR, dirname, 19057c478bd9Sstevel@tonic-gate sizeof (dirname)) == Z_OK) && 19067c478bd9Sstevel@tonic-gate (strcmp(tabptr->zone_fs_dir, dirname) == 0)) { 19077c478bd9Sstevel@tonic-gate if (firstmatch == NULL) 19087c478bd9Sstevel@tonic-gate firstmatch = cur; 19097c478bd9Sstevel@tonic-gate else 19107c478bd9Sstevel@tonic-gate return (Z_INSUFFICIENT_SPEC); 19117c478bd9Sstevel@tonic-gate } 19127c478bd9Sstevel@tonic-gate } 19137c478bd9Sstevel@tonic-gate if (strlen(tabptr->zone_fs_special) > 0) { 19147c478bd9Sstevel@tonic-gate if ((fetchprop(cur, DTD_ATTR_SPECIAL, special, 19157c478bd9Sstevel@tonic-gate sizeof (special)) == Z_OK)) { 19167c478bd9Sstevel@tonic-gate if (strcmp(tabptr->zone_fs_special, 19177c478bd9Sstevel@tonic-gate special) == 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_raw) > 0) { 19337c478bd9Sstevel@tonic-gate if ((fetchprop(cur, DTD_ATTR_RAW, raw, 19347c478bd9Sstevel@tonic-gate sizeof (raw)) == Z_OK)) { 19357c478bd9Sstevel@tonic-gate if (strcmp(tabptr->zone_fs_raw, raw) == 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 if (strlen(tabptr->zone_fs_type) > 0) { 19517c478bd9Sstevel@tonic-gate if ((fetchprop(cur, DTD_ATTR_TYPE, type, 19527c478bd9Sstevel@tonic-gate sizeof (type)) == Z_OK)) { 19537c478bd9Sstevel@tonic-gate if (strcmp(tabptr->zone_fs_type, type) == 0) { 19547c478bd9Sstevel@tonic-gate if (firstmatch == NULL) 19557c478bd9Sstevel@tonic-gate firstmatch = cur; 19567c478bd9Sstevel@tonic-gate else if (firstmatch != cur) 19577c478bd9Sstevel@tonic-gate return (Z_INSUFFICIENT_SPEC); 19587c478bd9Sstevel@tonic-gate } else { 19597c478bd9Sstevel@tonic-gate /* 19607c478bd9Sstevel@tonic-gate * If another property matched but this 19617c478bd9Sstevel@tonic-gate * one doesn't then reset firstmatch. 19627c478bd9Sstevel@tonic-gate */ 19637c478bd9Sstevel@tonic-gate if (firstmatch == cur) 19647c478bd9Sstevel@tonic-gate firstmatch = NULL; 19657c478bd9Sstevel@tonic-gate } 19667c478bd9Sstevel@tonic-gate } 19677c478bd9Sstevel@tonic-gate } 19687c478bd9Sstevel@tonic-gate } 19697c478bd9Sstevel@tonic-gate 19707c478bd9Sstevel@tonic-gate if (firstmatch == NULL) 19717c478bd9Sstevel@tonic-gate return (Z_NO_RESOURCE_ID); 19727c478bd9Sstevel@tonic-gate 19737c478bd9Sstevel@tonic-gate cur = firstmatch; 19747c478bd9Sstevel@tonic-gate 19757c478bd9Sstevel@tonic-gate if ((err = fetchprop(cur, DTD_ATTR_DIR, tabptr->zone_fs_dir, 19767c478bd9Sstevel@tonic-gate sizeof (tabptr->zone_fs_dir))) != Z_OK) 19777c478bd9Sstevel@tonic-gate return (err); 19787c478bd9Sstevel@tonic-gate 19797c478bd9Sstevel@tonic-gate if ((err = fetchprop(cur, DTD_ATTR_SPECIAL, tabptr->zone_fs_special, 19807c478bd9Sstevel@tonic-gate sizeof (tabptr->zone_fs_special))) != Z_OK) 19817c478bd9Sstevel@tonic-gate return (err); 19827c478bd9Sstevel@tonic-gate 19837c478bd9Sstevel@tonic-gate if ((err = fetchprop(cur, DTD_ATTR_RAW, tabptr->zone_fs_raw, 19847c478bd9Sstevel@tonic-gate sizeof (tabptr->zone_fs_raw))) != Z_OK) 19857c478bd9Sstevel@tonic-gate return (err); 19867c478bd9Sstevel@tonic-gate 19877c478bd9Sstevel@tonic-gate if ((err = fetchprop(cur, DTD_ATTR_TYPE, tabptr->zone_fs_type, 19887c478bd9Sstevel@tonic-gate sizeof (tabptr->zone_fs_type))) != Z_OK) 19897c478bd9Sstevel@tonic-gate return (err); 19907c478bd9Sstevel@tonic-gate 19917c478bd9Sstevel@tonic-gate /* options are optional */ 19927c478bd9Sstevel@tonic-gate tabptr->zone_fs_options = NULL; 19937c478bd9Sstevel@tonic-gate for (options = cur->xmlChildrenNode; options != NULL; 19947c478bd9Sstevel@tonic-gate options = options->next) { 19957c478bd9Sstevel@tonic-gate if ((fetchprop(options, DTD_ATTR_NAME, options_str, 19967c478bd9Sstevel@tonic-gate sizeof (options_str)) != Z_OK)) 19977c478bd9Sstevel@tonic-gate break; 19987c478bd9Sstevel@tonic-gate if (zonecfg_add_fs_option(tabptr, options_str) != Z_OK) 19997c478bd9Sstevel@tonic-gate break; 20007c478bd9Sstevel@tonic-gate } 20017c478bd9Sstevel@tonic-gate return (Z_OK); 20027c478bd9Sstevel@tonic-gate } 20037c478bd9Sstevel@tonic-gate 20047c478bd9Sstevel@tonic-gate int 20057c478bd9Sstevel@tonic-gate zonecfg_lookup_ipd(zone_dochandle_t handle, struct zone_fstab *tabptr) 20067c478bd9Sstevel@tonic-gate { 20077c478bd9Sstevel@tonic-gate xmlNodePtr cur, match; 20087c478bd9Sstevel@tonic-gate int err; 20097c478bd9Sstevel@tonic-gate char dirname[MAXPATHLEN]; 20107c478bd9Sstevel@tonic-gate 20117c478bd9Sstevel@tonic-gate if (tabptr == NULL) 20127c478bd9Sstevel@tonic-gate return (Z_INVAL); 20137c478bd9Sstevel@tonic-gate 20147c478bd9Sstevel@tonic-gate if ((err = operation_prep(handle)) != Z_OK) 20157c478bd9Sstevel@tonic-gate return (err); 20167c478bd9Sstevel@tonic-gate 20177c478bd9Sstevel@tonic-gate /* 20187c478bd9Sstevel@tonic-gate * General algorithm: 20197c478bd9Sstevel@tonic-gate * Walk the list of children looking for matches on any properties 20207c478bd9Sstevel@tonic-gate * specified in the fstab parameter. If more than one resource 20217c478bd9Sstevel@tonic-gate * matches, we return Z_INSUFFICIENT_SPEC; if none match, we return 20227c478bd9Sstevel@tonic-gate * Z_NO_RESOURCE_ID. 20237c478bd9Sstevel@tonic-gate */ 20247c478bd9Sstevel@tonic-gate cur = handle->zone_dh_cur; 20257c478bd9Sstevel@tonic-gate match = NULL; 20267c478bd9Sstevel@tonic-gate for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) { 20277c478bd9Sstevel@tonic-gate if (xmlStrcmp(cur->name, DTD_ELEM_IPD)) 20287c478bd9Sstevel@tonic-gate continue; 20297c478bd9Sstevel@tonic-gate if (strlen(tabptr->zone_fs_dir) > 0) { 20307c478bd9Sstevel@tonic-gate if ((fetchprop(cur, DTD_ATTR_DIR, dirname, 20317c478bd9Sstevel@tonic-gate sizeof (dirname)) == Z_OK) && 20327c478bd9Sstevel@tonic-gate (strcmp(tabptr->zone_fs_dir, dirname) == 0)) { 20337c478bd9Sstevel@tonic-gate if (match == NULL) 20347c478bd9Sstevel@tonic-gate match = cur; 20357c478bd9Sstevel@tonic-gate else 20367c478bd9Sstevel@tonic-gate return (Z_INSUFFICIENT_SPEC); 20377c478bd9Sstevel@tonic-gate } 20387c478bd9Sstevel@tonic-gate } 20397c478bd9Sstevel@tonic-gate } 20407c478bd9Sstevel@tonic-gate 20417c478bd9Sstevel@tonic-gate if (match == NULL) 20427c478bd9Sstevel@tonic-gate return (Z_NO_RESOURCE_ID); 20437c478bd9Sstevel@tonic-gate 20447c478bd9Sstevel@tonic-gate cur = match; 20457c478bd9Sstevel@tonic-gate 20467c478bd9Sstevel@tonic-gate if ((err = fetchprop(cur, DTD_ATTR_DIR, tabptr->zone_fs_dir, 20477c478bd9Sstevel@tonic-gate sizeof (tabptr->zone_fs_dir))) != Z_OK) 20487c478bd9Sstevel@tonic-gate return (err); 20497c478bd9Sstevel@tonic-gate 20507c478bd9Sstevel@tonic-gate return (Z_OK); 20517c478bd9Sstevel@tonic-gate } 20527c478bd9Sstevel@tonic-gate 20537c478bd9Sstevel@tonic-gate /* 20547c478bd9Sstevel@tonic-gate * Compare two IP addresses in string form. Allow for the possibility that 20557c478bd9Sstevel@tonic-gate * one might have "/<prefix-length>" at the end: allow a match on just the 20567c478bd9Sstevel@tonic-gate * IP address (or host name) part. 20577c478bd9Sstevel@tonic-gate */ 20587c478bd9Sstevel@tonic-gate 20597c478bd9Sstevel@tonic-gate boolean_t 20607c478bd9Sstevel@tonic-gate zonecfg_same_net_address(char *a1, char *a2) 20617c478bd9Sstevel@tonic-gate { 20627c478bd9Sstevel@tonic-gate char *slashp, *slashp1, *slashp2; 20637c478bd9Sstevel@tonic-gate int result; 20647c478bd9Sstevel@tonic-gate 20657c478bd9Sstevel@tonic-gate if (strcmp(a1, a2) == 0) 20667c478bd9Sstevel@tonic-gate return (B_TRUE); 20677c478bd9Sstevel@tonic-gate 20687c478bd9Sstevel@tonic-gate /* 20697c478bd9Sstevel@tonic-gate * If neither has a slash or both do, they need to match to be 20707c478bd9Sstevel@tonic-gate * considered the same, but they did not match above, so fail. 20717c478bd9Sstevel@tonic-gate */ 20727c478bd9Sstevel@tonic-gate slashp1 = strchr(a1, '/'); 20737c478bd9Sstevel@tonic-gate slashp2 = strchr(a2, '/'); 20747c478bd9Sstevel@tonic-gate if ((slashp1 == NULL && slashp2 == NULL) || 20757c478bd9Sstevel@tonic-gate (slashp1 != NULL && slashp2 != NULL)) 20767c478bd9Sstevel@tonic-gate return (B_FALSE); 20777c478bd9Sstevel@tonic-gate 20787c478bd9Sstevel@tonic-gate /* 20797c478bd9Sstevel@tonic-gate * Only one had a slash: pick that one, zero out the slash, compare 20807c478bd9Sstevel@tonic-gate * the "address only" strings, restore the slash, and return the 20817c478bd9Sstevel@tonic-gate * result of the comparison. 20827c478bd9Sstevel@tonic-gate */ 20837c478bd9Sstevel@tonic-gate slashp = (slashp1 == NULL) ? slashp2 : slashp1; 20847c478bd9Sstevel@tonic-gate *slashp = '\0'; 20857c478bd9Sstevel@tonic-gate result = strcmp(a1, a2); 20867c478bd9Sstevel@tonic-gate *slashp = '/'; 20877c478bd9Sstevel@tonic-gate return ((result == 0)); 20887c478bd9Sstevel@tonic-gate } 20897c478bd9Sstevel@tonic-gate 20907c478bd9Sstevel@tonic-gate int 20917c478bd9Sstevel@tonic-gate zonecfg_valid_net_address(char *address, struct lifreq *lifr) 20927c478bd9Sstevel@tonic-gate { 20937c478bd9Sstevel@tonic-gate struct sockaddr_in *sin4; 20947c478bd9Sstevel@tonic-gate struct sockaddr_in6 *sin6; 20957c478bd9Sstevel@tonic-gate struct addrinfo hints, *result; 20967c478bd9Sstevel@tonic-gate char *slashp = strchr(address, '/'); 20977c478bd9Sstevel@tonic-gate 20987c478bd9Sstevel@tonic-gate bzero(lifr, sizeof (struct lifreq)); 20997c478bd9Sstevel@tonic-gate sin4 = (struct sockaddr_in *)&lifr->lifr_addr; 21007c478bd9Sstevel@tonic-gate sin6 = (struct sockaddr_in6 *)&lifr->lifr_addr; 21017c478bd9Sstevel@tonic-gate if (slashp != NULL) 21027c478bd9Sstevel@tonic-gate *slashp = '\0'; 21037c478bd9Sstevel@tonic-gate if (inet_pton(AF_INET, address, &sin4->sin_addr) == 1) { 21047c478bd9Sstevel@tonic-gate sin4->sin_family = AF_INET; 21057c478bd9Sstevel@tonic-gate } else if (inet_pton(AF_INET6, address, &sin6->sin6_addr) == 1) { 21067c478bd9Sstevel@tonic-gate if (slashp == NULL) 21077c478bd9Sstevel@tonic-gate return (Z_IPV6_ADDR_PREFIX_LEN); 21087c478bd9Sstevel@tonic-gate sin6->sin6_family = AF_INET6; 21097c478bd9Sstevel@tonic-gate } else { 21107c478bd9Sstevel@tonic-gate /* "address" may be a host name */ 21117c478bd9Sstevel@tonic-gate (void) memset(&hints, 0, sizeof (hints)); 21127c478bd9Sstevel@tonic-gate hints.ai_family = PF_INET; 21137c478bd9Sstevel@tonic-gate if (getaddrinfo(address, NULL, &hints, &result) != 0) 21147c478bd9Sstevel@tonic-gate return (Z_BOGUS_ADDRESS); 21157c478bd9Sstevel@tonic-gate sin4->sin_family = result->ai_family; 2116a1be23daSdp 21177c478bd9Sstevel@tonic-gate (void) memcpy(&sin4->sin_addr, 21187c478bd9Sstevel@tonic-gate /* LINTED E_BAD_PTR_CAST_ALIGN */ 21197c478bd9Sstevel@tonic-gate &((struct sockaddr_in *)result->ai_addr)->sin_addr, 21207c478bd9Sstevel@tonic-gate sizeof (struct in_addr)); 2121a1be23daSdp 21227c478bd9Sstevel@tonic-gate freeaddrinfo(result); 21237c478bd9Sstevel@tonic-gate } 21247c478bd9Sstevel@tonic-gate return (Z_OK); 21257c478bd9Sstevel@tonic-gate } 21267c478bd9Sstevel@tonic-gate 2127f4b3ec61Sdh155122 boolean_t 2128f4b3ec61Sdh155122 zonecfg_ifname_exists(sa_family_t af, char *ifname) 2129f4b3ec61Sdh155122 { 2130f4b3ec61Sdh155122 struct lifreq lifr; 2131f4b3ec61Sdh155122 int so; 2132f4b3ec61Sdh155122 int save_errno; 2133f4b3ec61Sdh155122 2134f4b3ec61Sdh155122 (void) memset(&lifr, 0, sizeof (lifr)); 2135f4b3ec61Sdh155122 (void) strlcpy(lifr.lifr_name, ifname, sizeof (lifr.lifr_name)); 2136f4b3ec61Sdh155122 lifr.lifr_addr.ss_family = af; 2137f4b3ec61Sdh155122 if ((so = socket(af, SOCK_DGRAM, 0)) < 0) { 2138f4b3ec61Sdh155122 /* Odd - can't tell if the ifname exists */ 2139f4b3ec61Sdh155122 return (B_FALSE); 2140f4b3ec61Sdh155122 } 2141f4b3ec61Sdh155122 if (ioctl(so, SIOCGLIFFLAGS, (caddr_t)&lifr) < 0) { 2142f4b3ec61Sdh155122 save_errno = errno; 2143f4b3ec61Sdh155122 (void) close(so); 2144f4b3ec61Sdh155122 errno = save_errno; 2145f4b3ec61Sdh155122 return (B_FALSE); 2146f4b3ec61Sdh155122 } 2147f4b3ec61Sdh155122 (void) close(so); 2148f4b3ec61Sdh155122 return (B_TRUE); 2149f4b3ec61Sdh155122 } 2150f4b3ec61Sdh155122 21517c478bd9Sstevel@tonic-gate int 21527c478bd9Sstevel@tonic-gate zonecfg_lookup_nwif(zone_dochandle_t handle, struct zone_nwiftab *tabptr) 21537c478bd9Sstevel@tonic-gate { 21547c478bd9Sstevel@tonic-gate xmlNodePtr cur, firstmatch; 21557c478bd9Sstevel@tonic-gate int err; 21567c478bd9Sstevel@tonic-gate char address[INET6_ADDRSTRLEN], physical[LIFNAMSIZ]; 21577c478bd9Sstevel@tonic-gate 21587c478bd9Sstevel@tonic-gate if (tabptr == NULL) 21597c478bd9Sstevel@tonic-gate return (Z_INVAL); 21607c478bd9Sstevel@tonic-gate 21617c478bd9Sstevel@tonic-gate if ((err = operation_prep(handle)) != Z_OK) 21627c478bd9Sstevel@tonic-gate return (err); 21637c478bd9Sstevel@tonic-gate 21647c478bd9Sstevel@tonic-gate cur = handle->zone_dh_cur; 21657c478bd9Sstevel@tonic-gate firstmatch = NULL; 21667c478bd9Sstevel@tonic-gate for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) { 21677c478bd9Sstevel@tonic-gate if (xmlStrcmp(cur->name, DTD_ELEM_NET)) 21687c478bd9Sstevel@tonic-gate continue; 21697c478bd9Sstevel@tonic-gate if (strlen(tabptr->zone_nwif_physical) > 0) { 21707c478bd9Sstevel@tonic-gate if ((fetchprop(cur, DTD_ATTR_PHYSICAL, physical, 21717c478bd9Sstevel@tonic-gate sizeof (physical)) == Z_OK) && 21727c478bd9Sstevel@tonic-gate (strcmp(tabptr->zone_nwif_physical, 21737c478bd9Sstevel@tonic-gate physical) == 0)) { 21747c478bd9Sstevel@tonic-gate if (firstmatch == NULL) 21757c478bd9Sstevel@tonic-gate firstmatch = cur; 21767c478bd9Sstevel@tonic-gate else 21777c478bd9Sstevel@tonic-gate return (Z_INSUFFICIENT_SPEC); 21787c478bd9Sstevel@tonic-gate } 21797c478bd9Sstevel@tonic-gate } 21807c478bd9Sstevel@tonic-gate if (strlen(tabptr->zone_nwif_address) > 0) { 21817c478bd9Sstevel@tonic-gate if ((fetchprop(cur, DTD_ATTR_ADDRESS, address, 21827c478bd9Sstevel@tonic-gate sizeof (address)) == Z_OK)) { 21837c478bd9Sstevel@tonic-gate if (zonecfg_same_net_address( 21847c478bd9Sstevel@tonic-gate tabptr->zone_nwif_address, address)) { 21857c478bd9Sstevel@tonic-gate if (firstmatch == NULL) 21867c478bd9Sstevel@tonic-gate firstmatch = cur; 21877c478bd9Sstevel@tonic-gate else if (firstmatch != cur) 21887c478bd9Sstevel@tonic-gate return (Z_INSUFFICIENT_SPEC); 21897c478bd9Sstevel@tonic-gate } else { 21907c478bd9Sstevel@tonic-gate /* 21917c478bd9Sstevel@tonic-gate * If another property matched but this 21927c478bd9Sstevel@tonic-gate * one doesn't then reset firstmatch. 21937c478bd9Sstevel@tonic-gate */ 21947c478bd9Sstevel@tonic-gate if (firstmatch == cur) 21957c478bd9Sstevel@tonic-gate firstmatch = NULL; 21967c478bd9Sstevel@tonic-gate } 21977c478bd9Sstevel@tonic-gate } 21987c478bd9Sstevel@tonic-gate } 21997c478bd9Sstevel@tonic-gate } 22007c478bd9Sstevel@tonic-gate if (firstmatch == NULL) 22017c478bd9Sstevel@tonic-gate return (Z_NO_RESOURCE_ID); 22027c478bd9Sstevel@tonic-gate 22037c478bd9Sstevel@tonic-gate cur = firstmatch; 22047c478bd9Sstevel@tonic-gate 22057c478bd9Sstevel@tonic-gate if ((err = fetchprop(cur, DTD_ATTR_PHYSICAL, tabptr->zone_nwif_physical, 22067c478bd9Sstevel@tonic-gate sizeof (tabptr->zone_nwif_physical))) != Z_OK) 22077c478bd9Sstevel@tonic-gate return (err); 22087c478bd9Sstevel@tonic-gate 22097c478bd9Sstevel@tonic-gate if ((err = fetchprop(cur, DTD_ATTR_ADDRESS, tabptr->zone_nwif_address, 22107c478bd9Sstevel@tonic-gate sizeof (tabptr->zone_nwif_address))) != Z_OK) 22117c478bd9Sstevel@tonic-gate return (err); 22127c478bd9Sstevel@tonic-gate 2213de860bd9Sgfaden if ((err = fetchprop(cur, DTD_ATTR_DEFROUTER, 2214de860bd9Sgfaden tabptr->zone_nwif_defrouter, 2215de860bd9Sgfaden sizeof (tabptr->zone_nwif_defrouter))) != Z_OK) 2216de860bd9Sgfaden return (err); 2217de860bd9Sgfaden 22187c478bd9Sstevel@tonic-gate return (Z_OK); 22197c478bd9Sstevel@tonic-gate } 22207c478bd9Sstevel@tonic-gate 22217c478bd9Sstevel@tonic-gate static int 22227c478bd9Sstevel@tonic-gate zonecfg_add_nwif_core(zone_dochandle_t handle, struct zone_nwiftab *tabptr) 22237c478bd9Sstevel@tonic-gate { 22247c478bd9Sstevel@tonic-gate xmlNodePtr newnode, cur = handle->zone_dh_cur; 22257c478bd9Sstevel@tonic-gate int err; 22267c478bd9Sstevel@tonic-gate 22277c478bd9Sstevel@tonic-gate newnode = xmlNewTextChild(cur, NULL, DTD_ELEM_NET, NULL); 2228a1be23daSdp if ((err = newprop(newnode, DTD_ATTR_ADDRESS, 22297c478bd9Sstevel@tonic-gate tabptr->zone_nwif_address)) != Z_OK) 22307c478bd9Sstevel@tonic-gate return (err); 2231a1be23daSdp if ((err = newprop(newnode, DTD_ATTR_PHYSICAL, 22327c478bd9Sstevel@tonic-gate tabptr->zone_nwif_physical)) != Z_OK) 22337c478bd9Sstevel@tonic-gate return (err); 223444ce818bSjparcel /* 223544ce818bSjparcel * Do not add this property when it is not set, for backwards 223644ce818bSjparcel * compatibility and because it is optional. 223744ce818bSjparcel */ 223844ce818bSjparcel if ((strlen(tabptr->zone_nwif_defrouter) > 0) && 223944ce818bSjparcel ((err = newprop(newnode, DTD_ATTR_DEFROUTER, 224044ce818bSjparcel tabptr->zone_nwif_defrouter)) != Z_OK)) 2241de860bd9Sgfaden return (err); 22427c478bd9Sstevel@tonic-gate return (Z_OK); 22437c478bd9Sstevel@tonic-gate } 22447c478bd9Sstevel@tonic-gate 22457c478bd9Sstevel@tonic-gate int 22467c478bd9Sstevel@tonic-gate zonecfg_add_nwif(zone_dochandle_t handle, struct zone_nwiftab *tabptr) 22477c478bd9Sstevel@tonic-gate { 22487c478bd9Sstevel@tonic-gate int err; 22497c478bd9Sstevel@tonic-gate 22507c478bd9Sstevel@tonic-gate if (tabptr == NULL) 22517c478bd9Sstevel@tonic-gate return (Z_INVAL); 22527c478bd9Sstevel@tonic-gate 22537c478bd9Sstevel@tonic-gate if ((err = operation_prep(handle)) != Z_OK) 22547c478bd9Sstevel@tonic-gate return (err); 22557c478bd9Sstevel@tonic-gate 22567c478bd9Sstevel@tonic-gate if ((err = zonecfg_add_nwif_core(handle, tabptr)) != Z_OK) 22577c478bd9Sstevel@tonic-gate return (err); 22587c478bd9Sstevel@tonic-gate 22597c478bd9Sstevel@tonic-gate return (Z_OK); 22607c478bd9Sstevel@tonic-gate } 22617c478bd9Sstevel@tonic-gate 22627c478bd9Sstevel@tonic-gate static int 22637c478bd9Sstevel@tonic-gate zonecfg_delete_nwif_core(zone_dochandle_t handle, struct zone_nwiftab *tabptr) 22647c478bd9Sstevel@tonic-gate { 22657c478bd9Sstevel@tonic-gate xmlNodePtr cur = handle->zone_dh_cur; 22667c478bd9Sstevel@tonic-gate boolean_t addr_match, phys_match; 22677c478bd9Sstevel@tonic-gate 22687c478bd9Sstevel@tonic-gate for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) { 22697c478bd9Sstevel@tonic-gate if (xmlStrcmp(cur->name, DTD_ELEM_NET)) 22707c478bd9Sstevel@tonic-gate continue; 22717c478bd9Sstevel@tonic-gate 22727c478bd9Sstevel@tonic-gate addr_match = match_prop(cur, DTD_ATTR_ADDRESS, 22737c478bd9Sstevel@tonic-gate tabptr->zone_nwif_address); 22747c478bd9Sstevel@tonic-gate phys_match = match_prop(cur, DTD_ATTR_PHYSICAL, 22757c478bd9Sstevel@tonic-gate tabptr->zone_nwif_physical); 22767c478bd9Sstevel@tonic-gate 22777c478bd9Sstevel@tonic-gate if (addr_match && phys_match) { 22787c478bd9Sstevel@tonic-gate xmlUnlinkNode(cur); 22797c478bd9Sstevel@tonic-gate xmlFreeNode(cur); 22807c478bd9Sstevel@tonic-gate return (Z_OK); 22817c478bd9Sstevel@tonic-gate } 22827c478bd9Sstevel@tonic-gate } 22837c478bd9Sstevel@tonic-gate return (Z_NO_RESOURCE_ID); 22847c478bd9Sstevel@tonic-gate } 22857c478bd9Sstevel@tonic-gate 22867c478bd9Sstevel@tonic-gate int 22877c478bd9Sstevel@tonic-gate zonecfg_delete_nwif(zone_dochandle_t handle, struct zone_nwiftab *tabptr) 22887c478bd9Sstevel@tonic-gate { 22897c478bd9Sstevel@tonic-gate int err; 22907c478bd9Sstevel@tonic-gate 22917c478bd9Sstevel@tonic-gate if (tabptr == NULL) 22927c478bd9Sstevel@tonic-gate return (Z_INVAL); 22937c478bd9Sstevel@tonic-gate 22947c478bd9Sstevel@tonic-gate if ((err = operation_prep(handle)) != Z_OK) 22957c478bd9Sstevel@tonic-gate return (err); 22967c478bd9Sstevel@tonic-gate 22977c478bd9Sstevel@tonic-gate if ((err = zonecfg_delete_nwif_core(handle, tabptr)) != Z_OK) 22987c478bd9Sstevel@tonic-gate return (err); 22997c478bd9Sstevel@tonic-gate 23007c478bd9Sstevel@tonic-gate return (Z_OK); 23017c478bd9Sstevel@tonic-gate } 23027c478bd9Sstevel@tonic-gate 23037c478bd9Sstevel@tonic-gate int 23047c478bd9Sstevel@tonic-gate zonecfg_modify_nwif( 23057c478bd9Sstevel@tonic-gate zone_dochandle_t handle, 23067c478bd9Sstevel@tonic-gate struct zone_nwiftab *oldtabptr, 23077c478bd9Sstevel@tonic-gate struct zone_nwiftab *newtabptr) 23087c478bd9Sstevel@tonic-gate { 23097c478bd9Sstevel@tonic-gate int err; 23107c478bd9Sstevel@tonic-gate 23117c478bd9Sstevel@tonic-gate if (oldtabptr == NULL || newtabptr == NULL) 23127c478bd9Sstevel@tonic-gate return (Z_INVAL); 23137c478bd9Sstevel@tonic-gate 23147c478bd9Sstevel@tonic-gate if ((err = operation_prep(handle)) != Z_OK) 23157c478bd9Sstevel@tonic-gate return (err); 23167c478bd9Sstevel@tonic-gate 23177c478bd9Sstevel@tonic-gate if ((err = zonecfg_delete_nwif_core(handle, oldtabptr)) != Z_OK) 23187c478bd9Sstevel@tonic-gate return (err); 23197c478bd9Sstevel@tonic-gate 23207c478bd9Sstevel@tonic-gate if ((err = zonecfg_add_nwif_core(handle, newtabptr)) != Z_OK) 23217c478bd9Sstevel@tonic-gate return (err); 23227c478bd9Sstevel@tonic-gate 23237c478bd9Sstevel@tonic-gate return (Z_OK); 23247c478bd9Sstevel@tonic-gate } 23257c478bd9Sstevel@tonic-gate 23267c478bd9Sstevel@tonic-gate int 23277c478bd9Sstevel@tonic-gate zonecfg_lookup_dev(zone_dochandle_t handle, struct zone_devtab *tabptr) 23287c478bd9Sstevel@tonic-gate { 23297c478bd9Sstevel@tonic-gate xmlNodePtr cur, firstmatch; 23307c478bd9Sstevel@tonic-gate int err; 23317c478bd9Sstevel@tonic-gate char match[MAXPATHLEN]; 23327c478bd9Sstevel@tonic-gate 23337c478bd9Sstevel@tonic-gate if (tabptr == NULL) 23347c478bd9Sstevel@tonic-gate return (Z_INVAL); 23357c478bd9Sstevel@tonic-gate 23367c478bd9Sstevel@tonic-gate if ((err = operation_prep(handle)) != Z_OK) 23377c478bd9Sstevel@tonic-gate return (err); 23387c478bd9Sstevel@tonic-gate 23397c478bd9Sstevel@tonic-gate cur = handle->zone_dh_cur; 23407c478bd9Sstevel@tonic-gate firstmatch = NULL; 23417c478bd9Sstevel@tonic-gate for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) { 23427c478bd9Sstevel@tonic-gate if (xmlStrcmp(cur->name, DTD_ELEM_DEVICE)) 23437c478bd9Sstevel@tonic-gate continue; 23447c478bd9Sstevel@tonic-gate if (strlen(tabptr->zone_dev_match) == 0) 23457c478bd9Sstevel@tonic-gate continue; 23467c478bd9Sstevel@tonic-gate 23477c478bd9Sstevel@tonic-gate if ((fetchprop(cur, DTD_ATTR_MATCH, match, 23487c478bd9Sstevel@tonic-gate sizeof (match)) == Z_OK)) { 23497c478bd9Sstevel@tonic-gate if (strcmp(tabptr->zone_dev_match, 23507c478bd9Sstevel@tonic-gate match) == 0) { 23517c478bd9Sstevel@tonic-gate if (firstmatch == NULL) 23527c478bd9Sstevel@tonic-gate firstmatch = cur; 23537c478bd9Sstevel@tonic-gate else if (firstmatch != cur) 23547c478bd9Sstevel@tonic-gate return (Z_INSUFFICIENT_SPEC); 23557c478bd9Sstevel@tonic-gate } else { 23567c478bd9Sstevel@tonic-gate /* 23577c478bd9Sstevel@tonic-gate * If another property matched but this 23587c478bd9Sstevel@tonic-gate * one doesn't then reset firstmatch. 23597c478bd9Sstevel@tonic-gate */ 23607c478bd9Sstevel@tonic-gate if (firstmatch == cur) 23617c478bd9Sstevel@tonic-gate firstmatch = NULL; 23627c478bd9Sstevel@tonic-gate } 23637c478bd9Sstevel@tonic-gate } 23647c478bd9Sstevel@tonic-gate } 23657c478bd9Sstevel@tonic-gate if (firstmatch == NULL) 23667c478bd9Sstevel@tonic-gate return (Z_NO_RESOURCE_ID); 23677c478bd9Sstevel@tonic-gate 23687c478bd9Sstevel@tonic-gate cur = firstmatch; 23697c478bd9Sstevel@tonic-gate 23707c478bd9Sstevel@tonic-gate if ((err = fetchprop(cur, DTD_ATTR_MATCH, tabptr->zone_dev_match, 23717c478bd9Sstevel@tonic-gate sizeof (tabptr->zone_dev_match))) != Z_OK) 23727c478bd9Sstevel@tonic-gate return (err); 23737c478bd9Sstevel@tonic-gate 23747c478bd9Sstevel@tonic-gate return (Z_OK); 23757c478bd9Sstevel@tonic-gate } 23767c478bd9Sstevel@tonic-gate 23777c478bd9Sstevel@tonic-gate static int 23787c478bd9Sstevel@tonic-gate zonecfg_add_dev_core(zone_dochandle_t handle, struct zone_devtab *tabptr) 23797c478bd9Sstevel@tonic-gate { 23807c478bd9Sstevel@tonic-gate xmlNodePtr newnode, cur = handle->zone_dh_cur; 23817c478bd9Sstevel@tonic-gate int err; 23827c478bd9Sstevel@tonic-gate 23837c478bd9Sstevel@tonic-gate newnode = xmlNewTextChild(cur, NULL, DTD_ELEM_DEVICE, NULL); 23847c478bd9Sstevel@tonic-gate 2385a1be23daSdp if ((err = newprop(newnode, DTD_ATTR_MATCH, 23867c478bd9Sstevel@tonic-gate tabptr->zone_dev_match)) != Z_OK) 23877c478bd9Sstevel@tonic-gate return (err); 23887c478bd9Sstevel@tonic-gate 23897c478bd9Sstevel@tonic-gate return (Z_OK); 23907c478bd9Sstevel@tonic-gate } 23917c478bd9Sstevel@tonic-gate 23927c478bd9Sstevel@tonic-gate int 23937c478bd9Sstevel@tonic-gate zonecfg_add_dev(zone_dochandle_t handle, struct zone_devtab *tabptr) 23947c478bd9Sstevel@tonic-gate { 23957c478bd9Sstevel@tonic-gate int err; 23967c478bd9Sstevel@tonic-gate 23977c478bd9Sstevel@tonic-gate if (tabptr == NULL) 23987c478bd9Sstevel@tonic-gate return (Z_INVAL); 23997c478bd9Sstevel@tonic-gate 24007c478bd9Sstevel@tonic-gate if ((err = operation_prep(handle)) != Z_OK) 24017c478bd9Sstevel@tonic-gate return (err); 24027c478bd9Sstevel@tonic-gate 24037c478bd9Sstevel@tonic-gate if ((err = zonecfg_add_dev_core(handle, tabptr)) != Z_OK) 24047c478bd9Sstevel@tonic-gate return (err); 24057c478bd9Sstevel@tonic-gate 24067c478bd9Sstevel@tonic-gate return (Z_OK); 24077c478bd9Sstevel@tonic-gate } 24087c478bd9Sstevel@tonic-gate 24097c478bd9Sstevel@tonic-gate static int 24107c478bd9Sstevel@tonic-gate zonecfg_delete_dev_core(zone_dochandle_t handle, struct zone_devtab *tabptr) 24117c478bd9Sstevel@tonic-gate { 2412facf4a8dSllai1 xmlNodePtr cur = handle->zone_dh_cur; 24137c478bd9Sstevel@tonic-gate int match_match; 24147c478bd9Sstevel@tonic-gate 24157c478bd9Sstevel@tonic-gate for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) { 24167c478bd9Sstevel@tonic-gate if (xmlStrcmp(cur->name, DTD_ELEM_DEVICE)) 24177c478bd9Sstevel@tonic-gate continue; 24187c478bd9Sstevel@tonic-gate 24197c478bd9Sstevel@tonic-gate match_match = match_prop(cur, DTD_ATTR_MATCH, 24207c478bd9Sstevel@tonic-gate tabptr->zone_dev_match); 24217c478bd9Sstevel@tonic-gate 24227c478bd9Sstevel@tonic-gate if (match_match) { 24237c478bd9Sstevel@tonic-gate xmlUnlinkNode(cur); 24247c478bd9Sstevel@tonic-gate xmlFreeNode(cur); 24257c478bd9Sstevel@tonic-gate return (Z_OK); 24267c478bd9Sstevel@tonic-gate } 24277c478bd9Sstevel@tonic-gate } 24287c478bd9Sstevel@tonic-gate return (Z_NO_RESOURCE_ID); 24297c478bd9Sstevel@tonic-gate } 24307c478bd9Sstevel@tonic-gate 24317c478bd9Sstevel@tonic-gate int 24327c478bd9Sstevel@tonic-gate zonecfg_delete_dev(zone_dochandle_t handle, struct zone_devtab *tabptr) 24337c478bd9Sstevel@tonic-gate { 24347c478bd9Sstevel@tonic-gate int err; 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 if ((err = zonecfg_delete_dev_core(handle, tabptr)) != Z_OK) 24437c478bd9Sstevel@tonic-gate return (err); 24447c478bd9Sstevel@tonic-gate 24457c478bd9Sstevel@tonic-gate return (Z_OK); 24467c478bd9Sstevel@tonic-gate } 24477c478bd9Sstevel@tonic-gate 24487c478bd9Sstevel@tonic-gate int 24497c478bd9Sstevel@tonic-gate zonecfg_modify_dev( 24507c478bd9Sstevel@tonic-gate zone_dochandle_t handle, 24517c478bd9Sstevel@tonic-gate struct zone_devtab *oldtabptr, 24527c478bd9Sstevel@tonic-gate struct zone_devtab *newtabptr) 24537c478bd9Sstevel@tonic-gate { 24547c478bd9Sstevel@tonic-gate int err; 24557c478bd9Sstevel@tonic-gate 24567c478bd9Sstevel@tonic-gate if (oldtabptr == NULL || newtabptr == NULL) 24577c478bd9Sstevel@tonic-gate return (Z_INVAL); 24587c478bd9Sstevel@tonic-gate 24597c478bd9Sstevel@tonic-gate if ((err = operation_prep(handle)) != Z_OK) 24607c478bd9Sstevel@tonic-gate return (err); 24617c478bd9Sstevel@tonic-gate 24627c478bd9Sstevel@tonic-gate if ((err = zonecfg_delete_dev_core(handle, oldtabptr)) != Z_OK) 24637c478bd9Sstevel@tonic-gate return (err); 24647c478bd9Sstevel@tonic-gate 24657c478bd9Sstevel@tonic-gate if ((err = zonecfg_add_dev_core(handle, newtabptr)) != Z_OK) 24667c478bd9Sstevel@tonic-gate return (err); 24677c478bd9Sstevel@tonic-gate 24687c478bd9Sstevel@tonic-gate return (Z_OK); 24697c478bd9Sstevel@tonic-gate } 24707c478bd9Sstevel@tonic-gate 2471ee519a1fSgjelinek /* Lock to serialize all zonecfg_devwalks */ 2472ee519a1fSgjelinek static pthread_mutex_t zonecfg_devwalk_lock = PTHREAD_MUTEX_INITIALIZER; 2473ee519a1fSgjelinek /* 2474ee519a1fSgjelinek * Global variables used to pass data from zonecfg_devwalk to the nftw 2475ee519a1fSgjelinek * call-back (zonecfg_devwalk_cb). g_devwalk_data is really the void* 2476ee519a1fSgjelinek * parameter and g_devwalk_cb is really the *cb parameter from zonecfg_devwalk. 2477ee519a1fSgjelinek */ 2478ee519a1fSgjelinek static void *g_devwalk_data; 2479ee519a1fSgjelinek static int (*g_devwalk_cb)(const char *, uid_t, gid_t, mode_t, const char *, 2480ee519a1fSgjelinek void *); 2481ee519a1fSgjelinek static size_t g_devwalk_skip_prefix; 2482ee519a1fSgjelinek 2483ee519a1fSgjelinek /* 2484ee519a1fSgjelinek * This is the nftw call-back function used by zonecfg_devwalk. It is 2485ee519a1fSgjelinek * responsible for calling the actual call-back that is passed in to 2486ee519a1fSgjelinek * zonecfg_devwalk as the *cb argument. 2487ee519a1fSgjelinek */ 2488ee519a1fSgjelinek /* ARGSUSED2 */ 2489ee519a1fSgjelinek static int 2490ee519a1fSgjelinek zonecfg_devwalk_cb(const char *path, const struct stat *st, int f, 2491ee519a1fSgjelinek struct FTW *ftw) 2492ee519a1fSgjelinek { 2493ee519a1fSgjelinek acl_t *acl; 2494ee519a1fSgjelinek char *acl_txt = NULL; 2495ee519a1fSgjelinek 2496ee519a1fSgjelinek /* skip all but character and block devices */ 2497ee519a1fSgjelinek if (!S_ISBLK(st->st_mode) && !S_ISCHR(st->st_mode)) 2498ee519a1fSgjelinek return (0); 2499ee519a1fSgjelinek 2500ee519a1fSgjelinek if ((acl_get(path, ACL_NO_TRIVIAL, &acl) == 0) && 2501ee519a1fSgjelinek acl != NULL) { 2502ee519a1fSgjelinek acl_txt = acl_totext(acl, ACL_NORESOLVE); 2503ee519a1fSgjelinek acl_free(acl); 2504ee519a1fSgjelinek } 2505ee519a1fSgjelinek 2506ee519a1fSgjelinek if (strlen(path) <= g_devwalk_skip_prefix) 2507ee519a1fSgjelinek return (0); 2508ee519a1fSgjelinek 2509ee519a1fSgjelinek g_devwalk_cb(path + g_devwalk_skip_prefix, st->st_uid, st->st_gid, 2510ee519a1fSgjelinek st->st_mode & S_IAMB, acl_txt != NULL ? acl_txt : "", 2511ee519a1fSgjelinek g_devwalk_data); 2512ee519a1fSgjelinek free(acl_txt); 2513ee519a1fSgjelinek return (0); 2514ee519a1fSgjelinek } 2515ee519a1fSgjelinek 2516ee519a1fSgjelinek /* 2517ee519a1fSgjelinek * Walk the dev tree for the zone specified by hdl and call the call-back (cb) 2518ee519a1fSgjelinek * function for each entry in the tree. The call-back will be passed the 2519ee519a1fSgjelinek * name, uid, gid, mode, acl string and the void *data input parameter 2520ee519a1fSgjelinek * for each dev entry. 2521ee519a1fSgjelinek * 2522ee519a1fSgjelinek * Data is passed to the zonecfg_devwalk_cb through the global variables 2523ee519a1fSgjelinek * g_devwalk_data, *g_devwalk_cb, and g_devwalk_skip_prefix. The 2524ee519a1fSgjelinek * zonecfg_devwalk_cb function will actually call *cb. 2525ee519a1fSgjelinek */ 2526ee519a1fSgjelinek int 2527ee519a1fSgjelinek zonecfg_devwalk(zone_dochandle_t hdl, 2528ee519a1fSgjelinek int (*cb)(const char *, uid_t, gid_t, mode_t, const char *, void *), 2529ee519a1fSgjelinek void *data) 2530ee519a1fSgjelinek { 2531ee519a1fSgjelinek char path[MAXPATHLEN]; 2532ee519a1fSgjelinek int ret; 2533ee519a1fSgjelinek 2534ee519a1fSgjelinek if ((ret = zonecfg_get_zonepath(hdl, path, sizeof (path))) != Z_OK) 2535ee519a1fSgjelinek return (ret); 2536ee519a1fSgjelinek 2537ee519a1fSgjelinek if (strlcat(path, "/dev", sizeof (path)) >= sizeof (path)) 2538ee519a1fSgjelinek return (Z_TOO_BIG); 2539ee519a1fSgjelinek g_devwalk_skip_prefix = strlen(path) + 1; 2540ee519a1fSgjelinek 2541ee519a1fSgjelinek /* 2542ee519a1fSgjelinek * We have to serialize all zonecfg_devwalks in the same process 2543ee519a1fSgjelinek * (which should be fine), since nftw() is so badly designed. 2544ee519a1fSgjelinek */ 2545ee519a1fSgjelinek (void) pthread_mutex_lock(&zonecfg_devwalk_lock); 2546ee519a1fSgjelinek 2547ee519a1fSgjelinek g_devwalk_data = data; 2548ee519a1fSgjelinek g_devwalk_cb = cb; 2549ee519a1fSgjelinek (void) nftw(path, zonecfg_devwalk_cb, 0, FTW_PHYS); 2550ee519a1fSgjelinek 2551ee519a1fSgjelinek (void) pthread_mutex_unlock(&zonecfg_devwalk_lock); 2552ee519a1fSgjelinek return (Z_OK); 2553ee519a1fSgjelinek } 2554ee519a1fSgjelinek 2555ee519a1fSgjelinek /* 2556ee519a1fSgjelinek * Update the owner, group, mode and acl on the specified dev (inpath) for 2557ee519a1fSgjelinek * the zone (hdl). This function can be used to fix up the dev tree after 2558ee519a1fSgjelinek * attaching a migrated zone. 2559ee519a1fSgjelinek */ 2560ee519a1fSgjelinek int 2561ee519a1fSgjelinek zonecfg_devperms_apply(zone_dochandle_t hdl, const char *inpath, uid_t owner, 2562ee519a1fSgjelinek gid_t group, mode_t mode, const char *acltxt) 2563ee519a1fSgjelinek { 2564ee519a1fSgjelinek int ret; 2565ee519a1fSgjelinek char path[MAXPATHLEN]; 2566ee519a1fSgjelinek struct stat st; 2567ee519a1fSgjelinek acl_t *aclp; 2568ee519a1fSgjelinek 2569ee519a1fSgjelinek if ((ret = zonecfg_get_zonepath(hdl, path, sizeof (path))) != Z_OK) 2570ee519a1fSgjelinek return (ret); 2571ee519a1fSgjelinek 2572ee519a1fSgjelinek if (strlcat(path, "/dev/", sizeof (path)) >= sizeof (path)) 2573ee519a1fSgjelinek return (Z_TOO_BIG); 2574ee519a1fSgjelinek if (strlcat(path, inpath, sizeof (path)) >= sizeof (path)) 2575ee519a1fSgjelinek return (Z_TOO_BIG); 2576ee519a1fSgjelinek 2577ee519a1fSgjelinek if (stat(path, &st) == -1) 2578ee519a1fSgjelinek return (Z_INVAL); 2579ee519a1fSgjelinek 2580ee519a1fSgjelinek /* make sure we're only touching device nodes */ 2581ee519a1fSgjelinek if (!S_ISCHR(st.st_mode) && !S_ISBLK(st.st_mode)) 2582ee519a1fSgjelinek return (Z_INVAL); 2583ee519a1fSgjelinek 2584ee519a1fSgjelinek if (chown(path, owner, group) == -1) 2585ee519a1fSgjelinek return (Z_SYSTEM); 2586ee519a1fSgjelinek 2587ee519a1fSgjelinek if (chmod(path, mode) == -1) 2588ee519a1fSgjelinek return (Z_SYSTEM); 2589ee519a1fSgjelinek 2590ee519a1fSgjelinek if ((acltxt == NULL) || (strcmp(acltxt, "") == 0)) 2591ee519a1fSgjelinek return (Z_OK); 2592ee519a1fSgjelinek 2593ee519a1fSgjelinek if (acl_fromtext(acltxt, &aclp) != 0) 2594ee519a1fSgjelinek return (Z_SYSTEM); 2595ee519a1fSgjelinek 2596ee519a1fSgjelinek errno = 0; 2597ee519a1fSgjelinek if (acl_set(path, aclp) == -1) { 2598ee519a1fSgjelinek free(aclp); 2599ee519a1fSgjelinek return (Z_SYSTEM); 2600ee519a1fSgjelinek } 2601ee519a1fSgjelinek 2602ee519a1fSgjelinek free(aclp); 2603ee519a1fSgjelinek return (Z_OK); 2604ee519a1fSgjelinek } 2605ee519a1fSgjelinek 26067c478bd9Sstevel@tonic-gate /* 26077c478bd9Sstevel@tonic-gate * This function finds everything mounted under a zone's rootpath. 26087c478bd9Sstevel@tonic-gate * This returns the number of mounts under rootpath, or -1 on error. 26097c478bd9Sstevel@tonic-gate * callback is called once per mount found with the first argument 26107c478bd9Sstevel@tonic-gate * pointing to the mount point. 26117c478bd9Sstevel@tonic-gate * 26127c478bd9Sstevel@tonic-gate * If the callback function returns non-zero zonecfg_find_mounts 26137c478bd9Sstevel@tonic-gate * aborts with an error. 26147c478bd9Sstevel@tonic-gate */ 26157c478bd9Sstevel@tonic-gate int 26167c478bd9Sstevel@tonic-gate zonecfg_find_mounts(char *rootpath, int (*callback)(const char *, void *), 26177c478bd9Sstevel@tonic-gate void *priv) { 26187c478bd9Sstevel@tonic-gate FILE *mnttab; 26197c478bd9Sstevel@tonic-gate struct mnttab m; 26207c478bd9Sstevel@tonic-gate size_t l; 262107b574eeSgjelinek int zfsl; 26227c478bd9Sstevel@tonic-gate int rv = 0; 262307b574eeSgjelinek char zfs_path[MAXPATHLEN]; 26247c478bd9Sstevel@tonic-gate 26257c478bd9Sstevel@tonic-gate assert(rootpath != NULL); 26267c478bd9Sstevel@tonic-gate 262707b574eeSgjelinek if ((zfsl = snprintf(zfs_path, sizeof (zfs_path), "%s/.zfs/", rootpath)) 262807b574eeSgjelinek >= sizeof (zfs_path)) 262907b574eeSgjelinek return (-1); 263007b574eeSgjelinek 26317c478bd9Sstevel@tonic-gate l = strlen(rootpath); 26327c478bd9Sstevel@tonic-gate 26337c478bd9Sstevel@tonic-gate mnttab = fopen("/etc/mnttab", "r"); 26347c478bd9Sstevel@tonic-gate 26357c478bd9Sstevel@tonic-gate if (mnttab == NULL) 26367c478bd9Sstevel@tonic-gate return (-1); 26377c478bd9Sstevel@tonic-gate 26387c478bd9Sstevel@tonic-gate if (ioctl(fileno(mnttab), MNTIOC_SHOWHIDDEN, NULL) < 0) { 26397c478bd9Sstevel@tonic-gate rv = -1; 26407c478bd9Sstevel@tonic-gate goto out; 26417c478bd9Sstevel@tonic-gate } 26427c478bd9Sstevel@tonic-gate 26437c478bd9Sstevel@tonic-gate while (!getmntent(mnttab, &m)) { 26447c478bd9Sstevel@tonic-gate if ((strncmp(rootpath, m.mnt_mountp, l) == 0) && 264507b574eeSgjelinek (m.mnt_mountp[l] == '/') && 264607b574eeSgjelinek (strncmp(zfs_path, m.mnt_mountp, zfsl) != 0)) { 26477c478bd9Sstevel@tonic-gate rv++; 26487c478bd9Sstevel@tonic-gate if (callback == NULL) 26497c478bd9Sstevel@tonic-gate continue; 26507c478bd9Sstevel@tonic-gate if (callback(m.mnt_mountp, priv)) { 26517c478bd9Sstevel@tonic-gate rv = -1; 26527c478bd9Sstevel@tonic-gate goto out; 26537c478bd9Sstevel@tonic-gate 26547c478bd9Sstevel@tonic-gate } 26557c478bd9Sstevel@tonic-gate } 26567c478bd9Sstevel@tonic-gate } 26577c478bd9Sstevel@tonic-gate 26587c478bd9Sstevel@tonic-gate out: 26597c478bd9Sstevel@tonic-gate (void) fclose(mnttab); 26607c478bd9Sstevel@tonic-gate return (rv); 26617c478bd9Sstevel@tonic-gate } 26627c478bd9Sstevel@tonic-gate 26637c478bd9Sstevel@tonic-gate int 26647c478bd9Sstevel@tonic-gate zonecfg_lookup_attr(zone_dochandle_t handle, struct zone_attrtab *tabptr) 26657c478bd9Sstevel@tonic-gate { 26667c478bd9Sstevel@tonic-gate xmlNodePtr cur, firstmatch; 26677c478bd9Sstevel@tonic-gate int err; 26687c478bd9Sstevel@tonic-gate char name[MAXNAMELEN], type[MAXNAMELEN], value[MAXNAMELEN]; 26697c478bd9Sstevel@tonic-gate 26707c478bd9Sstevel@tonic-gate if (tabptr == NULL) 26717c478bd9Sstevel@tonic-gate return (Z_INVAL); 26727c478bd9Sstevel@tonic-gate 26737c478bd9Sstevel@tonic-gate if ((err = operation_prep(handle)) != Z_OK) 26747c478bd9Sstevel@tonic-gate return (err); 26757c478bd9Sstevel@tonic-gate 26767c478bd9Sstevel@tonic-gate cur = handle->zone_dh_cur; 26777c478bd9Sstevel@tonic-gate firstmatch = NULL; 26787c478bd9Sstevel@tonic-gate for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) { 26797c478bd9Sstevel@tonic-gate if (xmlStrcmp(cur->name, DTD_ELEM_ATTR)) 26807c478bd9Sstevel@tonic-gate continue; 26817c478bd9Sstevel@tonic-gate if (strlen(tabptr->zone_attr_name) > 0) { 26827c478bd9Sstevel@tonic-gate if ((fetchprop(cur, DTD_ATTR_NAME, name, 26837c478bd9Sstevel@tonic-gate sizeof (name)) == Z_OK) && 26847c478bd9Sstevel@tonic-gate (strcmp(tabptr->zone_attr_name, name) == 0)) { 26857c478bd9Sstevel@tonic-gate if (firstmatch == NULL) 26867c478bd9Sstevel@tonic-gate firstmatch = cur; 26877c478bd9Sstevel@tonic-gate else 26887c478bd9Sstevel@tonic-gate return (Z_INSUFFICIENT_SPEC); 26897c478bd9Sstevel@tonic-gate } 26907c478bd9Sstevel@tonic-gate } 26917c478bd9Sstevel@tonic-gate if (strlen(tabptr->zone_attr_type) > 0) { 26927c478bd9Sstevel@tonic-gate if ((fetchprop(cur, DTD_ATTR_TYPE, type, 26937c478bd9Sstevel@tonic-gate sizeof (type)) == Z_OK)) { 26947c478bd9Sstevel@tonic-gate if (strcmp(tabptr->zone_attr_type, type) == 0) { 26957c478bd9Sstevel@tonic-gate if (firstmatch == NULL) 26967c478bd9Sstevel@tonic-gate firstmatch = cur; 26977c478bd9Sstevel@tonic-gate else if (firstmatch != cur) 26987c478bd9Sstevel@tonic-gate return (Z_INSUFFICIENT_SPEC); 26997c478bd9Sstevel@tonic-gate } else { 27007c478bd9Sstevel@tonic-gate /* 27017c478bd9Sstevel@tonic-gate * If another property matched but this 27027c478bd9Sstevel@tonic-gate * one doesn't then reset firstmatch. 27037c478bd9Sstevel@tonic-gate */ 27047c478bd9Sstevel@tonic-gate if (firstmatch == cur) 27057c478bd9Sstevel@tonic-gate firstmatch = NULL; 27067c478bd9Sstevel@tonic-gate } 27077c478bd9Sstevel@tonic-gate } 27087c478bd9Sstevel@tonic-gate } 27097c478bd9Sstevel@tonic-gate if (strlen(tabptr->zone_attr_value) > 0) { 27107c478bd9Sstevel@tonic-gate if ((fetchprop(cur, DTD_ATTR_VALUE, value, 27117c478bd9Sstevel@tonic-gate sizeof (value)) == Z_OK)) { 27127c478bd9Sstevel@tonic-gate if (strcmp(tabptr->zone_attr_value, value) == 27137c478bd9Sstevel@tonic-gate 0) { 27147c478bd9Sstevel@tonic-gate if (firstmatch == NULL) 27157c478bd9Sstevel@tonic-gate firstmatch = cur; 27167c478bd9Sstevel@tonic-gate else if (firstmatch != cur) 27177c478bd9Sstevel@tonic-gate return (Z_INSUFFICIENT_SPEC); 27187c478bd9Sstevel@tonic-gate } else { 27197c478bd9Sstevel@tonic-gate /* 27207c478bd9Sstevel@tonic-gate * If another property matched but this 27217c478bd9Sstevel@tonic-gate * one doesn't then reset firstmatch. 27227c478bd9Sstevel@tonic-gate */ 27237c478bd9Sstevel@tonic-gate if (firstmatch == cur) 27247c478bd9Sstevel@tonic-gate firstmatch = NULL; 27257c478bd9Sstevel@tonic-gate } 27267c478bd9Sstevel@tonic-gate } 27277c478bd9Sstevel@tonic-gate } 27287c478bd9Sstevel@tonic-gate } 27297c478bd9Sstevel@tonic-gate if (firstmatch == NULL) 27307c478bd9Sstevel@tonic-gate return (Z_NO_RESOURCE_ID); 27317c478bd9Sstevel@tonic-gate 27327c478bd9Sstevel@tonic-gate cur = firstmatch; 27337c478bd9Sstevel@tonic-gate 27347c478bd9Sstevel@tonic-gate if ((err = fetchprop(cur, DTD_ATTR_NAME, tabptr->zone_attr_name, 27357c478bd9Sstevel@tonic-gate sizeof (tabptr->zone_attr_name))) != Z_OK) 27367c478bd9Sstevel@tonic-gate return (err); 27377c478bd9Sstevel@tonic-gate 27387c478bd9Sstevel@tonic-gate if ((err = fetchprop(cur, DTD_ATTR_TYPE, tabptr->zone_attr_type, 27397c478bd9Sstevel@tonic-gate sizeof (tabptr->zone_attr_type))) != Z_OK) 27407c478bd9Sstevel@tonic-gate return (err); 27417c478bd9Sstevel@tonic-gate 27427c478bd9Sstevel@tonic-gate if ((err = fetchprop(cur, DTD_ATTR_VALUE, tabptr->zone_attr_value, 27437c478bd9Sstevel@tonic-gate sizeof (tabptr->zone_attr_value))) != Z_OK) 27447c478bd9Sstevel@tonic-gate return (err); 27457c478bd9Sstevel@tonic-gate 27467c478bd9Sstevel@tonic-gate return (Z_OK); 27477c478bd9Sstevel@tonic-gate } 27487c478bd9Sstevel@tonic-gate 27497c478bd9Sstevel@tonic-gate static int 27507c478bd9Sstevel@tonic-gate zonecfg_add_attr_core(zone_dochandle_t handle, struct zone_attrtab *tabptr) 27517c478bd9Sstevel@tonic-gate { 27527c478bd9Sstevel@tonic-gate xmlNodePtr newnode, cur = handle->zone_dh_cur; 27537c478bd9Sstevel@tonic-gate int err; 27547c478bd9Sstevel@tonic-gate 27557c478bd9Sstevel@tonic-gate newnode = xmlNewTextChild(cur, NULL, DTD_ELEM_ATTR, NULL); 2756a1be23daSdp err = newprop(newnode, DTD_ATTR_NAME, tabptr->zone_attr_name); 27577c478bd9Sstevel@tonic-gate if (err != Z_OK) 27587c478bd9Sstevel@tonic-gate return (err); 2759a1be23daSdp err = newprop(newnode, DTD_ATTR_TYPE, tabptr->zone_attr_type); 27607c478bd9Sstevel@tonic-gate if (err != Z_OK) 27617c478bd9Sstevel@tonic-gate return (err); 2762a1be23daSdp err = newprop(newnode, DTD_ATTR_VALUE, tabptr->zone_attr_value); 27637c478bd9Sstevel@tonic-gate if (err != Z_OK) 27647c478bd9Sstevel@tonic-gate return (err); 27657c478bd9Sstevel@tonic-gate return (Z_OK); 27667c478bd9Sstevel@tonic-gate } 27677c478bd9Sstevel@tonic-gate 27687c478bd9Sstevel@tonic-gate int 27697c478bd9Sstevel@tonic-gate zonecfg_add_attr(zone_dochandle_t handle, struct zone_attrtab *tabptr) 27707c478bd9Sstevel@tonic-gate { 27717c478bd9Sstevel@tonic-gate int err; 27727c478bd9Sstevel@tonic-gate 27737c478bd9Sstevel@tonic-gate if (tabptr == NULL) 27747c478bd9Sstevel@tonic-gate return (Z_INVAL); 27757c478bd9Sstevel@tonic-gate 27767c478bd9Sstevel@tonic-gate if ((err = operation_prep(handle)) != Z_OK) 27777c478bd9Sstevel@tonic-gate return (err); 27787c478bd9Sstevel@tonic-gate 27797c478bd9Sstevel@tonic-gate if ((err = zonecfg_add_attr_core(handle, tabptr)) != Z_OK) 27807c478bd9Sstevel@tonic-gate return (err); 27817c478bd9Sstevel@tonic-gate 27827c478bd9Sstevel@tonic-gate return (Z_OK); 27837c478bd9Sstevel@tonic-gate } 27847c478bd9Sstevel@tonic-gate 27857c478bd9Sstevel@tonic-gate static int 27867c478bd9Sstevel@tonic-gate zonecfg_delete_attr_core(zone_dochandle_t handle, struct zone_attrtab *tabptr) 27877c478bd9Sstevel@tonic-gate { 27887c478bd9Sstevel@tonic-gate xmlNodePtr cur = handle->zone_dh_cur; 27897c478bd9Sstevel@tonic-gate int name_match, type_match, value_match; 27907c478bd9Sstevel@tonic-gate 27917c478bd9Sstevel@tonic-gate for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) { 27927c478bd9Sstevel@tonic-gate if (xmlStrcmp(cur->name, DTD_ELEM_ATTR)) 27937c478bd9Sstevel@tonic-gate continue; 27947c478bd9Sstevel@tonic-gate 27957c478bd9Sstevel@tonic-gate name_match = match_prop(cur, DTD_ATTR_NAME, 27967c478bd9Sstevel@tonic-gate tabptr->zone_attr_name); 27977c478bd9Sstevel@tonic-gate type_match = match_prop(cur, DTD_ATTR_TYPE, 27987c478bd9Sstevel@tonic-gate tabptr->zone_attr_type); 27997c478bd9Sstevel@tonic-gate value_match = match_prop(cur, DTD_ATTR_VALUE, 28007c478bd9Sstevel@tonic-gate tabptr->zone_attr_value); 28017c478bd9Sstevel@tonic-gate 28027c478bd9Sstevel@tonic-gate if (name_match && type_match && value_match) { 28037c478bd9Sstevel@tonic-gate xmlUnlinkNode(cur); 28047c478bd9Sstevel@tonic-gate xmlFreeNode(cur); 28057c478bd9Sstevel@tonic-gate return (Z_OK); 28067c478bd9Sstevel@tonic-gate } 28077c478bd9Sstevel@tonic-gate } 28087c478bd9Sstevel@tonic-gate return (Z_NO_RESOURCE_ID); 28097c478bd9Sstevel@tonic-gate } 28107c478bd9Sstevel@tonic-gate 28117c478bd9Sstevel@tonic-gate int 28127c478bd9Sstevel@tonic-gate zonecfg_delete_attr(zone_dochandle_t handle, struct zone_attrtab *tabptr) 28137c478bd9Sstevel@tonic-gate { 28147c478bd9Sstevel@tonic-gate int err; 28157c478bd9Sstevel@tonic-gate 28167c478bd9Sstevel@tonic-gate if (tabptr == NULL) 28177c478bd9Sstevel@tonic-gate return (Z_INVAL); 28187c478bd9Sstevel@tonic-gate 28197c478bd9Sstevel@tonic-gate if ((err = operation_prep(handle)) != Z_OK) 28207c478bd9Sstevel@tonic-gate return (err); 28217c478bd9Sstevel@tonic-gate 28227c478bd9Sstevel@tonic-gate if ((err = zonecfg_delete_attr_core(handle, tabptr)) != Z_OK) 28237c478bd9Sstevel@tonic-gate return (err); 28247c478bd9Sstevel@tonic-gate 28257c478bd9Sstevel@tonic-gate return (Z_OK); 28267c478bd9Sstevel@tonic-gate } 28277c478bd9Sstevel@tonic-gate 28287c478bd9Sstevel@tonic-gate int 28297c478bd9Sstevel@tonic-gate zonecfg_modify_attr( 28307c478bd9Sstevel@tonic-gate zone_dochandle_t handle, 28317c478bd9Sstevel@tonic-gate struct zone_attrtab *oldtabptr, 28327c478bd9Sstevel@tonic-gate struct zone_attrtab *newtabptr) 28337c478bd9Sstevel@tonic-gate { 28347c478bd9Sstevel@tonic-gate int err; 28357c478bd9Sstevel@tonic-gate 28367c478bd9Sstevel@tonic-gate if (oldtabptr == NULL || newtabptr == NULL) 28377c478bd9Sstevel@tonic-gate return (Z_INVAL); 28387c478bd9Sstevel@tonic-gate 28397c478bd9Sstevel@tonic-gate if ((err = operation_prep(handle)) != Z_OK) 28407c478bd9Sstevel@tonic-gate return (err); 28417c478bd9Sstevel@tonic-gate 28427c478bd9Sstevel@tonic-gate if ((err = zonecfg_delete_attr_core(handle, oldtabptr)) != Z_OK) 28437c478bd9Sstevel@tonic-gate return (err); 28447c478bd9Sstevel@tonic-gate 28457c478bd9Sstevel@tonic-gate if ((err = zonecfg_add_attr_core(handle, newtabptr)) != Z_OK) 28467c478bd9Sstevel@tonic-gate return (err); 28477c478bd9Sstevel@tonic-gate 28487c478bd9Sstevel@tonic-gate return (Z_OK); 28497c478bd9Sstevel@tonic-gate } 28507c478bd9Sstevel@tonic-gate 28517c478bd9Sstevel@tonic-gate int 28527c478bd9Sstevel@tonic-gate zonecfg_get_attr_boolean(const struct zone_attrtab *attr, boolean_t *value) 28537c478bd9Sstevel@tonic-gate { 28547c478bd9Sstevel@tonic-gate if (attr == NULL) 28557c478bd9Sstevel@tonic-gate return (Z_INVAL); 28567c478bd9Sstevel@tonic-gate 28577c478bd9Sstevel@tonic-gate if (strcmp(attr->zone_attr_type, DTD_ENTITY_BOOLEAN) != 0) 28587c478bd9Sstevel@tonic-gate return (Z_INVAL); 28597c478bd9Sstevel@tonic-gate 28607c478bd9Sstevel@tonic-gate if (strcmp(attr->zone_attr_value, DTD_ENTITY_TRUE) == 0) { 28617c478bd9Sstevel@tonic-gate *value = B_TRUE; 28627c478bd9Sstevel@tonic-gate return (Z_OK); 28637c478bd9Sstevel@tonic-gate } 28647c478bd9Sstevel@tonic-gate if (strcmp(attr->zone_attr_value, DTD_ENTITY_FALSE) == 0) { 28657c478bd9Sstevel@tonic-gate *value = B_FALSE; 28667c478bd9Sstevel@tonic-gate return (Z_OK); 28677c478bd9Sstevel@tonic-gate } 28687c478bd9Sstevel@tonic-gate return (Z_INVAL); 28697c478bd9Sstevel@tonic-gate } 28707c478bd9Sstevel@tonic-gate 28717c478bd9Sstevel@tonic-gate int 28727c478bd9Sstevel@tonic-gate zonecfg_get_attr_int(const struct zone_attrtab *attr, int64_t *value) 28737c478bd9Sstevel@tonic-gate { 28747c478bd9Sstevel@tonic-gate long long result; 28757c478bd9Sstevel@tonic-gate char *endptr; 28767c478bd9Sstevel@tonic-gate 28777c478bd9Sstevel@tonic-gate if (attr == NULL) 28787c478bd9Sstevel@tonic-gate return (Z_INVAL); 28797c478bd9Sstevel@tonic-gate 28807c478bd9Sstevel@tonic-gate if (strcmp(attr->zone_attr_type, DTD_ENTITY_INT) != 0) 28817c478bd9Sstevel@tonic-gate return (Z_INVAL); 28827c478bd9Sstevel@tonic-gate 28837c478bd9Sstevel@tonic-gate errno = 0; 28847c478bd9Sstevel@tonic-gate result = strtoll(attr->zone_attr_value, &endptr, 10); 28857c478bd9Sstevel@tonic-gate if (errno != 0 || *endptr != '\0') 28867c478bd9Sstevel@tonic-gate return (Z_INVAL); 28877c478bd9Sstevel@tonic-gate *value = result; 28887c478bd9Sstevel@tonic-gate return (Z_OK); 28897c478bd9Sstevel@tonic-gate } 28907c478bd9Sstevel@tonic-gate 28917c478bd9Sstevel@tonic-gate int 28927c478bd9Sstevel@tonic-gate zonecfg_get_attr_string(const struct zone_attrtab *attr, char *value, 28937c478bd9Sstevel@tonic-gate size_t val_sz) 28947c478bd9Sstevel@tonic-gate { 28957c478bd9Sstevel@tonic-gate if (attr == NULL) 28967c478bd9Sstevel@tonic-gate return (Z_INVAL); 28977c478bd9Sstevel@tonic-gate 28987c478bd9Sstevel@tonic-gate if (strcmp(attr->zone_attr_type, DTD_ENTITY_STRING) != 0) 28997c478bd9Sstevel@tonic-gate return (Z_INVAL); 29007c478bd9Sstevel@tonic-gate 29017c478bd9Sstevel@tonic-gate if (strlcpy(value, attr->zone_attr_value, val_sz) >= val_sz) 29027c478bd9Sstevel@tonic-gate return (Z_TOO_BIG); 29037c478bd9Sstevel@tonic-gate return (Z_OK); 29047c478bd9Sstevel@tonic-gate } 29057c478bd9Sstevel@tonic-gate 29067c478bd9Sstevel@tonic-gate int 29077c478bd9Sstevel@tonic-gate zonecfg_get_attr_uint(const struct zone_attrtab *attr, uint64_t *value) 29087c478bd9Sstevel@tonic-gate { 29097c478bd9Sstevel@tonic-gate unsigned long long result; 29107c478bd9Sstevel@tonic-gate long long neg_result; 29117c478bd9Sstevel@tonic-gate char *endptr; 29127c478bd9Sstevel@tonic-gate 29137c478bd9Sstevel@tonic-gate if (attr == NULL) 29147c478bd9Sstevel@tonic-gate return (Z_INVAL); 29157c478bd9Sstevel@tonic-gate 29167c478bd9Sstevel@tonic-gate if (strcmp(attr->zone_attr_type, DTD_ENTITY_UINT) != 0) 29177c478bd9Sstevel@tonic-gate return (Z_INVAL); 29187c478bd9Sstevel@tonic-gate 29197c478bd9Sstevel@tonic-gate errno = 0; 29207c478bd9Sstevel@tonic-gate result = strtoull(attr->zone_attr_value, &endptr, 10); 29217c478bd9Sstevel@tonic-gate if (errno != 0 || *endptr != '\0') 29227c478bd9Sstevel@tonic-gate return (Z_INVAL); 29237c478bd9Sstevel@tonic-gate errno = 0; 29247c478bd9Sstevel@tonic-gate neg_result = strtoll(attr->zone_attr_value, &endptr, 10); 29257c478bd9Sstevel@tonic-gate /* 29267c478bd9Sstevel@tonic-gate * Incredibly, strtoull("<negative number>", ...) will not fail but 29277c478bd9Sstevel@tonic-gate * return whatever (negative) number cast as a u_longlong_t, so we 29287c478bd9Sstevel@tonic-gate * need to look for this here. 29297c478bd9Sstevel@tonic-gate */ 29307c478bd9Sstevel@tonic-gate if (errno == 0 && neg_result < 0) 29317c478bd9Sstevel@tonic-gate return (Z_INVAL); 29327c478bd9Sstevel@tonic-gate *value = result; 29337c478bd9Sstevel@tonic-gate return (Z_OK); 29347c478bd9Sstevel@tonic-gate } 29357c478bd9Sstevel@tonic-gate 29367c478bd9Sstevel@tonic-gate int 29377c478bd9Sstevel@tonic-gate zonecfg_lookup_rctl(zone_dochandle_t handle, struct zone_rctltab *tabptr) 29387c478bd9Sstevel@tonic-gate { 29397c478bd9Sstevel@tonic-gate xmlNodePtr cur, val; 29407c478bd9Sstevel@tonic-gate char savedname[MAXNAMELEN]; 29417c478bd9Sstevel@tonic-gate struct zone_rctlvaltab *valptr; 29427c478bd9Sstevel@tonic-gate int err; 29437c478bd9Sstevel@tonic-gate 29447c478bd9Sstevel@tonic-gate if (tabptr->zone_rctl_name == NULL || 29457c478bd9Sstevel@tonic-gate strlen(tabptr->zone_rctl_name) == 0) 29467c478bd9Sstevel@tonic-gate return (Z_INVAL); 29477c478bd9Sstevel@tonic-gate 29487c478bd9Sstevel@tonic-gate if ((err = operation_prep(handle)) != Z_OK) 29497c478bd9Sstevel@tonic-gate return (err); 29507c478bd9Sstevel@tonic-gate 29517c478bd9Sstevel@tonic-gate cur = handle->zone_dh_cur; 29527c478bd9Sstevel@tonic-gate for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) { 29537c478bd9Sstevel@tonic-gate if (xmlStrcmp(cur->name, DTD_ELEM_RCTL)) 29547c478bd9Sstevel@tonic-gate continue; 29557c478bd9Sstevel@tonic-gate if ((fetchprop(cur, DTD_ATTR_NAME, savedname, 29567c478bd9Sstevel@tonic-gate sizeof (savedname)) == Z_OK) && 29577c478bd9Sstevel@tonic-gate (strcmp(savedname, tabptr->zone_rctl_name) == 0)) { 29587c478bd9Sstevel@tonic-gate tabptr->zone_rctl_valptr = NULL; 29597c478bd9Sstevel@tonic-gate for (val = cur->xmlChildrenNode; val != NULL; 29607c478bd9Sstevel@tonic-gate val = val->next) { 29617c478bd9Sstevel@tonic-gate valptr = (struct zone_rctlvaltab *)malloc( 29627c478bd9Sstevel@tonic-gate sizeof (struct zone_rctlvaltab)); 29637c478bd9Sstevel@tonic-gate if (valptr == NULL) 29647c478bd9Sstevel@tonic-gate return (Z_NOMEM); 29657c478bd9Sstevel@tonic-gate if ((fetchprop(val, DTD_ATTR_PRIV, 29667c478bd9Sstevel@tonic-gate valptr->zone_rctlval_priv, 29677c478bd9Sstevel@tonic-gate sizeof (valptr->zone_rctlval_priv)) != 29687c478bd9Sstevel@tonic-gate Z_OK)) 29697c478bd9Sstevel@tonic-gate break; 29707c478bd9Sstevel@tonic-gate if ((fetchprop(val, DTD_ATTR_LIMIT, 29717c478bd9Sstevel@tonic-gate valptr->zone_rctlval_limit, 29727c478bd9Sstevel@tonic-gate sizeof (valptr->zone_rctlval_limit)) != 29737c478bd9Sstevel@tonic-gate Z_OK)) 29747c478bd9Sstevel@tonic-gate break; 29757c478bd9Sstevel@tonic-gate if ((fetchprop(val, DTD_ATTR_ACTION, 29767c478bd9Sstevel@tonic-gate valptr->zone_rctlval_action, 29777c478bd9Sstevel@tonic-gate sizeof (valptr->zone_rctlval_action)) != 29787c478bd9Sstevel@tonic-gate Z_OK)) 29797c478bd9Sstevel@tonic-gate break; 29807c478bd9Sstevel@tonic-gate if (zonecfg_add_rctl_value(tabptr, valptr) != 29817c478bd9Sstevel@tonic-gate Z_OK) 29827c478bd9Sstevel@tonic-gate break; 29837c478bd9Sstevel@tonic-gate } 29847c478bd9Sstevel@tonic-gate return (Z_OK); 29857c478bd9Sstevel@tonic-gate } 29867c478bd9Sstevel@tonic-gate } 29877c478bd9Sstevel@tonic-gate return (Z_NO_RESOURCE_ID); 29887c478bd9Sstevel@tonic-gate } 29897c478bd9Sstevel@tonic-gate 29907c478bd9Sstevel@tonic-gate static int 29917c478bd9Sstevel@tonic-gate zonecfg_add_rctl_core(zone_dochandle_t handle, struct zone_rctltab *tabptr) 29927c478bd9Sstevel@tonic-gate { 29937c478bd9Sstevel@tonic-gate xmlNodePtr newnode, cur = handle->zone_dh_cur, valnode; 29947c478bd9Sstevel@tonic-gate struct zone_rctlvaltab *valptr; 29957c478bd9Sstevel@tonic-gate int err; 29967c478bd9Sstevel@tonic-gate 29977c478bd9Sstevel@tonic-gate newnode = xmlNewTextChild(cur, NULL, DTD_ELEM_RCTL, NULL); 2998a1be23daSdp err = newprop(newnode, DTD_ATTR_NAME, tabptr->zone_rctl_name); 29997c478bd9Sstevel@tonic-gate if (err != Z_OK) 30007c478bd9Sstevel@tonic-gate return (err); 30017c478bd9Sstevel@tonic-gate for (valptr = tabptr->zone_rctl_valptr; valptr != NULL; 30027c478bd9Sstevel@tonic-gate valptr = valptr->zone_rctlval_next) { 30037c478bd9Sstevel@tonic-gate valnode = xmlNewTextChild(newnode, NULL, 30047c478bd9Sstevel@tonic-gate DTD_ELEM_RCTLVALUE, NULL); 3005a1be23daSdp err = newprop(valnode, DTD_ATTR_PRIV, 30067c478bd9Sstevel@tonic-gate valptr->zone_rctlval_priv); 30077c478bd9Sstevel@tonic-gate if (err != Z_OK) 30087c478bd9Sstevel@tonic-gate return (err); 3009a1be23daSdp err = newprop(valnode, DTD_ATTR_LIMIT, 30107c478bd9Sstevel@tonic-gate valptr->zone_rctlval_limit); 30117c478bd9Sstevel@tonic-gate if (err != Z_OK) 30127c478bd9Sstevel@tonic-gate return (err); 3013a1be23daSdp err = newprop(valnode, DTD_ATTR_ACTION, 30147c478bd9Sstevel@tonic-gate valptr->zone_rctlval_action); 30157c478bd9Sstevel@tonic-gate if (err != Z_OK) 30167c478bd9Sstevel@tonic-gate return (err); 30177c478bd9Sstevel@tonic-gate } 30187c478bd9Sstevel@tonic-gate return (Z_OK); 30197c478bd9Sstevel@tonic-gate } 30207c478bd9Sstevel@tonic-gate 30217c478bd9Sstevel@tonic-gate int 30227c478bd9Sstevel@tonic-gate zonecfg_add_rctl(zone_dochandle_t handle, struct zone_rctltab *tabptr) 30237c478bd9Sstevel@tonic-gate { 30247c478bd9Sstevel@tonic-gate int err; 30257c478bd9Sstevel@tonic-gate 30267c478bd9Sstevel@tonic-gate if (tabptr == NULL || tabptr->zone_rctl_name == NULL) 30277c478bd9Sstevel@tonic-gate return (Z_INVAL); 30287c478bd9Sstevel@tonic-gate 30297c478bd9Sstevel@tonic-gate if ((err = operation_prep(handle)) != Z_OK) 30307c478bd9Sstevel@tonic-gate return (err); 30317c478bd9Sstevel@tonic-gate 30327c478bd9Sstevel@tonic-gate if ((err = zonecfg_add_rctl_core(handle, tabptr)) != Z_OK) 30337c478bd9Sstevel@tonic-gate return (err); 30347c478bd9Sstevel@tonic-gate 30357c478bd9Sstevel@tonic-gate return (Z_OK); 30367c478bd9Sstevel@tonic-gate } 30377c478bd9Sstevel@tonic-gate 30387c478bd9Sstevel@tonic-gate static int 30397c478bd9Sstevel@tonic-gate zonecfg_delete_rctl_core(zone_dochandle_t handle, struct zone_rctltab *tabptr) 30407c478bd9Sstevel@tonic-gate { 30417c478bd9Sstevel@tonic-gate xmlNodePtr cur = handle->zone_dh_cur; 30427c478bd9Sstevel@tonic-gate xmlChar *savedname; 30437c478bd9Sstevel@tonic-gate int name_result; 30447c478bd9Sstevel@tonic-gate 30457c478bd9Sstevel@tonic-gate for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) { 30467c478bd9Sstevel@tonic-gate if (xmlStrcmp(cur->name, DTD_ELEM_RCTL)) 30477c478bd9Sstevel@tonic-gate continue; 30487c478bd9Sstevel@tonic-gate 30497c478bd9Sstevel@tonic-gate savedname = xmlGetProp(cur, DTD_ATTR_NAME); 30507c478bd9Sstevel@tonic-gate if (savedname == NULL) /* shouldn't happen */ 30517c478bd9Sstevel@tonic-gate continue; 30527c478bd9Sstevel@tonic-gate name_result = xmlStrcmp(savedname, 30537c478bd9Sstevel@tonic-gate (const xmlChar *) tabptr->zone_rctl_name); 30547c478bd9Sstevel@tonic-gate xmlFree(savedname); 30557c478bd9Sstevel@tonic-gate 30567c478bd9Sstevel@tonic-gate if (name_result == 0) { 30577c478bd9Sstevel@tonic-gate xmlUnlinkNode(cur); 30587c478bd9Sstevel@tonic-gate xmlFreeNode(cur); 30597c478bd9Sstevel@tonic-gate return (Z_OK); 30607c478bd9Sstevel@tonic-gate } 30617c478bd9Sstevel@tonic-gate } 30627c478bd9Sstevel@tonic-gate return (Z_NO_RESOURCE_ID); 30637c478bd9Sstevel@tonic-gate } 30647c478bd9Sstevel@tonic-gate 30657c478bd9Sstevel@tonic-gate int 30667c478bd9Sstevel@tonic-gate zonecfg_delete_rctl(zone_dochandle_t handle, struct zone_rctltab *tabptr) 30677c478bd9Sstevel@tonic-gate { 30687c478bd9Sstevel@tonic-gate int err; 30697c478bd9Sstevel@tonic-gate 30707c478bd9Sstevel@tonic-gate if (tabptr == NULL || tabptr->zone_rctl_name == NULL) 30717c478bd9Sstevel@tonic-gate return (Z_INVAL); 30727c478bd9Sstevel@tonic-gate 30737c478bd9Sstevel@tonic-gate if ((err = operation_prep(handle)) != Z_OK) 30747c478bd9Sstevel@tonic-gate return (err); 30757c478bd9Sstevel@tonic-gate 30767c478bd9Sstevel@tonic-gate if ((err = zonecfg_delete_rctl_core(handle, tabptr)) != Z_OK) 30777c478bd9Sstevel@tonic-gate return (err); 30787c478bd9Sstevel@tonic-gate 30797c478bd9Sstevel@tonic-gate return (Z_OK); 30807c478bd9Sstevel@tonic-gate } 30817c478bd9Sstevel@tonic-gate 30827c478bd9Sstevel@tonic-gate int 30837c478bd9Sstevel@tonic-gate zonecfg_modify_rctl( 30847c478bd9Sstevel@tonic-gate zone_dochandle_t handle, 30857c478bd9Sstevel@tonic-gate struct zone_rctltab *oldtabptr, 30867c478bd9Sstevel@tonic-gate struct zone_rctltab *newtabptr) 30877c478bd9Sstevel@tonic-gate { 30887c478bd9Sstevel@tonic-gate int err; 30897c478bd9Sstevel@tonic-gate 30907c478bd9Sstevel@tonic-gate if (oldtabptr == NULL || oldtabptr->zone_rctl_name == NULL || 30917c478bd9Sstevel@tonic-gate newtabptr == NULL || newtabptr->zone_rctl_name == NULL) 30927c478bd9Sstevel@tonic-gate return (Z_INVAL); 30937c478bd9Sstevel@tonic-gate 30947c478bd9Sstevel@tonic-gate if ((err = operation_prep(handle)) != Z_OK) 30957c478bd9Sstevel@tonic-gate return (err); 30967c478bd9Sstevel@tonic-gate 30977c478bd9Sstevel@tonic-gate if ((err = zonecfg_delete_rctl_core(handle, oldtabptr)) != Z_OK) 30987c478bd9Sstevel@tonic-gate return (err); 30997c478bd9Sstevel@tonic-gate 31007c478bd9Sstevel@tonic-gate if ((err = zonecfg_add_rctl_core(handle, newtabptr)) != Z_OK) 31017c478bd9Sstevel@tonic-gate return (err); 31027c478bd9Sstevel@tonic-gate 31037c478bd9Sstevel@tonic-gate return (Z_OK); 31047c478bd9Sstevel@tonic-gate } 31057c478bd9Sstevel@tonic-gate 31067c478bd9Sstevel@tonic-gate int 31077c478bd9Sstevel@tonic-gate zonecfg_add_rctl_value( 31087c478bd9Sstevel@tonic-gate struct zone_rctltab *tabptr, 31097c478bd9Sstevel@tonic-gate struct zone_rctlvaltab *valtabptr) 31107c478bd9Sstevel@tonic-gate { 31117c478bd9Sstevel@tonic-gate struct zone_rctlvaltab *last, *old, *new; 31127c478bd9Sstevel@tonic-gate rctlblk_t *rctlblk = alloca(rctlblk_size()); 31137c478bd9Sstevel@tonic-gate 31147c478bd9Sstevel@tonic-gate last = tabptr->zone_rctl_valptr; 31157c478bd9Sstevel@tonic-gate for (old = last; old != NULL; old = old->zone_rctlval_next) 31167c478bd9Sstevel@tonic-gate last = old; /* walk to the end of the list */ 31177c478bd9Sstevel@tonic-gate new = valtabptr; /* alloc'd by caller */ 31187c478bd9Sstevel@tonic-gate new->zone_rctlval_next = NULL; 31197c478bd9Sstevel@tonic-gate if (zonecfg_construct_rctlblk(valtabptr, rctlblk) != Z_OK) 31207c478bd9Sstevel@tonic-gate return (Z_INVAL); 31217c478bd9Sstevel@tonic-gate if (!zonecfg_valid_rctlblk(rctlblk)) 31227c478bd9Sstevel@tonic-gate return (Z_INVAL); 31237c478bd9Sstevel@tonic-gate if (last == NULL) 31247c478bd9Sstevel@tonic-gate tabptr->zone_rctl_valptr = new; 31257c478bd9Sstevel@tonic-gate else 31267c478bd9Sstevel@tonic-gate last->zone_rctlval_next = new; 31277c478bd9Sstevel@tonic-gate return (Z_OK); 31287c478bd9Sstevel@tonic-gate } 31297c478bd9Sstevel@tonic-gate 31307c478bd9Sstevel@tonic-gate int 31317c478bd9Sstevel@tonic-gate zonecfg_remove_rctl_value( 31327c478bd9Sstevel@tonic-gate struct zone_rctltab *tabptr, 31337c478bd9Sstevel@tonic-gate struct zone_rctlvaltab *valtabptr) 31347c478bd9Sstevel@tonic-gate { 31357c478bd9Sstevel@tonic-gate struct zone_rctlvaltab *last, *this, *next; 31367c478bd9Sstevel@tonic-gate 31377c478bd9Sstevel@tonic-gate last = tabptr->zone_rctl_valptr; 31387c478bd9Sstevel@tonic-gate for (this = last; this != NULL; this = this->zone_rctlval_next) { 31397c478bd9Sstevel@tonic-gate if (strcmp(this->zone_rctlval_priv, 31407c478bd9Sstevel@tonic-gate valtabptr->zone_rctlval_priv) == 0 && 31417c478bd9Sstevel@tonic-gate strcmp(this->zone_rctlval_limit, 31427c478bd9Sstevel@tonic-gate valtabptr->zone_rctlval_limit) == 0 && 31437c478bd9Sstevel@tonic-gate strcmp(this->zone_rctlval_action, 31447c478bd9Sstevel@tonic-gate valtabptr->zone_rctlval_action) == 0) { 31457c478bd9Sstevel@tonic-gate next = this->zone_rctlval_next; 31467c478bd9Sstevel@tonic-gate if (this == tabptr->zone_rctl_valptr) 31477c478bd9Sstevel@tonic-gate tabptr->zone_rctl_valptr = next; 31487c478bd9Sstevel@tonic-gate else 31497c478bd9Sstevel@tonic-gate last->zone_rctlval_next = next; 31507c478bd9Sstevel@tonic-gate free(this); 31517c478bd9Sstevel@tonic-gate return (Z_OK); 31527c478bd9Sstevel@tonic-gate } else 31537c478bd9Sstevel@tonic-gate last = this; 31547c478bd9Sstevel@tonic-gate } 31557c478bd9Sstevel@tonic-gate return (Z_NO_PROPERTY_ID); 31567c478bd9Sstevel@tonic-gate } 31577c478bd9Sstevel@tonic-gate 31587c478bd9Sstevel@tonic-gate char * 31597c478bd9Sstevel@tonic-gate zonecfg_strerror(int errnum) 31607c478bd9Sstevel@tonic-gate { 31617c478bd9Sstevel@tonic-gate switch (errnum) { 31627c478bd9Sstevel@tonic-gate case Z_OK: 31637c478bd9Sstevel@tonic-gate return (dgettext(TEXT_DOMAIN, "OK")); 31647c478bd9Sstevel@tonic-gate case Z_EMPTY_DOCUMENT: 31657c478bd9Sstevel@tonic-gate return (dgettext(TEXT_DOMAIN, "Empty document")); 31667c478bd9Sstevel@tonic-gate case Z_WRONG_DOC_TYPE: 31677c478bd9Sstevel@tonic-gate return (dgettext(TEXT_DOMAIN, "Wrong document type")); 31687c478bd9Sstevel@tonic-gate case Z_BAD_PROPERTY: 31697c478bd9Sstevel@tonic-gate return (dgettext(TEXT_DOMAIN, "Bad document property")); 31707c478bd9Sstevel@tonic-gate case Z_TEMP_FILE: 31717c478bd9Sstevel@tonic-gate return (dgettext(TEXT_DOMAIN, 31727c478bd9Sstevel@tonic-gate "Problem creating temporary file")); 31737c478bd9Sstevel@tonic-gate case Z_SAVING_FILE: 31747c478bd9Sstevel@tonic-gate return (dgettext(TEXT_DOMAIN, "Problem saving file")); 31757c478bd9Sstevel@tonic-gate case Z_NO_ENTRY: 31767c478bd9Sstevel@tonic-gate return (dgettext(TEXT_DOMAIN, "No such entry")); 31777c478bd9Sstevel@tonic-gate case Z_BOGUS_ZONE_NAME: 31787c478bd9Sstevel@tonic-gate return (dgettext(TEXT_DOMAIN, "Bogus zone name")); 31797c478bd9Sstevel@tonic-gate case Z_REQD_RESOURCE_MISSING: 31807c478bd9Sstevel@tonic-gate return (dgettext(TEXT_DOMAIN, "Required resource missing")); 31817c478bd9Sstevel@tonic-gate case Z_REQD_PROPERTY_MISSING: 31827c478bd9Sstevel@tonic-gate return (dgettext(TEXT_DOMAIN, "Required property missing")); 31837c478bd9Sstevel@tonic-gate case Z_BAD_HANDLE: 31847c478bd9Sstevel@tonic-gate return (dgettext(TEXT_DOMAIN, "Bad handle")); 31857c478bd9Sstevel@tonic-gate case Z_NOMEM: 31867c478bd9Sstevel@tonic-gate return (dgettext(TEXT_DOMAIN, "Out of memory")); 31877c478bd9Sstevel@tonic-gate case Z_INVAL: 31887c478bd9Sstevel@tonic-gate return (dgettext(TEXT_DOMAIN, "Invalid argument")); 31897c478bd9Sstevel@tonic-gate case Z_ACCES: 31907c478bd9Sstevel@tonic-gate return (dgettext(TEXT_DOMAIN, "Permission denied")); 31917c478bd9Sstevel@tonic-gate case Z_TOO_BIG: 31927c478bd9Sstevel@tonic-gate return (dgettext(TEXT_DOMAIN, "Argument list too long")); 31937c478bd9Sstevel@tonic-gate case Z_MISC_FS: 31947c478bd9Sstevel@tonic-gate return (dgettext(TEXT_DOMAIN, 31957c478bd9Sstevel@tonic-gate "Miscellaneous file system error")); 31967c478bd9Sstevel@tonic-gate case Z_NO_ZONE: 31977c478bd9Sstevel@tonic-gate return (dgettext(TEXT_DOMAIN, "No such zone configured")); 31987c478bd9Sstevel@tonic-gate case Z_NO_RESOURCE_TYPE: 31997c478bd9Sstevel@tonic-gate return (dgettext(TEXT_DOMAIN, "No such resource type")); 32007c478bd9Sstevel@tonic-gate case Z_NO_RESOURCE_ID: 32017c478bd9Sstevel@tonic-gate return (dgettext(TEXT_DOMAIN, "No such resource with that id")); 32027c478bd9Sstevel@tonic-gate case Z_NO_PROPERTY_TYPE: 32037c478bd9Sstevel@tonic-gate return (dgettext(TEXT_DOMAIN, "No such property type")); 32047c478bd9Sstevel@tonic-gate case Z_NO_PROPERTY_ID: 32057c478bd9Sstevel@tonic-gate return (dgettext(TEXT_DOMAIN, "No such property with that id")); 3206087719fdSdp case Z_BAD_ZONE_STATE: 32077c478bd9Sstevel@tonic-gate return (dgettext(TEXT_DOMAIN, 3208087719fdSdp "Zone state is invalid for the requested operation")); 32097c478bd9Sstevel@tonic-gate case Z_INVALID_DOCUMENT: 32107c478bd9Sstevel@tonic-gate return (dgettext(TEXT_DOMAIN, "Invalid document")); 3211087719fdSdp case Z_NAME_IN_USE: 3212087719fdSdp return (dgettext(TEXT_DOMAIN, "Zone name already in use")); 32137c478bd9Sstevel@tonic-gate case Z_NO_SUCH_ID: 32147c478bd9Sstevel@tonic-gate return (dgettext(TEXT_DOMAIN, "No such zone ID")); 32157c478bd9Sstevel@tonic-gate case Z_UPDATING_INDEX: 32167c478bd9Sstevel@tonic-gate return (dgettext(TEXT_DOMAIN, "Problem updating index file")); 32177c478bd9Sstevel@tonic-gate case Z_LOCKING_FILE: 32187c478bd9Sstevel@tonic-gate return (dgettext(TEXT_DOMAIN, "Locking index file")); 32197c478bd9Sstevel@tonic-gate case Z_UNLOCKING_FILE: 32207c478bd9Sstevel@tonic-gate return (dgettext(TEXT_DOMAIN, "Unlocking index file")); 32217c478bd9Sstevel@tonic-gate case Z_INSUFFICIENT_SPEC: 32227c478bd9Sstevel@tonic-gate return (dgettext(TEXT_DOMAIN, "Insufficient specification")); 32237c478bd9Sstevel@tonic-gate case Z_RESOLVED_PATH: 32247c478bd9Sstevel@tonic-gate return (dgettext(TEXT_DOMAIN, "Resolved path mismatch")); 32257c478bd9Sstevel@tonic-gate case Z_IPV6_ADDR_PREFIX_LEN: 32267c478bd9Sstevel@tonic-gate return (dgettext(TEXT_DOMAIN, 32277c478bd9Sstevel@tonic-gate "IPv6 address missing required prefix length")); 32287c478bd9Sstevel@tonic-gate case Z_BOGUS_ADDRESS: 32297c478bd9Sstevel@tonic-gate return (dgettext(TEXT_DOMAIN, 32307c478bd9Sstevel@tonic-gate "Neither an IPv4 nor an IPv6 address nor a host name")); 3231ffbafc53Scomay case Z_PRIV_PROHIBITED: 3232ffbafc53Scomay return (dgettext(TEXT_DOMAIN, 3233ffbafc53Scomay "Specified privilege is prohibited")); 3234ffbafc53Scomay case Z_PRIV_REQUIRED: 3235ffbafc53Scomay return (dgettext(TEXT_DOMAIN, 3236ffbafc53Scomay "Required privilege is missing")); 3237ffbafc53Scomay case Z_PRIV_UNKNOWN: 3238ffbafc53Scomay return (dgettext(TEXT_DOMAIN, 3239ffbafc53Scomay "Specified privilege is unknown")); 32409acbbeafSnn35248 case Z_BRAND_ERROR: 32419acbbeafSnn35248 return (dgettext(TEXT_DOMAIN, 32429acbbeafSnn35248 "Brand-specific error")); 32430209230bSgjelinek case Z_INCOMPATIBLE: 32440209230bSgjelinek return (dgettext(TEXT_DOMAIN, "Incompatible settings")); 32450209230bSgjelinek case Z_ALIAS_DISALLOW: 32460209230bSgjelinek return (dgettext(TEXT_DOMAIN, 32470209230bSgjelinek "An incompatible rctl already exists for this property")); 32480209230bSgjelinek case Z_CLEAR_DISALLOW: 32490209230bSgjelinek return (dgettext(TEXT_DOMAIN, 32500209230bSgjelinek "Clearing this property is not allowed")); 32510209230bSgjelinek case Z_POOL: 32520209230bSgjelinek return (dgettext(TEXT_DOMAIN, "libpool(3LIB) error")); 32530209230bSgjelinek case Z_POOLS_NOT_ACTIVE: 32540209230bSgjelinek return (dgettext(TEXT_DOMAIN, "Pools facility not active; " 32550209230bSgjelinek "zone will not be bound to pool")); 32560209230bSgjelinek case Z_POOL_ENABLE: 32570209230bSgjelinek return (dgettext(TEXT_DOMAIN, 32580209230bSgjelinek "Could not enable pools facility")); 32590209230bSgjelinek case Z_NO_POOL: 32600209230bSgjelinek return (dgettext(TEXT_DOMAIN, 32610209230bSgjelinek "Pool not found; using default pool")); 32620209230bSgjelinek case Z_POOL_CREATE: 32630209230bSgjelinek return (dgettext(TEXT_DOMAIN, 32640209230bSgjelinek "Could not create a temporary pool")); 32650209230bSgjelinek case Z_POOL_BIND: 32660209230bSgjelinek return (dgettext(TEXT_DOMAIN, "Could not bind zone to pool")); 32677c478bd9Sstevel@tonic-gate default: 32687c478bd9Sstevel@tonic-gate return (dgettext(TEXT_DOMAIN, "Unknown error")); 32697c478bd9Sstevel@tonic-gate } 32707c478bd9Sstevel@tonic-gate } 32717c478bd9Sstevel@tonic-gate 32727c478bd9Sstevel@tonic-gate /* 32737c478bd9Sstevel@tonic-gate * Note that the zonecfg_setXent() and zonecfg_endXent() calls are all the 32747c478bd9Sstevel@tonic-gate * same, as they just turn around and call zonecfg_setent() / zonecfg_endent(). 32757c478bd9Sstevel@tonic-gate */ 32767c478bd9Sstevel@tonic-gate 32777c478bd9Sstevel@tonic-gate static int 32787c478bd9Sstevel@tonic-gate zonecfg_setent(zone_dochandle_t handle) 32797c478bd9Sstevel@tonic-gate { 32807c478bd9Sstevel@tonic-gate xmlNodePtr cur; 32817c478bd9Sstevel@tonic-gate int err; 32827c478bd9Sstevel@tonic-gate 32837c478bd9Sstevel@tonic-gate if (handle == NULL) 32847c478bd9Sstevel@tonic-gate return (Z_INVAL); 32857c478bd9Sstevel@tonic-gate 32867c478bd9Sstevel@tonic-gate if ((err = operation_prep(handle)) != Z_OK) { 32877c478bd9Sstevel@tonic-gate handle->zone_dh_cur = NULL; 32887c478bd9Sstevel@tonic-gate return (err); 32897c478bd9Sstevel@tonic-gate } 32907c478bd9Sstevel@tonic-gate cur = handle->zone_dh_cur; 32917c478bd9Sstevel@tonic-gate cur = cur->xmlChildrenNode; 32927c478bd9Sstevel@tonic-gate handle->zone_dh_cur = cur; 32937c478bd9Sstevel@tonic-gate return (Z_OK); 32947c478bd9Sstevel@tonic-gate } 32957c478bd9Sstevel@tonic-gate 32967c478bd9Sstevel@tonic-gate static int 32977c478bd9Sstevel@tonic-gate zonecfg_endent(zone_dochandle_t handle) 32987c478bd9Sstevel@tonic-gate { 32997c478bd9Sstevel@tonic-gate if (handle == NULL) 33007c478bd9Sstevel@tonic-gate return (Z_INVAL); 33017c478bd9Sstevel@tonic-gate 33027c478bd9Sstevel@tonic-gate handle->zone_dh_cur = handle->zone_dh_top; 33037c478bd9Sstevel@tonic-gate return (Z_OK); 33047c478bd9Sstevel@tonic-gate } 33057c478bd9Sstevel@tonic-gate 33060209230bSgjelinek /* 33070209230bSgjelinek * Do the work required to manipulate a process through libproc. 33080209230bSgjelinek * If grab_process() returns no errors (0), then release_process() 33090209230bSgjelinek * must eventually be called. 33100209230bSgjelinek * 33110209230bSgjelinek * Return values: 33120209230bSgjelinek * 0 Successful creation of agent thread 33130209230bSgjelinek * 1 Error grabbing 33140209230bSgjelinek * 2 Error creating agent 33150209230bSgjelinek */ 33160209230bSgjelinek static int 33170209230bSgjelinek grab_process(pr_info_handle_t *p) 33180209230bSgjelinek { 33190209230bSgjelinek int ret; 33200209230bSgjelinek 33210209230bSgjelinek if ((p->pr = Pgrab(p->pid, 0, &ret)) != NULL) { 33220209230bSgjelinek 33230209230bSgjelinek if (Psetflags(p->pr, PR_RLC) != 0) { 33240209230bSgjelinek Prelease(p->pr, 0); 33250209230bSgjelinek return (1); 33260209230bSgjelinek } 33270209230bSgjelinek if (Pcreate_agent(p->pr) == 0) { 33280209230bSgjelinek return (0); 33290209230bSgjelinek 33300209230bSgjelinek } else { 33310209230bSgjelinek Prelease(p->pr, 0); 33320209230bSgjelinek return (2); 33330209230bSgjelinek } 33340209230bSgjelinek } else { 33350209230bSgjelinek return (1); 33360209230bSgjelinek } 33370209230bSgjelinek } 33380209230bSgjelinek 33390209230bSgjelinek /* 33400209230bSgjelinek * Release the specified process. This destroys the agent 33410209230bSgjelinek * and releases the process. If the process is NULL, nothing 33420209230bSgjelinek * is done. This function should only be called if grab_process() 33430209230bSgjelinek * has previously been called and returned success. 33440209230bSgjelinek * 33450209230bSgjelinek * This function is Pgrab-safe. 33460209230bSgjelinek */ 33470209230bSgjelinek static void 33480209230bSgjelinek release_process(struct ps_prochandle *Pr) 33490209230bSgjelinek { 33500209230bSgjelinek if (Pr == NULL) 33510209230bSgjelinek return; 33520209230bSgjelinek 33530209230bSgjelinek Pdestroy_agent(Pr); 33540209230bSgjelinek Prelease(Pr, 0); 33550209230bSgjelinek } 33560209230bSgjelinek 33570209230bSgjelinek static boolean_t 33580209230bSgjelinek grab_zone_proc(char *zonename, pr_info_handle_t *p) 33590209230bSgjelinek { 33600209230bSgjelinek DIR *dirp; 33610209230bSgjelinek struct dirent *dentp; 33620209230bSgjelinek zoneid_t zoneid; 33630209230bSgjelinek int pid_self; 33640209230bSgjelinek psinfo_t psinfo; 33650209230bSgjelinek 33660209230bSgjelinek if (zone_get_id(zonename, &zoneid) != 0) 33670209230bSgjelinek return (B_FALSE); 33680209230bSgjelinek 33690209230bSgjelinek pid_self = getpid(); 33700209230bSgjelinek 33710209230bSgjelinek if ((dirp = opendir("/proc")) == NULL) 33720209230bSgjelinek return (B_FALSE); 33730209230bSgjelinek 33740209230bSgjelinek while (dentp = readdir(dirp)) { 33750209230bSgjelinek p->pid = atoi(dentp->d_name); 33760209230bSgjelinek 33770209230bSgjelinek /* Skip self */ 33780209230bSgjelinek if (p->pid == pid_self) 33790209230bSgjelinek continue; 33800209230bSgjelinek 33810209230bSgjelinek if (proc_get_psinfo(p->pid, &psinfo) != 0) 33820209230bSgjelinek continue; 33830209230bSgjelinek 33840209230bSgjelinek if (psinfo.pr_zoneid != zoneid) 33850209230bSgjelinek continue; 33860209230bSgjelinek 33870209230bSgjelinek /* attempt to grab process */ 33880209230bSgjelinek if (grab_process(p) != 0) 33890209230bSgjelinek continue; 33900209230bSgjelinek 33910209230bSgjelinek if (pr_getzoneid(p->pr) != zoneid) { 33920209230bSgjelinek release_process(p->pr); 33930209230bSgjelinek continue; 33940209230bSgjelinek } 33950209230bSgjelinek 33960209230bSgjelinek (void) closedir(dirp); 33970209230bSgjelinek return (B_TRUE); 33980209230bSgjelinek } 33990209230bSgjelinek 34000209230bSgjelinek (void) closedir(dirp); 34010209230bSgjelinek return (B_FALSE); 34020209230bSgjelinek } 34030209230bSgjelinek 34040209230bSgjelinek static boolean_t 34050209230bSgjelinek get_priv_rctl(struct ps_prochandle *pr, char *name, rctlblk_t *rblk) 34060209230bSgjelinek { 34070209230bSgjelinek if (pr_getrctl(pr, name, NULL, rblk, RCTL_FIRST)) 34080209230bSgjelinek return (B_FALSE); 34090209230bSgjelinek 34100209230bSgjelinek if (rctlblk_get_privilege(rblk) == RCPRIV_PRIVILEGED) 34110209230bSgjelinek return (B_TRUE); 34120209230bSgjelinek 34130209230bSgjelinek while (pr_getrctl(pr, name, rblk, rblk, RCTL_NEXT) == 0) { 34140209230bSgjelinek if (rctlblk_get_privilege(rblk) == RCPRIV_PRIVILEGED) 34150209230bSgjelinek return (B_TRUE); 34160209230bSgjelinek } 34170209230bSgjelinek 34180209230bSgjelinek return (B_FALSE); 34190209230bSgjelinek } 34200209230bSgjelinek 34210209230bSgjelinek /* 34220209230bSgjelinek * Apply the current rctl settings to the specified, running zone. 34230209230bSgjelinek */ 34240209230bSgjelinek int 34250209230bSgjelinek zonecfg_apply_rctls(char *zone_name, zone_dochandle_t handle) 34260209230bSgjelinek { 34270209230bSgjelinek int err; 34280209230bSgjelinek int res = Z_OK; 34290209230bSgjelinek rctlblk_t *rblk; 34300209230bSgjelinek pr_info_handle_t p; 34310209230bSgjelinek struct zone_rctltab rctl; 34320209230bSgjelinek 34330209230bSgjelinek if ((err = zonecfg_setrctlent(handle)) != Z_OK) 34340209230bSgjelinek return (err); 34350209230bSgjelinek 34360209230bSgjelinek if ((rblk = (rctlblk_t *)malloc(rctlblk_size())) == NULL) { 34370209230bSgjelinek (void) zonecfg_endrctlent(handle); 34380209230bSgjelinek return (Z_NOMEM); 34390209230bSgjelinek } 34400209230bSgjelinek 34410209230bSgjelinek if (!grab_zone_proc(zone_name, &p)) { 34420209230bSgjelinek (void) zonecfg_endrctlent(handle); 34430209230bSgjelinek free(rblk); 34440209230bSgjelinek return (Z_SYSTEM); 34450209230bSgjelinek } 34460209230bSgjelinek 34470209230bSgjelinek while (zonecfg_getrctlent(handle, &rctl) == Z_OK) { 34480209230bSgjelinek char *rname; 34490209230bSgjelinek struct zone_rctlvaltab *valptr; 34500209230bSgjelinek 34510209230bSgjelinek rname = rctl.zone_rctl_name; 34520209230bSgjelinek 34530209230bSgjelinek /* first delete all current privileged settings for this rctl */ 34540209230bSgjelinek while (get_priv_rctl(p.pr, rname, rblk)) { 34550209230bSgjelinek if (pr_setrctl(p.pr, rname, NULL, rblk, RCTL_DELETE) != 34560209230bSgjelinek 0) { 34570209230bSgjelinek res = Z_SYSTEM; 34580209230bSgjelinek goto done; 34590209230bSgjelinek } 34600209230bSgjelinek } 34610209230bSgjelinek 34620209230bSgjelinek /* now set each new value for the rctl */ 34630209230bSgjelinek for (valptr = rctl.zone_rctl_valptr; valptr != NULL; 34640209230bSgjelinek valptr = valptr->zone_rctlval_next) { 34650209230bSgjelinek if ((err = zonecfg_construct_rctlblk(valptr, rblk)) 34660209230bSgjelinek != Z_OK) { 34670209230bSgjelinek res = errno = err; 34680209230bSgjelinek goto done; 34690209230bSgjelinek } 34700209230bSgjelinek 34710209230bSgjelinek if (pr_setrctl(p.pr, rname, NULL, rblk, RCTL_INSERT)) { 34720209230bSgjelinek res = Z_SYSTEM; 34730209230bSgjelinek goto done; 34740209230bSgjelinek } 34750209230bSgjelinek } 34760209230bSgjelinek } 34770209230bSgjelinek 34780209230bSgjelinek done: 34790209230bSgjelinek release_process(p.pr); 34800209230bSgjelinek free(rblk); 34810209230bSgjelinek (void) zonecfg_endrctlent(handle); 34820209230bSgjelinek 34830209230bSgjelinek return (res); 34840209230bSgjelinek } 34850209230bSgjelinek 34860209230bSgjelinek static const xmlChar * 34870209230bSgjelinek nm_to_dtd(char *nm) 34880209230bSgjelinek { 34890209230bSgjelinek if (strcmp(nm, "device") == 0) 34900209230bSgjelinek return (DTD_ELEM_DEVICE); 34910209230bSgjelinek if (strcmp(nm, "fs") == 0) 34920209230bSgjelinek return (DTD_ELEM_FS); 34930209230bSgjelinek if (strcmp(nm, "inherit-pkg-dir") == 0) 34940209230bSgjelinek return (DTD_ELEM_IPD); 34950209230bSgjelinek if (strcmp(nm, "net") == 0) 34960209230bSgjelinek return (DTD_ELEM_NET); 34970209230bSgjelinek if (strcmp(nm, "attr") == 0) 34980209230bSgjelinek return (DTD_ELEM_ATTR); 34990209230bSgjelinek if (strcmp(nm, "rctl") == 0) 35000209230bSgjelinek return (DTD_ELEM_RCTL); 35010209230bSgjelinek if (strcmp(nm, "dataset") == 0) 35020209230bSgjelinek return (DTD_ELEM_DATASET); 35030209230bSgjelinek 35040209230bSgjelinek return (NULL); 35050209230bSgjelinek } 35060209230bSgjelinek 35070209230bSgjelinek int 35080209230bSgjelinek zonecfg_num_resources(zone_dochandle_t handle, char *rsrc) 35090209230bSgjelinek { 35100209230bSgjelinek int num = 0; 35110209230bSgjelinek const xmlChar *dtd; 35120209230bSgjelinek xmlNodePtr cur; 35130209230bSgjelinek 35140209230bSgjelinek if ((dtd = nm_to_dtd(rsrc)) == NULL) 35150209230bSgjelinek return (num); 35160209230bSgjelinek 35170209230bSgjelinek if (zonecfg_setent(handle) != Z_OK) 35180209230bSgjelinek return (num); 35190209230bSgjelinek 35200209230bSgjelinek for (cur = handle->zone_dh_cur; cur != NULL; cur = cur->next) 35210209230bSgjelinek if (xmlStrcmp(cur->name, dtd) == 0) 35220209230bSgjelinek num++; 35230209230bSgjelinek 35240209230bSgjelinek (void) zonecfg_endent(handle); 35250209230bSgjelinek 35260209230bSgjelinek return (num); 35270209230bSgjelinek } 35280209230bSgjelinek 35290209230bSgjelinek int 35300209230bSgjelinek zonecfg_del_all_resources(zone_dochandle_t handle, char *rsrc) 35310209230bSgjelinek { 35320209230bSgjelinek int err; 35330209230bSgjelinek const xmlChar *dtd; 35340209230bSgjelinek xmlNodePtr cur; 35350209230bSgjelinek 35360209230bSgjelinek if ((dtd = nm_to_dtd(rsrc)) == NULL) 35370209230bSgjelinek return (Z_NO_RESOURCE_TYPE); 35380209230bSgjelinek 35390209230bSgjelinek if ((err = zonecfg_setent(handle)) != Z_OK) 35400209230bSgjelinek return (err); 35410209230bSgjelinek 35420209230bSgjelinek cur = handle->zone_dh_cur; 35430209230bSgjelinek while (cur != NULL) { 35440209230bSgjelinek xmlNodePtr tmp; 35450209230bSgjelinek 35460209230bSgjelinek if (xmlStrcmp(cur->name, dtd)) { 35470209230bSgjelinek cur = cur->next; 35480209230bSgjelinek continue; 35490209230bSgjelinek } 35500209230bSgjelinek 35510209230bSgjelinek tmp = cur->next; 35520209230bSgjelinek xmlUnlinkNode(cur); 35530209230bSgjelinek xmlFreeNode(cur); 35540209230bSgjelinek cur = tmp; 35550209230bSgjelinek } 35560209230bSgjelinek 35570209230bSgjelinek (void) zonecfg_endent(handle); 35580209230bSgjelinek return (Z_OK); 35590209230bSgjelinek } 35600209230bSgjelinek 35610209230bSgjelinek static boolean_t 35620209230bSgjelinek valid_uint(char *s, uint64_t *n) 35630209230bSgjelinek { 35640209230bSgjelinek char *endp; 35650209230bSgjelinek 35660209230bSgjelinek /* strtoull accepts '-'?! so we want to flag that as an error */ 35670209230bSgjelinek if (strchr(s, '-') != NULL) 35680209230bSgjelinek return (B_FALSE); 35690209230bSgjelinek 35700209230bSgjelinek errno = 0; 35710209230bSgjelinek *n = strtoull(s, &endp, 10); 35720209230bSgjelinek 35730209230bSgjelinek if (errno != 0 || *endp != '\0') 35740209230bSgjelinek return (B_FALSE); 35750209230bSgjelinek return (B_TRUE); 35760209230bSgjelinek } 35770209230bSgjelinek 35780209230bSgjelinek /* 35790209230bSgjelinek * Convert a string representing a number (possibly a fraction) into an integer. 35800209230bSgjelinek * The string can have a modifier (K, M, G or T). The modifiers are treated 35810209230bSgjelinek * as powers of two (not 10). 35820209230bSgjelinek */ 35830209230bSgjelinek int 35840209230bSgjelinek zonecfg_str_to_bytes(char *str, uint64_t *bytes) 35850209230bSgjelinek { 35860209230bSgjelinek long double val; 35870209230bSgjelinek char *unitp; 35880209230bSgjelinek uint64_t scale; 35890209230bSgjelinek 35900209230bSgjelinek if ((val = strtold(str, &unitp)) < 0) 35910209230bSgjelinek return (-1); 35920209230bSgjelinek 35930209230bSgjelinek /* remove any leading white space from units string */ 35940209230bSgjelinek while (isspace(*unitp) != 0) 35950209230bSgjelinek ++unitp; 35960209230bSgjelinek 35970209230bSgjelinek /* if no units explicitly set, error */ 35980209230bSgjelinek if (unitp == NULL || *unitp == '\0') { 35990209230bSgjelinek scale = 1; 36000209230bSgjelinek } else { 36010209230bSgjelinek int i; 36020209230bSgjelinek char *units[] = {"K", "M", "G", "T", NULL}; 36030209230bSgjelinek 36040209230bSgjelinek scale = 1024; 36050209230bSgjelinek 36060209230bSgjelinek /* update scale based on units */ 36070209230bSgjelinek for (i = 0; units[i] != NULL; i++) { 36080209230bSgjelinek if (strcasecmp(unitp, units[i]) == 0) 36090209230bSgjelinek break; 36100209230bSgjelinek scale <<= 10; 36110209230bSgjelinek } 36120209230bSgjelinek 36130209230bSgjelinek if (units[i] == NULL) 36140209230bSgjelinek return (-1); 36150209230bSgjelinek } 36160209230bSgjelinek 36170209230bSgjelinek *bytes = (uint64_t)(val * scale); 36180209230bSgjelinek return (0); 36190209230bSgjelinek } 36200209230bSgjelinek 36210209230bSgjelinek boolean_t 36220209230bSgjelinek zonecfg_valid_ncpus(char *lowstr, char *highstr) 36230209230bSgjelinek { 36240209230bSgjelinek uint64_t low, high; 36250209230bSgjelinek 36260209230bSgjelinek if (!valid_uint(lowstr, &low) || !valid_uint(highstr, &high) || 36270209230bSgjelinek low < 1 || low > high) 36280209230bSgjelinek return (B_FALSE); 36290209230bSgjelinek 36300209230bSgjelinek return (B_TRUE); 36310209230bSgjelinek } 36320209230bSgjelinek 36330209230bSgjelinek boolean_t 36340209230bSgjelinek zonecfg_valid_importance(char *impstr) 36350209230bSgjelinek { 36360209230bSgjelinek uint64_t num; 36370209230bSgjelinek 36380209230bSgjelinek if (!valid_uint(impstr, &num)) 36390209230bSgjelinek return (B_FALSE); 36400209230bSgjelinek 36410209230bSgjelinek return (B_TRUE); 36420209230bSgjelinek } 36430209230bSgjelinek 36440209230bSgjelinek boolean_t 36450209230bSgjelinek zonecfg_valid_alias_limit(char *name, char *limitstr, uint64_t *limit) 36460209230bSgjelinek { 36470209230bSgjelinek int i; 36480209230bSgjelinek 36490209230bSgjelinek for (i = 0; aliases[i].shortname != NULL; i++) 36500209230bSgjelinek if (strcmp(name, aliases[i].shortname) == 0) 36510209230bSgjelinek break; 36520209230bSgjelinek 36530209230bSgjelinek if (aliases[i].shortname == NULL) 36540209230bSgjelinek return (B_FALSE); 36550209230bSgjelinek 36560209230bSgjelinek if (!valid_uint(limitstr, limit) || *limit < aliases[i].low_limit) 36570209230bSgjelinek return (B_FALSE); 36580209230bSgjelinek 36590209230bSgjelinek return (B_TRUE); 36600209230bSgjelinek } 36610209230bSgjelinek 36620209230bSgjelinek boolean_t 36630209230bSgjelinek zonecfg_valid_memlimit(char *memstr, uint64_t *mem_val) 36640209230bSgjelinek { 36650209230bSgjelinek if (zonecfg_str_to_bytes(memstr, mem_val) != 0) 36660209230bSgjelinek return (B_FALSE); 36670209230bSgjelinek 36680209230bSgjelinek return (B_TRUE); 36690209230bSgjelinek } 36700209230bSgjelinek 36710209230bSgjelinek static int 36720209230bSgjelinek zerr_pool(char *pool_err, int err_size, int res) 36730209230bSgjelinek { 36740209230bSgjelinek (void) strlcpy(pool_err, pool_strerror(pool_error()), err_size); 36750209230bSgjelinek return (res); 36760209230bSgjelinek } 36770209230bSgjelinek 36780209230bSgjelinek static int 36790209230bSgjelinek create_tmp_pset(char *pool_err, int err_size, pool_conf_t *pconf, pool_t *pool, 36800209230bSgjelinek char *name, int min, int max) 36810209230bSgjelinek { 36820209230bSgjelinek pool_resource_t *res; 36830209230bSgjelinek pool_elem_t *elem; 36840209230bSgjelinek pool_value_t *val; 36850209230bSgjelinek 36860209230bSgjelinek if ((res = pool_resource_create(pconf, "pset", name)) == NULL) 36870209230bSgjelinek return (zerr_pool(pool_err, err_size, Z_POOL)); 36880209230bSgjelinek 36890209230bSgjelinek if (pool_associate(pconf, pool, res) != PO_SUCCESS) 36900209230bSgjelinek return (zerr_pool(pool_err, err_size, Z_POOL)); 36910209230bSgjelinek 36920209230bSgjelinek if ((elem = pool_resource_to_elem(pconf, res)) == NULL) 36930209230bSgjelinek return (zerr_pool(pool_err, err_size, Z_POOL)); 36940209230bSgjelinek 36950209230bSgjelinek if ((val = pool_value_alloc()) == NULL) 36960209230bSgjelinek return (zerr_pool(pool_err, err_size, Z_POOL)); 36970209230bSgjelinek 36980209230bSgjelinek /* set the maximum number of cpus for the pset */ 36990209230bSgjelinek pool_value_set_uint64(val, (uint64_t)max); 37000209230bSgjelinek 37010209230bSgjelinek if (pool_put_property(pconf, elem, "pset.max", val) != PO_SUCCESS) { 37020209230bSgjelinek pool_value_free(val); 37030209230bSgjelinek return (zerr_pool(pool_err, err_size, Z_POOL)); 37040209230bSgjelinek } 37050209230bSgjelinek 37060209230bSgjelinek /* set the minimum number of cpus for the pset */ 37070209230bSgjelinek pool_value_set_uint64(val, (uint64_t)min); 37080209230bSgjelinek 37090209230bSgjelinek if (pool_put_property(pconf, elem, "pset.min", val) != PO_SUCCESS) { 37100209230bSgjelinek pool_value_free(val); 37110209230bSgjelinek return (zerr_pool(pool_err, err_size, Z_POOL)); 37120209230bSgjelinek } 37130209230bSgjelinek 37140209230bSgjelinek pool_value_free(val); 37150209230bSgjelinek 37160209230bSgjelinek return (Z_OK); 37170209230bSgjelinek } 37180209230bSgjelinek 37190209230bSgjelinek static int 37200209230bSgjelinek create_tmp_pool(char *pool_err, int err_size, pool_conf_t *pconf, char *name, 37210209230bSgjelinek struct zone_psettab *pset_tab) 37220209230bSgjelinek { 37230209230bSgjelinek pool_t *pool; 37240209230bSgjelinek int res = Z_OK; 37250209230bSgjelinek 37260209230bSgjelinek /* create a temporary pool configuration */ 37270209230bSgjelinek if (pool_conf_open(pconf, NULL, PO_TEMP) != PO_SUCCESS) { 37280209230bSgjelinek res = zerr_pool(pool_err, err_size, Z_POOL); 37290209230bSgjelinek return (res); 37300209230bSgjelinek } 37310209230bSgjelinek 37320209230bSgjelinek if ((pool = pool_create(pconf, name)) == NULL) { 37330209230bSgjelinek res = zerr_pool(pool_err, err_size, Z_POOL_CREATE); 37340209230bSgjelinek goto done; 37350209230bSgjelinek } 37360209230bSgjelinek 37370209230bSgjelinek /* set pool importance */ 37380209230bSgjelinek if (pset_tab->zone_importance[0] != '\0') { 37390209230bSgjelinek pool_elem_t *elem; 37400209230bSgjelinek pool_value_t *val; 37410209230bSgjelinek 37420209230bSgjelinek if ((elem = pool_to_elem(pconf, pool)) == NULL) { 37430209230bSgjelinek res = zerr_pool(pool_err, err_size, Z_POOL); 37440209230bSgjelinek goto done; 37450209230bSgjelinek } 37460209230bSgjelinek 37470209230bSgjelinek if ((val = pool_value_alloc()) == NULL) { 37480209230bSgjelinek res = zerr_pool(pool_err, err_size, Z_POOL); 37490209230bSgjelinek goto done; 37500209230bSgjelinek } 37510209230bSgjelinek 37520209230bSgjelinek pool_value_set_int64(val, 37530209230bSgjelinek (int64_t)atoi(pset_tab->zone_importance)); 37540209230bSgjelinek 37550209230bSgjelinek if (pool_put_property(pconf, elem, "pool.importance", val) 37560209230bSgjelinek != PO_SUCCESS) { 37570209230bSgjelinek res = zerr_pool(pool_err, err_size, Z_POOL); 37580209230bSgjelinek pool_value_free(val); 37590209230bSgjelinek goto done; 37600209230bSgjelinek } 37610209230bSgjelinek 37620209230bSgjelinek pool_value_free(val); 37630209230bSgjelinek } 37640209230bSgjelinek 37650209230bSgjelinek if ((res = create_tmp_pset(pool_err, err_size, pconf, pool, name, 37660209230bSgjelinek atoi(pset_tab->zone_ncpu_min), 37670209230bSgjelinek atoi(pset_tab->zone_ncpu_max))) != Z_OK) 37680209230bSgjelinek goto done; 37690209230bSgjelinek 37700209230bSgjelinek /* validation */ 37710209230bSgjelinek if (pool_conf_status(pconf) == POF_INVALID) { 37720209230bSgjelinek res = zerr_pool(pool_err, err_size, Z_POOL); 37730209230bSgjelinek goto done; 37740209230bSgjelinek } 37750209230bSgjelinek 37760209230bSgjelinek /* 37770209230bSgjelinek * This validation is the one we expect to fail if the user specified 37780209230bSgjelinek * an invalid configuration (too many cpus) for this system. 37790209230bSgjelinek */ 37800209230bSgjelinek if (pool_conf_validate(pconf, POV_RUNTIME) != PO_SUCCESS) { 37810209230bSgjelinek res = zerr_pool(pool_err, err_size, Z_POOL_CREATE); 37820209230bSgjelinek goto done; 37830209230bSgjelinek } 37840209230bSgjelinek 37850209230bSgjelinek /* 37860209230bSgjelinek * Commit the dynamic configuration but not the pool configuration 37870209230bSgjelinek * file. 37880209230bSgjelinek */ 37890209230bSgjelinek if (pool_conf_commit(pconf, 1) != PO_SUCCESS) 37900209230bSgjelinek res = zerr_pool(pool_err, err_size, Z_POOL); 37910209230bSgjelinek 37920209230bSgjelinek done: 37930209230bSgjelinek (void) pool_conf_close(pconf); 37940209230bSgjelinek return (res); 37950209230bSgjelinek } 37960209230bSgjelinek 37970209230bSgjelinek static int 37980209230bSgjelinek get_running_tmp_pset(pool_conf_t *pconf, pool_t *pool, pool_resource_t *pset, 37990209230bSgjelinek struct zone_psettab *pset_tab) 38000209230bSgjelinek { 38010209230bSgjelinek int nfound = 0; 38020209230bSgjelinek pool_elem_t *pe; 38030209230bSgjelinek pool_value_t *pv = pool_value_alloc(); 38040209230bSgjelinek uint64_t val_uint; 38050209230bSgjelinek 38060209230bSgjelinek if (pool != NULL) { 38070209230bSgjelinek pe = pool_to_elem(pconf, pool); 38080209230bSgjelinek if (pool_get_property(pconf, pe, "pool.importance", pv) 38090209230bSgjelinek != POC_INVAL) { 38100209230bSgjelinek int64_t val_int; 38110209230bSgjelinek 38120209230bSgjelinek (void) pool_value_get_int64(pv, &val_int); 38130209230bSgjelinek (void) snprintf(pset_tab->zone_importance, 38140209230bSgjelinek sizeof (pset_tab->zone_importance), "%d", val_int); 38150209230bSgjelinek nfound++; 38160209230bSgjelinek } 38170209230bSgjelinek } 38180209230bSgjelinek 38190209230bSgjelinek if (pset != NULL) { 38200209230bSgjelinek pe = pool_resource_to_elem(pconf, pset); 38210209230bSgjelinek if (pool_get_property(pconf, pe, "pset.min", pv) != POC_INVAL) { 38220209230bSgjelinek (void) pool_value_get_uint64(pv, &val_uint); 38230209230bSgjelinek (void) snprintf(pset_tab->zone_ncpu_min, 38240209230bSgjelinek sizeof (pset_tab->zone_ncpu_min), "%u", val_uint); 38250209230bSgjelinek nfound++; 38260209230bSgjelinek } 38270209230bSgjelinek 38280209230bSgjelinek if (pool_get_property(pconf, pe, "pset.max", pv) != POC_INVAL) { 38290209230bSgjelinek (void) pool_value_get_uint64(pv, &val_uint); 38300209230bSgjelinek (void) snprintf(pset_tab->zone_ncpu_max, 38310209230bSgjelinek sizeof (pset_tab->zone_ncpu_max), "%u", val_uint); 38320209230bSgjelinek nfound++; 38330209230bSgjelinek } 38340209230bSgjelinek } 38350209230bSgjelinek 38360209230bSgjelinek pool_value_free(pv); 38370209230bSgjelinek 38380209230bSgjelinek if (nfound == 3) 38390209230bSgjelinek return (PO_SUCCESS); 38400209230bSgjelinek 38410209230bSgjelinek return (PO_FAIL); 38420209230bSgjelinek } 38430209230bSgjelinek 38440209230bSgjelinek /* 38450209230bSgjelinek * Determine if a tmp pool is configured and if so, if the configuration is 38460209230bSgjelinek * still valid or if it has been changed since the tmp pool was created. 38470209230bSgjelinek * If the tmp pool configuration is no longer valid, delete the tmp pool. 38480209230bSgjelinek * 38490209230bSgjelinek * Set *valid=B_TRUE if there is an existing, valid tmp pool configuration. 38500209230bSgjelinek */ 38510209230bSgjelinek static int 38520209230bSgjelinek verify_del_tmp_pool(pool_conf_t *pconf, char *tmp_name, char *pool_err, 38530209230bSgjelinek int err_size, struct zone_psettab *pset_tab, boolean_t *exists) 38540209230bSgjelinek { 38550209230bSgjelinek int res = Z_OK; 38560209230bSgjelinek pool_t *pool; 38570209230bSgjelinek pool_resource_t *pset; 38580209230bSgjelinek struct zone_psettab pset_current; 38590209230bSgjelinek 38600209230bSgjelinek *exists = B_FALSE; 38610209230bSgjelinek 38620209230bSgjelinek if (pool_conf_open(pconf, pool_dynamic_location(), PO_RDWR) 38630209230bSgjelinek != PO_SUCCESS) { 38640209230bSgjelinek res = zerr_pool(pool_err, err_size, Z_POOL); 38650209230bSgjelinek return (res); 38660209230bSgjelinek } 38670209230bSgjelinek 38680209230bSgjelinek pool = pool_get_pool(pconf, tmp_name); 38690209230bSgjelinek pset = pool_get_resource(pconf, "pset", tmp_name); 38700209230bSgjelinek 38710209230bSgjelinek if (pool == NULL && pset == NULL) { 38720209230bSgjelinek /* no tmp pool configured */ 38730209230bSgjelinek goto done; 38740209230bSgjelinek } 38750209230bSgjelinek 38760209230bSgjelinek /* 38770209230bSgjelinek * If an existing tmp pool for this zone is configured with the proper 38780209230bSgjelinek * settings, then the tmp pool is valid. 38790209230bSgjelinek */ 38800209230bSgjelinek if (get_running_tmp_pset(pconf, pool, pset, &pset_current) 38810209230bSgjelinek == PO_SUCCESS && 38820209230bSgjelinek strcmp(pset_tab->zone_ncpu_min, 38830209230bSgjelinek pset_current.zone_ncpu_min) == 0 && 38840209230bSgjelinek strcmp(pset_tab->zone_ncpu_max, 38850209230bSgjelinek pset_current.zone_ncpu_max) == 0 && 38860209230bSgjelinek strcmp(pset_tab->zone_importance, 38870209230bSgjelinek pset_current.zone_importance) == 0) { 38880209230bSgjelinek *exists = B_TRUE; 38890209230bSgjelinek 38900209230bSgjelinek } else { 38910209230bSgjelinek /* 38920209230bSgjelinek * An out-of-date tmp pool configuration exists. Delete it 38930209230bSgjelinek * so that we can create the correct tmp pool config. 38940209230bSgjelinek */ 38950209230bSgjelinek if (pset != NULL && 38960209230bSgjelinek pool_resource_destroy(pconf, pset) != PO_SUCCESS) { 38970209230bSgjelinek res = zerr_pool(pool_err, err_size, Z_POOL); 38980209230bSgjelinek goto done; 38990209230bSgjelinek } 39000209230bSgjelinek 39010209230bSgjelinek if (pool != NULL && 39020209230bSgjelinek pool_destroy(pconf, pool) != PO_SUCCESS) { 39030209230bSgjelinek res = zerr_pool(pool_err, err_size, Z_POOL); 39040209230bSgjelinek goto done; 39050209230bSgjelinek } 39060209230bSgjelinek 39070209230bSgjelinek /* commit dynamic config */ 39080209230bSgjelinek if (pool_conf_commit(pconf, 0) != PO_SUCCESS) 39090209230bSgjelinek res = zerr_pool(pool_err, err_size, Z_POOL); 39100209230bSgjelinek } 39110209230bSgjelinek 39120209230bSgjelinek done: 39130209230bSgjelinek (void) pool_conf_close(pconf); 39140209230bSgjelinek 39150209230bSgjelinek return (res); 39160209230bSgjelinek } 39170209230bSgjelinek 39180209230bSgjelinek /* 39190209230bSgjelinek * Destroy any existing tmp pool. 39200209230bSgjelinek */ 39210209230bSgjelinek int 39220209230bSgjelinek zonecfg_destroy_tmp_pool(char *zone_name, char *pool_err, int err_size) 39230209230bSgjelinek { 39240209230bSgjelinek int status; 39250209230bSgjelinek int res = Z_OK; 39260209230bSgjelinek pool_conf_t *pconf; 39270209230bSgjelinek pool_t *pool; 39280209230bSgjelinek pool_resource_t *pset; 39290209230bSgjelinek char tmp_name[MAX_TMP_POOL_NAME]; 39300209230bSgjelinek 39310209230bSgjelinek /* if pools not enabled then nothing to do */ 39320209230bSgjelinek if (pool_get_status(&status) != PO_SUCCESS || status != POOL_ENABLED) 39330209230bSgjelinek return (Z_OK); 39340209230bSgjelinek 39350209230bSgjelinek if ((pconf = pool_conf_alloc()) == NULL) 39360209230bSgjelinek return (zerr_pool(pool_err, err_size, Z_POOL)); 39370209230bSgjelinek 39380209230bSgjelinek (void) snprintf(tmp_name, sizeof (tmp_name), TMP_POOL_NAME, zone_name); 39390209230bSgjelinek 39400209230bSgjelinek if (pool_conf_open(pconf, pool_dynamic_location(), PO_RDWR) 39410209230bSgjelinek != PO_SUCCESS) { 39420209230bSgjelinek res = zerr_pool(pool_err, err_size, Z_POOL); 39430209230bSgjelinek pool_conf_free(pconf); 39440209230bSgjelinek return (res); 39450209230bSgjelinek } 39460209230bSgjelinek 39470209230bSgjelinek pool = pool_get_pool(pconf, tmp_name); 39480209230bSgjelinek pset = pool_get_resource(pconf, "pset", tmp_name); 39490209230bSgjelinek 39500209230bSgjelinek if (pool == NULL && pset == NULL) { 39510209230bSgjelinek /* nothing to destroy, we're done */ 39520209230bSgjelinek goto done; 39530209230bSgjelinek } 39540209230bSgjelinek 39550209230bSgjelinek if (pset != NULL && pool_resource_destroy(pconf, pset) != PO_SUCCESS) { 39560209230bSgjelinek res = zerr_pool(pool_err, err_size, Z_POOL); 39570209230bSgjelinek goto done; 39580209230bSgjelinek } 39590209230bSgjelinek 39600209230bSgjelinek if (pool != NULL && pool_destroy(pconf, pool) != PO_SUCCESS) { 39610209230bSgjelinek res = zerr_pool(pool_err, err_size, Z_POOL); 39620209230bSgjelinek goto done; 39630209230bSgjelinek } 39640209230bSgjelinek 39650209230bSgjelinek /* commit dynamic config */ 39660209230bSgjelinek if (pool_conf_commit(pconf, 0) != PO_SUCCESS) 39670209230bSgjelinek res = zerr_pool(pool_err, err_size, Z_POOL); 39680209230bSgjelinek 39690209230bSgjelinek done: 39700209230bSgjelinek (void) pool_conf_close(pconf); 39710209230bSgjelinek pool_conf_free(pconf); 39720209230bSgjelinek 39730209230bSgjelinek return (res); 39740209230bSgjelinek } 39750209230bSgjelinek 39760209230bSgjelinek /* 39770209230bSgjelinek * Attempt to bind to a tmp pool for this zone. If there is no tmp pool 39780209230bSgjelinek * configured, we just return Z_OK. 39790209230bSgjelinek * 39800209230bSgjelinek * We either attempt to create the tmp pool for this zone or rebind to an 39810209230bSgjelinek * existing tmp pool for this zone. 39820209230bSgjelinek * 39830209230bSgjelinek * Rebinding is used when a zone with a tmp pool reboots so that we don't have 39840209230bSgjelinek * to recreate the tmp pool. To do this we need to be sure we work correctly 39850209230bSgjelinek * for the following cases: 39860209230bSgjelinek * 39870209230bSgjelinek * - there is an existing, properly configured tmp pool. 39880209230bSgjelinek * - zonecfg added tmp pool after zone was booted, must now create. 39890209230bSgjelinek * - zonecfg updated tmp pool config after zone was booted, in this case 39900209230bSgjelinek * we destroy the old tmp pool and create a new one. 39910209230bSgjelinek */ 39920209230bSgjelinek int 39930209230bSgjelinek zonecfg_bind_tmp_pool(zone_dochandle_t handle, zoneid_t zoneid, char *pool_err, 39940209230bSgjelinek int err_size) 39950209230bSgjelinek { 39960209230bSgjelinek struct zone_psettab pset_tab; 39970209230bSgjelinek int err; 39980209230bSgjelinek int status; 39990209230bSgjelinek pool_conf_t *pconf; 40000209230bSgjelinek boolean_t exists; 40010209230bSgjelinek char zone_name[ZONENAME_MAX]; 40020209230bSgjelinek char tmp_name[MAX_TMP_POOL_NAME]; 40030209230bSgjelinek 40040209230bSgjelinek (void) getzonenamebyid(zoneid, zone_name, sizeof (zone_name)); 40050209230bSgjelinek 40060209230bSgjelinek err = zonecfg_lookup_pset(handle, &pset_tab); 40070209230bSgjelinek 40080209230bSgjelinek /* if no temporary pool configured, we're done */ 40090209230bSgjelinek if (err == Z_NO_ENTRY) 40100209230bSgjelinek return (Z_OK); 40110209230bSgjelinek 40120209230bSgjelinek /* 40130209230bSgjelinek * importance might not have a value but we need to validate it here, 40140209230bSgjelinek * so set the default. 40150209230bSgjelinek */ 40160209230bSgjelinek if (pset_tab.zone_importance[0] == '\0') 40170209230bSgjelinek (void) strlcpy(pset_tab.zone_importance, "1", 40180209230bSgjelinek sizeof (pset_tab.zone_importance)); 40190209230bSgjelinek 40200209230bSgjelinek /* if pools not enabled, enable them now */ 40210209230bSgjelinek if (pool_get_status(&status) != PO_SUCCESS || status != POOL_ENABLED) { 40220209230bSgjelinek if (pool_set_status(POOL_ENABLED) != PO_SUCCESS) 40230209230bSgjelinek return (Z_POOL_ENABLE); 40240209230bSgjelinek } 40250209230bSgjelinek 40260209230bSgjelinek if ((pconf = pool_conf_alloc()) == NULL) 40270209230bSgjelinek return (zerr_pool(pool_err, err_size, Z_POOL)); 40280209230bSgjelinek 40290209230bSgjelinek (void) snprintf(tmp_name, sizeof (tmp_name), TMP_POOL_NAME, zone_name); 40300209230bSgjelinek 40310209230bSgjelinek /* 40320209230bSgjelinek * Check if a valid tmp pool/pset already exists. If so, we just 40330209230bSgjelinek * reuse it. 40340209230bSgjelinek */ 40350209230bSgjelinek if ((err = verify_del_tmp_pool(pconf, tmp_name, pool_err, err_size, 40360209230bSgjelinek &pset_tab, &exists)) != Z_OK) { 40370209230bSgjelinek pool_conf_free(pconf); 40380209230bSgjelinek return (err); 40390209230bSgjelinek } 40400209230bSgjelinek 40410209230bSgjelinek if (!exists) 40420209230bSgjelinek err = create_tmp_pool(pool_err, err_size, pconf, tmp_name, 40430209230bSgjelinek &pset_tab); 40440209230bSgjelinek 40450209230bSgjelinek pool_conf_free(pconf); 40460209230bSgjelinek 40470209230bSgjelinek if (err != Z_OK) 40480209230bSgjelinek return (err); 40490209230bSgjelinek 40500209230bSgjelinek /* Bind the zone to the pool. */ 40510209230bSgjelinek if (pool_set_binding(tmp_name, P_ZONEID, zoneid) != PO_SUCCESS) 40520209230bSgjelinek return (zerr_pool(pool_err, err_size, Z_POOL_BIND)); 40530209230bSgjelinek 40540209230bSgjelinek return (Z_OK); 40550209230bSgjelinek } 40560209230bSgjelinek 40570209230bSgjelinek /* 40580209230bSgjelinek * Attempt to bind to a permanent pool for this zone. If there is no 40590209230bSgjelinek * permanent pool configured, we just return Z_OK. 40600209230bSgjelinek */ 40610209230bSgjelinek int 40620209230bSgjelinek zonecfg_bind_pool(zone_dochandle_t handle, zoneid_t zoneid, char *pool_err, 40630209230bSgjelinek int err_size) 40640209230bSgjelinek { 40650209230bSgjelinek pool_conf_t *poolconf; 40660209230bSgjelinek pool_t *pool; 40670209230bSgjelinek char poolname[MAXPATHLEN]; 40680209230bSgjelinek int status; 40690209230bSgjelinek int error; 40700209230bSgjelinek 40710209230bSgjelinek /* 40720209230bSgjelinek * Find the pool mentioned in the zone configuration, and bind to it. 40730209230bSgjelinek */ 40740209230bSgjelinek error = zonecfg_get_pool(handle, poolname, sizeof (poolname)); 40750209230bSgjelinek if (error == Z_NO_ENTRY || (error == Z_OK && strlen(poolname) == 0)) { 40760209230bSgjelinek /* 40770209230bSgjelinek * The property is not set on the zone, so the pool 40780209230bSgjelinek * should be bound to the default pool. But that's 40790209230bSgjelinek * already done by the kernel, so we can just return. 40800209230bSgjelinek */ 40810209230bSgjelinek return (Z_OK); 40820209230bSgjelinek } 40830209230bSgjelinek if (error != Z_OK) { 40840209230bSgjelinek /* 40850209230bSgjelinek * Not an error, even though it shouldn't be happening. 40860209230bSgjelinek */ 40870209230bSgjelinek return (Z_OK); 40880209230bSgjelinek } 40890209230bSgjelinek /* 40900209230bSgjelinek * Don't do anything if pools aren't enabled. 40910209230bSgjelinek */ 40920209230bSgjelinek if (pool_get_status(&status) != PO_SUCCESS || status != POOL_ENABLED) 40930209230bSgjelinek return (Z_POOLS_NOT_ACTIVE); 40940209230bSgjelinek 40950209230bSgjelinek /* 40960209230bSgjelinek * Try to provide a sane error message if the requested pool doesn't 40970209230bSgjelinek * exist. 40980209230bSgjelinek */ 40990209230bSgjelinek if ((poolconf = pool_conf_alloc()) == NULL) 41000209230bSgjelinek return (zerr_pool(pool_err, err_size, Z_POOL)); 41010209230bSgjelinek 41020209230bSgjelinek if (pool_conf_open(poolconf, pool_dynamic_location(), PO_RDONLY) != 41030209230bSgjelinek PO_SUCCESS) { 41040209230bSgjelinek pool_conf_free(poolconf); 41050209230bSgjelinek return (zerr_pool(pool_err, err_size, Z_POOL)); 41060209230bSgjelinek } 41070209230bSgjelinek pool = pool_get_pool(poolconf, poolname); 41080209230bSgjelinek (void) pool_conf_close(poolconf); 41090209230bSgjelinek pool_conf_free(poolconf); 41100209230bSgjelinek if (pool == NULL) 41110209230bSgjelinek return (Z_NO_POOL); 41120209230bSgjelinek 41130209230bSgjelinek /* 41140209230bSgjelinek * Bind the zone to the pool. 41150209230bSgjelinek */ 41160209230bSgjelinek if (pool_set_binding(poolname, P_ZONEID, zoneid) != PO_SUCCESS) { 41170209230bSgjelinek /* if bind fails, return poolname for the error msg */ 41180209230bSgjelinek (void) strlcpy(pool_err, poolname, err_size); 41190209230bSgjelinek return (Z_POOL_BIND); 41200209230bSgjelinek } 41210209230bSgjelinek 41220209230bSgjelinek return (Z_OK); 41230209230bSgjelinek } 41240209230bSgjelinek 41250209230bSgjelinek 41260209230bSgjelinek static boolean_t 41270209230bSgjelinek svc_enabled(char *svc_name) 41280209230bSgjelinek { 41290209230bSgjelinek scf_simple_prop_t *prop; 41300209230bSgjelinek boolean_t found = B_FALSE; 41310209230bSgjelinek 41320209230bSgjelinek prop = scf_simple_prop_get(NULL, svc_name, SCF_PG_GENERAL, 41330209230bSgjelinek SCF_PROPERTY_ENABLED); 41340209230bSgjelinek 41350209230bSgjelinek if (scf_simple_prop_numvalues(prop) == 1 && 41360209230bSgjelinek *scf_simple_prop_next_boolean(prop) != 0) 41370209230bSgjelinek found = B_TRUE; 41380209230bSgjelinek 41390209230bSgjelinek scf_simple_prop_free(prop); 41400209230bSgjelinek 41410209230bSgjelinek return (found); 41420209230bSgjelinek } 41430209230bSgjelinek 41440209230bSgjelinek /* 41450209230bSgjelinek * If the zone has capped-memory, make sure the rcap service is enabled. 41460209230bSgjelinek */ 41470209230bSgjelinek int 41480209230bSgjelinek zonecfg_enable_rcapd(char *err, int size) 41490209230bSgjelinek { 41500209230bSgjelinek if (!svc_enabled(RCAP_SERVICE) && 41510209230bSgjelinek smf_enable_instance(RCAP_SERVICE, 0) == -1) { 41520209230bSgjelinek (void) strlcpy(err, scf_strerror(scf_error()), size); 41530209230bSgjelinek return (Z_SYSTEM); 41540209230bSgjelinek } 41550209230bSgjelinek 41560209230bSgjelinek return (Z_OK); 41570209230bSgjelinek } 41580209230bSgjelinek 41590209230bSgjelinek /* 41600209230bSgjelinek * Return true if pset has cpu range specified and poold is not enabled. 41610209230bSgjelinek */ 41620209230bSgjelinek boolean_t 41630209230bSgjelinek zonecfg_warn_poold(zone_dochandle_t handle) 41640209230bSgjelinek { 41650209230bSgjelinek struct zone_psettab pset_tab; 41660209230bSgjelinek int min, max; 41670209230bSgjelinek int err; 41680209230bSgjelinek 41690209230bSgjelinek err = zonecfg_lookup_pset(handle, &pset_tab); 41700209230bSgjelinek 41710209230bSgjelinek /* if no temporary pool configured, we're done */ 41720209230bSgjelinek if (err == Z_NO_ENTRY) 41730209230bSgjelinek return (B_FALSE); 41740209230bSgjelinek 41750209230bSgjelinek min = atoi(pset_tab.zone_ncpu_min); 41760209230bSgjelinek max = atoi(pset_tab.zone_ncpu_max); 41770209230bSgjelinek 41780209230bSgjelinek /* range not specified, no need for poold */ 41790209230bSgjelinek if (min == max) 41800209230bSgjelinek return (B_FALSE); 41810209230bSgjelinek 41820209230bSgjelinek /* we have a range, check if poold service is enabled */ 41830209230bSgjelinek if (svc_enabled(POOLD_SERVICE)) 41840209230bSgjelinek return (B_FALSE); 41850209230bSgjelinek 41860209230bSgjelinek return (B_TRUE); 41870209230bSgjelinek } 41880209230bSgjelinek 41890209230bSgjelinek static int 41900209230bSgjelinek get_pool_sched_class(char *poolname, char *class, int clsize) 41910209230bSgjelinek { 41920209230bSgjelinek int status; 41930209230bSgjelinek pool_conf_t *poolconf; 41940209230bSgjelinek pool_t *pool; 41950209230bSgjelinek pool_elem_t *pe; 41960209230bSgjelinek pool_value_t *pv = pool_value_alloc(); 41970209230bSgjelinek const char *sched_str; 41980209230bSgjelinek 41990209230bSgjelinek if (pool_get_status(&status) != PO_SUCCESS || status != POOL_ENABLED) 42000209230bSgjelinek return (Z_NO_POOL); 42010209230bSgjelinek 42020209230bSgjelinek if ((poolconf = pool_conf_alloc()) == NULL) 42030209230bSgjelinek return (Z_NO_POOL); 42040209230bSgjelinek 42050209230bSgjelinek if (pool_conf_open(poolconf, pool_dynamic_location(), PO_RDONLY) != 42060209230bSgjelinek PO_SUCCESS) { 42070209230bSgjelinek pool_conf_free(poolconf); 42080209230bSgjelinek return (Z_NO_POOL); 42090209230bSgjelinek } 42100209230bSgjelinek 42110209230bSgjelinek if ((pool = pool_get_pool(poolconf, poolname)) == NULL) { 42120209230bSgjelinek (void) pool_conf_close(poolconf); 42130209230bSgjelinek pool_conf_free(poolconf); 42140209230bSgjelinek return (Z_NO_POOL); 42150209230bSgjelinek } 42160209230bSgjelinek 42170209230bSgjelinek pe = pool_to_elem(poolconf, pool); 42180209230bSgjelinek if (pool_get_property(poolconf, pe, "pool.scheduler", pv) 42190209230bSgjelinek != POC_INVAL) { 42200209230bSgjelinek (void) pool_value_get_string(pv, &sched_str); 42210209230bSgjelinek if (strlcpy(class, sched_str, clsize) >= clsize) 42220209230bSgjelinek return (Z_TOO_BIG); 42230209230bSgjelinek } 42240209230bSgjelinek 42250209230bSgjelinek (void) pool_conf_close(poolconf); 42260209230bSgjelinek pool_conf_free(poolconf); 42270209230bSgjelinek return (Z_OK); 42280209230bSgjelinek } 42290209230bSgjelinek 42300209230bSgjelinek /* 42310209230bSgjelinek * Get the default scheduling class for the zone. This will either be the 42320209230bSgjelinek * class set on the zone's pool or the system default scheduling class. 42330209230bSgjelinek */ 42340209230bSgjelinek int 42350209230bSgjelinek zonecfg_get_dflt_sched_class(zone_dochandle_t handle, char *class, int clsize) 42360209230bSgjelinek { 42370209230bSgjelinek char poolname[MAXPATHLEN]; 42380209230bSgjelinek 42390209230bSgjelinek if (zonecfg_get_pool(handle, poolname, sizeof (poolname)) == Z_OK) { 42400209230bSgjelinek /* check if the zone's pool specified a sched class */ 42410209230bSgjelinek if (get_pool_sched_class(poolname, class, clsize) == Z_OK) 42420209230bSgjelinek return (Z_OK); 42430209230bSgjelinek } 42440209230bSgjelinek 42450209230bSgjelinek if (priocntl(0, 0, PC_GETDFLCL, class, (uint64_t)clsize) == -1) 42460209230bSgjelinek return (Z_TOO_BIG); 42470209230bSgjelinek 42480209230bSgjelinek return (Z_OK); 42490209230bSgjelinek } 42500209230bSgjelinek 42517c478bd9Sstevel@tonic-gate int 42527c478bd9Sstevel@tonic-gate zonecfg_setfsent(zone_dochandle_t handle) 42537c478bd9Sstevel@tonic-gate { 42547c478bd9Sstevel@tonic-gate return (zonecfg_setent(handle)); 42557c478bd9Sstevel@tonic-gate } 42567c478bd9Sstevel@tonic-gate 42577c478bd9Sstevel@tonic-gate int 42587c478bd9Sstevel@tonic-gate zonecfg_getfsent(zone_dochandle_t handle, struct zone_fstab *tabptr) 42597c478bd9Sstevel@tonic-gate { 42607c478bd9Sstevel@tonic-gate xmlNodePtr cur, options; 42617c478bd9Sstevel@tonic-gate char options_str[MAX_MNTOPT_STR]; 42627c478bd9Sstevel@tonic-gate int err; 42637c478bd9Sstevel@tonic-gate 42647c478bd9Sstevel@tonic-gate if (handle == NULL) 42657c478bd9Sstevel@tonic-gate return (Z_INVAL); 42667c478bd9Sstevel@tonic-gate 42677c478bd9Sstevel@tonic-gate if ((cur = handle->zone_dh_cur) == NULL) 42687c478bd9Sstevel@tonic-gate return (Z_NO_ENTRY); 42697c478bd9Sstevel@tonic-gate 42707c478bd9Sstevel@tonic-gate for (; cur != NULL; cur = cur->next) 42717c478bd9Sstevel@tonic-gate if (!xmlStrcmp(cur->name, DTD_ELEM_FS)) 42727c478bd9Sstevel@tonic-gate break; 42737c478bd9Sstevel@tonic-gate if (cur == NULL) { 42747c478bd9Sstevel@tonic-gate handle->zone_dh_cur = handle->zone_dh_top; 42757c478bd9Sstevel@tonic-gate return (Z_NO_ENTRY); 42767c478bd9Sstevel@tonic-gate } 42777c478bd9Sstevel@tonic-gate 42787c478bd9Sstevel@tonic-gate if ((err = fetchprop(cur, DTD_ATTR_SPECIAL, tabptr->zone_fs_special, 42797c478bd9Sstevel@tonic-gate sizeof (tabptr->zone_fs_special))) != Z_OK) { 42807c478bd9Sstevel@tonic-gate handle->zone_dh_cur = handle->zone_dh_top; 42817c478bd9Sstevel@tonic-gate return (err); 42827c478bd9Sstevel@tonic-gate } 42837c478bd9Sstevel@tonic-gate 42847c478bd9Sstevel@tonic-gate if ((err = fetchprop(cur, DTD_ATTR_RAW, tabptr->zone_fs_raw, 42857c478bd9Sstevel@tonic-gate sizeof (tabptr->zone_fs_raw))) != Z_OK) { 42867c478bd9Sstevel@tonic-gate handle->zone_dh_cur = handle->zone_dh_top; 42877c478bd9Sstevel@tonic-gate return (err); 42887c478bd9Sstevel@tonic-gate } 42897c478bd9Sstevel@tonic-gate 42907c478bd9Sstevel@tonic-gate if ((err = fetchprop(cur, DTD_ATTR_DIR, tabptr->zone_fs_dir, 42917c478bd9Sstevel@tonic-gate sizeof (tabptr->zone_fs_dir))) != Z_OK) { 42927c478bd9Sstevel@tonic-gate handle->zone_dh_cur = handle->zone_dh_top; 42937c478bd9Sstevel@tonic-gate return (err); 42947c478bd9Sstevel@tonic-gate } 42957c478bd9Sstevel@tonic-gate 42967c478bd9Sstevel@tonic-gate if ((err = fetchprop(cur, DTD_ATTR_TYPE, tabptr->zone_fs_type, 42977c478bd9Sstevel@tonic-gate sizeof (tabptr->zone_fs_type))) != Z_OK) { 42987c478bd9Sstevel@tonic-gate handle->zone_dh_cur = handle->zone_dh_top; 42997c478bd9Sstevel@tonic-gate return (err); 43007c478bd9Sstevel@tonic-gate } 43017c478bd9Sstevel@tonic-gate 43027c478bd9Sstevel@tonic-gate /* OK for options to be NULL */ 43037c478bd9Sstevel@tonic-gate tabptr->zone_fs_options = NULL; 43047c478bd9Sstevel@tonic-gate for (options = cur->xmlChildrenNode; options != NULL; 43057c478bd9Sstevel@tonic-gate options = options->next) { 43067c478bd9Sstevel@tonic-gate if (fetchprop(options, DTD_ATTR_NAME, options_str, 43077c478bd9Sstevel@tonic-gate sizeof (options_str)) != Z_OK) 43087c478bd9Sstevel@tonic-gate break; 43097c478bd9Sstevel@tonic-gate if (zonecfg_add_fs_option(tabptr, options_str) != Z_OK) 43107c478bd9Sstevel@tonic-gate break; 43117c478bd9Sstevel@tonic-gate } 43127c478bd9Sstevel@tonic-gate 43137c478bd9Sstevel@tonic-gate handle->zone_dh_cur = cur->next; 43147c478bd9Sstevel@tonic-gate return (Z_OK); 43157c478bd9Sstevel@tonic-gate } 43167c478bd9Sstevel@tonic-gate 43177c478bd9Sstevel@tonic-gate int 43187c478bd9Sstevel@tonic-gate zonecfg_endfsent(zone_dochandle_t handle) 43197c478bd9Sstevel@tonic-gate { 43207c478bd9Sstevel@tonic-gate return (zonecfg_endent(handle)); 43217c478bd9Sstevel@tonic-gate } 43227c478bd9Sstevel@tonic-gate 43237c478bd9Sstevel@tonic-gate int 43247c478bd9Sstevel@tonic-gate zonecfg_setipdent(zone_dochandle_t handle) 43257c478bd9Sstevel@tonic-gate { 43267c478bd9Sstevel@tonic-gate return (zonecfg_setent(handle)); 43277c478bd9Sstevel@tonic-gate } 43287c478bd9Sstevel@tonic-gate 43297c478bd9Sstevel@tonic-gate int 43307c478bd9Sstevel@tonic-gate zonecfg_getipdent(zone_dochandle_t handle, struct zone_fstab *tabptr) 43317c478bd9Sstevel@tonic-gate { 43327c478bd9Sstevel@tonic-gate xmlNodePtr cur; 43337c478bd9Sstevel@tonic-gate int err; 43347c478bd9Sstevel@tonic-gate 43357c478bd9Sstevel@tonic-gate if (handle == NULL) 43367c478bd9Sstevel@tonic-gate return (Z_INVAL); 43377c478bd9Sstevel@tonic-gate 43387c478bd9Sstevel@tonic-gate if ((cur = handle->zone_dh_cur) == NULL) 43397c478bd9Sstevel@tonic-gate return (Z_NO_ENTRY); 43407c478bd9Sstevel@tonic-gate 43417c478bd9Sstevel@tonic-gate for (; cur != NULL; cur = cur->next) 43427c478bd9Sstevel@tonic-gate if (!xmlStrcmp(cur->name, DTD_ELEM_IPD)) 43437c478bd9Sstevel@tonic-gate break; 43447c478bd9Sstevel@tonic-gate if (cur == NULL) { 43457c478bd9Sstevel@tonic-gate handle->zone_dh_cur = handle->zone_dh_top; 43467c478bd9Sstevel@tonic-gate return (Z_NO_ENTRY); 43477c478bd9Sstevel@tonic-gate } 43487c478bd9Sstevel@tonic-gate 43497c478bd9Sstevel@tonic-gate if ((err = fetchprop(cur, DTD_ATTR_DIR, tabptr->zone_fs_dir, 43507c478bd9Sstevel@tonic-gate sizeof (tabptr->zone_fs_dir))) != Z_OK) { 43517c478bd9Sstevel@tonic-gate handle->zone_dh_cur = handle->zone_dh_top; 43527c478bd9Sstevel@tonic-gate return (err); 43537c478bd9Sstevel@tonic-gate } 43547c478bd9Sstevel@tonic-gate 43557c478bd9Sstevel@tonic-gate handle->zone_dh_cur = cur->next; 43567c478bd9Sstevel@tonic-gate return (Z_OK); 43577c478bd9Sstevel@tonic-gate } 43587c478bd9Sstevel@tonic-gate 43597c478bd9Sstevel@tonic-gate int 43607c478bd9Sstevel@tonic-gate zonecfg_endipdent(zone_dochandle_t handle) 43617c478bd9Sstevel@tonic-gate { 43627c478bd9Sstevel@tonic-gate return (zonecfg_endent(handle)); 43637c478bd9Sstevel@tonic-gate } 43647c478bd9Sstevel@tonic-gate 43657c478bd9Sstevel@tonic-gate int 43667c478bd9Sstevel@tonic-gate zonecfg_setnwifent(zone_dochandle_t handle) 43677c478bd9Sstevel@tonic-gate { 43687c478bd9Sstevel@tonic-gate return (zonecfg_setent(handle)); 43697c478bd9Sstevel@tonic-gate } 43707c478bd9Sstevel@tonic-gate 43717c478bd9Sstevel@tonic-gate int 43727c478bd9Sstevel@tonic-gate zonecfg_getnwifent(zone_dochandle_t handle, struct zone_nwiftab *tabptr) 43737c478bd9Sstevel@tonic-gate { 43747c478bd9Sstevel@tonic-gate xmlNodePtr cur; 43757c478bd9Sstevel@tonic-gate int err; 43767c478bd9Sstevel@tonic-gate 43777c478bd9Sstevel@tonic-gate if (handle == NULL) 43787c478bd9Sstevel@tonic-gate return (Z_INVAL); 43797c478bd9Sstevel@tonic-gate 43807c478bd9Sstevel@tonic-gate if ((cur = handle->zone_dh_cur) == NULL) 43817c478bd9Sstevel@tonic-gate return (Z_NO_ENTRY); 43827c478bd9Sstevel@tonic-gate 43837c478bd9Sstevel@tonic-gate for (; cur != NULL; cur = cur->next) 43847c478bd9Sstevel@tonic-gate if (!xmlStrcmp(cur->name, DTD_ELEM_NET)) 43857c478bd9Sstevel@tonic-gate break; 43867c478bd9Sstevel@tonic-gate if (cur == NULL) { 43877c478bd9Sstevel@tonic-gate handle->zone_dh_cur = handle->zone_dh_top; 43887c478bd9Sstevel@tonic-gate return (Z_NO_ENTRY); 43897c478bd9Sstevel@tonic-gate } 43907c478bd9Sstevel@tonic-gate 43917c478bd9Sstevel@tonic-gate if ((err = fetchprop(cur, DTD_ATTR_ADDRESS, tabptr->zone_nwif_address, 43927c478bd9Sstevel@tonic-gate sizeof (tabptr->zone_nwif_address))) != Z_OK) { 43937c478bd9Sstevel@tonic-gate handle->zone_dh_cur = handle->zone_dh_top; 43947c478bd9Sstevel@tonic-gate return (err); 43957c478bd9Sstevel@tonic-gate } 43967c478bd9Sstevel@tonic-gate 43977c478bd9Sstevel@tonic-gate if ((err = fetchprop(cur, DTD_ATTR_PHYSICAL, tabptr->zone_nwif_physical, 43987c478bd9Sstevel@tonic-gate sizeof (tabptr->zone_nwif_physical))) != Z_OK) { 43997c478bd9Sstevel@tonic-gate handle->zone_dh_cur = handle->zone_dh_top; 44007c478bd9Sstevel@tonic-gate return (err); 44017c478bd9Sstevel@tonic-gate } 44027c478bd9Sstevel@tonic-gate 4403de860bd9Sgfaden if ((err = fetchprop(cur, DTD_ATTR_DEFROUTER, 4404de860bd9Sgfaden tabptr->zone_nwif_defrouter, 4405de860bd9Sgfaden sizeof (tabptr->zone_nwif_defrouter))) != Z_OK) { 4406de860bd9Sgfaden handle->zone_dh_cur = handle->zone_dh_top; 4407de860bd9Sgfaden return (err); 4408de860bd9Sgfaden } 4409de860bd9Sgfaden 44107c478bd9Sstevel@tonic-gate handle->zone_dh_cur = cur->next; 44117c478bd9Sstevel@tonic-gate return (Z_OK); 44127c478bd9Sstevel@tonic-gate } 44137c478bd9Sstevel@tonic-gate 44147c478bd9Sstevel@tonic-gate int 44157c478bd9Sstevel@tonic-gate zonecfg_endnwifent(zone_dochandle_t handle) 44167c478bd9Sstevel@tonic-gate { 44177c478bd9Sstevel@tonic-gate return (zonecfg_endent(handle)); 44187c478bd9Sstevel@tonic-gate } 44197c478bd9Sstevel@tonic-gate 44207c478bd9Sstevel@tonic-gate int 44217c478bd9Sstevel@tonic-gate zonecfg_setdevent(zone_dochandle_t handle) 44227c478bd9Sstevel@tonic-gate { 44237c478bd9Sstevel@tonic-gate return (zonecfg_setent(handle)); 44247c478bd9Sstevel@tonic-gate } 44257c478bd9Sstevel@tonic-gate 44267c478bd9Sstevel@tonic-gate int 44277c478bd9Sstevel@tonic-gate zonecfg_getdevent(zone_dochandle_t handle, struct zone_devtab *tabptr) 44287c478bd9Sstevel@tonic-gate { 44297c478bd9Sstevel@tonic-gate xmlNodePtr cur; 44307c478bd9Sstevel@tonic-gate int err; 44317c478bd9Sstevel@tonic-gate 44327c478bd9Sstevel@tonic-gate if (handle == NULL) 44337c478bd9Sstevel@tonic-gate return (Z_INVAL); 44347c478bd9Sstevel@tonic-gate 44357c478bd9Sstevel@tonic-gate if ((cur = handle->zone_dh_cur) == NULL) 44367c478bd9Sstevel@tonic-gate return (Z_NO_ENTRY); 44377c478bd9Sstevel@tonic-gate 44387c478bd9Sstevel@tonic-gate for (; cur != NULL; cur = cur->next) 44397c478bd9Sstevel@tonic-gate if (!xmlStrcmp(cur->name, DTD_ELEM_DEVICE)) 44407c478bd9Sstevel@tonic-gate break; 44417c478bd9Sstevel@tonic-gate if (cur == NULL) { 44427c478bd9Sstevel@tonic-gate handle->zone_dh_cur = handle->zone_dh_top; 44437c478bd9Sstevel@tonic-gate return (Z_NO_ENTRY); 44447c478bd9Sstevel@tonic-gate } 44457c478bd9Sstevel@tonic-gate 44467c478bd9Sstevel@tonic-gate if ((err = fetchprop(cur, DTD_ATTR_MATCH, tabptr->zone_dev_match, 44477c478bd9Sstevel@tonic-gate sizeof (tabptr->zone_dev_match))) != Z_OK) { 44487c478bd9Sstevel@tonic-gate handle->zone_dh_cur = handle->zone_dh_top; 44497c478bd9Sstevel@tonic-gate return (err); 44507c478bd9Sstevel@tonic-gate } 44517c478bd9Sstevel@tonic-gate 44527c478bd9Sstevel@tonic-gate handle->zone_dh_cur = cur->next; 44537c478bd9Sstevel@tonic-gate return (Z_OK); 44547c478bd9Sstevel@tonic-gate } 44557c478bd9Sstevel@tonic-gate 44567c478bd9Sstevel@tonic-gate int 44577c478bd9Sstevel@tonic-gate zonecfg_enddevent(zone_dochandle_t handle) 44587c478bd9Sstevel@tonic-gate { 44597c478bd9Sstevel@tonic-gate return (zonecfg_endent(handle)); 44607c478bd9Sstevel@tonic-gate } 44617c478bd9Sstevel@tonic-gate 44627c478bd9Sstevel@tonic-gate int 44637c478bd9Sstevel@tonic-gate zonecfg_setrctlent(zone_dochandle_t handle) 44647c478bd9Sstevel@tonic-gate { 44657c478bd9Sstevel@tonic-gate return (zonecfg_setent(handle)); 44667c478bd9Sstevel@tonic-gate } 44677c478bd9Sstevel@tonic-gate 44687c478bd9Sstevel@tonic-gate int 44697c478bd9Sstevel@tonic-gate zonecfg_getrctlent(zone_dochandle_t handle, struct zone_rctltab *tabptr) 44707c478bd9Sstevel@tonic-gate { 44717c478bd9Sstevel@tonic-gate xmlNodePtr cur, val; 44727c478bd9Sstevel@tonic-gate struct zone_rctlvaltab *valptr; 44737c478bd9Sstevel@tonic-gate int err; 44747c478bd9Sstevel@tonic-gate 44757c478bd9Sstevel@tonic-gate if (handle == NULL) 44767c478bd9Sstevel@tonic-gate return (Z_INVAL); 44777c478bd9Sstevel@tonic-gate 44787c478bd9Sstevel@tonic-gate if ((cur = handle->zone_dh_cur) == NULL) 44797c478bd9Sstevel@tonic-gate return (Z_NO_ENTRY); 44807c478bd9Sstevel@tonic-gate 44817c478bd9Sstevel@tonic-gate for (; cur != NULL; cur = cur->next) 44827c478bd9Sstevel@tonic-gate if (!xmlStrcmp(cur->name, DTD_ELEM_RCTL)) 44837c478bd9Sstevel@tonic-gate break; 44847c478bd9Sstevel@tonic-gate if (cur == NULL) { 44857c478bd9Sstevel@tonic-gate handle->zone_dh_cur = handle->zone_dh_top; 44867c478bd9Sstevel@tonic-gate return (Z_NO_ENTRY); 44877c478bd9Sstevel@tonic-gate } 44887c478bd9Sstevel@tonic-gate 44897c478bd9Sstevel@tonic-gate if ((err = fetchprop(cur, DTD_ATTR_NAME, tabptr->zone_rctl_name, 44907c478bd9Sstevel@tonic-gate sizeof (tabptr->zone_rctl_name))) != Z_OK) { 44917c478bd9Sstevel@tonic-gate handle->zone_dh_cur = handle->zone_dh_top; 44927c478bd9Sstevel@tonic-gate return (err); 44937c478bd9Sstevel@tonic-gate } 44947c478bd9Sstevel@tonic-gate 44957c478bd9Sstevel@tonic-gate tabptr->zone_rctl_valptr = NULL; 44967c478bd9Sstevel@tonic-gate for (val = cur->xmlChildrenNode; val != NULL; val = val->next) { 44977c478bd9Sstevel@tonic-gate valptr = (struct zone_rctlvaltab *)malloc( 44987c478bd9Sstevel@tonic-gate sizeof (struct zone_rctlvaltab)); 44997c478bd9Sstevel@tonic-gate if (valptr == NULL) 45007c478bd9Sstevel@tonic-gate return (Z_NOMEM); 45017c478bd9Sstevel@tonic-gate if (fetchprop(val, DTD_ATTR_PRIV, valptr->zone_rctlval_priv, 45027c478bd9Sstevel@tonic-gate sizeof (valptr->zone_rctlval_priv)) != Z_OK) 45037c478bd9Sstevel@tonic-gate break; 45047c478bd9Sstevel@tonic-gate if (fetchprop(val, DTD_ATTR_LIMIT, valptr->zone_rctlval_limit, 45057c478bd9Sstevel@tonic-gate sizeof (valptr->zone_rctlval_limit)) != Z_OK) 45067c478bd9Sstevel@tonic-gate break; 45077c478bd9Sstevel@tonic-gate if (fetchprop(val, DTD_ATTR_ACTION, valptr->zone_rctlval_action, 45087c478bd9Sstevel@tonic-gate sizeof (valptr->zone_rctlval_action)) != Z_OK) 45097c478bd9Sstevel@tonic-gate break; 45107c478bd9Sstevel@tonic-gate if (zonecfg_add_rctl_value(tabptr, valptr) != Z_OK) 45117c478bd9Sstevel@tonic-gate break; 45127c478bd9Sstevel@tonic-gate } 45137c478bd9Sstevel@tonic-gate 45147c478bd9Sstevel@tonic-gate handle->zone_dh_cur = cur->next; 45157c478bd9Sstevel@tonic-gate return (Z_OK); 45167c478bd9Sstevel@tonic-gate } 45177c478bd9Sstevel@tonic-gate 45187c478bd9Sstevel@tonic-gate int 45197c478bd9Sstevel@tonic-gate zonecfg_endrctlent(zone_dochandle_t handle) 45207c478bd9Sstevel@tonic-gate { 45217c478bd9Sstevel@tonic-gate return (zonecfg_endent(handle)); 45227c478bd9Sstevel@tonic-gate } 45237c478bd9Sstevel@tonic-gate 45247c478bd9Sstevel@tonic-gate int 45257c478bd9Sstevel@tonic-gate zonecfg_setattrent(zone_dochandle_t handle) 45267c478bd9Sstevel@tonic-gate { 45277c478bd9Sstevel@tonic-gate return (zonecfg_setent(handle)); 45287c478bd9Sstevel@tonic-gate } 45297c478bd9Sstevel@tonic-gate 45307c478bd9Sstevel@tonic-gate int 45317c478bd9Sstevel@tonic-gate zonecfg_getattrent(zone_dochandle_t handle, struct zone_attrtab *tabptr) 45327c478bd9Sstevel@tonic-gate { 45337c478bd9Sstevel@tonic-gate xmlNodePtr cur; 45347c478bd9Sstevel@tonic-gate int err; 45357c478bd9Sstevel@tonic-gate 45367c478bd9Sstevel@tonic-gate if (handle == NULL) 45377c478bd9Sstevel@tonic-gate return (Z_INVAL); 45387c478bd9Sstevel@tonic-gate 45397c478bd9Sstevel@tonic-gate if ((cur = handle->zone_dh_cur) == NULL) 45407c478bd9Sstevel@tonic-gate return (Z_NO_ENTRY); 45417c478bd9Sstevel@tonic-gate 45427c478bd9Sstevel@tonic-gate for (; cur != NULL; cur = cur->next) 45437c478bd9Sstevel@tonic-gate if (!xmlStrcmp(cur->name, DTD_ELEM_ATTR)) 45447c478bd9Sstevel@tonic-gate break; 45457c478bd9Sstevel@tonic-gate if (cur == NULL) { 45467c478bd9Sstevel@tonic-gate handle->zone_dh_cur = handle->zone_dh_top; 45477c478bd9Sstevel@tonic-gate return (Z_NO_ENTRY); 45487c478bd9Sstevel@tonic-gate } 45497c478bd9Sstevel@tonic-gate 45507c478bd9Sstevel@tonic-gate if ((err = fetchprop(cur, DTD_ATTR_NAME, tabptr->zone_attr_name, 45517c478bd9Sstevel@tonic-gate sizeof (tabptr->zone_attr_name))) != Z_OK) { 45527c478bd9Sstevel@tonic-gate handle->zone_dh_cur = handle->zone_dh_top; 45537c478bd9Sstevel@tonic-gate return (err); 45547c478bd9Sstevel@tonic-gate } 45557c478bd9Sstevel@tonic-gate 45567c478bd9Sstevel@tonic-gate if ((err = fetchprop(cur, DTD_ATTR_TYPE, tabptr->zone_attr_type, 45577c478bd9Sstevel@tonic-gate sizeof (tabptr->zone_attr_type))) != Z_OK) { 45587c478bd9Sstevel@tonic-gate handle->zone_dh_cur = handle->zone_dh_top; 45597c478bd9Sstevel@tonic-gate return (err); 45607c478bd9Sstevel@tonic-gate } 45617c478bd9Sstevel@tonic-gate 45627c478bd9Sstevel@tonic-gate if ((err = fetchprop(cur, DTD_ATTR_VALUE, tabptr->zone_attr_value, 45637c478bd9Sstevel@tonic-gate sizeof (tabptr->zone_attr_value))) != Z_OK) { 45647c478bd9Sstevel@tonic-gate handle->zone_dh_cur = handle->zone_dh_top; 45657c478bd9Sstevel@tonic-gate return (err); 45667c478bd9Sstevel@tonic-gate } 45677c478bd9Sstevel@tonic-gate 45687c478bd9Sstevel@tonic-gate handle->zone_dh_cur = cur->next; 45697c478bd9Sstevel@tonic-gate return (Z_OK); 45707c478bd9Sstevel@tonic-gate } 45717c478bd9Sstevel@tonic-gate 45727c478bd9Sstevel@tonic-gate int 45737c478bd9Sstevel@tonic-gate zonecfg_endattrent(zone_dochandle_t handle) 45747c478bd9Sstevel@tonic-gate { 45757c478bd9Sstevel@tonic-gate return (zonecfg_endent(handle)); 45767c478bd9Sstevel@tonic-gate } 45777c478bd9Sstevel@tonic-gate 4578ffbafc53Scomay /* 4579ffbafc53Scomay * The privileges available on the system and described in privileges(5) 45809acbbeafSnn35248 * fall into four categories with respect to non-global zones: 45819acbbeafSnn35248 * 45829acbbeafSnn35248 * Default set of privileges considered safe for all non-global 45839acbbeafSnn35248 * zones. These privileges are "safe" in the sense that a 45849acbbeafSnn35248 * privileged process in the zone cannot affect processes in any 45859acbbeafSnn35248 * other zone on the system. 45869acbbeafSnn35248 * 45879acbbeafSnn35248 * Set of privileges not currently permitted within a non-global 45889acbbeafSnn35248 * zone. These privileges are considered by default, "unsafe," 45899acbbeafSnn35248 * and include ones which affect global resources (such as the 45909acbbeafSnn35248 * system clock or physical memory) or are overly broad and cover 45919acbbeafSnn35248 * more than one mechanism in the system. In other cases, there 45929acbbeafSnn35248 * has not been sufficient virtualization in the parts of the 45939acbbeafSnn35248 * system the privilege covers to allow its use within a 45949acbbeafSnn35248 * non-global zone. 45959acbbeafSnn35248 * 45969acbbeafSnn35248 * Set of privileges required in order to get a zone booted and 45979acbbeafSnn35248 * init(1M) started. These cannot be removed from the zone's 45989acbbeafSnn35248 * privilege set. 45999acbbeafSnn35248 * 46009acbbeafSnn35248 * All other privileges are optional and are potentially useful for 4601ffbafc53Scomay * processes executing inside a non-global zone. 4602ffbafc53Scomay * 4603ffbafc53Scomay * When privileges are added to the system, a determination needs to be 4604ffbafc53Scomay * made as to which category the privilege belongs to. Ideally, 4605ffbafc53Scomay * privileges should be fine-grained enough and the mechanisms they cover 4606ffbafc53Scomay * virtualized enough so that they can be made available to non-global 4607ffbafc53Scomay * zones. 4608ffbafc53Scomay */ 4609ffbafc53Scomay 4610ffbafc53Scomay /* 4611ffbafc53Scomay * Define some of the tokens that priv_str_to_set(3C) recognizes. Since 4612ffbafc53Scomay * the privilege string separator can be any character, although it is 4613ffbafc53Scomay * usually a comma character, define these here as well in the event that 4614ffbafc53Scomay * they change or are augmented in the future. 4615ffbafc53Scomay */ 4616ffbafc53Scomay #define BASIC_TOKEN "basic" 4617ffbafc53Scomay #define DEFAULT_TOKEN "default" 4618ffbafc53Scomay #define ZONE_TOKEN "zone" 4619ffbafc53Scomay #define TOKEN_PRIV_CHAR ',' 4620ffbafc53Scomay #define TOKEN_PRIV_STR "," 4621ffbafc53Scomay 46229acbbeafSnn35248 typedef struct priv_node { 46239acbbeafSnn35248 struct priv_node *pn_next; /* Next privilege */ 46249acbbeafSnn35248 char *pn_priv; /* Privileges name */ 46259acbbeafSnn35248 } priv_node_t; 46269acbbeafSnn35248 46279acbbeafSnn35248 /* Privileges lists can differ across brands */ 46289acbbeafSnn35248 typedef struct priv_lists { 46299acbbeafSnn35248 /* Privileges considered safe for all non-global zones of a brand */ 46309acbbeafSnn35248 struct priv_node *pl_default; 46319acbbeafSnn35248 46329acbbeafSnn35248 /* Privileges not permitted for all non-global zones of a brand */ 46339acbbeafSnn35248 struct priv_node *pl_prohibited; 46349acbbeafSnn35248 46359acbbeafSnn35248 /* Privileges required for all non-global zones of a brand */ 46369acbbeafSnn35248 struct priv_node *pl_required; 4637bf1d7e28Sdh155122 4638bf1d7e28Sdh155122 /* 4639bf1d7e28Sdh155122 * ip-type of the zone these privileges lists apply to. 4640bf1d7e28Sdh155122 * It is used to pass ip-type to the callback function, 4641bf1d7e28Sdh155122 * priv_lists_cb, which has no way of getting the ip-type. 4642bf1d7e28Sdh155122 */ 4643bf1d7e28Sdh155122 const char *pl_iptype; 46449acbbeafSnn35248 } priv_lists_t; 46459acbbeafSnn35248 46469acbbeafSnn35248 static int 4647bf1d7e28Sdh155122 priv_lists_cb(void *data, priv_iter_t *priv_iter) 46487c478bd9Sstevel@tonic-gate { 46499acbbeafSnn35248 priv_lists_t *plp = (priv_lists_t *)data; 46509acbbeafSnn35248 priv_node_t *pnp; 46519acbbeafSnn35248 4652bf1d7e28Sdh155122 /* Skip this privilege if ip-type does not match */ 4653bf1d7e28Sdh155122 if ((strcmp(priv_iter->pi_iptype, "all") != 0) && 4654bf1d7e28Sdh155122 (strcmp(priv_iter->pi_iptype, plp->pl_iptype) != 0)) 4655bf1d7e28Sdh155122 return (0); 4656bf1d7e28Sdh155122 46579acbbeafSnn35248 /* Allocate a new priv list node. */ 46589acbbeafSnn35248 if ((pnp = malloc(sizeof (*pnp))) == NULL) 46599acbbeafSnn35248 return (-1); 4660bf1d7e28Sdh155122 if ((pnp->pn_priv = strdup(priv_iter->pi_name)) == NULL) { 46619acbbeafSnn35248 free(pnp); 46629acbbeafSnn35248 return (-1); 46639acbbeafSnn35248 } 46649acbbeafSnn35248 46659acbbeafSnn35248 /* Insert the new priv list node into the right list */ 4666bf1d7e28Sdh155122 if (strcmp(priv_iter->pi_set, "default") == 0) { 46679acbbeafSnn35248 pnp->pn_next = plp->pl_default; 46689acbbeafSnn35248 plp->pl_default = pnp; 4669bf1d7e28Sdh155122 } else if (strcmp(priv_iter->pi_set, "prohibited") == 0) { 46709acbbeafSnn35248 pnp->pn_next = plp->pl_prohibited; 46719acbbeafSnn35248 plp->pl_prohibited = pnp; 4672bf1d7e28Sdh155122 } else if (strcmp(priv_iter->pi_set, "required") == 0) { 46739acbbeafSnn35248 pnp->pn_next = plp->pl_required; 46749acbbeafSnn35248 plp->pl_required = pnp; 46759acbbeafSnn35248 } else { 46769acbbeafSnn35248 free(pnp->pn_priv); 46779acbbeafSnn35248 free(pnp); 46789acbbeafSnn35248 return (-1); 46799acbbeafSnn35248 } 46809acbbeafSnn35248 return (0); 46819acbbeafSnn35248 } 46829acbbeafSnn35248 46839acbbeafSnn35248 static void 46849acbbeafSnn35248 priv_lists_destroy(priv_lists_t *plp) 46859acbbeafSnn35248 { 46869acbbeafSnn35248 priv_node_t *pnp; 46879acbbeafSnn35248 46889acbbeafSnn35248 assert(plp != NULL); 46899acbbeafSnn35248 46909acbbeafSnn35248 while ((pnp = plp->pl_default) != NULL) { 46919acbbeafSnn35248 plp->pl_default = pnp->pn_next; 46929acbbeafSnn35248 free(pnp->pn_priv); 46939acbbeafSnn35248 free(pnp); 46949acbbeafSnn35248 } 46959acbbeafSnn35248 while ((pnp = plp->pl_prohibited) != NULL) { 46969acbbeafSnn35248 plp->pl_prohibited = pnp->pn_next; 46979acbbeafSnn35248 free(pnp->pn_priv); 46989acbbeafSnn35248 free(pnp); 46999acbbeafSnn35248 } 47009acbbeafSnn35248 while ((pnp = plp->pl_required) != NULL) { 47019acbbeafSnn35248 plp->pl_required = pnp->pn_next; 47029acbbeafSnn35248 free(pnp->pn_priv); 47039acbbeafSnn35248 free(pnp); 47049acbbeafSnn35248 } 47059acbbeafSnn35248 free(plp); 47069acbbeafSnn35248 } 47079acbbeafSnn35248 47089acbbeafSnn35248 static int 4709bf1d7e28Sdh155122 priv_lists_create(zone_dochandle_t handle, priv_lists_t **plpp, 4710bf1d7e28Sdh155122 const char *curr_iptype) 47119acbbeafSnn35248 { 47129acbbeafSnn35248 priv_lists_t *plp; 4713123807fbSedp brand_handle_t bh; 47149acbbeafSnn35248 char brand[MAXNAMELEN]; 47159acbbeafSnn35248 47169acbbeafSnn35248 if (handle != NULL) { 47179acbbeafSnn35248 if (zonecfg_get_brand(handle, brand, sizeof (brand)) != 0) 47189acbbeafSnn35248 return (Z_BRAND_ERROR); 47199acbbeafSnn35248 } else { 47209acbbeafSnn35248 (void) strlcpy(brand, NATIVE_BRAND_NAME, MAXNAMELEN); 47219acbbeafSnn35248 } 47229acbbeafSnn35248 4723123807fbSedp if ((bh = brand_open(brand)) == NULL) 47249acbbeafSnn35248 return (Z_BRAND_ERROR); 47259acbbeafSnn35248 47269acbbeafSnn35248 if ((plp = calloc(1, sizeof (priv_lists_t))) == NULL) { 4727123807fbSedp brand_close(bh); 47289acbbeafSnn35248 return (Z_NOMEM); 47299acbbeafSnn35248 } 47309acbbeafSnn35248 4731bf1d7e28Sdh155122 plp->pl_iptype = curr_iptype; 4732bf1d7e28Sdh155122 47339acbbeafSnn35248 /* construct the privilege lists */ 4734123807fbSedp if (brand_config_iter_privilege(bh, priv_lists_cb, plp) != 0) { 47359acbbeafSnn35248 priv_lists_destroy(plp); 4736123807fbSedp brand_close(bh); 47379acbbeafSnn35248 return (Z_BRAND_ERROR); 47389acbbeafSnn35248 } 47399acbbeafSnn35248 4740123807fbSedp brand_close(bh); 47419acbbeafSnn35248 *plpp = plp; 47429acbbeafSnn35248 return (Z_OK); 47439acbbeafSnn35248 } 47449acbbeafSnn35248 47459acbbeafSnn35248 static int 47469acbbeafSnn35248 get_default_privset(priv_set_t *privs, priv_lists_t *plp) 47479acbbeafSnn35248 { 47489acbbeafSnn35248 priv_node_t *pnp; 4749ffbafc53Scomay priv_set_t *basic; 47507c478bd9Sstevel@tonic-gate 4751ffbafc53Scomay basic = priv_str_to_set(BASIC_TOKEN, TOKEN_PRIV_STR, NULL); 47527c478bd9Sstevel@tonic-gate if (basic == NULL) 4753ffbafc53Scomay return (errno == ENOMEM ? Z_NOMEM : Z_INVAL); 47547c478bd9Sstevel@tonic-gate 47557c478bd9Sstevel@tonic-gate priv_union(basic, privs); 47567c478bd9Sstevel@tonic-gate priv_freeset(basic); 47577c478bd9Sstevel@tonic-gate 47589acbbeafSnn35248 for (pnp = plp->pl_default; pnp != NULL; pnp = pnp->pn_next) { 47599acbbeafSnn35248 if (priv_addset(privs, pnp->pn_priv) != 0) 47607c478bd9Sstevel@tonic-gate return (Z_INVAL); 47617c478bd9Sstevel@tonic-gate } 47629acbbeafSnn35248 47637c478bd9Sstevel@tonic-gate return (Z_OK); 47647c478bd9Sstevel@tonic-gate } 47657c478bd9Sstevel@tonic-gate 47669acbbeafSnn35248 int 4767bf1d7e28Sdh155122 zonecfg_default_privset(priv_set_t *privs, const char *curr_iptype) 47689acbbeafSnn35248 { 47699acbbeafSnn35248 priv_lists_t *plp; 47709acbbeafSnn35248 int ret; 47719acbbeafSnn35248 4772bf1d7e28Sdh155122 if ((ret = priv_lists_create(NULL, &plp, curr_iptype)) != Z_OK) 47739acbbeafSnn35248 return (ret); 47749acbbeafSnn35248 ret = get_default_privset(privs, plp); 47759acbbeafSnn35248 priv_lists_destroy(plp); 47769acbbeafSnn35248 return (ret); 47779acbbeafSnn35248 } 47789acbbeafSnn35248 4779ffbafc53Scomay void 4780ffbafc53Scomay append_priv_token(char *priv, char *str, size_t strlen) 4781ffbafc53Scomay { 4782ffbafc53Scomay if (*str != '\0') 4783ffbafc53Scomay (void) strlcat(str, TOKEN_PRIV_STR, strlen); 4784ffbafc53Scomay (void) strlcat(str, priv, strlen); 4785ffbafc53Scomay } 4786ffbafc53Scomay 4787ffbafc53Scomay /* 4788ffbafc53Scomay * Verify that the supplied string is a valid privilege limit set for a 4789ffbafc53Scomay * non-global zone. This string must not only be acceptable to 4790ffbafc53Scomay * priv_str_to_set(3C) which parses it, but it also must resolve to a 4791ffbafc53Scomay * privilege set that includes certain required privileges and lacks 4792ffbafc53Scomay * certain prohibited privileges. 4793ffbafc53Scomay */ 4794ffbafc53Scomay static int 4795ffbafc53Scomay verify_privset(char *privbuf, priv_set_t *privs, char **privname, 47969acbbeafSnn35248 boolean_t add_default, priv_lists_t *plp) 4797ffbafc53Scomay { 47989acbbeafSnn35248 priv_node_t *pnp; 47999acbbeafSnn35248 char *tmp, *cp, *lasts; 4800ffbafc53Scomay size_t len; 4801ffbafc53Scomay priv_set_t *mergeset; 4802ffbafc53Scomay const char *token; 4803ffbafc53Scomay 4804ffbafc53Scomay /* 4805ffbafc53Scomay * The verification of the privilege string occurs in several 4806ffbafc53Scomay * phases. In the first phase, the supplied string is scanned for 4807ffbafc53Scomay * the ZONE_TOKEN token which is not support as part of the 4808ffbafc53Scomay * "limitpriv" property. 4809ffbafc53Scomay * 4810ffbafc53Scomay * Duplicate the supplied privilege string since strtok_r(3C) 4811ffbafc53Scomay * tokenizes its input by null-terminating the tokens. 4812ffbafc53Scomay */ 4813ffbafc53Scomay if ((tmp = strdup(privbuf)) == NULL) 4814ffbafc53Scomay return (Z_NOMEM); 4815ffbafc53Scomay for (cp = strtok_r(tmp, TOKEN_PRIV_STR, &lasts); cp != NULL; 4816ffbafc53Scomay cp = strtok_r(NULL, TOKEN_PRIV_STR, &lasts)) { 4817ffbafc53Scomay if (strcmp(cp, ZONE_TOKEN) == 0) { 4818ffbafc53Scomay free(tmp); 4819ffbafc53Scomay if ((*privname = strdup(ZONE_TOKEN)) == NULL) 4820ffbafc53Scomay return (Z_NOMEM); 4821ffbafc53Scomay else 4822ffbafc53Scomay return (Z_PRIV_UNKNOWN); 4823ffbafc53Scomay } 4824ffbafc53Scomay } 4825ffbafc53Scomay free(tmp); 4826ffbafc53Scomay 4827ffbafc53Scomay if (add_default) { 4828ffbafc53Scomay /* 4829ffbafc53Scomay * If DEFAULT_TOKEN was specified, a string needs to be 4830ffbafc53Scomay * built containing the privileges from the default, safe 4831ffbafc53Scomay * set along with those of the "limitpriv" property. 4832ffbafc53Scomay */ 4833ffbafc53Scomay len = strlen(privbuf) + sizeof (BASIC_TOKEN) + 2; 48349acbbeafSnn35248 48359acbbeafSnn35248 for (pnp = plp->pl_default; pnp != NULL; pnp = pnp->pn_next) 48369acbbeafSnn35248 len += strlen(pnp->pn_priv) + 1; 4837ffbafc53Scomay tmp = alloca(len); 4838ffbafc53Scomay *tmp = '\0'; 4839ffbafc53Scomay 4840ffbafc53Scomay append_priv_token(BASIC_TOKEN, tmp, len); 48419acbbeafSnn35248 for (pnp = plp->pl_default; pnp != NULL; pnp = pnp->pn_next) 48429acbbeafSnn35248 append_priv_token(pnp->pn_priv, tmp, len); 4843ffbafc53Scomay (void) strlcat(tmp, TOKEN_PRIV_STR, len); 4844ffbafc53Scomay (void) strlcat(tmp, privbuf, len); 4845ffbafc53Scomay } else { 4846ffbafc53Scomay tmp = privbuf; 4847ffbafc53Scomay } 4848ffbafc53Scomay 4849ffbafc53Scomay 4850ffbafc53Scomay /* 4851ffbafc53Scomay * In the next phase, attempt to convert the merged privilege 4852ffbafc53Scomay * string into a privilege set. In the case of an error, either 4853ffbafc53Scomay * there was a memory allocation failure or there was an invalid 4854ffbafc53Scomay * privilege token in the string. In either case, return an 4855ffbafc53Scomay * appropriate error code but in the event of an invalid token, 4856ffbafc53Scomay * allocate a string containing its name and return that back to 4857ffbafc53Scomay * the caller. 4858ffbafc53Scomay */ 4859ffbafc53Scomay mergeset = priv_str_to_set(tmp, TOKEN_PRIV_STR, &token); 4860ffbafc53Scomay if (mergeset == NULL) { 4861ffbafc53Scomay if (token == NULL) 4862ffbafc53Scomay return (Z_NOMEM); 4863ffbafc53Scomay if ((cp = strchr(token, TOKEN_PRIV_CHAR)) != NULL) 4864ffbafc53Scomay *cp = '\0'; 4865ffbafc53Scomay if ((*privname = strdup(token)) == NULL) 4866ffbafc53Scomay return (Z_NOMEM); 4867ffbafc53Scomay else 4868ffbafc53Scomay return (Z_PRIV_UNKNOWN); 4869ffbafc53Scomay } 4870ffbafc53Scomay 4871ffbafc53Scomay /* 4872ffbafc53Scomay * Next, verify that none of the prohibited zone privileges are 4873ffbafc53Scomay * present in the merged privilege set. 4874ffbafc53Scomay */ 48759acbbeafSnn35248 for (pnp = plp->pl_prohibited; pnp != NULL; pnp = pnp->pn_next) { 48769acbbeafSnn35248 if (priv_ismember(mergeset, pnp->pn_priv)) { 4877ffbafc53Scomay priv_freeset(mergeset); 48789acbbeafSnn35248 if ((*privname = strdup(pnp->pn_priv)) == NULL) 4879ffbafc53Scomay return (Z_NOMEM); 4880ffbafc53Scomay else 4881ffbafc53Scomay return (Z_PRIV_PROHIBITED); 4882ffbafc53Scomay } 4883ffbafc53Scomay } 4884ffbafc53Scomay 4885ffbafc53Scomay /* 4886ffbafc53Scomay * Finally, verify that all of the required zone privileges are 4887ffbafc53Scomay * present in the merged privilege set. 4888ffbafc53Scomay */ 48899acbbeafSnn35248 for (pnp = plp->pl_required; pnp != NULL; pnp = pnp->pn_next) { 48909acbbeafSnn35248 if (!priv_ismember(mergeset, pnp->pn_priv)) { 4891ffbafc53Scomay priv_freeset(mergeset); 48929acbbeafSnn35248 if ((*privname = strdup(pnp->pn_priv)) == NULL) 4893ffbafc53Scomay return (Z_NOMEM); 4894ffbafc53Scomay else 4895ffbafc53Scomay return (Z_PRIV_REQUIRED); 4896ffbafc53Scomay } 4897ffbafc53Scomay } 4898ffbafc53Scomay 4899ffbafc53Scomay priv_copyset(mergeset, privs); 4900ffbafc53Scomay priv_freeset(mergeset); 4901ffbafc53Scomay return (Z_OK); 4902ffbafc53Scomay } 4903ffbafc53Scomay 4904ffbafc53Scomay /* 4905ffbafc53Scomay * Fill in the supplied privilege set with either the default, safe set of 4906ffbafc53Scomay * privileges suitable for a non-global zone, or one based on the 4907ffbafc53Scomay * "limitpriv" property in the zone's configuration. 4908ffbafc53Scomay * 4909ffbafc53Scomay * In the event of an invalid privilege specification in the 4910ffbafc53Scomay * configuration, a string is allocated and returned containing the 4911ffbafc53Scomay * "privilege" causing the issue. It is the caller's responsibility to 4912ffbafc53Scomay * free this memory when it is done with it. 4913ffbafc53Scomay */ 4914ffbafc53Scomay int 4915ffbafc53Scomay zonecfg_get_privset(zone_dochandle_t handle, priv_set_t *privs, 4916ffbafc53Scomay char **privname) 4917ffbafc53Scomay { 49189acbbeafSnn35248 priv_lists_t *plp; 49199acbbeafSnn35248 char *cp, *limitpriv = NULL; 49209acbbeafSnn35248 int err, limitlen; 4921bf1d7e28Sdh155122 zone_iptype_t iptype; 4922bf1d7e28Sdh155122 const char *curr_iptype; 4923ffbafc53Scomay 4924ffbafc53Scomay /* 4925ffbafc53Scomay * Attempt to lookup the "limitpriv" property. If it does not 4926ffbafc53Scomay * exist or matches the string DEFAULT_TOKEN exactly, then the 4927ffbafc53Scomay * default, safe privilege set is returned. 4928ffbafc53Scomay */ 49299acbbeafSnn35248 if ((err = zonecfg_get_limitpriv(handle, &limitpriv)) != Z_OK) 4930ffbafc53Scomay return (err); 49319acbbeafSnn35248 4932bf1d7e28Sdh155122 if ((err = zonecfg_get_iptype(handle, &iptype)) != Z_OK) 4933bf1d7e28Sdh155122 return (err); 4934bf1d7e28Sdh155122 4935bf1d7e28Sdh155122 switch (iptype) { 4936bf1d7e28Sdh155122 case ZS_SHARED: 4937bf1d7e28Sdh155122 curr_iptype = "shared"; 4938bf1d7e28Sdh155122 break; 4939bf1d7e28Sdh155122 case ZS_EXCLUSIVE: 4940bf1d7e28Sdh155122 curr_iptype = "exclusive"; 4941bf1d7e28Sdh155122 break; 4942bf1d7e28Sdh155122 } 4943bf1d7e28Sdh155122 4944bf1d7e28Sdh155122 if ((err = priv_lists_create(handle, &plp, curr_iptype)) != Z_OK) 49459acbbeafSnn35248 return (err); 49469acbbeafSnn35248 4947ffbafc53Scomay limitlen = strlen(limitpriv); 4948ffbafc53Scomay if (limitlen == 0 || strcmp(limitpriv, DEFAULT_TOKEN) == 0) { 4949ffbafc53Scomay free(limitpriv); 49509acbbeafSnn35248 err = get_default_privset(privs, plp); 49519acbbeafSnn35248 priv_lists_destroy(plp); 49529acbbeafSnn35248 return (err); 4953ffbafc53Scomay } 4954ffbafc53Scomay 4955ffbafc53Scomay /* 4956ffbafc53Scomay * Check if the string DEFAULT_TOKEN is the first token in a list 4957ffbafc53Scomay * of privileges. 4958ffbafc53Scomay */ 4959ffbafc53Scomay cp = strchr(limitpriv, TOKEN_PRIV_CHAR); 4960ffbafc53Scomay if (cp != NULL && 4961ffbafc53Scomay strncmp(limitpriv, DEFAULT_TOKEN, cp - limitpriv) == 0) 49629acbbeafSnn35248 err = verify_privset(cp + 1, privs, privname, B_TRUE, plp); 4963ffbafc53Scomay else 49649acbbeafSnn35248 err = verify_privset(limitpriv, privs, privname, B_FALSE, plp); 4965ffbafc53Scomay 4966ffbafc53Scomay free(limitpriv); 49679acbbeafSnn35248 priv_lists_destroy(plp); 4968ffbafc53Scomay return (err); 4969ffbafc53Scomay } 4970ffbafc53Scomay 49717c478bd9Sstevel@tonic-gate int 49727c478bd9Sstevel@tonic-gate zone_get_zonepath(char *zone_name, char *zonepath, size_t rp_sz) 49737c478bd9Sstevel@tonic-gate { 49747c478bd9Sstevel@tonic-gate zone_dochandle_t handle; 49757c478bd9Sstevel@tonic-gate boolean_t found = B_FALSE; 49767c478bd9Sstevel@tonic-gate struct zoneent *ze; 49777c478bd9Sstevel@tonic-gate FILE *cookie; 49787c478bd9Sstevel@tonic-gate int err; 4979108322fbScarlsonj char *cp; 49807c478bd9Sstevel@tonic-gate 49817c478bd9Sstevel@tonic-gate if (zone_name == NULL) 49827c478bd9Sstevel@tonic-gate return (Z_INVAL); 49837c478bd9Sstevel@tonic-gate 4984108322fbScarlsonj (void) strlcpy(zonepath, zonecfg_root, rp_sz); 4985108322fbScarlsonj cp = zonepath + strlen(zonepath); 4986108322fbScarlsonj while (cp > zonepath && cp[-1] == '/') 4987108322fbScarlsonj *--cp = '\0'; 4988108322fbScarlsonj 49897c478bd9Sstevel@tonic-gate if (strcmp(zone_name, GLOBAL_ZONENAME) == 0) { 4990108322fbScarlsonj if (zonepath[0] == '\0') 49917c478bd9Sstevel@tonic-gate (void) strlcpy(zonepath, "/", rp_sz); 49927c478bd9Sstevel@tonic-gate return (Z_OK); 49937c478bd9Sstevel@tonic-gate } 49947c478bd9Sstevel@tonic-gate 49957c478bd9Sstevel@tonic-gate /* 49967c478bd9Sstevel@tonic-gate * First check the index file. Because older versions did not have 49977c478bd9Sstevel@tonic-gate * a copy of the zone path, allow for it to be zero length, in which 49987c478bd9Sstevel@tonic-gate * case we ignore this result and fall back to the XML files. 49997c478bd9Sstevel@tonic-gate */ 50007c478bd9Sstevel@tonic-gate cookie = setzoneent(); 50017c478bd9Sstevel@tonic-gate while ((ze = getzoneent_private(cookie)) != NULL) { 50027c478bd9Sstevel@tonic-gate if (strcmp(ze->zone_name, zone_name) == 0) { 50037c478bd9Sstevel@tonic-gate found = B_TRUE; 5004108322fbScarlsonj if (ze->zone_path[0] != '\0') 5005108322fbScarlsonj (void) strlcpy(cp, ze->zone_path, 5006108322fbScarlsonj rp_sz - (cp - zonepath)); 50077c478bd9Sstevel@tonic-gate } 50087c478bd9Sstevel@tonic-gate free(ze); 50097c478bd9Sstevel@tonic-gate if (found) 50107c478bd9Sstevel@tonic-gate break; 50117c478bd9Sstevel@tonic-gate } 50127c478bd9Sstevel@tonic-gate endzoneent(cookie); 5013108322fbScarlsonj if (found && *cp != '\0') 50147c478bd9Sstevel@tonic-gate return (Z_OK); 50157c478bd9Sstevel@tonic-gate 50167c478bd9Sstevel@tonic-gate /* Fall back to the XML files. */ 50177c478bd9Sstevel@tonic-gate if ((handle = zonecfg_init_handle()) == NULL) 50187c478bd9Sstevel@tonic-gate return (Z_NOMEM); 50197c478bd9Sstevel@tonic-gate 50207c478bd9Sstevel@tonic-gate /* 50217c478bd9Sstevel@tonic-gate * Check the snapshot first: if a zone is running, its zonepath 50227c478bd9Sstevel@tonic-gate * may have changed. 50237c478bd9Sstevel@tonic-gate */ 50247c478bd9Sstevel@tonic-gate if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK) { 5025e767a340Sgjelinek if ((err = zonecfg_get_handle(zone_name, handle)) != Z_OK) { 5026e767a340Sgjelinek zonecfg_fini_handle(handle); 50277c478bd9Sstevel@tonic-gate return (err); 50287c478bd9Sstevel@tonic-gate } 5029e767a340Sgjelinek } 50307c478bd9Sstevel@tonic-gate err = zonecfg_get_zonepath(handle, zonepath, rp_sz); 50317c478bd9Sstevel@tonic-gate zonecfg_fini_handle(handle); 50327c478bd9Sstevel@tonic-gate return (err); 50337c478bd9Sstevel@tonic-gate } 50347c478bd9Sstevel@tonic-gate 50357c478bd9Sstevel@tonic-gate int 50367c478bd9Sstevel@tonic-gate zone_get_rootpath(char *zone_name, char *rootpath, size_t rp_sz) 50377c478bd9Sstevel@tonic-gate { 50387c478bd9Sstevel@tonic-gate int err; 50397c478bd9Sstevel@tonic-gate 50407c478bd9Sstevel@tonic-gate /* This function makes sense for non-global zones only. */ 50417c478bd9Sstevel@tonic-gate if (strcmp(zone_name, GLOBAL_ZONENAME) == 0) 50427c478bd9Sstevel@tonic-gate return (Z_BOGUS_ZONE_NAME); 50437c478bd9Sstevel@tonic-gate if ((err = zone_get_zonepath(zone_name, rootpath, rp_sz)) != Z_OK) 50447c478bd9Sstevel@tonic-gate return (err); 50457c478bd9Sstevel@tonic-gate if (strlcat(rootpath, "/root", rp_sz) >= rp_sz) 50467c478bd9Sstevel@tonic-gate return (Z_TOO_BIG); 50477c478bd9Sstevel@tonic-gate return (Z_OK); 50487c478bd9Sstevel@tonic-gate } 50497c478bd9Sstevel@tonic-gate 50509acbbeafSnn35248 int 50519acbbeafSnn35248 zone_get_brand(char *zone_name, char *brandname, size_t rp_sz) 50529acbbeafSnn35248 { 50539acbbeafSnn35248 int err; 50549acbbeafSnn35248 zone_dochandle_t handle; 50559acbbeafSnn35248 char myzone[MAXNAMELEN]; 50569acbbeafSnn35248 int myzoneid = getzoneid(); 50579acbbeafSnn35248 50589acbbeafSnn35248 /* 50599acbbeafSnn35248 * If we are not in the global zone, then we don't have the zone 50609acbbeafSnn35248 * .xml files with the brand name available. Thus, we are going to 50619acbbeafSnn35248 * have to ask the kernel for the information. 50629acbbeafSnn35248 */ 50639acbbeafSnn35248 if (myzoneid != GLOBAL_ZONEID) { 50642ec67e04Sgjelinek if (is_system_labeled()) { 50652ec67e04Sgjelinek (void) strlcpy(brandname, NATIVE_BRAND_NAME, rp_sz); 50662ec67e04Sgjelinek return (Z_OK); 50672ec67e04Sgjelinek } 50689acbbeafSnn35248 if (zone_getattr(myzoneid, ZONE_ATTR_NAME, myzone, 50699acbbeafSnn35248 sizeof (myzone)) < 0) 50709acbbeafSnn35248 return (Z_NO_ZONE); 50719acbbeafSnn35248 if (strncmp(zone_name, myzone, MAXNAMELEN) != NULL) 50729acbbeafSnn35248 return (Z_NO_ZONE); 50739acbbeafSnn35248 err = zone_getattr(myzoneid, ZONE_ATTR_BRAND, brandname, rp_sz); 50749acbbeafSnn35248 if (err < 0) 50759acbbeafSnn35248 return ((errno == EFAULT) ? Z_TOO_BIG : Z_INVAL); 50769acbbeafSnn35248 return (Z_OK); 50779acbbeafSnn35248 } 50789acbbeafSnn35248 50799acbbeafSnn35248 if (strcmp(zone_name, "global") == NULL) { 50809acbbeafSnn35248 (void) strlcpy(brandname, NATIVE_BRAND_NAME, rp_sz); 50812ec67e04Sgjelinek return (Z_OK); 50829acbbeafSnn35248 } 50839acbbeafSnn35248 if ((handle = zonecfg_init_handle()) == NULL) 50849acbbeafSnn35248 return (Z_NOMEM); 50859acbbeafSnn35248 50869acbbeafSnn35248 err = zonecfg_get_handle((char *)zone_name, handle); 50879acbbeafSnn35248 if (err == Z_OK) 50889acbbeafSnn35248 err = zonecfg_get_brand(handle, brandname, rp_sz); 50899acbbeafSnn35248 50909acbbeafSnn35248 zonecfg_fini_handle(handle); 50919acbbeafSnn35248 return (err); 50929acbbeafSnn35248 } 50939acbbeafSnn35248 5094facf4a8dSllai1 /* 5095facf4a8dSllai1 * Return the appropriate root for the active /dev. 5096facf4a8dSllai1 * For normal zone, the path is $ZONEPATH/root; 5097facf4a8dSllai1 * for scratch zone, the dev path is $ZONEPATH/lu. 5098facf4a8dSllai1 */ 5099facf4a8dSllai1 int 5100facf4a8dSllai1 zone_get_devroot(char *zone_name, char *devroot, size_t rp_sz) 5101facf4a8dSllai1 { 5102facf4a8dSllai1 int err; 5103facf4a8dSllai1 char *suffix; 5104facf4a8dSllai1 zone_state_t state; 5105facf4a8dSllai1 5106facf4a8dSllai1 /* This function makes sense for non-global zones only. */ 5107facf4a8dSllai1 if (strcmp(zone_name, GLOBAL_ZONENAME) == 0) 5108facf4a8dSllai1 return (Z_BOGUS_ZONE_NAME); 5109facf4a8dSllai1 if ((err = zone_get_zonepath(zone_name, devroot, rp_sz)) != Z_OK) 5110facf4a8dSllai1 return (err); 5111facf4a8dSllai1 5112facf4a8dSllai1 if (zone_get_state(zone_name, &state) == Z_OK && 5113facf4a8dSllai1 state == ZONE_STATE_MOUNTED) 5114facf4a8dSllai1 suffix = "/lu"; 5115facf4a8dSllai1 else 5116facf4a8dSllai1 suffix = "/root"; 5117facf4a8dSllai1 if (strlcat(devroot, suffix, rp_sz) >= rp_sz) 5118facf4a8dSllai1 return (Z_TOO_BIG); 5119facf4a8dSllai1 return (Z_OK); 5120facf4a8dSllai1 } 5121facf4a8dSllai1 51227c478bd9Sstevel@tonic-gate static zone_state_t 5123108322fbScarlsonj kernel_state_to_user_state(zoneid_t zoneid, zone_status_t kernel_state) 51247c478bd9Sstevel@tonic-gate { 5125108322fbScarlsonj char zoneroot[MAXPATHLEN]; 5126108322fbScarlsonj size_t zlen; 5127108322fbScarlsonj 51287c478bd9Sstevel@tonic-gate assert(kernel_state <= ZONE_MAX_STATE); 51297c478bd9Sstevel@tonic-gate switch (kernel_state) { 51307c478bd9Sstevel@tonic-gate case ZONE_IS_UNINITIALIZED: 5131bd41d0a8Snordmark case ZONE_IS_INITIALIZED: 5132bd41d0a8Snordmark /* The kernel will not return these two states */ 5133108322fbScarlsonj return (ZONE_STATE_READY); 51347c478bd9Sstevel@tonic-gate case ZONE_IS_READY: 5135108322fbScarlsonj /* 5136108322fbScarlsonj * If the zone's root is mounted on $ZONEPATH/lu, then 5137108322fbScarlsonj * it's a mounted scratch zone. 5138108322fbScarlsonj */ 5139108322fbScarlsonj if (zone_getattr(zoneid, ZONE_ATTR_ROOT, zoneroot, 5140108322fbScarlsonj sizeof (zoneroot)) >= 0) { 5141108322fbScarlsonj zlen = strlen(zoneroot); 5142108322fbScarlsonj if (zlen > 3 && 5143108322fbScarlsonj strcmp(zoneroot + zlen - 3, "/lu") == 0) 5144108322fbScarlsonj return (ZONE_STATE_MOUNTED); 5145108322fbScarlsonj } 51467c478bd9Sstevel@tonic-gate return (ZONE_STATE_READY); 51477c478bd9Sstevel@tonic-gate case ZONE_IS_BOOTING: 51487c478bd9Sstevel@tonic-gate case ZONE_IS_RUNNING: 51497c478bd9Sstevel@tonic-gate return (ZONE_STATE_RUNNING); 51507c478bd9Sstevel@tonic-gate case ZONE_IS_SHUTTING_DOWN: 51517c478bd9Sstevel@tonic-gate case ZONE_IS_EMPTY: 51527c478bd9Sstevel@tonic-gate return (ZONE_STATE_SHUTTING_DOWN); 51537c478bd9Sstevel@tonic-gate case ZONE_IS_DOWN: 51547c478bd9Sstevel@tonic-gate case ZONE_IS_DYING: 51557c478bd9Sstevel@tonic-gate case ZONE_IS_DEAD: 51567c478bd9Sstevel@tonic-gate default: 51577c478bd9Sstevel@tonic-gate return (ZONE_STATE_DOWN); 51587c478bd9Sstevel@tonic-gate } 51597c478bd9Sstevel@tonic-gate /* NOTREACHED */ 51607c478bd9Sstevel@tonic-gate } 51617c478bd9Sstevel@tonic-gate 51627c478bd9Sstevel@tonic-gate int 51637c478bd9Sstevel@tonic-gate zone_get_state(char *zone_name, zone_state_t *state_num) 51647c478bd9Sstevel@tonic-gate { 51657c478bd9Sstevel@tonic-gate zone_status_t status; 51667c478bd9Sstevel@tonic-gate zoneid_t zone_id; 51677c478bd9Sstevel@tonic-gate struct zoneent *ze; 51687c478bd9Sstevel@tonic-gate boolean_t found = B_FALSE; 51697c478bd9Sstevel@tonic-gate FILE *cookie; 5170108322fbScarlsonj char kernzone[ZONENAME_MAX]; 5171108322fbScarlsonj FILE *fp; 51727c478bd9Sstevel@tonic-gate 51737c478bd9Sstevel@tonic-gate if (zone_name == NULL) 51747c478bd9Sstevel@tonic-gate return (Z_INVAL); 51757c478bd9Sstevel@tonic-gate 5176108322fbScarlsonj /* 5177108322fbScarlsonj * If we're looking at an alternate root, then we need to query the 5178108322fbScarlsonj * kernel using the scratch zone name. 5179108322fbScarlsonj */ 5180108322fbScarlsonj zone_id = -1; 5181108322fbScarlsonj if (*zonecfg_root != '\0' && !zonecfg_is_scratch(zone_name)) { 5182108322fbScarlsonj if ((fp = zonecfg_open_scratch("", B_FALSE)) != NULL) { 5183108322fbScarlsonj if (zonecfg_find_scratch(fp, zone_name, zonecfg_root, 5184108322fbScarlsonj kernzone, sizeof (kernzone)) == 0) 5185108322fbScarlsonj zone_id = getzoneidbyname(kernzone); 5186108322fbScarlsonj zonecfg_close_scratch(fp); 5187108322fbScarlsonj } 5188108322fbScarlsonj } else { 5189108322fbScarlsonj zone_id = getzoneidbyname(zone_name); 5190108322fbScarlsonj } 5191108322fbScarlsonj 51927c478bd9Sstevel@tonic-gate /* check to see if zone is running */ 5193108322fbScarlsonj if (zone_id != -1 && 51947c478bd9Sstevel@tonic-gate zone_getattr(zone_id, ZONE_ATTR_STATUS, &status, 51957c478bd9Sstevel@tonic-gate sizeof (status)) >= 0) { 5196108322fbScarlsonj *state_num = kernel_state_to_user_state(zone_id, status); 51977c478bd9Sstevel@tonic-gate return (Z_OK); 51987c478bd9Sstevel@tonic-gate } 51997c478bd9Sstevel@tonic-gate 52007c478bd9Sstevel@tonic-gate cookie = setzoneent(); 52017c478bd9Sstevel@tonic-gate while ((ze = getzoneent_private(cookie)) != NULL) { 52027c478bd9Sstevel@tonic-gate if (strcmp(ze->zone_name, zone_name) == 0) { 52037c478bd9Sstevel@tonic-gate found = B_TRUE; 52047c478bd9Sstevel@tonic-gate *state_num = ze->zone_state; 52057c478bd9Sstevel@tonic-gate } 52067c478bd9Sstevel@tonic-gate free(ze); 52077c478bd9Sstevel@tonic-gate if (found) 52087c478bd9Sstevel@tonic-gate break; 52097c478bd9Sstevel@tonic-gate } 52107c478bd9Sstevel@tonic-gate endzoneent(cookie); 52117c478bd9Sstevel@tonic-gate return ((found) ? Z_OK : Z_NO_ZONE); 52127c478bd9Sstevel@tonic-gate } 52137c478bd9Sstevel@tonic-gate 52147c478bd9Sstevel@tonic-gate int 52157c478bd9Sstevel@tonic-gate zone_set_state(char *zone, zone_state_t state) 52167c478bd9Sstevel@tonic-gate { 52177c478bd9Sstevel@tonic-gate struct zoneent ze; 52187c478bd9Sstevel@tonic-gate 52197c478bd9Sstevel@tonic-gate if (state != ZONE_STATE_CONFIGURED && state != ZONE_STATE_INSTALLED && 52207c478bd9Sstevel@tonic-gate state != ZONE_STATE_INCOMPLETE) 52217c478bd9Sstevel@tonic-gate return (Z_INVAL); 52227c478bd9Sstevel@tonic-gate 5223087719fdSdp bzero(&ze, sizeof (ze)); 52247c478bd9Sstevel@tonic-gate (void) strlcpy(ze.zone_name, zone, sizeof (ze.zone_name)); 52257c478bd9Sstevel@tonic-gate ze.zone_state = state; 52267c478bd9Sstevel@tonic-gate (void) strlcpy(ze.zone_path, "", sizeof (ze.zone_path)); 52277c478bd9Sstevel@tonic-gate return (putzoneent(&ze, PZE_MODIFY)); 52287c478bd9Sstevel@tonic-gate } 52297c478bd9Sstevel@tonic-gate 52307c478bd9Sstevel@tonic-gate /* 52317c478bd9Sstevel@tonic-gate * Get id (if any) for specified zone. There are four possible outcomes: 52327c478bd9Sstevel@tonic-gate * - If the string corresponds to the numeric id of an active (booted) 52337c478bd9Sstevel@tonic-gate * zone, sets *zip to the zone id and returns 0. 52347c478bd9Sstevel@tonic-gate * - If the string corresponds to the name of an active (booted) zone, 52357c478bd9Sstevel@tonic-gate * sets *zip to the zone id and returns 0. 52367c478bd9Sstevel@tonic-gate * - If the string is a name in the configuration but is not booted, 52377c478bd9Sstevel@tonic-gate * sets *zip to ZONE_ID_UNDEFINED and returns 0. 52387c478bd9Sstevel@tonic-gate * - Otherwise, leaves *zip unchanged and returns -1. 52397c478bd9Sstevel@tonic-gate * 52407c478bd9Sstevel@tonic-gate * This function acts as an auxiliary filter on the function of the same 52417c478bd9Sstevel@tonic-gate * name in libc; the linker binds to this version if libzonecfg exists, 52427c478bd9Sstevel@tonic-gate * and the libc version if it doesn't. Any changes to this version of 52437c478bd9Sstevel@tonic-gate * the function should probably be reflected in the libc version as well. 52447c478bd9Sstevel@tonic-gate */ 52457c478bd9Sstevel@tonic-gate int 52467c478bd9Sstevel@tonic-gate zone_get_id(const char *str, zoneid_t *zip) 52477c478bd9Sstevel@tonic-gate { 52487c478bd9Sstevel@tonic-gate zone_dochandle_t hdl; 52497c478bd9Sstevel@tonic-gate zoneid_t zoneid; 52507c478bd9Sstevel@tonic-gate char *cp; 52517c478bd9Sstevel@tonic-gate int err; 52527c478bd9Sstevel@tonic-gate 52537c478bd9Sstevel@tonic-gate /* first try looking for active zone by id */ 52547c478bd9Sstevel@tonic-gate errno = 0; 52557c478bd9Sstevel@tonic-gate zoneid = (zoneid_t)strtol(str, &cp, 0); 52567c478bd9Sstevel@tonic-gate if (errno == 0 && cp != str && *cp == '\0' && 52577c478bd9Sstevel@tonic-gate getzonenamebyid(zoneid, NULL, 0) != -1) { 52587c478bd9Sstevel@tonic-gate *zip = zoneid; 52597c478bd9Sstevel@tonic-gate return (0); 52607c478bd9Sstevel@tonic-gate } 52617c478bd9Sstevel@tonic-gate 52627c478bd9Sstevel@tonic-gate /* then look for active zone by name */ 52637c478bd9Sstevel@tonic-gate if ((zoneid = getzoneidbyname(str)) != -1) { 52647c478bd9Sstevel@tonic-gate *zip = zoneid; 52657c478bd9Sstevel@tonic-gate return (0); 52667c478bd9Sstevel@tonic-gate } 52677c478bd9Sstevel@tonic-gate 52687c478bd9Sstevel@tonic-gate /* if in global zone, try looking up name in configuration database */ 52697c478bd9Sstevel@tonic-gate if (getzoneid() != GLOBAL_ZONEID || 52707c478bd9Sstevel@tonic-gate (hdl = zonecfg_init_handle()) == NULL) 52717c478bd9Sstevel@tonic-gate return (-1); 52727c478bd9Sstevel@tonic-gate 5273108322fbScarlsonj if (zonecfg_get_handle(str, hdl) == Z_OK) { 52747c478bd9Sstevel@tonic-gate /* zone exists but isn't active */ 52757c478bd9Sstevel@tonic-gate *zip = ZONE_ID_UNDEFINED; 52767c478bd9Sstevel@tonic-gate err = 0; 52777c478bd9Sstevel@tonic-gate } else { 52787c478bd9Sstevel@tonic-gate err = -1; 52797c478bd9Sstevel@tonic-gate } 52807c478bd9Sstevel@tonic-gate 52817c478bd9Sstevel@tonic-gate zonecfg_fini_handle(hdl); 52827c478bd9Sstevel@tonic-gate return (err); 52837c478bd9Sstevel@tonic-gate } 52847c478bd9Sstevel@tonic-gate 52857c478bd9Sstevel@tonic-gate char * 52867c478bd9Sstevel@tonic-gate zone_state_str(zone_state_t state_num) 52877c478bd9Sstevel@tonic-gate { 52887c478bd9Sstevel@tonic-gate switch (state_num) { 52897c478bd9Sstevel@tonic-gate case ZONE_STATE_CONFIGURED: 52907c478bd9Sstevel@tonic-gate return (ZONE_STATE_STR_CONFIGURED); 52917c478bd9Sstevel@tonic-gate case ZONE_STATE_INCOMPLETE: 52927c478bd9Sstevel@tonic-gate return (ZONE_STATE_STR_INCOMPLETE); 52937c478bd9Sstevel@tonic-gate case ZONE_STATE_INSTALLED: 52947c478bd9Sstevel@tonic-gate return (ZONE_STATE_STR_INSTALLED); 52957c478bd9Sstevel@tonic-gate case ZONE_STATE_READY: 52967c478bd9Sstevel@tonic-gate return (ZONE_STATE_STR_READY); 5297108322fbScarlsonj case ZONE_STATE_MOUNTED: 5298108322fbScarlsonj return (ZONE_STATE_STR_MOUNTED); 52997c478bd9Sstevel@tonic-gate case ZONE_STATE_RUNNING: 53007c478bd9Sstevel@tonic-gate return (ZONE_STATE_STR_RUNNING); 53017c478bd9Sstevel@tonic-gate case ZONE_STATE_SHUTTING_DOWN: 53027c478bd9Sstevel@tonic-gate return (ZONE_STATE_STR_SHUTTING_DOWN); 53037c478bd9Sstevel@tonic-gate case ZONE_STATE_DOWN: 53047c478bd9Sstevel@tonic-gate return (ZONE_STATE_STR_DOWN); 53057c478bd9Sstevel@tonic-gate default: 53067c478bd9Sstevel@tonic-gate return ("unknown"); 53077c478bd9Sstevel@tonic-gate } 53087c478bd9Sstevel@tonic-gate } 53097c478bd9Sstevel@tonic-gate 53107c478bd9Sstevel@tonic-gate /* 5311108322fbScarlsonj * Given a UUID value, find an associated zone name. This is intended to be 5312108322fbScarlsonj * used by callers who set up some 'default' name (corresponding to the 5313108322fbScarlsonj * expected name for the zone) in the zonename buffer, and thus the function 5314108322fbScarlsonj * doesn't touch this buffer on failure. 5315108322fbScarlsonj */ 5316108322fbScarlsonj int 5317555afedfScarlsonj zonecfg_get_name_by_uuid(const uuid_t uuidin, char *zonename, size_t namelen) 5318108322fbScarlsonj { 5319108322fbScarlsonj FILE *fp; 5320108322fbScarlsonj struct zoneent *ze; 5321555afedfScarlsonj uchar_t *uuid; 5322108322fbScarlsonj 5323108322fbScarlsonj /* 5324108322fbScarlsonj * A small amount of subterfuge via casts is necessary here because 5325108322fbScarlsonj * libuuid doesn't use const correctly, but we don't want to export 5326108322fbScarlsonj * this brokenness to our clients. 5327108322fbScarlsonj */ 5328555afedfScarlsonj uuid = (uchar_t *)uuidin; 5329555afedfScarlsonj if (uuid_is_null(uuid)) 5330108322fbScarlsonj return (Z_NO_ZONE); 5331108322fbScarlsonj if ((fp = setzoneent()) == NULL) 5332108322fbScarlsonj return (Z_NO_ZONE); 5333108322fbScarlsonj while ((ze = getzoneent_private(fp)) != NULL) { 5334555afedfScarlsonj if (uuid_compare(uuid, ze->zone_uuid) == 0) 5335108322fbScarlsonj break; 5336108322fbScarlsonj free(ze); 5337108322fbScarlsonj } 5338108322fbScarlsonj endzoneent(fp); 5339108322fbScarlsonj if (ze != NULL) { 5340108322fbScarlsonj (void) strlcpy(zonename, ze->zone_name, namelen); 5341108322fbScarlsonj free(ze); 5342108322fbScarlsonj return (Z_OK); 5343108322fbScarlsonj } else { 5344108322fbScarlsonj return (Z_NO_ZONE); 5345108322fbScarlsonj } 5346108322fbScarlsonj } 5347108322fbScarlsonj 5348108322fbScarlsonj /* 5349108322fbScarlsonj * Given a zone name, get its UUID. Returns a "NULL" UUID value if the zone 5350108322fbScarlsonj * exists but the file doesn't have a value set yet. Returns an error if the 5351108322fbScarlsonj * zone cannot be located. 5352108322fbScarlsonj */ 5353108322fbScarlsonj int 5354108322fbScarlsonj zonecfg_get_uuid(const char *zonename, uuid_t uuid) 5355108322fbScarlsonj { 5356108322fbScarlsonj FILE *fp; 5357108322fbScarlsonj struct zoneent *ze; 5358108322fbScarlsonj 5359108322fbScarlsonj if ((fp = setzoneent()) == NULL) 5360108322fbScarlsonj return (Z_NO_ZONE); 5361108322fbScarlsonj while ((ze = getzoneent_private(fp)) != NULL) { 5362108322fbScarlsonj if (strcmp(ze->zone_name, zonename) == 0) 5363108322fbScarlsonj break; 5364108322fbScarlsonj free(ze); 5365108322fbScarlsonj } 5366108322fbScarlsonj endzoneent(fp); 5367108322fbScarlsonj if (ze != NULL) { 5368108322fbScarlsonj uuid_copy(uuid, ze->zone_uuid); 5369108322fbScarlsonj free(ze); 5370108322fbScarlsonj return (Z_OK); 5371108322fbScarlsonj } else { 5372108322fbScarlsonj return (Z_NO_ZONE); 5373108322fbScarlsonj } 5374108322fbScarlsonj } 5375108322fbScarlsonj 5376108322fbScarlsonj /* 53777c478bd9Sstevel@tonic-gate * File-system convenience functions. 53787c478bd9Sstevel@tonic-gate */ 53797c478bd9Sstevel@tonic-gate boolean_t 53807c478bd9Sstevel@tonic-gate zonecfg_valid_fs_type(const char *type) 53817c478bd9Sstevel@tonic-gate { 53827c478bd9Sstevel@tonic-gate /* 53837c478bd9Sstevel@tonic-gate * We already know which FS types don't work. 53847c478bd9Sstevel@tonic-gate */ 53857c478bd9Sstevel@tonic-gate if (strcmp(type, "proc") == 0 || 53867c478bd9Sstevel@tonic-gate strcmp(type, "mntfs") == 0 || 53877c478bd9Sstevel@tonic-gate strcmp(type, "autofs") == 0 || 53887c478bd9Sstevel@tonic-gate strncmp(type, "nfs", sizeof ("nfs") - 1) == 0 || 53897c478bd9Sstevel@tonic-gate strcmp(type, "cachefs") == 0) 53907c478bd9Sstevel@tonic-gate return (B_FALSE); 53917c478bd9Sstevel@tonic-gate /* 53927c478bd9Sstevel@tonic-gate * The caller may do more detailed verification to make sure other 53937c478bd9Sstevel@tonic-gate * aspects of this filesystem type make sense. 53947c478bd9Sstevel@tonic-gate */ 53957c478bd9Sstevel@tonic-gate return (B_TRUE); 53967c478bd9Sstevel@tonic-gate } 53977c478bd9Sstevel@tonic-gate 53987c478bd9Sstevel@tonic-gate /* 53997c478bd9Sstevel@tonic-gate * Generally uninteresting rctl convenience functions. 54007c478bd9Sstevel@tonic-gate */ 54017c478bd9Sstevel@tonic-gate 54027c478bd9Sstevel@tonic-gate int 54037c478bd9Sstevel@tonic-gate zonecfg_construct_rctlblk(const struct zone_rctlvaltab *rctlval, 54047c478bd9Sstevel@tonic-gate rctlblk_t *rctlblk) 54057c478bd9Sstevel@tonic-gate { 54067c478bd9Sstevel@tonic-gate unsigned long long ull; 54077c478bd9Sstevel@tonic-gate char *endp; 54087c478bd9Sstevel@tonic-gate rctl_priv_t priv; 54097c478bd9Sstevel@tonic-gate rctl_qty_t limit; 54107c478bd9Sstevel@tonic-gate uint_t action; 54117c478bd9Sstevel@tonic-gate 54127c478bd9Sstevel@tonic-gate /* Get the privilege */ 54137c478bd9Sstevel@tonic-gate if (strcmp(rctlval->zone_rctlval_priv, "basic") == 0) { 54147c478bd9Sstevel@tonic-gate priv = RCPRIV_BASIC; 54157c478bd9Sstevel@tonic-gate } else if (strcmp(rctlval->zone_rctlval_priv, "privileged") == 0) { 54167c478bd9Sstevel@tonic-gate priv = RCPRIV_PRIVILEGED; 54177c478bd9Sstevel@tonic-gate } else { 54187c478bd9Sstevel@tonic-gate /* Invalid privilege */ 54197c478bd9Sstevel@tonic-gate return (Z_INVAL); 54207c478bd9Sstevel@tonic-gate } 54217c478bd9Sstevel@tonic-gate 54227c478bd9Sstevel@tonic-gate /* deal with negative input; strtoull(3c) doesn't do what we want */ 54237c478bd9Sstevel@tonic-gate if (rctlval->zone_rctlval_limit[0] == '-') 54247c478bd9Sstevel@tonic-gate return (Z_INVAL); 54257c478bd9Sstevel@tonic-gate /* Get the limit */ 54267c478bd9Sstevel@tonic-gate errno = 0; 54277c478bd9Sstevel@tonic-gate ull = strtoull(rctlval->zone_rctlval_limit, &endp, 0); 54287c478bd9Sstevel@tonic-gate if (errno != 0 || *endp != '\0') { 54297c478bd9Sstevel@tonic-gate /* parse failed */ 54307c478bd9Sstevel@tonic-gate return (Z_INVAL); 54317c478bd9Sstevel@tonic-gate } 54327c478bd9Sstevel@tonic-gate limit = (rctl_qty_t)ull; 54337c478bd9Sstevel@tonic-gate 54347c478bd9Sstevel@tonic-gate /* Get the action */ 54357c478bd9Sstevel@tonic-gate if (strcmp(rctlval->zone_rctlval_action, "none") == 0) { 54367c478bd9Sstevel@tonic-gate action = RCTL_LOCAL_NOACTION; 54377c478bd9Sstevel@tonic-gate } else if (strcmp(rctlval->zone_rctlval_action, "signal") == 0) { 54387c478bd9Sstevel@tonic-gate action = RCTL_LOCAL_SIGNAL; 54397c478bd9Sstevel@tonic-gate } else if (strcmp(rctlval->zone_rctlval_action, "deny") == 0) { 54407c478bd9Sstevel@tonic-gate action = RCTL_LOCAL_DENY; 54417c478bd9Sstevel@tonic-gate } else { 54427c478bd9Sstevel@tonic-gate /* Invalid Action */ 54437c478bd9Sstevel@tonic-gate return (Z_INVAL); 54447c478bd9Sstevel@tonic-gate } 54457c478bd9Sstevel@tonic-gate rctlblk_set_local_action(rctlblk, action, 0); 54467c478bd9Sstevel@tonic-gate rctlblk_set_privilege(rctlblk, priv); 54477c478bd9Sstevel@tonic-gate rctlblk_set_value(rctlblk, limit); 54487c478bd9Sstevel@tonic-gate return (Z_OK); 54497c478bd9Sstevel@tonic-gate } 54507c478bd9Sstevel@tonic-gate 54517c478bd9Sstevel@tonic-gate static int 54527c478bd9Sstevel@tonic-gate rctl_check(const char *rctlname, void *arg) 54537c478bd9Sstevel@tonic-gate { 54547c478bd9Sstevel@tonic-gate const char *attrname = arg; 54557c478bd9Sstevel@tonic-gate 54567c478bd9Sstevel@tonic-gate /* 54577c478bd9Sstevel@tonic-gate * Returning 1 here is our signal to zonecfg_is_rctl() that it is 54587c478bd9Sstevel@tonic-gate * indeed an rctl name recognized by the system. 54597c478bd9Sstevel@tonic-gate */ 54607c478bd9Sstevel@tonic-gate return (strcmp(rctlname, attrname) == 0 ? 1 : 0); 54617c478bd9Sstevel@tonic-gate } 54627c478bd9Sstevel@tonic-gate 54637c478bd9Sstevel@tonic-gate boolean_t 54647c478bd9Sstevel@tonic-gate zonecfg_is_rctl(const char *name) 54657c478bd9Sstevel@tonic-gate { 54667c478bd9Sstevel@tonic-gate return (rctl_walk(rctl_check, (void *)name) == 1); 54677c478bd9Sstevel@tonic-gate } 54687c478bd9Sstevel@tonic-gate 54697c478bd9Sstevel@tonic-gate boolean_t 54707c478bd9Sstevel@tonic-gate zonecfg_valid_rctlname(const char *name) 54717c478bd9Sstevel@tonic-gate { 54727c478bd9Sstevel@tonic-gate const char *c; 54737c478bd9Sstevel@tonic-gate 54747c478bd9Sstevel@tonic-gate if (strncmp(name, "zone.", sizeof ("zone.") - 1) != 0) 54757c478bd9Sstevel@tonic-gate return (B_FALSE); 54767c478bd9Sstevel@tonic-gate if (strlen(name) == sizeof ("zone.") - 1) 54777c478bd9Sstevel@tonic-gate return (B_FALSE); 54787c478bd9Sstevel@tonic-gate for (c = name + sizeof ("zone.") - 1; *c != '\0'; c++) { 54797c478bd9Sstevel@tonic-gate if (!isalpha(*c) && *c != '-') 54807c478bd9Sstevel@tonic-gate return (B_FALSE); 54817c478bd9Sstevel@tonic-gate } 54827c478bd9Sstevel@tonic-gate return (B_TRUE); 54837c478bd9Sstevel@tonic-gate } 54847c478bd9Sstevel@tonic-gate 54857c478bd9Sstevel@tonic-gate boolean_t 54867c478bd9Sstevel@tonic-gate zonecfg_valid_rctlblk(const rctlblk_t *rctlblk) 54877c478bd9Sstevel@tonic-gate { 54887c478bd9Sstevel@tonic-gate rctl_priv_t priv = rctlblk_get_privilege((rctlblk_t *)rctlblk); 54897c478bd9Sstevel@tonic-gate uint_t action = rctlblk_get_local_action((rctlblk_t *)rctlblk, NULL); 54907c478bd9Sstevel@tonic-gate 54917c478bd9Sstevel@tonic-gate if (priv != RCPRIV_PRIVILEGED) 54927c478bd9Sstevel@tonic-gate return (B_FALSE); 54937c478bd9Sstevel@tonic-gate if (action != RCTL_LOCAL_NOACTION && action != RCTL_LOCAL_DENY) 54947c478bd9Sstevel@tonic-gate return (B_FALSE); 54957c478bd9Sstevel@tonic-gate return (B_TRUE); 54967c478bd9Sstevel@tonic-gate } 54977c478bd9Sstevel@tonic-gate 54987c478bd9Sstevel@tonic-gate boolean_t 54997c478bd9Sstevel@tonic-gate zonecfg_valid_rctl(const char *name, const rctlblk_t *rctlblk) 55007c478bd9Sstevel@tonic-gate { 55017c478bd9Sstevel@tonic-gate rctlblk_t *current, *next; 55027c478bd9Sstevel@tonic-gate rctl_qty_t limit = rctlblk_get_value((rctlblk_t *)rctlblk); 55037c478bd9Sstevel@tonic-gate uint_t action = rctlblk_get_local_action((rctlblk_t *)rctlblk, NULL); 55047c478bd9Sstevel@tonic-gate uint_t global_flags; 55057c478bd9Sstevel@tonic-gate 55067c478bd9Sstevel@tonic-gate if (!zonecfg_valid_rctlblk(rctlblk)) 55077c478bd9Sstevel@tonic-gate return (B_FALSE); 55087c478bd9Sstevel@tonic-gate if (!zonecfg_valid_rctlname(name)) 55097c478bd9Sstevel@tonic-gate return (B_FALSE); 55107c478bd9Sstevel@tonic-gate 55117c478bd9Sstevel@tonic-gate current = alloca(rctlblk_size()); 55127c478bd9Sstevel@tonic-gate if (getrctl(name, NULL, current, RCTL_FIRST) != 0) 55137c478bd9Sstevel@tonic-gate return (B_TRUE); /* not an rctl on this system */ 55147c478bd9Sstevel@tonic-gate /* 55157c478bd9Sstevel@tonic-gate * Make sure the proposed value isn't greater than the current system 55167c478bd9Sstevel@tonic-gate * value. 55177c478bd9Sstevel@tonic-gate */ 55187c478bd9Sstevel@tonic-gate next = alloca(rctlblk_size()); 55197c478bd9Sstevel@tonic-gate while (rctlblk_get_privilege(current) != RCPRIV_SYSTEM) { 55207c478bd9Sstevel@tonic-gate rctlblk_t *tmp; 55217c478bd9Sstevel@tonic-gate 55227c478bd9Sstevel@tonic-gate if (getrctl(name, current, next, RCTL_NEXT) != 0) 55237c478bd9Sstevel@tonic-gate return (B_FALSE); /* shouldn't happen */ 55247c478bd9Sstevel@tonic-gate tmp = current; 55257c478bd9Sstevel@tonic-gate current = next; 55267c478bd9Sstevel@tonic-gate next = tmp; 55277c478bd9Sstevel@tonic-gate } 55287c478bd9Sstevel@tonic-gate if (limit > rctlblk_get_value(current)) 55297c478bd9Sstevel@tonic-gate return (B_FALSE); 55307c478bd9Sstevel@tonic-gate 55317c478bd9Sstevel@tonic-gate /* 55327c478bd9Sstevel@tonic-gate * Make sure the proposed action is allowed. 55337c478bd9Sstevel@tonic-gate */ 55347c478bd9Sstevel@tonic-gate global_flags = rctlblk_get_global_flags(current); 55357c478bd9Sstevel@tonic-gate if ((global_flags & RCTL_GLOBAL_DENY_NEVER) && 55367c478bd9Sstevel@tonic-gate action == RCTL_LOCAL_DENY) 55377c478bd9Sstevel@tonic-gate return (B_FALSE); 55387c478bd9Sstevel@tonic-gate if ((global_flags & RCTL_GLOBAL_DENY_ALWAYS) && 55397c478bd9Sstevel@tonic-gate action == RCTL_LOCAL_NOACTION) 55407c478bd9Sstevel@tonic-gate return (B_FALSE); 55417c478bd9Sstevel@tonic-gate 55427c478bd9Sstevel@tonic-gate return (B_TRUE); 55437c478bd9Sstevel@tonic-gate } 5544fa9e4066Sahrens 5545cf8f45c7Sdstaff /* 5546cf8f45c7Sdstaff * There is always a race condition between reading the initial copy of 5547cf8f45c7Sdstaff * a zones state and its state changing. We address this by providing 5548cf8f45c7Sdstaff * zonecfg_notify_critical_enter and zonecfg_noticy_critical_exit functions. 5549cf8f45c7Sdstaff * When zonecfg_critical_enter is called, sets the state field to LOCKED 5550cf8f45c7Sdstaff * and aquires biglock. Biglock protects against other threads executing 5551cf8f45c7Sdstaff * critical_enter and the state field protects against state changes during 5552cf8f45c7Sdstaff * the critical period. 5553cf8f45c7Sdstaff * 5554cf8f45c7Sdstaff * If any state changes occur, zn_cb will set the failed field of the znotify 5555cf8f45c7Sdstaff * structure. This will cause the critical_exit function to re-lock the 5556cf8f45c7Sdstaff * channel and return an error. Since evsnts may be delayed, the critical_exit 5557cf8f45c7Sdstaff * function "flushes" the queue by putting an event on the queue and waiting for 5558cf8f45c7Sdstaff * zn_cb to notify critical_exit that it received the ping event. 5559cf8f45c7Sdstaff */ 5560cf8f45c7Sdstaff static const char * 5561cf8f45c7Sdstaff string_get_tok(const char *in, char delim, int num) 5562cf8f45c7Sdstaff { 5563cf8f45c7Sdstaff int i = 0; 5564cf8f45c7Sdstaff 5565cf8f45c7Sdstaff for (; i < num; in++) { 5566cf8f45c7Sdstaff if (*in == delim) 5567cf8f45c7Sdstaff i++; 5568cf8f45c7Sdstaff if (*in == 0) 5569cf8f45c7Sdstaff return (NULL); 5570cf8f45c7Sdstaff } 5571cf8f45c7Sdstaff return (in); 5572cf8f45c7Sdstaff } 5573cf8f45c7Sdstaff 5574cf8f45c7Sdstaff static boolean_t 5575cf8f45c7Sdstaff is_ping(sysevent_t *ev) 5576cf8f45c7Sdstaff { 5577cf8f45c7Sdstaff if (strcmp(sysevent_get_subclass_name(ev), 5578cf8f45c7Sdstaff ZONE_EVENT_PING_SUBCLASS) == 0) { 5579cf8f45c7Sdstaff return (B_TRUE); 5580cf8f45c7Sdstaff } else { 5581cf8f45c7Sdstaff return (B_FALSE); 5582cf8f45c7Sdstaff } 5583cf8f45c7Sdstaff } 5584cf8f45c7Sdstaff 5585cf8f45c7Sdstaff static boolean_t 5586cf8f45c7Sdstaff is_my_ping(sysevent_t *ev) 5587cf8f45c7Sdstaff { 5588cf8f45c7Sdstaff const char *sender; 5589cf8f45c7Sdstaff char mypid[sizeof (pid_t) * 3 + 1]; 5590cf8f45c7Sdstaff 5591cf8f45c7Sdstaff (void) snprintf(mypid, sizeof (mypid), "%i", getpid()); 5592cf8f45c7Sdstaff sender = string_get_tok(sysevent_get_pub(ev), ':', 3); 5593cf8f45c7Sdstaff if (sender == NULL) 5594cf8f45c7Sdstaff return (B_FALSE); 5595cf8f45c7Sdstaff if (strcmp(sender, mypid) != 0) 5596cf8f45c7Sdstaff return (B_FALSE); 5597cf8f45c7Sdstaff return (B_TRUE); 5598cf8f45c7Sdstaff } 5599cf8f45c7Sdstaff 5600cf8f45c7Sdstaff static int 5601cf8f45c7Sdstaff do_callback(struct znotify *zevtchan, sysevent_t *ev) 5602cf8f45c7Sdstaff { 5603cf8f45c7Sdstaff nvlist_t *l; 5604cf8f45c7Sdstaff int zid; 5605cf8f45c7Sdstaff char *zonename; 5606cf8f45c7Sdstaff char *newstate; 5607cf8f45c7Sdstaff char *oldstate; 5608cf8f45c7Sdstaff int ret; 5609cf8f45c7Sdstaff hrtime_t when; 5610cf8f45c7Sdstaff 5611cf8f45c7Sdstaff if (strcmp(sysevent_get_subclass_name(ev), 5612cf8f45c7Sdstaff ZONE_EVENT_STATUS_SUBCLASS) == 0) { 5613cf8f45c7Sdstaff 5614cf8f45c7Sdstaff if (sysevent_get_attr_list(ev, &l) != 0) { 5615cf8f45c7Sdstaff if (errno == ENOMEM) { 5616cf8f45c7Sdstaff zevtchan->zn_failure_count++; 5617cf8f45c7Sdstaff return (EAGAIN); 5618cf8f45c7Sdstaff } 5619cf8f45c7Sdstaff return (0); 5620cf8f45c7Sdstaff } 5621cf8f45c7Sdstaff ret = 0; 5622cf8f45c7Sdstaff 5623cf8f45c7Sdstaff if ((nvlist_lookup_string(l, ZONE_CB_NAME, &zonename) == 0) && 5624cf8f45c7Sdstaff (nvlist_lookup_string(l, ZONE_CB_NEWSTATE, &newstate) 5625cf8f45c7Sdstaff == 0) && 5626cf8f45c7Sdstaff (nvlist_lookup_string(l, ZONE_CB_OLDSTATE, &oldstate) 5627cf8f45c7Sdstaff == 0) && 5628cf8f45c7Sdstaff (nvlist_lookup_uint64(l, ZONE_CB_TIMESTAMP, 5629cf8f45c7Sdstaff (uint64_t *)&when) == 0) && 5630cf8f45c7Sdstaff (nvlist_lookup_int32(l, ZONE_CB_ZONEID, &zid) == 0)) { 5631cf8f45c7Sdstaff ret = zevtchan->zn_callback(zonename, zid, newstate, 5632cf8f45c7Sdstaff oldstate, when, zevtchan->zn_private); 5633cf8f45c7Sdstaff } 5634cf8f45c7Sdstaff 5635cf8f45c7Sdstaff zevtchan->zn_failure_count = 0; 5636cf8f45c7Sdstaff nvlist_free(l); 5637cf8f45c7Sdstaff return (ret); 5638cf8f45c7Sdstaff } else { 5639cf8f45c7Sdstaff /* 5640cf8f45c7Sdstaff * We have received an event in an unknown subclass. Ignore. 5641cf8f45c7Sdstaff */ 5642cf8f45c7Sdstaff zevtchan->zn_failure_count = 0; 5643cf8f45c7Sdstaff return (0); 5644cf8f45c7Sdstaff } 5645cf8f45c7Sdstaff } 5646cf8f45c7Sdstaff 5647cf8f45c7Sdstaff static int 5648cf8f45c7Sdstaff zn_cb(sysevent_t *ev, void *p) 5649cf8f45c7Sdstaff { 5650cf8f45c7Sdstaff struct znotify *zevtchan = p; 5651cf8f45c7Sdstaff int error; 5652cf8f45c7Sdstaff 5653cf8f45c7Sdstaff (void) pthread_mutex_lock(&(zevtchan->zn_mutex)); 5654cf8f45c7Sdstaff 5655cf8f45c7Sdstaff if (is_ping(ev) && !is_my_ping(ev)) { 5656cf8f45c7Sdstaff (void) pthread_mutex_unlock((&zevtchan->zn_mutex)); 5657cf8f45c7Sdstaff return (0); 5658cf8f45c7Sdstaff } 5659cf8f45c7Sdstaff 5660cf8f45c7Sdstaff if (zevtchan->zn_state == ZN_LOCKED) { 5661cf8f45c7Sdstaff assert(!is_ping(ev)); 5662cf8f45c7Sdstaff zevtchan->zn_failed = B_TRUE; 5663cf8f45c7Sdstaff (void) pthread_mutex_unlock(&(zevtchan->zn_mutex)); 5664cf8f45c7Sdstaff return (0); 5665cf8f45c7Sdstaff } 5666cf8f45c7Sdstaff 5667cf8f45c7Sdstaff if (zevtchan->zn_state == ZN_PING_INFLIGHT) { 5668cf8f45c7Sdstaff if (is_ping(ev)) { 5669cf8f45c7Sdstaff zevtchan->zn_state = ZN_PING_RECEIVED; 5670cf8f45c7Sdstaff (void) pthread_cond_signal(&(zevtchan->zn_cond)); 5671cf8f45c7Sdstaff (void) pthread_mutex_unlock(&(zevtchan->zn_mutex)); 5672cf8f45c7Sdstaff return (0); 5673cf8f45c7Sdstaff } else { 5674cf8f45c7Sdstaff zevtchan->zn_failed = B_TRUE; 5675cf8f45c7Sdstaff (void) pthread_mutex_unlock(&(zevtchan->zn_mutex)); 5676cf8f45c7Sdstaff return (0); 5677cf8f45c7Sdstaff } 5678cf8f45c7Sdstaff } 5679cf8f45c7Sdstaff 5680cf8f45c7Sdstaff if (zevtchan->zn_state == ZN_UNLOCKED) { 5681cf8f45c7Sdstaff 5682cf8f45c7Sdstaff error = do_callback(zevtchan, ev); 5683cf8f45c7Sdstaff (void) pthread_mutex_unlock(&(zevtchan->zn_mutex)); 5684cf8f45c7Sdstaff /* 5685cf8f45c7Sdstaff * Every ENOMEM failure causes do_callback to increment 5686cf8f45c7Sdstaff * zn_failure_count and every success causes it to 5687cf8f45c7Sdstaff * set zn_failure_count to zero. If we got EAGAIN, 5688cf8f45c7Sdstaff * we will sleep for zn_failure_count seconds and return 5689cf8f45c7Sdstaff * EAGAIN to gpec to try again. 5690cf8f45c7Sdstaff * 5691cf8f45c7Sdstaff * After 55 seconds, or 10 try's we give up and drop the 5692cf8f45c7Sdstaff * event. 5693cf8f45c7Sdstaff */ 5694cf8f45c7Sdstaff if (error == EAGAIN) { 5695cf8f45c7Sdstaff if (zevtchan->zn_failure_count > ZONE_CB_RETRY_COUNT) { 5696cf8f45c7Sdstaff return (0); 5697cf8f45c7Sdstaff } 5698cf8f45c7Sdstaff (void) sleep(zevtchan->zn_failure_count); 5699cf8f45c7Sdstaff } 5700cf8f45c7Sdstaff return (error); 5701cf8f45c7Sdstaff } 5702cf8f45c7Sdstaff 5703cf8f45c7Sdstaff if (zevtchan->zn_state == ZN_PING_RECEIVED) { 5704cf8f45c7Sdstaff (void) pthread_mutex_unlock(&(zevtchan->zn_mutex)); 5705cf8f45c7Sdstaff return (0); 5706cf8f45c7Sdstaff } 5707cf8f45c7Sdstaff 5708cf8f45c7Sdstaff abort(); 5709cf8f45c7Sdstaff return (0); 5710cf8f45c7Sdstaff } 5711cf8f45c7Sdstaff 5712cf8f45c7Sdstaff void 5713cf8f45c7Sdstaff zonecfg_notify_critical_enter(void *h) 5714cf8f45c7Sdstaff { 5715cf8f45c7Sdstaff struct znotify *zevtchan = h; 5716cf8f45c7Sdstaff 5717cf8f45c7Sdstaff (void) pthread_mutex_lock(&(zevtchan->zn_bigmutex)); 5718cf8f45c7Sdstaff zevtchan->zn_state = ZN_LOCKED; 5719cf8f45c7Sdstaff } 5720cf8f45c7Sdstaff 5721cf8f45c7Sdstaff int 5722cf8f45c7Sdstaff zonecfg_notify_critical_exit(void * h) 5723cf8f45c7Sdstaff { 5724cf8f45c7Sdstaff 5725cf8f45c7Sdstaff struct znotify *zevtchan = h; 5726cf8f45c7Sdstaff 5727cf8f45c7Sdstaff if (zevtchan->zn_state == ZN_UNLOCKED) 5728cf8f45c7Sdstaff return (0); 5729cf8f45c7Sdstaff 5730cf8f45c7Sdstaff (void) pthread_mutex_lock(&(zevtchan->zn_mutex)); 5731cf8f45c7Sdstaff zevtchan->zn_state = ZN_PING_INFLIGHT; 5732cf8f45c7Sdstaff 5733ee519a1fSgjelinek (void) sysevent_evc_publish(zevtchan->zn_eventchan, 5734ee519a1fSgjelinek ZONE_EVENT_STATUS_CLASS, 5735cf8f45c7Sdstaff ZONE_EVENT_PING_SUBCLASS, ZONE_EVENT_PING_PUBLISHER, 5736cf8f45c7Sdstaff zevtchan->zn_subscriber_id, NULL, EVCH_SLEEP); 5737cf8f45c7Sdstaff 5738cf8f45c7Sdstaff while (zevtchan->zn_state != ZN_PING_RECEIVED) { 5739cf8f45c7Sdstaff (void) pthread_cond_wait(&(zevtchan->zn_cond), 5740cf8f45c7Sdstaff &(zevtchan->zn_mutex)); 5741cf8f45c7Sdstaff } 5742cf8f45c7Sdstaff 5743cf8f45c7Sdstaff if (zevtchan->zn_failed == B_TRUE) { 5744cf8f45c7Sdstaff zevtchan->zn_state = ZN_LOCKED; 5745cf8f45c7Sdstaff zevtchan->zn_failed = B_FALSE; 5746cf8f45c7Sdstaff (void) pthread_mutex_unlock(&(zevtchan->zn_mutex)); 5747cf8f45c7Sdstaff return (1); 5748cf8f45c7Sdstaff } 5749cf8f45c7Sdstaff 5750cf8f45c7Sdstaff zevtchan->zn_state = ZN_UNLOCKED; 5751cf8f45c7Sdstaff (void) pthread_mutex_unlock(&(zevtchan->zn_mutex)); 5752cf8f45c7Sdstaff (void) pthread_mutex_unlock(&(zevtchan->zn_bigmutex)); 5753cf8f45c7Sdstaff return (0); 5754cf8f45c7Sdstaff } 5755cf8f45c7Sdstaff 5756cf8f45c7Sdstaff void 5757cf8f45c7Sdstaff zonecfg_notify_critical_abort(void *h) 5758cf8f45c7Sdstaff { 5759cf8f45c7Sdstaff struct znotify *zevtchan = h; 5760cf8f45c7Sdstaff 5761cf8f45c7Sdstaff zevtchan->zn_state = ZN_UNLOCKED; 5762cf8f45c7Sdstaff zevtchan->zn_failed = B_FALSE; 5763cf8f45c7Sdstaff /* 5764cf8f45c7Sdstaff * Don't do anything about zn_lock. If it is held, it could only be 5765cf8f45c7Sdstaff * held by zn_cb and it will be unlocked soon. 5766cf8f45c7Sdstaff */ 5767cf8f45c7Sdstaff (void) pthread_mutex_unlock(&(zevtchan->zn_bigmutex)); 5768cf8f45c7Sdstaff } 5769cf8f45c7Sdstaff 5770cf8f45c7Sdstaff void * 5771cf8f45c7Sdstaff zonecfg_notify_bind(int(*func)(const char *zonename, zoneid_t zid, 5772cf8f45c7Sdstaff const char *newstate, const char *oldstate, hrtime_t when, void *p), 5773cf8f45c7Sdstaff void *p) 5774cf8f45c7Sdstaff { 5775cf8f45c7Sdstaff struct znotify *zevtchan; 5776cf8f45c7Sdstaff int i = 1; 5777cf8f45c7Sdstaff int r; 5778cf8f45c7Sdstaff 5779cf8f45c7Sdstaff zevtchan = malloc(sizeof (struct znotify)); 5780cf8f45c7Sdstaff 5781cf8f45c7Sdstaff if (zevtchan == NULL) 5782cf8f45c7Sdstaff return (NULL); 5783cf8f45c7Sdstaff 5784cf8f45c7Sdstaff zevtchan->zn_private = p; 5785cf8f45c7Sdstaff zevtchan->zn_callback = func; 5786cf8f45c7Sdstaff zevtchan->zn_state = ZN_UNLOCKED; 5787cf8f45c7Sdstaff zevtchan->zn_failed = B_FALSE; 5788cf8f45c7Sdstaff 5789cf8f45c7Sdstaff if (pthread_mutex_init(&(zevtchan->zn_mutex), NULL)) 57909d4be64eSdstaff goto out3; 5791cf8f45c7Sdstaff if (pthread_cond_init(&(zevtchan->zn_cond), NULL)) { 5792cf8f45c7Sdstaff (void) pthread_mutex_destroy(&(zevtchan->zn_mutex)); 57939d4be64eSdstaff goto out3; 5794cf8f45c7Sdstaff } 5795cf8f45c7Sdstaff if (pthread_mutex_init(&(zevtchan->zn_bigmutex), NULL)) { 5796cf8f45c7Sdstaff (void) pthread_mutex_destroy(&(zevtchan->zn_mutex)); 5797cf8f45c7Sdstaff (void) pthread_cond_destroy(&(zevtchan->zn_cond)); 57989d4be64eSdstaff goto out3; 5799cf8f45c7Sdstaff } 5800cf8f45c7Sdstaff 5801cf8f45c7Sdstaff if (sysevent_evc_bind(ZONE_EVENT_CHANNEL, &(zevtchan->zn_eventchan), 5802cf8f45c7Sdstaff 0) != 0) 5803cf8f45c7Sdstaff goto out2; 5804cf8f45c7Sdstaff 5805cf8f45c7Sdstaff do { 5806cf8f45c7Sdstaff /* 5807cf8f45c7Sdstaff * At 4 digits the subscriber ID gets too long and we have 5808cf8f45c7Sdstaff * no chance of successfully registering. 5809cf8f45c7Sdstaff */ 5810cf8f45c7Sdstaff if (i > 999) 58119d4be64eSdstaff goto out1; 5812cf8f45c7Sdstaff 5813cf8f45c7Sdstaff (void) sprintf(zevtchan->zn_subscriber_id, "zone_%li_%i", 5814cf8f45c7Sdstaff getpid() % 999999l, i); 5815cf8f45c7Sdstaff 5816cf8f45c7Sdstaff r = sysevent_evc_subscribe(zevtchan->zn_eventchan, 5817cf8f45c7Sdstaff zevtchan->zn_subscriber_id, ZONE_EVENT_STATUS_CLASS, zn_cb, 5818cf8f45c7Sdstaff zevtchan, 0); 5819cf8f45c7Sdstaff 5820cf8f45c7Sdstaff i++; 5821cf8f45c7Sdstaff 5822cf8f45c7Sdstaff } while (r); 5823cf8f45c7Sdstaff 5824cf8f45c7Sdstaff return (zevtchan); 58259d4be64eSdstaff out1: 5826cf8f45c7Sdstaff sysevent_evc_unbind(zevtchan->zn_eventchan); 58279d4be64eSdstaff out2: 5828cf8f45c7Sdstaff (void) pthread_mutex_destroy(&zevtchan->zn_mutex); 5829cf8f45c7Sdstaff (void) pthread_cond_destroy(&zevtchan->zn_cond); 5830cf8f45c7Sdstaff (void) pthread_mutex_destroy(&(zevtchan->zn_bigmutex)); 58319d4be64eSdstaff out3: 5832cf8f45c7Sdstaff free(zevtchan); 5833cf8f45c7Sdstaff 5834cf8f45c7Sdstaff return (NULL); 5835cf8f45c7Sdstaff } 5836cf8f45c7Sdstaff 5837cf8f45c7Sdstaff void 5838cf8f45c7Sdstaff zonecfg_notify_unbind(void *handle) 5839cf8f45c7Sdstaff { 5840cf8f45c7Sdstaff 5841cf8f45c7Sdstaff int ret; 5842cf8f45c7Sdstaff 5843cf8f45c7Sdstaff sysevent_evc_unbind(((struct znotify *)handle)->zn_eventchan); 5844cf8f45c7Sdstaff /* 5845cf8f45c7Sdstaff * Check that all evc threads have gone away. This should be 5846cf8f45c7Sdstaff * enforced by sysevent_evc_unbind. 5847cf8f45c7Sdstaff */ 5848cf8f45c7Sdstaff ret = pthread_mutex_trylock(&((struct znotify *)handle)->zn_mutex); 5849cf8f45c7Sdstaff 5850cf8f45c7Sdstaff if (ret) 5851cf8f45c7Sdstaff abort(); 5852cf8f45c7Sdstaff 5853cf8f45c7Sdstaff (void) pthread_mutex_unlock(&((struct znotify *)handle)->zn_mutex); 5854cf8f45c7Sdstaff (void) pthread_mutex_destroy(&((struct znotify *)handle)->zn_mutex); 5855cf8f45c7Sdstaff (void) pthread_cond_destroy(&((struct znotify *)handle)->zn_cond); 5856cf8f45c7Sdstaff (void) pthread_mutex_destroy(&((struct znotify *)handle)->zn_bigmutex); 5857cf8f45c7Sdstaff 5858cf8f45c7Sdstaff free(handle); 5859cf8f45c7Sdstaff } 5860cf8f45c7Sdstaff 5861fa9e4066Sahrens static int 5862fa9e4066Sahrens zonecfg_add_ds_core(zone_dochandle_t handle, struct zone_dstab *tabptr) 5863fa9e4066Sahrens { 5864fa9e4066Sahrens xmlNodePtr newnode, cur = handle->zone_dh_cur; 5865fa9e4066Sahrens int err; 5866fa9e4066Sahrens 5867fa9e4066Sahrens newnode = xmlNewTextChild(cur, NULL, DTD_ELEM_DATASET, NULL); 5868fa9e4066Sahrens if ((err = newprop(newnode, DTD_ATTR_NAME, 5869fa9e4066Sahrens tabptr->zone_dataset_name)) != Z_OK) 5870fa9e4066Sahrens return (err); 5871fa9e4066Sahrens return (Z_OK); 5872fa9e4066Sahrens } 5873fa9e4066Sahrens 5874fa9e4066Sahrens int 5875fa9e4066Sahrens zonecfg_add_ds(zone_dochandle_t handle, struct zone_dstab *tabptr) 5876fa9e4066Sahrens { 5877fa9e4066Sahrens int err; 5878fa9e4066Sahrens 5879fa9e4066Sahrens if (tabptr == NULL) 5880fa9e4066Sahrens return (Z_INVAL); 5881fa9e4066Sahrens 5882fa9e4066Sahrens if ((err = operation_prep(handle)) != Z_OK) 5883fa9e4066Sahrens return (err); 5884fa9e4066Sahrens 5885fa9e4066Sahrens if ((err = zonecfg_add_ds_core(handle, tabptr)) != Z_OK) 5886fa9e4066Sahrens return (err); 5887fa9e4066Sahrens 5888fa9e4066Sahrens return (Z_OK); 5889fa9e4066Sahrens } 5890fa9e4066Sahrens 5891fa9e4066Sahrens static int 5892fa9e4066Sahrens zonecfg_delete_ds_core(zone_dochandle_t handle, struct zone_dstab *tabptr) 5893fa9e4066Sahrens { 5894fa9e4066Sahrens xmlNodePtr cur = handle->zone_dh_cur; 5895fa9e4066Sahrens 5896fa9e4066Sahrens for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) { 5897fa9e4066Sahrens if (xmlStrcmp(cur->name, DTD_ELEM_DATASET)) 5898fa9e4066Sahrens continue; 5899fa9e4066Sahrens 5900fa9e4066Sahrens if (match_prop(cur, DTD_ATTR_NAME, 5901fa9e4066Sahrens tabptr->zone_dataset_name)) { 5902fa9e4066Sahrens xmlUnlinkNode(cur); 5903fa9e4066Sahrens xmlFreeNode(cur); 5904fa9e4066Sahrens return (Z_OK); 5905fa9e4066Sahrens } 5906fa9e4066Sahrens } 5907fa9e4066Sahrens return (Z_NO_RESOURCE_ID); 5908fa9e4066Sahrens } 5909fa9e4066Sahrens 5910fa9e4066Sahrens int 5911fa9e4066Sahrens zonecfg_delete_ds(zone_dochandle_t handle, struct zone_dstab *tabptr) 5912fa9e4066Sahrens { 5913fa9e4066Sahrens int err; 5914fa9e4066Sahrens 5915fa9e4066Sahrens if (tabptr == NULL) 5916fa9e4066Sahrens return (Z_INVAL); 5917fa9e4066Sahrens 5918fa9e4066Sahrens if ((err = operation_prep(handle)) != Z_OK) 5919fa9e4066Sahrens return (err); 5920fa9e4066Sahrens 5921fa9e4066Sahrens if ((err = zonecfg_delete_ds_core(handle, tabptr)) != Z_OK) 5922fa9e4066Sahrens return (err); 5923fa9e4066Sahrens 5924fa9e4066Sahrens return (Z_OK); 5925fa9e4066Sahrens } 5926fa9e4066Sahrens 5927fa9e4066Sahrens int 5928fa9e4066Sahrens zonecfg_modify_ds( 5929fa9e4066Sahrens zone_dochandle_t handle, 5930fa9e4066Sahrens struct zone_dstab *oldtabptr, 5931fa9e4066Sahrens struct zone_dstab *newtabptr) 5932fa9e4066Sahrens { 5933fa9e4066Sahrens int err; 5934fa9e4066Sahrens 5935fa9e4066Sahrens if (oldtabptr == NULL || newtabptr == NULL) 5936fa9e4066Sahrens return (Z_INVAL); 5937fa9e4066Sahrens 5938fa9e4066Sahrens if ((err = operation_prep(handle)) != Z_OK) 5939fa9e4066Sahrens return (err); 5940fa9e4066Sahrens 5941fa9e4066Sahrens if ((err = zonecfg_delete_ds_core(handle, oldtabptr)) != Z_OK) 5942fa9e4066Sahrens return (err); 5943fa9e4066Sahrens 5944fa9e4066Sahrens if ((err = zonecfg_add_ds_core(handle, newtabptr)) != Z_OK) 5945fa9e4066Sahrens return (err); 5946fa9e4066Sahrens 5947fa9e4066Sahrens return (Z_OK); 5948fa9e4066Sahrens } 5949fa9e4066Sahrens 5950fa9e4066Sahrens int 5951fa9e4066Sahrens zonecfg_lookup_ds(zone_dochandle_t handle, struct zone_dstab *tabptr) 5952fa9e4066Sahrens { 5953fa9e4066Sahrens xmlNodePtr cur, firstmatch; 5954fa9e4066Sahrens int err; 5955fa9e4066Sahrens char dataset[MAXNAMELEN]; 5956fa9e4066Sahrens 5957fa9e4066Sahrens if (tabptr == NULL) 5958fa9e4066Sahrens return (Z_INVAL); 5959fa9e4066Sahrens 5960fa9e4066Sahrens if ((err = operation_prep(handle)) != Z_OK) 5961fa9e4066Sahrens return (err); 5962fa9e4066Sahrens 5963fa9e4066Sahrens cur = handle->zone_dh_cur; 5964fa9e4066Sahrens firstmatch = NULL; 5965fa9e4066Sahrens for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) { 5966fa9e4066Sahrens if (xmlStrcmp(cur->name, DTD_ELEM_DATASET)) 5967fa9e4066Sahrens continue; 5968fa9e4066Sahrens if (strlen(tabptr->zone_dataset_name) > 0) { 5969fa9e4066Sahrens if ((fetchprop(cur, DTD_ATTR_NAME, dataset, 5970fa9e4066Sahrens sizeof (dataset)) == Z_OK) && 5971fa9e4066Sahrens (strcmp(tabptr->zone_dataset_name, 5972fa9e4066Sahrens dataset) == 0)) { 5973fa9e4066Sahrens if (firstmatch == NULL) 5974fa9e4066Sahrens firstmatch = cur; 5975fa9e4066Sahrens else 5976fa9e4066Sahrens return (Z_INSUFFICIENT_SPEC); 5977fa9e4066Sahrens } 5978fa9e4066Sahrens } 5979fa9e4066Sahrens } 5980fa9e4066Sahrens if (firstmatch == NULL) 5981fa9e4066Sahrens return (Z_NO_RESOURCE_ID); 5982fa9e4066Sahrens 5983fa9e4066Sahrens cur = firstmatch; 5984fa9e4066Sahrens 5985fa9e4066Sahrens if ((err = fetchprop(cur, DTD_ATTR_NAME, tabptr->zone_dataset_name, 5986fa9e4066Sahrens sizeof (tabptr->zone_dataset_name))) != Z_OK) 5987fa9e4066Sahrens return (err); 5988fa9e4066Sahrens 5989fa9e4066Sahrens return (Z_OK); 5990fa9e4066Sahrens } 5991fa9e4066Sahrens 5992fa9e4066Sahrens int 5993fa9e4066Sahrens zonecfg_setdsent(zone_dochandle_t handle) 5994fa9e4066Sahrens { 5995fa9e4066Sahrens return (zonecfg_setent(handle)); 5996fa9e4066Sahrens } 5997fa9e4066Sahrens 5998fa9e4066Sahrens int 5999fa9e4066Sahrens zonecfg_getdsent(zone_dochandle_t handle, struct zone_dstab *tabptr) 6000fa9e4066Sahrens { 6001fa9e4066Sahrens xmlNodePtr cur; 6002fa9e4066Sahrens int err; 6003fa9e4066Sahrens 6004fa9e4066Sahrens if (handle == NULL) 6005fa9e4066Sahrens return (Z_INVAL); 6006fa9e4066Sahrens 6007fa9e4066Sahrens if ((cur = handle->zone_dh_cur) == NULL) 6008fa9e4066Sahrens return (Z_NO_ENTRY); 6009fa9e4066Sahrens 6010fa9e4066Sahrens for (; cur != NULL; cur = cur->next) 6011fa9e4066Sahrens if (!xmlStrcmp(cur->name, DTD_ELEM_DATASET)) 6012fa9e4066Sahrens break; 6013fa9e4066Sahrens if (cur == NULL) { 6014fa9e4066Sahrens handle->zone_dh_cur = handle->zone_dh_top; 6015fa9e4066Sahrens return (Z_NO_ENTRY); 6016fa9e4066Sahrens } 6017fa9e4066Sahrens 6018fa9e4066Sahrens if ((err = fetchprop(cur, DTD_ATTR_NAME, tabptr->zone_dataset_name, 6019fa9e4066Sahrens sizeof (tabptr->zone_dataset_name))) != Z_OK) { 6020fa9e4066Sahrens handle->zone_dh_cur = handle->zone_dh_top; 6021fa9e4066Sahrens return (err); 6022fa9e4066Sahrens } 6023fa9e4066Sahrens 6024fa9e4066Sahrens handle->zone_dh_cur = cur->next; 6025fa9e4066Sahrens return (Z_OK); 6026fa9e4066Sahrens } 6027fa9e4066Sahrens 6028fa9e4066Sahrens int 6029fa9e4066Sahrens zonecfg_enddsent(zone_dochandle_t handle) 6030fa9e4066Sahrens { 6031fa9e4066Sahrens return (zonecfg_endent(handle)); 6032fa9e4066Sahrens } 6033ee519a1fSgjelinek 60340209230bSgjelinek /* 60350209230bSgjelinek * Support for aliased rctls; that is, rctls that have simplified names in 60360209230bSgjelinek * zonecfg. For example, max-lwps is an alias for a well defined zone.max-lwps 60370209230bSgjelinek * rctl. If there are multiple existing values for one of these rctls or if 60380209230bSgjelinek * there is a single value that does not match the well defined template (i.e. 60390209230bSgjelinek * it has a different action) then we cannot treat the rctl as having an alias 60400209230bSgjelinek * so we return Z_ALIAS_DISALLOW. That means that the rctl cannot be 60410209230bSgjelinek * managed in zonecfg via an alias and that the standard rctl syntax must be 60420209230bSgjelinek * used. 60430209230bSgjelinek * 60440209230bSgjelinek * The possible return values are: 60450209230bSgjelinek * Z_NO_PROPERTY_ID - invalid alias name 60460209230bSgjelinek * Z_ALIAS_DISALLOW - pre-existing, incompatible rctl definition 60470209230bSgjelinek * Z_NO_ENTRY - no rctl is configured for this alias 60480209230bSgjelinek * Z_OK - we got a valid rctl for the specified alias 60490209230bSgjelinek */ 60500209230bSgjelinek int 60510209230bSgjelinek zonecfg_get_aliased_rctl(zone_dochandle_t handle, char *name, uint64_t *rval) 60520209230bSgjelinek { 60530209230bSgjelinek boolean_t found = B_FALSE; 60540209230bSgjelinek boolean_t found_val = B_FALSE; 60550209230bSgjelinek xmlNodePtr cur, val; 60560209230bSgjelinek char savedname[MAXNAMELEN]; 60570209230bSgjelinek struct zone_rctlvaltab rctl; 60580209230bSgjelinek int i; 60590209230bSgjelinek int err; 60600209230bSgjelinek 60610209230bSgjelinek for (i = 0; aliases[i].shortname != NULL; i++) 60620209230bSgjelinek if (strcmp(name, aliases[i].shortname) == 0) 60630209230bSgjelinek break; 60640209230bSgjelinek 60650209230bSgjelinek if (aliases[i].shortname == NULL) 60660209230bSgjelinek return (Z_NO_PROPERTY_ID); 60670209230bSgjelinek 60680209230bSgjelinek if ((err = operation_prep(handle)) != Z_OK) 60690209230bSgjelinek return (err); 60700209230bSgjelinek 60710209230bSgjelinek cur = handle->zone_dh_cur; 60720209230bSgjelinek for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) { 60730209230bSgjelinek if (xmlStrcmp(cur->name, DTD_ELEM_RCTL) != 0) 60740209230bSgjelinek continue; 60750209230bSgjelinek if ((fetchprop(cur, DTD_ATTR_NAME, savedname, 60760209230bSgjelinek sizeof (savedname)) == Z_OK) && 60770209230bSgjelinek (strcmp(savedname, aliases[i].realname) == 0)) { 60780209230bSgjelinek 60790209230bSgjelinek /* 60800209230bSgjelinek * If we already saw one of these, we can't have an 60810209230bSgjelinek * alias since we just found another. 60820209230bSgjelinek */ 60830209230bSgjelinek if (found) 60840209230bSgjelinek return (Z_ALIAS_DISALLOW); 60850209230bSgjelinek found = B_TRUE; 60860209230bSgjelinek 60870209230bSgjelinek for (val = cur->xmlChildrenNode; val != NULL; 60880209230bSgjelinek val = val->next) { 60890209230bSgjelinek /* 60900209230bSgjelinek * If we already have one value, we can't have 60910209230bSgjelinek * an alias since we just found another. 60920209230bSgjelinek */ 60930209230bSgjelinek if (found_val) 60940209230bSgjelinek return (Z_ALIAS_DISALLOW); 60950209230bSgjelinek found_val = B_TRUE; 60960209230bSgjelinek 60970209230bSgjelinek if ((fetchprop(val, DTD_ATTR_PRIV, 60980209230bSgjelinek rctl.zone_rctlval_priv, 60990209230bSgjelinek sizeof (rctl.zone_rctlval_priv)) != Z_OK)) 61000209230bSgjelinek break; 61010209230bSgjelinek if ((fetchprop(val, DTD_ATTR_LIMIT, 61020209230bSgjelinek rctl.zone_rctlval_limit, 61030209230bSgjelinek sizeof (rctl.zone_rctlval_limit)) != Z_OK)) 61040209230bSgjelinek break; 61050209230bSgjelinek if ((fetchprop(val, DTD_ATTR_ACTION, 61060209230bSgjelinek rctl.zone_rctlval_action, 61070209230bSgjelinek sizeof (rctl.zone_rctlval_action)) != Z_OK)) 61080209230bSgjelinek break; 61090209230bSgjelinek } 61100209230bSgjelinek 61110209230bSgjelinek /* check priv and action match the expected vals */ 61120209230bSgjelinek if (strcmp(rctl.zone_rctlval_priv, 61130209230bSgjelinek aliases[i].priv) != 0 || 61140209230bSgjelinek strcmp(rctl.zone_rctlval_action, 61150209230bSgjelinek aliases[i].action) != 0) 61160209230bSgjelinek return (Z_ALIAS_DISALLOW); 61170209230bSgjelinek } 61180209230bSgjelinek } 61190209230bSgjelinek 61200209230bSgjelinek if (found) { 61210209230bSgjelinek *rval = strtoull(rctl.zone_rctlval_limit, NULL, 10); 61220209230bSgjelinek return (Z_OK); 61230209230bSgjelinek } 61240209230bSgjelinek 61250209230bSgjelinek return (Z_NO_ENTRY); 61260209230bSgjelinek } 61270209230bSgjelinek 61280209230bSgjelinek int 61290209230bSgjelinek zonecfg_rm_aliased_rctl(zone_dochandle_t handle, char *name) 61300209230bSgjelinek { 61310209230bSgjelinek int i; 61320209230bSgjelinek uint64_t val; 61330209230bSgjelinek struct zone_rctltab rctltab; 61340209230bSgjelinek 61350209230bSgjelinek /* 61360209230bSgjelinek * First check that we have a valid aliased rctl to remove. 61370209230bSgjelinek * This will catch an rctl entry with non-standard values or 61380209230bSgjelinek * multiple rctl values for this name. We need to ignore those 61390209230bSgjelinek * rctl entries. 61400209230bSgjelinek */ 61410209230bSgjelinek if (zonecfg_get_aliased_rctl(handle, name, &val) != Z_OK) 61420209230bSgjelinek return (Z_OK); 61430209230bSgjelinek 61440209230bSgjelinek for (i = 0; aliases[i].shortname != NULL; i++) 61450209230bSgjelinek if (strcmp(name, aliases[i].shortname) == 0) 61460209230bSgjelinek break; 61470209230bSgjelinek 61480209230bSgjelinek if (aliases[i].shortname == NULL) 61490209230bSgjelinek return (Z_NO_RESOURCE_ID); 61500209230bSgjelinek 61510209230bSgjelinek (void) strlcpy(rctltab.zone_rctl_name, aliases[i].realname, 61520209230bSgjelinek sizeof (rctltab.zone_rctl_name)); 61530209230bSgjelinek 61540209230bSgjelinek return (zonecfg_delete_rctl(handle, &rctltab)); 61550209230bSgjelinek } 61560209230bSgjelinek 61570209230bSgjelinek boolean_t 61580209230bSgjelinek zonecfg_aliased_rctl_ok(zone_dochandle_t handle, char *name) 61590209230bSgjelinek { 61600209230bSgjelinek uint64_t tmp_val; 61610209230bSgjelinek 61620209230bSgjelinek switch (zonecfg_get_aliased_rctl(handle, name, &tmp_val)) { 61630209230bSgjelinek case Z_OK: 61640209230bSgjelinek /*FALLTHRU*/ 61650209230bSgjelinek case Z_NO_ENTRY: 61660209230bSgjelinek return (B_TRUE); 61670209230bSgjelinek default: 61680209230bSgjelinek return (B_FALSE); 61690209230bSgjelinek } 61700209230bSgjelinek } 61710209230bSgjelinek 61720209230bSgjelinek int 61730209230bSgjelinek zonecfg_set_aliased_rctl(zone_dochandle_t handle, char *name, uint64_t val) 61740209230bSgjelinek { 61750209230bSgjelinek int i; 61760209230bSgjelinek int err; 61770209230bSgjelinek struct zone_rctltab rctltab; 61780209230bSgjelinek struct zone_rctlvaltab *rctlvaltab; 61790209230bSgjelinek char buf[128]; 61800209230bSgjelinek 61810209230bSgjelinek if (!zonecfg_aliased_rctl_ok(handle, name)) 61820209230bSgjelinek return (Z_ALIAS_DISALLOW); 61830209230bSgjelinek 61840209230bSgjelinek for (i = 0; aliases[i].shortname != NULL; i++) 61850209230bSgjelinek if (strcmp(name, aliases[i].shortname) == 0) 61860209230bSgjelinek break; 61870209230bSgjelinek 61880209230bSgjelinek if (aliases[i].shortname == NULL) 61890209230bSgjelinek return (Z_NO_RESOURCE_ID); 61900209230bSgjelinek 61910209230bSgjelinek /* remove any pre-existing definition for this rctl */ 61920209230bSgjelinek (void) zonecfg_rm_aliased_rctl(handle, name); 61930209230bSgjelinek 61940209230bSgjelinek (void) strlcpy(rctltab.zone_rctl_name, aliases[i].realname, 61950209230bSgjelinek sizeof (rctltab.zone_rctl_name)); 61960209230bSgjelinek 61970209230bSgjelinek rctltab.zone_rctl_valptr = NULL; 61980209230bSgjelinek 61990209230bSgjelinek if ((rctlvaltab = calloc(1, sizeof (struct zone_rctlvaltab))) == NULL) 62000209230bSgjelinek return (Z_NOMEM); 62010209230bSgjelinek 62020209230bSgjelinek (void) snprintf(buf, sizeof (buf), "%llu", (long long)val); 62030209230bSgjelinek 62040209230bSgjelinek (void) strlcpy(rctlvaltab->zone_rctlval_priv, aliases[i].priv, 62050209230bSgjelinek sizeof (rctlvaltab->zone_rctlval_priv)); 62060209230bSgjelinek (void) strlcpy(rctlvaltab->zone_rctlval_limit, buf, 62070209230bSgjelinek sizeof (rctlvaltab->zone_rctlval_limit)); 62080209230bSgjelinek (void) strlcpy(rctlvaltab->zone_rctlval_action, aliases[i].action, 62090209230bSgjelinek sizeof (rctlvaltab->zone_rctlval_action)); 62100209230bSgjelinek 62110209230bSgjelinek rctlvaltab->zone_rctlval_next = NULL; 62120209230bSgjelinek 62130209230bSgjelinek if ((err = zonecfg_add_rctl_value(&rctltab, rctlvaltab)) != Z_OK) 62140209230bSgjelinek return (err); 62150209230bSgjelinek 62160209230bSgjelinek return (zonecfg_add_rctl(handle, &rctltab)); 62170209230bSgjelinek } 62180209230bSgjelinek 62190209230bSgjelinek static int 62200209230bSgjelinek delete_tmp_pool(zone_dochandle_t handle) 62210209230bSgjelinek { 62220209230bSgjelinek int err; 62230209230bSgjelinek xmlNodePtr cur = handle->zone_dh_cur; 62240209230bSgjelinek 62250209230bSgjelinek if ((err = operation_prep(handle)) != Z_OK) 62260209230bSgjelinek return (err); 62270209230bSgjelinek 62280209230bSgjelinek for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) { 62290209230bSgjelinek if (xmlStrcmp(cur->name, DTD_ELEM_TMPPOOL) == 0) { 62300209230bSgjelinek xmlUnlinkNode(cur); 62310209230bSgjelinek xmlFreeNode(cur); 62320209230bSgjelinek return (Z_OK); 62330209230bSgjelinek } 62340209230bSgjelinek } 62350209230bSgjelinek 62360209230bSgjelinek return (Z_NO_RESOURCE_ID); 62370209230bSgjelinek } 62380209230bSgjelinek 62390209230bSgjelinek static int 62400209230bSgjelinek modify_tmp_pool(zone_dochandle_t handle, char *pool_importance) 62410209230bSgjelinek { 62420209230bSgjelinek int err; 62430209230bSgjelinek xmlNodePtr cur = handle->zone_dh_cur; 62440209230bSgjelinek xmlNodePtr newnode; 62450209230bSgjelinek 62460209230bSgjelinek err = delete_tmp_pool(handle); 62470209230bSgjelinek if (err != Z_OK && err != Z_NO_RESOURCE_ID) 62480209230bSgjelinek return (err); 62490209230bSgjelinek 62500209230bSgjelinek if (*pool_importance != '\0') { 62510209230bSgjelinek if ((err = operation_prep(handle)) != Z_OK) 62520209230bSgjelinek return (err); 62530209230bSgjelinek 62540209230bSgjelinek newnode = xmlNewTextChild(cur, NULL, DTD_ELEM_TMPPOOL, NULL); 62550209230bSgjelinek if ((err = newprop(newnode, DTD_ATTR_IMPORTANCE, 62560209230bSgjelinek pool_importance)) != Z_OK) 62570209230bSgjelinek return (err); 62580209230bSgjelinek } 62590209230bSgjelinek 62600209230bSgjelinek return (Z_OK); 62610209230bSgjelinek } 62620209230bSgjelinek 62630209230bSgjelinek static int 62640209230bSgjelinek add_pset_core(zone_dochandle_t handle, struct zone_psettab *tabptr) 62650209230bSgjelinek { 62660209230bSgjelinek xmlNodePtr newnode, cur = handle->zone_dh_cur; 62670209230bSgjelinek int err; 62680209230bSgjelinek 62690209230bSgjelinek newnode = xmlNewTextChild(cur, NULL, DTD_ELEM_PSET, NULL); 62700209230bSgjelinek if ((err = newprop(newnode, DTD_ATTR_NCPU_MIN, 62710209230bSgjelinek tabptr->zone_ncpu_min)) != Z_OK) 62720209230bSgjelinek return (err); 62730209230bSgjelinek if ((err = newprop(newnode, DTD_ATTR_NCPU_MAX, 62740209230bSgjelinek tabptr->zone_ncpu_max)) != Z_OK) 62750209230bSgjelinek return (err); 62760209230bSgjelinek 62770209230bSgjelinek if ((err = modify_tmp_pool(handle, tabptr->zone_importance)) != Z_OK) 62780209230bSgjelinek return (err); 62790209230bSgjelinek 62800209230bSgjelinek return (Z_OK); 62810209230bSgjelinek } 62820209230bSgjelinek 62830209230bSgjelinek int 62840209230bSgjelinek zonecfg_add_pset(zone_dochandle_t handle, struct zone_psettab *tabptr) 62850209230bSgjelinek { 62860209230bSgjelinek int err; 62870209230bSgjelinek 62880209230bSgjelinek if (tabptr == NULL) 62890209230bSgjelinek return (Z_INVAL); 62900209230bSgjelinek 62910209230bSgjelinek if ((err = operation_prep(handle)) != Z_OK) 62920209230bSgjelinek return (err); 62930209230bSgjelinek 62940209230bSgjelinek if ((err = add_pset_core(handle, tabptr)) != Z_OK) 62950209230bSgjelinek return (err); 62960209230bSgjelinek 62970209230bSgjelinek return (Z_OK); 62980209230bSgjelinek } 62990209230bSgjelinek 63000209230bSgjelinek int 63010209230bSgjelinek zonecfg_delete_pset(zone_dochandle_t handle) 63020209230bSgjelinek { 63030209230bSgjelinek int err; 63040209230bSgjelinek int res = Z_NO_RESOURCE_ID; 63050209230bSgjelinek xmlNodePtr cur = handle->zone_dh_cur; 63060209230bSgjelinek 63070209230bSgjelinek if ((err = operation_prep(handle)) != Z_OK) 63080209230bSgjelinek return (err); 63090209230bSgjelinek 63100209230bSgjelinek for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) { 63110209230bSgjelinek if (xmlStrcmp(cur->name, DTD_ELEM_PSET) == 0) { 63120209230bSgjelinek xmlUnlinkNode(cur); 63130209230bSgjelinek xmlFreeNode(cur); 63140209230bSgjelinek res = Z_OK; 63150209230bSgjelinek break; 63160209230bSgjelinek } 63170209230bSgjelinek } 63180209230bSgjelinek 63190209230bSgjelinek /* 63200209230bSgjelinek * Once we have msets, we should check that a mset 63210209230bSgjelinek * do not exist before we delete the tmp_pool data. 63220209230bSgjelinek */ 63230209230bSgjelinek err = delete_tmp_pool(handle); 63240209230bSgjelinek if (err != Z_OK && err != Z_NO_RESOURCE_ID) 63250209230bSgjelinek return (err); 63260209230bSgjelinek 63270209230bSgjelinek return (res); 63280209230bSgjelinek } 63290209230bSgjelinek 63300209230bSgjelinek int 63310209230bSgjelinek zonecfg_modify_pset(zone_dochandle_t handle, struct zone_psettab *tabptr) 63320209230bSgjelinek { 63330209230bSgjelinek int err; 63340209230bSgjelinek 63350209230bSgjelinek if (tabptr == NULL) 63360209230bSgjelinek return (Z_INVAL); 63370209230bSgjelinek 63380209230bSgjelinek if ((err = zonecfg_delete_pset(handle)) != Z_OK) 63390209230bSgjelinek return (err); 63400209230bSgjelinek 63410209230bSgjelinek if ((err = add_pset_core(handle, tabptr)) != Z_OK) 63420209230bSgjelinek return (err); 63430209230bSgjelinek 63440209230bSgjelinek return (Z_OK); 63450209230bSgjelinek } 63460209230bSgjelinek 63470209230bSgjelinek int 63480209230bSgjelinek zonecfg_lookup_pset(zone_dochandle_t handle, struct zone_psettab *tabptr) 63490209230bSgjelinek { 63500209230bSgjelinek xmlNodePtr cur; 63510209230bSgjelinek int err; 63520209230bSgjelinek int res = Z_NO_ENTRY; 63530209230bSgjelinek 63540209230bSgjelinek if (tabptr == NULL) 63550209230bSgjelinek return (Z_INVAL); 63560209230bSgjelinek 63570209230bSgjelinek if ((err = operation_prep(handle)) != Z_OK) 63580209230bSgjelinek return (err); 63590209230bSgjelinek 63600209230bSgjelinek /* this is an optional component */ 63610209230bSgjelinek tabptr->zone_importance[0] = '\0'; 63620209230bSgjelinek 63630209230bSgjelinek cur = handle->zone_dh_cur; 63640209230bSgjelinek for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) { 63650209230bSgjelinek if (xmlStrcmp(cur->name, DTD_ELEM_PSET) == 0) { 63660209230bSgjelinek if ((err = fetchprop(cur, DTD_ATTR_NCPU_MIN, 63670209230bSgjelinek tabptr->zone_ncpu_min, 63680209230bSgjelinek sizeof (tabptr->zone_ncpu_min))) != Z_OK) { 63690209230bSgjelinek handle->zone_dh_cur = handle->zone_dh_top; 63700209230bSgjelinek return (err); 63710209230bSgjelinek } 63720209230bSgjelinek 63730209230bSgjelinek if ((err = fetchprop(cur, DTD_ATTR_NCPU_MAX, 63740209230bSgjelinek tabptr->zone_ncpu_max, 63750209230bSgjelinek sizeof (tabptr->zone_ncpu_max))) != Z_OK) { 63760209230bSgjelinek handle->zone_dh_cur = handle->zone_dh_top; 63770209230bSgjelinek return (err); 63780209230bSgjelinek } 63790209230bSgjelinek 63800209230bSgjelinek res = Z_OK; 63810209230bSgjelinek 63820209230bSgjelinek } else if (xmlStrcmp(cur->name, DTD_ELEM_TMPPOOL) == 0) { 63830209230bSgjelinek if ((err = fetchprop(cur, DTD_ATTR_IMPORTANCE, 63840209230bSgjelinek tabptr->zone_importance, 63850209230bSgjelinek sizeof (tabptr->zone_importance))) != Z_OK) { 63860209230bSgjelinek handle->zone_dh_cur = handle->zone_dh_top; 63870209230bSgjelinek return (err); 63880209230bSgjelinek } 63890209230bSgjelinek } 63900209230bSgjelinek } 63910209230bSgjelinek 63920209230bSgjelinek return (res); 63930209230bSgjelinek } 63940209230bSgjelinek 63950209230bSgjelinek int 63960209230bSgjelinek zonecfg_getpsetent(zone_dochandle_t handle, struct zone_psettab *tabptr) 63970209230bSgjelinek { 63980209230bSgjelinek int err; 63990209230bSgjelinek 64000209230bSgjelinek if ((err = zonecfg_setent(handle)) != Z_OK) 64010209230bSgjelinek return (err); 64020209230bSgjelinek 64030209230bSgjelinek err = zonecfg_lookup_pset(handle, tabptr); 64040209230bSgjelinek 64050209230bSgjelinek (void) zonecfg_endent(handle); 64060209230bSgjelinek 64070209230bSgjelinek return (err); 64080209230bSgjelinek } 64090209230bSgjelinek 64100209230bSgjelinek static int 64110209230bSgjelinek add_mcap(zone_dochandle_t handle, struct zone_mcaptab *tabptr) 64120209230bSgjelinek { 64130209230bSgjelinek xmlNodePtr newnode, cur = handle->zone_dh_cur; 64140209230bSgjelinek int err; 64150209230bSgjelinek 64160209230bSgjelinek newnode = xmlNewTextChild(cur, NULL, DTD_ELEM_MCAP, NULL); 64170209230bSgjelinek if ((err = newprop(newnode, DTD_ATTR_PHYSCAP, tabptr->zone_physmem_cap)) 64180209230bSgjelinek != Z_OK) 64190209230bSgjelinek return (err); 64200209230bSgjelinek 64210209230bSgjelinek return (Z_OK); 64220209230bSgjelinek } 64230209230bSgjelinek 64240209230bSgjelinek int 64250209230bSgjelinek zonecfg_delete_mcap(zone_dochandle_t handle) 64260209230bSgjelinek { 64270209230bSgjelinek int err; 64280209230bSgjelinek xmlNodePtr cur = handle->zone_dh_cur; 64290209230bSgjelinek 64300209230bSgjelinek if ((err = operation_prep(handle)) != Z_OK) 64310209230bSgjelinek return (err); 64320209230bSgjelinek 64330209230bSgjelinek for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) { 64340209230bSgjelinek if (xmlStrcmp(cur->name, DTD_ELEM_MCAP) != 0) 64350209230bSgjelinek continue; 64360209230bSgjelinek 64370209230bSgjelinek xmlUnlinkNode(cur); 64380209230bSgjelinek xmlFreeNode(cur); 64390209230bSgjelinek return (Z_OK); 64400209230bSgjelinek } 64410209230bSgjelinek return (Z_NO_RESOURCE_ID); 64420209230bSgjelinek } 64430209230bSgjelinek 64440209230bSgjelinek int 64450209230bSgjelinek zonecfg_modify_mcap(zone_dochandle_t handle, struct zone_mcaptab *tabptr) 64460209230bSgjelinek { 64470209230bSgjelinek int err; 64480209230bSgjelinek 64490209230bSgjelinek if (tabptr == NULL) 64500209230bSgjelinek return (Z_INVAL); 64510209230bSgjelinek 64520209230bSgjelinek err = zonecfg_delete_mcap(handle); 64530209230bSgjelinek /* it is ok if there is no mcap entry */ 64540209230bSgjelinek if (err != Z_OK && err != Z_NO_RESOURCE_ID) 64550209230bSgjelinek return (err); 64560209230bSgjelinek 64570209230bSgjelinek if ((err = add_mcap(handle, tabptr)) != Z_OK) 64580209230bSgjelinek return (err); 64590209230bSgjelinek 64600209230bSgjelinek return (Z_OK); 64610209230bSgjelinek } 64620209230bSgjelinek 64630209230bSgjelinek int 64640209230bSgjelinek zonecfg_lookup_mcap(zone_dochandle_t handle, struct zone_mcaptab *tabptr) 64650209230bSgjelinek { 64660209230bSgjelinek xmlNodePtr cur; 64670209230bSgjelinek int err; 64680209230bSgjelinek 64690209230bSgjelinek if (tabptr == NULL) 64700209230bSgjelinek return (Z_INVAL); 64710209230bSgjelinek 64720209230bSgjelinek if ((err = operation_prep(handle)) != Z_OK) 64730209230bSgjelinek return (err); 64740209230bSgjelinek 64750209230bSgjelinek cur = handle->zone_dh_cur; 64760209230bSgjelinek for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) { 64770209230bSgjelinek if (xmlStrcmp(cur->name, DTD_ELEM_MCAP) != 0) 64780209230bSgjelinek continue; 64790209230bSgjelinek if ((err = fetchprop(cur, DTD_ATTR_PHYSCAP, 64800209230bSgjelinek tabptr->zone_physmem_cap, 64810209230bSgjelinek sizeof (tabptr->zone_physmem_cap))) != Z_OK) { 64820209230bSgjelinek handle->zone_dh_cur = handle->zone_dh_top; 64830209230bSgjelinek return (err); 64840209230bSgjelinek } 64850209230bSgjelinek 64860209230bSgjelinek return (Z_OK); 64870209230bSgjelinek } 64880209230bSgjelinek 64890209230bSgjelinek return (Z_NO_ENTRY); 64900209230bSgjelinek } 64910209230bSgjelinek 64920209230bSgjelinek static int 64930209230bSgjelinek getmcapent_core(zone_dochandle_t handle, struct zone_mcaptab *tabptr) 64940209230bSgjelinek { 64950209230bSgjelinek xmlNodePtr cur; 64960209230bSgjelinek int err; 64970209230bSgjelinek 64980209230bSgjelinek if (handle == NULL) 64990209230bSgjelinek return (Z_INVAL); 65000209230bSgjelinek 65010209230bSgjelinek if ((cur = handle->zone_dh_cur) == NULL) 65020209230bSgjelinek return (Z_NO_ENTRY); 65030209230bSgjelinek 65040209230bSgjelinek for (; cur != NULL; cur = cur->next) 65050209230bSgjelinek if (xmlStrcmp(cur->name, DTD_ELEM_MCAP) == 0) 65060209230bSgjelinek break; 65070209230bSgjelinek if (cur == NULL) { 65080209230bSgjelinek handle->zone_dh_cur = handle->zone_dh_top; 65090209230bSgjelinek return (Z_NO_ENTRY); 65100209230bSgjelinek } 65110209230bSgjelinek 65120209230bSgjelinek if ((err = fetchprop(cur, DTD_ATTR_PHYSCAP, tabptr->zone_physmem_cap, 65130209230bSgjelinek sizeof (tabptr->zone_physmem_cap))) != Z_OK) { 65140209230bSgjelinek handle->zone_dh_cur = handle->zone_dh_top; 65150209230bSgjelinek return (err); 65160209230bSgjelinek } 65170209230bSgjelinek 65180209230bSgjelinek handle->zone_dh_cur = cur->next; 65190209230bSgjelinek return (Z_OK); 65200209230bSgjelinek } 65210209230bSgjelinek 65220209230bSgjelinek int 65230209230bSgjelinek zonecfg_getmcapent(zone_dochandle_t handle, struct zone_mcaptab *tabptr) 65240209230bSgjelinek { 65250209230bSgjelinek int err; 65260209230bSgjelinek 65270209230bSgjelinek if ((err = zonecfg_setent(handle)) != Z_OK) 65280209230bSgjelinek return (err); 65290209230bSgjelinek 65300209230bSgjelinek err = getmcapent_core(handle, tabptr); 65310209230bSgjelinek 65320209230bSgjelinek (void) zonecfg_endent(handle); 65330209230bSgjelinek 65340209230bSgjelinek return (err); 65350209230bSgjelinek } 65360209230bSgjelinek 65376cfd72c6Sgjelinek /* 65386cfd72c6Sgjelinek * Get the full tree of pkg/patch metadata in a set of nested AVL trees. 65396cfd72c6Sgjelinek * pkgs_avl is an AVL tree of pkgs. Each pkg element contains a 65406cfd72c6Sgjelinek * zpe_patches_avl member which holds an AVL tree of patches for that pkg. 65416cfd72c6Sgjelinek * The patch elements have the same zpe_patches_avl member, each of which can 65426cfd72c6Sgjelinek * hold an AVL tree of patches that are obsoleted by the patch. 65436cfd72c6Sgjelinek * 65446cfd72c6Sgjelinek * The zone xml data contains DTD_ELEM_PACKAGE elements, followed by 65456cfd72c6Sgjelinek * DTD_ELEM_PATCH elements. The DTD_ELEM_PATCH patch element applies to the 65466cfd72c6Sgjelinek * DTD_ELEM_PACKAGE that precedes it. The DTD_ELEM_PATCH element may have 65476cfd72c6Sgjelinek * child DTD_ELEM_OBSOLETES nodes associated with it. The DTD_ELEM_PACKAGE 65486cfd72c6Sgjelinek * really should have had the DTD_ELEM_PATCH elements as children but it 65496cfd72c6Sgjelinek * was not defined that way initially so we are stuck with the DTD definition 65506cfd72c6Sgjelinek * now. However, we can safely assume the ordering for compatibility. 65516cfd72c6Sgjelinek */ 6552ee519a1fSgjelinek int 65536cfd72c6Sgjelinek zonecfg_getpkgdata(zone_dochandle_t handle, uu_avl_pool_t *pkg_pool, 65546cfd72c6Sgjelinek uu_avl_t *pkgs_avl) 6555ee519a1fSgjelinek { 6556ee519a1fSgjelinek xmlNodePtr cur; 65576cfd72c6Sgjelinek int res; 65586cfd72c6Sgjelinek zone_pkg_entry_t *pkg; 65596cfd72c6Sgjelinek char name[MAXNAMELEN]; 65606cfd72c6Sgjelinek char version[ZONE_PKG_VERSMAX]; 6561ee519a1fSgjelinek 6562ee519a1fSgjelinek if (handle == NULL) 6563ee519a1fSgjelinek return (Z_INVAL); 6564ee519a1fSgjelinek 65656cfd72c6Sgjelinek if ((res = zonecfg_setent(handle)) != Z_OK) 65666cfd72c6Sgjelinek return (res); 6567ee519a1fSgjelinek 65686cfd72c6Sgjelinek if ((cur = handle->zone_dh_cur) == NULL) { 65696cfd72c6Sgjelinek res = Z_NO_ENTRY; 65706cfd72c6Sgjelinek goto done; 6571ee519a1fSgjelinek } 6572ee519a1fSgjelinek 65736cfd72c6Sgjelinek for (; cur != NULL; cur = cur->next) { 65746cfd72c6Sgjelinek if (xmlStrcmp(cur->name, DTD_ELEM_PACKAGE) == 0) { 65756cfd72c6Sgjelinek uu_avl_index_t where; 65766cfd72c6Sgjelinek 65776cfd72c6Sgjelinek if ((res = fetchprop(cur, DTD_ATTR_NAME, name, 65786cfd72c6Sgjelinek sizeof (name))) != Z_OK) 65796cfd72c6Sgjelinek goto done; 65806cfd72c6Sgjelinek 65816cfd72c6Sgjelinek if ((res = fetchprop(cur, DTD_ATTR_VERSION, version, 65826cfd72c6Sgjelinek sizeof (version))) != Z_OK) 65836cfd72c6Sgjelinek goto done; 65846cfd72c6Sgjelinek 65856cfd72c6Sgjelinek if ((pkg = (zone_pkg_entry_t *) 65866cfd72c6Sgjelinek malloc(sizeof (zone_pkg_entry_t))) == NULL) { 65876cfd72c6Sgjelinek res = Z_NOMEM; 65886cfd72c6Sgjelinek goto done; 6589ee519a1fSgjelinek } 6590ee519a1fSgjelinek 65916cfd72c6Sgjelinek if ((pkg->zpe_name = strdup(name)) == NULL) { 65926cfd72c6Sgjelinek free(pkg); 65936cfd72c6Sgjelinek res = Z_NOMEM; 65946cfd72c6Sgjelinek goto done; 6595ee519a1fSgjelinek } 6596ee519a1fSgjelinek 65976cfd72c6Sgjelinek if ((pkg->zpe_vers = strdup(version)) == NULL) { 65986cfd72c6Sgjelinek free(pkg->zpe_name); 65996cfd72c6Sgjelinek free(pkg); 66006cfd72c6Sgjelinek res = Z_NOMEM; 66016cfd72c6Sgjelinek goto done; 6602ee519a1fSgjelinek } 6603ee519a1fSgjelinek 66046cfd72c6Sgjelinek pkg->zpe_patches_avl = NULL; 66056cfd72c6Sgjelinek 66066cfd72c6Sgjelinek uu_avl_node_init(pkg, &pkg->zpe_entry, pkg_pool); 66076cfd72c6Sgjelinek if (uu_avl_find(pkgs_avl, pkg, NULL, &where) != NULL) { 66086cfd72c6Sgjelinek free(pkg->zpe_name); 66096cfd72c6Sgjelinek free(pkg->zpe_vers); 66106cfd72c6Sgjelinek free(pkg); 66116cfd72c6Sgjelinek } else { 66126cfd72c6Sgjelinek uu_avl_insert(pkgs_avl, pkg, where); 6613ee519a1fSgjelinek } 6614ee519a1fSgjelinek 66156cfd72c6Sgjelinek } else if (xmlStrcmp(cur->name, DTD_ELEM_PATCH) == 0) { 66166cfd72c6Sgjelinek zone_pkg_entry_t *patch; 66176cfd72c6Sgjelinek uu_avl_index_t where; 66186cfd72c6Sgjelinek char *p; 66196cfd72c6Sgjelinek char *dashp = NULL; 66206cfd72c6Sgjelinek xmlNodePtr child; 66216cfd72c6Sgjelinek 66226cfd72c6Sgjelinek if ((res = fetchprop(cur, DTD_ATTR_ID, name, 66236cfd72c6Sgjelinek sizeof (name))) != Z_OK) 66246cfd72c6Sgjelinek goto done; 66256cfd72c6Sgjelinek 66266cfd72c6Sgjelinek if ((patch = (zone_pkg_entry_t *) 66276cfd72c6Sgjelinek malloc(sizeof (zone_pkg_entry_t))) == NULL) { 66286cfd72c6Sgjelinek res = Z_NOMEM; 66296cfd72c6Sgjelinek goto done; 6630ee519a1fSgjelinek } 6631ee519a1fSgjelinek 66326cfd72c6Sgjelinek if ((p = strchr(name, '-')) != NULL) { 66336cfd72c6Sgjelinek dashp = p; 66346cfd72c6Sgjelinek *p++ = '\0'; 66356cfd72c6Sgjelinek } else { 66366cfd72c6Sgjelinek p = ""; 6637ee519a1fSgjelinek } 6638ee519a1fSgjelinek 66396cfd72c6Sgjelinek if ((patch->zpe_name = strdup(name)) == NULL) { 66406cfd72c6Sgjelinek free(patch); 66416cfd72c6Sgjelinek res = Z_NOMEM; 66426cfd72c6Sgjelinek goto done; 6643ee519a1fSgjelinek } 6644ee519a1fSgjelinek 66456cfd72c6Sgjelinek if ((patch->zpe_vers = strdup(p)) == NULL) { 66466cfd72c6Sgjelinek free(patch->zpe_name); 66476cfd72c6Sgjelinek free(patch); 66486cfd72c6Sgjelinek res = Z_NOMEM; 66496cfd72c6Sgjelinek goto done; 6650ee519a1fSgjelinek } 6651ee519a1fSgjelinek 66526cfd72c6Sgjelinek if (dashp != NULL) 66536cfd72c6Sgjelinek *dashp = '-'; 66546cfd72c6Sgjelinek 66556cfd72c6Sgjelinek patch->zpe_patches_avl = NULL; 66566cfd72c6Sgjelinek 66576cfd72c6Sgjelinek if (pkg->zpe_patches_avl == NULL) { 66586cfd72c6Sgjelinek pkg->zpe_patches_avl = uu_avl_create(pkg_pool, 66596cfd72c6Sgjelinek NULL, UU_DEFAULT); 66606cfd72c6Sgjelinek if (pkg->zpe_patches_avl == NULL) { 66616cfd72c6Sgjelinek free(patch->zpe_name); 66626cfd72c6Sgjelinek free(patch->zpe_vers); 66636cfd72c6Sgjelinek free(patch); 66646cfd72c6Sgjelinek res = Z_NOMEM; 66656cfd72c6Sgjelinek goto done; 66666cfd72c6Sgjelinek } 66676cfd72c6Sgjelinek } 66686cfd72c6Sgjelinek 66696cfd72c6Sgjelinek uu_avl_node_init(patch, &patch->zpe_entry, pkg_pool); 66706cfd72c6Sgjelinek if (uu_avl_find(pkg->zpe_patches_avl, patch, NULL, 66716cfd72c6Sgjelinek &where) != NULL) { 66726cfd72c6Sgjelinek free(patch->zpe_name); 66736cfd72c6Sgjelinek free(patch->zpe_vers); 66746cfd72c6Sgjelinek free(patch); 66756cfd72c6Sgjelinek } else { 66766cfd72c6Sgjelinek uu_avl_insert(pkg->zpe_patches_avl, patch, 66776cfd72c6Sgjelinek where); 66786cfd72c6Sgjelinek } 66796cfd72c6Sgjelinek 66806cfd72c6Sgjelinek /* Add any patches this patch obsoletes. */ 66816cfd72c6Sgjelinek for (child = cur->xmlChildrenNode; child != NULL; 66826cfd72c6Sgjelinek child = child->next) { 66836cfd72c6Sgjelinek zone_pkg_entry_t *obs; 66846cfd72c6Sgjelinek 66856cfd72c6Sgjelinek if (xmlStrcmp(child->name, DTD_ELEM_OBSOLETES) 66866cfd72c6Sgjelinek != 0) 66876cfd72c6Sgjelinek continue; 66886cfd72c6Sgjelinek 66896cfd72c6Sgjelinek if ((res = fetchprop(child, DTD_ATTR_ID, 66906cfd72c6Sgjelinek name, sizeof (name))) != Z_OK) 66916cfd72c6Sgjelinek goto done; 66926cfd72c6Sgjelinek 66936cfd72c6Sgjelinek if ((obs = (zone_pkg_entry_t *)malloc( 66946cfd72c6Sgjelinek sizeof (zone_pkg_entry_t))) == NULL) { 66956cfd72c6Sgjelinek res = Z_NOMEM; 66966cfd72c6Sgjelinek goto done; 66976cfd72c6Sgjelinek } 66986cfd72c6Sgjelinek 66996cfd72c6Sgjelinek if ((obs->zpe_name = strdup(name)) == NULL) { 67006cfd72c6Sgjelinek free(obs); 67016cfd72c6Sgjelinek res = Z_NOMEM; 67026cfd72c6Sgjelinek goto done; 67036cfd72c6Sgjelinek } 67046cfd72c6Sgjelinek /* 67056cfd72c6Sgjelinek * The version doesn't matter for obsoleted 67066cfd72c6Sgjelinek * patches. 67076cfd72c6Sgjelinek */ 67086cfd72c6Sgjelinek obs->zpe_vers = NULL; 67096cfd72c6Sgjelinek obs->zpe_patches_avl = NULL; 67106cfd72c6Sgjelinek 67116cfd72c6Sgjelinek /* 67126cfd72c6Sgjelinek * If this is the first obsolete patch, add an 67136cfd72c6Sgjelinek * AVL tree to the parent patch element. 67146cfd72c6Sgjelinek */ 67156cfd72c6Sgjelinek if (patch->zpe_patches_avl == NULL) { 67166cfd72c6Sgjelinek patch->zpe_patches_avl = 67176cfd72c6Sgjelinek uu_avl_create(pkg_pool, NULL, 67186cfd72c6Sgjelinek UU_DEFAULT); 67196cfd72c6Sgjelinek if (patch->zpe_patches_avl == NULL) { 67206cfd72c6Sgjelinek free(obs->zpe_name); 67216cfd72c6Sgjelinek free(obs); 67226cfd72c6Sgjelinek res = Z_NOMEM; 67236cfd72c6Sgjelinek goto done; 67246cfd72c6Sgjelinek } 67256cfd72c6Sgjelinek } 67266cfd72c6Sgjelinek 67276cfd72c6Sgjelinek /* Insert obsolete patch into the AVL tree. */ 67286cfd72c6Sgjelinek uu_avl_node_init(obs, &obs->zpe_entry, 67296cfd72c6Sgjelinek pkg_pool); 67306cfd72c6Sgjelinek if (uu_avl_find(patch->zpe_patches_avl, obs, 67316cfd72c6Sgjelinek NULL, &where) != NULL) { 67326cfd72c6Sgjelinek free(obs->zpe_name); 67336cfd72c6Sgjelinek free(obs); 67346cfd72c6Sgjelinek } else { 67356cfd72c6Sgjelinek uu_avl_insert(patch->zpe_patches_avl, 67366cfd72c6Sgjelinek obs, where); 67376cfd72c6Sgjelinek } 67386cfd72c6Sgjelinek } 67396cfd72c6Sgjelinek } 67406cfd72c6Sgjelinek } 67416cfd72c6Sgjelinek 67426cfd72c6Sgjelinek done: 67436cfd72c6Sgjelinek (void) zonecfg_endent(handle); 67446cfd72c6Sgjelinek return (res); 6745ee519a1fSgjelinek } 6746ee519a1fSgjelinek 6747ee519a1fSgjelinek int 6748ee519a1fSgjelinek zonecfg_setdevperment(zone_dochandle_t handle) 6749ee519a1fSgjelinek { 6750ee519a1fSgjelinek return (zonecfg_setent(handle)); 6751ee519a1fSgjelinek } 6752ee519a1fSgjelinek 6753ee519a1fSgjelinek int 6754ee519a1fSgjelinek zonecfg_getdevperment(zone_dochandle_t handle, struct zone_devpermtab *tabptr) 6755ee519a1fSgjelinek { 6756ee519a1fSgjelinek xmlNodePtr cur; 6757ee519a1fSgjelinek int err; 6758ee519a1fSgjelinek char buf[128]; 6759ee519a1fSgjelinek 6760ee519a1fSgjelinek tabptr->zone_devperm_acl = NULL; 6761ee519a1fSgjelinek 6762ee519a1fSgjelinek if (handle == NULL) 6763ee519a1fSgjelinek return (Z_INVAL); 6764ee519a1fSgjelinek 6765ee519a1fSgjelinek if ((cur = handle->zone_dh_cur) == NULL) 6766ee519a1fSgjelinek return (Z_NO_ENTRY); 6767ee519a1fSgjelinek 6768ee519a1fSgjelinek for (; cur != NULL; cur = cur->next) 6769ee519a1fSgjelinek if (!xmlStrcmp(cur->name, DTD_ELEM_DEV_PERM)) 6770ee519a1fSgjelinek break; 6771ee519a1fSgjelinek if (cur == NULL) { 6772ee519a1fSgjelinek handle->zone_dh_cur = handle->zone_dh_top; 6773ee519a1fSgjelinek return (Z_NO_ENTRY); 6774ee519a1fSgjelinek } 6775ee519a1fSgjelinek 6776ee519a1fSgjelinek if ((err = fetchprop(cur, DTD_ATTR_NAME, tabptr->zone_devperm_name, 6777ee519a1fSgjelinek sizeof (tabptr->zone_devperm_name))) != Z_OK) { 6778ee519a1fSgjelinek handle->zone_dh_cur = handle->zone_dh_top; 6779ee519a1fSgjelinek return (err); 6780ee519a1fSgjelinek } 6781ee519a1fSgjelinek 6782ee519a1fSgjelinek if ((err = fetchprop(cur, DTD_ATTR_UID, buf, sizeof (buf))) != Z_OK) { 6783ee519a1fSgjelinek handle->zone_dh_cur = handle->zone_dh_top; 6784ee519a1fSgjelinek return (err); 6785ee519a1fSgjelinek } 6786ee519a1fSgjelinek tabptr->zone_devperm_uid = (uid_t)atol(buf); 6787ee519a1fSgjelinek 6788ee519a1fSgjelinek if ((err = fetchprop(cur, DTD_ATTR_GID, buf, sizeof (buf))) != Z_OK) { 6789ee519a1fSgjelinek handle->zone_dh_cur = handle->zone_dh_top; 6790ee519a1fSgjelinek return (err); 6791ee519a1fSgjelinek } 6792ee519a1fSgjelinek tabptr->zone_devperm_gid = (gid_t)atol(buf); 6793ee519a1fSgjelinek 6794ee519a1fSgjelinek if ((err = fetchprop(cur, DTD_ATTR_MODE, buf, sizeof (buf))) != Z_OK) { 6795ee519a1fSgjelinek handle->zone_dh_cur = handle->zone_dh_top; 6796ee519a1fSgjelinek return (err); 6797ee519a1fSgjelinek } 6798ee519a1fSgjelinek tabptr->zone_devperm_mode = (mode_t)strtol(buf, (char **)NULL, 8); 6799ee519a1fSgjelinek 6800ee519a1fSgjelinek if ((err = fetch_alloc_prop(cur, DTD_ATTR_ACL, 6801ee519a1fSgjelinek &(tabptr->zone_devperm_acl))) != Z_OK) { 6802ee519a1fSgjelinek handle->zone_dh_cur = handle->zone_dh_top; 6803ee519a1fSgjelinek return (err); 6804ee519a1fSgjelinek } 6805ee519a1fSgjelinek 6806ee519a1fSgjelinek handle->zone_dh_cur = cur->next; 6807ee519a1fSgjelinek return (Z_OK); 6808ee519a1fSgjelinek } 6809ee519a1fSgjelinek 6810ee519a1fSgjelinek int 6811ee519a1fSgjelinek zonecfg_enddevperment(zone_dochandle_t handle) 6812ee519a1fSgjelinek { 6813ee519a1fSgjelinek return (zonecfg_endent(handle)); 6814ee519a1fSgjelinek } 6815ee519a1fSgjelinek 6816ee519a1fSgjelinek /* 681739935be5Sgjelinek * Maintain a space separated list of unique pkg names. PATH_MAX is used in 681839935be5Sgjelinek * the pkg code as the maximum size for a pkg name. 681939935be5Sgjelinek */ 682039935be5Sgjelinek static int 682139935be5Sgjelinek add_pkg_to_str(char **str, char *pkg) 682239935be5Sgjelinek { 682339935be5Sgjelinek int len, newlen; 682439935be5Sgjelinek char tstr[PATH_MAX + 3]; 682539935be5Sgjelinek char *tmp; 682639935be5Sgjelinek 682739935be5Sgjelinek len = strlen(pkg); 682839935be5Sgjelinek if (*str == NULL) { 682939935be5Sgjelinek /* space for str + 2 spaces + NULL */ 683039935be5Sgjelinek if ((*str = (char *)malloc(len + 3)) == NULL) 683139935be5Sgjelinek return (Z_NOMEM); 683239935be5Sgjelinek (void) snprintf(*str, len + 3, " %s ", pkg); 683339935be5Sgjelinek return (Z_OK); 683439935be5Sgjelinek } 683539935be5Sgjelinek 683639935be5Sgjelinek (void) snprintf(tstr, sizeof (tstr), " %s ", pkg); 683739935be5Sgjelinek if (strstr(*str, tstr) != NULL) 683839935be5Sgjelinek return (Z_OK); 683939935be5Sgjelinek 684039935be5Sgjelinek /* space for str + 1 space + NULL */ 684139935be5Sgjelinek newlen = strlen(*str) + len + 2; 684239935be5Sgjelinek if ((tmp = (char *)realloc(*str, newlen)) == NULL) 684339935be5Sgjelinek return (Z_NOMEM); 684439935be5Sgjelinek *str = tmp; 684539935be5Sgjelinek (void) strlcat(*str, pkg, newlen); 684639935be5Sgjelinek (void) strlcat(*str, " ", newlen); 684739935be5Sgjelinek return (Z_OK); 684839935be5Sgjelinek } 684939935be5Sgjelinek 685039935be5Sgjelinek /* 6851ee519a1fSgjelinek * Process a list of pkgs from an entry in the contents file, adding each pkg 6852ee519a1fSgjelinek * name to the list of pkgs. 6853ee519a1fSgjelinek * 6854ee519a1fSgjelinek * It is possible for the pkg name to be preceeded by a special character 6855ee519a1fSgjelinek * which indicates some bookkeeping information for pkging. Check if the 6856ee519a1fSgjelinek * first char is not an Alpha char. If so, skip over it. 6857ee519a1fSgjelinek */ 6858ee519a1fSgjelinek static int 685939935be5Sgjelinek add_pkg_list(char *lastp, char ***plist, int *pcnt, char **pkg_warn) 6860ee519a1fSgjelinek { 6861ee519a1fSgjelinek char *p; 6862ee519a1fSgjelinek int pkg_cnt = *pcnt; 6863ee519a1fSgjelinek char **pkgs = *plist; 6864ee519a1fSgjelinek int res = Z_OK; 6865ee519a1fSgjelinek 6866ee519a1fSgjelinek while ((p = strtok_r(NULL, " ", &lastp)) != NULL) { 6867ee519a1fSgjelinek char **tmpp; 6868ee519a1fSgjelinek int i; 6869ee519a1fSgjelinek 6870ee519a1fSgjelinek /* skip over any special pkg bookkeeping char */ 687139935be5Sgjelinek if (!isalpha(*p)) { 6872ee519a1fSgjelinek p++; 687339935be5Sgjelinek if ((res = add_pkg_to_str(pkg_warn, p)) != Z_OK) 687439935be5Sgjelinek break; 687539935be5Sgjelinek } 6876ee519a1fSgjelinek 6877ee519a1fSgjelinek /* Check if the pkg is already in the list */ 6878ee519a1fSgjelinek for (i = 0; i < pkg_cnt; i++) { 6879ee519a1fSgjelinek if (strcmp(p, pkgs[i]) == 0) 6880ee519a1fSgjelinek break; 6881ee519a1fSgjelinek } 6882ee519a1fSgjelinek 6883ee519a1fSgjelinek if (i < pkg_cnt) 6884ee519a1fSgjelinek continue; 6885ee519a1fSgjelinek 6886ee519a1fSgjelinek /* The pkg is not in the list; add it. */ 6887ee519a1fSgjelinek if ((tmpp = (char **)realloc(pkgs, 6888ee519a1fSgjelinek sizeof (char *) * (pkg_cnt + 1))) == NULL) { 6889ee519a1fSgjelinek res = Z_NOMEM; 6890ee519a1fSgjelinek break; 6891ee519a1fSgjelinek } 6892ee519a1fSgjelinek pkgs = tmpp; 6893ee519a1fSgjelinek 6894ee519a1fSgjelinek if ((pkgs[pkg_cnt] = strdup(p)) == NULL) { 6895ee519a1fSgjelinek res = Z_NOMEM; 6896ee519a1fSgjelinek break; 6897ee519a1fSgjelinek } 6898ee519a1fSgjelinek pkg_cnt++; 6899ee519a1fSgjelinek } 6900ee519a1fSgjelinek 6901ee519a1fSgjelinek *plist = pkgs; 6902ee519a1fSgjelinek *pcnt = pkg_cnt; 6903ee519a1fSgjelinek 6904ee519a1fSgjelinek return (res); 6905ee519a1fSgjelinek } 6906ee519a1fSgjelinek 6907ee519a1fSgjelinek /* 690839935be5Sgjelinek * Process an entry from the contents file (type "directory"). If the 690939935be5Sgjelinek * directory path is in the list of ipds and is not under a lofs mount within 691039935be5Sgjelinek * the ipd then add the associated list of pkgs to the pkg list. The input 691139935be5Sgjelinek * parameter "entry" will be broken up by the parser within this function so 691239935be5Sgjelinek * its value will be modified when this function exits. 6913ee519a1fSgjelinek * 6914ee519a1fSgjelinek * The entries we are looking for will look something like: 6915ee519a1fSgjelinek * /usr d none 0755 root sys SUNWctpls SUNWidnl SUNWlibCf .... 6916ee519a1fSgjelinek */ 6917ee519a1fSgjelinek static int 691839935be5Sgjelinek get_path_pkgs(char *entry, char **ipds, char **fss, char ***pkgs, int *pkg_cnt, 691939935be5Sgjelinek char **pkg_warn) 6920ee519a1fSgjelinek { 6921ee519a1fSgjelinek char *f1; 6922ee519a1fSgjelinek char *f2; 6923ee519a1fSgjelinek char *lastp; 6924ee519a1fSgjelinek int i; 692539935be5Sgjelinek char *nlp; 6926ee519a1fSgjelinek 6927ee519a1fSgjelinek if ((f1 = strtok_r(entry, " ", &lastp)) == NULL || 6928ee519a1fSgjelinek (f2 = strtok_r(NULL, " ", &lastp)) == NULL || strcmp(f2, "d") != 0) 6929ee519a1fSgjelinek return (Z_OK); 6930ee519a1fSgjelinek 693139935be5Sgjelinek /* Check if this directory entry is in the list of ipds. */ 693239935be5Sgjelinek for (i = 0; ipds[i] != NULL; i++) { 693339935be5Sgjelinek char wildcard[MAXPATHLEN]; 6934ee519a1fSgjelinek 6935ee519a1fSgjelinek /* 693639935be5Sgjelinek * We want to match on the path and any other directory 693739935be5Sgjelinek * entries under this path. When we use FNM_PATHNAME then 693839935be5Sgjelinek * that means '/' will not be matched by a wildcard (*) so 693939935be5Sgjelinek * we omit FNM_PATHNAME on the call with the wildcard matching. 6940ee519a1fSgjelinek */ 694139935be5Sgjelinek (void) snprintf(wildcard, sizeof (wildcard), "%s/*", ipds[i]); 694239935be5Sgjelinek if (fnmatch(ipds[i], f1, FNM_PATHNAME) == 0 || 694339935be5Sgjelinek fnmatch(wildcard, f1, 0) == 0) { 694439935be5Sgjelinek /* It looks like we do want the pkgs for this path. */ 694539935be5Sgjelinek break; 694639935be5Sgjelinek } 694739935be5Sgjelinek } 694839935be5Sgjelinek 694939935be5Sgjelinek /* This entry did not match any of the ipds. */ 695039935be5Sgjelinek if (ipds[i] == NULL) 695139935be5Sgjelinek return (Z_OK); 695239935be5Sgjelinek 695339935be5Sgjelinek /* 695439935be5Sgjelinek * Check if there is a fs mounted under the ipd. If so, ignore this 695539935be5Sgjelinek * entry. 695639935be5Sgjelinek */ 695739935be5Sgjelinek for (i = 0; fss[i] != NULL; i++) { 695839935be5Sgjelinek char wildcard[MAXPATHLEN]; 695939935be5Sgjelinek 696039935be5Sgjelinek (void) snprintf(wildcard, sizeof (wildcard), "%s/*", fss[i]); 696139935be5Sgjelinek if (fnmatch(fss[i], f1, FNM_PATHNAME) == 0 || 696239935be5Sgjelinek fnmatch(wildcard, f1, 0) == 0) { 696339935be5Sgjelinek /* We should ignore this path. */ 696439935be5Sgjelinek break; 696539935be5Sgjelinek } 696639935be5Sgjelinek } 696739935be5Sgjelinek 696839935be5Sgjelinek /* If not null, then we matched an fs mount point so ignore entry. */ 696939935be5Sgjelinek if (fss[i] != NULL) 697039935be5Sgjelinek return (Z_OK); 697139935be5Sgjelinek 697239935be5Sgjelinek /* 697339935be5Sgjelinek * We do want the pkgs for this entry. First, skip over the next 4 697439935be5Sgjelinek * fields in the entry so that we call add_pkg_list starting with the 697539935be5Sgjelinek * pkg names. 697639935be5Sgjelinek */ 697739935be5Sgjelinek for (i = 0; i < 4 && strtok_r(NULL, " ", &lastp) != NULL; i++) 697839935be5Sgjelinek ; 697939935be5Sgjelinek /* If there are < 4 fields this entry is corrupt, just skip it. */ 698039935be5Sgjelinek if (i < 4) 6981ee519a1fSgjelinek return (Z_OK); 6982ee519a1fSgjelinek 698307b574eeSgjelinek /* strip newline from the line */ 698407b574eeSgjelinek nlp = (lastp + strlen(lastp) - 1); 698507b574eeSgjelinek if (*nlp == '\n') 698607b574eeSgjelinek *nlp = '\0'; 698707b574eeSgjelinek 698839935be5Sgjelinek return (add_pkg_list(lastp, pkgs, pkg_cnt, pkg_warn)); 6989ee519a1fSgjelinek } 6990ee519a1fSgjelinek 6991ee519a1fSgjelinek /* 6992ee519a1fSgjelinek * Read an entry from a pkginfo or contents file. Some of these lines can 6993ee519a1fSgjelinek * either be arbitrarily long or be continued by a backslash at the end of 6994ee519a1fSgjelinek * the line. This function coalesces lines that are longer than the read 6995ee519a1fSgjelinek * buffer, and lines that are continued, into one buffer which is returned. 6996ee519a1fSgjelinek * The caller must free this memory. NULL is returned when we hit EOF or 6997ee519a1fSgjelinek * if we run out of memory (errno is set to ENOMEM). 6998ee519a1fSgjelinek */ 6999ee519a1fSgjelinek static char * 7000ee519a1fSgjelinek read_pkg_data(FILE *fp) 7001ee519a1fSgjelinek { 7002ee519a1fSgjelinek char *start; 7003ee519a1fSgjelinek char *inp; 7004ee519a1fSgjelinek char *p; 7005ee519a1fSgjelinek int char_cnt = 0; 7006ee519a1fSgjelinek 7007ee519a1fSgjelinek errno = 0; 7008ee519a1fSgjelinek if ((start = (char *)malloc(PKGINFO_RD_LEN)) == NULL) { 7009ee519a1fSgjelinek errno = ENOMEM; 7010ee519a1fSgjelinek return (NULL); 7011ee519a1fSgjelinek } 7012ee519a1fSgjelinek 7013ee519a1fSgjelinek inp = start; 7014ee519a1fSgjelinek while ((p = fgets(inp, PKGINFO_RD_LEN, fp)) != NULL) { 7015ee519a1fSgjelinek int len; 7016ee519a1fSgjelinek 7017ee519a1fSgjelinek len = strlen(inp); 7018ee519a1fSgjelinek if (inp[len - 1] == '\n' && 7019ee519a1fSgjelinek (len == 1 || inp[len - 2] != '\\')) { 7020ee519a1fSgjelinek char_cnt = len; 7021ee519a1fSgjelinek break; 7022ee519a1fSgjelinek } 7023ee519a1fSgjelinek 7024ee519a1fSgjelinek if (inp[len - 2] == '\\') 7025ee519a1fSgjelinek char_cnt += len - 2; 7026ee519a1fSgjelinek else 7027ee519a1fSgjelinek char_cnt += PKGINFO_RD_LEN - 1; 7028ee519a1fSgjelinek 7029ee519a1fSgjelinek if ((p = realloc(start, char_cnt + PKGINFO_RD_LEN)) == NULL) { 7030ee519a1fSgjelinek errno = ENOMEM; 7031ee519a1fSgjelinek break; 7032ee519a1fSgjelinek } 7033ee519a1fSgjelinek 7034ee519a1fSgjelinek start = p; 7035ee519a1fSgjelinek inp = start + char_cnt; 7036ee519a1fSgjelinek } 7037ee519a1fSgjelinek 7038ee519a1fSgjelinek if (errno == ENOMEM || (p == NULL && char_cnt == 0)) { 7039ee519a1fSgjelinek free(start); 7040ee519a1fSgjelinek start = NULL; 7041ee519a1fSgjelinek } 7042ee519a1fSgjelinek 7043ee519a1fSgjelinek return (start); 7044ee519a1fSgjelinek } 7045ee519a1fSgjelinek 7046ee519a1fSgjelinek static void 7047ee519a1fSgjelinek free_ipd_pkgs(char **pkgs, int cnt) 7048ee519a1fSgjelinek { 7049ee519a1fSgjelinek int i; 7050ee519a1fSgjelinek 7051ee519a1fSgjelinek for (i = 0; i < cnt; i++) 7052ee519a1fSgjelinek free(pkgs[i]); 7053ee519a1fSgjelinek free(pkgs); 7054ee519a1fSgjelinek } 7055ee519a1fSgjelinek 7056ee519a1fSgjelinek /* 705739935be5Sgjelinek * Get a list of the inherited pkg dirs or fs entries configured for the 705839935be5Sgjelinek * zone. The type parameter will be either ZONE_IPD or ZONE_FS. 705939935be5Sgjelinek */ 706039935be5Sgjelinek static int 706139935be5Sgjelinek get_ipd_fs_list(zone_dochandle_t handle, enum zn_ipd_fs type, char ***list) 706239935be5Sgjelinek { 706339935be5Sgjelinek int res; 706439935be5Sgjelinek struct zone_fstab fstab; 706539935be5Sgjelinek int cnt = 0; 706639935be5Sgjelinek char **entries = NULL; 706739935be5Sgjelinek int i; 706839935be5Sgjelinek int (*fp)(zone_dochandle_t, struct zone_fstab *); 706939935be5Sgjelinek 707039935be5Sgjelinek if (type == ZONE_IPD) { 707139935be5Sgjelinek fp = zonecfg_getipdent; 707239935be5Sgjelinek res = zonecfg_setipdent(handle); 707339935be5Sgjelinek } else { 707439935be5Sgjelinek fp = zonecfg_getfsent; 707539935be5Sgjelinek res = zonecfg_setfsent(handle); 707639935be5Sgjelinek } 707739935be5Sgjelinek 707839935be5Sgjelinek if (res != Z_OK) 707939935be5Sgjelinek return (res); 708039935be5Sgjelinek 708139935be5Sgjelinek while (fp(handle, &fstab) == Z_OK) { 708239935be5Sgjelinek char **p; 708339935be5Sgjelinek 708439935be5Sgjelinek if ((p = (char **)realloc(entries, 708539935be5Sgjelinek sizeof (char *) * (cnt + 1))) == NULL) { 708639935be5Sgjelinek res = Z_NOMEM; 708739935be5Sgjelinek break; 708839935be5Sgjelinek } 708939935be5Sgjelinek entries = p; 709039935be5Sgjelinek 709139935be5Sgjelinek if ((entries[cnt] = strdup(fstab.zone_fs_dir)) == NULL) { 709239935be5Sgjelinek res = Z_NOMEM; 709339935be5Sgjelinek break; 709439935be5Sgjelinek } 709539935be5Sgjelinek 709639935be5Sgjelinek cnt++; 709739935be5Sgjelinek } 709839935be5Sgjelinek 709939935be5Sgjelinek if (type == ZONE_IPD) 710039935be5Sgjelinek (void) zonecfg_endipdent(handle); 710139935be5Sgjelinek else 710239935be5Sgjelinek (void) zonecfg_endfsent(handle); 710339935be5Sgjelinek 710439935be5Sgjelinek /* Add a NULL terminating element. */ 710539935be5Sgjelinek if (res == Z_OK) { 710639935be5Sgjelinek char **p; 710739935be5Sgjelinek 710839935be5Sgjelinek if ((p = (char **)realloc(entries, 710939935be5Sgjelinek sizeof (char *) * (cnt + 1))) == NULL) { 711039935be5Sgjelinek res = Z_NOMEM; 711139935be5Sgjelinek } else { 711239935be5Sgjelinek entries = p; 711339935be5Sgjelinek entries[cnt] = NULL; 711439935be5Sgjelinek } 711539935be5Sgjelinek } 711639935be5Sgjelinek 711739935be5Sgjelinek if (res != Z_OK) { 711839935be5Sgjelinek if (entries != NULL) { 711939935be5Sgjelinek for (i = 0; i < cnt; i++) 712039935be5Sgjelinek free(entries[i]); 712139935be5Sgjelinek free(entries); 712239935be5Sgjelinek } 712339935be5Sgjelinek return (res); 712439935be5Sgjelinek } 712539935be5Sgjelinek 712639935be5Sgjelinek *list = entries; 712739935be5Sgjelinek return (Z_OK); 712839935be5Sgjelinek } 712939935be5Sgjelinek 713039935be5Sgjelinek /* 7131ee519a1fSgjelinek * Get the list of inherited-pkg-dirs (ipd) for the zone and then get the 7132ee519a1fSgjelinek * list of pkgs that deliver into those dirs. 7133ee519a1fSgjelinek */ 7134ee519a1fSgjelinek static int 7135ee519a1fSgjelinek get_ipd_pkgs(zone_dochandle_t handle, char ***pkg_list, int *cnt) 7136ee519a1fSgjelinek { 7137ee519a1fSgjelinek int res; 713839935be5Sgjelinek char **ipds; 713939935be5Sgjelinek char **fss; 7140ee519a1fSgjelinek int pkg_cnt = 0; 7141ee519a1fSgjelinek char **pkgs = NULL; 7142ee519a1fSgjelinek int i; 7143ee519a1fSgjelinek 714439935be5Sgjelinek if ((res = get_ipd_fs_list(handle, ZONE_IPD, &ipds)) != Z_OK) 7145ee519a1fSgjelinek return (res); 7146ee519a1fSgjelinek 714739935be5Sgjelinek if ((res = get_ipd_fs_list(handle, ZONE_FS, &fss)) != Z_OK) { 714839935be5Sgjelinek for (i = 0; ipds[i] != NULL; i++) 7149ee519a1fSgjelinek free(ipds[i]); 7150ee519a1fSgjelinek free(ipds); 7151ee519a1fSgjelinek return (res); 7152ee519a1fSgjelinek } 7153ee519a1fSgjelinek 7154ee519a1fSgjelinek /* We only have to process the contents file if we have ipds. */ 715539935be5Sgjelinek if (ipds != NULL) { 7156ee519a1fSgjelinek FILE *fp; 7157ee519a1fSgjelinek 7158ee519a1fSgjelinek if ((fp = fopen(CONTENTS_FILE, "r")) != NULL) { 7159ee519a1fSgjelinek char *buf; 716039935be5Sgjelinek char *pkg_warn = NULL; 7161ee519a1fSgjelinek 7162ee519a1fSgjelinek while ((buf = read_pkg_data(fp)) != NULL) { 716339935be5Sgjelinek res = get_path_pkgs(buf, ipds, fss, &pkgs, 716439935be5Sgjelinek &pkg_cnt, &pkg_warn); 7165ee519a1fSgjelinek free(buf); 7166ee519a1fSgjelinek if (res != Z_OK) 7167ee519a1fSgjelinek break; 7168ee519a1fSgjelinek } 7169ee519a1fSgjelinek 7170ee519a1fSgjelinek (void) fclose(fp); 717139935be5Sgjelinek 717239935be5Sgjelinek if (pkg_warn != NULL) { 717339935be5Sgjelinek (void) fprintf(stderr, dgettext(TEXT_DOMAIN, 717439935be5Sgjelinek "WARNING: package operation in progress " 717539935be5Sgjelinek "on the following packages:\n %s\n"), 717639935be5Sgjelinek pkg_warn); 717739935be5Sgjelinek free(pkg_warn); 717839935be5Sgjelinek } 7179ee519a1fSgjelinek } 7180ee519a1fSgjelinek } 7181ee519a1fSgjelinek 718239935be5Sgjelinek for (i = 0; ipds[i] != NULL; i++) 7183ee519a1fSgjelinek free(ipds[i]); 7184ee519a1fSgjelinek free(ipds); 7185ee519a1fSgjelinek 718639935be5Sgjelinek for (i = 0; fss[i] != NULL; i++) 718739935be5Sgjelinek free(fss[i]); 718839935be5Sgjelinek free(fss); 718939935be5Sgjelinek 7190ee519a1fSgjelinek if (res != Z_OK) { 7191ee519a1fSgjelinek free_ipd_pkgs(pkgs, pkg_cnt); 7192ee519a1fSgjelinek } else { 7193ee519a1fSgjelinek *pkg_list = pkgs; 7194ee519a1fSgjelinek *cnt = pkg_cnt; 7195ee519a1fSgjelinek } 7196ee519a1fSgjelinek 7197ee519a1fSgjelinek return (res); 7198ee519a1fSgjelinek } 7199ee519a1fSgjelinek 7200ee519a1fSgjelinek /* 7201ee519a1fSgjelinek * Return true if pkg_name is in the list of pkgs that deliver into an 7202ee519a1fSgjelinek * inherited pkg directory for the zone. 7203ee519a1fSgjelinek */ 7204ee519a1fSgjelinek static boolean_t 7205ee519a1fSgjelinek dir_pkg(char *pkg_name, char **pkg_list, int cnt) 7206ee519a1fSgjelinek { 7207ee519a1fSgjelinek int i; 7208ee519a1fSgjelinek 7209ee519a1fSgjelinek for (i = 0; i < cnt; i++) { 7210ee519a1fSgjelinek if (strcmp(pkg_name, pkg_list[i]) == 0) 7211ee519a1fSgjelinek return (B_TRUE); 7212ee519a1fSgjelinek } 7213ee519a1fSgjelinek 7214ee519a1fSgjelinek return (B_FALSE); 7215ee519a1fSgjelinek } 7216ee519a1fSgjelinek 7217ee519a1fSgjelinek /* 72186cfd72c6Sgjelinek * Keep track of obsoleted patches for this specific patch. We don't need to 72196cfd72c6Sgjelinek * keep track of the patch version since once a patch is obsoleted, all prior 72206cfd72c6Sgjelinek * versions are also obsolete and there won't be any new versions. 72216cfd72c6Sgjelinek */ 72226cfd72c6Sgjelinek static int 72236cfd72c6Sgjelinek add_obs_patch(patch_node_t *patch, char *num, uu_list_pool_t *patches_pool) 72246cfd72c6Sgjelinek { 72256cfd72c6Sgjelinek obs_patch_node_t *obs; 72266cfd72c6Sgjelinek 72276cfd72c6Sgjelinek if (patch->obs_patches == NULL) { 72286cfd72c6Sgjelinek if ((patch->obs_patches = uu_list_create(patches_pool, NULL, 72296cfd72c6Sgjelinek 0)) == NULL) 72306cfd72c6Sgjelinek return (Z_NOMEM); 72316cfd72c6Sgjelinek } 72326cfd72c6Sgjelinek 72336cfd72c6Sgjelinek if ((obs = (obs_patch_node_t *)malloc(sizeof (obs_patch_node_t))) 72346cfd72c6Sgjelinek == NULL) 72356cfd72c6Sgjelinek return (Z_NOMEM); 72366cfd72c6Sgjelinek 72376cfd72c6Sgjelinek if ((obs->patch_num = strdup(num)) == NULL) { 72386cfd72c6Sgjelinek free(obs); 72396cfd72c6Sgjelinek return (Z_NOMEM); 72406cfd72c6Sgjelinek } 72416cfd72c6Sgjelinek 72426cfd72c6Sgjelinek uu_list_node_init(obs, &obs->link, patches_pool); 72436cfd72c6Sgjelinek (void) uu_list_insert_before(patch->obs_patches, NULL, obs); 72446cfd72c6Sgjelinek 72456cfd72c6Sgjelinek return (Z_OK); 72466cfd72c6Sgjelinek } 72476cfd72c6Sgjelinek 72486cfd72c6Sgjelinek /* 724939935be5Sgjelinek * Keep track of obsoleted patches. We don't need to keep track of the patch 725039935be5Sgjelinek * version since once a patch is obsoleted, all prior versions are also 725139935be5Sgjelinek * obsolete and there won't be any new versions. 7252ee519a1fSgjelinek */ 7253ee519a1fSgjelinek static int 72546cfd72c6Sgjelinek save_obs_patch(char *num, uu_avl_pool_t *patches_pool, uu_avl_t *obs_patches) 7255ee519a1fSgjelinek { 725639935be5Sgjelinek patch_node_t *patch; 725739935be5Sgjelinek uu_avl_index_t where; 725839935be5Sgjelinek 725939935be5Sgjelinek if ((patch = (patch_node_t *)malloc(sizeof (patch_node_t))) == NULL) 726039935be5Sgjelinek return (Z_NOMEM); 726139935be5Sgjelinek 726239935be5Sgjelinek if ((patch->patch_num = strdup(num)) == NULL) { 726339935be5Sgjelinek free(patch); 726439935be5Sgjelinek return (Z_NOMEM); 726539935be5Sgjelinek } 726639935be5Sgjelinek 726739935be5Sgjelinek patch->patch_vers = NULL; 72686cfd72c6Sgjelinek patch->obs_patches = NULL; 72696cfd72c6Sgjelinek 727039935be5Sgjelinek uu_avl_node_init(patch, &patch->patch_node, patches_pool); 727139935be5Sgjelinek 72726cfd72c6Sgjelinek if (uu_avl_find(obs_patches, patch, NULL, &where) != NULL) { 727339935be5Sgjelinek free(patch->patch_num); 727439935be5Sgjelinek free(patch); 727539935be5Sgjelinek return (Z_OK); 727639935be5Sgjelinek } 727739935be5Sgjelinek 72786cfd72c6Sgjelinek uu_avl_insert(obs_patches, patch, where); 727939935be5Sgjelinek return (Z_OK); 728039935be5Sgjelinek } 728139935be5Sgjelinek 728239935be5Sgjelinek /* 728339935be5Sgjelinek * Keep a list of patches for a pkg. If we see a newer version of a patch, 728439935be5Sgjelinek * we only keep track of the newer version. 728539935be5Sgjelinek */ 7286*e66285fdSgjelinek static boolean_t 728739935be5Sgjelinek save_patch(patch_node_t *patch, uu_avl_t *patches_avl) 728839935be5Sgjelinek { 728939935be5Sgjelinek patch_node_t *existing; 729039935be5Sgjelinek uu_avl_index_t where; 729139935be5Sgjelinek 729209a8462dSgjelinek /* 729309a8462dSgjelinek * Check if this is a newer version of a patch we already have. 729409a8462dSgjelinek * If it is an older version of a patch we already have, ignore it. 729509a8462dSgjelinek */ 729639935be5Sgjelinek if ((existing = (patch_node_t *)uu_avl_find(patches_avl, patch, NULL, 729739935be5Sgjelinek &where)) != NULL) { 729839935be5Sgjelinek char *endptr; 729939935be5Sgjelinek ulong_t pvers, evers; 730039935be5Sgjelinek 730139935be5Sgjelinek pvers = strtoul(patch->patch_vers, &endptr, 10); 730239935be5Sgjelinek evers = strtoul(existing->patch_vers, &endptr, 10); 730339935be5Sgjelinek 7304*e66285fdSgjelinek if (pvers <= evers) 7305*e66285fdSgjelinek return (B_FALSE); 7306*e66285fdSgjelinek 7307*e66285fdSgjelinek /* 7308*e66285fdSgjelinek * Remove the lower version patch from the tree so we can 7309*e66285fdSgjelinek * insert the new higher version one. We also discard the 7310*e66285fdSgjelinek * obsolete patch list from the old version since the new 7311*e66285fdSgjelinek * version will have its own, likely different, list. 7312*e66285fdSgjelinek */ 7313*e66285fdSgjelinek uu_avl_remove(patches_avl, existing); 7314*e66285fdSgjelinek free(existing->patch_num); 731539935be5Sgjelinek free(existing->patch_vers); 7316*e66285fdSgjelinek if (existing->obs_patches != NULL) { 7317*e66285fdSgjelinek obs_patch_node_t *op; 7318*e66285fdSgjelinek void *cookie2 = NULL; 7319*e66285fdSgjelinek 7320*e66285fdSgjelinek while ((op = uu_list_teardown(existing->obs_patches, 7321*e66285fdSgjelinek &cookie2)) != NULL) { 7322*e66285fdSgjelinek free(op->patch_num); 7323*e66285fdSgjelinek free(op); 732409a8462dSgjelinek } 7325*e66285fdSgjelinek uu_list_destroy(existing->obs_patches); 7326*e66285fdSgjelinek } 7327*e66285fdSgjelinek free(existing); 7328*e66285fdSgjelinek 7329*e66285fdSgjelinek /* 7330*e66285fdSgjelinek * Now that the old one is gone, find the new location 7331*e66285fdSgjelinek * in the tree. 7332*e66285fdSgjelinek */ 7333*e66285fdSgjelinek (void) uu_avl_find(patches_avl, patch, NULL, &where); 733439935be5Sgjelinek } 733539935be5Sgjelinek 733639935be5Sgjelinek uu_avl_insert(patches_avl, patch, where); 7337*e66285fdSgjelinek return (B_TRUE); 733839935be5Sgjelinek } 733939935be5Sgjelinek 734039935be5Sgjelinek /* 734139935be5Sgjelinek * Check if a patch is on the list of obsoleted patches. We don't need to 734239935be5Sgjelinek * check the patch version since once a patch is obsoleted, all prior versions 734339935be5Sgjelinek * are also obsolete and there won't be any new versions. 734439935be5Sgjelinek */ 734539935be5Sgjelinek static boolean_t 73466cfd72c6Sgjelinek obsolete_patch(patch_node_t *patch, uu_avl_t *obs_patches) 734739935be5Sgjelinek { 734839935be5Sgjelinek uu_avl_index_t where; 734939935be5Sgjelinek 73506cfd72c6Sgjelinek if (uu_avl_find(obs_patches, patch, NULL, &where) != NULL) 735139935be5Sgjelinek return (B_TRUE); 735239935be5Sgjelinek 735339935be5Sgjelinek return (B_FALSE); 735439935be5Sgjelinek } 735539935be5Sgjelinek 735639935be5Sgjelinek /* ARGSUSED */ 735739935be5Sgjelinek static int 735839935be5Sgjelinek patch_node_compare(const void *l_arg, const void *r_arg, void *private) 735939935be5Sgjelinek { 736039935be5Sgjelinek patch_node_t *l = (patch_node_t *)l_arg; 736139935be5Sgjelinek patch_node_t *r = (patch_node_t *)r_arg; 736239935be5Sgjelinek char *endptr; 736339935be5Sgjelinek ulong_t lnum, rnum; 736439935be5Sgjelinek 736539935be5Sgjelinek lnum = strtoul(l->patch_num, &endptr, 10); 736639935be5Sgjelinek rnum = strtoul(r->patch_num, &endptr, 10); 736739935be5Sgjelinek 736839935be5Sgjelinek if (lnum > rnum) 736939935be5Sgjelinek return (1); 737039935be5Sgjelinek if (lnum < rnum) 737139935be5Sgjelinek return (-1); 737239935be5Sgjelinek return (0); 737339935be5Sgjelinek } 737439935be5Sgjelinek 737539935be5Sgjelinek /* 737639935be5Sgjelinek * Parse the patchinfo string for the patch. 737739935be5Sgjelinek * 737839935be5Sgjelinek * We are parsing entries of the form: 737939935be5Sgjelinek * PATCH_INFO_121454-02=Installed: Wed Dec 7 07:13:51 PST 2005 From: mum \ 738039935be5Sgjelinek * Obsoletes: 120777-03 121087-02 119108-07 Requires: 119575-02 \ 738139935be5Sgjelinek * 119255-06 Incompatibles: 738239935be5Sgjelinek * 738339935be5Sgjelinek * A backed out patch will have "backed out\n" as the status. We should 73846cfd72c6Sgjelinek * skip these patches. We also ignore any entries that seem to be 73856cfd72c6Sgjelinek * corrupted. Obsolete patches are saved in the obs_patches parameter 73866cfd72c6Sgjelinek * AVL list. 738739935be5Sgjelinek */ 738839935be5Sgjelinek static int 738939935be5Sgjelinek parse_info(char *patchinfo, uu_avl_pool_t *patches_pool, uu_avl_t *patches_avl, 73906cfd72c6Sgjelinek uu_avl_t *obs_patches, uu_list_pool_t *list_pool) 739139935be5Sgjelinek { 7392ee519a1fSgjelinek char *p; 7393ee519a1fSgjelinek char *lastp; 739439935be5Sgjelinek char *ep; 739539935be5Sgjelinek char *pvers; 7396ee519a1fSgjelinek boolean_t add_info = B_FALSE; 739739935be5Sgjelinek patch_node_t *patch; 7398ee519a1fSgjelinek 739939935be5Sgjelinek if (strlen(patchinfo) < (sizeof (PATCHINFO) - 1)) 740007b574eeSgjelinek return (Z_OK); 740107b574eeSgjelinek 740239935be5Sgjelinek /* Skip over "PATCH_INFO_" to get the patch id. */ 740339935be5Sgjelinek p = patchinfo + sizeof (PATCHINFO) - 1; 740439935be5Sgjelinek if ((ep = strchr(p, '=')) == NULL) 740539935be5Sgjelinek return (Z_OK); 7406ee519a1fSgjelinek 740739935be5Sgjelinek *ep++ = '\0'; 740839935be5Sgjelinek 740939935be5Sgjelinek /* Ignore all but installed patches. */ 741039935be5Sgjelinek if (strncmp(ep, "Installed:", 10) != 0) 741139935be5Sgjelinek return (Z_OK); 741239935be5Sgjelinek 741339935be5Sgjelinek /* remove newline */ 741439935be5Sgjelinek lastp = (ep + strlen(ep) - 1); 741539935be5Sgjelinek if (*lastp == '\n') 741639935be5Sgjelinek *lastp = '\0'; 741739935be5Sgjelinek 741839935be5Sgjelinek if ((patch = (patch_node_t *)malloc(sizeof (patch_node_t))) == NULL) 741939935be5Sgjelinek return (Z_NOMEM); 742039935be5Sgjelinek 742139935be5Sgjelinek if ((pvers = strchr(p, '-')) != NULL) 742239935be5Sgjelinek *pvers++ = '\0'; 742339935be5Sgjelinek else 742439935be5Sgjelinek pvers = ""; 742539935be5Sgjelinek 742639935be5Sgjelinek if ((patch->patch_num = strdup(p)) == NULL) { 742739935be5Sgjelinek free(patch); 742839935be5Sgjelinek return (Z_NOMEM); 742939935be5Sgjelinek } 743039935be5Sgjelinek if ((patch->patch_vers = strdup(pvers)) == NULL) { 743139935be5Sgjelinek free(patch->patch_num); 743239935be5Sgjelinek free(patch); 743339935be5Sgjelinek return (Z_NOMEM); 743439935be5Sgjelinek } 74356cfd72c6Sgjelinek patch->obs_patches = NULL; 743639935be5Sgjelinek 743739935be5Sgjelinek uu_avl_node_init(patch, &patch->patch_node, patches_pool); 7438*e66285fdSgjelinek if (!save_patch(patch, patches_avl)) { 7439*e66285fdSgjelinek free(patch->patch_num); 7440*e66285fdSgjelinek free(patch->patch_vers); 7441*e66285fdSgjelinek assert(patch->obs_patches == NULL); 7442*e66285fdSgjelinek free(patch); 7443*e66285fdSgjelinek return (Z_OK); 7444*e66285fdSgjelinek } 7445ee519a1fSgjelinek 7446ee519a1fSgjelinek /* 7447ee519a1fSgjelinek * Start with the first token. This will probably be "Installed:". 7448ee519a1fSgjelinek * If we can't tokenize this entry, just return. 7449ee519a1fSgjelinek */ 745039935be5Sgjelinek if ((p = strtok_r(ep, " ", &lastp)) == NULL) 7451ee519a1fSgjelinek return (Z_OK); 7452ee519a1fSgjelinek 7453ee519a1fSgjelinek do { 7454ee519a1fSgjelinek if (strcmp(p, "Installed:") == 0 || 7455ee519a1fSgjelinek strcmp(p, "Requires:") == 0 || 745639935be5Sgjelinek strcmp(p, "From:") == 0 || 745739935be5Sgjelinek strcmp(p, "Incompatibles:") == 0) { 7458ee519a1fSgjelinek add_info = B_FALSE; 7459ee519a1fSgjelinek continue; 7460ee519a1fSgjelinek } else if (strcmp(p, "Obsoletes:") == 0) { 7461ee519a1fSgjelinek add_info = B_TRUE; 7462ee519a1fSgjelinek continue; 7463ee519a1fSgjelinek } 7464ee519a1fSgjelinek 7465ee519a1fSgjelinek if (!add_info) 7466ee519a1fSgjelinek continue; 7467ee519a1fSgjelinek 746839935be5Sgjelinek if ((pvers = strchr(p, '-')) != NULL) 746939935be5Sgjelinek *pvers = '\0'; 7470ee519a1fSgjelinek 74716cfd72c6Sgjelinek /* 74726cfd72c6Sgjelinek * We save all of the obsolete patches in one big list in the 74736cfd72c6Sgjelinek * obs_patches AVL tree so that we know not to output those as 74746cfd72c6Sgjelinek * part of the sw dependencies. However, we also need to save 74756cfd72c6Sgjelinek * the obsolete patch information for this sepcific patch so 74766cfd72c6Sgjelinek * so that we can do the cross manifest patch checking 74776cfd72c6Sgjelinek * correctly. 74786cfd72c6Sgjelinek */ 74796cfd72c6Sgjelinek if (save_obs_patch(p, patches_pool, obs_patches) != Z_OK) 74806cfd72c6Sgjelinek return (Z_NOMEM); 74816cfd72c6Sgjelinek if (add_obs_patch(patch, p, list_pool) != Z_OK) 748239935be5Sgjelinek return (Z_NOMEM); 7483ee519a1fSgjelinek } while ((p = strtok_r(NULL, " ", &lastp)) != NULL); 7484ee519a1fSgjelinek 7485ee519a1fSgjelinek return (Z_OK); 7486ee519a1fSgjelinek } 7487ee519a1fSgjelinek 748839935be5Sgjelinek /* 748939935be5Sgjelinek * AVL walker callback used to add patch to XML manifest. 749039935be5Sgjelinek * 749139935be5Sgjelinek * PATH_MAX is used in the pkg/patch code as the maximum size for the patch 749239935be5Sgjelinek * number/version string. 749339935be5Sgjelinek */ 749439935be5Sgjelinek static int 749539935be5Sgjelinek add_patch(void *e, void *p) 7496ee519a1fSgjelinek { 749739935be5Sgjelinek xmlNodePtr node; 7498ee519a1fSgjelinek xmlNodePtr cur; 749939935be5Sgjelinek char id[PATH_MAX]; 750039935be5Sgjelinek patch_node_t *patch; 750139935be5Sgjelinek patch_parms_t *args; 7502ee519a1fSgjelinek 750339935be5Sgjelinek patch = e; 750439935be5Sgjelinek args = p; 7505ee519a1fSgjelinek 750639935be5Sgjelinek /* skip this patch if it has been obsoleted */ 750739935be5Sgjelinek if (obsolete_patch(patch, args->obs_patches_avl)) 750839935be5Sgjelinek return (UU_WALK_NEXT); 750939935be5Sgjelinek 751039935be5Sgjelinek if (patch->patch_vers[0] == '\0') 751139935be5Sgjelinek (void) snprintf(id, sizeof (id), "%s", patch->patch_num); 751239935be5Sgjelinek else 751339935be5Sgjelinek (void) snprintf(id, sizeof (id), "%s-%s", patch->patch_num, 751439935be5Sgjelinek patch->patch_vers); 751539935be5Sgjelinek 751639935be5Sgjelinek if ((args->res = operation_prep(args->handle)) != Z_OK) 751739935be5Sgjelinek return (UU_WALK_DONE); 751839935be5Sgjelinek 751939935be5Sgjelinek cur = args->handle->zone_dh_cur; 752039935be5Sgjelinek node = xmlNewTextChild(cur, NULL, DTD_ELEM_PATCH, NULL); 752139935be5Sgjelinek if ((args->res = newprop(node, DTD_ATTR_ID, id)) != Z_OK) 752239935be5Sgjelinek return (UU_WALK_DONE); 752339935be5Sgjelinek 75246cfd72c6Sgjelinek if (patch->obs_patches != NULL) { 75256cfd72c6Sgjelinek obs_patch_node_t *op; 75266cfd72c6Sgjelinek xmlNodePtr node2; 75276cfd72c6Sgjelinek 75286cfd72c6Sgjelinek for (op = uu_list_first(patch->obs_patches); op != NULL; 75296cfd72c6Sgjelinek op = uu_list_next(patch->obs_patches, op)) { 75306cfd72c6Sgjelinek (void) snprintf(id, sizeof (id), "%s", op->patch_num); 75316cfd72c6Sgjelinek node2 = xmlNewTextChild(node, NULL, DTD_ELEM_OBSOLETES, 75326cfd72c6Sgjelinek NULL); 75336cfd72c6Sgjelinek if ((args->res = newprop(node2, DTD_ATTR_ID, id)) 75346cfd72c6Sgjelinek != Z_OK) 75356cfd72c6Sgjelinek return (UU_WALK_DONE); 75366cfd72c6Sgjelinek } 75376cfd72c6Sgjelinek } 75386cfd72c6Sgjelinek 753939935be5Sgjelinek return (UU_WALK_NEXT); 7540ee519a1fSgjelinek } 7541ee519a1fSgjelinek 754239935be5Sgjelinek static void 754339935be5Sgjelinek patch_avl_delete(uu_avl_t *patches_avl) 754439935be5Sgjelinek { 754539935be5Sgjelinek if (patches_avl != NULL) { 754639935be5Sgjelinek patch_node_t *p; 754739935be5Sgjelinek void *cookie = NULL; 754839935be5Sgjelinek 754939935be5Sgjelinek while ((p = (patch_node_t *)uu_avl_teardown(patches_avl, 755039935be5Sgjelinek &cookie)) != NULL) { 755139935be5Sgjelinek free(p->patch_num); 755239935be5Sgjelinek free(p->patch_vers); 75536cfd72c6Sgjelinek 75546cfd72c6Sgjelinek if (p->obs_patches != NULL) { 75556cfd72c6Sgjelinek obs_patch_node_t *op; 75566cfd72c6Sgjelinek void *cookie2 = NULL; 75576cfd72c6Sgjelinek 75586cfd72c6Sgjelinek while ((op = uu_list_teardown(p->obs_patches, 75596cfd72c6Sgjelinek &cookie2)) != NULL) { 75606cfd72c6Sgjelinek free(op->patch_num); 75616cfd72c6Sgjelinek free(op); 75626cfd72c6Sgjelinek } 75636cfd72c6Sgjelinek uu_list_destroy(p->obs_patches); 75646cfd72c6Sgjelinek } 75656cfd72c6Sgjelinek 756639935be5Sgjelinek free(p); 756739935be5Sgjelinek } 756839935be5Sgjelinek 756939935be5Sgjelinek uu_avl_destroy(patches_avl); 757039935be5Sgjelinek } 7571ee519a1fSgjelinek } 7572ee519a1fSgjelinek 7573ee519a1fSgjelinek /* 757439935be5Sgjelinek * Add the unique, highest version patches that are associated with this pkg 757539935be5Sgjelinek * to the sw inventory on the handle. 7576ee519a1fSgjelinek */ 7577ee519a1fSgjelinek static int 757839935be5Sgjelinek add_patches(zone_dochandle_t handle, struct zone_pkginfo *infop, 75796cfd72c6Sgjelinek uu_avl_pool_t *patches_pool, uu_avl_t *obs_patches, 75806cfd72c6Sgjelinek uu_list_pool_t *list_pool) 7581ee519a1fSgjelinek { 7582ee519a1fSgjelinek int i; 758339935be5Sgjelinek int res; 758439935be5Sgjelinek uu_avl_t *patches_avl; 758539935be5Sgjelinek patch_parms_t args; 758639935be5Sgjelinek 758739935be5Sgjelinek if ((patches_avl = uu_avl_create(patches_pool, NULL, UU_DEFAULT)) 758839935be5Sgjelinek == NULL) 758939935be5Sgjelinek return (Z_NOMEM); 7590ee519a1fSgjelinek 7591ee519a1fSgjelinek for (i = 0; i < infop->zpi_patch_cnt; i++) { 759239935be5Sgjelinek if ((res = parse_info(infop->zpi_patchinfo[i], patches_pool, 75936cfd72c6Sgjelinek patches_avl, obs_patches, list_pool)) != Z_OK) { 759439935be5Sgjelinek patch_avl_delete(patches_avl); 759539935be5Sgjelinek return (res); 759639935be5Sgjelinek } 7597ee519a1fSgjelinek } 7598ee519a1fSgjelinek 75996cfd72c6Sgjelinek args.obs_patches_avl = obs_patches; 760039935be5Sgjelinek args.handle = handle; 760139935be5Sgjelinek args.res = Z_OK; 760239935be5Sgjelinek 760339935be5Sgjelinek (void) uu_avl_walk(patches_avl, add_patch, &args, 0); 760439935be5Sgjelinek 760539935be5Sgjelinek patch_avl_delete(patches_avl); 760639935be5Sgjelinek return (args.res); 7607ee519a1fSgjelinek } 7608ee519a1fSgjelinek 7609ee519a1fSgjelinek /* 76106cfd72c6Sgjelinek * Keep track of the pkgs we have already processed so that we can quickly 76116cfd72c6Sgjelinek * skip those pkgs while recursively doing dependents. 76126cfd72c6Sgjelinek */ 76136cfd72c6Sgjelinek static boolean_t 76146cfd72c6Sgjelinek pkg_in_manifest(uu_avl_t *saw_pkgs, char *pname, uu_avl_pool_t *pkgs_pool) 76156cfd72c6Sgjelinek { 76166cfd72c6Sgjelinek uu_avl_index_t where; 76176cfd72c6Sgjelinek 76186cfd72c6Sgjelinek if (uu_avl_find(saw_pkgs, pname, NULL, &where) == NULL) { 76196cfd72c6Sgjelinek zone_pkg_entry_t *pkg; 76206cfd72c6Sgjelinek 76216cfd72c6Sgjelinek /* 76226cfd72c6Sgjelinek * We need to add it. If we don't have memory we just skip 76236cfd72c6Sgjelinek * this pkg since this routine improves performance but the 76246cfd72c6Sgjelinek * algorithm is still correct without it. 76256cfd72c6Sgjelinek */ 76266cfd72c6Sgjelinek if ((pkg = (zone_pkg_entry_t *) 76276cfd72c6Sgjelinek malloc(sizeof (zone_pkg_entry_t))) == NULL) 76286cfd72c6Sgjelinek return (B_FALSE); 76296cfd72c6Sgjelinek 76306cfd72c6Sgjelinek if ((pkg->zpe_name = strdup(pname)) == NULL) { 76316cfd72c6Sgjelinek free(pkg); 76326cfd72c6Sgjelinek return (B_FALSE); 76336cfd72c6Sgjelinek } 76346cfd72c6Sgjelinek 76356cfd72c6Sgjelinek pkg->zpe_vers = NULL; 76366cfd72c6Sgjelinek pkg->zpe_patches_avl = NULL; 76376cfd72c6Sgjelinek 76386cfd72c6Sgjelinek /* Insert pkg into the AVL tree. */ 76396cfd72c6Sgjelinek uu_avl_node_init(pkg, &pkg->zpe_entry, pkgs_pool); 76406cfd72c6Sgjelinek uu_avl_insert(saw_pkgs, pkg, where); 76416cfd72c6Sgjelinek return (B_FALSE); 76426cfd72c6Sgjelinek } 76436cfd72c6Sgjelinek 76446cfd72c6Sgjelinek return (B_TRUE); 76456cfd72c6Sgjelinek } 76466cfd72c6Sgjelinek 76476cfd72c6Sgjelinek /* 7648ee519a1fSgjelinek * Add the pkg to the sw inventory on the handle. 7649ee519a1fSgjelinek */ 7650ee519a1fSgjelinek static int 7651ee519a1fSgjelinek add_pkg(zone_dochandle_t handle, char *name, char *version) 7652ee519a1fSgjelinek { 7653ee519a1fSgjelinek xmlNodePtr newnode; 7654ee519a1fSgjelinek xmlNodePtr cur; 7655ee519a1fSgjelinek int err; 7656ee519a1fSgjelinek 7657ee519a1fSgjelinek if ((err = operation_prep(handle)) != Z_OK) 7658ee519a1fSgjelinek return (err); 7659ee519a1fSgjelinek 7660ee519a1fSgjelinek cur = handle->zone_dh_cur; 7661ee519a1fSgjelinek newnode = xmlNewTextChild(cur, NULL, DTD_ELEM_PACKAGE, NULL); 7662ee519a1fSgjelinek if ((err = newprop(newnode, DTD_ATTR_NAME, name)) != Z_OK) 7663ee519a1fSgjelinek return (err); 7664ee519a1fSgjelinek if ((err = newprop(newnode, DTD_ATTR_VERSION, version)) != Z_OK) 7665ee519a1fSgjelinek return (err); 7666ee519a1fSgjelinek return (Z_OK); 7667ee519a1fSgjelinek } 7668ee519a1fSgjelinek 7669ee519a1fSgjelinek static void 7670ee519a1fSgjelinek free_pkginfo(struct zone_pkginfo *infop) 7671ee519a1fSgjelinek { 7672ee519a1fSgjelinek free(infop->zpi_version); 7673ee519a1fSgjelinek if (infop->zpi_patch_cnt > 0) { 7674ee519a1fSgjelinek int i; 7675ee519a1fSgjelinek 7676ee519a1fSgjelinek for (i = 0; i < infop->zpi_patch_cnt; i++) 7677ee519a1fSgjelinek free(infop->zpi_patchinfo[i]); 7678ee519a1fSgjelinek free(infop->zpi_patchinfo); 7679ee519a1fSgjelinek } 7680ee519a1fSgjelinek } 7681ee519a1fSgjelinek 7682ee519a1fSgjelinek /* 7683ee519a1fSgjelinek * Read the pkginfo file and populate the structure with the data we need 7684ee519a1fSgjelinek * from this pkg for a sw inventory. 7685ee519a1fSgjelinek */ 7686ee519a1fSgjelinek static int 7687ee519a1fSgjelinek get_pkginfo(char *pkginfo, struct zone_pkginfo *infop) 7688ee519a1fSgjelinek { 7689ee519a1fSgjelinek FILE *fp; 7690ee519a1fSgjelinek char *buf; 7691ee519a1fSgjelinek int err = 0; 7692ee519a1fSgjelinek 7693ee519a1fSgjelinek infop->zpi_all_zones = B_FALSE; 7694ee519a1fSgjelinek infop->zpi_this_zone = B_FALSE; 7695ee519a1fSgjelinek infop->zpi_version = NULL; 7696ee519a1fSgjelinek infop->zpi_patch_cnt = 0; 7697ee519a1fSgjelinek infop->zpi_patchinfo = NULL; 7698ee519a1fSgjelinek 7699ee519a1fSgjelinek if ((fp = fopen(pkginfo, "r")) == NULL) 7700ee519a1fSgjelinek return (errno); 7701ee519a1fSgjelinek 7702ee519a1fSgjelinek while ((buf = read_pkg_data(fp)) != NULL) { 7703ee519a1fSgjelinek if (strncmp(buf, VERSION, sizeof (VERSION) - 1) == 0) { 7704ee519a1fSgjelinek int len; 7705ee519a1fSgjelinek 7706ee519a1fSgjelinek if ((infop->zpi_version = 7707ee519a1fSgjelinek strdup(buf + sizeof (VERSION) - 1)) == NULL) { 7708ee519a1fSgjelinek err = ENOMEM; 7709ee519a1fSgjelinek break; 7710ee519a1fSgjelinek } 7711ee519a1fSgjelinek 7712ee519a1fSgjelinek /* remove trailing newline */ 7713ee519a1fSgjelinek len = strlen(infop->zpi_version); 7714ee519a1fSgjelinek *(infop->zpi_version + len - 1) = 0; 7715ee519a1fSgjelinek 771645916cd2Sjpk } else if (strcmp(buf, SUNW_PKG_ALL_ZONES) == 0) { 7717ee519a1fSgjelinek infop->zpi_all_zones = B_TRUE; 7718ee519a1fSgjelinek 771945916cd2Sjpk } else if (strcmp(buf, SUNW_PKG_THIS_ZONE) == 0) { 7720ee519a1fSgjelinek infop->zpi_this_zone = B_TRUE; 7721ee519a1fSgjelinek 7722ee519a1fSgjelinek } else if (strncmp(buf, PATCHINFO, sizeof (PATCHINFO) - 1) 7723ee519a1fSgjelinek == 0) { 7724ee519a1fSgjelinek char **p; 7725ee519a1fSgjelinek 7726ee519a1fSgjelinek if ((p = (char **)realloc(infop->zpi_patchinfo, 7727ee519a1fSgjelinek sizeof (char *) * (infop->zpi_patch_cnt + 1))) 7728ee519a1fSgjelinek == NULL) { 7729ee519a1fSgjelinek err = ENOMEM; 7730ee519a1fSgjelinek break; 7731ee519a1fSgjelinek } 7732ee519a1fSgjelinek infop->zpi_patchinfo = p; 7733ee519a1fSgjelinek 7734ee519a1fSgjelinek if ((infop->zpi_patchinfo[infop->zpi_patch_cnt] = 7735ee519a1fSgjelinek strdup(buf)) == NULL) { 7736ee519a1fSgjelinek err = ENOMEM; 7737ee519a1fSgjelinek break; 7738ee519a1fSgjelinek } 7739ee519a1fSgjelinek infop->zpi_patch_cnt++; 7740ee519a1fSgjelinek } 7741ee519a1fSgjelinek 7742ee519a1fSgjelinek free(buf); 7743ee519a1fSgjelinek } 7744ee519a1fSgjelinek 7745ee519a1fSgjelinek free(buf); 7746ee519a1fSgjelinek 7747ee519a1fSgjelinek if (errno == ENOMEM) { 7748ee519a1fSgjelinek err = ENOMEM; 7749ee519a1fSgjelinek /* Clean up anything we did manage to allocate. */ 7750ee519a1fSgjelinek free_pkginfo(infop); 7751ee519a1fSgjelinek } 7752ee519a1fSgjelinek 7753ee519a1fSgjelinek (void) fclose(fp); 7754ee519a1fSgjelinek 7755ee519a1fSgjelinek return (err); 7756ee519a1fSgjelinek } 7757ee519a1fSgjelinek 7758ee519a1fSgjelinek /* 77596cfd72c6Sgjelinek * Add any dependent pkgs to the list. The pkg depend file lists pkg 77606cfd72c6Sgjelinek * dependencies, one per line with an entry that looks like: 77616cfd72c6Sgjelinek * P SUNWcar Core Architecture, (Root) 77626cfd72c6Sgjelinek * See the depend(4) man page. 77636cfd72c6Sgjelinek */ 77646cfd72c6Sgjelinek static int 77656cfd72c6Sgjelinek add_dependents(zone_dochandle_t handle, char *pname, 77666cfd72c6Sgjelinek uu_avl_pool_t *patches_pool, uu_avl_t *obs_patches, 77676cfd72c6Sgjelinek uu_list_pool_t *list_pool, uu_avl_t *saw_pkgs, uu_avl_pool_t *pkgs_pool) 77686cfd72c6Sgjelinek { 77696cfd72c6Sgjelinek int res = Z_OK; 77706cfd72c6Sgjelinek FILE *fp; 77716cfd72c6Sgjelinek char depend[MAXPATHLEN]; 77726cfd72c6Sgjelinek char *buf; 77736cfd72c6Sgjelinek struct stat sbuf; 77746cfd72c6Sgjelinek 77756cfd72c6Sgjelinek (void) snprintf(depend, sizeof (depend), "%s/%s/install/depend", 77766cfd72c6Sgjelinek PKG_PATH, pname); 77776cfd72c6Sgjelinek 77786cfd72c6Sgjelinek if (stat(depend, &sbuf) == -1 || !S_ISREG(sbuf.st_mode)) 77796cfd72c6Sgjelinek return (Z_OK); 77806cfd72c6Sgjelinek 77816cfd72c6Sgjelinek if ((fp = fopen(depend, "r")) == NULL) 77826cfd72c6Sgjelinek return (Z_OK); 77836cfd72c6Sgjelinek 77846cfd72c6Sgjelinek while ((buf = read_pkg_data(fp)) != NULL) { 77856cfd72c6Sgjelinek char *deppkg; 77866cfd72c6Sgjelinek char *delims = " \t"; 77876cfd72c6Sgjelinek char pkginfo[MAXPATHLEN]; 77886cfd72c6Sgjelinek struct zone_pkginfo info; 77896cfd72c6Sgjelinek 77906cfd72c6Sgjelinek if (*buf != 'P') { 77916cfd72c6Sgjelinek free(buf); 77926cfd72c6Sgjelinek continue; 77936cfd72c6Sgjelinek } 77946cfd72c6Sgjelinek 77956cfd72c6Sgjelinek /* Skip past the leading 'P '. */ 77966cfd72c6Sgjelinek if ((deppkg = strtok(buf + 2, delims)) == NULL) { 77976cfd72c6Sgjelinek free(buf); 77986cfd72c6Sgjelinek continue; 77996cfd72c6Sgjelinek } 78006cfd72c6Sgjelinek 78016cfd72c6Sgjelinek /* If the pkg is already in the manifest don't add it again. */ 78026cfd72c6Sgjelinek if (pkg_in_manifest(saw_pkgs, deppkg, pkgs_pool)) { 78036cfd72c6Sgjelinek free(buf); 78046cfd72c6Sgjelinek continue; 78056cfd72c6Sgjelinek } 78066cfd72c6Sgjelinek 78076cfd72c6Sgjelinek (void) snprintf(pkginfo, sizeof (pkginfo), "%s/%s/pkginfo", 78086cfd72c6Sgjelinek PKG_PATH, deppkg); 78096cfd72c6Sgjelinek 78106cfd72c6Sgjelinek if (stat(pkginfo, &sbuf) == -1 || !S_ISREG(sbuf.st_mode)) { 78116cfd72c6Sgjelinek free(buf); 78126cfd72c6Sgjelinek continue; 78136cfd72c6Sgjelinek } 78146cfd72c6Sgjelinek 78156cfd72c6Sgjelinek if (get_pkginfo(pkginfo, &info) != 0) { 78166cfd72c6Sgjelinek res = Z_NOMEM; 78176cfd72c6Sgjelinek free(buf); 78186cfd72c6Sgjelinek break; 78196cfd72c6Sgjelinek } 78206cfd72c6Sgjelinek 78216cfd72c6Sgjelinek if ((res = add_dependents(handle, deppkg, patches_pool, 78226cfd72c6Sgjelinek obs_patches, list_pool, saw_pkgs, pkgs_pool)) == Z_OK && 78236cfd72c6Sgjelinek (res = add_pkg(handle, deppkg, info.zpi_version)) == Z_OK) { 78246cfd72c6Sgjelinek if (info.zpi_patch_cnt > 0) 78256cfd72c6Sgjelinek res = add_patches(handle, &info, patches_pool, 78266cfd72c6Sgjelinek obs_patches, list_pool); 78276cfd72c6Sgjelinek } 78286cfd72c6Sgjelinek 78296cfd72c6Sgjelinek free(buf); 78306cfd72c6Sgjelinek free_pkginfo(&info); 78316cfd72c6Sgjelinek 78326cfd72c6Sgjelinek if (res != Z_OK) 78336cfd72c6Sgjelinek break; 78346cfd72c6Sgjelinek } 78356cfd72c6Sgjelinek 78366cfd72c6Sgjelinek (void) fclose(fp); 78376cfd72c6Sgjelinek return (res); 78386cfd72c6Sgjelinek } 78396cfd72c6Sgjelinek 78406cfd72c6Sgjelinek /* ARGSUSED */ 78416cfd72c6Sgjelinek static int 78426cfd72c6Sgjelinek pkg_entry_compare(const void *l_arg, const void *r_arg, void *private) 78436cfd72c6Sgjelinek { 78446cfd72c6Sgjelinek zone_pkg_entry_t *pkg = (zone_pkg_entry_t *)l_arg; 78456cfd72c6Sgjelinek char *name = (char *)r_arg; 78466cfd72c6Sgjelinek 78476cfd72c6Sgjelinek return (strcmp(pkg->zpe_name, name)); 78486cfd72c6Sgjelinek } 78496cfd72c6Sgjelinek 78506cfd72c6Sgjelinek static void 78516cfd72c6Sgjelinek pkg_avl_delete(uu_avl_t *pavl) 78526cfd72c6Sgjelinek { 78536cfd72c6Sgjelinek if (pavl != NULL) { 78546cfd72c6Sgjelinek zone_pkg_entry_t *p; 78556cfd72c6Sgjelinek void *cookie = NULL; 78566cfd72c6Sgjelinek 78576cfd72c6Sgjelinek while ((p = uu_avl_teardown(pavl, &cookie)) != NULL) { 78586cfd72c6Sgjelinek free(p->zpe_name); 78596cfd72c6Sgjelinek free(p); 78606cfd72c6Sgjelinek } 78616cfd72c6Sgjelinek 78626cfd72c6Sgjelinek uu_avl_destroy(pavl); 78636cfd72c6Sgjelinek } 78646cfd72c6Sgjelinek } 78656cfd72c6Sgjelinek 78666cfd72c6Sgjelinek /* 7867ee519a1fSgjelinek * Take a software inventory of the global zone. We need to get the set of 7868ee519a1fSgjelinek * packages and patches that are on the global zone that the specified 7869ee519a1fSgjelinek * non-global zone depends on. The packages we need in the inventory are: 7870ee519a1fSgjelinek * 7871ee519a1fSgjelinek * - skip the package if SUNW_PKG_THISZONE is 'true' 7872ee519a1fSgjelinek * otherwise, 7873ee519a1fSgjelinek * - add the package if 7874ee519a1fSgjelinek * a) SUNW_PKG_ALLZONES is 'true', 7875ee519a1fSgjelinek * or 7876ee519a1fSgjelinek * b) any file delivered by the package is in a file system that is inherited 7877ee519a1fSgjelinek * from the global zone. 7878ee519a1fSgjelinek * If the zone does not inherit any file systems (whole root) 7879ee519a1fSgjelinek * then (b) will be skipped. 7880ee519a1fSgjelinek * 7881ee519a1fSgjelinek * For each of the packages that is being added to the inventory, we will also 78826cfd72c6Sgjelinek * add its dependent packages to the inventory. 78836cfd72c6Sgjelinek * 78846cfd72c6Sgjelinek * For each of the packages that is being added to the inventory, we will also 7885ee519a1fSgjelinek * add all of the associated, unique patches to the inventory. 78866cfd72c6Sgjelinek * 78876cfd72c6Sgjelinek * See the comment for zonecfg_getpkgdata() for compatability restrictions on 78886cfd72c6Sgjelinek * how we must save the XML representation of the software inventory. 7889ee519a1fSgjelinek */ 7890ee519a1fSgjelinek static int 7891ee519a1fSgjelinek zonecfg_sw_inventory(zone_dochandle_t handle) 7892ee519a1fSgjelinek { 7893ee519a1fSgjelinek char pkginfo[MAXPATHLEN]; 7894ee519a1fSgjelinek int res; 7895ee519a1fSgjelinek struct dirent *dp; 7896ee519a1fSgjelinek DIR *dirp; 7897ee519a1fSgjelinek struct stat buf; 7898ee519a1fSgjelinek struct zone_pkginfo info; 7899ee519a1fSgjelinek int pkg_cnt = 0; 7900ee519a1fSgjelinek char **pkgs = NULL; 79016cfd72c6Sgjelinek uu_avl_pool_t *pkgs_pool = NULL; 79026cfd72c6Sgjelinek uu_avl_pool_t *patches_pool = NULL; 79036cfd72c6Sgjelinek uu_list_pool_t *list_pool = NULL; 79046cfd72c6Sgjelinek uu_avl_t *saw_pkgs = NULL; 79056cfd72c6Sgjelinek uu_avl_t *obs_patches = NULL; 79066cfd72c6Sgjelinek 79076cfd72c6Sgjelinek if ((pkgs_pool = uu_avl_pool_create("pkgs_pool", 79086cfd72c6Sgjelinek sizeof (zone_pkg_entry_t), offsetof(zone_pkg_entry_t, zpe_entry), 79096cfd72c6Sgjelinek pkg_entry_compare, UU_DEFAULT)) == NULL) { 79106cfd72c6Sgjelinek res = Z_NOMEM; 79116cfd72c6Sgjelinek goto done; 79126cfd72c6Sgjelinek } 79136cfd72c6Sgjelinek 79146cfd72c6Sgjelinek if ((saw_pkgs = uu_avl_create(pkgs_pool, NULL, UU_DEFAULT)) == NULL) { 79156cfd72c6Sgjelinek res = Z_NOMEM; 79166cfd72c6Sgjelinek goto done; 79176cfd72c6Sgjelinek } 7918ee519a1fSgjelinek 791939935be5Sgjelinek if ((patches_pool = uu_avl_pool_create("patches_pool", 792039935be5Sgjelinek sizeof (patch_node_t), offsetof(patch_node_t, patch_node), 792139935be5Sgjelinek patch_node_compare, UU_DEFAULT)) == NULL) { 79226cfd72c6Sgjelinek res = Z_NOMEM; 79236cfd72c6Sgjelinek goto done; 792439935be5Sgjelinek } 792539935be5Sgjelinek 79266cfd72c6Sgjelinek if ((list_pool = uu_list_pool_create("list_pool", 79276cfd72c6Sgjelinek sizeof (obs_patch_node_t), offsetof(obs_patch_node_t, link), NULL, 79286cfd72c6Sgjelinek UU_DEFAULT)) == NULL) { 79296cfd72c6Sgjelinek res = Z_NOMEM; 79306cfd72c6Sgjelinek goto done; 79316cfd72c6Sgjelinek } 79326cfd72c6Sgjelinek 79336cfd72c6Sgjelinek /* 79346cfd72c6Sgjelinek * The obs_patches AVL tree saves all of the obsolete patches so 79356cfd72c6Sgjelinek * that we know not to output those as part of the sw dependencies. 79366cfd72c6Sgjelinek */ 79376cfd72c6Sgjelinek if ((obs_patches = uu_avl_create(patches_pool, NULL, UU_DEFAULT)) 793839935be5Sgjelinek == NULL) { 79396cfd72c6Sgjelinek res = Z_NOMEM; 79406cfd72c6Sgjelinek goto done; 794139935be5Sgjelinek } 794239935be5Sgjelinek 794339935be5Sgjelinek if ((res = get_ipd_pkgs(handle, &pkgs, &pkg_cnt)) != Z_OK) { 79446cfd72c6Sgjelinek res = Z_NOMEM; 79456cfd72c6Sgjelinek goto done; 794639935be5Sgjelinek } 7947ee519a1fSgjelinek 7948ee519a1fSgjelinek if ((dirp = opendir(PKG_PATH)) == NULL) { 79496cfd72c6Sgjelinek res = Z_NOMEM; 79506cfd72c6Sgjelinek goto done; 7951ee519a1fSgjelinek } 7952ee519a1fSgjelinek 7953ee519a1fSgjelinek while ((dp = readdir(dirp)) != (struct dirent *)0) { 7954ee519a1fSgjelinek if (strcmp(dp->d_name, ".") == 0 || 7955ee519a1fSgjelinek strcmp(dp->d_name, "..") == 0) 7956ee519a1fSgjelinek continue; 7957ee519a1fSgjelinek 7958ee519a1fSgjelinek (void) snprintf(pkginfo, sizeof (pkginfo), "%s/%s/pkginfo", 7959ee519a1fSgjelinek PKG_PATH, dp->d_name); 7960ee519a1fSgjelinek 7961ee519a1fSgjelinek if (stat(pkginfo, &buf) == -1 || !S_ISREG(buf.st_mode)) 7962ee519a1fSgjelinek continue; 7963ee519a1fSgjelinek 7964ee519a1fSgjelinek if (get_pkginfo(pkginfo, &info) != 0) { 7965ee519a1fSgjelinek res = Z_NOMEM; 7966ee519a1fSgjelinek break; 7967ee519a1fSgjelinek } 7968ee519a1fSgjelinek 7969ee519a1fSgjelinek if (!info.zpi_this_zone && 7970ee519a1fSgjelinek (info.zpi_all_zones || 79716cfd72c6Sgjelinek dir_pkg(dp->d_name, pkgs, pkg_cnt)) && 79726cfd72c6Sgjelinek !pkg_in_manifest(saw_pkgs, dp->d_name, pkgs_pool)) { 79736cfd72c6Sgjelinek /* 79746cfd72c6Sgjelinek * Add dependents first so any patches will get 79756cfd72c6Sgjelinek * associated with the right pkg in the xml file. 79766cfd72c6Sgjelinek */ 79776cfd72c6Sgjelinek if ((res = add_dependents(handle, dp->d_name, 79786cfd72c6Sgjelinek patches_pool, obs_patches, list_pool, saw_pkgs, 79796cfd72c6Sgjelinek pkgs_pool)) == Z_OK && 79806cfd72c6Sgjelinek (res = add_pkg(handle, dp->d_name, 7981ee519a1fSgjelinek info.zpi_version)) == Z_OK) { 7982ee519a1fSgjelinek if (info.zpi_patch_cnt > 0) 798339935be5Sgjelinek res = add_patches(handle, &info, 79846cfd72c6Sgjelinek patches_pool, obs_patches, 79856cfd72c6Sgjelinek list_pool); 7986ee519a1fSgjelinek } 7987ee519a1fSgjelinek } 7988ee519a1fSgjelinek 7989ee519a1fSgjelinek free_pkginfo(&info); 7990ee519a1fSgjelinek 7991ee519a1fSgjelinek if (res != Z_OK) 7992ee519a1fSgjelinek break; 7993ee519a1fSgjelinek } 7994ee519a1fSgjelinek 7995ee519a1fSgjelinek (void) closedir(dirp); 7996ee519a1fSgjelinek 79976cfd72c6Sgjelinek done: 79986cfd72c6Sgjelinek pkg_avl_delete(saw_pkgs); 79996cfd72c6Sgjelinek patch_avl_delete(obs_patches); 80006cfd72c6Sgjelinek if (pkgs_pool != NULL) 80016cfd72c6Sgjelinek uu_avl_pool_destroy(pkgs_pool); 80026cfd72c6Sgjelinek if (patches_pool != NULL) 800339935be5Sgjelinek uu_avl_pool_destroy(patches_pool); 80046cfd72c6Sgjelinek if (list_pool != NULL) 80056cfd72c6Sgjelinek uu_list_pool_destroy(list_pool); 8006ee519a1fSgjelinek free_ipd_pkgs(pkgs, pkg_cnt); 8007ee519a1fSgjelinek 8008ee519a1fSgjelinek if (res == Z_OK) 8009ee519a1fSgjelinek handle->zone_dh_sw_inv = B_TRUE; 8010ee519a1fSgjelinek 8011ee519a1fSgjelinek return (res); 8012ee519a1fSgjelinek } 8013ee519a1fSgjelinek 8014ee519a1fSgjelinek /* 8015ee519a1fSgjelinek * zonecfg_devwalk call-back function used during detach to generate the 8016ee519a1fSgjelinek * dev info in the manifest. 8017ee519a1fSgjelinek */ 8018ee519a1fSgjelinek static int 8019ee519a1fSgjelinek get_detach_dev_entry(const char *name, uid_t uid, gid_t gid, mode_t mode, 8020ee519a1fSgjelinek const char *acl, void *hdl) 8021ee519a1fSgjelinek { 8022ee519a1fSgjelinek zone_dochandle_t handle = (zone_dochandle_t)hdl; 8023ee519a1fSgjelinek xmlNodePtr newnode; 8024ee519a1fSgjelinek xmlNodePtr cur; 8025ee519a1fSgjelinek int err; 8026ee519a1fSgjelinek char buf[128]; 8027ee519a1fSgjelinek 8028ee519a1fSgjelinek if ((err = operation_prep(handle)) != Z_OK) 8029ee519a1fSgjelinek return (err); 8030ee519a1fSgjelinek 8031ee519a1fSgjelinek cur = handle->zone_dh_cur; 8032ee519a1fSgjelinek newnode = xmlNewTextChild(cur, NULL, DTD_ELEM_DEV_PERM, NULL); 8033ee519a1fSgjelinek if ((err = newprop(newnode, DTD_ATTR_NAME, (char *)name)) != Z_OK) 8034ee519a1fSgjelinek return (err); 8035ee519a1fSgjelinek (void) snprintf(buf, sizeof (buf), "%lu", uid); 8036ee519a1fSgjelinek if ((err = newprop(newnode, DTD_ATTR_UID, buf)) != Z_OK) 8037ee519a1fSgjelinek return (err); 8038ee519a1fSgjelinek (void) snprintf(buf, sizeof (buf), "%lu", gid); 8039ee519a1fSgjelinek if ((err = newprop(newnode, DTD_ATTR_GID, buf)) != Z_OK) 8040ee519a1fSgjelinek return (err); 8041ee519a1fSgjelinek (void) snprintf(buf, sizeof (buf), "%o", mode); 8042ee519a1fSgjelinek if ((err = newprop(newnode, DTD_ATTR_MODE, buf)) != Z_OK) 8043ee519a1fSgjelinek return (err); 8044ee519a1fSgjelinek if ((err = newprop(newnode, DTD_ATTR_ACL, (char *)acl)) != Z_OK) 8045ee519a1fSgjelinek return (err); 8046ee519a1fSgjelinek return (Z_OK); 8047ee519a1fSgjelinek } 8048ee519a1fSgjelinek 8049ee519a1fSgjelinek /* 8050ee519a1fSgjelinek * Get the information required to support detaching a zone. This is 8051ee519a1fSgjelinek * called on the source system when detaching (the detaching parameter should 8052ee519a1fSgjelinek * be set to true) and on the destination system before attaching (the 8053ee519a1fSgjelinek * detaching parameter should be false). 8054ee519a1fSgjelinek * 8055ee519a1fSgjelinek * For native Solaris zones, the detach/attach process involves validating 8056ee519a1fSgjelinek * that the software on the global zone can support the zone when we attach. 8057ee519a1fSgjelinek * To do this we take a software inventory of the global zone. We also 8058ee519a1fSgjelinek * have to keep track of the device configuration so that we can properly 8059ee519a1fSgjelinek * recreate it on the destination. 8060ee519a1fSgjelinek */ 8061ee519a1fSgjelinek int 8062ee519a1fSgjelinek zonecfg_get_detach_info(zone_dochandle_t handle, boolean_t detaching) 8063ee519a1fSgjelinek { 8064ee519a1fSgjelinek int res; 8065ee519a1fSgjelinek 8066ee519a1fSgjelinek if ((res = zonecfg_sw_inventory(handle)) != Z_OK) 8067ee519a1fSgjelinek return (res); 8068ee519a1fSgjelinek 8069ee519a1fSgjelinek if (detaching) 8070ee519a1fSgjelinek res = zonecfg_devwalk(handle, get_detach_dev_entry, handle); 8071ee519a1fSgjelinek 8072ee519a1fSgjelinek return (res); 8073ee519a1fSgjelinek } 8074