xref: /titanic_41/usr/src/cmd/fm/fmd/common/fmd_builtin.c (revision fd9cb95cbb2f626355a60efb9d02c5f0a33c10e6)
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, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2005 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 <fmd_module.h>
30 #include <fmd_subr.h>
31 #include <fmd_error.h>
32 #include <fmd_string.h>
33 #include <fmd_event.h>
34 #include <fmd_builtin.h>
35 
36 static const struct fmd_builtin _fmd_builtins[] = {
37 	{ "fmd-self-diagnosis", self_init, self_fini },
38 	{ NULL, NULL, NULL }
39 };
40 
41 static int
42 bltin_init(fmd_module_t *mp)
43 {
44 	const fmd_builtin_t *bp;
45 
46 	for (bp = _fmd_builtins; bp->bltin_name != NULL; bp++) {
47 		if (strcmp(mp->mod_name, bp->bltin_name) == 0)
48 			break;
49 	}
50 
51 	if (bp == NULL)
52 		return (fmd_set_errno(EFMD_BLTIN_NAME));
53 
54 	if (bp->bltin_init == NULL)
55 		return (fmd_set_errno(EFMD_BLTIN_INIT));
56 
57 	mp->mod_data = (void *)bp;
58 	(void) pthread_mutex_unlock(&mp->mod_lock);
59 
60 	/*
61 	 * Call _fmd_init() in the module.  If this causes a module abort and
62 	 * mod_info has been registered, unregister it on behalf of the module.
63 	 */
64 	if (fmd_module_enter(mp, bp->bltin_init) != 0 && mp->mod_info != NULL)
65 		fmd_hdl_unregister((fmd_hdl_t *)mp);
66 
67 	fmd_module_exit(mp);
68 	(void) pthread_mutex_lock(&mp->mod_lock);
69 
70 	if (mp->mod_info == NULL)
71 		return (fmd_set_errno(EFMD_HDL_INIT));
72 
73 	return (0);
74 }
75 
76 static int
77 bltin_fini(fmd_module_t *mp)
78 {
79 	fmd_builtin_t *bp = mp->mod_data;
80 
81 	if (mp->mod_info != NULL) {
82 		(void) fmd_module_enter(mp, bp->bltin_fini);
83 
84 		if (mp->mod_info != NULL) {
85 			fmd_module_lock(mp);
86 			fmd_module_unregister(mp);
87 			fmd_module_unlock(mp);
88 		}
89 
90 		fmd_module_exit(mp);
91 	}
92 
93 	return (0);
94 }
95 
96 const fmd_modops_t fmd_bltin_ops = {
97 	bltin_init,
98 	bltin_fini,
99 	fmd_module_dispatch,
100 };
101 
102 void
103 fmd_builtin_loadall(fmd_modhash_t *mhp)
104 {
105 	const fmd_builtin_t *bp;
106 
107 	for (bp = _fmd_builtins; bp->bltin_name != NULL; bp++)
108 		(void) fmd_modhash_load(mhp, bp->bltin_name, &fmd_bltin_ops);
109 }
110