I am trying to use LD_PRELOAD
on linux to wrap calls to system
function to add some preprocessing to the argument. Here's my system.cpp
:
#define _GNU_SOURCE
#include <dlfcn.h>
#include <string>
#include <iostream>
typedef int (*orig_system_type)(const char *command);
int system(const char *command)
{
std::string new_cmd = std::string("set -f;") + command;
// next line is for debuggin only
std::cout << new_cmd << std::endl;
orig_system_type orig_system;
orig_system = (orig_system_type)dlsym(RTLD_NEXT,"system");
return orig_system(new_cmd.c_str());
}
I build it with
g++ -shared -fPIC -ldl -o libsystem.so system.cpp
which produces the .so object. I then run my program with
$ LD_PRELOAD=/path/to/libsystem.so ./myprogram
I do not get any errors - but seemingly my system
function is not called. Running with LD_DEBUG=libs
, I can see that my .so is being loaded, however my system
function is not being called and the one from the standard library is called instead.
What do I need to change in code/build to get it to work?