-
Notifications
You must be signed in to change notification settings - Fork 772
Expand file tree
/
Copy pathsignal_handler.rs
More file actions
159 lines (137 loc) · 4.34 KB
/
signal_handler.rs
File metadata and controls
159 lines (137 loc) · 4.34 KB
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
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 signal.is_fatal() {
if self.children.is_empty() {
process::exit(signal.code());
}
if 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 {
use std::fmt::Write;
writeln!(message, "{child}: {command:?}").unwrap();
}
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,
config: &Config,
f: impl Fn(process::Child) -> io::Result<T>,
) -> (io::Result<T>, Option<Signal>) {
let mut instance = Self::instance();
let color = config.color.context().stderr();
let pfx = color.prefix();
let sfx = color.suffix();
// Print an xtrace of run commands.
if config.verbosity.grandiloquent() {
// At the highest verbosity level, print the exact command as-is.
eprintln!("{pfx}+ {command:?}{sfx}");
} else if config.verbosity.loquacious() {
// For the second highest verbosity level, reconstruct the command but don't include
// environment (can be quite noisy with many `export`ed variables).
let mut dbg_cmd = Command::new(command.get_program());
dbg_cmd.args(command.get_args());
if let Some(cwd) = command.get_current_dir() {
dbg_cmd.current_dir(cwd);
}
eprintln!("{pfx}+ {dbg_cmd:?}{sfx}");
}
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::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)
}
}