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_arch_dso.h"
18#include "apr_strings.h"
19#include "apr_private.h"
20#include "apr_arch_file_io.h"
21#include "apr_arch_utf8.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)->cont = 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
46    if (dso->handle != NULL && !FreeLibrary(dso->handle)) {
47        return apr_get_os_error();
48    }
49    dso->handle = NULL;
50
51    return APR_SUCCESS;
52}
53
54APR_DECLARE(apr_status_t) apr_dso_load(struct apr_dso_handle_t **res_handle,
55                                       const char *path, apr_pool_t *ctx)
56{
57    HINSTANCE os_handle;
58    apr_status_t rv;
59#ifndef _WIN32_WCE
60    UINT em;
61#endif
62
63#if APR_HAS_UNICODE_FS
64    IF_WIN_OS_IS_UNICODE
65    {
66        apr_wchar_t wpath[APR_PATH_MAX];
67        if ((rv = utf8_to_unicode_path(wpath, sizeof(wpath)
68                                            / sizeof(apr_wchar_t), path))
69                != APR_SUCCESS) {
70            *res_handle = apr_pcalloc(ctx, sizeof(**res_handle));
71            return ((*res_handle)->load_error = rv);
72        }
73        /* Prevent ugly popups from killing our app */
74#ifndef _WIN32_WCE
75        em = SetErrorMode(SEM_FAILCRITICALERRORS);
76#endif
77        os_handle = LoadLibraryExW(wpath, NULL, 0);
78        if (!os_handle)
79            os_handle = LoadLibraryExW(wpath, NULL, LOAD_WITH_ALTERED_SEARCH_PATH);
80        if (!os_handle)
81            rv = apr_get_os_error();
82#ifndef _WIN32_WCE
83        SetErrorMode(em);
84#endif
85    }
86#endif /* APR_HAS_UNICODE_FS */
87#if APR_HAS_ANSI_FS
88    ELSE_WIN_OS_IS_ANSI
89    {
90        char fspec[APR_PATH_MAX], *p = fspec;
91        /* Must convert path from / to \ notation.
92         * Per PR2555, the LoadLibraryEx function is very picky about slashes.
93         * Debugging on NT 4 SP 6a reveals First Chance Exception within NTDLL.
94         * LoadLibrary in the MS PSDK also reveals that it -explicitly- states
95         * that backslashes must be used for the LoadLibrary family of calls.
96         */
97        apr_cpystrn(fspec, path, sizeof(fspec));
98        while ((p = strchr(p, '/')) != NULL)
99            *p = '\\';
100
101        /* Prevent ugly popups from killing our app */
102        em = SetErrorMode(SEM_FAILCRITICALERRORS);
103        os_handle = LoadLibraryEx(path, NULL, LOAD_WITH_ALTERED_SEARCH_PATH);
104        if (!os_handle)
105            os_handle = LoadLibraryEx(path, NULL, 0);
106        if (!os_handle)
107            rv = apr_get_os_error();
108        else
109            rv = APR_SUCCESS;
110        SetErrorMode(em);
111    }
112#endif
113
114    *res_handle = apr_pcalloc(ctx, sizeof(**res_handle));
115    (*res_handle)->cont = ctx;
116
117    if (rv) {
118        return ((*res_handle)->load_error = rv);
119    }
120
121    (*res_handle)->handle = (void*)os_handle;
122    (*res_handle)->load_error = APR_SUCCESS;
123
124    apr_pool_cleanup_register(ctx, *res_handle, dso_cleanup, apr_pool_cleanup_null);
125
126    return APR_SUCCESS;
127}
128
129APR_DECLARE(apr_status_t) apr_dso_unload(struct apr_dso_handle_t *handle)
130{
131    return apr_pool_cleanup_run(handle->cont, handle, dso_cleanup);
132}
133
134APR_DECLARE(apr_status_t) apr_dso_sym(apr_dso_handle_sym_t *ressym,
135                         struct apr_dso_handle_t *handle,
136                         const char *symname)
137{
138#ifdef _WIN32_WCE
139    apr_size_t symlen = strlen(symname) + 1;
140    apr_size_t wsymlen = 256;
141    apr_wchar_t wsymname[256];
142    apr_status_t rv;
143
144    rv = apr_conv_utf8_to_ucs2(wsymname, &wsymlen, symname, &symlen);
145    if (rv != APR_SUCCESS) {
146        return rv;
147    }
148    else if (symlen) {
149        return APR_ENAMETOOLONG;
150    }
151
152    *ressym = (apr_dso_handle_sym_t)GetProcAddressW(handle->handle, wsymname);
153#else
154    *ressym = (apr_dso_handle_sym_t)GetProcAddress(handle->handle, symname);
155#endif
156    if (!*ressym) {
157        return apr_get_os_error();
158    }
159    return APR_SUCCESS;
160}
161
162APR_DECLARE(const char *) apr_dso_error(apr_dso_handle_t *dso, char *buf, apr_size_t bufsize)
163{
164    return apr_strerror(dso->load_error, buf, bufsize);
165}
166
167#endif
168