Skip to content

Commit f557bc4

Browse files
authored
Merge pull request #509 from GuillaumeGomez/signal-swallowing
Stop swallowing signals in build_system when running sub-commands
2 parents 0f87072 + 766f59d commit f557bc4

File tree

1 file changed

+35
-15
lines changed

1 file changed

+35
-15
lines changed

build_system/src/utils.rs

+35-15
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,42 @@
11
use std::collections::HashMap;
2+
#[cfg(unix)]
3+
use std::ffi::c_int;
24
use std::ffi::OsStr;
35
use std::fmt::Debug;
46
use std::fs;
7+
#[cfg(unix)]
8+
use std::os::unix::process::ExitStatusExt;
59
use std::path::{Path, PathBuf};
610
use std::process::{Command, ExitStatus, Output};
711

12+
#[cfg(unix)]
13+
extern "C" {
14+
fn raise(signal: c_int) -> c_int;
15+
}
16+
17+
fn exec_command(
18+
input: &[&dyn AsRef<OsStr>],
19+
cwd: Option<&Path>,
20+
env: Option<&HashMap<String, String>>,
21+
) -> Result<ExitStatus, String> {
22+
let status = get_command_inner(input, cwd, env)
23+
.spawn()
24+
.map_err(|e| command_error(input, &cwd, e))?
25+
.wait()
26+
.map_err(|e| command_error(input, &cwd, e))?;
27+
#[cfg(unix)]
28+
{
29+
if let Some(signal) = status.signal() {
30+
unsafe {
31+
raise(signal as _);
32+
}
33+
// In case the signal didn't kill the current process.
34+
return Err(command_error(input, &cwd, format!("Process received signal {}", signal)));
35+
}
36+
}
37+
Ok(status)
38+
}
39+
840
fn get_command_inner(
941
input: &[&dyn AsRef<OsStr>],
1042
cwd: Option<&Path>,
@@ -89,11 +121,7 @@ pub fn run_command_with_output(
89121
input: &[&dyn AsRef<OsStr>],
90122
cwd: Option<&Path>,
91123
) -> Result<(), String> {
92-
let exit_status = get_command_inner(input, cwd, None)
93-
.spawn()
94-
.map_err(|e| command_error(input, &cwd, e))?
95-
.wait()
96-
.map_err(|e| command_error(input, &cwd, e))?;
124+
let exit_status = exec_command(input, cwd, None)?;
97125
check_exit_status(input, cwd, exit_status, None, true)?;
98126
Ok(())
99127
}
@@ -103,11 +131,7 @@ pub fn run_command_with_output_and_env(
103131
cwd: Option<&Path>,
104132
env: Option<&HashMap<String, String>>,
105133
) -> Result<(), String> {
106-
let exit_status = get_command_inner(input, cwd, env)
107-
.spawn()
108-
.map_err(|e| command_error(input, &cwd, e))?
109-
.wait()
110-
.map_err(|e| command_error(input, &cwd, e))?;
134+
let exit_status = exec_command(input, cwd, env)?;
111135
check_exit_status(input, cwd, exit_status, None, true)?;
112136
Ok(())
113137
}
@@ -117,11 +141,7 @@ pub fn run_command_with_output_and_env_no_err(
117141
cwd: Option<&Path>,
118142
env: Option<&HashMap<String, String>>,
119143
) -> Result<(), String> {
120-
let exit_status = get_command_inner(input, cwd, env)
121-
.spawn()
122-
.map_err(|e| command_error(input, &cwd, e))?
123-
.wait()
124-
.map_err(|e| command_error(input, &cwd, e))?;
144+
let exit_status = exec_command(input, cwd, env)?;
125145
check_exit_status(input, cwd, exit_status, None, false)?;
126146
Ok(())
127147
}

0 commit comments

Comments
 (0)