signals: Gate on target_os = "linux"
Browse files at this commite19e1f0f10fa4e294e62292608635877715232ddParent: c0ddf3f1 changed file +71 -48
- src/utils/signals.rs +71 -48
src/utils/signals.rsmodified+71 -48
| @@ -18,65 +18,88 @@ | ||
| 18 | 18 | //! never intended to spawn niri with a nonempty sigmask. It indicates a bug in whoever spawned us, |
| 19 | 19 | //! so we may as well clean up after them (which is easier than not doing so). |
| 20 | 20 | |
| 21 | -use std::{io, mem}; | |
| 21 | +pub use platform::*; | |
| 22 | 22 | |
| 23 | -use calloop::signals::{Signal, Signals}; | |
| 23 | +#[cfg(not(target_os = "linux"))] | |
| 24 | +mod platform { | |
| 25 | + use std::io; | |
| 24 | 26 | |
| 25 | -pub fn listen(handle: &calloop::LoopHandle<crate::niri::State>) { | |
| 26 | - handle | |
| 27 | - .insert_source( | |
| 28 | - Signals::new(&[Signal::SIGINT, Signal::SIGTERM, Signal::SIGHUP]).unwrap(), | |
| 29 | - |event, _, state| { | |
| 30 | - info!("quitting due to receiving signal {:?}", event.signal()); | |
| 31 | - state.niri.stop_signal.stop(); | |
| 32 | - }, | |
| 33 | - ) | |
| 34 | - .unwrap(); | |
| 35 | -} | |
| 27 | + // FIXME: implement for FreeBSD. But probably, that should be done in calloop::signals. | |
| 28 | + pub fn listen(_handle: &calloop::LoopHandle<crate::niri::State>) {} | |
| 36 | 29 | |
| 37 | -// We block the signals early, so that they apply to all threads. | |
| 38 | -// They are then blocked *again* by the `Signals` source. That's fine. | |
| 39 | -pub fn block_early() -> io::Result<()> { | |
| 40 | - set_sigmask(&preferred_sigset()?) | |
| 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 | + } | |
| 41 | 39 | } |
| 42 | 40 | |
| 43 | -pub fn unblock_all() -> io::Result<()> { | |
| 44 | - set_sigmask(&empty_sigset()?) | |
| 45 | -} | |
| 41 | +#[cfg(target_os = "linux")] | |
| 42 | +mod platform { | |
| 43 | + use std::{io, mem}; | |
| 44 | + | |
| 45 | + pub fn listen(handle: &calloop::LoopHandle<crate::niri::State>) { | |
| 46 | + use calloop::signals::{Signal, Signals}; | |
| 46 | 47 | |
| 47 | -fn empty_sigset() -> io::Result<libc::sigset_t> { | |
| 48 | - let mut sigset = mem::MaybeUninit::uninit(); | |
| 49 | - if unsafe { libc::sigemptyset(sigset.as_mut_ptr()) } == 0 { | |
| 50 | - Ok(unsafe { sigset.assume_init() }) | |
| 51 | - } else { | |
| 52 | - Err(io::Error::last_os_error()) | |
| 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(); | |
| 53 | 57 | } |
| 54 | -} | |
| 55 | 58 | |
| 56 | -fn preferred_sigset() -> io::Result<libc::sigset_t> { | |
| 57 | - let mut set = empty_sigset()?; | |
| 58 | - unsafe { | |
| 59 | - add_signal(&mut set, libc::SIGINT)?; | |
| 60 | - add_signal(&mut set, libc::SIGTERM)?; | |
| 61 | - add_signal(&mut set, libc::SIGHUP)?; | |
| 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()?) | |
| 62 | 63 | } |
| 63 | - Ok(set) | |
| 64 | -} | |
| 65 | 64 | |
| 66 | -// SAFETY: `signum` must be a valid signal number. | |
| 67 | -unsafe fn add_signal(set: &mut libc::sigset_t, signum: libc::c_int) -> io::Result<()> { | |
| 68 | - if unsafe { libc::sigaddset(set, signum) } == 0 { | |
| 69 | - Ok(()) | |
| 70 | - } else { | |
| 71 | - Err(io::Error::last_os_error()) | |
| 65 | + pub fn unblock_all() -> io::Result<()> { | |
| 66 | + set_sigmask(&empty_sigset()?) | |
| 72 | 67 | } |
| 73 | -} | |
| 74 | 68 | |
| 75 | -fn set_sigmask(set: &libc::sigset_t) -> io::Result<()> { | |
| 76 | - let oldset = std::ptr::null_mut(); // ignore old mask | |
| 77 | - if unsafe { libc::pthread_sigmask(libc::SIG_SETMASK, set, oldset) } == 0 { | |
| 78 | - Ok(()) | |
| 79 | - } else { | |
| 80 | - Err(io::Error::last_os_error()) | |
| 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 | + } | |
| 81 | 104 | } |
| 82 | 105 | } |