Skip to main content

vox_types/
fd.rs

1//! `Fd` — a file descriptor that travels across a vox connection.
2//!
3//! On the wire an [`Fd`] is encoded as a small varint *index* into a
4//! per-message fd table; the descriptors themselves travel out-of-band in
5//! `SCM_RIGHTS` ancillary data on a Unix-domain socket. This mirrors the
6//! `Tx<T>` → [`ChannelId`](crate::ChannelId) indirection: the bytes on the
7//! wire are just an index, the real resource is carried out-of-band and
8//! re-associated at the peer.
9//!
10//! The send/recv side-channel is plumbed through two thread-locals
11//! ([`collect_fds`] / [`provide_fds`]), installed around (de)serialization —
12//! exactly the shape of the channel binder in [`mod@crate::channel`].
13//!
14//! [`Fd`] itself is Unix-only (codegen refuses it for non-local / non-Rust
15//! targets). [`FrameFds`], [`collect_fds`] and [`provide_fds`] are portable
16//! — on non-Unix targets `FrameFds` is `()` and the helpers are pass-throughs
17//! — so the message-routing rail and generated client code need no `cfg`.
18
19/// The descriptors carried with one frame. `Vec<OwnedFd>` on Unix; `()`
20/// elsewhere (no transport can pass descriptors off-Unix).
21#[cfg(unix)]
22pub type FrameFds = Vec<std::os::fd::OwnedFd>;
23/// The descriptors carried with one frame (none off-Unix).
24#[cfg(not(unix))]
25pub type FrameFds = ();
26
27// ===========================================================================
28// Non-Unix: portable no-op surface.
29// ===========================================================================
30
31#[cfg(not(unix))]
32mod portable {
33    /// No collector off-Unix: run `f`, gather nothing.
34    pub fn collect_fds<R>(f: impl FnOnce() -> R) -> (R, super::FrameFds) {
35        (f(), ())
36    }
37
38    /// No source off-Unix: descriptors cannot arrive, just run `f`.
39    pub fn provide_fds<R>(_fds: super::FrameFds, f: impl FnOnce() -> R) -> R {
40        f()
41    }
42}
43
44#[cfg(not(unix))]
45pub use portable::{collect_fds, provide_fds};
46
47/// Number of descriptors in a frame's fd set (0 off-Unix).
48#[cfg(unix)]
49pub fn frame_fds_len(fds: &FrameFds) -> usize {
50    fds.len()
51}
52/// Number of descriptors in a frame's fd set (0 off-Unix).
53#[cfg(not(unix))]
54pub fn frame_fds_len(_fds: &FrameFds) -> usize {
55    0
56}
57
58// ===========================================================================
59// Unix: the real implementation.
60// ===========================================================================
61
62#[cfg(unix)]
63mod unix {
64    use std::cell::{Cell, RefCell};
65    use std::os::fd::{AsFd, AsRawFd, BorrowedFd, IntoRawFd, OwnedFd, RawFd};
66
67    use facet::{Facet, FacetOpaqueAdapter, OpaqueDeserialize, OpaqueSerialize, PtrConst};
68
69    use super::FrameFds;
70
71    /// Maximum number of descriptors carried in a single `SCM_RIGHTS` control
72    /// message. The kernel hard-caps this (`SCM_MAX_FD`); we surface a clean
73    /// error rather than silently chunking across `sendmsg` calls.
74    pub const SCM_MAX_FD: usize = 253;
75
76    /// `wire_index` sentinel: this `Fd` has not been pushed into a collector.
77    /// Also the value encoded when an outgoing `Fd` carries no descriptor, so
78    /// the peer's `take_fd` fails cleanly instead of the encoder aborting
79    /// across `extern "C"`.
80    const NOT_COLLECTED: u32 = u32::MAX;
81
82    /// A file descriptor that can be sent across a vox connection.
83    ///
84    /// Construct one with [`Fd::new`] from anything that owns a descriptor
85    /// (`OwnedFd`, `File`, `UnixStream`, …). After the value has been
86    /// received on the far side, take ownership with [`Fd::into_owned_fd`]
87    /// or borrow it with [`Fd::as_raw_fd`].
88    ///
89    /// Serializing an `Fd` *duplicates* its descriptor into the transport's
90    /// `SCM_RIGHTS` batch (the source `Fd` keeps ownership), so a response
91    /// may be encoded more than once — the operation store's replay-seal
92    /// pass and the wire pass — and the encoder's size/write double-call is
93    /// deduped by the `Fd` value's address.
94    #[derive(Facet)]
95    #[facet(opaque = FdAdapter, traits(Debug))]
96    pub struct Fd {
97        /// The descriptor. `Some` for an outgoing `Fd`; `Some` for an
98        /// incoming `Fd` built by `deserialize_build`; `None` once taken.
99        inner: Cell<Option<OwnedFd>>,
100        /// Scratch the adapter points `OpaqueSerialize` at. Holds
101        /// `NOT_COLLECTED` until the first `serialize_map`, then the index
102        /// assigned by the send collector. Serialized as a postcard varint.
103        wire_index: Cell<u32>,
104    }
105
106    impl Fd {
107        /// Wrap an owned descriptor for sending.
108        pub fn new(fd: impl Into<OwnedFd>) -> Self {
109            Self {
110                inner: Cell::new(Some(fd.into())),
111                wire_index: Cell::new(NOT_COLLECTED),
112            }
113        }
114
115        /// Borrow the raw descriptor without taking ownership. `None` if it
116        /// has already been taken.
117        pub fn as_raw_fd(&self) -> Option<RawFd> {
118            let taken = self.inner.take();
119            let raw = taken.as_ref().map(|f| f.as_raw_fd());
120            self.inner.set(taken);
121            raw
122        }
123
124        /// Take the owned descriptor out of this `Fd`.
125        pub fn into_owned_fd(self) -> Option<OwnedFd> {
126            self.inner.take()
127        }
128
129        /// Take the descriptor as a raw, *owned* `RawFd`. The caller is
130        /// responsible for closing it.
131        pub fn into_raw_fd(self) -> Option<RawFd> {
132            self.inner.take().map(IntoRawFd::into_raw_fd)
133        }
134    }
135
136    impl std::fmt::Debug for Fd {
137        fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
138            match self.as_raw_fd() {
139                Some(raw) => f.debug_tuple("Fd").field(&raw).finish(),
140                None => f.debug_tuple("Fd").field(&"<consumed>").finish(),
141            }
142        }
143    }
144
145    // SAFETY: `Cell<Option<OwnedFd>>` / `Cell<u32>` are `Send` (contents are
146    // `Send`); `Fd` is moved, never shared, so `!Sync` is fine — matching
147    // `Payload`'s send-only stance.
148    unsafe impl Send for Fd {}
149
150    // -----------------------------------------------------------------------
151    // Thread-local fd side-channel — same install-around-(de)serialize shape
152    // as `CHANNEL_BINDER` (`channel.rs`).
153    // -----------------------------------------------------------------------
154
155    /// Descriptors gathered while encoding one message, with a dedup map so
156    /// the encoder's size pass + write pass (which both run `serialize_map`
157    /// for the same value) duplicate each descriptor exactly once.
158    struct FdCollector {
159        fds: Vec<OwnedFd>,
160        /// `Fd` value address → assigned index, scoped to this collector.
161        seen: std::collections::HashMap<usize, u32>,
162    }
163
164    std::thread_local! {
165        static FD_COLLECTOR: RefCell<Option<FdCollector>> = const { RefCell::new(None) };
166        static FD_SOURCE: RefCell<Option<Vec<Option<OwnedFd>>>> = const { RefCell::new(None) };
167    }
168
169    /// Install an empty fd collector for the duration of `f`, returning what
170    /// `f` produced together with the descriptors it gathered.
171    ///
172    /// Descriptors are *duplicated* into the collector, so the source `Fd`
173    /// keeps ownership and the same response can be encoded more than once
174    /// (the operation store's replay-seal pass and the wire pass).
175    pub fn collect_fds<R>(f: impl FnOnce() -> R) -> (R, FrameFds) {
176        struct Restore(Option<FdCollector>);
177        impl Drop for Restore {
178            fn drop(&mut self) {
179                FD_COLLECTOR.with(|c| *c.borrow_mut() = self.0.take());
180            }
181        }
182        let fresh = FdCollector {
183            fds: Vec::new(),
184            seen: std::collections::HashMap::new(),
185        };
186        let _restore = Restore(FD_COLLECTOR.with(|c| c.borrow_mut().replace(fresh)));
187        let out = f();
188        let fds = FD_COLLECTOR
189            .with(|c| c.borrow_mut().as_mut().map(|col| std::mem::take(&mut col.fds)))
190            .unwrap_or_default();
191        (out, fds)
192    }
193
194    /// Provide the descriptors received with a frame for the duration of `f`
195    /// (typed payload decoding). Each [`Fd`] decoded inside claims one by
196    /// index.
197    pub fn provide_fds<R>(fds: FrameFds, f: impl FnOnce() -> R) -> R {
198        struct Restore(Option<Vec<Option<OwnedFd>>>);
199        impl Drop for Restore {
200            fn drop(&mut self) {
201                FD_SOURCE.with(|c| *c.borrow_mut() = self.0.take());
202            }
203        }
204        let slots = fds.into_iter().map(Some).collect();
205        let _restore = Restore(FD_SOURCE.with(|c| c.borrow_mut().replace(slots)));
206        f()
207    }
208
209    /// Duplicate `fd` into the active collector, returning its stable index.
210    ///
211    /// `key` is the source `Fd`'s value address: repeated calls for the same
212    /// value within one collector (size pass then write pass) return the
213    /// same index and duplicate the descriptor only once. Returns
214    /// `NOT_COLLECTED` — never panics — when no collector is installed (e.g.
215    /// the operation store's seal pre-encode: an fd response is inherently
216    /// non-replayable) or if `dup` fails; a panic here would abort the
217    /// process across the `extern "C"` encoder trampolines.
218    fn collect_fd(key: usize, fd: BorrowedFd<'_>) -> u32 {
219        FD_COLLECTOR.with(|c| {
220            let mut slot = c.borrow_mut();
221            let Some(col) = slot.as_mut() else {
222                return NOT_COLLECTED;
223            };
224            if let Some(&idx) = col.seen.get(&key) {
225                return idx;
226            }
227            let Ok(dup) = fd.try_clone_to_owned() else {
228                return NOT_COLLECTED;
229            };
230            let idx = col.fds.len() as u32;
231            col.fds.push(dup);
232            col.seen.insert(key, idx);
233            idx
234        })
235    }
236
237    /// Claim descriptor `index` from the active source.
238    fn take_fd(index: u32) -> Result<OwnedFd, String> {
239        if index == NOT_COLLECTED {
240            return Err("Fd was sent without a descriptor".to_string());
241        }
242        FD_SOURCE.with(|c| {
243            let mut slot = c.borrow_mut();
244            let vec = slot
245                .as_mut()
246                .ok_or_else(|| "Fd decoded with no fd source installed".to_string())?;
247            let len = vec.len();
248            let cell = vec
249                .get_mut(index as usize)
250                .ok_or_else(|| format!("Fd wire index {index} out of range ({len})"))?;
251            cell.take()
252                .ok_or_else(|| format!("Fd wire index {index} already claimed"))
253        })
254    }
255
256    /// Adapter that bridges [`Fd`] through the opaque field contract
257    /// (modelled on `PayloadAdapter` in `message.rs`).
258    pub struct FdAdapter;
259
260    impl FacetOpaqueAdapter for FdAdapter {
261        type Error = String;
262        type SendValue<'a> = Fd;
263        type RecvValue<'de> = Fd;
264
265        fn serialize_map(value: &Self::SendValue<'_>) -> OpaqueSerialize {
266            // Borrow (don't consume): `collect_fd` dups, so the same response
267            // can be encoded more than once (seal pass + wire pass) and the
268            // size/write double-call is deduped by the `Fd` value address.
269            // `Cell` has no `&` accessor for non-`Copy` contents, so swap
270            // out and back.
271            let taken = value.inner.take();
272            let idx = match taken.as_ref() {
273                Some(owned) => collect_fd(value as *const Fd as usize, owned.as_fd()),
274                None => NOT_COLLECTED,
275            };
276            value.inner.set(taken);
277            value.wire_index.set(idx);
278            OpaqueSerialize {
279                ptr: PtrConst::new(value.wire_index.as_ptr().cast::<u8>()),
280                shape: <u32 as Facet>::SHAPE,
281            }
282        }
283
284        fn deserialize_build<'de>(
285            input: OpaqueDeserialize<'de>,
286        ) -> Result<Self::RecvValue<'de>, Self::Error> {
287            let bytes = match &input {
288                OpaqueDeserialize::Borrowed(b) => *b,
289                OpaqueDeserialize::Owned(b) => b.as_slice(),
290            };
291            let mut cursor = vox_postcard::decode::Cursor::new(bytes);
292            let index = cursor
293                .read_varint()
294                .map_err(|e| format!("Fd index varint: {e}"))? as u32;
295            let owned = take_fd(index)?;
296            Ok(Fd {
297                inner: Cell::new(Some(owned)),
298                wire_index: Cell::new(index),
299            })
300        }
301    }
302
303    #[cfg(test)]
304    mod tests {
305        use super::*;
306        use std::io::{Read, Seek, Write};
307
308        fn temp_file_with(seed: &[u8]) -> std::fs::File {
309            let mut path = std::env::temp_dir();
310            let nanos = std::time::SystemTime::now()
311                .duration_since(std::time::UNIX_EPOCH)
312                .unwrap()
313                .as_nanos();
314            path.push(format!("vox-fd-test-{}-{nanos}", std::process::id()));
315            let mut f = std::fs::OpenOptions::new()
316                .create(true)
317                .read(true)
318                .write(true)
319                .truncate(true)
320                .open(&path)
321                .unwrap();
322            let _ = std::fs::remove_file(&path);
323            f.write_all(seed).unwrap();
324            f.rewind().unwrap();
325            f
326        }
327
328        #[test]
329        fn fd_round_trips_through_postcard() {
330            let file = temp_file_with(b"vox-fd-payload");
331            let msg = Fd::new(OwnedFd::from(file));
332
333            let (bytes, collected) = collect_fds(|| vox_postcard::to_vec(&msg).expect("encode"));
334            assert_eq!(collected.len(), 1, "one fd collected");
335            assert_eq!(&bytes[..4], &1u32.to_le_bytes());
336            assert_eq!(bytes[4], 0);
337
338            let decoded: Fd = provide_fds(collected, || {
339                vox_postcard::from_slice(&bytes).expect("decode")
340            });
341
342            let mut f = std::fs::File::from(decoded.into_owned_fd().expect("owned fd"));
343            let mut got = String::new();
344            f.read_to_string(&mut got).unwrap();
345            assert_eq!(got, "vox-fd-payload");
346        }
347
348        #[test]
349        fn missing_source_is_a_clean_error() {
350            let msg = Fd::new(OwnedFd::from(temp_file_with(b"x")));
351            let (bytes, _fds) = collect_fds(|| vox_postcard::to_vec(&msg).unwrap());
352            let r = std::panic::catch_unwind(|| vox_postcard::from_slice::<Fd>(&bytes));
353            assert!(
354                r.is_err() || r.unwrap().is_err(),
355                "decoding an Fd with no source must fail"
356            );
357        }
358    }
359}
360
361#[cfg(unix)]
362pub use unix::{Fd, FdAdapter, SCM_MAX_FD, collect_fds, provide_fds};