xref: /titanic_44/usr/src/lib/fm/topo/libtopo/common/fmd.c (revision 5c51f1241dbbdf2656d0e10011981411ed0c9673)
1 /*
2  *
3  * CDDL HEADER START
4  *
5  * The contents of this file are subject to the terms of the
6  * Common Development and Distribution License (the "License").
7  * You may not use this file except in compliance with the License.
8  *
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.
13  *
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]
19  *
20  * CDDL HEADER END
21  */
22 
23 /*
24  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
25  * Use is subject to license terms.
26  */
27 
28 #pragma ident	"%Z%%M%	%I%	%E% SMI"
29 
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <errno.h>
34 #include <ctype.h>
35 #include <alloca.h>
36 #include <limits.h>
37 #include <fm/topo_mod.h>
38 #include <sys/param.h>
39 #include <sys/systeminfo.h>
40 #include <sys/fm/protocol.h>
41 #include <sys/stat.h>
42 
43 #include <topo_method.h>
44 #include <topo_subr.h>
45 #include <fmd.h>
46 
47 static int fmd_enum(topo_mod_t *, tnode_t *, const char *, topo_instance_t,
48     topo_instance_t, void *, void *);
49 static void fmd_release(topo_mod_t *, tnode_t *);
50 static int fmd_fmri_nvl2str(topo_mod_t *, tnode_t *, topo_version_t,
51     nvlist_t *, nvlist_t **);
52 
53 const topo_method_t fmd_methods[] = {
54 	{ TOPO_METH_NVL2STR, TOPO_METH_NVL2STR_DESC, TOPO_METH_NVL2STR_VERSION,
55 	    TOPO_STABILITY_INTERNAL, fmd_fmri_nvl2str },
56 	{ NULL }
57 };
58 
59 static const topo_modops_t fmd_ops =
60 	{ fmd_enum, fmd_release };
61 static const topo_modinfo_t fmd_info =
62 	{ FMD, FM_FMRI_SCHEME_FMD, FMD_VERSION, &fmd_ops };
63 
64 int
65 fmd_init(topo_mod_t *mod, topo_version_t version)
66 {
67 	/*
68 	 * Turn on module debugging output
69 	 */
70 	if (getenv("TOPOFMDDEBUG"))
71 		topo_mod_setdebug(mod);
72 
73 	topo_mod_dprintf(mod, "initializing fmd builtin\n");
74 
75 	if (version != FMD_VERSION)
76 		return (topo_mod_seterrno(mod, EMOD_VER_NEW));
77 
78 	if (topo_mod_register(mod, &fmd_info, TOPO_VERSION) != 0) {
79 		topo_mod_dprintf(mod, "failed to register fmd: "
80 		    "%s\n", topo_mod_errmsg(mod));
81 		return (-1); /* mod errno already set */
82 	}
83 
84 	return (0);
85 }
86 
87 void
88 fmd_fini(topo_mod_t *mod)
89 {
90 	topo_mod_unregister(mod);
91 }
92 
93 
94 /*ARGSUSED*/
95 int
96 fmd_enum(topo_mod_t *mod, tnode_t *pnode, const char *name, topo_instance_t min,
97     topo_instance_t max, void *notused1, void *notused2)
98 {
99 	(void) topo_method_register(mod, pnode, fmd_methods);
100 	return (0);
101 }
102 
103 /*ARGSUSED*/
104 static void
105 fmd_release(topo_mod_t *mp, tnode_t *node)
106 {
107 	topo_method_unregister_all(mp, node);
108 }
109 
110 static ssize_t
111 fmri_nvl2str(nvlist_t *nvl, char *buf, size_t buflen)
112 {
113 	char *name;
114 
115 	if (nvlist_lookup_string(nvl, FM_FMRI_FMD_NAME, &name) != 0)
116 		return (0);
117 
118 	return (snprintf(buf, buflen,
119 	    "%s:///module/%s", FM_FMRI_SCHEME_FMD, name));
120 }
121 
122 /*ARGSUSED*/
123 static int
124 fmd_fmri_nvl2str(topo_mod_t *mod, tnode_t *node, topo_version_t version,
125     nvlist_t *nvl, nvlist_t **out)
126 {
127 	ssize_t len;
128 	char *name = NULL;
129 	nvlist_t *fmristr;
130 
131 	if (version > TOPO_METH_NVL2STR_VERSION)
132 		return (topo_mod_seterrno(mod, EMOD_VER_NEW));
133 
134 	if ((len = fmri_nvl2str(nvl, NULL, 0)) == 0 ||
135 	    (name = topo_mod_alloc(mod, len + 1)) == NULL ||
136 	    fmri_nvl2str(nvl, name, len + 1) == 0) {
137 		if (name != NULL)
138 			topo_mod_free(mod, name, len + 1);
139 		return (topo_mod_seterrno(mod, EMOD_FMRI_NVL));
140 	}
141 
142 	if (topo_mod_nvalloc(mod, &fmristr, NV_UNIQUE_NAME) != 0) {
143 		topo_mod_free(mod, name, len + 1);
144 		return (topo_mod_seterrno(mod, EMOD_FMRI_NVL));
145 	}
146 	if (nvlist_add_string(fmristr, "fmri-string", name) != 0) {
147 		topo_mod_free(mod, name, len + 1);
148 		nvlist_free(fmristr);
149 		return (topo_mod_seterrno(mod, EMOD_FMRI_NVL));
150 	}
151 	topo_mod_free(mod, name, len + 1);
152 	*out = fmristr;
153 
154 	return (0);
155 }
156