10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
50Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance
70Sstevel@tonic-gate * with the License.
80Sstevel@tonic-gate *
90Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate * See the License for the specific language governing permissions
120Sstevel@tonic-gate * and limitations under the License.
130Sstevel@tonic-gate *
140Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate *
200Sstevel@tonic-gate * CDDL HEADER END
210Sstevel@tonic-gate */
220Sstevel@tonic-gate /*
230Sstevel@tonic-gate * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
240Sstevel@tonic-gate * Use is subject to license terms.
250Sstevel@tonic-gate */
260Sstevel@tonic-gate
270Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
280Sstevel@tonic-gate
290Sstevel@tonic-gate #include <dlfcn.h>
300Sstevel@tonic-gate #include <link.h>
310Sstevel@tonic-gate
320Sstevel@tonic-gate #include <fmd_module.h>
330Sstevel@tonic-gate #include <fmd_error.h>
340Sstevel@tonic-gate #include <fmd_alloc.h>
350Sstevel@tonic-gate #include <fmd_subr.h>
360Sstevel@tonic-gate #include <fmd_event.h>
370Sstevel@tonic-gate #include <fmd.h>
380Sstevel@tonic-gate
390Sstevel@tonic-gate typedef struct fmd_rtld {
400Sstevel@tonic-gate void *rtld_dlp; /* libdl(3DL) handle for shared library */
410Sstevel@tonic-gate void (*rtld_init)(fmd_hdl_t *); /* shared library's _fmd_init() */
420Sstevel@tonic-gate void (*rtld_fini)(fmd_hdl_t *); /* shared library's _fmd_fini() */
430Sstevel@tonic-gate } fmd_rtld_t;
440Sstevel@tonic-gate
450Sstevel@tonic-gate static int
rtld_init(fmd_module_t * mp)460Sstevel@tonic-gate rtld_init(fmd_module_t *mp)
470Sstevel@tonic-gate {
480Sstevel@tonic-gate fmd_rtld_t *rp;
490Sstevel@tonic-gate void *dlp;
500Sstevel@tonic-gate
510Sstevel@tonic-gate if ((dlp = dlopen(mp->mod_path, RTLD_LOCAL | RTLD_NOW)) == NULL) {
520Sstevel@tonic-gate fmd_error(EFMD_RTLD_OPEN, "%s\n", dlerror());
530Sstevel@tonic-gate return (fmd_set_errno(EFMD_RTLD_OPEN));
540Sstevel@tonic-gate }
550Sstevel@tonic-gate
560Sstevel@tonic-gate rp = mp->mod_data = fmd_alloc(sizeof (fmd_rtld_t), FMD_SLEEP);
570Sstevel@tonic-gate
580Sstevel@tonic-gate rp->rtld_dlp = dlp;
590Sstevel@tonic-gate rp->rtld_init = (void (*)())dlsym(dlp, "_fmd_init");
600Sstevel@tonic-gate rp->rtld_fini = (void (*)())dlsym(dlp, "_fmd_fini");
610Sstevel@tonic-gate
620Sstevel@tonic-gate if (rp->rtld_init == NULL) {
630Sstevel@tonic-gate (void) dlclose(dlp);
640Sstevel@tonic-gate fmd_free(rp, sizeof (fmd_rtld_t));
650Sstevel@tonic-gate return (fmd_set_errno(EFMD_RTLD_INIT));
660Sstevel@tonic-gate }
670Sstevel@tonic-gate
680Sstevel@tonic-gate (void) pthread_mutex_unlock(&mp->mod_lock);
690Sstevel@tonic-gate
700Sstevel@tonic-gate /*
710Sstevel@tonic-gate * Call _fmd_init() in the module. If this causes a module abort and
720Sstevel@tonic-gate * mod_info has been registered, unregister it on behalf of the module.
730Sstevel@tonic-gate */
740Sstevel@tonic-gate if (fmd_module_enter(mp, rp->rtld_init) != 0 && mp->mod_info != NULL)
750Sstevel@tonic-gate fmd_hdl_unregister((fmd_hdl_t *)mp);
760Sstevel@tonic-gate
770Sstevel@tonic-gate fmd_module_exit(mp);
780Sstevel@tonic-gate (void) pthread_mutex_lock(&mp->mod_lock);
790Sstevel@tonic-gate
800Sstevel@tonic-gate if (mp->mod_info == NULL) {
810Sstevel@tonic-gate (void) dlclose(dlp);
820Sstevel@tonic-gate fmd_free(rp, sizeof (fmd_rtld_t));
830Sstevel@tonic-gate return (fmd_set_errno(EFMD_HDL_INIT));
840Sstevel@tonic-gate }
850Sstevel@tonic-gate
860Sstevel@tonic-gate return (0);
870Sstevel@tonic-gate }
880Sstevel@tonic-gate
890Sstevel@tonic-gate static int
rtld_fini(fmd_module_t * mp)900Sstevel@tonic-gate rtld_fini(fmd_module_t *mp)
910Sstevel@tonic-gate {
920Sstevel@tonic-gate fmd_rtld_t *rp = mp->mod_data;
930Sstevel@tonic-gate int doclose = 1, err = 0;
940Sstevel@tonic-gate
950Sstevel@tonic-gate if (mp->mod_info != NULL) {
960Sstevel@tonic-gate (void) fmd_module_enter(mp, rp->rtld_fini);
970Sstevel@tonic-gate
980Sstevel@tonic-gate if (mp->mod_info != NULL) {
990Sstevel@tonic-gate fmd_module_lock(mp);
1000Sstevel@tonic-gate fmd_module_unregister(mp);
1010Sstevel@tonic-gate fmd_module_unlock(mp);
1020Sstevel@tonic-gate }
1030Sstevel@tonic-gate
1040Sstevel@tonic-gate fmd_module_exit(mp);
1050Sstevel@tonic-gate }
1060Sstevel@tonic-gate
1070Sstevel@tonic-gate (void) fmd_conf_getprop(fmd.d_conf, "plugin.close", &doclose);
1080Sstevel@tonic-gate if (doclose)
1090Sstevel@tonic-gate err = dlclose(rp->rtld_dlp);
1100Sstevel@tonic-gate
1110Sstevel@tonic-gate fmd_free(rp, sizeof (fmd_rtld_t));
1120Sstevel@tonic-gate return (err);
1130Sstevel@tonic-gate }
1140Sstevel@tonic-gate
1150Sstevel@tonic-gate const fmd_modops_t fmd_rtld_ops = {
1160Sstevel@tonic-gate rtld_init,
1170Sstevel@tonic-gate rtld_fini,
1180Sstevel@tonic-gate fmd_module_dispatch,
119*1193Smws fmd_module_transport,
1200Sstevel@tonic-gate };
121