title: Overloading fork( )
body: <p>I have overloaded the <code>fork()</code> system call and created my own version of <code>fork()</code> using RTLD_NEXT. That is, <code>dlsym(RTLD_NEXT, fork)</code>. This will hit my version of fork. After this I want to replicate the task of actual <code>fork()</code> system call, that is, creating child process and returning the pid, and some more additional functionalities.</p>
  
  <p>I am not able to figure out how to do that. I checked the kernel source code for <code>fork()</code> (fork.c) and could not figure out much.</p>
  
  <p>Doing this:</p>
  
  <pre><code>dlsym(RTLD_NEXT,fork);  
  int fork(void) {
      int pid=_fork(); // Trying to call actual fork does not work
      return pid;
  }
  </code></pre>
  
  <p>How can I do that? Here is the link to kernel source code for fork: <a href="http://lxr.linux.no/linux+v2.6.32/kernel/fork.c#L10" rel="nofollow">http://lxr.linux.no/linux+v2.6.32/kernel/fork.c#L10</a></p>
  
  <p><strong>Edit (pulled in from comments)</strong>:</p>
  
  <p>I am working on a leak detecting tool, and this tool detects a double free when a child process deletes the memory allocated by the parent. To overcome this i will override <code>fork()</code>, and whenever there is a <code>fork()</code>, the parent's memory allocation table will be duplicated to the child.</p>