dso.c 5.2 KB

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