forked from rustcoreutils/posixutils-rs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkill.rs
200 lines (177 loc) · 4.47 KB
/
kill.rs
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
//
// Copyright (c) 2024 Jeff Garzik
//
// This file is part of the posixutils-rs project covered under
// the MIT License. For the full license text, please see the LICENSE
// file in the root directory of this project.
// SPDX-License-Identifier: MIT
//
use gettextrs::{bind_textdomain_codeset, setlocale, textdomain, LocaleCategory};
use plib::PROJECT_NAME;
#[cfg(target_os = "macos")]
const SIGLIST: [(&str, u32); 31] = [
("HUP", 1),
("INT", 2),
("QUIT", 3),
("ILL", 4),
("TRAP", 5),
("ABRT", 6),
("EMT", 7),
("FPE", 8),
("KILL", 9),
("BUS", 10),
("SEGV", 11),
("SYS", 12),
("PIPE", 13),
("ALRM", 14),
("TERM", 15),
("URG", 16),
("STOP", 17),
("TSTP", 18),
("CONT", 19),
("CHLD", 20),
("TTIN", 21),
("TTOU", 22),
("IO", 23),
("XCPU", 24),
("XFSZ", 25),
("VTALRM", 26),
("PROF", 27),
("WINCH", 28),
("INFO", 29),
("USR1", 30),
("USR2", 31),
];
#[cfg(target_os = "linux")]
const SIGLIST: [(&str, u32); 32] = [
("HUP", 1),
("INT", 2),
("QUIT", 3),
("ILL", 4),
("TRAP", 5),
("ABRT", 6),
("IOT", 6),
("BUS", 7),
("FPE", 8),
("KILL", 9),
("USR1", 10),
("SEGV", 11),
("USR2", 12),
("PIPE", 13),
("ALRM", 14),
("TERM", 15),
("STKFLT", 16),
("CHLD", 17),
("CONT", 18),
("STOP", 19),
("TSTP", 20),
("TTIN", 21),
("TTOU", 22),
("URG", 23),
("XCPU", 24),
("XFSZ", 25),
("VTALRM", 26),
("PROF", 27),
("WINCH", 28),
("IO", 29),
("PWR", 30),
("SYS", 31),
];
fn siglist_get(name: &str) -> Option<u32> {
for (signame, signo) in SIGLIST.iter() {
if *signame == name {
return Some(*signo);
}
}
None
}
fn lookup_signum(signame: &str) -> Result<u32, &'static str> {
if signame == "0" {
Ok(0)
} else {
match siglist_get(signame.to_uppercase().as_str()) {
Some(sig_no) => Ok(sig_no),
None => Err("Unknown signal name"),
}
}
}
enum ConfigMode {
Signal(u32),
List,
}
struct Config {
mode: ConfigMode,
pids: Vec<u32>,
}
fn parse_cmdline() -> Result<Config, &'static str> {
let mut pids = Vec::new();
let mut mode = ConfigMode::Signal(libc::SIGTERM as u32);
let mut in_args = true;
let mut in_s_arg = false;
for arg in std::env::args().skip(1) {
if in_args {
if in_s_arg {
let sig_no = lookup_signum(&arg)?;
mode = ConfigMode::Signal(sig_no);
in_s_arg = false;
} else if arg == "-s" || arg == "--signal" {
in_s_arg = true;
} else if arg == "-l" || arg == "--list" {
mode = ConfigMode::List;
} else if arg == "--" {
in_args = false;
} else if let Some(st) = arg.strip_prefix("-") {
let sig_no = match st.parse::<u32>() {
Ok(signo) => signo,
Err(_) => lookup_signum(st)?,
};
mode = ConfigMode::Signal(sig_no);
} else {
in_args = false;
}
if in_args || arg == "--" {
continue;
}
// fall through to process non-option arguments
}
match arg.parse::<u32>() {
Ok(pid) => pids.push(pid),
Err(_) => {
return Err("Invalid PID");
}
}
}
Ok(Config { mode, pids })
}
fn list_signals() -> u32 {
let mut output = String::new();
for (name, _) in SIGLIST.iter() {
output.push_str(name);
output.push(' ');
}
println!("{}", output);
0
}
fn send_signal(prog_cfg: &Config, sig_no: u32) -> u32 {
let mut exit_code = 0;
for pid in &prog_cfg.pids {
let res = unsafe { libc::kill(*pid as libc::pid_t, sig_no as i32) };
if res != 0 {
let err = std::io::Error::last_os_error();
eprintln!("kill pid {}: {}", pid, err);
exit_code = 1;
}
}
exit_code
}
fn main() -> Result<(), Box<dyn std::error::Error>> {
setlocale(LocaleCategory::LcAll, "");
textdomain(PROJECT_NAME)?;
bind_textdomain_codeset(PROJECT_NAME, "UTF-8")?;
let prog_cfg = parse_cmdline()?;
let exit_code = match prog_cfg.mode {
ConfigMode::List => list_signals(),
ConfigMode::Signal(sig_no) => send_signal(&prog_cfg, sig_no),
};
std::process::exit(exit_code as i32)
}