dso.c 3.9 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 "apr_arch_dso.h"
  17. #include "apr_strings.h"
  18. #include "apr_portable.h"
  19. #include <library.h>
  20. #include <unistd.h>
  21. APR_DECLARE(apr_status_t) apr_os_dso_handle_put(apr_dso_handle_t **aprdso,
  22. apr_os_dso_handle_t osdso,
  23. apr_pool_t *pool)
  24. {
  25. *aprdso = apr_pcalloc(pool, sizeof **aprdso);
  26. (*aprdso)->handle = osdso;
  27. (*aprdso)->pool = pool;
  28. return APR_SUCCESS;
  29. }
  30. APR_DECLARE(apr_status_t) apr_os_dso_handle_get(apr_os_dso_handle_t *osdso,
  31. apr_dso_handle_t *aprdso)
  32. {
  33. *osdso = aprdso->handle;
  34. return APR_SUCCESS;
  35. }
  36. static apr_status_t dso_cleanup(void *thedso)
  37. {
  38. apr_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(apr_status_t) apr_dso_load(apr_dso_handle_t **res_handle,
  58. const char *path, apr_pool_t *pool)
  59. {
  60. void *os_handle = NULL;
  61. char *fullpath = NULL;
  62. apr_status_t rv;
  63. if ((rv = apr_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 = apr_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 = apr_pstrdup(pool, fullpath);
  78. apr_pool_cleanup_register(pool, *res_handle, dso_cleanup, apr_pool_cleanup_null);
  79. return APR_SUCCESS;
  80. }
  81. APR_DECLARE(apr_status_t) apr_dso_unload(apr_dso_handle_t *handle)
  82. {
  83. return apr_pool_cleanup_run(handle->pool, handle, dso_cleanup);
  84. }
  85. APR_DECLARE(apr_status_t) apr_dso_sym(apr_dso_handle_sym_t *ressym,
  86. apr_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 = apr_pcalloc(handle->pool, sizeof(sym_list));
  96. symbol->next = handle->symbols;
  97. handle->symbols = symbol;
  98. symbol->symbol = apr_pstrdup(handle->pool, symname);
  99. *ressym = retval;
  100. return APR_SUCCESS;
  101. }
  102. APR_DECLARE(const char *) apr_dso_error(apr_dso_handle_t *dso, char *buffer,
  103. apr_size_t buflen)
  104. {
  105. if (dso->errormsg) {
  106. apr_cpystrn(buffer, dso->errormsg, buflen);
  107. return dso->errormsg;
  108. }
  109. return "No Error";
  110. }