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