Blame misc/apu_dso.c

Packit 383869
/* Licensed to the Apache Software Foundation (ASF) under one or more
Packit 383869
 * contributor license agreements.  See the NOTICE file distributed with
Packit 383869
 * this work for additional information regarding copyright ownership.
Packit 383869
 * The ASF licenses this file to You under the Apache License, Version 2.0
Packit 383869
 * (the "License"); you may not use this file except in compliance with
Packit 383869
 * the License.  You may obtain a copy of the License at
Packit 383869
 *
Packit 383869
 *     http://www.apache.org/licenses/LICENSE-2.0
Packit 383869
 *
Packit 383869
 * Unless required by applicable law or agreed to in writing, software
Packit 383869
 * distributed under the License is distributed on an "AS IS" BASIS,
Packit 383869
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Packit 383869
 * See the License for the specific language governing permissions and
Packit 383869
 * limitations under the License.
Packit 383869
 */
Packit 383869
Packit 383869
#include <ctype.h>
Packit 383869
#include <stdio.h>
Packit 383869
Packit 383869
#include "apu_config.h"
Packit 383869
#include "apu.h"
Packit 383869
Packit 383869
#include "apr_pools.h"
Packit 383869
#include "apr_tables.h"
Packit 383869
#include "apr_dso.h"
Packit 383869
#include "apr_strings.h"
Packit 383869
#include "apr_hash.h"
Packit 383869
#include "apr_file_io.h"
Packit 383869
#include "apr_env.h"
Packit 383869
#include "apr_atomic.h"
Packit 383869
Packit 383869
#include "apu_internal.h"
Packit 383869
#include "apu_version.h"
Packit 383869
Packit 383869
#if APU_DSO_BUILD
Packit 383869
Packit 383869
#if APR_HAS_THREADS
Packit 383869
static apr_thread_mutex_t* mutex = NULL;
Packit 383869
#endif
Packit 383869
static apr_hash_t *dsos = NULL;
Packit 383869
static apr_uint32_t initialised = 0, in_init = 1;
Packit 383869
Packit 383869
#if APR_HAS_THREADS
Packit 383869
apr_status_t apu_dso_mutex_lock()
Packit 383869
{
Packit 383869
    return apr_thread_mutex_lock(mutex);
Packit 383869
}
Packit 383869
apr_status_t apu_dso_mutex_unlock()
Packit 383869
{
Packit 383869
    return apr_thread_mutex_unlock(mutex);
Packit 383869
}
Packit 383869
#else
Packit 383869
apr_status_t apu_dso_mutex_lock() {
Packit 383869
    return APR_SUCCESS;
Packit 383869
}
Packit 383869
apr_status_t apu_dso_mutex_unlock() {
Packit 383869
    return APR_SUCCESS;
Packit 383869
}
Packit 383869
#endif
Packit 383869
Packit 383869
static apr_status_t apu_dso_term(void *ptr)
Packit 383869
{
Packit 383869
    /* set statics to NULL so init can work again */
Packit 383869
    dsos = NULL;
Packit 383869
#if APR_HAS_THREADS
Packit 383869
    mutex = NULL;
Packit 383869
#endif
Packit 383869
Packit 383869
    /* Everything else we need is handled by cleanups registered
Packit 383869
     * when we created mutexes and loaded DSOs
Packit 383869
     */
Packit 383869
    return APR_SUCCESS;
Packit 383869
}
Packit 383869
Packit 383869
apr_status_t apu_dso_init(apr_pool_t *pool)
Packit 383869
{
Packit 383869
    apr_status_t ret = APR_SUCCESS;
Packit 383869
    apr_pool_t *parent;
Packit 383869
Packit 383869
    if (apr_atomic_inc32(&initialised)) {
Packit 383869
        apr_atomic_set32(&initialised, 1); /* prevent wrap-around */
Packit 383869
Packit 383869
        while (apr_atomic_read32(&in_init)) /* wait until we get fully inited */
Packit 383869
            ;
Packit 383869
Packit 383869
        return APR_SUCCESS;
Packit 383869
    }
Packit 383869
Packit 383869
    /* Top level pool scope, need process-scope lifetime */
Packit 383869
    for (parent = apr_pool_parent_get(pool);
Packit 383869
         parent && parent != pool;
Packit 383869
         parent = apr_pool_parent_get(pool))
Packit 383869
        pool = parent;
Packit 383869
Packit 383869
    dsos = apr_hash_make(pool);
Packit 383869
Packit 383869
#if APR_HAS_THREADS
Packit 383869
    ret = apr_thread_mutex_create(&mutex, APR_THREAD_MUTEX_DEFAULT, pool);
Packit 383869
    /* This already registers a pool cleanup */
Packit 383869
#endif
Packit 383869
Packit 383869
    apr_pool_cleanup_register(pool, NULL, apu_dso_term,
Packit 383869
                              apr_pool_cleanup_null);
Packit 383869
Packit 383869
    apr_atomic_dec32(&in_init);
Packit 383869
Packit 383869
    return ret;
Packit 383869
}
Packit 383869
Packit 383869
apr_status_t apu_dso_load(apr_dso_handle_t **dlhandleptr,
Packit 383869
                          apr_dso_handle_sym_t *dsoptr,
Packit 383869
                          const char *module,
Packit 383869
                          const char *modsym,
Packit 383869
                          apr_pool_t *pool)
Packit 383869
{
Packit 383869
    apr_dso_handle_t *dlhandle = NULL;
Packit 383869
    char *pathlist;
Packit 383869
    char path[APR_PATH_MAX + 1];
Packit 383869
    apr_array_header_t *paths;
Packit 383869
    apr_pool_t *global;
Packit 383869
    apr_status_t rv = APR_EDSOOPEN;
Packit 383869
    char *eos = NULL;
Packit 383869
    int i;
Packit 383869
Packit 383869
    *dsoptr = apr_hash_get(dsos, module, APR_HASH_KEY_STRING);
Packit 383869
    if (*dsoptr) {
Packit 383869
        return APR_EINIT;
Packit 383869
    }
Packit 383869
Packit 383869
    /* The driver DSO must have exactly the same lifetime as the
Packit 383869
     * drivers hash table; ignore the passed-in pool */
Packit 383869
    global = apr_hash_pool_get(dsos);
Packit 383869
Packit 383869
    /* Retrieve our path search list or prepare for a single search */
Packit 383869
    if ((apr_env_get(&pathlist, APR_DSOPATH, pool) != APR_SUCCESS)
Packit 383869
          || (apr_filepath_list_split(&paths, pathlist, pool) != APR_SUCCESS))
Packit 383869
        paths = apr_array_make(pool, 1, sizeof(char*));
Packit 383869
Packit 383869
#if defined(APU_DSO_LIBDIR)
Packit 383869
    /* Always search our prefix path, but on some platforms such as
Packit 383869
     * win32 this may be left undefined
Packit 383869
     */
Packit 383869
    (*((char **)apr_array_push(paths))) = APU_DSO_LIBDIR;
Packit 383869
#endif
Packit 383869
Packit 383869
    for (i = 0; i < paths->nelts; ++i)
Packit 383869
    {
Packit 383869
#if defined(WIN32)
Packit 383869
        /* Use win32 dso search semantics and attempt to
Packit 383869
         * load the relative lib on the first pass.
Packit 383869
         */
Packit 383869
        if (!eos) {
Packit 383869
            eos = path;
Packit 383869
            --i;
Packit 383869
        }
Packit 383869
        else
Packit 383869
#endif
Packit 383869
        {
Packit 383869
            eos = apr_cpystrn(path, ((char**)paths->elts)[i], sizeof(path));
Packit 383869
            if ((eos > path) && (eos - path < sizeof(path) - 1))
Packit 383869
                *(eos++) = '/';
Packit 383869
        }
Packit 383869
        apr_cpystrn(eos, module, sizeof(path) - (eos - path));
Packit 383869
Packit 383869
        rv = apr_dso_load(&dlhandle, path, global);
Packit 383869
        if (dlhandleptr) {
Packit 383869
            *dlhandleptr = dlhandle;
Packit 383869
        }
Packit 383869
        if (rv == APR_SUCCESS) { /* APR_EDSOOPEN */
Packit 383869
            break;
Packit 383869
        }
Packit 383869
#if defined(APU_DSO_LIBDIR)
Packit 383869
        else if (i < paths->nelts - 1) {
Packit 383869
#else
Packit 383869
        else {   /* No APU_DSO_LIBDIR to skip */
Packit 383869
#endif
Packit 383869
             /* try with apr-util-APU_MAJOR_VERSION appended */
Packit 383869
            eos = apr_cpystrn(eos,
Packit 383869
                              "apr-util-" APU_STRINGIFY(APU_MAJOR_VERSION) "/",
Packit 383869
                              sizeof(path) - (eos - path));
Packit 383869
Packit 383869
            apr_cpystrn(eos, module, sizeof(path) - (eos - path));
Packit 383869
Packit 383869
            rv = apr_dso_load(&dlhandle, path, global);
Packit 383869
            if (dlhandleptr) {
Packit 383869
                *dlhandleptr = dlhandle;
Packit 383869
            }
Packit 383869
            if (rv == APR_SUCCESS) { /* APR_EDSOOPEN */
Packit 383869
                break;
Packit 383869
            }
Packit 383869
        }
Packit 383869
    }
Packit 383869
Packit 383869
    if (rv != APR_SUCCESS) /* APR_ESYMNOTFOUND */
Packit 383869
        return rv;
Packit 383869
Packit 383869
    rv = apr_dso_sym(dsoptr, dlhandle, modsym);
Packit 383869
    if (rv != APR_SUCCESS) { /* APR_ESYMNOTFOUND */
Packit 383869
        apr_dso_unload(dlhandle);
Packit 383869
    }
Packit 383869
    else {
Packit 383869
        module = apr_pstrdup(global, module);
Packit 383869
        apr_hash_set(dsos, module, APR_HASH_KEY_STRING, *dsoptr);
Packit 383869
    }
Packit 383869
    return rv;
Packit 383869
}
Packit 383869
Packit 383869
#endif /* APU_DSO_BUILD */
Packit 383869