dso.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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 "fspr_arch_dso.h"
  17. #include "fspr_strings.h"
  18. #include "fspr_portable.h"
  19. #include <library.h>
  20. #include <unistd.h>
  21. APR_DECLARE(fspr_status_t) fspr_os_dso_handle_put(fspr_dso_handle_t **aprdso,
  22. fspr_os_dso_handle_t osdso,
  23. fspr_pool_t *pool)
  24. {
  25. *aprdso = fspr_pcalloc(pool, sizeof **aprdso);
  26. (*aprdso)->handle = osdso;
  27. (*aprdso)->pool = pool;
  28. return APR_SUCCESS;
  29. }
  30. APR_DECLARE(fspr_status_t) fspr_os_dso_handle_get(fspr_os_dso_handle_t *osdso,
  31. fspr_dso_handle_t *aprdso)
  32. {
  33. *osdso = aprdso->handle;
  34. return APR_SUCCESS;
  35. }
  36. static fspr_status_t dso_cleanup(void *thedso)
  37. {
  38. fspr_dso_handle_t *dso = thedso;
  39. sym_list *symbol = NULL;
  40. void *NLMHandle = getnlmhandle();
  41. if (dso->handle == NULL)
  42. return APR_SUCCESS;
  43. if (dso->symbols != NULL) {
  44. symbol = dso->symbols;
  45. while (symbol) {
  46. UnImportPublicObject(NLMHandle, symbol->symbol);
  47. symbol = symbol->next;
  48. }
  49. }
  50. if (dlclose(dso->handle) != 0)
  51. return APR_EINIT;
  52. dso->handle = NULL;
  53. dso->symbols = NULL;
  54. dso->path = NULL;
  55. return APR_SUCCESS;
  56. }
  57. APR_DECLARE(fspr_status_t) fspr_dso_load(fspr_dso_handle_t **res_handle,
  58. const char *path, fspr_pool_t *pool)
  59. {
  60. void *os_handle = NULL;
  61. char *fullpath = NULL;
  62. fspr_status_t rv;
  63. if ((rv = fspr_filepath_merge(&fullpath, NULL, path,
  64. APR_FILEPATH_NATIVE, pool)) != APR_SUCCESS) {
  65. return rv;
  66. }
  67. os_handle = dlopen(fullpath, RTLD_NOW | RTLD_LOCAL);
  68. *res_handle = fspr_pcalloc(pool, sizeof(**res_handle));
  69. if(os_handle == NULL) {
  70. (*res_handle)->errormsg = dlerror();
  71. return APR_EDSOOPEN;
  72. }
  73. (*res_handle)->handle = (void*)os_handle;
  74. (*res_handle)->pool = pool;
  75. (*res_handle)->errormsg = NULL;
  76. (*res_handle)->symbols = NULL;
  77. (*res_handle)->path = fspr_pstrdup(pool, fullpath);
  78. fspr_pool_cleanup_register(pool, *res_handle, dso_cleanup, fspr_pool_cleanup_null);
  79. return APR_SUCCESS;
  80. }
  81. APR_DECLARE(fspr_status_t) fspr_dso_unload(fspr_dso_handle_t *handle)
  82. {
  83. return fspr_pool_cleanup_run(handle->pool, handle, dso_cleanup);
  84. }
  85. APR_DECLARE(fspr_status_t) fspr_dso_sym(fspr_dso_handle_sym_t *ressym,
  86. fspr_dso_handle_t *handle,
  87. const char *symname)
  88. {
  89. sym_list *symbol = NULL;
  90. void *retval = dlsym(handle->handle, symname);
  91. if (retval == NULL) {
  92. handle->errormsg = dlerror();
  93. return APR_ESYMNOTFOUND;
  94. }
  95. symbol = fspr_pcalloc(handle->pool, sizeof(sym_list));
  96. symbol->next = handle->symbols;
  97. handle->symbols = symbol;
  98. symbol->symbol = fspr_pstrdup(handle->pool, symname);
  99. *ressym = retval;
  100. return APR_SUCCESS;
  101. }
  102. APR_DECLARE(const char *) fspr_dso_error(fspr_dso_handle_t *dso, char *buffer,
  103. fspr_size_t buflen)
  104. {
  105. if (dso->errormsg) {
  106. fspr_cpystrn(buffer, dso->errormsg, buflen);
  107. return dso->errormsg;
  108. }
  109. return "No Error";
  110. }