4
0

lcr_ifact.c 13 KB

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