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