lcr_ifact.c 12 KB

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