title: Hack the standard function in library and call the native library function afterwards
body: <p>I am trying to hack the malloc function to call my malloc function first.Once my malloc function is executed within that, I want to invoke the standard malloc. But, I am getting a recursion, since it is just loading my defined malloc. How can i fix the below code?</p>
  
  <pre><code>#include &lt;dlfcn.h&gt;
  #include "stdio.h"
  //#include "stdlib.h"
  
  
  void *handle;
  
  void *handle_malloc;
  
  int (*loadprg)(void);
  
  void * (*malloc_sysm)(size_t);
  
  
  void init()
  {
      handle = dlopen ("/export/home/joshis1/Foxtel/temp/libloadmap.so", RTLD_LAZY);
      if( handle == NULL)
       {
         puts(dlerror());
       }
  
  
     handle_malloc = dlopen ("/lib/libc.so.6", RTLD_LAZY);
      if( handle_malloc == NULL)
       {
         puts("handle malloc error\r\n");
         puts(dlerror());
       }
  
  
  }
  
  
  #include "stdio.h"
  
  
  void *malloc(int size)
  {
     printf("called..my malloc\r\n");
  
     malloc_sysm = dlsym(handle_malloc,"malloc");
  
     if ( dlerror() != NULL)
      {
         puts("malloc symbol not found..");
         exit(1);
      }
  
  
      printf("This should call actual malloc now..\r\n");
      return  malloc_sysm(size);
  
  
  
  
  }
  
  
  int main()
  {
    int *t;
    init();
    printf("call load program now\r\n");
  
    loadprg = dlsym(handle, "loadprg");
  
    if( dlerror()!= NULL)
     {
        puts("Symbol load errror");
     }
  
    (*loadprg)();  
  
    printf("Close the handle now..\r\n");
  
    dlclose(handle);
  
  
    t = (int *) malloc (100);
  
    dlclose(handle_malloc);
  
  
  
    return 0;
  
  }
  </code></pre>
  
  <p>The output is recursion to my defined malloc(). how to fix this?</p>