title: Redirecting system() function call using LD_PRELOAD
body: <p>I want to replace the calls to system() function made by my program using LD_PRELOAD.</p>
  
  <p>So I created the following wrapper functions in a shared library for testing.</p>
  
  <pre><code>// syshook.c
  
  int system(const char * command)
  {
      printf("system() called for %s ************************************\n", command);
      return 55;
  }
  
  char * getenv (const char* name)
  {
      printf("my getenv() *********************");
      return 0;
  }
  </code></pre>
  
  <p>And compiled and linked to a shared object libsyshook.so with gcc. </p>
  
  <pre><code>gcc -Wall -fPIC -c *.c
  gcc -shared -Wl,-soname,libsyshook.so -o libsyshook.so.1.0
  ln -s libsyshook.so libsyshook.so.1.0
  </code></pre>
  
  <p>However, when I run the program with with LD_PRELOAD as shown below, my wrapper function for system() is not called, but the wrapper for getenv() called.</p>
  
  <pre><code>LD_PRELOAD="libsyshook.so" myprog
  </code></pre>
  
  <p>When I attach the debugger, I can see that system() call, calls the implementation in libpthread.so. So why is redirecting system() not working. I don't think there is any limitation on that ??</p>
  
  <p>Edit:
  My test program compiled to myprog above looks like this. Comments indicate my observations.</p>
  
  <pre><code>void TestClass::testMethod()
  {
      string cmdLine = "date";
      if (!mainWin) cmdLine = "time";
  
      int retFromSys = system(cmdLine.c_str());   // goes into libpthread when stepped in.
      cout &lt;&lt; "return from system " &lt;&lt; retFromSys &lt;&lt; endl; // prints 0, not 55  
      getenv("DEBUG_SYS");  // Wrapper function called for this. Prints "my getenv ****** ..."
  </code></pre>