forked from rust-lang/rustup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommand.rs
56 lines (47 loc) · 1.58 KB
/
command.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
use std::{
ffi::OsStr,
fmt::Debug,
io,
process::{self, Command, ExitStatus},
};
use anyhow::{Context, Result};
use crate::errors::*;
#[tracing::instrument(level = "trace", err(level = "trace"))]
pub(crate) fn run_command_for_dir<S: AsRef<OsStr> + Debug>(
mut cmd: Command,
arg0: &str,
args: &[S],
) -> Result<ExitStatus> {
cmd.args(args);
// FIXME rust-lang/rust#32254. It's not clear to me
// when and why this is needed.
// TODO: process support for mocked file descriptor inheritance here: until
// then tests that depend on rustups stdin being inherited won't work in-process.
cmd.stdin(process::Stdio::inherit());
return exec(&mut cmd).with_context(|| RustupError::RunningCommand {
name: OsStr::new(arg0).to_owned(),
});
#[cfg(unix)]
fn exec(cmd: &mut Command) -> io::Result<ExitStatus> {
use std::os::unix::prelude::*;
Err(cmd.exec())
}
#[cfg(windows)]
fn exec(cmd: &mut Command) -> io::Result<ExitStatus> {
use windows_sys::Win32::Foundation::{BOOL, FALSE, TRUE};
use windows_sys::Win32::System::Console::SetConsoleCtrlHandler;
unsafe extern "system" fn ctrlc_handler(_: u32) -> BOOL {
// Do nothing. Let the child process handle it.
TRUE
}
unsafe {
if SetConsoleCtrlHandler(Some(ctrlc_handler), TRUE) == FALSE {
return Err(io::Error::new(
io::ErrorKind::Other,
"Unable to set console handler",
));
}
}
cmd.status()
}
}