Home:ALL Converter>How to check if a linux shared library has been preloaded using LD_PRELOAD

How to check if a linux shared library has been preloaded using LD_PRELOAD

Ask Time:2016-06-09T08:52:29         Author:Timo Geusch

Json Formatter

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:

  1. Should the above pattern work for a library that's been loaded using LD_PRELOAD?
  2. Is there another way to have a process determine if a specific shared library has been preloaded?

Author:Timo Geusch,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/37715245/how-to-check-if-a-linux-shared-library-has-been-preloaded-using-ld-preload
yy