2022-06-24 12:56:38 -04:00
|
|
|
// SPDX-License-Identifier: GPL-3.0-only
|
2022-07-20 08:05:01 -07:00
|
|
|
use color_eyre::eyre::{Result, WrapErr};
|
2025-08-28 01:13:23 +02:00
|
|
|
use launch_pad::{ProcessManager, process::Process};
|
2022-06-28 13:40:16 -04:00
|
|
|
use serde::{Deserialize, Serialize};
|
2022-07-20 08:05:01 -07:00
|
|
|
use std::{collections::HashMap, os::unix::prelude::*};
|
2022-06-27 13:54:24 -04:00
|
|
|
use tokio::{
|
2025-10-20 18:59:32 -07:00
|
|
|
io::AsyncReadExt,
|
|
|
|
|
net::{UnixStream, unix::OwnedReadHalf},
|
2022-09-23 14:52:14 -04:00
|
|
|
sync::{mpsc, oneshot},
|
2022-07-12 15:57:10 -04:00
|
|
|
task::JoinHandle,
|
2022-06-27 13:54:24 -04:00
|
|
|
};
|
2022-06-24 11:43:48 -04:00
|
|
|
use tokio_util::sync::CancellationToken;
|
|
|
|
|
|
2023-10-31 11:38:33 -04:00
|
|
|
use crate::{process::mark_as_not_cloexec, service::SessionRequest};
|
2022-10-23 19:33:43 -04:00
|
|
|
|
2022-06-28 13:40:16 -04:00
|
|
|
#[derive(Debug, Serialize, Deserialize)]
|
|
|
|
|
#[serde(rename_all = "snake_case", tag = "message")]
|
|
|
|
|
pub enum Message {
|
2022-07-07 16:45:17 -04:00
|
|
|
SetEnv { variables: HashMap<String, String> },
|
2022-06-28 13:40:16 -04:00
|
|
|
}
|
|
|
|
|
|
2022-07-07 16:45:17 -04:00
|
|
|
// Cancellation safe!
|
|
|
|
|
#[derive(Default)]
|
|
|
|
|
struct IpcState {
|
2022-07-11 10:47:00 -04:00
|
|
|
env_tx: Option<oneshot::Sender<HashMap<String, String>>>,
|
2022-07-07 16:45:17 -04:00
|
|
|
length: Option<u16>,
|
|
|
|
|
bytes_read: usize,
|
|
|
|
|
buf: Vec<u8>,
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-11 10:47:00 -04:00
|
|
|
fn parse_and_handle_ipc(state: &mut IpcState) {
|
|
|
|
|
match serde_json::from_slice::<Message>(&state.buf) {
|
2022-07-07 16:45:17 -04:00
|
|
|
Ok(Message::SetEnv { variables }) => {
|
2022-07-11 10:47:00 -04:00
|
|
|
if let Some(env_tx) = state.env_tx.take() {
|
|
|
|
|
env_tx.send(variables).unwrap();
|
|
|
|
|
}
|
2022-07-07 16:45:17 -04:00
|
|
|
}
|
|
|
|
|
Err(_) => {
|
|
|
|
|
warn!(
|
|
|
|
|
"Unknown session socket message, are you using incompatible cosmic-session and \
|
|
|
|
|
cosmic-comp versions?"
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async fn receive_ipc(state: &mut IpcState, rx: &mut OwnedReadHalf) -> Result<()> {
|
2022-08-08 11:19:27 -04:00
|
|
|
// This is kind of a doozy, but this is kinda complex so it can be
|
|
|
|
|
// cancellation-safe.
|
2022-07-07 16:45:17 -04:00
|
|
|
match state.length {
|
2022-08-08 11:19:27 -04:00
|
|
|
// We already got the length, and are currently reading the message body.
|
2022-07-07 16:45:17 -04:00
|
|
|
Some(length) => {
|
|
|
|
|
let index = state.bytes_read.saturating_sub(1);
|
2022-08-08 11:19:27 -04:00
|
|
|
// Add the amount of bytes read to our state.
|
|
|
|
|
// I don't think this is entirely cancellation safe, which worries me.
|
2022-07-07 16:45:17 -04:00
|
|
|
state.bytes_read += rx
|
|
|
|
|
.read_exact(&mut state.buf[index..])
|
|
|
|
|
.await
|
|
|
|
|
.wrap_err("failed to read IPC length")?;
|
2022-08-08 11:19:27 -04:00
|
|
|
// If we've read enough bytes, parse the message.
|
2022-07-07 16:45:17 -04:00
|
|
|
if state.bytes_read >= length as usize {
|
2022-07-11 10:47:00 -04:00
|
|
|
parse_and_handle_ipc(state);
|
2022-08-08 11:19:27 -04:00
|
|
|
// Set the state back to the default "waiting for a length" mode.
|
2022-07-07 16:45:17 -04:00
|
|
|
state.length = None;
|
|
|
|
|
state.bytes_read = 0;
|
|
|
|
|
state.buf.clear();
|
|
|
|
|
}
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
None => {
|
2022-08-08 11:19:27 -04:00
|
|
|
// Resize the state buffer enough to fit a u16./
|
2022-07-07 16:45:17 -04:00
|
|
|
state.buf.resize(2, 0);
|
|
|
|
|
let index = state.bytes_read.saturating_sub(1);
|
2022-08-08 11:19:27 -04:00
|
|
|
// Read the remaining bytes of the length.
|
2022-07-07 16:45:17 -04:00
|
|
|
state.bytes_read += rx
|
|
|
|
|
.read_exact(&mut state.buf[index..])
|
|
|
|
|
.await
|
|
|
|
|
.wrap_err("failed to read IPC length")?;
|
2022-08-08 11:19:27 -04:00
|
|
|
// If we've read two bytes, then parse a native-endian u16 from them.
|
2022-07-07 16:45:17 -04:00
|
|
|
if state.bytes_read >= 2 {
|
|
|
|
|
let length = u16::from_ne_bytes(
|
|
|
|
|
state.buf[..2]
|
|
|
|
|
.try_into()
|
|
|
|
|
.wrap_err("failed to convert IPC length to u16")?,
|
|
|
|
|
);
|
2022-08-08 11:19:27 -04:00
|
|
|
// Set the state to "reading the message body" mode, as we now have the length.
|
2022-07-07 16:45:17 -04:00
|
|
|
state.length = Some(length);
|
|
|
|
|
state.bytes_read = 0;
|
|
|
|
|
state.buf.resize(length as usize, 0);
|
|
|
|
|
}
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn run_compositor(
|
2022-09-23 14:52:14 -04:00
|
|
|
process_manager: &ProcessManager,
|
2024-10-25 15:45:37 +02:00
|
|
|
exec: String,
|
|
|
|
|
args: Vec<String>,
|
2022-10-23 17:41:28 -04:00
|
|
|
_token: CancellationToken,
|
2022-07-11 10:47:00 -04:00
|
|
|
env_tx: oneshot::Sender<HashMap<String, String>>,
|
2023-10-31 11:38:33 -04:00
|
|
|
session_dbus_tx: mpsc::Sender<SessionRequest>,
|
2022-07-12 15:57:10 -04:00
|
|
|
) -> Result<JoinHandle<Result<()>>> {
|
2022-09-23 14:52:14 -04:00
|
|
|
let process_manager = process_manager.clone();
|
2022-08-08 11:19:27 -04:00
|
|
|
// Create a pair of unix sockets - one for us (session),
|
|
|
|
|
// one for the compositor (comp)
|
2022-07-07 16:45:17 -04:00
|
|
|
let (session, comp) = UnixStream::pair().wrap_err("failed to create pair of unix sockets")?;
|
2025-10-20 18:59:32 -07:00
|
|
|
let (mut session_rx, _session_tx) = session.into_split();
|
2022-08-08 11:19:27 -04:00
|
|
|
// Convert our compositor socket to a non-blocking file descriptor.
|
2022-06-27 14:17:31 -04:00
|
|
|
let comp = {
|
|
|
|
|
let std_stream = comp
|
|
|
|
|
.into_std()
|
2022-06-30 10:35:02 -04:00
|
|
|
.wrap_err("failed to convert compositor unix stream to a standard unix stream")?;
|
2022-06-27 14:17:31 -04:00
|
|
|
std_stream
|
|
|
|
|
.set_nonblocking(false)
|
2022-06-30 10:35:02 -04:00
|
|
|
.wrap_err("failed to mark compositor unix stream as blocking")?;
|
2022-08-12 09:14:26 -07:00
|
|
|
OwnedFd::from(std_stream)
|
2022-06-27 14:17:31 -04:00
|
|
|
};
|
2022-10-23 19:33:43 -04:00
|
|
|
mark_as_not_cloexec(&comp).expect("Failed to mark fd as not cloexec");
|
2023-01-13 11:55:55 -08:00
|
|
|
Ok(tokio::spawn(async move {
|
|
|
|
|
// Create a new process handler for cosmic-comp, with our compositor socket's
|
|
|
|
|
// file descriptor as the `COSMIC_SESSION_SOCK` environment variable.
|
|
|
|
|
process_manager
|
|
|
|
|
.start_process(
|
|
|
|
|
Process::new()
|
2024-10-25 15:45:37 +02:00
|
|
|
.with_executable(exec)
|
|
|
|
|
.with_args(args)
|
2023-10-31 11:38:33 -04:00
|
|
|
.with_env([("COSMIC_SESSION_SOCK", comp.as_raw_fd().to_string())])
|
|
|
|
|
.with_on_exit(move |pman, _, err_code, _will_restart| {
|
|
|
|
|
let session_dbus_tx = session_dbus_tx.clone();
|
|
|
|
|
async move {
|
|
|
|
|
pman.stop();
|
|
|
|
|
if err_code == Some(0) {
|
|
|
|
|
info!("cosmic-comp exited successfully");
|
|
|
|
|
session_dbus_tx.send(SessionRequest::Exit).await.unwrap();
|
|
|
|
|
} else if let Some(err_code) = err_code {
|
|
|
|
|
error!("cosmic-comp exited with error code {}", err_code);
|
|
|
|
|
session_dbus_tx.send(SessionRequest::Restart).await.unwrap();
|
|
|
|
|
} else {
|
|
|
|
|
warn!("cosmic-comp exited by signal");
|
|
|
|
|
session_dbus_tx.send(SessionRequest::Restart).await.unwrap();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}),
|
2023-01-13 11:55:55 -08:00
|
|
|
)
|
|
|
|
|
.await
|
|
|
|
|
.expect("failed to launch compositor");
|
|
|
|
|
// Create a new state object for IPC purposes.
|
|
|
|
|
let mut ipc_state = IpcState {
|
|
|
|
|
env_tx: Some(env_tx),
|
|
|
|
|
..IpcState::default()
|
|
|
|
|
};
|
|
|
|
|
loop {
|
|
|
|
|
tokio::select! {
|
|
|
|
|
/*
|
|
|
|
|
exit = receive_event(&mut rx) => if exit.is_none() {
|
|
|
|
|
break;
|
|
|
|
|
},
|
|
|
|
|
*/
|
|
|
|
|
// Receive IPC messages from the process,
|
|
|
|
|
// exiting the loop if IPC errors.
|
|
|
|
|
result = receive_ipc(&mut ipc_state, &mut session_rx) => if let Err(err) = result {
|
|
|
|
|
error!("failed to receive IPC: {:?}", err);
|
|
|
|
|
break;
|
|
|
|
|
},
|
2022-06-24 11:43:48 -04:00
|
|
|
}
|
|
|
|
|
}
|
2023-01-13 11:55:55 -08:00
|
|
|
Result::<()>::Ok(())
|
|
|
|
|
}))
|
2022-06-24 11:43:48 -04:00
|
|
|
}
|