Right now when I try to recvmsg from a unix socket to get file descriptors, they can be inherited by child processes. The only way to prevent that is to set CLOEXEC on them, but that is later than recovering them. I would like to be able to use MSG_CMSG_CLOEXEC as a MsgFlag when calling recvmsg( ), so that the file descriptors are not prone to race conditions.
(_addr, bs, cmsgs, flags) <- recvMsg sock 4096 512 mempty
let newFds = concat [fs | c <- cmsgs, cmsgId c == CmsgIdFds, Just fs <- [decodeCmsg c :: Maybe [Fd]]]
-- If another thread spawns a process at this timing, it will inherit the open file descriptors
forM_ newFds $ \(Fd c) -> setCloseOnExecIfNeeded c
Right now when I try to recvmsg from a unix socket to get file descriptors, they can be inherited by child processes. The only way to prevent that is to set CLOEXEC on them, but that is later than recovering them. I would like to be able to use MSG_CMSG_CLOEXEC as a MsgFlag when calling recvmsg( ), so that the file descriptors are not prone to race conditions.