10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
51957Sdnielsen  * Common Development and Distribution License (the "License").
61957Sdnielsen  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
21133Sdmick 
22133Sdmick /* Portions Copyright 2005 Cyril Plisko */
23133Sdmick 
240Sstevel@tonic-gate /*
25*6852Scth  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
260Sstevel@tonic-gate  * Use is subject to license terms.
270Sstevel@tonic-gate  */
280Sstevel@tonic-gate 
290Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
300Sstevel@tonic-gate 
310Sstevel@tonic-gate #include <errno.h>
320Sstevel@tonic-gate #include <stdio.h>
330Sstevel@tonic-gate #include <stdlib.h>
340Sstevel@tonic-gate #include <string.h>
350Sstevel@tonic-gate #include <locale.h>
360Sstevel@tonic-gate #include <langinfo.h>
370Sstevel@tonic-gate #include <time.h>
380Sstevel@tonic-gate 
390Sstevel@tonic-gate #if	!defined(DEBUG)
400Sstevel@tonic-gate #define	NDEBUG	1
410Sstevel@tonic-gate #else
420Sstevel@tonic-gate #undef	NDEBUG
430Sstevel@tonic-gate #endif
440Sstevel@tonic-gate 
450Sstevel@tonic-gate #include <assert.h>
460Sstevel@tonic-gate #include <sys/types.h>
470Sstevel@tonic-gate #include <sys/stat.h>
480Sstevel@tonic-gate #include <sys/param.h>
490Sstevel@tonic-gate #include <dlfcn.h>
500Sstevel@tonic-gate #include <synch.h>
510Sstevel@tonic-gate #include <sys/systeminfo.h>
520Sstevel@tonic-gate #include <sys/sunddi.h>
530Sstevel@tonic-gate #include <libdevinfo.h>
540Sstevel@tonic-gate #include <unistd.h>
550Sstevel@tonic-gate #include <stdarg.h>
560Sstevel@tonic-gate #include <limits.h>
570Sstevel@tonic-gate #include <ftw.h>
580Sstevel@tonic-gate #include <ctype.h>
590Sstevel@tonic-gate 
600Sstevel@tonic-gate #define	CFGA_PLUGIN_LIB
610Sstevel@tonic-gate #include <config_admin.h>
620Sstevel@tonic-gate 
630Sstevel@tonic-gate /* Limit size of sysinfo return */
640Sstevel@tonic-gate #define	SYSINFO_LENGTH	256
650Sstevel@tonic-gate 
660Sstevel@tonic-gate /*
670Sstevel@tonic-gate  * Attachment point specifier types.
680Sstevel@tonic-gate  */
690Sstevel@tonic-gate typedef enum {
700Sstevel@tonic-gate 	UNKNOWN_AP,
710Sstevel@tonic-gate 	LOGICAL_LINK_AP,
720Sstevel@tonic-gate 	LOGICAL_DRV_AP,
730Sstevel@tonic-gate 	PHYSICAL_AP,
740Sstevel@tonic-gate 	AP_TYPE
750Sstevel@tonic-gate } cfga_ap_types_t;
760Sstevel@tonic-gate 
770Sstevel@tonic-gate static char *listopt_array[] = {
780Sstevel@tonic-gate 
790Sstevel@tonic-gate #define	LISTOPT_CLASS	0
800Sstevel@tonic-gate 	"class",
810Sstevel@tonic-gate 	NULL
820Sstevel@tonic-gate };
830Sstevel@tonic-gate 
840Sstevel@tonic-gate typedef struct {
850Sstevel@tonic-gate 	int v_min;	/* Min acceptable version */
860Sstevel@tonic-gate 	int v_max;	/* Max acceptable version */
870Sstevel@tonic-gate } vers_req_t;
880Sstevel@tonic-gate 
890Sstevel@tonic-gate #define	INVALID_VERSION		-1
900Sstevel@tonic-gate #define	VALID_HSL_VERS(v)	(((v) >= CFGA_HSL_V1) && \
910Sstevel@tonic-gate 				((v) <= CFGA_HSL_VERS))
920Sstevel@tonic-gate 
930Sstevel@tonic-gate /*
940Sstevel@tonic-gate  * Incomplete definition
950Sstevel@tonic-gate  */
960Sstevel@tonic-gate struct cfga_vers_ops;
970Sstevel@tonic-gate 
980Sstevel@tonic-gate /*
990Sstevel@tonic-gate  * Structure that contains plugin library information.
1000Sstevel@tonic-gate  */
1010Sstevel@tonic-gate typedef struct plugin_lib {
1020Sstevel@tonic-gate 	struct	plugin_lib *next;	/* pointer to next */
1030Sstevel@tonic-gate 	mutex_t	lock;			/* protects refcnt */
1040Sstevel@tonic-gate 	int	refcnt;			/* reference count */
1050Sstevel@tonic-gate 	void	*handle;		/* handle from dlopen */
1060Sstevel@tonic-gate 	cfga_err_t	(*cfga_change_state_p)();
1070Sstevel@tonic-gate 	cfga_err_t	(*cfga_private_func_p)();
1080Sstevel@tonic-gate 	cfga_err_t	(*cfga_test_p)();
1090Sstevel@tonic-gate 	cfga_err_t	(*cfga_stat_p)();
1100Sstevel@tonic-gate 	cfga_err_t	(*cfga_list_p)();
1110Sstevel@tonic-gate 	cfga_err_t	(*cfga_help_p)();
1120Sstevel@tonic-gate 	int		(*cfga_ap_id_cmp_p)();
1130Sstevel@tonic-gate 	cfga_err_t	(*cfga_list_ext_p)();	/* For V2 plug-ins only */
1140Sstevel@tonic-gate 
1150Sstevel@tonic-gate 	int		plugin_vers;	/* actual plugin version */
1160Sstevel@tonic-gate 	struct cfga_vers_ops *vers_ops;	/* version dependant routines */
1170Sstevel@tonic-gate 	char	libpath[MAXPATHLEN];	/* full pathname to lib */
1180Sstevel@tonic-gate } plugin_lib_t;
1190Sstevel@tonic-gate 
1200Sstevel@tonic-gate static plugin_lib_t plugin_list;
1210Sstevel@tonic-gate 
1220Sstevel@tonic-gate typedef struct lib_cache {
1230Sstevel@tonic-gate 	struct lib_cache *lc_next;
1240Sstevel@tonic-gate 	plugin_lib_t *lc_libp;
1250Sstevel@tonic-gate 	char *lc_ap_id;
1260Sstevel@tonic-gate 	char *lc_ap_physical;	/* physical ap_id */
1270Sstevel@tonic-gate 	char *lc_ap_logical;	/* logical ap_id */
1280Sstevel@tonic-gate } lib_cache_t;
1290Sstevel@tonic-gate 
1300Sstevel@tonic-gate static lib_cache_t *lib_cache;
1310Sstevel@tonic-gate static mutex_t lib_cache_lock;
1320Sstevel@tonic-gate 
1330Sstevel@tonic-gate /*
1340Sstevel@tonic-gate  * Library locator data struct - used to pass down through the device
1350Sstevel@tonic-gate  * tree walking code.
1360Sstevel@tonic-gate  */
1370Sstevel@tonic-gate typedef struct lib_locator {
1380Sstevel@tonic-gate 	char	ap_base[MAXPATHLEN];
1390Sstevel@tonic-gate 	char	ap_logical[CFGA_LOG_EXT_LEN];
1400Sstevel@tonic-gate 	char	ap_physical[CFGA_PHYS_EXT_LEN];
1410Sstevel@tonic-gate 	char	ap_class[CFGA_CLASS_LEN];
1420Sstevel@tonic-gate 	char	pathname[MAXPATHLEN];
1430Sstevel@tonic-gate 	plugin_lib_t *libp;
1440Sstevel@tonic-gate 	cfga_err_t status;
1450Sstevel@tonic-gate 	vers_req_t vers_req;	/* plug-in version required */
1460Sstevel@tonic-gate } lib_loc_t;
1470Sstevel@tonic-gate 
1480Sstevel@tonic-gate /*
1490Sstevel@tonic-gate  * linked list of cfga_stat_data structs - used for
1500Sstevel@tonic-gate  * config_list
1510Sstevel@tonic-gate  */
1520Sstevel@tonic-gate typedef struct stat_data_list {
1530Sstevel@tonic-gate 	struct stat_data_list	*next;
1540Sstevel@tonic-gate 	cfga_stat_data_t	stat_data;
1550Sstevel@tonic-gate } stat_data_list_t;
1560Sstevel@tonic-gate 
1570Sstevel@tonic-gate /*
1580Sstevel@tonic-gate  * linked list of arrays. Each array represents a bunch
1590Sstevel@tonic-gate  * of list_data_t structures returned by a single call
1600Sstevel@tonic-gate  * to a plugin's cfga_list_ext() routine.
1610Sstevel@tonic-gate  */
1620Sstevel@tonic-gate typedef struct array_list {
1630Sstevel@tonic-gate 	struct array_list	*next;
1640Sstevel@tonic-gate 	cfga_list_data_t	*array;
1650Sstevel@tonic-gate 	int			nelem;
1660Sstevel@tonic-gate } array_list_t;
1670Sstevel@tonic-gate 
1680Sstevel@tonic-gate /*
1690Sstevel@tonic-gate  * encapsulate config_list args to get them through the tree
1700Sstevel@tonic-gate  * walking code
1710Sstevel@tonic-gate  */
1720Sstevel@tonic-gate typedef struct list_stat {
1730Sstevel@tonic-gate 	const char *opts;	/* Hardware specific options */
1740Sstevel@tonic-gate 	char **errstr;
1750Sstevel@tonic-gate 	cfga_flags_t flags;
1760Sstevel@tonic-gate 	int	*countp;	/* Total number of list and stat structures */
1770Sstevel@tonic-gate 	stat_data_list_t *sdl;	/* Linked list of stat structures */
1780Sstevel@tonic-gate 	array_list_t *al;	/* Linked list of arrays of list structures */
1790Sstevel@tonic-gate 	vers_req_t use_vers;	/* plugin versions to be stat'ed */
1800Sstevel@tonic-gate } list_stat_t;
1810Sstevel@tonic-gate 
1820Sstevel@tonic-gate /*
1830Sstevel@tonic-gate  * Internal operations for libcfgadm which are version dependant
1840Sstevel@tonic-gate  */
1850Sstevel@tonic-gate struct cfga_vers_ops {
1860Sstevel@tonic-gate 	cfga_err_t (*resolve_lib)(plugin_lib_t *libp);
1870Sstevel@tonic-gate 	cfga_err_t (*stat_plugin)(list_stat_t *, lib_loc_t *, char **errstring);
1880Sstevel@tonic-gate 	cfga_err_t (*mklog)(di_node_t, di_minor_t, plugin_lib_t *,
1890Sstevel@tonic-gate 	    lib_loc_t *liblocp);
1900Sstevel@tonic-gate 	cfga_err_t (*get_cond)(lib_loc_t *, cfga_cond_t *, char **);
1910Sstevel@tonic-gate };
1920Sstevel@tonic-gate 
1930Sstevel@tonic-gate 
1940Sstevel@tonic-gate /*
1950Sstevel@tonic-gate  * Lock to protect list of libraries
1960Sstevel@tonic-gate  */
1970Sstevel@tonic-gate static mutex_t plugin_list_lock;
1980Sstevel@tonic-gate 
1990Sstevel@tonic-gate /*
2000Sstevel@tonic-gate  * Forward declarations
2010Sstevel@tonic-gate  */
2020Sstevel@tonic-gate 
2030Sstevel@tonic-gate static const char *__config_strerror(cfga_err_t);
2040Sstevel@tonic-gate static void *config_calloc_check(size_t, size_t, char **);
2050Sstevel@tonic-gate static cfga_err_t resolve_lib_ref(plugin_lib_t *, lib_loc_t *);
2060Sstevel@tonic-gate static cfga_err_t config_get_lib(const char *, lib_loc_t *, char **);
2070Sstevel@tonic-gate static int check_ap(di_node_t, di_minor_t, void *);
2080Sstevel@tonic-gate static int check_ap_phys(di_node_t, di_minor_t, void *);
2090Sstevel@tonic-gate 
2100Sstevel@tonic-gate static cfga_err_t find_ap_common(lib_loc_t *libloc_p, const char *rootpath,
2110Sstevel@tonic-gate     int (*fcn)(di_node_t node, di_minor_t minor, void *arg), char **errstring);
2120Sstevel@tonic-gate 
2130Sstevel@tonic-gate static plugin_lib_t *lib_in_list(char *);
2140Sstevel@tonic-gate static cfga_err_t load_lib(di_node_t, di_minor_t, lib_loc_t *);
2150Sstevel@tonic-gate extern void bcopy(const void *, void *, size_t);
2160Sstevel@tonic-gate static void config_err(int, int, char **);
2170Sstevel@tonic-gate static void hold_lib(plugin_lib_t *);
2180Sstevel@tonic-gate static void rele_lib(plugin_lib_t *);
2190Sstevel@tonic-gate 
2200Sstevel@tonic-gate static cfga_err_t parse_listopt(char *listopts, char **classpp,
2210Sstevel@tonic-gate     char **errstring);
2220Sstevel@tonic-gate 
2230Sstevel@tonic-gate static cfga_err_t list_common(list_stat_t *lstatp, const char *class);
2240Sstevel@tonic-gate static int do_list_common(di_node_t node, di_minor_t minor, void *arg);
2250Sstevel@tonic-gate static cfga_err_t stat_common(int num_ap_ids, char *const *ap_ids,
2260Sstevel@tonic-gate     const char *class, list_stat_t *lstatp);
2270Sstevel@tonic-gate 
2280Sstevel@tonic-gate static cfga_err_t null_resolve(plugin_lib_t *libp);
2290Sstevel@tonic-gate static cfga_err_t resolve_v1(plugin_lib_t *libp);
2300Sstevel@tonic-gate static cfga_err_t resolve_v2(plugin_lib_t *libp);
2310Sstevel@tonic-gate 
2320Sstevel@tonic-gate static cfga_err_t mklog_common(di_node_t node, di_minor_t minor,
2330Sstevel@tonic-gate     lib_loc_t *liblocp, size_t len);
2340Sstevel@tonic-gate 
2350Sstevel@tonic-gate static cfga_err_t null_mklog(di_node_t node, di_minor_t minor,
2360Sstevel@tonic-gate     plugin_lib_t *libp, lib_loc_t *liblocp);
2370Sstevel@tonic-gate static cfga_err_t mklog_v1(di_node_t node, di_minor_t minor,
2380Sstevel@tonic-gate     plugin_lib_t *libp, lib_loc_t *liblocp);
2390Sstevel@tonic-gate static cfga_err_t mklog_v2(di_node_t node, di_minor_t minor,
2400Sstevel@tonic-gate     plugin_lib_t *libp, lib_loc_t *liblocp);
2410Sstevel@tonic-gate 
2420Sstevel@tonic-gate static cfga_err_t null_stat_plugin(list_stat_t *lstatp, lib_loc_t *libloc_p,
2430Sstevel@tonic-gate     char **errstring);
2440Sstevel@tonic-gate static cfga_err_t stat_plugin_v2(list_stat_t *lstat, lib_loc_t *libloc_p,
2450Sstevel@tonic-gate     char **errstring);
2460Sstevel@tonic-gate static cfga_err_t stat_plugin_v1(list_stat_t *lstat, lib_loc_t *libloc_p,
2470Sstevel@tonic-gate     char **errstring);
2480Sstevel@tonic-gate 
2490Sstevel@tonic-gate static cfga_err_t null_get_cond(lib_loc_t *liblocp, cfga_cond_t *condp,
2500Sstevel@tonic-gate     char **errstring);
2510Sstevel@tonic-gate static cfga_err_t get_cond_v1(lib_loc_t *liblocp, cfga_cond_t *condp,
2520Sstevel@tonic-gate     char **errstring);
2530Sstevel@tonic-gate static cfga_err_t get_cond_v2(lib_loc_t *liblocp, cfga_cond_t *condp,
2540Sstevel@tonic-gate     char **errstring);
2550Sstevel@tonic-gate 
2560Sstevel@tonic-gate static cfga_err_t realloc_data(cfga_stat_data_t **ap_id_list,
2570Sstevel@tonic-gate     int *nlistp, list_stat_t *lstatp);
2580Sstevel@tonic-gate static cfga_err_t realloc_data_ext(cfga_list_data_t **ap_id_list,
2590Sstevel@tonic-gate     int *nlistp, list_stat_t *lstatp);
2600Sstevel@tonic-gate 
2610Sstevel@tonic-gate static void stat_to_list(cfga_list_data_t *lp, cfga_stat_data_t *statp);
2620Sstevel@tonic-gate static void lstat_free(list_stat_t *lstatp);
2630Sstevel@tonic-gate static cfga_ap_types_t find_arg_type(const char *ap_id);
2640Sstevel@tonic-gate static int compat_plugin(vers_req_t *reqp, int plugin_vers);
2650Sstevel@tonic-gate 
2660Sstevel@tonic-gate static cfga_err_t check_flags(cfga_flags_t flags, cfga_flags_t mask,
2670Sstevel@tonic-gate     char **errstring);
2680Sstevel@tonic-gate static cfga_err_t check_apids(int num_ap_ids, char *const *ap_ids,
2690Sstevel@tonic-gate     char **errstring);
2700Sstevel@tonic-gate 
2710Sstevel@tonic-gate static char *get_class(di_minor_t minor);
2720Sstevel@tonic-gate static cfga_err_t split_apid(char *ap_id, char **dyncompp, char **errstring);
2730Sstevel@tonic-gate static void append_dyn(char *buf, const char *dyncomp, size_t blen);
2740Sstevel@tonic-gate static int default_ap_id_cmp(const char *ap_id1, const char *ap_id2);
2750Sstevel@tonic-gate static void destroy_cache();
2760Sstevel@tonic-gate 
2770Sstevel@tonic-gate /*
2780Sstevel@tonic-gate  * Plugin library search path helpers
2790Sstevel@tonic-gate  */
2800Sstevel@tonic-gate #define	LIB_PATH_BASE1	"/usr/platform/"
2810Sstevel@tonic-gate #define	LIB_PATH_BASE2	"/usr"
282133Sdmick #if defined(__sparcv9)
2830Sstevel@tonic-gate #define	LIB_PATH_MIDDLE	"/lib/cfgadm/sparcv9/"
284133Sdmick #elif defined(__amd64)
285133Sdmick #define	LIB_PATH_MIDDLE "/lib/cfgadm/amd64/"
2860Sstevel@tonic-gate #else
2870Sstevel@tonic-gate #define	LIB_PATH_MIDDLE	"/lib/cfgadm/"
2880Sstevel@tonic-gate #endif
2890Sstevel@tonic-gate #define	LIB_PATH_TAIL	".so.1"
2900Sstevel@tonic-gate 
2910Sstevel@tonic-gate 
2920Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)
2930Sstevel@tonic-gate #define	TEXT_DOMAIN	"SYS_TEST"
2940Sstevel@tonic-gate #endif
2950Sstevel@tonic-gate 
2960Sstevel@tonic-gate /*
2970Sstevel@tonic-gate  * Defined constants
2980Sstevel@tonic-gate  */
2990Sstevel@tonic-gate #define	DEVICES_DIR		"/devices"
3000Sstevel@tonic-gate #define	DOT_DOT_DEVICES		"../devices"
3010Sstevel@tonic-gate #define	CFGA_DEV_DIR		"/dev/cfg"
3020Sstevel@tonic-gate #define	SLASH			"/"
3030Sstevel@tonic-gate #define	S_FREE(x)	(((x) != NULL) ? (free(x), (x) = NULL) : (void *)0)
3040Sstevel@tonic-gate #define	GET_DYN(a)	(strstr((a), CFGA_DYN_SEP))
3050Sstevel@tonic-gate 
3060Sstevel@tonic-gate #define	CFGA_NO_CLASS		"none"
3070Sstevel@tonic-gate 
3080Sstevel@tonic-gate /*
3090Sstevel@tonic-gate  * Error strings
3100Sstevel@tonic-gate  */
3110Sstevel@tonic-gate #define	DI_INIT_FAILED	1
3120Sstevel@tonic-gate #define	ALLOC_FAILED	2
3130Sstevel@tonic-gate #define	INVALID_ARGS	3
3140Sstevel@tonic-gate 
3150Sstevel@tonic-gate static char *
3160Sstevel@tonic-gate err_strings[] = {
3170Sstevel@tonic-gate 	NULL,
3180Sstevel@tonic-gate 	"Device library initialize failed",
3190Sstevel@tonic-gate 	"Memory allocation failed",
3200Sstevel@tonic-gate 	"Invalid argument(s)"
3210Sstevel@tonic-gate };
3220Sstevel@tonic-gate 
3230Sstevel@tonic-gate static const char err_sep[] = ": ";
3240Sstevel@tonic-gate 
3250Sstevel@tonic-gate 
3260Sstevel@tonic-gate /*
3270Sstevel@tonic-gate  * Table of version dependant routines
3280Sstevel@tonic-gate  */
3290Sstevel@tonic-gate static struct cfga_vers_ops cfga_vers_ops[CFGA_HSL_VERS + 1] = {
3300Sstevel@tonic-gate 
3310Sstevel@tonic-gate {null_resolve,	null_stat_plugin,	null_mklog,	null_get_cond	},
3320Sstevel@tonic-gate {resolve_v1,	stat_plugin_v1,		mklog_v1,	get_cond_v1	},
3330Sstevel@tonic-gate {resolve_v2,	stat_plugin_v2,		mklog_v2,	get_cond_v2	}
3340Sstevel@tonic-gate 
3350Sstevel@tonic-gate };
3360Sstevel@tonic-gate #define	VERS_ARRAY_SZ	(sizeof (cfga_vers_ops)/sizeof (cfga_vers_ops[0]))
3370Sstevel@tonic-gate 
3380Sstevel@tonic-gate 
3390Sstevel@tonic-gate /*
3400Sstevel@tonic-gate  * Public interfaces for libcfgadm, as documented in config_admin.3x
3410Sstevel@tonic-gate  */
3420Sstevel@tonic-gate 
3430Sstevel@tonic-gate /*
3440Sstevel@tonic-gate  * config_change_state
3450Sstevel@tonic-gate  */
3460Sstevel@tonic-gate 
3470Sstevel@tonic-gate cfga_err_t
3480Sstevel@tonic-gate config_change_state(
3490Sstevel@tonic-gate 	cfga_cmd_t state_change_cmd,
3500Sstevel@tonic-gate 	int num_ap_ids,
3510Sstevel@tonic-gate 	char *const *ap_id,
3520Sstevel@tonic-gate 	const char *options,
3530Sstevel@tonic-gate 	struct cfga_confirm *confp,
3540Sstevel@tonic-gate 	struct cfga_msg *msgp,
3550Sstevel@tonic-gate 	char **errstring,
3560Sstevel@tonic-gate 	cfga_flags_t flags)
3570Sstevel@tonic-gate {
3580Sstevel@tonic-gate 	/*
3590Sstevel@tonic-gate 	 * for each arg -
3600Sstevel@tonic-gate 	 *  load hs library,
3610Sstevel@tonic-gate 	 *  if force
3620Sstevel@tonic-gate 	 *    call cfga_state_change_func
3630Sstevel@tonic-gate 	 *    return status
3640Sstevel@tonic-gate 	 *  else
3650Sstevel@tonic-gate 	 *    call it's cfga_stat
3660Sstevel@tonic-gate 	 *    check condition
3670Sstevel@tonic-gate 	 *    call cfga_state_change_func
3680Sstevel@tonic-gate 	 *    return status
3690Sstevel@tonic-gate 	 */
3700Sstevel@tonic-gate 	int i;
3710Sstevel@tonic-gate 	lib_loc_t libloc;
3720Sstevel@tonic-gate 	plugin_lib_t *libp;
3730Sstevel@tonic-gate 	cfga_cond_t cond;
3740Sstevel@tonic-gate 
3750Sstevel@tonic-gate 	cfga_err_t retval = CFGA_OK;
3760Sstevel@tonic-gate 
3770Sstevel@tonic-gate 	/* Sanity checks */
3780Sstevel@tonic-gate 	if (state_change_cmd == CFGA_CMD_NONE)
3790Sstevel@tonic-gate 		return (retval);
3800Sstevel@tonic-gate 
3810Sstevel@tonic-gate 	if ((state_change_cmd < CFGA_CMD_NONE) ||
3820Sstevel@tonic-gate 	    (state_change_cmd > CFGA_CMD_UNCONFIGURE))
3830Sstevel@tonic-gate 		return (CFGA_INVAL);
3840Sstevel@tonic-gate 
3850Sstevel@tonic-gate 	if (errstring != NULL) {
3860Sstevel@tonic-gate 		*errstring = NULL;
3870Sstevel@tonic-gate 	}
3880Sstevel@tonic-gate 
3890Sstevel@tonic-gate 	if (check_flags(flags, CFGA_FLAG_FORCE | CFGA_FLAG_VERBOSE, errstring)
3900Sstevel@tonic-gate 	    != CFGA_OK) {
3910Sstevel@tonic-gate 		return (CFGA_ERROR);
3920Sstevel@tonic-gate 	}
3930Sstevel@tonic-gate 
3940Sstevel@tonic-gate 	if (check_apids(num_ap_ids, ap_id, errstring) != CFGA_OK) {
3950Sstevel@tonic-gate 		return (CFGA_ERROR);
3960Sstevel@tonic-gate 	}
3970Sstevel@tonic-gate 
3980Sstevel@tonic-gate 	/*
3990Sstevel@tonic-gate 	 * operate on each ap_id
4000Sstevel@tonic-gate 	 */
4010Sstevel@tonic-gate 	for (i = 0; (i < num_ap_ids) && (retval == CFGA_OK); i++) {
4020Sstevel@tonic-gate 		libloc.libp = NULL;
4030Sstevel@tonic-gate 		if ((retval = config_get_lib(ap_id[i], &libloc, errstring)) !=
4040Sstevel@tonic-gate 		    CFGA_OK) {
4050Sstevel@tonic-gate 			break;
4060Sstevel@tonic-gate 		}
4070Sstevel@tonic-gate 
4080Sstevel@tonic-gate 		libp = libloc.libp;
4090Sstevel@tonic-gate 		if ((flags & CFGA_FLAG_FORCE) ||
4100Sstevel@tonic-gate 		    (state_change_cmd == CFGA_CMD_UNLOAD) ||
4110Sstevel@tonic-gate 		    (state_change_cmd == CFGA_CMD_DISCONNECT) ||
4120Sstevel@tonic-gate 		    (state_change_cmd == CFGA_CMD_UNCONFIGURE)) {
4130Sstevel@tonic-gate 			errno = 0;
4140Sstevel@tonic-gate 			retval = (*libp->cfga_change_state_p)
4150Sstevel@tonic-gate 			    (state_change_cmd, libloc.ap_physical, options,
4160Sstevel@tonic-gate 			    confp, msgp, errstring, flags);
4170Sstevel@tonic-gate 		} else {
4180Sstevel@tonic-gate 			/*
4190Sstevel@tonic-gate 			 * Need to check condition before proceeding in
4200Sstevel@tonic-gate 			 * the "configure direction"
4210Sstevel@tonic-gate 			 */
4220Sstevel@tonic-gate 			if ((retval = libp->vers_ops->get_cond(&libloc, &cond,
4230Sstevel@tonic-gate 			    errstring)) != CFGA_OK) {
4240Sstevel@tonic-gate 				break;
4250Sstevel@tonic-gate 			}
4260Sstevel@tonic-gate 
4270Sstevel@tonic-gate 			if (cond == CFGA_COND_OK || cond == CFGA_COND_UNKNOWN) {
4280Sstevel@tonic-gate 				errno = 0;
4290Sstevel@tonic-gate 				retval =
4300Sstevel@tonic-gate 				    (*libp->cfga_change_state_p)(
4310Sstevel@tonic-gate 				    state_change_cmd,
4320Sstevel@tonic-gate 				    libloc.ap_physical, options,
4330Sstevel@tonic-gate 				    confp, msgp, errstring,
4340Sstevel@tonic-gate 				    flags);
4350Sstevel@tonic-gate 			} else {
4360Sstevel@tonic-gate 				retval = CFGA_INSUFFICENT_CONDITION;
4370Sstevel@tonic-gate 			}
4380Sstevel@tonic-gate 		}
4390Sstevel@tonic-gate 		rele_lib(libp);
4400Sstevel@tonic-gate 	}
4410Sstevel@tonic-gate 
4420Sstevel@tonic-gate 	return (retval);
4430Sstevel@tonic-gate }
4440Sstevel@tonic-gate 
4450Sstevel@tonic-gate /*
4460Sstevel@tonic-gate  * config_private_func
4470Sstevel@tonic-gate  */
4480Sstevel@tonic-gate 
4490Sstevel@tonic-gate cfga_err_t
4500Sstevel@tonic-gate config_private_func(
4510Sstevel@tonic-gate 	const char *function,
4520Sstevel@tonic-gate 	int num_ap_ids,
4530Sstevel@tonic-gate 	char *const *ap_ids,
4540Sstevel@tonic-gate 	const char *options,
4550Sstevel@tonic-gate 	struct cfga_confirm *confp,
4560Sstevel@tonic-gate 	struct cfga_msg *msgp,
4570Sstevel@tonic-gate 	char **errstring,
4580Sstevel@tonic-gate 	cfga_flags_t flags)
4590Sstevel@tonic-gate {
4600Sstevel@tonic-gate 	int i;
4610Sstevel@tonic-gate 	lib_loc_t libloc;
4620Sstevel@tonic-gate 	cfga_err_t retval = CFGA_OK;
4630Sstevel@tonic-gate 
4640Sstevel@tonic-gate 
4650Sstevel@tonic-gate 	if (errstring != NULL) {
4660Sstevel@tonic-gate 		*errstring = NULL;
4670Sstevel@tonic-gate 	}
4680Sstevel@tonic-gate 
4690Sstevel@tonic-gate 	if (check_flags(flags, CFGA_FLAG_FORCE | CFGA_FLAG_VERBOSE, errstring)
4700Sstevel@tonic-gate 	    != CFGA_OK) {
4710Sstevel@tonic-gate 		return (CFGA_ERROR);
4720Sstevel@tonic-gate 	}
4730Sstevel@tonic-gate 
4740Sstevel@tonic-gate 	if (check_apids(num_ap_ids, ap_ids, errstring) != CFGA_OK) {
4750Sstevel@tonic-gate 		return (CFGA_ERROR);
4760Sstevel@tonic-gate 	}
4770Sstevel@tonic-gate 
4780Sstevel@tonic-gate 	/*
4790Sstevel@tonic-gate 	 * operate on each ap_id
4800Sstevel@tonic-gate 	 */
4810Sstevel@tonic-gate 	for (i = 0; (i < num_ap_ids) && (retval == CFGA_OK); i++) {
4820Sstevel@tonic-gate 		libloc.libp = NULL;
4830Sstevel@tonic-gate 		if ((retval = config_get_lib(ap_ids[i], &libloc, errstring)) !=
4840Sstevel@tonic-gate 		    CFGA_OK)  {
4850Sstevel@tonic-gate 			return (retval);
4860Sstevel@tonic-gate 		}
4870Sstevel@tonic-gate 
4880Sstevel@tonic-gate 		errno = 0;
4890Sstevel@tonic-gate 		retval = (*libloc.libp->cfga_private_func_p)(function,
4900Sstevel@tonic-gate 		    libloc.ap_physical, options, confp, msgp, errstring,
4910Sstevel@tonic-gate 		    flags);
4920Sstevel@tonic-gate 		rele_lib(libloc.libp);
4930Sstevel@tonic-gate 	}
4940Sstevel@tonic-gate 
4950Sstevel@tonic-gate 	return (retval);
4960Sstevel@tonic-gate }
4970Sstevel@tonic-gate 
4980Sstevel@tonic-gate 
4990Sstevel@tonic-gate /*
5000Sstevel@tonic-gate  * config_test
5010Sstevel@tonic-gate  */
5020Sstevel@tonic-gate 
5030Sstevel@tonic-gate cfga_err_t
5040Sstevel@tonic-gate config_test(
5050Sstevel@tonic-gate 	int num_ap_ids,
5060Sstevel@tonic-gate 	char *const *ap_ids,
5070Sstevel@tonic-gate 	const char *options,
5080Sstevel@tonic-gate 	struct cfga_msg *msgp,
5090Sstevel@tonic-gate 	char **errstring,
5100Sstevel@tonic-gate 	cfga_flags_t flags)
5110Sstevel@tonic-gate {
5120Sstevel@tonic-gate 	int i;
5130Sstevel@tonic-gate 	lib_loc_t libloc;
5140Sstevel@tonic-gate 	cfga_err_t retval = CFGA_OK;
5150Sstevel@tonic-gate 
5160Sstevel@tonic-gate 	if (errstring != NULL) {
5170Sstevel@tonic-gate 		*errstring = NULL;
5180Sstevel@tonic-gate 	}
5190Sstevel@tonic-gate 
5200Sstevel@tonic-gate 	if (check_flags(flags, CFGA_FLAG_FORCE | CFGA_FLAG_VERBOSE, errstring)
5210Sstevel@tonic-gate 	    != CFGA_OK) {
5220Sstevel@tonic-gate 		return (CFGA_ERROR);
5230Sstevel@tonic-gate 	}
5240Sstevel@tonic-gate 
5250Sstevel@tonic-gate 	if (check_apids(num_ap_ids, ap_ids, errstring) != CFGA_OK) {
5260Sstevel@tonic-gate 		return (CFGA_ERROR);
5270Sstevel@tonic-gate 	}
5280Sstevel@tonic-gate 
5290Sstevel@tonic-gate 	/*
5300Sstevel@tonic-gate 	 * operate on each ap_id
5310Sstevel@tonic-gate 	 */
5320Sstevel@tonic-gate 	for (i = 0; (i < num_ap_ids) && (retval == CFGA_OK); i++) {
5330Sstevel@tonic-gate 		libloc.libp = NULL;
5340Sstevel@tonic-gate 		if ((retval = config_get_lib(ap_ids[i], &libloc, errstring)) !=
5350Sstevel@tonic-gate 		    CFGA_OK) {
5360Sstevel@tonic-gate 			return (retval);
5370Sstevel@tonic-gate 		}
5380Sstevel@tonic-gate 
5390Sstevel@tonic-gate 		errno = 0;
5400Sstevel@tonic-gate 		retval = (*libloc.libp->cfga_test_p)(libloc.ap_physical,
5410Sstevel@tonic-gate 		    options, msgp, errstring, flags);
5420Sstevel@tonic-gate 		rele_lib(libloc.libp);
5430Sstevel@tonic-gate 	}
5440Sstevel@tonic-gate 
5450Sstevel@tonic-gate 	return (retval);
5460Sstevel@tonic-gate }
5470Sstevel@tonic-gate 
5480Sstevel@tonic-gate cfga_err_t
5490Sstevel@tonic-gate config_stat(
5500Sstevel@tonic-gate 	int num_ap_ids,
5510Sstevel@tonic-gate 	char *const *ap_ids,
5520Sstevel@tonic-gate 	struct cfga_stat_data *buf,
5530Sstevel@tonic-gate 	const char *options,
5540Sstevel@tonic-gate 	char **errstring)
5550Sstevel@tonic-gate {
5560Sstevel@tonic-gate 	int nstat, n, i;
5570Sstevel@tonic-gate 	list_stat_t lstat = {NULL};
5580Sstevel@tonic-gate 	cfga_err_t rc = CFGA_OK;
5590Sstevel@tonic-gate 
5600Sstevel@tonic-gate 	if (check_apids(num_ap_ids, ap_ids, errstring) != CFGA_OK) {
5610Sstevel@tonic-gate 		return (CFGA_ERROR);
5620Sstevel@tonic-gate 	}
5630Sstevel@tonic-gate 
5640Sstevel@tonic-gate 	/*
5650Sstevel@tonic-gate 	 * V1 entry points don't support dynamic attachment points
5660Sstevel@tonic-gate 	 */
5670Sstevel@tonic-gate 	for (i = 0; i < num_ap_ids; i++) {
5680Sstevel@tonic-gate 		if (GET_DYN(ap_ids[i]) != NULL) {
5690Sstevel@tonic-gate 			return (CFGA_APID_NOEXIST);
5700Sstevel@tonic-gate 		}
5710Sstevel@tonic-gate 	}
5720Sstevel@tonic-gate 
5730Sstevel@tonic-gate 
5740Sstevel@tonic-gate 	nstat = n = 0;
5750Sstevel@tonic-gate 	lstat.countp = &nstat;
5760Sstevel@tonic-gate 	lstat.opts = options;
5770Sstevel@tonic-gate 	lstat.errstr = errstring;
5780Sstevel@tonic-gate 	/*
5790Sstevel@tonic-gate 	 * This is a V1 interface which can use only V1 plugins
5800Sstevel@tonic-gate 	 */
5810Sstevel@tonic-gate 	lstat.use_vers.v_max = lstat.use_vers.v_min = CFGA_HSL_V1;
5820Sstevel@tonic-gate 
5830Sstevel@tonic-gate 	rc = stat_common(num_ap_ids, ap_ids, NULL, &lstat);
5840Sstevel@tonic-gate 	if (rc == CFGA_OK) {
5850Sstevel@tonic-gate 		assert(*lstat.countp == num_ap_ids);
5860Sstevel@tonic-gate 		rc = realloc_data(&buf, &n, &lstat);
5870Sstevel@tonic-gate 	}
5880Sstevel@tonic-gate 
5890Sstevel@tonic-gate 	return (rc);
5900Sstevel@tonic-gate }
5910Sstevel@tonic-gate 
5920Sstevel@tonic-gate /*
5930Sstevel@tonic-gate  * config_list
5940Sstevel@tonic-gate  */
5950Sstevel@tonic-gate cfga_err_t
5960Sstevel@tonic-gate config_list(
5970Sstevel@tonic-gate 	struct cfga_stat_data **ap_id_list,
5980Sstevel@tonic-gate 	int *nlistp,
5990Sstevel@tonic-gate 	const char *options,
6000Sstevel@tonic-gate 	char **errstring)
6010Sstevel@tonic-gate {
6020Sstevel@tonic-gate 	int nstat;
6030Sstevel@tonic-gate 	list_stat_t lstat = {NULL};
6040Sstevel@tonic-gate 	cfga_err_t retval = CFGA_ERROR;
6050Sstevel@tonic-gate 
6060Sstevel@tonic-gate 	if (errstring != NULL) {
6070Sstevel@tonic-gate 		*errstring = NULL;
6080Sstevel@tonic-gate 	}
6090Sstevel@tonic-gate 
6100Sstevel@tonic-gate 	nstat = 0;
6110Sstevel@tonic-gate 	lstat.countp = &nstat;
6120Sstevel@tonic-gate 	lstat.opts = options;
6130Sstevel@tonic-gate 	lstat.errstr = errstring;
6140Sstevel@tonic-gate 	/*
6150Sstevel@tonic-gate 	 * This is a V1 interface which can use only V1 plugins
6160Sstevel@tonic-gate 	 */
6170Sstevel@tonic-gate 	lstat.use_vers.v_max = lstat.use_vers.v_min = CFGA_HSL_V1;
6180Sstevel@tonic-gate 
6190Sstevel@tonic-gate 
6200Sstevel@tonic-gate 	*ap_id_list = NULL;
6210Sstevel@tonic-gate 	*nlistp = 0;
6220Sstevel@tonic-gate 
6230Sstevel@tonic-gate 	/*
6240Sstevel@tonic-gate 	 * V1 interfaces don't support prefiltering, no class
6250Sstevel@tonic-gate 	 * specified.
6260Sstevel@tonic-gate 	 */
6270Sstevel@tonic-gate 	retval = list_common(&lstat, NULL);
6280Sstevel@tonic-gate 	if (retval == CFGA_OK) {
6290Sstevel@tonic-gate 		retval = realloc_data(ap_id_list, nlistp, &lstat);
6300Sstevel@tonic-gate 	}
6310Sstevel@tonic-gate 
6320Sstevel@tonic-gate 	assert((ap_id_list != NULL && *nlistp != 0) ||
6330Sstevel@tonic-gate 	    (ap_id_list == NULL && *nlistp == 0));
6340Sstevel@tonic-gate 
6350Sstevel@tonic-gate 	if (retval == CFGA_OK && *nlistp == 0) {
6360Sstevel@tonic-gate 		return (CFGA_NOTSUPP);
6370Sstevel@tonic-gate 	} else {
6380Sstevel@tonic-gate 		return (retval);
6390Sstevel@tonic-gate 	}
6400Sstevel@tonic-gate }
6410Sstevel@tonic-gate 
6420Sstevel@tonic-gate 
6430Sstevel@tonic-gate /*
6440Sstevel@tonic-gate  * config_list_ext
6450Sstevel@tonic-gate  */
6460Sstevel@tonic-gate cfga_err_t
6470Sstevel@tonic-gate config_list_ext(
6480Sstevel@tonic-gate 	int num_ap_ids,
6490Sstevel@tonic-gate 	char *const *ap_ids,
6500Sstevel@tonic-gate 	struct cfga_list_data **ap_id_list,
6510Sstevel@tonic-gate 	int *nlistp,
6520Sstevel@tonic-gate 	const char *options,
6530Sstevel@tonic-gate 	const char *listopts,
6540Sstevel@tonic-gate 	char **errstring,
6550Sstevel@tonic-gate 	cfga_flags_t flags)
6560Sstevel@tonic-gate {
6570Sstevel@tonic-gate 	int nstat, list, prefilter;
6580Sstevel@tonic-gate 	list_stat_t lstat = {NULL};
6590Sstevel@tonic-gate 	char *class;
6600Sstevel@tonic-gate 
6610Sstevel@tonic-gate 	cfga_err_t rc = CFGA_ERROR;
6620Sstevel@tonic-gate 
6630Sstevel@tonic-gate 	*nlistp = 0;
6640Sstevel@tonic-gate 	*ap_id_list = NULL;
6650Sstevel@tonic-gate 
6660Sstevel@tonic-gate 	if (errstring != NULL) {
6670Sstevel@tonic-gate 		*errstring = NULL;
6680Sstevel@tonic-gate 	}
6690Sstevel@tonic-gate 
6700Sstevel@tonic-gate 	if (check_flags(flags, CFGA_FLAG_LIST_ALL, errstring) != CFGA_OK) {
6710Sstevel@tonic-gate 		return (CFGA_ERROR);
6720Sstevel@tonic-gate 	}
6730Sstevel@tonic-gate 
6740Sstevel@tonic-gate 	class = NULL;
6750Sstevel@tonic-gate 	if ((rc = parse_listopt((char *)listopts, &class, errstring))
6760Sstevel@tonic-gate 	    != CFGA_OK) {
6770Sstevel@tonic-gate 		return (rc);
6780Sstevel@tonic-gate 	}
6790Sstevel@tonic-gate 
6800Sstevel@tonic-gate 	prefilter = (class == NULL) ? 0 : 1;
6810Sstevel@tonic-gate 
6820Sstevel@tonic-gate 	nstat = 0;
6830Sstevel@tonic-gate 	lstat.countp = &nstat;
6840Sstevel@tonic-gate 	lstat.opts = options;
6850Sstevel@tonic-gate 	lstat.errstr = errstring;
6860Sstevel@tonic-gate 	lstat.flags = flags;
6870Sstevel@tonic-gate 	/*
6880Sstevel@tonic-gate 	 * We support both V1 and V2 plugins through this entry
6890Sstevel@tonic-gate 	 * point.
6900Sstevel@tonic-gate 	 */
6910Sstevel@tonic-gate 	lstat.use_vers.v_min = CFGA_HSL_V1;
6920Sstevel@tonic-gate 	lstat.use_vers.v_max = CFGA_HSL_V2;
6930Sstevel@tonic-gate 
6940Sstevel@tonic-gate 	list = 0;
6950Sstevel@tonic-gate 	if (num_ap_ids == 0 && ap_ids == NULL) {
6960Sstevel@tonic-gate 		/*
6970Sstevel@tonic-gate 		 * discover and stat all attachment points
6980Sstevel@tonic-gate 		 */
6990Sstevel@tonic-gate 		list = 1;
7000Sstevel@tonic-gate 		rc = list_common(&lstat, class);
7010Sstevel@tonic-gate 	} else if (num_ap_ids > 0 && ap_ids != NULL) {
7020Sstevel@tonic-gate 		/*
7030Sstevel@tonic-gate 		 * Stat specified attachment points. With dynamic expansion
7040Sstevel@tonic-gate 		 * more data may be returned than was specified by user.
7050Sstevel@tonic-gate 		 */
7060Sstevel@tonic-gate 		rc = stat_common(num_ap_ids, ap_ids, class, &lstat);
7070Sstevel@tonic-gate 	} else {
7080Sstevel@tonic-gate 		rc = CFGA_ERROR;
7090Sstevel@tonic-gate 	}
7100Sstevel@tonic-gate 
7110Sstevel@tonic-gate 	S_FREE(class);
7120Sstevel@tonic-gate 
7130Sstevel@tonic-gate 	if (rc != CFGA_OK) {
7140Sstevel@tonic-gate 		return (rc);
7150Sstevel@tonic-gate 	}
7160Sstevel@tonic-gate 
7170Sstevel@tonic-gate 	rc = realloc_data_ext(ap_id_list, nlistp, &lstat);
7180Sstevel@tonic-gate 
7190Sstevel@tonic-gate 	assert((ap_id_list != NULL && *nlistp != 0) ||
7200Sstevel@tonic-gate 	    (ap_id_list == NULL && *nlistp == 0));
7210Sstevel@tonic-gate 
7220Sstevel@tonic-gate 	/*
7230Sstevel@tonic-gate 	 * For the list command notify user if no attachment
7240Sstevel@tonic-gate 	 * point is found in the system.
7250Sstevel@tonic-gate 	 *
7260Sstevel@tonic-gate 	 */
7270Sstevel@tonic-gate 	if (list && rc == CFGA_OK && *nlistp == 0) {
7280Sstevel@tonic-gate 		/*
7290Sstevel@tonic-gate 		 * If attachment points are being prefiltered, absence of data
7300Sstevel@tonic-gate 		 * does not imply that config. admin. is not
7310Sstevel@tonic-gate 		 * supported by the system.
7320Sstevel@tonic-gate 		 */
7330Sstevel@tonic-gate 		if (prefilter) {
7340Sstevel@tonic-gate 			/*
7350Sstevel@tonic-gate 			 * Prefiltering: requested class is absent
7360Sstevel@tonic-gate 			 */
7370Sstevel@tonic-gate 			return (CFGA_APID_NOEXIST);
7380Sstevel@tonic-gate 		} else {
7390Sstevel@tonic-gate 			/*
7400Sstevel@tonic-gate 			 * No attachment points in system
7410Sstevel@tonic-gate 			 */
7420Sstevel@tonic-gate 			return (CFGA_NOTSUPP);
7430Sstevel@tonic-gate 		}
7440Sstevel@tonic-gate 	} else {
7450Sstevel@tonic-gate 		return (rc);
7460Sstevel@tonic-gate 	}
7470Sstevel@tonic-gate }
7480Sstevel@tonic-gate 
7490Sstevel@tonic-gate 
7500Sstevel@tonic-gate /*
7510Sstevel@tonic-gate  * config_unload_libs
7520Sstevel@tonic-gate  *
7530Sstevel@tonic-gate  * Attempts to remove all libs on the plugin list.
7540Sstevel@tonic-gate  */
7550Sstevel@tonic-gate void
7560Sstevel@tonic-gate config_unload_libs()
7570Sstevel@tonic-gate {
7580Sstevel@tonic-gate 	plugin_lib_t *libp, *prev = &plugin_list, *next = NULL;
7590Sstevel@tonic-gate 
7600Sstevel@tonic-gate 	/* destroy cache entries to remove refcnt agains plugins */
7610Sstevel@tonic-gate 	destroy_cache();
7620Sstevel@tonic-gate 
7630Sstevel@tonic-gate 	(void) mutex_lock(&plugin_list_lock);
7640Sstevel@tonic-gate 	for (libp = plugin_list.next; libp != NULL; libp = next) {
7650Sstevel@tonic-gate 		next = libp->next;
7660Sstevel@tonic-gate 		(void) mutex_lock(&libp->lock);
7670Sstevel@tonic-gate 		if (libp->refcnt) {
7680Sstevel@tonic-gate 			(void) mutex_unlock(&libp->lock);
7690Sstevel@tonic-gate 			prev = libp;
7700Sstevel@tonic-gate 			continue;
7710Sstevel@tonic-gate 		}
7720Sstevel@tonic-gate 		(void) mutex_unlock(&libp->lock);
7730Sstevel@tonic-gate 		prev->next = next;
7740Sstevel@tonic-gate 		(void) dlclose(libp->handle);
7750Sstevel@tonic-gate 		(void) mutex_destroy(&libp->lock);
7760Sstevel@tonic-gate 		free(libp);
7770Sstevel@tonic-gate 	}
7780Sstevel@tonic-gate 	(void) mutex_unlock(&plugin_list_lock);
7790Sstevel@tonic-gate }
7800Sstevel@tonic-gate 
7810Sstevel@tonic-gate /*
7820Sstevel@tonic-gate  * config_ap_id_cmp
7830Sstevel@tonic-gate  */
7840Sstevel@tonic-gate int
7850Sstevel@tonic-gate config_ap_id_cmp(
7860Sstevel@tonic-gate 	const cfga_ap_log_id_t ap1,
7870Sstevel@tonic-gate 	const cfga_ap_log_id_t ap2)
7880Sstevel@tonic-gate {
7890Sstevel@tonic-gate 	int ret;
7900Sstevel@tonic-gate 	lib_loc_t libloc;
7910Sstevel@tonic-gate 	char apstat1[CFGA_PHYS_EXT_LEN];
7920Sstevel@tonic-gate 	char apstat2[CFGA_PHYS_EXT_LEN];
7930Sstevel@tonic-gate 	char *sep1, *sep2;
7940Sstevel@tonic-gate 
7950Sstevel@tonic-gate 	/*
7960Sstevel@tonic-gate 	 * Extract static ap_ids
7970Sstevel@tonic-gate 	 */
7980Sstevel@tonic-gate 	(void) strlcpy(apstat1, ap1, sizeof (apstat1));
7990Sstevel@tonic-gate 	(void) strlcpy(apstat2, ap2, sizeof (apstat2));
8000Sstevel@tonic-gate 
8010Sstevel@tonic-gate 	sep1 = GET_DYN(apstat1);
8020Sstevel@tonic-gate 	sep2 = GET_DYN(apstat2);
8030Sstevel@tonic-gate 
8040Sstevel@tonic-gate 	if (sep1)
8050Sstevel@tonic-gate 		*sep1 = '\0';
8060Sstevel@tonic-gate 	if (sep2)
8070Sstevel@tonic-gate 		*sep2 = '\0';
8080Sstevel@tonic-gate 
8090Sstevel@tonic-gate 	/*
8100Sstevel@tonic-gate 	 * Use the default comparator for static ap_ids
8110Sstevel@tonic-gate 	 */
8120Sstevel@tonic-gate 	ret = default_ap_id_cmp(apstat1, apstat2);
8130Sstevel@tonic-gate 	if (ret)
8140Sstevel@tonic-gate 		return (ret);
8150Sstevel@tonic-gate 
8160Sstevel@tonic-gate 	/*
8170Sstevel@tonic-gate 	 * static components match. They belong to
8180Sstevel@tonic-gate 	 * the same static ap_id. Check if both are dynamic
8190Sstevel@tonic-gate 	 * If not, static < dynamic.
8200Sstevel@tonic-gate 	 */
8210Sstevel@tonic-gate 	if ((sep1 == NULL) ^ (sep2 == NULL))
8220Sstevel@tonic-gate 		return (sep1 ? 1 : -1);
8230Sstevel@tonic-gate 
8240Sstevel@tonic-gate 	/*
8250Sstevel@tonic-gate 	 * If both are static, then ap1 = ap2
8260Sstevel@tonic-gate 	 */
8270Sstevel@tonic-gate 	if (sep1 == NULL)
8280Sstevel@tonic-gate 		return (0);
8290Sstevel@tonic-gate 
8300Sstevel@tonic-gate 	/*
8310Sstevel@tonic-gate 	 * Both are dynamic and belong to same static ap_id.
8320Sstevel@tonic-gate 	 * Use the plugin comparator
8330Sstevel@tonic-gate 	 */
8340Sstevel@tonic-gate 	libloc.libp = NULL;
8350Sstevel@tonic-gate 	if (config_get_lib(ap1, &libloc, NULL) != CFGA_OK) {
8360Sstevel@tonic-gate 		return (strncmp(sep1, sep2, CFGA_PHYS_EXT_LEN));
8370Sstevel@tonic-gate 	}
8380Sstevel@tonic-gate 
8390Sstevel@tonic-gate 	ret = (*libloc.libp->cfga_ap_id_cmp_p)(ap1, ap2);
8400Sstevel@tonic-gate 
8410Sstevel@tonic-gate 	rele_lib(libloc.libp);
8420Sstevel@tonic-gate 
8430Sstevel@tonic-gate 	return (ret);
8440Sstevel@tonic-gate }
8450Sstevel@tonic-gate 
8460Sstevel@tonic-gate /*
8470Sstevel@tonic-gate  * config_strerror
8480Sstevel@tonic-gate  */
8490Sstevel@tonic-gate 
8500Sstevel@tonic-gate const char *
8510Sstevel@tonic-gate config_strerror(cfga_err_t cfgerrnum)
8520Sstevel@tonic-gate {
8530Sstevel@tonic-gate 	const char *ep = NULL;
8540Sstevel@tonic-gate 
8550Sstevel@tonic-gate 	if ((cfgerrnum < CFGA_OK) || (cfgerrnum > CFGA_ATTR_INVAL))
8560Sstevel@tonic-gate 		return (NULL);
8570Sstevel@tonic-gate 
8580Sstevel@tonic-gate 	ep = __config_strerror(cfgerrnum);
8590Sstevel@tonic-gate 
8600Sstevel@tonic-gate 	return ((ep != NULL) ? dgettext(TEXT_DOMAIN, ep) : NULL);
8610Sstevel@tonic-gate }
8620Sstevel@tonic-gate 
8630Sstevel@tonic-gate /*
8640Sstevel@tonic-gate  * config_help
8650Sstevel@tonic-gate  */
8660Sstevel@tonic-gate cfga_err_t
8670Sstevel@tonic-gate config_help(
8680Sstevel@tonic-gate 	int num_ap_ids,
8690Sstevel@tonic-gate 	char *const *ap_ids,
8700Sstevel@tonic-gate 	struct cfga_msg *msgp,
8710Sstevel@tonic-gate 	const char *options,
8720Sstevel@tonic-gate 	cfga_flags_t flags)
8730Sstevel@tonic-gate {
8740Sstevel@tonic-gate 	int i;
8750Sstevel@tonic-gate 	lib_loc_t libloc;
8760Sstevel@tonic-gate 	cfga_err_t retval = CFGA_OK;
8770Sstevel@tonic-gate 
8780Sstevel@tonic-gate 	if (check_flags(flags, CFGA_FLAG_FORCE | CFGA_FLAG_VERBOSE, NULL)
8790Sstevel@tonic-gate 	    != CFGA_OK) {
8800Sstevel@tonic-gate 		return (CFGA_ERROR);
8810Sstevel@tonic-gate 	}
8820Sstevel@tonic-gate 
8830Sstevel@tonic-gate 	if (num_ap_ids < 0) {
8840Sstevel@tonic-gate 		return (CFGA_ERROR);
8850Sstevel@tonic-gate 	}
8860Sstevel@tonic-gate 
8870Sstevel@tonic-gate 	if (num_ap_ids > 0 && ap_ids == NULL) {
8880Sstevel@tonic-gate 		return (CFGA_ERROR);
8890Sstevel@tonic-gate 	}
8900Sstevel@tonic-gate 
8910Sstevel@tonic-gate 	/*
8920Sstevel@tonic-gate 	 * operate on each ap_id
8930Sstevel@tonic-gate 	 */
8940Sstevel@tonic-gate 	for (i = 0; (i < num_ap_ids) && (retval == CFGA_OK); i++) {
8950Sstevel@tonic-gate 		libloc.libp = NULL;
8960Sstevel@tonic-gate 		if ((retval = config_get_lib(ap_ids[i], &libloc,
8970Sstevel@tonic-gate 		    NULL)) != CFGA_OK) {
8980Sstevel@tonic-gate 			return (retval);
8990Sstevel@tonic-gate 		}
9000Sstevel@tonic-gate 
9010Sstevel@tonic-gate 		errno = 0;
9020Sstevel@tonic-gate 		retval = (*libloc.libp->cfga_help_p)(msgp, options, flags);
9030Sstevel@tonic-gate 		rele_lib(libloc.libp);
9040Sstevel@tonic-gate 	}
9050Sstevel@tonic-gate 	return (retval);
9060Sstevel@tonic-gate }
9070Sstevel@tonic-gate 
9080Sstevel@tonic-gate /*
9090Sstevel@tonic-gate  * Private support routines for the public interfaces
9100Sstevel@tonic-gate  */
9110Sstevel@tonic-gate 
9120Sstevel@tonic-gate static const char *
9130Sstevel@tonic-gate __config_strerror(cfga_err_t cfgerrnum)
9140Sstevel@tonic-gate {
9150Sstevel@tonic-gate 	const char *ep = NULL;
9160Sstevel@tonic-gate 
9170Sstevel@tonic-gate 	switch (cfgerrnum) {
9180Sstevel@tonic-gate 	case CFGA_OK:
9190Sstevel@tonic-gate 		ep = "Configuration operation succeeded";
9200Sstevel@tonic-gate 		break;
9210Sstevel@tonic-gate 	case CFGA_NACK:
9220Sstevel@tonic-gate 		ep = "Configuration operation cancelled";
9230Sstevel@tonic-gate 		break;
9240Sstevel@tonic-gate 	case CFGA_INVAL:
9250Sstevel@tonic-gate 		ep = "Configuration operation invalid";
9260Sstevel@tonic-gate 		break;
9270Sstevel@tonic-gate 	case CFGA_NOTSUPP:
9280Sstevel@tonic-gate 		ep = "Configuration administration not supported";
9290Sstevel@tonic-gate 		break;
9300Sstevel@tonic-gate 	case CFGA_OPNOTSUPP:
9310Sstevel@tonic-gate 		ep = "Configuration operation not supported";
9320Sstevel@tonic-gate 		break;
9330Sstevel@tonic-gate 	case CFGA_PRIV:
9340Sstevel@tonic-gate 		ep = "Insufficient privileges";
9350Sstevel@tonic-gate 		break;
9360Sstevel@tonic-gate 	case CFGA_BUSY:
9370Sstevel@tonic-gate 		ep = "Component system is busy, try again";
9380Sstevel@tonic-gate 		break;
9390Sstevel@tonic-gate 	case CFGA_SYSTEM_BUSY:
9400Sstevel@tonic-gate 		ep = "System is busy, try again";
9410Sstevel@tonic-gate 		break;
9420Sstevel@tonic-gate 	case CFGA_DATA_ERROR:
9430Sstevel@tonic-gate 		ep = "Data error";
9440Sstevel@tonic-gate 		break;
9450Sstevel@tonic-gate 	case CFGA_LIB_ERROR:
9460Sstevel@tonic-gate 		ep = "Library error";
9470Sstevel@tonic-gate 		break;
9480Sstevel@tonic-gate 	case CFGA_NO_LIB:
9490Sstevel@tonic-gate 		ep = "No Library found";
9500Sstevel@tonic-gate 		break;
9510Sstevel@tonic-gate 	case CFGA_INSUFFICENT_CONDITION:
9520Sstevel@tonic-gate 		ep = "Insufficient condition";
9530Sstevel@tonic-gate 		break;
9540Sstevel@tonic-gate 	case CFGA_ERROR:
9550Sstevel@tonic-gate 		ep = "Hardware specific failure";
9560Sstevel@tonic-gate 		break;
9570Sstevel@tonic-gate 	case CFGA_APID_NOEXIST:
9580Sstevel@tonic-gate 		ep = "Attachment point not found";
9590Sstevel@tonic-gate 		break;
9600Sstevel@tonic-gate 	case CFGA_ATTR_INVAL:
9610Sstevel@tonic-gate 		ep = "No attachment point with specified attributes found";
9620Sstevel@tonic-gate 		break;
9630Sstevel@tonic-gate 	default:
9640Sstevel@tonic-gate 		ep = NULL;
9650Sstevel@tonic-gate 		break;
9660Sstevel@tonic-gate 	}
9670Sstevel@tonic-gate 	return (ep);
9680Sstevel@tonic-gate }
9690Sstevel@tonic-gate 
9700Sstevel@tonic-gate /*
9710Sstevel@tonic-gate  * listopts is a string in the getsubopt(3C) style:
9720Sstevel@tonic-gate  *	name1=value1,name2=value2,
9730Sstevel@tonic-gate  */
9740Sstevel@tonic-gate static cfga_err_t
9750Sstevel@tonic-gate parse_listopt(char *listopts, char **classpp, char **errstring)
9760Sstevel@tonic-gate {
9770Sstevel@tonic-gate 	char *bufp, *optp, *val = NULL;
9780Sstevel@tonic-gate 	cfga_err_t rc = CFGA_ERROR;
9790Sstevel@tonic-gate 
9800Sstevel@tonic-gate 	*classpp = NULL;
9810Sstevel@tonic-gate 
9820Sstevel@tonic-gate 	/*
9830Sstevel@tonic-gate 	 * NULL is a legal value for listopts
9840Sstevel@tonic-gate 	 */
9850Sstevel@tonic-gate 	if (listopts == NULL) {
9860Sstevel@tonic-gate 		return (CFGA_OK);
9870Sstevel@tonic-gate 	}
9880Sstevel@tonic-gate 
9890Sstevel@tonic-gate 	if ((bufp = config_calloc_check(1, strlen(listopts) + 1, errstring))
9900Sstevel@tonic-gate 	    == NULL) {
9910Sstevel@tonic-gate 		return (CFGA_LIB_ERROR);
9920Sstevel@tonic-gate 	}
9930Sstevel@tonic-gate 	(void) strcpy(bufp, listopts);
9940Sstevel@tonic-gate 
9950Sstevel@tonic-gate 	optp = bufp; /* getsubopt() modifies its argument */
9960Sstevel@tonic-gate 	while (*optp != '\0') {
9970Sstevel@tonic-gate 		switch (getsubopt(&optp, listopt_array, &val)) {
9980Sstevel@tonic-gate 		case LISTOPT_CLASS:
9990Sstevel@tonic-gate 			if (val == NULL || *classpp != NULL) {
10000Sstevel@tonic-gate 				rc = CFGA_ERROR;
10010Sstevel@tonic-gate 				goto out;
10020Sstevel@tonic-gate 			}
10030Sstevel@tonic-gate 			if ((*classpp = config_calloc_check(1, strlen(val) + 1,
10040Sstevel@tonic-gate 			    errstring)) == NULL) {
10050Sstevel@tonic-gate 				rc = CFGA_LIB_ERROR;
10060Sstevel@tonic-gate 				goto out;
10070Sstevel@tonic-gate 			}
10080Sstevel@tonic-gate 			(void) strcpy(*classpp, val);
10090Sstevel@tonic-gate 			break;
10100Sstevel@tonic-gate 		default:
10110Sstevel@tonic-gate 			rc = CFGA_ERROR;
10120Sstevel@tonic-gate 			goto out;
10130Sstevel@tonic-gate 		}
10140Sstevel@tonic-gate 	}
10150Sstevel@tonic-gate 
10160Sstevel@tonic-gate 	rc = CFGA_OK;
10170Sstevel@tonic-gate 	/*FALLTHRU*/
10180Sstevel@tonic-gate out:
10190Sstevel@tonic-gate 	S_FREE(bufp);
10200Sstevel@tonic-gate 	if (rc != CFGA_OK) {
10210Sstevel@tonic-gate 		S_FREE(*classpp);
10220Sstevel@tonic-gate 	}
10230Sstevel@tonic-gate 	return (rc);
10240Sstevel@tonic-gate }
10250Sstevel@tonic-gate 
10260Sstevel@tonic-gate /*ARGSUSED*/
10270Sstevel@tonic-gate static cfga_err_t
10280Sstevel@tonic-gate null_mklog(
10290Sstevel@tonic-gate 	di_node_t node,
10300Sstevel@tonic-gate 	di_minor_t minor,
10310Sstevel@tonic-gate 	plugin_lib_t *libp,
10320Sstevel@tonic-gate 	lib_loc_t *liblocp)
10330Sstevel@tonic-gate {
10340Sstevel@tonic-gate 	return (CFGA_OK);
10350Sstevel@tonic-gate }
10360Sstevel@tonic-gate 
10370Sstevel@tonic-gate static cfga_err_t
10380Sstevel@tonic-gate mklog_v1(
10390Sstevel@tonic-gate 	di_node_t node,
10400Sstevel@tonic-gate 	di_minor_t minor,
10410Sstevel@tonic-gate 	plugin_lib_t *libp,
10420Sstevel@tonic-gate 	lib_loc_t *liblocp)
10430Sstevel@tonic-gate {
10440Sstevel@tonic-gate 	const size_t len = CFGA_AP_LOG_ID_LEN;
10450Sstevel@tonic-gate 
10460Sstevel@tonic-gate 	assert(len <=  sizeof (liblocp->ap_logical));
10470Sstevel@tonic-gate 
10480Sstevel@tonic-gate 	if (libp->plugin_vers != CFGA_HSL_V1) {
10490Sstevel@tonic-gate 		return (CFGA_LIB_ERROR);
10500Sstevel@tonic-gate 	}
10510Sstevel@tonic-gate 
10520Sstevel@tonic-gate 	return (mklog_common(node, minor, liblocp, len));
10530Sstevel@tonic-gate }
10540Sstevel@tonic-gate 
10550Sstevel@tonic-gate 
10560Sstevel@tonic-gate /*
10570Sstevel@tonic-gate  * Obtain the devlink from a /devices path
10580Sstevel@tonic-gate  */
10590Sstevel@tonic-gate static int
10600Sstevel@tonic-gate get_link(di_devlink_t devlink, void *arg)
10610Sstevel@tonic-gate {
10620Sstevel@tonic-gate 	char *linkp = (char *)arg;
10630Sstevel@tonic-gate 
10640Sstevel@tonic-gate 	(void) snprintf(linkp, CFGA_LOG_EXT_LEN, "%s",
10650Sstevel@tonic-gate 	    di_devlink_path(devlink));
10660Sstevel@tonic-gate 	return (DI_WALK_TERMINATE);
10670Sstevel@tonic-gate }
10680Sstevel@tonic-gate 
10690Sstevel@tonic-gate static cfga_err_t
10700Sstevel@tonic-gate mklog_v2(
10710Sstevel@tonic-gate 	di_node_t node,
10720Sstevel@tonic-gate 	di_minor_t minor,
10730Sstevel@tonic-gate 	plugin_lib_t *libp,
10740Sstevel@tonic-gate 	lib_loc_t *liblocp)
10750Sstevel@tonic-gate {
10760Sstevel@tonic-gate 	const size_t len = CFGA_LOG_EXT_LEN;
10770Sstevel@tonic-gate 	di_devlink_handle_t hdl;
10780Sstevel@tonic-gate 
10790Sstevel@tonic-gate 	assert(len <=  sizeof (liblocp->ap_logical));
10800Sstevel@tonic-gate 
10810Sstevel@tonic-gate 	if (libp->plugin_vers != CFGA_HSL_V2) {
10820Sstevel@tonic-gate 		return (CFGA_LIB_ERROR);
10830Sstevel@tonic-gate 	}
10840Sstevel@tonic-gate 
10850Sstevel@tonic-gate 	/* open devlink database */
10860Sstevel@tonic-gate 	if ((hdl = di_devlink_init(NULL, 0)) == NULL) {
10870Sstevel@tonic-gate 		return (CFGA_LIB_ERROR);
10880Sstevel@tonic-gate 	}
10890Sstevel@tonic-gate 
10900Sstevel@tonic-gate 	liblocp->ap_logical[0] = '\0';
10910Sstevel@tonic-gate 	(void) di_devlink_walk(hdl, NULL,
10920Sstevel@tonic-gate 	    liblocp->ap_physical + strlen(DEVICES_DIR),
10930Sstevel@tonic-gate 	    DI_PRIMARY_LINK, (void *)liblocp->ap_logical, get_link);
10940Sstevel@tonic-gate 
10950Sstevel@tonic-gate 	(void) di_devlink_fini(&hdl);
10960Sstevel@tonic-gate 
10970Sstevel@tonic-gate 	if (liblocp->ap_logical[0] != '\0')
10980Sstevel@tonic-gate 		return (CFGA_OK);
10990Sstevel@tonic-gate 	return (mklog_common(node, minor, liblocp, len));
11000Sstevel@tonic-gate }
11010Sstevel@tonic-gate 
11020Sstevel@tonic-gate /*
11030Sstevel@tonic-gate  * mklog_common - make a logical name from the driver and instance
11040Sstevel@tonic-gate  */
11050Sstevel@tonic-gate static cfga_err_t
11060Sstevel@tonic-gate mklog_common(
11070Sstevel@tonic-gate 	di_node_t node,
11080Sstevel@tonic-gate 	di_minor_t minor,
11090Sstevel@tonic-gate 	lib_loc_t *libloc_p,
11100Sstevel@tonic-gate 	size_t len)
11110Sstevel@tonic-gate {
11120Sstevel@tonic-gate 	int inst;
11130Sstevel@tonic-gate 	char *drv, *minor_name;
11140Sstevel@tonic-gate 
11150Sstevel@tonic-gate 	drv = di_driver_name(node);
11160Sstevel@tonic-gate 	inst = di_instance(node);
11170Sstevel@tonic-gate 	minor_name = di_minor_name(minor);
11180Sstevel@tonic-gate 
11190Sstevel@tonic-gate 	errno = 0;
11200Sstevel@tonic-gate 	if (drv != NULL && inst != -1 && minor_name != NULL &&
11210Sstevel@tonic-gate 	    snprintf(libloc_p->ap_logical, len, "%s%d:%s", drv, inst,
11220Sstevel@tonic-gate 	    minor_name) < len) {	/* snprintf returns strlen */
11230Sstevel@tonic-gate 		return (CFGA_OK);
11240Sstevel@tonic-gate 	}
11250Sstevel@tonic-gate 
11260Sstevel@tonic-gate 	return (CFGA_LIB_ERROR);
11270Sstevel@tonic-gate }
11280Sstevel@tonic-gate 
11290Sstevel@tonic-gate /*
11300Sstevel@tonic-gate  * resolve_lib_ref - relocate to use plugin lib
11310Sstevel@tonic-gate  */
11320Sstevel@tonic-gate static cfga_err_t
11330Sstevel@tonic-gate resolve_lib_ref(
11340Sstevel@tonic-gate 	plugin_lib_t *libp,
11350Sstevel@tonic-gate 	lib_loc_t *libloc_p)
11360Sstevel@tonic-gate {
11370Sstevel@tonic-gate 	void *sym;
11380Sstevel@tonic-gate 	void *libhdlp = libp->handle;
11390Sstevel@tonic-gate 	int plug_vers;
11400Sstevel@tonic-gate 
11410Sstevel@tonic-gate 	if ((sym = dlsym(libhdlp, "cfga_version")) == NULL) {
11420Sstevel@tonic-gate 		/*
11430Sstevel@tonic-gate 		 * Version symbol not defined, must be the first version
11440Sstevel@tonic-gate 		 */
11450Sstevel@tonic-gate 		plug_vers = CFGA_HSL_V1;
11460Sstevel@tonic-gate 	} else {
11470Sstevel@tonic-gate 		plug_vers =   *((int *)sym);
11480Sstevel@tonic-gate 	}
11490Sstevel@tonic-gate 
11500Sstevel@tonic-gate 	/*
11510Sstevel@tonic-gate 	 * Check if plugin version matches request.
11520Sstevel@tonic-gate 	 */
11530Sstevel@tonic-gate 	if (!compat_plugin(&libloc_p->vers_req, plug_vers)) {
11540Sstevel@tonic-gate 		return (CFGA_NO_LIB);
11550Sstevel@tonic-gate 	}
11560Sstevel@tonic-gate 
11570Sstevel@tonic-gate 	/*
11580Sstevel@tonic-gate 	 * Record the plugin version and setup version dependant routines
11590Sstevel@tonic-gate 	 */
11600Sstevel@tonic-gate 	assert(plug_vers < VERS_ARRAY_SZ);
11610Sstevel@tonic-gate 	libp->plugin_vers = plug_vers;
11620Sstevel@tonic-gate 	libp->vers_ops = &cfga_vers_ops[plug_vers];
11630Sstevel@tonic-gate 
11640Sstevel@tonic-gate 	/* resolve symbols common to all versions */
11650Sstevel@tonic-gate 	if ((sym = dlsym(libhdlp, "cfga_change_state")) == NULL) {
11660Sstevel@tonic-gate 		perror("dlsym: cfga_change_state");
11670Sstevel@tonic-gate 		return (CFGA_LIB_ERROR);
11680Sstevel@tonic-gate 	} else
11690Sstevel@tonic-gate 		libp->cfga_change_state_p = (cfga_err_t (*)(cfga_cmd_t,
11700Sstevel@tonic-gate 		    const char *, const char *, struct cfga_confirm *,
11710Sstevel@tonic-gate 		    struct cfga_msg *, char **, cfga_flags_t)) sym;
11720Sstevel@tonic-gate 
11730Sstevel@tonic-gate 	if ((sym = dlsym(libhdlp, "cfga_private_func")) == NULL) {
11740Sstevel@tonic-gate 		perror("dlsym: cfga_private_func");
11750Sstevel@tonic-gate 		return (CFGA_LIB_ERROR);
11760Sstevel@tonic-gate 	} else
11770Sstevel@tonic-gate 		libp->cfga_private_func_p = (cfga_err_t (*)(const char *,
11780Sstevel@tonic-gate 		    const char *, const char *, struct cfga_confirm *,
11790Sstevel@tonic-gate 		    struct cfga_msg *, char **, cfga_flags_t))sym;
11800Sstevel@tonic-gate 
11810Sstevel@tonic-gate 	if ((sym = dlsym(libhdlp, "cfga_test")) == NULL) {
11820Sstevel@tonic-gate 		perror("dlsym: cfga_test");
11830Sstevel@tonic-gate 		return (CFGA_LIB_ERROR);
11840Sstevel@tonic-gate 	} else
11850Sstevel@tonic-gate 		libp->cfga_test_p = (cfga_err_t (*)(const char *, const char *,
11860Sstevel@tonic-gate 		    struct cfga_msg *, char **, cfga_flags_t))sym;
11870Sstevel@tonic-gate 
11880Sstevel@tonic-gate 	if ((sym = dlsym(libhdlp, "cfga_help")) == NULL) {
11890Sstevel@tonic-gate 		perror("dlsym: cfga_help");
11900Sstevel@tonic-gate 		return (CFGA_LIB_ERROR);
11910Sstevel@tonic-gate 	} else
11920Sstevel@tonic-gate 		libp->cfga_help_p = (cfga_err_t (*)(struct cfga_msg *,
11930Sstevel@tonic-gate 		    const char *, cfga_flags_t))sym;
11940Sstevel@tonic-gate 
11950Sstevel@tonic-gate 	if ((sym = dlsym(libhdlp, "cfga_ap_id_cmp")) == NULL) {
11960Sstevel@tonic-gate 		libp->cfga_ap_id_cmp_p = default_ap_id_cmp;
11970Sstevel@tonic-gate 	} else
11980Sstevel@tonic-gate 		libp->cfga_ap_id_cmp_p = (int (*)(const
11990Sstevel@tonic-gate 		    cfga_ap_log_id_t, const cfga_ap_log_id_t))sym;
12000Sstevel@tonic-gate 
12010Sstevel@tonic-gate 	/* Resolve version specific symbols */
12020Sstevel@tonic-gate 	return (libp->vers_ops->resolve_lib(libp));
12030Sstevel@tonic-gate }
12040Sstevel@tonic-gate 
12050Sstevel@tonic-gate /*ARGSUSED*/
12060Sstevel@tonic-gate static cfga_err_t
12070Sstevel@tonic-gate null_resolve(plugin_lib_t *libp)
12080Sstevel@tonic-gate {
12090Sstevel@tonic-gate 	return (CFGA_OK);
12100Sstevel@tonic-gate }
12110Sstevel@tonic-gate 
12120Sstevel@tonic-gate static cfga_err_t
12130Sstevel@tonic-gate resolve_v1(plugin_lib_t *libp)
12140Sstevel@tonic-gate {
12150Sstevel@tonic-gate 	void *sym, *libhdlp = libp->handle;
12160Sstevel@tonic-gate 
12170Sstevel@tonic-gate 
12180Sstevel@tonic-gate 	if (libp->plugin_vers != CFGA_HSL_V1) {
12190Sstevel@tonic-gate 		return (CFGA_NO_LIB);
12200Sstevel@tonic-gate 	}
12210Sstevel@tonic-gate 
12220Sstevel@tonic-gate 	if ((sym = dlsym(libhdlp, "cfga_stat")) == NULL) {
12230Sstevel@tonic-gate 		perror("dlsym: cfga_stat");
12240Sstevel@tonic-gate 		return (CFGA_LIB_ERROR);
12250Sstevel@tonic-gate 	} else
12260Sstevel@tonic-gate 		libp->cfga_stat_p = (cfga_err_t (*)(const char *,
12270Sstevel@tonic-gate 		    struct cfga_stat_data *, const char *,
12280Sstevel@tonic-gate 		    char **))sym;
12290Sstevel@tonic-gate 
12300Sstevel@tonic-gate 	if ((sym = dlsym(libhdlp, "cfga_list")) == NULL) {
12310Sstevel@tonic-gate 		perror("dlsym: cfga_list");
12320Sstevel@tonic-gate 		return (CFGA_LIB_ERROR);
12330Sstevel@tonic-gate 	} else
12340Sstevel@tonic-gate 		libp->cfga_list_p = (cfga_err_t (*)(struct cfga_stat_data **,
12350Sstevel@tonic-gate 		    int *, const char *, char **))sym;
12360Sstevel@tonic-gate 
12370Sstevel@tonic-gate 	return (CFGA_OK);
12380Sstevel@tonic-gate }
12390Sstevel@tonic-gate 
12400Sstevel@tonic-gate static cfga_err_t
12410Sstevel@tonic-gate resolve_v2(plugin_lib_t *libp)
12420Sstevel@tonic-gate {
12430Sstevel@tonic-gate 	void *sym;
12440Sstevel@tonic-gate 
12450Sstevel@tonic-gate 
12460Sstevel@tonic-gate 	if (libp->plugin_vers != CFGA_HSL_V2) {
12470Sstevel@tonic-gate 		return (CFGA_NO_LIB);
12480Sstevel@tonic-gate 	}
12490Sstevel@tonic-gate 
12500Sstevel@tonic-gate 	if ((sym = dlsym(libp->handle, "cfga_list_ext")) == NULL) {
12510Sstevel@tonic-gate 		perror("dlsym: cfga_list_ext");
12520Sstevel@tonic-gate 		return (CFGA_LIB_ERROR);
12530Sstevel@tonic-gate 	} else {
12540Sstevel@tonic-gate 		libp->cfga_list_ext_p = (cfga_err_t (*)(const char *,
12550Sstevel@tonic-gate 		    struct cfga_list_data **, int *, const char *,
12560Sstevel@tonic-gate 		    const char *, char **, cfga_flags_t))sym;
12570Sstevel@tonic-gate 		return (CFGA_OK);
12580Sstevel@tonic-gate 	}
12590Sstevel@tonic-gate }
12600Sstevel@tonic-gate 
12610Sstevel@tonic-gate /*
12620Sstevel@tonic-gate  * config_calloc_check - perform allocation, check result and
12630Sstevel@tonic-gate  * set error string
12640Sstevel@tonic-gate  */
12650Sstevel@tonic-gate static void *
12660Sstevel@tonic-gate config_calloc_check(
12670Sstevel@tonic-gate 	size_t nelem,
12680Sstevel@tonic-gate 	size_t elsize,
12690Sstevel@tonic-gate 	char **errstring)
12700Sstevel@tonic-gate {
12710Sstevel@tonic-gate 	void *p;
12720Sstevel@tonic-gate 
12730Sstevel@tonic-gate 	p = calloc(nelem, elsize);
12740Sstevel@tonic-gate 	if (p == NULL) {
12750Sstevel@tonic-gate 		config_err(0, ALLOC_FAILED, errstring);
12760Sstevel@tonic-gate 	}
12770Sstevel@tonic-gate 
12780Sstevel@tonic-gate 	return (p);
12790Sstevel@tonic-gate }
12800Sstevel@tonic-gate 
12810Sstevel@tonic-gate 
12820Sstevel@tonic-gate /*
12830Sstevel@tonic-gate  * config_get_lib - given an ap_id find the library name
12840Sstevel@tonic-gate  *	If successful, the plugin library is held.
12850Sstevel@tonic-gate  */
12860Sstevel@tonic-gate static cfga_err_t
12870Sstevel@tonic-gate config_get_lib(
12880Sstevel@tonic-gate 	const char *ap_id,
12890Sstevel@tonic-gate 	lib_loc_t *lib_loc_p,
12900Sstevel@tonic-gate 	char **errstring)
12910Sstevel@tonic-gate {
12920Sstevel@tonic-gate 	char *dyncomp, path[PATH_MAX];
12930Sstevel@tonic-gate 	char *apdup;
12940Sstevel@tonic-gate 	cfga_ap_types_t type = UNKNOWN_AP;
12950Sstevel@tonic-gate 	cfga_err_t ret = CFGA_ERROR;
12960Sstevel@tonic-gate 
12970Sstevel@tonic-gate 	if (ap_id == NULL) {
12980Sstevel@tonic-gate 		config_err(0, INVALID_ARGS, errstring);
12990Sstevel@tonic-gate 		return (ret);
13000Sstevel@tonic-gate 	}
13010Sstevel@tonic-gate 
13020Sstevel@tonic-gate 	lib_loc_p->libp = NULL;
13030Sstevel@tonic-gate 
13040Sstevel@tonic-gate 	if ((apdup = config_calloc_check(1, strlen(ap_id) + 1, errstring))
13050Sstevel@tonic-gate 	    == NULL) {
13060Sstevel@tonic-gate 		return (CFGA_LIB_ERROR);
13070Sstevel@tonic-gate 	}
13080Sstevel@tonic-gate 	(void) strcpy(apdup, ap_id);
13090Sstevel@tonic-gate 
13100Sstevel@tonic-gate 	/*
13110Sstevel@tonic-gate 	 * Separate into base and dynamic components
13120Sstevel@tonic-gate 	 */
13130Sstevel@tonic-gate 	if ((ret = split_apid(apdup, &dyncomp, errstring)) != CFGA_OK) {
13140Sstevel@tonic-gate 		goto out;
13150Sstevel@tonic-gate 	}
13160Sstevel@tonic-gate 
13170Sstevel@tonic-gate 	/*
13180Sstevel@tonic-gate 	 * No upper limit on version
13190Sstevel@tonic-gate 	 */
13200Sstevel@tonic-gate 	lib_loc_p->vers_req.v_max = CFGA_HSL_VERS;
13210Sstevel@tonic-gate 	if (dyncomp != NULL) {
13220Sstevel@tonic-gate 		/*
13230Sstevel@tonic-gate 		 * We need atleast version 2 of the plug-in library
13240Sstevel@tonic-gate 		 * interface since the ap_id has a dynamic component.
13250Sstevel@tonic-gate 		 */
13260Sstevel@tonic-gate 
13270Sstevel@tonic-gate 		lib_loc_p->vers_req.v_min = CFGA_HSL_V2;
13280Sstevel@tonic-gate 	} else {
13290Sstevel@tonic-gate 		lib_loc_p->vers_req.v_min = CFGA_HSL_V1;
13300Sstevel@tonic-gate 	}
13310Sstevel@tonic-gate 
13320Sstevel@tonic-gate 	/*
13330Sstevel@tonic-gate 	 * If the ap_id is a devlink in CFGA_DEV_DIR, follow link
13340Sstevel@tonic-gate 	 * to get the physical ap_id.
13350Sstevel@tonic-gate 	 */
13360Sstevel@tonic-gate 	if ((type = find_arg_type(apdup)) == LOGICAL_LINK_AP) {
13370Sstevel@tonic-gate 		(void) snprintf(lib_loc_p->ap_base, sizeof (lib_loc_p->ap_base),
13380Sstevel@tonic-gate 		    "%s%s", CFGA_DEV_DIR SLASH, apdup);
13390Sstevel@tonic-gate 	}
13400Sstevel@tonic-gate 
13410Sstevel@tonic-gate 	path[sizeof (path) - 1] = '\0';
13420Sstevel@tonic-gate 	if (type == LOGICAL_LINK_AP && realpath(lib_loc_p->ap_base, path)
13430Sstevel@tonic-gate 	    != NULL) {
13440Sstevel@tonic-gate 		(void) snprintf(lib_loc_p->ap_base, sizeof (lib_loc_p->ap_base),
13450Sstevel@tonic-gate 		    "%s", path);
13460Sstevel@tonic-gate 	} else {
13470Sstevel@tonic-gate 		(void) snprintf(lib_loc_p->ap_base, sizeof (lib_loc_p->ap_base),
13480Sstevel@tonic-gate 		    "%s", apdup);
13490Sstevel@tonic-gate 	}
13500Sstevel@tonic-gate 
13510Sstevel@tonic-gate 
13520Sstevel@tonic-gate 	/*
13530Sstevel@tonic-gate 	 * find and load the library
13540Sstevel@tonic-gate 	 * The base component of the ap_id is used to locate the plug-in
13550Sstevel@tonic-gate 	 */
13560Sstevel@tonic-gate 	if ((type = find_arg_type(lib_loc_p->ap_base)) == PHYSICAL_AP) {
13570Sstevel@tonic-gate 		/*
13580Sstevel@tonic-gate 		 * physical ap_id: Use ap_base as root for tree walk
13590Sstevel@tonic-gate 		 * A link based apid (logical) will resolve to a physical
13600Sstevel@tonic-gate 		 * ap_id.
13610Sstevel@tonic-gate 		 */
13620Sstevel@tonic-gate 		ret = find_ap_common(lib_loc_p, lib_loc_p->ap_base,
13630Sstevel@tonic-gate 		    check_ap_phys, errstring);
13640Sstevel@tonic-gate 	} else if ((type == LOGICAL_DRV_AP) ||
13650Sstevel@tonic-gate 	    (type == AP_TYPE && dyncomp == NULL)) {
13660Sstevel@tonic-gate 		/*
13670Sstevel@tonic-gate 		 * logical ap_id or ap_type: Use "/" as root for tree walk
13680Sstevel@tonic-gate 		 * Note: an aptype cannot have a dynamic component
13690Sstevel@tonic-gate 		 */
13700Sstevel@tonic-gate 		ret = find_ap_common(lib_loc_p, "/", check_ap, errstring);
13710Sstevel@tonic-gate 	} else {
13720Sstevel@tonic-gate 		ret = CFGA_APID_NOEXIST;
13730Sstevel@tonic-gate 	}
13740Sstevel@tonic-gate 
13750Sstevel@tonic-gate 	if (ret == CFGA_OK) {
13760Sstevel@tonic-gate #ifndef	NDEBUG
13770Sstevel@tonic-gate 		/*
13780Sstevel@tonic-gate 		 * variables used by assert() only which is disabled
13790Sstevel@tonic-gate 		 * by defining NDEBUG (see top of this file)
13800Sstevel@tonic-gate 		 */
13810Sstevel@tonic-gate 		plugin_lib_t *libp;
13820Sstevel@tonic-gate 
13830Sstevel@tonic-gate 		libp = lib_loc_p->libp;
13840Sstevel@tonic-gate #endif	/* NDEBUG */
13850Sstevel@tonic-gate 
13860Sstevel@tonic-gate 		assert(strcmp(libp->libpath, lib_loc_p->pathname) == 0);
13870Sstevel@tonic-gate 		assert(VALID_HSL_VERS(libp->plugin_vers));
13880Sstevel@tonic-gate 
13890Sstevel@tonic-gate 		/*
13900Sstevel@tonic-gate 		 * If a dynamic component was present, v1 plug-ins are not
13910Sstevel@tonic-gate 		 * acceptable.
13920Sstevel@tonic-gate 		 */
13930Sstevel@tonic-gate 		assert(dyncomp == NULL || libp->plugin_vers >= CFGA_HSL_V2);
13940Sstevel@tonic-gate 
13950Sstevel@tonic-gate 		/*
13960Sstevel@tonic-gate 		 * ap_physical is passed to plugins as their ap_id argument.
13970Sstevel@tonic-gate 		 * Append dynamic component if any.
13980Sstevel@tonic-gate 		 */
13990Sstevel@tonic-gate 		append_dyn(lib_loc_p->ap_physical, dyncomp,
14000Sstevel@tonic-gate 		    sizeof (lib_loc_p->ap_physical));
14010Sstevel@tonic-gate 	}
14020Sstevel@tonic-gate 
14030Sstevel@tonic-gate 	/* cleanup */
14040Sstevel@tonic-gate 	lib_loc_p->vers_req.v_min = INVALID_VERSION;
14050Sstevel@tonic-gate 	lib_loc_p->vers_req.v_max = INVALID_VERSION;
14060Sstevel@tonic-gate 	*lib_loc_p->ap_base = '\0';
14070Sstevel@tonic-gate 
14080Sstevel@tonic-gate 	/*FALLTHRU*/
14090Sstevel@tonic-gate out:
14100Sstevel@tonic-gate 	S_FREE(apdup);
14110Sstevel@tonic-gate 	S_FREE(dyncomp);
14120Sstevel@tonic-gate 	if (ret != CFGA_OK) {
14130Sstevel@tonic-gate 		lib_loc_p->libp = NULL;
14140Sstevel@tonic-gate 	}
14150Sstevel@tonic-gate 
14160Sstevel@tonic-gate 	assert(ret != CFGA_OK || lib_loc_p->libp != NULL);
14170Sstevel@tonic-gate 
14180Sstevel@tonic-gate 	return (ret);
14190Sstevel@tonic-gate }
14200Sstevel@tonic-gate 
14210Sstevel@tonic-gate 
14220Sstevel@tonic-gate /*
14230Sstevel@tonic-gate  * load_lib - Given a library pathname, create a entry for it
14240Sstevel@tonic-gate  * in the library list, if one does not already exist, and read
14250Sstevel@tonic-gate  * lock it to keep it there.
14260Sstevel@tonic-gate  */
14270Sstevel@tonic-gate static cfga_err_t
14280Sstevel@tonic-gate load_lib(
14290Sstevel@tonic-gate 	di_node_t node,
14300Sstevel@tonic-gate 	di_minor_t minor,
14310Sstevel@tonic-gate 	lib_loc_t *libloc_p)
14320Sstevel@tonic-gate {
14330Sstevel@tonic-gate 	plugin_lib_t *libp, *list_libp;
14340Sstevel@tonic-gate 	char *devfs_path;
14350Sstevel@tonic-gate 
14360Sstevel@tonic-gate 	/*
14370Sstevel@tonic-gate 	 * lock the library list
14380Sstevel@tonic-gate 	 */
14390Sstevel@tonic-gate 	(void) mutex_lock(&plugin_list_lock);
14400Sstevel@tonic-gate 
14410Sstevel@tonic-gate 	/*
14420Sstevel@tonic-gate 	 * see if lib exist in list, if not, allocate a new one
14430Sstevel@tonic-gate 	 */
14440Sstevel@tonic-gate 	list_libp = lib_in_list(libloc_p->pathname);
14450Sstevel@tonic-gate 	if (list_libp != NULL) {
14460Sstevel@tonic-gate 		hold_lib(list_libp);
14470Sstevel@tonic-gate 		(void) mutex_unlock(&plugin_list_lock);
14480Sstevel@tonic-gate 
14490Sstevel@tonic-gate 		/* fill in logical and physical name in libloc_p */
14500Sstevel@tonic-gate 		libloc_p->libp = libp = list_libp;
14510Sstevel@tonic-gate 		if (libp->vers_ops->mklog(node, minor, libp, libloc_p)
14520Sstevel@tonic-gate 		    != CFGA_OK) {
14530Sstevel@tonic-gate 			rele_lib(list_libp);
14540Sstevel@tonic-gate 			return (CFGA_LIB_ERROR);
14550Sstevel@tonic-gate 		}
14560Sstevel@tonic-gate 
14570Sstevel@tonic-gate 		devfs_path = di_devfs_path(node);
14580Sstevel@tonic-gate 		(void) snprintf(libloc_p->ap_physical, MAXPATHLEN, "%s%s:%s",
14590Sstevel@tonic-gate 		    DEVICES_DIR, devfs_path, di_minor_name(minor));
14600Sstevel@tonic-gate 		di_devfs_path_free(devfs_path);
14610Sstevel@tonic-gate 
14620Sstevel@tonic-gate 		return (CFGA_OK);
14630Sstevel@tonic-gate 	}
14640Sstevel@tonic-gate 
14650Sstevel@tonic-gate 	/* allocate a new plugin_lib_t structure */
14660Sstevel@tonic-gate 	libp = config_calloc_check(1, sizeof (plugin_lib_t), NULL);
14670Sstevel@tonic-gate 	if (libp == NULL) {
14680Sstevel@tonic-gate 		(void) mutex_unlock(&plugin_list_lock);
14690Sstevel@tonic-gate 		return (CFGA_LIB_ERROR);
14700Sstevel@tonic-gate 	}
14710Sstevel@tonic-gate 
14720Sstevel@tonic-gate 	(void) snprintf(libp->libpath, sizeof (libp->libpath), "%s",
14730Sstevel@tonic-gate 	    libloc_p->pathname);
14740Sstevel@tonic-gate 
14750Sstevel@tonic-gate 	/*
14760Sstevel@tonic-gate 	 * ensure that the lib is open and linked in
14770Sstevel@tonic-gate 	 */
14780Sstevel@tonic-gate 	libp->handle = dlopen(libp->libpath, RTLD_NOW);
14790Sstevel@tonic-gate 	if (libp->handle == NULL) {
14800Sstevel@tonic-gate 		(void) mutex_unlock(&plugin_list_lock);
14810Sstevel@tonic-gate 		free(libp);
14820Sstevel@tonic-gate 		return (CFGA_NO_LIB);
14830Sstevel@tonic-gate 	}
14840Sstevel@tonic-gate 
14850Sstevel@tonic-gate 	if (resolve_lib_ref(libp, libloc_p) != CFGA_OK ||
14860Sstevel@tonic-gate 	    libp->vers_ops->mklog(node, minor, libp, libloc_p) != CFGA_OK) {
14870Sstevel@tonic-gate 		(void) mutex_unlock(&plugin_list_lock);
14880Sstevel@tonic-gate 		(void) dlclose(libp->handle);
14890Sstevel@tonic-gate 		free(libp);
14900Sstevel@tonic-gate 		return (CFGA_NO_LIB);
14910Sstevel@tonic-gate 	}
14920Sstevel@tonic-gate 
14930Sstevel@tonic-gate 	/*
14940Sstevel@tonic-gate 	 * link in new entry to the end of list
14950Sstevel@tonic-gate 	 */
14960Sstevel@tonic-gate 	list_libp = &plugin_list;
14970Sstevel@tonic-gate 	while (list_libp->next != NULL)
14980Sstevel@tonic-gate 		list_libp = list_libp->next;
14990Sstevel@tonic-gate 	libp->next = list_libp->next;
15000Sstevel@tonic-gate 	list_libp->next = libp;
15010Sstevel@tonic-gate 
15020Sstevel@tonic-gate 	/* Initialize refcnt to 1 */
15030Sstevel@tonic-gate 	libp->refcnt = 1;
15040Sstevel@tonic-gate 	(void) mutex_init(&libp->lock, USYNC_THREAD, NULL);
15050Sstevel@tonic-gate 
15060Sstevel@tonic-gate 	(void) mutex_unlock(&plugin_list_lock);
15070Sstevel@tonic-gate 
15080Sstevel@tonic-gate 	/*
15090Sstevel@tonic-gate 	 * record libp and physical node name in the libloc struct
15100Sstevel@tonic-gate 	 */
15110Sstevel@tonic-gate 	libloc_p->libp = libp;
15120Sstevel@tonic-gate 	devfs_path = di_devfs_path(node);
15130Sstevel@tonic-gate 	(void) snprintf(libloc_p->ap_physical, MAXPATHLEN, "%s%s:%s",
15140Sstevel@tonic-gate 	    DEVICES_DIR, devfs_path, di_minor_name(minor));
15150Sstevel@tonic-gate 	di_devfs_path_free(devfs_path);
15160Sstevel@tonic-gate 
15170Sstevel@tonic-gate 	return (CFGA_OK);
15180Sstevel@tonic-gate }
15190Sstevel@tonic-gate 
15200Sstevel@tonic-gate 
15210Sstevel@tonic-gate #define	NUM_LIB_NAMES   2
15220Sstevel@tonic-gate 
15230Sstevel@tonic-gate /*
15240Sstevel@tonic-gate  * find_lib - Given an attachment point node find it's library
15250Sstevel@tonic-gate  *
15260Sstevel@tonic-gate  */
15270Sstevel@tonic-gate static cfga_err_t
15280Sstevel@tonic-gate find_lib(
15290Sstevel@tonic-gate 	di_node_t node,
15300Sstevel@tonic-gate 	di_minor_t minor,
15310Sstevel@tonic-gate 	lib_loc_t *libloc_p)
15320Sstevel@tonic-gate {
15330Sstevel@tonic-gate 	char lib[MAXPATHLEN];
15340Sstevel@tonic-gate 	char name[NUM_LIB_NAMES][MAXPATHLEN];
15350Sstevel@tonic-gate 	struct stat lib_stat;
15360Sstevel@tonic-gate 	void *dlhandle = NULL;
15371957Sdnielsen 	static char plat_name[SYSINFO_LENGTH];
15380Sstevel@tonic-gate 	static char machine_name[SYSINFO_LENGTH];
15390Sstevel@tonic-gate 	static char arch_name[SYSINFO_LENGTH];
15400Sstevel@tonic-gate 	int i = 0;
15410Sstevel@tonic-gate 	char *class = NULL, *drv = NULL;
15420Sstevel@tonic-gate 
15430Sstevel@tonic-gate 
15440Sstevel@tonic-gate 	/* Make sure pathname and class is null if we fail */
15450Sstevel@tonic-gate 	*libloc_p->ap_class = *libloc_p->pathname = *lib = '\0';
15460Sstevel@tonic-gate 
15470Sstevel@tonic-gate 	/*
15480Sstevel@tonic-gate 	 * Initialize machine name and arch name
15490Sstevel@tonic-gate 	 */
15500Sstevel@tonic-gate 	if (strncmp("", machine_name, MAXPATHLEN) == 0) {
15511957Sdnielsen 		if (sysinfo(SI_PLATFORM, plat_name, SYSINFO_LENGTH) == -1) {
15520Sstevel@tonic-gate 			return (CFGA_ERROR);
15530Sstevel@tonic-gate 		}
15540Sstevel@tonic-gate 		if (sysinfo(SI_ARCHITECTURE, arch_name, SYSINFO_LENGTH) == -1) {
15550Sstevel@tonic-gate 			return (CFGA_ERROR);
15560Sstevel@tonic-gate 		}
15571957Sdnielsen 		if (sysinfo(SI_MACHINE, machine_name, SYSINFO_LENGTH) == -1) {
15581957Sdnielsen 			return (CFGA_ERROR);
15591957Sdnielsen 		}
15600Sstevel@tonic-gate 	}
15610Sstevel@tonic-gate 
15620Sstevel@tonic-gate 	/*
15630Sstevel@tonic-gate 	 * Initialize possible library tags.
15640Sstevel@tonic-gate 	 */
15650Sstevel@tonic-gate 
15660Sstevel@tonic-gate 	drv = di_driver_name(node);
15670Sstevel@tonic-gate 	class = get_class(minor);
15680Sstevel@tonic-gate 
15690Sstevel@tonic-gate 	if (drv == NULL || class == NULL) {
15700Sstevel@tonic-gate 		return (CFGA_LIB_ERROR);
15710Sstevel@tonic-gate 	}
15720Sstevel@tonic-gate 
15730Sstevel@tonic-gate 	i = 0;
15740Sstevel@tonic-gate 	(void) snprintf(&name[i++][0], sizeof (name[0]), "%s", drv);
15750Sstevel@tonic-gate 	(void) snprintf(&name[i++][0], sizeof (name[0]), "%s", class);
15760Sstevel@tonic-gate 
15770Sstevel@tonic-gate 	/*
15780Sstevel@tonic-gate 	 * Cycle through the array of names to find the library.
15790Sstevel@tonic-gate 	 */
15800Sstevel@tonic-gate 	for (i = 0; i < NUM_LIB_NAMES; i++) {
15810Sstevel@tonic-gate 
15820Sstevel@tonic-gate 		/* Attachment points may not have a class (i.e. are generic) */
15830Sstevel@tonic-gate 		if (name[i][0] == '\0') {
15840Sstevel@tonic-gate 			continue;
15850Sstevel@tonic-gate 		}
15860Sstevel@tonic-gate 
15870Sstevel@tonic-gate 		/*
15881957Sdnielsen 		 * Try path based upon platform name
15891957Sdnielsen 		 */
15901957Sdnielsen 		(void) snprintf(lib, sizeof (lib), "%s%s%s%s%s",
15911957Sdnielsen 		    LIB_PATH_BASE1, plat_name, LIB_PATH_MIDDLE,
15921957Sdnielsen 		    name[i], LIB_PATH_TAIL);
15931957Sdnielsen 
15941957Sdnielsen 		if (stat(lib, &lib_stat) == 0) {
15951957Sdnielsen 			/* file exists, is it a lib */
15961957Sdnielsen 			dlhandle = dlopen(lib, RTLD_LAZY);
15971957Sdnielsen 			if (dlhandle != NULL) {
15981957Sdnielsen 				goto found;
15991957Sdnielsen 			}
16001957Sdnielsen 		}
16011957Sdnielsen 
16021957Sdnielsen 		/*
16030Sstevel@tonic-gate 		 * Try path based upon machine name
16040Sstevel@tonic-gate 		 */
16050Sstevel@tonic-gate 		(void) snprintf(lib, sizeof (lib), "%s%s%s%s%s",
16060Sstevel@tonic-gate 		    LIB_PATH_BASE1, machine_name, LIB_PATH_MIDDLE,
16070Sstevel@tonic-gate 		    name[i], LIB_PATH_TAIL);
16080Sstevel@tonic-gate 
16090Sstevel@tonic-gate 
16100Sstevel@tonic-gate 		if (stat(lib, &lib_stat) == 0) {
16110Sstevel@tonic-gate 			/* file exists, is it a lib */
16120Sstevel@tonic-gate 			dlhandle = dlopen(lib, RTLD_LAZY);
16130Sstevel@tonic-gate 			if (dlhandle != NULL) {
16140Sstevel@tonic-gate 				goto found;
16150Sstevel@tonic-gate 			}
16160Sstevel@tonic-gate 		}
16170Sstevel@tonic-gate 
16180Sstevel@tonic-gate 		/*
16190Sstevel@tonic-gate 		 * Try path based upon arch name
16200Sstevel@tonic-gate 		 */
16210Sstevel@tonic-gate 		(void) snprintf(lib, sizeof (lib), "%s%s%s%s%s",
16220Sstevel@tonic-gate 		    LIB_PATH_BASE1, arch_name, LIB_PATH_MIDDLE,
16230Sstevel@tonic-gate 		    name[i], LIB_PATH_TAIL);
16240Sstevel@tonic-gate 
16250Sstevel@tonic-gate 		if (stat(lib, &lib_stat) == 0) {
16260Sstevel@tonic-gate 			/* file exists, is it a lib */
16270Sstevel@tonic-gate 			dlhandle = dlopen(lib, RTLD_LAZY);
16280Sstevel@tonic-gate 			if (dlhandle != NULL) {
16290Sstevel@tonic-gate 				goto found;
16300Sstevel@tonic-gate 			}
16310Sstevel@tonic-gate 
16320Sstevel@tonic-gate 		}
16330Sstevel@tonic-gate 
16340Sstevel@tonic-gate 		/*
16350Sstevel@tonic-gate 		 * Try generic location
16360Sstevel@tonic-gate 		 */
16370Sstevel@tonic-gate 		(void) snprintf(lib, sizeof (lib), "%s%s%s%s",
16380Sstevel@tonic-gate 		    LIB_PATH_BASE2, LIB_PATH_MIDDLE, name[i], LIB_PATH_TAIL);
16390Sstevel@tonic-gate 
16400Sstevel@tonic-gate 
16410Sstevel@tonic-gate 
16420Sstevel@tonic-gate 		if (stat(lib, &lib_stat) == 0) {
16430Sstevel@tonic-gate 			/* file exists, is it a lib */
16440Sstevel@tonic-gate 			dlhandle = dlopen(lib, RTLD_LAZY);
16450Sstevel@tonic-gate 			if (dlhandle != NULL) {
16460Sstevel@tonic-gate 				goto found;
16470Sstevel@tonic-gate 			}
16480Sstevel@tonic-gate 
16490Sstevel@tonic-gate 		}
16500Sstevel@tonic-gate 	}
16510Sstevel@tonic-gate 
16520Sstevel@tonic-gate 
16530Sstevel@tonic-gate 	return (CFGA_NO_LIB);
16540Sstevel@tonic-gate 
16550Sstevel@tonic-gate found:
16560Sstevel@tonic-gate 	/* we got one! */
16570Sstevel@tonic-gate 	(void) snprintf(libloc_p->pathname, sizeof (libloc_p->pathname), "%s",
16580Sstevel@tonic-gate 	    lib);
16590Sstevel@tonic-gate 
16600Sstevel@tonic-gate 	(void) dlclose(dlhandle);
16610Sstevel@tonic-gate 
16620Sstevel@tonic-gate 	/* Record class name (if any) */
16630Sstevel@tonic-gate 	(void) snprintf(libloc_p->ap_class, sizeof (libloc_p->ap_class), "%s",
16640Sstevel@tonic-gate 	    class);
16650Sstevel@tonic-gate 
16660Sstevel@tonic-gate 	return (CFGA_OK);
16670Sstevel@tonic-gate }
16680Sstevel@tonic-gate 
16690Sstevel@tonic-gate static cfga_err_t
16700Sstevel@tonic-gate lookup_cache(lib_loc_t *libloc_p)
16710Sstevel@tonic-gate {
16720Sstevel@tonic-gate 	lib_cache_t *entry;
16730Sstevel@tonic-gate 	(void) mutex_lock(&lib_cache_lock);
16740Sstevel@tonic-gate 	entry = lib_cache;
16750Sstevel@tonic-gate 	while (entry) {
16760Sstevel@tonic-gate 		if (strcmp(entry->lc_ap_id, libloc_p->ap_base) == 0) {
16770Sstevel@tonic-gate 			plugin_lib_t *libp = entry->lc_libp;
16780Sstevel@tonic-gate 			libloc_p->libp = libp;
16790Sstevel@tonic-gate 			hold_lib(libp);
16800Sstevel@tonic-gate 			(void) strcpy(libloc_p->pathname, libp->libpath);
16810Sstevel@tonic-gate 			(void) strcpy(libloc_p->ap_physical,
16820Sstevel@tonic-gate 			    entry->lc_ap_physical);
16830Sstevel@tonic-gate 			(void) strcpy(libloc_p->ap_logical,
16840Sstevel@tonic-gate 			    entry->lc_ap_logical);
16850Sstevel@tonic-gate 			(void) mutex_unlock(&lib_cache_lock);
16860Sstevel@tonic-gate 			return (CFGA_OK);
16870Sstevel@tonic-gate 		}
16880Sstevel@tonic-gate 		entry = entry->lc_next;
16890Sstevel@tonic-gate 	}
16900Sstevel@tonic-gate 	(void) mutex_unlock(&lib_cache_lock);
16910Sstevel@tonic-gate 
16920Sstevel@tonic-gate 	return (CFGA_ERROR);
16930Sstevel@tonic-gate }
16940Sstevel@tonic-gate 
16950Sstevel@tonic-gate static void
16960Sstevel@tonic-gate update_cache(lib_loc_t *libloc_p)
16970Sstevel@tonic-gate {
16980Sstevel@tonic-gate 	lib_cache_t *entry;
16990Sstevel@tonic-gate 	entry = config_calloc_check(1, sizeof (lib_cache_t), NULL);
17000Sstevel@tonic-gate 	if (entry == NULL)
17010Sstevel@tonic-gate 		return;
17020Sstevel@tonic-gate 
17030Sstevel@tonic-gate 	entry->lc_ap_id = strdup(libloc_p->ap_base);
17040Sstevel@tonic-gate 	entry->lc_ap_physical = strdup(libloc_p->ap_physical);
17050Sstevel@tonic-gate 	entry->lc_ap_logical = strdup(libloc_p->ap_logical);
17060Sstevel@tonic-gate 	if ((entry->lc_ap_id == NULL) || (entry->lc_ap_physical == NULL) ||
17070Sstevel@tonic-gate 	    (entry->lc_ap_logical == NULL)) {
17080Sstevel@tonic-gate 		free(entry->lc_ap_id);
17090Sstevel@tonic-gate 		free(entry->lc_ap_physical);
17100Sstevel@tonic-gate 		free(entry->lc_ap_logical);
17110Sstevel@tonic-gate 		free(entry);
17120Sstevel@tonic-gate 		return;
17130Sstevel@tonic-gate 	}
17140Sstevel@tonic-gate 
17150Sstevel@tonic-gate 	(void) mutex_lock(&lib_cache_lock);
17160Sstevel@tonic-gate 	entry->lc_libp = libloc_p->libp;
17170Sstevel@tonic-gate 	entry->lc_next = lib_cache;
17180Sstevel@tonic-gate 	lib_cache = entry;
17190Sstevel@tonic-gate 	hold_lib(entry->lc_libp);	/* prevent stale cache */
17200Sstevel@tonic-gate 	(void) mutex_unlock(&lib_cache_lock);
17210Sstevel@tonic-gate }
17220Sstevel@tonic-gate 
17230Sstevel@tonic-gate static void
17240Sstevel@tonic-gate destroy_cache()
17250Sstevel@tonic-gate {
17260Sstevel@tonic-gate 	lib_cache_t *entry, *next;
17270Sstevel@tonic-gate 	(void) mutex_lock(&lib_cache_lock);
17280Sstevel@tonic-gate 	entry = lib_cache;
17290Sstevel@tonic-gate 	while (entry) {
17300Sstevel@tonic-gate 		next = entry->lc_next;
17310Sstevel@tonic-gate 		rele_lib(entry->lc_libp);
17320Sstevel@tonic-gate 		free(entry->lc_ap_id);
17330Sstevel@tonic-gate 		free(entry->lc_ap_physical);
17340Sstevel@tonic-gate 		free(entry->lc_ap_logical);
17350Sstevel@tonic-gate 		free(entry);
17360Sstevel@tonic-gate 		entry = next;
17370Sstevel@tonic-gate 	}
17380Sstevel@tonic-gate 	(void) mutex_unlock(&lib_cache_lock);
17390Sstevel@tonic-gate }
17400Sstevel@tonic-gate 
17410Sstevel@tonic-gate /*
17420Sstevel@tonic-gate  * find_ap_common - locate a particular attachment point
17430Sstevel@tonic-gate  */
17440Sstevel@tonic-gate static cfga_err_t
17450Sstevel@tonic-gate find_ap_common(
17460Sstevel@tonic-gate 	lib_loc_t *libloc_p,
17470Sstevel@tonic-gate 	const char *physpath,
17480Sstevel@tonic-gate 	int (*fcn)(di_node_t node, di_minor_t minor, void *arg),
17490Sstevel@tonic-gate 	char **errstring)
17500Sstevel@tonic-gate {
1751*6852Scth 	di_node_t rnode, wnode;
17520Sstevel@tonic-gate 	char *cp, *rpath;
17530Sstevel@tonic-gate 	size_t len;
17540Sstevel@tonic-gate 
17550Sstevel@tonic-gate 	if (lookup_cache(libloc_p) == CFGA_OK)
17560Sstevel@tonic-gate 		return (CFGA_OK);
17570Sstevel@tonic-gate 
17580Sstevel@tonic-gate 	if ((rpath = config_calloc_check(1, strlen(physpath) + 1,
17590Sstevel@tonic-gate 	    errstring)) == NULL) {
17600Sstevel@tonic-gate 		return (CFGA_LIB_ERROR);
17610Sstevel@tonic-gate 	}
17620Sstevel@tonic-gate 
17630Sstevel@tonic-gate 	(void) strcpy(rpath, physpath);
17640Sstevel@tonic-gate 
17650Sstevel@tonic-gate 	/* Remove devices prefix (if any) */
17660Sstevel@tonic-gate 	len = strlen(DEVICES_DIR);
17670Sstevel@tonic-gate 	if (strncmp(rpath, DEVICES_DIR SLASH, len + strlen(SLASH)) == 0) {
17680Sstevel@tonic-gate 		(void) memmove(rpath, rpath + len,
17690Sstevel@tonic-gate 		    strlen(rpath + len) + 1);
17700Sstevel@tonic-gate 	}
17710Sstevel@tonic-gate 
17720Sstevel@tonic-gate 	/* Remove dynamic component if any */
17730Sstevel@tonic-gate 	if ((cp = GET_DYN(rpath)) != NULL) {
17740Sstevel@tonic-gate 		*cp = '\0';
17750Sstevel@tonic-gate 	}
17760Sstevel@tonic-gate 
17770Sstevel@tonic-gate 	/* Remove minor name (if any) */
17780Sstevel@tonic-gate 	if ((cp = strrchr(rpath, ':')) != NULL) {
17790Sstevel@tonic-gate 		*cp = '\0';
17800Sstevel@tonic-gate 	}
17810Sstevel@tonic-gate 
17820Sstevel@tonic-gate 	/*
17830Sstevel@tonic-gate 	 * begin walk of device tree
17840Sstevel@tonic-gate 	 */
1785*6852Scth 	rnode = di_init("/", DINFOCACHE);
1786*6852Scth 	if (rnode)
1787*6852Scth 		wnode = di_lookup_node(rnode, rpath);
1788*6852Scth 	else
1789*6852Scth 		wnode = DI_NODE_NIL;
17900Sstevel@tonic-gate 	S_FREE(rpath);
17910Sstevel@tonic-gate 
1792*6852Scth 	if (wnode == DI_NODE_NIL) {
1793*6852Scth 		if (rnode == DI_NODE_NIL) {
17940Sstevel@tonic-gate 			config_err(errno, DI_INIT_FAILED, errstring);
17950Sstevel@tonic-gate 			return (CFGA_LIB_ERROR);
1796*6852Scth 		} else {
1797*6852Scth 			/*
1798*6852Scth 			 * di_lookup_node() may fail because the ap_id
1799*6852Scth 			 * does not exist
1800*6852Scth 			 */
1801*6852Scth 			di_fini(rnode);
1802*6852Scth 			return (CFGA_APID_NOEXIST);
18030Sstevel@tonic-gate 		}
18040Sstevel@tonic-gate 	}
18050Sstevel@tonic-gate 
18060Sstevel@tonic-gate 	libloc_p->libp = NULL;
18070Sstevel@tonic-gate 	libloc_p->status = CFGA_APID_NOEXIST;
18080Sstevel@tonic-gate 
1809*6852Scth 	(void) di_walk_minor(wnode, "ddi_ctl:attachment_point",
18100Sstevel@tonic-gate 	    DI_CHECK_ALIAS|DI_CHECK_INTERNAL_PATH,
18110Sstevel@tonic-gate 	    libloc_p, fcn);
18120Sstevel@tonic-gate 
18130Sstevel@tonic-gate 	di_fini(rnode);
18140Sstevel@tonic-gate 
18150Sstevel@tonic-gate 	if (libloc_p->libp != NULL) {
18160Sstevel@tonic-gate 		update_cache(libloc_p);
18170Sstevel@tonic-gate 		return (CFGA_OK);
18180Sstevel@tonic-gate 	} else {
18190Sstevel@tonic-gate 		return (libloc_p->status);
18200Sstevel@tonic-gate 	}
18210Sstevel@tonic-gate }
18220Sstevel@tonic-gate 
18230Sstevel@tonic-gate /*
18240Sstevel@tonic-gate  * check_ap - called for each attachment point found
18250Sstevel@tonic-gate  *
18260Sstevel@tonic-gate  * This is used in cases where a particular attachment point
18270Sstevel@tonic-gate  * or type of attachment point is specified via a logical name or ap_type.
18280Sstevel@tonic-gate  * Not used for physical names or in the list case with no
18290Sstevel@tonic-gate  * ap's specified.
18300Sstevel@tonic-gate  */
18310Sstevel@tonic-gate 
18320Sstevel@tonic-gate static int
18330Sstevel@tonic-gate check_ap(
18340Sstevel@tonic-gate 	di_node_t node,
18350Sstevel@tonic-gate 	di_minor_t minor,
18360Sstevel@tonic-gate 	void *arg)
18370Sstevel@tonic-gate {
18380Sstevel@tonic-gate 	char *cp = NULL;
18390Sstevel@tonic-gate 	char aptype[MAXPATHLEN];
18400Sstevel@tonic-gate 	char *recep_id = NULL;
18410Sstevel@tonic-gate 	char *node_minor;
18420Sstevel@tonic-gate 	char *drv_name;
18430Sstevel@tonic-gate 	char inst[MAXPATHLEN];
18440Sstevel@tonic-gate 	char inst2[MAXPATHLEN];
18450Sstevel@tonic-gate 	lib_loc_t *libloc_p;
18460Sstevel@tonic-gate 	int comparison_test;
18470Sstevel@tonic-gate 	int instance;
18480Sstevel@tonic-gate 	cfga_ap_types_t type;
18490Sstevel@tonic-gate 
18500Sstevel@tonic-gate 
18510Sstevel@tonic-gate 	libloc_p = (lib_loc_t *)arg;
18520Sstevel@tonic-gate 
18530Sstevel@tonic-gate 	(void) snprintf(aptype, sizeof (aptype), "%s", libloc_p->ap_base);
18540Sstevel@tonic-gate 
18550Sstevel@tonic-gate 	/*
18560Sstevel@tonic-gate 	 * This routime handles only aptypes and driver based logical apids.
18570Sstevel@tonic-gate 	 */
18580Sstevel@tonic-gate 	type = find_arg_type(aptype);
18590Sstevel@tonic-gate 	if (type == LOGICAL_DRV_AP) {
18600Sstevel@tonic-gate 		cp = strchr(aptype, ':');
18610Sstevel@tonic-gate 		*cp = '\0';
18620Sstevel@tonic-gate 		recep_id =  cp+1;
18630Sstevel@tonic-gate 		cp--;
18640Sstevel@tonic-gate 		while (isdigit(*cp) && cp != aptype)
18650Sstevel@tonic-gate 			cp--;
18660Sstevel@tonic-gate 		cp++;
18670Sstevel@tonic-gate 
18680Sstevel@tonic-gate 		(void) snprintf(inst, sizeof (inst), "%s", cp);
18690Sstevel@tonic-gate 
18700Sstevel@tonic-gate 		*cp = '\0';
18710Sstevel@tonic-gate 	} else if (type != AP_TYPE) {
18720Sstevel@tonic-gate 		libloc_p->status = CFGA_APID_NOEXIST;
18730Sstevel@tonic-gate 		return (DI_WALK_CONTINUE);
18740Sstevel@tonic-gate 	}
18750Sstevel@tonic-gate 
18760Sstevel@tonic-gate 	node_minor = di_minor_name(minor);
18770Sstevel@tonic-gate 	drv_name = di_driver_name(node);
18780Sstevel@tonic-gate 	instance = di_instance(node);
18790Sstevel@tonic-gate 
18800Sstevel@tonic-gate 	if (node_minor == NULL || drv_name == NULL || instance == -1) {
18810Sstevel@tonic-gate 		libloc_p->status = CFGA_APID_NOEXIST;
18820Sstevel@tonic-gate 		return (DI_WALK_CONTINUE);
18830Sstevel@tonic-gate 	}
18840Sstevel@tonic-gate 
18850Sstevel@tonic-gate 	(void) sprintf(inst2, "%d", instance);
18860Sstevel@tonic-gate 
18870Sstevel@tonic-gate 	/*
18880Sstevel@tonic-gate 	 * If the base matches driver and instance try and find a lib for it,
18890Sstevel@tonic-gate 	 * then load it. On any failure we continue the walk.
18900Sstevel@tonic-gate 	 *
18910Sstevel@tonic-gate 	 * driver based logical ap_ids are derived from driver name + instance.
18920Sstevel@tonic-gate 	 * Ap_types are just partial driver names.
18930Sstevel@tonic-gate 	 *
18940Sstevel@tonic-gate 	 */
18950Sstevel@tonic-gate 
18960Sstevel@tonic-gate 	comparison_test = 0;
18970Sstevel@tonic-gate 	if (type == AP_TYPE) {
18980Sstevel@tonic-gate 		if (strncmp(aptype, drv_name, strlen(aptype)) == 0) {
18990Sstevel@tonic-gate 			comparison_test = 1;
19000Sstevel@tonic-gate 		}
19010Sstevel@tonic-gate 	} else {
19020Sstevel@tonic-gate 		if (strcmp(aptype, drv_name) == 0 &&
19030Sstevel@tonic-gate 		    strcmp(recep_id, node_minor) == 0 &&
19040Sstevel@tonic-gate 		    strcmp(inst, inst2) == 0) {
19050Sstevel@tonic-gate 			comparison_test = 1;
19060Sstevel@tonic-gate 		}
19070Sstevel@tonic-gate 	}
19080Sstevel@tonic-gate 
19090Sstevel@tonic-gate 	if (comparison_test) {
19100Sstevel@tonic-gate 		/*
19110Sstevel@tonic-gate 		 * save the correct type of error so user does not get confused
19120Sstevel@tonic-gate 		 */
19130Sstevel@tonic-gate 		if (find_lib(node, minor, libloc_p) != CFGA_OK) {
19140Sstevel@tonic-gate 			libloc_p->status = CFGA_NO_LIB;
19150Sstevel@tonic-gate 			return (DI_WALK_CONTINUE);
19160Sstevel@tonic-gate 		}
19170Sstevel@tonic-gate 		if (load_lib(node, minor, libloc_p) != CFGA_OK) {
19180Sstevel@tonic-gate 			libloc_p->status = CFGA_LIB_ERROR;
19190Sstevel@tonic-gate 			return (DI_WALK_CONTINUE);
19200Sstevel@tonic-gate 		}
19210Sstevel@tonic-gate 		libloc_p->status = CFGA_OK;
19220Sstevel@tonic-gate 		return (DI_WALK_TERMINATE);
19230Sstevel@tonic-gate 	} else {
19240Sstevel@tonic-gate 		libloc_p->status = CFGA_APID_NOEXIST;
19250Sstevel@tonic-gate 		return (DI_WALK_CONTINUE);
19260Sstevel@tonic-gate 	}
19270Sstevel@tonic-gate }
19280Sstevel@tonic-gate 
19290Sstevel@tonic-gate 
19300Sstevel@tonic-gate /*
19310Sstevel@tonic-gate  * check_ap_phys - called for each attachment point found
19320Sstevel@tonic-gate  *
19330Sstevel@tonic-gate  * This is used in cases where a particular attachment point
19340Sstevel@tonic-gate  * is specified via a physical name. If the name matches then
19350Sstevel@tonic-gate  * we try and find and load the library for it.
19360Sstevel@tonic-gate  */
19370Sstevel@tonic-gate 
19380Sstevel@tonic-gate static int
19390Sstevel@tonic-gate check_ap_phys(
19400Sstevel@tonic-gate 	di_node_t node,
19410Sstevel@tonic-gate 	di_minor_t minor,
19420Sstevel@tonic-gate 	void *arg)
19430Sstevel@tonic-gate {
19440Sstevel@tonic-gate 	lib_loc_t *libloc_p;
19450Sstevel@tonic-gate 	char phys_name[MAXPATHLEN];
19460Sstevel@tonic-gate 	char *devfs_path;
19470Sstevel@tonic-gate 	char *minor_name;
19480Sstevel@tonic-gate 
19490Sstevel@tonic-gate 	libloc_p = (lib_loc_t *)arg;
19500Sstevel@tonic-gate 	devfs_path = di_devfs_path(node);
19510Sstevel@tonic-gate 	minor_name = di_minor_name(minor);
19520Sstevel@tonic-gate 
19530Sstevel@tonic-gate 	if (devfs_path == NULL || minor_name == NULL) {
19540Sstevel@tonic-gate 		libloc_p->status = CFGA_APID_NOEXIST;
19550Sstevel@tonic-gate 		return (DI_WALK_CONTINUE);
19560Sstevel@tonic-gate 	}
19570Sstevel@tonic-gate 
19580Sstevel@tonic-gate 	(void) snprintf(phys_name, sizeof (phys_name), "%s%s:%s",
19590Sstevel@tonic-gate 	    DEVICES_DIR, devfs_path, minor_name);
19600Sstevel@tonic-gate 
19610Sstevel@tonic-gate 	di_devfs_path_free(devfs_path);
19620Sstevel@tonic-gate 
19630Sstevel@tonic-gate 	if (strcmp(phys_name, libloc_p->ap_base) == 0) {
19640Sstevel@tonic-gate 		if (find_lib(node, minor, libloc_p) != CFGA_OK) {
19650Sstevel@tonic-gate 			libloc_p->status = CFGA_NO_LIB;
19660Sstevel@tonic-gate 			return (DI_WALK_CONTINUE);
19670Sstevel@tonic-gate 		}
19680Sstevel@tonic-gate 		if (load_lib(node, minor, libloc_p) != CFGA_OK) {
19690Sstevel@tonic-gate 			libloc_p->status = CFGA_LIB_ERROR;
19700Sstevel@tonic-gate 			return (DI_WALK_CONTINUE);
19710Sstevel@tonic-gate 		}
19720Sstevel@tonic-gate 		libloc_p->status = CFGA_OK;
19730Sstevel@tonic-gate 		return (DI_WALK_TERMINATE);
19740Sstevel@tonic-gate 	} else {
19750Sstevel@tonic-gate 		libloc_p->status = CFGA_APID_NOEXIST;
19760Sstevel@tonic-gate 		return (DI_WALK_CONTINUE);
19770Sstevel@tonic-gate 	}
19780Sstevel@tonic-gate }
19790Sstevel@tonic-gate 
19800Sstevel@tonic-gate /*
19810Sstevel@tonic-gate  * lib_in_list
19820Sstevel@tonic-gate  *
19830Sstevel@tonic-gate  * See if library, as specified by the full pathname and controller
19840Sstevel@tonic-gate  * instance number is already represented in the plugin library list.
19850Sstevel@tonic-gate  * If the instance number is -1 it is ignored.
19860Sstevel@tonic-gate  */
19870Sstevel@tonic-gate static plugin_lib_t *
19880Sstevel@tonic-gate lib_in_list(char *libpath)
19890Sstevel@tonic-gate {
19900Sstevel@tonic-gate 	plugin_lib_t *libp = NULL;
19910Sstevel@tonic-gate 
19920Sstevel@tonic-gate 	for (libp = plugin_list.next; libp != NULL; libp = libp->next) {
19930Sstevel@tonic-gate 		if (strncmp(libpath, libp->libpath, MAXPATHLEN) == 0) {
19940Sstevel@tonic-gate 			return (libp);
19950Sstevel@tonic-gate 		}
19960Sstevel@tonic-gate 	}
19970Sstevel@tonic-gate 	return (NULL);
19980Sstevel@tonic-gate }
19990Sstevel@tonic-gate 
20000Sstevel@tonic-gate 
20010Sstevel@tonic-gate 
20020Sstevel@tonic-gate 
20030Sstevel@tonic-gate /*
20040Sstevel@tonic-gate  * Coalesce stat and list data into single array
20050Sstevel@tonic-gate  */
20060Sstevel@tonic-gate static cfga_err_t
20070Sstevel@tonic-gate realloc_data_ext(
20080Sstevel@tonic-gate 	cfga_list_data_t **ap_id_list,
20090Sstevel@tonic-gate 	int *nlistp,
20100Sstevel@tonic-gate 	list_stat_t *lstatp)
20110Sstevel@tonic-gate {
20120Sstevel@tonic-gate 	int i, j;
20130Sstevel@tonic-gate 	stat_data_list_t *slp;
20140Sstevel@tonic-gate 	cfga_list_data_t *cldp;
20150Sstevel@tonic-gate 	array_list_t *alp;
20160Sstevel@tonic-gate 	cfga_err_t rc = CFGA_OK;
20170Sstevel@tonic-gate 
20180Sstevel@tonic-gate 
20190Sstevel@tonic-gate 	assert(*lstatp->countp >= 0);
20200Sstevel@tonic-gate 
20210Sstevel@tonic-gate 	if (*lstatp->countp == 0) {
20220Sstevel@tonic-gate 		*ap_id_list = NULL;
20230Sstevel@tonic-gate 		*nlistp = 0;
20240Sstevel@tonic-gate 		return (CFGA_OK);
20250Sstevel@tonic-gate 	}
20260Sstevel@tonic-gate 
20270Sstevel@tonic-gate 	/*
20280Sstevel@tonic-gate 	 * allocate the array
20290Sstevel@tonic-gate 	 */
20300Sstevel@tonic-gate 	if ((cldp = config_calloc_check(*lstatp->countp,
20310Sstevel@tonic-gate 	    sizeof (cfga_list_data_t), lstatp->errstr)) == NULL) {
20320Sstevel@tonic-gate 		rc = CFGA_LIB_ERROR;
20330Sstevel@tonic-gate 		goto out;
20340Sstevel@tonic-gate 	}
20350Sstevel@tonic-gate 
20360Sstevel@tonic-gate 	/*
20370Sstevel@tonic-gate 	 * copy all the stat elements (if any) into the array
20380Sstevel@tonic-gate 	 */
20390Sstevel@tonic-gate 	slp = lstatp->sdl;
20400Sstevel@tonic-gate 	for (i = 0; slp != NULL; i++) {
20410Sstevel@tonic-gate 		if (i >= *lstatp->countp) {
20420Sstevel@tonic-gate 			rc = CFGA_LIB_ERROR;
20430Sstevel@tonic-gate 			goto out;
20440Sstevel@tonic-gate 		}
20450Sstevel@tonic-gate 		stat_to_list(&cldp[i], &slp->stat_data);
20460Sstevel@tonic-gate 		slp = slp->next;
20470Sstevel@tonic-gate 	}
20480Sstevel@tonic-gate 
20490Sstevel@tonic-gate 	/*
20500Sstevel@tonic-gate 	 * copy all the list elements (if any) into the array
20510Sstevel@tonic-gate 	 */
20520Sstevel@tonic-gate 	alp = lstatp->al;
20530Sstevel@tonic-gate 	for (; alp != NULL; ) {
20540Sstevel@tonic-gate 		if (i + alp->nelem > *lstatp->countp) {
20550Sstevel@tonic-gate 			rc = CFGA_LIB_ERROR;
20560Sstevel@tonic-gate 			goto out;
20570Sstevel@tonic-gate 		}
20580Sstevel@tonic-gate 
20590Sstevel@tonic-gate 		for (j = 0; j < alp->nelem; i++, j++) {
20600Sstevel@tonic-gate 			cldp[i] = alp->array[j];
20610Sstevel@tonic-gate 		}
20620Sstevel@tonic-gate 		alp = alp->next;
20630Sstevel@tonic-gate 	}
20640Sstevel@tonic-gate 
20650Sstevel@tonic-gate 	if (i != *lstatp->countp) {
20660Sstevel@tonic-gate 		rc = CFGA_LIB_ERROR;
20670Sstevel@tonic-gate 	} else {
20680Sstevel@tonic-gate 		rc = CFGA_OK;
20690Sstevel@tonic-gate 	}
20700Sstevel@tonic-gate 
20710Sstevel@tonic-gate 	/*FALLTHRU*/
20720Sstevel@tonic-gate 
20730Sstevel@tonic-gate out:
20740Sstevel@tonic-gate 	/* clean up */
20750Sstevel@tonic-gate 	lstat_free(lstatp);
20760Sstevel@tonic-gate 
20770Sstevel@tonic-gate 	if (rc == CFGA_OK) {
20780Sstevel@tonic-gate 		*ap_id_list = cldp;
20790Sstevel@tonic-gate 		*nlistp = *lstatp->countp;
20800Sstevel@tonic-gate 	} else {
20810Sstevel@tonic-gate 		S_FREE(cldp);
20820Sstevel@tonic-gate 		*ap_id_list = NULL;
20830Sstevel@tonic-gate 		*nlistp = 0;
20840Sstevel@tonic-gate 	}
20850Sstevel@tonic-gate 	return (rc);
20860Sstevel@tonic-gate }
20870Sstevel@tonic-gate 
20880Sstevel@tonic-gate /*
20890Sstevel@tonic-gate  * The caller of this routine may supply a buffer through
20900Sstevel@tonic-gate  * ap_id_list for returning data. Otherwise, this routine allocates the
20910Sstevel@tonic-gate  * buffer.
20920Sstevel@tonic-gate  */
20930Sstevel@tonic-gate static cfga_err_t
20940Sstevel@tonic-gate realloc_data(cfga_stat_data_t **ap_id_list, int *nlistp, list_stat_t *lstatp)
20950Sstevel@tonic-gate {
20960Sstevel@tonic-gate 	int i;
20970Sstevel@tonic-gate 	stat_data_list_t *slp;
20980Sstevel@tonic-gate 	cfga_stat_data_t *csdp, *buf;
20990Sstevel@tonic-gate 	cfga_err_t rc;
21000Sstevel@tonic-gate 
21010Sstevel@tonic-gate 
21020Sstevel@tonic-gate 	assert(*lstatp->countp >= 0);
21030Sstevel@tonic-gate 
21040Sstevel@tonic-gate 	if (*lstatp->countp == 0) {
21050Sstevel@tonic-gate 		*nlistp = 0;
21060Sstevel@tonic-gate 		return (CFGA_OK);
21070Sstevel@tonic-gate 	}
21080Sstevel@tonic-gate 
21090Sstevel@tonic-gate 
21100Sstevel@tonic-gate 	/*
21110Sstevel@tonic-gate 	 * allocate the array if caller does not supply one.
21120Sstevel@tonic-gate 	 */
21130Sstevel@tonic-gate 	if (*ap_id_list == NULL) {
21140Sstevel@tonic-gate 		if ((buf = config_calloc_check(*lstatp->countp,
21150Sstevel@tonic-gate 		    sizeof (cfga_stat_data_t), lstatp->errstr)) == NULL) {
21160Sstevel@tonic-gate 			rc = CFGA_LIB_ERROR;
21170Sstevel@tonic-gate 			goto out;
21180Sstevel@tonic-gate 		}
21190Sstevel@tonic-gate 	} else {
21200Sstevel@tonic-gate 		buf = *ap_id_list;
21210Sstevel@tonic-gate 	}
21220Sstevel@tonic-gate 
21230Sstevel@tonic-gate 	/*
21240Sstevel@tonic-gate 	 * copy the stat elements into the array
21250Sstevel@tonic-gate 	 */
21260Sstevel@tonic-gate 	csdp = buf;
21270Sstevel@tonic-gate 	slp = lstatp->sdl;
21280Sstevel@tonic-gate 	for (i = 0; slp != NULL; i++) {
21290Sstevel@tonic-gate 		if (i >= *lstatp->countp) {
21300Sstevel@tonic-gate 			rc = CFGA_LIB_ERROR;
21310Sstevel@tonic-gate 			goto out;
21320Sstevel@tonic-gate 		}
21330Sstevel@tonic-gate 		*csdp++ = slp->stat_data;
21340Sstevel@tonic-gate 		slp = slp->next;
21350Sstevel@tonic-gate 	}
21360Sstevel@tonic-gate 
21370Sstevel@tonic-gate 	rc = CFGA_OK;
21380Sstevel@tonic-gate 
21390Sstevel@tonic-gate out:
21400Sstevel@tonic-gate 	if (rc == CFGA_OK) {
21410Sstevel@tonic-gate 		*nlistp = *lstatp->countp;
21420Sstevel@tonic-gate 		*ap_id_list = buf;
21430Sstevel@tonic-gate 	} else {
21440Sstevel@tonic-gate 		/*
21450Sstevel@tonic-gate 		 * Free buffer only if we allocated it.
21460Sstevel@tonic-gate 		 */
21470Sstevel@tonic-gate 		if (*ap_id_list == NULL) {
21480Sstevel@tonic-gate 			free(buf);
21490Sstevel@tonic-gate 		}
21500Sstevel@tonic-gate 		*nlistp = 0;
21510Sstevel@tonic-gate 	}
21520Sstevel@tonic-gate 
21530Sstevel@tonic-gate 	assert(lstatp->al == NULL);
21540Sstevel@tonic-gate 	lstat_free(lstatp);
21550Sstevel@tonic-gate 
21560Sstevel@tonic-gate 	return (rc);
21570Sstevel@tonic-gate }
21580Sstevel@tonic-gate 
21590Sstevel@tonic-gate 
21600Sstevel@tonic-gate /*
21610Sstevel@tonic-gate  * list_common - walk the device tree and stat all attachment points.
21620Sstevel@tonic-gate  */
21630Sstevel@tonic-gate static cfga_err_t
21640Sstevel@tonic-gate list_common(list_stat_t *lstatp, const char *class)
21650Sstevel@tonic-gate {
21660Sstevel@tonic-gate 	di_node_t rnode;
21670Sstevel@tonic-gate 	char nodetype[MAXPATHLEN];
21680Sstevel@tonic-gate 	const char *l_class, *l_sep;
21690Sstevel@tonic-gate 
21700Sstevel@tonic-gate 
21710Sstevel@tonic-gate 	/*
21720Sstevel@tonic-gate 	 * begin walk of device tree
21730Sstevel@tonic-gate 	 */
2174*6852Scth 	if ((rnode = di_init("/", DINFOCACHE)) == DI_NODE_NIL) {
21750Sstevel@tonic-gate 		config_err(errno, DI_INIT_FAILED, lstatp->errstr);
21760Sstevel@tonic-gate 		return (CFGA_LIB_ERROR);
21770Sstevel@tonic-gate 	}
21780Sstevel@tonic-gate 
21790Sstevel@tonic-gate 	/*
21800Sstevel@tonic-gate 	 * May walk a subset of all attachment points in the device tree if
21810Sstevel@tonic-gate 	 * a class is specified
21820Sstevel@tonic-gate 	 */
21830Sstevel@tonic-gate 	if (class != NULL) {
21840Sstevel@tonic-gate 		l_sep = ":";
21850Sstevel@tonic-gate 		l_class = class;
21860Sstevel@tonic-gate 	} else {
21870Sstevel@tonic-gate 		l_sep = l_class = "";
21880Sstevel@tonic-gate 	}
21890Sstevel@tonic-gate 
21900Sstevel@tonic-gate 	(void) snprintf(nodetype, sizeof (nodetype), "%s%s%s",
21910Sstevel@tonic-gate 	    DDI_NT_ATTACHMENT_POINT, l_sep, l_class);
21920Sstevel@tonic-gate 
21930Sstevel@tonic-gate 	(void) di_walk_minor(rnode, nodetype,
21940Sstevel@tonic-gate 	    DI_CHECK_ALIAS|DI_CHECK_INTERNAL_PATH, lstatp, do_list_common);
21950Sstevel@tonic-gate 	di_fini(rnode);
21960Sstevel@tonic-gate 
21970Sstevel@tonic-gate 	return (CFGA_OK);
21980Sstevel@tonic-gate }
21990Sstevel@tonic-gate 
22000Sstevel@tonic-gate static void
22010Sstevel@tonic-gate config_err(int errnum, int err_type, char **errstring)
22020Sstevel@tonic-gate {
22030Sstevel@tonic-gate 	char *p = NULL, *q = NULL;
22040Sstevel@tonic-gate 	char *syserr = NULL;
22050Sstevel@tonic-gate 	char syserr_num[20];
22060Sstevel@tonic-gate 	int len = 0;
22070Sstevel@tonic-gate 
22080Sstevel@tonic-gate 	/*
22090Sstevel@tonic-gate 	 * If errstring is null it means user in not interested in getting
22100Sstevel@tonic-gate 	 * error status. So we don't do all the work
22110Sstevel@tonic-gate 	 */
22120Sstevel@tonic-gate 	if (errstring == NULL) {
22130Sstevel@tonic-gate 		return;
22140Sstevel@tonic-gate 	}
22150Sstevel@tonic-gate 
22160Sstevel@tonic-gate 	if (errnum != 0) {
22170Sstevel@tonic-gate 		syserr = strerror(errnum);
22180Sstevel@tonic-gate 		if (syserr == NULL) {
22190Sstevel@tonic-gate 			(void) sprintf(syserr_num, "errno=%d", errnum);
22200Sstevel@tonic-gate 			syserr = syserr_num;
22210Sstevel@tonic-gate 		}
22220Sstevel@tonic-gate 	} else
22230Sstevel@tonic-gate 		syserr = NULL;
22240Sstevel@tonic-gate 
22250Sstevel@tonic-gate 	q = dgettext(TEXT_DOMAIN, err_strings[err_type]);
22260Sstevel@tonic-gate 
22270Sstevel@tonic-gate 	len = strlen(q);
22280Sstevel@tonic-gate 	if (syserr != NULL) {
22290Sstevel@tonic-gate 		len += strlen(err_sep) + strlen(syserr);
22300Sstevel@tonic-gate 	}
22310Sstevel@tonic-gate 
22320Sstevel@tonic-gate 	p = malloc(len + 1);
22330Sstevel@tonic-gate 	if (p == NULL) {
22340Sstevel@tonic-gate 		*errstring = NULL;
22350Sstevel@tonic-gate 		return;
22360Sstevel@tonic-gate 	}
22370Sstevel@tonic-gate 
22380Sstevel@tonic-gate 	(void) strcpy(p, q);
22390Sstevel@tonic-gate 	if (syserr != NULL) {
22400Sstevel@tonic-gate 		(void) strcat(p, err_sep);
22410Sstevel@tonic-gate 		(void) strcat(p, syserr);
22420Sstevel@tonic-gate 	}
22430Sstevel@tonic-gate 
22440Sstevel@tonic-gate 	*errstring  = p;
22450Sstevel@tonic-gate }
22460Sstevel@tonic-gate 
22470Sstevel@tonic-gate /*
22480Sstevel@tonic-gate  * do_list_common - Routine to list attachment point as part of
22490Sstevel@tonic-gate  * a config_list opertion. Used by both v1 and v2 interfaces.
22500Sstevel@tonic-gate  * This is somewhat similar to config_get_lib() and its helper routines
22510Sstevel@tonic-gate  * except that the ap_ids are always physical and don't have dynamic
22520Sstevel@tonic-gate  * components.
22530Sstevel@tonic-gate  */
22540Sstevel@tonic-gate static int
22550Sstevel@tonic-gate do_list_common(
22560Sstevel@tonic-gate 	di_node_t node,
22570Sstevel@tonic-gate 	di_minor_t minor,
22580Sstevel@tonic-gate 	void *arg)
22590Sstevel@tonic-gate {
22600Sstevel@tonic-gate 	lib_loc_t lib_loc;
22610Sstevel@tonic-gate 	plugin_lib_t *libp;
22620Sstevel@tonic-gate 	list_stat_t *lstatp = NULL;
22630Sstevel@tonic-gate 	cfga_err_t ret = CFGA_ERROR;
22640Sstevel@tonic-gate 
22650Sstevel@tonic-gate 
22660Sstevel@tonic-gate 	lstatp = (list_stat_t *)arg;
22670Sstevel@tonic-gate 
22680Sstevel@tonic-gate 	lib_loc.libp = NULL;
22690Sstevel@tonic-gate 	/*
22700Sstevel@tonic-gate 	 * try and find a lib for this node
22710Sstevel@tonic-gate 	 */
22720Sstevel@tonic-gate 	if (find_lib(node, minor, &lib_loc) != CFGA_OK) {
22730Sstevel@tonic-gate 		return (DI_WALK_CONTINUE);
22740Sstevel@tonic-gate 	}
22750Sstevel@tonic-gate 
22760Sstevel@tonic-gate 	/*
22770Sstevel@tonic-gate 	 * Load all plugins. We will check compatibility later in this
22780Sstevel@tonic-gate 	 * routine.
22790Sstevel@tonic-gate 	 */
22800Sstevel@tonic-gate 	lib_loc.vers_req.v_min = CFGA_HSL_V1;
22810Sstevel@tonic-gate 	lib_loc.vers_req.v_max = CFGA_HSL_VERS;
22820Sstevel@tonic-gate 
22830Sstevel@tonic-gate 	ret = load_lib(node, minor, &lib_loc);
22840Sstevel@tonic-gate 	if (ret != CFGA_OK) {
22850Sstevel@tonic-gate 		return (DI_WALK_CONTINUE);
22860Sstevel@tonic-gate 	}
22870Sstevel@tonic-gate 
22880Sstevel@tonic-gate 	libp = lib_loc.libp;
22890Sstevel@tonic-gate 	assert(libp != NULL);
22900Sstevel@tonic-gate 
22910Sstevel@tonic-gate 	/*
22920Sstevel@tonic-gate 	 * Note: For list type routines (list all attachment points in
22930Sstevel@tonic-gate 	 * device tree) we don't pass errstring to the plugin, nor do we
22940Sstevel@tonic-gate 	 * stop the walk if an error occurs in the plugin.
22950Sstevel@tonic-gate 	 */
22960Sstevel@tonic-gate 	if (compat_plugin(&lstatp->use_vers, libp->plugin_vers)) {
22970Sstevel@tonic-gate 		(void) libp->vers_ops->stat_plugin(lstatp, &lib_loc, NULL);
22980Sstevel@tonic-gate 	}
22990Sstevel@tonic-gate 	rele_lib(libp);
23000Sstevel@tonic-gate 
23010Sstevel@tonic-gate 	return (DI_WALK_CONTINUE);
23020Sstevel@tonic-gate }
23030Sstevel@tonic-gate 
23040Sstevel@tonic-gate /*
23050Sstevel@tonic-gate  * stat_common - stat a user specified set of attachment points.
23060Sstevel@tonic-gate  */
23070Sstevel@tonic-gate static cfga_err_t
23080Sstevel@tonic-gate stat_common(
23090Sstevel@tonic-gate 	int num_ap_ids,
23100Sstevel@tonic-gate 	char *const *ap_ids,
23110Sstevel@tonic-gate 	const char *class,
23120Sstevel@tonic-gate 	list_stat_t *lstatp)
23130Sstevel@tonic-gate {
23140Sstevel@tonic-gate 	int i;
23150Sstevel@tonic-gate 	lib_loc_t libloc;
23160Sstevel@tonic-gate 	plugin_lib_t *libp;
23170Sstevel@tonic-gate 	cfga_err_t rc = CFGA_OK;
23180Sstevel@tonic-gate 
23190Sstevel@tonic-gate 
23200Sstevel@tonic-gate 	/*
23210Sstevel@tonic-gate 	 * operate on each ap_id
23220Sstevel@tonic-gate 	 */
23230Sstevel@tonic-gate 	for (i = 0; i < num_ap_ids; i++) {
23240Sstevel@tonic-gate 		libloc.libp = NULL;
23250Sstevel@tonic-gate 		if ((rc = config_get_lib(ap_ids[i], &libloc,
23260Sstevel@tonic-gate 		    lstatp->errstr)) != CFGA_OK) {
23270Sstevel@tonic-gate 			break;
23280Sstevel@tonic-gate 		}
23290Sstevel@tonic-gate 		assert(libloc.libp != NULL);
23300Sstevel@tonic-gate 		libp = libloc.libp;
23310Sstevel@tonic-gate 
23320Sstevel@tonic-gate 		/*
23330Sstevel@tonic-gate 		 * do pre-filtering if requested
23340Sstevel@tonic-gate 		 */
23350Sstevel@tonic-gate 		if (class != NULL && strcmp(libloc.ap_class, class)) {
23360Sstevel@tonic-gate 			rele_lib(libp);
23370Sstevel@tonic-gate 			continue;
23380Sstevel@tonic-gate 		}
23390Sstevel@tonic-gate 
23400Sstevel@tonic-gate 		/*
23410Sstevel@tonic-gate 		 * Unlike list type routines, while stat'ing specific
23420Sstevel@tonic-gate 		 * attachment points we pass errstring to the plugins
23430Sstevel@tonic-gate 		 * and halt if an error occurs in the plugin.
23440Sstevel@tonic-gate 		 */
23450Sstevel@tonic-gate 		rc = libp->vers_ops->stat_plugin(lstatp, &libloc,
23460Sstevel@tonic-gate 		    lstatp->errstr);
23470Sstevel@tonic-gate 		rele_lib(libp);
23480Sstevel@tonic-gate 		if (rc != CFGA_OK) {
23490Sstevel@tonic-gate 			break;
23500Sstevel@tonic-gate 		}
23510Sstevel@tonic-gate 	}
23520Sstevel@tonic-gate 
23530Sstevel@tonic-gate 	if (rc != CFGA_OK) {
23540Sstevel@tonic-gate 		lstat_free(lstatp);
23550Sstevel@tonic-gate 	}
23560Sstevel@tonic-gate 	return (rc);
23570Sstevel@tonic-gate }
23580Sstevel@tonic-gate 
23590Sstevel@tonic-gate /*ARGSUSED*/
23600Sstevel@tonic-gate static cfga_err_t
23610Sstevel@tonic-gate null_stat_plugin(list_stat_t *lstatp, lib_loc_t *libloc_p, char **errstring)
23620Sstevel@tonic-gate {
23630Sstevel@tonic-gate 	return (CFGA_OK);
23640Sstevel@tonic-gate }
23650Sstevel@tonic-gate 
23660Sstevel@tonic-gate /*
23670Sstevel@tonic-gate  * Pass errstring as a separate argument. Some higher level routines need
23680Sstevel@tonic-gate  * it to be NULL.
23690Sstevel@tonic-gate  */
23700Sstevel@tonic-gate static cfga_err_t
23710Sstevel@tonic-gate stat_plugin_v1(list_stat_t *lstatp, lib_loc_t *libloc_p, char **errstring)
23720Sstevel@tonic-gate {
23730Sstevel@tonic-gate 	stat_data_list_t *slp, *slp2 = NULL;
23740Sstevel@tonic-gate 	cfga_err_t rc;
23750Sstevel@tonic-gate 
23760Sstevel@tonic-gate 	/*
23770Sstevel@tonic-gate 	 * allocate stat data buffer and list element
23780Sstevel@tonic-gate 	 */
23790Sstevel@tonic-gate 	if ((slp = config_calloc_check(1, sizeof (stat_data_list_t),
23800Sstevel@tonic-gate 	    errstring)) == NULL) {
23810Sstevel@tonic-gate 		return (CFGA_LIB_ERROR);
23820Sstevel@tonic-gate 	}
23830Sstevel@tonic-gate 
23840Sstevel@tonic-gate 	/*
23850Sstevel@tonic-gate 	 * Do the stat
23860Sstevel@tonic-gate 	 */
23870Sstevel@tonic-gate 	errno = 0;
23880Sstevel@tonic-gate 	if ((rc = (*(libloc_p->libp->cfga_stat_p))(libloc_p->ap_physical,
23890Sstevel@tonic-gate 	    &slp->stat_data, lstatp->opts, errstring)) != CFGA_OK) {
23900Sstevel@tonic-gate 		S_FREE(slp);
23910Sstevel@tonic-gate 		return (rc);
23920Sstevel@tonic-gate 	}
23930Sstevel@tonic-gate 	slp->next = NULL;
23940Sstevel@tonic-gate 
23950Sstevel@tonic-gate 	/*
23960Sstevel@tonic-gate 	 * Set up the logical and physical id's.
23970Sstevel@tonic-gate 	 * For v1 interfaces, the generic library (libcfgadm) creates the
23980Sstevel@tonic-gate 	 * ap_ids. mklog() is assumed to have been called in
23990Sstevel@tonic-gate 	 * the caller of this routine.
24000Sstevel@tonic-gate 	 */
24010Sstevel@tonic-gate 	(void) snprintf(slp->stat_data.ap_log_id, CFGA_AP_LOG_ID_LEN, "%s",
24020Sstevel@tonic-gate 	    libloc_p->ap_logical);
24030Sstevel@tonic-gate 
24040Sstevel@tonic-gate 	(void) snprintf(slp->stat_data.ap_phys_id, CFGA_AP_PHYS_ID_LEN, "%s",
24050Sstevel@tonic-gate 	    libloc_p->ap_physical);
24060Sstevel@tonic-gate 
24070Sstevel@tonic-gate 	/*
24080Sstevel@tonic-gate 	 * link it in
24090Sstevel@tonic-gate 	 */
24100Sstevel@tonic-gate 	if ((slp2 = lstatp->sdl) == NULL) {
24110Sstevel@tonic-gate 		lstatp->sdl = slp;
24120Sstevel@tonic-gate 	} else {
24130Sstevel@tonic-gate 		while (slp2->next != NULL)
24140Sstevel@tonic-gate 			slp2 = slp2->next;
24150Sstevel@tonic-gate 		slp2->next = slp;
24160Sstevel@tonic-gate 	}
24170Sstevel@tonic-gate 
24180Sstevel@tonic-gate 	/* keep count */
24190Sstevel@tonic-gate 	(*lstatp->countp)++;
24200Sstevel@tonic-gate 
24210Sstevel@tonic-gate 	return (CFGA_OK);
24220Sstevel@tonic-gate }
24230Sstevel@tonic-gate 
24240Sstevel@tonic-gate static cfga_err_t
24250Sstevel@tonic-gate stat_plugin_v2(list_stat_t *lstatp, lib_loc_t *libloc_p, char **errstring)
24260Sstevel@tonic-gate {
24270Sstevel@tonic-gate 	int i;
24280Sstevel@tonic-gate 	array_list_t *alp, *alp2 = NULL;
24290Sstevel@tonic-gate 	cfga_err_t rc;
24300Sstevel@tonic-gate 	char *class;
24310Sstevel@tonic-gate 
24320Sstevel@tonic-gate 	/*
24330Sstevel@tonic-gate 	 * allocate array list
24340Sstevel@tonic-gate 	 */
24350Sstevel@tonic-gate 	if ((alp = config_calloc_check(1, sizeof (array_list_t),
24360Sstevel@tonic-gate 	    errstring)) == NULL) {
24370Sstevel@tonic-gate 		return (CFGA_LIB_ERROR);
24380Sstevel@tonic-gate 	}
24390Sstevel@tonic-gate 
24400Sstevel@tonic-gate 	alp->array = NULL;
24410Sstevel@tonic-gate 	alp->nelem = 0;
24420Sstevel@tonic-gate 
24430Sstevel@tonic-gate 	/*
24440Sstevel@tonic-gate 	 * The listopts argument is currently unused. Use NULL
24450Sstevel@tonic-gate 	 */
24460Sstevel@tonic-gate 	errno = 0;
24470Sstevel@tonic-gate 	if ((rc = (*(libloc_p->libp->cfga_list_ext_p))(
24480Sstevel@tonic-gate 	    libloc_p->ap_physical, &alp->array, &alp->nelem, lstatp->opts, NULL,
24490Sstevel@tonic-gate 	    errstring, lstatp->flags)) != CFGA_OK || alp->nelem <= 0) {
24500Sstevel@tonic-gate 		S_FREE(alp);
24510Sstevel@tonic-gate 		return (rc);
24520Sstevel@tonic-gate 	}
24530Sstevel@tonic-gate 	alp->next = NULL;
24540Sstevel@tonic-gate 
24550Sstevel@tonic-gate 	/*
24560Sstevel@tonic-gate 	 * Set up the logical and physical id's if necessary.
24570Sstevel@tonic-gate 	 * For v2 interfaces, the generic library (libcfgadm) creates the
24580Sstevel@tonic-gate 	 * ap_ids only if there are no dynamic attachment points and the
24590Sstevel@tonic-gate 	 * plug-in does not create the name itself.  mklog() is
24600Sstevel@tonic-gate 	 * assumed to have been called in the caller of this routine.
24610Sstevel@tonic-gate 	 */
24620Sstevel@tonic-gate 	if (alp->nelem == 1) {
24630Sstevel@tonic-gate 		char cphys, clog;
24640Sstevel@tonic-gate 
24650Sstevel@tonic-gate 		clog = (alp->array[0]).ap_log_id[0];
24660Sstevel@tonic-gate 		cphys = (alp->array[0]).ap_phys_id[0];
24670Sstevel@tonic-gate 
24680Sstevel@tonic-gate 		if (clog == '\0') {
24690Sstevel@tonic-gate 			(void) snprintf((alp->array[0]).ap_log_id,
24700Sstevel@tonic-gate 			    sizeof ((alp->array[0]).ap_log_id), "%s",
24710Sstevel@tonic-gate 			    libloc_p->ap_logical);
24720Sstevel@tonic-gate 		}
24730Sstevel@tonic-gate 
24740Sstevel@tonic-gate 		if (cphys == '\0') {
24750Sstevel@tonic-gate 			(void) snprintf((alp->array[0]).ap_phys_id,
24760Sstevel@tonic-gate 			    sizeof ((alp->array[0]).ap_phys_id), "%s",
24770Sstevel@tonic-gate 			    libloc_p->ap_physical);
24780Sstevel@tonic-gate 		}
24790Sstevel@tonic-gate 	}
24800Sstevel@tonic-gate 
24810Sstevel@tonic-gate 	if (libloc_p->ap_class[0] == '\0') {
24820Sstevel@tonic-gate 		class = CFGA_NO_CLASS;
24830Sstevel@tonic-gate 	} else {
24840Sstevel@tonic-gate 		class = libloc_p->ap_class;
24850Sstevel@tonic-gate 	}
24860Sstevel@tonic-gate 
24870Sstevel@tonic-gate 	/* Fill in the class information for all list elements */
24880Sstevel@tonic-gate 	for (i = 0; i < alp->nelem; i++) {
24890Sstevel@tonic-gate 		(void) snprintf((alp->array[i]).ap_class,
24900Sstevel@tonic-gate 		    sizeof ((alp->array[i]).ap_class), "%s", class);
24910Sstevel@tonic-gate 	}
24920Sstevel@tonic-gate 
24930Sstevel@tonic-gate 	/*
24940Sstevel@tonic-gate 	 * link it in
24950Sstevel@tonic-gate 	 */
24960Sstevel@tonic-gate 	if ((alp2 = lstatp->al) == NULL) {
24970Sstevel@tonic-gate 		lstatp->al = alp;
24980Sstevel@tonic-gate 	} else {
24990Sstevel@tonic-gate 		while (alp2->next != NULL)
25000Sstevel@tonic-gate 			alp2 = alp2->next;
25010Sstevel@tonic-gate 		alp2->next = alp;
25020Sstevel@tonic-gate 	}
25030Sstevel@tonic-gate 
25040Sstevel@tonic-gate 	/* keep count */
25050Sstevel@tonic-gate 	(*lstatp->countp) += alp->nelem;
25060Sstevel@tonic-gate 
25070Sstevel@tonic-gate 	return (CFGA_OK);
25080Sstevel@tonic-gate }
25090Sstevel@tonic-gate 
25100Sstevel@tonic-gate /*
25110Sstevel@tonic-gate  * Check if a plugin version is within requested limits.
25120Sstevel@tonic-gate  */
25130Sstevel@tonic-gate static int
25140Sstevel@tonic-gate compat_plugin(vers_req_t *reqp, int plugin_vers)
25150Sstevel@tonic-gate {
25160Sstevel@tonic-gate 
25170Sstevel@tonic-gate 	if (!VALID_HSL_VERS(reqp->v_min) || !VALID_HSL_VERS(reqp->v_max) ||
25180Sstevel@tonic-gate 	    !VALID_HSL_VERS(plugin_vers)) {
25190Sstevel@tonic-gate 		return (0);
25200Sstevel@tonic-gate 	}
25210Sstevel@tonic-gate 
25220Sstevel@tonic-gate 	if (plugin_vers < reqp->v_min || plugin_vers > reqp->v_max) {
25230Sstevel@tonic-gate 		return (0);
25240Sstevel@tonic-gate 	}
25250Sstevel@tonic-gate 
25260Sstevel@tonic-gate 
25270Sstevel@tonic-gate 	return (1);
25280Sstevel@tonic-gate }
25290Sstevel@tonic-gate 
25300Sstevel@tonic-gate /*
25310Sstevel@tonic-gate  * find_arg_type - determine if an argument is an ap_id or an ap_type.
25320Sstevel@tonic-gate  * Adapted from cfgadm.c
25330Sstevel@tonic-gate  */
25340Sstevel@tonic-gate static cfga_ap_types_t
25350Sstevel@tonic-gate find_arg_type(const char *ap_id)
25360Sstevel@tonic-gate {
25370Sstevel@tonic-gate 	struct stat sbuf;
25380Sstevel@tonic-gate 	cfga_ap_types_t type = UNKNOWN_AP;
25390Sstevel@tonic-gate 	char *mkr = NULL;
25400Sstevel@tonic-gate 	size_t len;
25410Sstevel@tonic-gate 	int size_ap = 0, size_mkr = 0, digit = 0, i = 0;
25420Sstevel@tonic-gate 	char *cp, path[MAXPATHLEN], ap_base[MAXPATHLEN];
25430Sstevel@tonic-gate 
25440Sstevel@tonic-gate 
25450Sstevel@tonic-gate 	/*
25460Sstevel@tonic-gate 	 * sanity checks
25470Sstevel@tonic-gate 	 */
25480Sstevel@tonic-gate 	if (ap_id == NULL || *ap_id == '\0') {
25490Sstevel@tonic-gate 
25500Sstevel@tonic-gate 		return (UNKNOWN_AP);
25510Sstevel@tonic-gate 	}
25520Sstevel@tonic-gate 
25530Sstevel@tonic-gate 	/*
25540Sstevel@tonic-gate 	 * Extract the base component
25550Sstevel@tonic-gate 	 */
25560Sstevel@tonic-gate 	if ((cp = GET_DYN(ap_id)) != NULL) {
25570Sstevel@tonic-gate 		len = cp - ap_id;
25580Sstevel@tonic-gate 	} else {
25590Sstevel@tonic-gate 		len = strlen(ap_id);
25600Sstevel@tonic-gate 	}
25610Sstevel@tonic-gate 
25620Sstevel@tonic-gate 	if (len >= sizeof (ap_base)) {
25630Sstevel@tonic-gate 		return (UNKNOWN_AP);
25640Sstevel@tonic-gate 	}
25650Sstevel@tonic-gate 
25660Sstevel@tonic-gate 	/* Copy only the first "len" chars */
25670Sstevel@tonic-gate 	(void) strncpy(ap_base, ap_id, len);
25680Sstevel@tonic-gate 	ap_base[len] = '\0';
25690Sstevel@tonic-gate 
25700Sstevel@tonic-gate 	/*
25710Sstevel@tonic-gate 	 * If it starts with a slash and is stat-able its a physical.
25720Sstevel@tonic-gate 	 */
25730Sstevel@tonic-gate 	if (*ap_base == '/' && stat(ap_base, &sbuf) == 0) {
25740Sstevel@tonic-gate 		return (PHYSICAL_AP);
25750Sstevel@tonic-gate 	}
25760Sstevel@tonic-gate 
25770Sstevel@tonic-gate 	/*
25780Sstevel@tonic-gate 	 * Is this a symlink in CFGA_DEV_DIR ?
25790Sstevel@tonic-gate 	 */
25800Sstevel@tonic-gate 	(void) snprintf(path, sizeof (path), "%s%s",
25810Sstevel@tonic-gate 	    CFGA_DEV_DIR SLASH, ap_base);
25820Sstevel@tonic-gate 
25830Sstevel@tonic-gate 	if (lstat(path, &sbuf) == 0 && S_ISLNK(sbuf.st_mode) &&
25840Sstevel@tonic-gate 	    stat(path, &sbuf) == 0) {
25850Sstevel@tonic-gate 		return (LOGICAL_LINK_AP);
25860Sstevel@tonic-gate 	}
25870Sstevel@tonic-gate 
25880Sstevel@tonic-gate 	/*
25890Sstevel@tonic-gate 	 * Check for ":" which is always present in an ap_id
25900Sstevel@tonic-gate 	 * but not in an ap_type.
25910Sstevel@tonic-gate 	 * we need to check that the characters right before the : are digits
25920Sstevel@tonic-gate 	 * since an ap_id is of the form <name><instance>:<specific ap name>
25930Sstevel@tonic-gate 	 */
25940Sstevel@tonic-gate 	if ((mkr = strchr(ap_base, ':')) == NULL)  {
25950Sstevel@tonic-gate 		type = AP_TYPE;
25960Sstevel@tonic-gate 	} else {
25970Sstevel@tonic-gate 		size_ap = strlen(ap_base);
25980Sstevel@tonic-gate 		size_mkr = strlen(mkr);
25990Sstevel@tonic-gate 		mkr = ap_base;
26000Sstevel@tonic-gate 
26010Sstevel@tonic-gate 		digit = 0;
26020Sstevel@tonic-gate 		for (i = size_ap - size_mkr - 1;  i > 0; i--) {
26030Sstevel@tonic-gate 			if ((int)isdigit(mkr[i])) {
26040Sstevel@tonic-gate 				digit++;
26050Sstevel@tonic-gate 				break;
26060Sstevel@tonic-gate 			}
26070Sstevel@tonic-gate 		}
26080Sstevel@tonic-gate 		if (digit == 0) {
26090Sstevel@tonic-gate 			type = AP_TYPE;
26100Sstevel@tonic-gate 		} else {
26110Sstevel@tonic-gate 			type = LOGICAL_DRV_AP;
26120Sstevel@tonic-gate 		}
26130Sstevel@tonic-gate 	}
26140Sstevel@tonic-gate 
26150Sstevel@tonic-gate 	return (type);
26160Sstevel@tonic-gate }
26170Sstevel@tonic-gate 
26180Sstevel@tonic-gate /*ARGSUSED*/
26190Sstevel@tonic-gate static cfga_err_t
26200Sstevel@tonic-gate null_get_cond(lib_loc_t *liblocp, cfga_cond_t *condp, char **errstring)
26210Sstevel@tonic-gate {
26220Sstevel@tonic-gate 	return (CFGA_OK);
26230Sstevel@tonic-gate }
26240Sstevel@tonic-gate 
26250Sstevel@tonic-gate static cfga_err_t
26260Sstevel@tonic-gate get_cond_v1(lib_loc_t *liblocp, cfga_cond_t *condp, char **errstring)
26270Sstevel@tonic-gate {
26280Sstevel@tonic-gate 	plugin_lib_t *libp;
26290Sstevel@tonic-gate 	cfga_stat_data_t sdbuf;
26300Sstevel@tonic-gate 	cfga_err_t rc;
26310Sstevel@tonic-gate 
26320Sstevel@tonic-gate 
26330Sstevel@tonic-gate 	libp = liblocp->libp;
26340Sstevel@tonic-gate 	if (libp->plugin_vers != CFGA_HSL_V1) {
26350Sstevel@tonic-gate 		return (CFGA_LIB_ERROR);
26360Sstevel@tonic-gate 	}
26370Sstevel@tonic-gate 
26380Sstevel@tonic-gate 	errno = 0;
26390Sstevel@tonic-gate 	if ((rc = (*liblocp->libp->cfga_stat_p)(
26400Sstevel@tonic-gate 	    liblocp->ap_physical, &sdbuf, NULL, errstring))
26410Sstevel@tonic-gate 	    == CFGA_OK) {
26420Sstevel@tonic-gate 		*condp = sdbuf.ap_cond;
26430Sstevel@tonic-gate 	} else {
26440Sstevel@tonic-gate 		*condp = CFGA_COND_UNKNOWN;
26450Sstevel@tonic-gate 	}
26460Sstevel@tonic-gate 
26470Sstevel@tonic-gate 	return (rc);
26480Sstevel@tonic-gate }
26490Sstevel@tonic-gate 
26500Sstevel@tonic-gate static cfga_err_t
26510Sstevel@tonic-gate get_cond_v2(lib_loc_t *liblocp, cfga_cond_t *condp, char **errstring)
26520Sstevel@tonic-gate {
26530Sstevel@tonic-gate 	int nelem;
26540Sstevel@tonic-gate 	plugin_lib_t *libp;
26550Sstevel@tonic-gate 	cfga_list_data_t *ldbufp;
26560Sstevel@tonic-gate 	cfga_err_t rc;
26570Sstevel@tonic-gate 
26580Sstevel@tonic-gate 
26590Sstevel@tonic-gate 	libp = liblocp->libp;
26600Sstevel@tonic-gate 	if (libp->plugin_vers != CFGA_HSL_V2) {
26610Sstevel@tonic-gate 		return (CFGA_LIB_ERROR);
26620Sstevel@tonic-gate 	}
26630Sstevel@tonic-gate 
26640Sstevel@tonic-gate 	errno = 0;
26650Sstevel@tonic-gate 	nelem = 0;
26660Sstevel@tonic-gate 	ldbufp = NULL;
26670Sstevel@tonic-gate 	if ((rc = (*liblocp->libp->cfga_list_ext_p)(
26680Sstevel@tonic-gate 	    liblocp->ap_physical, &ldbufp, &nelem, NULL, NULL,
26690Sstevel@tonic-gate 	    errstring, 0)) == CFGA_OK) {
26700Sstevel@tonic-gate 		assert(nelem == 1 && ldbufp != NULL);
26710Sstevel@tonic-gate 
26720Sstevel@tonic-gate 		*condp = ldbufp->ap_cond;
26730Sstevel@tonic-gate 		S_FREE(ldbufp);
26740Sstevel@tonic-gate 	} else {
26750Sstevel@tonic-gate 		*condp = CFGA_COND_UNKNOWN;
26760Sstevel@tonic-gate 	}
26770Sstevel@tonic-gate 
26780Sstevel@tonic-gate 	return (rc);
26790Sstevel@tonic-gate }
26800Sstevel@tonic-gate 
26810Sstevel@tonic-gate /* mask represents the flags accepted */
26820Sstevel@tonic-gate static cfga_err_t
26830Sstevel@tonic-gate check_flags(cfga_flags_t flags, cfga_flags_t mask, char **errstring)
26840Sstevel@tonic-gate {
26850Sstevel@tonic-gate 	if ((flags & ~mask) != 0) {
26860Sstevel@tonic-gate 		config_err(0, INVALID_ARGS, errstring);
26870Sstevel@tonic-gate 		return (CFGA_ERROR);
26880Sstevel@tonic-gate 	} else {
26890Sstevel@tonic-gate 		return (CFGA_OK);
26900Sstevel@tonic-gate 	}
26910Sstevel@tonic-gate }
26920Sstevel@tonic-gate 
26930Sstevel@tonic-gate static cfga_err_t
26940Sstevel@tonic-gate check_apids(int num_ap_ids, char *const *ap_ids, char **errstring)
26950Sstevel@tonic-gate {
26960Sstevel@tonic-gate 	if (num_ap_ids <= 0 || ap_ids == NULL) {
26970Sstevel@tonic-gate 		config_err(0, INVALID_ARGS, errstring);
26980Sstevel@tonic-gate 		return (CFGA_ERROR);
26990Sstevel@tonic-gate 	} else {
27000Sstevel@tonic-gate 		return (CFGA_OK);
27010Sstevel@tonic-gate 	}
27020Sstevel@tonic-gate }
27030Sstevel@tonic-gate 
27040Sstevel@tonic-gate /*
27050Sstevel@tonic-gate  * Returns the class or the empty string if attacment point has
27060Sstevel@tonic-gate  * no class.
27070Sstevel@tonic-gate  */
27080Sstevel@tonic-gate static char *
27090Sstevel@tonic-gate get_class(di_minor_t minor)
27100Sstevel@tonic-gate {
27110Sstevel@tonic-gate 	char *cp, c;
27120Sstevel@tonic-gate 	size_t len;
27130Sstevel@tonic-gate 
27140Sstevel@tonic-gate 
27150Sstevel@tonic-gate 	if (minor == DI_MINOR_NIL) {
27160Sstevel@tonic-gate 		return (NULL);
27170Sstevel@tonic-gate 	}
27180Sstevel@tonic-gate 
27190Sstevel@tonic-gate 	cp = di_minor_nodetype(minor);
27200Sstevel@tonic-gate 	if (cp == NULL) {
27210Sstevel@tonic-gate 		return (NULL);
27220Sstevel@tonic-gate 	}
27230Sstevel@tonic-gate 
27240Sstevel@tonic-gate 	len = strlen(DDI_NT_ATTACHMENT_POINT);
27250Sstevel@tonic-gate 	if (strncmp(cp, DDI_NT_ATTACHMENT_POINT, len)) {
27260Sstevel@tonic-gate 		return (NULL);
27270Sstevel@tonic-gate 	}
27280Sstevel@tonic-gate 
27290Sstevel@tonic-gate 	cp += len;
27300Sstevel@tonic-gate 
27310Sstevel@tonic-gate 	c = *cp;
27320Sstevel@tonic-gate 	if (c != '\0' && c != ':') {
27330Sstevel@tonic-gate 		return (NULL);
27340Sstevel@tonic-gate 	}
27350Sstevel@tonic-gate 
27360Sstevel@tonic-gate 	if (c == ':') {
27370Sstevel@tonic-gate 		cp++;
27380Sstevel@tonic-gate 	}
27390Sstevel@tonic-gate 
27400Sstevel@tonic-gate 	return (cp);
27410Sstevel@tonic-gate 
27420Sstevel@tonic-gate }
27430Sstevel@tonic-gate 
27440Sstevel@tonic-gate /*
27450Sstevel@tonic-gate  * Transform stat data to list data
27460Sstevel@tonic-gate  */
27470Sstevel@tonic-gate static void
27480Sstevel@tonic-gate stat_to_list(cfga_list_data_t *lp, cfga_stat_data_t *statp)
27490Sstevel@tonic-gate {
27500Sstevel@tonic-gate 
27510Sstevel@tonic-gate 	(void) snprintf(lp->ap_log_id, sizeof (lp->ap_log_id), "%s",
27520Sstevel@tonic-gate 	    statp->ap_log_id);
27530Sstevel@tonic-gate 
27540Sstevel@tonic-gate 	(void) snprintf(lp->ap_phys_id, sizeof (lp->ap_phys_id), "%s",
27550Sstevel@tonic-gate 	    statp->ap_phys_id);
27560Sstevel@tonic-gate 
27570Sstevel@tonic-gate 	(void) snprintf(lp->ap_class, sizeof (lp->ap_class), "%s",
27580Sstevel@tonic-gate 	    CFGA_NO_CLASS);
27590Sstevel@tonic-gate 
27600Sstevel@tonic-gate 	lp->ap_r_state = statp->ap_r_state;
27610Sstevel@tonic-gate 	lp->ap_o_state = statp->ap_o_state;
27620Sstevel@tonic-gate 	lp->ap_cond = statp->ap_cond;
27630Sstevel@tonic-gate 	lp->ap_busy = statp->ap_busy;
27640Sstevel@tonic-gate 	lp->ap_status_time = statp->ap_status_time;
27650Sstevel@tonic-gate 
27660Sstevel@tonic-gate 	(void) snprintf(lp->ap_info, sizeof (lp->ap_info), "%s",
27670Sstevel@tonic-gate 	    statp->ap_info);
27680Sstevel@tonic-gate 	(void) snprintf(lp->ap_type, sizeof (lp->ap_type), "%s",
27690Sstevel@tonic-gate 	    statp->ap_type);
27700Sstevel@tonic-gate }
27710Sstevel@tonic-gate 
27720Sstevel@tonic-gate static void
27730Sstevel@tonic-gate lstat_free(list_stat_t *lstatp)
27740Sstevel@tonic-gate {
27750Sstevel@tonic-gate 	stat_data_list_t *slp, *slp2;
27760Sstevel@tonic-gate 	array_list_t *ap, *ap2;
27770Sstevel@tonic-gate 
27780Sstevel@tonic-gate 	slp = lstatp->sdl;
27790Sstevel@tonic-gate 	while (slp != NULL) {
27800Sstevel@tonic-gate 		slp2 = slp->next;
27810Sstevel@tonic-gate 		S_FREE(slp);
27820Sstevel@tonic-gate 		slp = slp2;
27830Sstevel@tonic-gate 	}
27840Sstevel@tonic-gate 
27850Sstevel@tonic-gate 	lstatp->sdl = NULL;
27860Sstevel@tonic-gate 
27870Sstevel@tonic-gate 	ap = lstatp->al;
27880Sstevel@tonic-gate 	while (ap != NULL) {
27890Sstevel@tonic-gate 		ap2 = ap->next;
27900Sstevel@tonic-gate 		S_FREE(ap->array);
27910Sstevel@tonic-gate 		S_FREE(ap);
27920Sstevel@tonic-gate 		ap = ap2;
27930Sstevel@tonic-gate 	}
27940Sstevel@tonic-gate 
27950Sstevel@tonic-gate 	lstatp->al = NULL;
27960Sstevel@tonic-gate }
27970Sstevel@tonic-gate 
27980Sstevel@tonic-gate static cfga_err_t
27990Sstevel@tonic-gate split_apid(char *ap_id, char **dyncompp, char **errstring)
28000Sstevel@tonic-gate {
28010Sstevel@tonic-gate 	char *cp;
28020Sstevel@tonic-gate 
28030Sstevel@tonic-gate 	*dyncompp = NULL;
28040Sstevel@tonic-gate 
28050Sstevel@tonic-gate 	if (ap_id == NULL) {
28060Sstevel@tonic-gate 		return (CFGA_ERROR);
28070Sstevel@tonic-gate 	}
28080Sstevel@tonic-gate 
28090Sstevel@tonic-gate 	if ((cp = strstr(ap_id, CFGA_DYN_SEP)) == NULL) {
28100Sstevel@tonic-gate 		return (CFGA_OK);
28110Sstevel@tonic-gate 	}
28120Sstevel@tonic-gate 
28130Sstevel@tonic-gate 	*cp = '\0';
28140Sstevel@tonic-gate 	cp += strlen(CFGA_DYN_SEP);
28150Sstevel@tonic-gate 	if ((*dyncompp = config_calloc_check(1, strlen(cp) + 1,
28160Sstevel@tonic-gate 	    errstring)) == NULL) {
28170Sstevel@tonic-gate 		return (CFGA_LIB_ERROR);
28180Sstevel@tonic-gate 	}
28190Sstevel@tonic-gate 	(void) strcpy(*dyncompp, cp);
28200Sstevel@tonic-gate 
28210Sstevel@tonic-gate 	return (CFGA_OK);
28220Sstevel@tonic-gate }
28230Sstevel@tonic-gate 
28240Sstevel@tonic-gate static void
28250Sstevel@tonic-gate append_dyn(char *buf, const char *dyncomp, size_t blen)
28260Sstevel@tonic-gate {
28270Sstevel@tonic-gate 	if (dyncomp != NULL) {
28280Sstevel@tonic-gate 		char *cp = buf + strlen(buf);
28290Sstevel@tonic-gate 		size_t len = blen - strlen(buf);
28300Sstevel@tonic-gate 
28310Sstevel@tonic-gate 		(void) snprintf(cp, len, "%s%s", CFGA_DYN_SEP,
28320Sstevel@tonic-gate 		    dyncomp);
28330Sstevel@tonic-gate 	}
28340Sstevel@tonic-gate }
28350Sstevel@tonic-gate 
28360Sstevel@tonic-gate /*
28370Sstevel@tonic-gate  * Default implementation of cfga_ap_id_cmp. Works for most cases
28380Sstevel@tonic-gate  * except for long hex number sequences like world-wide-name.
28390Sstevel@tonic-gate  *
28400Sstevel@tonic-gate  * This function compares the ap's in a generic way.  It does so by
28410Sstevel@tonic-gate  * determining the place of difference between the 2 aps.  If the first
28420Sstevel@tonic-gate  * difference is a digit, it attempts to obtain the numbers and compare them
28430Sstevel@tonic-gate  * Otherwise it just compares the aps as strings
28440Sstevel@tonic-gate  */
28450Sstevel@tonic-gate static int
28460Sstevel@tonic-gate default_ap_id_cmp(const char *ap_id1, const char *ap_id2)
28470Sstevel@tonic-gate {
28480Sstevel@tonic-gate 	int i = 0;
28490Sstevel@tonic-gate 
28500Sstevel@tonic-gate 	/*
28510Sstevel@tonic-gate 	 * Search for first different char
28520Sstevel@tonic-gate 	 */
28530Sstevel@tonic-gate 	while (ap_id1[i] == ap_id2[i] && ap_id1[i] != '\0')
28540Sstevel@tonic-gate 		i++;
28550Sstevel@tonic-gate 
28560Sstevel@tonic-gate 	/*
28570Sstevel@tonic-gate 	 * If one of the char is a digit, back up to where the
28580Sstevel@tonic-gate 	 * number started, compare the number.
28590Sstevel@tonic-gate 	 */
28600Sstevel@tonic-gate 	if (isdigit(ap_id1[i]) || isdigit(ap_id2[i])) {
28610Sstevel@tonic-gate 		while ((i > 0) && isdigit(ap_id1[i - 1]))
28620Sstevel@tonic-gate 			i--;
28630Sstevel@tonic-gate 
28640Sstevel@tonic-gate 		if (isdigit(ap_id1[i]) && isdigit(ap_id2[i]))
28650Sstevel@tonic-gate 			return (atoi(ap_id1 + i) - atoi(ap_id2 + i));
28660Sstevel@tonic-gate 	}
28670Sstevel@tonic-gate 
28680Sstevel@tonic-gate 	/* One of them isn't a number, compare the char */
28690Sstevel@tonic-gate 	return (ap_id1[i] - ap_id2[i]);
28700Sstevel@tonic-gate }
28710Sstevel@tonic-gate 
28720Sstevel@tonic-gate static void
28730Sstevel@tonic-gate hold_lib(plugin_lib_t *libp)
28740Sstevel@tonic-gate {
28750Sstevel@tonic-gate 	assert(libp->refcnt >= 0);
28760Sstevel@tonic-gate 	(void) mutex_lock(&libp->lock);
28770Sstevel@tonic-gate 	libp->refcnt++;
28780Sstevel@tonic-gate 	(void) mutex_unlock(&libp->lock);
28790Sstevel@tonic-gate }
28800Sstevel@tonic-gate 
28810Sstevel@tonic-gate static void
28820Sstevel@tonic-gate rele_lib(plugin_lib_t *libp)
28830Sstevel@tonic-gate {
28840Sstevel@tonic-gate 	assert(libp->refcnt > 0);
28850Sstevel@tonic-gate 	(void) mutex_lock(&libp->lock);
28860Sstevel@tonic-gate 	libp->refcnt--;
28870Sstevel@tonic-gate 	(void) mutex_unlock(&libp->lock);
28880Sstevel@tonic-gate }
2889