xref: /illumos-gate/usr/src/lib/fm/libfmd_snmp/common/resource.c (revision 168c213023b7f347f11abfc72f448b0c621ab718)
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 		(void) 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 		(void) 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 		(void) 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_all = 0;
288 	uc.uc_index = 0;
289 	uc.uc_type = UCT_ALL;
290 
291 	for (;;) {
292 		(void) pthread_mutex_lock(&update_lock);
293 		update_status = US_QUIET;
294 		while (update_status == US_QUIET)
295 			(void) pthread_cond_wait(&update_cv, &update_lock);
296 		update_status = US_INPROGRESS;
297 		(void) pthread_mutex_unlock(&update_lock);
298 		(void) rsrcinfo_update(&uc);
299 	}
300 }
301 
302 static void
303 request_update(void)
304 {
305 	(void) pthread_mutex_lock(&update_lock);
306 	if (update_status != US_QUIET) {
307 		(void) pthread_mutex_unlock(&update_lock);
308 		return;
309 	}
310 	update_status = US_NEEDED;
311 	(void) pthread_cond_signal(&update_cv);
312 	(void) pthread_mutex_unlock(&update_lock);
313 }
314 
315 /*ARGSUSED*/
316 static int
317 resource_compare_fmri(const void *l, const void *r, void *private)
318 {
319 	sunFmResource_data_t	*l_data = (sunFmResource_data_t *)l;
320 	sunFmResource_data_t	*r_data = (sunFmResource_data_t *)r;
321 
322 	ASSERT(l_data != NULL && r_data != NULL);
323 
324 	return (strcmp(l_data->d_ari_fmri, r_data->d_ari_fmri));
325 }
326 
327 /*ARGSUSED*/
328 static int
329 resource_compare_index(const void *l, const void *r, void *private)
330 {
331 	sunFmResource_data_t	*l_data = (sunFmResource_data_t *)l;
332 	sunFmResource_data_t	*r_data = (sunFmResource_data_t *)r;
333 
334 	ASSERT(l_data != NULL && r_data != NULL);
335 
336 	return (l_data->d_index < r_data->d_index ? -1 :
337 	    l_data->d_index > r_data->d_index ? 1 : 0);
338 }
339 
340 int
341 sunFmResourceTable_init(void)
342 {
343 	static oid sunFmResourceTable_oid[] = { SUNFMRESOURCETABLE_OID };
344 	static oid sunFmResourceCount_oid[] = { SUNFMRESOURCECOUNT_OID, 0 };
345 	netsnmp_table_registration_info *table_info;
346 	netsnmp_handler_registration *handler;
347 	int err;
348 
349 	if ((err = pthread_mutex_init(&update_lock, NULL)) != 0) {
350 		snmp_log(LOG_ERR, MODNAME_STR ": mutex_init failure: %s\n",
351 		    strerror(err));
352 		return (MIB_REGISTRATION_FAILED);
353 	}
354 	if ((err = pthread_cond_init(&update_cv, NULL)) != 0) {
355 		snmp_log(LOG_ERR, MODNAME_STR ": cond_init failure: %s\n",
356 		    strerror(err));
357 		return (MIB_REGISTRATION_FAILED);
358 	}
359 
360 	if ((err = pthread_create(NULL, NULL, (void *(*)(void *))update_thread,
361 	    NULL)) != 0) {
362 		snmp_log(LOG_ERR, MODNAME_STR ": error creating update "
363 		    "thread: %s\n", strerror(err));
364 		return (MIB_REGISTRATION_FAILED);
365 	}
366 
367 	if ((table_info =
368 	    SNMP_MALLOC_TYPEDEF(netsnmp_table_registration_info)) == NULL)
369 		return (MIB_REGISTRATION_FAILED);
370 
371 	if ((handler = netsnmp_create_handler_registration("sunFmResourceTable",
372 	    sunFmResourceTable_handler, sunFmResourceTable_oid,
373 	    OID_LENGTH(sunFmResourceTable_oid), HANDLER_CAN_RONLY)) == NULL) {
374 		SNMP_FREE(table_info);
375 		return (MIB_REGISTRATION_FAILED);
376 	}
377 
378 	/*
379 	 * The Net-SNMP template uses add_indexes here, but that
380 	 * function is unsafe because it does not check for failure.
381 	 */
382 	if (netsnmp_table_helper_add_index(table_info, ASN_UNSIGNED) == NULL) {
383 		SNMP_FREE(table_info);
384 		SNMP_FREE(handler);
385 		return (MIB_REGISTRATION_FAILED);
386 	}
387 
388 	table_info->min_column = SUNFMRESOURCE_COLMIN;
389 	table_info->max_column = SUNFMRESOURCE_COLMAX;
390 
391 	if ((rsrc_fmri_avl_pool = uu_avl_pool_create("rsrc_fmri",
392 	    sizeof (sunFmResource_data_t),
393 	    offsetof(sunFmResource_data_t, d_fmri_avl), resource_compare_fmri,
394 	    UU_AVL_DEBUG)) == NULL) {
395 		snmp_log(LOG_ERR, MODNAME_STR ": rsrc_fmri avl pool creation "
396 		    "failed: %s\n", uu_strerror(uu_error()));
397 		snmp_free_varbind(table_info->indexes);
398 		SNMP_FREE(table_info);
399 		SNMP_FREE(handler);
400 	}
401 
402 	if ((rsrc_fmri_avl = uu_avl_create(rsrc_fmri_avl_pool, NULL,
403 	    UU_AVL_DEBUG)) == NULL) {
404 		snmp_log(LOG_ERR, MODNAME_STR ": rsrc_fmri avl creation "
405 		    "failed: %s\n", uu_strerror(uu_error()));
406 		snmp_free_varbind(table_info->indexes);
407 		SNMP_FREE(table_info);
408 		SNMP_FREE(handler);
409 		uu_avl_pool_destroy(rsrc_fmri_avl_pool);
410 		return (MIB_REGISTRATION_FAILED);
411 	}
412 
413 	if ((rsrc_index_avl_pool = uu_avl_pool_create("rsrc_index",
414 	    sizeof (sunFmResource_data_t),
415 	    offsetof(sunFmResource_data_t, d_index_avl),
416 	    resource_compare_index, UU_AVL_DEBUG)) == NULL) {
417 		snmp_log(LOG_ERR, MODNAME_STR ": rsrc_index avl pool creation "
418 		    "failed: %s\n", uu_strerror(uu_error()));
419 		snmp_free_varbind(table_info->indexes);
420 		SNMP_FREE(table_info);
421 		SNMP_FREE(handler);
422 		uu_avl_destroy(rsrc_fmri_avl);
423 		uu_avl_pool_destroy(rsrc_fmri_avl_pool);
424 	}
425 
426 	if ((rsrc_index_avl = uu_avl_create(rsrc_index_avl_pool, NULL,
427 	    UU_AVL_DEBUG)) == NULL) {
428 		snmp_log(LOG_ERR, MODNAME_STR ": rsrc_index avl creation "
429 		    "failed: %s\n", uu_strerror(uu_error()));
430 		snmp_free_varbind(table_info->indexes);
431 		SNMP_FREE(table_info);
432 		SNMP_FREE(handler);
433 		uu_avl_destroy(rsrc_fmri_avl);
434 		uu_avl_pool_destroy(rsrc_fmri_avl_pool);
435 		uu_avl_pool_destroy(rsrc_index_avl_pool);
436 		return (MIB_REGISTRATION_FAILED);
437 	}
438 
439 	if ((err = netsnmp_register_table(handler, table_info)) !=
440 	    MIB_REGISTERED_OK) {
441 		snmp_free_varbind(table_info->indexes);
442 		SNMP_FREE(table_info);
443 		SNMP_FREE(handler);
444 		uu_avl_destroy(rsrc_fmri_avl);
445 		uu_avl_pool_destroy(rsrc_fmri_avl_pool);
446 		uu_avl_destroy(rsrc_index_avl);
447 		uu_avl_pool_destroy(rsrc_index_avl_pool);
448 		return (err);
449 	}
450 
451 	if ((err = netsnmp_register_read_only_instance(
452 	    netsnmp_create_handler_registration("sunFmResourceCount",
453 		sunFmResourceCount_handler, sunFmResourceCount_oid,
454 		OID_LENGTH(sunFmResourceCount_oid), HANDLER_CAN_RONLY))) !=
455 	    MIB_REGISTERED_OK) {
456 		/*
457 		 * There's no way to unregister the table handler, so we
458 		 * can't free any of the data, either.
459 		 */
460 		return (err);
461 	}
462 
463 	return (MIB_REGISTERED_OK);
464 }
465 
466 /*
467  * These two functions form the core of GET/GETNEXT/GETBULK handling (the
468  * only kind we do).  They perform two functions:
469  *
470  * - First, frob the request to set all the index variables to correspond
471  *   to the value that's going to be returned.  For GET, this is a nop;
472  *   for GETNEXT/GETBULK it always requires some work.
473  * - Second, find and return the fmd resource information corresponding to
474  *   the (possibly updated) indices.
475  *
476  * These should be as fast as possible; they run in the agent thread.
477  */
478 static sunFmResource_data_t *
479 sunFmResourceTable_nextrsrc(netsnmp_handler_registration *reginfo,
480     netsnmp_table_request_info *table_info)
481 {
482 	sunFmResource_data_t	*data;
483 	netsnmp_variable_list	*var;
484 	ulong_t index;
485 
486 	/*
487 	 * If we have no index, we must make one.
488 	 */
489 	if (table_info->number_indexes < 1) {
490 		oid tmpoid[MAX_OID_LEN];
491 		index = 1;
492 
493 		DEBUGMSGTL((MODNAME_STR, "nextrsrc: no indexes given\n"));
494 		var = SNMP_MALLOC_TYPEDEF(netsnmp_variable_list);
495 		snmp_set_var_typed_value(var, ASN_UNSIGNED, (uchar_t *)&index,
496 		    sizeof (index));
497 		(void) memcpy(tmpoid, reginfo->rootoid,
498 		    reginfo->rootoid_len * sizeof (oid));
499 		tmpoid[reginfo->rootoid_len] = 1;
500 		tmpoid[reginfo->rootoid_len + 1] = table_info->colnum;
501 		if (build_oid(&var->name, &var->name_length, tmpoid,
502 		    reginfo->rootoid_len + 2, var) != SNMPERR_SUCCESS) {
503 			snmp_free_varbind(var);
504 			return (NULL);
505 		}
506 		DEBUGMSGTL((MODNAME_STR, "nextrsrc: built fake index:\n"));
507 		DEBUGMSGVAR((MODNAME_STR, var));
508 		DEBUGMSG((MODNAME_STR, "\n"));
509 	} else {
510 		var = snmp_clone_varbind(table_info->indexes);
511 		index = *var->val.integer;
512 		DEBUGMSGTL((MODNAME_STR, "nextrsrc: received index:\n"));
513 		DEBUGMSGVAR((MODNAME_STR, var));
514 		DEBUGMSG((MODNAME_STR, "\n"));
515 		index++;
516 	}
517 
518 	snmp_free_varbind(table_info->indexes);
519 	table_info->indexes = NULL;
520 	table_info->number_indexes = 0;
521 
522 	if ((data = resource_lookup_index_nextvalid(index)) == NULL) {
523 		DEBUGMSGTL((MODNAME_STR, "nextrsrc: exact match not found for "
524 		    "index %lu; trying next column\n", index));
525 		if (table_info->colnum >=
526 		    netsnmp_find_table_registration_info(reginfo)->max_column) {
527 			snmp_free_varbind(var);
528 			DEBUGMSGTL((MODNAME_STR, "nextrsrc: out of columns\n"));
529 			return (NULL);
530 		}
531 		table_info->colnum++;
532 		index = 1;
533 
534 		data = resource_lookup_index_nextvalid(index);
535 	}
536 
537 	if (data == NULL) {
538 		DEBUGMSGTL((MODNAME_STR, "nextrsrc: exact match not found for "
539 		    "index %lu; stopping\n", index));
540 		snmp_free_varbind(var);
541 		return (NULL);
542 	}
543 
544 	*var->val.integer = index;
545 	table_info->indexes = var;
546 	table_info->number_indexes = 1;
547 
548 	DEBUGMSGTL((MODNAME_STR, "matching data is %lu/%s@%p\n", data->d_index,
549 	    data->d_ari_fmri, data));
550 
551 	return (data);
552 }
553 
554 /*ARGSUSED*/
555 static sunFmResource_data_t *
556 sunFmResourceTable_rsrc(netsnmp_handler_registration *reginfo,
557     netsnmp_table_request_info *table_info)
558 {
559 	ASSERT(table_info->number_indexes == 1);
560 
561 	return (resource_lookup_index_exact(table_info->index_oid[0]));
562 }
563 
564 /*ARGSUSED*/
565 static void
566 sunFmResourceTable_return(unsigned int reg, void *arg)
567 {
568 	netsnmp_delegated_cache		*cache = (netsnmp_delegated_cache *)arg;
569 	netsnmp_request_info		*request;
570 	netsnmp_agent_request_info	*reqinfo;
571 	netsnmp_handler_registration	*reginfo;
572 	netsnmp_table_request_info	*table_info;
573 	sunFmResource_data_t		*data;
574 	ulong_t				rsrcstate;
575 
576 	ASSERT(netsnmp_handler_check_cache(cache) != NULL);
577 
578 	(void) pthread_mutex_lock(&update_lock);
579 	if (update_status != US_QUIET) {
580 		struct timeval			tv;
581 
582 		tv.tv_sec = UPDATE_WAIT_MILLIS / 1000;
583 		tv.tv_usec = (UPDATE_WAIT_MILLIS % 1000) * 1000;
584 
585 		(void) snmp_alarm_register_hr(tv, 0, sunFmResourceTable_return,
586 		    cache);
587 		(void) pthread_mutex_unlock(&update_lock);
588 		return;
589 	}
590 
591 	request = cache->requests;
592 	reqinfo = cache->reqinfo;
593 	reginfo = cache->reginfo;
594 
595 	table_info = netsnmp_extract_table_info(request);
596 	request->delegated = 0;
597 
598 	ASSERT(table_info->colnum >= SUNFMRESOURCE_COLMIN);
599 	ASSERT(table_info->colnum <= SUNFMRESOURCE_COLMAX);
600 
601 	/*
602 	 * table_info->colnum contains the column number requested.
603 	 * table_info->indexes contains a linked list of snmp variable
604 	 * bindings for the indexes of the table.  Values in the list
605 	 * have been set corresponding to the indexes of the
606 	 * request.  We have other guarantees as well:
607 	 *
608 	 * - The column number is always within range.
609 	 * - If we have no index data, table_info->index_oid_len is 0.
610 	 * - We will never receive requests outside our table nor
611 	 *   those with the first subid anything other than 1 (Entry)
612 	 *   nor those without a column number.  This is true even
613 	 *   for GETNEXT requests.
614 	 */
615 
616 	switch (reqinfo->mode) {
617 	case MODE_GET:
618 		if ((data = sunFmResourceTable_rsrc(reginfo, table_info)) ==
619 		    NULL) {
620 			netsnmp_free_delegated_cache(cache);
621 			(void) pthread_mutex_unlock(&update_lock);
622 			return;
623 		}
624 		break;
625 	case MODE_GETNEXT:
626 	case MODE_GETBULK:
627 		if ((data = sunFmResourceTable_nextrsrc(reginfo, table_info)) ==
628 		    NULL) {
629 			netsnmp_free_delegated_cache(cache);
630 			(void) pthread_mutex_unlock(&update_lock);
631 			return;
632 		}
633 		break;
634 	default:
635 		snmp_log(LOG_ERR, MODNAME_STR ": Unsupported request mode %d\n",
636 		    reqinfo->mode);
637 		netsnmp_free_delegated_cache(cache);
638 		(void) pthread_mutex_unlock(&update_lock);
639 		return;
640 	}
641 
642 	switch (table_info->colnum) {
643 	case SUNFMRESOURCE_COL_FMRI:
644 		netsnmp_table_build_result(reginfo, request, table_info,
645 		    ASN_OCTET_STR, (uchar_t *)data->d_ari_fmri,
646 		    strlen(data->d_ari_fmri));
647 		break;
648 	case SUNFMRESOURCE_COL_STATUS:
649 		switch (data->d_ari_flags &
650 		    (FMD_ADM_RSRC_FAULTY|FMD_ADM_RSRC_UNUSABLE)) {
651 		default:
652 			rsrcstate = SUNFMRESOURCE_STATE_OK;
653 			break;
654 		case FMD_ADM_RSRC_FAULTY:
655 			rsrcstate = SUNFMRESOURCE_STATE_DEGRADED;
656 			break;
657 		case FMD_ADM_RSRC_UNUSABLE:
658 			rsrcstate = SUNFMRESOURCE_STATE_UNKNOWN;
659 			break;
660 		case FMD_ADM_RSRC_FAULTY | FMD_ADM_RSRC_UNUSABLE:
661 			rsrcstate = SUNFMRESOURCE_STATE_FAULTED;
662 			break;
663 		}
664 		netsnmp_table_build_result(reginfo, request, table_info,
665 		    ASN_INTEGER, (uchar_t *)&rsrcstate,
666 		    sizeof (rsrcstate));
667 		break;
668 	case SUNFMRESOURCE_COL_DIAGNOSISUUID:
669 		netsnmp_table_build_result(reginfo, request, table_info,
670 		    ASN_OCTET_STR, (uchar_t *)data->d_ari_case,
671 		    strlen(data->d_ari_case));
672 		break;
673 	default:
674 		break;
675 	}
676 	netsnmp_free_delegated_cache(cache);
677 	(void) pthread_mutex_unlock(&update_lock);
678 }
679 
680 static int
681 sunFmResourceTable_handler(netsnmp_mib_handler *handler,
682     netsnmp_handler_registration *reginfo, netsnmp_agent_request_info *reqinfo,
683     netsnmp_request_info *requests)
684 {
685 	netsnmp_request_info		*request;
686 	struct timeval			tv;
687 
688 	tv.tv_sec = UPDATE_WAIT_MILLIS / 1000;
689 	tv.tv_usec = (UPDATE_WAIT_MILLIS % 1000) * 1000;
690 
691 	request_update();
692 
693 	for (request = requests; request; request = request->next) {
694 		if (request->processed != 0)
695 			continue;
696 
697 		if (netsnmp_extract_table_info(request) == NULL)
698 			continue;
699 
700 		request->delegated = 1;
701 		(void) snmp_alarm_register_hr(tv, 0, sunFmResourceTable_return,
702 		    (void *) netsnmp_create_delegated_cache(handler, reginfo,
703 		    reqinfo, request, NULL));
704 	}
705 
706 	return (SNMP_ERR_NOERROR);
707 }
708 
709 /*ARGSUSED*/
710 static void
711 sunFmResourceCount_return(unsigned int reg, void *arg)
712 {
713 	netsnmp_delegated_cache		*cache = (netsnmp_delegated_cache *)arg;
714 	netsnmp_request_info		*request;
715 	netsnmp_agent_request_info	*reqinfo;
716 	ulong_t				rsrc_count_long;
717 
718 	ASSERT(netsnmp_handler_check_cache(cache) != NULL);
719 
720 	(void) pthread_mutex_lock(&update_lock);
721 	if (update_status != US_QUIET) {
722 		struct timeval	tv;
723 
724 		tv.tv_sec = UPDATE_WAIT_MILLIS / 1000;
725 		tv.tv_usec = (UPDATE_WAIT_MILLIS % 1000) * 1000;
726 
727 		(void) snmp_alarm_register_hr(tv, 0, sunFmResourceCount_return,
728 		    cache);
729 		(void) pthread_mutex_unlock(&update_lock);
730 		return;
731 	}
732 
733 	request = cache->requests;
734 	reqinfo = cache->reqinfo;
735 
736 	request->delegated = 0;
737 
738 	switch (reqinfo->mode) {
739 	/*
740 	 * According to the documentation, it's not possible for us ever to
741 	 * be called with MODE_GETNEXT.  However, Net-SNMP does the following:
742 	 * - set reqinfo->mode to MODE_GET
743 	 * - invoke the handler
744 	 * - set reqinfo->mode to MODE_GETNEXT (even if the request was not
745 	 *   actually processed; i.e. it's been delegated)
746 	 * Since we're called back later with the same reqinfo, we see
747 	 * GETNEXT.  Therefore this case is needed to work around the
748 	 * Net-SNMP bug.
749 	 */
750 	case MODE_GET:
751 	case MODE_GETNEXT:
752 		DEBUGMSGTL((MODNAME_STR, "resource count is %u\n", rsrc_count));
753 		rsrc_count_long = (ulong_t)rsrc_count;
754 		snmp_set_var_typed_value(request->requestvb, ASN_GAUGE,
755 		    (uchar_t *)&rsrc_count_long, sizeof (rsrc_count_long));
756 		break;
757 	default:
758 		snmp_log(LOG_ERR, MODNAME_STR ": Unsupported request mode %d\n",
759 		    reqinfo->mode);
760 	}
761 
762 	netsnmp_free_delegated_cache(cache);
763 	(void) pthread_mutex_unlock(&update_lock);
764 }
765 
766 static int
767 sunFmResourceCount_handler(netsnmp_mib_handler *handler,
768     netsnmp_handler_registration *reginfo, netsnmp_agent_request_info *reqinfo,
769     netsnmp_request_info *requests)
770 {
771 	struct timeval	tv;
772 
773 	tv.tv_sec = UPDATE_WAIT_MILLIS / 1000;
774 	tv.tv_usec = (UPDATE_WAIT_MILLIS % 1000) * 1000;
775 
776 	request_update();
777 
778 	/*
779 	 * We are never called for a GETNEXT when registered as an
780 	 * instance; it's handled for us and converted to a GET.
781 	 * Also, an instance handler is given only one request at a time, so
782 	 * we don't need to loop over a list of requests.
783 	 */
784 
785 	if (requests->processed != 0)
786 		return (SNMP_ERR_NOERROR);
787 
788 	requests->delegated = 1;
789 	(void) snmp_alarm_register_hr(tv, 0, sunFmResourceCount_return,
790 	    (void *) netsnmp_create_delegated_cache(handler, reginfo,
791 	    reqinfo, requests, NULL));
792 
793 	return (SNMP_ERR_NOERROR);
794 }
795