15c51f124SMoriah Waterland /*
25c51f124SMoriah Waterland  * CDDL HEADER START
35c51f124SMoriah Waterland  *
45c51f124SMoriah Waterland  * The contents of this file are subject to the terms of the
55c51f124SMoriah Waterland  * Common Development and Distribution License (the "License").
65c51f124SMoriah Waterland  * You may not use this file except in compliance with the License.
75c51f124SMoriah Waterland  *
85c51f124SMoriah Waterland  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
95c51f124SMoriah Waterland  * or http://www.opensolaris.org/os/licensing.
105c51f124SMoriah Waterland  * See the License for the specific language governing permissions
115c51f124SMoriah Waterland  * and limitations under the License.
125c51f124SMoriah Waterland  *
135c51f124SMoriah Waterland  * When distributing Covered Code, include this CDDL HEADER in each
145c51f124SMoriah Waterland  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
155c51f124SMoriah Waterland  * If applicable, add the following below this CDDL HEADER, with the
165c51f124SMoriah Waterland  * fields enclosed by brackets "[]" replaced with your own identifying
175c51f124SMoriah Waterland  * information: Portions Copyright [yyyy] [name of copyright owner]
185c51f124SMoriah Waterland  *
195c51f124SMoriah Waterland  * CDDL HEADER END
205c51f124SMoriah Waterland  */
215c51f124SMoriah Waterland 
225c51f124SMoriah Waterland /*
236e1ae2a3SGary Pennington  * Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
245c51f124SMoriah Waterland  */
255c51f124SMoriah Waterland 
265c51f124SMoriah Waterland 
275c51f124SMoriah Waterland /*
285c51f124SMoriah Waterland  * Module:	zones.c
295c51f124SMoriah Waterland  * Group:	libinstzones
305c51f124SMoriah Waterland  * Description:	Provide "zones" interface for install consolidation code
315c51f124SMoriah Waterland  *
325c51f124SMoriah Waterland  * Public Methods:
335c51f124SMoriah Waterland  *  z_create_zone_admin_file - Given a location to create the file, and
345c51f124SMoriah Waterland  *	optionally an existing administration file, generate an
355c51f124SMoriah Waterland  *	administration file that can be used to perform "non-interactive"
365c51f124SMoriah Waterland  *	operations in a non-global zone.
375c51f124SMoriah Waterland  *  z_free_zone_list - free contents of zoneList_t object
385c51f124SMoriah Waterland  *  z_get_nonglobal_zone_list - return zoneList_t object describing all
395c51f124SMoriah Waterland  *	non-global native zones
405c51f124SMoriah Waterland  *  z_get_nonglobal_zone_list_by_brand - return zoneList_t object describing
415c51f124SMoriah Waterland  *      all non-global zones matching the list of zone brands passed in.
425c51f124SMoriah Waterland  *  z_free_brand_list - free contents of a zoneBrandList_t object
435c51f124SMoriah Waterland  *  z_make_brand_list - return a zoneBrandList_t object describing the list
445c51f124SMoriah Waterland  *	of all zone brands passed in.
455c51f124SMoriah Waterland  *  z_get_zonename - return the name of the current zone
465c51f124SMoriah Waterland  *  z_global_only - Determine if the global zone is only zone on the spec list
475c51f124SMoriah Waterland  *  z_lock_this_zone - lock this zone
485c51f124SMoriah Waterland  *  z_lock_zones - lock specified zones
495c51f124SMoriah Waterland  *  z_mount_in_lz - Mount global zone directory in specified zone's root file
505c51f124SMoriah Waterland  *	system
515c51f124SMoriah Waterland  *  z_non_global_zones_exist - Determine if any non-global native zones exist
525c51f124SMoriah Waterland  *  z_on_zone_spec - Determine if named zone is on the zone_spec list
535c51f124SMoriah Waterland  *  z_running_in_global_zone - Determine if running in the "global" zone
545c51f124SMoriah Waterland  *  z_set_output_functions - Link program specific output functions
555c51f124SMoriah Waterland  *  z_set_zone_root - Set root for zones library operations
565c51f124SMoriah Waterland  *  z_set_zone_spec - Set list of zones on which actions will be performed
575c51f124SMoriah Waterland  *  z_umount_lz_mount - Unmount directory mounted with z_mount_in_lz
585c51f124SMoriah Waterland  *  z_unlock_this_zone - unlock this zone
595c51f124SMoriah Waterland  *  z_unlock_zones - unlock specified zones
605c51f124SMoriah Waterland  *  z_verify_zone_spec - Verify list of zones on which actions will be performed
615c51f124SMoriah Waterland  *  z_zlist_change_zone_state - Change the current state of the specified zone
625c51f124SMoriah Waterland  *  z_zlist_get_current_state - Determine the current kernel state of the
635c51f124SMoriah Waterland  *	specified zone
645c51f124SMoriah Waterland  *  z_zlist_get_original_state - Return the original kernal state of the
655c51f124SMoriah Waterland  *	specified zone
665c51f124SMoriah Waterland  *  z_zlist_get_scratch - Determine name of scratch zone
675c51f124SMoriah Waterland  *  z_zlist_get_zonename - Determine name of specified zone
685c51f124SMoriah Waterland  *  z_zlist_get_zonepath - Determine zonepath of specified zone
695c51f124SMoriah Waterland  *  z_zlist_restore_zone_state - Return the zone to the state it was originally
705c51f124SMoriah Waterland  *	in
715c51f124SMoriah Waterland  *  z_zone_exec - Execute a Unix command in a specified zone and return results
725c51f124SMoriah Waterland  *  z_zones_are_implemented - Determine if any zone operations can be performed
735c51f124SMoriah Waterland  *  z_is_zone_branded - determine if zone has a non-native brand
745c51f124SMoriah Waterland  *  z_is_zone_brand_in_list - determine if the zone's brand matches the
755c51f124SMoriah Waterland  *      brand list passed in.
765c51f124SMoriah Waterland  *  z_brands_are_implemented - determine if branded zones are implemented on
775c51f124SMoriah Waterland  *			this system
785c51f124SMoriah Waterland  */
795c51f124SMoriah Waterland 
805c51f124SMoriah Waterland /*
815c51f124SMoriah Waterland  * System includes
825c51f124SMoriah Waterland  */
835c51f124SMoriah Waterland 
845c51f124SMoriah Waterland #include <stdio.h>
855c51f124SMoriah Waterland #include <stdlib.h>
865c51f124SMoriah Waterland #include <unistd.h>
875c51f124SMoriah Waterland #include <fcntl.h>
885c51f124SMoriah Waterland #include <ctype.h>
895c51f124SMoriah Waterland #include <sys/types.h>
905c51f124SMoriah Waterland #include <sys/param.h>
915c51f124SMoriah Waterland #include <sys/sysmacros.h>
925c51f124SMoriah Waterland #include <string.h>
935c51f124SMoriah Waterland #include <strings.h>
945c51f124SMoriah Waterland #include <sys/stat.h>
955c51f124SMoriah Waterland #include <stdarg.h>
965c51f124SMoriah Waterland #include <limits.h>
975c51f124SMoriah Waterland #include <errno.h>
985c51f124SMoriah Waterland #include <time.h>
995c51f124SMoriah Waterland #include <signal.h>
1005c51f124SMoriah Waterland #include <stropts.h>
1015c51f124SMoriah Waterland #include <wait.h>
1025c51f124SMoriah Waterland #include <zone.h>
1035c51f124SMoriah Waterland #include <sys/brand.h>
1045c51f124SMoriah Waterland #include <libintl.h>
1055c51f124SMoriah Waterland #include <locale.h>
1065c51f124SMoriah Waterland #include <libzonecfg.h>
1075c51f124SMoriah Waterland #include <libcontract.h>
1085c51f124SMoriah Waterland #include <sys/contract/process.h>
1095c51f124SMoriah Waterland #include <sys/ctfs.h>
1105c51f124SMoriah Waterland #include <assert.h>
1115c51f124SMoriah Waterland #include <dlfcn.h>
1125c51f124SMoriah Waterland #include <link.h>
1135c51f124SMoriah Waterland #include <time.h>
1145c51f124SMoriah Waterland 
1155c51f124SMoriah Waterland /*
1165c51f124SMoriah Waterland  * local includes
1175c51f124SMoriah Waterland  */
1185c51f124SMoriah Waterland 
1195c51f124SMoriah Waterland /*
1205c51f124SMoriah Waterland  * When _INSTZONES_LIB_Z_DEFINE_GLOBAL_DATA is defined,
1215c51f124SMoriah Waterland  * instzones_lib.h will define the z_global_data structure.
1225c51f124SMoriah Waterland  * Otherwise an extern to the structure is inserted.
1235c51f124SMoriah Waterland  */
1245c51f124SMoriah Waterland 
1255c51f124SMoriah Waterland #define	_INSTZONES_LIB_Z_DEFINE_GLOBAL_DATA
1265c51f124SMoriah Waterland #include "instzones_lib.h"
1275c51f124SMoriah Waterland #include "zones_strings.h"
1285c51f124SMoriah Waterland 
1295c51f124SMoriah Waterland /*
1305c51f124SMoriah Waterland  * Private structures
1315c51f124SMoriah Waterland  */
1325c51f124SMoriah Waterland 
1335c51f124SMoriah Waterland #define	CLUSTER_BRAND_NAME	"cluster"
1345c51f124SMoriah Waterland 
1355c51f124SMoriah Waterland /* maximum number of arguments to exec() call */
1365c51f124SMoriah Waterland 
1375c51f124SMoriah Waterland #define	UUID_FORMAT	"%02d%02d%02d%03d-%02d%02d%02d%d-%016llx"
1385c51f124SMoriah Waterland 
1395c51f124SMoriah Waterland /*
1405c51f124SMoriah Waterland  * Library Function Prototypes
1415c51f124SMoriah Waterland  */
1425c51f124SMoriah Waterland 
1435c51f124SMoriah Waterland #define	streq(a, b) (strcmp((a), (b)) == 0)
1445c51f124SMoriah Waterland 
1455c51f124SMoriah Waterland /*
1465c51f124SMoriah Waterland  * Local Function Prototypes
1475c51f124SMoriah Waterland  */
1485c51f124SMoriah Waterland 
1495c51f124SMoriah Waterland /*
1505c51f124SMoriah Waterland  * global internal (private) declarations
1515c51f124SMoriah Waterland  */
1525c51f124SMoriah Waterland 
1535c51f124SMoriah Waterland /*
1545c51f124SMoriah Waterland  * *****************************************************************************
1555c51f124SMoriah Waterland  * global external (public) functions
1565c51f124SMoriah Waterland  * *****************************************************************************
1575c51f124SMoriah Waterland  */
1585c51f124SMoriah Waterland 
1595c51f124SMoriah Waterland /*
1605c51f124SMoriah Waterland  * Name:	z_create_zone_admin_file
1615c51f124SMoriah Waterland  * Description:	Given a location to create the file, and optionally an existing
1625c51f124SMoriah Waterland  *		administration file, generate an administration file that
1635c51f124SMoriah Waterland  *		can be used to perform "non-interactive" operations in a
1645c51f124SMoriah Waterland  *		non-global zone.
1655c51f124SMoriah Waterland  * Arguments:	a_zoneAdminFilename - pointer to string representing the
1665c51f124SMoriah Waterland  *			full path of zone admin file to create
1675c51f124SMoriah Waterland  *		a_userAdminFilename - pointer to string representing the path
1685c51f124SMoriah Waterland  *			to an existing "user" administration file - the
1695c51f124SMoriah Waterland  *			administration file created will contain the
1705c51f124SMoriah Waterland  *			settings contained in this file, modified as
1715c51f124SMoriah Waterland  *			appropriate to supress any interaction;
1725c51f124SMoriah Waterland  *			If this is == NULL then the administration file
1735c51f124SMoriah Waterland  *			created will not contain any extra settings
1745c51f124SMoriah Waterland  * Returns:	boolean_t
1755c51f124SMoriah Waterland  *			== B_TRUE - admin file created
1765c51f124SMoriah Waterland  *			== B_FALSE - failed to create admin file
1775c51f124SMoriah Waterland  */
1785c51f124SMoriah Waterland 
1795c51f124SMoriah Waterland boolean_t
z_create_zone_admin_file(char * a_zoneAdminFilename,char * a_userAdminFilename)1805c51f124SMoriah Waterland z_create_zone_admin_file(char *a_zoneAdminFilename, char *a_userAdminFilename)
1815c51f124SMoriah Waterland {
1825c51f124SMoriah Waterland 	FILE	*zFp;
1835c51f124SMoriah Waterland 	FILE	*uFp = (FILE *)NULL;
1845c51f124SMoriah Waterland 
1855c51f124SMoriah Waterland 	/* entry assertions */
1865c51f124SMoriah Waterland 
1875c51f124SMoriah Waterland 	assert(a_zoneAdminFilename != NULL);
1885c51f124SMoriah Waterland 	assert(*a_zoneAdminFilename != '\0');
1895c51f124SMoriah Waterland 
1905c51f124SMoriah Waterland 	/* create temporary zone admin file */
1915c51f124SMoriah Waterland 
1925c51f124SMoriah Waterland 	zFp = fopen(a_zoneAdminFilename, "w");
1935c51f124SMoriah Waterland 	if (zFp == (FILE *)NULL) {
1945c51f124SMoriah Waterland 		return (B_FALSE);
1955c51f124SMoriah Waterland 	}
1965c51f124SMoriah Waterland 
1975c51f124SMoriah Waterland 	/* open user admin file if specified */
1985c51f124SMoriah Waterland 
1995c51f124SMoriah Waterland 	if (a_userAdminFilename != (char *)NULL) {
2005c51f124SMoriah Waterland 		uFp = fopen(a_userAdminFilename, "r");
2015c51f124SMoriah Waterland 	}
2025c51f124SMoriah Waterland 
2035c51f124SMoriah Waterland 	/* create default admin file for zone pkg ops if no user admin file */
2045c51f124SMoriah Waterland 
2055c51f124SMoriah Waterland 	if (uFp == (FILE *)NULL) {
2065c51f124SMoriah Waterland 		/* create default admin file */
2075c51f124SMoriah Waterland 		(void) fprintf(zFp, "action=nocheck\nauthentication=nocheck\n"
2085c51f124SMoriah Waterland 		    "basedir=default\nconflict=nocheck\nidepend=nocheck\n"
2095c51f124SMoriah Waterland 		    "instance=unique\npartial=nocheck\nrdepend=nocheck\n"
2105c51f124SMoriah Waterland 		    "runlevel=nocheck\nsetuid=nocheck\nspace=nocheck\n"
2115c51f124SMoriah Waterland 		    "mail=\n");
2125c51f124SMoriah Waterland 	} else for (;;) {
2135c51f124SMoriah Waterland 		/* copy user admin file substitute/change appropriate entries */
2145c51f124SMoriah Waterland 		char	buf[LINE_MAX+1];
2155c51f124SMoriah Waterland 		char	*p;
2165c51f124SMoriah Waterland 
2175c51f124SMoriah Waterland 		/* read next line of user admin file */
2185c51f124SMoriah Waterland 
2195c51f124SMoriah Waterland 		p = fgets(buf, sizeof (buf), uFp);
2205c51f124SMoriah Waterland 		if (p == (char *)NULL) {
2215c51f124SMoriah Waterland 			(void) fclose(uFp);
2225c51f124SMoriah Waterland 			break;
2235c51f124SMoriah Waterland 		}
2245c51f124SMoriah Waterland 
2255c51f124SMoriah Waterland 		/* modify / replace / accept as appropriate */
2265c51f124SMoriah Waterland 
2275c51f124SMoriah Waterland 		if (strncmp(buf, "instance=quit", 13) == 0) {
2285c51f124SMoriah Waterland 			(void) fprintf(zFp, "%s", "instance=unique\n");
2295c51f124SMoriah Waterland 			/*LINTED*/
2305c51f124SMoriah Waterland 		} else if (strncmp(buf, "keystore=", 9) == 0) {
2315c51f124SMoriah Waterland 		} else if (strncmp(buf, "action=", 7) == 0) {
2325c51f124SMoriah Waterland 			(void) fprintf(zFp, "action=nocheck\n");
2335c51f124SMoriah Waterland 		} else if (strncmp(buf, "authentication=", 15) == 0) {
2345c51f124SMoriah Waterland 			(void) fprintf(zFp, "authentication=nocheck\n");
2355c51f124SMoriah Waterland 		} else if (strncmp(buf, "conflict=", 9) == 0) {
2365c51f124SMoriah Waterland 			(void) fprintf(zFp, "conflict=nocheck\n");
2375c51f124SMoriah Waterland 		} else if (strncmp(buf, "idepend=", 8) == 0) {
2385c51f124SMoriah Waterland 			(void) fprintf(zFp, "idepend=nocheck\n");
2395c51f124SMoriah Waterland 		} else if (strncmp(buf, "mail=", 5) == 0) {
2405c51f124SMoriah Waterland 			(void) fprintf(zFp, "mail=\n");
2415c51f124SMoriah Waterland 		} else if (strncmp(buf, "partial=", 8) == 0) {
2425c51f124SMoriah Waterland 			(void) fprintf(zFp, "partial=nocheck\n");
2435c51f124SMoriah Waterland 		} else if (strncmp(buf, "rdepend=", 8) == 0) {
2445c51f124SMoriah Waterland 			(void) fprintf(zFp, "rdepend=nocheck\n");
2455c51f124SMoriah Waterland 		} else if (strncmp(buf, "runlevel=", 9) == 0) {
2465c51f124SMoriah Waterland 			(void) fprintf(zFp, "runlevel=nocheck\n");
2475c51f124SMoriah Waterland 		} else if (strncmp(buf, "setuid=", 7) == 0) {
2485c51f124SMoriah Waterland 			(void) fprintf(zFp, "setuid=nocheck\n");
2495c51f124SMoriah Waterland 		} else if (strncmp(buf, "space=", 6) == 0) {
2505c51f124SMoriah Waterland 			(void) fprintf(zFp, "space=nocheck\n");
2515c51f124SMoriah Waterland 		} else {
2525c51f124SMoriah Waterland 			(void) fprintf(zFp, "%s", buf);
2535c51f124SMoriah Waterland 		}
2545c51f124SMoriah Waterland 	}
2555c51f124SMoriah Waterland 
2565c51f124SMoriah Waterland 	/* close admin file and return success */
2575c51f124SMoriah Waterland 
2585c51f124SMoriah Waterland 	(void) fclose(zFp);
2595c51f124SMoriah Waterland 	return (B_TRUE);
2605c51f124SMoriah Waterland }
2615c51f124SMoriah Waterland 
2625c51f124SMoriah Waterland /*
2635c51f124SMoriah Waterland  * Name:	z_brands_are_implemented
2645c51f124SMoriah Waterland  * Description:	Determine if any branded zones may be present
2655c51f124SMoriah Waterland  * Arguments:	void
2665c51f124SMoriah Waterland  * Returns:	boolean_t
2675c51f124SMoriah Waterland  *			== B_TRUE - branded zones are supported
2685c51f124SMoriah Waterland  *			== B_FALSE - branded zones are not supported
2695c51f124SMoriah Waterland  */
2705c51f124SMoriah Waterland 
2715c51f124SMoriah Waterland boolean_t
z_brands_are_implemented(void)2725c51f124SMoriah Waterland z_brands_are_implemented(void)
2735c51f124SMoriah Waterland {
2745c51f124SMoriah Waterland static	boolean_t	_brandsImplementedDetermined = B_FALSE;
2755c51f124SMoriah Waterland static	boolean_t	_brandsAreImplemented = B_FALSE;
2765c51f124SMoriah Waterland 
2775c51f124SMoriah Waterland 	/* if availability has not been determined, cache it now */
2785c51f124SMoriah Waterland 
2795c51f124SMoriah Waterland 	if (!_brandsImplementedDetermined) {
2805c51f124SMoriah Waterland 		_brandsImplementedDetermined = B_TRUE;
2815c51f124SMoriah Waterland 		_brandsAreImplemented = _z_brands_are_implemented();
2825c51f124SMoriah Waterland 		if (_brandsAreImplemented) {
2835c51f124SMoriah Waterland 			_z_echoDebug(DBG_BRANDS_ARE_IMPLEMENTED);
2845c51f124SMoriah Waterland 		} else {
2855c51f124SMoriah Waterland 			_z_echoDebug(DBG_BRANDS_NOT_IMPLEMENTED);
2865c51f124SMoriah Waterland 		}
2875c51f124SMoriah Waterland 	}
2885c51f124SMoriah Waterland 
2895c51f124SMoriah Waterland 	/* return cached answer */
2905c51f124SMoriah Waterland 
2915c51f124SMoriah Waterland 	return (_brandsAreImplemented);
2925c51f124SMoriah Waterland }
2935c51f124SMoriah Waterland 
2945c51f124SMoriah Waterland /*
2955c51f124SMoriah Waterland  * Name:	z_free_zone_list
2965c51f124SMoriah Waterland  * Description:	free contents of zoneList_t object
2975c51f124SMoriah Waterland  * Arguments:	a_zlst - handle to zoneList_t object to free
2985c51f124SMoriah Waterland  * Returns:	void
2995c51f124SMoriah Waterland  */
3005c51f124SMoriah Waterland 
3015c51f124SMoriah Waterland void
z_free_zone_list(zoneList_t a_zlst)3025c51f124SMoriah Waterland z_free_zone_list(zoneList_t a_zlst)
3035c51f124SMoriah Waterland {
3045c51f124SMoriah Waterland 	int	numzones;
3055c51f124SMoriah Waterland 
3065c51f124SMoriah Waterland 	/* ignore empty list */
3075c51f124SMoriah Waterland 
3085c51f124SMoriah Waterland 	if (a_zlst == (zoneList_t)NULL) {
3095c51f124SMoriah Waterland 		return;
3105c51f124SMoriah Waterland 	}
3115c51f124SMoriah Waterland 
3125c51f124SMoriah Waterland 	/* free each entry in the zone list */
3135c51f124SMoriah Waterland 
3145c51f124SMoriah Waterland 	for (numzones = 0; a_zlst[numzones]._zlName != (char *)NULL;
3155c51f124SMoriah Waterland 	    numzones++) {
3165c51f124SMoriah Waterland 		zoneListElement_t *zelm = &a_zlst[numzones];
3175c51f124SMoriah Waterland 
3185c51f124SMoriah Waterland 		/* free zone name string */
3195c51f124SMoriah Waterland 
3205c51f124SMoriah Waterland 		free(zelm->_zlName);
3215c51f124SMoriah Waterland 
3225c51f124SMoriah Waterland 		/* free zonepath string */
3235c51f124SMoriah Waterland 
3245c51f124SMoriah Waterland 		if (zelm->_zlPath != (char *)NULL) {
3255c51f124SMoriah Waterland 			free(zelm->_zlPath);
3265c51f124SMoriah Waterland 		}
3275c51f124SMoriah Waterland 
3285c51f124SMoriah Waterland 	}
3295c51f124SMoriah Waterland 
3305c51f124SMoriah Waterland 	/* free handle to the list */
3315c51f124SMoriah Waterland 
3325c51f124SMoriah Waterland 	free(a_zlst);
3335c51f124SMoriah Waterland }
3345c51f124SMoriah Waterland 
3355c51f124SMoriah Waterland /*
3365c51f124SMoriah Waterland  * Name:	z_get_nonglobal_zone_list
3375c51f124SMoriah Waterland  * Description: return zoneList_t object describing all non-global
3385c51f124SMoriah Waterland  *              native zones - branded zones are not included in list
3395c51f124SMoriah Waterland  * Arguments:	None.
3405c51f124SMoriah Waterland  * Returns:	zoneList_t
3415c51f124SMoriah Waterland  *			== NULL - error, list could not be generated
3425c51f124SMoriah Waterland  *			!= NULL - success, list returned
3435c51f124SMoriah Waterland  * NOTE:    	Any zoneList_t returned is placed in new storage for the
3445c51f124SMoriah Waterland  *		calling function. The caller must use 'z_free_zone_list' to
3455c51f124SMoriah Waterland  *		dispose of the storage once the list is no longer needed.
3465c51f124SMoriah Waterland  */
3475c51f124SMoriah Waterland 
3485c51f124SMoriah Waterland zoneList_t
z_get_nonglobal_zone_list(void)3495c51f124SMoriah Waterland z_get_nonglobal_zone_list(void)
3505c51f124SMoriah Waterland {
3515c51f124SMoriah Waterland 	zoneList_t zones;
3525c51f124SMoriah Waterland 	zoneBrandList_t *brands = NULL;
3535c51f124SMoriah Waterland 
3545c51f124SMoriah Waterland 	if ((brands = z_make_brand_list("native cluster", " ")) == NULL)
3555c51f124SMoriah Waterland 		return (NULL);
3565c51f124SMoriah Waterland 
3575c51f124SMoriah Waterland 	zones = z_get_nonglobal_zone_list_by_brand(brands);
3585c51f124SMoriah Waterland 
3595c51f124SMoriah Waterland 	z_free_brand_list(brands);
3605c51f124SMoriah Waterland 
3615c51f124SMoriah Waterland 	return (zones);
3625c51f124SMoriah Waterland }
3635c51f124SMoriah Waterland 
3645c51f124SMoriah Waterland /*
3655c51f124SMoriah Waterland  * Name:	z_free_brand_list
3665c51f124SMoriah Waterland  * Description: Free contents of zoneBrandList_t object
3675c51f124SMoriah Waterland  * Arguments:	brands - pointer to zoneBrandList_t object to free
3685c51f124SMoriah Waterland  * Returns: 	void
3695c51f124SMoriah Waterland  */
3705c51f124SMoriah Waterland void
z_free_brand_list(zoneBrandList_t * brands)3715c51f124SMoriah Waterland z_free_brand_list(zoneBrandList_t *brands)
3725c51f124SMoriah Waterland {
3735c51f124SMoriah Waterland 	while (brands != NULL) {
3745c51f124SMoriah Waterland 		zoneBrandList_t *temp = brands;
3755c51f124SMoriah Waterland 		free(brands->string_ptr);
3765c51f124SMoriah Waterland 		brands = brands->next;
3775c51f124SMoriah Waterland 		free(temp);
3785c51f124SMoriah Waterland 	}
3795c51f124SMoriah Waterland }
3805c51f124SMoriah Waterland 
3815c51f124SMoriah Waterland /*
3825c51f124SMoriah Waterland  * Name:	z_make_brand_list
3835c51f124SMoriah Waterland  * Description:	Given a string with a list of brand name delimited by
3845c51f124SMoriah Waterland  *		the delimeter passed in, build a zoneBrandList_t structure
3855c51f124SMoriah Waterland  *		with the list of brand names and return it to the caller.
3865c51f124SMoriah Waterland  * Arguments:
3875c51f124SMoriah Waterland  *		brands - const char pointer to string list of brand names
3885c51f124SMoriah Waterland  *		delim - const char pointer to string representing the
3895c51f124SMoriah Waterland  *			delimeter for brands string.
3905c51f124SMoriah Waterland  * Returns:	zoneBrandList_t *
3915c51f124SMoriah Waterland  *			== NULL - error, list could not be generated
3925c51f124SMoriah Waterland  *			!= NULL - success, list returned
3935c51f124SMoriah Waterland  * NOTE:	Any zoneBrandList_t returned is placed in new storage for the
3945c51f124SMoriah Waterland  *		calling function.  The caller must use 'z_free_brand_list' to
3955c51f124SMoriah Waterland  *		dispose of the storage once the list is no longer needed.
3965c51f124SMoriah Waterland  */
3975c51f124SMoriah Waterland zoneBrandList_t *
z_make_brand_list(const char * brands,const char * delim)3985c51f124SMoriah Waterland z_make_brand_list(const char *brands, const char *delim)
3995c51f124SMoriah Waterland {
4005c51f124SMoriah Waterland 	zoneBrandList_t *brand = NULL, *head = NULL;
4015c51f124SMoriah Waterland 	char		*blist = NULL;
4025c51f124SMoriah Waterland 	char		*str = NULL;
4035c51f124SMoriah Waterland 
4045c51f124SMoriah Waterland 	if ((blist = strdup(brands)) == NULL)
4055c51f124SMoriah Waterland 		return (NULL);
4065c51f124SMoriah Waterland 
4075c51f124SMoriah Waterland 	if ((str = strtok(blist, delim)) != NULL) {
4085c51f124SMoriah Waterland 		if ((brand = (zoneBrandList_t *)
4095c51f124SMoriah Waterland 		    malloc(sizeof (struct _zoneBrandList))) == NULL) {
4105c51f124SMoriah Waterland 			return (NULL);
4115c51f124SMoriah Waterland 		}
4125c51f124SMoriah Waterland 
4135c51f124SMoriah Waterland 		head = brand;
4145c51f124SMoriah Waterland 		brand->string_ptr = strdup(str);
4155c51f124SMoriah Waterland 		brand->next = NULL;
4165c51f124SMoriah Waterland 
4175c51f124SMoriah Waterland 		while ((str = strtok(NULL, delim)) != NULL) {
4185c51f124SMoriah Waterland 			if ((brand->next = (zoneBrandList_t *)
4195c51f124SMoriah Waterland 			    malloc(sizeof (struct _zoneBrandList))) == NULL) {
4205c51f124SMoriah Waterland 				return (NULL);
4215c51f124SMoriah Waterland 			}
4225c51f124SMoriah Waterland 
4235c51f124SMoriah Waterland 			brand = brand->next;
4245c51f124SMoriah Waterland 			brand->string_ptr = strdup(str);
4255c51f124SMoriah Waterland 			brand->next = NULL;
4265c51f124SMoriah Waterland 		}
4275c51f124SMoriah Waterland 	}
4285c51f124SMoriah Waterland 
4295c51f124SMoriah Waterland 	free(blist);
4305c51f124SMoriah Waterland 	return (head);
4315c51f124SMoriah Waterland }
4325c51f124SMoriah Waterland 
4335c51f124SMoriah Waterland /*
4345c51f124SMoriah Waterland  * Name:	z_get_nonglobal_zone_list_by_brand
4355c51f124SMoriah Waterland  * Description: return zoneList_t object describing all non-global
4365c51f124SMoriah Waterland  *              zones matching the list of brands passed in.
4375c51f124SMoriah Waterland  * Arguments:	brands - The list of zone brands to look for.
4385c51f124SMoriah Waterland  * Returns:	zoneList_t
4395c51f124SMoriah Waterland  *			== NULL - error, list could not be generated
4405c51f124SMoriah Waterland  *			!= NULL - success, list returned
4415c51f124SMoriah Waterland  * NOTE:    	Any zoneList_t returned is placed in new storage for the
4425c51f124SMoriah Waterland  *		calling function. The caller must use 'z_free_zone_list' to
4435c51f124SMoriah Waterland  *		dispose of the storage once the list is no longer needed.
4445c51f124SMoriah Waterland  */
4455c51f124SMoriah Waterland zoneList_t
z_get_nonglobal_zone_list_by_brand(zoneBrandList_t * brands)4465c51f124SMoriah Waterland z_get_nonglobal_zone_list_by_brand(zoneBrandList_t *brands)
4475c51f124SMoriah Waterland {
4485c51f124SMoriah Waterland 	FILE		*zoneIndexFP;
4495c51f124SMoriah Waterland 	int		numzones = 0;
4505c51f124SMoriah Waterland 	struct zoneent	*ze;
4515c51f124SMoriah Waterland 	zoneList_t	zlst = NULL;
4525c51f124SMoriah Waterland 	FILE		*mapFP;
4535c51f124SMoriah Waterland 	char		zonename[ZONENAME_MAX];
4545c51f124SMoriah Waterland 	zone_spec_t	*zent;
4555c51f124SMoriah Waterland 
4565c51f124SMoriah Waterland 	/* if zones are not implemented, return empty list */
4575c51f124SMoriah Waterland 
4585c51f124SMoriah Waterland 	if (!z_zones_are_implemented()) {
4595c51f124SMoriah Waterland 		return ((zoneList_t)NULL);
4605c51f124SMoriah Waterland 	}
4615c51f124SMoriah Waterland 
4625c51f124SMoriah Waterland 	/*
4635c51f124SMoriah Waterland 	 * Open the zone index file.  Note that getzoneent_private() handles
4645c51f124SMoriah Waterland 	 * NULL.
4655c51f124SMoriah Waterland 	 */
4665c51f124SMoriah Waterland 	zoneIndexFP = setzoneent();
4675c51f124SMoriah Waterland 
4685c51f124SMoriah Waterland 	mapFP = zonecfg_open_scratch("", B_FALSE);
4695c51f124SMoriah Waterland 
4705c51f124SMoriah Waterland 	/* index file open; scan all zones; see if any are at least installed */
4715c51f124SMoriah Waterland 
4725c51f124SMoriah Waterland 	while ((ze = getzoneent_private(zoneIndexFP)) != NULL) {
4735c51f124SMoriah Waterland 		zone_state_t	st;
4745c51f124SMoriah Waterland 
4755c51f124SMoriah Waterland 		/* skip the global zone */
4765c51f124SMoriah Waterland 
4775c51f124SMoriah Waterland 		if (strcmp(ze->zone_name, GLOBAL_ZONENAME) == 0) {
4785c51f124SMoriah Waterland 			free(ze);
4795c51f124SMoriah Waterland 			continue;
4805c51f124SMoriah Waterland 		}
4815c51f124SMoriah Waterland 
4825c51f124SMoriah Waterland 		/*
4835c51f124SMoriah Waterland 		 * skip any zones with brands not on the brand list
4845c51f124SMoriah Waterland 		 */
4855c51f124SMoriah Waterland 		if (!z_is_zone_brand_in_list(ze->zone_name, brands)) {
4865c51f124SMoriah Waterland 			free(ze);
4875c51f124SMoriah Waterland 			continue;
4885c51f124SMoriah Waterland 		}
4895c51f124SMoriah Waterland 
4905c51f124SMoriah Waterland 		/*
4915c51f124SMoriah Waterland 		 * If the user specified an explicit zone list, then ignore any
4925c51f124SMoriah Waterland 		 * zones that aren't on that list.
4935c51f124SMoriah Waterland 		 */
4945c51f124SMoriah Waterland 		if ((zent = _z_global_data._zone_spec) != NULL) {
4955c51f124SMoriah Waterland 			while (zent != NULL) {
4965c51f124SMoriah Waterland 				if (strcmp(zent->zl_name, ze->zone_name) == 0)
4975c51f124SMoriah Waterland 					break;
4985c51f124SMoriah Waterland 				zent = zent->zl_next;
4995c51f124SMoriah Waterland 			}
5005c51f124SMoriah Waterland 			if (zent == NULL) {
5015c51f124SMoriah Waterland 				free(ze);
5025c51f124SMoriah Waterland 				continue;
5035c51f124SMoriah Waterland 			}
5045c51f124SMoriah Waterland 		}
5055c51f124SMoriah Waterland 
5065c51f124SMoriah Waterland 		/* non-global zone: create entry for this zone */
5075c51f124SMoriah Waterland 
5085c51f124SMoriah Waterland 		if (numzones == 0) {
5095c51f124SMoriah Waterland 			zlst = (zoneList_t)_z_calloc(
5105c51f124SMoriah Waterland 			    sizeof (zoneListElement_t)*2);
5115c51f124SMoriah Waterland 		} else {
5125c51f124SMoriah Waterland 			zlst = (zoneList_t)_z_realloc(zlst,
5135c51f124SMoriah Waterland 			    sizeof (zoneListElement_t)*(numzones+2));
5145c51f124SMoriah Waterland 			(void) memset(&zlst[numzones], 0L,
5155c51f124SMoriah Waterland 			    sizeof (zoneListElement_t)*2);
5165c51f124SMoriah Waterland 		}
5175c51f124SMoriah Waterland 
5185c51f124SMoriah Waterland 		/*
5195c51f124SMoriah Waterland 		 * remember the zone name, zonepath and the current
5205c51f124SMoriah Waterland 		 * zone state of the zone.
5215c51f124SMoriah Waterland 		 */
5225c51f124SMoriah Waterland 		zlst[numzones]._zlName = _z_strdup(ze->zone_name);
5235c51f124SMoriah Waterland 		zlst[numzones]._zlPath = _z_strdup(ze->zone_path);
5245c51f124SMoriah Waterland 		zlst[numzones]._zlOrigInstallState = ze->zone_state;
5255c51f124SMoriah Waterland 		zlst[numzones]._zlCurrInstallState = ze->zone_state;
5265c51f124SMoriah Waterland 
5275c51f124SMoriah Waterland 		/* get the zone kernel status */
5285c51f124SMoriah Waterland 
5295c51f124SMoriah Waterland 		if (zone_get_state(ze->zone_name, &st) != Z_OK) {
5305c51f124SMoriah Waterland 			st = ZONE_STATE_INCOMPLETE;
5315c51f124SMoriah Waterland 		}
5325c51f124SMoriah Waterland 
5335c51f124SMoriah Waterland 		_z_echoDebug(DBG_ZONES_NGZ_LIST_STATES,
5345c51f124SMoriah Waterland 		    ze->zone_name, ze->zone_state, st);
5355c51f124SMoriah Waterland 
5365c51f124SMoriah Waterland 		/*
5375c51f124SMoriah Waterland 		 * For a scratch zone, we need to know the kernel zone name.
5385c51f124SMoriah Waterland 		 */
5395c51f124SMoriah Waterland 		if (zonecfg_in_alt_root() && mapFP != NULL &&
5405c51f124SMoriah Waterland 		    zonecfg_find_scratch(mapFP, ze->zone_name,
5415c51f124SMoriah Waterland 		    zonecfg_get_root(), zonename, sizeof (zonename)) != -1) {
5425c51f124SMoriah Waterland 			free(zlst[numzones]._zlScratchName);
5435c51f124SMoriah Waterland 			zlst[numzones]._zlScratchName = _z_strdup(zonename);
5445c51f124SMoriah Waterland 		}
5455c51f124SMoriah Waterland 
5465c51f124SMoriah Waterland 		/*
5475c51f124SMoriah Waterland 		 * remember the current kernel status of the zone.
5485c51f124SMoriah Waterland 		 */
5495c51f124SMoriah Waterland 
5505c51f124SMoriah Waterland 		zlst[numzones]._zlOrigKernelStatus = st;
5515c51f124SMoriah Waterland 		zlst[numzones]._zlCurrKernelStatus = st;
5525c51f124SMoriah Waterland 
5535c51f124SMoriah Waterland 		numzones++;
5545c51f124SMoriah Waterland 		free(ze);
5555c51f124SMoriah Waterland 	}
5565c51f124SMoriah Waterland 
5575c51f124SMoriah Waterland 	/* close the index file */
5585c51f124SMoriah Waterland 	endzoneent(zoneIndexFP);
5595c51f124SMoriah Waterland 
5605c51f124SMoriah Waterland 	if (mapFP != NULL)
5615c51f124SMoriah Waterland 		zonecfg_close_scratch(mapFP);
5625c51f124SMoriah Waterland 
5635c51f124SMoriah Waterland 	/* return generated list */
5645c51f124SMoriah Waterland 
5655c51f124SMoriah Waterland 	return (zlst);
5665c51f124SMoriah Waterland }
5675c51f124SMoriah Waterland 
5685c51f124SMoriah Waterland /*
5695c51f124SMoriah Waterland  * Name:	z_get_zonename
5705c51f124SMoriah Waterland  * Description:	return the name of the current zone
5715c51f124SMoriah Waterland  * Arguments:	void
5725c51f124SMoriah Waterland  * Returns:	char *
5735c51f124SMoriah Waterland  *			- pointer to string representing the name of the current
5745c51f124SMoriah Waterland  *			zone
5755c51f124SMoriah Waterland  * NOTE:    	Any string returned is placed in new storage for the
5765c51f124SMoriah Waterland  *		calling function. The caller must use 'Free' to dispose
5775c51f124SMoriah Waterland  *		of the storage once the string is no longer needed.
5785c51f124SMoriah Waterland  */
5795c51f124SMoriah Waterland 
5805c51f124SMoriah Waterland char *
z_get_zonename(void)5815c51f124SMoriah Waterland z_get_zonename(void)
5825c51f124SMoriah Waterland {
5835c51f124SMoriah Waterland 	ssize_t		zonenameLen;
5845c51f124SMoriah Waterland 	char		zonename[ZONENAME_MAX];
5855c51f124SMoriah Waterland 	zoneid_t	zoneid = (zoneid_t)-1;
5865c51f124SMoriah Waterland 
5875c51f124SMoriah Waterland 	/* if zones are not implemented, return "" */
5885c51f124SMoriah Waterland 
5895c51f124SMoriah Waterland 	if (!z_zones_are_implemented()) {
5905c51f124SMoriah Waterland 		return (_z_strdup(""));
5915c51f124SMoriah Waterland 	}
5925c51f124SMoriah Waterland 
5935c51f124SMoriah Waterland 	/* get the zone i.d. of the current zone */
5945c51f124SMoriah Waterland 
5955c51f124SMoriah Waterland 	zoneid = getzoneid();
5965c51f124SMoriah Waterland 
5975c51f124SMoriah Waterland 	/* get the name of the current zone */
5985c51f124SMoriah Waterland 
5995c51f124SMoriah Waterland 	zonenameLen = getzonenamebyid(zoneid, zonename, sizeof (zonename));
6005c51f124SMoriah Waterland 
6015c51f124SMoriah Waterland 	/* return "" if could not get zonename */
6025c51f124SMoriah Waterland 
6035c51f124SMoriah Waterland 	if (zonenameLen < 1) {
6045c51f124SMoriah Waterland 		return (_z_strdup(""));
6055c51f124SMoriah Waterland 	}
6065c51f124SMoriah Waterland 
6075c51f124SMoriah Waterland 	return (_z_strdup(zonename));
6085c51f124SMoriah Waterland }
6095c51f124SMoriah Waterland 
6105c51f124SMoriah Waterland /*
6115c51f124SMoriah Waterland  * Name:	z_global_only
6125c51f124SMoriah Waterland  * Description:	Determine if the global zone is only zone on the spec list.
6135c51f124SMoriah Waterland  * Arguments:	None
6145c51f124SMoriah Waterland  * Returns:	B_TRUE if global zone is the only zone on the list,
6155c51f124SMoriah Waterland  *		B_FALSE otherwise.
6165c51f124SMoriah Waterland  */
6175c51f124SMoriah Waterland 
6185c51f124SMoriah Waterland boolean_t
z_global_only(void)6195c51f124SMoriah Waterland z_global_only(void)
6205c51f124SMoriah Waterland {
6215c51f124SMoriah Waterland 	/* return true if zones are not implemented - treate as global zone */
6225c51f124SMoriah Waterland 
6235c51f124SMoriah Waterland 	if (!z_zones_are_implemented()) {
6245c51f124SMoriah Waterland 		return (B_TRUE);
6255c51f124SMoriah Waterland 	}
6265c51f124SMoriah Waterland 
6275c51f124SMoriah Waterland 	/* return true if this is the global zone */
6285c51f124SMoriah Waterland 
6295c51f124SMoriah Waterland 	if (_z_global_data._zone_spec != NULL &&
6305c51f124SMoriah Waterland 	    _z_global_data._zone_spec->zl_next == NULL &&
6315c51f124SMoriah Waterland 	    strcmp(_z_global_data._zone_spec->zl_name, GLOBAL_ZONENAME) == 0) {
6325c51f124SMoriah Waterland 		return (B_TRUE);
6335c51f124SMoriah Waterland 	}
6345c51f124SMoriah Waterland 
6355c51f124SMoriah Waterland 	/* return false - not the global zone */
6365c51f124SMoriah Waterland 
6375c51f124SMoriah Waterland 	return (B_FALSE);
6385c51f124SMoriah Waterland }
6395c51f124SMoriah Waterland 
6405c51f124SMoriah Waterland /*
6415c51f124SMoriah Waterland  * Name:	z_lock_this_zone
6425c51f124SMoriah Waterland  * Description:	lock this zone
6435c51f124SMoriah Waterland  * Arguments:	a_lflags - [RO, *RO] - (ZLOCKS_T)
6445c51f124SMoriah Waterland  *			Flags indicating which locks to acquire
6455c51f124SMoriah Waterland  * Returns:	boolean_t
6465c51f124SMoriah Waterland  *			== B_TRUE - success specified locks acquired
6475c51f124SMoriah Waterland  *			== B_FALSE - failure specified locks not acquired
6485c51f124SMoriah Waterland  * NOTE: the lock objects for "this zone" are maintained internally.
6495c51f124SMoriah Waterland  */
6505c51f124SMoriah Waterland 
6515c51f124SMoriah Waterland boolean_t
z_lock_this_zone(ZLOCKS_T a_lflags)6525c51f124SMoriah Waterland z_lock_this_zone(ZLOCKS_T a_lflags)
6535c51f124SMoriah Waterland {
6545c51f124SMoriah Waterland 	boolean_t	b;
6555c51f124SMoriah Waterland 	char		*zoneName;
6565c51f124SMoriah Waterland 	pid_t		pid = (pid_t)0;
6575c51f124SMoriah Waterland 
6585c51f124SMoriah Waterland 	/* entry assertions */
6595c51f124SMoriah Waterland 
6605c51f124SMoriah Waterland 	assert(a_lflags != ZLOCKS_NONE);
6615c51f124SMoriah Waterland 
6625c51f124SMoriah Waterland 	/* entry debugging info */
6635c51f124SMoriah Waterland 
6645c51f124SMoriah Waterland 	_z_echoDebug(DBG_ZONES_LCK_THIS, a_lflags);
6655c51f124SMoriah Waterland 
6665c51f124SMoriah Waterland 	zoneName = z_get_zonename();
6675c51f124SMoriah Waterland 	pid = getpid();
6685c51f124SMoriah Waterland 
6695c51f124SMoriah Waterland 	/* lock zone administration */
6705c51f124SMoriah Waterland 
6715c51f124SMoriah Waterland 	if (a_lflags & ZLOCKS_ZONE_ADMIN) {
6725c51f124SMoriah Waterland 		b = _z_lock_zone_object(&_z_global_data._z_ObjectLocks,
6735c51f124SMoriah Waterland 		    zoneName, LOBJ_ZONEADMIN, pid,
6745c51f124SMoriah Waterland 		    MSG_ZONES_LCK_THIS_ZONEADM,
6755c51f124SMoriah Waterland 		    ERR_ZONES_LCK_THIS_ZONEADM);
6765c51f124SMoriah Waterland 		if (!b) {
6775c51f124SMoriah Waterland 			(void) free(zoneName);
6785c51f124SMoriah Waterland 			return (B_FALSE);
6795c51f124SMoriah Waterland 		}
6805c51f124SMoriah Waterland 	}
6815c51f124SMoriah Waterland 
6825c51f124SMoriah Waterland 	/* lock package administration always */
6835c51f124SMoriah Waterland 
6845c51f124SMoriah Waterland 	if (a_lflags & ZLOCKS_PKG_ADMIN) {
6855c51f124SMoriah Waterland 		b = _z_lock_zone_object(&_z_global_data._z_ObjectLocks,
6865c51f124SMoriah Waterland 		    zoneName, LOBJ_PKGADMIN, pid,
6875c51f124SMoriah Waterland 		    MSG_ZONES_LCK_THIS_PKGADM,
6885c51f124SMoriah Waterland 		    ERR_ZONES_LCK_THIS_PKGADM);
6895c51f124SMoriah Waterland 		if (!b) {
6905c51f124SMoriah Waterland 			(void) z_unlock_this_zone(a_lflags);
6915c51f124SMoriah Waterland 			(void) free(zoneName);
6925c51f124SMoriah Waterland 			return (B_FALSE);
6935c51f124SMoriah Waterland 		}
6945c51f124SMoriah Waterland 	}
6955c51f124SMoriah Waterland 
6965c51f124SMoriah Waterland 	(void) free(zoneName);
6975c51f124SMoriah Waterland 
6985c51f124SMoriah Waterland 	return (B_TRUE);
6995c51f124SMoriah Waterland }
7005c51f124SMoriah Waterland 
7015c51f124SMoriah Waterland /*
7025c51f124SMoriah Waterland  * Name:	z_lock_zones
7035c51f124SMoriah Waterland  * Description:	lock specified zones
7045c51f124SMoriah Waterland  * Arguments:	a_zlst - zoneList_t object describing zones to lock
7055c51f124SMoriah Waterland  *		a_lflags - [RO, *RO] - (ZLOCKS_T)
7065c51f124SMoriah Waterland  *			Flags indicating which locks to acquire
7075c51f124SMoriah Waterland  * Returns:	boolean_t
7085c51f124SMoriah Waterland  *			== B_TRUE - success, zones locked
7095c51f124SMoriah Waterland  *			== B_FALSE - failure, zones not locked
7105c51f124SMoriah Waterland  */
7115c51f124SMoriah Waterland 
7125c51f124SMoriah Waterland boolean_t
z_lock_zones(zoneList_t a_zlst,ZLOCKS_T a_lflags)7135c51f124SMoriah Waterland z_lock_zones(zoneList_t a_zlst, ZLOCKS_T a_lflags)
7145c51f124SMoriah Waterland {
7155c51f124SMoriah Waterland 	boolean_t	b;
7165c51f124SMoriah Waterland 	int		i;
7175c51f124SMoriah Waterland 
7185c51f124SMoriah Waterland 	/* entry assertions */
7195c51f124SMoriah Waterland 
7205c51f124SMoriah Waterland 	assert(a_lflags != ZLOCKS_NONE);
7215c51f124SMoriah Waterland 
7225c51f124SMoriah Waterland 	/* entry debugging info */
7235c51f124SMoriah Waterland 
7245c51f124SMoriah Waterland 	_z_echoDebug(DBG_ZONES_LCK_ZONES, a_lflags);
7255c51f124SMoriah Waterland 
7265c51f124SMoriah Waterland 	/* if zones are not implemented, return TRUE */
7275c51f124SMoriah Waterland 
7285c51f124SMoriah Waterland 	if (z_zones_are_implemented() == B_FALSE) {
7295c51f124SMoriah Waterland 		_z_echoDebug(DBG_ZONES_LCK_ZONES_UNIMP);
7305c51f124SMoriah Waterland 		return (B_TRUE);
7315c51f124SMoriah Waterland 	}
7325c51f124SMoriah Waterland 
7335c51f124SMoriah Waterland 	/* lock this zone first before locking other zones */
7345c51f124SMoriah Waterland 
7355c51f124SMoriah Waterland 	b = z_lock_this_zone(a_lflags);
7365c51f124SMoriah Waterland 	if (b == B_FALSE) {
7375c51f124SMoriah Waterland 		return (b);
7385c51f124SMoriah Waterland 	}
7395c51f124SMoriah Waterland 
7405c51f124SMoriah Waterland 	/* ignore empty list */
7415c51f124SMoriah Waterland 
7425c51f124SMoriah Waterland 	if (a_zlst == (zoneList_t)NULL) {
7435c51f124SMoriah Waterland 		_z_echoDebug(DBG_ZONES_LCK_ZONES_NOZONES);
7445c51f124SMoriah Waterland 		return (B_FALSE);
7455c51f124SMoriah Waterland 	}
7465c51f124SMoriah Waterland 
7475c51f124SMoriah Waterland 	/* zones exist */
7485c51f124SMoriah Waterland 
7495c51f124SMoriah Waterland 	_z_echoDebug(DBG_ZONES_LCK_ZONES_EXIST);
7505c51f124SMoriah Waterland 
7515c51f124SMoriah Waterland 	/*
7525c51f124SMoriah Waterland 	 * lock each listed zone that is currently running
7535c51f124SMoriah Waterland 	 */
7545c51f124SMoriah Waterland 
7555c51f124SMoriah Waterland 	for (i = 0; (a_zlst[i]._zlName != (char *)NULL); i++) {
7565c51f124SMoriah Waterland 		/* ignore zone if already locked */
7575c51f124SMoriah Waterland 		if (a_zlst[i]._zlStatus & ZST_LOCKED) {
7585c51f124SMoriah Waterland 			continue;
7595c51f124SMoriah Waterland 		}
7605c51f124SMoriah Waterland 
7615c51f124SMoriah Waterland 		/* ignore zone if not running */
7625c51f124SMoriah Waterland 		if (a_zlst[i]._zlCurrKernelStatus != ZONE_STATE_RUNNING &&
7635c51f124SMoriah Waterland 		    a_zlst[i]._zlCurrKernelStatus != ZONE_STATE_MOUNTED) {
7645c51f124SMoriah Waterland 			continue;
7655c51f124SMoriah Waterland 		}
7665c51f124SMoriah Waterland 
7675c51f124SMoriah Waterland 		/*
7685c51f124SMoriah Waterland 		 * mark zone locked - if interrupted out during lock, an attempt
7695c51f124SMoriah Waterland 		 * will be made to release the lock
7705c51f124SMoriah Waterland 		 */
7715c51f124SMoriah Waterland 		a_zlst[i]._zlStatus |= ZST_LOCKED;
7725c51f124SMoriah Waterland 
7735c51f124SMoriah Waterland 		/* lock this zone */
7745c51f124SMoriah Waterland 		b = _z_lock_zone(&a_zlst[i], a_lflags);
7755c51f124SMoriah Waterland 
7765c51f124SMoriah Waterland 		/* on failure unlock all zones and return error */
7775c51f124SMoriah Waterland 		if (b != B_TRUE) {
7785c51f124SMoriah Waterland 			_z_program_error(ERR_ZONES_LCK_ZONES_FAILED,
7795c51f124SMoriah Waterland 			    a_zlst[i]._zlName);
7805c51f124SMoriah Waterland 			(void) z_unlock_zones(a_zlst, a_lflags);
7815c51f124SMoriah Waterland 			return (B_FALSE);
7825c51f124SMoriah Waterland 		}
7835c51f124SMoriah Waterland 	}
7845c51f124SMoriah Waterland 
7855c51f124SMoriah Waterland 	/* success */
7865c51f124SMoriah Waterland 
7875c51f124SMoriah Waterland 	return (B_TRUE);
7885c51f124SMoriah Waterland }
7895c51f124SMoriah Waterland 
7905c51f124SMoriah Waterland /*
7915c51f124SMoriah Waterland  * Name:	z_mount_in_lz
7925c51f124SMoriah Waterland  * Description:	Mount global zone directory in specified zone's root file system
7935c51f124SMoriah Waterland  * Arguments:	r_lzMountPoint - pointer to handle to string - on success, the
7945c51f124SMoriah Waterland  *			full path to the mount point relative to the global zone
7955c51f124SMoriah Waterland  *			root file system is returned here - this is needed to
7965c51f124SMoriah Waterland  *			unmount the directory when it is no longer needed
7975c51f124SMoriah Waterland  *		r_lzRootPath - pointer to handle to string - on success, the
7985c51f124SMoriah Waterland  *			full path to the mount point relative to the specified
7995c51f124SMoriah Waterland  *			zone's root file system is returned here - this is
8005c51f124SMoriah Waterland  *			passed to any command executing in the specified zone to
8015c51f124SMoriah Waterland  *			access the directory mounted
8025c51f124SMoriah Waterland  *		a_zoneName - pointer to string representing the name of the zone
8035c51f124SMoriah Waterland  *			to mount the specified global zone directory in
8045c51f124SMoriah Waterland  *		a_gzPath - pointer to string representing the full absolute path
8055c51f124SMoriah Waterland  *			of the global zone directory to LOFS mount inside of the
8065c51f124SMoriah Waterland  *			specified non-global zone
8075c51f124SMoriah Waterland  *		a_mountPointPrefix - pointer to string representing the prefix
8085c51f124SMoriah Waterland  *			to be used when creating the mount point name in the
8095c51f124SMoriah Waterland  *			specified zone's root directory
8105c51f124SMoriah Waterland  * Returns:	boolean_t
8115c51f124SMoriah Waterland  *			== B_TRUE - global zone directory mounted successfully
8125c51f124SMoriah Waterland  *			== B_FALSE - failed to mount directory in specified zone
8135c51f124SMoriah Waterland  * NOTE:    	Any strings returned is placed in new storage for the
8145c51f124SMoriah Waterland  *		calling function. The caller must use 'Free' to dispose
8155c51f124SMoriah Waterland  *		of the storage once the strings are no longer needed.
8165c51f124SMoriah Waterland  */
8175c51f124SMoriah Waterland 
8185c51f124SMoriah Waterland boolean_t
z_mount_in_lz(char ** r_lzMountPoint,char ** r_lzRootPath,char * a_zoneName,char * a_gzPath,char * a_mountPointPrefix)8195c51f124SMoriah Waterland z_mount_in_lz(char **r_lzMountPoint, char **r_lzRootPath, char *a_zoneName,
8205c51f124SMoriah Waterland 	char *a_gzPath, char *a_mountPointPrefix)
8215c51f124SMoriah Waterland {
8225c51f124SMoriah Waterland 	char		lzRootPath[MAXPATHLEN] = {'\0'};
8235c51f124SMoriah Waterland 	char		uuid[MAXPATHLEN] = {'\0'};
8245c51f124SMoriah Waterland 	char		gzMountPoint[MAXPATHLEN] = {'\0'};
8255c51f124SMoriah Waterland 	char		lzMountPoint[MAXPATHLEN] = {'\0'};
8265c51f124SMoriah Waterland 	hrtime_t	hretime;
8275c51f124SMoriah Waterland 	int		err;
8285c51f124SMoriah Waterland 	int		slen;
8295c51f124SMoriah Waterland 	struct tm	tstruct;
8305c51f124SMoriah Waterland 	time_t		thetime;
8315c51f124SMoriah Waterland 	zoneid_t	zid;
8325c51f124SMoriah Waterland 
8335c51f124SMoriah Waterland 	/* entry assertions */
8345c51f124SMoriah Waterland 
8355c51f124SMoriah Waterland 	assert(a_zoneName != (char *)NULL);
8365c51f124SMoriah Waterland 	assert(*a_zoneName != '\0');
8375c51f124SMoriah Waterland 	assert(a_gzPath != (char *)NULL);
8385c51f124SMoriah Waterland 	assert(*a_gzPath != '\0');
8395c51f124SMoriah Waterland 	assert(r_lzMountPoint != (char **)NULL);
8405c51f124SMoriah Waterland 	assert(r_lzRootPath != (char **)NULL);
8415c51f124SMoriah Waterland 
8425c51f124SMoriah Waterland 	/* entry debugging info */
8435c51f124SMoriah Waterland 
8445c51f124SMoriah Waterland 	_z_echoDebug(DBG_ZONES_MOUNT_IN_LZ_ENTRY, a_zoneName, a_gzPath);
8455c51f124SMoriah Waterland 
8465c51f124SMoriah Waterland 	/* reset returned non-global zone mount point path handle */
8475c51f124SMoriah Waterland 
8485c51f124SMoriah Waterland 	*r_lzMountPoint = (char *)NULL;
8495c51f124SMoriah Waterland 	*r_lzRootPath = (char *)NULL;
8505c51f124SMoriah Waterland 
8515c51f124SMoriah Waterland 	/* if zones are not implemented, return FALSE */
8525c51f124SMoriah Waterland 
8535c51f124SMoriah Waterland 	if (z_zones_are_implemented() == B_FALSE) {
8545c51f124SMoriah Waterland 		return (B_FALSE);
8555c51f124SMoriah Waterland 	}
8565c51f124SMoriah Waterland 
8575c51f124SMoriah Waterland 	/* error if global zone path is not absolute */
8585c51f124SMoriah Waterland 
8595c51f124SMoriah Waterland 	if (*a_gzPath != '/') {
8605c51f124SMoriah Waterland 		_z_program_error(ERR_GZPATH_NOT_ABSOLUTE, a_gzPath);
8615c51f124SMoriah Waterland 		return (B_FALSE);
8625c51f124SMoriah Waterland 	}
8635c51f124SMoriah Waterland 
8645c51f124SMoriah Waterland 	/* error if global zone path does not exist */
8655c51f124SMoriah Waterland 
8665c51f124SMoriah Waterland 	if (_z_is_directory(a_gzPath) != 0) {
8675c51f124SMoriah Waterland 		_z_program_error(ERR_GZPATH_NOT_DIR, a_gzPath, strerror(errno));
8685c51f124SMoriah Waterland 		return (B_FALSE);
8695c51f124SMoriah Waterland 	}
8705c51f124SMoriah Waterland 
8715c51f124SMoriah Waterland 	/* verify that specified non-global zone exists */
8725c51f124SMoriah Waterland 
8735c51f124SMoriah Waterland 	err = zone_get_id(a_zoneName, &zid);
8745c51f124SMoriah Waterland 	if (err != Z_OK) {
8755c51f124SMoriah Waterland 		_z_program_error(ERR_GET_ZONEID, a_zoneName,
8765c51f124SMoriah Waterland 		    zonecfg_strerror(err));
8775c51f124SMoriah Waterland 		return (B_FALSE);
8785c51f124SMoriah Waterland 	}
8795c51f124SMoriah Waterland 
8805c51f124SMoriah Waterland 	/* obtain global zone path to non-global zones root file system */
8815c51f124SMoriah Waterland 
8825c51f124SMoriah Waterland 	err = zone_get_rootpath(a_zoneName, lzRootPath, sizeof (lzRootPath));
8835c51f124SMoriah Waterland 	if (err != Z_OK) {
8845c51f124SMoriah Waterland 		_z_program_error(ERR_NO_ZONE_ROOTPATH, a_zoneName,
8855c51f124SMoriah Waterland 		    zonecfg_strerror(err));
8865c51f124SMoriah Waterland 		return (B_FALSE);
8875c51f124SMoriah Waterland 	}
8885c51f124SMoriah Waterland 
8895c51f124SMoriah Waterland 	if (lzRootPath[0] == '\0') {
8905c51f124SMoriah Waterland 		_z_program_error(ERR_ROOTPATH_EMPTY, a_zoneName);
8915c51f124SMoriah Waterland 		return (B_FALSE);
8925c51f124SMoriah Waterland 	}
8935c51f124SMoriah Waterland 
8945c51f124SMoriah Waterland 	/*
8955c51f124SMoriah Waterland 	 * lofs resolve the non-global zone's root path first in case
8965c51f124SMoriah Waterland 	 * its in a path that's been lofs mounted read-only.
8975c51f124SMoriah Waterland 	 */
8985c51f124SMoriah Waterland 	z_resolve_lofs(lzRootPath, sizeof (lzRootPath));
8995c51f124SMoriah Waterland 
9005c51f124SMoriah Waterland 	/* verify that the root path exists */
9015c51f124SMoriah Waterland 
9025c51f124SMoriah Waterland 	if (_z_is_directory(lzRootPath) != 0) {
9035c51f124SMoriah Waterland 		_z_program_error(ERR_LZROOT_NOTDIR, lzRootPath,
9045c51f124SMoriah Waterland 		    strerror(errno));
9055c51f124SMoriah Waterland 		return (B_FALSE);
9065c51f124SMoriah Waterland 	}
9075c51f124SMoriah Waterland 
9085c51f124SMoriah Waterland 	/*
9095c51f124SMoriah Waterland 	 * generate a unique key - the key is the same length as unique uid
9105c51f124SMoriah Waterland 	 * but contains different information that is as unique as can be made;
9115c51f124SMoriah Waterland 	 * include current hires time (nanosecond real timer). Such a unique
9125c51f124SMoriah Waterland 	 * i.d. will look like:
9135c51f124SMoriah Waterland 	 *		0203104092-1145345-0004e94d6af481a0
9145c51f124SMoriah Waterland 	 */
9155c51f124SMoriah Waterland 
9165c51f124SMoriah Waterland 	hretime = gethrtime();
9175c51f124SMoriah Waterland 
9185c51f124SMoriah Waterland 	thetime = time((time_t *)NULL);
9195c51f124SMoriah Waterland 	(void) localtime_r(&thetime, &tstruct);
9205c51f124SMoriah Waterland 
9215c51f124SMoriah Waterland 	slen = snprintf(uuid, sizeof (uuid),
9225c51f124SMoriah Waterland 	    UUID_FORMAT,
9235c51f124SMoriah Waterland 	    tstruct.tm_mday, tstruct.tm_mon, tstruct.tm_year,
9245c51f124SMoriah Waterland 	    tstruct.tm_yday, tstruct.tm_hour, tstruct.tm_min,
9255c51f124SMoriah Waterland 	    tstruct.tm_sec,	tstruct.tm_wday, hretime);
9265c51f124SMoriah Waterland 	if (slen > sizeof (uuid)) {
9275c51f124SMoriah Waterland 		_z_program_error(ERR_GZMOUNT_SNPRINTFUUID_FAILED,
9285c51f124SMoriah Waterland 		    UUID_FORMAT, sizeof (uuid));
9295c51f124SMoriah Waterland 		return (B_FALSE);
9305c51f124SMoriah Waterland 	}
9315c51f124SMoriah Waterland 
9325c51f124SMoriah Waterland 	/* create the global zone mount point */
9335c51f124SMoriah Waterland 
9345c51f124SMoriah Waterland 	slen = snprintf(gzMountPoint, sizeof (gzMountPoint), "%s/.SUNW_%s_%s",
9355c51f124SMoriah Waterland 	    lzRootPath,
9365c51f124SMoriah Waterland 	    a_mountPointPrefix ? a_mountPointPrefix : "zones", uuid);
9375c51f124SMoriah Waterland 	if (slen > sizeof (gzMountPoint)) {
9385c51f124SMoriah Waterland 		_z_program_error(ERR_GZMOUNT_SNPRINTFGMP_FAILED,
9395c51f124SMoriah Waterland 		    "%s/.SUNW_%s_%s", lzRootPath,
9405c51f124SMoriah Waterland 		    a_mountPointPrefix ? a_mountPointPrefix : "zones",
9415c51f124SMoriah Waterland 		    uuid, sizeof (gzMountPoint));
9425c51f124SMoriah Waterland 		return (B_FALSE);
9435c51f124SMoriah Waterland 	}
9445c51f124SMoriah Waterland 
9455c51f124SMoriah Waterland 	slen = snprintf(lzMountPoint, sizeof (lzMountPoint), "%s",
9465c51f124SMoriah Waterland 	    gzMountPoint+strlen(lzRootPath));
9475c51f124SMoriah Waterland 	if (slen > sizeof (lzMountPoint)) {
9485c51f124SMoriah Waterland 		_z_program_error(ERR_GZMOUNT_SNPRINTFLMP_FAILED,
9495c51f124SMoriah Waterland 		    "%s", gzMountPoint+strlen(lzRootPath),
9505c51f124SMoriah Waterland 		    sizeof (lzMountPoint));
9515c51f124SMoriah Waterland 		return (B_FALSE);
9525c51f124SMoriah Waterland 	}
9535c51f124SMoriah Waterland 
9545c51f124SMoriah Waterland 	_z_echoDebug(DBG_MNTPT_NAMES, a_gzPath, a_zoneName, gzMountPoint,
9555c51f124SMoriah Waterland 	    lzMountPoint);
9565c51f124SMoriah Waterland 
9575c51f124SMoriah Waterland 	/* error if the mount point already exists */
9585c51f124SMoriah Waterland 
9595c51f124SMoriah Waterland 	if (_z_is_directory(gzMountPoint) == 0) {
9605c51f124SMoriah Waterland 		_z_program_error(ERR_ZONEROOT_NOTDIR, gzMountPoint,
9615c51f124SMoriah Waterland 		    a_zoneName, strerror(errno));
9625c51f124SMoriah Waterland 		return (B_FALSE);
9635c51f124SMoriah Waterland 	}
9645c51f124SMoriah Waterland 
9655c51f124SMoriah Waterland 	/* create the temporary mount point */
9665c51f124SMoriah Waterland 
9675c51f124SMoriah Waterland 	if (mkdir(gzMountPoint, 0600) != 0) {
9685c51f124SMoriah Waterland 		_z_program_error(ERR_MNTPT_MKDIR, gzMountPoint, a_zoneName,
9695c51f124SMoriah Waterland 		    strerror(errno));
9705c51f124SMoriah Waterland 		return (B_FALSE);
9715c51f124SMoriah Waterland 	}
9725c51f124SMoriah Waterland 
9735c51f124SMoriah Waterland 	/* mount the global zone path on the non-global zone root file system */
9745c51f124SMoriah Waterland 
9755c51f124SMoriah Waterland 	err = mount(a_gzPath, gzMountPoint, MS_RDONLY|MS_DATA, "lofs",
9765c51f124SMoriah Waterland 	    (char *)NULL, 0, (char *)NULL, 0);
9775c51f124SMoriah Waterland 	if (err != 0) {
9785c51f124SMoriah Waterland 		_z_program_error(ERR_GZMOUNT_FAILED, a_gzPath,
9795c51f124SMoriah Waterland 		    gzMountPoint, a_zoneName, strerror(errno));
9805c51f124SMoriah Waterland 		return (B_FALSE);
9815c51f124SMoriah Waterland 	}
9825c51f124SMoriah Waterland 
9835c51f124SMoriah Waterland 	/* success - return both mountpoints to caller */
9845c51f124SMoriah Waterland 
9855c51f124SMoriah Waterland 	*r_lzMountPoint = _z_strdup(gzMountPoint);
9865c51f124SMoriah Waterland 
9875c51f124SMoriah Waterland 	*r_lzRootPath = _z_strdup(lzMountPoint);
9885c51f124SMoriah Waterland 
9895c51f124SMoriah Waterland 	/* return success */
9905c51f124SMoriah Waterland 
9915c51f124SMoriah Waterland 	return (B_TRUE);
9925c51f124SMoriah Waterland }
9935c51f124SMoriah Waterland 
9945c51f124SMoriah Waterland /*
9955c51f124SMoriah Waterland  * Name:	z_non_global_zones_exist
9965c51f124SMoriah Waterland  * Description:	Determine if any non-global native zones exist
9975c51f124SMoriah Waterland  * Arguments:	None.
9985c51f124SMoriah Waterland  * Returns:	boolean_t
9995c51f124SMoriah Waterland  *	== B_TRUE - at least one non-global native zone exists
10005c51f124SMoriah Waterland  *	== B_FALSE - no non-global native zone exists
10015c51f124SMoriah Waterland  */
10025c51f124SMoriah Waterland 
10035c51f124SMoriah Waterland boolean_t
z_non_global_zones_exist(void)10045c51f124SMoriah Waterland z_non_global_zones_exist(void)
10055c51f124SMoriah Waterland {
10065c51f124SMoriah Waterland 	FILE		*zoneIndexFP;
10075c51f124SMoriah Waterland 	boolean_t	anyExist = B_FALSE;
10085c51f124SMoriah Waterland 	struct zoneent	*ze;
10095c51f124SMoriah Waterland 	zone_spec_t	*zent;
10105c51f124SMoriah Waterland 
10115c51f124SMoriah Waterland 	/* if zones are not implemented, return FALSE */
10125c51f124SMoriah Waterland 
10135c51f124SMoriah Waterland 	if (z_zones_are_implemented() == B_FALSE) {
10145c51f124SMoriah Waterland 		return (B_FALSE);
10155c51f124SMoriah Waterland 	}
10165c51f124SMoriah Waterland 
10175c51f124SMoriah Waterland 	/* determine if any zones are configured */
10185c51f124SMoriah Waterland 	zoneIndexFP = setzoneent();
10195c51f124SMoriah Waterland 	if (zoneIndexFP == NULL) {
10205c51f124SMoriah Waterland 		return (B_FALSE);
10215c51f124SMoriah Waterland 	}
10225c51f124SMoriah Waterland 
10235c51f124SMoriah Waterland 	/* index file open; scan all zones; see if any are at least installed */
10245c51f124SMoriah Waterland 
10255c51f124SMoriah Waterland 	while ((ze = getzoneent_private(zoneIndexFP)) != NULL) {
10265c51f124SMoriah Waterland 		/*
10275c51f124SMoriah Waterland 		 * If the user specified an explicit zone list, then ignore any
10285c51f124SMoriah Waterland 		 * zones that aren't on that list.
10295c51f124SMoriah Waterland 		 */
10305c51f124SMoriah Waterland 		if ((zent = _z_global_data._zone_spec) != NULL) {
10315c51f124SMoriah Waterland 			while (zent != NULL) {
10325c51f124SMoriah Waterland 				if (strcmp(zent->zl_name, ze->zone_name) == 0)
10335c51f124SMoriah Waterland 					break;
10345c51f124SMoriah Waterland 				zent = zent->zl_next;
10355c51f124SMoriah Waterland 			}
10365c51f124SMoriah Waterland 			if (zent == NULL) {
10375c51f124SMoriah Waterland 				free(ze);
10385c51f124SMoriah Waterland 				continue;
10395c51f124SMoriah Waterland 			}
10405c51f124SMoriah Waterland 		}
10415c51f124SMoriah Waterland 
10425c51f124SMoriah Waterland 		/* skip the global zone */
10435c51f124SMoriah Waterland 		if (strcmp(ze->zone_name, GLOBAL_ZONENAME) == 0) {
10445c51f124SMoriah Waterland 			free(ze);
10455c51f124SMoriah Waterland 			continue;
10465c51f124SMoriah Waterland 		}
10475c51f124SMoriah Waterland 
10485c51f124SMoriah Waterland 		/* skip any branded zones */
10495c51f124SMoriah Waterland 		if (z_is_zone_branded(ze->zone_name)) {
10505c51f124SMoriah Waterland 			free(ze);
10515c51f124SMoriah Waterland 			continue;
10525c51f124SMoriah Waterland 		}
10535c51f124SMoriah Waterland 
10545c51f124SMoriah Waterland 		/* is this zone installed? */
10555c51f124SMoriah Waterland 		if (ze->zone_state >= ZONE_STATE_INSTALLED) {
10565c51f124SMoriah Waterland 			free(ze);
10575c51f124SMoriah Waterland 			anyExist = B_TRUE;
10585c51f124SMoriah Waterland 			break;
10595c51f124SMoriah Waterland 		}
10605c51f124SMoriah Waterland 		free(ze);
10615c51f124SMoriah Waterland 	}
10625c51f124SMoriah Waterland 
10635c51f124SMoriah Waterland 	/* close the index file */
10645c51f124SMoriah Waterland 
10655c51f124SMoriah Waterland 	endzoneent(zoneIndexFP);
10665c51f124SMoriah Waterland 
10675c51f124SMoriah Waterland 	/* return results */
10685c51f124SMoriah Waterland 
10695c51f124SMoriah Waterland 	return (anyExist);
10705c51f124SMoriah Waterland }
10715c51f124SMoriah Waterland 
10725c51f124SMoriah Waterland /*
10735c51f124SMoriah Waterland  * Name:	z_on_zone_spec
10745c51f124SMoriah Waterland  * Description:	Determine if named zone is on the zone_spec list.
10755c51f124SMoriah Waterland  * Arguments:	Pointer to name to test.
10765c51f124SMoriah Waterland  * Returns:	B_TRUE if named zone is on the list or if the user specified
10775c51f124SMoriah Waterland  *		no list at all (all zones is the default), B_FALSE otherwise.
10785c51f124SMoriah Waterland  */
10795c51f124SMoriah Waterland 
10805c51f124SMoriah Waterland boolean_t
z_on_zone_spec(const char * zonename)10815c51f124SMoriah Waterland z_on_zone_spec(const char *zonename)
10825c51f124SMoriah Waterland {
10835c51f124SMoriah Waterland 	zone_spec_t	*zent;
10845c51f124SMoriah Waterland 
10855c51f124SMoriah Waterland 	/* entry assertions */
10865c51f124SMoriah Waterland 
10875c51f124SMoriah Waterland 	assert(zonename != NULL);
10885c51f124SMoriah Waterland 	assert(*zonename != '\0');
10895c51f124SMoriah Waterland 
10905c51f124SMoriah Waterland 	/* return true if zones not implemented or no zone spec list defined */
10915c51f124SMoriah Waterland 
10925c51f124SMoriah Waterland 	if (!z_zones_are_implemented() || _z_global_data._zone_spec == NULL) {
10935c51f124SMoriah Waterland 		return (B_TRUE);
10945c51f124SMoriah Waterland 	}
10955c51f124SMoriah Waterland 
10965c51f124SMoriah Waterland 	/* return true if named zone is on the zone spec list */
10975c51f124SMoriah Waterland 
10985c51f124SMoriah Waterland 	for (zent = _z_global_data._zone_spec;
10995c51f124SMoriah Waterland 	    zent != NULL; zent = zent->zl_next) {
11005c51f124SMoriah Waterland 		if (strcmp(zent->zl_name, zonename) == 0)
11015c51f124SMoriah Waterland 			return (B_TRUE);
11025c51f124SMoriah Waterland 	}
11035c51f124SMoriah Waterland 
11045c51f124SMoriah Waterland 	/* named zone is not on the zone spec list */
11055c51f124SMoriah Waterland 
11065c51f124SMoriah Waterland 	return (B_FALSE);
11075c51f124SMoriah Waterland }
11085c51f124SMoriah Waterland 
11095c51f124SMoriah Waterland /*
11105c51f124SMoriah Waterland  * Name:	z_running_in_global_zone
11115c51f124SMoriah Waterland  * Description:	Determine if running in the "global" zone
11125c51f124SMoriah Waterland  * Arguments:	void
11135c51f124SMoriah Waterland  * Returns:	boolean_t
11145c51f124SMoriah Waterland  *			== B_TRUE - running in global zone
11155c51f124SMoriah Waterland  *			== B_FALSE - not running in global zone
11165c51f124SMoriah Waterland  */
11175c51f124SMoriah Waterland 
11185c51f124SMoriah Waterland boolean_t
z_running_in_global_zone(void)11195c51f124SMoriah Waterland z_running_in_global_zone(void)
11205c51f124SMoriah Waterland {
11215c51f124SMoriah Waterland 	static	boolean_t	_zoneIdDetermined = B_FALSE;
11225c51f124SMoriah Waterland 	static	boolean_t	_zoneIsGlobal = B_FALSE;
11235c51f124SMoriah Waterland 
11245c51f124SMoriah Waterland 	/* if ID has not been determined, cache it now */
11255c51f124SMoriah Waterland 
11265c51f124SMoriah Waterland 	if (!_zoneIdDetermined) {
11275c51f124SMoriah Waterland 		_zoneIdDetermined = B_TRUE;
11285c51f124SMoriah Waterland 		_zoneIsGlobal = _z_running_in_global_zone();
11295c51f124SMoriah Waterland 	}
11305c51f124SMoriah Waterland 
11315c51f124SMoriah Waterland 	return (_zoneIsGlobal);
11325c51f124SMoriah Waterland }
11335c51f124SMoriah Waterland 
11345c51f124SMoriah Waterland /*
11355c51f124SMoriah Waterland  * Name:	z_set_output_functions
11365c51f124SMoriah Waterland  * Description:	Link program specific output functions to this library.
11375c51f124SMoriah Waterland  * Arguments:	a_echo_fcn - (_z_printf_fcn_t)
11385c51f124SMoriah Waterland  *			Function to call to cause "normal operation" messages
11395c51f124SMoriah Waterland  *			to be output/displayed
11405c51f124SMoriah Waterland  *		a_echo_debug_fcn - (_z_printf_fcn_t)
11415c51f124SMoriah Waterland  *			Function to call to cause "debugging" messages
11425c51f124SMoriah Waterland  *			to be output/displayed
11435c51f124SMoriah Waterland  *		a_progerr_fcn - (_z_printf_fcn_t)
11445c51f124SMoriah Waterland  *			Function to call to cause "program error" messages
11455c51f124SMoriah Waterland  *			to be output/displayed
11465c51f124SMoriah Waterland  * Returns:	void
11475c51f124SMoriah Waterland  * NOTE:	If NULL is specified for any function, then the functionality
11485c51f124SMoriah Waterland  *		associated with that function is disabled.
11495c51f124SMoriah Waterland  * NOTE:	The function pointers provided must call a function that
11505c51f124SMoriah Waterland  *		takes two arguments:
11515c51f124SMoriah Waterland  *			function(char *format, char *message)
11525c51f124SMoriah Waterland  *		Any registered function will be called like:
11535c51f124SMoriah Waterland  *			function("%s", "message")
11545c51f124SMoriah Waterland  */
11555c51f124SMoriah Waterland 
11565c51f124SMoriah Waterland void
z_set_output_functions(_z_printf_fcn_t a_echo_fcn,_z_printf_fcn_t a_echo_debug_fcn,_z_printf_fcn_t a_progerr_fcn)11575c51f124SMoriah Waterland z_set_output_functions(_z_printf_fcn_t a_echo_fcn,
11585c51f124SMoriah Waterland     _z_printf_fcn_t a_echo_debug_fcn,
11595c51f124SMoriah Waterland     _z_printf_fcn_t a_progerr_fcn)
11605c51f124SMoriah Waterland {
11615c51f124SMoriah Waterland 	_z_global_data._z_echo = a_echo_fcn;
11625c51f124SMoriah Waterland 	_z_global_data._z_echo_debug = a_echo_debug_fcn;
11635c51f124SMoriah Waterland 	_z_global_data._z_progerr = a_progerr_fcn;
11645c51f124SMoriah Waterland }
11655c51f124SMoriah Waterland 
11665c51f124SMoriah Waterland /*
11675c51f124SMoriah Waterland  * Name:	z_set_zone_root
11685c51f124SMoriah Waterland  * Description:	Set root for zones library operations
11695c51f124SMoriah Waterland  * Arguments:	Path to root of boot environment containing zone; must be
11705c51f124SMoriah Waterland  *		absolute.
11715c51f124SMoriah Waterland  * Returns:	None.
11725c51f124SMoriah Waterland  * NOTE:	Must be called before performing any zone-related operations.
11735c51f124SMoriah Waterland  *		(Currently called directly by set_inst_root() during -R
11745c51f124SMoriah Waterland  *		argument handling.)
11755c51f124SMoriah Waterland  */
11765c51f124SMoriah Waterland 
11775c51f124SMoriah Waterland void
z_set_zone_root(const char * zroot)11785c51f124SMoriah Waterland z_set_zone_root(const char *zroot)
11795c51f124SMoriah Waterland {
11805c51f124SMoriah Waterland 	char *rootdir;
11815c51f124SMoriah Waterland 
11825c51f124SMoriah Waterland 	/* if zones are not implemented, just return */
11835c51f124SMoriah Waterland 
11845c51f124SMoriah Waterland 	if (!z_zones_are_implemented())
11855c51f124SMoriah Waterland 		return;
11865c51f124SMoriah Waterland 
11875c51f124SMoriah Waterland 	/* entry assertions */
11885c51f124SMoriah Waterland 
11895c51f124SMoriah Waterland