xref: /illumos-gate/usr/src/lib/fm/topo/libtopo/common/topo_rtld.c (revision 20a7641f9918de8574b8b3b47dbe35c4bfc78df1)
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  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #include <dlfcn.h>
27 #include <link.h>
28 #include <pthread.h>
29 #include <assert.h>
30 
31 #include <fm/topo_mod.h>
32 
33 #include <topo_error.h>
34 #include <topo_alloc.h>
35 #include <topo_subr.h>
36 
37 typedef struct topo_rtld {
38 	void *rtld_dlp;		/* libdl(3DL) handle for shared library */
39 	int (*rtld_init)(topo_mod_t *, topo_version_t); /* .so _topo_init() */
40 	void (*rtld_fini)(topo_mod_t *); /* .so _topo_fini() */
41 } topo_rtld_t;
42 
43 static int
44 rtld_fini(topo_mod_t *mod)
45 {
46 	topo_rtld_t *rp = mod->tm_data;
47 
48 	assert(mod != NULL);
49 
50 	if (mod->tm_flags & TOPO_MOD_REG) {
51 		rp->rtld_fini(mod);
52 		if (mod->tm_flags & TOPO_MOD_REG) {
53 			topo_mod_unregister(mod);
54 		}
55 	}
56 
57 	if (getenv("TOPONODLCLOSE") == NULL)
58 		(void) dlclose(rp->rtld_dlp);
59 	topo_mod_free(mod, rp, sizeof (topo_rtld_t));
60 
61 	return (0);
62 }
63 
64 static int
65 rtld_init(topo_mod_t *mod, topo_version_t version)
66 {
67 	int err;
68 	topo_rtld_t *rp;
69 	void *dlp;
70 
71 	if ((dlp = dlopen(mod->tm_path, RTLD_LOCAL | RTLD_NOW)) == NULL) {
72 		topo_dprintf(mod->tm_hdl, TOPO_DBG_ERR,
73 		    "dlopen() failed: %s\n", dlerror());
74 		return (topo_mod_seterrno(mod, ETOPO_RTLD_OPEN));
75 	}
76 
77 	if ((rp = mod->tm_data = topo_mod_alloc(mod, sizeof (topo_rtld_t)))
78 	    == NULL)
79 		return (topo_mod_seterrno(mod, ETOPO_RTLD_OPEN));
80 
81 	rp->rtld_dlp = dlp;
82 	rp->rtld_init = (int (*)())dlsym(dlp, "_topo_init");
83 	rp->rtld_fini = (void (*)())dlsym(dlp, "_topo_fini");
84 
85 	if (rp->rtld_init == NULL) {
86 		(void) dlclose(dlp);
87 		topo_free(rp, sizeof (topo_rtld_t));
88 		return (topo_mod_seterrno(mod, ETOPO_RTLD_INIT));
89 	}
90 
91 	/*
92 	 * Call _topo_init() in the module.
93 	 */
94 	err = rp->rtld_init(mod, version);
95 
96 	if (err < 0 || !(mod->tm_flags & TOPO_MOD_REG)) {
97 		(void) rtld_fini(mod);
98 		return (topo_mod_seterrno(mod, ETOPO_MOD_NOREG));
99 	}
100 
101 	return (0);
102 }
103 
104 const topo_imodops_t topo_rtld_ops = {
105 	rtld_init,
106 	rtld_fini,
107 };
108