fd_utils: carry over O_CLOEXEC on duplicated FDs

Due to b/30963384, every time zygote creates a new child, zygote reopens
all existing file descriptors, taking careful measures to preserve the
file status flags, file descriptor flags, and seek offset. However, dup2
resets the sole file descriptor flag (FD_CLOEXEC) on duplication,
defeating the hard work done to preserve the flag.

From "man dup"

  NAME
    dup, dup2, dup3 - duplicate a file descriptor

  DESCRIPTION
    The dup() system call creates a copy ...
    ...
    The two file descriptors do not share file descriptor flags
    (the  close-on-exec flag).  The close-on-exec flag
    (FD_CLOEXEC; see fcntl(2)) for the duplicate descriptor is off.
    ...
    The  dup2() system call performs the same task as dup() ...

Use dup3 instead to allow us to preserve the FD_CLOEXEC status.

Bug: 120983106
Test: Android compiles and boots
Change-Id: Idbb27c83092f30d8394c254cfbdf33406f74eb94
1 file changed