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 "apr_portable.h"
18#include "apr_strings.h"
19#include "apr_arch_dso.h"
20#include <errno.h>
21#include <string.h>
22
23#if APR_HAS_DSO
24
25APR_DECLARE(apr_status_t) apr_os_dso_handle_put(apr_dso_handle_t **aprdso,
26                                                apr_os_dso_handle_t osdso,
27                                                apr_pool_t *pool)
28{
29    *aprdso = apr_pcalloc(pool, sizeof **aprdso);
30    (*aprdso)->handle = osdso;
31    (*aprdso)->pool = pool;
32    return APR_SUCCESS;
33}
34
35APR_DECLARE(apr_status_t) apr_os_dso_handle_get(apr_os_dso_handle_t *osdso,
36                                                apr_dso_handle_t *aprdso)
37{
38    *osdso = aprdso->handle;
39    return APR_SUCCESS;
40}
41
42static apr_status_t dso_cleanup(void *thedso)
43{
44    apr_dso_handle_t *dso = thedso;
45    int rc;
46
47    if (dso->handle == 0)
48        return APR_SUCCESS;
49
50    rc = dllfree(dso->handle);
51
52    if (rc == 0) {
53        dso->handle = 0;
54        return APR_SUCCESS;
55    }
56    dso->failing_errno = errno;
57    return errno;
58}
59
60APR_DECLARE(apr_status_t) apr_dso_load(apr_dso_handle_t **res_handle,
61                                       const char *path, apr_pool_t *ctx)
62{
63    dllhandle *handle;
64    int rc;
65
66    *res_handle = apr_pcalloc(ctx, sizeof(**res_handle));
67    (*res_handle)->pool = ctx;
68    if ((handle = dllload(path)) != NULL) {
69        (*res_handle)->handle  = handle;
70        apr_pool_cleanup_register(ctx, *res_handle, dso_cleanup, apr_pool_cleanup_null);
71        return APR_SUCCESS;
72    }
73
74    (*res_handle)->failing_errno = errno;
75    return APR_EDSOOPEN;
76}
77
78APR_DECLARE(apr_status_t) apr_dso_unload(apr_dso_handle_t *handle)
79{
80    return apr_pool_cleanup_run(handle->pool, handle, dso_cleanup);
81}
82
83APR_DECLARE(apr_status_t) apr_dso_sym(apr_dso_handle_sym_t *ressym,
84                                      apr_dso_handle_t *handle,
85                                      const char *symname)
86{
87    void *func_ptr;
88    void *var_ptr;
89
90    if ((var_ptr = dllqueryvar(handle->handle, symname)) != NULL) {
91        *ressym = var_ptr;
92        return APR_SUCCESS;
93    }
94    if ((func_ptr = (void *)dllqueryfn(handle->handle, symname)) != NULL) {
95        *ressym = func_ptr;
96        return APR_SUCCESS;
97    }
98    handle->failing_errno = errno;
99    return APR_ESYMNOTFOUND;
100}
101
102APR_DECLARE(const char *) apr_dso_error(apr_dso_handle_t *handle, char *buffer,
103                          apr_size_t buflen)
104{
105    apr_cpystrn(buffer, strerror(handle->failing_errno), buflen);
106    return buffer;
107}
108
109#endif
110