lcr_ifact.c 7.4 KB

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