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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 #include "apu_config.h"
23 #include "apr_pools.h"
24 #include "apr_tables.h"
26 #include "apr_strings.h"
28 #include "apr_file_io.h"
30 #include "apr_atomic.h"
32 #include "apu_internal.h"
33 #include "apu_version.h"
38 static apr_thread_mutex_t* mutex = NULL;
40 static apr_hash_t *dsos = NULL;
41 static apr_uint32_t initialised = 0, in_init = 1;
44 apr_status_t apu_dso_mutex_lock()
46 return apr_thread_mutex_lock(mutex);
48 apr_status_t apu_dso_mutex_unlock()
50 return apr_thread_mutex_unlock(mutex);
53 apr_status_t apu_dso_mutex_lock() {
56 apr_status_t apu_dso_mutex_unlock() {
61 static apr_status_t apu_dso_term(void *ptr)
63 /* set statics to NULL so init can work again */
69 /* Everything else we need is handled by cleanups registered
70 * when we created mutexes and loaded DSOs
75 apr_status_t apu_dso_init(apr_pool_t *pool)
77 apr_status_t ret = APR_SUCCESS;
80 if (apr_atomic_inc32(&initialised)) {
81 apr_atomic_set32(&initialised, 1); /* prevent wrap-around */
83 while (apr_atomic_read32(&in_init)) /* wait until we get fully inited */
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))
95 dsos = apr_hash_make(pool);
98 ret = apr_thread_mutex_create(&mutex, APR_THREAD_MUTEX_DEFAULT, pool);
99 /* This already registers a pool cleanup */
102 apr_pool_cleanup_register(pool, NULL, apu_dso_term,
103 apr_pool_cleanup_null);
105 apr_atomic_dec32(&in_init);
110 apr_status_t apu_dso_load(apr_dso_handle_t **dlhandleptr,
111 apr_dso_handle_sym_t *dsoptr,
116 apr_dso_handle_t *dlhandle = NULL;
118 char path[APR_PATH_MAX + 1];
119 apr_array_header_t *paths;
121 apr_status_t rv = APR_EDSOOPEN;
125 *dsoptr = apr_hash_get(dsos, module, APR_HASH_KEY_STRING);
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);
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*));
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
143 (*((char **)apr_array_push(paths))) = APU_DSO_LIBDIR;
146 for (i = 0; i < paths->nelts; ++i)
149 /* Use win32 dso search semantics and attempt to
150 * load the relative lib on the first pass.
159 eos = apr_cpystrn(path, ((char**)paths->elts)[i], sizeof(path));
160 if ((eos > path) && (eos - path < sizeof(path) - 1))
163 apr_cpystrn(eos, module, sizeof(path) - (eos - path));
165 rv = apr_dso_load(&dlhandle, path, global);
167 *dlhandleptr = dlhandle;
169 if (rv == APR_SUCCESS) { /* APR_EDSOOPEN */
172 #if defined(APU_DSO_LIBDIR)
173 else if (i < paths->nelts - 1) {
175 else { /* No APU_DSO_LIBDIR to skip */
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));
182 apr_cpystrn(eos, module, sizeof(path) - (eos - path));
184 rv = apr_dso_load(&dlhandle, path, global);
186 *dlhandleptr = dlhandle;
188 if (rv == APR_SUCCESS) { /* APR_EDSOOPEN */
194 if (rv != APR_SUCCESS) /* APR_ESYMNOTFOUND */
197 rv = apr_dso_sym(dsoptr, dlhandle, modsym);
198 if (rv != APR_SUCCESS) { /* APR_ESYMNOTFOUND */
199 apr_dso_unload(dlhandle);
202 module = apr_pstrdup(global, module);
203 apr_hash_set(dsos, module, APR_HASH_KEY_STRING, *dsoptr);
208 #endif /* APU_DSO_BUILD */