4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
22 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
29 * Module: zones_args.c
31 * Description: Private functions used by zones library functions to manipulate
36 * _z_add_arg - add new argument to argument array for use in exec() calls
37 * _z_free_args - free all storage contained in an argument array previously
38 * _z_get_argc - return (int) argc count from argument array
39 * _z_get_argv - return (char **)argv pointer from argument array
40 * _z_new_args - create a new argument array for use in exec() calls
52 #include <sys/types.h>
53 #include <sys/param.h>
68 #include "instzones_lib.h"
69 #include "zones_strings.h"
76 * Library Function Prototypes
80 * Local Function Prototypes
84 * Global internal (private) declarations
88 * *****************************************************************************
89 * global external (public) functions
90 * *****************************************************************************
95 * Description: add new argument to argument array for use in exec() calls
96 * Arguments: a_args - [RO, *RW] - (argArray_t *)
97 * Pointer to argument array (previously allocated via
98 * a call to _z_new_args) to add the argument to
99 * a_format - [RO, *RO] - (char *)
100 * Pointer to "printf(3C)" style format argument
101 * ... - [RO, *RO] - (varies)
102 * Arguments as appropriate for format argument specified
107 * - to add an argument that specifies a file descriptor:
109 * _z_add_arg(aa, "/proc/self/fd/%d", fd);
110 * - to add a flag or other known text:
111 * _z_add_arg(aa, "-s")
112 * - to add random text:
114 * _z_add_arg(aa, "%s", random_text);
119 _z_add_arg(argArray_t
*a_args
, char *a_format
, ...)
126 /* entry assertions */
128 assert(a_args
!= NULL
);
129 assert(a_format
!= NULL
);
130 assert(*a_format
!= '\0');
133 * double argument array if array is full
136 if (a_args
->_aaNumArgs
>= a_args
->_aaMaxArgs
) {
140 newMax
= a_args
->_aaMaxArgs
* 2;
141 newArgs
= (char **)_z_realloc(a_args
->_aaArgs
,
142 (newMax
+1) * sizeof (char *));
143 a_args
->_aaArgs
= newArgs
;
144 a_args
->_aaMaxArgs
= newMax
;
148 * determine size of argument to add to list
151 va_start(ap
, a_format
);
152 vres
= vsnprintf(bfr
, sizeof (bfr
), a_format
, ap
);
156 * use the expanded argument if it will fit in the built in buffer,
157 * otherwise, allocate space to hold the argument
160 if (vres
< sizeof (bfr
)) {
161 /* duplicate text already generated in buffer */
162 rstr
= _z_strdup(bfr
);
164 /* allocate new space for argument to add */
166 rstr
= (char *)_z_malloc(vres
+2);
168 /* generate argument to add */
170 va_start(ap
, a_format
);
171 vres
= vsnprintf(rstr
, vres
+1, a_format
, ap
);
175 /* add argument to the end of the argument array */
177 a_args
->_aaArgs
[a_args
->_aaNumArgs
++] = rstr
;
178 a_args
->_aaArgs
[a_args
->_aaNumArgs
] = NULL
;
180 /* successful - return */
187 * Description: free all storage contained in an argument array previously
188 * allocated by a call to _z_new_args
189 * Arguments: a_args - [RO, *RW] - (argArray_t *)
190 * Pointer to argument array (previously allocated via
191 * a call to _z_new_args) to free
193 * NOTE: preserves errno (usually called right after e_execCmd*())
197 _z_free_args(argArray_t
*a_args
)
202 /* entry assertions */
204 assert(a_args
!= NULL
);
205 assert(a_args
->_aaArgs
!= NULL
);
207 /* free all arguments in the argument array */
209 for (i
= (a_args
->_aaNumArgs
-1); i
>= 0; i
--) {
210 assert(a_args
->_aaArgs
[i
] != NULL
);
211 (void) free(a_args
->_aaArgs
[i
]);
214 /* free argument array */
216 (void) free(a_args
->_aaArgs
);
218 /* free argument array structure */
229 * Description: return (int) argc count from argument array
230 * Arguments: a_args - [RO, *RW] - (argArray_t *)
231 * Pointer to argument array (previously allocated via
232 * a call to _z_new_args) to return argc count for
234 * Count of the number of arguments in the argument array
235 * suitable for use in an exec*() call
239 _z_get_argc(argArray_t
*a_args
)
241 return (a_args
->_aaNumArgs
);
246 * Description: return (char **)argv pointer from argument array
247 * Arguments: a_args - [RO, *RW] - (argArray_t *)
248 * Pointer to argument array (previously allocated via
249 * a call to _z_new_args) to return argv pointer for
251 * Pointer to (char **)argv pointer suitable for use
253 * NOTE: the actual character array is always terminated with a NULL
257 _z_get_argv(argArray_t
*a_args
)
259 return (a_args
->_aaArgs
);
264 * Description: create a new argument array for use in exec() calls
265 * Arguments: initialCount - [RO, *RO] - (int)
266 * Initial number of elements to populate the
267 * argument array with - use best guess
268 * Returns: argArray_t *
269 * Pointer to argument array that can be used in other
270 * functions that accept it as an argument
271 * == (argArray_t *)NULL - error
272 * NOTE: you must call _z_free_args() when the returned argument array is
273 * no longer needed so that all storage used can be freed up.
277 _z_new_args(int initialCount
)
281 /* entry assertions */
283 assert(initialCount
>= 0);
285 /* if no guess on size, then assume 1 */
287 if (initialCount
== 0) {
291 /* allocate new argument array structure */
293 aa
= (argArray_t
*)_z_calloc(sizeof (argArray_t
));
295 /* allocate initial argument array */
297 aa
->_aaArgs
= (char **)_z_calloc((initialCount
+1) * sizeof (char *));
299 /* initialize argument indexes */
302 aa
->_aaMaxArgs
= initialCount
;
304 /* successful - return pointer to argument array created */