xref: /titanic_50/usr/src/lib/fm/topo/libtopo/common/mod.c (revision 44743693dce3212f5edba623e0cb0327bd4337a3)
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 <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 *aprod = NULL;
270 	char *asrvr = NULL;
271 	char *modname = NULL;
272 	char numbuf[MAXINTSTR];
273 	int more_auth = 0;
274 	int err;
275 
276 	if (nvlist_lookup_uint8(nvl, FM_VERSION, &version) != 0 ||
277 	    version > FM_MOD_SCHEME_VERSION)
278 		return (-1);
279 
280 	/* Get authority, if present */
281 	err = nvlist_lookup_nvlist(nvl, FM_FMRI_AUTHORITY, &anvl);
282 	if (err != 0 && err != ENOENT)
283 		return (-1);
284 
285 	/*
286 	 *  For brevity, we only include the module name and id
287 	 *  present in the FMRI in our output string.  The FMRI
288 	 *  also has data on the package containing the module.
289 	 */
290 
291 	/* There must be a module name */
292 	err = nvlist_lookup_string(nvl, FM_FMRI_MOD_NAME, &modname);
293 	if (err != 0 || modname == NULL)
294 		return (-1);
295 
296 	/* There must be a module id */
297 	err = nvlist_lookup_int32(nvl, FM_FMRI_MOD_ID, &modid);
298 	if (err != 0)
299 		return (-1);
300 
301 	if (anvl != NULL) {
302 		(void) nvlist_lookup_string(anvl,
303 		    FM_FMRI_AUTH_PRODUCT, &aprod);
304 		(void) nvlist_lookup_string(anvl,
305 		    FM_FMRI_AUTH_CHASSIS, &achas);
306 		(void) nvlist_lookup_string(anvl,
307 		    FM_FMRI_AUTH_SERVER, &asrvr);
308 		if (aprod != NULL)
309 			more_auth++;
310 		if (achas != NULL)
311 			more_auth++;
312 		if (asrvr != NULL)
313 			more_auth++;
314 	}
315 
316 	/* mod:// */
317 	topo_fmristr_build(&size, buf, buflen, FM_FMRI_SCHEME_MOD, NULL, "://");
318 
319 	/* authority, if any */
320 	if (aprod != NULL)
321 		topo_fmristr_build(&size, buf, buflen, aprod,
322 		    ":" FM_FMRI_AUTH_PRODUCT "=", NULL);
323 	if (achas != NULL)
324 		topo_fmristr_build(&size, buf, buflen, achas,
325 		    ":" FM_FMRI_AUTH_CHASSIS "=", NULL);
326 	if (asrvr != NULL)
327 		topo_fmristr_build(&size, buf, buflen, asrvr,
328 		    ":" FM_FMRI_AUTH_SERVER "=", NULL);
329 
330 	/* module parts */
331 	topo_fmristr_build(&size, buf, buflen, modname,
332 	    "/" FM_FMRI_MOD_NAME "=", "/");
333 
334 	(void) snprintf(numbuf, MAXINTSTR, "%d", modid);
335 	topo_fmristr_build(&size, buf, buflen, numbuf, FM_FMRI_MOD_ID "=",
336 	    NULL);
337 
338 	return (size);
339 }
340 
341 /*ARGSUSED*/
342 static int
343 mod_fmri_nvl2str(topo_mod_t *mod, tnode_t *node, topo_version_t version,
344     nvlist_t *nvl, nvlist_t **out)
345 {
346 	ssize_t len;
347 	char *name = NULL;
348 	nvlist_t *fmristr;
349 
350 	if (version > TOPO_METH_NVL2STR_VERSION)
351 		return (topo_mod_seterrno(mod, EMOD_VER_NEW));
352 
353 	if ((len = fmri_nvl2str(nvl, NULL, 0)) == 0 ||
354 	    (name = topo_mod_alloc(mod, len + 1)) == NULL ||
355 	    fmri_nvl2str(nvl, name, len + 1) == 0) {
356 		if (name != NULL)
357 			topo_mod_free(mod, name, len + 1);
358 		return (topo_mod_seterrno(mod, EMOD_FMRI_NVL));
359 	}
360 
361 	if (topo_mod_nvalloc(mod, &fmristr, NV_UNIQUE_NAME) != 0)
362 		return (topo_mod_seterrno(mod, EMOD_FMRI_NVL));
363 	if (nvlist_add_string(fmristr, "fmri-string", name) != 0) {
364 		topo_mod_free(mod, name, len + 1);
365 		nvlist_free(fmristr);
366 		return (topo_mod_seterrno(mod, EMOD_FMRI_NVL));
367 	}
368 	topo_mod_free(mod, name, len + 1);
369 	*out = fmristr;
370 
371 	return (0);
372 }
373