2N/A/*
2N/A * CDDL HEADER START
2N/A *
2N/A * The contents of this file are subject to the terms of the
2N/A * Common Development and Distribution License (the "License").
2N/A * You may not use this file except in compliance with the License.
2N/A *
2N/A * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
2N/A * or http://www.opensolaris.org/os/licensing.
2N/A * See the License for the specific language governing permissions
2N/A * and limitations under the License.
2N/A *
2N/A * When distributing Covered Code, include this CDDL HEADER in each
2N/A * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
2N/A * If applicable, add the following below this CDDL HEADER, with the
2N/A * fields enclosed by brackets "[]" replaced with your own identifying
2N/A * information: Portions Copyright [yyyy] [name of copyright owner]
2N/A *
2N/A * CDDL HEADER END
2N/A */
2N/A
2N/A/*
2N/A * Copyright (c) 2006, 2012, Oracle and/or its affiliates. All rights reserved.
2N/A */
2N/A
2N/A#include <limits.h>
2N/A#include <strings.h>
2N/A#include <string.h>
2N/A#include <unistd.h>
2N/A#include <stdio.h>
2N/A#include <alloca.h>
2N/A#include <libnvpair.h>
2N/A#include <fm/topo_mod.h>
2N/A#include <sys/fm/protocol.h>
2N/A
2N/A#include <fcntl.h>
2N/A#include <sys/types.h>
2N/A#include <sys/stat.h>
2N/A#include <sys/objfs.h>
2N/A#include <sys/modctl.h>
2N/A#include <libelf.h>
2N/A#include <gelf.h>
2N/A
2N/A#include <topo_method.h>
2N/A#include <topo_subr.h>
2N/A#include <pkg.h>
2N/A
2N/Astatic int pkg_enum(topo_mod_t *, tnode_t *, const char *, topo_instance_t,
2N/A topo_instance_t, void *, void *);
2N/Astatic void pkg_release(topo_mod_t *, tnode_t *);
2N/Astatic int pkg_fmri_nvl2str(topo_mod_t *, tnode_t *, topo_version_t,
2N/A nvlist_t *, nvlist_t **);
2N/A
2N/Astatic const topo_method_t pkg_methods[] = {
2N/A { TOPO_METH_NVL2STR, TOPO_METH_NVL2STR_DESC, TOPO_METH_NVL2STR_VERSION,
2N/A TOPO_STABILITY_INTERNAL, pkg_fmri_nvl2str },
2N/A { NULL }
2N/A};
2N/A
2N/Astatic const topo_modops_t pkg_ops =
2N/A { pkg_enum, pkg_release };
2N/Astatic const topo_modinfo_t pkg_info =
2N/A { "pkg", FM_FMRI_SCHEME_PKG, PKG_VERSION, &pkg_ops };
2N/A
2N/Aint
2N/Apkg_init(topo_mod_t *mod, topo_version_t version)
2N/A{
2N/A if (getenv("TOPOPKGDEBUG"))
2N/A topo_mod_setdebug(mod);
2N/A topo_mod_dprintf(mod, "initializing pkg builtin\n");
2N/A
2N/A if (version != PKG_VERSION)
2N/A return (topo_mod_seterrno(mod, EMOD_VER_NEW));
2N/A
2N/A if (topo_mod_register(mod, &pkg_info, TOPO_VERSION) != 0) {
2N/A topo_mod_dprintf(mod, "failed to register pkg_info: "
2N/A "%s\n", topo_mod_errmsg(mod));
2N/A return (-1);
2N/A }
2N/A
2N/A return (0);
2N/A}
2N/A
2N/Avoid
2N/Apkg_fini(topo_mod_t *mod)
2N/A{
2N/A topo_mod_unregister(mod);
2N/A}
2N/A
2N/A/*ARGSUSED*/
2N/Astatic int
2N/Apkg_enum(topo_mod_t *mod, tnode_t *pnode, const char *name,
2N/A topo_instance_t min, topo_instance_t max, void *notused1, void *notused2)
2N/A{
2N/A /*
2N/A * Methods are registered, but there is no enumeration. Should
2N/A * enumeration be added be sure to cater for global vs non-global
2N/A * zones.
2N/A */
2N/A (void) topo_method_register(mod, pnode, pkg_methods);
2N/A return (0);
2N/A}
2N/A
2N/Astatic void
2N/Apkg_release(topo_mod_t *mod, tnode_t *node)
2N/A{
2N/A topo_method_unregister_all(mod, node);
2N/A}
2N/A
2N/Astatic ssize_t
2N/Afmri_nvl2str(nvlist_t *nvl, char *buf, size_t buflen)
2N/A{
2N/A nvlist_t *anvl = NULL;
2N/A nvpair_t *apair;
2N/A uint8_t version;
2N/A ssize_t size = 0;
2N/A char *pkgname = NULL, *aname, *aval;
2N/A int err;
2N/A
2N/A if (nvlist_lookup_uint8(nvl, FM_VERSION, &version) != 0 ||
2N/A version > FM_PKG_SCHEME_VERSION)
2N/A return (-1);
2N/A
2N/A /* Get authority, if present */
2N/A err = nvlist_lookup_nvlist(nvl, FM_FMRI_AUTHORITY, &anvl);
2N/A if (err != 0 && err != ENOENT)
2N/A return (-1);
2N/A
2N/A /*
2N/A * For brevity, we only include the pkgname and any authority
2N/A * info present in the FMRI in our output string. The FMRI
2N/A * also has data on the package directory and version.
2N/A */
2N/A err = nvlist_lookup_string(nvl, FM_FMRI_PKG_INST, &pkgname);
2N/A if (err != 0 || pkgname == NULL)
2N/A return (-1);
2N/A
2N/A /* pkg:// */
2N/A topo_fmristr_build(&size, buf, buflen, FM_FMRI_SCHEME_PKG, NULL, "://");
2N/A
2N/A /* authority, if any */
2N/A if (anvl != NULL) {
2N/A for (apair = nvlist_next_nvpair(anvl, NULL);
2N/A apair != NULL; apair = nvlist_next_nvpair(anvl, apair)) {
2N/A if (nvpair_type(apair) != DATA_TYPE_STRING ||
2N/A nvpair_value_string(apair, &aval) != 0)
2N/A continue;
2N/A aname = nvpair_name(apair);
2N/A /* omit chassis alias and host identity */
2N/A if (strcmp(aname, FM_FMRI_AUTH_V1_CHASSIS_ALIAS) == 0)
2N/A continue;
2N/A topo_fmristr_build(&size, buf, buflen, ":", NULL, NULL);
2N/A topo_fmristr_build(&size, buf, buflen, "=",
2N/A aname, aval);
2N/A }
2N/A }
2N/A
2N/A /* pkg-name part */
2N/A topo_fmristr_build(&size, buf, buflen, pkgname, "/", NULL);
2N/A
2N/A return (size);
2N/A}
2N/A
2N/A/*ARGSUSED*/
2N/Astatic int
2N/Apkg_fmri_nvl2str(topo_mod_t *mod, tnode_t *node, topo_version_t version,
2N/A nvlist_t *nvl, nvlist_t **out)
2N/A{
2N/A ssize_t len;
2N/A char *name = NULL;
2N/A nvlist_t *fmristr;
2N/A
2N/A if (version > TOPO_METH_NVL2STR_VERSION)
2N/A return (topo_mod_seterrno(mod, EMOD_VER_NEW));
2N/A
2N/A if ((len = fmri_nvl2str(nvl, NULL, 0)) == 0 ||
2N/A (name = topo_mod_alloc(mod, len + 1)) == NULL ||
2N/A fmri_nvl2str(nvl, name, len + 1) == 0) {
2N/A if (name != NULL)
2N/A topo_mod_free(mod, name, len + 1);
2N/A return (topo_mod_seterrno(mod, EMOD_FMRI_NVL));
2N/A }
2N/A
2N/A if (topo_mod_nvalloc(mod, &fmristr, NV_UNIQUE_NAME) != 0)
2N/A return (topo_mod_seterrno(mod, EMOD_FMRI_NVL));
2N/A if (nvlist_add_string(fmristr, "fmri-string", name) != 0) {
2N/A topo_mod_free(mod, name, len + 1);
2N/A nvlist_free(fmristr);
2N/A return (topo_mod_seterrno(mod, EMOD_FMRI_NVL));
2N/A }
2N/A topo_mod_free(mod, name, len + 1);
2N/A *out = fmristr;
2N/A
2N/A return (0);
2N/A}