lt_dlloader.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /* lt_dlloader.c -- dynamic library loader interface
  2. Copyright (C) 2004, 2007-2008, 2011-2015 Free Software Foundation,
  3. Inc.
  4. Written by Gary V. Vaughan, 2004
  5. NOTE: The canonical source of this file is maintained with the
  6. GNU Libtool package. Report bugs to bug-libtool@gnu.org.
  7. GNU Libltdl is free software; you can redistribute it and/or
  8. modify it under the terms of the GNU Lesser General Public
  9. License as published by the Free Software Foundation; either
  10. version 2 of the License, or (at your option) any later version.
  11. As a special exception to the GNU Lesser General Public License,
  12. if you distribute this file as part of a program or library that
  13. is built using GNU Libtool, you may include this file under the
  14. same distribution terms that you use for the rest of that program.
  15. GNU Libltdl is distributed in the hope that it will be useful,
  16. but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. GNU Lesser General Public License for more details.
  19. You should have received a copy of the GNU Lesser General Public
  20. License along with GNU Libltdl; see the file COPYING.LIB. If not, a
  21. copy can be downloaded from http://www.gnu.org/licenses/lgpl.html,
  22. or obtained by writing to the Free Software Foundation, Inc.,
  23. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  24. */
  25. #include "lt__private.h"
  26. #include "lt_dlloader.h"
  27. #define RETURN_SUCCESS 0
  28. #define RETURN_FAILURE 1
  29. static void * loader_callback (SList *item, void *userdata);
  30. /* A list of all the dlloaders we know about, each stored as a boxed
  31. SList item: */
  32. static SList *loaders = 0;
  33. /* Return NULL, unless the loader in this ITEM has a matching name,
  34. in which case we return the matching item so that its address is
  35. passed back out (for possible freeing) by slist_remove. */
  36. static void *
  37. loader_callback (SList *item, void *userdata)
  38. {
  39. const lt_dlvtable *vtable = (const lt_dlvtable *) item->userdata;
  40. const char * name = (const char *) userdata;
  41. assert (vtable);
  42. return STREQ (vtable->name, name) ? (void *) item : NULL;
  43. }
  44. /* Hook VTABLE into our global LOADERS list according to its own
  45. PRIORITY field value. */
  46. int
  47. lt_dlloader_add (const lt_dlvtable *vtable)
  48. {
  49. SList *item;
  50. if ((vtable == 0) /* diagnose invalid vtable fields */
  51. || (vtable->module_open == 0)
  52. || (vtable->module_close == 0)
  53. || (vtable->find_sym == 0)
  54. || ((vtable->priority != LT_DLLOADER_PREPEND) &&
  55. (vtable->priority != LT_DLLOADER_APPEND)))
  56. {
  57. LT__SETERROR (INVALID_LOADER);
  58. return RETURN_FAILURE;
  59. }
  60. item = slist_box (vtable);
  61. if (!item)
  62. {
  63. (*lt__alloc_die) ();
  64. /* Let the caller know something went wrong if lt__alloc_die
  65. doesn't abort. */
  66. return RETURN_FAILURE;
  67. }
  68. if (vtable->priority == LT_DLLOADER_PREPEND)
  69. {
  70. loaders = slist_cons (item, loaders);
  71. }
  72. else
  73. {
  74. assert (vtable->priority == LT_DLLOADER_APPEND);
  75. loaders = slist_concat (loaders, item);
  76. }
  77. return RETURN_SUCCESS;
  78. }
  79. #ifdef LT_DEBUG_LOADERS
  80. static void *
  81. loader_dump_callback (SList *item, void *userdata)
  82. {
  83. const lt_dlvtable *vtable = (const lt_dlvtable *) item->userdata;
  84. fprintf (stderr, ", %s", (vtable && vtable->name) ? vtable->name : "(null)");
  85. return 0;
  86. }
  87. void
  88. lt_dlloader_dump (void)
  89. {
  90. fprintf (stderr, "loaders: ");
  91. if (!loaders)
  92. {
  93. fprintf (stderr, "(empty)");
  94. }
  95. else
  96. {
  97. const lt_dlvtable *head = (const lt_dlvtable *) loaders->userdata;
  98. fprintf (stderr, "%s", (head && head->name) ? head->name : "(null)");
  99. if (slist_tail (loaders))
  100. slist_foreach (slist_tail (loaders), loader_dump_callback, NULL);
  101. }
  102. fprintf (stderr, "\n");
  103. }
  104. #endif
  105. /* An iterator for the global loader list: if LOADER is NULL, then
  106. return the first element, otherwise the following element. */
  107. lt_dlloader
  108. lt_dlloader_next (lt_dlloader loader)
  109. {
  110. SList *item = (SList *) loader;
  111. return (lt_dlloader) (item ? item->next : loaders);
  112. }
  113. /* Non-destructive unboxing of a loader. */
  114. const lt_dlvtable *
  115. lt_dlloader_get (lt_dlloader loader)
  116. {
  117. return (const lt_dlvtable *) (loader ? ((SList *) loader)->userdata : NULL);
  118. }
  119. /* Return the contents of the first item in the global loader list
  120. with a matching NAME after removing it from that list. If there
  121. was no match, return NULL; if there is an error, return NULL and
  122. set an error for lt_dlerror; do not set an error if only resident
  123. modules need this loader; in either case, the loader list is not
  124. changed if NULL is returned. */
  125. lt_dlvtable *
  126. lt_dlloader_remove (const char *name)
  127. {
  128. const lt_dlvtable * vtable = lt_dlloader_find (name);
  129. static const char id_string[] = "lt_dlloader_remove";
  130. lt_dlinterface_id iface;
  131. lt_dlhandle handle = 0;
  132. int in_use = 0;
  133. int in_use_by_resident = 0;
  134. if (!vtable)
  135. {
  136. LT__SETERROR (INVALID_LOADER);
  137. return 0;
  138. }
  139. /* Fail if there are any open modules that use this loader. */
  140. iface = lt_dlinterface_register (id_string, NULL);
  141. while ((handle = lt_dlhandle_iterate (iface, handle)))
  142. {
  143. lt_dlhandle cur = handle;
  144. if (cur->vtable == vtable)
  145. {
  146. in_use = 1;
  147. if (lt_dlisresident (handle))
  148. in_use_by_resident = 1;
  149. }
  150. }
  151. lt_dlinterface_free (iface);
  152. if (in_use)
  153. {
  154. if (!in_use_by_resident)
  155. LT__SETERROR (REMOVE_LOADER);
  156. return 0;
  157. }
  158. /* Call the loader finalisation function. */
  159. if (vtable && vtable->dlloader_exit)
  160. {
  161. if ((*vtable->dlloader_exit) (vtable->dlloader_data) != 0)
  162. {
  163. /* If there is an exit function, and it returns non-zero
  164. then it must set an error, and we will not remove it
  165. from the list. */
  166. return 0;
  167. }
  168. }
  169. /* If we got this far, remove the loader from our global list. */
  170. return (lt_dlvtable *)
  171. slist_unbox ((SList *) slist_remove (&loaders, loader_callback, (void *) name));
  172. }
  173. const lt_dlvtable *
  174. lt_dlloader_find (const char *name)
  175. {
  176. return lt_dlloader_get (slist_find (loaders, loader_callback, (void *) name));
  177. }