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 38static apr_thread_mutex_t* mutex = NULL; 39#endif 40static apr_hash_t *dsos = NULL; 41static apr_uint32_t initialised = 0, in_init = 1; 42 43#if APR_HAS_THREADS 44apr_status_t apu_dso_mutex_lock() 45{ 46 return apr_thread_mutex_lock(mutex); 47} 48apr_status_t apu_dso_mutex_unlock() 49{ 50 return apr_thread_mutex_unlock(mutex); 51} 52#else 53apr_status_t apu_dso_mutex_lock() { 54 return APR_SUCCESS; 55} 56apr_status_t apu_dso_mutex_unlock() { 57 return APR_SUCCESS; 58} 59#endif 60 61static 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 75apr_status_t apu_dso_init(apr_pool_t *pool) 76{ 77 apr_status_t ret = APR_SUCCESS; 78 apr_pool_t *global; 79 apr_pool_t *parent; 80 81 if (apr_atomic_inc32(&initialised)) { 82 apr_atomic_set32(&initialised, 1); /* prevent wrap-around */ 83 84 while (apr_atomic_read32(&in_init)) /* wait until we get fully inited */ 85 ; 86 87 return APR_SUCCESS; 88 } 89 90 /* Top level pool scope, need process-scope lifetime */ 91 for (parent = global = pool; parent; parent = apr_pool_parent_get(global)) 92 global = parent; 93 94 dsos = apr_hash_make(global); 95 96#if APR_HAS_THREADS 97 ret = apr_thread_mutex_create(&mutex, APR_THREAD_MUTEX_DEFAULT, global); 98 /* This already registers a pool cleanup */ 99#endif 100 101 apr_pool_cleanup_register(global, NULL, apu_dso_term, 102 apr_pool_cleanup_null); 103 104 apr_atomic_dec32(&in_init); 105 106 return ret; 107} 108 109apr_status_t apu_dso_load(apr_dso_handle_t **dlhandleptr, 110 apr_dso_handle_sym_t *dsoptr, 111 const char *module, 112 const char *modsym, 113 apr_pool_t *pool) 114{ 115 apr_dso_handle_t *dlhandle = NULL; 116 char *pathlist; 117 char path[APR_PATH_MAX + 1]; 118 apr_array_header_t *paths; 119 apr_pool_t *global; 120 apr_status_t rv = APR_EDSOOPEN; 121 char *eos = NULL; 122 int i; 123 124 *dsoptr = apr_hash_get(dsos, module, APR_HASH_KEY_STRING); 125 if (*dsoptr) { 126 return APR_EINIT; 127 } 128 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); 132 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*)); 137 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 141 */ 142 (*((char **)apr_array_push(paths))) = APU_DSO_LIBDIR; 143#endif 144 145 for (i = 0; i < paths->nelts; ++i) 146 { 147#if defined(WIN32) 148 /* Use win32 dso search semantics and attempt to 149 * load the relative lib on the first pass. 150 */ 151 if (!eos) { 152 eos = path; 153 --i; 154 } 155 else 156#endif 157 { 158 eos = apr_cpystrn(path, ((char**)paths->elts)[i], sizeof(path)); 159 if ((eos > path) && (eos - path < sizeof(path) - 1)) 160 *(eos++) = '/'; 161 } 162 apr_cpystrn(eos, module, sizeof(path) - (eos - path)); 163 164 rv = apr_dso_load(&dlhandle, path, global); 165 if (dlhandleptr) { 166 *dlhandleptr = dlhandle; 167 } 168 if (rv == APR_SUCCESS) { /* APR_EDSOOPEN */ 169 break; 170 } 171#if defined(APU_DSO_LIBDIR) 172 else if (i < paths->nelts - 1) { 173#else 174 else { /* No APU_DSO_LIBDIR to skip */ 175#endif 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)); 180 181 apr_cpystrn(eos, module, sizeof(path) - (eos - path)); 182 183 rv = apr_dso_load(&dlhandle, path, global); 184 if (dlhandleptr) { 185 *dlhandleptr = dlhandle; 186 } 187 if (rv == APR_SUCCESS) { /* APR_EDSOOPEN */ 188 break; 189 } 190 } 191 } 192 193 if (rv != APR_SUCCESS) /* APR_ESYMNOTFOUND */ 194 return rv; 195 196 rv = apr_dso_sym(dsoptr, dlhandle, modsym); 197 if (rv != APR_SUCCESS) { /* APR_ESYMNOTFOUND */ 198 apr_dso_unload(dlhandle); 199 } 200 else { 201 module = apr_pstrdup(global, module); 202 apr_hash_set(dsos, module, APR_HASH_KEY_STRING, *dsoptr); 203 } 204 return rv; 205} 206 207#endif /* APU_DSO_BUILD */ 208 209