aboutsummaryrefslogtreecommitdiff
path: root/src/utils/signals.rs
diff options
context:
space:
mode:
authorsodiboo <git@sodi.boo>2025-07-18 20:14:05 +0200
committerIvan Molodetskikh <yalterz@gmail.com>2025-07-18 11:41:17 -0700
commit485e667fec27aebc8be8dacfc35c702f276d4c47 (patch)
treed64f3b210c51af4b3693ef5948adacdbe8c00970 /src/utils/signals.rs
parent8f442dee060db7899abbdeb94d9d699920e3a6d5 (diff)
downloadniri-485e667fec27aebc8be8dacfc35c702f276d4c47.tar.gz
niri-485e667fec27aebc8be8dacfc35c702f276d4c47.tar.bz2
niri-485e667fec27aebc8be8dacfc35c702f276d4c47.zip
block signals early: now handled correctly with tracy ondemand
Diffstat (limited to 'src/utils/signals.rs')
-rw-r--r--src/utils/signals.rs25
1 files changed, 25 insertions, 0 deletions
diff --git a/src/utils/signals.rs b/src/utils/signals.rs
index 9aae44b1..bdface0f 100644
--- a/src/utils/signals.rs
+++ b/src/utils/signals.rs
@@ -34,6 +34,12 @@ pub fn listen(handle: &calloop::LoopHandle<crate::niri::State>) {
.unwrap();
}
+// We block the signals early, so that they apply to all threads.
+// They are then blocked *again* by the `Signals` source. That's fine.
+pub fn block_early() -> io::Result<()> {
+ set_sigmask(&preferred_sigset()?)
+}
+
pub fn unblock_all() -> io::Result<()> {
set_sigmask(&empty_sigset()?)
}
@@ -47,6 +53,25 @@ pub fn empty_sigset() -> io::Result<libc::sigset_t> {
}
}
+pub fn preferred_sigset() -> io::Result<libc::sigset_t> {
+ let mut set = empty_sigset()?;
+ unsafe {
+ add_signal(&mut set, libc::SIGINT)?;
+ add_signal(&mut set, libc::SIGTERM)?;
+ add_signal(&mut set, libc::SIGHUP)?;
+ }
+ Ok(set)
+}
+
+// SAFETY: `signum` must be a valid signal number.
+unsafe fn add_signal(set: &mut libc::sigset_t, signum: libc::c_int) -> io::Result<()> {
+ if unsafe { libc::sigaddset(set, signum) } == 0 {
+ Ok(())
+ } else {
+ Err(io::Error::last_os_error())
+ }
+}
+
pub fn set_sigmask(set: &libc::sigset_t) -> io::Result<()> {
let oldset = std::ptr::null_mut(); // ignore old mask
if unsafe { libc::pthread_sigmask(libc::SIG_SETMASK, set, oldset) } == 0 {