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 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
2N/A * Use is subject to license terms.
2N/A */
2N/A
2N/A#pragma ident "%Z%%M% %I% %E% SMI"
2N/A
2N/A#include <dlfcn.h>
2N/A#include <link.h>
2N/A#include <pthread.h>
2N/A#include <assert.h>
2N/A
2N/A#include <fm/topo_mod.h>
2N/A
2N/A#include <topo_error.h>
2N/A#include <topo_alloc.h>
2N/A#include <topo_subr.h>
2N/A
2N/Atypedef struct topo_rtld {
2N/A void *rtld_dlp; /* libdl(3DL) handle for shared library */
2N/A int (*rtld_init)(topo_mod_t *, topo_version_t); /* .so _topo_init() */
2N/A void (*rtld_fini)(topo_mod_t *); /* .so _topo_fini() */
2N/A} topo_rtld_t;
2N/A
2N/Astatic int
2N/Artld_fini(topo_mod_t *mod)
2N/A{
2N/A topo_rtld_t *rp = mod->tm_data;
2N/A
2N/A assert(mod != NULL);
2N/A
2N/A if (mod->tm_flags & TOPO_MOD_REG) {
2N/A rp->rtld_fini(mod);
2N/A if (mod->tm_flags & TOPO_MOD_REG) {
2N/A topo_mod_unregister(mod);
2N/A }
2N/A }
2N/A
2N/A if (getenv("TOPONODLCLOSE") == NULL)
2N/A (void) dlclose(rp->rtld_dlp);
2N/A topo_mod_free(mod, rp, sizeof (topo_rtld_t));
2N/A
2N/A return (0);
2N/A}
2N/A
2N/Astatic int
2N/Artld_init(topo_mod_t *mod, topo_version_t version)
2N/A{
2N/A int err;
2N/A topo_rtld_t *rp;
2N/A void *dlp;
2N/A
2N/A if ((dlp = dlopen(mod->tm_path, RTLD_LOCAL | RTLD_NOW)) == NULL) {
2N/A topo_dprintf(mod->tm_hdl, TOPO_DBG_ERR,
2N/A "dlopen() failed: %s\n", dlerror());
2N/A return (topo_mod_seterrno(mod, ETOPO_RTLD_OPEN));
2N/A }
2N/A
2N/A if ((rp = mod->tm_data = topo_mod_alloc(mod, sizeof (topo_rtld_t)))
2N/A == NULL)
2N/A return (topo_mod_seterrno(mod, ETOPO_RTLD_OPEN));
2N/A
2N/A rp->rtld_dlp = dlp;
2N/A rp->rtld_init = (int (*)())dlsym(dlp, "_topo_init");
2N/A rp->rtld_fini = (void (*)())dlsym(dlp, "_topo_fini");
2N/A
2N/A if (rp->rtld_init == NULL) {
2N/A (void) dlclose(dlp);
2N/A topo_free(rp, sizeof (topo_rtld_t));
2N/A return (topo_mod_seterrno(mod, ETOPO_RTLD_INIT));
2N/A }
2N/A
2N/A /*
2N/A * Call _topo_init() in the module.
2N/A */
2N/A err = rp->rtld_init(mod, version);
2N/A
2N/A if (err < 0 || !(mod->tm_flags & TOPO_MOD_REG)) {
2N/A (void) rtld_fini(mod);
2N/A return (topo_mod_seterrno(mod, ETOPO_MOD_NOREG));
2N/A }
2N/A
2N/A return (0);
2N/A}
2N/A
2N/Aconst topo_imodops_t topo_rtld_ops = {
2N/A rtld_init,
2N/A rtld_fini,
2N/A};