diff --git a/Cargo.toml b/Cargo.toml index a328b71..21f5bd7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -31,6 +31,9 @@ gtk = { package = "gtk4", version = "0.7.2", features = ["v4_2"], optional = tru adw = { package = "libadwaita", version = "0.5.2", features = ["v1_1"], optional = true } async-channel = { version = "2.1.1", optional = true } +[target.'cfg(unix)'.dependencies] +libc = "0.2.148" + [target.'cfg(all(unix, not(target_os="macos")))'.dependencies] wayland-client = { version="0.31.1", optional = true } wayland-protocols = { version="0.31.0", features=["client", "staging", "unstable"], optional = true } @@ -39,7 +42,6 @@ wayland-protocols-misc = { version="0.2.0", features=["client"], optional = true x11 = { version = "2.21.0", features = ["xlib", "xtest"], optional = true } ashpd = { version = "0.6.2", default-features = false, features = ["tokio"], optional = true } reis = { git = "https://github.com/ids1024/reis", features = [ "tokio" ], optional = true } -libc = { version="0.2.148", optional = true } [target.'cfg(target_os="macos")'.dependencies] core-graphics = { version = "0.23", features = ["highsierra"] } @@ -55,5 +57,5 @@ default = ["wayland", "x11", "xdg_desktop_portal", "libei", "gtk"] wayland = ["dep:wayland-client", "dep:wayland-protocols", "dep:wayland-protocols-wlr", "dep:wayland-protocols-misc" ] x11 = ["dep:x11"] xdg_desktop_portal = ["dep:ashpd"] -libei = ["dep:reis", "dep:ashpd", "dep:libc" ] +libei = ["dep:reis", "dep:ashpd"] gtk = ["dep:gtk", "dep:adw", "dep:async-channel"] diff --git a/src/frontend/cli.rs b/src/frontend/cli.rs index c71af8e..b1cf809 100644 --- a/src/frontend/cli.rs +++ b/src/frontend/cli.rs @@ -25,7 +25,7 @@ pub fn run() -> Result<()> { loop { let mut buf = String::new(); match std::io::stdin().read_line(&mut buf) { - Ok(0) => break, + Ok(0) => return, Ok(len) => { if let Some(events) = parse_cmd(buf, len) { for event in events.iter() { @@ -48,14 +48,16 @@ pub fn run() -> Result<()> { } } Err(e) => { - log::error!("error reading from stdin: {e}"); - break; + if e.kind() != ErrorKind::UnexpectedEof { + log::error!("error reading from stdin: {e}"); + } + return; } } } })?; - let writer = thread::Builder::new() + let _ = thread::Builder::new() .name("cli-frontend-notify".to_string()) .spawn(move || { loop { @@ -124,7 +126,7 @@ pub fn run() -> Result<()> { } })?; match reader.join() { - Ok(_) => (), + Ok(_) => {} Err(e) => { let msg = match (e.downcast_ref::<&str>(), e.downcast_ref::()) { (Some(&s), _) => s, @@ -134,17 +136,6 @@ pub fn run() -> Result<()> { log::error!("reader thread paniced: {msg}"); } } - match writer.join() { - Ok(_) => (), - Err(e) => { - let msg = match (e.downcast_ref::<&str>(), e.downcast_ref::()) { - (Some(&s), _) => s, - (_, Some(s)) => s, - _ => "no panic info", - }; - log::error!("writer thread paniced: {msg}"); - } - } Ok(()) } diff --git a/src/main.rs b/src/main.rs index 8561654..cd10823 100644 --- a/src/main.rs +++ b/src/main.rs @@ -39,6 +39,15 @@ pub fn run() -> Result<()> { let mut service = start_service()?; frontend::run_frontend(&config)?; log::info!("terminating service"); + #[cfg(unix)] + { + // on unix we give the service a chance to terminate gracefully + let pid = service.id() as libc::pid_t; + unsafe { + libc::kill(pid, libc::SIGINT); + } + service.wait()?; + } service.kill()?; } diff --git a/src/server.rs b/src/server.rs index 411455d..f7c7526 100644 --- a/src/server.rs +++ b/src/server.rs @@ -198,7 +198,10 @@ impl Server { Some(e) => e, None => return Err::<(), anyhow::Error>(anyhow!("frontend channel closed")), }; - Self::handle_frontend_event(&producer_notify_tx, &consumer_notify_tx, &client_manager, &resolve_tx, &mut frontend, &port_tx, frontend_event).await; + let exit = Self::handle_frontend_event(&producer_notify_tx, &consumer_notify_tx, &client_manager, &resolve_tx, &mut frontend, &port_tx, frontend_event).await; + if exit { + return Ok(()); + } } notify = frontend_notify_rx.recv() => { let notify = match notify { @@ -294,12 +297,23 @@ impl Server { let reaper = task::spawn_local(async move { tokio::select! { _ = signal::ctrl_c() => { - producer_task.abort(); - receiver_task.abort(); - frontend_task.abort(); - resolver_task.abort(); - udp_task.abort(); + log::info!("terminating service"); }, + _ = producer_task => { + // TODO restart producer? + } + _ = receiver_task => { + // TODO restart producer? + } + _ = frontend_task => { + // frontend exited => exit requested + } + _ = resolver_task => { + // resolver exited + } + _ = udp_task => { + // udp exited + } } });