Skip to content

Commit

Permalink
Merge pull request rust-lang#509 from GuillaumeGomez/signal-swallowing
Browse files Browse the repository at this point in the history
Stop swallowing signals in build_system when running sub-commands
  • Loading branch information
antoyo authored May 1, 2024
2 parents 0f87072 + 766f59d commit f557bc4
Showing 1 changed file with 35 additions and 15 deletions.
50 changes: 35 additions & 15 deletions build_system/src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,42 @@
use std::collections::HashMap;
#[cfg(unix)]
use std::ffi::c_int;
use std::ffi::OsStr;
use std::fmt::Debug;
use std::fs;
#[cfg(unix)]
use std::os::unix::process::ExitStatusExt;
use std::path::{Path, PathBuf};
use std::process::{Command, ExitStatus, Output};

#[cfg(unix)]
extern "C" {
fn raise(signal: c_int) -> c_int;
}

fn exec_command(
input: &[&dyn AsRef<OsStr>],
cwd: Option<&Path>,
env: Option<&HashMap<String, String>>,
) -> Result<ExitStatus, String> {
let status = get_command_inner(input, cwd, env)
.spawn()
.map_err(|e| command_error(input, &cwd, e))?
.wait()
.map_err(|e| command_error(input, &cwd, e))?;
#[cfg(unix)]
{
if let Some(signal) = status.signal() {
unsafe {
raise(signal as _);
}
// In case the signal didn't kill the current process.
return Err(command_error(input, &cwd, format!("Process received signal {}", signal)));
}
}
Ok(status)
}

fn get_command_inner(
input: &[&dyn AsRef<OsStr>],
cwd: Option<&Path>,
Expand Down Expand Up @@ -89,11 +121,7 @@ pub fn run_command_with_output(
input: &[&dyn AsRef<OsStr>],
cwd: Option<&Path>,
) -> Result<(), String> {
let exit_status = get_command_inner(input, cwd, None)
.spawn()
.map_err(|e| command_error(input, &cwd, e))?
.wait()
.map_err(|e| command_error(input, &cwd, e))?;
let exit_status = exec_command(input, cwd, None)?;
check_exit_status(input, cwd, exit_status, None, true)?;
Ok(())
}
Expand All @@ -103,11 +131,7 @@ pub fn run_command_with_output_and_env(
cwd: Option<&Path>,
env: Option<&HashMap<String, String>>,
) -> Result<(), String> {
let exit_status = get_command_inner(input, cwd, env)
.spawn()
.map_err(|e| command_error(input, &cwd, e))?
.wait()
.map_err(|e| command_error(input, &cwd, e))?;
let exit_status = exec_command(input, cwd, env)?;
check_exit_status(input, cwd, exit_status, None, true)?;
Ok(())
}
Expand All @@ -117,11 +141,7 @@ pub fn run_command_with_output_and_env_no_err(
cwd: Option<&Path>,
env: Option<&HashMap<String, String>>,
) -> Result<(), String> {
let exit_status = get_command_inner(input, cwd, env)
.spawn()
.map_err(|e| command_error(input, &cwd, e))?
.wait()
.map_err(|e| command_error(input, &cwd, e))?;
let exit_status = exec_command(input, cwd, env)?;
check_exit_status(input, cwd, exit_status, None, false)?;
Ok(())
}
Expand Down

0 comments on commit f557bc4

Please sign in to comment.