lcr_ifact.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  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 hdb_handle_t 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. const 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. const 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 (const char *path, const 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. if (strlen(string) > 0)
  212. string[strlen(string) - 1] = '\0';
  213. if (strncmp (string, "include", strlen ("include")) == 0) {
  214. newpath_tmp = string + strlen ("include") + 1;
  215. for (j = strlen (string);
  216. string[j] != ' ' &&
  217. string[j] != '/' &&
  218. j > 0;
  219. j--) {
  220. }
  221. string[j] = '\0';
  222. new_filename = &string[j] + 1;
  223. strcpy (newpath, path);
  224. strcat (newpath, "/");
  225. strcat (newpath, newpath_tmp);
  226. ldso_path_build (newpath, new_filename);
  227. continue;
  228. }
  229. path_list[path_list_entries++] = strdup (string);
  230. }
  231. fclose(fp);
  232. #endif
  233. return (0);
  234. }
  235. #if defined (COROSYNC_SOLARIS) && !defined(HAVE_SCANDIR)
  236. static int scandir (
  237. const char *dir, struct dirent ***namelist,
  238. int (*filter)(const struct dirent *),
  239. int (*compar)(const struct dirent **, const struct dirent **))
  240. {
  241. DIR *d;
  242. struct dirent *entry, **names = NULL;
  243. int namelist_items = 0, namelist_size = 0;
  244. d = opendir(dir);
  245. if (d == NULL)
  246. return -1;
  247. names = NULL;
  248. while ((entry = readdir (d)) != NULL) {
  249. struct dirent *tmpentry;
  250. if ((filter != NULL) && ((*filter)(entry) == 0)) {
  251. continue;
  252. }
  253. if (namelist_items >= namelist_size) {
  254. struct dirent **tmp;
  255. namelist_size += 512;
  256. if ((unsigned long)namelist_size > INT_MAX) {
  257. errno = EOVERFLOW;
  258. goto fail;
  259. }
  260. tmp = realloc (names,
  261. namelist_size * sizeof(struct dirent *));
  262. if (tmp == NULL) {
  263. goto fail;
  264. }
  265. names = tmp;
  266. }
  267. tmpentry = malloc (entry->d_reclen);
  268. if (tmpentry == NULL) {
  269. goto fail;
  270. }
  271. (void) memcpy (tmpentry, entry, entry->d_reclen);
  272. names[namelist_items++] = tmpentry;
  273. }
  274. (void) closedir (d);
  275. if ((namelist_items > 1) && (compar != NULL)) {
  276. qsort (names, namelist_items, sizeof (struct dirent *),
  277. (int (*)(const void *, const void *))compar);
  278. }
  279. *namelist = names;
  280. return namelist_items;
  281. fail:
  282. {
  283. int err = errno;
  284. (void) closedir (d);
  285. while (namelist_items != 0) {
  286. namelist_items--;
  287. free (*namelist[namelist_items]);
  288. }
  289. if (names != NULL) {
  290. free (names);
  291. }
  292. *namelist = NULL;
  293. errno = err;
  294. return -1;
  295. }
  296. }
  297. #endif
  298. #if defined (COROSYNC_SOLARIS) && !defined(HAVE_ALPHASORT)
  299. static int alphasort (const struct dirent **a, const struct dirent **b)
  300. {
  301. return strcmp ((*a)->d_name, (*b)->d_name);
  302. }
  303. #endif
  304. static int interface_find_and_load (
  305. const char *path,
  306. const char *iface_name,
  307. int version,
  308. struct lcr_component_instance **instance_ret,
  309. unsigned int *iface_number)
  310. {
  311. struct lcr_component_instance *instance;
  312. void *dl_handle;
  313. struct dirent **scandir_list;
  314. int scandir_entries;
  315. unsigned int libs_to_scan;
  316. char dl_name[1024];
  317. scandir_entries = scandir (path, &scandir_list, lcr_select_so, alphasort);
  318. if (scandir_entries > 0)
  319. /*
  320. * no error so load the object
  321. */
  322. for (libs_to_scan = 0; libs_to_scan < scandir_entries; libs_to_scan++) {
  323. /*
  324. * Load objects, scan them, unload them if they are not a match
  325. */
  326. sprintf (dl_name, "%s/%s", path, scandir_list[libs_to_scan]->d_name);
  327. /*
  328. * Don't reload already loaded libraries
  329. */
  330. if (lcr_lib_loaded (dl_name)) {
  331. continue;
  332. }
  333. dl_handle = dlopen (dl_name, RTLD_LAZY);
  334. if (dl_handle == NULL) {
  335. fprintf(stderr, "%s: open failed: %s\n",
  336. dl_name, dlerror());
  337. continue;
  338. }
  339. instance = lcr_comp_find (iface_name, version, iface_number);
  340. if (instance) {
  341. instance->dl_handle = dl_handle;
  342. strcpy (instance->library_name, dl_name);
  343. goto found;
  344. }
  345. /*
  346. * No matching interfaces found, try next shared object
  347. */
  348. if (g_component_handle != 0xFFFFFFFF) {
  349. hdb_handle_destroy (&lcr_component_instance_database,
  350. g_component_handle);
  351. g_component_handle = 0xFFFFFFFF;
  352. }
  353. dlclose (dl_handle);
  354. } /* scanning for lcrso loop */
  355. if (scandir_entries > 0) {
  356. int i;
  357. for (i = 0; i < scandir_entries; i++) {
  358. free (scandir_list[i]);
  359. }
  360. free (scandir_list);
  361. }
  362. return -1;
  363. found:
  364. *instance_ret = instance;
  365. if (scandir_entries > 0) {
  366. int i;
  367. for (i = 0; i < scandir_entries; i++) {
  368. free (scandir_list[i]);
  369. }
  370. free (scandir_list);
  371. }
  372. return 0;
  373. }
  374. static unsigned int lcr_initialized = 0;
  375. int lcr_ifact_reference (
  376. hdb_handle_t *iface_handle,
  377. const char *iface_name,
  378. int version,
  379. void **iface,
  380. void *context)
  381. {
  382. struct lcr_iface_instance *iface_instance;
  383. struct lcr_component_instance *instance;
  384. unsigned int iface_number;
  385. unsigned int res;
  386. unsigned int i;
  387. /*
  388. * Determine if the component is already loaded
  389. */
  390. instance = lcr_comp_find (iface_name, version, &iface_number);
  391. if (instance) {
  392. goto found;
  393. }
  394. if (lcr_initialized == 0) {
  395. lcr_initialized = 1;
  396. defaults_path_build ();
  397. ld_library_path_build ();
  398. ldso_path_build ("/etc", "ld.so.conf");
  399. }
  400. // TODO error checking in this code is weak
  401. /*
  402. * Search through all lcrso files for desired interface
  403. */
  404. for (i = 0; i < path_list_entries; i++) {
  405. res = interface_find_and_load (
  406. path_list[i],
  407. iface_name,
  408. version,
  409. &instance,
  410. &iface_number);
  411. if (res == 0) {
  412. goto found;
  413. }
  414. }
  415. /*
  416. * No matching interfaces found in all shared objects
  417. */
  418. return (-1);
  419. found:
  420. *iface = instance->ifaces[iface_number].interfaces;
  421. if (instance->ifaces[iface_number].constructor) {
  422. instance->ifaces[iface_number].constructor (context);
  423. }
  424. hdb_handle_create (&lcr_iface_instance_database,
  425. sizeof (struct lcr_iface_instance),
  426. iface_handle);
  427. hdb_handle_get (&lcr_iface_instance_database,
  428. *iface_handle, (void *)&iface_instance);
  429. iface_instance->component_handle = instance->comp_handle;
  430. iface_instance->context = context;
  431. iface_instance->destructor = instance->ifaces[iface_number].destructor;
  432. hdb_handle_put (&lcr_iface_instance_database, *iface_handle);
  433. return (0);
  434. }
  435. int lcr_ifact_release (hdb_handle_t handle)
  436. {
  437. struct lcr_iface_instance *iface_instance;
  438. int res = 0;
  439. res = hdb_handle_get (&lcr_iface_instance_database,
  440. handle, (void *)&iface_instance);
  441. if (iface_instance->destructor) {
  442. iface_instance->destructor (iface_instance->context);
  443. }
  444. hdb_handle_put (&lcr_component_instance_database,
  445. iface_instance->component_handle);
  446. hdb_handle_put (&lcr_iface_instance_database, handle);
  447. hdb_handle_destroy (&lcr_iface_instance_database, handle);
  448. return (res);
  449. }
  450. void lcr_component_register (struct lcr_comp *comp)
  451. {
  452. struct lcr_component_instance *instance;
  453. static hdb_handle_t comp_handle;
  454. int res;
  455. hdb_handle_create (&lcr_component_instance_database,
  456. sizeof (struct lcr_component_instance),
  457. &comp_handle);
  458. hdb_handle_get (&lcr_component_instance_database,
  459. comp_handle, (void *)&instance);
  460. instance->ifaces = comp->ifaces;
  461. instance->iface_count = comp->iface_count;
  462. instance->comp_handle = comp_handle;
  463. instance->dl_handle = NULL;
  464. hdb_handle_put (&lcr_component_instance_database,
  465. comp_handle);
  466. g_component_handle = comp_handle;
  467. }