xref: /titanic_50/usr/src/lib/fm/topo/libtopo/common/mod.c (revision 7ff836697c120cb94bd30d5c2204eb9b74718e4c)
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 2008 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 <topo_subr.h>
46 #include <mod.h>
47 
48 static int mod_enum(topo_mod_t *, tnode_t *, const char *, topo_instance_t,
49     topo_instance_t, void *, void *);
50 static void mod_release(topo_mod_t *, tnode_t *);
51 static int mod_fmri_create_meth(topo_mod_t *, tnode_t *, topo_version_t,
52     nvlist_t *, nvlist_t **);
53 static int mod_fmri_nvl2str(topo_mod_t *, tnode_t *, topo_version_t,
54     nvlist_t *, nvlist_t **);
55 
56 static const topo_method_t mod_methods[] = {
57 	{ TOPO_METH_FMRI, TOPO_METH_FMRI_DESC, TOPO_METH_FMRI_VERSION,
58 	    TOPO_STABILITY_INTERNAL, mod_fmri_create_meth },
59 	{ TOPO_METH_NVL2STR, TOPO_METH_NVL2STR_DESC, TOPO_METH_NVL2STR_VERSION,
60 	    TOPO_STABILITY_INTERNAL, mod_fmri_nvl2str },
61 	{ NULL }
62 };
63 
64 static const topo_modops_t mod_modops =
65 	{ mod_enum, mod_release };
66 static const topo_modinfo_t mod_info =
67 	{ "mod", FM_FMRI_SCHEME_MOD, MOD_VERSION, &mod_modops };
68 
69 int
70 mod_init(topo_mod_t *mod, topo_version_t version)
71 {
72 	if (getenv("TOPOMODDEBUG"))
73 		topo_mod_setdebug(mod);
74 	topo_mod_dprintf(mod, "initializing mod builtin\n");
75 
76 	if (version != MOD_VERSION)
77 		return (topo_mod_seterrno(mod, EMOD_VER_NEW));
78 
79 	if (topo_mod_register(mod, &mod_info, TOPO_VERSION) != 0) {
80 		topo_mod_dprintf(mod, "failed to register mod_info: "
81 		    "%s\n", topo_mod_errmsg(mod));
82 		return (-1); /* mod errno already set */
83 	}
84 
85 	return (0);
86 }
87 
88 void
89 mod_fini(topo_mod_t *mod)
90 {
91 	topo_mod_unregister(mod);
92 }
93 
94 /*ARGSUSED*/
95 static int
96 mod_enum(topo_mod_t *mod, tnode_t *pnode, const char *name,
97     topo_instance_t min, topo_instance_t max, void *notused1, void *notused2)
98 {
99 	(void) topo_method_register(mod, pnode, mod_methods);
100 	return (0);
101 }
102 
103 static void
104 mod_release(topo_mod_t *mod, tnode_t *node)
105 {
106 	topo_method_unregister_all(mod, node);
107 }
108 
109 static int
110 mod_binary_path_get(topo_mod_t *mp, const char *objpath)
111 {
112 	Elf *elf = NULL;
113 	Elf_Scn *scn = NULL;
114 	GElf_Ehdr ehdr;
115 	GElf_Shdr shdr;
116 	int fd;
117 
118 	if ((fd = open(objpath, O_RDONLY)) < 0) {
119 		topo_mod_dprintf(mp, "unable to open %s\n", objpath);
120 		return (-1);
121 	}
122 
123 	if (elf_version(EV_CURRENT) == EV_NONE) {
124 		topo_mod_dprintf(mp, "Elf version out of whack\n");
125 		goto mbpg_bail;
126 	}
127 	if ((elf = elf_begin(fd, ELF_C_READ, NULL)) == NULL) {
128 		topo_mod_dprintf(mp, "elf_begin failed\n");
129 		goto mbpg_bail;
130 	}
131 	if ((gelf_getehdr(elf, &ehdr)) == NULL) {
132 		topo_mod_dprintf(mp, "gelf_getehdr failed\n");
133 		goto mbpg_bail;
134 	}
135 	scn = elf_getscn(elf, 0);	/* "seek" to start of sections */
136 	while ((scn = elf_nextscn(elf, scn)) != NULL) {
137 		const char *sh_name;
138 		if (gelf_getshdr(scn, &shdr) == NULL) {
139 			topo_mod_dprintf(mp, "gelf_getshdr failed\n");
140 			goto mbpg_bail;
141 		}
142 		if (shdr.sh_type != SHT_PROGBITS)
143 			continue;
144 		sh_name = elf_strptr(elf,
145 		    ehdr.e_shstrndx, (size_t)shdr.sh_name);
146 		if (strcmp(sh_name, ".filename") != 0)
147 			continue;
148 		if (elf_getdata(scn, NULL) == NULL) {
149 			topo_mod_dprintf(mp, "no filename data");
150 			break;
151 		}
152 		break;
153 	}
154 	elf_end(elf);
155 	(void) close(fd);
156 	return (0);
157 
158 mbpg_bail:
159 	if (elf != NULL)
160 		elf_end(elf);
161 	if (fd >= 0)
162 		(void) close(fd);
163 	(void) topo_mod_seterrno(mp, EMOD_METHOD_INVAL);
164 	return (-1);
165 }
166 
167 static int
168 mod_nvl_data(topo_mod_t *mp, nvlist_t *out, const char *path)
169 {
170 	struct modinfo mi;
171 	struct stat64 s;
172 	int id, e;
173 
174 	if (stat64(path, &s) < 0) {
175 		topo_mod_dprintf(mp,
176 		    "No system object file for driver %s", path);
177 		return (topo_mod_seterrno(mp, EMOD_METHOD_INVAL));
178 	}
179 
180 	id = OBJFS_MODID(s.st_ino);
181 	mi.mi_id = mi.mi_nextid = id;
182 	mi.mi_info = MI_INFO_ONE | MI_INFO_NOBASE;
183 	if (modctl(MODINFO, id, &mi) < 0) {
184 		return (topo_mod_seterrno(mp, EMOD_METHOD_INVAL));
185 	}
186 	mi.mi_name[MODMAXNAMELEN - 1] = '\0';
187 	mi.mi_msinfo[0].msi_linkinfo[MODMAXNAMELEN - 1] = '\0';
188 	e = nvlist_add_string(out, FM_FMRI_SCHEME, FM_FMRI_SCHEME_MOD);
189 	e |= nvlist_add_uint8(out, FM_VERSION, FM_MOD_SCHEME_VERSION);
190 	e |= nvlist_add_int32(out, FM_FMRI_MOD_ID, id);
191 	e |= nvlist_add_string(out, FM_FMRI_MOD_NAME, mi.mi_name);
192 	e |= nvlist_add_string(out,
193 	    FM_FMRI_MOD_DESC, mi.mi_msinfo[0].msi_linkinfo);
194 	if (e != 0)
195 		return (topo_mod_seterrno(mp, EMOD_FMRI_NVL));
196 
197 	return (0);
198 }
199 
200 static nvlist_t *
201 mod_fmri_create(topo_mod_t *mp, const char *driver)
202 {
203 	nvlist_t *out = NULL;
204 	char objpath[PATH_MAX];
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 	/*
214 	 * Validate the module object ELF header if possible
215 	 */
216 	if (mod_binary_path_get(mp, objpath) < 0)
217 		goto mfc_bail;
218 
219 	if (mod_nvl_data(mp, out, objpath) < 0) {
220 		topo_mod_dprintf(mp, "failed to get modinfo for %s", driver);
221 		goto mfc_bail;
222 	}
223 
224 	return (out);
225 
226 mfc_bail:
227 	nvlist_free(out);
228 	return (NULL);
229 }
230 
231 /*ARGSUSED*/
232 static int
233 mod_fmri_create_meth(topo_mod_t *mp, tnode_t *node, topo_version_t version,
234     nvlist_t *in, nvlist_t **out)
235 {
236 	nvlist_t *args;
237 	nvlist_t *modnvl;
238 	char *driver;
239 
240 	if (version > TOPO_METH_FMRI_VERSION)
241 		return (topo_mod_seterrno(mp, EMOD_VER_NEW));
242 
243 	if (nvlist_lookup_nvlist(in, TOPO_METH_FMRI_ARG_NVL, &args) != 0 ||
244 	    nvlist_lookup_string(args, "DRIVER", &driver) != 0) {
245 		topo_mod_dprintf(mp, "no DRIVER string in method argument\n");
246 		return (topo_mod_seterrno(mp, EMOD_METHOD_INVAL));
247 	}
248 
249 	modnvl = mod_fmri_create(mp, driver);
250 	if (modnvl == NULL) {
251 		*out = NULL;
252 		topo_mod_dprintf(mp, "failed to create contained mod FMRI\n");
253 		return (-1);
254 	}
255 	*out = modnvl;
256 	return (0);
257 }
258 
259 #define	MAXINTSTR	11
260 
261 static ssize_t
262 fmri_nvl2str(nvlist_t *nvl, char *buf, size_t buflen)
263 {
264 	nvlist_t *anvl = NULL;
265 	uint8_t version;
266 	ssize_t size = 0;
267 	int32_t modid;
268 	char *achas = NULL;
269 	char *adom = NULL;
270 	char *aprod = NULL;
271 	char *asrvr = NULL;
272 	char *ahost = NULL;
273 	char *modname = NULL;
274 	char numbuf[MAXINTSTR];
275 	int err;
276 
277 	if (nvlist_lookup_uint8(nvl, FM_VERSION, &version) != 0 ||
278 	    version > FM_MOD_SCHEME_VERSION)
279 		return (-1);
280 
281 	/* Get authority, if present */
282 	err = nvlist_lookup_nvlist(nvl, FM_FMRI_AUTHORITY, &anvl);
283 	if (err != 0 && err != ENOENT)
284 		return (-1);
285 
286 	/*
287 	 *  For brevity, we only include the module name and id
288 	 *  present in the FMRI in our output string.  The FMRI
289 	 *  also has data on the package containing the module.
290 	 */
291 
292 	/* There must be a module name */
293 	err = nvlist_lookup_string(nvl, FM_FMRI_MOD_NAME, &modname);
294 	if (err != 0 || modname == NULL)
295 		return (-1);
296 
297 	/* There must be a module id */
298 	err = nvlist_lookup_int32(nvl, FM_FMRI_MOD_ID, &modid);
299 	if (err != 0)
300 		return (-1);
301 
302 	if (anvl != NULL) {
303 		(void) nvlist_lookup_string(anvl,
304 		    FM_FMRI_AUTH_PRODUCT, &aprod);
305 		(void) nvlist_lookup_string(anvl,
306 		    FM_FMRI_AUTH_CHASSIS, &achas);
307 		(void) nvlist_lookup_string(anvl,
308 		    FM_FMRI_AUTH_DOMAIN, &adom);
309 		(void) nvlist_lookup_string(anvl,
310 		    FM_FMRI_AUTH_SERVER, &asrvr);
311 		(void) nvlist_lookup_string(anvl,
312 		    FM_FMRI_AUTH_HOST, &ahost);
313 	}
314 
315 	/* mod:// */
316 	topo_fmristr_build(&size, buf, buflen, FM_FMRI_SCHEME_MOD, NULL, "://");
317 
318 	/* authority, if any */
319 	if (aprod != NULL)
320 		topo_fmristr_build(&size, buf, buflen, aprod,
321 		    ":" FM_FMRI_AUTH_PRODUCT "=", NULL);
322 	if (achas != NULL)
323 		topo_fmristr_build(&size, buf, buflen, achas,
324 		    ":" FM_FMRI_AUTH_CHASSIS "=", NULL);
325 	if (adom != NULL)
326 		topo_fmristr_build(&size, buf, buflen, adom,
327 		    ":" FM_FMRI_AUTH_DOMAIN "=", NULL);
328 	if (asrvr != NULL)
329 		topo_fmristr_build(&size, buf, buflen, asrvr,
330 		    ":" FM_FMRI_AUTH_SERVER "=", NULL);
331 	if (ahost != NULL)
332 		topo_fmristr_build(&size, buf, buflen, ahost,
333 		    ":" FM_FMRI_AUTH_HOST "=", NULL);
334 
335 	/* module parts */
336 	topo_fmristr_build(&size, buf, buflen, modname,
337 	    "/" FM_FMRI_MOD_NAME "=", "/");
338 
339 	(void) snprintf(numbuf, MAXINTSTR, "%d", modid);
340 	topo_fmristr_build(&size, buf, buflen, numbuf, FM_FMRI_MOD_ID "=",
341 	    NULL);
342 
343 	return (size);
344 }
345 
346 /*ARGSUSED*/
347 static int
348 mod_fmri_nvl2str(topo_mod_t *mod, tnode_t *node, topo_version_t version,
349     nvlist_t *nvl, nvlist_t **out)
350 {
351 	ssize_t len;
352 	char *name = NULL;
353 	nvlist_t *fmristr;
354 
355 	if (version > TOPO_METH_NVL2STR_VERSION)
356 		return (topo_mod_seterrno(mod, EMOD_VER_NEW));
357 
358 	if ((len = fmri_nvl2str(nvl, NULL, 0)) == 0 ||
359 	    (name = topo_mod_alloc(mod, len + 1)) == NULL ||
360 	    fmri_nvl2str(nvl, name, len + 1) == 0) {
361 		if (name != NULL)
362 			topo_mod_free(mod, name, len + 1);
363 		return (topo_mod_seterrno(mod, EMOD_FMRI_NVL));
364 	}
365 
366 	if (topo_mod_nvalloc(mod, &fmristr, NV_UNIQUE_NAME) != 0)
367 		return (topo_mod_seterrno(mod, EMOD_FMRI_NVL));
368 	if (nvlist_add_string(fmristr, "fmri-string", name) != 0) {
369 		topo_mod_free(mod, name, len + 1);
370 		nvlist_free(fmristr);
371 		return (topo_mod_seterrno(mod, EMOD_FMRI_NVL));
372 	}
373 	topo_mod_free(mod, name, len + 1);
374 	*out = fmristr;
375 
376 	return (0);
377 }
378