Search code examples
cclonefork

Simulatin vfork with clone


Is it possible to simulate the behaviour of vfrok with clone? So far I have

pid=clone(fn,cStack,SIGCHLD|CLONE_FS | CLONE_FILES | CLONE_VM | CLONE_VFORK,NULL);

but I still need to pass my own stack, so the new process is working in different stack frame but in the same address space (because of CLONE_VM), and as I understand, if I invoke vfork and don't use some function from exec, the new process is operating in the same address space as the parent, and it uses the same stack frame.

So is it possible to make a new process with clone that operates in the same address space and is using the same stack frame as the parent?


Solution

  • It's not possible without writing the function in assembler. This is a fundamental issue that cannot be "fixed"; even the vfork syscall wrapper itself must be written in assembly rather than C for most archs (any arch where the return address is stored on the stack). This is because after the child runs using the same stack as the parent, it will potentially have overwritten the return address that the function (vfork, clone, or some other wrapper) needs to return to in the parent.

    In assembler, you simply save the return address in a register before making the syscall.