Skip to content

Commit 3ad6191

Browse files
committed
process: Split signal into mod
1 parent e99af00 commit 3ad6191

File tree

3 files changed

+169
-228
lines changed

3 files changed

+169
-228
lines changed

process/kill.rs

Lines changed: 13 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -7,103 +7,14 @@
77
// SPDX-License-Identifier: MIT
88
//
99

10-
use gettextrs::{bind_textdomain_codeset, setlocale, textdomain, LocaleCategory};
11-
use plib::PROJECT_NAME;
12-
13-
#[cfg(target_os = "macos")]
14-
const SIGLIST: [(&str, u32); 31] = [
15-
("HUP", 1),
16-
("INT", 2),
17-
("QUIT", 3),
18-
("ILL", 4),
19-
("TRAP", 5),
20-
("ABRT", 6),
21-
("EMT", 7),
22-
("FPE", 8),
23-
("KILL", 9),
24-
("BUS", 10),
25-
("SEGV", 11),
26-
("SYS", 12),
27-
("PIPE", 13),
28-
("ALRM", 14),
29-
("TERM", 15),
30-
("URG", 16),
31-
("STOP", 17),
32-
("TSTP", 18),
33-
("CONT", 19),
34-
("CHLD", 20),
35-
("TTIN", 21),
36-
("TTOU", 22),
37-
("IO", 23),
38-
("XCPU", 24),
39-
("XFSZ", 25),
40-
("VTALRM", 26),
41-
("PROF", 27),
42-
("WINCH", 28),
43-
("INFO", 29),
44-
("USR1", 30),
45-
("USR2", 31),
46-
];
47-
48-
#[cfg(target_os = "linux")]
49-
const SIGLIST: [(&str, u32); 32] = [
50-
("HUP", 1),
51-
("INT", 2),
52-
("QUIT", 3),
53-
("ILL", 4),
54-
("TRAP", 5),
55-
("ABRT", 6),
56-
("IOT", 6),
57-
("BUS", 7),
58-
("FPE", 8),
59-
("KILL", 9),
60-
("USR1", 10),
61-
("SEGV", 11),
62-
("USR2", 12),
63-
("PIPE", 13),
64-
("ALRM", 14),
65-
("TERM", 15),
66-
("STKFLT", 16),
67-
("CHLD", 17),
68-
("CONT", 18),
69-
("STOP", 19),
70-
("TSTP", 20),
71-
("TTIN", 21),
72-
("TTOU", 22),
73-
("URG", 23),
74-
("XCPU", 24),
75-
("XFSZ", 25),
76-
("VTALRM", 26),
77-
("PROF", 27),
78-
("WINCH", 28),
79-
("IO", 29),
80-
("PWR", 30),
81-
("SYS", 31),
82-
];
83-
84-
fn siglist_get(name: &str) -> Option<u32> {
85-
for (signame, signo) in SIGLIST.iter() {
86-
if *signame == name {
87-
return Some(*signo);
88-
}
89-
}
10+
mod signal;
9011

91-
None
92-
}
12+
use gettextrs::{bind_textdomain_codeset, setlocale, textdomain, LocaleCategory};
9313

94-
fn lookup_signum(signame: &str) -> Result<u32, &'static str> {
95-
if signame == "0" {
96-
Ok(0)
97-
} else {
98-
match siglist_get(signame.to_uppercase().as_str()) {
99-
Some(sig_no) => Ok(sig_no),
100-
None => Err("Unknown signal name"),
101-
}
102-
}
103-
}
14+
use crate::signal::{list_signals, lookup_signum};
10415

10516
enum ConfigMode {
106-
Signal(u32),
17+
Signal(i32),
10718
List,
10819
}
10920

@@ -114,7 +25,7 @@ struct Config {
11425

11526
fn parse_cmdline() -> Result<Config, &'static str> {
11627
let mut pids = Vec::new();
117-
let mut mode = ConfigMode::Signal(libc::SIGTERM as u32);
28+
let mut mode = ConfigMode::Signal(libc::SIGTERM);
11829
let mut in_args = true;
11930
let mut in_s_arg = false;
12031
for arg in std::env::args().skip(1) {
@@ -130,7 +41,7 @@ fn parse_cmdline() -> Result<Config, &'static str> {
13041
} else if arg == "--" {
13142
in_args = false;
13243
} else if let Some(st) = arg.strip_prefix("-") {
133-
let sig_no = match st.parse::<u32>() {
44+
let sig_no: i32 = match st.parse() {
13445
Ok(signo) => signo,
13546
Err(_) => lookup_signum(st)?,
13647
};
@@ -157,19 +68,7 @@ fn parse_cmdline() -> Result<Config, &'static str> {
15768
Ok(Config { mode, pids })
15869
}
15970

160-
fn list_signals() -> u32 {
161-
let mut output = String::new();
162-
for (name, _) in SIGLIST.iter() {
163-
output.push_str(name);
164-
output.push(' ');
165-
}
166-
167-
println!("{}", output);
168-
169-
0
170-
}
171-
172-
fn send_signal(prog_cfg: &Config, sig_no: u32) -> u32 {
71+
fn send_signal(prog_cfg: &Config, sig_no: i32) -> u32 {
17372
let mut exit_code = 0;
17473

17574
for pid in &prog_cfg.pids {
@@ -186,13 +85,16 @@ fn send_signal(prog_cfg: &Config, sig_no: u32) -> u32 {
18685

18786
fn main() -> Result<(), Box<dyn std::error::Error>> {
18887
setlocale(LocaleCategory::LcAll, "");
189-
textdomain(PROJECT_NAME)?;
190-
bind_textdomain_codeset(PROJECT_NAME, "UTF-8")?;
88+
textdomain(env!("PROJECT_NAME"))?;
89+
bind_textdomain_codeset(env!("PROJECT_NAME"), "UTF-8")?;
19190

19291
let prog_cfg = parse_cmdline()?;
19392

19493
let exit_code = match prog_cfg.mode {
195-
ConfigMode::List => list_signals(),
94+
ConfigMode::List => {
95+
list_signals();
96+
0
97+
}
19698
ConfigMode::Signal(sig_no) => send_signal(&prog_cfg, sig_no),
19799
};
198100

process/signal.rs

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
//
2+
// Copyright (c) 2024 Jeff Garzik
3+
//
4+
// This file is part of the posixutils-rs project covered under
5+
// the MIT License. For the full license text, please see the LICENSE
6+
// file in the root directory of this project.
7+
// SPDX-License-Identifier: MIT
8+
//
9+
10+
// `kill` and `timeout` bins
11+
#![allow(dead_code)]
12+
13+
pub fn list_signals() {
14+
let mut output = String::new();
15+
for (name, _) in SIGLIST.iter() {
16+
output.push_str(name);
17+
output.push(' ');
18+
}
19+
20+
println!("{}", output);
21+
}
22+
23+
/// Parses [str] into [Signal].
24+
///
25+
/// # Arguments
26+
///
27+
/// * `s` - [str] that represents the signal name.
28+
///
29+
/// # Returns
30+
///
31+
/// Returns the parsed [Signal] value.
32+
///
33+
/// # Errors
34+
///
35+
/// Returns an [String] error if passed invalid signal name.
36+
pub fn parse_signal(s: &str) -> Result<i32, String> {
37+
let normalized = s.trim().to_uppercase();
38+
let normalized = normalized.strip_prefix("SIG").unwrap_or(&normalized);
39+
40+
for (name, num) in SIGLIST.iter() {
41+
if name == &normalized {
42+
return Ok(*num);
43+
}
44+
}
45+
Err(format!("invalid signal name '{s}'"))
46+
}
47+
48+
pub fn lookup_signum(signame: &str) -> Result<i32, &'static str> {
49+
if signame == "0" {
50+
Ok(0)
51+
} else {
52+
match siglist_get(signame.to_uppercase().as_str()) {
53+
Some(sig_no) => Ok(sig_no),
54+
None => Err("Unknown signal name"),
55+
}
56+
}
57+
}
58+
59+
fn siglist_get(name: &str) -> Option<i32> {
60+
for (signame, signo) in SIGLIST.iter() {
61+
if *signame == name {
62+
return Some(*signo);
63+
}
64+
}
65+
66+
None
67+
}
68+
69+
#[cfg(target_os = "macos")]
70+
const SIGLIST: [(&str, i32); 31] = [
71+
("HUP", 1),
72+
("INT", 2),
73+
("QUIT", 3),
74+
("ILL", 4),
75+
("TRAP", 5),
76+
("ABRT", 6),
77+
("EMT", 7),
78+
("FPE", 8),
79+
("KILL", 9),
80+
("BUS", 10),
81+
("SEGV", 11),
82+
("SYS", 12),
83+
("PIPE", 13),
84+
("ALRM", 14),
85+
("TERM", 15),
86+
("URG", 16),
87+
("STOP", 17),
88+
("TSTP", 18),
89+
("CONT", 19),
90+
("CHLD", 20),
91+
("TTIN", 21),
92+
("TTOU", 22),
93+
("IO", 23),
94+
("XCPU", 24),
95+
("XFSZ", 25),
96+
("VTALRM", 26),
97+
("PROF", 27),
98+
("WINCH", 28),
99+
("INFO", 29),
100+
("USR1", 30),
101+
("USR2", 31),
102+
];
103+
104+
#[cfg(target_os = "linux")]
105+
const SIGLIST: [(&str, i32); 32] = [
106+
("HUP", 1),
107+
("INT", 2),
108+
("QUIT", 3),
109+
("ILL", 4),
110+
("TRAP", 5),
111+
("ABRT", 6),
112+
("IOT", 6),
113+
("BUS", 7),
114+
("FPE", 8),
115+
("KILL", 9),
116+
("USR1", 10),
117+
("SEGV", 11),
118+
("USR2", 12),
119+
("PIPE", 13),
120+
("ALRM", 14),
121+
("TERM", 15),
122+
("STKFLT", 16),
123+
("CHLD", 17),
124+
("CONT", 18),
125+
("STOP", 19),
126+
("TSTP", 20),
127+
("TTIN", 21),
128+
("TTOU", 22),
129+
("URG", 23),
130+
("XCPU", 24),
131+
("XFSZ", 25),
132+
("VTALRM", 26),
133+
("PROF", 27),
134+
("WINCH", 28),
135+
("IO", 29),
136+
("PWR", 30),
137+
("SYS", 31),
138+
];

0 commit comments

Comments
 (0)