xref: /onnv-gate/usr/src/lib/fm/libfmd_snmp/common/module.c (revision 11050:be69f645ce17)
11303Swesolows /*
21303Swesolows  * CDDL HEADER START
31303Swesolows  *
41303Swesolows  * The contents of this file are subject to the terms of the
51303Swesolows  * Common Development and Distribution License (the "License").
61303Swesolows  * You may not use this file except in compliance with the License.
71303Swesolows  *
81303Swesolows  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
91303Swesolows  * or http://www.opensolaris.org/os/licensing.
101303Swesolows  * See the License for the specific language governing permissions
111303Swesolows  * and limitations under the License.
121303Swesolows  *
131303Swesolows  * When distributing Covered Code, include this CDDL HEADER in each
141303Swesolows  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
151303Swesolows  * If applicable, add the following below this CDDL HEADER, with the
161303Swesolows  * fields enclosed by brackets "[]" replaced with your own identifying
171303Swesolows  * information: Portions Copyright [yyyy] [name of copyright owner]
181303Swesolows  *
191303Swesolows  * CDDL HEADER END
201303Swesolows  */
211303Swesolows 
221303Swesolows /*
23*11050SRobert.Johnston@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
241303Swesolows  * Use is subject to license terms.
251303Swesolows  */
261303Swesolows 
271303Swesolows #include <fm/fmd_adm.h>
281303Swesolows #include <fm/fmd_snmp.h>
291303Swesolows #include <net-snmp/net-snmp-config.h>
301303Swesolows #include <net-snmp/net-snmp-includes.h>
311303Swesolows #include <net-snmp/agent/net-snmp-agent-includes.h>
321303Swesolows #include <pthread.h>
331303Swesolows #include <stddef.h>
341303Swesolows #include <errno.h>
351303Swesolows #include <libuutil.h>
361303Swesolows #include "sunFM_impl.h"
371303Swesolows #include "module.h"
381303Swesolows 
391303Swesolows static uu_avl_pool_t	*mod_name_avl_pool;
401303Swesolows static uu_avl_pool_t	*mod_index_avl_pool;
411303Swesolows static uu_avl_t		*mod_name_avl;
421303Swesolows static uu_avl_t		*mod_index_avl;
431303Swesolows 
441303Swesolows #define	VALID_AVL_STATE	(mod_name_avl_pool != NULL &&		\
451303Swesolows 	mod_index_avl_pool != NULL && mod_name_avl != NULL &&	\
461303Swesolows 	mod_index_avl != NULL)
471303Swesolows 
481303Swesolows #define	UPDATE_WAIT_MILLIS	10	/* poll interval in milliseconds */
491303Swesolows 
501303Swesolows /*
511303Swesolows  * Update types.  Single-index and all are mutually exclusive.
521303Swesolows  */
531303Swesolows #define	UCT_INDEX	0x1
541303Swesolows #define	UCT_ALL		0x2
551303Swesolows #define	UCT_FLAGS	0x3
561303Swesolows 
571303Swesolows #define	MODULE_DATA_VALID(d)	((d)->d_valid == valid_stamp)
581303Swesolows 
591303Swesolows /*
601303Swesolows  * Locking rules are straightforward.  There is only one updater thread
611303Swesolows  * for each table, and requests for update that are received while
621303Swesolows  * another update is in progress are ignored.  The single per-table lock
631303Swesolows  * protects all the data for the table, the valid_stamp and max_index
641303Swesolows  * tags for new data, and - importantly - all the hidden static data
651303Swesolows  * used by the Net-SNMP library.  The result return callbacks are always
661303Swesolows  * called in the master agent thread; holding the table lock is
671303Swesolows  * therefore sufficient since only one table's callback can be run at
681303Swesolows  * any one time.  Finer-grained locking is possible here but
691303Swesolows  * substantially more difficult because nearly all Net-SNMP functions
701303Swesolows  * are unsafe.
711303Swesolows  *
721303Swesolows  * In practice this is more than adequate, since the purpose of
731303Swesolows  * threading out the update is to prevent excessively time-consuming
741303Swesolows  * data collection from bottlenecking the entire agent, not to improve
751303Swesolows  * result throughput (SNMP is not intended to be used for applications
761303Swesolows  * requiring high throughput anyway).  If the agent itself ever becomes
771303Swesolows  * multithreaded, locking requirements become limited to our local
781303Swesolows  * per-table data (the tree, max_index, and valid_stamp), and the
791303Swesolows  * implementation could be revisited for better performance.
801303Swesolows  */
811303Swesolows 
821303Swesolows static ulong_t		max_index;
831303Swesolows static int		valid_stamp;
841303Swesolows static pthread_mutex_t	update_lock;
851303Swesolows static pthread_cond_t	update_cv;
861303Swesolows static volatile enum { US_QUIET, US_NEEDED, US_INPROGRESS } update_status;
871303Swesolows 
881303Swesolows static Netsnmp_Node_Handler	sunFmModuleTable_handler;
891303Swesolows 
901303Swesolows static sunFmModule_data_t *
key_build(const char * name,const ulong_t index)911303Swesolows key_build(const char *name, const ulong_t index)
921303Swesolows {
931303Swesolows 	static sunFmModule_data_t	key;
941303Swesolows 
951303Swesolows 	key.d_index = index;
961303Swesolows 	if (name)
972134Swesolows 		(void) strlcpy(key.d_ami_name, name, sizeof (key.d_ami_name));
981303Swesolows 	else
991303Swesolows 		key.d_ami_name[0] = '\0';
1001303Swesolows 
1011303Swesolows 	return (&key);
1021303Swesolows }
1031303Swesolows 
1041303Swesolows /*
1051303Swesolows  * If name is the name of a module we have previously seen and indexed, return
1061303Swesolows  * data for it.  Otherwise, return NULL.  Note that the module may not be
1071303Swesolows  * valid; that is, it may have been removed from the fault manager since its
1081303Swesolows  * information was last updated.
1091303Swesolows  */
1101303Swesolows static sunFmModule_data_t *
module_lookup_name(const char * name)1111303Swesolows module_lookup_name(const char *name)
1121303Swesolows {
1131303Swesolows 	sunFmModule_data_t	*key;
1141303Swesolows 
1151303Swesolows 	key = key_build(name, 0);
1161303Swesolows 	return (uu_avl_find(mod_name_avl, key, NULL, NULL));
1171303Swesolows }
1181303Swesolows 
1191303Swesolows /*
1201303Swesolows  * If index corresponds to a module we have previously seen and indexed, return
1211303Swesolows  * data for it.  Otherwise, return NULL.  Note that the module may not be
1221303Swesolows  * valid; that is, it may have been removed from the fault manager since its
1231303Swesolows  * information was last updated.
1241303Swesolows  */
1251303Swesolows static sunFmModule_data_t *
module_lookup_index_exact(const ulong_t index)1261303Swesolows module_lookup_index_exact(const ulong_t index)
1271303Swesolows {
1281303Swesolows 	sunFmModule_data_t	*key;
1291303Swesolows 
1301303Swesolows 	key = key_build(NULL, index);
1311303Swesolows 	return (uu_avl_find(mod_index_avl, key, NULL, NULL));
1321303Swesolows }
1331303Swesolows 
1341303Swesolows /*
1351303Swesolows  * If index corresponds to a valid (that is, extant as of latest information
1361303Swesolows  * from the fault manager) fmd module, return the data for that module.
1371303Swesolows  * Otherwise, return the data for the valid module whose index is as close as
1381303Swesolows  * possible to index but not lower.  This preserves the lexicographical
1391303Swesolows  * ordering required for GETNEXT processing.
1401303Swesolows  */
1411303Swesolows static sunFmModule_data_t *
module_lookup_index_nextvalid(const ulong_t index)1421303Swesolows module_lookup_index_nextvalid(const ulong_t index)
1431303Swesolows {
1441303Swesolows 	sunFmModule_data_t	*key, *data;
1451303Swesolows 	uu_avl_index_t		idx;
1461303Swesolows 
1471303Swesolows 	key = key_build(NULL, index);
1481303Swesolows 
1491303Swesolows 	if ((data = uu_avl_find(mod_index_avl, key, NULL, &idx)) != NULL &&
1501303Swesolows 	    MODULE_DATA_VALID(data))
1511303Swesolows 		return (data);
1521303Swesolows 
1531303Swesolows 	data = uu_avl_nearest_next(mod_index_avl, idx);
1541303Swesolows 
1553955Swesolows 	while (data != NULL && !MODULE_DATA_VALID(data))
1563955Swesolows 		data = uu_avl_next(mod_index_avl, data);
1571303Swesolows 
1581303Swesolows 	return (data);
1591303Swesolows }
1601303Swesolows 
1611303Swesolows /*
1621303Swesolows  * Possible update the contents of a single module within the cache.  This
1631303Swesolows  * is our callback from fmd_module_iter.
1641303Swesolows  */
1651303Swesolows static int
modinfo_update_one(const fmd_adm_modinfo_t * modinfo,void * arg)1661303Swesolows modinfo_update_one(const fmd_adm_modinfo_t *modinfo, void *arg)
1671303Swesolows {
1681303Swesolows 	const sunFmModule_update_ctx_t *update_ctx =
1691303Swesolows 	    (sunFmModule_update_ctx_t *)arg;
1701303Swesolows 	sunFmModule_data_t *data = module_lookup_name(modinfo->ami_name);
1711303Swesolows 
1721303Swesolows 	/*
1731303Swesolows 	 * An fmd module we haven't seen before.  We're obligated to index
1741303Swesolows 	 * it and link it into our cache so that we can find it, but we're
1751303Swesolows 	 * not obligated to fill it in completely unless we're doing a
1761303Swesolows 	 * thorough update or this is the module we were asked for.  This
1771303Swesolows 	 * avoids unnecessary iteration and memory manipulation for data
1781303Swesolows 	 * we're not going to return for this request.
1791303Swesolows 	 */
1801303Swesolows 	if (data == NULL) {
1811303Swesolows 		uu_avl_index_t idx;
1821303Swesolows 
1831303Swesolows 		DEBUGMSGTL((MODNAME_STR, "found new fmd module %s\n",
1841303Swesolows 		    modinfo->ami_name));
1851303Swesolows 		if ((data = SNMP_MALLOC_TYPEDEF(sunFmModule_data_t)) == NULL) {
186*11050SRobert.Johnston@Sun.COM 			(void) snmp_log(LOG_ERR, MODNAME_STR ": Out of memory "
187*11050SRobert.Johnston@Sun.COM 			    "for new module data at %s:%d\n", __FILE__,
188*11050SRobert.Johnston@Sun.COM 			    __LINE__);
1891303Swesolows 			return (1);
1901303Swesolows 		}
1911303Swesolows 		/*
1921303Swesolows 		 * We allocate indices sequentially and never reuse them.
1931303Swesolows 		 * This ensures we can always return valid GETNEXT responses
1941303Swesolows 		 * without having to reindex, and it provides the user a
1951303Swesolows 		 * more consistent view of the fault manager.
1961303Swesolows 		 */
1971303Swesolows 		data->d_index = ++max_index;
1981303Swesolows 		DEBUGMSGTL((MODNAME_STR, "index %lu is %s@%p\n", data->d_index,
1991303Swesolows 		    modinfo->ami_name, data));
2001303Swesolows 
2012134Swesolows 		(void) strlcpy(data->d_ami_name, modinfo->ami_name,
2021303Swesolows 		    sizeof (data->d_ami_name));
2031303Swesolows 
2041303Swesolows 		uu_avl_node_init(data, &data->d_name_avl, mod_name_avl_pool);
2051303Swesolows 		(void) uu_avl_find(mod_name_avl, data, NULL, &idx);
2061303Swesolows 		uu_avl_insert(mod_name_avl, data, idx);
2071303Swesolows 
2081303Swesolows 		uu_avl_node_init(data, &data->d_index_avl, mod_index_avl_pool);
2091303Swesolows 		(void) uu_avl_find(mod_index_avl, data, NULL, &idx);
2101303Swesolows 		uu_avl_insert(mod_index_avl, data, idx);
2111303Swesolows 
2121303Swesolows 		DEBUGMSGTL((MODNAME_STR, "completed new module %lu/%s@%p\n",
2131303Swesolows 		    data->d_index, data->d_ami_name, data));
2141303Swesolows 	}
2151303Swesolows 
2161303Swesolows 	data->d_valid = valid_stamp;
2171303Swesolows 
2181303Swesolows 	DEBUGMSGTL((MODNAME_STR, "timestamp updated for %lu/%s@%p: %lu\n",
2191303Swesolows 	    data->d_index, data->d_ami_name, data, data->d_valid));
2201303Swesolows 
2211303Swesolows 	if ((update_ctx->uc_type & UCT_ALL) ||
2221303Swesolows 	    update_ctx->uc_index == data->d_index) {
2232134Swesolows 		(void) strlcpy(data->d_ami_vers, modinfo->ami_vers,
2241303Swesolows 		    sizeof (data->d_ami_vers));
2252134Swesolows 		(void) strlcpy(data->d_ami_desc, modinfo->ami_desc,
2261303Swesolows 		    sizeof (data->d_ami_desc));
2271303Swesolows 		data->d_ami_flags = modinfo->ami_flags;
2281303Swesolows 	}
2291303Swesolows 
2301303Swesolows 	return (!(update_ctx->uc_type & UCT_ALL) &&
2311303Swesolows 	    update_ctx->uc_index == data->d_index);
2321303Swesolows }
2331303Swesolows 
2341303Swesolows /*
2351303Swesolows  * Update some or all module data from fmd.  If thorough is set, all modules
2361303Swesolows  * will be indexed and their data cached.  Otherwise, updates will stop once
2371303Swesolows  * the module matching index has been updated.
2381303Swesolows  *
2391303Swesolows  * Returns appropriate SNMP error codes.
2401303Swesolows  */
2411303Swesolows static int
modinfo_update(sunFmModule_update_ctx_t * update_ctx)2421303Swesolows modinfo_update(sunFmModule_update_ctx_t *update_ctx)
2431303Swesolows {
2441303Swesolows 	fmd_adm_t *adm;
2451303Swesolows 
2461303Swesolows 	ASSERT(update_ctx != NULL);
2471303Swesolows 	ASSERT((update_ctx->uc_type & (UCT_INDEX|UCT_ALL)) !=
2481303Swesolows 	    (UCT_INDEX|UCT_ALL));
2491303Swesolows 	ASSERT((update_ctx->uc_type & ~UCT_FLAGS) == 0);
2501303Swesolows 	ASSERT(VALID_AVL_STATE);
2511303Swesolows 
2521303Swesolows 	if ((adm = fmd_adm_open(update_ctx->uc_host, update_ctx->uc_prog,
2531303Swesolows 	    update_ctx->uc_version)) == NULL) {
254*11050SRobert.Johnston@Sun.COM 		(void) snmp_log(LOG_ERR, MODNAME_STR ": Communication with fmd "
2551303Swesolows 		    "failed: %s\n", strerror(errno));
2561303Swesolows 		return (SNMP_ERR_RESOURCEUNAVAILABLE);
2571303Swesolows 	}
2581303Swesolows 
2591303Swesolows 	++valid_stamp;
2601303Swesolows 	if (fmd_adm_module_iter(adm, modinfo_update_one, update_ctx) != 0) {
261*11050SRobert.Johnston@Sun.COM 		(void) snmp_log(LOG_ERR, MODNAME_STR ": fmd module information "
262*11050SRobert.Johnston@Sun.COM 		    "update failed: %s\n", fmd_adm_errmsg(adm));
2631303Swesolows 		fmd_adm_close(adm);
2641303Swesolows 		return (SNMP_ERR_RESOURCEUNAVAILABLE);
2651303Swesolows 	}
2661303Swesolows 
2671303Swesolows 	DEBUGMSGTL((MODNAME_STR, "module iteration completed\n"));
2681303Swesolows 
2691303Swesolows 	fmd_adm_close(adm);
2701303Swesolows 	return (SNMP_ERR_NOERROR);
2711303Swesolows }
2721303Swesolows 
2731303Swesolows /*ARGSUSED*/
2741303Swesolows static void
update_thread(void * arg)2751303Swesolows update_thread(void *arg)
2761303Swesolows {
2771303Swesolows 	/*
2781303Swesolows 	 * The current modinfo_update implementation offers minimal savings
2791303Swesolows 	 * for the use of index-only updates; therefore we always do a full
2801303Swesolows 	 * update.  If it becomes advantageous to limit updates to a single
2811303Swesolows 	 * index, the contexts can be queued by the handler instead.
2821303Swesolows 	 */
2831303Swesolows 	sunFmModule_update_ctx_t	uc;
2841303Swesolows 
2851303Swesolows 	uc.uc_host = NULL;
2861303Swesolows 	uc.uc_prog = FMD_ADM_PROGRAM;
2871303Swesolows 	uc.uc_version = FMD_ADM_VERSION;
2881303Swesolows 
2891303Swesolows 	uc.uc_index = 0;
2901303Swesolows 	uc.uc_type = UCT_ALL;
2911303Swesolows 
2921303Swesolows 	for (;;) {
2931303Swesolows 		(void) pthread_mutex_lock(&update_lock);
2941303Swesolows 		update_status = US_QUIET;
2951303Swesolows 		while (update_status == US_QUIET)
2961303Swesolows 			(void) pthread_cond_wait(&update_cv, &update_lock);
2971303Swesolows 		update_status = US_INPROGRESS;
2981303Swesolows 		(void) pthread_mutex_unlock(&update_lock);
2991303Swesolows 		(void) modinfo_update(&uc);
3001303Swesolows 	}
3011303Swesolows }
3021303Swesolows 
3031303Swesolows static void
request_update(void)3041303Swesolows request_update(void)
3051303Swesolows {
3061303Swesolows 	(void) pthread_mutex_lock(&update_lock);
3071303Swesolows 	if (update_status != US_QUIET) {
3081303Swesolows 		(void) pthread_mutex_unlock(&update_lock);
3091303Swesolows 		return;
3101303Swesolows 	}
3111303Swesolows 	update_status = US_NEEDED;
3121303Swesolows 	(void) pthread_cond_signal(&update_cv);
3131303Swesolows 	(void) pthread_mutex_unlock(&update_lock);
3141303Swesolows }
3151303Swesolows 
3161303Swesolows /*ARGSUSED*/
3171303Swesolows static int
module_compare_name(const void * l,const void * r,void * private)3181303Swesolows module_compare_name(const void *l, const void *r, void *private)
3191303Swesolows {
3201303Swesolows 	sunFmModule_data_t	*l_data = (sunFmModule_data_t *)l;
3211303Swesolows 	sunFmModule_data_t	*r_data = (sunFmModule_data_t *)r;
3221303Swesolows 
3231303Swesolows 	ASSERT(l_data != NULL && r_data != NULL);
3241303Swesolows 
3251303Swesolows 	return (strcmp(l_data->d_ami_name, r_data->d_ami_name));
3261303Swesolows }
3271303Swesolows 
3281303Swesolows /*ARGSUSED*/
3291303Swesolows static int
module_compare_index(const void * l,const void * r,void * private)3301303Swesolows module_compare_index(const void *l, const void *r, void *private)
3311303Swesolows {
3321303Swesolows 	sunFmModule_data_t	*l_data = (sunFmModule_data_t *)l;
3331303Swesolows 	sunFmModule_data_t	*r_data = (sunFmModule_data_t *)r;
3341303Swesolows 
3351303Swesolows 	ASSERT(l_data != NULL && r_data != NULL);
3361303Swesolows 
3371303Swesolows 	return (l_data->d_index < r_data->d_index ? -1 :
3381303Swesolows 	    l_data->d_index > r_data->d_index ? 1 : 0);
3391303Swesolows }
3401303Swesolows 
3411303Swesolows int
sunFmModuleTable_init(void)3421303Swesolows sunFmModuleTable_init(void)
3431303Swesolows {
3441303Swesolows 	static oid sunFmModuleTable_oid[] = { SUNFMMODULETABLE_OID };
3451303Swesolows 	netsnmp_table_registration_info *table_info;
3461303Swesolows 	netsnmp_handler_registration *handler;
3471303Swesolows 	int err;
3481303Swesolows 
3491303Swesolows 	if ((err = pthread_mutex_init(&update_lock, NULL)) != 0) {
350*11050SRobert.Johnston@Sun.COM 		(void) snmp_log(LOG_ERR, MODNAME_STR ": mutex_init failure: "
351*11050SRobert.Johnston@Sun.COM 		    "%s\n", strerror(err));
3521303Swesolows 		return (MIB_REGISTRATION_FAILED);
3531303Swesolows 	}
3541303Swesolows 	if ((err = pthread_cond_init(&update_cv, NULL)) != 0) {
355*11050SRobert.Johnston@Sun.COM 		(void) snmp_log(LOG_ERR, MODNAME_STR ": cond_init failure: "
356*11050SRobert.Johnston@Sun.COM 		    "%s\n", strerror(err));
3571303Swesolows 		return (MIB_REGISTRATION_FAILED);
3581303Swesolows 	}
3591303Swesolows 
3601303Swesolows 	if ((err = pthread_create(NULL, NULL, (void *(*)(void *))update_thread,
3611303Swesolows 	    NULL)) != 0) {
362*11050SRobert.Johnston@Sun.COM 		(void) snmp_log(LOG_ERR, MODNAME_STR ": error creating update "
3631303Swesolows 		    "thread: %s\n", strerror(err));
3641303Swesolows 		return (MIB_REGISTRATION_FAILED);
3651303Swesolows 	}
3661303Swesolows 
3671303Swesolows 	if ((table_info =
3681303Swesolows 	    SNMP_MALLOC_TYPEDEF(netsnmp_table_registration_info)) == NULL)
3691303Swesolows 		return (MIB_REGISTRATION_FAILED);
3701303Swesolows 
3711303Swesolows 	if ((handler = netsnmp_create_handler_registration("sunFmModuleTable",
3721303Swesolows 	    sunFmModuleTable_handler, sunFmModuleTable_oid,
3731303Swesolows 	    OID_LENGTH(sunFmModuleTable_oid), HANDLER_CAN_RONLY)) == NULL) {
3741303Swesolows 		SNMP_FREE(table_info);
3751303Swesolows 		return (MIB_REGISTRATION_FAILED);
3761303Swesolows 	}
3771303Swesolows 
3781303Swesolows 	/*
3791303Swesolows 	 * The Net-SNMP template uses add_indexes here, but that
3801303Swesolows 	 * function is unsafe because it does not check for failure.
3811303Swesolows 	 */
3821303Swesolows 	if (netsnmp_table_helper_add_index(table_info, ASN_UNSIGNED) == NULL) {
3831303Swesolows 		SNMP_FREE(table_info);
3841303Swesolows 		SNMP_FREE(handler);
3851303Swesolows 		return (MIB_REGISTRATION_FAILED);
3861303Swesolows 	}
3871303Swesolows 
3881303Swesolows 	table_info->min_column = SUNFMMODULE_COLMIN;
3891303Swesolows 	table_info->max_column = SUNFMMODULE_COLMAX;
3901303Swesolows 
3911303Swesolows 	if ((mod_name_avl_pool = uu_avl_pool_create("mod_name",
3921303Swesolows 	    sizeof (sunFmModule_data_t),
3931303Swesolows 	    offsetof(sunFmModule_data_t, d_name_avl), module_compare_name,
3941303Swesolows 	    UU_AVL_DEBUG)) == NULL) {
3951303Swesolows 		snmp_free_varbind(table_info->indexes);
3961303Swesolows 		SNMP_FREE(table_info);
3971303Swesolows 		SNMP_FREE(handler);
3981303Swesolows 	}
3991303Swesolows 
4001303Swesolows 	if ((mod_name_avl = uu_avl_create(mod_name_avl_pool, NULL,
4011303Swesolows 	    UU_AVL_DEBUG)) == NULL) {
402*11050SRobert.Johnston@Sun.COM 		(void) snmp_log(LOG_ERR, MODNAME_STR ": mod_name_avl creation "
4031303Swesolows 		    "failed: %s\n", uu_strerror(uu_error()));
4041303Swesolows 		snmp_free_varbind(table_info->indexes);
4051303Swesolows 		SNMP_FREE(table_info);
4061303Swesolows 		SNMP_FREE(handler);
4071303Swesolows 		uu_avl_pool_destroy(mod_name_avl_pool);
4081303Swesolows 		return (MIB_REGISTRATION_FAILED);
4091303Swesolows 	}
4101303Swesolows 
4111303Swesolows 	if ((mod_index_avl_pool = uu_avl_pool_create("mod_index",
4121303Swesolows 	    sizeof (sunFmModule_data_t),
4131303Swesolows 	    offsetof(sunFmModule_data_t, d_index_avl),
4141303Swesolows 	    module_compare_index, UU_AVL_DEBUG)) == NULL) {
4151303Swesolows 		snmp_free_varbind(table_info->indexes);
4161303Swesolows 		SNMP_FREE(table_info);
4171303Swesolows 		SNMP_FREE(handler);
4181303Swesolows 		uu_avl_destroy(mod_name_avl);
4191303Swesolows 		uu_avl_pool_destroy(mod_name_avl_pool);
4201303Swesolows 	}
4211303Swesolows 
4221303Swesolows 	if ((mod_index_avl = uu_avl_create(mod_index_avl_pool, NULL,
4231303Swesolows 	    UU_AVL_DEBUG)) == NULL) {
424*11050SRobert.Johnston@Sun.COM 		(void) snmp_log(LOG_ERR, MODNAME_STR ": mod_index_avl creation "
4251303Swesolows 		    "failed: %s\n", uu_strerror(uu_error()));
4261303Swesolows 		snmp_free_varbind(table_info->indexes);
4271303Swesolows 		SNMP_FREE(table_info);
4281303Swesolows 		SNMP_FREE(handler);
4291303Swesolows 		uu_avl_destroy(mod_name_avl);
4301303Swesolows 		uu_avl_pool_destroy(mod_name_avl_pool);
4311303Swesolows 		uu_avl_pool_destroy(mod_index_avl_pool);
4321303Swesolows 		return (MIB_REGISTRATION_FAILED);
4331303Swesolows 	}
4341303Swesolows 
4351303Swesolows 	if ((err = netsnmp_register_table(handler, table_info)) !=
4361303Swesolows 	    MIB_REGISTERED_OK) {
4371303Swesolows 		snmp_free_varbind(table_info->indexes);
4381303Swesolows 		SNMP_FREE(table_info);
4391303Swesolows 		SNMP_FREE(handler);
4401303Swesolows 		uu_avl_destroy(mod_name_avl);
4411303Swesolows 		uu_avl_pool_destroy(mod_name_avl_pool);
4421303Swesolows 		uu_avl_destroy(mod_index_avl);
4431303Swesolows 		uu_avl_pool_destroy(mod_index_avl_pool);
4441303Swesolows 		return (err);
4451303Swesolows 	}
4461303Swesolows 
4471303Swesolows 	return (MIB_REGISTERED_OK);
4481303Swesolows }
4491303Swesolows 
4501303Swesolows /*
4511303Swesolows  * These two functions form the core of GET/GETNEXT/GETBULK handling (the
4521303Swesolows  * only kind we do).  They perform two functions:
4531303Swesolows  *
4541303Swesolows  * - First, frob the request to set all the index variables to correspond
4551303Swesolows  *   to the value that's going to be returned.  For GET, this is a nop;
4561303Swesolows  *   for GETNEXT/GETBULK it always requires some work.
4571303Swesolows  * - Second, find and return the fmd module information corresponding to
4581303Swesolows  *   the (possibly updated) indices.
4591303Swesolows  *
4601303Swesolows  * These should be as fast as possible; they run in the agent thread.
4611303Swesolows  */
4621303Swesolows static sunFmModule_data_t *
sunFmModuleTable_nextmod(netsnmp_handler_registration * reginfo,netsnmp_table_request_info * table_info)4631303Swesolows sunFmModuleTable_nextmod(netsnmp_handler_registration *reginfo,
4641303Swesolows     netsnmp_table_request_info *table_info)
4651303Swesolows {
4661303Swesolows 	sunFmModule_data_t	*data;
4671303Swesolows 	netsnmp_variable_list	*var;
4681303Swesolows 	ulong_t index;
4691303Swesolows 
4701303Swesolows 	/*
4711303Swesolows 	 * If we have no index, we must make one.
4721303Swesolows 	 */
4731303Swesolows 	if (table_info->number_indexes < 1) {
4741303Swesolows 		oid tmpoid[MAX_OID_LEN];
4751303Swesolows 		index = 1;
4761303Swesolows 
4771303Swesolows 		DEBUGMSGTL((MODNAME_STR, "nextmod: no indexes given\n"));
4781303Swesolows 		var = SNMP_MALLOC_TYPEDEF(netsnmp_variable_list);
479*11050SRobert.Johnston@Sun.COM 		(void) snmp_set_var_typed_value(var, ASN_UNSIGNED,
480*11050SRobert.Johnston@Sun.COM 		    (uchar_t *)&index, sizeof (index));
4812134Swesolows 		(void) memcpy(tmpoid, reginfo->rootoid,
4821303Swesolows 		    reginfo->rootoid_len * sizeof (oid));
4831303Swesolows 		tmpoid[reginfo->rootoid_len] = 1;	/* Entry is .1 */
4841303Swesolows 		tmpoid[reginfo->rootoid_len + 1] = table_info->colnum;
4851303Swesolows 		if (build_oid(&var->name, &var->name_length, tmpoid,
4862134Swesolows 		    reginfo->rootoid_len + 2, var) != SNMPERR_SUCCESS) {
4872134Swesolows 			snmp_free_varbind(var);
4881303Swesolows 			return (NULL);
4892134Swesolows 		}
4901303Swesolows 		DEBUGMSGTL((MODNAME_STR, "nextmod: built fake index:\n"));
4911303Swesolows 		DEBUGMSGVAR((MODNAME_STR, var));
4921303Swesolows 		DEBUGMSG((MODNAME_STR, "\n"));
4931303Swesolows 	} else {
4942134Swesolows 		var = snmp_clone_varbind(table_info->indexes);
4951303Swesolows 		index = *var->val.integer;
4961303Swesolows 		DEBUGMSGTL((MODNAME_STR, "nextmod: received index:\n"));
4971303Swesolows 		DEBUGMSGVAR((MODNAME_STR, var));
4981303Swesolows 		DEBUGMSG((MODNAME_STR, "\n"));
4991303Swesolows 		index++;
5001303Swesolows 	}
5011303Swesolows 
5022134Swesolows 	snmp_free_varbind(table_info->indexes);
5032134Swesolows 	table_info->indexes = NULL;
5042134Swesolows 	table_info->number_indexes = 0;
5052134Swesolows 
5061303Swesolows 	if ((data = module_lookup_index_nextvalid(index)) == NULL) {
5071303Swesolows 		DEBUGMSGTL((MODNAME_STR, "nextmod: exact match not found for "
5081303Swesolows 		    "index %lu; trying next column\n", index));
5092134Swesolows 		if (table_info->colnum >=
5102134Swesolows 		    netsnmp_find_table_registration_info(reginfo)->max_column) {
5111303Swesolows 			snmp_free_varbind(var);
5121303Swesolows 			DEBUGMSGTL((MODNAME_STR, "nextmod: out of columns\n"));
5131303Swesolows 			return (NULL);
5141303Swesolows 		}
5151303Swesolows 		table_info->colnum++;
5161303Swesolows 		index = 1;
5171303Swesolows 
5181303Swesolows 		data = module_lookup_index_nextvalid(index);
5191303Swesolows 	}
5201303Swesolows 
5211303Swesolows 	if (data == NULL) {
5221303Swesolows 		DEBUGMSGTL((MODNAME_STR, "nextmod: exact match not found for "
5231303Swesolows 		    "index %lu; stopping\n", index));
5241303Swesolows 		snmp_free_varbind(var);
5251303Swesolows 		return (NULL);
5261303Swesolows 	}
5271303Swesolows 
5283955Swesolows 	*var->val.integer = data->d_index;
5291303Swesolows 	table_info->indexes = var;
5302134Swesolows 	table_info->number_indexes = 1;
5311303Swesolows 
5321303Swesolows 	DEBUGMSGTL((MODNAME_STR, "matching data is %lu/%s@%p\n", data->d_index,
5331303Swesolows 	    data->d_ami_name, data));
5341303Swesolows 
5351303Swesolows 	return (data);
5361303Swesolows }
5371303Swesolows 
5381303Swesolows /*ARGSUSED*/
5391303Swesolows static sunFmModule_data_t *
sunFmModuleTable_mod(netsnmp_handler_registration * reginfo,netsnmp_table_request_info * table_info)5401303Swesolows sunFmModuleTable_mod(netsnmp_handler_registration *reginfo,
5411303Swesolows     netsnmp_table_request_info *table_info)
5421303Swesolows {
5431303Swesolows 	ASSERT(table_info->number_indexes == 1);
5441303Swesolows 
5451303Swesolows 	return (module_lookup_index_exact(table_info->index_oid[0]));
5461303Swesolows }
5471303Swesolows 
5482134Swesolows /*ARGSUSED*/
5491303Swesolows static void
sunFmModuleTable_return(unsigned int reg,void * arg)5501303Swesolows sunFmModuleTable_return(unsigned int reg, void *arg)
5511303Swesolows {
5521303Swesolows 	netsnmp_delegated_cache		*cache = (netsnmp_delegated_cache *)arg;
5531303Swesolows 	netsnmp_request_info		*request;
5541303Swesolows 	netsnmp_agent_request_info	*reqinfo;
5551303Swesolows 	netsnmp_handler_registration	*reginfo;
5561303Swesolows 	netsnmp_table_request_info	*table_info;
5571303Swesolows 	sunFmModule_data_t		*data;
5581303Swesolows 	ulong_t				modstate;
5591303Swesolows 
5601303Swesolows 	ASSERT(netsnmp_handler_check_cache(cache) != NULL);
5611303Swesolows 
5621303Swesolows 	(void) pthread_mutex_lock(&update_lock);
5631303Swesolows 	if (update_status != US_QUIET) {
5641303Swesolows 		struct timeval			tv;
5651303Swesolows 
5661303Swesolows 		tv.tv_sec = UPDATE_WAIT_MILLIS / 1000;
5671303Swesolows 		tv.tv_usec = (UPDATE_WAIT_MILLIS % 1000) * 1000;
5681303Swesolows 
5691303Swesolows 		(void) snmp_alarm_register_hr(tv, 0, sunFmModuleTable_return,
5701303Swesolows 		    cache);
5711303Swesolows 		(void) pthread_mutex_unlock(&update_lock);
5721303Swesolows 		return;
5731303Swesolows 	}
5741303Swesolows 
5751303Swesolows 	request = cache->requests;
5761303Swesolows 	reqinfo = cache->reqinfo;
5771303Swesolows 	reginfo = cache->reginfo;
5781303Swesolows 
5791303Swesolows 	table_info = netsnmp_extract_table_info(request);
5801303Swesolows 	request->delegated = 0;
5811303Swesolows 
5821303Swesolows 	ASSERT(table_info->colnum >= SUNFMMODULE_COLMIN);
5831303Swesolows 	ASSERT(table_info->colnum <= SUNFMMODULE_COLMAX);
5841303Swesolows 
5851303Swesolows 	/*
5861303Swesolows 	 * table_info->colnum contains the column number requested.
5871303Swesolows 	 * table_info->indexes contains a linked list of snmp variable
5881303Swesolows 	 * bindings for the indexes of the table.  Values in the list
5891303Swesolows 	 * have been set corresponding to the indexes of the
5901303Swesolows 	 * request.  We have other guarantees as well:
5911303Swesolows 	 *
5921303Swesolows 	 * - The column number is always within range.
5931303Swesolows 	 * - If we have no index data, table_info->index_oid_len is 0.
5941303Swesolows 	 * - We will never receive requests outside our table nor
5951303Swesolows 	 *   those with the first subid anything other than 1 (Entry)
5961303Swesolows 	 *   nor those without a column number.  This is true even
5971303Swesolows 	 *   for GETNEXT requests.
5981303Swesolows 	 */
5991303Swesolows 
6001303Swesolows 	switch (reqinfo->mode) {
6011303Swesolows 	case MODE_GET:
6021303Swesolows 		if ((data = sunFmModuleTable_mod(reginfo, table_info)) ==
6031303Swesolows 		    NULL) {
6041303Swesolows 			netsnmp_free_delegated_cache(cache);
6051303Swesolows 			(void) pthread_mutex_unlock(&update_lock);
6061303Swesolows 			return;
6071303Swesolows 		}
6081303Swesolows 		break;
6091303Swesolows 	case MODE_GETNEXT:
6101303Swesolows 	case MODE_GETBULK:
6111303Swesolows 		if ((data = sunFmModuleTable_nextmod(reginfo, table_info)) ==
6121303Swesolows 		    NULL) {
6131303Swesolows 			netsnmp_free_delegated_cache(cache);
6141303Swesolows 			(void) pthread_mutex_unlock(&update_lock);
6151303Swesolows 			return;
6161303Swesolows 		}
6171303Swesolows 		break;
6181303Swesolows 	default:
619*11050SRobert.Johnston@Sun.COM 		(void) snmp_log(LOG_ERR, MODNAME_STR ": Unsupported request "
6201303Swesolows 		    "mode %d\n", reqinfo->mode);
6211303Swesolows 		netsnmp_free_delegated_cache(cache);
6221303Swesolows 		(void) pthread_mutex_unlock(&update_lock);
6231303Swesolows 		return;
6241303Swesolows 	}
6251303Swesolows 
6261303Swesolows 	switch (table_info->colnum) {
6271303Swesolows 	case SUNFMMODULE_COL_NAME:
628*11050SRobert.Johnston@Sun.COM 		(void) netsnmp_table_build_result(reginfo, request, table_info,
6291303Swesolows 		    ASN_OCTET_STR, (uchar_t *)data->d_ami_name,
6301303Swesolows 		    strlen(data->d_ami_name));
6311303Swesolows 		break;
6321303Swesolows 	case SUNFMMODULE_COL_VERSION:
633*11050SRobert.Johnston@Sun.COM 		(void) netsnmp_table_build_result(reginfo, request, table_info,
6341303Swesolows 		    ASN_OCTET_STR, (uchar_t *)data->d_ami_vers,
6351303Swesolows 		    strlen(data->d_ami_vers));
6361303Swesolows 		break;
6371303Swesolows 	case SUNFMMODULE_COL_STATUS:
6381303Swesolows 		modstate = (data->d_ami_flags & FMD_ADM_MOD_FAILED) ?
6391303Swesolows 		    SUNFMMODULE_STATE_FAILED : SUNFMMODULE_STATE_ACTIVE;
640*11050SRobert.Johnston@Sun.COM 		(void) netsnmp_table_build_result(reginfo, request, table_info,
6411303Swesolows 		    ASN_INTEGER, (uchar_t *)&modstate,
6421303Swesolows 		    sizeof (modstate));
6431303Swesolows 		break;
6441303Swesolows 	case SUNFMMODULE_COL_DESCRIPTION:
645*11050SRobert.Johnston@Sun.COM 		(void) netsnmp_table_build_result(reginfo, request, table_info,
6461303Swesolows 		    ASN_OCTET_STR, (uchar_t *)data->d_ami_desc,
6471303Swesolows 		    strlen(data->d_ami_desc));
6481303Swesolows 		break;
6491303Swesolows 	default:
6501303Swesolows 		break;
6511303Swesolows 	}
6521303Swesolows 	netsnmp_free_delegated_cache(cache);
6531303Swesolows 	(void) pthread_mutex_unlock(&update_lock);
6541303Swesolows }
6551303Swesolows 
6561303Swesolows static int
sunFmModuleTable_handler(netsnmp_mib_handler * handler,netsnmp_handler_registration * reginfo,netsnmp_agent_request_info * reqinfo,netsnmp_request_info * requests)6571303Swesolows sunFmModuleTable_handler(netsnmp_mib_handler *handler,
6581303Swesolows     netsnmp_handler_registration *reginfo, netsnmp_agent_request_info *reqinfo,
6591303Swesolows     netsnmp_request_info *requests)
6601303Swesolows {
6611303Swesolows 	netsnmp_request_info		*request;
6621303Swesolows 	struct timeval			tv;
6631303Swesolows 
6641303Swesolows 	tv.tv_sec = UPDATE_WAIT_MILLIS / 1000;
6651303Swesolows 	tv.tv_usec = (UPDATE_WAIT_MILLIS % 1000) * 1000;
6661303Swesolows 
6671303Swesolows 	request_update();
6681303Swesolows 
6691303Swesolows 	for (request = requests; request; request = request->next) {
6701303Swesolows 		if (request->processed != 0)
6711303Swesolows 			continue;
6721303Swesolows 
6731303Swesolows 		if (netsnmp_extract_table_info(request) == NULL)
6741303Swesolows 			continue;
6751303Swesolows 
6761303Swesolows 		request->delegated = 1;
6771303Swesolows 		(void) snmp_alarm_register_hr(tv, 0, sunFmModuleTable_return,
6781303Swesolows 		    (void *) netsnmp_create_delegated_cache(handler, reginfo,
6791303Swesolows 		    reqinfo, request, NULL));
6801303Swesolows 	}
6811303Swesolows 
6821303Swesolows 	return (SNMP_ERR_NOERROR);
6831303Swesolows }
684