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 (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved.
24  */
25 
26 #include <fmd_module.h>
27 #include <fmd_subr.h>
28 #include <fmd_error.h>
29 #include <fmd_string.h>
30 #include <fmd_event.h>
31 #include <fmd_builtin.h>
32 #include <zone.h>
33 
34 static const struct fmd_builtin _fmd_builtins[] = {
35 	{ "fmd-self-diagnosis", self_init, self_fini, FMD_BUILTIN_ALLCTXT },
36 	{ "sysevent-transport", sysev_init, sysev_fini,
37 		FMD_BUILTIN_CTXT_GLOBALZONE },
38 	{ NULL, NULL, NULL, 0 }
39 };
40 
41 static int
bltin_init(fmd_module_t * mp)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
bltin_fini(fmd_module_t * mp)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 	fmd_module_transport,
101 };
102 
103 int
fmd_builtin_loadall(fmd_modhash_t * mhp)104 fmd_builtin_loadall(fmd_modhash_t *mhp)
105 {
106 	const fmd_builtin_t *bp;
107 	uint32_t ctxt = 0;
108 	int err = 0;
109 
110 	ctxt |= (getzoneid() == GLOBAL_ZONEID) ? FMD_BUILTIN_CTXT_GLOBALZONE :
111 	    FMD_BUILTIN_CTXT_NONGLOBALZONE;
112 
113 	for (bp = _fmd_builtins; bp->bltin_name != NULL; bp++) {
114 		if (!(ctxt & bp->bltin_ctxts))
115 			continue;
116 
117 		if (fmd_modhash_load(mhp, bp->bltin_name,
118 		    &fmd_bltin_ops) == NULL)
119 			err = -1;
120 	}
121 
122 	return (err);
123 }
124