/* * Initialize the repository */ int mca_base_component_repository_init(void) { /* Setup internal structures */ if (!initialized) { #if OPAL_WANT_LIBLTDL /* Initialize libltdl */ if (lt_dlinit() != 0) { return OPAL_ERR_OUT_OF_RESOURCE; } #if OPAL_HAVE_LTDL_ADVISE if (lt_dladvise_init(&opal_mca_dladvise)) { return OPAL_ERR_OUT_OF_RESOURCE; } if (lt_dladvise_ext(&opal_mca_dladvise)) { return OPAL_ERROR; } if (lt_dladvise_global(&opal_mca_dladvise)) { return OPAL_ERROR; } #endif OBJ_CONSTRUCT(&repository, opal_list_t); #endif initialized = true; } /* All done */ return OPAL_SUCCESS; }
/* * Load shared module using the equivalent of the RTLD_GLOBAL flag */ static lt_dlhandle dlopenext_global (const char *filename) { lt_dlhandle handle = 0; lt_dladvise advise; if (!lt_dladvise_init (&advise) && !lt_dladvise_ext (&advise) && !lt_dladvise_global(&advise)) handle = lt_dlopenadvise (filename, advise); lt_dladvise_destroy (&advise); return handle; }
static lt_dlhandle my_dlopen(const char *filename) { lt_dlhandle handle = 0; lt_dladvise advice[1]; if (!lt_dladvise_init(advice) && !lt_dladvise_ext(advice) && !lt_dladvise_global(advice)) { handle = lt_dlopenadvise(filename, advice[0]); } lt_dladvise_destroy(advice); return handle; }
void plugins_init(const char *path) { char *plugins_path; lt_dladvise advise; if (path && !strcmp(path, "none")) { return; } if (!(plugins_path = path ? xstrdup(path) : xstrdup(ovs_pluginsdir()))) { VLOG_ERR("Failed to allocate plugins path"); return; } if (lt_dlinit() || lt_dlsetsearchpath(plugins_path) || lt_dladvise_init(&advise)) { VLOG_ERR("ltdl initializations: %s", lt_dlerror()); goto err_init; } if (!(interface_id = lt_dlinterface_register("ovs-plugin", NULL))) { VLOG_ERR("lt_dlinterface_register: %s", lt_dlerror()); goto err_interface_register; } if (lt_dladvise_global(&advise) || lt_dladvise_ext (&advise) || lt_dlforeachfile(lt_dlgetsearchpath(), &plugins_open_plugin, &advise)) { VLOG_ERR("ltdl setting advise: %s", lt_dlerror()); goto err_set_advise; } VLOG_INFO("Successfully initialized all plugins"); return; err_set_advise: lt_dlinterface_free(interface_id); err_interface_register: if (lt_dladvise_destroy(&advise)) { VLOG_ERR("destroying ltdl advise%s", lt_dlerror()); return; } err_init: free(plugins_path); }
static int dso_load_module(char *name) { int res; char *symbol_name, *path, *tmp; module *m; lt_ptr mh = NULL; lt_dladvise advise; if (name == NULL) { errno = EINVAL; return -1; } if (strncmp(name, "mod_", 4) != 0 || name[strlen(name)-2] != '.' || name[strlen(name)-1] != 'c') { errno = EINVAL; return -1; } pr_log_debug(DEBUG7, "loading '%s'", name); tmp = strrchr(name, '.'); if (tmp == NULL) { errno = EINVAL; return -1; } if (lt_dladvise_init(&advise) < 0) { pr_log_pri(PR_LOG_NOTICE, MOD_DSO_VERSION ": unable to initialise advise: %s", lt_dlerror()); errno = EPERM; return -1; } if (lt_dladvise_ext(&advise) < 0) { pr_log_pri(PR_LOG_NOTICE, MOD_DSO_VERSION ": unable to setting 'ext' advise hint: %s", lt_dlerror()); lt_dladvise_destroy(&advise); errno = EPERM; return -1; } if (lt_dladvise_global(&advise) < 0) { pr_log_pri(PR_LOG_NOTICE, MOD_DSO_VERSION ": unable to setting 'global' advise hint: %s", lt_dlerror()); lt_dladvise_destroy(&advise); errno = EPERM; return -1; } *tmp = '\0'; /* Load file: $prefix/libexec/<module> */ path = pdircat(dso_pool, dso_module_path, name, NULL); pr_trace_msg(trace_channel, 5, "loading module '%s'", path); mh = lt_dlopenadvise(path, advise); if (mh == NULL) { *tmp = '.'; pr_log_debug(DEBUG3, MOD_DSO_VERSION ": unable to dlopen '%s': %s (%s)", name, lt_dlerror(), strerror(errno)); pr_log_debug(DEBUG3, MOD_DSO_VERSION ": defaulting to 'self' for symbol resolution"); lt_dladvise_destroy(&advise); mh = lt_dlopen(NULL); if (mh == NULL) { pr_log_debug(DEBUG0, MOD_DSO_VERSION ": error loading 'self': %s", lt_dlerror()); if (errno == ENOENT) { pr_log_pri(PR_LOG_NOTICE, MOD_DSO_VERSION ": check to see if '%s.la' exists", path); } return -1; } } lt_dladvise_destroy(&advise); /* Tease name of the module structure out of the given name: * <module>.<ext> --> <module>_module */ *tmp = '\0'; symbol_name = pstrcat(dso_pool, name+4, "_module", NULL); /* Lookup module structure symbol by name. */ pr_trace_msg(trace_channel, 7, "looking for symbol '%s' in loaded module", symbol_name); m = (module *) lt_dlsym(mh, symbol_name); if (m == NULL) { *tmp = '.'; pr_log_debug(DEBUG1, MOD_DSO_VERSION ": unable to find module symbol '%s' in '%s'", symbol_name, mh ? name : "self"); pr_trace_msg(trace_channel, 1, "unable to find module symbol '%s' in '%s'", symbol_name, mh ? name : "self"); lt_dlclose(mh); mh = NULL; if (errno == ENOENT) { pr_log_pri(PR_LOG_NOTICE, MOD_DSO_VERSION ": check to see if '%s.la' exists", path); } return -1; } *tmp = '.'; m->handle = mh; /* Add the module to the core structures */ res = pr_module_load(m); if (res < 0) { if (errno == EEXIST) { pr_log_pri(PR_LOG_INFO, MOD_DSO_VERSION ": module 'mod_%s.c' already loaded", m->name); pr_trace_msg(trace_channel, 1, "module 'mod_%s.c' already loaded", m->name); } else if (errno == EACCES) { pr_log_pri(PR_LOG_ERR, MOD_DSO_VERSION ": module 'mod_%s.c' has wrong API version (0x%x), must be 0x%x", m->name, m->api_version, PR_MODULE_API_VERSION); pr_trace_msg(trace_channel, 1, "module 'mod_%s.c' has wrong API version (0x%x), must be 0x%x", m->name, m->api_version, PR_MODULE_API_VERSION); } else if (errno == EPERM) { pr_log_pri(PR_LOG_ERR, MOD_DSO_VERSION ": module 'mod_%s.c' failed to initialize", m->name); pr_trace_msg(trace_channel, 1, "module 'mod_%s.c' failed to initialize", m->name); } lt_dlclose(mh); mh = NULL; return -1; } pr_trace_msg(trace_channel, 8, "module '%s' successfully loaded", path); return 0; }
static int do_test(void) { FILE *fp; char filename[] = "./libompi_dbg_msgq"; char full_filename[] = "./libompi_dbg_msgq.la"; char line[1024]; int happy; lt_dlhandle dlhandle; #if OPAL_HAVE_LTDL_ADVISE lt_dladvise dladvise; #endif /* Double check that the .la file is there that we expect; if it's not, skip this test. */ fp = fopen(full_filename, "r"); if (NULL == fp) { fprintf(stderr, "File %s.la doesn't seem to exist; skipping this test\n", full_filename); exit(77); } /* We know the .la file is there, so read it, looking for the dlopen value. If the dlopen value is '' (i.e., empty), then there's nothing to dlopen (i.e., OMPI was built with --enable-static --disable-shared, so return 77 to skip this test. This is horrible, but I can't think of a better way to check it (since there is no good way to #define whether we have built statically or not...). */ happy = 0; while (1) { if (0 == fgets(line, sizeof(line) - 1, fp)) { break; } if (0 == strncmp(line, "dlname=", 7)) { if (0 == strncmp(line + 7, "''", 2)) { happy = 0; } else { happy = 1; } break; } } fclose(fp); if (!happy) { fprintf(stderr, "No test file to dlopen (perhaps --enable-static?); skipping\n"); exit(77); } /* Startup LT */ if (lt_dlinit() != 0) { fprintf(stderr, "Failed to lt_dlinit\n"); return 1; } printf("Trying to lt_dlopen file with dladvise_local: %s\n", filename); #if OPAL_HAVE_LTDL_ADVISE if (lt_dladvise_init(&dladvise) || lt_dladvise_ext(&dladvise) || lt_dladvise_local(&dladvise)) { fprintf(stderr, "lt_dladvise failed to initialize properly\n"); return 1; } dlhandle = lt_dlopenadvise(filename, dladvise); lt_dladvise_destroy(&dladvise); #else dlhandle = lt_dlopenext(filename); #endif if (NULL != dlhandle) { lt_dlclose(dlhandle); printf("File opened with dladvise_local, all passed\n"); return 0; } printf("Failed to open with dladvise_local: %s\n", lt_dlerror()); printf("Retrying with dladvise_global\n"); #if OPAL_HAVE_LTDL_ADVISE if (lt_dladvise_init(&dladvise) || lt_dladvise_ext(&dladvise) || lt_dladvise_global(&dladvise)) { fprintf(stderr, "lt_dladvise failed to initialize properly\n"); return 1; } dlhandle = lt_dlopenadvise(filename, dladvise); lt_dladvise_destroy(&dladvise); #else dlhandle = lt_dlopenext(filename); #endif if (NULL != dlhandle) { lt_dlclose(dlhandle); printf("File opened with dladvise_global\n"); return 0; } fprintf(stderr, "File failed to open with dladvise_global: %s\n", lt_dlerror()); return 2; }