xref: /titanic_50/usr/src/lib/fm/libfmd_snmp/common/resource.c (revision 7c4dcc5546f9f002dfc2b95de47c90f00d07c066)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include <fm/fmd_adm.h>
30 #include <fm/fmd_snmp.h>
31 #include <net-snmp/net-snmp-config.h>
32 #include <net-snmp/net-snmp-includes.h>
33 #include <net-snmp/agent/net-snmp-agent-includes.h>
34 #include <pthread.h>
35 #include <stddef.h>
36 #include <errno.h>
37 #include <libuutil.h>
38 #include "sunFM_impl.h"
39 #include "resource.h"
40 
41 static uu_avl_pool_t	*rsrc_fmri_avl_pool;
42 static uu_avl_pool_t	*rsrc_index_avl_pool;
43 static uu_avl_t		*rsrc_fmri_avl;
44 static uu_avl_t		*rsrc_index_avl;
45 
46 #define	VALID_AVL_STATE	(rsrc_fmri_avl_pool != NULL &&		\
47 	rsrc_index_avl_pool != NULL && rsrc_fmri_avl != NULL &&	\
48 	rsrc_index_avl != NULL)
49 
50 #define	UPDATE_WAIT_MILLIS	10	/* poll interval in milliseconds */
51 
52 /*
53  * Update types: single-index and all are mutually exclusive; a count
54  * update is optional.
55  */
56 #define	UCT_INDEX	0x1
57 #define	UCT_ALL		0x2
58 #define	UCT_COUNT	0x4
59 #define	UCT_FLAGS	0x7
60 
61 #define	RESOURCE_DATA_VALID(d)	((d)->d_valid == valid_stamp)
62 
63 /*
64  * Locking strategy is described in module.c.
65  */
66 static ulong_t		max_index;
67 static int		valid_stamp;
68 static uint32_t		rsrc_count;
69 static pthread_mutex_t	update_lock;
70 static pthread_cond_t	update_cv;
71 static volatile enum { US_QUIET, US_NEEDED, US_INPROGRESS } update_status;
72 
73 static Netsnmp_Node_Handler	sunFmResourceTable_handler;
74 static Netsnmp_Node_Handler	sunFmResourceCount_handler;
75 
76 static sunFmResource_data_t *
77 key_build(const char *fmri, const ulong_t index)
78 {
79 	static sunFmResource_data_t	key;
80 
81 	key.d_index = index;
82 	if (fmri)
83 		strlcpy(key.d_ari_fmri, fmri, sizeof (key.d_ari_fmri));
84 	else
85 		key.d_ari_fmri[0] = '\0';
86 
87 	return (&key);
88 }
89 
90 /*
91  * If fmri is the fmri of a resource we have previously seen and indexed, return
92  * data for it.  Otherwise, return NULL.  Note that the resource may not be
93  * valid; that is, it may have been removed from the fault manager since its
94  * information was last updated.
95  */
96 static sunFmResource_data_t *
97 resource_lookup_fmri(const char *fmri)
98 {
99 	sunFmResource_data_t	*key;
100 
101 	key = key_build(fmri, 0);
102 	return (uu_avl_find(rsrc_fmri_avl, key, NULL, NULL));
103 }
104 
105 /*
106  * If index corresponds to a resource we have previously seen and indexed,
107  * return data for it.  Otherwise, return NULL.  Note that the resource may
108  * not be valid; that is, it may have been expired from the fault manager
109  * since its information was last updated.
110  */
111 static sunFmResource_data_t *
112 resource_lookup_index_exact(const ulong_t index)
113 {
114 	sunFmResource_data_t	*key;
115 
116 	key = key_build(NULL, index);
117 	return (uu_avl_find(rsrc_index_avl, key, NULL, NULL));
118 }
119 
120 /*
121  * If index corresponds to a valid (that is, extant as of latest information
122  * from the fault manager) resource, return the data for that resource.
123  * Otherwise, return the data for the valid resource whose index is as close as
124  * possible to index but not lower.  This preserves the lexicographical
125  * ordering required for GETNEXT processing.
126  */
127 static sunFmResource_data_t *
128 resource_lookup_index_nextvalid(const ulong_t index)
129 {
130 	sunFmResource_data_t	*key, *data;
131 	uu_avl_index_t		idx;
132 
133 	key = key_build(NULL, index);
134 
135 	if ((data = uu_avl_find(rsrc_index_avl, key, NULL, &idx)) != NULL &&
136 	    RESOURCE_DATA_VALID(data))
137 			return (data);
138 
139 	data = uu_avl_nearest_next(rsrc_index_avl, idx);
140 
141 	while (data != NULL && !RESOURCE_DATA_VALID(data)) {
142 		(void) uu_avl_find(rsrc_index_avl, data, NULL, &idx);
143 		data = uu_avl_nearest_next(rsrc_index_avl, idx);
144 	}
145 
146 	return (data);
147 }
148 
149 /*
150  * Possible update the contents of a single resource within the cache.  This
151  * is our callback from fmd_rsrc_iter.
152  */
153 static int
154 rsrcinfo_update_one(const fmd_adm_rsrcinfo_t *rsrcinfo, void *arg)
155 {
156 	const sunFmResource_update_ctx_t *update_ctx =
157 	    (sunFmResource_update_ctx_t *)arg;
158 	sunFmResource_data_t *data = resource_lookup_fmri(rsrcinfo->ari_fmri);
159 
160 	++rsrc_count;
161 
162 	/*
163 	 * A resource we haven't seen before.  We're obligated to index
164 	 * it and link it into our cache so that we can find it, but we're
165 	 * not obligated to fill it in completely unless we're doing a
166 	 * full update or this is the resource we were asked for.  This
167 	 * avoids unnecessary iteration and memory manipulation for data
168 	 * we're not going to return for this request.
169 	 */
170 	if (data == NULL) {
171 		uu_avl_index_t idx;
172 
173 		DEBUGMSGTL((MODNAME_STR, "found new resource %s\n",
174 		    rsrcinfo->ari_fmri));
175 		if ((data = SNMP_MALLOC_TYPEDEF(sunFmResource_data_t)) ==
176 		    NULL) {
177 			snmp_log(LOG_ERR, MODNAME_STR ": Out of memory for "
178 			    "new resource data at %s:%d\n", __FILE__, __LINE__);
179 			return (1);
180 		}
181 		/*
182 		 * We allocate indices sequentially and never reuse them.
183 		 * This ensures we can always return valid GETNEXT responses
184 		 * without having to reindex, and it provides the user a
185 		 * more consistent view of the fault manager.
186 		 */
187 		data->d_index = ++max_index;
188 		DEBUGMSGTL((MODNAME_STR, "index %lu is %s@%p\n", data->d_index,
189 		    rsrcinfo->ari_fmri, data));
190 
191 		strlcpy(data->d_ari_fmri, rsrcinfo->ari_fmri,
192 		    sizeof (data->d_ari_fmri));
193 
194 		uu_avl_node_init(data, &data->d_fmri_avl, rsrc_fmri_avl_pool);
195 		(void) uu_avl_find(rsrc_fmri_avl, data, NULL, &idx);
196 		uu_avl_insert(rsrc_fmri_avl, data, idx);
197 
198 		uu_avl_node_init(data, &data->d_index_avl, rsrc_index_avl_pool);
199 		(void) uu_avl_find(rsrc_index_avl, data, NULL, &idx);
200 		uu_avl_insert(rsrc_index_avl, data, idx);
201 
202 		DEBUGMSGTL((MODNAME_STR, "completed new resource %lu/%s@%p\n",
203 		    data->d_index, data->d_ari_fmri, data));
204 	}
205 
206 	data->d_valid = valid_stamp;
207 
208 	DEBUGMSGTL((MODNAME_STR, "timestamp updated for %lu/%s@%p: %lu\n",
209 	    data->d_index, data->d_ari_fmri, data, data->d_valid));
210 
211 	if ((update_ctx->uc_type & UCT_ALL) ||
212 	    update_ctx->uc_index == data->d_index) {
213 		strlcpy(data->d_ari_case, rsrcinfo->ari_case,
214 		    sizeof (data->d_ari_case));
215 		data->d_ari_flags = rsrcinfo->ari_flags;
216 	}
217 
218 	return (!(update_ctx->uc_type & UCT_ALL) &&
219 	    update_ctx->uc_index == data->d_index);
220 }
221 
222 /*
223  * Update some or all resource data from fmd.  If type includes UCT_ALL, all
224  * resources will be indexed and their data cached.  If type includes
225  * UCT_INDEX, updates will stop once the resource matching index has been
226  * updated.  If UCT_COUNT is set, the number of faulted resources will be
227  * set.
228  *
229  * Returns appropriate SNMP error codes.
230  */
231 static int
232 rsrcinfo_update(sunFmResource_update_ctx_t *update_ctx)
233 {
234 	fmd_adm_t *adm;
235 	int err;
236 
237 	ASSERT(update_ctx != NULL);
238 	ASSERT((update_ctx->uc_type & (UCT_ALL|UCT_INDEX)) !=
239 	    (UCT_ALL|UCT_INDEX));
240 	ASSERT((update_ctx->uc_type & ~UCT_FLAGS) == 0);
241 	ASSERT(VALID_AVL_STATE);
242 
243 	if ((adm = fmd_adm_open(update_ctx->uc_host, update_ctx->uc_prog,
244 	    update_ctx->uc_version)) == NULL) {
245 		snmp_log(LOG_ERR, MODNAME_STR ": Communication with fmd "
246 		    "failed: %s\n", strerror(errno));
247 		return (SNMP_ERR_RESOURCEUNAVAILABLE);
248 	}
249 
250 	if (update_ctx->uc_type == UCT_COUNT) {
251 		err = fmd_adm_rsrc_count(adm, update_ctx->uc_all, &rsrc_count);
252 	} else {
253 		++valid_stamp;
254 		rsrc_count = 0;
255 		err = fmd_adm_rsrc_iter(adm, update_ctx->uc_all,
256 		    rsrcinfo_update_one, update_ctx);
257 		DEBUGMSGTL((MODNAME_STR, "resource iteration completed\n"));
258 	}
259 
260 	fmd_adm_close(adm);
261 
262 	if (err != 0) {
263 		snmp_log(LOG_ERR, MODNAME_STR ": fmd resource information "
264 		    "update failed: %s\n", fmd_adm_errmsg(adm));
265 		return (SNMP_ERR_RESOURCEUNAVAILABLE);
266 	}
267 
268 	return (SNMP_ERR_NOERROR);
269 }
270 
271 /*ARGSUSED*/
272 static void
273 update_thread(void *arg)
274 {
275 	/*
276 	 * The current rsrcinfo_update implementation offers minimal savings
277 	 * for the use of index-only updates; therefore we always do a full
278 	 * update.  If it becomes advantageous to limit updates to a single
279 	 * index, the contexts can be queued by the handler instead.
280 	 */
281 	sunFmResource_update_ctx_t	uc;
282 
283 	uc.uc_host = NULL;
284 	uc.uc_prog = FMD_ADM_PROGRAM;
285 	uc.uc_version = FMD_ADM_VERSION;
286 
287 	uc.uc_index = 0;
288 	uc.uc_type = UCT_ALL;
289 
290 	for (;;) {
291 		(void) pthread_mutex_lock(&update_lock);
292 		update_status = US_QUIET;
293 		while (update_status == US_QUIET)
294 			(void) pthread_cond_wait(&update_cv, &update_lock);
295 		update_status = US_INPROGRESS;
296 		(void) pthread_mutex_unlock(&update_lock);
297 		(void) rsrcinfo_update(&uc);
298 	}
299 }
300 
301 static void
302 request_update(void)
303 {
304 	(void) pthread_mutex_lock(&update_lock);
305 	if (update_status != US_QUIET) {
306 		(void) pthread_mutex_unlock(&update_lock);
307 		return;
308 	}
309 	update_status = US_NEEDED;
310 	(void) pthread_cond_signal(&update_cv);
311 	(void) pthread_mutex_unlock(&update_lock);
312 }
313 
314 /*ARGSUSED*/
315 static int
316 resource_compare_fmri(const void *l, const void *r, void *private)
317 {
318 	sunFmResource_data_t	*l_data = (sunFmResource_data_t *)l;
319 	sunFmResource_data_t	*r_data = (sunFmResource_data_t *)r;
320 
321 	ASSERT(l_data != NULL && r_data != NULL);
322 
323 	return (strcmp(l_data->d_ari_fmri, r_data->d_ari_fmri));
324 }
325 
326 /*ARGSUSED*/
327 static int
328 resource_compare_index(const void *l, const void *r, void *private)
329 {
330 	sunFmResource_data_t	*l_data = (sunFmResource_data_t *)l;
331 	sunFmResource_data_t	*r_data = (sunFmResource_data_t *)r;
332 
333 	ASSERT(l_data != NULL && r_data != NULL);
334 
335 	return (l_data->d_index < r_data->d_index ? -1 :
336 	    l_data->d_index > r_data->d_index ? 1 : 0);
337 }
338 
339 int
340 sunFmResourceTable_init(void)
341 {
342 	static oid sunFmResourceTable_oid[] = { SUNFMRESOURCETABLE_OID };
343 	static oid sunFmResourceCount_oid[] = { SUNFMRESOURCECOUNT_OID, 0 };
344 	netsnmp_table_registration_info *table_info;
345 	netsnmp_handler_registration *handler;
346 	int err;
347 
348 	if ((err = pthread_mutex_init(&update_lock, NULL)) != 0) {
349 		snmp_log(LOG_ERR, MODNAME_STR ": mutex_init failure: %s\n",
350 		    strerror(err));
351 		return (MIB_REGISTRATION_FAILED);
352 	}
353 	if ((err = pthread_cond_init(&update_cv, NULL)) != 0) {
354 		snmp_log(LOG_ERR, MODNAME_STR ": cond_init failure: %s\n",
355 		    strerror(err));
356 		return (MIB_REGISTRATION_FAILED);
357 	}
358 
359 	if ((err = pthread_create(NULL, NULL, (void *(*)(void *))update_thread,
360 	    NULL)) != 0) {
361 		snmp_log(LOG_ERR, MODNAME_STR ": error creating update "
362 		    "thread: %s\n", strerror(err));
363 		return (MIB_REGISTRATION_FAILED);
364 	}
365 
366 	if ((table_info =
367 	    SNMP_MALLOC_TYPEDEF(netsnmp_table_registration_info)) == NULL)
368 		return (MIB_REGISTRATION_FAILED);
369 
370 	if ((handler = netsnmp_create_handler_registration("sunFmResourceTable",
371 	    sunFmResourceTable_handler, sunFmResourceTable_oid,
372 	    OID_LENGTH(sunFmResourceTable_oid), HANDLER_CAN_RONLY)) == NULL) {
373 		SNMP_FREE(table_info);
374 		return (MIB_REGISTRATION_FAILED);
375 	}
376 
377 	/*
378 	 * The Net-SNMP template uses add_indexes here, but that
379 	 * function is unsafe because it does not check for failure.
380 	 */
381 	if (netsnmp_table_helper_add_index(table_info, ASN_UNSIGNED) == NULL) {
382 		SNMP_FREE(table_info);
383 		SNMP_FREE(handler);
384 		return (MIB_REGISTRATION_FAILED);
385 	}
386 
387 	table_info->min_column = SUNFMRESOURCE_COLMIN;
388 	table_info->max_column = SUNFMRESOURCE_COLMAX;
389 
390 	if ((rsrc_fmri_avl_pool = uu_avl_pool_create("rsrc_fmri",
391 	    sizeof (sunFmResource_data_t),
392 	    offsetof(sunFmResource_data_t, d_fmri_avl), resource_compare_fmri,
393 	    UU_AVL_DEBUG)) == NULL) {
394 		snmp_log(LOG_ERR, MODNAME_STR ": rsrc_fmri avl pool creation "
395 		    "failed: %s\n", uu_strerror(uu_error()));
396 		snmp_free_varbind(table_info->indexes);
397 		SNMP_FREE(table_info);
398 		SNMP_FREE(handler);
399 	}
400 
401 	if ((rsrc_fmri_avl = uu_avl_create(rsrc_fmri_avl_pool, NULL,
402 	    UU_AVL_DEBUG)) == NULL) {
403 		snmp_log(LOG_ERR, MODNAME_STR ": rsrc_fmri avl creation "
404 		    "failed: %s\n", uu_strerror(uu_error()));
405 		snmp_free_varbind(table_info->indexes);
406 		SNMP_FREE(table_info);
407 		SNMP_FREE(handler);
408 		uu_avl_pool_destroy(rsrc_fmri_avl_pool);
409 		return (MIB_REGISTRATION_FAILED);
410 	}
411 
412 	if ((rsrc_index_avl_pool = uu_avl_pool_create("rsrc_index",
413 	    sizeof (sunFmResource_data_t),
414 	    offsetof(sunFmResource_data_t, d_index_avl),
415 	    resource_compare_index, UU_AVL_DEBUG)) == NULL) {
416 		snmp_log(LOG_ERR, MODNAME_STR ": rsrc_index avl pool creation "
417 		    "failed: %s\n", uu_strerror(uu_error()));
418 		snmp_free_varbind(table_info->indexes);
419 		SNMP_FREE(table_info);
420 		SNMP_FREE(handler);
421 		uu_avl_destroy(rsrc_fmri_avl);
422 		uu_avl_pool_destroy(rsrc_fmri_avl_pool);
423 	}
424 
425 	if ((rsrc_index_avl = uu_avl_create(rsrc_index_avl_pool, NULL,
426 	    UU_AVL_DEBUG)) == NULL) {
427 		snmp_log(LOG_ERR, MODNAME_STR ": rsrc_index avl creation "
428 		    "failed: %s\n", uu_strerror(uu_error()));
429 		snmp_free_varbind(table_info->indexes);
430 		SNMP_FREE(table_info);
431 		SNMP_FREE(handler);
432 		uu_avl_destroy(rsrc_fmri_avl);
433 		uu_avl_pool_destroy(rsrc_fmri_avl_pool);
434 		uu_avl_pool_destroy(rsrc_index_avl_pool);
435 		return (MIB_REGISTRATION_FAILED);
436 	}
437 
438 	if ((err = netsnmp_register_table(handler, table_info)) !=
439 	    MIB_REGISTERED_OK) {
440 		snmp_free_varbind(table_info->indexes);
441 		SNMP_FREE(table_info);
442 		SNMP_FREE(handler);
443 		uu_avl_destroy(rsrc_fmri_avl);
444 		uu_avl_pool_destroy(rsrc_fmri_avl_pool);
445 		uu_avl_destroy(rsrc_index_avl);
446 		uu_avl_pool_destroy(rsrc_index_avl_pool);
447 		return (err);
448 	}
449 
450 	if ((err = netsnmp_register_read_only_instance(
451 	    netsnmp_create_handler_registration("sunFmResourceCount",
452 		sunFmResourceCount_handler, sunFmResourceCount_oid,
453 		OID_LENGTH(sunFmResourceCount_oid), HANDLER_CAN_RONLY))) !=
454 	    MIB_REGISTERED_OK) {
455 		/*
456 		 * There's no way to unregister the table handler, so we
457 		 * can't free any of the data, either.
458 		 */
459 		return (err);
460 	}
461 
462 	return (MIB_REGISTERED_OK);
463 }
464 
465 /*
466  * These two functions form the core of GET/GETNEXT/GETBULK handling (the
467  * only kind we do).  They perform two functions:
468  *
469  * - First, frob the request to set all the index variables to correspond
470  *   to the value that's going to be returned.  For GET, this is a nop;
471  *   for GETNEXT/GETBULK it always requires some work.
472  * - Second, find and return the fmd resource information corresponding to
473  *   the (possibly updated) indices.
474  *
475  * These should be as fast as possible; they run in the agent thread.
476  */
477 static sunFmResource_data_t *
478 sunFmResourceTable_nextrsrc(netsnmp_handler_registration *reginfo,
479     netsnmp_table_request_info *table_info)
480 {
481 	sunFmResource_data_t	*data;
482 	netsnmp_variable_list	*var;
483 	ulong_t index;
484 
485 	/*
486 	 * If we have no index, we must make one.
487 	 */
488 	if (table_info->number_indexes < 1) {
489 		oid tmpoid[MAX_OID_LEN];
490 		index = 1;
491 
492 		DEBUGMSGTL((MODNAME_STR, "nextrsrc: no indexes given\n"));
493 		var = SNMP_MALLOC_TYPEDEF(netsnmp_variable_list);
494 		snmp_set_var_typed_value(var, ASN_UNSIGNED, (uchar_t *)&index,
495 		    sizeof (index));
496 		memcpy(tmpoid, reginfo->rootoid,
497 		    reginfo->rootoid_len * sizeof (oid));
498 		tmpoid[reginfo->rootoid_len] = 1;
499 		tmpoid[reginfo->rootoid_len + 1] = table_info->colnum;
500 		if (build_oid(&var->name, &var->name_length, tmpoid,
501 		    reginfo->rootoid_len + 2, var) != SNMPERR_SUCCESS) {
502 			snmp_free_varbind(var);
503 			return (NULL);
504 		}
505 		DEBUGMSGTL((MODNAME_STR, "nextrsrc: built fake index:\n"));
506 		DEBUGMSGVAR((MODNAME_STR, var));
507 		DEBUGMSG((MODNAME_STR, "\n"));
508 	} else {
509 		var = table_info->indexes;
510 		index = *var->val.integer;
511 		DEBUGMSGTL((MODNAME_STR, "nextrsrc: received index:\n"));
512 		DEBUGMSGVAR((MODNAME_STR, var));
513 		DEBUGMSG((MODNAME_STR, "\n"));
514 		index++;
515 	}
516 
517 	if ((data = resource_lookup_index_nextvalid(index)) == NULL) {
518 		DEBUGMSGTL((MODNAME_STR, "nextrsrc: exact match not found for "
519 		    "index %lu; trying next column\n", index));
520 		if (table_info->colnum >= SUNFMRESOURCE_COLMAX) {
521 			snmp_free_varbind(var);
522 			table_info->indexes = NULL;
523 			table_info->number_indexes = 0;
524 			DEBUGMSGTL((MODNAME_STR, "nextrsrc: out of columns\n"));
525 			return (NULL);
526 		}
527 		table_info->colnum++;
528 		index = 1;
529 
530 		data = resource_lookup_index_nextvalid(index);
531 	}
532 
533 	if (data == NULL) {
534 		DEBUGMSGTL((MODNAME_STR, "nextrsrc: exact match not found for "
535 		    "index %lu; stopping\n", index));
536 		snmp_free_varbind(var);
537 		table_info->indexes = NULL;
538 		table_info->number_indexes = 0;
539 		return (NULL);
540 	}
541 
542 	*var->val.integer = index;
543 	table_info->indexes = var;
544 
545 	DEBUGMSGTL((MODNAME_STR, "matching data is %lu/%s@%p\n", data->d_index,
546 	    data->d_ari_fmri, data));
547 
548 	return (data);
549 }
550 
551 /*ARGSUSED*/
552 static sunFmResource_data_t *
553 sunFmResourceTable_rsrc(netsnmp_handler_registration *reginfo,
554     netsnmp_table_request_info *table_info)
555 {
556 	sunFmResource_data_t	*data;
557 	netsnmp_variable_list	*var;
558 
559 	ASSERT(table_info->number_indexes == 1);
560 
561 	return (resource_lookup_index_exact(table_info->index_oid[0]));
562 }
563 
564 static void
565 sunFmResourceTable_return(unsigned int reg, void *arg)
566 {
567 	netsnmp_delegated_cache		*cache = (netsnmp_delegated_cache *)arg;
568 	netsnmp_request_info		*request;
569 	netsnmp_agent_request_info	*reqinfo;
570 	netsnmp_handler_registration	*reginfo;
571 	netsnmp_mib_handler		*handler;
572 	netsnmp_table_request_info	*table_info;
573 	netsnmp_variable_list		*var;
574 	sunFmResource_data_t		*data;
575 	ulong_t				rsrcstate;
576 
577 	ASSERT(netsnmp_handler_check_cache(cache) != NULL);
578 
579 	(void) pthread_mutex_lock(&update_lock);
580 	if (update_status != US_QUIET) {
581 		struct timeval			tv;
582 
583 		tv.tv_sec = UPDATE_WAIT_MILLIS / 1000;
584 		tv.tv_usec = (UPDATE_WAIT_MILLIS % 1000) * 1000;
585 
586 		(void) snmp_alarm_register_hr(tv, 0, sunFmResourceTable_return,
587 		    cache);
588 		(void) pthread_mutex_unlock(&update_lock);
589 		return;
590 	}
591 
592 	request = cache->requests;
593 	reqinfo = cache->reqinfo;
594 	reginfo = cache->reginfo;
595 	handler = cache->handler;
596 
597 	var = request->requestvb;
598 	table_info = netsnmp_extract_table_info(request);
599 	request->delegated = 0;
600 
601 	ASSERT(table_info->colnum >= SUNFMRESOURCE_COLMIN);
602 	ASSERT(table_info->colnum <= SUNFMRESOURCE_COLMAX);
603 
604 	/*
605 	 * table_info->colnum contains the column number requested.
606 	 * table_info->indexes contains a linked list of snmp variable
607 	 * bindings for the indexes of the table.  Values in the list
608 	 * have been set corresponding to the indexes of the
609 	 * request.  We have other guarantees as well:
610 	 *
611 	 * - The column number is always within range.
612 	 * - If we have no index data, table_info->index_oid_len is 0.
613 	 * - We will never receive requests outside our table nor
614 	 *   those with the first subid anything other than 1 (Entry)
615 	 *   nor those without a column number.  This is true even
616 	 *   for GETNEXT requests.
617 	 */
618 
619 	switch (reqinfo->mode) {
620 	case MODE_GET:
621 		if ((data = sunFmResourceTable_rsrc(reginfo, table_info)) ==
622 		    NULL) {
623 			netsnmp_free_delegated_cache(cache);
624 			(void) pthread_mutex_unlock(&update_lock);
625 			return;
626 		}
627 		break;
628 	case MODE_GETNEXT:
629 	case MODE_GETBULK:
630 		if ((data = sunFmResourceTable_nextrsrc(reginfo, table_info)) ==
631 		    NULL) {
632 			netsnmp_free_delegated_cache(cache);
633 			(void) pthread_mutex_unlock(&update_lock);
634 			return;
635 		}
636 		break;
637 	default:
638 		snmp_log(LOG_ERR, MODNAME_STR ": Unsupported request mode %d\n",
639 		    reqinfo->mode);
640 		netsnmp_free_delegated_cache(cache);
641 		(void) pthread_mutex_unlock(&update_lock);
642 		return;
643 	}
644 
645 	switch (table_info->colnum) {
646 	case SUNFMRESOURCE_COL_FMRI:
647 		netsnmp_table_build_result(reginfo, request, table_info,
648 		    ASN_OCTET_STR, (uchar_t *)data->d_ari_fmri,
649 		    strlen(data->d_ari_fmri));
650 		break;
651 	case SUNFMRESOURCE_COL_STATUS:
652 		switch (data->d_ari_flags &
653 		    (FMD_ADM_RSRC_FAULTY|FMD_ADM_RSRC_UNUSABLE)) {
654 		default:
655 			rsrcstate = SUNFMRESOURCE_STATE_OK;
656 			break;
657 		case FMD_ADM_RSRC_FAULTY:
658 			rsrcstate = SUNFMRESOURCE_STATE_DEGRADED;
659 			break;
660 		case FMD_ADM_RSRC_UNUSABLE:
661 			rsrcstate = SUNFMRESOURCE_STATE_UNKNOWN;
662 			break;
663 		case FMD_ADM_RSRC_FAULTY | FMD_ADM_RSRC_UNUSABLE:
664 			rsrcstate = SUNFMRESOURCE_STATE_FAULTED;
665 			break;
666 		}
667 		netsnmp_table_build_result(reginfo, request, table_info,
668 		    ASN_INTEGER, (uchar_t *)&rsrcstate,
669 		    sizeof (rsrcstate));
670 		break;
671 	case SUNFMRESOURCE_COL_DIAGNOSISUUID:
672 		netsnmp_table_build_result(reginfo, request, table_info,
673 		    ASN_OCTET_STR, (uchar_t *)data->d_ari_case,
674 		    strlen(data->d_ari_case));
675 		break;
676 	default:
677 		break;
678 	}
679 	netsnmp_free_delegated_cache(cache);
680 	(void) pthread_mutex_unlock(&update_lock);
681 }
682 
683 static int
684 sunFmResourceTable_handler(netsnmp_mib_handler *handler,
685     netsnmp_handler_registration *reginfo, netsnmp_agent_request_info *reqinfo,
686     netsnmp_request_info *requests)
687 {
688 	netsnmp_request_info		*request;
689 	struct timeval			tv;
690 
691 	tv.tv_sec = UPDATE_WAIT_MILLIS / 1000;
692 	tv.tv_usec = (UPDATE_WAIT_MILLIS % 1000) * 1000;
693 
694 	request_update();
695 
696 	for (request = requests; request; request = request->next) {
697 		if (request->processed != 0)
698 			continue;
699 
700 		if (netsnmp_extract_table_info(request) == NULL)
701 			continue;
702 
703 		request->delegated = 1;
704 		(void) snmp_alarm_register_hr(tv, 0, sunFmResourceTable_return,
705 		    (void *) netsnmp_create_delegated_cache(handler, reginfo,
706 		    reqinfo, request, NULL));
707 	}
708 
709 	return (SNMP_ERR_NOERROR);
710 }
711 
712 static void
713 sunFmResourceCount_return(unsigned int reg, void *arg)
714 {
715 	netsnmp_delegated_cache		*cache = (netsnmp_delegated_cache *)arg;
716 	netsnmp_request_info		*request;
717 	netsnmp_agent_request_info	*reqinfo;
718 	netsnmp_handler_registration	*reginfo;
719 	netsnmp_mib_handler		*handler;
720 	ulong_t				rsrc_count_long;
721 	int				err;
722 
723 	ASSERT(netsnmp_handler_check_cache(cache) != NULL);
724 
725 	(void) pthread_mutex_lock(&update_lock);
726 	if (update_status != US_QUIET) {
727 		struct timeval	tv;
728 
729 		tv.tv_sec = UPDATE_WAIT_MILLIS / 1000;
730 		tv.tv_usec = (UPDATE_WAIT_MILLIS % 1000) * 1000;
731 
732 		(void) snmp_alarm_register_hr(tv, 0, sunFmResourceCount_return,
733 		    cache);
734 		(void) pthread_mutex_unlock(&update_lock);
735 		return;
736 	}
737 
738 	request = cache->requests;
739 	reqinfo = cache->reqinfo;
740 	reginfo = cache->reginfo;
741 	handler = cache->handler;
742 
743 	request->delegated = 0;
744 
745 	switch (reqinfo->mode) {
746 	case MODE_GET:
747 		DEBUGMSGTL((MODNAME_STR, "resource count is %u\n", rsrc_count));
748 		rsrc_count_long = (ulong_t)rsrc_count;
749 		snmp_set_var_typed_value(request->requestvb, ASN_GAUGE,
750 		    (uchar_t *)&rsrc_count_long, sizeof (rsrc_count_long));
751 		break;
752 	default:
753 		snmp_log(LOG_ERR, MODNAME_STR ": Unsupported request mode %d\n",
754 		    reqinfo->mode);
755 	}
756 
757 	netsnmp_free_delegated_cache(cache);
758 	(void) pthread_mutex_unlock(&update_lock);
759 }
760 
761 static int
762 sunFmResourceCount_handler(netsnmp_mib_handler *handler,
763     netsnmp_handler_registration *reginfo, netsnmp_agent_request_info *reqinfo,
764     netsnmp_request_info *requests)
765 {
766 	struct timeval	tv;
767 
768 	tv.tv_sec = UPDATE_WAIT_MILLIS / 1000;
769 	tv.tv_usec = (UPDATE_WAIT_MILLIS % 1000) * 1000;
770 
771 	request_update();
772 
773 	/*
774 	 * We are never called for a GETNEXT when registered as an
775 	 * instance; it's handled for us and converted to a GET.
776 	 * Also, an instance handler is given only one request at a time, so
777 	 * we don't need to loop over a list of requests.
778 	 */
779 
780 	if (requests->processed != 0)
781 		return (SNMP_ERR_NOERROR);
782 
783 	requests->delegated = 1;
784 	(void) snmp_alarm_register_hr(tv, 0, sunFmResourceCount_return,
785 	    (void *) netsnmp_create_delegated_cache(handler, reginfo,
786 	    reqinfo, requests, NULL));
787 
788 	return (SNMP_ERR_NOERROR);
789 }
790