Repository navigation
Tracking Issue for feature(unix_socket_ancillary_data) #76915
Copy link
Copy link
Open
Labels
A-ioArea: `std::io` (note: `std::fs`, `std::net`, `std::path` are A-filesystem, A-net, A-path)Area: `std::io` (note: `std::fs`, `std::net`, `std::path` are A-filesystem, A-net, A-path)C-tracking-issueCategory: An issue tracking the progress of sth. like the implementation of an RFCCategory: An issue tracking the progress of sth. like the implementation of an RFCF-unix_socket_ancillary_data`#![feature(unix_socket_ancillary_data)]``#![feature(unix_socket_ancillary_data)]`O-unixOperating system: Unix-likeOperating system: Unix-likeT-libsRelevant to the library team, which will review and decide on the PR/issue.Relevant to the library team, which will review and decide on the PR/issue.
Description
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
SocketAncillarystruct before stabilization to ensure it is compatible with all OSes. fuchsia,haiku,illumos,ios,macosandsolarisdoes not haveMSG_CMSG_CLOEXECconstant inlibcfuchsiaanduclibc(x86_64) does not havecmsghdrstruct inlibc- The current version of rust
libcdoes not haveucredstruct for the target OSemscripten, butemscriptenhas this struct in the standard C library
Known bugs/issues
Implementation history
- unix: Extend UnixStream and UnixDatagram to send and receive file descriptors #69864
- unix: Non-mutable bufs in send_vectored_with_ancillary_to #82589
- unix: Fix feature(unix_socket_ancillary_data) on macos and other BSDs #83374
- Add types for inspecting control messages in Unix socket ancillary data #117196
Reacted by Kevin M Granger, zopsicle, Tv, Marc-André Lureau, Raphaël Thériault, Thayne McCombs, Shell Chen, Sébastien Taylor, Renato Westphal, Max Verevkin and 11 more
Reacted by Gabriel de Perthuis, Kevin M Granger, Raphaël Thériault, Thayne McCombs, Fuu, bbb651 🇮🇱, Zorbatron, Suraj and Bryan Dela Cruz
Metadata
Metadata
Assignees
Labels
A-ioArea: `std::io` (note: `std::fs`, `std::net`, `std::path` are A-filesystem, A-net, A-path)Area: `std::io` (note: `std::fs`, `std::net`, `std::path` are A-filesystem, A-net, A-path)C-tracking-issueCategory: An issue tracking the progress of sth. like the implementation of an RFCCategory: An issue tracking the progress of sth. like the implementation of an RFCF-unix_socket_ancillary_data`#![feature(unix_socket_ancillary_data)]``#![feature(unix_socket_ancillary_data)]`O-unixOperating system: Unix-likeOperating system: Unix-likeT-libsRelevant to the library team, which will review and decide on the PR/issue.Relevant to the library team, which will review and decide on the PR/issue.
Activity
tadeokondrak commentedon Feb 25, 2021
send_vectored_with_ancillaryshould switch to non-mutable references for its parameters, sincesendmsgdoesn't write through them.#79753 changes just the
bufsparameter, 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)
LinkTed commentedon Feb 27, 2021
@tadeokondrak I open a new PR #82589.
LinkTed commentedon Mar 7, 2021
I create a new PR to send and receive TTL for UdpSocket from the ancillary interface. #82858
reyk commentedon Mar 26, 2021
Hi @LinkTed, I posted a fix in #83374 for macOS, OpenBSD, and possibly other BSDs.
Rollup merge of rust-lang#83374 - reyk:fix/bsd-ancillary, r=joshtriplett
LinkTed commentedon Apr 4, 2021
What has to be done to stabilize this API?
the8472 commentedon Apr 15, 2021
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
recvmsgandsndmsgsyscalls, we should expose them in a way that's forwards-compatible so we don't have to addrecv_vectored_with_ancillary_from_with_flagsor something like that in the future.Before stabilizing we should consider whether
recv_vectored_with_ancillary_fromshould gain another argument. I would recommend adding aReceiveOptionsargument that allows one to specify flags, similar toOpenOptions. The options could then be used to set linux'MSG_DONTWAIT,MSG_PEEKorMSG_ERRQUEUEfor example, maybe viacustom_flags()similar toOpenOptions. That way we don't have to support all OS-specific peculiarities.Or we could put the flags into
SocketAncillaryor 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
AncillaryDataenum should be marked as#[non_exhaustive]and maybe gain aUnknownvariant for things that the standard library doesn't support. And a way to get the raw ancillary message. That way one could for example decodestruct sock_extended_errin a 3rd party crate without waiting for std to implement it.LinkTed commentedon Apr 17, 2021
@the8472 Thank you for your feedback. If this PR request is #82858 merged I will start implementing your suggestions.
tv42 commentedon Jun 6, 2021
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).
LinkTed commentedon Jun 6, 2021
Hi @tv42,
what are the difference between
CMSG_SPACEandCMSG_LEN?Is there a function, which calculate the alignment for a struct to a specific address in Rust?
82 remaining items
jonleivent commentedon Sep 17, 2024
Would it be better for
SocketAncillary::newto take aMaybeUninitbuffer? I assume that the original buffer should not be read from after callingSocketAncillary::new. Also, this saves the initialization expense.the8472 commentedon Sep 17, 2024
at least sending it requires zero-initialization anyway.
jonleivent commentedon Sep 17, 2024
Is that the responsibility of the caller of, or of
SocketAncillary::newitself? 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 subsequentSocketAncillary::newcalls without resetting its contents?septatrix commentedon Sep 28, 2024
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)poscat0x04 commentedon Oct 1, 2025
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.
tmccombs commentedon Oct 1, 2025
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
eesekaj commentedon Apr 13, 2026
Hello,
I would like to submit the following proposals for your consideration:
The former:
In
SocketAncillarya pointer or reference to inner buffer should be exposed and a buffer length should be modifiable r i.efn 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
SocketAncillaryin the 3rd party code outside the STD, because at the moment I have to do bad things like:ancillary.rs
The second thing:
In
SocketAncillaryin a functionadd_fdsan argument fds accepts a slice ofRawFdas 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:
But the
into_raw_fd()function should be called inside theadd_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
Lancelotbronner commentedon May 24, 2026
What's the status of this feature, and what are the alternatives in the meantime?
psychon commentedon May 27, 2026
@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.
Lancelotbronner commentedon May 27, 2026
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?
Lancelotbronner commentedon Jul 22, 2026
What's the status on adding macOS support and standardizing this?