2023-06-27 15:26:52 -04:00
|
|
|
use color_eyre::eyre::Context;
|
|
|
|
|
use color_eyre::Result;
|
2023-07-20 15:35:35 -04:00
|
|
|
use launch_pad::process::Process;
|
|
|
|
|
use launch_pad::ProcessKey;
|
2023-11-09 09:50:58 -05:00
|
|
|
use std::os::fd::{IntoRawFd, OwnedFd, RawFd};
|
2023-07-07 16:50:22 -04:00
|
|
|
use std::os::unix::net::UnixStream;
|
2023-07-20 15:35:35 -04:00
|
|
|
use std::sync::{Arc, Mutex};
|
2023-11-09 09:50:58 -05:00
|
|
|
use tokio::sync::mpsc;
|
2023-07-20 15:35:35 -04:00
|
|
|
use tracing::Instrument;
|
2023-06-27 15:26:52 -04:00
|
|
|
|
2023-11-09 09:50:58 -05:00
|
|
|
use crate::comp::create_privileged_socket;
|
|
|
|
|
|
2023-06-27 15:26:52 -04:00
|
|
|
pub fn create_socket() -> Result<(OwnedFd, OwnedFd)> {
|
|
|
|
|
// Create a new pair of unnamed Unix sockets
|
|
|
|
|
let (sock_1, sock_2) = UnixStream::pair().wrap_err("failed to create socket pair")?;
|
|
|
|
|
|
2023-07-07 16:50:22 -04:00
|
|
|
// Turn the sockets into non-blocking fd, which we can pass to the child
|
2023-06-27 15:26:52 -04:00
|
|
|
// process
|
2023-07-07 16:50:22 -04:00
|
|
|
sock_1
|
2023-07-20 15:35:35 -04:00
|
|
|
.set_nonblocking(true)
|
|
|
|
|
.wrap_err("failed to mark client socket as non-blocking")?;
|
2023-06-27 15:26:52 -04:00
|
|
|
|
2023-07-07 16:50:22 -04:00
|
|
|
sock_2
|
2023-07-20 15:35:35 -04:00
|
|
|
.set_nonblocking(true)
|
|
|
|
|
.wrap_err("failed to mark client socket as non-blocking")?;
|
2023-07-07 16:50:22 -04:00
|
|
|
|
|
|
|
|
Ok((OwnedFd::from(sock_1), OwnedFd::from(sock_2)))
|
2023-06-27 15:26:52 -04:00
|
|
|
}
|
2023-07-20 15:35:35 -04:00
|
|
|
|
|
|
|
|
pub fn notifications_process(
|
|
|
|
|
span: tracing::Span,
|
|
|
|
|
cmd: &'static str,
|
|
|
|
|
key: Arc<Mutex<Option<ProcessKey>>>,
|
2023-11-09 09:50:58 -05:00
|
|
|
mut env_vars: Vec<(String, String)>,
|
2023-07-20 15:35:35 -04:00
|
|
|
fd: RawFd,
|
|
|
|
|
restart_span: tracing::Span,
|
|
|
|
|
restart_cmd: &'static str,
|
|
|
|
|
restart_key: Arc<Mutex<Option<ProcessKey>>>,
|
|
|
|
|
restart_env_vars: Vec<(String, String)>,
|
|
|
|
|
restart_fd: RawFd,
|
2023-11-09 09:50:58 -05:00
|
|
|
socket_tx: mpsc::UnboundedSender<Vec<tokio::net::UnixStream>>,
|
2023-07-20 15:35:35 -04:00
|
|
|
) -> Process {
|
2023-11-09 09:50:58 -05:00
|
|
|
env_vars.retain(|v| &v.0 != "WAYLAND_SOCKET");
|
|
|
|
|
|
2023-07-20 15:35:35 -04:00
|
|
|
let stdout_span = span.clone();
|
|
|
|
|
let stderr_span = span.clone();
|
2023-11-09 09:50:58 -05:00
|
|
|
let mut sockets = Vec::with_capacity(1);
|
|
|
|
|
let (env_vars, privileged_fd) = create_privileged_socket(&mut sockets, &env_vars).unwrap();
|
|
|
|
|
_ = socket_tx.send(sockets);
|
2023-07-20 15:35:35 -04:00
|
|
|
let env_clone = env_vars.clone();
|
2023-11-09 09:50:58 -05:00
|
|
|
let socket_tx_clone = socket_tx.clone();
|
|
|
|
|
let privileged_fd = privileged_fd.into_raw_fd();
|
2023-07-20 15:35:35 -04:00
|
|
|
Process::new()
|
|
|
|
|
.with_executable(cmd)
|
2023-11-09 09:50:58 -05:00
|
|
|
.with_fds(move || vec![privileged_fd, fd])
|
2023-07-20 15:35:35 -04:00
|
|
|
.with_on_stdout(move |_, _, line| {
|
|
|
|
|
let stdout_span = stdout_span.clone();
|
|
|
|
|
async move {
|
|
|
|
|
info!("{}", line);
|
|
|
|
|
}
|
|
|
|
|
.instrument(stdout_span)
|
|
|
|
|
})
|
|
|
|
|
.with_on_stderr(move |_, _, line| {
|
|
|
|
|
let stderr_span = stderr_span.clone();
|
|
|
|
|
async move {
|
|
|
|
|
warn!("{}", line);
|
|
|
|
|
}
|
|
|
|
|
.instrument(stderr_span)
|
|
|
|
|
})
|
2023-11-09 09:50:58 -05:00
|
|
|
.with_on_exit(move |pman, my_key, _, will_restart| {
|
2023-07-20 15:35:35 -04:00
|
|
|
// force restart of notifications / panel when the other exits
|
|
|
|
|
let new_process = notifications_process(
|
|
|
|
|
restart_span.clone(),
|
|
|
|
|
restart_cmd,
|
|
|
|
|
restart_key.clone(),
|
|
|
|
|
restart_env_vars.clone(),
|
|
|
|
|
restart_fd,
|
|
|
|
|
span.clone(),
|
|
|
|
|
cmd,
|
|
|
|
|
key.clone(),
|
|
|
|
|
env_clone.clone(),
|
|
|
|
|
fd,
|
2023-11-09 09:50:58 -05:00
|
|
|
socket_tx_clone.clone(),
|
2023-07-20 15:35:35 -04:00
|
|
|
);
|
|
|
|
|
let restart_key = restart_key.clone();
|
2023-11-09 09:50:58 -05:00
|
|
|
let socket_tx_clone = socket_tx_clone.clone();
|
|
|
|
|
|
|
|
|
|
let env_clone = env_clone.clone();
|
|
|
|
|
let mut pman_clone = pman.clone();
|
2023-07-20 15:35:35 -04:00
|
|
|
async move {
|
|
|
|
|
if will_restart {
|
2023-11-09 09:50:58 -05:00
|
|
|
let mut sockets = Vec::with_capacity(1);
|
|
|
|
|
let (env_vars, new_fd) =
|
|
|
|
|
create_privileged_socket(&mut sockets, &env_clone).unwrap();
|
|
|
|
|
|
|
|
|
|
let new_fd = new_fd.into_raw_fd();
|
|
|
|
|
|
|
|
|
|
if let Err(why) = socket_tx_clone.send(sockets) {
|
|
|
|
|
error!(?why, "Failed to send the privileged socket");
|
|
|
|
|
}
|
|
|
|
|
if let Err(why) = pman_clone.update_process_env(&my_key, env_vars).await {
|
|
|
|
|
error!(?why, "Failed to update environment variables");
|
|
|
|
|
}
|
|
|
|
|
if let Err(why) = pman_clone
|
|
|
|
|
.update_process_fds(&my_key, move || vec![new_fd, fd])
|
|
|
|
|
.await
|
|
|
|
|
{
|
|
|
|
|
error!(?why, "Failed to update fds");
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-20 15:35:35 -04:00
|
|
|
let Some(old) = restart_key.lock().unwrap().clone() else {
|
2023-11-09 09:50:58 -05:00
|
|
|
error!("Couldn't stop previous invocation of {}", cmd);
|
|
|
|
|
return;
|
2023-07-20 15:35:35 -04:00
|
|
|
};
|
|
|
|
|
_ = pman.stop_process(old).await;
|
|
|
|
|
|
|
|
|
|
if let Ok(new) = pman.start(new_process).await {
|
|
|
|
|
let mut guard = restart_key.lock().unwrap();
|
|
|
|
|
*guard = Some(new);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
.with_env(env_vars)
|
|
|
|
|
}
|