lcr_ifact.c 13 KB

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