forge
Explore
Log in

Don't you dare go Hollow

niri

Clone
★ 0

0 watching0 forksRustGPL-3.012.1 MiB055a94dIvan Molodetskikh1 year ago2247 commits

signals.rs105 lines, 3.7 KiBHistoryRawDownload
1//! We set a signal handler with `calloop::signals::Signals::new`.
2//! This does two things:
3//! 1. It blocks the thread from receiving these signals normally (pthread_sigmask)
4//! 2. It creates a signalfd to read them in the event loop.
5//!
6//! When spawning children, calloop already deals with the signalfd.
7//! `Signals::new` creates it with CLOEXEC, so it will not be inherited by children.
8//!
9//! But, the sigmask is always inherited, so we want to clear it before spawning children.
10//! That way, we don't affect their normal signal handling.
11//!
12//! In particular, if a child doesn't care about signals, we must not block it from receiving them.
13//!
14//! This module provides functions to clear the sigmask. Call them before spawning children.
15//!
16//! Technically, a "more correct" solution would be to remember the original sigmask and restore it
17//! after the child exits, but that's painful *and* likely to cause issues, because the user almost
18//! never intended to spawn niri with a nonempty sigmask. It indicates a bug in whoever spawned us,
19//! so we may as well clean up after them (which is easier than not doing so).
20
21pub use platform::*;
22
23#[cfg(not(target_os = "linux"))]
24mod platform {
25 use std::io;
26
27 // FIXME: implement for FreeBSD. But probably, that should be done in calloop::signals.
28 pub fn listen(_handle: &calloop::LoopHandle<crate::niri::State>) {}
29
30 // These two actually build as-is on FreeBSD, but without our own signal handling in listen(),
31 // they do more harm than good (they block termination signals without actually installing a
32 // termination handler).
33 pub fn block_early() -> io::Result<()> {
34 Ok(())
35 }
36 pub fn unblock_all() -> io::Result<()> {
37 Ok(())
38 }
39}
40
41#[cfg(target_os = "linux")]
42mod platform {
43 use std::{io, mem};
44
45 pub fn listen(handle: &calloop::LoopHandle<crate::niri::State>) {
46 use calloop::signals::{Signal, Signals};
47
48 handle
49 .insert_source(
50 Signals::new(&[Signal::SIGINT, Signal::SIGTERM, Signal::SIGHUP]).unwrap(),
51 |event, _, state| {
52 info!("quitting due to receiving signal {:?}", event.signal());
53 state.niri.stop_signal.stop();
54 },
55 )
56 .unwrap();
57 }
58
59 // We block the signals early, so that they apply to all threads.
60 // They are then blocked *again* by the `Signals` source. That's fine.
61 pub fn block_early() -> io::Result<()> {
62 set_sigmask(&preferred_sigset()?)
63 }
64
65 pub fn unblock_all() -> io::Result<()> {
66 set_sigmask(&empty_sigset()?)
67 }
68
69 fn empty_sigset() -> io::Result<libc::sigset_t> {
70 let mut sigset = mem::MaybeUninit::uninit();
71 if unsafe { libc::sigemptyset(sigset.as_mut_ptr()) } == 0 {
72 Ok(unsafe { sigset.assume_init() })
73 } else {
74 Err(io::Error::last_os_error())
75 }
76 }
77
78 fn preferred_sigset() -> io::Result<libc::sigset_t> {
79 let mut set = empty_sigset()?;
80 unsafe {
81 add_signal(&mut set, libc::SIGINT)?;
82 add_signal(&mut set, libc::SIGTERM)?;
83 add_signal(&mut set, libc::SIGHUP)?;
84 }
85 Ok(set)
86 }
87
88 // SAFETY: `signum` must be a valid signal number.
89 unsafe fn add_signal(set: &mut libc::sigset_t, signum: libc::c_int) -> io::Result<()> {
90 if unsafe { libc::sigaddset(set, signum) } == 0 {
91 Ok(())
92 } else {
93 Err(io::Error::last_os_error())
94 }
95 }
96
97 fn set_sigmask(set: &libc::sigset_t) -> io::Result<()> {
98 let oldset = std::ptr::null_mut(); // ignore old mask
99 if unsafe { libc::pthread_sigmask(libc::SIG_SETMASK, set, oldset) } == 0 {
100 Ok(())
101 } else {
102 Err(io::Error::last_os_error())
103 }
104 }
105}