netutils/ping: add sigint handler

This commit is contained in:
2025-12-04 08:30:08 +02:00
parent e1b905c65a
commit 61644bdef5
4 changed files with 34 additions and 2 deletions
+1
View File
@@ -1693,6 +1693,7 @@ dependencies = [
"bytemuck",
"clap",
"clap-num",
"cross",
"hclient",
"http",
"log",
+25 -2
View File
@@ -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::{
+1
View File
@@ -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
+7
View File
@@ -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();