lcr_ifact.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  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. char *res;
  141. res = getcwd (cwd, sizeof (cwd));
  142. if (res != NULL) {
  143. strcat (cwd, "/");
  144. path_list[0] = strdup (cwd);
  145. path_list_entries++;
  146. }
  147. path_list[path_list_entries++] = "/usr/libexec/lcrso";
  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[1024];
  180. char *newpath_tmp;
  181. char *new_filename;
  182. int j;
  183. struct dirent **scandir_list;
  184. unsigned int scandir_entries;
  185. sprintf (filename_cat, "%s/%s", path, filename);
  186. if (filename[0] == '*') {
  187. scandir_entries = scandir (
  188. path,
  189. &scandir_list,
  190. pathlist_select, alphasort);
  191. if (scandir_entries == 0) {
  192. return 0;
  193. } else if (scandir_entries == -1) {
  194. return -1;
  195. } else {
  196. for (j = 0; j < scandir_entries; j++) {
  197. ldso_path_build (path, scandir_list[j]->d_name);
  198. }
  199. }
  200. }
  201. fp = fopen (filename_cat, "r");
  202. if (fp == NULL) {
  203. return (-1);
  204. }
  205. while (fgets (string, sizeof (string), fp)) {
  206. string[strlen(string) - 1] = '\0';
  207. if (strncmp (string, "include", strlen ("include")) == 0) {
  208. newpath_tmp = string + strlen ("include") + 1;
  209. for (j = strlen (string);
  210. string[j] != ' ' &&
  211. string[j] != '/' &&
  212. j > 0;
  213. j--) {
  214. }
  215. string[j] = '\0';
  216. new_filename = &string[j] + 1;
  217. strcpy (newpath, path);
  218. strcat (newpath, "/");
  219. strcat (newpath, newpath_tmp);
  220. ldso_path_build (newpath, new_filename);
  221. continue;
  222. }
  223. path_list[path_list_entries++] = strdup (string);
  224. }
  225. fclose(fp);
  226. return (0);
  227. }
  228. static int interface_find_and_load (
  229. char *path,
  230. char *iface_name,
  231. int version,
  232. struct lcr_component_instance **instance_ret,
  233. unsigned int *iface_number)
  234. {
  235. struct lcr_component_instance *instance;
  236. void *dl_handle;
  237. struct dirent **scandir_list;
  238. int scandir_entries;
  239. unsigned int libs_to_scan;
  240. char dl_name[1024];
  241. scandir_entries = scandir (path, &scandir_list, lcr_select_so, alphasort);
  242. if (scandir_entries > 0)
  243. /*
  244. * no error so load the object
  245. */
  246. for (libs_to_scan = 0; libs_to_scan < scandir_entries; libs_to_scan++) {
  247. /*
  248. * Load objects, scan them, unload them if they are not a match
  249. */
  250. sprintf (dl_name, "%s/%s", path, scandir_list[libs_to_scan]->d_name);
  251. /*
  252. * Don't reload already loaded libraries
  253. */
  254. if (lcr_lib_loaded (dl_name)) {
  255. continue;
  256. }
  257. dl_handle = dlopen (dl_name, RTLD_LAZY);
  258. if (dl_handle == NULL) {
  259. continue;
  260. }
  261. instance = lcr_comp_find (iface_name, version, iface_number);
  262. if (instance) {
  263. instance->dl_handle = dl_handle;
  264. strcpy (instance->library_name, dl_name);
  265. goto found;
  266. }
  267. /*
  268. * No matching interfaces found, try next shared object
  269. */
  270. if (g_component_handle != 0xFFFFFFFF) {
  271. hdb_handle_destroy (&lcr_component_instance_database,
  272. g_component_handle);
  273. g_component_handle = 0xFFFFFFFF;
  274. }
  275. dlclose (dl_handle);
  276. } /* scanning for lcrso loop */
  277. return (-1);
  278. found:
  279. *instance_ret = instance;
  280. return (0);
  281. }
  282. int lcr_ifact_reference (
  283. unsigned int *iface_handle,
  284. char *iface_name,
  285. int version,
  286. void **iface,
  287. void *context)
  288. {
  289. struct lcr_iface_instance *iface_instance;
  290. struct lcr_component_instance *instance;
  291. unsigned int iface_number;
  292. unsigned int res;
  293. unsigned int i;
  294. /*
  295. * Determine if the component is already loaded
  296. */
  297. instance = lcr_comp_find (iface_name, version, &iface_number);
  298. if (instance) {
  299. goto found;
  300. }
  301. defaults_path_build ();
  302. ld_library_path_build ();
  303. ldso_path_build ("/etc", "ld.so.conf");
  304. // TODO error checking in this code is weak
  305. /*
  306. * Find all *.lcrso files in search paths
  307. */
  308. for (i = 0; i < path_list_entries; i++) {
  309. res = interface_find_and_load (
  310. path_list[i],
  311. iface_name,
  312. version,
  313. &instance,
  314. &iface_number);
  315. if (res == 0) {
  316. goto found;
  317. }
  318. }
  319. /*
  320. * No matching interfaces found in all shared objects
  321. */
  322. return (-1);
  323. found:
  324. *iface = instance->ifaces[iface_number].interfaces;
  325. if (instance->ifaces[iface_number].constructor) {
  326. instance->ifaces[iface_number].constructor (context);
  327. }
  328. hdb_handle_create (&lcr_iface_instance_database,
  329. sizeof (struct lcr_iface_instance),
  330. iface_handle);
  331. hdb_handle_get (&lcr_iface_instance_database,
  332. *iface_handle, (void *)&iface_instance);
  333. iface_instance->component_handle = g_component_handle;
  334. iface_instance->context = context;
  335. iface_instance->destructor = instance->ifaces[iface_number].destructor;
  336. return (0);
  337. }
  338. int lcr_ifact_release (unsigned int handle)
  339. {
  340. struct lcr_iface_instance *iface_instance;
  341. int res = 0;
  342. res = hdb_handle_get (&lcr_iface_instance_database,
  343. handle, (void *)&iface_instance);
  344. return (res);
  345. if (iface_instance->destructor) {
  346. iface_instance->destructor (iface_instance->context);
  347. }
  348. hdb_handle_put (&lcr_component_instance_database,
  349. iface_instance->component_handle);
  350. hdb_handle_put (&lcr_iface_instance_database, handle);
  351. hdb_handle_destroy (&lcr_iface_instance_database, handle);
  352. return (res);
  353. }
  354. void lcr_component_register (struct lcr_comp *comp)
  355. {
  356. struct lcr_component_instance *instance;
  357. hdb_handle_create (&lcr_component_instance_database,
  358. sizeof (struct lcr_component_instance),
  359. &g_component_handle);
  360. hdb_handle_get (&lcr_component_instance_database,
  361. g_component_handle, (void *)&instance);
  362. instance->ifaces = comp->ifaces;
  363. instance->iface_count = comp->iface_count;
  364. instance->dl_handle = NULL;
  365. hdb_handle_put (&lcr_component_instance_database,
  366. g_component_handle);
  367. }