xref: /titanic_41/usr/src/lib/fm/topo/libtopo/common/pkg.c (revision cb6207858a9fcc2feaee22e626912fba281ac969)
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 <string.h>
32 #include <unistd.h>
33 #include <stdio.h>
34 #include <alloca.h>
35 #include <libnvpair.h>
36 #include <fm/topo_mod.h>
37 #include <sys/fm/protocol.h>
38 
39 #include <fcntl.h>
40 #include <sys/types.h>
41 #include <sys/stat.h>
42 #include <sys/objfs.h>
43 #include <sys/modctl.h>
44 #include <libelf.h>
45 #include <gelf.h>
46 
47 #include <topo_method.h>
48 #include <pkg.h>
49 
50 #define	BUFLEN	(2 * PATH_MAX)
51 
52 static int pkg_enum(topo_mod_t *, tnode_t *, const char *, topo_instance_t,
53     topo_instance_t, void *, void *);
54 static void pkg_release(topo_mod_t *, tnode_t *);
55 static int pkg_fmri_create_meth(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 	{ NULL }
62 };
63 
64 static const topo_modops_t pkg_ops =
65 	{ pkg_enum, pkg_release };
66 static const topo_modinfo_t pkg_info =
67 	{ "pkg", FM_FMRI_SCHEME_PKG, PKG_VERSION, &pkg_ops };
68 
69 int
70 pkg_init(topo_mod_t *mod, topo_version_t version)
71 {
72 	if (getenv("TOPOPKGDEBUG"))
73 		topo_mod_setdebug(mod);
74 	topo_mod_dprintf(mod, "initializing mod builtin\n");
75 
76 	if (version != PKG_VERSION)
77 		return (topo_mod_seterrno(mod, EMOD_VER_NEW));
78 
79 	if (topo_mod_register(mod, &pkg_info, TOPO_VERSION) != 0) {
80 		topo_mod_dprintf(mod, "failed to register pkg_info: "
81 		    "%s\n", topo_mod_errmsg(mod));
82 		return (-1);
83 	}
84 
85 	return (0);
86 }
87 
88 void
89 pkg_fini(topo_mod_t *mod)
90 {
91 	topo_mod_unregister(mod);
92 }
93 
94 /*ARGSUSED*/
95 static int
96 pkg_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, pkg_methods);
100 	return (0);
101 }
102 
103 static void
104 pkg_release(topo_mod_t *mod, tnode_t *node)
105 {
106 	topo_method_unregister_all(mod, node);
107 }
108 
109 static int
110 read_thru(topo_mod_t *mp, FILE *fp, const char *substr)
111 {
112 	char *tmpbuf = alloca(2 * MAXPATHLEN);
113 	int notfound = 1;
114 
115 	while (fgets(tmpbuf, 2 * MAXPATHLEN, fp) != NULL) {
116 		if (substr == NULL)
117 			topo_mod_dprintf(mp, "%s", tmpbuf);
118 		else if (strstr(tmpbuf, substr) != NULL) {
119 			notfound = 0;
120 			break;
121 		}
122 	}
123 	return (notfound);
124 }
125 
126 static nvlist_t *
127 construct_fru_fmri(topo_mod_t *mp, const char *pkgname, FILE *fp)
128 {
129 	nvlist_t *f = NULL;
130 	char *tmpbuf = alloca(BUFLEN);
131 	char *pkgdir = NULL;
132 	char *pkgver = NULL;
133 	char *token;
134 	int e;
135 
136 	while (fgets(tmpbuf, BUFLEN, fp) != NULL) {
137 		if (strstr(tmpbuf, "VERSION:") != NULL) {
138 			token = strtok(tmpbuf, ":");
139 			token = strtok(NULL, ": \t\n");
140 			pkgver = topo_mod_strdup(mp, token);
141 		} else if (strstr(tmpbuf, "BASEDIR:") != NULL) {
142 			token = strtok(tmpbuf, ":");
143 			token = strtok(NULL, ": \t\n");
144 			pkgdir = topo_mod_strdup(mp, token);
145 		}
146 	}
147 
148 	if (pkgdir == NULL || pkgver == NULL) {
149 		(void) topo_mod_seterrno(mp, EMOD_METHOD_INVAL);
150 		goto fmrileave;
151 	}
152 
153 	if (topo_mod_nvalloc(mp, &f, NV_UNIQUE_NAME) != 0) {
154 		(void) topo_mod_seterrno(mp, EMOD_FMRI_NVL);
155 		goto fmrileave;
156 	}
157 
158 	e = nvlist_add_string(f, FM_FMRI_SCHEME, FM_FMRI_SCHEME_PKG);
159 	e |= nvlist_add_uint8(f, FM_VERSION, FM_PKG_SCHEME_VERSION);
160 	e |= nvlist_add_string(f, FM_FMRI_PKG_BASEDIR, pkgdir);
161 	e |= nvlist_add_string(f, FM_FMRI_PKG_INST, pkgname);
162 	e |= nvlist_add_string(f, FM_FMRI_PKG_VERSION, pkgver);
163 	if (e == 0)
164 		goto fmrileave;
165 
166 	topo_mod_dprintf(mp, "construction of pkg nvl failed");
167 	(void) topo_mod_seterrno(mp, EMOD_FMRI_NVL);
168 	nvlist_free(f);
169 	f = NULL;
170 
171 fmrileave:
172 	if (pkgdir != NULL)
173 		topo_mod_strfree(mp, pkgdir);
174 	if (pkgver != NULL)
175 		topo_mod_strfree(mp, pkgver);
176 
177 	return (f);
178 }
179 
180 #define	PKGINFO_CMD	"LC_MESSAGES= /usr/bin/pkginfo -l %s 2>/dev/null"
181 #define	PKGCHK_CMD	"LC_MESSAGES= /usr/sbin/pkgchk -lp %s 2>/dev/null"
182 #define	PKG_KEYPHRASE	"Referenced by the following packages:"
183 
184 static nvlist_t *
185 pkg_fmri_create(topo_mod_t *mp, const char *path)
186 {
187 	static char tmpbuf[BUFLEN];
188 	char *findpkgname;
189 	char *pkgname = NULL;
190 	FILE *pcout;
191 	nvlist_t *out = NULL;
192 
193 	(void) snprintf(tmpbuf, BUFLEN, PKGCHK_CMD, path);
194 	topo_mod_dprintf(mp, "popen of %s\n", tmpbuf);
195 	pcout = popen(tmpbuf, "r");
196 	if (read_thru(mp, pcout, PKG_KEYPHRASE)) {
197 		(void) pclose(pcout);
198 		goto pfc_bail;
199 	}
200 	(void) fgets(tmpbuf, BUFLEN, pcout);
201 	(void) pclose(pcout);
202 	topo_mod_dprintf(mp, "%s", tmpbuf);
203 
204 	if ((findpkgname = strtok(tmpbuf, " 	\n")) == NULL)
205 		goto pfc_bail;
206 	pkgname = topo_mod_strdup(mp, findpkgname);
207 
208 	(void) snprintf(tmpbuf, BUFLEN, PKGINFO_CMD, pkgname);
209 	topo_mod_dprintf(mp, "popen of %s\n", tmpbuf);
210 	pcout = popen(tmpbuf, "r");
211 	out = construct_fru_fmri(mp, pkgname, pcout);
212 	(void) pclose(pcout);
213 
214 pfc_bail:
215 	if (pkgname != NULL)
216 		topo_mod_strfree(mp, pkgname);
217 	return (out);
218 }
219 
220 /*ARGSUSED*/
221 static int
222 pkg_fmri_create_meth(topo_mod_t *mp, tnode_t *node, topo_version_t version,
223     nvlist_t *in, nvlist_t **out)
224 {
225 	nvlist_t *args = NULL;
226 	char *path;
227 
228 	if (version > TOPO_METH_FMRI_VERSION)
229 		return (topo_mod_seterrno(mp, EMOD_VER_NEW));
230 
231 	if (nvlist_lookup_nvlist(in, TOPO_METH_FMRI_ARG_NVL, &args) != 0 ||
232 	    nvlist_lookup_string(args, "path", &path) != 0) {
233 		topo_mod_dprintf(mp, "no path string in method argument\n");
234 		return (topo_mod_seterrno(mp, EMOD_METHOD_INVAL));
235 	}
236 
237 	if ((*out = pkg_fmri_create(mp, path)) == NULL)
238 		return (-1);
239 	return (0);
240 }
241