Skip to content

Instantly share code, notes, and snippets.

@baldurk
Created April 21, 2023 16:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save baldurk/9a93d0514d4f2de40971ff119b5641e2 to your computer and use it in GitHub Desktop.
Save baldurk/9a93d0514d4f2de40971ff119b5641e2 to your computer and use it in GitHub Desktop.
#include <stdlib.h>
#include <stdio.h>
struct Global {
Global() {
printf("a %s %s\n", getenv("HOME"), getenv("BLAH"));
setenv("BLAH", "blah", 1);
printf("b %s %s\n", getenv("HOME"), getenv("BLAH"));
}
} global;
extern "C" int close(int) {
printf("close %s %s\n", getenv("HOME"), getenv("BLAH"));
return 0;
}
$ g++ lib.cpp -shared -fPIC -o libtest.so
$ LD_PRELOAD=$(pwd)/libtest.so bash -c "echo hi"
a /home/baldurk (null)
b (null) blah
close (null) blah
hi
$ LD_PRELOAD=$(pwd)/libtest.so python -c "print('hi')"
a /home/baldurk (null)
b /home/baldurk blah
close /home/baldurk blah
# more identical close prints
close /home/baldurk blah
hi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment