I want to vfork() a child process, but have its stdout be different than the parent's stdout.
The obvious way to achieve this with fork() would be to dup2() (and close() the original file descriptor) in the child after forking.
Let's say I have the file descriptor ready before calling vfork() and just need to call these two system calls before calling an exec*() function. Am I allowed to do that?
My answer is probably yes.
Based on Linux manual, A call to vfork() is equivalent to calling clone(2) with flags specified as:
Note from clone(2):
CLONE_FILES (since Linux 2.0) If CLONE_FILES is set, the calling process and the child process share the same file descriptor table. Any file descriptor created by the calling process or by the child process is also valid in the other process. Similarly, if one of the processes closes a file descriptor, or changes its associated flags (using the fcntl(2) F_SETFD oper‐ ation), the other process is also affected.
So after vfork, file operations in child process are isolated by default.