sighand: move signal handling into workspace crate
This commit is contained in:
parent
5a3b2a483b
commit
c709ff997f
5 changed files with 93 additions and 65 deletions
13
Cargo.lock
generated
13
Cargo.lock
generated
|
|
@ -704,6 +704,7 @@ dependencies = [
|
|||
"jay-geometry",
|
||||
"jay-io-uring",
|
||||
"jay-layout-animation",
|
||||
"jay-sighand",
|
||||
"jay-time",
|
||||
"jay-toml-config",
|
||||
"jay-tracy",
|
||||
|
|
@ -856,6 +857,18 @@ dependencies = [
|
|||
"jay-geometry",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "jay-sighand"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"jay-async-engine",
|
||||
"jay-io-uring",
|
||||
"jay-utils",
|
||||
"log",
|
||||
"thiserror",
|
||||
"uapi",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "jay-time"
|
||||
version = "0.1.0"
|
||||
|
|
|
|||
|
|
@ -38,6 +38,7 @@ members = [
|
|||
"eventfd-cache",
|
||||
"wheel",
|
||||
"cpu-worker",
|
||||
"sighand",
|
||||
"toml-config",
|
||||
"algorithms",
|
||||
"toml-spec",
|
||||
|
|
@ -77,6 +78,7 @@ jay-tree-types = { version = "0.1.0", path = "tree-types" }
|
|||
jay-eventfd-cache = { version = "0.1.0", path = "eventfd-cache" }
|
||||
jay-wheel = { version = "0.1.0", path = "wheel" }
|
||||
jay-cpu-worker = { version = "0.1.0", path = "cpu-worker" }
|
||||
jay-sighand = { version = "0.1.0", path = "sighand" }
|
||||
|
||||
uapi = "0.2.13"
|
||||
thiserror = "2.0.11"
|
||||
|
|
|
|||
14
sighand/Cargo.toml
Normal file
14
sighand/Cargo.toml
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
[package]
|
||||
name = "jay-sighand"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
license = "GPL-3.0-only"
|
||||
|
||||
[dependencies]
|
||||
jay-async-engine = { version = "0.1.0", path = "../async-engine" }
|
||||
jay-io-uring = { version = "0.1.0", path = "../io-uring" }
|
||||
jay-utils = { version = "0.1.0", path = "../utils" }
|
||||
|
||||
log = { version = "0.4.20", features = ["std"] }
|
||||
thiserror = "2.0.11"
|
||||
uapi = "0.2.13"
|
||||
63
sighand/src/lib.rs
Normal file
63
sighand/src/lib.rs
Normal file
|
|
@ -0,0 +1,63 @@
|
|||
use {
|
||||
jay_async_engine::{AsyncEngine, SpawnedFuture},
|
||||
jay_io_uring::IoUring,
|
||||
jay_utils::{
|
||||
buf::TypedBuf,
|
||||
errorfmt::ErrorFmt,
|
||||
oserror::{OsError, OsErrorExt2},
|
||||
},
|
||||
std::rc::Rc,
|
||||
thiserror::Error,
|
||||
uapi::{OwnedFd, c},
|
||||
};
|
||||
|
||||
#[derive(Debug, Error)]
|
||||
pub enum SighandError {
|
||||
#[error("Could not block the signalfd signals")]
|
||||
BlockFailed(#[source] OsError),
|
||||
#[error("Could not create a signalfd")]
|
||||
CreateFailed(#[source] OsError),
|
||||
}
|
||||
|
||||
pub fn install(
|
||||
eng: &Rc<AsyncEngine>,
|
||||
ring: &Rc<IoUring>,
|
||||
) -> Result<SpawnedFuture<()>, SighandError> {
|
||||
let mut set: c::sigset_t = uapi::pod_zeroed();
|
||||
uapi::sigaddset(&mut set, c::SIGINT).unwrap();
|
||||
uapi::sigaddset(&mut set, c::SIGTERM).unwrap();
|
||||
uapi::sigaddset(&mut set, c::SIGPIPE).unwrap();
|
||||
uapi::pthread_sigmask(c::SIG_BLOCK, Some(&set), None).map_os_err(SighandError::BlockFailed)?;
|
||||
let fd = uapi::signalfd_new(&set, c::SFD_CLOEXEC)
|
||||
.map(Rc::new)
|
||||
.map_os_err(SighandError::CreateFailed)?;
|
||||
Ok(eng.spawn("signal handler", handle_signals(fd, ring.clone())))
|
||||
}
|
||||
|
||||
async fn handle_signals(fd: Rc<OwnedFd>, ring: Rc<IoUring>) {
|
||||
let mut buf = TypedBuf::<c::signalfd_siginfo>::new();
|
||||
loop {
|
||||
if let Err(e) = ring.read(&fd, buf.buf()).await {
|
||||
log::error!("Could not read from signal fd: {}", ErrorFmt(e));
|
||||
return;
|
||||
}
|
||||
let sig = buf.t().ssi_signo as i32;
|
||||
log::info!("Received signal {}", sig);
|
||||
if matches!(sig, c::SIGINT | c::SIGTERM) {
|
||||
log::info!("Exiting");
|
||||
ring.stop();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn reset_all() {
|
||||
const NSIG: c::c_int = 64;
|
||||
unsafe {
|
||||
for sig in 1..=NSIG {
|
||||
c::signal(sig, c::SIG_DFL);
|
||||
}
|
||||
}
|
||||
let mut set: c::sigset_t = uapi::pod_zeroed();
|
||||
uapi::sigfillset(&mut set).unwrap();
|
||||
let _ = uapi::pthread_sigmask(c::SIG_UNBLOCK, Some(&set), None);
|
||||
}
|
||||
|
|
@ -1,65 +1 @@
|
|||
use {
|
||||
crate::{
|
||||
async_engine::{AsyncEngine, SpawnedFuture},
|
||||
io_uring::IoUring,
|
||||
utils::{
|
||||
buf::TypedBuf,
|
||||
errorfmt::ErrorFmt,
|
||||
oserror::{OsError, OsErrorExt2},
|
||||
},
|
||||
},
|
||||
std::rc::Rc,
|
||||
thiserror::Error,
|
||||
uapi::{OwnedFd, c},
|
||||
};
|
||||
|
||||
#[derive(Debug, Error)]
|
||||
pub enum SighandError {
|
||||
#[error("Could not block the signalfd signals")]
|
||||
BlockFailed(#[source] OsError),
|
||||
#[error("Could not create a signalfd")]
|
||||
CreateFailed(#[source] OsError),
|
||||
}
|
||||
|
||||
pub fn install(
|
||||
eng: &Rc<AsyncEngine>,
|
||||
ring: &Rc<IoUring>,
|
||||
) -> Result<SpawnedFuture<()>, SighandError> {
|
||||
let mut set: c::sigset_t = uapi::pod_zeroed();
|
||||
uapi::sigaddset(&mut set, c::SIGINT).unwrap();
|
||||
uapi::sigaddset(&mut set, c::SIGTERM).unwrap();
|
||||
uapi::sigaddset(&mut set, c::SIGPIPE).unwrap();
|
||||
uapi::pthread_sigmask(c::SIG_BLOCK, Some(&set), None).map_os_err(SighandError::BlockFailed)?;
|
||||
let fd = uapi::signalfd_new(&set, c::SFD_CLOEXEC)
|
||||
.map(Rc::new)
|
||||
.map_os_err(SighandError::CreateFailed)?;
|
||||
Ok(eng.spawn("signal handler", handle_signals(fd, ring.clone())))
|
||||
}
|
||||
|
||||
async fn handle_signals(fd: Rc<OwnedFd>, ring: Rc<IoUring>) {
|
||||
let mut buf = TypedBuf::<c::signalfd_siginfo>::new();
|
||||
loop {
|
||||
if let Err(e) = ring.read(&fd, buf.buf()).await {
|
||||
log::error!("Could not read from signal fd: {}", ErrorFmt(e));
|
||||
return;
|
||||
}
|
||||
let sig = buf.t().ssi_signo as i32;
|
||||
log::info!("Received signal {}", sig);
|
||||
if matches!(sig, c::SIGINT | c::SIGTERM) {
|
||||
log::info!("Exiting");
|
||||
ring.stop();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn reset_all() {
|
||||
const NSIG: c::c_int = 64;
|
||||
unsafe {
|
||||
for sig in 1..=NSIG {
|
||||
c::signal(sig, c::SIG_DFL);
|
||||
}
|
||||
}
|
||||
let mut set: c::sigset_t = uapi::pod_zeroed();
|
||||
uapi::sigfillset(&mut set).unwrap();
|
||||
let _ = uapi::pthread_sigmask(c::SIG_UNBLOCK, Some(&set), None);
|
||||
}
|
||||
pub use jay_sighand::*;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue