lcr_ifact.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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. #ifdef OPENAIS_LINUX
  62. static int lcr_select_so (const struct dirent *dirent)
  63. #else
  64. static int lcr_select_so (struct dirent *dirent)
  65. #endif
  66. {
  67. unsigned int len;
  68. len = strlen (dirent->d_name);
  69. if (len > 6) {
  70. if (strcmp (".lcrso", dirent->d_name + len - 6) == 0) {
  71. return (1);
  72. }
  73. }
  74. return (0);
  75. }
  76. static inline struct lcr_component_instance *lcr_comp_find (
  77. char *iface_name,
  78. unsigned int version,
  79. int *iface_number)
  80. {
  81. struct lcr_component_instance *instance;
  82. unsigned int component_handle = 0;
  83. int i;
  84. /*
  85. * Try to find interface in already loaded component
  86. */
  87. hdb_iterator_reset (&lcr_component_instance_database);
  88. while (hdb_iterator_next (&lcr_component_instance_database,
  89. (void **)(void *)&instance, &component_handle) == 0) {
  90. for (i = 0; i < instance->iface_count; i++) {
  91. if ((strcmp (instance->ifaces[i].name, iface_name) == 0) &&
  92. instance->ifaces[i].version == version) {
  93. *iface_number = i;
  94. return (instance);
  95. }
  96. }
  97. hdb_handle_put (&lcr_component_instance_database, component_handle);
  98. }
  99. return (NULL);
  100. }
  101. static inline int lcr_lib_loaded (
  102. char *library_name)
  103. {
  104. struct lcr_component_instance *instance;
  105. unsigned int component_handle = 0;
  106. /*
  107. * Try to find interface in already loaded component
  108. */
  109. hdb_iterator_reset (&lcr_component_instance_database);
  110. while (hdb_iterator_next (&lcr_component_instance_database,
  111. (void **)(void *)&instance, &component_handle) == 0) {
  112. if (strcmp (instance->library_name, library_name) == 0) {
  113. return (1);
  114. }
  115. hdb_handle_put (&lcr_component_instance_database, component_handle);
  116. }
  117. return (0);
  118. }
  119. int lcr_ifact_reference (
  120. unsigned int *iface_handle,
  121. char *iface_name,
  122. int version,
  123. void **iface,
  124. void *context)
  125. {
  126. void *dl_handle;
  127. struct lcr_iface_instance *iface_instance;
  128. struct lcr_component_instance *instance;
  129. int iface_number;
  130. struct dirent **scandir_list;
  131. int scandir_entries;
  132. unsigned int libs_to_scan;
  133. char cwd[512];
  134. char dl_name[1024];
  135. getcwd (cwd, sizeof (cwd));
  136. strcat (cwd, "/");
  137. /*
  138. * Determine if the component is already loaded
  139. */
  140. instance = lcr_comp_find (iface_name, version, &iface_number);
  141. if (instance) {
  142. goto found;
  143. }
  144. // TODO error checking in this code is weak
  145. /*
  146. * Find all *.lcrso files in the cwd
  147. */
  148. scandir_entries = scandir(".", &scandir_list, lcr_select_so, alphasort);
  149. if (scandir_entries < 0)
  150. printf ("scandir error reason=%s\n", strerror (errno));
  151. else
  152. /*
  153. * no error so load the object
  154. */
  155. for (libs_to_scan = 0; libs_to_scan < scandir_entries; libs_to_scan++) {
  156. /*
  157. * Load objects, scan them, unload them if they are not a match
  158. */
  159. sprintf (dl_name, "%s%s", cwd, scandir_list[libs_to_scan]->d_name);
  160. /*
  161. * Don't reload already loaded libraries
  162. */
  163. if (lcr_lib_loaded (dl_name)) {
  164. continue;
  165. }
  166. dl_handle = dlopen (dl_name, RTLD_NOW);
  167. if (dl_handle == NULL) {
  168. printf ("Error loading interface %s reason=%s\n", dl_name, dlerror());
  169. return (-1);
  170. }
  171. instance = lcr_comp_find (iface_name, version, &iface_number);
  172. if (instance) {
  173. instance->dl_handle = dl_handle;
  174. strcpy (instance->library_name, dl_name);
  175. goto found;
  176. }
  177. /*
  178. * No matching interfaces found, try next shared object
  179. */
  180. if (g_component_handle != 0xFFFFFFFF) {
  181. hdb_handle_destroy (&lcr_component_instance_database,
  182. g_component_handle);
  183. g_component_handle = 0xFFFFFFFF;
  184. }
  185. dlclose (dl_handle);
  186. } /* scanning for lcrso loop */
  187. /*
  188. * No matching interfaces found in all shared objects
  189. */
  190. return (-1);
  191. found:
  192. *iface = instance->ifaces[iface_number].interfaces;
  193. if (instance->ifaces[iface_number].constructor) {
  194. instance->ifaces[iface_number].constructor (context);
  195. }
  196. hdb_handle_create (&lcr_iface_instance_database,
  197. sizeof (struct lcr_iface_instance),
  198. iface_handle);
  199. hdb_handle_get (&lcr_iface_instance_database,
  200. *iface_handle, (void *)&iface_instance);
  201. iface_instance->component_handle = g_component_handle;
  202. iface_instance->context = context;
  203. iface_instance->destructor = instance->ifaces[iface_number].destructor;
  204. return (0);
  205. }
  206. int lcr_ifact_release (unsigned int handle)
  207. {
  208. struct lcr_iface_instance *iface_instance;
  209. int res = 0;
  210. res = hdb_handle_get (&lcr_iface_instance_database,
  211. handle, (void *)&iface_instance);
  212. return (res);
  213. if (iface_instance->destructor) {
  214. iface_instance->destructor (iface_instance->context);
  215. }
  216. hdb_handle_put (&lcr_component_instance_database,
  217. iface_instance->component_handle);
  218. hdb_handle_put (&lcr_iface_instance_database, handle);
  219. hdb_handle_destroy (&lcr_iface_instance_database, handle);
  220. return (res);
  221. }
  222. void lcr_component_register (struct lcr_comp *comp)
  223. {
  224. struct lcr_component_instance *instance;
  225. hdb_handle_create (&lcr_component_instance_database,
  226. sizeof (struct lcr_component_instance),
  227. &g_component_handle);
  228. hdb_handle_get (&lcr_component_instance_database,
  229. g_component_handle, (void *)&instance);
  230. instance->ifaces = comp->ifaces;
  231. instance->iface_count = comp->iface_count;
  232. instance->dl_handle = NULL;
  233. hdb_handle_put (&lcr_component_instance_database,
  234. g_component_handle);
  235. }