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 | //! never intended to spawn niri with a nonempty sigmask. It indicates a bug in whoever spawned us, | 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). | 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>) { | 27 | + // FIXME: implement for FreeBSD. But probably, that should be done in calloop::signals. |
| 26 | - handle | 28 | + pub fn listen(_handle: &calloop::LoopHandle<crate::niri::State>) {} |
| 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 | -} | ||
| 36 | 29 | ||
| 37 | -// We block the signals early, so that they apply to all threads. | 30 | + // These two actually build as-is on FreeBSD, but without our own signal handling in listen(), |
| 38 | -// They are then blocked *again* by the `Signals` source. That's fine. | 31 | + // they do more harm than good (they block termination signals without actually installing a |
| 39 | -pub fn block_early() -> io::Result<()> { | 32 | + // termination handler). |
| 40 | - set_sigmask(&preferred_sigset()?) | 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<()> { | 41 | +#[cfg(target_os = "linux")] |
| 44 | - set_sigmask(&empty_sigset()?) | 42 | +mod platform { |
| 45 | -} | 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 | + handle |
| 48 | - let mut sigset = mem::MaybeUninit::uninit(); | 49 | + .insert_source( |
| 49 | - if unsafe { libc::sigemptyset(sigset.as_mut_ptr()) } == 0 { | 50 | + Signals::new(&[Signal::SIGINT, Signal::SIGTERM, Signal::SIGHUP]).unwrap(), |
| 50 | - Ok(unsafe { sigset.assume_init() }) | 51 | + |event, _, state| { |
| 51 | - } else { | 52 | + info!("quitting due to receiving signal {:?}", event.signal()); |
| 52 | - Err(io::Error::last_os_error()) | 53 | + state.niri.stop_signal.stop(); |
| 54 | + }, | ||
| 55 | + ) | ||
| 56 | + .unwrap(); | ||
| 53 | } | 57 | } |
| 54 | -} | ||
| 55 | 58 | ||
| 56 | -fn preferred_sigset() -> io::Result<libc::sigset_t> { | 59 | + // We block the signals early, so that they apply to all threads. |
| 57 | - let mut set = empty_sigset()?; | 60 | + // They are then blocked *again* by the `Signals` source. That's fine. |
| 58 | - unsafe { | 61 | + pub fn block_early() -> io::Result<()> { |
| 59 | - add_signal(&mut set, libc::SIGINT)?; | 62 | + set_sigmask(&preferred_sigset()?) |
| 60 | - add_signal(&mut set, libc::SIGTERM)?; | ||
| 61 | - add_signal(&mut set, libc::SIGHUP)?; | ||
| 62 | } | 63 | } |
| 63 | - Ok(set) | ||
| 64 | -} | ||
| 65 | 64 | ||
| 66 | -// SAFETY: `signum` must be a valid signal number. | 65 | + pub fn unblock_all() -> io::Result<()> { |
| 67 | -unsafe fn add_signal(set: &mut libc::sigset_t, signum: libc::c_int) -> io::Result<()> { | 66 | + set_sigmask(&empty_sigset()?) |
| 68 | - if unsafe { libc::sigaddset(set, signum) } == 0 { | ||
| 69 | - Ok(()) | ||
| 70 | - } else { | ||
| 71 | - Err(io::Error::last_os_error()) | ||
| 72 | } | 67 | } |
| 73 | -} | ||
| 74 | 68 | ||
| 75 | -fn set_sigmask(set: &libc::sigset_t) -> io::Result<()> { | 69 | + fn empty_sigset() -> io::Result<libc::sigset_t> { |
| 76 | - let oldset = std::ptr::null_mut(); // ignore old mask | 70 | + let mut sigset = mem::MaybeUninit::uninit(); |
| 77 | - if unsafe { libc::pthread_sigmask(libc::SIG_SETMASK, set, oldset) } == 0 { | 71 | + if unsafe { libc::sigemptyset(sigset.as_mut_ptr()) } == 0 { |
| 78 | - Ok(()) | 72 | + Ok(unsafe { sigset.assume_init() }) |
| 79 | - } else { | 73 | + } else { |
| 80 | - Err(io::Error::last_os_error()) | 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 | } |