haskell / haskell/network

MSG_CMSG_CLOEXEC for recvmsg( )

Open
#625 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Haskell
Stars
366
Forks
210
Avg merge
22h 38m
Merged PRs (30d)
5

Description

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.

```haskell
(_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
```

Contributor guide

No contributing guide indexed for this repository

Research direction

Start at the recvMsg entry point and the MsgFlag definition, then trace how recvmsg flags and received file descriptors are handled. Add support for MSG_CMSG_CLOEXEC so callers can request close-on-exec descriptors during receipt, and verify the behavior with the relevant socket tests.

Written by the indexing model from the issue text.

Assessment

Tech stack
haskell
Domain
networking
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Active
Clarity
Mostly clear
Newbie friendliness
68/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.