lcr_ifact.c 14 KB

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