2N/A/*
2N/A * CDDL HEADER START
2N/A *
2N/A * The contents of this file are subject to the terms of the
2N/A * Common Development and Distribution License (the "License").
2N/A * You may not use this file except in compliance with the License.
2N/A *
2N/A * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
2N/A * or http://www.opensolaris.org/os/licensing.
2N/A * See the License for the specific language governing permissions
2N/A * and limitations under the License.
2N/A *
2N/A * When distributing Covered Code, include this CDDL HEADER in each
2N/A * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
2N/A * If applicable, add the following below this CDDL HEADER, with the
2N/A * fields enclosed by brackets "[]" replaced with your own identifying
2N/A * information: Portions Copyright [yyyy] [name of copyright owner]
2N/A *
2N/A * CDDL HEADER END
2N/A */
2N/A
2N/A/*
2N/A * Copyright (c) 2004, 2012, Oracle and/or its affiliates. All rights reserved.
2N/A */
2N/A
2N/A
2N/A/*
2N/A * Module: zones.c
2N/A * Group: libinstzones
2N/A * Description: Provide "zones" interface for libbe. No new consumers shall
2N/A * be added - rather they should use zoneadm(1M) or libzonecfg
2N/A * interfaces.
2N/A *
2N/A * Public Methods:
2N/A * z_free_zone_list - free contents of zoneList_t object
2N/A * z_get_nonglobal_zone_list_by_brand - return zoneList_t object describing
2N/A * all non-global zones matching the list of zone brands passed in.
2N/A * z_free_brand_list - free contents of a zoneBrandList_t object
2N/A * z_make_brand_list - return a zoneBrandList_t object describing the list
2N/A * of all zone brands passed in.
2N/A * z_get_zonename - return the name of the current zone
2N/A * z_global_only - Determine if the global zone is only zone on the spec list
2N/A * z_lock_this_zone - lock this zone
2N/A * z_running_in_global_zone - Determine if running in the "global" zone
2N/A * z_set_output_functions - Link program specific output functions
2N/A * z_set_zone_root - Set root for zones library operations
2N/A * z_set_zone_spec - Set list of zones on which actions will be performed
2N/A * z_unlock_this_zone - unlock this zone
2N/A * z_verify_zone_spec - Verify list of zones on which actions will be performed
2N/A * z_zlist_get_current_state - Determine the current kernel state of the
2N/A * specified zone
2N/A * z_zlist_get_zonename - Determine name of specified zone
2N/A * z_zones_are_implemented - Determine if any zone operations can be performed
2N/A * z_is_zone_brand_in_list - determine if the zone's brand matches the
2N/A * brand list passed in.
2N/A * z_brands_are_implemented - determine if branded zones are implemented on
2N/A * this system
2N/A */
2N/A
2N/A/*
2N/A * System includes
2N/A */
2N/A
2N/A#include <stdio.h>
2N/A#include <stdlib.h>
2N/A#include <unistd.h>
2N/A#include <fcntl.h>
2N/A#include <ctype.h>
2N/A#include <sys/types.h>
2N/A#include <sys/param.h>
2N/A#include <sys/sysmacros.h>
2N/A#include <string.h>
2N/A#include <strings.h>
2N/A#include <sys/stat.h>
2N/A#include <stdarg.h>
2N/A#include <limits.h>
2N/A#include <errno.h>
2N/A#include <time.h>
2N/A#include <signal.h>
2N/A#include <stropts.h>
2N/A#include <wait.h>
2N/A#include <zone.h>
2N/A#include <sys/brand.h>
2N/A#include <libintl.h>
2N/A#include <locale.h>
2N/A#include <libzonecfg.h>
2N/A#include <libcontract.h>
2N/A#include <sys/contract/process.h>
2N/A#include <sys/ctfs.h>
2N/A#include <assert.h>
2N/A#include <dlfcn.h>
2N/A#include <link.h>
2N/A#include <time.h>
2N/A
2N/A/*
2N/A * local includes
2N/A */
2N/A
2N/A/*
2N/A * When _INSTZONES_LIB_Z_DEFINE_GLOBAL_DATA is defined,
2N/A * instzones_lib.h will define the z_global_data structure.
2N/A * Otherwise an extern to the structure is inserted.
2N/A */
2N/A
2N/A#define _INSTZONES_LIB_Z_DEFINE_GLOBAL_DATA
2N/A#include "instzones_lib.h"
2N/A#include "zones_strings.h"
2N/A
2N/A/*
2N/A * Private structures
2N/A */
2N/A
2N/A#define CLUSTER_BRAND_NAME "cluster"
2N/A
2N/A/* maximum number of arguments to exec() call */
2N/A
2N/A#define UUID_FORMAT "%02d%02d%02d%03d-%02d%02d%02d%d-%016llx"
2N/A
2N/A/*
2N/A * Library Function Prototypes
2N/A */
2N/A
2N/A#define streq(a, b) (strcmp((a), (b)) == 0)
2N/A
2N/A/*
2N/A * Local Function Prototypes
2N/A */
2N/A
2N/A/*
2N/A * global internal (private) declarations
2N/A */
2N/A
2N/A/*
2N/A * *****************************************************************************
2N/A * global external (public) functions
2N/A * *****************************************************************************
2N/A */
2N/A
2N/A/*
2N/A * Name: z_brands_are_implemented
2N/A * Description: Determine if any branded zones may be present
2N/A * Arguments: void
2N/A * Returns: boolean_t
2N/A * == B_TRUE - branded zones are supported
2N/A * == B_FALSE - branded zones are not supported
2N/A */
2N/A
2N/Aboolean_t
2N/Az_brands_are_implemented(void)
2N/A{
2N/Astatic boolean_t _brandsImplementedDetermined = B_FALSE;
2N/Astatic boolean_t _brandsAreImplemented = B_FALSE;
2N/A
2N/A /* if availability has not been determined, cache it now */
2N/A
2N/A if (!_brandsImplementedDetermined) {
2N/A _brandsImplementedDetermined = B_TRUE;
2N/A _brandsAreImplemented = _z_brands_are_implemented();
2N/A if (_brandsAreImplemented) {
2N/A _z_echoDebug(DBG_BRANDS_ARE_IMPLEMENTED);
2N/A } else {
2N/A _z_echoDebug(DBG_BRANDS_NOT_IMPLEMENTED);
2N/A }
2N/A }
2N/A
2N/A /* return cached answer */
2N/A
2N/A return (_brandsAreImplemented);
2N/A}
2N/A
2N/A/*
2N/A * Name: z_free_zone_list
2N/A * Description: free contents of zoneList_t object
2N/A * Arguments: a_zlst - handle to zoneList_t object to free
2N/A * Returns: void
2N/A */
2N/A
2N/Avoid
2N/Az_free_zone_list(zoneList_t a_zlst)
2N/A{
2N/A int numzones;
2N/A
2N/A /* ignore empty list */
2N/A
2N/A if (a_zlst == (zoneList_t)NULL) {
2N/A return;
2N/A }
2N/A
2N/A /* free each entry in the zone list */
2N/A
2N/A for (numzones = 0; a_zlst[numzones]._zlName != (char *)NULL;
2N/A numzones++) {
2N/A zoneListElement_t *zelm = &a_zlst[numzones];
2N/A
2N/A /* free zone name string */
2N/A
2N/A free(zelm->_zlName);
2N/A
2N/A /* free zonepath string */
2N/A
2N/A if (zelm->_zlPath != (char *)NULL) {
2N/A free(zelm->_zlPath);
2N/A }
2N/A
2N/A }
2N/A
2N/A /* free handle to the list */
2N/A
2N/A free(a_zlst);
2N/A}
2N/A
2N/A/*
2N/A * Name: z_free_brand_list
2N/A * Description: Free contents of zoneBrandList_t object
2N/A * Arguments: brands - pointer to zoneBrandList_t object to free
2N/A * Returns: void
2N/A */
2N/Avoid
2N/Az_free_brand_list(zoneBrandList_t *brands)
2N/A{
2N/A while (brands != NULL) {
2N/A zoneBrandList_t *temp = brands;
2N/A free(brands->string_ptr);
2N/A brands = brands->next;
2N/A free(temp);
2N/A }
2N/A}
2N/A
2N/A/*
2N/A * Name: z_make_brand_list
2N/A * Description: Given a string with a list of brand name delimited by
2N/A * the delimeter passed in, build a zoneBrandList_t structure
2N/A * with the list of brand names and return it to the caller.
2N/A * Arguments:
2N/A * brands - const char pointer to string list of brand names
2N/A * delim - const char pointer to string representing the
2N/A * delimeter for brands string.
2N/A * Returns: zoneBrandList_t *
2N/A * == NULL - error, list could not be generated
2N/A * != NULL - success, list returned
2N/A * NOTE: Any zoneBrandList_t returned is placed in new storage for the
2N/A * calling function. The caller must use 'z_free_brand_list' to
2N/A * dispose of the storage once the list is no longer needed.
2N/A */
2N/AzoneBrandList_t *
2N/Az_make_brand_list(const char *brands, const char *delim)
2N/A{
2N/A zoneBrandList_t *brand = NULL, *head = NULL;
2N/A char *blist = NULL;
2N/A char *str = NULL;
2N/A
2N/A if ((blist = strdup(brands)) == NULL)
2N/A return (NULL);
2N/A
2N/A if ((str = strtok(blist, delim)) != NULL) {
2N/A if ((brand = (zoneBrandList_t *)
2N/A malloc(sizeof (struct _zoneBrandList))) == NULL) {
2N/A return (NULL);
2N/A }
2N/A
2N/A head = brand;
2N/A brand->string_ptr = strdup(str);
2N/A brand->next = NULL;
2N/A
2N/A while ((str = strtok(NULL, delim)) != NULL) {
2N/A if ((brand->next = (zoneBrandList_t *)
2N/A malloc(sizeof (struct _zoneBrandList))) == NULL) {
2N/A return (NULL);
2N/A }
2N/A
2N/A brand = brand->next;
2N/A brand->string_ptr = strdup(str);
2N/A brand->next = NULL;
2N/A }
2N/A }
2N/A
2N/A free(blist);
2N/A return (head);
2N/A}
2N/A
2N/A/*
2N/A * Name: z_get_nonglobal_zone_list_by_brand
2N/A * Description: return zoneList_t object describing all non-global
2N/A * zones matching the list of brands passed in.
2N/A * Arguments: brands - The list of zone brands to look for.
2N/A * Returns: zoneList_t
2N/A * == NULL - error, list could not be generated
2N/A * != NULL - success, list returned
2N/A * NOTE: Any zoneList_t returned is placed in new storage for the
2N/A * calling function. The caller must use 'z_free_zone_list' to
2N/A * dispose of the storage once the list is no longer needed.
2N/A */
2N/AzoneList_t
2N/Az_get_nonglobal_zone_list_by_brand(zoneBrandList_t *brands)
2N/A{
2N/A FILE *zoneIndexFP;
2N/A int numzones = 0;
2N/A struct zoneent *ze;
2N/A zoneList_t zlst = NULL;
2N/A FILE *mapFP;
2N/A char zonename[ZONENAME_MAX];
2N/A zone_spec_t *zent;
2N/A
2N/A /* if zones are not implemented, return empty list */
2N/A
2N/A if (!z_zones_are_implemented()) {
2N/A return ((zoneList_t)NULL);
2N/A }
2N/A
2N/A /*
2N/A * Open the zone index file. Note that getzoneent_private() handles
2N/A * NULL.
2N/A */
2N/A zoneIndexFP = setzoneent();
2N/A
2N/A mapFP = zonecfg_open_scratch("", B_FALSE);
2N/A
2N/A /* index file open; scan all zones; see if any are at least installed */
2N/A
2N/A while ((ze = getzoneent_private(zoneIndexFP)) != NULL) {
2N/A
2N/A /* skip the global zone */
2N/A
2N/A if (strcmp(ze->zone_name, GLOBAL_ZONENAME) == 0) {
2N/A free(ze);
2N/A continue;
2N/A }
2N/A
2N/A /*
2N/A * skip any zones with brands not on the brand list
2N/A */
2N/A if (!z_is_zone_brand_in_list(ze->zone_name, brands)) {
2N/A free(ze);
2N/A continue;
2N/A }
2N/A
2N/A /*
2N/A * If the user specified an explicit zone list, then ignore any
2N/A * zones that aren't on that list.
2N/A */
2N/A if ((zent = _z_global_data._zone_spec) != NULL) {
2N/A while (zent != NULL) {
2N/A if (strcmp(zent->zl_name, ze->zone_name) == 0)
2N/A break;
2N/A zent = zent->zl_next;
2N/A }
2N/A if (zent == NULL) {
2N/A free(ze);
2N/A continue;
2N/A }
2N/A }
2N/A
2N/A /* non-global zone: create entry for this zone */
2N/A
2N/A if (numzones == 0) {
2N/A zlst = (zoneList_t)_z_calloc(
2N/A sizeof (zoneListElement_t)*2);
2N/A } else {
2N/A zlst = (zoneList_t)_z_realloc(zlst,
2N/A sizeof (zoneListElement_t)*(numzones+2));
2N/A (void) memset(&zlst[numzones], 0L,
2N/A sizeof (zoneListElement_t)*2);
2N/A }
2N/A
2N/A /*
2N/A * remember the zone name, zonepath and the current
2N/A * zone state of the zone.
2N/A */
2N/A zlst[numzones]._zlName = _z_strdup(ze->zone_name);
2N/A zlst[numzones]._zlPath = _z_strdup(ze->zone_path);
2N/A zlst[numzones]._zlState = ze->zone_state;
2N/A
2N/A /*
2N/A * For a scratch zone, we need to know the kernel zone name.
2N/A */
2N/A if (zonecfg_in_alt_root() && mapFP != NULL &&
2N/A zonecfg_find_scratch(mapFP, ze->zone_name,
2N/A zonecfg_get_root(), zonename, sizeof (zonename)) != -1) {
2N/A free(zlst[numzones]._zlScratchName);
2N/A zlst[numzones]._zlScratchName = _z_strdup(zonename);
2N/A }
2N/A
2N/A numzones++;
2N/A free(ze);
2N/A }
2N/A
2N/A /* close the index file */
2N/A endzoneent(zoneIndexFP);
2N/A
2N/A if (mapFP != NULL)
2N/A zonecfg_close_scratch(mapFP);
2N/A
2N/A /* return generated list */
2N/A
2N/A return (zlst);
2N/A}
2N/A
2N/A/*
2N/A * Name: z_get_zonename
2N/A * Description: return the name of the current zone
2N/A * Arguments: void
2N/A * Returns: char *
2N/A * - pointer to string representing the name of the current
2N/A * zone
2N/A * NOTE: Any string returned is placed in new storage for the
2N/A * calling function. The caller must use 'Free' to dispose
2N/A * of the storage once the string is no longer needed.
2N/A */
2N/A
2N/Achar *
2N/Az_get_zonename(void)
2N/A{
2N/A ssize_t zonenameLen;
2N/A char zonename[ZONENAME_MAX];
2N/A zoneid_t zoneid = (zoneid_t)-1;
2N/A
2N/A /* if zones are not implemented, return "" */
2N/A
2N/A if (!z_zones_are_implemented()) {
2N/A return (_z_strdup(""));
2N/A }
2N/A
2N/A /* get the zone i.d. of the current zone */
2N/A
2N/A zoneid = getzoneid();
2N/A
2N/A /* get the name of the current zone */
2N/A
2N/A zonenameLen = getzonenamebyid(zoneid, zonename, sizeof (zonename));
2N/A
2N/A /* return "" if could not get zonename */
2N/A
2N/A if (zonenameLen < 1) {
2N/A return (_z_strdup(""));
2N/A }
2N/A
2N/A return (_z_strdup(zonename));
2N/A}
2N/A
2N/A/*
2N/A * Name: z_global_only
2N/A * Description: Determine if the global zone is only zone on the spec list.
2N/A * Arguments: None
2N/A * Returns: B_TRUE if global zone is the only zone on the list,
2N/A * B_FALSE otherwise.
2N/A */
2N/A
2N/Aboolean_t
2N/Az_global_only(void)
2N/A{
2N/A /* return true if zones are not implemented - treate as global zone */
2N/A
2N/A if (!z_zones_are_implemented()) {
2N/A return (B_TRUE);
2N/A }
2N/A
2N/A /* return true if this is the global zone */
2N/A
2N/A if (_z_global_data._zone_spec != NULL &&
2N/A _z_global_data._zone_spec->zl_next == NULL &&
2N/A strcmp(_z_global_data._zone_spec->zl_name, GLOBAL_ZONENAME) == 0) {
2N/A return (B_TRUE);
2N/A }
2N/A
2N/A /* return false - not the global zone */
2N/A
2N/A return (B_FALSE);
2N/A}
2N/A
2N/A/*
2N/A * Name: z_lock_this_zone
2N/A * Description: lock this zone
2N/A * Arguments: a_lflags - [RO, *RO] - (ZLOCKS_T)
2N/A * Flags indicating which locks to acquire
2N/A * Returns: boolean_t
2N/A * == B_TRUE - success specified locks acquired
2N/A * == B_FALSE - failure specified locks not acquired
2N/A * NOTE: the lock objects for "this zone" are maintained internally.
2N/A */
2N/A
2N/Aboolean_t
2N/Az_lock_this_zone(ZLOCKS_T a_lflags)
2N/A{
2N/A boolean_t b;
2N/A char *zoneName;
2N/A pid_t pid = (pid_t)0;
2N/A
2N/A /* entry assertions */
2N/A
2N/A assert(a_lflags != ZLOCKS_NONE);
2N/A
2N/A /* entry debugging info */
2N/A
2N/A _z_echoDebug(DBG_ZONES_LCK_THIS, a_lflags);
2N/A
2N/A zoneName = z_get_zonename();
2N/A pid = getpid();
2N/A
2N/A /* lock package administration always */
2N/A
2N/A if (a_lflags & ZLOCKS_PKG_ADMIN) {
2N/A b = _z_lock_zone_object(&_z_global_data._z_ObjectLocks,
2N/A zoneName, LOBJ_PKGADMIN, pid,
2N/A MSG_ZONES_LCK_THIS_PKGADM,
2N/A ERR_ZONES_LCK_THIS_PKGADM);
2N/A if (!b) {
2N/A (void) z_unlock_this_zone(a_lflags);
2N/A (void) free(zoneName);
2N/A return (B_FALSE);
2N/A }
2N/A }
2N/A
2N/A (void) free(zoneName);
2N/A
2N/A return (B_TRUE);
2N/A}
2N/A
2N/A/*
2N/A * Name: z_running_in_global_zone
2N/A * Description: Determine if running in the "global" zone
2N/A * Arguments: void
2N/A * Returns: boolean_t
2N/A * == B_TRUE - running in global zone
2N/A * == B_FALSE - not running in global zone
2N/A */
2N/A
2N/Aboolean_t
2N/Az_running_in_global_zone(void)
2N/A{
2N/A static boolean_t _zoneIdDetermined = B_FALSE;
2N/A static boolean_t _zoneIsGlobal = B_FALSE;
2N/A
2N/A /* if ID has not been determined, cache it now */
2N/A
2N/A if (!_zoneIdDetermined) {
2N/A _zoneIdDetermined = B_TRUE;
2N/A _zoneIsGlobal = _z_running_in_global_zone();
2N/A }
2N/A
2N/A return (_zoneIsGlobal);
2N/A}
2N/A
2N/A/*
2N/A * Name: z_set_output_functions
2N/A * Description: Link program specific output functions to this library.
2N/A * Arguments: a_echo_fcn - (_z_printf_fcn_t)
2N/A * Function to call to cause "normal operation" messages
2N/A * to be output/displayed
2N/A * a_echo_debug_fcn - (_z_printf_fcn_t)
2N/A * Function to call to cause "debugging" messages
2N/A * to be output/displayed
2N/A * a_progerr_fcn - (_z_printf_fcn_t)
2N/A * Function to call to cause "program error" messages
2N/A * to be output/displayed
2N/A * Returns: void
2N/A * NOTE: If NULL is specified for any function, then the functionality
2N/A * associated with that function is disabled.
2N/A * NOTE: The function pointers provided must call a function that
2N/A * takes two arguments:
2N/A * function(char *format, char *message)
2N/A * Any registered function will be called like:
2N/A * function("%s", "message")
2N/A */
2N/A
2N/Avoid
2N/Az_set_output_functions(_z_printf_fcn_t a_echo_fcn,
2N/A _z_printf_fcn_t a_echo_debug_fcn,
2N/A _z_printf_fcn_t a_progerr_fcn)
2N/A{
2N/A _z_global_data._z_echo = a_echo_fcn;
2N/A _z_global_data._z_echo_debug = a_echo_debug_fcn;
2N/A _z_global_data._z_progerr = a_progerr_fcn;
2N/A}
2N/A
2N/A/*
2N/A * Name: z_set_zone_root
2N/A * Description: Set root for zones library operations
2N/A * Arguments: Path to root of boot environment containing zone; must be
2N/A * absolute.
2N/A * Returns: None.
2N/A * NOTE: Must be called before performing any zone-related operations.
2N/A * (Currently called directly by set_inst_root() during -R
2N/A * argument handling.)
2N/A */
2N/A
2N/Avoid
2N/Az_set_zone_root(const char *zroot)
2N/A{
2N/A char *rootdir;
2N/A
2N/A /* if zones are not implemented, just return */
2N/A
2N/A if (!z_zones_are_implemented())
2N/A return;
2N/A
2N/A /* entry assertions */
2N/A
2N/A assert(zroot != NULL);
2N/A
2N/A rootdir = _z_strdup((char *)zroot);
2N/A z_canoninplace(rootdir);
2N/A
2N/A if (strcmp(rootdir, "/") == 0) {
2N/A rootdir[0] = '\0';
2N/A }
2N/A
2N/A /* free any existing cached root path */
2N/A if (*_z_global_data._z_root_dir != '\0') {
2N/A free(_z_global_data._z_root_dir);
2N/A _z_global_data._z_root_dir = NULL;
2N/A }
2N/A
2N/A /* store duplicate of new zone root path */
2N/A
2N/A if (*rootdir != '\0') {
2N/A _z_global_data._z_root_dir = _z_strdup(rootdir);
2N/A } else {
2N/A _z_global_data._z_root_dir = "";
2N/A }
2N/A
2N/A /* set zone root path */
2N/A
2N/A zonecfg_set_root(rootdir);
2N/A
2N/A free(rootdir);
2N/A}
2N/A
2N/A/*
2N/A * Name: z_set_zone_spec
2N/A * Description: Set list of zones on which actions will be performed.
2N/A * Arguments: Whitespace-separated list of zone names.
2N/A * Returns: 0 on success, -1 on error.
2N/A * NOTES: Will call _z_program_error if argument can't be parsed or
2N/A * memory not available.
2N/A */
2N/Aint
2N/Az_set_zone_spec(const char *zlist)
2N/A{
2N/A const char *zend;
2N/A ptrdiff_t zlen;
2N/A zone_spec_t *zent;
2N/A zone_spec_t *zhead;
2N/A zone_spec_t **znextp = &zhead;
2N/A
2N/A /* entry assertions */
2N/A
2N/A assert(zlist != NULL);
2N/A
2N/A /* parse list to zone_spec_t list, store in global data */
2N/A
2N/A for (;;) {
2N/A while (isspace(*zlist)) {
2N/A zlist++;
2N/A }
2N/A if (*zlist == '\0') {
2N/A break;
2N/A }
2N/A for (zend = zlist; *zend != '\0'; zend++) {
2N/A if (isspace(*zend)) {
2N/A break;
2N/A }
2N/A }
2N/A zlen = ((ptrdiff_t)zend) - ((ptrdiff_t)zlist);
2N/A if (zlen >= ZONENAME_MAX) {
2N/A _z_program_error(ERR_ZONE_NAME_ILLEGAL, zlen, zlist);
2N/A return (-1);
2N/A }
2N/A zent = _z_malloc(sizeof (*zent));
2N/A (void) memcpy(zent->zl_name, zlist, zlen);
2N/A zent->zl_name[zlen] = '\0';
2N/A zent->zl_used = B_FALSE;
2N/A *znextp = zent;
2N/A znextp = &zent->zl_next;
2N/A zlist = zend;
2N/A }
2N/A *znextp = NULL;
2N/A
2N/A if (zhead == NULL) {
2N/A _z_program_error(ERR_ZONE_LIST_EMPTY);
2N/A return (-1);
2N/A }
2N/A
2N/A _z_global_data._zone_spec = zhead;
2N/A return (0);
2N/A}
2N/A
2N/A/*
2N/A * Name: z_unlock_this_zone
2N/A * Description: unlock this zone
2N/A * Arguments: a_lflags - [RO, *RO] - (ZLOCKS_T)
2N/A * Flags indicating which locks to release
2N/A * Returns: boolean_t
2N/A * == B_TRUE - success specified locks released
2N/A * == B_FALSE - failure specified locks may not be released
2N/A * NOTE: the lock objects for "this zone" are maintained internally.
2N/A */
2N/A
2N/Aboolean_t
2N/Az_unlock_this_zone(ZLOCKS_T a_lflags)
2N/A{
2N/A boolean_t b;
2N/A boolean_t errors = B_FALSE;
2N/A char *zoneName;
2N/A
2N/A /* entry assertions */
2N/A
2N/A assert(a_lflags != ZLOCKS_NONE);
2N/A
2N/A /* entry debugging info */
2N/A
2N/A _z_echoDebug(DBG_ZONES_ULK_THIS, a_lflags);
2N/A
2N/A /* return if no objects locked */
2N/A
2N/A if ((_z_global_data._z_ObjectLocks == (char *)NULL) ||
2N/A (*_z_global_data._z_ObjectLocks == '\0')) {
2N/A return (B_TRUE);
2N/A }
2N/A
2N/A zoneName = z_get_zonename();
2N/A
2N/A /* unlock package administration */
2N/A
2N/A if (a_lflags & ZLOCKS_PKG_ADMIN) {
2N/A b = _z_unlock_zone_object(&_z_global_data._z_ObjectLocks,
2N/A zoneName, LOBJ_PKGADMIN, ERR_ZONES_ULK_THIS_PACKAGE);
2N/A if (!b) {
2N/A errors = B_TRUE;
2N/A }
2N/A }
2N/A
2N/A (void) free(zoneName);
2N/A return (!errors);
2N/A}
2N/A
2N/A/*
2N/A * Name: z_verify_zone_spec
2N/A * Description: Verify list of zones on which actions will be performed.
2N/A * Arguments: None.
2N/A * Returns: 0 on success, -1 on error.
2N/A * NOTES: Will call _z_program_error if there are zones on the specified
2N/A * list that don't exist on the system. Requires that
2N/A * z_set_zone_root is called first (if it is called at all).
2N/A */
2N/Aint
2N/Az_verify_zone_spec(void)
2N/A{
2N/A FILE *zoneIndexFP;
2N/A boolean_t errors;
2N/A char zoneIndexPath[MAXPATHLEN];
2N/A struct zoneent *ze;
2N/A zone_spec_t *zent;
2N/A
2N/A if (!z_zones_are_implemented()) {
2N/A _z_program_error(ERR_ZONES_NOT_IMPLEMENTED);
2N/A return (-1);
2N/A }
2N/A
2N/A zoneIndexFP = setzoneent();
2N/A if (zoneIndexFP == NULL) {
2N/A _z_program_error(ERR_ZONEINDEX_OPEN, zoneIndexPath,
2N/A strerror(errno));
2N/A return (-1);
2N/A }
2N/A
2N/A while ((ze = getzoneent_private(zoneIndexFP)) != NULL) {
2N/A for (zent = _z_global_data._zone_spec;
2N/A zent != NULL; zent = zent->zl_next) {
2N/A if (strcmp(zent->zl_name, ze->zone_name) == 0) {
2N/A zent->zl_used = B_TRUE;
2N/A break;
2N/A }
2N/A }
2N/A free(ze);
2N/A }
2N/A endzoneent(zoneIndexFP);
2N/A
2N/A errors = B_FALSE;
2N/A for (zent = _z_global_data._zone_spec;
2N/A zent != NULL; zent = zent->zl_next) {
2N/A if (!zent->zl_used) {
2N/A _z_program_error(ERR_ZONE_NONEXISTENT, zent->zl_name);
2N/A errors = B_TRUE;
2N/A }
2N/A }
2N/A return (errors ? -1 : 0);
2N/A}
2N/A
2N/A/*
2N/A * Name: z_is_zone_brand_in_list
2N/A * Description: Determine whether zone's brand has a match in the list
2N/A * brands passed in.
2N/A * Arguments: zoneName - name of the zone to check for branding
2N/A * list - list of brands to check the zone against
2N/A * Returns: boolean_t
2N/A * == B_TRUE - zone has a matching brand
2N/A * == B_FALSE - zone brand is not in list
2N/A */
2N/Aboolean_t
2N/Az_is_zone_brand_in_list(char *zoneName, zoneBrandList_t *list)
2N/A{
2N/A char brandname[MAXNAMELEN];
2N/A int err;
2N/A zoneBrandList_t *sp;
2N/A
2N/A if (zoneName == NULL || list == NULL)
2N/A return (B_FALSE);
2N/A
2N/A /* if zones are not implemented, return FALSE */
2N/A if (!z_zones_are_implemented()) {
2N/A return (B_FALSE);
2N/A }
2N/A
2N/A /* if brands are not implemented, return FALSE */
2N/A if (!z_brands_are_implemented()) {
2N/A return (B_FALSE);
2N/A }
2N/A
2N/A err = zone_get_brand(zoneName, brandname, sizeof (brandname));
2N/A if (err != Z_OK) {
2N/A _z_program_error(ERR_BRAND_GETBRAND, zonecfg_strerror(err));
2N/A return (B_FALSE);
2N/A }
2N/A
2N/A for (sp = list; sp != NULL; sp = sp->next) {
2N/A if (sp->string_ptr != NULL &&
2N/A strcmp(sp->string_ptr, brandname) == 0) {
2N/A return (B_TRUE);
2N/A }
2N/A }
2N/A
2N/A return (B_FALSE);
2N/A}
2N/A
2N/A/*
2N/A * Name: z_zlist_get_current_state
2N/A * Description: Determine the current kernel state of the specified zone
2N/A * Arguments: a_zlst - handle to zoneList_t object describing all zones
2N/A * a_zoneIndex - index into a_zlst of the zone to return
2N/A * Returns: zone_state_t
2N/A * The current state of the specified zone is returned
2N/A */
2N/A
2N/Azone_state_t
2N/Az_zlist_get_current_state(zoneList_t a_zlst, int a_zoneIndex)
2N/A{
2N/A int i;
2N/A
2N/A /* ignore empty list */
2N/A
2N/A if (a_zlst == (zoneList_t)NULL) {
2N/A return (ZONE_STATE_INCOMPLETE);
2N/A }
2N/A
2N/A /* find the specified zone in the list */
2N/A
2N/A for (i = 0; (i != a_zoneIndex) &&
2N/A (a_zlst[i]._zlName != (char *)NULL); i++)
2N/A ;
2N/A
2N/A /* return error if the specified zone does not exist */
2N/A
2N/A if (a_zlst[i]._zlName == (char *)NULL) {
2N/A return (ZONE_STATE_INCOMPLETE);
2N/A }
2N/A
2N/A /* return selected zone's current kernel state */
2N/A
2N/A _z_echoDebug(DBG_ZONES_GET_ZONE_STATE,
2N/A a_zlst[i]._zlName ? a_zlst[i]._zlName : "",
2N/A a_zlst[i]._zlState);
2N/A
2N/A return (a_zlst[i]._zlState);
2N/A}
2N/A
2N/A/*
2N/A * Name: z_zlist_get_zonename
2N/A * Description: Determine name of specified zone
2N/A * Arguments: a_zlst - handle to zoneList_t object describing all zones
2N/A * a_zoneIndex - index into a_zlst of the zone to return the
2N/A * Return: char *
2N/A * == NULL - zone name could not be determined
2N/A * != NULL - pointer to string representing zone name
2N/A * NOTE: Any zoneList_t returned is placed in static storage that must
2N/A * NEVER be free()ed by the caller.
2N/A */
2N/A
2N/Achar *
2N/Az_zlist_get_zonename(zoneList_t a_zlst, int a_zoneIndex)
2N/A{
2N/A int i;
2N/A
2N/A /* ignore empty list */
2N/A
2N/A if (a_zlst == (zoneList_t)NULL) {
2N/A return ((char *)NULL);
2N/A }
2N/A
2N/A /* find the specified zone in the list */
2N/A
2N/A for (i = 0; (i != a_zoneIndex) &&
2N/A (a_zlst[i]._zlName != (char *)NULL); i++)
2N/A ;
2N/A
2N/A /* return error if the specified zone does not exist */
2N/A
2N/A if (a_zlst[i]._zlName == (char *)NULL) {
2N/A return (NULL);
2N/A }
2N/A
2N/A /* return selected zone's name */
2N/A
2N/A return (a_zlst[i]._zlName);
2N/A}
2N/A
2N/A/*
2N/A * Name: z_zlist_get_zonepath
2N/A * Description: Determine zonepath of specified zone
2N/A * Arguments: a_zlst - handle to zoneList_t object describing all zones
2N/A * a_zoneIndex - index into a_zlst of the zone to return
2N/A * Return: char *
2N/A * == NULL - zonepath could not be determined
2N/A * != NULL - pointer to string representing zonepath
2N/A * NOTE: Any zoneList_t returned is placed in static storage that must
2N/A * NEVER be free()ed by the caller.
2N/A */
2N/A
2N/Achar *
2N/Az_zlist_get_zonepath(zoneList_t a_zlst, int a_zoneIndex)
2N/A{
2N/A int i;
2N/A
2N/A /* ignore empty list */
2N/A
2N/A if (a_zlst == (zoneList_t)NULL) {
2N/A return ((char *)NULL);
2N/A }
2N/A
2N/A /* find the specified zone in the list */
2N/A
2N/A for (i = 0; (i != a_zoneIndex) &&
2N/A (a_zlst[i]._zlName != (char *)NULL); i++)
2N/A ;
2N/A
2N/A /* return error if the specified zone does not exist */
2N/A
2N/A if (a_zlst[i]._zlName == (char *)NULL) {
2N/A return (NULL);
2N/A }
2N/A
2N/A /* return selected zone's zonepath */
2N/A
2N/A return (a_zlst[i]._zlPath);
2N/A}
2N/A
2N/A/*
2N/A * Name: z_zones_are_implemented
2N/A * Description: Determine if any zone operations can be performed
2N/A * Arguments: void
2N/A * Returns: boolean_t
2N/A * == B_TRUE - zone operations are available
2N/A * == B_FALSE - no zone operations can be done
2N/A */
2N/A
2N/Aboolean_t
2N/Az_zones_are_implemented(void)
2N/A{
2N/A static boolean_t _zonesImplementedDetermined = B_FALSE;
2N/A static boolean_t _zonesAreImplemented = B_FALSE;
2N/A
2N/A /* if availability has not been determined, cache it now */
2N/A
2N/A if (!_zonesImplementedDetermined) {
2N/A _zonesImplementedDetermined = B_TRUE;
2N/A _zonesAreImplemented = _z_zones_are_implemented();
2N/A if (!_zonesAreImplemented) {
2N/A _z_echoDebug(DBG_ZONES_NOT_IMPLEMENTED);
2N/A } else {
2N/A _z_echoDebug(DBG_ZONES_ARE_IMPLEMENTED);
2N/A }
2N/A }
2N/A
2N/A return (_zonesAreImplemented);
2N/A}