Skip to content

Commit 485e43f

Browse files
authored
Merge pull request #698 from GuillaumeGomez/signal
Raise command signal to the current process
2 parents f518ec5 + d14a49d commit 485e43f

File tree

2 files changed

+27
-24
lines changed

2 files changed

+27
-24
lines changed

build_system/src/rust_tools.rs

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
use std::collections::HashMap;
22
use std::ffi::OsStr;
3+
#[cfg(unix)]
4+
use std::os::unix::process::CommandExt;
35
use std::path::PathBuf;
46

57
use crate::config::ConfigInfo;
6-
use crate::utils::{
7-
get_toolchain, run_command_with_output_and_env_no_err, rustc_toolchain_version_info,
8-
rustc_version_info,
9-
};
8+
use crate::utils::{get_toolchain, rustc_toolchain_version_info, rustc_version_info};
109

1110
fn args(command: &str) -> Result<Option<Vec<String>>, String> {
1211
// We skip the binary and the "cargo"/"rustc" option.
@@ -97,6 +96,26 @@ impl RustcTools {
9796
}
9897
}
9998

99+
fn exec(input: &[&dyn AsRef<OsStr>], env: &HashMap<String, String>) -> Result<(), String> {
100+
#[cfg(unix)]
101+
{
102+
// We use `exec` to call the `execvp` syscall instead of creating a new process where the
103+
// command will be executed because very few signals can actually kill a current process,
104+
// so if segmentation fault (SIGSEGV signal) happens and we raise to the current process,
105+
// it will simply do nothing and we won't have the nice error message for the shell.
106+
let error = crate::utils::get_command_inner(input, None, Some(env)).exec();
107+
eprintln!("execvp syscall failed: {error:?}");
108+
std::process::exit(1);
109+
}
110+
#[cfg(not(unix))]
111+
{
112+
if crate::utils::run_command_with_output_and_env_no_err(input, None, Some(env)).is_err() {
113+
std::process::exit(1);
114+
}
115+
Ok(())
116+
}
117+
}
118+
100119
pub fn run_cargo() -> Result<(), String> {
101120
let Some(mut tools) = RustcTools::new("cargo")? else { return Ok(()) };
102121
let rustflags = tools.env.get("RUSTFLAGS").cloned().unwrap_or_default();
@@ -105,11 +124,7 @@ pub fn run_cargo() -> Result<(), String> {
105124
for arg in &tools.args {
106125
command.push(arg);
107126
}
108-
if run_command_with_output_and_env_no_err(&command, None, Some(&tools.env)).is_err() {
109-
std::process::exit(1);
110-
}
111-
112-
Ok(())
127+
exec(&command, &tools.env)
113128
}
114129

115130
pub fn run_rustc() -> Result<(), String> {
@@ -118,8 +133,5 @@ pub fn run_rustc() -> Result<(), String> {
118133
for arg in &tools.args {
119134
command.push(arg);
120135
}
121-
if run_command_with_output_and_env_no_err(&command, None, Some(&tools.env)).is_err() {
122-
std::process::exit(1);
123-
}
124-
Ok(())
136+
exec(&command, &tools.env)
125137
}

build_system/src/utils.rs

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,12 @@
11
use std::collections::HashMap;
22
use std::ffi::OsStr;
3-
#[cfg(unix)]
4-
use std::ffi::c_int;
53
use std::fmt::Debug;
64
use std::fs;
75
#[cfg(unix)]
86
use std::os::unix::process::ExitStatusExt;
97
use std::path::{Path, PathBuf};
108
use std::process::{Command, ExitStatus, Output};
119

12-
#[cfg(unix)]
13-
unsafe extern "C" {
14-
fn raise(signal: c_int) -> c_int;
15-
}
16-
1710
fn exec_command(
1811
input: &[&dyn AsRef<OsStr>],
1912
cwd: Option<&Path>,
@@ -27,17 +20,14 @@ fn exec_command(
2720
#[cfg(unix)]
2821
{
2922
if let Some(signal) = status.signal() {
30-
unsafe {
31-
raise(signal as _);
32-
}
3323
// In case the signal didn't kill the current process.
3424
return Err(command_error(input, &cwd, format!("Process received signal {}", signal)));
3525
}
3626
}
3727
Ok(status)
3828
}
3929

40-
fn get_command_inner(
30+
pub(crate) fn get_command_inner(
4131
input: &[&dyn AsRef<OsStr>],
4232
cwd: Option<&Path>,
4333
env: Option<&HashMap<String, String>>,
@@ -136,6 +126,7 @@ pub fn run_command_with_output_and_env(
136126
Ok(())
137127
}
138128

129+
#[cfg(not(unix))]
139130
pub fn run_command_with_output_and_env_no_err(
140131
input: &[&dyn AsRef<OsStr>],
141132
cwd: Option<&Path>,

0 commit comments

Comments
 (0)