Skip to content

Commit b4f4b65

Browse files
committed
Auto merge of #38098 - luser:windows-commandext, r=alexcrichton
Add std::os::windows::process::CommandExt. Fixes #37827 This adds a CommandExt trait for Windows along with an implementation of it for std::process::Command with methods to set the process creation flags that are passed to CreateProcess.
2 parents 6bc551a + e6975e9 commit b4f4b65

File tree

3 files changed

+84
-2
lines changed

3 files changed

+84
-2
lines changed

src/libstd/process.rs

+58
Original file line numberDiff line numberDiff line change
@@ -1167,4 +1167,62 @@ mod tests {
11671167
Ok(_) => panic!(),
11681168
}
11691169
}
1170+
1171+
/// Test that process creation flags work by debugging a process.
1172+
/// Other creation flags make it hard or impossible to detect
1173+
/// behavioral changes in the process.
1174+
#[test]
1175+
#[cfg(windows)]
1176+
fn test_creation_flags() {
1177+
use os::windows::process::CommandExt;
1178+
use sys::c::{BOOL, DWORD, INFINITE};
1179+
#[repr(C, packed)]
1180+
struct DEBUG_EVENT {
1181+
pub event_code: DWORD,
1182+
pub process_id: DWORD,
1183+
pub thread_id: DWORD,
1184+
// This is a union in the real struct, but we don't
1185+
// need this data for the purposes of this test.
1186+
pub _junk: [u8; 164],
1187+
}
1188+
1189+
extern "system" {
1190+
fn WaitForDebugEvent(lpDebugEvent: *mut DEBUG_EVENT, dwMilliseconds: DWORD) -> BOOL;
1191+
fn ContinueDebugEvent(dwProcessId: DWORD, dwThreadId: DWORD,
1192+
dwContinueStatus: DWORD) -> BOOL;
1193+
}
1194+
1195+
const DEBUG_PROCESS: DWORD = 1;
1196+
const EXIT_PROCESS_DEBUG_EVENT: DWORD = 5;
1197+
const DBG_EXCEPTION_NOT_HANDLED: DWORD = 0x80010001;
1198+
1199+
let mut child = Command::new("cmd")
1200+
.creation_flags(DEBUG_PROCESS)
1201+
.stdin(Stdio::piped()).spawn().unwrap();
1202+
child.stdin.take().unwrap().write_all(b"exit\r\n").unwrap();
1203+
let mut events = 0;
1204+
let mut event = DEBUG_EVENT {
1205+
event_code: 0,
1206+
process_id: 0,
1207+
thread_id: 0,
1208+
_junk: [0; 164],
1209+
};
1210+
loop {
1211+
if unsafe { WaitForDebugEvent(&mut event as *mut DEBUG_EVENT, INFINITE) } == 0 {
1212+
panic!("WaitForDebugEvent failed!");
1213+
}
1214+
events += 1;
1215+
1216+
if event.event_code == EXIT_PROCESS_DEBUG_EVENT {
1217+
break;
1218+
}
1219+
1220+
if unsafe { ContinueDebugEvent(event.process_id,
1221+
event.thread_id,
1222+
DBG_EXCEPTION_NOT_HANDLED) } == 0 {
1223+
panic!("ContinueDebugEvent failed!");
1224+
}
1225+
}
1226+
assert!(events > 0);
1227+
}
11701228
}

src/libstd/sys/windows/ext/process.rs

+20-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
use os::windows::io::{FromRawHandle, RawHandle, AsRawHandle, IntoRawHandle};
1616
use process;
1717
use sys;
18-
use sys_common::{AsInner, FromInner, IntoInner};
18+
use sys_common::{AsInnerMut, AsInner, FromInner, IntoInner};
1919

2020
#[stable(feature = "process_extensions", since = "1.2.0")]
2121
impl FromRawHandle for process::Stdio {
@@ -97,3 +97,22 @@ impl ExitStatusExt for process::ExitStatus {
9797
process::ExitStatus::from_inner(From::from(raw))
9898
}
9999
}
100+
101+
/// Windows-specific extensions to the `std::process::Command` builder
102+
#[unstable(feature = "windows_process_extensions", issue = "37827")]
103+
pub trait CommandExt {
104+
/// Sets the [process creation flags][1] to be passed to `CreateProcess`.
105+
///
106+
/// These will always be ORed with `CREATE_UNICODE_ENVIRONMENT`.
107+
/// [1]: https://msdn.microsoft.com/en-us/library/windows/desktop/ms684863(v=vs.85).aspx
108+
#[unstable(feature = "windows_process_extensions", issue = "37827")]
109+
fn creation_flags(&mut self, flags: u32) -> &mut process::Command;
110+
}
111+
112+
#[unstable(feature = "windows_process_extensions", issue = "37827")]
113+
impl CommandExt for process::Command {
114+
fn creation_flags(&mut self, flags: u32) -> &mut process::Command {
115+
self.as_inner_mut().creation_flags(flags);
116+
self
117+
}
118+
}

src/libstd/sys/windows/process.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ pub struct Command {
5454
args: Vec<OsString>,
5555
env: Option<HashMap<OsString, OsString>>,
5656
cwd: Option<OsString>,
57+
flags: u32,
5758
detach: bool, // not currently exposed in std::process
5859
stdin: Option<Stdio>,
5960
stdout: Option<Stdio>,
@@ -84,6 +85,7 @@ impl Command {
8485
args: Vec::new(),
8586
env: None,
8687
cwd: None,
88+
flags: 0,
8789
detach: false,
8890
stdin: None,
8991
stdout: None,
@@ -124,6 +126,9 @@ impl Command {
124126
pub fn stderr(&mut self, stderr: Stdio) {
125127
self.stderr = Some(stderr);
126128
}
129+
pub fn creation_flags(&mut self, flags: u32) {
130+
self.flags = flags;
131+
}
127132

128133
pub fn spawn(&mut self, default: Stdio, needs_stdin: bool)
129134
-> io::Result<(Process, StdioPipes)> {
@@ -157,7 +162,7 @@ impl Command {
157162
cmd_str.push(0); // add null terminator
158163

159164
// stolen from the libuv code.
160-
let mut flags = c::CREATE_UNICODE_ENVIRONMENT;
165+
let mut flags = self.flags | c::CREATE_UNICODE_ENVIRONMENT;
161166
if self.detach {
162167
flags |= c::DETACHED_PROCESS | c::CREATE_NEW_PROCESS_GROUP;
163168
}

0 commit comments

Comments
 (0)