xref: /titanic_41/usr/src/lib/fm/topo/libtopo/common/pkg.c (revision 5203bc321053fb87d7073c7640548fab73634793)
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 2009 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #include <limits.h>
28 #include <strings.h>
29 #include <string.h>
30 #include <unistd.h>
31 #include <stdio.h>
32 #include <alloca.h>
33 #include <libnvpair.h>
34 #include <fm/topo_mod.h>
35 #include <sys/fm/protocol.h>
36 
37 #include <fcntl.h>
38 #include <sys/types.h>
39 #include <sys/stat.h>
40 #include <sys/objfs.h>
41 #include <sys/modctl.h>
42 #include <libelf.h>
43 #include <gelf.h>
44 
45 #include <topo_method.h>
46 #include <topo_subr.h>
47 #include <pkg.h>
48 
49 #define	BUFLEN	(2 * PATH_MAX)
50 
51 static int pkg_enum(topo_mod_t *, tnode_t *, const char *, topo_instance_t,
52     topo_instance_t, void *, void *);
53 static void pkg_release(topo_mod_t *, tnode_t *);
54 static int pkg_fmri_create_meth(topo_mod_t *, tnode_t *, topo_version_t,
55     nvlist_t *, nvlist_t **);
56 static int pkg_fmri_nvl2str(topo_mod_t *, tnode_t *, topo_version_t,
57     nvlist_t *, nvlist_t **);
58 
59 static const topo_method_t pkg_methods[] = {
60 	{ TOPO_METH_FMRI, TOPO_METH_FMRI_DESC, TOPO_METH_FMRI_VERSION,
61 	    TOPO_STABILITY_INTERNAL, pkg_fmri_create_meth },
62 	{ TOPO_METH_NVL2STR, TOPO_METH_NVL2STR_DESC, TOPO_METH_NVL2STR_VERSION,
63 	    TOPO_STABILITY_INTERNAL, pkg_fmri_nvl2str },
64 	{ NULL }
65 };
66 
67 static const topo_modops_t pkg_ops =
68 	{ pkg_enum, pkg_release };
69 static const topo_modinfo_t pkg_info =
70 	{ "pkg", FM_FMRI_SCHEME_PKG, PKG_VERSION, &pkg_ops };
71 
72 int
73 pkg_init(topo_mod_t *mod, topo_version_t version)
74 {
75 	if (getenv("TOPOPKGDEBUG"))
76 		topo_mod_setdebug(mod);
77 	topo_mod_dprintf(mod, "initializing mod builtin\n");
78 
79 	if (version != PKG_VERSION)
80 		return (topo_mod_seterrno(mod, EMOD_VER_NEW));
81 
82 	if (topo_mod_register(mod, &pkg_info, TOPO_VERSION) != 0) {
83 		topo_mod_dprintf(mod, "failed to register pkg_info: "
84 		    "%s\n", topo_mod_errmsg(mod));
85 		return (-1);
86 	}
87 
88 	return (0);
89 }
90 
91 void
92 pkg_fini(topo_mod_t *mod)
93 {
94 	topo_mod_unregister(mod);
95 }
96 
97 /*ARGSUSED*/
98 static int
99 pkg_enum(topo_mod_t *mod, tnode_t *pnode, const char *name,
100     topo_instance_t min, topo_instance_t max, void *notused1, void *notused2)
101 {
102 	(void) topo_method_register(mod, pnode, pkg_methods);
103 	return (0);
104 }
105 
106 static void
107 pkg_release(topo_mod_t *mod, tnode_t *node)
108 {
109 	topo_method_unregister_all(mod, node);
110 }
111 
112 static int
113 read_thru(topo_mod_t *mp, FILE *fp, const char *substr)
114 {
115 	char *tmpbuf = alloca(2 * MAXPATHLEN);
116 	int notfound = 1;
117 
118 	while (fgets(tmpbuf, 2 * MAXPATHLEN, fp) != NULL) {
119 		if (substr == NULL)
120 			topo_mod_dprintf(mp, "%s", tmpbuf);
121 		else if (strstr(tmpbuf, substr) != NULL) {
122 			notfound = 0;
123 			break;
124 		}
125 	}
126 	return (notfound);
127 }
128 
129 static nvlist_t *
130 construct_fru_fmri(topo_mod_t *mp, const char *pkgname, FILE *fp)
131 {
132 	nvlist_t *f = NULL;
133 	char *tmpbuf = alloca(BUFLEN);
134 	char *pkgdir = NULL;
135 	char *pkgver = NULL;
136 	char *token;
137 	int e;
138 
139 	while (fgets(tmpbuf, BUFLEN, fp) != NULL) {
140 		if (strstr(tmpbuf, "VERSION:") != NULL) {
141 			token = strtok(tmpbuf, ":");
142 			token = strtok(NULL, ": \t\n");
143 			pkgver = topo_mod_strdup(mp, token);
144 		} else if (strstr(tmpbuf, "BASEDIR:") != NULL) {
145 			token = strtok(tmpbuf, ":");
146 			token = strtok(NULL, ": \t\n");
147 			pkgdir = topo_mod_strdup(mp, token);
148 		}
149 	}
150 
151 	if (pkgdir == NULL || pkgver == NULL) {
152 		(void) topo_mod_seterrno(mp, EMOD_METHOD_INVAL);
153 		goto fmrileave;
154 	}
155 
156 	if (topo_mod_nvalloc(mp, &f, NV_UNIQUE_NAME) != 0) {
157 		(void) topo_mod_seterrno(mp, EMOD_FMRI_NVL);
158 		goto fmrileave;
159 	}
160 
161 	e = nvlist_add_string(f, FM_FMRI_SCHEME, FM_FMRI_SCHEME_PKG);
162 	e |= nvlist_add_uint8(f, FM_VERSION, FM_PKG_SCHEME_VERSION);
163 	e |= nvlist_add_string(f, FM_FMRI_PKG_BASEDIR, pkgdir);
164 	e |= nvlist_add_string(f, FM_FMRI_PKG_INST, pkgname);
165 	e |= nvlist_add_string(f, FM_FMRI_PKG_VERSION, pkgver);
166 	if (e == 0)
167 		goto fmrileave;
168 
169 	topo_mod_dprintf(mp, "construction of pkg nvl failed");
170 	(void) topo_mod_seterrno(mp, EMOD_FMRI_NVL);
171 	nvlist_free(f);
172 	f = NULL;
173 
174 fmrileave:
175 	if (pkgdir != NULL)
176 		topo_mod_strfree(mp, pkgdir);
177 	if (pkgver != NULL)
178 		topo_mod_strfree(mp, pkgver);
179 
180 	return (f);
181 }
182 
183 #define	PKGINFO_CMD	"LC_MESSAGES= /usr/bin/pkginfo -l %s 2>/dev/null"
184 #define	PKGCHK_CMD	"LC_MESSAGES= /usr/sbin/pkgchk -lp %s 2>/dev/null"
185 #define	PKG_KEYPHRASE	"Referenced by the following packages:"
186 
187 static nvlist_t *
188 pkg_fmri_create(topo_mod_t *mp, const char *path)
189 {
190 	static char tmpbuf[BUFLEN];
191 	char *findpkgname;
192 	char *pkgname = NULL;
193 	FILE *pcout;
194 	nvlist_t *out = NULL;
195 
196 	(void) snprintf(tmpbuf, BUFLEN, PKGCHK_CMD, path);
197 	topo_mod_dprintf(mp, "popen of %s\n", tmpbuf);
198 	pcout = popen(tmpbuf, "r");
199 	if (read_thru(mp, pcout, PKG_KEYPHRASE)) {
200 		(void) pclose(pcout);
201 		goto pfc_bail;
202 	}
203 	(void) fgets(tmpbuf, BUFLEN, pcout);
204 	(void) pclose(pcout);
205 	topo_mod_dprintf(mp, "%s", tmpbuf);
206 
207 	if ((findpkgname = strtok(tmpbuf, " 	\n")) == NULL)
208 		goto pfc_bail;
209 	pkgname = topo_mod_strdup(mp, findpkgname);
210 
211 	(void) snprintf(tmpbuf, BUFLEN, PKGINFO_CMD, pkgname);
212 	topo_mod_dprintf(mp, "popen of %s\n", tmpbuf);
213 	pcout = popen(tmpbuf, "r");
214 	out = construct_fru_fmri(mp, pkgname, pcout);
215 	(void) pclose(pcout);
216 
217 pfc_bail:
218 	if (pkgname != NULL)
219 		topo_mod_strfree(mp, pkgname);
220 	return (out);
221 }
222 
223 /*ARGSUSED*/
224 static int
225 pkg_fmri_create_meth(topo_mod_t *mp, tnode_t *node, topo_version_t version,
226     nvlist_t *in, nvlist_t **out)
227 {
228 	nvlist_t *args = NULL;
229 	char *path;
230 
231 	if (version > TOPO_METH_FMRI_VERSION)
232 		return (topo_mod_seterrno(mp, EMOD_VER_NEW));
233 
234 	if (nvlist_lookup_nvlist(in, TOPO_METH_FMRI_ARG_NVL, &args) != 0 ||
235 	    nvlist_lookup_string(args, "path", &path) != 0) {
236 		topo_mod_dprintf(mp, "no path string in method argument\n");
237 		return (topo_mod_seterrno(mp, EMOD_METHOD_INVAL));
238 	}
239 
240 	if ((*out = pkg_fmri_create(mp, path)) == NULL)
241 		return (-1);
242 	return (0);
243 }
244 
245 static ssize_t
246 fmri_nvl2str(nvlist_t *nvl, char *buf, size_t buflen)
247 {
248 	nvlist_t *anvl = NULL;
249 	nvpair_t *apair;
250 	uint8_t version;
251 	ssize_t size = 0;
252 	char *pkgname = NULL, *aname, *aval;
253 	int err;
254 
255 	if (nvlist_lookup_uint8(nvl, FM_VERSION, &version) != 0 ||
256 	    version > FM_PKG_SCHEME_VERSION)
257 		return (-1);
258 
259 	/* Get authority, if present */
260 	err = nvlist_lookup_nvlist(nvl, FM_FMRI_AUTHORITY, &anvl);
261 	if (err != 0 && err != ENOENT)
262 		return (-1);
263 
264 	/*
265 	 *  For brevity, we only include the pkgname and any authority
266 	 *  info present in the FMRI in our output string.  The FMRI
267 	 *  also has data on the package directory and version.
268 	 */
269 	err = nvlist_lookup_string(nvl, FM_FMRI_PKG_INST, &pkgname);
270 	if (err != 0 || pkgname == NULL)
271 		return (-1);
272 
273 	/* pkg:// */
274 	topo_fmristr_build(&size, buf, buflen, FM_FMRI_SCHEME_PKG, NULL, "://");
275 
276 	/* authority, if any */
277 	if (anvl != NULL) {
278 		for (apair = nvlist_next_nvpair(anvl, NULL);
279 		    apair != NULL; apair = nvlist_next_nvpair(anvl, apair)) {
280 			if (nvpair_type(apair) != DATA_TYPE_STRING ||
281 			    nvpair_value_string(apair, &aval) != 0)
282 				continue;
283 			aname = nvpair_name(apair);
284 			topo_fmristr_build(&size, buf, buflen, ":", NULL, NULL);
285 			topo_fmristr_build(&size, buf, buflen, "=",
286 			    aname, aval);
287 		}
288 	}
289 
290 	/* pkg-name part */
291 	topo_fmristr_build(&size, buf, buflen, pkgname, "/", NULL);
292 
293 	return (size);
294 }
295 
296 /*ARGSUSED*/
297 static int
298 pkg_fmri_nvl2str(topo_mod_t *mod, tnode_t *node, topo_version_t version,
299     nvlist_t *nvl, nvlist_t **out)
300 {
301 	ssize_t len;
302 	char *name = NULL;
303 	nvlist_t *fmristr;
304 
305 	if (version > TOPO_METH_NVL2STR_VERSION)
306 		return (topo_mod_seterrno(mod, EMOD_VER_NEW));
307 
308 	if ((len = fmri_nvl2str(nvl, NULL, 0)) == 0 ||
309 	    (name = topo_mod_alloc(mod, len + 1)) == NULL ||
310 	    fmri_nvl2str(nvl, name, len + 1) == 0) {
311 		if (name != NULL)
312 			topo_mod_free(mod, name, len + 1);
313 		return (topo_mod_seterrno(mod, EMOD_FMRI_NVL));
314 	}
315 
316 	if (topo_mod_nvalloc(mod, &fmristr, NV_UNIQUE_NAME) != 0)
317 		return (topo_mod_seterrno(mod, EMOD_FMRI_NVL));
318 	if (nvlist_add_string(fmristr, "fmri-string", name) != 0) {
319 		topo_mod_free(mod, name, len + 1);
320 		nvlist_free(fmristr);
321 		return (topo_mod_seterrno(mod, EMOD_FMRI_NVL));
322 	}
323 	topo_mod_free(mod, name, len + 1);
324 	*out = fmristr;
325 
326 	return (0);
327 }
328