/* ** From: paulm@solbourne.com (Paul Maybee) ** ** Here is the code to interpose a user function before a library function. ** If you place this into your own shared library (compile -pic) then this ** should work for an existing program. ** ** NOTE :- you must include library libdl.a in complation (-ldl) */ #include void * libopen(function) char *function; /* Open dynamic library and return a pointer to the function specified */ { static void *library = (void *)0; void *fptr; char *err; if( !library ) { library = dlopen("libc.so", RTLD_LAZY); /* open library */ if (!library) { err = dlerror(); if (err) printf("DL_Open (dlopen): %s\n", err); abort(); } } fptr = dlsym(library, function); /* get the function */ if (!fptr) { err = dlerror(); if (err) printf("DL_Open (dlsym): %s\n", err); abort(); } return(fptr); } #ifdef TEST #include char *getwd(name) char *name; { char *(*fptr)() = (char *(*)())libopen("getwd"); printf("Interposing my function\n"); return (*fptr)(name); /* call the real getwd */ } main() { char path[MAXPATHLEN]; (void)getwd(path); printf("path = %s\n", path); return(0); } #endif