2016-08-01

C: How to Detect LD_PRELOAD

As I'm writing a lib to be preloaded under something heavy, I tried to detect if LD_PRELOAD is set or not. For fun.

The easiest way to check is...get the environment variable! It is so straightforward, that I feel the urge to write it down :-)

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
 char *env_val = getenv("LD_PRELOAD");
 if (env_val[0] != 0) // May be set but value is empty.
  printf("LD_PRELOAD active: \"%s\"\n", env_val);
 return 0;
}

Note, this only checks the existence of the variable, not that if it is actually loaded...

No comments :

Post a Comment