Skip to content

MSG_CMSG_CLOEXEC for recvmsg( ) #625

Description

@Crdr0122

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

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions