Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug: ControlMessage Fd should be ControlMessage [Fd] (SCM_RIGHTS can contain > 1 Fd) #566

Open
Dretch opened this issue Jul 22, 2023 · 1 comment

Comments

@Dretch
Copy link
Contributor

Dretch commented Jul 22, 2023

The docs for SCM_RIGHTS suggest that multiple file descriptors can be sent in one control message:

   SCM_RIGHTS
         Send or receive a set of open file descriptors from
         another process.  The data portion contains an integer
         array of the file descriptors.

However, we have ControlMessage Fd: so only a single file descriptor can be written-to / read-from a single control message. Currently, when reading a message with more than one file descriptor, only the first one is returned and the others are ignored.

Therefore, please entertain the idea of changing ControlMessage Fd to ControlMessage [Fd]. This would be a breaking change, though.

For now I am using this workaround to read messages with > 1 Fd. Writing is less of a problem because you can often send multiple control messages, each containing a single Fd:

decodeFdCmsg :: Cmsg -> Maybe [Fd]
decodeFdCmsg (Cmsg cmsid (PS fptr off len))
  | cmsid /= CmsgIdFd = Nothing
  | len < sizeOfFd = Nothing
  | otherwise =
    unsafeDupablePerformIO $ withForeignPtr fptr $ \p0 -> do
      let p = castPtr (p0 `plusPtr` off)
          numFds = len `div` sizeOfFd
      Just <$> peekArray numFds p
  where
    sizeOfFd = sizeOf (0 :: Fd)
@kazu-yamamoto
Copy link
Collaborator

Nice catch!
We are working for the next major version.
It would take time but a PR including breaking changes is now welcome!

Dretch added a commit to Dretch/network that referenced this issue Jul 29, 2023
…askell#566)

ControlMessage has lost the Storable constraint, because it is not possible to implement Storable [Fd] because [Fd] is not fixed-size when encoded.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants