lcr_ifact.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  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 <fnmatch.h>
  36. #include "lcr_comp.h"
  37. #include "lcr_ifact.h"
  38. #include "../include/hdb.h"
  39. struct lcr_component_instance {
  40. struct lcr_iface *ifaces;
  41. int iface_count;
  42. void *dl_handle;
  43. int refcount;
  44. char library_name[256];
  45. };
  46. struct lcr_iface_instance {
  47. unsigned int component_handle;
  48. void *context;
  49. void (*destructor) (void *context);
  50. };
  51. static struct hdb_handle_database lcr_component_instance_database = {
  52. .handle_count = 0,
  53. .handles = 0,
  54. .iterator = 0
  55. };
  56. static struct hdb_handle_database lcr_iface_instance_database = {
  57. .handle_count = 0,
  58. .handles = 0,
  59. .iterator = 0
  60. };
  61. static unsigned int g_component_handle;
  62. #ifdef OPENAIS_LINUX
  63. static int lcr_select_so (const struct dirent *dirent)
  64. #else
  65. static int lcr_select_so (struct dirent *dirent)
  66. #endif
  67. {
  68. unsigned int len;
  69. len = strlen (dirent->d_name);
  70. if (len > 6) {
  71. if (strcmp (".lcrso", dirent->d_name + len - 6) == 0) {
  72. return (1);
  73. }
  74. }
  75. return (0);
  76. }
  77. #ifdef OPENAIS_LINUX
  78. static int pathlist_select (const struct dirent *dirent)
  79. #else
  80. static int pathlist_select (struct dirent *dirent)
  81. #endif
  82. {
  83. if (fnmatch ("*.conf", dirent->d_name, 0) == 0) {
  84. return (1);
  85. }
  86. return (0);
  87. }
  88. static inline struct lcr_component_instance *lcr_comp_find (
  89. char *iface_name,
  90. unsigned int version,
  91. unsigned int *iface_number)
  92. {
  93. struct lcr_component_instance *instance;
  94. void *instance_p = NULL;
  95. unsigned int component_handle = 0;
  96. int i;
  97. /*
  98. * Try to find interface in already loaded component
  99. */
  100. hdb_iterator_reset (&lcr_component_instance_database);
  101. while (hdb_iterator_next (&lcr_component_instance_database,
  102. &instance_p, &component_handle) == 0) {
  103. instance = (struct lcr_component_instance *)instance_p;
  104. for (i = 0; i < instance->iface_count; i++) {
  105. if ((strcmp (instance->ifaces[i].name, iface_name) == 0) &&
  106. instance->ifaces[i].version == version) {
  107. *iface_number = i;
  108. return (instance);
  109. }
  110. }
  111. hdb_handle_put (&lcr_component_instance_database, component_handle);
  112. }
  113. return (NULL);
  114. }
  115. static inline int lcr_lib_loaded (
  116. char *library_name)
  117. {
  118. struct lcr_component_instance *instance;
  119. void *instance_p = NULL;
  120. unsigned int component_handle = 0;
  121. /*
  122. * Try to find interface in already loaded component
  123. */
  124. hdb_iterator_reset (&lcr_component_instance_database);
  125. while (hdb_iterator_next (&lcr_component_instance_database,
  126. (void *)&instance_p, &component_handle) == 0) {
  127. instance = (struct lcr_component_instance *)instance_p;
  128. if (strcmp (instance->library_name, library_name) == 0) {
  129. return (1);
  130. }
  131. hdb_handle_put (&lcr_component_instance_database, component_handle);
  132. }
  133. return (0);
  134. }
  135. char *path_list[128];
  136. unsigned int path_list_entries = 0;
  137. static void defaults_path_build (void)
  138. {
  139. char cwd[1024];
  140. getcwd (cwd, sizeof (cwd));
  141. strcat (cwd, "/");
  142. path_list[0] = strdup (cwd);
  143. path_list[1] = "/lib";
  144. path_list[2] = "/usr/lib";
  145. path_list[3] = "/lib64";
  146. path_list[4] = "/usr/lib64";
  147. path_list_entries = 5;
  148. }
  149. static void ld_library_path_build (void)
  150. {
  151. char *ld_library_path;
  152. char *my_ld_library_path;
  153. char *p_s;
  154. unsigned int i;
  155. unsigned int len;
  156. ld_library_path = getenv ("LD_LIBRARY_PATH");
  157. if (ld_library_path == NULL) {
  158. return;
  159. }
  160. my_ld_library_path = strdup (ld_library_path);
  161. if (my_ld_library_path == 0) {
  162. return;
  163. }
  164. len = strlen (my_ld_library_path) + 1;
  165. for (i = 0, p_s = my_ld_library_path, i = 0; i < len; i++) {
  166. if (my_ld_library_path[i] == ':' || my_ld_library_path[i] == '\0') {
  167. my_ld_library_path[i]='\0';
  168. path_list[path_list_entries++] = strdup (p_s);
  169. p_s = &my_ld_library_path[i];
  170. }
  171. }
  172. free (my_ld_library_path);
  173. }
  174. static int ldso_path_build (char *path, char *filename)
  175. {
  176. FILE *fp;
  177. char string[1024];
  178. char filename_cat[1024];
  179. char *newpath;
  180. char *new_filename;
  181. int j;
  182. struct dirent **scandir_list;
  183. unsigned int scandir_entries;
  184. sprintf (filename_cat, "%s/%s", path, filename);
  185. if (filename[0] == '*') {
  186. scandir_entries = scandir (
  187. path,
  188. &scandir_list,
  189. pathlist_select, alphasort);
  190. if (scandir_entries == 0) {
  191. return 0;
  192. } else if (scandir_entries == -1) {
  193. return -1;
  194. } else {
  195. for (j = 0; j < scandir_entries; j++) {
  196. ldso_path_build (path, scandir_list[j]->d_name);
  197. }
  198. }
  199. }
  200. fp = fopen (filename_cat, "r");
  201. if (fp == NULL) {
  202. return (-1);
  203. }
  204. while (fgets (string, sizeof (string), fp)) {
  205. string[strlen(string) - 1] = '\0';
  206. if (strncmp (string, "include", strlen ("include")) == 0) {
  207. newpath = string + strlen ("include") + 1;
  208. for (j = strlen (string);
  209. string[j] != ' ' &&
  210. string[j] != '/' &&
  211. j > 0;
  212. j--) {
  213. }
  214. string[j] = '\0';
  215. new_filename = &string[j] + 1;
  216. ldso_path_build (newpath, new_filename);
  217. continue;
  218. }
  219. path_list[path_list_entries++] = strdup (string);
  220. }
  221. fclose(fp);
  222. return (0);
  223. }
  224. static int interface_find_and_load (
  225. char *path,
  226. char *iface_name,
  227. int version,
  228. struct lcr_component_instance **instance_ret,
  229. unsigned int *iface_number)
  230. {
  231. struct lcr_component_instance *instance;
  232. void *dl_handle;
  233. struct dirent **scandir_list;
  234. int scandir_entries;
  235. unsigned int libs_to_scan;
  236. char dl_name[1024];
  237. scandir_entries = scandir (path, &scandir_list, lcr_select_so, alphasort);
  238. if (scandir_entries < 0)
  239. printf ("scandir error reason=%s\n", strerror (errno));
  240. else
  241. /*
  242. * no error so load the object
  243. */
  244. for (libs_to_scan = 0; libs_to_scan < scandir_entries; libs_to_scan++) {
  245. /*
  246. * Load objects, scan them, unload them if they are not a match
  247. */
  248. sprintf (dl_name, "%s/%s", path, scandir_list[libs_to_scan]->d_name);
  249. /*
  250. * Don't reload already loaded libraries
  251. */
  252. if (lcr_lib_loaded (dl_name)) {
  253. continue;
  254. }
  255. dl_handle = dlopen (dl_name, RTLD_LAZY);
  256. if (dl_handle == NULL) {
  257. continue;
  258. }
  259. instance = lcr_comp_find (iface_name, version, iface_number);
  260. if (instance) {
  261. instance->dl_handle = dl_handle;
  262. strcpy (instance->library_name, dl_name);
  263. goto found;
  264. }
  265. /*
  266. * No matching interfaces found, try next shared object
  267. */
  268. if (g_component_handle != 0xFFFFFFFF) {
  269. hdb_handle_destroy (&lcr_component_instance_database,
  270. g_component_handle);
  271. g_component_handle = 0xFFFFFFFF;
  272. }
  273. dlclose (dl_handle);
  274. } /* scanning for lcrso loop */
  275. return (-1);
  276. found:
  277. *instance_ret = instance;
  278. return (0);
  279. }
  280. int lcr_ifact_reference (
  281. unsigned int *iface_handle,
  282. char *iface_name,
  283. int version,
  284. void **iface,
  285. void *context)
  286. {
  287. struct lcr_iface_instance *iface_instance;
  288. struct lcr_component_instance *instance;
  289. unsigned int iface_number;
  290. unsigned int res;
  291. unsigned int i;
  292. /*
  293. * Determine if the component is already loaded
  294. */
  295. instance = lcr_comp_find (iface_name, version, &iface_number);
  296. if (instance) {
  297. goto found;
  298. }
  299. defaults_path_build ();
  300. ld_library_path_build ();
  301. ldso_path_build ("/etc", "ld.so.conf");
  302. // TODO error checking in this code is weak
  303. /*
  304. * Find all *.lcrso files in the cwd
  305. */
  306. //path = getenv ("LD_LIBRARY_PATH");
  307. for (i = 0; i < path_list_entries; i++) {
  308. res = interface_find_and_load (
  309. path_list[i],
  310. iface_name,
  311. version,
  312. &instance,
  313. &iface_number);
  314. if (res == 0) {
  315. goto found;
  316. }
  317. }
  318. /*
  319. * No matching interfaces found in all shared objects
  320. */
  321. return (-1);
  322. found:
  323. *iface = instance->ifaces[iface_number].interfaces;
  324. if (instance->ifaces[iface_number].constructor) {
  325. instance->ifaces[iface_number].constructor (context);
  326. }
  327. hdb_handle_create (&lcr_iface_instance_database,
  328. sizeof (struct lcr_iface_instance),
  329. iface_handle);
  330. hdb_handle_get (&lcr_iface_instance_database,
  331. *iface_handle, (void *)&iface_instance);
  332. iface_instance->component_handle = g_component_handle;
  333. iface_instance->context = context;
  334. iface_instance->destructor = instance->ifaces[iface_number].destructor;
  335. return (0);
  336. }
  337. int lcr_ifact_release (unsigned int handle)
  338. {
  339. struct lcr_iface_instance *iface_instance;
  340. int res = 0;
  341. res = hdb_handle_get (&lcr_iface_instance_database,
  342. handle, (void *)&iface_instance);
  343. return (res);
  344. if (iface_instance->destructor) {
  345. iface_instance->destructor (iface_instance->context);
  346. }
  347. hdb_handle_put (&lcr_component_instance_database,
  348. iface_instance->component_handle);
  349. hdb_handle_put (&lcr_iface_instance_database, handle);
  350. hdb_handle_destroy (&lcr_iface_instance_database, handle);
  351. return (res);
  352. }
  353. void lcr_component_register (struct lcr_comp *comp)
  354. {
  355. struct lcr_component_instance *instance;
  356. hdb_handle_create (&lcr_component_instance_database,
  357. sizeof (struct lcr_component_instance),
  358. &g_component_handle);
  359. hdb_handle_get (&lcr_component_instance_database,
  360. g_component_handle, (void *)&instance);
  361. instance->ifaces = comp->ifaces;
  362. instance->iface_count = comp->iface_count;
  363. instance->dl_handle = NULL;
  364. hdb_handle_put (&lcr_component_instance_database,
  365. g_component_handle);
  366. }