lcr_ifact.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. /*
  2. * Copyright (C) 2006 Steven Dake (sdake@mvista.com)
  3. * Copyright (c) 2006 Sun Microsystems, Inc.
  4. *
  5. * This software licensed under BSD license, the text of which follows:
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. *
  10. * - Redistributions of source code must retain the above copyright notice,
  11. * this list of conditions and the following disclaimer.
  12. * - Redistributions in binary form must reproduce the above copyright notice,
  13. * this list of conditions and the following disclaimer in the documentation
  14. * and/or other materials provided with the distribution.
  15. * - Neither the name of the MontaVista Software, Inc. nor the names of its
  16. * contributors may be used to endorse or promote products derived from this
  17. * software without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  20. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  21. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  22. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  23. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  24. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  25. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  26. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  27. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  28. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  29. * THE POSSIBILITY OF SUCH DAMAGE.
  30. */
  31. #include <stdio.h>
  32. #include <dlfcn.h>
  33. #include <dirent.h>
  34. #include <errno.h>
  35. #include <string.h>
  36. #include <unistd.h>
  37. #include <fnmatch.h>
  38. #include "lcr_comp.h"
  39. #include "lcr_ifact.h"
  40. #include "../include/hdb.h"
  41. struct lcr_component_instance {
  42. struct lcr_iface *ifaces;
  43. int iface_count;
  44. void *dl_handle;
  45. int refcount;
  46. char library_name[256];
  47. };
  48. struct lcr_iface_instance {
  49. unsigned int component_handle;
  50. void *context;
  51. void (*destructor) (void *context);
  52. };
  53. static struct hdb_handle_database lcr_component_instance_database = {
  54. .handle_count = 0,
  55. .handles = 0,
  56. .iterator = 0
  57. };
  58. static struct hdb_handle_database lcr_iface_instance_database = {
  59. .handle_count = 0,
  60. .handles = 0,
  61. .iterator = 0
  62. };
  63. static unsigned int g_component_handle;
  64. #ifdef OPENAIS_LINUX
  65. static int lcr_select_so (const struct dirent *dirent)
  66. #else
  67. static int lcr_select_so (struct dirent *dirent)
  68. #endif
  69. {
  70. unsigned int len;
  71. len = strlen (dirent->d_name);
  72. if (len > 6) {
  73. if (strcmp (".lcrso", dirent->d_name + len - 6) == 0) {
  74. return (1);
  75. }
  76. }
  77. return (0);
  78. }
  79. #ifdef OPENAIS_LINUX
  80. static int pathlist_select (const struct dirent *dirent)
  81. #else
  82. static int pathlist_select (struct dirent *dirent)
  83. #endif
  84. {
  85. if (fnmatch ("*.conf", dirent->d_name, 0) == 0) {
  86. return (1);
  87. }
  88. return (0);
  89. }
  90. static inline struct lcr_component_instance *lcr_comp_find (
  91. char *iface_name,
  92. unsigned int version,
  93. unsigned int *iface_number)
  94. {
  95. struct lcr_component_instance *instance;
  96. void *instance_p = NULL;
  97. unsigned int component_handle = 0;
  98. int i;
  99. /*
  100. * Try to find interface in already loaded component
  101. */
  102. hdb_iterator_reset (&lcr_component_instance_database);
  103. while (hdb_iterator_next (&lcr_component_instance_database,
  104. &instance_p, &component_handle) == 0) {
  105. instance = (struct lcr_component_instance *)instance_p;
  106. for (i = 0; i < instance->iface_count; i++) {
  107. if ((strcmp (instance->ifaces[i].name, iface_name) == 0) &&
  108. instance->ifaces[i].version == version) {
  109. *iface_number = i;
  110. return (instance);
  111. }
  112. }
  113. hdb_handle_put (&lcr_component_instance_database, component_handle);
  114. }
  115. return (NULL);
  116. }
  117. static inline int lcr_lib_loaded (
  118. char *library_name)
  119. {
  120. struct lcr_component_instance *instance;
  121. void *instance_p = NULL;
  122. unsigned int component_handle = 0;
  123. /*
  124. * Try to find interface in already loaded component
  125. */
  126. hdb_iterator_reset (&lcr_component_instance_database);
  127. while (hdb_iterator_next (&lcr_component_instance_database,
  128. (void *)&instance_p, &component_handle) == 0) {
  129. instance = (struct lcr_component_instance *)instance_p;
  130. if (strcmp (instance->library_name, library_name) == 0) {
  131. return (1);
  132. }
  133. hdb_handle_put (&lcr_component_instance_database, component_handle);
  134. }
  135. return (0);
  136. }
  137. char *path_list[128];
  138. unsigned int path_list_entries = 0;
  139. static void defaults_path_build (void)
  140. {
  141. char cwd[1024];
  142. char *res;
  143. res = getcwd (cwd, sizeof (cwd));
  144. if (res != NULL) {
  145. strcat (cwd, "/");
  146. path_list[0] = strdup (cwd);
  147. path_list_entries++;
  148. }
  149. path_list[path_list_entries++] = "/usr/libexec/lcrso";
  150. }
  151. static void ld_library_path_build (void)
  152. {
  153. char *ld_library_path;
  154. char *my_ld_library_path;
  155. char *p_s, *ptrptr;
  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 == NULL) {
  162. return;
  163. }
  164. p_s = strtok_r (my_ld_library_path, ":", &ptrptr);
  165. while (p_s != NULL) {
  166. path_list[path_list_entries++] = strdup (p_s);
  167. p_s = strtok_r (NULL, ":", &ptrptr);
  168. }
  169. free (my_ld_library_path);
  170. }
  171. static int ldso_path_build (char *path, char *filename)
  172. {
  173. FILE *fp;
  174. char string[1024];
  175. char filename_cat[1024];
  176. char newpath[1024];
  177. char *newpath_tmp;
  178. char *new_filename;
  179. int j;
  180. struct dirent **scandir_list;
  181. unsigned int scandir_entries;
  182. sprintf (filename_cat, "%s/%s", path, filename);
  183. if (filename[0] == '*') {
  184. scandir_entries = scandir (
  185. path,
  186. &scandir_list,
  187. pathlist_select, alphasort);
  188. if (scandir_entries == 0) {
  189. return 0;
  190. } else if (scandir_entries == -1) {
  191. return -1;
  192. } else {
  193. for (j = 0; j < scandir_entries; j++) {
  194. ldso_path_build (path, scandir_list[j]->d_name);
  195. }
  196. }
  197. }
  198. fp = fopen (filename_cat, "r");
  199. if (fp == NULL) {
  200. return (-1);
  201. }
  202. while (fgets (string, sizeof (string), fp)) {
  203. string[strlen(string) - 1] = '\0';
  204. if (strncmp (string, "include", strlen ("include")) == 0) {
  205. newpath_tmp = string + strlen ("include") + 1;
  206. for (j = strlen (string);
  207. string[j] != ' ' &&
  208. string[j] != '/' &&
  209. j > 0;
  210. j--) {
  211. }
  212. string[j] = '\0';
  213. new_filename = &string[j] + 1;
  214. strcpy (newpath, path);
  215. strcat (newpath, "/");
  216. strcat (newpath, newpath_tmp);
  217. ldso_path_build (newpath, new_filename);
  218. continue;
  219. }
  220. path_list[path_list_entries++] = strdup (string);
  221. }
  222. fclose(fp);
  223. return (0);
  224. }
  225. static int interface_find_and_load (
  226. char *path,
  227. char *iface_name,
  228. int version,
  229. struct lcr_component_instance **instance_ret,
  230. unsigned int *iface_number)
  231. {
  232. struct lcr_component_instance *instance;
  233. void *dl_handle;
  234. struct dirent **scandir_list;
  235. int scandir_entries;
  236. unsigned int libs_to_scan;
  237. char dl_name[1024];
  238. scandir_entries = scandir (path, &scandir_list, lcr_select_so, alphasort);
  239. if (scandir_entries > 0)
  240. /*
  241. * no error so load the object
  242. */
  243. for (libs_to_scan = 0; libs_to_scan < scandir_entries; libs_to_scan++) {
  244. /*
  245. * Load objects, scan them, unload them if they are not a match
  246. */
  247. sprintf (dl_name, "%s/%s", path, scandir_list[libs_to_scan]->d_name);
  248. /*
  249. * Don't reload already loaded libraries
  250. */
  251. if (lcr_lib_loaded (dl_name)) {
  252. continue;
  253. }
  254. dl_handle = dlopen (dl_name, RTLD_LAZY);
  255. if (dl_handle == NULL) {
  256. continue;
  257. }
  258. instance = lcr_comp_find (iface_name, version, iface_number);
  259. if (instance) {
  260. instance->dl_handle = dl_handle;
  261. strcpy (instance->library_name, dl_name);
  262. goto found;
  263. }
  264. /*
  265. * No matching interfaces found, try next shared object
  266. */
  267. if (g_component_handle != 0xFFFFFFFF) {
  268. hdb_handle_destroy (&lcr_component_instance_database,
  269. g_component_handle);
  270. g_component_handle = 0xFFFFFFFF;
  271. }
  272. dlclose (dl_handle);
  273. } /* scanning for lcrso loop */
  274. return (-1);
  275. found:
  276. *instance_ret = instance;
  277. return (0);
  278. }
  279. static unsigned int lcr_initialized = 0;
  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. if (lcr_initialized == 0) {
  300. lcr_initialized = 1;
  301. defaults_path_build ();
  302. ld_library_path_build ();
  303. ldso_path_build ("/etc", "ld.so.conf");
  304. }
  305. // TODO error checking in this code is weak
  306. /*
  307. * Find all *.lcrso files in search paths
  308. */
  309. for (i = 0; i < path_list_entries; i++) {
  310. res = interface_find_and_load (
  311. path_list[i],
  312. iface_name,
  313. version,
  314. &instance,
  315. &iface_number);
  316. if (res == 0) {
  317. goto found;
  318. }
  319. }
  320. /*
  321. * No matching interfaces found in all shared objects
  322. */
  323. return (-1);
  324. found:
  325. *iface = instance->ifaces[iface_number].interfaces;
  326. if (instance->ifaces[iface_number].constructor) {
  327. instance->ifaces[iface_number].constructor (context);
  328. }
  329. hdb_handle_create (&lcr_iface_instance_database,
  330. sizeof (struct lcr_iface_instance),
  331. iface_handle);
  332. hdb_handle_get (&lcr_iface_instance_database,
  333. *iface_handle, (void *)&iface_instance);
  334. iface_instance->component_handle = g_component_handle;
  335. iface_instance->context = context;
  336. iface_instance->destructor = instance->ifaces[iface_number].destructor;
  337. return (0);
  338. }
  339. int lcr_ifact_release (unsigned int handle)
  340. {
  341. struct lcr_iface_instance *iface_instance;
  342. int res = 0;
  343. res = hdb_handle_get (&lcr_iface_instance_database,
  344. handle, (void *)&iface_instance);
  345. return (res);
  346. if (iface_instance->destructor) {
  347. iface_instance->destructor (iface_instance->context);
  348. }
  349. hdb_handle_put (&lcr_component_instance_database,
  350. iface_instance->component_handle);
  351. hdb_handle_put (&lcr_iface_instance_database, handle);
  352. hdb_handle_destroy (&lcr_iface_instance_database, handle);
  353. return (res);
  354. }
  355. void lcr_component_register (struct lcr_comp *comp)
  356. {
  357. struct lcr_component_instance *instance;
  358. hdb_handle_create (&lcr_component_instance_database,
  359. sizeof (struct lcr_component_instance),
  360. &g_component_handle);
  361. hdb_handle_get (&lcr_component_instance_database,
  362. g_component_handle, (void *)&instance);
  363. instance->ifaces = comp->ifaces;
  364. instance->iface_count = comp->iface_count;
  365. instance->dl_handle = NULL;
  366. hdb_handle_put (&lcr_component_instance_database,
  367. g_component_handle);
  368. }