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_sym_t *dsoptr,
110                          const char *module,
111                          const char *modsym,
112                          apr_pool_t *pool)
113{
114    apr_dso_handle_t *dlhandle = NULL;
115    char *pathlist;
116    char path[APR_PATH_MAX + 1];
117    apr_array_header_t *paths;
118    apr_pool_t *global;
119    apr_status_t rv = APR_EDSOOPEN;
120    char *eos = NULL;
121    int i;
122
123    *dsoptr = apr_hash_get(dsos, module, APR_HASH_KEY_STRING);
124    if (*dsoptr) {
125        return APR_EINIT;
126    }
127
128    /* The driver DSO must have exactly the same lifetime as the
129     * drivers hash table; ignore the passed-in pool */
130    global = apr_hash_pool_get(dsos);
131
132    /* Retrieve our path search list or prepare for a single search */
133    if ((apr_env_get(&pathlist, APR_DSOPATH, pool) != APR_SUCCESS)
134          || (apr_filepath_list_split(&paths, pathlist, pool) != APR_SUCCESS))
135        paths = apr_array_make(pool, 1, sizeof(char*));
136
137#if defined(APU_DSO_LIBDIR)
138    /* Always search our prefix path, but on some platforms such as
139     * win32 this may be left undefined
140     */
141    (*((char **)apr_array_push(paths))) = APU_DSO_LIBDIR;
142#endif
143
144    for (i = 0; i < paths->nelts; ++i)
145    {
146#if defined(WIN32)
147        /* Use win32 dso search semantics and attempt to
148         * load the relative lib on the first pass.
149         */
150        if (!eos) {
151            eos = path;
152            --i;
153        }
154        else
155#endif
156        {
157            eos = apr_cpystrn(path, ((char**)paths->elts)[i], sizeof(path));
158            if ((eos > path) && (eos - path < sizeof(path) - 1))
159                *(eos++) = '/';
160        }
161        apr_cpystrn(eos, module, sizeof(path) - (eos - path));
162
163        rv = apr_dso_load(&dlhandle, path, global);
164        if (rv == APR_SUCCESS) { /* APR_EDSOOPEN */
165            break;
166        }
167#if defined(APU_DSO_LIBDIR)
168        else if (i < paths->nelts - 1) {
169#else
170        else {   /* No APU_DSO_LIBDIR to skip */
171#endif
172             /* try with apr-util-APU_MAJOR_VERSION appended */
173            eos = apr_cpystrn(eos,
174                              "apr-util-" APU_STRINGIFY(APU_MAJOR_VERSION) "/",
175                              sizeof(path) - (eos - path));
176
177            apr_cpystrn(eos, module, sizeof(path) - (eos - path));
178
179            rv = apr_dso_load(&dlhandle, path, global);
180            if (rv == APR_SUCCESS) { /* APR_EDSOOPEN */
181                break;
182            }
183        }
184    }
185
186    if (rv != APR_SUCCESS) /* APR_ESYMNOTFOUND */
187        return rv;
188
189    rv = apr_dso_sym(dsoptr, dlhandle, modsym);
190    if (rv != APR_SUCCESS) { /* APR_ESYMNOTFOUND */
191        apr_dso_unload(dlhandle);
192    }
193    else {
194        module = apr_pstrdup(global, module);
195        apr_hash_set(dsos, module, APR_HASH_KEY_STRING, *dsoptr);
196    }
197    return rv;
198}
199
200#endif /* APU_DSO_BUILD */
201
202