mrk-cli 0.12.0

Render Markdown beautifully in the terminal: syntax highlighting, tables, themes, and Mermaid diagrams as images.
Documentation
//! Signals that would end mrk, held off while a child program runs, so mrk outlives the child and cleans up after it.
use std::fmt;
use std::io::{self, IsTerminal};
use std::process::{Child, Command, ExitStatus, Stdio};
use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
use std::sync::{Arc, Mutex, PoisonError};
use std::thread;
use std::time::{Duration, Instant};

use rustix::process::{Pid, Signal, kill_process_group, test_kill_process_group};
use signal_hook::SigId;
use signal_hook::consts::{SIGHUP, SIGINT, SIGQUIT, SIGTERM};
use signal_hook::flag;
use signal_hook::low_level;

/// How often a wait checks whether a held signal arrived or the child is gone.
const POLL: Duration = Duration::from_millis(50);
/// How long the child's process group has to end after `SIGTERM` before it gets `SIGKILL`.
const GRACE: Duration = Duration::from_secs(5);
/// No signal arrived.
const NONE: usize = 0;

/// The signals that end mrk by default: Ctrl-C, Ctrl-\\, a closed terminal and a parent program stopping it. The
/// pager holds the same ones (`terminal::pager::signals`), since it only runs with stdout on a terminal.
const ENDING_ON_A_TERMINAL: [i32; 4] = [SIGHUP, SIGINT, SIGQUIT, SIGTERM];
const ENDING_OFF_A_TERMINAL: [i32; 3] = [SIGINT, SIGQUIT, SIGTERM];

/// The ending signals mrk may hold. `nohup` ignores `SIGHUP` and sends stdout and stderr away from the terminal; whether
/// a signal was ignored cannot be read without `unsafe`, so `SIGHUP` is left as it was when neither is a terminal.
pub fn ending_not_ignored() -> &'static [i32] {
    ending_for(io::stdout().is_terminal() || io::stderr().is_terminal())
}

fn ending_for(on_a_terminal: bool) -> &'static [i32] {
    if on_a_terminal { &ENDING_ON_A_TERMINAL } else { &ENDING_OFF_A_TERMINAL }
}

/// Per signal, the flag of an action that ends mrk as the default disposition would, armed whenever no `Held` holds
/// that signal: signal-hook cannot give the default disposition back once it handled a signal.
static ENDING: Mutex<Vec<(i32, Arc<AtomicBool>)>> = Mutex::new(Vec::new());

/// mrk stopped what it ran because a signal asked it to end: it exits as that signal would, `128 + signal`, with no
/// report of its own.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Interrupted(pub i32);

impl fmt::Display for Interrupted {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(formatter, "stopped by signal {}", self.0)
    }
}

impl std::error::Error for Interrupted {}

/// The signals held until dropped; from then on they end mrk again.
pub struct Held {
    arrived: Arc<AtomicUsize>,
    catchers: Vec<SigId>,
    endings: Vec<Arc<AtomicBool>>,
}

impl Held {
    /// Each signal is caught before its default is disarmed, so one arriving in between still ends mrk.
    pub fn hold(signals: &[i32]) -> io::Result<Self> {
        let mut held = Self { arrived: Arc::new(AtomicUsize::new(NONE)), catchers: Vec::new(), endings: Vec::new() };
        for &signal in signals {
            let ending = ending(signal)?;
            let number = usize::try_from(signal).map_err(|_| io::Error::from(io::ErrorKind::InvalidInput))?;
            held.catchers.push(flag::register_usize(signal, Arc::clone(&held.arrived), number)?);
            ending.store(false, Ordering::SeqCst);
            held.endings.push(ending);
        }
        Ok(held)
    }

    /// The last held signal that arrived.
    pub fn arrived(&self) -> Option<Interrupted> {
        match self.arrived.load(Ordering::SeqCst) {
            NONE => None,
            signal => i32::try_from(signal).ok().map(Interrupted),
        }
    }
}

/// The defaults are armed again before the signals stop being caught, so none slips through in between.
impl Drop for Held {
    fn drop(&mut self) {
        self.endings.iter().for_each(|ending| ending.store(true, Ordering::SeqCst));
        self.catchers.iter().for_each(|catcher| {
            low_level::unregister(*catcher);
        });
    }
}

fn ending(signal: i32) -> io::Result<Arc<AtomicBool>> {
    let mut endings = ENDING.lock().unwrap_or_else(PoisonError::into_inner);
    if let Some((_, ending)) = endings.iter().find(|(known, _)| *known == signal) {
        return Ok(Arc::clone(ending));
    }
    let ending = Arc::new(AtomicBool::new(true));
    flag::register_conditional_default(signal, Arc::clone(&ending))?;
    endings.push((signal, Arc::clone(&ending)));
    Ok(ending)
}

/// `command` in a process group of its own, so the terminal's Ctrl-C reaches mrk alone and mrk ends the whole build.
/// It reads no input: a background group reading the terminal would stop, and mrk would wait for it forever.
pub fn spawn_alone(command: &mut Command) -> io::Result<Child> {
    use std::os::unix::process::CommandExt;
    command.process_group(0).stdin(Stdio::null()).spawn()
}

/// Waits for a child `spawn_alone` started; once a held signal arrives, ends its whole process group, `SIGTERM` then
/// `SIGKILL` after `GRACE`, and waits until no process of it is left, so nothing it started still writes afterwards.
pub fn wait_ending(child: &mut Child, held: &Held) -> io::Result<Result<ExitStatus, Interrupted>> {
    let group = Pid::from_child(child);
    loop {
        if let Some(interrupted) = held.arrived() {
            end_group(child, group)?;
            return Ok(Err(interrupted));
        }
        if let Some(status) = child.try_wait()? {
            return Ok(Ok(status));
        }
        thread::sleep(POLL);
    }
}

fn end_group(child: &mut Child, group: Pid) -> io::Result<()> {
    let _ = kill_process_group(group, Signal::TERM);
    let given_up = Instant::now() + GRACE;
    while is_alive(group) && Instant::now() < given_up {
        let _ = child.try_wait()?;
        thread::sleep(POLL);
    }
    let _ = kill_process_group(group, Signal::KILL);
    child.wait()?;
    while is_alive(group) {
        thread::sleep(POLL);
    }
    Ok(())
}

fn is_alive(group: Pid) -> bool {
    test_kill_process_group(group).is_ok()
}

#[cfg(test)]
mod tests {
    #![allow(clippy::unwrap_used)]
    use std::io::Write;

    use signal_hook::consts::{SIGUSR1, SIGUSR2};

    use super::*;

    #[test]
    fn a_held_signal_is_noted_instead_of_ending_mrk_however_often_it_is_held() {
        for _ in 0..2 {
            let held = Held::hold(&[SIGUSR1]).unwrap();
            assert_eq!(held.arrived(), None);

            low_level::raise(SIGUSR1).unwrap();

            assert_eq!(held.arrived(), Some(Interrupted(SIGUSR1)));
        }
    }

    #[test]
    fn the_whole_process_group_is_ended_once_a_held_signal_arrives() {
        let held = Held::hold(&[SIGUSR2]).unwrap();
        let mut shell = spawn_alone(Command::new("sh").args(["-c", "sleep 30 & sleep 30"])).unwrap();
        let group = Pid::from_child(&shell);

        low_level::raise(SIGUSR2).unwrap();

        assert_eq!(wait_ending(&mut shell, &held).unwrap(), Err(Interrupted(SIGUSR2)));
        assert!(!is_alive(group));
    }

    #[test]
    fn a_closed_terminal_and_ctrl_backslash_are_held_like_ctrl_c() {
        let held = Held::hold(&ENDING_ON_A_TERMINAL).unwrap();

        for signal in [SIGHUP, SIGQUIT] {
            low_level::raise(signal).unwrap();

            assert_eq!(held.arrived(), Some(Interrupted(signal)));
        }
    }

    #[test]
    fn a_closed_terminal_is_left_alone_when_neither_stdout_nor_stderr_is_a_terminal_as_under_nohup() {
        assert_eq!(ending_for(true), [SIGHUP, SIGINT, SIGQUIT, SIGTERM]);
        assert_eq!(ending_for(false), [SIGINT, SIGQUIT, SIGTERM]);
    }

    #[test]
    fn a_child_alone_in_its_group_reads_no_input() {
        let (input, mut writer) = io::pipe().unwrap();
        writer.write_all(b"typed\n").unwrap();
        drop(writer);
        let mut command = Command::new("sh");
        command.args(["-c", "! read line"]).stdin(input);

        assert!(spawn_alone(&mut command).unwrap().wait().unwrap().success());
    }
}