7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi/*
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi * CDDL HEADER START
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi *
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi * The contents of this file are subject to the terms of the
d08448cecf91bf1d4905a2ea150beed5d97eb822cindi * Common Development and Distribution License (the "License").
d08448cecf91bf1d4905a2ea150beed5d97eb822cindi * You may not use this file except in compliance with the License.
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi *
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi * or http://www.opensolaris.org/os/licensing.
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi * See the License for the specific language governing permissions
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi * and limitations under the License.
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi *
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi * When distributing Covered Code, include this CDDL HEADER in each
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi * If applicable, add the following below this CDDL HEADER, with the
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi * fields enclosed by brackets "[]" replaced with your own identifying
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi * information: Portions Copyright [yyyy] [name of copyright owner]
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi *
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi * CDDL HEADER END
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi */
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi/*
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi * Use is subject to license terms.
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi */
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi#pragma ident "%Z%%M% %I% %E% SMI"
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi#include <dlfcn.h>
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi#include <link.h>
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi#include <pthread.h>
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi#include <assert.h>
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi#include <fm/topo_mod.h>
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi#include <topo_error.h>
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi#include <topo_alloc.h>
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi#include <topo_subr.h>
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecinditypedef struct topo_rtld {
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi void *rtld_dlp; /* libdl(3DL) handle for shared library */
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi int (*rtld_init)(topo_mod_t *, topo_version_t); /* .so _topo_init() */
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi void (*rtld_fini)(topo_mod_t *); /* .so _topo_fini() */
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi} topo_rtld_t;
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindistatic int
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindirtld_fini(topo_mod_t *mod)
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi{
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi topo_rtld_t *rp = mod->tm_data;
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi assert(mod != NULL);
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi if (mod->tm_flags & TOPO_MOD_REG) {
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi rp->rtld_fini(mod);
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi if (mod->tm_flags & TOPO_MOD_REG) {
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi topo_mod_unregister(mod);
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi }
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi }
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi if (getenv("TOPONODLCLOSE") == NULL)
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi (void) dlclose(rp->rtld_dlp);
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi topo_mod_free(mod, rp, sizeof (topo_rtld_t));
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi return (0);
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi}
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindistatic int
0eb822a1c0c2bea495647510b75f77f0e57633ebcindirtld_init(topo_mod_t *mod, topo_version_t version)
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi{
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi int err;
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi topo_rtld_t *rp;
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi void *dlp;
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi if ((dlp = dlopen(mod->tm_path, RTLD_LOCAL | RTLD_NOW)) == NULL) {
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi topo_dprintf(mod->tm_hdl, TOPO_DBG_ERR,
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi "dlopen() failed: %s\n", dlerror());
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi return (topo_mod_seterrno(mod, ETOPO_RTLD_OPEN));
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi }
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi if ((rp = mod->tm_data = topo_mod_alloc(mod, sizeof (topo_rtld_t)))
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi == NULL)
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi return (topo_mod_seterrno(mod, ETOPO_RTLD_OPEN));
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi rp->rtld_dlp = dlp;
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi rp->rtld_init = (int (*)())dlsym(dlp, "_topo_init");
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi rp->rtld_fini = (void (*)())dlsym(dlp, "_topo_fini");
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi if (rp->rtld_init == NULL) {
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi (void) dlclose(dlp);
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi topo_free(rp, sizeof (topo_rtld_t));
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi return (topo_mod_seterrno(mod, ETOPO_RTLD_INIT));
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi }
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi /*
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi * Call _topo_init() in the module.
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi */
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi err = rp->rtld_init(mod, version);
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi if (err < 0 || !(mod->tm_flags & TOPO_MOD_REG)) {
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi (void) rtld_fini(mod);
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi return (topo_mod_seterrno(mod, ETOPO_MOD_NOREG));
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi }
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi return (0);
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi}
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi
0eb822a1c0c2bea495647510b75f77f0e57633ebcindiconst topo_imodops_t topo_rtld_ops = {
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi rtld_init,
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi rtld_fini,
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi};