2N/A/*
2N/A * CDDL HEADER START
2N/A *
2N/A * The contents of this file are subject to the terms of the
2N/A * Common Development and Distribution License (the "License").
2N/A * You may not use this file except in compliance with the License.
2N/A *
2N/A * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
2N/A * or http://www.opensolaris.org/os/licensing.
2N/A * See the License for the specific language governing permissions
2N/A * and limitations under the License.
2N/A *
2N/A * When distributing Covered Code, include this CDDL HEADER in each
2N/A * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
2N/A * If applicable, add the following below this CDDL HEADER, with the
2N/A * fields enclosed by brackets "[]" replaced with your own identifying
2N/A * information: Portions Copyright [yyyy] [name of copyright owner]
2N/A *
2N/A * CDDL HEADER END
2N/A */
2N/A
2N/A/*
2N/A * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
2N/A * Use is subject to license terms.
2N/A */
2N/A
2N/A#include <fm/fmd_adm.h>
2N/A#include <fm/fmd_snmp.h>
2N/A#include <net-snmp/net-snmp-config.h>
2N/A#include <net-snmp/net-snmp-includes.h>
2N/A#include <net-snmp/agent/net-snmp-agent-includes.h>
2N/A#include <pthread.h>
2N/A#include <stddef.h>
2N/A#include <errno.h>
2N/A#include <libuutil.h>
2N/A#include "sunFM_impl.h"
2N/A#include "resource.h"
2N/A
2N/Astatic uu_avl_pool_t *rsrc_fmri_avl_pool;
2N/Astatic uu_avl_pool_t *rsrc_index_avl_pool;
2N/Astatic uu_avl_t *rsrc_fmri_avl;
2N/Astatic uu_avl_t *rsrc_index_avl;
2N/A
2N/A#define VALID_AVL_STATE (rsrc_fmri_avl_pool != NULL && \
2N/A rsrc_index_avl_pool != NULL && rsrc_fmri_avl != NULL && \
2N/A rsrc_index_avl != NULL)
2N/A
2N/A#define UPDATE_WAIT_MILLIS 10 /* poll interval in milliseconds */
2N/A
2N/A/*
2N/A * Update types: single-index and all are mutually exclusive; a count
2N/A * update is optional.
2N/A */
2N/A#define UCT_INDEX 0x1
2N/A#define UCT_ALL 0x2
2N/A#define UCT_COUNT 0x4
2N/A#define UCT_FLAGS 0x7
2N/A
2N/A#define RESOURCE_DATA_VALID(d) ((d)->d_valid == valid_stamp)
2N/A
2N/A/*
2N/A * Locking strategy is described in module.c.
2N/A */
2N/Astatic ulong_t max_index;
2N/Astatic int valid_stamp;
2N/Astatic uint32_t rsrc_count;
2N/Astatic pthread_mutex_t update_lock;
2N/Astatic pthread_cond_t update_cv;
2N/Astatic volatile enum { US_QUIET, US_NEEDED, US_INPROGRESS } update_status;
2N/A
2N/Astatic Netsnmp_Node_Handler sunFmResourceTable_handler;
2N/Astatic Netsnmp_Node_Handler sunFmResourceCount_handler;
2N/A
2N/Astatic sunFmResource_data_t *
2N/Akey_build(const char *fmri, const ulong_t index)
2N/A{
2N/A static sunFmResource_data_t key;
2N/A
2N/A key.d_index = index;
2N/A if (fmri)
2N/A (void) strlcpy(key.d_ari_fmri, fmri, sizeof (key.d_ari_fmri));
2N/A else
2N/A key.d_ari_fmri[0] = '\0';
2N/A
2N/A return (&key);
2N/A}
2N/A
2N/A/*
2N/A * If fmri is the fmri of a resource we have previously seen and indexed, return
2N/A * data for it. Otherwise, return NULL. Note that the resource may not be
2N/A * valid; that is, it may have been removed from the fault manager since its
2N/A * information was last updated.
2N/A */
2N/Astatic sunFmResource_data_t *
2N/Aresource_lookup_fmri(const char *fmri)
2N/A{
2N/A sunFmResource_data_t *key;
2N/A
2N/A key = key_build(fmri, 0);
2N/A return (uu_avl_find(rsrc_fmri_avl, key, NULL, NULL));
2N/A}
2N/A
2N/A/*
2N/A * If index corresponds to a resource we have previously seen and indexed,
2N/A * return data for it. Otherwise, return NULL. Note that the resource may
2N/A * not be valid; that is, it may have been expired from the fault manager
2N/A * since its information was last updated.
2N/A */
2N/Astatic sunFmResource_data_t *
2N/Aresource_lookup_index_exact(const ulong_t index)
2N/A{
2N/A sunFmResource_data_t *key;
2N/A
2N/A key = key_build(NULL, index);
2N/A return (uu_avl_find(rsrc_index_avl, key, NULL, NULL));
2N/A}
2N/A
2N/A/*
2N/A * If index corresponds to a valid (that is, extant as of latest information
2N/A * from the fault manager) resource, return the data for that resource.
2N/A * Otherwise, return the data for the valid resource whose index is as close as
2N/A * possible to index but not lower. This preserves the lexicographical
2N/A * ordering required for GETNEXT processing.
2N/A */
2N/Astatic sunFmResource_data_t *
2N/Aresource_lookup_index_nextvalid(const ulong_t index)
2N/A{
2N/A sunFmResource_data_t *key, *data;
2N/A uu_avl_index_t idx;
2N/A
2N/A key = key_build(NULL, index);
2N/A
2N/A if ((data = uu_avl_find(rsrc_index_avl, key, NULL, &idx)) != NULL &&
2N/A RESOURCE_DATA_VALID(data))
2N/A return (data);
2N/A
2N/A data = uu_avl_nearest_next(rsrc_index_avl, idx);
2N/A
2N/A while (data != NULL && !RESOURCE_DATA_VALID(data))
2N/A data = uu_avl_next(rsrc_index_avl, data);
2N/A
2N/A return (data);
2N/A}
2N/A
2N/A/*
2N/A * Possible update the contents of a single resource within the cache. This
2N/A * is our callback from fmd_rsrc_iter.
2N/A */
2N/Astatic int
2N/Arsrcinfo_update_one(const fmd_adm_rsrcinfo_t *rsrcinfo, void *arg)
2N/A{
2N/A const sunFmResource_update_ctx_t *update_ctx =
2N/A (sunFmResource_update_ctx_t *)arg;
2N/A sunFmResource_data_t *data = resource_lookup_fmri(rsrcinfo->ari_fmri);
2N/A
2N/A ++rsrc_count;
2N/A
2N/A /*
2N/A * A resource we haven't seen before. We're obligated to index
2N/A * it and link it into our cache so that we can find it, but we're
2N/A * not obligated to fill it in completely unless we're doing a
2N/A * full update or this is the resource we were asked for. This
2N/A * avoids unnecessary iteration and memory manipulation for data
2N/A * we're not going to return for this request.
2N/A */
2N/A if (data == NULL) {
2N/A uu_avl_index_t idx;
2N/A
2N/A DEBUGMSGTL((MODNAME_STR, "found new resource %s\n",
2N/A rsrcinfo->ari_fmri));
2N/A if ((data = SNMP_MALLOC_TYPEDEF(sunFmResource_data_t)) ==
2N/A NULL) {
2N/A (void) snmp_log(LOG_ERR, MODNAME_STR ": Out of memory "
2N/A "for new resource data at %s:%d\n", __FILE__,
2N/A __LINE__);
2N/A return (1);
2N/A }
2N/A /*
2N/A * We allocate indices sequentially and never reuse them.
2N/A * This ensures we can always return valid GETNEXT responses
2N/A * without having to reindex, and it provides the user a
2N/A * more consistent view of the fault manager.
2N/A */
2N/A data->d_index = ++max_index;
2N/A DEBUGMSGTL((MODNAME_STR, "index %lu is %s@%p\n", data->d_index,
2N/A rsrcinfo->ari_fmri, data));
2N/A
2N/A (void) strlcpy(data->d_ari_fmri, rsrcinfo->ari_fmri,
2N/A sizeof (data->d_ari_fmri));
2N/A
2N/A uu_avl_node_init(data, &data->d_fmri_avl, rsrc_fmri_avl_pool);
2N/A (void) uu_avl_find(rsrc_fmri_avl, data, NULL, &idx);
2N/A uu_avl_insert(rsrc_fmri_avl, data, idx);
2N/A
2N/A uu_avl_node_init(data, &data->d_index_avl, rsrc_index_avl_pool);
2N/A (void) uu_avl_find(rsrc_index_avl, data, NULL, &idx);
2N/A uu_avl_insert(rsrc_index_avl, data, idx);
2N/A
2N/A DEBUGMSGTL((MODNAME_STR, "completed new resource %lu/%s@%p\n",
2N/A data->d_index, data->d_ari_fmri, data));
2N/A }
2N/A
2N/A data->d_valid = valid_stamp;
2N/A
2N/A DEBUGMSGTL((MODNAME_STR, "timestamp updated for %lu/%s@%p: %lu\n",
2N/A data->d_index, data->d_ari_fmri, data, data->d_valid));
2N/A
2N/A if ((update_ctx->uc_type & UCT_ALL) ||
2N/A update_ctx->uc_index == data->d_index) {
2N/A (void) strlcpy(data->d_ari_case, rsrcinfo->ari_case,
2N/A sizeof (data->d_ari_case));
2N/A data->d_ari_flags = rsrcinfo->ari_flags;
2N/A }
2N/A
2N/A return (!(update_ctx->uc_type & UCT_ALL) &&
2N/A update_ctx->uc_index == data->d_index);
2N/A}
2N/A
2N/A/*
2N/A * Update some or all resource data from fmd. If type includes UCT_ALL, all
2N/A * resources will be indexed and their data cached. If type includes
2N/A * UCT_INDEX, updates will stop once the resource matching index has been
2N/A * updated. If UCT_COUNT is set, the number of faulted resources will be
2N/A * set.
2N/A *
2N/A * Returns appropriate SNMP error codes.
2N/A */
2N/Astatic int
2N/Arsrcinfo_update(sunFmResource_update_ctx_t *update_ctx)
2N/A{
2N/A fmd_adm_t *adm;
2N/A int err;
2N/A
2N/A ASSERT(update_ctx != NULL);
2N/A ASSERT((update_ctx->uc_type & (UCT_ALL|UCT_INDEX)) !=
2N/A (UCT_ALL|UCT_INDEX));
2N/A ASSERT((update_ctx->uc_type & ~UCT_FLAGS) == 0);
2N/A ASSERT(VALID_AVL_STATE);
2N/A
2N/A if ((adm = fmd_adm_open(update_ctx->uc_host, update_ctx->uc_prog,
2N/A update_ctx->uc_version)) == NULL) {
2N/A (void) snmp_log(LOG_ERR, MODNAME_STR ": Communication with fmd "
2N/A "failed: %s\n", strerror(errno));
2N/A return (SNMP_ERR_RESOURCEUNAVAILABLE);
2N/A }
2N/A
2N/A if (update_ctx->uc_type == UCT_COUNT) {
2N/A err = fmd_adm_rsrc_count(adm, update_ctx->uc_all, &rsrc_count);
2N/A } else {
2N/A ++valid_stamp;
2N/A rsrc_count = 0;
2N/A err = fmd_adm_rsrc_iter(adm, update_ctx->uc_all,
2N/A rsrcinfo_update_one, update_ctx);
2N/A DEBUGMSGTL((MODNAME_STR, "resource iteration completed\n"));
2N/A }
2N/A
2N/A fmd_adm_close(adm);
2N/A
2N/A if (err != 0) {
2N/A (void) snmp_log(LOG_ERR, MODNAME_STR ": fmd resource "
2N/A "information update failed: %s\n", fmd_adm_errmsg(adm));
2N/A return (SNMP_ERR_RESOURCEUNAVAILABLE);
2N/A }
2N/A
2N/A return (SNMP_ERR_NOERROR);
2N/A}
2N/A
2N/A/*ARGSUSED*/
2N/Astatic void
2N/Aupdate_thread(void *arg)
2N/A{
2N/A /*
2N/A * The current rsrcinfo_update implementation offers minimal savings
2N/A * for the use of index-only updates; therefore we always do a full
2N/A * update. If it becomes advantageous to limit updates to a single
2N/A * index, the contexts can be queued by the handler instead.
2N/A */
2N/A sunFmResource_update_ctx_t uc;
2N/A
2N/A uc.uc_host = NULL;
2N/A uc.uc_prog = FMD_ADM_PROGRAM;
2N/A uc.uc_version = FMD_ADM_VERSION;
2N/A
2N/A uc.uc_all = 0;
2N/A uc.uc_index = 0;
2N/A uc.uc_type = UCT_ALL;
2N/A
2N/A for (;;) {
2N/A (void) pthread_mutex_lock(&update_lock);
2N/A update_status = US_QUIET;
2N/A while (update_status == US_QUIET)
2N/A (void) pthread_cond_wait(&update_cv, &update_lock);
2N/A update_status = US_INPROGRESS;
2N/A (void) pthread_mutex_unlock(&update_lock);
2N/A (void) rsrcinfo_update(&uc);
2N/A }
2N/A}
2N/A
2N/Astatic void
2N/Arequest_update(void)
2N/A{
2N/A (void) pthread_mutex_lock(&update_lock);
2N/A if (update_status != US_QUIET) {
2N/A (void) pthread_mutex_unlock(&update_lock);
2N/A return;
2N/A }
2N/A update_status = US_NEEDED;
2N/A (void) pthread_cond_signal(&update_cv);
2N/A (void) pthread_mutex_unlock(&update_lock);
2N/A}
2N/A
2N/A/*ARGSUSED*/
2N/Astatic int
2N/Aresource_compare_fmri(const void *l, const void *r, void *private)
2N/A{
2N/A sunFmResource_data_t *l_data = (sunFmResource_data_t *)l;
2N/A sunFmResource_data_t *r_data = (sunFmResource_data_t *)r;
2N/A
2N/A ASSERT(l_data != NULL && r_data != NULL);
2N/A
2N/A return (strcmp(l_data->d_ari_fmri, r_data->d_ari_fmri));
2N/A}
2N/A
2N/A/*ARGSUSED*/
2N/Astatic int
2N/Aresource_compare_index(const void *l, const void *r, void *private)
2N/A{
2N/A sunFmResource_data_t *l_data = (sunFmResource_data_t *)l;
2N/A sunFmResource_data_t *r_data = (sunFmResource_data_t *)r;
2N/A
2N/A ASSERT(l_data != NULL && r_data != NULL);
2N/A
2N/A return (l_data->d_index < r_data->d_index ? -1 :
2N/A l_data->d_index > r_data->d_index ? 1 : 0);
2N/A}
2N/A
2N/Aint
2N/AsunFmResourceTable_init(void)
2N/A{
2N/A static oid sunFmResourceTable_oid[] = { SUNFMRESOURCETABLE_OID };
2N/A static oid sunFmResourceCount_oid[] = { SUNFMRESOURCECOUNT_OID, 0 };
2N/A netsnmp_table_registration_info *table_info;
2N/A netsnmp_handler_registration *handler;
2N/A int err;
2N/A
2N/A if ((err = pthread_mutex_init(&update_lock, NULL)) != 0) {
2N/A (void) snmp_log(LOG_ERR, MODNAME_STR ": mutex_init failure: "
2N/A "%s\n", strerror(err));
2N/A return (MIB_REGISTRATION_FAILED);
2N/A }
2N/A if ((err = pthread_cond_init(&update_cv, NULL)) != 0) {
2N/A (void) snmp_log(LOG_ERR, MODNAME_STR ": cond_init failure: "
2N/A "%s\n", strerror(err));
2N/A return (MIB_REGISTRATION_FAILED);
2N/A }
2N/A
2N/A if ((err = pthread_create(NULL, NULL, (void *(*)(void *))update_thread,
2N/A NULL)) != 0) {
2N/A (void) snmp_log(LOG_ERR, MODNAME_STR ": error creating update "
2N/A "thread: %s\n", strerror(err));
2N/A return (MIB_REGISTRATION_FAILED);
2N/A }
2N/A
2N/A if ((table_info =
2N/A SNMP_MALLOC_TYPEDEF(netsnmp_table_registration_info)) == NULL)
2N/A return (MIB_REGISTRATION_FAILED);
2N/A
2N/A if ((handler = netsnmp_create_handler_registration("sunFmResourceTable",
2N/A sunFmResourceTable_handler, sunFmResourceTable_oid,
2N/A OID_LENGTH(sunFmResourceTable_oid), HANDLER_CAN_RONLY)) == NULL) {
2N/A SNMP_FREE(table_info);
2N/A return (MIB_REGISTRATION_FAILED);
2N/A }
2N/A
2N/A /*
2N/A * The Net-SNMP template uses add_indexes here, but that
2N/A * function is unsafe because it does not check for failure.
2N/A */
2N/A if (netsnmp_table_helper_add_index(table_info, ASN_UNSIGNED) == NULL) {
2N/A SNMP_FREE(table_info);
2N/A SNMP_FREE(handler);
2N/A return (MIB_REGISTRATION_FAILED);
2N/A }
2N/A
2N/A table_info->min_column = SUNFMRESOURCE_COLMIN;
2N/A table_info->max_column = SUNFMRESOURCE_COLMAX;
2N/A
2N/A if ((rsrc_fmri_avl_pool = uu_avl_pool_create("rsrc_fmri",
2N/A sizeof (sunFmResource_data_t),
2N/A offsetof(sunFmResource_data_t, d_fmri_avl), resource_compare_fmri,
2N/A UU_AVL_DEBUG)) == NULL) {
2N/A (void) snmp_log(LOG_ERR, MODNAME_STR ": rsrc_fmri avl pool "
2N/A "creation failed: %s\n", uu_strerror(uu_error()));
2N/A snmp_free_varbind(table_info->indexes);
2N/A SNMP_FREE(table_info);
2N/A SNMP_FREE(handler);
2N/A }
2N/A
2N/A if ((rsrc_fmri_avl = uu_avl_create(rsrc_fmri_avl_pool, NULL,
2N/A UU_AVL_DEBUG)) == NULL) {
2N/A (void) snmp_log(LOG_ERR, MODNAME_STR ": rsrc_fmri avl creation "
2N/A "failed: %s\n", uu_strerror(uu_error()));
2N/A snmp_free_varbind(table_info->indexes);
2N/A SNMP_FREE(table_info);
2N/A SNMP_FREE(handler);
2N/A uu_avl_pool_destroy(rsrc_fmri_avl_pool);
2N/A return (MIB_REGISTRATION_FAILED);
2N/A }
2N/A
2N/A if ((rsrc_index_avl_pool = uu_avl_pool_create("rsrc_index",
2N/A sizeof (sunFmResource_data_t),
2N/A offsetof(sunFmResource_data_t, d_index_avl),
2N/A resource_compare_index, UU_AVL_DEBUG)) == NULL) {
2N/A (void) snmp_log(LOG_ERR, MODNAME_STR ": rsrc_index avl pool "
2N/A "creation failed: %s\n", uu_strerror(uu_error()));
2N/A snmp_free_varbind(table_info->indexes);
2N/A SNMP_FREE(table_info);
2N/A SNMP_FREE(handler);
2N/A uu_avl_destroy(rsrc_fmri_avl);
2N/A uu_avl_pool_destroy(rsrc_fmri_avl_pool);
2N/A }
2N/A
2N/A if ((rsrc_index_avl = uu_avl_create(rsrc_index_avl_pool, NULL,
2N/A UU_AVL_DEBUG)) == NULL) {
2N/A (void) snmp_log(LOG_ERR, MODNAME_STR ": rsrc_index avl "
2N/A "creation failed: %s\n", uu_strerror(uu_error()));
2N/A snmp_free_varbind(table_info->indexes);
2N/A SNMP_FREE(table_info);
2N/A SNMP_FREE(handler);
2N/A uu_avl_destroy(rsrc_fmri_avl);
2N/A uu_avl_pool_destroy(rsrc_fmri_avl_pool);
2N/A uu_avl_pool_destroy(rsrc_index_avl_pool);
2N/A return (MIB_REGISTRATION_FAILED);
2N/A }
2N/A
2N/A if ((err = netsnmp_register_table(handler, table_info)) !=
2N/A MIB_REGISTERED_OK) {
2N/A snmp_free_varbind(table_info->indexes);
2N/A SNMP_FREE(table_info);
2N/A SNMP_FREE(handler);
2N/A uu_avl_destroy(rsrc_fmri_avl);
2N/A uu_avl_pool_destroy(rsrc_fmri_avl_pool);
2N/A uu_avl_destroy(rsrc_index_avl);
2N/A uu_avl_pool_destroy(rsrc_index_avl_pool);
2N/A return (err);
2N/A }
2N/A
2N/A if ((err = netsnmp_register_read_only_instance(
2N/A netsnmp_create_handler_registration("sunFmResourceCount",
2N/A sunFmResourceCount_handler, sunFmResourceCount_oid,
2N/A OID_LENGTH(sunFmResourceCount_oid), HANDLER_CAN_RONLY))) !=
2N/A MIB_REGISTERED_OK) {
2N/A /*
2N/A * There's no way to unregister the table handler, so we
2N/A * can't free any of the data, either.
2N/A */
2N/A return (err);
2N/A }
2N/A
2N/A return (MIB_REGISTERED_OK);
2N/A}
2N/A
2N/A/*
2N/A * These two functions form the core of GET/GETNEXT/GETBULK handling (the
2N/A * only kind we do). They perform two functions:
2N/A *
2N/A * - First, frob the request to set all the index variables to correspond
2N/A * to the value that's going to be returned. For GET, this is a nop;
2N/A * for GETNEXT/GETBULK it always requires some work.
2N/A * - Second, find and return the fmd resource information corresponding to
2N/A * the (possibly updated) indices.
2N/A *
2N/A * These should be as fast as possible; they run in the agent thread.
2N/A */
2N/Astatic sunFmResource_data_t *
2N/AsunFmResourceTable_nextrsrc(netsnmp_handler_registration *reginfo,
2N/A netsnmp_table_request_info *table_info)
2N/A{
2N/A sunFmResource_data_t *data;
2N/A netsnmp_variable_list *var;
2N/A ulong_t index;
2N/A
2N/A /*
2N/A * If we have no index, we must make one.
2N/A */
2N/A if (table_info->number_indexes < 1) {
2N/A oid tmpoid[MAX_OID_LEN];
2N/A index = 1;
2N/A
2N/A DEBUGMSGTL((MODNAME_STR, "nextrsrc: no indexes given\n"));
2N/A var = SNMP_MALLOC_TYPEDEF(netsnmp_variable_list);
2N/A (void) snmp_set_var_typed_value(var, ASN_UNSIGNED,
2N/A (uchar_t *)&index, sizeof (index));
2N/A (void) memcpy(tmpoid, reginfo->rootoid,
2N/A reginfo->rootoid_len * sizeof (oid));
2N/A tmpoid[reginfo->rootoid_len] = 1;
2N/A tmpoid[reginfo->rootoid_len + 1] = table_info->colnum;
2N/A if (build_oid(&var->name, &var->name_length, tmpoid,
2N/A reginfo->rootoid_len + 2, var) != SNMPERR_SUCCESS) {
2N/A snmp_free_varbind(var);
2N/A return (NULL);
2N/A }
2N/A DEBUGMSGTL((MODNAME_STR, "nextrsrc: built fake index:\n"));
2N/A DEBUGMSGVAR((MODNAME_STR, var));
2N/A DEBUGMSG((MODNAME_STR, "\n"));
2N/A } else {
2N/A var = snmp_clone_varbind(table_info->indexes);
2N/A index = *var->val.integer;
2N/A DEBUGMSGTL((MODNAME_STR, "nextrsrc: received index:\n"));
2N/A DEBUGMSGVAR((MODNAME_STR, var));
2N/A DEBUGMSG((MODNAME_STR, "\n"));
2N/A index++;
2N/A }
2N/A
2N/A snmp_free_varbind(table_info->indexes);
2N/A table_info->indexes = NULL;
2N/A table_info->number_indexes = 0;
2N/A
2N/A if ((data = resource_lookup_index_nextvalid(index)) == NULL) {
2N/A DEBUGMSGTL((MODNAME_STR, "nextrsrc: exact match not found for "
2N/A "index %lu; trying next column\n", index));
2N/A if (table_info->colnum >=
2N/A netsnmp_find_table_registration_info(reginfo)->max_column) {
2N/A snmp_free_varbind(var);
2N/A DEBUGMSGTL((MODNAME_STR, "nextrsrc: out of columns\n"));
2N/A return (NULL);
2N/A }
2N/A table_info->colnum++;
2N/A index = 1;
2N/A
2N/A data = resource_lookup_index_nextvalid(index);
2N/A }
2N/A
2N/A if (data == NULL) {
2N/A DEBUGMSGTL((MODNAME_STR, "nextrsrc: exact match not found for "
2N/A "index %lu; stopping\n", index));
2N/A snmp_free_varbind(var);
2N/A return (NULL);
2N/A }
2N/A
2N/A *var->val.integer = data->d_index;
2N/A table_info->indexes = var;
2N/A table_info->number_indexes = 1;
2N/A
2N/A DEBUGMSGTL((MODNAME_STR, "matching data is %lu/%s@%p\n", data->d_index,
2N/A data->d_ari_fmri, data));
2N/A
2N/A return (data);
2N/A}
2N/A
2N/A/*ARGSUSED*/
2N/Astatic sunFmResource_data_t *
2N/AsunFmResourceTable_rsrc(netsnmp_handler_registration *reginfo,
2N/A netsnmp_table_request_info *table_info)
2N/A{
2N/A ASSERT(table_info->number_indexes == 1);
2N/A
2N/A return (resource_lookup_index_exact(table_info->index_oid[0]));
2N/A}
2N/A
2N/A/*ARGSUSED*/
2N/Astatic void
2N/AsunFmResourceTable_return(unsigned int reg, void *arg)
2N/A{
2N/A netsnmp_delegated_cache *cache = (netsnmp_delegated_cache *)arg;
2N/A netsnmp_request_info *request;
2N/A netsnmp_agent_request_info *reqinfo;
2N/A netsnmp_handler_registration *reginfo;
2N/A netsnmp_table_request_info *table_info;
2N/A sunFmResource_data_t *data;
2N/A ulong_t rsrcstate;
2N/A
2N/A ASSERT(netsnmp_handler_check_cache(cache) != NULL);
2N/A
2N/A (void) pthread_mutex_lock(&update_lock);
2N/A if (update_status != US_QUIET) {
2N/A struct timeval tv;
2N/A
2N/A tv.tv_sec = UPDATE_WAIT_MILLIS / 1000;
2N/A tv.tv_usec = (UPDATE_WAIT_MILLIS % 1000) * 1000;
2N/A
2N/A (void) snmp_alarm_register_hr(tv, 0, sunFmResourceTable_return,
2N/A cache);
2N/A (void) pthread_mutex_unlock(&update_lock);
2N/A return;
2N/A }
2N/A
2N/A request = cache->requests;
2N/A reqinfo = cache->reqinfo;
2N/A reginfo = cache->reginfo;
2N/A
2N/A table_info = netsnmp_extract_table_info(request);
2N/A request->delegated = 0;
2N/A
2N/A ASSERT(table_info->colnum >= SUNFMRESOURCE_COLMIN);
2N/A ASSERT(table_info->colnum <= SUNFMRESOURCE_COLMAX);
2N/A
2N/A /*
2N/A * table_info->colnum contains the column number requested.
2N/A * table_info->indexes contains a linked list of snmp variable
2N/A * bindings for the indexes of the table. Values in the list
2N/A * have been set corresponding to the indexes of the
2N/A * request. We have other guarantees as well:
2N/A *
2N/A * - The column number is always within range.
2N/A * - If we have no index data, table_info->index_oid_len is 0.
2N/A * - We will never receive requests outside our table nor
2N/A * those with the first subid anything other than 1 (Entry)
2N/A * nor those without a column number. This is true even
2N/A * for GETNEXT requests.
2N/A */
2N/A
2N/A switch (reqinfo->mode) {
2N/A case MODE_GET:
2N/A if ((data = sunFmResourceTable_rsrc(reginfo, table_info)) ==
2N/A NULL) {
2N/A netsnmp_free_delegated_cache(cache);
2N/A (void) pthread_mutex_unlock(&update_lock);
2N/A return;
2N/A }
2N/A break;
2N/A case MODE_GETNEXT:
2N/A case MODE_GETBULK:
2N/A if ((data = sunFmResourceTable_nextrsrc(reginfo, table_info)) ==
2N/A NULL) {
2N/A netsnmp_free_delegated_cache(cache);
2N/A (void) pthread_mutex_unlock(&update_lock);
2N/A return;
2N/A }
2N/A break;
2N/A default:
2N/A (void) snmp_log(LOG_ERR, MODNAME_STR ": Unsupported request "
2N/A "mode %d\n", reqinfo->mode);
2N/A netsnmp_free_delegated_cache(cache);
2N/A (void) pthread_mutex_unlock(&update_lock);
2N/A return;
2N/A }
2N/A
2N/A switch (table_info->colnum) {
2N/A case SUNFMRESOURCE_COL_FMRI:
2N/A (void) netsnmp_table_build_result(reginfo, request, table_info,
2N/A ASN_OCTET_STR, (uchar_t *)data->d_ari_fmri,
2N/A strlen(data->d_ari_fmri));
2N/A break;
2N/A case SUNFMRESOURCE_COL_STATUS:
2N/A switch (data->d_ari_flags &
2N/A (FMD_ADM_RSRC_FAULTY|FMD_ADM_RSRC_UNUSABLE)) {
2N/A default:
2N/A rsrcstate = SUNFMRESOURCE_STATE_OK;
2N/A break;
2N/A case FMD_ADM_RSRC_FAULTY:
2N/A rsrcstate = SUNFMRESOURCE_STATE_DEGRADED;
2N/A break;
2N/A case FMD_ADM_RSRC_UNUSABLE:
2N/A rsrcstate = SUNFMRESOURCE_STATE_UNKNOWN;
2N/A break;
2N/A case FMD_ADM_RSRC_FAULTY | FMD_ADM_RSRC_UNUSABLE:
2N/A rsrcstate = SUNFMRESOURCE_STATE_FAULTED;
2N/A break;
2N/A }
2N/A (void) netsnmp_table_build_result(reginfo, request, table_info,
2N/A ASN_INTEGER, (uchar_t *)&rsrcstate,
2N/A sizeof (rsrcstate));
2N/A break;
2N/A case SUNFMRESOURCE_COL_DIAGNOSISUUID:
2N/A (void) netsnmp_table_build_result(reginfo, request, table_info,
2N/A ASN_OCTET_STR, (uchar_t *)data->d_ari_case,
2N/A strlen(data->d_ari_case));
2N/A break;
2N/A default:
2N/A break;
2N/A }
2N/A netsnmp_free_delegated_cache(cache);
2N/A (void) pthread_mutex_unlock(&update_lock);
2N/A}
2N/A
2N/Astatic int
2N/AsunFmResourceTable_handler(netsnmp_mib_handler *handler,
2N/A netsnmp_handler_registration *reginfo, netsnmp_agent_request_info *reqinfo,
2N/A netsnmp_request_info *requests)
2N/A{
2N/A netsnmp_request_info *request;
2N/A struct timeval tv;
2N/A
2N/A tv.tv_sec = UPDATE_WAIT_MILLIS / 1000;
2N/A tv.tv_usec = (UPDATE_WAIT_MILLIS % 1000) * 1000;
2N/A
2N/A request_update();
2N/A
2N/A for (request = requests; request; request = request->next) {
2N/A if (request->processed != 0)
2N/A continue;
2N/A
2N/A if (netsnmp_extract_table_info(request) == NULL)
2N/A continue;
2N/A
2N/A request->delegated = 1;
2N/A (void) snmp_alarm_register_hr(tv, 0, sunFmResourceTable_return,
2N/A (void *) netsnmp_create_delegated_cache(handler, reginfo,
2N/A reqinfo, request, NULL));
2N/A }
2N/A
2N/A return (SNMP_ERR_NOERROR);
2N/A}
2N/A
2N/A/*ARGSUSED*/
2N/Astatic void
2N/AsunFmResourceCount_return(unsigned int reg, void *arg)
2N/A{
2N/A netsnmp_delegated_cache *cache = (netsnmp_delegated_cache *)arg;
2N/A netsnmp_request_info *request;
2N/A netsnmp_agent_request_info *reqinfo;
2N/A ulong_t rsrc_count_long;
2N/A
2N/A ASSERT(netsnmp_handler_check_cache(cache) != NULL);
2N/A
2N/A (void) pthread_mutex_lock(&update_lock);
2N/A if (update_status != US_QUIET) {
2N/A struct timeval tv;
2N/A
2N/A tv.tv_sec = UPDATE_WAIT_MILLIS / 1000;
2N/A tv.tv_usec = (UPDATE_WAIT_MILLIS % 1000) * 1000;
2N/A
2N/A (void) snmp_alarm_register_hr(tv, 0, sunFmResourceCount_return,
2N/A cache);
2N/A (void) pthread_mutex_unlock(&update_lock);
2N/A return;
2N/A }
2N/A
2N/A request = cache->requests;
2N/A reqinfo = cache->reqinfo;
2N/A
2N/A request->delegated = 0;
2N/A
2N/A switch (reqinfo->mode) {
2N/A /*
2N/A * According to the documentation, it's not possible for us ever to
2N/A * be called with MODE_GETNEXT. However, Net-SNMP does the following:
2N/A * - set reqinfo->mode to MODE_GET
2N/A * - invoke the handler
2N/A * - set reqinfo->mode to MODE_GETNEXT (even if the request was not
2N/A * actually processed; i.e. it's been delegated)
2N/A * Since we're called back later with the same reqinfo, we see
2N/A * GETNEXT. Therefore this case is needed to work around the
2N/A * Net-SNMP bug.
2N/A */
2N/A case MODE_GET:
2N/A case MODE_GETNEXT:
2N/A DEBUGMSGTL((MODNAME_STR, "resource count is %u\n", rsrc_count));
2N/A rsrc_count_long = (ulong_t)rsrc_count;
2N/A (void) snmp_set_var_typed_value(request->requestvb, ASN_GAUGE,
2N/A (uchar_t *)&rsrc_count_long, sizeof (rsrc_count_long));
2N/A break;
2N/A default:
2N/A (void) snmp_log(LOG_ERR, MODNAME_STR ": Unsupported request "
2N/A "mode %d\n", reqinfo->mode);
2N/A }
2N/A
2N/A netsnmp_free_delegated_cache(cache);
2N/A (void) pthread_mutex_unlock(&update_lock);
2N/A}
2N/A
2N/Astatic int
2N/AsunFmResourceCount_handler(netsnmp_mib_handler *handler,
2N/A netsnmp_handler_registration *reginfo, netsnmp_agent_request_info *reqinfo,
2N/A netsnmp_request_info *requests)
2N/A{
2N/A struct timeval tv;
2N/A
2N/A tv.tv_sec = UPDATE_WAIT_MILLIS / 1000;
2N/A tv.tv_usec = (UPDATE_WAIT_MILLIS % 1000) * 1000;
2N/A
2N/A request_update();
2N/A
2N/A /*
2N/A * We are never called for a GETNEXT when registered as an
2N/A * instance; it's handled for us and converted to a GET.
2N/A * Also, an instance handler is given only one request at a time, so
2N/A * we don't need to loop over a list of requests.
2N/A */
2N/A
2N/A if (requests->processed != 0)
2N/A return (SNMP_ERR_NOERROR);
2N/A
2N/A requests->delegated = 1;
2N/A (void) snmp_alarm_register_hr(tv, 0, sunFmResourceCount_return,
2N/A (void *) netsnmp_create_delegated_cache(handler, reginfo,
2N/A reqinfo, requests, NULL));
2N/A
2N/A return (SNMP_ERR_NOERROR);
2N/A}