dmake: do not set MAKEFLAGS=k
[unleashed/tickless.git] / usr / src / cmd / fm / fmd / common / fmd_rtld.c
blob7385339c9f74d95d78d1f130804eb69d98d4a713
1 /*
2 * CDDL HEADER START
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.
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.
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]
20 * CDDL HEADER END
23 * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 #pragma ident "%Z%%M% %I% %E% SMI"
29 #include <dlfcn.h>
30 #include <link.h>
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>
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;
45 static int
46 rtld_init(fmd_module_t *mp)
48 fmd_rtld_t *rp;
49 void *dlp;
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));
56 rp = mp->mod_data = fmd_alloc(sizeof (fmd_rtld_t), FMD_SLEEP);
58 rp->rtld_dlp = dlp;
59 rp->rtld_init = (void (*)())dlsym(dlp, "_fmd_init");
60 rp->rtld_fini = (void (*)())dlsym(dlp, "_fmd_fini");
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));
68 (void) pthread_mutex_unlock(&mp->mod_lock);
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.
74 if (fmd_module_enter(mp, rp->rtld_init) != 0 && mp->mod_info != NULL)
75 fmd_hdl_unregister((fmd_hdl_t *)mp);
77 fmd_module_exit(mp);
78 (void) pthread_mutex_lock(&mp->mod_lock);
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));
86 return (0);
89 static int
90 rtld_fini(fmd_module_t *mp)
92 fmd_rtld_t *rp = mp->mod_data;
93 int doclose = 1, err = 0;
95 if (mp->mod_info != NULL) {
96 (void) fmd_module_enter(mp, rp->rtld_fini);
98 if (mp->mod_info != NULL) {
99 fmd_module_lock(mp);
100 fmd_module_unregister(mp);
101 fmd_module_unlock(mp);
104 fmd_module_exit(mp);
107 (void) fmd_conf_getprop(fmd.d_conf, "plugin.close", &doclose);
108 if (doclose)
109 err = dlclose(rp->rtld_dlp);
111 fmd_free(rp, sizeof (fmd_rtld_t));
112 return (err);
115 const fmd_modops_t fmd_rtld_ops = {
116 rtld_init,
117 rtld_fini,
118 fmd_module_dispatch,
119 fmd_module_transport,