2N/A/*
2N/A * CDDL HEADER START
2N/A *
2N/A * The contents of this file are subject to the terms of the
2N/A * Common Development and Distribution License (the "License").
2N/A * You may not use this file except in compliance with the License.
2N/A *
2N/A * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
2N/A * or http://www.opensolaris.org/os/licensing.
2N/A * See the License for the specific language governing permissions
2N/A * and limitations under the License.
2N/A *
2N/A * When distributing Covered Code, include this CDDL HEADER in each
2N/A * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
2N/A * If applicable, add the following below this CDDL HEADER, with the
2N/A * fields enclosed by brackets "[]" replaced with your own identifying
2N/A * information: Portions Copyright [yyyy] [name of copyright owner]
2N/A *
2N/A * CDDL HEADER END
2N/A */
2N/A/*
2N/A * Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
2N/A */
2N/A
2N/A
2N/A
2N/A/*
2N/A * Module: zones.c
2N/A * Group: libinstzones
2N/A * Description: Provide "zones" interface for install consolidation code
2N/A *
2N/A * Public Methods:
2N/A *
2N/A * _z_close_file_descriptors - close a file descriptor "a_fd" not in the
2N/A * list "a_fds"
2N/A * _z_echo - Output an interactive message if interaction is enabled
2N/A * _z_echoDebug - Output a debugging message if debugging is enabled
2N/A * _z_is_directory - determine if specified path exists and is a directory
2N/A * _z_program_error - Output an error message to the appropriate destinations
2N/A * _z_pluginCatchSigint - SIGINT/SIGHUP interrupt handler
2N/A * _z_running_in_global_zone - Determine if this process is running in the
2N/A * global zone
2N/A * _z_zones_are_implemented - Determine if zones are supported by the
2N/A * current system
2N/A * _z_brands_are_implemented - determine if branded zones are implemented on
2N/A * this system
2N/A */
2N/A
2N/A/*
2N/A * System includes
2N/A */
2N/A
2N/A#include <stdio.h>
2N/A#include <stdlib.h>
2N/A#include <unistd.h>
2N/A#include <fcntl.h>
2N/A#include <ctype.h>
2N/A#include <sys/types.h>
2N/A#include <sys/param.h>
2N/A#include <string.h>
2N/A#include <strings.h>
2N/A#include <sys/stat.h>
2N/A#include <stdarg.h>
2N/A#include <limits.h>
2N/A#include <errno.h>
2N/A#include <signal.h>
2N/A#include <stropts.h>
2N/A#include <libintl.h>
2N/A#include <locale.h>
2N/A#include <assert.h>
2N/A#include <dlfcn.h>
2N/A
2N/A/*
2N/A * local includes
2N/A */
2N/A
2N/A#include "instzones_lib.h"
2N/A#include "zones_strings.h"
2N/A
2N/A/*
2N/A * Private structures
2N/A */
2N/A
2N/A/*
2N/A * these dynamic libraries are required in order to use the branded zones
2N/A * functionality. If these libraries are not available at runtime,
2N/A * then the zones we find are assumed to be native zones.
2N/A */
2N/A
2N/A#define BRAND1_LIBRARY "libbrand.so.1"
2N/A#define BRAND_LIBRARY "libbrand.so"
2N/A
2N/A/*
2N/A * Library Function Prototypes
2N/A */
2N/A
2N/A/*
2N/A * Local Function Prototypes
2N/A */
2N/A
2N/Astatic void error_and_exit(int error_num);
2N/A
2N/Astatic void (*fatal_err_func)() = &error_and_exit;
2N/A
2N/A/* ----------------------- private functions -------------------------- */
2N/A/*
2N/A * error_and_exit()
2N/A * Abort routine. An exit code of '2' is used by all applications
2N/A * to indicate a non-recoverable fatal error.
2N/A * Parameters:
2N/A * error_num - error index number:
2N/A * ERR_MALLOC_FAIL
2N/A * Return:
2N/A * none
2N/A * Status:
2N/A * private
2N/A */
2N/Astatic void
2N/Aerror_and_exit(int error_num)
2N/A{
2N/A if (error_num == ERR_MALLOC_FAIL)
2N/A (void) fprintf(stderr, "Allocation of memory failed\n");
2N/A else
2N/A (void) fprintf(stderr, "ERROR: code %d\n", error_num);
2N/A exit(2);
2N/A}
2N/A
2N/A/*
2N/A * *****************************************************************************
2N/A * global external (public) functions
2N/A * *****************************************************************************
2N/A */
2N/A
2N/A/*
2N/A * Name: _z_close_file_descriptors
2N/A * Description: close a file descriptor "a_fd" not in the list "a_fds"
2N/A * This function is called from the fdwalk() library function.
2N/A * If the file descriptor passed in is NOT in the list passed in,
2N/A * the file is closed.
2N/A * Arguments: a_fds - [RO, *RO] - (void *)
2N/A * Pointer to list of file descriptors to keep open
2N/A * a_fd - [RO, *RO] - (int)
2N/A * File descriptor to check
2N/A * Returns: int
2N/A * 0 - success
2N/A */
2N/A
2N/Aint
2N/A_z_close_file_descriptors(void *a_fds, int a_fd)
2N/A{
2N/A int *fds;
2N/A int i;
2N/A
2N/A /* do not close standard input, output, or error file descriptors */
2N/A
2N/A if (a_fd == STDIN_FILENO || a_fd == STDOUT_FILENO ||
2N/A a_fd == STDERR_FILENO) {
2N/A return (0);
2N/A }
2N/A
2N/A /* if no file descriptor retention list, close this file */
2N/A
2N/A if (a_fds == (void *)NULL) {
2N/A (void) close(a_fd);
2N/A return (0);
2N/A }
2N/A
2N/A /*
2N/A * retention list provided, skip this descriptor if its in the list
2N/A */
2N/A
2N/A fds = (int *)a_fds;
2N/A
2N/A for (i = 0; fds[i] != -1; i++) {
2N/A if (fds[i] == a_fd) {
2N/A return (0);
2N/A }
2N/A }
2N/A
2N/A /* this descriptor not in retention list - close this file */
2N/A
2N/A (void) close(a_fd);
2N/A
2N/A return (0);
2N/A}
2N/A
2N/A/*
2N/A * Name: _z_echo
2N/A * Synopsis: Output an interactive message if interaction is enabled
2N/A * Description: Main method for outputting an interactive message; call to
2N/A * output interactive message if interation has not been disabled
2N/A * by a previous call to echoSetFlag(0).
2N/A * Arguments: format - [RO, RO*] (char *)
2N/A * printf-style format for debugging message to be output
2N/A * VARG_LIST - [RO] (?)
2N/A * arguments as appropriate to 'format' specified
2N/A * Returns: void
2N/A */
2N/A
2N/A/*PRINTFLIKE1*/
2N/Avoid
2N/A_z_echo(char *a_format, ...)
2N/A{
2N/A va_list ap;
2N/A char message[MAX_MESSAGE_SIZE];
2N/A
2N/A /* entry assertions */
2N/A
2N/A assert(a_format != NULL);
2N/A
2N/A /* return if no progerr function registered */
2N/A
2N/A if (_z_global_data._z_echo == NULL) {
2N/A return;
2N/A }
2N/A
2N/A /* capture message */
2N/A
2N/A va_start(ap, a_format);
2N/A (void) vsnprintf(message, sizeof (message), a_format, ap);
2N/A va_end(ap);
2N/A
2N/A /* pass message to registered function */
2N/A
2N/A (_z_global_data._z_echo)("%s", message);
2N/A}
2N/A
2N/A/*
2N/A * Name: _z_echoDebug
2N/A * Synopsis: Output a debugging message if debugging is enabled
2N/A * Description: Main method for outputting a debugging message; call to
2N/A * output debugging message if debugging has been enabled
2N/A * by a previous call to _z_echoDebugSetFlag(1).
2N/A * Arguments: format - [RO, RO*] (char *)
2N/A * printf-style format for debugging message to be output
2N/A * VARG_LIST - [RO] (?)
2N/A * arguments as appropriate to 'format' specified
2N/A * Returns: void
2N/A * NOTE: format of message will be:
2N/A * # [ aaa bbb ccc ] message
2N/A * where: aaa - process i.d.
2N/A * bbb - zone i.d.
2N/A * ccc - name of program
2N/A * for example:
2N/A * # [ 25685 0 pkgadd ] unable to get package list
2N/A */
2N/A
2N/A/*PRINTFLIKE1*/
2N/Avoid
2N/A_z_echoDebug(char *a_format, ...)
2N/A{
2N/A va_list ap;
2N/A char message[MAX_MESSAGE_SIZE];
2N/A
2N/A /* entry assertions */
2N/A
2N/A assert(a_format != NULL);
2N/A
2N/A /* return if no progerr function registered */
2N/A
2N/A if (_z_global_data._z_echo_debug == NULL) {
2N/A return;
2N/A }
2N/A
2N/A /* capture message */
2N/A
2N/A va_start(ap, a_format);
2N/A (void) vsnprintf(message, sizeof (message), a_format, ap);
2N/A va_end(ap);
2N/A
2N/A /* pass message to registered function */
2N/A
2N/A (_z_global_data._z_echo_debug)("%s", message);
2N/A}
2N/A
2N/A/*
2N/A * Name: _z_is_directory
2N/A * Description: determine if specified path exists and is a directory
2N/A * Arguments: path - pointer to string representing the path to verify
2N/A * returns: 0 - directory exists
2N/A * 1 - directory does not exist or is not a directory
2N/A * NOTE: errno is set appropriately
2N/A */
2N/A
2N/Aint
2N/A_z_is_directory(char *path)
2N/A{
2N/A struct stat statbuf;
2N/A
2N/A /* entry assertions */
2N/A
2N/A assert(path != NULL);
2N/A assert(*path != '\0');
2N/A
2N/A /* return error if path does not exist */
2N/A
2N/A if (stat(path, &statbuf) != 0) {
2N/A return (1);
2N/A }
2N/A
2N/A /* return error if path is not a directory */
2N/A
2N/A if ((statbuf.st_mode & S_IFMT) != S_IFDIR) {
2N/A errno = ENOTDIR;
2N/A return (1);
2N/A }
2N/A
2N/A /* path exists and is a directory */
2N/A
2N/A return (0);
2N/A}
2N/A
2N/A/*
2N/A * Name: _z_pluginCatchSigint
2N/A * Synopsis: SIGINT/SIGHUP interrupt handler
2N/A * Description: Catch the "SIGINT" and "SIGHUP" signals:
2N/A * -> increment _z_SigReceived global variable
2N/A * -> propagate signal to "_z_ChildProcessId" if registered (!= -1)
2N/A * Arguments: signo - [RO, *RO] - (int)
2N/A * Signal number that was caught
2N/A * Returns: void
2N/A */
2N/A
2N/Avoid
2N/A_z_sig_trap(int a_signo)
2N/A{
2N/A /* bump signals received count */
2N/A
2N/A _z_global_data._z_SigReceived++;
2N/A
2N/A /* if child process registered, propagate signal to child */
2N/A
2N/A if (_z_global_data._z_ChildProcessId > 0) {
2N/A (void) kill(_z_global_data._z_ChildProcessId, a_signo);
2N/A }
2N/A}
2N/A
2N/A/*
2N/A * Name: _z_program_error
2N/A * Description: Output an error message to the appropriate destinations
2N/A * Arguments: format - [RO, RO*] (char *)
2N/A * printf-style format for debugging message to be output
2N/A * VARG_LIST - [RO] (?)
2N/A * arguments as appropriate to 'format' specified
2N/A * Returns: void
2N/A * NOTE: format of message will be:
2N/A * [aaa: ] ERROR: message
2N/A * where: aaa - program name (if set)
2N/A * message - results of format and arguments
2N/A * for example:
2N/A * ERROR: unable to get package list
2N/A */
2N/A
2N/A/*PRINTFLIKE1*/
2N/Avoid
2N/A_z_program_error(char *a_format, ...)
2N/A{
2N/A va_list ap;
2N/A char message[MAX_MESSAGE_SIZE];
2N/A
2N/A /* entry assertions */
2N/A
2N/A assert(a_format != NULL);
2N/A
2N/A /* return if no progerr function registered */
2N/A
2N/A if (_z_global_data._z_progerr == NULL) {
2N/A return;
2N/A }
2N/A
2N/A /* capture message */
2N/A
2N/A va_start(ap, a_format);
2N/A (void) vsnprintf(message, sizeof (message), a_format, ap);
2N/A va_end(ap);
2N/A
2N/A /* pass message to registered function */
2N/A
2N/A (_z_global_data._z_progerr)(MSG_PROG_ERR, message);
2N/A}
2N/A
2N/A/*
2N/A * Name: _z_running_in_global_zone
2N/A * Synopsis: Determine if this process is running in the global zone
2N/A * Arguments: void
2N/A * Returns: boolean_t
2N/A * == B_TRUE - this process is running in the global zone
2N/A * == B_FALSE - this process is running in a nonglobal zone
2N/A */
2N/A
2N/Aboolean_t
2N/A_z_running_in_global_zone(void)
2N/A{
2N/A zoneid_t zoneid = (zoneid_t)-1;
2N/A
2N/A /*
2N/A * if zones are not implemented, there is no way to tell if zones
2N/A * are supported or not - in this case, we can only be running in the
2N/A * global zone (since non-global zones cannot exist) so return TRUE
2N/A */
2N/A
2N/A if (z_zones_are_implemented() == B_FALSE) {
2N/A return (B_TRUE);
2N/A }
2N/A
2N/A /* get the zone i.d. of the current zone */
2N/A
2N/A zoneid = getzoneid();
2N/A
2N/A /* return TRUE if this is the global zone i.d. */
2N/A
2N/A if (zoneid == GLOBAL_ZONEID) {
2N/A return (B_TRUE);
2N/A }
2N/A
2N/A /* return FALSE - not in the global zone */
2N/A
2N/A return (B_FALSE);
2N/A}
2N/A
2N/A/*
2N/A * Name: _z_zones_are_implemented
2N/A * Synopsis: Determine if zones are supported by the current system
2N/A * Arguments: void
2N/A * Returns: boolean_t
2N/A * == B_TRUE - zones are supported
2N/A * == B_FALSE - zones are not supported
2N/A */
2N/A
2N/Aboolean_t
2N/A_z_zones_are_implemented(void)
2N/A{
2N/A void *libptr = NULL;
2N/A
2N/A /* locate zone cfg library */
2N/A
2N/A libptr = dlopen(ZONECFG_LIBRARY, RTLD_NOW|RTLD_GLOBAL);
2N/A if (libptr == (void *)NULL) {
2N/A _z_echoDebug(DBG_LIBRARY_NOT_FOUND, ZONECFG_LIBRARY, dlerror());
2N/A libptr = dlopen(ZONECFG1_LIBRARY, RTLD_NOW|RTLD_GLOBAL);
2N/A }
2N/A
2N/A /* return false if library not available */
2N/A
2N/A if (libptr == (void *)NULL) {
2N/A _z_echoDebug(DBG_LIBRARY_NOT_FOUND, ZONECFG1_LIBRARY,
2N/A dlerror());
2N/A return (B_FALSE);
2N/A }
2N/A
2N/A /* library available - close handle */
2N/A
2N/A (void) dlclose(libptr);
2N/A
2N/A /* locate contract filesystem library */
2N/A
2N/A libptr = dlopen(CONTRACT_LIBRARY, RTLD_NOW|RTLD_GLOBAL);
2N/A if (libptr == (void *)NULL) {
2N/A _z_echoDebug(DBG_LIBRARY_NOT_FOUND, CONTRACT_LIBRARY,
2N/A dlerror());
2N/A libptr = dlopen(CONTRACT1_LIBRARY, RTLD_NOW|RTLD_GLOBAL);
2N/A }
2N/A
2N/A /* return false if library not available */
2N/A
2N/A if (libptr == (void *)NULL) {
2N/A _z_echoDebug(DBG_LIBRARY_NOT_FOUND, CONTRACT1_LIBRARY,
2N/A dlerror());
2N/A return (B_FALSE);
2N/A }
2N/A
2N/A /* library available - close handle */
2N/A
2N/A (void) dlclose(libptr);
2N/A
2N/A /* return success */
2N/A
2N/A return (B_TRUE);
2N/A}
2N/A
2N/Aboolean_t
2N/A_z_brands_are_implemented(void)
2N/A{
2N/A void *libptr;
2N/A
2N/A /* locate brand library */
2N/A
2N/A libptr = dlopen(BRAND_LIBRARY, RTLD_NOW|RTLD_GLOBAL);
2N/A if (libptr == NULL) {
2N/A _z_echoDebug(DBG_LIBRARY_NOT_FOUND, BRAND_LIBRARY, dlerror());
2N/A libptr = dlopen(BRAND1_LIBRARY, RTLD_NOW|RTLD_GLOBAL);
2N/A }
2N/A
2N/A /* return false if library not available */
2N/A
2N/A if (libptr == NULL) {
2N/A _z_echoDebug(DBG_LIBRARY_NOT_FOUND, BRAND1_LIBRARY, dlerror());
2N/A return (B_FALSE);
2N/A }
2N/A
2N/A /* library available - close handle */
2N/A
2N/A (void) dlclose(libptr);
2N/A
2N/A /* return success */
2N/A
2N/A return (B_TRUE);
2N/A}
2N/A
2N/A/*
2N/A * z_calloc()
2N/A * Allocate 'size' bytes from the heap using calloc()
2N/A * Parameters:
2N/A * size - number of bytes to allocate
2N/A * Return:
2N/A * NULL - calloc() failure
2N/A * void * - pointer to allocated structure
2N/A * Status:
2N/A * public
2N/A */
2N/Avoid *
2N/A_z_calloc(size_t size)
2N/A{
2N/A void * tmp;
2N/A
2N/A if ((tmp = (void *) malloc(size)) == NULL) {
2N/A fatal_err_func(ERR_MALLOC_FAIL);
2N/A return (NULL);
2N/A }
2N/A
2N/A (void) memset(tmp, 0, size);
2N/A return (tmp);
2N/A}
2N/A
2N/A/*
2N/A * z_malloc()
2N/A * Alloc 'size' bytes from heap using malloc()
2N/A * Parameters:
2N/A * size - number of bytes to malloc
2N/A * Return:
2N/A * NULL - malloc() failure
2N/A * void * - pointer to allocated structure
2N/A * Status:
2N/A * public
2N/A */
2N/Avoid *
2N/A_z_malloc(size_t size)
2N/A{
2N/A void *tmp;
2N/A
2N/A if ((tmp = (void *) malloc(size)) == NULL) {
2N/A fatal_err_func(ERR_MALLOC_FAIL);
2N/A return (NULL);
2N/A } else
2N/A return (tmp);
2N/A}
2N/A
2N/A/*
2N/A * _z_realloc()
2N/A * Calls realloc() with the specfied parameters. _z_realloc()
2N/A * checks for realloc failures and adjusts the return value
2N/A * automatically.
2N/A * Parameters:
2N/A * ptr - pointer to existing data block
2N/A * size - number of bytes additional
2N/A * Return:
2N/A * NULL - realloc() failed
2N/A * void * - pointer to realloc'd structured
2N/A * Status:
2N/A * public
2N/A */
2N/Avoid *
2N/A_z_realloc(void *ptr, size_t size)
2N/A{
2N/A void *tmp;
2N/A
2N/A if ((tmp = (void *)realloc(ptr, size)) == (void *)NULL) {
2N/A fatal_err_func(ERR_MALLOC_FAIL);
2N/A return ((void *)NULL);
2N/A } else
2N/A return (tmp);
2N/A}
2N/A
2N/A/*
2N/A * z_strdup()
2N/A * Allocate space for the string from the heap, copy 'str' into it,
2N/A * and return a pointer to it.
2N/A * Parameters:
2N/A * str - string to duplicate
2N/A * Return:
2N/A * NULL - duplication failed or 'str' was NULL
2N/A * char * - pointer to newly allocated/initialized structure
2N/A * Status:
2N/A * public
2N/A */
2N/Avoid *
2N/A_z_strdup(char *str)
2N/A{
2N/A char *tmp;
2N/A
2N/A if (str == NULL)
2N/A return ((char *)NULL);
2N/A
2N/A if ((tmp = strdup(str)) == NULL) {
2N/A fatal_err_func(ERR_MALLOC_FAIL);
2N/A return ((char *)NULL);
2N/A } else
2N/A return (tmp);
2N/A}