xref: /titanic_52/usr/src/lib/fm/topo/libtopo/common/mod.c (revision 7d968cb8b4b6274092771b93e94bf88d1ee31c6c)
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 2006 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 <limits.h>
30 #include <strings.h>
31 #include <unistd.h>
32 #include <libnvpair.h>
33 #include <fm/topo_mod.h>
34 #include <sys/fm/protocol.h>
35 
36 #include <fcntl.h>
37 #include <sys/types.h>
38 #include <sys/stat.h>
39 #include <sys/objfs.h>
40 #include <sys/modctl.h>
41 #include <libelf.h>
42 #include <gelf.h>
43 
44 #include <topo_method.h>
45 #include <mod.h>
46 
47 static int mod_enum(topo_mod_t *, tnode_t *, const char *, topo_instance_t,
48     topo_instance_t, void *, void *);
49 static void mod_release(topo_mod_t *, tnode_t *);
50 static int mod_fmri_create_meth(topo_mod_t *, tnode_t *, topo_version_t,
51     nvlist_t *, nvlist_t **);
52 
53 static const topo_method_t mod_methods[] = {
54 	{ TOPO_METH_FMRI, TOPO_METH_FMRI_DESC, TOPO_METH_FMRI_VERSION,
55 	    TOPO_STABILITY_INTERNAL, mod_fmri_create_meth },
56 	{ NULL }
57 };
58 
59 static const topo_modops_t mod_modops =
60 	{ mod_enum, mod_release };
61 static const topo_modinfo_t mod_info =
62 	{ "mod", FM_FMRI_SCHEME_MOD, MOD_VERSION, &mod_modops };
63 
64 int
65 mod_init(topo_mod_t *mod, topo_version_t version)
66 {
67 	if (getenv("TOPOMODDEBUG"))
68 		topo_mod_setdebug(mod);
69 	topo_mod_dprintf(mod, "initializing mod builtin\n");
70 
71 	if (version != MOD_VERSION)
72 		return (topo_mod_seterrno(mod, EMOD_VER_NEW));
73 
74 	if (topo_mod_register(mod, &mod_info, TOPO_VERSION) != 0) {
75 		topo_mod_dprintf(mod, "failed to register mod_info: "
76 		    "%s\n", topo_mod_errmsg(mod));
77 		return (-1); /* mod errno already set */
78 	}
79 
80 	return (0);
81 }
82 
83 void
84 mod_fini(topo_mod_t *mod)
85 {
86 	topo_mod_unregister(mod);
87 }
88 
89 /*ARGSUSED*/
90 static int
91 mod_enum(topo_mod_t *mod, tnode_t *pnode, const char *name,
92     topo_instance_t min, topo_instance_t max, void *notused1, void *notused2)
93 {
94 	(void) topo_method_register(mod, pnode, mod_methods);
95 	return (0);
96 }
97 
98 static void
99 mod_release(topo_mod_t *mod, tnode_t *node)
100 {
101 	topo_method_unregister_all(mod, node);
102 }
103 
104 static char *
105 mod_binary_path_get(topo_mod_t *mp, char *objpath)
106 {
107 	static char Pathbuf[PATH_MAX];
108 	Elf *elf = NULL;
109 	Elf_Scn *scn = NULL;
110 	Elf_Data *edata;
111 	GElf_Ehdr ehdr;
112 	GElf_Shdr shdr;
113 	int fd;
114 
115 	if ((fd = open(objpath, O_RDONLY)) < 0) {
116 		topo_mod_dprintf(mp, "failed to open %s", objpath);
117 		goto mbpg_bail;
118 	}
119 	if (elf_version(EV_CURRENT) == EV_NONE) {
120 		topo_mod_dprintf(mp, "Elf version out of whack\n");
121 		goto mbpg_bail;
122 	}
123 	if ((elf = elf_begin(fd, ELF_C_READ, NULL)) == NULL) {
124 		topo_mod_dprintf(mp, "elf_begin failed\n");
125 		goto mbpg_bail;
126 	}
127 	if ((gelf_getehdr(elf, &ehdr)) == NULL) {
128 		topo_mod_dprintf(mp, "gelf_getehdr failed\n");
129 		goto mbpg_bail;
130 	}
131 	scn = elf_getscn(elf, 0);	/* "seek" to start of sections */
132 	while ((scn = elf_nextscn(elf, scn)) != NULL) {
133 		const char *sh_name;
134 		if (gelf_getshdr(scn, &shdr) == NULL) {
135 			topo_mod_dprintf(mp, "gelf_getshdr failed\n");
136 			goto mbpg_bail;
137 		}
138 		if (shdr.sh_type != SHT_PROGBITS)
139 			continue;
140 		sh_name = elf_strptr(elf,
141 		    ehdr.e_shstrndx, (size_t)shdr.sh_name);
142 		if (strcmp(sh_name, ".filename") != 0)
143 			continue;
144 		if ((edata = elf_getdata(scn, NULL)) == NULL) {
145 			topo_mod_dprintf(mp, "no filename data");
146 			break;
147 		}
148 		(void) strlcpy(Pathbuf, edata->d_buf, PATH_MAX);
149 		break;
150 	}
151 	elf_end(elf);
152 	(void) close(fd);
153 	return (Pathbuf);
154 
155 mbpg_bail:
156 	if (elf != NULL)
157 		elf_end(elf);
158 	if (fd >= 0)
159 		(void) close(fd);
160 	(void) topo_mod_seterrno(mp, EMOD_METHOD_INVAL);
161 	return (NULL);
162 }
163 
164 static int
165 mod_nvl_data(topo_mod_t *mp, nvlist_t *out, const char *path)
166 {
167 	struct modinfo mi;
168 	struct stat64 s;
169 	int id, e;
170 
171 	if (stat64(path, &s) < 0) {
172 		topo_mod_dprintf(mp,
173 		    "No system object file for driver %s", path);
174 		return (topo_mod_seterrno(mp, EMOD_METHOD_INVAL));
175 	}
176 
177 	id = OBJFS_MODID(s.st_ino);
178 	mi.mi_id = mi.mi_nextid = id;
179 	mi.mi_info = MI_INFO_ONE | MI_INFO_NOBASE;
180 	if (modctl(MODINFO, id, &mi) < 0) {
181 		topo_mod_dprintf(mp, "failed to get modinfo for %s", path);
182 		return (topo_mod_seterrno(mp, EMOD_METHOD_INVAL));
183 	}
184 	mi.mi_name[MODMAXNAMELEN - 1] = '\0';
185 	mi.mi_msinfo[0].msi_linkinfo[MODMAXNAMELEN - 1] = '\0';
186 	e = nvlist_add_string(out, FM_FMRI_SCHEME, FM_FMRI_SCHEME_MOD);
187 	e |= nvlist_add_uint8(out, FM_VERSION, FM_MOD_SCHEME_VERSION);
188 	e |= nvlist_add_int32(out, FM_FMRI_MOD_ID, id);
189 	e |= nvlist_add_string(out, FM_FMRI_MOD_NAME, mi.mi_name);
190 	e |= nvlist_add_string(out,
191 	    FM_FMRI_MOD_DESC, mi.mi_msinfo[0].msi_linkinfo);
192 	if (e != 0)
193 		return (topo_mod_seterrno(mp, EMOD_FMRI_NVL));
194 
195 	return (0);
196 }
197 
198 static nvlist_t *
199 mod_fmri_create(topo_mod_t *mp, const char *driver)
200 {
201 	nvlist_t *out = NULL;
202 	nvlist_t *pkg = NULL;
203 	char objpath[PATH_MAX];
204 	char *path = NULL;
205 
206 	if (topo_mod_nvalloc(mp, &out, NV_UNIQUE_NAME) != 0) {
207 		(void) topo_mod_seterrno(mp, EMOD_FMRI_NVL);
208 		goto mfc_bail;
209 	}
210 
211 	(void) snprintf(objpath, PATH_MAX, "%s/%s/object", OBJFS_ROOT, driver);
212 
213 	if ((path = mod_binary_path_get(mp, objpath)) == NULL)
214 		goto mfc_bail;
215 
216 	if (mod_nvl_data(mp, out, objpath) < 0)
217 		goto mfc_bail;
218 
219 	pkg = topo_mod_pkgfmri(mp, FM_PKG_SCHEME_VERSION, path);
220 	if (pkg == NULL) {
221 		goto mfc_bail;
222 	}
223 
224 	if (nvlist_add_nvlist(out, FM_FMRI_MOD_PKG, pkg) != 0) {
225 		(void) topo_mod_seterrno(mp, EMOD_FMRI_NVL);
226 		goto mfc_bail;
227 	}
228 	nvlist_free(pkg);
229 
230 	return (out);
231 
232 mfc_bail:
233 	nvlist_free(pkg);
234 	nvlist_free(out);
235 	return (NULL);
236 }
237 
238 /*ARGSUSED*/
239 static int
240 mod_fmri_create_meth(topo_mod_t *mp, tnode_t *node, topo_version_t version,
241     nvlist_t *in, nvlist_t **out)
242 {
243 	nvlist_t *args;
244 	nvlist_t *modnvl;
245 	char *driver;
246 
247 	if (version > TOPO_METH_FMRI_VERSION)
248 		return (topo_mod_seterrno(mp, EMOD_VER_NEW));
249 
250 	if (nvlist_lookup_nvlist(in, TOPO_METH_FMRI_ARG_NVL, &args) != 0 ||
251 	    nvlist_lookup_string(args, "DRIVER", &driver) != 0) {
252 		topo_mod_dprintf(mp, "no DRIVER string in method argument\n");
253 		return (topo_mod_seterrno(mp, EMOD_METHOD_INVAL));
254 	}
255 
256 	modnvl = mod_fmri_create(mp, driver);
257 	if (modnvl == NULL) {
258 		*out = NULL;
259 		topo_mod_dprintf(mp, "failed to create contained mod FMRI\n");
260 		return (topo_mod_seterrno(mp, EMOD_FMRI_NVL));
261 	}
262 	*out = modnvl;
263 	return (0);
264 }
265