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) 2006, 2012, Oracle and/or its affiliates. All rights reserved.
2N/A */
2N/A
2N/A
2N/A
2N/A/*
2N/A * Module: zones_args.c
2N/A * Group: libinstzones
2N/A * Description: Private functions used by zones library functions to manipulate
2N/A * argument lists
2N/A *
2N/A * Public Methods:
2N/A *
2N/A * _z_add_arg - add new argument to argument array for use in exec() calls
2N/A * _z_free_args - free all storage contained in an argument array previously
2N/A * _z_get_argc - return (int) argc count from argument array
2N/A * _z_get_argv - return (char **)argv pointer from argument array
2N/A * _z_new_args - create a new argument array for use in exec() calls
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 <stdarg.h>
2N/A#include <limits.h>
2N/A#include <errno.h>
2N/A#include <stropts.h>
2N/A#include <libintl.h>
2N/A#include <locale.h>
2N/A#include <assert.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 * Library Function Prototypes
2N/A */
2N/A
2N/A/*
2N/A * Local Function Prototypes
2N/A */
2N/A
2N/A/*
2N/A * Global internal (private) declarations
2N/A */
2N/A
2N/A/*
2N/A * *****************************************************************************
2N/A * global external (public) functions
2N/A * *****************************************************************************
2N/A */
2N/A
2N/A/*
2N/A * Name: _z_add_arg
2N/A * Description: add new argument to argument array for use in exec() calls
2N/A * Arguments: a_args - [RO, *RW] - (argArray_t *)
2N/A * Pointer to argument array (previously allocated via
2N/A * a call to _z_new_args) to add the argument to
2N/A * a_format - [RO, *RO] - (char *)
2N/A * Pointer to "printf(3C)" style format argument
2N/A * ... - [RO, *RO] - (varies)
2N/A * Arguments as appropriate for format argument specified
2N/A * Returns: boolean_t
2N/A * B_TRUE - success
2N/A * B_FALSE - failure
2N/A * Examples:
2N/A * - to add an argument that specifies a file descriptor:
2N/A * int fd;
2N/A * _z_add_arg(aa, "/proc/self/fd/%d", fd);
2N/A * - to add a flag or other known text:
2N/A * _z_add_arg(aa, "-s")
2N/A * - to add random text:
2N/A * char *random_text;
2N/A * _z_add_arg(aa, "%s", random_text);
2N/A */
2N/A
2N/A/*PRINTFLIKE2*/
2N/Aboolean_t
2N/A_z_add_arg(argArray_t *a_args, char *a_format, ...)
2N/A{
2N/A char *rstr = NULL;
2N/A char bfr[MAX_CANON];
2N/A size_t vres = 0;
2N/A va_list ap;
2N/A
2N/A /* entry assertions */
2N/A
2N/A assert(a_args != NULL);
2N/A assert(a_format != NULL);
2N/A assert(*a_format != '\0');
2N/A
2N/A /*
2N/A * double argument array if array is full
2N/A */
2N/A
2N/A if (a_args->_aaNumArgs >= a_args->_aaMaxArgs) {
2N/A int newMax;
2N/A char **newArgs;
2N/A
2N/A newMax = a_args->_aaMaxArgs * 2;
2N/A newArgs = (char **)_z_realloc(a_args->_aaArgs,
2N/A (newMax+1) * sizeof (char *));
2N/A a_args->_aaArgs = newArgs;
2N/A a_args->_aaMaxArgs = newMax;
2N/A }
2N/A
2N/A /*
2N/A * determine size of argument to add to list
2N/A */
2N/A
2N/A va_start(ap, a_format);
2N/A vres = vsnprintf(bfr, sizeof (bfr), a_format, ap);
2N/A va_end(ap);
2N/A
2N/A /*
2N/A * use the expanded argument if it will fit in the built in buffer,
2N/A * otherwise, allocate space to hold the argument
2N/A */
2N/A
2N/A if (vres < sizeof (bfr)) {
2N/A /* duplicate text already generated in buffer */
2N/A rstr = _z_strdup(bfr);
2N/A } else {
2N/A /* allocate new space for argument to add */
2N/A
2N/A rstr = (char *)_z_malloc(vres+2);
2N/A
2N/A /* generate argument to add */
2N/A
2N/A va_start(ap, a_format);
2N/A vres = vsnprintf(rstr, vres+1, a_format, ap);
2N/A va_end(ap);
2N/A }
2N/A
2N/A /* add argument to the end of the argument array */
2N/A
2N/A a_args->_aaArgs[a_args->_aaNumArgs++] = rstr;
2N/A a_args->_aaArgs[a_args->_aaNumArgs] = NULL;
2N/A
2N/A /* successful - return */
2N/A
2N/A return (B_TRUE);
2N/A}
2N/A
2N/A/*
2N/A * Name: _z_free_args
2N/A * Description: free all storage contained in an argument array previously
2N/A * allocated by a call to _z_new_args
2N/A * Arguments: a_args - [RO, *RW] - (argArray_t *)
2N/A * Pointer to argument array (previously allocated via
2N/A * a call to _z_new_args) to free
2N/A * Returns: void
2N/A * NOTE: preserves errno (usually called right after e_execCmd*())
2N/A */
2N/A
2N/Avoid
2N/A_z_free_args(argArray_t *a_args)
2N/A{
2N/A int i;
2N/A int lerrno = errno;
2N/A
2N/A /* entry assertions */
2N/A
2N/A assert(a_args != NULL);
2N/A assert(a_args->_aaArgs != NULL);
2N/A
2N/A /* free all arguments in the argument array */
2N/A
2N/A for (i = (a_args->_aaNumArgs-1); i >= 0; i--) {
2N/A assert(a_args->_aaArgs[i] != NULL);
2N/A (void) free(a_args->_aaArgs[i]);
2N/A }
2N/A
2N/A /* free argument array */
2N/A
2N/A (void) free(a_args->_aaArgs);
2N/A
2N/A /* free argument array structure */
2N/A
2N/A (void) free(a_args);
2N/A
2N/A /* restore errno */
2N/A
2N/A errno = lerrno;
2N/A}
2N/A
2N/A/*
2N/A * Name: _z_get_argv
2N/A * Description: return (char **)argv pointer from argument array
2N/A * Arguments: a_args - [RO, *RW] - (argArray_t *)
2N/A * Pointer to argument array (previously allocated via
2N/A * a call to _z_new_args) to return argv pointer for
2N/A * Returns: char **
2N/A * Pointer to (char **)argv pointer suitable for use
2N/A * in an exec*() call
2N/A * NOTE: the actual character array is always terminated with a NULL
2N/A */
2N/A
2N/Achar **
2N/A_z_get_argv(argArray_t *a_args)
2N/A{
2N/A return (a_args->_aaArgs);
2N/A}
2N/A
2N/A/*
2N/A * Name: _z_new_args
2N/A * Description: create a new argument array for use in exec() calls
2N/A * Arguments: initialCount - [RO, *RO] - (int)
2N/A * Initial number of elements to populate the
2N/A * argument array with - use best guess
2N/A * Returns: argArray_t *
2N/A * Pointer to argument array that can be used in other
2N/A * functions that accept it as an argument
2N/A * == (argArray_t *)NULL - error
2N/A * NOTE: you must call _z_free_args() when the returned argument array is
2N/A * no longer needed so that all storage used can be freed up.
2N/A */
2N/A
2N/AargArray_t *
2N/A_z_new_args(int initialCount)
2N/A{
2N/A argArray_t *aa;
2N/A
2N/A /* entry assertions */
2N/A
2N/A assert(initialCount >= 0);
2N/A
2N/A /* if no guess on size, then assume 1 */
2N/A
2N/A if (initialCount == 0) {
2N/A initialCount = 1;
2N/A }
2N/A
2N/A /* allocate new argument array structure */
2N/A
2N/A aa = (argArray_t *)_z_calloc(sizeof (argArray_t));
2N/A
2N/A /* allocate initial argument array */
2N/A
2N/A aa->_aaArgs = (char **)_z_calloc((initialCount+1) * sizeof (char *));
2N/A
2N/A /* initialize argument indexes */
2N/A
2N/A aa->_aaNumArgs = 0;
2N/A aa->_aaMaxArgs = initialCount;
2N/A
2N/A /* successful - return pointer to argument array created */
2N/A
2N/A return (aa);
2N/A}