xref: /titanic_51/usr/src/lib/fm/libfmd_snmp/common/module.c (revision 23c352973f956f97f817e65150aad7e1cebeb228)
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 "module.h"
40 
41 static uu_avl_pool_t	*mod_name_avl_pool;
42 static uu_avl_pool_t	*mod_index_avl_pool;
43 static uu_avl_t		*mod_name_avl;
44 static uu_avl_t		*mod_index_avl;
45 
46 #define	VALID_AVL_STATE	(mod_name_avl_pool != NULL &&		\
47 	mod_index_avl_pool != NULL && mod_name_avl != NULL &&	\
48 	mod_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.
54  */
55 #define	UCT_INDEX	0x1
56 #define	UCT_ALL		0x2
57 #define	UCT_FLAGS	0x3
58 
59 #define	MODULE_DATA_VALID(d)	((d)->d_valid == valid_stamp)
60 
61 /*
62  * Locking rules are straightforward.  There is only one updater thread
63  * for each table, and requests for update that are received while
64  * another update is in progress are ignored.  The single per-table lock
65  * protects all the data for the table, the valid_stamp and max_index
66  * tags for new data, and - importantly - all the hidden static data
67  * used by the Net-SNMP library.  The result return callbacks are always
68  * called in the master agent thread; holding the table lock is
69  * therefore sufficient since only one table's callback can be run at
70  * any one time.  Finer-grained locking is possible here but
71  * substantially more difficult because nearly all Net-SNMP functions
72  * are unsafe.
73  *
74  * In practice this is more than adequate, since the purpose of
75  * threading out the update is to prevent excessively time-consuming
76  * data collection from bottlenecking the entire agent, not to improve
77  * result throughput (SNMP is not intended to be used for applications
78  * requiring high throughput anyway).  If the agent itself ever becomes
79  * multithreaded, locking requirements become limited to our local
80  * per-table data (the tree, max_index, and valid_stamp), and the
81  * implementation could be revisited for better performance.
82  */
83 
84 static ulong_t		max_index;
85 static int		valid_stamp;
86 static pthread_mutex_t	update_lock;
87 static pthread_cond_t	update_cv;
88 static volatile enum { US_QUIET, US_NEEDED, US_INPROGRESS } update_status;
89 
90 static Netsnmp_Node_Handler	sunFmModuleTable_handler;
91 
92 static sunFmModule_data_t *
93 key_build(const char *name, const ulong_t index)
94 {
95 	static sunFmModule_data_t	key;
96 
97 	key.d_index = index;
98 	if (name)
99 		(void) strlcpy(key.d_ami_name, name, sizeof (key.d_ami_name));
100 	else
101 		key.d_ami_name[0] = '\0';
102 
103 	return (&key);
104 }
105 
106 /*
107  * If name is the name of a module we have previously seen and indexed, return
108  * data for it.  Otherwise, return NULL.  Note that the module may not be
109  * valid; that is, it may have been removed from the fault manager since its
110  * information was last updated.
111  */
112 static sunFmModule_data_t *
113 module_lookup_name(const char *name)
114 {
115 	sunFmModule_data_t	*key;
116 
117 	key = key_build(name, 0);
118 	return (uu_avl_find(mod_name_avl, key, NULL, NULL));
119 }
120 
121 /*
122  * If index corresponds to a module we have previously seen and indexed, return
123  * data for it.  Otherwise, return NULL.  Note that the module may not be
124  * valid; that is, it may have been removed from the fault manager since its
125  * information was last updated.
126  */
127 static sunFmModule_data_t *
128 module_lookup_index_exact(const ulong_t index)
129 {
130 	sunFmModule_data_t	*key;
131 
132 	key = key_build(NULL, index);
133 	return (uu_avl_find(mod_index_avl, key, NULL, NULL));
134 }
135 
136 /*
137  * If index corresponds to a valid (that is, extant as of latest information
138  * from the fault manager) fmd module, return the data for that module.
139  * Otherwise, return the data for the valid module whose index is as close as
140  * possible to index but not lower.  This preserves the lexicographical
141  * ordering required for GETNEXT processing.
142  */
143 static sunFmModule_data_t *
144 module_lookup_index_nextvalid(const ulong_t index)
145 {
146 	sunFmModule_data_t	*key, *data;
147 	uu_avl_index_t		idx;
148 
149 	key = key_build(NULL, index);
150 
151 	if ((data = uu_avl_find(mod_index_avl, key, NULL, &idx)) != NULL &&
152 	    MODULE_DATA_VALID(data))
153 		return (data);
154 
155 	data = uu_avl_nearest_next(mod_index_avl, idx);
156 
157 	while (data != NULL && !MODULE_DATA_VALID(data)) {
158 		(void) uu_avl_find(mod_index_avl, data, NULL, &idx);
159 		data = uu_avl_nearest_next(mod_index_avl, idx);
160 	}
161 
162 	return (data);
163 }
164 
165 /*
166  * Possible update the contents of a single module within the cache.  This
167  * is our callback from fmd_module_iter.
168  */
169 static int
170 modinfo_update_one(const fmd_adm_modinfo_t *modinfo, void *arg)
171 {
172 	const sunFmModule_update_ctx_t *update_ctx =
173 	    (sunFmModule_update_ctx_t *)arg;
174 	sunFmModule_data_t *data = module_lookup_name(modinfo->ami_name);
175 
176 	/*
177 	 * An fmd module we haven't seen before.  We're obligated to index
178 	 * it and link it into our cache so that we can find it, but we're
179 	 * not obligated to fill it in completely unless we're doing a
180 	 * thorough update or this is the module we were asked for.  This
181 	 * avoids unnecessary iteration and memory manipulation for data
182 	 * we're not going to return for this request.
183 	 */
184 	if (data == NULL) {
185 		uu_avl_index_t idx;
186 
187 		DEBUGMSGTL((MODNAME_STR, "found new fmd module %s\n",
188 		    modinfo->ami_name));
189 		if ((data = SNMP_MALLOC_TYPEDEF(sunFmModule_data_t)) == NULL) {
190 			snmp_log(LOG_ERR, MODNAME_STR ": Out of memory for "
191 			    "new module data at %s:%d\n", __FILE__, __LINE__);
192 			return (1);
193 		}
194 		/*
195 		 * We allocate indices sequentially and never reuse them.
196 		 * This ensures we can always return valid GETNEXT responses
197 		 * without having to reindex, and it provides the user a
198 		 * more consistent view of the fault manager.
199 		 */
200 		data->d_index = ++max_index;
201 		DEBUGMSGTL((MODNAME_STR, "index %lu is %s@%p\n", data->d_index,
202 		    modinfo->ami_name, data));
203 
204 		(void) strlcpy(data->d_ami_name, modinfo->ami_name,
205 		    sizeof (data->d_ami_name));
206 
207 		uu_avl_node_init(data, &data->d_name_avl, mod_name_avl_pool);
208 		(void) uu_avl_find(mod_name_avl, data, NULL, &idx);
209 		uu_avl_insert(mod_name_avl, data, idx);
210 
211 		uu_avl_node_init(data, &data->d_index_avl, mod_index_avl_pool);
212 		(void) uu_avl_find(mod_index_avl, data, NULL, &idx);
213 		uu_avl_insert(mod_index_avl, data, idx);
214 
215 		DEBUGMSGTL((MODNAME_STR, "completed new module %lu/%s@%p\n",
216 		    data->d_index, data->d_ami_name, data));
217 	}
218 
219 	data->d_valid = valid_stamp;
220 
221 	DEBUGMSGTL((MODNAME_STR, "timestamp updated for %lu/%s@%p: %lu\n",
222 	    data->d_index, data->d_ami_name, data, data->d_valid));
223 
224 	if ((update_ctx->uc_type & UCT_ALL) ||
225 	    update_ctx->uc_index == data->d_index) {
226 		(void) strlcpy(data->d_ami_vers, modinfo->ami_vers,
227 		    sizeof (data->d_ami_vers));
228 		(void) strlcpy(data->d_ami_desc, modinfo->ami_desc,
229 		    sizeof (data->d_ami_desc));
230 		data->d_ami_flags = modinfo->ami_flags;
231 	}
232 
233 	return (!(update_ctx->uc_type & UCT_ALL) &&
234 	    update_ctx->uc_index == data->d_index);
235 }
236 
237 /*
238  * Update some or all module data from fmd.  If thorough is set, all modules
239  * will be indexed and their data cached.  Otherwise, updates will stop once
240  * the module matching index has been updated.
241  *
242  * Returns appropriate SNMP error codes.
243  */
244 static int
245 modinfo_update(sunFmModule_update_ctx_t *update_ctx)
246 {
247 	fmd_adm_t *adm;
248 
249 	ASSERT(update_ctx != NULL);
250 	ASSERT((update_ctx->uc_type & (UCT_INDEX|UCT_ALL)) !=
251 	    (UCT_INDEX|UCT_ALL));
252 	ASSERT((update_ctx->uc_type & ~UCT_FLAGS) == 0);
253 	ASSERT(VALID_AVL_STATE);
254 
255 	if ((adm = fmd_adm_open(update_ctx->uc_host, update_ctx->uc_prog,
256 	    update_ctx->uc_version)) == NULL) {
257 		snmp_log(LOG_ERR, MODNAME_STR ": Communication with fmd "
258 		    "failed: %s\n", strerror(errno));
259 		return (SNMP_ERR_RESOURCEUNAVAILABLE);
260 	}
261 
262 	++valid_stamp;
263 	if (fmd_adm_module_iter(adm, modinfo_update_one, update_ctx) != 0) {
264 		snmp_log(LOG_ERR, MODNAME_STR ": fmd module information update "
265 		    "failed: %s\n", fmd_adm_errmsg(adm));
266 		fmd_adm_close(adm);
267 		return (SNMP_ERR_RESOURCEUNAVAILABLE);
268 	}
269 
270 	DEBUGMSGTL((MODNAME_STR, "module iteration completed\n"));
271 
272 	fmd_adm_close(adm);
273 	return (SNMP_ERR_NOERROR);
274 }
275 
276 /*ARGSUSED*/
277 static void
278 update_thread(void *arg)
279 {
280 	/*
281 	 * The current modinfo_update implementation offers minimal savings
282 	 * for the use of index-only updates; therefore we always do a full
283 	 * update.  If it becomes advantageous to limit updates to a single
284 	 * index, the contexts can be queued by the handler instead.
285 	 */
286 	sunFmModule_update_ctx_t	uc;
287 
288 	uc.uc_host = NULL;
289 	uc.uc_prog = FMD_ADM_PROGRAM;
290 	uc.uc_version = FMD_ADM_VERSION;
291 
292 	uc.uc_index = 0;
293 	uc.uc_type = UCT_ALL;
294 
295 	for (;;) {
296 		(void) pthread_mutex_lock(&update_lock);
297 		update_status = US_QUIET;
298 		while (update_status == US_QUIET)
299 			(void) pthread_cond_wait(&update_cv, &update_lock);
300 		update_status = US_INPROGRESS;
301 		(void) pthread_mutex_unlock(&update_lock);
302 		(void) modinfo_update(&uc);
303 	}
304 }
305 
306 static void
307 request_update(void)
308 {
309 	(void) pthread_mutex_lock(&update_lock);
310 	if (update_status != US_QUIET) {
311 		(void) pthread_mutex_unlock(&update_lock);
312 		return;
313 	}
314 	update_status = US_NEEDED;
315 	(void) pthread_cond_signal(&update_cv);
316 	(void) pthread_mutex_unlock(&update_lock);
317 }
318 
319 /*ARGSUSED*/
320 static int
321 module_compare_name(const void *l, const void *r, void *private)
322 {
323 	sunFmModule_data_t	*l_data = (sunFmModule_data_t *)l;
324 	sunFmModule_data_t	*r_data = (sunFmModule_data_t *)r;
325 
326 	ASSERT(l_data != NULL && r_data != NULL);
327 
328 	return (strcmp(l_data->d_ami_name, r_data->d_ami_name));
329 }
330 
331 /*ARGSUSED*/
332 static int
333 module_compare_index(const void *l, const void *r, void *private)
334 {
335 	sunFmModule_data_t	*l_data = (sunFmModule_data_t *)l;
336 	sunFmModule_data_t	*r_data = (sunFmModule_data_t *)r;
337 
338 	ASSERT(l_data != NULL && r_data != NULL);
339 
340 	return (l_data->d_index < r_data->d_index ? -1 :
341 	    l_data->d_index > r_data->d_index ? 1 : 0);
342 }
343 
344 int
345 sunFmModuleTable_init(void)
346 {
347 	static oid sunFmModuleTable_oid[] = { SUNFMMODULETABLE_OID };
348 	netsnmp_table_registration_info *table_info;
349 	netsnmp_handler_registration *handler;
350 	int err;
351 
352 	if ((err = pthread_mutex_init(&update_lock, NULL)) != 0) {
353 		snmp_log(LOG_ERR, MODNAME_STR ": mutex_init failure: %s\n",
354 		    strerror(err));
355 		return (MIB_REGISTRATION_FAILED);
356 	}
357 	if ((err = pthread_cond_init(&update_cv, NULL)) != 0) {
358 		snmp_log(LOG_ERR, MODNAME_STR ": cond_init failure: %s\n",
359 		    strerror(err));
360 		return (MIB_REGISTRATION_FAILED);
361 	}
362 
363 	if ((err = pthread_create(NULL, NULL, (void *(*)(void *))update_thread,
364 	    NULL)) != 0) {
365 		snmp_log(LOG_ERR, MODNAME_STR ": error creating update "
366 		    "thread: %s\n", strerror(err));
367 		return (MIB_REGISTRATION_FAILED);
368 	}
369 
370 	if ((table_info =
371 	    SNMP_MALLOC_TYPEDEF(netsnmp_table_registration_info)) == NULL)
372 		return (MIB_REGISTRATION_FAILED);
373 
374 	if ((handler = netsnmp_create_handler_registration("sunFmModuleTable",
375 	    sunFmModuleTable_handler, sunFmModuleTable_oid,
376 	    OID_LENGTH(sunFmModuleTable_oid), HANDLER_CAN_RONLY)) == NULL) {
377 		SNMP_FREE(table_info);
378 		return (MIB_REGISTRATION_FAILED);
379 	}
380 
381 	/*
382 	 * The Net-SNMP template uses add_indexes here, but that
383 	 * function is unsafe because it does not check for failure.
384 	 */
385 	if (netsnmp_table_helper_add_index(table_info, ASN_UNSIGNED) == NULL) {
386 		SNMP_FREE(table_info);
387 		SNMP_FREE(handler);
388 		return (MIB_REGISTRATION_FAILED);
389 	}
390 
391 	table_info->min_column = SUNFMMODULE_COLMIN;
392 	table_info->max_column = SUNFMMODULE_COLMAX;
393 
394 	if ((mod_name_avl_pool = uu_avl_pool_create("mod_name",
395 	    sizeof (sunFmModule_data_t),
396 	    offsetof(sunFmModule_data_t, d_name_avl), module_compare_name,
397 	    UU_AVL_DEBUG)) == NULL) {
398 		snmp_free_varbind(table_info->indexes);
399 		SNMP_FREE(table_info);
400 		SNMP_FREE(handler);
401 	}
402 
403 	if ((mod_name_avl = uu_avl_create(mod_name_avl_pool, NULL,
404 	    UU_AVL_DEBUG)) == NULL) {
405 		snmp_log(LOG_ERR, MODNAME_STR ": mod_name_avl creation "
406 		    "failed: %s\n", uu_strerror(uu_error()));
407 		snmp_free_varbind(table_info->indexes);
408 		SNMP_FREE(table_info);
409 		SNMP_FREE(handler);
410 		uu_avl_pool_destroy(mod_name_avl_pool);
411 		return (MIB_REGISTRATION_FAILED);
412 	}
413 
414 	if ((mod_index_avl_pool = uu_avl_pool_create("mod_index",
415 	    sizeof (sunFmModule_data_t),
416 	    offsetof(sunFmModule_data_t, d_index_avl),
417 	    module_compare_index, UU_AVL_DEBUG)) == NULL) {
418 		snmp_free_varbind(table_info->indexes);
419 		SNMP_FREE(table_info);
420 		SNMP_FREE(handler);
421 		uu_avl_destroy(mod_name_avl);
422 		uu_avl_pool_destroy(mod_name_avl_pool);
423 	}
424 
425 	if ((mod_index_avl = uu_avl_create(mod_index_avl_pool, NULL,
426 	    UU_AVL_DEBUG)) == NULL) {
427 		snmp_log(LOG_ERR, MODNAME_STR ": mod_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(mod_name_avl);
433 		uu_avl_pool_destroy(mod_name_avl_pool);
434 		uu_avl_pool_destroy(mod_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(mod_name_avl);
444 		uu_avl_pool_destroy(mod_name_avl_pool);
445 		uu_avl_destroy(mod_index_avl);
446 		uu_avl_pool_destroy(mod_index_avl_pool);
447 		return (err);
448 	}
449 
450 	return (MIB_REGISTERED_OK);
451 }
452 
453 /*
454  * These two functions form the core of GET/GETNEXT/GETBULK handling (the
455  * only kind we do).  They perform two functions:
456  *
457  * - First, frob the request to set all the index variables to correspond
458  *   to the value that's going to be returned.  For GET, this is a nop;
459  *   for GETNEXT/GETBULK it always requires some work.
460  * - Second, find and return the fmd module information corresponding to
461  *   the (possibly updated) indices.
462  *
463  * These should be as fast as possible; they run in the agent thread.
464  */
465 static sunFmModule_data_t *
466 sunFmModuleTable_nextmod(netsnmp_handler_registration *reginfo,
467     netsnmp_table_request_info *table_info)
468 {
469 	sunFmModule_data_t	*data;
470 	netsnmp_variable_list	*var;
471 	ulong_t index;
472 
473 	/*
474 	 * If we have no index, we must make one.
475 	 */
476 	if (table_info->number_indexes < 1) {
477 		oid tmpoid[MAX_OID_LEN];
478 		index = 1;
479 
480 		DEBUGMSGTL((MODNAME_STR, "nextmod: no indexes given\n"));
481 		var = SNMP_MALLOC_TYPEDEF(netsnmp_variable_list);
482 		snmp_set_var_typed_value(var, ASN_UNSIGNED, (uchar_t *)&index,
483 		    sizeof (index));
484 		(void) memcpy(tmpoid, reginfo->rootoid,
485 		    reginfo->rootoid_len * sizeof (oid));
486 		tmpoid[reginfo->rootoid_len] = 1;	/* Entry is .1 */
487 		tmpoid[reginfo->rootoid_len + 1] = table_info->colnum;
488 		if (build_oid(&var->name, &var->name_length, tmpoid,
489 		    reginfo->rootoid_len + 2, var) != SNMPERR_SUCCESS) {
490 			snmp_free_varbind(var);
491 			return (NULL);
492 		}
493 		DEBUGMSGTL((MODNAME_STR, "nextmod: built fake index:\n"));
494 		DEBUGMSGVAR((MODNAME_STR, var));
495 		DEBUGMSG((MODNAME_STR, "\n"));
496 	} else {
497 		var = snmp_clone_varbind(table_info->indexes);
498 		index = *var->val.integer;
499 		DEBUGMSGTL((MODNAME_STR, "nextmod: received index:\n"));
500 		DEBUGMSGVAR((MODNAME_STR, var));
501 		DEBUGMSG((MODNAME_STR, "\n"));
502 		index++;
503 	}
504 
505 	snmp_free_varbind(table_info->indexes);
506 	table_info->indexes = NULL;
507 	table_info->number_indexes = 0;
508 
509 	if ((data = module_lookup_index_nextvalid(index)) == NULL) {
510 		DEBUGMSGTL((MODNAME_STR, "nextmod: exact match not found for "
511 		    "index %lu; trying next column\n", index));
512 		if (table_info->colnum >=
513 		    netsnmp_find_table_registration_info(reginfo)->max_column) {
514 			snmp_free_varbind(var);
515 			DEBUGMSGTL((MODNAME_STR, "nextmod: out of columns\n"));
516 			return (NULL);
517 		}
518 		table_info->colnum++;
519 		index = 1;
520 
521 		data = module_lookup_index_nextvalid(index);
522 	}
523 
524 	if (data == NULL) {
525 		DEBUGMSGTL((MODNAME_STR, "nextmod: exact match not found for "
526 		    "index %lu; stopping\n", index));
527 		snmp_free_varbind(var);
528 		return (NULL);
529 	}
530 
531 	*var->val.integer = index;
532 	table_info->indexes = var;
533 	table_info->number_indexes = 1;
534 
535 	DEBUGMSGTL((MODNAME_STR, "matching data is %lu/%s@%p\n", data->d_index,
536 	    data->d_ami_name, data));
537 
538 	return (data);
539 }
540 
541 /*ARGSUSED*/
542 static sunFmModule_data_t *
543 sunFmModuleTable_mod(netsnmp_handler_registration *reginfo,
544     netsnmp_table_request_info *table_info)
545 {
546 	ASSERT(table_info->number_indexes == 1);
547 
548 	return (module_lookup_index_exact(table_info->index_oid[0]));
549 }
550 
551 /*ARGSUSED*/
552 static void
553 sunFmModuleTable_return(unsigned int reg, void *arg)
554 {
555 	netsnmp_delegated_cache		*cache = (netsnmp_delegated_cache *)arg;
556 	netsnmp_request_info		*request;
557 	netsnmp_agent_request_info	*reqinfo;
558 	netsnmp_handler_registration	*reginfo;
559 	netsnmp_table_request_info	*table_info;
560 	sunFmModule_data_t		*data;
561 	ulong_t				modstate;
562 
563 	ASSERT(netsnmp_handler_check_cache(cache) != NULL);
564 
565 	(void) pthread_mutex_lock(&update_lock);
566 	if (update_status != US_QUIET) {
567 		struct timeval			tv;
568 
569 		tv.tv_sec = UPDATE_WAIT_MILLIS / 1000;
570 		tv.tv_usec = (UPDATE_WAIT_MILLIS % 1000) * 1000;
571 
572 		(void) snmp_alarm_register_hr(tv, 0, sunFmModuleTable_return,
573 		    cache);
574 		(void) pthread_mutex_unlock(&update_lock);
575 		return;
576 	}
577 
578 	request = cache->requests;
579 	reqinfo = cache->reqinfo;
580 	reginfo = cache->reginfo;
581 
582 	table_info = netsnmp_extract_table_info(request);
583 	request->delegated = 0;
584 
585 	ASSERT(table_info->colnum >= SUNFMMODULE_COLMIN);
586 	ASSERT(table_info->colnum <= SUNFMMODULE_COLMAX);
587 
588 	/*
589 	 * table_info->colnum contains the column number requested.
590 	 * table_info->indexes contains a linked list of snmp variable
591 	 * bindings for the indexes of the table.  Values in the list
592 	 * have been set corresponding to the indexes of the
593 	 * request.  We have other guarantees as well:
594 	 *
595 	 * - The column number is always within range.
596 	 * - If we have no index data, table_info->index_oid_len is 0.
597 	 * - We will never receive requests outside our table nor
598 	 *   those with the first subid anything other than 1 (Entry)
599 	 *   nor those without a column number.  This is true even
600 	 *   for GETNEXT requests.
601 	 */
602 
603 	switch (reqinfo->mode) {
604 	case MODE_GET:
605 		if ((data = sunFmModuleTable_mod(reginfo, table_info)) ==
606 		    NULL) {
607 			netsnmp_free_delegated_cache(cache);
608 			(void) pthread_mutex_unlock(&update_lock);
609 			return;
610 		}
611 		break;
612 	case MODE_GETNEXT:
613 	case MODE_GETBULK:
614 		if ((data = sunFmModuleTable_nextmod(reginfo, table_info)) ==
615 		    NULL) {
616 			netsnmp_free_delegated_cache(cache);
617 			(void) pthread_mutex_unlock(&update_lock);
618 			return;
619 		}
620 		break;
621 	default:
622 		snmp_log(LOG_ERR, MODNAME_STR ": Unsupported request "
623 		    "mode %d\n", reqinfo->mode);
624 		netsnmp_free_delegated_cache(cache);
625 		(void) pthread_mutex_unlock(&update_lock);
626 		return;
627 	}
628 
629 	switch (table_info->colnum) {
630 	case SUNFMMODULE_COL_NAME:
631 		netsnmp_table_build_result(reginfo, request, table_info,
632 		    ASN_OCTET_STR, (uchar_t *)data->d_ami_name,
633 		    strlen(data->d_ami_name));
634 		break;
635 	case SUNFMMODULE_COL_VERSION:
636 		netsnmp_table_build_result(reginfo, request, table_info,
637 		    ASN_OCTET_STR, (uchar_t *)data->d_ami_vers,
638 		    strlen(data->d_ami_vers));
639 		break;
640 	case SUNFMMODULE_COL_STATUS:
641 		modstate = (data->d_ami_flags & FMD_ADM_MOD_FAILED) ?
642 		    SUNFMMODULE_STATE_FAILED : SUNFMMODULE_STATE_ACTIVE;
643 		netsnmp_table_build_result(reginfo, request, table_info,
644 		    ASN_INTEGER, (uchar_t *)&modstate,
645 		    sizeof (modstate));
646 		break;
647 	case SUNFMMODULE_COL_DESCRIPTION:
648 		netsnmp_table_build_result(reginfo, request, table_info,
649 		    ASN_OCTET_STR, (uchar_t *)data->d_ami_desc,
650 		    strlen(data->d_ami_desc));
651 		break;
652 	default:
653 		break;
654 	}
655 	netsnmp_free_delegated_cache(cache);
656 	(void) pthread_mutex_unlock(&update_lock);
657 }
658 
659 static int
660 sunFmModuleTable_handler(netsnmp_mib_handler *handler,
661     netsnmp_handler_registration *reginfo, netsnmp_agent_request_info *reqinfo,
662     netsnmp_request_info *requests)
663 {
664 	netsnmp_request_info		*request;
665 	struct timeval			tv;
666 
667 	tv.tv_sec = UPDATE_WAIT_MILLIS / 1000;
668 	tv.tv_usec = (UPDATE_WAIT_MILLIS % 1000) * 1000;
669 
670 	request_update();
671 
672 	for (request = requests; request; request = request->next) {
673 		if (request->processed != 0)
674 			continue;
675 
676 		if (netsnmp_extract_table_info(request) == NULL)
677 			continue;
678 
679 		request->delegated = 1;
680 		(void) snmp_alarm_register_hr(tv, 0, sunFmModuleTable_return,
681 		    (void *) netsnmp_create_delegated_cache(handler, reginfo,
682 		    reqinfo, request, NULL));
683 	}
684 
685 	return (SNMP_ERR_NOERROR);
686 }
687