diff --git a/userspace/Cargo.lock b/userspace/Cargo.lock index d6065991..728e6928 100644 --- a/userspace/Cargo.lock +++ b/userspace/Cargo.lock @@ -1693,6 +1693,7 @@ dependencies = [ "bytemuck", "clap", "clap-num", + "cross", "hclient", "http", "log", diff --git a/userspace/lib/cross/src/sys/yggdrasil/mod.rs b/userspace/lib/cross/src/sys/yggdrasil/mod.rs index b1fe3d70..492e8c7c 100644 --- a/userspace/lib/cross/src/sys/yggdrasil/mod.rs +++ b/userspace/lib/cross/src/sys/yggdrasil/mod.rs @@ -11,10 +11,18 @@ pub mod term; pub mod time; pub mod timer; +use runtime::rt::process::Signal; use std::{ io, - os::{fd::RawFd, yggdrasil::process::CommandExt}, + os::{ + fd::RawFd, + yggdrasil::{ + process::CommandExt, + signal::{set_signal_handler, SignalHandler}, + }, + }, process::Command, + sync::atomic::{AtomicUsize, Ordering}, }; pub use mem::{FileMappingImpl, SharedMemoryImpl}; @@ -29,7 +37,22 @@ pub use timer::TimerFdImpl; use crate::process::CommandSpawnExt; -pub fn set_sigint_handler(_handler: fn()) {} +pub fn set_sigint_handler(handler: fn()) { + static HANDLER: AtomicUsize = AtomicUsize::new(0); + + fn handler_stub(_signal: Signal) { + let handler = HANDLER.load(Ordering::Acquire); + + if handler != 0 { + let handler: fn() = unsafe { core::mem::transmute(handler) }; + handler(); + } + } + + HANDLER.store(handler as usize, Ordering::Release); + + let _ = set_signal_handler(Signal::Interrupted, SignalHandler::Function(handler_stub)); +} pub fn send_kill(pid: u32) -> io::Result<()> { use runtime::rt::{ diff --git a/userspace/netutils/Cargo.toml b/userspace/netutils/Cargo.toml index b7b3e3be..a42676e3 100644 --- a/userspace/netutils/Cargo.toml +++ b/userspace/netutils/Cargo.toml @@ -8,6 +8,7 @@ yggdrasil-abi.workspace = true runtime.workspace = true logsink.workspace = true hclient.workspace = true +cross.workspace = true log.workspace = true bytemuck.workspace = true diff --git a/userspace/netutils/src/ping.rs b/userspace/netutils/src/ping.rs index 9cec6467..9d2c6aee 100644 --- a/userspace/netutils/src/ping.rs +++ b/userspace/netutils/src/ping.rs @@ -15,6 +15,7 @@ use std::{ use bytemuck::Zeroable; use clap::Parser; +use cross::signal::set_sigint_handler; use netutils::{netconfig::NetConfig, Error}; use runtime::abi::net::MacAddress; use yggdrasil_abi::net::{ @@ -339,7 +340,13 @@ fn ping( static INTERRUPTED: AtomicBool = AtomicBool::new(false); +fn sigint_handler() { + eprintln!("Sigint received, aborting"); + INTERRUPTED.store(true, Ordering::Release); +} + fn main() -> ExitCode { + set_sigint_handler(sigint_handler); // set_signal_handler(Signal::Interrupted, SignalHandler::Function(interrupt)); let args = Args::parse();