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; }
static int jl_uv_dlopen(const char* filename, uv_lib_t* lib, unsigned flags) { #if defined(_OS_WINDOWS_) needsSymRefreshModuleList = 1; #endif #if defined(RTLD_GLOBAL) && defined(RTLD_LAZY) /* POSIX flags available */ dlerror(); /* Reset error status. */ lib->handle = dlopen(filename, (flags & JL_RTLD_NOW ? RTLD_NOW : RTLD_LAZY) | JL_RTLD(flags, GLOBAL) | JL_RTLD(flags, LOCAL) #ifdef RTLD_NODELETE | JL_RTLD(flags, NODELETE) #endif #ifdef RTLD_NOLOAD | JL_RTLD(flags, NOLOAD) #endif #ifdef RTLD_DEEPBIND | JL_RTLD(flags, DEEPBIND) #endif #ifdef RTLD_FIRST | JL_RTLD(flags, FIRST) #endif ); if (lib->handle) { lib->errmsg = NULL; return 0; } else { lib->errmsg = strdup(dlerror()); return -1; } #else return uv_dlopen(filename, lib); #endif }
// Load a duktape C function from a shared library by path and name. static duk_ret_t duv_loadlib(duk_context *ctx) { const char *name, *path; uv_lib_t lib; duk_c_function fn; // Check the args const duv_schema_entry schema[] = { { "path", duk_is_string }, { "name", duk_is_string }, { NULL, NULL } }; dschema_check(ctx, schema); path = duk_get_string(ctx, 0); name = duk_get_string(ctx, 1); if (uv_dlopen(path, &lib)) { duk_error(ctx, DUK_ERR_ERROR, "Cannot load shared library %s", path); return 0; } if (uv_dlsym(&lib, name, (void**)&fn)) { duk_error(ctx, DUK_ERR_ERROR, "Unable to find %s in %s", name, path); return 0; } duk_push_c_function(ctx, fn, 0); return 1; }
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; }
int jl_uv_dlopen(const char* filename, uv_lib_t* lib) { #ifdef RTLD_DEEPBIND dlerror(); /* Reset error status. */ lib->handle = dlopen(filename, RTLD_LAZY|RTLD_DEEPBIND); if (lib->handle) { lib->errmsg = NULL; return 0; } else { lib->errmsg = strdup(dlerror()); return -1; } #else return uv_dlopen(filename, lib); #endif }
mrb_value mrb_uv_dlopen(mrb_state *mrb, char const *name) { mrb_value ret = mrb_obj_value(mrb_obj_alloc(mrb, MRB_TT_DATA, mrb_class_get_under(mrb, mrb_module_get(mrb, "UV"), "DL"))); uv_lib_t *lib = (uv_lib_t*)mrb_malloc(mrb, sizeof(uv_lib_t)); int err; DATA_TYPE(ret) = &dl_type; DATA_PTR(ret) = lib; err = uv_dlopen(name, lib); if (err == -1) { mrb_raise(mrb, E_UV_ERROR, uv_dlerror(lib)); } return ret; }
bool module_load(const char *path) { Module *module; ModuleInfo *moduleInfo; module = module_new(); module->Path = StrDup(path); if(uv_dlopen(path, &module->handle) < 0) { module_free(module); return false; } if(uv_dlsym(&module->handle, "ModuleInfoPtr", (void **)&moduleInfo) < 0) { module_free(module); return false; } if(moduleInfo == NULL) { module_free(module); return false; } if(moduleInfo->Load != NULL) { moduleInfo->Load(); } vector_push_back(ModuleList, module); return true; }
void julia_init(char *imageFile) { jl_page_size = jl_getpagesize(); jl_find_stack_bottom(); jl_dl_handle = jl_load_dynamic_library(NULL, JL_RTLD_DEFAULT); #ifdef __WIN32__ uv_dlopen("ntdll.dll",jl_ntdll_handle); //bypass julia's pathchecking for system dlls uv_dlopen("Kernel32.dll",jl_kernel32_handle); uv_dlopen("msvcrt.dll",jl_crtdll_handle); uv_dlopen("Ws2_32.dll",jl_winsock_handle); _jl_exe_handle.handle = GetModuleHandleA(NULL); #endif jl_io_loop = uv_default_loop(); //this loop will internal events (spawining process etc.) init_stdio(); #if defined(__linux__) int ncores = jl_cpu_cores(); if (ncores > 1) { cpu_set_t cpumask; CPU_ZERO(&cpumask); for(int i=0; i < ncores; i++) { CPU_SET(i, &cpumask); } sched_setaffinity(0, sizeof(cpu_set_t), &cpumask); } #endif #ifdef JL_GC_MARKSWEEP jl_gc_init(); jl_gc_disable(); #endif jl_init_frontend(); jl_init_types(); jl_init_tasks(jl_stack_lo, jl_stack_hi-jl_stack_lo); jl_init_codegen(); jl_an_empty_cell = (jl_value_t*)jl_alloc_cell_1d(0); jl_init_serializer(); if (!imageFile) { jl_main_module = jl_new_module(jl_symbol("Main")); jl_main_module->parent = jl_main_module; jl_core_module = jl_new_module(jl_symbol("Core")); jl_core_module->parent = jl_main_module; jl_set_const(jl_main_module, jl_symbol("Core"), (jl_value_t*)jl_core_module); jl_module_using(jl_main_module, jl_core_module); jl_current_module = jl_core_module; jl_init_intrinsic_functions(); jl_init_primitives(); jl_load("boot.jl"); jl_get_builtin_hooks(); jl_boot_file_loaded = 1; jl_init_box_caches(); } if (imageFile) { JL_TRY { jl_restore_system_image(imageFile); } JL_CATCH { JL_PRINTF(JL_STDERR, "error during init:\n"); jl_show(jl_stderr_obj(), jl_exception_in_transit); JL_PRINTF(JL_STDOUT, "\n"); jl_exit(1); } } // set module field of primitive types int i; void **table = jl_core_module->bindings.table; for(i=1; i < jl_core_module->bindings.size; i+=2) { if (table[i] != HT_NOTFOUND) { jl_binding_t *b = (jl_binding_t*)table[i]; if (b->value && jl_is_datatype(b->value)) { jl_datatype_t *tt = (jl_datatype_t*)b->value; tt->name->module = jl_core_module; } } } // the Main module is the one which is always open, and set as the // current module for bare (non-module-wrapped) toplevel expressions. // it does "using Base" if Base is available. if (jl_base_module != NULL) { jl_add_standard_imports(jl_main_module); } // eval() uses Main by default, so Main.eval === Core.eval jl_module_import(jl_main_module, jl_core_module, jl_symbol("eval")); jl_current_module = jl_main_module; #ifndef __WIN32__ struct sigaction actf; memset(&actf, 0, sizeof(struct sigaction)); sigemptyset(&actf.sa_mask); actf.sa_handler = fpe_handler; actf.sa_flags = 0; if (sigaction(SIGFPE, &actf, NULL) < 0) { JL_PRINTF(JL_STDERR, "sigaction: %s\n", strerror(errno)); jl_exit(1); } stack_t ss; ss.ss_flags = 0; ss.ss_size = SIGSTKSZ; ss.ss_sp = malloc(ss.ss_size); if (sigaltstack(&ss, NULL) < 0) { JL_PRINTF(JL_STDERR, "sigaltstack: %s\n", strerror(errno)); jl_exit(1); } struct sigaction act; memset(&act, 0, sizeof(struct sigaction)); sigemptyset(&act.sa_mask); act.sa_sigaction = segv_handler; act.sa_flags = SA_ONSTACK | SA_SIGINFO; if (sigaction(SIGSEGV, &act, NULL) < 0) { JL_PRINTF(JL_STDERR, "sigaction: %s\n", strerror(errno)); jl_exit(1); } #else if (signal(SIGFPE, (void (__cdecl *)(int))fpe_handler) == SIG_ERR) { JL_PRINTF(JL_STDERR, "Couldn't set SIGFPE\n"); jl_exit(1); } #endif #ifdef JL_GC_MARKSWEEP jl_gc_enable(); #endif }
uv_lib_t *jl_load_dynamic_library(char *fname) { int error; char *modname, *ext; char path[PATHBUF]; int i; uv_lib_t *handle=malloc(sizeof(uv_lib_t)); handle->errmsg=NULL; modname = fname; if (modname == NULL) { #if defined(__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", fname); #else handle->handle = dlopen(NULL,RTLD_NOW); #endif goto done; } else if (modname[0] == '/') { error = uv_dlopen(modname,handle); if (!error) goto done; } char *cwd; for(i=0; i < N_EXTENSIONS; i++) { ext = extensions[i]; path[0] = '\0'; handle->handle = NULL; if (modname[0] != '/') { if (julia_home) { /* try julia_home/usr/lib */ strncpy(path, julia_home, PATHBUF-1); strncat(path, "/usr/lib/", PATHBUF-1-strlen(path)); strncat(path, modname, PATHBUF-1-strlen(path)); strncat(path, ext, PATHBUF-1-strlen(path)); error = uv_dlopen(path, handle); if (!error) goto done; // if file exists but didn't load, show error details struct stat sbuf; if (stat(path, &sbuf) != -1) { JL_PRINTF(JL_STDERR, "%d\n", error); jl_errorf("could not load module %s", fname); } } cwd = getcwd(path, PATHBUF); if (cwd != NULL) { /* next try load from current directory */ strncat(path, "/", PATHBUF-1-strlen(path)); strncat(path, modname, PATHBUF-1-strlen(path)); strncat(path, ext, PATHBUF-1-strlen(path)); error = uv_dlopen(path, handle); if (!error) goto done; } } /* try loading from standard library path */ strncpy(path, modname, PATHBUF-1); strncat(path, ext, PATHBUF-1-strlen(path)); error = uv_dlopen(path, handle); if (!error) goto done; } JL_PRINTF(JL_STDERR, "could not load module %s (%d): %s\n", fname, error, uv_dlerror(handle)); jl_errorf("could not load module %s", fname); free(handle); return NULL; done: return handle; }
// master不保存settings,直接传给plugin_init() int load_task_setting(ls_master_t* master) { LOG("load_task_setting()\n"); const char* setting_file = "task/setting.json"; char* buf; long len; FILE* f = fopen(setting_file, "r"); if (f == NULL) { LOGE("Failed to open setting_file: %s\n", setting_file);// TODO errno return -1; } fseek(f, 0, SEEK_END); len = ftell(f); rewind(f); buf = (char*) malloc(len + 1); if (NULL == buf) { LOGE("Failed to malloc for task_setting: %s\n", strerror(errno)); return -1; } len = fread(buf, 1, len, f); buf[len] = '\0'; JSONNODE* setting = json_parse(buf); master->num_plugins = json_size(setting); master->plugins = (ls_plugin_t*)malloc(master->num_plugins * sizeof(ls_plugin_t)); ls_plugin_t* plugin; size_t plugin_index = 0; char plugin_path[128]; for (JSONNODE_ITERATOR i = json_begin(setting); i != json_end(setting); ++i, ++plugin_index) { plugin = master->plugins + plugin_index; plugin->plugin_index = plugin_index; json_char* plugin_name = json_name(*i); if (NULL == plugin_name) { LOGE("Failed to get plugin_name from task_setting\n"); return -1; } snprintf(plugin_path, sizeof(plugin_path), "plugin/%s/%s.so", plugin_name, plugin_name); if (uv_dlopen(plugin_path, &plugin->plugin_lib) < 0) { LOGE(" Failed to uv_dlopen: %s\n", uv_dlerror(&plugin->plugin_lib)); return -1; } if (uv_dlsym(&(plugin->plugin_lib), "plugin_declare", (void**)&(plugin->plugin_declare)) < 0) { LOGE(" Failed to uv_dlsym\n"); return -1; } if ((plugin->plugin_declare(plugin) < 0)) { LOGE(" Failed to plugin_declare\n"); return -1; } JSONNODE* settings = *i; // if (plugin->master_init != NULL && (plugin->master_init)(master, settings) < 0) if (plugin->plugin_init != NULL && (plugin->plugin_init)(settings) < 0) { LOGE("ERROR failed to plugin_init()\n"); return -1; } } // TODO json_free return 0; }
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; }
void julia_init(char *imageFile) { jl_page_size = jl_getpagesize(); jl_find_stack_bottom(); jl_dl_handle = jl_load_dynamic_library(NULL, JL_RTLD_DEFAULT); #ifdef _OS_WINDOWS_ uv_dlopen("ntdll.dll",jl_ntdll_handle); //bypass julia's pathchecking for system dlls uv_dlopen("Kernel32.dll",jl_kernel32_handle); uv_dlopen("msvcrt.dll",jl_crtdll_handle); uv_dlopen("Ws2_32.dll",jl_winsock_handle); _jl_exe_handle.handle = GetModuleHandleA(NULL); if (!DuplicateHandle( GetCurrentProcess(), GetCurrentThread(), GetCurrentProcess(), (PHANDLE)&hMainThread, 0, TRUE, DUPLICATE_SAME_ACCESS )) { JL_PRINTF(JL_STDERR, "Couldn't access handle to main thread\n"); } #if defined(_CPU_X86_64_) SymSetOptions(SYMOPT_UNDNAME | SYMOPT_DEFERRED_LOADS); SymInitialize(GetCurrentProcess(), NULL, 1); needsSymRefreshModuleList = 0; #endif #endif jl_io_loop = uv_default_loop(); //this loop will internal events (spawining process etc.) init_stdio(); #if defined(__linux__) int ncores = jl_cpu_cores(); if (ncores > 1) { cpu_set_t cpumask; CPU_ZERO(&cpumask); for(int i=0; i < ncores; i++) { CPU_SET(i, &cpumask); } sched_setaffinity(0, sizeof(cpu_set_t), &cpumask); } #endif #ifdef JL_GC_MARKSWEEP jl_gc_init(); jl_gc_disable(); #endif jl_init_frontend(); jl_init_types(); jl_init_tasks(jl_stack_lo, jl_stack_hi-jl_stack_lo); jl_init_codegen(); jl_an_empty_cell = (jl_value_t*)jl_alloc_cell_1d(0); jl_init_serializer(); if (!imageFile) { jl_main_module = jl_new_module(jl_symbol("Main")); jl_main_module->parent = jl_main_module; jl_core_module = jl_new_module(jl_symbol("Core")); jl_core_module->parent = jl_main_module; jl_set_const(jl_main_module, jl_symbol("Core"), (jl_value_t*)jl_core_module); jl_module_using(jl_main_module, jl_core_module); jl_current_module = jl_core_module; jl_init_intrinsic_functions(); jl_init_primitives(); jl_load("boot.jl"); jl_get_builtin_hooks(); jl_boot_file_loaded = 1; jl_init_box_caches(); } if (imageFile) { JL_TRY { jl_restore_system_image(imageFile); } JL_CATCH { JL_PRINTF(JL_STDERR, "error during init:\n"); jl_show(jl_stderr_obj(), jl_exception_in_transit); JL_PRINTF(JL_STDERR, "\n"); jl_exit(1); } } // set module field of primitive types int i; void **table = jl_core_module->bindings.table; for(i=1; i < jl_core_module->bindings.size; i+=2) { if (table[i] != HT_NOTFOUND) { jl_binding_t *b = (jl_binding_t*)table[i]; if (b->value && jl_is_datatype(b->value)) { jl_datatype_t *tt = (jl_datatype_t*)b->value; tt->name->module = jl_core_module; } } } // the Main module is the one which is always open, and set as the // current module for bare (non-module-wrapped) toplevel expressions. // it does "using Base" if Base is available. if (jl_base_module != NULL) { jl_add_standard_imports(jl_main_module); } // eval() uses Main by default, so Main.eval === Core.eval jl_module_import(jl_main_module, jl_core_module, jl_symbol("eval")); jl_current_module = jl_main_module; #ifndef _OS_WINDOWS_ signal_stack = malloc(SIGSTKSZ); struct sigaction actf; memset(&actf, 0, sizeof(struct sigaction)); sigemptyset(&actf.sa_mask); actf.sa_handler = fpe_handler; actf.sa_flags = 0; if (sigaction(SIGFPE, &actf, NULL) < 0) { JL_PRINTF(JL_STDERR, "sigaction: %s\n", strerror(errno)); jl_exit(1); } #if defined(_OS_LINUX_) stack_t ss; ss.ss_flags = 0; ss.ss_size = SIGSTKSZ; ss.ss_sp = signal_stack; if (sigaltstack(&ss, NULL) < 0) { JL_PRINTF(JL_STDERR, "sigaltstack: %s\n", strerror(errno)); jl_exit(1); } struct sigaction act; memset(&act, 0, sizeof(struct sigaction)); sigemptyset(&act.sa_mask); act.sa_sigaction = segv_handler; act.sa_flags = SA_ONSTACK | SA_SIGINFO; if (sigaction(SIGSEGV, &act, NULL) < 0) { JL_PRINTF(JL_STDERR, "sigaction: %s\n", strerror(errno)); jl_exit(1); } if (signal(SIGPIPE,SIG_IGN) == SIG_ERR) { JL_PRINTF(JL_STDERR, "Couldn't set SIGPIPE\n"); jl_exit(1); } #elif defined (_OS_DARWIN_) kern_return_t ret; mach_port_t self = mach_task_self(); ret = mach_port_allocate(self,MACH_PORT_RIGHT_RECEIVE,&segv_port); HANDLE_MACH_ERROR("mach_port_allocate",ret); ret = mach_port_insert_right(self,segv_port,segv_port,MACH_MSG_TYPE_MAKE_SEND); HANDLE_MACH_ERROR("mach_port_insert_right",ret); // Alright, create a thread to serve as the listener for exceptions pthread_t thread; pthread_attr_t attr; if (pthread_attr_init(&attr) != 0) { JL_PRINTF(JL_STDERR, "pthread_attr_init failed"); jl_exit(1); } pthread_attr_setdetachstate(&attr,PTHREAD_CREATE_DETACHED); if (pthread_create(&thread,&attr,mach_segv_listener,NULL) != 0) { JL_PRINTF(JL_STDERR, "pthread_create failed"); jl_exit(1); } pthread_attr_destroy(&attr); ret = task_set_exception_ports(self,EXC_MASK_BAD_ACCESS,segv_port,EXCEPTION_DEFAULT,MACHINE_THREAD_STATE); HANDLE_MACH_ERROR("task_set_exception_ports",ret); #endif #else if (signal(SIGFPE, (void (__cdecl *)(int))fpe_handler) == SIG_ERR) { JL_PRINTF(JL_STDERR, "Couldn't set SIGFPE\n"); jl_exit(1); } #endif #ifdef JL_GC_MARKSWEEP jl_gc_enable(); #endif }