blob: cce47ce753249579267f7791683fa95920d591cf [file] [log] [blame]
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2007 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
#include <libsysevent.h>
#include <pthread.h>
#include <stdlib.h>
#include <errno.h>
#include <fnmatch.h>
#include <strings.h>
#include <unistd.h>
#include <assert.h>
#include <libgen.h>
#include <libintl.h>
#include <alloca.h>
#include <ctype.h>
#include <sys/acl.h>
#include <sys/stat.h>
#include <sys/brand.h>
#include <sys/mntio.h>
#include <sys/mnttab.h>
#include <sys/nvpair.h>
#include <sys/types.h>
#include <sys/sockio.h>
#include <ftw.h>
#include <pool.h>
#include <libscf.h>
#include <libproc.h>
#include <sys/priocntl.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <libxml/xmlmemory.h>
#include <libxml/parser.h>
#include <libdevinfo.h>
#include <uuid/uuid.h>
#include <dirent.h>
#include <libbrand.h>
#include <libzonecfg.h>
#include "zonecfg_impl.h"
#define _PATH_TMPFILE "/zonecfg.XXXXXX"
#define ZONE_CB_RETRY_COUNT 10
#define ZONE_EVENT_PING_SUBCLASS "ping"
#define ZONE_EVENT_PING_PUBLISHER "solaris"
/* Hard-code the DTD element/attribute/entity names just once, here. */
#define DTD_ELEM_ATTR (const xmlChar *) "attr"
#define DTD_ELEM_COMMENT (const xmlChar *) "comment"
#define DTD_ELEM_DEVICE (const xmlChar *) "device"
#define DTD_ELEM_FS (const xmlChar *) "filesystem"
#define DTD_ELEM_FSOPTION (const xmlChar *) "fsoption"
#define DTD_ELEM_IPD (const xmlChar *) "inherited-pkg-dir"
#define DTD_ELEM_NET (const xmlChar *) "network"
#define DTD_ELEM_RCTL (const xmlChar *) "rctl"
#define DTD_ELEM_RCTLVALUE (const xmlChar *) "rctl-value"
#define DTD_ELEM_ZONE (const xmlChar *) "zone"
#define DTD_ELEM_DATASET (const xmlChar *) "dataset"
#define DTD_ELEM_TMPPOOL (const xmlChar *) "tmp_pool"
#define DTD_ELEM_PSET (const xmlChar *) "pset"
#define DTD_ELEM_MCAP (const xmlChar *) "mcap"
#define DTD_ELEM_PACKAGE (const xmlChar *) "package"
#define DTD_ELEM_PATCH (const xmlChar *) "patch"
#define DTD_ELEM_OBSOLETES (const xmlChar *) "obsoletes"
#define DTD_ELEM_INCOMPATIBLE (const xmlChar *) "incompatible"
#define DTD_ELEM_DEV_PERM (const xmlChar *) "dev-perm"
#define DTD_ATTR_ACTION (const xmlChar *) "action"
#define DTD_ATTR_ADDRESS (const xmlChar *) "address"
#define DTD_ATTR_AUTOBOOT (const xmlChar *) "autoboot"
#define DTD_ATTR_IPTYPE (const xmlChar *) "ip-type"
#define DTD_ATTR_DIR (const xmlChar *) "directory"
#define DTD_ATTR_LIMIT (const xmlChar *) "limit"
#define DTD_ATTR_LIMITPRIV (const xmlChar *) "limitpriv"
#define DTD_ATTR_BOOTARGS (const xmlChar *) "bootargs"
#define DTD_ATTR_SCHED (const xmlChar *) "scheduling-class"
#define DTD_ATTR_MATCH (const xmlChar *) "match"
#define DTD_ATTR_NAME (const xmlChar *) "name"
#define DTD_ATTR_PHYSICAL (const xmlChar *) "physical"
#define DTD_ATTR_POOL (const xmlChar *) "pool"
#define DTD_ATTR_PRIV (const xmlChar *) "priv"
#define DTD_ATTR_RAW (const xmlChar *) "raw"
#define DTD_ATTR_SPECIAL (const xmlChar *) "special"
#define DTD_ATTR_TYPE (const xmlChar *) "type"
#define DTD_ATTR_VALUE (const xmlChar *) "value"
#define DTD_ATTR_ZONEPATH (const xmlChar *) "zonepath"
#define DTD_ATTR_NCPU_MIN (const xmlChar *) "ncpu_min"
#define DTD_ATTR_NCPU_MAX (const xmlChar *) "ncpu_max"
#define DTD_ATTR_IMPORTANCE (const xmlChar *) "importance"
#define DTD_ATTR_PHYSCAP (const xmlChar *) "physcap"
#define DTD_ATTR_VERSION (const xmlChar *) "version"
#define DTD_ATTR_ID (const xmlChar *) "id"
#define DTD_ATTR_UID (const xmlChar *) "uid"
#define DTD_ATTR_GID (const xmlChar *) "gid"
#define DTD_ATTR_MODE (const xmlChar *) "mode"
#define DTD_ATTR_ACL (const xmlChar *) "acl"
#define DTD_ATTR_BRAND (const xmlChar *) "brand"
#define DTD_ENTITY_BOOLEAN "boolean"
#define DTD_ENTITY_DEVPATH "devpath"
#define DTD_ENTITY_DRIVER "driver"
#define DTD_ENTITY_DRVMIN "drv_min"
#define DTD_ENTITY_FALSE "false"
#define DTD_ENTITY_INT "int"
#define DTD_ENTITY_STRING "string"
#define DTD_ENTITY_TRUE "true"
#define DTD_ENTITY_UINT "uint"
#define DTD_ENTITY_BOOL_LEN 6 /* "false" */
#define DETACHED "SUNWdetached.xml"
#define ATTACH_FORCED "SUNWattached.xml"
#define PKG_PATH "/var/sadm/pkg"
#define CONTENTS_FILE "/var/sadm/install/contents"
#define SUNW_PKG_ALL_ZONES "SUNW_PKG_ALLZONES=true\n"
#define SUNW_PKG_THIS_ZONE "SUNW_PKG_THISZONE=true\n"
#define VERSION "VERSION="
#define PATCHLIST "PATCHLIST="
#define PATCHINFO "PATCH_INFO_"
#define PKGINFO_RD_LEN 128
#define TMP_POOL_NAME "SUNWtmp_%s"
#define MAX_TMP_POOL_NAME (ZONENAME_MAX + 9)
#define RCAP_SERVICE "system/rcap:default"
#define POOLD_SERVICE "system/pools/dynamic:default"
/*
* rctl alias definitions
*
* This holds the alias, the full rctl name, the default priv value, action
* and lower limit. The functions that handle rctl aliases step through
* this table, matching on the alias, and using the full values for setting
* the rctl entry as well the limit for validation.
*/
static struct alias {
char *shortname;
char *realname;
char *priv;
char *action;
uint64_t low_limit;
} aliases[] = {
{ALIAS_MAXLWPS, "zone.max-lwps", "privileged", "deny", 100},
{ALIAS_MAXSHMMEM, "zone.max-shm-memory", "privileged", "deny", 0},
{ALIAS_MAXSHMIDS, "zone.max-shm-ids", "privileged", "deny", 0},
{ALIAS_MAXMSGIDS, "zone.max-msg-ids", "privileged", "deny", 0},
{ALIAS_MAXSEMIDS, "zone.max-sem-ids", "privileged", "deny", 0},
{ALIAS_MAXLOCKEDMEM, "zone.max-locked-memory", "privileged", "deny", 0},
{ALIAS_MAXSWAP, "zone.max-swap", "privileged", "deny", 0},
{ALIAS_SHARES, "zone.cpu-shares", "privileged", "none", 0},
{NULL, NULL, NULL, NULL, 0}
};
/*
* Structure for applying rctls to a running zone. It allows important
* process values to be passed together easily.
*/
typedef struct pr_info_handle {
struct ps_prochandle *pr;
pid_t pid;
} pr_info_handle_t;
struct zone_dochandle {
char *zone_dh_rootdir;
xmlDocPtr zone_dh_doc;
xmlNodePtr zone_dh_cur;
xmlNodePtr zone_dh_top;
boolean_t zone_dh_newzone;
boolean_t zone_dh_snapshot;
boolean_t zone_dh_sw_inv;
char zone_dh_delete_name[ZONENAME_MAX];
};
struct znotify {
void * zn_private;
evchan_t *zn_eventchan;
int (*zn_callback)(const char *zonename, zoneid_t zid,
const char *newstate, const char *oldstate, hrtime_t when, void *p);
pthread_mutex_t zn_mutex;
pthread_cond_t zn_cond;
pthread_mutex_t zn_bigmutex;
volatile enum {ZN_UNLOCKED, ZN_LOCKED, ZN_PING_INFLIGHT,
ZN_PING_RECEIVED} zn_state;
char zn_subscriber_id[MAX_SUBID_LEN];
volatile boolean_t zn_failed;
int zn_failure_count;
};
struct zone_pkginfo {
boolean_t zpi_all_zones;
boolean_t zpi_this_zone;
int zpi_patch_cnt;
char *zpi_version;
char **zpi_patchinfo;
};
char *zonecfg_root = "";
/*
* For functions which return int, which is most of the functions herein,
* the return values should be from the Z_foo set defined in <libzonecfg.h>.
* In some instances, we take pains mapping some libc errno values to Z_foo
* values from this set.
*/
/*
* Set the root (/) path for all zonecfg configuration files. This is a
* private interface used by Live Upgrade extensions to access zone
* configuration inside mounted alternate boot environments.
*/
void
zonecfg_set_root(const char *rootpath)
{
if (*zonecfg_root != '\0')
free(zonecfg_root);
if (rootpath == NULL || rootpath[0] == '\0' || rootpath[1] == '\0' ||
(zonecfg_root = strdup(rootpath)) == NULL)
zonecfg_root = "";
}
const char *
zonecfg_get_root(void)
{
return (zonecfg_root);
}
boolean_t
zonecfg_in_alt_root(void)
{
return (*zonecfg_root != '\0');
}
/*
* Callers of the _file_path() functions are expected to have the second
* parameter be a (char foo[MAXPATHLEN]).
*/
static boolean_t
config_file_path(const char *zonename, char *answer)
{
return (snprintf(answer, MAXPATHLEN, "%s%s/%s.xml", zonecfg_root,
ZONE_CONFIG_ROOT, zonename) < MAXPATHLEN);
}
static boolean_t
snap_file_path(const char *zonename, char *answer)
{
return (snprintf(answer, MAXPATHLEN, "%s%s/%s.snapshot.xml",
zonecfg_root, ZONE_SNAPSHOT_ROOT, zonename) < MAXPATHLEN);
}
/*ARGSUSED*/
static void
zonecfg_error_func(void *ctx, const char *msg, ...)
{
/*
* This function does nothing by design. Its purpose is to prevent
* libxml from dumping unwanted messages to stdout/stderr.
*/
}
zone_dochandle_t
zonecfg_init_handle(void)
{
zone_dochandle_t handle = calloc(1, sizeof (struct zone_dochandle));
if (handle == NULL) {
errno = Z_NOMEM;
return (NULL);
}
/* generic libxml initialization */
xmlLineNumbersDefault(1);
xmlLoadExtDtdDefaultValue |= XML_DETECT_IDS;
xmlDoValidityCheckingDefaultValue = 1;
(void) xmlKeepBlanksDefault(0);
xmlGetWarningsDefaultValue = 0;
xmlSetGenericErrorFunc(NULL, zonecfg_error_func);
return (handle);
}
int
zonecfg_check_handle(zone_dochandle_t handle)
{
if (handle == NULL || handle->zone_dh_doc == NULL)
return (Z_BAD_HANDLE);
return (Z_OK);
}
void
zonecfg_fini_handle(zone_dochandle_t handle)
{
if (zonecfg_check_handle(handle) == Z_OK)
xmlFreeDoc(handle->zone_dh_doc);
if (handle != NULL)
free(handle);
}
static int
zonecfg_destroy_impl(char *filename)
{
if (unlink(filename) == -1) {
if (errno == EACCES)
return (Z_ACCES);
if (errno == ENOENT)
return (Z_NO_ZONE);
return (Z_MISC_FS);
}
return (Z_OK);
}
int
zonecfg_destroy(const char *zonename, boolean_t force)
{
char path[MAXPATHLEN];
struct zoneent ze;
int err, state_err;
zone_state_t state;
if (!config_file_path(zonename, path))
return (Z_MISC_FS);
state_err = zone_get_state((char *)zonename, &state);
err = access(path, W_OK);
/*
* If there is no file, and no index entry, reliably indicate that no
* such zone exists.
*/
if ((state_err == Z_NO_ZONE) && (err == -1) && (errno == ENOENT))
return (Z_NO_ZONE);
/*
* Handle any other filesystem related errors (except if the XML
* file is missing, which we treat silently), unless we're forcing,
* in which case we plow on.
*/
if (err == -1 && errno != ENOENT) {
if (errno == EACCES)
return (Z_ACCES);
else if (!force)
return (Z_MISC_FS);
}
if (state > ZONE_STATE_INSTALLED)
return (Z_BAD_ZONE_STATE);
if (!force && state > ZONE_STATE_CONFIGURED)
return (Z_BAD_ZONE_STATE);
/*
* Index deletion succeeds even if the entry doesn't exist. So this
* will fail only if we've had some more severe problem.
*/
bzero(&ze, sizeof (ze));
(void) strlcpy(ze.zone_name, zonename, sizeof (ze.zone_name));
if ((err = putzoneent(&ze, PZE_REMOVE)) != Z_OK)
if (!force)
return (err);
err = zonecfg_destroy_impl(path);
/*
* Treat failure to find the XML file silently, since, well, it's
* gone, and with the index file cleaned up, we're done.
*/
if (err == Z_OK || err == Z_NO_ZONE)
return (Z_OK);
return (err);
}
int
zonecfg_destroy_snapshot(const char *zonename)
{
char path[MAXPATHLEN];
if (!snap_file_path(zonename, path))
return (Z_MISC_FS);
return (zonecfg_destroy_impl(path));
}
static int
getroot(zone_dochandle_t handle, xmlNodePtr *root)
{
if (zonecfg_check_handle(handle) == Z_BAD_HANDLE)
return (Z_BAD_HANDLE);
*root = xmlDocGetRootElement(handle->zone_dh_doc);
if (*root == NULL)
return (Z_EMPTY_DOCUMENT);
if (xmlStrcmp((*root)->name, DTD_ELEM_ZONE))
return (Z_WRONG_DOC_TYPE);
return (Z_OK);
}
static int
operation_prep(zone_dochandle_t handle)
{
xmlNodePtr root;
int err;
if ((err = getroot(handle, &root)) != 0)
return (err);
handle->zone_dh_cur = root;
handle->zone_dh_top = root;
return (Z_OK);
}
static int
fetchprop(xmlNodePtr cur, const xmlChar *propname, char *dst, size_t dstsize)
{
xmlChar *property;
size_t srcsize;
if ((property = xmlGetProp(cur, propname)) == NULL)
return (Z_BAD_PROPERTY);
srcsize = strlcpy(dst, (char *)property, dstsize);
xmlFree(property);
if (srcsize >= dstsize)
return (Z_TOO_BIG);
return (Z_OK);
}
static int
fetch_alloc_prop(xmlNodePtr cur, const xmlChar *propname, char **dst)
{
xmlChar *property;
if ((property = xmlGetProp(cur, propname)) == NULL)
return (Z_BAD_PROPERTY);
if ((*dst = strdup((char *)property)) == NULL) {
xmlFree(property);
return (Z_NOMEM);
}
xmlFree(property);
return (Z_OK);
}
static int
getrootattr(zone_dochandle_t handle, const xmlChar *propname,
char *propval, size_t propsize)
{
xmlNodePtr root;
int err;
if ((err = getroot(handle, &root)) != 0)
return (err);
return (fetchprop(root, propname, propval, propsize));
}
static int
get_alloc_rootattr(zone_dochandle_t handle, const xmlChar *propname,
char **propval)
{
xmlNodePtr root;
int err;
if ((err = getroot(handle, &root)) != 0)
return (err);
return (fetch_alloc_prop(root, propname, propval));
}
static int
setrootattr(zone_dochandle_t handle, const xmlChar *propname,
const char *propval)
{
int err;
xmlNodePtr root;
if ((err = getroot(handle, &root)) != Z_OK)
return (err);
/*
* If we get a null propval remove the property (ignore return since it
* may not be set to begin with).
*/
if (propval == NULL) {
(void) xmlUnsetProp(root, propname);
} else {
if (xmlSetProp(root, propname, (const xmlChar *) propval)
== NULL)
return (Z_INVAL);
}
return (Z_OK);
}
static void
addcomment(zone_dochandle_t handle, const char *comment)
{
xmlNodePtr node;
node = xmlNewComment((xmlChar *) comment);
if (node != NULL)
(void) xmlAddPrevSibling(handle->zone_dh_top, node);
}
static void
stripcomments(zone_dochandle_t handle)
{
xmlDocPtr top;
xmlNodePtr child, next;
top = handle->zone_dh_doc;
for (child = top->xmlChildrenNode; child != NULL; child = next) {
next = child->next;
if (child->name == NULL)
continue;
if (xmlStrcmp(child->name, DTD_ELEM_COMMENT) == 0) {
next = child->next;
xmlUnlinkNode(child);
xmlFreeNode(child);
}
}
}
static void
strip_sw_inv(zone_dochandle_t handle)
{
xmlNodePtr root, child, next;
root = xmlDocGetRootElement(handle->zone_dh_doc);
for (child = root->xmlChildrenNode; child != NULL; child = next) {
next = child->next;
if (child->name == NULL)
continue;
if (xmlStrcmp(child->name, DTD_ELEM_PACKAGE) == 0 ||
xmlStrcmp(child->name, DTD_ELEM_PATCH) == 0) {
next = child->next;
xmlUnlinkNode(child);
xmlFreeNode(child);
}
}
}
static int
zonecfg_get_handle_impl(const char *zonename, const char *filename,
zone_dochandle_t handle)
{
xmlValidCtxtPtr cvp;
struct stat statbuf;
int valid;
if (zonename == NULL)
return (Z_NO_ZONE);
if ((handle->zone_dh_doc = xmlParseFile(filename)) == NULL) {
/* distinguish file not found vs. found but not parsed */
if (stat(filename, &statbuf) == 0)
return (Z_INVALID_DOCUMENT);
return (Z_NO_ZONE);
}
if ((cvp = xmlNewValidCtxt()) == NULL)
return (Z_NOMEM);
cvp->error = zonecfg_error_func;
cvp->warning = zonecfg_error_func;
valid = xmlValidateDocument(cvp, handle->zone_dh_doc);
xmlFreeValidCtxt(cvp);
if (valid == 0)
return (Z_INVALID_DOCUMENT);
/* delete any comments such as inherited Sun copyright / ident str */
stripcomments(handle);
return (Z_OK);
}
int
zonecfg_get_handle(const char *zonename, zone_dochandle_t handle)
{
char path[MAXPATHLEN];
if (!config_file_path(zonename, path))
return (Z_MISC_FS);
handle->zone_dh_newzone = B_FALSE;
return (zonecfg_get_handle_impl(zonename, path, handle));
}
int
zonecfg_get_attach_handle(const char *path, const char *zonename,
boolean_t preserve_sw, zone_dochandle_t handle)
{
char migpath[MAXPATHLEN];
int err;
struct stat buf;
if (snprintf(migpath, sizeof (migpath), "%s/root", path) >=
sizeof (migpath))
return (Z_NOMEM);
if (stat(migpath, &buf) == -1 || !S_ISDIR(buf.st_mode))
return (Z_NO_ZONE);
if (snprintf(migpath, sizeof (migpath), "%s/%s", path, DETACHED) >=
sizeof (migpath))
return (Z_NOMEM);
if ((err = zonecfg_get_handle_impl(zonename, migpath, handle)) != Z_OK)
return (err);
if (!preserve_sw)
strip_sw_inv(handle);
handle->zone_dh_newzone = B_TRUE;
if ((err = setrootattr(handle, DTD_ATTR_ZONEPATH, path)) != Z_OK)
return (err);
return (setrootattr(handle, DTD_ATTR_NAME, zonename));
}
int
zonecfg_get_snapshot_handle(const char *zonename, zone_dochandle_t handle)
{
char path[MAXPATHLEN];
if (!snap_file_path(zonename, path))
return (Z_MISC_FS);
handle->zone_dh_newzone = B_FALSE;
return (zonecfg_get_handle_impl(zonename, path, handle));
}
int
zonecfg_get_template_handle(const char *template, const char *zonename,
zone_dochandle_t handle)
{
char path[MAXPATHLEN];
int err;
if (!config_file_path(template, path))
return (Z_MISC_FS);
if ((err = zonecfg_get_handle_impl(template, path, handle)) != Z_OK)
return (err);
handle->zone_dh_newzone = B_TRUE;
return (setrootattr(handle, DTD_ATTR_NAME, zonename));
}
int
zonecfg_get_xml_handle(const char *path, zone_dochandle_t handle)
{
struct stat buf;
int err;
if (stat(path, &buf) == -1)
return (Z_MISC_FS);
if ((err = zonecfg_get_handle_impl("xml", path, handle)) != Z_OK)
return (err);
handle->zone_dh_newzone = B_TRUE;
return (Z_OK);
}
/*
* Initialize two handles from the manifest read on fd. The rem_handle
* is initialized from the input file, including the sw inventory. The
* local_handle is initialized with the same zone configuration but with
* no sw inventory.
*/
int
zonecfg_attach_manifest(int fd, zone_dochandle_t local_handle,
zone_dochandle_t rem_handle)
{
xmlValidCtxtPtr cvp;
int valid;
/* load the manifest into the handle for the remote system */
if ((rem_handle->zone_dh_doc = xmlReadFd(fd, NULL, NULL, 0)) == NULL) {
return (Z_INVALID_DOCUMENT);
}
if ((cvp = xmlNewValidCtxt()) == NULL)
return (Z_NOMEM);
cvp->error = zonecfg_error_func;
cvp->warning = zonecfg_error_func;
valid = xmlValidateDocument(cvp, rem_handle->zone_dh_doc);
xmlFreeValidCtxt(cvp);
if (valid == 0)
return (Z_INVALID_DOCUMENT);
/* delete any comments such as inherited Sun copyright / ident str */
stripcomments(rem_handle);
rem_handle->zone_dh_newzone = B_TRUE;
rem_handle->zone_dh_sw_inv = B_TRUE;
/*
* Now use the remote system handle to generate a local system handle
* with an identical zones configuration but no sw inventory.
*/
if ((local_handle->zone_dh_doc = xmlCopyDoc(rem_handle->zone_dh_doc,
1)) == NULL) {
return (Z_INVALID_DOCUMENT);
}
/*
* We need to re-run xmlValidateDocument on local_handle to properly
* update the in-core representation of the configuration.
*/
if ((cvp = xmlNewValidCtxt()) == NULL)
return (Z_NOMEM);
cvp->error = zonecfg_error_func;
cvp->warning = zonecfg_error_func;
valid = xmlValidateDocument(cvp, local_handle->zone_dh_doc);
xmlFreeValidCtxt(cvp);
if (valid == 0)
return (Z_INVALID_DOCUMENT);
strip_sw_inv(local_handle);
local_handle->zone_dh_newzone = B_TRUE;
local_handle->zone_dh_sw_inv = B_FALSE;
return (Z_OK);
}
static boolean_t
is_renaming(zone_dochandle_t handle)
{
if (handle->zone_dh_newzone)
return (B_FALSE);
if (strlen(handle->zone_dh_delete_name) > 0)
return (B_TRUE);
return (B_FALSE);
}
static boolean_t
is_new(zone_dochandle_t handle)
{
return (handle->zone_dh_newzone || handle->zone_dh_snapshot);
}
static boolean_t
is_snapshot(zone_dochandle_t handle)
{
return (handle->zone_dh_snapshot);
}
/*
* It would be great to be able to use libc's ctype(3c) macros, but we
* can't, as they are locale sensitive, and it would break our limited thread
* safety if this routine had to change the app locale on the fly.
*/
int
zonecfg_validate_zonename(const char *zone)
{
int i;
if (strcmp(zone, GLOBAL_ZONENAME) == 0)
return (Z_BOGUS_ZONE_NAME);
if (strlen(zone) >= ZONENAME_MAX)
return (Z_BOGUS_ZONE_NAME);
if (!((zone[0] >= 'a' && zone[0] <= 'z') ||
(zone[0] >= 'A' && zone[0] <= 'Z') ||
(zone[0] >= '0' && zone[0] <= '9')))
return (Z_BOGUS_ZONE_NAME);
for (i = 1; zone[i] != '\0'; i++) {
if (!((zone[i] >= 'a' && zone[i] <= 'z') ||
(zone[i] >= 'A' && zone[i] <= 'Z') ||
(zone[i] >= '0' && zone[i] <= '9') ||
(zone[i] == '-') || (zone[i] == '_') || (zone[i] == '.')))
return (Z_BOGUS_ZONE_NAME);
}
return (Z_OK);
}
/*
* Changing the zone name requires us to track both the old and new
* name of the zone until commit time.
*/
int
zonecfg_get_name(zone_dochandle_t handle, char *name, size_t namesize)
{
return (getrootattr(handle, DTD_ATTR_NAME, name, namesize));
}
int
zonecfg_set_name(zone_dochandle_t handle, char *name)
{
zone_state_t state;
char curname[ZONENAME_MAX], old_delname[ZONENAME_MAX];
int err;
if ((err = getrootattr(handle, DTD_ATTR_NAME, curname,
sizeof (curname))) != Z_OK)
return (err);
if (strcmp(name, curname) == 0)
return (Z_OK);
/*
* Switching zone names to one beginning with SUNW is not permitted.
*/
if (strncmp(name, "SUNW", 4) == 0)
return (Z_BOGUS_ZONE_NAME);
if ((err = zonecfg_validate_zonename(name)) != Z_OK)
return (err);
/*
* Setting the name back to the original name (effectively a revert of
* the name) is fine. But if we carry on, we'll falsely identify the
* name as "in use," so special case here.
*/
if (strcmp(name, handle->zone_dh_delete_name) == 0) {
err = setrootattr(handle, DTD_ATTR_NAME, name);
handle->zone_dh_delete_name[0] = '\0';
return (err);
}
/* Check to see if new name chosen is already in use */
if (zone_get_state(name, &state) != Z_NO_ZONE)
return (Z_NAME_IN_USE);
/*
* If this isn't already "new" or in a renaming transition, then
* we're initiating a rename here; so stash the "delete name"
* (i.e. the name of the zone we'll be removing) for the rename.
*/
(void) strlcpy(old_delname, handle->zone_dh_delete_name,
sizeof (old_delname));
if (!is_new(handle) && !is_renaming(handle)) {
/*
* Name change is allowed only when the zone we're altering
* is not ready or running.
*/
err = zone_get_state(curname, &state);
if (err == Z_OK) {
if (state > ZONE_STATE_INSTALLED)
return (Z_BAD_ZONE_STATE);
} else if (err != Z_NO_ZONE) {
return (err);
}
(void) strlcpy(handle->zone_dh_delete_name, curname,
sizeof (handle->zone_dh_delete_name));
assert(is_renaming(handle));
} else if (is_renaming(handle)) {
err = zone_get_state(handle->zone_dh_delete_name, &state);
if (err == Z_OK) {
if (state > ZONE_STATE_INSTALLED)
return (Z_BAD_ZONE_STATE);
} else if (err != Z_NO_ZONE) {
return (err);
}
}
if ((err = setrootattr(handle, DTD_ATTR_NAME, name)) != Z_OK) {
/*
* Restore the deletename to whatever it was at the
* top of the routine, since we've had a failure.
*/
(void) strlcpy(handle->zone_dh_delete_name, old_delname,
sizeof (handle->zone_dh_delete_name));
return (err);
}
return (Z_OK);
}
int
zonecfg_get_zonepath(zone_dochandle_t handle, char *path, size_t pathsize)
{
size_t len;
if ((len = strlcpy(path, zonecfg_root, pathsize)) >= pathsize)
return (Z_TOO_BIG);
return (getrootattr(handle, DTD_ATTR_ZONEPATH, path + len,
pathsize - len));
}
int
zonecfg_set_zonepath(zone_dochandle_t handle, char *zonepath)
{
size_t len;
/*
* The user deals in absolute paths in the running global zone, but the
* internal configuration files deal with boot environment relative
* paths. Strip out the alternate root when specified.
*/
len = strlen(zonecfg_root);
if (strncmp(zonepath, zonecfg_root, len) != 0 || zonepath[len] != '/')
return (Z_BAD_PROPERTY);
zonepath += len;
return (setrootattr(handle, DTD_ATTR_ZONEPATH, zonepath));
}
int
zonecfg_get_brand(zone_dochandle_t handle, char *brand, size_t brandsize)
{
int ret, sz;
ret = getrootattr(handle, DTD_ATTR_BRAND, brand, brandsize);
/* If the zone has no brand, it is native. */
if (ret == Z_OK && brand[0] == '\0') {
sz = strlcpy(brand, NATIVE_BRAND_NAME, brandsize);
if (sz >= brandsize)
ret = Z_TOO_BIG;
else
ret = Z_OK;
}
return (ret);
}
int
zonecfg_set_brand(zone_dochandle_t handle, char *brand)
{
return (setrootattr(handle, DTD_ATTR_BRAND, brand));
}
int
zonecfg_get_autoboot(zone_dochandle_t handle, boolean_t *autoboot)
{
char autobootstr[DTD_ENTITY_BOOL_LEN];
int ret;
if ((ret = getrootattr(handle, DTD_ATTR_AUTOBOOT, autobootstr,
sizeof (autobootstr))) != Z_OK)
return (ret);
if (strcmp(autobootstr, DTD_ENTITY_TRUE) == 0)
*autoboot = B_TRUE;
else if (strcmp(autobootstr, DTD_ENTITY_FALSE) == 0)
*autoboot = B_FALSE;
else
ret = Z_BAD_PROPERTY;
return (ret);
}
int
zonecfg_set_autoboot(zone_dochandle_t handle, boolean_t autoboot)
{
return (setrootattr(handle, DTD_ATTR_AUTOBOOT,
autoboot ? DTD_ENTITY_TRUE : DTD_ENTITY_FALSE));
}
int
zonecfg_get_pool(zone_dochandle_t handle, char *pool, size_t poolsize)
{
return (getrootattr(handle, DTD_ATTR_POOL, pool, poolsize));
}
int
zonecfg_set_pool(zone_dochandle_t handle, char *pool)
{
return (setrootattr(handle, DTD_ATTR_POOL, pool));
}
int
zonecfg_get_limitpriv(zone_dochandle_t handle, char **limitpriv)
{
return (get_alloc_rootattr(handle, DTD_ATTR_LIMITPRIV, limitpriv));
}
int
zonecfg_set_limitpriv(zone_dochandle_t handle, char *limitpriv)
{
return (setrootattr(handle, DTD_ATTR_LIMITPRIV, limitpriv));
}
int
zonecfg_get_bootargs(zone_dochandle_t handle, char *bargs, size_t bargssize)
{
return (getrootattr(handle, DTD_ATTR_BOOTARGS, bargs, bargssize));
}
int
zonecfg_set_bootargs(zone_dochandle_t handle, char *bargs)
{
return (setrootattr(handle, DTD_ATTR_BOOTARGS, bargs));
}
int
zonecfg_get_sched_class(zone_dochandle_t handle, char *sched, size_t schedsize)
{
return (getrootattr(handle, DTD_ATTR_SCHED, sched, schedsize));
}
int
zonecfg_set_sched(zone_dochandle_t handle, char *sched)
{
return (setrootattr(handle, DTD_ATTR_SCHED, sched));
}
/*
* /etc/zones/index caches a vital piece of information which is also
* in the <zonename>.xml file: the path to the zone. This is for performance,
* since we need to walk all zonepath's in order to be able to detect conflicts
* (see crosscheck_zonepaths() in the zoneadm command).
*
* An additional complexity is that when doing a rename, we'd like the entire
* index update operation (rename, and potential state changes) to be atomic.
* In general, the operation of this function should succeed or fail as
* a unit.
*/
int
zonecfg_refresh_index_file(zone_dochandle_t handle)
{
char name[ZONENAME_MAX], zonepath[MAXPATHLEN];
struct zoneent ze;
int err;
int opcode;
char *zn;
bzero(&ze, sizeof (ze));
ze.zone_state = -1; /* Preserve existing state in index */
if ((err = zonecfg_get_name(handle, name, sizeof (name))) != Z_OK)
return (err);
(void) strlcpy(ze.zone_name, name, sizeof (ze.zone_name));
if ((err = zonecfg_get_zonepath(handle, zonepath,
sizeof (zonepath))) != Z_OK)
return (err);
(void) strlcpy(ze.zone_path, zonepath + strlen(zonecfg_root),
sizeof (ze.zone_path));
if (is_renaming(handle)) {
opcode = PZE_MODIFY;
(void) strlcpy(ze.zone_name, handle->zone_dh_delete_name,
sizeof (ze.zone_name));
(void) strlcpy(ze.zone_newname, name, sizeof (ze.zone_newname));
} else if (is_new(handle)) {
FILE *cookie;
/*
* Be tolerant of the zone already existing in the index file,
* since we might be forcibly overwriting an existing
* configuration with a new one (for example 'create -F'
* in zonecfg).
*/
opcode = PZE_ADD;
cookie = setzoneent();
while ((zn = getzoneent(cookie)) != NULL) {
if (strcmp(zn, name) == 0) {
opcode = PZE_MODIFY;
free(zn);
break;
}
free(zn);
}
endzoneent(cookie);
ze.zone_state = ZONE_STATE_CONFIGURED;
} else {
opcode = PZE_MODIFY;
}
if ((err = putzoneent(&ze, opcode)) != Z_OK)
return (err);
return (Z_OK);
}
/*
* The goal of this routine is to cause the index file update and the
* document save to happen as an atomic operation. We do the document
* first, saving a backup copy using a hard link; if that succeeds, we go
* on to the index. If that fails, we roll the document back into place.
*
* Strategy:
*
* New zone 'foo' configuration:
* Create tmpfile (zonecfg.xxxxxx)
* Write XML to tmpfile
* Rename tmpfile to xmlfile (zonecfg.xxxxxx -> foo.xml)
* Add entry to index file
* If it fails, delete foo.xml, leaving nothing behind.
*
* Save existing zone 'foo':
* Make backup of foo.xml -> .backup
* Create tmpfile (zonecfg.xxxxxx)
* Write XML to tmpfile
* Rename tmpfile to xmlfile (zonecfg.xxxxxx -> foo.xml)
* Modify index file as needed
* If it fails, recover from .backup -> foo.xml
*
* Rename 'foo' to 'bar':
* Create tmpfile (zonecfg.xxxxxx)
* Write XML to tmpfile
* Rename tmpfile to xmlfile (zonecfg.xxxxxx -> bar.xml)
* Add entry for 'bar' to index file, Remove entry for 'foo' (refresh)
* If it fails, delete bar.xml; foo.xml is left behind.
*/
static int
zonecfg_save_impl(zone_dochandle_t handle, char *filename)
{
char tmpfile[MAXPATHLEN];
char bakdir[MAXPATHLEN], bakbase[MAXPATHLEN], bakfile[MAXPATHLEN];
int tmpfd, err, valid;
xmlValidCtxt cvp = { NULL };
boolean_t backup;
(void) strlcpy(tmpfile, filename, sizeof (tmpfile));
(void) dirname(tmpfile);
(void) strlcat(tmpfile, _PATH_TMPFILE, sizeof (tmpfile));
tmpfd = mkstemp(tmpfile);
if (tmpfd == -1) {
(void) unlink(tmpfile);
return (Z_TEMP_FILE);
}
(void) close(tmpfd);
cvp.error = zonecfg_error_func;
cvp.warning = zonecfg_error_func;
/*
* We do a final validation of the document. Since the library has
* malfunctioned if it fails to validate, we follow-up with an
* assert() that the doc is valid.
*/
valid = xmlValidateDocument(&cvp, handle->zone_dh_doc);
assert(valid != 0);
if (xmlSaveFormatFile(tmpfile, handle->zone_dh_doc, 1) <= 0)
goto err;
(void) chmod(tmpfile, 0644);
/*
* In the event we are doing a standard save, hard link a copy of the
* original file in .backup.<pid>.filename so we can restore it if
* something goes wrong.
*/
if (!is_new(handle) && !is_renaming(handle)) {
backup = B_TRUE;
(void) strlcpy(bakdir, filename, sizeof (bakdir));
(void) strlcpy(bakbase, filename, sizeof (bakbase));
(void) snprintf(bakfile, sizeof (bakfile), "%s/.backup.%d.%s",
dirname(bakdir), getpid(), basename(bakbase));
if (link(filename, bakfile) == -1) {
err = errno;
(void) unlink(tmpfile);
if (errno == EACCES)
return (Z_ACCES);
return (Z_MISC_FS);
}
}
/*
* Move the new document over top of the old.
* i.e.: zonecfg.XXXXXX -> myzone.xml
*/
if (rename(tmpfile, filename) == -1) {
err = errno;
(void) unlink(tmpfile);
if (backup)
(void) unlink(bakfile);
if (err == EACCES)
return (Z_ACCES);
return (Z_MISC_FS);
}
/*
* If this is a snapshot, we're done-- don't add an index entry.
*/
if (is_snapshot(handle))
return (Z_OK);
/* now update the index file to reflect whatever we just did */
if ((err = zonecfg_refresh_index_file(handle)) != Z_OK) {
if (backup) {
/*
* Try to restore from our backup.
*/
(void) unlink(filename);
(void) rename(bakfile, filename);
} else {
/*
* Either the zone is new, in which case we can delete
* new.xml, or we're doing a rename, so ditto.
*/
assert(is_new(handle) || is_renaming(handle));
(void) unlink(filename);
}
return (Z_UPDATING_INDEX);
}
if (backup)
(void) unlink(bakfile);
return (Z_OK);
err:
(void) unlink(tmpfile);
return (Z_SAVING_FILE);
}
int
zonecfg_save(zone_dochandle_t handle)
{
char zname[ZONENAME_MAX], path[MAXPATHLEN];
char delpath[MAXPATHLEN];
int err = Z_SAVING_FILE;
if (zonecfg_check_handle(handle) != Z_OK)
return (Z_BAD_HANDLE);
/*
* We don't support saving snapshots or a tree containing a sw
* inventory at this time.
*/
if (handle->zone_dh_snapshot || handle->zone_dh_sw_inv)
return (Z_INVAL);
if ((err = zonecfg_get_name(handle, zname, sizeof (zname))) != Z_OK)
return (err);
if (!config_file_path(zname, path))
return (Z_MISC_FS);
addcomment(handle, "\n DO NOT EDIT THIS "
"FILE. Use zonecfg(1M) instead.\n");
err = zonecfg_save_impl(handle, path);
stripcomments(handle);
if (err != Z_OK)
return (err);
handle->zone_dh_newzone = B_FALSE;
if (is_renaming(handle)) {
if (config_file_path(handle->zone_dh_delete_name, delpath))
(void) unlink(delpath);
handle->zone_dh_delete_name[0] = '\0';
}
return (Z_OK);
}
int
zonecfg_verify_save(zone_dochandle_t handle, char *filename)
{
int valid;
xmlValidCtxt cvp = { NULL };
if (zonecfg_check_handle(handle) != Z_OK)
return (Z_BAD_HANDLE);
cvp.error = zonecfg_error_func;
cvp.warning = zonecfg_error_func;
/*
* We do a final validation of the document. Since the library has
* malfunctioned if it fails to validate, we follow-up with an
* assert() that the doc is valid.
*/
valid = xmlValidateDocument(&cvp, handle->zone_dh_doc);
assert(valid != 0);
if (xmlSaveFormatFile(filename, handle->zone_dh_doc, 1) <= 0)
return (Z_SAVING_FILE);
return (Z_OK);
}
int
zonecfg_detach_save(zone_dochandle_t handle, uint_t flags)
{
char zname[ZONENAME_MAX];
char path[MAXPATHLEN];
char migpath[MAXPATHLEN];
xmlValidCtxt cvp = { NULL };
int err = Z_SAVING_FILE;
int valid;
if (zonecfg_check_handle(handle) != Z_OK)
return (Z_BAD_HANDLE);
/*
* We can only detach if we have taken a sw inventory.
*/
if (!handle->zone_dh_sw_inv)
return (Z_INVAL);
if (flags & ZONE_DRY_RUN) {
(void) strlcpy(migpath, "-", sizeof (migpath));
} else {
if ((err = zonecfg_get_name(handle, zname, sizeof (zname)))
!= Z_OK)
return (err);
if ((err = zone_get_zonepath(zname, path, sizeof (path)))
!= Z_OK)
return (err);
if (snprintf(migpath, sizeof (migpath), "%s/%s", path, DETACHED)
>= sizeof (migpath))
return (Z_NOMEM);
}
if ((err = operation_prep(handle)) != Z_OK)
return (err);
addcomment(handle, "\n DO NOT EDIT THIS FILE. "
"Use zonecfg(1M) and zoneadm(1M) attach.\n");
cvp.error = zonecfg_error_func;
cvp.warning = zonecfg_error_func;
/*
* We do a final validation of the document. Since the library has
* malfunctioned if it fails to validate, we follow-up with an
* assert() that the doc is valid.
*/
valid = xmlValidateDocument(&cvp, handle->zone_dh_doc);
assert(valid != 0);
if (xmlSaveFormatFile(migpath, handle->zone_dh_doc, 1) <= 0)
return (Z_SAVING_FILE);
if (!(flags & ZONE_DRY_RUN))
(void) chmod(migpath, 0644);
stripcomments(handle);
handle->zone_dh_newzone = B_FALSE;
return (Z_OK);
}
boolean_t
zonecfg_detached(const char *path)
{
char migpath[MAXPATHLEN];
struct stat buf;
if (snprintf(migpath, sizeof (migpath), "%s/%s", path, DETACHED) >=
sizeof (migpath))
return (B_FALSE);
if (stat(migpath, &buf) != -1)
return (B_TRUE);
return (B_FALSE);
}
void
zonecfg_rm_detached(zone_dochandle_t handle, boolean_t forced)
{
char zname[ZONENAME_MAX];
char path[MAXPATHLEN];
char detached[MAXPATHLEN];
char attached[MAXPATHLEN];
if (zonecfg_check_handle(handle) != Z_OK)
return;
if (zonecfg_get_name(handle, zname, sizeof (zname)) != Z_OK)
return;
if (zone_get_zonepath(zname, path, sizeof (path)) != Z_OK)
return;
(void) snprintf(detached, sizeof (detached), "%s/%s", path, DETACHED);
(void) snprintf(attached, sizeof (attached), "%s/%s", path,
ATTACH_FORCED);
if (forced) {
(void) rename(detached, attached);
} else {
(void) unlink(attached);
(void) unlink(detached);
}
}
/*
* Special case: if access(2) fails with ENOENT, then try again using
* ZONE_CONFIG_ROOT instead of config_file_path(zonename). This is how we
* work around the case of a config file which has not been created yet:
* the user will need access to the directory so use that as a heuristic.
*/
int
zonecfg_access(const char *zonename, int amode)
{
char path[MAXPATHLEN];
if (!config_file_path(zonename, path))
return (Z_INVAL);
if (access(path, amode) == 0)
return (Z_OK);
if (errno == ENOENT) {
if (snprintf(path, sizeof (path), "%s%s", zonecfg_root,
ZONE_CONFIG_ROOT) >= sizeof (path))
return (Z_INVAL);
if (access(path, amode) == 0)
return (Z_OK);
}
if (errno == EACCES)
return (Z_ACCES);
if (errno == EINVAL)
return (Z_INVAL);
return (Z_MISC_FS);
}
int
zonecfg_create_snapshot(const char *zonename)
{
zone_dochandle_t handle;
char path[MAXPATHLEN], zonepath[MAXPATHLEN], rpath[MAXPATHLEN];
int error = Z_OK, res;
if ((handle = zonecfg_init_handle()) == NULL) {
return (Z_NOMEM);
}
handle->zone_dh_newzone = B_TRUE;
handle->zone_dh_snapshot = B_TRUE;
if ((error = zonecfg_get_handle(zonename, handle)) != Z_OK)
goto out;
if ((error = operation_prep(handle)) != Z_OK)
goto out;
error = zonecfg_get_zonepath(handle, zonepath, sizeof (zonepath));
if (error != Z_OK)
goto out;
if ((res = resolvepath(zonepath, rpath, sizeof (rpath))) == -1) {
error = Z_RESOLVED_PATH;
goto out;
}
/*
* If the resolved path is not the same as the original path, then
* save the resolved path in the snapshot, thus preventing any
* potential problems down the line when zoneadmd goes to unmount
* file systems and depends on initial string matches with resolved
* paths.
*/
rpath[res] = '\0';
if (strcmp(zonepath, rpath) != 0) {
if ((error = zonecfg_set_zonepath(handle, rpath)) != Z_OK)
goto out;
}
if (snprintf(path, sizeof (path), "%s%s", zonecfg_root,
ZONE_SNAPSHOT_ROOT) >= sizeof (path)) {
error = Z_MISC_FS;
goto out;
}
if ((mkdir(path, S_IRWXU) == -1) && (errno != EEXIST)) {
error = Z_MISC_FS;
goto out;
}
if (!snap_file_path(zonename, path)) {
error = Z_MISC_FS;
goto out;
}
addcomment(handle, "\n DO NOT EDIT THIS FILE. "
"It is a snapshot of running zone state.\n");
error = zonecfg_save_impl(handle, path);
stripcomments(handle);
out:
zonecfg_fini_handle(handle);
return (error);
}
int
zonecfg_get_iptype(zone_dochandle_t handle, zone_iptype_t *iptypep)
{
char property[10]; /* 10 is big enough for "shared"/"exclusive" */
int err;
err = getrootattr(handle, DTD_ATTR_IPTYPE, property, sizeof (property));
if (err == Z_BAD_PROPERTY) {
/* Return default value */
*iptypep = ZS_SHARED;
return (Z_OK);
} else if (err != Z_OK) {
return (err);
}
if (strlen(property) == 0 ||
strcmp(property, "shared") == 0)
*iptypep = ZS_SHARED;
else if (strcmp(property, "exclusive") == 0)
*iptypep = ZS_EXCLUSIVE;
else
return (Z_INVAL);
return (Z_OK);
}
int
zonecfg_set_iptype(zone_dochandle_t handle, zone_iptype_t iptype)
{
xmlNodePtr cur;
if (handle == NULL)
return (Z_INVAL);
cur = xmlDocGetRootElement(handle->zone_dh_doc);
if (cur == NULL) {
return (Z_EMPTY_DOCUMENT);
}
if (xmlStrcmp(cur->name, DTD_ELEM_ZONE) != 0) {
return (Z_WRONG_DOC_TYPE);
}
switch (iptype) {
case ZS_SHARED:
/*
* Since "shared" is the default, we don't write it to the
* configuration file, so that it's easier to migrate those
* zones elsewhere, eg., to systems which are not IP-Instances
* aware.
* xmlUnsetProp only fails when the attribute doesn't exist,
* which we don't care.
*/
(void) xmlUnsetProp(cur, DTD_ATTR_IPTYPE);
break;
case ZS_EXCLUSIVE:
if (xmlSetProp(cur, DTD_ATTR_IPTYPE,
(const xmlChar *) "exclusive") == NULL)
return (Z_INVAL);
break;
}
return (Z_OK);
}
static int
newprop(xmlNodePtr node, const xmlChar *attrname, char *src)
{
xmlAttrPtr newattr;
newattr = xmlNewProp(node, attrname, (xmlChar *)src);
if (newattr == NULL) {
xmlUnlinkNode(node);
xmlFreeNode(node);
return (Z_BAD_PROPERTY);
}
return (Z_OK);
}
static int
zonecfg_add_filesystem_core(zone_dochandle_t handle, struct zone_fstab *tabptr)
{
xmlNodePtr newnode, cur = handle->zone_dh_cur, options_node;
zone_fsopt_t *ptr;
int err;
newnode = xmlNewTextChild(cur, NULL, DTD_ELEM_FS, NULL);
if ((err = newprop(newnode, DTD_ATTR_SPECIAL,
tabptr->zone_fs_special)) != Z_OK)
return (err);
if (tabptr->zone_fs_raw[0] != '\0' &&
(err = newprop(newnode, DTD_ATTR_RAW, tabptr->zone_fs_raw)) != Z_OK)
return (err);
if ((err = newprop(newnode, DTD_ATTR_DIR, tabptr->zone_fs_dir)) != Z_OK)
return (err);
if ((err = newprop(newnode, DTD_ATTR_TYPE,
tabptr->zone_fs_type)) != Z_OK)
return (err);
if (tabptr->zone_fs_options != NULL) {
for (ptr = tabptr->zone_fs_options; ptr != NULL;
ptr = ptr->zone_fsopt_next) {
options_node = xmlNewTextChild(newnode, NULL,
DTD_ELEM_FSOPTION, NULL);
if ((err = newprop(options_node, DTD_ATTR_NAME,
ptr->zone_fsopt_opt)) != Z_OK)
return (err);
}
}
return (Z_OK);
}
int
zonecfg_add_filesystem(zone_dochandle_t handle, struct zone_fstab *tabptr)
{
int err;
if (tabptr == NULL)
return (Z_INVAL);
if ((err = operation_prep(handle)) != Z_OK)
return (err);
if ((err = zonecfg_add_filesystem_core(handle, tabptr)) != Z_OK)
return (err);
return (Z_OK);
}
static int
zonecfg_add_ipd_core(zone_dochandle_t handle, struct zone_fstab *tabptr)
{
xmlNodePtr newnode, cur = handle->zone_dh_cur;
int err;
newnode = xmlNewTextChild(cur, NULL, DTD_ELEM_IPD, NULL);
if ((err = newprop(newnode, DTD_ATTR_DIR, tabptr->zone_fs_dir)) != Z_OK)
return (err);
return (Z_OK);
}
int
zonecfg_add_ipd(zone_dochandle_t handle, struct zone_fstab *tabptr)
{
int err;
if (tabptr == NULL)
return (Z_INVAL);
if ((err = operation_prep(handle)) != Z_OK)
return (err);
if ((err = zonecfg_add_ipd_core(handle, tabptr)) != Z_OK)
return (err);
return (Z_OK);
}
int
zonecfg_add_fs_option(struct zone_fstab *tabptr, char *option)
{
zone_fsopt_t *last, *old, *new;
last = tabptr->zone_fs_options;
for (old = last; old != NULL; old = old->zone_fsopt_next)
last = old; /* walk to the end of the list */
new = (zone_fsopt_t *)malloc(sizeof (zone_fsopt_t));
if (new == NULL)
return (Z_NOMEM);
(void) strlcpy(new->zone_fsopt_opt, option,
sizeof (new->zone_fsopt_opt));
new->zone_fsopt_next = NULL;
if (last == NULL)
tabptr->zone_fs_options = new;
else
last->zone_fsopt_next = new;
return (Z_OK);
}
int
zonecfg_remove_fs_option(struct zone_fstab *tabptr, char *option)
{
zone_fsopt_t *last, *this, *next;
last = tabptr->zone_fs_options;
for (this = last; this != NULL; this = this->zone_fsopt_next) {
if (strcmp(this->zone_fsopt_opt, option) == 0) {
next = this->zone_fsopt_next;
if (this == tabptr->zone_fs_options)
tabptr->zone_fs_options = next;
else
last->zone_fsopt_next = next;
free(this);
return (Z_OK);
} else
last = this;
}
return (Z_NO_PROPERTY_ID);
}
void
zonecfg_free_fs_option_list(zone_fsopt_t *list)
{
zone_fsopt_t *this, *next;
for (this = list; this != NULL; this = next) {
next = this->zone_fsopt_next;
free(this);
}
}
void
zonecfg_free_rctl_value_list(struct zone_rctlvaltab *valtab)
{
if (valtab == NULL)
return;
zonecfg_free_rctl_value_list(valtab->zone_rctlval_next);
free(valtab);
}
static boolean_t
match_prop(xmlNodePtr cur, const xmlChar *attr, char *user_prop)
{
xmlChar *gotten_prop;
int prop_result;
gotten_prop = xmlGetProp(cur, attr);
if (gotten_prop == NULL) /* shouldn't happen */
return (B_FALSE);
prop_result = xmlStrcmp(gotten_prop, (const xmlChar *) user_prop);
xmlFree(gotten_prop);
return ((prop_result == 0));
}
static int
zonecfg_delete_filesystem_core(zone_dochandle_t handle,
struct zone_fstab *tabptr)
{
xmlNodePtr cur = handle->zone_dh_cur;
boolean_t dir_match, spec_match, raw_match, type_match;
for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
if (xmlStrcmp(cur->name, DTD_ELEM_FS))
continue;
dir_match = match_prop(cur, DTD_ATTR_DIR, tabptr->zone_fs_dir);
spec_match = match_prop(cur, DTD_ATTR_SPECIAL,
tabptr->zone_fs_special);
raw_match = match_prop(cur, DTD_ATTR_RAW,
tabptr->zone_fs_raw);
type_match = match_prop(cur, DTD_ATTR_TYPE,
tabptr->zone_fs_type);
if (dir_match && spec_match && raw_match && type_match) {
xmlUnlinkNode(cur);
xmlFreeNode(cur);
return (Z_OK);
}
}
return (Z_NO_RESOURCE_ID);
}
int
zonecfg_delete_filesystem(zone_dochandle_t handle, struct zone_fstab *tabptr)
{
int err;
if (tabptr == NULL)
return (Z_INVAL);
if ((err = operation_prep(handle)) != Z_OK)
return (err);
if ((err = zonecfg_delete_filesystem_core(handle, tabptr)) != Z_OK)
return (err);
return (Z_OK);
}
int
zonecfg_modify_filesystem(
zone_dochandle_t handle,
struct zone_fstab *oldtabptr,
struct zone_fstab *newtabptr)
{
int err;
if (oldtabptr == NULL || newtabptr == NULL)
return (Z_INVAL);
if ((err = operation_prep(handle)) != Z_OK)
return (err);
if ((err = zonecfg_delete_filesystem_core(handle, oldtabptr)) != Z_OK)
return (err);
if ((err = zonecfg_add_filesystem_core(handle, newtabptr)) != Z_OK)
return (err);
return (Z_OK);
}
static int
zonecfg_delete_ipd_core(zone_dochandle_t handle, struct zone_fstab *tabptr)
{
xmlNodePtr cur = handle->zone_dh_cur;
for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
if (xmlStrcmp(cur->name, DTD_ELEM_IPD))
continue;
if (match_prop(cur, DTD_ATTR_DIR, tabptr->zone_fs_dir)) {
xmlUnlinkNode(cur);
xmlFreeNode(cur);
return (Z_OK);
}
}
return (Z_NO_RESOURCE_ID);
}
int
zonecfg_delete_ipd(zone_dochandle_t handle, struct zone_fstab *tabptr)
{
int err;
if (tabptr == NULL)
return (Z_INVAL);
if ((err = operation_prep(handle)) != Z_OK)
return (err);
if ((err = zonecfg_delete_ipd_core(handle, tabptr)) != Z_OK)
return (err);
return (Z_OK);
}
int
zonecfg_modify_ipd(zone_dochandle_t handle, struct zone_fstab *oldtabptr,
struct zone_fstab *newtabptr)
{
int err;
if (oldtabptr == NULL || newtabptr == NULL)
return (Z_INVAL);
if ((err = operation_prep(handle)) != Z_OK)
return (err);
if ((err = zonecfg_delete_ipd_core(handle, oldtabptr)) != Z_OK)
return (err);
if ((err = zonecfg_add_ipd_core(handle, newtabptr)) != Z_OK)
return (err);
return (Z_OK);
}
int
zonecfg_lookup_filesystem(
zone_dochandle_t handle,
struct zone_fstab *tabptr)
{
xmlNodePtr cur, options, firstmatch;
int err;
char dirname[MAXPATHLEN], special[MAXPATHLEN], raw[MAXPATHLEN];
char type[FSTYPSZ];
char options_str[MAX_MNTOPT_STR];
if (tabptr == NULL)
return (Z_INVAL);
if ((err = operation_prep(handle)) != Z_OK)
return (err);
/*
* Walk the list of children looking for matches on any properties
* specified in the fstab parameter. If more than one resource
* matches, we return Z_INSUFFICIENT_SPEC; if none match, we return
* Z_NO_RESOURCE_ID.
*/
cur = handle->zone_dh_cur;
firstmatch = NULL;
for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
if (xmlStrcmp(cur->name, DTD_ELEM_FS))
continue;
if (strlen(tabptr->zone_fs_dir) > 0) {
if ((fetchprop(cur, DTD_ATTR_DIR, dirname,
sizeof (dirname)) == Z_OK) &&
(strcmp(tabptr->zone_fs_dir, dirname) == 0)) {
if (firstmatch == NULL)
firstmatch = cur;
else
return (Z_INSUFFICIENT_SPEC);
}
}
if (strlen(tabptr->zone_fs_special) > 0) {
if ((fetchprop(cur, DTD_ATTR_SPECIAL, special,
sizeof (special)) == Z_OK)) {
if (strcmp(tabptr->zone_fs_special,
special) == 0) {
if (firstmatch == NULL)
firstmatch = cur;
else if (firstmatch != cur)
return (Z_INSUFFICIENT_SPEC);
} else {
/*
* If another property matched but this
* one doesn't then reset firstmatch.
*/
if (firstmatch == cur)
firstmatch = NULL;
}
}
}
if (strlen(tabptr->zone_fs_raw) > 0) {
if ((fetchprop(cur, DTD_ATTR_RAW, raw,
sizeof (raw)) == Z_OK)) {
if (strcmp(tabptr->zone_fs_raw, raw) == 0) {
if (firstmatch == NULL)
firstmatch = cur;
else if (firstmatch != cur)
return (Z_INSUFFICIENT_SPEC);
} else {
/*
* If another property matched but this
* one doesn't then reset firstmatch.
*/
if (firstmatch == cur)
firstmatch = NULL;
}
}
}
if (strlen(tabptr->zone_fs_type) > 0) {
if ((fetchprop(cur, DTD_ATTR_TYPE, type,
sizeof (type)) == Z_OK)) {
if (strcmp(tabptr->zone_fs_type, type) == 0) {
if (firstmatch == NULL)
firstmatch = cur;
else if (firstmatch != cur)
return (Z_INSUFFICIENT_SPEC);
} else {
/*
* If another property matched but this
* one doesn't then reset firstmatch.
*/
if (firstmatch == cur)
firstmatch = NULL;
}
}
}
}
if (firstmatch == NULL)
return (Z_NO_RESOURCE_ID);
cur = firstmatch;
if ((err = fetchprop(cur, DTD_ATTR_DIR, tabptr->zone_fs_dir,
sizeof (tabptr->zone_fs_dir))) != Z_OK)
return (err);
if ((err = fetchprop(cur, DTD_ATTR_SPECIAL, tabptr->zone_fs_special,
sizeof (tabptr->zone_fs_special))) != Z_OK)
return (err);
if ((err = fetchprop(cur, DTD_ATTR_RAW, tabptr->zone_fs_raw,
sizeof (tabptr->zone_fs_raw))) != Z_OK)
return (err);
if ((err = fetchprop(cur, DTD_ATTR_TYPE, tabptr->zone_fs_type,
sizeof (tabptr->zone_fs_type))) != Z_OK)
return (err);
/* options are optional */
tabptr->zone_fs_options = NULL;
for (options = cur->xmlChildrenNode; options != NULL;
options = options->next) {
if ((fetchprop(options, DTD_ATTR_NAME, options_str,
sizeof (options_str)) != Z_OK))
break;
if (zonecfg_add_fs_option(tabptr, options_str) != Z_OK)
break;
}
return (Z_OK);
}
int
zonecfg_lookup_ipd(zone_dochandle_t handle, struct zone_fstab *tabptr)
{
xmlNodePtr cur, match;
int err;
char dirname[MAXPATHLEN];
if (tabptr == NULL)
return (Z_INVAL);
if ((err = operation_prep(handle)) != Z_OK)
return (err);
/*
* General algorithm:
* Walk the list of children looking for matches on any properties
* specified in the fstab parameter. If more than one resource
* matches, we return Z_INSUFFICIENT_SPEC; if none match, we return
* Z_NO_RESOURCE_ID.
*/
cur = handle->zone_dh_cur;
match = NULL;
for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
if (xmlStrcmp(cur->name, DTD_ELEM_IPD))
continue;
if (strlen(tabptr->zone_fs_dir) > 0) {
if ((fetchprop(cur, DTD_ATTR_DIR, dirname,
sizeof (dirname)) == Z_OK) &&
(strcmp(tabptr->zone_fs_dir, dirname) == 0)) {
if (match == NULL)
match = cur;
else
return (Z_INSUFFICIENT_SPEC);
}
}
}
if (match == NULL)
return (Z_NO_RESOURCE_ID);
cur = match;
if ((err = fetchprop(cur, DTD_ATTR_DIR, tabptr->zone_fs_dir,
sizeof (tabptr->zone_fs_dir))) != Z_OK)
return (err);
return (Z_OK);
}
/*
* Compare two IP addresses in string form. Allow for the possibility that
* one might have "/<prefix-length>" at the end: allow a match on just the
* IP address (or host name) part.
*/
boolean_t
zonecfg_same_net_address(char *a1, char *a2)
{
char *slashp, *slashp1, *slashp2;
int result;
if (strcmp(a1, a2) == 0)
return (B_TRUE);
/*
* If neither has a slash or both do, they need to match to be
* considered the same, but they did not match above, so fail.
*/
slashp1 = strchr(a1, '/');
slashp2 = strchr(a2, '/');
if ((slashp1 == NULL && slashp2 == NULL) ||
(slashp1 != NULL && slashp2 != NULL))
return (B_FALSE);
/*
* Only one had a slash: pick that one, zero out the slash, compare
* the "address only" strings, restore the slash, and return the
* result of the comparison.
*/
slashp = (slashp1 == NULL) ? slashp2 : slashp1;
*slashp = '\0';
result = strcmp(a1, a2);
*slashp = '/';
return ((result == 0));
}
int
zonecfg_valid_net_address(char *address, struct lifreq *lifr)
{
struct sockaddr_in *sin4;
struct sockaddr_in6 *sin6;
struct addrinfo hints, *result;
char *slashp = strchr(address, '/');
bzero(lifr, sizeof (struct lifreq));
sin4 = (struct sockaddr_in *)&lifr->lifr_addr;
sin6 = (struct sockaddr_in6 *)&lifr->lifr_addr;
if (slashp != NULL)
*slashp = '\0';
if (inet_pton(AF_INET, address, &sin4->sin_addr) == 1) {
sin4->sin_family = AF_INET;
} else if (inet_pton(AF_INET6, address, &sin6->sin6_addr) == 1) {
if (slashp == NULL)
return (Z_IPV6_ADDR_PREFIX_LEN);
sin6->sin6_family = AF_INET6;
} else {
/* "address" may be a host name */
(void) memset(&hints, 0, sizeof (hints));
hints.ai_family = PF_INET;
if (getaddrinfo(address, NULL, &hints, &result) != 0)
return (Z_BOGUS_ADDRESS);
sin4->sin_family = result->ai_family;
(void) memcpy(&sin4->sin_addr,
/* LINTED E_BAD_PTR_CAST_ALIGN */
&((struct sockaddr_in *)result->ai_addr)->sin_addr,
sizeof (struct in_addr));
freeaddrinfo(result);
}
return (Z_OK);
}
boolean_t
zonecfg_ifname_exists(sa_family_t af, char *ifname)
{
struct lifreq lifr;
int so;
int save_errno;
(void) memset(&lifr, 0, sizeof (lifr));
(void) strlcpy(lifr.lifr_name, ifname, sizeof (lifr.lifr_name));
lifr.lifr_addr.ss_family = af;
if ((so = socket(af, SOCK_DGRAM, 0)) < 0) {
/* Odd - can't tell if the ifname exists */
return (B_FALSE);
}
if (ioctl(so, SIOCGLIFFLAGS, (caddr_t)&lifr) < 0) {
save_errno = errno;
(void) close(so);
errno = save_errno;
return (B_FALSE);
}
(void) close(so);
return (B_TRUE);
}
int
zonecfg_lookup_nwif(zone_dochandle_t handle, struct zone_nwiftab *tabptr)
{
xmlNodePtr cur, firstmatch;
int err;
char address[INET6_ADDRSTRLEN], physical[LIFNAMSIZ];
if (tabptr == NULL)
return (Z_INVAL);
if ((err = operation_prep(handle)) != Z_OK)
return (err);
cur = handle->zone_dh_cur;
firstmatch = NULL;
for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
if (xmlStrcmp(cur->name, DTD_ELEM_NET))
continue;
if (strlen(tabptr->zone_nwif_physical) > 0) {
if ((fetchprop(cur, DTD_ATTR_PHYSICAL, physical,
sizeof (physical)) == Z_OK) &&
(strcmp(tabptr->zone_nwif_physical,
physical) == 0)) {
if (firstmatch == NULL)
firstmatch = cur;
else
return (Z_INSUFFICIENT_SPEC);
}
}
if (strlen(tabptr->zone_nwif_address) > 0) {
if ((fetchprop(cur, DTD_ATTR_ADDRESS, address,
sizeof (address)) == Z_OK)) {
if (zonecfg_same_net_address(
tabptr->zone_nwif_address, address)) {
if (firstmatch == NULL)
firstmatch = cur;
else if (firstmatch != cur)
return (Z_INSUFFICIENT_SPEC);
} else {
/*
* If another property matched but this
* one doesn't then reset firstmatch.
*/
if (firstmatch == cur)
firstmatch = NULL;
}
}
}
}
if (firstmatch == NULL)
return (Z_NO_RESOURCE_ID);
cur = firstmatch;
if ((err = fetchprop(cur, DTD_ATTR_PHYSICAL, tabptr->zone_nwif_physical,
sizeof (tabptr->zone_nwif_physical))) != Z_OK)
return (err);
if ((err = fetchprop(cur, DTD_ATTR_ADDRESS, tabptr->zone_nwif_address,
sizeof (tabptr->zone_nwif_address))) != Z_OK)
return (err);
return (Z_OK);
}
static int
zonecfg_add_nwif_core(zone_dochandle_t handle, struct zone_nwiftab *tabptr)
{
xmlNodePtr newnode, cur = handle->zone_dh_cur;
int err;
newnode = xmlNewTextChild(cur, NULL, DTD_ELEM_NET, NULL);
if ((err = newprop(newnode, DTD_ATTR_ADDRESS,
tabptr->zone_nwif_address)) != Z_OK)
return (err);
if ((err = newprop(newnode, DTD_ATTR_PHYSICAL,
tabptr->zone_nwif_physical)) != Z_OK)
return (err);
return (Z_OK);
}
int
zonecfg_add_nwif(zone_dochandle_t handle, struct zone_nwiftab *tabptr)
{
int err;
if (tabptr == NULL)
return (Z_INVAL);
if ((err = operation_prep(handle)) != Z_OK)
return (err);
if ((err = zonecfg_add_nwif_core(handle, tabptr)) != Z_OK)
return (err);
return (Z_OK);
}
static int
zonecfg_delete_nwif_core(zone_dochandle_t handle, struct zone_nwiftab *tabptr)
{
xmlNodePtr cur = handle->zone_dh_cur;
boolean_t addr_match, phys_match;
for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
if (xmlStrcmp(cur->name, DTD_ELEM_NET))
continue;
addr_match = match_prop(cur, DTD_ATTR_ADDRESS,
tabptr->zone_nwif_address);
phys_match = match_prop(cur, DTD_ATTR_PHYSICAL,
tabptr->zone_nwif_physical);
if (addr_match && phys_match) {
xmlUnlinkNode(cur);
xmlFreeNode(cur);
return (Z_OK);
}
}
return (Z_NO_RESOURCE_ID);
}
int
zonecfg_delete_nwif(zone_dochandle_t handle, struct zone_nwiftab *tabptr)
{
int err;
if (tabptr == NULL)
return (Z_INVAL);
if ((err = operation_prep(handle)) != Z_OK)
return (err);
if ((err = zonecfg_delete_nwif_core(handle, tabptr)) != Z_OK)
return (err);
return (Z_OK);
}
int
zonecfg_modify_nwif(
zone_dochandle_t handle,
struct zone_nwiftab *oldtabptr,
struct zone_nwiftab *newtabptr)
{
int err;
if (oldtabptr == NULL || newtabptr == NULL)
return (Z_INVAL);
if ((err = operation_prep(handle)) != Z_OK)
return (err);
if ((err = zonecfg_delete_nwif_core(handle, oldtabptr)) != Z_OK)
return (err);
if ((err = zonecfg_add_nwif_core(handle, newtabptr)) != Z_OK)
return (err);
return (Z_OK);
}
int
zonecfg_lookup_dev(zone_dochandle_t handle, struct zone_devtab *tabptr)
{
xmlNodePtr cur, firstmatch;
int err;
char match[MAXPATHLEN];
if (tabptr == NULL)
return (Z_INVAL);
if ((err = operation_prep(handle)) != Z_OK)
return (err);
cur = handle->zone_dh_cur;
firstmatch = NULL;
for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
if (xmlStrcmp(cur->name, DTD_ELEM_DEVICE))
continue;
if (strlen(tabptr->zone_dev_match) == 0)
continue;
if ((fetchprop(cur, DTD_ATTR_MATCH, match,
sizeof (match)) == Z_OK)) {
if (strcmp(tabptr->zone_dev_match,
match) == 0) {
if (firstmatch == NULL)
firstmatch = cur;
else if (firstmatch != cur)
return (Z_INSUFFICIENT_SPEC);
} else {
/*
* If another property matched but this
* one doesn't then reset firstmatch.
*/
if (firstmatch == cur)
firstmatch = NULL;
}
}
}
if (firstmatch == NULL)
return (Z_NO_RESOURCE_ID);
cur = firstmatch;
if ((err = fetchprop(cur, DTD_ATTR_MATCH, tabptr->zone_dev_match,
sizeof (tabptr->zone_dev_match))) != Z_OK)
return (err);
return (Z_OK);
}
static int
zonecfg_add_dev_core(zone_dochandle_t handle, struct zone_devtab *tabptr)
{
xmlNodePtr newnode, cur = handle->zone_dh_cur;
int err;
newnode = xmlNewTextChild(cur, NULL, DTD_ELEM_DEVICE, NULL);
if ((err = newprop(newnode, DTD_ATTR_MATCH,
tabptr->zone_dev_match)) != Z_OK)
return (err);
return (Z_OK);
}
int
zonecfg_add_dev(zone_dochandle_t handle, struct zone_devtab *tabptr)
{
int err;
if (tabptr == NULL)
return (Z_INVAL);
if ((err = operation_prep(handle)) != Z_OK)
return (err);
if ((err = zonecfg_add_dev_core(handle, tabptr)) != Z_OK)
return (err);
return (Z_OK);
}
static int
zonecfg_delete_dev_core(zone_dochandle_t handle, struct zone_devtab *tabptr)
{
xmlNodePtr cur = handle->zone_dh_cur;
int match_match;
for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
if (xmlStrcmp(cur->name, DTD_ELEM_DEVICE))
continue;
match_match = match_prop(cur, DTD_ATTR_MATCH,
tabptr->zone_dev_match);
if (match_match) {
xmlUnlinkNode(cur);
xmlFreeNode(cur);
return (Z_OK);
}
}
return (Z_NO_RESOURCE_ID);
}
int
zonecfg_delete_dev(zone_dochandle_t handle, struct zone_devtab *tabptr)
{
int err;
if (tabptr == NULL)
return (Z_INVAL);
if ((err = operation_prep(handle)) != Z_OK)
return (err);
if ((err = zonecfg_delete_dev_core(handle, tabptr)) != Z_OK)
return (err);
return (Z_OK);
}
int
zonecfg_modify_dev(
zone_dochandle_t handle,
struct zone_devtab *oldtabptr,
struct zone_devtab *newtabptr)
{
int err;
if (oldtabptr == NULL || newtabptr == NULL)
return (Z_INVAL);
if ((err = operation_prep(handle)) != Z_OK)
return (err);
if ((err = zonecfg_delete_dev_core(handle, oldtabptr)) != Z_OK)
return (err);
if ((err = zonecfg_add_dev_core(handle, newtabptr)) != Z_OK)
return (err);
return (Z_OK);
}
/* Lock to serialize all zonecfg_devwalks */
static pthread_mutex_t zonecfg_devwalk_lock = PTHREAD_MUTEX_INITIALIZER;
/*
* Global variables used to pass data from zonecfg_devwalk to the nftw
* call-back (zonecfg_devwalk_cb). g_devwalk_data is really the void*
* parameter and g_devwalk_cb is really the *cb parameter from zonecfg_devwalk.
*/
static void *g_devwalk_data;
static int (*g_devwalk_cb)(const char *, uid_t, gid_t, mode_t, const char *,
void *);
static size_t g_devwalk_skip_prefix;
/*
* This is the nftw call-back function used by zonecfg_devwalk. It is
* responsible for calling the actual call-back that is passed in to
* zonecfg_devwalk as the *cb argument.
*/
/* ARGSUSED2 */
static int
zonecfg_devwalk_cb(const char *path, const struct stat *st, int f,
struct FTW *ftw)
{
acl_t *acl;
char *acl_txt = NULL;
/* skip all but character and block devices */
if (!S_ISBLK(st->st_mode) && !S_ISCHR(st->st_mode))
return (0);
if ((acl_get(path, ACL_NO_TRIVIAL, &acl) == 0) &&
acl != NULL) {
acl_txt = acl_totext(acl, ACL_NORESOLVE);
acl_free(acl);
}
if (strlen(path) <= g_devwalk_skip_prefix)
return (0);
g_devwalk_cb(path + g_devwalk_skip_prefix, st->st_uid, st->st_gid,
st->st_mode & S_IAMB, acl_txt != NULL ? acl_txt : "",
g_devwalk_data);
free(acl_txt);
return (0);
}
/*
* Walk the dev tree for the zone specified by hdl and call the call-back (cb)
* function for each entry in the tree. The call-back will be passed the
* name, uid, gid, mode, acl string and the void *data input parameter
* for each dev entry.
*
* Data is passed to the zonecfg_devwalk_cb through the global variables
* g_devwalk_data, *g_devwalk_cb, and g_devwalk_skip_prefix. The
* zonecfg_devwalk_cb function will actually call *cb.
*/
int
zonecfg_devwalk(zone_dochandle_t hdl,
int (*cb)(const char *, uid_t, gid_t, mode_t, const char *, void *),
void *data)
{
char path[MAXPATHLEN];
int ret;
if ((ret = zonecfg_get_zonepath(hdl, path, sizeof (path))) != Z_OK)
return (ret);
if (strlcat(path, "/dev", sizeof (path)) >= sizeof (path))
return (Z_TOO_BIG);
g_devwalk_skip_prefix = strlen(path) + 1;
/*
* We have to serialize all zonecfg_devwalks in the same process
* (which should be fine), since nftw() is so badly designed.
*/
(void) pthread_mutex_lock(&zonecfg_devwalk_lock);
g_devwalk_data = data;
g_devwalk_cb = cb;
(void) nftw(path, zonecfg_devwalk_cb, 0, FTW_PHYS);
(void) pthread_mutex_unlock(&zonecfg_devwalk_lock);
return (Z_OK);
}
/*
* Update the owner, group, mode and acl on the specified dev (inpath) for
* the zone (hdl). This function can be used to fix up the dev tree after
* attaching a migrated zone.
*/
int
zonecfg_devperms_apply(zone_dochandle_t hdl, const char *inpath, uid_t owner,
gid_t group, mode_t mode, const char *acltxt)
{
int ret;
char path[MAXPATHLEN];
struct stat st;
acl_t *aclp;
if ((ret = zonecfg_get_zonepath(hdl, path, sizeof (path))) != Z_OK)
return (ret);
if (strlcat(path, "/dev/", sizeof (path)) >= sizeof (path))
return (Z_TOO_BIG);
if (strlcat(path, inpath, sizeof (path)) >= sizeof (path))
return (Z_TOO_BIG);
if (stat(path, &st) == -1)
return (Z_INVAL);
/* make sure we're only touching device nodes */
if (!S_ISCHR(st.st_mode) && !S_ISBLK(st.st_mode))
return (Z_INVAL);
if (chown(path, owner, group) == -1)
return (Z_SYSTEM);
if (chmod(path, mode) == -1)
return (Z_SYSTEM);
if ((acltxt == NULL) || (strcmp(acltxt, "") == 0))
return (Z_OK);
if (acl_fromtext(acltxt, &aclp) != 0)
return (Z_SYSTEM);
errno = 0;
if (acl_set(path, aclp) == -1) {
free(aclp);
return (Z_SYSTEM);
}
free(aclp);
return (Z_OK);
}
/*
* This function finds everything mounted under a zone's rootpath.
* This returns the number of mounts under rootpath, or -1 on error.
* callback is called once per mount found with the first argument
* pointing to the mount point.
*
* If the callback function returns non-zero zonecfg_find_mounts
* aborts with an error.
*/
int
zonecfg_find_mounts(char *rootpath, int (*callback)(const char *, void *),
void *priv) {
FILE *mnttab;
struct mnttab m;
size_t l;
int zfsl;
int rv = 0;
char zfs_path[MAXPATHLEN];
assert(rootpath != NULL);
if ((zfsl = snprintf(zfs_path, sizeof (zfs_path), "%s/.zfs/", rootpath))
>= sizeof (zfs_path))
return (-1);
l = strlen(rootpath);
mnttab = fopen("/etc/mnttab", "r");
if (mnttab == NULL)
return (-1);
if (ioctl(fileno(mnttab), MNTIOC_SHOWHIDDEN, NULL) < 0) {
rv = -1;
goto out;
}
while (!getmntent(mnttab, &m)) {
if ((strncmp(rootpath, m.mnt_mountp, l) == 0) &&
(m.mnt_mountp[l] == '/') &&
(strncmp(zfs_path, m.mnt_mountp, zfsl) != 0)) {
rv++;
if (callback == NULL)
continue;
if (callback(m.mnt_mountp, priv)) {
rv = -1;
goto out;
}
}
}
out:
(void) fclose(mnttab);
return (rv);
}
int
zonecfg_lookup_attr(zone_dochandle_t handle, struct zone_attrtab *tabptr)
{
xmlNodePtr cur, firstmatch;
int err;
char name[MAXNAMELEN], type[MAXNAMELEN], value[MAXNAMELEN];
if (tabptr == NULL)
return (Z_INVAL);
if ((err = operation_prep(handle)) != Z_OK)
return (err);
cur = handle->zone_dh_cur;
firstmatch = NULL;
for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
if (xmlStrcmp(cur->name, DTD_ELEM_ATTR))
continue;
if (strlen(tabptr->zone_attr_name) > 0) {
if ((fetchprop(cur, DTD_ATTR_NAME, name,
sizeof (name)) == Z_OK) &&
(strcmp(tabptr->zone_attr_name, name) == 0)) {
if (firstmatch == NULL)
firstmatch = cur;
else
return (Z_INSUFFICIENT_SPEC);
}
}
if (strlen(tabptr->zone_attr_type) > 0) {
if ((fetchprop(cur, DTD_ATTR_TYPE, type,
sizeof (type)) == Z_OK)) {
if (strcmp(tabptr->zone_attr_type, type) == 0) {
if (firstmatch == NULL)
firstmatch = cur;
else if (firstmatch != cur)
return (Z_INSUFFICIENT_SPEC);
} else {
/*
* If another property matched but this
* one doesn't then reset firstmatch.
*/
if (firstmatch == cur)
firstmatch = NULL;
}
}
}
if (strlen(tabptr->zone_attr_value) > 0) {
if ((fetchprop(cur, DTD_ATTR_VALUE, value,
sizeof (value)) == Z_OK)) {
if (strcmp(tabptr->zone_attr_value, value) ==
0) {
if (firstmatch == NULL)
firstmatch = cur;
else if (firstmatch != cur)
return (Z_INSUFFICIENT_SPEC);
} else {
/*
* If another property matched but this
* one doesn't then reset firstmatch.
*/
if (firstmatch == cur)
firstmatch = NULL;
}
}
}
}
if (firstmatch == NULL)
return (Z_NO_RESOURCE_ID);
cur = firstmatch;
if ((err = fetchprop(cur, DTD_ATTR_NAME, tabptr->zone_attr_name,
sizeof (tabptr->zone_attr_name))) != Z_OK)
return (err);
if ((err = fetchprop(cur, DTD_ATTR_TYPE, tabptr->zone_attr_type,
sizeof (tabptr->zone_attr_type))) != Z_OK)
return (err);
if ((err = fetchprop(cur, DTD_ATTR_VALUE, tabptr->zone_attr_value,
sizeof (tabptr->zone_attr_value))) != Z_OK)
return (err);
return (Z_OK);
}
static int
zonecfg_add_attr_core(zone_dochandle_t handle, struct zone_attrtab *tabptr)
{
xmlNodePtr newnode, cur = handle->zone_dh_cur;
int err;
newnode = xmlNewTextChild(cur, NULL, DTD_ELEM_ATTR, NULL);
err = newprop(newnode, DTD_ATTR_NAME