Skip to content

Commit 78d8d04

Browse files
committed
feat: handle more exit signal
1 parent 769ea15 commit 78d8d04

File tree

3 files changed

+15
-50
lines changed

3 files changed

+15
-50
lines changed

Cargo.lock

Lines changed: 1 addition & 48 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ authors = ["eatradish <sakiiily@aosc.io>"]
1212
# Cli
1313
clap = { version = "4.5.19", features = ["cargo", "wrap_help", "color", "derive", "env", "string"] }
1414
anyhow = "1.0.89"
15-
ctrlc = "3.5"
1615
dialoguer = "0.12.0"
1716
tabled = { version = "0.20", features = ["ansi"] }
1817
tokio = "1.46.0"
@@ -52,6 +51,7 @@ once_cell = "1.21"
5251
itertools = "0.14.0"
5352
clap-i18n-richformatter = "0.1.0"
5453
debversion = "0.5"
54+
signal-hook = "0.3.18"
5555

5656
# oma crates
5757
oma-utils = { path = "./oma-utils", features = ["dbus", "human-bytes"] }

src/main.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use std::env::{self, args};
22
use std::ffi::CString;
33
use std::fs::{create_dir_all, read_dir, remove_file};
44
use std::io::{self, IsTerminal, stderr, stdin};
5+
use std::os::raw::c_int;
56
use std::path::{Path, PathBuf};
67

78
use std::process::{Command, exit};
@@ -37,6 +38,7 @@ use oma_utils::dbus::{create_dbus_connection, get_another_oma_status};
3738
use oma_utils::{OsRelease, is_termux};
3839
use reqwest::Client;
3940
use rustix::stdio::stdout;
41+
use signal_hook::iterator::Signals;
4042
use subcommand::utils::{LockError, is_terminal};
4143
use tokio::runtime::Runtime;
4244
use tracing::{debug, error, info, warn};
@@ -55,6 +57,9 @@ use crate::config::Config;
5557
use crate::error::Chain;
5658
use crate::install_progress::osc94_progress;
5759
use crate::subcommand::*;
60+
use signal_hook::consts::*;
61+
62+
const SIGNALS: &[c_int] = &[SIGTERM, SIGQUIT, SIGINT, SIGTSTP, SIGWINCH, SIGHUP, SIGCONT];
5863

5964
static NOT_DISPLAY_ABORT: AtomicBool = AtomicBool::new(false);
6065
static LOCKED: AtomicBool = AtomicBool::new(false);
@@ -172,7 +177,14 @@ fn main() {
172177
.completer("oma")
173178
.complete();
174179

175-
ctrlc::set_handler(single_handler).expect("oma could not initialize SIGINT handler.");
180+
thread::spawn(|| {
181+
let Ok(mut sigs) = Signals::new(SIGNALS) else {
182+
panic!("Failed to set signal handler.")
183+
};
184+
for _ in &mut sigs {
185+
single_handler();
186+
}
187+
});
176188

177189
// 要适配额外的插件子命令,所以这里要保留 matches
178190
let (matches, oma) = parse_args();

0 commit comments

Comments
 (0)