lcr_ifact.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530
  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. #ifdef OPENAIS_SOLARIS
  39. #include <iso/limits_iso.h>
  40. #endif
  41. #include "lcr_comp.h"
  42. #include "lcr_ifact.h"
  43. #include "../include/hdb.h"
  44. #include <stdlib.h>
  45. struct lcr_component_instance {
  46. struct lcr_iface *ifaces;
  47. int iface_count;
  48. unsigned int comp_handle;
  49. void *dl_handle;
  50. int refcount;
  51. char library_name[256];
  52. };
  53. struct lcr_iface_instance {
  54. unsigned int component_handle;
  55. void *context;
  56. void (*destructor) (void *context);
  57. };
  58. static struct hdb_handle_database lcr_component_instance_database = {
  59. .handle_count = 0,
  60. .handles = 0,
  61. .iterator = 0
  62. };
  63. static struct hdb_handle_database lcr_iface_instance_database = {
  64. .handle_count = 0,
  65. .handles = 0,
  66. .iterator = 0
  67. };
  68. static unsigned int g_component_handle = 0xFFFFFFFF;
  69. #if defined(OPENAIS_LINUX) || defined(OPENAIS_SOLARIS)
  70. static int lcr_select_so (const struct dirent *dirent)
  71. #else
  72. static int lcr_select_so (struct dirent *dirent)
  73. #endif
  74. {
  75. unsigned int len;
  76. len = strlen (dirent->d_name);
  77. if (len > 6) {
  78. if (strcmp (".lcrso", dirent->d_name + len - 6) == 0) {
  79. return (1);
  80. }
  81. }
  82. return (0);
  83. }
  84. #ifndef OPENAIS_SOLARIS
  85. #ifdef OPENAIS_LINUX
  86. static int pathlist_select (const struct dirent *dirent)
  87. #else
  88. static int pathlist_select (struct dirent *dirent)
  89. #endif
  90. {
  91. if (fnmatch ("*.conf", dirent->d_name, 0) == 0) {
  92. return (1);
  93. }
  94. return (0);
  95. }
  96. #endif
  97. static inline struct lcr_component_instance *lcr_comp_find (
  98. char *iface_name,
  99. unsigned int version,
  100. unsigned int *iface_number)
  101. {
  102. struct lcr_component_instance *instance;
  103. void *instance_p = NULL;
  104. unsigned int component_handle = 0;
  105. int i;
  106. /*
  107. * Try to find interface in already loaded component
  108. */
  109. hdb_iterator_reset (&lcr_component_instance_database);
  110. while (hdb_iterator_next (&lcr_component_instance_database,
  111. &instance_p, &component_handle) == 0) {
  112. instance = (struct lcr_component_instance *)instance_p;
  113. for (i = 0; i < instance->iface_count; i++) {
  114. if ((strcmp (instance->ifaces[i].name, iface_name) == 0) &&
  115. instance->ifaces[i].version == version) {
  116. *iface_number = i;
  117. return (instance);
  118. }
  119. }
  120. hdb_handle_put (&lcr_component_instance_database, component_handle);
  121. }
  122. return (NULL);
  123. }
  124. static inline int lcr_lib_loaded (
  125. char *library_name)
  126. {
  127. struct lcr_component_instance *instance;
  128. void *instance_p = NULL;
  129. unsigned int component_handle = 0;
  130. /*
  131. * Try to find interface in already loaded component
  132. */
  133. hdb_iterator_reset (&lcr_component_instance_database);
  134. while (hdb_iterator_next (&lcr_component_instance_database,
  135. (void *)&instance_p, &component_handle) == 0) {
  136. instance = (struct lcr_component_instance *)instance_p;
  137. if (strcmp (instance->library_name, library_name) == 0) {
  138. return (1);
  139. }
  140. hdb_handle_put (&lcr_component_instance_database, component_handle);
  141. }
  142. return (0);
  143. }
  144. char *path_list[128];
  145. unsigned int path_list_entries = 0;
  146. static void defaults_path_build (void)
  147. {
  148. char cwd[1024];
  149. char *res;
  150. res = getcwd (cwd, sizeof (cwd));
  151. if (res != NULL) {
  152. path_list[0] = strdup (cwd);
  153. path_list_entries++;
  154. }
  155. path_list[path_list_entries++] = LCRSODIR;
  156. }
  157. static void ld_library_path_build (void)
  158. {
  159. char *ld_library_path;
  160. char *my_ld_library_path;
  161. char *p_s, *ptrptr;
  162. ld_library_path = getenv ("LD_LIBRARY_PATH");
  163. if (ld_library_path == NULL) {
  164. return;
  165. }
  166. my_ld_library_path = strdup (ld_library_path);
  167. if (my_ld_library_path == NULL) {
  168. return;
  169. }
  170. p_s = strtok_r (my_ld_library_path, ":", &ptrptr);
  171. while (p_s != NULL) {
  172. path_list[path_list_entries++] = strdup (p_s);
  173. p_s = strtok_r (NULL, ":", &ptrptr);
  174. }
  175. free (my_ld_library_path);
  176. }
  177. static int ldso_path_build (char *path, char *filename)
  178. {
  179. #ifndef OPENAIS_SOLARIS
  180. FILE *fp;
  181. char string[1024];
  182. char filename_cat[1024];
  183. char newpath[1024];
  184. char *newpath_tmp;
  185. char *new_filename;
  186. int j;
  187. struct dirent **scandir_list;
  188. unsigned int scandir_entries;
  189. sprintf (filename_cat, "%s/%s", path, filename);
  190. if (filename[0] == '*') {
  191. scandir_entries = scandir (
  192. path,
  193. &scandir_list,
  194. pathlist_select, alphasort);
  195. if (scandir_entries == 0) {
  196. return 0;
  197. } else if (scandir_entries == -1) {
  198. return -1;
  199. } else {
  200. for (j = 0; j < scandir_entries; j++) {
  201. ldso_path_build (path, scandir_list[j]->d_name);
  202. }
  203. }
  204. }
  205. fp = fopen (filename_cat, "r");
  206. if (fp == NULL) {
  207. return (-1);
  208. }
  209. while (fgets (string, sizeof (string), fp)) {
  210. string[strlen(string) - 1] = '\0';
  211. if (strncmp (string, "include", strlen ("include")) == 0) {
  212. newpath_tmp = string + strlen ("include") + 1;
  213. for (j = strlen (string);
  214. string[j] != ' ' &&
  215. string[j] != '/' &&
  216. j > 0;
  217. j--) {
  218. }
  219. string[j] = '\0';
  220. new_filename = &string[j] + 1;
  221. strcpy (newpath, path);
  222. strcat (newpath, "/");
  223. strcat (newpath, newpath_tmp);
  224. ldso_path_build (newpath, new_filename);
  225. continue;
  226. }
  227. path_list[path_list_entries++] = strdup (string);
  228. }
  229. fclose(fp);
  230. #endif
  231. return (0);
  232. }
  233. #if defined (OPENAIS_SOLARIS) && !defined(HAVE_SCANDIR)
  234. static int scandir (
  235. const char *dir, struct dirent ***namelist,
  236. int (*filter)(const struct dirent *),
  237. int (*compar)(const struct dirent **, const struct dirent **))
  238. {
  239. DIR *d;
  240. struct dirent *entry, **names = NULL;
  241. int namelist_items = 0, namelist_size = 0;
  242. d = opendir(dir);
  243. if (d == NULL)
  244. return -1;
  245. names = NULL;
  246. while ((entry = readdir (d)) != NULL) {
  247. struct dirent *tmpentry;
  248. if ((filter != NULL) && ((*filter)(entry) == 0)) {
  249. continue;
  250. }
  251. if (namelist_items >= namelist_size) {
  252. struct dirent **tmp;
  253. namelist_size += 512;
  254. if ((unsigned long)namelist_size > INT_MAX) {
  255. errno = EOVERFLOW;
  256. goto fail;
  257. }
  258. tmp = realloc (names,
  259. namelist_size * sizeof(struct dirent *));
  260. if (tmp == NULL) {
  261. goto fail;
  262. }
  263. names = tmp;
  264. }
  265. tmpentry = malloc (entry->d_reclen);
  266. if (tmpentry == NULL) {
  267. goto fail;
  268. }
  269. (void) memcpy (tmpentry, entry, entry->d_reclen);
  270. names[namelist_items++] = tmpentry;
  271. }
  272. (void) closedir (d);
  273. if ((namelist_items > 1) && (compar != NULL)) {
  274. qsort (names, namelist_items, sizeof (struct dirent *),
  275. (int (*)(const void *, const void *))compar);
  276. }
  277. *namelist = names;
  278. return namelist_items;
  279. fail:
  280. {
  281. int err = errno;
  282. (void) closedir (d);
  283. while (namelist_items != 0) {
  284. namelist_items--;
  285. free (*namelist[namelist_items]);
  286. }
  287. if (names != NULL) {
  288. free (names);
  289. }
  290. *namelist = NULL;
  291. errno = err;
  292. return -1;
  293. }
  294. }
  295. #endif
  296. #if defined (OPENAIS_SOLARIS) && !defined(HAVE_ALPHASORT)
  297. static int alphasort (const struct dirent **a, const struct dirent **b)
  298. {
  299. return strcmp ((*a)->d_name, (*b)->d_name);
  300. }
  301. #endif
  302. static int interface_find_and_load (
  303. char *path,
  304. char *iface_name,
  305. int version,
  306. struct lcr_component_instance **instance_ret,
  307. unsigned int *iface_number)
  308. {
  309. struct lcr_component_instance *instance;
  310. void *dl_handle;
  311. struct dirent **scandir_list;
  312. int scandir_entries;
  313. unsigned int libs_to_scan;
  314. char dl_name[1024];
  315. scandir_entries = scandir (path, &scandir_list, lcr_select_so, alphasort);
  316. if (scandir_entries > 0)
  317. /*
  318. * no error so load the object
  319. */
  320. for (libs_to_scan = 0; libs_to_scan < scandir_entries; libs_to_scan++) {
  321. /*
  322. * Load objects, scan them, unload them if they are not a match
  323. */
  324. sprintf (dl_name, "%s/%s", path, scandir_list[libs_to_scan]->d_name);
  325. /*
  326. * Don't reload already loaded libraries
  327. */
  328. if (lcr_lib_loaded (dl_name)) {
  329. continue;
  330. }
  331. dl_handle = dlopen (dl_name, RTLD_LAZY);
  332. if (dl_handle == NULL) {
  333. fprintf(stderr, "%s: open failed: %s\n",
  334. dl_name, dlerror());
  335. continue;
  336. }
  337. instance = lcr_comp_find (iface_name, version, iface_number);
  338. if (instance) {
  339. instance->dl_handle = dl_handle;
  340. strcpy (instance->library_name, dl_name);
  341. goto found;
  342. }
  343. /*
  344. * No matching interfaces found, try next shared object
  345. */
  346. if (g_component_handle != 0xFFFFFFFF) {
  347. hdb_handle_destroy (&lcr_component_instance_database,
  348. g_component_handle);
  349. g_component_handle = 0xFFFFFFFF;
  350. }
  351. dlclose (dl_handle);
  352. } /* scanning for lcrso loop */
  353. if (scandir_entries > 0) {
  354. int i;
  355. for (i = 0; i < scandir_entries; i++) {
  356. free (scandir_list[i]);
  357. }
  358. free (scandir_list);
  359. }
  360. return -1;
  361. found:
  362. *instance_ret = instance;
  363. if (scandir_entries > 0) {
  364. int i;
  365. for (i = 0; i < scandir_entries; i++) {
  366. free (scandir_list[i]);
  367. }
  368. free (scandir_list);
  369. }
  370. return 0;
  371. }
  372. static unsigned int lcr_initialized = 0;
  373. int lcr_ifact_reference (
  374. unsigned int *iface_handle,
  375. char *iface_name,
  376. int version,
  377. void **iface,
  378. void *context)
  379. {
  380. struct lcr_iface_instance *iface_instance;
  381. struct lcr_component_instance *instance;
  382. unsigned int iface_number;
  383. unsigned int res;
  384. unsigned int i;
  385. /*
  386. * Determine if the component is already loaded
  387. */
  388. instance = lcr_comp_find (iface_name, version, &iface_number);
  389. if (instance) {
  390. goto found;
  391. }
  392. if (lcr_initialized == 0) {
  393. lcr_initialized = 1;
  394. defaults_path_build ();
  395. ld_library_path_build ();
  396. ldso_path_build ("/etc", "ld.so.conf");
  397. }
  398. // TODO error checking in this code is weak
  399. /*
  400. * Search through all lcrso files for desired interface
  401. */
  402. for (i = 0; i < path_list_entries; i++) {
  403. res = interface_find_and_load (
  404. path_list[i],
  405. iface_name,
  406. version,
  407. &instance,
  408. &iface_number);
  409. if (res == 0) {
  410. goto found;
  411. }
  412. }
  413. /*
  414. * No matching interfaces found in all shared objects
  415. */
  416. return (-1);
  417. found:
  418. *iface = instance->ifaces[iface_number].interfaces;
  419. if (instance->ifaces[iface_number].constructor) {
  420. instance->ifaces[iface_number].constructor (context);
  421. }
  422. hdb_handle_create (&lcr_iface_instance_database,
  423. sizeof (struct lcr_iface_instance),
  424. iface_handle);
  425. hdb_handle_get (&lcr_iface_instance_database,
  426. *iface_handle, (void *)&iface_instance);
  427. iface_instance->component_handle = instance->comp_handle;
  428. iface_instance->context = context;
  429. iface_instance->destructor = instance->ifaces[iface_number].destructor;
  430. hdb_handle_put (&lcr_iface_instance_database, *iface_handle);
  431. return (0);
  432. }
  433. int lcr_ifact_release (unsigned int handle)
  434. {
  435. struct lcr_iface_instance *iface_instance;
  436. int res = 0;
  437. res = hdb_handle_get (&lcr_iface_instance_database,
  438. handle, (void *)&iface_instance);
  439. if (iface_instance->destructor) {
  440. iface_instance->destructor (iface_instance->context);
  441. }
  442. hdb_handle_put (&lcr_component_instance_database,
  443. iface_instance->component_handle);
  444. hdb_handle_put (&lcr_iface_instance_database, handle);
  445. hdb_handle_destroy (&lcr_iface_instance_database, handle);
  446. return (res);
  447. }
  448. void lcr_component_register (struct lcr_comp *comp)
  449. {
  450. struct lcr_component_instance *instance;
  451. static unsigned int comp_handle;
  452. hdb_handle_create (&lcr_component_instance_database,
  453. sizeof (struct lcr_component_instance),
  454. &comp_handle);
  455. hdb_handle_get (&lcr_component_instance_database,
  456. comp_handle, (void *)&instance);
  457. instance->ifaces = comp->ifaces;
  458. instance->iface_count = comp->iface_count;
  459. instance->comp_handle = comp_handle;
  460. instance->dl_handle = NULL;
  461. hdb_handle_put (&lcr_component_instance_database,
  462. comp_handle);
  463. g_component_handle = comp_handle;
  464. }