✨ Socket passing works!
This commit is contained in:
parent
606800a711
commit
dedfb57285
3 changed files with 32 additions and 5 deletions
|
|
@ -12,6 +12,7 @@ async-signals = "0.4"
|
||||||
color-eyre = "0.6"
|
color-eyre = "0.6"
|
||||||
futures-util = "0.3"
|
futures-util = "0.3"
|
||||||
libc = "0.2"
|
libc = "0.2"
|
||||||
|
nix = { version = "0.24.1", features = ["fs"], default-features = false }
|
||||||
sendfd = { version = "0.4.1", features = ["tokio"] }
|
sendfd = { version = "0.4.1", features = ["tokio"] }
|
||||||
serde = { version = "1", features = ["derive"] }
|
serde = { version = "1", features = ["derive"] }
|
||||||
serde_json = "1"
|
serde_json = "1"
|
||||||
|
|
|
||||||
28
src/comp.rs
28
src/comp.rs
|
|
@ -1,9 +1,13 @@
|
||||||
// SPDX-License-Identifier: GPL-3.0-only
|
// SPDX-License-Identifier: GPL-3.0-only
|
||||||
use crate::process::{ProcessEvent, ProcessHandler};
|
use crate::process::{ProcessEvent, ProcessHandler};
|
||||||
use color_eyre::eyre::{ContextCompat, Result, WrapErr};
|
use color_eyre::eyre::{ContextCompat, Result, WrapErr};
|
||||||
|
use nix::fcntl;
|
||||||
use sendfd::SendWithFd;
|
use sendfd::SendWithFd;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use std::{collections::HashMap, os::unix::prelude::IntoRawFd};
|
use std::{
|
||||||
|
collections::HashMap,
|
||||||
|
os::unix::prelude::{AsRawFd, IntoRawFd},
|
||||||
|
};
|
||||||
use tokio::{
|
use tokio::{
|
||||||
io::{AsyncBufReadExt, AsyncWriteExt, BufReader, Lines},
|
io::{AsyncBufReadExt, AsyncWriteExt, BufReader, Lines},
|
||||||
net::{
|
net::{
|
||||||
|
|
@ -24,6 +28,21 @@ pub enum Message {
|
||||||
NewPrivilegedClient { count: usize },
|
NewPrivilegedClient { count: usize },
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn mark_as_cloexec(stream: &UnixStream) -> Result<()> {
|
||||||
|
let raw_fd = stream.as_raw_fd();
|
||||||
|
let fd_flags = fcntl::FdFlag::from_bits(
|
||||||
|
fcntl::fcntl(raw_fd, fcntl::FcntlArg::F_GETFD)
|
||||||
|
.wrap_err("failed to get GETFD value of stream")?,
|
||||||
|
)
|
||||||
|
.wrap_err("failed to get fd flags from file")?;
|
||||||
|
fcntl::fcntl(
|
||||||
|
raw_fd,
|
||||||
|
fcntl::FcntlArg::F_SETFD(fd_flags.difference(fcntl::FdFlag::FD_CLOEXEC)),
|
||||||
|
)
|
||||||
|
.wrap_err("failed to set CLOEXEC on file")?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
pub fn create_privileged_socket(
|
pub fn create_privileged_socket(
|
||||||
sockets: &mut Vec<UnixStream>,
|
sockets: &mut Vec<UnixStream>,
|
||||||
env_vars: &[(String, String)],
|
env_vars: &[(String, String)],
|
||||||
|
|
@ -32,6 +51,7 @@ pub fn create_privileged_socket(
|
||||||
UnixStream::pair().wrap_err("failed to create socket pair")?;
|
UnixStream::pair().wrap_err("failed to create socket pair")?;
|
||||||
sockets.push(comp_socket);
|
sockets.push(comp_socket);
|
||||||
let client_fd = {
|
let client_fd = {
|
||||||
|
mark_as_cloexec(&client_socket).wrap_err("failed to mark client stream as CLOEXEC")?;
|
||||||
let std_stream = client_socket
|
let std_stream = client_socket
|
||||||
.into_std()
|
.into_std()
|
||||||
.wrap_err("failed to convert client socket to std socket")?;
|
.wrap_err("failed to convert client socket to std socket")?;
|
||||||
|
|
@ -98,6 +118,7 @@ async fn send_fd(session_tx: &mut WriteHalf<'_>, stream: Vec<UnixStream>) -> Res
|
||||||
let fds = stream
|
let fds = stream
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.map(|stream| {
|
.map(|stream| {
|
||||||
|
mark_as_cloexec(&stream).wrap_err("failed to mark stream as CLOEXEC")?;
|
||||||
let std_stream = stream
|
let std_stream = stream
|
||||||
.into_std()
|
.into_std()
|
||||||
.wrap_err("failed to convert stream to std stream")?;
|
.wrap_err("failed to convert stream to std stream")?;
|
||||||
|
|
@ -118,8 +139,10 @@ async fn send_fd(session_tx: &mut WriteHalf<'_>, stream: Vec<UnixStream>) -> Res
|
||||||
.write_all(b"\n")
|
.write_all(b"\n")
|
||||||
.await
|
.await
|
||||||
.wrap_err("failed to write newline")?;
|
.wrap_err("failed to write newline")?;
|
||||||
|
tokio::time::sleep(std::time::Duration::from_micros(100)).await;
|
||||||
let tx: &UnixStream = session_tx.as_ref();
|
let tx: &UnixStream = session_tx.as_ref();
|
||||||
tx.send_with_fd(&[], &fds).wrap_err("failed to send fd")?;
|
tx.send_with_fd(&[0], &fds).wrap_err("failed to send fd")?;
|
||||||
|
info!("sent {} fds", fds.len());
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -135,6 +158,7 @@ pub async fn run_compositor(
|
||||||
let (session_rx, mut session_tx) = session.split();
|
let (session_rx, mut session_tx) = session.split();
|
||||||
let mut session = BufReader::new(session_rx).lines();
|
let mut session = BufReader::new(session_rx).lines();
|
||||||
let comp = {
|
let comp = {
|
||||||
|
mark_as_cloexec(&comp).wrap_err("failed to mark compositor stream as CLOEXEC")?;
|
||||||
let std_stream = comp
|
let std_stream = comp
|
||||||
.into_std()
|
.into_std()
|
||||||
.wrap_err("failed to convert compositor unix stream to a standard unix stream")?;
|
.wrap_err("failed to convert compositor unix stream to a standard unix stream")?;
|
||||||
|
|
|
||||||
|
|
@ -41,9 +41,11 @@ async fn main() -> Result<()> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
let env_vars = env_rx
|
// let env_vars = env_rx
|
||||||
.await
|
// .await
|
||||||
.expect("failed to receive environmental variables");
|
// .expect("failed to receive environmental variables");
|
||||||
|
tokio::time::sleep(std::time::Duration::from_millis(100)).await;
|
||||||
|
let env_vars = Vec::new();
|
||||||
info!("got environmental variables: {:?}", env_vars);
|
info!("got environmental variables: {:?}", env_vars);
|
||||||
|
|
||||||
let mut sockets = Vec::with_capacity(2);
|
let mut sockets = Vec::with_capacity(2);
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue