Skip to content

Tracking Issue for feature(unix_socket_ancillary_data) #76915

Description

@LinkTed

View all comments

Tracking issue for feature(unix_socket_ancillary_data) to extend UnixStream and UnixDatagram to send and receive ancillary data. For example file descriptors.

Unresolved Questions

  • Review the SocketAncillary struct before stabilization to ensure it is compatible with all OSes.
  • fuchsia, haiku, illumos, ios, macos and solaris does not have MSG_CMSG_CLOEXEC constant in libc
  • fuchsia and uclibc(x86_64) does not have cmsghdr struct in libc
  • The current version of rust libc does not have ucred struct for the target OS emscripten, but emscripten has this struct in the standard C library

Known bugs/issues

Implementation history

Activity

  1. added
    C-tracking-issueCategory: An issue tracking the progress of sth. like the implementation of an RFC
    on Sep 19, 2020
  2. added
    I-libs-radarLibs issues that are tracked on the team's radar.
    on Nov 6, 2020
  3. added
    A-ioArea: `std::io`, `std::fs`, `std::net` and `std::path`
    O-unixOperating system: Unix-like
    on Jan 6, 2021
  4. tadeokondrak commented on Feb 25, 2021

    @tadeokondrak
    Contributor

    send_vectored_with_ancillary should switch to non-mutable references for its parameters, since sendmsg doesn't write through them.

    #79753 changes just the bufs parameter, but it was closed for inactivity.

    (Edit: Hmm, actually changing the ancillary data too would require both a SocketAncillary and SocketAncillaryMut, like IoSlice. Probably not worth it there)

  5. LinkTed commented on Feb 27, 2021

    @LinkTed
    ContributorAuthor

    @tadeokondrak I open a new PR #82589.

  6. LinkTed commented on Mar 7, 2021

    @LinkTed
    ContributorAuthor

    I create a new PR to send and receive TTL for UdpSocket from the ancillary interface. #82858

  7. reyk commented on Mar 26, 2021

    @reyk
    Contributor

    Hi @LinkTed, I posted a fix in #83374 for macOS, OpenBSD, and possibly other BSDs.

  8. added a commit that references this issue on Mar 29, 2021
  9. LinkTed commented on Apr 4, 2021

    @LinkTed
    ContributorAuthor

    What has to be done to stabilize this API?

  10. the8472 commented on Apr 15, 2021

    @the8472
    Member

    I think the API still needs polish. All those niche socket features are tricky and right now it's probably not extensible enough for 3rd party libraries to fill platform-specific gaps such as flags or cmsgs only supported on one particular OS. These basically are wrappers for the recvmsg and sndmsg syscalls, we should expose them in a way that's forwards-compatible so we don't have to add recv_vectored_with_ancillary_from_with_flags or something like that in the future.

    Before stabilizing we should consider whether recv_vectored_with_ancillary_from should gain another argument. I would recommend adding a ReceiveOptions argument that allows one to specify flags, similar to OpenOptions. The options could then be used to set linux' MSG_DONTWAIT, MSG_PEEK or MSG_ERRQUEUE for example, maybe via custom_flags() similar to OpenOptions. That way we don't have to support all OS-specific peculiarities.

    Or we could put the flags into SocketAncillary or at least reserve the right to do that in the future, but in that case the struct would need to be renamed because it would serve several purposes at once.

    Similar could be done for sending.

    Additionally the AncillaryData enum should be marked as #[non_exhaustive] and maybe gain a Unknown variant for things that the standard library doesn't support. And a way to get the raw ancillary message. That way one could for example decode struct sock_extended_err in a 3rd party crate without waiting for std to implement it.

  11. LinkTed commented on Apr 17, 2021

    @LinkTed
    ContributorAuthor

    @the8472 Thank you for your feedback. If this PR request is #82858 merged I will start implementing your suggestions.

  12. tv42 commented on Jun 6, 2021

    @tv42
    Contributor

    What about CMSG_SPACE? Without it, we're forced to either guess or probe the appropriate buffer size.

    https://man7.org/linux/man-pages/man3/cmsg.3.html SCM_RIGHTS example shows how it's used to compute the correct buffer size (with some tricky business wrt alignment).

  13. LinkTed commented on Jun 6, 2021

    @LinkTed
    ContributorAuthor

    Hi @tv42,
    what are the difference between CMSG_SPACE and CMSG_LEN?

    Is there a function, which calculate the alignment for a struct to a specific address in Rust?

  14. 81 remaining items

  15. MaxVerevkin commented on Sep 16, 2024

    @MaxVerevkin

    Wouldn't it be better for SocketAncillary's add_creds and add_fds to return a Result<(), NotEnoughSpaceErrror> instead of a bool?

  16. jonleivent commented on Sep 17, 2024

    @jonleivent

    Would it be better for SocketAncillary::new to take a MaybeUninit buffer? I assume that the original buffer should not be read from after calling SocketAncillary::new. Also, this saves the initialization expense.

  17. the8472 commented on Sep 17, 2024

    @the8472
    Member
    When initializing a buffer that will contain a series of
                  cmsghdr structures (e.g., to be sent with [sendmsg(2)](https://man7.org/linux/man-pages/man2/sendmsg.2.html)),
                  that buffer should first be zero-initialized to ensure the
                  correct operation of CMSG_NXTHDR().
    

    at least sending it requires zero-initialization anyway.

  18. jonleivent commented on Sep 17, 2024

    @jonleivent

    that buffer should first be zero-initialized to ensure the
    correct operation of CMSG_NXTHDR().

    Is that the responsibility of the caller of, or of SocketAncillary::new itself? Because if this is a safety concern, how do you enforce it on the caller? Wouldn't a common mistake be to re-use the buffer for subsequent SocketAncillary::new calls without resetting its contents?

  19. septatrix commented on Sep 28, 2024

    @septatrix

    I see that it currently only includes SO_PASSCRED. What is the reason for that, is support for the other socket options also planned? It seems a bit arbitrary to only support one option but not the others (which are also frequently used)

  20. poscat0x04 commented on Oct 1, 2025

    @poscat0x04

    Are there any reason this is implemented only for UDS datagram sockets but not TCP/UDP sockets?
    It's a bit surprising to me that it is UDS sockets that first get their sendmsg/recvmsg operations implemented.

  21. tmccombs commented on Oct 1, 2025

    @tmccombs
    Contributor

    Because some of the most common use cases for this, such as sending file descriptors, or the "credentials" of the peer process, only works for UDS

  22. eesekaj commented on Apr 13, 2026

    @eesekaj

    Hello,
    I would like to submit the following proposals for your consideration:

    The former:
    In SocketAncillary a pointer or reference to inner buffer should be exposed and a buffer length should be modifiable r i.e
    fn get_ptr(&mut self) -> *mut u8; // or fn get_mut(&mut self) -> &mut [u8];
    and
    fn set_len(&mut self, len: usize) -> Result<()>
    Both changes will allow to use the SocketAncillary in the 3rd party code outside the STD, because at the moment I have to do bad things like:

    let anci_struct: *mut SocketAncillaryCover = ancillary as *mut _ as *mut SocketAncillaryCover;
    
      if ancillary.len() > 0
      {
          msg.msg_controllen = ancillary.len();
          msg.msg_control = unsafe { (&mut *anci_struct).buffer.as_mut_ptr().cast() };
      }

    ancillary.rs

    The second thing:
    In SocketAncillary in a function add_fds an argument fds accepts a slice of RawFd as input. In my humble (of cause) opinion, the argument should be &[OwnedFd], since once the FD(s) have been sent, it should no longer be accessible.

    At the moment, the code looks lile:

    let fds = [OwnedFd::from(a).into_raw_fd(), OwnedFd::from(b).into_raw_fd(), OwnedFd::from(aa).into_raw_fd()];
    
    ancillary.add_fds(&fds[..]);

    But the into_raw_fd() function should be called inside the add_to_ancillary_data() consuming the OwnedFd.

    I am continuing the maintenance of the crate "uds-fork" and those changes would suit the needs of the crate ancillary.rs

  23. Lancelotbronner commented on May 24, 2026

    @Lancelotbronner

    What's the status of this feature, and what are the alternatives in the meantime?

  24. psychon commented on May 27, 2026

    @psychon

    @Lancelotbronner What exactly are you looking for? I think "all of this" is already possible using libc or rustix. For example, here is some code to send and receive FDs using rustix.

  25. Lancelotbronner commented on May 27, 2026

    @Lancelotbronner

    Well for starters because the examples you've linked are a lot more code than what I got working with std, my only issue is that macOS support was removed.

    Is there a source-compatible crate?

  26. Lancelotbronner commented on Jul 22, 2026

    @Lancelotbronner

    What's the status on adding macOS support and standardizing this?

  27. added
    T-libsRelevant to the library team, which will review and decide on the PR/issue.
    and removed
    T-libs-api[DEPRECATED; DO NOT USE]
    on Aug 12, 2026
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

    A-ioArea: `std::io`, `std::fs`, `std::net` and `std::path`C-tracking-issueCategory: An issue tracking the progress of sth. like the implementation of an RFCF-unix_socket_ancillary_data`#![feature(unix_socket_ancillary_data)]`I-libs-radarLibs issues that are tracked on the team's radar.O-unixOperating system: Unix-likeT-libsRelevant to the library team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions