xref: /illumos-gate/usr/src/cmd/fm/fmd/common/fmd_builtin.c (revision 581cede61ac9c14d8d4ea452562a567189eead78)
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 /*
24  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
25  * Use is subject to license terms.
26  */
27 
28 #pragma ident	"%Z%%M%	%I%	%E% SMI"
29 
30 #include <fmd_module.h>
31 #include <fmd_subr.h>
32 #include <fmd_error.h>
33 #include <fmd_string.h>
34 #include <fmd_event.h>
35 #include <fmd_builtin.h>
36 
37 static const struct fmd_builtin _fmd_builtins[] = {
38 	{ "fmd-self-diagnosis", self_init, self_fini },
39 	{ "sysevent-transport", sysev_init, sysev_fini },
40 	{ NULL, NULL, NULL }
41 };
42 
43 static int
44 bltin_init(fmd_module_t *mp)
45 {
46 	const fmd_builtin_t *bp;
47 
48 	for (bp = _fmd_builtins; bp->bltin_name != NULL; bp++) {
49 		if (strcmp(mp->mod_name, bp->bltin_name) == 0)
50 			break;
51 	}
52 
53 	if (bp == NULL)
54 		return (fmd_set_errno(EFMD_BLTIN_NAME));
55 
56 	if (bp->bltin_init == NULL)
57 		return (fmd_set_errno(EFMD_BLTIN_INIT));
58 
59 	mp->mod_data = (void *)bp;
60 	(void) pthread_mutex_unlock(&mp->mod_lock);
61 
62 	/*
63 	 * Call _fmd_init() in the module.  If this causes a module abort and
64 	 * mod_info has been registered, unregister it on behalf of the module.
65 	 */
66 	if (fmd_module_enter(mp, bp->bltin_init) != 0 && mp->mod_info != NULL)
67 		fmd_hdl_unregister((fmd_hdl_t *)mp);
68 
69 	fmd_module_exit(mp);
70 	(void) pthread_mutex_lock(&mp->mod_lock);
71 
72 	if (mp->mod_info == NULL)
73 		return (fmd_set_errno(EFMD_HDL_INIT));
74 
75 	return (0);
76 }
77 
78 static int
79 bltin_fini(fmd_module_t *mp)
80 {
81 	fmd_builtin_t *bp = mp->mod_data;
82 
83 	if (mp->mod_info != NULL) {
84 		(void) fmd_module_enter(mp, bp->bltin_fini);
85 
86 		if (mp->mod_info != NULL) {
87 			fmd_module_lock(mp);
88 			fmd_module_unregister(mp);
89 			fmd_module_unlock(mp);
90 		}
91 
92 		fmd_module_exit(mp);
93 	}
94 
95 	return (0);
96 }
97 
98 const fmd_modops_t fmd_bltin_ops = {
99 	bltin_init,
100 	bltin_fini,
101 	fmd_module_dispatch,
102 	fmd_module_transport,
103 };
104 
105 int
106 fmd_builtin_loadall(fmd_modhash_t *mhp)
107 {
108 	const fmd_builtin_t *bp;
109 	int err = 0;
110 
111 	for (bp = _fmd_builtins; bp->bltin_name != NULL; bp++) {
112 		if (fmd_modhash_load(mhp, bp->bltin_name,
113 		    &fmd_bltin_ops) == NULL)
114 			err = -1;
115 	}
116 
117 	return (err);
118 }
119