xref: /titanic_41/usr/src/lib/fm/libfmd_snmp/common/module.c (revision 749f21d359d8fbd020c974a1a5227316221bfc9c)
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 		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 		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 		strlcpy(data->d_ami_vers, modinfo->ami_vers,
227 		    sizeof (data->d_ami_vers));
228 		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 		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 			return (NULL);
491 		DEBUGMSGTL((MODNAME_STR, "nextmod: built fake index:\n"));
492 		DEBUGMSGVAR((MODNAME_STR, var));
493 		DEBUGMSG((MODNAME_STR, "\n"));
494 	} else {
495 		var = table_info->indexes;
496 		index = *var->val.integer;
497 		DEBUGMSGTL((MODNAME_STR, "nextmod: received index:\n"));
498 		DEBUGMSGVAR((MODNAME_STR, var));
499 		DEBUGMSG((MODNAME_STR, "\n"));
500 		index++;
501 	}
502 
503 	if ((data = module_lookup_index_nextvalid(index)) == NULL) {
504 		DEBUGMSGTL((MODNAME_STR, "nextmod: exact match not found for "
505 		    "index %lu; trying next column\n", index));
506 		if (table_info->colnum >= SUNFMMODULE_COLMAX) {
507 			snmp_free_varbind(var);
508 			table_info->indexes = NULL;
509 			table_info->number_indexes = 0;
510 			DEBUGMSGTL((MODNAME_STR, "nextmod: out of columns\n"));
511 			return (NULL);
512 		}
513 		table_info->colnum++;
514 		index = 1;
515 
516 		data = module_lookup_index_nextvalid(index);
517 	}
518 
519 	if (data == NULL) {
520 		DEBUGMSGTL((MODNAME_STR, "nextmod: exact match not found for "
521 		    "index %lu; stopping\n", index));
522 		snmp_free_varbind(var);
523 		table_info->indexes = NULL;
524 		table_info->number_indexes = 0;
525 		return (NULL);
526 	}
527 
528 	*var->val.integer = index;
529 	table_info->indexes = var;
530 
531 	DEBUGMSGTL((MODNAME_STR, "matching data is %lu/%s@%p\n", data->d_index,
532 	    data->d_ami_name, data));
533 
534 	return (data);
535 }
536 
537 /*ARGSUSED*/
538 static sunFmModule_data_t *
539 sunFmModuleTable_mod(netsnmp_handler_registration *reginfo,
540     netsnmp_table_request_info *table_info)
541 {
542 	sunFmModule_data_t	*data;
543 	netsnmp_variable_list	*var;
544 
545 	ASSERT(table_info->number_indexes == 1);
546 
547 	return (module_lookup_index_exact(table_info->index_oid[0]));
548 }
549 
550 static void
551 sunFmModuleTable_return(unsigned int reg, void *arg)
552 {
553 	netsnmp_delegated_cache		*cache = (netsnmp_delegated_cache *)arg;
554 	netsnmp_request_info		*request;
555 	netsnmp_agent_request_info	*reqinfo;
556 	netsnmp_handler_registration	*reginfo;
557 	netsnmp_mib_handler		*handler;
558 	netsnmp_table_request_info	*table_info;
559 	netsnmp_variable_list		*var;
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 	handler = cache->handler;
582 
583 	var = request->requestvb;
584 	table_info = netsnmp_extract_table_info(request);
585 	request->delegated = 0;
586 
587 	ASSERT(table_info->colnum >= SUNFMMODULE_COLMIN);
588 	ASSERT(table_info->colnum <= SUNFMMODULE_COLMAX);
589 
590 	/*
591 	 * table_info->colnum contains the column number requested.
592 	 * table_info->indexes contains a linked list of snmp variable
593 	 * bindings for the indexes of the table.  Values in the list
594 	 * have been set corresponding to the indexes of the
595 	 * request.  We have other guarantees as well:
596 	 *
597 	 * - The column number is always within range.
598 	 * - If we have no index data, table_info->index_oid_len is 0.
599 	 * - We will never receive requests outside our table nor
600 	 *   those with the first subid anything other than 1 (Entry)
601 	 *   nor those without a column number.  This is true even
602 	 *   for GETNEXT requests.
603 	 */
604 
605 	switch (reqinfo->mode) {
606 	case MODE_GET:
607 		if ((data = sunFmModuleTable_mod(reginfo, table_info)) ==
608 		    NULL) {
609 			netsnmp_free_delegated_cache(cache);
610 			(void) pthread_mutex_unlock(&update_lock);
611 			return;
612 		}
613 		break;
614 	case MODE_GETNEXT:
615 	case MODE_GETBULK:
616 		if ((data = sunFmModuleTable_nextmod(reginfo, table_info)) ==
617 		    NULL) {
618 			netsnmp_free_delegated_cache(cache);
619 			(void) pthread_mutex_unlock(&update_lock);
620 			return;
621 		}
622 		break;
623 	default:
624 		snmp_log(LOG_ERR, MODNAME_STR ": Unsupported request "
625 		    "mode %d\n", reqinfo->mode);
626 		netsnmp_free_delegated_cache(cache);
627 		(void) pthread_mutex_unlock(&update_lock);
628 		return;
629 	}
630 
631 	switch (table_info->colnum) {
632 	case SUNFMMODULE_COL_NAME:
633 		netsnmp_table_build_result(reginfo, request, table_info,
634 		    ASN_OCTET_STR, (uchar_t *)data->d_ami_name,
635 		    strlen(data->d_ami_name));
636 		break;
637 	case SUNFMMODULE_COL_VERSION:
638 		netsnmp_table_build_result(reginfo, request, table_info,
639 		    ASN_OCTET_STR, (uchar_t *)data->d_ami_vers,
640 		    strlen(data->d_ami_vers));
641 		break;
642 	case SUNFMMODULE_COL_STATUS:
643 		modstate = (data->d_ami_flags & FMD_ADM_MOD_FAILED) ?
644 		    SUNFMMODULE_STATE_FAILED : SUNFMMODULE_STATE_ACTIVE;
645 		netsnmp_table_build_result(reginfo, request, table_info,
646 		    ASN_INTEGER, (uchar_t *)&modstate,
647 		    sizeof (modstate));
648 		break;
649 	case SUNFMMODULE_COL_DESCRIPTION:
650 		netsnmp_table_build_result(reginfo, request, table_info,
651 		    ASN_OCTET_STR, (uchar_t *)data->d_ami_desc,
652 		    strlen(data->d_ami_desc));
653 		break;
654 	default:
655 		break;
656 	}
657 	netsnmp_free_delegated_cache(cache);
658 	(void) pthread_mutex_unlock(&update_lock);
659 }
660 
661 static int
662 sunFmModuleTable_handler(netsnmp_mib_handler *handler,
663     netsnmp_handler_registration *reginfo, netsnmp_agent_request_info *reqinfo,
664     netsnmp_request_info *requests)
665 {
666 	netsnmp_request_info		*request;
667 	struct timeval			tv;
668 
669 	tv.tv_sec = UPDATE_WAIT_MILLIS / 1000;
670 	tv.tv_usec = (UPDATE_WAIT_MILLIS % 1000) * 1000;
671 
672 	request_update();
673 
674 	for (request = requests; request; request = request->next) {
675 		if (request->processed != 0)
676 			continue;
677 
678 		if (netsnmp_extract_table_info(request) == NULL)
679 			continue;
680 
681 		request->delegated = 1;
682 		(void) snmp_alarm_register_hr(tv, 0, sunFmModuleTable_return,
683 		    (void *) netsnmp_create_delegated_cache(handler, reginfo,
684 		    reqinfo, request, NULL));
685 	}
686 
687 	return (SNMP_ERR_NOERROR);
688 }
689