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