lcr_ifact.c 14 KB

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