dso.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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_portable.h"
  19. #if APR_HAS_DSO
  20. #if !defined(DSO_USE_DLFCN) && !defined(DSO_USE_SHL) && !defined(DSO_USE_DYLD)
  21. #error No DSO implementation specified.
  22. #endif
  23. #ifdef HAVE_STDDEF_H
  24. #include <stddef.h>
  25. #endif
  26. #if APR_HAVE_STDLIB_H
  27. #include <stdlib.h> /* malloc(), free() */
  28. #endif
  29. #if APR_HAVE_STRING_H
  30. #include <string.h> /* for strerror() on HP-UX */
  31. #endif
  32. #if defined(DSO_USE_DYLD)
  33. #define DYLD_LIBRARY_HANDLE (void *)-1
  34. #endif
  35. APR_DECLARE(apr_status_t) apr_os_dso_handle_put(apr_dso_handle_t **aprdso,
  36. apr_os_dso_handle_t osdso,
  37. apr_pool_t *pool)
  38. {
  39. *aprdso = apr_pcalloc(pool, sizeof **aprdso);
  40. (*aprdso)->handle = osdso;
  41. (*aprdso)->pool = pool;
  42. return APR_SUCCESS;
  43. }
  44. APR_DECLARE(apr_status_t) apr_os_dso_handle_get(apr_os_dso_handle_t *osdso,
  45. apr_dso_handle_t *aprdso)
  46. {
  47. *osdso = aprdso->handle;
  48. return APR_SUCCESS;
  49. }
  50. static apr_status_t dso_cleanup(void *thedso)
  51. {
  52. apr_dso_handle_t *dso = thedso;
  53. if (dso->handle == NULL)
  54. return APR_SUCCESS;
  55. #if defined(DSO_USE_SHL)
  56. shl_unload((shl_t)dso->handle);
  57. #elif defined(DSO_USE_DYLD)
  58. if (dso->handle != DYLD_LIBRARY_HANDLE) {
  59. NSUnLinkModule(dso->handle, FALSE);
  60. }
  61. #elif defined(DSO_USE_DLFCN)
  62. if (dlclose(dso->handle) != 0)
  63. return APR_EINIT;
  64. #endif
  65. dso->handle = NULL;
  66. return APR_SUCCESS;
  67. }
  68. APR_DECLARE(apr_status_t) apr_dso_load(apr_dso_handle_t **res_handle,
  69. const char *path, apr_pool_t *pool)
  70. {
  71. #if defined(DSO_USE_SHL)
  72. shl_t os_handle = shl_load(path, BIND_IMMEDIATE, 0L);
  73. #elif defined(DSO_USE_DYLD)
  74. NSObjectFileImage image;
  75. NSModule os_handle = NULL;
  76. NSObjectFileImageReturnCode dsoerr;
  77. const char* err_msg = NULL;
  78. dsoerr = NSCreateObjectFileImageFromFile(path, &image);
  79. if (dsoerr == NSObjectFileImageSuccess) {
  80. #if defined(NSLINKMODULE_OPTION_RETURN_ON_ERROR) && defined(NSLINKMODULE_OPTION_NONE)
  81. os_handle = NSLinkModule(image, path,
  82. NSLINKMODULE_OPTION_RETURN_ON_ERROR |
  83. NSLINKMODULE_OPTION_NONE);
  84. /* If something went wrong, get the errors... */
  85. if (!os_handle) {
  86. NSLinkEditErrors errors;
  87. int errorNumber;
  88. const char *fileName;
  89. NSLinkEditError(&errors, &errorNumber, &fileName, &err_msg);
  90. }
  91. #else
  92. os_handle = NSLinkModule(image, path, FALSE);
  93. #endif
  94. NSDestroyObjectFileImage(image);
  95. }
  96. else if ((dsoerr == NSObjectFileImageFormat ||
  97. dsoerr == NSObjectFileImageInappropriateFile) &&
  98. NSAddLibrary(path) == TRUE) {
  99. os_handle = (NSModule)DYLD_LIBRARY_HANDLE;
  100. }
  101. else {
  102. err_msg = "cannot create object file image or add library";
  103. }
  104. #elif defined(DSO_USE_DLFCN)
  105. #if defined(OSF1) || defined(SEQUENT) || defined(SNI) ||\
  106. (defined(__FreeBSD_version) && (__FreeBSD_version >= 220000)) ||\
  107. defined(__DragonFly__)
  108. void *os_handle = dlopen((char *)path, RTLD_NOW | RTLD_LOCAL);
  109. #else
  110. int flags = RTLD_NOW | RTLD_LOCAL;
  111. void *os_handle;
  112. #ifdef _AIX
  113. if (strchr(path + 1, '(') && path[strlen(path) - 1] == ')')
  114. {
  115. /* This special archive.a(dso.so) syntax is required for
  116. * the way libtool likes to build shared libraries on AIX.
  117. * dlopen() support for such a library requires that the
  118. * RTLD_MEMBER flag be enabled.
  119. */
  120. flags |= RTLD_MEMBER;
  121. }
  122. #endif
  123. os_handle = dlopen(path, flags);
  124. #endif
  125. #endif /* DSO_USE_x */
  126. *res_handle = apr_pcalloc(pool, sizeof(**res_handle));
  127. if(os_handle == NULL) {
  128. #if defined(DSO_USE_SHL)
  129. (*res_handle)->errormsg = strerror(errno);
  130. return APR_EDSOOPEN;
  131. #elif defined(DSO_USE_DYLD)
  132. (*res_handle)->errormsg = (err_msg) ? err_msg : "link failed";
  133. return APR_EDSOOPEN;
  134. #elif defined(DSO_USE_DLFCN)
  135. (*res_handle)->errormsg = dlerror();
  136. return APR_EDSOOPEN;
  137. #endif
  138. }
  139. (*res_handle)->handle = (void*)os_handle;
  140. (*res_handle)->pool = pool;
  141. (*res_handle)->errormsg = NULL;
  142. apr_pool_cleanup_register(pool, *res_handle, dso_cleanup, apr_pool_cleanup_null);
  143. return APR_SUCCESS;
  144. }
  145. APR_DECLARE(apr_status_t) apr_dso_unload(apr_dso_handle_t *handle)
  146. {
  147. return apr_pool_cleanup_run(handle->pool, handle, dso_cleanup);
  148. }
  149. APR_DECLARE(apr_status_t) apr_dso_sym(apr_dso_handle_sym_t *ressym,
  150. apr_dso_handle_t *handle,
  151. const char *symname)
  152. {
  153. #if defined(DSO_USE_SHL)
  154. void *symaddr = NULL;
  155. int status;
  156. errno = 0;
  157. status = shl_findsym((shl_t *)&handle->handle, symname, TYPE_PROCEDURE, &symaddr);
  158. if (status == -1 && errno == 0) /* try TYPE_DATA instead */
  159. status = shl_findsym((shl_t *)&handle->handle, symname, TYPE_DATA, &symaddr);
  160. if (status == -1)
  161. return APR_ESYMNOTFOUND;
  162. *ressym = symaddr;
  163. return APR_SUCCESS;
  164. #elif defined(DSO_USE_DYLD)
  165. void *retval = NULL;
  166. NSSymbol symbol;
  167. char *symname2 = (char*)malloc(sizeof(char)*(strlen(symname)+2));
  168. sprintf(symname2, "_%s", symname);
  169. #ifdef NSLINKMODULE_OPTION_PRIVATE
  170. if (handle->handle == DYLD_LIBRARY_HANDLE) {
  171. symbol = NSLookupAndBindSymbol(symname2);
  172. }
  173. else {
  174. symbol = NSLookupSymbolInModule((NSModule)handle->handle, symname2);
  175. }
  176. #else
  177. symbol = NSLookupAndBindSymbol(symname2);
  178. #endif
  179. free(symname2);
  180. if (symbol == NULL) {
  181. handle->errormsg = "undefined symbol";
  182. return APR_ESYMNOTFOUND;
  183. }
  184. retval = NSAddressOfSymbol(symbol);
  185. if (retval == NULL) {
  186. handle->errormsg = "cannot resolve symbol";
  187. return APR_ESYMNOTFOUND;
  188. }
  189. *ressym = retval;
  190. return APR_SUCCESS;
  191. #elif defined(DSO_USE_DLFCN)
  192. #if defined(DLSYM_NEEDS_UNDERSCORE)
  193. void *retval;
  194. char *symbol = (char*)malloc(sizeof(char)*(strlen(symname)+2));
  195. sprintf(symbol, "_%s", symname);
  196. retval = dlsym(handle->handle, symbol);
  197. free(symbol);
  198. #elif defined(SEQUENT) || defined(SNI)
  199. void *retval = dlsym(handle->handle, (char *)symname);
  200. #else
  201. void *retval = dlsym(handle->handle, symname);
  202. #endif /* DLSYM_NEEDS_UNDERSCORE */
  203. if (retval == NULL) {
  204. handle->errormsg = dlerror();
  205. return APR_ESYMNOTFOUND;
  206. }
  207. *ressym = retval;
  208. return APR_SUCCESS;
  209. #endif /* DSO_USE_x */
  210. }
  211. APR_DECLARE(const char *) apr_dso_error(apr_dso_handle_t *dso, char *buffer,
  212. apr_size_t buflen)
  213. {
  214. if (dso->errormsg) {
  215. apr_cpystrn(buffer, dso->errormsg, buflen);
  216. return dso->errormsg;
  217. }
  218. return "No Error";
  219. }
  220. #endif