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 * Copyright 2023 Oxide Computer Company 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 pkg 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 /* 103 * Methods are registered, but there is no enumeration. Should 104 * enumeration be added be sure to cater for global vs non-global 105 * zones. 106 */ 107 (void) topo_method_register(mod, pnode, pkg_methods); 108 return (0); 109 } 110 111 static void 112 pkg_release(topo_mod_t *mod, tnode_t *node) 113 { 114 topo_method_unregister_all(mod, node); 115 } 116 117 static int 118 read_thru(topo_mod_t *mp, FILE *fp, const char *substr) 119 { 120 char *tmpbuf = alloca(2 * MAXPATHLEN); 121 int notfound = 1; 122 123 while (fgets(tmpbuf, 2 * MAXPATHLEN, fp) != NULL) { 124 if (substr == NULL) 125 topo_mod_dprintf(mp, "%s", tmpbuf); 126 else if (strstr(tmpbuf, substr) != NULL) { 127 notfound = 0; 128 break; 129 } 130 } 131 return (notfound); 132 } 133 134 static nvlist_t * 135 construct_fru_fmri(topo_mod_t *mp, const char *pkgname, FILE *fp) 136 { 137 nvlist_t *f = NULL; 138 char *tmpbuf = alloca(BUFLEN); 139 char *pkgdir = NULL; 140 char *pkgver = NULL; 141 char *token; 142 int e; 143 144 while (fgets(tmpbuf, BUFLEN, fp) != NULL) { 145 if (strstr(tmpbuf, "VERSION:") != NULL) { 146 token = strtok(tmpbuf, ":"); 147 token = strtok(NULL, ": \t\n"); 148 pkgver = topo_mod_strdup(mp, token); 149 } else if (strstr(tmpbuf, "BASEDIR:") != NULL) { 150 token = strtok(tmpbuf, ":"); 151 token = strtok(NULL, ": \t\n"); 152 pkgdir = topo_mod_strdup(mp, token); 153 } 154 } 155 156 if (pkgdir == NULL || pkgver == NULL) { 157 (void) topo_mod_seterrno(mp, EMOD_METHOD_INVAL); 158 goto fmrileave; 159 } 160 161 if (topo_mod_nvalloc(mp, &f, NV_UNIQUE_NAME) != 0) { 162 (void) topo_mod_seterrno(mp, EMOD_FMRI_NVL); 163 goto fmrileave; 164 } 165 166 e = nvlist_add_string(f, FM_FMRI_SCHEME, FM_FMRI_SCHEME_PKG); 167 e |= nvlist_add_uint8(f, FM_VERSION, FM_PKG_SCHEME_VERSION); 168 e |= nvlist_add_string(f, FM_FMRI_PKG_BASEDIR, pkgdir); 169 e |= nvlist_add_string(f, FM_FMRI_PKG_INST, pkgname); 170 e |= nvlist_add_string(f, FM_FMRI_PKG_VERSION, pkgver); 171 if (e == 0) 172 goto fmrileave; 173 174 topo_mod_dprintf(mp, "construction of pkg nvl failed"); 175 (void) topo_mod_seterrno(mp, EMOD_FMRI_NVL); 176 nvlist_free(f); 177 f = NULL; 178 179 fmrileave: 180 if (pkgdir != NULL) 181 topo_mod_strfree(mp, pkgdir); 182 if (pkgver != NULL) 183 topo_mod_strfree(mp, pkgver); 184 185 return (f); 186 } 187 188 #define PKGINFO_CMD "LC_MESSAGES= /usr/bin/pkginfo -l %s 2>/dev/null" 189 #define PKGCHK_CMD "LC_MESSAGES= /usr/sbin/pkgchk -lp %s 2>/dev/null" 190 #define PKG_KEYPHRASE "Referenced by the following packages:" 191 192 static nvlist_t * 193 pkg_fmri_create(topo_mod_t *mp, const char *path) 194 { 195 static char tmpbuf[BUFLEN]; 196 char *findpkgname; 197 char *pkgname = NULL; 198 FILE *pcout; 199 nvlist_t *out = NULL; 200 201 (void) snprintf(tmpbuf, BUFLEN, PKGCHK_CMD, path); 202 topo_mod_dprintf(mp, "popen of %s\n", tmpbuf); 203 pcout = popen(tmpbuf, "r"); 204 if (read_thru(mp, pcout, PKG_KEYPHRASE)) { 205 (void) pclose(pcout); 206 goto pfc_bail; 207 } 208 (void) fgets(tmpbuf, BUFLEN, pcout); 209 (void) pclose(pcout); 210 topo_mod_dprintf(mp, "%s", tmpbuf); 211 212 if ((findpkgname = strtok(tmpbuf, " \t\n")) == NULL) 213 goto pfc_bail; 214 pkgname = topo_mod_strdup(mp, findpkgname); 215 216 (void) snprintf(tmpbuf, BUFLEN, PKGINFO_CMD, pkgname); 217 topo_mod_dprintf(mp, "popen of %s\n", tmpbuf); 218 pcout = popen(tmpbuf, "r"); 219 out = construct_fru_fmri(mp, pkgname, pcout); 220 (void) pclose(pcout); 221 222 pfc_bail: 223 if (pkgname != NULL) 224 topo_mod_strfree(mp, pkgname); 225 return (out); 226 } 227 228 /*ARGSUSED*/ 229 static int 230 pkg_fmri_create_meth(topo_mod_t *mp, tnode_t *node, topo_version_t version, 231 nvlist_t *in, nvlist_t **out) 232 { 233 nvlist_t *args = NULL; 234 char *path; 235 236 if (version > TOPO_METH_FMRI_VERSION) 237 return (topo_mod_seterrno(mp, EMOD_VER_NEW)); 238 239 if (nvlist_lookup_nvlist(in, TOPO_METH_FMRI_ARG_NVL, &args) != 0 || 240 nvlist_lookup_string(args, "path", &path) != 0) { 241 topo_mod_dprintf(mp, "no path string in method argument\n"); 242 return (topo_mod_seterrno(mp, EMOD_METHOD_INVAL)); 243 } 244 245 if ((*out = pkg_fmri_create(mp, path)) == NULL) 246 return (-1); 247 return (0); 248 } 249 250 static size_t 251 fmri_nvl2str(nvlist_t *nvl, char *buf, size_t buflen) 252 { 253 nvlist_t *anvl = NULL; 254 nvpair_t *apair; 255 uint8_t version; 256 size_t size = 0; 257 char *pkgname = NULL, *aname, *aval; 258 int err; 259 260 if (nvlist_lookup_uint8(nvl, FM_VERSION, &version) != 0 || 261 version > FM_PKG_SCHEME_VERSION) 262 return (0); 263 264 /* Get authority, if present */ 265 err = nvlist_lookup_nvlist(nvl, FM_FMRI_AUTHORITY, &anvl); 266 if (err != 0 && err != ENOENT) 267 return (0); 268 269 /* 270 * For brevity, we only include the pkgname and any authority 271 * info present in the FMRI in our output string. The FMRI 272 * also has data on the package directory and version. 273 */ 274 err = nvlist_lookup_string(nvl, FM_FMRI_PKG_INST, &pkgname); 275 if (err != 0 || pkgname == NULL) 276 return (0); 277 278 /* pkg:// */ 279 if (!topo_fmristr_build(&size, buf, buflen, FM_FMRI_SCHEME_PKG, NULL, 280 "://")) { 281 return (0); 282 } 283 284 /* authority, if any */ 285 if (anvl != NULL) { 286 for (apair = nvlist_next_nvpair(anvl, NULL); 287 apair != NULL; apair = nvlist_next_nvpair(anvl, apair)) { 288 if (nvpair_type(apair) != DATA_TYPE_STRING || 289 nvpair_value_string(apair, &aval) != 0) 290 continue; 291 aname = nvpair_name(apair); 292 if (!topo_fmristr_build(&size, buf, buflen, 293 ":", NULL, NULL) || 294 !topo_fmristr_build(&size, buf, buflen, 295 "=", aname, aval)) { 296 return (0); 297 } 298 } 299 } 300 301 /* pkg-name part */ 302 if (!topo_fmristr_build(&size, buf, buflen, pkgname, "/", NULL)) 303 return (0); 304 305 return (size); 306 } 307 308 /*ARGSUSED*/ 309 static int 310 pkg_fmri_nvl2str(topo_mod_t *mod, tnode_t *node, topo_version_t version, 311 nvlist_t *nvl, nvlist_t **out) 312 { 313 size_t len; 314 char *name = NULL; 315 nvlist_t *fmristr; 316 317 if (version > TOPO_METH_NVL2STR_VERSION) 318 return (topo_mod_seterrno(mod, EMOD_VER_NEW)); 319 320 if ((len = fmri_nvl2str(nvl, NULL, 0)) == 0 || 321 (name = topo_mod_alloc(mod, len + 1)) == NULL || 322 fmri_nvl2str(nvl, name, len + 1) == 0) { 323 if (name != NULL) 324 topo_mod_free(mod, name, len + 1); 325 return (topo_mod_seterrno(mod, EMOD_FMRI_NVL)); 326 } 327 328 if (topo_mod_nvalloc(mod, &fmristr, NV_UNIQUE_NAME) != 0) 329 return (topo_mod_seterrno(mod, EMOD_FMRI_NVL)); 330 if (nvlist_add_string(fmristr, "fmri-string", name) != 0) { 331 topo_mod_free(mod, name, len + 1); 332 nvlist_free(fmristr); 333 return (topo_mod_seterrno(mod, EMOD_FMRI_NVL)); 334 } 335 topo_mod_free(mod, name, len + 1); 336 *out = fmristr; 337 338 return (0); 339 } 340