1 karl 1.14 //%2006////////////////////////////////////////////////////////////////////////
|
2 chip 1.1 //
|
3 karl 1.7 // Copyright (c) 2000, 2001, 2002 BMC Software; Hewlett-Packard Development
4 // Company, L.P.; IBM Corp.; The Open Group; Tivoli Systems.
5 // Copyright (c) 2003 BMC Software; Hewlett-Packard Development Company, L.P.;
|
6 karl 1.6 // IBM Corp.; EMC Corporation, The Open Group.
|
7 karl 1.7 // Copyright (c) 2004 BMC Software; Hewlett-Packard Development Company, L.P.;
8 // IBM Corp.; EMC Corporation; VERITAS Software Corporation; The Open Group.
|
9 karl 1.8 // Copyright (c) 2005 Hewlett-Packard Development Company, L.P.; IBM Corp.;
10 // EMC Corporation; VERITAS Software Corporation; The Open Group.
|
11 karl 1.14 // Copyright (c) 2006 Hewlett-Packard Development Company, L.P.; IBM Corp.;
12 // EMC Corporation; Symantec Corporation; The Open Group.
|
13 chip 1.1 //
14 // Permission is hereby granted, free of charge, to any person obtaining a copy
15 // of this software and associated documentation files (the "Software"), to
16 // deal in the Software without restriction, including without limitation the
17 // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
18 // sell copies of the Software, and to permit persons to whom the Software is
19 // furnished to do so, subject to the following conditions:
|
20 karl 1.6 //
|
21 chip 1.1 // THE ABOVE COPYRIGHT NOTICE AND THIS PERMISSION NOTICE SHALL BE INCLUDED IN
22 // ALL COPIES OR SUBSTANTIAL PORTIONS OF THE SOFTWARE. THE SOFTWARE IS PROVIDED
23 // "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
24 // LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
25 // PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
26 // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
27 // ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 //
30 //==============================================================================
31 //
32 //%/////////////////////////////////////////////////////////////////////////////
33
34 #include "DynamicLibrary.h"
35
|
36 kumpf 1.2 #include <Pegasus/Common/ArrayInternal.h>
37 #include <Pegasus/Common/Pair.h>
|
38 kumpf 1.15 #include <Pegasus/Common/System.h>
39 #include <Pegasus/Common/Tracer.h>
|
40 chip 1.1
41 #include <dl.h>
42
43 PEGASUS_NAMESPACE_BEGIN
44
|
45 kumpf 1.20 // the HPUX dynamic library load and unload routines do not keep a reference
46 // count. this implies that if a library were loaded multiple times, a single
|
47 mike 1.10 // unload will release the library from the process thereby potentially leaving
|
48 kumpf 1.20 // dangling references behind. this is a tenative implementation that
|
49 mike 1.10 // encapsulates this behavior from users of the DynamicLibrary object. the goal
|
50 kumpf 1.20 // release the library only after an equal number of loads and unloads have
|
51 mike 1.10 // occured.
|
52 chip 1.1
|
53 kumpf 1.19 static Array<Pair<DynamicLibrary::DynamicLibraryHandle, int> > _references;
|
54 mike 1.10 static Mutex _mutex;
|
55 chip 1.1
|
56 kumpf 1.19 static Uint32 _increment_handle(DynamicLibrary::DynamicLibraryHandle handle)
|
57 chip 1.1 {
|
58 kumpf 1.16 AutoMutex autoMutex(_mutex);
59
|
60 chip 1.1 // seek and increment
|
61 kumpf 1.16 for (Uint32 i = 0, n = _references.size(); i < n; i++)
|
62 chip 1.1 {
|
63 kumpf 1.16 if (handle == _references[i].first)
|
64 chip 1.1 {
|
65 kumpf 1.16 Uint32 n = ++_references[i].second;
|
66 mike 1.10 return n;
|
67 chip 1.1 }
68 }
69
70 // not found, append and set at 1
|
71 kumpf 1.19 _references.append(
72 Pair<DynamicLibrary::DynamicLibraryHandle, int>(handle, 1));
|
73 chip 1.1
|
74 mike 1.10 return 1;
|
75 chip 1.1 }
76
|
77 kumpf 1.19 static Uint32 _decrement_handle(DynamicLibrary::DynamicLibraryHandle handle)
|
78 chip 1.1 {
|
79 kumpf 1.16 AutoMutex autoMutex(_mutex);
80
|
81 chip 1.1 // seek and decrement
|
82 kumpf 1.16 for (Uint32 i = 0, n = _references.size(); i < n; i++)
|
83 chip 1.1 {
|
84 kumpf 1.16 if (handle == _references[i].first)
|
85 chip 1.1 {
|
86 kumpf 1.16 Uint32 n = --_references[i].second;
|
87 chip 1.1
|
88 kumpf 1.20 if (n == 0)
|
89 chip 1.1 {
90 _references.remove(i);
91 }
92
|
93 mike 1.10 return n;
|
94 chip 1.1 }
95 }
96
97 // not found, must be 0
|
98 mike 1.10 return 0;
|
99 chip 1.1 }
100
|
101 kumpf 1.16 Boolean DynamicLibrary::_load()
|
102 chip 1.1 {
|
103 kumpf 1.16 PEG_METHOD_ENTER(TRC_OS_ABSTRACTION, "DynamicLibrary::_load()");
|
104 chip 1.5
105 CString cstr = _fileName.getCString();
106
|
107 marek 1.21 PEG_TRACE((TRC_OS_ABSTRACTION, Tracer::LEVEL2,
108 "Attempting to load library %s", (const char*)cstr));
|
109 chip 1.5
|
110 kumpf 1.15 if (System::bindVerbose)
|
111 chip 1.1 {
|
112 kumpf 1.15 _handle =
113 shl_load(cstr, BIND_IMMEDIATE | DYNAMIC_PATH | BIND_VERBOSE, 0L);
114 }
115 else
116 {
117 _handle = shl_load(cstr, BIND_IMMEDIATE | DYNAMIC_PATH, 0L);
118 }
119
|
120 marek 1.21 PEG_TRACE((TRC_OS_ABSTRACTION, Tracer::LEVEL2,
|
121 kumpf 1.15 "After loading lib %s, error code is %d",
|
122 marek 1.21 (const char*)cstr, (_handle == (void *)0)?errno:0));
|
123 kumpf 1.15
124 if (_handle != 0)
125 {
126 // increment handle if valid
|
127 chip 1.5 _increment_handle(_handle);
|
128 chip 1.1 }
|
129 kumpf 1.15 else
130 {
131 // If shl_load() returns NULL, errno is set to indicate the error
132 _loadErrorMessage = strerror(errno);
133 }
|
134 chip 1.1
|
135 kumpf 1.15 PEG_METHOD_EXIT();
|
136 kumpf 1.20 return isLoaded();
|
137 chip 1.1 }
138
|
139 kumpf 1.16 void DynamicLibrary::_unload()
|
140 chip 1.1 {
|
141 kumpf 1.18 // decrement handle and unload the library only if the reference count is 0
142 if (_decrement_handle(_handle) == 0)
|
143 chip 1.1 {
|
144 kumpf 1.13 shl_unload(reinterpret_cast<shl_t>(_handle));
|
145 chip 1.5 }
|
146 chip 1.1 }
147
|
148 kumpf 1.19 DynamicLibrary::DynamicSymbolHandle DynamicLibrary::getSymbol(
|
149 mike 1.10 const String & symbolName)
|
150 chip 1.1 {
|
151 kumpf 1.16 PEGASUS_ASSERT(isLoaded());
152
|
153 kumpf 1.19 DynamicSymbolHandle func = 0;
|
154 kumpf 1.16 CString cstr = symbolName.getCString();
|
155 chip 1.1
|
156 kumpf 1.16 if (shl_findsym((shl_t *)&_handle, cstr, TYPE_UNDEFINED, &func) == 0)
|
157 chip 1.1 {
|
158 kumpf 1.16 return func;
159 }
|
160 kumpf 1.13
|
161 kumpf 1.16 // NOTE: should the underscore be prepended by the caller or should
162 // this be a compile time option?
|
163 kumpf 1.13
|
164 kumpf 1.16 cstr = String(String("_") + symbolName).getCString();
|
165 kumpf 1.13
|
166 kumpf 1.16 if (shl_findsym((shl_t *)_handle, cstr, TYPE_UNDEFINED, &func) == 0)
167 {
168 return func;
|
169 chip 1.1 }
170
|
171 kumpf 1.16 return 0;
|
172 chip 1.1 }
173
174 PEGASUS_NAMESPACE_END
|