1 /* Licensed to the Apache Software Foundation (ASF) under one or more
2 * contributor license agreements. See the NOTICE file distributed with
3 * this work for additional information regarding copyright ownership.
4 * The ASF licenses this file to You under the Apache License, Version 2.0
5 * (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include <ctype.h>
18 #include <stdio.h>
19
20 #include "apu_config.h"
21 #include "apu.h"
22
23 #include "apr_pools.h"
24 #include "apr_tables.h"
25 #include "apr_dso.h"
26 #include "apr_strings.h"
27 #include "apr_hash.h"
28 #include "apr_file_io.h"
29 #include "apr_env.h"
30 #include "apr_atomic.h"
31
32 #include "apu_internal.h"
33 #include "apu_version.h"
34
35 #if APU_DSO_BUILD
36
37 #if APR_HAS_THREADS
38 static apr_thread_mutex_t* mutex = NULL;
39 #endif
40 static apr_hash_t *dsos = NULL;
41 static apr_uint32_t initialised = 0, in_init = 1;
42
43 #if APR_HAS_THREADS
apu_dso_mutex_lock()44 apr_status_t apu_dso_mutex_lock()
45 {
46 return apr_thread_mutex_lock(mutex);
47 }
apu_dso_mutex_unlock()48 apr_status_t apu_dso_mutex_unlock()
49 {
50 return apr_thread_mutex_unlock(mutex);
51 }
52 #else
apu_dso_mutex_lock()53 apr_status_t apu_dso_mutex_lock() {
54 return APR_SUCCESS;
55 }
apu_dso_mutex_unlock()56 apr_status_t apu_dso_mutex_unlock() {
57 return APR_SUCCESS;
58 }
59 #endif
60
apu_dso_term(void * ptr)61 static apr_status_t apu_dso_term(void *ptr)
62 {
63 /* set statics to NULL so init can work again */
64 dsos = NULL;
65 #if APR_HAS_THREADS
66 mutex = NULL;
67 #endif
68
69 /* Everything else we need is handled by cleanups registered
70 * when we created mutexes and loaded DSOs
71 */
72 return APR_SUCCESS;
73 }
74
apu_dso_init(apr_pool_t * pool)75 apr_status_t apu_dso_init(apr_pool_t *pool)
76 {
77 apr_status_t ret = APR_SUCCESS;
78 apr_pool_t *parent;
79
80 if (apr_atomic_inc32(&initialised)) {
81 apr_atomic_set32(&initialised, 1); /* prevent wrap-around */
82
83 while (apr_atomic_read32(&in_init)) /* wait until we get fully inited */
84 ;
85
86 return APR_SUCCESS;
87 }
88
89 /* Top level pool scope, need process-scope lifetime */
90 for (parent = apr_pool_parent_get(pool);
91 parent && parent != pool;
92 parent = apr_pool_parent_get(pool))
93 pool = parent;
94
95 dsos = apr_hash_make(pool);
96
97 #if APR_HAS_THREADS
98 ret = apr_thread_mutex_create(&mutex, APR_THREAD_MUTEX_DEFAULT, pool);
99 /* This already registers a pool cleanup */
100 #endif
101
102 apr_pool_cleanup_register(pool, NULL, apu_dso_term,
103 apr_pool_cleanup_null);
104
105 apr_atomic_dec32(&in_init);
106
107 return ret;
108 }
109
apu_dso_load(apr_dso_handle_t ** dlhandleptr,apr_dso_handle_sym_t * dsoptr,const char * module,const char * modsym,apr_pool_t * pool)110 apr_status_t apu_dso_load(apr_dso_handle_t **dlhandleptr,
111 apr_dso_handle_sym_t *dsoptr,
112 const char *module,
113 const char *modsym,
114 apr_pool_t *pool)
115 {
116 apr_dso_handle_t *dlhandle = NULL;
117 char *pathlist;
118 char path[APR_PATH_MAX + 1];
119 apr_array_header_t *paths;
120 apr_pool_t *global;
121 apr_status_t rv = APR_EDSOOPEN;
122 char *eos = NULL;
123 int i;
124
125 *dsoptr = apr_hash_get(dsos, module, APR_HASH_KEY_STRING);
126 if (*dsoptr) {
127 return APR_EINIT;
128 }
129
130 /* The driver DSO must have exactly the same lifetime as the
131 * drivers hash table; ignore the passed-in pool */
132 global = apr_hash_pool_get(dsos);
133
134 /* Retrieve our path search list or prepare for a single search */
135 if ((apr_env_get(&pathlist, APR_DSOPATH, pool) != APR_SUCCESS)
136 || (apr_filepath_list_split(&paths, pathlist, pool) != APR_SUCCESS))
137 paths = apr_array_make(pool, 1, sizeof(char*));
138
139 #if defined(APU_DSO_LIBDIR)
140 /* Always search our prefix path, but on some platforms such as
141 * win32 this may be left undefined
142 */
143 (*((char **)apr_array_push(paths))) = APU_DSO_LIBDIR;
144 #endif
145
146 for (i = 0; i < paths->nelts; ++i)
147 {
148 #if defined(WIN32)
149 /* Use win32 dso search semantics and attempt to
150 * load the relative lib on the first pass.
151 */
152 if (!eos) {
153 eos = path;
154 --i;
155 }
156 else
157 #endif
158 {
159 eos = apr_cpystrn(path, ((char**)paths->elts)[i], sizeof(path));
160 if ((eos > path) && (eos - path < sizeof(path) - 1))
161 *(eos++) = '/';
162 }
163 apr_cpystrn(eos, module, sizeof(path) - (eos - path));
164
165 rv = apr_dso_load(&dlhandle, path, global);
166 if (dlhandleptr) {
167 *dlhandleptr = dlhandle;
168 }
169 if (rv == APR_SUCCESS) { /* APR_EDSOOPEN */
170 break;
171 }
172 #if defined(APU_DSO_LIBDIR)
173 else if (i < paths->nelts - 1) {
174 #else
175 else { /* No APU_DSO_LIBDIR to skip */
176 #endif
177 /* try with apr-util-APU_MAJOR_VERSION appended */
178 eos = apr_cpystrn(eos,
179 "apr-util-" APU_STRINGIFY(APU_MAJOR_VERSION) "/",
180 sizeof(path) - (eos - path));
181
182 apr_cpystrn(eos, module, sizeof(path) - (eos - path));
183
184 rv = apr_dso_load(&dlhandle, path, global);
185 if (dlhandleptr) {
186 *dlhandleptr = dlhandle;
187 }
188 if (rv == APR_SUCCESS) { /* APR_EDSOOPEN */
189 break;
190 }
191 }
192 }
193
194 if (rv != APR_SUCCESS) /* APR_ESYMNOTFOUND */
195 return rv;
196
197 rv = apr_dso_sym(dsoptr, dlhandle, modsym);
198 if (rv != APR_SUCCESS) { /* APR_ESYMNOTFOUND */
199 apr_dso_unload(dlhandle);
200 }
201 else {
202 module = apr_pstrdup(global, module);
203 apr_hash_set(dsos, module, APR_HASH_KEY_STRING, *dsoptr);
204 }
205 return rv;
206 }
207
208 #endif /* APU_DSO_BUILD */
209
210