xref: /illumos-gate/usr/src/cmd/fm/fmd/common/fmd_rtld.c (revision 9164a50bf932130cbb5097a16f6986873ce0e6e5)
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 #include <dlfcn.h>
28 #include <link.h>
29 
30 #include <fmd_module.h>
31 #include <fmd_error.h>
32 #include <fmd_alloc.h>
33 #include <fmd_subr.h>
34 #include <fmd_event.h>
35 #include <fmd.h>
36 
37 typedef struct fmd_rtld {
38 	void *rtld_dlp;		/* libdl(3DL) handle for shared library */
39 	void (*rtld_init)(fmd_hdl_t *); /* shared library's _fmd_init() */
40 	void (*rtld_fini)(fmd_hdl_t *); /* shared library's _fmd_fini() */
41 } fmd_rtld_t;
42 
43 static int
44 rtld_init(fmd_module_t *mp)
45 {
46 	fmd_rtld_t *rp;
47 	void *dlp;
48 
49 	if ((dlp = dlopen(mp->mod_path, RTLD_LOCAL | RTLD_NOW)) == NULL) {
50 		fmd_error(EFMD_RTLD_OPEN, "%s\n", dlerror());
51 		return (fmd_set_errno(EFMD_RTLD_OPEN));
52 	}
53 
54 	rp = mp->mod_data = fmd_alloc(sizeof (fmd_rtld_t), FMD_SLEEP);
55 
56 	rp->rtld_dlp = dlp;
57 	rp->rtld_init = (void (*)())dlsym(dlp, "_fmd_init");
58 	rp->rtld_fini = (void (*)())dlsym(dlp, "_fmd_fini");
59 
60 	if (rp->rtld_init == NULL) {
61 		(void) dlclose(dlp);
62 		fmd_free(rp, sizeof (fmd_rtld_t));
63 		return (fmd_set_errno(EFMD_RTLD_INIT));
64 	}
65 
66 	(void) pthread_mutex_unlock(&mp->mod_lock);
67 
68 	/*
69 	 * Call _fmd_init() in the module.  If this causes a module abort and
70 	 * mod_info has been registered, unregister it on behalf of the module.
71 	 */
72 	if (fmd_module_enter(mp, rp->rtld_init) != 0 && mp->mod_info != NULL)
73 		fmd_hdl_unregister((fmd_hdl_t *)mp);
74 
75 	fmd_module_exit(mp);
76 	(void) pthread_mutex_lock(&mp->mod_lock);
77 
78 	if (mp->mod_info == NULL) {
79 		(void) dlclose(dlp);
80 		fmd_free(rp, sizeof (fmd_rtld_t));
81 		return (fmd_set_errno(EFMD_HDL_INIT));
82 	}
83 
84 	return (0);
85 }
86 
87 static int
88 rtld_fini(fmd_module_t *mp)
89 {
90 	fmd_rtld_t *rp = mp->mod_data;
91 	int doclose = 1, err = 0;
92 
93 	if (mp->mod_info != NULL) {
94 		(void) fmd_module_enter(mp, rp->rtld_fini);
95 
96 		if (mp->mod_info != NULL) {
97 			fmd_module_lock(mp);
98 			fmd_module_unregister(mp);
99 			fmd_module_unlock(mp);
100 		}
101 
102 		fmd_module_exit(mp);
103 	}
104 
105 	(void) fmd_conf_getprop(fmd.d_conf, "plugin.close", &doclose);
106 	if (doclose)
107 		err = dlclose(rp->rtld_dlp);
108 
109 	fmd_free(rp, sizeof (fmd_rtld_t));
110 	return (err);
111 }
112 
113 const fmd_modops_t fmd_rtld_ops = {
114 	rtld_init,
115 	rtld_fini,
116 	fmd_module_dispatch,
117 	fmd_module_transport,
118 };
119