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;
81 if (apr_atomic_inc32(&initialised)) {
82 apr_atomic_set32(&initialised, 1); /* prevent wrap-around */
84 while (apr_atomic_read32(&in_init)) /* wait until we get fully inited */
90 /* Top level pool scope, need process-scope lifetime */
91 for (parent = global = pool; parent; parent = apr_pool_parent_get(global))
94 dsos = apr_hash_make(global);
97 ret = apr_thread_mutex_create(&mutex, APR_THREAD_MUTEX_DEFAULT, global);
98 /* This already registers a pool cleanup */
101 apr_pool_cleanup_register(global, NULL, apu_dso_term,
102 apr_pool_cleanup_null);
104 apr_atomic_dec32(&in_init);
109 apr_status_t apu_dso_load(apr_dso_handle_t **dlhandleptr,
110 apr_dso_handle_sym_t *dsoptr,
115 apr_dso_handle_t *dlhandle = NULL;
117 char path[APR_PATH_MAX + 1];
118 apr_array_header_t *paths;
120 apr_status_t rv = APR_EDSOOPEN;
124 *dsoptr = apr_hash_get(dsos, module, APR_HASH_KEY_STRING);
129 /* The driver DSO must have exactly the same lifetime as the
130 * drivers hash table; ignore the passed-in pool */
131 global = apr_hash_pool_get(dsos);
133 /* Retrieve our path search list or prepare for a single search */
134 if ((apr_env_get(&pathlist, APR_DSOPATH, pool) != APR_SUCCESS)
135 || (apr_filepath_list_split(&paths, pathlist, pool) != APR_SUCCESS))
136 paths = apr_array_make(pool, 1, sizeof(char*));
138 #if defined(APU_DSO_LIBDIR)
139 /* Always search our prefix path, but on some platforms such as
140 * win32 this may be left undefined
142 (*((char **)apr_array_push(paths))) = APU_DSO_LIBDIR;
145 for (i = 0; i < paths->nelts; ++i)
148 /* Use win32 dso search semantics and attempt to
149 * load the relative lib on the first pass.
158 eos = apr_cpystrn(path, ((char**)paths->elts)[i], sizeof(path));
159 if ((eos > path) && (eos - path < sizeof(path) - 1))
162 apr_cpystrn(eos, module, sizeof(path) - (eos - path));
164 rv = apr_dso_load(&dlhandle, path, global);
166 *dlhandleptr = dlhandle;
168 if (rv == APR_SUCCESS) { /* APR_EDSOOPEN */
171 #if defined(APU_DSO_LIBDIR)
172 else if (i < paths->nelts - 1) {
174 else { /* No APU_DSO_LIBDIR to skip */
176 /* try with apr-util-APU_MAJOR_VERSION appended */
177 eos = apr_cpystrn(eos,
178 "apr-util-" APU_STRINGIFY(APU_MAJOR_VERSION) "/",
179 sizeof(path) - (eos - path));
181 apr_cpystrn(eos, module, sizeof(path) - (eos - path));
183 rv = apr_dso_load(&dlhandle, path, global);
185 *dlhandleptr = dlhandle;
187 if (rv == APR_SUCCESS) { /* APR_EDSOOPEN */
193 if (rv != APR_SUCCESS) /* APR_ESYMNOTFOUND */
196 rv = apr_dso_sym(dsoptr, dlhandle, modsym);
197 if (rv != APR_SUCCESS) { /* APR_ESYMNOTFOUND */
198 apr_dso_unload(dlhandle);
201 module = apr_pstrdup(global, module);
202 apr_hash_set(dsos, module, APR_HASH_KEY_STRING, *dsoptr);
207 #endif /* APU_DSO_BUILD */