/* * call-seq: initialize(libname, libflags) * @param [String] libname name of library to open * @param [Fixnum] libflags flags for library to open * @return [FFI::DynamicLibrary] * @raise {LoadError} if +libname+ cannot be opened * A new DynamicLibrary instance. */ static VALUE library_initialize(VALUE self, VALUE libname, VALUE libflags) { Library* library; int flags; Check_Type(libflags, T_FIXNUM); Data_Get_Struct(self, Library, library); flags = libflags != Qnil ? NUM2UINT(libflags) : 0; library->handle = dl_open(libname != Qnil ? StringValueCStr(libname) : NULL, flags); if (library->handle == NULL) { char errmsg[1024]; dl_error(errmsg, sizeof(errmsg)); rb_raise(rb_eLoadError, "Could not open library '%s': %s", libname != Qnil ? StringValueCStr(libname) : "[current process]", errmsg); } #ifdef __CYGWIN__ // On Cygwin 1.7.17 "dlsym(dlopen(0,0), 'getpid')" fails. (dlerror: "No such process") // As a workaround we can use "dlsym(RTLD_DEFAULT, 'getpid')" instead. // Since 0 == RTLD_DEFAULT we won't call dl_close later. if (libname == Qnil) { dl_close(library->handle); library->handle = RTLD_DEFAULT; } #endif rb_iv_set(self, "@name", libname != Qnil ? libname : rb_str_new2("[current process]")); return self; }
task main() { dl_init("dl_log.txt", false); dl_insert_int(23); dl_append_int(24); dl_close(); }
DLLEXPORT int dlclose(void *handle) { if (handle == GLOBAL_HANDLE) return POSIX_FAILED; dl_close(handle); return POSIX_SUCCESS; }
bool DynaLoader::freeLibrary () { if ( handle ) { bool res = dl_close(handle); handle = 0; return res; } return false; }
static void library_free(Library* library) { // dlclose() on MacOS tends to segfault - avoid it #ifndef __APPLE__ if (library->handle != NULL) { dl_close(library->handle); } #endif xfree(library); }
static uae_u32 open_library (const char *name, uae_u32 min_version) { syncdivisor = (3580000.0 * CYCLE_UNIT) / (double) syncbase; for (const char *c = name; *c; c++) { if (*c == '/' || *c == '\\' || *c == ':') { return UNI_ERROR_ILLEGAL_LIBRARY_NAME; } } TCHAR *tname = au (name); write_log (_T("uni: open native library '%s'\n"), tname); TCHAR *path = get_native_library_path (tname); free (tname); if (path == NULL) { write_log(_T("uni: library not found\n")); return UNI_ERROR_LIBRARY_NOT_FOUND; } write_log (_T("uni: found library at %s - opening\n"), path); #ifdef _WIN32 void *dl = LoadLibrary (path); #else void *dl = dlopen (path, RTLD_NOW); #endif free(path); if (dl == NULL) { write_log (_T("uni: error opening library errno %d\n"), errno); return UNI_ERROR_COULD_NOT_OPEN_LIBRARY; } // FIXME: check min version set_library_globals(dl); void *function_address = dl_symbol(dl, "uni_init"); if (function_address) { int error = ((uni_init_function) function_address)(); if (error) { dl_close(dl); return error; } } struct library_data *library_data = (struct library_data *) malloc( sizeof(struct library_data)); memset(library_data, 0, sizeof(struct library_data)); library_data->dl_handle = dl; uae_u32 handle = register_handle (library_data, NULL); write_log(_T("uni: opened library %08x (%p)\n"), handle, dl); return handle; }
uae_u32 uaenative_close_library(TrapContext *context, int flags) { if (!currprefs.native_code) { return UNI_ERROR_NOT_ENABLED; } uae_u32 handle; if (flags & UNI_FLAG_COMPAT) { handle = m68k_dreg (regs, 1); } else { handle = m68k_areg (regs, 1); } struct library_data *library_data = get_library_data_from_handle (handle); if (library_data == NULL) { return UNI_ERROR_INVALID_LIBRARY; } dl_close (library_data->dl_handle); // We now "free" the library and function entries for this library. This // makes the entries available for re-use. The bad thing about this is // that it could be possible for a buggy Amiga program to call a // mismatching function if a function handle is kept after the library // is closed. for (int i = 0; i <= g_max_handle; i++) { if (g_handles[i].library == library_data) { g_handles[i].library = NULL; g_handles[i].function = NULL; } } if (library_data->thread_id) { write_log (_T("uni: signalling uaenative_thread to stop\n")); library_data->thread_stop_flag = 1; // wake up thread so it can shut down uae_sem_post(&library_data->full_count); } else { free_library_data(library_data); } return 0; }
int libload_ASL(AmplExports *ae, const char *s, int ns, int warn) { Funcadd *fa; char buf0[2048], *buf; int ns1, rc, rcnf, warned; shl_t h; size_t n, nx; nx = 0; buf = buf0; if (!Abspath(s)) { if (!GetCurrentDirectory(sizeof(buf0),buf0)) return 2; nx = strlen(buf0); } n = ns + sizeof(afdll) + nx + 3; /* +3 for inserting _32 or _64 */ if (n > sizeof(buf0)) { buf = (char*)mymalloc(n); if (nx) memcpy(buf, buf0, nx); } if (nx) buf[nx++] = SLASH; strncpy(buf+nx, s, ns); buf[nx+ns] = 0; rc = warned = 0; rcnf = warn >> 1; warn &= 1; if ((h = dl_open(ae, buf, &warned, &ns1))) { found: if (find_dlsym(fa, h, FUNCADD) || find_dlsym(fa, h, "funcadd")) { #ifdef CLOSE_AT_RESET aflibname_ASL(ae,buf,buf+nx,ns1-nx,fa,0,dl_close,h); /* -DCLOSE_AT_RESET is for use in shared */ /* libraries, such as MATLAB mex functions, */ /* that may be loaded and unloaded several */ /* times during execution of the program. */ #else aflibname_ASL(ae,buf,buf+nx,ns1-nx,fa,1,dl_close,h); #endif } else { fprintf(stderr, "Could not find funcadd in %s\n", buf); dl_close(h); rc = 3; } } else if (warn) { if (!warned) { strcpy(buf+nx+ns, afdll); if ((h = dl_open(ae, buf, &warned, &ns1))) goto found; } if (warned) rc = 2; else goto notfound; } else { notfound: rc = rcnf; if (warn) fprintf(Stderr, "Cannot find library %.*s\nor %.*s%s\n", ns, s, ns, s, afdll); } if (buf != buf0) free(buf); return rc; }
/* * Class: com_googlecode_jffi_NativeLibrary * Method: dlclose * Signature: (J)V */ JNIEXPORT void JNICALL Java_com_kenai_jffi_Foreign_dlclose(JNIEnv* env, jclass cls, jlong handle) { dl_close(j2p(handle)); }