ctb_workspace_ipc/
data_plane.rs

1// SPDX-License-Identifier: AGPL-3.0-or-later
2/*
3This file is part of Collective Toolbox, a database and document workspace and utilities.
4Copyright (C) 2026 Collective Toolbox Developers
5Contact: info@collectivetoolbox.com
6
7This program is free software: you can redistribute it and/or modify it under
8the terms of the GNU Affero General Public License as published by the Free
9Software Foundation, either version 3 of the License, or (at your option) any
10later version.
11
12This program is distributed in the hope that it will be useful, but WITHOUT ANY
13WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more details.
15
16You should have received a copy of the GNU Affero General Public License along
17with this program.  If not, see <https://www.gnu.org/licenses/>.
18*/
19
20//! High-throughput IPC data plane and shared memory descriptor transfer.
21
22use crate::error::Error;
23use crate::multiplex::session::Session;
24#[allow(
25    unused_imports,
26    clippy::wildcard_imports,
27    reason = "Standard workspace module prelude"
28)]
29use crate::utilities::*;
30
31pub use ctb_utilities::shared_memory;
32use ctb_utilities::shared_memory::{ProducerBlob, SharedBlobDescriptor};
33
34// ============================================================================
35// Session-layer FD transfer helpers
36// ============================================================================
37
38/// Send a blob's file descriptor via the session layer.
39///
40/// For blobs backed by `UnixFd`, this sends the FD using `SCM_RIGHTS`.
41/// For other blob types (`FilePath`, Named), this is a no-op since they don't
42/// require out-of-band FD transfer.
43///
44/// # Arguments
45/// * `session` - The session to use for FD transfer
46/// * `descriptor` - The blob descriptor containing the FD to send
47///
48/// # Returns
49/// Returns `Ok(())` on success, or an error if FD transfer fails.
50#[cfg(unix)]
51pub async fn send_blob_fd<S: Session + ?Sized>(
52    session: &S,
53    descriptor: &SharedBlobDescriptor,
54) -> Result<(), Error> {
55    match descriptor {
56        SharedBlobDescriptor::UnixFd(fd) => session.send_fd(*fd).await,
57        // No FD transfer needed for file path or named descriptors
58        SharedBlobDescriptor::FilePath(_) | SharedBlobDescriptor::Named(_) => {
59            Ok(())
60        }
61    }
62}
63
64/// Receive a blob's file descriptor via the session layer.
65///
66/// For blobs expected to be backed by `UnixFd`, this receives the FD using
67/// `SCM_RIGHTS` and returns an updated descriptor with the local FD number.
68///
69/// # Arguments
70/// * `session` - The session to use for FD transfer
71/// * `descriptor` - The original blob descriptor (FD value will be replaced)
72///
73/// # Returns
74/// Returns an updated `SharedBlobDescriptor` with the received FD, or the
75/// original descriptor unchanged for non-FD blob types.
76#[cfg(unix)]
77pub async fn recv_blob_fd<S: Session + ?Sized>(
78    session: &S,
79    descriptor: &SharedBlobDescriptor,
80) -> Result<SharedBlobDescriptor, Error> {
81    match descriptor {
82        SharedBlobDescriptor::UnixFd(_) => {
83            // The original FD number is meaningless in the receiving process;
84            // we need to receive the actual FD via SCM_RIGHTS
85            let received_fd = session.recv_fd().await?;
86            Ok(SharedBlobDescriptor::UnixFd(received_fd))
87        }
88        // No FD transfer needed for file path or named descriptors
89        SharedBlobDescriptor::FilePath(p) => {
90            Ok(SharedBlobDescriptor::FilePath(p.clone()))
91        }
92        SharedBlobDescriptor::Named(n) => {
93            Ok(SharedBlobDescriptor::Named(n.clone()))
94        }
95    }
96}
97
98/// Send a producer blob with its FD via the session layer.
99///
100/// This combines sending the blob's metadata (via control plane messages) with
101/// sending the underlying FD (via `SCM_RIGHTS` for Unix FD-backed blobs).
102///
103/// # Protocol
104/// The caller should:
105/// 1. Send a control message containing the `BlobToken` and `SharedBlobDescriptor`
106/// 2. Call this function to send the FD out-of-band (for `UnixFd` descriptors)
107///
108/// # Arguments
109/// * `session` - The session to use for FD transfer
110/// * `blob` - The producer blob to send
111///
112/// # Returns
113/// Returns `Ok(())` on success.
114#[cfg(unix)]
115pub async fn send_producer_blob_fd<S: Session>(
116    session: &S,
117    blob: &ProducerBlob,
118) -> Result<(), Error> {
119    send_blob_fd(session, &blob.descriptor).await
120}
121
122#[cfg(not(unix))]
123pub async fn send_blob_fd<S: Session + ?Sized>(
124    _session: &S,
125    _descriptor: &SharedBlobDescriptor,
126) -> Result<(), Error> {
127    Ok(())
128}
129
130#[cfg(not(unix))]
131pub async fn recv_blob_fd<S: Session + ?Sized>(
132    _session: &S,
133    descriptor: &SharedBlobDescriptor,
134) -> Result<SharedBlobDescriptor, Error> {
135    Ok(descriptor.clone())
136}
137
138#[cfg(not(unix))]
139pub async fn send_producer_blob_fd<S: Session>(
140    _session: &S,
141    _blob: &ProducerBlob,
142) -> Result<(), Error> {
143    Ok(())
144}