xref: /titanic_41/usr/src/lib/fm/libfmd_snmp/common/resource.c (revision 2b24ab6b3865caeede9eeb9db6b83e1d89dcd1ea)
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 2007 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 		data = uu_avl_next(rsrc_index_avl, data);
143 
144 	return (data);
145 }
146 
147 /*
148  * Possible update the contents of a single resource within the cache.  This
149  * is our callback from fmd_rsrc_iter.
150  */
151 static int
152 rsrcinfo_update_one(const fmd_adm_rsrcinfo_t *rsrcinfo, void *arg)
153 {
154 	const sunFmResource_update_ctx_t *update_ctx =
155 	    (sunFmResource_update_ctx_t *)arg;
156 	sunFmResource_data_t *data = resource_lookup_fmri(rsrcinfo->ari_fmri);
157 
158 	++rsrc_count;
159 
160 	/*
161 	 * A resource we haven't seen before.  We're obligated to index
162 	 * it and link it into our cache so that we can find it, but we're
163 	 * not obligated to fill it in completely unless we're doing a
164 	 * full update or this is the resource we were asked for.  This
165 	 * avoids unnecessary iteration and memory manipulation for data
166 	 * we're not going to return for this request.
167 	 */
168 	if (data == NULL) {
169 		uu_avl_index_t idx;
170 
171 		DEBUGMSGTL((MODNAME_STR, "found new resource %s\n",
172 		    rsrcinfo->ari_fmri));
173 		if ((data = SNMP_MALLOC_TYPEDEF(sunFmResource_data_t)) ==
174 		    NULL) {
175 			snmp_log(LOG_ERR, MODNAME_STR ": Out of memory for "
176 			    "new resource data at %s:%d\n", __FILE__, __LINE__);
177 			return (1);
178 		}
179 		/*
180 		 * We allocate indices sequentially and never reuse them.
181 		 * This ensures we can always return valid GETNEXT responses
182 		 * without having to reindex, and it provides the user a
183 		 * more consistent view of the fault manager.
184 		 */
185 		data->d_index = ++max_index;
186 		DEBUGMSGTL((MODNAME_STR, "index %lu is %s@%p\n", data->d_index,
187 		    rsrcinfo->ari_fmri, data));
188 
189 		(void) strlcpy(data->d_ari_fmri, rsrcinfo->ari_fmri,
190 		    sizeof (data->d_ari_fmri));
191 
192 		uu_avl_node_init(data, &data->d_fmri_avl, rsrc_fmri_avl_pool);
193 		(void) uu_avl_find(rsrc_fmri_avl, data, NULL, &idx);
194 		uu_avl_insert(rsrc_fmri_avl, data, idx);
195 
196 		uu_avl_node_init(data, &data->d_index_avl, rsrc_index_avl_pool);
197 		(void) uu_avl_find(rsrc_index_avl, data, NULL, &idx);
198 		uu_avl_insert(rsrc_index_avl, data, idx);
199 
200 		DEBUGMSGTL((MODNAME_STR, "completed new resource %lu/%s@%p\n",
201 		    data->d_index, data->d_ari_fmri, data));
202 	}
203 
204 	data->d_valid = valid_stamp;
205 
206 	DEBUGMSGTL((MODNAME_STR, "timestamp updated for %lu/%s@%p: %lu\n",
207 	    data->d_index, data->d_ari_fmri, data, data->d_valid));
208 
209 	if ((update_ctx->uc_type & UCT_ALL) ||
210 	    update_ctx->uc_index == data->d_index) {
211 		(void) strlcpy(data->d_ari_case, rsrcinfo->ari_case,
212 		    sizeof (data->d_ari_case));
213 		data->d_ari_flags = rsrcinfo->ari_flags;
214 	}
215 
216 	return (!(update_ctx->uc_type & UCT_ALL) &&
217 	    update_ctx->uc_index == data->d_index);
218 }
219 
220 /*
221  * Update some or all resource data from fmd.  If type includes UCT_ALL, all
222  * resources will be indexed and their data cached.  If type includes
223  * UCT_INDEX, updates will stop once the resource matching index has been
224  * updated.  If UCT_COUNT is set, the number of faulted resources will be
225  * set.
226  *
227  * Returns appropriate SNMP error codes.
228  */
229 static int
230 rsrcinfo_update(sunFmResource_update_ctx_t *update_ctx)
231 {
232 	fmd_adm_t *adm;
233 	int err;
234 
235 	ASSERT(update_ctx != NULL);
236 	ASSERT((update_ctx->uc_type & (UCT_ALL|UCT_INDEX)) !=
237 	    (UCT_ALL|UCT_INDEX));
238 	ASSERT((update_ctx->uc_type & ~UCT_FLAGS) == 0);
239 	ASSERT(VALID_AVL_STATE);
240 
241 	if ((adm = fmd_adm_open(update_ctx->uc_host, update_ctx->uc_prog,
242 	    update_ctx->uc_version)) == NULL) {
243 		snmp_log(LOG_ERR, MODNAME_STR ": Communication with fmd "
244 		    "failed: %s\n", strerror(errno));
245 		return (SNMP_ERR_RESOURCEUNAVAILABLE);
246 	}
247 
248 	if (update_ctx->uc_type == UCT_COUNT) {
249 		err = fmd_adm_rsrc_count(adm, update_ctx->uc_all, &rsrc_count);
250 	} else {
251 		++valid_stamp;
252 		rsrc_count = 0;
253 		err = fmd_adm_rsrc_iter(adm, update_ctx->uc_all,
254 		    rsrcinfo_update_one, update_ctx);
255 		DEBUGMSGTL((MODNAME_STR, "resource iteration completed\n"));
256 	}
257 
258 	fmd_adm_close(adm);
259 
260 	if (err != 0) {
261 		snmp_log(LOG_ERR, MODNAME_STR ": fmd resource information "
262 		    "update failed: %s\n", fmd_adm_errmsg(adm));
263 		return (SNMP_ERR_RESOURCEUNAVAILABLE);
264 	}
265 
266 	return (SNMP_ERR_NOERROR);
267 }
268 
269 /*ARGSUSED*/
270 static void
271 update_thread(void *arg)
272 {
273 	/*
274 	 * The current rsrcinfo_update implementation offers minimal savings
275 	 * for the use of index-only updates; therefore we always do a full
276 	 * update.  If it becomes advantageous to limit updates to a single
277 	 * index, the contexts can be queued by the handler instead.
278 	 */
279 	sunFmResource_update_ctx_t	uc;
280 
281 	uc.uc_host = NULL;
282 	uc.uc_prog = FMD_ADM_PROGRAM;
283 	uc.uc_version = FMD_ADM_VERSION;
284 
285 	uc.uc_all = 0;
286 	uc.uc_index = 0;
287 	uc.uc_type = UCT_ALL;
288 
289 	for (;;) {
290 		(void) pthread_mutex_lock(&update_lock);
291 		update_status = US_QUIET;
292 		while (update_status == US_QUIET)
293 			(void) pthread_cond_wait(&update_cv, &update_lock);
294 		update_status = US_INPROGRESS;
295 		(void) pthread_mutex_unlock(&update_lock);
296 		(void) rsrcinfo_update(&uc);
297 	}
298 }
299 
300 static void
301 request_update(void)
302 {
303 	(void) pthread_mutex_lock(&update_lock);
304 	if (update_status != US_QUIET) {
305 		(void) pthread_mutex_unlock(&update_lock);
306 		return;
307 	}
308 	update_status = US_NEEDED;
309 	(void) pthread_cond_signal(&update_cv);
310 	(void) pthread_mutex_unlock(&update_lock);
311 }
312 
313 /*ARGSUSED*/
314 static int
315 resource_compare_fmri(const void *l, const void *r, void *private)
316 {
317 	sunFmResource_data_t	*l_data = (sunFmResource_data_t *)l;
318 	sunFmResource_data_t	*r_data = (sunFmResource_data_t *)r;
319 
320 	ASSERT(l_data != NULL && r_data != NULL);
321 
322 	return (strcmp(l_data->d_ari_fmri, r_data->d_ari_fmri));
323 }
324 
325 /*ARGSUSED*/
326 static int
327 resource_compare_index(const void *l, const void *r, void *private)
328 {
329 	sunFmResource_data_t	*l_data = (sunFmResource_data_t *)l;
330 	sunFmResource_data_t	*r_data = (sunFmResource_data_t *)r;
331 
332 	ASSERT(l_data != NULL && r_data != NULL);
333 
334 	return (l_data->d_index < r_data->d_index ? -1 :
335 	    l_data->d_index > r_data->d_index ? 1 : 0);
336 }
337 
338 int
339 sunFmResourceTable_init(void)
340 {
341 	static oid sunFmResourceTable_oid[] = { SUNFMRESOURCETABLE_OID };
342 	static oid sunFmResourceCount_oid[] = { SUNFMRESOURCECOUNT_OID, 0 };
343 	netsnmp_table_registration_info *table_info;
344 	netsnmp_handler_registration *handler;
345 	int err;
346 
347 	if ((err = pthread_mutex_init(&update_lock, NULL)) != 0) {
348 		snmp_log(LOG_ERR, MODNAME_STR ": mutex_init failure: %s\n",
349 		    strerror(err));
350 		return (MIB_REGISTRATION_FAILED);
351 	}
352 	if ((err = pthread_cond_init(&update_cv, NULL)) != 0) {
353 		snmp_log(LOG_ERR, MODNAME_STR ": cond_init failure: %s\n",
354 		    strerror(err));
355 		return (MIB_REGISTRATION_FAILED);
356 	}
357 
358 	if ((err = pthread_create(NULL, NULL, (void *(*)(void *))update_thread,
359 	    NULL)) != 0) {
360 		snmp_log(LOG_ERR, MODNAME_STR ": error creating update "
361 		    "thread: %s\n", strerror(err));
362 		return (MIB_REGISTRATION_FAILED);
363 	}
364 
365 	if ((table_info =
366 	    SNMP_MALLOC_TYPEDEF(netsnmp_table_registration_info)) == NULL)
367 		return (MIB_REGISTRATION_FAILED);
368 
369 	if ((handler = netsnmp_create_handler_registration("sunFmResourceTable",
370 	    sunFmResourceTable_handler, sunFmResourceTable_oid,
371 	    OID_LENGTH(sunFmResourceTable_oid), HANDLER_CAN_RONLY)) == NULL) {
372 		SNMP_FREE(table_info);
373 		return (MIB_REGISTRATION_FAILED);
374 	}
375 
376 	/*
377 	 * The Net-SNMP template uses add_indexes here, but that
378 	 * function is unsafe because it does not check for failure.
379 	 */
380 	if (netsnmp_table_helper_add_index(table_info, ASN_UNSIGNED) == NULL) {
381 		SNMP_FREE(table_info);
382 		SNMP_FREE(handler);
383 		return (MIB_REGISTRATION_FAILED);
384 	}
385 
386 	table_info->min_column = SUNFMRESOURCE_COLMIN;
387 	table_info->max_column = SUNFMRESOURCE_COLMAX;
388 
389 	if ((rsrc_fmri_avl_pool = uu_avl_pool_create("rsrc_fmri",
390 	    sizeof (sunFmResource_data_t),
391 	    offsetof(sunFmResource_data_t, d_fmri_avl), resource_compare_fmri,
392 	    UU_AVL_DEBUG)) == NULL) {
393 		snmp_log(LOG_ERR, MODNAME_STR ": rsrc_fmri avl pool creation "
394 		    "failed: %s\n", uu_strerror(uu_error()));
395 		snmp_free_varbind(table_info->indexes);
396 		SNMP_FREE(table_info);
397 		SNMP_FREE(handler);
398 	}
399 
400 	if ((rsrc_fmri_avl = uu_avl_create(rsrc_fmri_avl_pool, NULL,
401 	    UU_AVL_DEBUG)) == NULL) {
402 		snmp_log(LOG_ERR, MODNAME_STR ": rsrc_fmri avl creation "
403 		    "failed: %s\n", uu_strerror(uu_error()));
404 		snmp_free_varbind(table_info->indexes);
405 		SNMP_FREE(table_info);
406 		SNMP_FREE(handler);
407 		uu_avl_pool_destroy(rsrc_fmri_avl_pool);
408 		return (MIB_REGISTRATION_FAILED);
409 	}
410 
411 	if ((rsrc_index_avl_pool = uu_avl_pool_create("rsrc_index",
412 	    sizeof (sunFmResource_data_t),
413 	    offsetof(sunFmResource_data_t, d_index_avl),
414 	    resource_compare_index, UU_AVL_DEBUG)) == NULL) {
415 		snmp_log(LOG_ERR, MODNAME_STR ": rsrc_index avl pool creation "
416 		    "failed: %s\n", uu_strerror(uu_error()));
417 		snmp_free_varbind(table_info->indexes);
418 		SNMP_FREE(table_info);
419 		SNMP_FREE(handler);
420 		uu_avl_destroy(rsrc_fmri_avl);
421 		uu_avl_pool_destroy(rsrc_fmri_avl_pool);
422 	}
423 
424 	if ((rsrc_index_avl = uu_avl_create(rsrc_index_avl_pool, NULL,
425 	    UU_AVL_DEBUG)) == NULL) {
426 		snmp_log(LOG_ERR, MODNAME_STR ": rsrc_index avl creation "
427 		    "failed: %s\n", uu_strerror(uu_error()));
428 		snmp_free_varbind(table_info->indexes);
429 		SNMP_FREE(table_info);
430 		SNMP_FREE(handler);
431 		uu_avl_destroy(rsrc_fmri_avl);
432 		uu_avl_pool_destroy(rsrc_fmri_avl_pool);
433 		uu_avl_pool_destroy(rsrc_index_avl_pool);
434 		return (MIB_REGISTRATION_FAILED);
435 	}
436 
437 	if ((err = netsnmp_register_table(handler, table_info)) !=
438 	    MIB_REGISTERED_OK) {
439 		snmp_free_varbind(table_info->indexes);
440 		SNMP_FREE(table_info);
441 		SNMP_FREE(handler);
442 		uu_avl_destroy(rsrc_fmri_avl);
443 		uu_avl_pool_destroy(rsrc_fmri_avl_pool);
444 		uu_avl_destroy(rsrc_index_avl);
445 		uu_avl_pool_destroy(rsrc_index_avl_pool);
446 		return (err);
447 	}
448 
449 	if ((err = netsnmp_register_read_only_instance(
450 	    netsnmp_create_handler_registration("sunFmResourceCount",
451 		sunFmResourceCount_handler, sunFmResourceCount_oid,
452 		OID_LENGTH(sunFmResourceCount_oid), HANDLER_CAN_RONLY))) !=
453 	    MIB_REGISTERED_OK) {
454 		/*
455 		 * There's no way to unregister the table handler, so we
456 		 * can't free any of the data, either.
457 		 */
458 		return (err);
459 	}
460 
461 	return (MIB_REGISTERED_OK);
462 }
463 
464 /*
465  * These two functions form the core of GET/GETNEXT/GETBULK handling (the
466  * only kind we do).  They perform two functions:
467  *
468  * - First, frob the request to set all the index variables to correspond
469  *   to the value that's going to be returned.  For GET, this is a nop;
470  *   for GETNEXT/GETBULK it always requires some work.
471  * - Second, find and return the fmd resource information corresponding to
472  *   the (possibly updated) indices.
473  *
474  * These should be as fast as possible; they run in the agent thread.
475  */
476 static sunFmResource_data_t *
477 sunFmResourceTable_nextrsrc(netsnmp_handler_registration *reginfo,
478     netsnmp_table_request_info *table_info)
479 {
480 	sunFmResource_data_t	*data;
481 	netsnmp_variable_list	*var;
482 	ulong_t index;
483 
484 	/*
485 	 * If we have no index, we must make one.
486 	 */
487 	if (table_info->number_indexes < 1) {
488 		oid tmpoid[MAX_OID_LEN];
489 		index = 1;
490 
491 		DEBUGMSGTL((MODNAME_STR, "nextrsrc: no indexes given\n"));
492 		var = SNMP_MALLOC_TYPEDEF(netsnmp_variable_list);
493 		snmp_set_var_typed_value(var, ASN_UNSIGNED, (uchar_t *)&index,
494 		    sizeof (index));
495 		(void) memcpy(tmpoid, reginfo->rootoid,
496 		    reginfo->rootoid_len * sizeof (oid));
497 		tmpoid[reginfo->rootoid_len] = 1;
498 		tmpoid[reginfo->rootoid_len + 1] = table_info->colnum;
499 		if (build_oid(&var->name, &var->name_length, tmpoid,
500 		    reginfo->rootoid_len + 2, var) != SNMPERR_SUCCESS) {
501 			snmp_free_varbind(var);
502 			return (NULL);
503 		}
504 		DEBUGMSGTL((MODNAME_STR, "nextrsrc: built fake index:\n"));
505 		DEBUGMSGVAR((MODNAME_STR, var));
506 		DEBUGMSG((MODNAME_STR, "\n"));
507 	} else {
508 		var = snmp_clone_varbind(table_info->indexes);
509 		index = *var->val.integer;
510 		DEBUGMSGTL((MODNAME_STR, "nextrsrc: received index:\n"));
511 		DEBUGMSGVAR((MODNAME_STR, var));
512 		DEBUGMSG((MODNAME_STR, "\n"));
513 		index++;
514 	}
515 
516 	snmp_free_varbind(table_info->indexes);
517 	table_info->indexes = NULL;
518 	table_info->number_indexes = 0;
519 
520 	if ((data = resource_lookup_index_nextvalid(index)) == NULL) {
521 		DEBUGMSGTL((MODNAME_STR, "nextrsrc: exact match not found for "
522 		    "index %lu; trying next column\n", index));
523 		if (table_info->colnum >=
524 		    netsnmp_find_table_registration_info(reginfo)->max_column) {
525 			snmp_free_varbind(var);
526 			DEBUGMSGTL((MODNAME_STR, "nextrsrc: out of columns\n"));
527 			return (NULL);
528 		}
529 		table_info->colnum++;
530 		index = 1;
531 
532 		data = resource_lookup_index_nextvalid(index);
533 	}
534 
535 	if (data == NULL) {
536 		DEBUGMSGTL((MODNAME_STR, "nextrsrc: exact match not found for "
537 		    "index %lu; stopping\n", index));
538 		snmp_free_varbind(var);
539 		return (NULL);
540 	}
541 
542 	*var->val.integer = data->d_index;
543 	table_info->indexes = var;
544 	table_info->number_indexes = 1;
545 
546 	DEBUGMSGTL((MODNAME_STR, "matching data is %lu/%s@%p\n", data->d_index,
547 	    data->d_ari_fmri, data));
548 
549 	return (data);
550 }
551 
552 /*ARGSUSED*/
553 static sunFmResource_data_t *
554 sunFmResourceTable_rsrc(netsnmp_handler_registration *reginfo,
555     netsnmp_table_request_info *table_info)
556 {
557 	ASSERT(table_info->number_indexes == 1);
558 
559 	return (resource_lookup_index_exact(table_info->index_oid[0]));
560 }
561 
562 /*ARGSUSED*/
563 static void
564 sunFmResourceTable_return(unsigned int reg, void *arg)
565 {
566 	netsnmp_delegated_cache		*cache = (netsnmp_delegated_cache *)arg;
567 	netsnmp_request_info		*request;
568 	netsnmp_agent_request_info	*reqinfo;
569 	netsnmp_handler_registration	*reginfo;
570 	netsnmp_table_request_info	*table_info;
571 	sunFmResource_data_t		*data;
572 	ulong_t				rsrcstate;
573 
574 	ASSERT(netsnmp_handler_check_cache(cache) != NULL);
575 
576 	(void) pthread_mutex_lock(&update_lock);
577 	if (update_status != US_QUIET) {
578 		struct timeval			tv;
579 
580 		tv.tv_sec = UPDATE_WAIT_MILLIS / 1000;
581 		tv.tv_usec = (UPDATE_WAIT_MILLIS % 1000) * 1000;
582 
583 		(void) snmp_alarm_register_hr(tv, 0, sunFmResourceTable_return,
584 		    cache);
585 		(void) pthread_mutex_unlock(&update_lock);
586 		return;
587 	}
588 
589 	request = cache->requests;
590 	reqinfo = cache->reqinfo;
591 	reginfo = cache->reginfo;
592 
593 	table_info = netsnmp_extract_table_info(request);
594 	request->delegated = 0;
595 
596 	ASSERT(table_info->colnum >= SUNFMRESOURCE_COLMIN);
597 	ASSERT(table_info->colnum <= SUNFMRESOURCE_COLMAX);
598 
599 	/*
600 	 * table_info->colnum contains the column number requested.
601 	 * table_info->indexes contains a linked list of snmp variable
602 	 * bindings for the indexes of the table.  Values in the list
603 	 * have been set corresponding to the indexes of the
604 	 * request.  We have other guarantees as well:
605 	 *
606 	 * - The column number is always within range.
607 	 * - If we have no index data, table_info->index_oid_len is 0.
608 	 * - We will never receive requests outside our table nor
609 	 *   those with the first subid anything other than 1 (Entry)
610 	 *   nor those without a column number.  This is true even
611 	 *   for GETNEXT requests.
612 	 */
613 
614 	switch (reqinfo->mode) {
615 	case MODE_GET:
616 		if ((data = sunFmResourceTable_rsrc(reginfo, table_info)) ==
617 		    NULL) {
618 			netsnmp_free_delegated_cache(cache);
619 			(void) pthread_mutex_unlock(&update_lock);
620 			return;
621 		}
622 		break;
623 	case MODE_GETNEXT:
624 	case MODE_GETBULK:
625 		if ((data = sunFmResourceTable_nextrsrc(reginfo, table_info)) ==
626 		    NULL) {
627 			netsnmp_free_delegated_cache(cache);
628 			(void) pthread_mutex_unlock(&update_lock);
629 			return;
630 		}
631 		break;
632 	default:
633 		snmp_log(LOG_ERR, MODNAME_STR ": Unsupported request mode %d\n",
634 		    reqinfo->mode);
635 		netsnmp_free_delegated_cache(cache);
636 		(void) pthread_mutex_unlock(&update_lock);
637 		return;
638 	}
639 
640 	switch (table_info->colnum) {
641 	case SUNFMRESOURCE_COL_FMRI:
642 		netsnmp_table_build_result(reginfo, request, table_info,
643 		    ASN_OCTET_STR, (uchar_t *)data->d_ari_fmri,
644 		    strlen(data->d_ari_fmri));
645 		break;
646 	case SUNFMRESOURCE_COL_STATUS:
647 		switch (data->d_ari_flags &
648 		    (FMD_ADM_RSRC_FAULTY|FMD_ADM_RSRC_UNUSABLE)) {
649 		default:
650 			rsrcstate = SUNFMRESOURCE_STATE_OK;
651 			break;
652 		case FMD_ADM_RSRC_FAULTY:
653 			rsrcstate = SUNFMRESOURCE_STATE_DEGRADED;
654 			break;
655 		case FMD_ADM_RSRC_UNUSABLE:
656 			rsrcstate = SUNFMRESOURCE_STATE_UNKNOWN;
657 			break;
658 		case FMD_ADM_RSRC_FAULTY | FMD_ADM_RSRC_UNUSABLE:
659 			rsrcstate = SUNFMRESOURCE_STATE_FAULTED;
660 			break;
661 		}
662 		netsnmp_table_build_result(reginfo, request, table_info,
663 		    ASN_INTEGER, (uchar_t *)&rsrcstate,
664 		    sizeof (rsrcstate));
665 		break;
666 	case SUNFMRESOURCE_COL_DIAGNOSISUUID:
667 		netsnmp_table_build_result(reginfo, request, table_info,
668 		    ASN_OCTET_STR, (uchar_t *)data->d_ari_case,
669 		    strlen(data->d_ari_case));
670 		break;
671 	default:
672 		break;
673 	}
674 	netsnmp_free_delegated_cache(cache);
675 	(void) pthread_mutex_unlock(&update_lock);
676 }
677 
678 static int
679 sunFmResourceTable_handler(netsnmp_mib_handler *handler,
680     netsnmp_handler_registration *reginfo, netsnmp_agent_request_info *reqinfo,
681     netsnmp_request_info *requests)
682 {
683 	netsnmp_request_info		*request;
684 	struct timeval			tv;
685 
686 	tv.tv_sec = UPDATE_WAIT_MILLIS / 1000;
687 	tv.tv_usec = (UPDATE_WAIT_MILLIS % 1000) * 1000;
688 
689 	request_update();
690 
691 	for (request = requests; request; request = request->next) {
692 		if (request->processed != 0)
693 			continue;
694 
695 		if (netsnmp_extract_table_info(request) == NULL)
696 			continue;
697 
698 		request->delegated = 1;
699 		(void) snmp_alarm_register_hr(tv, 0, sunFmResourceTable_return,
700 		    (void *) netsnmp_create_delegated_cache(handler, reginfo,
701 		    reqinfo, request, NULL));
702 	}
703 
704 	return (SNMP_ERR_NOERROR);
705 }
706 
707 /*ARGSUSED*/
708 static void
709 sunFmResourceCount_return(unsigned int reg, void *arg)
710 {
711 	netsnmp_delegated_cache		*cache = (netsnmp_delegated_cache *)arg;
712 	netsnmp_request_info		*request;
713 	netsnmp_agent_request_info	*reqinfo;
714 	ulong_t				rsrc_count_long;
715 
716 	ASSERT(netsnmp_handler_check_cache(cache) != NULL);
717 
718 	(void) pthread_mutex_lock(&update_lock);
719 	if (update_status != US_QUIET) {
720 		struct timeval	tv;
721 
722 		tv.tv_sec = UPDATE_WAIT_MILLIS / 1000;
723 		tv.tv_usec = (UPDATE_WAIT_MILLIS % 1000) * 1000;
724 
725 		(void) snmp_alarm_register_hr(tv, 0, sunFmResourceCount_return,
726 		    cache);
727 		(void) pthread_mutex_unlock(&update_lock);
728 		return;
729 	}
730 
731 	request = cache->requests;
732 	reqinfo = cache->reqinfo;
733 
734 	request->delegated = 0;
735 
736 	switch (reqinfo->mode) {
737 	/*
738 	 * According to the documentation, it's not possible for us ever to
739 	 * be called with MODE_GETNEXT.  However, Net-SNMP does the following:
740 	 * - set reqinfo->mode to MODE_GET
741 	 * - invoke the handler
742 	 * - set reqinfo->mode to MODE_GETNEXT (even if the request was not
743 	 *   actually processed; i.e. it's been delegated)
744 	 * Since we're called back later with the same reqinfo, we see
745 	 * GETNEXT.  Therefore this case is needed to work around the
746 	 * Net-SNMP bug.
747 	 */
748 	case MODE_GET:
749 	case MODE_GETNEXT:
750 		DEBUGMSGTL((MODNAME_STR, "resource count is %u\n", rsrc_count));
751 		rsrc_count_long = (ulong_t)rsrc_count;
752 		snmp_set_var_typed_value(request->requestvb, ASN_GAUGE,
753 		    (uchar_t *)&rsrc_count_long, sizeof (rsrc_count_long));
754 		break;
755 	default:
756 		snmp_log(LOG_ERR, MODNAME_STR ": Unsupported request mode %d\n",
757 		    reqinfo->mode);
758 	}
759 
760 	netsnmp_free_delegated_cache(cache);
761 	(void) pthread_mutex_unlock(&update_lock);
762 }
763 
764 static int
765 sunFmResourceCount_handler(netsnmp_mib_handler *handler,
766     netsnmp_handler_registration *reginfo, netsnmp_agent_request_info *reqinfo,
767     netsnmp_request_info *requests)
768 {
769 	struct timeval	tv;
770 
771 	tv.tv_sec = UPDATE_WAIT_MILLIS / 1000;
772 	tv.tv_usec = (UPDATE_WAIT_MILLIS % 1000) * 1000;
773 
774 	request_update();
775 
776 	/*
777 	 * We are never called for a GETNEXT when registered as an
778 	 * instance; it's handled for us and converted to a GET.
779 	 * Also, an instance handler is given only one request at a time, so
780 	 * we don't need to loop over a list of requests.
781 	 */
782 
783 	if (requests->processed != 0)
784 		return (SNMP_ERR_NOERROR);
785 
786 	requests->delegated = 1;
787 	(void) snmp_alarm_register_hr(tv, 0, sunFmResourceCount_return,
788 	    (void *) netsnmp_create_delegated_cache(handler, reginfo,
789 	    reqinfo, requests, NULL));
790 
791 	return (SNMP_ERR_NOERROR);
792 }
793