-
Notifications
You must be signed in to change notification settings - Fork 531
/
Copy pathsignal_handler.rs
147 lines (127 loc) · 3.78 KB
/
signal_handler.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
use super::*;
pub(crate) struct SignalHandler {
caught: Option<Signal>,
children: BTreeMap<i32, Command>,
verbosity: Verbosity,
}
impl SignalHandler {
pub(crate) fn install(verbosity: Verbosity) -> RunResult<'static> {
let mut instance = Self::instance();
instance.verbosity = verbosity;
Platform::install_signal_handler(|signal| Self::instance().interrupt(signal))
}
pub(crate) fn instance() -> MutexGuard<'static, Self> {
static INSTANCE: Mutex<SignalHandler> = Mutex::new(SignalHandler::new());
match INSTANCE.lock() {
Ok(guard) => guard,
Err(poison_error) => {
eprintln!(
"{}",
Error::Internal {
message: format!("signal handler mutex poisoned: {poison_error}"),
}
.color_display(Color::auto().stderr())
);
process::exit(EXIT_FAILURE);
}
}
}
const fn new() -> Self {
Self {
caught: None,
children: BTreeMap::new(),
verbosity: Verbosity::default(),
}
}
fn interrupt(&mut self, signal: Signal) {
if self.children.is_empty() {
process::exit(signal.code().unwrap_or(1));
}
#[cfg(any(
target_os = "dragonfly",
target_os = "freebsd",
target_os = "ios",
target_os = "macos",
target_os = "netbsd",
target_os = "openbsd",
))]
if signal != Signal::Info && self.caught.is_none() {
self.caught = Some(signal);
}
match signal {
// SIGHUP, SIGINT, and SIGQUIT are normally sent on terminal close,
// ctrl-c, and ctrl-\, respectively, and are sent to all processes in the
// foreground process group. this includes child processes, so we ignore
// the signal and wait for them to exit
Signal::Hangup | Signal::Interrupt | Signal::Quit => {}
#[cfg(any(
target_os = "dragonfly",
target_os = "freebsd",
target_os = "ios",
target_os = "macos",
target_os = "netbsd",
target_os = "openbsd",
))]
Signal::Info => {
let id = process::id();
if self.children.is_empty() {
eprintln!("just {id}: no child processes");
} else {
let n = self.children.len();
let mut message = format!(
"just {id}: {n} child {}:\n",
if n == 1 { "process" } else { "processes" }
);
for (&child, command) in &self.children {
message.push_str(&format!("{child}: {command:?}\n"));
}
eprint!("{message}");
}
}
// SIGTERM is the default signal sent by kill. forward it to child
// processes and wait for them to exit
Signal::Terminate =>
{
#[cfg(not(windows))]
for &child in self.children.keys() {
if self.verbosity.loquacious() {
eprintln!("just: sending SIGTERM to child process {child}");
}
nix::sys::signal::kill(
nix::unistd::Pid::from_raw(child),
Some(Signal::Terminate.into()),
)
.ok();
}
}
}
}
pub(crate) fn spawn<T>(
mut command: Command,
f: impl Fn(process::Child) -> io::Result<T>,
) -> (io::Result<T>, Option<Signal>) {
let mut instance = Self::instance();
let child = match command.spawn() {
Err(err) => return (Err(err), None),
Ok(child) => child,
};
let pid = match child.id().try_into() {
Err(err) => {
return (
Err(io::Error::new(
io::ErrorKind::Other,
format!("invalid child PID: {err}"),
)),
None,
)
}
Ok(pid) => pid,
};
instance.children.insert(pid, command);
drop(instance);
let result = f(child);
let mut instance = Self::instance();
instance.children.remove(&pid);
(result, instance.caught)
}
}