Skip to content

Commit 91f8c84

Browse files
committed
feat: handle SIGTERM and SIGHUP
1 parent 8f3e9df commit 91f8c84

File tree

3 files changed

+40
-84
lines changed

3 files changed

+40
-84
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: 38 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,7 @@
1-
use std::env::{self, args};
2-
use std::ffi::CString;
3-
use std::fs::{create_dir_all, read_dir, remove_file};
4-
use std::io::{self, IsTerminal, stderr, stdin};
5-
use std::path::{Path, PathBuf};
6-
7-
use std::process::{Command, exit};
8-
use std::sync::{LazyLock, OnceLock};
9-
use std::thread;
10-
use std::time::{Duration, SystemTime, UNIX_EPOCH};
11-
12-
mod args;
13-
mod config;
14-
mod error;
15-
mod install_progress;
16-
mod lang;
17-
mod path_completions;
18-
mod pb;
19-
mod subcommand;
20-
mod table;
21-
mod tui;
22-
mod utils;
23-
1+
use crate::config::Config;
2+
use crate::error::Chain;
3+
use crate::install_progress::osc94_progress;
4+
use crate::subcommand::*;
245
use args::{CliExecuter, OhManagerAilurus};
256
use clap::builder::FalseyValueParser;
267
use clap::{ArgAction, ArgMatches, Args, ColorChoice, CommandFactory, FromArgMatches, arg};
@@ -29,14 +10,27 @@ use clap_i18n_richformatter::{ClapI18nRichFormatter, init_clap_rich_formatter_lo
2910
use error::OutputError;
3011
use i18n_embed::{DesktopLanguageRequester, Localizer};
3112
use lang::LANGUAGE_LOADER;
13+
use libc::SIGHUP;
3214
use oma_console::OmaLayer;
15+
use oma_console::console;
3316
use oma_console::print::{OmaColorFormat, termbg};
3417
use oma_console::writer::{MessageType, Writer, writeln_inner};
3518
use oma_pm::apt::AptConfig;
3619
use oma_utils::dbus::{create_dbus_connection, get_another_oma_status};
3720
use oma_utils::{OsRelease, is_termux};
3821
use reqwest::Client;
39-
use rustix::stdio::stdout;
22+
use signal_hook::consts::{SIGINT, SIGTERM};
23+
use signal_hook::iterator::Signals;
24+
use std::env::{self, args};
25+
use std::ffi::CString;
26+
use std::fs::{create_dir_all, read_dir, remove_file};
27+
use std::io::{self, IsTerminal, stderr, stdin, stdout};
28+
use std::path::{Path, PathBuf};
29+
use std::process::{Command, exit};
30+
use std::sync::atomic::{AtomicBool, Ordering};
31+
use std::sync::{LazyLock, OnceLock};
32+
use std::thread;
33+
use std::time::{Duration, SystemTime, UNIX_EPOCH};
4034
use subcommand::utils::{LockError, is_terminal};
4135
use tokio::runtime::Runtime;
4236
use tracing::{debug, error, info, warn};
@@ -47,14 +41,17 @@ use tracing_subscriber::{EnvFilter, Layer, fmt};
4741
use tui::Tui;
4842
use utils::{is_root, is_ssh_from_loginctl};
4943

50-
use std::sync::atomic::{AtomicBool, Ordering};
51-
52-
use oma_console::console;
53-
54-
use crate::config::Config;
55-
use crate::error::Chain;
56-
use crate::install_progress::osc94_progress;
57-
use crate::subcommand::*;
44+
mod args;
45+
mod config;
46+
mod error;
47+
mod install_progress;
48+
mod lang;
49+
mod path_completions;
50+
mod pb;
51+
mod subcommand;
52+
mod table;
53+
mod tui;
54+
mod utils;
5855

5956
static NOT_DISPLAY_ABORT: AtomicBool = AtomicBool::new(false);
6057
static LOCKED: AtomicBool = AtomicBool::new(false);
@@ -172,7 +169,15 @@ fn main() {
172169
.completer("oma")
173170
.complete();
174171

175-
ctrlc::set_handler(signal_handler).expect("oma could not initialize SIGINT handler.");
172+
thread::spawn(|| {
173+
let mut sigs =
174+
Signals::new([SIGTERM, SIGINT, SIGHUP]).expect("Failed to set signal handler");
175+
176+
for signal in &mut sigs {
177+
signal_handler();
178+
std::process::exit(128 + signal)
179+
}
180+
});
176181

177182
// 要适配额外的插件子命令,所以这里要保留 matches
178183
let (matches, oma) = parse_args();
@@ -703,6 +708,4 @@ fn signal_handler() {
703708
if !not_display_abort {
704709
info!("{}", fl!("user-aborted-op"));
705710
}
706-
707-
std::process::exit(130);
708711
}

0 commit comments

Comments
 (0)