I'm familiar with using dlopen()
to check if a shared library has been loaded into a process using a prior call to dlopen()
without triggering a load if it isn't present, like so:
void* lib = dlopen(lib_name, RTLD_NOLOAD);
if (lib != NULL) {
...
}
I've recently tried to apply the same pattern to determine if one of a handful of shared libraries have been loaded into a process space using LD_PRELOAD. However in all the cases, the above mentioned call to dlopen()
returns NULL
.
So basically, if I start the process using this command line
LD_PRELOAD=libawesome.so ./mycoolprocess
and then run the following check in the code in mycoolprocess.c
void* has_awesome = dlopen("libawesome.so", RTLD_NOLOAD);
if (has_awesome != NULL) {
printf("libawesome is available\n");
}
the call to dlopen()
always returns NULL
no matter if the shared library has been loaded using LD_PRELOAD or not. Based on Andrew Henle's comment below I also tried calling dlopen
with the absolute path to one of the reloaded shared objects, but dlopen
in this case still returns NULL despite the shared object being preloaded.
So my question is twofold:
- Should the above pattern work for a library that's been loaded using LD_PRELOAD?
- Is there another way to have a process determine if a specific shared library has been preloaded?