summaryrefslogtreecommitdiffstats
path: root/src/signal.rs
blob: 80d571be6043cc8d86537dd2b03d380943e0e3fb (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
/*
 * This file is part of agent686
 * Copyright (C) 2026 Salmonik <salmon@2137697.xyz>
 * SPDX-License-Identifier: GPL-3.0-or-later
 * https://git.2137697.xyz/agent686
 */

use std::sync::atomic::{AtomicBool, Ordering};

/// Set by the first Ctrl-C. Cleared between turns.
pub static CANCEL: AtomicBool = AtomicBool::new(false);

extern "C" fn handler(_: libc::c_int) {
    // First Ctrl-C aborts the turn. A second one is taken to mean "I really mean
    // it" and leaves immediately; `_exit` is async-signal-safe, `exit` is not.
    if CANCEL.swap(true, Ordering::SeqCst) {
        unsafe { libc::_exit(130) }
    }
}

pub fn install() {
    let handler: extern "C" fn(libc::c_int) = handler;
    unsafe {
        libc::signal(libc::SIGINT, handler as libc::sighandler_t);
    }
}