lcr_ifact.c 13 KB

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