refactor signal handling, and clear sigmask before spawning
Browse files at this commit8f442dee060db7899abbdeb94d9d699920e3a6d5Parent: 9c09bc75 changed files +63 -11
- src/main.rs +1 -11
- src/utils/mod.rs +1 -0
- src/utils/signals.rs +57 -0
- src/utils/spawning.rs +2 -0
- src/utils/xwayland/satellite.rs +2 -0
src/main.rsmodified+1 -11
| @@ -9,7 +9,6 @@ use std::path::{Path, PathBuf}; | |||
| 9 | use std::process::Command; | 9 | use std::process::Command; |
| 10 | use std::{env, mem}; | 10 | use std::{env, mem}; |
| 11 | 11 | ||
| 12 | -use calloop::signals::{Signal, Signals}; | ||
| 13 | use calloop::EventLoop; | 12 | use calloop::EventLoop; |
| 14 | use clap::{CommandFactory, Parser}; | 13 | use clap::{CommandFactory, Parser}; |
| 15 | use clap_complete::Shell; | 14 | use clap_complete::Shell; |
| @@ -197,16 +196,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> { | |||
| 197 | let mut event_loop = EventLoop::<State>::try_new().unwrap(); | 196 | let mut event_loop = EventLoop::<State>::try_new().unwrap(); |
| 198 | 197 | ||
| 199 | // Handle Ctrl+C and other signals. | 198 | // Handle Ctrl+C and other signals. |
| 200 | - event_loop | 199 | + niri::utils::signals::listen(&event_loop.handle()); |
| 201 | - .handle() | ||
| 202 | - .insert_source( | ||
| 203 | - Signals::new(&[Signal::SIGINT, Signal::SIGTERM, Signal::SIGHUP]).unwrap(), | ||
| 204 | - |event, _, state| { | ||
| 205 | - info!("quitting due to receiving signal {:?}", event.signal()); | ||
| 206 | - state.niri.stop_signal.stop(); | ||
| 207 | - }, | ||
| 208 | - ) | ||
| 209 | - .unwrap(); | ||
| 210 | 200 | ||
| 211 | // Create the compositor. | 201 | // Create the compositor. |
| 212 | let display = Display::new().unwrap(); | 202 | let display = Display::new().unwrap(); |
src/utils/mod.rsmodified+1 -0
| @@ -34,6 +34,7 @@ use crate::niri::ClientState; | |||
| 34 | 34 | ||
| 35 | pub mod id; | 35 | pub mod id; |
| 36 | pub mod scale; | 36 | pub mod scale; |
| 37 | +pub mod signals; | ||
| 37 | pub mod spawning; | 38 | pub mod spawning; |
| 38 | pub mod transaction; | 39 | pub mod transaction; |
| 39 | pub mod watcher; | 40 | pub mod watcher; |
src/utils/signals.rsadded+57 -0
| @@ -0,0 +1,57 @@ | |||
| 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 | + | ||
| 21 | +use std::{io, mem}; | ||
| 22 | + | ||
| 23 | +use calloop::signals::{Signal, Signals}; | ||
| 24 | + | ||
| 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 | +} | ||
| 36 | + | ||
| 37 | +pub fn unblock_all() -> io::Result<()> { | ||
| 38 | + set_sigmask(&empty_sigset()?) | ||
| 39 | +} | ||
| 40 | + | ||
| 41 | +pub fn empty_sigset() -> io::Result<libc::sigset_t> { | ||
| 42 | + let mut sigset = mem::MaybeUninit::uninit(); | ||
| 43 | + if unsafe { libc::sigemptyset(sigset.as_mut_ptr()) } == 0 { | ||
| 44 | + Ok(unsafe { sigset.assume_init() }) | ||
| 45 | + } else { | ||
| 46 | + Err(io::Error::last_os_error()) | ||
| 47 | + } | ||
| 48 | +} | ||
| 49 | + | ||
| 50 | +pub fn set_sigmask(set: &libc::sigset_t) -> io::Result<()> { | ||
| 51 | + let oldset = std::ptr::null_mut(); // ignore old mask | ||
| 52 | + if unsafe { libc::pthread_sigmask(libc::SIG_SETMASK, set, oldset) } == 0 { | ||
| 53 | + Ok(()) | ||
| 54 | + } else { | ||
| 55 | + Err(io::Error::last_os_error()) | ||
| 56 | + } | ||
| 57 | +} | ||
src/utils/spawning.rsmodified+2 -0
| @@ -141,6 +141,8 @@ fn spawn_sync( | |||
| 141 | process.env("DESKTOP_STARTUP_ID", token.as_str()); | 141 | process.env("DESKTOP_STARTUP_ID", token.as_str()); |
| 142 | } | 142 | } |
| 143 | 143 | ||
| 144 | + unsafe { process.pre_exec(crate::utils::signals::unblock_all) }; | ||
| 145 | + | ||
| 144 | let Some(mut child) = do_spawn(command, process) else { | 146 | let Some(mut child) = do_spawn(command, process) else { |
| 145 | return; | 147 | return; |
| 146 | }; | 148 | }; |
src/utils/xwayland/satellite.rsmodified+2 -0
| @@ -238,6 +238,8 @@ fn spawn(path: String, xwl: &Satellite) { | |||
| 238 | .stdout(Stdio::null()) | 238 | .stdout(Stdio::null()) |
| 239 | .stderr(Stdio::null()); | 239 | .stderr(Stdio::null()); |
| 240 | 240 | ||
| 241 | + unsafe { process.pre_exec(crate::utils::signals::unblock_all) }; | ||
| 242 | + | ||
| 241 | // Spawning and waiting takes some milliseconds, so do it in a thread. | 243 | // Spawning and waiting takes some milliseconds, so do it in a thread. |
| 242 | let res = thread::Builder::new() | 244 | let res = thread::Builder::new() |
| 243 | .name("Xwl-s Spawner".to_owned()) | 245 | .name("Xwl-s Spawner".to_owned()) |