xref: /illumos-gate/usr/src/lib/libipp/libipp.c (revision aab83bb8)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
237c478bd9Sstevel@tonic-gate  * Copyright 2001-2002 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #include <stdio.h>
287c478bd9Sstevel@tonic-gate #include <stdlib.h>
297c478bd9Sstevel@tonic-gate #include <sys/types.h>
307c478bd9Sstevel@tonic-gate #include <sys/stat.h>
317c478bd9Sstevel@tonic-gate #include <unistd.h>
327c478bd9Sstevel@tonic-gate #include <errno.h>
337c478bd9Sstevel@tonic-gate #include <strings.h>
347c478bd9Sstevel@tonic-gate #include <string.h>
357c478bd9Sstevel@tonic-gate #include <fcntl.h>
367c478bd9Sstevel@tonic-gate #include <assert.h>
377c478bd9Sstevel@tonic-gate #include <libipp.h>
387c478bd9Sstevel@tonic-gate #include <libnvpair.h>
397c478bd9Sstevel@tonic-gate #include <ipp/ippctl.h>
407c478bd9Sstevel@tonic-gate 
417c478bd9Sstevel@tonic-gate /*
427c478bd9Sstevel@tonic-gate  * Debug macros
437c478bd9Sstevel@tonic-gate  */
447c478bd9Sstevel@tonic-gate 
457c478bd9Sstevel@tonic-gate #if	defined(DEBUG) && !defined(lint)
467c478bd9Sstevel@tonic-gate uint32_t	ipp_debug_flags =
477c478bd9Sstevel@tonic-gate /*
487c478bd9Sstevel@tonic-gate  * DBG_IO |
497c478bd9Sstevel@tonic-gate  */
507c478bd9Sstevel@tonic-gate DBG_ERR |
517c478bd9Sstevel@tonic-gate 0;
527c478bd9Sstevel@tonic-gate 
537c478bd9Sstevel@tonic-gate #define	DBG0(flags, fmt)						\
547c478bd9Sstevel@tonic-gate 	do {								\
557c478bd9Sstevel@tonic-gate 		if (flags & ipp_debug_flags)				\
567c478bd9Sstevel@tonic-gate 			fprintf(stderr, "libipp: " __FN__ ": " fmt);	\
577c478bd9Sstevel@tonic-gate 	} while (0)
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate #define	DBG1(flags, fmt, a)						\
607c478bd9Sstevel@tonic-gate 	do {								\
617c478bd9Sstevel@tonic-gate 		if (flags & ipp_debug_flags)				\
627c478bd9Sstevel@tonic-gate 			fprintf(stderr, "libipp: " __FN__ ": " fmt, a);	\
637c478bd9Sstevel@tonic-gate 	} while (0)
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate #define	DBG2(flags, fmt, a, b)						\
667c478bd9Sstevel@tonic-gate 	do {								\
677c478bd9Sstevel@tonic-gate 		if (flags & ipp_debug_flags)				\
687c478bd9Sstevel@tonic-gate 			fprintf(stderr, "libipp: " __FN__ ": " fmt, a,	\
697c478bd9Sstevel@tonic-gate 			    b);						\
707c478bd9Sstevel@tonic-gate 	} while (0)
717c478bd9Sstevel@tonic-gate 
727c478bd9Sstevel@tonic-gate #define	DBG3(flags, fmt, a, b, c)					\
737c478bd9Sstevel@tonic-gate 	do {								\
747c478bd9Sstevel@tonic-gate 		if (flags & ipp_debug_flags)				\
757c478bd9Sstevel@tonic-gate 			fprintf(stderr, "libipp: " __FN__ ": " fmt, a,	\
767c478bd9Sstevel@tonic-gate 			    b, c);					\
777c478bd9Sstevel@tonic-gate 	} while (0)
787c478bd9Sstevel@tonic-gate 
797c478bd9Sstevel@tonic-gate #else	/* defined(DEBUG) && !defined(lint) */
807c478bd9Sstevel@tonic-gate #define	DBG0(flags, fmt)
817c478bd9Sstevel@tonic-gate #define	DBG1(flags, fmt, a)
827c478bd9Sstevel@tonic-gate #define	DBG2(flags, fmt, a, b)
837c478bd9Sstevel@tonic-gate #define	DBG3(flags, fmt, a, b, c)
847c478bd9Sstevel@tonic-gate #endif	/* defined(DEBUG) && !defined(lint) */
857c478bd9Sstevel@tonic-gate 
867c478bd9Sstevel@tonic-gate /*
877c478bd9Sstevel@tonic-gate  * Control device node
887c478bd9Sstevel@tonic-gate  */
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate #define	IPPCTL_DEVICE	"/devices/pseudo/ippctl@0:ctl"
917c478bd9Sstevel@tonic-gate 
927c478bd9Sstevel@tonic-gate /*
937c478bd9Sstevel@tonic-gate  * Structures.
947c478bd9Sstevel@tonic-gate  */
957c478bd9Sstevel@tonic-gate 
967c478bd9Sstevel@tonic-gate typedef	struct array_desc_t {
977c478bd9Sstevel@tonic-gate 	char	*name;
987c478bd9Sstevel@tonic-gate 	char	**array;
997c478bd9Sstevel@tonic-gate 	int	nelt;
1007c478bd9Sstevel@tonic-gate } array_desc_t;
1017c478bd9Sstevel@tonic-gate 
1027c478bd9Sstevel@tonic-gate /*
1037c478bd9Sstevel@tonic-gate  * Prototypes
1047c478bd9Sstevel@tonic-gate  */
1057c478bd9Sstevel@tonic-gate 
1067c478bd9Sstevel@tonic-gate static int	nvlist_callback(nvlist_t *, void *);
1077c478bd9Sstevel@tonic-gate static int	string_callback(nvlist_t *, void *);
1087c478bd9Sstevel@tonic-gate static int	string_array_callback(nvlist_t *, void *);
1097c478bd9Sstevel@tonic-gate static int	dispatch(nvlist_t **, int (*)(nvlist_t *, void *), void *);
1107c478bd9Sstevel@tonic-gate 
1117c478bd9Sstevel@tonic-gate /*
1127c478bd9Sstevel@tonic-gate  * API functions
1137c478bd9Sstevel@tonic-gate  */
1147c478bd9Sstevel@tonic-gate #define	__FN__	"ipp_action_create"
1157c478bd9Sstevel@tonic-gate int
ipp_action_create(const char * modname,const char * aname,nvlist_t ** nvlpp,ipp_flags_t flags)1167c478bd9Sstevel@tonic-gate ipp_action_create(
1177c478bd9Sstevel@tonic-gate 	const char	*modname,
1187c478bd9Sstevel@tonic-gate 	const char	*aname,
1197c478bd9Sstevel@tonic-gate 	nvlist_t	**nvlpp,
1207c478bd9Sstevel@tonic-gate 	ipp_flags_t	flags)
1217c478bd9Sstevel@tonic-gate {
1227c478bd9Sstevel@tonic-gate 	nvlist_t	*nvlp;
1237c478bd9Sstevel@tonic-gate 	int		rc;
1247c478bd9Sstevel@tonic-gate 
1257c478bd9Sstevel@tonic-gate 	/*
1267c478bd9Sstevel@tonic-gate 	 * Sanity check the arguments.
1277c478bd9Sstevel@tonic-gate 	 */
1287c478bd9Sstevel@tonic-gate 
1297c478bd9Sstevel@tonic-gate 	if (nvlpp == NULL || modname == NULL || aname == NULL) {
1307c478bd9Sstevel@tonic-gate 		DBG0(DBG_ERR, "bad argument\n");
1317c478bd9Sstevel@tonic-gate 		errno = EINVAL;
1327c478bd9Sstevel@tonic-gate 		return (-1);
1337c478bd9Sstevel@tonic-gate 	}
1347c478bd9Sstevel@tonic-gate 
1357c478bd9Sstevel@tonic-gate 	/*
1367c478bd9Sstevel@tonic-gate 	 * Add our data to the nvlist. (This information will be removed for
1377c478bd9Sstevel@tonic-gate 	 * use by ippctl).
1387c478bd9Sstevel@tonic-gate 	 */
1397c478bd9Sstevel@tonic-gate 
1407c478bd9Sstevel@tonic-gate 	nvlp = *nvlpp;
1417c478bd9Sstevel@tonic-gate 	if ((rc = nvlist_add_byte(nvlp, IPPCTL_OP,
1427c478bd9Sstevel@tonic-gate 	    IPPCTL_OP_ACTION_CREATE)) != 0) {
1437c478bd9Sstevel@tonic-gate 		DBG1(DBG_ERR, "failed to add '%s' to nvlist\n", IPPCTL_OP);
1447c478bd9Sstevel@tonic-gate 		goto failed;
1457c478bd9Sstevel@tonic-gate 	}
1467c478bd9Sstevel@tonic-gate 
1477c478bd9Sstevel@tonic-gate 	if ((rc = nvlist_add_string(nvlp, IPPCTL_MODNAME,
1487c478bd9Sstevel@tonic-gate 	    (char *)modname)) != 0) {
1497c478bd9Sstevel@tonic-gate 		DBG1(DBG_ERR, "failed to add '%s' to nvlist\n",
1507c478bd9Sstevel@tonic-gate 		    IPPCTL_MODNAME);
1517c478bd9Sstevel@tonic-gate 		goto failed;
1527c478bd9Sstevel@tonic-gate 	}
1537c478bd9Sstevel@tonic-gate 
1547c478bd9Sstevel@tonic-gate 	if ((rc = nvlist_add_string(nvlp, IPPCTL_ANAME, (char *)aname)) != 0) {
1557c478bd9Sstevel@tonic-gate 		DBG1(DBG_ERR, "failed to add '%s' to nvlist\n", IPPCTL_ANAME);
1567c478bd9Sstevel@tonic-gate 		goto failed;
1577c478bd9Sstevel@tonic-gate 	}
1587c478bd9Sstevel@tonic-gate 
1597c478bd9Sstevel@tonic-gate 	if ((rc = nvlist_add_uint32(nvlp, IPPCTL_FLAGS, flags)) != 0) {
1607c478bd9Sstevel@tonic-gate 		DBG1(DBG_ERR, "failed to add '%s' to nvlist\n", IPPCTL_FLAGS);
1617c478bd9Sstevel@tonic-gate 		goto failed;
1627c478bd9Sstevel@tonic-gate 	}
1637c478bd9Sstevel@tonic-gate 
1647c478bd9Sstevel@tonic-gate 	/*
1657c478bd9Sstevel@tonic-gate 	 * Talk to the kernel.
1667c478bd9Sstevel@tonic-gate 	 */
1677c478bd9Sstevel@tonic-gate 
1687c478bd9Sstevel@tonic-gate 	return (dispatch(nvlpp, nvlist_callback, (void *)nvlpp));
1697c478bd9Sstevel@tonic-gate failed:
1707c478bd9Sstevel@tonic-gate 	errno = rc;
1717c478bd9Sstevel@tonic-gate 	return (-1);
1727c478bd9Sstevel@tonic-gate }
1737c478bd9Sstevel@tonic-gate #undef	__FN__
1747c478bd9Sstevel@tonic-gate 
1757c478bd9Sstevel@tonic-gate #define	__FN__	"ipp_action_destroy"
1767c478bd9Sstevel@tonic-gate int
ipp_action_destroy(const char * aname,ipp_flags_t flags)1777c478bd9Sstevel@tonic-gate ipp_action_destroy(
1787c478bd9Sstevel@tonic-gate 	const char	*aname,
1797c478bd9Sstevel@tonic-gate 	ipp_flags_t	flags)
1807c478bd9Sstevel@tonic-gate {
1817c478bd9Sstevel@tonic-gate 	nvlist_t	*nvlp;
1827c478bd9Sstevel@tonic-gate 	int		rc;
1837c478bd9Sstevel@tonic-gate 
1847c478bd9Sstevel@tonic-gate 	/*
1857c478bd9Sstevel@tonic-gate 	 * Sanity check the arguments.
1867c478bd9Sstevel@tonic-gate 	 */
1877c478bd9Sstevel@tonic-gate 
1887c478bd9Sstevel@tonic-gate 	if (aname == NULL) {
1897c478bd9Sstevel@tonic-gate 		DBG0(DBG_ERR, "bad argument\n");
1907c478bd9Sstevel@tonic-gate 		errno = EINVAL;
1917c478bd9Sstevel@tonic-gate 		return (-1);
1927c478bd9Sstevel@tonic-gate 	}
1937c478bd9Sstevel@tonic-gate 
1947c478bd9Sstevel@tonic-gate 	/*
1957c478bd9Sstevel@tonic-gate 	 * Create an nvlist for our data as none is passed into the function.
1967c478bd9Sstevel@tonic-gate 	 */
1977c478bd9Sstevel@tonic-gate 
1987c478bd9Sstevel@tonic-gate 	if ((rc = nvlist_alloc(&nvlp, NV_UNIQUE_NAME, 0)) != 0) {
1997c478bd9Sstevel@tonic-gate 		DBG0(DBG_ERR, "failed to allocate nvlist\n");
2007c478bd9Sstevel@tonic-gate 		nvlp = NULL;
2017c478bd9Sstevel@tonic-gate 		goto failed;
2027c478bd9Sstevel@tonic-gate 	}
2037c478bd9Sstevel@tonic-gate 
2047c478bd9Sstevel@tonic-gate 	if ((rc = nvlist_add_byte(nvlp, IPPCTL_OP,
2057c478bd9Sstevel@tonic-gate 	    IPPCTL_OP_ACTION_DESTROY)) != 0) {
2067c478bd9Sstevel@tonic-gate 		DBG1(DBG_ERR, "failed to add '%s' to nvlist\n", IPPCTL_OP);
2077c478bd9Sstevel@tonic-gate 		goto failed;
2087c478bd9Sstevel@tonic-gate 	}
2097c478bd9Sstevel@tonic-gate 
2107c478bd9Sstevel@tonic-gate 	if ((rc = nvlist_add_string(nvlp, IPPCTL_ANAME, (char *)aname)) != 0) {
2117c478bd9Sstevel@tonic-gate 		DBG1(DBG_ERR, "failed to add '%s' to nvlist\n", IPPCTL_ANAME);
2127c478bd9Sstevel@tonic-gate 		goto failed;
2137c478bd9Sstevel@tonic-gate 	}
2147c478bd9Sstevel@tonic-gate 
2157c478bd9Sstevel@tonic-gate 	if ((rc = nvlist_add_uint32(nvlp, IPPCTL_FLAGS, flags)) != 0) {
2167c478bd9Sstevel@tonic-gate 		DBG1(DBG_ERR, "failed to add '%s' to nvlist\n", IPPCTL_FLAGS);
2177c478bd9Sstevel@tonic-gate 		goto failed;
2187c478bd9Sstevel@tonic-gate 	}
2197c478bd9Sstevel@tonic-gate 
2207c478bd9Sstevel@tonic-gate 	/*
2217c478bd9Sstevel@tonic-gate 	 * Talk to the kernel.
2227c478bd9Sstevel@tonic-gate 	 */
2237c478bd9Sstevel@tonic-gate 
2247c478bd9Sstevel@tonic-gate 	return (dispatch(&nvlp, NULL, NULL));
2257c478bd9Sstevel@tonic-gate failed:
226*aab83bb8SJosef 'Jeff' Sipek 	nvlist_free(nvlp);
2277c478bd9Sstevel@tonic-gate 	errno = rc;
2287c478bd9Sstevel@tonic-gate 	return (-1);
2297c478bd9Sstevel@tonic-gate }
2307c478bd9Sstevel@tonic-gate #undef	__FN__
2317c478bd9Sstevel@tonic-gate 
2327c478bd9Sstevel@tonic-gate #define	__FN__	"ipp_action_modify"
2337c478bd9Sstevel@tonic-gate int
ipp_action_modify(const char * aname,nvlist_t ** nvlpp,ipp_flags_t flags)2347c478bd9Sstevel@tonic-gate ipp_action_modify(
2357c478bd9Sstevel@tonic-gate 	const char	*aname,
2367c478bd9Sstevel@tonic-gate 	nvlist_t	**nvlpp,
2377c478bd9Sstevel@tonic-gate 	ipp_flags_t	flags)
2387c478bd9Sstevel@tonic-gate {
2397c478bd9Sstevel@tonic-gate 	nvlist_t	*nvlp;
2407c478bd9Sstevel@tonic-gate 	int		rc;
2417c478bd9Sstevel@tonic-gate 
2427c478bd9Sstevel@tonic-gate 	/*
2437c478bd9Sstevel@tonic-gate 	 * Sanity check the arguments.
2447c478bd9Sstevel@tonic-gate 	 */
2457c478bd9Sstevel@tonic-gate 
2467c478bd9Sstevel@tonic-gate 	if (nvlpp == NULL || aname == NULL) {
2477c478bd9Sstevel@tonic-gate 		DBG0(DBG_ERR, "bad argument\n");
2487c478bd9Sstevel@tonic-gate 		errno = EINVAL;
2497c478bd9Sstevel@tonic-gate 		return (-1);
2507c478bd9Sstevel@tonic-gate 	}
2517c478bd9Sstevel@tonic-gate 
2527c478bd9Sstevel@tonic-gate 	/*
2537c478bd9Sstevel@tonic-gate 	 * Add our data to the nvlist.
2547c478bd9Sstevel@tonic-gate 	 */
2557c478bd9Sstevel@tonic-gate 
2567c478bd9Sstevel@tonic-gate 	nvlp = *nvlpp;
2577c478bd9Sstevel@tonic-gate 	if ((rc = nvlist_add_byte(nvlp, IPPCTL_OP,
2587c478bd9Sstevel@tonic-gate 	    IPPCTL_OP_ACTION_MODIFY)) != 0) {
2597c478bd9Sstevel@tonic-gate 		DBG1(DBG_ERR, "failed to add '%s' to nvlist\n", IPPCTL_OP);
2607c478bd9Sstevel@tonic-gate 		goto failed;
2617c478bd9Sstevel@tonic-gate 	}
2627c478bd9Sstevel@tonic-gate 
2637c478bd9Sstevel@tonic-gate 	if ((rc = nvlist_add_string(nvlp, IPPCTL_ANAME, (char *)aname)) != 0) {
2647c478bd9Sstevel@tonic-gate 		DBG1(DBG_ERR, "failed to add '%s' to nvlist\n", IPPCTL_ANAME);
2657c478bd9Sstevel@tonic-gate 		goto failed;
2667c478bd9Sstevel@tonic-gate 	}
2677c478bd9Sstevel@tonic-gate 
2687c478bd9Sstevel@tonic-gate 	if ((rc = nvlist_add_uint32(nvlp, IPPCTL_FLAGS, flags)) != 0) {
2697c478bd9Sstevel@tonic-gate 		DBG1(DBG_ERR, "failed to add '%s' to nvlist\n", IPPCTL_FLAGS);
2707c478bd9Sstevel@tonic-gate 		goto failed;
2717c478bd9Sstevel@tonic-gate 	}
2727c478bd9Sstevel@tonic-gate 
2737c478bd9Sstevel@tonic-gate 	/*
2747c478bd9Sstevel@tonic-gate 	 * Talk to the kernel.
2757c478bd9Sstevel@tonic-gate 	 */
2767c478bd9Sstevel@tonic-gate 
2777c478bd9Sstevel@tonic-gate 	return (dispatch(nvlpp, nvlist_callback, (void *)nvlpp));
2787c478bd9Sstevel@tonic-gate failed:
2797c478bd9Sstevel@tonic-gate 	errno = rc;
2807c478bd9Sstevel@tonic-gate 	return (-1);
2817c478bd9Sstevel@tonic-gate }
2827c478bd9Sstevel@tonic-gate #undef	__FN__
2837c478bd9Sstevel@tonic-gate 
2847c478bd9Sstevel@tonic-gate #define	__FN__	"ipp_action_info"
2857c478bd9Sstevel@tonic-gate int
ipp_action_info(const char * aname,int (* fn)(nvlist_t *,void *),void * arg,ipp_flags_t flags)2867c478bd9Sstevel@tonic-gate ipp_action_info(
2877c478bd9Sstevel@tonic-gate 	const char	*aname,
2887c478bd9Sstevel@tonic-gate 	int		(*fn)(nvlist_t *, void *),
2897c478bd9Sstevel@tonic-gate 	void		*arg,
2907c478bd9Sstevel@tonic-gate 	ipp_flags_t	flags)
2917c478bd9Sstevel@tonic-gate {
2927c478bd9Sstevel@tonic-gate 	nvlist_t	*nvlp;
2937c478bd9Sstevel@tonic-gate 	int		rc;
2947c478bd9Sstevel@tonic-gate 
2957c478bd9Sstevel@tonic-gate 	/*
2967c478bd9Sstevel@tonic-gate 	 * Sanity check the arguments.
2977c478bd9Sstevel@tonic-gate 	 */
2987c478bd9Sstevel@tonic-gate 
2997c478bd9Sstevel@tonic-gate 	if (aname == NULL || fn == NULL) {
3007c478bd9Sstevel@tonic-gate 		DBG0(DBG_ERR, "bad argument\n");
3017c478bd9Sstevel@tonic-gate 		errno = EINVAL;
3027c478bd9Sstevel@tonic-gate 		return (-1);
3037c478bd9Sstevel@tonic-gate 	}
3047c478bd9Sstevel@tonic-gate 
3057c478bd9Sstevel@tonic-gate 	/*
3067c478bd9Sstevel@tonic-gate 	 * Create an nvlist for our data.
3077c478bd9Sstevel@tonic-gate 	 */
3087c478bd9Sstevel@tonic-gate 
3097c478bd9Sstevel@tonic-gate 	if ((rc = nvlist_alloc(&nvlp, NV_UNIQUE_NAME, 0)) != 0) {
3107c478bd9Sstevel@tonic-gate 		DBG0(DBG_ERR, "failed to allocate nvlist\n");
3117c478bd9Sstevel@tonic-gate 		nvlp = NULL;
3127c478bd9Sstevel@tonic-gate 	}
3137c478bd9Sstevel@tonic-gate 
3147c478bd9Sstevel@tonic-gate 	if ((rc = nvlist_add_byte(nvlp, IPPCTL_OP,
3157c478bd9Sstevel@tonic-gate 	    IPPCTL_OP_ACTION_INFO)) != 0) {
3167c478bd9Sstevel@tonic-gate 		DBG1(DBG_ERR, "failed to add '%s' to nvlist\n", IPPCTL_OP);
3177c478bd9Sstevel@tonic-gate 		goto failed;
3187c478bd9Sstevel@tonic-gate 	}
3197c478bd9Sstevel@tonic-gate 
3207c478bd9Sstevel@tonic-gate 	if ((rc = nvlist_add_string(nvlp, IPPCTL_ANAME, (char *)aname)) != 0) {
3217c478bd9Sstevel@tonic-gate 		DBG1(DBG_ERR, "failed to add '%s' to nvlist\n", IPPCTL_ANAME);
3227c478bd9Sstevel@tonic-gate 		goto failed;
3237c478bd9Sstevel@tonic-gate 	}
3247c478bd9Sstevel@tonic-gate 
3257c478bd9Sstevel@tonic-gate 	if ((rc = nvlist_add_uint32(nvlp, IPPCTL_FLAGS, flags)) != 0) {
3267c478bd9Sstevel@tonic-gate 		DBG1(DBG_ERR, "failed to add '%s' to nvlist\n", IPPCTL_FLAGS);
3277c478bd9Sstevel@tonic-gate 		goto failed;
3287c478bd9Sstevel@tonic-gate 	}
3297c478bd9Sstevel@tonic-gate 
3307c478bd9Sstevel@tonic-gate 	/*
3317c478bd9Sstevel@tonic-gate 	 * Talk to the kernel.
3327c478bd9Sstevel@tonic-gate 	 */
3337c478bd9Sstevel@tonic-gate 
3347c478bd9Sstevel@tonic-gate 	return (dispatch(&nvlp, fn, arg));
3357c478bd9Sstevel@tonic-gate failed:
336*aab83bb8SJosef 'Jeff' Sipek 	nvlist_free(nvlp);
3377c478bd9Sstevel@tonic-gate 	errno = rc;
3387c478bd9Sstevel@tonic-gate 	return (-1);
3397c478bd9Sstevel@tonic-gate }
3407c478bd9Sstevel@tonic-gate #undef	__FN__
3417c478bd9Sstevel@tonic-gate 
3427c478bd9Sstevel@tonic-gate #define	__FN__	"ipp_action_mod"
3437c478bd9Sstevel@tonic-gate int
ipp_action_mod(const char * aname,char ** modnamep)3447c478bd9Sstevel@tonic-gate ipp_action_mod(
3457c478bd9Sstevel@tonic-gate 	const char	*aname,
3467c478bd9Sstevel@tonic-gate 	char		**modnamep)
3477c478bd9Sstevel@tonic-gate {
3487c478bd9Sstevel@tonic-gate 	nvlist_t	*nvlp;
3497c478bd9Sstevel@tonic-gate 	int		rc;
3507c478bd9Sstevel@tonic-gate 
3517c478bd9Sstevel@tonic-gate 	/*
3527c478bd9Sstevel@tonic-gate 	 * Sanity check the arguments.
3537c478bd9Sstevel@tonic-gate 	 */
3547c478bd9Sstevel@tonic-gate 
3557c478bd9Sstevel@tonic-gate 	if (aname == NULL || modnamep == NULL) {
3567c478bd9Sstevel@tonic-gate 		DBG0(DBG_ERR, "bad argument\n");
3577c478bd9Sstevel@tonic-gate 		errno = EINVAL;
3587c478bd9Sstevel@tonic-gate 		return (-1);
3597c478bd9Sstevel@tonic-gate 	}
3607c478bd9Sstevel@tonic-gate 
3617c478bd9Sstevel@tonic-gate 	/*
3627c478bd9Sstevel@tonic-gate 	 * Create an nvlist for our data.
3637c478bd9Sstevel@tonic-gate 	 */
3647c478bd9Sstevel@tonic-gate 
3657c478bd9Sstevel@tonic-gate 	if ((rc = nvlist_alloc(&nvlp, NV_UNIQUE_NAME, 0)) != 0) {
3667c478bd9Sstevel@tonic-gate 		DBG0(DBG_ERR, "failed to allocate nvlist\n");
3677c478bd9Sstevel@tonic-gate 		nvlp = NULL;
3687c478bd9Sstevel@tonic-gate 		goto failed;
3697c478bd9Sstevel@tonic-gate 	}
3707c478bd9Sstevel@tonic-gate 
3717c478bd9Sstevel@tonic-gate 	if ((rc = nvlist_add_byte(nvlp, IPPCTL_OP,
3727c478bd9Sstevel@tonic-gate 	    IPPCTL_OP_ACTION_MOD)) != 0) {
3737c478bd9Sstevel@tonic-gate 		DBG1(DBG_ERR, "failed to add '%s' to nvlist\n", IPPCTL_OP);
3747c478bd9Sstevel@tonic-gate 		goto failed;
3757c478bd9Sstevel@tonic-gate 	}
3767c478bd9Sstevel@tonic-gate 
3777c478bd9Sstevel@tonic-gate 	if ((rc = nvlist_add_string(nvlp, IPPCTL_ANAME, (char *)aname)) != 0) {
3787c478bd9Sstevel@tonic-gate 		DBG1(DBG_ERR, "failed to add '%s' to nvlist\n", IPPCTL_ANAME);
3797c478bd9Sstevel@tonic-gate 		goto failed;
3807c478bd9Sstevel@tonic-gate 	}
3817c478bd9Sstevel@tonic-gate 
3827c478bd9Sstevel@tonic-gate 	/*
3837c478bd9Sstevel@tonic-gate 	 * Talk to the kernel.
3847c478bd9Sstevel@tonic-gate 	 */
3857c478bd9Sstevel@tonic-gate 
3867c478bd9Sstevel@tonic-gate 	return (dispatch(&nvlp, string_callback, (void *)modnamep));
3877c478bd9Sstevel@tonic-gate failed:
388*aab83bb8SJosef 'Jeff' Sipek 	nvlist_free(nvlp);
3897c478bd9Sstevel@tonic-gate 	errno = rc;
3907c478bd9Sstevel@tonic-gate 	return (-1);
3917c478bd9Sstevel@tonic-gate }
3927c478bd9Sstevel@tonic-gate #undef	__FN__
3937c478bd9Sstevel@tonic-gate 
3947c478bd9Sstevel@tonic-gate #define	__FN__	"ipp_list_mods"
3957c478bd9Sstevel@tonic-gate int
ipp_list_mods(char *** modname_arrayp,int * neltp)3967c478bd9Sstevel@tonic-gate ipp_list_mods(
3977c478bd9Sstevel@tonic-gate 	char		***modname_arrayp,
3987c478bd9Sstevel@tonic-gate 	int		*neltp)
3997c478bd9Sstevel@tonic-gate {
4007c478bd9Sstevel@tonic-gate 	nvlist_t	*nvlp;
4017c478bd9Sstevel@tonic-gate 	array_desc_t	ad;
4027c478bd9Sstevel@tonic-gate 	int		rc;
4037c478bd9Sstevel@tonic-gate 
4047c478bd9Sstevel@tonic-gate 	/*
4057c478bd9Sstevel@tonic-gate 	 * Sanity check the arguments.
4067c478bd9Sstevel@tonic-gate 	 */
4077c478bd9Sstevel@tonic-gate 
4087c478bd9Sstevel@tonic-gate 	if (modname_arrayp == NULL || neltp == NULL) {
4097c478bd9Sstevel@tonic-gate 		DBG0(DBG_ERR, "bad argument");
4107c478bd9Sstevel@tonic-gate 		errno = EINVAL;
4117c478bd9Sstevel@tonic-gate 		return (-1);
4127c478bd9Sstevel@tonic-gate 	}
4137c478bd9Sstevel@tonic-gate 
4147c478bd9Sstevel@tonic-gate 	/*
4157c478bd9Sstevel@tonic-gate 	 * Create an nvlist for our data.
4167c478bd9Sstevel@tonic-gate 	 */
4177c478bd9Sstevel@tonic-gate 
4187c478bd9Sstevel@tonic-gate 	if ((rc = nvlist_alloc(&nvlp, NV_UNIQUE_NAME, 0)) != 0) {
4197c478bd9Sstevel@tonic-gate 		DBG0(DBG_ERR, "failed to allocate nvlist\n");
4207c478bd9Sstevel@tonic-gate 		nvlp = NULL;
4217c478bd9Sstevel@tonic-gate 	}
4227c478bd9Sstevel@tonic-gate 
4237c478bd9Sstevel@tonic-gate 	if ((rc = nvlist_add_byte(nvlp, IPPCTL_OP,
4247c478bd9Sstevel@tonic-gate 	    IPPCTL_OP_LIST_MODS)) != 0) {
4257c478bd9Sstevel@tonic-gate 		DBG1(DBG_ERR, "failed to add '%s' to nvlist\n", IPPCTL_OP);
4267c478bd9Sstevel@tonic-gate 		goto failed;
4277c478bd9Sstevel@tonic-gate 	}
4287c478bd9Sstevel@tonic-gate 
4297c478bd9Sstevel@tonic-gate 	/*
4307c478bd9Sstevel@tonic-gate 	 * Talk to the kernel.
4317c478bd9Sstevel@tonic-gate 	 */
4327c478bd9Sstevel@tonic-gate 
4337c478bd9Sstevel@tonic-gate 	ad.name = IPPCTL_MODNAME_ARRAY;
4347c478bd9Sstevel@tonic-gate 	ad.array = NULL;
4357c478bd9Sstevel@tonic-gate 	ad.nelt = 0;
4367c478bd9Sstevel@tonic-gate 
4377c478bd9Sstevel@tonic-gate 	if ((rc = dispatch(&nvlp, string_array_callback, (void *)&ad)) == 0) {
4387c478bd9Sstevel@tonic-gate 		*modname_arrayp = ad.array;
4397c478bd9Sstevel@tonic-gate 		*neltp = ad.nelt;
4407c478bd9Sstevel@tonic-gate 	}
4417c478bd9Sstevel@tonic-gate 
4427c478bd9Sstevel@tonic-gate 	return (rc);
4437c478bd9Sstevel@tonic-gate failed:
444*aab83bb8SJosef 'Jeff' Sipek 	nvlist_free(nvlp);
4457c478bd9Sstevel@tonic-gate 	errno = rc;
4467c478bd9Sstevel@tonic-gate 	return (-1);
4477c478bd9Sstevel@tonic-gate }
4487c478bd9Sstevel@tonic-gate #undef	__FN__
4497c478bd9Sstevel@tonic-gate 
4507c478bd9Sstevel@tonic-gate #define	__FN__	"ipp_mod_list_actions"
4517c478bd9Sstevel@tonic-gate int
ipp_mod_list_actions(const char * modname,char *** aname_arrayp,int * neltp)4527c478bd9Sstevel@tonic-gate ipp_mod_list_actions(
4537c478bd9Sstevel@tonic-gate 	const char	*modname,
4547c478bd9Sstevel@tonic-gate 	char		***aname_arrayp,
4557c478bd9Sstevel@tonic-gate 	int		*neltp)
4567c478bd9Sstevel@tonic-gate {
4577c478bd9Sstevel@tonic-gate 	nvlist_t	*nvlp;
4587c478bd9Sstevel@tonic-gate 	array_desc_t	ad;
4597c478bd9Sstevel@tonic-gate 	int		rc;
4607c478bd9Sstevel@tonic-gate 
4617c478bd9Sstevel@tonic-gate 	/*
4627c478bd9Sstevel@tonic-gate 	 * Sanity check the arguments.
4637c478bd9Sstevel@tonic-gate 	 */
4647c478bd9Sstevel@tonic-gate 
4657c478bd9Sstevel@tonic-gate 	if (modname == NULL || aname_arrayp == NULL || neltp == NULL) {
4667c478bd9Sstevel@tonic-gate 		DBG0(DBG_ERR, "bad argument");
4677c478bd9Sstevel@tonic-gate 		errno = EINVAL;
4687c478bd9Sstevel@tonic-gate 		return (-1);
4697c478bd9Sstevel@tonic-gate 	}
4707c478bd9Sstevel@tonic-gate 
4717c478bd9Sstevel@tonic-gate 	/*
4727c478bd9Sstevel@tonic-gate 	 * Create an nvlist for our data.
4737c478bd9Sstevel@tonic-gate 	 */
4747c478bd9Sstevel@tonic-gate 
4757c478bd9Sstevel@tonic-gate 	if ((rc = nvlist_alloc(&nvlp, NV_UNIQUE_NAME, 0)) != 0) {
4767c478bd9Sstevel@tonic-gate 		DBG0(DBG_ERR, "failed to allocate nvlist\n");
4777c478bd9Sstevel@tonic-gate 		nvlp = NULL;
4787c478bd9Sstevel@tonic-gate 	}
4797c478bd9Sstevel@tonic-gate 
4807c478bd9Sstevel@tonic-gate 	if ((rc = nvlist_add_byte(nvlp, IPPCTL_OP,
4817c478bd9Sstevel@tonic-gate 	    IPPCTL_OP_MOD_LIST_ACTIONS)) != 0) {
4827c478bd9Sstevel@tonic-gate 		DBG1(DBG_ERR, "failed to add '%s' to nvlist\n", IPPCTL_OP);
4837c478bd9Sstevel@tonic-gate 		goto failed;
4847c478bd9Sstevel@tonic-gate 	}
4857c478bd9Sstevel@tonic-gate 
4867c478bd9Sstevel@tonic-gate 	if ((rc = nvlist_add_string(nvlp, IPPCTL_MODNAME,
4877c478bd9Sstevel@tonic-gate 	    (char *)modname)) != 0) {
4887c478bd9Sstevel@tonic-gate 		DBG1(DBG_ERR, "failed to add '%s' to nvlist\n", IPPCTL_MODNAME);
4897c478bd9Sstevel@tonic-gate 		goto failed;
4907c478bd9Sstevel@tonic-gate 	}
4917c478bd9Sstevel@tonic-gate 
4927c478bd9Sstevel@tonic-gate 	/*
4937c478bd9Sstevel@tonic-gate 	 * Talk to the kernel.
4947c478bd9Sstevel@tonic-gate 	 */
4957c478bd9Sstevel@tonic-gate 
4967c478bd9Sstevel@tonic-gate 	ad.name = IPPCTL_ANAME_ARRAY;
4977c478bd9Sstevel@tonic-gate 	ad.array = NULL;
4987c478bd9Sstevel@tonic-gate 	ad.nelt = 0;
4997c478bd9Sstevel@tonic-gate 
5007c478bd9Sstevel@tonic-gate 	if ((rc = dispatch(&nvlp, string_array_callback, (void *)&ad)) == 0) {
5017c478bd9Sstevel@tonic-gate 		*aname_arrayp = ad.array;
5027c478bd9Sstevel@tonic-gate 		*neltp = ad.nelt;
5037c478bd9Sstevel@tonic-gate 	}
5047c478bd9Sstevel@tonic-gate 
5057c478bd9Sstevel@tonic-gate 	return (rc);
5067c478bd9Sstevel@tonic-gate failed:
507*aab83bb8SJosef 'Jeff' Sipek 	nvlist_free(nvlp);
5087c478bd9Sstevel@tonic-gate 	errno = rc;
5097c478bd9Sstevel@tonic-gate 	return (-1);
5107c478bd9Sstevel@tonic-gate }
5117c478bd9Sstevel@tonic-gate #undef	__FN__
5127c478bd9Sstevel@tonic-gate 
5137c478bd9Sstevel@tonic-gate #define	__FN__	"ipp_free"
5147c478bd9Sstevel@tonic-gate void
ipp_free(char * buf)5157c478bd9Sstevel@tonic-gate ipp_free(
5167c478bd9Sstevel@tonic-gate 	char	*buf)
5177c478bd9Sstevel@tonic-gate {
5187c478bd9Sstevel@tonic-gate 	free(buf);
5197c478bd9Sstevel@tonic-gate }
5207c478bd9Sstevel@tonic-gate #undef	__FN__
5217c478bd9Sstevel@tonic-gate 
5227c478bd9Sstevel@tonic-gate #define	__FN__	"ipp_free_array"
5237c478bd9Sstevel@tonic-gate void
ipp_free_array(char ** array,int nelt)5247c478bd9Sstevel@tonic-gate ipp_free_array(
5257c478bd9Sstevel@tonic-gate 	char	**array,
5267c478bd9Sstevel@tonic-gate 	int	nelt)
5277c478bd9Sstevel@tonic-gate {
5287c478bd9Sstevel@tonic-gate 	int	i;
5297c478bd9Sstevel@tonic-gate 
5307c478bd9Sstevel@tonic-gate 	assert(array[nelt] == NULL);
5317c478bd9Sstevel@tonic-gate 
5327c478bd9Sstevel@tonic-gate 	for (i = 0; i < nelt; i++)
5337c478bd9Sstevel@tonic-gate 		free(array[i]);
5347c478bd9Sstevel@tonic-gate 
5357c478bd9Sstevel@tonic-gate 	free(array);
5367c478bd9Sstevel@tonic-gate }
5377c478bd9Sstevel@tonic-gate #undef	__FN__
5387c478bd9Sstevel@tonic-gate 
5397c478bd9Sstevel@tonic-gate #define	__FN__	"nvlist_callback"
5407c478bd9Sstevel@tonic-gate static int
nvlist_callback(nvlist_t * nvlp,void * arg)5417c478bd9Sstevel@tonic-gate nvlist_callback(
5427c478bd9Sstevel@tonic-gate 	nvlist_t	*nvlp,
5437c478bd9Sstevel@tonic-gate 	void		*arg)
5447c478bd9Sstevel@tonic-gate {
5457c478bd9Sstevel@tonic-gate 	nvlist_t	**nvlpp = (nvlist_t **)arg;
5467c478bd9Sstevel@tonic-gate 	int		rc;
5477c478bd9Sstevel@tonic-gate 
5487c478bd9Sstevel@tonic-gate 	/*
5497c478bd9Sstevel@tonic-gate 	 * Callback function used by ipp_action_create() and
5507c478bd9Sstevel@tonic-gate 	 * ipp_action_modify()
5517c478bd9Sstevel@tonic-gate 	 */
5527c478bd9Sstevel@tonic-gate 
5537c478bd9Sstevel@tonic-gate 	DBG0(DBG_IO, "called\n");
5547c478bd9Sstevel@tonic-gate 
5557c478bd9Sstevel@tonic-gate 	assert(nvlpp != NULL);
5567c478bd9Sstevel@tonic-gate 	assert(*nvlpp == NULL);
5577c478bd9Sstevel@tonic-gate 
5587c478bd9Sstevel@tonic-gate 	/*
5597c478bd9Sstevel@tonic-gate 	 * Duplicate the nvlist and set the given pointer to point at the new
5607c478bd9Sstevel@tonic-gate 	 * copy.
5617c478bd9Sstevel@tonic-gate 	 */
5627c478bd9Sstevel@tonic-gate 
5637c478bd9Sstevel@tonic-gate 	if ((rc = nvlist_dup(nvlp, nvlpp, 0)) != 0) {
5647c478bd9Sstevel@tonic-gate 		DBG0(DBG_ERR, "failed to dup nvlist\n");
5657c478bd9Sstevel@tonic-gate 		errno = rc;
5667c478bd9Sstevel@tonic-gate 		return (-1);
5677c478bd9Sstevel@tonic-gate 	}
5687c478bd9Sstevel@tonic-gate 
5697c478bd9Sstevel@tonic-gate 	return (0);
5707c478bd9Sstevel@tonic-gate }
5717c478bd9Sstevel@tonic-gate #undef	__FN__
5727c478bd9Sstevel@tonic-gate 
5737c478bd9Sstevel@tonic-gate #define	__FN__	"string_callback"
5747c478bd9Sstevel@tonic-gate static int
string_callback(nvlist_t * nvlp,void * arg)5757c478bd9Sstevel@tonic-gate string_callback(
5767c478bd9Sstevel@tonic-gate 	nvlist_t	*nvlp,
5777c478bd9Sstevel@tonic-gate 	void		*arg)
5787c478bd9Sstevel@tonic-gate {
5797c478bd9Sstevel@tonic-gate 	char		**namep = (char **)arg;
5807c478bd9Sstevel@tonic-gate 	char		*name;
5817c478bd9Sstevel@tonic-gate 	char		*ptr;
5827c478bd9Sstevel@tonic-gate 	int		rc;
5837c478bd9Sstevel@tonic-gate 
5847c478bd9Sstevel@tonic-gate 	/*
5857c478bd9Sstevel@tonic-gate 	 * Callback function used by ipp_action_mod()
5867c478bd9Sstevel@tonic-gate 	 */
5877c478bd9Sstevel@tonic-gate 
5887c478bd9Sstevel@tonic-gate 	DBG0(DBG_IO, "called\n");
5897c478bd9Sstevel@tonic-gate 
5907c478bd9Sstevel@tonic-gate 	assert(namep != NULL);
5917c478bd9Sstevel@tonic-gate 
5927c478bd9Sstevel@tonic-gate 	/*
5937c478bd9Sstevel@tonic-gate 	 * Look up the module name from the nvlist.
5947c478bd9Sstevel@tonic-gate 	 */
5957c478bd9Sstevel@tonic-gate 
5967c478bd9Sstevel@tonic-gate 	if ((rc = nvlist_lookup_string(nvlp, IPPCTL_MODNAME, &ptr)) != 0) {
5977c478bd9Sstevel@tonic-gate 		DBG0(DBG_ERR, "failed to find string\n");
5987c478bd9Sstevel@tonic-gate 		errno = rc;
5997c478bd9Sstevel@tonic-gate 		return (-1);
6007c478bd9Sstevel@tonic-gate 	}
6017c478bd9Sstevel@tonic-gate 
6027c478bd9Sstevel@tonic-gate 	/*
6037c478bd9Sstevel@tonic-gate 	 * Allocate a duplicate string.
6047c478bd9Sstevel@tonic-gate 	 */
6057c478bd9Sstevel@tonic-gate 
6067c478bd9Sstevel@tonic-gate 	if ((name = strdup(ptr)) == NULL) {
6077c478bd9Sstevel@tonic-gate 		DBG0(DBG_ERR, "failed to duplicate string\n");
6087c478bd9Sstevel@tonic-gate 		return (-1);
6097c478bd9Sstevel@tonic-gate 	}
6107c478bd9Sstevel@tonic-gate 
6117c478bd9Sstevel@tonic-gate 	/*
6127c478bd9Sstevel@tonic-gate 	 * Set the given pointer to point at the string.
6137c478bd9Sstevel@tonic-gate 	 */
6147c478bd9Sstevel@tonic-gate 
6157c478bd9Sstevel@tonic-gate 	*namep = name;
6167c478bd9Sstevel@tonic-gate 	return (0);
6177c478bd9Sstevel@tonic-gate }
6187c478bd9Sstevel@tonic-gate #undef	__FN__
6197c478bd9Sstevel@tonic-gate 
6207c478bd9Sstevel@tonic-gate #define	__FN__	"string_array_callback"
6217c478bd9Sstevel@tonic-gate static int
string_array_callback(nvlist_t * nvlp,void * arg)6227c478bd9Sstevel@tonic-gate string_array_callback(
6237c478bd9Sstevel@tonic-gate 	nvlist_t	*nvlp,
6247c478bd9Sstevel@tonic-gate 	void		*arg)
6257c478bd9Sstevel@tonic-gate {
6267c478bd9Sstevel@tonic-gate 	array_desc_t	*adp = (array_desc_t *)arg;
6277c478bd9Sstevel@tonic-gate 	char		**dst;
6287c478bd9Sstevel@tonic-gate 	char		**src;
6297c478bd9Sstevel@tonic-gate 	uint_t		nelt;
6307c478bd9Sstevel@tonic-gate 	int		i;
6317c478bd9Sstevel@tonic-gate 	int		rc;
6327c478bd9Sstevel@tonic-gate 
6337c478bd9Sstevel@tonic-gate 	/*
6347c478bd9Sstevel@tonic-gate 	 * Callback function used by ipp_list_mods()
6357c478bd9Sstevel@tonic-gate 	 */
6367c478bd9Sstevel@tonic-gate 
6377c478bd9Sstevel@tonic-gate 	DBG0(DBG_IO, "called\n");
6387c478bd9Sstevel@tonic-gate 
6397c478bd9Sstevel@tonic-gate 	assert(adp != NULL);
6407c478bd9Sstevel@tonic-gate 
6417c478bd9Sstevel@tonic-gate 	/*
6427c478bd9Sstevel@tonic-gate 	 * Look up the module name from the nvlist.
6437c478bd9Sstevel@tonic-gate 	 */
6447c478bd9Sstevel@tonic-gate 
6457c478bd9Sstevel@tonic-gate 	if ((rc = nvlist_lookup_string_array(nvlp, adp->name, &src,
6467c478bd9Sstevel@tonic-gate 	    &nelt)) != 0) {
6477c478bd9Sstevel@tonic-gate 		DBG0(DBG_ERR, "failed to find array\n");
6487c478bd9Sstevel@tonic-gate 		errno = rc;
6497c478bd9Sstevel@tonic-gate 		return (-1);
6507c478bd9Sstevel@tonic-gate 	}
6517c478bd9Sstevel@tonic-gate 
6527c478bd9Sstevel@tonic-gate 	/*
6537c478bd9Sstevel@tonic-gate 	 * Allocate an array.
6547c478bd9Sstevel@tonic-gate 	 */
6557c478bd9Sstevel@tonic-gate 
6567c478bd9Sstevel@tonic-gate 	if ((dst = malloc((nelt + 1) * sizeof (char *))) == NULL) {
6577c478bd9Sstevel@tonic-gate 		DBG0(DBG_ERR, "failed to allocate new array\n");
6587c478bd9Sstevel@tonic-gate 		return (-1);
6597c478bd9Sstevel@tonic-gate 	}
6607c478bd9Sstevel@tonic-gate 
6617c478bd9Sstevel@tonic-gate 	/*
6627c478bd9Sstevel@tonic-gate 	 * For each string in the array, allocate a new buffer and copy
6637c478bd9Sstevel@tonic-gate 	 * the string into it.
6647c478bd9Sstevel@tonic-gate 	 */
6657c478bd9Sstevel@tonic-gate 
6667c478bd9Sstevel@tonic-gate 	for (i = 0; i < nelt; i++) {
6677c478bd9Sstevel@tonic-gate 		if ((dst[i] = strdup(src[i])) == NULL) {
6687c478bd9Sstevel@tonic-gate 			while (--i >= 0) {
6697c478bd9Sstevel@tonic-gate 				free(dst[i]);
6707c478bd9Sstevel@tonic-gate 			}
6717c478bd9Sstevel@tonic-gate 			free(dst);
6727c478bd9Sstevel@tonic-gate 			DBG0(DBG_ERR, "failed to duplicate array\n");
6737c478bd9Sstevel@tonic-gate 			return (-1);
6747c478bd9Sstevel@tonic-gate 		}
6757c478bd9Sstevel@tonic-gate 	}
6767c478bd9Sstevel@tonic-gate 	dst[nelt] = NULL;
6777c478bd9Sstevel@tonic-gate 
6787c478bd9Sstevel@tonic-gate 	/*
6797c478bd9Sstevel@tonic-gate 	 * Set the information to be passed back.
6807c478bd9Sstevel@tonic-gate 	 */
6817c478bd9Sstevel@tonic-gate 
6827c478bd9Sstevel@tonic-gate 	adp->array = dst;
6837c478bd9Sstevel@tonic-gate 	adp->nelt = nelt;
6847c478bd9Sstevel@tonic-gate 
6857c478bd9Sstevel@tonic-gate 	return (0);
6867c478bd9Sstevel@tonic-gate }
6877c478bd9Sstevel@tonic-gate #undef	__FN__
6887c478bd9Sstevel@tonic-gate 
6897c478bd9Sstevel@tonic-gate #define	__FN__	"dispatch"
6907c478bd9Sstevel@tonic-gate static int
dispatch(nvlist_t ** nvlpp,int (* fn)(nvlist_t *,void *),void * arg)6917c478bd9Sstevel@tonic-gate dispatch(
6927c478bd9Sstevel@tonic-gate 	nvlist_t	**nvlpp,
6937c478bd9Sstevel@tonic-gate 	int		(*fn)(nvlist_t *, void *),
6947c478bd9Sstevel@tonic-gate 	void		*arg)
6957c478bd9Sstevel@tonic-gate {
6967c478bd9Sstevel@tonic-gate 	char		*cbuf = NULL;
6977c478bd9Sstevel@tonic-gate 	char		*dbuf = NULL;
6987c478bd9Sstevel@tonic-gate 	size_t		cbuflen = 0;
6997c478bd9Sstevel@tonic-gate 	size_t		dbuflen = 0;
7007c478bd9Sstevel@tonic-gate 	size_t		thisbuflen = 0;
7017c478bd9Sstevel@tonic-gate 	size_t		nextbuflen = 0;
7027c478bd9Sstevel@tonic-gate 	int		rc;
7037c478bd9Sstevel@tonic-gate 	ippctl_ioctl_t	iioc;
7047c478bd9Sstevel@tonic-gate 	int		fd;
7057c478bd9Sstevel@tonic-gate 	nvlist_t	*cnvlp;
7067c478bd9Sstevel@tonic-gate 	nvlist_t	*dnvlp = NULL;
7077c478bd9Sstevel@tonic-gate 	int		count;
7087c478bd9Sstevel@tonic-gate 	int		rval;
7097c478bd9Sstevel@tonic-gate 
7107c478bd9Sstevel@tonic-gate 	/*
7117c478bd9Sstevel@tonic-gate 	 * Sanity check the 'command' nvlist.
7127c478bd9Sstevel@tonic-gate 	 */
7137c478bd9Sstevel@tonic-gate 
7147c478bd9Sstevel@tonic-gate 	cnvlp = *nvlpp;
7157c478bd9Sstevel@tonic-gate 	if (cnvlp == NULL) {
7167c478bd9Sstevel@tonic-gate 		rc = EINVAL;
7177c478bd9Sstevel@tonic-gate 		return (-1);
7187c478bd9Sstevel@tonic-gate 	}
7197c478bd9Sstevel@tonic-gate 
7207c478bd9Sstevel@tonic-gate 	/*
7217c478bd9Sstevel@tonic-gate 	 * Pack the nvlist and then free the original.
7227c478bd9Sstevel@tonic-gate 	 */
7237c478bd9Sstevel@tonic-gate 
7247c478bd9Sstevel@tonic-gate 	if ((rc = nvlist_pack(cnvlp, &cbuf, &cbuflen, NV_ENCODE_NATIVE,
7257c478bd9Sstevel@tonic-gate 	    0)) != 0) {
7267c478bd9Sstevel@tonic-gate 		DBG0(DBG_ERR, "failed to pack nvlist\n");
7277c478bd9Sstevel@tonic-gate 		nvlist_free(cnvlp);
7287c478bd9Sstevel@tonic-gate 		errno = rc;
7297c478bd9Sstevel@tonic-gate 		return (-1);
7307c478bd9Sstevel@tonic-gate 	}
7317c478bd9Sstevel@tonic-gate 	nvlist_free(cnvlp);
7327c478bd9Sstevel@tonic-gate 	*nvlpp = NULL;
7337c478bd9Sstevel@tonic-gate 
7347c478bd9Sstevel@tonic-gate 	/*
7357c478bd9Sstevel@tonic-gate 	 * Open the control device node.
7367c478bd9Sstevel@tonic-gate 	 */
7377c478bd9Sstevel@tonic-gate 
7387c478bd9Sstevel@tonic-gate 	DBG1(DBG_IO, "opening %s\n", IPPCTL_DEVICE);
7397c478bd9Sstevel@tonic-gate 	if ((fd = open(IPPCTL_DEVICE, O_RDWR | O_NOCTTY)) == -1) {
7407c478bd9Sstevel@tonic-gate 		DBG1(DBG_ERR, "failed to open %s\n", IPPCTL_DEVICE);
7417c478bd9Sstevel@tonic-gate 		goto command_failed;
7427c478bd9Sstevel@tonic-gate 	}
7437c478bd9Sstevel@tonic-gate 
7447c478bd9Sstevel@tonic-gate 	/*
7457c478bd9Sstevel@tonic-gate 	 * Set up an ioctl structure to point at the packed nvlist.
7467c478bd9Sstevel@tonic-gate 	 */
7477c478bd9Sstevel@tonic-gate 
7487c478bd9Sstevel@tonic-gate 	iioc.ii_buf = cbuf;
7497c478bd9Sstevel@tonic-gate 	iioc.ii_buflen = cbuflen;
7507c478bd9Sstevel@tonic-gate 
7517c478bd9Sstevel@tonic-gate 	/*
7527c478bd9Sstevel@tonic-gate 	 * Issue a command ioctl, passing the ioctl structure.
7537c478bd9Sstevel@tonic-gate 	 */
7547c478bd9Sstevel@tonic-gate 
7557c478bd9Sstevel@tonic-gate 	DBG0(DBG_IO, "command\n");
7567c478bd9Sstevel@tonic-gate 	if ((rc = ioctl(fd, IPPCTL_CMD, &iioc)) < 0) {
7577c478bd9Sstevel@tonic-gate 		DBG0(DBG_ERR, "command ioctl failed\n");
7587c478bd9Sstevel@tonic-gate 		goto command_failed;
7597c478bd9Sstevel@tonic-gate 	}
7607c478bd9Sstevel@tonic-gate 
7617c478bd9Sstevel@tonic-gate 	/*
7627c478bd9Sstevel@tonic-gate 	 * Get back the length of the first data buffer.
7637c478bd9Sstevel@tonic-gate 	 */
7647c478bd9Sstevel@tonic-gate 
7657c478bd9Sstevel@tonic-gate 	if ((nextbuflen = (size_t)rc) == 0) {
7667c478bd9Sstevel@tonic-gate 		DBG0(DBG_ERR, "no data buffer\n");
7677c478bd9Sstevel@tonic-gate 		errno = EPROTO;
7687c478bd9Sstevel@tonic-gate 		goto command_failed;
7697c478bd9Sstevel@tonic-gate 	}
7707c478bd9Sstevel@tonic-gate 
7717c478bd9Sstevel@tonic-gate 	/*
7727c478bd9Sstevel@tonic-gate 	 * Try to re-use the command buffer as the first data buffer.
7737c478bd9Sstevel@tonic-gate 	 */
7747c478bd9Sstevel@tonic-gate 
7757c478bd9Sstevel@tonic-gate 	dbuf = cbuf;
7767c478bd9Sstevel@tonic-gate 	thisbuflen = cbuflen;
7777c478bd9Sstevel@tonic-gate 
7787c478bd9Sstevel@tonic-gate 	count = 0;
7797c478bd9Sstevel@tonic-gate 	while (nextbuflen != 0) {
7807c478bd9Sstevel@tonic-gate 		dbuflen = nextbuflen;
7817c478bd9Sstevel@tonic-gate 
7827c478bd9Sstevel@tonic-gate 		/*
7837c478bd9Sstevel@tonic-gate 		 * Check whether the buffer we have is long enough for the
7847c478bd9Sstevel@tonic-gate 		 * next lot of data. If it isn't, allocate a new one of
7857c478bd9Sstevel@tonic-gate 		 * the appropriate length.
7867c478bd9Sstevel@tonic-gate 		 */
7877c478bd9Sstevel@tonic-gate 
7887c478bd9Sstevel@tonic-gate 		if (nextbuflen > thisbuflen) {
7897c478bd9Sstevel@tonic-gate 			if ((dbuf = realloc(dbuf, nextbuflen)) == NULL) {
7907c478bd9Sstevel@tonic-gate 				DBG0(DBG_ERR,
7917c478bd9Sstevel@tonic-gate 				    "failed to allocate data buffer\n");
7927c478bd9Sstevel@tonic-gate 				goto data_failed;
7937c478bd9Sstevel@tonic-gate 			}
7947c478bd9Sstevel@tonic-gate 			thisbuflen = nextbuflen;
7957c478bd9Sstevel@tonic-gate 		}
7967c478bd9Sstevel@tonic-gate 
7977c478bd9Sstevel@tonic-gate 		/*
7987c478bd9Sstevel@tonic-gate 		 * Set up an ioctl structure to point at the data buffer.
7997c478bd9Sstevel@tonic-gate 		 */
8007c478bd9Sstevel@tonic-gate 
8017c478bd9Sstevel@tonic-gate 		iioc.ii_buf = dbuf;
8027c478bd9Sstevel@tonic-gate 		iioc.ii_buflen = dbuflen;
8037c478bd9Sstevel@tonic-gate 
8047c478bd9Sstevel@tonic-gate 		/*
8057c478bd9Sstevel@tonic-gate 		 * Issue a data ioctl, passing the ioctl structure.
8067c478bd9Sstevel@tonic-gate 		 */
8077c478bd9Sstevel@tonic-gate 
8087c478bd9Sstevel@tonic-gate 		DBG2(DBG_IO, "data[%d]: length = %d\n", count, dbuflen);
8097c478bd9Sstevel@tonic-gate 		if ((rc = ioctl(fd, IPPCTL_DATA, &iioc)) < 0) {
8107c478bd9Sstevel@tonic-gate 			DBG0(DBG_ERR, "data ioctl failed\n");
8117c478bd9Sstevel@tonic-gate 			goto data_failed;
8127c478bd9Sstevel@tonic-gate 		}
8137c478bd9Sstevel@tonic-gate 
8147c478bd9Sstevel@tonic-gate 		/*
8157c478bd9Sstevel@tonic-gate 		 * Get the length of the *next* data buffer, if there is
8167c478bd9Sstevel@tonic-gate 		 * one.
8177c478bd9Sstevel@tonic-gate 		 */
8187c478bd9Sstevel@tonic-gate 
8197c478bd9Sstevel@tonic-gate 		nextbuflen = (size_t)rc;
8207c478bd9Sstevel@tonic-gate 		DBG1(DBG_IO, "nextbuflen = %d\n", nextbuflen);
8217c478bd9Sstevel@tonic-gate 
8227c478bd9Sstevel@tonic-gate 		/*
8237c478bd9Sstevel@tonic-gate 		 * Unpack the nvlist that the current data buffer should
8247c478bd9Sstevel@tonic-gate 		 * now contain.
8257c478bd9Sstevel@tonic-gate 		 */
8267c478bd9Sstevel@tonic-gate 
8277c478bd9Sstevel@tonic-gate 		if ((rc = nvlist_unpack(dbuf, dbuflen, &dnvlp, 0)) != 0) {
8287c478bd9Sstevel@tonic-gate 			DBG0(DBG_ERR, "failed to unpack nvlist\n");
8297c478bd9Sstevel@tonic-gate 			errno = rc;
8307c478bd9Sstevel@tonic-gate 			goto data_failed;
8317c478bd9Sstevel@tonic-gate 		}
8327c478bd9Sstevel@tonic-gate 
8337c478bd9Sstevel@tonic-gate 		/*
8347c478bd9Sstevel@tonic-gate 		 * The first data buffer should contain the kernel function's
8357c478bd9Sstevel@tonic-gate 		 * return code. Subsequent buffers contain nvlists which
8367c478bd9Sstevel@tonic-gate 		 * should be passed to the given callback function.
8377c478bd9Sstevel@tonic-gate 		 */
8387c478bd9Sstevel@tonic-gate 
8397c478bd9Sstevel@tonic-gate 		if (count == 0) {
8407c478bd9Sstevel@tonic-gate 			if ((rc = nvlist_lookup_int32(dnvlp, IPPCTL_RC,
8417c478bd9Sstevel@tonic-gate 			    &rval)) != 0) {
8427c478bd9Sstevel@tonic-gate 				DBG0(DBG_ERR, "failed to find return code\n");
8437c478bd9Sstevel@tonic-gate 				nvlist_free(dnvlp);
8447c478bd9Sstevel@tonic-gate 				errno = rc;
8457c478bd9Sstevel@tonic-gate 				goto data_failed;
8467c478bd9Sstevel@tonic-gate 			}
8477c478bd9Sstevel@tonic-gate 		} else {
8487c478bd9Sstevel@tonic-gate 			if (fn != NULL)
8497c478bd9Sstevel@tonic-gate 				if (fn(dnvlp, arg) != 0) {
8507c478bd9Sstevel@tonic-gate 
8517c478bd9Sstevel@tonic-gate 					/*
8527c478bd9Sstevel@tonic-gate 					 * The callback function returned
8537c478bd9Sstevel@tonic-gate 					 * a non-zero value. Abort any further
8547c478bd9Sstevel@tonic-gate 					 * data collection.
8557c478bd9Sstevel@tonic-gate 					 */
8567c478bd9Sstevel@tonic-gate 
8577c478bd9Sstevel@tonic-gate 					nvlist_free(dnvlp);
8587c478bd9Sstevel@tonic-gate 					free(dbuf);
8597c478bd9Sstevel@tonic-gate 				}
8607c478bd9Sstevel@tonic-gate 		}
8617c478bd9Sstevel@tonic-gate 
8627c478bd9Sstevel@tonic-gate 		/*
8637c478bd9Sstevel@tonic-gate 		 * Free the nvlist now that we have extracted the return
8647c478bd9Sstevel@tonic-gate 		 * code or called the callback function.
8657c478bd9Sstevel@tonic-gate 		 */
8667c478bd9Sstevel@tonic-gate 
8677c478bd9Sstevel@tonic-gate 		nvlist_free(dnvlp);
8687c478bd9Sstevel@tonic-gate 		dnvlp = NULL;
8697c478bd9Sstevel@tonic-gate 
8707c478bd9Sstevel@tonic-gate 		count++;
8717c478bd9Sstevel@tonic-gate 	}
8727c478bd9Sstevel@tonic-gate 
8737c478bd9Sstevel@tonic-gate 	/*
8747c478bd9Sstevel@tonic-gate 	 * Free the data buffer as data collection is now complete.
8757c478bd9Sstevel@tonic-gate 	 */
8767c478bd9Sstevel@tonic-gate 
8777c478bd9Sstevel@tonic-gate 	free(dbuf);
8787c478bd9Sstevel@tonic-gate 
8797c478bd9Sstevel@tonic-gate 	/*
8807c478bd9Sstevel@tonic-gate 	 * Close the control device.
8817c478bd9Sstevel@tonic-gate 	 */
8827c478bd9Sstevel@tonic-gate 
8837c478bd9Sstevel@tonic-gate 	(void) close(fd);
8847c478bd9Sstevel@tonic-gate 
8857c478bd9Sstevel@tonic-gate 	/*
8867c478bd9Sstevel@tonic-gate 	 * If the kernel returned an error, we should return an error.
8877c478bd9Sstevel@tonic-gate 	 * and set errno.
8887c478bd9Sstevel@tonic-gate 	 */
8897c478bd9Sstevel@tonic-gate 
8907c478bd9Sstevel@tonic-gate 	if (rval != 0) {
8917c478bd9Sstevel@tonic-gate 		DBG1(DBG_IO, "kernel return code = %d\n", rval);
8927c478bd9Sstevel@tonic-gate 		errno = rval;
8937c478bd9Sstevel@tonic-gate 		return (-1);
8947c478bd9Sstevel@tonic-gate 	}
8957c478bd9Sstevel@tonic-gate 
8967c478bd9Sstevel@tonic-gate 	return (0);
8977c478bd9Sstevel@tonic-gate 
8987c478bd9Sstevel@tonic-gate command_failed:
8997c478bd9Sstevel@tonic-gate 	free(cbuf);
9007c478bd9Sstevel@tonic-gate 	if (fd != -1)
9017c478bd9Sstevel@tonic-gate 		(void) close(fd);
9027c478bd9Sstevel@tonic-gate 	return (-1);
9037c478bd9Sstevel@tonic-gate 
9047c478bd9Sstevel@tonic-gate data_failed:
9057c478bd9Sstevel@tonic-gate 	if (dbuf != NULL)
9067c478bd9Sstevel@tonic-gate 		free(dbuf);
9077c478bd9Sstevel@tonic-gate 	(void) close(fd);
9087c478bd9Sstevel@tonic-gate 	return (-1);
9097c478bd9Sstevel@tonic-gate }
9107c478bd9Sstevel@tonic-gate #undef	__FN__
911