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