Don't create on-disk sockets in tests

This commit is contained in:
Ivan Molodetskikh
2025-01-27 08:28:02 +03:00
parent 902222675a
commit 0226d9aec2
4 changed files with 70 additions and 38 deletions
+34 -21
View File
@@ -1,5 +1,6 @@
use std::cell::RefCell;
use std::collections::HashSet;
use std::ffi::OsStr;
use std::os::unix::net::{UnixListener, UnixStream};
use std::path::PathBuf;
use std::rc::Rc;
@@ -33,7 +34,10 @@ use crate::window::Mapped;
const EVENT_STREAM_BUFFER_SIZE: usize = 64;
pub struct IpcServer {
pub socket_path: PathBuf,
/// Path to the IPC socket.
///
/// This is `None` when creating `IpcServer` without a socket.
pub socket_path: Option<PathBuf>,
event_streams: Rc<RefCell<Vec<EventStreamSender>>>,
event_stream_state: Rc<RefCell<EventStreamState>>,
}
@@ -60,31 +64,38 @@ struct EventStreamSender {
impl IpcServer {
pub fn start(
event_loop: &LoopHandle<'static, State>,
wayland_socket_name: &str,
wayland_socket_name: Option<&OsStr>,
) -> anyhow::Result<Self> {
let _span = tracy_client::span!("Ipc::start");
let socket_name = format!("niri.{wayland_socket_name}.{}.sock", process::id());
let mut socket_path = socket_dir();
socket_path.push(socket_name);
let socket_path = if let Some(wayland_socket_name) = wayland_socket_name {
let wayland_socket_name = wayland_socket_name.to_string_lossy();
let socket_name = format!("niri.{wayland_socket_name}.{}.sock", process::id());
let mut socket_path = socket_dir();
socket_path.push(socket_name);
let listener = UnixListener::bind(&socket_path).context("error binding socket")?;
listener
.set_nonblocking(true)
.context("error setting socket to non-blocking")?;
let listener = UnixListener::bind(&socket_path).context("error binding socket")?;
listener
.set_nonblocking(true)
.context("error setting socket to non-blocking")?;
let source = Generic::new(listener, Interest::READ, Mode::Level);
event_loop
.insert_source(source, |_, socket, state| {
match socket.accept() {
Ok((stream, _)) => on_new_ipc_client(state, stream),
Err(e) if e.kind() == io::ErrorKind::WouldBlock => (),
Err(e) => return Err(e),
}
let source = Generic::new(listener, Interest::READ, Mode::Level);
event_loop
.insert_source(source, |_, socket, state| {
match socket.accept() {
Ok((stream, _)) => on_new_ipc_client(state, stream),
Err(e) if e.kind() == io::ErrorKind::WouldBlock => (),
Err(e) => return Err(e),
}
Ok(PostAction::Continue)
})
.unwrap();
Ok(PostAction::Continue)
})
.unwrap();
Some(socket_path)
} else {
None
};
Ok(Self {
socket_path,
@@ -119,7 +130,9 @@ impl IpcServer {
impl Drop for IpcServer {
fn drop(&mut self) {
let _ = unlink(&self.socket_path);
if let Some(socket_path) = &self.socket_path {
let _ = unlink(socket_path);
}
}
}
+5 -3
View File
@@ -181,11 +181,12 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
event_loop.get_signal(),
display,
false,
true,
)
.unwrap();
// Set WAYLAND_DISPLAY for children.
let socket_name = &state.niri.socket_name;
let socket_name = state.niri.socket_name.as_deref().unwrap();
env::set_var("WAYLAND_DISPLAY", socket_name);
info!(
"listening on Wayland socket: {}",
@@ -194,8 +195,9 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
// Set NIRI_SOCKET for children.
if let Some(ipc) = &state.niri.ipc_server {
env::set_var(SOCKET_PATH_ENV, &ipc.socket_path);
info!("IPC listening on: {}", ipc.socket_path.to_string_lossy());
let socket_path = ipc.socket_path.as_deref().unwrap();
env::set_var(SOCKET_PATH_ENV, socket_path);
info!("IPC listening on: {}", socket_path.to_string_lossy());
}
if cli.session {
+30 -14
View File
@@ -181,7 +181,11 @@ pub struct Niri {
pub scheduler: Scheduler<()>,
pub stop_signal: LoopSignal,
pub display_handle: DisplayHandle,
pub socket_name: OsString,
/// Name of the Wayland socket.
///
/// This is `None` when creating `Niri` without a Wayland socket.
pub socket_name: Option<OsString>,
pub start_time: Instant,
@@ -550,6 +554,7 @@ impl State {
stop_signal: LoopSignal,
display: Display<State>,
headless: bool,
create_wayland_socket: bool,
) -> Result<Self, Box<dyn std::error::Error>> {
let _span = tracy_client::span!("State::new");
@@ -571,7 +576,14 @@ impl State {
Backend::Tty(tty)
};
let mut niri = Niri::new(config.clone(), event_loop, stop_signal, display, &backend);
let mut niri = Niri::new(
config.clone(),
event_loop,
stop_signal,
display,
&backend,
create_wayland_socket,
);
backend.init(&mut niri);
let mut state = Self { backend, niri };
@@ -1743,6 +1755,7 @@ impl Niri {
stop_signal: LoopSignal,
display: Display<State>,
backend: &Backend,
create_wayland_socket: bool,
) -> Self {
let _span = tracy_client::span!("Niri::new");
@@ -1929,19 +1942,22 @@ impl Niri {
)
.unwrap();
let socket_source = ListeningSocketSource::new_auto().unwrap();
let socket_name = socket_source.socket_name().to_os_string();
event_loop
.insert_source(socket_source, move |client, _, state| {
state.niri.insert_client(NewClient {
client,
restricted: false,
credentials_unknown: false,
});
})
.unwrap();
let socket_name = create_wayland_socket.then(|| {
let socket_source = ListeningSocketSource::new_auto().unwrap();
let socket_name = socket_source.socket_name().to_os_string();
event_loop
.insert_source(socket_source, move |client, _, state| {
state.niri.insert_client(NewClient {
client,
restricted: false,
credentials_unknown: false,
});
})
.unwrap();
socket_name
});
let ipc_server = match IpcServer::start(&event_loop, &socket_name.to_string_lossy()) {
let ipc_server = match IpcServer::start(&event_loop, socket_name.as_deref()) {
Ok(server) => Some(server),
Err(err) => {
warn!("error starting IPC server: {err:?}");
+1
View File
@@ -22,6 +22,7 @@ impl Server {
event_loop.get_signal(),
display,
true,
false,
)
.unwrap();