1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate * CDDL HEADER START
3*0Sstevel@tonic-gate *
4*0Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance
7*0Sstevel@tonic-gate * with the License.
8*0Sstevel@tonic-gate *
9*0Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate * and limitations under the License.
13*0Sstevel@tonic-gate *
14*0Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate *
20*0Sstevel@tonic-gate * CDDL HEADER END
21*0Sstevel@tonic-gate */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate * Copyright (c) 1994, by Sun Microsytems, Inc.
24*0Sstevel@tonic-gate */
25*0Sstevel@tonic-gate
26*0Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
27*0Sstevel@tonic-gate
28*0Sstevel@tonic-gate /*
29*0Sstevel@tonic-gate * Functions to sync up library list with that of the run time linker
30*0Sstevel@tonic-gate */
31*0Sstevel@tonic-gate
32*0Sstevel@tonic-gate #ifndef DEBUG
33*0Sstevel@tonic-gate #define NDEBUG 1
34*0Sstevel@tonic-gate #endif
35*0Sstevel@tonic-gate
36*0Sstevel@tonic-gate #include <assert.h>
37*0Sstevel@tonic-gate
38*0Sstevel@tonic-gate #include "tnfctl_int.h"
39*0Sstevel@tonic-gate #include "kernel_int.h"
40*0Sstevel@tonic-gate #include "dbg.h"
41*0Sstevel@tonic-gate
42*0Sstevel@tonic-gate tnfctl_errcode_t
tnfctl_check_libs(tnfctl_handle_t * hndl)43*0Sstevel@tonic-gate tnfctl_check_libs(tnfctl_handle_t *hndl)
44*0Sstevel@tonic-gate {
45*0Sstevel@tonic-gate tnfctl_errcode_t prexstat;
46*0Sstevel@tonic-gate enum event_op_t dl_evt;
47*0Sstevel@tonic-gate boolean_t lmap_ok;
48*0Sstevel@tonic-gate
49*0Sstevel@tonic-gate if (hndl->mode == KERNEL_MODE) {
50*0Sstevel@tonic-gate prexstat = _tnfctl_refresh_kernel(hndl);
51*0Sstevel@tonic-gate return (prexstat);
52*0Sstevel@tonic-gate }
53*0Sstevel@tonic-gate
54*0Sstevel@tonic-gate /* hndl refers to a process */
55*0Sstevel@tonic-gate
56*0Sstevel@tonic-gate /* return value of lmap_ok, dl_evt ignored */
57*0Sstevel@tonic-gate prexstat = _tnfctl_refresh_process(hndl, &lmap_ok, &dl_evt);
58*0Sstevel@tonic-gate assert(lmap_ok == B_TRUE);
59*0Sstevel@tonic-gate return (prexstat);
60*0Sstevel@tonic-gate }
61*0Sstevel@tonic-gate
62*0Sstevel@tonic-gate /*
63*0Sstevel@tonic-gate * _tnfctl_lock_libs() locks the library list maintained in the prex
64*0Sstevel@tonic-gate * handle against it changing by a dlopen or dlclose.
65*0Sstevel@tonic-gate *
66*0Sstevel@tonic-gate * This locking code is between the thread that owns the handle and
67*0Sstevel@tonic-gate * another thread that may be doing a dlopen/dlclose. We do not support
68*0Sstevel@tonic-gate * the situation of having multiple threads operating on the same
69*0Sstevel@tonic-gate * handle - in effect the code per tnfctl_handle can be thought of as
70*0Sstevel@tonic-gate * being single-threaded - that is why we can check "hndl->in_objlist"
71*0Sstevel@tonic-gate * without first obtaining a lock. "in_objlist" is the re-entrancy
72*0Sstevel@tonic-gate * protector on the lock i.e. we can call _tnfctl_lock_libs() safely even if
73*0Sstevel@tonic-gate * this thread has the lock held. The return value "release_lock" indicates
74*0Sstevel@tonic-gate * whether the lock should be released or not. It can be passed into
75*0Sstevel@tonic-gate * _tnfctl_unlock_libs() which will do the right thing.
76*0Sstevel@tonic-gate *
77*0Sstevel@tonic-gate * All of this is a bit too complex for warlock. Here we give warlock a
78*0Sstevel@tonic-gate * simpler view, that these routines have the side effect of acquiring and
79*0Sstevel@tonic-gate * releasing the lock, period.
80*0Sstevel@tonic-gate */
81*0Sstevel@tonic-gate
82*0Sstevel@tonic-gate tnfctl_errcode_t
_tnfctl_lock_libs(tnfctl_handle_t * hndl,boolean_t * release_lock)83*0Sstevel@tonic-gate _tnfctl_lock_libs(tnfctl_handle_t *hndl, boolean_t *release_lock)
84*0Sstevel@tonic-gate {
85*0Sstevel@tonic-gate #if defined(__lock_lint)
86*0Sstevel@tonic-gate
87*0Sstevel@tonic-gate NOTE(MUTEX_ACQUIRED_AS_SIDE_EFFECT(&warlock_kludge->lmap_lock))
88*0Sstevel@tonic-gate mutex_lock(&warlock_kludge->lmap_lock);
89*0Sstevel@tonic-gate
90*0Sstevel@tonic-gate #else
91*0Sstevel@tonic-gate
92*0Sstevel@tonic-gate /* this interface is only for INTERNAL_MODE clients */
93*0Sstevel@tonic-gate assert(hndl->mode == INTERNAL_MODE);
94*0Sstevel@tonic-gate
95*0Sstevel@tonic-gate if (hndl->in_objlist == B_TRUE) {
96*0Sstevel@tonic-gate /*
97*0Sstevel@tonic-gate * already have _tnfctl_lmap_lock held which implies that
98*0Sstevel@tonic-gate * the library list has been sync'ed up
99*0Sstevel@tonic-gate */
100*0Sstevel@tonic-gate *release_lock = B_FALSE;
101*0Sstevel@tonic-gate return (TNFCTL_ERR_NONE);
102*0Sstevel@tonic-gate }
103*0Sstevel@tonic-gate
104*0Sstevel@tonic-gate /* lock is not currently held, so lock it */
105*0Sstevel@tonic-gate mutex_lock(&_tnfctl_lmap_lock);
106*0Sstevel@tonic-gate hndl->in_objlist = B_TRUE;
107*0Sstevel@tonic-gate
108*0Sstevel@tonic-gate *release_lock = B_TRUE;
109*0Sstevel@tonic-gate return (TNFCTL_ERR_NONE);
110*0Sstevel@tonic-gate
111*0Sstevel@tonic-gate #endif
112*0Sstevel@tonic-gate }
113*0Sstevel@tonic-gate
114*0Sstevel@tonic-gate void
_tnfctl_unlock_libs(tnfctl_handle_t * hndl,boolean_t release_lock)115*0Sstevel@tonic-gate _tnfctl_unlock_libs(tnfctl_handle_t *hndl, boolean_t release_lock)
116*0Sstevel@tonic-gate {
117*0Sstevel@tonic-gate #if defined(__lock_lint)
118*0Sstevel@tonic-gate
119*0Sstevel@tonic-gate NOTE(LOCK_RELEASED_AS_SIDE_EFFECT(&warlock_kludge->lmap_lock))
120*0Sstevel@tonic-gate mutex_unlock(&warlock_kludge->lmap_lock);
121*0Sstevel@tonic-gate
122*0Sstevel@tonic-gate #else
123*0Sstevel@tonic-gate
124*0Sstevel@tonic-gate /* this interface is only for INTERNAL_MODE clients */
125*0Sstevel@tonic-gate assert(hndl->mode == INTERNAL_MODE);
126*0Sstevel@tonic-gate
127*0Sstevel@tonic-gate if (release_lock) {
128*0Sstevel@tonic-gate hndl->in_objlist = B_FALSE;
129*0Sstevel@tonic-gate mutex_unlock(&_tnfctl_lmap_lock);
130*0Sstevel@tonic-gate }
131*0Sstevel@tonic-gate
132*0Sstevel@tonic-gate #endif
133*0Sstevel@tonic-gate }
134*0Sstevel@tonic-gate
135*0Sstevel@tonic-gate /*
136*0Sstevel@tonic-gate * _tnfctl_syn_lib_list() syncs up the library list maintained
137*0Sstevel@tonic-gate * in the prex handle with the libraries in the process (if needed)
138*0Sstevel@tonic-gate * NOTE: Assumes _tnfctl_lmap_lock is held.
139*0Sstevel@tonic-gate *
140*0Sstevel@tonic-gate */
141*0Sstevel@tonic-gate tnfctl_errcode_t
_tnfctl_sync_lib_list(tnfctl_handle_t * hndl)142*0Sstevel@tonic-gate _tnfctl_sync_lib_list(tnfctl_handle_t *hndl)
143*0Sstevel@tonic-gate {
144*0Sstevel@tonic-gate tnfctl_errcode_t prexstat = TNFCTL_ERR_NONE;
145*0Sstevel@tonic-gate enum event_op_t dl_evt;
146*0Sstevel@tonic-gate boolean_t lmap_ok;
147*0Sstevel@tonic-gate
148*0Sstevel@tonic-gate /* this interface is only for INTERNAL_MODE clients */
149*0Sstevel@tonic-gate assert(hndl->mode == INTERNAL_MODE);
150*0Sstevel@tonic-gate
151*0Sstevel@tonic-gate /* lmap_lock held and in_objlist marked as B_TRUE */
152*0Sstevel@tonic-gate if (_tnfctl_libs_changed == B_TRUE) {
153*0Sstevel@tonic-gate prexstat = _tnfctl_refresh_process(hndl, &lmap_ok, &dl_evt);
154*0Sstevel@tonic-gate if (prexstat) {
155*0Sstevel@tonic-gate return (prexstat);
156*0Sstevel@tonic-gate }
157*0Sstevel@tonic-gate }
158*0Sstevel@tonic-gate return (TNFCTL_ERR_NONE);
159*0Sstevel@tonic-gate }
160