lcr_ifact.c 10 KB

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