lcr_ifact.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. /*
  2. * Copyright (C) 2006 Steven Dake (sdake@mvista.com)
  3. *
  4. * This software licensed under BSD license, the text of which follows:
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are met:
  8. *
  9. * - Redistributions of source code must retain the above copyright notice,
  10. * this list of conditions and the following disclaimer.
  11. * - Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. * - Neither the name of the MontaVista Software, Inc. nor the names of its
  15. * contributors may be used to endorse or promote products derived from this
  16. * software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  19. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  20. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  21. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  22. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  23. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  24. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  25. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  26. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  27. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  28. * THE POSSIBILITY OF SUCH DAMAGE.
  29. */
  30. #include <stdio.h>
  31. #include <dlfcn.h>
  32. #include <dirent.h>
  33. #include <errno.h>
  34. #include <unistd.h>
  35. #include "lcr_comp.h"
  36. #include "lcr_ifact.h"
  37. #include "../include/hdb.h"
  38. struct lcr_component_instance {
  39. struct lcr_iface *ifaces;
  40. int iface_count;
  41. void *dl_handle;
  42. int refcount;
  43. char library_name[256];
  44. };
  45. struct lcr_iface_instance {
  46. unsigned int component_handle;
  47. void *context;
  48. void (*destructor) (void *context);
  49. };
  50. static struct hdb_handle_database lcr_component_instance_database = {
  51. .handle_count = 0,
  52. .handles = 0,
  53. .iterator = 0
  54. };
  55. static struct hdb_handle_database lcr_iface_instance_database = {
  56. .handle_count = 0,
  57. .handles = 0,
  58. .iterator = 0
  59. };
  60. static unsigned int g_component_handle;
  61. static int lcr_select_so (const struct dirent *dirent)
  62. {
  63. unsigned int len;
  64. len = strlen (dirent->d_name);
  65. if (len > 6) {
  66. if (strcmp (".lcrso", dirent->d_name + len - 6) == 0) {
  67. return (1);
  68. }
  69. }
  70. return (0);
  71. }
  72. static inline struct lcr_component_instance *lcr_comp_find (
  73. char *iface_name,
  74. unsigned int version,
  75. int *iface_number)
  76. {
  77. struct lcr_component_instance *instance;
  78. unsigned int component_handle;
  79. int i;
  80. /*
  81. * Try to find interface in already loaded component
  82. */
  83. hdb_iterator_reset (&lcr_component_instance_database);
  84. while (hdb_iterator_next (&lcr_component_instance_database,
  85. (void **)&instance, &component_handle) == 0) {
  86. for (i = 0; i < instance->iface_count; i++) {
  87. if ((strcmp (instance->ifaces[i].name, iface_name) == 0) &&
  88. instance->ifaces[i].version == version) {
  89. *iface_number = i;
  90. return (instance);
  91. }
  92. }
  93. hdb_handle_put (&lcr_component_instance_database, component_handle);
  94. }
  95. return (NULL);
  96. }
  97. static inline int lcr_lib_loaded (
  98. char *library_name)
  99. {
  100. struct lcr_component_instance *instance;
  101. unsigned int component_handle;
  102. /*
  103. * Try to find interface in already loaded component
  104. */
  105. hdb_iterator_reset (&lcr_component_instance_database);
  106. while (hdb_iterator_next (&lcr_component_instance_database,
  107. (void **)&instance, &component_handle) == 0) {
  108. if (strcmp (instance->library_name, library_name) == 0) {
  109. return (1);
  110. }
  111. hdb_handle_put (&lcr_component_instance_database, component_handle);
  112. }
  113. return (0);
  114. }
  115. int lcr_ifact_reference (
  116. unsigned int *iface_handle,
  117. char *iface_name,
  118. int version,
  119. void **iface,
  120. void *context)
  121. {
  122. void *dl_handle;
  123. struct lcr_iface_instance *iface_instance;
  124. struct lcr_component_instance *instance;
  125. int iface_number;
  126. struct dirent **scandir_list;
  127. int scandir_entries;
  128. unsigned int libs_to_scan;
  129. char cwd[512];
  130. char dl_name[1024];
  131. getcwd (cwd, sizeof (cwd));
  132. strcat (cwd, "/");
  133. /*
  134. * Determine if the component is already loaded
  135. */
  136. instance = lcr_comp_find (iface_name, version, &iface_number);
  137. if (instance) {
  138. goto found;
  139. }
  140. // TODO error checking in this code is weak
  141. /*
  142. * Find all *.lcrso files in the cwd
  143. */
  144. scandir_entries = scandir(".", &scandir_list, lcr_select_so, alphasort);
  145. if (scandir_entries < 0)
  146. printf ("scandir error reason=%s\n", strerror (errno));
  147. else
  148. /*
  149. * no error so load the object
  150. */
  151. for (libs_to_scan = 0; libs_to_scan < scandir_entries; libs_to_scan++) {
  152. /*
  153. * Load objects, scan them, unload them if they are not a match
  154. */
  155. sprintf (dl_name, "%s%s", cwd, scandir_list[libs_to_scan]->d_name);
  156. /*
  157. * Don't reload already loaded libraries
  158. */
  159. if (lcr_lib_loaded (dl_name)) {
  160. continue;
  161. }
  162. dl_handle = dlopen (dl_name, RTLD_NOW);
  163. if (dl_handle == NULL) {
  164. printf ("Error loading interface %s reason=%s\n", dl_name, dlerror());
  165. return (-1);
  166. }
  167. instance = lcr_comp_find (iface_name, version, &iface_number);
  168. if (instance) {
  169. instance->dl_handle = dl_handle;
  170. strcpy (instance->library_name, dl_name);
  171. goto found;
  172. }
  173. /*
  174. * No matching interfaces found, try next shared object
  175. */
  176. if (g_component_handle != 0xFFFFFFFF) {
  177. hdb_handle_destroy (&lcr_component_instance_database,
  178. g_component_handle);
  179. g_component_handle = 0xFFFFFFFF;
  180. }
  181. dlclose (dl_handle);
  182. } /* scanning for lcrso loop */
  183. /*
  184. * No matching interfaces found in all shared objects
  185. */
  186. return (-1);
  187. found:
  188. *iface = instance->ifaces[iface_number].interfaces;
  189. if (instance->ifaces[iface_number].constructor) {
  190. instance->ifaces[iface_number].constructor (context);
  191. }
  192. hdb_handle_create (&lcr_iface_instance_database,
  193. sizeof (struct lcr_iface_instance),
  194. iface_handle);
  195. hdb_handle_get (&lcr_iface_instance_database,
  196. *iface_handle, (void *)&iface_instance);
  197. iface_instance->component_handle = g_component_handle;
  198. iface_instance->context = context;
  199. iface_instance->destructor = instance->ifaces[iface_number].destructor;
  200. return (0);
  201. }
  202. int lcr_ifact_release (unsigned int handle)
  203. {
  204. struct lcr_iface_instance *iface_instance;
  205. int res = 0;
  206. res = hdb_handle_get (&lcr_iface_instance_database,
  207. handle, (void *)&iface_instance);
  208. return (res);
  209. if (iface_instance->destructor) {
  210. iface_instance->destructor (iface_instance->context);
  211. }
  212. hdb_handle_put (&lcr_component_instance_database,
  213. iface_instance->component_handle);
  214. hdb_handle_put (&lcr_iface_instance_database, handle);
  215. hdb_handle_destroy (&lcr_iface_instance_database, handle);
  216. return (res);
  217. }
  218. void lcr_component_register (struct lcr_comp *comp)
  219. {
  220. struct lcr_component_instance *instance;
  221. hdb_handle_create (&lcr_component_instance_database,
  222. sizeof (struct lcr_component_instance),
  223. &g_component_handle);
  224. hdb_handle_get (&lcr_component_instance_database,
  225. g_component_handle, (void *)&instance);
  226. instance->ifaces = comp->ifaces;
  227. instance->iface_count = comp->iface_count;
  228. instance->dl_handle = NULL;
  229. hdb_handle_put (&lcr_component_instance_database,
  230. g_component_handle);
  231. }