I have overloaded the fork() system call and created my own version of fork() using RTLD_NEXT. That is, dlsym(RTLD_NEXT, fork). This will hit my version of fork. After this I want to replicate the task of actual fork() system call, that is, creating child process and returning the pid, and some more additional functionalities.
I am not able to figure out how to do that. I checked the kernel source code for fork() (fork.c) and could not figure out much.
Doing this:
dlsym(RTLD_NEXT,fork);
int fork(void) {
int pid=_fork(); // Trying to call actual fork does not work
return pid;
}
How can I do that? Here is the link to kernel source code for fork: http://lxr.linux.no/linux+v2.6.32/kernel/fork.c#L10
Edit (pulled in from comments):
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 fork(), and whenever there is a fork(), the parent’s memory allocation table will be duplicated to the child.
You should be able to call the actual
forkwithsyscall(SYS_fork)after including<sys/syscall.h>. Seesyscall(2).