block signals early: now handled correctly with tracy ondemand
Browse files at this commit485e667fec27aebc8be8dacfc35c702f276d4c47Parent: 8f442de2 changed files +28 -0
- src/main.rs +3 -0
- src/utils/signals.rs +25 -0
src/main.rsmodified+3 -0
| @@ -134,6 +134,9 @@ fn main() -> Result<(), Box<dyn std::error::Error>> { | |||
| 134 | } | 134 | } |
| 135 | } | 135 | } |
| 136 | 136 | ||
| 137 | + // Needs to be done before starting Tracy, so that it applies to Tracy's threads. | ||
| 138 | + niri::utils::signals::block_early().unwrap(); | ||
| 139 | + | ||
| 137 | // Avoid starting Tracy for the `niri msg` code path since starting/stopping Tracy is a bit | 140 | // Avoid starting Tracy for the `niri msg` code path since starting/stopping Tracy is a bit |
| 138 | // slow. | 141 | // slow. |
| 139 | tracy_client::Client::start(); | 142 | tracy_client::Client::start(); |
src/utils/signals.rsmodified+25 -0
| @@ -34,6 +34,12 @@ pub fn listen(handle: &calloop::LoopHandle<crate::niri::State>) { | |||
| 34 | .unwrap(); | 34 | .unwrap(); |
| 35 | } | 35 | } |
| 36 | 36 | ||
| 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()?) | ||
| 41 | +} | ||
| 42 | + | ||
| 37 | pub fn unblock_all() -> io::Result<()> { | 43 | pub fn unblock_all() -> io::Result<()> { |
| 38 | set_sigmask(&empty_sigset()?) | 44 | set_sigmask(&empty_sigset()?) |
| 39 | } | 45 | } |
| @@ -47,6 +53,25 @@ pub fn empty_sigset() -> io::Result<libc::sigset_t> { | |||
| 47 | } | 53 | } |
| 48 | } | 54 | } |
| 49 | 55 | ||
| 56 | +pub 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)?; | ||
| 62 | + } | ||
| 63 | + Ok(set) | ||
| 64 | +} | ||
| 65 | + | ||
| 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()) | ||
| 72 | + } | ||
| 73 | +} | ||
| 74 | + | ||
| 50 | pub fn set_sigmask(set: &libc::sigset_t) -> io::Result<()> { | 75 | pub fn set_sigmask(set: &libc::sigset_t) -> io::Result<()> { |
| 51 | let oldset = std::ptr::null_mut(); // ignore old mask | 76 | let oldset = std::ptr::null_mut(); // ignore old mask |
| 52 | if unsafe { libc::pthread_sigmask(libc::SIG_SETMASK, set, oldset) } == 0 { | 77 | if unsafe { libc::pthread_sigmask(libc::SIG_SETMASK, set, oldset) } == 0 { |