iconv_t dl_iconv_open(const char *tocode, const char *fromcode) { if (initialized) { if (iconvlib == NULL) return (iconv_t)-1; } else { initialized = 1; iconvlib = dlopen(ICONVLIB, RTLD_LAZY | RTLD_GLOBAL); if (iconvlib == NULL) return (iconv_t)-1; iconv_open = (iconv_open_t *)dlfunc(iconvlib, ICONV_OPEN); if (iconv_open == NULL) goto dlfunc_err; dl_iconv = (dl_iconv_t *)dlfunc(iconvlib, ICONV_ENGINE); if (dl_iconv == NULL) goto dlfunc_err; dl_iconv_close = (dl_iconv_close_t *)dlfunc(iconvlib, ICONV_CLOSE); if (dl_iconv_close == NULL) goto dlfunc_err; } return iconv_open(tocode, fromcode); dlfunc_err: dlclose(iconvlib); iconvlib = NULL; return (iconv_t)-1; }
static void * dynlib_bind_internal(uim_lisp name) { void *library; void (*dynlib_instance_init)(void); void (*dynlib_instance_quit)(void); DPRINTFN(UIM_VLEVEL_DYNLIB, (stderr, "Loading %s", REFER_C_STR(name))); library = dlopen(REFER_C_STR(name), RTLD_NOW); if (library == NULL) { uim_notify_fatal(_("dynlib: %s: Load failed."), dlerror()); return uim_scm_f(); } dynlib_instance_init = (void (*)(void))dlfunc(library, "uim_dynlib_instance_init"); dynlib_instance_quit = (void (*)(void))dlfunc(library, "uim_dynlib_instance_quit"); if (!dynlib_instance_init) { uim_notify_fatal(_("dynlib: %s: Initialization failed."), REFER_C_STR(name)); return uim_scm_f(); } DPRINTFN(UIM_VLEVEL_DYNLIB, (stderr, "Calling dynlib_instance_init() for %s.\n", REFER_C_STR(name))); (*dynlib_instance_init)(); return LIST3(MAKE_PTR(library), MAKE_FPTR(dynlib_instance_init), MAKE_FPTR(dynlib_instance_quit)); }
struct vox_module_methods* vox_load_module (const char *name, int type) { char fullpath[MAXPATHLEN]; int found = find_module (name, fullpath); if (!found) return NULL; /* * Instead of track references to modules and store pointers to methods in * the hash table, we open modules with RTLD_NODELETE, so closure only * decrements reference counter and leaves a pointer to methods valid. */ void *handle = dlopen (fullpath, RTLD_LAZY | RTLD_NODELETE); if (handle == NULL) return NULL; struct vox_module* (*module_init)() = (struct vox_module* (*)()) dlfunc (handle, "module_init"); if (module_init == NULL) { dlclose (handle); return NULL; } dlclose (handle); struct vox_module *module = module_init(); if (module->type != type) return NULL; return module->methods; }
/* Load a built-in or dynamically linked module. If the `reg_fn' * argument is non-NULL, assume a built-in module and use reg_fn to * register it. Otherwise, search for a dynamic NSS module. */ static void nss_load_module(const char *source, nss_module_register_fn reg_fn) { char buf[PATH_MAX]; ns_mod mod; nss_module_register_fn fn; memset(&mod, 0, sizeof(mod)); mod.name = strdup(source); if (mod.name == NULL) { nss_log_simple(LOG_ERR, "memory allocation failure"); return; } if (reg_fn != NULL) { /* The placeholder is required, as a NULL handle * represents an invalid module. */ mod.handle = nss_builtin_handle; fn = reg_fn; } else if (!is_dynamic()) goto fin; else { if (snprintf(buf, sizeof(buf), "nss_%s.so.%d", mod.name, NSS_MODULE_INTERFACE_VERSION) >= (int)sizeof(buf)) goto fin; mod.handle = libc_dlopen(buf, RTLD_LOCAL|RTLD_LAZY); if (mod.handle == NULL) { #ifdef _NSS_DEBUG /* This gets pretty annoying since the built-in * sources aren't modules yet. */ nss_log(LOG_DEBUG, "%s, %s", mod.name, dlerror()); #endif goto fin; } fn = (nss_module_register_fn)dlfunc(mod.handle, "nss_module_register"); if (fn == NULL) { (void)dlclose(mod.handle); mod.handle = NULL; nss_log(LOG_ERR, "%s, %s", mod.name, dlerror()); goto fin; } } mod.mtab = fn(mod.name, &mod.mtabsize, &mod.unregister); if (mod.mtab == NULL || mod.mtabsize == 0) { if (mod.handle != nss_builtin_handle) (void)dlclose(mod.handle); mod.handle = NULL; nss_log(LOG_ERR, "%s, registration failed", mod.name); goto fin; } if (mod.mtabsize > 1) qsort(mod.mtab, mod.mtabsize, sizeof(mod.mtab[0]), mtab_compare); fin: _nsmod = vector_append(&mod, _nsmod, &_nsmodsize, sizeof(*_nsmod)); vector_sort(_nsmod, _nsmodsize, sizeof(*_nsmod), string_compare); }
void lib1(void) { void(*lib1_fn)(void); lib1_fn = dlfunc(RTLD_NEXT, "lib1"); if (lib1_fn == lib1) { printf("FAIL: lib1_fn=%p matches itself lib1=%p\n", lib1_fn, &lib1); } else printf("PASS: lib1_fn=%p does not match itself lib1=%p\n", lib1_fn, &lib1); }
// // Get pointer to the object // template <typename T> T * ASLoader<T>::GetObject(CCHAR_P szLibraryName, CCHAR_P szClassName) { // Check cache HandleInfo * pHandleInfo = CheckLibraryHandle(szLibraryName); // Locad library if (pHandleInfo == NULL) { DoLoadLibrary(szLibraryName); pHandleInfo = CheckLibraryHandle(szLibraryName); } STLW::string sInitHandler(szClassName); sInitHandler.append(sLibraryPrefix); // Initialize class #if defined(linux) || defined(sun) // Linux brain-damaged stupidity void * vPtr = dlsym(pHandleInfo -> handle, sInitHandler.c_str()); InitPtr vVInitPtr; memcpy((void *)(&vVInitPtr), (void *)(&vPtr), sizeof(void *)); #else InitPtr vVInitPtr = (InitPtr)dlfunc(pHandleInfo -> handle, sInitHandler.c_str()); #endif if (vVInitPtr == NULL) { STLW::string sTMP("Cannot get magic symbol \""); sTMP.append(sInitHandler); sTMP.append("\" from library \""); sTMP.append(szLibraryName); sTMP.append("\""); throw UnixException(sTMP.c_str(), errno); } // Number of references ++(pHandleInfo -> refcount); // Create object T * pASObject = ((*vVInitPtr)()); // All done if (pASObject != NULL) { return pASObject; } STLW::string sTMP("Internal error in module \""); sTMP.append(szClassName); sTMP.append("\", library \""); sTMP.append(szLibraryName); sTMP.append("\""); throw UnixException(sTMP.c_str(), errno); // Make compiler happy; this should *not* happened return NULL; }
int main (int argv, const char * argc[]) { int retCode=0; testFNPtr foofunc,foosym; foosym = (testFNPtr)dlsym(RTLD_NEXT,"foo"); if (!foosym) printf("foosym RTLD_NEXT %s\n",dlerror()); foofunc = (testFNPtr)dlfunc(RTLD_NEXT,"foo"); if (!foofunc) printf("foofunc RTLD_NEXT %s\n",dlerror()); if ((void*)foosym != (void*)foofunc) { retCode++; printf("RTLD_NEXT dlsym %x dlfunc %x\n",foosym,foofunc); } #ifdef RTLD_DEFAULT foosym = (testFNPtr)dlsym(RTLD_DEFAULT,"foo"); if (!foosym) printf("foosym RTLD_DEFAULT %s\n",dlerror()); foofunc = (testFNPtr)dlfunc(RTLD_DEFAULT,"foo"); if (!foofunc) printf("foofunc RTLD_DEFAULT %s\n",dlerror()); if ((void*)foosym != (void*)foofunc) { retCode++; printf("RTLD_DEFAULT dlsym %x dlfunc %x\n",foosym,foofunc); } #endif #ifdef RTLD_SELF foosym = (testFNPtr)dlsym(RTLD_SELF,"foo"); if (!foosym) printf("foosym RTLD_SELF %s\n",dlerror()); foofunc = (testFNPtr)dlfunc(RTLD_SELF,"foo"); if (!foofunc) printf("foofunc RTLD_SELF %s\n",dlerror()); if ((void*)foosym != (void*)foofunc) { retCode++; printf("RTLD_SELF dlsym %x dlfunc %x\n",foosym,foofunc); } #endif if (!retCode) printf("Okay, this actually works, cool, I am happy\n"); else printf("Well, confirms a bug, makes me happy I was not wrong\n"); return retCode; }
/* * Try to load a module from the suggested location. */ static pam_module_t * try_module(const char *modpath) { const pam_module_t *dlmodule; pam_module_t *module; int i, serrno; if ((module = calloc(1, sizeof *module)) == NULL || (module->path = strdup(modpath)) == NULL || (module->dlh = try_dlopen(modpath)) == NULL) goto err; dlmodule = dlsym(module->dlh, "_pam_module"); for (i = 0; i < PAM_NUM_PRIMITIVES; ++i) { if (dlmodule) { module->func[i] = dlmodule->func[i]; } else { module->func[i] = (pam_func_t)dlfunc(module->dlh, pam_sm_func_name[i]); /* * This openpam_log() call is a major source of * log spam, and the cases that matter are caught * and logged in openpam_dispatch(). This would * be less problematic if dlerror() returned an * error code so we could log an error only when * dlfunc() failed for a reason other than "no * such symbol". */ #if 0 if (module->func[i] == NULL) openpam_log(PAM_LOG_LIBDEBUG, "%s: %s(): %s", modpath, pam_sm_func_name[i], dlerror()); #endif } } return (module); err: serrno = errno; if (module != NULL) { if (module->dlh != NULL) dlclose(module->dlh); if (module->path != NULL) FREE(module->path); FREE(module); } errno = serrno; if (serrno != 0 && serrno != ENOENT) openpam_log(PAM_LOG_ERROR, "%s: %m", modpath); errno = serrno; return (NULL); }
static my_dlfunc_t load_func(const char *path, const char *name) { my_dlfunc_t f; f = (my_dlfunc_t)dlfunc(notify_dlhandle, name); if (!f) { fprintf(stderr, "uim-notify: cannot found '%s()' in %s\n", name, path); dlclose(notify_dlhandle); uim_notify_load_stderr(); return NULL; } return f; }
/** Constructor functions used to initialize the malloc implementation */ XBT_ATTRIB_CONSTRUCTOR(101) static void mm_legacy_constructor() { if (mm_initialized) return; mm_initializing = 1; __malloc_use_mmalloc = getenv(MC_ENV_VARIABLE) ? 1 : 0; if (__malloc_use_mmalloc) { __mmalloc_current_heap = mmalloc_preinit(); } else { #if HAVE_DLFUNC mm_real_realloc = (void *(*)(void *, size_t))dlfunc(RTLD_NEXT, "realloc"); mm_real_malloc = (void *(*)(size_t))dlfunc(RTLD_NEXT, "malloc"); mm_real_free = (void (*)(void *))dlfunc(RTLD_NEXT, "free"); mm_real_calloc = (void *(*)(size_t, size_t))dlfunc(RTLD_NEXT, "calloc"); #else mm_real_realloc = dlsym(RTLD_NEXT, "realloc"); mm_real_malloc = dlsym(RTLD_NEXT, "malloc"); mm_real_free = dlsym(RTLD_NEXT, "free"); mm_real_calloc = dlsym(RTLD_NEXT, "calloc"); #endif } mm_initializing = 0; mm_initialized = 1; }
/* * === FUNCTION ====================================================================== * Name: HandleInSide * Author: Chenzhi Xu * Created: 11/19/15 16:17:16 * Description: 处理内部的来报数据,发往子进程监控端的都由它来处理 * ===================================================================================== */ int HandleInSide ( CLIENT *connect ) { int sockfd=connect->fd; char recvbuf[2048]; int i,n,size; char path[256]; char ctid[128]; int wbyte; int ret; int fd; char *p; size=sizeof( recvbuf )-1; i=0; p=NULL; *recvbuf=0; while( (n = recv( sockfd, recvbuf, size, 0 )) > 0 ) { recvbuf[n]=0; if (p) { p=realloc(p,strlen(p)+n+1); } else { p=malloc(n+1); } strcpy( p+i, recvbuf ); i+=n; } connect->fd = -1; close( sockfd ); if (!i) { printf( "recv 0 byte ret=%d len=%d sockfd=%d errstr=%s\n",n, i, sockfd, strerror(errno) ); return 0; } #ifdef DEBUG sprintf( ctid, "%lu", ( unsigned long )pthread_self() ); sprintf( path, "%s", ctid ); if( (fd=open( path, O_RDWR|O_CREAT|O_TRUNC, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP )) == -1 ) { printf( "open err %s", path ); return -1; } if( ( wbyte=write( fd, p, i ) ) < i ) { close( fd ); err_sys( "recv %d write %d", n, wbyte ); } fsync( fd ); lseek( fd, 0, SEEK_SET ); close( fd ); #endif do { ret = ( int )dlfunc( "libbusiness.so", "unpack", p ); if (ret) { err_msg( "dlfunc err %d\n", ret ); sleep(1); } }while(ret<-200); free(p); p=NULL; return EXIT_SUCCESS; } /* ----- end of function handle_main ----- */
/* * === FUNCTION ====================================================================== * Name: HandleOutSide * Author: Chenzhi Xu * Created: 11/19/15 16:17:16 * Description: 处理外部的来报数据,发往父进程监控端的都由它来处理 * ===================================================================================== */ int HandleOutSide ( CLIENT *connect ) { int sockfd=connect->fd; char recvbuf[2048]; int i,n,size; char path[256]; char ctid[128]; int wbyte; int ret; int fd; char *p; size=sizeof( recvbuf )-1; i=0; p=NULL; *recvbuf=0; while( (n = recv( sockfd, recvbuf, size, 0 )) > 0 ) { recvbuf[n]=0; if (p) { p=realloc(p,strlen(p)+n+1); } else { p=malloc(n+1); } strcpy( p+i, recvbuf ); i+=n; } connect->fd = -1; close( sockfd ); if (!i) { printf( "recv 0 byte %s n=%d\n", recvbuf, n ); return 0; } #ifdef DEBUG sprintf( ctid, "%lu", ( unsigned long )pthread_self() ); sprintf( path, "%s_%d", ctid, i++ ); if( (fd=open( path, O_RDWR|O_CREAT|O_TRUNC, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP )) == -1 ) { printf( "open err %s", path ); return -1; } n=strlen(str); if( ( wbyte=write( fd, str, n ) ) < n ) { close( fd ); err_sys( "recv %d write %d", n, wbyte ); } printf("%s%s %d %d %s\n", AT, __FUNCTION__, wbyte, fd, str ); fsync( fd ); lseek( fd, 0, SEEK_SET ); close( fd ); #endif do { ret = ( int )dlfunc( "libbusiness.so", "unpack", p ); if (ret) { err_msg( "dlfunc err %d\n", ret ); sleep(1); } }while(ret<-200); free(p); p=NULL; return 0; }
/* * Scheme interfaces */ static uim_lisp notify_get_plugins_internal(void) { uim_lisp ret_; DIR *dirp; struct dirent *dp; size_t plen, slen; const uim_notify_desc *desc; void *handle; uim_notify_desc *(*desc_func)(void); const char *str; plen = sizeof(NOTIFY_PLUGIN_PREFIX); slen = sizeof(NOTIFY_PLUGIN_SUFFIX); desc = uim_notify_stderr_get_desc(); ret_ = CONS(LIST3(MAKE_SYM(desc->name), MAKE_STR(desc->name), MAKE_STR(desc->desc)), uim_scm_null()); if (getenv("UIM_DISABLE_NOTIFY") != NULL) return uim_scm_callf("reverse", "o", ret_); dirp = opendir(NOTIFY_PLUGIN_PATH); if (dirp) { while ((dp = readdir(dirp)) != NULL) { size_t len = strlen(dp->d_name); char path[PATH_MAX]; if ((len < plen + slen - 1) || (PATH_MAX < (sizeof(NOTIFY_PLUGIN_PATH "/") + len)) || (strcmp(dp->d_name, NOTIFY_PLUGIN_PREFIX) <= 0) || (strcmp(dp->d_name + len + 1 - slen, NOTIFY_PLUGIN_SUFFIX) != 0)) continue; snprintf(path, sizeof(path), "%s/%s", NOTIFY_PLUGIN_PATH, dp->d_name); handle = dlopen(path, RTLD_NOW); if ((str = dlerror()) != NULL) { fprintf(stderr, "load failed %s(%s)\n", path, str); continue; } desc_func = (uim_notify_desc *(*)(void))dlfunc(handle, "uim_notify_plugin_get_desc"); if (!desc_func) { fprintf(stderr, "cannot found 'uim_notify_get_desc()' in %s\n", path); dlclose(handle); continue; } desc = desc_func(); ret_ = CONS(LIST3(MAKE_SYM(desc->name), MAKE_STR(desc->name), MAKE_STR(desc->desc)), ret_); dlclose(handle); } (void)closedir(dirp); } return uim_scm_callf("reverse", "o", ret_); }