xref: /titanic_51/usr/src/lib/libinstzones/common/zones_utils.c (revision 6e1ae2a33c618c4c2b14aec7d2f21743ddea5837)
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 /*
22*6e1ae2a3SGary Pennington  * Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
235c51f124SMoriah Waterland  */
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  *
345c51f124SMoriah Waterland  *  _z_close_file_descriptors - close a file descriptor "a_fd" not in the
355c51f124SMoriah Waterland  *	list "a_fds"
365c51f124SMoriah Waterland  *  _z_echo - Output an interactive message if interaction is enabled
375c51f124SMoriah Waterland  *  _z_echoDebug - Output a debugging message if debugging is enabled
385c51f124SMoriah Waterland  *  _z_is_directory - determine if specified path exists and is a directory
395c51f124SMoriah Waterland  *  _z_program_error - Output an error message to the appropriate destinations
405c51f124SMoriah Waterland  *  _z_pluginCatchSigint - SIGINT/SIGHUP interrupt handler
415c51f124SMoriah Waterland  *  _z_running_in_global_zone - Determine if this process is running in the
425c51f124SMoriah Waterland  *	global zone
435c51f124SMoriah Waterland  *  _z_zones_are_implemented - Determine if zones are supported by the
445c51f124SMoriah Waterland  *	current system
455c51f124SMoriah Waterland  *  _z_brands_are_implemented - determine if branded zones are implemented on
465c51f124SMoriah Waterland  *		this system
475c51f124SMoriah Waterland  */
485c51f124SMoriah Waterland 
495c51f124SMoriah Waterland /*
505c51f124SMoriah Waterland  * System includes
515c51f124SMoriah Waterland  */
525c51f124SMoriah Waterland 
535c51f124SMoriah Waterland #include <stdio.h>
545c51f124SMoriah Waterland #include <stdlib.h>
555c51f124SMoriah Waterland #include <unistd.h>
565c51f124SMoriah Waterland #include <fcntl.h>
575c51f124SMoriah Waterland #include <ctype.h>
585c51f124SMoriah Waterland #include <sys/types.h>
595c51f124SMoriah Waterland #include <sys/param.h>
605c51f124SMoriah Waterland #include <string.h>
615c51f124SMoriah Waterland #include <strings.h>
625c51f124SMoriah Waterland #include <sys/stat.h>
635c51f124SMoriah Waterland #include <stdarg.h>
645c51f124SMoriah Waterland #include <limits.h>
655c51f124SMoriah Waterland #include <errno.h>
665c51f124SMoriah Waterland #include <signal.h>
675c51f124SMoriah Waterland #include <stropts.h>
685c51f124SMoriah Waterland #include <libintl.h>
695c51f124SMoriah Waterland #include <locale.h>
705c51f124SMoriah Waterland #include <assert.h>
715c51f124SMoriah Waterland #include <dlfcn.h>
725c51f124SMoriah Waterland 
735c51f124SMoriah Waterland /*
745c51f124SMoriah Waterland  * local includes
755c51f124SMoriah Waterland  */
765c51f124SMoriah Waterland 
775c51f124SMoriah Waterland #include "instzones_lib.h"
785c51f124SMoriah Waterland #include "zones_strings.h"
795c51f124SMoriah Waterland 
805c51f124SMoriah Waterland /*
815c51f124SMoriah Waterland  * Private structures
825c51f124SMoriah Waterland  */
835c51f124SMoriah Waterland 
845c51f124SMoriah Waterland /*
855c51f124SMoriah Waterland  * these dynamic libraries are required in order to use the branded zones
865c51f124SMoriah Waterland  * functionality.  If these libraries are not available at runtime,
875c51f124SMoriah Waterland  * then the zones we find are assumed to be native zones.
885c51f124SMoriah Waterland  */
895c51f124SMoriah Waterland 
905c51f124SMoriah Waterland #define	BRAND1_LIBRARY	"libbrand.so.1"
915c51f124SMoriah Waterland #define	BRAND_LIBRARY	"libbrand.so"
925c51f124SMoriah Waterland 
935c51f124SMoriah Waterland /*
945c51f124SMoriah Waterland  * Library Function Prototypes
955c51f124SMoriah Waterland  */
965c51f124SMoriah Waterland 
975c51f124SMoriah Waterland /*
985c51f124SMoriah Waterland  * Local Function Prototypes
995c51f124SMoriah Waterland  */
1005c51f124SMoriah Waterland 
1015c51f124SMoriah Waterland static void	error_and_exit(int error_num);
1025c51f124SMoriah Waterland 
1035c51f124SMoriah Waterland static void 	(*fatal_err_func)() = &error_and_exit;
1045c51f124SMoriah Waterland 
1055c51f124SMoriah Waterland /* ----------------------- private functions -------------------------- */
1065c51f124SMoriah Waterland /*
1075c51f124SMoriah Waterland  * error_and_exit()
1085c51f124SMoriah Waterland  *	Abort routine. An exit code of '2' is used by all applications
1095c51f124SMoriah Waterland  *	to indicate a non-recoverable fatal error.
1105c51f124SMoriah Waterland  * Parameters:
1115c51f124SMoriah Waterland  *	error_num - error index number:
1125c51f124SMoriah Waterland  *			ERR_MALLOC_FAIL
1135c51f124SMoriah Waterland  * Return:
1145c51f124SMoriah Waterland  *	none
1155c51f124SMoriah Waterland  * Status:
1165c51f124SMoriah Waterland  *	private
1175c51f124SMoriah Waterland  */
1185c51f124SMoriah Waterland static void
1195c51f124SMoriah Waterland error_and_exit(int error_num)
1205c51f124SMoriah Waterland {
1215c51f124SMoriah Waterland 	if (error_num == ERR_MALLOC_FAIL)
1225c51f124SMoriah Waterland 		(void) fprintf(stderr, "Allocation of memory failed\n");
1235c51f124SMoriah Waterland 	else
1245c51f124SMoriah Waterland 		(void) fprintf(stderr, "ERROR: code %d\n", error_num);
1255c51f124SMoriah Waterland 	exit(2);
1265c51f124SMoriah Waterland }
1275c51f124SMoriah Waterland 
1285c51f124SMoriah Waterland /*
1295c51f124SMoriah Waterland  * *****************************************************************************
1305c51f124SMoriah Waterland  * global external (public) functions
1315c51f124SMoriah Waterland  * *****************************************************************************
1325c51f124SMoriah Waterland  */
1335c51f124SMoriah Waterland 
1345c51f124SMoriah Waterland /*
1355c51f124SMoriah Waterland  * Name:	_z_close_file_descriptors
1365c51f124SMoriah Waterland  * Description:	close a file descriptor "a_fd" not in the list "a_fds"
1375c51f124SMoriah Waterland  *		This function is called from the fdwalk() library function.
1385c51f124SMoriah Waterland  *		If the file descriptor passed in is NOT in the list passed in,
1395c51f124SMoriah Waterland  *		the file is closed.
1405c51f124SMoriah Waterland  * Arguments:	a_fds - [RO, *RO] - (void *)
1415c51f124SMoriah Waterland  *			Pointer to list of file descriptors to keep open
1425c51f124SMoriah Waterland  *		a_fd - [RO, *RO] - (int)
1435c51f124SMoriah Waterland  *			File descriptor to check
1445c51f124SMoriah Waterland  * Returns:	int
1455c51f124SMoriah Waterland  *			0 - success
1465c51f124SMoriah Waterland  */
1475c51f124SMoriah Waterland 
1485c51f124SMoriah Waterland int
1495c51f124SMoriah Waterland _z_close_file_descriptors(void *a_fds, int a_fd)
1505c51f124SMoriah Waterland {
1515c51f124SMoriah Waterland 	int	*fds;
1525c51f124SMoriah Waterland 	int	i;
1535c51f124SMoriah Waterland 
1545c51f124SMoriah Waterland 	/* do not close standard input, output, or error file descriptors */
1555c51f124SMoriah Waterland 
1565c51f124SMoriah Waterland 	if (a_fd == STDIN_FILENO || a_fd == STDOUT_FILENO ||
1575c51f124SMoriah Waterland 	    a_fd == STDERR_FILENO) {
1585c51f124SMoriah Waterland 		return (0);
1595c51f124SMoriah Waterland 	}
1605c51f124SMoriah Waterland 
1615c51f124SMoriah Waterland 	/* if no file descriptor retention list, close this file */
1625c51f124SMoriah Waterland 
1635c51f124SMoriah Waterland 	if (a_fds == (void *)NULL) {
1645c51f124SMoriah Waterland 		(void) close(a_fd);
1655c51f124SMoriah Waterland 		return (0);
1665c51f124SMoriah Waterland 	}
1675c51f124SMoriah Waterland 
1685c51f124SMoriah Waterland 	/*
1695c51f124SMoriah Waterland 	 * retention list provided, skip this descriptor if its in the list
1705c51f124SMoriah Waterland 	 */
1715c51f124SMoriah Waterland 
1725c51f124SMoriah Waterland 	fds = (int *)a_fds;
1735c51f124SMoriah Waterland 
1745c51f124SMoriah Waterland 	for (i = 0; fds[i] != -1; i++) {
1755c51f124SMoriah Waterland 		if (fds[i] == a_fd) {
1765c51f124SMoriah Waterland 			return (0);
1775c51f124SMoriah Waterland 		}
1785c51f124SMoriah Waterland 	}
1795c51f124SMoriah Waterland 
1805c51f124SMoriah Waterland 	/* this descriptor not in retention list - close this file */
1815c51f124SMoriah Waterland 
1825c51f124SMoriah Waterland 	(void) close(a_fd);
1835c51f124SMoriah Waterland 
1845c51f124SMoriah Waterland 	return (0);
1855c51f124SMoriah Waterland }
1865c51f124SMoriah Waterland 
1875c51f124SMoriah Waterland /*
1885c51f124SMoriah Waterland  * Name:	_z_echo
1895c51f124SMoriah Waterland  * Synopsis:	Output an interactive message if interaction is enabled
1905c51f124SMoriah Waterland  * Description:	Main method for outputting an interactive message; call to
1915c51f124SMoriah Waterland  *		output interactive message if interation has not been disabled
1925c51f124SMoriah Waterland  *		by a previous call to echoSetFlag(0).
1935c51f124SMoriah Waterland  * Arguments:	format - [RO, RO*] (char *)
1945c51f124SMoriah Waterland  *			printf-style format for debugging message to be output
1955c51f124SMoriah Waterland  *		VARG_LIST - [RO] (?)
1965c51f124SMoriah Waterland  *			arguments as appropriate to 'format' specified
1975c51f124SMoriah Waterland  * Returns:	void
1985c51f124SMoriah Waterland  */
1995c51f124SMoriah Waterland 
2005c51f124SMoriah Waterland /*PRINTFLIKE1*/
2015c51f124SMoriah Waterland void
2025c51f124SMoriah Waterland _z_echo(char *a_format, ...)
2035c51f124SMoriah Waterland {
2045c51f124SMoriah Waterland 	va_list ap;
2055c51f124SMoriah Waterland 	char	message[MAX_MESSAGE_SIZE];
2065c51f124SMoriah Waterland 
2075c51f124SMoriah Waterland 	/* entry assertions */
2085c51f124SMoriah Waterland 
2095c51f124SMoriah Waterland 	assert(a_format != NULL);
2105c51f124SMoriah Waterland 
2115c51f124SMoriah Waterland 	/* return if no progerr function registered */
2125c51f124SMoriah Waterland 
2135c51f124SMoriah Waterland 	if (_z_global_data._z_echo == NULL) {
2145c51f124SMoriah Waterland 		return;
2155c51f124SMoriah Waterland 	}
2165c51f124SMoriah Waterland 
2175c51f124SMoriah Waterland 	/* capture message */
2185c51f124SMoriah Waterland 
2195c51f124SMoriah Waterland 	va_start(ap, a_format);
2205c51f124SMoriah Waterland 	(void) vsnprintf(message, sizeof (message), a_format, ap);
2215c51f124SMoriah Waterland 	va_end(ap);
2225c51f124SMoriah Waterland 
2235c51f124SMoriah Waterland 	/* pass message to registered function */
2245c51f124SMoriah Waterland 
2255c51f124SMoriah Waterland 	(_z_global_data._z_echo)("%s", message);
2265c51f124SMoriah Waterland }
2275c51f124SMoriah Waterland 
2285c51f124SMoriah Waterland /*
2295c51f124SMoriah Waterland  * Name:	_z_echoDebug
2305c51f124SMoriah Waterland  * Synopsis:	Output a debugging message if debugging is enabled
2315c51f124SMoriah Waterland  * Description:	Main method for outputting a debugging message; call to
2325c51f124SMoriah Waterland  *		output debugging message if debugging has been enabled
2335c51f124SMoriah Waterland  *		by a previous call to _z_echoDebugSetFlag(1).
2345c51f124SMoriah Waterland  * Arguments:	format - [RO, RO*] (char *)
2355c51f124SMoriah Waterland  *			printf-style format for debugging message to be output
2365c51f124SMoriah Waterland  *		VARG_LIST - [RO] (?)
2375c51f124SMoriah Waterland  *			arguments as appropriate to 'format' specified
2385c51f124SMoriah Waterland  * Returns:	void
2395c51f124SMoriah Waterland  * NOTE:	format of message will be:
2405c51f124SMoriah Waterland  *			# [ aaa bbb ccc ] message
2415c51f124SMoriah Waterland  *		where:	aaa - process i.d.
2425c51f124SMoriah Waterland  *			bbb - zone i.d.
2435c51f124SMoriah Waterland  *			ccc - name of program
2445c51f124SMoriah Waterland  * 		for example:
2455c51f124SMoriah Waterland  *			# [ 25685   0 pkgadd     ] unable to get package list
2465c51f124SMoriah Waterland  */
2475c51f124SMoriah Waterland 
2485c51f124SMoriah Waterland /*PRINTFLIKE1*/
2495c51f124SMoriah Waterland void
2505c51f124SMoriah Waterland _z_echoDebug(char *a_format, ...)
2515c51f124SMoriah Waterland {
2525c51f124SMoriah Waterland 	va_list ap;
2535c51f124SMoriah Waterland 	char	message[MAX_MESSAGE_SIZE];
2545c51f124SMoriah Waterland 
2555c51f124SMoriah Waterland 	/* entry assertions */
2565c51f124SMoriah Waterland 
2575c51f124SMoriah Waterland 	assert(a_format != NULL);
2585c51f124SMoriah Waterland 
2595c51f124SMoriah Waterland 	/* return if no progerr function registered */
2605c51f124SMoriah Waterland 
2615c51f124SMoriah Waterland 	if (_z_global_data._z_echo_debug == NULL) {
2625c51f124SMoriah Waterland 		return;
2635c51f124SMoriah Waterland 	}
2645c51f124SMoriah Waterland 
2655c51f124SMoriah Waterland 	/* capture message */
2665c51f124SMoriah Waterland 
2675c51f124SMoriah Waterland 	va_start(ap, a_format);
2685c51f124SMoriah Waterland 	(void) vsnprintf(message, sizeof (message), a_format, ap);
2695c51f124SMoriah Waterland 	va_end(ap);
2705c51f124SMoriah Waterland 
2715c51f124SMoriah Waterland 	/* pass message to registered function */
2725c51f124SMoriah Waterland 
2735c51f124SMoriah Waterland 	(_z_global_data._z_echo_debug)("%s", message);
2745c51f124SMoriah Waterland }
2755c51f124SMoriah Waterland 
2765c51f124SMoriah Waterland /*
2775c51f124SMoriah Waterland  * Name:	_z_is_directory
2785c51f124SMoriah Waterland  * Description:	determine if specified path exists and is a directory
2795c51f124SMoriah Waterland  * Arguments:	path - pointer to string representing the path to verify
2805c51f124SMoriah Waterland  * returns: 0 - directory exists
2815c51f124SMoriah Waterland  *	    1 - directory does not exist or is not a directory
2825c51f124SMoriah Waterland  * NOTE:	errno is set appropriately
2835c51f124SMoriah Waterland  */
2845c51f124SMoriah Waterland 
2855c51f124SMoriah Waterland int
2865c51f124SMoriah Waterland _z_is_directory(char *path)
2875c51f124SMoriah Waterland {
2885c51f124SMoriah Waterland 	struct stat statbuf;
2895c51f124SMoriah Waterland 
2905c51f124SMoriah Waterland 	/* entry assertions */
2915c51f124SMoriah Waterland 
2925c51f124SMoriah Waterland 	assert(path != NULL);
2935c51f124SMoriah Waterland 	assert(*path != '\0');
2945c51f124SMoriah Waterland 
2955c51f124SMoriah Waterland 	/* return error if path does not exist */
2965c51f124SMoriah Waterland 
2975c51f124SMoriah Waterland 	if (stat(path, &statbuf) != 0) {
2985c51f124SMoriah Waterland 		return (1);
2995c51f124SMoriah Waterland 	}
3005c51f124SMoriah Waterland 
3015c51f124SMoriah Waterland 	/* return error if path is not a directory */
3025c51f124SMoriah Waterland 
3035c51f124SMoriah Waterland 	if ((statbuf.st_mode & S_IFMT) != S_IFDIR) {
3045c51f124SMoriah Waterland 		errno = ENOTDIR;
3055c51f124SMoriah Waterland 		return (1);
3065c51f124SMoriah Waterland 	}
3075c51f124SMoriah Waterland 
3085c51f124SMoriah Waterland 	/* path exists and is a directory */
3095c51f124SMoriah Waterland 
3105c51f124SMoriah Waterland 	return (0);
3115c51f124SMoriah Waterland }
3125c51f124SMoriah Waterland 
3135c51f124SMoriah Waterland /*
3145c51f124SMoriah Waterland  * Name:	_z_pluginCatchSigint
3155c51f124SMoriah Waterland  * Synopsis:	SIGINT/SIGHUP interrupt handler
3165c51f124SMoriah Waterland  * Description:	Catch the "SIGINT" and "SIGHUP" signals:
3175c51f124SMoriah Waterland  *		-> increment _z_SigReceived global variable
3185c51f124SMoriah Waterland  *		-> propagate signal to "_z_ChildProcessId" if registered (!= -1)
3195c51f124SMoriah Waterland  * Arguments:	signo - [RO, *RO] - (int)
3205c51f124SMoriah Waterland  *			Signal number that was caught
3215c51f124SMoriah Waterland  * Returns:	void
3225c51f124SMoriah Waterland  */
3235c51f124SMoriah Waterland 
3245c51f124SMoriah Waterland void
3255c51f124SMoriah Waterland _z_sig_trap(int a_signo)
3265c51f124SMoriah Waterland {
3275c51f124SMoriah Waterland 	/* bump signals received count */
3285c51f124SMoriah Waterland 
3295c51f124SMoriah Waterland 	_z_global_data._z_SigReceived++;
3305c51f124SMoriah Waterland 
3315c51f124SMoriah Waterland 	/* if child process registered, propagate signal to child */
3325c51f124SMoriah Waterland 
3335c51f124SMoriah Waterland 	if (_z_global_data._z_ChildProcessId > 0) {
3345c51f124SMoriah Waterland 		(void) kill(_z_global_data._z_ChildProcessId, a_signo);
3355c51f124SMoriah Waterland 	}
3365c51f124SMoriah Waterland }
3375c51f124SMoriah Waterland 
3385c51f124SMoriah Waterland /*
3395c51f124SMoriah Waterland  * Name:	_z_program_error
3405c51f124SMoriah Waterland  * Description:	Output an error message to the appropriate destinations
3415c51f124SMoriah Waterland  * Arguments:	format - [RO, RO*] (char *)
3425c51f124SMoriah Waterland  *			printf-style format for debugging message to be output
3435c51f124SMoriah Waterland  *		VARG_LIST - [RO] (?)
3445c51f124SMoriah Waterland  *			arguments as appropriate to 'format' specified
3455c51f124SMoriah Waterland  * Returns:	void
3465c51f124SMoriah Waterland  * NOTE:	format of message will be:
3475c51f124SMoriah Waterland  *			[aaa: ] ERROR: message
3485c51f124SMoriah Waterland  *		where:	aaa - program name (if set)
3495c51f124SMoriah Waterland  *			message - results of format and arguments
3505c51f124SMoriah Waterland  * 		for example:
3515c51f124SMoriah Waterland  *			ERROR: unable to get package list
3525c51f124SMoriah Waterland  */
3535c51f124SMoriah Waterland 
3545c51f124SMoriah Waterland /*PRINTFLIKE1*/
3555c51f124SMoriah Waterland void
3565c51f124SMoriah Waterland _z_program_error(char *a_format, ...)
3575c51f124SMoriah Waterland {
3585c51f124SMoriah Waterland 	va_list ap;
3595c51f124SMoriah Waterland 	char	message[MAX_MESSAGE_SIZE];
3605c51f124SMoriah Waterland 
3615c51f124SMoriah Waterland 	/* entry assertions */
3625c51f124SMoriah Waterland 
3635c51f124SMoriah Waterland 	assert(a_format != NULL);
3645c51f124SMoriah Waterland 
3655c51f124SMoriah Waterland 	/* return if no progerr function registered */
3665c51f124SMoriah Waterland 
3675c51f124SMoriah Waterland 	if (_z_global_data._z_progerr == NULL) {
3685c51f124SMoriah Waterland 		return;
3695c51f124SMoriah Waterland 	}
3705c51f124SMoriah Waterland 
3715c51f124SMoriah Waterland 	/* capture message */
3725c51f124SMoriah Waterland 
3735c51f124SMoriah Waterland 	va_start(ap, a_format);
3745c51f124SMoriah Waterland 	(void) vsnprintf(message, sizeof (message), a_format, ap);
3755c51f124SMoriah Waterland 	va_end(ap);
3765c51f124SMoriah Waterland 
3775c51f124SMoriah Waterland 	/* pass message to registered function */
3785c51f124SMoriah Waterland 
3795c51f124SMoriah Waterland 	(_z_global_data._z_progerr)(MSG_PROG_ERR, message);
3805c51f124SMoriah Waterland }
3815c51f124SMoriah Waterland 
3825c51f124SMoriah Waterland /*
3835c51f124SMoriah Waterland  * Name:	_z_running_in_global_zone
3845c51f124SMoriah Waterland  * Synopsis:	Determine if this process is running in the global zone
3855c51f124SMoriah Waterland  * Arguments:	void
3865c51f124SMoriah Waterland  * Returns:	boolean_t
3875c51f124SMoriah Waterland  *			== B_TRUE - this process is running in the global zone
3885c51f124SMoriah Waterland  *			== B_FALSE - this process is running in a nonglobal zone
3895c51f124SMoriah Waterland  */
3905c51f124SMoriah Waterland 
3915c51f124SMoriah Waterland boolean_t
3925c51f124SMoriah Waterland _z_running_in_global_zone(void)
3935c51f124SMoriah Waterland {
3945c51f124SMoriah Waterland 	zoneid_t	zoneid = (zoneid_t)-1;
3955c51f124SMoriah Waterland 
3965c51f124SMoriah Waterland 	/*
3975c51f124SMoriah Waterland 	 * if zones are not implemented, there is no way to tell if zones
3985c51f124SMoriah Waterland 	 * are supported or not - in this case, we can only be running in the
3995c51f124SMoriah Waterland 	 * global zone (since non-global zones cannot exist) so return TRUE
4005c51f124SMoriah Waterland 	 */
4015c51f124SMoriah Waterland 
4025c51f124SMoriah Waterland 	if (z_zones_are_implemented() == B_FALSE) {
4035c51f124SMoriah Waterland 		return (B_TRUE);
4045c51f124SMoriah Waterland 	}
4055c51f124SMoriah Waterland 
4065c51f124SMoriah Waterland 	/* get the zone i.d. of the current zone */
4075c51f124SMoriah Waterland 
4085c51f124SMoriah Waterland 	zoneid = getzoneid();
4095c51f124SMoriah Waterland 
4105c51f124SMoriah Waterland 	/* return TRUE if this is the global zone i.d. */
4115c51f124SMoriah Waterland 
4125c51f124SMoriah Waterland 	if (zoneid == GLOBAL_ZONEID) {
4135c51f124SMoriah Waterland 		return (B_TRUE);
4145c51f124SMoriah Waterland 	}
4155c51f124SMoriah Waterland 
4165c51f124SMoriah Waterland 	/* return FALSE - not in the global zone */
4175c51f124SMoriah Waterland 
4185c51f124SMoriah Waterland 	return (B_FALSE);
4195c51f124SMoriah Waterland }
4205c51f124SMoriah Waterland 
4215c51f124SMoriah Waterland /*
4225c51f124SMoriah Waterland  * Name:	_z_zones_are_implemented
4235c51f124SMoriah Waterland  * Synopsis:	Determine if zones are supported by the current system
4245c51f124SMoriah Waterland  * Arguments:	void
4255c51f124SMoriah Waterland  * Returns:	boolean_t
4265c51f124SMoriah Waterland  *			== B_TRUE - zones are supported
4275c51f124SMoriah Waterland  *			== B_FALSE - zones are not supported
4285c51f124SMoriah Waterland  */
4295c51f124SMoriah Waterland 
4305c51f124SMoriah Waterland boolean_t
4315c51f124SMoriah Waterland _z_zones_are_implemented(void)
4325c51f124SMoriah Waterland {
4335c51f124SMoriah Waterland 	void	*libptr = NULL;
4345c51f124SMoriah Waterland 
4355c51f124SMoriah Waterland 	/* locate zone cfg library */
4365c51f124SMoriah Waterland 
4375c51f124SMoriah Waterland 	libptr = dlopen(ZONECFG_LIBRARY, RTLD_NOW|RTLD_GLOBAL);
4385c51f124SMoriah Waterland 	if (libptr == (void *)NULL) {
4395c51f124SMoriah Waterland 		_z_echoDebug(DBG_LIBRARY_NOT_FOUND, ZONECFG_LIBRARY, dlerror());
4405c51f124SMoriah Waterland 		libptr = dlopen(ZONECFG1_LIBRARY, RTLD_NOW|RTLD_GLOBAL);
4415c51f124SMoriah Waterland 	}
4425c51f124SMoriah Waterland 
4435c51f124SMoriah Waterland 	/* return false if library not available */
4445c51f124SMoriah Waterland 
4455c51f124SMoriah Waterland 	if (libptr == (void *)NULL) {
4465c51f124SMoriah Waterland 		_z_echoDebug(DBG_LIBRARY_NOT_FOUND, ZONECFG1_LIBRARY,
4475c51f124SMoriah Waterland 		    dlerror());
4485c51f124SMoriah Waterland 		return (B_FALSE);
4495c51f124SMoriah Waterland 	}
4505c51f124SMoriah Waterland 
4515c51f124SMoriah Waterland 	/* library available - close handle */
4525c51f124SMoriah Waterland 
4535c51f124SMoriah Waterland 	(void) dlclose(libptr);
4545c51f124SMoriah Waterland 
4555c51f124SMoriah Waterland 	/* locate contract filesystem library */
4565c51f124SMoriah Waterland 
4575c51f124SMoriah Waterland 	libptr = dlopen(CONTRACT_LIBRARY, RTLD_NOW|RTLD_GLOBAL);
4585c51f124SMoriah Waterland 	if (libptr == (void *)NULL) {
4595c51f124SMoriah Waterland 		_z_echoDebug(DBG_LIBRARY_NOT_FOUND, CONTRACT_LIBRARY,
4605c51f124SMoriah Waterland 		    dlerror());
4615c51f124SMoriah Waterland 		libptr = dlopen(CONTRACT1_LIBRARY, RTLD_NOW|RTLD_GLOBAL);
4625c51f124SMoriah Waterland 	}
4635c51f124SMoriah Waterland 
4645c51f124SMoriah Waterland 	/* return false if library not available */
4655c51f124SMoriah Waterland 
4665c51f124SMoriah Waterland 	if (libptr == (void *)NULL) {
4675c51f124SMoriah Waterland 		_z_echoDebug(DBG_LIBRARY_NOT_FOUND, CONTRACT1_LIBRARY,
4685c51f124SMoriah Waterland 		    dlerror());
4695c51f124SMoriah Waterland 		return (B_FALSE);
4705c51f124SMoriah Waterland 	}
4715c51f124SMoriah Waterland 
4725c51f124SMoriah Waterland 	/* library available - close handle */
4735c51f124SMoriah Waterland 
4745c51f124SMoriah Waterland 	(void) dlclose(libptr);
4755c51f124SMoriah Waterland 
4765c51f124SMoriah Waterland 	/* return success */
4775c51f124SMoriah Waterland 
4785c51f124SMoriah Waterland 	return (B_TRUE);
4795c51f124SMoriah Waterland }
4805c51f124SMoriah Waterland 
4815c51f124SMoriah Waterland boolean_t
4825c51f124SMoriah Waterland _z_brands_are_implemented(void)
4835c51f124SMoriah Waterland {
4845c51f124SMoriah Waterland 	void	*libptr;
4855c51f124SMoriah Waterland 
4865c51f124SMoriah Waterland 	/* locate brand library */
4875c51f124SMoriah Waterland 
4885c51f124SMoriah Waterland 	libptr = dlopen(BRAND_LIBRARY, RTLD_NOW|RTLD_GLOBAL);
4895c51f124SMoriah Waterland 	if (libptr == NULL) {
4905c51f124SMoriah Waterland 		_z_echoDebug(DBG_LIBRARY_NOT_FOUND, BRAND_LIBRARY, dlerror());
4915c51f124SMoriah Waterland 		libptr = dlopen(BRAND1_LIBRARY, RTLD_NOW|RTLD_GLOBAL);
4925c51f124SMoriah Waterland 	}
4935c51f124SMoriah Waterland 
4945c51f124SMoriah Waterland 	/* return false if library not available */
4955c51f124SMoriah Waterland 
4965c51f124SMoriah Waterland 	if (libptr == NULL) {
4975c51f124SMoriah Waterland 		_z_echoDebug(DBG_LIBRARY_NOT_FOUND, BRAND1_LIBRARY, dlerror());
4985c51f124SMoriah Waterland 		return (B_FALSE);
4995c51f124SMoriah Waterland 	}
5005c51f124SMoriah Waterland 
5015c51f124SMoriah Waterland 	/* library available - close handle */
5025c51f124SMoriah Waterland 
5035c51f124SMoriah Waterland 	(void) dlclose(libptr);
5045c51f124SMoriah Waterland 
5055c51f124SMoriah Waterland 	/* return success */
5065c51f124SMoriah Waterland 
5075c51f124SMoriah Waterland 	return (B_TRUE);
5085c51f124SMoriah Waterland }
5095c51f124SMoriah Waterland 
5105c51f124SMoriah Waterland /*
5115c51f124SMoriah Waterland  * z_calloc()
5125c51f124SMoriah Waterland  * 	Allocate 'size' bytes from the heap using calloc()
5135c51f124SMoriah Waterland  * Parameters:
5145c51f124SMoriah Waterland  *	size	- number of bytes to allocate
5155c51f124SMoriah Waterland  * Return:
5165c51f124SMoriah Waterland  *	NULL	- calloc() failure
5175c51f124SMoriah Waterland  *	void *	- pointer to allocated structure
5185c51f124SMoriah Waterland  * Status:
5195c51f124SMoriah Waterland  *	public
5205c51f124SMoriah Waterland  */
5215c51f124SMoriah Waterland void *
5225c51f124SMoriah Waterland _z_calloc(size_t size)
5235c51f124SMoriah Waterland {
5245c51f124SMoriah Waterland 	void *	tmp;
5255c51f124SMoriah Waterland 
5265c51f124SMoriah Waterland 	if ((tmp = (void *) malloc(size)) == NULL) {
5275c51f124SMoriah Waterland 		fatal_err_func(ERR_MALLOC_FAIL);
5285c51f124SMoriah Waterland 		return (NULL);
5295c51f124SMoriah Waterland 	}
5305c51f124SMoriah Waterland 
5315c51f124SMoriah Waterland 	(void) memset(tmp, 0, size);
5325c51f124SMoriah Waterland 	return (tmp);
5335c51f124SMoriah Waterland }
5345c51f124SMoriah Waterland 
5355c51f124SMoriah Waterland /*
5365c51f124SMoriah Waterland  * z_malloc()
5375c51f124SMoriah Waterland  * 	Alloc 'size' bytes from heap using malloc()
5385c51f124SMoriah Waterland  * Parameters:
5395c51f124SMoriah Waterland  *	size	- number of bytes to malloc
5405c51f124SMoriah Waterland  * Return:
5415c51f124SMoriah Waterland  *	NULL	- malloc() failure
5425c51f124SMoriah Waterland  *	void *	- pointer to allocated structure
5435c51f124SMoriah Waterland  * Status:
5445c51f124SMoriah Waterland  *	public
5455c51f124SMoriah Waterland  */
5465c51f124SMoriah Waterland void *
5475c51f124SMoriah Waterland _z_malloc(size_t size)
5485c51f124SMoriah Waterland {
5495c51f124SMoriah Waterland 	void *tmp;
5505c51f124SMoriah Waterland 
5515c51f124SMoriah Waterland 	if ((tmp = (void *) malloc(size)) == NULL) {
5525c51f124SMoriah Waterland 		fatal_err_func(ERR_MALLOC_FAIL);
5535c51f124SMoriah Waterland 		return (NULL);
5545c51f124SMoriah Waterland 	} else
5555c51f124SMoriah Waterland 		return (tmp);
5565c51f124SMoriah Waterland }
5575c51f124SMoriah Waterland 
5585c51f124SMoriah Waterland /*
5595c51f124SMoriah Waterland  * _z_realloc()
5605c51f124SMoriah Waterland  *	Calls realloc() with the specfied parameters. _z_realloc()
5615c51f124SMoriah Waterland  *	checks for realloc failures and adjusts the return value
5625c51f124SMoriah Waterland  *	automatically.
5635c51f124SMoriah Waterland  * Parameters:
5645c51f124SMoriah Waterland  *	ptr	- pointer to existing data block
5655c51f124SMoriah Waterland  * 	size	- number of bytes additional
5665c51f124SMoriah Waterland  * Return:
5675c51f124SMoriah Waterland  *	NULL	- realloc() failed
5685c51f124SMoriah Waterland  *	void *	- pointer to realloc'd structured
5695c51f124SMoriah Waterland  * Status:
5705c51f124SMoriah Waterland  *	public
5715c51f124SMoriah Waterland  */
5725c51f124SMoriah Waterland void *
5735c51f124SMoriah Waterland _z_realloc(void *ptr, size_t size)
5745c51f124SMoriah Waterland {
5755c51f124SMoriah Waterland 	void *tmp;
5765c51f124SMoriah Waterland 
5775c51f124SMoriah Waterland 	if ((tmp = (void *)realloc(ptr, size)) == (void *)NULL) {
5785c51f124SMoriah Waterland 		fatal_err_func(ERR_MALLOC_FAIL);
5795c51f124SMoriah Waterland 		return ((void *)NULL);
5805c51f124SMoriah Waterland 	} else
5815c51f124SMoriah Waterland 		return (tmp);
5825c51f124SMoriah Waterland }
5835c51f124SMoriah Waterland 
5845c51f124SMoriah Waterland /*
5855c51f124SMoriah Waterland  * z_strdup()
5865c51f124SMoriah Waterland  *	Allocate space for the string from the heap, copy 'str' into it,
5875c51f124SMoriah Waterland  *	and return a pointer to it.
5885c51f124SMoriah Waterland  * Parameters:
5895c51f124SMoriah Waterland  *	str	- string to duplicate
5905c51f124SMoriah Waterland  * Return:
5915c51f124SMoriah Waterland  *	NULL	- duplication failed or 'str' was NULL
5925c51f124SMoriah Waterland  * 	char *	- pointer to newly allocated/initialized structure
5935c51f124SMoriah Waterland  * Status:
5945c51f124SMoriah Waterland  *	public
5955c51f124SMoriah Waterland  */
5965c51f124SMoriah Waterland void *
5975c51f124SMoriah Waterland _z_strdup(char *str)
5985c51f124SMoriah Waterland {
5995c51f124SMoriah Waterland 	char *tmp;
6005c51f124SMoriah Waterland 
6015c51f124SMoriah Waterland 	if (str == NULL)
6025c51f124SMoriah Waterland 		return ((char *)NULL);
6035c51f124SMoriah Waterland 
6045c51f124SMoriah Waterland 	if ((tmp = strdup(str)) == NULL) {
6055c51f124SMoriah Waterland 		fatal_err_func(ERR_MALLOC_FAIL);
6065c51f124SMoriah Waterland 		return ((char *)NULL);
6075c51f124SMoriah Waterland 	} else
6085c51f124SMoriah Waterland 		return (tmp);
6095c51f124SMoriah Waterland }
610