static void setup_args(void) { int r, argc; char **argv; void (*Py_GetArgcArgv)(int *argc, char ***argv); uv_lib_t dlmain; r = uv_dlopen(NULL, &dlmain); if (r != 0) { return; } r = uv_dlsym(&dlmain, "Py_GetArgcArgv", (void **)&Py_GetArgcArgv); if (r != 0) { uv_dlclose(&dlmain); return; } Py_GetArgcArgv(&argc, &argv); uv_dlclose(&dlmain); uv_setup_args(argc, argv); setup_args_called = True; }
bool hcp::Runtime::loadLibrary(char* path, hcp::tCodec * destination, const char** error) { destination->binary = uv_lib_t(); if (uv_dlopen(path, &destination->binary) != 0) { if (error) { *error = uv_dlerror(&destination->binary); } return false; } void* loadHandle = nullptr; if (uv_dlsym(&destination->binary, "hcp_GetLibrary", &loadHandle) != 0) { uv_dlclose(&destination->binary); if (error) { *error = uv_dlerror(&destination->binary); } return false; } auto handle = ((LoadCodec)loadHandle)(); if (handle == nullptr) { if (error) { *error = "Codec Library failed to load."; } return false; } char* codecName = (char*)hcp_Malloc(_state, 512); auto result = hcp_LoadCodec(_state, handle, codecName, 512); if (result != HCP_NOERROR) { if (error) { hcp_Free(_state, codecName); // this causes a memory leak *error = (const char*)hcp_Malloc(_state, 1024); hcp_GetMessage(result, (hcp_szStr)*error, 1024); } uv_dlclose(&destination->binary); return false; } destination->lib = handle; destination->path = path; destination->name = codecName; return true; }
uv_lib_t *jl_load_dynamic_library(char *modname) { int error; char *ext; char path[PATHBUF]; int i; uv_lib_t *handle=malloc(sizeof(uv_lib_t)); handle->errmsg=NULL; if (modname == NULL) { #ifdef _WIN32 if(!GetModuleHandleExA(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, (LPCSTR)(&jl_load_dynamic_library), &handle->handle)) jl_errorf("could not load base module", modname); #else handle->handle = dlopen(NULL,RTLD_NOW); #endif goto done; } #ifdef _WIN32 else if (modname[1] == ':') { #else else if (modname[0] == '/') { #endif error = jl_uv_dlopen(modname,handle); if (!error) goto done; } for(i=0; i < N_EXTENSIONS; i++) { ext = extensions[i]; path[0] = '\0'; handle->handle = NULL; /* try loading from standard library path */ snprintf(path, PATHBUF, "%s%s", modname, ext); error = jl_uv_dlopen(path, handle); if (!error) goto done; } #if defined(__linux__) char *soname = jl_lookup_soname(modname, strlen(modname)); error = (soname==NULL) || jl_uv_dlopen(soname, handle); if (!error) goto done; #endif //JL_PRINTF(JL_STDERR, "could not load module %s (%d): %s\n", modname, error, uv_dlerror(handle)); jl_errorf("could not load module %s: %s", modname, uv_dlerror(handle)); uv_dlclose(handle); free(handle); return NULL; done: return handle; } void *jl_dlsym_e(uv_lib_t *handle, char *symbol) { void *ptr; int error=uv_dlsym(handle, symbol, &ptr); if(error) ptr=NULL; return ptr; }
void mrb_uv_dlclose(mrb_state *mrb, mrb_value dl) { uv_lib_t *lib = (uv_lib_t*)mrb_uv_get_ptr(mrb, dl, &dl_type); uv_dlclose(lib); mrb_free(mrb, DATA_PTR(dl)); DATA_PTR(dl) = NULL; }
static void mrb_uv_dlfree(mrb_state *mrb, void *p) { if (p) { uv_dlclose((uv_lib_t*)p); mrb_free(mrb, p); } }
int lws_uv_plugins_destroy(struct lws_context *context) { struct lws_plugin *plugin = context->plugin_list, *p; lws_plugin_destroy_func func; char path[256]; int pofs = 0; void *v; int m; #if defined(__MINGW32__) || !defined(WIN32) pofs = 3; #endif if (!plugin) return 0; while (plugin) { p = plugin; #if !defined(WIN32) && !defined(__MINGW32__) m = lws_snprintf(path, sizeof(path) - 1, "destroy_%s", plugin->name + pofs); path[m - 3] = '\0'; #else m = lws_snprintf(path, sizeof(path) - 1, "destroy_%s", plugin->name + pofs); path[m - 4] = '\0'; #endif if (uv_dlsym(&plugin->lib, path, &v)) { uv_dlerror(&plugin->lib); lwsl_err("Failed to get %s on %s: %s", path, plugin->name, plugin->lib.errmsg); } else { func = (lws_plugin_destroy_func)v; m = func(context); if (m) lwsl_err("Destroying %s failed %d\n", plugin->name, m); } uv_dlclose(&p->lib); plugin = p->list; p->list = NULL; free(p); } context->plugin_list = NULL; while (uv_loop_close(&context->uv.loop)) ; return 0; }
LWS_VISIBLE int lws_plat_plugins_destroy(struct lws_context * context) { struct lws_plugin *plugin = context->plugin_list, *p; lws_plugin_destroy_func func; char path[256]; void *v; int m; if (!plugin) return 0; // lwsl_notice("%s\n", __func__); while (plugin) { p = plugin; #if !defined(WIN32) m = lws_snprintf(path, sizeof(path) - 1, "destroy_%s", plugin->name + 3); path[m - 3] = '\0'; #else m = lws_snprintf(path, sizeof(path) - 1, "destroy_%s", plugin->name); path[m - 4] = '\0'; #endif if (uv_dlsym(&plugin->lib, path, &v)) { uv_dlerror(&plugin->lib); lwsl_err("Failed to get init on %s: %s", plugin->name, plugin->lib.errmsg); } else { func = (lws_plugin_destroy_func)v; m = func(context); if (m) lwsl_err("Destroying %s failed %d\n", plugin->name, m); } uv_dlclose(&p->lib); plugin = p->list; p->list = NULL; free(p); } context->plugin_list = NULL; return 0; }
static uv_lib_t *jl_load_dynamic_library_(const char *modname, unsigned flags, int throw_err) { int error; char *ext; char path[PATHBUF]; int i; uv_lib_t *handle = (uv_lib_t*)malloc(sizeof(uv_lib_t)); handle->errmsg = NULL; if (modname == NULL) { #ifdef _OS_WINDOWS_ if (!GetModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, (LPCWSTR)(&jl_load_dynamic_library), &handle->handle)) jl_errorf("could not load base module", modname); #else handle->handle = dlopen(NULL,RTLD_NOW); #endif goto done; } #ifdef _OS_WINDOWS_ else if (modname[1] == ':') { #else else if (modname[0] == '/') { #endif error = jl_uv_dlopen(modname,handle,flags); if (!error) goto done; } else if (jl_base_module != NULL) { jl_array_t *DL_LOAD_PATH = (jl_array_t*)jl_get_global(jl_base_module, jl_symbol("DL_LOAD_PATH")); if (DL_LOAD_PATH != NULL) { size_t j; for (j = 0; j < jl_array_len(DL_LOAD_PATH); j++) { char *dl_path = jl_string_data(jl_cell_data(DL_LOAD_PATH)[j]); size_t len = strlen(dl_path); if (len == 0) continue; for(i=0; i < N_EXTENSIONS; i++) { ext = extensions[i]; path[0] = '\0'; handle->handle = NULL; if (dl_path[len-1] == PATHSEPSTRING[0]) snprintf(path, PATHBUF, "%s%s%s", dl_path, modname, ext); else snprintf(path, PATHBUF, "%s" PATHSEPSTRING "%s%s", dl_path, modname, ext); if (handle->errmsg) { uv_dlclose(handle); } error = jl_uv_dlopen(path, handle, flags); if (!error) goto done; } } } } for(i=0; i < N_EXTENSIONS; i++) { ext = extensions[i]; path[0] = '\0'; handle->handle = NULL; /* try loading from standard library path */ snprintf(path, PATHBUF, "%s%s", modname, ext); if (handle->errmsg) { uv_dlclose(handle); } error = jl_uv_dlopen(path, handle, flags); if (!error) goto done; } #if defined(__linux__) || defined(__FreeBSD__) { const char *soname = jl_lookup_soname(modname, strlen(modname)); if (handle->errmsg) { free(handle->errmsg); handle->errmsg = NULL; } error = (soname==NULL) || jl_uv_dlopen(soname, handle, flags); if (!error) goto done; } #endif if (throw_err) { //jl_printf(JL_STDERR, "could not load module %s (%d): %s\n", modname, error, uv_dlerror(handle)); jl_errorf("could not load module %s: %s", modname, uv_dlerror(handle)); } uv_dlclose(handle); free(handle); return NULL; done: return handle; } jl_uv_libhandle jl_load_dynamic_library_e(const char *modname, unsigned flags) { return (jl_uv_libhandle) jl_load_dynamic_library_(modname, flags, 0); } jl_uv_libhandle jl_load_dynamic_library(const char *modname, unsigned flags) { return (jl_uv_libhandle) jl_load_dynamic_library_(modname, flags, 1); } void *jl_dlsym_e(jl_uv_libhandle handle, const char *symbol) { void *ptr; int error=uv_dlsym((uv_lib_t *) handle, symbol, &ptr); if (error) ptr=NULL; return ptr; }
LWS_VISIBLE int lws_plat_plugins_init(struct lws_context * context, const char * const *d) { struct lws_plugin_capability lcaps; struct lws_plugin *plugin; lws_plugin_init_func initfunc; int m, ret = 0; void *v; uv_dirent_t dent; uv_fs_t req; char path[256]; uv_loop_t loop; uv_lib_t lib; lib.errmsg = NULL; lib.handle = NULL; uv_loop_init(&loop); lwsl_notice(" Plugins:\n"); while (d && *d) { lwsl_notice(" Scanning %s\n", *d); m =uv_fs_scandir(&loop, &req, *d, 0, NULL); if (m < 1) { lwsl_err("Scandir on %s failed\n", *d); return 1; } while (uv_fs_scandir_next(&req, &dent) != UV_EOF) { if (strlen(dent.name) < 7) continue; lwsl_notice(" %s\n", dent.name); lws_snprintf(path, sizeof(path) - 1, "%s/%s", *d, dent.name); if (uv_dlopen(path, &lib)) { uv_dlerror(&lib); lwsl_err("Error loading DSO: %s\n", lib.errmsg); goto bail; } /* we could open it, can we get his init function? */ #if !defined(WIN32) m = lws_snprintf(path, sizeof(path) - 1, "init_%s", dent.name + 3 /* snip lib... */); path[m - 3] = '\0'; /* snip the .so */ #else m = lws_snprintf(path, sizeof(path) - 1, "init_%s", dent.name); path[m - 4] = '\0'; /* snip the .dll */ #endif if (uv_dlsym(&lib, path, &v)) { uv_dlerror(&lib); lwsl_err("Failed to get init on %s: %s", dent.name, lib.errmsg); goto bail; } initfunc = (lws_plugin_init_func)v; lcaps.api_magic = LWS_PLUGIN_API_MAGIC; m = initfunc(context, &lcaps); if (m) { lwsl_err("Initializing %s failed %d\n", dent.name, m); goto skip; } plugin = lws_malloc(sizeof(*plugin)); if (!plugin) { lwsl_err("OOM\n"); goto bail; } plugin->list = context->plugin_list; context->plugin_list = plugin; strncpy(plugin->name, dent.name, sizeof(plugin->name) - 1); plugin->name[sizeof(plugin->name) - 1] = '\0'; plugin->lib = lib; plugin->caps = lcaps; context->plugin_protocol_count += lcaps.count_protocols; context->plugin_extension_count += lcaps.count_extensions; continue; skip: uv_dlclose(&lib); } bail: uv_fs_req_cleanup(&req); d++; } uv_loop_close(&loop); return ret; }
static uv_lib_t *jl_load_dynamic_library_(const char *modname, unsigned flags, int throw_err) { int error; char *ext; char path[PATHBUF]; int i; uv_stat_t stbuf; uv_lib_t *handle = (uv_lib_t*)malloc(sizeof(uv_lib_t)); handle->errmsg = NULL; /* this branch returns handle of libjulia */ if (modname == NULL) { #ifdef _OS_WINDOWS_ if (!GetModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, (LPCWSTR)(&jl_load_dynamic_library), &handle->handle)) { free(handle); jl_error("could not load base module"); } #else handle->handle = dlopen(NULL,RTLD_NOW); #endif goto done; } /* this branch shortcuts absolute paths */ #ifdef _OS_WINDOWS_ else if (modname[1] == ':') { #else else if (modname[0] == '/') { #endif error = jl_uv_dlopen(modname,handle,flags); if (!error) goto done; // bail out and show the error if file actually exists if (jl_stat(modname, (char*)&stbuf) == 0) goto notfound; if (handle->errmsg) uv_dlclose(handle); } /* this branch permutes all base paths in DL_LOAD_PATH with all extensions note: skip when !jl_base_module to avoid UndefVarError(:DL_LOAD_PATH) */ else if (jl_base_module != NULL) { jl_array_t *DL_LOAD_PATH = (jl_array_t*)jl_get_global(jl_base_module, jl_symbol("DL_LOAD_PATH")); if (DL_LOAD_PATH != NULL) { size_t j; for (j = 0; j < jl_array_len(DL_LOAD_PATH); j++) { char *dl_path = jl_string_data(jl_cell_data(DL_LOAD_PATH)[j]); size_t len = strlen(dl_path); if (len == 0) continue; for(i=0; i < N_EXTENSIONS; i++) { ext = extensions[i]; path[0] = '\0'; handle->handle = NULL; if (dl_path[len-1] == PATHSEPSTRING[0]) snprintf(path, PATHBUF, "%s%s%s", dl_path, modname, ext); else snprintf(path, PATHBUF, "%s" PATHSEPSTRING "%s%s", dl_path, modname, ext); // free handle->errmsg, else it will leak on next uv_dlopen if (handle->errmsg) uv_dlclose(handle); error = jl_uv_dlopen(path, handle, flags); if (!error) goto done; // bail out and show the error if file actually exists if (jl_stat(path, (char*)&stbuf) == 0) goto notfound; } } } } // now fall back and look in default library paths, for all extensions for(i=0; i < N_EXTENSIONS; i++) { ext = extensions[i]; path[0] = '\0'; handle->handle = NULL; snprintf(path, PATHBUF, "%s%s", modname, ext); if (handle->errmsg) uv_dlclose(handle); error = jl_uv_dlopen(path, handle, flags); if (!error) goto done; } #if defined(__linux__) || defined(__FreeBSD__) // check map of versioned libs from "libX" to full soname "libX.so.ver" { const char *soname = jl_lookup_soname(modname, strlen(modname)); error = (soname==NULL) || jl_uv_dlopen(soname, handle, flags); if (!error) goto done; } #endif notfound: // copy the error message into the path buffer so we can free the lib handle path[0] = '\0'; snprintf(path, PATHBUF, "%s", uv_dlerror(handle)); uv_dlclose(handle); free(handle); if (throw_err) jl_errorf("could not load library '%s'\n%s", modname, path); return NULL; done: return handle; } jl_uv_libhandle jl_load_dynamic_library_e(const char *modname, unsigned flags) { return (jl_uv_libhandle) jl_load_dynamic_library_(modname, flags, 0); } jl_uv_libhandle jl_load_dynamic_library(const char *modname, unsigned flags) { return (jl_uv_libhandle) jl_load_dynamic_library_(modname, flags, 1); } void *jl_dlsym_e(jl_uv_libhandle handle, const char *symbol) { void *ptr; int error = uv_dlsym((uv_lib_t *) handle, symbol, &ptr); if (error) ptr=NULL; return ptr; }