1
0
Fork 0
forked from wry/wry

workspace: move crates under crates

This commit is contained in:
kossLAN 2026-05-29 18:55:59 -04:00
parent 0016bc8cf0
commit 6393fdf3c0
No known key found for this signature in database
354 changed files with 102 additions and 102 deletions

63
crates/sighand/src/lib.rs Normal file
View 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);
}